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