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