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