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