1fa20c234SSam Leffler /*- 2fa20c234SSam Leffler * Copyright (c) 2005 John Bicket 3fa20c234SSam Leffler * All rights reserved. 4fa20c234SSam Leffler * 5fa20c234SSam Leffler * Redistribution and use in source and binary forms, with or without 6fa20c234SSam Leffler * modification, are permitted provided that the following conditions 7fa20c234SSam Leffler * are met: 8fa20c234SSam Leffler * 1. Redistributions of source code must retain the above copyright 9fa20c234SSam Leffler * notice, this list of conditions and the following disclaimer, 10fa20c234SSam Leffler * without modification. 11fa20c234SSam Leffler * 2. Redistributions in binary form must reproduce at minimum a disclaimer 12fa20c234SSam Leffler * similar to the "NO WARRANTY" disclaimer below ("Disclaimer") and any 13fa20c234SSam Leffler * redistribution must be conditioned upon including a substantially 14fa20c234SSam Leffler * similar Disclaimer requirement for further binary redistribution. 15fa20c234SSam Leffler * 3. Neither the names of the above-listed copyright holders nor the names 16fa20c234SSam Leffler * of any contributors may be used to endorse or promote products derived 17fa20c234SSam Leffler * from this software without specific prior written permission. 18fa20c234SSam Leffler * 19fa20c234SSam Leffler * Alternatively, this software may be distributed under the terms of the 20fa20c234SSam Leffler * GNU General Public License ("GPL") version 2 as published by the Free 21fa20c234SSam Leffler * Software Foundation. 22fa20c234SSam Leffler * 23fa20c234SSam Leffler * NO WARRANTY 24fa20c234SSam Leffler * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 25fa20c234SSam Leffler * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 26fa20c234SSam Leffler * LIMITED TO, THE IMPLIED WARRANTIES OF NONINFRINGEMENT, MERCHANTIBILITY 27fa20c234SSam Leffler * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL 28fa20c234SSam Leffler * THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, 29fa20c234SSam Leffler * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 30fa20c234SSam Leffler * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 31fa20c234SSam Leffler * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER 32fa20c234SSam Leffler * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 33fa20c234SSam Leffler * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF 34fa20c234SSam Leffler * THE POSSIBILITY OF SUCH DAMAGES. 3568e8e04eSSam Leffler * 36fa20c234SSam Leffler */ 37fa20c234SSam Leffler 38fa20c234SSam Leffler #include <sys/cdefs.h> 39fa20c234SSam Leffler __FBSDID("$FreeBSD$"); 40fa20c234SSam Leffler 41fa20c234SSam Leffler /* 42fa20c234SSam Leffler * John Bicket's SampleRate control algorithm. 43fa20c234SSam Leffler */ 44fa20c234SSam Leffler #include "opt_inet.h" 45b032f27cSSam Leffler #include "opt_wlan.h" 46fa20c234SSam Leffler 47fa20c234SSam Leffler #include <sys/param.h> 48fa20c234SSam Leffler #include <sys/systm.h> 49fa20c234SSam Leffler #include <sys/sysctl.h> 50fa20c234SSam Leffler #include <sys/module.h> 51fa20c234SSam Leffler #include <sys/kernel.h> 52fa20c234SSam Leffler #include <sys/lock.h> 53fa20c234SSam Leffler #include <sys/mutex.h> 54fa20c234SSam Leffler #include <sys/errno.h> 55fa20c234SSam Leffler 56fa20c234SSam Leffler #include <machine/bus.h> 57fa20c234SSam Leffler #include <machine/resource.h> 58fa20c234SSam Leffler #include <sys/bus.h> 59fa20c234SSam Leffler 60fa20c234SSam Leffler #include <sys/socket.h> 61fa20c234SSam Leffler 62fa20c234SSam Leffler #include <net/if.h> 63fa20c234SSam Leffler #include <net/if_media.h> 64fa20c234SSam Leffler #include <net/if_arp.h> 65fa20c234SSam Leffler 66fa20c234SSam Leffler #include <net80211/ieee80211_var.h> 67fa20c234SSam Leffler 68fa20c234SSam Leffler #include <net/bpf.h> 69fa20c234SSam Leffler 70fa20c234SSam Leffler #ifdef INET 71fa20c234SSam Leffler #include <netinet/in.h> 72fa20c234SSam Leffler #include <netinet/if_ether.h> 73fa20c234SSam Leffler #endif 74fa20c234SSam Leffler 75fa20c234SSam Leffler #include <dev/ath/if_athvar.h> 76fa20c234SSam Leffler #include <dev/ath/ath_rate/sample/sample.h> 77fa20c234SSam Leffler #include <contrib/dev/ath/ah_desc.h> 78fa20c234SSam Leffler 79fa20c234SSam Leffler /* 80fa20c234SSam Leffler * This file is an implementation of the SampleRate algorithm 81fa20c234SSam Leffler * in "Bit-rate Selection in Wireless Networks" 82fa20c234SSam Leffler * (http://www.pdos.lcs.mit.edu/papers/jbicket-ms.ps) 83fa20c234SSam Leffler * 84fa20c234SSam Leffler * SampleRate chooses the bit-rate it predicts will provide the most 85fa20c234SSam Leffler * throughput based on estimates of the expected per-packet 86fa20c234SSam Leffler * transmission time for each bit-rate. SampleRate periodically sends 87fa20c234SSam Leffler * packets at bit-rates other than the current one to estimate when 88fa20c234SSam Leffler * another bit-rate will provide better performance. SampleRate 89fa20c234SSam Leffler * switches to another bit-rate when its estimated per-packet 90fa20c234SSam Leffler * transmission time becomes smaller than the current bit-rate's. 91fa20c234SSam Leffler * SampleRate reduces the number of bit-rates it must sample by 92fa20c234SSam Leffler * eliminating those that could not perform better than the one 93fa20c234SSam Leffler * currently being used. SampleRate also stops probing at a bit-rate 94fa20c234SSam Leffler * if it experiences several successive losses. 95fa20c234SSam Leffler * 96fa20c234SSam Leffler * The difference between the algorithm in the thesis and the one in this 97fa20c234SSam Leffler * file is that the one in this file uses a ewma instead of a window. 98fa20c234SSam Leffler * 99b91bf513SSam Leffler * Also, this implementation tracks the average transmission time for 100b91bf513SSam Leffler * a few different packet sizes independently for each link. 101fa20c234SSam Leffler */ 102fa20c234SSam Leffler 103b2763056SSam Leffler #define STALE_FAILURE_TIMEOUT_MS 10000 104b91bf513SSam Leffler #define MIN_SWITCH_MS 1000 105b2763056SSam Leffler 106b2763056SSam Leffler static void ath_rate_ctl_reset(struct ath_softc *, struct ieee80211_node *); 107fa20c234SSam Leffler 108b91bf513SSam Leffler static __inline int 109b91bf513SSam Leffler size_to_bin(int size) 110fa20c234SSam Leffler { 111fa20c234SSam Leffler int x = 0; 112fa20c234SSam Leffler for (x = 0; x < NUM_PACKET_SIZE_BINS; x++) { 11315f13975SSam Leffler if (size <= packet_size_bins[x]) { 114fa20c234SSam Leffler return x; 115fa20c234SSam Leffler } 116fa20c234SSam Leffler } 117fa20c234SSam Leffler return NUM_PACKET_SIZE_BINS-1; 118fa20c234SSam Leffler } 119b91bf513SSam Leffler static __inline int 120b91bf513SSam Leffler bin_to_size(int index) { 121fa20c234SSam Leffler return packet_size_bins[index]; 122fa20c234SSam Leffler } 123fa20c234SSam Leffler 1241bb9a085SSam Leffler static __inline int 1251bb9a085SSam Leffler rate_to_ndx(struct sample_node *sn, int rate) { 1261bb9a085SSam Leffler int x = 0; 1271bb9a085SSam Leffler for (x = 0; x < sn->num_rates; x++) { 1281bb9a085SSam Leffler if (sn->rates[x].rate == rate) { 1291bb9a085SSam Leffler return x; 1301bb9a085SSam Leffler } 1311bb9a085SSam Leffler } 1321bb9a085SSam Leffler return -1; 1331bb9a085SSam Leffler } 1341bb9a085SSam Leffler 135fa20c234SSam Leffler void 136fa20c234SSam Leffler ath_rate_node_init(struct ath_softc *sc, struct ath_node *an) 137fa20c234SSam Leffler { 138fa20c234SSam Leffler /* NB: assumed to be zero'd by caller */ 139fa20c234SSam Leffler } 140fa20c234SSam Leffler 141fa20c234SSam Leffler void 142fa20c234SSam Leffler ath_rate_node_cleanup(struct ath_softc *sc, struct ath_node *an) 143fa20c234SSam Leffler { 144fa20c234SSam Leffler } 145fa20c234SSam Leffler 146fa20c234SSam Leffler 147fa20c234SSam Leffler /* 148fa20c234SSam Leffler * returns the ndx with the lowest average_tx_time, 149fa20c234SSam Leffler * or -1 if all the average_tx_times are 0. 150fa20c234SSam Leffler */ 151b2763056SSam Leffler static __inline int best_rate_ndx(struct sample_node *sn, int size_bin, 152b2763056SSam Leffler int require_acked_before) 153fa20c234SSam Leffler { 154fa20c234SSam Leffler int x = 0; 155fa20c234SSam Leffler int best_rate_ndx = 0; 156fa20c234SSam Leffler int best_rate_tt = 0; 157fa20c234SSam Leffler for (x = 0; x < sn->num_rates; x++) { 158fa20c234SSam Leffler int tt = sn->stats[size_bin][x].average_tx_time; 159b2763056SSam Leffler if (tt <= 0 || (require_acked_before && 160b2763056SSam Leffler !sn->stats[size_bin][x].packets_acked)) { 161b2763056SSam Leffler continue; 162b2763056SSam Leffler } 163b91bf513SSam Leffler 164b91bf513SSam Leffler /* 9 megabits never works better than 12 */ 165b91bf513SSam Leffler if (sn->rates[x].rate == 18) 166b91bf513SSam Leffler continue; 167b91bf513SSam Leffler 168b91bf513SSam Leffler /* don't use a bit-rate that has been failing */ 169b91bf513SSam Leffler if (sn->stats[size_bin][x].successive_failures > 3) 170b91bf513SSam Leffler continue; 171b91bf513SSam Leffler 172fa20c234SSam Leffler if (!best_rate_tt || best_rate_tt > tt) { 173fa20c234SSam Leffler best_rate_tt = tt; 174fa20c234SSam Leffler best_rate_ndx = x; 175fa20c234SSam Leffler } 176fa20c234SSam Leffler } 177fa20c234SSam Leffler return (best_rate_tt) ? best_rate_ndx : -1; 178fa20c234SSam Leffler } 179fa20c234SSam Leffler 180fa20c234SSam Leffler /* 181b91bf513SSam Leffler * pick a good "random" bit-rate to sample other than the current one 182fa20c234SSam Leffler */ 183b91bf513SSam Leffler static __inline int 184b91bf513SSam Leffler pick_sample_ndx(struct sample_node *sn, int size_bin) 185fa20c234SSam Leffler { 186fa20c234SSam Leffler int x = 0; 187b2763056SSam Leffler int current_ndx = 0; 188b2763056SSam Leffler unsigned current_tt = 0; 189fa20c234SSam Leffler 190b2763056SSam Leffler current_ndx = sn->current_rate[size_bin]; 191b2763056SSam Leffler if (current_ndx < 0) { 192fa20c234SSam Leffler /* no successes yet, send at the lowest bit-rate */ 193fa20c234SSam Leffler return 0; 194fa20c234SSam Leffler } 195fa20c234SSam Leffler 196b2763056SSam Leffler current_tt = sn->stats[size_bin][current_ndx].average_tx_time; 197fa20c234SSam Leffler 198b2763056SSam Leffler for (x = 0; x < sn->num_rates; x++) { 199b2763056SSam Leffler int ndx = (sn->last_sample_ndx[size_bin]+1+x) % sn->num_rates; 200fa20c234SSam Leffler 201b91bf513SSam Leffler /* don't sample the current bit-rate */ 202b91bf513SSam Leffler if (ndx == current_ndx) 203b91bf513SSam Leffler continue; 204b91bf513SSam Leffler 205b91bf513SSam Leffler /* this bit-rate is always worse than the current one */ 206b91bf513SSam Leffler if (sn->stats[size_bin][ndx].perfect_tx_time > current_tt) 207b91bf513SSam Leffler continue; 208b91bf513SSam Leffler 209b91bf513SSam Leffler /* rarely sample bit-rates that fail a lot */ 210b91bf513SSam Leffler if (ticks - sn->stats[size_bin][ndx].last_tx < ((hz * STALE_FAILURE_TIMEOUT_MS)/1000) && 211b91bf513SSam Leffler sn->stats[size_bin][ndx].successive_failures > 3) 212b91bf513SSam Leffler continue; 213b91bf513SSam Leffler 214b91bf513SSam Leffler /* don't sample more than 2 indexes higher 215b91bf513SSam Leffler * for rates higher than 11 megabits 216b91bf513SSam Leffler */ 217b91bf513SSam Leffler if (sn->rates[ndx].rate > 22 && ndx > current_ndx + 2) 218b91bf513SSam Leffler continue; 219b91bf513SSam Leffler 220b91bf513SSam Leffler /* 9 megabits never works better than 12 */ 221b91bf513SSam Leffler if (sn->rates[ndx].rate == 18) 222b91bf513SSam Leffler continue; 223b91bf513SSam Leffler 224b91bf513SSam Leffler /* if we're using 11 megabits, only sample up to 12 megabits 225b91bf513SSam Leffler */ 226b91bf513SSam Leffler if (sn->rates[current_ndx].rate == 22 && ndx > current_ndx + 1) 227b91bf513SSam Leffler continue; 228b91bf513SSam Leffler 229b2763056SSam Leffler sn->last_sample_ndx[size_bin] = ndx; 230b2763056SSam Leffler return ndx; 231fa20c234SSam Leffler } 232b2763056SSam Leffler return current_ndx; 233fa20c234SSam Leffler } 234fa20c234SSam Leffler 235fa20c234SSam Leffler void 236fa20c234SSam Leffler ath_rate_findrate(struct ath_softc *sc, struct ath_node *an, 237a4d8dd10SSam Leffler int shortPreamble, size_t frameLen, 238fa20c234SSam Leffler u_int8_t *rix, int *try0, u_int8_t *txrate) 239fa20c234SSam Leffler { 240fa20c234SSam Leffler struct sample_node *sn = ATH_NODE_SAMPLE(an); 241fa20c234SSam Leffler struct sample_softc *ssc = ATH_SOFTC_SAMPLE(sc); 242b032f27cSSam Leffler struct ifnet *ifp = sc->sc_ifp; 243b032f27cSSam Leffler struct ieee80211com *ic = ifp->if_l2com; 244b91bf513SSam Leffler int ndx, size_bin, mrr, best_ndx, change_rates; 245b2763056SSam Leffler unsigned average_tx_time; 246fa20c234SSam Leffler 247b91bf513SSam Leffler mrr = sc->sc_mrretry && !(ic->ic_flags & IEEE80211_F_USEPROT); 248b2763056SSam Leffler size_bin = size_to_bin(frameLen); 249b2763056SSam Leffler best_ndx = best_rate_ndx(sn, size_bin, !mrr); 250fa20c234SSam Leffler 25143e9cf7cSSam Leffler if (best_ndx >= 0) { 252b2763056SSam Leffler average_tx_time = sn->stats[size_bin][best_ndx].average_tx_time; 253fa20c234SSam Leffler } else { 254b2763056SSam Leffler average_tx_time = 0; 255b2763056SSam Leffler } 256b91bf513SSam Leffler 257b2763056SSam Leffler if (sn->static_rate_ndx != -1) { 258b2763056SSam Leffler ndx = sn->static_rate_ndx; 259b2763056SSam Leffler *try0 = ATH_TXMAXTRY; 260b2763056SSam Leffler } else { 261b2763056SSam Leffler *try0 = mrr ? 2 : ATH_TXMAXTRY; 262b2763056SSam Leffler 263b91bf513SSam Leffler if (sn->sample_tt[size_bin] < average_tx_time * (sn->packets_since_sample[size_bin]*ssc->ath_sample_rate/100)) { 264fa20c234SSam Leffler /* 265b2763056SSam Leffler * we want to limit the time measuring the performance 266b2763056SSam Leffler * of other bit-rates to ath_sample_rate% of the 267b2763056SSam Leffler * total transmission time. 268fa20c234SSam Leffler */ 269fa20c234SSam Leffler ndx = pick_sample_ndx(sn, size_bin); 270b2763056SSam Leffler if (ndx != sn->current_rate[size_bin]) { 271b2763056SSam Leffler sn->current_sample_ndx[size_bin] = ndx; 272b2763056SSam Leffler } else { 273b2763056SSam Leffler sn->current_sample_ndx[size_bin] = -1; 274b2763056SSam Leffler } 275b2763056SSam Leffler sn->packets_since_sample[size_bin] = 0; 276b2763056SSam Leffler 277b2763056SSam Leffler } else { 278b91bf513SSam Leffler change_rates = 0; 279b91bf513SSam Leffler if (!sn->packets_sent[size_bin] || best_ndx == -1) { 280b91bf513SSam Leffler /* no packet has been sent successfully yet */ 281b91bf513SSam Leffler for (ndx = sn->num_rates-1; ndx > 0; ndx--) { 282b2763056SSam Leffler /* 283b91bf513SSam Leffler * pick the highest rate <= 36 Mbps 284b91bf513SSam Leffler * that hasn't failed. 285b2763056SSam Leffler */ 286b91bf513SSam Leffler if (sn->rates[ndx].rate <= 72 && 287b91bf513SSam Leffler sn->stats[size_bin][ndx].successive_failures == 0) { 288b91bf513SSam Leffler break; 289b91bf513SSam Leffler } 290b91bf513SSam Leffler } 291b91bf513SSam Leffler change_rates = 1; 292b91bf513SSam Leffler best_ndx = ndx; 293b91bf513SSam Leffler } else if (sn->packets_sent[size_bin] < 20) { 294b91bf513SSam Leffler /* let the bit-rate switch quickly during the first few packets */ 295b91bf513SSam Leffler change_rates = 1; 296b91bf513SSam Leffler } else if (ticks - ((hz*MIN_SWITCH_MS)/1000) > sn->ticks_since_switch[size_bin]) { 297b91bf513SSam Leffler /* 2 seconds have gone by */ 298b91bf513SSam Leffler change_rates = 1; 299b91bf513SSam Leffler } else if (average_tx_time * 2 < sn->stats[size_bin][sn->current_rate[size_bin]].average_tx_time) { 300b91bf513SSam Leffler /* the current bit-rate is twice as slow as the best one */ 301b91bf513SSam Leffler change_rates = 1; 302b91bf513SSam Leffler } 303b91bf513SSam Leffler 304b91bf513SSam Leffler sn->packets_since_sample[size_bin]++; 305b91bf513SSam Leffler 306b91bf513SSam Leffler if (change_rates) { 307b91bf513SSam Leffler if (best_ndx != sn->current_rate[size_bin]) { 308b032f27cSSam Leffler IEEE80211_NOTE(an->an_node.ni_vap, 309b032f27cSSam Leffler IEEE80211_MSG_RATECTL, 310b032f27cSSam Leffler &an->an_node, 311b032f27cSSam Leffler "%s: size %d switch rate %d (%d/%d) -> %d (%d/%d) after %d packets mrr %d", 312b2763056SSam Leffler __func__, 313b2763056SSam Leffler packet_size_bins[size_bin], 314b2763056SSam Leffler sn->rates[sn->current_rate[size_bin]].rate, 315b2763056SSam Leffler sn->stats[size_bin][sn->current_rate[size_bin]].average_tx_time, 316b2763056SSam Leffler sn->stats[size_bin][sn->current_rate[size_bin]].perfect_tx_time, 317b2763056SSam Leffler sn->rates[best_ndx].rate, 318b2763056SSam Leffler sn->stats[size_bin][best_ndx].average_tx_time, 319b2763056SSam Leffler sn->stats[size_bin][best_ndx].perfect_tx_time, 320b2763056SSam Leffler sn->packets_since_switch[size_bin], 321b2763056SSam Leffler mrr); 322b2763056SSam Leffler } 323b2763056SSam Leffler sn->packets_since_switch[size_bin] = 0; 324b2763056SSam Leffler sn->current_rate[size_bin] = best_ndx; 325b91bf513SSam Leffler sn->ticks_since_switch[size_bin] = ticks; 326b032f27cSSam Leffler /* 327b032f27cSSam Leffler * Set the visible txrate for this node. 328b032f27cSSam Leffler */ 329b032f27cSSam Leffler an->an_node.ni_txrate = sn->rates[best_ndx].rate; 330b2763056SSam Leffler } 331b2763056SSam Leffler ndx = sn->current_rate[size_bin]; 332b2763056SSam Leffler sn->packets_since_switch[size_bin]++; 333b91bf513SSam Leffler } 334fa20c234SSam Leffler } 335fa20c234SSam Leffler 336b91bf513SSam Leffler KASSERT(ndx >= 0 && ndx < sn->num_rates, ("ndx is %d", ndx)); 337fa20c234SSam Leffler 338fa20c234SSam Leffler *rix = sn->rates[ndx].rix; 339fa20c234SSam Leffler if (shortPreamble) { 340fa20c234SSam Leffler *txrate = sn->rates[ndx].shortPreambleRateCode; 341fa20c234SSam Leffler } else { 342fa20c234SSam Leffler *txrate = sn->rates[ndx].rateCode; 343fa20c234SSam Leffler } 344fa20c234SSam Leffler sn->packets_sent[size_bin]++; 345fa20c234SSam Leffler } 346fa20c234SSam Leffler 347fa20c234SSam Leffler void 348fa20c234SSam Leffler ath_rate_setupxtxdesc(struct ath_softc *sc, struct ath_node *an, 349a4d8dd10SSam Leffler struct ath_desc *ds, int shortPreamble, u_int8_t rix) 350fa20c234SSam Leffler { 351fa20c234SSam Leffler struct sample_node *sn = ATH_NODE_SAMPLE(an); 352fa20c234SSam Leffler int rateCode = -1; 353b91bf513SSam Leffler int frame_size = 0; 354b91bf513SSam Leffler int size_bin = 0; 355b91bf513SSam Leffler int ndx = 0; 356fa20c234SSam Leffler 357b91bf513SSam Leffler size_bin = size_to_bin(frame_size); // TODO: it's correct that frame_size alway 0 ? 358b91bf513SSam Leffler ndx = sn->current_rate[size_bin]; /* retry at the current bit-rate */ 359fa20c234SSam Leffler 360b91bf513SSam Leffler if (!sn->stats[size_bin][ndx].packets_acked) { 361b91bf513SSam Leffler ndx = 0; /* use the lowest bit-rate */ 362b2763056SSam Leffler } 363b91bf513SSam Leffler 364fa20c234SSam Leffler if (shortPreamble) { 365b2763056SSam Leffler rateCode = sn->rates[ndx].shortPreambleRateCode; 366fa20c234SSam Leffler } else { 367b2763056SSam Leffler rateCode = sn->rates[ndx].rateCode; 368fa20c234SSam Leffler } 369fa20c234SSam Leffler ath_hal_setupxtxdesc(sc->sc_ah, ds 370b2763056SSam Leffler , rateCode, 3 /* series 1 */ 371b2763056SSam Leffler , sn->rates[0].rateCode, 3 /* series 2 */ 372b2763056SSam Leffler , 0, 0 /* series 3 */ 373fa20c234SSam Leffler ); 374fa20c234SSam Leffler } 375fa20c234SSam Leffler 376b2763056SSam Leffler static void 377b2763056SSam Leffler update_stats(struct ath_softc *sc, struct ath_node *an, 378b2763056SSam Leffler int frame_size, 379b2763056SSam Leffler int ndx0, int tries0, 380b2763056SSam Leffler int ndx1, int tries1, 381b2763056SSam Leffler int ndx2, int tries2, 382b2763056SSam Leffler int ndx3, int tries3, 383b2763056SSam Leffler int short_tries, int tries, int status) 384fa20c234SSam Leffler { 385fa20c234SSam Leffler struct sample_node *sn = ATH_NODE_SAMPLE(an); 386fa20c234SSam Leffler struct sample_softc *ssc = ATH_SOFTC_SAMPLE(sc); 387fa20c234SSam Leffler int tt = 0; 388b2763056SSam Leffler int tries_so_far = 0; 389b2763056SSam Leffler int size_bin = 0; 390b2763056SSam Leffler int size = 0; 391b2763056SSam Leffler int rate = 0; 392fa20c234SSam Leffler 393fa20c234SSam Leffler size_bin = size_to_bin(frame_size); 394fa20c234SSam Leffler size = bin_to_size(size_bin); 39565f9edeeSSam Leffler 396ec9ee5e7SSam Leffler if (!(0 <= ndx0 && ndx0 < sn->num_rates)) 39765f9edeeSSam Leffler return; 398b2763056SSam Leffler rate = sn->rates[ndx0].rate; 399fa20c234SSam Leffler 400b2763056SSam Leffler tt += calc_usecs_unicast_packet(sc, size, sn->rates[ndx0].rix, 40165f9edeeSSam Leffler short_tries, 402b2763056SSam Leffler MIN(tries0, tries) - 1); 403b2763056SSam Leffler tries_so_far += tries0; 404b2763056SSam Leffler if (tries1 && tries0 < tries) { 405ec9ee5e7SSam Leffler if (!(0 <= ndx1 && ndx1 < sn->num_rates)) 40665f9edeeSSam Leffler return; 407b2763056SSam Leffler tt += calc_usecs_unicast_packet(sc, size, sn->rates[ndx1].rix, 40865f9edeeSSam Leffler short_tries, 409b2763056SSam Leffler MIN(tries1 + tries_so_far, tries) - tries_so_far - 1); 410b2763056SSam Leffler } 411b2763056SSam Leffler tries_so_far += tries1; 412fa20c234SSam Leffler 413b2763056SSam Leffler if (tries2 && tries0 + tries1 < tries) { 414ec9ee5e7SSam Leffler if (!(0 <= ndx2 && ndx2 < sn->num_rates)) 41565f9edeeSSam Leffler return; 416b2763056SSam Leffler tt += calc_usecs_unicast_packet(sc, size, sn->rates[ndx2].rix, 41765f9edeeSSam Leffler short_tries, 418b2763056SSam Leffler MIN(tries2 + tries_so_far, tries) - tries_so_far - 1); 419b2763056SSam Leffler } 420b2763056SSam Leffler 421b2763056SSam Leffler tries_so_far += tries2; 422b2763056SSam Leffler 423b2763056SSam Leffler if (tries3 && tries0 + tries1 + tries2 < tries) { 424ec9ee5e7SSam Leffler if (!(0 <= ndx3 && ndx3 < sn->num_rates)) 42565f9edeeSSam Leffler return; 426b2763056SSam Leffler tt += calc_usecs_unicast_packet(sc, size, sn->rates[ndx3].rix, 42765f9edeeSSam Leffler short_tries, 428b2763056SSam Leffler MIN(tries3 + tries_so_far, tries) - tries_so_far - 1); 429b2763056SSam Leffler } 430b2763056SSam Leffler if (sn->stats[size_bin][ndx0].total_packets < (100 / (100 - ssc->ath_smoothing_rate))) { 431fa20c234SSam Leffler /* just average the first few packets */ 432b2763056SSam Leffler int avg_tx = sn->stats[size_bin][ndx0].average_tx_time; 433b2763056SSam Leffler int packets = sn->stats[size_bin][ndx0].total_packets; 434b2763056SSam Leffler sn->stats[size_bin][ndx0].average_tx_time = (tt+(avg_tx*packets))/(packets+1); 435fa20c234SSam Leffler } else { 436fa20c234SSam Leffler /* use a ewma */ 437b2763056SSam Leffler sn->stats[size_bin][ndx0].average_tx_time = 438b2763056SSam Leffler ((sn->stats[size_bin][ndx0].average_tx_time * ssc->ath_smoothing_rate) + 439fa20c234SSam Leffler (tt * (100 - ssc->ath_smoothing_rate))) / 100; 440fa20c234SSam Leffler } 441fa20c234SSam Leffler 442b2763056SSam Leffler if (status) { 443fa20c234SSam Leffler int y; 444b91bf513SSam Leffler sn->stats[size_bin][ndx0].successive_failures++; 445b91bf513SSam Leffler for (y = size_bin+1; y < NUM_PACKET_SIZE_BINS; y++) { 446b91bf513SSam Leffler /* also say larger packets failed since we 447b91bf513SSam Leffler * assume if a small packet fails at a lower 448b91bf513SSam Leffler * bit-rate then a larger one will also. 449b91bf513SSam Leffler */ 450b2763056SSam Leffler sn->stats[y][ndx0].successive_failures++; 451b2763056SSam Leffler sn->stats[y][ndx0].last_tx = ticks; 452b91bf513SSam Leffler sn->stats[y][ndx0].tries += tries; 453b91bf513SSam Leffler sn->stats[y][ndx0].total_packets++; 454fa20c234SSam Leffler } 455fa20c234SSam Leffler } else { 456b2763056SSam Leffler sn->stats[size_bin][ndx0].packets_acked++; 457b2763056SSam Leffler sn->stats[size_bin][ndx0].successive_failures = 0; 458fa20c234SSam Leffler } 459b2763056SSam Leffler sn->stats[size_bin][ndx0].tries += tries; 460b2763056SSam Leffler sn->stats[size_bin][ndx0].last_tx = ticks; 461b2763056SSam Leffler sn->stats[size_bin][ndx0].total_packets++; 462b2763056SSam Leffler 463b2763056SSam Leffler 464b2763056SSam Leffler if (ndx0 == sn->current_sample_ndx[size_bin]) { 465b032f27cSSam Leffler IEEE80211_NOTE(an->an_node.ni_vap, IEEE80211_MSG_RATECTL, 466b032f27cSSam Leffler &an->an_node, 467b032f27cSSam Leffler "%s: size %d %s sample rate %d tries (%d/%d) tt %d avg_tt (%d/%d)", 468b032f27cSSam Leffler __func__, 46965f9edeeSSam Leffler size, 47065f9edeeSSam Leffler status ? "FAIL" : "OK", 47165f9edeeSSam Leffler rate, short_tries, tries, tt, 472b2763056SSam Leffler sn->stats[size_bin][ndx0].average_tx_time, 47365f9edeeSSam Leffler sn->stats[size_bin][ndx0].perfect_tx_time); 474b2763056SSam Leffler sn->sample_tt[size_bin] = tt; 475b2763056SSam Leffler sn->current_sample_ndx[size_bin] = -1; 476b2763056SSam Leffler } 477b2763056SSam Leffler } 478b2763056SSam Leffler 479ec9ee5e7SSam Leffler static void 480ec9ee5e7SSam Leffler badrate(struct ifnet *ifp, int series, int hwrate, int tries, int status) 481ec9ee5e7SSam Leffler { 482ec9ee5e7SSam Leffler if_printf(ifp, "bad series%d hwrate 0x%x, tries %u ts_status 0x%x\n", 483ec9ee5e7SSam Leffler series, hwrate, tries, status); 484ec9ee5e7SSam Leffler } 485ec9ee5e7SSam Leffler 486b2763056SSam Leffler void 48743e9cf7cSSam Leffler ath_rate_tx_complete(struct ath_softc *sc, struct ath_node *an, 48865f9edeeSSam Leffler const struct ath_buf *bf) 489b2763056SSam Leffler { 490b032f27cSSam Leffler struct ifnet *ifp = sc->sc_ifp; 491b032f27cSSam Leffler struct ieee80211com *ic = ifp->if_l2com; 492b2763056SSam Leffler struct sample_node *sn = ATH_NODE_SAMPLE(an); 49365f9edeeSSam Leffler const struct ath_tx_status *ts = &bf->bf_status.ds_txstat; 49465f9edeeSSam Leffler const struct ath_desc *ds0 = &bf->bf_desc[0]; 4951bb9a085SSam Leffler int final_rate, short_tries, long_tries, frame_size; 49646d4d74cSSam Leffler const HAL_RATE_TABLE *rt = sc->sc_currates; 4971bb9a085SSam Leffler int mrr; 498b2763056SSam Leffler 49946d4d74cSSam Leffler final_rate = sc->sc_hwmap[ 50046d4d74cSSam Leffler rt->rateCodeToIndex[ts->ts_rate &~ HAL_TXSTAT_ALTRATE]].ieeerate; 50165f9edeeSSam Leffler short_tries = ts->ts_shortretry; 50265f9edeeSSam Leffler long_tries = ts->ts_longretry + 1; 50343e9cf7cSSam Leffler frame_size = ds0->ds_ctl0 & 0x0fff; /* low-order 12 bits of ds_ctl0 */ 50443e9cf7cSSam Leffler if (frame_size == 0) /* NB: should not happen */ 505b2763056SSam Leffler frame_size = 1500; 506b2763056SSam Leffler 507b2763056SSam Leffler if (sn->num_rates <= 0) { 508b032f27cSSam Leffler IEEE80211_NOTE(an->an_node.ni_vap, IEEE80211_MSG_RATECTL, 509b032f27cSSam Leffler &an->an_node, 510b032f27cSSam Leffler "%s: size %d %s rate/try %d/%d no rates yet", 511b032f27cSSam Leffler __func__, 51243e9cf7cSSam Leffler bin_to_size(size_to_bin(frame_size)), 51365f9edeeSSam Leffler ts->ts_status ? "FAIL" : "OK", 51443e9cf7cSSam Leffler short_tries, long_tries); 515b2763056SSam Leffler return; 516b2763056SSam Leffler } 51765f9edeeSSam Leffler mrr = sc->sc_mrretry && !(ic->ic_flags & IEEE80211_F_USEPROT); 51865f9edeeSSam Leffler if (!mrr || !(ts->ts_rate & HAL_TXSTAT_ALTRATE)) { 5191bb9a085SSam Leffler int ndx = rate_to_ndx(sn, final_rate); 5201bb9a085SSam Leffler 521ec9ee5e7SSam Leffler if (ndx < 0) { 522ec9ee5e7SSam Leffler badrate(ifp, 0, ts->ts_rate, long_tries, ts->ts_status); 523ec9ee5e7SSam Leffler return; 524ec9ee5e7SSam Leffler } 52565f9edeeSSam Leffler /* 52665f9edeeSSam Leffler * Only one rate was used; optimize work. 52765f9edeeSSam Leffler */ 528b032f27cSSam Leffler IEEE80211_NOTE(an->an_node.ni_vap, IEEE80211_MSG_RATECTL, 529b032f27cSSam Leffler &an->an_node, "%s: size %d %s rate/try %d/%d/%d", 530b032f27cSSam Leffler __func__, 53143e9cf7cSSam Leffler bin_to_size(size_to_bin(frame_size)), 53265f9edeeSSam Leffler ts->ts_status ? "FAIL" : "OK", 5331bb9a085SSam Leffler final_rate, short_tries, long_tries); 534b2763056SSam Leffler update_stats(sc, an, frame_size, 5351bb9a085SSam Leffler ndx, long_tries, 536b2763056SSam Leffler 0, 0, 537b2763056SSam Leffler 0, 0, 538b2763056SSam Leffler 0, 0, 53965f9edeeSSam Leffler short_tries, long_tries, ts->ts_status); 540b2763056SSam Leffler } else { 5411bb9a085SSam Leffler int hwrate0, rate0, tries0, ndx0; 5421bb9a085SSam Leffler int hwrate1, rate1, tries1, ndx1; 5431bb9a085SSam Leffler int hwrate2, rate2, tries2, ndx2; 5441bb9a085SSam Leffler int hwrate3, rate3, tries3, ndx3; 54565f9edeeSSam Leffler int finalTSIdx = ts->ts_finaltsi; 546b2763056SSam Leffler 547b2763056SSam Leffler /* 548b2763056SSam Leffler * Process intermediate rates that failed. 549b2763056SSam Leffler */ 550517eabc6SSam Leffler if (sc->sc_ah->ah_magic != 0x20065416) { 551517eabc6SSam Leffler hwrate0 = MS(ds0->ds_ctl3, AR_XmitRate0); 552517eabc6SSam Leffler hwrate1 = MS(ds0->ds_ctl3, AR_XmitRate1); 553517eabc6SSam Leffler hwrate2 = MS(ds0->ds_ctl3, AR_XmitRate2); 554517eabc6SSam Leffler hwrate3 = MS(ds0->ds_ctl3, AR_XmitRate3); 555517eabc6SSam Leffler } else { 556517eabc6SSam Leffler hwrate0 = MS(ds0->ds_ctl3, AR5416_XmitRate0); 557517eabc6SSam Leffler hwrate1 = MS(ds0->ds_ctl3, AR5416_XmitRate1); 558517eabc6SSam Leffler hwrate2 = MS(ds0->ds_ctl3, AR5416_XmitRate2); 559517eabc6SSam Leffler hwrate3 = MS(ds0->ds_ctl3, AR5416_XmitRate3); 560517eabc6SSam Leffler } 561517eabc6SSam Leffler 56246d4d74cSSam Leffler rate0 = sc->sc_hwmap[rt->rateCodeToIndex[hwrate0]].ieeerate; 56365f9edeeSSam Leffler tries0 = MS(ds0->ds_ctl2, AR_XmitDataTries0); 5641bb9a085SSam Leffler ndx0 = rate_to_ndx(sn, rate0); 565b2763056SSam Leffler 56646d4d74cSSam Leffler rate1 = sc->sc_hwmap[rt->rateCodeToIndex[hwrate1]].ieeerate; 56765f9edeeSSam Leffler tries1 = MS(ds0->ds_ctl2, AR_XmitDataTries1); 5681bb9a085SSam Leffler ndx1 = rate_to_ndx(sn, rate1); 569b2763056SSam Leffler 57046d4d74cSSam Leffler rate2 = sc->sc_hwmap[rt->rateCodeToIndex[hwrate2]].ieeerate; 57165f9edeeSSam Leffler tries2 = MS(ds0->ds_ctl2, AR_XmitDataTries2); 5721bb9a085SSam Leffler ndx2 = rate_to_ndx(sn, rate2); 573b2763056SSam Leffler 57446d4d74cSSam Leffler rate3 = sc->sc_hwmap[rt->rateCodeToIndex[hwrate3]].ieeerate; 57565f9edeeSSam Leffler tries3 = MS(ds0->ds_ctl2, AR_XmitDataTries3); 5761bb9a085SSam Leffler ndx3 = rate_to_ndx(sn, rate3); 577b2763056SSam Leffler 578ec9ee5e7SSam Leffler if (tries0 && ndx0 < 0) 579ec9ee5e7SSam Leffler badrate(ifp, 0, hwrate0, tries0, ts->ts_status); 580ec9ee5e7SSam Leffler if (tries1 && ndx1 < 0) 581ec9ee5e7SSam Leffler badrate(ifp, 1, hwrate1, tries1, ts->ts_status); 582ec9ee5e7SSam Leffler if (tries2 && ndx2 < 0) 583ec9ee5e7SSam Leffler badrate(ifp, 2, hwrate2, tries2, ts->ts_status); 584ec9ee5e7SSam Leffler if (tries3 && ndx3 < 0) 585ec9ee5e7SSam Leffler badrate(ifp, 3, hwrate3, tries3, ts->ts_status); 586ec9ee5e7SSam Leffler 587b032f27cSSam Leffler IEEE80211_NOTE(an->an_node.ni_vap, IEEE80211_MSG_RATECTL, 588b032f27cSSam Leffler &an->an_node, 589b032f27cSSam Leffler "%s: size %d finaltsidx %d tries %d %s rate/try [%d/%d %d/%d %d/%d %d/%d]", 590b032f27cSSam Leffler __func__, 591b2763056SSam Leffler bin_to_size(size_to_bin(frame_size)), 592b2763056SSam Leffler finalTSIdx, 593b2763056SSam Leffler long_tries, 59465f9edeeSSam Leffler ts->ts_status ? "FAIL" : "OK", 5951bb9a085SSam Leffler rate0, tries0, 5961bb9a085SSam Leffler rate1, tries1, 5971bb9a085SSam Leffler rate2, tries2, 5981bb9a085SSam Leffler rate3, tries3); 599b2763056SSam Leffler 60065f9edeeSSam Leffler /* 60165f9edeeSSam Leffler * NB: series > 0 are not penalized for failure 60265f9edeeSSam Leffler * based on the try counts under the assumption 60365f9edeeSSam Leffler * that losses are often bursty and since we 60465f9edeeSSam Leffler * sample higher rates 1 try at a time doing so 60565f9edeeSSam Leffler * may unfairly penalize them. 60665f9edeeSSam Leffler */ 607b2763056SSam Leffler if (tries0) { 608b2763056SSam Leffler update_stats(sc, an, frame_size, 609b2763056SSam Leffler ndx0, tries0, 610b2763056SSam Leffler ndx1, tries1, 611b2763056SSam Leffler ndx2, tries2, 612b2763056SSam Leffler ndx3, tries3, 61365f9edeeSSam Leffler short_tries, long_tries, 614b91bf513SSam Leffler long_tries > tries0); 61565f9edeeSSam Leffler long_tries -= tries0; 616b2763056SSam Leffler } 617b2763056SSam Leffler 618b2763056SSam Leffler if (tries1 && finalTSIdx > 0) { 619b2763056SSam Leffler update_stats(sc, an, frame_size, 620b2763056SSam Leffler ndx1, tries1, 621b2763056SSam Leffler ndx2, tries2, 622b2763056SSam Leffler ndx3, tries3, 623b2763056SSam Leffler 0, 0, 62465f9edeeSSam Leffler short_tries, long_tries, 62565f9edeeSSam Leffler ts->ts_status); 62665f9edeeSSam Leffler long_tries -= tries1; 627b2763056SSam Leffler } 628b2763056SSam Leffler 629b2763056SSam Leffler if (tries2 && finalTSIdx > 1) { 630b2763056SSam Leffler update_stats(sc, an, frame_size, 631b2763056SSam Leffler ndx2, tries2, 632b2763056SSam Leffler ndx3, tries3, 633b2763056SSam Leffler 0, 0, 634b2763056SSam Leffler 0, 0, 63565f9edeeSSam Leffler short_tries, long_tries, 63665f9edeeSSam Leffler ts->ts_status); 63765f9edeeSSam Leffler long_tries -= tries2; 638b2763056SSam Leffler } 639b2763056SSam Leffler 640b2763056SSam Leffler if (tries3 && finalTSIdx > 2) { 641b2763056SSam Leffler update_stats(sc, an, frame_size, 642b2763056SSam Leffler ndx3, tries3, 643b2763056SSam Leffler 0, 0, 644b2763056SSam Leffler 0, 0, 645b2763056SSam Leffler 0, 0, 64665f9edeeSSam Leffler short_tries, long_tries, 64765f9edeeSSam Leffler ts->ts_status); 648b2763056SSam Leffler } 649b2763056SSam Leffler } 650fa20c234SSam Leffler } 651fa20c234SSam Leffler 652fa20c234SSam Leffler void 653fa20c234SSam Leffler ath_rate_newassoc(struct ath_softc *sc, struct ath_node *an, int isnew) 654fa20c234SSam Leffler { 655fa20c234SSam Leffler if (isnew) 656b2763056SSam Leffler ath_rate_ctl_reset(sc, &an->an_node); 657fa20c234SSam Leffler } 658fa20c234SSam Leffler 659fa20c234SSam Leffler /* 660fa20c234SSam Leffler * Initialize the tables for a node. 661fa20c234SSam Leffler */ 662fa20c234SSam Leffler static void 663b2763056SSam Leffler ath_rate_ctl_reset(struct ath_softc *sc, struct ieee80211_node *ni) 664fa20c234SSam Leffler { 665fa20c234SSam Leffler #define RATE(_ix) (ni->ni_rates.rs_rates[(_ix)] & IEEE80211_RATE_VAL) 666fa20c234SSam Leffler struct ath_node *an = ATH_NODE(ni); 667c62362cbSSam Leffler const struct ieee80211_txparam *tp = ni->ni_txparms; 668fa20c234SSam Leffler struct sample_node *sn = ATH_NODE_SAMPLE(an); 669fa20c234SSam Leffler const HAL_RATE_TABLE *rt = sc->sc_currates; 670b2763056SSam Leffler int x, y, srate; 671fa20c234SSam Leffler 672fa20c234SSam Leffler KASSERT(rt != NULL, ("no rate table, mode %u", sc->sc_curmode)); 673fa20c234SSam Leffler sn->static_rate_ndx = -1; 674b032f27cSSam Leffler if (tp != NULL && tp->ucastrate != IEEE80211_FIXED_RATE_NONE) { 675fa20c234SSam Leffler /* 67668e8e04eSSam Leffler * A fixed rate is to be used; ic_fixed_rate is the 67768e8e04eSSam Leffler * IEEE code for this rate (sans basic bit). Convert this 678fa20c234SSam Leffler * to the index into the negotiated rate set for 679aaba14d7SSam Leffler * the node. 680fa20c234SSam Leffler */ 681fa20c234SSam Leffler /* NB: the rate set is assumed sorted */ 682fa20c234SSam Leffler srate = ni->ni_rates.rs_nrates - 1; 683b032f27cSSam Leffler for (; srate >= 0 && RATE(srate) != tp->ucastrate; srate--) 684fa20c234SSam Leffler ; 68568e8e04eSSam Leffler /* 68668e8e04eSSam Leffler * The fixed rate may not be available due to races 68768e8e04eSSam Leffler * and mode settings. Also orphaned nodes created in 68868e8e04eSSam Leffler * adhoc mode may not have any rate set so this lookup 68968e8e04eSSam Leffler * can fail. 69068e8e04eSSam Leffler */ 69168e8e04eSSam Leffler if (srate >= 0) 692fa20c234SSam Leffler sn->static_rate_ndx = srate; 693fa20c234SSam Leffler } 694b2763056SSam Leffler 695fa20c234SSam Leffler sn->num_rates = ni->ni_rates.rs_nrates; 696fa20c234SSam Leffler for (x = 0; x < ni->ni_rates.rs_nrates; x++) { 697fa20c234SSam Leffler sn->rates[x].rate = ni->ni_rates.rs_rates[x] & IEEE80211_RATE_VAL; 698fa20c234SSam Leffler sn->rates[x].rix = sc->sc_rixmap[sn->rates[x].rate]; 6990ae09ec5SSam Leffler if (sn->rates[x].rix == 0xff) { 700b032f27cSSam Leffler IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_RATECTL, ni, 701b032f27cSSam Leffler "%s: ignore bogus rix at %d", __func__, x); 7020ae09ec5SSam Leffler continue; 7030ae09ec5SSam Leffler } 704fa20c234SSam Leffler sn->rates[x].rateCode = rt->info[sn->rates[x].rix].rateCode; 705fa20c234SSam Leffler sn->rates[x].shortPreambleRateCode = 706fa20c234SSam Leffler rt->info[sn->rates[x].rix].rateCode | 707fa20c234SSam Leffler rt->info[sn->rates[x].rix].shortPreamble; 708b2763056SSam Leffler } 709b032f27cSSam Leffler #ifdef IEEE80211_DEBUG 710b032f27cSSam Leffler if (ieee80211_msg(ni->ni_vap, IEEE80211_MSG_RATECTL)) { 711b032f27cSSam Leffler ieee80211_note(ni->ni_vap, "[%6D] %s: size 1600 rate/tt", 712b032f27cSSam Leffler __func__, ni->ni_macaddr, ":"); 713b032f27cSSam Leffler for (x = 0; x < sn->num_rates; x++) { 714b032f27cSSam Leffler if (sn->rates[x].rix == 0xff) 715b032f27cSSam Leffler continue; 716b032f27cSSam Leffler printf(" %d/%d", sn->rates[x].rate, 717b032f27cSSam Leffler calc_usecs_unicast_packet(sc, 1600, 718b032f27cSSam Leffler sn->rates[x].rix, 0,0)); 719b032f27cSSam Leffler } 720b032f27cSSam Leffler printf("\n"); 721b032f27cSSam Leffler } 722b032f27cSSam Leffler #endif 723b2763056SSam Leffler for (y = 0; y < NUM_PACKET_SIZE_BINS; y++) { 724b2763056SSam Leffler int size = bin_to_size(y); 725b91bf513SSam Leffler int ndx = 0; 726b2763056SSam Leffler sn->packets_sent[y] = 0; 727b2763056SSam Leffler sn->current_sample_ndx[y] = -1; 728b2763056SSam Leffler sn->last_sample_ndx[y] = 0; 729b2763056SSam Leffler 730b2763056SSam Leffler for (x = 0; x < ni->ni_rates.rs_nrates; x++) { 731b2763056SSam Leffler sn->stats[y][x].successive_failures = 0; 732b2763056SSam Leffler sn->stats[y][x].tries = 0; 733b2763056SSam Leffler sn->stats[y][x].total_packets = 0; 734b2763056SSam Leffler sn->stats[y][x].packets_acked = 0; 735b2763056SSam Leffler sn->stats[y][x].last_tx = 0; 736b2763056SSam Leffler 737b2763056SSam Leffler sn->stats[y][x].perfect_tx_time = 738b2763056SSam Leffler calc_usecs_unicast_packet(sc, size, 739b2763056SSam Leffler sn->rates[x].rix, 740b2763056SSam Leffler 0, 0); 741b2763056SSam Leffler sn->stats[y][x].average_tx_time = sn->stats[y][x].perfect_tx_time; 742b2763056SSam Leffler } 743b91bf513SSam Leffler 744b91bf513SSam Leffler /* set the initial rate */ 745b91bf513SSam Leffler for (ndx = sn->num_rates-1; ndx > 0; ndx--) { 746b91bf513SSam Leffler if (sn->rates[ndx].rate <= 72) { 747b91bf513SSam Leffler break; 748b2763056SSam Leffler } 749b91bf513SSam Leffler } 750b91bf513SSam Leffler sn->current_rate[y] = ndx; 751b91bf513SSam Leffler } 752b91bf513SSam Leffler 753b032f27cSSam Leffler IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_RATECTL, ni, 754b032f27cSSam Leffler "%s: %d rates %d%sMbps (%dus)- %d%sMbps (%dus)", __func__, 755b91bf513SSam Leffler sn->num_rates, 756b91bf513SSam Leffler sn->rates[0].rate/2, sn->rates[0].rate % 0x1 ? ".5" : "", 757b91bf513SSam Leffler sn->stats[1][0].perfect_tx_time, 758b91bf513SSam Leffler sn->rates[sn->num_rates-1].rate/2, 759b91bf513SSam Leffler sn->rates[sn->num_rates-1].rate % 0x1 ? ".5" : "", 760b91bf513SSam Leffler sn->stats[1][sn->num_rates-1].perfect_tx_time 761b91bf513SSam Leffler ); 762b91bf513SSam Leffler 763b032f27cSSam Leffler /* set the visible bit-rate */ 764d0d425bfSSam Leffler if (sn->static_rate_ndx != -1) 765b032f27cSSam Leffler ni->ni_txrate = sn->rates[sn->static_rate_ndx].rate; 766d0d425bfSSam Leffler else 767b032f27cSSam Leffler ni->ni_txrate = sn->rates[0].rate; 768fa20c234SSam Leffler #undef RATE 769fa20c234SSam Leffler } 770fa20c234SSam Leffler 771f0fd5e07SSam Leffler static void 772fa20c234SSam Leffler ath_rate_sysctlattach(struct ath_softc *sc, struct sample_softc *osc) 773fa20c234SSam Leffler { 774fa20c234SSam Leffler struct sysctl_ctx_list *ctx = device_get_sysctl_ctx(sc->sc_dev); 775fa20c234SSam Leffler struct sysctl_oid *tree = device_get_sysctl_tree(sc->sc_dev); 776fa20c234SSam Leffler 777fa20c234SSam Leffler /* XXX bounds check [0..100] */ 778fa20c234SSam Leffler SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO, 779fa20c234SSam Leffler "smoothing_rate", CTLFLAG_RW, &osc->ath_smoothing_rate, 0, 780fa20c234SSam Leffler "rate control: retry threshold to credit rate raise (%%)"); 781fa20c234SSam Leffler /* XXX bounds check [2..100] */ 782fa20c234SSam Leffler SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO, 783fa20c234SSam Leffler "sample_rate", CTLFLAG_RW, &osc->ath_sample_rate,0, 784fa20c234SSam Leffler "rate control: # good periods before raising rate"); 785fa20c234SSam Leffler } 786fa20c234SSam Leffler 787fa20c234SSam Leffler struct ath_ratectrl * 788fa20c234SSam Leffler ath_rate_attach(struct ath_softc *sc) 789fa20c234SSam Leffler { 790fa20c234SSam Leffler struct sample_softc *osc; 791fa20c234SSam Leffler 792fa20c234SSam Leffler osc = malloc(sizeof(struct sample_softc), M_DEVBUF, M_NOWAIT|M_ZERO); 793fa20c234SSam Leffler if (osc == NULL) 794fa20c234SSam Leffler return NULL; 795fa20c234SSam Leffler osc->arc.arc_space = sizeof(struct sample_node); 796fa20c234SSam Leffler osc->ath_smoothing_rate = 95; /* ewma percentage (out of 100) */ 797fa20c234SSam Leffler osc->ath_sample_rate = 10; /* send a different bit-rate 1/X packets */ 798fa20c234SSam Leffler ath_rate_sysctlattach(sc, osc); 799fa20c234SSam Leffler return &osc->arc; 800fa20c234SSam Leffler } 801fa20c234SSam Leffler 802fa20c234SSam Leffler void 803fa20c234SSam Leffler ath_rate_detach(struct ath_ratectrl *arc) 804fa20c234SSam Leffler { 805fa20c234SSam Leffler struct sample_softc *osc = (struct sample_softc *) arc; 806fa20c234SSam Leffler 807fa20c234SSam Leffler free(osc, M_DEVBUF); 808fa20c234SSam Leffler } 809fa20c234SSam Leffler 810fa20c234SSam Leffler /* 811fa20c234SSam Leffler * Module glue. 812fa20c234SSam Leffler */ 813fa20c234SSam Leffler static int 814fa20c234SSam Leffler sample_modevent(module_t mod, int type, void *unused) 815fa20c234SSam Leffler { 816fa20c234SSam Leffler switch (type) { 817fa20c234SSam Leffler case MOD_LOAD: 818fa20c234SSam Leffler if (bootverbose) 81943e9cf7cSSam Leffler printf("ath_rate: version 1.2 <SampleRate bit-rate selection algorithm>\n"); 820fa20c234SSam Leffler return 0; 821fa20c234SSam Leffler case MOD_UNLOAD: 822fa20c234SSam Leffler return 0; 823fa20c234SSam Leffler } 824fa20c234SSam Leffler return EINVAL; 825fa20c234SSam Leffler } 826fa20c234SSam Leffler 827fa20c234SSam Leffler static moduledata_t sample_mod = { 828fa20c234SSam Leffler "ath_rate", 829fa20c234SSam Leffler sample_modevent, 830fa20c234SSam Leffler 0 831fa20c234SSam Leffler }; 832fa20c234SSam Leffler DECLARE_MODULE(ath_rate, sample_mod, SI_SUB_DRIVERS, SI_ORDER_FIRST); 833fa20c234SSam Leffler MODULE_VERSION(ath_rate, 1); 8344e860beeSTai-hwa Liang MODULE_DEPEND(ath_rate, ath_hal, 1, 1, 1); /* Atheros HAL */ 835fa20c234SSam Leffler MODULE_DEPEND(ath_rate, wlan, 1, 1, 1); 836