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