2 * Copyright 2002-2005, Instant802 Networks, Inc.
3 * Copyright 2005-2006, Devicescape Software, Inc.
4 * Copyright (c) 2006 Jiri Benc <jbenc@suse.cz>
5 * Copyright 2017 Intel Deutschland GmbH
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
12 #include <linux/kernel.h>
13 #include <linux/rtnetlink.h>
14 #include <linux/module.h>
15 #include <linux/slab.h>
17 #include "ieee80211_i.h"
20 struct rate_control_alg {
21 struct list_head list;
22 const struct rate_control_ops *ops;
25 static LIST_HEAD(rate_ctrl_algs);
26 static DEFINE_MUTEX(rate_ctrl_mutex);
28 static char *ieee80211_default_rc_algo = CONFIG_MAC80211_RC_DEFAULT;
29 module_param(ieee80211_default_rc_algo, charp, 0644);
30 MODULE_PARM_DESC(ieee80211_default_rc_algo,
31 "Default rate control algorithm for mac80211 to use");
33 void rate_control_rate_init(struct sta_info *sta)
35 struct ieee80211_local *local = sta->sdata->local;
36 struct rate_control_ref *ref = sta->rate_ctrl;
37 struct ieee80211_sta *ista = &sta->sta;
38 void *priv_sta = sta->rate_ctrl_priv;
39 struct ieee80211_supported_band *sband;
40 struct ieee80211_chanctx_conf *chanctx_conf;
42 ieee80211_sta_set_rx_nss(sta);
49 chanctx_conf = rcu_dereference(sta->sdata->vif.chanctx_conf);
50 if (WARN_ON(!chanctx_conf)) {
55 sband = local->hw.wiphy->bands[chanctx_conf->def.chan->band];
57 spin_lock_bh(&sta->rate_ctrl_lock);
58 ref->ops->rate_init(ref->priv, sband, &chanctx_conf->def, ista,
60 spin_unlock_bh(&sta->rate_ctrl_lock);
62 set_sta_flag(sta, WLAN_STA_RATE_CONTROL);
65 void rate_control_rate_update(struct ieee80211_local *local,
66 struct ieee80211_supported_band *sband,
67 struct sta_info *sta, u32 changed)
69 struct rate_control_ref *ref = local->rate_ctrl;
70 struct ieee80211_sta *ista = &sta->sta;
71 void *priv_sta = sta->rate_ctrl_priv;
72 struct ieee80211_chanctx_conf *chanctx_conf;
74 if (ref && ref->ops->rate_update) {
77 chanctx_conf = rcu_dereference(sta->sdata->vif.chanctx_conf);
78 if (WARN_ON(!chanctx_conf)) {
83 spin_lock_bh(&sta->rate_ctrl_lock);
84 ref->ops->rate_update(ref->priv, sband, &chanctx_conf->def,
85 ista, priv_sta, changed);
86 spin_unlock_bh(&sta->rate_ctrl_lock);
89 drv_sta_rc_update(local, sta->sdata, &sta->sta, changed);
92 int ieee80211_rate_control_register(const struct rate_control_ops *ops)
94 struct rate_control_alg *alg;
99 mutex_lock(&rate_ctrl_mutex);
100 list_for_each_entry(alg, &rate_ctrl_algs, list) {
101 if (!strcmp(alg->ops->name, ops->name)) {
102 /* don't register an algorithm twice */
104 mutex_unlock(&rate_ctrl_mutex);
109 alg = kzalloc(sizeof(*alg), GFP_KERNEL);
111 mutex_unlock(&rate_ctrl_mutex);
116 list_add_tail(&alg->list, &rate_ctrl_algs);
117 mutex_unlock(&rate_ctrl_mutex);
121 EXPORT_SYMBOL(ieee80211_rate_control_register);
123 void ieee80211_rate_control_unregister(const struct rate_control_ops *ops)
125 struct rate_control_alg *alg;
127 mutex_lock(&rate_ctrl_mutex);
128 list_for_each_entry(alg, &rate_ctrl_algs, list) {
129 if (alg->ops == ops) {
130 list_del(&alg->list);
135 mutex_unlock(&rate_ctrl_mutex);
137 EXPORT_SYMBOL(ieee80211_rate_control_unregister);
139 static const struct rate_control_ops *
140 ieee80211_try_rate_control_ops_get(const char *name)
142 struct rate_control_alg *alg;
143 const struct rate_control_ops *ops = NULL;
148 mutex_lock(&rate_ctrl_mutex);
149 list_for_each_entry(alg, &rate_ctrl_algs, list) {
150 if (!strcmp(alg->ops->name, name)) {
155 mutex_unlock(&rate_ctrl_mutex);
159 /* Get the rate control algorithm. */
160 static const struct rate_control_ops *
161 ieee80211_rate_control_ops_get(const char *name)
163 const struct rate_control_ops *ops;
164 const char *alg_name;
166 kernel_param_lock(THIS_MODULE);
168 alg_name = ieee80211_default_rc_algo;
172 ops = ieee80211_try_rate_control_ops_get(alg_name);
174 /* try default if specific alg requested but not found */
175 ops = ieee80211_try_rate_control_ops_get(ieee80211_default_rc_algo);
177 /* try built-in one if specific alg requested but not found */
178 if (!ops && strlen(CONFIG_MAC80211_RC_DEFAULT))
179 ops = ieee80211_try_rate_control_ops_get(CONFIG_MAC80211_RC_DEFAULT);
180 kernel_param_unlock(THIS_MODULE);
185 #ifdef CONFIG_MAC80211_DEBUGFS
186 static ssize_t rcname_read(struct file *file, char __user *userbuf,
187 size_t count, loff_t *ppos)
189 struct rate_control_ref *ref = file->private_data;
190 int len = strlen(ref->ops->name);
192 return simple_read_from_buffer(userbuf, count, ppos,
193 ref->ops->name, len);
196 static const struct file_operations rcname_ops = {
199 .llseek = default_llseek,
203 static struct rate_control_ref *rate_control_alloc(const char *name,
204 struct ieee80211_local *local)
206 struct dentry *debugfsdir = NULL;
207 struct rate_control_ref *ref;
209 ref = kmalloc(sizeof(struct rate_control_ref), GFP_KERNEL);
212 ref->ops = ieee80211_rate_control_ops_get(name);
216 #ifdef CONFIG_MAC80211_DEBUGFS
217 debugfsdir = debugfs_create_dir("rc", local->hw.wiphy->debugfsdir);
218 local->debugfs.rcdir = debugfsdir;
219 debugfs_create_file("name", 0400, debugfsdir, ref, &rcname_ops);
222 ref->priv = ref->ops->alloc(&local->hw, debugfsdir);
232 static void rate_control_free(struct ieee80211_local *local,
233 struct rate_control_ref *ctrl_ref)
235 ctrl_ref->ops->free(ctrl_ref->priv);
237 #ifdef CONFIG_MAC80211_DEBUGFS
238 debugfs_remove_recursive(local->debugfs.rcdir);
239 local->debugfs.rcdir = NULL;
245 void ieee80211_check_rate_mask(struct ieee80211_sub_if_data *sdata)
247 struct ieee80211_local *local = sdata->local;
248 struct ieee80211_supported_band *sband;
249 u32 user_mask, basic_rates = sdata->vif.bss_conf.basic_rates;
250 enum nl80211_band band;
252 if (WARN_ON(!sdata->vif.bss_conf.chandef.chan))
255 if (WARN_ON_ONCE(!basic_rates))
258 band = sdata->vif.bss_conf.chandef.chan->band;
259 user_mask = sdata->rc_rateidx_mask[band];
260 sband = local->hw.wiphy->bands[band];
262 if (user_mask & basic_rates)
266 "no overlap between basic rates (0x%x) and user mask (0x%x on band %d) - clearing the latter",
267 basic_rates, user_mask, band);
268 sdata->rc_rateidx_mask[band] = (1 << sband->n_bitrates) - 1;
271 static bool rc_no_data_or_no_ack_use_min(struct ieee80211_tx_rate_control *txrc)
273 struct sk_buff *skb = txrc->skb;
274 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
275 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
278 fc = hdr->frame_control;
280 return (info->flags & (IEEE80211_TX_CTL_NO_ACK |
281 IEEE80211_TX_CTL_USE_MINRATE)) ||
282 !ieee80211_is_data(fc);
285 static void rc_send_low_basicrate(s8 *idx, u32 basic_rates,
286 struct ieee80211_supported_band *sband)
290 if (basic_rates == 0)
291 return; /* assume basic rates unknown and accept rate */
294 if (basic_rates & (1 << *idx))
295 return; /* selected rate is a basic rate */
297 for (i = *idx + 1; i <= sband->n_bitrates; i++) {
298 if (basic_rates & (1 << i)) {
304 /* could not find a basic rate; use original selection */
307 static void __rate_control_send_low(struct ieee80211_hw *hw,
308 struct ieee80211_supported_band *sband,
309 struct ieee80211_sta *sta,
310 struct ieee80211_tx_info *info,
315 ieee80211_chandef_rate_flags(&hw->conf.chandef);
317 if ((sband->band == NL80211_BAND_2GHZ) &&
318 (info->flags & IEEE80211_TX_CTL_NO_CCK_RATE))
319 rate_flags |= IEEE80211_RATE_ERP_G;
321 info->control.rates[0].idx = 0;
322 for (i = 0; i < sband->n_bitrates; i++) {
323 if (!(rate_mask & BIT(i)))
326 if ((rate_flags & sband->bitrates[i].flags) != rate_flags)
329 if (!rate_supported(sta, sband->band, i))
332 info->control.rates[0].idx = i;
335 WARN_ONCE(i == sband->n_bitrates,
336 "no supported rates (0x%x) in rate_mask 0x%x with flags 0x%x\n",
337 sta ? sta->supp_rates[sband->band] : -1,
338 rate_mask, rate_flags);
340 info->control.rates[0].count =
341 (info->flags & IEEE80211_TX_CTL_NO_ACK) ?
342 1 : hw->max_rate_tries;
344 info->control.skip_table = 1;
348 bool rate_control_send_low(struct ieee80211_sta *pubsta,
350 struct ieee80211_tx_rate_control *txrc)
352 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(txrc->skb);
353 struct ieee80211_supported_band *sband = txrc->sband;
354 struct sta_info *sta;
356 bool use_basicrate = false;
358 if (!pubsta || !priv_sta || rc_no_data_or_no_ack_use_min(txrc)) {
359 __rate_control_send_low(txrc->hw, sband, pubsta, info,
360 txrc->rate_idx_mask);
362 if (!pubsta && txrc->bss) {
363 mcast_rate = txrc->bss_conf->mcast_rate[sband->band];
364 if (mcast_rate > 0) {
365 info->control.rates[0].idx = mcast_rate - 1;
368 use_basicrate = true;
370 sta = container_of(pubsta, struct sta_info, sta);
371 if (ieee80211_vif_is_mesh(&sta->sdata->vif))
372 use_basicrate = true;
376 rc_send_low_basicrate(&info->control.rates[0].idx,
377 txrc->bss_conf->basic_rates,
384 EXPORT_SYMBOL(rate_control_send_low);
386 static bool rate_idx_match_legacy_mask(s8 *rate_idx, int n_bitrates, u32 mask)
390 /* See whether the selected rate or anything below it is allowed. */
391 for (j = *rate_idx; j >= 0; j--) {
392 if (mask & (1 << j)) {
393 /* Okay, found a suitable rate. Use it. */
399 /* Try to find a higher rate that would be allowed */
400 for (j = *rate_idx + 1; j < n_bitrates; j++) {
401 if (mask & (1 << j)) {
402 /* Okay, found a suitable rate. Use it. */
410 static bool rate_idx_match_mcs_mask(s8 *rate_idx, u8 *mcs_mask)
415 ridx = *rate_idx / 8;
416 rbit = *rate_idx % 8;
419 if (ridx < 0 || ridx >= IEEE80211_HT_MCS_MASK_LEN)
422 /* See whether the selected rate or anything below it is allowed. */
423 for (i = ridx; i >= 0; i--) {
424 for (j = rbit; j >= 0; j--)
425 if (mcs_mask[i] & BIT(j)) {
426 *rate_idx = i * 8 + j;
432 /* Try to find a higher rate that would be allowed */
433 ridx = (*rate_idx + 1) / 8;
434 rbit = (*rate_idx + 1) % 8;
436 for (i = ridx; i < IEEE80211_HT_MCS_MASK_LEN; i++) {
437 for (j = rbit; j < 8; j++)
438 if (mcs_mask[i] & BIT(j)) {
439 *rate_idx = i * 8 + j;
447 static bool rate_idx_match_vht_mcs_mask(s8 *rate_idx, u16 *vht_mask)
452 ridx = *rate_idx >> 4;
453 rbit = *rate_idx & 0xf;
455 if (ridx < 0 || ridx >= NL80211_VHT_NSS_MAX)
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 (vht_mask[i] & BIT(j)) {
462 *rate_idx = (i << 4) | j;
469 /* Try to find a higher rate that would be allowed */
470 ridx = (*rate_idx + 1) >> 4;
471 rbit = (*rate_idx + 1) & 0xf;
473 for (i = ridx; i < NL80211_VHT_NSS_MAX; i++) {
474 for (j = rbit; j < 16; j++) {
475 if (vht_mask[i] & BIT(j)) {
476 *rate_idx = (i << 4) | j;
485 static void rate_idx_match_mask(s8 *rate_idx, u16 *rate_flags,
486 struct ieee80211_supported_band *sband,
487 enum nl80211_chan_width chan_width,
489 u8 mcs_mask[IEEE80211_HT_MCS_MASK_LEN],
490 u16 vht_mask[NL80211_VHT_NSS_MAX])
492 if (*rate_flags & IEEE80211_TX_RC_VHT_MCS) {
493 /* handle VHT rates */
494 if (rate_idx_match_vht_mcs_mask(rate_idx, vht_mask))
498 /* keep protection flags */
499 *rate_flags &= (IEEE80211_TX_RC_USE_RTS_CTS |
500 IEEE80211_TX_RC_USE_CTS_PROTECT |
501 IEEE80211_TX_RC_USE_SHORT_PREAMBLE);
503 *rate_flags |= IEEE80211_TX_RC_MCS;
504 if (chan_width == NL80211_CHAN_WIDTH_40)
505 *rate_flags |= IEEE80211_TX_RC_40_MHZ_WIDTH;
507 if (rate_idx_match_mcs_mask(rate_idx, mcs_mask))
510 /* also try the legacy rates. */
511 *rate_flags &= ~(IEEE80211_TX_RC_MCS |
512 IEEE80211_TX_RC_40_MHZ_WIDTH);
513 if (rate_idx_match_legacy_mask(rate_idx, sband->n_bitrates,
516 } else if (*rate_flags & IEEE80211_TX_RC_MCS) {
517 /* handle HT rates */
518 if (rate_idx_match_mcs_mask(rate_idx, mcs_mask))
521 /* also try the legacy rates. */
523 /* keep protection flags */
524 *rate_flags &= (IEEE80211_TX_RC_USE_RTS_CTS |
525 IEEE80211_TX_RC_USE_CTS_PROTECT |
526 IEEE80211_TX_RC_USE_SHORT_PREAMBLE);
527 if (rate_idx_match_legacy_mask(rate_idx, sband->n_bitrates,
531 /* handle legacy rates */
532 if (rate_idx_match_legacy_mask(rate_idx, sband->n_bitrates,
536 /* if HT BSS, and we handle a data frame, also try HT rates */
537 switch (chan_width) {
538 case NL80211_CHAN_WIDTH_20_NOHT:
539 case NL80211_CHAN_WIDTH_5:
540 case NL80211_CHAN_WIDTH_10:
547 /* keep protection flags */
548 *rate_flags &= (IEEE80211_TX_RC_USE_RTS_CTS |
549 IEEE80211_TX_RC_USE_CTS_PROTECT |
550 IEEE80211_TX_RC_USE_SHORT_PREAMBLE);
552 *rate_flags |= IEEE80211_TX_RC_MCS;
554 if (chan_width == NL80211_CHAN_WIDTH_40)
555 *rate_flags |= IEEE80211_TX_RC_40_MHZ_WIDTH;
557 if (rate_idx_match_mcs_mask(rate_idx, mcs_mask))
562 * Uh.. No suitable rate exists. This should not really happen with
563 * sane TX rate mask configurations. However, should someone manage to
564 * configure supported rates and TX rate mask in incompatible way,
565 * allow the frame to be transmitted with whatever the rate control
570 static void rate_fixup_ratelist(struct ieee80211_vif *vif,
571 struct ieee80211_supported_band *sband,
572 struct ieee80211_tx_info *info,
573 struct ieee80211_tx_rate *rates,
576 struct ieee80211_rate *rate;
581 * Set up the RTS/CTS rate as the fastest basic rate
582 * that is not faster than the data rate unless there
583 * is no basic rate slower than the data rate, in which
584 * case we pick the slowest basic rate
586 * XXX: Should this check all retry rates?
588 if (!(rates[0].flags &
589 (IEEE80211_TX_RC_MCS | IEEE80211_TX_RC_VHT_MCS))) {
590 u32 basic_rates = vif->bss_conf.basic_rates;
591 s8 baserate = basic_rates ? ffs(basic_rates) - 1 : 0;
593 rate = &sband->bitrates[rates[0].idx];
595 for (i = 0; i < sband->n_bitrates; i++) {
596 /* must be a basic rate */
597 if (!(basic_rates & BIT(i)))
599 /* must not be faster than the data rate */
600 if (sband->bitrates[i].bitrate > rate->bitrate)
603 if (sband->bitrates[baserate].bitrate <
604 sband->bitrates[i].bitrate)
608 info->control.rts_cts_rate_idx = baserate;
611 for (i = 0; i < max_rates; i++) {
613 * make sure there's no valid rate following
614 * an invalid one, just in case drivers don't
615 * take the API seriously to stop at -1.
621 if (rates[i].idx < 0) {
627 * For now assume MCS is already set up correctly, this
630 if (rates[i].flags & IEEE80211_TX_RC_MCS) {
631 WARN_ON(rates[i].idx > 76);
633 if (!(rates[i].flags & IEEE80211_TX_RC_USE_RTS_CTS) &&
634 info->control.use_cts_prot)
636 IEEE80211_TX_RC_USE_CTS_PROTECT;
640 if (rates[i].flags & IEEE80211_TX_RC_VHT_MCS) {
641 WARN_ON(ieee80211_rate_get_vht_mcs(&rates[i]) > 9);
645 /* set up RTS protection if desired */
646 if (info->control.use_rts) {
647 rates[i].flags |= IEEE80211_TX_RC_USE_RTS_CTS;
648 info->control.use_cts_prot = false;
652 if (WARN_ON_ONCE(rates[i].idx >= sband->n_bitrates)) {
657 rate = &sband->bitrates[rates[i].idx];
659 /* set up short preamble */
660 if (info->control.short_preamble &&
661 rate->flags & IEEE80211_RATE_SHORT_PREAMBLE)
662 rates[i].flags |= IEEE80211_TX_RC_USE_SHORT_PREAMBLE;
664 /* set up G protection */
665 if (!(rates[i].flags & IEEE80211_TX_RC_USE_RTS_CTS) &&
666 info->control.use_cts_prot &&
667 rate->flags & IEEE80211_RATE_ERP_G)
668 rates[i].flags |= IEEE80211_TX_RC_USE_CTS_PROTECT;
673 static void rate_control_fill_sta_table(struct ieee80211_sta *sta,
674 struct ieee80211_tx_info *info,
675 struct ieee80211_tx_rate *rates,
678 struct ieee80211_sta_rates *ratetbl = NULL;
681 if (sta && !info->control.skip_table)
682 ratetbl = rcu_dereference(sta->rates);
684 /* Fill remaining rate slots with data from the sta rate table. */
685 max_rates = min_t(int, max_rates, IEEE80211_TX_RATE_TABLE_SIZE);
686 for (i = 0; i < max_rates; i++) {
687 if (i < ARRAY_SIZE(info->control.rates) &&
688 info->control.rates[i].idx >= 0 &&
689 info->control.rates[i].count) {
690 if (rates != info->control.rates)
691 rates[i] = info->control.rates[i];
692 } else if (ratetbl) {
693 rates[i].idx = ratetbl->rate[i].idx;
694 rates[i].flags = ratetbl->rate[i].flags;
695 if (info->control.use_rts)
696 rates[i].count = ratetbl->rate[i].count_rts;
697 else if (info->control.use_cts_prot)
698 rates[i].count = ratetbl->rate[i].count_cts;
700 rates[i].count = ratetbl->rate[i].count;
706 if (rates[i].idx < 0 || !rates[i].count)
711 static bool rate_control_cap_mask(struct ieee80211_sub_if_data *sdata,
712 struct ieee80211_supported_band *sband,
713 struct ieee80211_sta *sta, u32 *mask,
714 u8 mcs_mask[IEEE80211_HT_MCS_MASK_LEN],
715 u16 vht_mask[NL80211_VHT_NSS_MAX])
719 *mask = sdata->rc_rateidx_mask[sband->band];
720 flags = ieee80211_chandef_rate_flags(&sdata->vif.bss_conf.chandef);
721 for (i = 0; i < sband->n_bitrates; i++) {
722 if ((flags & sband->bitrates[i].flags) != flags)
726 if (*mask == (1 << sband->n_bitrates) - 1 &&
727 !sdata->rc_has_mcs_mask[sband->band] &&
728 !sdata->rc_has_vht_mcs_mask[sband->band])
731 if (sdata->rc_has_mcs_mask[sband->band])
732 memcpy(mcs_mask, sdata->rc_rateidx_mcs_mask[sband->band],
733 IEEE80211_HT_MCS_MASK_LEN);
735 memset(mcs_mask, 0xff, IEEE80211_HT_MCS_MASK_LEN);
737 if (sdata->rc_has_vht_mcs_mask[sband->band])
738 memcpy(vht_mask, sdata->rc_rateidx_vht_mcs_mask[sband->band],
739 sizeof(u16) * NL80211_VHT_NSS_MAX);
741 memset(vht_mask, 0xff, sizeof(u16) * NL80211_VHT_NSS_MAX);
745 u16 sta_vht_mask[NL80211_VHT_NSS_MAX];
747 /* Filter out rates that the STA does not support */
748 *mask &= sta->supp_rates[sband->band];
749 for (i = 0; i < IEEE80211_HT_MCS_MASK_LEN; i++)
750 mcs_mask[i] &= sta->ht_cap.mcs.rx_mask[i];
752 sta_vht_cap = sta->vht_cap.vht_mcs.rx_mcs_map;
753 ieee80211_get_vht_mask_from_cap(sta_vht_cap, sta_vht_mask);
754 for (i = 0; i < NL80211_VHT_NSS_MAX; i++)
755 vht_mask[i] &= sta_vht_mask[i];
762 rate_control_apply_mask_ratetbl(struct sta_info *sta,
763 struct ieee80211_supported_band *sband,
764 struct ieee80211_sta_rates *rates)
768 u8 mcs_mask[IEEE80211_HT_MCS_MASK_LEN];
769 u16 vht_mask[NL80211_VHT_NSS_MAX];
770 enum nl80211_chan_width chan_width;
772 if (!rate_control_cap_mask(sta->sdata, sband, &sta->sta, &mask,
776 chan_width = sta->sdata->vif.bss_conf.chandef.width;
777 for (i = 0; i < IEEE80211_TX_RATE_TABLE_SIZE; i++) {
778 if (rates->rate[i].idx < 0)
781 rate_idx_match_mask(&rates->rate[i].idx, &rates->rate[i].flags,
782 sband, chan_width, mask, mcs_mask,
787 static void rate_control_apply_mask(struct ieee80211_sub_if_data *sdata,
788 struct ieee80211_sta *sta,
789 struct ieee80211_supported_band *sband,
790 struct ieee80211_tx_rate *rates,
793 enum nl80211_chan_width chan_width;
794 u8 mcs_mask[IEEE80211_HT_MCS_MASK_LEN];
796 u16 rate_flags, vht_mask[NL80211_VHT_NSS_MAX];
800 * Try to enforce the rateidx mask the user wanted. skip this if the
801 * default mask (allow all rates) is used to save some processing for
804 if (!rate_control_cap_mask(sdata, sband, sta, &mask, mcs_mask,
809 * Make sure the rate index selected for each TX rate is
810 * included in the configured mask and change the rate indexes
813 chan_width = sdata->vif.bss_conf.chandef.width;
814 for (i = 0; i < max_rates; i++) {
815 /* Skip invalid rates */
816 if (rates[i].idx < 0)
819 rate_flags = rates[i].flags;
820 rate_idx_match_mask(&rates[i].idx, &rate_flags, sband,
821 chan_width, mask, mcs_mask, vht_mask);
822 rates[i].flags = rate_flags;
826 void ieee80211_get_tx_rates(struct ieee80211_vif *vif,
827 struct ieee80211_sta *sta,
829 struct ieee80211_tx_rate *dest,
832 struct ieee80211_sub_if_data *sdata;
833 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
834 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
835 struct ieee80211_supported_band *sband;
837 rate_control_fill_sta_table(sta, info, dest, max_rates);
842 sdata = vif_to_sdata(vif);
843 sband = sdata->local->hw.wiphy->bands[info->band];
845 if (ieee80211_is_data(hdr->frame_control))
846 rate_control_apply_mask(sdata, sta, sband, dest, max_rates);
849 __rate_control_send_low(&sdata->local->hw, sband, sta, info,
850 sdata->rc_rateidx_mask[info->band]);
853 rate_fixup_ratelist(vif, sband, info, dest, max_rates);
855 EXPORT_SYMBOL(ieee80211_get_tx_rates);
857 void rate_control_get_rate(struct ieee80211_sub_if_data *sdata,
858 struct sta_info *sta,
859 struct ieee80211_tx_rate_control *txrc)
861 struct rate_control_ref *ref = sdata->local->rate_ctrl;
862 void *priv_sta = NULL;
863 struct ieee80211_sta *ista = NULL;
864 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(txrc->skb);
867 if (sta && test_sta_flag(sta, WLAN_STA_RATE_CONTROL)) {
869 priv_sta = sta->rate_ctrl_priv;
872 for (i = 0; i < IEEE80211_TX_MAX_RATES; i++) {
873 info->control.rates[i].idx = -1;
874 info->control.rates[i].flags = 0;
875 info->control.rates[i].count = 0;
878 if (ieee80211_hw_check(&sdata->local->hw, HAS_RATE_CONTROL))
882 spin_lock_bh(&sta->rate_ctrl_lock);
883 ref->ops->get_rate(ref->priv, ista, priv_sta, txrc);
884 spin_unlock_bh(&sta->rate_ctrl_lock);
886 ref->ops->get_rate(ref->priv, NULL, NULL, txrc);
889 if (ieee80211_hw_check(&sdata->local->hw, SUPPORTS_RC_TABLE))
892 ieee80211_get_tx_rates(&sdata->vif, ista, txrc->skb,
894 ARRAY_SIZE(info->control.rates));
897 int rate_control_set_rates(struct ieee80211_hw *hw,
898 struct ieee80211_sta *pubsta,
899 struct ieee80211_sta_rates *rates)
901 struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
902 struct ieee80211_sta_rates *old;
903 struct ieee80211_supported_band *sband;
905 sband = hw->wiphy->bands[ieee80211_get_sdata_band(sta->sdata)];
906 rate_control_apply_mask_ratetbl(sta, sband, rates);
908 * mac80211 guarantees that this function will not be called
909 * concurrently, so the following RCU access is safe, even without
910 * extra locking. This can not be checked easily, so we just set
911 * the condition to true.
913 old = rcu_dereference_protected(pubsta->rates, true);
914 rcu_assign_pointer(pubsta->rates, rates);
916 kfree_rcu(old, rcu_head);
918 drv_sta_rate_tbl_update(hw_to_local(hw), sta->sdata, pubsta);
922 EXPORT_SYMBOL(rate_control_set_rates);
924 int ieee80211_init_rate_ctrl_alg(struct ieee80211_local *local,
927 struct rate_control_ref *ref;
931 if (local->open_count)
934 if (ieee80211_hw_check(&local->hw, HAS_RATE_CONTROL)) {
935 if (WARN_ON(!local->ops->set_rts_threshold))
940 ref = rate_control_alloc(name, local);
942 wiphy_warn(local->hw.wiphy,
943 "Failed to select rate control algorithm\n");
947 WARN_ON(local->rate_ctrl);
948 local->rate_ctrl = ref;
950 wiphy_debug(local->hw.wiphy, "Selected rate control algorithm '%s'\n",
956 void rate_control_deinitialize(struct ieee80211_local *local)
958 struct rate_control_ref *ref;
960 ref = local->rate_ctrl;
965 local->rate_ctrl = NULL;
966 rate_control_free(local, ref);