ath11k: add interface_modes to hw_params
[linux-2.6-microblaze.git] / drivers / net / wireless / ath / ath11k / mac.c
1 // SPDX-License-Identifier: BSD-3-Clause-Clear
2 /*
3  * Copyright (c) 2018-2019 The Linux Foundation. All rights reserved.
4  */
5
6 #include <net/mac80211.h>
7 #include <linux/etherdevice.h>
8 #include "mac.h"
9 #include "core.h"
10 #include "debug.h"
11 #include "wmi.h"
12 #include "hw.h"
13 #include "dp_tx.h"
14 #include "dp_rx.h"
15 #include "testmode.h"
16 #include "peer.h"
17 #include "debugfs_sta.h"
18
19 #define CHAN2G(_channel, _freq, _flags) { \
20         .band                   = NL80211_BAND_2GHZ, \
21         .hw_value               = (_channel), \
22         .center_freq            = (_freq), \
23         .flags                  = (_flags), \
24         .max_antenna_gain       = 0, \
25         .max_power              = 30, \
26 }
27
28 #define CHAN5G(_channel, _freq, _flags) { \
29         .band                   = NL80211_BAND_5GHZ, \
30         .hw_value               = (_channel), \
31         .center_freq            = (_freq), \
32         .flags                  = (_flags), \
33         .max_antenna_gain       = 0, \
34         .max_power              = 30, \
35 }
36
37 #define CHAN6G(_channel, _freq, _flags) { \
38         .band                   = NL80211_BAND_6GHZ, \
39         .hw_value               = (_channel), \
40         .center_freq            = (_freq), \
41         .flags                  = (_flags), \
42         .max_antenna_gain       = 0, \
43         .max_power              = 30, \
44 }
45
46 static const struct ieee80211_channel ath11k_2ghz_channels[] = {
47         CHAN2G(1, 2412, 0),
48         CHAN2G(2, 2417, 0),
49         CHAN2G(3, 2422, 0),
50         CHAN2G(4, 2427, 0),
51         CHAN2G(5, 2432, 0),
52         CHAN2G(6, 2437, 0),
53         CHAN2G(7, 2442, 0),
54         CHAN2G(8, 2447, 0),
55         CHAN2G(9, 2452, 0),
56         CHAN2G(10, 2457, 0),
57         CHAN2G(11, 2462, 0),
58         CHAN2G(12, 2467, 0),
59         CHAN2G(13, 2472, 0),
60         CHAN2G(14, 2484, 0),
61 };
62
63 static const struct ieee80211_channel ath11k_5ghz_channels[] = {
64         CHAN5G(36, 5180, 0),
65         CHAN5G(40, 5200, 0),
66         CHAN5G(44, 5220, 0),
67         CHAN5G(48, 5240, 0),
68         CHAN5G(52, 5260, 0),
69         CHAN5G(56, 5280, 0),
70         CHAN5G(60, 5300, 0),
71         CHAN5G(64, 5320, 0),
72         CHAN5G(100, 5500, 0),
73         CHAN5G(104, 5520, 0),
74         CHAN5G(108, 5540, 0),
75         CHAN5G(112, 5560, 0),
76         CHAN5G(116, 5580, 0),
77         CHAN5G(120, 5600, 0),
78         CHAN5G(124, 5620, 0),
79         CHAN5G(128, 5640, 0),
80         CHAN5G(132, 5660, 0),
81         CHAN5G(136, 5680, 0),
82         CHAN5G(140, 5700, 0),
83         CHAN5G(144, 5720, 0),
84         CHAN5G(149, 5745, 0),
85         CHAN5G(153, 5765, 0),
86         CHAN5G(157, 5785, 0),
87         CHAN5G(161, 5805, 0),
88         CHAN5G(165, 5825, 0),
89         CHAN5G(169, 5845, 0),
90         CHAN5G(173, 5865, 0),
91 };
92
93 static const struct ieee80211_channel ath11k_6ghz_channels[] = {
94         CHAN6G(1, 5955, 0),
95         CHAN6G(5, 5975, 0),
96         CHAN6G(9, 5995, 0),
97         CHAN6G(13, 6015, 0),
98         CHAN6G(17, 6035, 0),
99         CHAN6G(21, 6055, 0),
100         CHAN6G(25, 6075, 0),
101         CHAN6G(29, 6095, 0),
102         CHAN6G(33, 6115, 0),
103         CHAN6G(37, 6135, 0),
104         CHAN6G(41, 6155, 0),
105         CHAN6G(45, 6175, 0),
106         CHAN6G(49, 6195, 0),
107         CHAN6G(53, 6215, 0),
108         CHAN6G(57, 6235, 0),
109         CHAN6G(61, 6255, 0),
110         CHAN6G(65, 6275, 0),
111         CHAN6G(69, 6295, 0),
112         CHAN6G(73, 6315, 0),
113         CHAN6G(77, 6335, 0),
114         CHAN6G(81, 6355, 0),
115         CHAN6G(85, 6375, 0),
116         CHAN6G(89, 6395, 0),
117         CHAN6G(93, 6415, 0),
118         CHAN6G(97, 6435, 0),
119         CHAN6G(101, 6455, 0),
120         CHAN6G(105, 6475, 0),
121         CHAN6G(109, 6495, 0),
122         CHAN6G(113, 6515, 0),
123         CHAN6G(117, 6535, 0),
124         CHAN6G(121, 6555, 0),
125         CHAN6G(125, 6575, 0),
126         CHAN6G(129, 6595, 0),
127         CHAN6G(133, 6615, 0),
128         CHAN6G(137, 6635, 0),
129         CHAN6G(141, 6655, 0),
130         CHAN6G(145, 6675, 0),
131         CHAN6G(149, 6695, 0),
132         CHAN6G(153, 6715, 0),
133         CHAN6G(157, 6735, 0),
134         CHAN6G(161, 6755, 0),
135         CHAN6G(165, 6775, 0),
136         CHAN6G(169, 6795, 0),
137         CHAN6G(173, 6815, 0),
138         CHAN6G(177, 6835, 0),
139         CHAN6G(181, 6855, 0),
140         CHAN6G(185, 6875, 0),
141         CHAN6G(189, 6895, 0),
142         CHAN6G(193, 6915, 0),
143         CHAN6G(197, 6935, 0),
144         CHAN6G(201, 6955, 0),
145         CHAN6G(205, 6975, 0),
146         CHAN6G(209, 6995, 0),
147         CHAN6G(213, 7015, 0),
148         CHAN6G(217, 7035, 0),
149         CHAN6G(221, 7055, 0),
150         CHAN6G(225, 7075, 0),
151         CHAN6G(229, 7095, 0),
152         CHAN6G(233, 7115, 0),
153 };
154
155 static struct ieee80211_rate ath11k_legacy_rates[] = {
156         { .bitrate = 10,
157           .hw_value = ATH11K_HW_RATE_CCK_LP_1M },
158         { .bitrate = 20,
159           .hw_value = ATH11K_HW_RATE_CCK_LP_2M,
160           .hw_value_short = ATH11K_HW_RATE_CCK_SP_2M,
161           .flags = IEEE80211_RATE_SHORT_PREAMBLE },
162         { .bitrate = 55,
163           .hw_value = ATH11K_HW_RATE_CCK_LP_5_5M,
164           .hw_value_short = ATH11K_HW_RATE_CCK_SP_5_5M,
165           .flags = IEEE80211_RATE_SHORT_PREAMBLE },
166         { .bitrate = 110,
167           .hw_value = ATH11K_HW_RATE_CCK_LP_11M,
168           .hw_value_short = ATH11K_HW_RATE_CCK_SP_11M,
169           .flags = IEEE80211_RATE_SHORT_PREAMBLE },
170
171         { .bitrate = 60, .hw_value = ATH11K_HW_RATE_OFDM_6M },
172         { .bitrate = 90, .hw_value = ATH11K_HW_RATE_OFDM_9M },
173         { .bitrate = 120, .hw_value = ATH11K_HW_RATE_OFDM_12M },
174         { .bitrate = 180, .hw_value = ATH11K_HW_RATE_OFDM_18M },
175         { .bitrate = 240, .hw_value = ATH11K_HW_RATE_OFDM_24M },
176         { .bitrate = 360, .hw_value = ATH11K_HW_RATE_OFDM_36M },
177         { .bitrate = 480, .hw_value = ATH11K_HW_RATE_OFDM_48M },
178         { .bitrate = 540, .hw_value = ATH11K_HW_RATE_OFDM_54M },
179 };
180
181 static const int
182 ath11k_phymodes[NUM_NL80211_BANDS][ATH11K_CHAN_WIDTH_NUM] = {
183         [NL80211_BAND_2GHZ] = {
184                         [NL80211_CHAN_WIDTH_5] = MODE_UNKNOWN,
185                         [NL80211_CHAN_WIDTH_10] = MODE_UNKNOWN,
186                         [NL80211_CHAN_WIDTH_20_NOHT] = MODE_11AX_HE20_2G,
187                         [NL80211_CHAN_WIDTH_20] = MODE_11AX_HE20_2G,
188                         [NL80211_CHAN_WIDTH_40] = MODE_11AX_HE40_2G,
189                         [NL80211_CHAN_WIDTH_80] = MODE_11AX_HE80_2G,
190                         [NL80211_CHAN_WIDTH_80P80] = MODE_UNKNOWN,
191                         [NL80211_CHAN_WIDTH_160] = MODE_UNKNOWN,
192         },
193         [NL80211_BAND_5GHZ] = {
194                         [NL80211_CHAN_WIDTH_5] = MODE_UNKNOWN,
195                         [NL80211_CHAN_WIDTH_10] = MODE_UNKNOWN,
196                         [NL80211_CHAN_WIDTH_20_NOHT] = MODE_11AX_HE20,
197                         [NL80211_CHAN_WIDTH_20] = MODE_11AX_HE20,
198                         [NL80211_CHAN_WIDTH_40] = MODE_11AX_HE40,
199                         [NL80211_CHAN_WIDTH_80] = MODE_11AX_HE80,
200                         [NL80211_CHAN_WIDTH_160] = MODE_11AX_HE160,
201                         [NL80211_CHAN_WIDTH_80P80] = MODE_11AX_HE80_80,
202         },
203         [NL80211_BAND_6GHZ] = {
204                         [NL80211_CHAN_WIDTH_5] = MODE_UNKNOWN,
205                         [NL80211_CHAN_WIDTH_10] = MODE_UNKNOWN,
206                         [NL80211_CHAN_WIDTH_20_NOHT] = MODE_11AX_HE20,
207                         [NL80211_CHAN_WIDTH_20] = MODE_11AX_HE20,
208                         [NL80211_CHAN_WIDTH_40] = MODE_11AX_HE40,
209                         [NL80211_CHAN_WIDTH_80] = MODE_11AX_HE80,
210                         [NL80211_CHAN_WIDTH_160] = MODE_11AX_HE160,
211                         [NL80211_CHAN_WIDTH_80P80] = MODE_11AX_HE80_80,
212         },
213
214 };
215
216 const struct htt_rx_ring_tlv_filter ath11k_mac_mon_status_filter_default = {
217         .rx_filter = HTT_RX_FILTER_TLV_FLAGS_MPDU_START |
218                      HTT_RX_FILTER_TLV_FLAGS_PPDU_END |
219                      HTT_RX_FILTER_TLV_FLAGS_PPDU_END_STATUS_DONE,
220         .pkt_filter_flags0 = HTT_RX_FP_MGMT_FILTER_FLAGS0,
221         .pkt_filter_flags1 = HTT_RX_FP_MGMT_FILTER_FLAGS1,
222         .pkt_filter_flags2 = HTT_RX_FP_CTRL_FILTER_FLASG2,
223         .pkt_filter_flags3 = HTT_RX_FP_DATA_FILTER_FLASG3 |
224                              HTT_RX_FP_CTRL_FILTER_FLASG3
225 };
226
227 #define ATH11K_MAC_FIRST_OFDM_RATE_IDX 4
228 #define ath11k_g_rates ath11k_legacy_rates
229 #define ath11k_g_rates_size (ARRAY_SIZE(ath11k_legacy_rates))
230 #define ath11k_a_rates (ath11k_legacy_rates + 4)
231 #define ath11k_a_rates_size (ARRAY_SIZE(ath11k_legacy_rates) - 4)
232
233 #define ATH11K_MAC_SCAN_TIMEOUT_MSECS 200 /* in msecs */
234
235 static const u32 ath11k_smps_map[] = {
236         [WLAN_HT_CAP_SM_PS_STATIC] = WMI_PEER_SMPS_STATIC,
237         [WLAN_HT_CAP_SM_PS_DYNAMIC] = WMI_PEER_SMPS_DYNAMIC,
238         [WLAN_HT_CAP_SM_PS_INVALID] = WMI_PEER_SMPS_PS_NONE,
239         [WLAN_HT_CAP_SM_PS_DISABLED] = WMI_PEER_SMPS_PS_NONE,
240 };
241
242 static int ath11k_start_vdev_delay(struct ieee80211_hw *hw,
243                                    struct ieee80211_vif *vif);
244
245 u8 ath11k_mac_bw_to_mac80211_bw(u8 bw)
246 {
247         u8 ret = 0;
248
249         switch (bw) {
250         case ATH11K_BW_20:
251                 ret = RATE_INFO_BW_20;
252                 break;
253         case ATH11K_BW_40:
254                 ret = RATE_INFO_BW_40;
255                 break;
256         case ATH11K_BW_80:
257                 ret = RATE_INFO_BW_80;
258                 break;
259         case ATH11K_BW_160:
260                 ret = RATE_INFO_BW_160;
261                 break;
262         }
263
264         return ret;
265 }
266
267 enum ath11k_supported_bw ath11k_mac_mac80211_bw_to_ath11k_bw(enum rate_info_bw bw)
268 {
269         switch (bw) {
270         case RATE_INFO_BW_20:
271                 return ATH11K_BW_20;
272         case RATE_INFO_BW_40:
273                 return ATH11K_BW_40;
274         case RATE_INFO_BW_80:
275                 return ATH11K_BW_80;
276         case RATE_INFO_BW_160:
277                 return ATH11K_BW_160;
278         default:
279                 return ATH11K_BW_20;
280         }
281 }
282
283 int ath11k_mac_hw_ratecode_to_legacy_rate(u8 hw_rc, u8 preamble, u8 *rateidx,
284                                           u16 *rate)
285 {
286         /* As default, it is OFDM rates */
287         int i = ATH11K_MAC_FIRST_OFDM_RATE_IDX;
288         int max_rates_idx = ath11k_g_rates_size;
289
290         if (preamble == WMI_RATE_PREAMBLE_CCK) {
291                 hw_rc &= ~ATH11k_HW_RATECODE_CCK_SHORT_PREAM_MASK;
292                 i = 0;
293                 max_rates_idx = ATH11K_MAC_FIRST_OFDM_RATE_IDX;
294         }
295
296         while (i < max_rates_idx) {
297                 if (hw_rc == ath11k_legacy_rates[i].hw_value) {
298                         *rateidx = i;
299                         *rate = ath11k_legacy_rates[i].bitrate;
300                         return 0;
301                 }
302                 i++;
303         }
304
305         return -EINVAL;
306 }
307
308 static int get_num_chains(u32 mask)
309 {
310         int num_chains = 0;
311
312         while (mask) {
313                 if (mask & BIT(0))
314                         num_chains++;
315                 mask >>= 1;
316         }
317
318         return num_chains;
319 }
320
321 u8 ath11k_mac_bitrate_to_idx(const struct ieee80211_supported_band *sband,
322                              u32 bitrate)
323 {
324         int i;
325
326         for (i = 0; i < sband->n_bitrates; i++)
327                 if (sband->bitrates[i].bitrate == bitrate)
328                         return i;
329
330         return 0;
331 }
332
333 static u32
334 ath11k_mac_max_ht_nss(const u8 ht_mcs_mask[IEEE80211_HT_MCS_MASK_LEN])
335 {
336         int nss;
337
338         for (nss = IEEE80211_HT_MCS_MASK_LEN - 1; nss >= 0; nss--)
339                 if (ht_mcs_mask[nss])
340                         return nss + 1;
341
342         return 1;
343 }
344
345 static u32
346 ath11k_mac_max_vht_nss(const u16 vht_mcs_mask[NL80211_VHT_NSS_MAX])
347 {
348         int nss;
349
350         for (nss = NL80211_VHT_NSS_MAX - 1; nss >= 0; nss--)
351                 if (vht_mcs_mask[nss])
352                         return nss + 1;
353
354         return 1;
355 }
356
357 static u8 ath11k_parse_mpdudensity(u8 mpdudensity)
358 {
359 /* 802.11n D2.0 defined values for "Minimum MPDU Start Spacing":
360  *   0 for no restriction
361  *   1 for 1/4 us
362  *   2 for 1/2 us
363  *   3 for 1 us
364  *   4 for 2 us
365  *   5 for 4 us
366  *   6 for 8 us
367  *   7 for 16 us
368  */
369         switch (mpdudensity) {
370         case 0:
371                 return 0;
372         case 1:
373         case 2:
374         case 3:
375         /* Our lower layer calculations limit our precision to
376          * 1 microsecond
377          */
378                 return 1;
379         case 4:
380                 return 2;
381         case 5:
382                 return 4;
383         case 6:
384                 return 8;
385         case 7:
386                 return 16;
387         default:
388                 return 0;
389         }
390 }
391
392 static int ath11k_mac_vif_chan(struct ieee80211_vif *vif,
393                                struct cfg80211_chan_def *def)
394 {
395         struct ieee80211_chanctx_conf *conf;
396
397         rcu_read_lock();
398         conf = rcu_dereference(vif->chanctx_conf);
399         if (!conf) {
400                 rcu_read_unlock();
401                 return -ENOENT;
402         }
403
404         *def = conf->def;
405         rcu_read_unlock();
406
407         return 0;
408 }
409
410 static bool ath11k_mac_bitrate_is_cck(int bitrate)
411 {
412         switch (bitrate) {
413         case 10:
414         case 20:
415         case 55:
416         case 110:
417                 return true;
418         }
419
420         return false;
421 }
422
423 u8 ath11k_mac_hw_rate_to_idx(const struct ieee80211_supported_band *sband,
424                              u8 hw_rate, bool cck)
425 {
426         const struct ieee80211_rate *rate;
427         int i;
428
429         for (i = 0; i < sband->n_bitrates; i++) {
430                 rate = &sband->bitrates[i];
431
432                 if (ath11k_mac_bitrate_is_cck(rate->bitrate) != cck)
433                         continue;
434
435                 if (rate->hw_value == hw_rate)
436                         return i;
437                 else if (rate->flags & IEEE80211_RATE_SHORT_PREAMBLE &&
438                          rate->hw_value_short == hw_rate)
439                         return i;
440         }
441
442         return 0;
443 }
444
445 static u8 ath11k_mac_bitrate_to_rate(int bitrate)
446 {
447         return DIV_ROUND_UP(bitrate, 5) |
448                (ath11k_mac_bitrate_is_cck(bitrate) ? BIT(7) : 0);
449 }
450
451 static void ath11k_get_arvif_iter(void *data, u8 *mac,
452                                   struct ieee80211_vif *vif)
453 {
454         struct ath11k_vif_iter *arvif_iter = data;
455         struct ath11k_vif *arvif = (void *)vif->drv_priv;
456
457         if (arvif->vdev_id == arvif_iter->vdev_id)
458                 arvif_iter->arvif = arvif;
459 }
460
461 struct ath11k_vif *ath11k_mac_get_arvif(struct ath11k *ar, u32 vdev_id)
462 {
463         struct ath11k_vif_iter arvif_iter;
464         u32 flags;
465
466         memset(&arvif_iter, 0, sizeof(struct ath11k_vif_iter));
467         arvif_iter.vdev_id = vdev_id;
468
469         flags = IEEE80211_IFACE_ITER_RESUME_ALL;
470         ieee80211_iterate_active_interfaces_atomic(ar->hw,
471                                                    flags,
472                                                    ath11k_get_arvif_iter,
473                                                    &arvif_iter);
474         if (!arvif_iter.arvif) {
475                 ath11k_warn(ar->ab, "No VIF found for vdev %d\n", vdev_id);
476                 return NULL;
477         }
478
479         return arvif_iter.arvif;
480 }
481
482 struct ath11k_vif *ath11k_mac_get_arvif_by_vdev_id(struct ath11k_base *ab,
483                                                    u32 vdev_id)
484 {
485         int i;
486         struct ath11k_pdev *pdev;
487         struct ath11k_vif *arvif;
488
489         for (i = 0; i < ab->num_radios; i++) {
490                 pdev = rcu_dereference(ab->pdevs_active[i]);
491                 if (pdev && pdev->ar) {
492                         arvif = ath11k_mac_get_arvif(pdev->ar, vdev_id);
493                         if (arvif)
494                                 return arvif;
495                 }
496         }
497
498         return NULL;
499 }
500
501 struct ath11k *ath11k_mac_get_ar_by_vdev_id(struct ath11k_base *ab, u32 vdev_id)
502 {
503         int i;
504         struct ath11k_pdev *pdev;
505
506         for (i = 0; i < ab->num_radios; i++) {
507                 pdev = rcu_dereference(ab->pdevs_active[i]);
508                 if (pdev && pdev->ar) {
509                         if (pdev->ar->allocated_vdev_map & (1LL << vdev_id))
510                                 return pdev->ar;
511                 }
512         }
513
514         return NULL;
515 }
516
517 struct ath11k *ath11k_mac_get_ar_by_pdev_id(struct ath11k_base *ab, u32 pdev_id)
518 {
519         int i;
520         struct ath11k_pdev *pdev;
521
522         if (ab->hw_params.single_pdev_only) {
523                 pdev = rcu_dereference(ab->pdevs_active[0]);
524                 return pdev ? pdev->ar : NULL;
525         }
526
527         if (WARN_ON(pdev_id > ab->num_radios))
528                 return NULL;
529
530         for (i = 0; i < ab->num_radios; i++) {
531                 pdev = rcu_dereference(ab->pdevs_active[i]);
532
533                 if (pdev && pdev->pdev_id == pdev_id)
534                         return (pdev->ar ? pdev->ar : NULL);
535         }
536
537         return NULL;
538 }
539
540 struct ath11k *ath11k_mac_get_ar_vdev_stop_status(struct ath11k_base *ab,
541                                                   u32 vdev_id)
542 {
543         int i;
544         struct ath11k_pdev *pdev;
545         struct ath11k *ar;
546
547         for (i = 0; i < ab->num_radios; i++) {
548                 pdev = rcu_dereference(ab->pdevs_active[i]);
549                 if (pdev && pdev->ar) {
550                         ar = pdev->ar;
551
552                         spin_lock_bh(&ar->data_lock);
553                         if (ar->vdev_stop_status.stop_in_progress &&
554                             ar->vdev_stop_status.vdev_id == vdev_id) {
555                                 ar->vdev_stop_status.stop_in_progress = false;
556                                 spin_unlock_bh(&ar->data_lock);
557                                 return ar;
558                         }
559                         spin_unlock_bh(&ar->data_lock);
560                 }
561         }
562         return NULL;
563 }
564
565 static void ath11k_pdev_caps_update(struct ath11k *ar)
566 {
567         struct ath11k_base *ab = ar->ab;
568
569         ar->max_tx_power = ab->target_caps.hw_max_tx_power;
570
571         /* FIXME Set min_tx_power to ab->target_caps.hw_min_tx_power.
572          * But since the received value in svcrdy is same as hw_max_tx_power,
573          * we can set ar->min_tx_power to 0 currently until
574          * this is fixed in firmware
575          */
576         ar->min_tx_power = 0;
577
578         ar->txpower_limit_2g = ar->max_tx_power;
579         ar->txpower_limit_5g = ar->max_tx_power;
580         ar->txpower_scale = WMI_HOST_TP_SCALE_MAX;
581 }
582
583 static int ath11k_mac_txpower_recalc(struct ath11k *ar)
584 {
585         struct ath11k_pdev *pdev = ar->pdev;
586         struct ath11k_vif *arvif;
587         int ret, txpower = -1;
588         u32 param;
589
590         lockdep_assert_held(&ar->conf_mutex);
591
592         list_for_each_entry(arvif, &ar->arvifs, list) {
593                 if (arvif->txpower <= 0)
594                         continue;
595
596                 if (txpower == -1)
597                         txpower = arvif->txpower;
598                 else
599                         txpower = min(txpower, arvif->txpower);
600         }
601
602         if (txpower == -1)
603                 return 0;
604
605         /* txpwr is set as 2 units per dBm in FW*/
606         txpower = min_t(u32, max_t(u32, ar->min_tx_power, txpower),
607                         ar->max_tx_power) * 2;
608
609         ath11k_dbg(ar->ab, ATH11K_DBG_MAC, "txpower to set in hw %d\n",
610                    txpower / 2);
611
612         if ((pdev->cap.supported_bands & WMI_HOST_WLAN_2G_CAP) &&
613             ar->txpower_limit_2g != txpower) {
614                 param = WMI_PDEV_PARAM_TXPOWER_LIMIT2G;
615                 ret = ath11k_wmi_pdev_set_param(ar, param,
616                                                 txpower, ar->pdev->pdev_id);
617                 if (ret)
618                         goto fail;
619                 ar->txpower_limit_2g = txpower;
620         }
621
622         if ((pdev->cap.supported_bands & WMI_HOST_WLAN_5G_CAP) &&
623             ar->txpower_limit_5g != txpower) {
624                 param = WMI_PDEV_PARAM_TXPOWER_LIMIT5G;
625                 ret = ath11k_wmi_pdev_set_param(ar, param,
626                                                 txpower, ar->pdev->pdev_id);
627                 if (ret)
628                         goto fail;
629                 ar->txpower_limit_5g = txpower;
630         }
631
632         return 0;
633
634 fail:
635         ath11k_warn(ar->ab, "failed to recalc txpower limit %d using pdev param %d: %d\n",
636                     txpower / 2, param, ret);
637         return ret;
638 }
639
640 static int ath11k_recalc_rtscts_prot(struct ath11k_vif *arvif)
641 {
642         struct ath11k *ar = arvif->ar;
643         u32 vdev_param, rts_cts = 0;
644         int ret;
645
646         lockdep_assert_held(&ar->conf_mutex);
647
648         vdev_param = WMI_VDEV_PARAM_ENABLE_RTSCTS;
649
650         /* Enable RTS/CTS protection for sw retries (when legacy stations
651          * are in BSS) or by default only for second rate series.
652          * TODO: Check if we need to enable CTS 2 Self in any case
653          */
654         rts_cts = WMI_USE_RTS_CTS;
655
656         if (arvif->num_legacy_stations > 0)
657                 rts_cts |= WMI_RTSCTS_ACROSS_SW_RETRIES << 4;
658         else
659                 rts_cts |= WMI_RTSCTS_FOR_SECOND_RATESERIES << 4;
660
661         /* Need not send duplicate param value to firmware */
662         if (arvif->rtscts_prot_mode == rts_cts)
663                 return 0;
664
665         arvif->rtscts_prot_mode = rts_cts;
666
667         ath11k_dbg(ar->ab, ATH11K_DBG_MAC, "mac vdev %d recalc rts/cts prot %d\n",
668                    arvif->vdev_id, rts_cts);
669
670         ret =  ath11k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
671                                              vdev_param, rts_cts);
672         if (ret)
673                 ath11k_warn(ar->ab, "failed to recalculate rts/cts prot for vdev %d: %d\n",
674                             arvif->vdev_id, ret);
675
676         return ret;
677 }
678
679 static int ath11k_mac_set_kickout(struct ath11k_vif *arvif)
680 {
681         struct ath11k *ar = arvif->ar;
682         u32 param;
683         int ret;
684
685         ret = ath11k_wmi_pdev_set_param(ar, WMI_PDEV_PARAM_STA_KICKOUT_TH,
686                                         ATH11K_KICKOUT_THRESHOLD,
687                                         ar->pdev->pdev_id);
688         if (ret) {
689                 ath11k_warn(ar->ab, "failed to set kickout threshold on vdev %i: %d\n",
690                             arvif->vdev_id, ret);
691                 return ret;
692         }
693
694         param = WMI_VDEV_PARAM_AP_KEEPALIVE_MIN_IDLE_INACTIVE_TIME_SECS;
695         ret = ath11k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id, param,
696                                             ATH11K_KEEPALIVE_MIN_IDLE);
697         if (ret) {
698                 ath11k_warn(ar->ab, "failed to set keepalive minimum idle time on vdev %i: %d\n",
699                             arvif->vdev_id, ret);
700                 return ret;
701         }
702
703         param = WMI_VDEV_PARAM_AP_KEEPALIVE_MAX_IDLE_INACTIVE_TIME_SECS;
704         ret = ath11k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id, param,
705                                             ATH11K_KEEPALIVE_MAX_IDLE);
706         if (ret) {
707                 ath11k_warn(ar->ab, "failed to set keepalive maximum idle time on vdev %i: %d\n",
708                             arvif->vdev_id, ret);
709                 return ret;
710         }
711
712         param = WMI_VDEV_PARAM_AP_KEEPALIVE_MAX_UNRESPONSIVE_TIME_SECS;
713         ret = ath11k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id, param,
714                                             ATH11K_KEEPALIVE_MAX_UNRESPONSIVE);
715         if (ret) {
716                 ath11k_warn(ar->ab, "failed to set keepalive maximum unresponsive time on vdev %i: %d\n",
717                             arvif->vdev_id, ret);
718                 return ret;
719         }
720
721         return 0;
722 }
723
724 void ath11k_mac_peer_cleanup_all(struct ath11k *ar)
725 {
726         struct ath11k_peer *peer, *tmp;
727         struct ath11k_base *ab = ar->ab;
728
729         lockdep_assert_held(&ar->conf_mutex);
730
731         spin_lock_bh(&ab->base_lock);
732         list_for_each_entry_safe(peer, tmp, &ab->peers, list) {
733                 ath11k_peer_rx_tid_cleanup(ar, peer);
734                 list_del(&peer->list);
735                 kfree(peer);
736         }
737         spin_unlock_bh(&ab->base_lock);
738
739         ar->num_peers = 0;
740         ar->num_stations = 0;
741 }
742
743 static int ath11k_monitor_vdev_up(struct ath11k *ar, int vdev_id)
744 {
745         int ret = 0;
746
747         ret = ath11k_wmi_vdev_up(ar, vdev_id, 0, ar->mac_addr);
748         if (ret) {
749                 ath11k_warn(ar->ab, "failed to put up monitor vdev %i: %d\n",
750                             vdev_id, ret);
751                 return ret;
752         }
753
754         ath11k_dbg(ar->ab, ATH11K_DBG_MAC, "mac monitor vdev %i started\n",
755                    vdev_id);
756         return 0;
757 }
758
759 static int ath11k_mac_op_config(struct ieee80211_hw *hw, u32 changed)
760 {
761         struct ath11k *ar = hw->priv;
762         int ret = 0;
763
764         /* mac80211 requires this op to be present and that's why
765          * there's an empty function, this can be extended when
766          * required.
767          */
768
769         mutex_lock(&ar->conf_mutex);
770
771         /* TODO: Handle configuration changes as appropriate */
772
773         mutex_unlock(&ar->conf_mutex);
774
775         return ret;
776 }
777
778 static int ath11k_mac_setup_bcn_tmpl(struct ath11k_vif *arvif)
779 {
780         struct ath11k *ar = arvif->ar;
781         struct ath11k_base *ab = ar->ab;
782         struct ieee80211_hw *hw = ar->hw;
783         struct ieee80211_vif *vif = arvif->vif;
784         struct ieee80211_mutable_offsets offs = {};
785         struct sk_buff *bcn;
786         struct ieee80211_mgmt *mgmt;
787         u8 *ies;
788         int ret;
789
790         if (arvif->vdev_type != WMI_VDEV_TYPE_AP)
791                 return 0;
792
793         bcn = ieee80211_beacon_get_template(hw, vif, &offs);
794         if (!bcn) {
795                 ath11k_warn(ab, "failed to get beacon template from mac80211\n");
796                 return -EPERM;
797         }
798
799         ies = bcn->data + ieee80211_get_hdrlen_from_skb(bcn);
800         ies += sizeof(mgmt->u.beacon);
801
802         if (cfg80211_find_ie(WLAN_EID_RSN, ies, (skb_tail_pointer(bcn) - ies)))
803                 arvif->rsnie_present = true;
804
805         if (cfg80211_find_vendor_ie(WLAN_OUI_MICROSOFT,
806                                     WLAN_OUI_TYPE_MICROSOFT_WPA,
807                                     ies, (skb_tail_pointer(bcn) - ies)))
808                 arvif->wpaie_present = true;
809
810         ret = ath11k_wmi_bcn_tmpl(ar, arvif->vdev_id, &offs, bcn);
811
812         kfree_skb(bcn);
813
814         if (ret)
815                 ath11k_warn(ab, "failed to submit beacon template command: %d\n",
816                             ret);
817
818         return ret;
819 }
820
821 static void ath11k_control_beaconing(struct ath11k_vif *arvif,
822                                      struct ieee80211_bss_conf *info)
823 {
824         struct ath11k *ar = arvif->ar;
825         int ret = 0;
826
827         lockdep_assert_held(&arvif->ar->conf_mutex);
828
829         if (!info->enable_beacon) {
830                 ret = ath11k_wmi_vdev_down(ar, arvif->vdev_id);
831                 if (ret)
832                         ath11k_warn(ar->ab, "failed to down vdev_id %i: %d\n",
833                                     arvif->vdev_id, ret);
834
835                 arvif->is_up = false;
836                 return;
837         }
838
839         /* Install the beacon template to the FW */
840         ret = ath11k_mac_setup_bcn_tmpl(arvif);
841         if (ret) {
842                 ath11k_warn(ar->ab, "failed to update bcn tmpl during vdev up: %d\n",
843                             ret);
844                 return;
845         }
846
847         arvif->tx_seq_no = 0x1000;
848
849         arvif->aid = 0;
850
851         ether_addr_copy(arvif->bssid, info->bssid);
852
853         ret = ath11k_wmi_vdev_up(arvif->ar, arvif->vdev_id, arvif->aid,
854                                  arvif->bssid);
855         if (ret) {
856                 ath11k_warn(ar->ab, "failed to bring up vdev %d: %i\n",
857                             arvif->vdev_id, ret);
858                 return;
859         }
860
861         arvif->is_up = true;
862
863         ath11k_dbg(ar->ab, ATH11K_DBG_MAC, "mac vdev %d up\n", arvif->vdev_id);
864 }
865
866 static void ath11k_peer_assoc_h_basic(struct ath11k *ar,
867                                       struct ieee80211_vif *vif,
868                                       struct ieee80211_sta *sta,
869                                       struct peer_assoc_params *arg)
870 {
871         struct ath11k_vif *arvif = (void *)vif->drv_priv;
872         u32 aid;
873
874         lockdep_assert_held(&ar->conf_mutex);
875
876         if (vif->type == NL80211_IFTYPE_STATION)
877                 aid = vif->bss_conf.aid;
878         else
879                 aid = sta->aid;
880
881         ether_addr_copy(arg->peer_mac, sta->addr);
882         arg->vdev_id = arvif->vdev_id;
883         arg->peer_associd = aid;
884         arg->auth_flag = true;
885         /* TODO: STA WAR in ath10k for listen interval required? */
886         arg->peer_listen_intval = ar->hw->conf.listen_interval;
887         arg->peer_nss = 1;
888         arg->peer_caps = vif->bss_conf.assoc_capability;
889 }
890
891 static void ath11k_peer_assoc_h_crypto(struct ath11k *ar,
892                                        struct ieee80211_vif *vif,
893                                        struct ieee80211_sta *sta,
894                                        struct peer_assoc_params *arg)
895 {
896         struct ieee80211_bss_conf *info = &vif->bss_conf;
897         struct cfg80211_chan_def def;
898         struct cfg80211_bss *bss;
899         struct ath11k_vif *arvif = (struct ath11k_vif *)vif->drv_priv;
900         const u8 *rsnie = NULL;
901         const u8 *wpaie = NULL;
902
903         lockdep_assert_held(&ar->conf_mutex);
904
905         if (WARN_ON(ath11k_mac_vif_chan(vif, &def)))
906                 return;
907
908         bss = cfg80211_get_bss(ar->hw->wiphy, def.chan, info->bssid, NULL, 0,
909                                IEEE80211_BSS_TYPE_ANY, IEEE80211_PRIVACY_ANY);
910
911         if (arvif->rsnie_present || arvif->wpaie_present) {
912                 arg->need_ptk_4_way = true;
913                 if (arvif->wpaie_present)
914                         arg->need_gtk_2_way = true;
915         } else if (bss) {
916                 const struct cfg80211_bss_ies *ies;
917
918                 rcu_read_lock();
919                 rsnie = ieee80211_bss_get_ie(bss, WLAN_EID_RSN);
920
921                 ies = rcu_dereference(bss->ies);
922
923                 wpaie = cfg80211_find_vendor_ie(WLAN_OUI_MICROSOFT,
924                                                 WLAN_OUI_TYPE_MICROSOFT_WPA,
925                                                 ies->data,
926                                                 ies->len);
927                 rcu_read_unlock();
928                 cfg80211_put_bss(ar->hw->wiphy, bss);
929         }
930
931         /* FIXME: base on RSN IE/WPA IE is a correct idea? */
932         if (rsnie || wpaie) {
933                 ath11k_dbg(ar->ab, ATH11K_DBG_WMI,
934                            "%s: rsn ie found\n", __func__);
935                 arg->need_ptk_4_way = true;
936         }
937
938         if (wpaie) {
939                 ath11k_dbg(ar->ab, ATH11K_DBG_WMI,
940                            "%s: wpa ie found\n", __func__);
941                 arg->need_gtk_2_way = true;
942         }
943
944         if (sta->mfp) {
945                 /* TODO: Need to check if FW supports PMF? */
946                 arg->is_pmf_enabled = true;
947         }
948
949         /* TODO: safe_mode_enabled (bypass 4-way handshake) flag req? */
950 }
951
952 static void ath11k_peer_assoc_h_rates(struct ath11k *ar,
953                                       struct ieee80211_vif *vif,
954                                       struct ieee80211_sta *sta,
955                                       struct peer_assoc_params *arg)
956 {
957         struct ath11k_vif *arvif = (void *)vif->drv_priv;
958         struct wmi_rate_set_arg *rateset = &arg->peer_legacy_rates;
959         struct cfg80211_chan_def def;
960         const struct ieee80211_supported_band *sband;
961         const struct ieee80211_rate *rates;
962         enum nl80211_band band;
963         u32 ratemask;
964         u8 rate;
965         int i;
966
967         lockdep_assert_held(&ar->conf_mutex);
968
969         if (WARN_ON(ath11k_mac_vif_chan(vif, &def)))
970                 return;
971
972         band = def.chan->band;
973         sband = ar->hw->wiphy->bands[band];
974         ratemask = sta->supp_rates[band];
975         ratemask &= arvif->bitrate_mask.control[band].legacy;
976         rates = sband->bitrates;
977
978         rateset->num_rates = 0;
979
980         for (i = 0; i < 32; i++, ratemask >>= 1, rates++) {
981                 if (!(ratemask & 1))
982                         continue;
983
984                 rate = ath11k_mac_bitrate_to_rate(rates->bitrate);
985                 rateset->rates[rateset->num_rates] = rate;
986                 rateset->num_rates++;
987         }
988 }
989
990 static bool
991 ath11k_peer_assoc_h_ht_masked(const u8 ht_mcs_mask[IEEE80211_HT_MCS_MASK_LEN])
992 {
993         int nss;
994
995         for (nss = 0; nss < IEEE80211_HT_MCS_MASK_LEN; nss++)
996                 if (ht_mcs_mask[nss])
997                         return false;
998
999         return true;
1000 }
1001
1002 static bool
1003 ath11k_peer_assoc_h_vht_masked(const u16 vht_mcs_mask[NL80211_VHT_NSS_MAX])
1004 {
1005         int nss;
1006
1007         for (nss = 0; nss < NL80211_VHT_NSS_MAX; nss++)
1008                 if (vht_mcs_mask[nss])
1009                         return false;
1010
1011         return true;
1012 }
1013
1014 static void ath11k_peer_assoc_h_ht(struct ath11k *ar,
1015                                    struct ieee80211_vif *vif,
1016                                    struct ieee80211_sta *sta,
1017                                    struct peer_assoc_params *arg)
1018 {
1019         const struct ieee80211_sta_ht_cap *ht_cap = &sta->ht_cap;
1020         struct ath11k_vif *arvif = (void *)vif->drv_priv;
1021         struct cfg80211_chan_def def;
1022         enum nl80211_band band;
1023         const u8 *ht_mcs_mask;
1024         int i, n;
1025         u8 max_nss;
1026         u32 stbc;
1027
1028         lockdep_assert_held(&ar->conf_mutex);
1029
1030         if (WARN_ON(ath11k_mac_vif_chan(vif, &def)))
1031                 return;
1032
1033         if (!ht_cap->ht_supported)
1034                 return;
1035
1036         band = def.chan->band;
1037         ht_mcs_mask = arvif->bitrate_mask.control[band].ht_mcs;
1038
1039         if (ath11k_peer_assoc_h_ht_masked(ht_mcs_mask))
1040                 return;
1041
1042         arg->ht_flag = true;
1043
1044         arg->peer_max_mpdu = (1 << (IEEE80211_HT_MAX_AMPDU_FACTOR +
1045                                     ht_cap->ampdu_factor)) - 1;
1046
1047         arg->peer_mpdu_density =
1048                 ath11k_parse_mpdudensity(ht_cap->ampdu_density);
1049
1050         arg->peer_ht_caps = ht_cap->cap;
1051         arg->peer_rate_caps |= WMI_HOST_RC_HT_FLAG;
1052
1053         if (ht_cap->cap & IEEE80211_HT_CAP_LDPC_CODING)
1054                 arg->ldpc_flag = true;
1055
1056         if (sta->bandwidth >= IEEE80211_STA_RX_BW_40) {
1057                 arg->bw_40 = true;
1058                 arg->peer_rate_caps |= WMI_HOST_RC_CW40_FLAG;
1059         }
1060
1061         if (arvif->bitrate_mask.control[band].gi != NL80211_TXRATE_FORCE_LGI) {
1062                 if (ht_cap->cap & (IEEE80211_HT_CAP_SGI_20 |
1063                     IEEE80211_HT_CAP_SGI_40))
1064                         arg->peer_rate_caps |= WMI_HOST_RC_SGI_FLAG;
1065         }
1066
1067         if (ht_cap->cap & IEEE80211_HT_CAP_TX_STBC) {
1068                 arg->peer_rate_caps |= WMI_HOST_RC_TX_STBC_FLAG;
1069                 arg->stbc_flag = true;
1070         }
1071
1072         if (ht_cap->cap & IEEE80211_HT_CAP_RX_STBC) {
1073                 stbc = ht_cap->cap & IEEE80211_HT_CAP_RX_STBC;
1074                 stbc = stbc >> IEEE80211_HT_CAP_RX_STBC_SHIFT;
1075                 stbc = stbc << WMI_HOST_RC_RX_STBC_FLAG_S;
1076                 arg->peer_rate_caps |= stbc;
1077                 arg->stbc_flag = true;
1078         }
1079
1080         if (ht_cap->mcs.rx_mask[1] && ht_cap->mcs.rx_mask[2])
1081                 arg->peer_rate_caps |= WMI_HOST_RC_TS_FLAG;
1082         else if (ht_cap->mcs.rx_mask[1])
1083                 arg->peer_rate_caps |= WMI_HOST_RC_DS_FLAG;
1084
1085         for (i = 0, n = 0, max_nss = 0; i < IEEE80211_HT_MCS_MASK_LEN * 8; i++)
1086                 if ((ht_cap->mcs.rx_mask[i / 8] & BIT(i % 8)) &&
1087                     (ht_mcs_mask[i / 8] & BIT(i % 8))) {
1088                         max_nss = (i / 8) + 1;
1089                         arg->peer_ht_rates.rates[n++] = i;
1090                 }
1091
1092         /* This is a workaround for HT-enabled STAs which break the spec
1093          * and have no HT capabilities RX mask (no HT RX MCS map).
1094          *
1095          * As per spec, in section 20.3.5 Modulation and coding scheme (MCS),
1096          * MCS 0 through 7 are mandatory in 20MHz with 800 ns GI at all STAs.
1097          *
1098          * Firmware asserts if such situation occurs.
1099          */
1100         if (n == 0) {
1101                 arg->peer_ht_rates.num_rates = 8;
1102                 for (i = 0; i < arg->peer_ht_rates.num_rates; i++)
1103                         arg->peer_ht_rates.rates[i] = i;
1104         } else {
1105                 arg->peer_ht_rates.num_rates = n;
1106                 arg->peer_nss = min(sta->rx_nss, max_nss);
1107         }
1108
1109         ath11k_dbg(ar->ab, ATH11K_DBG_MAC, "mac ht peer %pM mcs cnt %d nss %d\n",
1110                    arg->peer_mac,
1111                    arg->peer_ht_rates.num_rates,
1112                    arg->peer_nss);
1113 }
1114
1115 static int ath11k_mac_get_max_vht_mcs_map(u16 mcs_map, int nss)
1116 {
1117         switch ((mcs_map >> (2 * nss)) & 0x3) {
1118         case IEEE80211_VHT_MCS_SUPPORT_0_7: return BIT(8) - 1;
1119         case IEEE80211_VHT_MCS_SUPPORT_0_8: return BIT(9) - 1;
1120         case IEEE80211_VHT_MCS_SUPPORT_0_9: return BIT(10) - 1;
1121         }
1122         return 0;
1123 }
1124
1125 static u16
1126 ath11k_peer_assoc_h_vht_limit(u16 tx_mcs_set,
1127                               const u16 vht_mcs_limit[NL80211_VHT_NSS_MAX])
1128 {
1129         int idx_limit;
1130         int nss;
1131         u16 mcs_map;
1132         u16 mcs;
1133
1134         for (nss = 0; nss < NL80211_VHT_NSS_MAX; nss++) {
1135                 mcs_map = ath11k_mac_get_max_vht_mcs_map(tx_mcs_set, nss) &
1136                           vht_mcs_limit[nss];
1137
1138                 if (mcs_map)
1139                         idx_limit = fls(mcs_map) - 1;
1140                 else
1141                         idx_limit = -1;
1142
1143                 switch (idx_limit) {
1144                 case 0:
1145                 case 1:
1146                 case 2:
1147                 case 3:
1148                 case 4:
1149                 case 5:
1150                 case 6:
1151                 case 7:
1152                         mcs = IEEE80211_VHT_MCS_SUPPORT_0_7;
1153                         break;
1154                 case 8:
1155                         mcs = IEEE80211_VHT_MCS_SUPPORT_0_8;
1156                         break;
1157                 case 9:
1158                         mcs = IEEE80211_VHT_MCS_SUPPORT_0_9;
1159                         break;
1160                 default:
1161                         WARN_ON(1);
1162                         fallthrough;
1163                 case -1:
1164                         mcs = IEEE80211_VHT_MCS_NOT_SUPPORTED;
1165                         break;
1166                 }
1167
1168                 tx_mcs_set &= ~(0x3 << (nss * 2));
1169                 tx_mcs_set |= mcs << (nss * 2);
1170         }
1171
1172         return tx_mcs_set;
1173 }
1174
1175 static void ath11k_peer_assoc_h_vht(struct ath11k *ar,
1176                                     struct ieee80211_vif *vif,
1177                                     struct ieee80211_sta *sta,
1178                                     struct peer_assoc_params *arg)
1179 {
1180         const struct ieee80211_sta_vht_cap *vht_cap = &sta->vht_cap;
1181         struct ath11k_vif *arvif = (void *)vif->drv_priv;
1182         struct cfg80211_chan_def def;
1183         enum nl80211_band band;
1184         const u16 *vht_mcs_mask;
1185         u8 ampdu_factor;
1186         u8 max_nss, vht_mcs;
1187         int i;
1188
1189         if (WARN_ON(ath11k_mac_vif_chan(vif, &def)))
1190                 return;
1191
1192         if (!vht_cap->vht_supported)
1193                 return;
1194
1195         band = def.chan->band;
1196         vht_mcs_mask = arvif->bitrate_mask.control[band].vht_mcs;
1197
1198         if (ath11k_peer_assoc_h_vht_masked(vht_mcs_mask))
1199                 return;
1200
1201         arg->vht_flag = true;
1202
1203         /* TODO: similar flags required? */
1204         arg->vht_capable = true;
1205
1206         if (def.chan->band == NL80211_BAND_2GHZ)
1207                 arg->vht_ng_flag = true;
1208
1209         arg->peer_vht_caps = vht_cap->cap;
1210
1211         ampdu_factor = (vht_cap->cap &
1212                         IEEE80211_VHT_CAP_MAX_A_MPDU_LENGTH_EXPONENT_MASK) >>
1213                        IEEE80211_VHT_CAP_MAX_A_MPDU_LENGTH_EXPONENT_SHIFT;
1214
1215         /* Workaround: Some Netgear/Linksys 11ac APs set Rx A-MPDU factor to
1216          * zero in VHT IE. Using it would result in degraded throughput.
1217          * arg->peer_max_mpdu at this point contains HT max_mpdu so keep
1218          * it if VHT max_mpdu is smaller.
1219          */
1220         arg->peer_max_mpdu = max(arg->peer_max_mpdu,
1221                                  (1U << (IEEE80211_HT_MAX_AMPDU_FACTOR +
1222                                         ampdu_factor)) - 1);
1223
1224         if (sta->bandwidth == IEEE80211_STA_RX_BW_80)
1225                 arg->bw_80 = true;
1226
1227         if (sta->bandwidth == IEEE80211_STA_RX_BW_160)
1228                 arg->bw_160 = true;
1229
1230         /* Calculate peer NSS capability from VHT capabilities if STA
1231          * supports VHT.
1232          */
1233         for (i = 0, max_nss = 0, vht_mcs = 0; i < NL80211_VHT_NSS_MAX; i++) {
1234                 vht_mcs = __le16_to_cpu(vht_cap->vht_mcs.rx_mcs_map) >>
1235                           (2 * i) & 3;
1236
1237                 if (vht_mcs != IEEE80211_VHT_MCS_NOT_SUPPORTED &&
1238                     vht_mcs_mask[i])
1239                         max_nss = i + 1;
1240         }
1241         arg->peer_nss = min(sta->rx_nss, max_nss);
1242         arg->rx_max_rate = __le16_to_cpu(vht_cap->vht_mcs.rx_highest);
1243         arg->rx_mcs_set = __le16_to_cpu(vht_cap->vht_mcs.rx_mcs_map);
1244         arg->tx_max_rate = __le16_to_cpu(vht_cap->vht_mcs.tx_highest);
1245         arg->tx_mcs_set = ath11k_peer_assoc_h_vht_limit(
1246                 __le16_to_cpu(vht_cap->vht_mcs.tx_mcs_map), vht_mcs_mask);
1247
1248         /* In IPQ8074 platform, VHT mcs rate 10 and 11 is enabled by default.
1249          * VHT mcs rate 10 and 11 is not suppoerted in 11ac standard.
1250          * so explicitly disable the VHT MCS rate 10 and 11 in 11ac mode.
1251          */
1252         arg->tx_mcs_set &= ~IEEE80211_VHT_MCS_SUPPORT_0_11_MASK;
1253         arg->tx_mcs_set |= IEEE80211_DISABLE_VHT_MCS_SUPPORT_0_11;
1254
1255         if ((arg->tx_mcs_set & IEEE80211_VHT_MCS_NOT_SUPPORTED) ==
1256                         IEEE80211_VHT_MCS_NOT_SUPPORTED)
1257                 arg->peer_vht_caps &= ~IEEE80211_VHT_CAP_MU_BEAMFORMEE_CAPABLE;
1258
1259         /* TODO:  Check */
1260         arg->tx_max_mcs_nss = 0xFF;
1261
1262         ath11k_dbg(ar->ab, ATH11K_DBG_MAC, "mac vht peer %pM max_mpdu %d flags 0x%x\n",
1263                    sta->addr, arg->peer_max_mpdu, arg->peer_flags);
1264
1265         /* TODO: rxnss_override */
1266 }
1267
1268 static void ath11k_peer_assoc_h_he(struct ath11k *ar,
1269                                    struct ieee80211_vif *vif,
1270                                    struct ieee80211_sta *sta,
1271                                    struct peer_assoc_params *arg)
1272 {
1273         const struct ieee80211_sta_he_cap *he_cap = &sta->he_cap;
1274         u8 ampdu_factor;
1275         u16 v;
1276
1277         if (!he_cap->has_he)
1278                 return;
1279
1280         arg->he_flag = true;
1281
1282         memcpy(&arg->peer_he_cap_macinfo, he_cap->he_cap_elem.mac_cap_info,
1283                sizeof(arg->peer_he_cap_macinfo));
1284         memcpy(&arg->peer_he_cap_phyinfo, he_cap->he_cap_elem.phy_cap_info,
1285                sizeof(arg->peer_he_cap_phyinfo));
1286         arg->peer_he_ops = vif->bss_conf.he_oper.params;
1287
1288         /* the top most byte is used to indicate BSS color info */
1289         arg->peer_he_ops &= 0xffffff;
1290
1291         /* As per section 26.6.1 11ax Draft5.0, if the Max AMPDU Exponent Extension
1292          * in HE cap is zero, use the arg->peer_max_mpdu as calculated while parsing
1293          * VHT caps(if VHT caps is present) or HT caps (if VHT caps is not present).
1294          *
1295          * For non-zero value of Max AMPDU Extponent Extension in HE MAC caps,
1296          * if a HE STA sends VHT cap and HE cap IE in assoc request then, use
1297          * MAX_AMPDU_LEN_FACTOR as 20 to calculate max_ampdu length.
1298          * If a HE STA that does not send VHT cap, but HE and HT cap in assoc
1299          * request, then use MAX_AMPDU_LEN_FACTOR as 16 to calculate max_ampdu
1300          * length.
1301          */
1302         ampdu_factor = (he_cap->he_cap_elem.mac_cap_info[3] &
1303                         IEEE80211_HE_MAC_CAP3_MAX_AMPDU_LEN_EXP_MASK) >>
1304                         IEEE80211_HE_MAC_CAP3_MAX_AMPDU_LEN_EXP_SHIFT;
1305
1306         if (ampdu_factor) {
1307                 if (sta->vht_cap.vht_supported)
1308                         arg->peer_max_mpdu = (1 << (IEEE80211_HE_VHT_MAX_AMPDU_FACTOR +
1309                                                     ampdu_factor)) - 1;
1310                 else if (sta->ht_cap.ht_supported)
1311                         arg->peer_max_mpdu = (1 << (IEEE80211_HE_HT_MAX_AMPDU_FACTOR +
1312                                                     ampdu_factor)) - 1;
1313         }
1314
1315         if (he_cap->he_cap_elem.phy_cap_info[6] &
1316             IEEE80211_HE_PHY_CAP6_PPE_THRESHOLD_PRESENT) {
1317                 int bit = 7;
1318                 int nss, ru;
1319
1320                 arg->peer_ppet.numss_m1 = he_cap->ppe_thres[0] &
1321                                           IEEE80211_PPE_THRES_NSS_MASK;
1322                 arg->peer_ppet.ru_bit_mask =
1323                         (he_cap->ppe_thres[0] &
1324                          IEEE80211_PPE_THRES_RU_INDEX_BITMASK_MASK) >>
1325                         IEEE80211_PPE_THRES_RU_INDEX_BITMASK_POS;
1326
1327                 for (nss = 0; nss <= arg->peer_ppet.numss_m1; nss++) {
1328                         for (ru = 0; ru < 4; ru++) {
1329                                 u32 val = 0;
1330                                 int i;
1331
1332                                 if ((arg->peer_ppet.ru_bit_mask & BIT(ru)) == 0)
1333                                         continue;
1334                                 for (i = 0; i < 6; i++) {
1335                                         val >>= 1;
1336                                         val |= ((he_cap->ppe_thres[bit / 8] >>
1337                                                  (bit % 8)) & 0x1) << 5;
1338                                         bit++;
1339                                 }
1340                                 arg->peer_ppet.ppet16_ppet8_ru3_ru0[nss] |=
1341                                                                 val << (ru * 6);
1342                         }
1343                 }
1344         }
1345
1346         if (he_cap->he_cap_elem.mac_cap_info[0] & IEEE80211_HE_MAC_CAP0_TWT_RES)
1347                 arg->twt_responder = true;
1348         if (he_cap->he_cap_elem.mac_cap_info[0] & IEEE80211_HE_MAC_CAP0_TWT_REQ)
1349                 arg->twt_requester = true;
1350
1351         switch (sta->bandwidth) {
1352         case IEEE80211_STA_RX_BW_160:
1353                 if (he_cap->he_cap_elem.phy_cap_info[0] &
1354                     IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_80PLUS80_MHZ_IN_5G) {
1355                         v = le16_to_cpu(he_cap->he_mcs_nss_supp.rx_mcs_80p80);
1356                         arg->peer_he_rx_mcs_set[WMI_HECAP_TXRX_MCS_NSS_IDX_80_80] = v;
1357
1358                         v = le16_to_cpu(he_cap->he_mcs_nss_supp.tx_mcs_80p80);
1359                         arg->peer_he_tx_mcs_set[WMI_HECAP_TXRX_MCS_NSS_IDX_80_80] = v;
1360
1361                         arg->peer_he_mcs_count++;
1362                 }
1363                 v = le16_to_cpu(he_cap->he_mcs_nss_supp.rx_mcs_160);
1364                 arg->peer_he_rx_mcs_set[WMI_HECAP_TXRX_MCS_NSS_IDX_160] = v;
1365
1366                 v = le16_to_cpu(he_cap->he_mcs_nss_supp.tx_mcs_160);
1367                 arg->peer_he_tx_mcs_set[WMI_HECAP_TXRX_MCS_NSS_IDX_160] = v;
1368
1369                 arg->peer_he_mcs_count++;
1370                 fallthrough;
1371
1372         default:
1373                 v = le16_to_cpu(he_cap->he_mcs_nss_supp.rx_mcs_80);
1374                 arg->peer_he_rx_mcs_set[WMI_HECAP_TXRX_MCS_NSS_IDX_80] = v;
1375
1376                 v = le16_to_cpu(he_cap->he_mcs_nss_supp.tx_mcs_80);
1377                 arg->peer_he_tx_mcs_set[WMI_HECAP_TXRX_MCS_NSS_IDX_80] = v;
1378
1379                 arg->peer_he_mcs_count++;
1380                 break;
1381         }
1382 }
1383
1384 static void ath11k_peer_assoc_h_smps(struct ieee80211_sta *sta,
1385                                      struct peer_assoc_params *arg)
1386 {
1387         const struct ieee80211_sta_ht_cap *ht_cap = &sta->ht_cap;
1388         int smps;
1389
1390         if (!ht_cap->ht_supported)
1391                 return;
1392
1393         smps = ht_cap->cap & IEEE80211_HT_CAP_SM_PS;
1394         smps >>= IEEE80211_HT_CAP_SM_PS_SHIFT;
1395
1396         switch (smps) {
1397         case WLAN_HT_CAP_SM_PS_STATIC:
1398                 arg->static_mimops_flag = true;
1399                 break;
1400         case WLAN_HT_CAP_SM_PS_DYNAMIC:
1401                 arg->dynamic_mimops_flag = true;
1402                 break;
1403         case WLAN_HT_CAP_SM_PS_DISABLED:
1404                 arg->spatial_mux_flag = true;
1405                 break;
1406         default:
1407                 break;
1408         }
1409 }
1410
1411 static void ath11k_peer_assoc_h_qos(struct ath11k *ar,
1412                                     struct ieee80211_vif *vif,
1413                                     struct ieee80211_sta *sta,
1414                                     struct peer_assoc_params *arg)
1415 {
1416         struct ath11k_vif *arvif = (void *)vif->drv_priv;
1417
1418         switch (arvif->vdev_type) {
1419         case WMI_VDEV_TYPE_AP:
1420                 if (sta->wme) {
1421                         /* TODO: Check WME vs QoS */
1422                         arg->is_wme_set = true;
1423                         arg->qos_flag = true;
1424                 }
1425
1426                 if (sta->wme && sta->uapsd_queues) {
1427                         /* TODO: Check WME vs QoS */
1428                         arg->is_wme_set = true;
1429                         arg->apsd_flag = true;
1430                         arg->peer_rate_caps |= WMI_HOST_RC_UAPSD_FLAG;
1431                 }
1432                 break;
1433         case WMI_VDEV_TYPE_STA:
1434                 if (sta->wme) {
1435                         arg->is_wme_set = true;
1436                         arg->qos_flag = true;
1437                 }
1438                 break;
1439         default:
1440                 break;
1441         }
1442
1443         ath11k_dbg(ar->ab, ATH11K_DBG_MAC, "mac peer %pM qos %d\n",
1444                    sta->addr, arg->qos_flag);
1445 }
1446
1447 static int ath11k_peer_assoc_qos_ap(struct ath11k *ar,
1448                                     struct ath11k_vif *arvif,
1449                                     struct ieee80211_sta *sta)
1450 {
1451         struct ap_ps_params params;
1452         u32 max_sp;
1453         u32 uapsd;
1454         int ret;
1455
1456         lockdep_assert_held(&ar->conf_mutex);
1457
1458         params.vdev_id = arvif->vdev_id;
1459
1460         ath11k_dbg(ar->ab, ATH11K_DBG_MAC, "mac uapsd_queues 0x%x max_sp %d\n",
1461                    sta->uapsd_queues, sta->max_sp);
1462
1463         uapsd = 0;
1464         if (sta->uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_VO)
1465                 uapsd |= WMI_AP_PS_UAPSD_AC3_DELIVERY_EN |
1466                          WMI_AP_PS_UAPSD_AC3_TRIGGER_EN;
1467         if (sta->uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_VI)
1468                 uapsd |= WMI_AP_PS_UAPSD_AC2_DELIVERY_EN |
1469                          WMI_AP_PS_UAPSD_AC2_TRIGGER_EN;
1470         if (sta->uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_BK)
1471                 uapsd |= WMI_AP_PS_UAPSD_AC1_DELIVERY_EN |
1472                          WMI_AP_PS_UAPSD_AC1_TRIGGER_EN;
1473         if (sta->uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_BE)
1474                 uapsd |= WMI_AP_PS_UAPSD_AC0_DELIVERY_EN |
1475                          WMI_AP_PS_UAPSD_AC0_TRIGGER_EN;
1476
1477         max_sp = 0;
1478         if (sta->max_sp < MAX_WMI_AP_PS_PEER_PARAM_MAX_SP)
1479                 max_sp = sta->max_sp;
1480
1481         params.param = WMI_AP_PS_PEER_PARAM_UAPSD;
1482         params.value = uapsd;
1483         ret = ath11k_wmi_send_set_ap_ps_param_cmd(ar, sta->addr, &params);
1484         if (ret)
1485                 goto err;
1486
1487         params.param = WMI_AP_PS_PEER_PARAM_MAX_SP;
1488         params.value = max_sp;
1489         ret = ath11k_wmi_send_set_ap_ps_param_cmd(ar, sta->addr, &params);
1490         if (ret)
1491                 goto err;
1492
1493         /* TODO revisit during testing */
1494         params.param = WMI_AP_PS_PEER_PARAM_SIFS_RESP_FRMTYPE;
1495         params.value = DISABLE_SIFS_RESPONSE_TRIGGER;
1496         ret = ath11k_wmi_send_set_ap_ps_param_cmd(ar, sta->addr, &params);
1497         if (ret)
1498                 goto err;
1499
1500         params.param = WMI_AP_PS_PEER_PARAM_SIFS_RESP_UAPSD;
1501         params.value = DISABLE_SIFS_RESPONSE_TRIGGER;
1502         ret = ath11k_wmi_send_set_ap_ps_param_cmd(ar, sta->addr, &params);
1503         if (ret)
1504                 goto err;
1505
1506         return 0;
1507
1508 err:
1509         ath11k_warn(ar->ab, "failed to set ap ps peer param %d for vdev %i: %d\n",
1510                     params.param, arvif->vdev_id, ret);
1511         return ret;
1512 }
1513
1514 static bool ath11k_mac_sta_has_ofdm_only(struct ieee80211_sta *sta)
1515 {
1516         return sta->supp_rates[NL80211_BAND_2GHZ] >>
1517                ATH11K_MAC_FIRST_OFDM_RATE_IDX;
1518 }
1519
1520 static enum wmi_phy_mode ath11k_mac_get_phymode_vht(struct ath11k *ar,
1521                                                     struct ieee80211_sta *sta)
1522 {
1523         if (sta->bandwidth == IEEE80211_STA_RX_BW_160) {
1524                 switch (sta->vht_cap.cap &
1525                         IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_MASK) {
1526                 case IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_160MHZ:
1527                         return MODE_11AC_VHT160;
1528                 case IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_160_80PLUS80MHZ:
1529                         return MODE_11AC_VHT80_80;
1530                 default:
1531                         /* not sure if this is a valid case? */
1532                         return MODE_11AC_VHT160;
1533                 }
1534         }
1535
1536         if (sta->bandwidth == IEEE80211_STA_RX_BW_80)
1537                 return MODE_11AC_VHT80;
1538
1539         if (sta->bandwidth == IEEE80211_STA_RX_BW_40)
1540                 return MODE_11AC_VHT40;
1541
1542         if (sta->bandwidth == IEEE80211_STA_RX_BW_20)
1543                 return MODE_11AC_VHT20;
1544
1545         return MODE_UNKNOWN;
1546 }
1547
1548 static enum wmi_phy_mode ath11k_mac_get_phymode_he(struct ath11k *ar,
1549                                                    struct ieee80211_sta *sta)
1550 {
1551         if (sta->bandwidth == IEEE80211_STA_RX_BW_160) {
1552                 if (sta->he_cap.he_cap_elem.phy_cap_info[0] &
1553                      IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_160MHZ_IN_5G)
1554                         return MODE_11AX_HE160;
1555                 else if (sta->he_cap.he_cap_elem.phy_cap_info[0] &
1556                      IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_80PLUS80_MHZ_IN_5G)
1557                         return MODE_11AX_HE80_80;
1558                 /* not sure if this is a valid case? */
1559                 return MODE_11AX_HE160;
1560         }
1561
1562         if (sta->bandwidth == IEEE80211_STA_RX_BW_80)
1563                 return MODE_11AX_HE80;
1564
1565         if (sta->bandwidth == IEEE80211_STA_RX_BW_40)
1566                 return MODE_11AX_HE40;
1567
1568         if (sta->bandwidth == IEEE80211_STA_RX_BW_20)
1569                 return MODE_11AX_HE20;
1570
1571         return MODE_UNKNOWN;
1572 }
1573
1574 static void ath11k_peer_assoc_h_phymode(struct ath11k *ar,
1575                                         struct ieee80211_vif *vif,
1576                                         struct ieee80211_sta *sta,
1577                                         struct peer_assoc_params *arg)
1578 {
1579         struct ath11k_vif *arvif = (void *)vif->drv_priv;
1580         struct cfg80211_chan_def def;
1581         enum nl80211_band band;
1582         const u8 *ht_mcs_mask;
1583         const u16 *vht_mcs_mask;
1584         enum wmi_phy_mode phymode = MODE_UNKNOWN;
1585
1586         if (WARN_ON(ath11k_mac_vif_chan(vif, &def)))
1587                 return;
1588
1589         band = def.chan->band;
1590         ht_mcs_mask = arvif->bitrate_mask.control[band].ht_mcs;
1591         vht_mcs_mask = arvif->bitrate_mask.control[band].vht_mcs;
1592
1593         switch (band) {
1594         case NL80211_BAND_2GHZ:
1595                 if (sta->he_cap.has_he) {
1596                         if (sta->bandwidth == IEEE80211_STA_RX_BW_80)
1597                                 phymode = MODE_11AX_HE80_2G;
1598                         else if (sta->bandwidth == IEEE80211_STA_RX_BW_40)
1599                                 phymode = MODE_11AX_HE40_2G;
1600                         else
1601                                 phymode = MODE_11AX_HE20_2G;
1602                 } else if (sta->vht_cap.vht_supported &&
1603                     !ath11k_peer_assoc_h_vht_masked(vht_mcs_mask)) {
1604                         if (sta->bandwidth == IEEE80211_STA_RX_BW_40)
1605                                 phymode = MODE_11AC_VHT40;
1606                         else
1607                                 phymode = MODE_11AC_VHT20;
1608                 } else if (sta->ht_cap.ht_supported &&
1609                            !ath11k_peer_assoc_h_ht_masked(ht_mcs_mask)) {
1610                         if (sta->bandwidth == IEEE80211_STA_RX_BW_40)
1611                                 phymode = MODE_11NG_HT40;
1612                         else
1613                                 phymode = MODE_11NG_HT20;
1614                 } else if (ath11k_mac_sta_has_ofdm_only(sta)) {
1615                         phymode = MODE_11G;
1616                 } else {
1617                         phymode = MODE_11B;
1618                 }
1619                 break;
1620         case NL80211_BAND_5GHZ:
1621         case NL80211_BAND_6GHZ:
1622                 /* Check HE first */
1623                 if (sta->he_cap.has_he) {
1624                         phymode = ath11k_mac_get_phymode_he(ar, sta);
1625                 } else if (sta->vht_cap.vht_supported &&
1626                     !ath11k_peer_assoc_h_vht_masked(vht_mcs_mask)) {
1627                         phymode = ath11k_mac_get_phymode_vht(ar, sta);
1628                 } else if (sta->ht_cap.ht_supported &&
1629                            !ath11k_peer_assoc_h_ht_masked(ht_mcs_mask)) {
1630                         if (sta->bandwidth >= IEEE80211_STA_RX_BW_40)
1631                                 phymode = MODE_11NA_HT40;
1632                         else
1633                                 phymode = MODE_11NA_HT20;
1634                 } else {
1635                         phymode = MODE_11A;
1636                 }
1637                 break;
1638         default:
1639                 break;
1640         }
1641
1642         ath11k_dbg(ar->ab, ATH11K_DBG_MAC, "mac peer %pM phymode %s\n",
1643                    sta->addr, ath11k_wmi_phymode_str(phymode));
1644
1645         arg->peer_phymode = phymode;
1646         WARN_ON(phymode == MODE_UNKNOWN);
1647 }
1648
1649 static void ath11k_peer_assoc_prepare(struct ath11k *ar,
1650                                       struct ieee80211_vif *vif,
1651                                       struct ieee80211_sta *sta,
1652                                       struct peer_assoc_params *arg,
1653                                       bool reassoc)
1654 {
1655         lockdep_assert_held(&ar->conf_mutex);
1656
1657         memset(arg, 0, sizeof(*arg));
1658
1659         reinit_completion(&ar->peer_assoc_done);
1660
1661         arg->peer_new_assoc = !reassoc;
1662         ath11k_peer_assoc_h_basic(ar, vif, sta, arg);
1663         ath11k_peer_assoc_h_crypto(ar, vif, sta, arg);
1664         ath11k_peer_assoc_h_rates(ar, vif, sta, arg);
1665         ath11k_peer_assoc_h_ht(ar, vif, sta, arg);
1666         ath11k_peer_assoc_h_vht(ar, vif, sta, arg);
1667         ath11k_peer_assoc_h_he(ar, vif, sta, arg);
1668         ath11k_peer_assoc_h_qos(ar, vif, sta, arg);
1669         ath11k_peer_assoc_h_phymode(ar, vif, sta, arg);
1670         ath11k_peer_assoc_h_smps(sta, arg);
1671
1672         /* TODO: amsdu_disable req? */
1673 }
1674
1675 static int ath11k_setup_peer_smps(struct ath11k *ar, struct ath11k_vif *arvif,
1676                                   const u8 *addr,
1677                                   const struct ieee80211_sta_ht_cap *ht_cap)
1678 {
1679         int smps;
1680
1681         if (!ht_cap->ht_supported)
1682                 return 0;
1683
1684         smps = ht_cap->cap & IEEE80211_HT_CAP_SM_PS;
1685         smps >>= IEEE80211_HT_CAP_SM_PS_SHIFT;
1686
1687         if (smps >= ARRAY_SIZE(ath11k_smps_map))
1688                 return -EINVAL;
1689
1690         return ath11k_wmi_set_peer_param(ar, addr, arvif->vdev_id,
1691                                          WMI_PEER_MIMO_PS_STATE,
1692                                          ath11k_smps_map[smps]);
1693 }
1694
1695 static void ath11k_bss_assoc(struct ieee80211_hw *hw,
1696                              struct ieee80211_vif *vif,
1697                              struct ieee80211_bss_conf *bss_conf)
1698 {
1699         struct ath11k *ar = hw->priv;
1700         struct ath11k_vif *arvif = (void *)vif->drv_priv;
1701         struct peer_assoc_params peer_arg;
1702         struct ieee80211_sta *ap_sta;
1703         int ret;
1704
1705         lockdep_assert_held(&ar->conf_mutex);
1706
1707         ath11k_dbg(ar->ab, ATH11K_DBG_MAC, "mac vdev %i assoc bssid %pM aid %d\n",
1708                    arvif->vdev_id, arvif->bssid, arvif->aid);
1709
1710         rcu_read_lock();
1711
1712         ap_sta = ieee80211_find_sta(vif, bss_conf->bssid);
1713         if (!ap_sta) {
1714                 ath11k_warn(ar->ab, "failed to find station entry for bss %pM vdev %i\n",
1715                             bss_conf->bssid, arvif->vdev_id);
1716                 rcu_read_unlock();
1717                 return;
1718         }
1719
1720         ath11k_peer_assoc_prepare(ar, vif, ap_sta, &peer_arg, false);
1721
1722         rcu_read_unlock();
1723
1724         ret = ath11k_wmi_send_peer_assoc_cmd(ar, &peer_arg);
1725         if (ret) {
1726                 ath11k_warn(ar->ab, "failed to run peer assoc for %pM vdev %i: %d\n",
1727                             bss_conf->bssid, arvif->vdev_id, ret);
1728                 return;
1729         }
1730
1731         if (!wait_for_completion_timeout(&ar->peer_assoc_done, 1 * HZ)) {
1732                 ath11k_warn(ar->ab, "failed to get peer assoc conf event for %pM vdev %i\n",
1733                             bss_conf->bssid, arvif->vdev_id);
1734                 return;
1735         }
1736
1737         ret = ath11k_setup_peer_smps(ar, arvif, bss_conf->bssid,
1738                                      &ap_sta->ht_cap);
1739         if (ret) {
1740                 ath11k_warn(ar->ab, "failed to setup peer SMPS for vdev %d: %d\n",
1741                             arvif->vdev_id, ret);
1742                 return;
1743         }
1744
1745         WARN_ON(arvif->is_up);
1746
1747         arvif->aid = bss_conf->aid;
1748         ether_addr_copy(arvif->bssid, bss_conf->bssid);
1749
1750         ret = ath11k_wmi_vdev_up(ar, arvif->vdev_id, arvif->aid, arvif->bssid);
1751         if (ret) {
1752                 ath11k_warn(ar->ab, "failed to set vdev %d up: %d\n",
1753                             arvif->vdev_id, ret);
1754                 return;
1755         }
1756
1757         arvif->is_up = true;
1758
1759         ath11k_dbg(ar->ab, ATH11K_DBG_MAC,
1760                    "mac vdev %d up (associated) bssid %pM aid %d\n",
1761                    arvif->vdev_id, bss_conf->bssid, bss_conf->aid);
1762
1763         /* Authorize BSS Peer */
1764         ret = ath11k_wmi_set_peer_param(ar, arvif->bssid,
1765                                         arvif->vdev_id,
1766                                         WMI_PEER_AUTHORIZE,
1767                                         1);
1768         if (ret)
1769                 ath11k_warn(ar->ab, "Unable to authorize BSS peer: %d\n", ret);
1770
1771         ret = ath11k_wmi_send_obss_spr_cmd(ar, arvif->vdev_id,
1772                                            &bss_conf->he_obss_pd);
1773         if (ret)
1774                 ath11k_warn(ar->ab, "failed to set vdev %i OBSS PD parameters: %d\n",
1775                             arvif->vdev_id, ret);
1776 }
1777
1778 static void ath11k_bss_disassoc(struct ieee80211_hw *hw,
1779                                 struct ieee80211_vif *vif)
1780 {
1781         struct ath11k *ar = hw->priv;
1782         struct ath11k_vif *arvif = (void *)vif->drv_priv;
1783         int ret;
1784
1785         lockdep_assert_held(&ar->conf_mutex);
1786
1787         ath11k_dbg(ar->ab, ATH11K_DBG_MAC, "mac vdev %i disassoc bssid %pM\n",
1788                    arvif->vdev_id, arvif->bssid);
1789
1790         ret = ath11k_wmi_vdev_down(ar, arvif->vdev_id);
1791         if (ret)
1792                 ath11k_warn(ar->ab, "failed to down vdev %i: %d\n",
1793                             arvif->vdev_id, ret);
1794
1795         arvif->is_up = false;
1796
1797         /* TODO: cancel connection_loss_work */
1798 }
1799
1800 static u32 ath11k_mac_get_rate_hw_value(int bitrate)
1801 {
1802         u32 preamble;
1803         u16 hw_value;
1804         int rate;
1805         size_t i;
1806
1807         if (ath11k_mac_bitrate_is_cck(bitrate))
1808                 preamble = WMI_RATE_PREAMBLE_CCK;
1809         else
1810                 preamble = WMI_RATE_PREAMBLE_OFDM;
1811
1812         for (i = 0; i < ARRAY_SIZE(ath11k_legacy_rates); i++) {
1813                 if (ath11k_legacy_rates[i].bitrate != bitrate)
1814                         continue;
1815
1816                 hw_value = ath11k_legacy_rates[i].hw_value;
1817                 rate = ATH11K_HW_RATE_CODE(hw_value, 0, preamble);
1818
1819                 return rate;
1820         }
1821
1822         return -EINVAL;
1823 }
1824
1825 static void ath11k_recalculate_mgmt_rate(struct ath11k *ar,
1826                                          struct ieee80211_vif *vif,
1827                                          struct cfg80211_chan_def *def)
1828 {
1829         struct ath11k_vif *arvif = (void *)vif->drv_priv;
1830         const struct ieee80211_supported_band *sband;
1831         u8 basic_rate_idx;
1832         int hw_rate_code;
1833         u32 vdev_param;
1834         u16 bitrate;
1835         int ret;
1836
1837         lockdep_assert_held(&ar->conf_mutex);
1838
1839         sband = ar->hw->wiphy->bands[def->chan->band];
1840         basic_rate_idx = ffs(vif->bss_conf.basic_rates) - 1;
1841         bitrate = sband->bitrates[basic_rate_idx].bitrate;
1842
1843         hw_rate_code = ath11k_mac_get_rate_hw_value(bitrate);
1844         if (hw_rate_code < 0) {
1845                 ath11k_warn(ar->ab, "bitrate not supported %d\n", bitrate);
1846                 return;
1847         }
1848
1849         vdev_param = WMI_VDEV_PARAM_MGMT_RATE;
1850         ret = ath11k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id, vdev_param,
1851                                             hw_rate_code);
1852         if (ret)
1853                 ath11k_warn(ar->ab, "failed to set mgmt tx rate %d\n", ret);
1854
1855         vdev_param = WMI_VDEV_PARAM_BEACON_RATE;
1856         ret = ath11k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id, vdev_param,
1857                                             hw_rate_code);
1858         if (ret)
1859                 ath11k_warn(ar->ab, "failed to set beacon tx rate %d\n", ret);
1860 }
1861
1862 static void ath11k_mac_op_bss_info_changed(struct ieee80211_hw *hw,
1863                                            struct ieee80211_vif *vif,
1864                                            struct ieee80211_bss_conf *info,
1865                                            u32 changed)
1866 {
1867         struct ath11k *ar = hw->priv;
1868         struct ath11k_vif *arvif = ath11k_vif_to_arvif(vif);
1869         struct cfg80211_chan_def def;
1870         u32 param_id, param_value;
1871         enum nl80211_band band;
1872         u32 vdev_param;
1873         int mcast_rate;
1874         u32 preamble;
1875         u16 hw_value;
1876         u16 bitrate;
1877         int ret = 0;
1878         u8 rateidx;
1879         u32 rate;
1880
1881         mutex_lock(&ar->conf_mutex);
1882
1883         if (changed & BSS_CHANGED_BEACON_INT) {
1884                 arvif->beacon_interval = info->beacon_int;
1885
1886                 param_id = WMI_VDEV_PARAM_BEACON_INTERVAL;
1887                 ret = ath11k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
1888                                                     param_id,
1889                                                     arvif->beacon_interval);
1890                 if (ret)
1891                         ath11k_warn(ar->ab, "Failed to set beacon interval for VDEV: %d\n",
1892                                     arvif->vdev_id);
1893                 else
1894                         ath11k_dbg(ar->ab, ATH11K_DBG_MAC,
1895                                    "Beacon interval: %d set for VDEV: %d\n",
1896                                    arvif->beacon_interval, arvif->vdev_id);
1897         }
1898
1899         if (changed & BSS_CHANGED_BEACON) {
1900                 param_id = WMI_PDEV_PARAM_BEACON_TX_MODE;
1901                 param_value = WMI_BEACON_STAGGERED_MODE;
1902                 ret = ath11k_wmi_pdev_set_param(ar, param_id,
1903                                                 param_value, ar->pdev->pdev_id);
1904                 if (ret)
1905                         ath11k_warn(ar->ab, "Failed to set beacon mode for VDEV: %d\n",
1906                                     arvif->vdev_id);
1907                 else
1908                         ath11k_dbg(ar->ab, ATH11K_DBG_MAC,
1909                                    "Set staggered beacon mode for VDEV: %d\n",
1910                                    arvif->vdev_id);
1911
1912                 ret = ath11k_mac_setup_bcn_tmpl(arvif);
1913                 if (ret)
1914                         ath11k_warn(ar->ab, "failed to update bcn template: %d\n",
1915                                     ret);
1916
1917                 if (vif->bss_conf.he_support) {
1918                         ret = ath11k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
1919                                                             WMI_VDEV_PARAM_BA_MODE,
1920                                                             WMI_BA_MODE_BUFFER_SIZE_256);
1921                         if (ret)
1922                                 ath11k_warn(ar->ab,
1923                                             "failed to set BA BUFFER SIZE 256 for vdev: %d\n",
1924                                             arvif->vdev_id);
1925                         else
1926                                 ath11k_dbg(ar->ab, ATH11K_DBG_MAC,
1927                                            "Set BA BUFFER SIZE 256 for VDEV: %d\n",
1928                                            arvif->vdev_id);
1929                 }
1930         }
1931
1932         if (changed & (BSS_CHANGED_BEACON_INFO | BSS_CHANGED_BEACON)) {
1933                 arvif->dtim_period = info->dtim_period;
1934
1935                 param_id = WMI_VDEV_PARAM_DTIM_PERIOD;
1936                 ret = ath11k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
1937                                                     param_id,
1938                                                     arvif->dtim_period);
1939
1940                 if (ret)
1941                         ath11k_warn(ar->ab, "Failed to set dtim period for VDEV %d: %i\n",
1942                                     arvif->vdev_id, ret);
1943                 else
1944                         ath11k_dbg(ar->ab, ATH11K_DBG_MAC,
1945                                    "DTIM period: %d set for VDEV: %d\n",
1946                                    arvif->dtim_period, arvif->vdev_id);
1947         }
1948
1949         if (changed & BSS_CHANGED_SSID &&
1950             vif->type == NL80211_IFTYPE_AP) {
1951                 arvif->u.ap.ssid_len = info->ssid_len;
1952                 if (info->ssid_len)
1953                         memcpy(arvif->u.ap.ssid, info->ssid, info->ssid_len);
1954                 arvif->u.ap.hidden_ssid = info->hidden_ssid;
1955         }
1956
1957         if (changed & BSS_CHANGED_BSSID && !is_zero_ether_addr(info->bssid))
1958                 ether_addr_copy(arvif->bssid, info->bssid);
1959
1960         if (changed & BSS_CHANGED_BEACON_ENABLED)
1961                 ath11k_control_beaconing(arvif, info);
1962
1963         if (changed & BSS_CHANGED_ERP_CTS_PROT) {
1964                 u32 cts_prot;
1965
1966                 cts_prot = !!(info->use_cts_prot);
1967                 param_id = WMI_VDEV_PARAM_PROTECTION_MODE;
1968
1969                 if (arvif->is_started) {
1970                         ret = ath11k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
1971                                                             param_id, cts_prot);
1972                         if (ret)
1973                                 ath11k_warn(ar->ab, "Failed to set CTS prot for VDEV: %d\n",
1974                                             arvif->vdev_id);
1975                         else
1976                                 ath11k_dbg(ar->ab, ATH11K_DBG_MAC, "Set CTS prot: %d for VDEV: %d\n",
1977                                            cts_prot, arvif->vdev_id);
1978                 } else {
1979                         ath11k_dbg(ar->ab, ATH11K_DBG_MAC, "defer protection mode setup, vdev is not ready yet\n");
1980                 }
1981         }
1982
1983         if (changed & BSS_CHANGED_ERP_SLOT) {
1984                 u32 slottime;
1985
1986                 if (info->use_short_slot)
1987                         slottime = WMI_VDEV_SLOT_TIME_SHORT; /* 9us */
1988
1989                 else
1990                         slottime = WMI_VDEV_SLOT_TIME_LONG; /* 20us */
1991
1992                 param_id = WMI_VDEV_PARAM_SLOT_TIME;
1993                 ret = ath11k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
1994                                                     param_id, slottime);
1995                 if (ret)
1996                         ath11k_warn(ar->ab, "Failed to set erp slot for VDEV: %d\n",
1997                                     arvif->vdev_id);
1998                 else
1999                         ath11k_dbg(ar->ab, ATH11K_DBG_MAC,
2000                                    "Set slottime: %d for VDEV: %d\n",
2001                                    slottime, arvif->vdev_id);
2002         }
2003
2004         if (changed & BSS_CHANGED_ERP_PREAMBLE) {
2005                 u32 preamble;
2006
2007                 if (info->use_short_preamble)
2008                         preamble = WMI_VDEV_PREAMBLE_SHORT;
2009                 else
2010                         preamble = WMI_VDEV_PREAMBLE_LONG;
2011
2012                 param_id = WMI_VDEV_PARAM_PREAMBLE;
2013                 ret = ath11k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
2014                                                     param_id, preamble);
2015                 if (ret)
2016                         ath11k_warn(ar->ab, "Failed to set preamble for VDEV: %d\n",
2017                                     arvif->vdev_id);
2018                 else
2019                         ath11k_dbg(ar->ab, ATH11K_DBG_MAC,
2020                                    "Set preamble: %d for VDEV: %d\n",
2021                                    preamble, arvif->vdev_id);
2022         }
2023
2024         if (changed & BSS_CHANGED_ASSOC) {
2025                 if (info->assoc)
2026                         ath11k_bss_assoc(hw, vif, info);
2027                 else
2028                         ath11k_bss_disassoc(hw, vif);
2029         }
2030
2031         if (changed & BSS_CHANGED_TXPOWER) {
2032                 ath11k_dbg(ar->ab, ATH11K_DBG_MAC, "mac vdev_id %i txpower %d\n",
2033                            arvif->vdev_id, info->txpower);
2034
2035                 arvif->txpower = info->txpower;
2036                 ath11k_mac_txpower_recalc(ar);
2037         }
2038
2039         if (changed & BSS_CHANGED_MCAST_RATE &&
2040             !ath11k_mac_vif_chan(arvif->vif, &def)) {
2041                 band = def.chan->band;
2042                 mcast_rate = vif->bss_conf.mcast_rate[band];
2043
2044                 if (mcast_rate > 0)
2045                         rateidx = mcast_rate - 1;
2046                 else
2047                         rateidx = ffs(vif->bss_conf.basic_rates) - 1;
2048
2049                 if (ar->pdev->cap.supported_bands & WMI_HOST_WLAN_5G_CAP)
2050                         rateidx += ATH11K_MAC_FIRST_OFDM_RATE_IDX;
2051
2052                 bitrate = ath11k_legacy_rates[rateidx].bitrate;
2053                 hw_value = ath11k_legacy_rates[rateidx].hw_value;
2054
2055                 if (ath11k_mac_bitrate_is_cck(bitrate))
2056                         preamble = WMI_RATE_PREAMBLE_CCK;
2057                 else
2058                         preamble = WMI_RATE_PREAMBLE_OFDM;
2059
2060                 rate = ATH11K_HW_RATE_CODE(hw_value, 0, preamble);
2061
2062                 ath11k_dbg(ar->ab, ATH11K_DBG_MAC,
2063                            "mac vdev %d mcast_rate %x\n",
2064                            arvif->vdev_id, rate);
2065
2066                 vdev_param = WMI_VDEV_PARAM_MCAST_DATA_RATE;
2067                 ret = ath11k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
2068                                                     vdev_param, rate);
2069                 if (ret)
2070                         ath11k_warn(ar->ab,
2071                                     "failed to set mcast rate on vdev %i: %d\n",
2072                                     arvif->vdev_id,  ret);
2073
2074                 vdev_param = WMI_VDEV_PARAM_BCAST_DATA_RATE;
2075                 ret = ath11k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
2076                                                     vdev_param, rate);
2077                 if (ret)
2078                         ath11k_warn(ar->ab,
2079                                     "failed to set bcast rate on vdev %i: %d\n",
2080                                     arvif->vdev_id,  ret);
2081         }
2082
2083         if (changed & BSS_CHANGED_BASIC_RATES &&
2084             !ath11k_mac_vif_chan(arvif->vif, &def))
2085                 ath11k_recalculate_mgmt_rate(ar, vif, &def);
2086
2087         if (changed & BSS_CHANGED_TWT) {
2088                 if (info->twt_requester || info->twt_responder)
2089                         ath11k_wmi_send_twt_enable_cmd(ar, ar->pdev->pdev_id);
2090                 else
2091                         ath11k_wmi_send_twt_disable_cmd(ar, ar->pdev->pdev_id);
2092         }
2093
2094         if (changed & BSS_CHANGED_HE_OBSS_PD)
2095                 ath11k_wmi_send_obss_spr_cmd(ar, arvif->vdev_id,
2096                                              &info->he_obss_pd);
2097
2098         if (changed & BSS_CHANGED_HE_BSS_COLOR) {
2099                 if (vif->type == NL80211_IFTYPE_AP) {
2100                         ret = ath11k_wmi_send_obss_color_collision_cfg_cmd(
2101                                 ar, arvif->vdev_id, info->he_bss_color.color,
2102                                 ATH11K_BSS_COLOR_COLLISION_DETECTION_AP_PERIOD_MS,
2103                                 info->he_bss_color.enabled);
2104                         if (ret)
2105                                 ath11k_warn(ar->ab, "failed to set bss color collision on vdev %i: %d\n",
2106                                             arvif->vdev_id,  ret);
2107                 } else if (vif->type == NL80211_IFTYPE_STATION) {
2108                         ret = ath11k_wmi_send_bss_color_change_enable_cmd(ar,
2109                                                                           arvif->vdev_id,
2110                                                                           1);
2111                         if (ret)
2112                                 ath11k_warn(ar->ab, "failed to enable bss color change on vdev %i: %d\n",
2113                                             arvif->vdev_id,  ret);
2114                         ret = ath11k_wmi_send_obss_color_collision_cfg_cmd(
2115                                 ar, arvif->vdev_id, 0,
2116                                 ATH11K_BSS_COLOR_COLLISION_DETECTION_STA_PERIOD_MS, 1);
2117                         if (ret)
2118                                 ath11k_warn(ar->ab, "failed to set bss color collision on vdev %i: %d\n",
2119                                             arvif->vdev_id,  ret);
2120                 }
2121         }
2122
2123         mutex_unlock(&ar->conf_mutex);
2124 }
2125
2126 void __ath11k_mac_scan_finish(struct ath11k *ar)
2127 {
2128         lockdep_assert_held(&ar->data_lock);
2129
2130         switch (ar->scan.state) {
2131         case ATH11K_SCAN_IDLE:
2132                 break;
2133         case ATH11K_SCAN_RUNNING:
2134         case ATH11K_SCAN_ABORTING:
2135                 if (!ar->scan.is_roc) {
2136                         struct cfg80211_scan_info info = {
2137                                 .aborted = (ar->scan.state ==
2138                                             ATH11K_SCAN_ABORTING),
2139                         };
2140
2141                         ieee80211_scan_completed(ar->hw, &info);
2142                 } else if (ar->scan.roc_notify) {
2143                         ieee80211_remain_on_channel_expired(ar->hw);
2144                 }
2145                 fallthrough;
2146         case ATH11K_SCAN_STARTING:
2147                 ar->scan.state = ATH11K_SCAN_IDLE;
2148                 ar->scan_channel = NULL;
2149                 ar->scan.roc_freq = 0;
2150                 cancel_delayed_work(&ar->scan.timeout);
2151                 complete(&ar->scan.completed);
2152                 break;
2153         }
2154 }
2155
2156 void ath11k_mac_scan_finish(struct ath11k *ar)
2157 {
2158         spin_lock_bh(&ar->data_lock);
2159         __ath11k_mac_scan_finish(ar);
2160         spin_unlock_bh(&ar->data_lock);
2161 }
2162
2163 static int ath11k_scan_stop(struct ath11k *ar)
2164 {
2165         struct scan_cancel_param arg = {
2166                 .req_type = WLAN_SCAN_CANCEL_SINGLE,
2167                 .scan_id = ATH11K_SCAN_ID,
2168         };
2169         int ret;
2170
2171         lockdep_assert_held(&ar->conf_mutex);
2172
2173         /* TODO: Fill other STOP Params */
2174         arg.pdev_id = ar->pdev->pdev_id;
2175
2176         ret = ath11k_wmi_send_scan_stop_cmd(ar, &arg);
2177         if (ret) {
2178                 ath11k_warn(ar->ab, "failed to stop wmi scan: %d\n", ret);
2179                 goto out;
2180         }
2181
2182         ret = wait_for_completion_timeout(&ar->scan.completed, 3 * HZ);
2183         if (ret == 0) {
2184                 ath11k_warn(ar->ab,
2185                             "failed to receive scan abort comple: timed out\n");
2186                 ret = -ETIMEDOUT;
2187         } else if (ret > 0) {
2188                 ret = 0;
2189         }
2190
2191 out:
2192         /* Scan state should be updated upon scan completion but in case
2193          * firmware fails to deliver the event (for whatever reason) it is
2194          * desired to clean up scan state anyway. Firmware may have just
2195          * dropped the scan completion event delivery due to transport pipe
2196          * being overflown with data and/or it can recover on its own before
2197          * next scan request is submitted.
2198          */
2199         spin_lock_bh(&ar->data_lock);
2200         if (ar->scan.state != ATH11K_SCAN_IDLE)
2201                 __ath11k_mac_scan_finish(ar);
2202         spin_unlock_bh(&ar->data_lock);
2203
2204         return ret;
2205 }
2206
2207 static void ath11k_scan_abort(struct ath11k *ar)
2208 {
2209         int ret;
2210
2211         lockdep_assert_held(&ar->conf_mutex);
2212
2213         spin_lock_bh(&ar->data_lock);
2214
2215         switch (ar->scan.state) {
2216         case ATH11K_SCAN_IDLE:
2217                 /* This can happen if timeout worker kicked in and called
2218                  * abortion while scan completion was being processed.
2219                  */
2220                 break;
2221         case ATH11K_SCAN_STARTING:
2222         case ATH11K_SCAN_ABORTING:
2223                 ath11k_warn(ar->ab, "refusing scan abortion due to invalid scan state: %d\n",
2224                             ar->scan.state);
2225                 break;
2226         case ATH11K_SCAN_RUNNING:
2227                 ar->scan.state = ATH11K_SCAN_ABORTING;
2228                 spin_unlock_bh(&ar->data_lock);
2229
2230                 ret = ath11k_scan_stop(ar);
2231                 if (ret)
2232                         ath11k_warn(ar->ab, "failed to abort scan: %d\n", ret);
2233
2234                 spin_lock_bh(&ar->data_lock);
2235                 break;
2236         }
2237
2238         spin_unlock_bh(&ar->data_lock);
2239 }
2240
2241 static void ath11k_scan_timeout_work(struct work_struct *work)
2242 {
2243         struct ath11k *ar = container_of(work, struct ath11k,
2244                                          scan.timeout.work);
2245
2246         mutex_lock(&ar->conf_mutex);
2247         ath11k_scan_abort(ar);
2248         mutex_unlock(&ar->conf_mutex);
2249 }
2250
2251 static int ath11k_start_scan(struct ath11k *ar,
2252                              struct scan_req_params *arg)
2253 {
2254         int ret;
2255
2256         lockdep_assert_held(&ar->conf_mutex);
2257
2258         if (ath11k_spectral_get_mode(ar) == ATH11K_SPECTRAL_BACKGROUND)
2259                 ath11k_spectral_reset_buffer(ar);
2260
2261         ret = ath11k_wmi_send_scan_start_cmd(ar, arg);
2262         if (ret)
2263                 return ret;
2264
2265         ret = wait_for_completion_timeout(&ar->scan.started, 1 * HZ);
2266         if (ret == 0) {
2267                 ret = ath11k_scan_stop(ar);
2268                 if (ret)
2269                         ath11k_warn(ar->ab, "failed to stop scan: %d\n", ret);
2270
2271                 return -ETIMEDOUT;
2272         }
2273
2274         /* If we failed to start the scan, return error code at
2275          * this point.  This is probably due to some issue in the
2276          * firmware, but no need to wedge the driver due to that...
2277          */
2278         spin_lock_bh(&ar->data_lock);
2279         if (ar->scan.state == ATH11K_SCAN_IDLE) {
2280                 spin_unlock_bh(&ar->data_lock);
2281                 return -EINVAL;
2282         }
2283         spin_unlock_bh(&ar->data_lock);
2284
2285         return 0;
2286 }
2287
2288 static int ath11k_mac_op_hw_scan(struct ieee80211_hw *hw,
2289                                  struct ieee80211_vif *vif,
2290                                  struct ieee80211_scan_request *hw_req)
2291 {
2292         struct ath11k *ar = hw->priv;
2293         struct ath11k_vif *arvif = ath11k_vif_to_arvif(vif);
2294         struct cfg80211_scan_request *req = &hw_req->req;
2295         struct scan_req_params arg;
2296         int ret = 0;
2297         int i;
2298
2299         mutex_lock(&ar->conf_mutex);
2300
2301         spin_lock_bh(&ar->data_lock);
2302         switch (ar->scan.state) {
2303         case ATH11K_SCAN_IDLE:
2304                 reinit_completion(&ar->scan.started);
2305                 reinit_completion(&ar->scan.completed);
2306                 ar->scan.state = ATH11K_SCAN_STARTING;
2307                 ar->scan.is_roc = false;
2308                 ar->scan.vdev_id = arvif->vdev_id;
2309                 ret = 0;
2310                 break;
2311         case ATH11K_SCAN_STARTING:
2312         case ATH11K_SCAN_RUNNING:
2313         case ATH11K_SCAN_ABORTING:
2314                 ret = -EBUSY;
2315                 break;
2316         }
2317         spin_unlock_bh(&ar->data_lock);
2318
2319         if (ret)
2320                 goto exit;
2321
2322         memset(&arg, 0, sizeof(arg));
2323         ath11k_wmi_start_scan_init(ar, &arg);
2324         arg.vdev_id = arvif->vdev_id;
2325         arg.scan_id = ATH11K_SCAN_ID;
2326
2327         if (req->ie_len) {
2328                 arg.extraie.len = req->ie_len;
2329                 arg.extraie.ptr = kzalloc(req->ie_len, GFP_KERNEL);
2330                 memcpy(arg.extraie.ptr, req->ie, req->ie_len);
2331         }
2332
2333         if (req->n_ssids) {
2334                 arg.num_ssids = req->n_ssids;
2335                 for (i = 0; i < arg.num_ssids; i++) {
2336                         arg.ssid[i].length  = req->ssids[i].ssid_len;
2337                         memcpy(&arg.ssid[i].ssid, req->ssids[i].ssid,
2338                                req->ssids[i].ssid_len);
2339                 }
2340         } else {
2341                 arg.scan_flags |= WMI_SCAN_FLAG_PASSIVE;
2342         }
2343
2344         if (req->n_channels) {
2345                 arg.num_chan = req->n_channels;
2346                 for (i = 0; i < arg.num_chan; i++)
2347                         arg.chan_list[i] = req->channels[i]->center_freq;
2348         }
2349
2350         ret = ath11k_start_scan(ar, &arg);
2351         if (ret) {
2352                 ath11k_warn(ar->ab, "failed to start hw scan: %d\n", ret);
2353                 spin_lock_bh(&ar->data_lock);
2354                 ar->scan.state = ATH11K_SCAN_IDLE;
2355                 spin_unlock_bh(&ar->data_lock);
2356         }
2357
2358         /* Add a 200ms margin to account for event/command processing */
2359         ieee80211_queue_delayed_work(ar->hw, &ar->scan.timeout,
2360                                      msecs_to_jiffies(arg.max_scan_time +
2361                                                       ATH11K_MAC_SCAN_TIMEOUT_MSECS));
2362
2363 exit:
2364         if (req->ie_len)
2365                 kfree(arg.extraie.ptr);
2366
2367         mutex_unlock(&ar->conf_mutex);
2368         return ret;
2369 }
2370
2371 static void ath11k_mac_op_cancel_hw_scan(struct ieee80211_hw *hw,
2372                                          struct ieee80211_vif *vif)
2373 {
2374         struct ath11k *ar = hw->priv;
2375
2376         mutex_lock(&ar->conf_mutex);
2377         ath11k_scan_abort(ar);
2378         mutex_unlock(&ar->conf_mutex);
2379
2380         cancel_delayed_work_sync(&ar->scan.timeout);
2381 }
2382
2383 static int ath11k_install_key(struct ath11k_vif *arvif,
2384                               struct ieee80211_key_conf *key,
2385                               enum set_key_cmd cmd,
2386                               const u8 *macaddr, u32 flags)
2387 {
2388         int ret;
2389         struct ath11k *ar = arvif->ar;
2390         struct wmi_vdev_install_key_arg arg = {
2391                 .vdev_id = arvif->vdev_id,
2392                 .key_idx = key->keyidx,
2393                 .key_len = key->keylen,
2394                 .key_data = key->key,
2395                 .key_flags = flags,
2396                 .macaddr = macaddr,
2397         };
2398
2399         lockdep_assert_held(&arvif->ar->conf_mutex);
2400
2401         reinit_completion(&ar->install_key_done);
2402
2403         if (test_bit(ATH11K_FLAG_HW_CRYPTO_DISABLED, &ar->ab->dev_flags))
2404                 return 0;
2405
2406         if (cmd == DISABLE_KEY) {
2407                 /* TODO: Check if FW expects  value other than NONE for del */
2408                 /* arg.key_cipher = WMI_CIPHER_NONE; */
2409                 arg.key_len = 0;
2410                 arg.key_data = NULL;
2411                 goto install;
2412         }
2413
2414         switch (key->cipher) {
2415         case WLAN_CIPHER_SUITE_CCMP:
2416                 arg.key_cipher = WMI_CIPHER_AES_CCM;
2417                 /* TODO: Re-check if flag is valid */
2418                 key->flags |= IEEE80211_KEY_FLAG_GENERATE_IV_MGMT;
2419                 break;
2420         case WLAN_CIPHER_SUITE_TKIP:
2421                 arg.key_cipher = WMI_CIPHER_TKIP;
2422                 arg.key_txmic_len = 8;
2423                 arg.key_rxmic_len = 8;
2424                 break;
2425         case WLAN_CIPHER_SUITE_CCMP_256:
2426                 arg.key_cipher = WMI_CIPHER_AES_CCM;
2427                 break;
2428         case WLAN_CIPHER_SUITE_GCMP:
2429         case WLAN_CIPHER_SUITE_GCMP_256:
2430                 arg.key_cipher = WMI_CIPHER_AES_GCM;
2431                 break;
2432         default:
2433                 ath11k_warn(ar->ab, "cipher %d is not supported\n", key->cipher);
2434                 return -EOPNOTSUPP;
2435         }
2436
2437         if (test_bit(ATH11K_FLAG_RAW_MODE, &ar->ab->dev_flags))
2438                 key->flags |= IEEE80211_KEY_FLAG_GENERATE_IV |
2439                               IEEE80211_KEY_FLAG_RESERVE_TAILROOM;
2440
2441 install:
2442         ret = ath11k_wmi_vdev_install_key(arvif->ar, &arg);
2443
2444         if (ret)
2445                 return ret;
2446
2447         if (!wait_for_completion_timeout(&ar->install_key_done, 1 * HZ))
2448                 return -ETIMEDOUT;
2449
2450         return ar->install_key_status ? -EINVAL : 0;
2451 }
2452
2453 static int ath11k_clear_peer_keys(struct ath11k_vif *arvif,
2454                                   const u8 *addr)
2455 {
2456         struct ath11k *ar = arvif->ar;
2457         struct ath11k_base *ab = ar->ab;
2458         struct ath11k_peer *peer;
2459         int first_errno = 0;
2460         int ret;
2461         int i;
2462         u32 flags = 0;
2463
2464         lockdep_assert_held(&ar->conf_mutex);
2465
2466         spin_lock_bh(&ab->base_lock);
2467         peer = ath11k_peer_find(ab, arvif->vdev_id, addr);
2468         spin_unlock_bh(&ab->base_lock);
2469
2470         if (!peer)
2471                 return -ENOENT;
2472
2473         for (i = 0; i < ARRAY_SIZE(peer->keys); i++) {
2474                 if (!peer->keys[i])
2475                         continue;
2476
2477                 /* key flags are not required to delete the key */
2478                 ret = ath11k_install_key(arvif, peer->keys[i],
2479                                          DISABLE_KEY, addr, flags);
2480                 if (ret < 0 && first_errno == 0)
2481                         first_errno = ret;
2482
2483                 if (ret < 0)
2484                         ath11k_warn(ab, "failed to remove peer key %d: %d\n",
2485                                     i, ret);
2486
2487                 spin_lock_bh(&ab->base_lock);
2488                 peer->keys[i] = NULL;
2489                 spin_unlock_bh(&ab->base_lock);
2490         }
2491
2492         return first_errno;
2493 }
2494
2495 static int ath11k_mac_op_set_key(struct ieee80211_hw *hw, enum set_key_cmd cmd,
2496                                  struct ieee80211_vif *vif, struct ieee80211_sta *sta,
2497                                  struct ieee80211_key_conf *key)
2498 {
2499         struct ath11k *ar = hw->priv;
2500         struct ath11k_base *ab = ar->ab;
2501         struct ath11k_vif *arvif = ath11k_vif_to_arvif(vif);
2502         struct ath11k_peer *peer;
2503         struct ath11k_sta *arsta;
2504         const u8 *peer_addr;
2505         int ret = 0;
2506         u32 flags = 0;
2507
2508         /* BIP needs to be done in software */
2509         if (key->cipher == WLAN_CIPHER_SUITE_AES_CMAC ||
2510             key->cipher == WLAN_CIPHER_SUITE_BIP_GMAC_128 ||
2511             key->cipher == WLAN_CIPHER_SUITE_BIP_GMAC_256 ||
2512             key->cipher == WLAN_CIPHER_SUITE_BIP_CMAC_256)
2513                 return 1;
2514
2515         if (test_bit(ATH11K_FLAG_HW_CRYPTO_DISABLED, &ar->ab->dev_flags))
2516                 return 1;
2517
2518         if (key->keyidx > WMI_MAX_KEY_INDEX)
2519                 return -ENOSPC;
2520
2521         mutex_lock(&ar->conf_mutex);
2522
2523         if (sta)
2524                 peer_addr = sta->addr;
2525         else if (arvif->vdev_type == WMI_VDEV_TYPE_STA)
2526                 peer_addr = vif->bss_conf.bssid;
2527         else
2528                 peer_addr = vif->addr;
2529
2530         key->hw_key_idx = key->keyidx;
2531
2532         /* the peer should not disappear in mid-way (unless FW goes awry) since
2533          * we already hold conf_mutex. we just make sure its there now.
2534          */
2535         spin_lock_bh(&ab->base_lock);
2536         peer = ath11k_peer_find(ab, arvif->vdev_id, peer_addr);
2537         spin_unlock_bh(&ab->base_lock);
2538
2539         if (!peer) {
2540                 if (cmd == SET_KEY) {
2541                         ath11k_warn(ab, "cannot install key for non-existent peer %pM\n",
2542                                     peer_addr);
2543                         ret = -EOPNOTSUPP;
2544                         goto exit;
2545                 } else {
2546                         /* if the peer doesn't exist there is no key to disable
2547                          * anymore
2548                          */
2549                         goto exit;
2550                 }
2551         }
2552
2553         if (key->flags & IEEE80211_KEY_FLAG_PAIRWISE)
2554                 flags |= WMI_KEY_PAIRWISE;
2555         else
2556                 flags |= WMI_KEY_GROUP;
2557
2558         ret = ath11k_install_key(arvif, key, cmd, peer_addr, flags);
2559         if (ret) {
2560                 ath11k_warn(ab, "ath11k_install_key failed (%d)\n", ret);
2561                 goto exit;
2562         }
2563
2564         ret = ath11k_dp_peer_rx_pn_replay_config(arvif, peer_addr, cmd, key);
2565         if (ret) {
2566                 ath11k_warn(ab, "failed to offload PN replay detection %d\n", ret);
2567                 goto exit;
2568         }
2569
2570         spin_lock_bh(&ab->base_lock);
2571         peer = ath11k_peer_find(ab, arvif->vdev_id, peer_addr);
2572         if (peer && cmd == SET_KEY) {
2573                 peer->keys[key->keyidx] = key;
2574                 if (key->flags & IEEE80211_KEY_FLAG_PAIRWISE) {
2575                         peer->ucast_keyidx = key->keyidx;
2576                         peer->sec_type = ath11k_dp_tx_get_encrypt_type(key->cipher);
2577                 } else {
2578                         peer->mcast_keyidx = key->keyidx;
2579                         peer->sec_type_grp = ath11k_dp_tx_get_encrypt_type(key->cipher);
2580                 }
2581         } else if (peer && cmd == DISABLE_KEY) {
2582                 peer->keys[key->keyidx] = NULL;
2583                 if (key->flags & IEEE80211_KEY_FLAG_PAIRWISE)
2584                         peer->ucast_keyidx = 0;
2585                 else
2586                         peer->mcast_keyidx = 0;
2587         } else if (!peer)
2588                 /* impossible unless FW goes crazy */
2589                 ath11k_warn(ab, "peer %pM disappeared!\n", peer_addr);
2590
2591         if (sta) {
2592                 arsta = (struct ath11k_sta *)sta->drv_priv;
2593
2594                 switch (key->cipher) {
2595                 case WLAN_CIPHER_SUITE_TKIP:
2596                 case WLAN_CIPHER_SUITE_CCMP:
2597                 case WLAN_CIPHER_SUITE_CCMP_256:
2598                 case WLAN_CIPHER_SUITE_GCMP:
2599                 case WLAN_CIPHER_SUITE_GCMP_256:
2600                         if (cmd == SET_KEY)
2601                                 arsta->pn_type = HAL_PN_TYPE_WPA;
2602                         else
2603                                 arsta->pn_type = HAL_PN_TYPE_NONE;
2604                         break;
2605                 default:
2606                         arsta->pn_type = HAL_PN_TYPE_NONE;
2607                         break;
2608                 }
2609         }
2610
2611         spin_unlock_bh(&ab->base_lock);
2612
2613 exit:
2614         mutex_unlock(&ar->conf_mutex);
2615         return ret;
2616 }
2617
2618 static int
2619 ath11k_mac_bitrate_mask_num_vht_rates(struct ath11k *ar,
2620                                       enum nl80211_band band,
2621                                       const struct cfg80211_bitrate_mask *mask)
2622 {
2623         int num_rates = 0;
2624         int i;
2625
2626         for (i = 0; i < ARRAY_SIZE(mask->control[band].vht_mcs); i++)
2627                 num_rates += hweight16(mask->control[band].vht_mcs[i]);
2628
2629         return num_rates;
2630 }
2631
2632 static int
2633 ath11k_mac_set_peer_vht_fixed_rate(struct ath11k_vif *arvif,
2634                                    struct ieee80211_sta *sta,
2635                                    const struct cfg80211_bitrate_mask *mask,
2636                                    enum nl80211_band band)
2637 {
2638         struct ath11k *ar = arvif->ar;
2639         u8 vht_rate, nss;
2640         u32 rate_code;
2641         int ret, i;
2642
2643         lockdep_assert_held(&ar->conf_mutex);
2644
2645         nss = 0;
2646
2647         for (i = 0; i < ARRAY_SIZE(mask->control[band].vht_mcs); i++) {
2648                 if (hweight16(mask->control[band].vht_mcs[i]) == 1) {
2649                         nss = i + 1;
2650                         vht_rate = ffs(mask->control[band].vht_mcs[i]) - 1;
2651                 }
2652         }
2653
2654         if (!nss) {
2655                 ath11k_warn(ar->ab, "No single VHT Fixed rate found to set for %pM",
2656                             sta->addr);
2657                 return -EINVAL;
2658         }
2659
2660         ath11k_dbg(ar->ab, ATH11K_DBG_MAC,
2661                    "Setting Fixed VHT Rate for peer %pM. Device will not switch to any other selected rates",
2662                    sta->addr);
2663
2664         rate_code = ATH11K_HW_RATE_CODE(vht_rate, nss - 1,
2665                                         WMI_RATE_PREAMBLE_VHT);
2666         ret = ath11k_wmi_set_peer_param(ar, sta->addr,
2667                                         arvif->vdev_id,
2668                                         WMI_PEER_PARAM_FIXED_RATE,
2669                                         rate_code);
2670         if (ret)
2671                 ath11k_warn(ar->ab,
2672                             "failed to update STA %pM Fixed Rate %d: %d\n",
2673                              sta->addr, rate_code, ret);
2674
2675         return ret;
2676 }
2677
2678 static int ath11k_station_assoc(struct ath11k *ar,
2679                                 struct ieee80211_vif *vif,
2680                                 struct ieee80211_sta *sta,
2681                                 bool reassoc)
2682 {
2683         struct ath11k_vif *arvif = ath11k_vif_to_arvif(vif);
2684         struct peer_assoc_params peer_arg;
2685         int ret = 0;
2686         struct cfg80211_chan_def def;
2687         enum nl80211_band band;
2688         struct cfg80211_bitrate_mask *mask;
2689         u8 num_vht_rates;
2690
2691         lockdep_assert_held(&ar->conf_mutex);
2692
2693         if (WARN_ON(ath11k_mac_vif_chan(vif, &def)))
2694                 return -EPERM;
2695
2696         band = def.chan->band;
2697         mask = &arvif->bitrate_mask;
2698
2699         ath11k_peer_assoc_prepare(ar, vif, sta, &peer_arg, reassoc);
2700
2701         ret = ath11k_wmi_send_peer_assoc_cmd(ar, &peer_arg);
2702         if (ret) {
2703                 ath11k_warn(ar->ab, "failed to run peer assoc for STA %pM vdev %i: %d\n",
2704                             sta->addr, arvif->vdev_id, ret);
2705                 return ret;
2706         }
2707
2708         if (!wait_for_completion_timeout(&ar->peer_assoc_done, 1 * HZ)) {
2709                 ath11k_warn(ar->ab, "failed to get peer assoc conf event for %pM vdev %i\n",
2710                             sta->addr, arvif->vdev_id);
2711                 return -ETIMEDOUT;
2712         }
2713
2714         num_vht_rates = ath11k_mac_bitrate_mask_num_vht_rates(ar, band, mask);
2715
2716         /* If single VHT rate is configured (by set_bitrate_mask()),
2717          * peer_assoc will disable VHT. This is now enabled by a peer specific
2718          * fixed param.
2719          * Note that all other rates and NSS will be disabled for this peer.
2720          */
2721         if (sta->vht_cap.vht_supported && num_vht_rates == 1) {
2722                 ret = ath11k_mac_set_peer_vht_fixed_rate(arvif, sta, mask,
2723                                                          band);
2724                 if (ret)
2725                         return ret;
2726         }
2727
2728         /* Re-assoc is run only to update supported rates for given station. It
2729          * doesn't make much sense to reconfigure the peer completely.
2730          */
2731         if (reassoc)
2732                 return 0;
2733
2734         ret = ath11k_setup_peer_smps(ar, arvif, sta->addr,
2735                                      &sta->ht_cap);
2736         if (ret) {
2737                 ath11k_warn(ar->ab, "failed to setup peer SMPS for vdev %d: %d\n",
2738                             arvif->vdev_id, ret);
2739                 return ret;
2740         }
2741
2742         if (!sta->wme) {
2743                 arvif->num_legacy_stations++;
2744                 ret = ath11k_recalc_rtscts_prot(arvif);
2745                 if (ret)
2746                         return ret;
2747         }
2748
2749         if (sta->wme && sta->uapsd_queues) {
2750                 ret = ath11k_peer_assoc_qos_ap(ar, arvif, sta);
2751                 if (ret) {
2752                         ath11k_warn(ar->ab, "failed to set qos params for STA %pM for vdev %i: %d\n",
2753                                     sta->addr, arvif->vdev_id, ret);
2754                         return ret;
2755                 }
2756         }
2757
2758         return 0;
2759 }
2760
2761 static int ath11k_station_disassoc(struct ath11k *ar,
2762                                    struct ieee80211_vif *vif,
2763                                    struct ieee80211_sta *sta)
2764 {
2765         struct ath11k_vif *arvif = (void *)vif->drv_priv;
2766         int ret = 0;
2767
2768         lockdep_assert_held(&ar->conf_mutex);
2769
2770         if (!sta->wme) {
2771                 arvif->num_legacy_stations--;
2772                 ret = ath11k_recalc_rtscts_prot(arvif);
2773                 if (ret)
2774                         return ret;
2775         }
2776
2777         ret = ath11k_clear_peer_keys(arvif, sta->addr);
2778         if (ret) {
2779                 ath11k_warn(ar->ab, "failed to clear all peer keys for vdev %i: %d\n",
2780                             arvif->vdev_id, ret);
2781                 return ret;
2782         }
2783         return 0;
2784 }
2785
2786 static void ath11k_sta_rc_update_wk(struct work_struct *wk)
2787 {
2788         struct ath11k *ar;
2789         struct ath11k_vif *arvif;
2790         struct ath11k_sta *arsta;
2791         struct ieee80211_sta *sta;
2792         struct cfg80211_chan_def def;
2793         enum nl80211_band band;
2794         const u8 *ht_mcs_mask;
2795         const u16 *vht_mcs_mask;
2796         u32 changed, bw, nss, smps;
2797         int err, num_vht_rates;
2798         const struct cfg80211_bitrate_mask *mask;
2799         struct peer_assoc_params peer_arg;
2800
2801         arsta = container_of(wk, struct ath11k_sta, update_wk);
2802         sta = container_of((void *)arsta, struct ieee80211_sta, drv_priv);
2803         arvif = arsta->arvif;
2804         ar = arvif->ar;
2805
2806         if (WARN_ON(ath11k_mac_vif_chan(arvif->vif, &def)))
2807                 return;
2808
2809         band = def.chan->band;
2810         ht_mcs_mask = arvif->bitrate_mask.control[band].ht_mcs;
2811         vht_mcs_mask = arvif->bitrate_mask.control[band].vht_mcs;
2812
2813         spin_lock_bh(&ar->data_lock);
2814
2815         changed = arsta->changed;
2816         arsta->changed = 0;
2817
2818         bw = arsta->bw;
2819         nss = arsta->nss;
2820         smps = arsta->smps;
2821
2822         spin_unlock_bh(&ar->data_lock);
2823
2824         mutex_lock(&ar->conf_mutex);
2825
2826         nss = max_t(u32, 1, nss);
2827         nss = min(nss, max(ath11k_mac_max_ht_nss(ht_mcs_mask),
2828                            ath11k_mac_max_vht_nss(vht_mcs_mask)));
2829
2830         if (changed & IEEE80211_RC_BW_CHANGED) {
2831                 err = ath11k_wmi_set_peer_param(ar, sta->addr, arvif->vdev_id,
2832                                                 WMI_PEER_CHWIDTH, bw);
2833                 if (err)
2834                         ath11k_warn(ar->ab, "failed to update STA %pM peer bw %d: %d\n",
2835                                     sta->addr, bw, err);
2836         }
2837
2838         if (changed & IEEE80211_RC_NSS_CHANGED) {
2839                 ath11k_dbg(ar->ab, ATH11K_DBG_MAC, "mac update sta %pM nss %d\n",
2840                            sta->addr, nss);
2841
2842                 err = ath11k_wmi_set_peer_param(ar, sta->addr, arvif->vdev_id,
2843                                                 WMI_PEER_NSS, nss);
2844                 if (err)
2845                         ath11k_warn(ar->ab, "failed to update STA %pM nss %d: %d\n",
2846                                     sta->addr, nss, err);
2847         }
2848
2849         if (changed & IEEE80211_RC_SMPS_CHANGED) {
2850                 ath11k_dbg(ar->ab, ATH11K_DBG_MAC, "mac update sta %pM smps %d\n",
2851                            sta->addr, smps);
2852
2853                 err = ath11k_wmi_set_peer_param(ar, sta->addr, arvif->vdev_id,
2854                                                 WMI_PEER_MIMO_PS_STATE, smps);
2855                 if (err)
2856                         ath11k_warn(ar->ab, "failed to update STA %pM smps %d: %d\n",
2857                                     sta->addr, smps, err);
2858         }
2859
2860         if (changed & IEEE80211_RC_SUPP_RATES_CHANGED) {
2861                 mask = &arvif->bitrate_mask;
2862                 num_vht_rates = ath11k_mac_bitrate_mask_num_vht_rates(ar, band,
2863                                                                       mask);
2864
2865                 /* Peer_assoc_prepare will reject vht rates in
2866                  * bitrate_mask if its not available in range format and
2867                  * sets vht tx_rateset as unsupported. So multiple VHT MCS
2868                  * setting(eg. MCS 4,5,6) per peer is not supported here.
2869                  * But, Single rate in VHT mask can be set as per-peer
2870                  * fixed rate. But even if any HT rates are configured in
2871                  * the bitrate mask, device will not switch to those rates
2872                  * when per-peer Fixed rate is set.
2873                  * TODO: Check RATEMASK_CMDID to support auto rates selection
2874                  * across HT/VHT and for multiple VHT MCS support.
2875                  */
2876                 if (sta->vht_cap.vht_supported && num_vht_rates == 1) {
2877                         ath11k_mac_set_peer_vht_fixed_rate(arvif, sta, mask,
2878                                                            band);
2879                 } else {
2880                         /* If the peer is non-VHT or no fixed VHT rate
2881                          * is provided in the new bitrate mask we set the
2882                          * other rates using peer_assoc command.
2883                          */
2884                         ath11k_peer_assoc_prepare(ar, arvif->vif, sta,
2885                                                   &peer_arg, true);
2886
2887                         err = ath11k_wmi_send_peer_assoc_cmd(ar, &peer_arg);
2888                         if (err)
2889                                 ath11k_warn(ar->ab, "failed to run peer assoc for STA %pM vdev %i: %d\n",
2890                                             sta->addr, arvif->vdev_id, err);
2891
2892                         if (!wait_for_completion_timeout(&ar->peer_assoc_done, 1 * HZ))
2893                                 ath11k_warn(ar->ab, "failed to get peer assoc conf event for %pM vdev %i\n",
2894                                             sta->addr, arvif->vdev_id);
2895                 }
2896         }
2897
2898         mutex_unlock(&ar->conf_mutex);
2899 }
2900
2901 static int ath11k_mac_inc_num_stations(struct ath11k_vif *arvif,
2902                                        struct ieee80211_sta *sta)
2903 {
2904         struct ath11k *ar = arvif->ar;
2905
2906         lockdep_assert_held(&ar->conf_mutex);
2907
2908         if (arvif->vdev_type == WMI_VDEV_TYPE_STA && !sta->tdls)
2909                 return 0;
2910
2911         if (ar->num_stations >= ar->max_num_stations)
2912                 return -ENOBUFS;
2913
2914         ar->num_stations++;
2915
2916         return 0;
2917 }
2918
2919 static void ath11k_mac_dec_num_stations(struct ath11k_vif *arvif,
2920                                         struct ieee80211_sta *sta)
2921 {
2922         struct ath11k *ar = arvif->ar;
2923
2924         lockdep_assert_held(&ar->conf_mutex);
2925
2926         if (arvif->vdev_type == WMI_VDEV_TYPE_STA && !sta->tdls)
2927                 return;
2928
2929         ar->num_stations--;
2930 }
2931
2932 static int ath11k_mac_station_add(struct ath11k *ar,
2933                                   struct ieee80211_vif *vif,
2934                                   struct ieee80211_sta *sta)
2935 {
2936         struct ath11k_base *ab = ar->ab;
2937         struct ath11k_vif *arvif = ath11k_vif_to_arvif(vif);
2938         struct ath11k_sta *arsta = (struct ath11k_sta *)sta->drv_priv;
2939         struct peer_create_params peer_param;
2940         int ret;
2941
2942         lockdep_assert_held(&ar->conf_mutex);
2943
2944         ret = ath11k_mac_inc_num_stations(arvif, sta);
2945         if (ret) {
2946                 ath11k_warn(ab, "refusing to associate station: too many connected already (%d)\n",
2947                             ar->max_num_stations);
2948                 goto exit;
2949         }
2950
2951         arsta->rx_stats = kzalloc(sizeof(*arsta->rx_stats), GFP_KERNEL);
2952         if (!arsta->rx_stats) {
2953                 ret = -ENOMEM;
2954                 goto dec_num_station;
2955         }
2956
2957         peer_param.vdev_id = arvif->vdev_id;
2958         peer_param.peer_addr = sta->addr;
2959         peer_param.peer_type = WMI_PEER_TYPE_DEFAULT;
2960
2961         ret = ath11k_peer_create(ar, arvif, sta, &peer_param);
2962         if (ret) {
2963                 ath11k_warn(ab, "Failed to add peer: %pM for VDEV: %d\n",
2964                             sta->addr, arvif->vdev_id);
2965                 goto free_rx_stats;
2966         }
2967
2968         ath11k_dbg(ab, ATH11K_DBG_MAC, "Added peer: %pM for VDEV: %d\n",
2969                    sta->addr, arvif->vdev_id);
2970
2971         if (ath11k_debugfs_is_extd_tx_stats_enabled(ar)) {
2972                 arsta->tx_stats = kzalloc(sizeof(*arsta->tx_stats), GFP_KERNEL);
2973                 if (!arsta->tx_stats) {
2974                         ret = -ENOMEM;
2975                         goto free_peer;
2976                 }
2977         }
2978
2979         if (ieee80211_vif_is_mesh(vif)) {
2980                 ret = ath11k_wmi_set_peer_param(ar, sta->addr,
2981                                                 arvif->vdev_id,
2982                                                 WMI_PEER_USE_4ADDR, 1);
2983                 if (ret) {
2984                         ath11k_warn(ab, "failed to STA %pM 4addr capability: %d\n",
2985                                     sta->addr, ret);
2986                         goto free_tx_stats;
2987                 }
2988         }
2989
2990         ret = ath11k_dp_peer_setup(ar, arvif->vdev_id, sta->addr);
2991         if (ret) {
2992                 ath11k_warn(ab, "failed to setup dp for peer %pM on vdev %i (%d)\n",
2993                             sta->addr, arvif->vdev_id, ret);
2994                 goto free_tx_stats;
2995         }
2996
2997         if (ab->hw_params.vdev_start_delay &&
2998             arvif->vdev_type != WMI_VDEV_TYPE_AP) {
2999                 ret = ath11k_start_vdev_delay(ar->hw, vif);
3000                 if (ret) {
3001                         ath11k_warn(ab, "failed to delay vdev start: %d\n", ret);
3002                         goto free_tx_stats;
3003                 }
3004         }
3005
3006         return 0;
3007
3008 free_tx_stats:
3009         kfree(arsta->tx_stats);
3010         arsta->tx_stats = NULL;
3011 free_peer:
3012         ath11k_peer_delete(ar, arvif->vdev_id, sta->addr);
3013 free_rx_stats:
3014         kfree(arsta->rx_stats);
3015         arsta->rx_stats = NULL;
3016 dec_num_station:
3017         ath11k_mac_dec_num_stations(arvif, sta);
3018 exit:
3019         return ret;
3020 }
3021
3022 static int ath11k_mac_op_sta_state(struct ieee80211_hw *hw,
3023                                    struct ieee80211_vif *vif,
3024                                    struct ieee80211_sta *sta,
3025                                    enum ieee80211_sta_state old_state,
3026                                    enum ieee80211_sta_state new_state)
3027 {
3028         struct ath11k *ar = hw->priv;
3029         struct ath11k_vif *arvif = ath11k_vif_to_arvif(vif);
3030         struct ath11k_sta *arsta = (struct ath11k_sta *)sta->drv_priv;
3031         struct ath11k_peer *peer;
3032         int ret = 0;
3033
3034         /* cancel must be done outside the mutex to avoid deadlock */
3035         if ((old_state == IEEE80211_STA_NONE &&
3036              new_state == IEEE80211_STA_NOTEXIST))
3037                 cancel_work_sync(&arsta->update_wk);
3038
3039         mutex_lock(&ar->conf_mutex);
3040
3041         if (old_state == IEEE80211_STA_NOTEXIST &&
3042             new_state == IEEE80211_STA_NONE) {
3043                 memset(arsta, 0, sizeof(*arsta));
3044                 arsta->arvif = arvif;
3045                 INIT_WORK(&arsta->update_wk, ath11k_sta_rc_update_wk);
3046
3047                 ret = ath11k_mac_station_add(ar, vif, sta);
3048                 if (ret)
3049                         ath11k_warn(ar->ab, "Failed to add station: %pM for VDEV: %d\n",
3050                                     sta->addr, arvif->vdev_id);
3051         } else if ((old_state == IEEE80211_STA_NONE &&
3052                     new_state == IEEE80211_STA_NOTEXIST)) {
3053                 ath11k_dp_peer_cleanup(ar, arvif->vdev_id, sta->addr);
3054
3055                 ret = ath11k_peer_delete(ar, arvif->vdev_id, sta->addr);
3056                 if (ret)
3057                         ath11k_warn(ar->ab, "Failed to delete peer: %pM for VDEV: %d\n",
3058                                     sta->addr, arvif->vdev_id);
3059                 else
3060                         ath11k_dbg(ar->ab, ATH11K_DBG_MAC, "Removed peer: %pM for VDEV: %d\n",
3061                                    sta->addr, arvif->vdev_id);
3062
3063                 ath11k_mac_dec_num_stations(arvif, sta);
3064                 spin_lock_bh(&ar->ab->base_lock);
3065                 peer = ath11k_peer_find(ar->ab, arvif->vdev_id, sta->addr);
3066                 if (peer && peer->sta == sta) {
3067                         ath11k_warn(ar->ab, "Found peer entry %pM n vdev %i after it was supposedly removed\n",
3068                                     vif->addr, arvif->vdev_id);
3069                         peer->sta = NULL;
3070                         list_del(&peer->list);
3071                         kfree(peer);
3072                         ar->num_peers--;
3073                 }
3074                 spin_unlock_bh(&ar->ab->base_lock);
3075
3076                 kfree(arsta->tx_stats);
3077                 arsta->tx_stats = NULL;
3078
3079                 kfree(arsta->rx_stats);
3080                 arsta->rx_stats = NULL;
3081         } else if (old_state == IEEE80211_STA_AUTH &&
3082                    new_state == IEEE80211_STA_ASSOC &&
3083                    (vif->type == NL80211_IFTYPE_AP ||
3084                     vif->type == NL80211_IFTYPE_MESH_POINT ||
3085                     vif->type == NL80211_IFTYPE_ADHOC)) {
3086                 ret = ath11k_station_assoc(ar, vif, sta, false);
3087                 if (ret)
3088                         ath11k_warn(ar->ab, "Failed to associate station: %pM\n",
3089                                     sta->addr);
3090         } else if (old_state == IEEE80211_STA_ASSOC &&
3091                    new_state == IEEE80211_STA_AUTH &&
3092                    (vif->type == NL80211_IFTYPE_AP ||
3093                     vif->type == NL80211_IFTYPE_MESH_POINT ||
3094                     vif->type == NL80211_IFTYPE_ADHOC)) {
3095                 ret = ath11k_station_disassoc(ar, vif, sta);
3096                 if (ret)
3097                         ath11k_warn(ar->ab, "Failed to disassociate station: %pM\n",
3098                                     sta->addr);
3099         }
3100
3101         mutex_unlock(&ar->conf_mutex);
3102         return ret;
3103 }
3104
3105 static int ath11k_mac_op_sta_set_txpwr(struct ieee80211_hw *hw,
3106                                        struct ieee80211_vif *vif,
3107                                        struct ieee80211_sta *sta)
3108 {
3109         struct ath11k *ar = hw->priv;
3110         struct ath11k_vif *arvif = (void *)vif->drv_priv;
3111         int ret = 0;
3112         s16 txpwr;
3113
3114         if (sta->txpwr.type == NL80211_TX_POWER_AUTOMATIC) {
3115                 txpwr = 0;
3116         } else {
3117                 txpwr = sta->txpwr.power;
3118                 if (!txpwr)
3119                         return -EINVAL;
3120         }
3121
3122         if (txpwr > ATH11K_TX_POWER_MAX_VAL || txpwr < ATH11K_TX_POWER_MIN_VAL)
3123                 return -EINVAL;
3124
3125         mutex_lock(&ar->conf_mutex);
3126
3127         ret = ath11k_wmi_set_peer_param(ar, sta->addr, arvif->vdev_id,
3128                                         WMI_PEER_USE_FIXED_PWR, txpwr);
3129         if (ret) {
3130                 ath11k_warn(ar->ab, "failed to set tx power for station ret: %d\n",
3131                             ret);
3132                 goto out;
3133         }
3134
3135 out:
3136         mutex_unlock(&ar->conf_mutex);
3137         return ret;
3138 }
3139
3140 static void ath11k_mac_op_sta_rc_update(struct ieee80211_hw *hw,
3141                                         struct ieee80211_vif *vif,
3142                                         struct ieee80211_sta *sta,
3143                                         u32 changed)
3144 {
3145         struct ath11k *ar = hw->priv;
3146         struct ath11k_sta *arsta = (struct ath11k_sta *)sta->drv_priv;
3147         struct ath11k_vif *arvif = (void *)vif->drv_priv;
3148         struct ath11k_peer *peer;
3149         u32 bw, smps;
3150
3151         spin_lock_bh(&ar->ab->base_lock);
3152
3153         peer = ath11k_peer_find(ar->ab, arvif->vdev_id, sta->addr);
3154         if (!peer) {
3155                 spin_unlock_bh(&ar->ab->base_lock);
3156                 ath11k_warn(ar->ab, "mac sta rc update failed to find peer %pM on vdev %i\n",
3157                             sta->addr, arvif->vdev_id);
3158                 return;
3159         }
3160
3161         spin_unlock_bh(&ar->ab->base_lock);
3162
3163         ath11k_dbg(ar->ab, ATH11K_DBG_MAC,
3164                    "mac sta rc update for %pM changed %08x bw %d nss %d smps %d\n",
3165                    sta->addr, changed, sta->bandwidth, sta->rx_nss,
3166                    sta->smps_mode);
3167
3168         spin_lock_bh(&ar->data_lock);
3169
3170         if (changed & IEEE80211_RC_BW_CHANGED) {
3171                 bw = WMI_PEER_CHWIDTH_20MHZ;
3172
3173                 switch (sta->bandwidth) {
3174                 case IEEE80211_STA_RX_BW_20:
3175                         bw = WMI_PEER_CHWIDTH_20MHZ;
3176                         break;
3177                 case IEEE80211_STA_RX_BW_40:
3178                         bw = WMI_PEER_CHWIDTH_40MHZ;
3179                         break;
3180                 case IEEE80211_STA_RX_BW_80:
3181                         bw = WMI_PEER_CHWIDTH_80MHZ;
3182                         break;
3183                 case IEEE80211_STA_RX_BW_160:
3184                         bw = WMI_PEER_CHWIDTH_160MHZ;
3185                         break;
3186                 default:
3187                         ath11k_warn(ar->ab, "Invalid bandwidth %d in rc update for %pM\n",
3188                                     sta->bandwidth, sta->addr);
3189                         bw = WMI_PEER_CHWIDTH_20MHZ;
3190                         break;
3191                 }
3192
3193                 arsta->bw = bw;
3194         }
3195
3196         if (changed & IEEE80211_RC_NSS_CHANGED)
3197                 arsta->nss = sta->rx_nss;
3198
3199         if (changed & IEEE80211_RC_SMPS_CHANGED) {
3200                 smps = WMI_PEER_SMPS_PS_NONE;
3201
3202                 switch (sta->smps_mode) {
3203                 case IEEE80211_SMPS_AUTOMATIC:
3204                 case IEEE80211_SMPS_OFF:
3205                         smps = WMI_PEER_SMPS_PS_NONE;
3206                         break;
3207                 case IEEE80211_SMPS_STATIC:
3208                         smps = WMI_PEER_SMPS_STATIC;
3209                         break;
3210                 case IEEE80211_SMPS_DYNAMIC:
3211                         smps = WMI_PEER_SMPS_DYNAMIC;
3212                         break;
3213                 default:
3214                         ath11k_warn(ar->ab, "Invalid smps %d in sta rc update for %pM\n",
3215                                     sta->smps_mode, sta->addr);
3216                         smps = WMI_PEER_SMPS_PS_NONE;
3217                         break;
3218                 }
3219
3220                 arsta->smps = smps;
3221         }
3222
3223         arsta->changed |= changed;
3224
3225         spin_unlock_bh(&ar->data_lock);
3226
3227         ieee80211_queue_work(hw, &arsta->update_wk);
3228 }
3229
3230 static int ath11k_conf_tx_uapsd(struct ath11k *ar, struct ieee80211_vif *vif,
3231                                 u16 ac, bool enable)
3232 {
3233         struct ath11k_vif *arvif = ath11k_vif_to_arvif(vif);
3234         u32 value = 0;
3235         int ret = 0;
3236
3237         if (arvif->vdev_type != WMI_VDEV_TYPE_STA)
3238                 return 0;
3239
3240         switch (ac) {
3241         case IEEE80211_AC_VO:
3242                 value = WMI_STA_PS_UAPSD_AC3_DELIVERY_EN |
3243                         WMI_STA_PS_UAPSD_AC3_TRIGGER_EN;
3244                 break;
3245         case IEEE80211_AC_VI:
3246                 value = WMI_STA_PS_UAPSD_AC2_DELIVERY_EN |
3247                         WMI_STA_PS_UAPSD_AC2_TRIGGER_EN;
3248                 break;
3249         case IEEE80211_AC_BE:
3250                 value = WMI_STA_PS_UAPSD_AC1_DELIVERY_EN |
3251                         WMI_STA_PS_UAPSD_AC1_TRIGGER_EN;
3252                 break;
3253         case IEEE80211_AC_BK:
3254                 value = WMI_STA_PS_UAPSD_AC0_DELIVERY_EN |
3255                         WMI_STA_PS_UAPSD_AC0_TRIGGER_EN;
3256                 break;
3257         }
3258
3259         if (enable)
3260                 arvif->u.sta.uapsd |= value;
3261         else
3262                 arvif->u.sta.uapsd &= ~value;
3263
3264         ret = ath11k_wmi_set_sta_ps_param(ar, arvif->vdev_id,
3265                                           WMI_STA_PS_PARAM_UAPSD,
3266                                           arvif->u.sta.uapsd);
3267         if (ret) {
3268                 ath11k_warn(ar->ab, "could not set uapsd params %d\n", ret);
3269                 goto exit;
3270         }
3271
3272         if (arvif->u.sta.uapsd)
3273                 value = WMI_STA_PS_RX_WAKE_POLICY_POLL_UAPSD;
3274         else
3275                 value = WMI_STA_PS_RX_WAKE_POLICY_WAKE;
3276
3277         ret = ath11k_wmi_set_sta_ps_param(ar, arvif->vdev_id,
3278                                           WMI_STA_PS_PARAM_RX_WAKE_POLICY,
3279                                           value);
3280         if (ret)
3281                 ath11k_warn(ar->ab, "could not set rx wake param %d\n", ret);
3282
3283 exit:
3284         return ret;
3285 }
3286
3287 static int ath11k_mac_op_conf_tx(struct ieee80211_hw *hw,
3288                                  struct ieee80211_vif *vif, u16 ac,
3289                                  const struct ieee80211_tx_queue_params *params)
3290 {
3291         struct ath11k *ar = hw->priv;
3292         struct ath11k_vif *arvif = (void *)vif->drv_priv;
3293         struct wmi_wmm_params_arg *p = NULL;
3294         int ret;
3295
3296         mutex_lock(&ar->conf_mutex);
3297
3298         switch (ac) {
3299         case IEEE80211_AC_VO:
3300                 p = &arvif->wmm_params.ac_vo;
3301                 break;
3302         case IEEE80211_AC_VI:
3303                 p = &arvif->wmm_params.ac_vi;
3304                 break;
3305         case IEEE80211_AC_BE:
3306                 p = &arvif->wmm_params.ac_be;
3307                 break;
3308         case IEEE80211_AC_BK:
3309                 p = &arvif->wmm_params.ac_bk;
3310                 break;
3311         }
3312
3313         if (WARN_ON(!p)) {
3314                 ret = -EINVAL;
3315                 goto exit;
3316         }
3317
3318         p->cwmin = params->cw_min;
3319         p->cwmax = params->cw_max;
3320         p->aifs = params->aifs;
3321         p->txop = params->txop;
3322
3323         ret = ath11k_wmi_send_wmm_update_cmd_tlv(ar, arvif->vdev_id,
3324                                                  &arvif->wmm_params);
3325         if (ret) {
3326                 ath11k_warn(ar->ab, "failed to set wmm params: %d\n", ret);
3327                 goto exit;
3328         }
3329
3330         ret = ath11k_conf_tx_uapsd(ar, vif, ac, params->uapsd);
3331
3332         if (ret)
3333                 ath11k_warn(ar->ab, "failed to set sta uapsd: %d\n", ret);
3334
3335 exit:
3336         mutex_unlock(&ar->conf_mutex);
3337         return ret;
3338 }
3339
3340 static struct ieee80211_sta_ht_cap
3341 ath11k_create_ht_cap(struct ath11k *ar, u32 ar_ht_cap, u32 rate_cap_rx_chainmask)
3342 {
3343         int i;
3344         struct ieee80211_sta_ht_cap ht_cap = {0};
3345         u32 ar_vht_cap = ar->pdev->cap.vht_cap;
3346
3347         if (!(ar_ht_cap & WMI_HT_CAP_ENABLED))
3348                 return ht_cap;
3349
3350         ht_cap.ht_supported = 1;
3351         ht_cap.ampdu_factor = IEEE80211_HT_MAX_AMPDU_64K;
3352         ht_cap.ampdu_density = IEEE80211_HT_MPDU_DENSITY_NONE;
3353         ht_cap.cap |= IEEE80211_HT_CAP_SUP_WIDTH_20_40;
3354         ht_cap.cap |= IEEE80211_HT_CAP_DSSSCCK40;
3355         ht_cap.cap |= WLAN_HT_CAP_SM_PS_STATIC << IEEE80211_HT_CAP_SM_PS_SHIFT;
3356
3357         if (ar_ht_cap & WMI_HT_CAP_HT20_SGI)
3358                 ht_cap.cap |= IEEE80211_HT_CAP_SGI_20;
3359
3360         if (ar_ht_cap & WMI_HT_CAP_HT40_SGI)
3361                 ht_cap.cap |= IEEE80211_HT_CAP_SGI_40;
3362
3363         if (ar_ht_cap & WMI_HT_CAP_DYNAMIC_SMPS) {
3364                 u32 smps;
3365
3366                 smps   = WLAN_HT_CAP_SM_PS_DYNAMIC;
3367                 smps <<= IEEE80211_HT_CAP_SM_PS_SHIFT;
3368
3369                 ht_cap.cap |= smps;
3370         }
3371
3372         if (ar_ht_cap & WMI_HT_CAP_TX_STBC)
3373                 ht_cap.cap |= IEEE80211_HT_CAP_TX_STBC;
3374
3375         if (ar_ht_cap & WMI_HT_CAP_RX_STBC) {
3376                 u32 stbc;
3377
3378                 stbc   = ar_ht_cap;
3379                 stbc  &= WMI_HT_CAP_RX_STBC;
3380                 stbc >>= WMI_HT_CAP_RX_STBC_MASK_SHIFT;
3381                 stbc <<= IEEE80211_HT_CAP_RX_STBC_SHIFT;
3382                 stbc  &= IEEE80211_HT_CAP_RX_STBC;
3383
3384                 ht_cap.cap |= stbc;
3385         }
3386
3387         if (ar_ht_cap & WMI_HT_CAP_RX_LDPC)
3388                 ht_cap.cap |= IEEE80211_HT_CAP_LDPC_CODING;
3389
3390         if (ar_ht_cap & WMI_HT_CAP_L_SIG_TXOP_PROT)
3391                 ht_cap.cap |= IEEE80211_HT_CAP_LSIG_TXOP_PROT;
3392
3393         if (ar_vht_cap & WMI_VHT_CAP_MAX_MPDU_LEN_MASK)
3394                 ht_cap.cap |= IEEE80211_HT_CAP_MAX_AMSDU;
3395
3396         for (i = 0; i < ar->num_rx_chains; i++) {
3397                 if (rate_cap_rx_chainmask & BIT(i))
3398                         ht_cap.mcs.rx_mask[i] = 0xFF;
3399         }
3400
3401         ht_cap.mcs.tx_params |= IEEE80211_HT_MCS_TX_DEFINED;
3402
3403         return ht_cap;
3404 }
3405
3406 static int ath11k_mac_set_txbf_conf(struct ath11k_vif *arvif)
3407 {
3408         u32 value = 0;
3409         struct ath11k *ar = arvif->ar;
3410         int nsts;
3411         int sound_dim;
3412         u32 vht_cap = ar->pdev->cap.vht_cap;
3413         u32 vdev_param = WMI_VDEV_PARAM_TXBF;
3414
3415         if (vht_cap & (IEEE80211_VHT_CAP_SU_BEAMFORMEE_CAPABLE)) {
3416                 nsts = vht_cap & IEEE80211_VHT_CAP_BEAMFORMEE_STS_MASK;
3417                 nsts >>= IEEE80211_VHT_CAP_BEAMFORMEE_STS_SHIFT;
3418                 value |= SM(nsts, WMI_TXBF_STS_CAP_OFFSET);
3419         }
3420
3421         if (vht_cap & (IEEE80211_VHT_CAP_SU_BEAMFORMER_CAPABLE)) {
3422                 sound_dim = vht_cap &
3423                             IEEE80211_VHT_CAP_SOUNDING_DIMENSIONS_MASK;
3424                 sound_dim >>= IEEE80211_VHT_CAP_SOUNDING_DIMENSIONS_SHIFT;
3425                 if (sound_dim > (ar->num_tx_chains - 1))
3426                         sound_dim = ar->num_tx_chains - 1;
3427                 value |= SM(sound_dim, WMI_BF_SOUND_DIM_OFFSET);
3428         }
3429
3430         if (!value)
3431                 return 0;
3432
3433         if (vht_cap & IEEE80211_VHT_CAP_SU_BEAMFORMER_CAPABLE) {
3434                 value |= WMI_VDEV_PARAM_TXBF_SU_TX_BFER;
3435
3436                 if ((vht_cap & IEEE80211_VHT_CAP_MU_BEAMFORMER_CAPABLE) &&
3437                     arvif->vdev_type == WMI_VDEV_TYPE_AP)
3438                         value |= WMI_VDEV_PARAM_TXBF_MU_TX_BFER;
3439         }
3440
3441         /* TODO: SUBFEE not validated in HK, disable here until validated? */
3442
3443         if (vht_cap & IEEE80211_VHT_CAP_SU_BEAMFORMEE_CAPABLE) {
3444                 value |= WMI_VDEV_PARAM_TXBF_SU_TX_BFEE;
3445
3446                 if ((vht_cap & IEEE80211_VHT_CAP_MU_BEAMFORMEE_CAPABLE) &&
3447                     arvif->vdev_type == WMI_VDEV_TYPE_STA)
3448                         value |= WMI_VDEV_PARAM_TXBF_MU_TX_BFEE;
3449         }
3450
3451         return ath11k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
3452                                              vdev_param, value);
3453 }
3454
3455 static void ath11k_set_vht_txbf_cap(struct ath11k *ar, u32 *vht_cap)
3456 {
3457         bool subfer, subfee;
3458         int sound_dim = 0;
3459
3460         subfer = !!(*vht_cap & (IEEE80211_VHT_CAP_SU_BEAMFORMER_CAPABLE));
3461         subfee = !!(*vht_cap & (IEEE80211_VHT_CAP_SU_BEAMFORMEE_CAPABLE));
3462
3463         if (ar->num_tx_chains < 2) {
3464                 *vht_cap &= ~(IEEE80211_VHT_CAP_SU_BEAMFORMER_CAPABLE);
3465                 subfer = false;
3466         }
3467
3468         /* If SU Beaformer is not set, then disable MU Beamformer Capability */
3469         if (!subfer)
3470                 *vht_cap &= ~(IEEE80211_VHT_CAP_MU_BEAMFORMER_CAPABLE);
3471
3472         /* If SU Beaformee is not set, then disable MU Beamformee Capability */
3473         if (!subfee)
3474                 *vht_cap &= ~(IEEE80211_VHT_CAP_MU_BEAMFORMEE_CAPABLE);
3475
3476         sound_dim = (*vht_cap & IEEE80211_VHT_CAP_SOUNDING_DIMENSIONS_MASK);
3477         sound_dim >>= IEEE80211_VHT_CAP_SOUNDING_DIMENSIONS_SHIFT;
3478         *vht_cap &= ~IEEE80211_VHT_CAP_SOUNDING_DIMENSIONS_MASK;
3479
3480         /* TODO: Need to check invalid STS and Sound_dim values set by FW? */
3481
3482         /* Enable Sounding Dimension Field only if SU BF is enabled */
3483         if (subfer) {
3484                 if (sound_dim > (ar->num_tx_chains - 1))
3485                         sound_dim = ar->num_tx_chains - 1;
3486
3487                 sound_dim <<= IEEE80211_VHT_CAP_SOUNDING_DIMENSIONS_SHIFT;
3488                 sound_dim &=  IEEE80211_VHT_CAP_SOUNDING_DIMENSIONS_MASK;
3489                 *vht_cap |= sound_dim;
3490         }
3491
3492         /* Use the STS advertised by FW unless SU Beamformee is not supported*/
3493         if (!subfee)
3494                 *vht_cap &= ~(IEEE80211_VHT_CAP_BEAMFORMEE_STS_MASK);
3495 }
3496
3497 static struct ieee80211_sta_vht_cap
3498 ath11k_create_vht_cap(struct ath11k *ar, u32 rate_cap_tx_chainmask,
3499                       u32 rate_cap_rx_chainmask)
3500 {
3501         struct ieee80211_sta_vht_cap vht_cap = {0};
3502         u16 txmcs_map, rxmcs_map;
3503         int i;
3504
3505         vht_cap.vht_supported = 1;
3506         vht_cap.cap = ar->pdev->cap.vht_cap;
3507
3508         ath11k_set_vht_txbf_cap(ar, &vht_cap.cap);
3509
3510         /* TODO: Enable back VHT160 mode once association issues are fixed */
3511         /* Disabling VHT160 and VHT80+80 modes */
3512         vht_cap.cap &= ~IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_MASK;
3513         vht_cap.cap &= ~IEEE80211_VHT_CAP_SHORT_GI_160;
3514
3515         rxmcs_map = 0;
3516         txmcs_map = 0;
3517         for (i = 0; i < 8; i++) {
3518                 if (i < ar->num_tx_chains && rate_cap_tx_chainmask & BIT(i))
3519                         txmcs_map |= IEEE80211_VHT_MCS_SUPPORT_0_9 << (i * 2);
3520                 else
3521                         txmcs_map |= IEEE80211_VHT_MCS_NOT_SUPPORTED << (i * 2);
3522
3523                 if (i < ar->num_rx_chains && rate_cap_rx_chainmask & BIT(i))
3524                         rxmcs_map |= IEEE80211_VHT_MCS_SUPPORT_0_9 << (i * 2);
3525                 else
3526                         rxmcs_map |= IEEE80211_VHT_MCS_NOT_SUPPORTED << (i * 2);
3527         }
3528
3529         if (rate_cap_tx_chainmask <= 1)
3530                 vht_cap.cap &= ~IEEE80211_VHT_CAP_TXSTBC;
3531
3532         vht_cap.vht_mcs.rx_mcs_map = cpu_to_le16(rxmcs_map);
3533         vht_cap.vht_mcs.tx_mcs_map = cpu_to_le16(txmcs_map);
3534
3535         return vht_cap;
3536 }
3537
3538 static void ath11k_mac_setup_ht_vht_cap(struct ath11k *ar,
3539                                         struct ath11k_pdev_cap *cap,
3540                                         u32 *ht_cap_info)
3541 {
3542         struct ieee80211_supported_band *band;
3543         u32 rate_cap_tx_chainmask;
3544         u32 rate_cap_rx_chainmask;
3545         u32 ht_cap;
3546
3547         rate_cap_tx_chainmask = ar->cfg_tx_chainmask >> cap->tx_chain_mask_shift;
3548         rate_cap_rx_chainmask = ar->cfg_rx_chainmask >> cap->rx_chain_mask_shift;
3549
3550         if (cap->supported_bands & WMI_HOST_WLAN_2G_CAP) {
3551                 band = &ar->mac.sbands[NL80211_BAND_2GHZ];
3552                 ht_cap = cap->band[NL80211_BAND_2GHZ].ht_cap_info;
3553                 if (ht_cap_info)
3554                         *ht_cap_info = ht_cap;
3555                 band->ht_cap = ath11k_create_ht_cap(ar, ht_cap,
3556                                                     rate_cap_rx_chainmask);
3557         }
3558
3559         if (cap->supported_bands & WMI_HOST_WLAN_5G_CAP && !ar->supports_6ghz) {
3560                 band = &ar->mac.sbands[NL80211_BAND_5GHZ];
3561                 ht_cap = cap->band[NL80211_BAND_5GHZ].ht_cap_info;
3562                 if (ht_cap_info)
3563                         *ht_cap_info = ht_cap;
3564                 band->ht_cap = ath11k_create_ht_cap(ar, ht_cap,
3565                                                     rate_cap_rx_chainmask);
3566                 band->vht_cap = ath11k_create_vht_cap(ar, rate_cap_tx_chainmask,
3567                                                       rate_cap_rx_chainmask);
3568         }
3569 }
3570
3571 static int ath11k_check_chain_mask(struct ath11k *ar, u32 ant, bool is_tx_ant)
3572 {
3573         /* TODO: Check the request chainmask against the supported
3574          * chainmask table which is advertised in extented_service_ready event
3575          */
3576
3577         return 0;
3578 }
3579
3580 static void ath11k_gen_ppe_thresh(struct ath11k_ppe_threshold *fw_ppet,
3581                                   u8 *he_ppet)
3582 {
3583         int nss, ru;
3584         u8 bit = 7;
3585
3586         he_ppet[0] = fw_ppet->numss_m1 & IEEE80211_PPE_THRES_NSS_MASK;
3587         he_ppet[0] |= (fw_ppet->ru_bit_mask <<
3588                        IEEE80211_PPE_THRES_RU_INDEX_BITMASK_POS) &
3589                       IEEE80211_PPE_THRES_RU_INDEX_BITMASK_MASK;
3590         for (nss = 0; nss <= fw_ppet->numss_m1; nss++) {
3591                 for (ru = 0; ru < 4; ru++) {
3592                         u8 val;
3593                         int i;
3594
3595                         if ((fw_ppet->ru_bit_mask & BIT(ru)) == 0)
3596                                 continue;
3597                         val = (fw_ppet->ppet16_ppet8_ru3_ru0[nss] >> (ru * 6)) &
3598                                0x3f;
3599                         val = ((val >> 3) & 0x7) | ((val & 0x7) << 3);
3600                         for (i = 5; i >= 0; i--) {
3601                                 he_ppet[bit / 8] |=
3602                                         ((val >> i) & 0x1) << ((bit % 8));
3603                                 bit++;
3604                         }
3605                 }
3606         }
3607 }
3608
3609 static void
3610 ath11k_mac_filter_he_cap_mesh(struct ieee80211_he_cap_elem *he_cap_elem)
3611 {
3612         u8 m;
3613
3614         m = IEEE80211_HE_MAC_CAP0_TWT_RES |
3615             IEEE80211_HE_MAC_CAP0_TWT_REQ;
3616         he_cap_elem->mac_cap_info[0] &= ~m;
3617
3618         m = IEEE80211_HE_MAC_CAP2_TRS |
3619             IEEE80211_HE_MAC_CAP2_BCAST_TWT |
3620             IEEE80211_HE_MAC_CAP2_MU_CASCADING;
3621         he_cap_elem->mac_cap_info[2] &= ~m;
3622
3623         m = IEEE80211_HE_MAC_CAP3_FLEX_TWT_SCHED |
3624             IEEE80211_HE_MAC_CAP2_BCAST_TWT |
3625             IEEE80211_HE_MAC_CAP2_MU_CASCADING;
3626         he_cap_elem->mac_cap_info[3] &= ~m;
3627
3628         m = IEEE80211_HE_MAC_CAP4_BSRP_BQRP_A_MPDU_AGG |
3629             IEEE80211_HE_MAC_CAP4_BQR;
3630         he_cap_elem->mac_cap_info[4] &= ~m;
3631
3632         m = IEEE80211_HE_MAC_CAP5_SUBCHAN_SELECVITE_TRANSMISSION |
3633             IEEE80211_HE_MAC_CAP5_UL_2x996_TONE_RU |
3634             IEEE80211_HE_MAC_CAP5_PUNCTURED_SOUNDING |
3635             IEEE80211_HE_MAC_CAP5_HT_VHT_TRIG_FRAME_RX;
3636         he_cap_elem->mac_cap_info[5] &= ~m;
3637
3638         m = IEEE80211_HE_PHY_CAP2_UL_MU_FULL_MU_MIMO |
3639             IEEE80211_HE_PHY_CAP2_UL_MU_PARTIAL_MU_MIMO;
3640         he_cap_elem->phy_cap_info[2] &= ~m;
3641
3642         m = IEEE80211_HE_PHY_CAP3_RX_HE_MU_PPDU_FROM_NON_AP_STA |
3643             IEEE80211_HE_PHY_CAP3_DCM_MAX_CONST_TX_MASK |
3644             IEEE80211_HE_PHY_CAP3_DCM_MAX_CONST_RX_MASK;
3645         he_cap_elem->phy_cap_info[3] &= ~m;
3646
3647         m = IEEE80211_HE_PHY_CAP4_MU_BEAMFORMER;
3648         he_cap_elem->phy_cap_info[4] &= ~m;
3649
3650         m = IEEE80211_HE_PHY_CAP5_NG16_MU_FEEDBACK;
3651         he_cap_elem->phy_cap_info[5] &= ~m;
3652
3653         m = IEEE80211_HE_PHY_CAP6_CODEBOOK_SIZE_75_MU |
3654             IEEE80211_HE_PHY_CAP6_TRIG_MU_BEAMFORMER_FB |
3655             IEEE80211_HE_PHY_CAP6_TRIG_CQI_FB |
3656             IEEE80211_HE_PHY_CAP6_PARTIAL_BANDWIDTH_DL_MUMIMO;
3657         he_cap_elem->phy_cap_info[6] &= ~m;
3658
3659         m = IEEE80211_HE_PHY_CAP7_SRP_BASED_SR |
3660             IEEE80211_HE_PHY_CAP7_POWER_BOOST_FACTOR_AR |
3661             IEEE80211_HE_PHY_CAP7_STBC_TX_ABOVE_80MHZ |
3662             IEEE80211_HE_PHY_CAP7_STBC_RX_ABOVE_80MHZ;
3663         he_cap_elem->phy_cap_info[7] &= ~m;
3664
3665         m = IEEE80211_HE_PHY_CAP8_HE_ER_SU_PPDU_4XLTF_AND_08_US_GI |
3666             IEEE80211_HE_PHY_CAP8_20MHZ_IN_40MHZ_HE_PPDU_IN_2G |
3667             IEEE80211_HE_PHY_CAP8_20MHZ_IN_160MHZ_HE_PPDU |
3668             IEEE80211_HE_PHY_CAP8_80MHZ_IN_160MHZ_HE_PPDU;
3669         he_cap_elem->phy_cap_info[8] &= ~m;
3670
3671         m = IEEE80211_HE_PHY_CAP9_LONGER_THAN_16_SIGB_OFDM_SYM |
3672             IEEE80211_HE_PHY_CAP9_NON_TRIGGERED_CQI_FEEDBACK |
3673             IEEE80211_HE_PHY_CAP9_RX_1024_QAM_LESS_THAN_242_TONE_RU |
3674             IEEE80211_HE_PHY_CAP9_TX_1024_QAM_LESS_THAN_242_TONE_RU |
3675             IEEE80211_HE_PHY_CAP9_RX_FULL_BW_SU_USING_MU_WITH_COMP_SIGB |
3676             IEEE80211_HE_PHY_CAP9_RX_FULL_BW_SU_USING_MU_WITH_NON_COMP_SIGB;
3677         he_cap_elem->phy_cap_info[9] &= ~m;
3678 }
3679
3680 static __le16 ath11k_mac_setup_he_6ghz_cap(struct ath11k_pdev_cap *pcap,
3681                                            struct ath11k_band_cap *bcap)
3682 {
3683         u8 val;
3684
3685         bcap->he_6ghz_capa = IEEE80211_HT_MPDU_DENSITY_NONE;
3686         if (bcap->ht_cap_info & WMI_HT_CAP_DYNAMIC_SMPS)
3687                 bcap->he_6ghz_capa |=
3688                         FIELD_PREP(IEEE80211_HE_6GHZ_CAP_SM_PS,
3689                                    WLAN_HT_CAP_SM_PS_DYNAMIC);
3690         else
3691                 bcap->he_6ghz_capa |=
3692                         FIELD_PREP(IEEE80211_HE_6GHZ_CAP_SM_PS,
3693                                    WLAN_HT_CAP_SM_PS_DISABLED);
3694         val = FIELD_GET(IEEE80211_VHT_CAP_MAX_A_MPDU_LENGTH_EXPONENT_MASK,
3695                         pcap->vht_cap);
3696         bcap->he_6ghz_capa |=
3697                 FIELD_PREP(IEEE80211_HE_6GHZ_CAP_MAX_AMPDU_LEN_EXP, val);
3698         val = FIELD_GET(IEEE80211_VHT_CAP_MAX_MPDU_MASK, pcap->vht_cap);
3699         bcap->he_6ghz_capa |=
3700                 FIELD_PREP(IEEE80211_HE_6GHZ_CAP_MAX_MPDU_LEN, val);
3701         if (pcap->vht_cap & IEEE80211_VHT_CAP_RX_ANTENNA_PATTERN)
3702                 bcap->he_6ghz_capa |= IEEE80211_HE_6GHZ_CAP_RX_ANTPAT_CONS;
3703         if (pcap->vht_cap & IEEE80211_VHT_CAP_TX_ANTENNA_PATTERN)
3704                 bcap->he_6ghz_capa |= IEEE80211_HE_6GHZ_CAP_TX_ANTPAT_CONS;
3705
3706         return cpu_to_le16(bcap->he_6ghz_capa);
3707 }
3708
3709 static int ath11k_mac_copy_he_cap(struct ath11k *ar,
3710                                   struct ath11k_pdev_cap *cap,
3711                                   struct ieee80211_sband_iftype_data *data,
3712                                   int band)
3713 {
3714         int i, idx = 0;
3715
3716         for (i = 0; i < NUM_NL80211_IFTYPES; i++) {
3717                 struct ieee80211_sta_he_cap *he_cap = &data[idx].he_cap;
3718                 struct ath11k_band_cap *band_cap = &cap->band[band];
3719                 struct ieee80211_he_cap_elem *he_cap_elem =
3720                                 &he_cap->he_cap_elem;
3721
3722                 switch (i) {
3723                 case NL80211_IFTYPE_STATION:
3724                 case NL80211_IFTYPE_AP:
3725                 case NL80211_IFTYPE_MESH_POINT:
3726                         break;
3727
3728                 default:
3729                         continue;
3730                 }
3731
3732                 data[idx].types_mask = BIT(i);
3733                 he_cap->has_he = true;
3734                 memcpy(he_cap_elem->mac_cap_info, band_cap->he_cap_info,
3735                        sizeof(he_cap_elem->mac_cap_info));
3736                 memcpy(he_cap_elem->phy_cap_info, band_cap->he_cap_phy_info,
3737                        sizeof(he_cap_elem->phy_cap_info));
3738
3739                 he_cap_elem->mac_cap_info[1] &=
3740                         IEEE80211_HE_MAC_CAP1_TF_MAC_PAD_DUR_MASK;
3741                 he_cap_elem->phy_cap_info[4] &=
3742                         ~IEEE80211_HE_PHY_CAP4_BEAMFORMEE_MAX_STS_UNDER_80MHZ_MASK;
3743                 he_cap_elem->phy_cap_info[4] &=
3744                         ~IEEE80211_HE_PHY_CAP4_BEAMFORMEE_MAX_STS_ABOVE_80MHZ_MASK;
3745                 he_cap_elem->phy_cap_info[4] |= (ar->num_tx_chains - 1) << 2;
3746
3747                 he_cap_elem->phy_cap_info[5] &=
3748                         ~IEEE80211_HE_PHY_CAP5_BEAMFORMEE_NUM_SND_DIM_UNDER_80MHZ_MASK;
3749                 he_cap_elem->phy_cap_info[5] &=
3750                         ~IEEE80211_HE_PHY_CAP5_BEAMFORMEE_NUM_SND_DIM_ABOVE_80MHZ_MASK;
3751                 he_cap_elem->phy_cap_info[5] |= ar->num_tx_chains - 1;
3752
3753                 switch (i) {
3754                 case NL80211_IFTYPE_AP:
3755                         he_cap_elem->phy_cap_info[3] &=
3756                                 ~IEEE80211_HE_PHY_CAP3_DCM_MAX_CONST_TX_MASK;
3757                         he_cap_elem->phy_cap_info[9] |=
3758                                 IEEE80211_HE_PHY_CAP9_RX_1024_QAM_LESS_THAN_242_TONE_RU;
3759                         break;
3760                 case NL80211_IFTYPE_STATION:
3761                         he_cap_elem->mac_cap_info[0] &=
3762                                 ~IEEE80211_HE_MAC_CAP0_TWT_RES;
3763                         he_cap_elem->mac_cap_info[0] |=
3764                                 IEEE80211_HE_MAC_CAP0_TWT_REQ;
3765                         he_cap_elem->phy_cap_info[9] |=
3766                                 IEEE80211_HE_PHY_CAP9_TX_1024_QAM_LESS_THAN_242_TONE_RU;
3767                         break;
3768                 case NL80211_IFTYPE_MESH_POINT:
3769                         ath11k_mac_filter_he_cap_mesh(he_cap_elem);
3770                         break;
3771                 }
3772
3773                 he_cap->he_mcs_nss_supp.rx_mcs_80 =
3774                         cpu_to_le16(band_cap->he_mcs & 0xffff);
3775                 he_cap->he_mcs_nss_supp.tx_mcs_80 =
3776                         cpu_to_le16(band_cap->he_mcs & 0xffff);
3777                 he_cap->he_mcs_nss_supp.rx_mcs_160 =
3778                         cpu_to_le16((band_cap->he_mcs >> 16) & 0xffff);
3779                 he_cap->he_mcs_nss_supp.tx_mcs_160 =
3780                         cpu_to_le16((band_cap->he_mcs >> 16) & 0xffff);
3781                 he_cap->he_mcs_nss_supp.rx_mcs_80p80 =
3782                         cpu_to_le16((band_cap->he_mcs >> 16) & 0xffff);
3783                 he_cap->he_mcs_nss_supp.tx_mcs_80p80 =
3784                         cpu_to_le16((band_cap->he_mcs >> 16) & 0xffff);
3785
3786                 memset(he_cap->ppe_thres, 0, sizeof(he_cap->ppe_thres));
3787                 if (he_cap_elem->phy_cap_info[6] &
3788                     IEEE80211_HE_PHY_CAP6_PPE_THRESHOLD_PRESENT)
3789                         ath11k_gen_ppe_thresh(&band_cap->he_ppet,
3790                                               he_cap->ppe_thres);
3791
3792                 if (band == NL80211_BAND_6GHZ) {
3793                         data[idx].he_6ghz_capa.capa =
3794                                 ath11k_mac_setup_he_6ghz_cap(cap, band_cap);
3795                 }
3796                 idx++;
3797         }
3798
3799         return idx;
3800 }
3801
3802 static void ath11k_mac_setup_he_cap(struct ath11k *ar,
3803                                     struct ath11k_pdev_cap *cap)
3804 {
3805         struct ieee80211_supported_band *band;
3806         int count;
3807
3808         if (cap->supported_bands & WMI_HOST_WLAN_2G_CAP) {
3809                 count = ath11k_mac_copy_he_cap(ar, cap,
3810                                                ar->mac.iftype[NL80211_BAND_2GHZ],
3811                                                NL80211_BAND_2GHZ);
3812                 band = &ar->mac.sbands[NL80211_BAND_2GHZ];
3813                 band->iftype_data = ar->mac.iftype[NL80211_BAND_2GHZ];
3814                 band->n_iftype_data = count;
3815         }
3816
3817         if (cap->supported_bands & WMI_HOST_WLAN_5G_CAP) {
3818                 count = ath11k_mac_copy_he_cap(ar, cap,
3819                                                ar->mac.iftype[NL80211_BAND_5GHZ],
3820                                                NL80211_BAND_5GHZ);
3821                 band = &ar->mac.sbands[NL80211_BAND_5GHZ];
3822                 band->iftype_data = ar->mac.iftype[NL80211_BAND_5GHZ];
3823                 band->n_iftype_data = count;
3824         }
3825
3826         if (cap->supported_bands & WMI_HOST_WLAN_5G_CAP &&
3827             ar->supports_6ghz) {
3828                 count = ath11k_mac_copy_he_cap(ar, cap,
3829                                                ar->mac.iftype[NL80211_BAND_6GHZ],
3830                                                NL80211_BAND_6GHZ);
3831                 band = &ar->mac.sbands[NL80211_BAND_6GHZ];
3832                 band->iftype_data = ar->mac.iftype[NL80211_BAND_6GHZ];
3833                 band->n_iftype_data = count;
3834         }
3835 }
3836
3837 static int __ath11k_set_antenna(struct ath11k *ar, u32 tx_ant, u32 rx_ant)
3838 {
3839         int ret;
3840
3841         lockdep_assert_held(&ar->conf_mutex);
3842
3843         if (ath11k_check_chain_mask(ar, tx_ant, true))
3844                 return -EINVAL;
3845
3846         if (ath11k_check_chain_mask(ar, rx_ant, false))
3847                 return -EINVAL;
3848
3849         ar->cfg_tx_chainmask = tx_ant;
3850         ar->cfg_rx_chainmask = rx_ant;
3851
3852         if (ar->state != ATH11K_STATE_ON &&
3853             ar->state != ATH11K_STATE_RESTARTED)
3854                 return 0;
3855
3856         ret = ath11k_wmi_pdev_set_param(ar, WMI_PDEV_PARAM_TX_CHAIN_MASK,
3857                                         tx_ant, ar->pdev->pdev_id);
3858         if (ret) {
3859                 ath11k_warn(ar->ab, "failed to set tx-chainmask: %d, req 0x%x\n",
3860                             ret, tx_ant);
3861                 return ret;
3862         }
3863
3864         ar->num_tx_chains = get_num_chains(tx_ant);
3865
3866         ret = ath11k_wmi_pdev_set_param(ar, WMI_PDEV_PARAM_RX_CHAIN_MASK,
3867                                         rx_ant, ar->pdev->pdev_id);
3868         if (ret) {
3869                 ath11k_warn(ar->ab, "failed to set rx-chainmask: %d, req 0x%x\n",
3870                             ret, rx_ant);
3871                 return ret;
3872         }
3873
3874         ar->num_rx_chains = get_num_chains(rx_ant);
3875
3876         /* Reload HT/VHT/HE capability */
3877         ath11k_mac_setup_ht_vht_cap(ar, &ar->pdev->cap, NULL);
3878         ath11k_mac_setup_he_cap(ar, &ar->pdev->cap);
3879
3880         return 0;
3881 }
3882
3883 int ath11k_mac_tx_mgmt_pending_free(int buf_id, void *skb, void *ctx)
3884 {
3885         struct sk_buff *msdu = skb;
3886         struct ieee80211_tx_info *info;
3887         struct ath11k *ar = ctx;
3888         struct ath11k_base *ab = ar->ab;
3889
3890         spin_lock_bh(&ar->txmgmt_idr_lock);
3891         idr_remove(&ar->txmgmt_idr, buf_id);
3892         spin_unlock_bh(&ar->txmgmt_idr_lock);
3893         dma_unmap_single(ab->dev, ATH11K_SKB_CB(msdu)->paddr, msdu->len,
3894                          DMA_TO_DEVICE);
3895
3896         info = IEEE80211_SKB_CB(msdu);
3897         memset(&info->status, 0, sizeof(info->status));
3898
3899         ieee80211_free_txskb(ar->hw, msdu);
3900
3901         return 0;
3902 }
3903
3904 static int ath11k_mac_vif_txmgmt_idr_remove(int buf_id, void *skb, void *ctx)
3905 {
3906         struct ieee80211_vif *vif = ctx;
3907         struct ath11k_skb_cb *skb_cb = ATH11K_SKB_CB((struct sk_buff *)skb);
3908         struct sk_buff *msdu = skb;
3909         struct ath11k *ar = skb_cb->ar;
3910         struct ath11k_base *ab = ar->ab;
3911
3912         if (skb_cb->vif == vif) {
3913                 spin_lock_bh(&ar->txmgmt_idr_lock);
3914                 idr_remove(&ar->txmgmt_idr, buf_id);
3915                 spin_unlock_bh(&ar->txmgmt_idr_lock);
3916                 dma_unmap_single(ab->dev, skb_cb->paddr, msdu->len,
3917                                  DMA_TO_DEVICE);
3918         }
3919
3920         return 0;
3921 }
3922
3923 static int ath11k_mac_mgmt_tx_wmi(struct ath11k *ar, struct ath11k_vif *arvif,
3924                                   struct sk_buff *skb)
3925 {
3926         struct ath11k_base *ab = ar->ab;
3927         struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
3928         struct ieee80211_tx_info *info;
3929         dma_addr_t paddr;
3930         int buf_id;
3931         int ret;
3932
3933         spin_lock_bh(&ar->txmgmt_idr_lock);
3934         buf_id = idr_alloc(&ar->txmgmt_idr, skb, 0,
3935                            ATH11K_TX_MGMT_NUM_PENDING_MAX, GFP_ATOMIC);
3936         spin_unlock_bh(&ar->txmgmt_idr_lock);
3937         if (buf_id < 0)
3938                 return -ENOSPC;
3939
3940         info = IEEE80211_SKB_CB(skb);
3941         if (!(info->flags & IEEE80211_TX_CTL_HW_80211_ENCAP)) {
3942                 if ((ieee80211_is_action(hdr->frame_control) ||
3943                      ieee80211_is_deauth(hdr->frame_control) ||
3944                      ieee80211_is_disassoc(hdr->frame_control)) &&
3945                      ieee80211_has_protected(hdr->frame_control)) {
3946                         skb_put(skb, IEEE80211_CCMP_MIC_LEN);
3947                 }
3948         }
3949
3950         paddr = dma_map_single(ab->dev, skb->data, skb->len, DMA_TO_DEVICE);
3951         if (dma_mapping_error(ab->dev, paddr)) {
3952                 ath11k_warn(ab, "failed to DMA map mgmt Tx buffer\n");
3953                 ret = -EIO;
3954                 goto err_free_idr;
3955         }
3956
3957         ATH11K_SKB_CB(skb)->paddr = paddr;
3958
3959         ret = ath11k_wmi_mgmt_send(ar, arvif->vdev_id, buf_id, skb);
3960         if (ret) {
3961                 ath11k_warn(ar->ab, "failed to send mgmt frame: %d\n", ret);
3962                 goto err_unmap_buf;
3963         }
3964
3965         return 0;
3966
3967 err_unmap_buf:
3968         dma_unmap_single(ab->dev, ATH11K_SKB_CB(skb)->paddr,
3969                          skb->len, DMA_TO_DEVICE);
3970 err_free_idr:
3971         spin_lock_bh(&ar->txmgmt_idr_lock);
3972         idr_remove(&ar->txmgmt_idr, buf_id);
3973         spin_unlock_bh(&ar->txmgmt_idr_lock);
3974
3975         return ret;
3976 }
3977
3978 static void ath11k_mgmt_over_wmi_tx_purge(struct ath11k *ar)
3979 {
3980         struct sk_buff *skb;
3981
3982         while ((skb = skb_dequeue(&ar->wmi_mgmt_tx_queue)) != NULL)
3983                 ieee80211_free_txskb(ar->hw, skb);
3984 }
3985
3986 static void ath11k_mgmt_over_wmi_tx_work(struct work_struct *work)
3987 {
3988         struct ath11k *ar = container_of(work, struct ath11k, wmi_mgmt_tx_work);
3989         struct ieee80211_tx_info *info;
3990         struct ath11k_vif *arvif;
3991         struct sk_buff *skb;
3992         int ret;
3993
3994         while ((skb = skb_dequeue(&ar->wmi_mgmt_tx_queue)) != NULL) {
3995                 info = IEEE80211_SKB_CB(skb);
3996                 if (!info->control.vif) {
3997                         ath11k_warn(ar->ab, "no vif found for mgmt frame, flags 0x%x\n",
3998                                     info->control.flags);
3999                         ieee80211_free_txskb(ar->hw, skb);
4000                         continue;
4001                 }
4002
4003                 arvif = ath11k_vif_to_arvif(info->control.vif);
4004                 if (ar->allocated_vdev_map & (1LL << arvif->vdev_id) &&
4005                     arvif->is_started) {
4006                         ret = ath11k_mac_mgmt_tx_wmi(ar, arvif, skb);
4007                         if (ret) {
4008                                 ath11k_warn(ar->ab, "failed to tx mgmt frame, vdev_id %d :%d\n",
4009                                             arvif->vdev_id, ret);
4010                                 ieee80211_free_txskb(ar->hw, skb);
4011                         } else {
4012                                 atomic_inc(&ar->num_pending_mgmt_tx);
4013                         }
4014                 } else {
4015                         ath11k_warn(ar->ab,
4016                                     "dropping mgmt frame for vdev %d, flags 0x%x is_started %d\n",
4017                                     arvif->vdev_id, info->control.flags,
4018                                     arvif->is_started);
4019                         ieee80211_free_txskb(ar->hw, skb);
4020                 }
4021         }
4022 }
4023
4024 static int ath11k_mac_mgmt_tx(struct ath11k *ar, struct sk_buff *skb,
4025                               bool is_prb_rsp)
4026 {
4027         struct sk_buff_head *q = &ar->wmi_mgmt_tx_queue;
4028
4029         if (test_bit(ATH11K_FLAG_CRASH_FLUSH, &ar->ab->dev_flags))
4030                 return -ESHUTDOWN;
4031
4032         /* Drop probe response packets when the pending management tx
4033          * count has reached a certain threshold, so as to prioritize
4034          * other mgmt packets like auth and assoc to be sent on time
4035          * for establishing successful connections.
4036          */
4037         if (is_prb_rsp &&
4038             atomic_read(&ar->num_pending_mgmt_tx) > ATH11K_PRB_RSP_DROP_THRESHOLD) {
4039                 ath11k_warn(ar->ab,
4040                             "dropping probe response as pending queue is almost full\n");
4041                 return -ENOSPC;
4042         }
4043
4044         if (skb_queue_len(q) == ATH11K_TX_MGMT_NUM_PENDING_MAX) {
4045                 ath11k_warn(ar->ab, "mgmt tx queue is full\n");
4046                 return -ENOSPC;
4047         }
4048
4049         skb_queue_tail(q, skb);
4050         ieee80211_queue_work(ar->hw, &ar->wmi_mgmt_tx_work);
4051
4052         return 0;
4053 }
4054
4055 static void ath11k_mac_op_tx(struct ieee80211_hw *hw,
4056                              struct ieee80211_tx_control *control,
4057                              struct sk_buff *skb)
4058 {
4059         struct ath11k_skb_cb *skb_cb = ATH11K_SKB_CB(skb);
4060         struct ath11k *ar = hw->priv;
4061         struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
4062         struct ieee80211_vif *vif = info->control.vif;
4063         struct ath11k_vif *arvif = ath11k_vif_to_arvif(vif);
4064         struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
4065         bool is_prb_rsp;
4066         int ret;
4067
4068         if (info->flags & IEEE80211_TX_CTL_HW_80211_ENCAP) {
4069                 skb_cb->flags |= ATH11K_SKB_HW_80211_ENCAP;
4070         } else if (ieee80211_is_mgmt(hdr->frame_control)) {
4071                 is_prb_rsp = ieee80211_is_probe_resp(hdr->frame_control);
4072                 ret = ath11k_mac_mgmt_tx(ar, skb, is_prb_rsp);
4073                 if (ret) {
4074                         ath11k_warn(ar->ab, "failed to queue management frame %d\n",
4075                                     ret);
4076                         ieee80211_free_txskb(ar->hw, skb);
4077                 }
4078                 return;
4079         }
4080
4081         ret = ath11k_dp_tx(ar, arvif, skb);
4082         if (ret) {
4083                 ath11k_warn(ar->ab, "failed to transmit frame %d\n", ret);
4084                 ieee80211_free_txskb(ar->hw, skb);
4085         }
4086 }
4087
4088 void ath11k_mac_drain_tx(struct ath11k *ar)
4089 {
4090         /* make sure rcu-protected mac80211 tx path itself is drained */
4091         synchronize_net();
4092
4093         cancel_work_sync(&ar->wmi_mgmt_tx_work);
4094         ath11k_mgmt_over_wmi_tx_purge(ar);
4095 }
4096
4097 static int ath11k_mac_config_mon_status_default(struct ath11k *ar, bool enable)
4098 {
4099         struct htt_rx_ring_tlv_filter tlv_filter = {0};
4100         struct ath11k_base *ab = ar->ab;
4101         int i, ret = 0;
4102         u32 ring_id;
4103
4104         if (enable) {
4105                 tlv_filter = ath11k_mac_mon_status_filter_default;
4106                 tlv_filter.rx_filter = ath11k_debugfs_rx_filter(ar);
4107         }
4108
4109         for (i = 0; i < ab->hw_params.num_rxmda_per_pdev; i++) {
4110                 ring_id = ar->dp.rx_mon_status_refill_ring[i].refill_buf_ring.ring_id;
4111                 ret = ath11k_dp_tx_htt_rx_filter_setup(ar->ab, ring_id,
4112                                                        ar->dp.mac_id + i,
4113                                                        HAL_RXDMA_MONITOR_STATUS,
4114                                                        DP_RX_BUFFER_SIZE,
4115                                                        &tlv_filter);
4116         }
4117
4118         return ret;
4119 }
4120
4121 static int ath11k_mac_op_start(struct ieee80211_hw *hw)
4122 {
4123         struct ath11k *ar = hw->priv;
4124         struct ath11k_base *ab = ar->ab;
4125         struct ath11k_pdev *pdev = ar->pdev;
4126         int ret;
4127
4128         ath11k_mac_drain_tx(ar);
4129         mutex_lock(&ar->conf_mutex);
4130
4131         switch (ar->state) {
4132         case ATH11K_STATE_OFF:
4133                 ar->state = ATH11K_STATE_ON;
4134                 break;
4135         case ATH11K_STATE_RESTARTING:
4136                 ar->state = ATH11K_STATE_RESTARTED;
4137                 break;
4138         case ATH11K_STATE_RESTARTED:
4139         case ATH11K_STATE_WEDGED:
4140         case ATH11K_STATE_ON:
4141                 WARN_ON(1);
4142                 ret = -EINVAL;
4143                 goto err;
4144         }
4145
4146         ret = ath11k_wmi_pdev_set_param(ar, WMI_PDEV_PARAM_PMF_QOS,
4147                                         1, pdev->pdev_id);
4148
4149         if (ret) {
4150                 ath11k_err(ar->ab, "failed to enable PMF QOS: (%d\n", ret);
4151                 goto err;
4152         }
4153
4154         ret = ath11k_wmi_pdev_set_param(ar, WMI_PDEV_PARAM_DYNAMIC_BW, 1,
4155                                         pdev->pdev_id);
4156         if (ret) {
4157                 ath11k_err(ar->ab, "failed to enable dynamic bw: %d\n", ret);
4158                 goto err;
4159         }
4160
4161         ret = ath11k_wmi_pdev_set_param(ar, WMI_PDEV_PARAM_ARP_AC_OVERRIDE,
4162                                         0, pdev->pdev_id);
4163         if (ret) {
4164                 ath11k_err(ab, "failed to set ac override for ARP: %d\n",
4165                            ret);
4166                 goto err;
4167         }
4168
4169         ret = ath11k_wmi_send_dfs_phyerr_offload_enable_cmd(ar, pdev->pdev_id);
4170         if (ret) {
4171                 ath11k_err(ab, "failed to offload radar detection: %d\n",
4172                            ret);
4173                 goto err;
4174         }
4175
4176         ret = ath11k_dp_tx_htt_h2t_ppdu_stats_req(ar,
4177                                                   HTT_PPDU_STATS_TAG_DEFAULT);
4178         if (ret) {
4179                 ath11k_err(ab, "failed to req ppdu stats: %d\n", ret);
4180                 goto err;
4181         }
4182
4183         ret = ath11k_wmi_pdev_set_param(ar, WMI_PDEV_PARAM_MESH_MCAST_ENABLE,
4184                                         1, pdev->pdev_id);
4185
4186         if (ret) {
4187                 ath11k_err(ar->ab, "failed to enable MESH MCAST ENABLE: (%d\n", ret);
4188                 goto err;
4189         }
4190
4191         __ath11k_set_antenna(ar, ar->cfg_tx_chainmask, ar->cfg_rx_chainmask);
4192
4193         /* TODO: Do we need to enable ANI? */
4194
4195         ath11k_reg_update_chan_list(ar);
4196
4197         ar->num_started_vdevs = 0;
4198         ar->num_created_vdevs = 0;
4199         ar->num_peers = 0;
4200         ar->allocated_vdev_map = 0;
4201
4202         /* Configure monitor status ring with default rx_filter to get rx status
4203          * such as rssi, rx_duration.
4204          */
4205         ret = ath11k_mac_config_mon_status_default(ar, true);
4206         if (ret) {
4207                 ath11k_err(ab, "failed to configure monitor status ring with default rx_filter: (%d)\n",
4208                            ret);
4209                 goto err;
4210         }
4211
4212         /* Configure the hash seed for hash based reo dest ring selection */
4213         ath11k_wmi_pdev_lro_cfg(ar, ar->pdev->pdev_id);
4214
4215         mutex_unlock(&ar->conf_mutex);
4216
4217         rcu_assign_pointer(ab->pdevs_active[ar->pdev_idx],
4218                            &ab->pdevs[ar->pdev_idx]);
4219
4220         return 0;
4221
4222 err:
4223         ar->state = ATH11K_STATE_OFF;
4224         mutex_unlock(&ar->conf_mutex);
4225
4226         return ret;
4227 }
4228
4229 static void ath11k_mac_op_stop(struct ieee80211_hw *hw)
4230 {
4231         struct ath11k *ar = hw->priv;
4232         struct htt_ppdu_stats_info *ppdu_stats, *tmp;
4233         int ret;
4234
4235         ath11k_mac_drain_tx(ar);
4236
4237         mutex_lock(&ar->conf_mutex);
4238         ret = ath11k_mac_config_mon_status_default(ar, false);
4239         if (ret)
4240                 ath11k_err(ar->ab, "failed to clear rx_filter for monitor status ring: (%d)\n",
4241                            ret);
4242
4243         clear_bit(ATH11K_CAC_RUNNING, &ar->dev_flags);
4244         ar->state = ATH11K_STATE_OFF;
4245         mutex_unlock(&ar->conf_mutex);
4246
4247         cancel_delayed_work_sync(&ar->scan.timeout);
4248         cancel_work_sync(&ar->regd_update_work);
4249
4250         spin_lock_bh(&ar->data_lock);
4251         list_for_each_entry_safe(ppdu_stats, tmp, &ar->ppdu_stats_info, list) {
4252                 list_del(&ppdu_stats->list);
4253                 kfree(ppdu_stats);
4254         }
4255         spin_unlock_bh(&ar->data_lock);
4256
4257         rcu_assign_pointer(ar->ab->pdevs_active[ar->pdev_idx], NULL);
4258
4259         synchronize_rcu();
4260
4261         atomic_set(&ar->num_pending_mgmt_tx, 0);
4262 }
4263
4264 static void
4265 ath11k_mac_setup_vdev_create_params(struct ath11k_vif *arvif,
4266                                     struct vdev_create_params *params)
4267 {
4268         struct ath11k *ar = arvif->ar;
4269         struct ath11k_pdev *pdev = ar->pdev;
4270
4271         params->if_id = arvif->vdev_id;
4272         params->type = arvif->vdev_type;
4273         params->subtype = arvif->vdev_subtype;
4274         params->pdev_id = pdev->pdev_id;
4275
4276         if (pdev->cap.supported_bands & WMI_HOST_WLAN_2G_CAP) {
4277                 params->chains[NL80211_BAND_2GHZ].tx = ar->num_tx_chains;
4278                 params->chains[NL80211_BAND_2GHZ].rx = ar->num_rx_chains;
4279         }
4280         if (pdev->cap.supported_bands & WMI_HOST_WLAN_5G_CAP) {
4281                 params->chains[NL80211_BAND_5GHZ].tx = ar->num_tx_chains;
4282                 params->chains[NL80211_BAND_5GHZ].rx = ar->num_rx_chains;
4283         }
4284         if (pdev->cap.supported_bands & WMI_HOST_WLAN_5G_CAP &&
4285             ar->supports_6ghz) {
4286                 params->chains[NL80211_BAND_6GHZ].tx = ar->num_tx_chains;
4287                 params->chains[NL80211_BAND_6GHZ].rx = ar->num_rx_chains;
4288         }
4289 }
4290
4291 static u32
4292 ath11k_mac_prepare_he_mode(struct ath11k_pdev *pdev, u32 viftype)
4293 {
4294         struct ath11k_pdev_cap *pdev_cap = &pdev->cap;
4295         struct ath11k_band_cap *cap_band = NULL;
4296         u32 *hecap_phy_ptr = NULL;
4297         u32 hemode = 0;
4298
4299         if (pdev->cap.supported_bands & WMI_HOST_WLAN_2G_CAP)
4300                 cap_band = &pdev_cap->band[NL80211_BAND_2GHZ];
4301         else
4302                 cap_band = &pdev_cap->band[NL80211_BAND_5GHZ];
4303
4304         hecap_phy_ptr = &cap_band->he_cap_phy_info[0];
4305
4306         hemode = FIELD_PREP(HE_MODE_SU_TX_BFEE, HE_SU_BFEE_ENABLE) |
4307                  FIELD_PREP(HE_MODE_SU_TX_BFER, HECAP_PHY_SUBFMR_GET(hecap_phy_ptr)) |
4308                  FIELD_PREP(HE_MODE_UL_MUMIMO, HECAP_PHY_ULMUMIMO_GET(hecap_phy_ptr));
4309
4310         /* TODO WDS and other modes */
4311         if (viftype == NL80211_IFTYPE_AP) {
4312                 hemode |= FIELD_PREP(HE_MODE_MU_TX_BFER,
4313                           HECAP_PHY_MUBFMR_GET(hecap_phy_ptr)) |
4314                           FIELD_PREP(HE_MODE_DL_OFDMA, HE_DL_MUOFDMA_ENABLE) |
4315                           FIELD_PREP(HE_MODE_UL_OFDMA, HE_UL_MUOFDMA_ENABLE);
4316         } else {
4317                 hemode |= FIELD_PREP(HE_MODE_MU_TX_BFEE, HE_MU_BFEE_ENABLE);
4318         }
4319
4320         return hemode;
4321 }
4322
4323 static int ath11k_set_he_mu_sounding_mode(struct ath11k *ar,
4324                                           struct ath11k_vif *arvif)
4325 {
4326         u32 param_id, param_value;
4327         struct ath11k_base *ab = ar->ab;
4328         int ret = 0;
4329
4330         param_id = WMI_VDEV_PARAM_SET_HEMU_MODE;
4331         param_value = ath11k_mac_prepare_he_mode(ar->pdev, arvif->vif->type);
4332         ret = ath11k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
4333                                             param_id, param_value);
4334         if (ret) {
4335                 ath11k_warn(ab, "failed to set vdev %d HE MU mode: %d param_value %x\n",
4336                             arvif->vdev_id, ret, param_value);
4337                 return ret;
4338         }
4339         param_id = WMI_VDEV_PARAM_SET_HE_SOUNDING_MODE;
4340         param_value =
4341                 FIELD_PREP(HE_VHT_SOUNDING_MODE, HE_VHT_SOUNDING_MODE_ENABLE) |
4342                 FIELD_PREP(HE_TRIG_NONTRIG_SOUNDING_MODE,
4343                            HE_TRIG_NONTRIG_SOUNDING_MODE_ENABLE);
4344         ret = ath11k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
4345                                             param_id, param_value);
4346         if (ret) {
4347                 ath11k_warn(ab, "failed to set vdev %d HE MU mode: %d\n",
4348                             arvif->vdev_id, ret);
4349                 return ret;
4350         }
4351         return ret;
4352 }
4353
4354 static void ath11k_mac_op_update_vif_offload(struct ieee80211_hw *hw,
4355                                              struct ieee80211_vif *vif)
4356 {
4357         struct ath11k *ar = hw->priv;
4358         struct ath11k_base *ab = ar->ab;
4359         struct ath11k_vif *arvif = ath11k_vif_to_arvif(vif);
4360         u32 param_id, param_value;
4361         int ret;
4362
4363         param_id = WMI_VDEV_PARAM_TX_ENCAP_TYPE;
4364         if (ath11k_frame_mode != ATH11K_HW_TXRX_ETHERNET ||
4365             (vif->type != NL80211_IFTYPE_STATION &&
4366              vif->type != NL80211_IFTYPE_AP))
4367                 vif->offload_flags &= ~IEEE80211_OFFLOAD_ENCAP_ENABLED;
4368
4369         if (vif->offload_flags & IEEE80211_OFFLOAD_ENCAP_ENABLED)
4370                 param_value = ATH11K_HW_TXRX_ETHERNET;
4371         else if (test_bit(ATH11K_FLAG_RAW_MODE, &ab->dev_flags))
4372                 param_value = ATH11K_HW_TXRX_RAW;
4373         else
4374                 param_value = ATH11K_HW_TXRX_NATIVE_WIFI;
4375
4376         ret = ath11k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
4377                                             param_id, param_value);
4378         if (ret) {
4379                 ath11k_warn(ab, "failed to set vdev %d tx encap mode: %d\n",
4380                             arvif->vdev_id, ret);
4381                 vif->offload_flags &= ~IEEE80211_OFFLOAD_ENCAP_ENABLED;
4382         }
4383 }
4384
4385 static int ath11k_mac_op_add_interface(struct ieee80211_hw *hw,
4386                                        struct ieee80211_vif *vif)
4387 {
4388         struct ath11k *ar = hw->priv;
4389         struct ath11k_base *ab = ar->ab;
4390         struct ath11k_vif *arvif = ath11k_vif_to_arvif(vif);
4391         struct vdev_create_params vdev_param = {0};
4392         struct peer_create_params peer_param;
4393         u32 param_id, param_value;
4394         u16 nss;
4395         int i;
4396         int ret;
4397         int bit;
4398
4399         vif->driver_flags |= IEEE80211_VIF_SUPPORTS_UAPSD;
4400
4401         mutex_lock(&ar->conf_mutex);
4402
4403         if (vif->type == NL80211_IFTYPE_AP &&
4404             ar->num_peers > (ar->max_num_peers - 1)) {
4405                 ath11k_warn(ab, "failed to create vdev due to insufficient peer entry resource in firmware\n");
4406                 ret = -ENOBUFS;
4407                 goto err;
4408         }
4409
4410         if (ar->num_created_vdevs > (TARGET_NUM_VDEVS - 1)) {
4411                 ath11k_warn(ab, "failed to create vdev, reached max vdev limit %d\n",
4412                             TARGET_NUM_VDEVS);
4413                 ret = -EBUSY;
4414                 goto err;
4415         }
4416
4417         memset(arvif, 0, sizeof(*arvif));
4418
4419         arvif->ar = ar;
4420         arvif->vif = vif;
4421
4422         INIT_LIST_HEAD(&arvif->list);
4423
4424         /* Should we initialize any worker to handle connection loss indication
4425          * from firmware in sta mode?
4426          */
4427
4428         for (i = 0; i < ARRAY_SIZE(arvif->bitrate_mask.control); i++) {
4429                 arvif->bitrate_mask.control[i].legacy = 0xffffffff;
4430                 memset(arvif->bitrate_mask.control[i].ht_mcs, 0xff,
4431                        sizeof(arvif->bitrate_mask.control[i].ht_mcs));
4432                 memset(arvif->bitrate_mask.control[i].vht_mcs, 0xff,
4433                        sizeof(arvif->bitrate_mask.control[i].vht_mcs));
4434         }
4435
4436         bit = __ffs64(ab->free_vdev_map);
4437
4438         arvif->vdev_id = bit;
4439         arvif->vdev_subtype = WMI_VDEV_SUBTYPE_NONE;
4440
4441         switch (vif->type) {
4442         case NL80211_IFTYPE_UNSPECIFIED:
4443         case NL80211_IFTYPE_STATION:
4444                 arvif->vdev_type = WMI_VDEV_TYPE_STA;
4445                 break;
4446         case NL80211_IFTYPE_MESH_POINT:
4447                 arvif->vdev_subtype = WMI_VDEV_SUBTYPE_MESH_11S;
4448                 fallthrough;
4449         case NL80211_IFTYPE_AP:
4450                 arvif->vdev_type = WMI_VDEV_TYPE_AP;
4451                 break;
4452         case NL80211_IFTYPE_MONITOR:
4453                 arvif->vdev_type = WMI_VDEV_TYPE_MONITOR;
4454                 break;
4455         default:
4456                 WARN_ON(1);
4457                 break;
4458         }
4459
4460         ath11k_dbg(ar->ab, ATH11K_DBG_MAC, "mac add interface id %d type %d subtype %d map %llx\n",
4461                    arvif->vdev_id, arvif->vdev_type, arvif->vdev_subtype,
4462                    ab->free_vdev_map);
4463
4464         vif->cab_queue = arvif->vdev_id % (ATH11K_HW_MAX_QUEUES - 1);
4465         for (i = 0; i < ARRAY_SIZE(vif->hw_queue); i++)
4466                 vif->hw_queue[i] = i % (ATH11K_HW_MAX_QUEUES - 1);
4467
4468         ath11k_mac_setup_vdev_create_params(arvif, &vdev_param);
4469
4470         ret = ath11k_wmi_vdev_create(ar, vif->addr, &vdev_param);
4471         if (ret) {
4472                 ath11k_warn(ab, "failed to create WMI vdev %d: %d\n",
4473                             arvif->vdev_id, ret);
4474                 goto err;
4475         }
4476
4477         ar->num_created_vdevs++;
4478         ath11k_dbg(ab, ATH11K_DBG_MAC, "vdev %pM created, vdev_id %d\n",
4479                    vif->addr, arvif->vdev_id);
4480         ar->allocated_vdev_map |= 1LL << arvif->vdev_id;
4481         ab->free_vdev_map &= ~(1LL << arvif->vdev_id);
4482
4483         spin_lock_bh(&ar->data_lock);
4484         list_add(&arvif->list, &ar->arvifs);
4485         spin_unlock_bh(&ar->data_lock);
4486
4487         ath11k_mac_op_update_vif_offload(hw, vif);
4488
4489         nss = get_num_chains(ar->cfg_tx_chainmask) ? : 1;
4490         ret = ath11k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
4491                                             WMI_VDEV_PARAM_NSS, nss);
4492         if (ret) {
4493                 ath11k_warn(ab, "failed to set vdev %d chainmask 0x%x, nss %d :%d\n",
4494                             arvif->vdev_id, ar->cfg_tx_chainmask, nss, ret);
4495                 goto err_vdev_del;
4496         }
4497
4498         switch (arvif->vdev_type) {
4499         case WMI_VDEV_TYPE_AP:
4500                 peer_param.vdev_id = arvif->vdev_id;
4501                 peer_param.peer_addr = vif->addr;
4502                 peer_param.peer_type = WMI_PEER_TYPE_DEFAULT;
4503                 ret = ath11k_peer_create(ar, arvif, NULL, &peer_param);
4504                 if (ret) {
4505                         ath11k_warn(ab, "failed to vdev %d create peer for AP: %d\n",
4506                                     arvif->vdev_id, ret);
4507                         goto err_vdev_del;
4508                 }
4509
4510                 ret = ath11k_mac_set_kickout(arvif);
4511                 if (ret) {
4512                         ath11k_warn(ar->ab, "failed to set vdev %i kickout parameters: %d\n",
4513                                     arvif->vdev_id, ret);
4514                         goto err_peer_del;
4515                 }
4516                 break;
4517         case WMI_VDEV_TYPE_STA:
4518                 param_id = WMI_STA_PS_PARAM_RX_WAKE_POLICY;
4519                 param_value = WMI_STA_PS_RX_WAKE_POLICY_WAKE;
4520                 ret = ath11k_wmi_set_sta_ps_param(ar, arvif->vdev_id,
4521                                                   param_id, param_value);
4522                 if (ret) {
4523                         ath11k_warn(ar->ab, "failed to set vdev %d RX wake policy: %d\n",
4524                                     arvif->vdev_id, ret);
4525                         goto err_peer_del;
4526                 }
4527
4528                 param_id = WMI_STA_PS_PARAM_TX_WAKE_THRESHOLD;
4529                 param_value = WMI_STA_PS_TX_WAKE_THRESHOLD_ALWAYS;
4530                 ret = ath11k_wmi_set_sta_ps_param(ar, arvif->vdev_id,
4531                                                   param_id, param_value);
4532                 if (ret) {
4533                         ath11k_warn(ar->ab, "failed to set vdev %d TX wake threshold: %d\n",
4534                                     arvif->vdev_id, ret);
4535                         goto err_peer_del;
4536                 }
4537
4538                 param_id = WMI_STA_PS_PARAM_PSPOLL_COUNT;
4539                 param_value = WMI_STA_PS_PSPOLL_COUNT_NO_MAX;
4540                 ret = ath11k_wmi_set_sta_ps_param(ar, arvif->vdev_id,
4541                                                   param_id, param_value);
4542                 if (ret) {
4543                         ath11k_warn(ar->ab, "failed to set vdev %d pspoll count: %d\n",
4544                                     arvif->vdev_id, ret);
4545                         goto err_peer_del;
4546                 }
4547
4548                 ret = ath11k_wmi_pdev_set_ps_mode(ar, arvif->vdev_id, false);
4549                 if (ret) {
4550                         ath11k_warn(ar->ab, "failed to disable vdev %d ps mode: %d\n",
4551                                     arvif->vdev_id, ret);
4552                         goto err_peer_del;
4553                 }
4554                 break;
4555         default:
4556                 break;
4557         }
4558
4559         arvif->txpower = vif->bss_conf.txpower;
4560         ret = ath11k_mac_txpower_recalc(ar);
4561         if (ret)
4562                 goto err_peer_del;
4563
4564         param_id = WMI_VDEV_PARAM_RTS_THRESHOLD;
4565         param_value = ar->hw->wiphy->rts_threshold;
4566         ret = ath11k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
4567                                             param_id, param_value);
4568         if (ret) {
4569                 ath11k_warn(ar->ab, "failed to set rts threshold for vdev %d: %d\n",
4570                             arvif->vdev_id, ret);
4571         }
4572
4573         ath11k_dp_vdev_tx_attach(ar, arvif);
4574
4575         mutex_unlock(&ar->conf_mutex);
4576
4577         return 0;
4578
4579 err_peer_del:
4580         if (arvif->vdev_type == WMI_VDEV_TYPE_AP) {
4581                 ar->num_peers--;
4582                 ath11k_wmi_send_peer_delete_cmd(ar, vif->addr, arvif->vdev_id);
4583         }
4584
4585 err_vdev_del:
4586         ath11k_wmi_vdev_delete(ar, arvif->vdev_id);
4587         ar->num_created_vdevs--;
4588         ar->allocated_vdev_map &= ~(1LL << arvif->vdev_id);
4589         ab->free_vdev_map |= 1LL << arvif->vdev_id;
4590         spin_lock_bh(&ar->data_lock);
4591         list_del(&arvif->list);
4592         spin_unlock_bh(&ar->data_lock);
4593
4594 err:
4595         mutex_unlock(&ar->conf_mutex);
4596
4597         return ret;
4598 }
4599
4600 static int ath11k_mac_vif_unref(int buf_id, void *skb, void *ctx)
4601 {
4602         struct ieee80211_vif *vif = (struct ieee80211_vif *)ctx;
4603         struct ath11k_skb_cb *skb_cb = ATH11K_SKB_CB((struct sk_buff *)skb);
4604
4605         if (skb_cb->vif == vif)
4606                 skb_cb->vif = NULL;
4607
4608         return 0;
4609 }
4610
4611 static void ath11k_mac_op_remove_interface(struct ieee80211_hw *hw,
4612                                            struct ieee80211_vif *vif)
4613 {
4614         struct ath11k *ar = hw->priv;
4615         struct ath11k_vif *arvif = ath11k_vif_to_arvif(vif);
4616         struct ath11k_base *ab = ar->ab;
4617         int ret;
4618         int i;
4619
4620         mutex_lock(&ar->conf_mutex);
4621
4622         ath11k_dbg(ab, ATH11K_DBG_MAC, "mac remove interface (vdev %d)\n",
4623                    arvif->vdev_id);
4624
4625         spin_lock_bh(&ar->data_lock);
4626         list_del(&arvif->list);
4627         spin_unlock_bh(&ar->data_lock);
4628
4629         if (arvif->vdev_type == WMI_VDEV_TYPE_AP) {
4630                 ret = ath11k_peer_delete(ar, arvif->vdev_id, vif->addr);
4631                 if (ret)
4632                         ath11k_warn(ab, "failed to submit AP self-peer removal on vdev %d: %d\n",
4633                                     arvif->vdev_id, ret);
4634         }
4635
4636         ret = ath11k_wmi_vdev_delete(ar, arvif->vdev_id);
4637         if (ret)
4638                 ath11k_warn(ab, "failed to delete WMI vdev %d: %d\n",
4639                             arvif->vdev_id, ret);
4640
4641         ar->num_created_vdevs--;
4642         ath11k_dbg(ab, ATH11K_DBG_MAC, "vdev %pM deleted, vdev_id %d\n",
4643                    vif->addr, arvif->vdev_id);
4644         ar->allocated_vdev_map &= ~(1LL << arvif->vdev_id);
4645         ab->free_vdev_map |= 1LL << (arvif->vdev_id);
4646
4647         ath11k_peer_cleanup(ar, arvif->vdev_id);
4648
4649         idr_for_each(&ar->txmgmt_idr,
4650                      ath11k_mac_vif_txmgmt_idr_remove, vif);
4651
4652         for (i = 0; i < DP_TCL_NUM_RING_MAX; i++) {
4653                 spin_lock_bh(&ab->dp.tx_ring[i].tx_idr_lock);
4654                 idr_for_each(&ab->dp.tx_ring[i].txbuf_idr,
4655                              ath11k_mac_vif_unref, vif);
4656                 spin_unlock_bh(&ab->dp.tx_ring[i].tx_idr_lock);
4657         }
4658
4659         /* Recalc txpower for remaining vdev */
4660         ath11k_mac_txpower_recalc(ar);
4661         clear_bit(ATH11K_FLAG_MONITOR_ENABLED, &ar->monitor_flags);
4662
4663         /* TODO: recal traffic pause state based on the available vdevs */
4664
4665         mutex_unlock(&ar->conf_mutex);
4666 }
4667
4668 /* FIXME: Has to be verified. */
4669 #define SUPPORTED_FILTERS                       \
4670         (FIF_ALLMULTI |                         \
4671         FIF_CONTROL |                           \
4672         FIF_PSPOLL |                            \
4673         FIF_OTHER_BSS |                         \
4674         FIF_BCN_PRBRESP_PROMISC |               \
4675         FIF_PROBE_REQ |                         \
4676         FIF_FCSFAIL)
4677
4678 static void ath11k_mac_op_configure_filter(struct ieee80211_hw *hw,
4679                                            unsigned int changed_flags,
4680                                            unsigned int *total_flags,
4681                                            u64 multicast)
4682 {
4683         struct ath11k *ar = hw->priv;
4684         bool reset_flag = false;
4685         int ret = 0;
4686
4687         mutex_lock(&ar->conf_mutex);
4688
4689         changed_flags &= SUPPORTED_FILTERS;
4690         *total_flags &= SUPPORTED_FILTERS;
4691         ar->filter_flags = *total_flags;
4692
4693         /* For monitor mode */
4694         reset_flag = !(ar->filter_flags & FIF_BCN_PRBRESP_PROMISC);
4695
4696         ret = ath11k_dp_tx_htt_monitor_mode_ring_config(ar, reset_flag);
4697         if (!ret) {
4698                 if (!reset_flag)
4699                         set_bit(ATH11K_FLAG_MONITOR_ENABLED, &ar->monitor_flags);
4700                 else
4701                         clear_bit(ATH11K_FLAG_MONITOR_ENABLED, &ar->monitor_flags);
4702         } else {
4703                 ath11k_warn(ar->ab,
4704                             "fail to set monitor filter: %d\n", ret);
4705         }
4706         mutex_unlock(&ar->conf_mutex);
4707 }
4708
4709 static int ath11k_mac_op_get_antenna(struct ieee80211_hw *hw, u32 *tx_ant, u32 *rx_ant)
4710 {
4711         struct ath11k *ar = hw->priv;
4712
4713         mutex_lock(&ar->conf_mutex);
4714
4715         *tx_ant = ar->cfg_tx_chainmask;
4716         *rx_ant = ar->cfg_rx_chainmask;
4717
4718         mutex_unlock(&ar->conf_mutex);
4719
4720         return 0;
4721 }
4722
4723 static int ath11k_mac_op_set_antenna(struct ieee80211_hw *hw, u32 tx_ant, u32 rx_ant)
4724 {
4725         struct ath11k *ar = hw->priv;
4726         int ret;
4727
4728         mutex_lock(&ar->conf_mutex);
4729         ret = __ath11k_set_antenna(ar, tx_ant, rx_ant);
4730         mutex_unlock(&ar->conf_mutex);
4731
4732         return ret;
4733 }
4734
4735 static int ath11k_mac_op_ampdu_action(struct ieee80211_hw *hw,
4736                                       struct ieee80211_vif *vif,
4737                                       struct ieee80211_ampdu_params *params)
4738 {
4739         struct ath11k *ar = hw->priv;
4740         int ret = -EINVAL;
4741
4742         mutex_lock(&ar->conf_mutex);
4743
4744         switch (params->action) {
4745         case IEEE80211_AMPDU_RX_START:
4746                 ret = ath11k_dp_rx_ampdu_start(ar, params);
4747                 break;
4748         case IEEE80211_AMPDU_RX_STOP:
4749                 ret = ath11k_dp_rx_ampdu_stop(ar, params);
4750                 break;
4751         case IEEE80211_AMPDU_TX_START:
4752         case IEEE80211_AMPDU_TX_STOP_CONT:
4753         case IEEE80211_AMPDU_TX_STOP_FLUSH:
4754         case IEEE80211_AMPDU_TX_STOP_FLUSH_CONT:
4755         case IEEE80211_AMPDU_TX_OPERATIONAL:
4756                 /* Tx A-MPDU aggregation offloaded to hw/fw so deny mac80211
4757                  * Tx aggregation requests.
4758                  */
4759                 ret = -EOPNOTSUPP;
4760                 break;
4761         }
4762
4763         mutex_unlock(&ar->conf_mutex);
4764
4765         return ret;
4766 }
4767
4768 static int ath11k_mac_op_add_chanctx(struct ieee80211_hw *hw,
4769                                      struct ieee80211_chanctx_conf *ctx)
4770 {
4771         struct ath11k *ar = hw->priv;
4772         struct ath11k_base *ab = ar->ab;
4773
4774         ath11k_dbg(ab, ATH11K_DBG_MAC,
4775                    "mac chanctx add freq %hu width %d ptr %pK\n",
4776                    ctx->def.chan->center_freq, ctx->def.width, ctx);
4777
4778         mutex_lock(&ar->conf_mutex);
4779
4780         spin_lock_bh(&ar->data_lock);
4781         /* TODO: In case of multiple channel context, populate rx_channel from
4782          * Rx PPDU desc information.
4783          */
4784         ar->rx_channel = ctx->def.chan;
4785         spin_unlock_bh(&ar->data_lock);
4786
4787         mutex_unlock(&ar->conf_mutex);
4788
4789         return 0;
4790 }
4791
4792 static void ath11k_mac_op_remove_chanctx(struct ieee80211_hw *hw,
4793                                          struct ieee80211_chanctx_conf *ctx)
4794 {
4795         struct ath11k *ar = hw->priv;
4796         struct ath11k_base *ab = ar->ab;
4797
4798         ath11k_dbg(ab, ATH11K_DBG_MAC,
4799                    "mac chanctx remove freq %hu width %d ptr %pK\n",
4800                    ctx->def.chan->center_freq, ctx->def.width, ctx);
4801
4802         mutex_lock(&ar->conf_mutex);
4803
4804         spin_lock_bh(&ar->data_lock);
4805         /* TODO: In case of there is one more channel context left, populate
4806          * rx_channel with the channel of that remaining channel context.
4807          */
4808         ar->rx_channel = NULL;
4809         spin_unlock_bh(&ar->data_lock);
4810
4811         mutex_unlock(&ar->conf_mutex);
4812 }
4813
4814 static inline int ath11k_mac_vdev_setup_sync(struct ath11k *ar)
4815 {
4816         lockdep_assert_held(&ar->conf_mutex);
4817
4818         if (test_bit(ATH11K_FLAG_CRASH_FLUSH, &ar->ab->dev_flags))
4819                 return -ESHUTDOWN;
4820
4821         if (!wait_for_completion_timeout(&ar->vdev_setup_done,
4822                                          ATH11K_VDEV_SETUP_TIMEOUT_HZ))
4823                 return -ETIMEDOUT;
4824
4825         return ar->last_wmi_vdev_start_status ? -EINVAL : 0;
4826 }
4827
4828 static int
4829 ath11k_mac_vdev_start_restart(struct ath11k_vif *arvif,
4830                               const struct cfg80211_chan_def *chandef,
4831                               bool restart)
4832 {
4833         struct ath11k *ar = arvif->ar;
4834         struct ath11k_base *ab = ar->ab;
4835         struct wmi_vdev_start_req_arg arg = {};
4836         int he_support = arvif->vif->bss_conf.he_support;
4837         int ret = 0;
4838
4839         lockdep_assert_held(&ar->conf_mutex);
4840
4841         reinit_completion(&ar->vdev_setup_done);
4842
4843         arg.vdev_id = arvif->vdev_id;
4844         arg.dtim_period = arvif->dtim_period;
4845         arg.bcn_intval = arvif->beacon_interval;
4846
4847         arg.channel.freq = chandef->chan->center_freq;
4848         arg.channel.band_center_freq1 = chandef->center_freq1;
4849         arg.channel.band_center_freq2 = chandef->center_freq2;
4850         arg.channel.mode =
4851                 ath11k_phymodes[chandef->chan->band][chandef->width];
4852
4853         arg.channel.min_power = 0;
4854         arg.channel.max_power = chandef->chan->max_power * 2;
4855         arg.channel.max_reg_power = chandef->chan->max_reg_power * 2;
4856         arg.channel.max_antenna_gain = chandef->chan->max_antenna_gain * 2;
4857
4858         arg.pref_tx_streams = ar->num_tx_chains;
4859         arg.pref_rx_streams = ar->num_rx_chains;
4860
4861         if (arvif->vdev_type == WMI_VDEV_TYPE_AP) {
4862                 arg.ssid = arvif->u.ap.ssid;
4863                 arg.ssid_len = arvif->u.ap.ssid_len;
4864                 arg.hidden_ssid = arvif->u.ap.hidden_ssid;
4865
4866                 /* For now allow DFS for AP mode */
4867                 arg.channel.chan_radar =
4868                         !!(chandef->chan->flags & IEEE80211_CHAN_RADAR);
4869
4870                 arg.channel.passive = arg.channel.chan_radar;
4871
4872                 spin_lock_bh(&ab->base_lock);
4873                 arg.regdomain = ar->ab->dfs_region;
4874                 spin_unlock_bh(&ab->base_lock);
4875
4876                 /* TODO: Notify if secondary 80Mhz also needs radar detection */
4877                 if (he_support) {
4878                         ret = ath11k_set_he_mu_sounding_mode(ar, arvif);
4879                         if (ret) {
4880                                 ath11k_warn(ar->ab, "failed to set he mode vdev %i\n",
4881                                             arg.vdev_id);
4882                                 return ret;
4883                         }
4884                 }
4885         }
4886
4887         arg.channel.passive |= !!(chandef->chan->flags & IEEE80211_CHAN_NO_IR);
4888
4889         ath11k_dbg(ab, ATH11K_DBG_MAC,
4890                    "mac vdev %d start center_freq %d phymode %s\n",
4891                    arg.vdev_id, arg.channel.freq,
4892                    ath11k_wmi_phymode_str(arg.channel.mode));
4893
4894         ret = ath11k_wmi_vdev_start(ar, &arg, restart);
4895         if (ret) {
4896                 ath11k_warn(ar->ab, "failed to %s WMI vdev %i\n",
4897                             restart ? "restart" : "start", arg.vdev_id);
4898                 return ret;
4899         }
4900
4901         ret = ath11k_mac_vdev_setup_sync(ar);
4902         if (ret) {
4903                 ath11k_warn(ab, "failed to synchronize setup for vdev %i %s: %d\n",
4904                             arg.vdev_id, restart ? "restart" : "start", ret);
4905                 return ret;
4906         }
4907
4908         ar->num_started_vdevs++;
4909         ath11k_dbg(ab, ATH11K_DBG_MAC,  "vdev %pM started, vdev_id %d\n",
4910                    arvif->vif->addr, arvif->vdev_id);
4911
4912         /* Enable CAC Flag in the driver by checking the channel DFS cac time,
4913          * i.e dfs_cac_ms value which will be valid only for radar channels
4914          * and state as NL80211_DFS_USABLE which indicates CAC needs to be
4915          * done before channel usage. This flags is used to drop rx packets.
4916          * during CAC.
4917          */
4918         /* TODO Set the flag for other interface types as required */
4919         if (arvif->vdev_type == WMI_VDEV_TYPE_AP &&
4920             chandef->chan->dfs_cac_ms &&
4921             chandef->chan->dfs_state == NL80211_DFS_USABLE) {
4922                 set_bit(ATH11K_CAC_RUNNING, &ar->dev_flags);
4923                 ath11k_dbg(ab, ATH11K_DBG_MAC,
4924                            "CAC Started in chan_freq %d for vdev %d\n",
4925                            arg.channel.freq, arg.vdev_id);
4926         }
4927
4928         ret = ath11k_mac_set_txbf_conf(arvif);
4929         if (ret)
4930                 ath11k_warn(ab, "failed to set txbf conf for vdev %d: %d\n",
4931                             arvif->vdev_id, ret);
4932
4933         return 0;
4934 }
4935
4936 static int ath11k_mac_vdev_stop(struct ath11k_vif *arvif)
4937 {
4938         struct ath11k *ar = arvif->ar;
4939         int ret;
4940
4941         lockdep_assert_held(&ar->conf_mutex);
4942
4943         reinit_completion(&ar->vdev_setup_done);
4944
4945         spin_lock_bh(&ar->data_lock);
4946
4947         ar->vdev_stop_status.stop_in_progress = true;
4948         ar->vdev_stop_status.vdev_id = arvif->vdev_id;
4949
4950         spin_unlock_bh(&ar->data_lock);
4951
4952         ret = ath11k_wmi_vdev_stop(ar, arvif->vdev_id);
4953         if (ret) {
4954                 ath11k_warn(ar->ab, "failed to stop WMI vdev %i: %d\n",
4955                             arvif->vdev_id, ret);
4956                 goto err;
4957         }
4958
4959         ret = ath11k_mac_vdev_setup_sync(ar);
4960         if (ret) {
4961                 ath11k_warn(ar->ab, "failed to synchronize setup for vdev %i: %d\n",
4962                             arvif->vdev_id, ret);
4963                 goto err;
4964         }
4965
4966         WARN_ON(ar->num_started_vdevs == 0);
4967
4968         ar->num_started_vdevs--;
4969         ath11k_dbg(ar->ab, ATH11K_DBG_MAC, "vdev %pM stopped, vdev_id %d\n",
4970                    arvif->vif->addr, arvif->vdev_id);
4971
4972         if (test_bit(ATH11K_CAC_RUNNING, &ar->dev_flags)) {
4973                 clear_bit(ATH11K_CAC_RUNNING, &ar->dev_flags);
4974                 ath11k_dbg(ar->ab, ATH11K_DBG_MAC, "CAC Stopped for vdev %d\n",
4975                            arvif->vdev_id);
4976         }
4977
4978         return 0;
4979 err:
4980         spin_lock_bh(&ar->data_lock);
4981         ar->vdev_stop_status.stop_in_progress = false;
4982         spin_unlock_bh(&ar->data_lock);
4983
4984         return ret;
4985 }
4986
4987 static int ath11k_mac_vdev_start(struct ath11k_vif *arvif,
4988                                  const struct cfg80211_chan_def *chandef)
4989 {
4990         return ath11k_mac_vdev_start_restart(arvif, chandef, false);
4991 }
4992
4993 static int ath11k_mac_vdev_restart(struct ath11k_vif *arvif,
4994                                    const struct cfg80211_chan_def *chandef)
4995 {
4996         return ath11k_mac_vdev_start_restart(arvif, chandef, true);
4997 }
4998
4999 struct ath11k_mac_change_chanctx_arg {
5000         struct ieee80211_chanctx_conf *ctx;
5001         struct ieee80211_vif_chanctx_switch *vifs;
5002         int n_vifs;
5003         int next_vif;
5004 };
5005
5006 static void
5007 ath11k_mac_change_chanctx_cnt_iter(void *data, u8 *mac,
5008                                    struct ieee80211_vif *vif)
5009 {
5010         struct ath11k_mac_change_chanctx_arg *arg = data;
5011
5012         if (rcu_access_pointer(vif->chanctx_conf) != arg->ctx)
5013                 return;
5014
5015         arg->n_vifs++;
5016 }
5017
5018 static void
5019 ath11k_mac_change_chanctx_fill_iter(void *data, u8 *mac,
5020                                     struct ieee80211_vif *vif)
5021 {
5022         struct ath11k_mac_change_chanctx_arg *arg = data;
5023         struct ieee80211_chanctx_conf *ctx;
5024
5025         ctx = rcu_access_pointer(vif->chanctx_conf);
5026         if (ctx != arg->ctx)
5027                 return;
5028
5029         if (WARN_ON(arg->next_vif == arg->n_vifs))
5030                 return;
5031
5032         arg->vifs[arg->next_vif].vif = vif;
5033         arg->vifs[arg->next_vif].old_ctx = ctx;
5034         arg->vifs[arg->next_vif].new_ctx = ctx;
5035         arg->next_vif++;
5036 }
5037
5038 static void
5039 ath11k_mac_update_vif_chan(struct ath11k *ar,
5040                            struct ieee80211_vif_chanctx_switch *vifs,
5041                            int n_vifs)
5042 {
5043         struct ath11k_base *ab = ar->ab;
5044         struct ath11k_vif *arvif;
5045         int ret;
5046         int i;
5047
5048         lockdep_assert_held(&ar->conf_mutex);
5049
5050         for (i = 0; i < n_vifs; i++) {
5051                 arvif = (void *)vifs[i].vif->drv_priv;
5052
5053                 ath11k_dbg(ab, ATH11K_DBG_MAC,
5054                            "mac chanctx switch vdev_id %i freq %hu->%hu width %d->%d\n",
5055                            arvif->vdev_id,
5056                            vifs[i].old_ctx->def.chan->center_freq,
5057                            vifs[i].new_ctx->def.chan->center_freq,
5058                            vifs[i].old_ctx->def.width,
5059                            vifs[i].new_ctx->def.width);
5060
5061                 if (WARN_ON(!arvif->is_started))
5062                         continue;
5063
5064                 if (WARN_ON(!arvif->is_up))
5065                         continue;
5066
5067                 ret = ath11k_wmi_vdev_down(ar, arvif->vdev_id);
5068                 if (ret) {
5069                         ath11k_warn(ab, "failed to down vdev %d: %d\n",
5070                                     arvif->vdev_id, ret);
5071                         continue;
5072                 }
5073         }
5074
5075         /* All relevant vdevs are downed and associated channel resources
5076          * should be available for the channel switch now.
5077          */
5078
5079         /* TODO: Update ar->rx_channel */
5080
5081         for (i = 0; i < n_vifs; i++) {
5082                 arvif = (void *)vifs[i].vif->drv_priv;
5083
5084                 if (WARN_ON(!arvif->is_started))
5085                         continue;
5086
5087                 if (WARN_ON(!arvif->is_up))
5088                         continue;
5089
5090                 ret = ath11k_mac_setup_bcn_tmpl(arvif);
5091                 if (ret)
5092                         ath11k_warn(ab, "failed to update bcn tmpl during csa: %d\n",
5093                                     ret);
5094
5095                 ret = ath11k_mac_vdev_restart(arvif, &vifs[i].new_ctx->def);
5096                 if (ret) {
5097                         ath11k_warn(ab, "failed to restart vdev %d: %d\n",
5098                                     arvif->vdev_id, ret);
5099                         continue;
5100                 }
5101
5102                 ret = ath11k_wmi_vdev_up(arvif->ar, arvif->vdev_id, arvif->aid,
5103                                          arvif->bssid);
5104                 if (ret) {
5105                         ath11k_warn(ab, "failed to bring vdev up %d: %d\n",
5106                                     arvif->vdev_id, ret);
5107                         continue;
5108                 }
5109         }
5110 }
5111
5112 static void
5113 ath11k_mac_update_active_vif_chan(struct ath11k *ar,
5114                                   struct ieee80211_chanctx_conf *ctx)
5115 {
5116         struct ath11k_mac_change_chanctx_arg arg = { .ctx = ctx };
5117
5118         lockdep_assert_held(&ar->conf_mutex);
5119
5120         ieee80211_iterate_active_interfaces_atomic(ar->hw,
5121                                                    IEEE80211_IFACE_ITER_NORMAL,
5122                                                    ath11k_mac_change_chanctx_cnt_iter,
5123                                                    &arg);
5124         if (arg.n_vifs == 0)
5125                 return;
5126
5127         arg.vifs = kcalloc(arg.n_vifs, sizeof(arg.vifs[0]), GFP_KERNEL);
5128         if (!arg.vifs)
5129                 return;
5130
5131         ieee80211_iterate_active_interfaces_atomic(ar->hw,
5132                                                    IEEE80211_IFACE_ITER_NORMAL,
5133                                                    ath11k_mac_change_chanctx_fill_iter,
5134                                                    &arg);
5135
5136         ath11k_mac_update_vif_chan(ar, arg.vifs, arg.n_vifs);
5137
5138         kfree(arg.vifs);
5139 }
5140
5141 static void ath11k_mac_op_change_chanctx(struct ieee80211_hw *hw,
5142                                          struct ieee80211_chanctx_conf *ctx,
5143                                          u32 changed)
5144 {
5145         struct ath11k *ar = hw->priv;
5146         struct ath11k_base *ab = ar->ab;
5147
5148         mutex_lock(&ar->conf_mutex);
5149
5150         ath11k_dbg(ab, ATH11K_DBG_MAC,
5151                    "mac chanctx change freq %hu width %d ptr %pK changed %x\n",
5152                    ctx->def.chan->center_freq, ctx->def.width, ctx, changed);
5153
5154         /* This shouldn't really happen because channel switching should use
5155          * switch_vif_chanctx().
5156          */
5157         if (WARN_ON(changed & IEEE80211_CHANCTX_CHANGE_CHANNEL))
5158                 goto unlock;
5159
5160         if (changed & IEEE80211_CHANCTX_CHANGE_WIDTH)
5161                 ath11k_mac_update_active_vif_chan(ar, ctx);
5162
5163         /* TODO: Recalc radar detection */
5164
5165 unlock:
5166         mutex_unlock(&ar->conf_mutex);
5167 }
5168
5169 static int ath11k_start_vdev_delay(struct ieee80211_hw *hw,
5170                                    struct ieee80211_vif *vif)
5171 {
5172         struct ath11k *ar = hw->priv;
5173         struct ath11k_base *ab = ar->ab;
5174         struct ath11k_vif *arvif = (void *)vif->drv_priv;
5175         int ret;
5176
5177         if (WARN_ON(arvif->is_started))
5178                 return -EBUSY;
5179
5180         ret = ath11k_mac_vdev_start(arvif, &arvif->chanctx.def);
5181         if (ret) {
5182                 ath11k_warn(ab, "failed to start vdev %i addr %pM on freq %d: %d\n",
5183                             arvif->vdev_id, vif->addr,
5184                             arvif->chanctx.def.chan->center_freq, ret);
5185                 return ret;
5186         }
5187
5188         if (arvif->vdev_type == WMI_VDEV_TYPE_MONITOR) {
5189                 ret = ath11k_monitor_vdev_up(ar, arvif->vdev_id);
5190                 if (ret) {
5191                         ath11k_warn(ab, "failed put monitor up: %d\n", ret);
5192                         return ret;
5193                 }
5194         }
5195
5196         arvif->is_started = true;
5197
5198         /* TODO: Setup ps and cts/rts protection */
5199         return 0;
5200 }
5201
5202 static int
5203 ath11k_mac_op_assign_vif_chanctx(struct ieee80211_hw *hw,
5204                                  struct ieee80211_vif *vif,
5205                                  struct ieee80211_chanctx_conf *ctx)
5206 {
5207         struct ath11k *ar = hw->priv;
5208         struct ath11k_base *ab = ar->ab;
5209         struct ath11k_vif *arvif = (void *)vif->drv_priv;
5210         int ret;
5211
5212         mutex_lock(&ar->conf_mutex);
5213
5214         ath11k_dbg(ab, ATH11K_DBG_MAC,
5215                    "mac chanctx assign ptr %pK vdev_id %i\n",
5216                    ctx, arvif->vdev_id);
5217
5218         /* for QCA6390 bss peer must be created before vdev_start */
5219         if (ab->hw_params.vdev_start_delay &&
5220             arvif->vdev_type != WMI_VDEV_TYPE_AP) {
5221                 memcpy(&arvif->chanctx, ctx, sizeof(*ctx));
5222                 mutex_unlock(&ar->conf_mutex);
5223                 return 0;
5224         }
5225
5226         if (WARN_ON(arvif->is_started)) {
5227                 mutex_unlock(&ar->conf_mutex);
5228                 return -EBUSY;
5229         }
5230
5231         ret = ath11k_mac_vdev_start(arvif, &ctx->def);
5232         if (ret) {
5233                 ath11k_warn(ab, "failed to start vdev %i addr %pM on freq %d: %d\n",
5234                             arvif->vdev_id, vif->addr,
5235                             ctx->def.chan->center_freq, ret);
5236                 goto err;
5237         }
5238         if (arvif->vdev_type == WMI_VDEV_TYPE_MONITOR) {
5239                 ret = ath11k_monitor_vdev_up(ar, arvif->vdev_id);
5240                 if (ret)
5241                         goto err;
5242         }
5243
5244         arvif->is_started = true;
5245
5246         /* TODO: Setup ps and cts/rts protection */
5247
5248         mutex_unlock(&ar->conf_mutex);
5249
5250         return 0;
5251
5252 err:
5253         mutex_unlock(&ar->conf_mutex);
5254
5255         return ret;
5256 }
5257
5258 static void
5259 ath11k_mac_op_unassign_vif_chanctx(struct ieee80211_hw *hw,
5260                                    struct ieee80211_vif *vif,
5261                                    struct ieee80211_chanctx_conf *ctx)
5262 {
5263         struct ath11k *ar = hw->priv;
5264         struct ath11k_base *ab = ar->ab;
5265         struct ath11k_vif *arvif = (void *)vif->drv_priv;
5266         int ret;
5267
5268         mutex_lock(&ar->conf_mutex);
5269
5270         ath11k_dbg(ab, ATH11K_DBG_MAC,
5271                    "mac chanctx unassign ptr %pK vdev_id %i\n",
5272                    ctx, arvif->vdev_id);
5273
5274         WARN_ON(!arvif->is_started);
5275
5276         ret = ath11k_mac_vdev_stop(arvif);
5277         if (ret)
5278                 ath11k_warn(ab, "failed to stop vdev %i: %d\n",
5279                             arvif->vdev_id, ret);
5280
5281         arvif->is_started = false;
5282
5283         mutex_unlock(&ar->conf_mutex);
5284 }
5285
5286 static int
5287 ath11k_mac_op_switch_vif_chanctx(struct ieee80211_hw *hw,
5288                                  struct ieee80211_vif_chanctx_switch *vifs,
5289                                  int n_vifs,
5290                                  enum ieee80211_chanctx_switch_mode mode)
5291 {
5292         struct ath11k *ar = hw->priv;
5293
5294         mutex_lock(&ar->conf_mutex);
5295
5296         ath11k_dbg(ar->ab, ATH11K_DBG_MAC,
5297                    "mac chanctx switch n_vifs %d mode %d\n",
5298                    n_vifs, mode);
5299         ath11k_mac_update_vif_chan(ar, vifs, n_vifs);
5300
5301         mutex_unlock(&ar->conf_mutex);
5302
5303         return 0;
5304 }
5305
5306 static int
5307 ath11k_set_vdev_param_to_all_vifs(struct ath11k *ar, int param, u32 value)
5308 {
5309         struct ath11k_vif *arvif;
5310         int ret = 0;
5311
5312         mutex_lock(&ar->conf_mutex);
5313         list_for_each_entry(arvif, &ar->arvifs, list) {
5314                 ath11k_dbg(ar->ab, ATH11K_DBG_MAC, "setting mac vdev %d param %d value %d\n",
5315                            param, arvif->vdev_id, value);
5316
5317                 ret = ath11k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
5318                                                     param, value);
5319                 if (ret) {
5320                         ath11k_warn(ar->ab, "failed to set param %d for vdev %d: %d\n",
5321                                     param, arvif->vdev_id, ret);
5322                         break;
5323                 }
5324         }
5325         mutex_unlock(&ar->conf_mutex);
5326         return ret;
5327 }
5328
5329 /* mac80211 stores device specific RTS/Fragmentation threshold value,
5330  * this is set interface specific to firmware from ath11k driver
5331  */
5332 static int ath11k_mac_op_set_rts_threshold(struct ieee80211_hw *hw, u32 value)
5333 {
5334         struct ath11k *ar = hw->priv;
5335         int param_id = WMI_VDEV_PARAM_RTS_THRESHOLD;
5336
5337         return ath11k_set_vdev_param_to_all_vifs(ar, param_id, value);
5338 }
5339
5340 static int ath11k_mac_op_set_frag_threshold(struct ieee80211_hw *hw, u32 value)
5341 {
5342         /* Even though there's a WMI vdev param for fragmentation threshold no
5343          * known firmware actually implements it. Moreover it is not possible to
5344          * rely frame fragmentation to mac80211 because firmware clears the
5345          * "more fragments" bit in frame control making it impossible for remote
5346          * devices to reassemble frames.
5347          *
5348          * Hence implement a dummy callback just to say fragmentation isn't
5349          * supported. This effectively prevents mac80211 from doing frame
5350          * fragmentation in software.
5351          */
5352         return -EOPNOTSUPP;
5353 }
5354
5355 static void ath11k_mac_op_flush(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
5356                                 u32 queues, bool drop)
5357 {
5358         struct ath11k *ar = hw->priv;
5359         long time_left;
5360
5361         if (drop)
5362                 return;
5363
5364         time_left = wait_event_timeout(ar->dp.tx_empty_waitq,
5365                                        (atomic_read(&ar->dp.num_tx_pending) == 0),
5366                                        ATH11K_FLUSH_TIMEOUT);
5367         if (time_left == 0)
5368                 ath11k_warn(ar->ab, "failed to flush transmit queue %ld\n", time_left);
5369 }
5370
5371 static int
5372 ath11k_mac_bitrate_mask_num_ht_rates(struct ath11k *ar,
5373                                      enum nl80211_band band,
5374                                      const struct cfg80211_bitrate_mask *mask)
5375 {
5376         int num_rates = 0;
5377         int i;
5378
5379         for (i = 0; i < ARRAY_SIZE(mask->control[band].ht_mcs); i++)
5380                 num_rates += hweight16(mask->control[band].ht_mcs[i]);
5381
5382         return num_rates;
5383 }
5384
5385 static bool
5386 ath11k_mac_has_single_legacy_rate(struct ath11k *ar,
5387                                   enum nl80211_band band,
5388                                   const struct cfg80211_bitrate_mask *mask)
5389 {
5390         int num_rates = 0;
5391
5392         num_rates = hweight32(mask->control[band].legacy);
5393
5394         if (ath11k_mac_bitrate_mask_num_ht_rates(ar, band, mask))
5395                 return false;
5396
5397         if (ath11k_mac_bitrate_mask_num_vht_rates(ar, band, mask))
5398                 return false;
5399
5400         return num_rates == 1;
5401 }
5402
5403 static bool
5404 ath11k_mac_bitrate_mask_get_single_nss(struct ath11k *ar,
5405                                        enum nl80211_band band,
5406                                        const struct cfg80211_bitrate_mask *mask,
5407                                        int *nss)
5408 {
5409         struct ieee80211_supported_band *sband = &ar->mac.sbands[band];
5410         u16 vht_mcs_map = le16_to_cpu(sband->vht_cap.vht_mcs.tx_mcs_map);
5411         u8 ht_nss_mask = 0;
5412         u8 vht_nss_mask = 0;
5413         int i;
5414
5415         /* No need to consider legacy here. Basic rates are always present
5416          * in bitrate mask
5417          */
5418
5419         for (i = 0; i < ARRAY_SIZE(mask->control[band].ht_mcs); i++) {
5420                 if (mask->control[band].ht_mcs[i] == 0)
5421                         continue;
5422                 else if (mask->control[band].ht_mcs[i] ==
5423                          sband->ht_cap.mcs.rx_mask[i])
5424                         ht_nss_mask |= BIT(i);
5425                 else
5426                         return false;
5427         }
5428
5429         for (i = 0; i < ARRAY_SIZE(mask->control[band].vht_mcs); i++) {
5430                 if (mask->control[band].vht_mcs[i] == 0)
5431                         continue;
5432                 else if (mask->control[band].vht_mcs[i] ==
5433                          ath11k_mac_get_max_vht_mcs_map(vht_mcs_map, i))
5434                         vht_nss_mask |= BIT(i);
5435                 else
5436                         return false;
5437         }
5438
5439         if (ht_nss_mask != vht_nss_mask)
5440                 return false;
5441
5442         if (ht_nss_mask == 0)
5443                 return false;
5444
5445         if (BIT(fls(ht_nss_mask)) - 1 != ht_nss_mask)
5446                 return false;
5447
5448         *nss = fls(ht_nss_mask);
5449
5450         return true;
5451 }
5452
5453 static int
5454 ath11k_mac_get_single_legacy_rate(struct ath11k *ar,
5455                                   enum nl80211_band band,
5456                                   const struct cfg80211_bitrate_mask *mask,
5457                                   u32 *rate, u8 *nss)
5458 {
5459         int rate_idx;
5460         u16 bitrate;
5461         u8 preamble;
5462         u8 hw_rate;
5463
5464         if (hweight32(mask->control[band].legacy) != 1)
5465                 return -EINVAL;
5466
5467         rate_idx = ffs(mask->control[band].legacy) - 1;
5468
5469         if (band == NL80211_BAND_5GHZ || band == NL80211_BAND_6GHZ)
5470                 rate_idx += ATH11K_MAC_FIRST_OFDM_RATE_IDX;
5471
5472         hw_rate = ath11k_legacy_rates[rate_idx].hw_value;
5473         bitrate = ath11k_legacy_rates[rate_idx].bitrate;
5474
5475         if (ath11k_mac_bitrate_is_cck(bitrate))
5476                 preamble = WMI_RATE_PREAMBLE_CCK;
5477         else
5478                 preamble = WMI_RATE_PREAMBLE_OFDM;
5479
5480         *nss = 1;
5481         *rate = ATH11K_HW_RATE_CODE(hw_rate, 0, preamble);
5482
5483         return 0;
5484 }
5485
5486 static int ath11k_mac_set_fixed_rate_params(struct ath11k_vif *arvif,
5487                                             u32 rate, u8 nss, u8 sgi, u8 ldpc)
5488 {
5489         struct ath11k *ar = arvif->ar;
5490         u32 vdev_param;
5491         int ret;
5492
5493         lockdep_assert_held(&ar->conf_mutex);
5494
5495         ath11k_dbg(ar->ab, ATH11K_DBG_MAC, "mac set fixed rate params vdev %i rate 0x%02hhx nss %hhu sgi %hhu\n",
5496                    arvif->vdev_id, rate, nss, sgi);
5497
5498         vdev_param = WMI_VDEV_PARAM_FIXED_RATE;
5499         ret = ath11k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
5500                                             vdev_param, rate);
5501         if (ret) {
5502                 ath11k_warn(ar->ab, "failed to set fixed rate param 0x%02x: %d\n",
5503                             rate, ret);
5504                 return ret;
5505         }
5506
5507         vdev_param = WMI_VDEV_PARAM_NSS;
5508         ret = ath11k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
5509                                             vdev_param, nss);
5510         if (ret) {
5511                 ath11k_warn(ar->ab, "failed to set nss param %d: %d\n",
5512                             nss, ret);
5513                 return ret;
5514         }
5515
5516         vdev_param = WMI_VDEV_PARAM_SGI;
5517         ret = ath11k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
5518                                             vdev_param, sgi);
5519         if (ret) {
5520                 ath11k_warn(ar->ab, "failed to set sgi param %d: %d\n",
5521                             sgi, ret);
5522                 return ret;
5523         }
5524
5525         vdev_param = WMI_VDEV_PARAM_LDPC;
5526         ret = ath11k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
5527                                             vdev_param, ldpc);
5528         if (ret) {
5529                 ath11k_warn(ar->ab, "failed to set ldpc param %d: %d\n",
5530                             ldpc, ret);
5531                 return ret;
5532         }
5533
5534         return 0;
5535 }
5536
5537 static bool
5538 ath11k_mac_vht_mcs_range_present(struct ath11k *ar,
5539                                  enum nl80211_band band,
5540                                  const struct cfg80211_bitrate_mask *mask)
5541 {
5542         int i;
5543         u16 vht_mcs;
5544
5545         for (i = 0; i < NL80211_VHT_NSS_MAX; i++) {
5546                 vht_mcs = mask->control[band].vht_mcs[i];
5547
5548                 switch (vht_mcs) {
5549                 case 0:
5550                 case BIT(8) - 1:
5551                 case BIT(9) - 1:
5552                 case BIT(10) - 1:
5553                         break;
5554                 default:
5555                         return false;
5556                 }
5557         }
5558
5559         return true;
5560 }
5561
5562 static void ath11k_mac_set_bitrate_mask_iter(void *data,
5563                                              struct ieee80211_sta *sta)
5564 {
5565         struct ath11k_vif *arvif = data;
5566         struct ath11k_sta *arsta = (struct ath11k_sta *)sta->drv_priv;
5567         struct ath11k *ar = arvif->ar;
5568
5569         spin_lock_bh(&ar->data_lock);
5570         arsta->changed |= IEEE80211_RC_SUPP_RATES_CHANGED;
5571         spin_unlock_bh(&ar->data_lock);
5572
5573         ieee80211_queue_work(ar->hw, &arsta->update_wk);
5574 }
5575
5576 static void ath11k_mac_disable_peer_fixed_rate(void *data,
5577                                                struct ieee80211_sta *sta)
5578 {
5579         struct ath11k_vif *arvif = data;
5580         struct ath11k *ar = arvif->ar;
5581         int ret;
5582
5583         ret = ath11k_wmi_set_peer_param(ar, sta->addr,
5584                                         arvif->vdev_id,
5585                                         WMI_PEER_PARAM_FIXED_RATE,
5586                                         WMI_FIXED_RATE_NONE);
5587         if (ret)
5588                 ath11k_warn(ar->ab,
5589                             "failed to disable peer fixed rate for STA %pM ret %d\n",
5590                             sta->addr, ret);
5591 }
5592
5593 static int
5594 ath11k_mac_op_set_bitrate_mask(struct ieee80211_hw *hw,
5595                                struct ieee80211_vif *vif,
5596                                const struct cfg80211_bitrate_mask *mask)
5597 {
5598         struct ath11k_vif *arvif = (void *)vif->drv_priv;
5599         struct cfg80211_chan_def def;
5600         struct ath11k *ar = arvif->ar;
5601         enum nl80211_band band;
5602         const u8 *ht_mcs_mask;
5603         const u16 *vht_mcs_mask;
5604         u32 rate;
5605         u8 nss;
5606         u8 sgi;
5607         u8 ldpc;
5608         int single_nss;
5609         int ret;
5610         int num_rates;
5611
5612         if (ath11k_mac_vif_chan(vif, &def))
5613                 return -EPERM;
5614
5615         band = def.chan->band;
5616         ht_mcs_mask = mask->control[band].ht_mcs;
5617         vht_mcs_mask = mask->control[band].vht_mcs;
5618         ldpc = !!(ar->ht_cap_info & WMI_HT_CAP_LDPC);
5619
5620         sgi = mask->control[band].gi;
5621         if (sgi == NL80211_TXRATE_FORCE_LGI)
5622                 return -EINVAL;
5623
5624         /* mac80211 doesn't support sending a fixed HT/VHT MCS alone, rather it
5625          * requires passing atleast one of used basic rates along with them.
5626          * Fixed rate setting across different preambles(legacy, HT, VHT) is
5627          * not supported by the FW. Hence use of FIXED_RATE vdev param is not
5628          * suitable for setting single HT/VHT rates.
5629          * But, there could be a single basic rate passed from userspace which
5630          * can be done through the FIXED_RATE param.
5631          */
5632         if (ath11k_mac_has_single_legacy_rate(ar, band, mask)) {
5633                 ret = ath11k_mac_get_single_legacy_rate(ar, band, mask, &rate,
5634                                                         &nss);
5635                 if (ret) {
5636                         ath11k_warn(ar->ab, "failed to get single legacy rate for vdev %i: %d\n",
5637                                     arvif->vdev_id, ret);
5638                         return ret;
5639                 }
5640                 ieee80211_iterate_stations_atomic(ar->hw,
5641                                                   ath11k_mac_disable_peer_fixed_rate,
5642                                                   arvif);
5643         } else if (ath11k_mac_bitrate_mask_get_single_nss(ar, band, mask,
5644                                                           &single_nss)) {
5645                 rate = WMI_FIXED_RATE_NONE;
5646                 nss = single_nss;
5647         } else {
5648                 rate = WMI_FIXED_RATE_NONE;
5649                 nss = min_t(u32, ar->num_tx_chains,
5650                             max(ath11k_mac_max_ht_nss(ht_mcs_mask),
5651                                 ath11k_mac_max_vht_nss(vht_mcs_mask)));
5652
5653                 /* If multiple rates across different preambles are given
5654                  * we can reconfigure this info with all peers using PEER_ASSOC
5655                  * command with the below exception cases.
5656                  * - Single VHT Rate : peer_assoc command accommodates only MCS
5657                  * range values i.e 0-7, 0-8, 0-9 for VHT. Though mac80211
5658                  * mandates passing basic rates along with HT/VHT rates, FW
5659                  * doesn't allow switching from VHT to Legacy. Hence instead of
5660                  * setting legacy and VHT rates using RATEMASK_CMD vdev cmd,
5661                  * we could set this VHT rate as peer fixed rate param, which
5662                  * will override FIXED rate and FW rate control algorithm.
5663                  * If single VHT rate is passed along with HT rates, we select
5664                  * the VHT rate as fixed rate for vht peers.
5665                  * - Multiple VHT Rates : When Multiple VHT rates are given,this
5666                  * can be set using RATEMASK CMD which uses FW rate-ctl alg.
5667                  * TODO: Setting multiple VHT MCS and replacing peer_assoc with
5668                  * RATEMASK_CMDID can cover all use cases of setting rates
5669                  * across multiple preambles and rates within same type.
5670                  * But requires more validation of the command at this point.
5671                  */
5672
5673                 num_rates = ath11k_mac_bitrate_mask_num_vht_rates(ar, band,
5674                                                                   mask);
5675
5676                 if (!ath11k_mac_vht_mcs_range_present(ar, band, mask) &&
5677                     num_rates > 1) {
5678                         /* TODO: Handle multiple VHT MCS values setting using
5679                          * RATEMASK CMD
5680                          */
5681                         ath11k_warn(ar->ab,
5682                                     "Setting more than one MCS Value in bitrate mask not supported\n");
5683                         return -EINVAL;
5684                 }
5685
5686                 ieee80211_iterate_stations_atomic(ar->hw,
5687                                                   ath11k_mac_disable_peer_fixed_rate,
5688                                                   arvif);
5689
5690                 mutex_lock(&ar->conf_mutex);
5691
5692                 arvif->bitrate_mask = *mask;
5693                 ieee80211_iterate_stations_atomic(ar->hw,
5694                                                   ath11k_mac_set_bitrate_mask_iter,
5695                                                   arvif);
5696
5697                 mutex_unlock(&ar->conf_mutex);
5698         }
5699
5700         mutex_lock(&ar->conf_mutex);
5701
5702         ret = ath11k_mac_set_fixed_rate_params(arvif, rate, nss, sgi, ldpc);
5703         if (ret) {
5704                 ath11k_warn(ar->ab, "failed to set fixed rate params on vdev %i: %d\n",
5705                             arvif->vdev_id, ret);
5706         }
5707
5708         mutex_unlock(&ar->conf_mutex);
5709
5710         return ret;
5711 }
5712
5713 static void
5714 ath11k_mac_op_reconfig_complete(struct ieee80211_hw *hw,
5715                                 enum ieee80211_reconfig_type reconfig_type)
5716 {
5717         struct ath11k *ar = hw->priv;
5718
5719         if (reconfig_type != IEEE80211_RECONFIG_TYPE_RESTART)
5720                 return;
5721
5722         mutex_lock(&ar->conf_mutex);
5723
5724         if (ar->state == ATH11K_STATE_RESTARTED) {
5725                 ath11k_warn(ar->ab, "pdev %d successfully recovered\n",
5726                             ar->pdev->pdev_id);
5727                 ar->state = ATH11K_STATE_ON;
5728                 ieee80211_wake_queues(ar->hw);
5729         }
5730
5731         mutex_unlock(&ar->conf_mutex);
5732 }
5733
5734 static void
5735 ath11k_mac_update_bss_chan_survey(struct ath11k *ar,
5736                                   struct ieee80211_channel *channel)
5737 {
5738         int ret;
5739         enum wmi_bss_chan_info_req_type type = WMI_BSS_SURVEY_REQ_TYPE_READ;
5740
5741         lockdep_assert_held(&ar->conf_mutex);
5742
5743         if (!test_bit(WMI_TLV_SERVICE_BSS_CHANNEL_INFO_64, ar->ab->wmi_ab.svc_map) ||
5744             ar->rx_channel != channel)
5745                 return;
5746
5747         if (ar->scan.state != ATH11K_SCAN_IDLE) {
5748                 ath11k_dbg(ar->ab, ATH11K_DBG_MAC,
5749                            "ignoring bss chan info req while scanning..\n");
5750                 return;
5751         }
5752
5753         reinit_completion(&ar->bss_survey_done);
5754
5755         ret = ath11k_wmi_pdev_bss_chan_info_request(ar, type);
5756         if (ret) {
5757                 ath11k_warn(ar->ab, "failed to send pdev bss chan info request\n");
5758                 return;
5759         }
5760
5761         ret = wait_for_completion_timeout(&ar->bss_survey_done, 3 * HZ);
5762         if (ret == 0)
5763                 ath11k_warn(ar->ab, "bss channel survey timed out\n");
5764 }
5765
5766 static int ath11k_mac_op_get_survey(struct ieee80211_hw *hw, int idx,
5767                                     struct survey_info *survey)
5768 {
5769         struct ath11k *ar = hw->priv;
5770         struct ieee80211_supported_band *sband;
5771         struct survey_info *ar_survey;
5772         int ret = 0;
5773
5774         if (idx >= ATH11K_NUM_CHANS)
5775                 return -ENOENT;
5776
5777         ar_survey = &ar->survey[idx];
5778
5779         mutex_lock(&ar->conf_mutex);
5780
5781         sband = hw->wiphy->bands[NL80211_BAND_2GHZ];
5782         if (sband && idx >= sband->n_channels) {
5783                 idx -= sband->n_channels;
5784                 sband = NULL;
5785         }
5786
5787         if (!sband)
5788                 sband = hw->wiphy->bands[NL80211_BAND_5GHZ];
5789
5790         if (!sband || idx >= sband->n_channels) {
5791                 ret = -ENOENT;
5792                 goto exit;
5793         }
5794
5795         ath11k_mac_update_bss_chan_survey(ar, &sband->channels[idx]);
5796
5797         spin_lock_bh(&ar->data_lock);
5798         memcpy(survey, ar_survey, sizeof(*survey));
5799         spin_unlock_bh(&ar->data_lock);
5800
5801         survey->channel = &sband->channels[idx];
5802
5803         if (ar->rx_channel == survey->channel)
5804                 survey->filled |= SURVEY_INFO_IN_USE;
5805
5806 exit:
5807         mutex_unlock(&ar->conf_mutex);
5808         return ret;
5809 }
5810
5811 static void ath11k_mac_op_sta_statistics(struct ieee80211_hw *hw,
5812                                          struct ieee80211_vif *vif,
5813                                          struct ieee80211_sta *sta,
5814                                          struct station_info *sinfo)
5815 {
5816         struct ath11k_sta *arsta = (struct ath11k_sta *)sta->drv_priv;
5817
5818         sinfo->rx_duration = arsta->rx_duration;
5819         sinfo->filled |= BIT_ULL(NL80211_STA_INFO_RX_DURATION);
5820
5821         sinfo->tx_duration = arsta->tx_duration;
5822         sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_DURATION);
5823
5824         if (!arsta->txrate.legacy && !arsta->txrate.nss)
5825                 return;
5826
5827         if (arsta->txrate.legacy) {
5828                 sinfo->txrate.legacy = arsta->txrate.legacy;
5829         } else {
5830                 sinfo->txrate.mcs = arsta->txrate.mcs;
5831                 sinfo->txrate.nss = arsta->txrate.nss;
5832                 sinfo->txrate.bw = arsta->txrate.bw;
5833                 sinfo->txrate.he_gi = arsta->txrate.he_gi;
5834                 sinfo->txrate.he_dcm = arsta->txrate.he_dcm;
5835                 sinfo->txrate.he_ru_alloc = arsta->txrate.he_ru_alloc;
5836         }
5837         sinfo->txrate.flags = arsta->txrate.flags;
5838         sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_BITRATE);
5839
5840         /* TODO: Use real NF instead of default one. */
5841         sinfo->signal = arsta->rssi_comb + ATH11K_DEFAULT_NOISE_FLOOR;
5842 }
5843
5844 static const struct ieee80211_ops ath11k_ops = {
5845         .tx                             = ath11k_mac_op_tx,
5846         .start                          = ath11k_mac_op_start,
5847         .stop                           = ath11k_mac_op_stop,
5848         .reconfig_complete              = ath11k_mac_op_reconfig_complete,
5849         .add_interface                  = ath11k_mac_op_add_interface,
5850         .remove_interface               = ath11k_mac_op_remove_interface,
5851         .update_vif_offload             = ath11k_mac_op_update_vif_offload,
5852         .config                         = ath11k_mac_op_config,
5853         .bss_info_changed               = ath11k_mac_op_bss_info_changed,
5854         .configure_filter               = ath11k_mac_op_configure_filter,
5855         .hw_scan                        = ath11k_mac_op_hw_scan,
5856         .cancel_hw_scan                 = ath11k_mac_op_cancel_hw_scan,
5857         .set_key                        = ath11k_mac_op_set_key,
5858         .sta_state                      = ath11k_mac_op_sta_state,
5859         .sta_set_txpwr                  = ath11k_mac_op_sta_set_txpwr,
5860         .sta_rc_update                  = ath11k_mac_op_sta_rc_update,
5861         .conf_tx                        = ath11k_mac_op_conf_tx,
5862         .set_antenna                    = ath11k_mac_op_set_antenna,
5863         .get_antenna                    = ath11k_mac_op_get_antenna,
5864         .ampdu_action                   = ath11k_mac_op_ampdu_action,
5865         .add_chanctx                    = ath11k_mac_op_add_chanctx,
5866         .remove_chanctx                 = ath11k_mac_op_remove_chanctx,
5867         .change_chanctx                 = ath11k_mac_op_change_chanctx,
5868         .assign_vif_chanctx             = ath11k_mac_op_assign_vif_chanctx,
5869         .unassign_vif_chanctx           = ath11k_mac_op_unassign_vif_chanctx,
5870         .switch_vif_chanctx             = ath11k_mac_op_switch_vif_chanctx,
5871         .set_rts_threshold              = ath11k_mac_op_set_rts_threshold,
5872         .set_frag_threshold             = ath11k_mac_op_set_frag_threshold,
5873         .set_bitrate_mask               = ath11k_mac_op_set_bitrate_mask,
5874         .get_survey                     = ath11k_mac_op_get_survey,
5875         .flush                          = ath11k_mac_op_flush,
5876         .sta_statistics                 = ath11k_mac_op_sta_statistics,
5877         CFG80211_TESTMODE_CMD(ath11k_tm_cmd)
5878 #ifdef CONFIG_ATH11K_DEBUGFS
5879         .sta_add_debugfs                = ath11k_debugfs_sta_op_add,
5880 #endif
5881 };
5882
5883 static void ath11k_mac_update_ch_list(struct ath11k *ar,
5884                                       struct ieee80211_supported_band *band,
5885                                       u32 freq_low, u32 freq_high)
5886 {
5887         int i;
5888
5889         if (!(freq_low && freq_high))
5890                 return;
5891
5892         for (i = 0; i < band->n_channels; i++) {
5893                 if (band->channels[i].center_freq < freq_low ||
5894                     band->channels[i].center_freq > freq_high)
5895                         band->channels[i].flags |= IEEE80211_CHAN_DISABLED;
5896         }
5897 }
5898
5899 static u32 ath11k_get_phy_id(struct ath11k *ar, u32 band)
5900 {
5901         struct ath11k_pdev *pdev = ar->pdev;
5902         struct ath11k_pdev_cap *pdev_cap = &pdev->cap;
5903
5904         if (band == WMI_HOST_WLAN_2G_CAP)
5905                 return pdev_cap->band[NL80211_BAND_2GHZ].phy_id;
5906
5907         if (band == WMI_HOST_WLAN_5G_CAP)
5908                 return pdev_cap->band[NL80211_BAND_5GHZ].phy_id;
5909
5910         ath11k_warn(ar->ab, "unsupported phy cap:%d\n", band);
5911
5912         return 0;
5913 }
5914
5915 static int ath11k_mac_setup_channels_rates(struct ath11k *ar,
5916                                            u32 supported_bands)
5917 {
5918         struct ieee80211_supported_band *band;
5919         struct ath11k_hal_reg_capabilities_ext *reg_cap;
5920         void *channels;
5921         u32 phy_id;
5922
5923         BUILD_BUG_ON((ARRAY_SIZE(ath11k_2ghz_channels) +
5924                       ARRAY_SIZE(ath11k_5ghz_channels) +
5925                       ARRAY_SIZE(ath11k_6ghz_channels)) !=
5926                      ATH11K_NUM_CHANS);
5927
5928         reg_cap = &ar->ab->hal_reg_cap[ar->pdev_idx];
5929
5930         if (supported_bands & WMI_HOST_WLAN_2G_CAP) {
5931                 channels = kmemdup(ath11k_2ghz_channels,
5932                                    sizeof(ath11k_2ghz_channels),
5933                                    GFP_KERNEL);
5934                 if (!channels)
5935                         return -ENOMEM;
5936
5937                 band = &ar->mac.sbands[NL80211_BAND_2GHZ];
5938                 band->band = NL80211_BAND_2GHZ;
5939                 band->n_channels = ARRAY_SIZE(ath11k_2ghz_channels);
5940                 band->channels = channels;
5941                 band->n_bitrates = ath11k_g_rates_size;
5942                 band->bitrates = ath11k_g_rates;
5943                 ar->hw->wiphy->bands[NL80211_BAND_2GHZ] = band;
5944
5945                 if (ar->ab->hw_params.single_pdev_only) {
5946                         phy_id = ath11k_get_phy_id(ar, WMI_HOST_WLAN_2G_CAP);
5947                         reg_cap = &ar->ab->hal_reg_cap[phy_id];
5948                 }
5949                 ath11k_mac_update_ch_list(ar, band,
5950                                           reg_cap->low_2ghz_chan,
5951                                           reg_cap->high_2ghz_chan);
5952         }
5953
5954         if (supported_bands & WMI_HOST_WLAN_5G_CAP) {
5955                 if (reg_cap->high_5ghz_chan >= ATH11K_MAX_6G_FREQ) {
5956                         channels = kmemdup(ath11k_6ghz_channels,
5957                                            sizeof(ath11k_6ghz_channels), GFP_KERNEL);
5958                         if (!channels) {
5959                                 kfree(ar->mac.sbands[NL80211_BAND_2GHZ].channels);
5960                                 return -ENOMEM;
5961                         }
5962
5963                         ar->supports_6ghz = true;
5964                         band = &ar->mac.sbands[NL80211_BAND_6GHZ];
5965                         band->band = NL80211_BAND_6GHZ;
5966                         band->n_channels = ARRAY_SIZE(ath11k_6ghz_channels);
5967                         band->channels = channels;
5968                         band->n_bitrates = ath11k_a_rates_size;
5969                         band->bitrates = ath11k_a_rates;
5970                         ar->hw->wiphy->bands[NL80211_BAND_6GHZ] = band;
5971                         ath11k_mac_update_ch_list(ar, band,
5972                                                   reg_cap->low_5ghz_chan,
5973                                                   reg_cap->high_5ghz_chan);
5974                 }
5975
5976                 if (reg_cap->low_5ghz_chan < ATH11K_MIN_6G_FREQ) {
5977                         channels = kmemdup(ath11k_5ghz_channels,
5978                                            sizeof(ath11k_5ghz_channels),
5979                                            GFP_KERNEL);
5980                         if (!channels) {
5981                                 kfree(ar->mac.sbands[NL80211_BAND_2GHZ].channels);
5982                                 kfree(ar->mac.sbands[NL80211_BAND_6GHZ].channels);
5983                                 return -ENOMEM;
5984                         }
5985
5986                         band = &ar->mac.sbands[NL80211_BAND_5GHZ];
5987                         band->band = NL80211_BAND_5GHZ;
5988                         band->n_channels = ARRAY_SIZE(ath11k_5ghz_channels);
5989                         band->channels = channels;
5990                         band->n_bitrates = ath11k_a_rates_size;
5991                         band->bitrates = ath11k_a_rates;
5992                         ar->hw->wiphy->bands[NL80211_BAND_5GHZ] = band;
5993
5994                         if (ar->ab->hw_params.single_pdev_only) {
5995                                 phy_id = ath11k_get_phy_id(ar, WMI_HOST_WLAN_5G_CAP);
5996                                 reg_cap = &ar->ab->hal_reg_cap[phy_id];
5997                         }
5998
5999                         ath11k_mac_update_ch_list(ar, band,
6000                                                   reg_cap->low_5ghz_chan,
6001                                                   reg_cap->high_5ghz_chan);
6002                 }
6003         }
6004
6005         return 0;
6006 }
6007
6008 static int ath11k_mac_setup_iface_combinations(struct ath11k *ar)
6009 {
6010         struct ath11k_base *ab = ar->ab;
6011         struct ieee80211_iface_combination *combinations;
6012         struct ieee80211_iface_limit *limits;
6013         int n_limits;
6014
6015         combinations = kzalloc(sizeof(*combinations), GFP_KERNEL);
6016         if (!combinations)
6017                 return -ENOMEM;
6018
6019         n_limits = 2;
6020
6021         limits = kcalloc(n_limits, sizeof(*limits), GFP_KERNEL);
6022         if (!limits)
6023                 return -ENOMEM;
6024
6025         limits[0].max = 1;
6026         limits[0].types |= BIT(NL80211_IFTYPE_STATION);
6027
6028         limits[1].max = 16;
6029         limits[1].types |= BIT(NL80211_IFTYPE_AP);
6030
6031         if (IS_ENABLED(CONFIG_MAC80211_MESH) &&
6032             ab->hw_params.interface_modes & BIT(NL80211_IFTYPE_MESH_POINT))
6033                 limits[1].types |= BIT(NL80211_IFTYPE_MESH_POINT);
6034
6035         combinations[0].limits = limits;
6036         combinations[0].n_limits = n_limits;
6037         combinations[0].max_interfaces = 16;
6038         combinations[0].num_different_channels = 1;
6039         combinations[0].beacon_int_infra_match = true;
6040         combinations[0].beacon_int_min_gcd = 100;
6041         combinations[0].radar_detect_widths = BIT(NL80211_CHAN_WIDTH_20_NOHT) |
6042                                                 BIT(NL80211_CHAN_WIDTH_20) |
6043                                                 BIT(NL80211_CHAN_WIDTH_40) |
6044                                                 BIT(NL80211_CHAN_WIDTH_80);
6045
6046         ar->hw->wiphy->iface_combinations = combinations;
6047         ar->hw->wiphy->n_iface_combinations = 1;
6048
6049         return 0;
6050 }
6051
6052 static const u8 ath11k_if_types_ext_capa[] = {
6053         [0] = WLAN_EXT_CAPA1_EXT_CHANNEL_SWITCHING,
6054         [7] = WLAN_EXT_CAPA8_OPMODE_NOTIF,
6055 };
6056
6057 static const u8 ath11k_if_types_ext_capa_sta[] = {
6058         [0] = WLAN_EXT_CAPA1_EXT_CHANNEL_SWITCHING,
6059         [7] = WLAN_EXT_CAPA8_OPMODE_NOTIF,
6060         [9] = WLAN_EXT_CAPA10_TWT_REQUESTER_SUPPORT,
6061 };
6062
6063 static const u8 ath11k_if_types_ext_capa_ap[] = {
6064         [0] = WLAN_EXT_CAPA1_EXT_CHANNEL_SWITCHING,
6065         [7] = WLAN_EXT_CAPA8_OPMODE_NOTIF,
6066         [9] = WLAN_EXT_CAPA10_TWT_RESPONDER_SUPPORT,
6067 };
6068
6069 static const struct wiphy_iftype_ext_capab ath11k_iftypes_ext_capa[] = {
6070         {
6071                 .extended_capabilities = ath11k_if_types_ext_capa,
6072                 .extended_capabilities_mask = ath11k_if_types_ext_capa,
6073                 .extended_capabilities_len = sizeof(ath11k_if_types_ext_capa),
6074         }, {
6075                 .iftype = NL80211_IFTYPE_STATION,
6076                 .extended_capabilities = ath11k_if_types_ext_capa_sta,
6077                 .extended_capabilities_mask = ath11k_if_types_ext_capa_sta,
6078                 .extended_capabilities_len =
6079                                 sizeof(ath11k_if_types_ext_capa_sta),
6080         }, {
6081                 .iftype = NL80211_IFTYPE_AP,
6082                 .extended_capabilities = ath11k_if_types_ext_capa_ap,
6083                 .extended_capabilities_mask = ath11k_if_types_ext_capa_ap,
6084                 .extended_capabilities_len =
6085                                 sizeof(ath11k_if_types_ext_capa_ap),
6086         },
6087 };
6088
6089 static void __ath11k_mac_unregister(struct ath11k *ar)
6090 {
6091         cancel_work_sync(&ar->regd_update_work);
6092
6093         ieee80211_unregister_hw(ar->hw);
6094
6095         idr_for_each(&ar->txmgmt_idr, ath11k_mac_tx_mgmt_pending_free, ar);
6096         idr_destroy(&ar->txmgmt_idr);
6097
6098         kfree(ar->mac.sbands[NL80211_BAND_2GHZ].channels);
6099         kfree(ar->mac.sbands[NL80211_BAND_5GHZ].channels);
6100         kfree(ar->mac.sbands[NL80211_BAND_6GHZ].channels);
6101
6102         kfree(ar->hw->wiphy->iface_combinations[0].limits);
6103         kfree(ar->hw->wiphy->iface_combinations);
6104
6105         SET_IEEE80211_DEV(ar->hw, NULL);
6106 }
6107
6108 void ath11k_mac_unregister(struct ath11k_base *ab)
6109 {
6110         struct ath11k *ar;
6111         struct ath11k_pdev *pdev;
6112         int i;
6113
6114         for (i = 0; i < ab->num_radios; i++) {
6115                 pdev = &ab->pdevs[i];
6116                 ar = pdev->ar;
6117                 if (!ar)
6118                         continue;
6119
6120                 __ath11k_mac_unregister(ar);
6121         }
6122 }
6123
6124 static int __ath11k_mac_register(struct ath11k *ar)
6125 {
6126         struct ath11k_base *ab = ar->ab;
6127         struct ath11k_pdev_cap *cap = &ar->pdev->cap;
6128         static const u32 cipher_suites[] = {
6129                 WLAN_CIPHER_SUITE_TKIP,
6130                 WLAN_CIPHER_SUITE_CCMP,
6131                 WLAN_CIPHER_SUITE_AES_CMAC,
6132                 WLAN_CIPHER_SUITE_BIP_CMAC_256,
6133                 WLAN_CIPHER_SUITE_BIP_GMAC_128,
6134                 WLAN_CIPHER_SUITE_BIP_GMAC_256,
6135                 WLAN_CIPHER_SUITE_GCMP,
6136                 WLAN_CIPHER_SUITE_GCMP_256,
6137                 WLAN_CIPHER_SUITE_CCMP_256,
6138         };
6139         int ret;
6140         u32 ht_cap = 0;
6141
6142         ath11k_pdev_caps_update(ar);
6143
6144         SET_IEEE80211_PERM_ADDR(ar->hw, ar->mac_addr);
6145
6146         SET_IEEE80211_DEV(ar->hw, ab->dev);
6147
6148         ret = ath11k_mac_setup_channels_rates(ar,
6149                                               cap->supported_bands);
6150         if (ret)
6151                 goto err;
6152
6153         ath11k_mac_setup_ht_vht_cap(ar, cap, &ht_cap);
6154         ath11k_mac_setup_he_cap(ar, cap);
6155
6156         ret = ath11k_mac_setup_iface_combinations(ar);
6157         if (ret) {
6158                 ath11k_err(ar->ab, "failed to setup interface combinations: %d\n", ret);
6159                 goto err_free_channels;
6160         }
6161
6162         ar->hw->wiphy->available_antennas_rx = cap->rx_chain_mask;
6163         ar->hw->wiphy->available_antennas_tx = cap->tx_chain_mask;
6164
6165         ar->hw->wiphy->interface_modes = ab->hw_params.interface_modes;
6166
6167         ieee80211_hw_set(ar->hw, SIGNAL_DBM);
6168         ieee80211_hw_set(ar->hw, SUPPORTS_PS);
6169         ieee80211_hw_set(ar->hw, SUPPORTS_DYNAMIC_PS);
6170         ieee80211_hw_set(ar->hw, MFP_CAPABLE);
6171         ieee80211_hw_set(ar->hw, REPORTS_TX_ACK_STATUS);
6172         ieee80211_hw_set(ar->hw, HAS_RATE_CONTROL);
6173         ieee80211_hw_set(ar->hw, AP_LINK_PS);
6174         ieee80211_hw_set(ar->hw, SPECTRUM_MGMT);
6175         ieee80211_hw_set(ar->hw, CONNECTION_MONITOR);
6176         ieee80211_hw_set(ar->hw, SUPPORTS_PER_STA_GTK);
6177         ieee80211_hw_set(ar->hw, WANT_MONITOR_VIF);
6178         ieee80211_hw_set(ar->hw, CHANCTX_STA_CSA);
6179         ieee80211_hw_set(ar->hw, QUEUE_CONTROL);
6180         ieee80211_hw_set(ar->hw, SUPPORTS_TX_FRAG);
6181         ieee80211_hw_set(ar->hw, REPORTS_LOW_ACK);
6182         ieee80211_hw_set(ar->hw, SUPPORTS_TX_ENCAP_OFFLOAD);
6183         if (ht_cap & WMI_HT_CAP_ENABLED) {
6184                 ieee80211_hw_set(ar->hw, AMPDU_AGGREGATION);
6185                 ieee80211_hw_set(ar->hw, TX_AMPDU_SETUP_IN_HW);
6186                 ieee80211_hw_set(ar->hw, SUPPORTS_REORDERING_BUFFER);
6187                 ieee80211_hw_set(ar->hw, SUPPORTS_AMSDU_IN_AMPDU);
6188                 ieee80211_hw_set(ar->hw, USES_RSS);
6189         }
6190
6191         ar->hw->wiphy->features |= NL80211_FEATURE_STATIC_SMPS;
6192         ar->hw->wiphy->flags |= WIPHY_FLAG_IBSS_RSN;
6193
6194         /* TODO: Check if HT capability advertised from firmware is different
6195          * for each band for a dual band capable radio. It will be tricky to
6196          * handle it when the ht capability different for each band.
6197          */
6198         if (ht_cap & WMI_HT_CAP_DYNAMIC_SMPS)
6199                 ar->hw->wiphy->features |= NL80211_FEATURE_DYNAMIC_SMPS;
6200
6201         ar->hw->wiphy->max_scan_ssids = WLAN_SCAN_PARAMS_MAX_SSID;
6202         ar->hw->wiphy->max_scan_ie_len = WLAN_SCAN_PARAMS_MAX_IE_LEN;
6203
6204         ar->hw->max_listen_interval = ATH11K_MAX_HW_LISTEN_INTERVAL;
6205
6206         ar->hw->wiphy->flags |= WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL;
6207         ar->hw->wiphy->flags |= WIPHY_FLAG_HAS_CHANNEL_SWITCH;
6208         ar->hw->wiphy->max_remain_on_channel_duration = 5000;
6209
6210         ar->hw->wiphy->flags |= WIPHY_FLAG_AP_UAPSD;
6211         ar->hw->wiphy->features |= NL80211_FEATURE_AP_MODE_CHAN_WIDTH_CHANGE |
6212                                    NL80211_FEATURE_AP_SCAN;
6213
6214         ar->max_num_stations = TARGET_NUM_STATIONS;
6215         ar->max_num_peers = TARGET_NUM_PEERS_PDEV;
6216
6217         ar->hw->wiphy->max_ap_assoc_sta = ar->max_num_stations;
6218
6219         ar->hw->queues = ATH11K_HW_MAX_QUEUES;
6220         ar->hw->wiphy->tx_queue_len = ATH11K_QUEUE_LEN;
6221         ar->hw->offchannel_tx_hw_queue = ATH11K_HW_MAX_QUEUES - 1;
6222         ar->hw->max_rx_aggregation_subframes = IEEE80211_MAX_AMPDU_BUF;
6223
6224         ar->hw->vif_data_size = sizeof(struct ath11k_vif);
6225         ar->hw->sta_data_size = sizeof(struct ath11k_sta);
6226
6227         wiphy_ext_feature_set(ar->hw->wiphy, NL80211_EXT_FEATURE_CQM_RSSI_LIST);
6228         wiphy_ext_feature_set(ar->hw->wiphy, NL80211_EXT_FEATURE_STA_TX_PWR);
6229
6230         ar->hw->wiphy->cipher_suites = cipher_suites;
6231         ar->hw->wiphy->n_cipher_suites = ARRAY_SIZE(cipher_suites);
6232
6233         ar->hw->wiphy->iftype_ext_capab = ath11k_iftypes_ext_capa;
6234         ar->hw->wiphy->num_iftype_ext_capab =
6235                 ARRAY_SIZE(ath11k_iftypes_ext_capa);
6236
6237         ath11k_reg_init(ar);
6238
6239         if (!test_bit(ATH11K_FLAG_RAW_MODE, &ab->dev_flags)) {
6240                 ar->hw->netdev_features = NETIF_F_HW_CSUM;
6241                 ieee80211_hw_set(ar->hw, SW_CRYPTO_CONTROL);
6242                 ieee80211_hw_set(ar->hw, SUPPORT_FAST_XMIT);
6243         }
6244
6245         ret = ieee80211_register_hw(ar->hw);
6246         if (ret) {
6247                 ath11k_err(ar->ab, "ieee80211 registration failed: %d\n", ret);
6248                 goto err_free_if_combs;
6249         }
6250
6251         /* Apply the regd received during initialization */
6252         ret = ath11k_regd_update(ar, true);
6253         if (ret) {
6254                 ath11k_err(ar->ab, "ath11k regd update failed: %d\n", ret);
6255                 goto err_free_if_combs;
6256         }
6257
6258         ret = ath11k_debugfs_register(ar);
6259         if (ret) {
6260                 ath11k_err(ar->ab, "debugfs registration failed: %d\n", ret);
6261                 goto err_free_if_combs;
6262         }
6263
6264         return 0;
6265
6266 err_free_if_combs:
6267         kfree(ar->hw->wiphy->iface_combinations[0].limits);
6268         kfree(ar->hw->wiphy->iface_combinations);
6269
6270 err_free_channels:
6271         kfree(ar->mac.sbands[NL80211_BAND_2GHZ].channels);
6272         kfree(ar->mac.sbands[NL80211_BAND_5GHZ].channels);
6273         kfree(ar->mac.sbands[NL80211_BAND_6GHZ].channels);
6274
6275 err:
6276         SET_IEEE80211_DEV(ar->hw, NULL);
6277         return ret;
6278 }
6279
6280 int ath11k_mac_register(struct ath11k_base *ab)
6281 {
6282         struct ath11k *ar;
6283         struct ath11k_pdev *pdev;
6284         int i;
6285         int ret;
6286
6287         if (test_bit(ATH11K_FLAG_REGISTERED, &ab->dev_flags))
6288                 return 0;
6289
6290         for (i = 0; i < ab->num_radios; i++) {
6291                 pdev = &ab->pdevs[i];
6292                 ar = pdev->ar;
6293                 if (ab->pdevs_macaddr_valid) {
6294                         ether_addr_copy(ar->mac_addr, pdev->mac_addr);
6295                 } else {
6296                         ether_addr_copy(ar->mac_addr, ab->mac_addr);
6297                         ar->mac_addr[4] += i;
6298                 }
6299
6300                 ret = __ath11k_mac_register(ar);
6301                 if (ret)
6302                         goto err_cleanup;
6303
6304                 idr_init(&ar->txmgmt_idr);
6305                 spin_lock_init(&ar->txmgmt_idr_lock);
6306         }
6307
6308         /* Initialize channel counters frequency value in hertz */
6309         ab->cc_freq_hz = IPQ8074_CC_FREQ_HERTZ;
6310         ab->free_vdev_map = (1LL << (ab->num_radios * TARGET_NUM_VDEVS)) - 1;
6311
6312         return 0;
6313
6314 err_cleanup:
6315         for (i = i - 1; i >= 0; i--) {
6316                 pdev = &ab->pdevs[i];
6317                 ar = pdev->ar;
6318                 __ath11k_mac_unregister(ar);
6319         }
6320
6321         return ret;
6322 }
6323
6324 int ath11k_mac_allocate(struct ath11k_base *ab)
6325 {
6326         struct ieee80211_hw *hw;
6327         struct ath11k *ar;
6328         struct ath11k_pdev *pdev;
6329         int ret;
6330         int i;
6331
6332         if (test_bit(ATH11K_FLAG_REGISTERED, &ab->dev_flags))
6333                 return 0;
6334
6335         for (i = 0; i < ab->num_radios; i++) {
6336                 pdev = &ab->pdevs[i];
6337                 hw = ieee80211_alloc_hw(sizeof(struct ath11k), &ath11k_ops);
6338                 if (!hw) {
6339                         ath11k_warn(ab, "failed to allocate mac80211 hw device\n");
6340                         ret = -ENOMEM;
6341                         goto err_free_mac;
6342                 }
6343
6344                 ar = hw->priv;
6345                 ar->hw = hw;
6346                 ar->ab = ab;
6347                 ar->pdev = pdev;
6348                 ar->pdev_idx = i;
6349                 ar->lmac_id = ath11k_hw_get_mac_from_pdev_id(&ab->hw_params, i);
6350
6351                 ar->wmi = &ab->wmi_ab.wmi[i];
6352                 /* FIXME wmi[0] is already initialized during attach,
6353                  * Should we do this again?
6354                  */
6355                 ath11k_wmi_pdev_attach(ab, i);
6356
6357                 ar->cfg_tx_chainmask = pdev->cap.tx_chain_mask;
6358                 ar->cfg_rx_chainmask = pdev->cap.rx_chain_mask;
6359                 ar->num_tx_chains = get_num_chains(pdev->cap.tx_chain_mask);
6360                 ar->num_rx_chains = get_num_chains(pdev->cap.rx_chain_mask);
6361
6362                 pdev->ar = ar;
6363                 spin_lock_init(&ar->data_lock);
6364                 INIT_LIST_HEAD(&ar->arvifs);
6365                 INIT_LIST_HEAD(&ar->ppdu_stats_info);
6366                 mutex_init(&ar->conf_mutex);
6367                 init_completion(&ar->vdev_setup_done);
6368                 init_completion(&ar->peer_assoc_done);
6369                 init_completion(&ar->install_key_done);
6370                 init_completion(&ar->bss_survey_done);
6371                 init_completion(&ar->scan.started);
6372                 init_completion(&ar->scan.completed);
6373                 init_completion(&ar->thermal.wmi_sync);
6374
6375                 INIT_DELAYED_WORK(&ar->scan.timeout, ath11k_scan_timeout_work);
6376                 INIT_WORK(&ar->regd_update_work, ath11k_regd_update_work);
6377
6378                 INIT_WORK(&ar->wmi_mgmt_tx_work, ath11k_mgmt_over_wmi_tx_work);
6379                 skb_queue_head_init(&ar->wmi_mgmt_tx_queue);
6380                 clear_bit(ATH11K_FLAG_MONITOR_ENABLED, &ar->monitor_flags);
6381         }
6382
6383         return 0;
6384
6385 err_free_mac:
6386         ath11k_mac_destroy(ab);
6387
6388         return ret;
6389 }
6390
6391 void ath11k_mac_destroy(struct ath11k_base *ab)
6392 {
6393         struct ath11k *ar;
6394         struct ath11k_pdev *pdev;
6395         int i;
6396
6397         for (i = 0; i < ab->num_radios; i++) {
6398                 pdev = &ab->pdevs[i];
6399                 ar = pdev->ar;
6400                 if (!ar)
6401                         continue;
6402
6403                 ieee80211_free_hw(ar->hw);
6404                 pdev->ar = NULL;
6405         }
6406 }