]> Git Repo - J-linux.git/blob - drivers/net/wireless/ath/ath11k/mac.c
Merge tag 'amd-drm-next-6.5-2023-06-09' of https://gitlab.freedesktop.org/agd5f/linux...
[J-linux.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  * Copyright (c) 2021-2022 Qualcomm Innovation Center, Inc. All rights reserved.
5  */
6
7 #include <net/mac80211.h>
8 #include <linux/etherdevice.h>
9 #include <linux/bitfield.h>
10 #include <linux/inetdevice.h>
11 #include <net/if_inet6.h>
12 #include <net/ipv6.h>
13
14 #include "mac.h"
15 #include "core.h"
16 #include "debug.h"
17 #include "wmi.h"
18 #include "hw.h"
19 #include "dp_tx.h"
20 #include "dp_rx.h"
21 #include "testmode.h"
22 #include "peer.h"
23 #include "debugfs_sta.h"
24 #include "hif.h"
25 #include "wow.h"
26
27 #define CHAN2G(_channel, _freq, _flags) { \
28         .band                   = NL80211_BAND_2GHZ, \
29         .hw_value               = (_channel), \
30         .center_freq            = (_freq), \
31         .flags                  = (_flags), \
32         .max_antenna_gain       = 0, \
33         .max_power              = 30, \
34 }
35
36 #define CHAN5G(_channel, _freq, _flags) { \
37         .band                   = NL80211_BAND_5GHZ, \
38         .hw_value               = (_channel), \
39         .center_freq            = (_freq), \
40         .flags                  = (_flags), \
41         .max_antenna_gain       = 0, \
42         .max_power              = 30, \
43 }
44
45 #define CHAN6G(_channel, _freq, _flags) { \
46         .band                   = NL80211_BAND_6GHZ, \
47         .hw_value               = (_channel), \
48         .center_freq            = (_freq), \
49         .flags                  = (_flags), \
50         .max_antenna_gain       = 0, \
51         .max_power              = 30, \
52 }
53
54 static const struct ieee80211_channel ath11k_2ghz_channels[] = {
55         CHAN2G(1, 2412, 0),
56         CHAN2G(2, 2417, 0),
57         CHAN2G(3, 2422, 0),
58         CHAN2G(4, 2427, 0),
59         CHAN2G(5, 2432, 0),
60         CHAN2G(6, 2437, 0),
61         CHAN2G(7, 2442, 0),
62         CHAN2G(8, 2447, 0),
63         CHAN2G(9, 2452, 0),
64         CHAN2G(10, 2457, 0),
65         CHAN2G(11, 2462, 0),
66         CHAN2G(12, 2467, 0),
67         CHAN2G(13, 2472, 0),
68         CHAN2G(14, 2484, 0),
69 };
70
71 static const struct ieee80211_channel ath11k_5ghz_channels[] = {
72         CHAN5G(36, 5180, 0),
73         CHAN5G(40, 5200, 0),
74         CHAN5G(44, 5220, 0),
75         CHAN5G(48, 5240, 0),
76         CHAN5G(52, 5260, 0),
77         CHAN5G(56, 5280, 0),
78         CHAN5G(60, 5300, 0),
79         CHAN5G(64, 5320, 0),
80         CHAN5G(100, 5500, 0),
81         CHAN5G(104, 5520, 0),
82         CHAN5G(108, 5540, 0),
83         CHAN5G(112, 5560, 0),
84         CHAN5G(116, 5580, 0),
85         CHAN5G(120, 5600, 0),
86         CHAN5G(124, 5620, 0),
87         CHAN5G(128, 5640, 0),
88         CHAN5G(132, 5660, 0),
89         CHAN5G(136, 5680, 0),
90         CHAN5G(140, 5700, 0),
91         CHAN5G(144, 5720, 0),
92         CHAN5G(149, 5745, 0),
93         CHAN5G(153, 5765, 0),
94         CHAN5G(157, 5785, 0),
95         CHAN5G(161, 5805, 0),
96         CHAN5G(165, 5825, 0),
97         CHAN5G(169, 5845, 0),
98         CHAN5G(173, 5865, 0),
99         CHAN5G(177, 5885, 0),
100 };
101
102 static const struct ieee80211_channel ath11k_6ghz_channels[] = {
103         CHAN6G(1, 5955, 0),
104         CHAN6G(5, 5975, 0),
105         CHAN6G(9, 5995, 0),
106         CHAN6G(13, 6015, 0),
107         CHAN6G(17, 6035, 0),
108         CHAN6G(21, 6055, 0),
109         CHAN6G(25, 6075, 0),
110         CHAN6G(29, 6095, 0),
111         CHAN6G(33, 6115, 0),
112         CHAN6G(37, 6135, 0),
113         CHAN6G(41, 6155, 0),
114         CHAN6G(45, 6175, 0),
115         CHAN6G(49, 6195, 0),
116         CHAN6G(53, 6215, 0),
117         CHAN6G(57, 6235, 0),
118         CHAN6G(61, 6255, 0),
119         CHAN6G(65, 6275, 0),
120         CHAN6G(69, 6295, 0),
121         CHAN6G(73, 6315, 0),
122         CHAN6G(77, 6335, 0),
123         CHAN6G(81, 6355, 0),
124         CHAN6G(85, 6375, 0),
125         CHAN6G(89, 6395, 0),
126         CHAN6G(93, 6415, 0),
127         CHAN6G(97, 6435, 0),
128         CHAN6G(101, 6455, 0),
129         CHAN6G(105, 6475, 0),
130         CHAN6G(109, 6495, 0),
131         CHAN6G(113, 6515, 0),
132         CHAN6G(117, 6535, 0),
133         CHAN6G(121, 6555, 0),
134         CHAN6G(125, 6575, 0),
135         CHAN6G(129, 6595, 0),
136         CHAN6G(133, 6615, 0),
137         CHAN6G(137, 6635, 0),
138         CHAN6G(141, 6655, 0),
139         CHAN6G(145, 6675, 0),
140         CHAN6G(149, 6695, 0),
141         CHAN6G(153, 6715, 0),
142         CHAN6G(157, 6735, 0),
143         CHAN6G(161, 6755, 0),
144         CHAN6G(165, 6775, 0),
145         CHAN6G(169, 6795, 0),
146         CHAN6G(173, 6815, 0),
147         CHAN6G(177, 6835, 0),
148         CHAN6G(181, 6855, 0),
149         CHAN6G(185, 6875, 0),
150         CHAN6G(189, 6895, 0),
151         CHAN6G(193, 6915, 0),
152         CHAN6G(197, 6935, 0),
153         CHAN6G(201, 6955, 0),
154         CHAN6G(205, 6975, 0),
155         CHAN6G(209, 6995, 0),
156         CHAN6G(213, 7015, 0),
157         CHAN6G(217, 7035, 0),
158         CHAN6G(221, 7055, 0),
159         CHAN6G(225, 7075, 0),
160         CHAN6G(229, 7095, 0),
161         CHAN6G(233, 7115, 0),
162
163         /* new addition in IEEE Std 802.11ax-2021 */
164         CHAN6G(2, 5935, 0),
165 };
166
167 static struct ieee80211_rate ath11k_legacy_rates[] = {
168         { .bitrate = 10,
169           .hw_value = ATH11K_HW_RATE_CCK_LP_1M },
170         { .bitrate = 20,
171           .hw_value = ATH11K_HW_RATE_CCK_LP_2M,
172           .hw_value_short = ATH11K_HW_RATE_CCK_SP_2M,
173           .flags = IEEE80211_RATE_SHORT_PREAMBLE },
174         { .bitrate = 55,
175           .hw_value = ATH11K_HW_RATE_CCK_LP_5_5M,
176           .hw_value_short = ATH11K_HW_RATE_CCK_SP_5_5M,
177           .flags = IEEE80211_RATE_SHORT_PREAMBLE },
178         { .bitrate = 110,
179           .hw_value = ATH11K_HW_RATE_CCK_LP_11M,
180           .hw_value_short = ATH11K_HW_RATE_CCK_SP_11M,
181           .flags = IEEE80211_RATE_SHORT_PREAMBLE },
182
183         { .bitrate = 60, .hw_value = ATH11K_HW_RATE_OFDM_6M },
184         { .bitrate = 90, .hw_value = ATH11K_HW_RATE_OFDM_9M },
185         { .bitrate = 120, .hw_value = ATH11K_HW_RATE_OFDM_12M },
186         { .bitrate = 180, .hw_value = ATH11K_HW_RATE_OFDM_18M },
187         { .bitrate = 240, .hw_value = ATH11K_HW_RATE_OFDM_24M },
188         { .bitrate = 360, .hw_value = ATH11K_HW_RATE_OFDM_36M },
189         { .bitrate = 480, .hw_value = ATH11K_HW_RATE_OFDM_48M },
190         { .bitrate = 540, .hw_value = ATH11K_HW_RATE_OFDM_54M },
191 };
192
193 static const int
194 ath11k_phymodes[NUM_NL80211_BANDS][ATH11K_CHAN_WIDTH_NUM] = {
195         [NL80211_BAND_2GHZ] = {
196                         [NL80211_CHAN_WIDTH_5] = MODE_UNKNOWN,
197                         [NL80211_CHAN_WIDTH_10] = MODE_UNKNOWN,
198                         [NL80211_CHAN_WIDTH_20_NOHT] = MODE_11AX_HE20_2G,
199                         [NL80211_CHAN_WIDTH_20] = MODE_11AX_HE20_2G,
200                         [NL80211_CHAN_WIDTH_40] = MODE_11AX_HE40_2G,
201                         [NL80211_CHAN_WIDTH_80] = MODE_11AX_HE80_2G,
202                         [NL80211_CHAN_WIDTH_80P80] = MODE_UNKNOWN,
203                         [NL80211_CHAN_WIDTH_160] = MODE_UNKNOWN,
204         },
205         [NL80211_BAND_5GHZ] = {
206                         [NL80211_CHAN_WIDTH_5] = MODE_UNKNOWN,
207                         [NL80211_CHAN_WIDTH_10] = MODE_UNKNOWN,
208                         [NL80211_CHAN_WIDTH_20_NOHT] = MODE_11AX_HE20,
209                         [NL80211_CHAN_WIDTH_20] = MODE_11AX_HE20,
210                         [NL80211_CHAN_WIDTH_40] = MODE_11AX_HE40,
211                         [NL80211_CHAN_WIDTH_80] = MODE_11AX_HE80,
212                         [NL80211_CHAN_WIDTH_160] = MODE_11AX_HE160,
213                         [NL80211_CHAN_WIDTH_80P80] = MODE_11AX_HE80_80,
214         },
215         [NL80211_BAND_6GHZ] = {
216                         [NL80211_CHAN_WIDTH_5] = MODE_UNKNOWN,
217                         [NL80211_CHAN_WIDTH_10] = MODE_UNKNOWN,
218                         [NL80211_CHAN_WIDTH_20_NOHT] = MODE_11AX_HE20,
219                         [NL80211_CHAN_WIDTH_20] = MODE_11AX_HE20,
220                         [NL80211_CHAN_WIDTH_40] = MODE_11AX_HE40,
221                         [NL80211_CHAN_WIDTH_80] = MODE_11AX_HE80,
222                         [NL80211_CHAN_WIDTH_160] = MODE_11AX_HE160,
223                         [NL80211_CHAN_WIDTH_80P80] = MODE_11AX_HE80_80,
224         },
225
226 };
227
228 const struct htt_rx_ring_tlv_filter ath11k_mac_mon_status_filter_default = {
229         .rx_filter = HTT_RX_FILTER_TLV_FLAGS_MPDU_START |
230                      HTT_RX_FILTER_TLV_FLAGS_PPDU_END |
231                      HTT_RX_FILTER_TLV_FLAGS_PPDU_END_STATUS_DONE,
232         .pkt_filter_flags0 = HTT_RX_FP_MGMT_FILTER_FLAGS0,
233         .pkt_filter_flags1 = HTT_RX_FP_MGMT_FILTER_FLAGS1,
234         .pkt_filter_flags2 = HTT_RX_FP_CTRL_FILTER_FLASG2,
235         .pkt_filter_flags3 = HTT_RX_FP_DATA_FILTER_FLASG3 |
236                              HTT_RX_FP_CTRL_FILTER_FLASG3
237 };
238
239 #define ATH11K_MAC_FIRST_OFDM_RATE_IDX 4
240 #define ath11k_g_rates ath11k_legacy_rates
241 #define ath11k_g_rates_size (ARRAY_SIZE(ath11k_legacy_rates))
242 #define ath11k_a_rates (ath11k_legacy_rates + 4)
243 #define ath11k_a_rates_size (ARRAY_SIZE(ath11k_legacy_rates) - 4)
244
245 #define ATH11K_MAC_SCAN_CMD_EVT_OVERHEAD                200 /* in msecs */
246
247 /* Overhead due to the processing of channel switch events from FW */
248 #define ATH11K_SCAN_CHANNEL_SWITCH_WMI_EVT_OVERHEAD     10 /* in msecs */
249
250 static const u32 ath11k_smps_map[] = {
251         [WLAN_HT_CAP_SM_PS_STATIC] = WMI_PEER_SMPS_STATIC,
252         [WLAN_HT_CAP_SM_PS_DYNAMIC] = WMI_PEER_SMPS_DYNAMIC,
253         [WLAN_HT_CAP_SM_PS_INVALID] = WMI_PEER_SMPS_PS_NONE,
254         [WLAN_HT_CAP_SM_PS_DISABLED] = WMI_PEER_SMPS_PS_NONE,
255 };
256
257 static int ath11k_start_vdev_delay(struct ieee80211_hw *hw,
258                                    struct ieee80211_vif *vif);
259
260 enum nl80211_he_ru_alloc ath11k_mac_phy_he_ru_to_nl80211_he_ru_alloc(u16 ru_phy)
261 {
262         enum nl80211_he_ru_alloc ret;
263
264         switch (ru_phy) {
265         case RU_26:
266                 ret = NL80211_RATE_INFO_HE_RU_ALLOC_26;
267                 break;
268         case RU_52:
269                 ret = NL80211_RATE_INFO_HE_RU_ALLOC_52;
270                 break;
271         case RU_106:
272                 ret = NL80211_RATE_INFO_HE_RU_ALLOC_106;
273                 break;
274         case RU_242:
275                 ret = NL80211_RATE_INFO_HE_RU_ALLOC_242;
276                 break;
277         case RU_484:
278                 ret = NL80211_RATE_INFO_HE_RU_ALLOC_484;
279                 break;
280         case RU_996:
281                 ret = NL80211_RATE_INFO_HE_RU_ALLOC_996;
282                 break;
283         default:
284                 ret = NL80211_RATE_INFO_HE_RU_ALLOC_26;
285                 break;
286         }
287
288         return ret;
289 }
290
291 enum nl80211_he_ru_alloc ath11k_mac_he_ru_tones_to_nl80211_he_ru_alloc(u16 ru_tones)
292 {
293         enum nl80211_he_ru_alloc ret;
294
295         switch (ru_tones) {
296         case 26:
297                 ret = NL80211_RATE_INFO_HE_RU_ALLOC_26;
298                 break;
299         case 52:
300                 ret = NL80211_RATE_INFO_HE_RU_ALLOC_52;
301                 break;
302         case 106:
303                 ret = NL80211_RATE_INFO_HE_RU_ALLOC_106;
304                 break;
305         case 242:
306                 ret = NL80211_RATE_INFO_HE_RU_ALLOC_242;
307                 break;
308         case 484:
309                 ret = NL80211_RATE_INFO_HE_RU_ALLOC_484;
310                 break;
311         case 996:
312                 ret = NL80211_RATE_INFO_HE_RU_ALLOC_996;
313                 break;
314         case (996 * 2):
315                 ret = NL80211_RATE_INFO_HE_RU_ALLOC_2x996;
316                 break;
317         default:
318                 ret = NL80211_RATE_INFO_HE_RU_ALLOC_26;
319                 break;
320         }
321
322         return ret;
323 }
324
325 enum nl80211_he_gi ath11k_mac_he_gi_to_nl80211_he_gi(u8 sgi)
326 {
327         enum nl80211_he_gi ret;
328
329         switch (sgi) {
330         case RX_MSDU_START_SGI_0_8_US:
331                 ret = NL80211_RATE_INFO_HE_GI_0_8;
332                 break;
333         case RX_MSDU_START_SGI_1_6_US:
334                 ret = NL80211_RATE_INFO_HE_GI_1_6;
335                 break;
336         case RX_MSDU_START_SGI_3_2_US:
337                 ret = NL80211_RATE_INFO_HE_GI_3_2;
338                 break;
339         default:
340                 ret = NL80211_RATE_INFO_HE_GI_0_8;
341                 break;
342         }
343
344         return ret;
345 }
346
347 u8 ath11k_mac_bw_to_mac80211_bw(u8 bw)
348 {
349         u8 ret = 0;
350
351         switch (bw) {
352         case ATH11K_BW_20:
353                 ret = RATE_INFO_BW_20;
354                 break;
355         case ATH11K_BW_40:
356                 ret = RATE_INFO_BW_40;
357                 break;
358         case ATH11K_BW_80:
359                 ret = RATE_INFO_BW_80;
360                 break;
361         case ATH11K_BW_160:
362                 ret = RATE_INFO_BW_160;
363                 break;
364         }
365
366         return ret;
367 }
368
369 enum ath11k_supported_bw ath11k_mac_mac80211_bw_to_ath11k_bw(enum rate_info_bw bw)
370 {
371         switch (bw) {
372         case RATE_INFO_BW_20:
373                 return ATH11K_BW_20;
374         case RATE_INFO_BW_40:
375                 return ATH11K_BW_40;
376         case RATE_INFO_BW_80:
377                 return ATH11K_BW_80;
378         case RATE_INFO_BW_160:
379                 return ATH11K_BW_160;
380         default:
381                 return ATH11K_BW_20;
382         }
383 }
384
385 int ath11k_mac_hw_ratecode_to_legacy_rate(u8 hw_rc, u8 preamble, u8 *rateidx,
386                                           u16 *rate)
387 {
388         /* As default, it is OFDM rates */
389         int i = ATH11K_MAC_FIRST_OFDM_RATE_IDX;
390         int max_rates_idx = ath11k_g_rates_size;
391
392         if (preamble == WMI_RATE_PREAMBLE_CCK) {
393                 hw_rc &= ~ATH11k_HW_RATECODE_CCK_SHORT_PREAM_MASK;
394                 i = 0;
395                 max_rates_idx = ATH11K_MAC_FIRST_OFDM_RATE_IDX;
396         }
397
398         while (i < max_rates_idx) {
399                 if (hw_rc == ath11k_legacy_rates[i].hw_value) {
400                         *rateidx = i;
401                         *rate = ath11k_legacy_rates[i].bitrate;
402                         return 0;
403                 }
404                 i++;
405         }
406
407         return -EINVAL;
408 }
409
410 static int get_num_chains(u32 mask)
411 {
412         int num_chains = 0;
413
414         while (mask) {
415                 if (mask & BIT(0))
416                         num_chains++;
417                 mask >>= 1;
418         }
419
420         return num_chains;
421 }
422
423 u8 ath11k_mac_bitrate_to_idx(const struct ieee80211_supported_band *sband,
424                              u32 bitrate)
425 {
426         int i;
427
428         for (i = 0; i < sband->n_bitrates; i++)
429                 if (sband->bitrates[i].bitrate == bitrate)
430                         return i;
431
432         return 0;
433 }
434
435 static u32
436 ath11k_mac_max_ht_nss(const u8 ht_mcs_mask[IEEE80211_HT_MCS_MASK_LEN])
437 {
438         int nss;
439
440         for (nss = IEEE80211_HT_MCS_MASK_LEN - 1; nss >= 0; nss--)
441                 if (ht_mcs_mask[nss])
442                         return nss + 1;
443
444         return 1;
445 }
446
447 static u32
448 ath11k_mac_max_vht_nss(const u16 vht_mcs_mask[NL80211_VHT_NSS_MAX])
449 {
450         int nss;
451
452         for (nss = NL80211_VHT_NSS_MAX - 1; nss >= 0; nss--)
453                 if (vht_mcs_mask[nss])
454                         return nss + 1;
455
456         return 1;
457 }
458
459 static u32
460 ath11k_mac_max_he_nss(const u16 he_mcs_mask[NL80211_HE_NSS_MAX])
461 {
462         int nss;
463
464         for (nss = NL80211_HE_NSS_MAX - 1; nss >= 0; nss--)
465                 if (he_mcs_mask[nss])
466                         return nss + 1;
467
468         return 1;
469 }
470
471 static u8 ath11k_parse_mpdudensity(u8 mpdudensity)
472 {
473 /* 802.11n D2.0 defined values for "Minimum MPDU Start Spacing":
474  *   0 for no restriction
475  *   1 for 1/4 us
476  *   2 for 1/2 us
477  *   3 for 1 us
478  *   4 for 2 us
479  *   5 for 4 us
480  *   6 for 8 us
481  *   7 for 16 us
482  */
483         switch (mpdudensity) {
484         case 0:
485                 return 0;
486         case 1:
487         case 2:
488         case 3:
489         /* Our lower layer calculations limit our precision to
490          * 1 microsecond
491          */
492                 return 1;
493         case 4:
494                 return 2;
495         case 5:
496                 return 4;
497         case 6:
498                 return 8;
499         case 7:
500                 return 16;
501         default:
502                 return 0;
503         }
504 }
505
506 static int ath11k_mac_vif_chan(struct ieee80211_vif *vif,
507                                struct cfg80211_chan_def *def)
508 {
509         struct ieee80211_chanctx_conf *conf;
510
511         rcu_read_lock();
512         conf = rcu_dereference(vif->bss_conf.chanctx_conf);
513         if (!conf) {
514                 rcu_read_unlock();
515                 return -ENOENT;
516         }
517
518         *def = conf->def;
519         rcu_read_unlock();
520
521         return 0;
522 }
523
524 static bool ath11k_mac_bitrate_is_cck(int bitrate)
525 {
526         switch (bitrate) {
527         case 10:
528         case 20:
529         case 55:
530         case 110:
531                 return true;
532         }
533
534         return false;
535 }
536
537 u8 ath11k_mac_hw_rate_to_idx(const struct ieee80211_supported_band *sband,
538                              u8 hw_rate, bool cck)
539 {
540         const struct ieee80211_rate *rate;
541         int i;
542
543         for (i = 0; i < sband->n_bitrates; i++) {
544                 rate = &sband->bitrates[i];
545
546                 if (ath11k_mac_bitrate_is_cck(rate->bitrate) != cck)
547                         continue;
548
549                 if (rate->hw_value == hw_rate)
550                         return i;
551                 else if (rate->flags & IEEE80211_RATE_SHORT_PREAMBLE &&
552                          rate->hw_value_short == hw_rate)
553                         return i;
554         }
555
556         return 0;
557 }
558
559 static u8 ath11k_mac_bitrate_to_rate(int bitrate)
560 {
561         return DIV_ROUND_UP(bitrate, 5) |
562                (ath11k_mac_bitrate_is_cck(bitrate) ? BIT(7) : 0);
563 }
564
565 static void ath11k_get_arvif_iter(void *data, u8 *mac,
566                                   struct ieee80211_vif *vif)
567 {
568         struct ath11k_vif_iter *arvif_iter = data;
569         struct ath11k_vif *arvif = (void *)vif->drv_priv;
570
571         if (arvif->vdev_id == arvif_iter->vdev_id)
572                 arvif_iter->arvif = arvif;
573 }
574
575 struct ath11k_vif *ath11k_mac_get_arvif(struct ath11k *ar, u32 vdev_id)
576 {
577         struct ath11k_vif_iter arvif_iter;
578         u32 flags;
579
580         memset(&arvif_iter, 0, sizeof(struct ath11k_vif_iter));
581         arvif_iter.vdev_id = vdev_id;
582
583         flags = IEEE80211_IFACE_ITER_RESUME_ALL;
584         ieee80211_iterate_active_interfaces_atomic(ar->hw,
585                                                    flags,
586                                                    ath11k_get_arvif_iter,
587                                                    &arvif_iter);
588         if (!arvif_iter.arvif) {
589                 ath11k_warn(ar->ab, "No VIF found for vdev %d\n", vdev_id);
590                 return NULL;
591         }
592
593         return arvif_iter.arvif;
594 }
595
596 struct ath11k_vif *ath11k_mac_get_arvif_by_vdev_id(struct ath11k_base *ab,
597                                                    u32 vdev_id)
598 {
599         int i;
600         struct ath11k_pdev *pdev;
601         struct ath11k_vif *arvif;
602
603         for (i = 0; i < ab->num_radios; i++) {
604                 pdev = rcu_dereference(ab->pdevs_active[i]);
605                 if (pdev && pdev->ar &&
606                     (pdev->ar->allocated_vdev_map & (1LL << vdev_id))) {
607                         arvif = ath11k_mac_get_arvif(pdev->ar, vdev_id);
608                         if (arvif)
609                                 return arvif;
610                 }
611         }
612
613         return NULL;
614 }
615
616 struct ath11k *ath11k_mac_get_ar_by_vdev_id(struct ath11k_base *ab, u32 vdev_id)
617 {
618         int i;
619         struct ath11k_pdev *pdev;
620
621         for (i = 0; i < ab->num_radios; i++) {
622                 pdev = rcu_dereference(ab->pdevs_active[i]);
623                 if (pdev && pdev->ar) {
624                         if (pdev->ar->allocated_vdev_map & (1LL << vdev_id))
625                                 return pdev->ar;
626                 }
627         }
628
629         return NULL;
630 }
631
632 struct ath11k *ath11k_mac_get_ar_by_pdev_id(struct ath11k_base *ab, u32 pdev_id)
633 {
634         int i;
635         struct ath11k_pdev *pdev;
636
637         if (ab->hw_params.single_pdev_only) {
638                 pdev = rcu_dereference(ab->pdevs_active[0]);
639                 return pdev ? pdev->ar : NULL;
640         }
641
642         if (WARN_ON(pdev_id > ab->num_radios))
643                 return NULL;
644
645         for (i = 0; i < ab->num_radios; i++) {
646                 pdev = rcu_dereference(ab->pdevs_active[i]);
647
648                 if (pdev && pdev->pdev_id == pdev_id)
649                         return (pdev->ar ? pdev->ar : NULL);
650         }
651
652         return NULL;
653 }
654
655 struct ath11k_vif *ath11k_mac_get_vif_up(struct ath11k_base *ab)
656 {
657         struct ath11k *ar;
658         struct ath11k_pdev *pdev;
659         struct ath11k_vif *arvif;
660         int i;
661
662         for (i = 0; i < ab->num_radios; i++) {
663                 pdev = &ab->pdevs[i];
664                 ar = pdev->ar;
665                 list_for_each_entry(arvif, &ar->arvifs, list) {
666                         if (arvif->is_up)
667                                 return arvif;
668                 }
669         }
670
671         return NULL;
672 }
673
674 static bool ath11k_mac_band_match(enum nl80211_band band1, enum WMI_HOST_WLAN_BAND band2)
675 {
676         return (((band1 == NL80211_BAND_2GHZ) && (band2 & WMI_HOST_WLAN_2G_CAP)) ||
677                 (((band1 == NL80211_BAND_5GHZ) || (band1 == NL80211_BAND_6GHZ)) &&
678                    (band2 & WMI_HOST_WLAN_5G_CAP)));
679 }
680
681 u8 ath11k_mac_get_target_pdev_id_from_vif(struct ath11k_vif *arvif)
682 {
683         struct ath11k *ar = arvif->ar;
684         struct ath11k_base *ab = ar->ab;
685         struct ieee80211_vif *vif = arvif->vif;
686         struct cfg80211_chan_def def;
687         enum nl80211_band band;
688         u8 pdev_id = ab->target_pdev_ids[0].pdev_id;
689         int i;
690
691         if (WARN_ON(ath11k_mac_vif_chan(vif, &def)))
692                 return pdev_id;
693
694         band = def.chan->band;
695
696         for (i = 0; i < ab->target_pdev_count; i++) {
697                 if (ath11k_mac_band_match(band, ab->target_pdev_ids[i].supported_bands))
698                         return ab->target_pdev_ids[i].pdev_id;
699         }
700
701         return pdev_id;
702 }
703
704 u8 ath11k_mac_get_target_pdev_id(struct ath11k *ar)
705 {
706         struct ath11k_vif *arvif;
707
708         arvif = ath11k_mac_get_vif_up(ar->ab);
709
710         if (arvif)
711                 return ath11k_mac_get_target_pdev_id_from_vif(arvif);
712         else
713                 return ar->ab->target_pdev_ids[0].pdev_id;
714 }
715
716 static void ath11k_pdev_caps_update(struct ath11k *ar)
717 {
718         struct ath11k_base *ab = ar->ab;
719
720         ar->max_tx_power = ab->target_caps.hw_max_tx_power;
721
722         /* FIXME Set min_tx_power to ab->target_caps.hw_min_tx_power.
723          * But since the received value in svcrdy is same as hw_max_tx_power,
724          * we can set ar->min_tx_power to 0 currently until
725          * this is fixed in firmware
726          */
727         ar->min_tx_power = 0;
728
729         ar->txpower_limit_2g = ar->max_tx_power;
730         ar->txpower_limit_5g = ar->max_tx_power;
731         ar->txpower_scale = WMI_HOST_TP_SCALE_MAX;
732 }
733
734 static int ath11k_mac_txpower_recalc(struct ath11k *ar)
735 {
736         struct ath11k_pdev *pdev = ar->pdev;
737         struct ath11k_vif *arvif;
738         int ret, txpower = -1;
739         u32 param;
740
741         lockdep_assert_held(&ar->conf_mutex);
742
743         list_for_each_entry(arvif, &ar->arvifs, list) {
744                 if (arvif->txpower <= 0)
745                         continue;
746
747                 if (txpower == -1)
748                         txpower = arvif->txpower;
749                 else
750                         txpower = min(txpower, arvif->txpower);
751         }
752
753         if (txpower == -1)
754                 return 0;
755
756         /* txpwr is set as 2 units per dBm in FW*/
757         txpower = min_t(u32, max_t(u32, ar->min_tx_power, txpower),
758                         ar->max_tx_power) * 2;
759
760         ath11k_dbg(ar->ab, ATH11K_DBG_MAC, "txpower to set in hw %d\n",
761                    txpower / 2);
762
763         if ((pdev->cap.supported_bands & WMI_HOST_WLAN_2G_CAP) &&
764             ar->txpower_limit_2g != txpower) {
765                 param = WMI_PDEV_PARAM_TXPOWER_LIMIT2G;
766                 ret = ath11k_wmi_pdev_set_param(ar, param,
767                                                 txpower, ar->pdev->pdev_id);
768                 if (ret)
769                         goto fail;
770                 ar->txpower_limit_2g = txpower;
771         }
772
773         if ((pdev->cap.supported_bands & WMI_HOST_WLAN_5G_CAP) &&
774             ar->txpower_limit_5g != txpower) {
775                 param = WMI_PDEV_PARAM_TXPOWER_LIMIT5G;
776                 ret = ath11k_wmi_pdev_set_param(ar, param,
777                                                 txpower, ar->pdev->pdev_id);
778                 if (ret)
779                         goto fail;
780                 ar->txpower_limit_5g = txpower;
781         }
782
783         return 0;
784
785 fail:
786         ath11k_warn(ar->ab, "failed to recalc txpower limit %d using pdev param %d: %d\n",
787                     txpower / 2, param, ret);
788         return ret;
789 }
790
791 static int ath11k_recalc_rtscts_prot(struct ath11k_vif *arvif)
792 {
793         struct ath11k *ar = arvif->ar;
794         u32 vdev_param, rts_cts = 0;
795         int ret;
796
797         lockdep_assert_held(&ar->conf_mutex);
798
799         vdev_param = WMI_VDEV_PARAM_ENABLE_RTSCTS;
800
801         /* Enable RTS/CTS protection for sw retries (when legacy stations
802          * are in BSS) or by default only for second rate series.
803          * TODO: Check if we need to enable CTS 2 Self in any case
804          */
805         rts_cts = WMI_USE_RTS_CTS;
806
807         if (arvif->num_legacy_stations > 0)
808                 rts_cts |= WMI_RTSCTS_ACROSS_SW_RETRIES << 4;
809         else
810                 rts_cts |= WMI_RTSCTS_FOR_SECOND_RATESERIES << 4;
811
812         /* Need not send duplicate param value to firmware */
813         if (arvif->rtscts_prot_mode == rts_cts)
814                 return 0;
815
816         arvif->rtscts_prot_mode = rts_cts;
817
818         ath11k_dbg(ar->ab, ATH11K_DBG_MAC, "mac vdev %d recalc rts/cts prot %d\n",
819                    arvif->vdev_id, rts_cts);
820
821         ret =  ath11k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
822                                              vdev_param, rts_cts);
823         if (ret)
824                 ath11k_warn(ar->ab, "failed to recalculate rts/cts prot for vdev %d: %d\n",
825                             arvif->vdev_id, ret);
826
827         return ret;
828 }
829
830 static int ath11k_mac_set_kickout(struct ath11k_vif *arvif)
831 {
832         struct ath11k *ar = arvif->ar;
833         u32 param;
834         int ret;
835
836         ret = ath11k_wmi_pdev_set_param(ar, WMI_PDEV_PARAM_STA_KICKOUT_TH,
837                                         ATH11K_KICKOUT_THRESHOLD,
838                                         ar->pdev->pdev_id);
839         if (ret) {
840                 ath11k_warn(ar->ab, "failed to set kickout threshold on vdev %i: %d\n",
841                             arvif->vdev_id, ret);
842                 return ret;
843         }
844
845         param = WMI_VDEV_PARAM_AP_KEEPALIVE_MIN_IDLE_INACTIVE_TIME_SECS;
846         ret = ath11k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id, param,
847                                             ATH11K_KEEPALIVE_MIN_IDLE);
848         if (ret) {
849                 ath11k_warn(ar->ab, "failed to set keepalive minimum idle time on vdev %i: %d\n",
850                             arvif->vdev_id, ret);
851                 return ret;
852         }
853
854         param = WMI_VDEV_PARAM_AP_KEEPALIVE_MAX_IDLE_INACTIVE_TIME_SECS;
855         ret = ath11k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id, param,
856                                             ATH11K_KEEPALIVE_MAX_IDLE);
857         if (ret) {
858                 ath11k_warn(ar->ab, "failed to set keepalive maximum idle time on vdev %i: %d\n",
859                             arvif->vdev_id, ret);
860                 return ret;
861         }
862
863         param = WMI_VDEV_PARAM_AP_KEEPALIVE_MAX_UNRESPONSIVE_TIME_SECS;
864         ret = ath11k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id, param,
865                                             ATH11K_KEEPALIVE_MAX_UNRESPONSIVE);
866         if (ret) {
867                 ath11k_warn(ar->ab, "failed to set keepalive maximum unresponsive time on vdev %i: %d\n",
868                             arvif->vdev_id, ret);
869                 return ret;
870         }
871
872         return 0;
873 }
874
875 void ath11k_mac_peer_cleanup_all(struct ath11k *ar)
876 {
877         struct ath11k_peer *peer, *tmp;
878         struct ath11k_base *ab = ar->ab;
879
880         lockdep_assert_held(&ar->conf_mutex);
881
882         mutex_lock(&ab->tbl_mtx_lock);
883         spin_lock_bh(&ab->base_lock);
884         list_for_each_entry_safe(peer, tmp, &ab->peers, list) {
885                 ath11k_peer_rx_tid_cleanup(ar, peer);
886                 ath11k_peer_rhash_delete(ab, peer);
887                 list_del(&peer->list);
888                 kfree(peer);
889         }
890         spin_unlock_bh(&ab->base_lock);
891         mutex_unlock(&ab->tbl_mtx_lock);
892
893         ar->num_peers = 0;
894         ar->num_stations = 0;
895 }
896
897 static inline int ath11k_mac_vdev_setup_sync(struct ath11k *ar)
898 {
899         lockdep_assert_held(&ar->conf_mutex);
900
901         if (test_bit(ATH11K_FLAG_CRASH_FLUSH, &ar->ab->dev_flags))
902                 return -ESHUTDOWN;
903
904         if (!wait_for_completion_timeout(&ar->vdev_setup_done,
905                                          ATH11K_VDEV_SETUP_TIMEOUT_HZ))
906                 return -ETIMEDOUT;
907
908         return ar->last_wmi_vdev_start_status ? -EINVAL : 0;
909 }
910
911 static void
912 ath11k_mac_get_any_chandef_iter(struct ieee80211_hw *hw,
913                                 struct ieee80211_chanctx_conf *conf,
914                                 void *data)
915 {
916         struct cfg80211_chan_def **def = data;
917
918         *def = &conf->def;
919 }
920
921 static int ath11k_mac_monitor_vdev_start(struct ath11k *ar, int vdev_id,
922                                          struct cfg80211_chan_def *chandef)
923 {
924         struct ieee80211_channel *channel;
925         struct wmi_vdev_start_req_arg arg = {};
926         int ret;
927
928         lockdep_assert_held(&ar->conf_mutex);
929
930         channel = chandef->chan;
931
932         arg.vdev_id = vdev_id;
933         arg.channel.freq = channel->center_freq;
934         arg.channel.band_center_freq1 = chandef->center_freq1;
935         arg.channel.band_center_freq2 = chandef->center_freq2;
936
937         arg.channel.mode = ath11k_phymodes[chandef->chan->band][chandef->width];
938         arg.channel.chan_radar = !!(channel->flags & IEEE80211_CHAN_RADAR);
939
940         arg.channel.min_power = 0;
941         arg.channel.max_power = channel->max_power;
942         arg.channel.max_reg_power = channel->max_reg_power;
943         arg.channel.max_antenna_gain = channel->max_antenna_gain;
944
945         arg.pref_tx_streams = ar->num_tx_chains;
946         arg.pref_rx_streams = ar->num_rx_chains;
947
948         arg.channel.passive = !!(chandef->chan->flags & IEEE80211_CHAN_NO_IR);
949
950         reinit_completion(&ar->vdev_setup_done);
951         reinit_completion(&ar->vdev_delete_done);
952
953         ret = ath11k_wmi_vdev_start(ar, &arg, false);
954         if (ret) {
955                 ath11k_warn(ar->ab, "failed to request monitor vdev %i start: %d\n",
956                             vdev_id, ret);
957                 return ret;
958         }
959
960         ret = ath11k_mac_vdev_setup_sync(ar);
961         if (ret) {
962                 ath11k_warn(ar->ab, "failed to synchronize setup for monitor vdev %i start: %d\n",
963                             vdev_id, ret);
964                 return ret;
965         }
966
967         ret = ath11k_wmi_vdev_up(ar, vdev_id, 0, ar->mac_addr);
968         if (ret) {
969                 ath11k_warn(ar->ab, "failed to put up monitor vdev %i: %d\n",
970                             vdev_id, ret);
971                 goto vdev_stop;
972         }
973
974         ath11k_dbg(ar->ab, ATH11K_DBG_MAC, "mac monitor vdev %i started\n",
975                    vdev_id);
976
977         return 0;
978
979 vdev_stop:
980         reinit_completion(&ar->vdev_setup_done);
981
982         ret = ath11k_wmi_vdev_stop(ar, vdev_id);
983         if (ret) {
984                 ath11k_warn(ar->ab, "failed to stop monitor vdev %i after start failure: %d\n",
985                             vdev_id, ret);
986                 return ret;
987         }
988
989         ret = ath11k_mac_vdev_setup_sync(ar);
990         if (ret) {
991                 ath11k_warn(ar->ab, "failed to synchronize setup for vdev %i stop: %d\n",
992                             vdev_id, ret);
993                 return ret;
994         }
995
996         return -EIO;
997 }
998
999 static int ath11k_mac_monitor_vdev_stop(struct ath11k *ar)
1000 {
1001         int ret;
1002
1003         lockdep_assert_held(&ar->conf_mutex);
1004
1005         reinit_completion(&ar->vdev_setup_done);
1006
1007         ret = ath11k_wmi_vdev_stop(ar, ar->monitor_vdev_id);
1008         if (ret) {
1009                 ath11k_warn(ar->ab, "failed to request monitor vdev %i stop: %d\n",
1010                             ar->monitor_vdev_id, ret);
1011                 return ret;
1012         }
1013
1014         ret = ath11k_mac_vdev_setup_sync(ar);
1015         if (ret) {
1016                 ath11k_warn(ar->ab, "failed to synchronize monitor vdev %i stop: %d\n",
1017                             ar->monitor_vdev_id, ret);
1018                 return ret;
1019         }
1020
1021         ret = ath11k_wmi_vdev_down(ar, ar->monitor_vdev_id);
1022         if (ret) {
1023                 ath11k_warn(ar->ab, "failed to put down monitor vdev %i: %d\n",
1024                             ar->monitor_vdev_id, ret);
1025                 return ret;
1026         }
1027
1028         ath11k_dbg(ar->ab, ATH11K_DBG_MAC, "mac monitor vdev %i stopped\n",
1029                    ar->monitor_vdev_id);
1030
1031         return 0;
1032 }
1033
1034 static int ath11k_mac_monitor_vdev_create(struct ath11k *ar)
1035 {
1036         struct ath11k_pdev *pdev = ar->pdev;
1037         struct vdev_create_params param = {};
1038         int bit, ret;
1039         u8 tmp_addr[6] = {0};
1040         u16 nss;
1041
1042         lockdep_assert_held(&ar->conf_mutex);
1043
1044         if (test_bit(ATH11K_FLAG_MONITOR_VDEV_CREATED, &ar->monitor_flags))
1045                 return 0;
1046
1047         if (ar->ab->free_vdev_map == 0) {
1048                 ath11k_warn(ar->ab, "failed to find free vdev id for monitor vdev\n");
1049                 return -ENOMEM;
1050         }
1051
1052         bit = __ffs64(ar->ab->free_vdev_map);
1053
1054         ar->monitor_vdev_id = bit;
1055
1056         param.if_id = ar->monitor_vdev_id;
1057         param.type = WMI_VDEV_TYPE_MONITOR;
1058         param.subtype = WMI_VDEV_SUBTYPE_NONE;
1059         param.pdev_id = pdev->pdev_id;
1060
1061         if (pdev->cap.supported_bands & WMI_HOST_WLAN_2G_CAP) {
1062                 param.chains[NL80211_BAND_2GHZ].tx = ar->num_tx_chains;
1063                 param.chains[NL80211_BAND_2GHZ].rx = ar->num_rx_chains;
1064         }
1065         if (pdev->cap.supported_bands & WMI_HOST_WLAN_5G_CAP) {
1066                 param.chains[NL80211_BAND_5GHZ].tx = ar->num_tx_chains;
1067                 param.chains[NL80211_BAND_5GHZ].rx = ar->num_rx_chains;
1068         }
1069
1070         ret = ath11k_wmi_vdev_create(ar, tmp_addr, &param);
1071         if (ret) {
1072                 ath11k_warn(ar->ab, "failed to request monitor vdev %i creation: %d\n",
1073                             ar->monitor_vdev_id, ret);
1074                 ar->monitor_vdev_id = -1;
1075                 return ret;
1076         }
1077
1078         nss = get_num_chains(ar->cfg_tx_chainmask) ? : 1;
1079         ret = ath11k_wmi_vdev_set_param_cmd(ar, ar->monitor_vdev_id,
1080                                             WMI_VDEV_PARAM_NSS, nss);
1081         if (ret) {
1082                 ath11k_warn(ar->ab, "failed to set vdev %d chainmask 0x%x, nss %d :%d\n",
1083                             ar->monitor_vdev_id, ar->cfg_tx_chainmask, nss, ret);
1084                 goto err_vdev_del;
1085         }
1086
1087         ret = ath11k_mac_txpower_recalc(ar);
1088         if (ret) {
1089                 ath11k_warn(ar->ab, "failed to recalc txpower for monitor vdev %d: %d\n",
1090                             ar->monitor_vdev_id, ret);
1091                 goto err_vdev_del;
1092         }
1093
1094         ar->allocated_vdev_map |= 1LL << ar->monitor_vdev_id;
1095         ar->ab->free_vdev_map &= ~(1LL << ar->monitor_vdev_id);
1096         ar->num_created_vdevs++;
1097         set_bit(ATH11K_FLAG_MONITOR_VDEV_CREATED, &ar->monitor_flags);
1098
1099         ath11k_dbg(ar->ab, ATH11K_DBG_MAC, "mac monitor vdev %d created\n",
1100                    ar->monitor_vdev_id);
1101
1102         return 0;
1103
1104 err_vdev_del:
1105         ath11k_wmi_vdev_delete(ar, ar->monitor_vdev_id);
1106         ar->monitor_vdev_id = -1;
1107         return ret;
1108 }
1109
1110 static int ath11k_mac_monitor_vdev_delete(struct ath11k *ar)
1111 {
1112         int ret;
1113         unsigned long time_left;
1114
1115         lockdep_assert_held(&ar->conf_mutex);
1116
1117         if (!test_bit(ATH11K_FLAG_MONITOR_VDEV_CREATED, &ar->monitor_flags))
1118                 return 0;
1119
1120         reinit_completion(&ar->vdev_delete_done);
1121
1122         ret = ath11k_wmi_vdev_delete(ar, ar->monitor_vdev_id);
1123         if (ret) {
1124                 ath11k_warn(ar->ab, "failed to request wmi monitor vdev %i removal: %d\n",
1125                             ar->monitor_vdev_id, ret);
1126                 return ret;
1127         }
1128
1129         time_left = wait_for_completion_timeout(&ar->vdev_delete_done,
1130                                                 ATH11K_VDEV_DELETE_TIMEOUT_HZ);
1131         if (time_left == 0) {
1132                 ath11k_warn(ar->ab, "Timeout in receiving vdev delete response\n");
1133         } else {
1134                 ath11k_dbg(ar->ab, ATH11K_DBG_MAC, "mac monitor vdev %d deleted\n",
1135                            ar->monitor_vdev_id);
1136
1137                 ar->allocated_vdev_map &= ~(1LL << ar->monitor_vdev_id);
1138                 ar->ab->free_vdev_map |= 1LL << (ar->monitor_vdev_id);
1139                 ar->num_created_vdevs--;
1140                 ar->monitor_vdev_id = -1;
1141                 clear_bit(ATH11K_FLAG_MONITOR_VDEV_CREATED, &ar->monitor_flags);
1142         }
1143
1144         return ret;
1145 }
1146
1147 static int ath11k_mac_monitor_start(struct ath11k *ar)
1148 {
1149         struct cfg80211_chan_def *chandef = NULL;
1150         int ret;
1151
1152         lockdep_assert_held(&ar->conf_mutex);
1153
1154         if (test_bit(ATH11K_FLAG_MONITOR_STARTED, &ar->monitor_flags))
1155                 return 0;
1156
1157         ieee80211_iter_chan_contexts_atomic(ar->hw,
1158                                             ath11k_mac_get_any_chandef_iter,
1159                                             &chandef);
1160         if (!chandef)
1161                 return 0;
1162
1163         ret = ath11k_mac_monitor_vdev_start(ar, ar->monitor_vdev_id, chandef);
1164         if (ret) {
1165                 ath11k_warn(ar->ab, "failed to start monitor vdev: %d\n", ret);
1166                 ath11k_mac_monitor_vdev_delete(ar);
1167                 return ret;
1168         }
1169
1170         set_bit(ATH11K_FLAG_MONITOR_STARTED, &ar->monitor_flags);
1171
1172         ar->num_started_vdevs++;
1173         ret = ath11k_dp_tx_htt_monitor_mode_ring_config(ar, false);
1174         if (ret) {
1175                 ath11k_warn(ar->ab, "failed to configure htt monitor mode ring during start: %d",
1176                             ret);
1177                 return ret;
1178         }
1179
1180         ath11k_dbg(ar->ab, ATH11K_DBG_MAC, "mac monitor started\n");
1181
1182         return 0;
1183 }
1184
1185 static int ath11k_mac_monitor_stop(struct ath11k *ar)
1186 {
1187         int ret;
1188
1189         lockdep_assert_held(&ar->conf_mutex);
1190
1191         if (!test_bit(ATH11K_FLAG_MONITOR_STARTED, &ar->monitor_flags))
1192                 return 0;
1193
1194         ret = ath11k_mac_monitor_vdev_stop(ar);
1195         if (ret) {
1196                 ath11k_warn(ar->ab, "failed to stop monitor vdev: %d\n", ret);
1197                 return ret;
1198         }
1199
1200         clear_bit(ATH11K_FLAG_MONITOR_STARTED, &ar->monitor_flags);
1201         ar->num_started_vdevs--;
1202
1203         ret = ath11k_dp_tx_htt_monitor_mode_ring_config(ar, true);
1204         if (ret) {
1205                 ath11k_warn(ar->ab, "failed to configure htt monitor mode ring during stop: %d",
1206                             ret);
1207                 return ret;
1208         }
1209
1210         ath11k_dbg(ar->ab, ATH11K_DBG_MAC, "mac monitor stopped ret %d\n", ret);
1211
1212         return 0;
1213 }
1214
1215 static int ath11k_mac_vif_setup_ps(struct ath11k_vif *arvif)
1216 {
1217         struct ath11k *ar = arvif->ar;
1218         struct ieee80211_vif *vif = arvif->vif;
1219         struct ieee80211_conf *conf = &ar->hw->conf;
1220         enum wmi_sta_powersave_param param;
1221         enum wmi_sta_ps_mode psmode;
1222         int ret;
1223         int timeout;
1224         bool enable_ps;
1225
1226         lockdep_assert_held(&arvif->ar->conf_mutex);
1227
1228         if (arvif->vif->type != NL80211_IFTYPE_STATION)
1229                 return 0;
1230
1231         enable_ps = arvif->ps;
1232
1233         if (!arvif->is_started) {
1234                 /* mac80211 can update vif powersave state while disconnected.
1235                  * Firmware doesn't behave nicely and consumes more power than
1236                  * necessary if PS is disabled on a non-started vdev. Hence
1237                  * force-enable PS for non-running vdevs.
1238                  */
1239                 psmode = WMI_STA_PS_MODE_ENABLED;
1240         } else if (enable_ps) {
1241                 psmode = WMI_STA_PS_MODE_ENABLED;
1242                 param = WMI_STA_PS_PARAM_INACTIVITY_TIME;
1243
1244                 timeout = conf->dynamic_ps_timeout;
1245                 if (timeout == 0) {
1246                         /* firmware doesn't like 0 */
1247                         timeout = ieee80211_tu_to_usec(vif->bss_conf.beacon_int) / 1000;
1248                 }
1249
1250                 ret = ath11k_wmi_set_sta_ps_param(ar, arvif->vdev_id, param,
1251                                                   timeout);
1252                 if (ret) {
1253                         ath11k_warn(ar->ab, "failed to set inactivity time for vdev %d: %i\n",
1254                                     arvif->vdev_id, ret);
1255                         return ret;
1256                 }
1257         } else {
1258                 psmode = WMI_STA_PS_MODE_DISABLED;
1259         }
1260
1261         ath11k_dbg(ar->ab, ATH11K_DBG_MAC, "mac vdev %d psmode %s\n",
1262                    arvif->vdev_id, psmode ? "enable" : "disable");
1263
1264         ret = ath11k_wmi_pdev_set_ps_mode(ar, arvif->vdev_id, psmode);
1265         if (ret) {
1266                 ath11k_warn(ar->ab, "failed to set sta power save mode %d for vdev %d: %d\n",
1267                             psmode, arvif->vdev_id, ret);
1268                 return ret;
1269         }
1270
1271         return 0;
1272 }
1273
1274 static int ath11k_mac_config_ps(struct ath11k *ar)
1275 {
1276         struct ath11k_vif *arvif;
1277         int ret = 0;
1278
1279         lockdep_assert_held(&ar->conf_mutex);
1280
1281         list_for_each_entry(arvif, &ar->arvifs, list) {
1282                 ret = ath11k_mac_vif_setup_ps(arvif);
1283                 if (ret) {
1284                         ath11k_warn(ar->ab, "failed to setup powersave: %d\n", ret);
1285                         break;
1286                 }
1287         }
1288
1289         return ret;
1290 }
1291
1292 static int ath11k_mac_op_config(struct ieee80211_hw *hw, u32 changed)
1293 {
1294         struct ath11k *ar = hw->priv;
1295         struct ieee80211_conf *conf = &hw->conf;
1296         int ret = 0;
1297
1298         mutex_lock(&ar->conf_mutex);
1299
1300         if (changed & IEEE80211_CONF_CHANGE_MONITOR) {
1301                 if (conf->flags & IEEE80211_CONF_MONITOR) {
1302                         set_bit(ATH11K_FLAG_MONITOR_CONF_ENABLED, &ar->monitor_flags);
1303
1304                         if (test_bit(ATH11K_FLAG_MONITOR_VDEV_CREATED,
1305                                      &ar->monitor_flags))
1306                                 goto out;
1307
1308                         ret = ath11k_mac_monitor_vdev_create(ar);
1309                         if (ret) {
1310                                 ath11k_warn(ar->ab, "failed to create monitor vdev: %d",
1311                                             ret);
1312                                 goto out;
1313                         }
1314
1315                         ret = ath11k_mac_monitor_start(ar);
1316                         if (ret) {
1317                                 ath11k_warn(ar->ab, "failed to start monitor: %d",
1318                                             ret);
1319                                 goto err_mon_del;
1320                         }
1321                 } else {
1322                         clear_bit(ATH11K_FLAG_MONITOR_CONF_ENABLED, &ar->monitor_flags);
1323
1324                         if (!test_bit(ATH11K_FLAG_MONITOR_VDEV_CREATED,
1325                                       &ar->monitor_flags))
1326                                 goto out;
1327
1328                         ret = ath11k_mac_monitor_stop(ar);
1329                         if (ret) {
1330                                 ath11k_warn(ar->ab, "failed to stop monitor: %d",
1331                                             ret);
1332                                 goto out;
1333                         }
1334
1335                         ret = ath11k_mac_monitor_vdev_delete(ar);
1336                         if (ret) {
1337                                 ath11k_warn(ar->ab, "failed to delete monitor vdev: %d",
1338                                             ret);
1339                                 goto out;
1340                         }
1341                 }
1342         }
1343
1344 out:
1345         mutex_unlock(&ar->conf_mutex);
1346         return ret;
1347
1348 err_mon_del:
1349         ath11k_mac_monitor_vdev_delete(ar);
1350         mutex_unlock(&ar->conf_mutex);
1351         return ret;
1352 }
1353
1354 static int ath11k_mac_setup_bcn_tmpl(struct ath11k_vif *arvif)
1355 {
1356         struct ath11k *ar = arvif->ar;
1357         struct ath11k_base *ab = ar->ab;
1358         struct ieee80211_hw *hw = ar->hw;
1359         struct ieee80211_vif *vif = arvif->vif;
1360         struct ieee80211_mutable_offsets offs = {};
1361         struct sk_buff *bcn;
1362         struct ieee80211_mgmt *mgmt;
1363         u8 *ies;
1364         int ret;
1365
1366         if (arvif->vdev_type != WMI_VDEV_TYPE_AP)
1367                 return 0;
1368
1369         bcn = ieee80211_beacon_get_template(hw, vif, &offs, 0);
1370         if (!bcn) {
1371                 ath11k_warn(ab, "failed to get beacon template from mac80211\n");
1372                 return -EPERM;
1373         }
1374
1375         ies = bcn->data + ieee80211_get_hdrlen_from_skb(bcn);
1376         ies += sizeof(mgmt->u.beacon);
1377
1378         if (cfg80211_find_ie(WLAN_EID_RSN, ies, (skb_tail_pointer(bcn) - ies)))
1379                 arvif->rsnie_present = true;
1380         else
1381                 arvif->rsnie_present = false;
1382
1383         if (cfg80211_find_vendor_ie(WLAN_OUI_MICROSOFT,
1384                                     WLAN_OUI_TYPE_MICROSOFT_WPA,
1385                                     ies, (skb_tail_pointer(bcn) - ies)))
1386                 arvif->wpaie_present = true;
1387         else
1388                 arvif->wpaie_present = false;
1389
1390         ret = ath11k_wmi_bcn_tmpl(ar, arvif->vdev_id, &offs, bcn);
1391
1392         kfree_skb(bcn);
1393
1394         if (ret)
1395                 ath11k_warn(ab, "failed to submit beacon template command: %d\n",
1396                             ret);
1397
1398         return ret;
1399 }
1400
1401 void ath11k_mac_bcn_tx_event(struct ath11k_vif *arvif)
1402 {
1403         struct ieee80211_vif *vif = arvif->vif;
1404
1405         if (!vif->bss_conf.color_change_active && !arvif->bcca_zero_sent)
1406                 return;
1407
1408         if (vif->bss_conf.color_change_active &&
1409             ieee80211_beacon_cntdwn_is_complete(vif)) {
1410                 arvif->bcca_zero_sent = true;
1411                 ieee80211_color_change_finish(vif);
1412                 return;
1413         }
1414
1415         arvif->bcca_zero_sent = false;
1416
1417         if (vif->bss_conf.color_change_active)
1418                 ieee80211_beacon_update_cntdwn(vif);
1419         ath11k_mac_setup_bcn_tmpl(arvif);
1420 }
1421
1422 static void ath11k_control_beaconing(struct ath11k_vif *arvif,
1423                                      struct ieee80211_bss_conf *info)
1424 {
1425         struct ath11k *ar = arvif->ar;
1426         int ret = 0;
1427
1428         lockdep_assert_held(&arvif->ar->conf_mutex);
1429
1430         if (!info->enable_beacon) {
1431                 ret = ath11k_wmi_vdev_down(ar, arvif->vdev_id);
1432                 if (ret)
1433                         ath11k_warn(ar->ab, "failed to down vdev_id %i: %d\n",
1434                                     arvif->vdev_id, ret);
1435
1436                 arvif->is_up = false;
1437                 return;
1438         }
1439
1440         /* Install the beacon template to the FW */
1441         ret = ath11k_mac_setup_bcn_tmpl(arvif);
1442         if (ret) {
1443                 ath11k_warn(ar->ab, "failed to update bcn tmpl during vdev up: %d\n",
1444                             ret);
1445                 return;
1446         }
1447
1448         arvif->tx_seq_no = 0x1000;
1449
1450         arvif->aid = 0;
1451
1452         ether_addr_copy(arvif->bssid, info->bssid);
1453
1454         ret = ath11k_wmi_vdev_up(arvif->ar, arvif->vdev_id, arvif->aid,
1455                                  arvif->bssid);
1456         if (ret) {
1457                 ath11k_warn(ar->ab, "failed to bring up vdev %d: %i\n",
1458                             arvif->vdev_id, ret);
1459                 return;
1460         }
1461
1462         arvif->is_up = true;
1463
1464         ath11k_dbg(ar->ab, ATH11K_DBG_MAC, "mac vdev %d up\n", arvif->vdev_id);
1465 }
1466
1467 static void ath11k_mac_handle_beacon_iter(void *data, u8 *mac,
1468                                           struct ieee80211_vif *vif)
1469 {
1470         struct sk_buff *skb = data;
1471         struct ieee80211_mgmt *mgmt = (void *)skb->data;
1472         struct ath11k_vif *arvif = (void *)vif->drv_priv;
1473
1474         if (vif->type != NL80211_IFTYPE_STATION)
1475                 return;
1476
1477         if (!ether_addr_equal(mgmt->bssid, vif->bss_conf.bssid))
1478                 return;
1479
1480         cancel_delayed_work(&arvif->connection_loss_work);
1481 }
1482
1483 void ath11k_mac_handle_beacon(struct ath11k *ar, struct sk_buff *skb)
1484 {
1485         ieee80211_iterate_active_interfaces_atomic(ar->hw,
1486                                                    IEEE80211_IFACE_ITER_NORMAL,
1487                                                    ath11k_mac_handle_beacon_iter,
1488                                                    skb);
1489 }
1490
1491 static void ath11k_mac_handle_beacon_miss_iter(void *data, u8 *mac,
1492                                                struct ieee80211_vif *vif)
1493 {
1494         u32 *vdev_id = data;
1495         struct ath11k_vif *arvif = (void *)vif->drv_priv;
1496         struct ath11k *ar = arvif->ar;
1497         struct ieee80211_hw *hw = ar->hw;
1498
1499         if (arvif->vdev_id != *vdev_id)
1500                 return;
1501
1502         if (!arvif->is_up)
1503                 return;
1504
1505         ieee80211_beacon_loss(vif);
1506
1507         /* Firmware doesn't report beacon loss events repeatedly. If AP probe
1508          * (done by mac80211) succeeds but beacons do not resume then it
1509          * doesn't make sense to continue operation. Queue connection loss work
1510          * which can be cancelled when beacon is received.
1511          */
1512         ieee80211_queue_delayed_work(hw, &arvif->connection_loss_work,
1513                                      ATH11K_CONNECTION_LOSS_HZ);
1514 }
1515
1516 void ath11k_mac_handle_beacon_miss(struct ath11k *ar, u32 vdev_id)
1517 {
1518         ieee80211_iterate_active_interfaces_atomic(ar->hw,
1519                                                    IEEE80211_IFACE_ITER_NORMAL,
1520                                                    ath11k_mac_handle_beacon_miss_iter,
1521                                                    &vdev_id);
1522 }
1523
1524 static void ath11k_mac_vif_sta_connection_loss_work(struct work_struct *work)
1525 {
1526         struct ath11k_vif *arvif = container_of(work, struct ath11k_vif,
1527                                                 connection_loss_work.work);
1528         struct ieee80211_vif *vif = arvif->vif;
1529
1530         if (!arvif->is_up)
1531                 return;
1532
1533         ieee80211_connection_loss(vif);
1534 }
1535
1536 static void ath11k_peer_assoc_h_basic(struct ath11k *ar,
1537                                       struct ieee80211_vif *vif,
1538                                       struct ieee80211_sta *sta,
1539                                       struct peer_assoc_params *arg)
1540 {
1541         struct ath11k_vif *arvif = (void *)vif->drv_priv;
1542         u32 aid;
1543
1544         lockdep_assert_held(&ar->conf_mutex);
1545
1546         if (vif->type == NL80211_IFTYPE_STATION)
1547                 aid = vif->cfg.aid;
1548         else
1549                 aid = sta->aid;
1550
1551         ether_addr_copy(arg->peer_mac, sta->addr);
1552         arg->vdev_id = arvif->vdev_id;
1553         arg->peer_associd = aid;
1554         arg->auth_flag = true;
1555         /* TODO: STA WAR in ath10k for listen interval required? */
1556         arg->peer_listen_intval = ar->hw->conf.listen_interval;
1557         arg->peer_nss = 1;
1558         arg->peer_caps = vif->bss_conf.assoc_capability;
1559 }
1560
1561 static void ath11k_peer_assoc_h_crypto(struct ath11k *ar,
1562                                        struct ieee80211_vif *vif,
1563                                        struct ieee80211_sta *sta,
1564                                        struct peer_assoc_params *arg)
1565 {
1566         struct ieee80211_bss_conf *info = &vif->bss_conf;
1567         struct cfg80211_chan_def def;
1568         struct cfg80211_bss *bss;
1569         struct ath11k_vif *arvif = (struct ath11k_vif *)vif->drv_priv;
1570         const u8 *rsnie = NULL;
1571         const u8 *wpaie = NULL;
1572
1573         lockdep_assert_held(&ar->conf_mutex);
1574
1575         if (WARN_ON(ath11k_mac_vif_chan(vif, &def)))
1576                 return;
1577
1578         bss = cfg80211_get_bss(ar->hw->wiphy, def.chan, info->bssid, NULL, 0,
1579                                IEEE80211_BSS_TYPE_ANY, IEEE80211_PRIVACY_ANY);
1580
1581         if (arvif->rsnie_present || arvif->wpaie_present) {
1582                 arg->need_ptk_4_way = true;
1583                 if (arvif->wpaie_present)
1584                         arg->need_gtk_2_way = true;
1585         } else if (bss) {
1586                 const struct cfg80211_bss_ies *ies;
1587
1588                 rcu_read_lock();
1589                 rsnie = ieee80211_bss_get_ie(bss, WLAN_EID_RSN);
1590
1591                 ies = rcu_dereference(bss->ies);
1592
1593                 wpaie = cfg80211_find_vendor_ie(WLAN_OUI_MICROSOFT,
1594                                                 WLAN_OUI_TYPE_MICROSOFT_WPA,
1595                                                 ies->data,
1596                                                 ies->len);
1597                 rcu_read_unlock();
1598                 cfg80211_put_bss(ar->hw->wiphy, bss);
1599         }
1600
1601         /* FIXME: base on RSN IE/WPA IE is a correct idea? */
1602         if (rsnie || wpaie) {
1603                 ath11k_dbg(ar->ab, ATH11K_DBG_WMI,
1604                            "%s: rsn ie found\n", __func__);
1605                 arg->need_ptk_4_way = true;
1606         }
1607
1608         if (wpaie) {
1609                 ath11k_dbg(ar->ab, ATH11K_DBG_WMI,
1610                            "%s: wpa ie found\n", __func__);
1611                 arg->need_gtk_2_way = true;
1612         }
1613
1614         if (sta->mfp) {
1615                 /* TODO: Need to check if FW supports PMF? */
1616                 arg->is_pmf_enabled = true;
1617         }
1618
1619         /* TODO: safe_mode_enabled (bypass 4-way handshake) flag req? */
1620 }
1621
1622 static void ath11k_peer_assoc_h_rates(struct ath11k *ar,
1623                                       struct ieee80211_vif *vif,
1624                                       struct ieee80211_sta *sta,
1625                                       struct peer_assoc_params *arg)
1626 {
1627         struct ath11k_vif *arvif = (void *)vif->drv_priv;
1628         struct wmi_rate_set_arg *rateset = &arg->peer_legacy_rates;
1629         struct cfg80211_chan_def def;
1630         const struct ieee80211_supported_band *sband;
1631         const struct ieee80211_rate *rates;
1632         enum nl80211_band band;
1633         u32 ratemask;
1634         u8 rate;
1635         int i;
1636
1637         lockdep_assert_held(&ar->conf_mutex);
1638
1639         if (WARN_ON(ath11k_mac_vif_chan(vif, &def)))
1640                 return;
1641
1642         band = def.chan->band;
1643         sband = ar->hw->wiphy->bands[band];
1644         ratemask = sta->deflink.supp_rates[band];
1645         ratemask &= arvif->bitrate_mask.control[band].legacy;
1646         rates = sband->bitrates;
1647
1648         rateset->num_rates = 0;
1649
1650         for (i = 0; i < 32; i++, ratemask >>= 1, rates++) {
1651                 if (!(ratemask & 1))
1652                         continue;
1653
1654                 rate = ath11k_mac_bitrate_to_rate(rates->bitrate);
1655                 rateset->rates[rateset->num_rates] = rate;
1656                 rateset->num_rates++;
1657         }
1658 }
1659
1660 static bool
1661 ath11k_peer_assoc_h_ht_masked(const u8 ht_mcs_mask[IEEE80211_HT_MCS_MASK_LEN])
1662 {
1663         int nss;
1664
1665         for (nss = 0; nss < IEEE80211_HT_MCS_MASK_LEN; nss++)
1666                 if (ht_mcs_mask[nss])
1667                         return false;
1668
1669         return true;
1670 }
1671
1672 static bool
1673 ath11k_peer_assoc_h_vht_masked(const u16 vht_mcs_mask[])
1674 {
1675         int nss;
1676
1677         for (nss = 0; nss < NL80211_VHT_NSS_MAX; nss++)
1678                 if (vht_mcs_mask[nss])
1679                         return false;
1680
1681         return true;
1682 }
1683
1684 static void ath11k_peer_assoc_h_ht(struct ath11k *ar,
1685                                    struct ieee80211_vif *vif,
1686                                    struct ieee80211_sta *sta,
1687                                    struct peer_assoc_params *arg)
1688 {
1689         const struct ieee80211_sta_ht_cap *ht_cap = &sta->deflink.ht_cap;
1690         struct ath11k_vif *arvif = (void *)vif->drv_priv;
1691         struct cfg80211_chan_def def;
1692         enum nl80211_band band;
1693         const u8 *ht_mcs_mask;
1694         int i, n;
1695         u8 max_nss;
1696         u32 stbc;
1697
1698         lockdep_assert_held(&ar->conf_mutex);
1699
1700         if (WARN_ON(ath11k_mac_vif_chan(vif, &def)))
1701                 return;
1702
1703         if (!ht_cap->ht_supported)
1704                 return;
1705
1706         band = def.chan->band;
1707         ht_mcs_mask = arvif->bitrate_mask.control[band].ht_mcs;
1708
1709         if (ath11k_peer_assoc_h_ht_masked(ht_mcs_mask))
1710                 return;
1711
1712         arg->ht_flag = true;
1713
1714         arg->peer_max_mpdu = (1 << (IEEE80211_HT_MAX_AMPDU_FACTOR +
1715                                     ht_cap->ampdu_factor)) - 1;
1716
1717         arg->peer_mpdu_density =
1718                 ath11k_parse_mpdudensity(ht_cap->ampdu_density);
1719
1720         arg->peer_ht_caps = ht_cap->cap;
1721         arg->peer_rate_caps |= WMI_HOST_RC_HT_FLAG;
1722
1723         if (ht_cap->cap & IEEE80211_HT_CAP_LDPC_CODING)
1724                 arg->ldpc_flag = true;
1725
1726         if (sta->deflink.bandwidth >= IEEE80211_STA_RX_BW_40) {
1727                 arg->bw_40 = true;
1728                 arg->peer_rate_caps |= WMI_HOST_RC_CW40_FLAG;
1729         }
1730
1731         /* As firmware handles this two flags (IEEE80211_HT_CAP_SGI_20
1732          * and IEEE80211_HT_CAP_SGI_40) for enabling SGI, we reset
1733          * both flags if guard interval is Default GI
1734          */
1735         if (arvif->bitrate_mask.control[band].gi == NL80211_TXRATE_DEFAULT_GI)
1736                 arg->peer_ht_caps &= ~(IEEE80211_HT_CAP_SGI_20 |
1737                                 IEEE80211_HT_CAP_SGI_40);
1738
1739         if (arvif->bitrate_mask.control[band].gi != NL80211_TXRATE_FORCE_LGI) {
1740                 if (ht_cap->cap & (IEEE80211_HT_CAP_SGI_20 |
1741                     IEEE80211_HT_CAP_SGI_40))
1742                         arg->peer_rate_caps |= WMI_HOST_RC_SGI_FLAG;
1743         }
1744
1745         if (ht_cap->cap & IEEE80211_HT_CAP_TX_STBC) {
1746                 arg->peer_rate_caps |= WMI_HOST_RC_TX_STBC_FLAG;
1747                 arg->stbc_flag = true;
1748         }
1749
1750         if (ht_cap->cap & IEEE80211_HT_CAP_RX_STBC) {
1751                 stbc = ht_cap->cap & IEEE80211_HT_CAP_RX_STBC;
1752                 stbc = stbc >> IEEE80211_HT_CAP_RX_STBC_SHIFT;
1753                 stbc = stbc << WMI_HOST_RC_RX_STBC_FLAG_S;
1754                 arg->peer_rate_caps |= stbc;
1755                 arg->stbc_flag = true;
1756         }
1757
1758         if (ht_cap->mcs.rx_mask[1] && ht_cap->mcs.rx_mask[2])
1759                 arg->peer_rate_caps |= WMI_HOST_RC_TS_FLAG;
1760         else if (ht_cap->mcs.rx_mask[1])
1761                 arg->peer_rate_caps |= WMI_HOST_RC_DS_FLAG;
1762
1763         for (i = 0, n = 0, max_nss = 0; i < IEEE80211_HT_MCS_MASK_LEN * 8; i++)
1764                 if ((ht_cap->mcs.rx_mask[i / 8] & BIT(i % 8)) &&
1765                     (ht_mcs_mask[i / 8] & BIT(i % 8))) {
1766                         max_nss = (i / 8) + 1;
1767                         arg->peer_ht_rates.rates[n++] = i;
1768                 }
1769
1770         /* This is a workaround for HT-enabled STAs which break the spec
1771          * and have no HT capabilities RX mask (no HT RX MCS map).
1772          *
1773          * As per spec, in section 20.3.5 Modulation and coding scheme (MCS),
1774          * MCS 0 through 7 are mandatory in 20MHz with 800 ns GI at all STAs.
1775          *
1776          * Firmware asserts if such situation occurs.
1777          */
1778         if (n == 0) {
1779                 arg->peer_ht_rates.num_rates = 8;
1780                 for (i = 0; i < arg->peer_ht_rates.num_rates; i++)
1781                         arg->peer_ht_rates.rates[i] = i;
1782         } else {
1783                 arg->peer_ht_rates.num_rates = n;
1784                 arg->peer_nss = min(sta->deflink.rx_nss, max_nss);
1785         }
1786
1787         ath11k_dbg(ar->ab, ATH11K_DBG_MAC, "mac ht peer %pM mcs cnt %d nss %d\n",
1788                    arg->peer_mac,
1789                    arg->peer_ht_rates.num_rates,
1790                    arg->peer_nss);
1791 }
1792
1793 static int ath11k_mac_get_max_vht_mcs_map(u16 mcs_map, int nss)
1794 {
1795         switch ((mcs_map >> (2 * nss)) & 0x3) {
1796         case IEEE80211_VHT_MCS_SUPPORT_0_7: return BIT(8) - 1;
1797         case IEEE80211_VHT_MCS_SUPPORT_0_8: return BIT(9) - 1;
1798         case IEEE80211_VHT_MCS_SUPPORT_0_9: return BIT(10) - 1;
1799         }
1800         return 0;
1801 }
1802
1803 static u16
1804 ath11k_peer_assoc_h_vht_limit(u16 tx_mcs_set,
1805                               const u16 vht_mcs_limit[NL80211_VHT_NSS_MAX])
1806 {
1807         int idx_limit;
1808         int nss;
1809         u16 mcs_map;
1810         u16 mcs;
1811
1812         for (nss = 0; nss < NL80211_VHT_NSS_MAX; nss++) {
1813                 mcs_map = ath11k_mac_get_max_vht_mcs_map(tx_mcs_set, nss) &
1814                           vht_mcs_limit[nss];
1815
1816                 if (mcs_map)
1817                         idx_limit = fls(mcs_map) - 1;
1818                 else
1819                         idx_limit = -1;
1820
1821                 switch (idx_limit) {
1822                 case 0:
1823                 case 1:
1824                 case 2:
1825                 case 3:
1826                 case 4:
1827                 case 5:
1828                 case 6:
1829                 case 7:
1830                         mcs = IEEE80211_VHT_MCS_SUPPORT_0_7;
1831                         break;
1832                 case 8:
1833                         mcs = IEEE80211_VHT_MCS_SUPPORT_0_8;
1834                         break;
1835                 case 9:
1836                         mcs = IEEE80211_VHT_MCS_SUPPORT_0_9;
1837                         break;
1838                 default:
1839                         WARN_ON(1);
1840                         fallthrough;
1841                 case -1:
1842                         mcs = IEEE80211_VHT_MCS_NOT_SUPPORTED;
1843                         break;
1844                 }
1845
1846                 tx_mcs_set &= ~(0x3 << (nss * 2));
1847                 tx_mcs_set |= mcs << (nss * 2);
1848         }
1849
1850         return tx_mcs_set;
1851 }
1852
1853 static u8 ath11k_get_nss_160mhz(struct ath11k *ar,
1854                                 u8 max_nss)
1855 {
1856         u8 nss_ratio_info = ar->pdev->cap.nss_ratio_info;
1857         u8 max_sup_nss = 0;
1858
1859         switch (nss_ratio_info) {
1860         case WMI_NSS_RATIO_1BY2_NSS:
1861                 max_sup_nss = max_nss >> 1;
1862                 break;
1863         case WMI_NSS_RATIO_3BY4_NSS:
1864                 ath11k_warn(ar->ab, "WMI_NSS_RATIO_3BY4_NSS not supported\n");
1865                 break;
1866         case WMI_NSS_RATIO_1_NSS:
1867                 max_sup_nss = max_nss;
1868                 break;
1869         case WMI_NSS_RATIO_2_NSS:
1870                 ath11k_warn(ar->ab, "WMI_NSS_RATIO_2_NSS not supported\n");
1871                 break;
1872         default:
1873                 ath11k_warn(ar->ab, "invalid nss ratio received from firmware: %d\n",
1874                             nss_ratio_info);
1875                 break;
1876         }
1877
1878         return max_sup_nss;
1879 }
1880
1881 static void ath11k_peer_assoc_h_vht(struct ath11k *ar,
1882                                     struct ieee80211_vif *vif,
1883                                     struct ieee80211_sta *sta,
1884                                     struct peer_assoc_params *arg)
1885 {
1886         const struct ieee80211_sta_vht_cap *vht_cap = &sta->deflink.vht_cap;
1887         struct ath11k_vif *arvif = (void *)vif->drv_priv;
1888         struct cfg80211_chan_def def;
1889         enum nl80211_band band;
1890         u16 *vht_mcs_mask;
1891         u8 ampdu_factor;
1892         u8 max_nss, vht_mcs;
1893         int i, vht_nss, nss_idx;
1894         bool user_rate_valid = true;
1895         u32 rx_nss, tx_nss, nss_160;
1896
1897         if (WARN_ON(ath11k_mac_vif_chan(vif, &def)))
1898                 return;
1899
1900         if (!vht_cap->vht_supported)
1901                 return;
1902
1903         band = def.chan->band;
1904         vht_mcs_mask = arvif->bitrate_mask.control[band].vht_mcs;
1905
1906         if (ath11k_peer_assoc_h_vht_masked(vht_mcs_mask))
1907                 return;
1908
1909         arg->vht_flag = true;
1910
1911         /* TODO: similar flags required? */
1912         arg->vht_capable = true;
1913
1914         if (def.chan->band == NL80211_BAND_2GHZ)
1915                 arg->vht_ng_flag = true;
1916
1917         arg->peer_vht_caps = vht_cap->cap;
1918
1919         ampdu_factor = (vht_cap->cap &
1920                         IEEE80211_VHT_CAP_MAX_A_MPDU_LENGTH_EXPONENT_MASK) >>
1921                        IEEE80211_VHT_CAP_MAX_A_MPDU_LENGTH_EXPONENT_SHIFT;
1922
1923         /* Workaround: Some Netgear/Linksys 11ac APs set Rx A-MPDU factor to
1924          * zero in VHT IE. Using it would result in degraded throughput.
1925          * arg->peer_max_mpdu at this point contains HT max_mpdu so keep
1926          * it if VHT max_mpdu is smaller.
1927          */
1928         arg->peer_max_mpdu = max(arg->peer_max_mpdu,
1929                                  (1U << (IEEE80211_HT_MAX_AMPDU_FACTOR +
1930                                         ampdu_factor)) - 1);
1931
1932         if (sta->deflink.bandwidth == IEEE80211_STA_RX_BW_80)
1933                 arg->bw_80 = true;
1934
1935         if (sta->deflink.bandwidth == IEEE80211_STA_RX_BW_160)
1936                 arg->bw_160 = true;
1937
1938         vht_nss =  ath11k_mac_max_vht_nss(vht_mcs_mask);
1939
1940         if (vht_nss > sta->deflink.rx_nss) {
1941                 user_rate_valid = false;
1942                 for (nss_idx = sta->deflink.rx_nss - 1; nss_idx >= 0; nss_idx--) {
1943                         if (vht_mcs_mask[nss_idx]) {
1944                                 user_rate_valid = true;
1945                                 break;
1946                         }
1947                 }
1948         }
1949
1950         if (!user_rate_valid) {
1951                 ath11k_dbg(ar->ab, ATH11K_DBG_MAC, "mac setting vht range mcs value to peer supported nss %d for peer %pM\n",
1952                            sta->deflink.rx_nss, sta->addr);
1953                 vht_mcs_mask[sta->deflink.rx_nss - 1] = vht_mcs_mask[vht_nss - 1];
1954         }
1955
1956         /* Calculate peer NSS capability from VHT capabilities if STA
1957          * supports VHT.
1958          */
1959         for (i = 0, max_nss = 0; i < NL80211_VHT_NSS_MAX; i++) {
1960                 vht_mcs = __le16_to_cpu(vht_cap->vht_mcs.rx_mcs_map) >>
1961                           (2 * i) & 3;
1962
1963                 if (vht_mcs != IEEE80211_VHT_MCS_NOT_SUPPORTED &&
1964                     vht_mcs_mask[i])
1965                         max_nss = i + 1;
1966         }
1967         arg->peer_nss = min(sta->deflink.rx_nss, max_nss);
1968         arg->rx_max_rate = __le16_to_cpu(vht_cap->vht_mcs.rx_highest);
1969         arg->rx_mcs_set = __le16_to_cpu(vht_cap->vht_mcs.rx_mcs_map);
1970         arg->tx_max_rate = __le16_to_cpu(vht_cap->vht_mcs.tx_highest);
1971         arg->tx_mcs_set = ath11k_peer_assoc_h_vht_limit(
1972                 __le16_to_cpu(vht_cap->vht_mcs.tx_mcs_map), vht_mcs_mask);
1973
1974         /* In IPQ8074 platform, VHT mcs rate 10 and 11 is enabled by default.
1975          * VHT mcs rate 10 and 11 is not suppoerted in 11ac standard.
1976          * so explicitly disable the VHT MCS rate 10 and 11 in 11ac mode.
1977          */
1978         arg->tx_mcs_set &= ~IEEE80211_VHT_MCS_SUPPORT_0_11_MASK;
1979         arg->tx_mcs_set |= IEEE80211_DISABLE_VHT_MCS_SUPPORT_0_11;
1980
1981         if ((arg->tx_mcs_set & IEEE80211_VHT_MCS_NOT_SUPPORTED) ==
1982                         IEEE80211_VHT_MCS_NOT_SUPPORTED)
1983                 arg->peer_vht_caps &= ~IEEE80211_VHT_CAP_MU_BEAMFORMEE_CAPABLE;
1984
1985         /* TODO:  Check */
1986         arg->tx_max_mcs_nss = 0xFF;
1987
1988         if (arg->peer_phymode == MODE_11AC_VHT160 ||
1989             arg->peer_phymode == MODE_11AC_VHT80_80) {
1990                 tx_nss = ath11k_get_nss_160mhz(ar, max_nss);
1991                 rx_nss = min(arg->peer_nss, tx_nss);
1992                 arg->peer_bw_rxnss_override = ATH11K_BW_NSS_MAP_ENABLE;
1993
1994                 if (!rx_nss) {
1995                         ath11k_warn(ar->ab, "invalid max_nss\n");
1996                         return;
1997                 }
1998
1999                 if (arg->peer_phymode == MODE_11AC_VHT160)
2000                         nss_160 = FIELD_PREP(ATH11K_PEER_RX_NSS_160MHZ, rx_nss - 1);
2001                 else
2002                         nss_160 = FIELD_PREP(ATH11K_PEER_RX_NSS_80_80MHZ, rx_nss - 1);
2003
2004                 arg->peer_bw_rxnss_override |= nss_160;
2005         }
2006
2007         ath11k_dbg(ar->ab, ATH11K_DBG_MAC,
2008                    "mac vht peer %pM max_mpdu %d flags 0x%x nss_override 0x%x\n",
2009                    sta->addr, arg->peer_max_mpdu, arg->peer_flags,
2010                    arg->peer_bw_rxnss_override);
2011 }
2012
2013 static int ath11k_mac_get_max_he_mcs_map(u16 mcs_map, int nss)
2014 {
2015         switch ((mcs_map >> (2 * nss)) & 0x3) {
2016         case IEEE80211_HE_MCS_SUPPORT_0_7: return BIT(8) - 1;
2017         case IEEE80211_HE_MCS_SUPPORT_0_9: return BIT(10) - 1;
2018         case IEEE80211_HE_MCS_SUPPORT_0_11: return BIT(12) - 1;
2019         }
2020         return 0;
2021 }
2022
2023 static u16 ath11k_peer_assoc_h_he_limit(u16 tx_mcs_set,
2024                                         const u16 he_mcs_limit[NL80211_HE_NSS_MAX])
2025 {
2026         int idx_limit;
2027         int nss;
2028         u16 mcs_map;
2029         u16 mcs;
2030
2031         for (nss = 0; nss < NL80211_HE_NSS_MAX; nss++) {
2032                 mcs_map = ath11k_mac_get_max_he_mcs_map(tx_mcs_set, nss) &
2033                         he_mcs_limit[nss];
2034
2035                 if (mcs_map)
2036                         idx_limit = fls(mcs_map) - 1;
2037                 else
2038                         idx_limit = -1;
2039
2040                 switch (idx_limit) {
2041                 case 0 ... 7:
2042                         mcs = IEEE80211_HE_MCS_SUPPORT_0_7;
2043                         break;
2044                 case 8:
2045                 case 9:
2046                         mcs = IEEE80211_HE_MCS_SUPPORT_0_9;
2047                         break;
2048                 case 10:
2049                 case 11:
2050                         mcs = IEEE80211_HE_MCS_SUPPORT_0_11;
2051                         break;
2052                 default:
2053                         WARN_ON(1);
2054                         fallthrough;
2055                 case -1:
2056                         mcs = IEEE80211_HE_MCS_NOT_SUPPORTED;
2057                         break;
2058                 }
2059
2060                 tx_mcs_set &= ~(0x3 << (nss * 2));
2061                 tx_mcs_set |= mcs << (nss * 2);
2062         }
2063
2064         return tx_mcs_set;
2065 }
2066
2067 static bool
2068 ath11k_peer_assoc_h_he_masked(const u16 he_mcs_mask[NL80211_HE_NSS_MAX])
2069 {
2070         int nss;
2071
2072         for (nss = 0; nss < NL80211_HE_NSS_MAX; nss++)
2073                 if (he_mcs_mask[nss])
2074                         return false;
2075
2076         return true;
2077 }
2078
2079 static void ath11k_peer_assoc_h_he(struct ath11k *ar,
2080                                    struct ieee80211_vif *vif,
2081                                    struct ieee80211_sta *sta,
2082                                    struct peer_assoc_params *arg)
2083 {
2084         struct ath11k_vif *arvif = (void *)vif->drv_priv;
2085         struct cfg80211_chan_def def;
2086         const struct ieee80211_sta_he_cap *he_cap = &sta->deflink.he_cap;
2087         enum nl80211_band band;
2088         u16 he_mcs_mask[NL80211_HE_NSS_MAX];
2089         u8 max_nss, he_mcs;
2090         u16 he_tx_mcs = 0, v = 0;
2091         int i, he_nss, nss_idx;
2092         bool user_rate_valid = true;
2093         u32 rx_nss, tx_nss, nss_160;
2094         u8 ampdu_factor, rx_mcs_80, rx_mcs_160;
2095         u16 mcs_160_map, mcs_80_map;
2096         bool support_160;
2097
2098         if (WARN_ON(ath11k_mac_vif_chan(vif, &def)))
2099                 return;
2100
2101         if (!he_cap->has_he)
2102                 return;
2103
2104         band = def.chan->band;
2105         memcpy(he_mcs_mask, arvif->bitrate_mask.control[band].he_mcs,
2106                sizeof(he_mcs_mask));
2107
2108         if (ath11k_peer_assoc_h_he_masked(he_mcs_mask))
2109                 return;
2110
2111         arg->he_flag = true;
2112         support_160 = !!(he_cap->he_cap_elem.phy_cap_info[0] &
2113                   IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_160MHZ_IN_5G);
2114
2115         /* Supported HE-MCS and NSS Set of peer he_cap is intersection with self he_cp */
2116         mcs_160_map = le16_to_cpu(he_cap->he_mcs_nss_supp.rx_mcs_160);
2117         mcs_80_map = le16_to_cpu(he_cap->he_mcs_nss_supp.rx_mcs_80);
2118
2119         if (support_160) {
2120                 for (i = 7; i >= 0; i--) {
2121                         u8 mcs_160 = (mcs_160_map >> (2 * i)) & 3;
2122
2123                         if (mcs_160 != IEEE80211_VHT_MCS_NOT_SUPPORTED) {
2124                                 rx_mcs_160 = i + 1;
2125                                 break;
2126                         }
2127                 }
2128         }
2129
2130         for (i = 7; i >= 0; i--) {
2131                 u8 mcs_80 = (mcs_80_map >> (2 * i)) & 3;
2132
2133                 if (mcs_80 != IEEE80211_VHT_MCS_NOT_SUPPORTED) {
2134                         rx_mcs_80 = i + 1;
2135                         break;
2136                 }
2137         }
2138
2139         if (support_160)
2140                 max_nss = min(rx_mcs_80, rx_mcs_160);
2141         else
2142                 max_nss = rx_mcs_80;
2143
2144         arg->peer_nss = min(sta->deflink.rx_nss, max_nss);
2145
2146         memcpy_and_pad(&arg->peer_he_cap_macinfo,
2147                        sizeof(arg->peer_he_cap_macinfo),
2148                        he_cap->he_cap_elem.mac_cap_info,
2149                        sizeof(he_cap->he_cap_elem.mac_cap_info),
2150                        0);
2151         memcpy_and_pad(&arg->peer_he_cap_phyinfo,
2152                        sizeof(arg->peer_he_cap_phyinfo),
2153                        he_cap->he_cap_elem.phy_cap_info,
2154                        sizeof(he_cap->he_cap_elem.phy_cap_info),
2155                        0);
2156         arg->peer_he_ops = vif->bss_conf.he_oper.params;
2157
2158         /* the top most byte is used to indicate BSS color info */
2159         arg->peer_he_ops &= 0xffffff;
2160
2161         /* As per section 26.6.1 11ax Draft5.0, if the Max AMPDU Exponent Extension
2162          * in HE cap is zero, use the arg->peer_max_mpdu as calculated while parsing
2163          * VHT caps(if VHT caps is present) or HT caps (if VHT caps is not present).
2164          *
2165          * For non-zero value of Max AMPDU Extponent Extension in HE MAC caps,
2166          * if a HE STA sends VHT cap and HE cap IE in assoc request then, use
2167          * MAX_AMPDU_LEN_FACTOR as 20 to calculate max_ampdu length.
2168          * If a HE STA that does not send VHT cap, but HE and HT cap in assoc
2169          * request, then use MAX_AMPDU_LEN_FACTOR as 16 to calculate max_ampdu
2170          * length.
2171          */
2172         ampdu_factor = u8_get_bits(he_cap->he_cap_elem.mac_cap_info[3],
2173                                    IEEE80211_HE_MAC_CAP3_MAX_AMPDU_LEN_EXP_MASK);
2174
2175         if (ampdu_factor) {
2176                 if (sta->deflink.vht_cap.vht_supported)
2177                         arg->peer_max_mpdu = (1 << (IEEE80211_HE_VHT_MAX_AMPDU_FACTOR +
2178                                                     ampdu_factor)) - 1;
2179                 else if (sta->deflink.ht_cap.ht_supported)
2180                         arg->peer_max_mpdu = (1 << (IEEE80211_HE_HT_MAX_AMPDU_FACTOR +
2181                                                     ampdu_factor)) - 1;
2182         }
2183
2184         if (he_cap->he_cap_elem.phy_cap_info[6] &
2185             IEEE80211_HE_PHY_CAP6_PPE_THRESHOLD_PRESENT) {
2186                 int bit = 7;
2187                 int nss, ru;
2188
2189                 arg->peer_ppet.numss_m1 = he_cap->ppe_thres[0] &
2190                                           IEEE80211_PPE_THRES_NSS_MASK;
2191                 arg->peer_ppet.ru_bit_mask =
2192                         (he_cap->ppe_thres[0] &
2193                          IEEE80211_PPE_THRES_RU_INDEX_BITMASK_MASK) >>
2194                         IEEE80211_PPE_THRES_RU_INDEX_BITMASK_POS;
2195
2196                 for (nss = 0; nss <= arg->peer_ppet.numss_m1; nss++) {
2197                         for (ru = 0; ru < 4; ru++) {
2198                                 u32 val = 0;
2199                                 int i;
2200
2201                                 if ((arg->peer_ppet.ru_bit_mask & BIT(ru)) == 0)
2202                                         continue;
2203                                 for (i = 0; i < 6; i++) {
2204                                         val >>= 1;
2205                                         val |= ((he_cap->ppe_thres[bit / 8] >>
2206                                                  (bit % 8)) & 0x1) << 5;
2207                                         bit++;
2208                                 }
2209                                 arg->peer_ppet.ppet16_ppet8_ru3_ru0[nss] |=
2210                                                                 val << (ru * 6);
2211                         }
2212                 }
2213         }
2214
2215         if (he_cap->he_cap_elem.mac_cap_info[0] & IEEE80211_HE_MAC_CAP0_TWT_RES)
2216                 arg->twt_responder = true;
2217         if (he_cap->he_cap_elem.mac_cap_info[0] & IEEE80211_HE_MAC_CAP0_TWT_REQ)
2218                 arg->twt_requester = true;
2219
2220         he_nss =  ath11k_mac_max_he_nss(he_mcs_mask);
2221
2222         if (he_nss > sta->deflink.rx_nss) {
2223                 user_rate_valid = false;
2224                 for (nss_idx = sta->deflink.rx_nss - 1; nss_idx >= 0; nss_idx--) {
2225                         if (he_mcs_mask[nss_idx]) {
2226                                 user_rate_valid = true;
2227                                 break;
2228                         }
2229                 }
2230         }
2231
2232         if (!user_rate_valid) {
2233                 ath11k_dbg(ar->ab, ATH11K_DBG_MAC, "mac setting he range mcs value to peer supported nss %d for peer %pM\n",
2234                            sta->deflink.rx_nss, sta->addr);
2235                 he_mcs_mask[sta->deflink.rx_nss - 1] = he_mcs_mask[he_nss - 1];
2236         }
2237
2238         switch (sta->deflink.bandwidth) {
2239         case IEEE80211_STA_RX_BW_160:
2240                 if (he_cap->he_cap_elem.phy_cap_info[0] &
2241                     IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_80PLUS80_MHZ_IN_5G) {
2242                         v = le16_to_cpu(he_cap->he_mcs_nss_supp.rx_mcs_80p80);
2243                         v = ath11k_peer_assoc_h_he_limit(v, he_mcs_mask);
2244                         arg->peer_he_rx_mcs_set[WMI_HECAP_TXRX_MCS_NSS_IDX_80_80] = v;
2245
2246                         v = le16_to_cpu(he_cap->he_mcs_nss_supp.tx_mcs_80p80);
2247                         arg->peer_he_tx_mcs_set[WMI_HECAP_TXRX_MCS_NSS_IDX_80_80] = v;
2248
2249                         arg->peer_he_mcs_count++;
2250                         he_tx_mcs = v;
2251                 }
2252                 v = le16_to_cpu(he_cap->he_mcs_nss_supp.rx_mcs_160);
2253                 arg->peer_he_rx_mcs_set[WMI_HECAP_TXRX_MCS_NSS_IDX_160] = v;
2254
2255                 v = le16_to_cpu(he_cap->he_mcs_nss_supp.tx_mcs_160);
2256                 v = ath11k_peer_assoc_h_he_limit(v, he_mcs_mask);
2257                 arg->peer_he_tx_mcs_set[WMI_HECAP_TXRX_MCS_NSS_IDX_160] = v;
2258
2259                 arg->peer_he_mcs_count++;
2260                 if (!he_tx_mcs)
2261                         he_tx_mcs = v;
2262                 fallthrough;
2263
2264         default:
2265                 v = le16_to_cpu(he_cap->he_mcs_nss_supp.rx_mcs_80);
2266                 arg->peer_he_rx_mcs_set[WMI_HECAP_TXRX_MCS_NSS_IDX_80] = v;
2267
2268                 v = le16_to_cpu(he_cap->he_mcs_nss_supp.tx_mcs_80);
2269                 v = ath11k_peer_assoc_h_he_limit(v, he_mcs_mask);
2270                 arg->peer_he_tx_mcs_set[WMI_HECAP_TXRX_MCS_NSS_IDX_80] = v;
2271
2272                 arg->peer_he_mcs_count++;
2273                 if (!he_tx_mcs)
2274                         he_tx_mcs = v;
2275                 break;
2276         }
2277
2278         /* Calculate peer NSS capability from HE capabilities if STA
2279          * supports HE.
2280          */
2281         for (i = 0, max_nss = 0; i < NL80211_HE_NSS_MAX; i++) {
2282                 he_mcs = he_tx_mcs >> (2 * i) & 3;
2283
2284                 /* In case of fixed rates, MCS Range in he_tx_mcs might have
2285                  * unsupported range, with he_mcs_mask set, so check either of them
2286                  * to find nss.
2287                  */
2288                 if (he_mcs != IEEE80211_HE_MCS_NOT_SUPPORTED ||
2289                     he_mcs_mask[i])
2290                         max_nss = i + 1;
2291         }
2292         arg->peer_nss = min(sta->deflink.rx_nss, max_nss);
2293
2294         if (arg->peer_phymode == MODE_11AX_HE160 ||
2295             arg->peer_phymode == MODE_11AX_HE80_80) {
2296                 tx_nss = ath11k_get_nss_160mhz(ar, max_nss);
2297                 rx_nss = min(arg->peer_nss, tx_nss);
2298                 arg->peer_bw_rxnss_override = ATH11K_BW_NSS_MAP_ENABLE;
2299
2300                 if (!rx_nss) {
2301                         ath11k_warn(ar->ab, "invalid max_nss\n");
2302                         return;
2303                 }
2304
2305                 if (arg->peer_phymode == MODE_11AX_HE160)
2306                         nss_160 = FIELD_PREP(ATH11K_PEER_RX_NSS_160MHZ, rx_nss - 1);
2307                 else
2308                         nss_160 = FIELD_PREP(ATH11K_PEER_RX_NSS_80_80MHZ, rx_nss - 1);
2309
2310                 arg->peer_bw_rxnss_override |= nss_160;
2311         }
2312
2313         ath11k_dbg(ar->ab, ATH11K_DBG_MAC,
2314                    "mac he peer %pM nss %d mcs cnt %d nss_override 0x%x\n",
2315                    sta->addr, arg->peer_nss,
2316                    arg->peer_he_mcs_count,
2317                    arg->peer_bw_rxnss_override);
2318 }
2319
2320 static void ath11k_peer_assoc_h_he_6ghz(struct ath11k *ar,
2321                                         struct ieee80211_vif *vif,
2322                                         struct ieee80211_sta *sta,
2323                                         struct peer_assoc_params *arg)
2324 {
2325         const struct ieee80211_sta_he_cap *he_cap = &sta->deflink.he_cap;
2326         struct cfg80211_chan_def def;
2327         enum nl80211_band band;
2328         u8  ampdu_factor;
2329
2330         if (WARN_ON(ath11k_mac_vif_chan(vif, &def)))
2331                 return;
2332
2333         band = def.chan->band;
2334
2335         if (!arg->he_flag || band != NL80211_BAND_6GHZ || !sta->deflink.he_6ghz_capa.capa)
2336                 return;
2337
2338         if (sta->deflink.bandwidth == IEEE80211_STA_RX_BW_40)
2339                 arg->bw_40 = true;
2340
2341         if (sta->deflink.bandwidth == IEEE80211_STA_RX_BW_80)
2342                 arg->bw_80 = true;
2343
2344         if (sta->deflink.bandwidth == IEEE80211_STA_RX_BW_160)
2345                 arg->bw_160 = true;
2346
2347         arg->peer_he_caps_6ghz = le16_to_cpu(sta->deflink.he_6ghz_capa.capa);
2348         arg->peer_mpdu_density =
2349                 ath11k_parse_mpdudensity(FIELD_GET(IEEE80211_HE_6GHZ_CAP_MIN_MPDU_START,
2350                                                    arg->peer_he_caps_6ghz));
2351
2352         /* From IEEE Std 802.11ax-2021 - Section 10.12.2: An HE STA shall be capable of
2353          * receiving A-MPDU where the A-MPDU pre-EOF padding length is up to the value
2354          * indicated by the Maximum A-MPDU Length Exponent Extension field in the HE
2355          * Capabilities element and the Maximum A-MPDU Length Exponent field in HE 6 GHz
2356          * Band Capabilities element in the 6 GHz band.
2357          *
2358          * Here, we are extracting the Max A-MPDU Exponent Extension from HE caps and
2359          * factor is the Maximum A-MPDU Length Exponent from HE 6 GHZ Band capability.
2360          */
2361         ampdu_factor = FIELD_GET(IEEE80211_HE_MAC_CAP3_MAX_AMPDU_LEN_EXP_MASK,
2362                                  he_cap->he_cap_elem.mac_cap_info[3]) +
2363                         FIELD_GET(IEEE80211_HE_6GHZ_CAP_MAX_AMPDU_LEN_EXP,
2364                                   arg->peer_he_caps_6ghz);
2365
2366         arg->peer_max_mpdu = (1u << (IEEE80211_HE_6GHZ_MAX_AMPDU_FACTOR +
2367                                      ampdu_factor)) - 1;
2368 }
2369
2370 static void ath11k_peer_assoc_h_smps(struct ieee80211_sta *sta,
2371                                      struct peer_assoc_params *arg)
2372 {
2373         const struct ieee80211_sta_ht_cap *ht_cap = &sta->deflink.ht_cap;
2374         int smps;
2375
2376         if (!ht_cap->ht_supported && !sta->deflink.he_6ghz_capa.capa)
2377                 return;
2378
2379         if (ht_cap->ht_supported) {
2380                 smps = ht_cap->cap & IEEE80211_HT_CAP_SM_PS;
2381                 smps >>= IEEE80211_HT_CAP_SM_PS_SHIFT;
2382         } else {
2383                 smps = le16_get_bits(sta->deflink.he_6ghz_capa.capa,
2384                                      IEEE80211_HE_6GHZ_CAP_SM_PS);
2385         }
2386
2387         switch (smps) {
2388         case WLAN_HT_CAP_SM_PS_STATIC:
2389                 arg->static_mimops_flag = true;
2390                 break;
2391         case WLAN_HT_CAP_SM_PS_DYNAMIC:
2392                 arg->dynamic_mimops_flag = true;
2393                 break;
2394         case WLAN_HT_CAP_SM_PS_DISABLED:
2395                 arg->spatial_mux_flag = true;
2396                 break;
2397         default:
2398                 break;
2399         }
2400 }
2401
2402 static void ath11k_peer_assoc_h_qos(struct ath11k *ar,
2403                                     struct ieee80211_vif *vif,
2404                                     struct ieee80211_sta *sta,
2405                                     struct peer_assoc_params *arg)
2406 {
2407         struct ath11k_vif *arvif = (void *)vif->drv_priv;
2408
2409         switch (arvif->vdev_type) {
2410         case WMI_VDEV_TYPE_AP:
2411                 if (sta->wme) {
2412                         /* TODO: Check WME vs QoS */
2413                         arg->is_wme_set = true;
2414                         arg->qos_flag = true;
2415                 }
2416
2417                 if (sta->wme && sta->uapsd_queues) {
2418                         /* TODO: Check WME vs QoS */
2419                         arg->is_wme_set = true;
2420                         arg->apsd_flag = true;
2421                         arg->peer_rate_caps |= WMI_HOST_RC_UAPSD_FLAG;
2422                 }
2423                 break;
2424         case WMI_VDEV_TYPE_STA:
2425                 if (sta->wme) {
2426                         arg->is_wme_set = true;
2427                         arg->qos_flag = true;
2428                 }
2429                 break;
2430         default:
2431                 break;
2432         }
2433
2434         ath11k_dbg(ar->ab, ATH11K_DBG_MAC, "mac peer %pM qos %d\n",
2435                    sta->addr, arg->qos_flag);
2436 }
2437
2438 static int ath11k_peer_assoc_qos_ap(struct ath11k *ar,
2439                                     struct ath11k_vif *arvif,
2440                                     struct ieee80211_sta *sta)
2441 {
2442         struct ap_ps_params params;
2443         u32 max_sp;
2444         u32 uapsd;
2445         int ret;
2446
2447         lockdep_assert_held(&ar->conf_mutex);
2448
2449         params.vdev_id = arvif->vdev_id;
2450
2451         ath11k_dbg(ar->ab, ATH11K_DBG_MAC, "mac uapsd_queues 0x%x max_sp %d\n",
2452                    sta->uapsd_queues, sta->max_sp);
2453
2454         uapsd = 0;
2455         if (sta->uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_VO)
2456                 uapsd |= WMI_AP_PS_UAPSD_AC3_DELIVERY_EN |
2457                          WMI_AP_PS_UAPSD_AC3_TRIGGER_EN;
2458         if (sta->uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_VI)
2459                 uapsd |= WMI_AP_PS_UAPSD_AC2_DELIVERY_EN |
2460                          WMI_AP_PS_UAPSD_AC2_TRIGGER_EN;
2461         if (sta->uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_BK)
2462                 uapsd |= WMI_AP_PS_UAPSD_AC1_DELIVERY_EN |
2463                          WMI_AP_PS_UAPSD_AC1_TRIGGER_EN;
2464         if (sta->uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_BE)
2465                 uapsd |= WMI_AP_PS_UAPSD_AC0_DELIVERY_EN |
2466                          WMI_AP_PS_UAPSD_AC0_TRIGGER_EN;
2467
2468         max_sp = 0;
2469         if (sta->max_sp < MAX_WMI_AP_PS_PEER_PARAM_MAX_SP)
2470                 max_sp = sta->max_sp;
2471
2472         params.param = WMI_AP_PS_PEER_PARAM_UAPSD;
2473         params.value = uapsd;
2474         ret = ath11k_wmi_send_set_ap_ps_param_cmd(ar, sta->addr, &params);
2475         if (ret)
2476                 goto err;
2477
2478         params.param = WMI_AP_PS_PEER_PARAM_MAX_SP;
2479         params.value = max_sp;
2480         ret = ath11k_wmi_send_set_ap_ps_param_cmd(ar, sta->addr, &params);
2481         if (ret)
2482                 goto err;
2483
2484         /* TODO revisit during testing */
2485         params.param = WMI_AP_PS_PEER_PARAM_SIFS_RESP_FRMTYPE;
2486         params.value = DISABLE_SIFS_RESPONSE_TRIGGER;
2487         ret = ath11k_wmi_send_set_ap_ps_param_cmd(ar, sta->addr, &params);
2488         if (ret)
2489                 goto err;
2490
2491         params.param = WMI_AP_PS_PEER_PARAM_SIFS_RESP_UAPSD;
2492         params.value = DISABLE_SIFS_RESPONSE_TRIGGER;
2493         ret = ath11k_wmi_send_set_ap_ps_param_cmd(ar, sta->addr, &params);
2494         if (ret)
2495                 goto err;
2496
2497         return 0;
2498
2499 err:
2500         ath11k_warn(ar->ab, "failed to set ap ps peer param %d for vdev %i: %d\n",
2501                     params.param, arvif->vdev_id, ret);
2502         return ret;
2503 }
2504
2505 static bool ath11k_mac_sta_has_ofdm_only(struct ieee80211_sta *sta)
2506 {
2507         return sta->deflink.supp_rates[NL80211_BAND_2GHZ] >>
2508                ATH11K_MAC_FIRST_OFDM_RATE_IDX;
2509 }
2510
2511 static enum wmi_phy_mode ath11k_mac_get_phymode_vht(struct ath11k *ar,
2512                                                     struct ieee80211_sta *sta)
2513 {
2514         if (sta->deflink.bandwidth == IEEE80211_STA_RX_BW_160) {
2515                 switch (sta->deflink.vht_cap.cap &
2516                         IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_MASK) {
2517                 case IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_160MHZ:
2518                         return MODE_11AC_VHT160;
2519                 case IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_160_80PLUS80MHZ:
2520                         return MODE_11AC_VHT80_80;
2521                 default:
2522                         /* not sure if this is a valid case? */
2523                         return MODE_11AC_VHT160;
2524                 }
2525         }
2526
2527         if (sta->deflink.bandwidth == IEEE80211_STA_RX_BW_80)
2528                 return MODE_11AC_VHT80;
2529
2530         if (sta->deflink.bandwidth == IEEE80211_STA_RX_BW_40)
2531                 return MODE_11AC_VHT40;
2532
2533         if (sta->deflink.bandwidth == IEEE80211_STA_RX_BW_20)
2534                 return MODE_11AC_VHT20;
2535
2536         return MODE_UNKNOWN;
2537 }
2538
2539 static enum wmi_phy_mode ath11k_mac_get_phymode_he(struct ath11k *ar,
2540                                                    struct ieee80211_sta *sta)
2541 {
2542         if (sta->deflink.bandwidth == IEEE80211_STA_RX_BW_160) {
2543                 if (sta->deflink.he_cap.he_cap_elem.phy_cap_info[0] &
2544                      IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_160MHZ_IN_5G)
2545                         return MODE_11AX_HE160;
2546                 else if (sta->deflink.he_cap.he_cap_elem.phy_cap_info[0] &
2547                          IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_80PLUS80_MHZ_IN_5G)
2548                         return MODE_11AX_HE80_80;
2549                 /* not sure if this is a valid case? */
2550                 return MODE_11AX_HE160;
2551         }
2552
2553         if (sta->deflink.bandwidth == IEEE80211_STA_RX_BW_80)
2554                 return MODE_11AX_HE80;
2555
2556         if (sta->deflink.bandwidth == IEEE80211_STA_RX_BW_40)
2557                 return MODE_11AX_HE40;
2558
2559         if (sta->deflink.bandwidth == IEEE80211_STA_RX_BW_20)
2560                 return MODE_11AX_HE20;
2561
2562         return MODE_UNKNOWN;
2563 }
2564
2565 static void ath11k_peer_assoc_h_phymode(struct ath11k *ar,
2566                                         struct ieee80211_vif *vif,
2567                                         struct ieee80211_sta *sta,
2568                                         struct peer_assoc_params *arg)
2569 {
2570         struct ath11k_vif *arvif = (void *)vif->drv_priv;
2571         struct cfg80211_chan_def def;
2572         enum nl80211_band band;
2573         const u8 *ht_mcs_mask;
2574         const u16 *vht_mcs_mask;
2575         const u16 *he_mcs_mask;
2576         enum wmi_phy_mode phymode = MODE_UNKNOWN;
2577
2578         if (WARN_ON(ath11k_mac_vif_chan(vif, &def)))
2579                 return;
2580
2581         band = def.chan->band;
2582         ht_mcs_mask = arvif->bitrate_mask.control[band].ht_mcs;
2583         vht_mcs_mask = arvif->bitrate_mask.control[band].vht_mcs;
2584         he_mcs_mask = arvif->bitrate_mask.control[band].he_mcs;
2585
2586         switch (band) {
2587         case NL80211_BAND_2GHZ:
2588                 if (sta->deflink.he_cap.has_he &&
2589                     !ath11k_peer_assoc_h_he_masked(he_mcs_mask)) {
2590                         if (sta->deflink.bandwidth == IEEE80211_STA_RX_BW_80)
2591                                 phymode = MODE_11AX_HE80_2G;
2592                         else if (sta->deflink.bandwidth == IEEE80211_STA_RX_BW_40)
2593                                 phymode = MODE_11AX_HE40_2G;
2594                         else
2595                                 phymode = MODE_11AX_HE20_2G;
2596                 } else if (sta->deflink.vht_cap.vht_supported &&
2597                            !ath11k_peer_assoc_h_vht_masked(vht_mcs_mask)) {
2598                         if (sta->deflink.bandwidth == IEEE80211_STA_RX_BW_40)
2599                                 phymode = MODE_11AC_VHT40;
2600                         else
2601                                 phymode = MODE_11AC_VHT20;
2602                 } else if (sta->deflink.ht_cap.ht_supported &&
2603                            !ath11k_peer_assoc_h_ht_masked(ht_mcs_mask)) {
2604                         if (sta->deflink.bandwidth == IEEE80211_STA_RX_BW_40)
2605                                 phymode = MODE_11NG_HT40;
2606                         else
2607                                 phymode = MODE_11NG_HT20;
2608                 } else if (ath11k_mac_sta_has_ofdm_only(sta)) {
2609                         phymode = MODE_11G;
2610                 } else {
2611                         phymode = MODE_11B;
2612                 }
2613                 break;
2614         case NL80211_BAND_5GHZ:
2615         case NL80211_BAND_6GHZ:
2616                 /* Check HE first */
2617                 if (sta->deflink.he_cap.has_he &&
2618                     !ath11k_peer_assoc_h_he_masked(he_mcs_mask)) {
2619                         phymode = ath11k_mac_get_phymode_he(ar, sta);
2620                 } else if (sta->deflink.vht_cap.vht_supported &&
2621                            !ath11k_peer_assoc_h_vht_masked(vht_mcs_mask)) {
2622                         phymode = ath11k_mac_get_phymode_vht(ar, sta);
2623                 } else if (sta->deflink.ht_cap.ht_supported &&
2624                            !ath11k_peer_assoc_h_ht_masked(ht_mcs_mask)) {
2625                         if (sta->deflink.bandwidth >= IEEE80211_STA_RX_BW_40)
2626                                 phymode = MODE_11NA_HT40;
2627                         else
2628                                 phymode = MODE_11NA_HT20;
2629                 } else {
2630                         phymode = MODE_11A;
2631                 }
2632                 break;
2633         default:
2634                 break;
2635         }
2636
2637         ath11k_dbg(ar->ab, ATH11K_DBG_MAC, "mac peer %pM phymode %s\n",
2638                    sta->addr, ath11k_wmi_phymode_str(phymode));
2639
2640         arg->peer_phymode = phymode;
2641         WARN_ON(phymode == MODE_UNKNOWN);
2642 }
2643
2644 static void ath11k_peer_assoc_prepare(struct ath11k *ar,
2645                                       struct ieee80211_vif *vif,
2646                                       struct ieee80211_sta *sta,
2647                                       struct peer_assoc_params *arg,
2648                                       bool reassoc)
2649 {
2650         struct ath11k_sta *arsta;
2651
2652         lockdep_assert_held(&ar->conf_mutex);
2653
2654         arsta = (struct ath11k_sta *)sta->drv_priv;
2655
2656         memset(arg, 0, sizeof(*arg));
2657
2658         reinit_completion(&ar->peer_assoc_done);
2659
2660         arg->peer_new_assoc = !reassoc;
2661         ath11k_peer_assoc_h_basic(ar, vif, sta, arg);
2662         ath11k_peer_assoc_h_crypto(ar, vif, sta, arg);
2663         ath11k_peer_assoc_h_rates(ar, vif, sta, arg);
2664         ath11k_peer_assoc_h_phymode(ar, vif, sta, arg);
2665         ath11k_peer_assoc_h_ht(ar, vif, sta, arg);
2666         ath11k_peer_assoc_h_vht(ar, vif, sta, arg);
2667         ath11k_peer_assoc_h_he(ar, vif, sta, arg);
2668         ath11k_peer_assoc_h_he_6ghz(ar, vif, sta, arg);
2669         ath11k_peer_assoc_h_qos(ar, vif, sta, arg);
2670         ath11k_peer_assoc_h_smps(sta, arg);
2671
2672         arsta->peer_nss = arg->peer_nss;
2673
2674         /* TODO: amsdu_disable req? */
2675 }
2676
2677 static int ath11k_setup_peer_smps(struct ath11k *ar, struct ath11k_vif *arvif,
2678                                   const u8 *addr,
2679                                   const struct ieee80211_sta_ht_cap *ht_cap,
2680                                   u16 he_6ghz_capa)
2681 {
2682         int smps;
2683
2684         if (!ht_cap->ht_supported && !he_6ghz_capa)
2685                 return 0;
2686
2687         if (ht_cap->ht_supported) {
2688                 smps = ht_cap->cap & IEEE80211_HT_CAP_SM_PS;
2689                 smps >>= IEEE80211_HT_CAP_SM_PS_SHIFT;
2690         } else {
2691                 smps = FIELD_GET(IEEE80211_HE_6GHZ_CAP_SM_PS, he_6ghz_capa);
2692         }
2693
2694         if (smps >= ARRAY_SIZE(ath11k_smps_map))
2695                 return -EINVAL;
2696
2697         return ath11k_wmi_set_peer_param(ar, addr, arvif->vdev_id,
2698                                          WMI_PEER_MIMO_PS_STATE,
2699                                          ath11k_smps_map[smps]);
2700 }
2701
2702 static bool ath11k_mac_set_he_txbf_conf(struct ath11k_vif *arvif)
2703 {
2704         struct ath11k *ar = arvif->ar;
2705         u32 param, value;
2706         int ret;
2707
2708         if (!arvif->vif->bss_conf.he_support)
2709                 return true;
2710
2711         param = WMI_VDEV_PARAM_SET_HEMU_MODE;
2712         value = 0;
2713         if (arvif->vif->bss_conf.he_su_beamformer) {
2714                 value |= FIELD_PREP(HE_MODE_SU_TX_BFER, HE_SU_BFER_ENABLE);
2715                 if (arvif->vif->bss_conf.he_mu_beamformer &&
2716                     arvif->vdev_type == WMI_VDEV_TYPE_AP)
2717                         value |= FIELD_PREP(HE_MODE_MU_TX_BFER, HE_MU_BFER_ENABLE);
2718         }
2719
2720         if (arvif->vif->type != NL80211_IFTYPE_MESH_POINT) {
2721                 value |= FIELD_PREP(HE_MODE_DL_OFDMA, HE_DL_MUOFDMA_ENABLE) |
2722                          FIELD_PREP(HE_MODE_UL_OFDMA, HE_UL_MUOFDMA_ENABLE);
2723
2724                 if (arvif->vif->bss_conf.he_full_ul_mumimo)
2725                         value |= FIELD_PREP(HE_MODE_UL_MUMIMO, HE_UL_MUMIMO_ENABLE);
2726
2727                 if (arvif->vif->bss_conf.he_su_beamformee)
2728                         value |= FIELD_PREP(HE_MODE_SU_TX_BFEE, HE_SU_BFEE_ENABLE);
2729         }
2730
2731         ret = ath11k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id, param, value);
2732         if (ret) {
2733                 ath11k_warn(ar->ab, "failed to set vdev %d HE MU mode: %d\n",
2734                             arvif->vdev_id, ret);
2735                 return false;
2736         }
2737
2738         param = WMI_VDEV_PARAM_SET_HE_SOUNDING_MODE;
2739         value = FIELD_PREP(HE_VHT_SOUNDING_MODE, HE_VHT_SOUNDING_MODE_ENABLE) |
2740                 FIELD_PREP(HE_TRIG_NONTRIG_SOUNDING_MODE,
2741                            HE_TRIG_NONTRIG_SOUNDING_MODE_ENABLE);
2742         ret = ath11k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
2743                                             param, value);
2744         if (ret) {
2745                 ath11k_warn(ar->ab, "failed to set vdev %d sounding mode: %d\n",
2746                             arvif->vdev_id, ret);
2747                 return false;
2748         }
2749         return true;
2750 }
2751
2752 static bool ath11k_mac_vif_recalc_sta_he_txbf(struct ath11k *ar,
2753                                               struct ieee80211_vif *vif,
2754                                               struct ieee80211_sta_he_cap *he_cap)
2755 {
2756         struct ath11k_vif *arvif = (void *)vif->drv_priv;
2757         struct ieee80211_he_cap_elem he_cap_elem = {0};
2758         struct ieee80211_sta_he_cap *cap_band = NULL;
2759         struct cfg80211_chan_def def;
2760         u32 param = WMI_VDEV_PARAM_SET_HEMU_MODE;
2761         u32 hemode = 0;
2762         int ret;
2763
2764         if (!vif->bss_conf.he_support)
2765                 return true;
2766
2767         if (vif->type != NL80211_IFTYPE_STATION)
2768                 return false;
2769
2770         if (WARN_ON(ath11k_mac_vif_chan(vif, &def)))
2771                 return false;
2772
2773         if (def.chan->band == NL80211_BAND_2GHZ)
2774                 cap_band = &ar->mac.iftype[NL80211_BAND_2GHZ][vif->type].he_cap;
2775         else
2776                 cap_band = &ar->mac.iftype[NL80211_BAND_5GHZ][vif->type].he_cap;
2777
2778         memcpy(&he_cap_elem, &cap_band->he_cap_elem, sizeof(he_cap_elem));
2779
2780         if (HECAP_PHY_SUBFME_GET(he_cap_elem.phy_cap_info)) {
2781                 if (HECAP_PHY_SUBFMR_GET(he_cap->he_cap_elem.phy_cap_info))
2782                         hemode |= FIELD_PREP(HE_MODE_SU_TX_BFEE, HE_SU_BFEE_ENABLE);
2783                 if (HECAP_PHY_MUBFMR_GET(he_cap->he_cap_elem.phy_cap_info))
2784                         hemode |= FIELD_PREP(HE_MODE_MU_TX_BFEE, HE_MU_BFEE_ENABLE);
2785         }
2786
2787         if (vif->type != NL80211_IFTYPE_MESH_POINT) {
2788                 hemode |= FIELD_PREP(HE_MODE_DL_OFDMA, HE_DL_MUOFDMA_ENABLE) |
2789                           FIELD_PREP(HE_MODE_UL_OFDMA, HE_UL_MUOFDMA_ENABLE);
2790
2791                 if (HECAP_PHY_ULMUMIMO_GET(he_cap_elem.phy_cap_info))
2792                         if (HECAP_PHY_ULMUMIMO_GET(he_cap->he_cap_elem.phy_cap_info))
2793                                 hemode |= FIELD_PREP(HE_MODE_UL_MUMIMO,
2794                                                      HE_UL_MUMIMO_ENABLE);
2795
2796                 if (FIELD_GET(HE_MODE_MU_TX_BFEE, hemode))
2797                         hemode |= FIELD_PREP(HE_MODE_SU_TX_BFEE, HE_SU_BFEE_ENABLE);
2798
2799                 if (FIELD_GET(HE_MODE_MU_TX_BFER, hemode))
2800                         hemode |= FIELD_PREP(HE_MODE_SU_TX_BFER, HE_SU_BFER_ENABLE);
2801         }
2802
2803         ret = ath11k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id, param, hemode);
2804         if (ret) {
2805                 ath11k_warn(ar->ab, "failed to submit vdev param txbf 0x%x: %d\n",
2806                             hemode, ret);
2807                 return false;
2808         }
2809
2810         return true;
2811 }
2812
2813 static void ath11k_bss_assoc(struct ieee80211_hw *hw,
2814                              struct ieee80211_vif *vif,
2815                              struct ieee80211_bss_conf *bss_conf)
2816 {
2817         struct ath11k *ar = hw->priv;
2818         struct ath11k_vif *arvif = (void *)vif->drv_priv;
2819         struct peer_assoc_params peer_arg;
2820         struct ieee80211_sta *ap_sta;
2821         struct ath11k_peer *peer;
2822         bool is_auth = false;
2823         struct ieee80211_sta_he_cap  he_cap;
2824         int ret;
2825
2826         lockdep_assert_held(&ar->conf_mutex);
2827
2828         ath11k_dbg(ar->ab, ATH11K_DBG_MAC, "mac vdev %i assoc bssid %pM aid %d\n",
2829                    arvif->vdev_id, arvif->bssid, arvif->aid);
2830
2831         rcu_read_lock();
2832
2833         ap_sta = ieee80211_find_sta(vif, bss_conf->bssid);
2834         if (!ap_sta) {
2835                 ath11k_warn(ar->ab, "failed to find station entry for bss %pM vdev %i\n",
2836                             bss_conf->bssid, arvif->vdev_id);
2837                 rcu_read_unlock();
2838                 return;
2839         }
2840
2841         /* he_cap here is updated at assoc success for sta mode only */
2842         he_cap  = ap_sta->deflink.he_cap;
2843
2844         ath11k_peer_assoc_prepare(ar, vif, ap_sta, &peer_arg, false);
2845
2846         rcu_read_unlock();
2847
2848         peer_arg.is_assoc = true;
2849         ret = ath11k_wmi_send_peer_assoc_cmd(ar, &peer_arg);
2850         if (ret) {
2851                 ath11k_warn(ar->ab, "failed to run peer assoc for %pM vdev %i: %d\n",
2852                             bss_conf->bssid, arvif->vdev_id, ret);
2853                 return;
2854         }
2855
2856         if (!wait_for_completion_timeout(&ar->peer_assoc_done, 1 * HZ)) {
2857                 ath11k_warn(ar->ab, "failed to get peer assoc conf event for %pM vdev %i\n",
2858                             bss_conf->bssid, arvif->vdev_id);
2859                 return;
2860         }
2861
2862         ret = ath11k_setup_peer_smps(ar, arvif, bss_conf->bssid,
2863                                      &ap_sta->deflink.ht_cap,
2864                                      le16_to_cpu(ap_sta->deflink.he_6ghz_capa.capa));
2865         if (ret) {
2866                 ath11k_warn(ar->ab, "failed to setup peer SMPS for vdev %d: %d\n",
2867                             arvif->vdev_id, ret);
2868                 return;
2869         }
2870
2871         if (!ath11k_mac_vif_recalc_sta_he_txbf(ar, vif, &he_cap)) {
2872                 ath11k_warn(ar->ab, "failed to recalc he txbf for vdev %i on bss %pM\n",
2873                             arvif->vdev_id, bss_conf->bssid);
2874                 return;
2875         }
2876
2877         WARN_ON(arvif->is_up);
2878
2879         arvif->aid = vif->cfg.aid;
2880         ether_addr_copy(arvif->bssid, bss_conf->bssid);
2881
2882         ret = ath11k_wmi_vdev_up(ar, arvif->vdev_id, arvif->aid, arvif->bssid);
2883         if (ret) {
2884                 ath11k_warn(ar->ab, "failed to set vdev %d up: %d\n",
2885                             arvif->vdev_id, ret);
2886                 return;
2887         }
2888
2889         arvif->is_up = true;
2890         arvif->rekey_data.enable_offload = false;
2891
2892         ath11k_dbg(ar->ab, ATH11K_DBG_MAC,
2893                    "mac vdev %d up (associated) bssid %pM aid %d\n",
2894                    arvif->vdev_id, bss_conf->bssid, vif->cfg.aid);
2895
2896         spin_lock_bh(&ar->ab->base_lock);
2897
2898         peer = ath11k_peer_find(ar->ab, arvif->vdev_id, arvif->bssid);
2899         if (peer && peer->is_authorized)
2900                 is_auth = true;
2901
2902         spin_unlock_bh(&ar->ab->base_lock);
2903
2904         if (is_auth) {
2905                 ret = ath11k_wmi_set_peer_param(ar, arvif->bssid,
2906                                                 arvif->vdev_id,
2907                                                 WMI_PEER_AUTHORIZE,
2908                                                 1);
2909                 if (ret)
2910                         ath11k_warn(ar->ab, "Unable to authorize BSS peer: %d\n", ret);
2911         }
2912
2913         ret = ath11k_wmi_send_obss_spr_cmd(ar, arvif->vdev_id,
2914                                            &bss_conf->he_obss_pd);
2915         if (ret)
2916                 ath11k_warn(ar->ab, "failed to set vdev %i OBSS PD parameters: %d\n",
2917                             arvif->vdev_id, ret);
2918
2919         ret = ath11k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
2920                                             WMI_VDEV_PARAM_DTIM_POLICY,
2921                                             WMI_DTIM_POLICY_STICK);
2922         if (ret)
2923                 ath11k_warn(ar->ab, "failed to set vdev %d dtim policy: %d\n",
2924                             arvif->vdev_id, ret);
2925
2926         ath11k_mac_11d_scan_stop_all(ar->ab);
2927 }
2928
2929 static void ath11k_bss_disassoc(struct ieee80211_hw *hw,
2930                                 struct ieee80211_vif *vif)
2931 {
2932         struct ath11k *ar = hw->priv;
2933         struct ath11k_vif *arvif = (void *)vif->drv_priv;
2934         int ret;
2935
2936         lockdep_assert_held(&ar->conf_mutex);
2937
2938         ath11k_dbg(ar->ab, ATH11K_DBG_MAC, "mac vdev %i disassoc bssid %pM\n",
2939                    arvif->vdev_id, arvif->bssid);
2940
2941         ret = ath11k_wmi_vdev_down(ar, arvif->vdev_id);
2942         if (ret)
2943                 ath11k_warn(ar->ab, "failed to down vdev %i: %d\n",
2944                             arvif->vdev_id, ret);
2945
2946         arvif->is_up = false;
2947
2948         memset(&arvif->rekey_data, 0, sizeof(arvif->rekey_data));
2949
2950         cancel_delayed_work_sync(&arvif->connection_loss_work);
2951 }
2952
2953 static u32 ath11k_mac_get_rate_hw_value(int bitrate)
2954 {
2955         u32 preamble;
2956         u16 hw_value;
2957         int rate;
2958         size_t i;
2959
2960         if (ath11k_mac_bitrate_is_cck(bitrate))
2961                 preamble = WMI_RATE_PREAMBLE_CCK;
2962         else
2963                 preamble = WMI_RATE_PREAMBLE_OFDM;
2964
2965         for (i = 0; i < ARRAY_SIZE(ath11k_legacy_rates); i++) {
2966                 if (ath11k_legacy_rates[i].bitrate != bitrate)
2967                         continue;
2968
2969                 hw_value = ath11k_legacy_rates[i].hw_value;
2970                 rate = ATH11K_HW_RATE_CODE(hw_value, 0, preamble);
2971
2972                 return rate;
2973         }
2974
2975         return -EINVAL;
2976 }
2977
2978 static void ath11k_recalculate_mgmt_rate(struct ath11k *ar,
2979                                          struct ieee80211_vif *vif,
2980                                          struct cfg80211_chan_def *def)
2981 {
2982         struct ath11k_vif *arvif = (void *)vif->drv_priv;
2983         const struct ieee80211_supported_band *sband;
2984         u8 basic_rate_idx;
2985         int hw_rate_code;
2986         u32 vdev_param;
2987         u16 bitrate;
2988         int ret;
2989
2990         lockdep_assert_held(&ar->conf_mutex);
2991
2992         sband = ar->hw->wiphy->bands[def->chan->band];
2993         basic_rate_idx = ffs(vif->bss_conf.basic_rates) - 1;
2994         bitrate = sband->bitrates[basic_rate_idx].bitrate;
2995
2996         hw_rate_code = ath11k_mac_get_rate_hw_value(bitrate);
2997         if (hw_rate_code < 0) {
2998                 ath11k_warn(ar->ab, "bitrate not supported %d\n", bitrate);
2999                 return;
3000         }
3001
3002         vdev_param = WMI_VDEV_PARAM_MGMT_RATE;
3003         ret = ath11k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id, vdev_param,
3004                                             hw_rate_code);
3005         if (ret)
3006                 ath11k_warn(ar->ab, "failed to set mgmt tx rate %d\n", ret);
3007
3008         /* For WCN6855, firmware will clear this param when vdev starts, hence
3009          * cache it here so that we can reconfigure it once vdev starts.
3010          */
3011         ar->hw_rate_code = hw_rate_code;
3012
3013         vdev_param = WMI_VDEV_PARAM_BEACON_RATE;
3014         ret = ath11k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id, vdev_param,
3015                                             hw_rate_code);
3016         if (ret)
3017                 ath11k_warn(ar->ab, "failed to set beacon tx rate %d\n", ret);
3018 }
3019
3020 static int ath11k_mac_fils_discovery(struct ath11k_vif *arvif,
3021                                      struct ieee80211_bss_conf *info)
3022 {
3023         struct ath11k *ar = arvif->ar;
3024         struct sk_buff *tmpl;
3025         int ret;
3026         u32 interval;
3027         bool unsol_bcast_probe_resp_enabled = false;
3028
3029         if (info->fils_discovery.max_interval) {
3030                 interval = info->fils_discovery.max_interval;
3031
3032                 tmpl = ieee80211_get_fils_discovery_tmpl(ar->hw, arvif->vif);
3033                 if (tmpl)
3034                         ret = ath11k_wmi_fils_discovery_tmpl(ar, arvif->vdev_id,
3035                                                              tmpl);
3036         } else if (info->unsol_bcast_probe_resp_interval) {
3037                 unsol_bcast_probe_resp_enabled = 1;
3038                 interval = info->unsol_bcast_probe_resp_interval;
3039
3040                 tmpl = ieee80211_get_unsol_bcast_probe_resp_tmpl(ar->hw,
3041                                                                  arvif->vif);
3042                 if (tmpl)
3043                         ret = ath11k_wmi_probe_resp_tmpl(ar, arvif->vdev_id,
3044                                                          tmpl);
3045         } else { /* Disable */
3046                 return ath11k_wmi_fils_discovery(ar, arvif->vdev_id, 0, false);
3047         }
3048
3049         if (!tmpl) {
3050                 ath11k_warn(ar->ab,
3051                             "mac vdev %i failed to retrieve %s template\n",
3052                             arvif->vdev_id, (unsol_bcast_probe_resp_enabled ?
3053                             "unsolicited broadcast probe response" :
3054                             "FILS discovery"));
3055                 return -EPERM;
3056         }
3057         kfree_skb(tmpl);
3058
3059         if (!ret)
3060                 ret = ath11k_wmi_fils_discovery(ar, arvif->vdev_id, interval,
3061                                                 unsol_bcast_probe_resp_enabled);
3062
3063         return ret;
3064 }
3065
3066 static int ath11k_mac_config_obss_pd(struct ath11k *ar,
3067                                      struct ieee80211_he_obss_pd *he_obss_pd)
3068 {
3069         u32 bitmap[2], param_id, param_val, pdev_id;
3070         int ret;
3071         s8 non_srg_th = 0, srg_th = 0;
3072
3073         pdev_id = ar->pdev->pdev_id;
3074
3075         /* Set and enable SRG/non-SRG OBSS PD Threshold */
3076         param_id = WMI_PDEV_PARAM_SET_CMD_OBSS_PD_THRESHOLD;
3077         if (test_bit(ATH11K_FLAG_MONITOR_STARTED, &ar->monitor_flags)) {
3078                 ret = ath11k_wmi_pdev_set_param(ar, param_id, 0, pdev_id);
3079                 if (ret)
3080                         ath11k_warn(ar->ab,
3081                                     "failed to set obss_pd_threshold for pdev: %u\n",
3082                                     pdev_id);
3083                 return ret;
3084         }
3085
3086         ath11k_dbg(ar->ab, ATH11K_DBG_MAC,
3087                    "mac obss pd sr_ctrl %x non_srg_thres %u srg_max %u\n",
3088                    he_obss_pd->sr_ctrl, he_obss_pd->non_srg_max_offset,
3089                    he_obss_pd->max_offset);
3090
3091         param_val = 0;
3092
3093         if (he_obss_pd->sr_ctrl &
3094             IEEE80211_HE_SPR_NON_SRG_OBSS_PD_SR_DISALLOWED) {
3095                 non_srg_th = ATH11K_OBSS_PD_MAX_THRESHOLD;
3096         } else {
3097                 if (he_obss_pd->sr_ctrl & IEEE80211_HE_SPR_NON_SRG_OFFSET_PRESENT)
3098                         non_srg_th = (ATH11K_OBSS_PD_MAX_THRESHOLD +
3099                                       he_obss_pd->non_srg_max_offset);
3100                 else
3101                         non_srg_th = ATH11K_OBSS_PD_NON_SRG_MAX_THRESHOLD;
3102
3103                 param_val |= ATH11K_OBSS_PD_NON_SRG_EN;
3104         }
3105
3106         if (he_obss_pd->sr_ctrl & IEEE80211_HE_SPR_SRG_INFORMATION_PRESENT) {
3107                 srg_th = ATH11K_OBSS_PD_MAX_THRESHOLD + he_obss_pd->max_offset;
3108                 param_val |= ATH11K_OBSS_PD_SRG_EN;
3109         }
3110
3111         if (test_bit(WMI_TLV_SERVICE_SRG_SRP_SPATIAL_REUSE_SUPPORT,
3112                      ar->ab->wmi_ab.svc_map)) {
3113                 param_val |= ATH11K_OBSS_PD_THRESHOLD_IN_DBM;
3114                 param_val |= FIELD_PREP(GENMASK(15, 8), srg_th);
3115         } else {
3116                 non_srg_th -= ATH11K_DEFAULT_NOISE_FLOOR;
3117                 /* SRG not supported and threshold in dB */
3118                 param_val &= ~(ATH11K_OBSS_PD_SRG_EN |
3119                                ATH11K_OBSS_PD_THRESHOLD_IN_DBM);
3120         }
3121
3122         param_val |= (non_srg_th & GENMASK(7, 0));
3123         ret = ath11k_wmi_pdev_set_param(ar, param_id, param_val, pdev_id);
3124         if (ret) {
3125                 ath11k_warn(ar->ab,
3126                             "failed to set obss_pd_threshold for pdev: %u\n",
3127                             pdev_id);
3128                 return ret;
3129         }
3130
3131         /* Enable OBSS PD for all access category */
3132         param_id  = WMI_PDEV_PARAM_SET_CMD_OBSS_PD_PER_AC;
3133         param_val = 0xf;
3134         ret = ath11k_wmi_pdev_set_param(ar, param_id, param_val, pdev_id);
3135         if (ret) {
3136                 ath11k_warn(ar->ab,
3137                             "failed to set obss_pd_per_ac for pdev: %u\n",
3138                             pdev_id);
3139                 return ret;
3140         }
3141
3142         /* Set SR Prohibit */
3143         param_id  = WMI_PDEV_PARAM_ENABLE_SR_PROHIBIT;
3144         param_val = !!(he_obss_pd->sr_ctrl &
3145                        IEEE80211_HE_SPR_HESIGA_SR_VAL15_ALLOWED);
3146         ret = ath11k_wmi_pdev_set_param(ar, param_id, param_val, pdev_id);
3147         if (ret) {
3148                 ath11k_warn(ar->ab, "failed to set sr_prohibit for pdev: %u\n",
3149                             pdev_id);
3150                 return ret;
3151         }
3152
3153         if (!test_bit(WMI_TLV_SERVICE_SRG_SRP_SPATIAL_REUSE_SUPPORT,
3154                       ar->ab->wmi_ab.svc_map))
3155                 return 0;
3156
3157         /* Set SRG BSS Color Bitmap */
3158         memcpy(bitmap, he_obss_pd->bss_color_bitmap, sizeof(bitmap));
3159         ret = ath11k_wmi_pdev_set_srg_bss_color_bitmap(ar, bitmap);
3160         if (ret) {
3161                 ath11k_warn(ar->ab,
3162                             "failed to set bss_color_bitmap for pdev: %u\n",
3163                             pdev_id);
3164                 return ret;
3165         }
3166
3167         /* Set SRG Partial BSSID Bitmap */
3168         memcpy(bitmap, he_obss_pd->partial_bssid_bitmap, sizeof(bitmap));
3169         ret = ath11k_wmi_pdev_set_srg_patial_bssid_bitmap(ar, bitmap);
3170         if (ret) {
3171                 ath11k_warn(ar->ab,
3172                             "failed to set partial_bssid_bitmap for pdev: %u\n",
3173                             pdev_id);
3174                 return ret;
3175         }
3176
3177         memset(bitmap, 0xff, sizeof(bitmap));
3178
3179         /* Enable all BSS Colors for SRG */
3180         ret = ath11k_wmi_pdev_srg_obss_color_enable_bitmap(ar, bitmap);
3181         if (ret) {
3182                 ath11k_warn(ar->ab,
3183                             "failed to set srg_color_en_bitmap pdev: %u\n",
3184                             pdev_id);
3185                 return ret;
3186         }
3187
3188         /* Enable all partial BSSID mask for SRG */
3189         ret = ath11k_wmi_pdev_srg_obss_bssid_enable_bitmap(ar, bitmap);
3190         if (ret) {
3191                 ath11k_warn(ar->ab,
3192                             "failed to set srg_bssid_en_bitmap pdev: %u\n",
3193                             pdev_id);
3194                 return ret;
3195         }
3196
3197         /* Enable all BSS Colors for non-SRG */
3198         ret = ath11k_wmi_pdev_non_srg_obss_color_enable_bitmap(ar, bitmap);
3199         if (ret) {
3200                 ath11k_warn(ar->ab,
3201                             "failed to set non_srg_color_en_bitmap pdev: %u\n",
3202                             pdev_id);
3203                 return ret;
3204         }
3205
3206         /* Enable all partial BSSID mask for non-SRG */
3207         ret = ath11k_wmi_pdev_non_srg_obss_bssid_enable_bitmap(ar, bitmap);
3208         if (ret) {
3209                 ath11k_warn(ar->ab,
3210                             "failed to set non_srg_bssid_en_bitmap pdev: %u\n",
3211                             pdev_id);
3212                 return ret;
3213         }
3214
3215         return 0;
3216 }
3217
3218 static void ath11k_mac_op_bss_info_changed(struct ieee80211_hw *hw,
3219                                            struct ieee80211_vif *vif,
3220                                            struct ieee80211_bss_conf *info,
3221                                            u64 changed)
3222 {
3223         struct ath11k *ar = hw->priv;
3224         struct ath11k_vif *arvif = ath11k_vif_to_arvif(vif);
3225         struct cfg80211_chan_def def;
3226         u32 param_id, param_value;
3227         enum nl80211_band band;
3228         u32 vdev_param;
3229         int mcast_rate;
3230         u32 preamble;
3231         u16 hw_value;
3232         u16 bitrate;
3233         int ret = 0;
3234         u8 rateidx;
3235         u32 rate, param;
3236         u32 ipv4_cnt;
3237
3238         mutex_lock(&ar->conf_mutex);
3239
3240         if (changed & BSS_CHANGED_BEACON_INT) {
3241                 arvif->beacon_interval = info->beacon_int;
3242
3243                 param_id = WMI_VDEV_PARAM_BEACON_INTERVAL;
3244                 ret = ath11k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
3245                                                     param_id,
3246                                                     arvif->beacon_interval);
3247                 if (ret)
3248                         ath11k_warn(ar->ab, "Failed to set beacon interval for VDEV: %d\n",
3249                                     arvif->vdev_id);
3250                 else
3251                         ath11k_dbg(ar->ab, ATH11K_DBG_MAC,
3252                                    "Beacon interval: %d set for VDEV: %d\n",
3253                                    arvif->beacon_interval, arvif->vdev_id);
3254         }
3255
3256         if (changed & BSS_CHANGED_BEACON) {
3257                 param_id = WMI_PDEV_PARAM_BEACON_TX_MODE;
3258                 param_value = WMI_BEACON_STAGGERED_MODE;
3259                 ret = ath11k_wmi_pdev_set_param(ar, param_id,
3260                                                 param_value, ar->pdev->pdev_id);
3261                 if (ret)
3262                         ath11k_warn(ar->ab, "Failed to set beacon mode for VDEV: %d\n",
3263                                     arvif->vdev_id);
3264                 else
3265                         ath11k_dbg(ar->ab, ATH11K_DBG_MAC,
3266                                    "Set staggered beacon mode for VDEV: %d\n",
3267                                    arvif->vdev_id);
3268
3269                 if (!arvif->do_not_send_tmpl || !arvif->bcca_zero_sent) {
3270                         ret = ath11k_mac_setup_bcn_tmpl(arvif);
3271                         if (ret)
3272                                 ath11k_warn(ar->ab, "failed to update bcn template: %d\n",
3273                                             ret);
3274                 }
3275
3276                 if (arvif->bcca_zero_sent)
3277                         arvif->do_not_send_tmpl = true;
3278                 else
3279                         arvif->do_not_send_tmpl = false;
3280
3281                 if (vif->bss_conf.he_support) {
3282                         ret = ath11k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
3283                                                             WMI_VDEV_PARAM_BA_MODE,
3284                                                             WMI_BA_MODE_BUFFER_SIZE_256);
3285                         if (ret)
3286                                 ath11k_warn(ar->ab,
3287                                             "failed to set BA BUFFER SIZE 256 for vdev: %d\n",
3288                                             arvif->vdev_id);
3289                         else
3290                                 ath11k_dbg(ar->ab, ATH11K_DBG_MAC,
3291                                            "Set BA BUFFER SIZE 256 for VDEV: %d\n",
3292                                            arvif->vdev_id);
3293                 }
3294         }
3295
3296         if (changed & (BSS_CHANGED_BEACON_INFO | BSS_CHANGED_BEACON)) {
3297                 arvif->dtim_period = info->dtim_period;
3298
3299                 param_id = WMI_VDEV_PARAM_DTIM_PERIOD;
3300                 ret = ath11k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
3301                                                     param_id,
3302                                                     arvif->dtim_period);
3303
3304                 if (ret)
3305                         ath11k_warn(ar->ab, "Failed to set dtim period for VDEV %d: %i\n",
3306                                     arvif->vdev_id, ret);
3307                 else
3308                         ath11k_dbg(ar->ab, ATH11K_DBG_MAC,
3309                                    "DTIM period: %d set for VDEV: %d\n",
3310                                    arvif->dtim_period, arvif->vdev_id);
3311         }
3312
3313         if (changed & BSS_CHANGED_SSID &&
3314             vif->type == NL80211_IFTYPE_AP) {
3315                 arvif->u.ap.ssid_len = vif->cfg.ssid_len;
3316                 if (vif->cfg.ssid_len)
3317                         memcpy(arvif->u.ap.ssid, vif->cfg.ssid,
3318                                vif->cfg.ssid_len);
3319                 arvif->u.ap.hidden_ssid = info->hidden_ssid;
3320         }
3321
3322         if (changed & BSS_CHANGED_BSSID && !is_zero_ether_addr(info->bssid))
3323                 ether_addr_copy(arvif->bssid, info->bssid);
3324
3325         if (changed & BSS_CHANGED_BEACON_ENABLED) {
3326                 if (info->enable_beacon)
3327                         ath11k_mac_set_he_txbf_conf(arvif);
3328                 ath11k_control_beaconing(arvif, info);
3329
3330                 if (arvif->is_up && vif->bss_conf.he_support &&
3331                     vif->bss_conf.he_oper.params) {
3332                         param_id = WMI_VDEV_PARAM_HEOPS_0_31;
3333                         param_value = vif->bss_conf.he_oper.params;
3334                         ret = ath11k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
3335                                                             param_id, param_value);
3336                         ath11k_dbg(ar->ab, ATH11K_DBG_MAC,
3337                                    "he oper param: %x set for VDEV: %d\n",
3338                                    param_value, arvif->vdev_id);
3339
3340                         if (ret)
3341                                 ath11k_warn(ar->ab, "Failed to set he oper params %x for VDEV %d: %i\n",
3342                                             param_value, arvif->vdev_id, ret);
3343                 }
3344         }
3345
3346         if (changed & BSS_CHANGED_ERP_CTS_PROT) {
3347                 u32 cts_prot;
3348
3349                 cts_prot = !!(info->use_cts_prot);
3350                 param_id = WMI_VDEV_PARAM_PROTECTION_MODE;
3351
3352                 if (arvif->is_started) {
3353                         ret = ath11k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
3354                                                             param_id, cts_prot);
3355                         if (ret)
3356                                 ath11k_warn(ar->ab, "Failed to set CTS prot for VDEV: %d\n",
3357                                             arvif->vdev_id);
3358                         else
3359                                 ath11k_dbg(ar->ab, ATH11K_DBG_MAC, "Set CTS prot: %d for VDEV: %d\n",
3360                                            cts_prot, arvif->vdev_id);
3361                 } else {
3362                         ath11k_dbg(ar->ab, ATH11K_DBG_MAC, "defer protection mode setup, vdev is not ready yet\n");
3363                 }
3364         }
3365
3366         if (changed & BSS_CHANGED_ERP_SLOT) {
3367                 u32 slottime;
3368
3369                 if (info->use_short_slot)
3370                         slottime = WMI_VDEV_SLOT_TIME_SHORT; /* 9us */
3371
3372                 else
3373                         slottime = WMI_VDEV_SLOT_TIME_LONG; /* 20us */
3374
3375                 param_id = WMI_VDEV_PARAM_SLOT_TIME;
3376                 ret = ath11k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
3377                                                     param_id, slottime);
3378                 if (ret)
3379                         ath11k_warn(ar->ab, "Failed to set erp slot for VDEV: %d\n",
3380                                     arvif->vdev_id);
3381                 else
3382                         ath11k_dbg(ar->ab, ATH11K_DBG_MAC,
3383                                    "Set slottime: %d for VDEV: %d\n",
3384                                    slottime, arvif->vdev_id);
3385         }
3386
3387         if (changed & BSS_CHANGED_ERP_PREAMBLE) {
3388                 u32 preamble;
3389
3390                 if (info->use_short_preamble)
3391                         preamble = WMI_VDEV_PREAMBLE_SHORT;
3392                 else
3393                         preamble = WMI_VDEV_PREAMBLE_LONG;
3394
3395                 param_id = WMI_VDEV_PARAM_PREAMBLE;
3396                 ret = ath11k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
3397                                                     param_id, preamble);
3398                 if (ret)
3399                         ath11k_warn(ar->ab, "Failed to set preamble for VDEV: %d\n",
3400                                     arvif->vdev_id);
3401                 else
3402                         ath11k_dbg(ar->ab, ATH11K_DBG_MAC,
3403                                    "Set preamble: %d for VDEV: %d\n",
3404                                    preamble, arvif->vdev_id);
3405         }
3406
3407         if (changed & BSS_CHANGED_ASSOC) {
3408                 if (vif->cfg.assoc)
3409                         ath11k_bss_assoc(hw, vif, info);
3410                 else
3411                         ath11k_bss_disassoc(hw, vif);
3412         }
3413
3414         if (changed & BSS_CHANGED_TXPOWER) {
3415                 ath11k_dbg(ar->ab, ATH11K_DBG_MAC, "mac vdev_id %i txpower %d\n",
3416                            arvif->vdev_id, info->txpower);
3417
3418                 arvif->txpower = info->txpower;
3419                 ath11k_mac_txpower_recalc(ar);
3420         }
3421
3422         if (changed & BSS_CHANGED_PS &&
3423             ar->ab->hw_params.supports_sta_ps) {
3424                 arvif->ps = vif->cfg.ps;
3425
3426                 ret = ath11k_mac_config_ps(ar);
3427                 if (ret)
3428                         ath11k_warn(ar->ab, "failed to setup ps on vdev %i: %d\n",
3429                                     arvif->vdev_id, ret);
3430         }
3431
3432         if (changed & BSS_CHANGED_MCAST_RATE &&
3433             !ath11k_mac_vif_chan(arvif->vif, &def)) {
3434                 band = def.chan->band;
3435                 mcast_rate = vif->bss_conf.mcast_rate[band];
3436
3437                 if (mcast_rate > 0)
3438                         rateidx = mcast_rate - 1;
3439                 else
3440                         rateidx = ffs(vif->bss_conf.basic_rates) - 1;
3441
3442                 if (ar->pdev->cap.supported_bands & WMI_HOST_WLAN_5G_CAP)
3443                         rateidx += ATH11K_MAC_FIRST_OFDM_RATE_IDX;
3444
3445                 bitrate = ath11k_legacy_rates[rateidx].bitrate;
3446                 hw_value = ath11k_legacy_rates[rateidx].hw_value;
3447
3448                 if (ath11k_mac_bitrate_is_cck(bitrate))
3449                         preamble = WMI_RATE_PREAMBLE_CCK;
3450                 else
3451                         preamble = WMI_RATE_PREAMBLE_OFDM;
3452
3453                 rate = ATH11K_HW_RATE_CODE(hw_value, 0, preamble);
3454
3455                 ath11k_dbg(ar->ab, ATH11K_DBG_MAC,
3456                            "mac vdev %d mcast_rate %x\n",
3457                            arvif->vdev_id, rate);
3458
3459                 vdev_param = WMI_VDEV_PARAM_MCAST_DATA_RATE;
3460                 ret = ath11k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
3461                                                     vdev_param, rate);
3462                 if (ret)
3463                         ath11k_warn(ar->ab,
3464                                     "failed to set mcast rate on vdev %i: %d\n",
3465                                     arvif->vdev_id,  ret);
3466
3467                 vdev_param = WMI_VDEV_PARAM_BCAST_DATA_RATE;
3468                 ret = ath11k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
3469                                                     vdev_param, rate);
3470                 if (ret)
3471                         ath11k_warn(ar->ab,
3472                                     "failed to set bcast rate on vdev %i: %d\n",
3473                                     arvif->vdev_id,  ret);
3474         }
3475
3476         if (changed & BSS_CHANGED_BASIC_RATES &&
3477             !ath11k_mac_vif_chan(arvif->vif, &def))
3478                 ath11k_recalculate_mgmt_rate(ar, vif, &def);
3479
3480         if (changed & BSS_CHANGED_TWT) {
3481                 struct wmi_twt_enable_params twt_params = {0};
3482
3483                 if (info->twt_requester || info->twt_responder) {
3484                         ath11k_wmi_fill_default_twt_params(&twt_params);
3485                         ath11k_wmi_send_twt_enable_cmd(ar, ar->pdev->pdev_id,
3486                                                        &twt_params);
3487                 } else {
3488                         ath11k_wmi_send_twt_disable_cmd(ar, ar->pdev->pdev_id);
3489                 }
3490         }
3491
3492         if (changed & BSS_CHANGED_HE_OBSS_PD)
3493                 ath11k_mac_config_obss_pd(ar, &info->he_obss_pd);
3494
3495         if (changed & BSS_CHANGED_HE_BSS_COLOR) {
3496                 if (vif->type == NL80211_IFTYPE_AP) {
3497                         ret = ath11k_wmi_send_obss_color_collision_cfg_cmd(
3498                                 ar, arvif->vdev_id, info->he_bss_color.color,
3499                                 ATH11K_BSS_COLOR_COLLISION_DETECTION_AP_PERIOD_MS,
3500                                 info->he_bss_color.enabled);
3501                         if (ret)
3502                                 ath11k_warn(ar->ab, "failed to set bss color collision on vdev %i: %d\n",
3503                                             arvif->vdev_id,  ret);
3504
3505                         param_id = WMI_VDEV_PARAM_BSS_COLOR;
3506                         if (info->he_bss_color.enabled)
3507                                 param_value = info->he_bss_color.color <<
3508                                                 IEEE80211_HE_OPERATION_BSS_COLOR_OFFSET;
3509                         else
3510                                 param_value = IEEE80211_HE_OPERATION_BSS_COLOR_DISABLED;
3511
3512                         ret = ath11k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
3513                                                             param_id,
3514                                                             param_value);
3515                         if (ret)
3516                                 ath11k_warn(ar->ab,
3517                                             "failed to set bss color param on vdev %i: %d\n",
3518                                             arvif->vdev_id,  ret);
3519
3520                         ath11k_dbg(ar->ab, ATH11K_DBG_MAC,
3521                                    "bss color param 0x%x set on vdev %i\n",
3522                                    param_value, arvif->vdev_id);
3523                 } else if (vif->type == NL80211_IFTYPE_STATION) {
3524                         ret = ath11k_wmi_send_bss_color_change_enable_cmd(ar,
3525                                                                           arvif->vdev_id,
3526                                                                           1);
3527                         if (ret)
3528                                 ath11k_warn(ar->ab, "failed to enable bss color change on vdev %i: %d\n",
3529                                             arvif->vdev_id,  ret);
3530                         ret = ath11k_wmi_send_obss_color_collision_cfg_cmd(
3531                                 ar, arvif->vdev_id, 0,
3532                                 ATH11K_BSS_COLOR_COLLISION_DETECTION_STA_PERIOD_MS, 1);
3533                         if (ret)
3534                                 ath11k_warn(ar->ab, "failed to set bss color collision on vdev %i: %d\n",
3535                                             arvif->vdev_id,  ret);
3536                 }
3537         }
3538
3539         if (changed & BSS_CHANGED_FTM_RESPONDER &&
3540             arvif->ftm_responder != info->ftm_responder &&
3541             test_bit(WMI_TLV_SERVICE_RTT, ar->ab->wmi_ab.svc_map) &&
3542             (vif->type == NL80211_IFTYPE_AP ||
3543              vif->type == NL80211_IFTYPE_MESH_POINT)) {
3544                 arvif->ftm_responder = info->ftm_responder;
3545                 param = WMI_VDEV_PARAM_ENABLE_DISABLE_RTT_RESPONDER_ROLE;
3546                 ret = ath11k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id, param,
3547                                                     arvif->ftm_responder);
3548                 if (ret)
3549                         ath11k_warn(ar->ab, "Failed to set ftm responder %i: %d\n",
3550                                     arvif->vdev_id, ret);
3551         }
3552
3553         if (changed & BSS_CHANGED_FILS_DISCOVERY ||
3554             changed & BSS_CHANGED_UNSOL_BCAST_PROBE_RESP)
3555                 ath11k_mac_fils_discovery(arvif, info);
3556
3557         if (changed & BSS_CHANGED_ARP_FILTER) {
3558                 ipv4_cnt = min(vif->cfg.arp_addr_cnt, ATH11K_IPV4_MAX_COUNT);
3559                 memcpy(arvif->arp_ns_offload.ipv4_addr,
3560                        vif->cfg.arp_addr_list,
3561                        ipv4_cnt * sizeof(u32));
3562                 memcpy(arvif->arp_ns_offload.mac_addr, vif->addr, ETH_ALEN);
3563                 arvif->arp_ns_offload.ipv4_count = ipv4_cnt;
3564
3565                 ath11k_dbg(ar->ab, ATH11K_DBG_MAC, "mac arp_addr_cnt %d vif->addr %pM, offload_addr %pI4\n",
3566                            vif->cfg.arp_addr_cnt,
3567                            vif->addr, arvif->arp_ns_offload.ipv4_addr);
3568         }
3569
3570         mutex_unlock(&ar->conf_mutex);
3571 }
3572
3573 void __ath11k_mac_scan_finish(struct ath11k *ar)
3574 {
3575         lockdep_assert_held(&ar->data_lock);
3576
3577         switch (ar->scan.state) {
3578         case ATH11K_SCAN_IDLE:
3579                 break;
3580         case ATH11K_SCAN_RUNNING:
3581         case ATH11K_SCAN_ABORTING:
3582                 if (ar->scan.is_roc && ar->scan.roc_notify)
3583                         ieee80211_remain_on_channel_expired(ar->hw);
3584                 fallthrough;
3585         case ATH11K_SCAN_STARTING:
3586                 if (!ar->scan.is_roc) {
3587                         struct cfg80211_scan_info info = {
3588                                 .aborted = ((ar->scan.state ==
3589                                             ATH11K_SCAN_ABORTING) ||
3590                                             (ar->scan.state ==
3591                                             ATH11K_SCAN_STARTING)),
3592                         };
3593
3594                         ieee80211_scan_completed(ar->hw, &info);
3595                 }
3596
3597                 ar->scan.state = ATH11K_SCAN_IDLE;
3598                 ar->scan_channel = NULL;
3599                 ar->scan.roc_freq = 0;
3600                 cancel_delayed_work(&ar->scan.timeout);
3601                 complete_all(&ar->scan.completed);
3602                 break;
3603         }
3604 }
3605
3606 void ath11k_mac_scan_finish(struct ath11k *ar)
3607 {
3608         spin_lock_bh(&ar->data_lock);
3609         __ath11k_mac_scan_finish(ar);
3610         spin_unlock_bh(&ar->data_lock);
3611 }
3612
3613 static int ath11k_scan_stop(struct ath11k *ar)
3614 {
3615         struct scan_cancel_param arg = {
3616                 .req_type = WLAN_SCAN_CANCEL_SINGLE,
3617                 .scan_id = ATH11K_SCAN_ID,
3618         };
3619         int ret;
3620
3621         lockdep_assert_held(&ar->conf_mutex);
3622
3623         /* TODO: Fill other STOP Params */
3624         arg.pdev_id = ar->pdev->pdev_id;
3625
3626         ret = ath11k_wmi_send_scan_stop_cmd(ar, &arg);
3627         if (ret) {
3628                 ath11k_warn(ar->ab, "failed to stop wmi scan: %d\n", ret);
3629                 goto out;
3630         }
3631
3632         ret = wait_for_completion_timeout(&ar->scan.completed, 3 * HZ);
3633         if (ret == 0) {
3634                 ath11k_warn(ar->ab,
3635                             "failed to receive scan abort comple: timed out\n");
3636                 ret = -ETIMEDOUT;
3637         } else if (ret > 0) {
3638                 ret = 0;
3639         }
3640
3641 out:
3642         /* Scan state should be updated upon scan completion but in case
3643          * firmware fails to deliver the event (for whatever reason) it is
3644          * desired to clean up scan state anyway. Firmware may have just
3645          * dropped the scan completion event delivery due to transport pipe
3646          * being overflown with data and/or it can recover on its own before
3647          * next scan request is submitted.
3648          */
3649         spin_lock_bh(&ar->data_lock);
3650         if (ar->scan.state != ATH11K_SCAN_IDLE)
3651                 __ath11k_mac_scan_finish(ar);
3652         spin_unlock_bh(&ar->data_lock);
3653
3654         return ret;
3655 }
3656
3657 static void ath11k_scan_abort(struct ath11k *ar)
3658 {
3659         int ret;
3660
3661         lockdep_assert_held(&ar->conf_mutex);
3662
3663         spin_lock_bh(&ar->data_lock);
3664
3665         switch (ar->scan.state) {
3666         case ATH11K_SCAN_IDLE:
3667                 /* This can happen if timeout worker kicked in and called
3668                  * abortion while scan completion was being processed.
3669                  */
3670                 break;
3671         case ATH11K_SCAN_STARTING:
3672         case ATH11K_SCAN_ABORTING:
3673                 ath11k_warn(ar->ab, "refusing scan abortion due to invalid scan state: %d\n",
3674                             ar->scan.state);
3675                 break;
3676         case ATH11K_SCAN_RUNNING:
3677                 ar->scan.state = ATH11K_SCAN_ABORTING;
3678                 spin_unlock_bh(&ar->data_lock);
3679
3680                 ret = ath11k_scan_stop(ar);
3681                 if (ret)
3682                         ath11k_warn(ar->ab, "failed to abort scan: %d\n", ret);
3683
3684                 spin_lock_bh(&ar->data_lock);
3685                 break;
3686         }
3687
3688         spin_unlock_bh(&ar->data_lock);
3689 }
3690
3691 static void ath11k_scan_timeout_work(struct work_struct *work)
3692 {
3693         struct ath11k *ar = container_of(work, struct ath11k,
3694                                          scan.timeout.work);
3695
3696         mutex_lock(&ar->conf_mutex);
3697         ath11k_scan_abort(ar);
3698         mutex_unlock(&ar->conf_mutex);
3699 }
3700
3701 static int ath11k_start_scan(struct ath11k *ar,
3702                              struct scan_req_params *arg)
3703 {
3704         int ret;
3705         unsigned long timeout = 1 * HZ;
3706
3707         lockdep_assert_held(&ar->conf_mutex);
3708
3709         if (ath11k_spectral_get_mode(ar) == ATH11K_SPECTRAL_BACKGROUND)
3710                 ath11k_spectral_reset_buffer(ar);
3711
3712         ret = ath11k_wmi_send_scan_start_cmd(ar, arg);
3713         if (ret)
3714                 return ret;
3715
3716         if (test_bit(WMI_TLV_SERVICE_11D_OFFLOAD, ar->ab->wmi_ab.svc_map)) {
3717                 timeout = 5 * HZ;
3718
3719                 if (ar->supports_6ghz)
3720                         timeout += 5 * HZ;
3721         }
3722
3723         ret = wait_for_completion_timeout(&ar->scan.started, timeout);
3724         if (ret == 0) {
3725                 ret = ath11k_scan_stop(ar);
3726                 if (ret)
3727                         ath11k_warn(ar->ab, "failed to stop scan: %d\n", ret);
3728
3729                 return -ETIMEDOUT;
3730         }
3731
3732         /* If we failed to start the scan, return error code at
3733          * this point.  This is probably due to some issue in the
3734          * firmware, but no need to wedge the driver due to that...
3735          */
3736         spin_lock_bh(&ar->data_lock);
3737         if (ar->scan.state == ATH11K_SCAN_IDLE) {
3738                 spin_unlock_bh(&ar->data_lock);
3739                 return -EINVAL;
3740         }
3741         spin_unlock_bh(&ar->data_lock);
3742
3743         return 0;
3744 }
3745
3746 static int ath11k_mac_op_hw_scan(struct ieee80211_hw *hw,
3747                                  struct ieee80211_vif *vif,
3748                                  struct ieee80211_scan_request *hw_req)
3749 {
3750         struct ath11k *ar = hw->priv;
3751         struct ath11k_vif *arvif = ath11k_vif_to_arvif(vif);
3752         struct cfg80211_scan_request *req = &hw_req->req;
3753         struct scan_req_params *arg = NULL;
3754         int ret = 0;
3755         int i;
3756         u32 scan_timeout;
3757
3758         /* Firmwares advertising the support of triggering 11D algorithm
3759          * on the scan results of a regular scan expects driver to send
3760          * WMI_11D_SCAN_START_CMDID before sending WMI_START_SCAN_CMDID.
3761          * With this feature, separate 11D scan can be avoided since
3762          * regdomain can be determined with the scan results of the
3763          * regular scan.
3764          */
3765         if (ar->state_11d == ATH11K_11D_PREPARING &&
3766             test_bit(WMI_TLV_SERVICE_SUPPORT_11D_FOR_HOST_SCAN,
3767                      ar->ab->wmi_ab.svc_map))
3768                 ath11k_mac_11d_scan_start(ar, arvif->vdev_id);
3769
3770         mutex_lock(&ar->conf_mutex);
3771
3772         spin_lock_bh(&ar->data_lock);
3773         switch (ar->scan.state) {
3774         case ATH11K_SCAN_IDLE:
3775                 reinit_completion(&ar->scan.started);
3776                 reinit_completion(&ar->scan.completed);
3777                 ar->scan.state = ATH11K_SCAN_STARTING;
3778                 ar->scan.is_roc = false;
3779                 ar->scan.vdev_id = arvif->vdev_id;
3780                 ret = 0;
3781                 break;
3782         case ATH11K_SCAN_STARTING:
3783         case ATH11K_SCAN_RUNNING:
3784         case ATH11K_SCAN_ABORTING:
3785                 ret = -EBUSY;
3786                 break;
3787         }
3788         spin_unlock_bh(&ar->data_lock);
3789
3790         if (ret)
3791                 goto exit;
3792
3793         arg = kzalloc(sizeof(*arg), GFP_KERNEL);
3794
3795         if (!arg) {
3796                 ret = -ENOMEM;
3797                 goto exit;
3798         }
3799
3800         ath11k_wmi_start_scan_init(ar, arg);
3801         arg->vdev_id = arvif->vdev_id;
3802         arg->scan_id = ATH11K_SCAN_ID;
3803
3804         if (req->ie_len) {
3805                 arg->extraie.ptr = kmemdup(req->ie, req->ie_len, GFP_KERNEL);
3806                 if (!arg->extraie.ptr) {
3807                         ret = -ENOMEM;
3808                         goto exit;
3809                 }
3810                 arg->extraie.len = req->ie_len;
3811         }
3812
3813         if (req->n_ssids) {
3814                 arg->num_ssids = req->n_ssids;
3815                 for (i = 0; i < arg->num_ssids; i++) {
3816                         arg->ssid[i].length  = req->ssids[i].ssid_len;
3817                         memcpy(&arg->ssid[i].ssid, req->ssids[i].ssid,
3818                                req->ssids[i].ssid_len);
3819                 }
3820         } else {
3821                 arg->scan_flags |= WMI_SCAN_FLAG_PASSIVE;
3822         }
3823
3824         if (req->n_channels) {
3825                 arg->num_chan = req->n_channels;
3826                 arg->chan_list = kcalloc(arg->num_chan, sizeof(*arg->chan_list),
3827                                          GFP_KERNEL);
3828
3829                 if (!arg->chan_list) {
3830                         ret = -ENOMEM;
3831                         goto exit;
3832                 }
3833
3834                 for (i = 0; i < arg->num_chan; i++) {
3835                         if (test_bit(WMI_TLV_SERVICE_SCAN_CONFIG_PER_CHANNEL,
3836                                      ar->ab->wmi_ab.svc_map)) {
3837                                 arg->chan_list[i] =
3838                                         u32_encode_bits(req->channels[i]->center_freq,
3839                                                         WMI_SCAN_CONFIG_PER_CHANNEL_MASK);
3840
3841                                 /* If NL80211_SCAN_FLAG_COLOCATED_6GHZ is set in scan
3842                                  * flags, then scan all PSC channels in 6 GHz band and
3843                                  * those non-PSC channels where RNR IE is found during
3844                                  * the legacy 2.4/5 GHz scan.
3845                                  * If NL80211_SCAN_FLAG_COLOCATED_6GHZ is not set,
3846                                  * then all channels in 6 GHz will be scanned.
3847                                  */
3848                                 if (req->channels[i]->band == NL80211_BAND_6GHZ &&
3849                                     req->flags & NL80211_SCAN_FLAG_COLOCATED_6GHZ &&
3850                                     !cfg80211_channel_is_psc(req->channels[i]))
3851                                         arg->chan_list[i] |=
3852                                                 WMI_SCAN_CH_FLAG_SCAN_ONLY_IF_RNR_FOUND;
3853                         } else {
3854                                 arg->chan_list[i] = req->channels[i]->center_freq;
3855                         }
3856                 }
3857         }
3858
3859         if (req->flags & NL80211_SCAN_FLAG_RANDOM_ADDR) {
3860                 arg->scan_f_add_spoofed_mac_in_probe = 1;
3861                 ether_addr_copy(arg->mac_addr.addr, req->mac_addr);
3862                 ether_addr_copy(arg->mac_mask.addr, req->mac_addr_mask);
3863         }
3864
3865         /* if duration is set, default dwell times will be overwritten */
3866         if (req->duration) {
3867                 arg->dwell_time_active = req->duration;
3868                 arg->dwell_time_active_2g = req->duration;
3869                 arg->dwell_time_active_6g = req->duration;
3870                 arg->dwell_time_passive = req->duration;
3871                 arg->dwell_time_passive_6g = req->duration;
3872                 arg->burst_duration = req->duration;
3873
3874                 scan_timeout = min_t(u32, arg->max_rest_time *
3875                                 (arg->num_chan - 1) + (req->duration +
3876                                 ATH11K_SCAN_CHANNEL_SWITCH_WMI_EVT_OVERHEAD) *
3877                                 arg->num_chan, arg->max_scan_time);
3878         } else {
3879                 scan_timeout = arg->max_scan_time;
3880         }
3881
3882         /* Add a margin to account for event/command processing */
3883         scan_timeout += ATH11K_MAC_SCAN_CMD_EVT_OVERHEAD;
3884
3885         ret = ath11k_start_scan(ar, arg);
3886         if (ret) {
3887                 ath11k_warn(ar->ab, "failed to start hw scan: %d\n", ret);
3888                 spin_lock_bh(&ar->data_lock);
3889                 ar->scan.state = ATH11K_SCAN_IDLE;
3890                 spin_unlock_bh(&ar->data_lock);
3891         }
3892
3893         ieee80211_queue_delayed_work(ar->hw, &ar->scan.timeout,
3894                                      msecs_to_jiffies(scan_timeout));
3895
3896 exit:
3897         if (arg) {
3898                 kfree(arg->chan_list);
3899                 kfree(arg->extraie.ptr);
3900                 kfree(arg);
3901         }
3902
3903         mutex_unlock(&ar->conf_mutex);
3904
3905         if (ar->state_11d == ATH11K_11D_PREPARING)
3906                 ath11k_mac_11d_scan_start(ar, arvif->vdev_id);
3907
3908         return ret;
3909 }
3910
3911 static void ath11k_mac_op_cancel_hw_scan(struct ieee80211_hw *hw,
3912                                          struct ieee80211_vif *vif)
3913 {
3914         struct ath11k *ar = hw->priv;
3915
3916         mutex_lock(&ar->conf_mutex);
3917         ath11k_scan_abort(ar);
3918         mutex_unlock(&ar->conf_mutex);
3919
3920         cancel_delayed_work_sync(&ar->scan.timeout);
3921 }
3922
3923 static int ath11k_install_key(struct ath11k_vif *arvif,
3924                               struct ieee80211_key_conf *key,
3925                               enum set_key_cmd cmd,
3926                               const u8 *macaddr, u32 flags)
3927 {
3928         int ret;
3929         struct ath11k *ar = arvif->ar;
3930         struct wmi_vdev_install_key_arg arg = {
3931                 .vdev_id = arvif->vdev_id,
3932                 .key_idx = key->keyidx,
3933                 .key_len = key->keylen,
3934                 .key_data = key->key,
3935                 .key_flags = flags,
3936                 .macaddr = macaddr,
3937         };
3938
3939         lockdep_assert_held(&arvif->ar->conf_mutex);
3940
3941         reinit_completion(&ar->install_key_done);
3942
3943         if (test_bit(ATH11K_FLAG_HW_CRYPTO_DISABLED, &ar->ab->dev_flags))
3944                 return 0;
3945
3946         if (cmd == DISABLE_KEY) {
3947                 arg.key_cipher = WMI_CIPHER_NONE;
3948                 arg.key_data = NULL;
3949                 goto install;
3950         }
3951
3952         switch (key->cipher) {
3953         case WLAN_CIPHER_SUITE_CCMP:
3954                 arg.key_cipher = WMI_CIPHER_AES_CCM;
3955                 /* TODO: Re-check if flag is valid */
3956                 key->flags |= IEEE80211_KEY_FLAG_GENERATE_IV_MGMT;
3957                 break;
3958         case WLAN_CIPHER_SUITE_TKIP:
3959                 arg.key_cipher = WMI_CIPHER_TKIP;
3960                 arg.key_txmic_len = 8;
3961                 arg.key_rxmic_len = 8;
3962                 break;
3963         case WLAN_CIPHER_SUITE_CCMP_256:
3964                 arg.key_cipher = WMI_CIPHER_AES_CCM;
3965                 break;
3966         case WLAN_CIPHER_SUITE_GCMP:
3967         case WLAN_CIPHER_SUITE_GCMP_256:
3968                 arg.key_cipher = WMI_CIPHER_AES_GCM;
3969                 break;
3970         default:
3971                 ath11k_warn(ar->ab, "cipher %d is not supported\n", key->cipher);
3972                 return -EOPNOTSUPP;
3973         }
3974
3975         if (test_bit(ATH11K_FLAG_RAW_MODE, &ar->ab->dev_flags))
3976                 key->flags |= IEEE80211_KEY_FLAG_GENERATE_IV |
3977                               IEEE80211_KEY_FLAG_RESERVE_TAILROOM;
3978
3979 install:
3980         ret = ath11k_wmi_vdev_install_key(arvif->ar, &arg);
3981
3982         if (ret)
3983                 return ret;
3984
3985         if (!wait_for_completion_timeout(&ar->install_key_done, 1 * HZ))
3986                 return -ETIMEDOUT;
3987
3988         return ar->install_key_status ? -EINVAL : 0;
3989 }
3990
3991 static int ath11k_clear_peer_keys(struct ath11k_vif *arvif,
3992                                   const u8 *addr)
3993 {
3994         struct ath11k *ar = arvif->ar;
3995         struct ath11k_base *ab = ar->ab;
3996         struct ath11k_peer *peer;
3997         int first_errno = 0;
3998         int ret;
3999         int i;
4000         u32 flags = 0;
4001
4002         lockdep_assert_held(&ar->conf_mutex);
4003
4004         spin_lock_bh(&ab->base_lock);
4005         peer = ath11k_peer_find(ab, arvif->vdev_id, addr);
4006         spin_unlock_bh(&ab->base_lock);
4007
4008         if (!peer)
4009                 return -ENOENT;
4010
4011         for (i = 0; i < ARRAY_SIZE(peer->keys); i++) {
4012                 if (!peer->keys[i])
4013                         continue;
4014
4015                 /* key flags are not required to delete the key */
4016                 ret = ath11k_install_key(arvif, peer->keys[i],
4017                                          DISABLE_KEY, addr, flags);
4018                 if (ret < 0 && first_errno == 0)
4019                         first_errno = ret;
4020
4021                 if (ret < 0)
4022                         ath11k_warn(ab, "failed to remove peer key %d: %d\n",
4023                                     i, ret);
4024
4025                 spin_lock_bh(&ab->base_lock);
4026                 peer->keys[i] = NULL;
4027                 spin_unlock_bh(&ab->base_lock);
4028         }
4029
4030         return first_errno;
4031 }
4032
4033 static int ath11k_mac_op_set_key(struct ieee80211_hw *hw, enum set_key_cmd cmd,
4034                                  struct ieee80211_vif *vif, struct ieee80211_sta *sta,
4035                                  struct ieee80211_key_conf *key)
4036 {
4037         struct ath11k *ar = hw->priv;
4038         struct ath11k_base *ab = ar->ab;
4039         struct ath11k_vif *arvif = ath11k_vif_to_arvif(vif);
4040         struct ath11k_peer *peer;
4041         struct ath11k_sta *arsta;
4042         const u8 *peer_addr;
4043         int ret = 0;
4044         u32 flags = 0;
4045
4046         /* BIP needs to be done in software */
4047         if (key->cipher == WLAN_CIPHER_SUITE_AES_CMAC ||
4048             key->cipher == WLAN_CIPHER_SUITE_BIP_GMAC_128 ||
4049             key->cipher == WLAN_CIPHER_SUITE_BIP_GMAC_256 ||
4050             key->cipher == WLAN_CIPHER_SUITE_BIP_CMAC_256)
4051                 return 1;
4052
4053         if (test_bit(ATH11K_FLAG_HW_CRYPTO_DISABLED, &ar->ab->dev_flags))
4054                 return 1;
4055
4056         if (key->keyidx > WMI_MAX_KEY_INDEX)
4057                 return -ENOSPC;
4058
4059         mutex_lock(&ar->conf_mutex);
4060
4061         if (sta)
4062                 peer_addr = sta->addr;
4063         else if (arvif->vdev_type == WMI_VDEV_TYPE_STA)
4064                 peer_addr = vif->bss_conf.bssid;
4065         else
4066                 peer_addr = vif->addr;
4067
4068         key->hw_key_idx = key->keyidx;
4069
4070         /* the peer should not disappear in mid-way (unless FW goes awry) since
4071          * we already hold conf_mutex. we just make sure its there now.
4072          */
4073         spin_lock_bh(&ab->base_lock);
4074         peer = ath11k_peer_find(ab, arvif->vdev_id, peer_addr);
4075
4076         /* flush the fragments cache during key (re)install to
4077          * ensure all frags in the new frag list belong to the same key.
4078          */
4079         if (peer && sta && cmd == SET_KEY)
4080                 ath11k_peer_frags_flush(ar, peer);
4081         spin_unlock_bh(&ab->base_lock);
4082
4083         if (!peer) {
4084                 if (cmd == SET_KEY) {
4085                         ath11k_warn(ab, "cannot install key for non-existent peer %pM\n",
4086                                     peer_addr);
4087                         ret = -EOPNOTSUPP;
4088                         goto exit;
4089                 } else {
4090                         /* if the peer doesn't exist there is no key to disable
4091                          * anymore
4092                          */
4093                         goto exit;
4094                 }
4095         }
4096
4097         if (key->flags & IEEE80211_KEY_FLAG_PAIRWISE)
4098                 flags |= WMI_KEY_PAIRWISE;
4099         else
4100                 flags |= WMI_KEY_GROUP;
4101
4102         ret = ath11k_install_key(arvif, key, cmd, peer_addr, flags);
4103         if (ret) {
4104                 ath11k_warn(ab, "ath11k_install_key failed (%d)\n", ret);
4105                 goto exit;
4106         }
4107
4108         ret = ath11k_dp_peer_rx_pn_replay_config(arvif, peer_addr, cmd, key);
4109         if (ret) {
4110                 ath11k_warn(ab, "failed to offload PN replay detection %d\n", ret);
4111                 goto exit;
4112         }
4113
4114         spin_lock_bh(&ab->base_lock);
4115         peer = ath11k_peer_find(ab, arvif->vdev_id, peer_addr);
4116         if (peer && cmd == SET_KEY) {
4117                 peer->keys[key->keyidx] = key;
4118                 if (key->flags & IEEE80211_KEY_FLAG_PAIRWISE) {
4119                         peer->ucast_keyidx = key->keyidx;
4120                         peer->sec_type = ath11k_dp_tx_get_encrypt_type(key->cipher);
4121                 } else {
4122                         peer->mcast_keyidx = key->keyidx;
4123                         peer->sec_type_grp = ath11k_dp_tx_get_encrypt_type(key->cipher);
4124                 }
4125         } else if (peer && cmd == DISABLE_KEY) {
4126                 peer->keys[key->keyidx] = NULL;
4127                 if (key->flags & IEEE80211_KEY_FLAG_PAIRWISE)
4128                         peer->ucast_keyidx = 0;
4129                 else
4130                         peer->mcast_keyidx = 0;
4131         } else if (!peer)
4132                 /* impossible unless FW goes crazy */
4133                 ath11k_warn(ab, "peer %pM disappeared!\n", peer_addr);
4134
4135         if (sta) {
4136                 arsta = (struct ath11k_sta *)sta->drv_priv;
4137
4138                 switch (key->cipher) {
4139                 case WLAN_CIPHER_SUITE_TKIP:
4140                 case WLAN_CIPHER_SUITE_CCMP:
4141                 case WLAN_CIPHER_SUITE_CCMP_256:
4142                 case WLAN_CIPHER_SUITE_GCMP:
4143                 case WLAN_CIPHER_SUITE_GCMP_256:
4144                         if (cmd == SET_KEY)
4145                                 arsta->pn_type = HAL_PN_TYPE_WPA;
4146                         else
4147                                 arsta->pn_type = HAL_PN_TYPE_NONE;
4148                         break;
4149                 default:
4150                         arsta->pn_type = HAL_PN_TYPE_NONE;
4151                         break;
4152                 }
4153         }
4154
4155         spin_unlock_bh(&ab->base_lock);
4156
4157 exit:
4158         mutex_unlock(&ar->conf_mutex);
4159         return ret;
4160 }
4161
4162 static int
4163 ath11k_mac_bitrate_mask_num_vht_rates(struct ath11k *ar,
4164                                       enum nl80211_band band,
4165                                       const struct cfg80211_bitrate_mask *mask)
4166 {
4167         int num_rates = 0;
4168         int i;
4169
4170         for (i = 0; i < ARRAY_SIZE(mask->control[band].vht_mcs); i++)
4171                 num_rates += hweight16(mask->control[band].vht_mcs[i]);
4172
4173         return num_rates;
4174 }
4175
4176 static int
4177 ath11k_mac_bitrate_mask_num_he_rates(struct ath11k *ar,
4178                                      enum nl80211_band band,
4179                                      const struct cfg80211_bitrate_mask *mask)
4180 {
4181         int num_rates = 0;
4182         int i;
4183
4184         for (i = 0; i < ARRAY_SIZE(mask->control[band].he_mcs); i++)
4185                 num_rates += hweight16(mask->control[band].he_mcs[i]);
4186
4187         return num_rates;
4188 }
4189
4190 static int
4191 ath11k_mac_set_peer_vht_fixed_rate(struct ath11k_vif *arvif,
4192                                    struct ieee80211_sta *sta,
4193                                    const struct cfg80211_bitrate_mask *mask,
4194                                    enum nl80211_band band)
4195 {
4196         struct ath11k *ar = arvif->ar;
4197         u8 vht_rate, nss;
4198         u32 rate_code;
4199         int ret, i;
4200
4201         lockdep_assert_held(&ar->conf_mutex);
4202
4203         nss = 0;
4204
4205         for (i = 0; i < ARRAY_SIZE(mask->control[band].vht_mcs); i++) {
4206                 if (hweight16(mask->control[band].vht_mcs[i]) == 1) {
4207                         nss = i + 1;
4208                         vht_rate = ffs(mask->control[band].vht_mcs[i]) - 1;
4209                 }
4210         }
4211
4212         if (!nss) {
4213                 ath11k_warn(ar->ab, "No single VHT Fixed rate found to set for %pM",
4214                             sta->addr);
4215                 return -EINVAL;
4216         }
4217
4218         /* Avoid updating invalid nss as fixed rate*/
4219         if (nss > sta->deflink.rx_nss)
4220                 return -EINVAL;
4221
4222         ath11k_dbg(ar->ab, ATH11K_DBG_MAC,
4223                    "Setting Fixed VHT Rate for peer %pM. Device will not switch to any other selected rates",
4224                    sta->addr);
4225
4226         rate_code = ATH11K_HW_RATE_CODE(vht_rate, nss - 1,
4227                                         WMI_RATE_PREAMBLE_VHT);
4228         ret = ath11k_wmi_set_peer_param(ar, sta->addr,
4229                                         arvif->vdev_id,
4230                                         WMI_PEER_PARAM_FIXED_RATE,
4231                                         rate_code);
4232         if (ret)
4233                 ath11k_warn(ar->ab,
4234                             "failed to update STA %pM Fixed Rate %d: %d\n",
4235                              sta->addr, rate_code, ret);
4236
4237         return ret;
4238 }
4239
4240 static int
4241 ath11k_mac_set_peer_he_fixed_rate(struct ath11k_vif *arvif,
4242                                   struct ieee80211_sta *sta,
4243                                   const struct cfg80211_bitrate_mask *mask,
4244                                   enum nl80211_band band)
4245 {
4246         struct ath11k *ar = arvif->ar;
4247         u8 he_rate, nss;
4248         u32 rate_code;
4249         int ret, i;
4250
4251         lockdep_assert_held(&ar->conf_mutex);
4252
4253         nss = 0;
4254
4255         for (i = 0; i < ARRAY_SIZE(mask->control[band].he_mcs); i++) {
4256                 if (hweight16(mask->control[band].he_mcs[i]) == 1) {
4257                         nss = i + 1;
4258                         he_rate = ffs(mask->control[band].he_mcs[i]) - 1;
4259                 }
4260         }
4261
4262         if (!nss) {
4263                 ath11k_warn(ar->ab, "No single he fixed rate found to set for %pM",
4264                             sta->addr);
4265                 return -EINVAL;
4266         }
4267
4268         /* Avoid updating invalid nss as fixed rate */
4269         if (nss > sta->deflink.rx_nss)
4270                 return -EINVAL;
4271
4272         ath11k_dbg(ar->ab, ATH11K_DBG_MAC,
4273                    "mac setting fixed he rate for peer %pM, device will not switch to any other selected rates",
4274                    sta->addr);
4275
4276         rate_code = ATH11K_HW_RATE_CODE(he_rate, nss - 1,
4277                                         WMI_RATE_PREAMBLE_HE);
4278
4279         ret = ath11k_wmi_set_peer_param(ar, sta->addr,
4280                                         arvif->vdev_id,
4281                                         WMI_PEER_PARAM_FIXED_RATE,
4282                                         rate_code);
4283         if (ret)
4284                 ath11k_warn(ar->ab,
4285                             "failed to update sta %pM fixed rate %d: %d\n",
4286                             sta->addr, rate_code, ret);
4287
4288         return ret;
4289 }
4290
4291 static int ath11k_station_assoc(struct ath11k *ar,
4292                                 struct ieee80211_vif *vif,
4293                                 struct ieee80211_sta *sta,
4294                                 bool reassoc)
4295 {
4296         struct ath11k_vif *arvif = ath11k_vif_to_arvif(vif);
4297         struct peer_assoc_params peer_arg;
4298         int ret = 0;
4299         struct cfg80211_chan_def def;
4300         enum nl80211_band band;
4301         struct cfg80211_bitrate_mask *mask;
4302         u8 num_vht_rates, num_he_rates;
4303
4304         lockdep_assert_held(&ar->conf_mutex);
4305
4306         if (WARN_ON(ath11k_mac_vif_chan(vif, &def)))
4307                 return -EPERM;
4308
4309         band = def.chan->band;
4310         mask = &arvif->bitrate_mask;
4311
4312         ath11k_peer_assoc_prepare(ar, vif, sta, &peer_arg, reassoc);
4313
4314         peer_arg.is_assoc = true;
4315         ret = ath11k_wmi_send_peer_assoc_cmd(ar, &peer_arg);
4316         if (ret) {
4317                 ath11k_warn(ar->ab, "failed to run peer assoc for STA %pM vdev %i: %d\n",
4318                             sta->addr, arvif->vdev_id, ret);
4319                 return ret;
4320         }
4321
4322         if (!wait_for_completion_timeout(&ar->peer_assoc_done, 1 * HZ)) {
4323                 ath11k_warn(ar->ab, "failed to get peer assoc conf event for %pM vdev %i\n",
4324                             sta->addr, arvif->vdev_id);
4325                 return -ETIMEDOUT;
4326         }
4327
4328         num_vht_rates = ath11k_mac_bitrate_mask_num_vht_rates(ar, band, mask);
4329         num_he_rates = ath11k_mac_bitrate_mask_num_he_rates(ar, band, mask);
4330
4331         /* If single VHT/HE rate is configured (by set_bitrate_mask()),
4332          * peer_assoc will disable VHT/HE. This is now enabled by a peer specific
4333          * fixed param.
4334          * Note that all other rates and NSS will be disabled for this peer.
4335          */
4336         if (sta->deflink.vht_cap.vht_supported && num_vht_rates == 1) {
4337                 ret = ath11k_mac_set_peer_vht_fixed_rate(arvif, sta, mask,
4338                                                          band);
4339                 if (ret)
4340                         return ret;
4341         } else if (sta->deflink.he_cap.has_he && num_he_rates == 1) {
4342                 ret = ath11k_mac_set_peer_he_fixed_rate(arvif, sta, mask,
4343                                                         band);
4344                 if (ret)
4345                         return ret;
4346         }
4347
4348         /* Re-assoc is run only to update supported rates for given station. It
4349          * doesn't make much sense to reconfigure the peer completely.
4350          */
4351         if (reassoc)
4352                 return 0;
4353
4354         ret = ath11k_setup_peer_smps(ar, arvif, sta->addr,
4355                                      &sta->deflink.ht_cap,
4356                                      le16_to_cpu(sta->deflink.he_6ghz_capa.capa));
4357         if (ret) {
4358                 ath11k_warn(ar->ab, "failed to setup peer SMPS for vdev %d: %d\n",
4359                             arvif->vdev_id, ret);
4360                 return ret;
4361         }
4362
4363         if (!sta->wme) {
4364                 arvif->num_legacy_stations++;
4365                 ret = ath11k_recalc_rtscts_prot(arvif);
4366                 if (ret)
4367                         return ret;
4368         }
4369
4370         if (sta->wme && sta->uapsd_queues) {
4371                 ret = ath11k_peer_assoc_qos_ap(ar, arvif, sta);
4372                 if (ret) {
4373                         ath11k_warn(ar->ab, "failed to set qos params for STA %pM for vdev %i: %d\n",
4374                                     sta->addr, arvif->vdev_id, ret);
4375                         return ret;
4376                 }
4377         }
4378
4379         return 0;
4380 }
4381
4382 static int ath11k_station_disassoc(struct ath11k *ar,
4383                                    struct ieee80211_vif *vif,
4384                                    struct ieee80211_sta *sta)
4385 {
4386         struct ath11k_vif *arvif = (void *)vif->drv_priv;
4387         int ret = 0;
4388
4389         lockdep_assert_held(&ar->conf_mutex);
4390
4391         if (!sta->wme) {
4392                 arvif->num_legacy_stations--;
4393                 ret = ath11k_recalc_rtscts_prot(arvif);
4394                 if (ret)
4395                         return ret;
4396         }
4397
4398         ret = ath11k_clear_peer_keys(arvif, sta->addr);
4399         if (ret) {
4400                 ath11k_warn(ar->ab, "failed to clear all peer keys for vdev %i: %d\n",
4401                             arvif->vdev_id, ret);
4402                 return ret;
4403         }
4404         return 0;
4405 }
4406
4407 static void ath11k_sta_rc_update_wk(struct work_struct *wk)
4408 {
4409         struct ath11k *ar;
4410         struct ath11k_vif *arvif;
4411         struct ath11k_sta *arsta;
4412         struct ieee80211_sta *sta;
4413         struct cfg80211_chan_def def;
4414         enum nl80211_band band;
4415         const u8 *ht_mcs_mask;
4416         const u16 *vht_mcs_mask;
4417         const u16 *he_mcs_mask;
4418         u32 changed, bw, nss, smps, bw_prev;
4419         int err, num_vht_rates, num_he_rates;
4420         const struct cfg80211_bitrate_mask *mask;
4421         struct peer_assoc_params peer_arg;
4422         enum wmi_phy_mode peer_phymode;
4423
4424         arsta = container_of(wk, struct ath11k_sta, update_wk);
4425         sta = container_of((void *)arsta, struct ieee80211_sta, drv_priv);
4426         arvif = arsta->arvif;
4427         ar = arvif->ar;
4428
4429         if (WARN_ON(ath11k_mac_vif_chan(arvif->vif, &def)))
4430                 return;
4431
4432         band = def.chan->band;
4433         ht_mcs_mask = arvif->bitrate_mask.control[band].ht_mcs;
4434         vht_mcs_mask = arvif->bitrate_mask.control[band].vht_mcs;
4435         he_mcs_mask = arvif->bitrate_mask.control[band].he_mcs;
4436
4437         spin_lock_bh(&ar->data_lock);
4438
4439         changed = arsta->changed;
4440         arsta->changed = 0;
4441
4442         bw = arsta->bw;
4443         bw_prev = arsta->bw_prev;
4444         nss = arsta->nss;
4445         smps = arsta->smps;
4446
4447         spin_unlock_bh(&ar->data_lock);
4448
4449         mutex_lock(&ar->conf_mutex);
4450
4451         nss = max_t(u32, 1, nss);
4452         nss = min(nss, max(max(ath11k_mac_max_ht_nss(ht_mcs_mask),
4453                                ath11k_mac_max_vht_nss(vht_mcs_mask)),
4454                            ath11k_mac_max_he_nss(he_mcs_mask)));
4455
4456         if (changed & IEEE80211_RC_BW_CHANGED) {
4457                 /* Get the peer phymode */
4458                 ath11k_peer_assoc_h_phymode(ar, arvif->vif, sta, &peer_arg);
4459                 peer_phymode = peer_arg.peer_phymode;
4460
4461                 ath11k_dbg(ar->ab, ATH11K_DBG_MAC, "mac update sta %pM peer bw %d phymode %d\n",
4462                            sta->addr, bw, peer_phymode);
4463
4464                 if (bw > bw_prev) {
4465                         /* BW is upgraded. In this case we send WMI_PEER_PHYMODE
4466                          * followed by WMI_PEER_CHWIDTH
4467                          */
4468                         ath11k_dbg(ar->ab, ATH11K_DBG_MAC, "mac BW upgrade for sta %pM new BW %d, old BW %d\n",
4469                                    sta->addr, bw, bw_prev);
4470
4471                         err = ath11k_wmi_set_peer_param(ar, sta->addr, arvif->vdev_id,
4472                                                         WMI_PEER_PHYMODE, peer_phymode);
4473
4474                         if (err) {
4475                                 ath11k_warn(ar->ab, "failed to update STA %pM peer phymode %d: %d\n",
4476                                             sta->addr, peer_phymode, err);
4477                                 goto err_rc_bw_changed;
4478                         }
4479
4480                         err = ath11k_wmi_set_peer_param(ar, sta->addr, arvif->vdev_id,
4481                                                         WMI_PEER_CHWIDTH, bw);
4482
4483                         if (err)
4484                                 ath11k_warn(ar->ab, "failed to update STA %pM peer bw %d: %d\n",
4485                                             sta->addr, bw, err);
4486                 } else {
4487                         /* BW is downgraded. In this case we send WMI_PEER_CHWIDTH
4488                          * followed by WMI_PEER_PHYMODE
4489                          */
4490                         ath11k_dbg(ar->ab, ATH11K_DBG_MAC, "mac BW downgrade for sta %pM new BW %d,old BW %d\n",
4491                                    sta->addr, bw, bw_prev);
4492
4493                         err = ath11k_wmi_set_peer_param(ar, sta->addr, arvif->vdev_id,
4494                                                         WMI_PEER_CHWIDTH, bw);
4495
4496                         if (err) {
4497                                 ath11k_warn(ar->ab, "failed to update STA %pM peer bw %d: %d\n",
4498                                             sta->addr, bw, err);
4499                                 goto err_rc_bw_changed;
4500                         }
4501
4502                         err = ath11k_wmi_set_peer_param(ar, sta->addr, arvif->vdev_id,
4503                                                         WMI_PEER_PHYMODE, peer_phymode);
4504
4505                         if (err)
4506                                 ath11k_warn(ar->ab, "failed to update STA %pM peer phymode %d: %d\n",
4507                                             sta->addr, peer_phymode, err);
4508                 }
4509         }
4510
4511         if (changed & IEEE80211_RC_NSS_CHANGED) {
4512                 ath11k_dbg(ar->ab, ATH11K_DBG_MAC, "mac update sta %pM nss %d\n",
4513                            sta->addr, nss);
4514
4515                 err = ath11k_wmi_set_peer_param(ar, sta->addr, arvif->vdev_id,
4516                                                 WMI_PEER_NSS, nss);
4517                 if (err)
4518                         ath11k_warn(ar->ab, "failed to update STA %pM nss %d: %d\n",
4519                                     sta->addr, nss, err);
4520         }
4521
4522         if (changed & IEEE80211_RC_SMPS_CHANGED) {
4523                 ath11k_dbg(ar->ab, ATH11K_DBG_MAC, "mac update sta %pM smps %d\n",
4524                            sta->addr, smps);
4525
4526                 err = ath11k_wmi_set_peer_param(ar, sta->addr, arvif->vdev_id,
4527                                                 WMI_PEER_MIMO_PS_STATE, smps);
4528                 if (err)
4529                         ath11k_warn(ar->ab, "failed to update STA %pM smps %d: %d\n",
4530                                     sta->addr, smps, err);
4531         }
4532
4533         if (changed & IEEE80211_RC_SUPP_RATES_CHANGED) {
4534                 mask = &arvif->bitrate_mask;
4535                 num_vht_rates = ath11k_mac_bitrate_mask_num_vht_rates(ar, band,
4536                                                                       mask);
4537                 num_he_rates = ath11k_mac_bitrate_mask_num_he_rates(ar, band,
4538                                                                     mask);
4539
4540                 /* Peer_assoc_prepare will reject vht rates in
4541                  * bitrate_mask if its not available in range format and
4542                  * sets vht tx_rateset as unsupported. So multiple VHT MCS
4543                  * setting(eg. MCS 4,5,6) per peer is not supported here.
4544                  * But, Single rate in VHT mask can be set as per-peer
4545                  * fixed rate. But even if any HT rates are configured in
4546                  * the bitrate mask, device will not switch to those rates
4547                  * when per-peer Fixed rate is set.
4548                  * TODO: Check RATEMASK_CMDID to support auto rates selection
4549                  * across HT/VHT and for multiple VHT MCS support.
4550                  */
4551                 if (sta->deflink.vht_cap.vht_supported && num_vht_rates == 1) {
4552                         ath11k_mac_set_peer_vht_fixed_rate(arvif, sta, mask,
4553                                                            band);
4554                 } else if (sta->deflink.he_cap.has_he && num_he_rates == 1) {
4555                         ath11k_mac_set_peer_he_fixed_rate(arvif, sta, mask,
4556                                                           band);
4557                 } else {
4558                         /* If the peer is non-VHT/HE or no fixed VHT/HE rate
4559                          * is provided in the new bitrate mask we set the
4560                          * other rates using peer_assoc command. Also clear
4561                          * the peer fixed rate settings as it has higher proprity
4562                          * than peer assoc
4563                          */
4564                         err = ath11k_wmi_set_peer_param(ar, sta->addr,
4565                                                         arvif->vdev_id,
4566                                                         WMI_PEER_PARAM_FIXED_RATE,
4567                                                         WMI_FIXED_RATE_NONE);
4568                         if (err)
4569                                 ath11k_warn(ar->ab,
4570                                             "failed to disable peer fixed rate for sta %pM: %d\n",
4571                                             sta->addr, err);
4572
4573                         ath11k_peer_assoc_prepare(ar, arvif->vif, sta,
4574                                                   &peer_arg, true);
4575
4576                         peer_arg.is_assoc = false;
4577                         err = ath11k_wmi_send_peer_assoc_cmd(ar, &peer_arg);
4578                         if (err)
4579                                 ath11k_warn(ar->ab, "failed to run peer assoc for STA %pM vdev %i: %d\n",
4580                                             sta->addr, arvif->vdev_id, err);
4581
4582                         if (!wait_for_completion_timeout(&ar->peer_assoc_done, 1 * HZ))
4583                                 ath11k_warn(ar->ab, "failed to get peer assoc conf event for %pM vdev %i\n",
4584                                             sta->addr, arvif->vdev_id);
4585                 }
4586         }
4587
4588 err_rc_bw_changed:
4589         mutex_unlock(&ar->conf_mutex);
4590 }
4591
4592 static void ath11k_sta_set_4addr_wk(struct work_struct *wk)
4593 {
4594         struct ath11k *ar;
4595         struct ath11k_vif *arvif;
4596         struct ath11k_sta *arsta;
4597         struct ieee80211_sta *sta;
4598         int ret = 0;
4599
4600         arsta = container_of(wk, struct ath11k_sta, set_4addr_wk);
4601         sta = container_of((void *)arsta, struct ieee80211_sta, drv_priv);
4602         arvif = arsta->arvif;
4603         ar = arvif->ar;
4604
4605         ath11k_dbg(ar->ab, ATH11K_DBG_MAC,
4606                    "setting USE_4ADDR for peer %pM\n", sta->addr);
4607
4608         ret = ath11k_wmi_set_peer_param(ar, sta->addr,
4609                                         arvif->vdev_id,
4610                                         WMI_PEER_USE_4ADDR, 1);
4611
4612         if (ret)
4613                 ath11k_warn(ar->ab, "failed to set peer %pM 4addr capability: %d\n",
4614                             sta->addr, ret);
4615 }
4616
4617 static int ath11k_mac_inc_num_stations(struct ath11k_vif *arvif,
4618                                        struct ieee80211_sta *sta)
4619 {
4620         struct ath11k *ar = arvif->ar;
4621
4622         lockdep_assert_held(&ar->conf_mutex);
4623
4624         if (arvif->vdev_type == WMI_VDEV_TYPE_STA && !sta->tdls)
4625                 return 0;
4626
4627         if (ar->num_stations >= ar->max_num_stations)
4628                 return -ENOBUFS;
4629
4630         ar->num_stations++;
4631
4632         return 0;
4633 }
4634
4635 static void ath11k_mac_dec_num_stations(struct ath11k_vif *arvif,
4636                                         struct ieee80211_sta *sta)
4637 {
4638         struct ath11k *ar = arvif->ar;
4639
4640         lockdep_assert_held(&ar->conf_mutex);
4641
4642         if (arvif->vdev_type == WMI_VDEV_TYPE_STA && !sta->tdls)
4643                 return;
4644
4645         ar->num_stations--;
4646 }
4647
4648 static int ath11k_mac_station_add(struct ath11k *ar,
4649                                   struct ieee80211_vif *vif,
4650                                   struct ieee80211_sta *sta)
4651 {
4652         struct ath11k_base *ab = ar->ab;
4653         struct ath11k_vif *arvif = ath11k_vif_to_arvif(vif);
4654         struct ath11k_sta *arsta = (struct ath11k_sta *)sta->drv_priv;
4655         struct peer_create_params peer_param;
4656         int ret;
4657
4658         lockdep_assert_held(&ar->conf_mutex);
4659
4660         ret = ath11k_mac_inc_num_stations(arvif, sta);
4661         if (ret) {
4662                 ath11k_warn(ab, "refusing to associate station: too many connected already (%d)\n",
4663                             ar->max_num_stations);
4664                 goto exit;
4665         }
4666
4667         arsta->rx_stats = kzalloc(sizeof(*arsta->rx_stats), GFP_KERNEL);
4668         if (!arsta->rx_stats) {
4669                 ret = -ENOMEM;
4670                 goto dec_num_station;
4671         }
4672
4673         peer_param.vdev_id = arvif->vdev_id;
4674         peer_param.peer_addr = sta->addr;
4675         peer_param.peer_type = WMI_PEER_TYPE_DEFAULT;
4676
4677         ret = ath11k_peer_create(ar, arvif, sta, &peer_param);
4678         if (ret) {
4679                 ath11k_warn(ab, "Failed to add peer: %pM for VDEV: %d\n",
4680                             sta->addr, arvif->vdev_id);
4681                 goto free_rx_stats;
4682         }
4683
4684         ath11k_dbg(ab, ATH11K_DBG_MAC, "Added peer: %pM for VDEV: %d\n",
4685                    sta->addr, arvif->vdev_id);
4686
4687         if (ath11k_debugfs_is_extd_tx_stats_enabled(ar)) {
4688                 arsta->tx_stats = kzalloc(sizeof(*arsta->tx_stats), GFP_KERNEL);
4689                 if (!arsta->tx_stats) {
4690                         ret = -ENOMEM;
4691                         goto free_peer;
4692                 }
4693         }
4694
4695         if (ieee80211_vif_is_mesh(vif)) {
4696                 ath11k_dbg(ab, ATH11K_DBG_MAC,
4697                            "setting USE_4ADDR for mesh STA %pM\n", sta->addr);
4698                 ret = ath11k_wmi_set_peer_param(ar, sta->addr,
4699                                                 arvif->vdev_id,
4700                                                 WMI_PEER_USE_4ADDR, 1);
4701                 if (ret) {
4702                         ath11k_warn(ab, "failed to set mesh STA %pM 4addr capability: %d\n",
4703                                     sta->addr, ret);
4704                         goto free_tx_stats;
4705                 }
4706         }
4707
4708         ret = ath11k_dp_peer_setup(ar, arvif->vdev_id, sta->addr);
4709         if (ret) {
4710                 ath11k_warn(ab, "failed to setup dp for peer %pM on vdev %i (%d)\n",
4711                             sta->addr, arvif->vdev_id, ret);
4712                 goto free_tx_stats;
4713         }
4714
4715         if (ab->hw_params.vdev_start_delay &&
4716             !arvif->is_started &&
4717             arvif->vdev_type != WMI_VDEV_TYPE_AP) {
4718                 ret = ath11k_start_vdev_delay(ar->hw, vif);
4719                 if (ret) {
4720                         ath11k_warn(ab, "failed to delay vdev start: %d\n", ret);
4721                         goto free_tx_stats;
4722                 }
4723         }
4724
4725         ewma_avg_rssi_init(&arsta->avg_rssi);
4726         return 0;
4727
4728 free_tx_stats:
4729         kfree(arsta->tx_stats);
4730         arsta->tx_stats = NULL;
4731 free_peer:
4732         ath11k_peer_delete(ar, arvif->vdev_id, sta->addr);
4733 free_rx_stats:
4734         kfree(arsta->rx_stats);
4735         arsta->rx_stats = NULL;
4736 dec_num_station:
4737         ath11k_mac_dec_num_stations(arvif, sta);
4738 exit:
4739         return ret;
4740 }
4741
4742 static u32 ath11k_mac_ieee80211_sta_bw_to_wmi(struct ath11k *ar,
4743                                               struct ieee80211_sta *sta)
4744 {
4745         u32 bw = WMI_PEER_CHWIDTH_20MHZ;
4746
4747         switch (sta->deflink.bandwidth) {
4748         case IEEE80211_STA_RX_BW_20:
4749                 bw = WMI_PEER_CHWIDTH_20MHZ;
4750                 break;
4751         case IEEE80211_STA_RX_BW_40:
4752                 bw = WMI_PEER_CHWIDTH_40MHZ;
4753                 break;
4754         case IEEE80211_STA_RX_BW_80:
4755                 bw = WMI_PEER_CHWIDTH_80MHZ;
4756                 break;
4757         case IEEE80211_STA_RX_BW_160:
4758                 bw = WMI_PEER_CHWIDTH_160MHZ;
4759                 break;
4760         default:
4761                 ath11k_warn(ar->ab, "Invalid bandwidth %d for %pM\n",
4762                             sta->deflink.bandwidth, sta->addr);
4763                 bw = WMI_PEER_CHWIDTH_20MHZ;
4764                 break;
4765         }
4766
4767         return bw;
4768 }
4769
4770 static int ath11k_mac_op_sta_state(struct ieee80211_hw *hw,
4771                                    struct ieee80211_vif *vif,
4772                                    struct ieee80211_sta *sta,
4773                                    enum ieee80211_sta_state old_state,
4774                                    enum ieee80211_sta_state new_state)
4775 {
4776         struct ath11k *ar = hw->priv;
4777         struct ath11k_vif *arvif = ath11k_vif_to_arvif(vif);
4778         struct ath11k_sta *arsta = (struct ath11k_sta *)sta->drv_priv;
4779         struct ath11k_peer *peer;
4780         int ret = 0;
4781
4782         /* cancel must be done outside the mutex to avoid deadlock */
4783         if ((old_state == IEEE80211_STA_NONE &&
4784              new_state == IEEE80211_STA_NOTEXIST)) {
4785                 cancel_work_sync(&arsta->update_wk);
4786                 cancel_work_sync(&arsta->set_4addr_wk);
4787         }
4788
4789         mutex_lock(&ar->conf_mutex);
4790
4791         if (old_state == IEEE80211_STA_NOTEXIST &&
4792             new_state == IEEE80211_STA_NONE) {
4793                 memset(arsta, 0, sizeof(*arsta));
4794                 arsta->arvif = arvif;
4795                 arsta->peer_ps_state = WMI_PEER_PS_STATE_DISABLED;
4796                 INIT_WORK(&arsta->update_wk, ath11k_sta_rc_update_wk);
4797                 INIT_WORK(&arsta->set_4addr_wk, ath11k_sta_set_4addr_wk);
4798
4799                 ret = ath11k_mac_station_add(ar, vif, sta);
4800                 if (ret)
4801                         ath11k_warn(ar->ab, "Failed to add station: %pM for VDEV: %d\n",
4802                                     sta->addr, arvif->vdev_id);
4803         } else if ((old_state == IEEE80211_STA_NONE &&
4804                     new_state == IEEE80211_STA_NOTEXIST)) {
4805                 bool skip_peer_delete = ar->ab->hw_params.vdev_start_delay &&
4806                         vif->type == NL80211_IFTYPE_STATION;
4807
4808                 ath11k_dp_peer_cleanup(ar, arvif->vdev_id, sta->addr);
4809
4810                 if (!skip_peer_delete) {
4811                         ret = ath11k_peer_delete(ar, arvif->vdev_id, sta->addr);
4812                         if (ret)
4813                                 ath11k_warn(ar->ab,
4814                                             "Failed to delete peer: %pM for VDEV: %d\n",
4815                                             sta->addr, arvif->vdev_id);
4816                         else
4817                                 ath11k_dbg(ar->ab,
4818                                            ATH11K_DBG_MAC,
4819                                            "Removed peer: %pM for VDEV: %d\n",
4820                                            sta->addr, arvif->vdev_id);
4821                 }
4822
4823                 ath11k_mac_dec_num_stations(arvif, sta);
4824                 mutex_lock(&ar->ab->tbl_mtx_lock);
4825                 spin_lock_bh(&ar->ab->base_lock);
4826                 peer = ath11k_peer_find(ar->ab, arvif->vdev_id, sta->addr);
4827                 if (skip_peer_delete && peer) {
4828                         peer->sta = NULL;
4829                 } else if (peer && peer->sta == sta) {
4830                         ath11k_warn(ar->ab, "Found peer entry %pM n vdev %i after it was supposedly removed\n",
4831                                     vif->addr, arvif->vdev_id);
4832                         ath11k_peer_rhash_delete(ar->ab, peer);
4833                         peer->sta = NULL;
4834                         list_del(&peer->list);
4835                         kfree(peer);
4836                         ar->num_peers--;
4837                 }
4838                 spin_unlock_bh(&ar->ab->base_lock);
4839                 mutex_unlock(&ar->ab->tbl_mtx_lock);
4840
4841                 kfree(arsta->tx_stats);
4842                 arsta->tx_stats = NULL;
4843
4844                 kfree(arsta->rx_stats);
4845                 arsta->rx_stats = NULL;
4846         } else if (old_state == IEEE80211_STA_AUTH &&
4847                    new_state == IEEE80211_STA_ASSOC &&
4848                    (vif->type == NL80211_IFTYPE_AP ||
4849                     vif->type == NL80211_IFTYPE_MESH_POINT ||
4850                     vif->type == NL80211_IFTYPE_ADHOC)) {
4851                 ret = ath11k_station_assoc(ar, vif, sta, false);
4852                 if (ret)
4853                         ath11k_warn(ar->ab, "Failed to associate station: %pM\n",
4854                                     sta->addr);
4855
4856                 spin_lock_bh(&ar->data_lock);
4857                 /* Set arsta bw and prev bw */
4858                 arsta->bw = ath11k_mac_ieee80211_sta_bw_to_wmi(ar, sta);
4859                 arsta->bw_prev = arsta->bw;
4860                 spin_unlock_bh(&ar->data_lock);
4861         } else if (old_state == IEEE80211_STA_ASSOC &&
4862                    new_state == IEEE80211_STA_AUTHORIZED) {
4863                 spin_lock_bh(&ar->ab->base_lock);
4864
4865                 peer = ath11k_peer_find(ar->ab, arvif->vdev_id, sta->addr);
4866                 if (peer)
4867                         peer->is_authorized = true;
4868
4869                 spin_unlock_bh(&ar->ab->base_lock);
4870
4871                 if (vif->type == NL80211_IFTYPE_STATION && arvif->is_up) {
4872                         ret = ath11k_wmi_set_peer_param(ar, sta->addr,
4873                                                         arvif->vdev_id,
4874                                                         WMI_PEER_AUTHORIZE,
4875                                                         1);
4876                         if (ret)
4877                                 ath11k_warn(ar->ab, "Unable to authorize peer %pM vdev %d: %d\n",
4878                                             sta->addr, arvif->vdev_id, ret);
4879                 }
4880         } else if (old_state == IEEE80211_STA_AUTHORIZED &&
4881                    new_state == IEEE80211_STA_ASSOC) {
4882                 spin_lock_bh(&ar->ab->base_lock);
4883
4884                 peer = ath11k_peer_find(ar->ab, arvif->vdev_id, sta->addr);
4885                 if (peer)
4886                         peer->is_authorized = false;
4887
4888                 spin_unlock_bh(&ar->ab->base_lock);
4889         } else if (old_state == IEEE80211_STA_ASSOC &&
4890                    new_state == IEEE80211_STA_AUTH &&
4891                    (vif->type == NL80211_IFTYPE_AP ||
4892                     vif->type == NL80211_IFTYPE_MESH_POINT ||
4893                     vif->type == NL80211_IFTYPE_ADHOC)) {
4894                 ret = ath11k_station_disassoc(ar, vif, sta);
4895                 if (ret)
4896                         ath11k_warn(ar->ab, "Failed to disassociate station: %pM\n",
4897                                     sta->addr);
4898         }
4899
4900         mutex_unlock(&ar->conf_mutex);
4901         return ret;
4902 }
4903
4904 static int ath11k_mac_op_sta_set_txpwr(struct ieee80211_hw *hw,
4905                                        struct ieee80211_vif *vif,
4906                                        struct ieee80211_sta *sta)
4907 {
4908         struct ath11k *ar = hw->priv;
4909         struct ath11k_vif *arvif = (void *)vif->drv_priv;
4910         int ret = 0;
4911         s16 txpwr;
4912
4913         if (sta->deflink.txpwr.type == NL80211_TX_POWER_AUTOMATIC) {
4914                 txpwr = 0;
4915         } else {
4916                 txpwr = sta->deflink.txpwr.power;
4917                 if (!txpwr)
4918                         return -EINVAL;
4919         }
4920
4921         if (txpwr > ATH11K_TX_POWER_MAX_VAL || txpwr < ATH11K_TX_POWER_MIN_VAL)
4922                 return -EINVAL;
4923
4924         mutex_lock(&ar->conf_mutex);
4925
4926         ret = ath11k_wmi_set_peer_param(ar, sta->addr, arvif->vdev_id,
4927                                         WMI_PEER_USE_FIXED_PWR, txpwr);
4928         if (ret) {
4929                 ath11k_warn(ar->ab, "failed to set tx power for station ret: %d\n",
4930                             ret);
4931                 goto out;
4932         }
4933
4934 out:
4935         mutex_unlock(&ar->conf_mutex);
4936         return ret;
4937 }
4938
4939 static void ath11k_mac_op_sta_set_4addr(struct ieee80211_hw *hw,
4940                                         struct ieee80211_vif *vif,
4941                                         struct ieee80211_sta *sta, bool enabled)
4942 {
4943         struct ath11k *ar = hw->priv;
4944         struct ath11k_sta *arsta = (struct ath11k_sta *)sta->drv_priv;
4945
4946         if (enabled && !arsta->use_4addr_set) {
4947                 ieee80211_queue_work(ar->hw, &arsta->set_4addr_wk);
4948                 arsta->use_4addr_set = true;
4949         }
4950 }
4951
4952 static void ath11k_mac_op_sta_rc_update(struct ieee80211_hw *hw,
4953                                         struct ieee80211_vif *vif,
4954                                         struct ieee80211_sta *sta,
4955                                         u32 changed)
4956 {
4957         struct ath11k *ar = hw->priv;
4958         struct ath11k_sta *arsta = (struct ath11k_sta *)sta->drv_priv;
4959         struct ath11k_vif *arvif = (void *)vif->drv_priv;
4960         struct ath11k_peer *peer;
4961         u32 bw, smps;
4962
4963         spin_lock_bh(&ar->ab->base_lock);
4964
4965         peer = ath11k_peer_find(ar->ab, arvif->vdev_id, sta->addr);
4966         if (!peer) {
4967                 spin_unlock_bh(&ar->ab->base_lock);
4968                 ath11k_warn(ar->ab, "mac sta rc update failed to find peer %pM on vdev %i\n",
4969                             sta->addr, arvif->vdev_id);
4970                 return;
4971         }
4972
4973         spin_unlock_bh(&ar->ab->base_lock);
4974
4975         ath11k_dbg(ar->ab, ATH11K_DBG_MAC,
4976                    "mac sta rc update for %pM changed %08x bw %d nss %d smps %d\n",
4977                    sta->addr, changed, sta->deflink.bandwidth,
4978                    sta->deflink.rx_nss,
4979                    sta->deflink.smps_mode);
4980
4981         spin_lock_bh(&ar->data_lock);
4982
4983         if (changed & IEEE80211_RC_BW_CHANGED) {
4984                 bw = ath11k_mac_ieee80211_sta_bw_to_wmi(ar, sta);
4985                 arsta->bw_prev = arsta->bw;
4986                 arsta->bw = bw;
4987         }
4988
4989         if (changed & IEEE80211_RC_NSS_CHANGED)
4990                 arsta->nss = sta->deflink.rx_nss;
4991
4992         if (changed & IEEE80211_RC_SMPS_CHANGED) {
4993                 smps = WMI_PEER_SMPS_PS_NONE;
4994
4995                 switch (sta->deflink.smps_mode) {
4996                 case IEEE80211_SMPS_AUTOMATIC:
4997                 case IEEE80211_SMPS_OFF:
4998                         smps = WMI_PEER_SMPS_PS_NONE;
4999                         break;
5000                 case IEEE80211_SMPS_STATIC:
5001                         smps = WMI_PEER_SMPS_STATIC;
5002                         break;
5003                 case IEEE80211_SMPS_DYNAMIC:
5004                         smps = WMI_PEER_SMPS_DYNAMIC;
5005                         break;
5006                 default:
5007                         ath11k_warn(ar->ab, "Invalid smps %d in sta rc update for %pM\n",
5008                                     sta->deflink.smps_mode, sta->addr);
5009                         smps = WMI_PEER_SMPS_PS_NONE;
5010                         break;
5011                 }
5012
5013                 arsta->smps = smps;
5014         }
5015
5016         arsta->changed |= changed;
5017
5018         spin_unlock_bh(&ar->data_lock);
5019
5020         ieee80211_queue_work(hw, &arsta->update_wk);
5021 }
5022
5023 static int ath11k_conf_tx_uapsd(struct ath11k *ar, struct ieee80211_vif *vif,
5024                                 u16 ac, bool enable)
5025 {
5026         struct ath11k_vif *arvif = ath11k_vif_to_arvif(vif);
5027         u32 value = 0;
5028         int ret = 0;
5029
5030         if (arvif->vdev_type != WMI_VDEV_TYPE_STA)
5031                 return 0;
5032
5033         switch (ac) {
5034         case IEEE80211_AC_VO:
5035                 value = WMI_STA_PS_UAPSD_AC3_DELIVERY_EN |
5036                         WMI_STA_PS_UAPSD_AC3_TRIGGER_EN;
5037                 break;
5038         case IEEE80211_AC_VI:
5039                 value = WMI_STA_PS_UAPSD_AC2_DELIVERY_EN |
5040                         WMI_STA_PS_UAPSD_AC2_TRIGGER_EN;
5041                 break;
5042         case IEEE80211_AC_BE:
5043                 value = WMI_STA_PS_UAPSD_AC1_DELIVERY_EN |
5044                         WMI_STA_PS_UAPSD_AC1_TRIGGER_EN;
5045                 break;
5046         case IEEE80211_AC_BK:
5047                 value = WMI_STA_PS_UAPSD_AC0_DELIVERY_EN |
5048                         WMI_STA_PS_UAPSD_AC0_TRIGGER_EN;
5049                 break;
5050         }
5051
5052         if (enable)
5053                 arvif->u.sta.uapsd |= value;
5054         else
5055                 arvif->u.sta.uapsd &= ~value;
5056
5057         ret = ath11k_wmi_set_sta_ps_param(ar, arvif->vdev_id,
5058                                           WMI_STA_PS_PARAM_UAPSD,
5059                                           arvif->u.sta.uapsd);
5060         if (ret) {
5061                 ath11k_warn(ar->ab, "could not set uapsd params %d\n", ret);
5062                 goto exit;
5063         }
5064
5065         if (arvif->u.sta.uapsd)
5066                 value = WMI_STA_PS_RX_WAKE_POLICY_POLL_UAPSD;
5067         else
5068                 value = WMI_STA_PS_RX_WAKE_POLICY_WAKE;
5069
5070         ret = ath11k_wmi_set_sta_ps_param(ar, arvif->vdev_id,
5071                                           WMI_STA_PS_PARAM_RX_WAKE_POLICY,
5072                                           value);
5073         if (ret)
5074                 ath11k_warn(ar->ab, "could not set rx wake param %d\n", ret);
5075
5076 exit:
5077         return ret;
5078 }
5079
5080 static int ath11k_mac_op_conf_tx(struct ieee80211_hw *hw,
5081                                  struct ieee80211_vif *vif,
5082                                  unsigned int link_id, u16 ac,
5083                                  const struct ieee80211_tx_queue_params *params)
5084 {
5085         struct ath11k *ar = hw->priv;
5086         struct ath11k_vif *arvif = (void *)vif->drv_priv;
5087         struct wmi_wmm_params_arg *p = NULL;
5088         int ret;
5089
5090         mutex_lock(&ar->conf_mutex);
5091
5092         switch (ac) {
5093         case IEEE80211_AC_VO:
5094                 p = &arvif->wmm_params.ac_vo;
5095                 break;
5096         case IEEE80211_AC_VI:
5097                 p = &arvif->wmm_params.ac_vi;
5098                 break;
5099         case IEEE80211_AC_BE:
5100                 p = &arvif->wmm_params.ac_be;
5101                 break;
5102         case IEEE80211_AC_BK:
5103                 p = &arvif->wmm_params.ac_bk;
5104                 break;
5105         }
5106
5107         if (WARN_ON(!p)) {
5108                 ret = -EINVAL;
5109                 goto exit;
5110         }
5111
5112         p->cwmin = params->cw_min;
5113         p->cwmax = params->cw_max;
5114         p->aifs = params->aifs;
5115         p->txop = params->txop;
5116
5117         ret = ath11k_wmi_send_wmm_update_cmd_tlv(ar, arvif->vdev_id,
5118                                                  &arvif->wmm_params);
5119         if (ret) {
5120                 ath11k_warn(ar->ab, "failed to set wmm params: %d\n", ret);
5121                 goto exit;
5122         }
5123
5124         ret = ath11k_conf_tx_uapsd(ar, vif, ac, params->uapsd);
5125
5126         if (ret)
5127                 ath11k_warn(ar->ab, "failed to set sta uapsd: %d\n", ret);
5128
5129 exit:
5130         mutex_unlock(&ar->conf_mutex);
5131         return ret;
5132 }
5133
5134 static struct ieee80211_sta_ht_cap
5135 ath11k_create_ht_cap(struct ath11k *ar, u32 ar_ht_cap, u32 rate_cap_rx_chainmask)
5136 {
5137         int i;
5138         struct ieee80211_sta_ht_cap ht_cap = {0};
5139         u32 ar_vht_cap = ar->pdev->cap.vht_cap;
5140
5141         if (!(ar_ht_cap & WMI_HT_CAP_ENABLED))
5142                 return ht_cap;
5143
5144         ht_cap.ht_supported = 1;
5145         ht_cap.ampdu_factor = IEEE80211_HT_MAX_AMPDU_64K;
5146         ht_cap.ampdu_density = IEEE80211_HT_MPDU_DENSITY_NONE;
5147         ht_cap.cap |= IEEE80211_HT_CAP_SUP_WIDTH_20_40;
5148         ht_cap.cap |= IEEE80211_HT_CAP_DSSSCCK40;
5149         ht_cap.cap |= WLAN_HT_CAP_SM_PS_STATIC << IEEE80211_HT_CAP_SM_PS_SHIFT;
5150
5151         if (ar_ht_cap & WMI_HT_CAP_HT20_SGI)
5152                 ht_cap.cap |= IEEE80211_HT_CAP_SGI_20;
5153
5154         if (ar_ht_cap & WMI_HT_CAP_HT40_SGI)
5155                 ht_cap.cap |= IEEE80211_HT_CAP_SGI_40;
5156
5157         if (ar_ht_cap & WMI_HT_CAP_DYNAMIC_SMPS) {
5158                 u32 smps;
5159
5160                 smps   = WLAN_HT_CAP_SM_PS_DYNAMIC;
5161                 smps <<= IEEE80211_HT_CAP_SM_PS_SHIFT;
5162
5163                 ht_cap.cap |= smps;
5164         }
5165
5166         if (ar_ht_cap & WMI_HT_CAP_TX_STBC)
5167                 ht_cap.cap |= IEEE80211_HT_CAP_TX_STBC;
5168
5169         if (ar_ht_cap & WMI_HT_CAP_RX_STBC) {
5170                 u32 stbc;
5171
5172                 stbc   = ar_ht_cap;
5173                 stbc  &= WMI_HT_CAP_RX_STBC;
5174                 stbc >>= WMI_HT_CAP_RX_STBC_MASK_SHIFT;
5175                 stbc <<= IEEE80211_HT_CAP_RX_STBC_SHIFT;
5176                 stbc  &= IEEE80211_HT_CAP_RX_STBC;
5177
5178                 ht_cap.cap |= stbc;
5179         }
5180
5181         if (ar_ht_cap & WMI_HT_CAP_RX_LDPC)
5182                 ht_cap.cap |= IEEE80211_HT_CAP_LDPC_CODING;
5183
5184         if (ar_ht_cap & WMI_HT_CAP_L_SIG_TXOP_PROT)
5185                 ht_cap.cap |= IEEE80211_HT_CAP_LSIG_TXOP_PROT;
5186
5187         if (ar_vht_cap & WMI_VHT_CAP_MAX_MPDU_LEN_MASK)
5188                 ht_cap.cap |= IEEE80211_HT_CAP_MAX_AMSDU;
5189
5190         for (i = 0; i < ar->num_rx_chains; i++) {
5191                 if (rate_cap_rx_chainmask & BIT(i))
5192                         ht_cap.mcs.rx_mask[i] = 0xFF;
5193         }
5194
5195         ht_cap.mcs.tx_params |= IEEE80211_HT_MCS_TX_DEFINED;
5196
5197         return ht_cap;
5198 }
5199
5200 static int ath11k_mac_set_txbf_conf(struct ath11k_vif *arvif)
5201 {
5202         u32 value = 0;
5203         struct ath11k *ar = arvif->ar;
5204         int nsts;
5205         int sound_dim;
5206         u32 vht_cap = ar->pdev->cap.vht_cap;
5207         u32 vdev_param = WMI_VDEV_PARAM_TXBF;
5208
5209         if (vht_cap & (IEEE80211_VHT_CAP_SU_BEAMFORMEE_CAPABLE)) {
5210                 nsts = vht_cap & IEEE80211_VHT_CAP_BEAMFORMEE_STS_MASK;
5211                 nsts >>= IEEE80211_VHT_CAP_BEAMFORMEE_STS_SHIFT;
5212                 if (nsts > (ar->num_rx_chains - 1))
5213                         nsts = ar->num_rx_chains - 1;
5214                 value |= SM(nsts, WMI_TXBF_STS_CAP_OFFSET);
5215         }
5216
5217         if (vht_cap & (IEEE80211_VHT_CAP_SU_BEAMFORMER_CAPABLE)) {
5218                 sound_dim = vht_cap &
5219                             IEEE80211_VHT_CAP_SOUNDING_DIMENSIONS_MASK;
5220                 sound_dim >>= IEEE80211_VHT_CAP_SOUNDING_DIMENSIONS_SHIFT;
5221                 if (sound_dim > (ar->num_tx_chains - 1))
5222                         sound_dim = ar->num_tx_chains - 1;
5223                 value |= SM(sound_dim, WMI_BF_SOUND_DIM_OFFSET);
5224         }
5225
5226         if (!value)
5227                 return 0;
5228
5229         if (vht_cap & IEEE80211_VHT_CAP_SU_BEAMFORMER_CAPABLE) {
5230                 value |= WMI_VDEV_PARAM_TXBF_SU_TX_BFER;
5231
5232                 if ((vht_cap & IEEE80211_VHT_CAP_MU_BEAMFORMER_CAPABLE) &&
5233                     arvif->vdev_type == WMI_VDEV_TYPE_AP)
5234                         value |= WMI_VDEV_PARAM_TXBF_MU_TX_BFER;
5235         }
5236
5237         /* TODO: SUBFEE not validated in HK, disable here until validated? */
5238
5239         if (vht_cap & IEEE80211_VHT_CAP_SU_BEAMFORMEE_CAPABLE) {
5240                 value |= WMI_VDEV_PARAM_TXBF_SU_TX_BFEE;
5241
5242                 if ((vht_cap & IEEE80211_VHT_CAP_MU_BEAMFORMEE_CAPABLE) &&
5243                     arvif->vdev_type == WMI_VDEV_TYPE_STA)
5244                         value |= WMI_VDEV_PARAM_TXBF_MU_TX_BFEE;
5245         }
5246
5247         return ath11k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
5248                                              vdev_param, value);
5249 }
5250
5251 static void ath11k_set_vht_txbf_cap(struct ath11k *ar, u32 *vht_cap)
5252 {
5253         bool subfer, subfee;
5254         int sound_dim = 0, nsts = 0;
5255
5256         subfer = !!(*vht_cap & (IEEE80211_VHT_CAP_SU_BEAMFORMER_CAPABLE));
5257         subfee = !!(*vht_cap & (IEEE80211_VHT_CAP_SU_BEAMFORMEE_CAPABLE));
5258
5259         if (ar->num_tx_chains < 2) {
5260                 *vht_cap &= ~(IEEE80211_VHT_CAP_SU_BEAMFORMER_CAPABLE);
5261                 subfer = false;
5262         }
5263
5264         if (ar->num_rx_chains < 2) {
5265                 *vht_cap &= ~(IEEE80211_VHT_CAP_SU_BEAMFORMEE_CAPABLE);
5266                 subfee = false;
5267         }
5268
5269         /* If SU Beaformer is not set, then disable MU Beamformer Capability */
5270         if (!subfer)
5271                 *vht_cap &= ~(IEEE80211_VHT_CAP_MU_BEAMFORMER_CAPABLE);
5272
5273         /* If SU Beaformee is not set, then disable MU Beamformee Capability */
5274         if (!subfee)
5275                 *vht_cap &= ~(IEEE80211_VHT_CAP_MU_BEAMFORMEE_CAPABLE);
5276
5277         sound_dim = (*vht_cap & IEEE80211_VHT_CAP_SOUNDING_DIMENSIONS_MASK);
5278         sound_dim >>= IEEE80211_VHT_CAP_SOUNDING_DIMENSIONS_SHIFT;
5279         *vht_cap &= ~IEEE80211_VHT_CAP_SOUNDING_DIMENSIONS_MASK;
5280
5281         nsts = (*vht_cap & IEEE80211_VHT_CAP_BEAMFORMEE_STS_MASK);
5282         nsts >>= IEEE80211_VHT_CAP_BEAMFORMEE_STS_SHIFT;
5283         *vht_cap &= ~IEEE80211_VHT_CAP_BEAMFORMEE_STS_MASK;
5284
5285         /* Enable Sounding Dimension Field only if SU BF is enabled */
5286         if (subfer) {
5287                 if (sound_dim > (ar->num_tx_chains - 1))
5288                         sound_dim = ar->num_tx_chains - 1;
5289
5290                 sound_dim <<= IEEE80211_VHT_CAP_SOUNDING_DIMENSIONS_SHIFT;
5291                 sound_dim &=  IEEE80211_VHT_CAP_SOUNDING_DIMENSIONS_MASK;
5292                 *vht_cap |= sound_dim;
5293         }
5294
5295         /* Enable Beamformee STS Field only if SU BF is enabled */
5296         if (subfee) {
5297                 if (nsts > (ar->num_rx_chains - 1))
5298                         nsts = ar->num_rx_chains - 1;
5299
5300                 nsts <<= IEEE80211_VHT_CAP_BEAMFORMEE_STS_SHIFT;
5301                 nsts &=  IEEE80211_VHT_CAP_BEAMFORMEE_STS_MASK;
5302                 *vht_cap |= nsts;
5303         }
5304 }
5305
5306 static struct ieee80211_sta_vht_cap
5307 ath11k_create_vht_cap(struct ath11k *ar, u32 rate_cap_tx_chainmask,
5308                       u32 rate_cap_rx_chainmask)
5309 {
5310         struct ieee80211_sta_vht_cap vht_cap = {0};
5311         u16 txmcs_map, rxmcs_map;
5312         int i;
5313
5314         vht_cap.vht_supported = 1;
5315         vht_cap.cap = ar->pdev->cap.vht_cap;
5316
5317         if (ar->pdev->cap.nss_ratio_enabled)
5318                 vht_cap.vht_mcs.tx_highest |=
5319                         cpu_to_le16(IEEE80211_VHT_EXT_NSS_BW_CAPABLE);
5320
5321         ath11k_set_vht_txbf_cap(ar, &vht_cap.cap);
5322
5323         rxmcs_map = 0;
5324         txmcs_map = 0;
5325         for (i = 0; i < 8; i++) {
5326                 if (i < ar->num_tx_chains && rate_cap_tx_chainmask & BIT(i))
5327                         txmcs_map |= IEEE80211_VHT_MCS_SUPPORT_0_9 << (i * 2);
5328                 else
5329                         txmcs_map |= IEEE80211_VHT_MCS_NOT_SUPPORTED << (i * 2);
5330
5331                 if (i < ar->num_rx_chains && rate_cap_rx_chainmask & BIT(i))
5332                         rxmcs_map |= IEEE80211_VHT_MCS_SUPPORT_0_9 << (i * 2);
5333                 else
5334                         rxmcs_map |= IEEE80211_VHT_MCS_NOT_SUPPORTED << (i * 2);
5335         }
5336
5337         if (rate_cap_tx_chainmask <= 1)
5338                 vht_cap.cap &= ~IEEE80211_VHT_CAP_TXSTBC;
5339
5340         vht_cap.vht_mcs.rx_mcs_map = cpu_to_le16(rxmcs_map);
5341         vht_cap.vht_mcs.tx_mcs_map = cpu_to_le16(txmcs_map);
5342
5343         return vht_cap;
5344 }
5345
5346 static void ath11k_mac_setup_ht_vht_cap(struct ath11k *ar,
5347                                         struct ath11k_pdev_cap *cap,
5348                                         u32 *ht_cap_info)
5349 {
5350         struct ieee80211_supported_band *band;
5351         u32 rate_cap_tx_chainmask;
5352         u32 rate_cap_rx_chainmask;
5353         u32 ht_cap;
5354
5355         rate_cap_tx_chainmask = ar->cfg_tx_chainmask >> cap->tx_chain_mask_shift;
5356         rate_cap_rx_chainmask = ar->cfg_rx_chainmask >> cap->rx_chain_mask_shift;
5357
5358         if (cap->supported_bands & WMI_HOST_WLAN_2G_CAP) {
5359                 band = &ar->mac.sbands[NL80211_BAND_2GHZ];
5360                 ht_cap = cap->band[NL80211_BAND_2GHZ].ht_cap_info;
5361                 if (ht_cap_info)
5362                         *ht_cap_info = ht_cap;
5363                 band->ht_cap = ath11k_create_ht_cap(ar, ht_cap,
5364                                                     rate_cap_rx_chainmask);
5365         }
5366
5367         if (cap->supported_bands & WMI_HOST_WLAN_5G_CAP &&
5368             (ar->ab->hw_params.single_pdev_only ||
5369              !ar->supports_6ghz)) {
5370                 band = &ar->mac.sbands[NL80211_BAND_5GHZ];
5371                 ht_cap = cap->band[NL80211_BAND_5GHZ].ht_cap_info;
5372                 if (ht_cap_info)
5373                         *ht_cap_info = ht_cap;
5374                 band->ht_cap = ath11k_create_ht_cap(ar, ht_cap,
5375                                                     rate_cap_rx_chainmask);
5376                 band->vht_cap = ath11k_create_vht_cap(ar, rate_cap_tx_chainmask,
5377                                                       rate_cap_rx_chainmask);
5378         }
5379 }
5380
5381 static int ath11k_check_chain_mask(struct ath11k *ar, u32 ant, bool is_tx_ant)
5382 {
5383         /* TODO: Check the request chainmask against the supported
5384          * chainmask table which is advertised in extented_service_ready event
5385          */
5386
5387         return 0;
5388 }
5389
5390 static void ath11k_gen_ppe_thresh(struct ath11k_ppe_threshold *fw_ppet,
5391                                   u8 *he_ppet)
5392 {
5393         int nss, ru;
5394         u8 bit = 7;
5395
5396         he_ppet[0] = fw_ppet->numss_m1 & IEEE80211_PPE_THRES_NSS_MASK;
5397         he_ppet[0] |= (fw_ppet->ru_bit_mask <<
5398                        IEEE80211_PPE_THRES_RU_INDEX_BITMASK_POS) &
5399                       IEEE80211_PPE_THRES_RU_INDEX_BITMASK_MASK;
5400         for (nss = 0; nss <= fw_ppet->numss_m1; nss++) {
5401                 for (ru = 0; ru < 4; ru++) {
5402                         u8 val;
5403                         int i;
5404
5405                         if ((fw_ppet->ru_bit_mask & BIT(ru)) == 0)
5406                                 continue;
5407                         val = (fw_ppet->ppet16_ppet8_ru3_ru0[nss] >> (ru * 6)) &
5408                                0x3f;
5409                         val = ((val >> 3) & 0x7) | ((val & 0x7) << 3);
5410                         for (i = 5; i >= 0; i--) {
5411                                 he_ppet[bit / 8] |=
5412                                         ((val >> i) & 0x1) << ((bit % 8));
5413                                 bit++;
5414                         }
5415                 }
5416         }
5417 }
5418
5419 static void
5420 ath11k_mac_filter_he_cap_mesh(struct ieee80211_he_cap_elem *he_cap_elem)
5421 {
5422         u8 m;
5423
5424         m = IEEE80211_HE_MAC_CAP0_TWT_RES |
5425             IEEE80211_HE_MAC_CAP0_TWT_REQ;
5426         he_cap_elem->mac_cap_info[0] &= ~m;
5427
5428         m = IEEE80211_HE_MAC_CAP2_TRS |
5429             IEEE80211_HE_MAC_CAP2_BCAST_TWT |
5430             IEEE80211_HE_MAC_CAP2_MU_CASCADING;
5431         he_cap_elem->mac_cap_info[2] &= ~m;
5432
5433         m = IEEE80211_HE_MAC_CAP3_FLEX_TWT_SCHED |
5434             IEEE80211_HE_MAC_CAP2_BCAST_TWT |
5435             IEEE80211_HE_MAC_CAP2_MU_CASCADING;
5436         he_cap_elem->mac_cap_info[3] &= ~m;
5437
5438         m = IEEE80211_HE_MAC_CAP4_BSRP_BQRP_A_MPDU_AGG |
5439             IEEE80211_HE_MAC_CAP4_BQR;
5440         he_cap_elem->mac_cap_info[4] &= ~m;
5441
5442         m = IEEE80211_HE_MAC_CAP5_SUBCHAN_SELECTIVE_TRANSMISSION |
5443             IEEE80211_HE_MAC_CAP5_UL_2x996_TONE_RU |
5444             IEEE80211_HE_MAC_CAP5_PUNCTURED_SOUNDING |
5445             IEEE80211_HE_MAC_CAP5_HT_VHT_TRIG_FRAME_RX;
5446         he_cap_elem->mac_cap_info[5] &= ~m;
5447
5448         m = IEEE80211_HE_PHY_CAP2_UL_MU_FULL_MU_MIMO |
5449             IEEE80211_HE_PHY_CAP2_UL_MU_PARTIAL_MU_MIMO;
5450         he_cap_elem->phy_cap_info[2] &= ~m;
5451
5452         m = IEEE80211_HE_PHY_CAP3_RX_PARTIAL_BW_SU_IN_20MHZ_MU |
5453             IEEE80211_HE_PHY_CAP3_DCM_MAX_CONST_TX_MASK |
5454             IEEE80211_HE_PHY_CAP3_DCM_MAX_CONST_RX_MASK;
5455         he_cap_elem->phy_cap_info[3] &= ~m;
5456
5457         m = IEEE80211_HE_PHY_CAP4_MU_BEAMFORMER;
5458         he_cap_elem->phy_cap_info[4] &= ~m;
5459
5460         m = IEEE80211_HE_PHY_CAP5_NG16_MU_FEEDBACK;
5461         he_cap_elem->phy_cap_info[5] &= ~m;
5462
5463         m = IEEE80211_HE_PHY_CAP6_CODEBOOK_SIZE_75_MU |
5464             IEEE80211_HE_PHY_CAP6_TRIG_MU_BEAMFORMING_PARTIAL_BW_FB |
5465             IEEE80211_HE_PHY_CAP6_TRIG_CQI_FB |
5466             IEEE80211_HE_PHY_CAP6_PARTIAL_BANDWIDTH_DL_MUMIMO;
5467         he_cap_elem->phy_cap_info[6] &= ~m;
5468
5469         m = IEEE80211_HE_PHY_CAP7_PSR_BASED_SR |
5470             IEEE80211_HE_PHY_CAP7_POWER_BOOST_FACTOR_SUPP |
5471             IEEE80211_HE_PHY_CAP7_STBC_TX_ABOVE_80MHZ |
5472             IEEE80211_HE_PHY_CAP7_STBC_RX_ABOVE_80MHZ;
5473         he_cap_elem->phy_cap_info[7] &= ~m;
5474
5475         m = IEEE80211_HE_PHY_CAP8_HE_ER_SU_PPDU_4XLTF_AND_08_US_GI |
5476             IEEE80211_HE_PHY_CAP8_20MHZ_IN_40MHZ_HE_PPDU_IN_2G |
5477             IEEE80211_HE_PHY_CAP8_20MHZ_IN_160MHZ_HE_PPDU |
5478             IEEE80211_HE_PHY_CAP8_80MHZ_IN_160MHZ_HE_PPDU;
5479         he_cap_elem->phy_cap_info[8] &= ~m;
5480
5481         m = IEEE80211_HE_PHY_CAP9_LONGER_THAN_16_SIGB_OFDM_SYM |
5482             IEEE80211_HE_PHY_CAP9_NON_TRIGGERED_CQI_FEEDBACK |
5483             IEEE80211_HE_PHY_CAP9_RX_1024_QAM_LESS_THAN_242_TONE_RU |
5484             IEEE80211_HE_PHY_CAP9_TX_1024_QAM_LESS_THAN_242_TONE_RU |
5485             IEEE80211_HE_PHY_CAP9_RX_FULL_BW_SU_USING_MU_WITH_COMP_SIGB |
5486             IEEE80211_HE_PHY_CAP9_RX_FULL_BW_SU_USING_MU_WITH_NON_COMP_SIGB;
5487         he_cap_elem->phy_cap_info[9] &= ~m;
5488 }
5489
5490 static __le16 ath11k_mac_setup_he_6ghz_cap(struct ath11k_pdev_cap *pcap,
5491                                            struct ath11k_band_cap *bcap)
5492 {
5493         u8 val;
5494
5495         bcap->he_6ghz_capa = IEEE80211_HT_MPDU_DENSITY_NONE;
5496         if (bcap->ht_cap_info & WMI_HT_CAP_DYNAMIC_SMPS)
5497                 bcap->he_6ghz_capa |=
5498                         FIELD_PREP(IEEE80211_HE_6GHZ_CAP_SM_PS,
5499                                    WLAN_HT_CAP_SM_PS_DYNAMIC);
5500         else
5501                 bcap->he_6ghz_capa |=
5502                         FIELD_PREP(IEEE80211_HE_6GHZ_CAP_SM_PS,
5503                                    WLAN_HT_CAP_SM_PS_DISABLED);
5504         val = FIELD_GET(IEEE80211_VHT_CAP_MAX_A_MPDU_LENGTH_EXPONENT_MASK,
5505                         pcap->vht_cap);
5506         bcap->he_6ghz_capa |=
5507                 FIELD_PREP(IEEE80211_HE_6GHZ_CAP_MAX_AMPDU_LEN_EXP, val);
5508         val = FIELD_GET(IEEE80211_VHT_CAP_MAX_MPDU_MASK, pcap->vht_cap);
5509         bcap->he_6ghz_capa |=
5510                 FIELD_PREP(IEEE80211_HE_6GHZ_CAP_MAX_MPDU_LEN, val);
5511         if (pcap->vht_cap & IEEE80211_VHT_CAP_RX_ANTENNA_PATTERN)
5512                 bcap->he_6ghz_capa |= IEEE80211_HE_6GHZ_CAP_RX_ANTPAT_CONS;
5513         if (pcap->vht_cap & IEEE80211_VHT_CAP_TX_ANTENNA_PATTERN)
5514                 bcap->he_6ghz_capa |= IEEE80211_HE_6GHZ_CAP_TX_ANTPAT_CONS;
5515
5516         return cpu_to_le16(bcap->he_6ghz_capa);
5517 }
5518
5519 static void ath11k_mac_set_hemcsmap(struct ath11k *ar,
5520                                     struct ath11k_pdev_cap *cap,
5521                                     struct ieee80211_sta_he_cap *he_cap,
5522                                     int band)
5523 {
5524         u16 txmcs_map, rxmcs_map;
5525         u32 i;
5526
5527         rxmcs_map = 0;
5528         txmcs_map = 0;
5529         for (i = 0; i < 8; i++) {
5530                 if (i < ar->num_tx_chains &&
5531                     (ar->cfg_tx_chainmask >> cap->tx_chain_mask_shift) & BIT(i))
5532                         txmcs_map |= IEEE80211_HE_MCS_SUPPORT_0_11 << (i * 2);
5533                 else
5534                         txmcs_map |= IEEE80211_HE_MCS_NOT_SUPPORTED << (i * 2);
5535
5536                 if (i < ar->num_rx_chains &&
5537                     (ar->cfg_rx_chainmask >> cap->tx_chain_mask_shift) & BIT(i))
5538                         rxmcs_map |= IEEE80211_HE_MCS_SUPPORT_0_11 << (i * 2);
5539                 else
5540                         rxmcs_map |= IEEE80211_HE_MCS_NOT_SUPPORTED << (i * 2);
5541         }
5542         he_cap->he_mcs_nss_supp.rx_mcs_80 =
5543                 cpu_to_le16(rxmcs_map & 0xffff);
5544         he_cap->he_mcs_nss_supp.tx_mcs_80 =
5545                 cpu_to_le16(txmcs_map & 0xffff);
5546         he_cap->he_mcs_nss_supp.rx_mcs_160 =
5547                 cpu_to_le16(rxmcs_map & 0xffff);
5548         he_cap->he_mcs_nss_supp.tx_mcs_160 =
5549                 cpu_to_le16(txmcs_map & 0xffff);
5550         he_cap->he_mcs_nss_supp.rx_mcs_80p80 =
5551                 cpu_to_le16(rxmcs_map & 0xffff);
5552         he_cap->he_mcs_nss_supp.tx_mcs_80p80 =
5553                 cpu_to_le16(txmcs_map & 0xffff);
5554 }
5555
5556 static int ath11k_mac_copy_he_cap(struct ath11k *ar,
5557                                   struct ath11k_pdev_cap *cap,
5558                                   struct ieee80211_sband_iftype_data *data,
5559                                   int band)
5560 {
5561         int i, idx = 0;
5562
5563         for (i = 0; i < NUM_NL80211_IFTYPES; i++) {
5564                 struct ieee80211_sta_he_cap *he_cap = &data[idx].he_cap;
5565                 struct ath11k_band_cap *band_cap = &cap->band[band];
5566                 struct ieee80211_he_cap_elem *he_cap_elem =
5567                                 &he_cap->he_cap_elem;
5568
5569                 switch (i) {
5570                 case NL80211_IFTYPE_STATION:
5571                 case NL80211_IFTYPE_AP:
5572                 case NL80211_IFTYPE_MESH_POINT:
5573                         break;
5574
5575                 default:
5576                         continue;
5577                 }
5578
5579                 data[idx].types_mask = BIT(i);
5580                 he_cap->has_he = true;
5581                 memcpy(he_cap_elem->mac_cap_info, band_cap->he_cap_info,
5582                        sizeof(he_cap_elem->mac_cap_info));
5583                 memcpy(he_cap_elem->phy_cap_info, band_cap->he_cap_phy_info,
5584                        sizeof(he_cap_elem->phy_cap_info));
5585
5586                 he_cap_elem->mac_cap_info[1] &=
5587                         IEEE80211_HE_MAC_CAP1_TF_MAC_PAD_DUR_MASK;
5588
5589                 he_cap_elem->phy_cap_info[5] &=
5590                         ~IEEE80211_HE_PHY_CAP5_BEAMFORMEE_NUM_SND_DIM_UNDER_80MHZ_MASK;
5591                 he_cap_elem->phy_cap_info[5] |= ar->num_tx_chains - 1;
5592
5593                 switch (i) {
5594                 case NL80211_IFTYPE_AP:
5595                         he_cap_elem->phy_cap_info[3] &=
5596                                 ~IEEE80211_HE_PHY_CAP3_DCM_MAX_CONST_TX_MASK;
5597                         he_cap_elem->phy_cap_info[9] |=
5598                                 IEEE80211_HE_PHY_CAP9_RX_1024_QAM_LESS_THAN_242_TONE_RU;
5599                         break;
5600                 case NL80211_IFTYPE_STATION:
5601                         he_cap_elem->mac_cap_info[0] &=
5602                                 ~IEEE80211_HE_MAC_CAP0_TWT_RES;
5603                         he_cap_elem->mac_cap_info[0] |=
5604                                 IEEE80211_HE_MAC_CAP0_TWT_REQ;
5605                         he_cap_elem->phy_cap_info[9] |=
5606                                 IEEE80211_HE_PHY_CAP9_TX_1024_QAM_LESS_THAN_242_TONE_RU;
5607                         break;
5608                 case NL80211_IFTYPE_MESH_POINT:
5609                         ath11k_mac_filter_he_cap_mesh(he_cap_elem);
5610                         break;
5611                 }
5612
5613                 ath11k_mac_set_hemcsmap(ar, cap, he_cap, band);
5614
5615                 memset(he_cap->ppe_thres, 0, sizeof(he_cap->ppe_thres));
5616                 if (he_cap_elem->phy_cap_info[6] &
5617                     IEEE80211_HE_PHY_CAP6_PPE_THRESHOLD_PRESENT)
5618                         ath11k_gen_ppe_thresh(&band_cap->he_ppet,
5619                                               he_cap->ppe_thres);
5620
5621                 if (band == NL80211_BAND_6GHZ) {
5622                         data[idx].he_6ghz_capa.capa =
5623                                 ath11k_mac_setup_he_6ghz_cap(cap, band_cap);
5624                 }
5625                 idx++;
5626         }
5627
5628         return idx;
5629 }
5630
5631 static void ath11k_mac_setup_he_cap(struct ath11k *ar,
5632                                     struct ath11k_pdev_cap *cap)
5633 {
5634         struct ieee80211_supported_band *band;
5635         int count;
5636
5637         if (cap->supported_bands & WMI_HOST_WLAN_2G_CAP) {
5638                 count = ath11k_mac_copy_he_cap(ar, cap,
5639                                                ar->mac.iftype[NL80211_BAND_2GHZ],
5640                                                NL80211_BAND_2GHZ);
5641                 band = &ar->mac.sbands[NL80211_BAND_2GHZ];
5642                 band->iftype_data = ar->mac.iftype[NL80211_BAND_2GHZ];
5643                 band->n_iftype_data = count;
5644         }
5645
5646         if (cap->supported_bands & WMI_HOST_WLAN_5G_CAP) {
5647                 count = ath11k_mac_copy_he_cap(ar, cap,
5648                                                ar->mac.iftype[NL80211_BAND_5GHZ],
5649                                                NL80211_BAND_5GHZ);
5650                 band = &ar->mac.sbands[NL80211_BAND_5GHZ];
5651                 band->iftype_data = ar->mac.iftype[NL80211_BAND_5GHZ];
5652                 band->n_iftype_data = count;
5653         }
5654
5655         if (cap->supported_bands & WMI_HOST_WLAN_5G_CAP &&
5656             ar->supports_6ghz) {
5657                 count = ath11k_mac_copy_he_cap(ar, cap,
5658                                                ar->mac.iftype[NL80211_BAND_6GHZ],
5659                                                NL80211_BAND_6GHZ);
5660                 band = &ar->mac.sbands[NL80211_BAND_6GHZ];
5661                 band->iftype_data = ar->mac.iftype[NL80211_BAND_6GHZ];
5662                 band->n_iftype_data = count;
5663         }
5664 }
5665
5666 static int __ath11k_set_antenna(struct ath11k *ar, u32 tx_ant, u32 rx_ant)
5667 {
5668         int ret;
5669
5670         lockdep_assert_held(&ar->conf_mutex);
5671
5672         if (ath11k_check_chain_mask(ar, tx_ant, true))
5673                 return -EINVAL;
5674
5675         if (ath11k_check_chain_mask(ar, rx_ant, false))
5676                 return -EINVAL;
5677
5678         ar->cfg_tx_chainmask = tx_ant;
5679         ar->cfg_rx_chainmask = rx_ant;
5680
5681         if (ar->state != ATH11K_STATE_ON &&
5682             ar->state != ATH11K_STATE_RESTARTED)
5683                 return 0;
5684
5685         ret = ath11k_wmi_pdev_set_param(ar, WMI_PDEV_PARAM_TX_CHAIN_MASK,
5686                                         tx_ant, ar->pdev->pdev_id);
5687         if (ret) {
5688                 ath11k_warn(ar->ab, "failed to set tx-chainmask: %d, req 0x%x\n",
5689                             ret, tx_ant);
5690                 return ret;
5691         }
5692
5693         ar->num_tx_chains = get_num_chains(tx_ant);
5694
5695         ret = ath11k_wmi_pdev_set_param(ar, WMI_PDEV_PARAM_RX_CHAIN_MASK,
5696                                         rx_ant, ar->pdev->pdev_id);
5697         if (ret) {
5698                 ath11k_warn(ar->ab, "failed to set rx-chainmask: %d, req 0x%x\n",
5699                             ret, rx_ant);
5700                 return ret;
5701         }
5702
5703         ar->num_rx_chains = get_num_chains(rx_ant);
5704
5705         /* Reload HT/VHT/HE capability */
5706         ath11k_mac_setup_ht_vht_cap(ar, &ar->pdev->cap, NULL);
5707         ath11k_mac_setup_he_cap(ar, &ar->pdev->cap);
5708
5709         return 0;
5710 }
5711
5712 static void ath11k_mgmt_over_wmi_tx_drop(struct ath11k *ar, struct sk_buff *skb)
5713 {
5714         int num_mgmt;
5715
5716         ieee80211_free_txskb(ar->hw, skb);
5717
5718         num_mgmt = atomic_dec_if_positive(&ar->num_pending_mgmt_tx);
5719
5720         if (num_mgmt < 0)
5721                 WARN_ON_ONCE(1);
5722
5723         if (!num_mgmt)
5724                 wake_up(&ar->txmgmt_empty_waitq);
5725 }
5726
5727 static void ath11k_mac_tx_mgmt_free(struct ath11k *ar, int buf_id)
5728 {
5729         struct sk_buff *msdu;
5730         struct ieee80211_tx_info *info;
5731
5732         spin_lock_bh(&ar->txmgmt_idr_lock);
5733         msdu = idr_remove(&ar->txmgmt_idr, buf_id);
5734         spin_unlock_bh(&ar->txmgmt_idr_lock);
5735
5736         if (!msdu)
5737                 return;
5738
5739         dma_unmap_single(ar->ab->dev, ATH11K_SKB_CB(msdu)->paddr, msdu->len,
5740                          DMA_TO_DEVICE);
5741
5742         info = IEEE80211_SKB_CB(msdu);
5743         memset(&info->status, 0, sizeof(info->status));
5744
5745         ath11k_mgmt_over_wmi_tx_drop(ar, msdu);
5746 }
5747
5748 int ath11k_mac_tx_mgmt_pending_free(int buf_id, void *skb, void *ctx)
5749 {
5750         struct ath11k *ar = ctx;
5751
5752         ath11k_mac_tx_mgmt_free(ar, buf_id);
5753
5754         return 0;
5755 }
5756
5757 static int ath11k_mac_vif_txmgmt_idr_remove(int buf_id, void *skb, void *ctx)
5758 {
5759         struct ieee80211_vif *vif = ctx;
5760         struct ath11k_skb_cb *skb_cb = ATH11K_SKB_CB((struct sk_buff *)skb);
5761         struct ath11k *ar = skb_cb->ar;
5762
5763         if (skb_cb->vif == vif)
5764                 ath11k_mac_tx_mgmt_free(ar, buf_id);
5765
5766         return 0;
5767 }
5768
5769 static int ath11k_mac_mgmt_tx_wmi(struct ath11k *ar, struct ath11k_vif *arvif,
5770                                   struct sk_buff *skb)
5771 {
5772         struct ath11k_base *ab = ar->ab;
5773         struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
5774         struct ieee80211_tx_info *info;
5775         dma_addr_t paddr;
5776         int buf_id;
5777         int ret;
5778
5779         ATH11K_SKB_CB(skb)->ar = ar;
5780
5781         spin_lock_bh(&ar->txmgmt_idr_lock);
5782         buf_id = idr_alloc(&ar->txmgmt_idr, skb, 0,
5783                            ATH11K_TX_MGMT_NUM_PENDING_MAX, GFP_ATOMIC);
5784         spin_unlock_bh(&ar->txmgmt_idr_lock);
5785
5786         ath11k_dbg(ar->ab, ATH11K_DBG_MAC,
5787                    "mac tx mgmt frame, buf id %d\n", buf_id);
5788
5789         if (buf_id < 0)
5790                 return -ENOSPC;
5791
5792         info = IEEE80211_SKB_CB(skb);
5793         if (!(info->flags & IEEE80211_TX_CTL_HW_80211_ENCAP)) {
5794                 if ((ieee80211_is_action(hdr->frame_control) ||
5795                      ieee80211_is_deauth(hdr->frame_control) ||
5796                      ieee80211_is_disassoc(hdr->frame_control)) &&
5797                      ieee80211_has_protected(hdr->frame_control)) {
5798                         skb_put(skb, IEEE80211_CCMP_MIC_LEN);
5799                 }
5800         }
5801
5802         paddr = dma_map_single(ab->dev, skb->data, skb->len, DMA_TO_DEVICE);
5803         if (dma_mapping_error(ab->dev, paddr)) {
5804                 ath11k_warn(ab, "failed to DMA map mgmt Tx buffer\n");
5805                 ret = -EIO;
5806                 goto err_free_idr;
5807         }
5808
5809         ATH11K_SKB_CB(skb)->paddr = paddr;
5810
5811         ret = ath11k_wmi_mgmt_send(ar, arvif->vdev_id, buf_id, skb);
5812         if (ret) {
5813                 ath11k_warn(ar->ab, "failed to send mgmt frame: %d\n", ret);
5814                 goto err_unmap_buf;
5815         }
5816
5817         return 0;
5818
5819 err_unmap_buf:
5820         dma_unmap_single(ab->dev, ATH11K_SKB_CB(skb)->paddr,
5821                          skb->len, DMA_TO_DEVICE);
5822 err_free_idr:
5823         spin_lock_bh(&ar->txmgmt_idr_lock);
5824         idr_remove(&ar->txmgmt_idr, buf_id);
5825         spin_unlock_bh(&ar->txmgmt_idr_lock);
5826
5827         return ret;
5828 }
5829
5830 static void ath11k_mgmt_over_wmi_tx_purge(struct ath11k *ar)
5831 {
5832         struct sk_buff *skb;
5833
5834         while ((skb = skb_dequeue(&ar->wmi_mgmt_tx_queue)) != NULL)
5835                 ath11k_mgmt_over_wmi_tx_drop(ar, skb);
5836 }
5837
5838 static void ath11k_mgmt_over_wmi_tx_work(struct work_struct *work)
5839 {
5840         struct ath11k *ar = container_of(work, struct ath11k, wmi_mgmt_tx_work);
5841         struct ath11k_skb_cb *skb_cb;
5842         struct ath11k_vif *arvif;
5843         struct sk_buff *skb;
5844         int ret;
5845
5846         while ((skb = skb_dequeue(&ar->wmi_mgmt_tx_queue)) != NULL) {
5847                 skb_cb = ATH11K_SKB_CB(skb);
5848                 if (!skb_cb->vif) {
5849                         ath11k_warn(ar->ab, "no vif found for mgmt frame\n");
5850                         ath11k_mgmt_over_wmi_tx_drop(ar, skb);
5851                         continue;
5852                 }
5853
5854                 arvif = ath11k_vif_to_arvif(skb_cb->vif);
5855                 mutex_lock(&ar->conf_mutex);
5856                 if (ar->allocated_vdev_map & (1LL << arvif->vdev_id)) {
5857                         ret = ath11k_mac_mgmt_tx_wmi(ar, arvif, skb);
5858                         if (ret) {
5859                                 ath11k_warn(ar->ab, "failed to tx mgmt frame, vdev_id %d :%d\n",
5860                                             arvif->vdev_id, ret);
5861                                 ath11k_mgmt_over_wmi_tx_drop(ar, skb);
5862                         } else {
5863                                 ath11k_dbg(ar->ab, ATH11K_DBG_MAC,
5864                                            "mac tx mgmt frame, vdev_id %d\n",
5865                                            arvif->vdev_id);
5866                         }
5867                 } else {
5868                         ath11k_warn(ar->ab,
5869                                     "dropping mgmt frame for vdev %d, is_started %d\n",
5870                                     arvif->vdev_id,
5871                                     arvif->is_started);
5872                         ath11k_mgmt_over_wmi_tx_drop(ar, skb);
5873                 }
5874                 mutex_unlock(&ar->conf_mutex);
5875         }
5876 }
5877
5878 static int ath11k_mac_mgmt_tx(struct ath11k *ar, struct sk_buff *skb,
5879                               bool is_prb_rsp)
5880 {
5881         struct sk_buff_head *q = &ar->wmi_mgmt_tx_queue;
5882
5883         if (test_bit(ATH11K_FLAG_CRASH_FLUSH, &ar->ab->dev_flags))
5884                 return -ESHUTDOWN;
5885
5886         /* Drop probe response packets when the pending management tx
5887          * count has reached a certain threshold, so as to prioritize
5888          * other mgmt packets like auth and assoc to be sent on time
5889          * for establishing successful connections.
5890          */
5891         if (is_prb_rsp &&
5892             atomic_read(&ar->num_pending_mgmt_tx) > ATH11K_PRB_RSP_DROP_THRESHOLD) {
5893                 ath11k_warn(ar->ab,
5894                             "dropping probe response as pending queue is almost full\n");
5895                 return -ENOSPC;
5896         }
5897
5898         if (skb_queue_len_lockless(q) >= ATH11K_TX_MGMT_NUM_PENDING_MAX) {
5899                 ath11k_warn(ar->ab, "mgmt tx queue is full\n");
5900                 return -ENOSPC;
5901         }
5902
5903         skb_queue_tail(q, skb);
5904         atomic_inc(&ar->num_pending_mgmt_tx);
5905         queue_work(ar->ab->workqueue_aux, &ar->wmi_mgmt_tx_work);
5906
5907         return 0;
5908 }
5909
5910 static void ath11k_mac_op_tx(struct ieee80211_hw *hw,
5911                              struct ieee80211_tx_control *control,
5912                              struct sk_buff *skb)
5913 {
5914         struct ath11k_skb_cb *skb_cb = ATH11K_SKB_CB(skb);
5915         struct ath11k *ar = hw->priv;
5916         struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
5917         struct ieee80211_vif *vif = info->control.vif;
5918         struct ath11k_vif *arvif = ath11k_vif_to_arvif(vif);
5919         struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
5920         struct ieee80211_key_conf *key = info->control.hw_key;
5921         struct ath11k_sta *arsta = NULL;
5922         u32 info_flags = info->flags;
5923         bool is_prb_rsp;
5924         int ret;
5925
5926         memset(skb_cb, 0, sizeof(*skb_cb));
5927         skb_cb->vif = vif;
5928
5929         if (key) {
5930                 skb_cb->cipher = key->cipher;
5931                 skb_cb->flags |= ATH11K_SKB_CIPHER_SET;
5932         }
5933
5934         if (info_flags & IEEE80211_TX_CTL_HW_80211_ENCAP) {
5935                 skb_cb->flags |= ATH11K_SKB_HW_80211_ENCAP;
5936         } else if (ieee80211_is_mgmt(hdr->frame_control)) {
5937                 is_prb_rsp = ieee80211_is_probe_resp(hdr->frame_control);
5938                 ret = ath11k_mac_mgmt_tx(ar, skb, is_prb_rsp);
5939                 if (ret) {
5940                         ath11k_warn(ar->ab, "failed to queue management frame %d\n",
5941                                     ret);
5942                         ieee80211_free_txskb(ar->hw, skb);
5943                 }
5944                 return;
5945         }
5946
5947         if (control->sta)
5948                 arsta = (struct ath11k_sta *)control->sta->drv_priv;
5949
5950         ret = ath11k_dp_tx(ar, arvif, arsta, skb);
5951         if (unlikely(ret)) {
5952                 ath11k_warn(ar->ab, "failed to transmit frame %d\n", ret);
5953                 ieee80211_free_txskb(ar->hw, skb);
5954         }
5955 }
5956
5957 void ath11k_mac_drain_tx(struct ath11k *ar)
5958 {
5959         /* make sure rcu-protected mac80211 tx path itself is drained */
5960         synchronize_net();
5961
5962         cancel_work_sync(&ar->wmi_mgmt_tx_work);
5963         ath11k_mgmt_over_wmi_tx_purge(ar);
5964 }
5965
5966 static int ath11k_mac_config_mon_status_default(struct ath11k *ar, bool enable)
5967 {
5968         struct htt_rx_ring_tlv_filter tlv_filter = {0};
5969         struct ath11k_base *ab = ar->ab;
5970         int i, ret = 0;
5971         u32 ring_id;
5972
5973         if (enable) {
5974                 tlv_filter = ath11k_mac_mon_status_filter_default;
5975                 if (ath11k_debugfs_rx_filter(ar))
5976                         tlv_filter.rx_filter = ath11k_debugfs_rx_filter(ar);
5977         }
5978
5979         for (i = 0; i < ab->hw_params.num_rxmda_per_pdev; i++) {
5980                 ring_id = ar->dp.rx_mon_status_refill_ring[i].refill_buf_ring.ring_id;
5981                 ret = ath11k_dp_tx_htt_rx_filter_setup(ar->ab, ring_id,
5982                                                        ar->dp.mac_id + i,
5983                                                        HAL_RXDMA_MONITOR_STATUS,
5984                                                        DP_RX_BUFFER_SIZE,
5985                                                        &tlv_filter);
5986         }
5987
5988         if (enable && !ar->ab->hw_params.rxdma1_enable)
5989                 mod_timer(&ar->ab->mon_reap_timer, jiffies +
5990                           msecs_to_jiffies(ATH11K_MON_TIMER_INTERVAL));
5991
5992         return ret;
5993 }
5994
5995 static void ath11k_mac_wait_reconfigure(struct ath11k_base *ab)
5996 {
5997         int recovery_start_count;
5998
5999         if (!ab->is_reset)
6000                 return;
6001
6002         recovery_start_count = atomic_inc_return(&ab->recovery_start_count);
6003         ath11k_dbg(ab, ATH11K_DBG_MAC, "recovery start count %d\n", recovery_start_count);
6004
6005         if (recovery_start_count == ab->num_radios) {
6006                 complete(&ab->recovery_start);
6007                 ath11k_dbg(ab, ATH11K_DBG_MAC, "recovery started success\n");
6008         }
6009
6010         ath11k_dbg(ab, ATH11K_DBG_MAC, "waiting reconfigure...\n");
6011
6012         wait_for_completion_timeout(&ab->reconfigure_complete,
6013                                     ATH11K_RECONFIGURE_TIMEOUT_HZ);
6014 }
6015
6016 static int ath11k_mac_op_start(struct ieee80211_hw *hw)
6017 {
6018         struct ath11k *ar = hw->priv;
6019         struct ath11k_base *ab = ar->ab;
6020         struct ath11k_pdev *pdev = ar->pdev;
6021         int ret;
6022
6023         ath11k_mac_drain_tx(ar);
6024         mutex_lock(&ar->conf_mutex);
6025
6026         switch (ar->state) {
6027         case ATH11K_STATE_OFF:
6028                 ar->state = ATH11K_STATE_ON;
6029                 break;
6030         case ATH11K_STATE_RESTARTING:
6031                 ar->state = ATH11K_STATE_RESTARTED;
6032                 ath11k_mac_wait_reconfigure(ab);
6033                 break;
6034         case ATH11K_STATE_RESTARTED:
6035         case ATH11K_STATE_WEDGED:
6036         case ATH11K_STATE_ON:
6037                 WARN_ON(1);
6038                 ret = -EINVAL;
6039                 goto err;
6040         }
6041
6042         ret = ath11k_wmi_pdev_set_param(ar, WMI_PDEV_PARAM_PMF_QOS,
6043                                         1, pdev->pdev_id);
6044
6045         if (ret) {
6046                 ath11k_err(ar->ab, "failed to enable PMF QOS: (%d\n", ret);
6047                 goto err;
6048         }
6049
6050         ret = ath11k_wmi_pdev_set_param(ar, WMI_PDEV_PARAM_DYNAMIC_BW, 1,
6051                                         pdev->pdev_id);
6052         if (ret) {
6053                 ath11k_err(ar->ab, "failed to enable dynamic bw: %d\n", ret);
6054                 goto err;
6055         }
6056
6057         if (test_bit(WMI_TLV_SERVICE_SPOOF_MAC_SUPPORT, ar->wmi->wmi_ab->svc_map)) {
6058                 ret = ath11k_wmi_scan_prob_req_oui(ar, ar->mac_addr);
6059                 if (ret) {
6060                         ath11k_err(ab, "failed to set prob req oui: %i\n", ret);
6061                         goto err;
6062                 }
6063         }
6064
6065         ret = ath11k_wmi_pdev_set_param(ar, WMI_PDEV_PARAM_ARP_AC_OVERRIDE,
6066                                         0, pdev->pdev_id);
6067         if (ret) {
6068                 ath11k_err(ab, "failed to set ac override for ARP: %d\n",
6069                            ret);
6070                 goto err;
6071         }
6072
6073         ret = ath11k_wmi_send_dfs_phyerr_offload_enable_cmd(ar, pdev->pdev_id);
6074         if (ret) {
6075                 ath11k_err(ab, "failed to offload radar detection: %d\n",
6076                            ret);
6077                 goto err;
6078         }
6079
6080         ret = ath11k_dp_tx_htt_h2t_ppdu_stats_req(ar,
6081                                                   HTT_PPDU_STATS_TAG_DEFAULT);
6082         if (ret) {
6083                 ath11k_err(ab, "failed to req ppdu stats: %d\n", ret);
6084                 goto err;
6085         }
6086
6087         ret = ath11k_wmi_pdev_set_param(ar, WMI_PDEV_PARAM_MESH_MCAST_ENABLE,
6088                                         1, pdev->pdev_id);
6089
6090         if (ret) {
6091                 ath11k_err(ar->ab, "failed to enable MESH MCAST ENABLE: (%d\n", ret);
6092                 goto err;
6093         }
6094
6095         __ath11k_set_antenna(ar, ar->cfg_tx_chainmask, ar->cfg_rx_chainmask);
6096
6097         /* TODO: Do we need to enable ANI? */
6098
6099         ath11k_reg_update_chan_list(ar, false);
6100
6101         ar->num_started_vdevs = 0;
6102         ar->num_created_vdevs = 0;
6103         ar->num_peers = 0;
6104         ar->allocated_vdev_map = 0;
6105
6106         /* Configure monitor status ring with default rx_filter to get rx status
6107          * such as rssi, rx_duration.
6108          */
6109         ret = ath11k_mac_config_mon_status_default(ar, true);
6110         if (ret) {
6111                 ath11k_err(ab, "failed to configure monitor status ring with default rx_filter: (%d)\n",
6112                            ret);
6113                 goto err;
6114         }
6115
6116         /* Configure the hash seed for hash based reo dest ring selection */
6117         ath11k_wmi_pdev_lro_cfg(ar, ar->pdev->pdev_id);
6118
6119         /* allow device to enter IMPS */
6120         if (ab->hw_params.idle_ps) {
6121                 ret = ath11k_wmi_pdev_set_param(ar, WMI_PDEV_PARAM_IDLE_PS_CONFIG,
6122                                                 1, pdev->pdev_id);
6123                 if (ret) {
6124                         ath11k_err(ab, "failed to enable idle ps: %d\n", ret);
6125                         goto err;
6126                 }
6127         }
6128
6129         mutex_unlock(&ar->conf_mutex);
6130
6131         rcu_assign_pointer(ab->pdevs_active[ar->pdev_idx],
6132                            &ab->pdevs[ar->pdev_idx]);
6133
6134         return 0;
6135
6136 err:
6137         ar->state = ATH11K_STATE_OFF;
6138         mutex_unlock(&ar->conf_mutex);
6139
6140         return ret;
6141 }
6142
6143 static void ath11k_mac_op_stop(struct ieee80211_hw *hw)
6144 {
6145         struct ath11k *ar = hw->priv;
6146         struct htt_ppdu_stats_info *ppdu_stats, *tmp;
6147         int ret;
6148
6149         ath11k_mac_drain_tx(ar);
6150
6151         mutex_lock(&ar->conf_mutex);
6152         ret = ath11k_mac_config_mon_status_default(ar, false);
6153         if (ret)
6154                 ath11k_err(ar->ab, "failed to clear rx_filter for monitor status ring: (%d)\n",
6155                            ret);
6156
6157         clear_bit(ATH11K_CAC_RUNNING, &ar->dev_flags);
6158         ar->state = ATH11K_STATE_OFF;
6159         mutex_unlock(&ar->conf_mutex);
6160
6161         cancel_delayed_work_sync(&ar->scan.timeout);
6162         cancel_work_sync(&ar->regd_update_work);
6163         cancel_work_sync(&ar->ab->update_11d_work);
6164
6165         if (ar->state_11d == ATH11K_11D_PREPARING) {
6166                 ar->state_11d = ATH11K_11D_IDLE;
6167                 complete(&ar->completed_11d_scan);
6168         }
6169
6170         spin_lock_bh(&ar->data_lock);
6171         list_for_each_entry_safe(ppdu_stats, tmp, &ar->ppdu_stats_info, list) {
6172                 list_del(&ppdu_stats->list);
6173                 kfree(ppdu_stats);
6174         }
6175         spin_unlock_bh(&ar->data_lock);
6176
6177         rcu_assign_pointer(ar->ab->pdevs_active[ar->pdev_idx], NULL);
6178
6179         synchronize_rcu();
6180
6181         atomic_set(&ar->num_pending_mgmt_tx, 0);
6182 }
6183
6184 static void
6185 ath11k_mac_setup_vdev_create_params(struct ath11k_vif *arvif,
6186                                     struct vdev_create_params *params)
6187 {
6188         struct ath11k *ar = arvif->ar;
6189         struct ath11k_pdev *pdev = ar->pdev;
6190
6191         params->if_id = arvif->vdev_id;
6192         params->type = arvif->vdev_type;
6193         params->subtype = arvif->vdev_subtype;
6194         params->pdev_id = pdev->pdev_id;
6195
6196         if (pdev->cap.supported_bands & WMI_HOST_WLAN_2G_CAP) {
6197                 params->chains[NL80211_BAND_2GHZ].tx = ar->num_tx_chains;
6198                 params->chains[NL80211_BAND_2GHZ].rx = ar->num_rx_chains;
6199         }
6200         if (pdev->cap.supported_bands & WMI_HOST_WLAN_5G_CAP) {
6201                 params->chains[NL80211_BAND_5GHZ].tx = ar->num_tx_chains;
6202                 params->chains[NL80211_BAND_5GHZ].rx = ar->num_rx_chains;
6203         }
6204         if (pdev->cap.supported_bands & WMI_HOST_WLAN_5G_CAP &&
6205             ar->supports_6ghz) {
6206                 params->chains[NL80211_BAND_6GHZ].tx = ar->num_tx_chains;
6207                 params->chains[NL80211_BAND_6GHZ].rx = ar->num_rx_chains;
6208         }
6209 }
6210
6211 static void ath11k_mac_op_update_vif_offload(struct ieee80211_hw *hw,
6212                                              struct ieee80211_vif *vif)
6213 {
6214         struct ath11k *ar = hw->priv;
6215         struct ath11k_base *ab = ar->ab;
6216         struct ath11k_vif *arvif = ath11k_vif_to_arvif(vif);
6217         u32 param_id, param_value;
6218         int ret;
6219
6220         param_id = WMI_VDEV_PARAM_TX_ENCAP_TYPE;
6221         if (ath11k_frame_mode != ATH11K_HW_TXRX_ETHERNET ||
6222             (vif->type != NL80211_IFTYPE_STATION &&
6223              vif->type != NL80211_IFTYPE_AP))
6224                 vif->offload_flags &= ~(IEEE80211_OFFLOAD_ENCAP_ENABLED |
6225                                         IEEE80211_OFFLOAD_DECAP_ENABLED);
6226
6227         if (vif->offload_flags & IEEE80211_OFFLOAD_ENCAP_ENABLED)
6228                 param_value = ATH11K_HW_TXRX_ETHERNET;
6229         else if (test_bit(ATH11K_FLAG_RAW_MODE, &ab->dev_flags))
6230                 param_value = ATH11K_HW_TXRX_RAW;
6231         else
6232                 param_value = ATH11K_HW_TXRX_NATIVE_WIFI;
6233
6234         ret = ath11k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
6235                                             param_id, param_value);
6236         if (ret) {
6237                 ath11k_warn(ab, "failed to set vdev %d tx encap mode: %d\n",
6238                             arvif->vdev_id, ret);
6239                 vif->offload_flags &= ~IEEE80211_OFFLOAD_ENCAP_ENABLED;
6240         }
6241
6242         param_id = WMI_VDEV_PARAM_RX_DECAP_TYPE;
6243         if (vif->offload_flags & IEEE80211_OFFLOAD_DECAP_ENABLED)
6244                 param_value = ATH11K_HW_TXRX_ETHERNET;
6245         else if (test_bit(ATH11K_FLAG_RAW_MODE, &ab->dev_flags))
6246                 param_value = ATH11K_HW_TXRX_RAW;
6247         else
6248                 param_value = ATH11K_HW_TXRX_NATIVE_WIFI;
6249
6250         ret = ath11k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
6251                                             param_id, param_value);
6252         if (ret) {
6253                 ath11k_warn(ab, "failed to set vdev %d rx decap mode: %d\n",
6254                             arvif->vdev_id, ret);
6255                 vif->offload_flags &= ~IEEE80211_OFFLOAD_DECAP_ENABLED;
6256         }
6257 }
6258
6259 static bool ath11k_mac_vif_ap_active_any(struct ath11k_base *ab)
6260 {
6261         struct ath11k *ar;
6262         struct ath11k_pdev *pdev;
6263         struct ath11k_vif *arvif;
6264         int i;
6265
6266         for (i = 0; i < ab->num_radios; i++) {
6267                 pdev = &ab->pdevs[i];
6268                 ar = pdev->ar;
6269                 list_for_each_entry(arvif, &ar->arvifs, list) {
6270                         if (arvif->is_up && arvif->vdev_type == WMI_VDEV_TYPE_AP)
6271                                 return true;
6272                 }
6273         }
6274         return false;
6275 }
6276
6277 void ath11k_mac_11d_scan_start(struct ath11k *ar, u32 vdev_id)
6278 {
6279         struct wmi_11d_scan_start_params param;
6280         int ret;
6281
6282         mutex_lock(&ar->ab->vdev_id_11d_lock);
6283
6284         ath11k_dbg(ar->ab, ATH11K_DBG_MAC, "mac vdev id for 11d scan %d\n",
6285                    ar->vdev_id_11d_scan);
6286
6287         if (ar->regdom_set_by_user)
6288                 goto fin;
6289
6290         if (ar->vdev_id_11d_scan != ATH11K_11D_INVALID_VDEV_ID)
6291                 goto fin;
6292
6293         if (!test_bit(WMI_TLV_SERVICE_11D_OFFLOAD, ar->ab->wmi_ab.svc_map))
6294                 goto fin;
6295
6296         if (ath11k_mac_vif_ap_active_any(ar->ab))
6297                 goto fin;
6298
6299         param.vdev_id = vdev_id;
6300         param.start_interval_msec = 0;
6301         param.scan_period_msec = ATH11K_SCAN_11D_INTERVAL;
6302
6303         ath11k_dbg(ar->ab, ATH11K_DBG_MAC, "mac start 11d scan\n");
6304
6305         ret = ath11k_wmi_send_11d_scan_start_cmd(ar, &param);
6306         if (ret) {
6307                 ath11k_warn(ar->ab, "failed to start 11d scan vdev %d ret: %d\n",
6308                             vdev_id, ret);
6309         } else {
6310                 ar->vdev_id_11d_scan = vdev_id;
6311                 if (ar->state_11d == ATH11K_11D_PREPARING)
6312                         ar->state_11d = ATH11K_11D_RUNNING;
6313         }
6314
6315 fin:
6316         if (ar->state_11d == ATH11K_11D_PREPARING) {
6317                 ar->state_11d = ATH11K_11D_IDLE;
6318                 complete(&ar->completed_11d_scan);
6319         }
6320
6321         mutex_unlock(&ar->ab->vdev_id_11d_lock);
6322 }
6323
6324 void ath11k_mac_11d_scan_stop(struct ath11k *ar)
6325 {
6326         int ret;
6327         u32 vdev_id;
6328
6329         if (!test_bit(WMI_TLV_SERVICE_11D_OFFLOAD, ar->ab->wmi_ab.svc_map))
6330                 return;
6331
6332         ath11k_dbg(ar->ab, ATH11K_DBG_MAC, "mac stop 11d scan\n");
6333
6334         mutex_lock(&ar->ab->vdev_id_11d_lock);
6335
6336         ath11k_dbg(ar->ab, ATH11K_DBG_MAC, "mac stop 11d vdev id %d\n",
6337                    ar->vdev_id_11d_scan);
6338
6339         if (ar->state_11d == ATH11K_11D_PREPARING) {
6340                 ar->state_11d = ATH11K_11D_IDLE;
6341                 complete(&ar->completed_11d_scan);
6342         }
6343
6344         if (ar->vdev_id_11d_scan != ATH11K_11D_INVALID_VDEV_ID) {
6345                 vdev_id = ar->vdev_id_11d_scan;
6346
6347                 ret = ath11k_wmi_send_11d_scan_stop_cmd(ar, vdev_id);
6348                 if (ret) {
6349                         ath11k_warn(ar->ab,
6350                                     "failed to stopt 11d scan vdev %d ret: %d\n",
6351                                     vdev_id, ret);
6352                 } else {
6353                         ar->vdev_id_11d_scan = ATH11K_11D_INVALID_VDEV_ID;
6354                         ar->state_11d = ATH11K_11D_IDLE;
6355                         complete(&ar->completed_11d_scan);
6356                 }
6357         }
6358         mutex_unlock(&ar->ab->vdev_id_11d_lock);
6359 }
6360
6361 void ath11k_mac_11d_scan_stop_all(struct ath11k_base *ab)
6362 {
6363         struct ath11k *ar;
6364         struct ath11k_pdev *pdev;
6365         int i;
6366
6367         ath11k_dbg(ab, ATH11K_DBG_MAC, "mac stop soc 11d scan\n");
6368
6369         for (i = 0; i < ab->num_radios; i++) {
6370                 pdev = &ab->pdevs[i];
6371                 ar = pdev->ar;
6372
6373                 ath11k_mac_11d_scan_stop(ar);
6374         }
6375 }
6376
6377 static int ath11k_mac_vdev_delete(struct ath11k *ar, struct ath11k_vif *arvif)
6378 {
6379         unsigned long time_left;
6380         struct ieee80211_vif *vif = arvif->vif;
6381         int ret = 0;
6382
6383         lockdep_assert_held(&ar->conf_mutex);
6384
6385         reinit_completion(&ar->vdev_delete_done);
6386
6387         ret = ath11k_wmi_vdev_delete(ar, arvif->vdev_id);
6388         if (ret) {
6389                 ath11k_warn(ar->ab, "failed to delete WMI vdev %d: %d\n",
6390                             arvif->vdev_id, ret);
6391                 return ret;
6392         }
6393
6394         time_left = wait_for_completion_timeout(&ar->vdev_delete_done,
6395                                                 ATH11K_VDEV_DELETE_TIMEOUT_HZ);
6396         if (time_left == 0) {
6397                 ath11k_warn(ar->ab, "Timeout in receiving vdev delete response\n");
6398                 return -ETIMEDOUT;
6399         }
6400
6401         ar->ab->free_vdev_map |= 1LL << (arvif->vdev_id);
6402         ar->allocated_vdev_map &= ~(1LL << arvif->vdev_id);
6403         ar->num_created_vdevs--;
6404
6405         ath11k_dbg(ar->ab, ATH11K_DBG_MAC, "vdev %pM deleted, vdev_id %d\n",
6406                    vif->addr, arvif->vdev_id);
6407
6408         return ret;
6409 }
6410
6411 static int ath11k_mac_op_add_interface(struct ieee80211_hw *hw,
6412                                        struct ieee80211_vif *vif)
6413 {
6414         struct ath11k *ar = hw->priv;
6415         struct ath11k_base *ab = ar->ab;
6416         struct ath11k_vif *arvif = ath11k_vif_to_arvif(vif);
6417         struct vdev_create_params vdev_param = {0};
6418         struct peer_create_params peer_param;
6419         u32 param_id, param_value;
6420         u16 nss;
6421         int i;
6422         int ret, fbret;
6423         int bit;
6424
6425         vif->driver_flags |= IEEE80211_VIF_SUPPORTS_UAPSD;
6426
6427         mutex_lock(&ar->conf_mutex);
6428
6429         if (vif->type == NL80211_IFTYPE_AP &&
6430             ar->num_peers > (ar->max_num_peers - 1)) {
6431                 ath11k_warn(ab, "failed to create vdev due to insufficient peer entry resource in firmware\n");
6432                 ret = -ENOBUFS;
6433                 goto err;
6434         }
6435
6436         if (ar->num_created_vdevs > (TARGET_NUM_VDEVS(ab) - 1)) {
6437                 ath11k_warn(ab, "failed to create vdev %u, reached max vdev limit %d\n",
6438                             ar->num_created_vdevs, TARGET_NUM_VDEVS(ab));
6439                 ret = -EBUSY;
6440                 goto err;
6441         }
6442
6443         /* In the case of hardware recovery, debugfs files are
6444          * not deleted since ieee80211_ops.remove_interface() is
6445          * not invoked. In such cases, try to delete the files.
6446          * These will be re-created later.
6447          */
6448         ath11k_debugfs_remove_interface(arvif);
6449
6450         memset(arvif, 0, sizeof(*arvif));
6451
6452         arvif->ar = ar;
6453         arvif->vif = vif;
6454
6455         INIT_LIST_HEAD(&arvif->list);
6456         INIT_DELAYED_WORK(&arvif->connection_loss_work,
6457                           ath11k_mac_vif_sta_connection_loss_work);
6458
6459         for (i = 0; i < ARRAY_SIZE(arvif->bitrate_mask.control); i++) {
6460                 arvif->bitrate_mask.control[i].legacy = 0xffffffff;
6461                 arvif->bitrate_mask.control[i].gi = NL80211_TXRATE_FORCE_SGI;
6462                 memset(arvif->bitrate_mask.control[i].ht_mcs, 0xff,
6463                        sizeof(arvif->bitrate_mask.control[i].ht_mcs));
6464                 memset(arvif->bitrate_mask.control[i].vht_mcs, 0xff,
6465                        sizeof(arvif->bitrate_mask.control[i].vht_mcs));
6466                 memset(arvif->bitrate_mask.control[i].he_mcs, 0xff,
6467                        sizeof(arvif->bitrate_mask.control[i].he_mcs));
6468         }
6469
6470         bit = __ffs64(ab->free_vdev_map);
6471
6472         arvif->vdev_id = bit;
6473         arvif->vdev_subtype = WMI_VDEV_SUBTYPE_NONE;
6474
6475         switch (vif->type) {
6476         case NL80211_IFTYPE_UNSPECIFIED:
6477         case NL80211_IFTYPE_STATION:
6478                 arvif->vdev_type = WMI_VDEV_TYPE_STA;
6479                 break;
6480         case NL80211_IFTYPE_MESH_POINT:
6481                 arvif->vdev_subtype = WMI_VDEV_SUBTYPE_MESH_11S;
6482                 fallthrough;
6483         case NL80211_IFTYPE_AP:
6484                 arvif->vdev_type = WMI_VDEV_TYPE_AP;
6485                 break;
6486         case NL80211_IFTYPE_MONITOR:
6487                 arvif->vdev_type = WMI_VDEV_TYPE_MONITOR;
6488                 ar->monitor_vdev_id = bit;
6489                 break;
6490         default:
6491                 WARN_ON(1);
6492                 break;
6493         }
6494
6495         ath11k_dbg(ar->ab, ATH11K_DBG_MAC, "mac add interface id %d type %d subtype %d map %llx\n",
6496                    arvif->vdev_id, arvif->vdev_type, arvif->vdev_subtype,
6497                    ab->free_vdev_map);
6498
6499         vif->cab_queue = arvif->vdev_id % (ATH11K_HW_MAX_QUEUES - 1);
6500         for (i = 0; i < ARRAY_SIZE(vif->hw_queue); i++)
6501                 vif->hw_queue[i] = i % (ATH11K_HW_MAX_QUEUES - 1);
6502
6503         ath11k_mac_setup_vdev_create_params(arvif, &vdev_param);
6504
6505         ret = ath11k_wmi_vdev_create(ar, vif->addr, &vdev_param);
6506         if (ret) {
6507                 ath11k_warn(ab, "failed to create WMI vdev %d: %d\n",
6508                             arvif->vdev_id, ret);
6509                 goto err;
6510         }
6511
6512         ar->num_created_vdevs++;
6513         ath11k_dbg(ab, ATH11K_DBG_MAC, "vdev %pM created, vdev_id %d\n",
6514                    vif->addr, arvif->vdev_id);
6515         ar->allocated_vdev_map |= 1LL << arvif->vdev_id;
6516         ab->free_vdev_map &= ~(1LL << arvif->vdev_id);
6517
6518         spin_lock_bh(&ar->data_lock);
6519         list_add(&arvif->list, &ar->arvifs);
6520         spin_unlock_bh(&ar->data_lock);
6521
6522         ath11k_mac_op_update_vif_offload(hw, vif);
6523
6524         nss = get_num_chains(ar->cfg_tx_chainmask) ? : 1;
6525         ret = ath11k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
6526                                             WMI_VDEV_PARAM_NSS, nss);
6527         if (ret) {
6528                 ath11k_warn(ab, "failed to set vdev %d chainmask 0x%x, nss %d :%d\n",
6529                             arvif->vdev_id, ar->cfg_tx_chainmask, nss, ret);
6530                 goto err_vdev_del;
6531         }
6532
6533         switch (arvif->vdev_type) {
6534         case WMI_VDEV_TYPE_AP:
6535                 peer_param.vdev_id = arvif->vdev_id;
6536                 peer_param.peer_addr = vif->addr;
6537                 peer_param.peer_type = WMI_PEER_TYPE_DEFAULT;
6538                 ret = ath11k_peer_create(ar, arvif, NULL, &peer_param);
6539                 if (ret) {
6540                         ath11k_warn(ab, "failed to vdev %d create peer for AP: %d\n",
6541                                     arvif->vdev_id, ret);
6542                         goto err_vdev_del;
6543                 }
6544
6545                 ret = ath11k_mac_set_kickout(arvif);
6546                 if (ret) {
6547                         ath11k_warn(ar->ab, "failed to set vdev %i kickout parameters: %d\n",
6548                                     arvif->vdev_id, ret);
6549                         goto err_peer_del;
6550                 }
6551
6552                 ath11k_mac_11d_scan_stop_all(ar->ab);
6553                 break;
6554         case WMI_VDEV_TYPE_STA:
6555                 param_id = WMI_STA_PS_PARAM_RX_WAKE_POLICY;
6556                 param_value = WMI_STA_PS_RX_WAKE_POLICY_WAKE;
6557                 ret = ath11k_wmi_set_sta_ps_param(ar, arvif->vdev_id,
6558                                                   param_id, param_value);
6559                 if (ret) {
6560                         ath11k_warn(ar->ab, "failed to set vdev %d RX wake policy: %d\n",
6561                                     arvif->vdev_id, ret);
6562                         goto err_peer_del;
6563                 }
6564
6565                 param_id = WMI_STA_PS_PARAM_TX_WAKE_THRESHOLD;
6566                 param_value = WMI_STA_PS_TX_WAKE_THRESHOLD_ALWAYS;
6567                 ret = ath11k_wmi_set_sta_ps_param(ar, arvif->vdev_id,
6568                                                   param_id, param_value);
6569                 if (ret) {
6570                         ath11k_warn(ar->ab, "failed to set vdev %d TX wake threshold: %d\n",
6571                                     arvif->vdev_id, ret);
6572                         goto err_peer_del;
6573                 }
6574
6575                 param_id = WMI_STA_PS_PARAM_PSPOLL_COUNT;
6576                 param_value = WMI_STA_PS_PSPOLL_COUNT_NO_MAX;
6577                 ret = ath11k_wmi_set_sta_ps_param(ar, arvif->vdev_id,
6578                                                   param_id, param_value);
6579                 if (ret) {
6580                         ath11k_warn(ar->ab, "failed to set vdev %d pspoll count: %d\n",
6581                                     arvif->vdev_id, ret);
6582                         goto err_peer_del;
6583                 }
6584
6585                 ret = ath11k_wmi_pdev_set_ps_mode(ar, arvif->vdev_id,
6586                                                   WMI_STA_PS_MODE_DISABLED);
6587                 if (ret) {
6588                         ath11k_warn(ar->ab, "failed to disable vdev %d ps mode: %d\n",
6589                                     arvif->vdev_id, ret);
6590                         goto err_peer_del;
6591                 }
6592
6593                 if (test_bit(WMI_TLV_SERVICE_11D_OFFLOAD, ab->wmi_ab.svc_map)) {
6594                         reinit_completion(&ar->completed_11d_scan);
6595                         ar->state_11d = ATH11K_11D_PREPARING;
6596                 }
6597                 break;
6598         case WMI_VDEV_TYPE_MONITOR:
6599                 set_bit(ATH11K_FLAG_MONITOR_VDEV_CREATED, &ar->monitor_flags);
6600                 break;
6601         default:
6602                 break;
6603         }
6604
6605         arvif->txpower = vif->bss_conf.txpower;
6606         ret = ath11k_mac_txpower_recalc(ar);
6607         if (ret)
6608                 goto err_peer_del;
6609
6610         param_id = WMI_VDEV_PARAM_RTS_THRESHOLD;
6611         param_value = ar->hw->wiphy->rts_threshold;
6612         ret = ath11k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
6613                                             param_id, param_value);
6614         if (ret) {
6615                 ath11k_warn(ar->ab, "failed to set rts threshold for vdev %d: %d\n",
6616                             arvif->vdev_id, ret);
6617         }
6618
6619         ath11k_dp_vdev_tx_attach(ar, arvif);
6620
6621         ath11k_debugfs_add_interface(arvif);
6622
6623         if (vif->type != NL80211_IFTYPE_MONITOR &&
6624             test_bit(ATH11K_FLAG_MONITOR_CONF_ENABLED, &ar->monitor_flags)) {
6625                 ret = ath11k_mac_monitor_vdev_create(ar);
6626                 if (ret)
6627                         ath11k_warn(ar->ab, "failed to create monitor vdev during add interface: %d",
6628                                     ret);
6629         }
6630
6631         mutex_unlock(&ar->conf_mutex);
6632
6633         return 0;
6634
6635 err_peer_del:
6636         if (arvif->vdev_type == WMI_VDEV_TYPE_AP) {
6637                 fbret = ath11k_peer_delete(ar, arvif->vdev_id, vif->addr);
6638                 if (fbret) {
6639                         ath11k_warn(ar->ab, "fallback fail to delete peer addr %pM vdev_id %d ret %d\n",
6640                                     vif->addr, arvif->vdev_id, fbret);
6641                         goto err;
6642                 }
6643         }
6644
6645 err_vdev_del:
6646         ath11k_mac_vdev_delete(ar, arvif);
6647         spin_lock_bh(&ar->data_lock);
6648         list_del(&arvif->list);
6649         spin_unlock_bh(&ar->data_lock);
6650
6651 err:
6652         mutex_unlock(&ar->conf_mutex);
6653
6654         return ret;
6655 }
6656
6657 static int ath11k_mac_vif_unref(int buf_id, void *skb, void *ctx)
6658 {
6659         struct ieee80211_vif *vif = (struct ieee80211_vif *)ctx;
6660         struct ath11k_skb_cb *skb_cb = ATH11K_SKB_CB((struct sk_buff *)skb);
6661
6662         if (skb_cb->vif == vif)
6663                 skb_cb->vif = NULL;
6664
6665         return 0;
6666 }
6667
6668 static void ath11k_mac_op_remove_interface(struct ieee80211_hw *hw,
6669                                            struct ieee80211_vif *vif)
6670 {
6671         struct ath11k *ar = hw->priv;
6672         struct ath11k_vif *arvif = ath11k_vif_to_arvif(vif);
6673         struct ath11k_base *ab = ar->ab;
6674         int ret;
6675         int i;
6676
6677         cancel_delayed_work_sync(&arvif->connection_loss_work);
6678
6679         mutex_lock(&ar->conf_mutex);
6680
6681         ath11k_dbg(ab, ATH11K_DBG_MAC, "mac remove interface (vdev %d)\n",
6682                    arvif->vdev_id);
6683
6684         ret = ath11k_spectral_vif_stop(arvif);
6685         if (ret)
6686                 ath11k_warn(ab, "failed to stop spectral for vdev %i: %d\n",
6687                             arvif->vdev_id, ret);
6688
6689         if (arvif->vdev_type == WMI_VDEV_TYPE_STA)
6690                 ath11k_mac_11d_scan_stop(ar);
6691
6692         if (arvif->vdev_type == WMI_VDEV_TYPE_AP) {
6693                 ret = ath11k_peer_delete(ar, arvif->vdev_id, vif->addr);
6694                 if (ret)
6695                         ath11k_warn(ab, "failed to submit AP self-peer removal on vdev %d: %d\n",
6696                                     arvif->vdev_id, ret);
6697         }
6698
6699         ret = ath11k_mac_vdev_delete(ar, arvif);
6700         if (ret) {
6701                 ath11k_warn(ab, "failed to delete vdev %d: %d\n",
6702                             arvif->vdev_id, ret);
6703                 goto err_vdev_del;
6704         }
6705
6706         if (arvif->vdev_type == WMI_VDEV_TYPE_MONITOR) {
6707                 clear_bit(ATH11K_FLAG_MONITOR_VDEV_CREATED, &ar->monitor_flags);
6708                 ar->monitor_vdev_id = -1;
6709         } else if (test_bit(ATH11K_FLAG_MONITOR_VDEV_CREATED, &ar->monitor_flags) &&
6710                    !test_bit(ATH11K_FLAG_MONITOR_STARTED, &ar->monitor_flags)) {
6711                 ret = ath11k_mac_monitor_vdev_delete(ar);
6712                 if (ret)
6713                         /* continue even if there's an error */
6714                         ath11k_warn(ar->ab, "failed to delete vdev monitor during remove interface: %d",
6715                                     ret);
6716         }
6717
6718 err_vdev_del:
6719         spin_lock_bh(&ar->data_lock);
6720         list_del(&arvif->list);
6721         spin_unlock_bh(&ar->data_lock);
6722
6723         ath11k_peer_cleanup(ar, arvif->vdev_id);
6724
6725         idr_for_each(&ar->txmgmt_idr,
6726                      ath11k_mac_vif_txmgmt_idr_remove, vif);
6727
6728         for (i = 0; i < ab->hw_params.max_tx_ring; i++) {
6729                 spin_lock_bh(&ab->dp.tx_ring[i].tx_idr_lock);
6730                 idr_for_each(&ab->dp.tx_ring[i].txbuf_idr,
6731                              ath11k_mac_vif_unref, vif);
6732                 spin_unlock_bh(&ab->dp.tx_ring[i].tx_idr_lock);
6733         }
6734
6735         /* Recalc txpower for remaining vdev */
6736         ath11k_mac_txpower_recalc(ar);
6737
6738         ath11k_debugfs_remove_interface(arvif);
6739
6740         /* TODO: recal traffic pause state based on the available vdevs */
6741
6742         mutex_unlock(&ar->conf_mutex);
6743 }
6744
6745 /* FIXME: Has to be verified. */
6746 #define SUPPORTED_FILTERS                       \
6747         (FIF_ALLMULTI |                         \
6748         FIF_CONTROL |                           \
6749         FIF_PSPOLL |                            \
6750         FIF_OTHER_BSS |                         \
6751         FIF_BCN_PRBRESP_PROMISC |               \
6752         FIF_PROBE_REQ |                         \
6753         FIF_FCSFAIL)
6754
6755 static void ath11k_mac_op_configure_filter(struct ieee80211_hw *hw,
6756                                            unsigned int changed_flags,
6757                                            unsigned int *total_flags,
6758                                            u64 multicast)
6759 {
6760         struct ath11k *ar = hw->priv;
6761
6762         mutex_lock(&ar->conf_mutex);
6763
6764         *total_flags &= SUPPORTED_FILTERS;
6765         ar->filter_flags = *total_flags;
6766
6767         mutex_unlock(&ar->conf_mutex);
6768 }
6769
6770 static int ath11k_mac_op_get_antenna(struct ieee80211_hw *hw, u32 *tx_ant, u32 *rx_ant)
6771 {
6772         struct ath11k *ar = hw->priv;
6773
6774         mutex_lock(&ar->conf_mutex);
6775
6776         *tx_ant = ar->cfg_tx_chainmask;
6777         *rx_ant = ar->cfg_rx_chainmask;
6778
6779         mutex_unlock(&ar->conf_mutex);
6780
6781         return 0;
6782 }
6783
6784 static int ath11k_mac_op_set_antenna(struct ieee80211_hw *hw, u32 tx_ant, u32 rx_ant)
6785 {
6786         struct ath11k *ar = hw->priv;
6787         int ret;
6788
6789         mutex_lock(&ar->conf_mutex);
6790         ret = __ath11k_set_antenna(ar, tx_ant, rx_ant);
6791         mutex_unlock(&ar->conf_mutex);
6792
6793         return ret;
6794 }
6795
6796 static int ath11k_mac_op_ampdu_action(struct ieee80211_hw *hw,
6797                                       struct ieee80211_vif *vif,
6798                                       struct ieee80211_ampdu_params *params)
6799 {
6800         struct ath11k *ar = hw->priv;
6801         int ret = -EINVAL;
6802
6803         mutex_lock(&ar->conf_mutex);
6804
6805         switch (params->action) {
6806         case IEEE80211_AMPDU_RX_START:
6807                 ret = ath11k_dp_rx_ampdu_start(ar, params);
6808                 break;
6809         case IEEE80211_AMPDU_RX_STOP:
6810                 ret = ath11k_dp_rx_ampdu_stop(ar, params);
6811                 break;
6812         case IEEE80211_AMPDU_TX_START:
6813         case IEEE80211_AMPDU_TX_STOP_CONT:
6814         case IEEE80211_AMPDU_TX_STOP_FLUSH:
6815         case IEEE80211_AMPDU_TX_STOP_FLUSH_CONT:
6816         case IEEE80211_AMPDU_TX_OPERATIONAL:
6817                 /* Tx A-MPDU aggregation offloaded to hw/fw so deny mac80211
6818                  * Tx aggregation requests.
6819                  */
6820                 ret = -EOPNOTSUPP;
6821                 break;
6822         }
6823
6824         mutex_unlock(&ar->conf_mutex);
6825
6826         return ret;
6827 }
6828
6829 static int ath11k_mac_op_add_chanctx(struct ieee80211_hw *hw,
6830                                      struct ieee80211_chanctx_conf *ctx)
6831 {
6832         struct ath11k *ar = hw->priv;
6833         struct ath11k_base *ab = ar->ab;
6834
6835         ath11k_dbg(ab, ATH11K_DBG_MAC,
6836                    "mac chanctx add freq %u width %d ptr %pK\n",
6837                    ctx->def.chan->center_freq, ctx->def.width, ctx);
6838
6839         mutex_lock(&ar->conf_mutex);
6840
6841         spin_lock_bh(&ar->data_lock);
6842         /* TODO: In case of multiple channel context, populate rx_channel from
6843          * Rx PPDU desc information.
6844          */
6845         ar->rx_channel = ctx->def.chan;
6846         spin_unlock_bh(&ar->data_lock);
6847
6848         mutex_unlock(&ar->conf_mutex);
6849
6850         return 0;
6851 }
6852
6853 static void ath11k_mac_op_remove_chanctx(struct ieee80211_hw *hw,
6854                                          struct ieee80211_chanctx_conf *ctx)
6855 {
6856         struct ath11k *ar = hw->priv;
6857         struct ath11k_base *ab = ar->ab;
6858
6859         ath11k_dbg(ab, ATH11K_DBG_MAC,
6860                    "mac chanctx remove freq %u width %d ptr %pK\n",
6861                    ctx->def.chan->center_freq, ctx->def.width, ctx);
6862
6863         mutex_lock(&ar->conf_mutex);
6864
6865         spin_lock_bh(&ar->data_lock);
6866         /* TODO: In case of there is one more channel context left, populate
6867          * rx_channel with the channel of that remaining channel context.
6868          */
6869         ar->rx_channel = NULL;
6870         spin_unlock_bh(&ar->data_lock);
6871
6872         mutex_unlock(&ar->conf_mutex);
6873 }
6874
6875 static int
6876 ath11k_mac_vdev_start_restart(struct ath11k_vif *arvif,
6877                               struct ieee80211_chanctx_conf *ctx,
6878                               bool restart)
6879 {
6880         struct ath11k *ar = arvif->ar;
6881         struct ath11k_base *ab = ar->ab;
6882         struct wmi_vdev_start_req_arg arg = {};
6883         const struct cfg80211_chan_def *chandef = &ctx->def;
6884         int ret = 0;
6885
6886         lockdep_assert_held(&ar->conf_mutex);
6887
6888         reinit_completion(&ar->vdev_setup_done);
6889
6890         arg.vdev_id = arvif->vdev_id;
6891         arg.dtim_period = arvif->dtim_period;
6892         arg.bcn_intval = arvif->beacon_interval;
6893
6894         arg.channel.freq = chandef->chan->center_freq;
6895         arg.channel.band_center_freq1 = chandef->center_freq1;
6896         arg.channel.band_center_freq2 = chandef->center_freq2;
6897         arg.channel.mode =
6898                 ath11k_phymodes[chandef->chan->band][chandef->width];
6899
6900         arg.channel.min_power = 0;
6901         arg.channel.max_power = chandef->chan->max_power;
6902         arg.channel.max_reg_power = chandef->chan->max_reg_power;
6903         arg.channel.max_antenna_gain = chandef->chan->max_antenna_gain;
6904
6905         arg.pref_tx_streams = ar->num_tx_chains;
6906         arg.pref_rx_streams = ar->num_rx_chains;
6907
6908         if (arvif->vdev_type == WMI_VDEV_TYPE_AP) {
6909                 arg.ssid = arvif->u.ap.ssid;
6910                 arg.ssid_len = arvif->u.ap.ssid_len;
6911                 arg.hidden_ssid = arvif->u.ap.hidden_ssid;
6912
6913                 /* For now allow DFS for AP mode */
6914                 arg.channel.chan_radar =
6915                         !!(chandef->chan->flags & IEEE80211_CHAN_RADAR);
6916
6917                 arg.channel.freq2_radar = ctx->radar_enabled;
6918
6919                 arg.channel.passive = arg.channel.chan_radar;
6920
6921                 spin_lock_bh(&ab->base_lock);
6922                 arg.regdomain = ar->ab->dfs_region;
6923                 spin_unlock_bh(&ab->base_lock);
6924         }
6925
6926         arg.channel.passive |= !!(chandef->chan->flags & IEEE80211_CHAN_NO_IR);
6927
6928         ath11k_dbg(ab, ATH11K_DBG_MAC,
6929                    "mac vdev %d start center_freq %d phymode %s\n",
6930                    arg.vdev_id, arg.channel.freq,
6931                    ath11k_wmi_phymode_str(arg.channel.mode));
6932
6933         ret = ath11k_wmi_vdev_start(ar, &arg, restart);
6934         if (ret) {
6935                 ath11k_warn(ar->ab, "failed to %s WMI vdev %i\n",
6936                             restart ? "restart" : "start", arg.vdev_id);
6937                 return ret;
6938         }
6939
6940         ret = ath11k_mac_vdev_setup_sync(ar);
6941         if (ret) {
6942                 ath11k_warn(ab, "failed to synchronize setup for vdev %i %s: %d\n",
6943                             arg.vdev_id, restart ? "restart" : "start", ret);
6944                 return ret;
6945         }
6946
6947         if (!restart)
6948                 ar->num_started_vdevs++;
6949
6950         ath11k_dbg(ab, ATH11K_DBG_MAC,  "vdev %pM started, vdev_id %d\n",
6951                    arvif->vif->addr, arvif->vdev_id);
6952
6953         /* Enable CAC Flag in the driver by checking the channel DFS cac time,
6954          * i.e dfs_cac_ms value which will be valid only for radar channels
6955          * and state as NL80211_DFS_USABLE which indicates CAC needs to be
6956          * done before channel usage. This flags is used to drop rx packets.
6957          * during CAC.
6958          */
6959         /* TODO Set the flag for other interface types as required */
6960         if (arvif->vdev_type == WMI_VDEV_TYPE_AP &&
6961             chandef->chan->dfs_cac_ms &&
6962             chandef->chan->dfs_state == NL80211_DFS_USABLE) {
6963                 set_bit(ATH11K_CAC_RUNNING, &ar->dev_flags);
6964                 ath11k_dbg(ab, ATH11K_DBG_MAC,
6965                            "CAC Started in chan_freq %d for vdev %d\n",
6966                            arg.channel.freq, arg.vdev_id);
6967         }
6968
6969         ret = ath11k_mac_set_txbf_conf(arvif);
6970         if (ret)
6971                 ath11k_warn(ab, "failed to set txbf conf for vdev %d: %d\n",
6972                             arvif->vdev_id, ret);
6973
6974         return 0;
6975 }
6976
6977 static int ath11k_mac_vdev_stop(struct ath11k_vif *arvif)
6978 {
6979         struct ath11k *ar = arvif->ar;
6980         int ret;
6981
6982         lockdep_assert_held(&ar->conf_mutex);
6983
6984         reinit_completion(&ar->vdev_setup_done);
6985
6986         ret = ath11k_wmi_vdev_stop(ar, arvif->vdev_id);
6987         if (ret) {
6988                 ath11k_warn(ar->ab, "failed to stop WMI vdev %i: %d\n",
6989                             arvif->vdev_id, ret);
6990                 goto err;
6991         }
6992
6993         ret = ath11k_mac_vdev_setup_sync(ar);
6994         if (ret) {
6995                 ath11k_warn(ar->ab, "failed to synchronize setup for vdev %i: %d\n",
6996                             arvif->vdev_id, ret);
6997                 goto err;
6998         }
6999
7000         WARN_ON(ar->num_started_vdevs == 0);
7001
7002         ar->num_started_vdevs--;
7003         ath11k_dbg(ar->ab, ATH11K_DBG_MAC, "vdev %pM stopped, vdev_id %d\n",
7004                    arvif->vif->addr, arvif->vdev_id);
7005
7006         if (test_bit(ATH11K_CAC_RUNNING, &ar->dev_flags)) {
7007                 clear_bit(ATH11K_CAC_RUNNING, &ar->dev_flags);
7008                 ath11k_dbg(ar->ab, ATH11K_DBG_MAC, "CAC Stopped for vdev %d\n",
7009                            arvif->vdev_id);
7010         }
7011
7012         return 0;
7013 err:
7014         return ret;
7015 }
7016
7017 static int ath11k_mac_vdev_start(struct ath11k_vif *arvif,
7018                                  struct ieee80211_chanctx_conf *ctx)
7019 {
7020         return ath11k_mac_vdev_start_restart(arvif, ctx, false);
7021 }
7022
7023 static int ath11k_mac_vdev_restart(struct ath11k_vif *arvif,
7024                                    struct ieee80211_chanctx_conf *ctx)
7025 {
7026         return ath11k_mac_vdev_start_restart(arvif, ctx, true);
7027 }
7028
7029 struct ath11k_mac_change_chanctx_arg {
7030         struct ieee80211_chanctx_conf *ctx;
7031         struct ieee80211_vif_chanctx_switch *vifs;
7032         int n_vifs;
7033         int next_vif;
7034 };
7035
7036 static void
7037 ath11k_mac_change_chanctx_cnt_iter(void *data, u8 *mac,
7038                                    struct ieee80211_vif *vif)
7039 {
7040         struct ath11k_mac_change_chanctx_arg *arg = data;
7041
7042         if (rcu_access_pointer(vif->bss_conf.chanctx_conf) != arg->ctx)
7043                 return;
7044
7045         arg->n_vifs++;
7046 }
7047
7048 static void
7049 ath11k_mac_change_chanctx_fill_iter(void *data, u8 *mac,
7050                                     struct ieee80211_vif *vif)
7051 {
7052         struct ath11k_mac_change_chanctx_arg *arg = data;
7053         struct ieee80211_chanctx_conf *ctx;
7054
7055         ctx = rcu_access_pointer(vif->bss_conf.chanctx_conf);
7056         if (ctx != arg->ctx)
7057                 return;
7058
7059         if (WARN_ON(arg->next_vif == arg->n_vifs))
7060                 return;
7061
7062         arg->vifs[arg->next_vif].vif = vif;
7063         arg->vifs[arg->next_vif].old_ctx = ctx;
7064         arg->vifs[arg->next_vif].new_ctx = ctx;
7065         arg->next_vif++;
7066 }
7067
7068 static void
7069 ath11k_mac_update_vif_chan(struct ath11k *ar,
7070                            struct ieee80211_vif_chanctx_switch *vifs,
7071                            int n_vifs)
7072 {
7073         struct ath11k_base *ab = ar->ab;
7074         struct ath11k_vif *arvif;
7075         int ret;
7076         int i;
7077         bool monitor_vif = false;
7078
7079         lockdep_assert_held(&ar->conf_mutex);
7080
7081         /* Associated channel resources of all relevant vdevs
7082          * should be available for the channel switch now.
7083          */
7084
7085         /* TODO: Update ar->rx_channel */
7086
7087         for (i = 0; i < n_vifs; i++) {
7088                 arvif = (void *)vifs[i].vif->drv_priv;
7089
7090                 if (WARN_ON(!arvif->is_started))
7091                         continue;
7092
7093                 /* change_chanctx can be called even before vdev_up from
7094                  * ieee80211_start_ap->ieee80211_vif_use_channel->
7095                  * ieee80211_recalc_radar_chanctx.
7096                  *
7097                  * Firmware expect vdev_restart only if vdev is up.
7098                  * If vdev is down then it expect vdev_stop->vdev_start.
7099                  */
7100                 if (arvif->is_up) {
7101                         ret = ath11k_mac_vdev_restart(arvif, vifs[i].new_ctx);
7102                         if (ret) {
7103                                 ath11k_warn(ab, "failed to restart vdev %d: %d\n",
7104                                             arvif->vdev_id, ret);
7105                                 continue;
7106                         }
7107                 } else {
7108                         ret = ath11k_mac_vdev_stop(arvif);
7109                         if (ret) {
7110                                 ath11k_warn(ab, "failed to stop vdev %d: %d\n",
7111                                             arvif->vdev_id, ret);
7112                                 continue;
7113                         }
7114
7115                         ret = ath11k_mac_vdev_start(arvif, vifs[i].new_ctx);
7116                         if (ret)
7117                                 ath11k_warn(ab, "failed to start vdev %d: %d\n",
7118                                             arvif->vdev_id, ret);
7119
7120                         continue;
7121                 }
7122
7123                 ret = ath11k_mac_setup_bcn_tmpl(arvif);
7124                 if (ret)
7125                         ath11k_warn(ab, "failed to update bcn tmpl during csa: %d\n",
7126                                     ret);
7127
7128                 ret = ath11k_wmi_vdev_up(arvif->ar, arvif->vdev_id, arvif->aid,
7129                                          arvif->bssid);
7130                 if (ret) {
7131                         ath11k_warn(ab, "failed to bring vdev up %d: %d\n",
7132                                     arvif->vdev_id, ret);
7133                         continue;
7134                 }
7135         }
7136
7137         /* Restart the internal monitor vdev on new channel */
7138         if (!monitor_vif &&
7139             test_bit(ATH11K_FLAG_MONITOR_VDEV_CREATED, &ar->monitor_flags)) {
7140                 ret = ath11k_mac_monitor_stop(ar);
7141                 if (ret) {
7142                         ath11k_warn(ar->ab, "failed to stop monitor during vif channel update: %d",
7143                                     ret);
7144                         return;
7145                 }
7146
7147                 ret = ath11k_mac_monitor_start(ar);
7148                 if (ret) {
7149                         ath11k_warn(ar->ab, "failed to start monitor during vif channel update: %d",
7150                                     ret);
7151                         return;
7152                 }
7153         }
7154 }
7155
7156 static void
7157 ath11k_mac_update_active_vif_chan(struct ath11k *ar,
7158                                   struct ieee80211_chanctx_conf *ctx)
7159 {
7160         struct ath11k_mac_change_chanctx_arg arg = { .ctx = ctx };
7161
7162         lockdep_assert_held(&ar->conf_mutex);
7163
7164         ieee80211_iterate_active_interfaces_atomic(ar->hw,
7165                                                    IEEE80211_IFACE_ITER_NORMAL,
7166                                                    ath11k_mac_change_chanctx_cnt_iter,
7167                                                    &arg);
7168         if (arg.n_vifs == 0)
7169                 return;
7170
7171         arg.vifs = kcalloc(arg.n_vifs, sizeof(arg.vifs[0]), GFP_KERNEL);
7172         if (!arg.vifs)
7173                 return;
7174
7175         ieee80211_iterate_active_interfaces_atomic(ar->hw,
7176                                                    IEEE80211_IFACE_ITER_NORMAL,
7177                                                    ath11k_mac_change_chanctx_fill_iter,
7178                                                    &arg);
7179
7180         ath11k_mac_update_vif_chan(ar, arg.vifs, arg.n_vifs);
7181
7182         kfree(arg.vifs);
7183 }
7184
7185 static void ath11k_mac_op_change_chanctx(struct ieee80211_hw *hw,
7186                                          struct ieee80211_chanctx_conf *ctx,
7187                                          u32 changed)
7188 {
7189         struct ath11k *ar = hw->priv;
7190         struct ath11k_base *ab = ar->ab;
7191
7192         mutex_lock(&ar->conf_mutex);
7193
7194         ath11k_dbg(ab, ATH11K_DBG_MAC,
7195                    "mac chanctx change freq %u width %d ptr %pK changed %x\n",
7196                    ctx->def.chan->center_freq, ctx->def.width, ctx, changed);
7197
7198         /* This shouldn't really happen because channel switching should use
7199          * switch_vif_chanctx().
7200          */
7201         if (WARN_ON(changed & IEEE80211_CHANCTX_CHANGE_CHANNEL))
7202                 goto unlock;
7203
7204         if (changed & IEEE80211_CHANCTX_CHANGE_WIDTH ||
7205             changed & IEEE80211_CHANCTX_CHANGE_RADAR)
7206                 ath11k_mac_update_active_vif_chan(ar, ctx);
7207
7208         /* TODO: Recalc radar detection */
7209
7210 unlock:
7211         mutex_unlock(&ar->conf_mutex);
7212 }
7213
7214 static int ath11k_start_vdev_delay(struct ieee80211_hw *hw,
7215                                    struct ieee80211_vif *vif)
7216 {
7217         struct ath11k *ar = hw->priv;
7218         struct ath11k_base *ab = ar->ab;
7219         struct ath11k_vif *arvif = (void *)vif->drv_priv;
7220         int ret;
7221
7222         if (WARN_ON(arvif->is_started))
7223                 return -EBUSY;
7224
7225         ret = ath11k_mac_vdev_start(arvif, &arvif->chanctx);
7226         if (ret) {
7227                 ath11k_warn(ab, "failed to start vdev %i addr %pM on freq %d: %d\n",
7228                             arvif->vdev_id, vif->addr,
7229                             arvif->chanctx.def.chan->center_freq, ret);
7230                 return ret;
7231         }
7232
7233         /* Reconfigure hardware rate code since it is cleared by firmware.
7234          */
7235         if (ar->hw_rate_code > 0) {
7236                 u32 vdev_param = WMI_VDEV_PARAM_MGMT_RATE;
7237
7238                 ret = ath11k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id, vdev_param,
7239                                                     ar->hw_rate_code);
7240                 if (ret) {
7241                         ath11k_warn(ar->ab, "failed to set mgmt tx rate %d\n", ret);
7242                         return ret;
7243                 }
7244         }
7245
7246         if (arvif->vdev_type == WMI_VDEV_TYPE_MONITOR) {
7247                 ret = ath11k_wmi_vdev_up(ar, arvif->vdev_id, 0, ar->mac_addr);
7248                 if (ret) {
7249                         ath11k_warn(ab, "failed put monitor up: %d\n", ret);
7250                         return ret;
7251                 }
7252         }
7253
7254         arvif->is_started = true;
7255
7256         /* TODO: Setup ps and cts/rts protection */
7257         return 0;
7258 }
7259
7260 static int
7261 ath11k_mac_op_assign_vif_chanctx(struct ieee80211_hw *hw,
7262                                  struct ieee80211_vif *vif,
7263                                  struct ieee80211_bss_conf *link_conf,
7264                                  struct ieee80211_chanctx_conf *ctx)
7265 {
7266         struct ath11k *ar = hw->priv;
7267         struct ath11k_base *ab = ar->ab;
7268         struct ath11k_vif *arvif = (void *)vif->drv_priv;
7269         int ret;
7270         struct peer_create_params param;
7271
7272         mutex_lock(&ar->conf_mutex);
7273
7274         ath11k_dbg(ab, ATH11K_DBG_MAC,
7275                    "mac chanctx assign ptr %pK vdev_id %i\n",
7276                    ctx, arvif->vdev_id);
7277
7278         /* for QCA6390 bss peer must be created before vdev_start */
7279         if (ab->hw_params.vdev_start_delay &&
7280             arvif->vdev_type != WMI_VDEV_TYPE_AP &&
7281             arvif->vdev_type != WMI_VDEV_TYPE_MONITOR &&
7282             !ath11k_peer_find_by_vdev_id(ab, arvif->vdev_id)) {
7283                 memcpy(&arvif->chanctx, ctx, sizeof(*ctx));
7284                 ret = 0;
7285                 goto out;
7286         }
7287
7288         if (WARN_ON(arvif->is_started)) {
7289                 ret = -EBUSY;
7290                 goto out;
7291         }
7292
7293         if (ab->hw_params.vdev_start_delay &&
7294             arvif->vdev_type != WMI_VDEV_TYPE_AP &&
7295             arvif->vdev_type != WMI_VDEV_TYPE_MONITOR) {
7296                 param.vdev_id = arvif->vdev_id;
7297                 param.peer_type = WMI_PEER_TYPE_DEFAULT;
7298                 param.peer_addr = ar->mac_addr;
7299
7300                 ret = ath11k_peer_create(ar, arvif, NULL, &param);
7301                 if (ret) {
7302                         ath11k_warn(ab, "failed to create peer after vdev start delay: %d",
7303                                     ret);
7304                         goto out;
7305                 }
7306         }
7307
7308         if (arvif->vdev_type == WMI_VDEV_TYPE_MONITOR) {
7309                 ret = ath11k_mac_monitor_start(ar);
7310                 if (ret) {
7311                         ath11k_warn(ar->ab, "failed to start monitor during vif channel context assignment: %d",
7312                                     ret);
7313                         goto out;
7314                 }
7315
7316                 arvif->is_started = true;
7317                 goto out;
7318         }
7319
7320         ret = ath11k_mac_vdev_start(arvif, ctx);
7321         if (ret) {
7322                 ath11k_warn(ab, "failed to start vdev %i addr %pM on freq %d: %d\n",
7323                             arvif->vdev_id, vif->addr,
7324                             ctx->def.chan->center_freq, ret);
7325                 goto out;
7326         }
7327
7328         arvif->is_started = true;
7329
7330         if (arvif->vdev_type != WMI_VDEV_TYPE_MONITOR &&
7331             test_bit(ATH11K_FLAG_MONITOR_VDEV_CREATED, &ar->monitor_flags)) {
7332                 ret = ath11k_mac_monitor_start(ar);
7333                 if (ret) {
7334                         ath11k_warn(ar->ab, "failed to start monitor during vif channel context assignment: %d",
7335                                     ret);
7336                         goto out;
7337                 }
7338         }
7339
7340         /* TODO: Setup ps and cts/rts protection */
7341
7342         ret = 0;
7343
7344 out:
7345         mutex_unlock(&ar->conf_mutex);
7346
7347         return ret;
7348 }
7349
7350 static void
7351 ath11k_mac_op_unassign_vif_chanctx(struct ieee80211_hw *hw,
7352                                    struct ieee80211_vif *vif,
7353                                    struct ieee80211_bss_conf *link_conf,
7354                                    struct ieee80211_chanctx_conf *ctx)
7355 {
7356         struct ath11k *ar = hw->priv;
7357         struct ath11k_base *ab = ar->ab;
7358         struct ath11k_vif *arvif = (void *)vif->drv_priv;
7359         struct ath11k_peer *peer;
7360         int ret;
7361
7362         mutex_lock(&ar->conf_mutex);
7363
7364         ath11k_dbg(ab, ATH11K_DBG_MAC,
7365                    "mac chanctx unassign ptr %pK vdev_id %i\n",
7366                    ctx, arvif->vdev_id);
7367
7368         WARN_ON(!arvif->is_started);
7369
7370         if (ab->hw_params.vdev_start_delay &&
7371             arvif->vdev_type == WMI_VDEV_TYPE_MONITOR) {
7372                 spin_lock_bh(&ab->base_lock);
7373                 peer = ath11k_peer_find_by_addr(ab, ar->mac_addr);
7374                 spin_unlock_bh(&ab->base_lock);
7375                 if (peer)
7376                         ath11k_peer_delete(ar, arvif->vdev_id, ar->mac_addr);
7377         }
7378
7379         if (arvif->vdev_type == WMI_VDEV_TYPE_MONITOR) {
7380                 ret = ath11k_mac_monitor_stop(ar);
7381                 if (ret) {
7382                         ath11k_warn(ar->ab, "failed to stop monitor during vif channel context unassignment: %d",
7383                                     ret);
7384                         mutex_unlock(&ar->conf_mutex);
7385                         return;
7386                 }
7387
7388                 arvif->is_started = false;
7389                 mutex_unlock(&ar->conf_mutex);
7390                 return;
7391         }
7392
7393         ret = ath11k_mac_vdev_stop(arvif);
7394         if (ret)
7395                 ath11k_warn(ab, "failed to stop vdev %i: %d\n",
7396                             arvif->vdev_id, ret);
7397
7398         arvif->is_started = false;
7399
7400         if (ab->hw_params.vdev_start_delay &&
7401             arvif->vdev_type == WMI_VDEV_TYPE_STA) {
7402                 ret = ath11k_peer_delete(ar, arvif->vdev_id, arvif->bssid);
7403                 if (ret)
7404                         ath11k_warn(ar->ab,
7405                                     "failed to delete peer %pM for vdev %d: %d\n",
7406                                     arvif->bssid, arvif->vdev_id, ret);
7407                 else
7408                         ath11k_dbg(ar->ab, ATH11K_DBG_MAC,
7409                                    "mac removed peer %pM  vdev %d after vdev stop\n",
7410                                    arvif->bssid, arvif->vdev_id);
7411         }
7412
7413         if (ab->hw_params.vdev_start_delay &&
7414             arvif->vdev_type == WMI_VDEV_TYPE_MONITOR)
7415                 ath11k_wmi_vdev_down(ar, arvif->vdev_id);
7416
7417         if (arvif->vdev_type != WMI_VDEV_TYPE_MONITOR &&
7418             ar->num_started_vdevs == 1 &&
7419             test_bit(ATH11K_FLAG_MONITOR_VDEV_CREATED, &ar->monitor_flags)) {
7420                 ret = ath11k_mac_monitor_stop(ar);
7421                 if (ret)
7422                         /* continue even if there's an error */
7423                         ath11k_warn(ar->ab, "failed to stop monitor during vif channel context unassignment: %d",
7424                                     ret);
7425         }
7426
7427         if (arvif->vdev_type == WMI_VDEV_TYPE_STA)
7428                 ath11k_mac_11d_scan_start(ar, arvif->vdev_id);
7429
7430         mutex_unlock(&ar->conf_mutex);
7431 }
7432
7433 static int
7434 ath11k_mac_op_switch_vif_chanctx(struct ieee80211_hw *hw,
7435                                  struct ieee80211_vif_chanctx_switch *vifs,
7436                                  int n_vifs,
7437                                  enum ieee80211_chanctx_switch_mode mode)
7438 {
7439         struct ath11k *ar = hw->priv;
7440
7441         mutex_lock(&ar->conf_mutex);
7442
7443         ath11k_dbg(ar->ab, ATH11K_DBG_MAC,
7444                    "mac chanctx switch n_vifs %d mode %d\n",
7445                    n_vifs, mode);
7446         ath11k_mac_update_vif_chan(ar, vifs, n_vifs);
7447
7448         mutex_unlock(&ar->conf_mutex);
7449
7450         return 0;
7451 }
7452
7453 static int
7454 ath11k_set_vdev_param_to_all_vifs(struct ath11k *ar, int param, u32 value)
7455 {
7456         struct ath11k_vif *arvif;
7457         int ret = 0;
7458
7459         mutex_lock(&ar->conf_mutex);
7460         list_for_each_entry(arvif, &ar->arvifs, list) {
7461                 ath11k_dbg(ar->ab, ATH11K_DBG_MAC, "setting mac vdev %d param %d value %d\n",
7462                            param, arvif->vdev_id, value);
7463
7464                 ret = ath11k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
7465                                                     param, value);
7466                 if (ret) {
7467                         ath11k_warn(ar->ab, "failed to set param %d for vdev %d: %d\n",
7468                                     param, arvif->vdev_id, ret);
7469                         break;
7470                 }
7471         }
7472         mutex_unlock(&ar->conf_mutex);
7473         return ret;
7474 }
7475
7476 /* mac80211 stores device specific RTS/Fragmentation threshold value,
7477  * this is set interface specific to firmware from ath11k driver
7478  */
7479 static int ath11k_mac_op_set_rts_threshold(struct ieee80211_hw *hw, u32 value)
7480 {
7481         struct ath11k *ar = hw->priv;
7482         int param_id = WMI_VDEV_PARAM_RTS_THRESHOLD;
7483
7484         return ath11k_set_vdev_param_to_all_vifs(ar, param_id, value);
7485 }
7486
7487 static int ath11k_mac_op_set_frag_threshold(struct ieee80211_hw *hw, u32 value)
7488 {
7489         /* Even though there's a WMI vdev param for fragmentation threshold no
7490          * known firmware actually implements it. Moreover it is not possible to
7491          * rely frame fragmentation to mac80211 because firmware clears the
7492          * "more fragments" bit in frame control making it impossible for remote
7493          * devices to reassemble frames.
7494          *
7495          * Hence implement a dummy callback just to say fragmentation isn't
7496          * supported. This effectively prevents mac80211 from doing frame
7497          * fragmentation in software.
7498          */
7499         return -EOPNOTSUPP;
7500 }
7501
7502 static int ath11k_mac_flush_tx_complete(struct ath11k *ar)
7503 {
7504         long time_left;
7505         int ret = 0;
7506
7507         time_left = wait_event_timeout(ar->dp.tx_empty_waitq,
7508                                        (atomic_read(&ar->dp.num_tx_pending) == 0),
7509                                        ATH11K_FLUSH_TIMEOUT);
7510         if (time_left == 0) {
7511                 ath11k_warn(ar->ab, "failed to flush transmit queue, data pkts pending %d\n",
7512                             atomic_read(&ar->dp.num_tx_pending));
7513                 ret = -ETIMEDOUT;
7514         }
7515
7516         time_left = wait_event_timeout(ar->txmgmt_empty_waitq,
7517                                        (atomic_read(&ar->num_pending_mgmt_tx) == 0),
7518                                        ATH11K_FLUSH_TIMEOUT);
7519         if (time_left == 0) {
7520                 ath11k_warn(ar->ab, "failed to flush mgmt transmit queue, mgmt pkts pending %d\n",
7521                             atomic_read(&ar->num_pending_mgmt_tx));
7522                 ret = -ETIMEDOUT;
7523         }
7524
7525         return ret;
7526 }
7527
7528 int ath11k_mac_wait_tx_complete(struct ath11k *ar)
7529 {
7530         ath11k_mac_drain_tx(ar);
7531         return ath11k_mac_flush_tx_complete(ar);
7532 }
7533
7534 static void ath11k_mac_op_flush(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
7535                                 u32 queues, bool drop)
7536 {
7537         struct ath11k *ar = hw->priv;
7538
7539         if (drop)
7540                 return;
7541
7542         ath11k_mac_flush_tx_complete(ar);
7543 }
7544
7545 static int
7546 ath11k_mac_bitrate_mask_num_ht_rates(struct ath11k *ar,
7547                                      enum nl80211_band band,
7548                                      const struct cfg80211_bitrate_mask *mask)
7549 {
7550         int num_rates = 0;
7551         int i;
7552
7553         for (i = 0; i < ARRAY_SIZE(mask->control[band].ht_mcs); i++)
7554                 num_rates += hweight16(mask->control[band].ht_mcs[i]);
7555
7556         return num_rates;
7557 }
7558
7559 static bool
7560 ath11k_mac_has_single_legacy_rate(struct ath11k *ar,
7561                                   enum nl80211_band band,
7562                                   const struct cfg80211_bitrate_mask *mask)
7563 {
7564         int num_rates = 0;
7565
7566         num_rates = hweight32(mask->control[band].legacy);
7567
7568         if (ath11k_mac_bitrate_mask_num_ht_rates(ar, band, mask))
7569                 return false;
7570
7571         if (ath11k_mac_bitrate_mask_num_vht_rates(ar, band, mask))
7572                 return false;
7573
7574         if (ath11k_mac_bitrate_mask_num_he_rates(ar, band, mask))
7575                 return false;
7576
7577         return num_rates == 1;
7578 }
7579
7580 static __le16
7581 ath11k_mac_get_tx_mcs_map(const struct ieee80211_sta_he_cap *he_cap)
7582 {
7583         if (he_cap->he_cap_elem.phy_cap_info[0] &
7584             IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_80PLUS80_MHZ_IN_5G)
7585                 return he_cap->he_mcs_nss_supp.tx_mcs_80p80;
7586
7587         if (he_cap->he_cap_elem.phy_cap_info[0] &
7588             IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_160MHZ_IN_5G)
7589                 return he_cap->he_mcs_nss_supp.tx_mcs_160;
7590
7591         return he_cap->he_mcs_nss_supp.tx_mcs_80;
7592 }
7593
7594 static bool
7595 ath11k_mac_bitrate_mask_get_single_nss(struct ath11k *ar,
7596                                        enum nl80211_band band,
7597                                        const struct cfg80211_bitrate_mask *mask,
7598                                        int *nss)
7599 {
7600         struct ieee80211_supported_band *sband = &ar->mac.sbands[band];
7601         u16 vht_mcs_map = le16_to_cpu(sband->vht_cap.vht_mcs.tx_mcs_map);
7602         u16 he_mcs_map = 0;
7603         u8 ht_nss_mask = 0;
7604         u8 vht_nss_mask = 0;
7605         u8 he_nss_mask = 0;
7606         int i;
7607
7608         /* No need to consider legacy here. Basic rates are always present
7609          * in bitrate mask
7610          */
7611
7612         for (i = 0; i < ARRAY_SIZE(mask->control[band].ht_mcs); i++) {
7613                 if (mask->control[band].ht_mcs[i] == 0)
7614                         continue;
7615                 else if (mask->control[band].ht_mcs[i] ==
7616                          sband->ht_cap.mcs.rx_mask[i])
7617                         ht_nss_mask |= BIT(i);
7618                 else
7619                         return false;
7620         }
7621
7622         for (i = 0; i < ARRAY_SIZE(mask->control[band].vht_mcs); i++) {
7623                 if (mask->control[band].vht_mcs[i] == 0)
7624                         continue;
7625                 else if (mask->control[band].vht_mcs[i] ==
7626                          ath11k_mac_get_max_vht_mcs_map(vht_mcs_map, i))
7627                         vht_nss_mask |= BIT(i);
7628                 else
7629                         return false;
7630         }
7631
7632         he_mcs_map = le16_to_cpu(ath11k_mac_get_tx_mcs_map(&sband->iftype_data->he_cap));
7633
7634         for (i = 0; i < ARRAY_SIZE(mask->control[band].he_mcs); i++) {
7635                 if (mask->control[band].he_mcs[i] == 0)
7636                         continue;
7637
7638                 if (mask->control[band].he_mcs[i] ==
7639                     ath11k_mac_get_max_he_mcs_map(he_mcs_map, i))
7640                         he_nss_mask |= BIT(i);
7641                 else
7642                         return false;
7643         }
7644
7645         if (ht_nss_mask != vht_nss_mask || ht_nss_mask != he_nss_mask)
7646                 return false;
7647
7648         if (ht_nss_mask == 0)
7649                 return false;
7650
7651         if (BIT(fls(ht_nss_mask)) - 1 != ht_nss_mask)
7652                 return false;
7653
7654         *nss = fls(ht_nss_mask);
7655
7656         return true;
7657 }
7658
7659 static int
7660 ath11k_mac_get_single_legacy_rate(struct ath11k *ar,
7661                                   enum nl80211_band band,
7662                                   const struct cfg80211_bitrate_mask *mask,
7663                                   u32 *rate, u8 *nss)
7664 {
7665         int rate_idx;
7666         u16 bitrate;
7667         u8 preamble;
7668         u8 hw_rate;
7669
7670         if (hweight32(mask->control[band].legacy) != 1)
7671                 return -EINVAL;
7672
7673         rate_idx = ffs(mask->control[band].legacy) - 1;
7674
7675         if (band == NL80211_BAND_5GHZ || band == NL80211_BAND_6GHZ)
7676                 rate_idx += ATH11K_MAC_FIRST_OFDM_RATE_IDX;
7677
7678         hw_rate = ath11k_legacy_rates[rate_idx].hw_value;
7679         bitrate = ath11k_legacy_rates[rate_idx].bitrate;
7680
7681         if (ath11k_mac_bitrate_is_cck(bitrate))
7682                 preamble = WMI_RATE_PREAMBLE_CCK;
7683         else
7684                 preamble = WMI_RATE_PREAMBLE_OFDM;
7685
7686         *nss = 1;
7687         *rate = ATH11K_HW_RATE_CODE(hw_rate, 0, preamble);
7688
7689         return 0;
7690 }
7691
7692 static int
7693 ath11k_mac_set_fixed_rate_gi_ltf(struct ath11k_vif *arvif, u8 he_gi, u8 he_ltf)
7694 {
7695         struct ath11k *ar = arvif->ar;
7696         int ret;
7697
7698         /* 0.8 = 0, 1.6 = 2 and 3.2 = 3. */
7699         if (he_gi && he_gi != 0xFF)
7700                 he_gi += 1;
7701
7702         ret = ath11k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
7703                                             WMI_VDEV_PARAM_SGI, he_gi);
7704         if (ret) {
7705                 ath11k_warn(ar->ab, "failed to set he gi %d: %d\n",
7706                             he_gi, ret);
7707                 return ret;
7708         }
7709         /* start from 1 */
7710         if (he_ltf != 0xFF)
7711                 he_ltf += 1;
7712
7713         ret = ath11k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
7714                                             WMI_VDEV_PARAM_HE_LTF, he_ltf);
7715         if (ret) {
7716                 ath11k_warn(ar->ab, "failed to set he ltf %d: %d\n",
7717                             he_ltf, ret);
7718                 return ret;
7719         }
7720
7721         return 0;
7722 }
7723
7724 static int
7725 ath11k_mac_set_auto_rate_gi_ltf(struct ath11k_vif *arvif, u16 he_gi, u8 he_ltf)
7726 {
7727         struct ath11k *ar = arvif->ar;
7728         int ret;
7729         u32 he_ar_gi_ltf;
7730
7731         if (he_gi != 0xFF) {
7732                 switch (he_gi) {
7733                 case NL80211_RATE_INFO_HE_GI_0_8:
7734                         he_gi = WMI_AUTORATE_800NS_GI;
7735                         break;
7736                 case NL80211_RATE_INFO_HE_GI_1_6:
7737                         he_gi = WMI_AUTORATE_1600NS_GI;
7738                         break;
7739                 case NL80211_RATE_INFO_HE_GI_3_2:
7740                         he_gi = WMI_AUTORATE_3200NS_GI;
7741                         break;
7742                 default:
7743                         ath11k_warn(ar->ab, "invalid he gi: %d\n", he_gi);
7744                         return -EINVAL;
7745                 }
7746         }
7747
7748         if (he_ltf != 0xFF) {
7749                 switch (he_ltf) {
7750                 case NL80211_RATE_INFO_HE_1XLTF:
7751                         he_ltf = WMI_HE_AUTORATE_LTF_1X;
7752                         break;
7753                 case NL80211_RATE_INFO_HE_2XLTF:
7754                         he_ltf = WMI_HE_AUTORATE_LTF_2X;
7755                         break;
7756                 case NL80211_RATE_INFO_HE_4XLTF:
7757                         he_ltf = WMI_HE_AUTORATE_LTF_4X;
7758                         break;
7759                 default:
7760                         ath11k_warn(ar->ab, "invalid he ltf: %d\n", he_ltf);
7761                         return -EINVAL;
7762                 }
7763         }
7764
7765         he_ar_gi_ltf = he_gi | he_ltf;
7766         ret = ath11k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
7767                                             WMI_VDEV_PARAM_AUTORATE_MISC_CFG,
7768                                             he_ar_gi_ltf);
7769         if (ret) {
7770                 ath11k_warn(ar->ab,
7771                             "failed to set he autorate gi %u ltf %u: %d\n",
7772                             he_gi, he_ltf, ret);
7773                 return ret;
7774         }
7775
7776         return 0;
7777 }
7778
7779 static int ath11k_mac_set_rate_params(struct ath11k_vif *arvif,
7780                                       u32 rate, u8 nss, u8 sgi, u8 ldpc,
7781                                       u8 he_gi, u8 he_ltf, bool he_fixed_rate)
7782 {
7783         struct ath11k *ar = arvif->ar;
7784         u32 vdev_param;
7785         int ret;
7786
7787         lockdep_assert_held(&ar->conf_mutex);
7788
7789         ath11k_dbg(ar->ab, ATH11K_DBG_MAC,
7790                    "mac set rate params vdev %i rate 0x%02x nss 0x%02x sgi 0x%02x ldpc 0x%02x he_gi 0x%02x he_ltf 0x%02x he_fixed_rate %d\n",
7791                    arvif->vdev_id, rate, nss, sgi, ldpc, he_gi,
7792                    he_ltf, he_fixed_rate);
7793
7794         if (!arvif->vif->bss_conf.he_support) {
7795                 vdev_param = WMI_VDEV_PARAM_FIXED_RATE;
7796                 ret = ath11k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
7797                                                     vdev_param, rate);
7798                 if (ret) {
7799                         ath11k_warn(ar->ab, "failed to set fixed rate param 0x%02x: %d\n",
7800                                     rate, ret);
7801                         return ret;
7802                 }
7803         }
7804
7805         vdev_param = WMI_VDEV_PARAM_NSS;
7806         ret = ath11k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
7807                                             vdev_param, nss);
7808         if (ret) {
7809                 ath11k_warn(ar->ab, "failed to set nss param %d: %d\n",
7810                             nss, ret);
7811                 return ret;
7812         }
7813
7814         vdev_param = WMI_VDEV_PARAM_LDPC;
7815         ret = ath11k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
7816                                             vdev_param, ldpc);
7817         if (ret) {
7818                 ath11k_warn(ar->ab, "failed to set ldpc param %d: %d\n",
7819                             ldpc, ret);
7820                 return ret;
7821         }
7822
7823         if (arvif->vif->bss_conf.he_support) {
7824                 if (he_fixed_rate) {
7825                         ret = ath11k_mac_set_fixed_rate_gi_ltf(arvif, he_gi,
7826                                                                he_ltf);
7827                         if (ret) {
7828                                 ath11k_warn(ar->ab, "failed to set fixed rate gi ltf: %d\n",
7829                                             ret);
7830                                 return ret;
7831                         }
7832                 } else {
7833                         ret = ath11k_mac_set_auto_rate_gi_ltf(arvif, he_gi,
7834                                                               he_ltf);
7835                         if (ret) {
7836                                 ath11k_warn(ar->ab, "failed to set auto rate gi ltf: %d\n",
7837                                             ret);
7838                                 return ret;
7839                         }
7840                 }
7841         } else {
7842                 vdev_param = WMI_VDEV_PARAM_SGI;
7843                 ret = ath11k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
7844                                                     vdev_param, sgi);
7845                 if (ret) {
7846                         ath11k_warn(ar->ab, "failed to set sgi param %d: %d\n",
7847                                     sgi, ret);
7848                         return ret;
7849                 }
7850         }
7851
7852         return 0;
7853 }
7854
7855 static bool
7856 ath11k_mac_vht_mcs_range_present(struct ath11k *ar,
7857                                  enum nl80211_band band,
7858                                  const struct cfg80211_bitrate_mask *mask)
7859 {
7860         int i;
7861         u16 vht_mcs;
7862
7863         for (i = 0; i < NL80211_VHT_NSS_MAX; i++) {
7864                 vht_mcs = mask->control[band].vht_mcs[i];
7865
7866                 switch (vht_mcs) {
7867                 case 0:
7868                 case BIT(8) - 1:
7869                 case BIT(9) - 1:
7870                 case BIT(10) - 1:
7871                         break;
7872                 default:
7873                         return false;
7874                 }
7875         }
7876
7877         return true;
7878 }
7879
7880 static bool
7881 ath11k_mac_he_mcs_range_present(struct ath11k *ar,
7882                                 enum nl80211_band band,
7883                                 const struct cfg80211_bitrate_mask *mask)
7884 {
7885         int i;
7886         u16 he_mcs;
7887
7888         for (i = 0; i < NL80211_HE_NSS_MAX; i++) {
7889                 he_mcs = mask->control[band].he_mcs[i];
7890
7891                 switch (he_mcs) {
7892                 case 0:
7893                 case BIT(8) - 1:
7894                 case BIT(10) - 1:
7895                 case BIT(12) - 1:
7896                         break;
7897                 default:
7898                         return false;
7899                 }
7900         }
7901
7902         return true;
7903 }
7904
7905 static void ath11k_mac_set_bitrate_mask_iter(void *data,
7906                                              struct ieee80211_sta *sta)
7907 {
7908         struct ath11k_vif *arvif = data;
7909         struct ath11k_sta *arsta = (struct ath11k_sta *)sta->drv_priv;
7910         struct ath11k *ar = arvif->ar;
7911
7912         spin_lock_bh(&ar->data_lock);
7913         arsta->changed |= IEEE80211_RC_SUPP_RATES_CHANGED;
7914         spin_unlock_bh(&ar->data_lock);
7915
7916         ieee80211_queue_work(ar->hw, &arsta->update_wk);
7917 }
7918
7919 static void ath11k_mac_disable_peer_fixed_rate(void *data,
7920                                                struct ieee80211_sta *sta)
7921 {
7922         struct ath11k_vif *arvif = data;
7923         struct ath11k *ar = arvif->ar;
7924         int ret;
7925
7926         ret = ath11k_wmi_set_peer_param(ar, sta->addr,
7927                                         arvif->vdev_id,
7928                                         WMI_PEER_PARAM_FIXED_RATE,
7929                                         WMI_FIXED_RATE_NONE);
7930         if (ret)
7931                 ath11k_warn(ar->ab,
7932                             "failed to disable peer fixed rate for STA %pM ret %d\n",
7933                             sta->addr, ret);
7934 }
7935
7936 static bool
7937 ath11k_mac_validate_vht_he_fixed_rate_settings(struct ath11k *ar, enum nl80211_band band,
7938                                                const struct cfg80211_bitrate_mask *mask)
7939 {
7940         bool he_fixed_rate = false, vht_fixed_rate = false;
7941         struct ath11k_peer *peer, *tmp;
7942         const u16 *vht_mcs_mask, *he_mcs_mask;
7943         struct ieee80211_link_sta *deflink;
7944         u8 vht_nss, he_nss;
7945         bool ret = true;
7946
7947         vht_mcs_mask = mask->control[band].vht_mcs;
7948         he_mcs_mask = mask->control[band].he_mcs;
7949
7950         if (ath11k_mac_bitrate_mask_num_vht_rates(ar, band, mask) == 1)
7951                 vht_fixed_rate = true;
7952
7953         if (ath11k_mac_bitrate_mask_num_he_rates(ar, band, mask) == 1)
7954                 he_fixed_rate = true;
7955
7956         if (!vht_fixed_rate && !he_fixed_rate)
7957                 return true;
7958
7959         vht_nss = ath11k_mac_max_vht_nss(vht_mcs_mask);
7960         he_nss =  ath11k_mac_max_he_nss(he_mcs_mask);
7961
7962         rcu_read_lock();
7963         spin_lock_bh(&ar->ab->base_lock);
7964         list_for_each_entry_safe(peer, tmp, &ar->ab->peers, list) {
7965                 if (peer->sta) {
7966                         deflink = &peer->sta->deflink;
7967
7968                         if (vht_fixed_rate && (!deflink->vht_cap.vht_supported ||
7969                                                deflink->rx_nss < vht_nss)) {
7970                                 ret = false;
7971                                 goto out;
7972                         }
7973
7974                         if (he_fixed_rate && (!deflink->he_cap.has_he ||
7975                                               deflink->rx_nss < he_nss)) {
7976                                 ret = false;
7977                                 goto out;
7978                         }
7979                 }
7980         }
7981
7982 out:
7983         spin_unlock_bh(&ar->ab->base_lock);
7984         rcu_read_unlock();
7985         return ret;
7986 }
7987
7988 static int
7989 ath11k_mac_op_set_bitrate_mask(struct ieee80211_hw *hw,
7990                                struct ieee80211_vif *vif,
7991                                const struct cfg80211_bitrate_mask *mask)
7992 {
7993         struct ath11k_vif *arvif = (void *)vif->drv_priv;
7994         struct cfg80211_chan_def def;
7995         struct ath11k_pdev_cap *cap;
7996         struct ath11k *ar = arvif->ar;
7997         enum nl80211_band band;
7998         const u8 *ht_mcs_mask;
7999         const u16 *vht_mcs_mask;
8000         const u16 *he_mcs_mask;
8001         u8 he_ltf = 0;
8002         u8 he_gi = 0;
8003         u32 rate;
8004         u8 nss;
8005         u8 sgi;
8006         u8 ldpc;
8007         int single_nss;
8008         int ret;
8009         int num_rates;
8010         bool he_fixed_rate = false;
8011
8012         if (ath11k_mac_vif_chan(vif, &def))
8013                 return -EPERM;
8014
8015         band = def.chan->band;
8016         cap = &ar->pdev->cap;
8017         ht_mcs_mask = mask->control[band].ht_mcs;
8018         vht_mcs_mask = mask->control[band].vht_mcs;
8019         he_mcs_mask = mask->control[band].he_mcs;
8020         ldpc = !!(cap->band[band].ht_cap_info & WMI_HT_CAP_TX_LDPC);
8021
8022         sgi = mask->control[band].gi;
8023         if (sgi == NL80211_TXRATE_FORCE_LGI)
8024                 return -EINVAL;
8025
8026         he_gi = mask->control[band].he_gi;
8027         he_ltf = mask->control[band].he_ltf;
8028
8029         /* mac80211 doesn't support sending a fixed HT/VHT MCS alone, rather it
8030          * requires passing at least one of used basic rates along with them.
8031          * Fixed rate setting across different preambles(legacy, HT, VHT) is
8032          * not supported by the FW. Hence use of FIXED_RATE vdev param is not
8033          * suitable for setting single HT/VHT rates.
8034          * But, there could be a single basic rate passed from userspace which
8035          * can be done through the FIXED_RATE param.
8036          */
8037         if (ath11k_mac_has_single_legacy_rate(ar, band, mask)) {
8038                 ret = ath11k_mac_get_single_legacy_rate(ar, band, mask, &rate,
8039                                                         &nss);
8040                 if (ret) {
8041                         ath11k_warn(ar->ab, "failed to get single legacy rate for vdev %i: %d\n",
8042                                     arvif->vdev_id, ret);
8043                         return ret;
8044                 }
8045                 ieee80211_iterate_stations_atomic(ar->hw,
8046                                                   ath11k_mac_disable_peer_fixed_rate,
8047                                                   arvif);
8048         } else if (ath11k_mac_bitrate_mask_get_single_nss(ar, band, mask,
8049                                                           &single_nss)) {
8050                 rate = WMI_FIXED_RATE_NONE;
8051                 nss = single_nss;
8052                 mutex_lock(&ar->conf_mutex);
8053                 arvif->bitrate_mask = *mask;
8054                 ieee80211_iterate_stations_atomic(ar->hw,
8055                                                   ath11k_mac_set_bitrate_mask_iter,
8056                                                   arvif);
8057                 mutex_unlock(&ar->conf_mutex);
8058         } else {
8059                 rate = WMI_FIXED_RATE_NONE;
8060
8061                 if (!ath11k_mac_validate_vht_he_fixed_rate_settings(ar, band, mask))
8062                         ath11k_warn(ar->ab,
8063                                     "could not update fixed rate settings to all peers due to mcs/nss incompatibility\n");
8064                 nss = min_t(u32, ar->num_tx_chains,
8065                             max(max(ath11k_mac_max_ht_nss(ht_mcs_mask),
8066                                     ath11k_mac_max_vht_nss(vht_mcs_mask)),
8067                                 ath11k_mac_max_he_nss(he_mcs_mask)));
8068
8069                 /* If multiple rates across different preambles are given
8070                  * we can reconfigure this info with all peers using PEER_ASSOC
8071                  * command with the below exception cases.
8072                  * - Single VHT Rate : peer_assoc command accommodates only MCS
8073                  * range values i.e 0-7, 0-8, 0-9 for VHT. Though mac80211
8074                  * mandates passing basic rates along with HT/VHT rates, FW
8075                  * doesn't allow switching from VHT to Legacy. Hence instead of
8076                  * setting legacy and VHT rates using RATEMASK_CMD vdev cmd,
8077                  * we could set this VHT rate as peer fixed rate param, which
8078                  * will override FIXED rate and FW rate control algorithm.
8079                  * If single VHT rate is passed along with HT rates, we select
8080                  * the VHT rate as fixed rate for vht peers.
8081                  * - Multiple VHT Rates : When Multiple VHT rates are given,this
8082                  * can be set using RATEMASK CMD which uses FW rate-ctl alg.
8083                  * TODO: Setting multiple VHT MCS and replacing peer_assoc with
8084                  * RATEMASK_CMDID can cover all use cases of setting rates
8085                  * across multiple preambles and rates within same type.
8086                  * But requires more validation of the command at this point.
8087                  */
8088
8089                 num_rates = ath11k_mac_bitrate_mask_num_vht_rates(ar, band,
8090                                                                   mask);
8091
8092                 if (!ath11k_mac_vht_mcs_range_present(ar, band, mask) &&
8093                     num_rates > 1) {
8094                         /* TODO: Handle multiple VHT MCS values setting using
8095                          * RATEMASK CMD
8096                          */
8097                         ath11k_warn(ar->ab,
8098                                     "setting %d mcs values in bitrate mask not supported\n",
8099                                 num_rates);
8100                         return -EINVAL;
8101                 }
8102
8103                 num_rates = ath11k_mac_bitrate_mask_num_he_rates(ar, band,
8104                                                                  mask);
8105                 if (num_rates == 1)
8106                         he_fixed_rate = true;
8107
8108                 if (!ath11k_mac_he_mcs_range_present(ar, band, mask) &&
8109                     num_rates > 1) {
8110                         ath11k_warn(ar->ab,
8111                                     "Setting more than one HE MCS Value in bitrate mask not supported\n");
8112                         return -EINVAL;
8113                 }
8114
8115                 mutex_lock(&ar->conf_mutex);
8116                 ieee80211_iterate_stations_atomic(ar->hw,
8117                                                   ath11k_mac_disable_peer_fixed_rate,
8118                                                   arvif);
8119
8120                 arvif->bitrate_mask = *mask;
8121                 ieee80211_iterate_stations_atomic(ar->hw,
8122                                                   ath11k_mac_set_bitrate_mask_iter,
8123                                                   arvif);
8124
8125                 mutex_unlock(&ar->conf_mutex);
8126         }
8127
8128         mutex_lock(&ar->conf_mutex);
8129
8130         ret = ath11k_mac_set_rate_params(arvif, rate, nss, sgi, ldpc, he_gi,
8131                                          he_ltf, he_fixed_rate);
8132         if (ret) {
8133                 ath11k_warn(ar->ab, "failed to set rate params on vdev %i: %d\n",
8134                             arvif->vdev_id, ret);
8135         }
8136
8137         mutex_unlock(&ar->conf_mutex);
8138
8139         return ret;
8140 }
8141
8142 static void
8143 ath11k_mac_op_reconfig_complete(struct ieee80211_hw *hw,
8144                                 enum ieee80211_reconfig_type reconfig_type)
8145 {
8146         struct ath11k *ar = hw->priv;
8147         struct ath11k_base *ab = ar->ab;
8148         int recovery_count;
8149         struct ath11k_vif *arvif;
8150
8151         if (reconfig_type != IEEE80211_RECONFIG_TYPE_RESTART)
8152                 return;
8153
8154         mutex_lock(&ar->conf_mutex);
8155
8156         if (ar->state == ATH11K_STATE_RESTARTED) {
8157                 ath11k_warn(ar->ab, "pdev %d successfully recovered\n",
8158                             ar->pdev->pdev_id);
8159                 ar->state = ATH11K_STATE_ON;
8160                 ieee80211_wake_queues(ar->hw);
8161
8162                 if (ar->ab->hw_params.current_cc_support &&
8163                     ar->alpha2[0] != 0 && ar->alpha2[1] != 0) {
8164                         struct wmi_set_current_country_params set_current_param = {};
8165
8166                         memcpy(&set_current_param.alpha2, ar->alpha2, 2);
8167                         ath11k_wmi_send_set_current_country_cmd(ar, &set_current_param);
8168                 }
8169
8170                 if (ab->is_reset) {
8171                         recovery_count = atomic_inc_return(&ab->recovery_count);
8172                         ath11k_dbg(ab, ATH11K_DBG_BOOT,
8173                                    "recovery count %d\n", recovery_count);
8174                         /* When there are multiple radios in an SOC,
8175                          * the recovery has to be done for each radio
8176                          */
8177                         if (recovery_count == ab->num_radios) {
8178                                 atomic_dec(&ab->reset_count);
8179                                 complete(&ab->reset_complete);
8180                                 ab->is_reset = false;
8181                                 atomic_set(&ab->fail_cont_count, 0);
8182                                 ath11k_dbg(ab, ATH11K_DBG_BOOT, "reset success\n");
8183                         }
8184                 }
8185                 if (ar->ab->hw_params.support_fw_mac_sequence) {
8186                         list_for_each_entry(arvif, &ar->arvifs, list) {
8187                                 if (arvif->is_up && arvif->vdev_type == WMI_VDEV_TYPE_STA)
8188                                         ieee80211_hw_restart_disconnect(arvif->vif);
8189                         }
8190                 }
8191         }
8192
8193         mutex_unlock(&ar->conf_mutex);
8194 }
8195
8196 static void
8197 ath11k_mac_update_bss_chan_survey(struct ath11k *ar,
8198                                   struct ieee80211_channel *channel)
8199 {
8200         int ret;
8201         enum wmi_bss_chan_info_req_type type = WMI_BSS_SURVEY_REQ_TYPE_READ;
8202
8203         lockdep_assert_held(&ar->conf_mutex);
8204
8205         if (!test_bit(WMI_TLV_SERVICE_BSS_CHANNEL_INFO_64, ar->ab->wmi_ab.svc_map) ||
8206             ar->rx_channel != channel)
8207                 return;
8208
8209         if (ar->scan.state != ATH11K_SCAN_IDLE) {
8210                 ath11k_dbg(ar->ab, ATH11K_DBG_MAC,
8211                            "ignoring bss chan info req while scanning..\n");
8212                 return;
8213         }
8214
8215         reinit_completion(&ar->bss_survey_done);
8216
8217         ret = ath11k_wmi_pdev_bss_chan_info_request(ar, type);
8218         if (ret) {
8219                 ath11k_warn(ar->ab, "failed to send pdev bss chan info request\n");
8220                 return;
8221         }
8222
8223         ret = wait_for_completion_timeout(&ar->bss_survey_done, 3 * HZ);
8224         if (ret == 0)
8225                 ath11k_warn(ar->ab, "bss channel survey timed out\n");
8226 }
8227
8228 static int ath11k_mac_op_get_survey(struct ieee80211_hw *hw, int idx,
8229                                     struct survey_info *survey)
8230 {
8231         struct ath11k *ar = hw->priv;
8232         struct ieee80211_supported_band *sband;
8233         struct survey_info *ar_survey;
8234         int ret = 0;
8235
8236         if (idx >= ATH11K_NUM_CHANS)
8237                 return -ENOENT;
8238
8239         ar_survey = &ar->survey[idx];
8240
8241         mutex_lock(&ar->conf_mutex);
8242
8243         sband = hw->wiphy->bands[NL80211_BAND_2GHZ];
8244         if (sband && idx >= sband->n_channels) {
8245                 idx -= sband->n_channels;
8246                 sband = NULL;
8247         }
8248
8249         if (!sband)
8250                 sband = hw->wiphy->bands[NL80211_BAND_5GHZ];
8251         if (sband && idx >= sband->n_channels) {
8252                 idx -= sband->n_channels;
8253                 sband = NULL;
8254         }
8255
8256         if (!sband)
8257                 sband = hw->wiphy->bands[NL80211_BAND_6GHZ];
8258         if (!sband || idx >= sband->n_channels) {
8259                 ret = -ENOENT;
8260                 goto exit;
8261         }
8262
8263         ath11k_mac_update_bss_chan_survey(ar, &sband->channels[idx]);
8264
8265         spin_lock_bh(&ar->data_lock);
8266         memcpy(survey, ar_survey, sizeof(*survey));
8267         spin_unlock_bh(&ar->data_lock);
8268
8269         survey->channel = &sband->channels[idx];
8270
8271         if (ar->rx_channel == survey->channel)
8272                 survey->filled |= SURVEY_INFO_IN_USE;
8273
8274 exit:
8275         mutex_unlock(&ar->conf_mutex);
8276         return ret;
8277 }
8278
8279 static void ath11k_mac_put_chain_rssi(struct station_info *sinfo,
8280                                       struct ath11k_sta *arsta,
8281                                       char *pre,
8282                                       bool clear)
8283 {
8284         struct ath11k *ar = arsta->arvif->ar;
8285         int i;
8286         s8 rssi;
8287
8288         for (i = 0; i < ARRAY_SIZE(sinfo->chain_signal); i++) {
8289                 sinfo->chains &= ~BIT(i);
8290                 rssi = arsta->chain_signal[i];
8291                 if (clear)
8292                         arsta->chain_signal[i] = ATH11K_INVALID_RSSI_FULL;
8293
8294                 ath11k_dbg(ar->ab, ATH11K_DBG_MAC,
8295                            "mac sta statistics %s rssi[%d] %d\n", pre, i, rssi);
8296
8297                 if (rssi != ATH11K_DEFAULT_NOISE_FLOOR &&
8298                     rssi != ATH11K_INVALID_RSSI_FULL &&
8299                     rssi != ATH11K_INVALID_RSSI_EMPTY &&
8300                     rssi != 0) {
8301                         sinfo->chain_signal[i] = rssi;
8302                         sinfo->chains |= BIT(i);
8303                         sinfo->filled |= BIT_ULL(NL80211_STA_INFO_CHAIN_SIGNAL);
8304                 }
8305         }
8306 }
8307
8308 static void ath11k_mac_op_sta_statistics(struct ieee80211_hw *hw,
8309                                          struct ieee80211_vif *vif,
8310                                          struct ieee80211_sta *sta,
8311                                          struct station_info *sinfo)
8312 {
8313         struct ath11k_sta *arsta = (struct ath11k_sta *)sta->drv_priv;
8314         struct ath11k *ar = arsta->arvif->ar;
8315         s8 signal;
8316         bool db2dbm = test_bit(WMI_TLV_SERVICE_HW_DB2DBM_CONVERSION_SUPPORT,
8317                                ar->ab->wmi_ab.svc_map);
8318
8319         sinfo->rx_duration = arsta->rx_duration;
8320         sinfo->filled |= BIT_ULL(NL80211_STA_INFO_RX_DURATION);
8321
8322         sinfo->tx_duration = arsta->tx_duration;
8323         sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_DURATION);
8324
8325         if (arsta->txrate.legacy || arsta->txrate.nss) {
8326                 if (arsta->txrate.legacy) {
8327                         sinfo->txrate.legacy = arsta->txrate.legacy;
8328                 } else {
8329                         sinfo->txrate.mcs = arsta->txrate.mcs;
8330                         sinfo->txrate.nss = arsta->txrate.nss;
8331                         sinfo->txrate.bw = arsta->txrate.bw;
8332                         sinfo->txrate.he_gi = arsta->txrate.he_gi;
8333                         sinfo->txrate.he_dcm = arsta->txrate.he_dcm;
8334                         sinfo->txrate.he_ru_alloc = arsta->txrate.he_ru_alloc;
8335                 }
8336                 sinfo->txrate.flags = arsta->txrate.flags;
8337                 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_BITRATE);
8338         }
8339
8340         ath11k_mac_put_chain_rssi(sinfo, arsta, "ppdu", false);
8341
8342         if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_CHAIN_SIGNAL)) &&
8343             arsta->arvif->vdev_type == WMI_VDEV_TYPE_STA &&
8344             ar->ab->hw_params.supports_rssi_stats &&
8345             !ath11k_debugfs_get_fw_stats(ar, ar->pdev->pdev_id, 0,
8346                                          WMI_REQUEST_RSSI_PER_CHAIN_STAT)) {
8347                 ath11k_mac_put_chain_rssi(sinfo, arsta, "fw stats", true);
8348         }
8349
8350         signal = arsta->rssi_comb;
8351         if (!signal &&
8352             arsta->arvif->vdev_type == WMI_VDEV_TYPE_STA &&
8353             ar->ab->hw_params.supports_rssi_stats &&
8354             !(ath11k_debugfs_get_fw_stats(ar, ar->pdev->pdev_id, 0,
8355                                         WMI_REQUEST_VDEV_STAT)))
8356                 signal = arsta->rssi_beacon;
8357
8358         ath11k_dbg(ar->ab, ATH11K_DBG_MAC,
8359                    "mac sta statistics db2dbm %u rssi comb %d rssi beacon %d\n",
8360                    db2dbm, arsta->rssi_comb, arsta->rssi_beacon);
8361
8362         if (signal) {
8363                 sinfo->signal = db2dbm ? signal : signal + ATH11K_DEFAULT_NOISE_FLOOR;
8364                 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_SIGNAL);
8365         }
8366
8367         sinfo->signal_avg = ewma_avg_rssi_read(&arsta->avg_rssi) +
8368                 ATH11K_DEFAULT_NOISE_FLOOR;
8369         sinfo->filled |= BIT_ULL(NL80211_STA_INFO_SIGNAL_AVG);
8370 }
8371
8372 #if IS_ENABLED(CONFIG_IPV6)
8373 static void ath11k_generate_ns_mc_addr(struct ath11k *ar,
8374                                        struct ath11k_arp_ns_offload *offload)
8375 {
8376         int i;
8377
8378         for (i = 0; i < offload->ipv6_count; i++) {
8379                 offload->self_ipv6_addr[i][0] = 0xff;
8380                 offload->self_ipv6_addr[i][1] = 0x02;
8381                 offload->self_ipv6_addr[i][11] = 0x01;
8382                 offload->self_ipv6_addr[i][12] = 0xff;
8383                 offload->self_ipv6_addr[i][13] =
8384                                         offload->ipv6_addr[i][13];
8385                 offload->self_ipv6_addr[i][14] =
8386                                         offload->ipv6_addr[i][14];
8387                 offload->self_ipv6_addr[i][15] =
8388                                         offload->ipv6_addr[i][15];
8389                 ath11k_dbg(ar->ab, ATH11K_DBG_MAC, "NS solicited addr %pI6\n",
8390                            offload->self_ipv6_addr[i]);
8391         }
8392 }
8393
8394 static void ath11k_mac_op_ipv6_changed(struct ieee80211_hw *hw,
8395                                        struct ieee80211_vif *vif,
8396                                        struct inet6_dev *idev)
8397 {
8398         struct ath11k *ar = hw->priv;
8399         struct ath11k_arp_ns_offload *offload;
8400         struct ath11k_vif *arvif = ath11k_vif_to_arvif(vif);
8401         struct inet6_ifaddr *ifa6;
8402         struct ifacaddr6 *ifaca6;
8403         struct list_head *p;
8404         u32 count, scope;
8405
8406         ath11k_dbg(ar->ab, ATH11K_DBG_MAC, "mac op ipv6 changed\n");
8407
8408         offload = &arvif->arp_ns_offload;
8409         count = 0;
8410
8411         read_lock_bh(&idev->lock);
8412
8413         memset(offload->ipv6_addr, 0, sizeof(offload->ipv6_addr));
8414         memset(offload->self_ipv6_addr, 0, sizeof(offload->self_ipv6_addr));
8415         memcpy(offload->mac_addr, vif->addr, ETH_ALEN);
8416
8417         /* get unicast address */
8418         list_for_each(p, &idev->addr_list) {
8419                 if (count >= ATH11K_IPV6_MAX_COUNT)
8420                         goto generate;
8421
8422                 ifa6 = list_entry(p, struct inet6_ifaddr, if_list);
8423                 if (ifa6->flags & IFA_F_DADFAILED)
8424                         continue;
8425                 scope = ipv6_addr_src_scope(&ifa6->addr);
8426                 if (scope == IPV6_ADDR_SCOPE_LINKLOCAL ||
8427                     scope == IPV6_ADDR_SCOPE_GLOBAL) {
8428                         memcpy(offload->ipv6_addr[count], &ifa6->addr.s6_addr,
8429                                sizeof(ifa6->addr.s6_addr));
8430                         offload->ipv6_type[count] = ATH11K_IPV6_UC_TYPE;
8431                         ath11k_dbg(ar->ab, ATH11K_DBG_MAC, "mac count %d ipv6 uc %pI6 scope %d\n",
8432                                    count, offload->ipv6_addr[count],
8433                                    scope);
8434                         count++;
8435                 } else {
8436                         ath11k_warn(ar->ab, "Unsupported ipv6 scope: %d\n", scope);
8437                 }
8438         }
8439
8440         /* get anycast address */
8441         for (ifaca6 = idev->ac_list; ifaca6; ifaca6 = ifaca6->aca_next) {
8442                 if (count >= ATH11K_IPV6_MAX_COUNT)
8443                         goto generate;
8444
8445                 scope = ipv6_addr_src_scope(&ifaca6->aca_addr);
8446                 if (scope == IPV6_ADDR_SCOPE_LINKLOCAL ||
8447                     scope == IPV6_ADDR_SCOPE_GLOBAL) {
8448                         memcpy(offload->ipv6_addr[count], &ifaca6->aca_addr,
8449                                sizeof(ifaca6->aca_addr));
8450                         offload->ipv6_type[count] = ATH11K_IPV6_AC_TYPE;
8451                         ath11k_dbg(ar->ab, ATH11K_DBG_MAC, "mac count %d ipv6 ac %pI6 scope %d\n",
8452                                    count, offload->ipv6_addr[count],
8453                                    scope);
8454                         count++;
8455                 } else {
8456                         ath11k_warn(ar->ab, "Unsupported ipv scope: %d\n", scope);
8457                 }
8458         }
8459
8460 generate:
8461         offload->ipv6_count = count;
8462         read_unlock_bh(&idev->lock);
8463
8464         /* generate ns multicast address */
8465         ath11k_generate_ns_mc_addr(ar, offload);
8466 }
8467 #endif
8468
8469 static void ath11k_mac_op_set_rekey_data(struct ieee80211_hw *hw,
8470                                          struct ieee80211_vif *vif,
8471                                          struct cfg80211_gtk_rekey_data *data)
8472 {
8473         struct ath11k *ar = hw->priv;
8474         struct ath11k_vif *arvif = ath11k_vif_to_arvif(vif);
8475         struct ath11k_rekey_data *rekey_data = &arvif->rekey_data;
8476
8477         ath11k_dbg(ar->ab, ATH11K_DBG_MAC, "mac set rekey data vdev %d\n",
8478                    arvif->vdev_id);
8479
8480         mutex_lock(&ar->conf_mutex);
8481
8482         memcpy(rekey_data->kck, data->kck, NL80211_KCK_LEN);
8483         memcpy(rekey_data->kek, data->kek, NL80211_KEK_LEN);
8484
8485         /* The supplicant works on big-endian, the firmware expects it on
8486          * little endian.
8487          */
8488         rekey_data->replay_ctr = get_unaligned_be64(data->replay_ctr);
8489
8490         arvif->rekey_data.enable_offload = true;
8491
8492         ath11k_dbg_dump(ar->ab, ATH11K_DBG_MAC, "kck", NULL,
8493                         rekey_data->kck, NL80211_KCK_LEN);
8494         ath11k_dbg_dump(ar->ab, ATH11K_DBG_MAC, "kek", NULL,
8495                         rekey_data->kck, NL80211_KEK_LEN);
8496         ath11k_dbg_dump(ar->ab, ATH11K_DBG_MAC, "replay ctr", NULL,
8497                         &rekey_data->replay_ctr, sizeof(rekey_data->replay_ctr));
8498
8499         mutex_unlock(&ar->conf_mutex);
8500 }
8501
8502 static int ath11k_mac_op_set_bios_sar_specs(struct ieee80211_hw *hw,
8503                                             const struct cfg80211_sar_specs *sar)
8504 {
8505         struct ath11k *ar = hw->priv;
8506         const struct cfg80211_sar_sub_specs *sspec;
8507         int ret, index;
8508         u8 *sar_tbl;
8509         u32 i;
8510
8511         if (!sar || sar->type != NL80211_SAR_TYPE_POWER ||
8512             sar->num_sub_specs == 0)
8513                 return -EINVAL;
8514
8515         mutex_lock(&ar->conf_mutex);
8516
8517         if (!test_bit(WMI_TLV_SERVICE_BIOS_SAR_SUPPORT, ar->ab->wmi_ab.svc_map) ||
8518             !ar->ab->hw_params.bios_sar_capa) {
8519                 ret = -EOPNOTSUPP;
8520                 goto exit;
8521         }
8522
8523         ret = ath11k_wmi_pdev_set_bios_geo_table_param(ar);
8524         if (ret) {
8525                 ath11k_warn(ar->ab, "failed to set geo table: %d\n", ret);
8526                 goto exit;
8527         }
8528
8529         sar_tbl = kzalloc(BIOS_SAR_TABLE_LEN, GFP_KERNEL);
8530         if (!sar_tbl) {
8531                 ret = -ENOMEM;
8532                 goto exit;
8533         }
8534
8535         sspec = sar->sub_specs;
8536         for (i = 0; i < sar->num_sub_specs; i++) {
8537                 if (sspec->freq_range_index >= (BIOS_SAR_TABLE_LEN >> 1)) {
8538                         ath11k_warn(ar->ab, "Ignore bad frequency index %u, max allowed %u\n",
8539                                     sspec->freq_range_index, BIOS_SAR_TABLE_LEN >> 1);
8540                         continue;
8541                 }
8542
8543                 /* chain0 and chain1 share same power setting */
8544                 sar_tbl[sspec->freq_range_index] = sspec->power;
8545                 index = sspec->freq_range_index + (BIOS_SAR_TABLE_LEN >> 1);
8546                 sar_tbl[index] = sspec->power;
8547                 ath11k_dbg(ar->ab, ATH11K_DBG_MAC, "sar tbl[%d] = %d\n",
8548                            sspec->freq_range_index, sar_tbl[sspec->freq_range_index]);
8549                 sspec++;
8550         }
8551
8552         ret = ath11k_wmi_pdev_set_bios_sar_table_param(ar, sar_tbl);
8553         if (ret)
8554                 ath11k_warn(ar->ab, "failed to set sar power: %d", ret);
8555
8556         kfree(sar_tbl);
8557 exit:
8558         mutex_unlock(&ar->conf_mutex);
8559
8560         return ret;
8561 }
8562
8563 static int ath11k_mac_op_cancel_remain_on_channel(struct ieee80211_hw *hw,
8564                                                   struct ieee80211_vif *vif)
8565 {
8566         struct ath11k *ar = hw->priv;
8567
8568         mutex_lock(&ar->conf_mutex);
8569
8570         spin_lock_bh(&ar->data_lock);
8571         ar->scan.roc_notify = false;
8572         spin_unlock_bh(&ar->data_lock);
8573
8574         ath11k_scan_abort(ar);
8575
8576         mutex_unlock(&ar->conf_mutex);
8577
8578         cancel_delayed_work_sync(&ar->scan.timeout);
8579
8580         return 0;
8581 }
8582
8583 static int ath11k_mac_op_remain_on_channel(struct ieee80211_hw *hw,
8584                                            struct ieee80211_vif *vif,
8585                                            struct ieee80211_channel *chan,
8586                                            int duration,
8587                                            enum ieee80211_roc_type type)
8588 {
8589         struct ath11k *ar = hw->priv;
8590         struct ath11k_vif *arvif = (void *)vif->drv_priv;
8591         struct scan_req_params arg;
8592         int ret;
8593         u32 scan_time_msec;
8594
8595         mutex_lock(&ar->conf_mutex);
8596
8597         spin_lock_bh(&ar->data_lock);
8598         switch (ar->scan.state) {
8599         case ATH11K_SCAN_IDLE:
8600                 reinit_completion(&ar->scan.started);
8601                 reinit_completion(&ar->scan.completed);
8602                 reinit_completion(&ar->scan.on_channel);
8603                 ar->scan.state = ATH11K_SCAN_STARTING;
8604                 ar->scan.is_roc = true;
8605                 ar->scan.vdev_id = arvif->vdev_id;
8606                 ar->scan.roc_freq = chan->center_freq;
8607                 ar->scan.roc_notify = true;
8608                 ret = 0;
8609                 break;
8610         case ATH11K_SCAN_STARTING:
8611         case ATH11K_SCAN_RUNNING:
8612         case ATH11K_SCAN_ABORTING:
8613                 ret = -EBUSY;
8614                 break;
8615         }
8616         spin_unlock_bh(&ar->data_lock);
8617
8618         if (ret)
8619                 goto exit;
8620
8621         scan_time_msec = ar->hw->wiphy->max_remain_on_channel_duration * 2;
8622
8623         memset(&arg, 0, sizeof(arg));
8624         ath11k_wmi_start_scan_init(ar, &arg);
8625         arg.num_chan = 1;
8626         arg.chan_list = kcalloc(arg.num_chan, sizeof(*arg.chan_list),
8627                                 GFP_KERNEL);
8628         if (!arg.chan_list) {
8629                 ret = -ENOMEM;
8630                 goto exit;
8631         }
8632
8633         arg.vdev_id = arvif->vdev_id;
8634         arg.scan_id = ATH11K_SCAN_ID;
8635         arg.chan_list[0] = chan->center_freq;
8636         arg.dwell_time_active = scan_time_msec;
8637         arg.dwell_time_passive = scan_time_msec;
8638         arg.max_scan_time = scan_time_msec;
8639         arg.scan_flags |= WMI_SCAN_FLAG_PASSIVE;
8640         arg.scan_flags |= WMI_SCAN_FILTER_PROBE_REQ;
8641         arg.burst_duration = duration;
8642
8643         ret = ath11k_start_scan(ar, &arg);
8644         if (ret) {
8645                 ath11k_warn(ar->ab, "failed to start roc scan: %d\n", ret);
8646
8647                 spin_lock_bh(&ar->data_lock);
8648                 ar->scan.state = ATH11K_SCAN_IDLE;
8649                 spin_unlock_bh(&ar->data_lock);
8650                 goto free_chan_list;
8651         }
8652
8653         ret = wait_for_completion_timeout(&ar->scan.on_channel, 3 * HZ);
8654         if (ret == 0) {
8655                 ath11k_warn(ar->ab, "failed to switch to channel for roc scan\n");
8656                 ret = ath11k_scan_stop(ar);
8657                 if (ret)
8658                         ath11k_warn(ar->ab, "failed to stop scan: %d\n", ret);
8659                 ret = -ETIMEDOUT;
8660                 goto free_chan_list;
8661         }
8662
8663         ieee80211_queue_delayed_work(ar->hw, &ar->scan.timeout,
8664                                      msecs_to_jiffies(duration));
8665
8666         ret = 0;
8667
8668 free_chan_list:
8669         kfree(arg.chan_list);
8670 exit:
8671         mutex_unlock(&ar->conf_mutex);
8672         return ret;
8673 }
8674
8675 static int ath11k_fw_stats_request(struct ath11k *ar,
8676                                    struct stats_request_params *req_param)
8677 {
8678         struct ath11k_base *ab = ar->ab;
8679         unsigned long time_left;
8680         int ret;
8681
8682         lockdep_assert_held(&ar->conf_mutex);
8683
8684         spin_lock_bh(&ar->data_lock);
8685         ar->fw_stats_done = false;
8686         ath11k_fw_stats_pdevs_free(&ar->fw_stats.pdevs);
8687         spin_unlock_bh(&ar->data_lock);
8688
8689         reinit_completion(&ar->fw_stats_complete);
8690
8691         ret = ath11k_wmi_send_stats_request_cmd(ar, req_param);
8692         if (ret) {
8693                 ath11k_warn(ab, "could not request fw stats (%d)\n",
8694                             ret);
8695                 return ret;
8696         }
8697
8698         time_left = wait_for_completion_timeout(&ar->fw_stats_complete,
8699                                                 1 * HZ);
8700
8701         if (!time_left)
8702                 return -ETIMEDOUT;
8703
8704         return 0;
8705 }
8706
8707 static int ath11k_mac_op_get_txpower(struct ieee80211_hw *hw,
8708                                      struct ieee80211_vif *vif,
8709                                      int *dbm)
8710 {
8711         struct ath11k *ar = hw->priv;
8712         struct ath11k_base *ab = ar->ab;
8713         struct stats_request_params req_param = {0};
8714         struct ath11k_fw_stats_pdev *pdev;
8715         int ret;
8716
8717         /* Final Tx power is minimum of Target Power, CTL power, Regulatory
8718          * Power, PSD EIRP Power. We just know the Regulatory power from the
8719          * regulatory rules obtained. FW knows all these power and sets the min
8720          * of these. Hence, we request the FW pdev stats in which FW reports
8721          * the minimum of all vdev's channel Tx power.
8722          */
8723         mutex_lock(&ar->conf_mutex);
8724
8725         if (ar->state != ATH11K_STATE_ON)
8726                 goto err_fallback;
8727
8728         req_param.pdev_id = ar->pdev->pdev_id;
8729         req_param.stats_id = WMI_REQUEST_PDEV_STAT;
8730
8731         ret = ath11k_fw_stats_request(ar, &req_param);
8732         if (ret) {
8733                 ath11k_warn(ab, "failed to request fw pdev stats: %d\n", ret);
8734                 goto err_fallback;
8735         }
8736
8737         spin_lock_bh(&ar->data_lock);
8738         pdev = list_first_entry_or_null(&ar->fw_stats.pdevs,
8739                                         struct ath11k_fw_stats_pdev, list);
8740         if (!pdev) {
8741                 spin_unlock_bh(&ar->data_lock);
8742                 goto err_fallback;
8743         }
8744
8745         /* tx power is set as 2 units per dBm in FW. */
8746         *dbm = pdev->chan_tx_power / 2;
8747
8748         spin_unlock_bh(&ar->data_lock);
8749         mutex_unlock(&ar->conf_mutex);
8750
8751         ath11k_dbg(ar->ab, ATH11K_DBG_MAC, "txpower from firmware %d, reported %d dBm\n",
8752                    pdev->chan_tx_power, *dbm);
8753         return 0;
8754
8755 err_fallback:
8756         mutex_unlock(&ar->conf_mutex);
8757         /* We didn't get txpower from FW. Hence, relying on vif->bss_conf.txpower */
8758         *dbm = vif->bss_conf.txpower;
8759         ath11k_dbg(ar->ab, ATH11K_DBG_MAC, "txpower from firmware NaN, reported %d dBm\n",
8760                    *dbm);
8761         return 0;
8762 }
8763
8764 static const struct ieee80211_ops ath11k_ops = {
8765         .tx                             = ath11k_mac_op_tx,
8766         .wake_tx_queue                  = ieee80211_handle_wake_tx_queue,
8767         .start                          = ath11k_mac_op_start,
8768         .stop                           = ath11k_mac_op_stop,
8769         .reconfig_complete              = ath11k_mac_op_reconfig_complete,
8770         .add_interface                  = ath11k_mac_op_add_interface,
8771         .remove_interface               = ath11k_mac_op_remove_interface,
8772         .update_vif_offload             = ath11k_mac_op_update_vif_offload,
8773         .config                         = ath11k_mac_op_config,
8774         .bss_info_changed               = ath11k_mac_op_bss_info_changed,
8775         .configure_filter               = ath11k_mac_op_configure_filter,
8776         .hw_scan                        = ath11k_mac_op_hw_scan,
8777         .cancel_hw_scan                 = ath11k_mac_op_cancel_hw_scan,
8778         .set_key                        = ath11k_mac_op_set_key,
8779         .set_rekey_data                 = ath11k_mac_op_set_rekey_data,
8780         .sta_state                      = ath11k_mac_op_sta_state,
8781         .sta_set_4addr                  = ath11k_mac_op_sta_set_4addr,
8782         .sta_set_txpwr                  = ath11k_mac_op_sta_set_txpwr,
8783         .sta_rc_update                  = ath11k_mac_op_sta_rc_update,
8784         .conf_tx                        = ath11k_mac_op_conf_tx,
8785         .set_antenna                    = ath11k_mac_op_set_antenna,
8786         .get_antenna                    = ath11k_mac_op_get_antenna,
8787         .ampdu_action                   = ath11k_mac_op_ampdu_action,
8788         .add_chanctx                    = ath11k_mac_op_add_chanctx,
8789         .remove_chanctx                 = ath11k_mac_op_remove_chanctx,
8790         .change_chanctx                 = ath11k_mac_op_change_chanctx,
8791         .assign_vif_chanctx             = ath11k_mac_op_assign_vif_chanctx,
8792         .unassign_vif_chanctx           = ath11k_mac_op_unassign_vif_chanctx,
8793         .switch_vif_chanctx             = ath11k_mac_op_switch_vif_chanctx,
8794         .set_rts_threshold              = ath11k_mac_op_set_rts_threshold,
8795         .set_frag_threshold             = ath11k_mac_op_set_frag_threshold,
8796         .set_bitrate_mask               = ath11k_mac_op_set_bitrate_mask,
8797         .get_survey                     = ath11k_mac_op_get_survey,
8798         .flush                          = ath11k_mac_op_flush,
8799         .sta_statistics                 = ath11k_mac_op_sta_statistics,
8800         CFG80211_TESTMODE_CMD(ath11k_tm_cmd)
8801
8802 #ifdef CONFIG_PM
8803         .suspend                        = ath11k_wow_op_suspend,
8804         .resume                         = ath11k_wow_op_resume,
8805         .set_wakeup                     = ath11k_wow_op_set_wakeup,
8806 #endif
8807
8808 #ifdef CONFIG_ATH11K_DEBUGFS
8809         .sta_add_debugfs                = ath11k_debugfs_sta_op_add,
8810 #endif
8811
8812 #if IS_ENABLED(CONFIG_IPV6)
8813         .ipv6_addr_change = ath11k_mac_op_ipv6_changed,
8814 #endif
8815         .get_txpower                    = ath11k_mac_op_get_txpower,
8816
8817         .set_sar_specs                  = ath11k_mac_op_set_bios_sar_specs,
8818         .remain_on_channel              = ath11k_mac_op_remain_on_channel,
8819         .cancel_remain_on_channel       = ath11k_mac_op_cancel_remain_on_channel,
8820 };
8821
8822 static void ath11k_mac_update_ch_list(struct ath11k *ar,
8823                                       struct ieee80211_supported_band *band,
8824                                       u32 freq_low, u32 freq_high)
8825 {
8826         int i;
8827
8828         if (!(freq_low && freq_high))
8829                 return;
8830
8831         for (i = 0; i < band->n_channels; i++) {
8832                 if (band->channels[i].center_freq < freq_low ||
8833                     band->channels[i].center_freq > freq_high)
8834                         band->channels[i].flags |= IEEE80211_CHAN_DISABLED;
8835         }
8836 }
8837
8838 static u32 ath11k_get_phy_id(struct ath11k *ar, u32 band)
8839 {
8840         struct ath11k_pdev *pdev = ar->pdev;
8841         struct ath11k_pdev_cap *pdev_cap = &pdev->cap;
8842
8843         if (band == WMI_HOST_WLAN_2G_CAP)
8844                 return pdev_cap->band[NL80211_BAND_2GHZ].phy_id;
8845
8846         if (band == WMI_HOST_WLAN_5G_CAP)
8847                 return pdev_cap->band[NL80211_BAND_5GHZ].phy_id;
8848
8849         ath11k_warn(ar->ab, "unsupported phy cap:%d\n", band);
8850
8851         return 0;
8852 }
8853
8854 static int ath11k_mac_setup_channels_rates(struct ath11k *ar,
8855                                            u32 supported_bands)
8856 {
8857         struct ieee80211_supported_band *band;
8858         struct ath11k_hal_reg_capabilities_ext *reg_cap, *temp_reg_cap;
8859         void *channels;
8860         u32 phy_id;
8861
8862         BUILD_BUG_ON((ARRAY_SIZE(ath11k_2ghz_channels) +
8863                       ARRAY_SIZE(ath11k_5ghz_channels) +
8864                       ARRAY_SIZE(ath11k_6ghz_channels)) !=
8865                      ATH11K_NUM_CHANS);
8866
8867         reg_cap = &ar->ab->hal_reg_cap[ar->pdev_idx];
8868         temp_reg_cap = reg_cap;
8869
8870         if (supported_bands & WMI_HOST_WLAN_2G_CAP) {
8871                 channels = kmemdup(ath11k_2ghz_channels,
8872                                    sizeof(ath11k_2ghz_channels),
8873                                    GFP_KERNEL);
8874                 if (!channels)
8875                         return -ENOMEM;
8876
8877                 band = &ar->mac.sbands[NL80211_BAND_2GHZ];
8878                 band->band = NL80211_BAND_2GHZ;
8879                 band->n_channels = ARRAY_SIZE(ath11k_2ghz_channels);
8880                 band->channels = channels;
8881                 band->n_bitrates = ath11k_g_rates_size;
8882                 band->bitrates = ath11k_g_rates;
8883                 ar->hw->wiphy->bands[NL80211_BAND_2GHZ] = band;
8884
8885                 if (ar->ab->hw_params.single_pdev_only) {
8886                         phy_id = ath11k_get_phy_id(ar, WMI_HOST_WLAN_2G_CAP);
8887                         temp_reg_cap = &ar->ab->hal_reg_cap[phy_id];
8888                 }
8889                 ath11k_mac_update_ch_list(ar, band,
8890                                           temp_reg_cap->low_2ghz_chan,
8891                                           temp_reg_cap->high_2ghz_chan);
8892         }
8893
8894         if (supported_bands & WMI_HOST_WLAN_5G_CAP) {
8895                 if (reg_cap->high_5ghz_chan >= ATH11K_MAX_6G_FREQ) {
8896                         channels = kmemdup(ath11k_6ghz_channels,
8897                                            sizeof(ath11k_6ghz_channels), GFP_KERNEL);
8898                         if (!channels) {
8899                                 kfree(ar->mac.sbands[NL80211_BAND_2GHZ].channels);
8900                                 return -ENOMEM;
8901                         }
8902
8903                         ar->supports_6ghz = true;
8904                         band = &ar->mac.sbands[NL80211_BAND_6GHZ];
8905                         band->band = NL80211_BAND_6GHZ;
8906                         band->n_channels = ARRAY_SIZE(ath11k_6ghz_channels);
8907                         band->channels = channels;
8908                         band->n_bitrates = ath11k_a_rates_size;
8909                         band->bitrates = ath11k_a_rates;
8910                         ar->hw->wiphy->bands[NL80211_BAND_6GHZ] = band;
8911
8912                         if (ar->ab->hw_params.single_pdev_only) {
8913                                 phy_id = ath11k_get_phy_id(ar, WMI_HOST_WLAN_5G_CAP);
8914                                 temp_reg_cap = &ar->ab->hal_reg_cap[phy_id];
8915                         }
8916
8917                         ath11k_mac_update_ch_list(ar, band,
8918                                                   temp_reg_cap->low_5ghz_chan,
8919                                                   temp_reg_cap->high_5ghz_chan);
8920                 }
8921
8922                 if (reg_cap->low_5ghz_chan < ATH11K_MIN_6G_FREQ) {
8923                         channels = kmemdup(ath11k_5ghz_channels,
8924                                            sizeof(ath11k_5ghz_channels),
8925                                            GFP_KERNEL);
8926                         if (!channels) {
8927                                 kfree(ar->mac.sbands[NL80211_BAND_2GHZ].channels);
8928                                 kfree(ar->mac.sbands[NL80211_BAND_6GHZ].channels);
8929                                 return -ENOMEM;
8930                         }
8931
8932                         band = &ar->mac.sbands[NL80211_BAND_5GHZ];
8933                         band->band = NL80211_BAND_5GHZ;
8934                         band->n_channels = ARRAY_SIZE(ath11k_5ghz_channels);
8935                         band->channels = channels;
8936                         band->n_bitrates = ath11k_a_rates_size;
8937                         band->bitrates = ath11k_a_rates;
8938                         ar->hw->wiphy->bands[NL80211_BAND_5GHZ] = band;
8939
8940                         if (ar->ab->hw_params.single_pdev_only) {
8941                                 phy_id = ath11k_get_phy_id(ar, WMI_HOST_WLAN_5G_CAP);
8942                                 temp_reg_cap = &ar->ab->hal_reg_cap[phy_id];
8943                         }
8944
8945                         ath11k_mac_update_ch_list(ar, band,
8946                                                   temp_reg_cap->low_5ghz_chan,
8947                                                   temp_reg_cap->high_5ghz_chan);
8948                 }
8949         }
8950
8951         return 0;
8952 }
8953
8954 static int ath11k_mac_setup_iface_combinations(struct ath11k *ar)
8955 {
8956         struct ath11k_base *ab = ar->ab;
8957         struct ieee80211_iface_combination *combinations;
8958         struct ieee80211_iface_limit *limits;
8959         int n_limits;
8960
8961         combinations = kzalloc(sizeof(*combinations), GFP_KERNEL);
8962         if (!combinations)
8963                 return -ENOMEM;
8964
8965         n_limits = 2;
8966
8967         limits = kcalloc(n_limits, sizeof(*limits), GFP_KERNEL);
8968         if (!limits) {
8969                 kfree(combinations);
8970                 return -ENOMEM;
8971         }
8972
8973         limits[0].max = 1;
8974         limits[0].types |= BIT(NL80211_IFTYPE_STATION);
8975
8976         limits[1].max = 16;
8977         limits[1].types |= BIT(NL80211_IFTYPE_AP);
8978
8979         if (IS_ENABLED(CONFIG_MAC80211_MESH) &&
8980             ab->hw_params.interface_modes & BIT(NL80211_IFTYPE_MESH_POINT))
8981                 limits[1].types |= BIT(NL80211_IFTYPE_MESH_POINT);
8982
8983         combinations[0].limits = limits;
8984         combinations[0].n_limits = n_limits;
8985         combinations[0].max_interfaces = 16;
8986         combinations[0].num_different_channels = 1;
8987         combinations[0].beacon_int_infra_match = true;
8988         combinations[0].beacon_int_min_gcd = 100;
8989         combinations[0].radar_detect_widths = BIT(NL80211_CHAN_WIDTH_20_NOHT) |
8990                                                 BIT(NL80211_CHAN_WIDTH_20) |
8991                                                 BIT(NL80211_CHAN_WIDTH_40) |
8992                                                 BIT(NL80211_CHAN_WIDTH_80) |
8993                                                 BIT(NL80211_CHAN_WIDTH_80P80) |
8994                                                 BIT(NL80211_CHAN_WIDTH_160);
8995
8996         ar->hw->wiphy->iface_combinations = combinations;
8997         ar->hw->wiphy->n_iface_combinations = 1;
8998
8999         return 0;
9000 }
9001
9002 static const u8 ath11k_if_types_ext_capa[] = {
9003         [0] = WLAN_EXT_CAPA1_EXT_CHANNEL_SWITCHING,
9004         [7] = WLAN_EXT_CAPA8_OPMODE_NOTIF,
9005 };
9006
9007 static const u8 ath11k_if_types_ext_capa_sta[] = {
9008         [0] = WLAN_EXT_CAPA1_EXT_CHANNEL_SWITCHING,
9009         [7] = WLAN_EXT_CAPA8_OPMODE_NOTIF,
9010         [9] = WLAN_EXT_CAPA10_TWT_REQUESTER_SUPPORT,
9011 };
9012
9013 static const u8 ath11k_if_types_ext_capa_ap[] = {
9014         [0] = WLAN_EXT_CAPA1_EXT_CHANNEL_SWITCHING,
9015         [7] = WLAN_EXT_CAPA8_OPMODE_NOTIF,
9016         [9] = WLAN_EXT_CAPA10_TWT_RESPONDER_SUPPORT,
9017 };
9018
9019 static const struct wiphy_iftype_ext_capab ath11k_iftypes_ext_capa[] = {
9020         {
9021                 .extended_capabilities = ath11k_if_types_ext_capa,
9022                 .extended_capabilities_mask = ath11k_if_types_ext_capa,
9023                 .extended_capabilities_len = sizeof(ath11k_if_types_ext_capa),
9024         }, {
9025                 .iftype = NL80211_IFTYPE_STATION,
9026                 .extended_capabilities = ath11k_if_types_ext_capa_sta,
9027                 .extended_capabilities_mask = ath11k_if_types_ext_capa_sta,
9028                 .extended_capabilities_len =
9029                                 sizeof(ath11k_if_types_ext_capa_sta),
9030         }, {
9031                 .iftype = NL80211_IFTYPE_AP,
9032                 .extended_capabilities = ath11k_if_types_ext_capa_ap,
9033                 .extended_capabilities_mask = ath11k_if_types_ext_capa_ap,
9034                 .extended_capabilities_len =
9035                                 sizeof(ath11k_if_types_ext_capa_ap),
9036         },
9037 };
9038
9039 static void __ath11k_mac_unregister(struct ath11k *ar)
9040 {
9041         cancel_work_sync(&ar->regd_update_work);
9042
9043         ieee80211_unregister_hw(ar->hw);
9044
9045         idr_for_each(&ar->txmgmt_idr, ath11k_mac_tx_mgmt_pending_free, ar);
9046         idr_destroy(&ar->txmgmt_idr);
9047
9048         kfree(ar->mac.sbands[NL80211_BAND_2GHZ].channels);
9049         kfree(ar->mac.sbands[NL80211_BAND_5GHZ].channels);
9050         kfree(ar->mac.sbands[NL80211_BAND_6GHZ].channels);
9051
9052         kfree(ar->hw->wiphy->iface_combinations[0].limits);
9053         kfree(ar->hw->wiphy->iface_combinations);
9054
9055         SET_IEEE80211_DEV(ar->hw, NULL);
9056 }
9057
9058 void ath11k_mac_unregister(struct ath11k_base *ab)
9059 {
9060         struct ath11k *ar;
9061         struct ath11k_pdev *pdev;
9062         int i;
9063
9064         for (i = 0; i < ab->num_radios; i++) {
9065                 pdev = &ab->pdevs[i];
9066                 ar = pdev->ar;
9067                 if (!ar)
9068                         continue;
9069
9070                 __ath11k_mac_unregister(ar);
9071         }
9072
9073         ath11k_peer_rhash_tbl_destroy(ab);
9074 }
9075
9076 static int __ath11k_mac_register(struct ath11k *ar)
9077 {
9078         struct ath11k_base *ab = ar->ab;
9079         struct ath11k_pdev_cap *cap = &ar->pdev->cap;
9080         static const u32 cipher_suites[] = {
9081                 WLAN_CIPHER_SUITE_TKIP,
9082                 WLAN_CIPHER_SUITE_CCMP,
9083                 WLAN_CIPHER_SUITE_AES_CMAC,
9084                 WLAN_CIPHER_SUITE_BIP_CMAC_256,
9085                 WLAN_CIPHER_SUITE_BIP_GMAC_128,
9086                 WLAN_CIPHER_SUITE_BIP_GMAC_256,
9087                 WLAN_CIPHER_SUITE_GCMP,
9088                 WLAN_CIPHER_SUITE_GCMP_256,
9089                 WLAN_CIPHER_SUITE_CCMP_256,
9090         };
9091         int ret;
9092         u32 ht_cap = 0;
9093
9094         ath11k_pdev_caps_update(ar);
9095
9096         SET_IEEE80211_PERM_ADDR(ar->hw, ar->mac_addr);
9097
9098         SET_IEEE80211_DEV(ar->hw, ab->dev);
9099
9100         ret = ath11k_mac_setup_channels_rates(ar,
9101                                               cap->supported_bands);
9102         if (ret)
9103                 goto err;
9104
9105         ath11k_mac_setup_ht_vht_cap(ar, cap, &ht_cap);
9106         ath11k_mac_setup_he_cap(ar, cap);
9107
9108         ret = ath11k_mac_setup_iface_combinations(ar);
9109         if (ret) {
9110                 ath11k_err(ar->ab, "failed to setup interface combinations: %d\n", ret);
9111                 goto err_free_channels;
9112         }
9113
9114         ar->hw->wiphy->available_antennas_rx = cap->rx_chain_mask;
9115         ar->hw->wiphy->available_antennas_tx = cap->tx_chain_mask;
9116
9117         ar->hw->wiphy->interface_modes = ab->hw_params.interface_modes;
9118
9119         if (ab->hw_params.single_pdev_only && ar->supports_6ghz)
9120                 ieee80211_hw_set(ar->hw, SINGLE_SCAN_ON_ALL_BANDS);
9121
9122         if (ab->hw_params.supports_multi_bssid) {
9123                 ieee80211_hw_set(ar->hw, SUPPORTS_MULTI_BSSID);
9124                 ieee80211_hw_set(ar->hw, SUPPORTS_ONLY_HE_MULTI_BSSID);
9125         }
9126
9127         ieee80211_hw_set(ar->hw, SIGNAL_DBM);
9128         ieee80211_hw_set(ar->hw, SUPPORTS_PS);
9129         ieee80211_hw_set(ar->hw, SUPPORTS_DYNAMIC_PS);
9130         ieee80211_hw_set(ar->hw, MFP_CAPABLE);
9131         ieee80211_hw_set(ar->hw, REPORTS_TX_ACK_STATUS);
9132         ieee80211_hw_set(ar->hw, HAS_RATE_CONTROL);
9133         ieee80211_hw_set(ar->hw, AP_LINK_PS);
9134         ieee80211_hw_set(ar->hw, SPECTRUM_MGMT);
9135         ieee80211_hw_set(ar->hw, CONNECTION_MONITOR);
9136         ieee80211_hw_set(ar->hw, SUPPORTS_PER_STA_GTK);
9137         ieee80211_hw_set(ar->hw, WANT_MONITOR_VIF);
9138         ieee80211_hw_set(ar->hw, CHANCTX_STA_CSA);
9139         ieee80211_hw_set(ar->hw, QUEUE_CONTROL);
9140         ieee80211_hw_set(ar->hw, SUPPORTS_TX_FRAG);
9141         ieee80211_hw_set(ar->hw, REPORTS_LOW_ACK);
9142
9143         if (ath11k_frame_mode == ATH11K_HW_TXRX_ETHERNET) {
9144                 ieee80211_hw_set(ar->hw, SUPPORTS_TX_ENCAP_OFFLOAD);
9145                 ieee80211_hw_set(ar->hw, SUPPORTS_RX_DECAP_OFFLOAD);
9146         }
9147
9148         if (cap->nss_ratio_enabled)
9149                 ieee80211_hw_set(ar->hw, SUPPORTS_VHT_EXT_NSS_BW);
9150
9151         if ((ht_cap & WMI_HT_CAP_ENABLED) || ar->supports_6ghz) {
9152                 ieee80211_hw_set(ar->hw, AMPDU_AGGREGATION);
9153                 ieee80211_hw_set(ar->hw, TX_AMPDU_SETUP_IN_HW);
9154                 ieee80211_hw_set(ar->hw, SUPPORTS_REORDERING_BUFFER);
9155                 ieee80211_hw_set(ar->hw, SUPPORTS_AMSDU_IN_AMPDU);
9156                 ieee80211_hw_set(ar->hw, USES_RSS);
9157         }
9158
9159         ar->hw->wiphy->features |= NL80211_FEATURE_STATIC_SMPS;
9160         ar->hw->wiphy->flags |= WIPHY_FLAG_IBSS_RSN;
9161
9162         /* TODO: Check if HT capability advertised from firmware is different
9163          * for each band for a dual band capable radio. It will be tricky to
9164          * handle it when the ht capability different for each band.
9165          */
9166         if (ht_cap & WMI_HT_CAP_DYNAMIC_SMPS ||
9167             (ar->supports_6ghz && ab->hw_params.supports_dynamic_smps_6ghz))
9168                 ar->hw->wiphy->features |= NL80211_FEATURE_DYNAMIC_SMPS;
9169
9170         ar->hw->wiphy->max_scan_ssids = WLAN_SCAN_PARAMS_MAX_SSID;
9171         ar->hw->wiphy->max_scan_ie_len = WLAN_SCAN_PARAMS_MAX_IE_LEN;
9172
9173         ar->hw->max_listen_interval = ATH11K_MAX_HW_LISTEN_INTERVAL;
9174
9175         ar->hw->wiphy->flags |= WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL;
9176         ar->hw->wiphy->flags |= WIPHY_FLAG_HAS_CHANNEL_SWITCH;
9177         ar->hw->wiphy->max_remain_on_channel_duration = 5000;
9178
9179         ar->hw->wiphy->flags |= WIPHY_FLAG_AP_UAPSD;
9180         ar->hw->wiphy->features |= NL80211_FEATURE_AP_MODE_CHAN_WIDTH_CHANGE |
9181                                    NL80211_FEATURE_AP_SCAN;
9182
9183         ar->max_num_stations = TARGET_NUM_STATIONS(ab);
9184         ar->max_num_peers = TARGET_NUM_PEERS_PDEV(ab);
9185
9186         ar->hw->wiphy->max_ap_assoc_sta = ar->max_num_stations;
9187
9188         if (test_bit(WMI_TLV_SERVICE_SPOOF_MAC_SUPPORT, ar->wmi->wmi_ab->svc_map)) {
9189                 ar->hw->wiphy->features |=
9190                         NL80211_FEATURE_SCAN_RANDOM_MAC_ADDR;
9191         }
9192
9193         if (test_bit(WMI_TLV_SERVICE_NLO, ar->wmi->wmi_ab->svc_map)) {
9194                 ar->hw->wiphy->max_sched_scan_ssids = WMI_PNO_MAX_SUPP_NETWORKS;
9195                 ar->hw->wiphy->max_match_sets = WMI_PNO_MAX_SUPP_NETWORKS;
9196                 ar->hw->wiphy->max_sched_scan_ie_len = WMI_PNO_MAX_IE_LENGTH;
9197                 ar->hw->wiphy->max_sched_scan_plans = WMI_PNO_MAX_SCHED_SCAN_PLANS;
9198                 ar->hw->wiphy->max_sched_scan_plan_interval =
9199                         WMI_PNO_MAX_SCHED_SCAN_PLAN_INT;
9200                 ar->hw->wiphy->max_sched_scan_plan_iterations =
9201                         WMI_PNO_MAX_SCHED_SCAN_PLAN_ITRNS;
9202                 ar->hw->wiphy->features |= NL80211_FEATURE_ND_RANDOM_MAC_ADDR;
9203         }
9204
9205         ret = ath11k_wow_init(ar);
9206         if (ret) {
9207                 ath11k_warn(ar->ab, "failed to init wow: %d\n", ret);
9208                 goto err_free_if_combs;
9209         }
9210
9211         if (test_bit(WMI_TLV_SERVICE_TX_DATA_MGMT_ACK_RSSI,
9212                      ar->ab->wmi_ab.svc_map))
9213                 wiphy_ext_feature_set(ar->hw->wiphy,
9214                                       NL80211_EXT_FEATURE_ACK_SIGNAL_SUPPORT);
9215
9216         ar->hw->queues = ATH11K_HW_MAX_QUEUES;
9217         ar->hw->wiphy->tx_queue_len = ATH11K_QUEUE_LEN;
9218         ar->hw->offchannel_tx_hw_queue = ATH11K_HW_MAX_QUEUES - 1;
9219         ar->hw->max_rx_aggregation_subframes = IEEE80211_MAX_AMPDU_BUF_HE;
9220
9221         ar->hw->vif_data_size = sizeof(struct ath11k_vif);
9222         ar->hw->sta_data_size = sizeof(struct ath11k_sta);
9223
9224         wiphy_ext_feature_set(ar->hw->wiphy, NL80211_EXT_FEATURE_CQM_RSSI_LIST);
9225         wiphy_ext_feature_set(ar->hw->wiphy, NL80211_EXT_FEATURE_STA_TX_PWR);
9226         if (test_bit(WMI_TLV_SERVICE_BSS_COLOR_OFFLOAD,
9227                      ar->ab->wmi_ab.svc_map)) {
9228                 wiphy_ext_feature_set(ar->hw->wiphy,
9229                                       NL80211_EXT_FEATURE_BSS_COLOR);
9230                 ieee80211_hw_set(ar->hw, DETECTS_COLOR_COLLISION);
9231         }
9232
9233         ar->hw->wiphy->cipher_suites = cipher_suites;
9234         ar->hw->wiphy->n_cipher_suites = ARRAY_SIZE(cipher_suites);
9235
9236         ar->hw->wiphy->iftype_ext_capab = ath11k_iftypes_ext_capa;
9237         ar->hw->wiphy->num_iftype_ext_capab =
9238                 ARRAY_SIZE(ath11k_iftypes_ext_capa);
9239
9240         if (ar->supports_6ghz) {
9241                 wiphy_ext_feature_set(ar->hw->wiphy,
9242                                       NL80211_EXT_FEATURE_FILS_DISCOVERY);
9243                 wiphy_ext_feature_set(ar->hw->wiphy,
9244                                       NL80211_EXT_FEATURE_UNSOL_BCAST_PROBE_RESP);
9245         }
9246
9247         wiphy_ext_feature_set(ar->hw->wiphy,
9248                               NL80211_EXT_FEATURE_SET_SCAN_DWELL);
9249
9250         if (test_bit(WMI_TLV_SERVICE_RTT, ar->ab->wmi_ab.svc_map))
9251                 wiphy_ext_feature_set(ar->hw->wiphy,
9252                                       NL80211_EXT_FEATURE_ENABLE_FTM_RESPONDER);
9253
9254         ath11k_reg_init(ar);
9255
9256         if (!test_bit(ATH11K_FLAG_RAW_MODE, &ab->dev_flags)) {
9257                 ar->hw->netdev_features = NETIF_F_HW_CSUM;
9258                 ieee80211_hw_set(ar->hw, SW_CRYPTO_CONTROL);
9259                 ieee80211_hw_set(ar->hw, SUPPORT_FAST_XMIT);
9260         }
9261
9262         if (test_bit(WMI_TLV_SERVICE_BIOS_SAR_SUPPORT, ar->ab->wmi_ab.svc_map) &&
9263             ab->hw_params.bios_sar_capa)
9264                 ar->hw->wiphy->sar_capa = ab->hw_params.bios_sar_capa;
9265
9266         ret = ieee80211_register_hw(ar->hw);
9267         if (ret) {
9268                 ath11k_err(ar->ab, "ieee80211 registration failed: %d\n", ret);
9269                 goto err_free_if_combs;
9270         }
9271
9272         if (!ab->hw_params.supports_monitor)
9273                 /* There's a race between calling ieee80211_register_hw()
9274                  * and here where the monitor mode is enabled for a little
9275                  * while. But that time is so short and in practise it make
9276                  * a difference in real life.
9277                  */
9278                 ar->hw->wiphy->interface_modes &= ~BIT(NL80211_IFTYPE_MONITOR);
9279
9280         /* Apply the regd received during initialization */
9281         ret = ath11k_regd_update(ar);
9282         if (ret) {
9283                 ath11k_err(ar->ab, "ath11k regd update failed: %d\n", ret);
9284                 goto err_unregister_hw;
9285         }
9286
9287         if (ab->hw_params.current_cc_support && ab->new_alpha2[0]) {
9288                 struct wmi_set_current_country_params set_current_param = {};
9289
9290                 memcpy(&set_current_param.alpha2, ab->new_alpha2, 2);
9291                 memcpy(&ar->alpha2, ab->new_alpha2, 2);
9292                 ret = ath11k_wmi_send_set_current_country_cmd(ar, &set_current_param);
9293                 if (ret)
9294                         ath11k_warn(ar->ab,
9295                                     "failed set cc code for mac register: %d\n", ret);
9296         }
9297
9298         ret = ath11k_debugfs_register(ar);
9299         if (ret) {
9300                 ath11k_err(ar->ab, "debugfs registration failed: %d\n", ret);
9301                 goto err_unregister_hw;
9302         }
9303
9304         return 0;
9305
9306 err_unregister_hw:
9307         ieee80211_unregister_hw(ar->hw);
9308
9309 err_free_if_combs:
9310         kfree(ar->hw->wiphy->iface_combinations[0].limits);
9311         kfree(ar->hw->wiphy->iface_combinations);
9312
9313 err_free_channels:
9314         kfree(ar->mac.sbands[NL80211_BAND_2GHZ].channels);
9315         kfree(ar->mac.sbands[NL80211_BAND_5GHZ].channels);
9316         kfree(ar->mac.sbands[NL80211_BAND_6GHZ].channels);
9317
9318 err:
9319         SET_IEEE80211_DEV(ar->hw, NULL);
9320         return ret;
9321 }
9322
9323 int ath11k_mac_register(struct ath11k_base *ab)
9324 {
9325         struct ath11k *ar;
9326         struct ath11k_pdev *pdev;
9327         int i;
9328         int ret;
9329         u8 mac_addr[ETH_ALEN] = {0};
9330
9331         if (test_bit(ATH11K_FLAG_REGISTERED, &ab->dev_flags))
9332                 return 0;
9333
9334         /* Initialize channel counters frequency value in hertz */
9335         ab->cc_freq_hz = IPQ8074_CC_FREQ_HERTZ;
9336         ab->free_vdev_map = (1LL << (ab->num_radios * TARGET_NUM_VDEVS(ab))) - 1;
9337
9338         ret = ath11k_peer_rhash_tbl_init(ab);
9339         if (ret)
9340                 return ret;
9341
9342         device_get_mac_address(ab->dev, mac_addr);
9343
9344         for (i = 0; i < ab->num_radios; i++) {
9345                 pdev = &ab->pdevs[i];
9346                 ar = pdev->ar;
9347                 if (ab->pdevs_macaddr_valid) {
9348                         ether_addr_copy(ar->mac_addr, pdev->mac_addr);
9349                 } else {
9350                         if (is_zero_ether_addr(mac_addr))
9351                                 ether_addr_copy(ar->mac_addr, ab->mac_addr);
9352                         else
9353                                 ether_addr_copy(ar->mac_addr, mac_addr);
9354                         ar->mac_addr[4] += i;
9355                 }
9356
9357                 idr_init(&ar->txmgmt_idr);
9358                 spin_lock_init(&ar->txmgmt_idr_lock);
9359
9360                 ret = __ath11k_mac_register(ar);
9361                 if (ret)
9362                         goto err_cleanup;
9363
9364                 init_waitqueue_head(&ar->txmgmt_empty_waitq);
9365         }
9366
9367         return 0;
9368
9369 err_cleanup:
9370         for (i = i - 1; i >= 0; i--) {
9371                 pdev = &ab->pdevs[i];
9372                 ar = pdev->ar;
9373                 __ath11k_mac_unregister(ar);
9374         }
9375
9376         ath11k_peer_rhash_tbl_destroy(ab);
9377
9378         return ret;
9379 }
9380
9381 int ath11k_mac_allocate(struct ath11k_base *ab)
9382 {
9383         struct ieee80211_hw *hw;
9384         struct ath11k *ar;
9385         struct ath11k_pdev *pdev;
9386         int ret;
9387         int i;
9388
9389         if (test_bit(ATH11K_FLAG_REGISTERED, &ab->dev_flags))
9390                 return 0;
9391
9392         for (i = 0; i < ab->num_radios; i++) {
9393                 pdev = &ab->pdevs[i];
9394                 hw = ieee80211_alloc_hw(sizeof(struct ath11k), &ath11k_ops);
9395                 if (!hw) {
9396                         ath11k_warn(ab, "failed to allocate mac80211 hw device\n");
9397                         ret = -ENOMEM;
9398                         goto err_free_mac;
9399                 }
9400
9401                 ar = hw->priv;
9402                 ar->hw = hw;
9403                 ar->ab = ab;
9404                 ar->pdev = pdev;
9405                 ar->pdev_idx = i;
9406                 ar->lmac_id = ath11k_hw_get_mac_from_pdev_id(&ab->hw_params, i);
9407
9408                 ar->wmi = &ab->wmi_ab.wmi[i];
9409                 /* FIXME wmi[0] is already initialized during attach,
9410                  * Should we do this again?
9411                  */
9412                 ath11k_wmi_pdev_attach(ab, i);
9413
9414                 ar->cfg_tx_chainmask = pdev->cap.tx_chain_mask;
9415                 ar->cfg_rx_chainmask = pdev->cap.rx_chain_mask;
9416                 ar->num_tx_chains = get_num_chains(pdev->cap.tx_chain_mask);
9417                 ar->num_rx_chains = get_num_chains(pdev->cap.rx_chain_mask);
9418
9419                 pdev->ar = ar;
9420                 spin_lock_init(&ar->data_lock);
9421                 INIT_LIST_HEAD(&ar->arvifs);
9422                 INIT_LIST_HEAD(&ar->ppdu_stats_info);
9423                 mutex_init(&ar->conf_mutex);
9424                 init_completion(&ar->vdev_setup_done);
9425                 init_completion(&ar->vdev_delete_done);
9426                 init_completion(&ar->peer_assoc_done);
9427                 init_completion(&ar->peer_delete_done);
9428                 init_completion(&ar->install_key_done);
9429                 init_completion(&ar->bss_survey_done);
9430                 init_completion(&ar->scan.started);
9431                 init_completion(&ar->scan.completed);
9432                 init_completion(&ar->scan.on_channel);
9433                 init_completion(&ar->thermal.wmi_sync);
9434
9435                 INIT_DELAYED_WORK(&ar->scan.timeout, ath11k_scan_timeout_work);
9436                 INIT_WORK(&ar->regd_update_work, ath11k_regd_update_work);
9437
9438                 INIT_WORK(&ar->wmi_mgmt_tx_work, ath11k_mgmt_over_wmi_tx_work);
9439                 skb_queue_head_init(&ar->wmi_mgmt_tx_queue);
9440
9441                 clear_bit(ATH11K_FLAG_MONITOR_STARTED, &ar->monitor_flags);
9442
9443                 ar->monitor_vdev_id = -1;
9444                 clear_bit(ATH11K_FLAG_MONITOR_VDEV_CREATED, &ar->monitor_flags);
9445                 ar->vdev_id_11d_scan = ATH11K_11D_INVALID_VDEV_ID;
9446                 init_completion(&ar->completed_11d_scan);
9447
9448                 ath11k_fw_stats_init(ar);
9449         }
9450
9451         return 0;
9452
9453 err_free_mac:
9454         ath11k_mac_destroy(ab);
9455
9456         return ret;
9457 }
9458
9459 void ath11k_mac_destroy(struct ath11k_base *ab)
9460 {
9461         struct ath11k *ar;
9462         struct ath11k_pdev *pdev;
9463         int i;
9464
9465         for (i = 0; i < ab->num_radios; i++) {
9466                 pdev = &ab->pdevs[i];
9467                 ar = pdev->ar;
9468                 if (!ar)
9469                         continue;
9470
9471                 ieee80211_free_hw(ar->hw);
9472                 pdev->ar = NULL;
9473         }
9474 }
9475
9476 int ath11k_mac_vif_set_keepalive(struct ath11k_vif *arvif,
9477                                  enum wmi_sta_keepalive_method method,
9478                                  u32 interval)
9479 {
9480         struct ath11k *ar = arvif->ar;
9481         struct wmi_sta_keepalive_arg arg = {};
9482         int ret;
9483
9484         lockdep_assert_held(&ar->conf_mutex);
9485
9486         if (arvif->vdev_type != WMI_VDEV_TYPE_STA)
9487                 return 0;
9488
9489         if (!test_bit(WMI_TLV_SERVICE_STA_KEEP_ALIVE, ar->ab->wmi_ab.svc_map))
9490                 return 0;
9491
9492         arg.vdev_id = arvif->vdev_id;
9493         arg.enabled = 1;
9494         arg.method = method;
9495         arg.interval = interval;
9496
9497         ret = ath11k_wmi_sta_keepalive(ar, &arg);
9498         if (ret) {
9499                 ath11k_warn(ar->ab, "failed to set keepalive on vdev %i: %d\n",
9500                             arvif->vdev_id, ret);
9501                 return ret;
9502         }
9503
9504         return 0;
9505 }
This page took 0.625975 seconds and 4 git commands to generate.