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