blob: 45927202c71c6e0664331e2347cb40c9ed1170c3 [file] [log] [blame]
Thomas Gleixnerd2912cb2019-06-04 10:11:33 +02001// SPDX-License-Identifier: GPL-2.0-only
Jiri Bencf0706e82007-05-05 11:45:53 -07002/*
3 * Copyright 2002-2005, Instant802 Networks, Inc.
4 * Copyright 2005-2006, Devicescape Software, Inc.
5 * Copyright (c) 2006 Jiri Benc <jbenc@suse.cz>
Johannes Berge8e4f522017-03-08 11:12:10 +01006 * Copyright 2017 Intel Deutschland GmbH
Jiri Bencf0706e82007-05-05 11:45:53 -07007 */
8
9#include <linux/kernel.h>
Johannes Bergff688082007-07-27 15:43:23 +020010#include <linux/rtnetlink.h>
Paul Gortmaker3a9a2312011-05-27 09:12:25 -040011#include <linux/module.h>
Johannes Bergcc01f9b2014-01-22 10:36:59 +010012#include <linux/slab.h>
Johannes Berg2c8dccc2008-04-08 15:14:40 -040013#include "rate.h"
Jiri Bencf0706e82007-05-05 11:45:53 -070014#include "ieee80211_i.h"
Johannes Berg4b7679a2008-09-18 18:14:18 +020015#include "debugfs.h"
Jiri Bencf0706e82007-05-05 11:45:53 -070016
17struct rate_control_alg {
18 struct list_head list;
Johannes Berg631ad702014-01-20 23:29:34 +010019 const struct rate_control_ops *ops;
Jiri Bencf0706e82007-05-05 11:45:53 -070020};
21
22static LIST_HEAD(rate_ctrl_algs);
23static DEFINE_MUTEX(rate_ctrl_mutex);
24
Stefano Brivioc21b39a2007-12-19 01:26:16 +010025static 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
Denys Vlasenkoeb6d9292015-07-15 14:56:06 +020030void 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
Thomas Pedersencac8c522020-09-21 19:28:13 -070054 /* TODO: check for minstrel_s1g ? */
55 if (sband->band == NL80211_BAND_S1GHZ) {
Thomas Pedersen12bf8fa2020-10-05 09:45:22 -070056 ieee80211_s1g_sta_rate_init(sta);
Thomas Pedersencac8c522020-09-21 19:28:13 -070057 rcu_read_unlock();
58 return;
59 }
60
Denys Vlasenkoeb6d9292015-07-15 14:56:06 +020061 spin_lock_bh(&sta->rate_ctrl_lock);
62 ref->ops->rate_init(ref->priv, sband, &chanctx_conf->def, ista,
63 priv_sta);
64 spin_unlock_bh(&sta->rate_ctrl_lock);
65 rcu_read_unlock();
66 set_sta_flag(sta, WLAN_STA_RATE_CONTROL);
67}
68
Felix Fietkau18fb84d2017-04-26 17:11:35 +020069void rate_control_tx_status(struct ieee80211_local *local,
70 struct ieee80211_supported_band *sband,
71 struct ieee80211_tx_status *st)
72{
73 struct rate_control_ref *ref = local->rate_ctrl;
74 struct sta_info *sta = container_of(st->sta, struct sta_info, sta);
75 void *priv_sta = sta->rate_ctrl_priv;
76
77 if (!ref || !test_sta_flag(sta, WLAN_STA_RATE_CONTROL))
78 return;
79
80 spin_lock_bh(&sta->rate_ctrl_lock);
81 if (ref->ops->tx_status_ext)
82 ref->ops->tx_status_ext(ref->priv, sband, priv_sta, st);
83 else if (st->skb)
84 ref->ops->tx_status(ref->priv, sband, st->sta, priv_sta, st->skb);
85 else
86 WARN_ON_ONCE(1);
87
88 spin_unlock_bh(&sta->rate_ctrl_lock);
89}
90
Denys Vlasenkoeb6d9292015-07-15 14:56:06 +020091void rate_control_rate_update(struct ieee80211_local *local,
92 struct ieee80211_supported_band *sband,
93 struct sta_info *sta, u32 changed)
94{
95 struct rate_control_ref *ref = local->rate_ctrl;
96 struct ieee80211_sta *ista = &sta->sta;
97 void *priv_sta = sta->rate_ctrl_priv;
98 struct ieee80211_chanctx_conf *chanctx_conf;
99
100 if (ref && ref->ops->rate_update) {
101 rcu_read_lock();
102
103 chanctx_conf = rcu_dereference(sta->sdata->vif.chanctx_conf);
104 if (WARN_ON(!chanctx_conf)) {
105 rcu_read_unlock();
106 return;
107 }
108
109 spin_lock_bh(&sta->rate_ctrl_lock);
110 ref->ops->rate_update(ref->priv, sband, &chanctx_conf->def,
111 ista, priv_sta, changed);
112 spin_unlock_bh(&sta->rate_ctrl_lock);
113 rcu_read_unlock();
114 }
115 drv_sta_rc_update(local, sta->sdata, &sta->sta, changed);
116}
117
Johannes Berg631ad702014-01-20 23:29:34 +0100118int ieee80211_rate_control_register(const struct rate_control_ops *ops)
Jiri Bencf0706e82007-05-05 11:45:53 -0700119{
120 struct rate_control_alg *alg;
121
Johannes Bergac71c692007-10-28 14:17:44 +0100122 if (!ops->name)
123 return -EINVAL;
124
Johannes Berg999acd92007-10-28 14:49:33 +0100125 mutex_lock(&rate_ctrl_mutex);
126 list_for_each_entry(alg, &rate_ctrl_algs, list) {
127 if (!strcmp(alg->ops->name, ops->name)) {
128 /* don't register an algorithm twice */
129 WARN_ON(1);
Cyrill Gorcunovb808ab12007-12-13 15:52:11 -0800130 mutex_unlock(&rate_ctrl_mutex);
Johannes Berg999acd92007-10-28 14:49:33 +0100131 return -EALREADY;
132 }
133 }
134
Yoann Padioleaudd00cc42007-07-19 01:49:03 -0700135 alg = kzalloc(sizeof(*alg), GFP_KERNEL);
Jiri Bencf0706e82007-05-05 11:45:53 -0700136 if (alg == NULL) {
Johannes Berg999acd92007-10-28 14:49:33 +0100137 mutex_unlock(&rate_ctrl_mutex);
Jiri Bencf0706e82007-05-05 11:45:53 -0700138 return -ENOMEM;
139 }
Jiri Bencf0706e82007-05-05 11:45:53 -0700140 alg->ops = ops;
141
Jiri Bencf0706e82007-05-05 11:45:53 -0700142 list_add_tail(&alg->list, &rate_ctrl_algs);
143 mutex_unlock(&rate_ctrl_mutex);
144
145 return 0;
146}
147EXPORT_SYMBOL(ieee80211_rate_control_register);
148
Johannes Berg631ad702014-01-20 23:29:34 +0100149void ieee80211_rate_control_unregister(const struct rate_control_ops *ops)
Jiri Bencf0706e82007-05-05 11:45:53 -0700150{
151 struct rate_control_alg *alg;
152
153 mutex_lock(&rate_ctrl_mutex);
154 list_for_each_entry(alg, &rate_ctrl_algs, list) {
155 if (alg->ops == ops) {
156 list_del(&alg->list);
Cyrill Gorcunov20880e82007-12-13 16:17:03 -0800157 kfree(alg);
Jiri Bencf0706e82007-05-05 11:45:53 -0700158 break;
159 }
160 }
161 mutex_unlock(&rate_ctrl_mutex);
Jiri Bencf0706e82007-05-05 11:45:53 -0700162}
163EXPORT_SYMBOL(ieee80211_rate_control_unregister);
164
Johannes Berg631ad702014-01-20 23:29:34 +0100165static const struct rate_control_ops *
Jiri Bencf0706e82007-05-05 11:45:53 -0700166ieee80211_try_rate_control_ops_get(const char *name)
167{
168 struct rate_control_alg *alg;
Johannes Berg631ad702014-01-20 23:29:34 +0100169 const struct rate_control_ops *ops = NULL;
Jiri Bencf0706e82007-05-05 11:45:53 -0700170
Johannes Bergac71c692007-10-28 14:17:44 +0100171 if (!name)
172 return NULL;
173
Jiri Bencf0706e82007-05-05 11:45:53 -0700174 mutex_lock(&rate_ctrl_mutex);
175 list_for_each_entry(alg, &rate_ctrl_algs, list) {
Johannes Bergcc01f9b2014-01-22 10:36:59 +0100176 if (!strcmp(alg->ops->name, name)) {
177 ops = alg->ops;
178 break;
179 }
Jiri Bencf0706e82007-05-05 11:45:53 -0700180 }
181 mutex_unlock(&rate_ctrl_mutex);
182 return ops;
183}
184
Stefano Brivioc21b39a2007-12-19 01:26:16 +0100185/* Get the rate control algorithm. */
Johannes Berg631ad702014-01-20 23:29:34 +0100186static const struct rate_control_ops *
Jiri Bencf0706e82007-05-05 11:45:53 -0700187ieee80211_rate_control_ops_get(const char *name)
188{
Johannes Berg631ad702014-01-20 23:29:34 +0100189 const struct rate_control_ops *ops;
Stefano Brivioc21b39a2007-12-19 01:26:16 +0100190 const char *alg_name;
Jiri Bencf0706e82007-05-05 11:45:53 -0700191
Dan Streetmanb51d23e2015-06-17 06:18:52 +0930192 kernel_param_lock(THIS_MODULE);
Johannes Bergac71c692007-10-28 14:17:44 +0100193 if (!name)
Stefano Brivioc21b39a2007-12-19 01:26:16 +0100194 alg_name = ieee80211_default_rc_algo;
195 else
196 alg_name = name;
Johannes Bergac71c692007-10-28 14:17:44 +0100197
Stefano Brivioc21b39a2007-12-19 01:26:16 +0100198 ops = ieee80211_try_rate_control_ops_get(alg_name);
Stefano Brivioc21b39a2007-12-19 01:26:16 +0100199 if (!ops && name)
200 /* try default if specific alg requested but not found */
201 ops = ieee80211_try_rate_control_ops_get(ieee80211_default_rc_algo);
202
Matthias Kaehlcke93f56de2017-04-06 16:31:41 -0700203 /* Note: check for > 0 is intentional to avoid clang warning */
204 if (!ops && (strlen(CONFIG_MAC80211_RC_DEFAULT) > 0))
205 /* try built-in one if specific alg requested but not found */
Johannes Berg4b475892008-01-02 15:17:03 +0100206 ops = ieee80211_try_rate_control_ops_get(CONFIG_MAC80211_RC_DEFAULT);
Matthias Kaehlcke93f56de2017-04-06 16:31:41 -0700207
Dan Streetmanb51d23e2015-06-17 06:18:52 +0930208 kernel_param_unlock(THIS_MODULE);
Johannes Berg4b475892008-01-02 15:17:03 +0100209
Jiri Bencf0706e82007-05-05 11:45:53 -0700210 return ops;
211}
212
Johannes Berg4b7679a2008-09-18 18:14:18 +0200213#ifdef CONFIG_MAC80211_DEBUGFS
214static ssize_t rcname_read(struct file *file, char __user *userbuf,
215 size_t count, loff_t *ppos)
216{
217 struct rate_control_ref *ref = file->private_data;
218 int len = strlen(ref->ops->name);
219
220 return simple_read_from_buffer(userbuf, count, ppos,
221 ref->ops->name, len);
222}
223
Johannes Berg6cb5f3e2020-04-23 11:13:49 +0200224const struct file_operations rcname_ops = {
Johannes Berg4b7679a2008-09-18 18:14:18 +0200225 .read = rcname_read,
Stephen Boyd234e3402012-04-05 14:25:11 -0700226 .open = simple_open,
Arnd Bergmann6038f372010-08-15 18:52:59 +0200227 .llseek = default_llseek,
Johannes Berg4b7679a2008-09-18 18:14:18 +0200228};
229#endif
230
Johannes Berg6cb5f3e2020-04-23 11:13:49 +0200231static struct rate_control_ref *
232rate_control_alloc(const char *name, struct ieee80211_local *local)
Jiri Bencf0706e82007-05-05 11:45:53 -0700233{
234 struct rate_control_ref *ref;
235
236 ref = kmalloc(sizeof(struct rate_control_ref), GFP_KERNEL);
237 if (!ref)
Johannes Bergcc01f9b2014-01-22 10:36:59 +0100238 return NULL;
Jiri Bencf0706e82007-05-05 11:45:53 -0700239 ref->ops = ieee80211_rate_control_ops_get(name);
240 if (!ref->ops)
Johannes Bergcc01f9b2014-01-22 10:36:59 +0100241 goto free;
Johannes Berg4b7679a2008-09-18 18:14:18 +0200242
Johannes Berg6cb5f3e2020-04-23 11:13:49 +0200243 ref->priv = ref->ops->alloc(&local->hw);
Jiri Bencf0706e82007-05-05 11:45:53 -0700244 if (!ref->priv)
Johannes Bergcc01f9b2014-01-22 10:36:59 +0100245 goto free;
Jiri Bencf0706e82007-05-05 11:45:53 -0700246 return ref;
247
Johannes Bergcc01f9b2014-01-22 10:36:59 +0100248free:
Jiri Bencf0706e82007-05-05 11:45:53 -0700249 kfree(ref);
Jiri Bencf0706e82007-05-05 11:45:53 -0700250 return NULL;
251}
252
Johannes Berga8589582017-02-15 15:02:07 +0100253static void rate_control_free(struct ieee80211_local *local,
254 struct rate_control_ref *ctrl_ref)
Jiri Bencf0706e82007-05-05 11:45:53 -0700255{
Jiri Bencf0706e82007-05-05 11:45:53 -0700256 ctrl_ref->ops->free(ctrl_ref->priv);
Johannes Berg4b7679a2008-09-18 18:14:18 +0200257
258#ifdef CONFIG_MAC80211_DEBUGFS
Johannes Berga8589582017-02-15 15:02:07 +0100259 debugfs_remove_recursive(local->debugfs.rcdir);
260 local->debugfs.rcdir = NULL;
Johannes Berg4b7679a2008-09-18 18:14:18 +0200261#endif
262
Jiri Bencf0706e82007-05-05 11:45:53 -0700263 kfree(ctrl_ref);
264}
265
Johannes Berge8e4f522017-03-08 11:12:10 +0100266void 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
Thomas Pedersen1821f8b2020-09-21 19:28:12 -0700276 band = sdata->vif.bss_conf.chandef.chan->band;
277 if (band == NL80211_BAND_S1GHZ) {
278 /* TODO */
279 return;
280 }
281
Johannes Berge8e4f522017-03-08 11:12:10 +0100282 if (WARN_ON_ONCE(!basic_rates))
283 return;
284
Johannes Berge8e4f522017-03-08 11:12:10 +0100285 user_mask = sdata->rc_rateidx_mask[band];
286 sband = local->hw.wiphy->bands[band];
287
288 if (user_mask & basic_rates)
289 return;
290
291 sdata_dbg(sdata,
292 "no overlap between basic rates (0x%x) and user mask (0x%x on band %d) - clearing the latter",
293 basic_rates, user_mask, band);
294 sdata->rc_rateidx_mask[band] = (1 << sband->n_bitrates) - 1;
295}
296
Rajkumar Manoharanb6f35302011-09-29 20:34:04 +0530297static bool rc_no_data_or_no_ack_use_min(struct ieee80211_tx_rate_control *txrc)
Luis R. Rodriguez4c6d4f52009-07-16 10:05:41 -0700298{
299 struct sk_buff *skb = txrc->skb;
300 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
301 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
302 __le16 fc;
303
304 fc = hdr->frame_control;
305
Rajkumar Manoharanb6f35302011-09-29 20:34:04 +0530306 return (info->flags & (IEEE80211_TX_CTL_NO_ACK |
307 IEEE80211_TX_CTL_USE_MINRATE)) ||
308 !ieee80211_is_data(fc);
Luis R. Rodriguez4c6d4f52009-07-16 10:05:41 -0700309}
310
Thomas Pedersen1821f8b2020-09-21 19:28:12 -0700311static void rc_send_low_basicrate(struct ieee80211_tx_rate *rate,
312 u32 basic_rates,
Felix Fietkau8f0729b2010-11-11 15:07:23 +0100313 struct ieee80211_supported_band *sband)
Jouni Malinene00cfce2009-12-29 12:59:19 +0200314{
315 u8 i;
316
Thomas Pedersen1821f8b2020-09-21 19:28:12 -0700317 if (sband->band == NL80211_BAND_S1GHZ) {
318 /* TODO */
319 rate->flags |= IEEE80211_TX_RC_S1G_MCS;
320 rate->idx = 0;
321 return;
322 }
323
Jouni Malinene00cfce2009-12-29 12:59:19 +0200324 if (basic_rates == 0)
325 return; /* assume basic rates unknown and accept rate */
Thomas Pedersen1821f8b2020-09-21 19:28:12 -0700326 if (rate->idx < 0)
Jouni Malinene00cfce2009-12-29 12:59:19 +0200327 return;
Thomas Pedersen1821f8b2020-09-21 19:28:12 -0700328 if (basic_rates & (1 << rate->idx))
Jouni Malinene00cfce2009-12-29 12:59:19 +0200329 return; /* selected rate is a basic rate */
330
Thomas Pedersen1821f8b2020-09-21 19:28:12 -0700331 for (i = rate->idx + 1; i <= sband->n_bitrates; i++) {
Jouni Malinene00cfce2009-12-29 12:59:19 +0200332 if (basic_rates & (1 << i)) {
Thomas Pedersen1821f8b2020-09-21 19:28:12 -0700333 rate->idx = i;
Jouni Malinene00cfce2009-12-29 12:59:19 +0200334 return;
335 }
336 }
337
338 /* could not find a basic rate; use original selection */
339}
340
Felix Fietkau0d528d82013-04-22 16:14:41 +0200341static void __rate_control_send_low(struct ieee80211_hw *hw,
342 struct ieee80211_supported_band *sband,
343 struct ieee80211_sta *sta,
Andrei Otcheretianski1d2d3502013-10-14 11:04:28 +0200344 struct ieee80211_tx_info *info,
345 u32 rate_mask)
Felix Fietkau0d528d82013-04-22 16:14:41 +0200346{
Simon Wunderlich2103dec2013-07-08 16:55:53 +0200347 int i;
348 u32 rate_flags =
349 ieee80211_chandef_rate_flags(&hw->conf.chandef);
350
Thomas Pedersen1821f8b2020-09-21 19:28:12 -0700351 if (sband->band == NL80211_BAND_S1GHZ) {
352 info->control.rates[0].flags |= IEEE80211_TX_RC_S1G_MCS;
353 info->control.rates[0].idx = 0;
354 return;
355 }
356
Johannes Berg57fbcce2016-04-12 15:56:15 +0200357 if ((sband->band == NL80211_BAND_2GHZ) &&
Simon Wunderlich2103dec2013-07-08 16:55:53 +0200358 (info->flags & IEEE80211_TX_CTL_NO_CCK_RATE))
359 rate_flags |= IEEE80211_RATE_ERP_G;
360
361 info->control.rates[0].idx = 0;
362 for (i = 0; i < sband->n_bitrates; i++) {
Andrei Otcheretianski1d2d3502013-10-14 11:04:28 +0200363 if (!(rate_mask & BIT(i)))
364 continue;
365
Andrei Otcheretianski1431fcb2013-10-14 10:46:55 +0200366 if ((rate_flags & sband->bitrates[i].flags) != rate_flags)
367 continue;
368
Simon Wunderlich2103dec2013-07-08 16:55:53 +0200369 if (!rate_supported(sta, sband->band, i))
370 continue;
371
372 info->control.rates[0].idx = i;
373 break;
374 }
Johannes Berg0e5c3712015-09-23 14:02:47 +0200375 WARN_ONCE(i == sband->n_bitrates,
Johannes Berg163a7cd2019-05-29 15:25:37 +0300376 "no supported rates for sta %pM (0x%x, band %d) in rate_mask 0x%x with flags 0x%x\n",
377 sta ? sta->addr : NULL,
Johannes Bergef95d8b2015-10-25 10:59:42 +0200378 sta ? sta->supp_rates[sband->band] : -1,
Johannes Berg163a7cd2019-05-29 15:25:37 +0300379 sband->band,
Johannes Berg0e5c3712015-09-23 14:02:47 +0200380 rate_mask, rate_flags);
Felix Fietkau0d528d82013-04-22 16:14:41 +0200381
382 info->control.rates[0].count =
383 (info->flags & IEEE80211_TX_CTL_NO_ACK) ?
384 1 : hw->max_rate_tries;
385
386 info->control.skip_table = 1;
387}
388
Rajkumar Manoharanaad14ce2011-09-25 14:53:31 +0530389
Johannes Berg1e87fec2019-05-16 11:44:52 +0200390static bool rate_control_send_low(struct ieee80211_sta *pubsta,
391 struct ieee80211_tx_rate_control *txrc)
Luis R. Rodriguez4c6d4f52009-07-16 10:05:41 -0700392{
393 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(txrc->skb);
Felix Fietkaudd5b4cc2010-11-22 20:58:24 +0100394 struct ieee80211_supported_band *sband = txrc->sband;
Chun-Yeow Yeoh06703072013-08-06 15:39:56 -0700395 struct sta_info *sta;
Felix Fietkaudd5b4cc2010-11-22 20:58:24 +0100396 int mcast_rate;
Chun-Yeow Yeoh06703072013-08-06 15:39:56 -0700397 bool use_basicrate = false;
Luis R. Rodriguez4c6d4f52009-07-16 10:05:41 -0700398
Johannes Berg1e87fec2019-05-16 11:44:52 +0200399 if (!pubsta || rc_no_data_or_no_ack_use_min(txrc)) {
Andrei Otcheretianski1d2d3502013-10-14 11:04:28 +0200400 __rate_control_send_low(txrc->hw, sband, pubsta, info,
401 txrc->rate_idx_mask);
Felix Fietkau0d528d82013-04-22 16:14:41 +0200402
Chun-Yeow Yeoh06703072013-08-06 15:39:56 -0700403 if (!pubsta && txrc->bss) {
Felix Fietkaudd5b4cc2010-11-22 20:58:24 +0100404 mcast_rate = txrc->bss_conf->mcast_rate[sband->band];
405 if (mcast_rate > 0) {
406 info->control.rates[0].idx = mcast_rate - 1;
407 return true;
408 }
Chun-Yeow Yeoh06703072013-08-06 15:39:56 -0700409 use_basicrate = true;
410 } else if (pubsta) {
411 sta = container_of(pubsta, struct sta_info, sta);
412 if (ieee80211_vif_is_mesh(&sta->sdata->vif))
413 use_basicrate = true;
414 }
Felix Fietkaudd5b4cc2010-11-22 20:58:24 +0100415
Chun-Yeow Yeoh06703072013-08-06 15:39:56 -0700416 if (use_basicrate)
Thomas Pedersen1821f8b2020-09-21 19:28:12 -0700417 rc_send_low_basicrate(&info->control.rates[0],
Jouni Malinene00cfce2009-12-29 12:59:19 +0200418 txrc->bss_conf->basic_rates,
Felix Fietkaudd5b4cc2010-11-22 20:58:24 +0100419 sband);
Chun-Yeow Yeoh06703072013-08-06 15:39:56 -0700420
Luis R. Rodriguez4c6d4f52009-07-16 10:05:41 -0700421 return true;
422 }
423 return false;
424}
Luis R. Rodriguez4c6d4f52009-07-16 10:05:41 -0700425
Lorenzo Bianconi90c66bd2015-08-06 23:47:31 +0200426static bool rate_idx_match_legacy_mask(s8 *rate_idx, int n_bitrates, u32 mask)
Jouni Malinen37eb0b12010-01-06 13:09:08 +0200427{
428 int j;
429
430 /* See whether the selected rate or anything below it is allowed. */
Lorenzo Bianconi90c66bd2015-08-06 23:47:31 +0200431 for (j = *rate_idx; j >= 0; j--) {
Jouni Malinen37eb0b12010-01-06 13:09:08 +0200432 if (mask & (1 << j)) {
433 /* Okay, found a suitable rate. Use it. */
Lorenzo Bianconi90c66bd2015-08-06 23:47:31 +0200434 *rate_idx = j;
Simon Wunderlich19468412012-01-28 17:25:33 +0100435 return true;
Jouni Malinen37eb0b12010-01-06 13:09:08 +0200436 }
437 }
438
439 /* Try to find a higher rate that would be allowed */
Lorenzo Bianconi90c66bd2015-08-06 23:47:31 +0200440 for (j = *rate_idx + 1; j < n_bitrates; j++) {
Jouni Malinen37eb0b12010-01-06 13:09:08 +0200441 if (mask & (1 << j)) {
442 /* Okay, found a suitable rate. Use it. */
Lorenzo Bianconi90c66bd2015-08-06 23:47:31 +0200443 *rate_idx = j;
Simon Wunderlich19468412012-01-28 17:25:33 +0100444 return true;
445 }
446 }
447 return false;
448}
449
Lorenzo Bianconi90c66bd2015-08-06 23:47:31 +0200450static bool rate_idx_match_mcs_mask(s8 *rate_idx, u8 *mcs_mask)
Simon Wunderlich19468412012-01-28 17:25:33 +0100451{
452 int i, j;
453 int ridx, rbit;
454
Lorenzo Bianconi90c66bd2015-08-06 23:47:31 +0200455 ridx = *rate_idx / 8;
456 rbit = *rate_idx % 8;
Simon Wunderlich19468412012-01-28 17:25:33 +0100457
458 /* sanity check */
Dan Carpenter910570b52012-02-01 10:42:11 +0300459 if (ridx < 0 || ridx >= IEEE80211_HT_MCS_MASK_LEN)
Simon Wunderlich19468412012-01-28 17:25:33 +0100460 return false;
461
462 /* See whether the selected rate or anything below it is allowed. */
463 for (i = ridx; i >= 0; i--) {
464 for (j = rbit; j >= 0; j--)
465 if (mcs_mask[i] & BIT(j)) {
Lorenzo Bianconi90c66bd2015-08-06 23:47:31 +0200466 *rate_idx = i * 8 + j;
Simon Wunderlich19468412012-01-28 17:25:33 +0100467 return true;
468 }
469 rbit = 7;
470 }
471
472 /* Try to find a higher rate that would be allowed */
Lorenzo Bianconi90c66bd2015-08-06 23:47:31 +0200473 ridx = (*rate_idx + 1) / 8;
474 rbit = (*rate_idx + 1) % 8;
Simon Wunderlich19468412012-01-28 17:25:33 +0100475
476 for (i = ridx; i < IEEE80211_HT_MCS_MASK_LEN; i++) {
477 for (j = rbit; j < 8; j++)
478 if (mcs_mask[i] & BIT(j)) {
Lorenzo Bianconi90c66bd2015-08-06 23:47:31 +0200479 *rate_idx = i * 8 + j;
Simon Wunderlich19468412012-01-28 17:25:33 +0100480 return true;
481 }
482 rbit = 0;
483 }
484 return false;
485}
486
Lorenzo Bianconib119ad62015-08-06 23:47:33 +0200487static bool rate_idx_match_vht_mcs_mask(s8 *rate_idx, u16 *vht_mask)
488{
489 int i, j;
490 int ridx, rbit;
Simon Wunderlich19468412012-01-28 17:25:33 +0100491
Lorenzo Bianconib119ad62015-08-06 23:47:33 +0200492 ridx = *rate_idx >> 4;
493 rbit = *rate_idx & 0xf;
494
495 if (ridx < 0 || ridx >= NL80211_VHT_NSS_MAX)
496 return false;
497
498 /* See whether the selected rate or anything below it is allowed. */
499 for (i = ridx; i >= 0; i--) {
500 for (j = rbit; j >= 0; j--) {
501 if (vht_mask[i] & BIT(j)) {
502 *rate_idx = (i << 4) | j;
503 return true;
504 }
505 }
506 rbit = 15;
507 }
508
509 /* Try to find a higher rate that would be allowed */
510 ridx = (*rate_idx + 1) >> 4;
511 rbit = (*rate_idx + 1) & 0xf;
512
513 for (i = ridx; i < NL80211_VHT_NSS_MAX; i++) {
514 for (j = rbit; j < 16; j++) {
515 if (vht_mask[i] & BIT(j)) {
516 *rate_idx = (i << 4) | j;
517 return true;
518 }
519 }
520 rbit = 0;
521 }
522 return false;
523}
Simon Wunderlich19468412012-01-28 17:25:33 +0100524
Lorenzo Bianconi90c66bd2015-08-06 23:47:31 +0200525static void rate_idx_match_mask(s8 *rate_idx, u16 *rate_flags,
Felix Fietkau0d528d82013-04-22 16:14:41 +0200526 struct ieee80211_supported_band *sband,
527 enum nl80211_chan_width chan_width,
Simon Wunderlich19468412012-01-28 17:25:33 +0100528 u32 mask,
Lorenzo Bianconib119ad62015-08-06 23:47:33 +0200529 u8 mcs_mask[IEEE80211_HT_MCS_MASK_LEN],
530 u16 vht_mask[NL80211_VHT_NSS_MAX])
Simon Wunderlich19468412012-01-28 17:25:33 +0100531{
Lorenzo Bianconib119ad62015-08-06 23:47:33 +0200532 if (*rate_flags & IEEE80211_TX_RC_VHT_MCS) {
533 /* handle VHT rates */
534 if (rate_idx_match_vht_mcs_mask(rate_idx, vht_mask))
535 return;
536
537 *rate_idx = 0;
538 /* keep protection flags */
539 *rate_flags &= (IEEE80211_TX_RC_USE_RTS_CTS |
540 IEEE80211_TX_RC_USE_CTS_PROTECT |
541 IEEE80211_TX_RC_USE_SHORT_PREAMBLE);
542
543 *rate_flags |= IEEE80211_TX_RC_MCS;
544 if (chan_width == NL80211_CHAN_WIDTH_40)
545 *rate_flags |= IEEE80211_TX_RC_40_MHZ_WIDTH;
546
547 if (rate_idx_match_mcs_mask(rate_idx, mcs_mask))
548 return;
549
550 /* also try the legacy rates. */
551 *rate_flags &= ~(IEEE80211_TX_RC_MCS |
552 IEEE80211_TX_RC_40_MHZ_WIDTH);
553 if (rate_idx_match_legacy_mask(rate_idx, sband->n_bitrates,
554 mask))
555 return;
556 } else if (*rate_flags & IEEE80211_TX_RC_MCS) {
557 /* handle HT rates */
Lorenzo Bianconi90c66bd2015-08-06 23:47:31 +0200558 if (rate_idx_match_mcs_mask(rate_idx, mcs_mask))
Simon Wunderlich19468412012-01-28 17:25:33 +0100559 return;
560
561 /* also try the legacy rates. */
Lorenzo Bianconi90c66bd2015-08-06 23:47:31 +0200562 *rate_idx = 0;
Simon Wunderlich19468412012-01-28 17:25:33 +0100563 /* keep protection flags */
Lorenzo Bianconi90c66bd2015-08-06 23:47:31 +0200564 *rate_flags &= (IEEE80211_TX_RC_USE_RTS_CTS |
565 IEEE80211_TX_RC_USE_CTS_PROTECT |
566 IEEE80211_TX_RC_USE_SHORT_PREAMBLE);
567 if (rate_idx_match_legacy_mask(rate_idx, sband->n_bitrates,
568 mask))
Simon Wunderlich19468412012-01-28 17:25:33 +0100569 return;
Lorenzo Bianconib119ad62015-08-06 23:47:33 +0200570 } else {
Simon Wunderlich19468412012-01-28 17:25:33 +0100571 /* handle legacy rates */
Lorenzo Bianconi90c66bd2015-08-06 23:47:31 +0200572 if (rate_idx_match_legacy_mask(rate_idx, sband->n_bitrates,
573 mask))
Simon Wunderlich19468412012-01-28 17:25:33 +0100574 return;
575
576 /* if HT BSS, and we handle a data frame, also try HT rates */
Simon Wunderlich0418a442013-05-16 13:00:31 +0200577 switch (chan_width) {
578 case NL80211_CHAN_WIDTH_20_NOHT:
579 case NL80211_CHAN_WIDTH_5:
580 case NL80211_CHAN_WIDTH_10:
Simon Wunderlich19468412012-01-28 17:25:33 +0100581 return;
Simon Wunderlich0418a442013-05-16 13:00:31 +0200582 default:
583 break;
584 }
Simon Wunderlich19468412012-01-28 17:25:33 +0100585
Lorenzo Bianconi90c66bd2015-08-06 23:47:31 +0200586 *rate_idx = 0;
Simon Wunderlich19468412012-01-28 17:25:33 +0100587 /* keep protection flags */
Lorenzo Bianconi90c66bd2015-08-06 23:47:31 +0200588 *rate_flags &= (IEEE80211_TX_RC_USE_RTS_CTS |
589 IEEE80211_TX_RC_USE_CTS_PROTECT |
590 IEEE80211_TX_RC_USE_SHORT_PREAMBLE);
Simon Wunderlich19468412012-01-28 17:25:33 +0100591
Lorenzo Bianconi90c66bd2015-08-06 23:47:31 +0200592 *rate_flags |= IEEE80211_TX_RC_MCS;
Simon Wunderlich19468412012-01-28 17:25:33 +0100593
Felix Fietkau0d528d82013-04-22 16:14:41 +0200594 if (chan_width == NL80211_CHAN_WIDTH_40)
Lorenzo Bianconi90c66bd2015-08-06 23:47:31 +0200595 *rate_flags |= IEEE80211_TX_RC_40_MHZ_WIDTH;
Simon Wunderlich19468412012-01-28 17:25:33 +0100596
Lorenzo Bianconi90c66bd2015-08-06 23:47:31 +0200597 if (rate_idx_match_mcs_mask(rate_idx, mcs_mask))
Jouni Malinen37eb0b12010-01-06 13:09:08 +0200598 return;
Jouni Malinen37eb0b12010-01-06 13:09:08 +0200599 }
600
601 /*
602 * Uh.. No suitable rate exists. This should not really happen with
603 * sane TX rate mask configurations. However, should someone manage to
604 * configure supported rates and TX rate mask in incompatible way,
605 * allow the frame to be transmitted with whatever the rate control
606 * selected.
607 */
608}
609
Felix Fietkau0d528d82013-04-22 16:14:41 +0200610static void rate_fixup_ratelist(struct ieee80211_vif *vif,
611 struct ieee80211_supported_band *sband,
612 struct ieee80211_tx_info *info,
613 struct ieee80211_tx_rate *rates,
614 int max_rates)
615{
616 struct ieee80211_rate *rate;
617 bool inval = false;
618 int i;
619
620 /*
621 * Set up the RTS/CTS rate as the fastest basic rate
622 * that is not faster than the data rate unless there
623 * is no basic rate slower than the data rate, in which
624 * case we pick the slowest basic rate
625 *
626 * XXX: Should this check all retry rates?
627 */
Felix Fietkau336004e2014-11-21 23:29:14 +0100628 if (!(rates[0].flags &
629 (IEEE80211_TX_RC_MCS | IEEE80211_TX_RC_VHT_MCS))) {
Felix Fietkau0d528d82013-04-22 16:14:41 +0200630 u32 basic_rates = vif->bss_conf.basic_rates;
Karl Beldanc7abf252014-10-13 14:34:41 +0200631 s8 baserate = basic_rates ? ffs(basic_rates) - 1 : 0;
Felix Fietkau0d528d82013-04-22 16:14:41 +0200632
633 rate = &sband->bitrates[rates[0].idx];
634
635 for (i = 0; i < sband->n_bitrates; i++) {
636 /* must be a basic rate */
637 if (!(basic_rates & BIT(i)))
638 continue;
639 /* must not be faster than the data rate */
640 if (sband->bitrates[i].bitrate > rate->bitrate)
641 continue;
642 /* maximum */
643 if (sband->bitrates[baserate].bitrate <
644 sband->bitrates[i].bitrate)
645 baserate = i;
646 }
647
648 info->control.rts_cts_rate_idx = baserate;
649 }
650
651 for (i = 0; i < max_rates; i++) {
652 /*
653 * make sure there's no valid rate following
654 * an invalid one, just in case drivers don't
655 * take the API seriously to stop at -1.
656 */
657 if (inval) {
658 rates[i].idx = -1;
659 continue;
660 }
661 if (rates[i].idx < 0) {
662 inval = true;
663 continue;
664 }
665
666 /*
667 * For now assume MCS is already set up correctly, this
668 * needs to be fixed.
669 */
670 if (rates[i].flags & IEEE80211_TX_RC_MCS) {
671 WARN_ON(rates[i].idx > 76);
672
673 if (!(rates[i].flags & IEEE80211_TX_RC_USE_RTS_CTS) &&
674 info->control.use_cts_prot)
675 rates[i].flags |=
676 IEEE80211_TX_RC_USE_CTS_PROTECT;
677 continue;
678 }
679
680 if (rates[i].flags & IEEE80211_TX_RC_VHT_MCS) {
681 WARN_ON(ieee80211_rate_get_vht_mcs(&rates[i]) > 9);
682 continue;
683 }
684
685 /* set up RTS protection if desired */
686 if (info->control.use_rts) {
687 rates[i].flags |= IEEE80211_TX_RC_USE_RTS_CTS;
688 info->control.use_cts_prot = false;
689 }
690
691 /* RC is busted */
692 if (WARN_ON_ONCE(rates[i].idx >= sband->n_bitrates)) {
693 rates[i].idx = -1;
694 continue;
695 }
696
697 rate = &sband->bitrates[rates[i].idx];
698
699 /* set up short preamble */
700 if (info->control.short_preamble &&
701 rate->flags & IEEE80211_RATE_SHORT_PREAMBLE)
702 rates[i].flags |= IEEE80211_TX_RC_USE_SHORT_PREAMBLE;
703
704 /* set up G protection */
705 if (!(rates[i].flags & IEEE80211_TX_RC_USE_RTS_CTS) &&
706 info->control.use_cts_prot &&
707 rate->flags & IEEE80211_RATE_ERP_G)
708 rates[i].flags |= IEEE80211_TX_RC_USE_CTS_PROTECT;
709 }
710}
711
712
713static void rate_control_fill_sta_table(struct ieee80211_sta *sta,
714 struct ieee80211_tx_info *info,
715 struct ieee80211_tx_rate *rates,
716 int max_rates)
717{
718 struct ieee80211_sta_rates *ratetbl = NULL;
719 int i;
720
721 if (sta && !info->control.skip_table)
722 ratetbl = rcu_dereference(sta->rates);
723
724 /* Fill remaining rate slots with data from the sta rate table. */
725 max_rates = min_t(int, max_rates, IEEE80211_TX_RATE_TABLE_SIZE);
726 for (i = 0; i < max_rates; i++) {
727 if (i < ARRAY_SIZE(info->control.rates) &&
728 info->control.rates[i].idx >= 0 &&
729 info->control.rates[i].count) {
730 if (rates != info->control.rates)
731 rates[i] = info->control.rates[i];
732 } else if (ratetbl) {
733 rates[i].idx = ratetbl->rate[i].idx;
734 rates[i].flags = ratetbl->rate[i].flags;
735 if (info->control.use_rts)
736 rates[i].count = ratetbl->rate[i].count_rts;
737 else if (info->control.use_cts_prot)
738 rates[i].count = ratetbl->rate[i].count_cts;
739 else
740 rates[i].count = ratetbl->rate[i].count;
741 } else {
742 rates[i].idx = -1;
743 rates[i].count = 0;
744 }
745
746 if (rates[i].idx < 0 || !rates[i].count)
747 break;
748 }
749}
750
Lorenzo Bianconi90c66bd2015-08-06 23:47:31 +0200751static bool rate_control_cap_mask(struct ieee80211_sub_if_data *sdata,
752 struct ieee80211_supported_band *sband,
753 struct ieee80211_sta *sta, u32 *mask,
Lorenzo Bianconib119ad62015-08-06 23:47:33 +0200754 u8 mcs_mask[IEEE80211_HT_MCS_MASK_LEN],
755 u16 vht_mask[NL80211_VHT_NSS_MAX])
Lorenzo Bianconi90c66bd2015-08-06 23:47:31 +0200756{
757 u32 i, flags;
758
759 *mask = sdata->rc_rateidx_mask[sband->band];
760 flags = ieee80211_chandef_rate_flags(&sdata->vif.bss_conf.chandef);
761 for (i = 0; i < sband->n_bitrates; i++) {
762 if ((flags & sband->bitrates[i].flags) != flags)
763 *mask &= ~BIT(i);
764 }
765
766 if (*mask == (1 << sband->n_bitrates) - 1 &&
Lorenzo Bianconib119ad62015-08-06 23:47:33 +0200767 !sdata->rc_has_mcs_mask[sband->band] &&
768 !sdata->rc_has_vht_mcs_mask[sband->band])
Lorenzo Bianconi90c66bd2015-08-06 23:47:31 +0200769 return false;
770
771 if (sdata->rc_has_mcs_mask[sband->band])
772 memcpy(mcs_mask, sdata->rc_rateidx_mcs_mask[sband->band],
773 IEEE80211_HT_MCS_MASK_LEN);
774 else
775 memset(mcs_mask, 0xff, IEEE80211_HT_MCS_MASK_LEN);
776
Lorenzo Bianconib119ad62015-08-06 23:47:33 +0200777 if (sdata->rc_has_vht_mcs_mask[sband->band])
778 memcpy(vht_mask, sdata->rc_rateidx_vht_mcs_mask[sband->band],
779 sizeof(u16) * NL80211_VHT_NSS_MAX);
780 else
781 memset(vht_mask, 0xff, sizeof(u16) * NL80211_VHT_NSS_MAX);
782
Lorenzo Bianconi90c66bd2015-08-06 23:47:31 +0200783 if (sta) {
Lorenzo Bianconib119ad62015-08-06 23:47:33 +0200784 __le16 sta_vht_cap;
785 u16 sta_vht_mask[NL80211_VHT_NSS_MAX];
786
Lorenzo Bianconi90c66bd2015-08-06 23:47:31 +0200787 /* Filter out rates that the STA does not support */
788 *mask &= sta->supp_rates[sband->band];
Thierry Reding98a1f822015-08-26 12:22:14 +0200789 for (i = 0; i < IEEE80211_HT_MCS_MASK_LEN; i++)
Lorenzo Bianconi90c66bd2015-08-06 23:47:31 +0200790 mcs_mask[i] &= sta->ht_cap.mcs.rx_mask[i];
Lorenzo Bianconib119ad62015-08-06 23:47:33 +0200791
792 sta_vht_cap = sta->vht_cap.vht_mcs.rx_mcs_map;
793 ieee80211_get_vht_mask_from_cap(sta_vht_cap, sta_vht_mask);
794 for (i = 0; i < NL80211_VHT_NSS_MAX; i++)
795 vht_mask[i] &= sta_vht_mask[i];
Lorenzo Bianconi90c66bd2015-08-06 23:47:31 +0200796 }
797
798 return true;
799}
800
Lorenzo Bianconie9108672015-08-06 23:47:32 +0200801static void
802rate_control_apply_mask_ratetbl(struct sta_info *sta,
803 struct ieee80211_supported_band *sband,
804 struct ieee80211_sta_rates *rates)
805{
806 int i;
807 u32 mask;
808 u8 mcs_mask[IEEE80211_HT_MCS_MASK_LEN];
Lorenzo Bianconib119ad62015-08-06 23:47:33 +0200809 u16 vht_mask[NL80211_VHT_NSS_MAX];
Lorenzo Bianconie9108672015-08-06 23:47:32 +0200810 enum nl80211_chan_width chan_width;
811
812 if (!rate_control_cap_mask(sta->sdata, sband, &sta->sta, &mask,
Lorenzo Bianconib119ad62015-08-06 23:47:33 +0200813 mcs_mask, vht_mask))
Lorenzo Bianconie9108672015-08-06 23:47:32 +0200814 return;
815
816 chan_width = sta->sdata->vif.bss_conf.chandef.width;
817 for (i = 0; i < IEEE80211_TX_RATE_TABLE_SIZE; i++) {
818 if (rates->rate[i].idx < 0)
819 break;
820
821 rate_idx_match_mask(&rates->rate[i].idx, &rates->rate[i].flags,
Lorenzo Bianconib119ad62015-08-06 23:47:33 +0200822 sband, chan_width, mask, mcs_mask,
823 vht_mask);
Lorenzo Bianconie9108672015-08-06 23:47:32 +0200824 }
825}
826
Felix Fietkau0d528d82013-04-22 16:14:41 +0200827static void rate_control_apply_mask(struct ieee80211_sub_if_data *sdata,
828 struct ieee80211_sta *sta,
829 struct ieee80211_supported_band *sband,
Felix Fietkau0d528d82013-04-22 16:14:41 +0200830 struct ieee80211_tx_rate *rates,
831 int max_rates)
832{
833 enum nl80211_chan_width chan_width;
834 u8 mcs_mask[IEEE80211_HT_MCS_MASK_LEN];
Felix Fietkau0d528d82013-04-22 16:14:41 +0200835 u32 mask;
Lorenzo Bianconib119ad62015-08-06 23:47:33 +0200836 u16 rate_flags, vht_mask[NL80211_VHT_NSS_MAX];
Felix Fietkau0d528d82013-04-22 16:14:41 +0200837 int i;
838
839 /*
840 * Try to enforce the rateidx mask the user wanted. skip this if the
841 * default mask (allow all rates) is used to save some processing for
842 * the common case.
843 */
Lorenzo Bianconib119ad62015-08-06 23:47:33 +0200844 if (!rate_control_cap_mask(sdata, sband, sta, &mask, mcs_mask,
845 vht_mask))
Felix Fietkau0d528d82013-04-22 16:14:41 +0200846 return;
847
Felix Fietkau0d528d82013-04-22 16:14:41 +0200848 /*
849 * Make sure the rate index selected for each TX rate is
850 * included in the configured mask and change the rate indexes
851 * if needed.
852 */
853 chan_width = sdata->vif.bss_conf.chandef.width;
854 for (i = 0; i < max_rates; i++) {
855 /* Skip invalid rates */
856 if (rates[i].idx < 0)
857 break;
858
Lorenzo Bianconi90c66bd2015-08-06 23:47:31 +0200859 rate_flags = rates[i].flags;
860 rate_idx_match_mask(&rates[i].idx, &rate_flags, sband,
Lorenzo Bianconib119ad62015-08-06 23:47:33 +0200861 chan_width, mask, mcs_mask, vht_mask);
Lorenzo Bianconi90c66bd2015-08-06 23:47:31 +0200862 rates[i].flags = rate_flags;
Felix Fietkau0d528d82013-04-22 16:14:41 +0200863 }
864}
865
866void ieee80211_get_tx_rates(struct ieee80211_vif *vif,
867 struct ieee80211_sta *sta,
868 struct sk_buff *skb,
869 struct ieee80211_tx_rate *dest,
870 int max_rates)
871{
872 struct ieee80211_sub_if_data *sdata;
873 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
874 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
875 struct ieee80211_supported_band *sband;
876
877 rate_control_fill_sta_table(sta, info, dest, max_rates);
878
879 if (!vif)
880 return;
881
882 sdata = vif_to_sdata(vif);
883 sband = sdata->local->hw.wiphy->bands[info->band];
884
885 if (ieee80211_is_data(hdr->frame_control))
Lorenzo Bianconi35225eb2015-08-06 23:47:30 +0200886 rate_control_apply_mask(sdata, sta, sband, dest, max_rates);
Felix Fietkau0d528d82013-04-22 16:14:41 +0200887
888 if (dest[0].idx < 0)
Andrei Otcheretianski1d2d3502013-10-14 11:04:28 +0200889 __rate_control_send_low(&sdata->local->hw, sband, sta, info,
890 sdata->rc_rateidx_mask[info->band]);
Felix Fietkau0d528d82013-04-22 16:14:41 +0200891
892 if (sta)
893 rate_fixup_ratelist(vif, sband, info, dest, max_rates);
894}
895EXPORT_SYMBOL(ieee80211_get_tx_rates);
896
Johannes Berg4b7679a2008-09-18 18:14:18 +0200897void rate_control_get_rate(struct ieee80211_sub_if_data *sdata,
Johannes Berge6a98542008-10-21 12:40:02 +0200898 struct sta_info *sta,
899 struct ieee80211_tx_rate_control *txrc)
Mattias Nissler1abbe492007-12-20 13:50:07 +0100900{
Johannes Berg4b7679a2008-09-18 18:14:18 +0200901 struct rate_control_ref *ref = sdata->local->rate_ctrl;
902 void *priv_sta = NULL;
903 struct ieee80211_sta *ista = NULL;
Johannes Berge6a98542008-10-21 12:40:02 +0200904 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(txrc->skb);
Mattias Nissler1abbe492007-12-20 13:50:07 +0100905 int i;
Mattias Nissler1abbe492007-12-20 13:50:07 +0100906
Johannes Berge6a98542008-10-21 12:40:02 +0200907 for (i = 0; i < IEEE80211_TX_MAX_RATES; i++) {
908 info->control.rates[i].idx = -1;
909 info->control.rates[i].flags = 0;
Mohammed Shafi Shajakhan8617b092012-02-20 10:05:31 +0530910 info->control.rates[i].count = 0;
Mattias Nissler1abbe492007-12-20 13:50:07 +0100911 }
Johannes Berge6a98542008-10-21 12:40:02 +0200912
Johannes Berg583a7a32019-05-29 15:25:36 +0300913 if (rate_control_send_low(sta ? &sta->sta : NULL, txrc))
Juuso Oikarinen5affcd6ba2010-02-12 10:05:45 +0200914 return;
915
Johannes Berg583a7a32019-05-29 15:25:36 +0300916 if (ieee80211_hw_check(&sdata->local->hw, HAS_RATE_CONTROL))
Johannes Berg1e87fec2019-05-16 11:44:52 +0200917 return;
918
Johannes Bergbd718fc2019-05-29 15:25:35 +0300919 if (sta && test_sta_flag(sta, WLAN_STA_RATE_CONTROL)) {
920 ista = &sta->sta;
921 priv_sta = sta->rate_ctrl_priv;
922 }
923
Johannes Berg35c347a2015-03-05 16:10:08 +0100924 if (ista) {
925 spin_lock_bh(&sta->rate_ctrl_lock);
926 ref->ops->get_rate(ref->priv, ista, priv_sta, txrc);
927 spin_unlock_bh(&sta->rate_ctrl_lock);
928 } else {
Johannes Berg1e87fec2019-05-16 11:44:52 +0200929 rate_control_send_low(NULL, txrc);
Johannes Berg35c347a2015-03-05 16:10:08 +0100930 }
Johannes Berge6a98542008-10-21 12:40:02 +0200931
Johannes Berg30686bf2015-06-02 21:39:54 +0200932 if (ieee80211_hw_check(&sdata->local->hw, SUPPORTS_RC_TABLE))
Felix Fietkau0d528d82013-04-22 16:14:41 +0200933 return;
Felix Fietkau2ffbe6d2013-04-16 13:38:42 +0200934
Felix Fietkau0d528d82013-04-22 16:14:41 +0200935 ieee80211_get_tx_rates(&sdata->vif, ista, txrc->skb,
936 info->control.rates,
937 ARRAY_SIZE(info->control.rates));
Mattias Nissler1abbe492007-12-20 13:50:07 +0100938}
939
Felix Fietkau0d528d82013-04-22 16:14:41 +0200940int rate_control_set_rates(struct ieee80211_hw *hw,
941 struct ieee80211_sta *pubsta,
942 struct ieee80211_sta_rates *rates)
943{
Johannes Bergf815e2b2014-11-19 00:10:42 +0100944 struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
Felix Fietkauf6b3d852013-05-03 10:01:03 +0200945 struct ieee80211_sta_rates *old;
Lorenzo Bianconie9108672015-08-06 23:47:32 +0200946 struct ieee80211_supported_band *sband;
Felix Fietkau0d528d82013-04-22 16:14:41 +0200947
Mohammed Shafi Shajakhan21a8e9d2017-04-27 12:45:38 +0530948 sband = ieee80211_get_sband(sta->sdata);
949 if (!sband)
950 return -EINVAL;
Lorenzo Bianconie9108672015-08-06 23:47:32 +0200951 rate_control_apply_mask_ratetbl(sta, sband, rates);
Felix Fietkauf6b3d852013-05-03 10:01:03 +0200952 /*
953 * mac80211 guarantees that this function will not be called
954 * concurrently, so the following RCU access is safe, even without
955 * extra locking. This can not be checked easily, so we just set
956 * the condition to true.
957 */
958 old = rcu_dereference_protected(pubsta->rates, true);
Felix Fietkau0d528d82013-04-22 16:14:41 +0200959 rcu_assign_pointer(pubsta->rates, rates);
960 if (old)
961 kfree_rcu(old, rcu_head);
962
Johannes Bergf815e2b2014-11-19 00:10:42 +0100963 drv_sta_rate_tbl_update(hw_to_local(hw), sta->sdata, pubsta);
964
Toke Høiland-Jørgensen484a54c2017-04-06 11:38:26 +0200965 ieee80211_sta_set_expected_throughput(pubsta, sta_get_expected_throughput(sta));
966
Felix Fietkau0d528d82013-04-22 16:14:41 +0200967 return 0;
968}
969EXPORT_SYMBOL(rate_control_set_rates);
970
Johannes Bergff688082007-07-27 15:43:23 +0200971int ieee80211_init_rate_ctrl_alg(struct ieee80211_local *local,
972 const char *name)
973{
Johannes Berg889cbb92012-01-17 10:33:29 +0100974 struct rate_control_ref *ref;
Johannes Bergff688082007-07-27 15:43:23 +0200975
976 ASSERT_RTNL();
Johannes Bergaf65cd962009-11-17 18:18:36 +0100977
Johannes Berg3b8d81e02009-06-17 17:43:56 +0200978 if (local->open_count)
Johannes Bergff688082007-07-27 15:43:23 +0200979 return -EBUSY;
980
Johannes Berg30686bf2015-06-02 21:39:54 +0200981 if (ieee80211_hw_check(&local->hw, HAS_RATE_CONTROL)) {
Johannes Bergaf65cd962009-11-17 18:18:36 +0100982 if (WARN_ON(!local->ops->set_rts_threshold))
983 return -EINVAL;
984 return 0;
985 }
986
Johannes Bergff688082007-07-27 15:43:23 +0200987 ref = rate_control_alloc(name, local);
988 if (!ref) {
Joe Perches0fb9a9e2010-08-20 16:25:38 -0700989 wiphy_warn(local->hw.wiphy,
990 "Failed to select rate control algorithm\n");
Johannes Bergff688082007-07-27 15:43:23 +0200991 return -ENOENT;
992 }
993
Johannes Berg889cbb92012-01-17 10:33:29 +0100994 WARN_ON(local->rate_ctrl);
Johannes Bergff688082007-07-27 15:43:23 +0200995 local->rate_ctrl = ref;
Johannes Bergff688082007-07-27 15:43:23 +0200996
Joe Perches0fb9a9e2010-08-20 16:25:38 -0700997 wiphy_debug(local->hw.wiphy, "Selected rate control algorithm '%s'\n",
998 ref->ops->name);
Johannes Bergff688082007-07-27 15:43:23 +0200999
Johannes Bergff688082007-07-27 15:43:23 +02001000 return 0;
1001}
1002
1003void rate_control_deinitialize(struct ieee80211_local *local)
1004{
1005 struct rate_control_ref *ref;
1006
1007 ref = local->rate_ctrl;
Johannes Bergaf65cd962009-11-17 18:18:36 +01001008
1009 if (!ref)
1010 return;
1011
Johannes Bergff688082007-07-27 15:43:23 +02001012 local->rate_ctrl = NULL;
Johannes Berga8589582017-02-15 15:02:07 +01001013 rate_control_free(local, ref);
Johannes Bergff688082007-07-27 15:43:23 +02001014}