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 spin_lock_bh(&sta
->rate_ctrl_lock
);
55 ref
->ops
->rate_init(ref
->priv
, sband
, &chanctx_conf
->def
, ista
,
57 spin_unlock_bh(&sta
->rate_ctrl_lock
);
59 set_sta_flag(sta
, WLAN_STA_RATE_CONTROL
);
62 void rate_control_tx_status(struct ieee80211_local
*local
,
63 struct ieee80211_supported_band
*sband
,
64 struct ieee80211_tx_status
*st
)
66 struct rate_control_ref
*ref
= local
->rate_ctrl
;
67 struct sta_info
*sta
= container_of(st
->sta
, struct sta_info
, sta
);
68 void *priv_sta
= sta
->rate_ctrl_priv
;
70 if (!ref
|| !test_sta_flag(sta
, WLAN_STA_RATE_CONTROL
))
73 spin_lock_bh(&sta
->rate_ctrl_lock
);
74 if (ref
->ops
->tx_status_ext
)
75 ref
->ops
->tx_status_ext(ref
->priv
, sband
, priv_sta
, st
);
77 ref
->ops
->tx_status(ref
->priv
, sband
, st
->sta
, priv_sta
, st
->skb
);
81 spin_unlock_bh(&sta
->rate_ctrl_lock
);
84 void rate_control_rate_update(struct ieee80211_local
*local
,
85 struct ieee80211_supported_band
*sband
,
86 struct sta_info
*sta
, u32 changed
)
88 struct rate_control_ref
*ref
= local
->rate_ctrl
;
89 struct ieee80211_sta
*ista
= &sta
->sta
;
90 void *priv_sta
= sta
->rate_ctrl_priv
;
91 struct ieee80211_chanctx_conf
*chanctx_conf
;
93 if (ref
&& ref
->ops
->rate_update
) {
96 chanctx_conf
= rcu_dereference(sta
->sdata
->vif
.chanctx_conf
);
97 if (WARN_ON(!chanctx_conf
)) {
102 spin_lock_bh(&sta
->rate_ctrl_lock
);
103 ref
->ops
->rate_update(ref
->priv
, sband
, &chanctx_conf
->def
,
104 ista
, priv_sta
, changed
);
105 spin_unlock_bh(&sta
->rate_ctrl_lock
);
108 drv_sta_rc_update(local
, sta
->sdata
, &sta
->sta
, changed
);
111 int ieee80211_rate_control_register(const struct rate_control_ops
*ops
)
113 struct rate_control_alg
*alg
;
118 mutex_lock(&rate_ctrl_mutex
);
119 list_for_each_entry(alg
, &rate_ctrl_algs
, list
) {
120 if (!strcmp(alg
->ops
->name
, ops
->name
)) {
121 /* don't register an algorithm twice */
123 mutex_unlock(&rate_ctrl_mutex
);
128 alg
= kzalloc(sizeof(*alg
), GFP_KERNEL
);
130 mutex_unlock(&rate_ctrl_mutex
);
135 list_add_tail(&alg
->list
, &rate_ctrl_algs
);
136 mutex_unlock(&rate_ctrl_mutex
);
140 EXPORT_SYMBOL(ieee80211_rate_control_register
);
142 void ieee80211_rate_control_unregister(const struct rate_control_ops
*ops
)
144 struct rate_control_alg
*alg
;
146 mutex_lock(&rate_ctrl_mutex
);
147 list_for_each_entry(alg
, &rate_ctrl_algs
, list
) {
148 if (alg
->ops
== ops
) {
149 list_del(&alg
->list
);
154 mutex_unlock(&rate_ctrl_mutex
);
156 EXPORT_SYMBOL(ieee80211_rate_control_unregister
);
158 static const struct rate_control_ops
*
159 ieee80211_try_rate_control_ops_get(const char *name
)
161 struct rate_control_alg
*alg
;
162 const struct rate_control_ops
*ops
= NULL
;
167 mutex_lock(&rate_ctrl_mutex
);
168 list_for_each_entry(alg
, &rate_ctrl_algs
, list
) {
169 if (!strcmp(alg
->ops
->name
, name
)) {
174 mutex_unlock(&rate_ctrl_mutex
);
178 /* Get the rate control algorithm. */
179 static const struct rate_control_ops
*
180 ieee80211_rate_control_ops_get(const char *name
)
182 const struct rate_control_ops
*ops
;
183 const char *alg_name
;
185 kernel_param_lock(THIS_MODULE
);
187 alg_name
= ieee80211_default_rc_algo
;
191 ops
= ieee80211_try_rate_control_ops_get(alg_name
);
193 /* try default if specific alg requested but not found */
194 ops
= ieee80211_try_rate_control_ops_get(ieee80211_default_rc_algo
);
196 /* Note: check for > 0 is intentional to avoid clang warning */
197 if (!ops
&& (strlen(CONFIG_MAC80211_RC_DEFAULT
) > 0))
198 /* try built-in one if specific alg requested but not found */
199 ops
= ieee80211_try_rate_control_ops_get(CONFIG_MAC80211_RC_DEFAULT
);
201 kernel_param_unlock(THIS_MODULE
);
206 #ifdef CONFIG_MAC80211_DEBUGFS
207 static ssize_t
rcname_read(struct file
*file
, char __user
*userbuf
,
208 size_t count
, loff_t
*ppos
)
210 struct rate_control_ref
*ref
= file
->private_data
;
211 int len
= strlen(ref
->ops
->name
);
213 return simple_read_from_buffer(userbuf
, count
, ppos
,
214 ref
->ops
->name
, len
);
217 const struct file_operations rcname_ops
= {
220 .llseek
= default_llseek
,
224 static struct rate_control_ref
*
225 rate_control_alloc(const char *name
, struct ieee80211_local
*local
)
227 struct rate_control_ref
*ref
;
229 ref
= kmalloc(sizeof(struct rate_control_ref
), GFP_KERNEL
);
232 ref
->ops
= ieee80211_rate_control_ops_get(name
);
236 ref
->priv
= ref
->ops
->alloc(&local
->hw
);
246 static void rate_control_free(struct ieee80211_local
*local
,
247 struct rate_control_ref
*ctrl_ref
)
249 ctrl_ref
->ops
->free(ctrl_ref
->priv
);
251 #ifdef CONFIG_MAC80211_DEBUGFS
252 debugfs_remove_recursive(local
->debugfs
.rcdir
);
253 local
->debugfs
.rcdir
= NULL
;
259 void ieee80211_check_rate_mask(struct ieee80211_sub_if_data
*sdata
)
261 struct ieee80211_local
*local
= sdata
->local
;
262 struct ieee80211_supported_band
*sband
;
263 u32 user_mask
, basic_rates
= sdata
->vif
.bss_conf
.basic_rates
;
264 enum nl80211_band band
;
266 if (WARN_ON(!sdata
->vif
.bss_conf
.chandef
.chan
))
269 if (WARN_ON_ONCE(!basic_rates
))
272 band
= sdata
->vif
.bss_conf
.chandef
.chan
->band
;
273 user_mask
= sdata
->rc_rateidx_mask
[band
];
274 sband
= local
->hw
.wiphy
->bands
[band
];
276 if (user_mask
& basic_rates
)
280 "no overlap between basic rates (0x%x) and user mask (0x%x on band %d) - clearing the latter",
281 basic_rates
, user_mask
, band
);
282 sdata
->rc_rateidx_mask
[band
] = (1 << sband
->n_bitrates
) - 1;
285 static bool rc_no_data_or_no_ack_use_min(struct ieee80211_tx_rate_control
*txrc
)
287 struct sk_buff
*skb
= txrc
->skb
;
288 struct ieee80211_hdr
*hdr
= (struct ieee80211_hdr
*) skb
->data
;
289 struct ieee80211_tx_info
*info
= IEEE80211_SKB_CB(skb
);
292 fc
= hdr
->frame_control
;
294 return (info
->flags
& (IEEE80211_TX_CTL_NO_ACK
|
295 IEEE80211_TX_CTL_USE_MINRATE
)) ||
296 !ieee80211_is_data(fc
);
299 static void rc_send_low_basicrate(s8
*idx
, u32 basic_rates
,
300 struct ieee80211_supported_band
*sband
)
304 if (basic_rates
== 0)
305 return; /* assume basic rates unknown and accept rate */
308 if (basic_rates
& (1 << *idx
))
309 return; /* selected rate is a basic rate */
311 for (i
= *idx
+ 1; i
<= sband
->n_bitrates
; i
++) {
312 if (basic_rates
& (1 << i
)) {
318 /* could not find a basic rate; use original selection */
321 static void __rate_control_send_low(struct ieee80211_hw
*hw
,
322 struct ieee80211_supported_band
*sband
,
323 struct ieee80211_sta
*sta
,
324 struct ieee80211_tx_info
*info
,
329 ieee80211_chandef_rate_flags(&hw
->conf
.chandef
);
331 if ((sband
->band
== NL80211_BAND_2GHZ
) &&
332 (info
->flags
& IEEE80211_TX_CTL_NO_CCK_RATE
))
333 rate_flags
|= IEEE80211_RATE_ERP_G
;
335 info
->control
.rates
[0].idx
= 0;
336 for (i
= 0; i
< sband
->n_bitrates
; i
++) {
337 if (!(rate_mask
& BIT(i
)))
340 if ((rate_flags
& sband
->bitrates
[i
].flags
) != rate_flags
)
343 if (!rate_supported(sta
, sband
->band
, i
))
346 info
->control
.rates
[0].idx
= i
;
349 WARN_ONCE(i
== sband
->n_bitrates
,
350 "no supported rates for sta %pM (0x%x, band %d) in rate_mask 0x%x with flags 0x%x\n",
351 sta
? sta
->addr
: NULL
,
352 sta
? sta
->supp_rates
[sband
->band
] : -1,
354 rate_mask
, rate_flags
);
356 info
->control
.rates
[0].count
=
357 (info
->flags
& IEEE80211_TX_CTL_NO_ACK
) ?
358 1 : hw
->max_rate_tries
;
360 info
->control
.skip_table
= 1;
364 static bool rate_control_send_low(struct ieee80211_sta
*pubsta
,
365 struct ieee80211_tx_rate_control
*txrc
)
367 struct ieee80211_tx_info
*info
= IEEE80211_SKB_CB(txrc
->skb
);
368 struct ieee80211_supported_band
*sband
= txrc
->sband
;
369 struct sta_info
*sta
;
371 bool use_basicrate
= false;
373 if (!pubsta
|| rc_no_data_or_no_ack_use_min(txrc
)) {
374 __rate_control_send_low(txrc
->hw
, sband
, pubsta
, info
,
375 txrc
->rate_idx_mask
);
377 if (!pubsta
&& txrc
->bss
) {
378 mcast_rate
= txrc
->bss_conf
->mcast_rate
[sband
->band
];
379 if (mcast_rate
> 0) {
380 info
->control
.rates
[0].idx
= mcast_rate
- 1;
383 use_basicrate
= true;
385 sta
= container_of(pubsta
, struct sta_info
, sta
);
386 if (ieee80211_vif_is_mesh(&sta
->sdata
->vif
))
387 use_basicrate
= true;
391 rc_send_low_basicrate(&info
->control
.rates
[0].idx
,
392 txrc
->bss_conf
->basic_rates
,
400 static bool rate_idx_match_legacy_mask(s8
*rate_idx
, int n_bitrates
, u32 mask
)
404 /* See whether the selected rate or anything below it is allowed. */
405 for (j
= *rate_idx
; j
>= 0; j
--) {
406 if (mask
& (1 << j
)) {
407 /* Okay, found a suitable rate. Use it. */
413 /* Try to find a higher rate that would be allowed */
414 for (j
= *rate_idx
+ 1; j
< n_bitrates
; j
++) {
415 if (mask
& (1 << j
)) {
416 /* Okay, found a suitable rate. Use it. */
424 static bool rate_idx_match_mcs_mask(s8
*rate_idx
, u8
*mcs_mask
)
429 ridx
= *rate_idx
/ 8;
430 rbit
= *rate_idx
% 8;
433 if (ridx
< 0 || ridx
>= IEEE80211_HT_MCS_MASK_LEN
)
436 /* See whether the selected rate or anything below it is allowed. */
437 for (i
= ridx
; i
>= 0; i
--) {
438 for (j
= rbit
; j
>= 0; j
--)
439 if (mcs_mask
[i
] & BIT(j
)) {
440 *rate_idx
= i
* 8 + j
;
446 /* Try to find a higher rate that would be allowed */
447 ridx
= (*rate_idx
+ 1) / 8;
448 rbit
= (*rate_idx
+ 1) % 8;
450 for (i
= ridx
; i
< IEEE80211_HT_MCS_MASK_LEN
; i
++) {
451 for (j
= rbit
; j
< 8; j
++)
452 if (mcs_mask
[i
] & BIT(j
)) {
453 *rate_idx
= i
* 8 + j
;
461 static bool rate_idx_match_vht_mcs_mask(s8
*rate_idx
, u16
*vht_mask
)
466 ridx
= *rate_idx
>> 4;
467 rbit
= *rate_idx
& 0xf;
469 if (ridx
< 0 || ridx
>= NL80211_VHT_NSS_MAX
)
472 /* See whether the selected rate or anything below it is allowed. */
473 for (i
= ridx
; i
>= 0; i
--) {
474 for (j
= rbit
; j
>= 0; j
--) {
475 if (vht_mask
[i
] & BIT(j
)) {
476 *rate_idx
= (i
<< 4) | j
;
483 /* Try to find a higher rate that would be allowed */
484 ridx
= (*rate_idx
+ 1) >> 4;
485 rbit
= (*rate_idx
+ 1) & 0xf;
487 for (i
= ridx
; i
< NL80211_VHT_NSS_MAX
; i
++) {
488 for (j
= rbit
; j
< 16; j
++) {
489 if (vht_mask
[i
] & BIT(j
)) {
490 *rate_idx
= (i
<< 4) | j
;
499 static void rate_idx_match_mask(s8
*rate_idx
, u16
*rate_flags
,
500 struct ieee80211_supported_band
*sband
,
501 enum nl80211_chan_width chan_width
,
503 u8 mcs_mask
[IEEE80211_HT_MCS_MASK_LEN
],
504 u16 vht_mask
[NL80211_VHT_NSS_MAX
])
506 if (*rate_flags
& IEEE80211_TX_RC_VHT_MCS
) {
507 /* handle VHT rates */
508 if (rate_idx_match_vht_mcs_mask(rate_idx
, vht_mask
))
512 /* keep protection flags */
513 *rate_flags
&= (IEEE80211_TX_RC_USE_RTS_CTS
|
514 IEEE80211_TX_RC_USE_CTS_PROTECT
|
515 IEEE80211_TX_RC_USE_SHORT_PREAMBLE
);
517 *rate_flags
|= IEEE80211_TX_RC_MCS
;
518 if (chan_width
== NL80211_CHAN_WIDTH_40
)
519 *rate_flags
|= IEEE80211_TX_RC_40_MHZ_WIDTH
;
521 if (rate_idx_match_mcs_mask(rate_idx
, mcs_mask
))
524 /* also try the legacy rates. */
525 *rate_flags
&= ~(IEEE80211_TX_RC_MCS
|
526 IEEE80211_TX_RC_40_MHZ_WIDTH
);
527 if (rate_idx_match_legacy_mask(rate_idx
, sband
->n_bitrates
,
530 } else if (*rate_flags
& IEEE80211_TX_RC_MCS
) {
531 /* handle HT rates */
532 if (rate_idx_match_mcs_mask(rate_idx
, mcs_mask
))
535 /* also try the legacy rates. */
537 /* keep protection flags */
538 *rate_flags
&= (IEEE80211_TX_RC_USE_RTS_CTS
|
539 IEEE80211_TX_RC_USE_CTS_PROTECT
|
540 IEEE80211_TX_RC_USE_SHORT_PREAMBLE
);
541 if (rate_idx_match_legacy_mask(rate_idx
, sband
->n_bitrates
,
545 /* handle legacy rates */
546 if (rate_idx_match_legacy_mask(rate_idx
, sband
->n_bitrates
,
550 /* if HT BSS, and we handle a data frame, also try HT rates */
551 switch (chan_width
) {
552 case NL80211_CHAN_WIDTH_20_NOHT
:
553 case NL80211_CHAN_WIDTH_5
:
554 case NL80211_CHAN_WIDTH_10
:
561 /* keep protection flags */
562 *rate_flags
&= (IEEE80211_TX_RC_USE_RTS_CTS
|
563 IEEE80211_TX_RC_USE_CTS_PROTECT
|
564 IEEE80211_TX_RC_USE_SHORT_PREAMBLE
);
566 *rate_flags
|= IEEE80211_TX_RC_MCS
;
568 if (chan_width
== NL80211_CHAN_WIDTH_40
)
569 *rate_flags
|= IEEE80211_TX_RC_40_MHZ_WIDTH
;
571 if (rate_idx_match_mcs_mask(rate_idx
, mcs_mask
))
576 * Uh.. No suitable rate exists. This should not really happen with
577 * sane TX rate mask configurations. However, should someone manage to
578 * configure supported rates and TX rate mask in incompatible way,
579 * allow the frame to be transmitted with whatever the rate control
584 static void rate_fixup_ratelist(struct ieee80211_vif
*vif
,
585 struct ieee80211_supported_band
*sband
,
586 struct ieee80211_tx_info
*info
,
587 struct ieee80211_tx_rate
*rates
,
590 struct ieee80211_rate
*rate
;
595 * Set up the RTS/CTS rate as the fastest basic rate
596 * that is not faster than the data rate unless there
597 * is no basic rate slower than the data rate, in which
598 * case we pick the slowest basic rate
600 * XXX: Should this check all retry rates?
602 if (!(rates
[0].flags
&
603 (IEEE80211_TX_RC_MCS
| IEEE80211_TX_RC_VHT_MCS
))) {
604 u32 basic_rates
= vif
->bss_conf
.basic_rates
;
605 s8 baserate
= basic_rates
? ffs(basic_rates
) - 1 : 0;
607 rate
= &sband
->bitrates
[rates
[0].idx
];
609 for (i
= 0; i
< sband
->n_bitrates
; i
++) {
610 /* must be a basic rate */
611 if (!(basic_rates
& BIT(i
)))
613 /* must not be faster than the data rate */
614 if (sband
->bitrates
[i
].bitrate
> rate
->bitrate
)
617 if (sband
->bitrates
[baserate
].bitrate
<
618 sband
->bitrates
[i
].bitrate
)
622 info
->control
.rts_cts_rate_idx
= baserate
;
625 for (i
= 0; i
< max_rates
; i
++) {
627 * make sure there's no valid rate following
628 * an invalid one, just in case drivers don't
629 * take the API seriously to stop at -1.
635 if (rates
[i
].idx
< 0) {
641 * For now assume MCS is already set up correctly, this
644 if (rates
[i
].flags
& IEEE80211_TX_RC_MCS
) {
645 WARN_ON(rates
[i
].idx
> 76);
647 if (!(rates
[i
].flags
& IEEE80211_TX_RC_USE_RTS_CTS
) &&
648 info
->control
.use_cts_prot
)
650 IEEE80211_TX_RC_USE_CTS_PROTECT
;
654 if (rates
[i
].flags
& IEEE80211_TX_RC_VHT_MCS
) {
655 WARN_ON(ieee80211_rate_get_vht_mcs(&rates
[i
]) > 9);
659 /* set up RTS protection if desired */
660 if (info
->control
.use_rts
) {
661 rates
[i
].flags
|= IEEE80211_TX_RC_USE_RTS_CTS
;
662 info
->control
.use_cts_prot
= false;
666 if (WARN_ON_ONCE(rates
[i
].idx
>= sband
->n_bitrates
)) {
671 rate
= &sband
->bitrates
[rates
[i
].idx
];
673 /* set up short preamble */
674 if (info
->control
.short_preamble
&&
675 rate
->flags
& IEEE80211_RATE_SHORT_PREAMBLE
)
676 rates
[i
].flags
|= IEEE80211_TX_RC_USE_SHORT_PREAMBLE
;
678 /* set up G protection */
679 if (!(rates
[i
].flags
& IEEE80211_TX_RC_USE_RTS_CTS
) &&
680 info
->control
.use_cts_prot
&&
681 rate
->flags
& IEEE80211_RATE_ERP_G
)
682 rates
[i
].flags
|= IEEE80211_TX_RC_USE_CTS_PROTECT
;
687 static void rate_control_fill_sta_table(struct ieee80211_sta
*sta
,
688 struct ieee80211_tx_info
*info
,
689 struct ieee80211_tx_rate
*rates
,
692 struct ieee80211_sta_rates
*ratetbl
= NULL
;
695 if (sta
&& !info
->control
.skip_table
)
696 ratetbl
= rcu_dereference(sta
->rates
);
698 /* Fill remaining rate slots with data from the sta rate table. */
699 max_rates
= min_t(int, max_rates
, IEEE80211_TX_RATE_TABLE_SIZE
);
700 for (i
= 0; i
< max_rates
; i
++) {
701 if (i
< ARRAY_SIZE(info
->control
.rates
) &&
702 info
->control
.rates
[i
].idx
>= 0 &&
703 info
->control
.rates
[i
].count
) {
704 if (rates
!= info
->control
.rates
)
705 rates
[i
] = info
->control
.rates
[i
];
706 } else if (ratetbl
) {
707 rates
[i
].idx
= ratetbl
->rate
[i
].idx
;
708 rates
[i
].flags
= ratetbl
->rate
[i
].flags
;
709 if (info
->control
.use_rts
)
710 rates
[i
].count
= ratetbl
->rate
[i
].count_rts
;
711 else if (info
->control
.use_cts_prot
)
712 rates
[i
].count
= ratetbl
->rate
[i
].count_cts
;
714 rates
[i
].count
= ratetbl
->rate
[i
].count
;
720 if (rates
[i
].idx
< 0 || !rates
[i
].count
)
725 static bool rate_control_cap_mask(struct ieee80211_sub_if_data
*sdata
,
726 struct ieee80211_supported_band
*sband
,
727 struct ieee80211_sta
*sta
, u32
*mask
,
728 u8 mcs_mask
[IEEE80211_HT_MCS_MASK_LEN
],
729 u16 vht_mask
[NL80211_VHT_NSS_MAX
])
733 *mask
= sdata
->rc_rateidx_mask
[sband
->band
];
734 flags
= ieee80211_chandef_rate_flags(&sdata
->vif
.bss_conf
.chandef
);
735 for (i
= 0; i
< sband
->n_bitrates
; i
++) {
736 if ((flags
& sband
->bitrates
[i
].flags
) != flags
)
740 if (*mask
== (1 << sband
->n_bitrates
) - 1 &&
741 !sdata
->rc_has_mcs_mask
[sband
->band
] &&
742 !sdata
->rc_has_vht_mcs_mask
[sband
->band
])
745 if (sdata
->rc_has_mcs_mask
[sband
->band
])
746 memcpy(mcs_mask
, sdata
->rc_rateidx_mcs_mask
[sband
->band
],
747 IEEE80211_HT_MCS_MASK_LEN
);
749 memset(mcs_mask
, 0xff, IEEE80211_HT_MCS_MASK_LEN
);
751 if (sdata
->rc_has_vht_mcs_mask
[sband
->band
])
752 memcpy(vht_mask
, sdata
->rc_rateidx_vht_mcs_mask
[sband
->band
],
753 sizeof(u16
) * NL80211_VHT_NSS_MAX
);
755 memset(vht_mask
, 0xff, sizeof(u16
) * NL80211_VHT_NSS_MAX
);
759 u16 sta_vht_mask
[NL80211_VHT_NSS_MAX
];
761 /* Filter out rates that the STA does not support */
762 *mask
&= sta
->supp_rates
[sband
->band
];
763 for (i
= 0; i
< IEEE80211_HT_MCS_MASK_LEN
; i
++)
764 mcs_mask
[i
] &= sta
->ht_cap
.mcs
.rx_mask
[i
];
766 sta_vht_cap
= sta
->vht_cap
.vht_mcs
.rx_mcs_map
;
767 ieee80211_get_vht_mask_from_cap(sta_vht_cap
, sta_vht_mask
);
768 for (i
= 0; i
< NL80211_VHT_NSS_MAX
; i
++)
769 vht_mask
[i
] &= sta_vht_mask
[i
];
776 rate_control_apply_mask_ratetbl(struct sta_info
*sta
,
777 struct ieee80211_supported_band
*sband
,
778 struct ieee80211_sta_rates
*rates
)
782 u8 mcs_mask
[IEEE80211_HT_MCS_MASK_LEN
];
783 u16 vht_mask
[NL80211_VHT_NSS_MAX
];
784 enum nl80211_chan_width chan_width
;
786 if (!rate_control_cap_mask(sta
->sdata
, sband
, &sta
->sta
, &mask
,
790 chan_width
= sta
->sdata
->vif
.bss_conf
.chandef
.width
;
791 for (i
= 0; i
< IEEE80211_TX_RATE_TABLE_SIZE
; i
++) {
792 if (rates
->rate
[i
].idx
< 0)
795 rate_idx_match_mask(&rates
->rate
[i
].idx
, &rates
->rate
[i
].flags
,
796 sband
, chan_width
, mask
, mcs_mask
,
801 static void rate_control_apply_mask(struct ieee80211_sub_if_data
*sdata
,
802 struct ieee80211_sta
*sta
,
803 struct ieee80211_supported_band
*sband
,
804 struct ieee80211_tx_rate
*rates
,
807 enum nl80211_chan_width chan_width
;
808 u8 mcs_mask
[IEEE80211_HT_MCS_MASK_LEN
];
810 u16 rate_flags
, vht_mask
[NL80211_VHT_NSS_MAX
];
814 * Try to enforce the rateidx mask the user wanted. skip this if the
815 * default mask (allow all rates) is used to save some processing for
818 if (!rate_control_cap_mask(sdata
, sband
, sta
, &mask
, mcs_mask
,
823 * Make sure the rate index selected for each TX rate is
824 * included in the configured mask and change the rate indexes
827 chan_width
= sdata
->vif
.bss_conf
.chandef
.width
;
828 for (i
= 0; i
< max_rates
; i
++) {
829 /* Skip invalid rates */
830 if (rates
[i
].idx
< 0)
833 rate_flags
= rates
[i
].flags
;
834 rate_idx_match_mask(&rates
[i
].idx
, &rate_flags
, sband
,
835 chan_width
, mask
, mcs_mask
, vht_mask
);
836 rates
[i
].flags
= rate_flags
;
840 void ieee80211_get_tx_rates(struct ieee80211_vif
*vif
,
841 struct ieee80211_sta
*sta
,
843 struct ieee80211_tx_rate
*dest
,
846 struct ieee80211_sub_if_data
*sdata
;
847 struct ieee80211_hdr
*hdr
= (struct ieee80211_hdr
*) skb
->data
;
848 struct ieee80211_tx_info
*info
= IEEE80211_SKB_CB(skb
);
849 struct ieee80211_supported_band
*sband
;
851 rate_control_fill_sta_table(sta
, info
, dest
, max_rates
);
856 sdata
= vif_to_sdata(vif
);
857 sband
= sdata
->local
->hw
.wiphy
->bands
[info
->band
];
859 if (ieee80211_is_data(hdr
->frame_control
))
860 rate_control_apply_mask(sdata
, sta
, sband
, dest
, max_rates
);
863 __rate_control_send_low(&sdata
->local
->hw
, sband
, sta
, info
,
864 sdata
->rc_rateidx_mask
[info
->band
]);
867 rate_fixup_ratelist(vif
, sband
, info
, dest
, max_rates
);
869 EXPORT_SYMBOL(ieee80211_get_tx_rates
);
871 void rate_control_get_rate(struct ieee80211_sub_if_data
*sdata
,
872 struct sta_info
*sta
,
873 struct ieee80211_tx_rate_control
*txrc
)
875 struct rate_control_ref
*ref
= sdata
->local
->rate_ctrl
;
876 void *priv_sta
= NULL
;
877 struct ieee80211_sta
*ista
= NULL
;
878 struct ieee80211_tx_info
*info
= IEEE80211_SKB_CB(txrc
->skb
);
881 for (i
= 0; i
< IEEE80211_TX_MAX_RATES
; i
++) {
882 info
->control
.rates
[i
].idx
= -1;
883 info
->control
.rates
[i
].flags
= 0;
884 info
->control
.rates
[i
].count
= 0;
887 if (rate_control_send_low(sta
? &sta
->sta
: NULL
, txrc
))
890 if (ieee80211_hw_check(&sdata
->local
->hw
, HAS_RATE_CONTROL
))
893 if (sta
&& test_sta_flag(sta
, WLAN_STA_RATE_CONTROL
)) {
895 priv_sta
= sta
->rate_ctrl_priv
;
899 spin_lock_bh(&sta
->rate_ctrl_lock
);
900 ref
->ops
->get_rate(ref
->priv
, ista
, priv_sta
, txrc
);
901 spin_unlock_bh(&sta
->rate_ctrl_lock
);
903 rate_control_send_low(NULL
, txrc
);
906 if (ieee80211_hw_check(&sdata
->local
->hw
, SUPPORTS_RC_TABLE
))
909 ieee80211_get_tx_rates(&sdata
->vif
, ista
, txrc
->skb
,
911 ARRAY_SIZE(info
->control
.rates
));
914 int rate_control_set_rates(struct ieee80211_hw
*hw
,
915 struct ieee80211_sta
*pubsta
,
916 struct ieee80211_sta_rates
*rates
)
918 struct sta_info
*sta
= container_of(pubsta
, struct sta_info
, sta
);
919 struct ieee80211_sta_rates
*old
;
920 struct ieee80211_supported_band
*sband
;
922 sband
= ieee80211_get_sband(sta
->sdata
);
925 rate_control_apply_mask_ratetbl(sta
, sband
, rates
);
927 * mac80211 guarantees that this function will not be called
928 * concurrently, so the following RCU access is safe, even without
929 * extra locking. This can not be checked easily, so we just set
930 * the condition to true.
932 old
= rcu_dereference_protected(pubsta
->rates
, true);
933 rcu_assign_pointer(pubsta
->rates
, rates
);
935 kfree_rcu(old
, rcu_head
);
938 drv_sta_rate_tbl_update(hw_to_local(hw
), sta
->sdata
, pubsta
);
940 ieee80211_sta_set_expected_throughput(pubsta
, sta_get_expected_throughput(sta
));
944 EXPORT_SYMBOL(rate_control_set_rates
);
946 int ieee80211_init_rate_ctrl_alg(struct ieee80211_local
*local
,
949 struct rate_control_ref
*ref
;
953 if (local
->open_count
)
956 if (ieee80211_hw_check(&local
->hw
, HAS_RATE_CONTROL
)) {
957 if (WARN_ON(!local
->ops
->set_rts_threshold
))
962 ref
= rate_control_alloc(name
, local
);
964 wiphy_warn(local
->hw
.wiphy
,
965 "Failed to select rate control algorithm\n");
969 WARN_ON(local
->rate_ctrl
);
970 local
->rate_ctrl
= ref
;
972 wiphy_debug(local
->hw
.wiphy
, "Selected rate control algorithm '%s'\n",
978 void rate_control_deinitialize(struct ieee80211_local
*local
)
980 struct rate_control_ref
*ref
;
982 ref
= local
->rate_ctrl
;
987 local
->rate_ctrl
= NULL
;
988 rate_control_free(local
, ref
);