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 if (sband->band == NL80211_BAND_S1GHZ) { 376 info->control.rates[0].flags |= IEEE80211_TX_RC_S1G_MCS; 377 info->control.rates[0].idx = 0; 378 return; 379 } 380 381 if ((sband->band == NL80211_BAND_2GHZ) && 382 (info->flags & IEEE80211_TX_CTL_NO_CCK_RATE)) 383 rate_flags |= IEEE80211_RATE_ERP_G; 384 385 info->control.rates[0].idx = 0; 386 for (i = 0; i < sband->n_bitrates; i++) { 387 if (!(rate_mask & BIT(i))) 388 continue; 389 390 if ((rate_flags & sband->bitrates[i].flags) != rate_flags) 391 continue; 392 393 if (!rate_supported(sta, sband->band, i)) 394 continue; 395 396 info->control.rates[0].idx = i; 397 break; 398 } 399 WARN_ONCE(i == sband->n_bitrates, 400 "no supported rates for sta %pM (0x%x, band %d) in rate_mask 0x%x with flags 0x%x\n", 401 sta ? sta->addr : NULL, 402 sta ? sta->deflink.supp_rates[sband->band] : -1, 403 sband->band, 404 rate_mask, rate_flags); 405 406 info->control.rates[0].count = 407 (info->flags & IEEE80211_TX_CTL_NO_ACK) ? 408 1 : hw->max_rate_tries; 409 410 info->control.skip_table = 1; 411 } 412 413 414 static bool rate_control_send_low(struct ieee80211_sta *pubsta, 415 struct ieee80211_tx_rate_control *txrc) 416 { 417 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(txrc->skb); 418 struct ieee80211_supported_band *sband = txrc->sband; 419 struct sta_info *sta; 420 int mcast_rate; 421 bool use_basicrate = false; 422 423 if (!sband) 424 return false; 425 426 if (!pubsta || rc_no_data_or_no_ack_use_min(txrc)) { 427 __rate_control_send_low(txrc->hw, sband, pubsta, info, 428 txrc->rate_idx_mask); 429 430 if (!pubsta && txrc->bss) { 431 mcast_rate = txrc->bss_conf->mcast_rate[sband->band]; 432 if (mcast_rate > 0) { 433 info->control.rates[0].idx = mcast_rate - 1; 434 return true; 435 } 436 use_basicrate = true; 437 } else if (pubsta) { 438 sta = container_of(pubsta, struct sta_info, sta); 439 if (ieee80211_vif_is_mesh(&sta->sdata->vif)) 440 use_basicrate = true; 441 } 442 443 if (use_basicrate) 444 rc_send_low_basicrate(&info->control.rates[0], 445 txrc->bss_conf->basic_rates, 446 sband); 447 448 return true; 449 } 450 return false; 451 } 452 453 static bool rate_idx_match_legacy_mask(s8 *rate_idx, int n_bitrates, u32 mask) 454 { 455 int j; 456 457 /* See whether the selected rate or anything below it is allowed. */ 458 for (j = *rate_idx; j >= 0; j--) { 459 if (mask & (1 << j)) { 460 /* Okay, found a suitable rate. Use it. */ 461 *rate_idx = j; 462 return true; 463 } 464 } 465 466 /* Try to find a higher rate that would be allowed */ 467 for (j = *rate_idx + 1; j < n_bitrates; j++) { 468 if (mask & (1 << j)) { 469 /* Okay, found a suitable rate. Use it. */ 470 *rate_idx = j; 471 return true; 472 } 473 } 474 return false; 475 } 476 477 static bool rate_idx_match_mcs_mask(s8 *rate_idx, u8 *mcs_mask) 478 { 479 int i, j; 480 int ridx, rbit; 481 482 ridx = *rate_idx / 8; 483 rbit = *rate_idx % 8; 484 485 /* sanity check */ 486 if (ridx < 0 || ridx >= IEEE80211_HT_MCS_MASK_LEN) 487 return false; 488 489 /* See whether the selected rate or anything below it is allowed. */ 490 for (i = ridx; i >= 0; i--) { 491 for (j = rbit; j >= 0; j--) 492 if (mcs_mask[i] & BIT(j)) { 493 *rate_idx = i * 8 + j; 494 return true; 495 } 496 rbit = 7; 497 } 498 499 /* Try to find a higher rate that would be allowed */ 500 ridx = (*rate_idx + 1) / 8; 501 rbit = (*rate_idx + 1) % 8; 502 503 for (i = ridx; i < IEEE80211_HT_MCS_MASK_LEN; i++) { 504 for (j = rbit; j < 8; j++) 505 if (mcs_mask[i] & BIT(j)) { 506 *rate_idx = i * 8 + j; 507 return true; 508 } 509 rbit = 0; 510 } 511 return false; 512 } 513 514 static bool rate_idx_match_vht_mcs_mask(s8 *rate_idx, u16 *vht_mask) 515 { 516 int i, j; 517 int ridx, rbit; 518 519 ridx = *rate_idx >> 4; 520 rbit = *rate_idx & 0xf; 521 522 if (ridx < 0 || ridx >= NL80211_VHT_NSS_MAX) 523 return false; 524 525 /* See whether the selected rate or anything below it is allowed. */ 526 for (i = ridx; i >= 0; i--) { 527 for (j = rbit; j >= 0; j--) { 528 if (vht_mask[i] & BIT(j)) { 529 *rate_idx = (i << 4) | j; 530 return true; 531 } 532 } 533 rbit = 15; 534 } 535 536 /* Try to find a higher rate that would be allowed */ 537 ridx = (*rate_idx + 1) >> 4; 538 rbit = (*rate_idx + 1) & 0xf; 539 540 for (i = ridx; i < NL80211_VHT_NSS_MAX; i++) { 541 for (j = rbit; j < 16; j++) { 542 if (vht_mask[i] & BIT(j)) { 543 *rate_idx = (i << 4) | j; 544 return true; 545 } 546 } 547 rbit = 0; 548 } 549 return false; 550 } 551 552 static void rate_idx_match_mask(s8 *rate_idx, u16 *rate_flags, 553 struct ieee80211_supported_band *sband, 554 enum nl80211_chan_width chan_width, 555 u32 mask, 556 u8 mcs_mask[IEEE80211_HT_MCS_MASK_LEN], 557 u16 vht_mask[NL80211_VHT_NSS_MAX]) 558 { 559 if (*rate_flags & IEEE80211_TX_RC_VHT_MCS) { 560 /* handle VHT rates */ 561 if (rate_idx_match_vht_mcs_mask(rate_idx, vht_mask)) 562 return; 563 564 *rate_idx = 0; 565 /* keep protection flags */ 566 *rate_flags &= (IEEE80211_TX_RC_USE_RTS_CTS | 567 IEEE80211_TX_RC_USE_CTS_PROTECT | 568 IEEE80211_TX_RC_USE_SHORT_PREAMBLE); 569 570 *rate_flags |= IEEE80211_TX_RC_MCS; 571 if (chan_width == NL80211_CHAN_WIDTH_40) 572 *rate_flags |= IEEE80211_TX_RC_40_MHZ_WIDTH; 573 574 if (rate_idx_match_mcs_mask(rate_idx, mcs_mask)) 575 return; 576 577 /* also try the legacy rates. */ 578 *rate_flags &= ~(IEEE80211_TX_RC_MCS | 579 IEEE80211_TX_RC_40_MHZ_WIDTH); 580 if (rate_idx_match_legacy_mask(rate_idx, sband->n_bitrates, 581 mask)) 582 return; 583 } else if (*rate_flags & IEEE80211_TX_RC_MCS) { 584 /* handle HT rates */ 585 if (rate_idx_match_mcs_mask(rate_idx, mcs_mask)) 586 return; 587 588 /* also try the legacy rates. */ 589 *rate_idx = 0; 590 /* keep protection flags */ 591 *rate_flags &= (IEEE80211_TX_RC_USE_RTS_CTS | 592 IEEE80211_TX_RC_USE_CTS_PROTECT | 593 IEEE80211_TX_RC_USE_SHORT_PREAMBLE); 594 if (rate_idx_match_legacy_mask(rate_idx, sband->n_bitrates, 595 mask)) 596 return; 597 } else { 598 /* handle legacy rates */ 599 if (rate_idx_match_legacy_mask(rate_idx, sband->n_bitrates, 600 mask)) 601 return; 602 603 /* if HT BSS, and we handle a data frame, also try HT rates */ 604 switch (chan_width) { 605 case NL80211_CHAN_WIDTH_20_NOHT: 606 case NL80211_CHAN_WIDTH_5: 607 case NL80211_CHAN_WIDTH_10: 608 return; 609 default: 610 break; 611 } 612 613 *rate_idx = 0; 614 /* keep protection flags */ 615 *rate_flags &= (IEEE80211_TX_RC_USE_RTS_CTS | 616 IEEE80211_TX_RC_USE_CTS_PROTECT | 617 IEEE80211_TX_RC_USE_SHORT_PREAMBLE); 618 619 *rate_flags |= IEEE80211_TX_RC_MCS; 620 621 if (chan_width == NL80211_CHAN_WIDTH_40) 622 *rate_flags |= IEEE80211_TX_RC_40_MHZ_WIDTH; 623 624 if (rate_idx_match_mcs_mask(rate_idx, mcs_mask)) 625 return; 626 } 627 628 /* 629 * Uh.. No suitable rate exists. This should not really happen with 630 * sane TX rate mask configurations. However, should someone manage to 631 * configure supported rates and TX rate mask in incompatible way, 632 * allow the frame to be transmitted with whatever the rate control 633 * selected. 634 */ 635 } 636 637 static void rate_fixup_ratelist(struct ieee80211_vif *vif, 638 struct ieee80211_supported_band *sband, 639 struct ieee80211_tx_info *info, 640 struct ieee80211_tx_rate *rates, 641 int max_rates) 642 { 643 struct ieee80211_rate *rate; 644 bool inval = false; 645 int i; 646 647 /* 648 * Set up the RTS/CTS rate as the fastest basic rate 649 * that is not faster than the data rate unless there 650 * is no basic rate slower than the data rate, in which 651 * case we pick the slowest basic rate 652 * 653 * XXX: Should this check all retry rates? 654 */ 655 if (!(rates[0].flags & 656 (IEEE80211_TX_RC_MCS | IEEE80211_TX_RC_VHT_MCS))) { 657 u32 basic_rates = vif->bss_conf.basic_rates; 658 s8 baserate = basic_rates ? ffs(basic_rates) - 1 : 0; 659 660 rate = &sband->bitrates[rates[0].idx]; 661 662 for (i = 0; i < sband->n_bitrates; i++) { 663 /* must be a basic rate */ 664 if (!(basic_rates & BIT(i))) 665 continue; 666 /* must not be faster than the data rate */ 667 if (sband->bitrates[i].bitrate > rate->bitrate) 668 continue; 669 /* maximum */ 670 if (sband->bitrates[baserate].bitrate < 671 sband->bitrates[i].bitrate) 672 baserate = i; 673 } 674 675 info->control.rts_cts_rate_idx = baserate; 676 } 677 678 for (i = 0; i < max_rates; i++) { 679 /* 680 * make sure there's no valid rate following 681 * an invalid one, just in case drivers don't 682 * take the API seriously to stop at -1. 683 */ 684 if (inval) { 685 rates[i].idx = -1; 686 continue; 687 } 688 if (rates[i].idx < 0) { 689 inval = true; 690 continue; 691 } 692 693 /* 694 * For now assume MCS is already set up correctly, this 695 * needs to be fixed. 696 */ 697 if (rates[i].flags & IEEE80211_TX_RC_MCS) { 698 WARN_ON(rates[i].idx > 76); 699 700 if (!(rates[i].flags & IEEE80211_TX_RC_USE_RTS_CTS) && 701 info->control.use_cts_prot) 702 rates[i].flags |= 703 IEEE80211_TX_RC_USE_CTS_PROTECT; 704 continue; 705 } 706 707 if (rates[i].flags & IEEE80211_TX_RC_VHT_MCS) { 708 WARN_ON(ieee80211_rate_get_vht_mcs(&rates[i]) > 9); 709 continue; 710 } 711 712 /* set up RTS protection if desired */ 713 if (info->control.use_rts) { 714 rates[i].flags |= IEEE80211_TX_RC_USE_RTS_CTS; 715 info->control.use_cts_prot = false; 716 } 717 718 /* RC is busted */ 719 if (WARN_ON_ONCE(rates[i].idx >= sband->n_bitrates)) { 720 rates[i].idx = -1; 721 continue; 722 } 723 724 rate = &sband->bitrates[rates[i].idx]; 725 726 /* set up short preamble */ 727 if (info->control.short_preamble && 728 rate->flags & IEEE80211_RATE_SHORT_PREAMBLE) 729 rates[i].flags |= IEEE80211_TX_RC_USE_SHORT_PREAMBLE; 730 731 /* set up G protection */ 732 if (!(rates[i].flags & IEEE80211_TX_RC_USE_RTS_CTS) && 733 info->control.use_cts_prot && 734 rate->flags & IEEE80211_RATE_ERP_G) 735 rates[i].flags |= IEEE80211_TX_RC_USE_CTS_PROTECT; 736 } 737 } 738 739 740 static void rate_control_fill_sta_table(struct ieee80211_sta *sta, 741 struct ieee80211_tx_info *info, 742 struct ieee80211_tx_rate *rates, 743 int max_rates) 744 { 745 struct ieee80211_sta_rates *ratetbl = NULL; 746 int i; 747 748 if (sta && !info->control.skip_table) 749 ratetbl = rcu_dereference(sta->rates); 750 751 /* Fill remaining rate slots with data from the sta rate table. */ 752 max_rates = min_t(int, max_rates, IEEE80211_TX_RATE_TABLE_SIZE); 753 for (i = 0; i < max_rates; i++) { 754 if (i < ARRAY_SIZE(info->control.rates) && 755 info->control.rates[i].idx >= 0 && 756 info->control.rates[i].count) { 757 if (rates != info->control.rates) 758 rates[i] = info->control.rates[i]; 759 } else if (ratetbl) { 760 rates[i].idx = ratetbl->rate[i].idx; 761 rates[i].flags = ratetbl->rate[i].flags; 762 if (info->control.use_rts) 763 rates[i].count = ratetbl->rate[i].count_rts; 764 else if (info->control.use_cts_prot) 765 rates[i].count = ratetbl->rate[i].count_cts; 766 else 767 rates[i].count = ratetbl->rate[i].count; 768 } else { 769 rates[i].idx = -1; 770 rates[i].count = 0; 771 } 772 773 if (rates[i].idx < 0 || !rates[i].count) 774 break; 775 } 776 } 777 778 static bool rate_control_cap_mask(struct ieee80211_sub_if_data *sdata, 779 struct ieee80211_supported_band *sband, 780 struct ieee80211_sta *sta, u32 *mask, 781 u8 mcs_mask[IEEE80211_HT_MCS_MASK_LEN], 782 u16 vht_mask[NL80211_VHT_NSS_MAX]) 783 { 784 u32 i; 785 786 *mask = sdata->rc_rateidx_mask[sband->band]; 787 788 if (*mask == (1 << sband->n_bitrates) - 1 && 789 !sdata->rc_has_mcs_mask[sband->band] && 790 !sdata->rc_has_vht_mcs_mask[sband->band]) 791 return false; 792 793 if (sdata->rc_has_mcs_mask[sband->band]) 794 memcpy(mcs_mask, sdata->rc_rateidx_mcs_mask[sband->band], 795 IEEE80211_HT_MCS_MASK_LEN); 796 else 797 memset(mcs_mask, 0xff, IEEE80211_HT_MCS_MASK_LEN); 798 799 if (sdata->rc_has_vht_mcs_mask[sband->band]) 800 memcpy(vht_mask, sdata->rc_rateidx_vht_mcs_mask[sband->band], 801 sizeof(u16) * NL80211_VHT_NSS_MAX); 802 else 803 memset(vht_mask, 0xff, sizeof(u16) * NL80211_VHT_NSS_MAX); 804 805 if (sta) { 806 __le16 sta_vht_cap; 807 u16 sta_vht_mask[NL80211_VHT_NSS_MAX]; 808 809 /* Filter out rates that the STA does not support */ 810 *mask &= sta->deflink.supp_rates[sband->band]; 811 for (i = 0; i < IEEE80211_HT_MCS_MASK_LEN; i++) 812 mcs_mask[i] &= sta->deflink.ht_cap.mcs.rx_mask[i]; 813 814 sta_vht_cap = sta->deflink.vht_cap.vht_mcs.rx_mcs_map; 815 ieee80211_get_vht_mask_from_cap(sta_vht_cap, sta_vht_mask); 816 for (i = 0; i < NL80211_VHT_NSS_MAX; i++) 817 vht_mask[i] &= sta_vht_mask[i]; 818 } 819 820 return true; 821 } 822 823 static void 824 rate_control_apply_mask_ratetbl(struct sta_info *sta, 825 struct ieee80211_supported_band *sband, 826 struct ieee80211_sta_rates *rates) 827 { 828 int i; 829 u32 mask; 830 u8 mcs_mask[IEEE80211_HT_MCS_MASK_LEN]; 831 u16 vht_mask[NL80211_VHT_NSS_MAX]; 832 enum nl80211_chan_width chan_width; 833 834 if (!rate_control_cap_mask(sta->sdata, sband, &sta->sta, &mask, 835 mcs_mask, vht_mask)) 836 return; 837 838 chan_width = sta->sdata->vif.bss_conf.chanreq.oper.width; 839 for (i = 0; i < IEEE80211_TX_RATE_TABLE_SIZE; i++) { 840 if (rates->rate[i].idx < 0) 841 break; 842 843 rate_idx_match_mask(&rates->rate[i].idx, &rates->rate[i].flags, 844 sband, chan_width, mask, mcs_mask, 845 vht_mask); 846 } 847 } 848 849 static void rate_control_apply_mask(struct ieee80211_sub_if_data *sdata, 850 struct ieee80211_sta *sta, 851 struct ieee80211_supported_band *sband, 852 struct ieee80211_tx_rate *rates, 853 int max_rates) 854 { 855 enum nl80211_chan_width chan_width; 856 u8 mcs_mask[IEEE80211_HT_MCS_MASK_LEN]; 857 u32 mask; 858 u16 rate_flags, vht_mask[NL80211_VHT_NSS_MAX]; 859 int i; 860 861 /* 862 * Try to enforce the rateidx mask the user wanted. skip this if the 863 * default mask (allow all rates) is used to save some processing for 864 * the common case. 865 */ 866 if (!rate_control_cap_mask(sdata, sband, sta, &mask, mcs_mask, 867 vht_mask)) 868 return; 869 870 /* 871 * Make sure the rate index selected for each TX rate is 872 * included in the configured mask and change the rate indexes 873 * if needed. 874 */ 875 chan_width = sdata->vif.bss_conf.chanreq.oper.width; 876 for (i = 0; i < max_rates; i++) { 877 /* Skip invalid rates */ 878 if (rates[i].idx < 0) 879 break; 880 881 rate_flags = rates[i].flags; 882 rate_idx_match_mask(&rates[i].idx, &rate_flags, sband, 883 chan_width, mask, mcs_mask, vht_mask); 884 rates[i].flags = rate_flags; 885 } 886 } 887 888 void ieee80211_get_tx_rates(struct ieee80211_vif *vif, 889 struct ieee80211_sta *sta, 890 struct sk_buff *skb, 891 struct ieee80211_tx_rate *dest, 892 int max_rates) 893 { 894 struct ieee80211_sub_if_data *sdata; 895 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb); 896 struct ieee80211_supported_band *sband; 897 u32 mask = ~0; 898 899 rate_control_fill_sta_table(sta, info, dest, max_rates); 900 901 if (!vif) 902 return; 903 904 sdata = vif_to_sdata(vif); 905 if (info->band >= NUM_NL80211_BANDS) 906 return; 907 908 sband = sdata->local->hw.wiphy->bands[info->band]; 909 910 if (ieee80211_is_tx_data(skb)) 911 rate_control_apply_mask(sdata, sta, sband, dest, max_rates); 912 913 if (!(info->control.flags & IEEE80211_TX_CTRL_DONT_USE_RATE_MASK)) 914 mask = sdata->rc_rateidx_mask[info->band]; 915 916 if (dest[0].idx < 0) 917 __rate_control_send_low(&sdata->local->hw, sband, sta, info, 918 mask); 919 920 if (sta) 921 rate_fixup_ratelist(vif, sband, info, dest, max_rates); 922 } 923 EXPORT_SYMBOL(ieee80211_get_tx_rates); 924 925 void rate_control_get_rate(struct ieee80211_sub_if_data *sdata, 926 struct sta_info *sta, 927 struct ieee80211_tx_rate_control *txrc) 928 { 929 struct rate_control_ref *ref = sdata->local->rate_ctrl; 930 void *priv_sta = NULL; 931 struct ieee80211_sta *ista = NULL; 932 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(txrc->skb); 933 int i; 934 935 for (i = 0; i < IEEE80211_TX_MAX_RATES; i++) { 936 info->control.rates[i].idx = -1; 937 info->control.rates[i].flags = 0; 938 info->control.rates[i].count = 0; 939 } 940 941 if (rate_control_send_low(sta ? &sta->sta : NULL, txrc)) 942 return; 943 944 if (ieee80211_hw_check(&sdata->local->hw, HAS_RATE_CONTROL)) 945 return; 946 947 if (sta && test_sta_flag(sta, WLAN_STA_RATE_CONTROL)) { 948 ista = &sta->sta; 949 priv_sta = sta->rate_ctrl_priv; 950 } 951 952 if (ista) { 953 spin_lock_bh(&sta->rate_ctrl_lock); 954 ref->ops->get_rate(ref->priv, ista, priv_sta, txrc); 955 spin_unlock_bh(&sta->rate_ctrl_lock); 956 } else { 957 rate_control_send_low(NULL, txrc); 958 } 959 960 if (ieee80211_hw_check(&sdata->local->hw, SUPPORTS_RC_TABLE)) 961 return; 962 963 ieee80211_get_tx_rates(&sdata->vif, ista, txrc->skb, 964 info->control.rates, 965 ARRAY_SIZE(info->control.rates)); 966 } 967 968 int rate_control_set_rates(struct ieee80211_hw *hw, 969 struct ieee80211_sta *pubsta, 970 struct ieee80211_sta_rates *rates) 971 { 972 struct sta_info *sta = container_of(pubsta, struct sta_info, sta); 973 struct ieee80211_sta_rates *old; 974 struct ieee80211_supported_band *sband; 975 976 sband = ieee80211_get_sband(sta->sdata); 977 if (!sband) 978 return -EINVAL; 979 rate_control_apply_mask_ratetbl(sta, sband, rates); 980 /* 981 * mac80211 guarantees that this function will not be called 982 * concurrently, so the following RCU access is safe, even without 983 * extra locking. This can not be checked easily, so we just set 984 * the condition to true. 985 */ 986 old = rcu_dereference_protected(pubsta->rates, true); 987 rcu_assign_pointer(pubsta->rates, rates); 988 if (old) 989 kfree_rcu(old, rcu_head); 990 991 if (sta->uploaded) 992 drv_sta_rate_tbl_update(hw_to_local(hw), sta->sdata, pubsta); 993 994 return 0; 995 } 996 EXPORT_SYMBOL(rate_control_set_rates); 997 998 int ieee80211_init_rate_ctrl_alg(struct ieee80211_local *local, 999 const char *name) 1000 { 1001 struct rate_control_ref *ref; 1002 1003 ASSERT_RTNL(); 1004 1005 if (local->open_count) 1006 return -EBUSY; 1007 1008 if (ieee80211_hw_check(&local->hw, HAS_RATE_CONTROL)) { 1009 if (WARN_ON(!local->ops->set_rts_threshold)) 1010 return -EINVAL; 1011 return 0; 1012 } 1013 1014 ref = rate_control_alloc(name, local); 1015 if (!ref) { 1016 wiphy_warn(local->hw.wiphy, 1017 "Failed to select rate control algorithm\n"); 1018 return -ENOENT; 1019 } 1020 1021 WARN_ON(local->rate_ctrl); 1022 local->rate_ctrl = ref; 1023 1024 wiphy_debug(local->hw.wiphy, "Selected rate control algorithm '%s'\n", 1025 ref->ops->name); 1026 1027 return 0; 1028 } 1029 1030 void rate_control_deinitialize(struct ieee80211_local *local) 1031 { 1032 struct rate_control_ref *ref; 1033 1034 ref = local->rate_ctrl; 1035 1036 if (!ref) 1037 return; 1038 1039 local->rate_ctrl = NULL; 1040 rate_control_free(local, ref); 1041 } 1042