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