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