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