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