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