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