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