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