Home | History | Annotate | Line # | Download | only in ic
athrate-sample.c revision 1.2
      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 #include <sys/cdefs.h>
     38 __FBSDID("$FreeBSD: src/sys/dev/ath/ath_rate/sample/sample.c,v 1.8 2005/04/02 18:56:50 sam Exp $");
     39 
     40 /*
     41  * John Bicket's SampleRate control algorithm.
     42  */
     43 #include "opt_inet.h"
     44 
     45 #include <sys/param.h>
     46 #include <sys/systm.h>
     47 #include <sys/sysctl.h>
     48 #include <sys/kernel.h>
     49 #include <sys/lock.h>
     50 #include <sys/errno.h>
     51 
     52 #include <machine/bus.h>
     53 #include <machine/resource.h>
     54 #include <sys/bus.h>
     55 
     56 #include <sys/socket.h>
     57 
     58 #include <net/if.h>
     59 #include <net/if_media.h>
     60 #include <net/if_arp.h>
     61 #include <net/ethernet.h>		/* XXX for ether_sprintf */
     62 
     63 #include <net80211/ieee80211_var.h>
     64 
     65 #include <net/bpf.h>
     66 
     67 #ifdef INET
     68 #include <netinet/in.h>
     69 #endif
     70 
     71 #include <dev/ic/athvar.h>
     72 #include <dev/ic/athrate-sample.h>
     73 #include <contrib/dev/ic/athhal_desc.h>
     74 
     75 #define	SAMPLE_DEBUG
     76 #ifdef SAMPLE_DEBUG
     77 enum {
     78 	ATH_DEBUG_RATE		= 0x00000010,	/* rate control */
     79 };
     80 #define	DPRINTF(sc, _fmt, ...) do {				\
     81 	if (sc->sc_debug & ATH_DEBUG_RATE)			\
     82 		printf(_fmt, __VA_ARGS__);			\
     83 } while (0)
     84 #else
     85 #define	DPRINTF(sc, _fmt, ...)
     86 #endif
     87 
     88 /*
     89  * This file is an implementation of the SampleRate algorithm
     90  * in "Bit-rate Selection in Wireless Networks"
     91  * (http://www.pdos.lcs.mit.edu/papers/jbicket-ms.ps)
     92  *
     93  * SampleRate chooses the bit-rate it predicts will provide the most
     94  * throughput based on estimates of the expected per-packet
     95  * transmission time for each bit-rate.  SampleRate periodically sends
     96  * packets at bit-rates other than the current one to estimate when
     97  * another bit-rate will provide better performance. SampleRate
     98  * switches to another bit-rate when its estimated per-packet
     99  * transmission time becomes smaller than the current bit-rate's.
    100  * SampleRate reduces the number of bit-rates it must sample by
    101  * eliminating those that could not perform better than the one
    102  * currently being used.  SampleRate also stops probing at a bit-rate
    103  * if it experiences several successive losses.
    104  *
    105  * The difference between the algorithm in the thesis and the one in this
    106  * file is that the one in this file uses a ewma instead of a window.
    107  *
    108  */
    109 
    110 #define STALE_FAILURE_TIMEOUT_MS 10000
    111 
    112 static void	ath_rate_ctl_reset(struct ath_softc *, struct ieee80211_node *);
    113 
    114 static __inline int size_to_bin(int size)
    115 {
    116 	int x = 0;
    117 	for (x = 0; x < NUM_PACKET_SIZE_BINS; x++) {
    118 		if (size <= packet_size_bins[x]) {
    119 			return x;
    120 		}
    121 	}
    122 	return NUM_PACKET_SIZE_BINS-1;
    123 }
    124 static __inline int bin_to_size(int index) {
    125 	return packet_size_bins[index];
    126 }
    127 
    128 static __inline int rate_to_ndx(struct sample_node *sn, int rate) {
    129 	int x = 0;
    130 	for (x = 0; x < sn->num_rates; x++) {
    131 		if (sn->rates[x].rate == rate) {
    132 			return x;
    133 		}
    134 	}
    135 	return -1;
    136 }
    137 
    138 /*
    139  * Setup rate codes for management/control frames.  We force
    140  * all such frames to the lowest rate.
    141  */
    142 static void
    143 ath_rate_setmgtrates(struct ath_softc *sc, struct ath_node *an)
    144 {
    145 	const HAL_RATE_TABLE *rt = sc->sc_currates;
    146 
    147 	/* setup rates for management frames */
    148 	/* XXX management/control frames always go at lowest speed */
    149 	an->an_tx_mgtrate = rt->info[0].rateCode;
    150 	an->an_tx_mgtratesp = an->an_tx_mgtrate
    151 			    | rt->info[0].shortPreamble;
    152 }
    153 
    154 void
    155 ath_rate_node_init(struct ath_softc *sc, struct ath_node *an)
    156 {
    157 	DPRINTF(sc, "%s:\n", __func__);
    158 	/* NB: assumed to be zero'd by caller */
    159 	ath_rate_setmgtrates(sc, an);
    160 }
    161 
    162 void
    163 ath_rate_node_cleanup(struct ath_softc *sc, struct ath_node *an)
    164 {
    165 	DPRINTF(sc, "%s:\n", __func__);
    166 }
    167 
    168 
    169 /*
    170  * returns the ndx with the lowest average_tx_time,
    171  * or -1 if all the average_tx_times are 0.
    172  */
    173 static __inline int best_rate_ndx(struct sample_node *sn, int size_bin,
    174 				  int require_acked_before)
    175 {
    176 	int x = 0;
    177         int best_rate_ndx = 0;
    178         int best_rate_tt = 0;
    179         for (x = 0; x < sn->num_rates; x++) {
    180 		int tt = sn->stats[size_bin][x].average_tx_time;
    181 		if (tt <= 0 || (require_acked_before &&
    182 				!sn->stats[size_bin][x].packets_acked)) {
    183 			continue;
    184 		}
    185 		if (!best_rate_tt || best_rate_tt > tt) {
    186 			best_rate_tt = tt;
    187 			best_rate_ndx = x;
    188 		}
    189         }
    190         return (best_rate_tt) ? best_rate_ndx : -1;
    191 }
    192 
    193 /*
    194  * pick a ndx s.t. the perfect_tx_time
    195  * is less than the best bit-rate's average_tx_time
    196  * and the ndx has not had four successive failures.
    197  */
    198 static __inline int pick_sample_ndx(struct sample_node *sn, int size_bin)
    199 {
    200 	int x = 0;
    201 	int current_ndx = 0;
    202 	unsigned current_tt = 0;
    203 
    204 	current_ndx = sn->current_rate[size_bin];
    205 	if (current_ndx < 0) {
    206 		/* no successes yet, send at the lowest bit-rate */
    207 		return 0;
    208 	}
    209 
    210 	current_tt = sn->stats[size_bin][current_ndx].average_tx_time;
    211 
    212 	for (x = 0; x < sn->num_rates; x++) {
    213 		int ndx = (sn->last_sample_ndx[size_bin] + 1 + x) % sn->num_rates;
    214 		/*
    215 		 * clear any stale stuff out.
    216 		 */
    217 		if (ticks - sn->stats[size_bin][ndx].last_tx > ((hz * STALE_FAILURE_TIMEOUT_MS)/1000)) {
    218 			sn->stats[size_bin][ndx].average_tx_time = sn->stats[size_bin][ndx].perfect_tx_time;
    219 			sn->stats[size_bin][ndx].successive_failures = 0;
    220 			sn->stats[size_bin][ndx].tries = 0;
    221 			sn->stats[size_bin][ndx].total_packets = 0;
    222 			sn->stats[size_bin][ndx].packets_acked = 0;
    223 		}
    224 
    225 		if (ndx != current_ndx &&
    226 		    sn->stats[size_bin][ndx].perfect_tx_time < current_tt &&
    227 		    sn->stats[size_bin][ndx].successive_failures < 4) {
    228 			sn->last_sample_ndx[size_bin] = ndx;
    229 			return ndx;
    230 		}
    231 	}
    232 	return current_ndx;
    233 }
    234 
    235 void
    236 ath_rate_findrate(struct ath_softc *sc, struct ath_node *an,
    237 		  int shortPreamble, size_t frameLen,
    238 		  u_int8_t *rix, int *try0, u_int8_t *txrate)
    239 {
    240 	struct sample_node *sn = ATH_NODE_SAMPLE(an);
    241 	struct sample_softc *ssc = ATH_SOFTC_SAMPLE(sc);
    242 	struct ieee80211com *ic = &sc->sc_ic;
    243 	int ndx, size_bin, mrr, best_ndx;
    244 	unsigned average_tx_time;
    245 
    246 	mrr = sc->sc_mrretry && !(ic->ic_flags & IEEE80211_F_USEPROT) &&
    247 		!(frameLen > ic->ic_rtsthreshold);
    248 	size_bin = size_to_bin(frameLen);
    249 	best_ndx = best_rate_ndx(sn, size_bin, !mrr);
    250 
    251 	if (best_ndx >= 0) {
    252 		average_tx_time = sn->stats[size_bin][best_ndx].average_tx_time;
    253 	} else {
    254 		average_tx_time = 0;
    255 	}
    256 	if (sn->static_rate_ndx != -1) {
    257 		ndx = sn->static_rate_ndx;
    258 		*try0 = ATH_TXMAXTRY;
    259 	} else {
    260 		ndx = 0;
    261 		*try0 = mrr ? 2 : ATH_TXMAXTRY;
    262 
    263 		DPRINTF(sc, "%s: %s size %d mrr %d packets_sent %d best_ndx %d "
    264 			"sample tt %d packets since %d\n"
    265 			, __func__, ether_sprintf(an->an_node.ni_macaddr)
    266 			, packet_size_bins[size_bin]
    267 			, mrr
    268 			, sn->packets_sent[size_bin]
    269 			, best_ndx
    270 			, sn->sample_tt[size_bin]
    271 			, sn->packets_since_sample[size_bin]
    272 		);
    273 		if (!sn->packets_sent[size_bin]) {
    274 			/* no packets sent */
    275 			if (best_ndx == -1) {
    276 				ndx = sn->num_rates - 1;
    277 				if (sc->sc_curmode != IEEE80211_MODE_11B) {
    278 					for (; ndx >= 0 && sn->rates[ndx].rate > 72; ndx--)
    279 						;
    280 
    281 				}
    282 			} else {
    283 				ndx = best_ndx;
    284 			}
    285 		} else if (best_ndx == -1) {
    286 			/* no packet has succeeded yet */
    287 			if (mrr) {
    288 				/*
    289 				 * no packet has succeeded, try the
    290 				 * highest bitrate that hasn't failed
    291 				 */
    292 				for (ndx = sn->num_rates-1; ndx >= 0; ndx--) {
    293 					if (sn->stats[size_bin][ndx].successive_failures == 0) {
    294 						break;
    295 					}
    296 				}
    297 			} else {
    298 				ndx = sn->num_rates - 1;
    299 				if (sc->sc_curmode != IEEE80211_MODE_11B) {
    300 					for (; ndx >= 0 && sn->rates[ndx].rate > 72; ndx--)
    301 						;
    302 
    303 				}
    304 			}
    305 		} else if (sn->sample_tt[size_bin] < (sn->packets_since_sample[size_bin]*ssc->ath_sample_rate/100) * average_tx_time &&
    306 			   sn->packets_since_sample[size_bin] > 15) {
    307 			/*
    308 			 * we want to limit the time measuring the performance
    309 			 * of other bit-rates to ath_sample_rate% of the
    310 			 * total transmission time.
    311 			 */
    312 			ndx = pick_sample_ndx(sn, size_bin);
    313 			if (ndx != sn->current_rate[size_bin]) {
    314 				DPRINTF(sc, "%s: %s size %d last sample tt %d sampling %d packets since %d\n",
    315 					__func__,
    316 					ether_sprintf(an->an_node.ni_macaddr),
    317 					packet_size_bins[size_bin],
    318 					sn->sample_tt[size_bin],
    319 					sn->rates[ndx].rate,
    320 					sn->packets_since_sample[size_bin]);
    321 				sn->current_sample_ndx[size_bin] = ndx;
    322 			} else {
    323 				sn->current_sample_ndx[size_bin] = -1;
    324 			}
    325 			sn->packets_since_sample[size_bin] = 0;
    326 
    327 		} else {
    328 			sn->packets_since_sample[size_bin]++;
    329 			/*
    330 			 * don't switch bit-rates every packet.  only
    331 			 * switch during the first few packets we send
    332 			 * or after 100 packets, or if the current
    333 			 * bit-rate begins to perform twice as bad as
    334 			 * another one.
    335 			 */
    336 			if (sn->packets_sent[size_bin] < 20 ||
    337 			    ticks - ((hz*2000)/1000) > sn->jiffies_since_switch[size_bin] ||
    338 			    average_tx_time * 2 < sn->stats[size_bin][sn->current_rate[size_bin]].average_tx_time ) {
    339 				if (sn->packets_sent[size_bin] > 20) {
    340 					DPRINTF(sc, "%s: %s size %d switch rate %d (%d/%d) -> %d (%d/%d) after %d packets mmr %d\n",
    341 						__func__,
    342 						ether_sprintf(an->an_node.ni_macaddr),
    343 						packet_size_bins[size_bin],
    344 						sn->rates[sn->current_rate[size_bin]].rate,
    345 						sn->stats[size_bin][sn->current_rate[size_bin]].average_tx_time,
    346 						sn->stats[size_bin][sn->current_rate[size_bin]].perfect_tx_time,
    347 						sn->rates[best_ndx].rate,
    348 						sn->stats[size_bin][best_ndx].average_tx_time,
    349 						sn->stats[size_bin][best_ndx].perfect_tx_time,
    350 						sn->packets_since_switch[size_bin],
    351 						mrr);
    352 				}
    353 				sn->packets_since_switch[size_bin] = 0;
    354 				sn->current_rate[size_bin] = best_ndx;
    355 				sn->jiffies_since_switch[size_bin] = ticks;
    356 			}
    357 			ndx = sn->current_rate[size_bin];
    358 			sn->packets_since_switch[size_bin]++;
    359 		}
    360 
    361 	}
    362 
    363 	if (ndx < 0) {
    364 		ndx = 0;
    365 	}
    366 	*rix = sn->rates[ndx].rix;
    367 	if (shortPreamble) {
    368 		*txrate = sn->rates[ndx].shortPreambleRateCode;
    369 	} else {
    370 		*txrate = sn->rates[ndx].rateCode;
    371 	}
    372 	sn->packets_sent[size_bin]++;
    373 	an->an_node.ni_txrate = ndx;
    374 }
    375 
    376 void
    377 ath_rate_setupxtxdesc(struct ath_softc *sc, struct ath_node *an,
    378 		      struct ath_desc *ds, int shortPreamble, u_int8_t rix)
    379 {
    380 	struct sample_node *sn = ATH_NODE_SAMPLE(an);
    381 	int rateCode = -1;
    382 	int frame_size, size_bin, best_ndx, ndx;
    383 
    384 	frame_size = ds->ds_ctl0 & 0x0fff; /* low-order 12 bits of ds_ctl0 */
    385 	KASSERT(frame_size != 0, ("no frame size"));
    386 	size_bin = size_to_bin(frame_size);
    387 	best_ndx = best_rate_ndx(sn, size_bin, 0);
    388 
    389 	if (best_ndx == -1 || !sn->stats[size_bin][best_ndx].packets_acked) {
    390 		/*
    391 		 * no packet has succeeded, so also try at the
    392 		 * lowest bitate.
    393 		 */
    394 		ndx = 0;
    395 	} else {
    396 		/*
    397 		 * we're trying a different bit-rate, and it could be lossy,
    398 		 * so if it fails try at the best bit-rate.
    399 		 */
    400 		ndx = best_ndx;
    401 	}
    402 	KASSERT(0 <= ndx && ndx < IEEE80211_RATE_MAXSIZE,
    403 		("invalid ndx %d", ndx));
    404 	if (shortPreamble) {
    405 		rateCode = sn->rates[ndx].shortPreambleRateCode;
    406 	} else {
    407 		rateCode = sn->rates[ndx].rateCode;
    408 	}
    409 	ath_hal_setupxtxdesc(sc->sc_ah, ds
    410 			     , rateCode, 3	        /* series 1 */
    411 			     , sn->rates[0].rateCode, 3	/* series 2 */
    412 			     , 0, 0	                /* series 3 */
    413 			     );
    414 }
    415 
    416 static void
    417 update_stats(struct ath_softc *sc, struct ath_node *an,
    418 		  int frame_size,
    419 		  int ndx0, int tries0,
    420 		  int ndx1, int tries1,
    421 		  int ndx2, int tries2,
    422 		  int ndx3, int tries3,
    423 		  int short_tries, int tries, int status)
    424 {
    425 	struct sample_node *sn = ATH_NODE_SAMPLE(an);
    426 	struct sample_softc *ssc = ATH_SOFTC_SAMPLE(sc);
    427 	int tt = 0;
    428 	int tries_so_far = 0;
    429 	int size_bin = 0;
    430 	int size = 0;
    431 	int rate = 0;
    432 
    433 	size_bin = size_to_bin(frame_size);
    434 	size = bin_to_size(size_bin);
    435 	rate = sn->rates[ndx0].rate;
    436 
    437 	tt += calc_usecs_unicast_packet(sc, size, sn->rates[ndx0].rix,
    438 					short_tries-1,
    439 					MIN(tries0, tries) - 1);
    440 	tries_so_far += tries0;
    441 	if (tries1 && tries0 < tries) {
    442 		tt += calc_usecs_unicast_packet(sc, size, sn->rates[ndx1].rix,
    443 						short_tries-1,
    444 						MIN(tries1 + tries_so_far, tries) - tries_so_far - 1);
    445 	}
    446 	tries_so_far += tries1;
    447 
    448 	if (tries2 && tries0 + tries1 < tries) {
    449 		tt += calc_usecs_unicast_packet(sc, size, sn->rates[ndx2].rix,
    450 					       short_tries-1,
    451 						MIN(tries2 + tries_so_far, tries) - tries_so_far - 1);
    452 	}
    453 
    454 	tries_so_far += tries2;
    455 
    456 	if (tries3 && tries0 + tries1 + tries2 < tries) {
    457 		tt += calc_usecs_unicast_packet(sc, size, sn->rates[ndx3].rix,
    458 						short_tries-1,
    459 						MIN(tries3 + tries_so_far, tries) - tries_so_far - 1);
    460 	}
    461 #ifdef SAMPLE_DEBUG
    462 	if (short_tries + tries > 3 || status) {
    463 		DPRINTF(sc, "%s: %s size %d rate %d ndx %d tries (%d/%d) tries0 %d tt %d avg_tt %d perfect_tt %d status %d\n",
    464 			__func__, ether_sprintf(an->an_node.ni_macaddr),
    465 			size,
    466 			rate, ndx0, short_tries, tries, tries0, tt,
    467 			sn->stats[size_bin][ndx0].average_tx_time,
    468 			sn->stats[size_bin][ndx0].perfect_tx_time,
    469 			status);
    470 	}
    471 #endif /* SAMPLE_DEBUG */
    472 	if (sn->stats[size_bin][ndx0].total_packets < (100 / (100 - ssc->ath_smoothing_rate))) {
    473 		/* just average the first few packets */
    474 		int avg_tx = sn->stats[size_bin][ndx0].average_tx_time;
    475 		int packets = sn->stats[size_bin][ndx0].total_packets;
    476 		sn->stats[size_bin][ndx0].average_tx_time = (tt+(avg_tx*packets))/(packets+1);
    477 	} else {
    478 		/* use a ewma */
    479 		sn->stats[size_bin][ndx0].average_tx_time =
    480 			((sn->stats[size_bin][ndx0].average_tx_time * ssc->ath_smoothing_rate) +
    481 			 (tt * (100 - ssc->ath_smoothing_rate))) / 100;
    482 	}
    483 
    484 	if (status) {
    485 		/*
    486 		 * this packet failed - count this as a failure
    487 		 * for larger packets also, since we assume
    488 		 * if a small packet fails at a lower bit-rate
    489 		 * then a larger one will also.
    490 		 */
    491 		int y;
    492 		for (y = size_bin; y < NUM_PACKET_SIZE_BINS; y++) {
    493 			sn->stats[y][ndx0].successive_failures++;
    494 			sn->stats[y][ndx0].last_tx = ticks;
    495 		}
    496 	} else {
    497 		sn->stats[size_bin][ndx0].packets_acked++;
    498 		sn->stats[size_bin][ndx0].successive_failures = 0;
    499 	}
    500 	sn->stats[size_bin][ndx0].tries += tries;
    501 	sn->stats[size_bin][ndx0].last_tx = ticks;
    502 	sn->stats[size_bin][ndx0].total_packets++;
    503 
    504 
    505 	if (ndx0 == sn->current_sample_ndx[size_bin]) {
    506 		DPRINTF(sc, "%s: %s size %d sample rate %d tries (%d/%d) tt %d avg_tt (%d/%d) status %d\n",
    507 			__func__, ether_sprintf(an->an_node.ni_macaddr),
    508 			size, rate, short_tries, tries, tt,
    509 			sn->stats[size_bin][ndx0].average_tx_time,
    510 			sn->stats[size_bin][ndx0].perfect_tx_time,
    511 			status);
    512 		sn->sample_tt[size_bin] = tt;
    513 		sn->current_sample_ndx[size_bin] = -1;
    514 	}
    515 }
    516 
    517 void
    518 ath_rate_tx_complete(struct ath_softc *sc, struct ath_node *an,
    519 	const struct ath_desc *ds, const struct ath_desc *ds0)
    520 {
    521 	struct sample_node *sn = ATH_NODE_SAMPLE(an);
    522 	const struct ar5212_desc *ads = (const struct ar5212_desc *)&ds->ds_ctl0;
    523 	int final_rate, short_tries, long_tries, frame_size;
    524 	int ndx = -1;
    525 
    526 	final_rate = sc->sc_hwmap[ds->ds_txstat.ts_rate &~ HAL_TXSTAT_ALTRATE].ieeerate;
    527 	short_tries = ds->ds_txstat.ts_shortretry + 1;
    528 	long_tries = ds->ds_txstat.ts_longretry + 1;
    529 	frame_size = ds0->ds_ctl0 & 0x0fff; /* low-order 12 bits of ds_ctl0 */
    530 	if (frame_size == 0)		    /* NB: should not happen */
    531 		frame_size = 1500;
    532 
    533 	if (sn->num_rates <= 0) {
    534 		DPRINTF(sc, "%s: %s size %d status %d rate/try %d/%d "
    535 			"no rates yet\n",
    536 			__func__, ether_sprintf(an->an_node.ni_macaddr),
    537 			bin_to_size(size_to_bin(frame_size)),
    538 			ds->ds_txstat.ts_status,
    539 			short_tries, long_tries);
    540 		return;
    541 	}
    542 
    543 	if (sc->sc_mrretry && ds->ds_txstat.ts_status) {
    544 		/* this packet failed */
    545 		DPRINTF(sc, "%s: %s size %d rate/try %d/%d %d/%d %d/%d %d/%d status %s retries (%d/%d)\n",
    546 			__func__,
    547 			ether_sprintf(an->an_node.ni_macaddr),
    548 			bin_to_size(size_to_bin(frame_size)),
    549 			sc->sc_hwmap[ads->xmit_rate0].ieeerate,
    550 				ads->xmit_tries0,
    551 			sc->sc_hwmap[ads->xmit_rate1].ieeerate,
    552 				ads->xmit_tries1,
    553 			sc->sc_hwmap[ads->xmit_rate2].ieeerate,
    554 				ads->xmit_tries2,
    555 			sc->sc_hwmap[ads->xmit_rate3].ieeerate,
    556 				ads->xmit_tries3,
    557 			ds->ds_txstat.ts_status ? "FAIL" : "OK",
    558 			short_tries,
    559 			long_tries);
    560 	}
    561 
    562 	if (!(ds->ds_txstat.ts_rate & HAL_TXSTAT_ALTRATE)) {
    563 		/* only one rate was used */
    564 		ndx = rate_to_ndx(sn, final_rate);
    565 		DPRINTF(sc, "%s: %s size %d status %d rate/try %d/%d/%d\n",
    566 			__func__, ether_sprintf(an->an_node.ni_macaddr),
    567 			bin_to_size(size_to_bin(frame_size)),
    568 			ds->ds_txstat.ts_status,
    569 			ndx, short_tries, long_tries);
    570 		if (ndx >= 0 && ndx < sn->num_rates) {
    571 			update_stats(sc, an, frame_size,
    572 				     ndx, long_tries,
    573 				     0, 0,
    574 				     0, 0,
    575 				     0, 0,
    576 				     short_tries, long_tries, ds->ds_txstat.ts_status);
    577 		}
    578 	} else {
    579 		int rate0, tries0, ndx0;
    580 		int rate1, tries1, ndx1;
    581 		int rate2, tries2, ndx2;
    582 		int rate3, tries3, ndx3;
    583 		int finalTSIdx = ads->final_ts_index;
    584 
    585 		/*
    586 		 * Process intermediate rates that failed.
    587 		 */
    588 
    589 		rate0 = sc->sc_hwmap[ads->xmit_rate0].ieeerate;
    590 		tries0 = ads->xmit_tries0;
    591 		ndx0 = rate_to_ndx(sn, rate0);
    592 
    593 		rate1 = sc->sc_hwmap[ads->xmit_rate1].ieeerate;
    594 		tries1 = ads->xmit_tries1;
    595 		ndx1 = rate_to_ndx(sn, rate1);
    596 
    597 		rate2 = sc->sc_hwmap[ads->xmit_rate2].ieeerate;
    598 		tries2 = ads->xmit_tries2;
    599 		ndx2 = rate_to_ndx(sn, rate2);
    600 
    601 		rate3 = sc->sc_hwmap[ads->xmit_rate3].ieeerate;
    602 		tries3 = ads->xmit_tries3;
    603 		ndx3 = rate_to_ndx(sn, rate3);
    604 
    605 #if 1
    606 		DPRINTF(sc, "%s: %s size %d finaltsidx %d tries %d status %d rate/try %d/%d %d/%d %d/%d %d/%d\n",
    607 			__func__, ether_sprintf(an->an_node.ni_macaddr),
    608 			bin_to_size(size_to_bin(frame_size)),
    609 			finalTSIdx,
    610 			long_tries,
    611 			ds->ds_txstat.ts_status,
    612 			rate0, tries0,
    613 			rate1, tries1,
    614 			rate2, tries2,
    615 			rate3, tries3);
    616 #endif
    617 
    618 		if (tries0) {
    619 			update_stats(sc, an, frame_size,
    620 				     ndx0, tries0,
    621 				     ndx1, tries1,
    622 				     ndx2, tries2,
    623 				     ndx3, tries3,
    624 				     short_tries, ds->ds_txstat.ts_longretry + 1,
    625 				     ds->ds_txstat.ts_status);
    626 		}
    627 
    628 		if (tries1 && finalTSIdx > 0) {
    629 			update_stats(sc, an, frame_size,
    630 				     ndx1, tries1,
    631 				     ndx2, tries2,
    632 				     ndx3, tries3,
    633 				     0, 0,
    634 				     short_tries, ds->ds_txstat.ts_longretry + 1 - tries0,
    635 				     ds->ds_txstat.ts_status);
    636 		}
    637 
    638 		if (tries2 && finalTSIdx > 1) {
    639 			update_stats(sc, an, frame_size,
    640 				     ndx2, tries2,
    641 				     ndx3, tries3,
    642 				     0, 0,
    643 				     0, 0,
    644 				     short_tries, ds->ds_txstat.ts_longretry + 1 - tries0 - tries1,
    645 				     ds->ds_txstat.ts_status);
    646 		}
    647 
    648 		if (tries3 && finalTSIdx > 2) {
    649 			update_stats(sc, an, frame_size,
    650 				     ndx3, tries3,
    651 				     0, 0,
    652 				     0, 0,
    653 				     0, 0,
    654 				     short_tries, ds->ds_txstat.ts_longretry + 1 - tries0 - tries1 - tries2,
    655 				     ds->ds_txstat.ts_status);
    656 		}
    657 	}
    658 }
    659 
    660 void
    661 ath_rate_newassoc(struct ath_softc *sc, struct ath_node *an, int isnew)
    662 {
    663 	DPRINTF(sc, "%s: %s isnew %d\n", __func__,
    664 		ether_sprintf(an->an_node.ni_macaddr), isnew);
    665 	if (isnew)
    666 		ath_rate_ctl_reset(sc, &an->an_node);
    667 }
    668 
    669 /*
    670  * Initialize the tables for a node.
    671  */
    672 static void
    673 ath_rate_ctl_reset(struct ath_softc *sc, struct ieee80211_node *ni)
    674 {
    675 #define	RATE(_ix)	(ni->ni_rates.rs_rates[(_ix)] & IEEE80211_RATE_VAL)
    676 	struct ieee80211com *ic = &sc->sc_ic;
    677 	struct ath_node *an = ATH_NODE(ni);
    678 	struct sample_node *sn = ATH_NODE_SAMPLE(an);
    679 	const HAL_RATE_TABLE *rt = sc->sc_currates;
    680 	int x, y, srate;
    681 
    682 	KASSERT(rt != NULL, ("no rate table, mode %u", sc->sc_curmode));
    683         sn->static_rate_ndx = -1;
    684 	if (ic->ic_fixed_rate != -1) {
    685 		/*
    686 		 * A fixed rate is to be used; ic_fixed_rate is an
    687 		 * index into the supported rate set.  Convert this
    688 		 * to the index into the negotiated rate set for
    689 		 * the node.  We know the rate is there because the
    690 		 * rate set is checked when the station associates.
    691 		 */
    692 		const struct ieee80211_rateset *rs =
    693 			&ic->ic_sup_rates[ic->ic_curmode];
    694 		int r = rs->rs_rates[ic->ic_fixed_rate] & IEEE80211_RATE_VAL;
    695 		/* NB: the rate set is assumed sorted */
    696 		srate = ni->ni_rates.rs_nrates - 1;
    697 		for (; srate >= 0 && RATE(srate) != r; srate--)
    698 			;
    699 		KASSERT(srate >= 0,
    700 			("fixed rate %d not in rate set", ic->ic_fixed_rate));
    701                 sn->static_rate_ndx = srate;
    702 	}
    703 
    704         DPRINTF(sc, "%s: %s size 1600 rate/tt", __func__, ether_sprintf(ni->ni_macaddr));
    705 
    706 	sn->num_rates = ni->ni_rates.rs_nrates;
    707         for (x = 0; x < ni->ni_rates.rs_nrates; x++) {
    708 		sn->rates[x].rate = ni->ni_rates.rs_rates[x] & IEEE80211_RATE_VAL;
    709 		sn->rates[x].rix = sc->sc_rixmap[sn->rates[x].rate];
    710 		sn->rates[x].rateCode = rt->info[sn->rates[x].rix].rateCode;
    711 		sn->rates[x].shortPreambleRateCode =
    712 			rt->info[sn->rates[x].rix].rateCode |
    713 			rt->info[sn->rates[x].rix].shortPreamble;
    714 
    715 		DPRINTF(sc, " %d/%d", sn->rates[x].rate,
    716 			calc_usecs_unicast_packet(sc, 1600, sn->rates[x].rix,
    717 						  0,0));
    718 	}
    719 	DPRINTF(sc, "%s\n", "");
    720 
    721 	/* set the visible bit-rate to the lowest one available */
    722 	ni->ni_txrate = 0;
    723 	sn->num_rates = ni->ni_rates.rs_nrates;
    724 
    725 	for (y = 0; y < NUM_PACKET_SIZE_BINS; y++) {
    726 		int size = bin_to_size(y);
    727 		sn->packets_sent[y] = 0;
    728 		sn->current_sample_ndx[y] = -1;
    729 		sn->last_sample_ndx[y] = 0;
    730 
    731 		for (x = 0; x < ni->ni_rates.rs_nrates; x++) {
    732 			sn->stats[y][x].successive_failures = 0;
    733 			sn->stats[y][x].tries = 0;
    734 			sn->stats[y][x].total_packets = 0;
    735 			sn->stats[y][x].packets_acked = 0;
    736 			sn->stats[y][x].last_tx = 0;
    737 
    738 			sn->stats[y][x].perfect_tx_time =
    739 				calc_usecs_unicast_packet(sc, size,
    740 							  sn->rates[x].rix,
    741 							  0, 0);
    742 			sn->stats[y][x].average_tx_time = sn->stats[y][x].perfect_tx_time;
    743 		}
    744 	}
    745 #undef RATE
    746 }
    747 
    748 static void
    749 rate_cb(void *arg, struct ieee80211_node *ni)
    750 {
    751 	struct ath_softc *sc = arg;
    752 
    753 	ath_rate_newassoc(sc, ATH_NODE(ni), 1);
    754 }
    755 
    756 /*
    757  * Reset the rate control state for each 802.11 state transition.
    758  */
    759 void
    760 ath_rate_newstate(struct ath_softc *sc, enum ieee80211_state state)
    761 {
    762 	struct ieee80211com *ic = &sc->sc_ic;
    763 
    764 	if (state == IEEE80211_S_RUN) {
    765 		if (ic->ic_opmode != IEEE80211_M_STA) {
    766 			/*
    767 			 * Sync rates for associated stations and neighbors.
    768 			 */
    769 			ieee80211_iterate_nodes(&ic->ic_sta, rate_cb, sc);
    770 		}
    771 		ath_rate_newassoc(sc, ATH_NODE(ic->ic_bss), 1);
    772 	}
    773 }
    774 
    775 static void
    776 ath_rate_sysctlattach(struct ath_softc *sc, struct sample_softc *osc)
    777 {
    778 	struct sysctl_ctx_list *ctx = device_get_sysctl_ctx(sc->sc_dev);
    779 	struct sysctl_oid *tree = device_get_sysctl_tree(sc->sc_dev);
    780 
    781 	/* XXX bounds check [0..100] */
    782 	SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
    783 		"smoothing_rate", CTLFLAG_RW, &osc->ath_smoothing_rate, 0,
    784 		"rate control: retry threshold to credit rate raise (%%)");
    785 	/* XXX bounds check [2..100] */
    786 	SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
    787 		"sample_rate", CTLFLAG_RW, &osc->ath_sample_rate,0,
    788 		"rate control: # good periods before raising rate");
    789 }
    790 
    791 struct ath_ratectrl *
    792 ath_rate_attach(struct ath_softc *sc)
    793 {
    794 	struct sample_softc *osc;
    795 
    796 	DPRINTF(sc, "%s:\n", __func__);
    797 	osc = malloc(sizeof(struct sample_softc), M_DEVBUF, M_NOWAIT|M_ZERO);
    798 	if (osc == NULL)
    799 		return NULL;
    800 	osc->arc.arc_space = sizeof(struct sample_node);
    801 	osc->ath_smoothing_rate = 95;	/* ewma percentage (out of 100) */
    802 	osc->ath_sample_rate = 10;	/* send a different bit-rate 1/X packets */
    803 	ath_rate_sysctlattach(sc, osc);
    804 	return &osc->arc;
    805 }
    806 
    807 void
    808 ath_rate_detach(struct ath_ratectrl *arc)
    809 {
    810 	struct sample_softc *osc = (struct sample_softc *) arc;
    811 
    812 	free(osc, M_DEVBUF);
    813 }
    814