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