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