xref: /freebsd/sys/dev/ath/ath_rate/sample/sample.c (revision 33644623554bb0fc57ed3c7d874193a498679b22)
1fa20c234SSam Leffler /*-
2fa20c234SSam Leffler  * Copyright (c) 2005 John Bicket
3fa20c234SSam Leffler  * All rights reserved.
4fa20c234SSam Leffler  *
5fa20c234SSam Leffler  * Redistribution and use in source and binary forms, with or without
6fa20c234SSam Leffler  * modification, are permitted provided that the following conditions
7fa20c234SSam Leffler  * are met:
8fa20c234SSam Leffler  * 1. Redistributions of source code must retain the above copyright
9fa20c234SSam Leffler  *    notice, this list of conditions and the following disclaimer,
10fa20c234SSam Leffler  *    without modification.
11fa20c234SSam Leffler  * 2. Redistributions in binary form must reproduce at minimum a disclaimer
12fa20c234SSam Leffler  *    similar to the "NO WARRANTY" disclaimer below ("Disclaimer") and any
13fa20c234SSam Leffler  *    redistribution must be conditioned upon including a substantially
14fa20c234SSam Leffler  *    similar Disclaimer requirement for further binary redistribution.
15fa20c234SSam Leffler  * 3. Neither the names of the above-listed copyright holders nor the names
16fa20c234SSam Leffler  *    of any contributors may be used to endorse or promote products derived
17fa20c234SSam Leffler  *    from this software without specific prior written permission.
18fa20c234SSam Leffler  *
19fa20c234SSam Leffler  * Alternatively, this software may be distributed under the terms of the
20fa20c234SSam Leffler  * GNU General Public License ("GPL") version 2 as published by the Free
21fa20c234SSam Leffler  * Software Foundation.
22fa20c234SSam Leffler  *
23fa20c234SSam Leffler  * NO WARRANTY
24fa20c234SSam Leffler  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
25fa20c234SSam Leffler  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
26fa20c234SSam Leffler  * LIMITED TO, THE IMPLIED WARRANTIES OF NONINFRINGEMENT, MERCHANTIBILITY
27fa20c234SSam Leffler  * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
28fa20c234SSam Leffler  * THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY,
29fa20c234SSam Leffler  * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
30fa20c234SSam Leffler  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
31fa20c234SSam Leffler  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
32fa20c234SSam Leffler  * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
33fa20c234SSam Leffler  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
34fa20c234SSam Leffler  * THE POSSIBILITY OF SUCH DAMAGES.
3568e8e04eSSam Leffler  *
36fa20c234SSam Leffler  */
37fa20c234SSam Leffler 
38fa20c234SSam Leffler #include <sys/cdefs.h>
39fa20c234SSam Leffler __FBSDID("$FreeBSD$");
40fa20c234SSam Leffler 
41fa20c234SSam Leffler /*
42fa20c234SSam Leffler  * John Bicket's SampleRate control algorithm.
43fa20c234SSam Leffler  */
44fa20c234SSam Leffler #include "opt_inet.h"
45b032f27cSSam Leffler #include "opt_wlan.h"
46fa20c234SSam Leffler 
47fa20c234SSam Leffler #include <sys/param.h>
48fa20c234SSam Leffler #include <sys/systm.h>
49fa20c234SSam Leffler #include <sys/sysctl.h>
50fa20c234SSam Leffler #include <sys/module.h>
51fa20c234SSam Leffler #include <sys/kernel.h>
52fa20c234SSam Leffler #include <sys/lock.h>
53fa20c234SSam Leffler #include <sys/mutex.h>
54fa20c234SSam Leffler #include <sys/errno.h>
55fa20c234SSam Leffler 
56fa20c234SSam Leffler #include <machine/bus.h>
57fa20c234SSam Leffler #include <machine/resource.h>
58fa20c234SSam Leffler #include <sys/bus.h>
59fa20c234SSam Leffler 
60fa20c234SSam Leffler #include <sys/socket.h>
61fa20c234SSam Leffler 
62fa20c234SSam Leffler #include <net/if.h>
63fa20c234SSam Leffler #include <net/if_media.h>
64fa20c234SSam Leffler #include <net/if_arp.h>
65c1565b61SSam Leffler #include <net/ethernet.h>		/* XXX for ether_sprintf */
66fa20c234SSam Leffler 
67fa20c234SSam Leffler #include <net80211/ieee80211_var.h>
68fa20c234SSam Leffler 
69fa20c234SSam Leffler #include <net/bpf.h>
70fa20c234SSam Leffler 
71fa20c234SSam Leffler #ifdef INET
72fa20c234SSam Leffler #include <netinet/in.h>
73fa20c234SSam Leffler #include <netinet/if_ether.h>
74fa20c234SSam Leffler #endif
75fa20c234SSam Leffler 
76fa20c234SSam Leffler #include <dev/ath/if_athvar.h>
77fa20c234SSam Leffler #include <dev/ath/ath_rate/sample/sample.h>
7833644623SSam Leffler #include <dev/ath/ath_hal/ah_desc.h>
79fa20c234SSam Leffler 
80fa20c234SSam Leffler /*
81fa20c234SSam Leffler  * This file is an implementation of the SampleRate algorithm
82fa20c234SSam Leffler  * in "Bit-rate Selection in Wireless Networks"
83fa20c234SSam Leffler  * (http://www.pdos.lcs.mit.edu/papers/jbicket-ms.ps)
84fa20c234SSam Leffler  *
85fa20c234SSam Leffler  * SampleRate chooses the bit-rate it predicts will provide the most
86fa20c234SSam Leffler  * throughput based on estimates of the expected per-packet
87fa20c234SSam Leffler  * transmission time for each bit-rate.  SampleRate periodically sends
88fa20c234SSam Leffler  * packets at bit-rates other than the current one to estimate when
89fa20c234SSam Leffler  * another bit-rate will provide better performance. SampleRate
90fa20c234SSam Leffler  * switches to another bit-rate when its estimated per-packet
91fa20c234SSam Leffler  * transmission time becomes smaller than the current bit-rate's.
92fa20c234SSam Leffler  * SampleRate reduces the number of bit-rates it must sample by
93fa20c234SSam Leffler  * eliminating those that could not perform better than the one
94fa20c234SSam Leffler  * currently being used.  SampleRate also stops probing at a bit-rate
95fa20c234SSam Leffler  * if it experiences several successive losses.
96fa20c234SSam Leffler  *
97fa20c234SSam Leffler  * The difference between the algorithm in the thesis and the one in this
98fa20c234SSam Leffler  * file is that the one in this file uses a ewma instead of a window.
99fa20c234SSam Leffler  *
100b91bf513SSam Leffler  * Also, this implementation tracks the average transmission time for
101b91bf513SSam Leffler  * a few different packet sizes independently for each link.
102fa20c234SSam Leffler  */
103fa20c234SSam Leffler 
104b2763056SSam Leffler static void	ath_rate_ctl_reset(struct ath_softc *, struct ieee80211_node *);
105fa20c234SSam Leffler 
106c1565b61SSam Leffler static const int packet_size_bins[NUM_PACKET_SIZE_BINS] = { 250, 1600 };
107c1565b61SSam Leffler 
108b91bf513SSam Leffler static __inline int
109b91bf513SSam Leffler size_to_bin(int size)
110fa20c234SSam Leffler {
111c1565b61SSam Leffler #if NUM_PACKET_SIZE_BINS > 1
112c1565b61SSam Leffler 	if (size <= packet_size_bins[0])
113c1565b61SSam Leffler 		return 0;
114c1565b61SSam Leffler #endif
115c1565b61SSam Leffler #if NUM_PACKET_SIZE_BINS > 2
116c1565b61SSam Leffler 	if (size <= packet_size_bins[1])
117c1565b61SSam Leffler 		return 1;
118c1565b61SSam Leffler #endif
119c1565b61SSam Leffler #if NUM_PACKET_SIZE_BINS > 3
120c1565b61SSam Leffler 	if (size <= packet_size_bins[2])
121c1565b61SSam Leffler 		return 2;
122c1565b61SSam Leffler #endif
123c1565b61SSam Leffler #if NUM_PACKET_SIZE_BINS > 4
124c1565b61SSam Leffler #error "add support for more packet sizes"
125c1565b61SSam Leffler #endif
126fa20c234SSam Leffler 	return NUM_PACKET_SIZE_BINS-1;
127fa20c234SSam Leffler }
128fa20c234SSam Leffler 
1291bb9a085SSam Leffler static __inline int
130c1565b61SSam Leffler bin_to_size(int index)
131c1565b61SSam Leffler {
132c1565b61SSam Leffler 	return packet_size_bins[index];
1331bb9a085SSam Leffler }
1341bb9a085SSam Leffler 
135fa20c234SSam Leffler void
136fa20c234SSam Leffler ath_rate_node_init(struct ath_softc *sc, struct ath_node *an)
137fa20c234SSam Leffler {
138fa20c234SSam Leffler 	/* NB: assumed to be zero'd by caller */
139fa20c234SSam Leffler }
140fa20c234SSam Leffler 
141fa20c234SSam Leffler void
142fa20c234SSam Leffler ath_rate_node_cleanup(struct ath_softc *sc, struct ath_node *an)
143fa20c234SSam Leffler {
144fa20c234SSam Leffler }
145fa20c234SSam Leffler 
146fa20c234SSam Leffler /*
147c1565b61SSam Leffler  * Return the rix with the lowest average_tx_time,
148fa20c234SSam Leffler  * or -1 if all the average_tx_times are 0.
149fa20c234SSam Leffler  */
150c1565b61SSam Leffler static __inline int
151c1565b61SSam Leffler pick_best_rate(struct sample_node *sn, const HAL_RATE_TABLE *rt,
152c1565b61SSam Leffler     int size_bin, int require_acked_before)
153fa20c234SSam Leffler {
154c1565b61SSam Leffler         int best_rate_rix, best_rate_tt;
155c1565b61SSam Leffler 	uint32_t mask;
156c1565b61SSam Leffler 	int rix, tt;
157b91bf513SSam Leffler 
158c1565b61SSam Leffler         best_rate_rix = 0;
159c1565b61SSam Leffler         best_rate_tt = 0;
160c1565b61SSam Leffler 	for (mask = sn->ratemask, rix = 0; mask != 0; mask >>= 1, rix++) {
161c1565b61SSam Leffler 		if ((mask & 1) == 0)		/* not a supported rate */
162c1565b61SSam Leffler 			continue;
163c1565b61SSam Leffler 
164c1565b61SSam Leffler 		tt = sn->stats[size_bin][rix].average_tx_time;
165c1565b61SSam Leffler 		if (tt <= 0 ||
166c1565b61SSam Leffler 		    (require_acked_before &&
167c1565b61SSam Leffler 		     !sn->stats[size_bin][rix].packets_acked))
168b91bf513SSam Leffler 			continue;
169b91bf513SSam Leffler 
170b91bf513SSam Leffler 		/* don't use a bit-rate that has been failing */
171c1565b61SSam Leffler 		if (sn->stats[size_bin][rix].successive_failures > 3)
172b91bf513SSam Leffler 			continue;
173b91bf513SSam Leffler 
174c1565b61SSam Leffler 		if (best_rate_tt == 0 || tt < best_rate_tt) {
175fa20c234SSam Leffler 			best_rate_tt = tt;
176c1565b61SSam Leffler 			best_rate_rix = rix;
177fa20c234SSam Leffler 		}
178fa20c234SSam Leffler         }
179c1565b61SSam Leffler         return (best_rate_tt ? best_rate_rix : -1);
180fa20c234SSam Leffler }
181fa20c234SSam Leffler 
182fa20c234SSam Leffler /*
183c1565b61SSam Leffler  * Pick a good "random" bit-rate to sample other than the current one.
184fa20c234SSam Leffler  */
185b91bf513SSam Leffler static __inline int
186c1565b61SSam Leffler pick_sample_rate(struct sample_softc *ssc , struct sample_node *sn,
187c1565b61SSam Leffler     const HAL_RATE_TABLE *rt, int size_bin)
188fa20c234SSam Leffler {
189c1565b61SSam Leffler #define	DOT11RATE(ix)	(rt->info[ix].dot11Rate & IEEE80211_RATE_VAL)
190c1565b61SSam Leffler 	int current_rix, rix;
191c1565b61SSam Leffler 	unsigned current_tt;
192c1565b61SSam Leffler 	uint32_t mask;
193fa20c234SSam Leffler 
194c1565b61SSam Leffler 	current_rix = sn->current_rix[size_bin];
195c1565b61SSam Leffler 	if (current_rix < 0) {
196fa20c234SSam Leffler 		/* no successes yet, send at the lowest bit-rate */
197fa20c234SSam Leffler 		return 0;
198fa20c234SSam Leffler 	}
199fa20c234SSam Leffler 
200c1565b61SSam Leffler 	current_tt = sn->stats[size_bin][current_rix].average_tx_time;
201fa20c234SSam Leffler 
202c1565b61SSam Leffler 	rix = sn->last_sample_rix[size_bin]+1;	/* next sample rate */
203c1565b61SSam Leffler 	mask = sn->ratemask &~ (1<<current_rix);/* don't sample current rate */
204c1565b61SSam Leffler 	while (mask != 0) {
205c1565b61SSam Leffler 		if ((mask & (1<<rix)) == 0) {	/* not a supported rate */
206c1565b61SSam Leffler 	nextrate:
207c1565b61SSam Leffler 			if (++rix >= rt->rateCount)
208c1565b61SSam Leffler 				rix = 0;
209b91bf513SSam Leffler 			continue;
210c1565b61SSam Leffler 		}
211b91bf513SSam Leffler 
212b91bf513SSam Leffler 		/* this bit-rate is always worse than the current one */
213c1565b61SSam Leffler 		if (sn->stats[size_bin][rix].perfect_tx_time > current_tt) {
214c1565b61SSam Leffler 			mask &= ~(1<<rix);
215c1565b61SSam Leffler 			goto nextrate;
216c1565b61SSam Leffler 		}
217b91bf513SSam Leffler 
218b91bf513SSam Leffler 		/* rarely sample bit-rates that fail a lot */
219c1565b61SSam Leffler 		if (sn->stats[size_bin][rix].successive_failures > ssc->max_successive_failures &&
220c1565b61SSam Leffler 		    ticks - sn->stats[size_bin][rix].last_tx < ssc->stale_failure_timeout) {
221c1565b61SSam Leffler 			mask &= ~(1<<rix);
222c1565b61SSam Leffler 			goto nextrate;
223fa20c234SSam Leffler 		}
224c1565b61SSam Leffler 
225c1565b61SSam Leffler 		/* don't sample more than 2 rates higher for rates > 11M */
226c1565b61SSam Leffler 		if (DOT11RATE(rix) > 2*11 && rix > current_rix + 2) {
227c1565b61SSam Leffler 			mask &= ~(1<<rix);
228c1565b61SSam Leffler 			goto nextrate;
229c1565b61SSam Leffler 		}
230c1565b61SSam Leffler 
231c1565b61SSam Leffler 		sn->last_sample_rix[size_bin] = rix;
232c1565b61SSam Leffler 		return rix;
233c1565b61SSam Leffler 	}
234c1565b61SSam Leffler 	return current_rix;
235c1565b61SSam Leffler #undef DOT11RATE
236fa20c234SSam Leffler }
237fa20c234SSam Leffler 
238fa20c234SSam Leffler void
239fa20c234SSam Leffler ath_rate_findrate(struct ath_softc *sc, struct ath_node *an,
240a4d8dd10SSam Leffler 		  int shortPreamble, size_t frameLen,
241c1565b61SSam Leffler 		  u_int8_t *rix0, int *try0, u_int8_t *txrate)
242fa20c234SSam Leffler {
243c1565b61SSam Leffler #define	DOT11RATE(ix)	(rt->info[ix].dot11Rate & IEEE80211_RATE_VAL)
244c1565b61SSam Leffler #define	RATE(ix)	(DOT11RATE(ix) / 2)
245fa20c234SSam Leffler 	struct sample_node *sn = ATH_NODE_SAMPLE(an);
246fa20c234SSam Leffler 	struct sample_softc *ssc = ATH_SOFTC_SAMPLE(sc);
247b032f27cSSam Leffler 	struct ifnet *ifp = sc->sc_ifp;
248b032f27cSSam Leffler 	struct ieee80211com *ic = ifp->if_l2com;
249c1565b61SSam Leffler 	const HAL_RATE_TABLE *rt = sc->sc_currates;
250c1565b61SSam Leffler 	const int size_bin = size_to_bin(frameLen);
251c1565b61SSam Leffler 	int rix, mrr, best_rix, change_rates;
252b2763056SSam Leffler 	unsigned average_tx_time;
253fa20c234SSam Leffler 
254c1565b61SSam Leffler 	if (sn->static_rix != -1) {
255c1565b61SSam Leffler 		rix = sn->static_rix;
256c1565b61SSam Leffler 		*try0 = ATH_TXMAXTRY;
257c1565b61SSam Leffler 		goto done;
258c1565b61SSam Leffler 	}
259fa20c234SSam Leffler 
260c1565b61SSam Leffler 	mrr = sc->sc_mrretry && !(ic->ic_flags & IEEE80211_F_USEPROT);
261c1565b61SSam Leffler 
262c1565b61SSam Leffler 	best_rix = pick_best_rate(sn, rt, size_bin, !mrr);
263c1565b61SSam Leffler 	if (best_rix >= 0) {
264c1565b61SSam Leffler 		average_tx_time = sn->stats[size_bin][best_rix].average_tx_time;
265fa20c234SSam Leffler 	} else {
266b2763056SSam Leffler 		average_tx_time = 0;
267b2763056SSam Leffler 	}
268fa20c234SSam Leffler 	/*
269c1565b61SSam Leffler 	 * Limit the time measuring the performance of other tx
270c1565b61SSam Leffler 	 * rates to sample_rate% of the total transmission time.
271fa20c234SSam Leffler 	 */
272c1565b61SSam Leffler 	if (sn->sample_tt[size_bin] < average_tx_time * (sn->packets_since_sample[size_bin]*ssc->sample_rate/100)) {
273c1565b61SSam Leffler 		rix = pick_sample_rate(ssc, sn, rt, size_bin);
274c1565b61SSam Leffler 		IEEE80211_NOTE(an->an_node.ni_vap, IEEE80211_MSG_RATECTL,
275c1565b61SSam Leffler 		     &an->an_node, "size %u sample rate %d current rate %d",
276c1565b61SSam Leffler 		     bin_to_size(size_bin), RATE(rix),
277c1565b61SSam Leffler 		     RATE(sn->current_rix[size_bin]));
278c1565b61SSam Leffler 		if (rix != sn->current_rix[size_bin]) {
279c1565b61SSam Leffler 			sn->current_sample_rix[size_bin] = rix;
280b2763056SSam Leffler 		} else {
281c1565b61SSam Leffler 			sn->current_sample_rix[size_bin] = -1;
282b2763056SSam Leffler 		}
283b2763056SSam Leffler 		sn->packets_since_sample[size_bin] = 0;
284b2763056SSam Leffler 	} else {
285b91bf513SSam Leffler 		change_rates = 0;
286c1565b61SSam Leffler 		if (!sn->packets_sent[size_bin] || best_rix == -1) {
287b91bf513SSam Leffler 			/* no packet has been sent successfully yet */
288c1565b61SSam Leffler 			for (rix = rt->rateCount-1; rix > 0; rix--) {
289c1565b61SSam Leffler 				if ((sn->ratemask & (1<<rix)) == 0)
290c1565b61SSam Leffler 					continue;
291b2763056SSam Leffler 				/*
292c1565b61SSam Leffler 				 * Pick the highest rate <= 36 Mbps
293b91bf513SSam Leffler 				 * that hasn't failed.
294b2763056SSam Leffler 				 */
295c1565b61SSam Leffler 				if (DOT11RATE(rix) <= 72 &&
296c1565b61SSam Leffler 				    sn->stats[size_bin][rix].successive_failures == 0) {
297b91bf513SSam Leffler 					break;
298b91bf513SSam Leffler 				}
299b91bf513SSam Leffler 			}
300b91bf513SSam Leffler 			change_rates = 1;
301c1565b61SSam Leffler 			best_rix = rix;
302b91bf513SSam Leffler 		} else if (sn->packets_sent[size_bin] < 20) {
303b91bf513SSam Leffler 			/* let the bit-rate switch quickly during the first few packets */
304b91bf513SSam Leffler 			change_rates = 1;
305c1565b61SSam Leffler 		} else if (ticks - ssc->min_switch > sn->ticks_since_switch[size_bin]) {
306c1565b61SSam Leffler 			/* min_switch seconds have gone by */
307b91bf513SSam Leffler 			change_rates = 1;
308c1565b61SSam Leffler 		} else if (2*average_tx_time < sn->stats[size_bin][sn->current_rix[size_bin]].average_tx_time) {
309b91bf513SSam Leffler 			/* the current bit-rate is twice as slow as the best one */
310b91bf513SSam Leffler 			change_rates = 1;
311b91bf513SSam Leffler 		}
312b91bf513SSam Leffler 
313b91bf513SSam Leffler 		sn->packets_since_sample[size_bin]++;
314b91bf513SSam Leffler 
315b91bf513SSam Leffler 		if (change_rates) {
316c1565b61SSam Leffler 			if (best_rix != sn->current_rix[size_bin]) {
317b032f27cSSam Leffler 				IEEE80211_NOTE(an->an_node.ni_vap,
318b032f27cSSam Leffler 				    IEEE80211_MSG_RATECTL,
319b032f27cSSam Leffler 				    &an->an_node,
320b032f27cSSam Leffler "%s: size %d switch rate %d (%d/%d) -> %d (%d/%d) after %d packets mrr %d",
321b2763056SSam Leffler 				    __func__,
322c1565b61SSam Leffler 				    bin_to_size(size_bin),
323c1565b61SSam Leffler 				    RATE(sn->current_rix[size_bin]),
324c1565b61SSam Leffler 				    sn->stats[size_bin][sn->current_rix[size_bin]].average_tx_time,
325c1565b61SSam Leffler 				    sn->stats[size_bin][sn->current_rix[size_bin]].perfect_tx_time,
326c1565b61SSam Leffler 				    RATE(best_rix),
327c1565b61SSam Leffler 				    sn->stats[size_bin][best_rix].average_tx_time,
328c1565b61SSam Leffler 				    sn->stats[size_bin][best_rix].perfect_tx_time,
329b2763056SSam Leffler 				    sn->packets_since_switch[size_bin],
330b2763056SSam Leffler 				    mrr);
331b2763056SSam Leffler 			}
332b2763056SSam Leffler 			sn->packets_since_switch[size_bin] = 0;
333c1565b61SSam Leffler 			sn->current_rix[size_bin] = best_rix;
334b91bf513SSam Leffler 			sn->ticks_since_switch[size_bin] = ticks;
335b032f27cSSam Leffler 			/*
336b032f27cSSam Leffler 			 * Set the visible txrate for this node.
337b032f27cSSam Leffler 			 */
338c1565b61SSam Leffler 			an->an_node.ni_txrate = DOT11RATE(best_rix);
339b2763056SSam Leffler 		}
340c1565b61SSam Leffler 		rix = sn->current_rix[size_bin];
341b2763056SSam Leffler 		sn->packets_since_switch[size_bin]++;
342b91bf513SSam Leffler 	}
343c1565b61SSam Leffler 	*try0 = mrr ? sn->sched[rix].t0 : ATH_TXMAXTRY;
344c1565b61SSam Leffler done:
345c1565b61SSam Leffler 	KASSERT(rix >= 0 && rix < rt->rateCount, ("rix is %d", rix));
346fa20c234SSam Leffler 
347c1565b61SSam Leffler 	*rix0 = rix;
348c1565b61SSam Leffler 	*txrate = rt->info[rix].rateCode
349c1565b61SSam Leffler 		| (shortPreamble ? rt->info[rix].shortPreamble : 0);
350fa20c234SSam Leffler 	sn->packets_sent[size_bin]++;
351c1565b61SSam Leffler #undef DOT11RATE
352c1565b61SSam Leffler #undef RATE
353fa20c234SSam Leffler }
354fa20c234SSam Leffler 
355c1565b61SSam Leffler #define A(_r) \
356c1565b61SSam Leffler     (((_r) == 6)   ? 0 : (((_r) == 9)   ? 1 : (((_r) == 12)  ? 2 : \
357c1565b61SSam Leffler     (((_r) == 18)  ? 3 : (((_r) == 24)  ? 4 : (((_r) == 36)  ? 5 : \
358c1565b61SSam Leffler     (((_r) == 48)  ? 6 : (((_r) == 54)  ? 7 : 0))))))))
359c1565b61SSam Leffler static const struct txschedule series_11a[] = {
360c1565b61SSam Leffler 	{ 3,A( 6), 3,A(  6), 0,A(  6), 0,A( 6) },	/*   6Mb/s */
361c1565b61SSam Leffler 	{ 4,A( 9), 3,A(  6), 4,A(  6), 0,A( 6) },	/*   9Mb/s */
362c1565b61SSam Leffler 	{ 4,A(12), 3,A(  6), 4,A(  6), 0,A( 6) },	/*  12Mb/s */
363c1565b61SSam Leffler 	{ 4,A(18), 3,A( 12), 4,A(  6), 2,A( 6) },	/*  18Mb/s */
364c1565b61SSam Leffler 	{ 4,A(24), 3,A( 18), 4,A( 12), 2,A( 6) },	/*  24Mb/s */
365c1565b61SSam Leffler 	{ 4,A(36), 3,A( 24), 4,A( 18), 2,A( 6) },	/*  36Mb/s */
366c1565b61SSam Leffler 	{ 4,A(48), 3,A( 36), 4,A( 24), 2,A(12) },	/*  48Mb/s */
367c1565b61SSam Leffler 	{ 4,A(54), 3,A( 48), 4,A( 36), 2,A(24) }	/*  54Mb/s */
368c1565b61SSam Leffler };
369c1565b61SSam Leffler #undef A
370c1565b61SSam Leffler 
371c1565b61SSam Leffler #define G(_r) \
372c1565b61SSam Leffler     (((_r) == 1)   ? 0 : (((_r) == 2)   ? 1 : (((_r) == 5.5) ? 2 : \
373c1565b61SSam Leffler     (((_r) == 11)  ? 3 : (((_r) == 6)   ? 4 : (((_r) == 9)   ? 5 : \
374c1565b61SSam Leffler     (((_r) == 12)  ? 6 : (((_r) == 18)  ? 7 : (((_r) == 24)  ? 8 : \
375c1565b61SSam Leffler     (((_r) == 36)  ? 9 : (((_r) == 48)  ? 10 : (((_r) == 54)  ? 11 : 0))))))))))))
376c1565b61SSam Leffler static const struct txschedule series_11g[] = {
377c1565b61SSam Leffler 	{ 3,G( 1), 3,G(  1), 0,G(  1), 0,G( 1) },	/*   1Mb/s */
378c1565b61SSam Leffler 	{ 4,G( 2), 3,G(  1), 4,G(  1), 0,G( 1) },	/*   2Mb/s */
379c1565b61SSam Leffler 	{ 4,G(5.5),3,G(  2), 4,G(  1), 2,G( 1) },	/* 5.5Mb/s */
380c1565b61SSam Leffler 	{ 4,G(11), 3,G(5.5), 4,G(  2), 2,G( 1) },	/*  11Mb/s */
381c1565b61SSam Leffler 	{ 4,G( 6), 3,G(5.5), 4,G(  2), 2,G( 1) },	/*   6Mb/s */
382c1565b61SSam Leffler 	{ 4,G( 9), 3,G(  6), 4,G(5.5), 2,G( 1) },	/*   9Mb/s */
383c1565b61SSam Leffler 	{ 4,G(12), 3,G( 11), 4,G(5.5), 2,G( 1) },	/*  12Mb/s */
384c1565b61SSam Leffler 	{ 4,G(18), 3,G( 12), 4,G( 11), 2,G( 1) },	/*  18Mb/s */
385c1565b61SSam Leffler 	{ 4,G(24), 3,G( 18), 4,G( 12), 2,G( 1) },	/*  24Mb/s */
386c1565b61SSam Leffler 	{ 4,G(36), 3,G( 24), 4,G( 18), 2,G( 1) },	/*  36Mb/s */
387c1565b61SSam Leffler 	{ 4,G(48), 3,G( 36), 4,G( 24), 2,G( 1) },	/*  48Mb/s */
388c1565b61SSam Leffler 	{ 4,G(54), 3,G( 48), 4,G( 36), 2,G( 1) }	/*  54Mb/s */
389c1565b61SSam Leffler };
390c1565b61SSam Leffler #undef G
391c1565b61SSam Leffler 
392c1565b61SSam Leffler #define H(_r) \
393c1565b61SSam Leffler     (((_r) == 3)   ? 0 : (((_r) == 4.5) ? 1 : (((_r) == 6)  ? 2 : \
394c1565b61SSam Leffler     (((_r) == 9)   ? 3 : (((_r) == 12)  ? 4 : (((_r) == 18) ? 5 : \
395c1565b61SSam Leffler     (((_r) == 24)  ? 6 : (((_r) == 27)  ? 7 : 0))))))))
396c1565b61SSam Leffler static const struct txschedule series_half[] = {
397c1565b61SSam Leffler 	{ 3,H( 3), 3,H(  3), 0,H(  3), 0,H( 3) },	/*   3Mb/s */
398c1565b61SSam Leffler 	{ 4,H(4.5),3,H(  3), 4,H(  3), 0,H( 3) },	/* 4.5Mb/s */
399c1565b61SSam Leffler 	{ 4,H( 6), 3,H(  3), 4,H(  3), 0,H( 3) },	/*   6Mb/s */
400c1565b61SSam Leffler 	{ 4,H( 9), 3,H(  6), 4,H(  3), 2,H( 3) },	/*   9Mb/s */
401c1565b61SSam Leffler 	{ 4,H(12), 3,H(  9), 4,H(  6), 2,H( 3) },	/*  12Mb/s */
402c1565b61SSam Leffler 	{ 4,H(18), 3,H( 12), 4,H(  9), 2,H( 3) },	/*  18Mb/s */
403c1565b61SSam Leffler 	{ 4,H(24), 3,H( 18), 4,H( 12), 2,H( 6) },	/*  24Mb/s */
404c1565b61SSam Leffler 	{ 4,H(27), 3,H( 24), 4,H( 18), 2,H(12) }	/*  27Mb/s */
405c1565b61SSam Leffler };
406c1565b61SSam Leffler #undef H
407c1565b61SSam Leffler 
408fe7432d7SSam Leffler #ifdef Q
409fe7432d7SSam Leffler #undef Q		/* sun4v bogosity */
410fe7432d7SSam Leffler #endif
411c1565b61SSam Leffler #define Q(_r) \
412c1565b61SSam Leffler     (((_r) == 1.5) ? 0 : (((_r) ==2.25) ? 1 : (((_r) == 3)  ? 2 : \
413c1565b61SSam Leffler     (((_r) == 4.5) ? 3 : (((_r) ==  6)  ? 4 : (((_r) == 9)  ? 5 : \
414c1565b61SSam Leffler     (((_r) == 12)  ? 6 : (((_r) == 13.5)? 7 : 0))))))))
415c1565b61SSam Leffler static const struct txschedule series_quarter[] = {
416c1565b61SSam Leffler 	{ 3,Q( 1.5),3,Q(1.5), 0,Q(1.5), 0,Q(1.5) },	/* 1.5Mb/s */
417c1565b61SSam Leffler 	{ 4,Q(2.25),3,Q(1.5), 4,Q(1.5), 0,Q(1.5) },	/*2.25Mb/s */
418c1565b61SSam Leffler 	{ 4,Q(   3),3,Q(1.5), 4,Q(1.5), 0,Q(1.5) },	/*   3Mb/s */
419c1565b61SSam Leffler 	{ 4,Q( 4.5),3,Q(  3), 4,Q(1.5), 2,Q(1.5) },	/* 4.5Mb/s */
420c1565b61SSam Leffler 	{ 4,Q(   6),3,Q(4.5), 4,Q(  3), 2,Q(1.5) },	/*   6Mb/s */
421c1565b61SSam Leffler 	{ 4,Q(   9),3,Q(  6), 4,Q(4.5), 2,Q(1.5) },	/*   9Mb/s */
422c1565b61SSam Leffler 	{ 4,Q(  12),3,Q(  9), 4,Q(  6), 2,Q(  3) },	/*  12Mb/s */
423c1565b61SSam Leffler 	{ 4,Q(13.5),3,Q( 12), 4,Q(  9), 2,Q(  6) }	/*13.5Mb/s */
424c1565b61SSam Leffler };
425c1565b61SSam Leffler #undef Q
426c1565b61SSam Leffler 
427fa20c234SSam Leffler void
428fa20c234SSam Leffler ath_rate_setupxtxdesc(struct ath_softc *sc, struct ath_node *an,
429a4d8dd10SSam Leffler 		      struct ath_desc *ds, int shortPreamble, u_int8_t rix)
430fa20c234SSam Leffler {
431fa20c234SSam Leffler 	struct sample_node *sn = ATH_NODE_SAMPLE(an);
432c1565b61SSam Leffler 	const struct txschedule *sched = &sn->sched[rix];
433c1565b61SSam Leffler 	const HAL_RATE_TABLE *rt = sc->sc_currates;
434c1565b61SSam Leffler 	uint8_t rix1, s1code, rix2, s2code, rix3, s3code;
435fa20c234SSam Leffler 
436c1565b61SSam Leffler 	/* XXX precalculate short preamble tables */
437c1565b61SSam Leffler 	rix1 = sched->r1;
438c1565b61SSam Leffler 	s1code = rt->info[rix1].rateCode
439c1565b61SSam Leffler 	       | (shortPreamble ? rt->info[rix1].shortPreamble : 0);
440c1565b61SSam Leffler 	rix2 = sched->r2;
441c1565b61SSam Leffler 	s2code = rt->info[rix2].rateCode
442c1565b61SSam Leffler 	       | (shortPreamble ? rt->info[rix2].shortPreamble : 0);
443c1565b61SSam Leffler 	rix3 = sched->r3;
444c1565b61SSam Leffler 	s3code = rt->info[rix3].rateCode
445c1565b61SSam Leffler 	       | (shortPreamble ? rt->info[rix3].shortPreamble : 0);
446c1565b61SSam Leffler 	ath_hal_setupxtxdesc(sc->sc_ah, ds,
447c1565b61SSam Leffler 	    s1code, sched->t1,		/* series 1 */
448c1565b61SSam Leffler 	    s2code, sched->t2,		/* series 2 */
449c1565b61SSam Leffler 	    s3code, sched->t3);		/* series 3 */
450fa20c234SSam Leffler }
451fa20c234SSam Leffler 
452b2763056SSam Leffler static void
453b2763056SSam Leffler update_stats(struct ath_softc *sc, struct ath_node *an,
454b2763056SSam Leffler 		  int frame_size,
455c1565b61SSam Leffler 		  int rix0, int tries0,
456c1565b61SSam Leffler 		  int rix1, int tries1,
457c1565b61SSam Leffler 		  int rix2, int tries2,
458c1565b61SSam Leffler 		  int rix3, int tries3,
459b2763056SSam Leffler 		  int short_tries, int tries, int status)
460fa20c234SSam Leffler {
461fa20c234SSam Leffler 	struct sample_node *sn = ATH_NODE_SAMPLE(an);
462fa20c234SSam Leffler 	struct sample_softc *ssc = ATH_SOFTC_SAMPLE(sc);
463c1565b61SSam Leffler 	const int size_bin = size_to_bin(frame_size);
464c1565b61SSam Leffler 	const int size = bin_to_size(size_bin);
465c1565b61SSam Leffler 	int tt, tries_so_far;
466fa20c234SSam Leffler 
467c1565b61SSam Leffler 	if (!IS_RATE_DEFINED(sn, rix0))
46865f9edeeSSam Leffler 		return;
469c1565b61SSam Leffler 	tt = calc_usecs_unicast_packet(sc, size, rix0, short_tries,
470b2763056SSam Leffler 		MIN(tries0, tries) - 1);
471c1565b61SSam Leffler 	tries_so_far = tries0;
472c1565b61SSam Leffler 
473c1565b61SSam Leffler 	if (tries1 && tries_so_far < tries) {
474c1565b61SSam Leffler 		if (!IS_RATE_DEFINED(sn, rix1))
47565f9edeeSSam Leffler 			return;
476c1565b61SSam Leffler 		tt += calc_usecs_unicast_packet(sc, size, rix1, short_tries,
477b2763056SSam Leffler 			MIN(tries1 + tries_so_far, tries) - tries_so_far - 1);
478b2763056SSam Leffler 		tries_so_far += tries1;
479b2763056SSam Leffler 	}
480b2763056SSam Leffler 
481c1565b61SSam Leffler 	if (tries2 && tries_so_far < tries) {
482c1565b61SSam Leffler 		if (!IS_RATE_DEFINED(sn, rix2))
48365f9edeeSSam Leffler 			return;
484c1565b61SSam Leffler 		tt += calc_usecs_unicast_packet(sc, size, rix2, short_tries,
485c1565b61SSam Leffler 			MIN(tries2 + tries_so_far, tries) - tries_so_far - 1);
486c1565b61SSam Leffler 		tries_so_far += tries2;
487c1565b61SSam Leffler 	}
488c1565b61SSam Leffler 
489c1565b61SSam Leffler 	if (tries3 && tries_so_far < tries) {
490c1565b61SSam Leffler 		if (!IS_RATE_DEFINED(sn, rix3))
491c1565b61SSam Leffler 			return;
492c1565b61SSam Leffler 		tt += calc_usecs_unicast_packet(sc, size, rix3, short_tries,
493b2763056SSam Leffler 			MIN(tries3 + tries_so_far, tries) - tries_so_far - 1);
494b2763056SSam Leffler 	}
495c1565b61SSam Leffler 
496c1565b61SSam Leffler 	if (sn->stats[size_bin][rix0].total_packets < ssc->smoothing_minpackets) {
497fa20c234SSam Leffler 		/* just average the first few packets */
498c1565b61SSam Leffler 		int avg_tx = sn->stats[size_bin][rix0].average_tx_time;
499c1565b61SSam Leffler 		int packets = sn->stats[size_bin][rix0].total_packets;
500c1565b61SSam Leffler 		sn->stats[size_bin][rix0].average_tx_time = (tt+(avg_tx*packets))/(packets+1);
501fa20c234SSam Leffler 	} else {
502fa20c234SSam Leffler 		/* use a ewma */
503c1565b61SSam Leffler 		sn->stats[size_bin][rix0].average_tx_time =
504c1565b61SSam Leffler 			((sn->stats[size_bin][rix0].average_tx_time * ssc->smoothing_rate) +
505c1565b61SSam Leffler 			 (tt * (100 - ssc->smoothing_rate))) / 100;
506fa20c234SSam Leffler 	}
507fa20c234SSam Leffler 
508c1565b61SSam Leffler 	if (status != 0) {
509fa20c234SSam Leffler 		int y;
510c1565b61SSam Leffler 		sn->stats[size_bin][rix0].successive_failures++;
511b91bf513SSam Leffler 		for (y = size_bin+1; y < NUM_PACKET_SIZE_BINS; y++) {
512c1565b61SSam Leffler 			/*
513c1565b61SSam Leffler 			 * Also say larger packets failed since we
514c1565b61SSam Leffler 			 * assume if a small packet fails at a
515b91bf513SSam Leffler 			 * bit-rate then a larger one will also.
516b91bf513SSam Leffler 			 */
517c1565b61SSam Leffler 			sn->stats[y][rix0].successive_failures++;
518c1565b61SSam Leffler 			sn->stats[y][rix0].last_tx = ticks;
519c1565b61SSam Leffler 			sn->stats[y][rix0].tries += tries;
520c1565b61SSam Leffler 			sn->stats[y][rix0].total_packets++;
521fa20c234SSam Leffler 		}
522fa20c234SSam Leffler 	} else {
523c1565b61SSam Leffler 		sn->stats[size_bin][rix0].packets_acked++;
524c1565b61SSam Leffler 		sn->stats[size_bin][rix0].successive_failures = 0;
525fa20c234SSam Leffler 	}
526c1565b61SSam Leffler 	sn->stats[size_bin][rix0].tries += tries;
527c1565b61SSam Leffler 	sn->stats[size_bin][rix0].last_tx = ticks;
528c1565b61SSam Leffler 	sn->stats[size_bin][rix0].total_packets++;
529b2763056SSam Leffler 
530c1565b61SSam Leffler 	if (rix0 == sn->current_sample_rix[size_bin]) {
531b032f27cSSam Leffler 		IEEE80211_NOTE(an->an_node.ni_vap, IEEE80211_MSG_RATECTL,
532b032f27cSSam Leffler 		   &an->an_node,
533b032f27cSSam Leffler "%s: size %d %s sample rate %d tries (%d/%d) tt %d avg_tt (%d/%d)",
534b032f27cSSam Leffler 		    __func__,
53565f9edeeSSam Leffler 		    size,
53665f9edeeSSam Leffler 		    status ? "FAIL" : "OK",
537c1565b61SSam Leffler 		    rix0, short_tries, tries, tt,
538c1565b61SSam Leffler 		    sn->stats[size_bin][rix0].average_tx_time,
539c1565b61SSam Leffler 		    sn->stats[size_bin][rix0].perfect_tx_time);
540b2763056SSam Leffler 		sn->sample_tt[size_bin] = tt;
541c1565b61SSam Leffler 		sn->current_sample_rix[size_bin] = -1;
542b2763056SSam Leffler 	}
543b2763056SSam Leffler }
544b2763056SSam Leffler 
545ec9ee5e7SSam Leffler static void
546ec9ee5e7SSam Leffler badrate(struct ifnet *ifp, int series, int hwrate, int tries, int status)
547ec9ee5e7SSam Leffler {
548ec9ee5e7SSam Leffler 	if_printf(ifp, "bad series%d hwrate 0x%x, tries %u ts_status 0x%x\n",
549ec9ee5e7SSam Leffler 	    series, hwrate, tries, status);
550ec9ee5e7SSam Leffler }
551ec9ee5e7SSam Leffler 
552b2763056SSam Leffler void
55343e9cf7cSSam Leffler ath_rate_tx_complete(struct ath_softc *sc, struct ath_node *an,
55465f9edeeSSam Leffler 	const struct ath_buf *bf)
555b2763056SSam Leffler {
556b032f27cSSam Leffler 	struct ifnet *ifp = sc->sc_ifp;
557b032f27cSSam Leffler 	struct ieee80211com *ic = ifp->if_l2com;
558b2763056SSam Leffler 	struct sample_node *sn = ATH_NODE_SAMPLE(an);
55965f9edeeSSam Leffler 	const struct ath_tx_status *ts = &bf->bf_status.ds_txstat;
56065f9edeeSSam Leffler 	const struct ath_desc *ds0 = &bf->bf_desc[0];
561c1565b61SSam Leffler 	int final_rix, short_tries, long_tries, frame_size;
56246d4d74cSSam Leffler 	const HAL_RATE_TABLE *rt = sc->sc_currates;
5631bb9a085SSam Leffler 	int mrr;
564b2763056SSam Leffler 
565c1565b61SSam Leffler 	final_rix = rt->rateCodeToIndex[ts->ts_rate &~ HAL_TXSTAT_ALTRATE];
56665f9edeeSSam Leffler 	short_tries = ts->ts_shortretry;
56765f9edeeSSam Leffler 	long_tries = ts->ts_longretry + 1;
56843e9cf7cSSam Leffler 	frame_size = ds0->ds_ctl0 & 0x0fff; /* low-order 12 bits of ds_ctl0 */
56943e9cf7cSSam Leffler 	if (frame_size == 0)		    /* NB: should not happen */
570b2763056SSam Leffler 		frame_size = 1500;
571b2763056SSam Leffler 
572c1565b61SSam Leffler 	if (sn->ratemask == 0) {
573b032f27cSSam Leffler 		IEEE80211_NOTE(an->an_node.ni_vap, IEEE80211_MSG_RATECTL,
574b032f27cSSam Leffler 		    &an->an_node,
575b032f27cSSam Leffler 		    "%s: size %d %s rate/try %d/%d no rates yet",
576b032f27cSSam Leffler 		    __func__,
57743e9cf7cSSam Leffler 		    bin_to_size(size_to_bin(frame_size)),
57865f9edeeSSam Leffler 		    ts->ts_status ? "FAIL" : "OK",
57943e9cf7cSSam Leffler 		    short_tries, long_tries);
580b2763056SSam Leffler 		return;
581b2763056SSam Leffler 	}
58265f9edeeSSam Leffler 	mrr = sc->sc_mrretry && !(ic->ic_flags & IEEE80211_F_USEPROT);
58365f9edeeSSam Leffler 	if (!mrr || !(ts->ts_rate & HAL_TXSTAT_ALTRATE)) {
584c1565b61SSam Leffler 		if (!IS_RATE_DEFINED(sn, final_rix)) {
585ec9ee5e7SSam Leffler 			badrate(ifp, 0, ts->ts_rate, long_tries, ts->ts_status);
586ec9ee5e7SSam Leffler 			return;
587ec9ee5e7SSam Leffler 		}
58865f9edeeSSam Leffler 		/*
58965f9edeeSSam Leffler 		 * Only one rate was used; optimize work.
59065f9edeeSSam Leffler 		 */
591b032f27cSSam Leffler 		IEEE80211_NOTE(an->an_node.ni_vap, IEEE80211_MSG_RATECTL,
592b032f27cSSam Leffler 		     &an->an_node, "%s: size %d %s rate/try %d/%d/%d",
593b032f27cSSam Leffler 		     __func__,
59443e9cf7cSSam Leffler 		     bin_to_size(size_to_bin(frame_size)),
59565f9edeeSSam Leffler 		     ts->ts_status ? "FAIL" : "OK",
596c1565b61SSam Leffler 		     final_rix, short_tries, long_tries);
597b2763056SSam Leffler 		update_stats(sc, an, frame_size,
598c1565b61SSam Leffler 			     final_rix, long_tries,
599b2763056SSam Leffler 			     0, 0,
600b2763056SSam Leffler 			     0, 0,
601b2763056SSam Leffler 			     0, 0,
60265f9edeeSSam Leffler 			     short_tries, long_tries, ts->ts_status);
603b2763056SSam Leffler 	} else {
604c1565b61SSam Leffler 		int hwrate0, rix0, tries0;
605c1565b61SSam Leffler 		int hwrate1, rix1, tries1;
606c1565b61SSam Leffler 		int hwrate2, rix2, tries2;
607c1565b61SSam Leffler 		int hwrate3, rix3, tries3;
60865f9edeeSSam Leffler 		int finalTSIdx = ts->ts_finaltsi;
609b2763056SSam Leffler 
610b2763056SSam Leffler 		/*
611b2763056SSam Leffler 		 * Process intermediate rates that failed.
612b2763056SSam Leffler 		 */
613517eabc6SSam Leffler 		if (sc->sc_ah->ah_magic != 0x20065416) {
614517eabc6SSam Leffler 			hwrate0 = MS(ds0->ds_ctl3, AR_XmitRate0);
615517eabc6SSam Leffler 			hwrate1 = MS(ds0->ds_ctl3, AR_XmitRate1);
616517eabc6SSam Leffler 			hwrate2 = MS(ds0->ds_ctl3, AR_XmitRate2);
617517eabc6SSam Leffler 			hwrate3 = MS(ds0->ds_ctl3, AR_XmitRate3);
618517eabc6SSam Leffler 		} else {
619517eabc6SSam Leffler 			hwrate0 = MS(ds0->ds_ctl3, AR5416_XmitRate0);
620517eabc6SSam Leffler 			hwrate1 = MS(ds0->ds_ctl3, AR5416_XmitRate1);
621517eabc6SSam Leffler 			hwrate2 = MS(ds0->ds_ctl3, AR5416_XmitRate2);
622517eabc6SSam Leffler 			hwrate3 = MS(ds0->ds_ctl3, AR5416_XmitRate3);
623517eabc6SSam Leffler 		}
624517eabc6SSam Leffler 
625c1565b61SSam Leffler 		rix0 = rt->rateCodeToIndex[hwrate0];
62665f9edeeSSam Leffler 		tries0 = MS(ds0->ds_ctl2, AR_XmitDataTries0);
627b2763056SSam Leffler 
628c1565b61SSam Leffler 		rix1 = rt->rateCodeToIndex[hwrate1];
62965f9edeeSSam Leffler 		tries1 = MS(ds0->ds_ctl2, AR_XmitDataTries1);
630b2763056SSam Leffler 
631c1565b61SSam Leffler 		rix2 = rt->rateCodeToIndex[hwrate2];
63265f9edeeSSam Leffler 		tries2 = MS(ds0->ds_ctl2, AR_XmitDataTries2);
633b2763056SSam Leffler 
634c1565b61SSam Leffler 		rix3 = rt->rateCodeToIndex[hwrate3];
63565f9edeeSSam Leffler 		tries3 = MS(ds0->ds_ctl2, AR_XmitDataTries3);
636ec9ee5e7SSam Leffler 
637b032f27cSSam Leffler 		IEEE80211_NOTE(an->an_node.ni_vap, IEEE80211_MSG_RATECTL,
638b032f27cSSam Leffler 		    &an->an_node,
639b032f27cSSam Leffler "%s: size %d finaltsidx %d tries %d %s rate/try [%d/%d %d/%d %d/%d %d/%d]",
640b032f27cSSam Leffler 		     __func__,
641b2763056SSam Leffler 		     bin_to_size(size_to_bin(frame_size)),
642b2763056SSam Leffler 		     finalTSIdx,
643b2763056SSam Leffler 		     long_tries,
64465f9edeeSSam Leffler 		     ts->ts_status ? "FAIL" : "OK",
645c1565b61SSam Leffler 		     rix0, tries0,
646c1565b61SSam Leffler 		     rix1, tries1,
647c1565b61SSam Leffler 		     rix2, tries2,
648c1565b61SSam Leffler 		     rix3, tries3);
649c1565b61SSam Leffler 
650c1565b61SSam Leffler 		if (tries0 && !IS_RATE_DEFINED(sn, rix0))
651c1565b61SSam Leffler 			badrate(ifp, 0, hwrate0, tries0, ts->ts_status);
652c1565b61SSam Leffler 		if (tries1 && !IS_RATE_DEFINED(sn, rix1))
653c1565b61SSam Leffler 			badrate(ifp, 1, hwrate1, tries1, ts->ts_status);
654c1565b61SSam Leffler 		if (tries2 && !IS_RATE_DEFINED(sn, rix2))
655c1565b61SSam Leffler 			badrate(ifp, 2, hwrate2, tries2, ts->ts_status);
656c1565b61SSam Leffler 		if (tries3 && !IS_RATE_DEFINED(sn, rix3))
657c1565b61SSam Leffler 			badrate(ifp, 3, hwrate3, tries3, ts->ts_status);
658b2763056SSam Leffler 
65965f9edeeSSam Leffler 		/*
66065f9edeeSSam Leffler 		 * NB: series > 0 are not penalized for failure
66165f9edeeSSam Leffler 		 * based on the try counts under the assumption
66265f9edeeSSam Leffler 		 * that losses are often bursty and since we
66365f9edeeSSam Leffler 		 * sample higher rates 1 try at a time doing so
66465f9edeeSSam Leffler 		 * may unfairly penalize them.
66565f9edeeSSam Leffler 		 */
666b2763056SSam Leffler 		if (tries0) {
667b2763056SSam Leffler 			update_stats(sc, an, frame_size,
668c1565b61SSam Leffler 				     rix0, tries0,
669c1565b61SSam Leffler 				     rix1, tries1,
670c1565b61SSam Leffler 				     rix2, tries2,
671c1565b61SSam Leffler 				     rix3, tries3,
67265f9edeeSSam Leffler 				     short_tries, long_tries,
673b91bf513SSam Leffler 				     long_tries > tries0);
67465f9edeeSSam Leffler 			long_tries -= tries0;
675b2763056SSam Leffler 		}
676b2763056SSam Leffler 
677b2763056SSam Leffler 		if (tries1 && finalTSIdx > 0) {
678b2763056SSam Leffler 			update_stats(sc, an, frame_size,
679c1565b61SSam Leffler 				     rix1, tries1,
680c1565b61SSam Leffler 				     rix2, tries2,
681c1565b61SSam Leffler 				     rix3, tries3,
682b2763056SSam Leffler 				     0, 0,
68365f9edeeSSam Leffler 				     short_tries, long_tries,
68465f9edeeSSam Leffler 				     ts->ts_status);
68565f9edeeSSam Leffler 			long_tries -= tries1;
686b2763056SSam Leffler 		}
687b2763056SSam Leffler 
688b2763056SSam Leffler 		if (tries2 && finalTSIdx > 1) {
689b2763056SSam Leffler 			update_stats(sc, an, frame_size,
690c1565b61SSam Leffler 				     rix2, tries2,
691c1565b61SSam Leffler 				     rix3, tries3,
692b2763056SSam Leffler 				     0, 0,
693b2763056SSam Leffler 				     0, 0,
69465f9edeeSSam Leffler 				     short_tries, long_tries,
69565f9edeeSSam Leffler 				     ts->ts_status);
69665f9edeeSSam Leffler 			long_tries -= tries2;
697b2763056SSam Leffler 		}
698b2763056SSam Leffler 
699b2763056SSam Leffler 		if (tries3 && finalTSIdx > 2) {
700b2763056SSam Leffler 			update_stats(sc, an, frame_size,
701c1565b61SSam Leffler 				     rix3, tries3,
702b2763056SSam Leffler 				     0, 0,
703b2763056SSam Leffler 				     0, 0,
704b2763056SSam Leffler 				     0, 0,
70565f9edeeSSam Leffler 				     short_tries, long_tries,
70665f9edeeSSam Leffler 				     ts->ts_status);
707b2763056SSam Leffler 		}
708b2763056SSam Leffler 	}
709fa20c234SSam Leffler }
710fa20c234SSam Leffler 
711fa20c234SSam Leffler void
712fa20c234SSam Leffler ath_rate_newassoc(struct ath_softc *sc, struct ath_node *an, int isnew)
713fa20c234SSam Leffler {
714fa20c234SSam Leffler 	if (isnew)
715b2763056SSam Leffler 		ath_rate_ctl_reset(sc, &an->an_node);
716fa20c234SSam Leffler }
717fa20c234SSam Leffler 
718c1565b61SSam Leffler static const struct txschedule *mrr_schedules[IEEE80211_MODE_MAX+2] = {
719c1565b61SSam Leffler 	NULL,		/* IEEE80211_MODE_AUTO */
720c1565b61SSam Leffler 	series_11a,	/* IEEE80211_MODE_11A */
721c1565b61SSam Leffler 	series_11g,	/* IEEE80211_MODE_11B */
722c1565b61SSam Leffler 	series_11g,	/* IEEE80211_MODE_11G */
723c1565b61SSam Leffler 	NULL,		/* IEEE80211_MODE_FH */
724c1565b61SSam Leffler 	series_11a,	/* IEEE80211_MODE_TURBO_A */
725c1565b61SSam Leffler 	series_11g,	/* IEEE80211_MODE_TURBO_G */
726c1565b61SSam Leffler 	series_11a,	/* IEEE80211_MODE_STURBO_A */
727c1565b61SSam Leffler 	series_11a,	/* IEEE80211_MODE_11NA */
728c1565b61SSam Leffler 	series_11g,	/* IEEE80211_MODE_11NG */
729c1565b61SSam Leffler 	series_half,	/* IEEE80211_MODE_HALF */
730c1565b61SSam Leffler 	series_quarter,	/* IEEE80211_MODE_QUARTER */
731c1565b61SSam Leffler };
732c1565b61SSam Leffler 
733fa20c234SSam Leffler /*
734fa20c234SSam Leffler  * Initialize the tables for a node.
735fa20c234SSam Leffler  */
736fa20c234SSam Leffler static void
737b2763056SSam Leffler ath_rate_ctl_reset(struct ath_softc *sc, struct ieee80211_node *ni)
738fa20c234SSam Leffler {
739fa20c234SSam Leffler #define	RATE(_ix)	(ni->ni_rates.rs_rates[(_ix)] & IEEE80211_RATE_VAL)
740c1565b61SSam Leffler #define	DOT11RATE(_ix)	(rt->info[(_ix)].dot11Rate & IEEE80211_RATE_VAL)
741fa20c234SSam Leffler 	struct ath_node *an = ATH_NODE(ni);
742c62362cbSSam Leffler 	const struct ieee80211_txparam *tp = ni->ni_txparms;
743fa20c234SSam Leffler 	struct sample_node *sn = ATH_NODE_SAMPLE(an);
744fa20c234SSam Leffler 	const HAL_RATE_TABLE *rt = sc->sc_currates;
745c1565b61SSam Leffler 	int x, y, srate, rix;
746fa20c234SSam Leffler 
747fa20c234SSam Leffler 	KASSERT(rt != NULL, ("no rate table, mode %u", sc->sc_curmode));
748c1565b61SSam Leffler 
749c1565b61SSam Leffler 	KASSERT(sc->sc_curmode < IEEE80211_MODE_MAX+2,
750c1565b61SSam Leffler 	    ("curmode %u", sc->sc_curmode));
751c1565b61SSam Leffler 	sn->sched = mrr_schedules[sc->sc_curmode];
752c1565b61SSam Leffler 	KASSERT(sn->sched != NULL,
753c1565b61SSam Leffler 	    ("no mrr schedule for mode %u", sc->sc_curmode));
754c1565b61SSam Leffler 
755c1565b61SSam Leffler         sn->static_rix = -1;
756b032f27cSSam Leffler 	if (tp != NULL && tp->ucastrate != IEEE80211_FIXED_RATE_NONE) {
757fa20c234SSam Leffler 		/*
75868e8e04eSSam Leffler 		 * A fixed rate is to be used; ic_fixed_rate is the
75968e8e04eSSam Leffler 		 * IEEE code for this rate (sans basic bit).  Convert this
760fa20c234SSam Leffler 		 * to the index into the negotiated rate set for
761aaba14d7SSam Leffler 		 * the node.
762fa20c234SSam Leffler 		 */
763fa20c234SSam Leffler 		/* NB: the rate set is assumed sorted */
764fa20c234SSam Leffler 		srate = ni->ni_rates.rs_nrates - 1;
765b032f27cSSam Leffler 		for (; srate >= 0 && RATE(srate) != tp->ucastrate; srate--)
766fa20c234SSam Leffler 			;
76768e8e04eSSam Leffler 		/*
76868e8e04eSSam Leffler 		 * The fixed rate may not be available due to races
76968e8e04eSSam Leffler 		 * and mode settings.  Also orphaned nodes created in
77068e8e04eSSam Leffler 		 * adhoc mode may not have any rate set so this lookup
77168e8e04eSSam Leffler 		 * can fail.
77268e8e04eSSam Leffler 		 */
77368e8e04eSSam Leffler 		if (srate >= 0)
774c1565b61SSam Leffler 			sn->static_rix = sc->sc_rixmap[srate];
775fa20c234SSam Leffler 	}
776b2763056SSam Leffler 
777c1565b61SSam Leffler 	/*
778c1565b61SSam Leffler 	 * Construct a bitmask of usable rates.  This has all
779c1565b61SSam Leffler 	 * negotiated rates minus those marked by the hal as
780c1565b61SSam Leffler 	 * to be ignored for doing rate control.
781c1565b61SSam Leffler 	 */
782c1565b61SSam Leffler 	sn->ratemask = 0;
783fa20c234SSam Leffler 	for (x = 0; x < ni->ni_rates.rs_nrates; x++) {
784c1565b61SSam Leffler 		rix = sc->sc_rixmap[RATE(x)];
785c1565b61SSam Leffler 		if (rix == 0xff)
7860ae09ec5SSam Leffler 			continue;
787c1565b61SSam Leffler 		/* skip rates marked broken by hal */
788c1565b61SSam Leffler 		if (!rt->info[rix].valid)
789c1565b61SSam Leffler 			continue;
790c1565b61SSam Leffler 		KASSERT(rix < SAMPLE_MAXRATES,
791c1565b61SSam Leffler 		    ("rate %u has rix %d", RATE(x), rix));
792c1565b61SSam Leffler 		sn->ratemask |= 1<<rix;
793b2763056SSam Leffler 	}
794b032f27cSSam Leffler #ifdef IEEE80211_DEBUG
795b032f27cSSam Leffler 	if (ieee80211_msg(ni->ni_vap, IEEE80211_MSG_RATECTL)) {
796c1565b61SSam Leffler 		uint32_t mask;
797c1565b61SSam Leffler 
798b032f27cSSam Leffler 		ieee80211_note(ni->ni_vap, "[%6D] %s: size 1600 rate/tt",
799c1565b61SSam Leffler 		    ni->ni_macaddr, ":", __func__);
800c1565b61SSam Leffler 		for (mask = sn->ratemask, rix = 0; mask != 0; mask >>= 1, rix++) {
801c1565b61SSam Leffler 			if ((mask & 1) == 0)
802b032f27cSSam Leffler 				continue;
803c1565b61SSam Leffler 			printf(" %d/%d", DOT11RATE(rix) / 2,
804c1565b61SSam Leffler 			    calc_usecs_unicast_packet(sc, 1600, rix, 0,0));
805b032f27cSSam Leffler 		}
806b032f27cSSam Leffler 		printf("\n");
807b032f27cSSam Leffler 	}
808b032f27cSSam Leffler #endif
809b2763056SSam Leffler 	for (y = 0; y < NUM_PACKET_SIZE_BINS; y++) {
810b2763056SSam Leffler 		int size = bin_to_size(y);
811c1565b61SSam Leffler 		uint32_t mask;
812c1565b61SSam Leffler 
813b2763056SSam Leffler 		sn->packets_sent[y] = 0;
814c1565b61SSam Leffler 		sn->current_sample_rix[y] = -1;
815c1565b61SSam Leffler 		sn->last_sample_rix[y] = 0;
816c1565b61SSam Leffler 		/* XXX start with first valid rate */
817c1565b61SSam Leffler 		sn->current_rix[y] = ffs(sn->ratemask)-1;
818b2763056SSam Leffler 
819c1565b61SSam Leffler 		/*
820c1565b61SSam Leffler 		 * Initialize the statistics buckets; these are
821c1565b61SSam Leffler 		 * indexed by the rate code index.
822c1565b61SSam Leffler 		 */
823c1565b61SSam Leffler 		for (rix = 0, mask = sn->ratemask; mask != 0; rix++, mask >>= 1) {
824c1565b61SSam Leffler 			if ((mask & 1) == 0)		/* not a valid rate */
825c1565b61SSam Leffler 				continue;
826c1565b61SSam Leffler 			sn->stats[y][rix].successive_failures = 0;
827c1565b61SSam Leffler 			sn->stats[y][rix].tries = 0;
828c1565b61SSam Leffler 			sn->stats[y][rix].total_packets = 0;
829c1565b61SSam Leffler 			sn->stats[y][rix].packets_acked = 0;
830c1565b61SSam Leffler 			sn->stats[y][rix].last_tx = 0;
831b2763056SSam Leffler 
832c1565b61SSam Leffler 			sn->stats[y][rix].perfect_tx_time =
833c1565b61SSam Leffler 			    calc_usecs_unicast_packet(sc, size, rix, 0, 0);
834c1565b61SSam Leffler 			sn->stats[y][rix].average_tx_time =
835c1565b61SSam Leffler 			    sn->stats[y][rix].perfect_tx_time;
836b2763056SSam Leffler 		}
837b91bf513SSam Leffler 	}
838c1565b61SSam Leffler #if 0
839c1565b61SSam Leffler 	/* XXX 0, num_rates-1 are wrong */
840b032f27cSSam Leffler 	IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_RATECTL, ni,
841b032f27cSSam Leffler 	    "%s: %d rates %d%sMbps (%dus)- %d%sMbps (%dus)", __func__,
842b91bf513SSam Leffler 	    sn->num_rates,
843c1565b61SSam Leffler 	    DOT11RATE(0)/2, DOT11RATE(0) % 1 ? ".5" : "",
844b91bf513SSam Leffler 	    sn->stats[1][0].perfect_tx_time,
845c1565b61SSam Leffler 	    DOT11RATE(sn->num_rates-1)/2, DOT11RATE(sn->num_rates-1) % 1 ? ".5" : "",
846b91bf513SSam Leffler 	    sn->stats[1][sn->num_rates-1].perfect_tx_time
847b91bf513SSam Leffler 	);
848c1565b61SSam Leffler #endif
849b032f27cSSam Leffler 	/* set the visible bit-rate */
850c1565b61SSam Leffler 	if (sn->static_rix != -1)
851c1565b61SSam Leffler 		ni->ni_txrate = DOT11RATE(sn->static_rix);
852d0d425bfSSam Leffler 	else
853c1565b61SSam Leffler 		ni->ni_txrate = RATE(0);
854fa20c234SSam Leffler #undef RATE
855c1565b61SSam Leffler #undef DOT11RATE
856fa20c234SSam Leffler }
857fa20c234SSam Leffler 
858f0fd5e07SSam Leffler static void
859c1565b61SSam Leffler sample_stats(void *arg, struct ieee80211_node *ni)
860c1565b61SSam Leffler {
861c1565b61SSam Leffler 	struct ath_softc *sc = arg;
862c1565b61SSam Leffler 	const HAL_RATE_TABLE *rt = sc->sc_currates;
863c1565b61SSam Leffler 	struct sample_node *sn = ATH_NODE_SAMPLE(ATH_NODE(ni));
864c1565b61SSam Leffler 	uint32_t mask;
865c1565b61SSam Leffler 	int rix, y;
866c1565b61SSam Leffler 
867c1565b61SSam Leffler 	printf("\n[%s] refcnt %d static_rix %d ratemask 0x%x\n",
868c1565b61SSam Leffler 	    ether_sprintf(ni->ni_macaddr), ieee80211_node_refcnt(ni),
869c1565b61SSam Leffler 	    sn->static_rix, sn->ratemask);
870c1565b61SSam Leffler 	for (y = 0; y < NUM_PACKET_SIZE_BINS; y++) {
871c1565b61SSam Leffler 		printf("[%4u] cur rix %d since switch: packets %d ticks %u\n",
872c1565b61SSam Leffler 		    bin_to_size(y), sn->current_rix[y],
873c1565b61SSam Leffler 		    sn->packets_since_switch[y], sn->ticks_since_switch[y]);
874c1565b61SSam Leffler 		printf("[%4u] last sample %d cur sample %d packets sent %d\n",
875c1565b61SSam Leffler 		    bin_to_size(y), sn->last_sample_rix[y],
876c1565b61SSam Leffler 		    sn->current_sample_rix[y], sn->packets_sent[y]);
877c1565b61SSam Leffler 		printf("[%4u] packets since sample %d sample tt %u\n",
878c1565b61SSam Leffler 		    bin_to_size(y), sn->packets_since_sample[y],
879c1565b61SSam Leffler 		    sn->sample_tt[y]);
880c1565b61SSam Leffler 	}
881c1565b61SSam Leffler 	for (mask = sn->ratemask, rix = 0; mask != 0; mask >>= 1, rix++) {
882c1565b61SSam Leffler 		if ((mask & 1) == 0)
883c1565b61SSam Leffler 				continue;
884c1565b61SSam Leffler 		for (y = 0; y < NUM_PACKET_SIZE_BINS; y++) {
885c1565b61SSam Leffler 			if (sn->stats[y][rix].total_packets == 0)
886c1565b61SSam Leffler 				continue;
887c1565b61SSam Leffler 			printf("[%2u:%4u] %8d:%-8d (%3d%%) T %8d F %4d avg %5u last %u\n",
888c1565b61SSam Leffler 			    (rt->info[rix].dot11Rate & IEEE80211_RATE_VAL)/2,
889c1565b61SSam Leffler 			    bin_to_size(y),
890c1565b61SSam Leffler 			    sn->stats[y][rix].total_packets,
891c1565b61SSam Leffler 			    sn->stats[y][rix].packets_acked,
892c1565b61SSam Leffler 			    (100*sn->stats[y][rix].packets_acked)/sn->stats[y][rix].total_packets,
893c1565b61SSam Leffler 			    sn->stats[y][rix].tries,
894c1565b61SSam Leffler 			    sn->stats[y][rix].successive_failures,
895c1565b61SSam Leffler 			    sn->stats[y][rix].average_tx_time,
896c1565b61SSam Leffler 			    ticks - sn->stats[y][rix].last_tx);
897c1565b61SSam Leffler 		}
898c1565b61SSam Leffler 	}
899c1565b61SSam Leffler }
900c1565b61SSam Leffler 
901c1565b61SSam Leffler static int
902c1565b61SSam Leffler ath_rate_sysctl_stats(SYSCTL_HANDLER_ARGS)
903c1565b61SSam Leffler {
904c1565b61SSam Leffler 	struct ath_softc *sc = arg1;
905c1565b61SSam Leffler 	struct ifnet *ifp = sc->sc_ifp;
906c1565b61SSam Leffler 	struct ieee80211com *ic = ifp->if_l2com;
907c1565b61SSam Leffler 	int error, v;
908c1565b61SSam Leffler 
909c1565b61SSam Leffler 	v = 0;
910c1565b61SSam Leffler 	error = sysctl_handle_int(oidp, &v, 0, req);
911c1565b61SSam Leffler 	if (error || !req->newptr)
912c1565b61SSam Leffler 		return error;
913c1565b61SSam Leffler 	ieee80211_iterate_nodes(&ic->ic_sta, sample_stats, sc);
914c1565b61SSam Leffler 	return 0;
915c1565b61SSam Leffler }
916c1565b61SSam Leffler 
917c1565b61SSam Leffler static int
918c1565b61SSam Leffler ath_rate_sysctl_smoothing_rate(SYSCTL_HANDLER_ARGS)
919c1565b61SSam Leffler {
920c1565b61SSam Leffler 	struct sample_softc *ssc = arg1;
921c1565b61SSam Leffler 	int rate, error;
922c1565b61SSam Leffler 
923c1565b61SSam Leffler 	rate = ssc->smoothing_rate;
924c1565b61SSam Leffler 	error = sysctl_handle_int(oidp, &rate, 0, req);
925c1565b61SSam Leffler 	if (error || !req->newptr)
926c1565b61SSam Leffler 		return error;
927c1565b61SSam Leffler 	if (!(0 <= rate && rate < 100))
928c1565b61SSam Leffler 		return EINVAL;
929c1565b61SSam Leffler 	ssc->smoothing_rate = rate;
930c1565b61SSam Leffler 	ssc->smoothing_minpackets = 100 / (100 - rate);
931c1565b61SSam Leffler 	return 0;
932c1565b61SSam Leffler }
933c1565b61SSam Leffler 
934c1565b61SSam Leffler static int
935c1565b61SSam Leffler ath_rate_sysctl_sample_rate(SYSCTL_HANDLER_ARGS)
936c1565b61SSam Leffler {
937c1565b61SSam Leffler 	struct sample_softc *ssc = arg1;
938c1565b61SSam Leffler 	int rate, error;
939c1565b61SSam Leffler 
940c1565b61SSam Leffler 	rate = ssc->sample_rate;
941c1565b61SSam Leffler 	error = sysctl_handle_int(oidp, &rate, 0, req);
942c1565b61SSam Leffler 	if (error || !req->newptr)
943c1565b61SSam Leffler 		return error;
944c1565b61SSam Leffler 	if (!(2 <= rate && rate <= 100))
945c1565b61SSam Leffler 		return EINVAL;
946c1565b61SSam Leffler 	ssc->sample_rate = rate;
947c1565b61SSam Leffler 	return 0;
948c1565b61SSam Leffler }
949c1565b61SSam Leffler 
950c1565b61SSam Leffler static void
951c1565b61SSam Leffler ath_rate_sysctlattach(struct ath_softc *sc, struct sample_softc *ssc)
952fa20c234SSam Leffler {
953fa20c234SSam Leffler 	struct sysctl_ctx_list *ctx = device_get_sysctl_ctx(sc->sc_dev);
954fa20c234SSam Leffler 	struct sysctl_oid *tree = device_get_sysctl_tree(sc->sc_dev);
955fa20c234SSam Leffler 
956c1565b61SSam Leffler 	SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
957c1565b61SSam Leffler 	    "smoothing_rate", CTLTYPE_INT | CTLFLAG_RW, ssc, 0,
958c1565b61SSam Leffler 	    ath_rate_sysctl_smoothing_rate, "I",
959c1565b61SSam Leffler 	    "sample: smoothing rate for avg tx time (%%)");
960c1565b61SSam Leffler 	SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
961c1565b61SSam Leffler 	    "sample_rate", CTLTYPE_INT | CTLFLAG_RW, ssc, 0,
962c1565b61SSam Leffler 	    ath_rate_sysctl_sample_rate, "I",
963c1565b61SSam Leffler 	    "sample: percent air time devoted to sampling new rates (%%)");
964c1565b61SSam Leffler 	/* XXX max_successive_failures, stale_failure_timeout, min_switch */
965c1565b61SSam Leffler 	SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
966c1565b61SSam Leffler 	    "sample_stats", CTLTYPE_INT | CTLFLAG_RW, sc, 0,
967c1565b61SSam Leffler 	    ath_rate_sysctl_stats, "I", "sample: print statistics");
968fa20c234SSam Leffler }
969fa20c234SSam Leffler 
970fa20c234SSam Leffler struct ath_ratectrl *
971fa20c234SSam Leffler ath_rate_attach(struct ath_softc *sc)
972fa20c234SSam Leffler {
973c1565b61SSam Leffler 	struct sample_softc *ssc;
974fa20c234SSam Leffler 
975c1565b61SSam Leffler 	ssc = malloc(sizeof(struct sample_softc), M_DEVBUF, M_NOWAIT|M_ZERO);
976c1565b61SSam Leffler 	if (ssc == NULL)
977fa20c234SSam Leffler 		return NULL;
978c1565b61SSam Leffler 	ssc->arc.arc_space = sizeof(struct sample_node);
979c1565b61SSam Leffler 	ssc->smoothing_rate = 95;		/* ewma percentage ([0..99]) */
980c1565b61SSam Leffler 	ssc->smoothing_minpackets = 100 / (100 - ssc->smoothing_rate);
981c1565b61SSam Leffler 	ssc->sample_rate = 10;			/* %time to try diff tx rates */
982c1565b61SSam Leffler 	ssc->max_successive_failures = 3;	/* threshold for rate sampling*/
983c1565b61SSam Leffler 	ssc->stale_failure_timeout = 10 * hz;	/* 10 seconds */
984c1565b61SSam Leffler 	ssc->min_switch = hz;			/* 1 second */
985c1565b61SSam Leffler 	ath_rate_sysctlattach(sc, ssc);
986c1565b61SSam Leffler 	return &ssc->arc;
987fa20c234SSam Leffler }
988fa20c234SSam Leffler 
989fa20c234SSam Leffler void
990fa20c234SSam Leffler ath_rate_detach(struct ath_ratectrl *arc)
991fa20c234SSam Leffler {
992c1565b61SSam Leffler 	struct sample_softc *ssc = (struct sample_softc *) arc;
993fa20c234SSam Leffler 
994c1565b61SSam Leffler 	free(ssc, M_DEVBUF);
995fa20c234SSam Leffler }
996fa20c234SSam Leffler 
997fa20c234SSam Leffler /*
998fa20c234SSam Leffler  * Module glue.
999fa20c234SSam Leffler  */
1000fa20c234SSam Leffler static int
1001fa20c234SSam Leffler sample_modevent(module_t mod, int type, void *unused)
1002fa20c234SSam Leffler {
1003fa20c234SSam Leffler 	switch (type) {
1004fa20c234SSam Leffler 	case MOD_LOAD:
1005fa20c234SSam Leffler 		if (bootverbose)
1006c1565b61SSam Leffler 			printf("ath_rate: version 1.9 <SampleRate bit-rate selection algorithm>\n");
1007fa20c234SSam Leffler 		return 0;
1008fa20c234SSam Leffler 	case MOD_UNLOAD:
1009fa20c234SSam Leffler 		return 0;
1010fa20c234SSam Leffler 	}
1011fa20c234SSam Leffler 	return EINVAL;
1012fa20c234SSam Leffler }
1013fa20c234SSam Leffler 
1014fa20c234SSam Leffler static moduledata_t sample_mod = {
1015fa20c234SSam Leffler 	"ath_rate",
1016fa20c234SSam Leffler 	sample_modevent,
1017fa20c234SSam Leffler 	0
1018fa20c234SSam Leffler };
1019fa20c234SSam Leffler DECLARE_MODULE(ath_rate, sample_mod, SI_SUB_DRIVERS, SI_ORDER_FIRST);
1020fa20c234SSam Leffler MODULE_VERSION(ath_rate, 1);
1021fa20c234SSam Leffler MODULE_DEPEND(ath_rate, wlan, 1, 1, 1);
1022