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