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