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