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