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