1// SPDX-License-Identifier: GPL-2.0-only
2/*
3 * Copyright 2002-2005, Instant802 Networks, Inc.
4 * Copyright 2005-2006, Devicescape Software, Inc.
5 * Copyright (c) 2006 Jiri Benc <jbenc@suse.cz>
6 * Copyright 2017 Intel Deutschland GmbH
7 * Copyright (C) 2019, 2022-2025 Intel Corporation
8 */
9
10#include <linux/kernel.h>
11#include <linux/rtnetlink.h>
12#include <linux/module.h>
13#include <linux/slab.h>
14#include "rate.h"
15#include "ieee80211_i.h"
16#include "debugfs.h"
17
18struct rate_control_alg {
19 struct list_head list;
20 const struct rate_control_ops *ops;
21};
22
23static LIST_HEAD(rate_ctrl_algs);
24static DEFINE_MUTEX(rate_ctrl_mutex);
25
26static char *ieee80211_default_rc_algo = CONFIG_MAC80211_RC_DEFAULT;
27module_param(ieee80211_default_rc_algo, charp, 0644);
28MODULE_PARM_DESC(ieee80211_default_rc_algo,
29 "Default rate control algorithm for mac80211 to use");
30
31void rate_control_rate_init(struct link_sta_info *link_sta)
32{
33 struct sta_info *sta = link_sta->sta;
34 struct ieee80211_local *local = sta->sdata->local;
35 struct rate_control_ref *ref = sta->rate_ctrl;
36 struct ieee80211_sta *ista = &sta->sta;
37 void *priv_sta = sta->rate_ctrl_priv;
38 struct ieee80211_supported_band *sband;
39 struct ieee80211_chanctx_conf *chanctx_conf;
40
41 ieee80211_sta_init_nss(link_sta);
42
43 if (!ref)
44 return;
45
46 /* SW rate control isn't supported with MLO right now */
47 if (WARN_ON(ieee80211_vif_is_mld(&sta->sdata->vif)))
48 return;
49
50 rcu_read_lock();
51
52 chanctx_conf = rcu_dereference(sta->sdata->vif.bss_conf.chanctx_conf);
53 if (WARN_ON(!chanctx_conf)) {
54 rcu_read_unlock();
55 return;
56 }
57
58 sband = local->hw.wiphy->bands[chanctx_conf->def.chan->band];
59
60 /* TODO: check for minstrel_s1g ? */
61 if (sband->band == NL80211_BAND_S1GHZ) {
62 ieee80211_s1g_sta_rate_init(sta);
63 rcu_read_unlock();
64 return;
65 }
66
67 spin_lock_bh(lock: &sta->rate_ctrl_lock);
68 ref->ops->rate_init(ref->priv, sband, &chanctx_conf->def, ista,
69 priv_sta);
70 spin_unlock_bh(lock: &sta->rate_ctrl_lock);
71 rcu_read_unlock();
72 set_sta_flag(sta, flag: WLAN_STA_RATE_CONTROL);
73}
74
75void rate_control_rate_init_all_links(struct sta_info *sta)
76{
77 int link_id;
78
79 for (link_id = 0; link_id < ARRAY_SIZE(sta->link); link_id++) {
80 struct link_sta_info *link_sta;
81
82 link_sta = sdata_dereference(sta->link[link_id], sta->sdata);
83 if (!link_sta)
84 continue;
85
86 rate_control_rate_init(link_sta);
87 }
88}
89
90void rate_control_tx_status(struct ieee80211_local *local,
91 struct ieee80211_tx_status *st)
92{
93 struct rate_control_ref *ref = local->rate_ctrl;
94 struct sta_info *sta = container_of(st->sta, struct sta_info, sta);
95 void *priv_sta = sta->rate_ctrl_priv;
96 struct ieee80211_supported_band *sband;
97
98 if (!ref || !test_sta_flag(sta, flag: WLAN_STA_RATE_CONTROL))
99 return;
100
101 if (st->info->band >= NUM_NL80211_BANDS)
102 return;
103
104 sband = local->hw.wiphy->bands[st->info->band];
105
106 spin_lock_bh(lock: &sta->rate_ctrl_lock);
107 if (ref->ops->tx_status_ext)
108 ref->ops->tx_status_ext(ref->priv, sband, priv_sta, st);
109 else if (st->skb)
110 ref->ops->tx_status(ref->priv, sband, st->sta, priv_sta, st->skb);
111 else
112 WARN_ON_ONCE(1);
113
114 spin_unlock_bh(lock: &sta->rate_ctrl_lock);
115}
116
117void rate_control_rate_update(struct ieee80211_local *local,
118 struct ieee80211_supported_band *sband,
119 struct link_sta_info *link_sta,
120 u32 changed)
121{
122 struct rate_control_ref *ref = local->rate_ctrl;
123 struct sta_info *sta = link_sta->sta;
124 struct ieee80211_sta *ista = &sta->sta;
125 void *priv_sta = sta->rate_ctrl_priv;
126 struct ieee80211_chanctx_conf *chanctx_conf;
127
128 if (ref && ref->ops->rate_update) {
129 rcu_read_lock();
130
131 chanctx_conf = rcu_dereference(sta->sdata->vif.bss_conf.chanctx_conf);
132 if (WARN_ON(!chanctx_conf)) {
133 rcu_read_unlock();
134 return;
135 }
136
137 spin_lock_bh(lock: &sta->rate_ctrl_lock);
138 ref->ops->rate_update(ref->priv, sband, &chanctx_conf->def,
139 ista, priv_sta, changed);
140 spin_unlock_bh(lock: &sta->rate_ctrl_lock);
141 rcu_read_unlock();
142 }
143
144 if (sta->uploaded)
145 drv_link_sta_rc_update(local, sdata: sta->sdata, link_sta: link_sta->pub,
146 changed);
147}
148
149int ieee80211_rate_control_register(const struct rate_control_ops *ops)
150{
151 struct rate_control_alg *alg;
152
153 if (!ops->name)
154 return -EINVAL;
155
156 mutex_lock(lock: &rate_ctrl_mutex);
157 list_for_each_entry(alg, &rate_ctrl_algs, list) {
158 if (!strcmp(alg->ops->name, ops->name)) {
159 /* don't register an algorithm twice */
160 WARN_ON(1);
161 mutex_unlock(lock: &rate_ctrl_mutex);
162 return -EALREADY;
163 }
164 }
165
166 alg = kzalloc(sizeof(*alg), GFP_KERNEL);
167 if (alg == NULL) {
168 mutex_unlock(lock: &rate_ctrl_mutex);
169 return -ENOMEM;
170 }
171 alg->ops = ops;
172
173 list_add_tail(new: &alg->list, head: &rate_ctrl_algs);
174 mutex_unlock(lock: &rate_ctrl_mutex);
175
176 return 0;
177}
178EXPORT_SYMBOL(ieee80211_rate_control_register);
179
180void ieee80211_rate_control_unregister(const struct rate_control_ops *ops)
181{
182 struct rate_control_alg *alg;
183
184 mutex_lock(lock: &rate_ctrl_mutex);
185 list_for_each_entry(alg, &rate_ctrl_algs, list) {
186 if (alg->ops == ops) {
187 list_del(entry: &alg->list);
188 kfree(objp: alg);
189 break;
190 }
191 }
192 mutex_unlock(lock: &rate_ctrl_mutex);
193}
194EXPORT_SYMBOL(ieee80211_rate_control_unregister);
195
196static const struct rate_control_ops *
197ieee80211_try_rate_control_ops_get(const char *name)
198{
199 struct rate_control_alg *alg;
200 const struct rate_control_ops *ops = NULL;
201
202 if (!name)
203 return NULL;
204
205 mutex_lock(lock: &rate_ctrl_mutex);
206 list_for_each_entry(alg, &rate_ctrl_algs, list) {
207 if (!strcmp(alg->ops->name, name)) {
208 ops = alg->ops;
209 break;
210 }
211 }
212 mutex_unlock(lock: &rate_ctrl_mutex);
213 return ops;
214}
215
216/* Get the rate control algorithm. */
217static const struct rate_control_ops *
218ieee80211_rate_control_ops_get(const char *name)
219{
220 const struct rate_control_ops *ops;
221 const char *alg_name;
222
223 kernel_param_lock(THIS_MODULE);
224 if (!name)
225 alg_name = ieee80211_default_rc_algo;
226 else
227 alg_name = name;
228
229 ops = ieee80211_try_rate_control_ops_get(name: alg_name);
230 if (!ops && name)
231 /* try default if specific alg requested but not found */
232 ops = ieee80211_try_rate_control_ops_get(name: ieee80211_default_rc_algo);
233
234 /* Note: check for > 0 is intentional to avoid clang warning */
235 if (!ops && (strlen(CONFIG_MAC80211_RC_DEFAULT) > 0))
236 /* try built-in one if specific alg requested but not found */
237 ops = ieee80211_try_rate_control_ops_get(CONFIG_MAC80211_RC_DEFAULT);
238
239 kernel_param_unlock(THIS_MODULE);
240
241 return ops;
242}
243
244#ifdef CONFIG_MAC80211_DEBUGFS
245static ssize_t rcname_read(struct file *file, char __user *userbuf,
246 size_t count, loff_t *ppos)
247{
248 struct rate_control_ref *ref = file->private_data;
249 int len = strlen(ref->ops->name);
250
251 return simple_read_from_buffer(userbuf, count, ppos,
252 ref->ops->name, len);
253}
254
255const struct debugfs_short_fops rcname_ops = {
256 .read = rcname_read,
257 .llseek = default_llseek,
258};
259#endif
260
261static struct rate_control_ref *
262rate_control_alloc(const char *name, struct ieee80211_local *local)
263{
264 struct rate_control_ref *ref;
265
266 ref = kmalloc(sizeof(struct rate_control_ref), GFP_KERNEL);
267 if (!ref)
268 return NULL;
269 ref->ops = ieee80211_rate_control_ops_get(name);
270 if (!ref->ops)
271 goto free;
272
273 ref->priv = ref->ops->alloc(&local->hw);
274 if (!ref->priv)
275 goto free;
276 return ref;
277
278free:
279 kfree(objp: ref);
280 return NULL;
281}
282
283static void rate_control_free(struct ieee80211_local *local,
284 struct rate_control_ref *ctrl_ref)
285{
286 ctrl_ref->ops->free(ctrl_ref->priv);
287
288#ifdef CONFIG_MAC80211_DEBUGFS
289 debugfs_remove_recursive(local->debugfs.rcdir);
290 local->debugfs.rcdir = NULL;
291#endif
292
293 kfree(objp: ctrl_ref);
294}
295
296void ieee80211_check_rate_mask(struct ieee80211_link_data *link)
297{
298 struct ieee80211_sub_if_data *sdata = link->sdata;
299 struct ieee80211_local *local = sdata->local;
300 struct ieee80211_supported_band *sband;
301 u32 user_mask, basic_rates = link->conf->basic_rates;
302 enum nl80211_band band;
303
304 if (WARN_ON(!link->conf->chanreq.oper.chan))
305 return;
306
307 band = link->conf->chanreq.oper.chan->band;
308 if (band == NL80211_BAND_S1GHZ) {
309 /* TODO */
310 return;
311 }
312
313 if (WARN_ON_ONCE(!basic_rates))
314 return;
315
316 user_mask = sdata->rc_rateidx_mask[band];
317 sband = local->hw.wiphy->bands[band];
318
319 if (user_mask & basic_rates)
320 return;
321
322 sdata_dbg(sdata,
323 "no overlap between basic rates (0x%x) and user mask (0x%x on band %d) - clearing the latter",
324 basic_rates, user_mask, band);
325 sdata->rc_rateidx_mask[band] = (1 << sband->n_bitrates) - 1;
326}
327
328static bool rc_no_data_or_no_ack_use_min(struct ieee80211_tx_rate_control *txrc)
329{
330 struct sk_buff *skb = txrc->skb;
331 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
332
333 return (info->flags & (IEEE80211_TX_CTL_NO_ACK |
334 IEEE80211_TX_CTL_USE_MINRATE)) ||
335 !ieee80211_is_tx_data(skb);
336}
337
338static void rc_send_low_basicrate(struct ieee80211_tx_rate *rate,
339 u32 basic_rates,
340 struct ieee80211_supported_band *sband)
341{
342 u8 i;
343
344 if (sband->band == NL80211_BAND_S1GHZ) {
345 /* TODO */
346 rate->flags |= IEEE80211_TX_RC_S1G_MCS;
347 rate->idx = 0;
348 return;
349 }
350
351 if (basic_rates == 0)
352 return; /* assume basic rates unknown and accept rate */
353 if (rate->idx < 0)
354 return;
355 if (basic_rates & (1 << rate->idx))
356 return; /* selected rate is a basic rate */
357
358 for (i = rate->idx + 1; i <= sband->n_bitrates; i++) {
359 if (basic_rates & (1 << i)) {
360 rate->idx = i;
361 return;
362 }
363 }
364
365 /* could not find a basic rate; use original selection */
366}
367
368static void __rate_control_send_low(struct ieee80211_hw *hw,
369 struct ieee80211_supported_band *sband,
370 struct ieee80211_sta *sta,
371 struct ieee80211_tx_info *info,
372 u32 rate_mask)
373{
374 u32 rate_flags = 0;
375 int i;
376
377 if (sband->band == NL80211_BAND_S1GHZ) {
378 info->control.rates[0].flags |= IEEE80211_TX_RC_S1G_MCS;
379 info->control.rates[0].idx = 0;
380 return;
381 }
382
383 if ((sband->band == NL80211_BAND_2GHZ) &&
384 (info->flags & IEEE80211_TX_CTL_NO_CCK_RATE))
385 rate_flags |= IEEE80211_RATE_ERP_G;
386
387 info->control.rates[0].idx = 0;
388 for (i = 0; i < sband->n_bitrates; i++) {
389 if (!(rate_mask & BIT(i)))
390 continue;
391
392 if ((rate_flags & sband->bitrates[i].flags) != rate_flags)
393 continue;
394
395 if (!rate_supported(sta, band: sband->band, index: i))
396 continue;
397
398 info->control.rates[0].idx = i;
399 break;
400 }
401 WARN_ONCE(i == sband->n_bitrates,
402 "no supported rates for sta %pM (0x%x, band %d) in rate_mask 0x%x with flags 0x%x\n",
403 sta ? sta->addr : NULL,
404 sta ? sta->deflink.supp_rates[sband->band] : -1,
405 sband->band,
406 rate_mask, rate_flags);
407
408 info->control.rates[0].count =
409 (info->flags & IEEE80211_TX_CTL_NO_ACK) ?
410 1 : hw->max_rate_tries;
411
412 info->control.skip_table = 1;
413}
414
415
416static bool rate_control_send_low(struct ieee80211_sta *pubsta,
417 struct ieee80211_tx_rate_control *txrc)
418{
419 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb: txrc->skb);
420 struct ieee80211_supported_band *sband = txrc->sband;
421 struct sta_info *sta;
422 int mcast_rate;
423 bool use_basicrate = false;
424
425 if (!sband)
426 return false;
427
428 if (!pubsta || rc_no_data_or_no_ack_use_min(txrc)) {
429 __rate_control_send_low(hw: txrc->hw, sband, sta: pubsta, info,
430 rate_mask: txrc->rate_idx_mask);
431
432 if (!pubsta && txrc->bss) {
433 mcast_rate = txrc->bss_conf->mcast_rate[sband->band];
434 if (mcast_rate > 0) {
435 info->control.rates[0].idx = mcast_rate - 1;
436 return true;
437 }
438 use_basicrate = true;
439 } else if (pubsta) {
440 sta = container_of(pubsta, struct sta_info, sta);
441 if (ieee80211_vif_is_mesh(vif: &sta->sdata->vif))
442 use_basicrate = true;
443 }
444
445 if (use_basicrate)
446 rc_send_low_basicrate(rate: &info->control.rates[0],
447 basic_rates: txrc->bss_conf->basic_rates,
448 sband);
449
450 return true;
451 }
452 return false;
453}
454
455static bool rate_idx_match_legacy_mask(s8 *rate_idx, int n_bitrates, u32 mask)
456{
457 int j;
458
459 /* See whether the selected rate or anything below it is allowed. */
460 for (j = *rate_idx; j >= 0; j--) {
461 if (mask & (1 << j)) {
462 /* Okay, found a suitable rate. Use it. */
463 *rate_idx = j;
464 return true;
465 }
466 }
467
468 /* Try to find a higher rate that would be allowed */
469 for (j = *rate_idx + 1; j < n_bitrates; j++) {
470 if (mask & (1 << j)) {
471 /* Okay, found a suitable rate. Use it. */
472 *rate_idx = j;
473 return true;
474 }
475 }
476 return false;
477}
478
479static bool rate_idx_match_mcs_mask(s8 *rate_idx, u8 *mcs_mask)
480{
481 int i, j;
482 int ridx, rbit;
483
484 ridx = *rate_idx / 8;
485 rbit = *rate_idx % 8;
486
487 /* sanity check */
488 if (ridx < 0 || ridx >= IEEE80211_HT_MCS_MASK_LEN)
489 return false;
490
491 /* See whether the selected rate or anything below it is allowed. */
492 for (i = ridx; i >= 0; i--) {
493 for (j = rbit; j >= 0; j--)
494 if (mcs_mask[i] & BIT(j)) {
495 *rate_idx = i * 8 + j;
496 return true;
497 }
498 rbit = 7;
499 }
500
501 /* Try to find a higher rate that would be allowed */
502 ridx = (*rate_idx + 1) / 8;
503 rbit = (*rate_idx + 1) % 8;
504
505 for (i = ridx; i < IEEE80211_HT_MCS_MASK_LEN; i++) {
506 for (j = rbit; j < 8; j++)
507 if (mcs_mask[i] & BIT(j)) {
508 *rate_idx = i * 8 + j;
509 return true;
510 }
511 rbit = 0;
512 }
513 return false;
514}
515
516static bool rate_idx_match_vht_mcs_mask(s8 *rate_idx, u16 *vht_mask)
517{
518 int i, j;
519 int ridx, rbit;
520
521 ridx = *rate_idx >> 4;
522 rbit = *rate_idx & 0xf;
523
524 if (ridx < 0 || ridx >= NL80211_VHT_NSS_MAX)
525 return false;
526
527 /* See whether the selected rate or anything below it is allowed. */
528 for (i = ridx; i >= 0; i--) {
529 for (j = rbit; j >= 0; j--) {
530 if (vht_mask[i] & BIT(j)) {
531 *rate_idx = (i << 4) | j;
532 return true;
533 }
534 }
535 rbit = 15;
536 }
537
538 /* Try to find a higher rate that would be allowed */
539 ridx = (*rate_idx + 1) >> 4;
540 rbit = (*rate_idx + 1) & 0xf;
541
542 for (i = ridx; i < NL80211_VHT_NSS_MAX; i++) {
543 for (j = rbit; j < 16; j++) {
544 if (vht_mask[i] & BIT(j)) {
545 *rate_idx = (i << 4) | j;
546 return true;
547 }
548 }
549 rbit = 0;
550 }
551 return false;
552}
553
554static void rate_idx_match_mask(s8 *rate_idx, u16 *rate_flags,
555 struct ieee80211_supported_band *sband,
556 enum nl80211_chan_width chan_width,
557 u32 mask,
558 u8 mcs_mask[IEEE80211_HT_MCS_MASK_LEN],
559 u16 vht_mask[NL80211_VHT_NSS_MAX])
560{
561 if (*rate_flags & IEEE80211_TX_RC_VHT_MCS) {
562 /* handle VHT rates */
563 if (rate_idx_match_vht_mcs_mask(rate_idx, vht_mask))
564 return;
565
566 *rate_idx = 0;
567 /* keep protection flags */
568 *rate_flags &= (IEEE80211_TX_RC_USE_RTS_CTS |
569 IEEE80211_TX_RC_USE_CTS_PROTECT |
570 IEEE80211_TX_RC_USE_SHORT_PREAMBLE);
571
572 *rate_flags |= IEEE80211_TX_RC_MCS;
573 if (chan_width == NL80211_CHAN_WIDTH_40)
574 *rate_flags |= IEEE80211_TX_RC_40_MHZ_WIDTH;
575
576 if (rate_idx_match_mcs_mask(rate_idx, mcs_mask))
577 return;
578
579 /* also try the legacy rates. */
580 *rate_flags &= ~(IEEE80211_TX_RC_MCS |
581 IEEE80211_TX_RC_40_MHZ_WIDTH);
582 if (rate_idx_match_legacy_mask(rate_idx, n_bitrates: sband->n_bitrates,
583 mask))
584 return;
585 } else if (*rate_flags & IEEE80211_TX_RC_MCS) {
586 /* handle HT rates */
587 if (rate_idx_match_mcs_mask(rate_idx, mcs_mask))
588 return;
589
590 /* also try the legacy rates. */
591 *rate_idx = 0;
592 /* keep protection flags */
593 *rate_flags &= (IEEE80211_TX_RC_USE_RTS_CTS |
594 IEEE80211_TX_RC_USE_CTS_PROTECT |
595 IEEE80211_TX_RC_USE_SHORT_PREAMBLE);
596 if (rate_idx_match_legacy_mask(rate_idx, n_bitrates: sband->n_bitrates,
597 mask))
598 return;
599 } else {
600 /* handle legacy rates */
601 if (rate_idx_match_legacy_mask(rate_idx, n_bitrates: sband->n_bitrates,
602 mask))
603 return;
604
605 /* if HT BSS, and we handle a data frame, also try HT rates */
606 switch (chan_width) {
607 case NL80211_CHAN_WIDTH_20_NOHT:
608 case NL80211_CHAN_WIDTH_5:
609 case NL80211_CHAN_WIDTH_10:
610 return;
611 default:
612 break;
613 }
614
615 *rate_idx = 0;
616 /* keep protection flags */
617 *rate_flags &= (IEEE80211_TX_RC_USE_RTS_CTS |
618 IEEE80211_TX_RC_USE_CTS_PROTECT |
619 IEEE80211_TX_RC_USE_SHORT_PREAMBLE);
620
621 *rate_flags |= IEEE80211_TX_RC_MCS;
622
623 if (chan_width == NL80211_CHAN_WIDTH_40)
624 *rate_flags |= IEEE80211_TX_RC_40_MHZ_WIDTH;
625
626 if (rate_idx_match_mcs_mask(rate_idx, mcs_mask))
627 return;
628 }
629
630 /*
631 * Uh.. No suitable rate exists. This should not really happen with
632 * sane TX rate mask configurations. However, should someone manage to
633 * configure supported rates and TX rate mask in incompatible way,
634 * allow the frame to be transmitted with whatever the rate control
635 * selected.
636 */
637}
638
639static void rate_fixup_ratelist(struct ieee80211_vif *vif,
640 struct ieee80211_supported_band *sband,
641 struct ieee80211_tx_info *info,
642 struct ieee80211_tx_rate *rates,
643 int max_rates)
644{
645 struct ieee80211_rate *rate;
646 bool inval = false;
647 int i;
648
649 /*
650 * Set up the RTS/CTS rate as the fastest basic rate
651 * that is not faster than the data rate unless there
652 * is no basic rate slower than the data rate, in which
653 * case we pick the slowest basic rate
654 *
655 * XXX: Should this check all retry rates?
656 */
657 if (!(rates[0].flags &
658 (IEEE80211_TX_RC_MCS | IEEE80211_TX_RC_VHT_MCS))) {
659 u32 basic_rates = vif->bss_conf.basic_rates;
660 s8 baserate = basic_rates ? ffs(basic_rates) - 1 : 0;
661
662 rate = &sband->bitrates[rates[0].idx];
663
664 for (i = 0; i < sband->n_bitrates; i++) {
665 /* must be a basic rate */
666 if (!(basic_rates & BIT(i)))
667 continue;
668 /* must not be faster than the data rate */
669 if (sband->bitrates[i].bitrate > rate->bitrate)
670 continue;
671 /* maximum */
672 if (sband->bitrates[baserate].bitrate <
673 sband->bitrates[i].bitrate)
674 baserate = i;
675 }
676
677 info->control.rts_cts_rate_idx = baserate;
678 }
679
680 for (i = 0; i < max_rates; i++) {
681 /*
682 * make sure there's no valid rate following
683 * an invalid one, just in case drivers don't
684 * take the API seriously to stop at -1.
685 */
686 if (inval) {
687 rates[i].idx = -1;
688 continue;
689 }
690 if (rates[i].idx < 0) {
691 inval = true;
692 continue;
693 }
694
695 /*
696 * For now assume MCS is already set up correctly, this
697 * needs to be fixed.
698 */
699 if (rates[i].flags & IEEE80211_TX_RC_MCS) {
700 WARN_ON(rates[i].idx > 76);
701
702 if (!(rates[i].flags & IEEE80211_TX_RC_USE_RTS_CTS) &&
703 info->control.use_cts_prot)
704 rates[i].flags |=
705 IEEE80211_TX_RC_USE_CTS_PROTECT;
706 continue;
707 }
708
709 if (rates[i].flags & IEEE80211_TX_RC_VHT_MCS) {
710 WARN_ON(ieee80211_rate_get_vht_mcs(&rates[i]) > 9);
711 continue;
712 }
713
714 /* set up RTS protection if desired */
715 if (info->control.use_rts) {
716 rates[i].flags |= IEEE80211_TX_RC_USE_RTS_CTS;
717 info->control.use_cts_prot = false;
718 }
719
720 /* RC is busted */
721 if (WARN_ON_ONCE(rates[i].idx >= sband->n_bitrates)) {
722 rates[i].idx = -1;
723 continue;
724 }
725
726 rate = &sband->bitrates[rates[i].idx];
727
728 /* set up short preamble */
729 if (info->control.short_preamble &&
730 rate->flags & IEEE80211_RATE_SHORT_PREAMBLE)
731 rates[i].flags |= IEEE80211_TX_RC_USE_SHORT_PREAMBLE;
732
733 /* set up G protection */
734 if (!(rates[i].flags & IEEE80211_TX_RC_USE_RTS_CTS) &&
735 info->control.use_cts_prot &&
736 rate->flags & IEEE80211_RATE_ERP_G)
737 rates[i].flags |= IEEE80211_TX_RC_USE_CTS_PROTECT;
738 }
739}
740
741
742static void rate_control_fill_sta_table(struct ieee80211_sta *sta,
743 struct ieee80211_tx_info *info,
744 struct ieee80211_tx_rate *rates,
745 int max_rates)
746{
747 struct ieee80211_sta_rates *ratetbl = NULL;
748 int i;
749
750 if (sta && !info->control.skip_table)
751 ratetbl = rcu_dereference(sta->rates);
752
753 /* Fill remaining rate slots with data from the sta rate table. */
754 max_rates = min_t(int, max_rates, IEEE80211_TX_RATE_TABLE_SIZE);
755 for (i = 0; i < max_rates; i++) {
756 if (i < ARRAY_SIZE(info->control.rates) &&
757 info->control.rates[i].idx >= 0 &&
758 info->control.rates[i].count) {
759 if (rates != info->control.rates)
760 rates[i] = info->control.rates[i];
761 } else if (ratetbl) {
762 rates[i].idx = ratetbl->rate[i].idx;
763 rates[i].flags = ratetbl->rate[i].flags;
764 if (info->control.use_rts)
765 rates[i].count = ratetbl->rate[i].count_rts;
766 else if (info->control.use_cts_prot)
767 rates[i].count = ratetbl->rate[i].count_cts;
768 else
769 rates[i].count = ratetbl->rate[i].count;
770 } else {
771 rates[i].idx = -1;
772 rates[i].count = 0;
773 }
774
775 if (rates[i].idx < 0 || !rates[i].count)
776 break;
777 }
778}
779
780static bool rate_control_cap_mask(struct ieee80211_sub_if_data *sdata,
781 struct ieee80211_supported_band *sband,
782 struct ieee80211_sta *sta, u32 *mask,
783 u8 mcs_mask[IEEE80211_HT_MCS_MASK_LEN],
784 u16 vht_mask[NL80211_VHT_NSS_MAX])
785{
786 u32 i;
787
788 *mask = sdata->rc_rateidx_mask[sband->band];
789
790 if (*mask == (1 << sband->n_bitrates) - 1 &&
791 !sdata->rc_has_mcs_mask[sband->band] &&
792 !sdata->rc_has_vht_mcs_mask[sband->band])
793 return false;
794
795 if (sdata->rc_has_mcs_mask[sband->band])
796 memcpy(to: mcs_mask, from: sdata->rc_rateidx_mcs_mask[sband->band],
797 IEEE80211_HT_MCS_MASK_LEN);
798 else
799 memset(s: mcs_mask, c: 0xff, IEEE80211_HT_MCS_MASK_LEN);
800
801 if (sdata->rc_has_vht_mcs_mask[sband->band])
802 memcpy(to: vht_mask, from: sdata->rc_rateidx_vht_mcs_mask[sband->band],
803 len: sizeof(u16) * NL80211_VHT_NSS_MAX);
804 else
805 memset(s: vht_mask, c: 0xff, n: sizeof(u16) * NL80211_VHT_NSS_MAX);
806
807 if (sta) {
808 __le16 sta_vht_cap;
809 u16 sta_vht_mask[NL80211_VHT_NSS_MAX];
810
811 /* Filter out rates that the STA does not support */
812 *mask &= sta->deflink.supp_rates[sband->band];
813 for (i = 0; i < IEEE80211_HT_MCS_MASK_LEN; i++)
814 mcs_mask[i] &= sta->deflink.ht_cap.mcs.rx_mask[i];
815
816 sta_vht_cap = sta->deflink.vht_cap.vht_mcs.rx_mcs_map;
817 ieee80211_get_vht_mask_from_cap(vht_cap: sta_vht_cap, vht_mask: sta_vht_mask);
818 for (i = 0; i < NL80211_VHT_NSS_MAX; i++)
819 vht_mask[i] &= sta_vht_mask[i];
820 }
821
822 return true;
823}
824
825static void
826rate_control_apply_mask_ratetbl(struct sta_info *sta,
827 struct ieee80211_supported_band *sband,
828 struct ieee80211_sta_rates *rates)
829{
830 int i;
831 u32 mask;
832 u8 mcs_mask[IEEE80211_HT_MCS_MASK_LEN];
833 u16 vht_mask[NL80211_VHT_NSS_MAX];
834 enum nl80211_chan_width chan_width;
835
836 if (!rate_control_cap_mask(sdata: sta->sdata, sband, sta: &sta->sta, mask: &mask,
837 mcs_mask, vht_mask))
838 return;
839
840 chan_width = sta->sdata->vif.bss_conf.chanreq.oper.width;
841 for (i = 0; i < IEEE80211_TX_RATE_TABLE_SIZE; i++) {
842 if (rates->rate[i].idx < 0)
843 break;
844
845 rate_idx_match_mask(rate_idx: &rates->rate[i].idx, rate_flags: &rates->rate[i].flags,
846 sband, chan_width, mask, mcs_mask,
847 vht_mask);
848 }
849}
850
851static void rate_control_apply_mask(struct ieee80211_sub_if_data *sdata,
852 struct ieee80211_sta *sta,
853 struct ieee80211_supported_band *sband,
854 struct ieee80211_tx_rate *rates,
855 int max_rates)
856{
857 enum nl80211_chan_width chan_width;
858 u8 mcs_mask[IEEE80211_HT_MCS_MASK_LEN];
859 u32 mask;
860 u16 rate_flags, vht_mask[NL80211_VHT_NSS_MAX];
861 int i;
862
863 /*
864 * Try to enforce the rateidx mask the user wanted. skip this if the
865 * default mask (allow all rates) is used to save some processing for
866 * the common case.
867 */
868 if (!rate_control_cap_mask(sdata, sband, sta, mask: &mask, mcs_mask,
869 vht_mask))
870 return;
871
872 /*
873 * Make sure the rate index selected for each TX rate is
874 * included in the configured mask and change the rate indexes
875 * if needed.
876 */
877 chan_width = sdata->vif.bss_conf.chanreq.oper.width;
878 for (i = 0; i < max_rates; i++) {
879 /* Skip invalid rates */
880 if (rates[i].idx < 0)
881 break;
882
883 rate_flags = rates[i].flags;
884 rate_idx_match_mask(rate_idx: &rates[i].idx, rate_flags: &rate_flags, sband,
885 chan_width, mask, mcs_mask, vht_mask);
886 rates[i].flags = rate_flags;
887 }
888}
889
890void ieee80211_get_tx_rates(struct ieee80211_vif *vif,
891 struct ieee80211_sta *sta,
892 struct sk_buff *skb,
893 struct ieee80211_tx_rate *dest,
894 int max_rates)
895{
896 struct ieee80211_sub_if_data *sdata;
897 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
898 struct ieee80211_supported_band *sband;
899 u32 mask = ~0;
900
901 rate_control_fill_sta_table(sta, info, rates: dest, max_rates);
902
903 if (!vif)
904 return;
905
906 sdata = vif_to_sdata(p: vif);
907 if (info->band >= NUM_NL80211_BANDS)
908 return;
909
910 sband = sdata->local->hw.wiphy->bands[info->band];
911
912 if (ieee80211_is_tx_data(skb))
913 rate_control_apply_mask(sdata, sta, sband, rates: dest, max_rates);
914
915 if (!(info->control.flags & IEEE80211_TX_CTRL_DONT_USE_RATE_MASK))
916 mask = sdata->rc_rateidx_mask[info->band];
917
918 if (dest[0].idx < 0)
919 __rate_control_send_low(hw: &sdata->local->hw, sband, sta, info,
920 rate_mask: mask);
921
922 if (sta)
923 rate_fixup_ratelist(vif, sband, info, rates: dest, max_rates);
924}
925EXPORT_SYMBOL(ieee80211_get_tx_rates);
926
927void rate_control_get_rate(struct ieee80211_sub_if_data *sdata,
928 struct sta_info *sta,
929 struct ieee80211_tx_rate_control *txrc)
930{
931 struct rate_control_ref *ref = sdata->local->rate_ctrl;
932 void *priv_sta = NULL;
933 struct ieee80211_sta *ista = NULL;
934 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb: txrc->skb);
935 int i;
936
937 for (i = 0; i < IEEE80211_TX_MAX_RATES; i++) {
938 info->control.rates[i].idx = -1;
939 info->control.rates[i].flags = 0;
940 info->control.rates[i].count = 0;
941 }
942
943 if (rate_control_send_low(pubsta: sta ? &sta->sta : NULL, txrc))
944 return;
945
946 if (ieee80211_hw_check(&sdata->local->hw, HAS_RATE_CONTROL))
947 return;
948
949 if (sta && test_sta_flag(sta, flag: WLAN_STA_RATE_CONTROL)) {
950 ista = &sta->sta;
951 priv_sta = sta->rate_ctrl_priv;
952 }
953
954 if (ista) {
955 spin_lock_bh(lock: &sta->rate_ctrl_lock);
956 ref->ops->get_rate(ref->priv, ista, priv_sta, txrc);
957 spin_unlock_bh(lock: &sta->rate_ctrl_lock);
958 } else {
959 rate_control_send_low(NULL, txrc);
960 }
961
962 if (ieee80211_hw_check(&sdata->local->hw, SUPPORTS_RC_TABLE))
963 return;
964
965 ieee80211_get_tx_rates(&sdata->vif, ista, txrc->skb,
966 info->control.rates,
967 ARRAY_SIZE(info->control.rates));
968}
969
970int rate_control_set_rates(struct ieee80211_hw *hw,
971 struct ieee80211_sta *pubsta,
972 struct ieee80211_sta_rates *rates)
973{
974 struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
975 struct ieee80211_sta_rates *old;
976 struct ieee80211_supported_band *sband;
977
978 sband = ieee80211_get_sband(sdata: sta->sdata);
979 if (!sband)
980 return -EINVAL;
981 rate_control_apply_mask_ratetbl(sta, sband, rates);
982 /*
983 * mac80211 guarantees that this function will not be called
984 * concurrently, so the following RCU access is safe, even without
985 * extra locking. This can not be checked easily, so we just set
986 * the condition to true.
987 */
988 old = rcu_dereference_protected(pubsta->rates, true);
989 rcu_assign_pointer(pubsta->rates, rates);
990 if (old)
991 kfree_rcu(old, rcu_head);
992
993 if (sta->uploaded)
994 drv_sta_rate_tbl_update(local: hw_to_local(hw), sdata: sta->sdata, sta: pubsta);
995
996 return 0;
997}
998EXPORT_SYMBOL(rate_control_set_rates);
999
1000int ieee80211_init_rate_ctrl_alg(struct ieee80211_local *local,
1001 const char *name)
1002{
1003 struct rate_control_ref *ref;
1004
1005 ASSERT_RTNL();
1006
1007 if (local->open_count)
1008 return -EBUSY;
1009
1010 if (ieee80211_hw_check(&local->hw, HAS_RATE_CONTROL)) {
1011 if (WARN_ON(!local->ops->set_rts_threshold))
1012 return -EINVAL;
1013 return 0;
1014 }
1015
1016 ref = rate_control_alloc(name, local);
1017 if (!ref) {
1018 wiphy_warn(local->hw.wiphy,
1019 "Failed to select rate control algorithm\n");
1020 return -ENOENT;
1021 }
1022
1023 WARN_ON(local->rate_ctrl);
1024 local->rate_ctrl = ref;
1025
1026 wiphy_debug(local->hw.wiphy, "Selected rate control algorithm '%s'\n",
1027 ref->ops->name);
1028
1029 return 0;
1030}
1031
1032void rate_control_deinitialize(struct ieee80211_local *local)
1033{
1034 struct rate_control_ref *ref;
1035
1036 ref = local->rate_ctrl;
1037
1038 if (!ref)
1039 return;
1040
1041 local->rate_ctrl = NULL;
1042 rate_control_free(local, ctrl_ref: ref);
1043}
1044