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