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