1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * Copyright 2002-2005, Instant802 Networks, Inc. 4 * Copyright 2005-2006, Devicescape Software, Inc. 5 * Copyright (c) 2006 Jiri Benc <jbenc@suse.cz> 6 * Copyright 2017 Intel Deutschland GmbH 7 * Copyright (C) 2019, 2022-2026 Intel Corporation 8 */ 9 10 #include <linux/kernel.h> 11 #include <linux/rtnetlink.h> 12 #include <linux/module.h> 13 #include <linux/slab.h> 14 #include "rate.h" 15 #include "ieee80211_i.h" 16 #include "debugfs.h" 17 18 struct rate_control_alg { 19 struct list_head list; 20 const struct rate_control_ops *ops; 21 }; 22 23 static LIST_HEAD(rate_ctrl_algs); 24 static DEFINE_MUTEX(rate_ctrl_mutex); 25 26 static char *ieee80211_default_rc_algo = CONFIG_MAC80211_RC_DEFAULT; 27 module_param(ieee80211_default_rc_algo, charp, 0644); 28 MODULE_PARM_DESC(ieee80211_default_rc_algo, 29 "Default rate control algorithm for mac80211 to use"); 30 31 void rate_control_rate_init(struct link_sta_info *link_sta) 32 { 33 struct sta_info *sta = link_sta->sta; 34 struct ieee80211_local *local = sta->sdata->local; 35 struct rate_control_ref *ref = sta->rate_ctrl; 36 struct ieee80211_sta *ista = &sta->sta; 37 void *priv_sta = sta->rate_ctrl_priv; 38 struct ieee80211_supported_band *sband; 39 struct ieee80211_chanctx_conf *chanctx_conf; 40 41 if (!ref) 42 return; 43 44 /* SW rate control isn't supported with MLO right now */ 45 if (WARN_ON(ieee80211_vif_is_mld(&sta->sdata->vif))) 46 return; 47 48 rcu_read_lock(); 49 50 chanctx_conf = rcu_dereference(sta->sdata->vif.bss_conf.chanctx_conf); 51 if (WARN_ON(!chanctx_conf)) { 52 rcu_read_unlock(); 53 return; 54 } 55 56 sband = local->hw.wiphy->bands[chanctx_conf->def.chan->band]; 57 58 /* TODO: check for minstrel_s1g ? */ 59 if (sband->band == NL80211_BAND_S1GHZ) { 60 rcu_read_unlock(); 61 return; 62 } 63 64 spin_lock_bh(&sta->rate_ctrl_lock); 65 ref->ops->rate_init(ref->priv, sband, &chanctx_conf->def, ista, 66 priv_sta); 67 spin_unlock_bh(&sta->rate_ctrl_lock); 68 rcu_read_unlock(); 69 set_sta_flag(sta, WLAN_STA_RATE_CONTROL); 70 } 71 72 void rate_control_rate_init_all_links(struct sta_info *sta) 73 { 74 int link_id; 75 76 for (link_id = 0; link_id < ARRAY_SIZE(sta->link); link_id++) { 77 struct link_sta_info *link_sta; 78 79 link_sta = sdata_dereference(sta->link[link_id], sta->sdata); 80 if (!link_sta) 81 continue; 82 83 rate_control_rate_init(link_sta); 84 } 85 } 86 87 void rate_control_tx_status(struct ieee80211_local *local, 88 struct ieee80211_tx_status *st) 89 { 90 struct rate_control_ref *ref = local->rate_ctrl; 91 struct sta_info *sta = container_of(st->sta, struct sta_info, sta); 92 void *priv_sta = sta->rate_ctrl_priv; 93 struct ieee80211_supported_band *sband; 94 95 if (!ref || !test_sta_flag(sta, WLAN_STA_RATE_CONTROL)) 96 return; 97 98 if (st->info->band >= NUM_NL80211_BANDS) 99 return; 100 101 sband = local->hw.wiphy->bands[st->info->band]; 102 103 spin_lock_bh(&sta->rate_ctrl_lock); 104 if (ref->ops->tx_status_ext) 105 ref->ops->tx_status_ext(ref->priv, sband, priv_sta, st); 106 else if (st->skb) 107 ref->ops->tx_status(ref->priv, sband, st->sta, priv_sta, st->skb); 108 else 109 WARN_ON_ONCE(1); 110 111 spin_unlock_bh(&sta->rate_ctrl_lock); 112 } 113 114 void rate_control_rate_update(struct ieee80211_local *local, 115 struct ieee80211_supported_band *sband, 116 struct link_sta_info *link_sta, 117 u32 changed) 118 { 119 struct rate_control_ref *ref = local->rate_ctrl; 120 struct sta_info *sta = link_sta->sta; 121 struct ieee80211_sta *ista = &sta->sta; 122 void *priv_sta = sta->rate_ctrl_priv; 123 struct ieee80211_chanctx_conf *chanctx_conf; 124 125 if (ref && ref->ops->rate_update) { 126 rcu_read_lock(); 127 128 chanctx_conf = rcu_dereference(sta->sdata->vif.bss_conf.chanctx_conf); 129 if (WARN_ON(!chanctx_conf)) { 130 rcu_read_unlock(); 131 return; 132 } 133 134 spin_lock_bh(&sta->rate_ctrl_lock); 135 ref->ops->rate_update(ref->priv, sband, &chanctx_conf->def, 136 ista, priv_sta, changed); 137 spin_unlock_bh(&sta->rate_ctrl_lock); 138 rcu_read_unlock(); 139 } 140 141 if (sta->uploaded) 142 drv_link_sta_rc_update(local, sta->sdata, link_sta->pub, 143 changed); 144 } 145 146 int ieee80211_rate_control_register(const struct rate_control_ops *ops) 147 { 148 struct rate_control_alg *alg; 149 150 if (!ops->name) 151 return -EINVAL; 152 153 mutex_lock(&rate_ctrl_mutex); 154 list_for_each_entry(alg, &rate_ctrl_algs, list) { 155 if (!strcmp(alg->ops->name, ops->name)) { 156 /* don't register an algorithm twice */ 157 WARN_ON(1); 158 mutex_unlock(&rate_ctrl_mutex); 159 return -EALREADY; 160 } 161 } 162 163 alg = kzalloc_obj(*alg); 164 if (alg == NULL) { 165 mutex_unlock(&rate_ctrl_mutex); 166 return -ENOMEM; 167 } 168 alg->ops = ops; 169 170 list_add_tail(&alg->list, &rate_ctrl_algs); 171 mutex_unlock(&rate_ctrl_mutex); 172 173 return 0; 174 } 175 EXPORT_SYMBOL(ieee80211_rate_control_register); 176 177 void ieee80211_rate_control_unregister(const struct rate_control_ops *ops) 178 { 179 struct rate_control_alg *alg; 180 181 mutex_lock(&rate_ctrl_mutex); 182 list_for_each_entry(alg, &rate_ctrl_algs, list) { 183 if (alg->ops == ops) { 184 list_del(&alg->list); 185 kfree(alg); 186 break; 187 } 188 } 189 mutex_unlock(&rate_ctrl_mutex); 190 } 191 EXPORT_SYMBOL(ieee80211_rate_control_unregister); 192 193 static const struct rate_control_ops * 194 ieee80211_try_rate_control_ops_get(const char *name) 195 { 196 struct rate_control_alg *alg; 197 const struct rate_control_ops *ops = NULL; 198 199 if (!name) 200 return NULL; 201 202 mutex_lock(&rate_ctrl_mutex); 203 list_for_each_entry(alg, &rate_ctrl_algs, list) { 204 if (!strcmp(alg->ops->name, name)) { 205 ops = alg->ops; 206 break; 207 } 208 } 209 mutex_unlock(&rate_ctrl_mutex); 210 return ops; 211 } 212 213 /* Get the rate control algorithm. */ 214 static const struct rate_control_ops * 215 ieee80211_rate_control_ops_get(const char *name) 216 { 217 const struct rate_control_ops *ops; 218 const char *alg_name; 219 220 kernel_param_lock(THIS_MODULE); 221 if (!name) 222 alg_name = ieee80211_default_rc_algo; 223 else 224 alg_name = name; 225 226 ops = ieee80211_try_rate_control_ops_get(alg_name); 227 if (!ops && name) 228 /* try default if specific alg requested but not found */ 229 ops = ieee80211_try_rate_control_ops_get(ieee80211_default_rc_algo); 230 231 /* Note: check for > 0 is intentional to avoid clang warning */ 232 if (!ops && (strlen(CONFIG_MAC80211_RC_DEFAULT) > 0)) 233 /* try built-in one if specific alg requested but not found */ 234 ops = ieee80211_try_rate_control_ops_get(CONFIG_MAC80211_RC_DEFAULT); 235 236 kernel_param_unlock(THIS_MODULE); 237 238 return ops; 239 } 240 241 #ifdef CONFIG_MAC80211_DEBUGFS 242 static ssize_t rcname_read(struct file *file, char __user *userbuf, 243 size_t count, loff_t *ppos) 244 { 245 struct rate_control_ref *ref = file->private_data; 246 int len = strlen(ref->ops->name); 247 248 return simple_read_from_buffer(userbuf, count, ppos, 249 ref->ops->name, len); 250 } 251 252 const struct debugfs_short_fops rcname_ops = { 253 .read = rcname_read, 254 .llseek = default_llseek, 255 }; 256 #endif 257 258 static struct rate_control_ref * 259 rate_control_alloc(const char *name, struct ieee80211_local *local) 260 { 261 struct rate_control_ref *ref; 262 263 ref = kmalloc_obj(struct rate_control_ref); 264 if (!ref) 265 return NULL; 266 ref->ops = ieee80211_rate_control_ops_get(name); 267 if (!ref->ops) 268 goto free; 269 270 ref->priv = ref->ops->alloc(&local->hw); 271 if (!ref->priv) 272 goto free; 273 return ref; 274 275 free: 276 kfree(ref); 277 return NULL; 278 } 279 280 static void rate_control_free(struct ieee80211_local *local, 281 struct rate_control_ref *ctrl_ref) 282 { 283 ctrl_ref->ops->free(ctrl_ref->priv); 284 285 #ifdef CONFIG_MAC80211_DEBUGFS 286 debugfs_remove_recursive(local->debugfs.rcdir); 287 local->debugfs.rcdir = NULL; 288 #endif 289 290 kfree(ctrl_ref); 291 } 292 293 void ieee80211_check_rate_mask(struct ieee80211_link_data *link) 294 { 295 struct ieee80211_sub_if_data *sdata = link->sdata; 296 struct ieee80211_local *local = sdata->local; 297 struct ieee80211_supported_band *sband; 298 u32 user_mask, basic_rates = link->conf->basic_rates; 299 enum nl80211_band band; 300 301 if (WARN_ON(!link->conf->chanreq.oper.chan)) 302 return; 303 304 band = link->conf->chanreq.oper.chan->band; 305 if (band == NL80211_BAND_S1GHZ) { 306 /* TODO */ 307 return; 308 } 309 310 if (WARN_ON_ONCE(!basic_rates)) 311 return; 312 313 user_mask = sdata->rc_rateidx_mask[band]; 314 sband = local->hw.wiphy->bands[band]; 315 316 if (user_mask & basic_rates) 317 return; 318 319 sdata_dbg(sdata, 320 "no overlap between basic rates (0x%x) and user mask (0x%x on band %d) - clearing the latter", 321 basic_rates, user_mask, band); 322 sdata->rc_rateidx_mask[band] = (1 << sband->n_bitrates) - 1; 323 } 324 325 static bool rc_no_data_or_no_ack_use_min(struct ieee80211_tx_rate_control *txrc) 326 { 327 struct sk_buff *skb = txrc->skb; 328 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb); 329 330 return (info->flags & (IEEE80211_TX_CTL_NO_ACK | 331 IEEE80211_TX_CTL_USE_MINRATE)) || 332 !ieee80211_is_tx_data(skb); 333 } 334 335 static void rc_send_low_basicrate(struct ieee80211_tx_rate *rate, 336 u32 basic_rates, 337 struct ieee80211_supported_band *sband) 338 { 339 u8 i; 340 341 if (sband->band == NL80211_BAND_S1GHZ) { 342 /* TODO */ 343 rate->flags |= IEEE80211_TX_RC_S1G_MCS; 344 rate->idx = 0; 345 return; 346 } 347 348 if (basic_rates == 0) 349 return; /* assume basic rates unknown and accept rate */ 350 if (rate->idx < 0) 351 return; 352 if (basic_rates & (1 << rate->idx)) 353 return; /* selected rate is a basic rate */ 354 355 for (i = rate->idx + 1; i <= sband->n_bitrates; i++) { 356 if (basic_rates & (1 << i)) { 357 rate->idx = i; 358 return; 359 } 360 } 361 362 /* could not find a basic rate; use original selection */ 363 } 364 365 static void __rate_control_send_low(struct ieee80211_hw *hw, 366 struct ieee80211_supported_band *sband, 367 struct ieee80211_sta *sta, 368 struct ieee80211_tx_info *info, 369 u32 rate_mask) 370 { 371 u32 rate_flags = 0; 372 int i; 373 374 /* 375 * Frames that shouldn't use the rate mask could be anything, 376 * even on a different band, so don't take the sta into account 377 * to avoid ending up without rates. 378 */ 379 if (info->control.flags & IEEE80211_TX_CTRL_DONT_USE_RATE_MASK) 380 sta = NULL; 381 382 if (sband->band == NL80211_BAND_S1GHZ) { 383 info->control.rates[0].flags |= IEEE80211_TX_RC_S1G_MCS; 384 info->control.rates[0].idx = 0; 385 return; 386 } 387 388 if ((sband->band == NL80211_BAND_2GHZ) && 389 (info->flags & IEEE80211_TX_CTL_NO_CCK_RATE)) 390 rate_flags |= IEEE80211_RATE_ERP_G; 391 392 info->control.rates[0].idx = 0; 393 for (i = 0; i < sband->n_bitrates; i++) { 394 if (!(rate_mask & BIT(i))) 395 continue; 396 397 if ((rate_flags & sband->bitrates[i].flags) != rate_flags) 398 continue; 399 400 if (!rate_supported(sta, sband->band, i)) 401 continue; 402 403 info->control.rates[0].idx = i; 404 break; 405 } 406 WARN_ONCE(i == sband->n_bitrates, 407 "no supported rates for sta %pM (0x%x, band %d) in rate_mask 0x%x with flags 0x%x\n", 408 sta ? sta->addr : NULL, 409 sta ? sta->deflink.supp_rates[sband->band] : -1, 410 sband->band, 411 rate_mask, rate_flags); 412 413 info->control.rates[0].count = 414 (info->flags & IEEE80211_TX_CTL_NO_ACK) ? 415 1 : hw->max_rate_tries; 416 417 info->control.skip_table = 1; 418 } 419 420 421 static bool rate_control_send_low(struct ieee80211_sta *pubsta, 422 struct ieee80211_tx_rate_control *txrc) 423 { 424 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(txrc->skb); 425 struct ieee80211_supported_band *sband = txrc->sband; 426 struct sta_info *sta; 427 int mcast_rate; 428 bool use_basicrate = false; 429 430 if (!sband) 431 return false; 432 433 if (!pubsta || rc_no_data_or_no_ack_use_min(txrc)) { 434 __rate_control_send_low(txrc->hw, sband, pubsta, info, 435 txrc->rate_idx_mask); 436 437 if (!pubsta && txrc->bss) { 438 mcast_rate = txrc->bss_conf->mcast_rate[sband->band]; 439 if (mcast_rate > 0) { 440 info->control.rates[0].idx = mcast_rate - 1; 441 return true; 442 } 443 use_basicrate = true; 444 } else if (pubsta) { 445 sta = container_of(pubsta, struct sta_info, sta); 446 if (ieee80211_vif_is_mesh(&sta->sdata->vif)) 447 use_basicrate = true; 448 } 449 450 if (use_basicrate) 451 rc_send_low_basicrate(&info->control.rates[0], 452 txrc->bss_conf->basic_rates, 453 sband); 454 455 return true; 456 } 457 return false; 458 } 459 460 static bool rate_idx_match_legacy_mask(s8 *rate_idx, int n_bitrates, u32 mask) 461 { 462 int j; 463 464 /* See whether the selected rate or anything below it is allowed. */ 465 for (j = *rate_idx; j >= 0; j--) { 466 if (mask & (1 << j)) { 467 /* Okay, found a suitable rate. Use it. */ 468 *rate_idx = j; 469 return true; 470 } 471 } 472 473 /* Try to find a higher rate that would be allowed */ 474 for (j = *rate_idx + 1; j < n_bitrates; j++) { 475 if (mask & (1 << j)) { 476 /* Okay, found a suitable rate. Use it. */ 477 *rate_idx = j; 478 return true; 479 } 480 } 481 return false; 482 } 483 484 static bool rate_idx_match_mcs_mask(s8 *rate_idx, u8 *mcs_mask) 485 { 486 int i, j; 487 int ridx, rbit; 488 489 ridx = *rate_idx / 8; 490 rbit = *rate_idx % 8; 491 492 /* sanity check */ 493 if (ridx < 0 || ridx >= IEEE80211_HT_MCS_MASK_LEN) 494 return false; 495 496 /* See whether the selected rate or anything below it is allowed. */ 497 for (i = ridx; i >= 0; i--) { 498 for (j = rbit; j >= 0; j--) 499 if (mcs_mask[i] & BIT(j)) { 500 *rate_idx = i * 8 + j; 501 return true; 502 } 503 rbit = 7; 504 } 505 506 /* Try to find a higher rate that would be allowed */ 507 ridx = (*rate_idx + 1) / 8; 508 rbit = (*rate_idx + 1) % 8; 509 510 for (i = ridx; i < IEEE80211_HT_MCS_MASK_LEN; i++) { 511 for (j = rbit; j < 8; j++) 512 if (mcs_mask[i] & BIT(j)) { 513 *rate_idx = i * 8 + j; 514 return true; 515 } 516 rbit = 0; 517 } 518 return false; 519 } 520 521 static bool rate_idx_match_vht_mcs_mask(s8 *rate_idx, u16 *vht_mask) 522 { 523 int i, j; 524 int ridx, rbit; 525 526 ridx = *rate_idx >> 4; 527 rbit = *rate_idx & 0xf; 528 529 if (ridx < 0 || ridx >= NL80211_VHT_NSS_MAX) 530 return false; 531 532 /* See whether the selected rate or anything below it is allowed. */ 533 for (i = ridx; i >= 0; i--) { 534 for (j = rbit; j >= 0; j--) { 535 if (vht_mask[i] & BIT(j)) { 536 *rate_idx = (i << 4) | j; 537 return true; 538 } 539 } 540 rbit = 15; 541 } 542 543 /* Try to find a higher rate that would be allowed */ 544 ridx = (*rate_idx + 1) >> 4; 545 rbit = (*rate_idx + 1) & 0xf; 546 547 for (i = ridx; i < NL80211_VHT_NSS_MAX; i++) { 548 for (j = rbit; j < 16; j++) { 549 if (vht_mask[i] & BIT(j)) { 550 *rate_idx = (i << 4) | j; 551 return true; 552 } 553 } 554 rbit = 0; 555 } 556 return false; 557 } 558 559 static void rate_idx_match_mask(s8 *rate_idx, u16 *rate_flags, 560 struct ieee80211_supported_band *sband, 561 enum nl80211_chan_width chan_width, 562 u32 mask, 563 u8 mcs_mask[IEEE80211_HT_MCS_MASK_LEN], 564 u16 vht_mask[NL80211_VHT_NSS_MAX]) 565 { 566 if (*rate_flags & IEEE80211_TX_RC_VHT_MCS) { 567 /* handle VHT rates */ 568 if (rate_idx_match_vht_mcs_mask(rate_idx, vht_mask)) 569 return; 570 571 *rate_idx = 0; 572 /* keep protection flags */ 573 *rate_flags &= (IEEE80211_TX_RC_USE_RTS_CTS | 574 IEEE80211_TX_RC_USE_CTS_PROTECT | 575 IEEE80211_TX_RC_USE_SHORT_PREAMBLE); 576 577 *rate_flags |= IEEE80211_TX_RC_MCS; 578 if (chan_width == NL80211_CHAN_WIDTH_40) 579 *rate_flags |= IEEE80211_TX_RC_40_MHZ_WIDTH; 580 581 if (rate_idx_match_mcs_mask(rate_idx, mcs_mask)) 582 return; 583 584 /* also try the legacy rates. */ 585 *rate_flags &= ~(IEEE80211_TX_RC_MCS | 586 IEEE80211_TX_RC_40_MHZ_WIDTH); 587 if (rate_idx_match_legacy_mask(rate_idx, sband->n_bitrates, 588 mask)) 589 return; 590 } else if (*rate_flags & IEEE80211_TX_RC_MCS) { 591 /* handle HT rates */ 592 if (rate_idx_match_mcs_mask(rate_idx, mcs_mask)) 593 return; 594 595 /* also try the legacy rates. */ 596 *rate_idx = 0; 597 /* keep protection flags */ 598 *rate_flags &= (IEEE80211_TX_RC_USE_RTS_CTS | 599 IEEE80211_TX_RC_USE_CTS_PROTECT | 600 IEEE80211_TX_RC_USE_SHORT_PREAMBLE); 601 if (rate_idx_match_legacy_mask(rate_idx, sband->n_bitrates, 602 mask)) 603 return; 604 } else { 605 /* handle legacy rates */ 606 if (rate_idx_match_legacy_mask(rate_idx, sband->n_bitrates, 607 mask)) 608 return; 609 610 /* if HT BSS, and we handle a data frame, also try HT rates */ 611 if (chan_width == NL80211_CHAN_WIDTH_20_NOHT) 612 return; 613 614 *rate_idx = 0; 615 /* keep protection flags */ 616 *rate_flags &= (IEEE80211_TX_RC_USE_RTS_CTS | 617 IEEE80211_TX_RC_USE_CTS_PROTECT | 618 IEEE80211_TX_RC_USE_SHORT_PREAMBLE); 619 620 *rate_flags |= IEEE80211_TX_RC_MCS; 621 622 if (chan_width == NL80211_CHAN_WIDTH_40) 623 *rate_flags |= IEEE80211_TX_RC_40_MHZ_WIDTH; 624 625 if (rate_idx_match_mcs_mask(rate_idx, mcs_mask)) 626 return; 627 } 628 629 /* 630 * Uh.. No suitable rate exists. This should not really happen with 631 * sane TX rate mask configurations. However, should someone manage to 632 * configure supported rates and TX rate mask in incompatible way, 633 * allow the frame to be transmitted with whatever the rate control 634 * selected. 635 */ 636 } 637 638 static void rate_fixup_ratelist(struct ieee80211_vif *vif, 639 struct ieee80211_supported_band *sband, 640 struct ieee80211_tx_info *info, 641 struct ieee80211_tx_rate *rates, 642 int max_rates) 643 { 644 struct ieee80211_rate *rate; 645 bool inval = false; 646 int i; 647 648 /* 649 * Set up the RTS/CTS rate as the fastest basic rate 650 * that is not faster than the data rate unless there 651 * is no basic rate slower than the data rate, in which 652 * case we pick the slowest basic rate 653 * 654 * XXX: Should this check all retry rates? 655 */ 656 if (!(rates[0].flags & 657 (IEEE80211_TX_RC_MCS | IEEE80211_TX_RC_VHT_MCS))) { 658 u32 basic_rates = vif->bss_conf.basic_rates; 659 s8 baserate = basic_rates ? ffs(basic_rates) - 1 : 0; 660 661 rate = &sband->bitrates[rates[0].idx]; 662 663 for (i = 0; i < sband->n_bitrates; i++) { 664 /* must be a basic rate */ 665 if (!(basic_rates & BIT(i))) 666 continue; 667 /* must not be faster than the data rate */ 668 if (sband->bitrates[i].bitrate > rate->bitrate) 669 continue; 670 /* maximum */ 671 if (sband->bitrates[baserate].bitrate < 672 sband->bitrates[i].bitrate) 673 baserate = i; 674 } 675 676 info->control.rts_cts_rate_idx = baserate; 677 } 678 679 for (i = 0; i < max_rates; i++) { 680 /* 681 * make sure there's no valid rate following 682 * an invalid one, just in case drivers don't 683 * take the API seriously to stop at -1. 684 */ 685 if (inval) { 686 rates[i].idx = -1; 687 continue; 688 } 689 if (rates[i].idx < 0) { 690 inval = true; 691 continue; 692 } 693 694 /* 695 * For now assume MCS is already set up correctly, this 696 * needs to be fixed. 697 */ 698 if (rates[i].flags & IEEE80211_TX_RC_MCS) { 699 WARN_ON(rates[i].idx > 76); 700 701 if (!(rates[i].flags & IEEE80211_TX_RC_USE_RTS_CTS) && 702 info->control.use_cts_prot) 703 rates[i].flags |= 704 IEEE80211_TX_RC_USE_CTS_PROTECT; 705 continue; 706 } 707 708 if (rates[i].flags & IEEE80211_TX_RC_VHT_MCS) { 709 WARN_ON(ieee80211_rate_get_vht_mcs(&rates[i]) > 9); 710 continue; 711 } 712 713 /* set up RTS protection if desired */ 714 if (info->control.use_rts) { 715 rates[i].flags |= IEEE80211_TX_RC_USE_RTS_CTS; 716 info->control.use_cts_prot = false; 717 } 718 719 /* RC is busted */ 720 if (WARN_ON_ONCE(rates[i].idx >= sband->n_bitrates)) { 721 rates[i].idx = -1; 722 continue; 723 } 724 725 rate = &sband->bitrates[rates[i].idx]; 726 727 /* set up short preamble */ 728 if (info->control.short_preamble && 729 rate->flags & IEEE80211_RATE_SHORT_PREAMBLE) 730 rates[i].flags |= IEEE80211_TX_RC_USE_SHORT_PREAMBLE; 731 732 /* set up G protection */ 733 if (!(rates[i].flags & IEEE80211_TX_RC_USE_RTS_CTS) && 734 info->control.use_cts_prot && 735 rate->flags & IEEE80211_RATE_ERP_G) 736 rates[i].flags |= IEEE80211_TX_RC_USE_CTS_PROTECT; 737 } 738 } 739 740 741 static void rate_control_fill_sta_table(struct ieee80211_sta *sta, 742 struct ieee80211_tx_info *info, 743 struct ieee80211_tx_rate *rates, 744 int max_rates) 745 { 746 struct ieee80211_sta_rates *ratetbl = NULL; 747 int i; 748 749 if (sta && !info->control.skip_table) 750 ratetbl = rcu_dereference(sta->rates); 751 752 /* Fill remaining rate slots with data from the sta rate table. */ 753 max_rates = min_t(int, max_rates, IEEE80211_TX_RATE_TABLE_SIZE); 754 for (i = 0; i < max_rates; i++) { 755 if (i < ARRAY_SIZE(info->control.rates) && 756 info->control.rates[i].idx >= 0 && 757 info->control.rates[i].count) { 758 if (rates != info->control.rates) 759 rates[i] = info->control.rates[i]; 760 } else if (ratetbl) { 761 rates[i].idx = ratetbl->rate[i].idx; 762 rates[i].flags = ratetbl->rate[i].flags; 763 if (info->control.use_rts) 764 rates[i].count = ratetbl->rate[i].count_rts; 765 else if (info->control.use_cts_prot) 766 rates[i].count = ratetbl->rate[i].count_cts; 767 else 768 rates[i].count = ratetbl->rate[i].count; 769 } else { 770 rates[i].idx = -1; 771 rates[i].count = 0; 772 } 773 774 if (rates[i].idx < 0 || !rates[i].count) 775 break; 776 } 777 } 778 779 static bool rate_control_cap_mask(struct ieee80211_sub_if_data *sdata, 780 struct ieee80211_supported_band *sband, 781 struct ieee80211_sta *sta, u32 *mask, 782 u8 mcs_mask[IEEE80211_HT_MCS_MASK_LEN], 783 u16 vht_mask[NL80211_VHT_NSS_MAX]) 784 { 785 u32 i; 786 787 *mask = sdata->rc_rateidx_mask[sband->band]; 788 789 if (*mask == (1 << sband->n_bitrates) - 1 && 790 !sdata->rc_has_mcs_mask[sband->band] && 791 !sdata->rc_has_vht_mcs_mask[sband->band]) 792 return false; 793 794 if (sdata->rc_has_mcs_mask[sband->band]) 795 memcpy(mcs_mask, sdata->rc_rateidx_mcs_mask[sband->band], 796 IEEE80211_HT_MCS_MASK_LEN); 797 else 798 memset(mcs_mask, 0xff, IEEE80211_HT_MCS_MASK_LEN); 799 800 if (sdata->rc_has_vht_mcs_mask[sband->band]) 801 memcpy(vht_mask, sdata->rc_rateidx_vht_mcs_mask[sband->band], 802 sizeof(u16) * NL80211_VHT_NSS_MAX); 803 else 804 memset(vht_mask, 0xff, sizeof(u16) * NL80211_VHT_NSS_MAX); 805 806 if (sta) { 807 __le16 sta_vht_cap; 808 u16 sta_vht_mask[NL80211_VHT_NSS_MAX]; 809 810 /* Filter out rates that the STA does not support */ 811 *mask &= sta->deflink.supp_rates[sband->band]; 812 for (i = 0; i < IEEE80211_HT_MCS_MASK_LEN; i++) 813 mcs_mask[i] &= sta->deflink.ht_cap.mcs.rx_mask[i]; 814 815 sta_vht_cap = sta->deflink.vht_cap.vht_mcs.rx_mcs_map; 816 ieee80211_get_vht_mask_from_cap(sta_vht_cap, sta_vht_mask); 817 for (i = 0; i < NL80211_VHT_NSS_MAX; i++) 818 vht_mask[i] &= sta_vht_mask[i]; 819 } 820 821 return true; 822 } 823 824 static void 825 rate_control_apply_mask_ratetbl(struct sta_info *sta, 826 struct ieee80211_supported_band *sband, 827 struct ieee80211_sta_rates *rates) 828 { 829 int i; 830 u32 mask; 831 u8 mcs_mask[IEEE80211_HT_MCS_MASK_LEN]; 832 u16 vht_mask[NL80211_VHT_NSS_MAX]; 833 enum nl80211_chan_width chan_width; 834 835 if (!rate_control_cap_mask(sta->sdata, sband, &sta->sta, &mask, 836 mcs_mask, vht_mask)) 837 return; 838 839 chan_width = sta->sdata->vif.bss_conf.chanreq.oper.width; 840 for (i = 0; i < IEEE80211_TX_RATE_TABLE_SIZE; i++) { 841 if (rates->rate[i].idx < 0) 842 break; 843 844 rate_idx_match_mask(&rates->rate[i].idx, &rates->rate[i].flags, 845 sband, chan_width, mask, mcs_mask, 846 vht_mask); 847 } 848 } 849 850 static void rate_control_apply_mask(struct ieee80211_sub_if_data *sdata, 851 struct ieee80211_sta *sta, 852 struct ieee80211_supported_band *sband, 853 struct ieee80211_tx_rate *rates, 854 int max_rates) 855 { 856 enum nl80211_chan_width chan_width; 857 u8 mcs_mask[IEEE80211_HT_MCS_MASK_LEN]; 858 u32 mask; 859 u16 rate_flags, vht_mask[NL80211_VHT_NSS_MAX]; 860 int i; 861 862 /* 863 * Try to enforce the rateidx mask the user wanted. skip this if the 864 * default mask (allow all rates) is used to save some processing for 865 * the common case. 866 */ 867 if (!rate_control_cap_mask(sdata, sband, sta, &mask, mcs_mask, 868 vht_mask)) 869 return; 870 871 /* 872 * Make sure the rate index selected for each TX rate is 873 * included in the configured mask and change the rate indexes 874 * if needed. 875 */ 876 chan_width = sdata->vif.bss_conf.chanreq.oper.width; 877 for (i = 0; i < max_rates; i++) { 878 /* Skip invalid rates */ 879 if (rates[i].idx < 0) 880 break; 881 882 rate_flags = rates[i].flags; 883 rate_idx_match_mask(&rates[i].idx, &rate_flags, sband, 884 chan_width, mask, mcs_mask, vht_mask); 885 rates[i].flags = rate_flags; 886 } 887 } 888 889 void ieee80211_get_tx_rates(struct ieee80211_vif *vif, 890 struct ieee80211_sta *sta, 891 struct sk_buff *skb, 892 struct ieee80211_tx_rate *dest, 893 int max_rates) 894 { 895 struct ieee80211_sub_if_data *sdata; 896 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb); 897 struct ieee80211_supported_band *sband; 898 u32 mask = ~0; 899 900 rate_control_fill_sta_table(sta, info, dest, max_rates); 901 902 if (!vif) 903 return; 904 905 sdata = vif_to_sdata(vif); 906 if (info->band >= NUM_NL80211_BANDS) 907 return; 908 909 sband = sdata->local->hw.wiphy->bands[info->band]; 910 911 if (ieee80211_is_tx_data(skb)) 912 rate_control_apply_mask(sdata, sta, sband, dest, max_rates); 913 914 if (!(info->control.flags & IEEE80211_TX_CTRL_DONT_USE_RATE_MASK)) 915 mask = sdata->rc_rateidx_mask[info->band]; 916 917 if (dest[0].idx < 0) 918 __rate_control_send_low(&sdata->local->hw, sband, sta, info, 919 mask); 920 921 if (sta) 922 rate_fixup_ratelist(vif, sband, info, dest, max_rates); 923 } 924 EXPORT_SYMBOL(ieee80211_get_tx_rates); 925 926 void rate_control_get_rate(struct ieee80211_sub_if_data *sdata, 927 struct sta_info *sta, 928 struct ieee80211_tx_rate_control *txrc) 929 { 930 struct rate_control_ref *ref = sdata->local->rate_ctrl; 931 void *priv_sta = NULL; 932 struct ieee80211_sta *ista = NULL; 933 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(txrc->skb); 934 int i; 935 936 for (i = 0; i < IEEE80211_TX_MAX_RATES; i++) { 937 info->control.rates[i].idx = -1; 938 info->control.rates[i].flags = 0; 939 info->control.rates[i].count = 0; 940 } 941 942 if (rate_control_send_low(sta ? &sta->sta : NULL, txrc)) 943 return; 944 945 if (ieee80211_hw_check(&sdata->local->hw, HAS_RATE_CONTROL)) 946 return; 947 948 if (sta && test_sta_flag(sta, WLAN_STA_RATE_CONTROL)) { 949 ista = &sta->sta; 950 priv_sta = sta->rate_ctrl_priv; 951 } 952 953 if (ista) { 954 spin_lock_bh(&sta->rate_ctrl_lock); 955 ref->ops->get_rate(ref->priv, ista, priv_sta, txrc); 956 spin_unlock_bh(&sta->rate_ctrl_lock); 957 } else { 958 rate_control_send_low(NULL, txrc); 959 } 960 961 if (ieee80211_hw_check(&sdata->local->hw, SUPPORTS_RC_TABLE)) 962 return; 963 964 ieee80211_get_tx_rates(&sdata->vif, ista, txrc->skb, 965 info->control.rates, 966 ARRAY_SIZE(info->control.rates)); 967 } 968 969 int rate_control_set_rates(struct ieee80211_hw *hw, 970 struct ieee80211_sta *pubsta, 971 struct ieee80211_sta_rates *rates) 972 { 973 struct sta_info *sta = container_of(pubsta, struct sta_info, sta); 974 struct ieee80211_sta_rates *old; 975 struct ieee80211_supported_band *sband; 976 977 sband = ieee80211_get_sband(sta->sdata); 978 if (!sband) 979 return -EINVAL; 980 rate_control_apply_mask_ratetbl(sta, sband, rates); 981 /* 982 * mac80211 guarantees that this function will not be called 983 * concurrently, so the following RCU access is safe, even without 984 * extra locking. This can not be checked easily, so we just set 985 * the condition to true. 986 */ 987 old = rcu_dereference_protected(pubsta->rates, true); 988 rcu_assign_pointer(pubsta->rates, rates); 989 if (old) 990 kfree_rcu(old, rcu_head); 991 992 if (sta->uploaded) 993 drv_sta_rate_tbl_update(hw_to_local(hw), sta->sdata, pubsta); 994 995 return 0; 996 } 997 EXPORT_SYMBOL(rate_control_set_rates); 998 999 int ieee80211_init_rate_ctrl_alg(struct ieee80211_local *local, 1000 const char *name) 1001 { 1002 struct rate_control_ref *ref; 1003 1004 if (ieee80211_hw_check(&local->hw, HAS_RATE_CONTROL)) { 1005 if (WARN_ON(!local->ops->set_rts_threshold)) 1006 return -EINVAL; 1007 return 0; 1008 } 1009 1010 ref = rate_control_alloc(name, local); 1011 if (!ref) { 1012 wiphy_warn(local->hw.wiphy, 1013 "Failed to select rate control algorithm\n"); 1014 return -ENOENT; 1015 } 1016 1017 WARN_ON(local->rate_ctrl); 1018 local->rate_ctrl = ref; 1019 1020 wiphy_debug(local->hw.wiphy, "Selected rate control algorithm '%s'\n", 1021 ref->ops->name); 1022 1023 return 0; 1024 } 1025 1026 void rate_control_deinitialize(struct ieee80211_local *local) 1027 { 1028 struct rate_control_ref *ref; 1029 1030 ref = local->rate_ctrl; 1031 1032 if (!ref) 1033 return; 1034 1035 local->rate_ctrl = NULL; 1036 rate_control_free(local, ref); 1037 } 1038