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