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