1 // SPDX-License-Identifier: GPL-2.0-only
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
10 #include <linux/kernel.h>
11 #include <linux/rtnetlink.h>
12 #include <linux/module.h>
13 #include <linux/slab.h>
15 #include "ieee80211_i.h"
18 struct rate_control_alg {
19 struct list_head list;
20 const struct rate_control_ops *ops;
23 static LIST_HEAD(rate_ctrl_algs);
24 static DEFINE_MUTEX(rate_ctrl_mutex);
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");
31 void rate_control_rate_init(struct sta_info *sta)
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;
40 ieee80211_sta_set_rx_nss(sta);
47 chanctx_conf = rcu_dereference(sta->sdata->vif.bss_conf.chanctx_conf);
48 if (WARN_ON(!chanctx_conf)) {
53 sband = local->hw.wiphy->bands[chanctx_conf->def.chan->band];
55 /* TODO: check for minstrel_s1g ? */
56 if (sband->band == NL80211_BAND_S1GHZ) {
57 ieee80211_s1g_sta_rate_init(sta);
62 spin_lock_bh(&sta->rate_ctrl_lock);
63 ref->ops->rate_init(ref->priv, sband, &chanctx_conf->def, ista,
65 spin_unlock_bh(&sta->rate_ctrl_lock);
67 set_sta_flag(sta, WLAN_STA_RATE_CONTROL);
70 void rate_control_tx_status(struct ieee80211_local *local,
71 struct ieee80211_supported_band *sband,
72 struct ieee80211_tx_status *st)
74 struct rate_control_ref *ref = local->rate_ctrl;
75 struct sta_info *sta = container_of(st->sta, struct sta_info, sta);
76 void *priv_sta = sta->rate_ctrl_priv;
78 if (!ref || !test_sta_flag(sta, WLAN_STA_RATE_CONTROL))
81 spin_lock_bh(&sta->rate_ctrl_lock);
82 if (ref->ops->tx_status_ext)
83 ref->ops->tx_status_ext(ref->priv, sband, priv_sta, st);
85 ref->ops->tx_status(ref->priv, sband, st->sta, priv_sta, st->skb);
89 spin_unlock_bh(&sta->rate_ctrl_lock);
92 void rate_control_rate_update(struct ieee80211_local *local,
93 struct ieee80211_supported_band *sband,
94 struct sta_info *sta, u32 changed)
96 struct rate_control_ref *ref = local->rate_ctrl;
97 struct ieee80211_sta *ista = &sta->sta;
98 void *priv_sta = sta->rate_ctrl_priv;
99 struct ieee80211_chanctx_conf *chanctx_conf;
101 if (ref && ref->ops->rate_update) {
104 chanctx_conf = rcu_dereference(sta->sdata->vif.bss_conf.chanctx_conf);
105 if (WARN_ON(!chanctx_conf)) {
110 spin_lock_bh(&sta->rate_ctrl_lock);
111 ref->ops->rate_update(ref->priv, sband, &chanctx_conf->def,
112 ista, priv_sta, changed);
113 spin_unlock_bh(&sta->rate_ctrl_lock);
116 drv_sta_rc_update(local, sta->sdata, &sta->sta, changed);
119 int ieee80211_rate_control_register(const struct rate_control_ops *ops)
121 struct rate_control_alg *alg;
126 mutex_lock(&rate_ctrl_mutex);
127 list_for_each_entry(alg, &rate_ctrl_algs, list) {
128 if (!strcmp(alg->ops->name, ops->name)) {
129 /* don't register an algorithm twice */
131 mutex_unlock(&rate_ctrl_mutex);
136 alg = kzalloc(sizeof(*alg), GFP_KERNEL);
138 mutex_unlock(&rate_ctrl_mutex);
143 list_add_tail(&alg->list, &rate_ctrl_algs);
144 mutex_unlock(&rate_ctrl_mutex);
148 EXPORT_SYMBOL(ieee80211_rate_control_register);
150 void ieee80211_rate_control_unregister(const struct rate_control_ops *ops)
152 struct rate_control_alg *alg;
154 mutex_lock(&rate_ctrl_mutex);
155 list_for_each_entry(alg, &rate_ctrl_algs, list) {
156 if (alg->ops == ops) {
157 list_del(&alg->list);
162 mutex_unlock(&rate_ctrl_mutex);
164 EXPORT_SYMBOL(ieee80211_rate_control_unregister);
166 static const struct rate_control_ops *
167 ieee80211_try_rate_control_ops_get(const char *name)
169 struct rate_control_alg *alg;
170 const struct rate_control_ops *ops = NULL;
175 mutex_lock(&rate_ctrl_mutex);
176 list_for_each_entry(alg, &rate_ctrl_algs, list) {
177 if (!strcmp(alg->ops->name, name)) {
182 mutex_unlock(&rate_ctrl_mutex);
186 /* Get the rate control algorithm. */
187 static const struct rate_control_ops *
188 ieee80211_rate_control_ops_get(const char *name)
190 const struct rate_control_ops *ops;
191 const char *alg_name;
193 kernel_param_lock(THIS_MODULE);
195 alg_name = ieee80211_default_rc_algo;
199 ops = ieee80211_try_rate_control_ops_get(alg_name);
201 /* try default if specific alg requested but not found */
202 ops = ieee80211_try_rate_control_ops_get(ieee80211_default_rc_algo);
204 /* Note: check for > 0 is intentional to avoid clang warning */
205 if (!ops && (strlen(CONFIG_MAC80211_RC_DEFAULT) > 0))
206 /* try built-in one if specific alg requested but not found */
207 ops = ieee80211_try_rate_control_ops_get(CONFIG_MAC80211_RC_DEFAULT);
209 kernel_param_unlock(THIS_MODULE);
214 #ifdef CONFIG_MAC80211_DEBUGFS
215 static ssize_t rcname_read(struct file *file, char __user *userbuf,
216 size_t count, loff_t *ppos)
218 struct rate_control_ref *ref = file->private_data;
219 int len = strlen(ref->ops->name);
221 return simple_read_from_buffer(userbuf, count, ppos,
222 ref->ops->name, len);
225 const struct file_operations rcname_ops = {
228 .llseek = default_llseek,
232 static struct rate_control_ref *
233 rate_control_alloc(const char *name, struct ieee80211_local *local)
235 struct rate_control_ref *ref;
237 ref = kmalloc(sizeof(struct rate_control_ref), GFP_KERNEL);
240 ref->ops = ieee80211_rate_control_ops_get(name);
244 ref->priv = ref->ops->alloc(&local->hw);
254 static void rate_control_free(struct ieee80211_local *local,
255 struct rate_control_ref *ctrl_ref)
257 ctrl_ref->ops->free(ctrl_ref->priv);
259 #ifdef CONFIG_MAC80211_DEBUGFS
260 debugfs_remove_recursive(local->debugfs.rcdir);
261 local->debugfs.rcdir = NULL;
267 void ieee80211_check_rate_mask(struct ieee80211_sub_if_data *sdata)
269 struct ieee80211_local *local = sdata->local;
270 struct ieee80211_supported_band *sband;
271 u32 user_mask, basic_rates = sdata->vif.bss_conf.basic_rates;
272 enum nl80211_band band;
274 if (WARN_ON(!sdata->vif.bss_conf.chandef.chan))
277 band = sdata->vif.bss_conf.chandef.chan->band;
278 if (band == NL80211_BAND_S1GHZ) {
283 if (WARN_ON_ONCE(!basic_rates))
286 user_mask = sdata->rc_rateidx_mask[band];
287 sband = local->hw.wiphy->bands[band];
289 if (user_mask & basic_rates)
293 "no overlap between basic rates (0x%x) and user mask (0x%x on band %d) - clearing the latter",
294 basic_rates, user_mask, band);
295 sdata->rc_rateidx_mask[band] = (1 << sband->n_bitrates) - 1;
298 static bool rc_no_data_or_no_ack_use_min(struct ieee80211_tx_rate_control *txrc)
300 struct sk_buff *skb = txrc->skb;
301 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
303 return (info->flags & (IEEE80211_TX_CTL_NO_ACK |
304 IEEE80211_TX_CTL_USE_MINRATE)) ||
305 !ieee80211_is_tx_data(skb);
308 static void rc_send_low_basicrate(struct ieee80211_tx_rate *rate,
310 struct ieee80211_supported_band *sband)
314 if (sband->band == NL80211_BAND_S1GHZ) {
316 rate->flags |= IEEE80211_TX_RC_S1G_MCS;
321 if (basic_rates == 0)
322 return; /* assume basic rates unknown and accept rate */
325 if (basic_rates & (1 << rate->idx))
326 return; /* selected rate is a basic rate */
328 for (i = rate->idx + 1; i <= sband->n_bitrates; i++) {
329 if (basic_rates & (1 << i)) {
335 /* could not find a basic rate; use original selection */
338 static void __rate_control_send_low(struct ieee80211_hw *hw,
339 struct ieee80211_supported_band *sband,
340 struct ieee80211_sta *sta,
341 struct ieee80211_tx_info *info,
346 ieee80211_chandef_rate_flags(&hw->conf.chandef);
348 if (sband->band == NL80211_BAND_S1GHZ) {
349 info->control.rates[0].flags |= IEEE80211_TX_RC_S1G_MCS;
350 info->control.rates[0].idx = 0;
354 if ((sband->band == NL80211_BAND_2GHZ) &&
355 (info->flags & IEEE80211_TX_CTL_NO_CCK_RATE))
356 rate_flags |= IEEE80211_RATE_ERP_G;
358 info->control.rates[0].idx = 0;
359 for (i = 0; i < sband->n_bitrates; i++) {
360 if (!(rate_mask & BIT(i)))
363 if ((rate_flags & sband->bitrates[i].flags) != rate_flags)
366 if (!rate_supported(sta, sband->band, i))
369 info->control.rates[0].idx = i;
372 WARN_ONCE(i == sband->n_bitrates,
373 "no supported rates for sta %pM (0x%x, band %d) in rate_mask 0x%x with flags 0x%x\n",
374 sta ? sta->addr : NULL,
375 sta ? sta->deflink.supp_rates[sband->band] : -1,
377 rate_mask, rate_flags);
379 info->control.rates[0].count =
380 (info->flags & IEEE80211_TX_CTL_NO_ACK) ?
381 1 : hw->max_rate_tries;
383 info->control.skip_table = 1;
387 static bool rate_control_send_low(struct ieee80211_sta *pubsta,
388 struct ieee80211_tx_rate_control *txrc)
390 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(txrc->skb);
391 struct ieee80211_supported_band *sband = txrc->sband;
392 struct sta_info *sta;
394 bool use_basicrate = false;
396 if (!pubsta || rc_no_data_or_no_ack_use_min(txrc)) {
397 __rate_control_send_low(txrc->hw, sband, pubsta, info,
398 txrc->rate_idx_mask);
400 if (!pubsta && txrc->bss) {
401 mcast_rate = txrc->bss_conf->mcast_rate[sband->band];
402 if (mcast_rate > 0) {
403 info->control.rates[0].idx = mcast_rate - 1;
406 use_basicrate = true;
408 sta = container_of(pubsta, struct sta_info, sta);
409 if (ieee80211_vif_is_mesh(&sta->sdata->vif))
410 use_basicrate = true;
414 rc_send_low_basicrate(&info->control.rates[0],
415 txrc->bss_conf->basic_rates,
423 static bool rate_idx_match_legacy_mask(s8 *rate_idx, int n_bitrates, u32 mask)
427 /* See whether the selected rate or anything below it is allowed. */
428 for (j = *rate_idx; j >= 0; j--) {
429 if (mask & (1 << j)) {
430 /* Okay, found a suitable rate. Use it. */
436 /* Try to find a higher rate that would be allowed */
437 for (j = *rate_idx + 1; j < n_bitrates; j++) {
438 if (mask & (1 << j)) {
439 /* Okay, found a suitable rate. Use it. */
447 static bool rate_idx_match_mcs_mask(s8 *rate_idx, u8 *mcs_mask)
452 ridx = *rate_idx / 8;
453 rbit = *rate_idx % 8;
456 if (ridx < 0 || ridx >= IEEE80211_HT_MCS_MASK_LEN)
459 /* See whether the selected rate or anything below it is allowed. */
460 for (i = ridx; i >= 0; i--) {
461 for (j = rbit; j >= 0; j--)
462 if (mcs_mask[i] & BIT(j)) {
463 *rate_idx = i * 8 + j;
469 /* Try to find a higher rate that would be allowed */
470 ridx = (*rate_idx + 1) / 8;
471 rbit = (*rate_idx + 1) % 8;
473 for (i = ridx; i < IEEE80211_HT_MCS_MASK_LEN; i++) {
474 for (j = rbit; j < 8; j++)
475 if (mcs_mask[i] & BIT(j)) {
476 *rate_idx = i * 8 + j;
484 static bool rate_idx_match_vht_mcs_mask(s8 *rate_idx, u16 *vht_mask)
489 ridx = *rate_idx >> 4;
490 rbit = *rate_idx & 0xf;
492 if (ridx < 0 || ridx >= NL80211_VHT_NSS_MAX)
495 /* See whether the selected rate or anything below it is allowed. */
496 for (i = ridx; i >= 0; i--) {
497 for (j = rbit; j >= 0; j--) {
498 if (vht_mask[i] & BIT(j)) {
499 *rate_idx = (i << 4) | j;
506 /* Try to find a higher rate that would be allowed */
507 ridx = (*rate_idx + 1) >> 4;
508 rbit = (*rate_idx + 1) & 0xf;
510 for (i = ridx; i < NL80211_VHT_NSS_MAX; i++) {
511 for (j = rbit; j < 16; j++) {
512 if (vht_mask[i] & BIT(j)) {
513 *rate_idx = (i << 4) | j;
522 static void rate_idx_match_mask(s8 *rate_idx, u16 *rate_flags,
523 struct ieee80211_supported_band *sband,
524 enum nl80211_chan_width chan_width,
526 u8 mcs_mask[IEEE80211_HT_MCS_MASK_LEN],
527 u16 vht_mask[NL80211_VHT_NSS_MAX])
529 if (*rate_flags & IEEE80211_TX_RC_VHT_MCS) {
530 /* handle VHT rates */
531 if (rate_idx_match_vht_mcs_mask(rate_idx, vht_mask))
535 /* keep protection flags */
536 *rate_flags &= (IEEE80211_TX_RC_USE_RTS_CTS |
537 IEEE80211_TX_RC_USE_CTS_PROTECT |
538 IEEE80211_TX_RC_USE_SHORT_PREAMBLE);
540 *rate_flags |= IEEE80211_TX_RC_MCS;
541 if (chan_width == NL80211_CHAN_WIDTH_40)
542 *rate_flags |= IEEE80211_TX_RC_40_MHZ_WIDTH;
544 if (rate_idx_match_mcs_mask(rate_idx, mcs_mask))
547 /* also try the legacy rates. */
548 *rate_flags &= ~(IEEE80211_TX_RC_MCS |
549 IEEE80211_TX_RC_40_MHZ_WIDTH);
550 if (rate_idx_match_legacy_mask(rate_idx, sband->n_bitrates,
553 } else if (*rate_flags & IEEE80211_TX_RC_MCS) {
554 /* handle HT rates */
555 if (rate_idx_match_mcs_mask(rate_idx, mcs_mask))
558 /* also try the legacy rates. */
560 /* keep protection flags */
561 *rate_flags &= (IEEE80211_TX_RC_USE_RTS_CTS |
562 IEEE80211_TX_RC_USE_CTS_PROTECT |
563 IEEE80211_TX_RC_USE_SHORT_PREAMBLE);
564 if (rate_idx_match_legacy_mask(rate_idx, sband->n_bitrates,
568 /* handle legacy rates */
569 if (rate_idx_match_legacy_mask(rate_idx, sband->n_bitrates,
573 /* if HT BSS, and we handle a data frame, also try HT rates */
574 switch (chan_width) {
575 case NL80211_CHAN_WIDTH_20_NOHT:
576 case NL80211_CHAN_WIDTH_5:
577 case NL80211_CHAN_WIDTH_10:
584 /* keep protection flags */
585 *rate_flags &= (IEEE80211_TX_RC_USE_RTS_CTS |
586 IEEE80211_TX_RC_USE_CTS_PROTECT |
587 IEEE80211_TX_RC_USE_SHORT_PREAMBLE);
589 *rate_flags |= IEEE80211_TX_RC_MCS;
591 if (chan_width == NL80211_CHAN_WIDTH_40)
592 *rate_flags |= IEEE80211_TX_RC_40_MHZ_WIDTH;
594 if (rate_idx_match_mcs_mask(rate_idx, mcs_mask))
599 * Uh.. No suitable rate exists. This should not really happen with
600 * sane TX rate mask configurations. However, should someone manage to
601 * configure supported rates and TX rate mask in incompatible way,
602 * allow the frame to be transmitted with whatever the rate control
607 static void rate_fixup_ratelist(struct ieee80211_vif *vif,
608 struct ieee80211_supported_band *sband,
609 struct ieee80211_tx_info *info,
610 struct ieee80211_tx_rate *rates,
613 struct ieee80211_rate *rate;
618 * Set up the RTS/CTS rate as the fastest basic rate
619 * that is not faster than the data rate unless there
620 * is no basic rate slower than the data rate, in which
621 * case we pick the slowest basic rate
623 * XXX: Should this check all retry rates?
625 if (!(rates[0].flags &
626 (IEEE80211_TX_RC_MCS | IEEE80211_TX_RC_VHT_MCS))) {
627 u32 basic_rates = vif->bss_conf.basic_rates;
628 s8 baserate = basic_rates ? ffs(basic_rates) - 1 : 0;
630 rate = &sband->bitrates[rates[0].idx];
632 for (i = 0; i < sband->n_bitrates; i++) {
633 /* must be a basic rate */
634 if (!(basic_rates & BIT(i)))
636 /* must not be faster than the data rate */
637 if (sband->bitrates[i].bitrate > rate->bitrate)
640 if (sband->bitrates[baserate].bitrate <
641 sband->bitrates[i].bitrate)
645 info->control.rts_cts_rate_idx = baserate;
648 for (i = 0; i < max_rates; i++) {
650 * make sure there's no valid rate following
651 * an invalid one, just in case drivers don't
652 * take the API seriously to stop at -1.
658 if (rates[i].idx < 0) {
664 * For now assume MCS is already set up correctly, this
667 if (rates[i].flags & IEEE80211_TX_RC_MCS) {
668 WARN_ON(rates[i].idx > 76);
670 if (!(rates[i].flags & IEEE80211_TX_RC_USE_RTS_CTS) &&
671 info->control.use_cts_prot)
673 IEEE80211_TX_RC_USE_CTS_PROTECT;
677 if (rates[i].flags & IEEE80211_TX_RC_VHT_MCS) {
678 WARN_ON(ieee80211_rate_get_vht_mcs(&rates[i]) > 9);
682 /* set up RTS protection if desired */
683 if (info->control.use_rts) {
684 rates[i].flags |= IEEE80211_TX_RC_USE_RTS_CTS;
685 info->control.use_cts_prot = false;
689 if (WARN_ON_ONCE(rates[i].idx >= sband->n_bitrates)) {
694 rate = &sband->bitrates[rates[i].idx];
696 /* set up short preamble */
697 if (info->control.short_preamble &&
698 rate->flags & IEEE80211_RATE_SHORT_PREAMBLE)
699 rates[i].flags |= IEEE80211_TX_RC_USE_SHORT_PREAMBLE;
701 /* set up G protection */
702 if (!(rates[i].flags & IEEE80211_TX_RC_USE_RTS_CTS) &&
703 info->control.use_cts_prot &&
704 rate->flags & IEEE80211_RATE_ERP_G)
705 rates[i].flags |= IEEE80211_TX_RC_USE_CTS_PROTECT;
710 static void rate_control_fill_sta_table(struct ieee80211_sta *sta,
711 struct ieee80211_tx_info *info,
712 struct ieee80211_tx_rate *rates,
715 struct ieee80211_sta_rates *ratetbl = NULL;
718 if (sta && !info->control.skip_table)
719 ratetbl = rcu_dereference(sta->rates);
721 /* Fill remaining rate slots with data from the sta rate table. */
722 max_rates = min_t(int, max_rates, IEEE80211_TX_RATE_TABLE_SIZE);
723 for (i = 0; i < max_rates; i++) {
724 if (i < ARRAY_SIZE(info->control.rates) &&
725 info->control.rates[i].idx >= 0 &&
726 info->control.rates[i].count) {
727 if (rates != info->control.rates)
728 rates[i] = info->control.rates[i];
729 } else if (ratetbl) {
730 rates[i].idx = ratetbl->rate[i].idx;
731 rates[i].flags = ratetbl->rate[i].flags;
732 if (info->control.use_rts)
733 rates[i].count = ratetbl->rate[i].count_rts;
734 else if (info->control.use_cts_prot)
735 rates[i].count = ratetbl->rate[i].count_cts;
737 rates[i].count = ratetbl->rate[i].count;
743 if (rates[i].idx < 0 || !rates[i].count)
748 static bool rate_control_cap_mask(struct ieee80211_sub_if_data *sdata,
749 struct ieee80211_supported_band *sband,
750 struct ieee80211_sta *sta, u32 *mask,
751 u8 mcs_mask[IEEE80211_HT_MCS_MASK_LEN],
752 u16 vht_mask[NL80211_VHT_NSS_MAX])
756 *mask = sdata->rc_rateidx_mask[sband->band];
757 flags = ieee80211_chandef_rate_flags(&sdata->vif.bss_conf.chandef);
758 for (i = 0; i < sband->n_bitrates; i++) {
759 if ((flags & sband->bitrates[i].flags) != flags)
763 if (*mask == (1 << sband->n_bitrates) - 1 &&
764 !sdata->rc_has_mcs_mask[sband->band] &&
765 !sdata->rc_has_vht_mcs_mask[sband->band])
768 if (sdata->rc_has_mcs_mask[sband->band])
769 memcpy(mcs_mask, sdata->rc_rateidx_mcs_mask[sband->band],
770 IEEE80211_HT_MCS_MASK_LEN);
772 memset(mcs_mask, 0xff, IEEE80211_HT_MCS_MASK_LEN);
774 if (sdata->rc_has_vht_mcs_mask[sband->band])
775 memcpy(vht_mask, sdata->rc_rateidx_vht_mcs_mask[sband->band],
776 sizeof(u16) * NL80211_VHT_NSS_MAX);
778 memset(vht_mask, 0xff, sizeof(u16) * NL80211_VHT_NSS_MAX);
782 u16 sta_vht_mask[NL80211_VHT_NSS_MAX];
784 /* Filter out rates that the STA does not support */
785 *mask &= sta->deflink.supp_rates[sband->band];
786 for (i = 0; i < IEEE80211_HT_MCS_MASK_LEN; i++)
787 mcs_mask[i] &= sta->deflink.ht_cap.mcs.rx_mask[i];
789 sta_vht_cap = sta->deflink.vht_cap.vht_mcs.rx_mcs_map;
790 ieee80211_get_vht_mask_from_cap(sta_vht_cap, sta_vht_mask);
791 for (i = 0; i < NL80211_VHT_NSS_MAX; i++)
792 vht_mask[i] &= sta_vht_mask[i];
799 rate_control_apply_mask_ratetbl(struct sta_info *sta,
800 struct ieee80211_supported_band *sband,
801 struct ieee80211_sta_rates *rates)
805 u8 mcs_mask[IEEE80211_HT_MCS_MASK_LEN];
806 u16 vht_mask[NL80211_VHT_NSS_MAX];
807 enum nl80211_chan_width chan_width;
809 if (!rate_control_cap_mask(sta->sdata, sband, &sta->sta, &mask,
813 chan_width = sta->sdata->vif.bss_conf.chandef.width;
814 for (i = 0; i < IEEE80211_TX_RATE_TABLE_SIZE; i++) {
815 if (rates->rate[i].idx < 0)
818 rate_idx_match_mask(&rates->rate[i].idx, &rates->rate[i].flags,
819 sband, chan_width, mask, mcs_mask,
824 static void rate_control_apply_mask(struct ieee80211_sub_if_data *sdata,
825 struct ieee80211_sta *sta,
826 struct ieee80211_supported_band *sband,
827 struct ieee80211_tx_rate *rates,
830 enum nl80211_chan_width chan_width;
831 u8 mcs_mask[IEEE80211_HT_MCS_MASK_LEN];
833 u16 rate_flags, vht_mask[NL80211_VHT_NSS_MAX];
837 * Try to enforce the rateidx mask the user wanted. skip this if the
838 * default mask (allow all rates) is used to save some processing for
841 if (!rate_control_cap_mask(sdata, sband, sta, &mask, mcs_mask,
846 * Make sure the rate index selected for each TX rate is
847 * included in the configured mask and change the rate indexes
850 chan_width = sdata->vif.bss_conf.chandef.width;
851 for (i = 0; i < max_rates; i++) {
852 /* Skip invalid rates */
853 if (rates[i].idx < 0)
856 rate_flags = rates[i].flags;
857 rate_idx_match_mask(&rates[i].idx, &rate_flags, sband,
858 chan_width, mask, mcs_mask, vht_mask);
859 rates[i].flags = rate_flags;
863 void ieee80211_get_tx_rates(struct ieee80211_vif *vif,
864 struct ieee80211_sta *sta,
866 struct ieee80211_tx_rate *dest,
869 struct ieee80211_sub_if_data *sdata;
870 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
871 struct ieee80211_supported_band *sband;
873 rate_control_fill_sta_table(sta, info, dest, max_rates);
878 sdata = vif_to_sdata(vif);
879 sband = sdata->local->hw.wiphy->bands[info->band];
881 if (ieee80211_is_tx_data(skb))
882 rate_control_apply_mask(sdata, sta, sband, dest, max_rates);
885 __rate_control_send_low(&sdata->local->hw, sband, sta, info,
886 sdata->rc_rateidx_mask[info->band]);
889 rate_fixup_ratelist(vif, sband, info, dest, max_rates);
891 EXPORT_SYMBOL(ieee80211_get_tx_rates);
893 void rate_control_get_rate(struct ieee80211_sub_if_data *sdata,
894 struct sta_info *sta,
895 struct ieee80211_tx_rate_control *txrc)
897 struct rate_control_ref *ref = sdata->local->rate_ctrl;
898 void *priv_sta = NULL;
899 struct ieee80211_sta *ista = NULL;
900 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(txrc->skb);
903 for (i = 0; i < IEEE80211_TX_MAX_RATES; i++) {
904 info->control.rates[i].idx = -1;
905 info->control.rates[i].flags = 0;
906 info->control.rates[i].count = 0;
909 if (rate_control_send_low(sta ? &sta->sta : NULL, txrc))
912 if (ieee80211_hw_check(&sdata->local->hw, HAS_RATE_CONTROL))
915 if (sta && test_sta_flag(sta, WLAN_STA_RATE_CONTROL)) {
917 priv_sta = sta->rate_ctrl_priv;
921 spin_lock_bh(&sta->rate_ctrl_lock);
922 ref->ops->get_rate(ref->priv, ista, priv_sta, txrc);
923 spin_unlock_bh(&sta->rate_ctrl_lock);
925 rate_control_send_low(NULL, txrc);
928 if (ieee80211_hw_check(&sdata->local->hw, SUPPORTS_RC_TABLE))
931 ieee80211_get_tx_rates(&sdata->vif, ista, txrc->skb,
933 ARRAY_SIZE(info->control.rates));
936 int rate_control_set_rates(struct ieee80211_hw *hw,
937 struct ieee80211_sta *pubsta,
938 struct ieee80211_sta_rates *rates)
940 struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
941 struct ieee80211_sta_rates *old;
942 struct ieee80211_supported_band *sband;
944 sband = ieee80211_get_sband(sta->sdata);
947 rate_control_apply_mask_ratetbl(sta, sband, rates);
949 * mac80211 guarantees that this function will not be called
950 * concurrently, so the following RCU access is safe, even without
951 * extra locking. This can not be checked easily, so we just set
952 * the condition to true.
954 old = rcu_dereference_protected(pubsta->rates, true);
955 rcu_assign_pointer(pubsta->rates, rates);
957 kfree_rcu(old, rcu_head);
960 drv_sta_rate_tbl_update(hw_to_local(hw), sta->sdata, pubsta);
962 ieee80211_sta_set_expected_throughput(pubsta, sta_get_expected_throughput(sta));
966 EXPORT_SYMBOL(rate_control_set_rates);
968 int ieee80211_init_rate_ctrl_alg(struct ieee80211_local *local,
971 struct rate_control_ref *ref;
975 if (local->open_count)
978 if (ieee80211_hw_check(&local->hw, HAS_RATE_CONTROL)) {
979 if (WARN_ON(!local->ops->set_rts_threshold))
984 ref = rate_control_alloc(name, local);
986 wiphy_warn(local->hw.wiphy,
987 "Failed to select rate control algorithm\n");
991 WARN_ON(local->rate_ctrl);
992 local->rate_ctrl = ref;
994 wiphy_debug(local->hw.wiphy, "Selected rate control algorithm '%s'\n",
1000 void rate_control_deinitialize(struct ieee80211_local *local)
1002 struct rate_control_ref *ref;
1004 ref = local->rate_ctrl;
1009 local->rate_ctrl = NULL;
1010 rate_control_free(local, ref);