Merge from vendor branch OPENSSL:
[dragonfly.git] / sys / dev / netif / 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  * $FreeBSD: src/sys/dev/ath/ath_rate/sample/sample.c,v 1.8.2.3 2006/03/14 23:22:27 sam Exp $
37  * $DragonFly: src/sys/dev/netif/ath/rate_sample/sample.c,v 1.6 2007/02/22 05:17:10 sephe Exp $
38  */
39
40 /*
41  * John Bicket's SampleRate control algorithm.
42  */
43
44 #include <sys/param.h>
45 #include <sys/systm.h> 
46 #include <sys/sysctl.h>
47 #include <sys/module.h>
48 #include <sys/kernel.h>
49 #include <sys/lock.h>
50 #include <sys/errno.h>
51 #include <sys/bus.h>
52
53 #include <sys/socket.h>
54  
55 #include <net/if.h>
56 #include <net/if_media.h>
57 #include <net/if_arp.h>
58
59 #include <netproto/802_11/ieee80211_var.h>
60
61 #include <net/bpf.h>
62
63 #include <dev/netif/ath/ath/if_athvar.h>
64 #include <dev/netif/ath/rate_sample/sample.h>
65 #include <contrib/dev/ath/ah_desc.h>
66
67 #define SAMPLE_DEBUG
68 #ifdef SAMPLE_DEBUG
69 enum {
70         ATH_DEBUG_NODE          = 0x00080000,   /* node management */
71         ATH_DEBUG_RATE          = 0x00000010,   /* rate control */
72         ATH_DEBUG_ANY           = 0xffffffff
73 };
74 #define DPRINTF(sc, m, _fmt, ...) do {                          \
75         if (sc->sc_debug & (m))                                 \
76                 kprintf(_fmt, __VA_ARGS__);                     \
77 } while (0)
78 #else
79 #define DPRINTF(sc, m, _fmt, ...) do {                          \
80         (void)sc;
81 } while (0)
82 #endif
83
84 /*
85  * This file is an implementation of the SampleRate algorithm
86  * in "Bit-rate Selection in Wireless Networks"
87  * (http://www.pdos.lcs.mit.edu/papers/jbicket-ms.ps)
88  *
89  * SampleRate chooses the bit-rate it predicts will provide the most
90  * throughput based on estimates of the expected per-packet
91  * transmission time for each bit-rate.  SampleRate periodically sends
92  * packets at bit-rates other than the current one to estimate when
93  * another bit-rate will provide better performance. SampleRate
94  * switches to another bit-rate when its estimated per-packet
95  * transmission time becomes smaller than the current bit-rate's.
96  * SampleRate reduces the number of bit-rates it must sample by
97  * eliminating those that could not perform better than the one
98  * currently being used.  SampleRate also stops probing at a bit-rate
99  * if it experiences several successive losses.
100  *
101  * The difference between the algorithm in the thesis and the one in this
102  * file is that the one in this file uses a ewma instead of a window.
103  *
104  * Also, this implementation tracks the average transmission time for
105  * a few different packet sizes independently for each link.
106  */
107
108 #define STALE_FAILURE_TIMEOUT_MS 10000
109 #define MIN_SWITCH_MS 1000
110
111 static void     ath_rate_ctl_reset(struct ath_softc *, struct ieee80211_node *);
112
113 static __inline int
114 size_to_bin(int size) 
115 {
116         int x = 0;
117         for (x = 0; x < NUM_PACKET_SIZE_BINS; x++) {
118                 if (size <= packet_size_bins[x]) {
119                         return x;
120                 }
121         }
122         return NUM_PACKET_SIZE_BINS-1;
123 }
124 static __inline int
125 bin_to_size(int index) {
126         return packet_size_bins[index];
127 }
128
129 static __inline int
130 rate_to_ndx(struct sample_node *sn, int rate) {
131         int x = 0;
132         for (x = 0; x < sn->num_rates; x++) {
133                 if (sn->rates[x].rate == rate) {
134                         return x;
135                 }      
136         }
137         return -1;
138 }
139
140 void
141 ath_rate_node_init(struct ath_softc *sc, struct ath_node *an)
142 {
143         DPRINTF(sc, ATH_DEBUG_NODE, "%s:\n", __func__);
144         /* NB: assumed to be zero'd by caller */
145 }
146
147 void
148 ath_rate_node_cleanup(struct ath_softc *sc, struct ath_node *an)
149 {
150         DPRINTF(sc, ATH_DEBUG_NODE, "%s:\n", __func__);
151 }
152
153
154 /*
155  * returns the ndx with the lowest average_tx_time,
156  * or -1 if all the average_tx_times are 0.
157  */
158 static __inline int
159 best_rate_ndx(struct sample_node *sn, int size_bin, int require_acked_before)
160 {
161         int x = 0;
162         int best_rate_ndx = 0;
163         int best_rate_tt = 0;
164         for (x = 0; x < sn->num_rates; x++) {
165                 int tt = sn->stats[size_bin][x].average_tx_time;
166                 if (tt <= 0 || (require_acked_before && 
167                                 !sn->stats[size_bin][x].packets_acked)) {
168                         continue;
169                 }
170
171                 /* 9 megabits never works better than 12 */
172                 if (sn->rates[x].rate == 18) 
173                         continue;
174
175                 /* don't use a bit-rate that has been failing */
176                 if (sn->stats[size_bin][x].successive_failures > 3)
177                         continue;
178
179                 if (!best_rate_tt || best_rate_tt > tt) {
180                         best_rate_tt = tt;
181                         best_rate_ndx = x;
182                 }
183         }
184         return (best_rate_tt) ? best_rate_ndx : -1;
185 }
186
187 /*
188  * pick a good "random" bit-rate to sample other than the current one
189  */
190 static __inline int
191 pick_sample_ndx(struct sample_node *sn, int size_bin) 
192 {
193         int x = 0;
194         int current_ndx = 0;
195         unsigned current_tt = 0;
196         
197         current_ndx = sn->current_rate[size_bin];
198         if (current_ndx < 0) {
199                 /* no successes yet, send at the lowest bit-rate */
200                 return 0;
201         }
202         
203         current_tt = sn->stats[size_bin][current_ndx].average_tx_time;
204         
205         for (x = 0; x < sn->num_rates; x++) {
206                 int ndx = (sn->last_sample_ndx[size_bin]+1+x) % sn->num_rates;
207
208                 /* don't sample the current bit-rate */
209                 if (ndx == current_ndx) 
210                         continue;
211
212                 /* this bit-rate is always worse than the current one */
213                 if (sn->stats[size_bin][ndx].perfect_tx_time > current_tt) 
214                         continue;
215
216                 /* rarely sample bit-rates that fail a lot */
217                 if (ticks - sn->stats[size_bin][ndx].last_tx < ((hz * STALE_FAILURE_TIMEOUT_MS)/1000) &&
218                     sn->stats[size_bin][ndx].successive_failures > 3)
219                         continue;
220
221                 /* don't sample more than 2 indexes higher 
222                  * for rates higher than 11 megabits
223                  */
224                 if (sn->rates[ndx].rate > 22 && ndx > current_ndx + 2)
225                         continue;
226
227                 /* 9 megabits never works better than 12 */
228                 if (sn->rates[ndx].rate == 18) 
229                         continue;
230
231                 /* if we're using 11 megabits, only sample up to 12 megabits
232                  */
233                 if (sn->rates[current_ndx].rate == 22 && ndx > current_ndx + 1) 
234                         continue;
235
236                 sn->last_sample_ndx[size_bin] = ndx;
237                 return ndx;
238         }
239         return current_ndx;
240 }
241
242 void
243 ath_rate_findrate(struct ath_softc *sc, struct ath_node *an,
244                   int shortPreamble, size_t frameLen,
245                   u_int8_t *rix, int *try0, u_int8_t *txrate)
246 {
247         struct sample_node *sn = ATH_NODE_SAMPLE(an);
248         struct sample_softc *ssc = ATH_SOFTC_SAMPLE(sc);
249         struct ieee80211com *ic = &sc->sc_ic;
250         int ndx, size_bin, mrr, best_ndx, change_rates;
251         unsigned average_tx_time;
252
253         mrr = sc->sc_mrretry && !(ic->ic_flags & IEEE80211_F_USEPROT);
254         size_bin = size_to_bin(frameLen);
255         best_ndx = best_rate_ndx(sn, size_bin, !mrr);
256
257         if (best_ndx >= 0) {
258                 average_tx_time = sn->stats[size_bin][best_ndx].average_tx_time;
259         } else {
260                 average_tx_time = 0;
261         }
262         
263         if (sn->static_rate_ndx != -1) {
264                 ndx = sn->static_rate_ndx;
265                 *try0 = ATH_TXMAXTRY;
266         } else {
267                 *try0 = mrr ? 2 : ATH_TXMAXTRY;
268                 
269                 if (sn->sample_tt[size_bin] < average_tx_time * (sn->packets_since_sample[size_bin]*ssc->ath_sample_rate/100)) {
270                         /*
271                          * we want to limit the time measuring the performance
272                          * of other bit-rates to ath_sample_rate% of the
273                          * total transmission time.
274                          */
275                         ndx = pick_sample_ndx(sn, size_bin);
276                         if (ndx != sn->current_rate[size_bin]) {
277                                 sn->current_sample_ndx[size_bin] = ndx;
278                         } else {
279                                 sn->current_sample_ndx[size_bin] = -1;
280                         }
281                         sn->packets_since_sample[size_bin] = 0;
282
283                 } else {
284                         change_rates = 0;
285                         if (!sn->packets_sent[size_bin] || best_ndx == -1) {
286                                 /* no packet has been sent successfully yet */
287                                 for (ndx = sn->num_rates-1; ndx > 0; ndx--) {
288                                         /* 
289                                          * pick the highest rate <= 36 Mbps
290                                          * that hasn't failed.
291                                          */
292                                         if (sn->rates[ndx].rate <= 72 && 
293                                             sn->stats[size_bin][ndx].successive_failures == 0) {
294                                                 break;
295                                         }
296                                 }
297                                 change_rates = 1;
298                                 best_ndx = ndx;
299                         } else if (sn->packets_sent[size_bin] < 20) {
300                                 /* let the bit-rate switch quickly during the first few packets */
301                                 change_rates = 1;
302                         } else if (ticks - ((hz*MIN_SWITCH_MS)/1000) > sn->ticks_since_switch[size_bin]) {
303                                 /* 2 seconds have gone by */
304                                 change_rates = 1;
305                         } else if (average_tx_time * 2 < sn->stats[size_bin][sn->current_rate[size_bin]].average_tx_time) {
306                                 /* the current bit-rate is twice as slow as the best one */
307                                 change_rates = 1;
308                         }
309
310                         sn->packets_since_sample[size_bin]++;
311                         
312                         if (change_rates) {
313                                 if (best_ndx != sn->current_rate[size_bin]) {
314                                         DPRINTF(sc, ATH_DEBUG_RATE,
315                                                 "%s: %6D size %d switch rate %d (%d/%d) -> %d (%d/%d) after %d packets mrr %d\n",
316                                                 __func__,
317                                                 an->an_node.ni_macaddr, ":",
318                                                 packet_size_bins[size_bin],
319                                                 sn->rates[sn->current_rate[size_bin]].rate,
320                                                 sn->stats[size_bin][sn->current_rate[size_bin]].average_tx_time,
321                                                 sn->stats[size_bin][sn->current_rate[size_bin]].perfect_tx_time,
322                                                 sn->rates[best_ndx].rate,
323                                                 sn->stats[size_bin][best_ndx].average_tx_time,
324                                                 sn->stats[size_bin][best_ndx].perfect_tx_time,
325                                                 sn->packets_since_switch[size_bin],
326                                                 mrr);
327                                 }
328                                 sn->packets_since_switch[size_bin] = 0;
329                                 sn->current_rate[size_bin] = best_ndx;
330                                 sn->ticks_since_switch[size_bin] = ticks;
331                         }
332                         ndx = sn->current_rate[size_bin];
333                         sn->packets_since_switch[size_bin]++;
334                         if (size_bin == 0) {
335                                 /* 
336                                  * set the visible txrate for this node
337                                  * to the rate of small packets
338                                  */
339                                 an->an_node.ni_txrate = ndx;
340                         }
341                 }
342         }
343
344         KASSERT(ndx >= 0 && ndx < sn->num_rates, ("ndx is %d", ndx));
345
346         *rix = sn->rates[ndx].rix;
347         if (shortPreamble) {
348                 *txrate = sn->rates[ndx].shortPreambleRateCode;
349         } else {
350                 *txrate = sn->rates[ndx].rateCode;
351         }
352         sn->packets_sent[size_bin]++;
353 }
354
355 void
356 ath_rate_setupxtxdesc(struct ath_softc *sc, struct ath_node *an,
357                       struct ath_desc *ds, int shortPreamble, u_int8_t rix)
358 {
359         struct sample_node *sn = ATH_NODE_SAMPLE(an);
360         int rateCode = -1;
361         int frame_size = 0;
362         int size_bin = 0;
363         int ndx = 0;
364
365         /* TODO: it's correct that frame_size alway 0 ? */
366         size_bin = size_to_bin(frame_size);
367         
368         ndx = sn->current_rate[size_bin]; /* retry at the current bit-rate */
369         
370         if (!sn->stats[size_bin][ndx].packets_acked) {
371                 ndx = 0;  /* use the lowest bit-rate */
372         }
373
374         if (shortPreamble) {
375                 rateCode = sn->rates[ndx].shortPreambleRateCode;
376         } else {
377                 rateCode = sn->rates[ndx].rateCode;
378         }
379         ath_hal_setupxtxdesc(sc->sc_ah, ds
380                              , rateCode, 3              /* series 1 */
381                              , sn->rates[0].rateCode, 3 /* series 2 */
382                              , 0, 0                     /* series 3 */
383                              );
384 }
385
386 static void
387 update_stats(struct ath_softc *sc, struct ath_node *an, 
388                   int frame_size,
389                   int ndx0, int tries0,
390                   int ndx1, int tries1,
391                   int ndx2, int tries2,
392                   int ndx3, int tries3,
393                   int short_tries, int tries, int status)
394 {
395         struct sample_node *sn = ATH_NODE_SAMPLE(an);
396         struct sample_softc *ssc = ATH_SOFTC_SAMPLE(sc);
397         int tt = 0;
398         int tries_so_far = 0;
399         int size_bin = 0;
400         int size = 0;
401         int rate = 0;
402
403         size_bin = size_to_bin(frame_size);
404         size = bin_to_size(size_bin);
405
406         if (!(0 <= ndx0 && ndx0 < sn->num_rates)) {
407                 kprintf("%s: bogus ndx0 %d, max %u, mode %u\n",
408                         __func__, ndx0, sn->num_rates, sc->sc_curmode);
409                 return;
410         }
411         rate = sn->rates[ndx0].rate;
412
413         tt += calc_usecs_unicast_packet(sc, size, sn->rates[ndx0].rix, 
414                                         short_tries,
415                                         MIN(tries0, tries) - 1);
416         tries_so_far += tries0;
417         if (tries1 && tries0 < tries) {
418                 if (!(0 <= ndx1 && ndx1 < sn->num_rates)) {
419                         kprintf("%s: bogus ndx1 %d, max %u, mode %u\n",
420                                 __func__, ndx1, sn->num_rates, sc->sc_curmode);
421                         return;
422                 }
423                 tt += calc_usecs_unicast_packet(sc, size, sn->rates[ndx1].rix, 
424                                                 short_tries,
425                                                 MIN(tries1 + tries_so_far, tries) - tries_so_far - 1);
426         }
427         tries_so_far += tries1;
428
429         if (tries2 && tries0 + tries1 < tries) {
430                 if (!(0 <= ndx2 && ndx2 < sn->num_rates)) {
431                         kprintf("%s: bogus ndx2 %d, max %u, mode %u\n",
432                                 __func__, ndx2, sn->num_rates, sc->sc_curmode);
433                         return;
434                 }
435                 tt += calc_usecs_unicast_packet(sc, size, sn->rates[ndx2].rix,
436                                                 short_tries,
437                                                 MIN(tries2 + tries_so_far, tries) - tries_so_far - 1);
438         }
439
440         tries_so_far += tries2;
441
442         if (tries3 && tries0 + tries1 + tries2 < tries) {
443                 if (!(0 <= ndx3 && ndx3 < sn->num_rates)) {
444                         kprintf("%s: bogus ndx3 %d, max %u, mode %u\n",
445                                 __func__, ndx3, sn->num_rates, sc->sc_curmode);
446                         return;
447                 }
448                 tt += calc_usecs_unicast_packet(sc, size, sn->rates[ndx3].rix, 
449                                                 short_tries,
450                                                 MIN(tries3 + tries_so_far, tries) - tries_so_far - 1);
451         }
452         if (sn->stats[size_bin][ndx0].total_packets < (100 / (100 - ssc->ath_smoothing_rate))) {
453                 /* just average the first few packets */
454                 int avg_tx = sn->stats[size_bin][ndx0].average_tx_time;
455                 int packets = sn->stats[size_bin][ndx0].total_packets;
456                 sn->stats[size_bin][ndx0].average_tx_time = (tt+(avg_tx*packets))/(packets+1);
457         } else {
458                 /* use a ewma */
459                 sn->stats[size_bin][ndx0].average_tx_time = 
460                         ((sn->stats[size_bin][ndx0].average_tx_time * ssc->ath_smoothing_rate) + 
461                          (tt * (100 - ssc->ath_smoothing_rate))) / 100;
462         }
463         
464         if (status) {
465                 int y;
466                 sn->stats[size_bin][ndx0].successive_failures++;
467                 for (y = size_bin+1; y < NUM_PACKET_SIZE_BINS; y++) {
468                         /* also say larger packets failed since we
469                          * assume if a small packet fails at a lower
470                          * bit-rate then a larger one will also.
471                          */
472                         sn->stats[y][ndx0].successive_failures++;
473                         sn->stats[y][ndx0].last_tx = ticks;
474                         sn->stats[y][ndx0].tries += tries;
475                         sn->stats[y][ndx0].total_packets++;
476                 }
477         } else {
478                 sn->stats[size_bin][ndx0].packets_acked++;
479                 sn->stats[size_bin][ndx0].successive_failures = 0;
480         }
481         sn->stats[size_bin][ndx0].tries += tries;
482         sn->stats[size_bin][ndx0].last_tx = ticks;
483         sn->stats[size_bin][ndx0].total_packets++;
484
485
486         if (ndx0 == sn->current_sample_ndx[size_bin]) {
487                 DPRINTF(sc, ATH_DEBUG_RATE,
488                         "%s: %6D size %d %s sample rate %d tries (%d/%d) tt %d avg_tt (%d/%d)\n", 
489                         __func__, an->an_node.ni_macaddr, ":",
490                         size,
491                         status ? "FAIL" : "OK",
492                         rate, short_tries, tries, tt,
493                         sn->stats[size_bin][ndx0].average_tx_time,
494                         sn->stats[size_bin][ndx0].perfect_tx_time);
495                 sn->sample_tt[size_bin] = tt;
496                 sn->current_sample_ndx[size_bin] = -1;
497         }
498 }
499
500 void
501 ath_rate_tx_complete(struct ath_softc *sc, struct ath_node *an,
502         const struct ath_buf *bf)
503 {
504         struct ieee80211com *ic = &sc->sc_ic;
505         struct sample_node *sn = ATH_NODE_SAMPLE(an);
506         const struct ath_tx_status *ts = &bf->bf_status.ds_txstat;
507         const struct ath_desc *ds0 = &bf->bf_desc[0];
508         int final_rate, short_tries, long_tries, frame_size;
509         int mrr;
510
511         final_rate = sc->sc_hwmap[ts->ts_rate &~ HAL_TXSTAT_ALTRATE].ieeerate;
512         short_tries = ts->ts_shortretry;
513         long_tries = ts->ts_longretry + 1;
514         frame_size = ds0->ds_ctl0 & 0x0fff; /* low-order 12 bits of ds_ctl0 */
515         if (frame_size == 0)                /* NB: should not happen */
516                 frame_size = 1500;
517
518         if (sn->num_rates <= 0) {
519                 DPRINTF(sc, ATH_DEBUG_RATE,
520                         "%s: %6D size %d %s rate/try %d/%d no rates yet\n",
521                         __func__, an->an_node.ni_macaddr, ":",
522                         bin_to_size(size_to_bin(frame_size)),
523                         ts->ts_status ? "FAIL" : "OK",
524                         short_tries, long_tries);
525                 return;
526         }
527
528         mrr = sc->sc_mrretry && !(ic->ic_flags & IEEE80211_F_USEPROT);
529         if (!mrr || !(ts->ts_rate & HAL_TXSTAT_ALTRATE)) {
530                 int ndx = rate_to_ndx(sn, final_rate);
531
532                 /*
533                  * Only one rate was used; optimize work.
534                  */
535                 DPRINTF(sc, ATH_DEBUG_RATE,
536                         "%s: %6D size %d %s rate/try %d/%d/%d\n",
537                         __func__, an->an_node.ni_macaddr, ":",
538                         bin_to_size(size_to_bin(frame_size)),
539                         ts->ts_status ? "FAIL" : "OK",
540                         final_rate, short_tries, long_tries);
541                 update_stats(sc, an, frame_size, 
542                              ndx, long_tries,
543                              0, 0,
544                              0, 0,
545                              0, 0,
546                              short_tries, long_tries, ts->ts_status);
547         } else {
548                 int hwrate0, rate0, tries0, ndx0;
549                 int hwrate1, rate1, tries1, ndx1;
550                 int hwrate2, rate2, tries2, ndx2;
551                 int hwrate3, rate3, tries3, ndx3;
552                 int finalTSIdx = ts->ts_finaltsi;
553
554                 /*
555                  * Process intermediate rates that failed.
556                  */
557                 if (sc->sc_ah->ah_magic != 0x20065416) {
558                         hwrate0 = MS(ds0->ds_ctl3, AR_XmitRate0);
559                         hwrate1 = MS(ds0->ds_ctl3, AR_XmitRate1);
560                         hwrate2 = MS(ds0->ds_ctl3, AR_XmitRate2);
561                         hwrate3 = MS(ds0->ds_ctl3, AR_XmitRate3);
562                 } else {
563                         hwrate0 = MS(ds0->ds_ctl3, AR5416_XmitRate0);
564                         hwrate1 = MS(ds0->ds_ctl3, AR5416_XmitRate1);
565                         hwrate2 = MS(ds0->ds_ctl3, AR5416_XmitRate2);
566                         hwrate3 = MS(ds0->ds_ctl3, AR5416_XmitRate3);
567                 }
568
569                 rate0 = sc->sc_hwmap[hwrate0].ieeerate;
570                 tries0 = MS(ds0->ds_ctl2, AR_XmitDataTries0);
571                 ndx0 = rate_to_ndx(sn, rate0);
572
573                 rate1 = sc->sc_hwmap[hwrate1].ieeerate;
574                 tries1 = MS(ds0->ds_ctl2, AR_XmitDataTries1);
575                 ndx1 = rate_to_ndx(sn, rate1);
576
577                 rate2 = sc->sc_hwmap[hwrate2].ieeerate;
578                 tries2 = MS(ds0->ds_ctl2, AR_XmitDataTries2);
579                 ndx2 = rate_to_ndx(sn, rate2);
580
581                 rate3 = sc->sc_hwmap[hwrate3].ieeerate;
582                 tries3 = MS(ds0->ds_ctl2, AR_XmitDataTries3);
583                 ndx3 = rate_to_ndx(sn, rate3);
584
585                 DPRINTF(sc, ATH_DEBUG_RATE,
586                         "%s: %6D size %d finaltsidx %d tries %d %s rate/try [%d/%d %d/%d %d/%d %d/%d]\n",
587                         __func__, an->an_node.ni_macaddr, ":",
588                         bin_to_size(size_to_bin(frame_size)),
589                         finalTSIdx,
590                         long_tries, 
591                         ts->ts_status ? "FAIL" : "OK",
592                         rate0, tries0,
593                         rate1, tries1,
594                         rate2, tries2,
595                         rate3, tries3);
596
597                 /*
598                  * NB: series > 0 are not penalized for failure
599                  * based on the try counts under the assumption
600                  * that losses are often bursty and since we
601                  * sample higher rates 1 try at a time doing so
602                  * may unfairly penalize them.
603                  */
604                 if (tries0) {
605                         update_stats(sc, an, frame_size, 
606                                      ndx0, tries0, 
607                                      ndx1, tries1, 
608                                      ndx2, tries2, 
609                                      ndx3, tries3, 
610                                      short_tries, long_tries,
611                                      long_tries > tries0);
612                         long_tries -= tries0;
613                 }
614                 
615                 if (tries1 && finalTSIdx > 0) {
616                         update_stats(sc, an, frame_size, 
617                                      ndx1, tries1, 
618                                      ndx2, tries2, 
619                                      ndx3, tries3, 
620                                      0, 0, 
621                                      short_tries, long_tries, 
622                                      ts->ts_status);
623                         long_tries -= tries1;
624                 }
625
626                 if (tries2 && finalTSIdx > 1) {
627                         update_stats(sc, an, frame_size, 
628                                      ndx2, tries2, 
629                                      ndx3, tries3, 
630                                      0, 0,
631                                      0, 0,
632                                      short_tries, long_tries, 
633                                      ts->ts_status);
634                         long_tries -= tries2;
635                 }
636
637                 if (tries3 && finalTSIdx > 2) {
638                         update_stats(sc, an, frame_size, 
639                                      ndx3, tries3, 
640                                      0, 0,
641                                      0, 0,
642                                      0, 0,
643                                      short_tries, long_tries, 
644                                      ts->ts_status);
645                 }
646         }
647 }
648
649 void
650 ath_rate_newassoc(struct ath_softc *sc, struct ath_node *an, int isnew)
651 {
652         DPRINTF(sc, ATH_DEBUG_NODE, "%s: %6D isnew %d\n", __func__,
653                 an->an_node.ni_macaddr, ":", isnew);
654         if (isnew)
655                 ath_rate_ctl_reset(sc, &an->an_node);
656 }
657
658 /*
659  * Initialize the tables for a node.
660  */
661 static void
662 ath_rate_ctl_reset(struct ath_softc *sc, struct ieee80211_node *ni)
663 {
664 #define RATE(_ix)       (ni->ni_rates.rs_rates[(_ix)] & IEEE80211_RATE_VAL)
665         struct ieee80211com *ic = &sc->sc_ic;
666         struct ath_node *an = ATH_NODE(ni);
667         struct sample_node *sn = ATH_NODE_SAMPLE(an);
668         const HAL_RATE_TABLE *rt = sc->sc_currates;
669         int x, y, srate;
670
671         KASSERT(rt != NULL, ("no rate table, mode %u", sc->sc_curmode));
672         sn->static_rate_ndx = -1;
673         if (ic->ic_fixed_rate != IEEE80211_FIXED_RATE_NONE) {
674                 /*
675                  * A fixed rate is to be used; ic_fixed_rate is an
676                  * index into the supported rate set.  Convert this
677                  * to the index into the negotiated rate set for
678                  * the node.
679                  */
680                 const struct ieee80211_rateset *rs =
681                         &ic->ic_sup_rates[ic->ic_curmode];
682                 int r = rs->rs_rates[ic->ic_fixed_rate] & IEEE80211_RATE_VAL;
683                 /* NB: the rate set is assumed sorted */
684                 srate = ni->ni_rates.rs_nrates - 1;
685                 for (; srate >= 0 && RATE(srate) != r; srate--)
686                         ;
687                 KASSERT(srate >= 0,
688                         ("fixed rate %d not in rate set", ic->ic_fixed_rate));
689                 sn->static_rate_ndx = srate;
690         }
691
692         DPRINTF(sc, ATH_DEBUG_RATE, "%s: %6D size 1600 rate/tt", __func__,
693                 ni->ni_macaddr, ":");
694
695         sn->num_rates = ni->ni_rates.rs_nrates;
696         for (x = 0; x < ni->ni_rates.rs_nrates; x++) {
697                 sn->rates[x].rate = ni->ni_rates.rs_rates[x] & IEEE80211_RATE_VAL;
698                 sn->rates[x].rix = sc->sc_rixmap[sn->rates[x].rate];
699                 if (sn->rates[x].rix == 0xff) {
700                         DPRINTF(sc, ATH_DEBUG_RATE,
701                                 "%s: ignore bogus rix at %d\n", __func__, x);
702                         continue;
703                 }
704                 sn->rates[x].rateCode = rt->info[sn->rates[x].rix].rateCode;
705                 sn->rates[x].shortPreambleRateCode = 
706                         rt->info[sn->rates[x].rix].rateCode | 
707                         rt->info[sn->rates[x].rix].shortPreamble;
708
709                 DPRINTF(sc, ATH_DEBUG_RATE, " %d/%d", sn->rates[x].rate,
710                         calc_usecs_unicast_packet(sc, 1600, sn->rates[x].rix, 
711                                                   0, 0));
712         }
713         DPRINTF(sc, ATH_DEBUG_RATE, "%s\n", "");
714         
715         /* set the visible bit-rate to the lowest one available */
716         ni->ni_txrate = 0;
717         sn->num_rates = ni->ni_rates.rs_nrates;
718         
719         for (y = 0; y < NUM_PACKET_SIZE_BINS; y++) {
720                 int size = bin_to_size(y);
721                 int ndx = 0;
722                 sn->packets_sent[y] = 0;
723                 sn->current_sample_ndx[y] = -1;
724                 sn->last_sample_ndx[y] = 0;
725                 
726                 for (x = 0; x < ni->ni_rates.rs_nrates; x++) {
727                         sn->stats[y][x].successive_failures = 0;
728                         sn->stats[y][x].tries = 0;
729                         sn->stats[y][x].total_packets = 0;
730                         sn->stats[y][x].packets_acked = 0;
731                         sn->stats[y][x].last_tx = 0;
732                         
733                         sn->stats[y][x].perfect_tx_time = 
734                                 calc_usecs_unicast_packet(sc, size, 
735                                                           sn->rates[x].rix,
736                                                           0, 0);
737                         sn->stats[y][x].average_tx_time = sn->stats[y][x].perfect_tx_time;
738                 }
739
740                 /* set the initial rate */
741                 for (ndx = sn->num_rates-1; ndx > 0; ndx--) {
742                         if (sn->rates[ndx].rate <= 72) {
743                                 break;
744                         }
745                 }
746                 sn->current_rate[y] = ndx;
747         }
748
749         DPRINTF(sc, ATH_DEBUG_RATE,
750                 "%s: %6D %d rates %d%sMbps (%dus)- %d%sMbps (%dus)\n",
751                 __func__, ni->ni_macaddr, ":",
752                 sn->num_rates,
753                 sn->rates[0].rate/2, sn->rates[0].rate % 0x1 ? ".5" : "",
754                 sn->stats[1][0].perfect_tx_time,
755                 sn->rates[sn->num_rates-1].rate/2,
756                         sn->rates[sn->num_rates-1].rate % 0x1 ? ".5" : "",
757                 sn->stats[1][sn->num_rates-1].perfect_tx_time
758         );
759
760         if (sn->static_rate_ndx != -1)
761                 ni->ni_txrate = sn->static_rate_ndx;
762         else
763                 ni->ni_txrate = sn->current_rate[0];
764 #undef RATE
765 }
766
767 static void
768 rate_cb(void *arg, struct ieee80211_node *ni)
769 {
770         struct ath_softc *sc = arg;
771
772         ath_rate_newassoc(sc, ATH_NODE(ni), 1);
773 }
774
775 /*
776  * Reset the rate control state for each 802.11 state transition.
777  */
778 void
779 ath_rate_newstate(struct ath_softc *sc, enum ieee80211_state state)
780 {
781         struct ieee80211com *ic = &sc->sc_ic;
782
783         ASSERT_SERIALIZED(ic->ic_if.if_serializer);
784
785         if (state == IEEE80211_S_RUN) {
786                 if (ic->ic_opmode != IEEE80211_M_STA) {
787                         /*
788                          * Sync rates for associated stations and neighbors.
789                          */
790                         ieee80211_iterate_nodes(&ic->ic_sta, rate_cb, sc);
791                 }
792                 ath_rate_newassoc(sc, ATH_NODE(ic->ic_bss), 1);
793         }
794 }
795
796 static void
797 ath_rate_sysctlattach(struct ath_softc *sc, struct sample_softc *osc)
798 {
799         struct sysctl_ctx_list *ctx = &sc->sc_sysctl_ctx;
800         struct sysctl_oid *tree = sc->sc_sysctl_tree;
801
802         /* XXX bounds check [0..100] */
803         SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
804                 "smoothing_rate", CTLFLAG_RW, &osc->ath_smoothing_rate, 0,
805                 "rate control: retry threshold to credit rate raise (%%)");
806         /* XXX bounds check [2..100] */
807         SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
808                 "sample_rate", CTLFLAG_RW, &osc->ath_sample_rate,0,
809                 "rate control: # good periods before raising rate");
810 }
811
812 struct ath_ratectrl *
813 ath_rate_attach(struct ath_softc *sc)
814 {
815         struct sample_softc *osc;
816         
817         DPRINTF(sc, ATH_DEBUG_ANY, "%s:\n", __func__);
818         osc = kmalloc(sizeof(struct sample_softc), M_DEVBUF, M_NOWAIT|M_ZERO);
819         if (osc == NULL)
820                 return NULL;
821         osc->arc.arc_space = sizeof(struct sample_node);
822         osc->ath_smoothing_rate = 95;   /* ewma percentage (out of 100) */
823         osc->ath_sample_rate = 10;      /* send a different bit-rate 1/X packets */
824         ath_rate_sysctlattach(sc, osc);
825         return &osc->arc;
826 }
827
828 void
829 ath_rate_detach(struct ath_ratectrl *arc)
830 {
831         struct sample_softc *osc = (struct sample_softc *) arc;
832         
833         kfree(osc, M_DEVBUF);
834 }
835
836 void
837 ath_rate_stop(struct ath_ratectrl *arc __unused)
838 {
839 }
840
841 /*
842  * Module glue.
843  */
844 static int
845 sample_modevent(module_t mod, int type, void *unused)
846 {
847         switch (type) {
848         case MOD_LOAD:
849                 if (bootverbose)
850                         kprintf("ath_rate: version 1.2 <SampleRate bit-rate selection algorithm>\n");
851                 return 0;
852         case MOD_UNLOAD:
853                 return 0;
854         }
855         return EINVAL;
856 }
857
858 static moduledata_t sample_mod = {
859         "ath_rate",
860         sample_modevent,
861         0
862 };
863 DECLARE_MODULE(ath_rate, sample_mod, SI_SUB_DRIVERS, SI_ORDER_FIRST);
864 MODULE_VERSION(ath_rate, 1);
865 MODULE_DEPEND(ath_rate, ath_hal, 1, 1, 1);      /* Atheros HAL */
866 MODULE_DEPEND(ath_rate, wlan, 1, 1, 1);