xref: /freebsd/sys/dev/ath/ath_rate/sample/sample.c (revision e09c8c4cd48b3461e4a6ca62f2f153317c723b1d)
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/kernel.h>
51fa20c234SSam Leffler #include <sys/lock.h>
52fa20c234SSam Leffler #include <sys/mutex.h>
53fa20c234SSam Leffler #include <sys/errno.h>
54fa20c234SSam Leffler 
55fa20c234SSam Leffler #include <machine/bus.h>
56fa20c234SSam Leffler #include <machine/resource.h>
57fa20c234SSam Leffler #include <sys/bus.h>
58fa20c234SSam Leffler 
59fa20c234SSam Leffler #include <sys/socket.h>
60fa20c234SSam Leffler 
61fa20c234SSam Leffler #include <net/if.h>
62fa20c234SSam Leffler #include <net/if_media.h>
63fa20c234SSam Leffler #include <net/if_arp.h>
64c1565b61SSam Leffler #include <net/ethernet.h>		/* XXX for ether_sprintf */
65fa20c234SSam Leffler 
66fa20c234SSam Leffler #include <net80211/ieee80211_var.h>
67fa20c234SSam Leffler 
68fa20c234SSam Leffler #include <net/bpf.h>
69fa20c234SSam Leffler 
70fa20c234SSam Leffler #ifdef INET
71fa20c234SSam Leffler #include <netinet/in.h>
72fa20c234SSam Leffler #include <netinet/if_ether.h>
73fa20c234SSam Leffler #endif
74fa20c234SSam Leffler 
75fa20c234SSam Leffler #include <dev/ath/if_athvar.h>
76fa20c234SSam Leffler #include <dev/ath/ath_rate/sample/sample.h>
7733644623SSam Leffler #include <dev/ath/ath_hal/ah_desc.h>
78a6a308a4SAdrian Chadd #include <dev/ath/ath_rate/sample/tx_schedules.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 
146a6a308a4SAdrian Chadd static int
147a6a308a4SAdrian Chadd dot11rate(const HAL_RATE_TABLE *rt, int rix)
148a6a308a4SAdrian Chadd {
149a6a308a4SAdrian Chadd 	return rt->info[rix].phy == IEEE80211_T_HT ?
150a6a308a4SAdrian Chadd 	    rt->info[rix].dot11Rate : (rt->info[rix].dot11Rate & IEEE80211_RATE_VAL) / 2;
151a6a308a4SAdrian Chadd }
152a6a308a4SAdrian Chadd 
153fa20c234SSam Leffler /*
154c1565b61SSam Leffler  * Return the rix with the lowest average_tx_time,
155fa20c234SSam Leffler  * or -1 if all the average_tx_times are 0.
156fa20c234SSam Leffler  */
157c1565b61SSam Leffler static __inline int
158c1565b61SSam Leffler pick_best_rate(struct sample_node *sn, const HAL_RATE_TABLE *rt,
159c1565b61SSam Leffler     int size_bin, int require_acked_before)
160fa20c234SSam Leffler {
161c1565b61SSam Leffler         int best_rate_rix, best_rate_tt;
162c1565b61SSam Leffler 	uint32_t mask;
163c1565b61SSam Leffler 	int rix, tt;
164b91bf513SSam Leffler 
165c1565b61SSam Leffler         best_rate_rix = 0;
166c1565b61SSam Leffler         best_rate_tt = 0;
167c1565b61SSam Leffler 	for (mask = sn->ratemask, rix = 0; mask != 0; mask >>= 1, rix++) {
168c1565b61SSam Leffler 		if ((mask & 1) == 0)		/* not a supported rate */
169c1565b61SSam Leffler 			continue;
170c1565b61SSam Leffler 
171c1565b61SSam Leffler 		tt = sn->stats[size_bin][rix].average_tx_time;
172c1565b61SSam Leffler 		if (tt <= 0 ||
173c1565b61SSam Leffler 		    (require_acked_before &&
174c1565b61SSam Leffler 		     !sn->stats[size_bin][rix].packets_acked))
175b91bf513SSam Leffler 			continue;
176b91bf513SSam Leffler 
177b91bf513SSam Leffler 		/* don't use a bit-rate that has been failing */
178c1565b61SSam Leffler 		if (sn->stats[size_bin][rix].successive_failures > 3)
179b91bf513SSam Leffler 			continue;
180b91bf513SSam Leffler 
181c1565b61SSam Leffler 		if (best_rate_tt == 0 || tt < best_rate_tt) {
182fa20c234SSam Leffler 			best_rate_tt = tt;
183c1565b61SSam Leffler 			best_rate_rix = rix;
184fa20c234SSam Leffler 		}
185fa20c234SSam Leffler         }
186c1565b61SSam Leffler         return (best_rate_tt ? best_rate_rix : -1);
187fa20c234SSam Leffler }
188fa20c234SSam Leffler 
189fa20c234SSam Leffler /*
190c1565b61SSam Leffler  * Pick a good "random" bit-rate to sample other than the current one.
191fa20c234SSam Leffler  */
192b91bf513SSam Leffler static __inline int
193c1565b61SSam Leffler pick_sample_rate(struct sample_softc *ssc , struct sample_node *sn,
194c1565b61SSam Leffler     const HAL_RATE_TABLE *rt, int size_bin)
195fa20c234SSam Leffler {
196c1565b61SSam Leffler #define	DOT11RATE(ix)	(rt->info[ix].dot11Rate & IEEE80211_RATE_VAL)
197a6a308a4SAdrian Chadd #define	MCS(ix)		(rt->info[ix].dot11Rate | IEEE80211_RATE_MCS)
198c1565b61SSam Leffler 	int current_rix, rix;
199c1565b61SSam Leffler 	unsigned current_tt;
200c1565b61SSam Leffler 	uint32_t mask;
201fa20c234SSam Leffler 
202c1565b61SSam Leffler 	current_rix = sn->current_rix[size_bin];
203c1565b61SSam Leffler 	if (current_rix < 0) {
204fa20c234SSam Leffler 		/* no successes yet, send at the lowest bit-rate */
205fa20c234SSam Leffler 		return 0;
206fa20c234SSam Leffler 	}
207fa20c234SSam Leffler 
208c1565b61SSam Leffler 	current_tt = sn->stats[size_bin][current_rix].average_tx_time;
209fa20c234SSam Leffler 
210c1565b61SSam Leffler 	rix = sn->last_sample_rix[size_bin]+1;	/* next sample rate */
211c1565b61SSam Leffler 	mask = sn->ratemask &~ (1<<current_rix);/* don't sample current rate */
212c1565b61SSam Leffler 	while (mask != 0) {
213c1565b61SSam Leffler 		if ((mask & (1<<rix)) == 0) {	/* not a supported rate */
214c1565b61SSam Leffler 	nextrate:
215c1565b61SSam Leffler 			if (++rix >= rt->rateCount)
216c1565b61SSam Leffler 				rix = 0;
217b91bf513SSam Leffler 			continue;
218c1565b61SSam Leffler 		}
219b91bf513SSam Leffler 
220b91bf513SSam Leffler 		/* this bit-rate is always worse than the current one */
221c1565b61SSam Leffler 		if (sn->stats[size_bin][rix].perfect_tx_time > current_tt) {
222c1565b61SSam Leffler 			mask &= ~(1<<rix);
223c1565b61SSam Leffler 			goto nextrate;
224c1565b61SSam Leffler 		}
225b91bf513SSam Leffler 
226b91bf513SSam Leffler 		/* rarely sample bit-rates that fail a lot */
227c1565b61SSam Leffler 		if (sn->stats[size_bin][rix].successive_failures > ssc->max_successive_failures &&
228c1565b61SSam Leffler 		    ticks - sn->stats[size_bin][rix].last_tx < ssc->stale_failure_timeout) {
229c1565b61SSam Leffler 			mask &= ~(1<<rix);
230c1565b61SSam Leffler 			goto nextrate;
231fa20c234SSam Leffler 		}
232c1565b61SSam Leffler 
233c1565b61SSam Leffler 		/* don't sample more than 2 rates higher for rates > 11M */
234c1565b61SSam Leffler 		if (DOT11RATE(rix) > 2*11 && rix > current_rix + 2) {
235c1565b61SSam Leffler 			mask &= ~(1<<rix);
236c1565b61SSam Leffler 			goto nextrate;
237c1565b61SSam Leffler 		}
238c1565b61SSam Leffler 
239c1565b61SSam Leffler 		sn->last_sample_rix[size_bin] = rix;
240c1565b61SSam Leffler 		return rix;
241c1565b61SSam Leffler 	}
242c1565b61SSam Leffler 	return current_rix;
243c1565b61SSam Leffler #undef DOT11RATE
244a6a308a4SAdrian Chadd #undef	MCS
245fa20c234SSam Leffler }
246fa20c234SSam Leffler 
247fa20c234SSam Leffler void
248fa20c234SSam Leffler ath_rate_findrate(struct ath_softc *sc, struct ath_node *an,
249a4d8dd10SSam Leffler 		  int shortPreamble, size_t frameLen,
250c1565b61SSam Leffler 		  u_int8_t *rix0, int *try0, u_int8_t *txrate)
251fa20c234SSam Leffler {
252c1565b61SSam Leffler #define	DOT11RATE(ix)	(rt->info[ix].dot11Rate & IEEE80211_RATE_VAL)
253a6a308a4SAdrian Chadd #define	MCS(ix)		(rt->info[ix].dot11Rate | IEEE80211_RATE_MCS)
254c1565b61SSam Leffler #define	RATE(ix)	(DOT11RATE(ix) / 2)
255fa20c234SSam Leffler 	struct sample_node *sn = ATH_NODE_SAMPLE(an);
256fa20c234SSam Leffler 	struct sample_softc *ssc = ATH_SOFTC_SAMPLE(sc);
257b032f27cSSam Leffler 	struct ifnet *ifp = sc->sc_ifp;
258b032f27cSSam Leffler 	struct ieee80211com *ic = ifp->if_l2com;
259c1565b61SSam Leffler 	const HAL_RATE_TABLE *rt = sc->sc_currates;
260c1565b61SSam Leffler 	const int size_bin = size_to_bin(frameLen);
261c1565b61SSam Leffler 	int rix, mrr, best_rix, change_rates;
262b2763056SSam Leffler 	unsigned average_tx_time;
263fa20c234SSam Leffler 
264c1565b61SSam Leffler 	if (sn->static_rix != -1) {
265c1565b61SSam Leffler 		rix = sn->static_rix;
266c1565b61SSam Leffler 		*try0 = ATH_TXMAXTRY;
267c1565b61SSam Leffler 		goto done;
268c1565b61SSam Leffler 	}
269fa20c234SSam Leffler 
270c1565b61SSam Leffler 	mrr = sc->sc_mrretry && !(ic->ic_flags & IEEE80211_F_USEPROT);
271c1565b61SSam Leffler 
272c1565b61SSam Leffler 	best_rix = pick_best_rate(sn, rt, size_bin, !mrr);
273c1565b61SSam Leffler 	if (best_rix >= 0) {
274c1565b61SSam Leffler 		average_tx_time = sn->stats[size_bin][best_rix].average_tx_time;
275fa20c234SSam Leffler 	} else {
276b2763056SSam Leffler 		average_tx_time = 0;
277b2763056SSam Leffler 	}
278fa20c234SSam Leffler 	/*
279c1565b61SSam Leffler 	 * Limit the time measuring the performance of other tx
280c1565b61SSam Leffler 	 * rates to sample_rate% of the total transmission time.
281fa20c234SSam Leffler 	 */
282c1565b61SSam Leffler 	if (sn->sample_tt[size_bin] < average_tx_time * (sn->packets_since_sample[size_bin]*ssc->sample_rate/100)) {
283c1565b61SSam Leffler 		rix = pick_sample_rate(ssc, sn, rt, size_bin);
284c1565b61SSam Leffler 		IEEE80211_NOTE(an->an_node.ni_vap, IEEE80211_MSG_RATECTL,
285c1565b61SSam Leffler 		     &an->an_node, "size %u sample rate %d current rate %d",
286c1565b61SSam Leffler 		     bin_to_size(size_bin), RATE(rix),
287c1565b61SSam Leffler 		     RATE(sn->current_rix[size_bin]));
288c1565b61SSam Leffler 		if (rix != sn->current_rix[size_bin]) {
289c1565b61SSam Leffler 			sn->current_sample_rix[size_bin] = rix;
290b2763056SSam Leffler 		} else {
291c1565b61SSam Leffler 			sn->current_sample_rix[size_bin] = -1;
292b2763056SSam Leffler 		}
293b2763056SSam Leffler 		sn->packets_since_sample[size_bin] = 0;
294b2763056SSam Leffler 	} else {
295b91bf513SSam Leffler 		change_rates = 0;
296c1565b61SSam Leffler 		if (!sn->packets_sent[size_bin] || best_rix == -1) {
297b91bf513SSam Leffler 			/* no packet has been sent successfully yet */
298c1565b61SSam Leffler 			for (rix = rt->rateCount-1; rix > 0; rix--) {
299c1565b61SSam Leffler 				if ((sn->ratemask & (1<<rix)) == 0)
300c1565b61SSam Leffler 					continue;
301b2763056SSam Leffler 				/*
302c1565b61SSam Leffler 				 * Pick the highest rate <= 36 Mbps
303b91bf513SSam Leffler 				 * that hasn't failed.
304b2763056SSam Leffler 				 */
305c1565b61SSam Leffler 				if (DOT11RATE(rix) <= 72 &&
306c1565b61SSam Leffler 				    sn->stats[size_bin][rix].successive_failures == 0) {
307b91bf513SSam Leffler 					break;
308b91bf513SSam Leffler 				}
309b91bf513SSam Leffler 			}
310b91bf513SSam Leffler 			change_rates = 1;
311c1565b61SSam Leffler 			best_rix = rix;
312b91bf513SSam Leffler 		} else if (sn->packets_sent[size_bin] < 20) {
313b91bf513SSam Leffler 			/* let the bit-rate switch quickly during the first few packets */
314b91bf513SSam Leffler 			change_rates = 1;
315c1565b61SSam Leffler 		} else if (ticks - ssc->min_switch > sn->ticks_since_switch[size_bin]) {
316c1565b61SSam Leffler 			/* min_switch seconds have gone by */
317b91bf513SSam Leffler 			change_rates = 1;
318c1565b61SSam Leffler 		} else if (2*average_tx_time < sn->stats[size_bin][sn->current_rix[size_bin]].average_tx_time) {
319b91bf513SSam Leffler 			/* the current bit-rate is twice as slow as the best one */
320b91bf513SSam Leffler 			change_rates = 1;
321b91bf513SSam Leffler 		}
322b91bf513SSam Leffler 
323b91bf513SSam Leffler 		sn->packets_since_sample[size_bin]++;
324b91bf513SSam Leffler 
325b91bf513SSam Leffler 		if (change_rates) {
326c1565b61SSam Leffler 			if (best_rix != sn->current_rix[size_bin]) {
327b032f27cSSam Leffler 				IEEE80211_NOTE(an->an_node.ni_vap,
328b032f27cSSam Leffler 				    IEEE80211_MSG_RATECTL,
329b032f27cSSam Leffler 				    &an->an_node,
330b032f27cSSam Leffler "%s: size %d switch rate %d (%d/%d) -> %d (%d/%d) after %d packets mrr %d",
331b2763056SSam Leffler 				    __func__,
332c1565b61SSam Leffler 				    bin_to_size(size_bin),
333c1565b61SSam Leffler 				    RATE(sn->current_rix[size_bin]),
334c1565b61SSam Leffler 				    sn->stats[size_bin][sn->current_rix[size_bin]].average_tx_time,
335c1565b61SSam Leffler 				    sn->stats[size_bin][sn->current_rix[size_bin]].perfect_tx_time,
336c1565b61SSam Leffler 				    RATE(best_rix),
337c1565b61SSam Leffler 				    sn->stats[size_bin][best_rix].average_tx_time,
338c1565b61SSam Leffler 				    sn->stats[size_bin][best_rix].perfect_tx_time,
339b2763056SSam Leffler 				    sn->packets_since_switch[size_bin],
340b2763056SSam Leffler 				    mrr);
341b2763056SSam Leffler 			}
342b2763056SSam Leffler 			sn->packets_since_switch[size_bin] = 0;
343c1565b61SSam Leffler 			sn->current_rix[size_bin] = best_rix;
344b91bf513SSam Leffler 			sn->ticks_since_switch[size_bin] = ticks;
345b032f27cSSam Leffler 			/*
346b032f27cSSam Leffler 			 * Set the visible txrate for this node.
347b032f27cSSam Leffler 			 */
348a6a308a4SAdrian Chadd 			an->an_node.ni_txrate = (rt->info[best_rix].phy == IEEE80211_T_HT) ?  MCS(best_rix) : DOT11RATE(best_rix);
349b2763056SSam Leffler 		}
350c1565b61SSam Leffler 		rix = sn->current_rix[size_bin];
351b2763056SSam Leffler 		sn->packets_since_switch[size_bin]++;
352b91bf513SSam Leffler 	}
353c1565b61SSam Leffler 	*try0 = mrr ? sn->sched[rix].t0 : ATH_TXMAXTRY;
354c1565b61SSam Leffler done:
355c1565b61SSam Leffler 	KASSERT(rix >= 0 && rix < rt->rateCount, ("rix is %d", rix));
356fa20c234SSam Leffler 
357c1565b61SSam Leffler 	*rix0 = rix;
358c1565b61SSam Leffler 	*txrate = rt->info[rix].rateCode
359c1565b61SSam Leffler 		| (shortPreamble ? rt->info[rix].shortPreamble : 0);
360fa20c234SSam Leffler 	sn->packets_sent[size_bin]++;
361c1565b61SSam Leffler #undef DOT11RATE
362a6a308a4SAdrian Chadd #undef MCS
363c1565b61SSam Leffler #undef RATE
364fa20c234SSam Leffler }
365fa20c234SSam Leffler 
366710c3778SAdrian Chadd /*
367710c3778SAdrian Chadd  * Get the TX rates. Don't fiddle with short preamble flags for them;
368710c3778SAdrian Chadd  * the caller can do that.
369710c3778SAdrian Chadd  */
370710c3778SAdrian Chadd void
371710c3778SAdrian Chadd ath_rate_getxtxrates(struct ath_softc *sc, struct ath_node *an,
372710c3778SAdrian Chadd     uint8_t rix0, uint8_t *rix, uint8_t *try)
373710c3778SAdrian Chadd {
374710c3778SAdrian Chadd 	struct sample_node *sn = ATH_NODE_SAMPLE(an);
375710c3778SAdrian Chadd 	const struct txschedule *sched = &sn->sched[rix0];
376710c3778SAdrian Chadd 
377710c3778SAdrian Chadd 	KASSERT(rix0 == sched->r0, ("rix0 (%x) != sched->r0 (%x)!\n", rix0, sched->r0));
378710c3778SAdrian Chadd 
379710c3778SAdrian Chadd /*	rix[0] = sched->r0; */
380710c3778SAdrian Chadd 	rix[1] = sched->r1;
381710c3778SAdrian Chadd 	rix[2] = sched->r2;
382710c3778SAdrian Chadd 	rix[3] = sched->r3;
383710c3778SAdrian Chadd 
384710c3778SAdrian Chadd 	try[0] = sched->t0;
385710c3778SAdrian Chadd 	try[1] = sched->t1;
386710c3778SAdrian Chadd 	try[2] = sched->t2;
387710c3778SAdrian Chadd 	try[3] = sched->t3;
388710c3778SAdrian Chadd }
389710c3778SAdrian Chadd 
390fa20c234SSam Leffler void
391fa20c234SSam Leffler ath_rate_setupxtxdesc(struct ath_softc *sc, struct ath_node *an,
392a4d8dd10SSam Leffler 		      struct ath_desc *ds, int shortPreamble, u_int8_t rix)
393fa20c234SSam Leffler {
394fa20c234SSam Leffler 	struct sample_node *sn = ATH_NODE_SAMPLE(an);
395c1565b61SSam Leffler 	const struct txschedule *sched = &sn->sched[rix];
396c1565b61SSam Leffler 	const HAL_RATE_TABLE *rt = sc->sc_currates;
397c1565b61SSam Leffler 	uint8_t rix1, s1code, rix2, s2code, rix3, s3code;
398fa20c234SSam Leffler 
399c1565b61SSam Leffler 	/* XXX precalculate short preamble tables */
400c1565b61SSam Leffler 	rix1 = sched->r1;
401c1565b61SSam Leffler 	s1code = rt->info[rix1].rateCode
402c1565b61SSam Leffler 	       | (shortPreamble ? rt->info[rix1].shortPreamble : 0);
403c1565b61SSam Leffler 	rix2 = sched->r2;
404c1565b61SSam Leffler 	s2code = rt->info[rix2].rateCode
405c1565b61SSam Leffler 	       | (shortPreamble ? rt->info[rix2].shortPreamble : 0);
406c1565b61SSam Leffler 	rix3 = sched->r3;
407c1565b61SSam Leffler 	s3code = rt->info[rix3].rateCode
408c1565b61SSam Leffler 	       | (shortPreamble ? rt->info[rix3].shortPreamble : 0);
409c1565b61SSam Leffler 	ath_hal_setupxtxdesc(sc->sc_ah, ds,
410c1565b61SSam Leffler 	    s1code, sched->t1,		/* series 1 */
411c1565b61SSam Leffler 	    s2code, sched->t2,		/* series 2 */
412c1565b61SSam Leffler 	    s3code, sched->t3);		/* series 3 */
413fa20c234SSam Leffler }
414fa20c234SSam Leffler 
415b2763056SSam Leffler static void
416b2763056SSam Leffler update_stats(struct ath_softc *sc, struct ath_node *an,
417b2763056SSam Leffler 		  int frame_size,
418c1565b61SSam Leffler 		  int rix0, int tries0,
419c1565b61SSam Leffler 		  int rix1, int tries1,
420c1565b61SSam Leffler 		  int rix2, int tries2,
421c1565b61SSam Leffler 		  int rix3, int tries3,
422b2763056SSam Leffler 		  int short_tries, int tries, int status)
423fa20c234SSam Leffler {
424fa20c234SSam Leffler 	struct sample_node *sn = ATH_NODE_SAMPLE(an);
425fa20c234SSam Leffler 	struct sample_softc *ssc = ATH_SOFTC_SAMPLE(sc);
426c1565b61SSam Leffler 	const int size_bin = size_to_bin(frame_size);
427c1565b61SSam Leffler 	const int size = bin_to_size(size_bin);
428c1565b61SSam Leffler 	int tt, tries_so_far;
429*e09c8c4cSAdrian Chadd 	int is_ht40 = (an->an_node.ni_htcap & IEEE80211_HTCAP_CHWIDTH40);
430fa20c234SSam Leffler 
431c1565b61SSam Leffler 	if (!IS_RATE_DEFINED(sn, rix0))
43265f9edeeSSam Leffler 		return;
433c1565b61SSam Leffler 	tt = calc_usecs_unicast_packet(sc, size, rix0, short_tries,
434*e09c8c4cSAdrian Chadd 		MIN(tries0, tries) - 1, is_ht40);
435c1565b61SSam Leffler 	tries_so_far = tries0;
436c1565b61SSam Leffler 
437c1565b61SSam Leffler 	if (tries1 && tries_so_far < tries) {
438c1565b61SSam Leffler 		if (!IS_RATE_DEFINED(sn, rix1))
43965f9edeeSSam Leffler 			return;
440c1565b61SSam Leffler 		tt += calc_usecs_unicast_packet(sc, size, rix1, short_tries,
441*e09c8c4cSAdrian Chadd 			MIN(tries1 + tries_so_far, tries) - tries_so_far - 1, is_ht40);
442b2763056SSam Leffler 		tries_so_far += tries1;
443b2763056SSam Leffler 	}
444b2763056SSam Leffler 
445c1565b61SSam Leffler 	if (tries2 && tries_so_far < tries) {
446c1565b61SSam Leffler 		if (!IS_RATE_DEFINED(sn, rix2))
44765f9edeeSSam Leffler 			return;
448c1565b61SSam Leffler 		tt += calc_usecs_unicast_packet(sc, size, rix2, short_tries,
449*e09c8c4cSAdrian Chadd 			MIN(tries2 + tries_so_far, tries) - tries_so_far - 1, is_ht40);
450c1565b61SSam Leffler 		tries_so_far += tries2;
451c1565b61SSam Leffler 	}
452c1565b61SSam Leffler 
453c1565b61SSam Leffler 	if (tries3 && tries_so_far < tries) {
454c1565b61SSam Leffler 		if (!IS_RATE_DEFINED(sn, rix3))
455c1565b61SSam Leffler 			return;
456c1565b61SSam Leffler 		tt += calc_usecs_unicast_packet(sc, size, rix3, short_tries,
457*e09c8c4cSAdrian Chadd 			MIN(tries3 + tries_so_far, tries) - tries_so_far - 1, is_ht40);
458b2763056SSam Leffler 	}
459c1565b61SSam Leffler 
460c1565b61SSam Leffler 	if (sn->stats[size_bin][rix0].total_packets < ssc->smoothing_minpackets) {
461fa20c234SSam Leffler 		/* just average the first few packets */
462c1565b61SSam Leffler 		int avg_tx = sn->stats[size_bin][rix0].average_tx_time;
463c1565b61SSam Leffler 		int packets = sn->stats[size_bin][rix0].total_packets;
464c1565b61SSam Leffler 		sn->stats[size_bin][rix0].average_tx_time = (tt+(avg_tx*packets))/(packets+1);
465fa20c234SSam Leffler 	} else {
466fa20c234SSam Leffler 		/* use a ewma */
467c1565b61SSam Leffler 		sn->stats[size_bin][rix0].average_tx_time =
468c1565b61SSam Leffler 			((sn->stats[size_bin][rix0].average_tx_time * ssc->smoothing_rate) +
469c1565b61SSam Leffler 			 (tt * (100 - ssc->smoothing_rate))) / 100;
470fa20c234SSam Leffler 	}
471fa20c234SSam Leffler 
472c1565b61SSam Leffler 	if (status != 0) {
473fa20c234SSam Leffler 		int y;
474c1565b61SSam Leffler 		sn->stats[size_bin][rix0].successive_failures++;
475b91bf513SSam Leffler 		for (y = size_bin+1; y < NUM_PACKET_SIZE_BINS; y++) {
476c1565b61SSam Leffler 			/*
477c1565b61SSam Leffler 			 * Also say larger packets failed since we
478c1565b61SSam Leffler 			 * assume if a small packet fails at a
479b91bf513SSam Leffler 			 * bit-rate then a larger one will also.
480b91bf513SSam Leffler 			 */
481c1565b61SSam Leffler 			sn->stats[y][rix0].successive_failures++;
482c1565b61SSam Leffler 			sn->stats[y][rix0].last_tx = ticks;
483c1565b61SSam Leffler 			sn->stats[y][rix0].tries += tries;
484c1565b61SSam Leffler 			sn->stats[y][rix0].total_packets++;
485fa20c234SSam Leffler 		}
486fa20c234SSam Leffler 	} else {
487c1565b61SSam Leffler 		sn->stats[size_bin][rix0].packets_acked++;
488c1565b61SSam Leffler 		sn->stats[size_bin][rix0].successive_failures = 0;
489fa20c234SSam Leffler 	}
490c1565b61SSam Leffler 	sn->stats[size_bin][rix0].tries += tries;
491c1565b61SSam Leffler 	sn->stats[size_bin][rix0].last_tx = ticks;
492c1565b61SSam Leffler 	sn->stats[size_bin][rix0].total_packets++;
493b2763056SSam Leffler 
494c1565b61SSam Leffler 	if (rix0 == sn->current_sample_rix[size_bin]) {
495b032f27cSSam Leffler 		IEEE80211_NOTE(an->an_node.ni_vap, IEEE80211_MSG_RATECTL,
496b032f27cSSam Leffler 		   &an->an_node,
497b032f27cSSam Leffler "%s: size %d %s sample rate %d tries (%d/%d) tt %d avg_tt (%d/%d)",
498b032f27cSSam Leffler 		    __func__,
49965f9edeeSSam Leffler 		    size,
50065f9edeeSSam Leffler 		    status ? "FAIL" : "OK",
501c1565b61SSam Leffler 		    rix0, short_tries, tries, tt,
502c1565b61SSam Leffler 		    sn->stats[size_bin][rix0].average_tx_time,
503c1565b61SSam Leffler 		    sn->stats[size_bin][rix0].perfect_tx_time);
504b2763056SSam Leffler 		sn->sample_tt[size_bin] = tt;
505c1565b61SSam Leffler 		sn->current_sample_rix[size_bin] = -1;
506b2763056SSam Leffler 	}
507b2763056SSam Leffler }
508b2763056SSam Leffler 
509ec9ee5e7SSam Leffler static void
510ec9ee5e7SSam Leffler badrate(struct ifnet *ifp, int series, int hwrate, int tries, int status)
511ec9ee5e7SSam Leffler {
512ec9ee5e7SSam Leffler 	if_printf(ifp, "bad series%d hwrate 0x%x, tries %u ts_status 0x%x\n",
513ec9ee5e7SSam Leffler 	    series, hwrate, tries, status);
514ec9ee5e7SSam Leffler }
515ec9ee5e7SSam Leffler 
516b2763056SSam Leffler void
51743e9cf7cSSam Leffler ath_rate_tx_complete(struct ath_softc *sc, struct ath_node *an,
51865f9edeeSSam Leffler 	const struct ath_buf *bf)
519b2763056SSam Leffler {
520b032f27cSSam Leffler 	struct ifnet *ifp = sc->sc_ifp;
521b032f27cSSam Leffler 	struct ieee80211com *ic = ifp->if_l2com;
522b2763056SSam Leffler 	struct sample_node *sn = ATH_NODE_SAMPLE(an);
52365f9edeeSSam Leffler 	const struct ath_tx_status *ts = &bf->bf_status.ds_txstat;
52465f9edeeSSam Leffler 	const struct ath_desc *ds0 = &bf->bf_desc[0];
525c1565b61SSam Leffler 	int final_rix, short_tries, long_tries, frame_size;
52646d4d74cSSam Leffler 	const HAL_RATE_TABLE *rt = sc->sc_currates;
5271bb9a085SSam Leffler 	int mrr;
528b2763056SSam Leffler 
529f6cbf16aSSam Leffler 	final_rix = rt->rateCodeToIndex[ts->ts_rate];
53065f9edeeSSam Leffler 	short_tries = ts->ts_shortretry;
53165f9edeeSSam Leffler 	long_tries = ts->ts_longretry + 1;
53243e9cf7cSSam Leffler 	frame_size = ds0->ds_ctl0 & 0x0fff; /* low-order 12 bits of ds_ctl0 */
53343e9cf7cSSam Leffler 	if (frame_size == 0)		    /* NB: should not happen */
534b2763056SSam Leffler 		frame_size = 1500;
535b2763056SSam Leffler 
536c1565b61SSam Leffler 	if (sn->ratemask == 0) {
537b032f27cSSam Leffler 		IEEE80211_NOTE(an->an_node.ni_vap, IEEE80211_MSG_RATECTL,
538b032f27cSSam Leffler 		    &an->an_node,
539b032f27cSSam Leffler 		    "%s: size %d %s rate/try %d/%d no rates yet",
540b032f27cSSam Leffler 		    __func__,
54143e9cf7cSSam Leffler 		    bin_to_size(size_to_bin(frame_size)),
54265f9edeeSSam Leffler 		    ts->ts_status ? "FAIL" : "OK",
54343e9cf7cSSam Leffler 		    short_tries, long_tries);
544b2763056SSam Leffler 		return;
545b2763056SSam Leffler 	}
54665f9edeeSSam Leffler 	mrr = sc->sc_mrretry && !(ic->ic_flags & IEEE80211_F_USEPROT);
547f6cbf16aSSam Leffler 	if (!mrr || ts->ts_finaltsi == 0) {
548c1565b61SSam Leffler 		if (!IS_RATE_DEFINED(sn, final_rix)) {
549ec9ee5e7SSam Leffler 			badrate(ifp, 0, ts->ts_rate, long_tries, ts->ts_status);
550ec9ee5e7SSam Leffler 			return;
551ec9ee5e7SSam Leffler 		}
55265f9edeeSSam Leffler 		/*
55365f9edeeSSam Leffler 		 * Only one rate was used; optimize work.
55465f9edeeSSam Leffler 		 */
555b032f27cSSam Leffler 		IEEE80211_NOTE(an->an_node.ni_vap, IEEE80211_MSG_RATECTL,
556b032f27cSSam Leffler 		     &an->an_node, "%s: size %d %s rate/try %d/%d/%d",
557b032f27cSSam Leffler 		     __func__,
55843e9cf7cSSam Leffler 		     bin_to_size(size_to_bin(frame_size)),
55965f9edeeSSam Leffler 		     ts->ts_status ? "FAIL" : "OK",
560a6a308a4SAdrian Chadd 		     dot11rate(rt, final_rix), short_tries, long_tries);
561b2763056SSam Leffler 		update_stats(sc, an, frame_size,
562c1565b61SSam Leffler 			     final_rix, long_tries,
563b2763056SSam Leffler 			     0, 0,
564b2763056SSam Leffler 			     0, 0,
565b2763056SSam Leffler 			     0, 0,
56665f9edeeSSam Leffler 			     short_tries, long_tries, ts->ts_status);
567b2763056SSam Leffler 	} else {
568bd97c52aSAdrian Chadd 		int hwrates[4], tries[4], rix[4];
56965f9edeeSSam Leffler 		int finalTSIdx = ts->ts_finaltsi;
570bd97c52aSAdrian Chadd 		int i;
571b2763056SSam Leffler 
572b2763056SSam Leffler 		/*
573b2763056SSam Leffler 		 * Process intermediate rates that failed.
574b2763056SSam Leffler 		 */
575bd97c52aSAdrian Chadd 		ath_hal_gettxcompletionrates(sc->sc_ah, ds0, hwrates, tries);
576a6a308a4SAdrian Chadd 
577a6a308a4SAdrian Chadd 		for (i = 0; i < 4; i++) {
578bd97c52aSAdrian Chadd 			rix[i] = rt->rateCodeToIndex[hwrates[i]];
579a6a308a4SAdrian Chadd 		}
580ec9ee5e7SSam Leffler 
581b032f27cSSam Leffler 		IEEE80211_NOTE(an->an_node.ni_vap, IEEE80211_MSG_RATECTL,
582b032f27cSSam Leffler 		    &an->an_node,
583b032f27cSSam Leffler "%s: size %d finaltsidx %d tries %d %s rate/try [%d/%d %d/%d %d/%d %d/%d]",
584b032f27cSSam Leffler 		     __func__,
585b2763056SSam Leffler 		     bin_to_size(size_to_bin(frame_size)),
586b2763056SSam Leffler 		     finalTSIdx,
587b2763056SSam Leffler 		     long_tries,
58865f9edeeSSam Leffler 		     ts->ts_status ? "FAIL" : "OK",
589a6a308a4SAdrian Chadd 		     dot11rate(rt, rix[0]), tries[0],
590a6a308a4SAdrian Chadd 		     dot11rate(rt, rix[1]), tries[1],
591a6a308a4SAdrian Chadd 		     dot11rate(rt, rix[2]), tries[2],
592a6a308a4SAdrian Chadd 		     dot11rate(rt, rix[3]), tries[3]);
593c1565b61SSam Leffler 
594a6a308a4SAdrian Chadd 		for (i = 0; i < 4; i++) {
595a6a308a4SAdrian Chadd 			if (tries[i] && !IS_RATE_DEFINED(sn, rix[i]))
596a6a308a4SAdrian Chadd 				badrate(ifp, 0, hwrates[i], tries[i], ts->ts_status);
597a6a308a4SAdrian Chadd 		}
598b2763056SSam Leffler 
59965f9edeeSSam Leffler 		/*
60065f9edeeSSam Leffler 		 * NB: series > 0 are not penalized for failure
60165f9edeeSSam Leffler 		 * based on the try counts under the assumption
60265f9edeeSSam Leffler 		 * that losses are often bursty and since we
60365f9edeeSSam Leffler 		 * sample higher rates 1 try at a time doing so
60465f9edeeSSam Leffler 		 * may unfairly penalize them.
60565f9edeeSSam Leffler 		 */
606bd97c52aSAdrian Chadd 		if (tries[0]) {
607b2763056SSam Leffler 			update_stats(sc, an, frame_size,
608bd97c52aSAdrian Chadd 				     rix[0], tries[0],
609bd97c52aSAdrian Chadd 				     rix[1], tries[1],
610bd97c52aSAdrian Chadd 				     rix[2], tries[2],
611bd97c52aSAdrian Chadd 				     rix[3], tries[3],
61265f9edeeSSam Leffler 				     short_tries, long_tries,
613bd97c52aSAdrian Chadd 				     long_tries > tries[0]);
614bd97c52aSAdrian Chadd 			long_tries -= tries[0];
615b2763056SSam Leffler 		}
616b2763056SSam Leffler 
617bd97c52aSAdrian Chadd 		if (tries[1] && finalTSIdx > 0) {
618b2763056SSam Leffler 			update_stats(sc, an, frame_size,
619bd97c52aSAdrian Chadd 				     rix[1], tries[1],
620bd97c52aSAdrian Chadd 				     rix[2], tries[2],
621bd97c52aSAdrian Chadd 				     rix[3], tries[3],
622b2763056SSam Leffler 				     0, 0,
62365f9edeeSSam Leffler 				     short_tries, long_tries,
62465f9edeeSSam Leffler 				     ts->ts_status);
625bd97c52aSAdrian Chadd 			long_tries -= tries[1];
626b2763056SSam Leffler 		}
627b2763056SSam Leffler 
628bd97c52aSAdrian Chadd 		if (tries[2] && finalTSIdx > 1) {
629b2763056SSam Leffler 			update_stats(sc, an, frame_size,
630bd97c52aSAdrian Chadd 				     rix[2], tries[2],
631bd97c52aSAdrian Chadd 				     rix[3], tries[3],
632b2763056SSam Leffler 				     0, 0,
633b2763056SSam Leffler 				     0, 0,
63465f9edeeSSam Leffler 				     short_tries, long_tries,
63565f9edeeSSam Leffler 				     ts->ts_status);
636bd97c52aSAdrian Chadd 			long_tries -= tries[2];
637b2763056SSam Leffler 		}
638b2763056SSam Leffler 
639bd97c52aSAdrian Chadd 		if (tries[3] && finalTSIdx > 2) {
640b2763056SSam Leffler 			update_stats(sc, an, frame_size,
641bd97c52aSAdrian Chadd 				     rix[3], tries[3],
642b2763056SSam Leffler 				     0, 0,
643b2763056SSam Leffler 				     0, 0,
644b2763056SSam Leffler 				     0, 0,
64565f9edeeSSam Leffler 				     short_tries, long_tries,
64665f9edeeSSam Leffler 				     ts->ts_status);
647b2763056SSam Leffler 		}
648b2763056SSam Leffler 	}
649fa20c234SSam Leffler }
650fa20c234SSam Leffler 
651fa20c234SSam Leffler void
652fa20c234SSam Leffler ath_rate_newassoc(struct ath_softc *sc, struct ath_node *an, int isnew)
653fa20c234SSam Leffler {
654fa20c234SSam Leffler 	if (isnew)
655b2763056SSam Leffler 		ath_rate_ctl_reset(sc, &an->an_node);
656fa20c234SSam Leffler }
657fa20c234SSam Leffler 
658c1565b61SSam Leffler static const struct txschedule *mrr_schedules[IEEE80211_MODE_MAX+2] = {
659c1565b61SSam Leffler 	NULL,		/* IEEE80211_MODE_AUTO */
660c1565b61SSam Leffler 	series_11a,	/* IEEE80211_MODE_11A */
661c1565b61SSam Leffler 	series_11g,	/* IEEE80211_MODE_11B */
662c1565b61SSam Leffler 	series_11g,	/* IEEE80211_MODE_11G */
663c1565b61SSam Leffler 	NULL,		/* IEEE80211_MODE_FH */
664c1565b61SSam Leffler 	series_11a,	/* IEEE80211_MODE_TURBO_A */
665c1565b61SSam Leffler 	series_11g,	/* IEEE80211_MODE_TURBO_G */
666c1565b61SSam Leffler 	series_11a,	/* IEEE80211_MODE_STURBO_A */
667a6a308a4SAdrian Chadd 	series_11na,	/* IEEE80211_MODE_11NA */
668a6a308a4SAdrian Chadd 	series_11ng,	/* IEEE80211_MODE_11NG */
669c1565b61SSam Leffler 	series_half,	/* IEEE80211_MODE_HALF */
670c1565b61SSam Leffler 	series_quarter,	/* IEEE80211_MODE_QUARTER */
671c1565b61SSam Leffler };
672c1565b61SSam Leffler 
673fa20c234SSam Leffler /*
674fa20c234SSam Leffler  * Initialize the tables for a node.
675fa20c234SSam Leffler  */
676fa20c234SSam Leffler static void
677b2763056SSam Leffler ath_rate_ctl_reset(struct ath_softc *sc, struct ieee80211_node *ni)
678fa20c234SSam Leffler {
679fa20c234SSam Leffler #define	RATE(_ix)	(ni->ni_rates.rs_rates[(_ix)] & IEEE80211_RATE_VAL)
680c1565b61SSam Leffler #define	DOT11RATE(_ix)	(rt->info[(_ix)].dot11Rate & IEEE80211_RATE_VAL)
681a6a308a4SAdrian Chadd #define	MCS(_ix)	(ni->ni_htrates.rs_rates[_ix] | IEEE80211_RATE_MCS)
682a6a308a4SAdrian Chadd 
683fa20c234SSam Leffler 	struct ath_node *an = ATH_NODE(ni);
684c62362cbSSam Leffler 	const struct ieee80211_txparam *tp = ni->ni_txparms;
685fa20c234SSam Leffler 	struct sample_node *sn = ATH_NODE_SAMPLE(an);
686fa20c234SSam Leffler 	const HAL_RATE_TABLE *rt = sc->sc_currates;
687c1565b61SSam Leffler 	int x, y, srate, rix;
688fa20c234SSam Leffler 
689fa20c234SSam Leffler 	KASSERT(rt != NULL, ("no rate table, mode %u", sc->sc_curmode));
690c1565b61SSam Leffler 
691c1565b61SSam Leffler 	KASSERT(sc->sc_curmode < IEEE80211_MODE_MAX+2,
692c1565b61SSam Leffler 	    ("curmode %u", sc->sc_curmode));
693c1565b61SSam Leffler 	sn->sched = mrr_schedules[sc->sc_curmode];
694c1565b61SSam Leffler 	KASSERT(sn->sched != NULL,
695c1565b61SSam Leffler 	    ("no mrr schedule for mode %u", sc->sc_curmode));
696c1565b61SSam Leffler 
697c1565b61SSam Leffler         sn->static_rix = -1;
698b032f27cSSam Leffler 	if (tp != NULL && tp->ucastrate != IEEE80211_FIXED_RATE_NONE) {
699fa20c234SSam Leffler 		/*
7005f82a460SSam Leffler 		 * A fixed rate is to be used; ucastrate is the IEEE code
7015f82a460SSam Leffler 		 * for this rate (sans basic bit).  Check this against the
7025f82a460SSam Leffler 		 * negotiated rate set for the node.  Note the fixed rate
7035f82a460SSam Leffler 		 * may not be available for various reasons so we only
7045f82a460SSam Leffler 		 * setup the static rate index if the lookup is successful.
705fa20c234SSam Leffler 		 */
706a6a308a4SAdrian Chadd 
707a6a308a4SAdrian Chadd 		/* XXX todo: check MCS rates */
708a6a308a4SAdrian Chadd 
709a6a308a4SAdrian Chadd 		/* Check legacy rates */
7105f82a460SSam Leffler 		for (srate = ni->ni_rates.rs_nrates - 1; srate >= 0; srate--)
7115f82a460SSam Leffler 			if (RATE(srate) == tp->ucastrate) {
7125f82a460SSam Leffler 				sn->static_rix = sc->sc_rixmap[tp->ucastrate];
7135f82a460SSam Leffler 				break;
7145f82a460SSam Leffler 			}
7155e86169aSSam Leffler #ifdef IEEE80211_DEBUG
7165e86169aSSam Leffler 			if (sn->static_rix == -1) {
7175e86169aSSam Leffler 				IEEE80211_NOTE(ni->ni_vap,
7185e86169aSSam Leffler 				    IEEE80211_MSG_RATECTL, ni,
7195e86169aSSam Leffler 				    "%s: ucastrate %u not found, nrates %u",
7205e86169aSSam Leffler 				    __func__, tp->ucastrate,
7215e86169aSSam Leffler 				    ni->ni_rates.rs_nrates);
7225e86169aSSam Leffler 			}
7235e86169aSSam Leffler #endif
724fa20c234SSam Leffler 	}
725b2763056SSam Leffler 
726c1565b61SSam Leffler 	/*
727c1565b61SSam Leffler 	 * Construct a bitmask of usable rates.  This has all
728c1565b61SSam Leffler 	 * negotiated rates minus those marked by the hal as
729c1565b61SSam Leffler 	 * to be ignored for doing rate control.
730c1565b61SSam Leffler 	 */
731c1565b61SSam Leffler 	sn->ratemask = 0;
732a6a308a4SAdrian Chadd 	/* MCS rates */
733a6a308a4SAdrian Chadd 	if (ni->ni_flags & IEEE80211_NODE_HT) {
734a6a308a4SAdrian Chadd 		for (x = 0; x < ni->ni_htrates.rs_nrates; x++) {
735a6a308a4SAdrian Chadd 			rix = sc->sc_rixmap[MCS(x)];
736a6a308a4SAdrian Chadd 			if (rix == 0xff)
737a6a308a4SAdrian Chadd 				continue;
738a6a308a4SAdrian Chadd 			/* skip rates marked broken by hal */
739a6a308a4SAdrian Chadd 			if (!rt->info[rix].valid)
740a6a308a4SAdrian Chadd 				continue;
741a6a308a4SAdrian Chadd 			KASSERT(rix < SAMPLE_MAXRATES,
742a6a308a4SAdrian Chadd 			    ("mcs %u has rix %d", MCS(x), rix));
743a6a308a4SAdrian Chadd 			sn->ratemask |= 1<<rix;
744a6a308a4SAdrian Chadd 		}
745a6a308a4SAdrian Chadd 	}
746a6a308a4SAdrian Chadd 
747a6a308a4SAdrian Chadd 	/* Legacy rates */
748fa20c234SSam Leffler 	for (x = 0; x < ni->ni_rates.rs_nrates; x++) {
749c1565b61SSam Leffler 		rix = sc->sc_rixmap[RATE(x)];
750c1565b61SSam Leffler 		if (rix == 0xff)
7510ae09ec5SSam Leffler 			continue;
752c1565b61SSam Leffler 		/* skip rates marked broken by hal */
753c1565b61SSam Leffler 		if (!rt->info[rix].valid)
754c1565b61SSam Leffler 			continue;
755c1565b61SSam Leffler 		KASSERT(rix < SAMPLE_MAXRATES,
756c1565b61SSam Leffler 		    ("rate %u has rix %d", RATE(x), rix));
757c1565b61SSam Leffler 		sn->ratemask |= 1<<rix;
758b2763056SSam Leffler 	}
759b032f27cSSam Leffler #ifdef IEEE80211_DEBUG
760b032f27cSSam Leffler 	if (ieee80211_msg(ni->ni_vap, IEEE80211_MSG_RATECTL)) {
761c1565b61SSam Leffler 		uint32_t mask;
762c1565b61SSam Leffler 
763b032f27cSSam Leffler 		ieee80211_note(ni->ni_vap, "[%6D] %s: size 1600 rate/tt",
764c1565b61SSam Leffler 		    ni->ni_macaddr, ":", __func__);
765c1565b61SSam Leffler 		for (mask = sn->ratemask, rix = 0; mask != 0; mask >>= 1, rix++) {
766c1565b61SSam Leffler 			if ((mask & 1) == 0)
767b032f27cSSam Leffler 				continue;
768a6a308a4SAdrian Chadd 			printf(" %d/%d", dot11rate(rt, rix),
769*e09c8c4cSAdrian Chadd 			    calc_usecs_unicast_packet(sc, 1600, rix, 0,0,
770*e09c8c4cSAdrian Chadd 			        (ni->ni_htcap & IEEE80211_HTCAP_CHWIDTH40)));
771b032f27cSSam Leffler 		}
772b032f27cSSam Leffler 		printf("\n");
773b032f27cSSam Leffler 	}
774b032f27cSSam Leffler #endif
775b2763056SSam Leffler 	for (y = 0; y < NUM_PACKET_SIZE_BINS; y++) {
776b2763056SSam Leffler 		int size = bin_to_size(y);
777c1565b61SSam Leffler 		uint32_t mask;
778c1565b61SSam Leffler 
779b2763056SSam Leffler 		sn->packets_sent[y] = 0;
780c1565b61SSam Leffler 		sn->current_sample_rix[y] = -1;
781c1565b61SSam Leffler 		sn->last_sample_rix[y] = 0;
782c1565b61SSam Leffler 		/* XXX start with first valid rate */
783c1565b61SSam Leffler 		sn->current_rix[y] = ffs(sn->ratemask)-1;
784b2763056SSam Leffler 
785c1565b61SSam Leffler 		/*
786c1565b61SSam Leffler 		 * Initialize the statistics buckets; these are
787c1565b61SSam Leffler 		 * indexed by the rate code index.
788c1565b61SSam Leffler 		 */
789c1565b61SSam Leffler 		for (rix = 0, mask = sn->ratemask; mask != 0; rix++, mask >>= 1) {
790c1565b61SSam Leffler 			if ((mask & 1) == 0)		/* not a valid rate */
791c1565b61SSam Leffler 				continue;
792c1565b61SSam Leffler 			sn->stats[y][rix].successive_failures = 0;
793c1565b61SSam Leffler 			sn->stats[y][rix].tries = 0;
794c1565b61SSam Leffler 			sn->stats[y][rix].total_packets = 0;
795c1565b61SSam Leffler 			sn->stats[y][rix].packets_acked = 0;
796c1565b61SSam Leffler 			sn->stats[y][rix].last_tx = 0;
797b2763056SSam Leffler 
798c1565b61SSam Leffler 			sn->stats[y][rix].perfect_tx_time =
799*e09c8c4cSAdrian Chadd 			    calc_usecs_unicast_packet(sc, size, rix, 0, 0,
800*e09c8c4cSAdrian Chadd 			    (ni->ni_htcap & IEEE80211_HTCAP_CHWIDTH40));
801c1565b61SSam Leffler 			sn->stats[y][rix].average_tx_time =
802c1565b61SSam Leffler 			    sn->stats[y][rix].perfect_tx_time;
803b2763056SSam Leffler 		}
804b91bf513SSam Leffler 	}
805c1565b61SSam Leffler #if 0
806c1565b61SSam Leffler 	/* XXX 0, num_rates-1 are wrong */
807b032f27cSSam Leffler 	IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_RATECTL, ni,
808b032f27cSSam Leffler 	    "%s: %d rates %d%sMbps (%dus)- %d%sMbps (%dus)", __func__,
809b91bf513SSam Leffler 	    sn->num_rates,
810c1565b61SSam Leffler 	    DOT11RATE(0)/2, DOT11RATE(0) % 1 ? ".5" : "",
811b91bf513SSam Leffler 	    sn->stats[1][0].perfect_tx_time,
812c1565b61SSam Leffler 	    DOT11RATE(sn->num_rates-1)/2, DOT11RATE(sn->num_rates-1) % 1 ? ".5" : "",
813b91bf513SSam Leffler 	    sn->stats[1][sn->num_rates-1].perfect_tx_time
814b91bf513SSam Leffler 	);
815c1565b61SSam Leffler #endif
816b032f27cSSam Leffler 	/* set the visible bit-rate */
817c1565b61SSam Leffler 	if (sn->static_rix != -1)
818c1565b61SSam Leffler 		ni->ni_txrate = DOT11RATE(sn->static_rix);
819d0d425bfSSam Leffler 	else
820c1565b61SSam Leffler 		ni->ni_txrate = RATE(0);
821fa20c234SSam Leffler #undef RATE
822c1565b61SSam Leffler #undef DOT11RATE
823fa20c234SSam Leffler }
824fa20c234SSam Leffler 
825f0fd5e07SSam Leffler static void
826c1565b61SSam Leffler sample_stats(void *arg, struct ieee80211_node *ni)
827c1565b61SSam Leffler {
828c1565b61SSam Leffler 	struct ath_softc *sc = arg;
829c1565b61SSam Leffler 	const HAL_RATE_TABLE *rt = sc->sc_currates;
830c1565b61SSam Leffler 	struct sample_node *sn = ATH_NODE_SAMPLE(ATH_NODE(ni));
831c1565b61SSam Leffler 	uint32_t mask;
832c1565b61SSam Leffler 	int rix, y;
833c1565b61SSam Leffler 
834c1565b61SSam Leffler 	printf("\n[%s] refcnt %d static_rix %d ratemask 0x%x\n",
835c1565b61SSam Leffler 	    ether_sprintf(ni->ni_macaddr), ieee80211_node_refcnt(ni),
836c1565b61SSam Leffler 	    sn->static_rix, sn->ratemask);
837c1565b61SSam Leffler 	for (y = 0; y < NUM_PACKET_SIZE_BINS; y++) {
838c1565b61SSam Leffler 		printf("[%4u] cur rix %d since switch: packets %d ticks %u\n",
839c1565b61SSam Leffler 		    bin_to_size(y), sn->current_rix[y],
840c1565b61SSam Leffler 		    sn->packets_since_switch[y], sn->ticks_since_switch[y]);
841c1565b61SSam Leffler 		printf("[%4u] last sample %d cur sample %d packets sent %d\n",
842c1565b61SSam Leffler 		    bin_to_size(y), sn->last_sample_rix[y],
843c1565b61SSam Leffler 		    sn->current_sample_rix[y], sn->packets_sent[y]);
844c1565b61SSam Leffler 		printf("[%4u] packets since sample %d sample tt %u\n",
845c1565b61SSam Leffler 		    bin_to_size(y), sn->packets_since_sample[y],
846c1565b61SSam Leffler 		    sn->sample_tt[y]);
847c1565b61SSam Leffler 	}
848c1565b61SSam Leffler 	for (mask = sn->ratemask, rix = 0; mask != 0; mask >>= 1, rix++) {
849c1565b61SSam Leffler 		if ((mask & 1) == 0)
850c1565b61SSam Leffler 				continue;
851c1565b61SSam Leffler 		for (y = 0; y < NUM_PACKET_SIZE_BINS; y++) {
852c1565b61SSam Leffler 			if (sn->stats[y][rix].total_packets == 0)
853c1565b61SSam Leffler 				continue;
854c1565b61SSam Leffler 			printf("[%2u:%4u] %8d:%-8d (%3d%%) T %8d F %4d avg %5u last %u\n",
855a6a308a4SAdrian Chadd 			    dot11rate(rt, rix),
856c1565b61SSam Leffler 			    bin_to_size(y),
857c1565b61SSam Leffler 			    sn->stats[y][rix].total_packets,
858c1565b61SSam Leffler 			    sn->stats[y][rix].packets_acked,
859c1565b61SSam Leffler 			    (100*sn->stats[y][rix].packets_acked)/sn->stats[y][rix].total_packets,
860c1565b61SSam Leffler 			    sn->stats[y][rix].tries,
861c1565b61SSam Leffler 			    sn->stats[y][rix].successive_failures,
862c1565b61SSam Leffler 			    sn->stats[y][rix].average_tx_time,
863c1565b61SSam Leffler 			    ticks - sn->stats[y][rix].last_tx);
864c1565b61SSam Leffler 		}
865c1565b61SSam Leffler 	}
866c1565b61SSam Leffler }
867c1565b61SSam Leffler 
868c1565b61SSam Leffler static int
869c1565b61SSam Leffler ath_rate_sysctl_stats(SYSCTL_HANDLER_ARGS)
870c1565b61SSam Leffler {
871c1565b61SSam Leffler 	struct ath_softc *sc = arg1;
872c1565b61SSam Leffler 	struct ifnet *ifp = sc->sc_ifp;
873c1565b61SSam Leffler 	struct ieee80211com *ic = ifp->if_l2com;
874c1565b61SSam Leffler 	int error, v;
875c1565b61SSam Leffler 
876c1565b61SSam Leffler 	v = 0;
877c1565b61SSam Leffler 	error = sysctl_handle_int(oidp, &v, 0, req);
878c1565b61SSam Leffler 	if (error || !req->newptr)
879c1565b61SSam Leffler 		return error;
880c1565b61SSam Leffler 	ieee80211_iterate_nodes(&ic->ic_sta, sample_stats, sc);
881c1565b61SSam Leffler 	return 0;
882c1565b61SSam Leffler }
883c1565b61SSam Leffler 
884c1565b61SSam Leffler static int
885c1565b61SSam Leffler ath_rate_sysctl_smoothing_rate(SYSCTL_HANDLER_ARGS)
886c1565b61SSam Leffler {
887c1565b61SSam Leffler 	struct sample_softc *ssc = arg1;
888c1565b61SSam Leffler 	int rate, error;
889c1565b61SSam Leffler 
890c1565b61SSam Leffler 	rate = ssc->smoothing_rate;
891c1565b61SSam Leffler 	error = sysctl_handle_int(oidp, &rate, 0, req);
892c1565b61SSam Leffler 	if (error || !req->newptr)
893c1565b61SSam Leffler 		return error;
894c1565b61SSam Leffler 	if (!(0 <= rate && rate < 100))
895c1565b61SSam Leffler 		return EINVAL;
896c1565b61SSam Leffler 	ssc->smoothing_rate = rate;
897c1565b61SSam Leffler 	ssc->smoothing_minpackets = 100 / (100 - rate);
898c1565b61SSam Leffler 	return 0;
899c1565b61SSam Leffler }
900c1565b61SSam Leffler 
901c1565b61SSam Leffler static int
902c1565b61SSam Leffler ath_rate_sysctl_sample_rate(SYSCTL_HANDLER_ARGS)
903c1565b61SSam Leffler {
904c1565b61SSam Leffler 	struct sample_softc *ssc = arg1;
905c1565b61SSam Leffler 	int rate, error;
906c1565b61SSam Leffler 
907c1565b61SSam Leffler 	rate = ssc->sample_rate;
908c1565b61SSam Leffler 	error = sysctl_handle_int(oidp, &rate, 0, req);
909c1565b61SSam Leffler 	if (error || !req->newptr)
910c1565b61SSam Leffler 		return error;
911c1565b61SSam Leffler 	if (!(2 <= rate && rate <= 100))
912c1565b61SSam Leffler 		return EINVAL;
913c1565b61SSam Leffler 	ssc->sample_rate = rate;
914c1565b61SSam Leffler 	return 0;
915c1565b61SSam Leffler }
916c1565b61SSam Leffler 
917c1565b61SSam Leffler static void
918c1565b61SSam Leffler ath_rate_sysctlattach(struct ath_softc *sc, struct sample_softc *ssc)
919fa20c234SSam Leffler {
920fa20c234SSam Leffler 	struct sysctl_ctx_list *ctx = device_get_sysctl_ctx(sc->sc_dev);
921fa20c234SSam Leffler 	struct sysctl_oid *tree = device_get_sysctl_tree(sc->sc_dev);
922fa20c234SSam Leffler 
923c1565b61SSam Leffler 	SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
924c1565b61SSam Leffler 	    "smoothing_rate", CTLTYPE_INT | CTLFLAG_RW, ssc, 0,
925c1565b61SSam Leffler 	    ath_rate_sysctl_smoothing_rate, "I",
926c1565b61SSam Leffler 	    "sample: smoothing rate for avg tx time (%%)");
927c1565b61SSam Leffler 	SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
928c1565b61SSam Leffler 	    "sample_rate", CTLTYPE_INT | CTLFLAG_RW, ssc, 0,
929c1565b61SSam Leffler 	    ath_rate_sysctl_sample_rate, "I",
930c1565b61SSam Leffler 	    "sample: percent air time devoted to sampling new rates (%%)");
931c1565b61SSam Leffler 	/* XXX max_successive_failures, stale_failure_timeout, min_switch */
932c1565b61SSam Leffler 	SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
933c1565b61SSam Leffler 	    "sample_stats", CTLTYPE_INT | CTLFLAG_RW, sc, 0,
934c1565b61SSam Leffler 	    ath_rate_sysctl_stats, "I", "sample: print statistics");
935fa20c234SSam Leffler }
936fa20c234SSam Leffler 
937fa20c234SSam Leffler struct ath_ratectrl *
938fa20c234SSam Leffler ath_rate_attach(struct ath_softc *sc)
939fa20c234SSam Leffler {
940c1565b61SSam Leffler 	struct sample_softc *ssc;
941fa20c234SSam Leffler 
942c1565b61SSam Leffler 	ssc = malloc(sizeof(struct sample_softc), M_DEVBUF, M_NOWAIT|M_ZERO);
943c1565b61SSam Leffler 	if (ssc == NULL)
944fa20c234SSam Leffler 		return NULL;
945c1565b61SSam Leffler 	ssc->arc.arc_space = sizeof(struct sample_node);
946c1565b61SSam Leffler 	ssc->smoothing_rate = 95;		/* ewma percentage ([0..99]) */
947c1565b61SSam Leffler 	ssc->smoothing_minpackets = 100 / (100 - ssc->smoothing_rate);
948c1565b61SSam Leffler 	ssc->sample_rate = 10;			/* %time to try diff tx rates */
949c1565b61SSam Leffler 	ssc->max_successive_failures = 3;	/* threshold for rate sampling*/
950c1565b61SSam Leffler 	ssc->stale_failure_timeout = 10 * hz;	/* 10 seconds */
951c1565b61SSam Leffler 	ssc->min_switch = hz;			/* 1 second */
952c1565b61SSam Leffler 	ath_rate_sysctlattach(sc, ssc);
953c1565b61SSam Leffler 	return &ssc->arc;
954fa20c234SSam Leffler }
955fa20c234SSam Leffler 
956fa20c234SSam Leffler void
957fa20c234SSam Leffler ath_rate_detach(struct ath_ratectrl *arc)
958fa20c234SSam Leffler {
959c1565b61SSam Leffler 	struct sample_softc *ssc = (struct sample_softc *) arc;
960fa20c234SSam Leffler 
961c1565b61SSam Leffler 	free(ssc, M_DEVBUF);
962fa20c234SSam Leffler }
963