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