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