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 /* Note: check for > 0 is intentional to avoid clang warning */
178 if (!ops && (strlen(CONFIG_MAC80211_RC_DEFAULT) > 0))
179 /* try built-in one if specific alg requested but not found */
180 ops = ieee80211_try_rate_control_ops_get(CONFIG_MAC80211_RC_DEFAULT);
182 kernel_param_unlock(THIS_MODULE);
187 #ifdef CONFIG_MAC80211_DEBUGFS
188 static ssize_t rcname_read(struct file *file, char __user *userbuf,
189 size_t count, loff_t *ppos)
191 struct rate_control_ref *ref = file->private_data;
192 int len = strlen(ref->ops->name);
194 return simple_read_from_buffer(userbuf, count, ppos,
195 ref->ops->name, len);
198 static const struct file_operations rcname_ops = {
201 .llseek = default_llseek,
205 static struct rate_control_ref *rate_control_alloc(const char *name,
206 struct ieee80211_local *local)
208 struct dentry *debugfsdir = NULL;
209 struct rate_control_ref *ref;
211 ref = kmalloc(sizeof(struct rate_control_ref), GFP_KERNEL);
214 ref->ops = ieee80211_rate_control_ops_get(name);
218 #ifdef CONFIG_MAC80211_DEBUGFS
219 debugfsdir = debugfs_create_dir("rc", local->hw.wiphy->debugfsdir);
220 local->debugfs.rcdir = debugfsdir;
221 debugfs_create_file("name", 0400, debugfsdir, ref, &rcname_ops);
224 ref->priv = ref->ops->alloc(&local->hw, debugfsdir);
234 static void rate_control_free(struct ieee80211_local *local,
235 struct rate_control_ref *ctrl_ref)
237 ctrl_ref->ops->free(ctrl_ref->priv);
239 #ifdef CONFIG_MAC80211_DEBUGFS
240 debugfs_remove_recursive(local->debugfs.rcdir);
241 local->debugfs.rcdir = NULL;
247 void ieee80211_check_rate_mask(struct ieee80211_sub_if_data *sdata)
249 struct ieee80211_local *local = sdata->local;
250 struct ieee80211_supported_band *sband;
251 u32 user_mask, basic_rates = sdata->vif.bss_conf.basic_rates;
252 enum nl80211_band band;
254 if (WARN_ON(!sdata->vif.bss_conf.chandef.chan))
257 if (WARN_ON_ONCE(!basic_rates))
260 band = sdata->vif.bss_conf.chandef.chan->band;
261 user_mask = sdata->rc_rateidx_mask[band];
262 sband = local->hw.wiphy->bands[band];
264 if (user_mask & basic_rates)
268 "no overlap between basic rates (0x%x) and user mask (0x%x on band %d) - clearing the latter",
269 basic_rates, user_mask, band);
270 sdata->rc_rateidx_mask[band] = (1 << sband->n_bitrates) - 1;
273 static bool rc_no_data_or_no_ack_use_min(struct ieee80211_tx_rate_control *txrc)
275 struct sk_buff *skb = txrc->skb;
276 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
277 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
280 fc = hdr->frame_control;
282 return (info->flags & (IEEE80211_TX_CTL_NO_ACK |
283 IEEE80211_TX_CTL_USE_MINRATE)) ||
284 !ieee80211_is_data(fc);
287 static void rc_send_low_basicrate(s8 *idx, u32 basic_rates,
288 struct ieee80211_supported_band *sband)
292 if (basic_rates == 0)
293 return; /* assume basic rates unknown and accept rate */
296 if (basic_rates & (1 << *idx))
297 return; /* selected rate is a basic rate */
299 for (i = *idx + 1; i <= sband->n_bitrates; i++) {
300 if (basic_rates & (1 << i)) {
306 /* could not find a basic rate; use original selection */
309 static void __rate_control_send_low(struct ieee80211_hw *hw,
310 struct ieee80211_supported_band *sband,
311 struct ieee80211_sta *sta,
312 struct ieee80211_tx_info *info,
317 ieee80211_chandef_rate_flags(&hw->conf.chandef);
319 if ((sband->band == NL80211_BAND_2GHZ) &&
320 (info->flags & IEEE80211_TX_CTL_NO_CCK_RATE))
321 rate_flags |= IEEE80211_RATE_ERP_G;
323 info->control.rates[0].idx = 0;
324 for (i = 0; i < sband->n_bitrates; i++) {
325 if (!(rate_mask & BIT(i)))
328 if ((rate_flags & sband->bitrates[i].flags) != rate_flags)
331 if (!rate_supported(sta, sband->band, i))
334 info->control.rates[0].idx = i;
337 WARN_ONCE(i == sband->n_bitrates,
338 "no supported rates (0x%x) in rate_mask 0x%x with flags 0x%x\n",
339 sta ? sta->supp_rates[sband->band] : -1,
340 rate_mask, rate_flags);
342 info->control.rates[0].count =
343 (info->flags & IEEE80211_TX_CTL_NO_ACK) ?
344 1 : hw->max_rate_tries;
346 info->control.skip_table = 1;
350 bool rate_control_send_low(struct ieee80211_sta *pubsta,
352 struct ieee80211_tx_rate_control *txrc)
354 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(txrc->skb);
355 struct ieee80211_supported_band *sband = txrc->sband;
356 struct sta_info *sta;
358 bool use_basicrate = false;
360 if (!pubsta || !priv_sta || rc_no_data_or_no_ack_use_min(txrc)) {
361 __rate_control_send_low(txrc->hw, sband, pubsta, info,
362 txrc->rate_idx_mask);
364 if (!pubsta && txrc->bss) {
365 mcast_rate = txrc->bss_conf->mcast_rate[sband->band];
366 if (mcast_rate > 0) {
367 info->control.rates[0].idx = mcast_rate - 1;
370 use_basicrate = true;
372 sta = container_of(pubsta, struct sta_info, sta);
373 if (ieee80211_vif_is_mesh(&sta->sdata->vif))
374 use_basicrate = true;
378 rc_send_low_basicrate(&info->control.rates[0].idx,
379 txrc->bss_conf->basic_rates,
386 EXPORT_SYMBOL(rate_control_send_low);
388 static bool rate_idx_match_legacy_mask(s8 *rate_idx, int n_bitrates, u32 mask)
392 /* See whether the selected rate or anything below it is allowed. */
393 for (j = *rate_idx; j >= 0; j--) {
394 if (mask & (1 << j)) {
395 /* Okay, found a suitable rate. Use it. */
401 /* Try to find a higher rate that would be allowed */
402 for (j = *rate_idx + 1; j < n_bitrates; j++) {
403 if (mask & (1 << j)) {
404 /* Okay, found a suitable rate. Use it. */
412 static bool rate_idx_match_mcs_mask(s8 *rate_idx, u8 *mcs_mask)
417 ridx = *rate_idx / 8;
418 rbit = *rate_idx % 8;
421 if (ridx < 0 || ridx >= IEEE80211_HT_MCS_MASK_LEN)
424 /* See whether the selected rate or anything below it is allowed. */
425 for (i = ridx; i >= 0; i--) {
426 for (j = rbit; j >= 0; j--)
427 if (mcs_mask[i] & BIT(j)) {
428 *rate_idx = i * 8 + j;
434 /* Try to find a higher rate that would be allowed */
435 ridx = (*rate_idx + 1) / 8;
436 rbit = (*rate_idx + 1) % 8;
438 for (i = ridx; i < IEEE80211_HT_MCS_MASK_LEN; i++) {
439 for (j = rbit; j < 8; j++)
440 if (mcs_mask[i] & BIT(j)) {
441 *rate_idx = i * 8 + j;
449 static bool rate_idx_match_vht_mcs_mask(s8 *rate_idx, u16 *vht_mask)
454 ridx = *rate_idx >> 4;
455 rbit = *rate_idx & 0xf;
457 if (ridx < 0 || ridx >= NL80211_VHT_NSS_MAX)
460 /* See whether the selected rate or anything below it is allowed. */
461 for (i = ridx; i >= 0; i--) {
462 for (j = rbit; j >= 0; j--) {
463 if (vht_mask[i] & BIT(j)) {
464 *rate_idx = (i << 4) | j;
471 /* Try to find a higher rate that would be allowed */
472 ridx = (*rate_idx + 1) >> 4;
473 rbit = (*rate_idx + 1) & 0xf;
475 for (i = ridx; i < NL80211_VHT_NSS_MAX; i++) {
476 for (j = rbit; j < 16; j++) {
477 if (vht_mask[i] & BIT(j)) {
478 *rate_idx = (i << 4) | j;
487 static void rate_idx_match_mask(s8 *rate_idx, u16 *rate_flags,
488 struct ieee80211_supported_band *sband,
489 enum nl80211_chan_width chan_width,
491 u8 mcs_mask[IEEE80211_HT_MCS_MASK_LEN],
492 u16 vht_mask[NL80211_VHT_NSS_MAX])
494 if (*rate_flags & IEEE80211_TX_RC_VHT_MCS) {
495 /* handle VHT rates */
496 if (rate_idx_match_vht_mcs_mask(rate_idx, vht_mask))
500 /* keep protection flags */
501 *rate_flags &= (IEEE80211_TX_RC_USE_RTS_CTS |
502 IEEE80211_TX_RC_USE_CTS_PROTECT |
503 IEEE80211_TX_RC_USE_SHORT_PREAMBLE);
505 *rate_flags |= IEEE80211_TX_RC_MCS;
506 if (chan_width == NL80211_CHAN_WIDTH_40)
507 *rate_flags |= IEEE80211_TX_RC_40_MHZ_WIDTH;
509 if (rate_idx_match_mcs_mask(rate_idx, mcs_mask))
512 /* also try the legacy rates. */
513 *rate_flags &= ~(IEEE80211_TX_RC_MCS |
514 IEEE80211_TX_RC_40_MHZ_WIDTH);
515 if (rate_idx_match_legacy_mask(rate_idx, sband->n_bitrates,
518 } else if (*rate_flags & IEEE80211_TX_RC_MCS) {
519 /* handle HT rates */
520 if (rate_idx_match_mcs_mask(rate_idx, mcs_mask))
523 /* also try the legacy rates. */
525 /* keep protection flags */
526 *rate_flags &= (IEEE80211_TX_RC_USE_RTS_CTS |
527 IEEE80211_TX_RC_USE_CTS_PROTECT |
528 IEEE80211_TX_RC_USE_SHORT_PREAMBLE);
529 if (rate_idx_match_legacy_mask(rate_idx, sband->n_bitrates,
533 /* handle legacy rates */
534 if (rate_idx_match_legacy_mask(rate_idx, sband->n_bitrates,
538 /* if HT BSS, and we handle a data frame, also try HT rates */
539 switch (chan_width) {
540 case NL80211_CHAN_WIDTH_20_NOHT:
541 case NL80211_CHAN_WIDTH_5:
542 case NL80211_CHAN_WIDTH_10:
549 /* keep protection flags */
550 *rate_flags &= (IEEE80211_TX_RC_USE_RTS_CTS |
551 IEEE80211_TX_RC_USE_CTS_PROTECT |
552 IEEE80211_TX_RC_USE_SHORT_PREAMBLE);
554 *rate_flags |= IEEE80211_TX_RC_MCS;
556 if (chan_width == NL80211_CHAN_WIDTH_40)
557 *rate_flags |= IEEE80211_TX_RC_40_MHZ_WIDTH;
559 if (rate_idx_match_mcs_mask(rate_idx, mcs_mask))
564 * Uh.. No suitable rate exists. This should not really happen with
565 * sane TX rate mask configurations. However, should someone manage to
566 * configure supported rates and TX rate mask in incompatible way,
567 * allow the frame to be transmitted with whatever the rate control
572 static void rate_fixup_ratelist(struct ieee80211_vif *vif,
573 struct ieee80211_supported_band *sband,
574 struct ieee80211_tx_info *info,
575 struct ieee80211_tx_rate *rates,
578 struct ieee80211_rate *rate;
583 * Set up the RTS/CTS rate as the fastest basic rate
584 * that is not faster than the data rate unless there
585 * is no basic rate slower than the data rate, in which
586 * case we pick the slowest basic rate
588 * XXX: Should this check all retry rates?
590 if (!(rates[0].flags &
591 (IEEE80211_TX_RC_MCS | IEEE80211_TX_RC_VHT_MCS))) {
592 u32 basic_rates = vif->bss_conf.basic_rates;
593 s8 baserate = basic_rates ? ffs(basic_rates) - 1 : 0;
595 rate = &sband->bitrates[rates[0].idx];
597 for (i = 0; i < sband->n_bitrates; i++) {
598 /* must be a basic rate */
599 if (!(basic_rates & BIT(i)))
601 /* must not be faster than the data rate */
602 if (sband->bitrates[i].bitrate > rate->bitrate)
605 if (sband->bitrates[baserate].bitrate <
606 sband->bitrates[i].bitrate)
610 info->control.rts_cts_rate_idx = baserate;
613 for (i = 0; i < max_rates; i++) {
615 * make sure there's no valid rate following
616 * an invalid one, just in case drivers don't
617 * take the API seriously to stop at -1.
623 if (rates[i].idx < 0) {
629 * For now assume MCS is already set up correctly, this
632 if (rates[i].flags & IEEE80211_TX_RC_MCS) {
633 WARN_ON(rates[i].idx > 76);
635 if (!(rates[i].flags & IEEE80211_TX_RC_USE_RTS_CTS) &&
636 info->control.use_cts_prot)
638 IEEE80211_TX_RC_USE_CTS_PROTECT;
642 if (rates[i].flags & IEEE80211_TX_RC_VHT_MCS) {
643 WARN_ON(ieee80211_rate_get_vht_mcs(&rates[i]) > 9);
647 /* set up RTS protection if desired */
648 if (info->control.use_rts) {
649 rates[i].flags |= IEEE80211_TX_RC_USE_RTS_CTS;
650 info->control.use_cts_prot = false;
654 if (WARN_ON_ONCE(rates[i].idx >= sband->n_bitrates)) {
659 rate = &sband->bitrates[rates[i].idx];
661 /* set up short preamble */
662 if (info->control.short_preamble &&
663 rate->flags & IEEE80211_RATE_SHORT_PREAMBLE)
664 rates[i].flags |= IEEE80211_TX_RC_USE_SHORT_PREAMBLE;
666 /* set up G protection */
667 if (!(rates[i].flags & IEEE80211_TX_RC_USE_RTS_CTS) &&
668 info->control.use_cts_prot &&
669 rate->flags & IEEE80211_RATE_ERP_G)
670 rates[i].flags |= IEEE80211_TX_RC_USE_CTS_PROTECT;
675 static void rate_control_fill_sta_table(struct ieee80211_sta *sta,
676 struct ieee80211_tx_info *info,
677 struct ieee80211_tx_rate *rates,
680 struct ieee80211_sta_rates *ratetbl = NULL;
683 if (sta && !info->control.skip_table)
684 ratetbl = rcu_dereference(sta->rates);
686 /* Fill remaining rate slots with data from the sta rate table. */
687 max_rates = min_t(int, max_rates, IEEE80211_TX_RATE_TABLE_SIZE);
688 for (i = 0; i < max_rates; i++) {
689 if (i < ARRAY_SIZE(info->control.rates) &&
690 info->control.rates[i].idx >= 0 &&
691 info->control.rates[i].count) {
692 if (rates != info->control.rates)
693 rates[i] = info->control.rates[i];
694 } else if (ratetbl) {
695 rates[i].idx = ratetbl->rate[i].idx;
696 rates[i].flags = ratetbl->rate[i].flags;
697 if (info->control.use_rts)
698 rates[i].count = ratetbl->rate[i].count_rts;
699 else if (info->control.use_cts_prot)
700 rates[i].count = ratetbl->rate[i].count_cts;
702 rates[i].count = ratetbl->rate[i].count;
708 if (rates[i].idx < 0 || !rates[i].count)
713 static bool rate_control_cap_mask(struct ieee80211_sub_if_data *sdata,
714 struct ieee80211_supported_band *sband,
715 struct ieee80211_sta *sta, u32 *mask,
716 u8 mcs_mask[IEEE80211_HT_MCS_MASK_LEN],
717 u16 vht_mask[NL80211_VHT_NSS_MAX])
721 *mask = sdata->rc_rateidx_mask[sband->band];
722 flags = ieee80211_chandef_rate_flags(&sdata->vif.bss_conf.chandef);
723 for (i = 0; i < sband->n_bitrates; i++) {
724 if ((flags & sband->bitrates[i].flags) != flags)
728 if (*mask == (1 << sband->n_bitrates) - 1 &&
729 !sdata->rc_has_mcs_mask[sband->band] &&
730 !sdata->rc_has_vht_mcs_mask[sband->band])
733 if (sdata->rc_has_mcs_mask[sband->band])
734 memcpy(mcs_mask, sdata->rc_rateidx_mcs_mask[sband->band],
735 IEEE80211_HT_MCS_MASK_LEN);
737 memset(mcs_mask, 0xff, IEEE80211_HT_MCS_MASK_LEN);
739 if (sdata->rc_has_vht_mcs_mask[sband->band])
740 memcpy(vht_mask, sdata->rc_rateidx_vht_mcs_mask[sband->band],
741 sizeof(u16) * NL80211_VHT_NSS_MAX);
743 memset(vht_mask, 0xff, sizeof(u16) * NL80211_VHT_NSS_MAX);
747 u16 sta_vht_mask[NL80211_VHT_NSS_MAX];
749 /* Filter out rates that the STA does not support */
750 *mask &= sta->supp_rates[sband->band];
751 for (i = 0; i < IEEE80211_HT_MCS_MASK_LEN; i++)
752 mcs_mask[i] &= sta->ht_cap.mcs.rx_mask[i];
754 sta_vht_cap = sta->vht_cap.vht_mcs.rx_mcs_map;
755 ieee80211_get_vht_mask_from_cap(sta_vht_cap, sta_vht_mask);
756 for (i = 0; i < NL80211_VHT_NSS_MAX; i++)
757 vht_mask[i] &= sta_vht_mask[i];
764 rate_control_apply_mask_ratetbl(struct sta_info *sta,
765 struct ieee80211_supported_band *sband,
766 struct ieee80211_sta_rates *rates)
770 u8 mcs_mask[IEEE80211_HT_MCS_MASK_LEN];
771 u16 vht_mask[NL80211_VHT_NSS_MAX];
772 enum nl80211_chan_width chan_width;
774 if (!rate_control_cap_mask(sta->sdata, sband, &sta->sta, &mask,
778 chan_width = sta->sdata->vif.bss_conf.chandef.width;
779 for (i = 0; i < IEEE80211_TX_RATE_TABLE_SIZE; i++) {
780 if (rates->rate[i].idx < 0)
783 rate_idx_match_mask(&rates->rate[i].idx, &rates->rate[i].flags,
784 sband, chan_width, mask, mcs_mask,
789 static void rate_control_apply_mask(struct ieee80211_sub_if_data *sdata,
790 struct ieee80211_sta *sta,
791 struct ieee80211_supported_band *sband,
792 struct ieee80211_tx_rate *rates,
795 enum nl80211_chan_width chan_width;
796 u8 mcs_mask[IEEE80211_HT_MCS_MASK_LEN];
798 u16 rate_flags, vht_mask[NL80211_VHT_NSS_MAX];
802 * Try to enforce the rateidx mask the user wanted. skip this if the
803 * default mask (allow all rates) is used to save some processing for
806 if (!rate_control_cap_mask(sdata, sband, sta, &mask, mcs_mask,
811 * Make sure the rate index selected for each TX rate is
812 * included in the configured mask and change the rate indexes
815 chan_width = sdata->vif.bss_conf.chandef.width;
816 for (i = 0; i < max_rates; i++) {
817 /* Skip invalid rates */
818 if (rates[i].idx < 0)
821 rate_flags = rates[i].flags;
822 rate_idx_match_mask(&rates[i].idx, &rate_flags, sband,
823 chan_width, mask, mcs_mask, vht_mask);
824 rates[i].flags = rate_flags;
828 void ieee80211_get_tx_rates(struct ieee80211_vif *vif,
829 struct ieee80211_sta *sta,
831 struct ieee80211_tx_rate *dest,
834 struct ieee80211_sub_if_data *sdata;
835 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
836 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
837 struct ieee80211_supported_band *sband;
839 rate_control_fill_sta_table(sta, info, dest, max_rates);
844 sdata = vif_to_sdata(vif);
845 sband = sdata->local->hw.wiphy->bands[info->band];
847 if (ieee80211_is_data(hdr->frame_control))
848 rate_control_apply_mask(sdata, sta, sband, dest, max_rates);
851 __rate_control_send_low(&sdata->local->hw, sband, sta, info,
852 sdata->rc_rateidx_mask[info->band]);
855 rate_fixup_ratelist(vif, sband, info, dest, max_rates);
857 EXPORT_SYMBOL(ieee80211_get_tx_rates);
859 void rate_control_get_rate(struct ieee80211_sub_if_data *sdata,
860 struct sta_info *sta,
861 struct ieee80211_tx_rate_control *txrc)
863 struct rate_control_ref *ref = sdata->local->rate_ctrl;
864 void *priv_sta = NULL;
865 struct ieee80211_sta *ista = NULL;
866 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(txrc->skb);
869 if (sta && test_sta_flag(sta, WLAN_STA_RATE_CONTROL)) {
871 priv_sta = sta->rate_ctrl_priv;
874 for (i = 0; i < IEEE80211_TX_MAX_RATES; i++) {
875 info->control.rates[i].idx = -1;
876 info->control.rates[i].flags = 0;
877 info->control.rates[i].count = 0;
880 if (ieee80211_hw_check(&sdata->local->hw, HAS_RATE_CONTROL))
884 spin_lock_bh(&sta->rate_ctrl_lock);
885 ref->ops->get_rate(ref->priv, ista, priv_sta, txrc);
886 spin_unlock_bh(&sta->rate_ctrl_lock);
888 ref->ops->get_rate(ref->priv, NULL, NULL, txrc);
891 if (ieee80211_hw_check(&sdata->local->hw, SUPPORTS_RC_TABLE))
894 ieee80211_get_tx_rates(&sdata->vif, ista, txrc->skb,
896 ARRAY_SIZE(info->control.rates));
899 int rate_control_set_rates(struct ieee80211_hw *hw,
900 struct ieee80211_sta *pubsta,
901 struct ieee80211_sta_rates *rates)
903 struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
904 struct ieee80211_sta_rates *old;
905 struct ieee80211_supported_band *sband;
907 sband = hw->wiphy->bands[ieee80211_get_sdata_band(sta->sdata)];
908 rate_control_apply_mask_ratetbl(sta, sband, rates);
910 * mac80211 guarantees that this function will not be called
911 * concurrently, so the following RCU access is safe, even without
912 * extra locking. This can not be checked easily, so we just set
913 * the condition to true.
915 old = rcu_dereference_protected(pubsta->rates, true);
916 rcu_assign_pointer(pubsta->rates, rates);
918 kfree_rcu(old, rcu_head);
920 drv_sta_rate_tbl_update(hw_to_local(hw), sta->sdata, pubsta);
924 EXPORT_SYMBOL(rate_control_set_rates);
926 int ieee80211_init_rate_ctrl_alg(struct ieee80211_local *local,
929 struct rate_control_ref *ref;
933 if (local->open_count)
936 if (ieee80211_hw_check(&local->hw, HAS_RATE_CONTROL)) {
937 if (WARN_ON(!local->ops->set_rts_threshold))
942 ref = rate_control_alloc(name, local);
944 wiphy_warn(local->hw.wiphy,
945 "Failed to select rate control algorithm\n");
949 WARN_ON(local->rate_ctrl);
950 local->rate_ctrl = ref;
952 wiphy_debug(local->hw.wiphy, "Selected rate control algorithm '%s'\n",
958 void rate_control_deinitialize(struct ieee80211_local *local)
960 struct rate_control_ref *ref;
962 ref = local->rate_ctrl;
967 local->rate_ctrl = NULL;
968 rate_control_free(local, ref);