1 /*- 2 * Copyright (c) 2004 INRIA 3 * Copyright (c) 2002-2005 Sam Leffler, Errno Consulting 4 * All rights reserved. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 9 * 1. Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer, 11 * without modification. 12 * 2. Redistributions in binary form must reproduce at minimum a disclaimer 13 * similar to the "NO WARRANTY" disclaimer below ("Disclaimer") and any 14 * redistribution must be conditioned upon including a substantially 15 * similar Disclaimer requirement for further binary redistribution. 16 * 3. Neither the names of the above-listed copyright holders nor the names 17 * of any contributors may be used to endorse or promote products derived 18 * from this software without specific prior written permission. 19 * 20 * Alternatively, this software may be distributed under the terms of the 21 * GNU General Public License ("GPL") version 2 as published by the Free 22 * Software Foundation. 23 * 24 * NO WARRANTY 25 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 26 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 27 * LIMITED TO, THE IMPLIED WARRANTIES OF NONINFRINGEMENT, MERCHANTIBILITY 28 * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL 29 * THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, 30 * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 31 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 32 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER 33 * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 34 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF 35 * THE POSSIBILITY OF SUCH DAMAGES. 36 * 37 */ 38 39 #include <sys/cdefs.h> 40 __FBSDID("$FreeBSD: src/sys/dev/ath/ath_rate/amrr/amrr.c,v 1.10 2005/08/09 10:19:43 rwatson Exp $"); 41 42 /* 43 * AMRR rate control. See: 44 * http://www-sop.inria.fr/rapports/sophia/RR-5208.html 45 * "IEEE 802.11 Rate Adaptation: A Practical Approach" by 46 * Mathieu Lacage, Hossein Manshaei, Thierry Turletti 47 */ 48 #include "opt_inet.h" 49 50 #include <sys/param.h> 51 #include <sys/systm.h> 52 #include <sys/sysctl.h> 53 #include <sys/kernel.h> 54 #include <sys/lock.h> 55 #include <sys/errno.h> 56 57 #include <machine/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/if_ether.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 #endif 73 74 #include <dev/ic/athvar.h> 75 #include <dev/ic/athrate-amrr.h> 76 #include <contrib/dev/ic/athhal_desc.h> 77 78 #define AMRR_DEBUG 79 #ifdef AMRR_DEBUG 80 #define DPRINTF(sc, _fmt, ...) do { \ 81 if (sc->sc_debug & 0x10) \ 82 printf(_fmt, __VA_ARGS__); \ 83 } while (0) 84 #else 85 #define DPRINTF(sc, _fmt, ...) 86 #endif 87 88 static int ath_rateinterval = 1000; /* rate ctl interval (ms) */ 89 static int ath_rate_max_success_threshold = 10; 90 static int ath_rate_min_success_threshold = 1; 91 92 static void ath_ratectl(void *); 93 static void ath_rate_update(struct ath_softc *, struct ieee80211_node *, 94 int rate); 95 static void ath_rate_ctl_start(struct ath_softc *, struct ieee80211_node *); 96 static void ath_rate_ctl(void *, struct ieee80211_node *); 97 98 void 99 ath_rate_node_init(struct ath_softc *sc, struct ath_node *an) 100 { 101 /* NB: assumed to be zero'd by caller */ 102 ath_rate_update(sc, &an->an_node, 0); 103 } 104 105 void 106 ath_rate_node_cleanup(struct ath_softc *sc, struct ath_node *an) 107 { 108 } 109 110 void 111 ath_rate_findrate(struct ath_softc *sc, struct ath_node *an, 112 int shortPreamble, size_t frameLen, 113 u_int8_t *rix, int *try0, u_int8_t *txrate) 114 { 115 struct amrr_node *amn = ATH_NODE_AMRR(an); 116 117 *rix = amn->amn_tx_rix0; 118 *try0 = amn->amn_tx_try0; 119 if (shortPreamble) 120 *txrate = amn->amn_tx_rate0sp; 121 else 122 *txrate = amn->amn_tx_rate0; 123 } 124 125 void 126 ath_rate_setupxtxdesc(struct ath_softc *sc, struct ath_node *an, 127 struct ath_desc *ds, int shortPreamble, u_int8_t rix) 128 { 129 struct amrr_node *amn = ATH_NODE_AMRR(an); 130 131 ath_hal_setupxtxdesc(sc->sc_ah, ds 132 , amn->amn_tx_rate1sp, amn->amn_tx_try1 /* series 1 */ 133 , amn->amn_tx_rate2sp, amn->amn_tx_try2 /* series 2 */ 134 , amn->amn_tx_rate3sp, amn->amn_tx_try3 /* series 3 */ 135 ); 136 } 137 138 void 139 ath_rate_tx_complete(struct ath_softc *sc, struct ath_node *an, 140 const struct ath_desc *ds, const struct ath_desc *ds0) 141 { 142 struct amrr_node *amn = ATH_NODE_AMRR(an); 143 int sr = ds->ds_txstat.ts_shortretry; 144 int lr = ds->ds_txstat.ts_longretry; 145 int retry_count = sr + lr; 146 147 amn->amn_tx_try0_cnt++; 148 if (retry_count == 1) { 149 amn->amn_tx_try1_cnt++; 150 } else if (retry_count == 2) { 151 amn->amn_tx_try1_cnt++; 152 amn->amn_tx_try2_cnt++; 153 } else if (retry_count == 3) { 154 amn->amn_tx_try1_cnt++; 155 amn->amn_tx_try2_cnt++; 156 amn->amn_tx_try3_cnt++; 157 } else if (retry_count > 3) { 158 amn->amn_tx_try1_cnt++; 159 amn->amn_tx_try2_cnt++; 160 amn->amn_tx_try3_cnt++; 161 amn->amn_tx_failure_cnt++; 162 } 163 } 164 165 void 166 ath_rate_newassoc(struct ath_softc *sc, struct ath_node *an, int isnew) 167 { 168 if (isnew) 169 ath_rate_ctl_start(sc, &an->an_node); 170 } 171 172 static void 173 node_reset (struct amrr_node *amn) 174 { 175 amn->amn_tx_try0_cnt = 0; 176 amn->amn_tx_try1_cnt = 0; 177 amn->amn_tx_try2_cnt = 0; 178 amn->amn_tx_try3_cnt = 0; 179 amn->amn_tx_failure_cnt = 0; 180 amn->amn_success = 0; 181 amn->amn_recovery = 0; 182 amn->amn_success_threshold = ath_rate_min_success_threshold; 183 } 184 185 186 /** 187 * The code below assumes that we are dealing with hardware multi rate retry 188 * I have no idea what will happen if you try to use this module with another 189 * type of hardware. Your machine might catch fire or it might work with 190 * horrible performance... 191 */ 192 static void 193 ath_rate_update(struct ath_softc *sc, struct ieee80211_node *ni, int rate) 194 { 195 struct ath_node *an = ATH_NODE(ni); 196 struct amrr_node *amn = ATH_NODE_AMRR(an); 197 const HAL_RATE_TABLE *rt = sc->sc_currates; 198 u_int8_t rix; 199 200 KASSERT(rt != NULL, ("no rate table, mode %u", sc->sc_curmode)); 201 202 DPRINTF(sc, "%s: set xmit rate for %s to %dM\n", 203 __func__, ether_sprintf(ni->ni_macaddr), 204 ni->ni_rates.rs_nrates > 0 ? 205 (ni->ni_rates.rs_rates[rate] & IEEE80211_RATE_VAL) / 2 : 0); 206 207 ni->ni_txrate = rate; 208 /* XXX management/control frames always go at the lowest speed */ 209 an->an_tx_mgtrate = rt->info[0].rateCode; 210 an->an_tx_mgtratesp = an->an_tx_mgtrate | rt->info[0].shortPreamble; 211 /* 212 * Before associating a node has no rate set setup 213 * so we can't calculate any transmit codes to use. 214 * This is ok since we should never be sending anything 215 * but management frames and those always go at the 216 * lowest hardware rate. 217 */ 218 if (ni->ni_rates.rs_nrates > 0) { 219 amn->amn_tx_rix0 = sc->sc_rixmap[ 220 ni->ni_rates.rs_rates[rate] & IEEE80211_RATE_VAL]; 221 amn->amn_tx_rate0 = rt->info[amn->amn_tx_rix0].rateCode; 222 amn->amn_tx_rate0sp = amn->amn_tx_rate0 | 223 rt->info[amn->amn_tx_rix0].shortPreamble; 224 if (sc->sc_mrretry) { 225 amn->amn_tx_try0 = 1; 226 amn->amn_tx_try1 = 1; 227 amn->amn_tx_try2 = 1; 228 amn->amn_tx_try3 = 1; 229 if (--rate >= 0) { 230 rix = sc->sc_rixmap[ 231 ni->ni_rates.rs_rates[rate]&IEEE80211_RATE_VAL]; 232 amn->amn_tx_rate1 = rt->info[rix].rateCode; 233 amn->amn_tx_rate1sp = amn->amn_tx_rate1 | 234 rt->info[rix].shortPreamble; 235 } else { 236 amn->amn_tx_rate1 = amn->amn_tx_rate1sp = 0; 237 } 238 if (--rate >= 0) { 239 rix = sc->sc_rixmap[ 240 ni->ni_rates.rs_rates[rate]&IEEE80211_RATE_VAL]; 241 amn->amn_tx_rate2 = rt->info[rix].rateCode; 242 amn->amn_tx_rate2sp = amn->amn_tx_rate2 | 243 rt->info[rix].shortPreamble; 244 } else { 245 amn->amn_tx_rate2 = amn->amn_tx_rate2sp = 0; 246 } 247 if (rate > 0) { 248 /* NB: only do this if we didn't already do it above */ 249 amn->amn_tx_rate3 = rt->info[0].rateCode; 250 amn->amn_tx_rate3sp = 251 an->an_tx_mgtrate | rt->info[0].shortPreamble; 252 } else { 253 amn->amn_tx_rate3 = amn->amn_tx_rate3sp = 0; 254 } 255 } else { 256 amn->amn_tx_try0 = ATH_TXMAXTRY; 257 /* theorically, these statements are useless because 258 * the code which uses them tests for an_tx_try0 == ATH_TXMAXTRY 259 */ 260 amn->amn_tx_try1 = 0; 261 amn->amn_tx_try2 = 0; 262 amn->amn_tx_try3 = 0; 263 amn->amn_tx_rate1 = amn->amn_tx_rate1sp = 0; 264 amn->amn_tx_rate2 = amn->amn_tx_rate2sp = 0; 265 amn->amn_tx_rate3 = amn->amn_tx_rate3sp = 0; 266 } 267 } 268 node_reset (amn); 269 } 270 271 /* 272 * Set the starting transmit rate for a node. 273 */ 274 static void 275 ath_rate_ctl_start(struct ath_softc *sc, struct ieee80211_node *ni) 276 { 277 #define RATE(_ix) (ni->ni_rates.rs_rates[(_ix)] & IEEE80211_RATE_VAL) 278 struct ieee80211com *ic = &sc->sc_ic; 279 int srate; 280 281 KASSERT(ni->ni_rates.rs_nrates > 0, ("no rates")); 282 if (ic->ic_fixed_rate == IEEE80211_FIXED_RATE_NONE) { 283 /* 284 * No fixed rate is requested. For 11b start with 285 * the highest negotiated rate; otherwise, for 11g 286 * and 11a, we start "in the middle" at 24Mb or 36Mb. 287 */ 288 srate = ni->ni_rates.rs_nrates - 1; 289 if (sc->sc_curmode != IEEE80211_MODE_11B) { 290 /* 291 * Scan the negotiated rate set to find the 292 * closest rate. 293 */ 294 /* NB: the rate set is assumed sorted */ 295 for (; srate >= 0 && RATE(srate) > 72; srate--) 296 ; 297 KASSERT(srate >= 0, ("bogus rate set")); 298 } 299 } else { 300 /* 301 * A fixed rate is to be used; ic_fixed_rate is an 302 * index into the supported rate set. Convert this 303 * to the index into the negotiated rate set for 304 * the node. We know the rate is there because the 305 * rate set is checked when the station associates. 306 */ 307 const struct ieee80211_rateset *rs = 308 &ic->ic_sup_rates[ic->ic_curmode]; 309 int r = rs->rs_rates[ic->ic_fixed_rate] & IEEE80211_RATE_VAL; 310 /* NB: the rate set is assumed sorted */ 311 srate = ni->ni_rates.rs_nrates - 1; 312 for (; srate >= 0 && RATE(srate) != r; srate--) 313 ; 314 KASSERT(srate >= 0, 315 ("fixed rate %d not in rate set", ic->ic_fixed_rate)); 316 } 317 ath_rate_update(sc, ni, srate); 318 #undef RATE 319 } 320 321 static void 322 ath_rate_cb(void *arg, struct ieee80211_node *ni) 323 { 324 struct ath_softc *sc = arg; 325 326 ath_rate_update(sc, ni, 0); 327 } 328 329 /* 330 * Reset the rate control state for each 802.11 state transition. 331 */ 332 void 333 ath_rate_newstate(struct ath_softc *sc, enum ieee80211_state state) 334 { 335 struct amrr_softc *asc = (struct amrr_softc *) sc->sc_rc; 336 struct ieee80211com *ic = &sc->sc_ic; 337 struct ieee80211_node *ni; 338 339 if (state == IEEE80211_S_INIT) { 340 callout_stop(&asc->timer); 341 return; 342 } 343 if (ic->ic_opmode == IEEE80211_M_STA) { 344 /* 345 * Reset local xmit state; this is really only 346 * meaningful when operating in station mode. 347 */ 348 ni = ic->ic_bss; 349 if (state == IEEE80211_S_RUN) { 350 ath_rate_ctl_start(sc, ni); 351 } else { 352 ath_rate_update(sc, ni, 0); 353 } 354 } else { 355 /* 356 * When operating as a station the node table holds 357 * the AP's that were discovered during scanning. 358 * For any other operating mode we want to reset the 359 * tx rate state of each node. 360 */ 361 ieee80211_iterate_nodes(&ic->ic_sta, ath_rate_cb, sc); 362 ath_rate_update(sc, ic->ic_bss, 0); 363 } 364 if (ic->ic_fixed_rate == IEEE80211_FIXED_RATE_NONE && 365 state == IEEE80211_S_RUN) { 366 int interval; 367 /* 368 * Start the background rate control thread if we 369 * are not configured to use a fixed xmit rate. 370 */ 371 interval = ath_rateinterval; 372 if (ic->ic_opmode == IEEE80211_M_STA) 373 interval /= 2; 374 callout_reset(&asc->timer, (interval * hz) / 1000, 375 ath_ratectl, &sc->sc_if); 376 } 377 } 378 379 /* 380 * Examine and potentially adjust the transmit rate. 381 */ 382 static void 383 ath_rate_ctl(void *arg, struct ieee80211_node *ni) 384 { 385 struct ath_softc *sc = arg; 386 struct amrr_node *amn = ATH_NODE_AMRR(ATH_NODE (ni)); 387 int old_rate; 388 389 #define is_success(amn) \ 390 (amn->amn_tx_try1_cnt < (amn->amn_tx_try0_cnt/10)) 391 #define is_enough(amn) \ 392 (amn->amn_tx_try0_cnt > 10) 393 #define is_failure(amn) \ 394 (amn->amn_tx_try1_cnt > (amn->amn_tx_try0_cnt/3)) 395 #define is_max_rate(ni) \ 396 ((ni->ni_txrate + 1) >= ni->ni_rates.rs_nrates) 397 #define is_min_rate(ni) \ 398 (ni->ni_txrate == 0) 399 400 old_rate = ni->ni_txrate; 401 402 DPRINTF (sc, "cnt0: %d cnt1: %d cnt2: %d cnt3: %d -- threshold: %d\n", 403 amn->amn_tx_try0_cnt, 404 amn->amn_tx_try1_cnt, 405 amn->amn_tx_try2_cnt, 406 amn->amn_tx_try3_cnt, 407 amn->amn_success_threshold); 408 if (is_success (amn) && is_enough (amn)) { 409 amn->amn_success++; 410 if (amn->amn_success == amn->amn_success_threshold && 411 !is_max_rate (ni)) { 412 amn->amn_recovery = 1; 413 amn->amn_success = 0; 414 ni->ni_txrate++; 415 DPRINTF (sc, "increase rate to %d\n", ni->ni_txrate); 416 } else { 417 amn->amn_recovery = 0; 418 } 419 } else if (is_failure (amn)) { 420 amn->amn_success = 0; 421 if (!is_min_rate (ni)) { 422 if (amn->amn_recovery) { 423 /* recovery failure. */ 424 amn->amn_success_threshold *= 2; 425 amn->amn_success_threshold = min (amn->amn_success_threshold, 426 (u_int)ath_rate_max_success_threshold); 427 DPRINTF (sc, "decrease rate recovery thr: %d\n", amn->amn_success_threshold); 428 } else { 429 /* simple failure. */ 430 amn->amn_success_threshold = ath_rate_min_success_threshold; 431 DPRINTF (sc, "decrease rate normal thr: %d\n", amn->amn_success_threshold); 432 } 433 amn->amn_recovery = 0; 434 ni->ni_txrate--; 435 } else { 436 amn->amn_recovery = 0; 437 } 438 439 } 440 if (is_enough (amn) || old_rate != ni->ni_txrate) { 441 /* reset counters. */ 442 amn->amn_tx_try0_cnt = 0; 443 amn->amn_tx_try1_cnt = 0; 444 amn->amn_tx_try2_cnt = 0; 445 amn->amn_tx_try3_cnt = 0; 446 amn->amn_tx_failure_cnt = 0; 447 } 448 if (old_rate != ni->ni_txrate) { 449 ath_rate_update(sc, ni, ni->ni_txrate); 450 } 451 } 452 453 static void 454 ath_ratectl(void *arg) 455 { 456 struct ifnet *ifp = arg; 457 struct ath_softc *sc = ifp->if_softc; 458 struct amrr_softc *asc = (struct amrr_softc *) sc->sc_rc; 459 struct ieee80211com *ic = &sc->sc_ic; 460 int interval; 461 462 if (ifp->if_drv_flags & IFF_DRV_RUNNING) { 463 sc->sc_stats.ast_rate_calls++; 464 465 if (ic->ic_opmode == IEEE80211_M_STA) 466 ath_rate_ctl(sc, ic->ic_bss); /* NB: no reference */ 467 else 468 ieee80211_iterate_nodes(&ic->ic_sta, ath_rate_ctl, sc); 469 } 470 interval = ath_rateinterval; 471 if (ic->ic_opmode == IEEE80211_M_STA) 472 interval /= 2; 473 callout_reset(&asc->timer, (interval * hz) / 1000, 474 ath_ratectl, &sc->sc_if); 475 } 476 477 static void 478 ath_rate_sysctlattach(struct ath_softc *sc) 479 { 480 struct sysctl_ctx_list *ctx = device_get_sysctl_ctx(sc->sc_dev); 481 struct sysctl_oid *tree = device_get_sysctl_tree(sc->sc_dev); 482 483 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO, 484 "rate_interval", CTLFLAG_RW, &ath_rateinterval, 0, 485 "rate control: operation interval (ms)"); 486 /* XXX bounds check values */ 487 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO, 488 "max_sucess_threshold", CTLFLAG_RW, 489 &ath_rate_max_success_threshold, 0, ""); 490 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO, 491 "min_sucess_threshold", CTLFLAG_RW, 492 &ath_rate_min_success_threshold, 0, ""); 493 } 494 495 struct ath_ratectrl * 496 ath_rate_attach(struct ath_softc *sc) 497 { 498 struct amrr_softc *asc; 499 500 asc = malloc(sizeof(struct amrr_softc), M_DEVBUF, M_NOWAIT|M_ZERO); 501 if (asc == NULL) 502 return NULL; 503 asc->arc.arc_space = sizeof(struct amrr_node); 504 callout_init(&asc->timer, debug_mpsafenet ? CALLOUT_MPSAFE : 0); 505 ath_rate_sysctlattach(sc); 506 507 return &asc->arc; 508 } 509 510 void 511 ath_rate_detach(struct ath_ratectrl *arc) 512 { 513 struct amrr_softc *asc = (struct amrr_softc *) arc; 514 515 callout_drain(&asc->timer); 516 free(asc, M_DEVBUF); 517 } 518