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