Merge branch 'vendor/BINUTILS225'
[dragonfly.git] / sys / dev / netif / ath / ath_rate / sample / sample.c
1 /*-
2  * Copyright (c) 2005 John Bicket
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer,
10  *    without modification.
11  * 2. Redistributions in binary form must reproduce at minimum a disclaimer
12  *    similar to the "NO WARRANTY" disclaimer below ("Disclaimer") and any
13  *    redistribution must be conditioned upon including a substantially
14  *    similar Disclaimer requirement for further binary redistribution.
15  * 3. Neither the names of the above-listed copyright holders nor the names
16  *    of any contributors may be used to endorse or promote products derived
17  *    from this software without specific prior written permission.
18  *
19  * Alternatively, this software may be distributed under the terms of the
20  * GNU General Public License ("GPL") version 2 as published by the Free
21  * Software Foundation.
22  *
23  * NO WARRANTY
24  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
25  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
26  * LIMITED TO, THE IMPLIED WARRANTIES OF NONINFRINGEMENT, MERCHANTIBILITY
27  * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
28  * THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY,
29  * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
30  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
31  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
32  * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
33  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
34  * THE POSSIBILITY OF SUCH DAMAGES.
35  *
36  */
37
38 #include <sys/cdefs.h>
39 __FBSDID("$FreeBSD$");
40
41 /*
42  * John Bicket's SampleRate control algorithm.
43  */
44 #include "opt_ath.h"
45 #include "opt_inet.h"
46 #include "opt_wlan.h"
47 #include "opt_ah.h"
48
49 #include <sys/param.h>
50 #include <sys/systm.h> 
51 #include <sys/sysctl.h>
52 #include <sys/kernel.h>
53 #include <sys/lock.h>
54 #include <sys/malloc.h>
55 #include <sys/mutex.h>
56 #include <sys/errno.h>
57
58 #if defined(__DragonFly__)
59 /* empty */
60 #else
61 #include <machine/bus.h>
62 #include <machine/resource.h>
63 #endif
64 #include <sys/bus.h>
65
66 #include <sys/socket.h>
67  
68 #include <net/if.h>
69 #include <net/if_var.h>
70 #include <net/if_media.h>
71 #include <net/if_arp.h>
72 #include <net/ethernet.h>               /* XXX for ether_sprintf */
73
74 #include <netproto/802_11/ieee80211_var.h>
75
76 #include <net/bpf.h>
77
78 #ifdef INET
79 #include <netinet/in.h> 
80 #include <netinet/if_ether.h>
81 #endif
82
83 #include <dev/netif/ath/ath/if_athvar.h>
84 #include <dev/netif/ath/ath_rate/sample/sample.h>
85 #include <dev/netif/ath/ath_hal/ah_desc.h>
86 #include <dev/netif/ath/ath_rate/sample/tx_schedules.h>
87
88 #if defined(__DragonFly__)
89 extern const char* ath_hal_ether_sprintf(const uint8_t *mac);
90 #endif
91
92 /*
93  * This file is an implementation of the SampleRate algorithm
94  * in "Bit-rate Selection in Wireless Networks"
95  * (http://www.pdos.lcs.mit.edu/papers/jbicket-ms.ps)
96  *
97  * SampleRate chooses the bit-rate it predicts will provide the most
98  * throughput based on estimates of the expected per-packet
99  * transmission time for each bit-rate.  SampleRate periodically sends
100  * packets at bit-rates other than the current one to estimate when
101  * another bit-rate will provide better performance. SampleRate
102  * switches to another bit-rate when its estimated per-packet
103  * transmission time becomes smaller than the current bit-rate's.
104  * SampleRate reduces the number of bit-rates it must sample by
105  * eliminating those that could not perform better than the one
106  * currently being used.  SampleRate also stops probing at a bit-rate
107  * if it experiences several successive losses.
108  *
109  * The difference between the algorithm in the thesis and the one in this
110  * file is that the one in this file uses a ewma instead of a window.
111  *
112  * Also, this implementation tracks the average transmission time for
113  * a few different packet sizes independently for each link.
114  */
115
116 static void     ath_rate_ctl_reset(struct ath_softc *, struct ieee80211_node *);
117
118 static __inline int
119 size_to_bin(int size) 
120 {
121 #if NUM_PACKET_SIZE_BINS > 1
122         if (size <= packet_size_bins[0])
123                 return 0;
124 #endif
125 #if NUM_PACKET_SIZE_BINS > 2
126         if (size <= packet_size_bins[1])
127                 return 1;
128 #endif
129 #if NUM_PACKET_SIZE_BINS > 3
130         if (size <= packet_size_bins[2])
131                 return 2;
132 #endif
133 #if NUM_PACKET_SIZE_BINS > 4
134 #error "add support for more packet sizes"
135 #endif
136         return NUM_PACKET_SIZE_BINS-1;
137 }
138
139 void
140 ath_rate_node_init(struct ath_softc *sc, struct ath_node *an)
141 {
142         /* NB: assumed to be zero'd by caller */
143 }
144
145 void
146 ath_rate_node_cleanup(struct ath_softc *sc, struct ath_node *an)
147 {
148 }
149
150 static int
151 dot11rate(const HAL_RATE_TABLE *rt, int rix)
152 {
153         if (rix < 0)
154                 return -1;
155         return rt->info[rix].phy == IEEE80211_T_HT ?
156             rt->info[rix].dot11Rate : (rt->info[rix].dot11Rate & IEEE80211_RATE_VAL) / 2;
157 }
158
159 static const char *
160 dot11rate_label(const HAL_RATE_TABLE *rt, int rix)
161 {
162         if (rix < 0)
163                 return "";
164         return rt->info[rix].phy == IEEE80211_T_HT ? "MCS" : "Mb ";
165 }
166
167 /*
168  * Return the rix with the lowest average_tx_time,
169  * or -1 if all the average_tx_times are 0.
170  */
171 static __inline int
172 pick_best_rate(struct ath_node *an, const HAL_RATE_TABLE *rt,
173     int size_bin, int require_acked_before)
174 {
175         struct sample_node *sn = ATH_NODE_SAMPLE(an);
176         int best_rate_rix, best_rate_tt, best_rate_pct;
177         uint64_t mask;
178         int rix, tt, pct;
179
180         best_rate_rix = 0;
181         best_rate_tt = 0;
182         best_rate_pct = 0;
183         for (mask = sn->ratemask, rix = 0; mask != 0; mask >>= 1, rix++) {
184                 if ((mask & 1) == 0)            /* not a supported rate */
185                         continue;
186
187                 /* Don't pick a non-HT rate for a HT node */
188                 if ((an->an_node.ni_flags & IEEE80211_NODE_HT) &&
189                     (rt->info[rix].phy != IEEE80211_T_HT)) {
190                         continue;
191                 }
192
193                 tt = sn->stats[size_bin][rix].average_tx_time;
194                 if (tt <= 0 ||
195                     (require_acked_before &&
196                      !sn->stats[size_bin][rix].packets_acked))
197                         continue;
198
199                 /* Calculate percentage if possible */
200                 if (sn->stats[size_bin][rix].total_packets > 0) {
201                         pct = sn->stats[size_bin][rix].ewma_pct;
202                 } else {
203                         /* XXX for now, assume 95% ok */
204                         pct = 95;
205                 }
206
207                 /* don't use a bit-rate that has been failing */
208                 if (sn->stats[size_bin][rix].successive_failures > 3)
209                         continue;
210
211                 /*
212                  * For HT, Don't use a bit rate that is much more
213                  * lossy than the best.
214                  *
215                  * XXX this isn't optimal; it's just designed to
216                  * eliminate rates that are going to be obviously
217                  * worse.
218                  */
219                 if (an->an_node.ni_flags & IEEE80211_NODE_HT) {
220                         if (best_rate_pct > (pct + 50))
221                                 continue;
222                 }
223
224                 /*
225                  * For non-MCS rates, use the current average txtime for
226                  * comparison.
227                  */
228                 if (! (an->an_node.ni_flags & IEEE80211_NODE_HT)) {
229                         if (best_rate_tt == 0 || tt <= best_rate_tt) {
230                                 best_rate_tt = tt;
231                                 best_rate_rix = rix;
232                                 best_rate_pct = pct;
233                         }
234                 }
235
236                 /*
237                  * Since 2 stream rates have slightly higher TX times,
238                  * allow a little bit of leeway. This should later
239                  * be abstracted out and properly handled.
240                  */
241                 if (an->an_node.ni_flags & IEEE80211_NODE_HT) {
242                         if (best_rate_tt == 0 || (tt * 8 <= best_rate_tt * 10)) {
243                                 best_rate_tt = tt;
244                                 best_rate_rix = rix;
245                                 best_rate_pct = pct;
246                         }
247                 }
248         }
249         return (best_rate_tt ? best_rate_rix : -1);
250 }
251
252 /*
253  * Pick a good "random" bit-rate to sample other than the current one.
254  */
255 static __inline int
256 pick_sample_rate(struct sample_softc *ssc , struct ath_node *an,
257     const HAL_RATE_TABLE *rt, int size_bin)
258 {
259 #define DOT11RATE(ix)   (rt->info[ix].dot11Rate & IEEE80211_RATE_VAL)
260 #define MCS(ix)         (rt->info[ix].dot11Rate | IEEE80211_RATE_MCS)
261         struct sample_node *sn = ATH_NODE_SAMPLE(an);
262         int current_rix, rix;
263         unsigned current_tt;
264         uint64_t mask;
265         
266         current_rix = sn->current_rix[size_bin];
267         if (current_rix < 0) {
268                 /* no successes yet, send at the lowest bit-rate */
269                 /* XXX should return MCS0 if HT */
270                 return 0;
271         }
272
273         current_tt = sn->stats[size_bin][current_rix].average_tx_time;
274
275         rix = sn->last_sample_rix[size_bin]+1;  /* next sample rate */
276         mask = sn->ratemask &~ ((uint64_t) 1<<current_rix);/* don't sample current rate */
277         while (mask != 0) {
278                 if ((mask & ((uint64_t) 1<<rix)) == 0) {        /* not a supported rate */
279         nextrate:
280                         if (++rix >= rt->rateCount)
281                                 rix = 0;
282                         continue;
283                 }
284
285                 /*
286                  * The following code stops trying to sample
287                  * non-MCS rates when speaking to an MCS node.
288                  * However, at least for CCK rates in 2.4GHz mode,
289                  * the non-MCS rates MAY actually provide better
290                  * PER at the very far edge of reception.
291                  *
292                  * However! Until ath_rate_form_aggr() grows
293                  * some logic to not form aggregates if the
294                  * selected rate is non-MCS, this won't work.
295                  *
296                  * So don't disable this code until you've taught
297                  * ath_rate_form_aggr() to drop out if any of
298                  * the selected rates are non-MCS.
299                  */
300 #if 1
301                 /* if the node is HT and the rate isn't HT, don't bother sample */
302                 if ((an->an_node.ni_flags & IEEE80211_NODE_HT) &&
303                     (rt->info[rix].phy != IEEE80211_T_HT)) {
304                         mask &= ~((uint64_t) 1<<rix);
305                         goto nextrate;
306                 }
307 #endif
308
309                 /* this bit-rate is always worse than the current one */
310                 if (sn->stats[size_bin][rix].perfect_tx_time > current_tt) {
311                         mask &= ~((uint64_t) 1<<rix);
312                         goto nextrate;
313                 }
314
315                 /* rarely sample bit-rates that fail a lot */
316                 if (sn->stats[size_bin][rix].successive_failures > ssc->max_successive_failures &&
317                     ticks - sn->stats[size_bin][rix].last_tx < ssc->stale_failure_timeout) {
318                         mask &= ~((uint64_t) 1<<rix);
319                         goto nextrate;
320                 }
321
322                 /*
323                  * For HT, only sample a few rates on either side of the
324                  * current rix; there's quite likely a lot of them.
325                  */
326                 if (an->an_node.ni_flags & IEEE80211_NODE_HT) {
327                         if (rix < (current_rix - 3) ||
328                             rix > (current_rix + 3)) {
329                                 mask &= ~((uint64_t) 1<<rix);
330                                 goto nextrate;
331                         }
332                 }
333
334                 /* Don't sample more than 2 rates higher for rates > 11M for non-HT rates */
335                 if (! (an->an_node.ni_flags & IEEE80211_NODE_HT)) {
336                         if (DOT11RATE(rix) > 2*11 && rix > current_rix + 2) {
337                                 mask &= ~((uint64_t) 1<<rix);
338                                 goto nextrate;
339                         }
340                 }
341
342                 sn->last_sample_rix[size_bin] = rix;
343                 return rix;
344         }
345         return current_rix;
346 #undef DOT11RATE
347 #undef  MCS
348 }
349
350 static int
351 ath_rate_get_static_rix(struct ath_softc *sc, const struct ieee80211_node *ni)
352 {
353 #define RATE(_ix)       (ni->ni_rates.rs_rates[(_ix)] & IEEE80211_RATE_VAL)
354 #define DOT11RATE(_ix)  (rt->info[(_ix)].dot11Rate & IEEE80211_RATE_VAL)
355 #define MCS(_ix)        (ni->ni_htrates.rs_rates[_ix] | IEEE80211_RATE_MCS)
356         const struct ieee80211_txparam *tp = ni->ni_txparms;
357         int srate;
358
359         /* Check MCS rates */
360         for (srate = ni->ni_htrates.rs_nrates - 1; srate >= 0; srate--) {
361                 if (MCS(srate) == tp->ucastrate)
362                         return sc->sc_rixmap[tp->ucastrate];
363         }
364
365         /* Check legacy rates */
366         for (srate = ni->ni_rates.rs_nrates - 1; srate >= 0; srate--) {
367                 if (RATE(srate) == tp->ucastrate)
368                         return sc->sc_rixmap[tp->ucastrate];
369         }
370         return -1;
371 #undef  RATE
372 #undef  DOT11RATE
373 #undef  MCS
374 }
375
376 static void
377 ath_rate_update_static_rix(struct ath_softc *sc, struct ieee80211_node *ni)
378 {
379         struct ath_node *an = ATH_NODE(ni);
380         const struct ieee80211_txparam *tp = ni->ni_txparms;
381         struct sample_node *sn = ATH_NODE_SAMPLE(an);
382
383         if (tp != NULL && tp->ucastrate != IEEE80211_FIXED_RATE_NONE) {
384                 /*
385                  * A fixed rate is to be used; ucastrate is the IEEE code
386                  * for this rate (sans basic bit).  Check this against the
387                  * negotiated rate set for the node.  Note the fixed rate
388                  * may not be available for various reasons so we only
389                  * setup the static rate index if the lookup is successful.
390                  */
391                 sn->static_rix = ath_rate_get_static_rix(sc, ni);
392         } else {
393                 sn->static_rix = -1;
394         }
395 }
396
397 /*
398  * Pick a non-HT rate to begin using.
399  */
400 static int
401 ath_rate_pick_seed_rate_legacy(struct ath_softc *sc, struct ath_node *an,
402     int frameLen)
403 {
404 #define DOT11RATE(ix)   (rt->info[ix].dot11Rate & IEEE80211_RATE_VAL)
405 #define MCS(ix)         (rt->info[ix].dot11Rate | IEEE80211_RATE_MCS)
406 #define RATE(ix)        (DOT11RATE(ix) / 2)
407         int rix = -1;
408         const HAL_RATE_TABLE *rt = sc->sc_currates;
409         struct sample_node *sn = ATH_NODE_SAMPLE(an);
410         const int size_bin = size_to_bin(frameLen);
411
412         /* no packet has been sent successfully yet */
413         for (rix = rt->rateCount-1; rix > 0; rix--) {
414                 if ((sn->ratemask & ((uint64_t) 1<<rix)) == 0)
415                         continue;
416
417                 /* Skip HT rates */
418                 if (rt->info[rix].phy == IEEE80211_T_HT)
419                         continue;
420
421                 /*
422                  * Pick the highest rate <= 36 Mbps
423                  * that hasn't failed.
424                  */
425                 if (DOT11RATE(rix) <= 72 &&
426                     sn->stats[size_bin][rix].successive_failures == 0) {
427                         break;
428                 }
429         }
430         return rix;
431 #undef  RATE
432 #undef  MCS
433 #undef  DOT11RATE
434 }
435
436 /*
437  * Pick a HT rate to begin using.
438  *
439  * Don't use any non-HT rates; only consider HT rates.
440  */
441 static int
442 ath_rate_pick_seed_rate_ht(struct ath_softc *sc, struct ath_node *an,
443     int frameLen)
444 {
445 #define DOT11RATE(ix)   (rt->info[ix].dot11Rate & IEEE80211_RATE_VAL)
446 #define MCS(ix)         (rt->info[ix].dot11Rate | IEEE80211_RATE_MCS)
447 #define RATE(ix)        (DOT11RATE(ix) / 2)
448         int rix = -1, ht_rix = -1;
449         const HAL_RATE_TABLE *rt = sc->sc_currates;
450         struct sample_node *sn = ATH_NODE_SAMPLE(an);
451         const int size_bin = size_to_bin(frameLen);
452
453         /* no packet has been sent successfully yet */
454         for (rix = rt->rateCount-1; rix > 0; rix--) {
455                 /* Skip rates we can't use */
456                 if ((sn->ratemask & ((uint64_t) 1<<rix)) == 0)
457                         continue;
458
459                 /* Keep a copy of the last seen HT rate index */
460                 if (rt->info[rix].phy == IEEE80211_T_HT)
461                         ht_rix = rix;
462
463                 /* Skip non-HT rates */
464                 if (rt->info[rix].phy != IEEE80211_T_HT)
465                         continue;
466
467                 /*
468                  * Pick a medium-speed rate regardless of stream count
469                  * which has not seen any failures. Higher rates may fail;
470                  * we'll try them later.
471                  */
472                 if (((MCS(rix) & 0x7) <= 4) &&
473                     sn->stats[size_bin][rix].successive_failures == 0) {
474                         break;
475                 }
476         }
477
478         /*
479          * If all the MCS rates have successive failures, rix should be
480          * > 0; otherwise use the lowest MCS rix (hopefully MCS 0.)
481          */
482         return MAX(rix, ht_rix);
483 #undef  RATE
484 #undef  MCS
485 #undef  DOT11RATE
486 }
487
488
489 void
490 ath_rate_findrate(struct ath_softc *sc, struct ath_node *an,
491                   int shortPreamble, size_t frameLen,
492                   u_int8_t *rix0, int *try0, u_int8_t *txrate)
493 {
494 #define DOT11RATE(ix)   (rt->info[ix].dot11Rate & IEEE80211_RATE_VAL)
495 #define MCS(ix)         (rt->info[ix].dot11Rate | IEEE80211_RATE_MCS)
496 #define RATE(ix)        (DOT11RATE(ix) / 2)
497         struct sample_node *sn = ATH_NODE_SAMPLE(an);
498         struct sample_softc *ssc = ATH_SOFTC_SAMPLE(sc);
499         struct ieee80211com *ic = &sc->sc_ic;
500         const HAL_RATE_TABLE *rt = sc->sc_currates;
501         const int size_bin = size_to_bin(frameLen);
502         int rix, mrr, best_rix, change_rates;
503         unsigned average_tx_time;
504
505         ath_rate_update_static_rix(sc, &an->an_node);
506
507         if (sn->currates != sc->sc_currates) {
508                 device_printf(sc->sc_dev, "%s: currates != sc_currates!\n",
509                     __func__);
510                 rix = 0;
511                 *try0 = ATH_TXMAXTRY;
512                 goto done;
513         }
514
515         if (sn->static_rix != -1) {
516                 rix = sn->static_rix;
517                 *try0 = ATH_TXMAXTRY;
518                 goto done;
519         }
520
521         mrr = sc->sc_mrretry;
522         /* XXX check HT protmode too */
523         if (mrr && (ic->ic_flags & IEEE80211_F_USEPROT && !sc->sc_mrrprot))
524                 mrr = 0;
525
526         best_rix = pick_best_rate(an, rt, size_bin, !mrr);
527         if (best_rix >= 0) {
528                 average_tx_time = sn->stats[size_bin][best_rix].average_tx_time;
529         } else {
530                 average_tx_time = 0;
531         }
532         /*
533          * Limit the time measuring the performance of other tx
534          * rates to sample_rate% of the total transmission time.
535          */
536         if (sn->sample_tt[size_bin] < average_tx_time * (sn->packets_since_sample[size_bin]*ssc->sample_rate/100)) {
537                 rix = pick_sample_rate(ssc, an, rt, size_bin);
538                 IEEE80211_NOTE(an->an_node.ni_vap, IEEE80211_MSG_RATECTL,
539                      &an->an_node, "att %d sample_tt %d size %u sample rate %d %s current rate %d %s",
540                      average_tx_time,
541                      sn->sample_tt[size_bin],
542                      bin_to_size(size_bin),
543                      dot11rate(rt, rix),
544                      dot11rate_label(rt, rix),
545                      dot11rate(rt, sn->current_rix[size_bin]),
546                      dot11rate_label(rt, sn->current_rix[size_bin]));
547                 if (rix != sn->current_rix[size_bin]) {
548                         sn->current_sample_rix[size_bin] = rix;
549                 } else {
550                         sn->current_sample_rix[size_bin] = -1;
551                 }
552                 sn->packets_since_sample[size_bin] = 0;
553         } else {
554                 change_rates = 0;
555                 if (!sn->packets_sent[size_bin] || best_rix == -1) {
556                         /* no packet has been sent successfully yet */
557                         change_rates = 1;
558                         if (an->an_node.ni_flags & IEEE80211_NODE_HT)
559                                 best_rix =
560                                     ath_rate_pick_seed_rate_ht(sc, an, frameLen);
561                         else
562                                 best_rix =
563                                     ath_rate_pick_seed_rate_legacy(sc, an, frameLen);
564                 } else if (sn->packets_sent[size_bin] < 20) {
565                         /* let the bit-rate switch quickly during the first few packets */
566                         IEEE80211_NOTE(an->an_node.ni_vap,
567                             IEEE80211_MSG_RATECTL, &an->an_node,
568                             "%s: switching quickly..", __func__);
569                         change_rates = 1;
570                 } else if (ticks - ssc->min_switch > sn->ticks_since_switch[size_bin]) {
571                         /* min_switch seconds have gone by */
572                         IEEE80211_NOTE(an->an_node.ni_vap,
573                             IEEE80211_MSG_RATECTL, &an->an_node,
574                             "%s: min_switch %d > ticks_since_switch %d..",
575                             __func__, ticks - ssc->min_switch, sn->ticks_since_switch[size_bin]);
576                         change_rates = 1;
577                 } else if ((! (an->an_node.ni_flags & IEEE80211_NODE_HT)) &&
578                     (2*average_tx_time < sn->stats[size_bin][sn->current_rix[size_bin]].average_tx_time)) {
579                         /* the current bit-rate is twice as slow as the best one */
580                         IEEE80211_NOTE(an->an_node.ni_vap,
581                             IEEE80211_MSG_RATECTL, &an->an_node,
582                             "%s: 2x att (= %d) < cur_rix att %d",
583                             __func__,
584                             2 * average_tx_time, sn->stats[size_bin][sn->current_rix[size_bin]].average_tx_time);
585                         change_rates = 1;
586                 } else if ((an->an_node.ni_flags & IEEE80211_NODE_HT)) {
587                         int cur_rix = sn->current_rix[size_bin];
588                         int cur_att = sn->stats[size_bin][cur_rix].average_tx_time;
589                         /*
590                          * If the node is HT, upgrade it if the MCS rate is
591                          * higher and the average tx time is within 20% of
592                          * the current rate. It can fail a little.
593                          *
594                          * This is likely not optimal!
595                          */
596 #if 0
597                         kprintf("cur rix/att %x/%d, best rix/att %x/%d\n",
598                             MCS(cur_rix), cur_att, MCS(best_rix), average_tx_time);
599 #endif
600                         if ((MCS(best_rix) > MCS(cur_rix)) &&
601                             (average_tx_time * 8) <= (cur_att * 10)) {
602                                 IEEE80211_NOTE(an->an_node.ni_vap,
603                                     IEEE80211_MSG_RATECTL, &an->an_node,
604                                     "%s: HT: best_rix 0x%d > cur_rix 0x%x, average_tx_time %d, cur_att %d",
605                                     __func__,
606                                     MCS(best_rix), MCS(cur_rix), average_tx_time, cur_att);
607                                 change_rates = 1;
608                         }
609                 }
610
611                 sn->packets_since_sample[size_bin]++;
612                 
613                 if (change_rates) {
614                         if (best_rix != sn->current_rix[size_bin]) {
615                                 IEEE80211_NOTE(an->an_node.ni_vap,
616                                     IEEE80211_MSG_RATECTL,
617                                     &an->an_node,
618 "%s: size %d switch rate %d (%d/%d) -> %d (%d/%d) after %d packets mrr %d",
619                                     __func__,
620                                     bin_to_size(size_bin),
621                                     RATE(sn->current_rix[size_bin]),
622                                     sn->stats[size_bin][sn->current_rix[size_bin]].average_tx_time,
623                                     sn->stats[size_bin][sn->current_rix[size_bin]].perfect_tx_time,
624                                     RATE(best_rix),
625                                     sn->stats[size_bin][best_rix].average_tx_time,
626                                     sn->stats[size_bin][best_rix].perfect_tx_time,
627                                     sn->packets_since_switch[size_bin],
628                                     mrr);
629                         }
630                         sn->packets_since_switch[size_bin] = 0;
631                         sn->current_rix[size_bin] = best_rix;
632                         sn->ticks_since_switch[size_bin] = ticks;
633                         /* 
634                          * Set the visible txrate for this node.
635                          */
636                         an->an_node.ni_txrate = (rt->info[best_rix].phy == IEEE80211_T_HT) ?  MCS(best_rix) : DOT11RATE(best_rix);
637                 }
638                 rix = sn->current_rix[size_bin];
639                 sn->packets_since_switch[size_bin]++;
640         }
641         *try0 = mrr ? sn->sched[rix].t0 : ATH_TXMAXTRY;
642 done:
643
644         /*
645          * This bug totally sucks and should be fixed.
646          *
647          * For now though, let's not panic, so we can start to figure
648          * out how to better reproduce it.
649          */
650         if (rix < 0 || rix >= rt->rateCount) {
651                 kprintf("%s: ERROR: rix %d out of bounds (rateCount=%d)\n",
652                     __func__,
653                     rix,
654                     rt->rateCount);
655                     rix = 0;    /* XXX just default for now */
656         }
657         KASSERT(rix >= 0 && rix < rt->rateCount, ("rix is %d", rix));
658
659         *rix0 = rix;
660         *txrate = rt->info[rix].rateCode
661                 | (shortPreamble ? rt->info[rix].shortPreamble : 0);
662         sn->packets_sent[size_bin]++;
663 #undef DOT11RATE
664 #undef MCS
665 #undef RATE
666 }
667
668 /*
669  * Get the TX rates. Don't fiddle with short preamble flags for them;
670  * the caller can do that.
671  */
672 void
673 ath_rate_getxtxrates(struct ath_softc *sc, struct ath_node *an,
674     uint8_t rix0, struct ath_rc_series *rc)
675 {
676         struct sample_node *sn = ATH_NODE_SAMPLE(an);
677         const struct txschedule *sched = &sn->sched[rix0];
678
679         KASSERT(rix0 == sched->r0, ("rix0 (%x) != sched->r0 (%x)!\n",
680             rix0, sched->r0));
681
682         rc[0].flags = rc[1].flags = rc[2].flags = rc[3].flags = 0;
683
684         rc[0].rix = sched->r0;
685         rc[1].rix = sched->r1;
686         rc[2].rix = sched->r2;
687         rc[3].rix = sched->r3;
688
689         rc[0].tries = sched->t0;
690         rc[1].tries = sched->t1;
691         rc[2].tries = sched->t2;
692         rc[3].tries = sched->t3;
693 }
694
695 void
696 ath_rate_setupxtxdesc(struct ath_softc *sc, struct ath_node *an,
697                       struct ath_desc *ds, int shortPreamble, u_int8_t rix)
698 {
699         struct sample_node *sn = ATH_NODE_SAMPLE(an);
700         const struct txschedule *sched = &sn->sched[rix];
701         const HAL_RATE_TABLE *rt = sc->sc_currates;
702         uint8_t rix1, s1code, rix2, s2code, rix3, s3code;
703
704         /* XXX precalculate short preamble tables */
705         rix1 = sched->r1;
706         s1code = rt->info[rix1].rateCode
707                | (shortPreamble ? rt->info[rix1].shortPreamble : 0);
708         rix2 = sched->r2;
709         s2code = rt->info[rix2].rateCode
710                | (shortPreamble ? rt->info[rix2].shortPreamble : 0);
711         rix3 = sched->r3;
712         s3code = rt->info[rix3].rateCode
713                | (shortPreamble ? rt->info[rix3].shortPreamble : 0);
714         ath_hal_setupxtxdesc(sc->sc_ah, ds,
715             s1code, sched->t1,          /* series 1 */
716             s2code, sched->t2,          /* series 2 */
717             s3code, sched->t3);         /* series 3 */
718 }
719
720 static void
721 update_stats(struct ath_softc *sc, struct ath_node *an, 
722                   int frame_size,
723                   int rix0, int tries0,
724                   int rix1, int tries1,
725                   int rix2, int tries2,
726                   int rix3, int tries3,
727                   int short_tries, int tries, int status,
728                   int nframes, int nbad)
729 {
730         struct sample_node *sn = ATH_NODE_SAMPLE(an);
731         struct sample_softc *ssc = ATH_SOFTC_SAMPLE(sc);
732 #ifdef IEEE80211_DEBUG
733         const HAL_RATE_TABLE *rt = sc->sc_currates;
734 #endif
735         const int size_bin = size_to_bin(frame_size);
736         const int size = bin_to_size(size_bin);
737         int tt, tries_so_far;
738         int is_ht40 = (an->an_node.ni_chw == 40);
739         int pct;
740
741         if (!IS_RATE_DEFINED(sn, rix0))
742                 return;
743         tt = calc_usecs_unicast_packet(sc, size, rix0, short_tries,
744                 MIN(tries0, tries) - 1, is_ht40);
745         tries_so_far = tries0;
746
747         if (tries1 && tries_so_far < tries) {
748                 if (!IS_RATE_DEFINED(sn, rix1))
749                         return;
750                 tt += calc_usecs_unicast_packet(sc, size, rix1, short_tries,
751                         MIN(tries1 + tries_so_far, tries) - tries_so_far - 1, is_ht40);
752                 tries_so_far += tries1;
753         }
754
755         if (tries2 && tries_so_far < tries) {
756                 if (!IS_RATE_DEFINED(sn, rix2))
757                         return;
758                 tt += calc_usecs_unicast_packet(sc, size, rix2, short_tries,
759                         MIN(tries2 + tries_so_far, tries) - tries_so_far - 1, is_ht40);
760                 tries_so_far += tries2;
761         }
762
763         if (tries3 && tries_so_far < tries) {
764                 if (!IS_RATE_DEFINED(sn, rix3))
765                         return;
766                 tt += calc_usecs_unicast_packet(sc, size, rix3, short_tries,
767                         MIN(tries3 + tries_so_far, tries) - tries_so_far - 1, is_ht40);
768         }
769
770         if (sn->stats[size_bin][rix0].total_packets < ssc->smoothing_minpackets) {
771                 /* just average the first few packets */
772                 int avg_tx = sn->stats[size_bin][rix0].average_tx_time;
773                 int packets = sn->stats[size_bin][rix0].total_packets;
774                 sn->stats[size_bin][rix0].average_tx_time = (tt+(avg_tx*packets))/(packets+nframes);
775         } else {
776                 /* use a ewma */
777                 sn->stats[size_bin][rix0].average_tx_time = 
778                         ((sn->stats[size_bin][rix0].average_tx_time * ssc->smoothing_rate) + 
779                          (tt * (100 - ssc->smoothing_rate))) / 100;
780         }
781         
782         /*
783          * XXX Don't mark the higher bit rates as also having failed; as this
784          * unfortunately stops those rates from being tasted when trying to
785          * TX. This happens with 11n aggregation.
786          */
787         if (nframes == nbad) {
788 #if 0
789                 int y;
790 #endif
791                 sn->stats[size_bin][rix0].successive_failures += nbad;
792 #if 0
793                 for (y = size_bin+1; y < NUM_PACKET_SIZE_BINS; y++) {
794                         /*
795                          * Also say larger packets failed since we
796                          * assume if a small packet fails at a
797                          * bit-rate then a larger one will also.
798                          */
799                         sn->stats[y][rix0].successive_failures += nbad;
800                         sn->stats[y][rix0].last_tx = ticks;
801                         sn->stats[y][rix0].tries += tries;
802                         sn->stats[y][rix0].total_packets += nframes;
803                 }
804 #endif
805         } else {
806                 sn->stats[size_bin][rix0].packets_acked += (nframes - nbad);
807                 sn->stats[size_bin][rix0].successive_failures = 0;
808         }
809         sn->stats[size_bin][rix0].tries += tries;
810         sn->stats[size_bin][rix0].last_tx = ticks;
811         sn->stats[size_bin][rix0].total_packets += nframes;
812
813         /* update EWMA for this rix */
814
815         /* Calculate percentage based on current rate */
816         if (nframes == 0)
817                 nframes = nbad = 1;
818         pct = ((nframes - nbad) * 1000) / nframes;
819
820         if (sn->stats[size_bin][rix0].total_packets <
821             ssc->smoothing_minpackets) {
822                 /* just average the first few packets */
823                 int a_pct = (sn->stats[size_bin][rix0].packets_acked * 1000) /
824                     (sn->stats[size_bin][rix0].total_packets);
825                 sn->stats[size_bin][rix0].ewma_pct = a_pct;
826         } else {
827                 /* use a ewma */
828                 sn->stats[size_bin][rix0].ewma_pct =
829                         ((sn->stats[size_bin][rix0].ewma_pct * ssc->smoothing_rate) +
830                          (pct * (100 - ssc->smoothing_rate))) / 100;
831         }
832
833
834         if (rix0 == sn->current_sample_rix[size_bin]) {
835                 IEEE80211_NOTE(an->an_node.ni_vap, IEEE80211_MSG_RATECTL,
836                    &an->an_node,
837 "%s: size %d %s sample rate %d %s tries (%d/%d) tt %d avg_tt (%d/%d) nfrm %d nbad %d", 
838                     __func__, 
839                     size,
840                     status ? "FAIL" : "OK",
841                     dot11rate(rt, rix0),
842                     dot11rate_label(rt, rix0),
843                     short_tries, tries, tt, 
844                     sn->stats[size_bin][rix0].average_tx_time,
845                     sn->stats[size_bin][rix0].perfect_tx_time,
846                     nframes, nbad);
847                 sn->sample_tt[size_bin] = tt;
848                 sn->current_sample_rix[size_bin] = -1;
849         }
850 }
851
852 static void
853 badrate(struct ath_softc *sc, int series, int hwrate, int tries, int status)
854 {
855
856         device_printf(sc->sc_dev,
857             "bad series%d hwrate 0x%x, tries %u ts_status 0x%x\n",
858             series, hwrate, tries, status);
859 }
860
861 void
862 ath_rate_tx_complete(struct ath_softc *sc, struct ath_node *an,
863         const struct ath_rc_series *rc, const struct ath_tx_status *ts,
864         int frame_size, int nframes, int nbad)
865 {
866         struct ieee80211com *ic = &sc->sc_ic;
867         struct sample_node *sn = ATH_NODE_SAMPLE(an);
868         int final_rix, short_tries, long_tries;
869         const HAL_RATE_TABLE *rt = sc->sc_currates;
870         int status = ts->ts_status;
871         int mrr;
872
873         final_rix = rt->rateCodeToIndex[ts->ts_rate];
874         short_tries = ts->ts_shortretry;
875         long_tries = ts->ts_longretry + 1;
876
877         if (nframes == 0) {
878                 device_printf(sc->sc_dev, "%s: nframes=0?\n", __func__);
879                 return;
880         }
881
882         if (frame_size == 0)                /* NB: should not happen */
883                 frame_size = 1500;
884
885         if (sn->ratemask == 0) {
886                 IEEE80211_NOTE(an->an_node.ni_vap, IEEE80211_MSG_RATECTL,
887                     &an->an_node,
888                     "%s: size %d %s rate/try %d/%d no rates yet", 
889                     __func__,
890                     bin_to_size(size_to_bin(frame_size)),
891                     status ? "FAIL" : "OK",
892                     short_tries, long_tries);
893                 return;
894         }
895         mrr = sc->sc_mrretry;
896         /* XXX check HT protmode too */
897         if (mrr && (ic->ic_flags & IEEE80211_F_USEPROT && !sc->sc_mrrprot))
898                 mrr = 0;
899
900         if (!mrr || ts->ts_finaltsi == 0) {
901                 if (!IS_RATE_DEFINED(sn, final_rix)) {
902                         device_printf(sc->sc_dev,
903                             "%s: ts_rate=%d ts_finaltsi=%d, final_rix=%d\n",
904                             __func__, ts->ts_rate, ts->ts_finaltsi, final_rix);
905                         badrate(sc, 0, ts->ts_rate, long_tries, status);
906                         return;
907                 }
908                 /*
909                  * Only one rate was used; optimize work.
910                  */
911                 IEEE80211_NOTE(an->an_node.ni_vap, IEEE80211_MSG_RATECTL,
912                      &an->an_node, "%s: size %d (%d bytes) %s rate/short/long %d %s/%d/%d nframes/nbad [%d/%d]",
913                      __func__,
914                      bin_to_size(size_to_bin(frame_size)),
915                      frame_size,
916                      status ? "FAIL" : "OK",
917                      dot11rate(rt, final_rix), dot11rate_label(rt, final_rix),
918                      short_tries, long_tries, nframes, nbad);
919                 update_stats(sc, an, frame_size, 
920                              final_rix, long_tries,
921                              0, 0,
922                              0, 0,
923                              0, 0,
924                              short_tries, long_tries, status,
925                              nframes, nbad);
926
927         } else {
928                 int finalTSIdx = ts->ts_finaltsi;
929                 int i;
930
931                 /*
932                  * Process intermediate rates that failed.
933                  */
934
935                 IEEE80211_NOTE(an->an_node.ni_vap, IEEE80211_MSG_RATECTL,
936                     &an->an_node,
937 "%s: size %d (%d bytes) finaltsidx %d short %d long %d %s rate/try [%d %s/%d %d %s/%d %d %s/%d %d %s/%d] nframes/nbad [%d/%d]", 
938                      __func__,
939                      bin_to_size(size_to_bin(frame_size)),
940                      frame_size,
941                      finalTSIdx,
942                      short_tries,
943                      long_tries,
944                      status ? "FAIL" : "OK",
945                      dot11rate(rt, rc[0].rix),
946                       dot11rate_label(rt, rc[0].rix), rc[0].tries,
947                      dot11rate(rt, rc[1].rix),
948                       dot11rate_label(rt, rc[1].rix), rc[1].tries,
949                      dot11rate(rt, rc[2].rix),
950                       dot11rate_label(rt, rc[2].rix), rc[2].tries,
951                      dot11rate(rt, rc[3].rix),
952                       dot11rate_label(rt, rc[3].rix), rc[3].tries,
953                      nframes, nbad);
954
955                 for (i = 0; i < 4; i++) {
956                         if (rc[i].tries && !IS_RATE_DEFINED(sn, rc[i].rix))
957                                 badrate(sc, 0, rc[i].ratecode, rc[i].tries,
958                                     status);
959                 }
960
961                 /*
962                  * NB: series > 0 are not penalized for failure
963                  * based on the try counts under the assumption
964                  * that losses are often bursty and since we
965                  * sample higher rates 1 try at a time doing so
966                  * may unfairly penalize them.
967                  */
968                 if (rc[0].tries) {
969                         update_stats(sc, an, frame_size,
970                                      rc[0].rix, rc[0].tries,
971                                      rc[1].rix, rc[1].tries,
972                                      rc[2].rix, rc[2].tries,
973                                      rc[3].rix, rc[3].tries,
974                                      short_tries, long_tries,
975                                      long_tries > rc[0].tries,
976                                      nframes, nbad);
977                         long_tries -= rc[0].tries;
978                 }
979                 
980                 if (rc[1].tries && finalTSIdx > 0) {
981                         update_stats(sc, an, frame_size,
982                                      rc[1].rix, rc[1].tries,
983                                      rc[2].rix, rc[2].tries,
984                                      rc[3].rix, rc[3].tries,
985                                      0, 0,
986                                      short_tries, long_tries,
987                                      status,
988                                      nframes, nbad);
989                         long_tries -= rc[1].tries;
990                 }
991
992                 if (rc[2].tries && finalTSIdx > 1) {
993                         update_stats(sc, an, frame_size,
994                                      rc[2].rix, rc[2].tries,
995                                      rc[3].rix, rc[3].tries,
996                                      0, 0,
997                                      0, 0,
998                                      short_tries, long_tries,
999                                      status,
1000                                      nframes, nbad);
1001                         long_tries -= rc[2].tries;
1002                 }
1003
1004                 if (rc[3].tries && finalTSIdx > 2) {
1005                         update_stats(sc, an, frame_size,
1006                                      rc[3].rix, rc[3].tries,
1007                                      0, 0,
1008                                      0, 0,
1009                                      0, 0,
1010                                      short_tries, long_tries,
1011                                      status,
1012                                      nframes, nbad);
1013                 }
1014         }
1015 }
1016
1017 void
1018 ath_rate_newassoc(struct ath_softc *sc, struct ath_node *an, int isnew)
1019 {
1020         if (isnew)
1021                 ath_rate_ctl_reset(sc, &an->an_node);
1022 }
1023
1024 static const struct txschedule *mrr_schedules[IEEE80211_MODE_MAX+2] = {
1025         NULL,           /* IEEE80211_MODE_AUTO */
1026         series_11a,     /* IEEE80211_MODE_11A */
1027         series_11g,     /* IEEE80211_MODE_11B */
1028         series_11g,     /* IEEE80211_MODE_11G */
1029         NULL,           /* IEEE80211_MODE_FH */
1030         series_11a,     /* IEEE80211_MODE_TURBO_A */
1031         series_11g,     /* IEEE80211_MODE_TURBO_G */
1032         series_11a,     /* IEEE80211_MODE_STURBO_A */
1033         series_11na,    /* IEEE80211_MODE_11NA */
1034         series_11ng,    /* IEEE80211_MODE_11NG */
1035         series_half,    /* IEEE80211_MODE_HALF */
1036         series_quarter, /* IEEE80211_MODE_QUARTER */
1037 };
1038
1039 /*
1040  * Initialize the tables for a node.
1041  */
1042 static void
1043 ath_rate_ctl_reset(struct ath_softc *sc, struct ieee80211_node *ni)
1044 {
1045 #define RATE(_ix)       (ni->ni_rates.rs_rates[(_ix)] & IEEE80211_RATE_VAL)
1046 #define DOT11RATE(_ix)  (rt->info[(_ix)].dot11Rate & IEEE80211_RATE_VAL)
1047 #define MCS(_ix)        (ni->ni_htrates.rs_rates[_ix] | IEEE80211_RATE_MCS)
1048         struct ath_node *an = ATH_NODE(ni);
1049         struct sample_node *sn = ATH_NODE_SAMPLE(an);
1050         const HAL_RATE_TABLE *rt = sc->sc_currates;
1051         int x, y, rix;
1052
1053         KASSERT(rt != NULL, ("no rate table, mode %u", sc->sc_curmode));
1054
1055         KASSERT(sc->sc_curmode < IEEE80211_MODE_MAX+2,
1056             ("curmode %u", sc->sc_curmode));
1057
1058         sn->sched = mrr_schedules[sc->sc_curmode];
1059         KASSERT(sn->sched != NULL,
1060             ("no mrr schedule for mode %u", sc->sc_curmode));
1061
1062         sn->static_rix = -1;
1063         ath_rate_update_static_rix(sc, ni);
1064
1065         sn->currates = sc->sc_currates;
1066
1067         /*
1068          * Construct a bitmask of usable rates.  This has all
1069          * negotiated rates minus those marked by the hal as
1070          * to be ignored for doing rate control.
1071          */
1072         sn->ratemask = 0;
1073         /* MCS rates */
1074         if (ni->ni_flags & IEEE80211_NODE_HT) {
1075                 for (x = 0; x < ni->ni_htrates.rs_nrates; x++) {
1076                         rix = sc->sc_rixmap[MCS(x)];
1077                         if (rix == 0xff)
1078                                 continue;
1079                         /* skip rates marked broken by hal */
1080                         if (!rt->info[rix].valid)
1081                                 continue;
1082                         KASSERT(rix < SAMPLE_MAXRATES,
1083                             ("mcs %u has rix %d", MCS(x), rix));
1084                         sn->ratemask |= (uint64_t) 1<<rix;
1085                 }
1086         }
1087
1088         /* Legacy rates */
1089         for (x = 0; x < ni->ni_rates.rs_nrates; x++) {
1090                 rix = sc->sc_rixmap[RATE(x)];
1091                 if (rix == 0xff)
1092                         continue;
1093                 /* skip rates marked broken by hal */
1094                 if (!rt->info[rix].valid)
1095                         continue;
1096                 KASSERT(rix < SAMPLE_MAXRATES,
1097                     ("rate %u has rix %d", RATE(x), rix));
1098                 sn->ratemask |= (uint64_t) 1<<rix;
1099         }
1100 #ifdef IEEE80211_DEBUG
1101         if (ieee80211_msg(ni->ni_vap, IEEE80211_MSG_RATECTL)) {
1102                 uint64_t mask;
1103
1104 #if defined(__DragonFly__)
1105                 ieee80211_note(ni->ni_vap, "[%s] %s: size 1600 rate/tt",
1106                     ath_hal_ether_sprintf(ni->ni_macaddr), __func__);
1107 #else
1108                 ieee80211_note(ni->ni_vap, "[%6D] %s: size 1600 rate/tt",
1109                     ni->ni_macaddr, ":", __func__);
1110 #endif
1111                 for (mask = sn->ratemask, rix = 0; mask != 0; mask >>= 1, rix++) {
1112                         if ((mask & 1) == 0)
1113                                 continue;
1114                         kprintf(" %d %s/%d", dot11rate(rt, rix), dot11rate_label(rt, rix),
1115                             calc_usecs_unicast_packet(sc, 1600, rix, 0,0,
1116                                 (ni->ni_chw == 40)));
1117                 }
1118                 kprintf("\n");
1119         }
1120 #endif
1121         for (y = 0; y < NUM_PACKET_SIZE_BINS; y++) {
1122                 int size = bin_to_size(y);
1123                 uint64_t mask;
1124
1125                 sn->packets_sent[y] = 0;
1126                 sn->current_sample_rix[y] = -1;
1127                 sn->last_sample_rix[y] = 0;
1128                 /* XXX start with first valid rate */
1129                 sn->current_rix[y] = ffs(sn->ratemask)-1;
1130                 
1131                 /*
1132                  * Initialize the statistics buckets; these are
1133                  * indexed by the rate code index.
1134                  */
1135                 for (rix = 0, mask = sn->ratemask; mask != 0; rix++, mask >>= 1) {
1136                         if ((mask & 1) == 0)            /* not a valid rate */
1137                                 continue;
1138                         sn->stats[y][rix].successive_failures = 0;
1139                         sn->stats[y][rix].tries = 0;
1140                         sn->stats[y][rix].total_packets = 0;
1141                         sn->stats[y][rix].packets_acked = 0;
1142                         sn->stats[y][rix].last_tx = 0;
1143                         sn->stats[y][rix].ewma_pct = 0;
1144                         
1145                         sn->stats[y][rix].perfect_tx_time =
1146                             calc_usecs_unicast_packet(sc, size, rix, 0, 0,
1147                             (ni->ni_chw == 40));
1148                         sn->stats[y][rix].average_tx_time =
1149                             sn->stats[y][rix].perfect_tx_time;
1150                 }
1151         }
1152 #if 0
1153         /* XXX 0, num_rates-1 are wrong */
1154         IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_RATECTL, ni,
1155             "%s: %d rates %d%sMbps (%dus)- %d%sMbps (%dus)", __func__, 
1156             sn->num_rates,
1157             DOT11RATE(0)/2, DOT11RATE(0) % 1 ? ".5" : "",
1158             sn->stats[1][0].perfect_tx_time,
1159             DOT11RATE(sn->num_rates-1)/2, DOT11RATE(sn->num_rates-1) % 1 ? ".5" : "",
1160             sn->stats[1][sn->num_rates-1].perfect_tx_time
1161         );
1162 #endif
1163         /* set the visible bit-rate */
1164         if (sn->static_rix != -1)
1165                 ni->ni_txrate = DOT11RATE(sn->static_rix);
1166         else
1167                 ni->ni_txrate = RATE(0);
1168 #undef RATE
1169 #undef DOT11RATE
1170 }
1171
1172 /*
1173  * Fetch the statistics for the given node.
1174  *
1175  * The ieee80211 node must be referenced and unlocked, however the ath_node
1176  * must be locked.
1177  *
1178  * The main difference here is that we convert the rate indexes
1179  * to 802.11 rates, or the userland output won't make much sense
1180  * as it has no access to the rix table.
1181  */
1182 int
1183 ath_rate_fetch_node_stats(struct ath_softc *sc, struct ath_node *an,
1184     struct ath_rateioctl *rs)
1185 {
1186         struct sample_node *sn = ATH_NODE_SAMPLE(an);
1187         const HAL_RATE_TABLE *rt = sc->sc_currates;
1188         struct ath_rateioctl_tlv av;
1189         struct ath_rateioctl_rt *tv;
1190         int y;
1191         int o = 0;
1192
1193         ATH_NODE_LOCK_ASSERT(an);
1194
1195         /*
1196          * Ensure there's enough space for the statistics.
1197          */
1198         if (rs->len <
1199             sizeof(struct ath_rateioctl_tlv) +
1200             sizeof(struct ath_rateioctl_rt) +
1201             sizeof(struct ath_rateioctl_tlv) +
1202             sizeof(struct sample_node)) {
1203                 device_printf(sc->sc_dev, "%s: len=%d, too short\n",
1204                     __func__,
1205                     rs->len);
1206                 return (EINVAL);
1207         }
1208
1209         /*
1210          * Take a temporary copy of the sample node state so we can
1211          * modify it before we copy it.
1212          */
1213 #if defined(__DragonFly__)
1214         tv = kmalloc(sizeof(struct ath_rateioctl_rt), M_TEMP,
1215                 M_INTWAIT | M_ZERO);
1216 #else
1217         tv = malloc(sizeof(struct ath_rateioctl_rt), M_TEMP,
1218                 M_NOWAIT | M_ZERO);
1219 #endif
1220         if (tv == NULL) {
1221                 return (ENOMEM);
1222         }
1223
1224         /*
1225          * Populate the rate table mapping TLV.
1226          */
1227         tv->nentries = rt->rateCount;
1228         for (y = 0; y < rt->rateCount; y++) {
1229                 tv->ratecode[y] = rt->info[y].dot11Rate & IEEE80211_RATE_VAL;
1230                 if (rt->info[y].phy == IEEE80211_T_HT)
1231                         tv->ratecode[y] |= IEEE80211_RATE_MCS;
1232         }
1233
1234         o = 0;
1235         /*
1236          * First TLV - rate code mapping
1237          */
1238         av.tlv_id = ATH_RATE_TLV_RATETABLE;
1239         av.tlv_len = sizeof(struct ath_rateioctl_rt);
1240         copyout(&av, rs->buf + o, sizeof(struct ath_rateioctl_tlv));
1241         o += sizeof(struct ath_rateioctl_tlv);
1242         copyout(tv, rs->buf + o, sizeof(struct ath_rateioctl_rt));
1243         o += sizeof(struct ath_rateioctl_rt);
1244
1245         /*
1246          * Second TLV - sample node statistics
1247          */
1248         av.tlv_id = ATH_RATE_TLV_SAMPLENODE;
1249         av.tlv_len = sizeof(struct sample_node);
1250         copyout(&av, rs->buf + o, sizeof(struct ath_rateioctl_tlv));
1251         o += sizeof(struct ath_rateioctl_tlv);
1252
1253         /*
1254          * Copy the statistics over to the provided buffer.
1255          */
1256         copyout(sn, rs->buf + o, sizeof(struct sample_node));
1257         o += sizeof(struct sample_node);
1258
1259         kfree(tv, M_TEMP);
1260
1261         return (0);
1262 }
1263
1264 static void
1265 sample_stats(void *arg, struct ieee80211_node *ni)
1266 {
1267         struct ath_softc *sc = arg;
1268         const HAL_RATE_TABLE *rt = sc->sc_currates;
1269         struct sample_node *sn = ATH_NODE_SAMPLE(ATH_NODE(ni));
1270         uint64_t mask;
1271         int rix, y;
1272
1273         kprintf("\n[%s] refcnt %d static_rix (%d %s) ratemask 0x%jx\n",
1274             ether_sprintf(ni->ni_macaddr), ieee80211_node_refcnt(ni),
1275             dot11rate(rt, sn->static_rix),
1276             dot11rate_label(rt, sn->static_rix),
1277             (uintmax_t)sn->ratemask);
1278         for (y = 0; y < NUM_PACKET_SIZE_BINS; y++) {
1279                 kprintf("[%4u] cur rix %d (%d %s) since switch: packets %d ticks %u\n",
1280                     bin_to_size(y), sn->current_rix[y],
1281                     dot11rate(rt, sn->current_rix[y]),
1282                     dot11rate_label(rt, sn->current_rix[y]),
1283                     sn->packets_since_switch[y], sn->ticks_since_switch[y]);
1284                 kprintf("[%4u] last sample (%d %s) cur sample (%d %s) packets sent %d\n",
1285                     bin_to_size(y),
1286                     dot11rate(rt, sn->last_sample_rix[y]),
1287                     dot11rate_label(rt, sn->last_sample_rix[y]),
1288                     dot11rate(rt, sn->current_sample_rix[y]),
1289                     dot11rate_label(rt, sn->current_sample_rix[y]),
1290                     sn->packets_sent[y]);
1291                 kprintf("[%4u] packets since sample %d sample tt %u\n",
1292                     bin_to_size(y), sn->packets_since_sample[y],
1293                     sn->sample_tt[y]);
1294         }
1295         for (mask = sn->ratemask, rix = 0; mask != 0; mask >>= 1, rix++) {
1296                 if ((mask & 1) == 0)
1297                                 continue;
1298                 for (y = 0; y < NUM_PACKET_SIZE_BINS; y++) {
1299                         if (sn->stats[y][rix].total_packets == 0)
1300                                 continue;
1301                         kprintf("[%2u %s:%4u] %8ju:%-8ju (%3d%%) (EWMA %3d.%1d%%) T %8ju F %4d avg %5u last %u\n",
1302                             dot11rate(rt, rix), dot11rate_label(rt, rix),
1303                             bin_to_size(y),
1304                             (uintmax_t) sn->stats[y][rix].total_packets,
1305                             (uintmax_t) sn->stats[y][rix].packets_acked,
1306                             (int) ((sn->stats[y][rix].packets_acked * 100ULL) /
1307                              sn->stats[y][rix].total_packets),
1308                             sn->stats[y][rix].ewma_pct / 10,
1309                             sn->stats[y][rix].ewma_pct % 10,
1310                             (uintmax_t) sn->stats[y][rix].tries,
1311                             sn->stats[y][rix].successive_failures,
1312                             sn->stats[y][rix].average_tx_time,
1313                             ticks - sn->stats[y][rix].last_tx);
1314                 }
1315         }
1316 }
1317
1318 static int
1319 ath_rate_sysctl_stats(SYSCTL_HANDLER_ARGS)
1320 {
1321         struct ath_softc *sc = arg1;
1322         struct ieee80211com *ic = &sc->sc_ic;
1323         int error, v;
1324
1325         v = 0;
1326         error = sysctl_handle_int(oidp, &v, 0, req);
1327         if (error || !req->newptr)
1328                 return error;
1329         ieee80211_iterate_nodes(&ic->ic_sta, sample_stats, sc);
1330         return 0;
1331 }
1332
1333 static int
1334 ath_rate_sysctl_smoothing_rate(SYSCTL_HANDLER_ARGS)
1335 {
1336         struct sample_softc *ssc = arg1;
1337         int rate, error;
1338
1339         rate = ssc->smoothing_rate;
1340         error = sysctl_handle_int(oidp, &rate, 0, req);
1341         if (error || !req->newptr)
1342                 return error;
1343         if (!(0 <= rate && rate < 100))
1344                 return EINVAL;
1345         ssc->smoothing_rate = rate;
1346         ssc->smoothing_minpackets = 100 / (100 - rate);
1347         return 0;
1348 }
1349
1350 static int
1351 ath_rate_sysctl_sample_rate(SYSCTL_HANDLER_ARGS)
1352 {
1353         struct sample_softc *ssc = arg1;
1354         int rate, error;
1355
1356         rate = ssc->sample_rate;
1357         error = sysctl_handle_int(oidp, &rate, 0, req);
1358         if (error || !req->newptr)
1359                 return error;
1360         if (!(2 <= rate && rate <= 100))
1361                 return EINVAL;
1362         ssc->sample_rate = rate;
1363         return 0;
1364 }
1365
1366 static void
1367 ath_rate_sysctlattach(struct ath_softc *sc, struct sample_softc *ssc)
1368 {
1369         struct sysctl_ctx_list *ctx = device_get_sysctl_ctx(sc->sc_dev);
1370         struct sysctl_oid *tree = device_get_sysctl_tree(sc->sc_dev);
1371
1372         SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
1373             "smoothing_rate", CTLTYPE_INT | CTLFLAG_RW, ssc, 0,
1374             ath_rate_sysctl_smoothing_rate, "I",
1375             "sample: smoothing rate for avg tx time (%%)");
1376         SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
1377             "sample_rate", CTLTYPE_INT | CTLFLAG_RW, ssc, 0,
1378             ath_rate_sysctl_sample_rate, "I",
1379             "sample: percent air time devoted to sampling new rates (%%)");
1380         /* XXX max_successive_failures, stale_failure_timeout, min_switch */
1381         SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
1382             "sample_stats", CTLTYPE_INT | CTLFLAG_RW, sc, 0,
1383             ath_rate_sysctl_stats, "I", "sample: print statistics");
1384 }
1385
1386 struct ath_ratectrl *
1387 ath_rate_attach(struct ath_softc *sc)
1388 {
1389         struct sample_softc *ssc;
1390         
1391 #if defined(__DragonFly__)
1392         ssc = kmalloc(sizeof(struct sample_softc), M_DEVBUF, M_INTWAIT|M_ZERO);
1393 #else
1394         ssc = malloc(sizeof(struct sample_softc), M_DEVBUF, M_NOWAIT|M_ZERO);
1395 #endif
1396         if (ssc == NULL)
1397                 return NULL;
1398         ssc->arc.arc_space = sizeof(struct sample_node);
1399         ssc->smoothing_rate = 75;               /* ewma percentage ([0..99]) */
1400         ssc->smoothing_minpackets = 100 / (100 - ssc->smoothing_rate);
1401         ssc->sample_rate = 10;                  /* %time to try diff tx rates */
1402         ssc->max_successive_failures = 3;       /* threshold for rate sampling*/
1403         ssc->stale_failure_timeout = 10 * hz;   /* 10 seconds */
1404         ssc->min_switch = hz;                   /* 1 second */
1405         ath_rate_sysctlattach(sc, ssc);
1406         return &ssc->arc;
1407 }
1408
1409 void
1410 ath_rate_detach(struct ath_ratectrl *arc)
1411 {
1412         struct sample_softc *ssc = (struct sample_softc *) arc;
1413         
1414         kfree(ssc, M_DEVBUF);
1415 }
1416
1417 #if defined(__DragonFly__)
1418
1419 /*
1420  * Module glue.
1421  */
1422 static int
1423 sample_modevent(module_t mod, int type, void *unused)
1424 {
1425         int error;
1426
1427         wlan_serialize_enter();
1428
1429         switch (type) {
1430         case MOD_LOAD:
1431                 if (bootverbose) {
1432                         kprintf("ath_rate: <SampleRate bit-rate "
1433                                 "selection algorithm>\n");
1434                 }
1435                 error = 0;
1436                 break;
1437         case MOD_UNLOAD:
1438                 error = 0;
1439                 break;
1440         default:
1441                 error = EINVAL;
1442                 break;
1443         }
1444         wlan_serialize_exit();
1445
1446         return error;
1447 }
1448
1449 static moduledata_t sample_mod = {
1450         "ath_rate",
1451         sample_modevent,
1452         0
1453 };
1454
1455 DECLARE_MODULE(ath_rate, sample_mod, SI_SUB_DRIVERS, SI_ORDER_FIRST);
1456 MODULE_VERSION(ath_rate, 1);
1457 MODULE_DEPEND(ath_rate, ath_hal, 1, 1, 1);
1458 MODULE_DEPEND(ath_rate, wlan, 1, 1, 1);
1459
1460 #endif