blob: 63266d73c2521ccac7f58696d3e9de2f9f741f39 [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
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
Felix Fietkau18fb84d2017-04-26 17:11:35 +020062void 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
Denys Vlasenkoeb6d9292015-07-15 14:56:06 +020084void 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
Johannes Berg631ad702014-01-20 23:29:34 +0100111int ieee80211_rate_control_register(const struct rate_control_ops *ops)
Jiri Bencf0706e82007-05-05 11:45:53 -0700112{
113 struct rate_control_alg *alg;
114
Johannes Bergac71c692007-10-28 14:17:44 +0100115 if (!ops->name)
116 return -EINVAL;
117
Johannes Berg999acd92007-10-28 14:49:33 +0100118 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);
Cyrill Gorcunovb808ab12007-12-13 15:52:11 -0800123 mutex_unlock(&rate_ctrl_mutex);
Johannes Berg999acd92007-10-28 14:49:33 +0100124 return -EALREADY;
125 }
126 }
127
Yoann Padioleaudd00cc42007-07-19 01:49:03 -0700128 alg = kzalloc(sizeof(*alg), GFP_KERNEL);
Jiri Bencf0706e82007-05-05 11:45:53 -0700129 if (alg == NULL) {
Johannes Berg999acd92007-10-28 14:49:33 +0100130 mutex_unlock(&rate_ctrl_mutex);
Jiri Bencf0706e82007-05-05 11:45:53 -0700131 return -ENOMEM;
132 }
Jiri Bencf0706e82007-05-05 11:45:53 -0700133 alg->ops = ops;
134
Jiri Bencf0706e82007-05-05 11:45:53 -0700135 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
Johannes Berg631ad702014-01-20 23:29:34 +0100142void ieee80211_rate_control_unregister(const struct rate_control_ops *ops)
Jiri Bencf0706e82007-05-05 11:45:53 -0700143{
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);
Cyrill Gorcunov20880e82007-12-13 16:17:03 -0800150 kfree(alg);
Jiri Bencf0706e82007-05-05 11:45:53 -0700151 break;
152 }
153 }
154 mutex_unlock(&rate_ctrl_mutex);
Jiri Bencf0706e82007-05-05 11:45:53 -0700155}
156EXPORT_SYMBOL(ieee80211_rate_control_unregister);
157
Johannes Berg631ad702014-01-20 23:29:34 +0100158static const struct rate_control_ops *
Jiri Bencf0706e82007-05-05 11:45:53 -0700159ieee80211_try_rate_control_ops_get(const char *name)
160{
161 struct rate_control_alg *alg;
Johannes Berg631ad702014-01-20 23:29:34 +0100162 const struct rate_control_ops *ops = NULL;
Jiri Bencf0706e82007-05-05 11:45:53 -0700163
Johannes Bergac71c692007-10-28 14:17:44 +0100164 if (!name)
165 return NULL;
166
Jiri Bencf0706e82007-05-05 11:45:53 -0700167 mutex_lock(&rate_ctrl_mutex);
168 list_for_each_entry(alg, &rate_ctrl_algs, list) {
Johannes Bergcc01f9b2014-01-22 10:36:59 +0100169 if (!strcmp(alg->ops->name, name)) {
170 ops = alg->ops;
171 break;
172 }
Jiri Bencf0706e82007-05-05 11:45:53 -0700173 }
174 mutex_unlock(&rate_ctrl_mutex);
175 return ops;
176}
177
Stefano Brivioc21b39a2007-12-19 01:26:16 +0100178/* Get the rate control algorithm. */
Johannes Berg631ad702014-01-20 23:29:34 +0100179static const struct rate_control_ops *
Jiri Bencf0706e82007-05-05 11:45:53 -0700180ieee80211_rate_control_ops_get(const char *name)
181{
Johannes Berg631ad702014-01-20 23:29:34 +0100182 const struct rate_control_ops *ops;
Stefano Brivioc21b39a2007-12-19 01:26:16 +0100183 const char *alg_name;
Jiri Bencf0706e82007-05-05 11:45:53 -0700184
Dan Streetmanb51d23e2015-06-17 06:18:52 +0930185 kernel_param_lock(THIS_MODULE);
Johannes Bergac71c692007-10-28 14:17:44 +0100186 if (!name)
Stefano Brivioc21b39a2007-12-19 01:26:16 +0100187 alg_name = ieee80211_default_rc_algo;
188 else
189 alg_name = name;
Johannes Bergac71c692007-10-28 14:17:44 +0100190
Stefano Brivioc21b39a2007-12-19 01:26:16 +0100191 ops = ieee80211_try_rate_control_ops_get(alg_name);
Stefano Brivioc21b39a2007-12-19 01:26:16 +0100192 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
Matthias Kaehlcke93f56de2017-04-06 16:31:41 -0700196 /* 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 */
Johannes Berg4b475892008-01-02 15:17:03 +0100199 ops = ieee80211_try_rate_control_ops_get(CONFIG_MAC80211_RC_DEFAULT);
Matthias Kaehlcke93f56de2017-04-06 16:31:41 -0700200
Dan Streetmanb51d23e2015-06-17 06:18:52 +0930201 kernel_param_unlock(THIS_MODULE);
Johannes Berg4b475892008-01-02 15:17:03 +0100202
Jiri Bencf0706e82007-05-05 11:45:53 -0700203 return ops;
204}
205
Johannes Berg4b7679a2008-09-18 18:14:18 +0200206#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
Johannes Berg6cb5f3e2020-04-23 11:13:49 +0200217const struct file_operations rcname_ops = {
Johannes Berg4b7679a2008-09-18 18:14:18 +0200218 .read = rcname_read,
Stephen Boyd234e3402012-04-05 14:25:11 -0700219 .open = simple_open,
Arnd Bergmann6038f372010-08-15 18:52:59 +0200220 .llseek = default_llseek,
Johannes Berg4b7679a2008-09-18 18:14:18 +0200221};
222#endif
223
Johannes Berg6cb5f3e2020-04-23 11:13:49 +0200224static struct rate_control_ref *
225rate_control_alloc(const char *name, struct ieee80211_local *local)
Jiri Bencf0706e82007-05-05 11:45:53 -0700226{
227 struct rate_control_ref *ref;
228
229 ref = kmalloc(sizeof(struct rate_control_ref), GFP_KERNEL);
230 if (!ref)
Johannes Bergcc01f9b2014-01-22 10:36:59 +0100231 return NULL;
Jiri Bencf0706e82007-05-05 11:45:53 -0700232 ref->ops = ieee80211_rate_control_ops_get(name);
233 if (!ref->ops)
Johannes Bergcc01f9b2014-01-22 10:36:59 +0100234 goto free;
Johannes Berg4b7679a2008-09-18 18:14:18 +0200235
Johannes Berg6cb5f3e2020-04-23 11:13:49 +0200236 ref->priv = ref->ops->alloc(&local->hw);
Jiri Bencf0706e82007-05-05 11:45:53 -0700237 if (!ref->priv)
Johannes Bergcc01f9b2014-01-22 10:36:59 +0100238 goto free;
Jiri Bencf0706e82007-05-05 11:45:53 -0700239 return ref;
240
Johannes Bergcc01f9b2014-01-22 10:36:59 +0100241free:
Jiri Bencf0706e82007-05-05 11:45:53 -0700242 kfree(ref);
Jiri Bencf0706e82007-05-05 11:45:53 -0700243 return NULL;
244}
245
Johannes Berga8589582017-02-15 15:02:07 +0100246static void rate_control_free(struct ieee80211_local *local,
247 struct rate_control_ref *ctrl_ref)
Jiri Bencf0706e82007-05-05 11:45:53 -0700248{
Jiri Bencf0706e82007-05-05 11:45:53 -0700249 ctrl_ref->ops->free(ctrl_ref->priv);
Johannes Berg4b7679a2008-09-18 18:14:18 +0200250
251#ifdef CONFIG_MAC80211_DEBUGFS
Johannes Berga8589582017-02-15 15:02:07 +0100252 debugfs_remove_recursive(local->debugfs.rcdir);
253 local->debugfs.rcdir = NULL;
Johannes Berg4b7679a2008-09-18 18:14:18 +0200254#endif
255
Jiri Bencf0706e82007-05-05 11:45:53 -0700256 kfree(ctrl_ref);
257}
258
Johannes Berge8e4f522017-03-08 11:12:10 +0100259void ieee80211_check_rate_mask(struct ieee80211_sub_if_data *sdata)
260{
261 struct ieee80211_local *local = sdata->local;
262 struct ieee80211_supported_band *sband;
263 u32 user_mask, basic_rates = sdata->vif.bss_conf.basic_rates;
264 enum nl80211_band band;
265
266 if (WARN_ON(!sdata->vif.bss_conf.chandef.chan))
267 return;
268
Thomas Pedersen1821f8b2020-09-21 19:28:12 -0700269 band = sdata->vif.bss_conf.chandef.chan->band;
270 if (band == NL80211_BAND_S1GHZ) {
271 /* TODO */
272 return;
273 }
274
Johannes Berge8e4f522017-03-08 11:12:10 +0100275 if (WARN_ON_ONCE(!basic_rates))
276 return;
277
Johannes Berge8e4f522017-03-08 11:12:10 +0100278 user_mask = sdata->rc_rateidx_mask[band];
279 sband = local->hw.wiphy->bands[band];
280
281 if (user_mask & basic_rates)
282 return;
283
284 sdata_dbg(sdata,
285 "no overlap between basic rates (0x%x) and user mask (0x%x on band %d) - clearing the latter",
286 basic_rates, user_mask, band);
287 sdata->rc_rateidx_mask[band] = (1 << sband->n_bitrates) - 1;
288}
289
Rajkumar Manoharanb6f35302011-09-29 20:34:04 +0530290static bool rc_no_data_or_no_ack_use_min(struct ieee80211_tx_rate_control *txrc)
Luis R. Rodriguez4c6d4f52009-07-16 10:05:41 -0700291{
292 struct sk_buff *skb = txrc->skb;
293 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
294 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
295 __le16 fc;
296
297 fc = hdr->frame_control;
298
Rajkumar Manoharanb6f35302011-09-29 20:34:04 +0530299 return (info->flags & (IEEE80211_TX_CTL_NO_ACK |
300 IEEE80211_TX_CTL_USE_MINRATE)) ||
301 !ieee80211_is_data(fc);
Luis R. Rodriguez4c6d4f52009-07-16 10:05:41 -0700302}
303
Thomas Pedersen1821f8b2020-09-21 19:28:12 -0700304static void rc_send_low_basicrate(struct ieee80211_tx_rate *rate,
305 u32 basic_rates,
Felix Fietkau8f0729b2010-11-11 15:07:23 +0100306 struct ieee80211_supported_band *sband)
Jouni Malinene00cfce2009-12-29 12:59:19 +0200307{
308 u8 i;
309
Thomas Pedersen1821f8b2020-09-21 19:28:12 -0700310 if (sband->band == NL80211_BAND_S1GHZ) {
311 /* TODO */
312 rate->flags |= IEEE80211_TX_RC_S1G_MCS;
313 rate->idx = 0;
314 return;
315 }
316
Jouni Malinene00cfce2009-12-29 12:59:19 +0200317 if (basic_rates == 0)
318 return; /* assume basic rates unknown and accept rate */
Thomas Pedersen1821f8b2020-09-21 19:28:12 -0700319 if (rate->idx < 0)
Jouni Malinene00cfce2009-12-29 12:59:19 +0200320 return;
Thomas Pedersen1821f8b2020-09-21 19:28:12 -0700321 if (basic_rates & (1 << rate->idx))
Jouni Malinene00cfce2009-12-29 12:59:19 +0200322 return; /* selected rate is a basic rate */
323
Thomas Pedersen1821f8b2020-09-21 19:28:12 -0700324 for (i = rate->idx + 1; i <= sband->n_bitrates; i++) {
Jouni Malinene00cfce2009-12-29 12:59:19 +0200325 if (basic_rates & (1 << i)) {
Thomas Pedersen1821f8b2020-09-21 19:28:12 -0700326 rate->idx = i;
Jouni Malinene00cfce2009-12-29 12:59:19 +0200327 return;
328 }
329 }
330
331 /* could not find a basic rate; use original selection */
332}
333
Felix Fietkau0d528d82013-04-22 16:14:41 +0200334static void __rate_control_send_low(struct ieee80211_hw *hw,
335 struct ieee80211_supported_band *sband,
336 struct ieee80211_sta *sta,
Andrei Otcheretianski1d2d3502013-10-14 11:04:28 +0200337 struct ieee80211_tx_info *info,
338 u32 rate_mask)
Felix Fietkau0d528d82013-04-22 16:14:41 +0200339{
Simon Wunderlich2103dec2013-07-08 16:55:53 +0200340 int i;
341 u32 rate_flags =
342 ieee80211_chandef_rate_flags(&hw->conf.chandef);
343
Thomas Pedersen1821f8b2020-09-21 19:28:12 -0700344 if (sband->band == NL80211_BAND_S1GHZ) {
345 info->control.rates[0].flags |= IEEE80211_TX_RC_S1G_MCS;
346 info->control.rates[0].idx = 0;
347 return;
348 }
349
Johannes Berg57fbcce2016-04-12 15:56:15 +0200350 if ((sband->band == NL80211_BAND_2GHZ) &&
Simon Wunderlich2103dec2013-07-08 16:55:53 +0200351 (info->flags & IEEE80211_TX_CTL_NO_CCK_RATE))
352 rate_flags |= IEEE80211_RATE_ERP_G;
353
354 info->control.rates[0].idx = 0;
355 for (i = 0; i < sband->n_bitrates; i++) {
Andrei Otcheretianski1d2d3502013-10-14 11:04:28 +0200356 if (!(rate_mask & BIT(i)))
357 continue;
358
Andrei Otcheretianski1431fcb2013-10-14 10:46:55 +0200359 if ((rate_flags & sband->bitrates[i].flags) != rate_flags)
360 continue;
361
Simon Wunderlich2103dec2013-07-08 16:55:53 +0200362 if (!rate_supported(sta, sband->band, i))
363 continue;
364
365 info->control.rates[0].idx = i;
366 break;
367 }
Johannes Berg0e5c3712015-09-23 14:02:47 +0200368 WARN_ONCE(i == sband->n_bitrates,
Johannes Berg163a7cd2019-05-29 15:25:37 +0300369 "no supported rates for sta %pM (0x%x, band %d) in rate_mask 0x%x with flags 0x%x\n",
370 sta ? sta->addr : NULL,
Johannes Bergef95d8b2015-10-25 10:59:42 +0200371 sta ? sta->supp_rates[sband->band] : -1,
Johannes Berg163a7cd2019-05-29 15:25:37 +0300372 sband->band,
Johannes Berg0e5c3712015-09-23 14:02:47 +0200373 rate_mask, rate_flags);
Felix Fietkau0d528d82013-04-22 16:14:41 +0200374
375 info->control.rates[0].count =
376 (info->flags & IEEE80211_TX_CTL_NO_ACK) ?
377 1 : hw->max_rate_tries;
378
379 info->control.skip_table = 1;
380}
381
Rajkumar Manoharanaad14ce2011-09-25 14:53:31 +0530382
Johannes Berg1e87fec2019-05-16 11:44:52 +0200383static bool rate_control_send_low(struct ieee80211_sta *pubsta,
384 struct ieee80211_tx_rate_control *txrc)
Luis R. Rodriguez4c6d4f52009-07-16 10:05:41 -0700385{
386 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(txrc->skb);
Felix Fietkaudd5b4cc2010-11-22 20:58:24 +0100387 struct ieee80211_supported_band *sband = txrc->sband;
Chun-Yeow Yeoh06703072013-08-06 15:39:56 -0700388 struct sta_info *sta;
Felix Fietkaudd5b4cc2010-11-22 20:58:24 +0100389 int mcast_rate;
Chun-Yeow Yeoh06703072013-08-06 15:39:56 -0700390 bool use_basicrate = false;
Luis R. Rodriguez4c6d4f52009-07-16 10:05:41 -0700391
Johannes Berg1e87fec2019-05-16 11:44:52 +0200392 if (!pubsta || rc_no_data_or_no_ack_use_min(txrc)) {
Andrei Otcheretianski1d2d3502013-10-14 11:04:28 +0200393 __rate_control_send_low(txrc->hw, sband, pubsta, info,
394 txrc->rate_idx_mask);
Felix Fietkau0d528d82013-04-22 16:14:41 +0200395
Chun-Yeow Yeoh06703072013-08-06 15:39:56 -0700396 if (!pubsta && txrc->bss) {
Felix Fietkaudd5b4cc2010-11-22 20:58:24 +0100397 mcast_rate = txrc->bss_conf->mcast_rate[sband->band];
398 if (mcast_rate > 0) {
399 info->control.rates[0].idx = mcast_rate - 1;
400 return true;
401 }
Chun-Yeow Yeoh06703072013-08-06 15:39:56 -0700402 use_basicrate = true;
403 } else if (pubsta) {
404 sta = container_of(pubsta, struct sta_info, sta);
405 if (ieee80211_vif_is_mesh(&sta->sdata->vif))
406 use_basicrate = true;
407 }
Felix Fietkaudd5b4cc2010-11-22 20:58:24 +0100408
Chun-Yeow Yeoh06703072013-08-06 15:39:56 -0700409 if (use_basicrate)
Thomas Pedersen1821f8b2020-09-21 19:28:12 -0700410 rc_send_low_basicrate(&info->control.rates[0],
Jouni Malinene00cfce2009-12-29 12:59:19 +0200411 txrc->bss_conf->basic_rates,
Felix Fietkaudd5b4cc2010-11-22 20:58:24 +0100412 sband);
Chun-Yeow Yeoh06703072013-08-06 15:39:56 -0700413
Luis R. Rodriguez4c6d4f52009-07-16 10:05:41 -0700414 return true;
415 }
416 return false;
417}
Luis R. Rodriguez4c6d4f52009-07-16 10:05:41 -0700418
Lorenzo Bianconi90c66bd2015-08-06 23:47:31 +0200419static bool rate_idx_match_legacy_mask(s8 *rate_idx, int n_bitrates, u32 mask)
Jouni Malinen37eb0b12010-01-06 13:09:08 +0200420{
421 int j;
422
423 /* See whether the selected rate or anything below it is allowed. */
Lorenzo Bianconi90c66bd2015-08-06 23:47:31 +0200424 for (j = *rate_idx; j >= 0; j--) {
Jouni Malinen37eb0b12010-01-06 13:09:08 +0200425 if (mask & (1 << j)) {
426 /* Okay, found a suitable rate. Use it. */
Lorenzo Bianconi90c66bd2015-08-06 23:47:31 +0200427 *rate_idx = j;
Simon Wunderlich19468412012-01-28 17:25:33 +0100428 return true;
Jouni Malinen37eb0b12010-01-06 13:09:08 +0200429 }
430 }
431
432 /* Try to find a higher rate that would be allowed */
Lorenzo Bianconi90c66bd2015-08-06 23:47:31 +0200433 for (j = *rate_idx + 1; j < n_bitrates; j++) {
Jouni Malinen37eb0b12010-01-06 13:09:08 +0200434 if (mask & (1 << j)) {
435 /* Okay, found a suitable rate. Use it. */
Lorenzo Bianconi90c66bd2015-08-06 23:47:31 +0200436 *rate_idx = j;
Simon Wunderlich19468412012-01-28 17:25:33 +0100437 return true;
438 }
439 }
440 return false;
441}
442
Lorenzo Bianconi90c66bd2015-08-06 23:47:31 +0200443static bool rate_idx_match_mcs_mask(s8 *rate_idx, u8 *mcs_mask)
Simon Wunderlich19468412012-01-28 17:25:33 +0100444{
445 int i, j;
446 int ridx, rbit;
447
Lorenzo Bianconi90c66bd2015-08-06 23:47:31 +0200448 ridx = *rate_idx / 8;
449 rbit = *rate_idx % 8;
Simon Wunderlich19468412012-01-28 17:25:33 +0100450
451 /* sanity check */
Dan Carpenter910570b52012-02-01 10:42:11 +0300452 if (ridx < 0 || ridx >= IEEE80211_HT_MCS_MASK_LEN)
Simon Wunderlich19468412012-01-28 17:25:33 +0100453 return false;
454
455 /* See whether the selected rate or anything below it is allowed. */
456 for (i = ridx; i >= 0; i--) {
457 for (j = rbit; j >= 0; j--)
458 if (mcs_mask[i] & BIT(j)) {
Lorenzo Bianconi90c66bd2015-08-06 23:47:31 +0200459 *rate_idx = i * 8 + j;
Simon Wunderlich19468412012-01-28 17:25:33 +0100460 return true;
461 }
462 rbit = 7;
463 }
464
465 /* Try to find a higher rate that would be allowed */
Lorenzo Bianconi90c66bd2015-08-06 23:47:31 +0200466 ridx = (*rate_idx + 1) / 8;
467 rbit = (*rate_idx + 1) % 8;
Simon Wunderlich19468412012-01-28 17:25:33 +0100468
469 for (i = ridx; i < IEEE80211_HT_MCS_MASK_LEN; i++) {
470 for (j = rbit; j < 8; j++)
471 if (mcs_mask[i] & BIT(j)) {
Lorenzo Bianconi90c66bd2015-08-06 23:47:31 +0200472 *rate_idx = i * 8 + j;
Simon Wunderlich19468412012-01-28 17:25:33 +0100473 return true;
474 }
475 rbit = 0;
476 }
477 return false;
478}
479
Lorenzo Bianconib119ad62015-08-06 23:47:33 +0200480static bool rate_idx_match_vht_mcs_mask(s8 *rate_idx, u16 *vht_mask)
481{
482 int i, j;
483 int ridx, rbit;
Simon Wunderlich19468412012-01-28 17:25:33 +0100484
Lorenzo Bianconib119ad62015-08-06 23:47:33 +0200485 ridx = *rate_idx >> 4;
486 rbit = *rate_idx & 0xf;
487
488 if (ridx < 0 || ridx >= NL80211_VHT_NSS_MAX)
489 return false;
490
491 /* See whether the selected rate or anything below it is allowed. */
492 for (i = ridx; i >= 0; i--) {
493 for (j = rbit; j >= 0; j--) {
494 if (vht_mask[i] & BIT(j)) {
495 *rate_idx = (i << 4) | j;
496 return true;
497 }
498 }
499 rbit = 15;
500 }
501
502 /* Try to find a higher rate that would be allowed */
503 ridx = (*rate_idx + 1) >> 4;
504 rbit = (*rate_idx + 1) & 0xf;
505
506 for (i = ridx; i < NL80211_VHT_NSS_MAX; i++) {
507 for (j = rbit; j < 16; j++) {
508 if (vht_mask[i] & BIT(j)) {
509 *rate_idx = (i << 4) | j;
510 return true;
511 }
512 }
513 rbit = 0;
514 }
515 return false;
516}
Simon Wunderlich19468412012-01-28 17:25:33 +0100517
Lorenzo Bianconi90c66bd2015-08-06 23:47:31 +0200518static void rate_idx_match_mask(s8 *rate_idx, u16 *rate_flags,
Felix Fietkau0d528d82013-04-22 16:14:41 +0200519 struct ieee80211_supported_band *sband,
520 enum nl80211_chan_width chan_width,
Simon Wunderlich19468412012-01-28 17:25:33 +0100521 u32 mask,
Lorenzo Bianconib119ad62015-08-06 23:47:33 +0200522 u8 mcs_mask[IEEE80211_HT_MCS_MASK_LEN],
523 u16 vht_mask[NL80211_VHT_NSS_MAX])
Simon Wunderlich19468412012-01-28 17:25:33 +0100524{
Lorenzo Bianconib119ad62015-08-06 23:47:33 +0200525 if (*rate_flags & IEEE80211_TX_RC_VHT_MCS) {
526 /* handle VHT rates */
527 if (rate_idx_match_vht_mcs_mask(rate_idx, vht_mask))
528 return;
529
530 *rate_idx = 0;
531 /* keep protection flags */
532 *rate_flags &= (IEEE80211_TX_RC_USE_RTS_CTS |
533 IEEE80211_TX_RC_USE_CTS_PROTECT |
534 IEEE80211_TX_RC_USE_SHORT_PREAMBLE);
535
536 *rate_flags |= IEEE80211_TX_RC_MCS;
537 if (chan_width == NL80211_CHAN_WIDTH_40)
538 *rate_flags |= IEEE80211_TX_RC_40_MHZ_WIDTH;
539
540 if (rate_idx_match_mcs_mask(rate_idx, mcs_mask))
541 return;
542
543 /* also try the legacy rates. */
544 *rate_flags &= ~(IEEE80211_TX_RC_MCS |
545 IEEE80211_TX_RC_40_MHZ_WIDTH);
546 if (rate_idx_match_legacy_mask(rate_idx, sband->n_bitrates,
547 mask))
548 return;
549 } else if (*rate_flags & IEEE80211_TX_RC_MCS) {
550 /* handle HT rates */
Lorenzo Bianconi90c66bd2015-08-06 23:47:31 +0200551 if (rate_idx_match_mcs_mask(rate_idx, mcs_mask))
Simon Wunderlich19468412012-01-28 17:25:33 +0100552 return;
553
554 /* also try the legacy rates. */
Lorenzo Bianconi90c66bd2015-08-06 23:47:31 +0200555 *rate_idx = 0;
Simon Wunderlich19468412012-01-28 17:25:33 +0100556 /* keep protection flags */
Lorenzo Bianconi90c66bd2015-08-06 23:47:31 +0200557 *rate_flags &= (IEEE80211_TX_RC_USE_RTS_CTS |
558 IEEE80211_TX_RC_USE_CTS_PROTECT |
559 IEEE80211_TX_RC_USE_SHORT_PREAMBLE);
560 if (rate_idx_match_legacy_mask(rate_idx, sband->n_bitrates,
561 mask))
Simon Wunderlich19468412012-01-28 17:25:33 +0100562 return;
Lorenzo Bianconib119ad62015-08-06 23:47:33 +0200563 } else {
Simon Wunderlich19468412012-01-28 17:25:33 +0100564 /* handle legacy rates */
Lorenzo Bianconi90c66bd2015-08-06 23:47:31 +0200565 if (rate_idx_match_legacy_mask(rate_idx, sband->n_bitrates,
566 mask))
Simon Wunderlich19468412012-01-28 17:25:33 +0100567 return;
568
569 /* if HT BSS, and we handle a data frame, also try HT rates */
Simon Wunderlich0418a442013-05-16 13:00:31 +0200570 switch (chan_width) {
571 case NL80211_CHAN_WIDTH_20_NOHT:
572 case NL80211_CHAN_WIDTH_5:
573 case NL80211_CHAN_WIDTH_10:
Simon Wunderlich19468412012-01-28 17:25:33 +0100574 return;
Simon Wunderlich0418a442013-05-16 13:00:31 +0200575 default:
576 break;
577 }
Simon Wunderlich19468412012-01-28 17:25:33 +0100578
Lorenzo Bianconi90c66bd2015-08-06 23:47:31 +0200579 *rate_idx = 0;
Simon Wunderlich19468412012-01-28 17:25:33 +0100580 /* keep protection flags */
Lorenzo Bianconi90c66bd2015-08-06 23:47:31 +0200581 *rate_flags &= (IEEE80211_TX_RC_USE_RTS_CTS |
582 IEEE80211_TX_RC_USE_CTS_PROTECT |
583 IEEE80211_TX_RC_USE_SHORT_PREAMBLE);
Simon Wunderlich19468412012-01-28 17:25:33 +0100584
Lorenzo Bianconi90c66bd2015-08-06 23:47:31 +0200585 *rate_flags |= IEEE80211_TX_RC_MCS;
Simon Wunderlich19468412012-01-28 17:25:33 +0100586
Felix Fietkau0d528d82013-04-22 16:14:41 +0200587 if (chan_width == NL80211_CHAN_WIDTH_40)
Lorenzo Bianconi90c66bd2015-08-06 23:47:31 +0200588 *rate_flags |= IEEE80211_TX_RC_40_MHZ_WIDTH;
Simon Wunderlich19468412012-01-28 17:25:33 +0100589
Lorenzo Bianconi90c66bd2015-08-06 23:47:31 +0200590 if (rate_idx_match_mcs_mask(rate_idx, mcs_mask))
Jouni Malinen37eb0b12010-01-06 13:09:08 +0200591 return;
Jouni Malinen37eb0b12010-01-06 13:09:08 +0200592 }
593
594 /*
595 * Uh.. No suitable rate exists. This should not really happen with
596 * sane TX rate mask configurations. However, should someone manage to
597 * configure supported rates and TX rate mask in incompatible way,
598 * allow the frame to be transmitted with whatever the rate control
599 * selected.
600 */
601}
602
Felix Fietkau0d528d82013-04-22 16:14:41 +0200603static void rate_fixup_ratelist(struct ieee80211_vif *vif,
604 struct ieee80211_supported_band *sband,
605 struct ieee80211_tx_info *info,
606 struct ieee80211_tx_rate *rates,
607 int max_rates)
608{
609 struct ieee80211_rate *rate;
610 bool inval = false;
611 int i;
612
613 /*
614 * Set up the RTS/CTS rate as the fastest basic rate
615 * that is not faster than the data rate unless there
616 * is no basic rate slower than the data rate, in which
617 * case we pick the slowest basic rate
618 *
619 * XXX: Should this check all retry rates?
620 */
Felix Fietkau336004e2014-11-21 23:29:14 +0100621 if (!(rates[0].flags &
622 (IEEE80211_TX_RC_MCS | IEEE80211_TX_RC_VHT_MCS))) {
Felix Fietkau0d528d82013-04-22 16:14:41 +0200623 u32 basic_rates = vif->bss_conf.basic_rates;
Karl Beldanc7abf252014-10-13 14:34:41 +0200624 s8 baserate = basic_rates ? ffs(basic_rates) - 1 : 0;
Felix Fietkau0d528d82013-04-22 16:14:41 +0200625
626 rate = &sband->bitrates[rates[0].idx];
627
628 for (i = 0; i < sband->n_bitrates; i++) {
629 /* must be a basic rate */
630 if (!(basic_rates & BIT(i)))
631 continue;
632 /* must not be faster than the data rate */
633 if (sband->bitrates[i].bitrate > rate->bitrate)
634 continue;
635 /* maximum */
636 if (sband->bitrates[baserate].bitrate <
637 sband->bitrates[i].bitrate)
638 baserate = i;
639 }
640
641 info->control.rts_cts_rate_idx = baserate;
642 }
643
644 for (i = 0; i < max_rates; i++) {
645 /*
646 * make sure there's no valid rate following
647 * an invalid one, just in case drivers don't
648 * take the API seriously to stop at -1.
649 */
650 if (inval) {
651 rates[i].idx = -1;
652 continue;
653 }
654 if (rates[i].idx < 0) {
655 inval = true;
656 continue;
657 }
658
659 /*
660 * For now assume MCS is already set up correctly, this
661 * needs to be fixed.
662 */
663 if (rates[i].flags & IEEE80211_TX_RC_MCS) {
664 WARN_ON(rates[i].idx > 76);
665
666 if (!(rates[i].flags & IEEE80211_TX_RC_USE_RTS_CTS) &&
667 info->control.use_cts_prot)
668 rates[i].flags |=
669 IEEE80211_TX_RC_USE_CTS_PROTECT;
670 continue;
671 }
672
673 if (rates[i].flags & IEEE80211_TX_RC_VHT_MCS) {
674 WARN_ON(ieee80211_rate_get_vht_mcs(&rates[i]) > 9);
675 continue;
676 }
677
678 /* set up RTS protection if desired */
679 if (info->control.use_rts) {
680 rates[i].flags |= IEEE80211_TX_RC_USE_RTS_CTS;
681 info->control.use_cts_prot = false;
682 }
683
684 /* RC is busted */
685 if (WARN_ON_ONCE(rates[i].idx >= sband->n_bitrates)) {
686 rates[i].idx = -1;
687 continue;
688 }
689
690 rate = &sband->bitrates[rates[i].idx];
691
692 /* set up short preamble */
693 if (info->control.short_preamble &&
694 rate->flags & IEEE80211_RATE_SHORT_PREAMBLE)
695 rates[i].flags |= IEEE80211_TX_RC_USE_SHORT_PREAMBLE;
696
697 /* set up G protection */
698 if (!(rates[i].flags & IEEE80211_TX_RC_USE_RTS_CTS) &&
699 info->control.use_cts_prot &&
700 rate->flags & IEEE80211_RATE_ERP_G)
701 rates[i].flags |= IEEE80211_TX_RC_USE_CTS_PROTECT;
702 }
703}
704
705
706static void rate_control_fill_sta_table(struct ieee80211_sta *sta,
707 struct ieee80211_tx_info *info,
708 struct ieee80211_tx_rate *rates,
709 int max_rates)
710{
711 struct ieee80211_sta_rates *ratetbl = NULL;
712 int i;
713
714 if (sta && !info->control.skip_table)
715 ratetbl = rcu_dereference(sta->rates);
716
717 /* Fill remaining rate slots with data from the sta rate table. */
718 max_rates = min_t(int, max_rates, IEEE80211_TX_RATE_TABLE_SIZE);
719 for (i = 0; i < max_rates; i++) {
720 if (i < ARRAY_SIZE(info->control.rates) &&
721 info->control.rates[i].idx >= 0 &&
722 info->control.rates[i].count) {
723 if (rates != info->control.rates)
724 rates[i] = info->control.rates[i];
725 } else if (ratetbl) {
726 rates[i].idx = ratetbl->rate[i].idx;
727 rates[i].flags = ratetbl->rate[i].flags;
728 if (info->control.use_rts)
729 rates[i].count = ratetbl->rate[i].count_rts;
730 else if (info->control.use_cts_prot)
731 rates[i].count = ratetbl->rate[i].count_cts;
732 else
733 rates[i].count = ratetbl->rate[i].count;
734 } else {
735 rates[i].idx = -1;
736 rates[i].count = 0;
737 }
738
739 if (rates[i].idx < 0 || !rates[i].count)
740 break;
741 }
742}
743
Lorenzo Bianconi90c66bd2015-08-06 23:47:31 +0200744static bool rate_control_cap_mask(struct ieee80211_sub_if_data *sdata,
745 struct ieee80211_supported_band *sband,
746 struct ieee80211_sta *sta, u32 *mask,
Lorenzo Bianconib119ad62015-08-06 23:47:33 +0200747 u8 mcs_mask[IEEE80211_HT_MCS_MASK_LEN],
748 u16 vht_mask[NL80211_VHT_NSS_MAX])
Lorenzo Bianconi90c66bd2015-08-06 23:47:31 +0200749{
750 u32 i, flags;
751
752 *mask = sdata->rc_rateidx_mask[sband->band];
753 flags = ieee80211_chandef_rate_flags(&sdata->vif.bss_conf.chandef);
754 for (i = 0; i < sband->n_bitrates; i++) {
755 if ((flags & sband->bitrates[i].flags) != flags)
756 *mask &= ~BIT(i);
757 }
758
759 if (*mask == (1 << sband->n_bitrates) - 1 &&
Lorenzo Bianconib119ad62015-08-06 23:47:33 +0200760 !sdata->rc_has_mcs_mask[sband->band] &&
761 !sdata->rc_has_vht_mcs_mask[sband->band])
Lorenzo Bianconi90c66bd2015-08-06 23:47:31 +0200762 return false;
763
764 if (sdata->rc_has_mcs_mask[sband->band])
765 memcpy(mcs_mask, sdata->rc_rateidx_mcs_mask[sband->band],
766 IEEE80211_HT_MCS_MASK_LEN);
767 else
768 memset(mcs_mask, 0xff, IEEE80211_HT_MCS_MASK_LEN);
769
Lorenzo Bianconib119ad62015-08-06 23:47:33 +0200770 if (sdata->rc_has_vht_mcs_mask[sband->band])
771 memcpy(vht_mask, sdata->rc_rateidx_vht_mcs_mask[sband->band],
772 sizeof(u16) * NL80211_VHT_NSS_MAX);
773 else
774 memset(vht_mask, 0xff, sizeof(u16) * NL80211_VHT_NSS_MAX);
775
Lorenzo Bianconi90c66bd2015-08-06 23:47:31 +0200776 if (sta) {
Lorenzo Bianconib119ad62015-08-06 23:47:33 +0200777 __le16 sta_vht_cap;
778 u16 sta_vht_mask[NL80211_VHT_NSS_MAX];
779
Lorenzo Bianconi90c66bd2015-08-06 23:47:31 +0200780 /* Filter out rates that the STA does not support */
781 *mask &= sta->supp_rates[sband->band];
Thierry Reding98a1f822015-08-26 12:22:14 +0200782 for (i = 0; i < IEEE80211_HT_MCS_MASK_LEN; i++)
Lorenzo Bianconi90c66bd2015-08-06 23:47:31 +0200783 mcs_mask[i] &= sta->ht_cap.mcs.rx_mask[i];
Lorenzo Bianconib119ad62015-08-06 23:47:33 +0200784
785 sta_vht_cap = sta->vht_cap.vht_mcs.rx_mcs_map;
786 ieee80211_get_vht_mask_from_cap(sta_vht_cap, sta_vht_mask);
787 for (i = 0; i < NL80211_VHT_NSS_MAX; i++)
788 vht_mask[i] &= sta_vht_mask[i];
Lorenzo Bianconi90c66bd2015-08-06 23:47:31 +0200789 }
790
791 return true;
792}
793
Lorenzo Bianconie9108672015-08-06 23:47:32 +0200794static void
795rate_control_apply_mask_ratetbl(struct sta_info *sta,
796 struct ieee80211_supported_band *sband,
797 struct ieee80211_sta_rates *rates)
798{
799 int i;
800 u32 mask;
801 u8 mcs_mask[IEEE80211_HT_MCS_MASK_LEN];
Lorenzo Bianconib119ad62015-08-06 23:47:33 +0200802 u16 vht_mask[NL80211_VHT_NSS_MAX];
Lorenzo Bianconie9108672015-08-06 23:47:32 +0200803 enum nl80211_chan_width chan_width;
804
805 if (!rate_control_cap_mask(sta->sdata, sband, &sta->sta, &mask,
Lorenzo Bianconib119ad62015-08-06 23:47:33 +0200806 mcs_mask, vht_mask))
Lorenzo Bianconie9108672015-08-06 23:47:32 +0200807 return;
808
809 chan_width = sta->sdata->vif.bss_conf.chandef.width;
810 for (i = 0; i < IEEE80211_TX_RATE_TABLE_SIZE; i++) {
811 if (rates->rate[i].idx < 0)
812 break;
813
814 rate_idx_match_mask(&rates->rate[i].idx, &rates->rate[i].flags,
Lorenzo Bianconib119ad62015-08-06 23:47:33 +0200815 sband, chan_width, mask, mcs_mask,
816 vht_mask);
Lorenzo Bianconie9108672015-08-06 23:47:32 +0200817 }
818}
819
Felix Fietkau0d528d82013-04-22 16:14:41 +0200820static void rate_control_apply_mask(struct ieee80211_sub_if_data *sdata,
821 struct ieee80211_sta *sta,
822 struct ieee80211_supported_band *sband,
Felix Fietkau0d528d82013-04-22 16:14:41 +0200823 struct ieee80211_tx_rate *rates,
824 int max_rates)
825{
826 enum nl80211_chan_width chan_width;
827 u8 mcs_mask[IEEE80211_HT_MCS_MASK_LEN];
Felix Fietkau0d528d82013-04-22 16:14:41 +0200828 u32 mask;
Lorenzo Bianconib119ad62015-08-06 23:47:33 +0200829 u16 rate_flags, vht_mask[NL80211_VHT_NSS_MAX];
Felix Fietkau0d528d82013-04-22 16:14:41 +0200830 int i;
831
832 /*
833 * Try to enforce the rateidx mask the user wanted. skip this if the
834 * default mask (allow all rates) is used to save some processing for
835 * the common case.
836 */
Lorenzo Bianconib119ad62015-08-06 23:47:33 +0200837 if (!rate_control_cap_mask(sdata, sband, sta, &mask, mcs_mask,
838 vht_mask))
Felix Fietkau0d528d82013-04-22 16:14:41 +0200839 return;
840
Felix Fietkau0d528d82013-04-22 16:14:41 +0200841 /*
842 * Make sure the rate index selected for each TX rate is
843 * included in the configured mask and change the rate indexes
844 * if needed.
845 */
846 chan_width = sdata->vif.bss_conf.chandef.width;
847 for (i = 0; i < max_rates; i++) {
848 /* Skip invalid rates */
849 if (rates[i].idx < 0)
850 break;
851
Lorenzo Bianconi90c66bd2015-08-06 23:47:31 +0200852 rate_flags = rates[i].flags;
853 rate_idx_match_mask(&rates[i].idx, &rate_flags, sband,
Lorenzo Bianconib119ad62015-08-06 23:47:33 +0200854 chan_width, mask, mcs_mask, vht_mask);
Lorenzo Bianconi90c66bd2015-08-06 23:47:31 +0200855 rates[i].flags = rate_flags;
Felix Fietkau0d528d82013-04-22 16:14:41 +0200856 }
857}
858
859void ieee80211_get_tx_rates(struct ieee80211_vif *vif,
860 struct ieee80211_sta *sta,
861 struct sk_buff *skb,
862 struct ieee80211_tx_rate *dest,
863 int max_rates)
864{
865 struct ieee80211_sub_if_data *sdata;
866 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
867 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
868 struct ieee80211_supported_band *sband;
869
870 rate_control_fill_sta_table(sta, info, dest, max_rates);
871
872 if (!vif)
873 return;
874
875 sdata = vif_to_sdata(vif);
876 sband = sdata->local->hw.wiphy->bands[info->band];
877
878 if (ieee80211_is_data(hdr->frame_control))
Lorenzo Bianconi35225eb2015-08-06 23:47:30 +0200879 rate_control_apply_mask(sdata, sta, sband, dest, max_rates);
Felix Fietkau0d528d82013-04-22 16:14:41 +0200880
881 if (dest[0].idx < 0)
Andrei Otcheretianski1d2d3502013-10-14 11:04:28 +0200882 __rate_control_send_low(&sdata->local->hw, sband, sta, info,
883 sdata->rc_rateidx_mask[info->band]);
Felix Fietkau0d528d82013-04-22 16:14:41 +0200884
885 if (sta)
886 rate_fixup_ratelist(vif, sband, info, dest, max_rates);
887}
888EXPORT_SYMBOL(ieee80211_get_tx_rates);
889
Johannes Berg4b7679a2008-09-18 18:14:18 +0200890void rate_control_get_rate(struct ieee80211_sub_if_data *sdata,
Johannes Berge6a98542008-10-21 12:40:02 +0200891 struct sta_info *sta,
892 struct ieee80211_tx_rate_control *txrc)
Mattias Nissler1abbe492007-12-20 13:50:07 +0100893{
Johannes Berg4b7679a2008-09-18 18:14:18 +0200894 struct rate_control_ref *ref = sdata->local->rate_ctrl;
895 void *priv_sta = NULL;
896 struct ieee80211_sta *ista = NULL;
Johannes Berge6a98542008-10-21 12:40:02 +0200897 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(txrc->skb);
Mattias Nissler1abbe492007-12-20 13:50:07 +0100898 int i;
Mattias Nissler1abbe492007-12-20 13:50:07 +0100899
Johannes Berge6a98542008-10-21 12:40:02 +0200900 for (i = 0; i < IEEE80211_TX_MAX_RATES; i++) {
901 info->control.rates[i].idx = -1;
902 info->control.rates[i].flags = 0;
Mohammed Shafi Shajakhan8617b092012-02-20 10:05:31 +0530903 info->control.rates[i].count = 0;
Mattias Nissler1abbe492007-12-20 13:50:07 +0100904 }
Johannes Berge6a98542008-10-21 12:40:02 +0200905
Johannes Berg583a7a32019-05-29 15:25:36 +0300906 if (rate_control_send_low(sta ? &sta->sta : NULL, txrc))
Juuso Oikarinen5affcd6ba2010-02-12 10:05:45 +0200907 return;
908
Johannes Berg583a7a32019-05-29 15:25:36 +0300909 if (ieee80211_hw_check(&sdata->local->hw, HAS_RATE_CONTROL))
Johannes Berg1e87fec2019-05-16 11:44:52 +0200910 return;
911
Johannes Bergbd718fc2019-05-29 15:25:35 +0300912 if (sta && test_sta_flag(sta, WLAN_STA_RATE_CONTROL)) {
913 ista = &sta->sta;
914 priv_sta = sta->rate_ctrl_priv;
915 }
916
Johannes Berg35c347a2015-03-05 16:10:08 +0100917 if (ista) {
918 spin_lock_bh(&sta->rate_ctrl_lock);
919 ref->ops->get_rate(ref->priv, ista, priv_sta, txrc);
920 spin_unlock_bh(&sta->rate_ctrl_lock);
921 } else {
Johannes Berg1e87fec2019-05-16 11:44:52 +0200922 rate_control_send_low(NULL, txrc);
Johannes Berg35c347a2015-03-05 16:10:08 +0100923 }
Johannes Berge6a98542008-10-21 12:40:02 +0200924
Johannes Berg30686bf2015-06-02 21:39:54 +0200925 if (ieee80211_hw_check(&sdata->local->hw, SUPPORTS_RC_TABLE))
Felix Fietkau0d528d82013-04-22 16:14:41 +0200926 return;
Felix Fietkau2ffbe6d2013-04-16 13:38:42 +0200927
Felix Fietkau0d528d82013-04-22 16:14:41 +0200928 ieee80211_get_tx_rates(&sdata->vif, ista, txrc->skb,
929 info->control.rates,
930 ARRAY_SIZE(info->control.rates));
Mattias Nissler1abbe492007-12-20 13:50:07 +0100931}
932
Felix Fietkau0d528d82013-04-22 16:14:41 +0200933int rate_control_set_rates(struct ieee80211_hw *hw,
934 struct ieee80211_sta *pubsta,
935 struct ieee80211_sta_rates *rates)
936{
Johannes Bergf815e2b2014-11-19 00:10:42 +0100937 struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
Felix Fietkauf6b3d852013-05-03 10:01:03 +0200938 struct ieee80211_sta_rates *old;
Lorenzo Bianconie9108672015-08-06 23:47:32 +0200939 struct ieee80211_supported_band *sband;
Felix Fietkau0d528d82013-04-22 16:14:41 +0200940
Mohammed Shafi Shajakhan21a8e9d2017-04-27 12:45:38 +0530941 sband = ieee80211_get_sband(sta->sdata);
942 if (!sband)
943 return -EINVAL;
Lorenzo Bianconie9108672015-08-06 23:47:32 +0200944 rate_control_apply_mask_ratetbl(sta, sband, rates);
Felix Fietkauf6b3d852013-05-03 10:01:03 +0200945 /*
946 * mac80211 guarantees that this function will not be called
947 * concurrently, so the following RCU access is safe, even without
948 * extra locking. This can not be checked easily, so we just set
949 * the condition to true.
950 */
951 old = rcu_dereference_protected(pubsta->rates, true);
Felix Fietkau0d528d82013-04-22 16:14:41 +0200952 rcu_assign_pointer(pubsta->rates, rates);
953 if (old)
954 kfree_rcu(old, rcu_head);
955
Johannes Bergf815e2b2014-11-19 00:10:42 +0100956 drv_sta_rate_tbl_update(hw_to_local(hw), sta->sdata, pubsta);
957
Toke Høiland-Jørgensen484a54c2017-04-06 11:38:26 +0200958 ieee80211_sta_set_expected_throughput(pubsta, sta_get_expected_throughput(sta));
959
Felix Fietkau0d528d82013-04-22 16:14:41 +0200960 return 0;
961}
962EXPORT_SYMBOL(rate_control_set_rates);
963
Johannes Bergff688082007-07-27 15:43:23 +0200964int ieee80211_init_rate_ctrl_alg(struct ieee80211_local *local,
965 const char *name)
966{
Johannes Berg889cbb92012-01-17 10:33:29 +0100967 struct rate_control_ref *ref;
Johannes Bergff688082007-07-27 15:43:23 +0200968
969 ASSERT_RTNL();
Johannes Bergaf65cd962009-11-17 18:18:36 +0100970
Johannes Berg3b8d81e02009-06-17 17:43:56 +0200971 if (local->open_count)
Johannes Bergff688082007-07-27 15:43:23 +0200972 return -EBUSY;
973
Johannes Berg30686bf2015-06-02 21:39:54 +0200974 if (ieee80211_hw_check(&local->hw, HAS_RATE_CONTROL)) {
Johannes Bergaf65cd962009-11-17 18:18:36 +0100975 if (WARN_ON(!local->ops->set_rts_threshold))
976 return -EINVAL;
977 return 0;
978 }
979
Johannes Bergff688082007-07-27 15:43:23 +0200980 ref = rate_control_alloc(name, local);
981 if (!ref) {
Joe Perches0fb9a9e2010-08-20 16:25:38 -0700982 wiphy_warn(local->hw.wiphy,
983 "Failed to select rate control algorithm\n");
Johannes Bergff688082007-07-27 15:43:23 +0200984 return -ENOENT;
985 }
986
Johannes Berg889cbb92012-01-17 10:33:29 +0100987 WARN_ON(local->rate_ctrl);
Johannes Bergff688082007-07-27 15:43:23 +0200988 local->rate_ctrl = ref;
Johannes Bergff688082007-07-27 15:43:23 +0200989
Joe Perches0fb9a9e2010-08-20 16:25:38 -0700990 wiphy_debug(local->hw.wiphy, "Selected rate control algorithm '%s'\n",
991 ref->ops->name);
Johannes Bergff688082007-07-27 15:43:23 +0200992
Johannes Bergff688082007-07-27 15:43:23 +0200993 return 0;
994}
995
996void rate_control_deinitialize(struct ieee80211_local *local)
997{
998 struct rate_control_ref *ref;
999
1000 ref = local->rate_ctrl;
Johannes Bergaf65cd962009-11-17 18:18:36 +01001001
1002 if (!ref)
1003 return;
1004
Johannes Bergff688082007-07-27 15:43:23 +02001005 local->rate_ctrl = NULL;
Johannes Berga8589582017-02-15 15:02:07 +01001006 rate_control_free(local, ref);
Johannes Bergff688082007-07-27 15:43:23 +02001007}