sample.c revision 172209
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 172209 2007-09-17 16:25:02Z sam $");
40
41/*
42 * John Bicket's SampleRate control algorithm.
43 */
44#include "opt_inet.h"
45
46#include <sys/param.h>
47#include <sys/systm.h>
48#include <sys/sysctl.h>
49#include <sys/module.h>
50#include <sys/kernel.h>
51#include <sys/lock.h>
52#include <sys/mutex.h>
53#include <sys/errno.h>
54
55#include <machine/bus.h>
56#include <machine/resource.h>
57#include <sys/bus.h>
58
59#include <sys/socket.h>
60
61#include <net/if.h>
62#include <net/if_media.h>
63#include <net/if_arp.h>
64#include <net/ethernet.h>		/* XXX for ether_sprintf */
65
66#include <net80211/ieee80211_var.h>
67
68#include <net/bpf.h>
69
70#ifdef INET
71#include <netinet/in.h>
72#include <netinet/if_ether.h>
73#endif
74
75#include <dev/ath/if_athvar.h>
76#include <dev/ath/ath_rate/sample/sample.h>
77#include <contrib/dev/ath/ah_desc.h>
78
79#define	SAMPLE_DEBUG
80#ifdef SAMPLE_DEBUG
81enum {
82	ATH_DEBUG_NODE		= 0x00080000,	/* node management */
83	ATH_DEBUG_RATE		= 0x00000010,	/* rate control */
84	ATH_DEBUG_ANY		= 0xffffffff
85};
86#define	DPRINTF(sc, m, fmt, ...) do {				\
87	if (sc->sc_debug & (m))					\
88		printf(fmt, __VA_ARGS__);			\
89} while (0)
90#else
91#define	DPRINTF(sc, m, fmt, ...) do {				\
92	(void) sc;						\
93} while (0)
94#endif
95
96/*
97 * This file is an implementation of the SampleRate algorithm
98 * in "Bit-rate Selection in Wireless Networks"
99 * (http://www.pdos.lcs.mit.edu/papers/jbicket-ms.ps)
100 *
101 * SampleRate chooses the bit-rate it predicts will provide the most
102 * throughput based on estimates of the expected per-packet
103 * transmission time for each bit-rate.  SampleRate periodically sends
104 * packets at bit-rates other than the current one to estimate when
105 * another bit-rate will provide better performance. SampleRate
106 * switches to another bit-rate when its estimated per-packet
107 * transmission time becomes smaller than the current bit-rate's.
108 * SampleRate reduces the number of bit-rates it must sample by
109 * eliminating those that could not perform better than the one
110 * currently being used.  SampleRate also stops probing at a bit-rate
111 * if it experiences several successive losses.
112 *
113 * The difference between the algorithm in the thesis and the one in this
114 * file is that the one in this file uses a ewma instead of a window.
115 *
116 * Also, this implementation tracks the average transmission time for
117 * a few different packet sizes independently for each link.
118 */
119
120#define STALE_FAILURE_TIMEOUT_MS 10000
121#define MIN_SWITCH_MS 1000
122
123static void	ath_rate_ctl_reset(struct ath_softc *, struct ieee80211_node *);
124
125static __inline int
126size_to_bin(int size)
127{
128	int x = 0;
129	for (x = 0; x < NUM_PACKET_SIZE_BINS; x++) {
130		if (size <= packet_size_bins[x]) {
131			return x;
132		}
133	}
134	return NUM_PACKET_SIZE_BINS-1;
135}
136static __inline int
137bin_to_size(int index) {
138	return packet_size_bins[index];
139}
140
141void
142ath_rate_node_init(struct ath_softc *sc, struct ath_node *an)
143{
144	DPRINTF(sc, ATH_DEBUG_NODE, "%s:\n", __func__);
145	/* NB: assumed to be zero'd by caller */
146}
147
148void
149ath_rate_node_cleanup(struct ath_softc *sc, struct ath_node *an)
150{
151	DPRINTF(sc, ATH_DEBUG_NODE, "%s:\n", __func__);
152}
153
154
155/*
156 * returns the ndx with the lowest average_tx_time,
157 * or -1 if all the average_tx_times are 0.
158 */
159static __inline int best_rate_ndx(struct sample_node *sn, int size_bin,
160				  int require_acked_before)
161{
162	int x = 0;
163        int best_rate_ndx = 0;
164        int best_rate_tt = 0;
165        for (x = 0; x < sn->num_rates; x++) {
166		int tt = sn->stats[size_bin][x].average_tx_time;
167		if (tt <= 0 || (require_acked_before &&
168				!sn->stats[size_bin][x].packets_acked)) {
169			continue;
170		}
171
172		/* 9 megabits never works better than 12 */
173		if (sn->rates[x].rate == 18)
174			continue;
175
176		/* don't use a bit-rate that has been failing */
177		if (sn->stats[size_bin][x].successive_failures > 3)
178			continue;
179
180		if (!best_rate_tt || best_rate_tt > tt) {
181			best_rate_tt = tt;
182			best_rate_ndx = x;
183		}
184        }
185        return (best_rate_tt) ? best_rate_ndx : -1;
186}
187
188/*
189 * pick a good "random" bit-rate to sample other than the current one
190 */
191static __inline int
192pick_sample_ndx(struct sample_node *sn, int size_bin)
193{
194	int x = 0;
195	int current_ndx = 0;
196	unsigned current_tt = 0;
197
198	current_ndx = sn->current_rate[size_bin];
199	if (current_ndx < 0) {
200		/* no successes yet, send at the lowest bit-rate */
201		return 0;
202	}
203
204	current_tt = sn->stats[size_bin][current_ndx].average_tx_time;
205
206	for (x = 0; x < sn->num_rates; x++) {
207		int ndx = (sn->last_sample_ndx[size_bin]+1+x) % sn->num_rates;
208
209	        /* don't sample the current bit-rate */
210		if (ndx == current_ndx)
211			continue;
212
213		/* this bit-rate is always worse than the current one */
214		if (sn->stats[size_bin][ndx].perfect_tx_time > current_tt)
215			continue;
216
217		/* rarely sample bit-rates that fail a lot */
218		if (ticks - sn->stats[size_bin][ndx].last_tx < ((hz * STALE_FAILURE_TIMEOUT_MS)/1000) &&
219		    sn->stats[size_bin][ndx].successive_failures > 3)
220			continue;
221
222		/* don't sample more than 2 indexes higher
223		 * for rates higher than 11 megabits
224		 */
225		if (sn->rates[ndx].rate > 22 && ndx > current_ndx + 2)
226			continue;
227
228		/* 9 megabits never works better than 12 */
229		if (sn->rates[ndx].rate == 18)
230			continue;
231
232		/* if we're using 11 megabits, only sample up to 12 megabits
233		 */
234		if (sn->rates[current_ndx].rate == 22 && ndx > current_ndx + 1)
235			continue;
236
237		sn->last_sample_ndx[size_bin] = ndx;
238		return ndx;
239	}
240	return current_ndx;
241}
242
243void
244ath_rate_findrate(struct ath_softc *sc, struct ath_node *an,
245		  int shortPreamble, size_t frameLen,
246		  u_int8_t *rix, int *try0, u_int8_t *txrate)
247{
248	struct sample_node *sn = ATH_NODE_SAMPLE(an);
249	struct sample_softc *ssc = ATH_SOFTC_SAMPLE(sc);
250	struct ieee80211com *ic = &sc->sc_ic;
251	int ndx, size_bin, mrr, best_ndx, change_rates;
252	unsigned average_tx_time;
253
254	mrr = sc->sc_mrretry && !(ic->ic_flags & IEEE80211_F_USEPROT);
255	size_bin = size_to_bin(frameLen);
256	best_ndx = best_rate_ndx(sn, size_bin, !mrr);
257
258	if (best_ndx >= 0) {
259		average_tx_time = sn->stats[size_bin][best_ndx].average_tx_time;
260	} else {
261		average_tx_time = 0;
262	}
263
264	if (sn->static_rate_ndx != -1) {
265		ndx = sn->static_rate_ndx;
266		*try0 = ATH_TXMAXTRY;
267	} else {
268		*try0 = mrr ? 2 : ATH_TXMAXTRY;
269
270		if (sn->sample_tt[size_bin] < average_tx_time * (sn->packets_since_sample[size_bin]*ssc->ath_sample_rate/100)) {
271			/*
272			 * we want to limit the time measuring the performance
273			 * of other bit-rates to ath_sample_rate% of the
274			 * total transmission time.
275			 */
276			ndx = pick_sample_ndx(sn, size_bin);
277			if (ndx != sn->current_rate[size_bin]) {
278				sn->current_sample_ndx[size_bin] = ndx;
279			} else {
280				sn->current_sample_ndx[size_bin] = -1;
281			}
282			sn->packets_since_sample[size_bin] = 0;
283
284		} else {
285			change_rates = 0;
286			if (!sn->packets_sent[size_bin] || best_ndx == -1) {
287				/* no packet has been sent successfully yet */
288				for (ndx = sn->num_rates-1; ndx > 0; ndx--) {
289					/*
290					 * pick the highest rate <= 36 Mbps
291					 * that hasn't failed.
292					 */
293					if (sn->rates[ndx].rate <= 72 &&
294					    sn->stats[size_bin][ndx].successive_failures == 0) {
295						break;
296					}
297				}
298				change_rates = 1;
299				best_ndx = ndx;
300			} else if (sn->packets_sent[size_bin] < 20) {
301				/* let the bit-rate switch quickly during the first few packets */
302				change_rates = 1;
303			} else if (ticks - ((hz*MIN_SWITCH_MS)/1000) > sn->ticks_since_switch[size_bin]) {
304				/* 2 seconds have gone by */
305				change_rates = 1;
306			} else if (average_tx_time * 2 < sn->stats[size_bin][sn->current_rate[size_bin]].average_tx_time) {
307				/* the current bit-rate is twice as slow as the best one */
308				change_rates = 1;
309			}
310
311			sn->packets_since_sample[size_bin]++;
312
313			if (change_rates) {
314				if (best_ndx != sn->current_rate[size_bin]) {
315					DPRINTF(sc, ATH_DEBUG_RATE,
316"%s: %s size %d switch rate %d (%d/%d) -> %d (%d/%d) after %d packets mrr %d\n",
317					    __func__,
318					    ether_sprintf(an->an_node.ni_macaddr),
319					    packet_size_bins[size_bin],
320					    sn->rates[sn->current_rate[size_bin]].rate,
321					    sn->stats[size_bin][sn->current_rate[size_bin]].average_tx_time,
322					    sn->stats[size_bin][sn->current_rate[size_bin]].perfect_tx_time,
323					    sn->rates[best_ndx].rate,
324					    sn->stats[size_bin][best_ndx].average_tx_time,
325					    sn->stats[size_bin][best_ndx].perfect_tx_time,
326					    sn->packets_since_switch[size_bin],
327					    mrr);
328				}
329				sn->packets_since_switch[size_bin] = 0;
330				sn->current_rate[size_bin] = best_ndx;
331				sn->ticks_since_switch[size_bin] = ticks;
332			}
333			ndx = sn->current_rate[size_bin];
334			sn->packets_since_switch[size_bin]++;
335			if (size_bin == 0) {
336	    			/*
337	    			 * set the visible txrate for this node
338			         * to the rate of small packets
339			         */
340				an->an_node.ni_txrate = ndx;
341			}
342		}
343	}
344
345	KASSERT(ndx >= 0 && ndx < sn->num_rates, ("ndx is %d", ndx));
346
347	*rix = sn->rates[ndx].rix;
348	if (shortPreamble) {
349		*txrate = sn->rates[ndx].shortPreambleRateCode;
350	} else {
351		*txrate = sn->rates[ndx].rateCode;
352	}
353	sn->packets_sent[size_bin]++;
354}
355
356void
357ath_rate_setupxtxdesc(struct ath_softc *sc, struct ath_node *an,
358		      struct ath_desc *ds, int shortPreamble, u_int8_t rix)
359{
360	struct sample_node *sn = ATH_NODE_SAMPLE(an);
361	int rateCode = -1;
362	int frame_size = 0;
363	int size_bin = 0;
364	int ndx = 0;
365
366	size_bin = size_to_bin(frame_size);	// TODO: it's correct that frame_size alway 0 ?
367	ndx = sn->current_rate[size_bin]; /* retry at the current bit-rate */
368
369	if (!sn->stats[size_bin][ndx].packets_acked) {
370		ndx = 0;  /* use the lowest bit-rate */
371	}
372
373	if (shortPreamble) {
374		rateCode = sn->rates[ndx].shortPreambleRateCode;
375	} else {
376		rateCode = sn->rates[ndx].rateCode;
377	}
378	ath_hal_setupxtxdesc(sc->sc_ah, ds
379			     , rateCode, 3	        /* series 1 */
380			     , sn->rates[0].rateCode, 3	/* series 2 */
381			     , 0, 0	                /* series 3 */
382			     );
383}
384
385static void
386update_stats(struct ath_softc *sc, struct ath_node *an,
387		  int frame_size,
388		  int ndx0, int tries0,
389		  int ndx1, int tries1,
390		  int ndx2, int tries2,
391		  int ndx3, int tries3,
392		  int short_tries, int tries, int status)
393{
394	struct sample_node *sn = ATH_NODE_SAMPLE(an);
395	struct sample_softc *ssc = ATH_SOFTC_SAMPLE(sc);
396	int tt = 0;
397	int tries_so_far = 0;
398	int size_bin = 0;
399	int size = 0;
400	int rate = 0;
401
402	size_bin = size_to_bin(frame_size);
403	size = bin_to_size(size_bin);
404
405	if (!(0 <= ndx0 && ndx0 < sn->num_rates)) {
406		printf("%s: bogus ndx0 %d, max %u, mode %u\n",
407		    __func__, ndx0, sn->num_rates, sc->sc_curmode);
408		return;
409	}
410	rate = sn->rates[ndx0].rate;
411
412	tt += calc_usecs_unicast_packet(sc, size, sn->rates[ndx0].rix,
413					short_tries,
414					MIN(tries0, tries) - 1);
415	tries_so_far += tries0;
416	if (tries1 && tries0 < tries) {
417		if (!(0 <= ndx1 && ndx1 < sn->num_rates)) {
418			printf("%s: bogus ndx1 %d, max %u, mode %u\n",
419			    __func__, ndx1, sn->num_rates, sc->sc_curmode);
420			return;
421		}
422		tt += calc_usecs_unicast_packet(sc, size, sn->rates[ndx1].rix,
423						short_tries,
424						MIN(tries1 + tries_so_far, tries) - tries_so_far - 1);
425	}
426	tries_so_far += tries1;
427
428	if (tries2 && tries0 + tries1 < tries) {
429		if (!(0 <= ndx2 && ndx2 < sn->num_rates)) {
430			printf("%s: bogus ndx2 %d, max %u, mode %u\n",
431			    __func__, ndx2, sn->num_rates, sc->sc_curmode);
432			return;
433		}
434		tt += calc_usecs_unicast_packet(sc, size, sn->rates[ndx2].rix,
435					       short_tries,
436						MIN(tries2 + tries_so_far, tries) - tries_so_far - 1);
437	}
438
439	tries_so_far += tries2;
440
441	if (tries3 && tries0 + tries1 + tries2 < tries) {
442		if (!(0 <= ndx3 && ndx3 < sn->num_rates)) {
443			printf("%s: bogus ndx3 %d, max %u, mode %u\n",
444			    __func__, ndx3, sn->num_rates, sc->sc_curmode);
445			return;
446		}
447		tt += calc_usecs_unicast_packet(sc, size, sn->rates[ndx3].rix,
448						short_tries,
449						MIN(tries3 + tries_so_far, tries) - tries_so_far - 1);
450	}
451	if (sn->stats[size_bin][ndx0].total_packets < (100 / (100 - ssc->ath_smoothing_rate))) {
452		/* just average the first few packets */
453		int avg_tx = sn->stats[size_bin][ndx0].average_tx_time;
454		int packets = sn->stats[size_bin][ndx0].total_packets;
455		sn->stats[size_bin][ndx0].average_tx_time = (tt+(avg_tx*packets))/(packets+1);
456	} else {
457		/* use a ewma */
458		sn->stats[size_bin][ndx0].average_tx_time =
459			((sn->stats[size_bin][ndx0].average_tx_time * ssc->ath_smoothing_rate) +
460			 (tt * (100 - ssc->ath_smoothing_rate))) / 100;
461	}
462
463	if (status) {
464		int y;
465		sn->stats[size_bin][ndx0].successive_failures++;
466		for (y = size_bin+1; y < NUM_PACKET_SIZE_BINS; y++) {
467			/* also say larger packets failed since we
468			 * assume if a small packet fails at a lower
469			 * bit-rate then a larger one will also.
470			 */
471			sn->stats[y][ndx0].successive_failures++;
472			sn->stats[y][ndx0].last_tx = ticks;
473			sn->stats[y][ndx0].tries += tries;
474			sn->stats[y][ndx0].total_packets++;
475		}
476	} else {
477		sn->stats[size_bin][ndx0].packets_acked++;
478		sn->stats[size_bin][ndx0].successive_failures = 0;
479	}
480	sn->stats[size_bin][ndx0].tries += tries;
481	sn->stats[size_bin][ndx0].last_tx = ticks;
482	sn->stats[size_bin][ndx0].total_packets++;
483
484
485	if (ndx0 == sn->current_sample_ndx[size_bin]) {
486		DPRINTF(sc, ATH_DEBUG_RATE,
487"%s: %s size %d %s sample rate %d tries (%d/%d) tt %d avg_tt (%d/%d)\n",
488		    __func__, ether_sprintf(an->an_node.ni_macaddr),
489		    size,
490		    status ? "FAIL" : "OK",
491		    rate, short_tries, tries, tt,
492		    sn->stats[size_bin][ndx0].average_tx_time,
493		    sn->stats[size_bin][ndx0].perfect_tx_time);
494		sn->sample_tt[size_bin] = tt;
495		sn->current_sample_ndx[size_bin] = -1;
496	}
497}
498
499void
500ath_rate_tx_complete(struct ath_softc *sc, struct ath_node *an,
501	const struct ath_buf *bf)
502{
503	struct ieee80211com *ic = &sc->sc_ic;
504	struct sample_node *sn = ATH_NODE_SAMPLE(an);
505	const struct ath_tx_status *ts = &bf->bf_status.ds_txstat;
506	const struct ath_desc *ds0 = &bf->bf_desc[0];
507	const HAL_RATE_TABLE *rt = sc->sc_currates;
508	int short_tries, long_tries, frame_size;
509	int mrr, ndx0, ndx1, ndx2, ndx3;
510
511	ndx0 = rt->rateCodeToIndex[ts->ts_rate &~ HAL_TXSTAT_ALTRATE];
512	short_tries = ts->ts_shortretry;
513	long_tries = ts->ts_longretry + 1;
514	frame_size = ds0->ds_ctl0 & 0x0fff; /* low-order 12 bits of ds_ctl0 */
515	if (frame_size == 0)		    /* NB: should not happen */
516		frame_size = 1500;
517
518	if (sn->num_rates <= 0) {
519		DPRINTF(sc, ATH_DEBUG_RATE,
520		    "%s: %s size %d %s rate/try %d/%d no rates yet\n",
521		    __func__, ether_sprintf(an->an_node.ni_macaddr),
522		    bin_to_size(size_to_bin(frame_size)),
523		    ts->ts_status ? "FAIL" : "OK",
524		    short_tries, long_tries);
525		return;
526	}
527	mrr = sc->sc_mrretry && !(ic->ic_flags & IEEE80211_F_USEPROT);
528	if (!mrr || !(ts->ts_rate & HAL_TXSTAT_ALTRATE)) {
529		/*
530		 * Only one rate was used; optimize work.
531		 */
532		DPRINTF(sc, ATH_DEBUG_RATE,
533		    "%s: %s size %d %s rate/try %d/%d/%d\n",
534		     __func__, ether_sprintf(an->an_node.ni_macaddr),
535		     bin_to_size(size_to_bin(frame_size)),
536		     ts->ts_status ? "FAIL" : "OK",
537		     sn->rates[ndx0].rate, short_tries, long_tries);
538		update_stats(sc, an, frame_size,
539			     ndx0, long_tries,
540			     0, 0,
541			     0, 0,
542			     0, 0,
543			     short_tries, long_tries, ts->ts_status);
544	} else {
545		int hwrate0, tries0;
546		int hwrate1, tries1;
547		int hwrate2, tries2;
548		int hwrate3, tries3;
549		int finalTSIdx = ts->ts_finaltsi;
550
551		/*
552		 * Process intermediate rates that failed.
553		 */
554		if (sc->sc_ah->ah_magic != 0x20065416) {
555			hwrate0 = MS(ds0->ds_ctl3, AR_XmitRate0);
556			hwrate1 = MS(ds0->ds_ctl3, AR_XmitRate1);
557			hwrate2 = MS(ds0->ds_ctl3, AR_XmitRate2);
558			hwrate3 = MS(ds0->ds_ctl3, AR_XmitRate3);
559		} else {
560			hwrate0 = MS(ds0->ds_ctl3, AR5416_XmitRate0);
561			hwrate1 = MS(ds0->ds_ctl3, AR5416_XmitRate1);
562			hwrate2 = MS(ds0->ds_ctl3, AR5416_XmitRate2);
563			hwrate3 = MS(ds0->ds_ctl3, AR5416_XmitRate3);
564		}
565
566		ndx0 = rt->rateCodeToIndex[hwrate0];
567		tries0 = MS(ds0->ds_ctl2, AR_XmitDataTries0);
568
569		ndx1 = rt->rateCodeToIndex[hwrate1];
570		tries1 = MS(ds0->ds_ctl2, AR_XmitDataTries1);
571
572		ndx2 = rt->rateCodeToIndex[hwrate2];
573		tries2 = MS(ds0->ds_ctl2, AR_XmitDataTries2);
574
575		ndx3 = rt->rateCodeToIndex[hwrate3];
576		tries3 = MS(ds0->ds_ctl2, AR_XmitDataTries3);
577
578		DPRINTF(sc, ATH_DEBUG_RATE,
579"%s: %s size %d finaltsidx %d tries %d %s rate/try [%d/%d %d/%d %d/%d %d/%d]\n",
580		     __func__, ether_sprintf(an->an_node.ni_macaddr),
581		     bin_to_size(size_to_bin(frame_size)),
582		     finalTSIdx,
583		     long_tries,
584		     ts->ts_status ? "FAIL" : "OK",
585		     sn->rates[ndx0].rate, tries0,
586		     sn->rates[ndx1].rate, tries1,
587		     sn->rates[ndx2].rate, tries2,
588		     sn->rates[ndx3].rate, tries3);
589
590		/*
591		 * NB: series > 0 are not penalized for failure
592		 * based on the try counts under the assumption
593		 * that losses are often bursty and since we
594		 * sample higher rates 1 try at a time doing so
595		 * may unfairly penalize them.
596		 */
597		if (tries0) {
598			update_stats(sc, an, frame_size,
599				     ndx0, tries0,
600				     ndx1, tries1,
601				     ndx2, tries2,
602				     ndx3, tries3,
603				     short_tries, long_tries,
604				     long_tries > tries0);
605			long_tries -= tries0;
606		}
607
608		if (tries1 && finalTSIdx > 0) {
609			update_stats(sc, an, frame_size,
610				     ndx1, tries1,
611				     ndx2, tries2,
612				     ndx3, tries3,
613				     0, 0,
614				     short_tries, long_tries,
615				     ts->ts_status);
616			long_tries -= tries1;
617		}
618
619		if (tries2 && finalTSIdx > 1) {
620			update_stats(sc, an, frame_size,
621				     ndx2, tries2,
622				     ndx3, tries3,
623				     0, 0,
624				     0, 0,
625				     short_tries, long_tries,
626				     ts->ts_status);
627			long_tries -= tries2;
628		}
629
630		if (tries3 && finalTSIdx > 2) {
631			update_stats(sc, an, frame_size,
632				     ndx3, tries3,
633				     0, 0,
634				     0, 0,
635				     0, 0,
636				     short_tries, long_tries,
637				     ts->ts_status);
638		}
639	}
640}
641
642void
643ath_rate_newassoc(struct ath_softc *sc, struct ath_node *an, int isnew)
644{
645	DPRINTF(sc, ATH_DEBUG_NODE, "%s: %s isnew %d\n", __func__,
646	     ether_sprintf(an->an_node.ni_macaddr), isnew);
647	if (isnew)
648		ath_rate_ctl_reset(sc, &an->an_node);
649}
650
651/*
652 * Initialize the tables for a node.
653 */
654static void
655ath_rate_ctl_reset(struct ath_softc *sc, struct ieee80211_node *ni)
656{
657#define	RATE(_ix)	(ni->ni_rates.rs_rates[(_ix)] & IEEE80211_RATE_VAL)
658	struct ieee80211com *ic = &sc->sc_ic;
659	struct ath_node *an = ATH_NODE(ni);
660	struct sample_node *sn = ATH_NODE_SAMPLE(an);
661	const HAL_RATE_TABLE *rt = sc->sc_currates;
662	int x, y, srate;
663
664	KASSERT(rt != NULL, ("no rate table, mode %u", sc->sc_curmode));
665        sn->static_rate_ndx = -1;
666	if (ic->ic_fixed_rate != IEEE80211_FIXED_RATE_NONE) {
667		/*
668		 * A fixed rate is to be used; ic_fixed_rate is the
669		 * IEEE code for this rate (sans basic bit).  Convert this
670		 * to the index into the negotiated rate set for
671		 * the node.
672		 */
673		/* NB: the rate set is assumed sorted */
674		srate = ni->ni_rates.rs_nrates - 1;
675		for (; srate >= 0 && RATE(srate) != ic->ic_fixed_rate; srate--)
676			;
677		/*
678		 * The fixed rate may not be available due to races
679		 * and mode settings.  Also orphaned nodes created in
680		 * adhoc mode may not have any rate set so this lookup
681		 * can fail.
682		 */
683		if (srate >= 0)
684			sn->static_rate_ndx = srate;
685	}
686
687        DPRINTF(sc, ATH_DEBUG_RATE, "%s: %s size 1600 rate/tt",
688	    __func__, ether_sprintf(ni->ni_macaddr));
689
690	sn->num_rates = ni->ni_rates.rs_nrates;
691        for (x = 0; x < ni->ni_rates.rs_nrates; x++) {
692		sn->rates[x].rate = ni->ni_rates.rs_rates[x] & IEEE80211_RATE_VAL;
693		sn->rates[x].rix = sc->sc_rixmap[sn->rates[x].rate];
694		if (sn->rates[x].rix == 0xff) {
695			DPRINTF(sc, ATH_DEBUG_RATE,
696			    "%s: ignore bogus rix at %d\n", __func__, x);
697			continue;
698		}
699		sn->rates[x].rateCode = rt->info[sn->rates[x].rix].rateCode;
700		sn->rates[x].shortPreambleRateCode =
701			rt->info[sn->rates[x].rix].rateCode |
702			rt->info[sn->rates[x].rix].shortPreamble;
703
704		DPRINTF(sc, ATH_DEBUG_RATE, " %d/%d", sn->rates[x].rate,
705		    calc_usecs_unicast_packet(sc, 1600, sn->rates[x].rix, 0,0));
706	}
707	DPRINTF(sc, ATH_DEBUG_RATE, "%s\n", "");
708
709	/* set the visible bit-rate to the lowest one available */
710	ni->ni_txrate = 0;
711	sn->num_rates = ni->ni_rates.rs_nrates;
712
713	for (y = 0; y < NUM_PACKET_SIZE_BINS; y++) {
714		int size = bin_to_size(y);
715		int ndx = 0;
716		sn->packets_sent[y] = 0;
717		sn->current_sample_ndx[y] = -1;
718		sn->last_sample_ndx[y] = 0;
719
720		for (x = 0; x < ni->ni_rates.rs_nrates; x++) {
721			sn->stats[y][x].successive_failures = 0;
722			sn->stats[y][x].tries = 0;
723			sn->stats[y][x].total_packets = 0;
724			sn->stats[y][x].packets_acked = 0;
725			sn->stats[y][x].last_tx = 0;
726
727			sn->stats[y][x].perfect_tx_time =
728				calc_usecs_unicast_packet(sc, size,
729							  sn->rates[x].rix,
730							  0, 0);
731			sn->stats[y][x].average_tx_time = sn->stats[y][x].perfect_tx_time;
732		}
733
734		/* set the initial rate */
735		for (ndx = sn->num_rates-1; ndx > 0; ndx--) {
736			if (sn->rates[ndx].rate <= 72) {
737				break;
738			}
739		}
740		sn->current_rate[y] = ndx;
741	}
742
743	DPRINTF(sc, ATH_DEBUG_RATE,
744	    "%s: %s %d rates %d%sMbps (%dus)- %d%sMbps (%dus)\n",
745	    __func__, ether_sprintf(ni->ni_macaddr),
746	    sn->num_rates,
747	    sn->rates[0].rate/2, sn->rates[0].rate % 0x1 ? ".5" : "",
748	    sn->stats[1][0].perfect_tx_time,
749	    sn->rates[sn->num_rates-1].rate/2,
750		sn->rates[sn->num_rates-1].rate % 0x1 ? ".5" : "",
751	    sn->stats[1][sn->num_rates-1].perfect_tx_time
752	);
753
754        if (sn->static_rate_ndx != -1)
755		ni->ni_txrate = sn->static_rate_ndx;
756	else
757		ni->ni_txrate = sn->current_rate[0];
758#undef RATE
759}
760
761static void
762rate_cb(void *arg, struct ieee80211_node *ni)
763{
764	struct ath_softc *sc = arg;
765
766	ath_rate_newassoc(sc, ATH_NODE(ni), 1);
767}
768
769/*
770 * Reset the rate control state for each 802.11 state transition.
771 */
772void
773ath_rate_newstate(struct ath_softc *sc, enum ieee80211_state state)
774{
775	struct ieee80211com *ic = &sc->sc_ic;
776
777	if (state == IEEE80211_S_RUN) {
778		if (ic->ic_opmode != IEEE80211_M_STA) {
779			/*
780			 * Sync rates for associated stations and neighbors.
781			 */
782			ieee80211_iterate_nodes(&ic->ic_sta, rate_cb, sc);
783		}
784		ath_rate_newassoc(sc, ATH_NODE(ic->ic_bss), 1);
785	}
786}
787
788static void
789ath_rate_sysctlattach(struct ath_softc *sc, struct sample_softc *osc)
790{
791	struct sysctl_ctx_list *ctx = device_get_sysctl_ctx(sc->sc_dev);
792	struct sysctl_oid *tree = device_get_sysctl_tree(sc->sc_dev);
793
794	/* XXX bounds check [0..100] */
795	SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
796		"smoothing_rate", CTLFLAG_RW, &osc->ath_smoothing_rate, 0,
797		"rate control: retry threshold to credit rate raise (%%)");
798	/* XXX bounds check [2..100] */
799	SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
800		"sample_rate", CTLFLAG_RW, &osc->ath_sample_rate,0,
801		"rate control: # good periods before raising rate");
802}
803
804struct ath_ratectrl *
805ath_rate_attach(struct ath_softc *sc)
806{
807	struct sample_softc *osc;
808
809	DPRINTF(sc, ATH_DEBUG_ANY, "%s:\n", __func__);
810	osc = malloc(sizeof(struct sample_softc), M_DEVBUF, M_NOWAIT|M_ZERO);
811	if (osc == NULL)
812		return NULL;
813	osc->arc.arc_space = sizeof(struct sample_node);
814	osc->ath_smoothing_rate = 95;	/* ewma percentage (out of 100) */
815	osc->ath_sample_rate = 10;	/* send a different bit-rate 1/X packets */
816	ath_rate_sysctlattach(sc, osc);
817	return &osc->arc;
818}
819
820void
821ath_rate_detach(struct ath_ratectrl *arc)
822{
823	struct sample_softc *osc = (struct sample_softc *) arc;
824
825	free(osc, M_DEVBUF);
826}
827
828/*
829 * Module glue.
830 */
831static int
832sample_modevent(module_t mod, int type, void *unused)
833{
834	switch (type) {
835	case MOD_LOAD:
836		if (bootverbose)
837			printf("ath_rate: version 1.2 <SampleRate bit-rate selection algorithm>\n");
838		return 0;
839	case MOD_UNLOAD:
840		return 0;
841	}
842	return EINVAL;
843}
844
845static moduledata_t sample_mod = {
846	"ath_rate",
847	sample_modevent,
848	0
849};
850DECLARE_MODULE(ath_rate, sample_mod, SI_SUB_DRIVERS, SI_ORDER_FIRST);
851MODULE_VERSION(ath_rate, 1);
852MODULE_DEPEND(ath_rate, ath_hal, 1, 1, 1);	/* Atheros HAL */
853MODULE_DEPEND(ath_rate, wlan, 1, 1, 1);
854