1572ff6f6SMatthew Dillon /*-
2572ff6f6SMatthew Dillon * Copyright (c) 2005 John Bicket
3572ff6f6SMatthew Dillon * All rights reserved.
4572ff6f6SMatthew Dillon *
5572ff6f6SMatthew Dillon * Redistribution and use in source and binary forms, with or without
6572ff6f6SMatthew Dillon * modification, are permitted provided that the following conditions
7572ff6f6SMatthew Dillon * are met:
8572ff6f6SMatthew Dillon * 1. Redistributions of source code must retain the above copyright
9572ff6f6SMatthew Dillon * notice, this list of conditions and the following disclaimer,
10572ff6f6SMatthew Dillon * without modification.
11572ff6f6SMatthew Dillon * 2. Redistributions in binary form must reproduce at minimum a disclaimer
12572ff6f6SMatthew Dillon * similar to the "NO WARRANTY" disclaimer below ("Disclaimer") and any
13572ff6f6SMatthew Dillon * redistribution must be conditioned upon including a substantially
14572ff6f6SMatthew Dillon * similar Disclaimer requirement for further binary redistribution.
15572ff6f6SMatthew Dillon * 3. Neither the names of the above-listed copyright holders nor the names
16572ff6f6SMatthew Dillon * of any contributors may be used to endorse or promote products derived
17572ff6f6SMatthew Dillon * from this software without specific prior written permission.
18572ff6f6SMatthew Dillon *
19572ff6f6SMatthew Dillon * Alternatively, this software may be distributed under the terms of the
20572ff6f6SMatthew Dillon * GNU General Public License ("GPL") version 2 as published by the Free
21572ff6f6SMatthew Dillon * Software Foundation.
22572ff6f6SMatthew Dillon *
23572ff6f6SMatthew Dillon * NO WARRANTY
24572ff6f6SMatthew Dillon * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
25572ff6f6SMatthew Dillon * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
26*df052c2aSSascha Wildner * LIMITED TO, THE IMPLIED WARRANTIES OF NONINFRINGEMENT, MERCHANTABILITY
27572ff6f6SMatthew Dillon * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
28572ff6f6SMatthew Dillon * THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY,
29572ff6f6SMatthew Dillon * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
30572ff6f6SMatthew Dillon * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
31572ff6f6SMatthew Dillon * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
32572ff6f6SMatthew Dillon * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
33572ff6f6SMatthew Dillon * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
34572ff6f6SMatthew Dillon * THE POSSIBILITY OF SUCH DAMAGES.
35572ff6f6SMatthew Dillon *
36572ff6f6SMatthew Dillon */
37572ff6f6SMatthew Dillon
38572ff6f6SMatthew Dillon #include <sys/cdefs.h>
39848b370cSMatthew Dillon __FBSDID("$FreeBSD$");
40572ff6f6SMatthew Dillon
41572ff6f6SMatthew Dillon /*
42572ff6f6SMatthew Dillon * John Bicket's SampleRate control algorithm.
43572ff6f6SMatthew Dillon */
44572ff6f6SMatthew Dillon #include "opt_ath.h"
45572ff6f6SMatthew Dillon #include "opt_inet.h"
46572ff6f6SMatthew Dillon #include "opt_wlan.h"
47572ff6f6SMatthew Dillon #include "opt_ah.h"
48572ff6f6SMatthew Dillon
49572ff6f6SMatthew Dillon #include <sys/param.h>
50572ff6f6SMatthew Dillon #include <sys/systm.h>
51572ff6f6SMatthew Dillon #include <sys/sysctl.h>
52572ff6f6SMatthew Dillon #include <sys/kernel.h>
53572ff6f6SMatthew Dillon #include <sys/lock.h>
54572ff6f6SMatthew Dillon #include <sys/malloc.h>
55572ff6f6SMatthew Dillon #include <sys/errno.h>
56848b370cSMatthew Dillon
57dc249793SMatthew Dillon #if defined(__DragonFly__)
58dc249793SMatthew Dillon /* empty */
59dc249793SMatthew Dillon #else
60b14ca477SMatthew Dillon #include <machine/bus.h>
61b14ca477SMatthew Dillon #include <machine/resource.h>
62dc249793SMatthew Dillon #endif
63572ff6f6SMatthew Dillon #include <sys/bus.h>
64848b370cSMatthew Dillon
65572ff6f6SMatthew Dillon #include <sys/socket.h>
66572ff6f6SMatthew Dillon
67572ff6f6SMatthew Dillon #include <net/if.h>
68572ff6f6SMatthew Dillon #include <net/if_var.h>
69572ff6f6SMatthew Dillon #include <net/if_media.h>
70572ff6f6SMatthew Dillon #include <net/if_arp.h>
71572ff6f6SMatthew Dillon #include <net/ethernet.h> /* XXX for ether_sprintf */
72572ff6f6SMatthew Dillon
73dc249793SMatthew Dillon #include <netproto/802_11/ieee80211_var.h>
74572ff6f6SMatthew Dillon
75572ff6f6SMatthew Dillon #include <net/bpf.h>
76572ff6f6SMatthew Dillon
77572ff6f6SMatthew Dillon #ifdef INET
78572ff6f6SMatthew Dillon #include <netinet/in.h>
79572ff6f6SMatthew Dillon #include <netinet/if_ether.h>
80572ff6f6SMatthew Dillon #endif
81572ff6f6SMatthew Dillon
82dc249793SMatthew Dillon #include <dev/netif/ath/ath/if_athvar.h>
83dc249793SMatthew Dillon #include <dev/netif/ath/ath_rate/sample/sample.h>
84dc249793SMatthew Dillon #include <dev/netif/ath/ath_hal/ah_desc.h>
85dc249793SMatthew Dillon #include <dev/netif/ath/ath_rate/sample/tx_schedules.h>
86dc249793SMatthew Dillon
87dc249793SMatthew Dillon #if defined(__DragonFly__)
88dc249793SMatthew Dillon extern const char* ath_hal_ether_sprintf(const uint8_t *mac);
89dc249793SMatthew Dillon #endif
90f92fae3fSSascha Wildner
91572ff6f6SMatthew Dillon /*
92572ff6f6SMatthew Dillon * This file is an implementation of the SampleRate algorithm
93572ff6f6SMatthew Dillon * in "Bit-rate Selection in Wireless Networks"
94572ff6f6SMatthew Dillon * (http://www.pdos.lcs.mit.edu/papers/jbicket-ms.ps)
95572ff6f6SMatthew Dillon *
96572ff6f6SMatthew Dillon * SampleRate chooses the bit-rate it predicts will provide the most
97572ff6f6SMatthew Dillon * throughput based on estimates of the expected per-packet
98572ff6f6SMatthew Dillon * transmission time for each bit-rate. SampleRate periodically sends
99572ff6f6SMatthew Dillon * packets at bit-rates other than the current one to estimate when
100572ff6f6SMatthew Dillon * another bit-rate will provide better performance. SampleRate
101572ff6f6SMatthew Dillon * switches to another bit-rate when its estimated per-packet
102572ff6f6SMatthew Dillon * transmission time becomes smaller than the current bit-rate's.
103572ff6f6SMatthew Dillon * SampleRate reduces the number of bit-rates it must sample by
104572ff6f6SMatthew Dillon * eliminating those that could not perform better than the one
105572ff6f6SMatthew Dillon * currently being used. SampleRate also stops probing at a bit-rate
106572ff6f6SMatthew Dillon * if it experiences several successive losses.
107572ff6f6SMatthew Dillon *
108572ff6f6SMatthew Dillon * The difference between the algorithm in the thesis and the one in this
109572ff6f6SMatthew Dillon * file is that the one in this file uses a ewma instead of a window.
110572ff6f6SMatthew Dillon *
111572ff6f6SMatthew Dillon * Also, this implementation tracks the average transmission time for
112572ff6f6SMatthew Dillon * a few different packet sizes independently for each link.
113572ff6f6SMatthew Dillon */
114572ff6f6SMatthew Dillon
115572ff6f6SMatthew Dillon static void ath_rate_ctl_reset(struct ath_softc *, struct ieee80211_node *);
116572ff6f6SMatthew Dillon
117572ff6f6SMatthew Dillon static __inline int
size_to_bin(int size)118572ff6f6SMatthew Dillon size_to_bin(int size)
119572ff6f6SMatthew Dillon {
120572ff6f6SMatthew Dillon #if NUM_PACKET_SIZE_BINS > 1
121572ff6f6SMatthew Dillon if (size <= packet_size_bins[0])
122572ff6f6SMatthew Dillon return 0;
123572ff6f6SMatthew Dillon #endif
124572ff6f6SMatthew Dillon #if NUM_PACKET_SIZE_BINS > 2
125572ff6f6SMatthew Dillon if (size <= packet_size_bins[1])
126572ff6f6SMatthew Dillon return 1;
127572ff6f6SMatthew Dillon #endif
128572ff6f6SMatthew Dillon #if NUM_PACKET_SIZE_BINS > 3
129572ff6f6SMatthew Dillon if (size <= packet_size_bins[2])
130572ff6f6SMatthew Dillon return 2;
131572ff6f6SMatthew Dillon #endif
132572ff6f6SMatthew Dillon #if NUM_PACKET_SIZE_BINS > 4
133572ff6f6SMatthew Dillon #error "add support for more packet sizes"
134572ff6f6SMatthew Dillon #endif
135572ff6f6SMatthew Dillon return NUM_PACKET_SIZE_BINS-1;
136572ff6f6SMatthew Dillon }
137572ff6f6SMatthew Dillon
138572ff6f6SMatthew Dillon void
ath_rate_node_init(struct ath_softc * sc,struct ath_node * an)139572ff6f6SMatthew Dillon ath_rate_node_init(struct ath_softc *sc, struct ath_node *an)
140572ff6f6SMatthew Dillon {
141572ff6f6SMatthew Dillon /* NB: assumed to be zero'd by caller */
142572ff6f6SMatthew Dillon }
143572ff6f6SMatthew Dillon
144572ff6f6SMatthew Dillon void
ath_rate_node_cleanup(struct ath_softc * sc,struct ath_node * an)145572ff6f6SMatthew Dillon ath_rate_node_cleanup(struct ath_softc *sc, struct ath_node *an)
146572ff6f6SMatthew Dillon {
147572ff6f6SMatthew Dillon }
148572ff6f6SMatthew Dillon
149572ff6f6SMatthew Dillon static int
dot11rate(const HAL_RATE_TABLE * rt,int rix)150572ff6f6SMatthew Dillon dot11rate(const HAL_RATE_TABLE *rt, int rix)
151572ff6f6SMatthew Dillon {
152572ff6f6SMatthew Dillon if (rix < 0)
153572ff6f6SMatthew Dillon return -1;
154572ff6f6SMatthew Dillon return rt->info[rix].phy == IEEE80211_T_HT ?
155572ff6f6SMatthew Dillon rt->info[rix].dot11Rate : (rt->info[rix].dot11Rate & IEEE80211_RATE_VAL) / 2;
156572ff6f6SMatthew Dillon }
157572ff6f6SMatthew Dillon
158572ff6f6SMatthew Dillon static const char *
dot11rate_label(const HAL_RATE_TABLE * rt,int rix)159572ff6f6SMatthew Dillon dot11rate_label(const HAL_RATE_TABLE *rt, int rix)
160572ff6f6SMatthew Dillon {
161572ff6f6SMatthew Dillon if (rix < 0)
162572ff6f6SMatthew Dillon return "";
163572ff6f6SMatthew Dillon return rt->info[rix].phy == IEEE80211_T_HT ? "MCS" : "Mb ";
164572ff6f6SMatthew Dillon }
165572ff6f6SMatthew Dillon
166572ff6f6SMatthew Dillon /*
167572ff6f6SMatthew Dillon * Return the rix with the lowest average_tx_time,
168572ff6f6SMatthew Dillon * or -1 if all the average_tx_times are 0.
169572ff6f6SMatthew Dillon */
170572ff6f6SMatthew Dillon static __inline int
pick_best_rate(struct ath_node * an,const HAL_RATE_TABLE * rt,int size_bin,int require_acked_before)171572ff6f6SMatthew Dillon pick_best_rate(struct ath_node *an, const HAL_RATE_TABLE *rt,
172572ff6f6SMatthew Dillon int size_bin, int require_acked_before)
173572ff6f6SMatthew Dillon {
174572ff6f6SMatthew Dillon struct sample_node *sn = ATH_NODE_SAMPLE(an);
175572ff6f6SMatthew Dillon int best_rate_rix, best_rate_tt, best_rate_pct;
176572ff6f6SMatthew Dillon uint64_t mask;
177572ff6f6SMatthew Dillon int rix, tt, pct;
178572ff6f6SMatthew Dillon
179572ff6f6SMatthew Dillon best_rate_rix = 0;
180572ff6f6SMatthew Dillon best_rate_tt = 0;
181572ff6f6SMatthew Dillon best_rate_pct = 0;
182572ff6f6SMatthew Dillon for (mask = sn->ratemask, rix = 0; mask != 0; mask >>= 1, rix++) {
183572ff6f6SMatthew Dillon if ((mask & 1) == 0) /* not a supported rate */
184572ff6f6SMatthew Dillon continue;
185572ff6f6SMatthew Dillon
186572ff6f6SMatthew Dillon /* Don't pick a non-HT rate for a HT node */
187572ff6f6SMatthew Dillon if ((an->an_node.ni_flags & IEEE80211_NODE_HT) &&
188572ff6f6SMatthew Dillon (rt->info[rix].phy != IEEE80211_T_HT)) {
189572ff6f6SMatthew Dillon continue;
190572ff6f6SMatthew Dillon }
191572ff6f6SMatthew Dillon
192572ff6f6SMatthew Dillon tt = sn->stats[size_bin][rix].average_tx_time;
193572ff6f6SMatthew Dillon if (tt <= 0 ||
194572ff6f6SMatthew Dillon (require_acked_before &&
195572ff6f6SMatthew Dillon !sn->stats[size_bin][rix].packets_acked))
196572ff6f6SMatthew Dillon continue;
197572ff6f6SMatthew Dillon
198572ff6f6SMatthew Dillon /* Calculate percentage if possible */
199572ff6f6SMatthew Dillon if (sn->stats[size_bin][rix].total_packets > 0) {
200572ff6f6SMatthew Dillon pct = sn->stats[size_bin][rix].ewma_pct;
201572ff6f6SMatthew Dillon } else {
202572ff6f6SMatthew Dillon /* XXX for now, assume 95% ok */
203572ff6f6SMatthew Dillon pct = 95;
204572ff6f6SMatthew Dillon }
205572ff6f6SMatthew Dillon
206572ff6f6SMatthew Dillon /* don't use a bit-rate that has been failing */
207572ff6f6SMatthew Dillon if (sn->stats[size_bin][rix].successive_failures > 3)
208572ff6f6SMatthew Dillon continue;
209572ff6f6SMatthew Dillon
210572ff6f6SMatthew Dillon /*
211572ff6f6SMatthew Dillon * For HT, Don't use a bit rate that is much more
212572ff6f6SMatthew Dillon * lossy than the best.
213572ff6f6SMatthew Dillon *
214572ff6f6SMatthew Dillon * XXX this isn't optimal; it's just designed to
215572ff6f6SMatthew Dillon * eliminate rates that are going to be obviously
216572ff6f6SMatthew Dillon * worse.
217572ff6f6SMatthew Dillon */
218572ff6f6SMatthew Dillon if (an->an_node.ni_flags & IEEE80211_NODE_HT) {
219572ff6f6SMatthew Dillon if (best_rate_pct > (pct + 50))
220572ff6f6SMatthew Dillon continue;
221572ff6f6SMatthew Dillon }
222572ff6f6SMatthew Dillon
223572ff6f6SMatthew Dillon /*
224572ff6f6SMatthew Dillon * For non-MCS rates, use the current average txtime for
225572ff6f6SMatthew Dillon * comparison.
226572ff6f6SMatthew Dillon */
227572ff6f6SMatthew Dillon if (! (an->an_node.ni_flags & IEEE80211_NODE_HT)) {
228572ff6f6SMatthew Dillon if (best_rate_tt == 0 || tt <= best_rate_tt) {
229572ff6f6SMatthew Dillon best_rate_tt = tt;
230572ff6f6SMatthew Dillon best_rate_rix = rix;
231572ff6f6SMatthew Dillon best_rate_pct = pct;
232572ff6f6SMatthew Dillon }
233572ff6f6SMatthew Dillon }
234572ff6f6SMatthew Dillon
235572ff6f6SMatthew Dillon /*
236572ff6f6SMatthew Dillon * Since 2 stream rates have slightly higher TX times,
237572ff6f6SMatthew Dillon * allow a little bit of leeway. This should later
238572ff6f6SMatthew Dillon * be abstracted out and properly handled.
239572ff6f6SMatthew Dillon */
240572ff6f6SMatthew Dillon if (an->an_node.ni_flags & IEEE80211_NODE_HT) {
241572ff6f6SMatthew Dillon if (best_rate_tt == 0 || (tt * 8 <= best_rate_tt * 10)) {
242572ff6f6SMatthew Dillon best_rate_tt = tt;
243572ff6f6SMatthew Dillon best_rate_rix = rix;
244572ff6f6SMatthew Dillon best_rate_pct = pct;
245572ff6f6SMatthew Dillon }
246572ff6f6SMatthew Dillon }
247572ff6f6SMatthew Dillon }
248572ff6f6SMatthew Dillon return (best_rate_tt ? best_rate_rix : -1);
249572ff6f6SMatthew Dillon }
250572ff6f6SMatthew Dillon
251572ff6f6SMatthew Dillon /*
252572ff6f6SMatthew Dillon * Pick a good "random" bit-rate to sample other than the current one.
253572ff6f6SMatthew Dillon */
254572ff6f6SMatthew Dillon static __inline int
pick_sample_rate(struct sample_softc * ssc,struct ath_node * an,const HAL_RATE_TABLE * rt,int size_bin)255572ff6f6SMatthew Dillon pick_sample_rate(struct sample_softc *ssc , struct ath_node *an,
256572ff6f6SMatthew Dillon const HAL_RATE_TABLE *rt, int size_bin)
257572ff6f6SMatthew Dillon {
258572ff6f6SMatthew Dillon #define DOT11RATE(ix) (rt->info[ix].dot11Rate & IEEE80211_RATE_VAL)
259572ff6f6SMatthew Dillon #define MCS(ix) (rt->info[ix].dot11Rate | IEEE80211_RATE_MCS)
260572ff6f6SMatthew Dillon struct sample_node *sn = ATH_NODE_SAMPLE(an);
261572ff6f6SMatthew Dillon int current_rix, rix;
262572ff6f6SMatthew Dillon unsigned current_tt;
263572ff6f6SMatthew Dillon uint64_t mask;
264572ff6f6SMatthew Dillon
265572ff6f6SMatthew Dillon current_rix = sn->current_rix[size_bin];
266572ff6f6SMatthew Dillon if (current_rix < 0) {
267572ff6f6SMatthew Dillon /* no successes yet, send at the lowest bit-rate */
268572ff6f6SMatthew Dillon /* XXX should return MCS0 if HT */
269572ff6f6SMatthew Dillon return 0;
270572ff6f6SMatthew Dillon }
271572ff6f6SMatthew Dillon
272572ff6f6SMatthew Dillon current_tt = sn->stats[size_bin][current_rix].average_tx_time;
273572ff6f6SMatthew Dillon
274572ff6f6SMatthew Dillon rix = sn->last_sample_rix[size_bin]+1; /* next sample rate */
275572ff6f6SMatthew Dillon mask = sn->ratemask &~ ((uint64_t) 1<<current_rix);/* don't sample current rate */
276572ff6f6SMatthew Dillon while (mask != 0) {
277572ff6f6SMatthew Dillon if ((mask & ((uint64_t) 1<<rix)) == 0) { /* not a supported rate */
278572ff6f6SMatthew Dillon nextrate:
279572ff6f6SMatthew Dillon if (++rix >= rt->rateCount)
280572ff6f6SMatthew Dillon rix = 0;
281572ff6f6SMatthew Dillon continue;
282572ff6f6SMatthew Dillon }
283572ff6f6SMatthew Dillon
284572ff6f6SMatthew Dillon /*
285572ff6f6SMatthew Dillon * The following code stops trying to sample
286572ff6f6SMatthew Dillon * non-MCS rates when speaking to an MCS node.
287572ff6f6SMatthew Dillon * However, at least for CCK rates in 2.4GHz mode,
288572ff6f6SMatthew Dillon * the non-MCS rates MAY actually provide better
289572ff6f6SMatthew Dillon * PER at the very far edge of reception.
290572ff6f6SMatthew Dillon *
291572ff6f6SMatthew Dillon * However! Until ath_rate_form_aggr() grows
292572ff6f6SMatthew Dillon * some logic to not form aggregates if the
293572ff6f6SMatthew Dillon * selected rate is non-MCS, this won't work.
294572ff6f6SMatthew Dillon *
295572ff6f6SMatthew Dillon * So don't disable this code until you've taught
296572ff6f6SMatthew Dillon * ath_rate_form_aggr() to drop out if any of
297572ff6f6SMatthew Dillon * the selected rates are non-MCS.
298572ff6f6SMatthew Dillon */
299572ff6f6SMatthew Dillon #if 1
300572ff6f6SMatthew Dillon /* if the node is HT and the rate isn't HT, don't bother sample */
301572ff6f6SMatthew Dillon if ((an->an_node.ni_flags & IEEE80211_NODE_HT) &&
302572ff6f6SMatthew Dillon (rt->info[rix].phy != IEEE80211_T_HT)) {
303572ff6f6SMatthew Dillon mask &= ~((uint64_t) 1<<rix);
304572ff6f6SMatthew Dillon goto nextrate;
305572ff6f6SMatthew Dillon }
306572ff6f6SMatthew Dillon #endif
307572ff6f6SMatthew Dillon
308572ff6f6SMatthew Dillon /* this bit-rate is always worse than the current one */
309572ff6f6SMatthew Dillon if (sn->stats[size_bin][rix].perfect_tx_time > current_tt) {
310572ff6f6SMatthew Dillon mask &= ~((uint64_t) 1<<rix);
311572ff6f6SMatthew Dillon goto nextrate;
312572ff6f6SMatthew Dillon }
313572ff6f6SMatthew Dillon
314572ff6f6SMatthew Dillon /* rarely sample bit-rates that fail a lot */
315572ff6f6SMatthew Dillon if (sn->stats[size_bin][rix].successive_failures > ssc->max_successive_failures &&
316572ff6f6SMatthew Dillon ticks - sn->stats[size_bin][rix].last_tx < ssc->stale_failure_timeout) {
317572ff6f6SMatthew Dillon mask &= ~((uint64_t) 1<<rix);
318572ff6f6SMatthew Dillon goto nextrate;
319572ff6f6SMatthew Dillon }
320572ff6f6SMatthew Dillon
321572ff6f6SMatthew Dillon /*
322572ff6f6SMatthew Dillon * For HT, only sample a few rates on either side of the
323572ff6f6SMatthew Dillon * current rix; there's quite likely a lot of them.
324572ff6f6SMatthew Dillon */
325572ff6f6SMatthew Dillon if (an->an_node.ni_flags & IEEE80211_NODE_HT) {
326572ff6f6SMatthew Dillon if (rix < (current_rix - 3) ||
327572ff6f6SMatthew Dillon rix > (current_rix + 3)) {
328572ff6f6SMatthew Dillon mask &= ~((uint64_t) 1<<rix);
329572ff6f6SMatthew Dillon goto nextrate;
330572ff6f6SMatthew Dillon }
331572ff6f6SMatthew Dillon }
332572ff6f6SMatthew Dillon
333572ff6f6SMatthew Dillon /* Don't sample more than 2 rates higher for rates > 11M for non-HT rates */
334572ff6f6SMatthew Dillon if (! (an->an_node.ni_flags & IEEE80211_NODE_HT)) {
335572ff6f6SMatthew Dillon if (DOT11RATE(rix) > 2*11 && rix > current_rix + 2) {
336572ff6f6SMatthew Dillon mask &= ~((uint64_t) 1<<rix);
337572ff6f6SMatthew Dillon goto nextrate;
338572ff6f6SMatthew Dillon }
339572ff6f6SMatthew Dillon }
340572ff6f6SMatthew Dillon
341572ff6f6SMatthew Dillon sn->last_sample_rix[size_bin] = rix;
342572ff6f6SMatthew Dillon return rix;
343572ff6f6SMatthew Dillon }
344572ff6f6SMatthew Dillon return current_rix;
345572ff6f6SMatthew Dillon #undef DOT11RATE
346572ff6f6SMatthew Dillon #undef MCS
347572ff6f6SMatthew Dillon }
348572ff6f6SMatthew Dillon
349572ff6f6SMatthew Dillon static int
ath_rate_get_static_rix(struct ath_softc * sc,const struct ieee80211_node * ni)350572ff6f6SMatthew Dillon ath_rate_get_static_rix(struct ath_softc *sc, const struct ieee80211_node *ni)
351572ff6f6SMatthew Dillon {
352572ff6f6SMatthew Dillon #define RATE(_ix) (ni->ni_rates.rs_rates[(_ix)] & IEEE80211_RATE_VAL)
353572ff6f6SMatthew Dillon #define DOT11RATE(_ix) (rt->info[(_ix)].dot11Rate & IEEE80211_RATE_VAL)
354572ff6f6SMatthew Dillon #define MCS(_ix) (ni->ni_htrates.rs_rates[_ix] | IEEE80211_RATE_MCS)
355572ff6f6SMatthew Dillon const struct ieee80211_txparam *tp = ni->ni_txparms;
356572ff6f6SMatthew Dillon int srate;
357572ff6f6SMatthew Dillon
358572ff6f6SMatthew Dillon /* Check MCS rates */
359572ff6f6SMatthew Dillon for (srate = ni->ni_htrates.rs_nrates - 1; srate >= 0; srate--) {
360572ff6f6SMatthew Dillon if (MCS(srate) == tp->ucastrate)
361572ff6f6SMatthew Dillon return sc->sc_rixmap[tp->ucastrate];
362572ff6f6SMatthew Dillon }
363572ff6f6SMatthew Dillon
364572ff6f6SMatthew Dillon /* Check legacy rates */
365572ff6f6SMatthew Dillon for (srate = ni->ni_rates.rs_nrates - 1; srate >= 0; srate--) {
366572ff6f6SMatthew Dillon if (RATE(srate) == tp->ucastrate)
367572ff6f6SMatthew Dillon return sc->sc_rixmap[tp->ucastrate];
368572ff6f6SMatthew Dillon }
369572ff6f6SMatthew Dillon return -1;
370572ff6f6SMatthew Dillon #undef RATE
371572ff6f6SMatthew Dillon #undef DOT11RATE
372572ff6f6SMatthew Dillon #undef MCS
373572ff6f6SMatthew Dillon }
374572ff6f6SMatthew Dillon
375572ff6f6SMatthew Dillon static void
ath_rate_update_static_rix(struct ath_softc * sc,struct ieee80211_node * ni)376572ff6f6SMatthew Dillon ath_rate_update_static_rix(struct ath_softc *sc, struct ieee80211_node *ni)
377572ff6f6SMatthew Dillon {
378572ff6f6SMatthew Dillon struct ath_node *an = ATH_NODE(ni);
379572ff6f6SMatthew Dillon const struct ieee80211_txparam *tp = ni->ni_txparms;
380572ff6f6SMatthew Dillon struct sample_node *sn = ATH_NODE_SAMPLE(an);
381572ff6f6SMatthew Dillon
382572ff6f6SMatthew Dillon if (tp != NULL && tp->ucastrate != IEEE80211_FIXED_RATE_NONE) {
383572ff6f6SMatthew Dillon /*
384572ff6f6SMatthew Dillon * A fixed rate is to be used; ucastrate is the IEEE code
385572ff6f6SMatthew Dillon * for this rate (sans basic bit). Check this against the
386572ff6f6SMatthew Dillon * negotiated rate set for the node. Note the fixed rate
387572ff6f6SMatthew Dillon * may not be available for various reasons so we only
388572ff6f6SMatthew Dillon * setup the static rate index if the lookup is successful.
389572ff6f6SMatthew Dillon */
390572ff6f6SMatthew Dillon sn->static_rix = ath_rate_get_static_rix(sc, ni);
391572ff6f6SMatthew Dillon } else {
392572ff6f6SMatthew Dillon sn->static_rix = -1;
393572ff6f6SMatthew Dillon }
394572ff6f6SMatthew Dillon }
395572ff6f6SMatthew Dillon
396572ff6f6SMatthew Dillon /*
397572ff6f6SMatthew Dillon * Pick a non-HT rate to begin using.
398572ff6f6SMatthew Dillon */
399572ff6f6SMatthew Dillon static int
ath_rate_pick_seed_rate_legacy(struct ath_softc * sc,struct ath_node * an,int frameLen)400572ff6f6SMatthew Dillon ath_rate_pick_seed_rate_legacy(struct ath_softc *sc, struct ath_node *an,
401572ff6f6SMatthew Dillon int frameLen)
402572ff6f6SMatthew Dillon {
403572ff6f6SMatthew Dillon #define DOT11RATE(ix) (rt->info[ix].dot11Rate & IEEE80211_RATE_VAL)
404572ff6f6SMatthew Dillon #define MCS(ix) (rt->info[ix].dot11Rate | IEEE80211_RATE_MCS)
405572ff6f6SMatthew Dillon #define RATE(ix) (DOT11RATE(ix) / 2)
406572ff6f6SMatthew Dillon int rix = -1;
407572ff6f6SMatthew Dillon const HAL_RATE_TABLE *rt = sc->sc_currates;
408572ff6f6SMatthew Dillon struct sample_node *sn = ATH_NODE_SAMPLE(an);
409572ff6f6SMatthew Dillon const int size_bin = size_to_bin(frameLen);
410572ff6f6SMatthew Dillon
411572ff6f6SMatthew Dillon /* no packet has been sent successfully yet */
412572ff6f6SMatthew Dillon for (rix = rt->rateCount-1; rix > 0; rix--) {
413572ff6f6SMatthew Dillon if ((sn->ratemask & ((uint64_t) 1<<rix)) == 0)
414572ff6f6SMatthew Dillon continue;
415572ff6f6SMatthew Dillon
416572ff6f6SMatthew Dillon /* Skip HT rates */
417572ff6f6SMatthew Dillon if (rt->info[rix].phy == IEEE80211_T_HT)
418572ff6f6SMatthew Dillon continue;
419572ff6f6SMatthew Dillon
420572ff6f6SMatthew Dillon /*
421572ff6f6SMatthew Dillon * Pick the highest rate <= 36 Mbps
422572ff6f6SMatthew Dillon * that hasn't failed.
423572ff6f6SMatthew Dillon */
424572ff6f6SMatthew Dillon if (DOT11RATE(rix) <= 72 &&
425572ff6f6SMatthew Dillon sn->stats[size_bin][rix].successive_failures == 0) {
426572ff6f6SMatthew Dillon break;
427572ff6f6SMatthew Dillon }
428572ff6f6SMatthew Dillon }
429572ff6f6SMatthew Dillon return rix;
430572ff6f6SMatthew Dillon #undef RATE
431572ff6f6SMatthew Dillon #undef MCS
432572ff6f6SMatthew Dillon #undef DOT11RATE
433572ff6f6SMatthew Dillon }
434572ff6f6SMatthew Dillon
435572ff6f6SMatthew Dillon /*
436572ff6f6SMatthew Dillon * Pick a HT rate to begin using.
437572ff6f6SMatthew Dillon *
438572ff6f6SMatthew Dillon * Don't use any non-HT rates; only consider HT rates.
439572ff6f6SMatthew Dillon */
440572ff6f6SMatthew Dillon static int
ath_rate_pick_seed_rate_ht(struct ath_softc * sc,struct ath_node * an,int frameLen)441572ff6f6SMatthew Dillon ath_rate_pick_seed_rate_ht(struct ath_softc *sc, struct ath_node *an,
442572ff6f6SMatthew Dillon int frameLen)
443572ff6f6SMatthew Dillon {
444572ff6f6SMatthew Dillon #define DOT11RATE(ix) (rt->info[ix].dot11Rate & IEEE80211_RATE_VAL)
445572ff6f6SMatthew Dillon #define MCS(ix) (rt->info[ix].dot11Rate | IEEE80211_RATE_MCS)
446572ff6f6SMatthew Dillon #define RATE(ix) (DOT11RATE(ix) / 2)
447572ff6f6SMatthew Dillon int rix = -1, ht_rix = -1;
448572ff6f6SMatthew Dillon const HAL_RATE_TABLE *rt = sc->sc_currates;
449572ff6f6SMatthew Dillon struct sample_node *sn = ATH_NODE_SAMPLE(an);
450572ff6f6SMatthew Dillon const int size_bin = size_to_bin(frameLen);
451572ff6f6SMatthew Dillon
452572ff6f6SMatthew Dillon /* no packet has been sent successfully yet */
453572ff6f6SMatthew Dillon for (rix = rt->rateCount-1; rix > 0; rix--) {
454572ff6f6SMatthew Dillon /* Skip rates we can't use */
455572ff6f6SMatthew Dillon if ((sn->ratemask & ((uint64_t) 1<<rix)) == 0)
456572ff6f6SMatthew Dillon continue;
457572ff6f6SMatthew Dillon
458572ff6f6SMatthew Dillon /* Keep a copy of the last seen HT rate index */
459572ff6f6SMatthew Dillon if (rt->info[rix].phy == IEEE80211_T_HT)
460572ff6f6SMatthew Dillon ht_rix = rix;
461572ff6f6SMatthew Dillon
462572ff6f6SMatthew Dillon /* Skip non-HT rates */
463572ff6f6SMatthew Dillon if (rt->info[rix].phy != IEEE80211_T_HT)
464572ff6f6SMatthew Dillon continue;
465572ff6f6SMatthew Dillon
466572ff6f6SMatthew Dillon /*
467572ff6f6SMatthew Dillon * Pick a medium-speed rate regardless of stream count
468572ff6f6SMatthew Dillon * which has not seen any failures. Higher rates may fail;
469572ff6f6SMatthew Dillon * we'll try them later.
470572ff6f6SMatthew Dillon */
471572ff6f6SMatthew Dillon if (((MCS(rix) & 0x7) <= 4) &&
472572ff6f6SMatthew Dillon sn->stats[size_bin][rix].successive_failures == 0) {
473572ff6f6SMatthew Dillon break;
474572ff6f6SMatthew Dillon }
475572ff6f6SMatthew Dillon }
476572ff6f6SMatthew Dillon
477572ff6f6SMatthew Dillon /*
478572ff6f6SMatthew Dillon * If all the MCS rates have successive failures, rix should be
479572ff6f6SMatthew Dillon * > 0; otherwise use the lowest MCS rix (hopefully MCS 0.)
480572ff6f6SMatthew Dillon */
481572ff6f6SMatthew Dillon return MAX(rix, ht_rix);
482572ff6f6SMatthew Dillon #undef RATE
483572ff6f6SMatthew Dillon #undef MCS
484572ff6f6SMatthew Dillon #undef DOT11RATE
485572ff6f6SMatthew Dillon }
486572ff6f6SMatthew Dillon
487572ff6f6SMatthew Dillon
488572ff6f6SMatthew Dillon void
ath_rate_findrate(struct ath_softc * sc,struct ath_node * an,int shortPreamble,size_t frameLen,u_int8_t * rix0,int * try0,u_int8_t * txrate)489572ff6f6SMatthew Dillon ath_rate_findrate(struct ath_softc *sc, struct ath_node *an,
490572ff6f6SMatthew Dillon int shortPreamble, size_t frameLen,
491572ff6f6SMatthew Dillon u_int8_t *rix0, int *try0, u_int8_t *txrate)
492572ff6f6SMatthew Dillon {
493572ff6f6SMatthew Dillon #define DOT11RATE(ix) (rt->info[ix].dot11Rate & IEEE80211_RATE_VAL)
494572ff6f6SMatthew Dillon #define MCS(ix) (rt->info[ix].dot11Rate | IEEE80211_RATE_MCS)
495572ff6f6SMatthew Dillon #define RATE(ix) (DOT11RATE(ix) / 2)
496572ff6f6SMatthew Dillon struct sample_node *sn = ATH_NODE_SAMPLE(an);
497572ff6f6SMatthew Dillon struct sample_softc *ssc = ATH_SOFTC_SAMPLE(sc);
498b14ca477SMatthew Dillon struct ieee80211com *ic = &sc->sc_ic;
499572ff6f6SMatthew Dillon const HAL_RATE_TABLE *rt = sc->sc_currates;
500572ff6f6SMatthew Dillon const int size_bin = size_to_bin(frameLen);
501572ff6f6SMatthew Dillon int rix, mrr, best_rix, change_rates;
502572ff6f6SMatthew Dillon unsigned average_tx_time;
503572ff6f6SMatthew Dillon
504572ff6f6SMatthew Dillon ath_rate_update_static_rix(sc, &an->an_node);
505572ff6f6SMatthew Dillon
506572ff6f6SMatthew Dillon if (sn->currates != sc->sc_currates) {
507572ff6f6SMatthew Dillon device_printf(sc->sc_dev, "%s: currates != sc_currates!\n",
508572ff6f6SMatthew Dillon __func__);
509572ff6f6SMatthew Dillon rix = 0;
510572ff6f6SMatthew Dillon *try0 = ATH_TXMAXTRY;
511572ff6f6SMatthew Dillon goto done;
512572ff6f6SMatthew Dillon }
513572ff6f6SMatthew Dillon
514572ff6f6SMatthew Dillon if (sn->static_rix != -1) {
515572ff6f6SMatthew Dillon rix = sn->static_rix;
516572ff6f6SMatthew Dillon *try0 = ATH_TXMAXTRY;
517572ff6f6SMatthew Dillon goto done;
518572ff6f6SMatthew Dillon }
519572ff6f6SMatthew Dillon
520572ff6f6SMatthew Dillon mrr = sc->sc_mrretry;
521572ff6f6SMatthew Dillon /* XXX check HT protmode too */
522572ff6f6SMatthew Dillon if (mrr && (ic->ic_flags & IEEE80211_F_USEPROT && !sc->sc_mrrprot))
523572ff6f6SMatthew Dillon mrr = 0;
524572ff6f6SMatthew Dillon
525572ff6f6SMatthew Dillon best_rix = pick_best_rate(an, rt, size_bin, !mrr);
526572ff6f6SMatthew Dillon if (best_rix >= 0) {
527572ff6f6SMatthew Dillon average_tx_time = sn->stats[size_bin][best_rix].average_tx_time;
528572ff6f6SMatthew Dillon } else {
529572ff6f6SMatthew Dillon average_tx_time = 0;
530572ff6f6SMatthew Dillon }
531572ff6f6SMatthew Dillon /*
532572ff6f6SMatthew Dillon * Limit the time measuring the performance of other tx
533572ff6f6SMatthew Dillon * rates to sample_rate% of the total transmission time.
534572ff6f6SMatthew Dillon */
535572ff6f6SMatthew Dillon if (sn->sample_tt[size_bin] < average_tx_time * (sn->packets_since_sample[size_bin]*ssc->sample_rate/100)) {
536572ff6f6SMatthew Dillon rix = pick_sample_rate(ssc, an, rt, size_bin);
537572ff6f6SMatthew Dillon IEEE80211_NOTE(an->an_node.ni_vap, IEEE80211_MSG_RATECTL,
538572ff6f6SMatthew Dillon &an->an_node, "att %d sample_tt %d size %u sample rate %d %s current rate %d %s",
539572ff6f6SMatthew Dillon average_tx_time,
540572ff6f6SMatthew Dillon sn->sample_tt[size_bin],
541572ff6f6SMatthew Dillon bin_to_size(size_bin),
542572ff6f6SMatthew Dillon dot11rate(rt, rix),
543572ff6f6SMatthew Dillon dot11rate_label(rt, rix),
544572ff6f6SMatthew Dillon dot11rate(rt, sn->current_rix[size_bin]),
545572ff6f6SMatthew Dillon dot11rate_label(rt, sn->current_rix[size_bin]));
546572ff6f6SMatthew Dillon if (rix != sn->current_rix[size_bin]) {
547572ff6f6SMatthew Dillon sn->current_sample_rix[size_bin] = rix;
548572ff6f6SMatthew Dillon } else {
549572ff6f6SMatthew Dillon sn->current_sample_rix[size_bin] = -1;
550572ff6f6SMatthew Dillon }
551572ff6f6SMatthew Dillon sn->packets_since_sample[size_bin] = 0;
552572ff6f6SMatthew Dillon } else {
553572ff6f6SMatthew Dillon change_rates = 0;
554572ff6f6SMatthew Dillon if (!sn->packets_sent[size_bin] || best_rix == -1) {
555572ff6f6SMatthew Dillon /* no packet has been sent successfully yet */
556572ff6f6SMatthew Dillon change_rates = 1;
557572ff6f6SMatthew Dillon if (an->an_node.ni_flags & IEEE80211_NODE_HT)
558572ff6f6SMatthew Dillon best_rix =
559572ff6f6SMatthew Dillon ath_rate_pick_seed_rate_ht(sc, an, frameLen);
560572ff6f6SMatthew Dillon else
561572ff6f6SMatthew Dillon best_rix =
562572ff6f6SMatthew Dillon ath_rate_pick_seed_rate_legacy(sc, an, frameLen);
563572ff6f6SMatthew Dillon } else if (sn->packets_sent[size_bin] < 20) {
564572ff6f6SMatthew Dillon /* let the bit-rate switch quickly during the first few packets */
565572ff6f6SMatthew Dillon IEEE80211_NOTE(an->an_node.ni_vap,
566572ff6f6SMatthew Dillon IEEE80211_MSG_RATECTL, &an->an_node,
567572ff6f6SMatthew Dillon "%s: switching quickly..", __func__);
568572ff6f6SMatthew Dillon change_rates = 1;
569572ff6f6SMatthew Dillon } else if (ticks - ssc->min_switch > sn->ticks_since_switch[size_bin]) {
570572ff6f6SMatthew Dillon /* min_switch seconds have gone by */
571572ff6f6SMatthew Dillon IEEE80211_NOTE(an->an_node.ni_vap,
572572ff6f6SMatthew Dillon IEEE80211_MSG_RATECTL, &an->an_node,
573572ff6f6SMatthew Dillon "%s: min_switch %d > ticks_since_switch %d..",
574572ff6f6SMatthew Dillon __func__, ticks - ssc->min_switch, sn->ticks_since_switch[size_bin]);
575572ff6f6SMatthew Dillon change_rates = 1;
576572ff6f6SMatthew Dillon } else if ((! (an->an_node.ni_flags & IEEE80211_NODE_HT)) &&
577572ff6f6SMatthew Dillon (2*average_tx_time < sn->stats[size_bin][sn->current_rix[size_bin]].average_tx_time)) {
578572ff6f6SMatthew Dillon /* the current bit-rate is twice as slow as the best one */
579572ff6f6SMatthew Dillon IEEE80211_NOTE(an->an_node.ni_vap,
580572ff6f6SMatthew Dillon IEEE80211_MSG_RATECTL, &an->an_node,
581572ff6f6SMatthew Dillon "%s: 2x att (= %d) < cur_rix att %d",
582572ff6f6SMatthew Dillon __func__,
583572ff6f6SMatthew Dillon 2 * average_tx_time, sn->stats[size_bin][sn->current_rix[size_bin]].average_tx_time);
584572ff6f6SMatthew Dillon change_rates = 1;
585572ff6f6SMatthew Dillon } else if ((an->an_node.ni_flags & IEEE80211_NODE_HT)) {
586572ff6f6SMatthew Dillon int cur_rix = sn->current_rix[size_bin];
587572ff6f6SMatthew Dillon int cur_att = sn->stats[size_bin][cur_rix].average_tx_time;
588572ff6f6SMatthew Dillon /*
589572ff6f6SMatthew Dillon * If the node is HT, upgrade it if the MCS rate is
590572ff6f6SMatthew Dillon * higher and the average tx time is within 20% of
591572ff6f6SMatthew Dillon * the current rate. It can fail a little.
592572ff6f6SMatthew Dillon *
593572ff6f6SMatthew Dillon * This is likely not optimal!
594572ff6f6SMatthew Dillon */
595572ff6f6SMatthew Dillon #if 0
596dc249793SMatthew Dillon kprintf("cur rix/att %x/%d, best rix/att %x/%d\n",
597572ff6f6SMatthew Dillon MCS(cur_rix), cur_att, MCS(best_rix), average_tx_time);
598572ff6f6SMatthew Dillon #endif
599572ff6f6SMatthew Dillon if ((MCS(best_rix) > MCS(cur_rix)) &&
600572ff6f6SMatthew Dillon (average_tx_time * 8) <= (cur_att * 10)) {
601572ff6f6SMatthew Dillon IEEE80211_NOTE(an->an_node.ni_vap,
602572ff6f6SMatthew Dillon IEEE80211_MSG_RATECTL, &an->an_node,
603572ff6f6SMatthew Dillon "%s: HT: best_rix 0x%d > cur_rix 0x%x, average_tx_time %d, cur_att %d",
604572ff6f6SMatthew Dillon __func__,
605572ff6f6SMatthew Dillon MCS(best_rix), MCS(cur_rix), average_tx_time, cur_att);
606572ff6f6SMatthew Dillon change_rates = 1;
607572ff6f6SMatthew Dillon }
608572ff6f6SMatthew Dillon }
609572ff6f6SMatthew Dillon
610572ff6f6SMatthew Dillon sn->packets_since_sample[size_bin]++;
611572ff6f6SMatthew Dillon
612572ff6f6SMatthew Dillon if (change_rates) {
613572ff6f6SMatthew Dillon if (best_rix != sn->current_rix[size_bin]) {
614572ff6f6SMatthew Dillon IEEE80211_NOTE(an->an_node.ni_vap,
615572ff6f6SMatthew Dillon IEEE80211_MSG_RATECTL,
616572ff6f6SMatthew Dillon &an->an_node,
617572ff6f6SMatthew Dillon "%s: size %d switch rate %d (%d/%d) -> %d (%d/%d) after %d packets mrr %d",
618572ff6f6SMatthew Dillon __func__,
619572ff6f6SMatthew Dillon bin_to_size(size_bin),
620572ff6f6SMatthew Dillon RATE(sn->current_rix[size_bin]),
621572ff6f6SMatthew Dillon sn->stats[size_bin][sn->current_rix[size_bin]].average_tx_time,
622572ff6f6SMatthew Dillon sn->stats[size_bin][sn->current_rix[size_bin]].perfect_tx_time,
623572ff6f6SMatthew Dillon RATE(best_rix),
624572ff6f6SMatthew Dillon sn->stats[size_bin][best_rix].average_tx_time,
625572ff6f6SMatthew Dillon sn->stats[size_bin][best_rix].perfect_tx_time,
626572ff6f6SMatthew Dillon sn->packets_since_switch[size_bin],
627572ff6f6SMatthew Dillon mrr);
628572ff6f6SMatthew Dillon }
629572ff6f6SMatthew Dillon sn->packets_since_switch[size_bin] = 0;
630572ff6f6SMatthew Dillon sn->current_rix[size_bin] = best_rix;
631572ff6f6SMatthew Dillon sn->ticks_since_switch[size_bin] = ticks;
632572ff6f6SMatthew Dillon /*
633572ff6f6SMatthew Dillon * Set the visible txrate for this node.
634572ff6f6SMatthew Dillon */
635572ff6f6SMatthew Dillon an->an_node.ni_txrate = (rt->info[best_rix].phy == IEEE80211_T_HT) ? MCS(best_rix) : DOT11RATE(best_rix);
636572ff6f6SMatthew Dillon }
637572ff6f6SMatthew Dillon rix = sn->current_rix[size_bin];
638572ff6f6SMatthew Dillon sn->packets_since_switch[size_bin]++;
639572ff6f6SMatthew Dillon }
640572ff6f6SMatthew Dillon *try0 = mrr ? sn->sched[rix].t0 : ATH_TXMAXTRY;
641572ff6f6SMatthew Dillon done:
642572ff6f6SMatthew Dillon
643572ff6f6SMatthew Dillon /*
644572ff6f6SMatthew Dillon * This bug totally sucks and should be fixed.
645572ff6f6SMatthew Dillon *
646572ff6f6SMatthew Dillon * For now though, let's not panic, so we can start to figure
647572ff6f6SMatthew Dillon * out how to better reproduce it.
648572ff6f6SMatthew Dillon */
649572ff6f6SMatthew Dillon if (rix < 0 || rix >= rt->rateCount) {
650dc249793SMatthew Dillon kprintf("%s: ERROR: rix %d out of bounds (rateCount=%d)\n",
651572ff6f6SMatthew Dillon __func__,
652572ff6f6SMatthew Dillon rix,
653572ff6f6SMatthew Dillon rt->rateCount);
654572ff6f6SMatthew Dillon rix = 0; /* XXX just default for now */
655572ff6f6SMatthew Dillon }
656572ff6f6SMatthew Dillon KASSERT(rix >= 0 && rix < rt->rateCount, ("rix is %d", rix));
657572ff6f6SMatthew Dillon
658572ff6f6SMatthew Dillon *rix0 = rix;
659572ff6f6SMatthew Dillon *txrate = rt->info[rix].rateCode
660572ff6f6SMatthew Dillon | (shortPreamble ? rt->info[rix].shortPreamble : 0);
661572ff6f6SMatthew Dillon sn->packets_sent[size_bin]++;
662572ff6f6SMatthew Dillon #undef DOT11RATE
663572ff6f6SMatthew Dillon #undef MCS
664572ff6f6SMatthew Dillon #undef RATE
665572ff6f6SMatthew Dillon }
666572ff6f6SMatthew Dillon
667572ff6f6SMatthew Dillon /*
668572ff6f6SMatthew Dillon * Get the TX rates. Don't fiddle with short preamble flags for them;
669572ff6f6SMatthew Dillon * the caller can do that.
670572ff6f6SMatthew Dillon */
671572ff6f6SMatthew Dillon void
ath_rate_getxtxrates(struct ath_softc * sc,struct ath_node * an,uint8_t rix0,struct ath_rc_series * rc)672572ff6f6SMatthew Dillon ath_rate_getxtxrates(struct ath_softc *sc, struct ath_node *an,
673572ff6f6SMatthew Dillon uint8_t rix0, struct ath_rc_series *rc)
674572ff6f6SMatthew Dillon {
675572ff6f6SMatthew Dillon struct sample_node *sn = ATH_NODE_SAMPLE(an);
676572ff6f6SMatthew Dillon const struct txschedule *sched = &sn->sched[rix0];
677572ff6f6SMatthew Dillon
678572ff6f6SMatthew Dillon KASSERT(rix0 == sched->r0, ("rix0 (%x) != sched->r0 (%x)!\n",
679572ff6f6SMatthew Dillon rix0, sched->r0));
680572ff6f6SMatthew Dillon
681572ff6f6SMatthew Dillon rc[0].flags = rc[1].flags = rc[2].flags = rc[3].flags = 0;
682572ff6f6SMatthew Dillon
683572ff6f6SMatthew Dillon rc[0].rix = sched->r0;
684572ff6f6SMatthew Dillon rc[1].rix = sched->r1;
685572ff6f6SMatthew Dillon rc[2].rix = sched->r2;
686572ff6f6SMatthew Dillon rc[3].rix = sched->r3;
687572ff6f6SMatthew Dillon
688572ff6f6SMatthew Dillon rc[0].tries = sched->t0;
689572ff6f6SMatthew Dillon rc[1].tries = sched->t1;
690572ff6f6SMatthew Dillon rc[2].tries = sched->t2;
691572ff6f6SMatthew Dillon rc[3].tries = sched->t3;
692572ff6f6SMatthew Dillon }
693572ff6f6SMatthew Dillon
694572ff6f6SMatthew Dillon void
ath_rate_setupxtxdesc(struct ath_softc * sc,struct ath_node * an,struct ath_desc * ds,int shortPreamble,u_int8_t rix)695572ff6f6SMatthew Dillon ath_rate_setupxtxdesc(struct ath_softc *sc, struct ath_node *an,
696572ff6f6SMatthew Dillon struct ath_desc *ds, int shortPreamble, u_int8_t rix)
697572ff6f6SMatthew Dillon {
698572ff6f6SMatthew Dillon struct sample_node *sn = ATH_NODE_SAMPLE(an);
699572ff6f6SMatthew Dillon const struct txschedule *sched = &sn->sched[rix];
700572ff6f6SMatthew Dillon const HAL_RATE_TABLE *rt = sc->sc_currates;
701572ff6f6SMatthew Dillon uint8_t rix1, s1code, rix2, s2code, rix3, s3code;
702572ff6f6SMatthew Dillon
703572ff6f6SMatthew Dillon /* XXX precalculate short preamble tables */
704572ff6f6SMatthew Dillon rix1 = sched->r1;
705572ff6f6SMatthew Dillon s1code = rt->info[rix1].rateCode
706572ff6f6SMatthew Dillon | (shortPreamble ? rt->info[rix1].shortPreamble : 0);
707572ff6f6SMatthew Dillon rix2 = sched->r2;
708572ff6f6SMatthew Dillon s2code = rt->info[rix2].rateCode
709572ff6f6SMatthew Dillon | (shortPreamble ? rt->info[rix2].shortPreamble : 0);
710572ff6f6SMatthew Dillon rix3 = sched->r3;
711572ff6f6SMatthew Dillon s3code = rt->info[rix3].rateCode
712572ff6f6SMatthew Dillon | (shortPreamble ? rt->info[rix3].shortPreamble : 0);
713572ff6f6SMatthew Dillon ath_hal_setupxtxdesc(sc->sc_ah, ds,
714572ff6f6SMatthew Dillon s1code, sched->t1, /* series 1 */
715572ff6f6SMatthew Dillon s2code, sched->t2, /* series 2 */
716572ff6f6SMatthew Dillon s3code, sched->t3); /* series 3 */
717572ff6f6SMatthew Dillon }
718572ff6f6SMatthew Dillon
719572ff6f6SMatthew Dillon static void
update_stats(struct ath_softc * sc,struct ath_node * an,int frame_size,int rix0,int tries0,int rix1,int tries1,int rix2,int tries2,int rix3,int tries3,int short_tries,int tries,int status,int nframes,int nbad)720572ff6f6SMatthew Dillon update_stats(struct ath_softc *sc, struct ath_node *an,
721572ff6f6SMatthew Dillon int frame_size,
722572ff6f6SMatthew Dillon int rix0, int tries0,
723572ff6f6SMatthew Dillon int rix1, int tries1,
724572ff6f6SMatthew Dillon int rix2, int tries2,
725572ff6f6SMatthew Dillon int rix3, int tries3,
726572ff6f6SMatthew Dillon int short_tries, int tries, int status,
727572ff6f6SMatthew Dillon int nframes, int nbad)
728572ff6f6SMatthew Dillon {
729572ff6f6SMatthew Dillon struct sample_node *sn = ATH_NODE_SAMPLE(an);
730572ff6f6SMatthew Dillon struct sample_softc *ssc = ATH_SOFTC_SAMPLE(sc);
731572ff6f6SMatthew Dillon #ifdef IEEE80211_DEBUG
732572ff6f6SMatthew Dillon const HAL_RATE_TABLE *rt = sc->sc_currates;
733572ff6f6SMatthew Dillon #endif
734572ff6f6SMatthew Dillon const int size_bin = size_to_bin(frame_size);
735572ff6f6SMatthew Dillon const int size = bin_to_size(size_bin);
736572ff6f6SMatthew Dillon int tt, tries_so_far;
737572ff6f6SMatthew Dillon int is_ht40 = (an->an_node.ni_chw == 40);
738572ff6f6SMatthew Dillon int pct;
739572ff6f6SMatthew Dillon
740572ff6f6SMatthew Dillon if (!IS_RATE_DEFINED(sn, rix0))
741572ff6f6SMatthew Dillon return;
742572ff6f6SMatthew Dillon tt = calc_usecs_unicast_packet(sc, size, rix0, short_tries,
743572ff6f6SMatthew Dillon MIN(tries0, tries) - 1, is_ht40);
744572ff6f6SMatthew Dillon tries_so_far = tries0;
745572ff6f6SMatthew Dillon
746572ff6f6SMatthew Dillon if (tries1 && tries_so_far < tries) {
747572ff6f6SMatthew Dillon if (!IS_RATE_DEFINED(sn, rix1))
748572ff6f6SMatthew Dillon return;
749572ff6f6SMatthew Dillon tt += calc_usecs_unicast_packet(sc, size, rix1, short_tries,
750572ff6f6SMatthew Dillon MIN(tries1 + tries_so_far, tries) - tries_so_far - 1, is_ht40);
751572ff6f6SMatthew Dillon tries_so_far += tries1;
752572ff6f6SMatthew Dillon }
753572ff6f6SMatthew Dillon
754572ff6f6SMatthew Dillon if (tries2 && tries_so_far < tries) {
755572ff6f6SMatthew Dillon if (!IS_RATE_DEFINED(sn, rix2))
756572ff6f6SMatthew Dillon return;
757572ff6f6SMatthew Dillon tt += calc_usecs_unicast_packet(sc, size, rix2, short_tries,
758572ff6f6SMatthew Dillon MIN(tries2 + tries_so_far, tries) - tries_so_far - 1, is_ht40);
759572ff6f6SMatthew Dillon tries_so_far += tries2;
760572ff6f6SMatthew Dillon }
761572ff6f6SMatthew Dillon
762572ff6f6SMatthew Dillon if (tries3 && tries_so_far < tries) {
763572ff6f6SMatthew Dillon if (!IS_RATE_DEFINED(sn, rix3))
764572ff6f6SMatthew Dillon return;
765572ff6f6SMatthew Dillon tt += calc_usecs_unicast_packet(sc, size, rix3, short_tries,
766572ff6f6SMatthew Dillon MIN(tries3 + tries_so_far, tries) - tries_so_far - 1, is_ht40);
767572ff6f6SMatthew Dillon }
768572ff6f6SMatthew Dillon
769572ff6f6SMatthew Dillon if (sn->stats[size_bin][rix0].total_packets < ssc->smoothing_minpackets) {
770572ff6f6SMatthew Dillon /* just average the first few packets */
771572ff6f6SMatthew Dillon int avg_tx = sn->stats[size_bin][rix0].average_tx_time;
772572ff6f6SMatthew Dillon int packets = sn->stats[size_bin][rix0].total_packets;
773572ff6f6SMatthew Dillon sn->stats[size_bin][rix0].average_tx_time = (tt+(avg_tx*packets))/(packets+nframes);
774572ff6f6SMatthew Dillon } else {
775572ff6f6SMatthew Dillon /* use a ewma */
776572ff6f6SMatthew Dillon sn->stats[size_bin][rix0].average_tx_time =
777572ff6f6SMatthew Dillon ((sn->stats[size_bin][rix0].average_tx_time * ssc->smoothing_rate) +
778572ff6f6SMatthew Dillon (tt * (100 - ssc->smoothing_rate))) / 100;
779572ff6f6SMatthew Dillon }
780572ff6f6SMatthew Dillon
781572ff6f6SMatthew Dillon /*
782572ff6f6SMatthew Dillon * XXX Don't mark the higher bit rates as also having failed; as this
783572ff6f6SMatthew Dillon * unfortunately stops those rates from being tasted when trying to
784572ff6f6SMatthew Dillon * TX. This happens with 11n aggregation.
785572ff6f6SMatthew Dillon */
786572ff6f6SMatthew Dillon if (nframes == nbad) {
787572ff6f6SMatthew Dillon #if 0
788572ff6f6SMatthew Dillon int y;
789572ff6f6SMatthew Dillon #endif
790572ff6f6SMatthew Dillon sn->stats[size_bin][rix0].successive_failures += nbad;
791572ff6f6SMatthew Dillon #if 0
792572ff6f6SMatthew Dillon for (y = size_bin+1; y < NUM_PACKET_SIZE_BINS; y++) {
793572ff6f6SMatthew Dillon /*
794572ff6f6SMatthew Dillon * Also say larger packets failed since we
795572ff6f6SMatthew Dillon * assume if a small packet fails at a
796572ff6f6SMatthew Dillon * bit-rate then a larger one will also.
797572ff6f6SMatthew Dillon */
798572ff6f6SMatthew Dillon sn->stats[y][rix0].successive_failures += nbad;
799572ff6f6SMatthew Dillon sn->stats[y][rix0].last_tx = ticks;
800572ff6f6SMatthew Dillon sn->stats[y][rix0].tries += tries;
801572ff6f6SMatthew Dillon sn->stats[y][rix0].total_packets += nframes;
802572ff6f6SMatthew Dillon }
803572ff6f6SMatthew Dillon #endif
804572ff6f6SMatthew Dillon } else {
805572ff6f6SMatthew Dillon sn->stats[size_bin][rix0].packets_acked += (nframes - nbad);
806572ff6f6SMatthew Dillon sn->stats[size_bin][rix0].successive_failures = 0;
807572ff6f6SMatthew Dillon }
808572ff6f6SMatthew Dillon sn->stats[size_bin][rix0].tries += tries;
809572ff6f6SMatthew Dillon sn->stats[size_bin][rix0].last_tx = ticks;
810572ff6f6SMatthew Dillon sn->stats[size_bin][rix0].total_packets += nframes;
811572ff6f6SMatthew Dillon
812572ff6f6SMatthew Dillon /* update EWMA for this rix */
813572ff6f6SMatthew Dillon
814572ff6f6SMatthew Dillon /* Calculate percentage based on current rate */
815572ff6f6SMatthew Dillon if (nframes == 0)
816572ff6f6SMatthew Dillon nframes = nbad = 1;
817572ff6f6SMatthew Dillon pct = ((nframes - nbad) * 1000) / nframes;
818572ff6f6SMatthew Dillon
819572ff6f6SMatthew Dillon if (sn->stats[size_bin][rix0].total_packets <
820572ff6f6SMatthew Dillon ssc->smoothing_minpackets) {
821572ff6f6SMatthew Dillon /* just average the first few packets */
822572ff6f6SMatthew Dillon int a_pct = (sn->stats[size_bin][rix0].packets_acked * 1000) /
823572ff6f6SMatthew Dillon (sn->stats[size_bin][rix0].total_packets);
824572ff6f6SMatthew Dillon sn->stats[size_bin][rix0].ewma_pct = a_pct;
825572ff6f6SMatthew Dillon } else {
826572ff6f6SMatthew Dillon /* use a ewma */
827572ff6f6SMatthew Dillon sn->stats[size_bin][rix0].ewma_pct =
828572ff6f6SMatthew Dillon ((sn->stats[size_bin][rix0].ewma_pct * ssc->smoothing_rate) +
829572ff6f6SMatthew Dillon (pct * (100 - ssc->smoothing_rate))) / 100;
830572ff6f6SMatthew Dillon }
831572ff6f6SMatthew Dillon
832572ff6f6SMatthew Dillon
833572ff6f6SMatthew Dillon if (rix0 == sn->current_sample_rix[size_bin]) {
834572ff6f6SMatthew Dillon IEEE80211_NOTE(an->an_node.ni_vap, IEEE80211_MSG_RATECTL,
835572ff6f6SMatthew Dillon &an->an_node,
836572ff6f6SMatthew Dillon "%s: size %d %s sample rate %d %s tries (%d/%d) tt %d avg_tt (%d/%d) nfrm %d nbad %d",
837572ff6f6SMatthew Dillon __func__,
838572ff6f6SMatthew Dillon size,
839572ff6f6SMatthew Dillon status ? "FAIL" : "OK",
840572ff6f6SMatthew Dillon dot11rate(rt, rix0),
841572ff6f6SMatthew Dillon dot11rate_label(rt, rix0),
842572ff6f6SMatthew Dillon short_tries, tries, tt,
843572ff6f6SMatthew Dillon sn->stats[size_bin][rix0].average_tx_time,
844572ff6f6SMatthew Dillon sn->stats[size_bin][rix0].perfect_tx_time,
845572ff6f6SMatthew Dillon nframes, nbad);
846572ff6f6SMatthew Dillon sn->sample_tt[size_bin] = tt;
847572ff6f6SMatthew Dillon sn->current_sample_rix[size_bin] = -1;
848572ff6f6SMatthew Dillon }
849572ff6f6SMatthew Dillon }
850572ff6f6SMatthew Dillon
851572ff6f6SMatthew Dillon static void
badrate(struct ath_softc * sc,int series,int hwrate,int tries,int status)852b14ca477SMatthew Dillon badrate(struct ath_softc *sc, int series, int hwrate, int tries, int status)
853572ff6f6SMatthew Dillon {
854b14ca477SMatthew Dillon
855b14ca477SMatthew Dillon device_printf(sc->sc_dev,
856b14ca477SMatthew Dillon "bad series%d hwrate 0x%x, tries %u ts_status 0x%x\n",
857572ff6f6SMatthew Dillon series, hwrate, tries, status);
858572ff6f6SMatthew Dillon }
859572ff6f6SMatthew Dillon
860572ff6f6SMatthew Dillon void
ath_rate_tx_complete(struct ath_softc * sc,struct ath_node * an,const struct ath_rc_series * rc,const struct ath_tx_status * ts,int frame_size,int nframes,int nbad)861572ff6f6SMatthew Dillon ath_rate_tx_complete(struct ath_softc *sc, struct ath_node *an,
862572ff6f6SMatthew Dillon const struct ath_rc_series *rc, const struct ath_tx_status *ts,
863572ff6f6SMatthew Dillon int frame_size, int nframes, int nbad)
864572ff6f6SMatthew Dillon {
865b14ca477SMatthew Dillon struct ieee80211com *ic = &sc->sc_ic;
866572ff6f6SMatthew Dillon struct sample_node *sn = ATH_NODE_SAMPLE(an);
867572ff6f6SMatthew Dillon int final_rix, short_tries, long_tries;
868572ff6f6SMatthew Dillon const HAL_RATE_TABLE *rt = sc->sc_currates;
869572ff6f6SMatthew Dillon int status = ts->ts_status;
870572ff6f6SMatthew Dillon int mrr;
871572ff6f6SMatthew Dillon
872572ff6f6SMatthew Dillon final_rix = rt->rateCodeToIndex[ts->ts_rate];
873572ff6f6SMatthew Dillon short_tries = ts->ts_shortretry;
874572ff6f6SMatthew Dillon long_tries = ts->ts_longretry + 1;
875572ff6f6SMatthew Dillon
876572ff6f6SMatthew Dillon if (nframes == 0) {
877572ff6f6SMatthew Dillon device_printf(sc->sc_dev, "%s: nframes=0?\n", __func__);
878572ff6f6SMatthew Dillon return;
879572ff6f6SMatthew Dillon }
880572ff6f6SMatthew Dillon
881572ff6f6SMatthew Dillon if (frame_size == 0) /* NB: should not happen */
882572ff6f6SMatthew Dillon frame_size = 1500;
883572ff6f6SMatthew Dillon
884572ff6f6SMatthew Dillon if (sn->ratemask == 0) {
885572ff6f6SMatthew Dillon IEEE80211_NOTE(an->an_node.ni_vap, IEEE80211_MSG_RATECTL,
886572ff6f6SMatthew Dillon &an->an_node,
887572ff6f6SMatthew Dillon "%s: size %d %s rate/try %d/%d no rates yet",
888572ff6f6SMatthew Dillon __func__,
889572ff6f6SMatthew Dillon bin_to_size(size_to_bin(frame_size)),
890572ff6f6SMatthew Dillon status ? "FAIL" : "OK",
891572ff6f6SMatthew Dillon short_tries, long_tries);
892572ff6f6SMatthew Dillon return;
893572ff6f6SMatthew Dillon }
894572ff6f6SMatthew Dillon mrr = sc->sc_mrretry;
895572ff6f6SMatthew Dillon /* XXX check HT protmode too */
896572ff6f6SMatthew Dillon if (mrr && (ic->ic_flags & IEEE80211_F_USEPROT && !sc->sc_mrrprot))
897572ff6f6SMatthew Dillon mrr = 0;
898572ff6f6SMatthew Dillon
899572ff6f6SMatthew Dillon if (!mrr || ts->ts_finaltsi == 0) {
900572ff6f6SMatthew Dillon if (!IS_RATE_DEFINED(sn, final_rix)) {
901b14ca477SMatthew Dillon device_printf(sc->sc_dev,
902b14ca477SMatthew Dillon "%s: ts_rate=%d ts_finaltsi=%d, final_rix=%d\n",
903b14ca477SMatthew Dillon __func__, ts->ts_rate, ts->ts_finaltsi, final_rix);
904b14ca477SMatthew Dillon badrate(sc, 0, ts->ts_rate, long_tries, status);
905572ff6f6SMatthew Dillon return;
906572ff6f6SMatthew Dillon }
907572ff6f6SMatthew Dillon /*
908572ff6f6SMatthew Dillon * Only one rate was used; optimize work.
909572ff6f6SMatthew Dillon */
910572ff6f6SMatthew Dillon IEEE80211_NOTE(an->an_node.ni_vap, IEEE80211_MSG_RATECTL,
911572ff6f6SMatthew Dillon &an->an_node, "%s: size %d (%d bytes) %s rate/short/long %d %s/%d/%d nframes/nbad [%d/%d]",
912572ff6f6SMatthew Dillon __func__,
913572ff6f6SMatthew Dillon bin_to_size(size_to_bin(frame_size)),
914572ff6f6SMatthew Dillon frame_size,
915572ff6f6SMatthew Dillon status ? "FAIL" : "OK",
916572ff6f6SMatthew Dillon dot11rate(rt, final_rix), dot11rate_label(rt, final_rix),
917572ff6f6SMatthew Dillon short_tries, long_tries, nframes, nbad);
918572ff6f6SMatthew Dillon update_stats(sc, an, frame_size,
919572ff6f6SMatthew Dillon final_rix, long_tries,
920572ff6f6SMatthew Dillon 0, 0,
921572ff6f6SMatthew Dillon 0, 0,
922572ff6f6SMatthew Dillon 0, 0,
923572ff6f6SMatthew Dillon short_tries, long_tries, status,
924572ff6f6SMatthew Dillon nframes, nbad);
925572ff6f6SMatthew Dillon
926572ff6f6SMatthew Dillon } else {
927572ff6f6SMatthew Dillon int finalTSIdx = ts->ts_finaltsi;
928572ff6f6SMatthew Dillon int i;
929572ff6f6SMatthew Dillon
930572ff6f6SMatthew Dillon /*
931572ff6f6SMatthew Dillon * Process intermediate rates that failed.
932572ff6f6SMatthew Dillon */
933572ff6f6SMatthew Dillon
934572ff6f6SMatthew Dillon IEEE80211_NOTE(an->an_node.ni_vap, IEEE80211_MSG_RATECTL,
935572ff6f6SMatthew Dillon &an->an_node,
936572ff6f6SMatthew Dillon "%s: size %d (%d bytes) finaltsidx %d short %d long %d %s rate/try [%d %s/%d %d %s/%d %d %s/%d %d %s/%d] nframes/nbad [%d/%d]",
937572ff6f6SMatthew Dillon __func__,
938572ff6f6SMatthew Dillon bin_to_size(size_to_bin(frame_size)),
939572ff6f6SMatthew Dillon frame_size,
940572ff6f6SMatthew Dillon finalTSIdx,
941572ff6f6SMatthew Dillon short_tries,
942572ff6f6SMatthew Dillon long_tries,
943572ff6f6SMatthew Dillon status ? "FAIL" : "OK",
944572ff6f6SMatthew Dillon dot11rate(rt, rc[0].rix),
945572ff6f6SMatthew Dillon dot11rate_label(rt, rc[0].rix), rc[0].tries,
946572ff6f6SMatthew Dillon dot11rate(rt, rc[1].rix),
947572ff6f6SMatthew Dillon dot11rate_label(rt, rc[1].rix), rc[1].tries,
948572ff6f6SMatthew Dillon dot11rate(rt, rc[2].rix),
949572ff6f6SMatthew Dillon dot11rate_label(rt, rc[2].rix), rc[2].tries,
950572ff6f6SMatthew Dillon dot11rate(rt, rc[3].rix),
951572ff6f6SMatthew Dillon dot11rate_label(rt, rc[3].rix), rc[3].tries,
952572ff6f6SMatthew Dillon nframes, nbad);
953572ff6f6SMatthew Dillon
954572ff6f6SMatthew Dillon for (i = 0; i < 4; i++) {
955572ff6f6SMatthew Dillon if (rc[i].tries && !IS_RATE_DEFINED(sn, rc[i].rix))
956b14ca477SMatthew Dillon badrate(sc, 0, rc[i].ratecode, rc[i].tries,
957572ff6f6SMatthew Dillon status);
958572ff6f6SMatthew Dillon }
959572ff6f6SMatthew Dillon
960572ff6f6SMatthew Dillon /*
961572ff6f6SMatthew Dillon * NB: series > 0 are not penalized for failure
962572ff6f6SMatthew Dillon * based on the try counts under the assumption
963572ff6f6SMatthew Dillon * that losses are often bursty and since we
964572ff6f6SMatthew Dillon * sample higher rates 1 try at a time doing so
965572ff6f6SMatthew Dillon * may unfairly penalize them.
966572ff6f6SMatthew Dillon */
967572ff6f6SMatthew Dillon if (rc[0].tries) {
968572ff6f6SMatthew Dillon update_stats(sc, an, frame_size,
969572ff6f6SMatthew Dillon rc[0].rix, rc[0].tries,
970572ff6f6SMatthew Dillon rc[1].rix, rc[1].tries,
971572ff6f6SMatthew Dillon rc[2].rix, rc[2].tries,
972572ff6f6SMatthew Dillon rc[3].rix, rc[3].tries,
973572ff6f6SMatthew Dillon short_tries, long_tries,
974572ff6f6SMatthew Dillon long_tries > rc[0].tries,
975572ff6f6SMatthew Dillon nframes, nbad);
976572ff6f6SMatthew Dillon long_tries -= rc[0].tries;
977572ff6f6SMatthew Dillon }
978572ff6f6SMatthew Dillon
979572ff6f6SMatthew Dillon if (rc[1].tries && finalTSIdx > 0) {
980572ff6f6SMatthew Dillon update_stats(sc, an, frame_size,
981572ff6f6SMatthew Dillon rc[1].rix, rc[1].tries,
982572ff6f6SMatthew Dillon rc[2].rix, rc[2].tries,
983572ff6f6SMatthew Dillon rc[3].rix, rc[3].tries,
984572ff6f6SMatthew Dillon 0, 0,
985572ff6f6SMatthew Dillon short_tries, long_tries,
986572ff6f6SMatthew Dillon status,
987572ff6f6SMatthew Dillon nframes, nbad);
988572ff6f6SMatthew Dillon long_tries -= rc[1].tries;
989572ff6f6SMatthew Dillon }
990572ff6f6SMatthew Dillon
991572ff6f6SMatthew Dillon if (rc[2].tries && finalTSIdx > 1) {
992572ff6f6SMatthew Dillon update_stats(sc, an, frame_size,
993572ff6f6SMatthew Dillon rc[2].rix, rc[2].tries,
994572ff6f6SMatthew Dillon rc[3].rix, rc[3].tries,
995572ff6f6SMatthew Dillon 0, 0,
996572ff6f6SMatthew Dillon 0, 0,
997572ff6f6SMatthew Dillon short_tries, long_tries,
998572ff6f6SMatthew Dillon status,
999572ff6f6SMatthew Dillon nframes, nbad);
1000572ff6f6SMatthew Dillon long_tries -= rc[2].tries;
1001572ff6f6SMatthew Dillon }
1002572ff6f6SMatthew Dillon
1003572ff6f6SMatthew Dillon if (rc[3].tries && finalTSIdx > 2) {
1004572ff6f6SMatthew Dillon update_stats(sc, an, frame_size,
1005572ff6f6SMatthew Dillon rc[3].rix, rc[3].tries,
1006572ff6f6SMatthew Dillon 0, 0,
1007572ff6f6SMatthew Dillon 0, 0,
1008572ff6f6SMatthew Dillon 0, 0,
1009572ff6f6SMatthew Dillon short_tries, long_tries,
1010572ff6f6SMatthew Dillon status,
1011572ff6f6SMatthew Dillon nframes, nbad);
1012572ff6f6SMatthew Dillon }
1013572ff6f6SMatthew Dillon }
1014572ff6f6SMatthew Dillon }
1015572ff6f6SMatthew Dillon
1016572ff6f6SMatthew Dillon void
ath_rate_newassoc(struct ath_softc * sc,struct ath_node * an,int isnew)1017572ff6f6SMatthew Dillon ath_rate_newassoc(struct ath_softc *sc, struct ath_node *an, int isnew)
1018572ff6f6SMatthew Dillon {
1019572ff6f6SMatthew Dillon if (isnew)
1020572ff6f6SMatthew Dillon ath_rate_ctl_reset(sc, &an->an_node);
1021572ff6f6SMatthew Dillon }
1022572ff6f6SMatthew Dillon
1023572ff6f6SMatthew Dillon static const struct txschedule *mrr_schedules[IEEE80211_MODE_MAX+2] = {
1024572ff6f6SMatthew Dillon NULL, /* IEEE80211_MODE_AUTO */
1025572ff6f6SMatthew Dillon series_11a, /* IEEE80211_MODE_11A */
1026572ff6f6SMatthew Dillon series_11g, /* IEEE80211_MODE_11B */
1027572ff6f6SMatthew Dillon series_11g, /* IEEE80211_MODE_11G */
1028572ff6f6SMatthew Dillon NULL, /* IEEE80211_MODE_FH */
1029572ff6f6SMatthew Dillon series_11a, /* IEEE80211_MODE_TURBO_A */
1030572ff6f6SMatthew Dillon series_11g, /* IEEE80211_MODE_TURBO_G */
1031572ff6f6SMatthew Dillon series_11a, /* IEEE80211_MODE_STURBO_A */
1032572ff6f6SMatthew Dillon series_11na, /* IEEE80211_MODE_11NA */
1033572ff6f6SMatthew Dillon series_11ng, /* IEEE80211_MODE_11NG */
1034572ff6f6SMatthew Dillon series_half, /* IEEE80211_MODE_HALF */
1035572ff6f6SMatthew Dillon series_quarter, /* IEEE80211_MODE_QUARTER */
1036572ff6f6SMatthew Dillon };
1037572ff6f6SMatthew Dillon
1038572ff6f6SMatthew Dillon /*
1039572ff6f6SMatthew Dillon * Initialize the tables for a node.
1040572ff6f6SMatthew Dillon */
1041572ff6f6SMatthew Dillon static void
ath_rate_ctl_reset(struct ath_softc * sc,struct ieee80211_node * ni)1042572ff6f6SMatthew Dillon ath_rate_ctl_reset(struct ath_softc *sc, struct ieee80211_node *ni)
1043572ff6f6SMatthew Dillon {
1044572ff6f6SMatthew Dillon #define RATE(_ix) (ni->ni_rates.rs_rates[(_ix)] & IEEE80211_RATE_VAL)
1045572ff6f6SMatthew Dillon #define DOT11RATE(_ix) (rt->info[(_ix)].dot11Rate & IEEE80211_RATE_VAL)
1046572ff6f6SMatthew Dillon #define MCS(_ix) (ni->ni_htrates.rs_rates[_ix] | IEEE80211_RATE_MCS)
1047572ff6f6SMatthew Dillon struct ath_node *an = ATH_NODE(ni);
1048572ff6f6SMatthew Dillon struct sample_node *sn = ATH_NODE_SAMPLE(an);
1049572ff6f6SMatthew Dillon const HAL_RATE_TABLE *rt = sc->sc_currates;
1050572ff6f6SMatthew Dillon int x, y, rix;
1051572ff6f6SMatthew Dillon
1052572ff6f6SMatthew Dillon KASSERT(rt != NULL, ("no rate table, mode %u", sc->sc_curmode));
1053572ff6f6SMatthew Dillon
1054572ff6f6SMatthew Dillon KASSERT(sc->sc_curmode < IEEE80211_MODE_MAX+2,
1055572ff6f6SMatthew Dillon ("curmode %u", sc->sc_curmode));
1056572ff6f6SMatthew Dillon
1057572ff6f6SMatthew Dillon sn->sched = mrr_schedules[sc->sc_curmode];
1058572ff6f6SMatthew Dillon KASSERT(sn->sched != NULL,
1059572ff6f6SMatthew Dillon ("no mrr schedule for mode %u", sc->sc_curmode));
1060572ff6f6SMatthew Dillon
1061572ff6f6SMatthew Dillon sn->static_rix = -1;
1062572ff6f6SMatthew Dillon ath_rate_update_static_rix(sc, ni);
1063572ff6f6SMatthew Dillon
1064572ff6f6SMatthew Dillon sn->currates = sc->sc_currates;
1065572ff6f6SMatthew Dillon
1066572ff6f6SMatthew Dillon /*
1067572ff6f6SMatthew Dillon * Construct a bitmask of usable rates. This has all
1068572ff6f6SMatthew Dillon * negotiated rates minus those marked by the hal as
1069572ff6f6SMatthew Dillon * to be ignored for doing rate control.
1070572ff6f6SMatthew Dillon */
1071572ff6f6SMatthew Dillon sn->ratemask = 0;
1072572ff6f6SMatthew Dillon /* MCS rates */
1073572ff6f6SMatthew Dillon if (ni->ni_flags & IEEE80211_NODE_HT) {
1074572ff6f6SMatthew Dillon for (x = 0; x < ni->ni_htrates.rs_nrates; x++) {
1075572ff6f6SMatthew Dillon rix = sc->sc_rixmap[MCS(x)];
1076572ff6f6SMatthew Dillon if (rix == 0xff)
1077572ff6f6SMatthew Dillon continue;
1078572ff6f6SMatthew Dillon /* skip rates marked broken by hal */
1079572ff6f6SMatthew Dillon if (!rt->info[rix].valid)
1080572ff6f6SMatthew Dillon continue;
1081572ff6f6SMatthew Dillon KASSERT(rix < SAMPLE_MAXRATES,
1082572ff6f6SMatthew Dillon ("mcs %u has rix %d", MCS(x), rix));
1083572ff6f6SMatthew Dillon sn->ratemask |= (uint64_t) 1<<rix;
1084572ff6f6SMatthew Dillon }
1085572ff6f6SMatthew Dillon }
1086572ff6f6SMatthew Dillon
1087572ff6f6SMatthew Dillon /* Legacy rates */
1088572ff6f6SMatthew Dillon for (x = 0; x < ni->ni_rates.rs_nrates; x++) {
1089572ff6f6SMatthew Dillon rix = sc->sc_rixmap[RATE(x)];
1090572ff6f6SMatthew Dillon if (rix == 0xff)
1091572ff6f6SMatthew Dillon continue;
1092572ff6f6SMatthew Dillon /* skip rates marked broken by hal */
1093572ff6f6SMatthew Dillon if (!rt->info[rix].valid)
1094572ff6f6SMatthew Dillon continue;
1095572ff6f6SMatthew Dillon KASSERT(rix < SAMPLE_MAXRATES,
1096572ff6f6SMatthew Dillon ("rate %u has rix %d", RATE(x), rix));
1097572ff6f6SMatthew Dillon sn->ratemask |= (uint64_t) 1<<rix;
1098572ff6f6SMatthew Dillon }
1099572ff6f6SMatthew Dillon #ifdef IEEE80211_DEBUG
1100572ff6f6SMatthew Dillon if (ieee80211_msg(ni->ni_vap, IEEE80211_MSG_RATECTL)) {
1101572ff6f6SMatthew Dillon uint64_t mask;
1102572ff6f6SMatthew Dillon
1103dc249793SMatthew Dillon #if defined(__DragonFly__)
1104dc249793SMatthew Dillon ieee80211_note(ni->ni_vap, "[%s] %s: size 1600 rate/tt",
1105dc249793SMatthew Dillon ath_hal_ether_sprintf(ni->ni_macaddr), __func__);
1106dc249793SMatthew Dillon #else
1107b14ca477SMatthew Dillon ieee80211_note(ni->ni_vap, "[%6D] %s: size 1600 rate/tt",
1108b14ca477SMatthew Dillon ni->ni_macaddr, ":", __func__);
1109dc249793SMatthew Dillon #endif
1110572ff6f6SMatthew Dillon for (mask = sn->ratemask, rix = 0; mask != 0; mask >>= 1, rix++) {
1111572ff6f6SMatthew Dillon if ((mask & 1) == 0)
1112572ff6f6SMatthew Dillon continue;
1113dc249793SMatthew Dillon kprintf(" %d %s/%d", dot11rate(rt, rix), dot11rate_label(rt, rix),
1114572ff6f6SMatthew Dillon calc_usecs_unicast_packet(sc, 1600, rix, 0,0,
1115572ff6f6SMatthew Dillon (ni->ni_chw == 40)));
1116572ff6f6SMatthew Dillon }
1117dc249793SMatthew Dillon kprintf("\n");
1118572ff6f6SMatthew Dillon }
1119572ff6f6SMatthew Dillon #endif
1120572ff6f6SMatthew Dillon for (y = 0; y < NUM_PACKET_SIZE_BINS; y++) {
1121572ff6f6SMatthew Dillon int size = bin_to_size(y);
1122572ff6f6SMatthew Dillon uint64_t mask;
1123572ff6f6SMatthew Dillon
1124572ff6f6SMatthew Dillon sn->packets_sent[y] = 0;
1125572ff6f6SMatthew Dillon sn->current_sample_rix[y] = -1;
1126572ff6f6SMatthew Dillon sn->last_sample_rix[y] = 0;
1127572ff6f6SMatthew Dillon /* XXX start with first valid rate */
1128572ff6f6SMatthew Dillon sn->current_rix[y] = ffs(sn->ratemask)-1;
1129572ff6f6SMatthew Dillon
1130572ff6f6SMatthew Dillon /*
1131572ff6f6SMatthew Dillon * Initialize the statistics buckets; these are
1132572ff6f6SMatthew Dillon * indexed by the rate code index.
1133572ff6f6SMatthew Dillon */
1134572ff6f6SMatthew Dillon for (rix = 0, mask = sn->ratemask; mask != 0; rix++, mask >>= 1) {
1135572ff6f6SMatthew Dillon if ((mask & 1) == 0) /* not a valid rate */
1136572ff6f6SMatthew Dillon continue;
1137572ff6f6SMatthew Dillon sn->stats[y][rix].successive_failures = 0;
1138572ff6f6SMatthew Dillon sn->stats[y][rix].tries = 0;
1139572ff6f6SMatthew Dillon sn->stats[y][rix].total_packets = 0;
1140572ff6f6SMatthew Dillon sn->stats[y][rix].packets_acked = 0;
1141572ff6f6SMatthew Dillon sn->stats[y][rix].last_tx = 0;
1142572ff6f6SMatthew Dillon sn->stats[y][rix].ewma_pct = 0;
1143572ff6f6SMatthew Dillon
1144572ff6f6SMatthew Dillon sn->stats[y][rix].perfect_tx_time =
1145572ff6f6SMatthew Dillon calc_usecs_unicast_packet(sc, size, rix, 0, 0,
1146572ff6f6SMatthew Dillon (ni->ni_chw == 40));
1147572ff6f6SMatthew Dillon sn->stats[y][rix].average_tx_time =
1148572ff6f6SMatthew Dillon sn->stats[y][rix].perfect_tx_time;
1149572ff6f6SMatthew Dillon }
1150572ff6f6SMatthew Dillon }
1151572ff6f6SMatthew Dillon #if 0
1152572ff6f6SMatthew Dillon /* XXX 0, num_rates-1 are wrong */
1153572ff6f6SMatthew Dillon IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_RATECTL, ni,
1154572ff6f6SMatthew Dillon "%s: %d rates %d%sMbps (%dus)- %d%sMbps (%dus)", __func__,
1155572ff6f6SMatthew Dillon sn->num_rates,
1156572ff6f6SMatthew Dillon DOT11RATE(0)/2, DOT11RATE(0) % 1 ? ".5" : "",
1157572ff6f6SMatthew Dillon sn->stats[1][0].perfect_tx_time,
1158572ff6f6SMatthew Dillon DOT11RATE(sn->num_rates-1)/2, DOT11RATE(sn->num_rates-1) % 1 ? ".5" : "",
1159572ff6f6SMatthew Dillon sn->stats[1][sn->num_rates-1].perfect_tx_time
1160572ff6f6SMatthew Dillon );
1161572ff6f6SMatthew Dillon #endif
1162572ff6f6SMatthew Dillon /* set the visible bit-rate */
1163572ff6f6SMatthew Dillon if (sn->static_rix != -1)
1164572ff6f6SMatthew Dillon ni->ni_txrate = DOT11RATE(sn->static_rix);
1165572ff6f6SMatthew Dillon else
1166572ff6f6SMatthew Dillon ni->ni_txrate = RATE(0);
1167572ff6f6SMatthew Dillon #undef RATE
1168572ff6f6SMatthew Dillon #undef DOT11RATE
1169572ff6f6SMatthew Dillon }
1170572ff6f6SMatthew Dillon
1171572ff6f6SMatthew Dillon /*
1172572ff6f6SMatthew Dillon * Fetch the statistics for the given node.
1173572ff6f6SMatthew Dillon *
1174572ff6f6SMatthew Dillon * The ieee80211 node must be referenced and unlocked, however the ath_node
1175572ff6f6SMatthew Dillon * must be locked.
1176572ff6f6SMatthew Dillon *
1177572ff6f6SMatthew Dillon * The main difference here is that we convert the rate indexes
1178572ff6f6SMatthew Dillon * to 802.11 rates, or the userland output won't make much sense
1179572ff6f6SMatthew Dillon * as it has no access to the rix table.
1180572ff6f6SMatthew Dillon */
1181572ff6f6SMatthew Dillon int
ath_rate_fetch_node_stats(struct ath_softc * sc,struct ath_node * an,struct ath_rateioctl * rs)1182572ff6f6SMatthew Dillon ath_rate_fetch_node_stats(struct ath_softc *sc, struct ath_node *an,
1183572ff6f6SMatthew Dillon struct ath_rateioctl *rs)
1184572ff6f6SMatthew Dillon {
1185572ff6f6SMatthew Dillon struct sample_node *sn = ATH_NODE_SAMPLE(an);
1186572ff6f6SMatthew Dillon const HAL_RATE_TABLE *rt = sc->sc_currates;
1187572ff6f6SMatthew Dillon struct ath_rateioctl_tlv av;
1188572ff6f6SMatthew Dillon struct ath_rateioctl_rt *tv;
1189572ff6f6SMatthew Dillon int y;
1190572ff6f6SMatthew Dillon int o = 0;
1191572ff6f6SMatthew Dillon
1192572ff6f6SMatthew Dillon ATH_NODE_LOCK_ASSERT(an);
1193572ff6f6SMatthew Dillon
1194572ff6f6SMatthew Dillon /*
1195572ff6f6SMatthew Dillon * Ensure there's enough space for the statistics.
1196572ff6f6SMatthew Dillon */
1197572ff6f6SMatthew Dillon if (rs->len <
1198572ff6f6SMatthew Dillon sizeof(struct ath_rateioctl_tlv) +
1199572ff6f6SMatthew Dillon sizeof(struct ath_rateioctl_rt) +
1200572ff6f6SMatthew Dillon sizeof(struct ath_rateioctl_tlv) +
1201572ff6f6SMatthew Dillon sizeof(struct sample_node)) {
1202572ff6f6SMatthew Dillon device_printf(sc->sc_dev, "%s: len=%d, too short\n",
1203572ff6f6SMatthew Dillon __func__,
1204572ff6f6SMatthew Dillon rs->len);
1205572ff6f6SMatthew Dillon return (EINVAL);
1206572ff6f6SMatthew Dillon }
1207572ff6f6SMatthew Dillon
1208572ff6f6SMatthew Dillon /*
1209572ff6f6SMatthew Dillon * Take a temporary copy of the sample node state so we can
1210572ff6f6SMatthew Dillon * modify it before we copy it.
1211572ff6f6SMatthew Dillon */
1212dc249793SMatthew Dillon #if defined(__DragonFly__)
1213dc249793SMatthew Dillon tv = kmalloc(sizeof(struct ath_rateioctl_rt), M_TEMP,
1214dc249793SMatthew Dillon M_INTWAIT | M_ZERO);
1215dc249793SMatthew Dillon #else
1216b14ca477SMatthew Dillon tv = malloc(sizeof(struct ath_rateioctl_rt), M_TEMP,
1217b14ca477SMatthew Dillon M_NOWAIT | M_ZERO);
1218dc249793SMatthew Dillon #endif
1219572ff6f6SMatthew Dillon if (tv == NULL) {
1220572ff6f6SMatthew Dillon return (ENOMEM);
1221572ff6f6SMatthew Dillon }
1222572ff6f6SMatthew Dillon
1223572ff6f6SMatthew Dillon /*
1224572ff6f6SMatthew Dillon * Populate the rate table mapping TLV.
1225572ff6f6SMatthew Dillon */
1226572ff6f6SMatthew Dillon tv->nentries = rt->rateCount;
1227572ff6f6SMatthew Dillon for (y = 0; y < rt->rateCount; y++) {
1228572ff6f6SMatthew Dillon tv->ratecode[y] = rt->info[y].dot11Rate & IEEE80211_RATE_VAL;
1229572ff6f6SMatthew Dillon if (rt->info[y].phy == IEEE80211_T_HT)
1230572ff6f6SMatthew Dillon tv->ratecode[y] |= IEEE80211_RATE_MCS;
1231572ff6f6SMatthew Dillon }
1232572ff6f6SMatthew Dillon
1233572ff6f6SMatthew Dillon o = 0;
1234572ff6f6SMatthew Dillon /*
1235572ff6f6SMatthew Dillon * First TLV - rate code mapping
1236572ff6f6SMatthew Dillon */
1237572ff6f6SMatthew Dillon av.tlv_id = ATH_RATE_TLV_RATETABLE;
1238572ff6f6SMatthew Dillon av.tlv_len = sizeof(struct ath_rateioctl_rt);
1239572ff6f6SMatthew Dillon copyout(&av, rs->buf + o, sizeof(struct ath_rateioctl_tlv));
1240572ff6f6SMatthew Dillon o += sizeof(struct ath_rateioctl_tlv);
1241572ff6f6SMatthew Dillon copyout(tv, rs->buf + o, sizeof(struct ath_rateioctl_rt));
1242572ff6f6SMatthew Dillon o += sizeof(struct ath_rateioctl_rt);
1243572ff6f6SMatthew Dillon
1244572ff6f6SMatthew Dillon /*
1245572ff6f6SMatthew Dillon * Second TLV - sample node statistics
1246572ff6f6SMatthew Dillon */
1247572ff6f6SMatthew Dillon av.tlv_id = ATH_RATE_TLV_SAMPLENODE;
1248572ff6f6SMatthew Dillon av.tlv_len = sizeof(struct sample_node);
1249572ff6f6SMatthew Dillon copyout(&av, rs->buf + o, sizeof(struct ath_rateioctl_tlv));
1250572ff6f6SMatthew Dillon o += sizeof(struct ath_rateioctl_tlv);
1251572ff6f6SMatthew Dillon
1252572ff6f6SMatthew Dillon /*
1253572ff6f6SMatthew Dillon * Copy the statistics over to the provided buffer.
1254572ff6f6SMatthew Dillon */
1255572ff6f6SMatthew Dillon copyout(sn, rs->buf + o, sizeof(struct sample_node));
1256572ff6f6SMatthew Dillon o += sizeof(struct sample_node);
1257572ff6f6SMatthew Dillon
1258dc249793SMatthew Dillon kfree(tv, M_TEMP);
1259572ff6f6SMatthew Dillon
1260572ff6f6SMatthew Dillon return (0);
1261572ff6f6SMatthew Dillon }
1262572ff6f6SMatthew Dillon
1263572ff6f6SMatthew Dillon static void
sample_stats(void * arg,struct ieee80211_node * ni)1264572ff6f6SMatthew Dillon sample_stats(void *arg, struct ieee80211_node *ni)
1265572ff6f6SMatthew Dillon {
1266572ff6f6SMatthew Dillon struct ath_softc *sc = arg;
1267572ff6f6SMatthew Dillon const HAL_RATE_TABLE *rt = sc->sc_currates;
1268572ff6f6SMatthew Dillon struct sample_node *sn = ATH_NODE_SAMPLE(ATH_NODE(ni));
1269572ff6f6SMatthew Dillon uint64_t mask;
1270572ff6f6SMatthew Dillon int rix, y;
1271572ff6f6SMatthew Dillon
1272dc249793SMatthew Dillon kprintf("\n[%s] refcnt %d static_rix (%d %s) ratemask 0x%jx\n",
1273848b370cSMatthew Dillon ether_sprintf(ni->ni_macaddr), ieee80211_node_refcnt(ni),
1274572ff6f6SMatthew Dillon dot11rate(rt, sn->static_rix),
1275572ff6f6SMatthew Dillon dot11rate_label(rt, sn->static_rix),
1276572ff6f6SMatthew Dillon (uintmax_t)sn->ratemask);
1277572ff6f6SMatthew Dillon for (y = 0; y < NUM_PACKET_SIZE_BINS; y++) {
1278dc249793SMatthew Dillon kprintf("[%4u] cur rix %d (%d %s) since switch: packets %d ticks %u\n",
1279572ff6f6SMatthew Dillon bin_to_size(y), sn->current_rix[y],
1280572ff6f6SMatthew Dillon dot11rate(rt, sn->current_rix[y]),
1281572ff6f6SMatthew Dillon dot11rate_label(rt, sn->current_rix[y]),
1282572ff6f6SMatthew Dillon sn->packets_since_switch[y], sn->ticks_since_switch[y]);
1283dc249793SMatthew Dillon kprintf("[%4u] last sample (%d %s) cur sample (%d %s) packets sent %d\n",
1284572ff6f6SMatthew Dillon bin_to_size(y),
1285572ff6f6SMatthew Dillon dot11rate(rt, sn->last_sample_rix[y]),
1286572ff6f6SMatthew Dillon dot11rate_label(rt, sn->last_sample_rix[y]),
1287572ff6f6SMatthew Dillon dot11rate(rt, sn->current_sample_rix[y]),
1288572ff6f6SMatthew Dillon dot11rate_label(rt, sn->current_sample_rix[y]),
1289572ff6f6SMatthew Dillon sn->packets_sent[y]);
1290dc249793SMatthew Dillon kprintf("[%4u] packets since sample %d sample tt %u\n",
1291572ff6f6SMatthew Dillon bin_to_size(y), sn->packets_since_sample[y],
1292572ff6f6SMatthew Dillon sn->sample_tt[y]);
1293572ff6f6SMatthew Dillon }
1294572ff6f6SMatthew Dillon for (mask = sn->ratemask, rix = 0; mask != 0; mask >>= 1, rix++) {
1295572ff6f6SMatthew Dillon if ((mask & 1) == 0)
1296572ff6f6SMatthew Dillon continue;
1297572ff6f6SMatthew Dillon for (y = 0; y < NUM_PACKET_SIZE_BINS; y++) {
1298572ff6f6SMatthew Dillon if (sn->stats[y][rix].total_packets == 0)
1299572ff6f6SMatthew Dillon continue;
1300dc249793SMatthew Dillon kprintf("[%2u %s:%4u] %8ju:%-8ju (%3d%%) (EWMA %3d.%1d%%) T %8ju F %4d avg %5u last %u\n",
1301572ff6f6SMatthew Dillon dot11rate(rt, rix), dot11rate_label(rt, rix),
1302572ff6f6SMatthew Dillon bin_to_size(y),
1303572ff6f6SMatthew Dillon (uintmax_t) sn->stats[y][rix].total_packets,
1304572ff6f6SMatthew Dillon (uintmax_t) sn->stats[y][rix].packets_acked,
1305572ff6f6SMatthew Dillon (int) ((sn->stats[y][rix].packets_acked * 100ULL) /
1306572ff6f6SMatthew Dillon sn->stats[y][rix].total_packets),
1307572ff6f6SMatthew Dillon sn->stats[y][rix].ewma_pct / 10,
1308572ff6f6SMatthew Dillon sn->stats[y][rix].ewma_pct % 10,
1309572ff6f6SMatthew Dillon (uintmax_t) sn->stats[y][rix].tries,
1310572ff6f6SMatthew Dillon sn->stats[y][rix].successive_failures,
1311572ff6f6SMatthew Dillon sn->stats[y][rix].average_tx_time,
1312572ff6f6SMatthew Dillon ticks - sn->stats[y][rix].last_tx);
1313572ff6f6SMatthew Dillon }
1314572ff6f6SMatthew Dillon }
1315572ff6f6SMatthew Dillon }
1316572ff6f6SMatthew Dillon
1317572ff6f6SMatthew Dillon static int
ath_rate_sysctl_stats(SYSCTL_HANDLER_ARGS)1318572ff6f6SMatthew Dillon ath_rate_sysctl_stats(SYSCTL_HANDLER_ARGS)
1319572ff6f6SMatthew Dillon {
1320572ff6f6SMatthew Dillon struct ath_softc *sc = arg1;
1321b14ca477SMatthew Dillon struct ieee80211com *ic = &sc->sc_ic;
1322572ff6f6SMatthew Dillon int error, v;
1323572ff6f6SMatthew Dillon
1324572ff6f6SMatthew Dillon v = 0;
1325572ff6f6SMatthew Dillon error = sysctl_handle_int(oidp, &v, 0, req);
1326572ff6f6SMatthew Dillon if (error || !req->newptr)
1327572ff6f6SMatthew Dillon return error;
1328572ff6f6SMatthew Dillon ieee80211_iterate_nodes(&ic->ic_sta, sample_stats, sc);
1329572ff6f6SMatthew Dillon return 0;
1330572ff6f6SMatthew Dillon }
1331572ff6f6SMatthew Dillon
1332572ff6f6SMatthew Dillon static int
ath_rate_sysctl_smoothing_rate(SYSCTL_HANDLER_ARGS)1333572ff6f6SMatthew Dillon ath_rate_sysctl_smoothing_rate(SYSCTL_HANDLER_ARGS)
1334572ff6f6SMatthew Dillon {
1335572ff6f6SMatthew Dillon struct sample_softc *ssc = arg1;
1336572ff6f6SMatthew Dillon int rate, error;
1337572ff6f6SMatthew Dillon
1338572ff6f6SMatthew Dillon rate = ssc->smoothing_rate;
1339572ff6f6SMatthew Dillon error = sysctl_handle_int(oidp, &rate, 0, req);
1340572ff6f6SMatthew Dillon if (error || !req->newptr)
1341572ff6f6SMatthew Dillon return error;
1342572ff6f6SMatthew Dillon if (!(0 <= rate && rate < 100))
1343572ff6f6SMatthew Dillon return EINVAL;
1344572ff6f6SMatthew Dillon ssc->smoothing_rate = rate;
1345572ff6f6SMatthew Dillon ssc->smoothing_minpackets = 100 / (100 - rate);
1346572ff6f6SMatthew Dillon return 0;
1347572ff6f6SMatthew Dillon }
1348572ff6f6SMatthew Dillon
1349572ff6f6SMatthew Dillon static int
ath_rate_sysctl_sample_rate(SYSCTL_HANDLER_ARGS)1350572ff6f6SMatthew Dillon ath_rate_sysctl_sample_rate(SYSCTL_HANDLER_ARGS)
1351572ff6f6SMatthew Dillon {
1352572ff6f6SMatthew Dillon struct sample_softc *ssc = arg1;
1353572ff6f6SMatthew Dillon int rate, error;
1354572ff6f6SMatthew Dillon
1355572ff6f6SMatthew Dillon rate = ssc->sample_rate;
1356572ff6f6SMatthew Dillon error = sysctl_handle_int(oidp, &rate, 0, req);
1357572ff6f6SMatthew Dillon if (error || !req->newptr)
1358572ff6f6SMatthew Dillon return error;
1359572ff6f6SMatthew Dillon if (!(2 <= rate && rate <= 100))
1360572ff6f6SMatthew Dillon return EINVAL;
1361572ff6f6SMatthew Dillon ssc->sample_rate = rate;
1362572ff6f6SMatthew Dillon return 0;
1363572ff6f6SMatthew Dillon }
1364572ff6f6SMatthew Dillon
1365572ff6f6SMatthew Dillon static void
ath_rate_sysctlattach(struct ath_softc * sc,struct sample_softc * ssc)1366572ff6f6SMatthew Dillon ath_rate_sysctlattach(struct ath_softc *sc, struct sample_softc *ssc)
1367572ff6f6SMatthew Dillon {
136826595b18SSascha Wildner struct sysctl_ctx_list *ctx = device_get_sysctl_ctx(sc->sc_dev);
136926595b18SSascha Wildner struct sysctl_oid *tree = device_get_sysctl_tree(sc->sc_dev);
1370572ff6f6SMatthew Dillon
1371572ff6f6SMatthew Dillon SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
1372572ff6f6SMatthew Dillon "smoothing_rate", CTLTYPE_INT | CTLFLAG_RW, ssc, 0,
1373572ff6f6SMatthew Dillon ath_rate_sysctl_smoothing_rate, "I",
1374572ff6f6SMatthew Dillon "sample: smoothing rate for avg tx time (%%)");
1375572ff6f6SMatthew Dillon SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
1376572ff6f6SMatthew Dillon "sample_rate", CTLTYPE_INT | CTLFLAG_RW, ssc, 0,
1377572ff6f6SMatthew Dillon ath_rate_sysctl_sample_rate, "I",
1378572ff6f6SMatthew Dillon "sample: percent air time devoted to sampling new rates (%%)");
1379572ff6f6SMatthew Dillon /* XXX max_successive_failures, stale_failure_timeout, min_switch */
1380572ff6f6SMatthew Dillon SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
1381572ff6f6SMatthew Dillon "sample_stats", CTLTYPE_INT | CTLFLAG_RW, sc, 0,
1382572ff6f6SMatthew Dillon ath_rate_sysctl_stats, "I", "sample: print statistics");
1383572ff6f6SMatthew Dillon }
1384572ff6f6SMatthew Dillon
1385572ff6f6SMatthew Dillon struct ath_ratectrl *
ath_rate_attach(struct ath_softc * sc)1386572ff6f6SMatthew Dillon ath_rate_attach(struct ath_softc *sc)
1387572ff6f6SMatthew Dillon {
1388572ff6f6SMatthew Dillon struct sample_softc *ssc;
1389572ff6f6SMatthew Dillon
1390dc249793SMatthew Dillon #if defined(__DragonFly__)
1391dc249793SMatthew Dillon ssc = kmalloc(sizeof(struct sample_softc), M_DEVBUF, M_INTWAIT|M_ZERO);
1392dc249793SMatthew Dillon #else
1393b14ca477SMatthew Dillon ssc = malloc(sizeof(struct sample_softc), M_DEVBUF, M_NOWAIT|M_ZERO);
1394dc249793SMatthew Dillon #endif
1395572ff6f6SMatthew Dillon if (ssc == NULL)
1396572ff6f6SMatthew Dillon return NULL;
1397572ff6f6SMatthew Dillon ssc->arc.arc_space = sizeof(struct sample_node);
1398572ff6f6SMatthew Dillon ssc->smoothing_rate = 75; /* ewma percentage ([0..99]) */
1399572ff6f6SMatthew Dillon ssc->smoothing_minpackets = 100 / (100 - ssc->smoothing_rate);
1400572ff6f6SMatthew Dillon ssc->sample_rate = 10; /* %time to try diff tx rates */
1401572ff6f6SMatthew Dillon ssc->max_successive_failures = 3; /* threshold for rate sampling*/
1402572ff6f6SMatthew Dillon ssc->stale_failure_timeout = 10 * hz; /* 10 seconds */
1403572ff6f6SMatthew Dillon ssc->min_switch = hz; /* 1 second */
1404572ff6f6SMatthew Dillon ath_rate_sysctlattach(sc, ssc);
1405572ff6f6SMatthew Dillon return &ssc->arc;
1406572ff6f6SMatthew Dillon }
1407572ff6f6SMatthew Dillon
1408572ff6f6SMatthew Dillon void
ath_rate_detach(struct ath_ratectrl * arc)1409572ff6f6SMatthew Dillon ath_rate_detach(struct ath_ratectrl *arc)
1410572ff6f6SMatthew Dillon {
1411572ff6f6SMatthew Dillon struct sample_softc *ssc = (struct sample_softc *) arc;
1412572ff6f6SMatthew Dillon
1413dc249793SMatthew Dillon kfree(ssc, M_DEVBUF);
1414572ff6f6SMatthew Dillon }
1415dc249793SMatthew Dillon
1416dc249793SMatthew Dillon #if defined(__DragonFly__)
1417dc249793SMatthew Dillon
1418dc249793SMatthew Dillon /*
1419dc249793SMatthew Dillon * Module glue.
1420dc249793SMatthew Dillon */
1421dc249793SMatthew Dillon static int
sample_modevent(module_t mod,int type,void * unused)1422dc249793SMatthew Dillon sample_modevent(module_t mod, int type, void *unused)
1423dc249793SMatthew Dillon {
1424dc249793SMatthew Dillon int error;
1425dc249793SMatthew Dillon
1426dc249793SMatthew Dillon wlan_serialize_enter();
1427dc249793SMatthew Dillon
1428dc249793SMatthew Dillon switch (type) {
1429dc249793SMatthew Dillon case MOD_LOAD:
1430dc249793SMatthew Dillon if (bootverbose) {
1431dc249793SMatthew Dillon kprintf("ath_rate: <SampleRate bit-rate "
1432dc249793SMatthew Dillon "selection algorithm>\n");
1433dc249793SMatthew Dillon }
1434dc249793SMatthew Dillon error = 0;
1435dc249793SMatthew Dillon break;
1436dc249793SMatthew Dillon case MOD_UNLOAD:
1437dc249793SMatthew Dillon error = 0;
1438dc249793SMatthew Dillon break;
1439dc249793SMatthew Dillon default:
1440dc249793SMatthew Dillon error = EINVAL;
1441dc249793SMatthew Dillon break;
1442dc249793SMatthew Dillon }
1443dc249793SMatthew Dillon wlan_serialize_exit();
1444dc249793SMatthew Dillon
1445dc249793SMatthew Dillon return error;
1446dc249793SMatthew Dillon }
1447dc249793SMatthew Dillon
1448dc249793SMatthew Dillon static moduledata_t sample_mod = {
1449dc249793SMatthew Dillon "ath_rate",
1450dc249793SMatthew Dillon sample_modevent,
1451dc249793SMatthew Dillon 0
1452dc249793SMatthew Dillon };
1453dc249793SMatthew Dillon
1454dc249793SMatthew Dillon DECLARE_MODULE(ath_rate, sample_mod, SI_SUB_DRIVERS, SI_ORDER_FIRST);
1455dc249793SMatthew Dillon MODULE_VERSION(ath_rate, 1);
1456dc249793SMatthew Dillon MODULE_DEPEND(ath_rate, ath_hal, 1, 1, 1);
1457dc249793SMatthew Dillon MODULE_DEPEND(ath_rate, wlan, 1, 1, 1);
1458dc249793SMatthew Dillon
1459dc249793SMatthew Dillon #endif
1460