xref: /freebsd/sys/dev/ath/ath_rate/sample/sample.c (revision 884a2a699669ec61e2366e3e358342dbc94be24a)
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 static const char *
154 dot11rate_label(const HAL_RATE_TABLE *rt, int rix)
155 {
156 	return rt->info[rix].phy == IEEE80211_T_HT ? "MCS" : "Mb ";
157 }
158 
159 /*
160  * Return the rix with the lowest average_tx_time,
161  * or -1 if all the average_tx_times are 0.
162  */
163 static __inline int
164 pick_best_rate(struct ath_node *an, const HAL_RATE_TABLE *rt,
165     int size_bin, int require_acked_before)
166 {
167 	struct sample_node *sn = ATH_NODE_SAMPLE(an);
168         int best_rate_rix, best_rate_tt;
169 	uint32_t mask;
170 	int rix, tt;
171 
172         best_rate_rix = 0;
173         best_rate_tt = 0;
174 	for (mask = sn->ratemask, rix = 0; mask != 0; mask >>= 1, rix++) {
175 		if ((mask & 1) == 0)		/* not a supported rate */
176 			continue;
177 
178 		/* Don't pick a non-HT rate for a HT node */
179 		if ((an->an_node.ni_flags & IEEE80211_NODE_HT) &&
180 		    (rt->info[rix].phy != IEEE80211_T_HT)) {
181 			continue;
182 		}
183 
184 		tt = sn->stats[size_bin][rix].average_tx_time;
185 		if (tt <= 0 ||
186 		    (require_acked_before &&
187 		     !sn->stats[size_bin][rix].packets_acked))
188 			continue;
189 
190 		/* don't use a bit-rate that has been failing */
191 		if (sn->stats[size_bin][rix].successive_failures > 3)
192 			continue;
193 
194 		if (best_rate_tt == 0 || tt < best_rate_tt) {
195 			best_rate_tt = tt;
196 			best_rate_rix = rix;
197 		}
198         }
199         return (best_rate_tt ? best_rate_rix : -1);
200 }
201 
202 /*
203  * Pick a good "random" bit-rate to sample other than the current one.
204  */
205 static __inline int
206 pick_sample_rate(struct sample_softc *ssc , struct ath_node *an,
207     const HAL_RATE_TABLE *rt, int size_bin)
208 {
209 #define	DOT11RATE(ix)	(rt->info[ix].dot11Rate & IEEE80211_RATE_VAL)
210 #define	MCS(ix)		(rt->info[ix].dot11Rate | IEEE80211_RATE_MCS)
211 	struct sample_node *sn = ATH_NODE_SAMPLE(an);
212 	int current_rix, rix;
213 	unsigned current_tt;
214 	uint32_t mask;
215 
216 	current_rix = sn->current_rix[size_bin];
217 	if (current_rix < 0) {
218 		/* no successes yet, send at the lowest bit-rate */
219 		/* XXX should return MCS0 if HT */
220 		return 0;
221 	}
222 
223 	current_tt = sn->stats[size_bin][current_rix].average_tx_time;
224 
225 	rix = sn->last_sample_rix[size_bin]+1;	/* next sample rate */
226 	mask = sn->ratemask &~ (1<<current_rix);/* don't sample current rate */
227 	while (mask != 0) {
228 		if ((mask & (1<<rix)) == 0) {	/* not a supported rate */
229 	nextrate:
230 			if (++rix >= rt->rateCount)
231 				rix = 0;
232 			continue;
233 		}
234 
235 		/* if the node is HT and the rate isn't HT, don't bother sample */
236 		if ((an->an_node.ni_flags & IEEE80211_NODE_HT) &&
237 		    (rt->info[rix].phy != IEEE80211_T_HT)) {
238 			mask &= ~(1<<rix);
239 			goto nextrate;
240 		}
241 
242 		/* this bit-rate is always worse than the current one */
243 		if (sn->stats[size_bin][rix].perfect_tx_time > current_tt) {
244 			mask &= ~(1<<rix);
245 			goto nextrate;
246 		}
247 
248 		/* rarely sample bit-rates that fail a lot */
249 		if (sn->stats[size_bin][rix].successive_failures > ssc->max_successive_failures &&
250 		    ticks - sn->stats[size_bin][rix].last_tx < ssc->stale_failure_timeout) {
251 			mask &= ~(1<<rix);
252 			goto nextrate;
253 		}
254 
255 		/* Don't sample more than 2 rates higher for rates > 11M for non-HT rates */
256 		if (! (an->an_node.ni_flags & IEEE80211_NODE_HT)) {
257 			if (DOT11RATE(rix) > 2*11 && rix > current_rix + 2) {
258 				mask &= ~(1<<rix);
259 				goto nextrate;
260 			}
261 		}
262 
263 		sn->last_sample_rix[size_bin] = rix;
264 		return rix;
265 	}
266 	return current_rix;
267 #undef DOT11RATE
268 #undef	MCS
269 }
270 
271 static int
272 ath_rate_get_static_rix(struct ath_softc *sc, const struct ieee80211_node *ni)
273 {
274 #define	RATE(_ix)	(ni->ni_rates.rs_rates[(_ix)] & IEEE80211_RATE_VAL)
275 #define	DOT11RATE(_ix)	(rt->info[(_ix)].dot11Rate & IEEE80211_RATE_VAL)
276 #define	MCS(_ix)	(ni->ni_htrates.rs_rates[_ix] | IEEE80211_RATE_MCS)
277 	const struct ieee80211_txparam *tp = ni->ni_txparms;
278 	int srate;
279 
280 	/* Check MCS rates */
281 	for (srate = ni->ni_htrates.rs_nrates - 1; srate >= 0; srate--) {
282 		if (MCS(srate) == tp->ucastrate)
283 			return sc->sc_rixmap[tp->ucastrate];
284 	}
285 
286 	/* Check legacy rates */
287 	for (srate = ni->ni_rates.rs_nrates - 1; srate >= 0; srate--) {
288 		if (RATE(srate) == tp->ucastrate)
289 			return sc->sc_rixmap[tp->ucastrate];
290 	}
291 	return -1;
292 #undef	RATE
293 #undef	DOT11RATE
294 #undef	MCS
295 }
296 
297 static void
298 ath_rate_update_static_rix(struct ath_softc *sc, struct ieee80211_node *ni)
299 {
300 	struct ath_node *an = ATH_NODE(ni);
301 	const struct ieee80211_txparam *tp = ni->ni_txparms;
302 	struct sample_node *sn = ATH_NODE_SAMPLE(an);
303 
304 	if (tp != NULL && tp->ucastrate != IEEE80211_FIXED_RATE_NONE) {
305 		/*
306 		 * A fixed rate is to be used; ucastrate is the IEEE code
307 		 * for this rate (sans basic bit).  Check this against the
308 		 * negotiated rate set for the node.  Note the fixed rate
309 		 * may not be available for various reasons so we only
310 		 * setup the static rate index if the lookup is successful.
311 		 */
312 		sn->static_rix = ath_rate_get_static_rix(sc, ni);
313 	} else {
314 		sn->static_rix = -1;
315 	}
316 }
317 
318 
319 
320 void
321 ath_rate_findrate(struct ath_softc *sc, struct ath_node *an,
322 		  int shortPreamble, size_t frameLen,
323 		  u_int8_t *rix0, int *try0, u_int8_t *txrate)
324 {
325 #define	DOT11RATE(ix)	(rt->info[ix].dot11Rate & IEEE80211_RATE_VAL)
326 #define	MCS(ix)		(rt->info[ix].dot11Rate | IEEE80211_RATE_MCS)
327 #define	RATE(ix)	(DOT11RATE(ix) / 2)
328 	struct sample_node *sn = ATH_NODE_SAMPLE(an);
329 	struct sample_softc *ssc = ATH_SOFTC_SAMPLE(sc);
330 	struct ifnet *ifp = sc->sc_ifp;
331 	struct ieee80211com *ic = ifp->if_l2com;
332 	const HAL_RATE_TABLE *rt = sc->sc_currates;
333 	const int size_bin = size_to_bin(frameLen);
334 	int rix, mrr, best_rix, change_rates;
335 	unsigned average_tx_time;
336 
337 	ath_rate_update_static_rix(sc, &an->an_node);
338 
339 	if (sn->static_rix != -1) {
340 		rix = sn->static_rix;
341 		*try0 = ATH_TXMAXTRY;
342 		goto done;
343 	}
344 
345 	/* XXX TODO: this doesn't know about 11gn vs 11g protection; teach it */
346 	mrr = sc->sc_mrretry && !(ic->ic_flags & IEEE80211_F_USEPROT);
347 
348 	best_rix = pick_best_rate(an, rt, size_bin, !mrr);
349 	if (best_rix >= 0) {
350 		average_tx_time = sn->stats[size_bin][best_rix].average_tx_time;
351 	} else {
352 		average_tx_time = 0;
353 	}
354 	/*
355 	 * Limit the time measuring the performance of other tx
356 	 * rates to sample_rate% of the total transmission time.
357 	 */
358 	if (sn->sample_tt[size_bin] < average_tx_time * (sn->packets_since_sample[size_bin]*ssc->sample_rate/100)) {
359 		rix = pick_sample_rate(ssc, an, rt, size_bin);
360 		IEEE80211_NOTE(an->an_node.ni_vap, IEEE80211_MSG_RATECTL,
361 		     &an->an_node, "size %u sample rate %d current rate %d",
362 		     bin_to_size(size_bin), RATE(rix),
363 		     RATE(sn->current_rix[size_bin]));
364 		if (rix != sn->current_rix[size_bin]) {
365 			sn->current_sample_rix[size_bin] = rix;
366 		} else {
367 			sn->current_sample_rix[size_bin] = -1;
368 		}
369 		sn->packets_since_sample[size_bin] = 0;
370 	} else {
371 		change_rates = 0;
372 		if (!sn->packets_sent[size_bin] || best_rix == -1) {
373 			/* no packet has been sent successfully yet */
374 			for (rix = rt->rateCount-1; rix > 0; rix--) {
375 				if ((sn->ratemask & (1<<rix)) == 0)
376 					continue;
377 				/*
378 				 * Pick the highest rate <= 36 Mbps
379 				 * that hasn't failed.
380 				 */
381 				if (DOT11RATE(rix) <= 72 &&
382 				    sn->stats[size_bin][rix].successive_failures == 0) {
383 					break;
384 				}
385 			}
386 			change_rates = 1;
387 			best_rix = rix;
388 		} else if (sn->packets_sent[size_bin] < 20) {
389 			/* let the bit-rate switch quickly during the first few packets */
390 			change_rates = 1;
391 		} else if (ticks - ssc->min_switch > sn->ticks_since_switch[size_bin]) {
392 			/* min_switch seconds have gone by */
393 			change_rates = 1;
394 		} else if (2*average_tx_time < sn->stats[size_bin][sn->current_rix[size_bin]].average_tx_time) {
395 			/* the current bit-rate is twice as slow as the best one */
396 			change_rates = 1;
397 		}
398 
399 		sn->packets_since_sample[size_bin]++;
400 
401 		if (change_rates) {
402 			if (best_rix != sn->current_rix[size_bin]) {
403 				IEEE80211_NOTE(an->an_node.ni_vap,
404 				    IEEE80211_MSG_RATECTL,
405 				    &an->an_node,
406 "%s: size %d switch rate %d (%d/%d) -> %d (%d/%d) after %d packets mrr %d",
407 				    __func__,
408 				    bin_to_size(size_bin),
409 				    RATE(sn->current_rix[size_bin]),
410 				    sn->stats[size_bin][sn->current_rix[size_bin]].average_tx_time,
411 				    sn->stats[size_bin][sn->current_rix[size_bin]].perfect_tx_time,
412 				    RATE(best_rix),
413 				    sn->stats[size_bin][best_rix].average_tx_time,
414 				    sn->stats[size_bin][best_rix].perfect_tx_time,
415 				    sn->packets_since_switch[size_bin],
416 				    mrr);
417 			}
418 			sn->packets_since_switch[size_bin] = 0;
419 			sn->current_rix[size_bin] = best_rix;
420 			sn->ticks_since_switch[size_bin] = ticks;
421 			/*
422 			 * Set the visible txrate for this node.
423 			 */
424 			an->an_node.ni_txrate = (rt->info[best_rix].phy == IEEE80211_T_HT) ?  MCS(best_rix) : DOT11RATE(best_rix);
425 		}
426 		rix = sn->current_rix[size_bin];
427 		sn->packets_since_switch[size_bin]++;
428 	}
429 	*try0 = mrr ? sn->sched[rix].t0 : ATH_TXMAXTRY;
430 done:
431 	KASSERT(rix >= 0 && rix < rt->rateCount, ("rix is %d", rix));
432 
433 	*rix0 = rix;
434 	*txrate = rt->info[rix].rateCode
435 		| (shortPreamble ? rt->info[rix].shortPreamble : 0);
436 	sn->packets_sent[size_bin]++;
437 #undef DOT11RATE
438 #undef MCS
439 #undef RATE
440 }
441 
442 /*
443  * Get the TX rates. Don't fiddle with short preamble flags for them;
444  * the caller can do that.
445  */
446 void
447 ath_rate_getxtxrates(struct ath_softc *sc, struct ath_node *an,
448     uint8_t rix0, uint8_t *rix, uint8_t *try)
449 {
450 	struct sample_node *sn = ATH_NODE_SAMPLE(an);
451 	const struct txschedule *sched = &sn->sched[rix0];
452 
453 	KASSERT(rix0 == sched->r0, ("rix0 (%x) != sched->r0 (%x)!\n", rix0, sched->r0));
454 
455 /*	rix[0] = sched->r0; */
456 	rix[1] = sched->r1;
457 	rix[2] = sched->r2;
458 	rix[3] = sched->r3;
459 
460 	try[0] = sched->t0;
461 	try[1] = sched->t1;
462 	try[2] = sched->t2;
463 	try[3] = sched->t3;
464 }
465 
466 void
467 ath_rate_setupxtxdesc(struct ath_softc *sc, struct ath_node *an,
468 		      struct ath_desc *ds, int shortPreamble, u_int8_t rix)
469 {
470 	struct sample_node *sn = ATH_NODE_SAMPLE(an);
471 	const struct txschedule *sched = &sn->sched[rix];
472 	const HAL_RATE_TABLE *rt = sc->sc_currates;
473 	uint8_t rix1, s1code, rix2, s2code, rix3, s3code;
474 
475 	/* XXX precalculate short preamble tables */
476 	rix1 = sched->r1;
477 	s1code = rt->info[rix1].rateCode
478 	       | (shortPreamble ? rt->info[rix1].shortPreamble : 0);
479 	rix2 = sched->r2;
480 	s2code = rt->info[rix2].rateCode
481 	       | (shortPreamble ? rt->info[rix2].shortPreamble : 0);
482 	rix3 = sched->r3;
483 	s3code = rt->info[rix3].rateCode
484 	       | (shortPreamble ? rt->info[rix3].shortPreamble : 0);
485 	ath_hal_setupxtxdesc(sc->sc_ah, ds,
486 	    s1code, sched->t1,		/* series 1 */
487 	    s2code, sched->t2,		/* series 2 */
488 	    s3code, sched->t3);		/* series 3 */
489 }
490 
491 static void
492 update_stats(struct ath_softc *sc, struct ath_node *an,
493 		  int frame_size,
494 		  int rix0, int tries0,
495 		  int rix1, int tries1,
496 		  int rix2, int tries2,
497 		  int rix3, int tries3,
498 		  int short_tries, int tries, int status)
499 {
500 	struct sample_node *sn = ATH_NODE_SAMPLE(an);
501 	struct sample_softc *ssc = ATH_SOFTC_SAMPLE(sc);
502 	const int size_bin = size_to_bin(frame_size);
503 	const int size = bin_to_size(size_bin);
504 	int tt, tries_so_far;
505 	int is_ht40 = (an->an_node.ni_chw == 40);
506 
507 	if (!IS_RATE_DEFINED(sn, rix0))
508 		return;
509 	tt = calc_usecs_unicast_packet(sc, size, rix0, short_tries,
510 		MIN(tries0, tries) - 1, is_ht40);
511 	tries_so_far = tries0;
512 
513 	if (tries1 && tries_so_far < tries) {
514 		if (!IS_RATE_DEFINED(sn, rix1))
515 			return;
516 		tt += calc_usecs_unicast_packet(sc, size, rix1, short_tries,
517 			MIN(tries1 + tries_so_far, tries) - tries_so_far - 1, is_ht40);
518 		tries_so_far += tries1;
519 	}
520 
521 	if (tries2 && tries_so_far < tries) {
522 		if (!IS_RATE_DEFINED(sn, rix2))
523 			return;
524 		tt += calc_usecs_unicast_packet(sc, size, rix2, short_tries,
525 			MIN(tries2 + tries_so_far, tries) - tries_so_far - 1, is_ht40);
526 		tries_so_far += tries2;
527 	}
528 
529 	if (tries3 && tries_so_far < tries) {
530 		if (!IS_RATE_DEFINED(sn, rix3))
531 			return;
532 		tt += calc_usecs_unicast_packet(sc, size, rix3, short_tries,
533 			MIN(tries3 + tries_so_far, tries) - tries_so_far - 1, is_ht40);
534 	}
535 
536 	if (sn->stats[size_bin][rix0].total_packets < ssc->smoothing_minpackets) {
537 		/* just average the first few packets */
538 		int avg_tx = sn->stats[size_bin][rix0].average_tx_time;
539 		int packets = sn->stats[size_bin][rix0].total_packets;
540 		sn->stats[size_bin][rix0].average_tx_time = (tt+(avg_tx*packets))/(packets+1);
541 	} else {
542 		/* use a ewma */
543 		sn->stats[size_bin][rix0].average_tx_time =
544 			((sn->stats[size_bin][rix0].average_tx_time * ssc->smoothing_rate) +
545 			 (tt * (100 - ssc->smoothing_rate))) / 100;
546 	}
547 
548 	if (status != 0) {
549 		int y;
550 		sn->stats[size_bin][rix0].successive_failures++;
551 		for (y = size_bin+1; y < NUM_PACKET_SIZE_BINS; y++) {
552 			/*
553 			 * Also say larger packets failed since we
554 			 * assume if a small packet fails at a
555 			 * bit-rate then a larger one will also.
556 			 */
557 			sn->stats[y][rix0].successive_failures++;
558 			sn->stats[y][rix0].last_tx = ticks;
559 			sn->stats[y][rix0].tries += tries;
560 			sn->stats[y][rix0].total_packets++;
561 		}
562 	} else {
563 		sn->stats[size_bin][rix0].packets_acked++;
564 		sn->stats[size_bin][rix0].successive_failures = 0;
565 	}
566 	sn->stats[size_bin][rix0].tries += tries;
567 	sn->stats[size_bin][rix0].last_tx = ticks;
568 	sn->stats[size_bin][rix0].total_packets++;
569 
570 	if (rix0 == sn->current_sample_rix[size_bin]) {
571 		IEEE80211_NOTE(an->an_node.ni_vap, IEEE80211_MSG_RATECTL,
572 		   &an->an_node,
573 "%s: size %d %s sample rate %d tries (%d/%d) tt %d avg_tt (%d/%d)",
574 		    __func__,
575 		    size,
576 		    status ? "FAIL" : "OK",
577 		    rix0, short_tries, tries, tt,
578 		    sn->stats[size_bin][rix0].average_tx_time,
579 		    sn->stats[size_bin][rix0].perfect_tx_time);
580 		sn->sample_tt[size_bin] = tt;
581 		sn->current_sample_rix[size_bin] = -1;
582 	}
583 }
584 
585 static void
586 badrate(struct ifnet *ifp, int series, int hwrate, int tries, int status)
587 {
588 	if_printf(ifp, "bad series%d hwrate 0x%x, tries %u ts_status 0x%x\n",
589 	    series, hwrate, tries, status);
590 }
591 
592 void
593 ath_rate_tx_complete(struct ath_softc *sc, struct ath_node *an,
594 	const struct ath_buf *bf)
595 {
596 	struct ifnet *ifp = sc->sc_ifp;
597 	struct ieee80211com *ic = ifp->if_l2com;
598 	struct sample_node *sn = ATH_NODE_SAMPLE(an);
599 	const struct ath_tx_status *ts = &bf->bf_status.ds_txstat;
600 	const struct ath_desc *ds0 = &bf->bf_desc[0];
601 	int final_rix, short_tries, long_tries, frame_size;
602 	const HAL_RATE_TABLE *rt = sc->sc_currates;
603 	int mrr;
604 
605 	final_rix = rt->rateCodeToIndex[ts->ts_rate];
606 	short_tries = ts->ts_shortretry;
607 	long_tries = ts->ts_longretry + 1;
608 	frame_size = ds0->ds_ctl0 & 0x0fff; /* low-order 12 bits of ds_ctl0 */
609 	if (frame_size == 0)		    /* NB: should not happen */
610 		frame_size = 1500;
611 
612 	if (sn->ratemask == 0) {
613 		IEEE80211_NOTE(an->an_node.ni_vap, IEEE80211_MSG_RATECTL,
614 		    &an->an_node,
615 		    "%s: size %d %s rate/try %d/%d no rates yet",
616 		    __func__,
617 		    bin_to_size(size_to_bin(frame_size)),
618 		    ts->ts_status ? "FAIL" : "OK",
619 		    short_tries, long_tries);
620 		return;
621 	}
622 	mrr = sc->sc_mrretry && !(ic->ic_flags & IEEE80211_F_USEPROT);
623 	if (!mrr || ts->ts_finaltsi == 0) {
624 		if (!IS_RATE_DEFINED(sn, final_rix)) {
625 			badrate(ifp, 0, ts->ts_rate, long_tries, ts->ts_status);
626 			return;
627 		}
628 		/*
629 		 * Only one rate was used; optimize work.
630 		 */
631 		IEEE80211_NOTE(an->an_node.ni_vap, IEEE80211_MSG_RATECTL,
632 		     &an->an_node, "%s: size %d (%d bytes) %s rate/try %d %s/%d/%d",
633 		     __func__,
634 		     bin_to_size(size_to_bin(frame_size)),
635 		     frame_size,
636 		     ts->ts_status ? "FAIL" : "OK",
637 		     dot11rate(rt, final_rix), dot11rate_label(rt, final_rix), short_tries, long_tries);
638 		update_stats(sc, an, frame_size,
639 			     final_rix, long_tries,
640 			     0, 0,
641 			     0, 0,
642 			     0, 0,
643 			     short_tries, long_tries, ts->ts_status);
644 	} else {
645 		int hwrates[4], tries[4], rix[4];
646 		int finalTSIdx = ts->ts_finaltsi;
647 		int i;
648 
649 		/*
650 		 * Process intermediate rates that failed.
651 		 */
652 		ath_hal_gettxcompletionrates(sc->sc_ah, ds0, hwrates, tries);
653 
654 		for (i = 0; i < 4; i++) {
655 			rix[i] = rt->rateCodeToIndex[hwrates[i]];
656 		}
657 
658 		IEEE80211_NOTE(an->an_node.ni_vap, IEEE80211_MSG_RATECTL,
659 		    &an->an_node,
660 "%s: size %d (%d bytes) finaltsidx %d tries %d %s rate/try [%d %s/%d %d %s/%d %d %s/%d %d %s/%d]",
661 		     __func__,
662 		     bin_to_size(size_to_bin(frame_size)),
663 		     frame_size,
664 		     finalTSIdx,
665 		     long_tries,
666 		     ts->ts_status ? "FAIL" : "OK",
667 		     dot11rate(rt, rix[0]), dot11rate_label(rt, rix[0]), tries[0],
668 		     dot11rate(rt, rix[1]), dot11rate_label(rt, rix[1]), tries[1],
669 		     dot11rate(rt, rix[2]), dot11rate_label(rt, rix[2]), tries[2],
670 		     dot11rate(rt, rix[3]), dot11rate_label(rt, rix[3]), tries[3]);
671 
672 		for (i = 0; i < 4; i++) {
673 			if (tries[i] && !IS_RATE_DEFINED(sn, rix[i]))
674 				badrate(ifp, 0, hwrates[i], tries[i], ts->ts_status);
675 		}
676 
677 		/*
678 		 * NB: series > 0 are not penalized for failure
679 		 * based on the try counts under the assumption
680 		 * that losses are often bursty and since we
681 		 * sample higher rates 1 try at a time doing so
682 		 * may unfairly penalize them.
683 		 */
684 		if (tries[0]) {
685 			update_stats(sc, an, frame_size,
686 				     rix[0], tries[0],
687 				     rix[1], tries[1],
688 				     rix[2], tries[2],
689 				     rix[3], tries[3],
690 				     short_tries, long_tries,
691 				     long_tries > tries[0]);
692 			long_tries -= tries[0];
693 		}
694 
695 		if (tries[1] && finalTSIdx > 0) {
696 			update_stats(sc, an, frame_size,
697 				     rix[1], tries[1],
698 				     rix[2], tries[2],
699 				     rix[3], tries[3],
700 				     0, 0,
701 				     short_tries, long_tries,
702 				     ts->ts_status);
703 			long_tries -= tries[1];
704 		}
705 
706 		if (tries[2] && finalTSIdx > 1) {
707 			update_stats(sc, an, frame_size,
708 				     rix[2], tries[2],
709 				     rix[3], tries[3],
710 				     0, 0,
711 				     0, 0,
712 				     short_tries, long_tries,
713 				     ts->ts_status);
714 			long_tries -= tries[2];
715 		}
716 
717 		if (tries[3] && finalTSIdx > 2) {
718 			update_stats(sc, an, frame_size,
719 				     rix[3], tries[3],
720 				     0, 0,
721 				     0, 0,
722 				     0, 0,
723 				     short_tries, long_tries,
724 				     ts->ts_status);
725 		}
726 	}
727 }
728 
729 void
730 ath_rate_newassoc(struct ath_softc *sc, struct ath_node *an, int isnew)
731 {
732 	if (isnew)
733 		ath_rate_ctl_reset(sc, &an->an_node);
734 }
735 
736 static const struct txschedule *mrr_schedules[IEEE80211_MODE_MAX+2] = {
737 	NULL,		/* IEEE80211_MODE_AUTO */
738 	series_11a,	/* IEEE80211_MODE_11A */
739 	series_11g,	/* IEEE80211_MODE_11B */
740 	series_11g,	/* IEEE80211_MODE_11G */
741 	NULL,		/* IEEE80211_MODE_FH */
742 	series_11a,	/* IEEE80211_MODE_TURBO_A */
743 	series_11g,	/* IEEE80211_MODE_TURBO_G */
744 	series_11a,	/* IEEE80211_MODE_STURBO_A */
745 	series_11na,	/* IEEE80211_MODE_11NA */
746 	series_11ng,	/* IEEE80211_MODE_11NG */
747 	series_half,	/* IEEE80211_MODE_HALF */
748 	series_quarter,	/* IEEE80211_MODE_QUARTER */
749 };
750 
751 /*
752  * Initialize the tables for a node.
753  */
754 static void
755 ath_rate_ctl_reset(struct ath_softc *sc, struct ieee80211_node *ni)
756 {
757 #define	RATE(_ix)	(ni->ni_rates.rs_rates[(_ix)] & IEEE80211_RATE_VAL)
758 #define	DOT11RATE(_ix)	(rt->info[(_ix)].dot11Rate & IEEE80211_RATE_VAL)
759 #define	MCS(_ix)	(ni->ni_htrates.rs_rates[_ix] | IEEE80211_RATE_MCS)
760 	struct ath_node *an = ATH_NODE(ni);
761 	struct sample_node *sn = ATH_NODE_SAMPLE(an);
762 	const HAL_RATE_TABLE *rt = sc->sc_currates;
763 	int x, y, rix;
764 
765 	KASSERT(rt != NULL, ("no rate table, mode %u", sc->sc_curmode));
766 
767 	KASSERT(sc->sc_curmode < IEEE80211_MODE_MAX+2,
768 	    ("curmode %u", sc->sc_curmode));
769 	sn->sched = mrr_schedules[sc->sc_curmode];
770 	KASSERT(sn->sched != NULL,
771 	    ("no mrr schedule for mode %u", sc->sc_curmode));
772 
773         sn->static_rix = -1;
774 	ath_rate_update_static_rix(sc, ni);
775 
776 	/*
777 	 * Construct a bitmask of usable rates.  This has all
778 	 * negotiated rates minus those marked by the hal as
779 	 * to be ignored for doing rate control.
780 	 */
781 	sn->ratemask = 0;
782 	/* MCS rates */
783 	if (ni->ni_flags & IEEE80211_NODE_HT) {
784 		for (x = 0; x < ni->ni_htrates.rs_nrates; x++) {
785 			rix = sc->sc_rixmap[MCS(x)];
786 			if (rix == 0xff)
787 				continue;
788 			/* skip rates marked broken by hal */
789 			if (!rt->info[rix].valid)
790 				continue;
791 			KASSERT(rix < SAMPLE_MAXRATES,
792 			    ("mcs %u has rix %d", MCS(x), rix));
793 			sn->ratemask |= 1<<rix;
794 		}
795 	}
796 
797 	/* Legacy rates */
798 	for (x = 0; x < ni->ni_rates.rs_nrates; x++) {
799 		rix = sc->sc_rixmap[RATE(x)];
800 		if (rix == 0xff)
801 			continue;
802 		/* skip rates marked broken by hal */
803 		if (!rt->info[rix].valid)
804 			continue;
805 		KASSERT(rix < SAMPLE_MAXRATES,
806 		    ("rate %u has rix %d", RATE(x), rix));
807 		sn->ratemask |= 1<<rix;
808 	}
809 #ifdef IEEE80211_DEBUG
810 	if (ieee80211_msg(ni->ni_vap, IEEE80211_MSG_RATECTL)) {
811 		uint32_t mask;
812 
813 		ieee80211_note(ni->ni_vap, "[%6D] %s: size 1600 rate/tt",
814 		    ni->ni_macaddr, ":", __func__);
815 		for (mask = sn->ratemask, rix = 0; mask != 0; mask >>= 1, rix++) {
816 			if ((mask & 1) == 0)
817 				continue;
818 			printf(" %d %s/%d", dot11rate(rt, rix), dot11rate_label(rt, rix),
819 			    calc_usecs_unicast_packet(sc, 1600, rix, 0,0,
820 			        (ni->ni_chw == 40)));
821 		}
822 		printf("\n");
823 	}
824 #endif
825 	for (y = 0; y < NUM_PACKET_SIZE_BINS; y++) {
826 		int size = bin_to_size(y);
827 		uint32_t mask;
828 
829 		sn->packets_sent[y] = 0;
830 		sn->current_sample_rix[y] = -1;
831 		sn->last_sample_rix[y] = 0;
832 		/* XXX start with first valid rate */
833 		sn->current_rix[y] = ffs(sn->ratemask)-1;
834 
835 		/*
836 		 * Initialize the statistics buckets; these are
837 		 * indexed by the rate code index.
838 		 */
839 		for (rix = 0, mask = sn->ratemask; mask != 0; rix++, mask >>= 1) {
840 			if ((mask & 1) == 0)		/* not a valid rate */
841 				continue;
842 			sn->stats[y][rix].successive_failures = 0;
843 			sn->stats[y][rix].tries = 0;
844 			sn->stats[y][rix].total_packets = 0;
845 			sn->stats[y][rix].packets_acked = 0;
846 			sn->stats[y][rix].last_tx = 0;
847 
848 			sn->stats[y][rix].perfect_tx_time =
849 			    calc_usecs_unicast_packet(sc, size, rix, 0, 0,
850 			    (ni->ni_chw == 40));
851 			sn->stats[y][rix].average_tx_time =
852 			    sn->stats[y][rix].perfect_tx_time;
853 		}
854 	}
855 #if 0
856 	/* XXX 0, num_rates-1 are wrong */
857 	IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_RATECTL, ni,
858 	    "%s: %d rates %d%sMbps (%dus)- %d%sMbps (%dus)", __func__,
859 	    sn->num_rates,
860 	    DOT11RATE(0)/2, DOT11RATE(0) % 1 ? ".5" : "",
861 	    sn->stats[1][0].perfect_tx_time,
862 	    DOT11RATE(sn->num_rates-1)/2, DOT11RATE(sn->num_rates-1) % 1 ? ".5" : "",
863 	    sn->stats[1][sn->num_rates-1].perfect_tx_time
864 	);
865 #endif
866 	/* set the visible bit-rate */
867 	if (sn->static_rix != -1)
868 		ni->ni_txrate = DOT11RATE(sn->static_rix);
869 	else
870 		ni->ni_txrate = RATE(0);
871 #undef RATE
872 #undef DOT11RATE
873 }
874 
875 static void
876 sample_stats(void *arg, struct ieee80211_node *ni)
877 {
878 	struct ath_softc *sc = arg;
879 	const HAL_RATE_TABLE *rt = sc->sc_currates;
880 	struct sample_node *sn = ATH_NODE_SAMPLE(ATH_NODE(ni));
881 	uint32_t mask;
882 	int rix, y;
883 
884 	printf("\n[%s] refcnt %d static_rix %d ratemask 0x%x\n",
885 	    ether_sprintf(ni->ni_macaddr), ieee80211_node_refcnt(ni),
886 	    sn->static_rix, sn->ratemask);
887 	for (y = 0; y < NUM_PACKET_SIZE_BINS; y++) {
888 		printf("[%4u] cur rix %d (%d %s) since switch: packets %d ticks %u\n",
889 		    bin_to_size(y), sn->current_rix[y],
890 		    dot11rate(rt, sn->current_rix[y]),
891 		    dot11rate_label(rt, sn->current_rix[y]),
892 		    sn->packets_since_switch[y], sn->ticks_since_switch[y]);
893 		printf("[%4u] last sample %d cur sample %d packets sent %d\n",
894 		    bin_to_size(y), sn->last_sample_rix[y],
895 		    sn->current_sample_rix[y], sn->packets_sent[y]);
896 		printf("[%4u] packets since sample %d sample tt %u\n",
897 		    bin_to_size(y), sn->packets_since_sample[y],
898 		    sn->sample_tt[y]);
899 	}
900 	for (mask = sn->ratemask, rix = 0; mask != 0; mask >>= 1, rix++) {
901 		if ((mask & 1) == 0)
902 				continue;
903 		for (y = 0; y < NUM_PACKET_SIZE_BINS; y++) {
904 			if (sn->stats[y][rix].total_packets == 0)
905 				continue;
906 			printf("[%2u %s:%4u] %8d:%-8d (%3d%%) T %8d F %4d avg %5u last %u\n",
907 			    dot11rate(rt, rix), dot11rate_label(rt, rix),
908 			    bin_to_size(y),
909 			    sn->stats[y][rix].total_packets,
910 			    sn->stats[y][rix].packets_acked,
911 			    (100*sn->stats[y][rix].packets_acked)/sn->stats[y][rix].total_packets,
912 			    sn->stats[y][rix].tries,
913 			    sn->stats[y][rix].successive_failures,
914 			    sn->stats[y][rix].average_tx_time,
915 			    ticks - sn->stats[y][rix].last_tx);
916 		}
917 	}
918 }
919 
920 static int
921 ath_rate_sysctl_stats(SYSCTL_HANDLER_ARGS)
922 {
923 	struct ath_softc *sc = arg1;
924 	struct ifnet *ifp = sc->sc_ifp;
925 	struct ieee80211com *ic = ifp->if_l2com;
926 	int error, v;
927 
928 	v = 0;
929 	error = sysctl_handle_int(oidp, &v, 0, req);
930 	if (error || !req->newptr)
931 		return error;
932 	ieee80211_iterate_nodes(&ic->ic_sta, sample_stats, sc);
933 	return 0;
934 }
935 
936 static int
937 ath_rate_sysctl_smoothing_rate(SYSCTL_HANDLER_ARGS)
938 {
939 	struct sample_softc *ssc = arg1;
940 	int rate, error;
941 
942 	rate = ssc->smoothing_rate;
943 	error = sysctl_handle_int(oidp, &rate, 0, req);
944 	if (error || !req->newptr)
945 		return error;
946 	if (!(0 <= rate && rate < 100))
947 		return EINVAL;
948 	ssc->smoothing_rate = rate;
949 	ssc->smoothing_minpackets = 100 / (100 - rate);
950 	return 0;
951 }
952 
953 static int
954 ath_rate_sysctl_sample_rate(SYSCTL_HANDLER_ARGS)
955 {
956 	struct sample_softc *ssc = arg1;
957 	int rate, error;
958 
959 	rate = ssc->sample_rate;
960 	error = sysctl_handle_int(oidp, &rate, 0, req);
961 	if (error || !req->newptr)
962 		return error;
963 	if (!(2 <= rate && rate <= 100))
964 		return EINVAL;
965 	ssc->sample_rate = rate;
966 	return 0;
967 }
968 
969 static void
970 ath_rate_sysctlattach(struct ath_softc *sc, struct sample_softc *ssc)
971 {
972 	struct sysctl_ctx_list *ctx = device_get_sysctl_ctx(sc->sc_dev);
973 	struct sysctl_oid *tree = device_get_sysctl_tree(sc->sc_dev);
974 
975 	SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
976 	    "smoothing_rate", CTLTYPE_INT | CTLFLAG_RW, ssc, 0,
977 	    ath_rate_sysctl_smoothing_rate, "I",
978 	    "sample: smoothing rate for avg tx time (%%)");
979 	SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
980 	    "sample_rate", CTLTYPE_INT | CTLFLAG_RW, ssc, 0,
981 	    ath_rate_sysctl_sample_rate, "I",
982 	    "sample: percent air time devoted to sampling new rates (%%)");
983 	/* XXX max_successive_failures, stale_failure_timeout, min_switch */
984 	SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
985 	    "sample_stats", CTLTYPE_INT | CTLFLAG_RW, sc, 0,
986 	    ath_rate_sysctl_stats, "I", "sample: print statistics");
987 }
988 
989 struct ath_ratectrl *
990 ath_rate_attach(struct ath_softc *sc)
991 {
992 	struct sample_softc *ssc;
993 
994 	ssc = malloc(sizeof(struct sample_softc), M_DEVBUF, M_NOWAIT|M_ZERO);
995 	if (ssc == NULL)
996 		return NULL;
997 	ssc->arc.arc_space = sizeof(struct sample_node);
998 	ssc->smoothing_rate = 95;		/* ewma percentage ([0..99]) */
999 	ssc->smoothing_minpackets = 100 / (100 - ssc->smoothing_rate);
1000 	ssc->sample_rate = 10;			/* %time to try diff tx rates */
1001 	ssc->max_successive_failures = 3;	/* threshold for rate sampling*/
1002 	ssc->stale_failure_timeout = 10 * hz;	/* 10 seconds */
1003 	ssc->min_switch = hz;			/* 1 second */
1004 	ath_rate_sysctlattach(sc, ssc);
1005 	return &ssc->arc;
1006 }
1007 
1008 void
1009 ath_rate_detach(struct ath_ratectrl *arc)
1010 {
1011 	struct sample_softc *ssc = (struct sample_softc *) arc;
1012 
1013 	free(ssc, M_DEVBUF);
1014 }
1015