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
9 #include <linux/kernel.h>
10 #include <linux/rtnetlink.h>
11 #include <linux/module.h>
12 #include <linux/slab.h>
14 #include "ieee80211_i.h"
17 struct rate_control_alg {
18 struct list_head list;
19 const struct rate_control_ops *ops;
22 static LIST_HEAD(rate_ctrl_algs);
23 static DEFINE_MUTEX(rate_ctrl_mutex);
25 static char *ieee80211_default_rc_algo = CONFIG_MAC80211_RC_DEFAULT;
26 module_param(ieee80211_default_rc_algo, charp, 0644);
27 MODULE_PARM_DESC(ieee80211_default_rc_algo,
28 "Default rate control algorithm for mac80211 to use");
30 void rate_control_rate_init(struct sta_info *sta)
32 struct ieee80211_local *local = sta->sdata->local;
33 struct rate_control_ref *ref = sta->rate_ctrl;
34 struct ieee80211_sta *ista = &sta->sta;
35 void *priv_sta = sta->rate_ctrl_priv;
36 struct ieee80211_supported_band *sband;
37 struct ieee80211_chanctx_conf *chanctx_conf;
39 ieee80211_sta_set_rx_nss(sta);
46 chanctx_conf = rcu_dereference(sta->sdata->vif.chanctx_conf);
47 if (WARN_ON(!chanctx_conf)) {
52 sband = local->hw.wiphy->bands[chanctx_conf->def.chan->band];
54 /* TODO: check for minstrel_s1g ? */
55 if (sband->band == NL80211_BAND_S1GHZ) {
56 ieee80211_s1g_sta_rate_init(sta);
61 spin_lock_bh(&sta->rate_ctrl_lock);
62 ref->ops->rate_init(ref->priv, sband, &chanctx_conf->def, ista,
64 spin_unlock_bh(&sta->rate_ctrl_lock);
66 set_sta_flag(sta, WLAN_STA_RATE_CONTROL);
69 void rate_control_tx_status(struct ieee80211_local *local,
70 struct ieee80211_supported_band *sband,
71 struct ieee80211_tx_status *st)
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;
77 if (!ref || !test_sta_flag(sta, WLAN_STA_RATE_CONTROL))
80 spin_lock_bh(&sta->rate_ctrl_lock);
81 if (ref->ops->tx_status_ext)
82 ref->ops->tx_status_ext(ref->priv, sband, priv_sta, st);
84 ref->ops->tx_status(ref->priv, sband, st->sta, priv_sta, st->skb);
88 spin_unlock_bh(&sta->rate_ctrl_lock);
91 void rate_control_rate_update(struct ieee80211_local *local,
92 struct ieee80211_supported_band *sband,
93 struct sta_info *sta, u32 changed)
95 struct rate_control_ref *ref = local->rate_ctrl;
96 struct ieee80211_sta *ista = &sta->sta;
97 void *priv_sta = sta->rate_ctrl_priv;
98 struct ieee80211_chanctx_conf *chanctx_conf;
100 if (ref && ref->ops->rate_update) {
103 chanctx_conf = rcu_dereference(sta->sdata->vif.chanctx_conf);
104 if (WARN_ON(!chanctx_conf)) {
109 spin_lock_bh(&sta->rate_ctrl_lock);
110 ref->ops->rate_update(ref->priv, sband, &chanctx_conf->def,
111 ista, priv_sta, changed);
112 spin_unlock_bh(&sta->rate_ctrl_lock);
115 drv_sta_rc_update(local, sta->sdata, &sta->sta, changed);
118 int ieee80211_rate_control_register(const struct rate_control_ops *ops)
120 struct rate_control_alg *alg;
125 mutex_lock(&rate_ctrl_mutex);
126 list_for_each_entry(alg, &rate_ctrl_algs, list) {
127 if (!strcmp(alg->ops->name, ops->name)) {
128 /* don't register an algorithm twice */
130 mutex_unlock(&rate_ctrl_mutex);
135 alg = kzalloc(sizeof(*alg), GFP_KERNEL);
137 mutex_unlock(&rate_ctrl_mutex);
142 list_add_tail(&alg->list, &rate_ctrl_algs);
143 mutex_unlock(&rate_ctrl_mutex);
147 EXPORT_SYMBOL(ieee80211_rate_control_register);
149 void ieee80211_rate_control_unregister(const struct rate_control_ops *ops)
151 struct rate_control_alg *alg;
153 mutex_lock(&rate_ctrl_mutex);
154 list_for_each_entry(alg, &rate_ctrl_algs, list) {
155 if (alg->ops == ops) {
156 list_del(&alg->list);
161 mutex_unlock(&rate_ctrl_mutex);
163 EXPORT_SYMBOL(ieee80211_rate_control_unregister);
165 static const struct rate_control_ops *
166 ieee80211_try_rate_control_ops_get(const char *name)
168 struct rate_control_alg *alg;
169 const struct rate_control_ops *ops = NULL;
174 mutex_lock(&rate_ctrl_mutex);
175 list_for_each_entry(alg, &rate_ctrl_algs, list) {
176 if (!strcmp(alg->ops->name, name)) {
181 mutex_unlock(&rate_ctrl_mutex);
185 /* Get the rate control algorithm. */
186 static const struct rate_control_ops *
187 ieee80211_rate_control_ops_get(const char *name)
189 const struct rate_control_ops *ops;
190 const char *alg_name;
192 kernel_param_lock(THIS_MODULE);
194 alg_name = ieee80211_default_rc_algo;
198 ops = ieee80211_try_rate_control_ops_get(alg_name);
200 /* try default if specific alg requested but not found */
201 ops = ieee80211_try_rate_control_ops_get(ieee80211_default_rc_algo);
203 /* Note: check for > 0 is intentional to avoid clang warning */
204 if (!ops && (strlen(CONFIG_MAC80211_RC_DEFAULT) > 0))
205 /* try built-in one if specific alg requested but not found */
206 ops = ieee80211_try_rate_control_ops_get(CONFIG_MAC80211_RC_DEFAULT);
208 kernel_param_unlock(THIS_MODULE);
213 #ifdef CONFIG_MAC80211_DEBUGFS
214 static ssize_t rcname_read(struct file *file, char __user *userbuf,
215 size_t count, loff_t *ppos)
217 struct rate_control_ref *ref = file->private_data;
218 int len = strlen(ref->ops->name);
220 return simple_read_from_buffer(userbuf, count, ppos,
221 ref->ops->name, len);
224 const struct file_operations rcname_ops = {
227 .llseek = default_llseek,
231 static struct rate_control_ref *
232 rate_control_alloc(const char *name, struct ieee80211_local *local)
234 struct rate_control_ref *ref;
236 ref = kmalloc(sizeof(struct rate_control_ref), GFP_KERNEL);
239 ref->ops = ieee80211_rate_control_ops_get(name);
243 ref->priv = ref->ops->alloc(&local->hw);
253 static void rate_control_free(struct ieee80211_local *local,
254 struct rate_control_ref *ctrl_ref)
256 ctrl_ref->ops->free(ctrl_ref->priv);
258 #ifdef CONFIG_MAC80211_DEBUGFS
259 debugfs_remove_recursive(local->debugfs.rcdir);
260 local->debugfs.rcdir = NULL;
266 void ieee80211_check_rate_mask(struct ieee80211_sub_if_data *sdata)
268 struct ieee80211_local *local = sdata->local;
269 struct ieee80211_supported_band *sband;
270 u32 user_mask, basic_rates = sdata->vif.bss_conf.basic_rates;
271 enum nl80211_band band;
273 if (WARN_ON(!sdata->vif.bss_conf.chandef.chan))
276 band = sdata->vif.bss_conf.chandef.chan->band;
277 if (band == NL80211_BAND_S1GHZ) {
282 if (WARN_ON_ONCE(!basic_rates))
285 user_mask = sdata->rc_rateidx_mask[band];
286 sband = local->hw.wiphy->bands[band];
288 if (user_mask & basic_rates)
292 "no overlap between basic rates (0x%x) and user mask (0x%x on band %d) - clearing the latter",
293 basic_rates, user_mask, band);
294 sdata->rc_rateidx_mask[band] = (1 << sband->n_bitrates) - 1;
297 static bool rc_no_data_or_no_ack_use_min(struct ieee80211_tx_rate_control *txrc)
299 struct sk_buff *skb = txrc->skb;
300 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
302 return (info->flags & (IEEE80211_TX_CTL_NO_ACK |
303 IEEE80211_TX_CTL_USE_MINRATE)) ||
304 !ieee80211_is_tx_data(skb);
307 static void rc_send_low_basicrate(struct ieee80211_tx_rate *rate,
309 struct ieee80211_supported_band *sband)
313 if (sband->band == NL80211_BAND_S1GHZ) {
315 rate->flags |= IEEE80211_TX_RC_S1G_MCS;
320 if (basic_rates == 0)
321 return; /* assume basic rates unknown and accept rate */
324 if (basic_rates & (1 << rate->idx))
325 return; /* selected rate is a basic rate */
327 for (i = rate->idx + 1; i <= sband->n_bitrates; i++) {
328 if (basic_rates & (1 << i)) {
334 /* could not find a basic rate; use original selection */
337 static void __rate_control_send_low(struct ieee80211_hw *hw,
338 struct ieee80211_supported_band *sband,
339 struct ieee80211_sta *sta,
340 struct ieee80211_tx_info *info,
345 ieee80211_chandef_rate_flags(&hw->conf.chandef);
347 if (sband->band == NL80211_BAND_S1GHZ) {
348 info->control.rates[0].flags |= IEEE80211_TX_RC_S1G_MCS;
349 info->control.rates[0].idx = 0;
353 if ((sband->band == NL80211_BAND_2GHZ) &&
354 (info->flags & IEEE80211_TX_CTL_NO_CCK_RATE))
355 rate_flags |= IEEE80211_RATE_ERP_G;
357 info->control.rates[0].idx = 0;
358 for (i = 0; i < sband->n_bitrates; i++) {
359 if (!(rate_mask & BIT(i)))
362 if ((rate_flags & sband->bitrates[i].flags) != rate_flags)
365 if (!rate_supported(sta, sband->band, i))
368 info->control.rates[0].idx = i;
371 WARN_ONCE(i == sband->n_bitrates,
372 "no supported rates for sta %pM (0x%x, band %d) in rate_mask 0x%x with flags 0x%x\n",
373 sta ? sta->addr : NULL,
374 sta ? sta->supp_rates[sband->band] : -1,
376 rate_mask, rate_flags);
378 info->control.rates[0].count =
379 (info->flags & IEEE80211_TX_CTL_NO_ACK) ?
380 1 : hw->max_rate_tries;
382 info->control.skip_table = 1;
386 static bool rate_control_send_low(struct ieee80211_sta *pubsta,
387 struct ieee80211_tx_rate_control *txrc)
389 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(txrc->skb);
390 struct ieee80211_supported_band *sband = txrc->sband;
391 struct sta_info *sta;
393 bool use_basicrate = false;
395 if (!pubsta || rc_no_data_or_no_ack_use_min(txrc)) {
396 __rate_control_send_low(txrc->hw, sband, pubsta, info,
397 txrc->rate_idx_mask);
399 if (!pubsta && txrc->bss) {
400 mcast_rate = txrc->bss_conf->mcast_rate[sband->band];
401 if (mcast_rate > 0) {
402 info->control.rates[0].idx = mcast_rate - 1;
405 use_basicrate = true;
407 sta = container_of(pubsta, struct sta_info, sta);
408 if (ieee80211_vif_is_mesh(&sta->sdata->vif))
409 use_basicrate = true;
413 rc_send_low_basicrate(&info->control.rates[0],
414 txrc->bss_conf->basic_rates,
422 static bool rate_idx_match_legacy_mask(s8 *rate_idx, int n_bitrates, u32 mask)
426 /* See whether the selected rate or anything below it is allowed. */
427 for (j = *rate_idx; j >= 0; j--) {
428 if (mask & (1 << j)) {
429 /* Okay, found a suitable rate. Use it. */
435 /* Try to find a higher rate that would be allowed */
436 for (j = *rate_idx + 1; j < n_bitrates; j++) {
437 if (mask & (1 << j)) {
438 /* Okay, found a suitable rate. Use it. */
446 static bool rate_idx_match_mcs_mask(s8 *rate_idx, u8 *mcs_mask)
451 ridx = *rate_idx / 8;
452 rbit = *rate_idx % 8;
455 if (ridx < 0 || ridx >= IEEE80211_HT_MCS_MASK_LEN)
458 /* See whether the selected rate or anything below it is allowed. */
459 for (i = ridx; i >= 0; i--) {
460 for (j = rbit; j >= 0; j--)
461 if (mcs_mask[i] & BIT(j)) {
462 *rate_idx = i * 8 + j;
468 /* Try to find a higher rate that would be allowed */
469 ridx = (*rate_idx + 1) / 8;
470 rbit = (*rate_idx + 1) % 8;
472 for (i = ridx; i < IEEE80211_HT_MCS_MASK_LEN; i++) {
473 for (j = rbit; j < 8; j++)
474 if (mcs_mask[i] & BIT(j)) {
475 *rate_idx = i * 8 + j;
483 static bool rate_idx_match_vht_mcs_mask(s8 *rate_idx, u16 *vht_mask)
488 ridx = *rate_idx >> 4;
489 rbit = *rate_idx & 0xf;
491 if (ridx < 0 || ridx >= NL80211_VHT_NSS_MAX)
494 /* See whether the selected rate or anything below it is allowed. */
495 for (i = ridx; i >= 0; i--) {
496 for (j = rbit; j >= 0; j--) {
497 if (vht_mask[i] & BIT(j)) {
498 *rate_idx = (i << 4) | j;
505 /* Try to find a higher rate that would be allowed */
506 ridx = (*rate_idx + 1) >> 4;
507 rbit = (*rate_idx + 1) & 0xf;
509 for (i = ridx; i < NL80211_VHT_NSS_MAX; i++) {
510 for (j = rbit; j < 16; j++) {
511 if (vht_mask[i] & BIT(j)) {
512 *rate_idx = (i << 4) | j;
521 static void rate_idx_match_mask(s8 *rate_idx, u16 *rate_flags,
522 struct ieee80211_supported_band *sband,
523 enum nl80211_chan_width chan_width,
525 u8 mcs_mask[IEEE80211_HT_MCS_MASK_LEN],
526 u16 vht_mask[NL80211_VHT_NSS_MAX])
528 if (*rate_flags & IEEE80211_TX_RC_VHT_MCS) {
529 /* handle VHT rates */
530 if (rate_idx_match_vht_mcs_mask(rate_idx, vht_mask))
534 /* keep protection flags */
535 *rate_flags &= (IEEE80211_TX_RC_USE_RTS_CTS |
536 IEEE80211_TX_RC_USE_CTS_PROTECT |
537 IEEE80211_TX_RC_USE_SHORT_PREAMBLE);
539 *rate_flags |= IEEE80211_TX_RC_MCS;
540 if (chan_width == NL80211_CHAN_WIDTH_40)
541 *rate_flags |= IEEE80211_TX_RC_40_MHZ_WIDTH;
543 if (rate_idx_match_mcs_mask(rate_idx, mcs_mask))
546 /* also try the legacy rates. */
547 *rate_flags &= ~(IEEE80211_TX_RC_MCS |
548 IEEE80211_TX_RC_40_MHZ_WIDTH);
549 if (rate_idx_match_legacy_mask(rate_idx, sband->n_bitrates,
552 } else if (*rate_flags & IEEE80211_TX_RC_MCS) {
553 /* handle HT rates */
554 if (rate_idx_match_mcs_mask(rate_idx, mcs_mask))
557 /* also try the legacy rates. */
559 /* keep protection flags */
560 *rate_flags &= (IEEE80211_TX_RC_USE_RTS_CTS |
561 IEEE80211_TX_RC_USE_CTS_PROTECT |
562 IEEE80211_TX_RC_USE_SHORT_PREAMBLE);
563 if (rate_idx_match_legacy_mask(rate_idx, sband->n_bitrates,
567 /* handle legacy rates */
568 if (rate_idx_match_legacy_mask(rate_idx, sband->n_bitrates,
572 /* if HT BSS, and we handle a data frame, also try HT rates */
573 switch (chan_width) {
574 case NL80211_CHAN_WIDTH_20_NOHT:
575 case NL80211_CHAN_WIDTH_5:
576 case NL80211_CHAN_WIDTH_10:
583 /* keep protection flags */
584 *rate_flags &= (IEEE80211_TX_RC_USE_RTS_CTS |
585 IEEE80211_TX_RC_USE_CTS_PROTECT |
586 IEEE80211_TX_RC_USE_SHORT_PREAMBLE);
588 *rate_flags |= IEEE80211_TX_RC_MCS;
590 if (chan_width == NL80211_CHAN_WIDTH_40)
591 *rate_flags |= IEEE80211_TX_RC_40_MHZ_WIDTH;
593 if (rate_idx_match_mcs_mask(rate_idx, mcs_mask))
598 * Uh.. No suitable rate exists. This should not really happen with
599 * sane TX rate mask configurations. However, should someone manage to
600 * configure supported rates and TX rate mask in incompatible way,
601 * allow the frame to be transmitted with whatever the rate control
606 static void rate_fixup_ratelist(struct ieee80211_vif *vif,
607 struct ieee80211_supported_band *sband,
608 struct ieee80211_tx_info *info,
609 struct ieee80211_tx_rate *rates,
612 struct ieee80211_rate *rate;
617 * Set up the RTS/CTS rate as the fastest basic rate
618 * that is not faster than the data rate unless there
619 * is no basic rate slower than the data rate, in which
620 * case we pick the slowest basic rate
622 * XXX: Should this check all retry rates?
624 if (!(rates[0].flags &
625 (IEEE80211_TX_RC_MCS | IEEE80211_TX_RC_VHT_MCS))) {
626 u32 basic_rates = vif->bss_conf.basic_rates;
627 s8 baserate = basic_rates ? ffs(basic_rates) - 1 : 0;
629 rate = &sband->bitrates[rates[0].idx];
631 for (i = 0; i < sband->n_bitrates; i++) {
632 /* must be a basic rate */
633 if (!(basic_rates & BIT(i)))
635 /* must not be faster than the data rate */
636 if (sband->bitrates[i].bitrate > rate->bitrate)
639 if (sband->bitrates[baserate].bitrate <
640 sband->bitrates[i].bitrate)
644 info->control.rts_cts_rate_idx = baserate;
647 for (i = 0; i < max_rates; i++) {
649 * make sure there's no valid rate following
650 * an invalid one, just in case drivers don't
651 * take the API seriously to stop at -1.
657 if (rates[i].idx < 0) {
663 * For now assume MCS is already set up correctly, this
666 if (rates[i].flags & IEEE80211_TX_RC_MCS) {
667 WARN_ON(rates[i].idx > 76);
669 if (!(rates[i].flags & IEEE80211_TX_RC_USE_RTS_CTS) &&
670 info->control.use_cts_prot)
672 IEEE80211_TX_RC_USE_CTS_PROTECT;
676 if (rates[i].flags & IEEE80211_TX_RC_VHT_MCS) {
677 WARN_ON(ieee80211_rate_get_vht_mcs(&rates[i]) > 9);
681 /* set up RTS protection if desired */
682 if (info->control.use_rts) {
683 rates[i].flags |= IEEE80211_TX_RC_USE_RTS_CTS;
684 info->control.use_cts_prot = false;
688 if (WARN_ON_ONCE(rates[i].idx >= sband->n_bitrates)) {
693 rate = &sband->bitrates[rates[i].idx];
695 /* set up short preamble */
696 if (info->control.short_preamble &&
697 rate->flags & IEEE80211_RATE_SHORT_PREAMBLE)
698 rates[i].flags |= IEEE80211_TX_RC_USE_SHORT_PREAMBLE;
700 /* set up G protection */
701 if (!(rates[i].flags & IEEE80211_TX_RC_USE_RTS_CTS) &&
702 info->control.use_cts_prot &&
703 rate->flags & IEEE80211_RATE_ERP_G)
704 rates[i].flags |= IEEE80211_TX_RC_USE_CTS_PROTECT;
709 static void rate_control_fill_sta_table(struct ieee80211_sta *sta,
710 struct ieee80211_tx_info *info,
711 struct ieee80211_tx_rate *rates,
714 struct ieee80211_sta_rates *ratetbl = NULL;
717 if (sta && !info->control.skip_table)
718 ratetbl = rcu_dereference(sta->rates);
720 /* Fill remaining rate slots with data from the sta rate table. */
721 max_rates = min_t(int, max_rates, IEEE80211_TX_RATE_TABLE_SIZE);
722 for (i = 0; i < max_rates; i++) {
723 if (i < ARRAY_SIZE(info->control.rates) &&
724 info->control.rates[i].idx >= 0 &&
725 info->control.rates[i].count) {
726 if (rates != info->control.rates)
727 rates[i] = info->control.rates[i];
728 } else if (ratetbl) {
729 rates[i].idx = ratetbl->rate[i].idx;
730 rates[i].flags = ratetbl->rate[i].flags;
731 if (info->control.use_rts)
732 rates[i].count = ratetbl->rate[i].count_rts;
733 else if (info->control.use_cts_prot)
734 rates[i].count = ratetbl->rate[i].count_cts;
736 rates[i].count = ratetbl->rate[i].count;
742 if (rates[i].idx < 0 || !rates[i].count)
747 static bool rate_control_cap_mask(struct ieee80211_sub_if_data *sdata,
748 struct ieee80211_supported_band *sband,
749 struct ieee80211_sta *sta, u32 *mask,
750 u8 mcs_mask[IEEE80211_HT_MCS_MASK_LEN],
751 u16 vht_mask[NL80211_VHT_NSS_MAX])
755 *mask = sdata->rc_rateidx_mask[sband->band];
756 flags = ieee80211_chandef_rate_flags(&sdata->vif.bss_conf.chandef);
757 for (i = 0; i < sband->n_bitrates; i++) {
758 if ((flags & sband->bitrates[i].flags) != flags)
762 if (*mask == (1 << sband->n_bitrates) - 1 &&
763 !sdata->rc_has_mcs_mask[sband->band] &&
764 !sdata->rc_has_vht_mcs_mask[sband->band])
767 if (sdata->rc_has_mcs_mask[sband->band])
768 memcpy(mcs_mask, sdata->rc_rateidx_mcs_mask[sband->band],
769 IEEE80211_HT_MCS_MASK_LEN);
771 memset(mcs_mask, 0xff, IEEE80211_HT_MCS_MASK_LEN);
773 if (sdata->rc_has_vht_mcs_mask[sband->band])
774 memcpy(vht_mask, sdata->rc_rateidx_vht_mcs_mask[sband->band],
775 sizeof(u16) * NL80211_VHT_NSS_MAX);
777 memset(vht_mask, 0xff, sizeof(u16) * NL80211_VHT_NSS_MAX);
781 u16 sta_vht_mask[NL80211_VHT_NSS_MAX];
783 /* Filter out rates that the STA does not support */
784 *mask &= sta->supp_rates[sband->band];
785 for (i = 0; i < IEEE80211_HT_MCS_MASK_LEN; i++)
786 mcs_mask[i] &= sta->ht_cap.mcs.rx_mask[i];
788 sta_vht_cap = sta->vht_cap.vht_mcs.rx_mcs_map;
789 ieee80211_get_vht_mask_from_cap(sta_vht_cap, sta_vht_mask);
790 for (i = 0; i < NL80211_VHT_NSS_MAX; i++)
791 vht_mask[i] &= sta_vht_mask[i];
798 rate_control_apply_mask_ratetbl(struct sta_info *sta,
799 struct ieee80211_supported_band *sband,
800 struct ieee80211_sta_rates *rates)
804 u8 mcs_mask[IEEE80211_HT_MCS_MASK_LEN];
805 u16 vht_mask[NL80211_VHT_NSS_MAX];
806 enum nl80211_chan_width chan_width;
808 if (!rate_control_cap_mask(sta->sdata, sband, &sta->sta, &mask,
812 chan_width = sta->sdata->vif.bss_conf.chandef.width;
813 for (i = 0; i < IEEE80211_TX_RATE_TABLE_SIZE; i++) {
814 if (rates->rate[i].idx < 0)
817 rate_idx_match_mask(&rates->rate[i].idx, &rates->rate[i].flags,
818 sband, chan_width, mask, mcs_mask,
823 static void rate_control_apply_mask(struct ieee80211_sub_if_data *sdata,
824 struct ieee80211_sta *sta,
825 struct ieee80211_supported_band *sband,
826 struct ieee80211_tx_rate *rates,
829 enum nl80211_chan_width chan_width;
830 u8 mcs_mask[IEEE80211_HT_MCS_MASK_LEN];
832 u16 rate_flags, vht_mask[NL80211_VHT_NSS_MAX];
836 * Try to enforce the rateidx mask the user wanted. skip this if the
837 * default mask (allow all rates) is used to save some processing for
840 if (!rate_control_cap_mask(sdata, sband, sta, &mask, mcs_mask,
845 * Make sure the rate index selected for each TX rate is
846 * included in the configured mask and change the rate indexes
849 chan_width = sdata->vif.bss_conf.chandef.width;
850 for (i = 0; i < max_rates; i++) {
851 /* Skip invalid rates */
852 if (rates[i].idx < 0)
855 rate_flags = rates[i].flags;
856 rate_idx_match_mask(&rates[i].idx, &rate_flags, sband,
857 chan_width, mask, mcs_mask, vht_mask);
858 rates[i].flags = rate_flags;
862 void ieee80211_get_tx_rates(struct ieee80211_vif *vif,
863 struct ieee80211_sta *sta,
865 struct ieee80211_tx_rate *dest,
868 struct ieee80211_sub_if_data *sdata;
869 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
870 struct ieee80211_supported_band *sband;
872 rate_control_fill_sta_table(sta, info, dest, max_rates);
877 sdata = vif_to_sdata(vif);
878 sband = sdata->local->hw.wiphy->bands[info->band];
880 if (ieee80211_is_tx_data(skb))
881 rate_control_apply_mask(sdata, sta, sband, dest, max_rates);
884 __rate_control_send_low(&sdata->local->hw, sband, sta, info,
885 sdata->rc_rateidx_mask[info->band]);
888 rate_fixup_ratelist(vif, sband, info, dest, max_rates);
890 EXPORT_SYMBOL(ieee80211_get_tx_rates);
892 void rate_control_get_rate(struct ieee80211_sub_if_data *sdata,
893 struct sta_info *sta,
894 struct ieee80211_tx_rate_control *txrc)
896 struct rate_control_ref *ref = sdata->local->rate_ctrl;
897 void *priv_sta = NULL;
898 struct ieee80211_sta *ista = NULL;
899 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(txrc->skb);
902 for (i = 0; i < IEEE80211_TX_MAX_RATES; i++) {
903 info->control.rates[i].idx = -1;
904 info->control.rates[i].flags = 0;
905 info->control.rates[i].count = 0;
908 if (rate_control_send_low(sta ? &sta->sta : NULL, txrc))
911 if (ieee80211_hw_check(&sdata->local->hw, HAS_RATE_CONTROL))
914 if (sta && test_sta_flag(sta, WLAN_STA_RATE_CONTROL)) {
916 priv_sta = sta->rate_ctrl_priv;
920 spin_lock_bh(&sta->rate_ctrl_lock);
921 ref->ops->get_rate(ref->priv, ista, priv_sta, txrc);
922 spin_unlock_bh(&sta->rate_ctrl_lock);
924 rate_control_send_low(NULL, txrc);
927 if (ieee80211_hw_check(&sdata->local->hw, SUPPORTS_RC_TABLE))
930 ieee80211_get_tx_rates(&sdata->vif, ista, txrc->skb,
932 ARRAY_SIZE(info->control.rates));
935 int rate_control_set_rates(struct ieee80211_hw *hw,
936 struct ieee80211_sta *pubsta,
937 struct ieee80211_sta_rates *rates)
939 struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
940 struct ieee80211_sta_rates *old;
941 struct ieee80211_supported_band *sband;
943 sband = ieee80211_get_sband(sta->sdata);
946 rate_control_apply_mask_ratetbl(sta, sband, rates);
948 * mac80211 guarantees that this function will not be called
949 * concurrently, so the following RCU access is safe, even without
950 * extra locking. This can not be checked easily, so we just set
951 * the condition to true.
953 old = rcu_dereference_protected(pubsta->rates, true);
954 rcu_assign_pointer(pubsta->rates, rates);
956 kfree_rcu(old, rcu_head);
959 drv_sta_rate_tbl_update(hw_to_local(hw), sta->sdata, pubsta);
961 ieee80211_sta_set_expected_throughput(pubsta, sta_get_expected_throughput(sta));
965 EXPORT_SYMBOL(rate_control_set_rates);
967 int ieee80211_init_rate_ctrl_alg(struct ieee80211_local *local,
970 struct rate_control_ref *ref;
974 if (local->open_count)
977 if (ieee80211_hw_check(&local->hw, HAS_RATE_CONTROL)) {
978 if (WARN_ON(!local->ops->set_rts_threshold))
983 ref = rate_control_alloc(name, local);
985 wiphy_warn(local->hw.wiphy,
986 "Failed to select rate control algorithm\n");
990 WARN_ON(local->rate_ctrl);
991 local->rate_ctrl = ref;
993 wiphy_debug(local->hw.wiphy, "Selected rate control algorithm '%s'\n",
999 void rate_control_deinitialize(struct ieee80211_local *local)
1001 struct rate_control_ref *ref;
1003 ref = local->rate_ctrl;
1008 local->rate_ctrl = NULL;
1009 rate_control_free(local, ref);