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$"); 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_ath.h" 49 #include "opt_inet.h" 50 #include "opt_wlan.h" 51 52 #include <sys/param.h> 53 #include <sys/systm.h> 54 #include <sys/sysctl.h> 55 #include <sys/kernel.h> 56 #include <sys/lock.h> 57 #include <sys/mutex.h> 58 #include <sys/errno.h> 59 60 #if defined(__DragonFly__) 61 /* empty */ 62 #else 63 #include <machine/bus.h> 64 #include <machine/resource.h> 65 #endif 66 #include <sys/bus.h> 67 68 #include <sys/socket.h> 69 70 #include <net/if.h> 71 #include <net/if_media.h> 72 #include <net/if_arp.h> 73 74 #include <netproto/802_11/ieee80211_var.h> 75 76 #include <net/bpf.h> 77 78 #ifdef INET 79 #include <netinet/in.h> 80 #include <netinet/if_ether.h> 81 #endif 82 83 #include <dev/netif/ath/ath/if_athvar.h> 84 #include <dev/netif/ath/ath_rate/amrr/amrr.h> 85 #include <dev/netif/ath/ath_hal/ah_desc.h> 86 87 static int ath_rateinterval = 1000; /* rate ctl interval (ms) */ 88 static int ath_rate_max_success_threshold = 10; 89 static int ath_rate_min_success_threshold = 1; 90 91 static void ath_rate_update(struct ath_softc *, struct ieee80211_node *, 92 int rate); 93 static void ath_rate_ctl_start(struct ath_softc *, struct ieee80211_node *); 94 static void ath_rate_ctl(void *, struct ieee80211_node *); 95 96 void 97 ath_rate_node_init(struct ath_softc *sc, struct ath_node *an) 98 { 99 /* NB: assumed to be zero'd by caller */ 100 } 101 102 void 103 ath_rate_node_cleanup(struct ath_softc *sc, struct ath_node *an) 104 { 105 } 106 107 void 108 ath_rate_findrate(struct ath_softc *sc, struct ath_node *an, 109 int shortPreamble, size_t frameLen, 110 u_int8_t *rix, int *try0, u_int8_t *txrate) 111 { 112 struct amrr_node *amn = ATH_NODE_AMRR(an); 113 114 *rix = amn->amn_tx_rix0; 115 *try0 = amn->amn_tx_try0; 116 if (shortPreamble) 117 *txrate = amn->amn_tx_rate0sp; 118 else 119 *txrate = amn->amn_tx_rate0; 120 } 121 122 /* 123 * Get the TX rates. 124 * 125 * The short preamble bits aren't set here; the caller should augment 126 * the returned rate with the relevant preamble rate flag. 127 */ 128 void 129 ath_rate_getxtxrates(struct ath_softc *sc, struct ath_node *an, 130 uint8_t rix0, struct ath_rc_series *rc) 131 { 132 struct amrr_node *amn = ATH_NODE_AMRR(an); 133 134 rc[0].flags = rc[1].flags = rc[2].flags = rc[3].flags = 0; 135 136 rc[0].rix = amn->amn_tx_rate0; 137 rc[1].rix = amn->amn_tx_rate1; 138 rc[2].rix = amn->amn_tx_rate2; 139 rc[3].rix = amn->amn_tx_rate3; 140 141 rc[0].tries = amn->amn_tx_try0; 142 rc[1].tries = amn->amn_tx_try1; 143 rc[2].tries = amn->amn_tx_try2; 144 rc[3].tries = amn->amn_tx_try3; 145 } 146 147 148 void 149 ath_rate_setupxtxdesc(struct ath_softc *sc, struct ath_node *an, 150 struct ath_desc *ds, int shortPreamble, u_int8_t rix) 151 { 152 struct amrr_node *amn = ATH_NODE_AMRR(an); 153 154 ath_hal_setupxtxdesc(sc->sc_ah, ds 155 , amn->amn_tx_rate1sp, amn->amn_tx_try1 /* series 1 */ 156 , amn->amn_tx_rate2sp, amn->amn_tx_try2 /* series 2 */ 157 , amn->amn_tx_rate3sp, amn->amn_tx_try3 /* series 3 */ 158 ); 159 } 160 161 void 162 ath_rate_tx_complete(struct ath_softc *sc, struct ath_node *an, 163 const struct ath_rc_series *rc, const struct ath_tx_status *ts, 164 int frame_size, int nframes, int nbad) 165 { 166 struct amrr_node *amn = ATH_NODE_AMRR(an); 167 int sr = ts->ts_shortretry; 168 int lr = ts->ts_longretry; 169 int retry_count = sr + lr; 170 171 amn->amn_tx_try0_cnt++; 172 if (retry_count == 1) { 173 amn->amn_tx_try1_cnt++; 174 } else if (retry_count == 2) { 175 amn->amn_tx_try1_cnt++; 176 amn->amn_tx_try2_cnt++; 177 } else if (retry_count == 3) { 178 amn->amn_tx_try1_cnt++; 179 amn->amn_tx_try2_cnt++; 180 amn->amn_tx_try3_cnt++; 181 } else if (retry_count > 3) { 182 amn->amn_tx_try1_cnt++; 183 amn->amn_tx_try2_cnt++; 184 amn->amn_tx_try3_cnt++; 185 amn->amn_tx_failure_cnt++; 186 } 187 if (amn->amn_interval != 0 && 188 ticks - amn->amn_ticks > amn->amn_interval) { 189 ath_rate_ctl(sc, &an->an_node); 190 amn->amn_ticks = ticks; 191 } 192 } 193 194 void 195 ath_rate_newassoc(struct ath_softc *sc, struct ath_node *an, int isnew) 196 { 197 if (isnew) 198 ath_rate_ctl_start(sc, &an->an_node); 199 } 200 201 static void 202 node_reset(struct amrr_node *amn) 203 { 204 amn->amn_tx_try0_cnt = 0; 205 amn->amn_tx_try1_cnt = 0; 206 amn->amn_tx_try2_cnt = 0; 207 amn->amn_tx_try3_cnt = 0; 208 amn->amn_tx_failure_cnt = 0; 209 amn->amn_success = 0; 210 amn->amn_recovery = 0; 211 amn->amn_success_threshold = ath_rate_min_success_threshold; 212 } 213 214 215 /** 216 * The code below assumes that we are dealing with hardware multi rate retry 217 * I have no idea what will happen if you try to use this module with another 218 * type of hardware. Your machine might catch fire or it might work with 219 * horrible performance... 220 */ 221 static void 222 ath_rate_update(struct ath_softc *sc, struct ieee80211_node *ni, int rate) 223 { 224 struct ath_node *an = ATH_NODE(ni); 225 struct amrr_node *amn = ATH_NODE_AMRR(an); 226 struct ieee80211vap *vap = ni->ni_vap; 227 const HAL_RATE_TABLE *rt = sc->sc_currates; 228 u_int8_t rix; 229 230 KASSERT(rt != NULL, ("no rate table, mode %u", sc->sc_curmode)); 231 232 IEEE80211_NOTE(vap, IEEE80211_MSG_RATECTL, ni, 233 "%s: set xmit rate to %dM", __func__, 234 ni->ni_rates.rs_nrates > 0 ? 235 (ni->ni_rates.rs_rates[rate] & IEEE80211_RATE_VAL) / 2 : 0); 236 237 amn->amn_rix = rate; 238 /* 239 * Before associating a node has no rate set setup 240 * so we can't calculate any transmit codes to use. 241 * This is ok since we should never be sending anything 242 * but management frames and those always go at the 243 * lowest hardware rate. 244 */ 245 if (ni->ni_rates.rs_nrates > 0) { 246 ni->ni_txrate = ni->ni_rates.rs_rates[rate] & IEEE80211_RATE_VAL; 247 amn->amn_tx_rix0 = sc->sc_rixmap[ni->ni_txrate]; 248 amn->amn_tx_rate0 = rt->info[amn->amn_tx_rix0].rateCode; 249 amn->amn_tx_rate0sp = amn->amn_tx_rate0 | 250 rt->info[amn->amn_tx_rix0].shortPreamble; 251 if (sc->sc_mrretry) { 252 amn->amn_tx_try0 = 1; 253 amn->amn_tx_try1 = 1; 254 amn->amn_tx_try2 = 1; 255 amn->amn_tx_try3 = 1; 256 if (--rate >= 0) { 257 rix = sc->sc_rixmap[ 258 ni->ni_rates.rs_rates[rate]&IEEE80211_RATE_VAL]; 259 amn->amn_tx_rate1 = rt->info[rix].rateCode; 260 amn->amn_tx_rate1sp = amn->amn_tx_rate1 | 261 rt->info[rix].shortPreamble; 262 } else { 263 amn->amn_tx_rate1 = amn->amn_tx_rate1sp = 0; 264 } 265 if (--rate >= 0) { 266 rix = sc->sc_rixmap[ 267 ni->ni_rates.rs_rates[rate]&IEEE80211_RATE_VAL]; 268 amn->amn_tx_rate2 = rt->info[rix].rateCode; 269 amn->amn_tx_rate2sp = amn->amn_tx_rate2 | 270 rt->info[rix].shortPreamble; 271 } else { 272 amn->amn_tx_rate2 = amn->amn_tx_rate2sp = 0; 273 } 274 if (rate > 0) { 275 /* NB: only do this if we didn't already do it above */ 276 amn->amn_tx_rate3 = rt->info[0].rateCode; 277 amn->amn_tx_rate3sp = 278 amn->amn_tx_rate3 | rt->info[0].shortPreamble; 279 } else { 280 amn->amn_tx_rate3 = amn->amn_tx_rate3sp = 0; 281 } 282 } else { 283 amn->amn_tx_try0 = ATH_TXMAXTRY; 284 /* theorically, these statements are useless because 285 * the code which uses them tests for an_tx_try0 == ATH_TXMAXTRY 286 */ 287 amn->amn_tx_try1 = 0; 288 amn->amn_tx_try2 = 0; 289 amn->amn_tx_try3 = 0; 290 amn->amn_tx_rate1 = amn->amn_tx_rate1sp = 0; 291 amn->amn_tx_rate2 = amn->amn_tx_rate2sp = 0; 292 amn->amn_tx_rate3 = amn->amn_tx_rate3sp = 0; 293 } 294 } 295 node_reset(amn); 296 297 amn->amn_interval = ath_rateinterval; 298 if (vap->iv_opmode == IEEE80211_M_STA) 299 amn->amn_interval /= 2; 300 amn->amn_interval = (amn->amn_interval * hz) / 1000; 301 } 302 303 /* 304 * Set the starting transmit rate for a node. 305 */ 306 static void 307 ath_rate_ctl_start(struct ath_softc *sc, struct ieee80211_node *ni) 308 { 309 #define RATE(_ix) (ni->ni_rates.rs_rates[(_ix)] & IEEE80211_RATE_VAL) 310 const struct ieee80211_txparam *tp = ni->ni_txparms; 311 int srate; 312 313 KASSERT(ni->ni_rates.rs_nrates > 0, ("no rates")); 314 if (tp == NULL || tp->ucastrate == IEEE80211_FIXED_RATE_NONE) { 315 /* 316 * No fixed rate is requested. For 11b start with 317 * the highest negotiated rate; otherwise, for 11g 318 * and 11a, we start "in the middle" at 24Mb or 36Mb. 319 */ 320 srate = ni->ni_rates.rs_nrates - 1; 321 if (sc->sc_curmode != IEEE80211_MODE_11B) { 322 /* 323 * Scan the negotiated rate set to find the 324 * closest rate. 325 */ 326 /* NB: the rate set is assumed sorted */ 327 for (; srate >= 0 && RATE(srate) > 72; srate--) 328 ; 329 } 330 } else { 331 /* 332 * A fixed rate is to be used; ic_fixed_rate is the 333 * IEEE code for this rate (sans basic bit). Convert this 334 * to the index into the negotiated rate set for 335 * the node. We know the rate is there because the 336 * rate set is checked when the station associates. 337 */ 338 /* NB: the rate set is assumed sorted */ 339 srate = ni->ni_rates.rs_nrates - 1; 340 for (; srate >= 0 && RATE(srate) != tp->ucastrate; srate--) 341 ; 342 } 343 /* 344 * The selected rate may not be available due to races 345 * and mode settings. Also orphaned nodes created in 346 * adhoc mode may not have any rate set so this lookup 347 * can fail. This is not fatal. 348 */ 349 ath_rate_update(sc, ni, srate < 0 ? 0 : srate); 350 #undef RATE 351 } 352 353 /* 354 * Examine and potentially adjust the transmit rate. 355 */ 356 static void 357 ath_rate_ctl(void *arg, struct ieee80211_node *ni) 358 { 359 struct ath_softc *sc = arg; 360 struct amrr_node *amn = ATH_NODE_AMRR(ATH_NODE (ni)); 361 int rix; 362 363 #define is_success(amn) \ 364 (amn->amn_tx_try1_cnt < (amn->amn_tx_try0_cnt/10)) 365 #define is_enough(amn) \ 366 (amn->amn_tx_try0_cnt > 10) 367 #define is_failure(amn) \ 368 (amn->amn_tx_try1_cnt > (amn->amn_tx_try0_cnt/3)) 369 370 rix = amn->amn_rix; 371 372 IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_RATECTL, ni, 373 "cnt0: %d cnt1: %d cnt2: %d cnt3: %d -- threshold: %d", 374 amn->amn_tx_try0_cnt, amn->amn_tx_try1_cnt, amn->amn_tx_try2_cnt, 375 amn->amn_tx_try3_cnt, amn->amn_success_threshold); 376 if (is_success (amn) && is_enough (amn)) { 377 amn->amn_success++; 378 if (amn->amn_success == amn->amn_success_threshold && 379 rix + 1 < ni->ni_rates.rs_nrates) { 380 amn->amn_recovery = 1; 381 amn->amn_success = 0; 382 rix++; 383 IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_RATECTL, ni, 384 "increase rate to %d", rix); 385 } else { 386 amn->amn_recovery = 0; 387 } 388 } else if (is_failure (amn)) { 389 amn->amn_success = 0; 390 if (rix > 0) { 391 if (amn->amn_recovery) { 392 /* recovery failure. */ 393 amn->amn_success_threshold *= 2; 394 amn->amn_success_threshold = min (amn->amn_success_threshold, 395 (u_int)ath_rate_max_success_threshold); 396 IEEE80211_NOTE(ni->ni_vap, 397 IEEE80211_MSG_RATECTL, ni, 398 "decrease rate recovery thr: %d", 399 amn->amn_success_threshold); 400 } else { 401 /* simple failure. */ 402 amn->amn_success_threshold = ath_rate_min_success_threshold; 403 IEEE80211_NOTE(ni->ni_vap, 404 IEEE80211_MSG_RATECTL, ni, 405 "decrease rate normal thr: %d", 406 amn->amn_success_threshold); 407 } 408 amn->amn_recovery = 0; 409 rix--; 410 } else { 411 amn->amn_recovery = 0; 412 } 413 414 } 415 if (is_enough (amn) || rix != amn->amn_rix) { 416 /* reset counters. */ 417 amn->amn_tx_try0_cnt = 0; 418 amn->amn_tx_try1_cnt = 0; 419 amn->amn_tx_try2_cnt = 0; 420 amn->amn_tx_try3_cnt = 0; 421 amn->amn_tx_failure_cnt = 0; 422 } 423 if (rix != amn->amn_rix) { 424 ath_rate_update(sc, ni, rix); 425 } 426 } 427 428 static int 429 ath_rate_fetch_node_stats(struct ath_softc *sc, struct ath_node *an, 430 struct ath_rateioctl *re) 431 { 432 433 return (EINVAL); 434 } 435 436 static void 437 ath_rate_sysctlattach(struct ath_softc *sc) 438 { 439 struct sysctl_ctx_list *ctx = device_get_sysctl_ctx(sc->sc_dev); 440 struct sysctl_oid *tree = device_get_sysctl_tree(sc->sc_dev); 441 442 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO, 443 "rate_interval", CTLFLAG_RW, &ath_rateinterval, 0, 444 "rate control: operation interval (ms)"); 445 /* XXX bounds check values */ 446 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO, 447 "max_sucess_threshold", CTLFLAG_RW, 448 &ath_rate_max_success_threshold, 0, ""); 449 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO, 450 "min_sucess_threshold", CTLFLAG_RW, 451 &ath_rate_min_success_threshold, 0, ""); 452 } 453 454 struct ath_ratectrl * 455 ath_rate_attach(struct ath_softc *sc) 456 { 457 struct amrr_softc *asc; 458 459 asc = kmalloc(sizeof(struct amrr_softc), M_DEVBUF, M_INTWAIT|M_ZERO); 460 if (asc == NULL) 461 return NULL; 462 asc->arc.arc_space = sizeof(struct amrr_node); 463 ath_rate_sysctlattach(sc); 464 465 return &asc->arc; 466 } 467 468 void 469 ath_rate_detach(struct ath_ratectrl *arc) 470 { 471 struct amrr_softc *asc = (struct amrr_softc *) arc; 472 473 kfree(asc, M_DEVBUF); 474 } 475 476 #if defined(__DragonFly__) 477 478 /* 479 * Module glue. 480 */ 481 static int 482 amrr_modevent(module_t mod, int type, void *unused) 483 { 484 int error; 485 486 wlan_serialize_enter(); 487 488 switch (type) { 489 case MOD_LOAD: 490 if (bootverbose) { 491 kprintf("ath_rate: <AMRR rate control " 492 "algorithm> version 0.1\n"); 493 } 494 error = 0; 495 break; 496 case MOD_UNLOAD: 497 error = 0; 498 break; 499 default: 500 error = EINVAL; 501 break; 502 } 503 wlan_serialize_exit(); 504 505 return error; 506 } 507 508 static moduledata_t amrr_mod = { 509 "ath_rate", 510 amrr_modevent, 511 0 512 }; 513 514 DECLARE_MODULE(ath_rate, amrr_mod, SI_SUB_DRIVERS, SI_ORDER_FIRST); 515 MODULE_VERSION(ath_rate, 1); 516 MODULE_DEPEND(ath_rate, ath_hal, 1, 1, 1); 517 MODULE_DEPEND(ath_rate, wlan, 1, 1, 1); 518 519 #endif 520