xref: /freebsd/sys/dev/ath/ath_rate/sample/sample.c (revision a6a308a4df69b5b9c7f7da2f06890b227015ba82)
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>
78*a6a308a4SAdrian 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 
146*a6a308a4SAdrian Chadd static int
147*a6a308a4SAdrian Chadd dot11rate(const HAL_RATE_TABLE *rt, int rix)
148*a6a308a4SAdrian Chadd {
149*a6a308a4SAdrian Chadd 	return rt->info[rix].phy == IEEE80211_T_HT ?
150*a6a308a4SAdrian Chadd 	    rt->info[rix].dot11Rate : (rt->info[rix].dot11Rate & IEEE80211_RATE_VAL) / 2;
151*a6a308a4SAdrian Chadd }
152*a6a308a4SAdrian 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)
197*a6a308a4SAdrian 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
244*a6a308a4SAdrian 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)
253*a6a308a4SAdrian 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 			 */
348*a6a308a4SAdrian 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
362*a6a308a4SAdrian Chadd #undef MCS
363c1565b61SSam Leffler #undef RATE
364fa20c234SSam Leffler }
365fa20c234SSam Leffler 
366fa20c234SSam Leffler void
367fa20c234SSam Leffler ath_rate_setupxtxdesc(struct ath_softc *sc, struct ath_node *an,
368a4d8dd10SSam Leffler 		      struct ath_desc *ds, int shortPreamble, u_int8_t rix)
369fa20c234SSam Leffler {
370fa20c234SSam Leffler 	struct sample_node *sn = ATH_NODE_SAMPLE(an);
371c1565b61SSam Leffler 	const struct txschedule *sched = &sn->sched[rix];
372c1565b61SSam Leffler 	const HAL_RATE_TABLE *rt = sc->sc_currates;
373c1565b61SSam Leffler 	uint8_t rix1, s1code, rix2, s2code, rix3, s3code;
374fa20c234SSam Leffler 
375c1565b61SSam Leffler 	/* XXX precalculate short preamble tables */
376c1565b61SSam Leffler 	rix1 = sched->r1;
377c1565b61SSam Leffler 	s1code = rt->info[rix1].rateCode
378c1565b61SSam Leffler 	       | (shortPreamble ? rt->info[rix1].shortPreamble : 0);
379c1565b61SSam Leffler 	rix2 = sched->r2;
380c1565b61SSam Leffler 	s2code = rt->info[rix2].rateCode
381c1565b61SSam Leffler 	       | (shortPreamble ? rt->info[rix2].shortPreamble : 0);
382c1565b61SSam Leffler 	rix3 = sched->r3;
383c1565b61SSam Leffler 	s3code = rt->info[rix3].rateCode
384c1565b61SSam Leffler 	       | (shortPreamble ? rt->info[rix3].shortPreamble : 0);
385c1565b61SSam Leffler 	ath_hal_setupxtxdesc(sc->sc_ah, ds,
386c1565b61SSam Leffler 	    s1code, sched->t1,		/* series 1 */
387c1565b61SSam Leffler 	    s2code, sched->t2,		/* series 2 */
388c1565b61SSam Leffler 	    s3code, sched->t3);		/* series 3 */
389fa20c234SSam Leffler }
390fa20c234SSam Leffler 
391b2763056SSam Leffler static void
392b2763056SSam Leffler update_stats(struct ath_softc *sc, struct ath_node *an,
393b2763056SSam Leffler 		  int frame_size,
394c1565b61SSam Leffler 		  int rix0, int tries0,
395c1565b61SSam Leffler 		  int rix1, int tries1,
396c1565b61SSam Leffler 		  int rix2, int tries2,
397c1565b61SSam Leffler 		  int rix3, int tries3,
398b2763056SSam Leffler 		  int short_tries, int tries, int status)
399fa20c234SSam Leffler {
400fa20c234SSam Leffler 	struct sample_node *sn = ATH_NODE_SAMPLE(an);
401fa20c234SSam Leffler 	struct sample_softc *ssc = ATH_SOFTC_SAMPLE(sc);
402c1565b61SSam Leffler 	const int size_bin = size_to_bin(frame_size);
403c1565b61SSam Leffler 	const int size = bin_to_size(size_bin);
404c1565b61SSam Leffler 	int tt, tries_so_far;
405fa20c234SSam Leffler 
406c1565b61SSam Leffler 	if (!IS_RATE_DEFINED(sn, rix0))
40765f9edeeSSam Leffler 		return;
408c1565b61SSam Leffler 	tt = calc_usecs_unicast_packet(sc, size, rix0, short_tries,
409b2763056SSam Leffler 		MIN(tries0, tries) - 1);
410c1565b61SSam Leffler 	tries_so_far = tries0;
411c1565b61SSam Leffler 
412c1565b61SSam Leffler 	if (tries1 && tries_so_far < tries) {
413c1565b61SSam Leffler 		if (!IS_RATE_DEFINED(sn, rix1))
41465f9edeeSSam Leffler 			return;
415c1565b61SSam Leffler 		tt += calc_usecs_unicast_packet(sc, size, rix1, short_tries,
416b2763056SSam Leffler 			MIN(tries1 + tries_so_far, tries) - tries_so_far - 1);
417b2763056SSam Leffler 		tries_so_far += tries1;
418b2763056SSam Leffler 	}
419b2763056SSam Leffler 
420c1565b61SSam Leffler 	if (tries2 && tries_so_far < tries) {
421c1565b61SSam Leffler 		if (!IS_RATE_DEFINED(sn, rix2))
42265f9edeeSSam Leffler 			return;
423c1565b61SSam Leffler 		tt += calc_usecs_unicast_packet(sc, size, rix2, short_tries,
424c1565b61SSam Leffler 			MIN(tries2 + tries_so_far, tries) - tries_so_far - 1);
425c1565b61SSam Leffler 		tries_so_far += tries2;
426c1565b61SSam Leffler 	}
427c1565b61SSam Leffler 
428c1565b61SSam Leffler 	if (tries3 && tries_so_far < tries) {
429c1565b61SSam Leffler 		if (!IS_RATE_DEFINED(sn, rix3))
430c1565b61SSam Leffler 			return;
431c1565b61SSam Leffler 		tt += calc_usecs_unicast_packet(sc, size, rix3, short_tries,
432b2763056SSam Leffler 			MIN(tries3 + tries_so_far, tries) - tries_so_far - 1);
433b2763056SSam Leffler 	}
434c1565b61SSam Leffler 
435c1565b61SSam Leffler 	if (sn->stats[size_bin][rix0].total_packets < ssc->smoothing_minpackets) {
436fa20c234SSam Leffler 		/* just average the first few packets */
437c1565b61SSam Leffler 		int avg_tx = sn->stats[size_bin][rix0].average_tx_time;
438c1565b61SSam Leffler 		int packets = sn->stats[size_bin][rix0].total_packets;
439c1565b61SSam Leffler 		sn->stats[size_bin][rix0].average_tx_time = (tt+(avg_tx*packets))/(packets+1);
440fa20c234SSam Leffler 	} else {
441fa20c234SSam Leffler 		/* use a ewma */
442c1565b61SSam Leffler 		sn->stats[size_bin][rix0].average_tx_time =
443c1565b61SSam Leffler 			((sn->stats[size_bin][rix0].average_tx_time * ssc->smoothing_rate) +
444c1565b61SSam Leffler 			 (tt * (100 - ssc->smoothing_rate))) / 100;
445fa20c234SSam Leffler 	}
446fa20c234SSam Leffler 
447c1565b61SSam Leffler 	if (status != 0) {
448fa20c234SSam Leffler 		int y;
449c1565b61SSam Leffler 		sn->stats[size_bin][rix0].successive_failures++;
450b91bf513SSam Leffler 		for (y = size_bin+1; y < NUM_PACKET_SIZE_BINS; y++) {
451c1565b61SSam Leffler 			/*
452c1565b61SSam Leffler 			 * Also say larger packets failed since we
453c1565b61SSam Leffler 			 * assume if a small packet fails at a
454b91bf513SSam Leffler 			 * bit-rate then a larger one will also.
455b91bf513SSam Leffler 			 */
456c1565b61SSam Leffler 			sn->stats[y][rix0].successive_failures++;
457c1565b61SSam Leffler 			sn->stats[y][rix0].last_tx = ticks;
458c1565b61SSam Leffler 			sn->stats[y][rix0].tries += tries;
459c1565b61SSam Leffler 			sn->stats[y][rix0].total_packets++;
460fa20c234SSam Leffler 		}
461fa20c234SSam Leffler 	} else {
462c1565b61SSam Leffler 		sn->stats[size_bin][rix0].packets_acked++;
463c1565b61SSam Leffler 		sn->stats[size_bin][rix0].successive_failures = 0;
464fa20c234SSam Leffler 	}
465c1565b61SSam Leffler 	sn->stats[size_bin][rix0].tries += tries;
466c1565b61SSam Leffler 	sn->stats[size_bin][rix0].last_tx = ticks;
467c1565b61SSam Leffler 	sn->stats[size_bin][rix0].total_packets++;
468b2763056SSam Leffler 
469c1565b61SSam Leffler 	if (rix0 == sn->current_sample_rix[size_bin]) {
470b032f27cSSam Leffler 		IEEE80211_NOTE(an->an_node.ni_vap, IEEE80211_MSG_RATECTL,
471b032f27cSSam Leffler 		   &an->an_node,
472b032f27cSSam Leffler "%s: size %d %s sample rate %d tries (%d/%d) tt %d avg_tt (%d/%d)",
473b032f27cSSam Leffler 		    __func__,
47465f9edeeSSam Leffler 		    size,
47565f9edeeSSam Leffler 		    status ? "FAIL" : "OK",
476c1565b61SSam Leffler 		    rix0, short_tries, tries, tt,
477c1565b61SSam Leffler 		    sn->stats[size_bin][rix0].average_tx_time,
478c1565b61SSam Leffler 		    sn->stats[size_bin][rix0].perfect_tx_time);
479b2763056SSam Leffler 		sn->sample_tt[size_bin] = tt;
480c1565b61SSam Leffler 		sn->current_sample_rix[size_bin] = -1;
481b2763056SSam Leffler 	}
482b2763056SSam Leffler }
483b2763056SSam Leffler 
484ec9ee5e7SSam Leffler static void
485ec9ee5e7SSam Leffler badrate(struct ifnet *ifp, int series, int hwrate, int tries, int status)
486ec9ee5e7SSam Leffler {
487ec9ee5e7SSam Leffler 	if_printf(ifp, "bad series%d hwrate 0x%x, tries %u ts_status 0x%x\n",
488ec9ee5e7SSam Leffler 	    series, hwrate, tries, status);
489ec9ee5e7SSam Leffler }
490ec9ee5e7SSam Leffler 
491b2763056SSam Leffler void
49243e9cf7cSSam Leffler ath_rate_tx_complete(struct ath_softc *sc, struct ath_node *an,
49365f9edeeSSam Leffler 	const struct ath_buf *bf)
494b2763056SSam Leffler {
495b032f27cSSam Leffler 	struct ifnet *ifp = sc->sc_ifp;
496b032f27cSSam Leffler 	struct ieee80211com *ic = ifp->if_l2com;
497b2763056SSam Leffler 	struct sample_node *sn = ATH_NODE_SAMPLE(an);
49865f9edeeSSam Leffler 	const struct ath_tx_status *ts = &bf->bf_status.ds_txstat;
49965f9edeeSSam Leffler 	const struct ath_desc *ds0 = &bf->bf_desc[0];
500c1565b61SSam Leffler 	int final_rix, short_tries, long_tries, frame_size;
50146d4d74cSSam Leffler 	const HAL_RATE_TABLE *rt = sc->sc_currates;
5021bb9a085SSam Leffler 	int mrr;
503b2763056SSam Leffler 
504f6cbf16aSSam Leffler 	final_rix = rt->rateCodeToIndex[ts->ts_rate];
50565f9edeeSSam Leffler 	short_tries = ts->ts_shortretry;
50665f9edeeSSam Leffler 	long_tries = ts->ts_longretry + 1;
50743e9cf7cSSam Leffler 	frame_size = ds0->ds_ctl0 & 0x0fff; /* low-order 12 bits of ds_ctl0 */
50843e9cf7cSSam Leffler 	if (frame_size == 0)		    /* NB: should not happen */
509b2763056SSam Leffler 		frame_size = 1500;
510b2763056SSam Leffler 
511c1565b61SSam Leffler 	if (sn->ratemask == 0) {
512b032f27cSSam Leffler 		IEEE80211_NOTE(an->an_node.ni_vap, IEEE80211_MSG_RATECTL,
513b032f27cSSam Leffler 		    &an->an_node,
514b032f27cSSam Leffler 		    "%s: size %d %s rate/try %d/%d no rates yet",
515b032f27cSSam Leffler 		    __func__,
51643e9cf7cSSam Leffler 		    bin_to_size(size_to_bin(frame_size)),
51765f9edeeSSam Leffler 		    ts->ts_status ? "FAIL" : "OK",
51843e9cf7cSSam Leffler 		    short_tries, long_tries);
519b2763056SSam Leffler 		return;
520b2763056SSam Leffler 	}
52165f9edeeSSam Leffler 	mrr = sc->sc_mrretry && !(ic->ic_flags & IEEE80211_F_USEPROT);
522f6cbf16aSSam Leffler 	if (!mrr || ts->ts_finaltsi == 0) {
523c1565b61SSam Leffler 		if (!IS_RATE_DEFINED(sn, final_rix)) {
524ec9ee5e7SSam Leffler 			badrate(ifp, 0, ts->ts_rate, long_tries, ts->ts_status);
525ec9ee5e7SSam Leffler 			return;
526ec9ee5e7SSam Leffler 		}
52765f9edeeSSam Leffler 		/*
52865f9edeeSSam Leffler 		 * Only one rate was used; optimize work.
52965f9edeeSSam Leffler 		 */
530b032f27cSSam Leffler 		IEEE80211_NOTE(an->an_node.ni_vap, IEEE80211_MSG_RATECTL,
531b032f27cSSam Leffler 		     &an->an_node, "%s: size %d %s rate/try %d/%d/%d",
532b032f27cSSam Leffler 		     __func__,
53343e9cf7cSSam Leffler 		     bin_to_size(size_to_bin(frame_size)),
53465f9edeeSSam Leffler 		     ts->ts_status ? "FAIL" : "OK",
535*a6a308a4SAdrian Chadd 		     dot11rate(rt, final_rix), short_tries, long_tries);
536b2763056SSam Leffler 		update_stats(sc, an, frame_size,
537c1565b61SSam Leffler 			     final_rix, long_tries,
538b2763056SSam Leffler 			     0, 0,
539b2763056SSam Leffler 			     0, 0,
540b2763056SSam Leffler 			     0, 0,
54165f9edeeSSam Leffler 			     short_tries, long_tries, ts->ts_status);
542b2763056SSam Leffler 	} else {
543bd97c52aSAdrian Chadd 		int hwrates[4], tries[4], rix[4];
54465f9edeeSSam Leffler 		int finalTSIdx = ts->ts_finaltsi;
545bd97c52aSAdrian Chadd 		int i;
546b2763056SSam Leffler 
547b2763056SSam Leffler 		/*
548b2763056SSam Leffler 		 * Process intermediate rates that failed.
549b2763056SSam Leffler 		 */
550bd97c52aSAdrian Chadd 		ath_hal_gettxcompletionrates(sc->sc_ah, ds0, hwrates, tries);
551*a6a308a4SAdrian Chadd 
552*a6a308a4SAdrian Chadd 		for (i = 0; i < 4; i++) {
553bd97c52aSAdrian Chadd 			rix[i] = rt->rateCodeToIndex[hwrates[i]];
554*a6a308a4SAdrian Chadd 		}
555ec9ee5e7SSam Leffler 
556b032f27cSSam Leffler 		IEEE80211_NOTE(an->an_node.ni_vap, IEEE80211_MSG_RATECTL,
557b032f27cSSam Leffler 		    &an->an_node,
558b032f27cSSam Leffler "%s: size %d finaltsidx %d tries %d %s rate/try [%d/%d %d/%d %d/%d %d/%d]",
559b032f27cSSam Leffler 		     __func__,
560b2763056SSam Leffler 		     bin_to_size(size_to_bin(frame_size)),
561b2763056SSam Leffler 		     finalTSIdx,
562b2763056SSam Leffler 		     long_tries,
56365f9edeeSSam Leffler 		     ts->ts_status ? "FAIL" : "OK",
564*a6a308a4SAdrian Chadd 		     dot11rate(rt, rix[0]), tries[0],
565*a6a308a4SAdrian Chadd 		     dot11rate(rt, rix[1]), tries[1],
566*a6a308a4SAdrian Chadd 		     dot11rate(rt, rix[2]), tries[2],
567*a6a308a4SAdrian Chadd 		     dot11rate(rt, rix[3]), tries[3]);
568c1565b61SSam Leffler 
569*a6a308a4SAdrian Chadd 		for (i = 0; i < 4; i++) {
570*a6a308a4SAdrian Chadd 			if (tries[i] && !IS_RATE_DEFINED(sn, rix[i]))
571*a6a308a4SAdrian Chadd 				badrate(ifp, 0, hwrates[i], tries[i], ts->ts_status);
572*a6a308a4SAdrian Chadd 		}
573b2763056SSam Leffler 
57465f9edeeSSam Leffler 		/*
57565f9edeeSSam Leffler 		 * NB: series > 0 are not penalized for failure
57665f9edeeSSam Leffler 		 * based on the try counts under the assumption
57765f9edeeSSam Leffler 		 * that losses are often bursty and since we
57865f9edeeSSam Leffler 		 * sample higher rates 1 try at a time doing so
57965f9edeeSSam Leffler 		 * may unfairly penalize them.
58065f9edeeSSam Leffler 		 */
581bd97c52aSAdrian Chadd 		if (tries[0]) {
582b2763056SSam Leffler 			update_stats(sc, an, frame_size,
583bd97c52aSAdrian Chadd 				     rix[0], tries[0],
584bd97c52aSAdrian Chadd 				     rix[1], tries[1],
585bd97c52aSAdrian Chadd 				     rix[2], tries[2],
586bd97c52aSAdrian Chadd 				     rix[3], tries[3],
58765f9edeeSSam Leffler 				     short_tries, long_tries,
588bd97c52aSAdrian Chadd 				     long_tries > tries[0]);
589bd97c52aSAdrian Chadd 			long_tries -= tries[0];
590b2763056SSam Leffler 		}
591b2763056SSam Leffler 
592bd97c52aSAdrian Chadd 		if (tries[1] && finalTSIdx > 0) {
593b2763056SSam Leffler 			update_stats(sc, an, frame_size,
594bd97c52aSAdrian Chadd 				     rix[1], tries[1],
595bd97c52aSAdrian Chadd 				     rix[2], tries[2],
596bd97c52aSAdrian Chadd 				     rix[3], tries[3],
597b2763056SSam Leffler 				     0, 0,
59865f9edeeSSam Leffler 				     short_tries, long_tries,
59965f9edeeSSam Leffler 				     ts->ts_status);
600bd97c52aSAdrian Chadd 			long_tries -= tries[1];
601b2763056SSam Leffler 		}
602b2763056SSam Leffler 
603bd97c52aSAdrian Chadd 		if (tries[2] && finalTSIdx > 1) {
604b2763056SSam Leffler 			update_stats(sc, an, frame_size,
605bd97c52aSAdrian Chadd 				     rix[2], tries[2],
606bd97c52aSAdrian Chadd 				     rix[3], tries[3],
607b2763056SSam Leffler 				     0, 0,
608b2763056SSam Leffler 				     0, 0,
60965f9edeeSSam Leffler 				     short_tries, long_tries,
61065f9edeeSSam Leffler 				     ts->ts_status);
611bd97c52aSAdrian Chadd 			long_tries -= tries[2];
612b2763056SSam Leffler 		}
613b2763056SSam Leffler 
614bd97c52aSAdrian Chadd 		if (tries[3] && finalTSIdx > 2) {
615b2763056SSam Leffler 			update_stats(sc, an, frame_size,
616bd97c52aSAdrian Chadd 				     rix[3], tries[3],
617b2763056SSam Leffler 				     0, 0,
618b2763056SSam Leffler 				     0, 0,
619b2763056SSam Leffler 				     0, 0,
62065f9edeeSSam Leffler 				     short_tries, long_tries,
62165f9edeeSSam Leffler 				     ts->ts_status);
622b2763056SSam Leffler 		}
623b2763056SSam Leffler 	}
624fa20c234SSam Leffler }
625fa20c234SSam Leffler 
626fa20c234SSam Leffler void
627fa20c234SSam Leffler ath_rate_newassoc(struct ath_softc *sc, struct ath_node *an, int isnew)
628fa20c234SSam Leffler {
629fa20c234SSam Leffler 	if (isnew)
630b2763056SSam Leffler 		ath_rate_ctl_reset(sc, &an->an_node);
631fa20c234SSam Leffler }
632fa20c234SSam Leffler 
633c1565b61SSam Leffler static const struct txschedule *mrr_schedules[IEEE80211_MODE_MAX+2] = {
634c1565b61SSam Leffler 	NULL,		/* IEEE80211_MODE_AUTO */
635c1565b61SSam Leffler 	series_11a,	/* IEEE80211_MODE_11A */
636c1565b61SSam Leffler 	series_11g,	/* IEEE80211_MODE_11B */
637c1565b61SSam Leffler 	series_11g,	/* IEEE80211_MODE_11G */
638c1565b61SSam Leffler 	NULL,		/* IEEE80211_MODE_FH */
639c1565b61SSam Leffler 	series_11a,	/* IEEE80211_MODE_TURBO_A */
640c1565b61SSam Leffler 	series_11g,	/* IEEE80211_MODE_TURBO_G */
641c1565b61SSam Leffler 	series_11a,	/* IEEE80211_MODE_STURBO_A */
642*a6a308a4SAdrian Chadd 	series_11na,	/* IEEE80211_MODE_11NA */
643*a6a308a4SAdrian Chadd 	series_11ng,	/* IEEE80211_MODE_11NG */
644c1565b61SSam Leffler 	series_half,	/* IEEE80211_MODE_HALF */
645c1565b61SSam Leffler 	series_quarter,	/* IEEE80211_MODE_QUARTER */
646c1565b61SSam Leffler };
647c1565b61SSam Leffler 
648fa20c234SSam Leffler /*
649fa20c234SSam Leffler  * Initialize the tables for a node.
650fa20c234SSam Leffler  */
651fa20c234SSam Leffler static void
652b2763056SSam Leffler ath_rate_ctl_reset(struct ath_softc *sc, struct ieee80211_node *ni)
653fa20c234SSam Leffler {
654fa20c234SSam Leffler #define	RATE(_ix)	(ni->ni_rates.rs_rates[(_ix)] & IEEE80211_RATE_VAL)
655c1565b61SSam Leffler #define	DOT11RATE(_ix)	(rt->info[(_ix)].dot11Rate & IEEE80211_RATE_VAL)
656*a6a308a4SAdrian Chadd #define	MCS(_ix)	(ni->ni_htrates.rs_rates[_ix] | IEEE80211_RATE_MCS)
657*a6a308a4SAdrian Chadd 
658fa20c234SSam Leffler 	struct ath_node *an = ATH_NODE(ni);
659c62362cbSSam Leffler 	const struct ieee80211_txparam *tp = ni->ni_txparms;
660fa20c234SSam Leffler 	struct sample_node *sn = ATH_NODE_SAMPLE(an);
661fa20c234SSam Leffler 	const HAL_RATE_TABLE *rt = sc->sc_currates;
662c1565b61SSam Leffler 	int x, y, srate, rix;
663fa20c234SSam Leffler 
664fa20c234SSam Leffler 	KASSERT(rt != NULL, ("no rate table, mode %u", sc->sc_curmode));
665c1565b61SSam Leffler 
666c1565b61SSam Leffler 	KASSERT(sc->sc_curmode < IEEE80211_MODE_MAX+2,
667c1565b61SSam Leffler 	    ("curmode %u", sc->sc_curmode));
668c1565b61SSam Leffler 	sn->sched = mrr_schedules[sc->sc_curmode];
669c1565b61SSam Leffler 	KASSERT(sn->sched != NULL,
670c1565b61SSam Leffler 	    ("no mrr schedule for mode %u", sc->sc_curmode));
671c1565b61SSam Leffler 
672c1565b61SSam Leffler         sn->static_rix = -1;
673b032f27cSSam Leffler 	if (tp != NULL && tp->ucastrate != IEEE80211_FIXED_RATE_NONE) {
674fa20c234SSam Leffler 		/*
6755f82a460SSam Leffler 		 * A fixed rate is to be used; ucastrate is the IEEE code
6765f82a460SSam Leffler 		 * for this rate (sans basic bit).  Check this against the
6775f82a460SSam Leffler 		 * negotiated rate set for the node.  Note the fixed rate
6785f82a460SSam Leffler 		 * may not be available for various reasons so we only
6795f82a460SSam Leffler 		 * setup the static rate index if the lookup is successful.
680fa20c234SSam Leffler 		 */
681*a6a308a4SAdrian Chadd 
682*a6a308a4SAdrian Chadd 		/* XXX todo: check MCS rates */
683*a6a308a4SAdrian Chadd 
684*a6a308a4SAdrian Chadd 		/* Check legacy rates */
6855f82a460SSam Leffler 		for (srate = ni->ni_rates.rs_nrates - 1; srate >= 0; srate--)
6865f82a460SSam Leffler 			if (RATE(srate) == tp->ucastrate) {
6875f82a460SSam Leffler 				sn->static_rix = sc->sc_rixmap[tp->ucastrate];
6885f82a460SSam Leffler 				break;
6895f82a460SSam Leffler 			}
6905e86169aSSam Leffler #ifdef IEEE80211_DEBUG
6915e86169aSSam Leffler 			if (sn->static_rix == -1) {
6925e86169aSSam Leffler 				IEEE80211_NOTE(ni->ni_vap,
6935e86169aSSam Leffler 				    IEEE80211_MSG_RATECTL, ni,
6945e86169aSSam Leffler 				    "%s: ucastrate %u not found, nrates %u",
6955e86169aSSam Leffler 				    __func__, tp->ucastrate,
6965e86169aSSam Leffler 				    ni->ni_rates.rs_nrates);
6975e86169aSSam Leffler 			}
6985e86169aSSam Leffler #endif
699fa20c234SSam Leffler 	}
700b2763056SSam Leffler 
701c1565b61SSam Leffler 	/*
702c1565b61SSam Leffler 	 * Construct a bitmask of usable rates.  This has all
703c1565b61SSam Leffler 	 * negotiated rates minus those marked by the hal as
704c1565b61SSam Leffler 	 * to be ignored for doing rate control.
705c1565b61SSam Leffler 	 */
706c1565b61SSam Leffler 	sn->ratemask = 0;
707*a6a308a4SAdrian Chadd 	/* MCS rates */
708*a6a308a4SAdrian Chadd 	if (ni->ni_flags & IEEE80211_NODE_HT) {
709*a6a308a4SAdrian Chadd 		for (x = 0; x < ni->ni_htrates.rs_nrates; x++) {
710*a6a308a4SAdrian Chadd 			rix = sc->sc_rixmap[MCS(x)];
711*a6a308a4SAdrian Chadd 			if (rix == 0xff)
712*a6a308a4SAdrian Chadd 				continue;
713*a6a308a4SAdrian Chadd 			/* skip rates marked broken by hal */
714*a6a308a4SAdrian Chadd 			if (!rt->info[rix].valid)
715*a6a308a4SAdrian Chadd 				continue;
716*a6a308a4SAdrian Chadd 			KASSERT(rix < SAMPLE_MAXRATES,
717*a6a308a4SAdrian Chadd 			    ("mcs %u has rix %d", MCS(x), rix));
718*a6a308a4SAdrian Chadd 			sn->ratemask |= 1<<rix;
719*a6a308a4SAdrian Chadd 		}
720*a6a308a4SAdrian Chadd 	}
721*a6a308a4SAdrian Chadd 
722*a6a308a4SAdrian Chadd 	/* Legacy rates */
723fa20c234SSam Leffler 	for (x = 0; x < ni->ni_rates.rs_nrates; x++) {
724c1565b61SSam Leffler 		rix = sc->sc_rixmap[RATE(x)];
725c1565b61SSam Leffler 		if (rix == 0xff)
7260ae09ec5SSam Leffler 			continue;
727c1565b61SSam Leffler 		/* skip rates marked broken by hal */
728c1565b61SSam Leffler 		if (!rt->info[rix].valid)
729c1565b61SSam Leffler 			continue;
730c1565b61SSam Leffler 		KASSERT(rix < SAMPLE_MAXRATES,
731c1565b61SSam Leffler 		    ("rate %u has rix %d", RATE(x), rix));
732c1565b61SSam Leffler 		sn->ratemask |= 1<<rix;
733b2763056SSam Leffler 	}
734b032f27cSSam Leffler #ifdef IEEE80211_DEBUG
735b032f27cSSam Leffler 	if (ieee80211_msg(ni->ni_vap, IEEE80211_MSG_RATECTL)) {
736c1565b61SSam Leffler 		uint32_t mask;
737c1565b61SSam Leffler 
738b032f27cSSam Leffler 		ieee80211_note(ni->ni_vap, "[%6D] %s: size 1600 rate/tt",
739c1565b61SSam Leffler 		    ni->ni_macaddr, ":", __func__);
740c1565b61SSam Leffler 		for (mask = sn->ratemask, rix = 0; mask != 0; mask >>= 1, rix++) {
741c1565b61SSam Leffler 			if ((mask & 1) == 0)
742b032f27cSSam Leffler 				continue;
743*a6a308a4SAdrian Chadd 			printf(" %d/%d", dot11rate(rt, rix),
744c1565b61SSam Leffler 			    calc_usecs_unicast_packet(sc, 1600, rix, 0,0));
745b032f27cSSam Leffler 		}
746b032f27cSSam Leffler 		printf("\n");
747b032f27cSSam Leffler 	}
748b032f27cSSam Leffler #endif
749b2763056SSam Leffler 	for (y = 0; y < NUM_PACKET_SIZE_BINS; y++) {
750b2763056SSam Leffler 		int size = bin_to_size(y);
751c1565b61SSam Leffler 		uint32_t mask;
752c1565b61SSam Leffler 
753b2763056SSam Leffler 		sn->packets_sent[y] = 0;
754c1565b61SSam Leffler 		sn->current_sample_rix[y] = -1;
755c1565b61SSam Leffler 		sn->last_sample_rix[y] = 0;
756c1565b61SSam Leffler 		/* XXX start with first valid rate */
757c1565b61SSam Leffler 		sn->current_rix[y] = ffs(sn->ratemask)-1;
758b2763056SSam Leffler 
759c1565b61SSam Leffler 		/*
760c1565b61SSam Leffler 		 * Initialize the statistics buckets; these are
761c1565b61SSam Leffler 		 * indexed by the rate code index.
762c1565b61SSam Leffler 		 */
763c1565b61SSam Leffler 		for (rix = 0, mask = sn->ratemask; mask != 0; rix++, mask >>= 1) {
764c1565b61SSam Leffler 			if ((mask & 1) == 0)		/* not a valid rate */
765c1565b61SSam Leffler 				continue;
766c1565b61SSam Leffler 			sn->stats[y][rix].successive_failures = 0;
767c1565b61SSam Leffler 			sn->stats[y][rix].tries = 0;
768c1565b61SSam Leffler 			sn->stats[y][rix].total_packets = 0;
769c1565b61SSam Leffler 			sn->stats[y][rix].packets_acked = 0;
770c1565b61SSam Leffler 			sn->stats[y][rix].last_tx = 0;
771b2763056SSam Leffler 
772c1565b61SSam Leffler 			sn->stats[y][rix].perfect_tx_time =
773c1565b61SSam Leffler 			    calc_usecs_unicast_packet(sc, size, rix, 0, 0);
774c1565b61SSam Leffler 			sn->stats[y][rix].average_tx_time =
775c1565b61SSam Leffler 			    sn->stats[y][rix].perfect_tx_time;
776b2763056SSam Leffler 		}
777b91bf513SSam Leffler 	}
778c1565b61SSam Leffler #if 0
779c1565b61SSam Leffler 	/* XXX 0, num_rates-1 are wrong */
780b032f27cSSam Leffler 	IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_RATECTL, ni,
781b032f27cSSam Leffler 	    "%s: %d rates %d%sMbps (%dus)- %d%sMbps (%dus)", __func__,
782b91bf513SSam Leffler 	    sn->num_rates,
783c1565b61SSam Leffler 	    DOT11RATE(0)/2, DOT11RATE(0) % 1 ? ".5" : "",
784b91bf513SSam Leffler 	    sn->stats[1][0].perfect_tx_time,
785c1565b61SSam Leffler 	    DOT11RATE(sn->num_rates-1)/2, DOT11RATE(sn->num_rates-1) % 1 ? ".5" : "",
786b91bf513SSam Leffler 	    sn->stats[1][sn->num_rates-1].perfect_tx_time
787b91bf513SSam Leffler 	);
788c1565b61SSam Leffler #endif
789b032f27cSSam Leffler 	/* set the visible bit-rate */
790c1565b61SSam Leffler 	if (sn->static_rix != -1)
791c1565b61SSam Leffler 		ni->ni_txrate = DOT11RATE(sn->static_rix);
792d0d425bfSSam Leffler 	else
793c1565b61SSam Leffler 		ni->ni_txrate = RATE(0);
794fa20c234SSam Leffler #undef RATE
795c1565b61SSam Leffler #undef DOT11RATE
796fa20c234SSam Leffler }
797fa20c234SSam Leffler 
798f0fd5e07SSam Leffler static void
799c1565b61SSam Leffler sample_stats(void *arg, struct ieee80211_node *ni)
800c1565b61SSam Leffler {
801c1565b61SSam Leffler 	struct ath_softc *sc = arg;
802c1565b61SSam Leffler 	const HAL_RATE_TABLE *rt = sc->sc_currates;
803c1565b61SSam Leffler 	struct sample_node *sn = ATH_NODE_SAMPLE(ATH_NODE(ni));
804c1565b61SSam Leffler 	uint32_t mask;
805c1565b61SSam Leffler 	int rix, y;
806c1565b61SSam Leffler 
807c1565b61SSam Leffler 	printf("\n[%s] refcnt %d static_rix %d ratemask 0x%x\n",
808c1565b61SSam Leffler 	    ether_sprintf(ni->ni_macaddr), ieee80211_node_refcnt(ni),
809c1565b61SSam Leffler 	    sn->static_rix, sn->ratemask);
810c1565b61SSam Leffler 	for (y = 0; y < NUM_PACKET_SIZE_BINS; y++) {
811c1565b61SSam Leffler 		printf("[%4u] cur rix %d since switch: packets %d ticks %u\n",
812c1565b61SSam Leffler 		    bin_to_size(y), sn->current_rix[y],
813c1565b61SSam Leffler 		    sn->packets_since_switch[y], sn->ticks_since_switch[y]);
814c1565b61SSam Leffler 		printf("[%4u] last sample %d cur sample %d packets sent %d\n",
815c1565b61SSam Leffler 		    bin_to_size(y), sn->last_sample_rix[y],
816c1565b61SSam Leffler 		    sn->current_sample_rix[y], sn->packets_sent[y]);
817c1565b61SSam Leffler 		printf("[%4u] packets since sample %d sample tt %u\n",
818c1565b61SSam Leffler 		    bin_to_size(y), sn->packets_since_sample[y],
819c1565b61SSam Leffler 		    sn->sample_tt[y]);
820c1565b61SSam Leffler 	}
821c1565b61SSam Leffler 	for (mask = sn->ratemask, rix = 0; mask != 0; mask >>= 1, rix++) {
822c1565b61SSam Leffler 		if ((mask & 1) == 0)
823c1565b61SSam Leffler 				continue;
824c1565b61SSam Leffler 		for (y = 0; y < NUM_PACKET_SIZE_BINS; y++) {
825c1565b61SSam Leffler 			if (sn->stats[y][rix].total_packets == 0)
826c1565b61SSam Leffler 				continue;
827c1565b61SSam Leffler 			printf("[%2u:%4u] %8d:%-8d (%3d%%) T %8d F %4d avg %5u last %u\n",
828*a6a308a4SAdrian Chadd 			    dot11rate(rt, rix),
829c1565b61SSam Leffler 			    bin_to_size(y),
830c1565b61SSam Leffler 			    sn->stats[y][rix].total_packets,
831c1565b61SSam Leffler 			    sn->stats[y][rix].packets_acked,
832c1565b61SSam Leffler 			    (100*sn->stats[y][rix].packets_acked)/sn->stats[y][rix].total_packets,
833c1565b61SSam Leffler 			    sn->stats[y][rix].tries,
834c1565b61SSam Leffler 			    sn->stats[y][rix].successive_failures,
835c1565b61SSam Leffler 			    sn->stats[y][rix].average_tx_time,
836c1565b61SSam Leffler 			    ticks - sn->stats[y][rix].last_tx);
837c1565b61SSam Leffler 		}
838c1565b61SSam Leffler 	}
839c1565b61SSam Leffler }
840c1565b61SSam Leffler 
841c1565b61SSam Leffler static int
842c1565b61SSam Leffler ath_rate_sysctl_stats(SYSCTL_HANDLER_ARGS)
843c1565b61SSam Leffler {
844c1565b61SSam Leffler 	struct ath_softc *sc = arg1;
845c1565b61SSam Leffler 	struct ifnet *ifp = sc->sc_ifp;
846c1565b61SSam Leffler 	struct ieee80211com *ic = ifp->if_l2com;
847c1565b61SSam Leffler 	int error, v;
848c1565b61SSam Leffler 
849c1565b61SSam Leffler 	v = 0;
850c1565b61SSam Leffler 	error = sysctl_handle_int(oidp, &v, 0, req);
851c1565b61SSam Leffler 	if (error || !req->newptr)
852c1565b61SSam Leffler 		return error;
853c1565b61SSam Leffler 	ieee80211_iterate_nodes(&ic->ic_sta, sample_stats, sc);
854c1565b61SSam Leffler 	return 0;
855c1565b61SSam Leffler }
856c1565b61SSam Leffler 
857c1565b61SSam Leffler static int
858c1565b61SSam Leffler ath_rate_sysctl_smoothing_rate(SYSCTL_HANDLER_ARGS)
859c1565b61SSam Leffler {
860c1565b61SSam Leffler 	struct sample_softc *ssc = arg1;
861c1565b61SSam Leffler 	int rate, error;
862c1565b61SSam Leffler 
863c1565b61SSam Leffler 	rate = ssc->smoothing_rate;
864c1565b61SSam Leffler 	error = sysctl_handle_int(oidp, &rate, 0, req);
865c1565b61SSam Leffler 	if (error || !req->newptr)
866c1565b61SSam Leffler 		return error;
867c1565b61SSam Leffler 	if (!(0 <= rate && rate < 100))
868c1565b61SSam Leffler 		return EINVAL;
869c1565b61SSam Leffler 	ssc->smoothing_rate = rate;
870c1565b61SSam Leffler 	ssc->smoothing_minpackets = 100 / (100 - rate);
871c1565b61SSam Leffler 	return 0;
872c1565b61SSam Leffler }
873c1565b61SSam Leffler 
874c1565b61SSam Leffler static int
875c1565b61SSam Leffler ath_rate_sysctl_sample_rate(SYSCTL_HANDLER_ARGS)
876c1565b61SSam Leffler {
877c1565b61SSam Leffler 	struct sample_softc *ssc = arg1;
878c1565b61SSam Leffler 	int rate, error;
879c1565b61SSam Leffler 
880c1565b61SSam Leffler 	rate = ssc->sample_rate;
881c1565b61SSam Leffler 	error = sysctl_handle_int(oidp, &rate, 0, req);
882c1565b61SSam Leffler 	if (error || !req->newptr)
883c1565b61SSam Leffler 		return error;
884c1565b61SSam Leffler 	if (!(2 <= rate && rate <= 100))
885c1565b61SSam Leffler 		return EINVAL;
886c1565b61SSam Leffler 	ssc->sample_rate = rate;
887c1565b61SSam Leffler 	return 0;
888c1565b61SSam Leffler }
889c1565b61SSam Leffler 
890c1565b61SSam Leffler static void
891c1565b61SSam Leffler ath_rate_sysctlattach(struct ath_softc *sc, struct sample_softc *ssc)
892fa20c234SSam Leffler {
893fa20c234SSam Leffler 	struct sysctl_ctx_list *ctx = device_get_sysctl_ctx(sc->sc_dev);
894fa20c234SSam Leffler 	struct sysctl_oid *tree = device_get_sysctl_tree(sc->sc_dev);
895fa20c234SSam Leffler 
896c1565b61SSam Leffler 	SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
897c1565b61SSam Leffler 	    "smoothing_rate", CTLTYPE_INT | CTLFLAG_RW, ssc, 0,
898c1565b61SSam Leffler 	    ath_rate_sysctl_smoothing_rate, "I",
899c1565b61SSam Leffler 	    "sample: smoothing rate for avg tx time (%%)");
900c1565b61SSam Leffler 	SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
901c1565b61SSam Leffler 	    "sample_rate", CTLTYPE_INT | CTLFLAG_RW, ssc, 0,
902c1565b61SSam Leffler 	    ath_rate_sysctl_sample_rate, "I",
903c1565b61SSam Leffler 	    "sample: percent air time devoted to sampling new rates (%%)");
904c1565b61SSam Leffler 	/* XXX max_successive_failures, stale_failure_timeout, min_switch */
905c1565b61SSam Leffler 	SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
906c1565b61SSam Leffler 	    "sample_stats", CTLTYPE_INT | CTLFLAG_RW, sc, 0,
907c1565b61SSam Leffler 	    ath_rate_sysctl_stats, "I", "sample: print statistics");
908fa20c234SSam Leffler }
909fa20c234SSam Leffler 
910fa20c234SSam Leffler struct ath_ratectrl *
911fa20c234SSam Leffler ath_rate_attach(struct ath_softc *sc)
912fa20c234SSam Leffler {
913c1565b61SSam Leffler 	struct sample_softc *ssc;
914fa20c234SSam Leffler 
915c1565b61SSam Leffler 	ssc = malloc(sizeof(struct sample_softc), M_DEVBUF, M_NOWAIT|M_ZERO);
916c1565b61SSam Leffler 	if (ssc == NULL)
917fa20c234SSam Leffler 		return NULL;
918c1565b61SSam Leffler 	ssc->arc.arc_space = sizeof(struct sample_node);
919c1565b61SSam Leffler 	ssc->smoothing_rate = 95;		/* ewma percentage ([0..99]) */
920c1565b61SSam Leffler 	ssc->smoothing_minpackets = 100 / (100 - ssc->smoothing_rate);
921c1565b61SSam Leffler 	ssc->sample_rate = 10;			/* %time to try diff tx rates */
922c1565b61SSam Leffler 	ssc->max_successive_failures = 3;	/* threshold for rate sampling*/
923c1565b61SSam Leffler 	ssc->stale_failure_timeout = 10 * hz;	/* 10 seconds */
924c1565b61SSam Leffler 	ssc->min_switch = hz;			/* 1 second */
925c1565b61SSam Leffler 	ath_rate_sysctlattach(sc, ssc);
926c1565b61SSam Leffler 	return &ssc->arc;
927fa20c234SSam Leffler }
928fa20c234SSam Leffler 
929fa20c234SSam Leffler void
930fa20c234SSam Leffler ath_rate_detach(struct ath_ratectrl *arc)
931fa20c234SSam Leffler {
932c1565b61SSam Leffler 	struct sample_softc *ssc = (struct sample_softc *) arc;
933fa20c234SSam Leffler 
934c1565b61SSam Leffler 	free(ssc, M_DEVBUF);
935fa20c234SSam Leffler }
936