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