xref: /openbmc/linux/drivers/net/wireless/ath/ath11k/mac.c (revision 31eeb6b0)
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_80)
2323 		arg->bw_80 = true;
2324 
2325 	if (sta->bandwidth == IEEE80211_STA_RX_BW_160)
2326 		arg->bw_160 = true;
2327 
2328 	arg->peer_he_caps_6ghz = le16_to_cpu(sta->he_6ghz_capa.capa);
2329 	arg->peer_mpdu_density =
2330 		ath11k_parse_mpdudensity(FIELD_GET(IEEE80211_HE_6GHZ_CAP_MIN_MPDU_START,
2331 						   arg->peer_he_caps_6ghz));
2332 
2333 	/* From IEEE Std 802.11ax-2021 - Section 10.12.2: An HE STA shall be capable of
2334 	 * receiving A-MPDU where the A-MPDU pre-EOF padding length is up to the value
2335 	 * indicated by the Maximum A-MPDU Length Exponent Extension field in the HE
2336 	 * Capabilities element and the Maximum A-MPDU Length Exponent field in HE 6 GHz
2337 	 * Band Capabilities element in the 6 GHz band.
2338 	 *
2339 	 * Here, we are extracting the Max A-MPDU Exponent Extension from HE caps and
2340 	 * factor is the Maximum A-MPDU Length Exponent from HE 6 GHZ Band capability.
2341 	 */
2342 	ampdu_factor = FIELD_GET(IEEE80211_HE_MAC_CAP3_MAX_AMPDU_LEN_EXP_MASK,
2343 				 he_cap->he_cap_elem.mac_cap_info[3]) +
2344 			FIELD_GET(IEEE80211_HE_6GHZ_CAP_MAX_AMPDU_LEN_EXP,
2345 				  arg->peer_he_caps_6ghz);
2346 
2347 	arg->peer_max_mpdu = (1u << (IEEE80211_HE_6GHZ_MAX_AMPDU_FACTOR +
2348 				     ampdu_factor)) - 1;
2349 }
2350 
2351 static void ath11k_peer_assoc_h_smps(struct ieee80211_sta *sta,
2352 				     struct peer_assoc_params *arg)
2353 {
2354 	const struct ieee80211_sta_ht_cap *ht_cap = &sta->ht_cap;
2355 	int smps;
2356 
2357 	if (!ht_cap->ht_supported && !sta->he_6ghz_capa.capa)
2358 		return;
2359 
2360 	if (ht_cap->ht_supported) {
2361 		smps = ht_cap->cap & IEEE80211_HT_CAP_SM_PS;
2362 		smps >>= IEEE80211_HT_CAP_SM_PS_SHIFT;
2363 	} else {
2364 		smps = le16_get_bits(sta->he_6ghz_capa.capa,
2365 				     IEEE80211_HE_6GHZ_CAP_SM_PS);
2366 	}
2367 
2368 	switch (smps) {
2369 	case WLAN_HT_CAP_SM_PS_STATIC:
2370 		arg->static_mimops_flag = true;
2371 		break;
2372 	case WLAN_HT_CAP_SM_PS_DYNAMIC:
2373 		arg->dynamic_mimops_flag = true;
2374 		break;
2375 	case WLAN_HT_CAP_SM_PS_DISABLED:
2376 		arg->spatial_mux_flag = true;
2377 		break;
2378 	default:
2379 		break;
2380 	}
2381 }
2382 
2383 static void ath11k_peer_assoc_h_qos(struct ath11k *ar,
2384 				    struct ieee80211_vif *vif,
2385 				    struct ieee80211_sta *sta,
2386 				    struct peer_assoc_params *arg)
2387 {
2388 	struct ath11k_vif *arvif = (void *)vif->drv_priv;
2389 
2390 	switch (arvif->vdev_type) {
2391 	case WMI_VDEV_TYPE_AP:
2392 		if (sta->wme) {
2393 			/* TODO: Check WME vs QoS */
2394 			arg->is_wme_set = true;
2395 			arg->qos_flag = true;
2396 		}
2397 
2398 		if (sta->wme && sta->uapsd_queues) {
2399 			/* TODO: Check WME vs QoS */
2400 			arg->is_wme_set = true;
2401 			arg->apsd_flag = true;
2402 			arg->peer_rate_caps |= WMI_HOST_RC_UAPSD_FLAG;
2403 		}
2404 		break;
2405 	case WMI_VDEV_TYPE_STA:
2406 		if (sta->wme) {
2407 			arg->is_wme_set = true;
2408 			arg->qos_flag = true;
2409 		}
2410 		break;
2411 	default:
2412 		break;
2413 	}
2414 
2415 	ath11k_dbg(ar->ab, ATH11K_DBG_MAC, "mac peer %pM qos %d\n",
2416 		   sta->addr, arg->qos_flag);
2417 }
2418 
2419 static int ath11k_peer_assoc_qos_ap(struct ath11k *ar,
2420 				    struct ath11k_vif *arvif,
2421 				    struct ieee80211_sta *sta)
2422 {
2423 	struct ap_ps_params params;
2424 	u32 max_sp;
2425 	u32 uapsd;
2426 	int ret;
2427 
2428 	lockdep_assert_held(&ar->conf_mutex);
2429 
2430 	params.vdev_id = arvif->vdev_id;
2431 
2432 	ath11k_dbg(ar->ab, ATH11K_DBG_MAC, "mac uapsd_queues 0x%x max_sp %d\n",
2433 		   sta->uapsd_queues, sta->max_sp);
2434 
2435 	uapsd = 0;
2436 	if (sta->uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_VO)
2437 		uapsd |= WMI_AP_PS_UAPSD_AC3_DELIVERY_EN |
2438 			 WMI_AP_PS_UAPSD_AC3_TRIGGER_EN;
2439 	if (sta->uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_VI)
2440 		uapsd |= WMI_AP_PS_UAPSD_AC2_DELIVERY_EN |
2441 			 WMI_AP_PS_UAPSD_AC2_TRIGGER_EN;
2442 	if (sta->uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_BK)
2443 		uapsd |= WMI_AP_PS_UAPSD_AC1_DELIVERY_EN |
2444 			 WMI_AP_PS_UAPSD_AC1_TRIGGER_EN;
2445 	if (sta->uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_BE)
2446 		uapsd |= WMI_AP_PS_UAPSD_AC0_DELIVERY_EN |
2447 			 WMI_AP_PS_UAPSD_AC0_TRIGGER_EN;
2448 
2449 	max_sp = 0;
2450 	if (sta->max_sp < MAX_WMI_AP_PS_PEER_PARAM_MAX_SP)
2451 		max_sp = sta->max_sp;
2452 
2453 	params.param = WMI_AP_PS_PEER_PARAM_UAPSD;
2454 	params.value = uapsd;
2455 	ret = ath11k_wmi_send_set_ap_ps_param_cmd(ar, sta->addr, &params);
2456 	if (ret)
2457 		goto err;
2458 
2459 	params.param = WMI_AP_PS_PEER_PARAM_MAX_SP;
2460 	params.value = max_sp;
2461 	ret = ath11k_wmi_send_set_ap_ps_param_cmd(ar, sta->addr, &params);
2462 	if (ret)
2463 		goto err;
2464 
2465 	/* TODO revisit during testing */
2466 	params.param = WMI_AP_PS_PEER_PARAM_SIFS_RESP_FRMTYPE;
2467 	params.value = DISABLE_SIFS_RESPONSE_TRIGGER;
2468 	ret = ath11k_wmi_send_set_ap_ps_param_cmd(ar, sta->addr, &params);
2469 	if (ret)
2470 		goto err;
2471 
2472 	params.param = WMI_AP_PS_PEER_PARAM_SIFS_RESP_UAPSD;
2473 	params.value = DISABLE_SIFS_RESPONSE_TRIGGER;
2474 	ret = ath11k_wmi_send_set_ap_ps_param_cmd(ar, sta->addr, &params);
2475 	if (ret)
2476 		goto err;
2477 
2478 	return 0;
2479 
2480 err:
2481 	ath11k_warn(ar->ab, "failed to set ap ps peer param %d for vdev %i: %d\n",
2482 		    params.param, arvif->vdev_id, ret);
2483 	return ret;
2484 }
2485 
2486 static bool ath11k_mac_sta_has_ofdm_only(struct ieee80211_sta *sta)
2487 {
2488 	return sta->supp_rates[NL80211_BAND_2GHZ] >>
2489 	       ATH11K_MAC_FIRST_OFDM_RATE_IDX;
2490 }
2491 
2492 static enum wmi_phy_mode ath11k_mac_get_phymode_vht(struct ath11k *ar,
2493 						    struct ieee80211_sta *sta)
2494 {
2495 	if (sta->bandwidth == IEEE80211_STA_RX_BW_160) {
2496 		switch (sta->vht_cap.cap &
2497 			IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_MASK) {
2498 		case IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_160MHZ:
2499 			return MODE_11AC_VHT160;
2500 		case IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_160_80PLUS80MHZ:
2501 			return MODE_11AC_VHT80_80;
2502 		default:
2503 			/* not sure if this is a valid case? */
2504 			return MODE_11AC_VHT160;
2505 		}
2506 	}
2507 
2508 	if (sta->bandwidth == IEEE80211_STA_RX_BW_80)
2509 		return MODE_11AC_VHT80;
2510 
2511 	if (sta->bandwidth == IEEE80211_STA_RX_BW_40)
2512 		return MODE_11AC_VHT40;
2513 
2514 	if (sta->bandwidth == IEEE80211_STA_RX_BW_20)
2515 		return MODE_11AC_VHT20;
2516 
2517 	return MODE_UNKNOWN;
2518 }
2519 
2520 static enum wmi_phy_mode ath11k_mac_get_phymode_he(struct ath11k *ar,
2521 						   struct ieee80211_sta *sta)
2522 {
2523 	if (sta->bandwidth == IEEE80211_STA_RX_BW_160) {
2524 		if (sta->he_cap.he_cap_elem.phy_cap_info[0] &
2525 		     IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_160MHZ_IN_5G)
2526 			return MODE_11AX_HE160;
2527 		else if (sta->he_cap.he_cap_elem.phy_cap_info[0] &
2528 		     IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_80PLUS80_MHZ_IN_5G)
2529 			return MODE_11AX_HE80_80;
2530 		/* not sure if this is a valid case? */
2531 		return MODE_11AX_HE160;
2532 	}
2533 
2534 	if (sta->bandwidth == IEEE80211_STA_RX_BW_80)
2535 		return MODE_11AX_HE80;
2536 
2537 	if (sta->bandwidth == IEEE80211_STA_RX_BW_40)
2538 		return MODE_11AX_HE40;
2539 
2540 	if (sta->bandwidth == IEEE80211_STA_RX_BW_20)
2541 		return MODE_11AX_HE20;
2542 
2543 	return MODE_UNKNOWN;
2544 }
2545 
2546 static void ath11k_peer_assoc_h_phymode(struct ath11k *ar,
2547 					struct ieee80211_vif *vif,
2548 					struct ieee80211_sta *sta,
2549 					struct peer_assoc_params *arg)
2550 {
2551 	struct ath11k_vif *arvif = (void *)vif->drv_priv;
2552 	struct cfg80211_chan_def def;
2553 	enum nl80211_band band;
2554 	const u8 *ht_mcs_mask;
2555 	const u16 *vht_mcs_mask;
2556 	const u16 *he_mcs_mask;
2557 	enum wmi_phy_mode phymode = MODE_UNKNOWN;
2558 
2559 	if (WARN_ON(ath11k_mac_vif_chan(vif, &def)))
2560 		return;
2561 
2562 	band = def.chan->band;
2563 	ht_mcs_mask = arvif->bitrate_mask.control[band].ht_mcs;
2564 	vht_mcs_mask = arvif->bitrate_mask.control[band].vht_mcs;
2565 	he_mcs_mask = arvif->bitrate_mask.control[band].he_mcs;
2566 
2567 	switch (band) {
2568 	case NL80211_BAND_2GHZ:
2569 		if (sta->he_cap.has_he &&
2570 		    !ath11k_peer_assoc_h_he_masked(he_mcs_mask)) {
2571 			if (sta->bandwidth == IEEE80211_STA_RX_BW_80)
2572 				phymode = MODE_11AX_HE80_2G;
2573 			else if (sta->bandwidth == IEEE80211_STA_RX_BW_40)
2574 				phymode = MODE_11AX_HE40_2G;
2575 			else
2576 				phymode = MODE_11AX_HE20_2G;
2577 		} else if (sta->vht_cap.vht_supported &&
2578 		    !ath11k_peer_assoc_h_vht_masked(vht_mcs_mask)) {
2579 			if (sta->bandwidth == IEEE80211_STA_RX_BW_40)
2580 				phymode = MODE_11AC_VHT40;
2581 			else
2582 				phymode = MODE_11AC_VHT20;
2583 		} else if (sta->ht_cap.ht_supported &&
2584 			   !ath11k_peer_assoc_h_ht_masked(ht_mcs_mask)) {
2585 			if (sta->bandwidth == IEEE80211_STA_RX_BW_40)
2586 				phymode = MODE_11NG_HT40;
2587 			else
2588 				phymode = MODE_11NG_HT20;
2589 		} else if (ath11k_mac_sta_has_ofdm_only(sta)) {
2590 			phymode = MODE_11G;
2591 		} else {
2592 			phymode = MODE_11B;
2593 		}
2594 		break;
2595 	case NL80211_BAND_5GHZ:
2596 	case NL80211_BAND_6GHZ:
2597 		/* Check HE first */
2598 		if (sta->he_cap.has_he &&
2599 		    !ath11k_peer_assoc_h_he_masked(he_mcs_mask)) {
2600 			phymode = ath11k_mac_get_phymode_he(ar, sta);
2601 		} else if (sta->vht_cap.vht_supported &&
2602 		    !ath11k_peer_assoc_h_vht_masked(vht_mcs_mask)) {
2603 			phymode = ath11k_mac_get_phymode_vht(ar, sta);
2604 		} else if (sta->ht_cap.ht_supported &&
2605 			   !ath11k_peer_assoc_h_ht_masked(ht_mcs_mask)) {
2606 			if (sta->bandwidth >= IEEE80211_STA_RX_BW_40)
2607 				phymode = MODE_11NA_HT40;
2608 			else
2609 				phymode = MODE_11NA_HT20;
2610 		} else {
2611 			phymode = MODE_11A;
2612 		}
2613 		break;
2614 	default:
2615 		break;
2616 	}
2617 
2618 	ath11k_dbg(ar->ab, ATH11K_DBG_MAC, "mac peer %pM phymode %s\n",
2619 		   sta->addr, ath11k_wmi_phymode_str(phymode));
2620 
2621 	arg->peer_phymode = phymode;
2622 	WARN_ON(phymode == MODE_UNKNOWN);
2623 }
2624 
2625 static void ath11k_peer_assoc_prepare(struct ath11k *ar,
2626 				      struct ieee80211_vif *vif,
2627 				      struct ieee80211_sta *sta,
2628 				      struct peer_assoc_params *arg,
2629 				      bool reassoc)
2630 {
2631 	struct ath11k_sta *arsta;
2632 
2633 	lockdep_assert_held(&ar->conf_mutex);
2634 
2635 	arsta = (struct ath11k_sta *)sta->drv_priv;
2636 
2637 	memset(arg, 0, sizeof(*arg));
2638 
2639 	reinit_completion(&ar->peer_assoc_done);
2640 
2641 	arg->peer_new_assoc = !reassoc;
2642 	ath11k_peer_assoc_h_basic(ar, vif, sta, arg);
2643 	ath11k_peer_assoc_h_crypto(ar, vif, sta, arg);
2644 	ath11k_peer_assoc_h_rates(ar, vif, sta, arg);
2645 	ath11k_peer_assoc_h_phymode(ar, vif, sta, arg);
2646 	ath11k_peer_assoc_h_ht(ar, vif, sta, arg);
2647 	ath11k_peer_assoc_h_vht(ar, vif, sta, arg);
2648 	ath11k_peer_assoc_h_he(ar, vif, sta, arg);
2649 	ath11k_peer_assoc_h_he_6ghz(ar, vif, sta, arg);
2650 	ath11k_peer_assoc_h_qos(ar, vif, sta, arg);
2651 	ath11k_peer_assoc_h_smps(sta, arg);
2652 
2653 	arsta->peer_nss = arg->peer_nss;
2654 
2655 	/* TODO: amsdu_disable req? */
2656 }
2657 
2658 static int ath11k_setup_peer_smps(struct ath11k *ar, struct ath11k_vif *arvif,
2659 				  const u8 *addr,
2660 				  const struct ieee80211_sta_ht_cap *ht_cap,
2661 				  u16 he_6ghz_capa)
2662 {
2663 	int smps;
2664 
2665 	if (!ht_cap->ht_supported && !he_6ghz_capa)
2666 		return 0;
2667 
2668 	if (ht_cap->ht_supported) {
2669 		smps = ht_cap->cap & IEEE80211_HT_CAP_SM_PS;
2670 		smps >>= IEEE80211_HT_CAP_SM_PS_SHIFT;
2671 	} else {
2672 		smps = FIELD_GET(IEEE80211_HE_6GHZ_CAP_SM_PS, he_6ghz_capa);
2673 	}
2674 
2675 	if (smps >= ARRAY_SIZE(ath11k_smps_map))
2676 		return -EINVAL;
2677 
2678 	return ath11k_wmi_set_peer_param(ar, addr, arvif->vdev_id,
2679 					 WMI_PEER_MIMO_PS_STATE,
2680 					 ath11k_smps_map[smps]);
2681 }
2682 
2683 static void ath11k_bss_assoc(struct ieee80211_hw *hw,
2684 			     struct ieee80211_vif *vif,
2685 			     struct ieee80211_bss_conf *bss_conf)
2686 {
2687 	struct ath11k *ar = hw->priv;
2688 	struct ath11k_vif *arvif = (void *)vif->drv_priv;
2689 	struct peer_assoc_params peer_arg;
2690 	struct ieee80211_sta *ap_sta;
2691 	struct ath11k_peer *peer;
2692 	bool is_auth = false;
2693 	int ret;
2694 
2695 	lockdep_assert_held(&ar->conf_mutex);
2696 
2697 	ath11k_dbg(ar->ab, ATH11K_DBG_MAC, "mac vdev %i assoc bssid %pM aid %d\n",
2698 		   arvif->vdev_id, arvif->bssid, arvif->aid);
2699 
2700 	rcu_read_lock();
2701 
2702 	ap_sta = ieee80211_find_sta(vif, bss_conf->bssid);
2703 	if (!ap_sta) {
2704 		ath11k_warn(ar->ab, "failed to find station entry for bss %pM vdev %i\n",
2705 			    bss_conf->bssid, arvif->vdev_id);
2706 		rcu_read_unlock();
2707 		return;
2708 	}
2709 
2710 	ath11k_peer_assoc_prepare(ar, vif, ap_sta, &peer_arg, false);
2711 
2712 	rcu_read_unlock();
2713 
2714 	peer_arg.is_assoc = true;
2715 	ret = ath11k_wmi_send_peer_assoc_cmd(ar, &peer_arg);
2716 	if (ret) {
2717 		ath11k_warn(ar->ab, "failed to run peer assoc for %pM vdev %i: %d\n",
2718 			    bss_conf->bssid, arvif->vdev_id, ret);
2719 		return;
2720 	}
2721 
2722 	if (!wait_for_completion_timeout(&ar->peer_assoc_done, 1 * HZ)) {
2723 		ath11k_warn(ar->ab, "failed to get peer assoc conf event for %pM vdev %i\n",
2724 			    bss_conf->bssid, arvif->vdev_id);
2725 		return;
2726 	}
2727 
2728 	ret = ath11k_setup_peer_smps(ar, arvif, bss_conf->bssid,
2729 				     &ap_sta->ht_cap,
2730 				     le16_to_cpu(ap_sta->he_6ghz_capa.capa));
2731 	if (ret) {
2732 		ath11k_warn(ar->ab, "failed to setup peer SMPS for vdev %d: %d\n",
2733 			    arvif->vdev_id, ret);
2734 		return;
2735 	}
2736 
2737 	WARN_ON(arvif->is_up);
2738 
2739 	arvif->aid = bss_conf->aid;
2740 	ether_addr_copy(arvif->bssid, bss_conf->bssid);
2741 
2742 	ret = ath11k_wmi_vdev_up(ar, arvif->vdev_id, arvif->aid, arvif->bssid);
2743 	if (ret) {
2744 		ath11k_warn(ar->ab, "failed to set vdev %d up: %d\n",
2745 			    arvif->vdev_id, ret);
2746 		return;
2747 	}
2748 
2749 	arvif->is_up = true;
2750 
2751 	ath11k_dbg(ar->ab, ATH11K_DBG_MAC,
2752 		   "mac vdev %d up (associated) bssid %pM aid %d\n",
2753 		   arvif->vdev_id, bss_conf->bssid, bss_conf->aid);
2754 
2755 	spin_lock_bh(&ar->ab->base_lock);
2756 
2757 	peer = ath11k_peer_find(ar->ab, arvif->vdev_id, arvif->bssid);
2758 	if (peer && peer->is_authorized)
2759 		is_auth = true;
2760 
2761 	spin_unlock_bh(&ar->ab->base_lock);
2762 
2763 	if (is_auth) {
2764 		ret = ath11k_wmi_set_peer_param(ar, arvif->bssid,
2765 						arvif->vdev_id,
2766 						WMI_PEER_AUTHORIZE,
2767 						1);
2768 		if (ret)
2769 			ath11k_warn(ar->ab, "Unable to authorize BSS peer: %d\n", ret);
2770 	}
2771 
2772 	ret = ath11k_wmi_send_obss_spr_cmd(ar, arvif->vdev_id,
2773 					   &bss_conf->he_obss_pd);
2774 	if (ret)
2775 		ath11k_warn(ar->ab, "failed to set vdev %i OBSS PD parameters: %d\n",
2776 			    arvif->vdev_id, ret);
2777 
2778 	ret = ath11k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
2779 					    WMI_VDEV_PARAM_DTIM_POLICY,
2780 					    WMI_DTIM_POLICY_STICK);
2781 	if (ret)
2782 		ath11k_warn(ar->ab, "failed to set vdev %d dtim policy: %d\n",
2783 			    arvif->vdev_id, ret);
2784 
2785 	ath11k_mac_11d_scan_stop_all(ar->ab);
2786 }
2787 
2788 static void ath11k_bss_disassoc(struct ieee80211_hw *hw,
2789 				struct ieee80211_vif *vif)
2790 {
2791 	struct ath11k *ar = hw->priv;
2792 	struct ath11k_vif *arvif = (void *)vif->drv_priv;
2793 	int ret;
2794 
2795 	lockdep_assert_held(&ar->conf_mutex);
2796 
2797 	ath11k_dbg(ar->ab, ATH11K_DBG_MAC, "mac vdev %i disassoc bssid %pM\n",
2798 		   arvif->vdev_id, arvif->bssid);
2799 
2800 	ret = ath11k_wmi_vdev_down(ar, arvif->vdev_id);
2801 	if (ret)
2802 		ath11k_warn(ar->ab, "failed to down vdev %i: %d\n",
2803 			    arvif->vdev_id, ret);
2804 
2805 	arvif->is_up = false;
2806 
2807 	cancel_delayed_work_sync(&arvif->connection_loss_work);
2808 }
2809 
2810 static u32 ath11k_mac_get_rate_hw_value(int bitrate)
2811 {
2812 	u32 preamble;
2813 	u16 hw_value;
2814 	int rate;
2815 	size_t i;
2816 
2817 	if (ath11k_mac_bitrate_is_cck(bitrate))
2818 		preamble = WMI_RATE_PREAMBLE_CCK;
2819 	else
2820 		preamble = WMI_RATE_PREAMBLE_OFDM;
2821 
2822 	for (i = 0; i < ARRAY_SIZE(ath11k_legacy_rates); i++) {
2823 		if (ath11k_legacy_rates[i].bitrate != bitrate)
2824 			continue;
2825 
2826 		hw_value = ath11k_legacy_rates[i].hw_value;
2827 		rate = ATH11K_HW_RATE_CODE(hw_value, 0, preamble);
2828 
2829 		return rate;
2830 	}
2831 
2832 	return -EINVAL;
2833 }
2834 
2835 static void ath11k_recalculate_mgmt_rate(struct ath11k *ar,
2836 					 struct ieee80211_vif *vif,
2837 					 struct cfg80211_chan_def *def)
2838 {
2839 	struct ath11k_vif *arvif = (void *)vif->drv_priv;
2840 	const struct ieee80211_supported_band *sband;
2841 	u8 basic_rate_idx;
2842 	int hw_rate_code;
2843 	u32 vdev_param;
2844 	u16 bitrate;
2845 	int ret;
2846 
2847 	lockdep_assert_held(&ar->conf_mutex);
2848 
2849 	sband = ar->hw->wiphy->bands[def->chan->band];
2850 	basic_rate_idx = ffs(vif->bss_conf.basic_rates) - 1;
2851 	bitrate = sband->bitrates[basic_rate_idx].bitrate;
2852 
2853 	hw_rate_code = ath11k_mac_get_rate_hw_value(bitrate);
2854 	if (hw_rate_code < 0) {
2855 		ath11k_warn(ar->ab, "bitrate not supported %d\n", bitrate);
2856 		return;
2857 	}
2858 
2859 	vdev_param = WMI_VDEV_PARAM_MGMT_RATE;
2860 	ret = ath11k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id, vdev_param,
2861 					    hw_rate_code);
2862 	if (ret)
2863 		ath11k_warn(ar->ab, "failed to set mgmt tx rate %d\n", ret);
2864 
2865 	vdev_param = WMI_VDEV_PARAM_BEACON_RATE;
2866 	ret = ath11k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id, vdev_param,
2867 					    hw_rate_code);
2868 	if (ret)
2869 		ath11k_warn(ar->ab, "failed to set beacon tx rate %d\n", ret);
2870 }
2871 
2872 static int ath11k_mac_fils_discovery(struct ath11k_vif *arvif,
2873 				     struct ieee80211_bss_conf *info)
2874 {
2875 	struct ath11k *ar = arvif->ar;
2876 	struct sk_buff *tmpl;
2877 	int ret;
2878 	u32 interval;
2879 	bool unsol_bcast_probe_resp_enabled = false;
2880 
2881 	if (info->fils_discovery.max_interval) {
2882 		interval = info->fils_discovery.max_interval;
2883 
2884 		tmpl = ieee80211_get_fils_discovery_tmpl(ar->hw, arvif->vif);
2885 		if (tmpl)
2886 			ret = ath11k_wmi_fils_discovery_tmpl(ar, arvif->vdev_id,
2887 							     tmpl);
2888 	} else if (info->unsol_bcast_probe_resp_interval) {
2889 		unsol_bcast_probe_resp_enabled = 1;
2890 		interval = info->unsol_bcast_probe_resp_interval;
2891 
2892 		tmpl = ieee80211_get_unsol_bcast_probe_resp_tmpl(ar->hw,
2893 								 arvif->vif);
2894 		if (tmpl)
2895 			ret = ath11k_wmi_probe_resp_tmpl(ar, arvif->vdev_id,
2896 							 tmpl);
2897 	} else { /* Disable */
2898 		return ath11k_wmi_fils_discovery(ar, arvif->vdev_id, 0, false);
2899 	}
2900 
2901 	if (!tmpl) {
2902 		ath11k_warn(ar->ab,
2903 			    "mac vdev %i failed to retrieve %s template\n",
2904 			    arvif->vdev_id, (unsol_bcast_probe_resp_enabled ?
2905 			    "unsolicited broadcast probe response" :
2906 			    "FILS discovery"));
2907 		return -EPERM;
2908 	}
2909 	kfree_skb(tmpl);
2910 
2911 	if (!ret)
2912 		ret = ath11k_wmi_fils_discovery(ar, arvif->vdev_id, interval,
2913 						unsol_bcast_probe_resp_enabled);
2914 
2915 	return ret;
2916 }
2917 
2918 static int ath11k_mac_config_obss_pd(struct ath11k *ar,
2919 				     struct ieee80211_he_obss_pd *he_obss_pd)
2920 {
2921 	u32 bitmap[2], param_id, param_val, pdev_id;
2922 	int ret;
2923 	s8 non_srg_th = 0, srg_th = 0;
2924 
2925 	pdev_id = ar->pdev->pdev_id;
2926 
2927 	/* Set and enable SRG/non-SRG OBSS PD Threshold */
2928 	param_id = WMI_PDEV_PARAM_SET_CMD_OBSS_PD_THRESHOLD;
2929 	if (test_bit(ATH11K_FLAG_MONITOR_STARTED, &ar->monitor_flags)) {
2930 		ret = ath11k_wmi_pdev_set_param(ar, param_id, 0, pdev_id);
2931 		if (ret)
2932 			ath11k_warn(ar->ab,
2933 				    "failed to set obss_pd_threshold for pdev: %u\n",
2934 				    pdev_id);
2935 		return ret;
2936 	}
2937 
2938 	ath11k_dbg(ar->ab, ATH11K_DBG_MAC,
2939 		   "mac obss pd sr_ctrl %x non_srg_thres %u srg_max %u\n",
2940 		   he_obss_pd->sr_ctrl, he_obss_pd->non_srg_max_offset,
2941 		   he_obss_pd->max_offset);
2942 
2943 	param_val = 0;
2944 
2945 	if (he_obss_pd->sr_ctrl &
2946 	    IEEE80211_HE_SPR_NON_SRG_OBSS_PD_SR_DISALLOWED) {
2947 		non_srg_th = ATH11K_OBSS_PD_MAX_THRESHOLD;
2948 	} else {
2949 		if (he_obss_pd->sr_ctrl & IEEE80211_HE_SPR_NON_SRG_OFFSET_PRESENT)
2950 			non_srg_th = (ATH11K_OBSS_PD_MAX_THRESHOLD +
2951 				      he_obss_pd->non_srg_max_offset);
2952 		else
2953 			non_srg_th = ATH11K_OBSS_PD_NON_SRG_MAX_THRESHOLD;
2954 
2955 		param_val |= ATH11K_OBSS_PD_NON_SRG_EN;
2956 	}
2957 
2958 	if (he_obss_pd->sr_ctrl & IEEE80211_HE_SPR_SRG_INFORMATION_PRESENT) {
2959 		srg_th = ATH11K_OBSS_PD_MAX_THRESHOLD + he_obss_pd->max_offset;
2960 		param_val |= ATH11K_OBSS_PD_SRG_EN;
2961 	}
2962 
2963 	if (test_bit(WMI_TLV_SERVICE_SRG_SRP_SPATIAL_REUSE_SUPPORT,
2964 		     ar->ab->wmi_ab.svc_map)) {
2965 		param_val |= ATH11K_OBSS_PD_THRESHOLD_IN_DBM;
2966 		param_val |= FIELD_PREP(GENMASK(15, 8), srg_th);
2967 	} else {
2968 		non_srg_th -= ATH11K_DEFAULT_NOISE_FLOOR;
2969 		/* SRG not supported and threshold in dB */
2970 		param_val &= ~(ATH11K_OBSS_PD_SRG_EN |
2971 			       ATH11K_OBSS_PD_THRESHOLD_IN_DBM);
2972 	}
2973 
2974 	param_val |= (non_srg_th & GENMASK(7, 0));
2975 	ret = ath11k_wmi_pdev_set_param(ar, param_id, param_val, pdev_id);
2976 	if (ret) {
2977 		ath11k_warn(ar->ab,
2978 			    "failed to set obss_pd_threshold for pdev: %u\n",
2979 			    pdev_id);
2980 		return ret;
2981 	}
2982 
2983 	/* Enable OBSS PD for all access category */
2984 	param_id  = WMI_PDEV_PARAM_SET_CMD_OBSS_PD_PER_AC;
2985 	param_val = 0xf;
2986 	ret = ath11k_wmi_pdev_set_param(ar, param_id, param_val, pdev_id);
2987 	if (ret) {
2988 		ath11k_warn(ar->ab,
2989 			    "failed to set obss_pd_per_ac for pdev: %u\n",
2990 			    pdev_id);
2991 		return ret;
2992 	}
2993 
2994 	/* Set SR Prohibit */
2995 	param_id  = WMI_PDEV_PARAM_ENABLE_SR_PROHIBIT;
2996 	param_val = !!(he_obss_pd->sr_ctrl &
2997 		       IEEE80211_HE_SPR_HESIGA_SR_VAL15_ALLOWED);
2998 	ret = ath11k_wmi_pdev_set_param(ar, param_id, param_val, pdev_id);
2999 	if (ret) {
3000 		ath11k_warn(ar->ab, "failed to set sr_prohibit for pdev: %u\n",
3001 			    pdev_id);
3002 		return ret;
3003 	}
3004 
3005 	if (!test_bit(WMI_TLV_SERVICE_SRG_SRP_SPATIAL_REUSE_SUPPORT,
3006 		      ar->ab->wmi_ab.svc_map))
3007 		return 0;
3008 
3009 	/* Set SRG BSS Color Bitmap */
3010 	memcpy(bitmap, he_obss_pd->bss_color_bitmap, sizeof(bitmap));
3011 	ret = ath11k_wmi_pdev_set_srg_bss_color_bitmap(ar, bitmap);
3012 	if (ret) {
3013 		ath11k_warn(ar->ab,
3014 			    "failed to set bss_color_bitmap for pdev: %u\n",
3015 			    pdev_id);
3016 		return ret;
3017 	}
3018 
3019 	/* Set SRG Partial BSSID Bitmap */
3020 	memcpy(bitmap, he_obss_pd->partial_bssid_bitmap, sizeof(bitmap));
3021 	ret = ath11k_wmi_pdev_set_srg_patial_bssid_bitmap(ar, bitmap);
3022 	if (ret) {
3023 		ath11k_warn(ar->ab,
3024 			    "failed to set partial_bssid_bitmap for pdev: %u\n",
3025 			    pdev_id);
3026 		return ret;
3027 	}
3028 
3029 	memset(bitmap, 0xff, sizeof(bitmap));
3030 
3031 	/* Enable all BSS Colors for SRG */
3032 	ret = ath11k_wmi_pdev_srg_obss_color_enable_bitmap(ar, bitmap);
3033 	if (ret) {
3034 		ath11k_warn(ar->ab,
3035 			    "failed to set srg_color_en_bitmap pdev: %u\n",
3036 			    pdev_id);
3037 		return ret;
3038 	}
3039 
3040 	/* Enable all patial BSSID mask for SRG */
3041 	ret = ath11k_wmi_pdev_srg_obss_bssid_enable_bitmap(ar, bitmap);
3042 	if (ret) {
3043 		ath11k_warn(ar->ab,
3044 			    "failed to set srg_bssid_en_bitmap pdev: %u\n",
3045 			    pdev_id);
3046 		return ret;
3047 	}
3048 
3049 	/* Enable all BSS Colors for non-SRG */
3050 	ret = ath11k_wmi_pdev_non_srg_obss_color_enable_bitmap(ar, bitmap);
3051 	if (ret) {
3052 		ath11k_warn(ar->ab,
3053 			    "failed to set non_srg_color_en_bitmap pdev: %u\n",
3054 			    pdev_id);
3055 		return ret;
3056 	}
3057 
3058 	/* Enable all patial BSSID mask for non-SRG */
3059 	ret = ath11k_wmi_pdev_non_srg_obss_bssid_enable_bitmap(ar, bitmap);
3060 	if (ret) {
3061 		ath11k_warn(ar->ab,
3062 			    "failed to set non_srg_bssid_en_bitmap pdev: %u\n",
3063 			    pdev_id);
3064 		return ret;
3065 	}
3066 
3067 	return 0;
3068 }
3069 
3070 static void ath11k_mac_op_bss_info_changed(struct ieee80211_hw *hw,
3071 					   struct ieee80211_vif *vif,
3072 					   struct ieee80211_bss_conf *info,
3073 					   u32 changed)
3074 {
3075 	struct ath11k *ar = hw->priv;
3076 	struct ath11k_vif *arvif = ath11k_vif_to_arvif(vif);
3077 	struct cfg80211_chan_def def;
3078 	u32 param_id, param_value;
3079 	enum nl80211_band band;
3080 	u32 vdev_param;
3081 	int mcast_rate;
3082 	u32 preamble;
3083 	u16 hw_value;
3084 	u16 bitrate;
3085 	int ret = 0;
3086 	u8 rateidx;
3087 	u32 rate;
3088 
3089 	mutex_lock(&ar->conf_mutex);
3090 
3091 	if (changed & BSS_CHANGED_BEACON_INT) {
3092 		arvif->beacon_interval = info->beacon_int;
3093 
3094 		param_id = WMI_VDEV_PARAM_BEACON_INTERVAL;
3095 		ret = ath11k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
3096 						    param_id,
3097 						    arvif->beacon_interval);
3098 		if (ret)
3099 			ath11k_warn(ar->ab, "Failed to set beacon interval for VDEV: %d\n",
3100 				    arvif->vdev_id);
3101 		else
3102 			ath11k_dbg(ar->ab, ATH11K_DBG_MAC,
3103 				   "Beacon interval: %d set for VDEV: %d\n",
3104 				   arvif->beacon_interval, arvif->vdev_id);
3105 	}
3106 
3107 	if (changed & BSS_CHANGED_BEACON) {
3108 		param_id = WMI_PDEV_PARAM_BEACON_TX_MODE;
3109 		param_value = WMI_BEACON_STAGGERED_MODE;
3110 		ret = ath11k_wmi_pdev_set_param(ar, param_id,
3111 						param_value, ar->pdev->pdev_id);
3112 		if (ret)
3113 			ath11k_warn(ar->ab, "Failed to set beacon mode for VDEV: %d\n",
3114 				    arvif->vdev_id);
3115 		else
3116 			ath11k_dbg(ar->ab, ATH11K_DBG_MAC,
3117 				   "Set staggered beacon mode for VDEV: %d\n",
3118 				   arvif->vdev_id);
3119 
3120 		if (!arvif->do_not_send_tmpl || !arvif->bcca_zero_sent) {
3121 			ret = ath11k_mac_setup_bcn_tmpl(arvif);
3122 			if (ret)
3123 				ath11k_warn(ar->ab, "failed to update bcn template: %d\n",
3124 					    ret);
3125 		}
3126 
3127 		if (arvif->bcca_zero_sent)
3128 			arvif->do_not_send_tmpl = true;
3129 		else
3130 			arvif->do_not_send_tmpl = false;
3131 	}
3132 
3133 	if (changed & (BSS_CHANGED_BEACON_INFO | BSS_CHANGED_BEACON)) {
3134 		arvif->dtim_period = info->dtim_period;
3135 
3136 		param_id = WMI_VDEV_PARAM_DTIM_PERIOD;
3137 		ret = ath11k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
3138 						    param_id,
3139 						    arvif->dtim_period);
3140 
3141 		if (ret)
3142 			ath11k_warn(ar->ab, "Failed to set dtim period for VDEV %d: %i\n",
3143 				    arvif->vdev_id, ret);
3144 		else
3145 			ath11k_dbg(ar->ab, ATH11K_DBG_MAC,
3146 				   "DTIM period: %d set for VDEV: %d\n",
3147 				   arvif->dtim_period, arvif->vdev_id);
3148 	}
3149 
3150 	if (changed & BSS_CHANGED_SSID &&
3151 	    vif->type == NL80211_IFTYPE_AP) {
3152 		arvif->u.ap.ssid_len = info->ssid_len;
3153 		if (info->ssid_len)
3154 			memcpy(arvif->u.ap.ssid, info->ssid, info->ssid_len);
3155 		arvif->u.ap.hidden_ssid = info->hidden_ssid;
3156 	}
3157 
3158 	if (changed & BSS_CHANGED_BSSID && !is_zero_ether_addr(info->bssid))
3159 		ether_addr_copy(arvif->bssid, info->bssid);
3160 
3161 	if (changed & BSS_CHANGED_BEACON_ENABLED) {
3162 		ath11k_control_beaconing(arvif, info);
3163 
3164 		if (arvif->is_up && vif->bss_conf.he_support &&
3165 		    vif->bss_conf.he_oper.params) {
3166 			ret = ath11k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
3167 							    WMI_VDEV_PARAM_BA_MODE,
3168 							    WMI_BA_MODE_BUFFER_SIZE_256);
3169 			if (ret)
3170 				ath11k_warn(ar->ab,
3171 					    "failed to set BA BUFFER SIZE 256 for vdev: %d\n",
3172 					    arvif->vdev_id);
3173 
3174 			param_id = WMI_VDEV_PARAM_HEOPS_0_31;
3175 			param_value = vif->bss_conf.he_oper.params;
3176 			ret = ath11k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
3177 							    param_id, param_value);
3178 			ath11k_dbg(ar->ab, ATH11K_DBG_MAC,
3179 				   "he oper param: %x set for VDEV: %d\n",
3180 				   param_value, arvif->vdev_id);
3181 
3182 			if (ret)
3183 				ath11k_warn(ar->ab, "Failed to set he oper params %x for VDEV %d: %i\n",
3184 					    param_value, arvif->vdev_id, ret);
3185 		}
3186 	}
3187 
3188 	if (changed & BSS_CHANGED_ERP_CTS_PROT) {
3189 		u32 cts_prot;
3190 
3191 		cts_prot = !!(info->use_cts_prot);
3192 		param_id = WMI_VDEV_PARAM_PROTECTION_MODE;
3193 
3194 		if (arvif->is_started) {
3195 			ret = ath11k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
3196 							    param_id, cts_prot);
3197 			if (ret)
3198 				ath11k_warn(ar->ab, "Failed to set CTS prot for VDEV: %d\n",
3199 					    arvif->vdev_id);
3200 			else
3201 				ath11k_dbg(ar->ab, ATH11K_DBG_MAC, "Set CTS prot: %d for VDEV: %d\n",
3202 					   cts_prot, arvif->vdev_id);
3203 		} else {
3204 			ath11k_dbg(ar->ab, ATH11K_DBG_MAC, "defer protection mode setup, vdev is not ready yet\n");
3205 		}
3206 	}
3207 
3208 	if (changed & BSS_CHANGED_ERP_SLOT) {
3209 		u32 slottime;
3210 
3211 		if (info->use_short_slot)
3212 			slottime = WMI_VDEV_SLOT_TIME_SHORT; /* 9us */
3213 
3214 		else
3215 			slottime = WMI_VDEV_SLOT_TIME_LONG; /* 20us */
3216 
3217 		param_id = WMI_VDEV_PARAM_SLOT_TIME;
3218 		ret = ath11k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
3219 						    param_id, slottime);
3220 		if (ret)
3221 			ath11k_warn(ar->ab, "Failed to set erp slot for VDEV: %d\n",
3222 				    arvif->vdev_id);
3223 		else
3224 			ath11k_dbg(ar->ab, ATH11K_DBG_MAC,
3225 				   "Set slottime: %d for VDEV: %d\n",
3226 				   slottime, arvif->vdev_id);
3227 	}
3228 
3229 	if (changed & BSS_CHANGED_ERP_PREAMBLE) {
3230 		u32 preamble;
3231 
3232 		if (info->use_short_preamble)
3233 			preamble = WMI_VDEV_PREAMBLE_SHORT;
3234 		else
3235 			preamble = WMI_VDEV_PREAMBLE_LONG;
3236 
3237 		param_id = WMI_VDEV_PARAM_PREAMBLE;
3238 		ret = ath11k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
3239 						    param_id, preamble);
3240 		if (ret)
3241 			ath11k_warn(ar->ab, "Failed to set preamble for VDEV: %d\n",
3242 				    arvif->vdev_id);
3243 		else
3244 			ath11k_dbg(ar->ab, ATH11K_DBG_MAC,
3245 				   "Set preamble: %d for VDEV: %d\n",
3246 				   preamble, arvif->vdev_id);
3247 	}
3248 
3249 	if (changed & BSS_CHANGED_ASSOC) {
3250 		if (info->assoc)
3251 			ath11k_bss_assoc(hw, vif, info);
3252 		else
3253 			ath11k_bss_disassoc(hw, vif);
3254 	}
3255 
3256 	if (changed & BSS_CHANGED_TXPOWER) {
3257 		ath11k_dbg(ar->ab, ATH11K_DBG_MAC, "mac vdev_id %i txpower %d\n",
3258 			   arvif->vdev_id, info->txpower);
3259 
3260 		arvif->txpower = info->txpower;
3261 		ath11k_mac_txpower_recalc(ar);
3262 	}
3263 
3264 	if (changed & BSS_CHANGED_PS &&
3265 	    ar->ab->hw_params.supports_sta_ps) {
3266 		arvif->ps = vif->bss_conf.ps;
3267 
3268 		ret = ath11k_mac_config_ps(ar);
3269 		if (ret)
3270 			ath11k_warn(ar->ab, "failed to setup ps on vdev %i: %d\n",
3271 				    arvif->vdev_id, ret);
3272 	}
3273 
3274 	if (changed & BSS_CHANGED_MCAST_RATE &&
3275 	    !ath11k_mac_vif_chan(arvif->vif, &def)) {
3276 		band = def.chan->band;
3277 		mcast_rate = vif->bss_conf.mcast_rate[band];
3278 
3279 		if (mcast_rate > 0)
3280 			rateidx = mcast_rate - 1;
3281 		else
3282 			rateidx = ffs(vif->bss_conf.basic_rates) - 1;
3283 
3284 		if (ar->pdev->cap.supported_bands & WMI_HOST_WLAN_5G_CAP)
3285 			rateidx += ATH11K_MAC_FIRST_OFDM_RATE_IDX;
3286 
3287 		bitrate = ath11k_legacy_rates[rateidx].bitrate;
3288 		hw_value = ath11k_legacy_rates[rateidx].hw_value;
3289 
3290 		if (ath11k_mac_bitrate_is_cck(bitrate))
3291 			preamble = WMI_RATE_PREAMBLE_CCK;
3292 		else
3293 			preamble = WMI_RATE_PREAMBLE_OFDM;
3294 
3295 		rate = ATH11K_HW_RATE_CODE(hw_value, 0, preamble);
3296 
3297 		ath11k_dbg(ar->ab, ATH11K_DBG_MAC,
3298 			   "mac vdev %d mcast_rate %x\n",
3299 			   arvif->vdev_id, rate);
3300 
3301 		vdev_param = WMI_VDEV_PARAM_MCAST_DATA_RATE;
3302 		ret = ath11k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
3303 						    vdev_param, rate);
3304 		if (ret)
3305 			ath11k_warn(ar->ab,
3306 				    "failed to set mcast rate on vdev %i: %d\n",
3307 				    arvif->vdev_id,  ret);
3308 
3309 		vdev_param = WMI_VDEV_PARAM_BCAST_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 bcast rate on vdev %i: %d\n",
3315 				    arvif->vdev_id,  ret);
3316 	}
3317 
3318 	if (changed & BSS_CHANGED_BASIC_RATES &&
3319 	    !ath11k_mac_vif_chan(arvif->vif, &def))
3320 		ath11k_recalculate_mgmt_rate(ar, vif, &def);
3321 
3322 	if (changed & BSS_CHANGED_TWT) {
3323 		if (info->twt_requester || info->twt_responder)
3324 			ath11k_wmi_send_twt_enable_cmd(ar, ar->pdev->pdev_id);
3325 		else
3326 			ath11k_wmi_send_twt_disable_cmd(ar, ar->pdev->pdev_id);
3327 	}
3328 
3329 	if (changed & BSS_CHANGED_HE_OBSS_PD)
3330 		ath11k_mac_config_obss_pd(ar, &info->he_obss_pd);
3331 
3332 	if (changed & BSS_CHANGED_HE_BSS_COLOR) {
3333 		if (vif->type == NL80211_IFTYPE_AP) {
3334 			ret = ath11k_wmi_send_obss_color_collision_cfg_cmd(
3335 				ar, arvif->vdev_id, info->he_bss_color.color,
3336 				ATH11K_BSS_COLOR_COLLISION_DETECTION_AP_PERIOD_MS,
3337 				info->he_bss_color.enabled);
3338 			if (ret)
3339 				ath11k_warn(ar->ab, "failed to set bss color collision on vdev %i: %d\n",
3340 					    arvif->vdev_id,  ret);
3341 
3342 			param_id = WMI_VDEV_PARAM_BSS_COLOR;
3343 			if (info->he_bss_color.enabled)
3344 				param_value = info->he_bss_color.color <<
3345 						IEEE80211_HE_OPERATION_BSS_COLOR_OFFSET;
3346 			else
3347 				param_value = IEEE80211_HE_OPERATION_BSS_COLOR_DISABLED;
3348 
3349 			ret = ath11k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
3350 							    param_id,
3351 							    param_value);
3352 			if (ret)
3353 				ath11k_warn(ar->ab,
3354 					    "failed to set bss color param on vdev %i: %d\n",
3355 					    arvif->vdev_id,  ret);
3356 
3357 			ath11k_dbg(ar->ab, ATH11K_DBG_MAC,
3358 				   "bss color param 0x%x set on vdev %i\n",
3359 				   param_value, arvif->vdev_id);
3360 		} else if (vif->type == NL80211_IFTYPE_STATION) {
3361 			ret = ath11k_wmi_send_bss_color_change_enable_cmd(ar,
3362 									  arvif->vdev_id,
3363 									  1);
3364 			if (ret)
3365 				ath11k_warn(ar->ab, "failed to enable bss color change on vdev %i: %d\n",
3366 					    arvif->vdev_id,  ret);
3367 			ret = ath11k_wmi_send_obss_color_collision_cfg_cmd(
3368 				ar, arvif->vdev_id, 0,
3369 				ATH11K_BSS_COLOR_COLLISION_DETECTION_STA_PERIOD_MS, 1);
3370 			if (ret)
3371 				ath11k_warn(ar->ab, "failed to set bss color collision on vdev %i: %d\n",
3372 					    arvif->vdev_id,  ret);
3373 		}
3374 	}
3375 
3376 	if (changed & BSS_CHANGED_FILS_DISCOVERY ||
3377 	    changed & BSS_CHANGED_UNSOL_BCAST_PROBE_RESP)
3378 		ath11k_mac_fils_discovery(arvif, info);
3379 
3380 	mutex_unlock(&ar->conf_mutex);
3381 }
3382 
3383 void __ath11k_mac_scan_finish(struct ath11k *ar)
3384 {
3385 	lockdep_assert_held(&ar->data_lock);
3386 
3387 	switch (ar->scan.state) {
3388 	case ATH11K_SCAN_IDLE:
3389 		break;
3390 	case ATH11K_SCAN_RUNNING:
3391 	case ATH11K_SCAN_ABORTING:
3392 		if (ar->scan.is_roc && ar->scan.roc_notify)
3393 			ieee80211_remain_on_channel_expired(ar->hw);
3394 		fallthrough;
3395 	case ATH11K_SCAN_STARTING:
3396 		if (!ar->scan.is_roc) {
3397 			struct cfg80211_scan_info info = {
3398 				.aborted = ((ar->scan.state ==
3399 					    ATH11K_SCAN_ABORTING) ||
3400 					    (ar->scan.state ==
3401 					    ATH11K_SCAN_STARTING)),
3402 			};
3403 
3404 			ieee80211_scan_completed(ar->hw, &info);
3405 		}
3406 
3407 		ar->scan.state = ATH11K_SCAN_IDLE;
3408 		ar->scan_channel = NULL;
3409 		ar->scan.roc_freq = 0;
3410 		cancel_delayed_work(&ar->scan.timeout);
3411 		complete(&ar->scan.completed);
3412 		break;
3413 	}
3414 }
3415 
3416 void ath11k_mac_scan_finish(struct ath11k *ar)
3417 {
3418 	spin_lock_bh(&ar->data_lock);
3419 	__ath11k_mac_scan_finish(ar);
3420 	spin_unlock_bh(&ar->data_lock);
3421 }
3422 
3423 static int ath11k_scan_stop(struct ath11k *ar)
3424 {
3425 	struct scan_cancel_param arg = {
3426 		.req_type = WLAN_SCAN_CANCEL_SINGLE,
3427 		.scan_id = ATH11K_SCAN_ID,
3428 	};
3429 	int ret;
3430 
3431 	lockdep_assert_held(&ar->conf_mutex);
3432 
3433 	/* TODO: Fill other STOP Params */
3434 	arg.pdev_id = ar->pdev->pdev_id;
3435 
3436 	ret = ath11k_wmi_send_scan_stop_cmd(ar, &arg);
3437 	if (ret) {
3438 		ath11k_warn(ar->ab, "failed to stop wmi scan: %d\n", ret);
3439 		goto out;
3440 	}
3441 
3442 	ret = wait_for_completion_timeout(&ar->scan.completed, 3 * HZ);
3443 	if (ret == 0) {
3444 		ath11k_warn(ar->ab,
3445 			    "failed to receive scan abort comple: timed out\n");
3446 		ret = -ETIMEDOUT;
3447 	} else if (ret > 0) {
3448 		ret = 0;
3449 	}
3450 
3451 out:
3452 	/* Scan state should be updated upon scan completion but in case
3453 	 * firmware fails to deliver the event (for whatever reason) it is
3454 	 * desired to clean up scan state anyway. Firmware may have just
3455 	 * dropped the scan completion event delivery due to transport pipe
3456 	 * being overflown with data and/or it can recover on its own before
3457 	 * next scan request is submitted.
3458 	 */
3459 	spin_lock_bh(&ar->data_lock);
3460 	if (ar->scan.state != ATH11K_SCAN_IDLE)
3461 		__ath11k_mac_scan_finish(ar);
3462 	spin_unlock_bh(&ar->data_lock);
3463 
3464 	return ret;
3465 }
3466 
3467 static void ath11k_scan_abort(struct ath11k *ar)
3468 {
3469 	int ret;
3470 
3471 	lockdep_assert_held(&ar->conf_mutex);
3472 
3473 	spin_lock_bh(&ar->data_lock);
3474 
3475 	switch (ar->scan.state) {
3476 	case ATH11K_SCAN_IDLE:
3477 		/* This can happen if timeout worker kicked in and called
3478 		 * abortion while scan completion was being processed.
3479 		 */
3480 		break;
3481 	case ATH11K_SCAN_STARTING:
3482 	case ATH11K_SCAN_ABORTING:
3483 		ath11k_warn(ar->ab, "refusing scan abortion due to invalid scan state: %d\n",
3484 			    ar->scan.state);
3485 		break;
3486 	case ATH11K_SCAN_RUNNING:
3487 		ar->scan.state = ATH11K_SCAN_ABORTING;
3488 		spin_unlock_bh(&ar->data_lock);
3489 
3490 		ret = ath11k_scan_stop(ar);
3491 		if (ret)
3492 			ath11k_warn(ar->ab, "failed to abort scan: %d\n", ret);
3493 
3494 		spin_lock_bh(&ar->data_lock);
3495 		break;
3496 	}
3497 
3498 	spin_unlock_bh(&ar->data_lock);
3499 }
3500 
3501 static void ath11k_scan_timeout_work(struct work_struct *work)
3502 {
3503 	struct ath11k *ar = container_of(work, struct ath11k,
3504 					 scan.timeout.work);
3505 
3506 	mutex_lock(&ar->conf_mutex);
3507 	ath11k_scan_abort(ar);
3508 	mutex_unlock(&ar->conf_mutex);
3509 }
3510 
3511 static int ath11k_start_scan(struct ath11k *ar,
3512 			     struct scan_req_params *arg)
3513 {
3514 	int ret;
3515 	unsigned long timeout = 1 * HZ;
3516 
3517 	lockdep_assert_held(&ar->conf_mutex);
3518 
3519 	if (ath11k_spectral_get_mode(ar) == ATH11K_SPECTRAL_BACKGROUND)
3520 		ath11k_spectral_reset_buffer(ar);
3521 
3522 	ret = ath11k_wmi_send_scan_start_cmd(ar, arg);
3523 	if (ret)
3524 		return ret;
3525 
3526 	if (test_bit(WMI_TLV_SERVICE_11D_OFFLOAD, ar->ab->wmi_ab.svc_map)) {
3527 		timeout = 5 * HZ;
3528 
3529 		if (ar->supports_6ghz)
3530 			timeout += 5 * HZ;
3531 	}
3532 
3533 	ret = wait_for_completion_timeout(&ar->scan.started, timeout);
3534 	if (ret == 0) {
3535 		ret = ath11k_scan_stop(ar);
3536 		if (ret)
3537 			ath11k_warn(ar->ab, "failed to stop scan: %d\n", ret);
3538 
3539 		return -ETIMEDOUT;
3540 	}
3541 
3542 	/* If we failed to start the scan, return error code at
3543 	 * this point.  This is probably due to some issue in the
3544 	 * firmware, but no need to wedge the driver due to that...
3545 	 */
3546 	spin_lock_bh(&ar->data_lock);
3547 	if (ar->scan.state == ATH11K_SCAN_IDLE) {
3548 		spin_unlock_bh(&ar->data_lock);
3549 		return -EINVAL;
3550 	}
3551 	spin_unlock_bh(&ar->data_lock);
3552 
3553 	return 0;
3554 }
3555 
3556 static int ath11k_mac_op_hw_scan(struct ieee80211_hw *hw,
3557 				 struct ieee80211_vif *vif,
3558 				 struct ieee80211_scan_request *hw_req)
3559 {
3560 	struct ath11k *ar = hw->priv;
3561 	struct ath11k_vif *arvif = ath11k_vif_to_arvif(vif);
3562 	struct cfg80211_scan_request *req = &hw_req->req;
3563 	struct scan_req_params arg;
3564 	int ret = 0;
3565 	int i;
3566 
3567 	mutex_lock(&ar->conf_mutex);
3568 
3569 	spin_lock_bh(&ar->data_lock);
3570 	switch (ar->scan.state) {
3571 	case ATH11K_SCAN_IDLE:
3572 		reinit_completion(&ar->scan.started);
3573 		reinit_completion(&ar->scan.completed);
3574 		ar->scan.state = ATH11K_SCAN_STARTING;
3575 		ar->scan.is_roc = false;
3576 		ar->scan.vdev_id = arvif->vdev_id;
3577 		ret = 0;
3578 		break;
3579 	case ATH11K_SCAN_STARTING:
3580 	case ATH11K_SCAN_RUNNING:
3581 	case ATH11K_SCAN_ABORTING:
3582 		ret = -EBUSY;
3583 		break;
3584 	}
3585 	spin_unlock_bh(&ar->data_lock);
3586 
3587 	if (ret)
3588 		goto exit;
3589 
3590 	/* Currently the pending_11d=true only happened 1 time while
3591 	 * wlan interface up in ath11k_mac_11d_scan_start(), it is called by
3592 	 * ath11k_mac_op_add_interface(), after wlan interface up,
3593 	 * pending_11d=false always.
3594 	 * If remove below wait, it always happened scan fail and lead connect
3595 	 * fail while wlan interface up, because it has a 11d scan which is running
3596 	 * in firmware, and lead this scan failed.
3597 	 */
3598 	if (ar->pending_11d) {
3599 		long time_left;
3600 		unsigned long timeout = 5 * HZ;
3601 
3602 		if (ar->supports_6ghz)
3603 			timeout += 5 * HZ;
3604 
3605 		time_left = wait_for_completion_timeout(&ar->finish_11d_ch_list, timeout);
3606 		ath11k_dbg(ar->ab, ATH11K_DBG_MAC,
3607 			   "mac wait 11d channel list time left %ld\n", time_left);
3608 	}
3609 
3610 	memset(&arg, 0, sizeof(arg));
3611 	ath11k_wmi_start_scan_init(ar, &arg);
3612 	arg.vdev_id = arvif->vdev_id;
3613 	arg.scan_id = ATH11K_SCAN_ID;
3614 
3615 	if (req->ie_len) {
3616 		arg.extraie.ptr = kmemdup(req->ie, req->ie_len, GFP_KERNEL);
3617 		if (!arg.extraie.ptr) {
3618 			ret = -ENOMEM;
3619 			goto exit;
3620 		}
3621 		arg.extraie.len = req->ie_len;
3622 	}
3623 
3624 	if (req->n_ssids) {
3625 		arg.num_ssids = req->n_ssids;
3626 		for (i = 0; i < arg.num_ssids; i++) {
3627 			arg.ssid[i].length  = req->ssids[i].ssid_len;
3628 			memcpy(&arg.ssid[i].ssid, req->ssids[i].ssid,
3629 			       req->ssids[i].ssid_len);
3630 		}
3631 	} else {
3632 		arg.scan_flags |= WMI_SCAN_FLAG_PASSIVE;
3633 	}
3634 
3635 	if (req->n_channels) {
3636 		arg.num_chan = req->n_channels;
3637 		arg.chan_list = kcalloc(arg.num_chan, sizeof(*arg.chan_list),
3638 					GFP_KERNEL);
3639 
3640 		if (!arg.chan_list) {
3641 			ret = -ENOMEM;
3642 			goto exit;
3643 		}
3644 
3645 		for (i = 0; i < arg.num_chan; i++)
3646 			arg.chan_list[i] = req->channels[i]->center_freq;
3647 	}
3648 
3649 	if (req->flags & NL80211_SCAN_FLAG_RANDOM_ADDR) {
3650 		arg.scan_f_add_spoofed_mac_in_probe = 1;
3651 		ether_addr_copy(arg.mac_addr.addr, req->mac_addr);
3652 		ether_addr_copy(arg.mac_mask.addr, req->mac_addr_mask);
3653 	}
3654 
3655 	ret = ath11k_start_scan(ar, &arg);
3656 	if (ret) {
3657 		ath11k_warn(ar->ab, "failed to start hw scan: %d\n", ret);
3658 		spin_lock_bh(&ar->data_lock);
3659 		ar->scan.state = ATH11K_SCAN_IDLE;
3660 		spin_unlock_bh(&ar->data_lock);
3661 	}
3662 
3663 	/* Add a 200ms margin to account for event/command processing */
3664 	ieee80211_queue_delayed_work(ar->hw, &ar->scan.timeout,
3665 				     msecs_to_jiffies(arg.max_scan_time +
3666 						      ATH11K_MAC_SCAN_TIMEOUT_MSECS));
3667 
3668 exit:
3669 	kfree(arg.chan_list);
3670 
3671 	if (req->ie_len)
3672 		kfree(arg.extraie.ptr);
3673 
3674 	mutex_unlock(&ar->conf_mutex);
3675 	return ret;
3676 }
3677 
3678 static void ath11k_mac_op_cancel_hw_scan(struct ieee80211_hw *hw,
3679 					 struct ieee80211_vif *vif)
3680 {
3681 	struct ath11k *ar = hw->priv;
3682 
3683 	mutex_lock(&ar->conf_mutex);
3684 	ath11k_scan_abort(ar);
3685 	mutex_unlock(&ar->conf_mutex);
3686 
3687 	cancel_delayed_work_sync(&ar->scan.timeout);
3688 }
3689 
3690 static int ath11k_install_key(struct ath11k_vif *arvif,
3691 			      struct ieee80211_key_conf *key,
3692 			      enum set_key_cmd cmd,
3693 			      const u8 *macaddr, u32 flags)
3694 {
3695 	int ret;
3696 	struct ath11k *ar = arvif->ar;
3697 	struct wmi_vdev_install_key_arg arg = {
3698 		.vdev_id = arvif->vdev_id,
3699 		.key_idx = key->keyidx,
3700 		.key_len = key->keylen,
3701 		.key_data = key->key,
3702 		.key_flags = flags,
3703 		.macaddr = macaddr,
3704 	};
3705 
3706 	lockdep_assert_held(&arvif->ar->conf_mutex);
3707 
3708 	reinit_completion(&ar->install_key_done);
3709 
3710 	if (test_bit(ATH11K_FLAG_HW_CRYPTO_DISABLED, &ar->ab->dev_flags))
3711 		return 0;
3712 
3713 	if (cmd == DISABLE_KEY) {
3714 		arg.key_cipher = WMI_CIPHER_NONE;
3715 		arg.key_data = NULL;
3716 		goto install;
3717 	}
3718 
3719 	switch (key->cipher) {
3720 	case WLAN_CIPHER_SUITE_CCMP:
3721 		arg.key_cipher = WMI_CIPHER_AES_CCM;
3722 		/* TODO: Re-check if flag is valid */
3723 		key->flags |= IEEE80211_KEY_FLAG_GENERATE_IV_MGMT;
3724 		break;
3725 	case WLAN_CIPHER_SUITE_TKIP:
3726 		arg.key_cipher = WMI_CIPHER_TKIP;
3727 		arg.key_txmic_len = 8;
3728 		arg.key_rxmic_len = 8;
3729 		break;
3730 	case WLAN_CIPHER_SUITE_CCMP_256:
3731 		arg.key_cipher = WMI_CIPHER_AES_CCM;
3732 		break;
3733 	case WLAN_CIPHER_SUITE_GCMP:
3734 	case WLAN_CIPHER_SUITE_GCMP_256:
3735 		arg.key_cipher = WMI_CIPHER_AES_GCM;
3736 		break;
3737 	default:
3738 		ath11k_warn(ar->ab, "cipher %d is not supported\n", key->cipher);
3739 		return -EOPNOTSUPP;
3740 	}
3741 
3742 	if (test_bit(ATH11K_FLAG_RAW_MODE, &ar->ab->dev_flags))
3743 		key->flags |= IEEE80211_KEY_FLAG_GENERATE_IV |
3744 			      IEEE80211_KEY_FLAG_RESERVE_TAILROOM;
3745 
3746 install:
3747 	ret = ath11k_wmi_vdev_install_key(arvif->ar, &arg);
3748 
3749 	if (ret)
3750 		return ret;
3751 
3752 	if (!wait_for_completion_timeout(&ar->install_key_done, 1 * HZ))
3753 		return -ETIMEDOUT;
3754 
3755 	return ar->install_key_status ? -EINVAL : 0;
3756 }
3757 
3758 static int ath11k_clear_peer_keys(struct ath11k_vif *arvif,
3759 				  const u8 *addr)
3760 {
3761 	struct ath11k *ar = arvif->ar;
3762 	struct ath11k_base *ab = ar->ab;
3763 	struct ath11k_peer *peer;
3764 	int first_errno = 0;
3765 	int ret;
3766 	int i;
3767 	u32 flags = 0;
3768 
3769 	lockdep_assert_held(&ar->conf_mutex);
3770 
3771 	spin_lock_bh(&ab->base_lock);
3772 	peer = ath11k_peer_find(ab, arvif->vdev_id, addr);
3773 	spin_unlock_bh(&ab->base_lock);
3774 
3775 	if (!peer)
3776 		return -ENOENT;
3777 
3778 	for (i = 0; i < ARRAY_SIZE(peer->keys); i++) {
3779 		if (!peer->keys[i])
3780 			continue;
3781 
3782 		/* key flags are not required to delete the key */
3783 		ret = ath11k_install_key(arvif, peer->keys[i],
3784 					 DISABLE_KEY, addr, flags);
3785 		if (ret < 0 && first_errno == 0)
3786 			first_errno = ret;
3787 
3788 		if (ret < 0)
3789 			ath11k_warn(ab, "failed to remove peer key %d: %d\n",
3790 				    i, ret);
3791 
3792 		spin_lock_bh(&ab->base_lock);
3793 		peer->keys[i] = NULL;
3794 		spin_unlock_bh(&ab->base_lock);
3795 	}
3796 
3797 	return first_errno;
3798 }
3799 
3800 static int ath11k_mac_op_set_key(struct ieee80211_hw *hw, enum set_key_cmd cmd,
3801 				 struct ieee80211_vif *vif, struct ieee80211_sta *sta,
3802 				 struct ieee80211_key_conf *key)
3803 {
3804 	struct ath11k *ar = hw->priv;
3805 	struct ath11k_base *ab = ar->ab;
3806 	struct ath11k_vif *arvif = ath11k_vif_to_arvif(vif);
3807 	struct ath11k_peer *peer;
3808 	struct ath11k_sta *arsta;
3809 	const u8 *peer_addr;
3810 	int ret = 0;
3811 	u32 flags = 0;
3812 
3813 	/* BIP needs to be done in software */
3814 	if (key->cipher == WLAN_CIPHER_SUITE_AES_CMAC ||
3815 	    key->cipher == WLAN_CIPHER_SUITE_BIP_GMAC_128 ||
3816 	    key->cipher == WLAN_CIPHER_SUITE_BIP_GMAC_256 ||
3817 	    key->cipher == WLAN_CIPHER_SUITE_BIP_CMAC_256)
3818 		return 1;
3819 
3820 	if (test_bit(ATH11K_FLAG_HW_CRYPTO_DISABLED, &ar->ab->dev_flags))
3821 		return 1;
3822 
3823 	if (key->keyidx > WMI_MAX_KEY_INDEX)
3824 		return -ENOSPC;
3825 
3826 	mutex_lock(&ar->conf_mutex);
3827 
3828 	if (sta)
3829 		peer_addr = sta->addr;
3830 	else if (arvif->vdev_type == WMI_VDEV_TYPE_STA)
3831 		peer_addr = vif->bss_conf.bssid;
3832 	else
3833 		peer_addr = vif->addr;
3834 
3835 	key->hw_key_idx = key->keyidx;
3836 
3837 	/* the peer should not disappear in mid-way (unless FW goes awry) since
3838 	 * we already hold conf_mutex. we just make sure its there now.
3839 	 */
3840 	spin_lock_bh(&ab->base_lock);
3841 	peer = ath11k_peer_find(ab, arvif->vdev_id, peer_addr);
3842 
3843 	/* flush the fragments cache during key (re)install to
3844 	 * ensure all frags in the new frag list belong to the same key.
3845 	 */
3846 	if (peer && sta && cmd == SET_KEY)
3847 		ath11k_peer_frags_flush(ar, peer);
3848 	spin_unlock_bh(&ab->base_lock);
3849 
3850 	if (!peer) {
3851 		if (cmd == SET_KEY) {
3852 			ath11k_warn(ab, "cannot install key for non-existent peer %pM\n",
3853 				    peer_addr);
3854 			ret = -EOPNOTSUPP;
3855 			goto exit;
3856 		} else {
3857 			/* if the peer doesn't exist there is no key to disable
3858 			 * anymore
3859 			 */
3860 			goto exit;
3861 		}
3862 	}
3863 
3864 	if (key->flags & IEEE80211_KEY_FLAG_PAIRWISE)
3865 		flags |= WMI_KEY_PAIRWISE;
3866 	else
3867 		flags |= WMI_KEY_GROUP;
3868 
3869 	ret = ath11k_install_key(arvif, key, cmd, peer_addr, flags);
3870 	if (ret) {
3871 		ath11k_warn(ab, "ath11k_install_key failed (%d)\n", ret);
3872 		goto exit;
3873 	}
3874 
3875 	ret = ath11k_dp_peer_rx_pn_replay_config(arvif, peer_addr, cmd, key);
3876 	if (ret) {
3877 		ath11k_warn(ab, "failed to offload PN replay detection %d\n", ret);
3878 		goto exit;
3879 	}
3880 
3881 	spin_lock_bh(&ab->base_lock);
3882 	peer = ath11k_peer_find(ab, arvif->vdev_id, peer_addr);
3883 	if (peer && cmd == SET_KEY) {
3884 		peer->keys[key->keyidx] = key;
3885 		if (key->flags & IEEE80211_KEY_FLAG_PAIRWISE) {
3886 			peer->ucast_keyidx = key->keyidx;
3887 			peer->sec_type = ath11k_dp_tx_get_encrypt_type(key->cipher);
3888 		} else {
3889 			peer->mcast_keyidx = key->keyidx;
3890 			peer->sec_type_grp = ath11k_dp_tx_get_encrypt_type(key->cipher);
3891 		}
3892 	} else if (peer && cmd == DISABLE_KEY) {
3893 		peer->keys[key->keyidx] = NULL;
3894 		if (key->flags & IEEE80211_KEY_FLAG_PAIRWISE)
3895 			peer->ucast_keyidx = 0;
3896 		else
3897 			peer->mcast_keyidx = 0;
3898 	} else if (!peer)
3899 		/* impossible unless FW goes crazy */
3900 		ath11k_warn(ab, "peer %pM disappeared!\n", peer_addr);
3901 
3902 	if (sta) {
3903 		arsta = (struct ath11k_sta *)sta->drv_priv;
3904 
3905 		switch (key->cipher) {
3906 		case WLAN_CIPHER_SUITE_TKIP:
3907 		case WLAN_CIPHER_SUITE_CCMP:
3908 		case WLAN_CIPHER_SUITE_CCMP_256:
3909 		case WLAN_CIPHER_SUITE_GCMP:
3910 		case WLAN_CIPHER_SUITE_GCMP_256:
3911 			if (cmd == SET_KEY)
3912 				arsta->pn_type = HAL_PN_TYPE_WPA;
3913 			else
3914 				arsta->pn_type = HAL_PN_TYPE_NONE;
3915 			break;
3916 		default:
3917 			arsta->pn_type = HAL_PN_TYPE_NONE;
3918 			break;
3919 		}
3920 	}
3921 
3922 	spin_unlock_bh(&ab->base_lock);
3923 
3924 exit:
3925 	mutex_unlock(&ar->conf_mutex);
3926 	return ret;
3927 }
3928 
3929 static int
3930 ath11k_mac_bitrate_mask_num_vht_rates(struct ath11k *ar,
3931 				      enum nl80211_band band,
3932 				      const struct cfg80211_bitrate_mask *mask)
3933 {
3934 	int num_rates = 0;
3935 	int i;
3936 
3937 	for (i = 0; i < ARRAY_SIZE(mask->control[band].vht_mcs); i++)
3938 		num_rates += hweight16(mask->control[band].vht_mcs[i]);
3939 
3940 	return num_rates;
3941 }
3942 
3943 static int
3944 ath11k_mac_bitrate_mask_num_he_rates(struct ath11k *ar,
3945 				     enum nl80211_band band,
3946 				     const struct cfg80211_bitrate_mask *mask)
3947 {
3948 	int num_rates = 0;
3949 	int i;
3950 
3951 	for (i = 0; i < ARRAY_SIZE(mask->control[band].he_mcs); i++)
3952 		num_rates += hweight16(mask->control[band].he_mcs[i]);
3953 
3954 	return num_rates;
3955 }
3956 
3957 static int
3958 ath11k_mac_set_peer_vht_fixed_rate(struct ath11k_vif *arvif,
3959 				   struct ieee80211_sta *sta,
3960 				   const struct cfg80211_bitrate_mask *mask,
3961 				   enum nl80211_band band)
3962 {
3963 	struct ath11k *ar = arvif->ar;
3964 	u8 vht_rate, nss;
3965 	u32 rate_code;
3966 	int ret, i;
3967 
3968 	lockdep_assert_held(&ar->conf_mutex);
3969 
3970 	nss = 0;
3971 
3972 	for (i = 0; i < ARRAY_SIZE(mask->control[band].vht_mcs); i++) {
3973 		if (hweight16(mask->control[band].vht_mcs[i]) == 1) {
3974 			nss = i + 1;
3975 			vht_rate = ffs(mask->control[band].vht_mcs[i]) - 1;
3976 		}
3977 	}
3978 
3979 	if (!nss) {
3980 		ath11k_warn(ar->ab, "No single VHT Fixed rate found to set for %pM",
3981 			    sta->addr);
3982 		return -EINVAL;
3983 	}
3984 
3985 	/* Avoid updating invalid nss as fixed rate*/
3986 	if (nss > sta->rx_nss)
3987 		return -EINVAL;
3988 
3989 	ath11k_dbg(ar->ab, ATH11K_DBG_MAC,
3990 		   "Setting Fixed VHT Rate for peer %pM. Device will not switch to any other selected rates",
3991 		   sta->addr);
3992 
3993 	rate_code = ATH11K_HW_RATE_CODE(vht_rate, nss - 1,
3994 					WMI_RATE_PREAMBLE_VHT);
3995 	ret = ath11k_wmi_set_peer_param(ar, sta->addr,
3996 					arvif->vdev_id,
3997 					WMI_PEER_PARAM_FIXED_RATE,
3998 					rate_code);
3999 	if (ret)
4000 		ath11k_warn(ar->ab,
4001 			    "failed to update STA %pM Fixed Rate %d: %d\n",
4002 			     sta->addr, rate_code, ret);
4003 
4004 	return ret;
4005 }
4006 
4007 static int
4008 ath11k_mac_set_peer_he_fixed_rate(struct ath11k_vif *arvif,
4009 				  struct ieee80211_sta *sta,
4010 				  const struct cfg80211_bitrate_mask *mask,
4011 				  enum nl80211_band band)
4012 {
4013 	struct ath11k *ar = arvif->ar;
4014 	u8 he_rate, nss;
4015 	u32 rate_code;
4016 	int ret, i;
4017 
4018 	lockdep_assert_held(&ar->conf_mutex);
4019 
4020 	nss = 0;
4021 
4022 	for (i = 0; i < ARRAY_SIZE(mask->control[band].he_mcs); i++) {
4023 		if (hweight16(mask->control[band].he_mcs[i]) == 1) {
4024 			nss = i + 1;
4025 			he_rate = ffs(mask->control[band].he_mcs[i]) - 1;
4026 		}
4027 	}
4028 
4029 	if (!nss) {
4030 		ath11k_warn(ar->ab, "No single he fixed rate found to set for %pM",
4031 			    sta->addr);
4032 		return -EINVAL;
4033 	}
4034 
4035 	/* Avoid updating invalid nss as fixed rate */
4036 	if (nss > sta->rx_nss)
4037 		return -EINVAL;
4038 
4039 	ath11k_dbg(ar->ab, ATH11K_DBG_MAC,
4040 		   "mac setting fixed he rate for peer %pM, device will not switch to any other selected rates",
4041 		   sta->addr);
4042 
4043 	rate_code = ATH11K_HW_RATE_CODE(he_rate, nss - 1,
4044 					WMI_RATE_PREAMBLE_HE);
4045 
4046 	ret = ath11k_wmi_set_peer_param(ar, sta->addr,
4047 					arvif->vdev_id,
4048 					WMI_PEER_PARAM_FIXED_RATE,
4049 					rate_code);
4050 	if (ret)
4051 		ath11k_warn(ar->ab,
4052 			    "failed to update sta %pM fixed rate %d: %d\n",
4053 			    sta->addr, rate_code, ret);
4054 
4055 	return ret;
4056 }
4057 
4058 static int ath11k_station_assoc(struct ath11k *ar,
4059 				struct ieee80211_vif *vif,
4060 				struct ieee80211_sta *sta,
4061 				bool reassoc)
4062 {
4063 	struct ath11k_vif *arvif = ath11k_vif_to_arvif(vif);
4064 	struct peer_assoc_params peer_arg;
4065 	int ret = 0;
4066 	struct cfg80211_chan_def def;
4067 	enum nl80211_band band;
4068 	struct cfg80211_bitrate_mask *mask;
4069 	u8 num_vht_rates, num_he_rates;
4070 
4071 	lockdep_assert_held(&ar->conf_mutex);
4072 
4073 	if (WARN_ON(ath11k_mac_vif_chan(vif, &def)))
4074 		return -EPERM;
4075 
4076 	band = def.chan->band;
4077 	mask = &arvif->bitrate_mask;
4078 
4079 	ath11k_peer_assoc_prepare(ar, vif, sta, &peer_arg, reassoc);
4080 
4081 	peer_arg.is_assoc = true;
4082 	ret = ath11k_wmi_send_peer_assoc_cmd(ar, &peer_arg);
4083 	if (ret) {
4084 		ath11k_warn(ar->ab, "failed to run peer assoc for STA %pM vdev %i: %d\n",
4085 			    sta->addr, arvif->vdev_id, ret);
4086 		return ret;
4087 	}
4088 
4089 	if (!wait_for_completion_timeout(&ar->peer_assoc_done, 1 * HZ)) {
4090 		ath11k_warn(ar->ab, "failed to get peer assoc conf event for %pM vdev %i\n",
4091 			    sta->addr, arvif->vdev_id);
4092 		return -ETIMEDOUT;
4093 	}
4094 
4095 	num_vht_rates = ath11k_mac_bitrate_mask_num_vht_rates(ar, band, mask);
4096 	num_he_rates = ath11k_mac_bitrate_mask_num_he_rates(ar, band, mask);
4097 
4098 	/* If single VHT/HE rate is configured (by set_bitrate_mask()),
4099 	 * peer_assoc will disable VHT/HE. This is now enabled by a peer specific
4100 	 * fixed param.
4101 	 * Note that all other rates and NSS will be disabled for this peer.
4102 	 */
4103 	if (sta->vht_cap.vht_supported && num_vht_rates == 1) {
4104 		ret = ath11k_mac_set_peer_vht_fixed_rate(arvif, sta, mask,
4105 							 band);
4106 		if (ret)
4107 			return ret;
4108 	} else if (sta->he_cap.has_he && num_he_rates == 1) {
4109 		ret = ath11k_mac_set_peer_he_fixed_rate(arvif, sta, mask,
4110 							band);
4111 		if (ret)
4112 			return ret;
4113 	}
4114 
4115 	/* Re-assoc is run only to update supported rates for given station. It
4116 	 * doesn't make much sense to reconfigure the peer completely.
4117 	 */
4118 	if (reassoc)
4119 		return 0;
4120 
4121 	ret = ath11k_setup_peer_smps(ar, arvif, sta->addr,
4122 				     &sta->ht_cap, le16_to_cpu(sta->he_6ghz_capa.capa));
4123 	if (ret) {
4124 		ath11k_warn(ar->ab, "failed to setup peer SMPS for vdev %d: %d\n",
4125 			    arvif->vdev_id, ret);
4126 		return ret;
4127 	}
4128 
4129 	if (!sta->wme) {
4130 		arvif->num_legacy_stations++;
4131 		ret = ath11k_recalc_rtscts_prot(arvif);
4132 		if (ret)
4133 			return ret;
4134 	}
4135 
4136 	if (sta->wme && sta->uapsd_queues) {
4137 		ret = ath11k_peer_assoc_qos_ap(ar, arvif, sta);
4138 		if (ret) {
4139 			ath11k_warn(ar->ab, "failed to set qos params for STA %pM for vdev %i: %d\n",
4140 				    sta->addr, arvif->vdev_id, ret);
4141 			return ret;
4142 		}
4143 	}
4144 
4145 	return 0;
4146 }
4147 
4148 static int ath11k_station_disassoc(struct ath11k *ar,
4149 				   struct ieee80211_vif *vif,
4150 				   struct ieee80211_sta *sta)
4151 {
4152 	struct ath11k_vif *arvif = (void *)vif->drv_priv;
4153 	int ret = 0;
4154 
4155 	lockdep_assert_held(&ar->conf_mutex);
4156 
4157 	if (!sta->wme) {
4158 		arvif->num_legacy_stations--;
4159 		ret = ath11k_recalc_rtscts_prot(arvif);
4160 		if (ret)
4161 			return ret;
4162 	}
4163 
4164 	ret = ath11k_clear_peer_keys(arvif, sta->addr);
4165 	if (ret) {
4166 		ath11k_warn(ar->ab, "failed to clear all peer keys for vdev %i: %d\n",
4167 			    arvif->vdev_id, ret);
4168 		return ret;
4169 	}
4170 	return 0;
4171 }
4172 
4173 static void ath11k_sta_rc_update_wk(struct work_struct *wk)
4174 {
4175 	struct ath11k *ar;
4176 	struct ath11k_vif *arvif;
4177 	struct ath11k_sta *arsta;
4178 	struct ieee80211_sta *sta;
4179 	struct cfg80211_chan_def def;
4180 	enum nl80211_band band;
4181 	const u8 *ht_mcs_mask;
4182 	const u16 *vht_mcs_mask;
4183 	const u16 *he_mcs_mask;
4184 	u32 changed, bw, nss, smps;
4185 	int err, num_vht_rates, num_he_rates;
4186 	const struct cfg80211_bitrate_mask *mask;
4187 	struct peer_assoc_params peer_arg;
4188 
4189 	arsta = container_of(wk, struct ath11k_sta, update_wk);
4190 	sta = container_of((void *)arsta, struct ieee80211_sta, drv_priv);
4191 	arvif = arsta->arvif;
4192 	ar = arvif->ar;
4193 
4194 	if (WARN_ON(ath11k_mac_vif_chan(arvif->vif, &def)))
4195 		return;
4196 
4197 	band = def.chan->band;
4198 	ht_mcs_mask = arvif->bitrate_mask.control[band].ht_mcs;
4199 	vht_mcs_mask = arvif->bitrate_mask.control[band].vht_mcs;
4200 	he_mcs_mask = arvif->bitrate_mask.control[band].he_mcs;
4201 
4202 	spin_lock_bh(&ar->data_lock);
4203 
4204 	changed = arsta->changed;
4205 	arsta->changed = 0;
4206 
4207 	bw = arsta->bw;
4208 	nss = arsta->nss;
4209 	smps = arsta->smps;
4210 
4211 	spin_unlock_bh(&ar->data_lock);
4212 
4213 	mutex_lock(&ar->conf_mutex);
4214 
4215 	nss = max_t(u32, 1, nss);
4216 	nss = min(nss, max(max(ath11k_mac_max_ht_nss(ht_mcs_mask),
4217 			       ath11k_mac_max_vht_nss(vht_mcs_mask)),
4218 			   ath11k_mac_max_he_nss(he_mcs_mask)));
4219 
4220 	if (changed & IEEE80211_RC_BW_CHANGED) {
4221 		/* Send peer assoc command before set peer bandwidth param to
4222 		 * avoid the mismatch between the peer phymode and the peer
4223 		 * bandwidth.
4224 		 */
4225 		ath11k_peer_assoc_prepare(ar, arvif->vif, sta, &peer_arg, true);
4226 
4227 		peer_arg.is_assoc = false;
4228 		err = ath11k_wmi_send_peer_assoc_cmd(ar, &peer_arg);
4229 		if (err) {
4230 			ath11k_warn(ar->ab, "failed to send peer assoc for STA %pM vdev %i: %d\n",
4231 				    sta->addr, arvif->vdev_id, err);
4232 		} else if (wait_for_completion_timeout(&ar->peer_assoc_done, 1 * HZ)) {
4233 			err = ath11k_wmi_set_peer_param(ar, sta->addr, arvif->vdev_id,
4234 							WMI_PEER_CHWIDTH, bw);
4235 			if (err)
4236 				ath11k_warn(ar->ab, "failed to update STA %pM peer bw %d: %d\n",
4237 					    sta->addr, bw, err);
4238 		} else {
4239 			ath11k_warn(ar->ab, "failed to get peer assoc conf event for %pM vdev %i\n",
4240 				    sta->addr, arvif->vdev_id);
4241 		}
4242 	}
4243 
4244 	if (changed & IEEE80211_RC_NSS_CHANGED) {
4245 		ath11k_dbg(ar->ab, ATH11K_DBG_MAC, "mac update sta %pM nss %d\n",
4246 			   sta->addr, nss);
4247 
4248 		err = ath11k_wmi_set_peer_param(ar, sta->addr, arvif->vdev_id,
4249 						WMI_PEER_NSS, nss);
4250 		if (err)
4251 			ath11k_warn(ar->ab, "failed to update STA %pM nss %d: %d\n",
4252 				    sta->addr, nss, err);
4253 	}
4254 
4255 	if (changed & IEEE80211_RC_SMPS_CHANGED) {
4256 		ath11k_dbg(ar->ab, ATH11K_DBG_MAC, "mac update sta %pM smps %d\n",
4257 			   sta->addr, smps);
4258 
4259 		err = ath11k_wmi_set_peer_param(ar, sta->addr, arvif->vdev_id,
4260 						WMI_PEER_MIMO_PS_STATE, smps);
4261 		if (err)
4262 			ath11k_warn(ar->ab, "failed to update STA %pM smps %d: %d\n",
4263 				    sta->addr, smps, err);
4264 	}
4265 
4266 	if (changed & IEEE80211_RC_SUPP_RATES_CHANGED) {
4267 		mask = &arvif->bitrate_mask;
4268 		num_vht_rates = ath11k_mac_bitrate_mask_num_vht_rates(ar, band,
4269 								      mask);
4270 		num_he_rates = ath11k_mac_bitrate_mask_num_he_rates(ar, band,
4271 								    mask);
4272 
4273 		/* Peer_assoc_prepare will reject vht rates in
4274 		 * bitrate_mask if its not available in range format and
4275 		 * sets vht tx_rateset as unsupported. So multiple VHT MCS
4276 		 * setting(eg. MCS 4,5,6) per peer is not supported here.
4277 		 * But, Single rate in VHT mask can be set as per-peer
4278 		 * fixed rate. But even if any HT rates are configured in
4279 		 * the bitrate mask, device will not switch to those rates
4280 		 * when per-peer Fixed rate is set.
4281 		 * TODO: Check RATEMASK_CMDID to support auto rates selection
4282 		 * across HT/VHT and for multiple VHT MCS support.
4283 		 */
4284 		if (sta->vht_cap.vht_supported && num_vht_rates == 1) {
4285 			ath11k_mac_set_peer_vht_fixed_rate(arvif, sta, mask,
4286 							   band);
4287 		} else if (sta->he_cap.has_he && num_he_rates == 1) {
4288 			ath11k_mac_set_peer_he_fixed_rate(arvif, sta, mask,
4289 							  band);
4290 		} else {
4291 			/* If the peer is non-VHT/HE or no fixed VHT/HE rate
4292 			 * is provided in the new bitrate mask we set the
4293 			 * other rates using peer_assoc command. Also clear
4294 			 * the peer fixed rate settings as it has higher proprity
4295 			 * than peer assoc
4296 			 */
4297 			err = ath11k_wmi_set_peer_param(ar, sta->addr,
4298 							arvif->vdev_id,
4299 							WMI_PEER_PARAM_FIXED_RATE,
4300 							WMI_FIXED_RATE_NONE);
4301 			if (err)
4302 				ath11k_warn(ar->ab,
4303 					    "failed to disable peer fixed rate for sta %pM: %d\n",
4304 					    sta->addr, err);
4305 
4306 			ath11k_peer_assoc_prepare(ar, arvif->vif, sta,
4307 						  &peer_arg, true);
4308 
4309 			peer_arg.is_assoc = false;
4310 			err = ath11k_wmi_send_peer_assoc_cmd(ar, &peer_arg);
4311 			if (err)
4312 				ath11k_warn(ar->ab, "failed to run peer assoc for STA %pM vdev %i: %d\n",
4313 					    sta->addr, arvif->vdev_id, err);
4314 
4315 			if (!wait_for_completion_timeout(&ar->peer_assoc_done, 1 * HZ))
4316 				ath11k_warn(ar->ab, "failed to get peer assoc conf event for %pM vdev %i\n",
4317 					    sta->addr, arvif->vdev_id);
4318 		}
4319 	}
4320 
4321 	mutex_unlock(&ar->conf_mutex);
4322 }
4323 
4324 static void ath11k_sta_set_4addr_wk(struct work_struct *wk)
4325 {
4326 	struct ath11k *ar;
4327 	struct ath11k_vif *arvif;
4328 	struct ath11k_sta *arsta;
4329 	struct ieee80211_sta *sta;
4330 	int ret = 0;
4331 
4332 	arsta = container_of(wk, struct ath11k_sta, set_4addr_wk);
4333 	sta = container_of((void *)arsta, struct ieee80211_sta, drv_priv);
4334 	arvif = arsta->arvif;
4335 	ar = arvif->ar;
4336 
4337 	ath11k_dbg(ar->ab, ATH11K_DBG_MAC,
4338 		   "setting USE_4ADDR for peer %pM\n", sta->addr);
4339 
4340 	ret = ath11k_wmi_set_peer_param(ar, sta->addr,
4341 					arvif->vdev_id,
4342 					WMI_PEER_USE_4ADDR, 1);
4343 
4344 	if (ret)
4345 		ath11k_warn(ar->ab, "failed to set peer %pM 4addr capability: %d\n",
4346 			    sta->addr, ret);
4347 }
4348 
4349 static int ath11k_mac_inc_num_stations(struct ath11k_vif *arvif,
4350 				       struct ieee80211_sta *sta)
4351 {
4352 	struct ath11k *ar = arvif->ar;
4353 
4354 	lockdep_assert_held(&ar->conf_mutex);
4355 
4356 	if (arvif->vdev_type == WMI_VDEV_TYPE_STA && !sta->tdls)
4357 		return 0;
4358 
4359 	if (ar->num_stations >= ar->max_num_stations)
4360 		return -ENOBUFS;
4361 
4362 	ar->num_stations++;
4363 
4364 	return 0;
4365 }
4366 
4367 static void ath11k_mac_dec_num_stations(struct ath11k_vif *arvif,
4368 					struct ieee80211_sta *sta)
4369 {
4370 	struct ath11k *ar = arvif->ar;
4371 
4372 	lockdep_assert_held(&ar->conf_mutex);
4373 
4374 	if (arvif->vdev_type == WMI_VDEV_TYPE_STA && !sta->tdls)
4375 		return;
4376 
4377 	ar->num_stations--;
4378 }
4379 
4380 static int ath11k_mac_station_add(struct ath11k *ar,
4381 				  struct ieee80211_vif *vif,
4382 				  struct ieee80211_sta *sta)
4383 {
4384 	struct ath11k_base *ab = ar->ab;
4385 	struct ath11k_vif *arvif = ath11k_vif_to_arvif(vif);
4386 	struct ath11k_sta *arsta = (struct ath11k_sta *)sta->drv_priv;
4387 	struct peer_create_params peer_param;
4388 	int ret;
4389 
4390 	lockdep_assert_held(&ar->conf_mutex);
4391 
4392 	ret = ath11k_mac_inc_num_stations(arvif, sta);
4393 	if (ret) {
4394 		ath11k_warn(ab, "refusing to associate station: too many connected already (%d)\n",
4395 			    ar->max_num_stations);
4396 		goto exit;
4397 	}
4398 
4399 	arsta->rx_stats = kzalloc(sizeof(*arsta->rx_stats), GFP_KERNEL);
4400 	if (!arsta->rx_stats) {
4401 		ret = -ENOMEM;
4402 		goto dec_num_station;
4403 	}
4404 
4405 	peer_param.vdev_id = arvif->vdev_id;
4406 	peer_param.peer_addr = sta->addr;
4407 	peer_param.peer_type = WMI_PEER_TYPE_DEFAULT;
4408 
4409 	ret = ath11k_peer_create(ar, arvif, sta, &peer_param);
4410 	if (ret) {
4411 		ath11k_warn(ab, "Failed to add peer: %pM for VDEV: %d\n",
4412 			    sta->addr, arvif->vdev_id);
4413 		goto free_rx_stats;
4414 	}
4415 
4416 	ath11k_dbg(ab, ATH11K_DBG_MAC, "Added peer: %pM for VDEV: %d\n",
4417 		   sta->addr, arvif->vdev_id);
4418 
4419 	if (ath11k_debugfs_is_extd_tx_stats_enabled(ar)) {
4420 		arsta->tx_stats = kzalloc(sizeof(*arsta->tx_stats), GFP_KERNEL);
4421 		if (!arsta->tx_stats) {
4422 			ret = -ENOMEM;
4423 			goto free_peer;
4424 		}
4425 	}
4426 
4427 	if (ieee80211_vif_is_mesh(vif)) {
4428 		ath11k_dbg(ab, ATH11K_DBG_MAC,
4429 			   "setting USE_4ADDR for mesh STA %pM\n", sta->addr);
4430 		ret = ath11k_wmi_set_peer_param(ar, sta->addr,
4431 						arvif->vdev_id,
4432 						WMI_PEER_USE_4ADDR, 1);
4433 		if (ret) {
4434 			ath11k_warn(ab, "failed to set mesh STA %pM 4addr capability: %d\n",
4435 				    sta->addr, ret);
4436 			goto free_tx_stats;
4437 		}
4438 	}
4439 
4440 	ret = ath11k_dp_peer_setup(ar, arvif->vdev_id, sta->addr);
4441 	if (ret) {
4442 		ath11k_warn(ab, "failed to setup dp for peer %pM on vdev %i (%d)\n",
4443 			    sta->addr, arvif->vdev_id, ret);
4444 		goto free_tx_stats;
4445 	}
4446 
4447 	if (ab->hw_params.vdev_start_delay &&
4448 	    !arvif->is_started &&
4449 	    arvif->vdev_type != WMI_VDEV_TYPE_AP) {
4450 		ret = ath11k_start_vdev_delay(ar->hw, vif);
4451 		if (ret) {
4452 			ath11k_warn(ab, "failed to delay vdev start: %d\n", ret);
4453 			goto free_tx_stats;
4454 		}
4455 	}
4456 
4457 	return 0;
4458 
4459 free_tx_stats:
4460 	kfree(arsta->tx_stats);
4461 	arsta->tx_stats = NULL;
4462 free_peer:
4463 	ath11k_peer_delete(ar, arvif->vdev_id, sta->addr);
4464 free_rx_stats:
4465 	kfree(arsta->rx_stats);
4466 	arsta->rx_stats = NULL;
4467 dec_num_station:
4468 	ath11k_mac_dec_num_stations(arvif, sta);
4469 exit:
4470 	return ret;
4471 }
4472 
4473 static int ath11k_mac_op_sta_state(struct ieee80211_hw *hw,
4474 				   struct ieee80211_vif *vif,
4475 				   struct ieee80211_sta *sta,
4476 				   enum ieee80211_sta_state old_state,
4477 				   enum ieee80211_sta_state new_state)
4478 {
4479 	struct ath11k *ar = hw->priv;
4480 	struct ath11k_vif *arvif = ath11k_vif_to_arvif(vif);
4481 	struct ath11k_sta *arsta = (struct ath11k_sta *)sta->drv_priv;
4482 	struct ath11k_peer *peer;
4483 	int ret = 0;
4484 
4485 	/* cancel must be done outside the mutex to avoid deadlock */
4486 	if ((old_state == IEEE80211_STA_NONE &&
4487 	     new_state == IEEE80211_STA_NOTEXIST)) {
4488 		cancel_work_sync(&arsta->update_wk);
4489 		cancel_work_sync(&arsta->set_4addr_wk);
4490 	}
4491 
4492 	mutex_lock(&ar->conf_mutex);
4493 
4494 	if (old_state == IEEE80211_STA_NOTEXIST &&
4495 	    new_state == IEEE80211_STA_NONE) {
4496 		memset(arsta, 0, sizeof(*arsta));
4497 		arsta->arvif = arvif;
4498 		INIT_WORK(&arsta->update_wk, ath11k_sta_rc_update_wk);
4499 		INIT_WORK(&arsta->set_4addr_wk, ath11k_sta_set_4addr_wk);
4500 
4501 		ret = ath11k_mac_station_add(ar, vif, sta);
4502 		if (ret)
4503 			ath11k_warn(ar->ab, "Failed to add station: %pM for VDEV: %d\n",
4504 				    sta->addr, arvif->vdev_id);
4505 	} else if ((old_state == IEEE80211_STA_NONE &&
4506 		    new_state == IEEE80211_STA_NOTEXIST)) {
4507 		ath11k_dp_peer_cleanup(ar, arvif->vdev_id, sta->addr);
4508 
4509 		if (ar->ab->hw_params.vdev_start_delay &&
4510 		    vif->type == NL80211_IFTYPE_STATION)
4511 			goto free;
4512 
4513 		ret = ath11k_peer_delete(ar, arvif->vdev_id, sta->addr);
4514 		if (ret)
4515 			ath11k_warn(ar->ab, "Failed to delete peer: %pM for VDEV: %d\n",
4516 				    sta->addr, arvif->vdev_id);
4517 		else
4518 			ath11k_dbg(ar->ab, ATH11K_DBG_MAC, "Removed peer: %pM for VDEV: %d\n",
4519 				   sta->addr, arvif->vdev_id);
4520 
4521 		ath11k_mac_dec_num_stations(arvif, sta);
4522 		spin_lock_bh(&ar->ab->base_lock);
4523 		peer = ath11k_peer_find(ar->ab, arvif->vdev_id, sta->addr);
4524 		if (peer && peer->sta == sta) {
4525 			ath11k_warn(ar->ab, "Found peer entry %pM n vdev %i after it was supposedly removed\n",
4526 				    vif->addr, arvif->vdev_id);
4527 			peer->sta = NULL;
4528 			list_del(&peer->list);
4529 			kfree(peer);
4530 			ar->num_peers--;
4531 		}
4532 		spin_unlock_bh(&ar->ab->base_lock);
4533 
4534 free:
4535 		kfree(arsta->tx_stats);
4536 		arsta->tx_stats = NULL;
4537 
4538 		kfree(arsta->rx_stats);
4539 		arsta->rx_stats = NULL;
4540 	} else if (old_state == IEEE80211_STA_AUTH &&
4541 		   new_state == IEEE80211_STA_ASSOC &&
4542 		   (vif->type == NL80211_IFTYPE_AP ||
4543 		    vif->type == NL80211_IFTYPE_MESH_POINT ||
4544 		    vif->type == NL80211_IFTYPE_ADHOC)) {
4545 		ret = ath11k_station_assoc(ar, vif, sta, false);
4546 		if (ret)
4547 			ath11k_warn(ar->ab, "Failed to associate station: %pM\n",
4548 				    sta->addr);
4549 	} else if (old_state == IEEE80211_STA_ASSOC &&
4550 		   new_state == IEEE80211_STA_AUTHORIZED) {
4551 		spin_lock_bh(&ar->ab->base_lock);
4552 
4553 		peer = ath11k_peer_find(ar->ab, arvif->vdev_id, sta->addr);
4554 		if (peer)
4555 			peer->is_authorized = true;
4556 
4557 		spin_unlock_bh(&ar->ab->base_lock);
4558 
4559 		if (vif->type == NL80211_IFTYPE_STATION && arvif->is_up) {
4560 			ret = ath11k_wmi_set_peer_param(ar, sta->addr,
4561 							arvif->vdev_id,
4562 							WMI_PEER_AUTHORIZE,
4563 							1);
4564 			if (ret)
4565 				ath11k_warn(ar->ab, "Unable to authorize peer %pM vdev %d: %d\n",
4566 					    sta->addr, arvif->vdev_id, ret);
4567 		}
4568 	} else if (old_state == IEEE80211_STA_AUTHORIZED &&
4569 		   new_state == IEEE80211_STA_ASSOC) {
4570 		spin_lock_bh(&ar->ab->base_lock);
4571 
4572 		peer = ath11k_peer_find(ar->ab, arvif->vdev_id, sta->addr);
4573 		if (peer)
4574 			peer->is_authorized = false;
4575 
4576 		spin_unlock_bh(&ar->ab->base_lock);
4577 	} else if (old_state == IEEE80211_STA_ASSOC &&
4578 		   new_state == IEEE80211_STA_AUTH &&
4579 		   (vif->type == NL80211_IFTYPE_AP ||
4580 		    vif->type == NL80211_IFTYPE_MESH_POINT ||
4581 		    vif->type == NL80211_IFTYPE_ADHOC)) {
4582 		ret = ath11k_station_disassoc(ar, vif, sta);
4583 		if (ret)
4584 			ath11k_warn(ar->ab, "Failed to disassociate station: %pM\n",
4585 				    sta->addr);
4586 	}
4587 
4588 	mutex_unlock(&ar->conf_mutex);
4589 	return ret;
4590 }
4591 
4592 static int ath11k_mac_op_sta_set_txpwr(struct ieee80211_hw *hw,
4593 				       struct ieee80211_vif *vif,
4594 				       struct ieee80211_sta *sta)
4595 {
4596 	struct ath11k *ar = hw->priv;
4597 	struct ath11k_vif *arvif = (void *)vif->drv_priv;
4598 	int ret = 0;
4599 	s16 txpwr;
4600 
4601 	if (sta->txpwr.type == NL80211_TX_POWER_AUTOMATIC) {
4602 		txpwr = 0;
4603 	} else {
4604 		txpwr = sta->txpwr.power;
4605 		if (!txpwr)
4606 			return -EINVAL;
4607 	}
4608 
4609 	if (txpwr > ATH11K_TX_POWER_MAX_VAL || txpwr < ATH11K_TX_POWER_MIN_VAL)
4610 		return -EINVAL;
4611 
4612 	mutex_lock(&ar->conf_mutex);
4613 
4614 	ret = ath11k_wmi_set_peer_param(ar, sta->addr, arvif->vdev_id,
4615 					WMI_PEER_USE_FIXED_PWR, txpwr);
4616 	if (ret) {
4617 		ath11k_warn(ar->ab, "failed to set tx power for station ret: %d\n",
4618 			    ret);
4619 		goto out;
4620 	}
4621 
4622 out:
4623 	mutex_unlock(&ar->conf_mutex);
4624 	return ret;
4625 }
4626 
4627 static void ath11k_mac_op_sta_set_4addr(struct ieee80211_hw *hw,
4628 					struct ieee80211_vif *vif,
4629 					struct ieee80211_sta *sta, bool enabled)
4630 {
4631 	struct ath11k *ar = hw->priv;
4632 	struct ath11k_sta *arsta = (struct ath11k_sta *)sta->drv_priv;
4633 
4634 	if (enabled && !arsta->use_4addr_set) {
4635 		ieee80211_queue_work(ar->hw, &arsta->set_4addr_wk);
4636 		arsta->use_4addr_set = true;
4637 	}
4638 }
4639 
4640 static void ath11k_mac_op_sta_rc_update(struct ieee80211_hw *hw,
4641 					struct ieee80211_vif *vif,
4642 					struct ieee80211_sta *sta,
4643 					u32 changed)
4644 {
4645 	struct ath11k *ar = hw->priv;
4646 	struct ath11k_sta *arsta = (struct ath11k_sta *)sta->drv_priv;
4647 	struct ath11k_vif *arvif = (void *)vif->drv_priv;
4648 	struct ath11k_peer *peer;
4649 	u32 bw, smps;
4650 
4651 	spin_lock_bh(&ar->ab->base_lock);
4652 
4653 	peer = ath11k_peer_find(ar->ab, arvif->vdev_id, sta->addr);
4654 	if (!peer) {
4655 		spin_unlock_bh(&ar->ab->base_lock);
4656 		ath11k_warn(ar->ab, "mac sta rc update failed to find peer %pM on vdev %i\n",
4657 			    sta->addr, arvif->vdev_id);
4658 		return;
4659 	}
4660 
4661 	spin_unlock_bh(&ar->ab->base_lock);
4662 
4663 	ath11k_dbg(ar->ab, ATH11K_DBG_MAC,
4664 		   "mac sta rc update for %pM changed %08x bw %d nss %d smps %d\n",
4665 		   sta->addr, changed, sta->bandwidth, sta->rx_nss,
4666 		   sta->smps_mode);
4667 
4668 	spin_lock_bh(&ar->data_lock);
4669 
4670 	if (changed & IEEE80211_RC_BW_CHANGED) {
4671 		bw = WMI_PEER_CHWIDTH_20MHZ;
4672 
4673 		switch (sta->bandwidth) {
4674 		case IEEE80211_STA_RX_BW_20:
4675 			bw = WMI_PEER_CHWIDTH_20MHZ;
4676 			break;
4677 		case IEEE80211_STA_RX_BW_40:
4678 			bw = WMI_PEER_CHWIDTH_40MHZ;
4679 			break;
4680 		case IEEE80211_STA_RX_BW_80:
4681 			bw = WMI_PEER_CHWIDTH_80MHZ;
4682 			break;
4683 		case IEEE80211_STA_RX_BW_160:
4684 			bw = WMI_PEER_CHWIDTH_160MHZ;
4685 			break;
4686 		default:
4687 			ath11k_warn(ar->ab, "Invalid bandwidth %d in rc update for %pM\n",
4688 				    sta->bandwidth, sta->addr);
4689 			bw = WMI_PEER_CHWIDTH_20MHZ;
4690 			break;
4691 		}
4692 
4693 		arsta->bw = bw;
4694 	}
4695 
4696 	if (changed & IEEE80211_RC_NSS_CHANGED)
4697 		arsta->nss = sta->rx_nss;
4698 
4699 	if (changed & IEEE80211_RC_SMPS_CHANGED) {
4700 		smps = WMI_PEER_SMPS_PS_NONE;
4701 
4702 		switch (sta->smps_mode) {
4703 		case IEEE80211_SMPS_AUTOMATIC:
4704 		case IEEE80211_SMPS_OFF:
4705 			smps = WMI_PEER_SMPS_PS_NONE;
4706 			break;
4707 		case IEEE80211_SMPS_STATIC:
4708 			smps = WMI_PEER_SMPS_STATIC;
4709 			break;
4710 		case IEEE80211_SMPS_DYNAMIC:
4711 			smps = WMI_PEER_SMPS_DYNAMIC;
4712 			break;
4713 		default:
4714 			ath11k_warn(ar->ab, "Invalid smps %d in sta rc update for %pM\n",
4715 				    sta->smps_mode, sta->addr);
4716 			smps = WMI_PEER_SMPS_PS_NONE;
4717 			break;
4718 		}
4719 
4720 		arsta->smps = smps;
4721 	}
4722 
4723 	arsta->changed |= changed;
4724 
4725 	spin_unlock_bh(&ar->data_lock);
4726 
4727 	ieee80211_queue_work(hw, &arsta->update_wk);
4728 }
4729 
4730 static int ath11k_conf_tx_uapsd(struct ath11k *ar, struct ieee80211_vif *vif,
4731 				u16 ac, bool enable)
4732 {
4733 	struct ath11k_vif *arvif = ath11k_vif_to_arvif(vif);
4734 	u32 value = 0;
4735 	int ret = 0;
4736 
4737 	if (arvif->vdev_type != WMI_VDEV_TYPE_STA)
4738 		return 0;
4739 
4740 	switch (ac) {
4741 	case IEEE80211_AC_VO:
4742 		value = WMI_STA_PS_UAPSD_AC3_DELIVERY_EN |
4743 			WMI_STA_PS_UAPSD_AC3_TRIGGER_EN;
4744 		break;
4745 	case IEEE80211_AC_VI:
4746 		value = WMI_STA_PS_UAPSD_AC2_DELIVERY_EN |
4747 			WMI_STA_PS_UAPSD_AC2_TRIGGER_EN;
4748 		break;
4749 	case IEEE80211_AC_BE:
4750 		value = WMI_STA_PS_UAPSD_AC1_DELIVERY_EN |
4751 			WMI_STA_PS_UAPSD_AC1_TRIGGER_EN;
4752 		break;
4753 	case IEEE80211_AC_BK:
4754 		value = WMI_STA_PS_UAPSD_AC0_DELIVERY_EN |
4755 			WMI_STA_PS_UAPSD_AC0_TRIGGER_EN;
4756 		break;
4757 	}
4758 
4759 	if (enable)
4760 		arvif->u.sta.uapsd |= value;
4761 	else
4762 		arvif->u.sta.uapsd &= ~value;
4763 
4764 	ret = ath11k_wmi_set_sta_ps_param(ar, arvif->vdev_id,
4765 					  WMI_STA_PS_PARAM_UAPSD,
4766 					  arvif->u.sta.uapsd);
4767 	if (ret) {
4768 		ath11k_warn(ar->ab, "could not set uapsd params %d\n", ret);
4769 		goto exit;
4770 	}
4771 
4772 	if (arvif->u.sta.uapsd)
4773 		value = WMI_STA_PS_RX_WAKE_POLICY_POLL_UAPSD;
4774 	else
4775 		value = WMI_STA_PS_RX_WAKE_POLICY_WAKE;
4776 
4777 	ret = ath11k_wmi_set_sta_ps_param(ar, arvif->vdev_id,
4778 					  WMI_STA_PS_PARAM_RX_WAKE_POLICY,
4779 					  value);
4780 	if (ret)
4781 		ath11k_warn(ar->ab, "could not set rx wake param %d\n", ret);
4782 
4783 exit:
4784 	return ret;
4785 }
4786 
4787 static int ath11k_mac_op_conf_tx(struct ieee80211_hw *hw,
4788 				 struct ieee80211_vif *vif, u16 ac,
4789 				 const struct ieee80211_tx_queue_params *params)
4790 {
4791 	struct ath11k *ar = hw->priv;
4792 	struct ath11k_vif *arvif = (void *)vif->drv_priv;
4793 	struct wmi_wmm_params_arg *p = NULL;
4794 	int ret;
4795 
4796 	mutex_lock(&ar->conf_mutex);
4797 
4798 	switch (ac) {
4799 	case IEEE80211_AC_VO:
4800 		p = &arvif->wmm_params.ac_vo;
4801 		break;
4802 	case IEEE80211_AC_VI:
4803 		p = &arvif->wmm_params.ac_vi;
4804 		break;
4805 	case IEEE80211_AC_BE:
4806 		p = &arvif->wmm_params.ac_be;
4807 		break;
4808 	case IEEE80211_AC_BK:
4809 		p = &arvif->wmm_params.ac_bk;
4810 		break;
4811 	}
4812 
4813 	if (WARN_ON(!p)) {
4814 		ret = -EINVAL;
4815 		goto exit;
4816 	}
4817 
4818 	p->cwmin = params->cw_min;
4819 	p->cwmax = params->cw_max;
4820 	p->aifs = params->aifs;
4821 	p->txop = params->txop;
4822 
4823 	ret = ath11k_wmi_send_wmm_update_cmd_tlv(ar, arvif->vdev_id,
4824 						 &arvif->wmm_params);
4825 	if (ret) {
4826 		ath11k_warn(ar->ab, "failed to set wmm params: %d\n", ret);
4827 		goto exit;
4828 	}
4829 
4830 	ret = ath11k_conf_tx_uapsd(ar, vif, ac, params->uapsd);
4831 
4832 	if (ret)
4833 		ath11k_warn(ar->ab, "failed to set sta uapsd: %d\n", ret);
4834 
4835 exit:
4836 	mutex_unlock(&ar->conf_mutex);
4837 	return ret;
4838 }
4839 
4840 static struct ieee80211_sta_ht_cap
4841 ath11k_create_ht_cap(struct ath11k *ar, u32 ar_ht_cap, u32 rate_cap_rx_chainmask)
4842 {
4843 	int i;
4844 	struct ieee80211_sta_ht_cap ht_cap = {0};
4845 	u32 ar_vht_cap = ar->pdev->cap.vht_cap;
4846 
4847 	if (!(ar_ht_cap & WMI_HT_CAP_ENABLED))
4848 		return ht_cap;
4849 
4850 	ht_cap.ht_supported = 1;
4851 	ht_cap.ampdu_factor = IEEE80211_HT_MAX_AMPDU_64K;
4852 	ht_cap.ampdu_density = IEEE80211_HT_MPDU_DENSITY_NONE;
4853 	ht_cap.cap |= IEEE80211_HT_CAP_SUP_WIDTH_20_40;
4854 	ht_cap.cap |= IEEE80211_HT_CAP_DSSSCCK40;
4855 	ht_cap.cap |= WLAN_HT_CAP_SM_PS_STATIC << IEEE80211_HT_CAP_SM_PS_SHIFT;
4856 
4857 	if (ar_ht_cap & WMI_HT_CAP_HT20_SGI)
4858 		ht_cap.cap |= IEEE80211_HT_CAP_SGI_20;
4859 
4860 	if (ar_ht_cap & WMI_HT_CAP_HT40_SGI)
4861 		ht_cap.cap |= IEEE80211_HT_CAP_SGI_40;
4862 
4863 	if (ar_ht_cap & WMI_HT_CAP_DYNAMIC_SMPS) {
4864 		u32 smps;
4865 
4866 		smps   = WLAN_HT_CAP_SM_PS_DYNAMIC;
4867 		smps <<= IEEE80211_HT_CAP_SM_PS_SHIFT;
4868 
4869 		ht_cap.cap |= smps;
4870 	}
4871 
4872 	if (ar_ht_cap & WMI_HT_CAP_TX_STBC)
4873 		ht_cap.cap |= IEEE80211_HT_CAP_TX_STBC;
4874 
4875 	if (ar_ht_cap & WMI_HT_CAP_RX_STBC) {
4876 		u32 stbc;
4877 
4878 		stbc   = ar_ht_cap;
4879 		stbc  &= WMI_HT_CAP_RX_STBC;
4880 		stbc >>= WMI_HT_CAP_RX_STBC_MASK_SHIFT;
4881 		stbc <<= IEEE80211_HT_CAP_RX_STBC_SHIFT;
4882 		stbc  &= IEEE80211_HT_CAP_RX_STBC;
4883 
4884 		ht_cap.cap |= stbc;
4885 	}
4886 
4887 	if (ar_ht_cap & WMI_HT_CAP_RX_LDPC)
4888 		ht_cap.cap |= IEEE80211_HT_CAP_LDPC_CODING;
4889 
4890 	if (ar_ht_cap & WMI_HT_CAP_L_SIG_TXOP_PROT)
4891 		ht_cap.cap |= IEEE80211_HT_CAP_LSIG_TXOP_PROT;
4892 
4893 	if (ar_vht_cap & WMI_VHT_CAP_MAX_MPDU_LEN_MASK)
4894 		ht_cap.cap |= IEEE80211_HT_CAP_MAX_AMSDU;
4895 
4896 	for (i = 0; i < ar->num_rx_chains; i++) {
4897 		if (rate_cap_rx_chainmask & BIT(i))
4898 			ht_cap.mcs.rx_mask[i] = 0xFF;
4899 	}
4900 
4901 	ht_cap.mcs.tx_params |= IEEE80211_HT_MCS_TX_DEFINED;
4902 
4903 	return ht_cap;
4904 }
4905 
4906 static int ath11k_mac_set_txbf_conf(struct ath11k_vif *arvif)
4907 {
4908 	u32 value = 0;
4909 	struct ath11k *ar = arvif->ar;
4910 	int nsts;
4911 	int sound_dim;
4912 	u32 vht_cap = ar->pdev->cap.vht_cap;
4913 	u32 vdev_param = WMI_VDEV_PARAM_TXBF;
4914 
4915 	if (vht_cap & (IEEE80211_VHT_CAP_SU_BEAMFORMEE_CAPABLE)) {
4916 		nsts = vht_cap & IEEE80211_VHT_CAP_BEAMFORMEE_STS_MASK;
4917 		nsts >>= IEEE80211_VHT_CAP_BEAMFORMEE_STS_SHIFT;
4918 		value |= SM(nsts, WMI_TXBF_STS_CAP_OFFSET);
4919 	}
4920 
4921 	if (vht_cap & (IEEE80211_VHT_CAP_SU_BEAMFORMER_CAPABLE)) {
4922 		sound_dim = vht_cap &
4923 			    IEEE80211_VHT_CAP_SOUNDING_DIMENSIONS_MASK;
4924 		sound_dim >>= IEEE80211_VHT_CAP_SOUNDING_DIMENSIONS_SHIFT;
4925 		if (sound_dim > (ar->num_tx_chains - 1))
4926 			sound_dim = ar->num_tx_chains - 1;
4927 		value |= SM(sound_dim, WMI_BF_SOUND_DIM_OFFSET);
4928 	}
4929 
4930 	if (!value)
4931 		return 0;
4932 
4933 	if (vht_cap & IEEE80211_VHT_CAP_SU_BEAMFORMER_CAPABLE) {
4934 		value |= WMI_VDEV_PARAM_TXBF_SU_TX_BFER;
4935 
4936 		if ((vht_cap & IEEE80211_VHT_CAP_MU_BEAMFORMER_CAPABLE) &&
4937 		    arvif->vdev_type == WMI_VDEV_TYPE_AP)
4938 			value |= WMI_VDEV_PARAM_TXBF_MU_TX_BFER;
4939 	}
4940 
4941 	/* TODO: SUBFEE not validated in HK, disable here until validated? */
4942 
4943 	if (vht_cap & IEEE80211_VHT_CAP_SU_BEAMFORMEE_CAPABLE) {
4944 		value |= WMI_VDEV_PARAM_TXBF_SU_TX_BFEE;
4945 
4946 		if ((vht_cap & IEEE80211_VHT_CAP_MU_BEAMFORMEE_CAPABLE) &&
4947 		    arvif->vdev_type == WMI_VDEV_TYPE_STA)
4948 			value |= WMI_VDEV_PARAM_TXBF_MU_TX_BFEE;
4949 	}
4950 
4951 	return ath11k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
4952 					     vdev_param, value);
4953 }
4954 
4955 static void ath11k_set_vht_txbf_cap(struct ath11k *ar, u32 *vht_cap)
4956 {
4957 	bool subfer, subfee;
4958 	int sound_dim = 0;
4959 
4960 	subfer = !!(*vht_cap & (IEEE80211_VHT_CAP_SU_BEAMFORMER_CAPABLE));
4961 	subfee = !!(*vht_cap & (IEEE80211_VHT_CAP_SU_BEAMFORMEE_CAPABLE));
4962 
4963 	if (ar->num_tx_chains < 2) {
4964 		*vht_cap &= ~(IEEE80211_VHT_CAP_SU_BEAMFORMER_CAPABLE);
4965 		subfer = false;
4966 	}
4967 
4968 	/* If SU Beaformer is not set, then disable MU Beamformer Capability */
4969 	if (!subfer)
4970 		*vht_cap &= ~(IEEE80211_VHT_CAP_MU_BEAMFORMER_CAPABLE);
4971 
4972 	/* If SU Beaformee is not set, then disable MU Beamformee Capability */
4973 	if (!subfee)
4974 		*vht_cap &= ~(IEEE80211_VHT_CAP_MU_BEAMFORMEE_CAPABLE);
4975 
4976 	sound_dim = (*vht_cap & IEEE80211_VHT_CAP_SOUNDING_DIMENSIONS_MASK);
4977 	sound_dim >>= IEEE80211_VHT_CAP_SOUNDING_DIMENSIONS_SHIFT;
4978 	*vht_cap &= ~IEEE80211_VHT_CAP_SOUNDING_DIMENSIONS_MASK;
4979 
4980 	/* TODO: Need to check invalid STS and Sound_dim values set by FW? */
4981 
4982 	/* Enable Sounding Dimension Field only if SU BF is enabled */
4983 	if (subfer) {
4984 		if (sound_dim > (ar->num_tx_chains - 1))
4985 			sound_dim = ar->num_tx_chains - 1;
4986 
4987 		sound_dim <<= IEEE80211_VHT_CAP_SOUNDING_DIMENSIONS_SHIFT;
4988 		sound_dim &=  IEEE80211_VHT_CAP_SOUNDING_DIMENSIONS_MASK;
4989 		*vht_cap |= sound_dim;
4990 	}
4991 
4992 	/* Use the STS advertised by FW unless SU Beamformee is not supported*/
4993 	if (!subfee)
4994 		*vht_cap &= ~(IEEE80211_VHT_CAP_BEAMFORMEE_STS_MASK);
4995 }
4996 
4997 static struct ieee80211_sta_vht_cap
4998 ath11k_create_vht_cap(struct ath11k *ar, u32 rate_cap_tx_chainmask,
4999 		      u32 rate_cap_rx_chainmask)
5000 {
5001 	struct ieee80211_sta_vht_cap vht_cap = {0};
5002 	u16 txmcs_map, rxmcs_map;
5003 	int i;
5004 
5005 	vht_cap.vht_supported = 1;
5006 	vht_cap.cap = ar->pdev->cap.vht_cap;
5007 
5008 	if (ar->pdev->cap.nss_ratio_enabled)
5009 		vht_cap.vht_mcs.tx_highest |=
5010 			cpu_to_le16(IEEE80211_VHT_EXT_NSS_BW_CAPABLE);
5011 
5012 	ath11k_set_vht_txbf_cap(ar, &vht_cap.cap);
5013 
5014 	rxmcs_map = 0;
5015 	txmcs_map = 0;
5016 	for (i = 0; i < 8; i++) {
5017 		if (i < ar->num_tx_chains && rate_cap_tx_chainmask & BIT(i))
5018 			txmcs_map |= IEEE80211_VHT_MCS_SUPPORT_0_9 << (i * 2);
5019 		else
5020 			txmcs_map |= IEEE80211_VHT_MCS_NOT_SUPPORTED << (i * 2);
5021 
5022 		if (i < ar->num_rx_chains && rate_cap_rx_chainmask & BIT(i))
5023 			rxmcs_map |= IEEE80211_VHT_MCS_SUPPORT_0_9 << (i * 2);
5024 		else
5025 			rxmcs_map |= IEEE80211_VHT_MCS_NOT_SUPPORTED << (i * 2);
5026 	}
5027 
5028 	if (rate_cap_tx_chainmask <= 1)
5029 		vht_cap.cap &= ~IEEE80211_VHT_CAP_TXSTBC;
5030 
5031 	vht_cap.vht_mcs.rx_mcs_map = cpu_to_le16(rxmcs_map);
5032 	vht_cap.vht_mcs.tx_mcs_map = cpu_to_le16(txmcs_map);
5033 
5034 	return vht_cap;
5035 }
5036 
5037 static void ath11k_mac_setup_ht_vht_cap(struct ath11k *ar,
5038 					struct ath11k_pdev_cap *cap,
5039 					u32 *ht_cap_info)
5040 {
5041 	struct ieee80211_supported_band *band;
5042 	u32 rate_cap_tx_chainmask;
5043 	u32 rate_cap_rx_chainmask;
5044 	u32 ht_cap;
5045 
5046 	rate_cap_tx_chainmask = ar->cfg_tx_chainmask >> cap->tx_chain_mask_shift;
5047 	rate_cap_rx_chainmask = ar->cfg_rx_chainmask >> cap->rx_chain_mask_shift;
5048 
5049 	if (cap->supported_bands & WMI_HOST_WLAN_2G_CAP) {
5050 		band = &ar->mac.sbands[NL80211_BAND_2GHZ];
5051 		ht_cap = cap->band[NL80211_BAND_2GHZ].ht_cap_info;
5052 		if (ht_cap_info)
5053 			*ht_cap_info = ht_cap;
5054 		band->ht_cap = ath11k_create_ht_cap(ar, ht_cap,
5055 						    rate_cap_rx_chainmask);
5056 	}
5057 
5058 	if (cap->supported_bands & WMI_HOST_WLAN_5G_CAP &&
5059 	    (ar->ab->hw_params.single_pdev_only ||
5060 	     !ar->supports_6ghz)) {
5061 		band = &ar->mac.sbands[NL80211_BAND_5GHZ];
5062 		ht_cap = cap->band[NL80211_BAND_5GHZ].ht_cap_info;
5063 		if (ht_cap_info)
5064 			*ht_cap_info = ht_cap;
5065 		band->ht_cap = ath11k_create_ht_cap(ar, ht_cap,
5066 						    rate_cap_rx_chainmask);
5067 		band->vht_cap = ath11k_create_vht_cap(ar, rate_cap_tx_chainmask,
5068 						      rate_cap_rx_chainmask);
5069 	}
5070 }
5071 
5072 static int ath11k_check_chain_mask(struct ath11k *ar, u32 ant, bool is_tx_ant)
5073 {
5074 	/* TODO: Check the request chainmask against the supported
5075 	 * chainmask table which is advertised in extented_service_ready event
5076 	 */
5077 
5078 	return 0;
5079 }
5080 
5081 static void ath11k_gen_ppe_thresh(struct ath11k_ppe_threshold *fw_ppet,
5082 				  u8 *he_ppet)
5083 {
5084 	int nss, ru;
5085 	u8 bit = 7;
5086 
5087 	he_ppet[0] = fw_ppet->numss_m1 & IEEE80211_PPE_THRES_NSS_MASK;
5088 	he_ppet[0] |= (fw_ppet->ru_bit_mask <<
5089 		       IEEE80211_PPE_THRES_RU_INDEX_BITMASK_POS) &
5090 		      IEEE80211_PPE_THRES_RU_INDEX_BITMASK_MASK;
5091 	for (nss = 0; nss <= fw_ppet->numss_m1; nss++) {
5092 		for (ru = 0; ru < 4; ru++) {
5093 			u8 val;
5094 			int i;
5095 
5096 			if ((fw_ppet->ru_bit_mask & BIT(ru)) == 0)
5097 				continue;
5098 			val = (fw_ppet->ppet16_ppet8_ru3_ru0[nss] >> (ru * 6)) &
5099 			       0x3f;
5100 			val = ((val >> 3) & 0x7) | ((val & 0x7) << 3);
5101 			for (i = 5; i >= 0; i--) {
5102 				he_ppet[bit / 8] |=
5103 					((val >> i) & 0x1) << ((bit % 8));
5104 				bit++;
5105 			}
5106 		}
5107 	}
5108 }
5109 
5110 static void
5111 ath11k_mac_filter_he_cap_mesh(struct ieee80211_he_cap_elem *he_cap_elem)
5112 {
5113 	u8 m;
5114 
5115 	m = IEEE80211_HE_MAC_CAP0_TWT_RES |
5116 	    IEEE80211_HE_MAC_CAP0_TWT_REQ;
5117 	he_cap_elem->mac_cap_info[0] &= ~m;
5118 
5119 	m = IEEE80211_HE_MAC_CAP2_TRS |
5120 	    IEEE80211_HE_MAC_CAP2_BCAST_TWT |
5121 	    IEEE80211_HE_MAC_CAP2_MU_CASCADING;
5122 	he_cap_elem->mac_cap_info[2] &= ~m;
5123 
5124 	m = IEEE80211_HE_MAC_CAP3_FLEX_TWT_SCHED |
5125 	    IEEE80211_HE_MAC_CAP2_BCAST_TWT |
5126 	    IEEE80211_HE_MAC_CAP2_MU_CASCADING;
5127 	he_cap_elem->mac_cap_info[3] &= ~m;
5128 
5129 	m = IEEE80211_HE_MAC_CAP4_BSRP_BQRP_A_MPDU_AGG |
5130 	    IEEE80211_HE_MAC_CAP4_BQR;
5131 	he_cap_elem->mac_cap_info[4] &= ~m;
5132 
5133 	m = IEEE80211_HE_MAC_CAP5_SUBCHAN_SELECTIVE_TRANSMISSION |
5134 	    IEEE80211_HE_MAC_CAP5_UL_2x996_TONE_RU |
5135 	    IEEE80211_HE_MAC_CAP5_PUNCTURED_SOUNDING |
5136 	    IEEE80211_HE_MAC_CAP5_HT_VHT_TRIG_FRAME_RX;
5137 	he_cap_elem->mac_cap_info[5] &= ~m;
5138 
5139 	m = IEEE80211_HE_PHY_CAP2_UL_MU_FULL_MU_MIMO |
5140 	    IEEE80211_HE_PHY_CAP2_UL_MU_PARTIAL_MU_MIMO;
5141 	he_cap_elem->phy_cap_info[2] &= ~m;
5142 
5143 	m = IEEE80211_HE_PHY_CAP3_RX_PARTIAL_BW_SU_IN_20MHZ_MU |
5144 	    IEEE80211_HE_PHY_CAP3_DCM_MAX_CONST_TX_MASK |
5145 	    IEEE80211_HE_PHY_CAP3_DCM_MAX_CONST_RX_MASK;
5146 	he_cap_elem->phy_cap_info[3] &= ~m;
5147 
5148 	m = IEEE80211_HE_PHY_CAP4_MU_BEAMFORMER;
5149 	he_cap_elem->phy_cap_info[4] &= ~m;
5150 
5151 	m = IEEE80211_HE_PHY_CAP5_NG16_MU_FEEDBACK;
5152 	he_cap_elem->phy_cap_info[5] &= ~m;
5153 
5154 	m = IEEE80211_HE_PHY_CAP6_CODEBOOK_SIZE_75_MU |
5155 	    IEEE80211_HE_PHY_CAP6_TRIG_MU_BEAMFORMING_PARTIAL_BW_FB |
5156 	    IEEE80211_HE_PHY_CAP6_TRIG_CQI_FB |
5157 	    IEEE80211_HE_PHY_CAP6_PARTIAL_BANDWIDTH_DL_MUMIMO;
5158 	he_cap_elem->phy_cap_info[6] &= ~m;
5159 
5160 	m = IEEE80211_HE_PHY_CAP7_PSR_BASED_SR |
5161 	    IEEE80211_HE_PHY_CAP7_POWER_BOOST_FACTOR_SUPP |
5162 	    IEEE80211_HE_PHY_CAP7_STBC_TX_ABOVE_80MHZ |
5163 	    IEEE80211_HE_PHY_CAP7_STBC_RX_ABOVE_80MHZ;
5164 	he_cap_elem->phy_cap_info[7] &= ~m;
5165 
5166 	m = IEEE80211_HE_PHY_CAP8_HE_ER_SU_PPDU_4XLTF_AND_08_US_GI |
5167 	    IEEE80211_HE_PHY_CAP8_20MHZ_IN_40MHZ_HE_PPDU_IN_2G |
5168 	    IEEE80211_HE_PHY_CAP8_20MHZ_IN_160MHZ_HE_PPDU |
5169 	    IEEE80211_HE_PHY_CAP8_80MHZ_IN_160MHZ_HE_PPDU;
5170 	he_cap_elem->phy_cap_info[8] &= ~m;
5171 
5172 	m = IEEE80211_HE_PHY_CAP9_LONGER_THAN_16_SIGB_OFDM_SYM |
5173 	    IEEE80211_HE_PHY_CAP9_NON_TRIGGERED_CQI_FEEDBACK |
5174 	    IEEE80211_HE_PHY_CAP9_RX_1024_QAM_LESS_THAN_242_TONE_RU |
5175 	    IEEE80211_HE_PHY_CAP9_TX_1024_QAM_LESS_THAN_242_TONE_RU |
5176 	    IEEE80211_HE_PHY_CAP9_RX_FULL_BW_SU_USING_MU_WITH_COMP_SIGB |
5177 	    IEEE80211_HE_PHY_CAP9_RX_FULL_BW_SU_USING_MU_WITH_NON_COMP_SIGB;
5178 	he_cap_elem->phy_cap_info[9] &= ~m;
5179 }
5180 
5181 static __le16 ath11k_mac_setup_he_6ghz_cap(struct ath11k_pdev_cap *pcap,
5182 					   struct ath11k_band_cap *bcap)
5183 {
5184 	u8 val;
5185 
5186 	bcap->he_6ghz_capa = IEEE80211_HT_MPDU_DENSITY_NONE;
5187 	if (bcap->ht_cap_info & WMI_HT_CAP_DYNAMIC_SMPS)
5188 		bcap->he_6ghz_capa |=
5189 			FIELD_PREP(IEEE80211_HE_6GHZ_CAP_SM_PS,
5190 				   WLAN_HT_CAP_SM_PS_DYNAMIC);
5191 	else
5192 		bcap->he_6ghz_capa |=
5193 			FIELD_PREP(IEEE80211_HE_6GHZ_CAP_SM_PS,
5194 				   WLAN_HT_CAP_SM_PS_DISABLED);
5195 	val = FIELD_GET(IEEE80211_VHT_CAP_MAX_A_MPDU_LENGTH_EXPONENT_MASK,
5196 			pcap->vht_cap);
5197 	bcap->he_6ghz_capa |=
5198 		FIELD_PREP(IEEE80211_HE_6GHZ_CAP_MAX_AMPDU_LEN_EXP, val);
5199 	val = FIELD_GET(IEEE80211_VHT_CAP_MAX_MPDU_MASK, pcap->vht_cap);
5200 	bcap->he_6ghz_capa |=
5201 		FIELD_PREP(IEEE80211_HE_6GHZ_CAP_MAX_MPDU_LEN, val);
5202 	if (pcap->vht_cap & IEEE80211_VHT_CAP_RX_ANTENNA_PATTERN)
5203 		bcap->he_6ghz_capa |= IEEE80211_HE_6GHZ_CAP_RX_ANTPAT_CONS;
5204 	if (pcap->vht_cap & IEEE80211_VHT_CAP_TX_ANTENNA_PATTERN)
5205 		bcap->he_6ghz_capa |= IEEE80211_HE_6GHZ_CAP_TX_ANTPAT_CONS;
5206 
5207 	return cpu_to_le16(bcap->he_6ghz_capa);
5208 }
5209 
5210 static int ath11k_mac_copy_he_cap(struct ath11k *ar,
5211 				  struct ath11k_pdev_cap *cap,
5212 				  struct ieee80211_sband_iftype_data *data,
5213 				  int band)
5214 {
5215 	int i, idx = 0;
5216 
5217 	for (i = 0; i < NUM_NL80211_IFTYPES; i++) {
5218 		struct ieee80211_sta_he_cap *he_cap = &data[idx].he_cap;
5219 		struct ath11k_band_cap *band_cap = &cap->band[band];
5220 		struct ieee80211_he_cap_elem *he_cap_elem =
5221 				&he_cap->he_cap_elem;
5222 
5223 		switch (i) {
5224 		case NL80211_IFTYPE_STATION:
5225 		case NL80211_IFTYPE_AP:
5226 		case NL80211_IFTYPE_MESH_POINT:
5227 			break;
5228 
5229 		default:
5230 			continue;
5231 		}
5232 
5233 		data[idx].types_mask = BIT(i);
5234 		he_cap->has_he = true;
5235 		memcpy(he_cap_elem->mac_cap_info, band_cap->he_cap_info,
5236 		       sizeof(he_cap_elem->mac_cap_info));
5237 		memcpy(he_cap_elem->phy_cap_info, band_cap->he_cap_phy_info,
5238 		       sizeof(he_cap_elem->phy_cap_info));
5239 
5240 		he_cap_elem->mac_cap_info[1] &=
5241 			IEEE80211_HE_MAC_CAP1_TF_MAC_PAD_DUR_MASK;
5242 
5243 		he_cap_elem->phy_cap_info[5] &=
5244 			~IEEE80211_HE_PHY_CAP5_BEAMFORMEE_NUM_SND_DIM_UNDER_80MHZ_MASK;
5245 		he_cap_elem->phy_cap_info[5] |= ar->num_tx_chains - 1;
5246 
5247 		switch (i) {
5248 		case NL80211_IFTYPE_AP:
5249 			he_cap_elem->phy_cap_info[3] &=
5250 				~IEEE80211_HE_PHY_CAP3_DCM_MAX_CONST_TX_MASK;
5251 			he_cap_elem->phy_cap_info[9] |=
5252 				IEEE80211_HE_PHY_CAP9_RX_1024_QAM_LESS_THAN_242_TONE_RU;
5253 			break;
5254 		case NL80211_IFTYPE_STATION:
5255 			he_cap_elem->mac_cap_info[0] &=
5256 				~IEEE80211_HE_MAC_CAP0_TWT_RES;
5257 			he_cap_elem->mac_cap_info[0] |=
5258 				IEEE80211_HE_MAC_CAP0_TWT_REQ;
5259 			he_cap_elem->phy_cap_info[9] |=
5260 				IEEE80211_HE_PHY_CAP9_TX_1024_QAM_LESS_THAN_242_TONE_RU;
5261 			break;
5262 		case NL80211_IFTYPE_MESH_POINT:
5263 			ath11k_mac_filter_he_cap_mesh(he_cap_elem);
5264 			break;
5265 		}
5266 
5267 		he_cap->he_mcs_nss_supp.rx_mcs_80 =
5268 			cpu_to_le16(band_cap->he_mcs & 0xffff);
5269 		he_cap->he_mcs_nss_supp.tx_mcs_80 =
5270 			cpu_to_le16(band_cap->he_mcs & 0xffff);
5271 		he_cap->he_mcs_nss_supp.rx_mcs_160 =
5272 			cpu_to_le16((band_cap->he_mcs >> 16) & 0xffff);
5273 		he_cap->he_mcs_nss_supp.tx_mcs_160 =
5274 			cpu_to_le16((band_cap->he_mcs >> 16) & 0xffff);
5275 		he_cap->he_mcs_nss_supp.rx_mcs_80p80 =
5276 			cpu_to_le16((band_cap->he_mcs >> 16) & 0xffff);
5277 		he_cap->he_mcs_nss_supp.tx_mcs_80p80 =
5278 			cpu_to_le16((band_cap->he_mcs >> 16) & 0xffff);
5279 
5280 		memset(he_cap->ppe_thres, 0, sizeof(he_cap->ppe_thres));
5281 		if (he_cap_elem->phy_cap_info[6] &
5282 		    IEEE80211_HE_PHY_CAP6_PPE_THRESHOLD_PRESENT)
5283 			ath11k_gen_ppe_thresh(&band_cap->he_ppet,
5284 					      he_cap->ppe_thres);
5285 
5286 		if (band == NL80211_BAND_6GHZ) {
5287 			data[idx].he_6ghz_capa.capa =
5288 				ath11k_mac_setup_he_6ghz_cap(cap, band_cap);
5289 		}
5290 		idx++;
5291 	}
5292 
5293 	return idx;
5294 }
5295 
5296 static void ath11k_mac_setup_he_cap(struct ath11k *ar,
5297 				    struct ath11k_pdev_cap *cap)
5298 {
5299 	struct ieee80211_supported_band *band;
5300 	int count;
5301 
5302 	if (cap->supported_bands & WMI_HOST_WLAN_2G_CAP) {
5303 		count = ath11k_mac_copy_he_cap(ar, cap,
5304 					       ar->mac.iftype[NL80211_BAND_2GHZ],
5305 					       NL80211_BAND_2GHZ);
5306 		band = &ar->mac.sbands[NL80211_BAND_2GHZ];
5307 		band->iftype_data = ar->mac.iftype[NL80211_BAND_2GHZ];
5308 		band->n_iftype_data = count;
5309 	}
5310 
5311 	if (cap->supported_bands & WMI_HOST_WLAN_5G_CAP) {
5312 		count = ath11k_mac_copy_he_cap(ar, cap,
5313 					       ar->mac.iftype[NL80211_BAND_5GHZ],
5314 					       NL80211_BAND_5GHZ);
5315 		band = &ar->mac.sbands[NL80211_BAND_5GHZ];
5316 		band->iftype_data = ar->mac.iftype[NL80211_BAND_5GHZ];
5317 		band->n_iftype_data = count;
5318 	}
5319 
5320 	if (cap->supported_bands & WMI_HOST_WLAN_5G_CAP &&
5321 	    ar->supports_6ghz) {
5322 		count = ath11k_mac_copy_he_cap(ar, cap,
5323 					       ar->mac.iftype[NL80211_BAND_6GHZ],
5324 					       NL80211_BAND_6GHZ);
5325 		band = &ar->mac.sbands[NL80211_BAND_6GHZ];
5326 		band->iftype_data = ar->mac.iftype[NL80211_BAND_6GHZ];
5327 		band->n_iftype_data = count;
5328 	}
5329 }
5330 
5331 static int __ath11k_set_antenna(struct ath11k *ar, u32 tx_ant, u32 rx_ant)
5332 {
5333 	int ret;
5334 
5335 	lockdep_assert_held(&ar->conf_mutex);
5336 
5337 	if (ath11k_check_chain_mask(ar, tx_ant, true))
5338 		return -EINVAL;
5339 
5340 	if (ath11k_check_chain_mask(ar, rx_ant, false))
5341 		return -EINVAL;
5342 
5343 	ar->cfg_tx_chainmask = tx_ant;
5344 	ar->cfg_rx_chainmask = rx_ant;
5345 
5346 	if (ar->state != ATH11K_STATE_ON &&
5347 	    ar->state != ATH11K_STATE_RESTARTED)
5348 		return 0;
5349 
5350 	ret = ath11k_wmi_pdev_set_param(ar, WMI_PDEV_PARAM_TX_CHAIN_MASK,
5351 					tx_ant, ar->pdev->pdev_id);
5352 	if (ret) {
5353 		ath11k_warn(ar->ab, "failed to set tx-chainmask: %d, req 0x%x\n",
5354 			    ret, tx_ant);
5355 		return ret;
5356 	}
5357 
5358 	ar->num_tx_chains = get_num_chains(tx_ant);
5359 
5360 	ret = ath11k_wmi_pdev_set_param(ar, WMI_PDEV_PARAM_RX_CHAIN_MASK,
5361 					rx_ant, ar->pdev->pdev_id);
5362 	if (ret) {
5363 		ath11k_warn(ar->ab, "failed to set rx-chainmask: %d, req 0x%x\n",
5364 			    ret, rx_ant);
5365 		return ret;
5366 	}
5367 
5368 	ar->num_rx_chains = get_num_chains(rx_ant);
5369 
5370 	/* Reload HT/VHT/HE capability */
5371 	ath11k_mac_setup_ht_vht_cap(ar, &ar->pdev->cap, NULL);
5372 	ath11k_mac_setup_he_cap(ar, &ar->pdev->cap);
5373 
5374 	return 0;
5375 }
5376 
5377 static void ath11k_mgmt_over_wmi_tx_drop(struct ath11k *ar, struct sk_buff *skb)
5378 {
5379 	int num_mgmt;
5380 
5381 	ieee80211_free_txskb(ar->hw, skb);
5382 
5383 	num_mgmt = atomic_dec_if_positive(&ar->num_pending_mgmt_tx);
5384 
5385 	if (num_mgmt < 0)
5386 		WARN_ON_ONCE(1);
5387 
5388 	if (!num_mgmt)
5389 		wake_up(&ar->txmgmt_empty_waitq);
5390 }
5391 
5392 static void ath11k_mac_tx_mgmt_free(struct ath11k *ar, int buf_id)
5393 {
5394 	struct sk_buff *msdu;
5395 	struct ieee80211_tx_info *info;
5396 
5397 	spin_lock_bh(&ar->txmgmt_idr_lock);
5398 	msdu = idr_remove(&ar->txmgmt_idr, buf_id);
5399 	spin_unlock_bh(&ar->txmgmt_idr_lock);
5400 
5401 	if (!msdu)
5402 		return;
5403 
5404 	dma_unmap_single(ar->ab->dev, ATH11K_SKB_CB(msdu)->paddr, msdu->len,
5405 			 DMA_TO_DEVICE);
5406 
5407 	info = IEEE80211_SKB_CB(msdu);
5408 	memset(&info->status, 0, sizeof(info->status));
5409 
5410 	ath11k_mgmt_over_wmi_tx_drop(ar, msdu);
5411 }
5412 
5413 int ath11k_mac_tx_mgmt_pending_free(int buf_id, void *skb, void *ctx)
5414 {
5415 	struct ath11k *ar = ctx;
5416 
5417 	ath11k_mac_tx_mgmt_free(ar, buf_id);
5418 
5419 	return 0;
5420 }
5421 
5422 static int ath11k_mac_vif_txmgmt_idr_remove(int buf_id, void *skb, void *ctx)
5423 {
5424 	struct ieee80211_vif *vif = ctx;
5425 	struct ath11k_skb_cb *skb_cb = ATH11K_SKB_CB((struct sk_buff *)skb);
5426 	struct ath11k *ar = skb_cb->ar;
5427 
5428 	if (skb_cb->vif == vif)
5429 		ath11k_mac_tx_mgmt_free(ar, buf_id);
5430 
5431 	return 0;
5432 }
5433 
5434 static int ath11k_mac_mgmt_tx_wmi(struct ath11k *ar, struct ath11k_vif *arvif,
5435 				  struct sk_buff *skb)
5436 {
5437 	struct ath11k_base *ab = ar->ab;
5438 	struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
5439 	struct ieee80211_tx_info *info;
5440 	dma_addr_t paddr;
5441 	int buf_id;
5442 	int ret;
5443 
5444 	ATH11K_SKB_CB(skb)->ar = ar;
5445 
5446 	spin_lock_bh(&ar->txmgmt_idr_lock);
5447 	buf_id = idr_alloc(&ar->txmgmt_idr, skb, 0,
5448 			   ATH11K_TX_MGMT_NUM_PENDING_MAX, GFP_ATOMIC);
5449 	spin_unlock_bh(&ar->txmgmt_idr_lock);
5450 
5451 	ath11k_dbg(ar->ab, ATH11K_DBG_MAC,
5452 		   "mac tx mgmt frame, buf id %d\n", buf_id);
5453 
5454 	if (buf_id < 0)
5455 		return -ENOSPC;
5456 
5457 	info = IEEE80211_SKB_CB(skb);
5458 	if (!(info->flags & IEEE80211_TX_CTL_HW_80211_ENCAP)) {
5459 		if ((ieee80211_is_action(hdr->frame_control) ||
5460 		     ieee80211_is_deauth(hdr->frame_control) ||
5461 		     ieee80211_is_disassoc(hdr->frame_control)) &&
5462 		     ieee80211_has_protected(hdr->frame_control)) {
5463 			skb_put(skb, IEEE80211_CCMP_MIC_LEN);
5464 		}
5465 	}
5466 
5467 	paddr = dma_map_single(ab->dev, skb->data, skb->len, DMA_TO_DEVICE);
5468 	if (dma_mapping_error(ab->dev, paddr)) {
5469 		ath11k_warn(ab, "failed to DMA map mgmt Tx buffer\n");
5470 		ret = -EIO;
5471 		goto err_free_idr;
5472 	}
5473 
5474 	ATH11K_SKB_CB(skb)->paddr = paddr;
5475 
5476 	ret = ath11k_wmi_mgmt_send(ar, arvif->vdev_id, buf_id, skb);
5477 	if (ret) {
5478 		ath11k_warn(ar->ab, "failed to send mgmt frame: %d\n", ret);
5479 		goto err_unmap_buf;
5480 	}
5481 
5482 	return 0;
5483 
5484 err_unmap_buf:
5485 	dma_unmap_single(ab->dev, ATH11K_SKB_CB(skb)->paddr,
5486 			 skb->len, DMA_TO_DEVICE);
5487 err_free_idr:
5488 	spin_lock_bh(&ar->txmgmt_idr_lock);
5489 	idr_remove(&ar->txmgmt_idr, buf_id);
5490 	spin_unlock_bh(&ar->txmgmt_idr_lock);
5491 
5492 	return ret;
5493 }
5494 
5495 static void ath11k_mgmt_over_wmi_tx_purge(struct ath11k *ar)
5496 {
5497 	struct sk_buff *skb;
5498 
5499 	while ((skb = skb_dequeue(&ar->wmi_mgmt_tx_queue)) != NULL)
5500 		ath11k_mgmt_over_wmi_tx_drop(ar, skb);
5501 }
5502 
5503 static void ath11k_mgmt_over_wmi_tx_work(struct work_struct *work)
5504 {
5505 	struct ath11k *ar = container_of(work, struct ath11k, wmi_mgmt_tx_work);
5506 	struct ath11k_skb_cb *skb_cb;
5507 	struct ath11k_vif *arvif;
5508 	struct sk_buff *skb;
5509 	int ret;
5510 
5511 	while ((skb = skb_dequeue(&ar->wmi_mgmt_tx_queue)) != NULL) {
5512 		skb_cb = ATH11K_SKB_CB(skb);
5513 		if (!skb_cb->vif) {
5514 			ath11k_warn(ar->ab, "no vif found for mgmt frame\n");
5515 			ath11k_mgmt_over_wmi_tx_drop(ar, skb);
5516 			continue;
5517 		}
5518 
5519 		arvif = ath11k_vif_to_arvif(skb_cb->vif);
5520 		if (ar->allocated_vdev_map & (1LL << arvif->vdev_id) &&
5521 		    arvif->is_started) {
5522 			ret = ath11k_mac_mgmt_tx_wmi(ar, arvif, skb);
5523 			if (ret) {
5524 				ath11k_warn(ar->ab, "failed to tx mgmt frame, vdev_id %d :%d\n",
5525 					    arvif->vdev_id, ret);
5526 				ath11k_mgmt_over_wmi_tx_drop(ar, skb);
5527 			} else {
5528 				ath11k_dbg(ar->ab, ATH11K_DBG_MAC,
5529 					   "mac tx mgmt frame, vdev_id %d\n",
5530 					   arvif->vdev_id);
5531 			}
5532 		} else {
5533 			ath11k_warn(ar->ab,
5534 				    "dropping mgmt frame for vdev %d, is_started %d\n",
5535 				    arvif->vdev_id,
5536 				    arvif->is_started);
5537 			ath11k_mgmt_over_wmi_tx_drop(ar, skb);
5538 		}
5539 	}
5540 }
5541 
5542 static int ath11k_mac_mgmt_tx(struct ath11k *ar, struct sk_buff *skb,
5543 			      bool is_prb_rsp)
5544 {
5545 	struct sk_buff_head *q = &ar->wmi_mgmt_tx_queue;
5546 
5547 	if (test_bit(ATH11K_FLAG_CRASH_FLUSH, &ar->ab->dev_flags))
5548 		return -ESHUTDOWN;
5549 
5550 	/* Drop probe response packets when the pending management tx
5551 	 * count has reached a certain threshold, so as to prioritize
5552 	 * other mgmt packets like auth and assoc to be sent on time
5553 	 * for establishing successful connections.
5554 	 */
5555 	if (is_prb_rsp &&
5556 	    atomic_read(&ar->num_pending_mgmt_tx) > ATH11K_PRB_RSP_DROP_THRESHOLD) {
5557 		ath11k_warn(ar->ab,
5558 			    "dropping probe response as pending queue is almost full\n");
5559 		return -ENOSPC;
5560 	}
5561 
5562 	if (skb_queue_len_lockless(q) >= ATH11K_TX_MGMT_NUM_PENDING_MAX) {
5563 		ath11k_warn(ar->ab, "mgmt tx queue is full\n");
5564 		return -ENOSPC;
5565 	}
5566 
5567 	skb_queue_tail(q, skb);
5568 	atomic_inc(&ar->num_pending_mgmt_tx);
5569 	ieee80211_queue_work(ar->hw, &ar->wmi_mgmt_tx_work);
5570 
5571 	return 0;
5572 }
5573 
5574 int ath11k_mac_rfkill_config(struct ath11k *ar)
5575 {
5576 	struct ath11k_base *ab = ar->ab;
5577 	u32 param;
5578 	int ret;
5579 
5580 	if (ab->hw_params.rfkill_pin == 0)
5581 		return -EOPNOTSUPP;
5582 
5583 	ath11k_dbg(ab, ATH11K_DBG_MAC,
5584 		   "mac rfkill_pin %d rfkill_cfg %d rfkill_on_level %d",
5585 		   ab->hw_params.rfkill_pin, ab->hw_params.rfkill_cfg,
5586 		   ab->hw_params.rfkill_on_level);
5587 
5588 	param = FIELD_PREP(WMI_RFKILL_CFG_RADIO_LEVEL,
5589 			   ab->hw_params.rfkill_on_level) |
5590 		FIELD_PREP(WMI_RFKILL_CFG_GPIO_PIN_NUM,
5591 			   ab->hw_params.rfkill_pin) |
5592 		FIELD_PREP(WMI_RFKILL_CFG_PIN_AS_GPIO,
5593 			   ab->hw_params.rfkill_cfg);
5594 
5595 	ret = ath11k_wmi_pdev_set_param(ar, WMI_PDEV_PARAM_HW_RFKILL_CONFIG,
5596 					param, ar->pdev->pdev_id);
5597 	if (ret) {
5598 		ath11k_warn(ab,
5599 			    "failed to set rfkill config 0x%x: %d\n",
5600 			    param, ret);
5601 		return ret;
5602 	}
5603 
5604 	return 0;
5605 }
5606 
5607 int ath11k_mac_rfkill_enable_radio(struct ath11k *ar, bool enable)
5608 {
5609 	enum wmi_rfkill_enable_radio param;
5610 	int ret;
5611 
5612 	if (enable)
5613 		param = WMI_RFKILL_ENABLE_RADIO_ON;
5614 	else
5615 		param = WMI_RFKILL_ENABLE_RADIO_OFF;
5616 
5617 	ath11k_dbg(ar->ab, ATH11K_DBG_MAC, "mac %d rfkill enable %d",
5618 		   ar->pdev_idx, param);
5619 
5620 	ret = ath11k_wmi_pdev_set_param(ar, WMI_PDEV_PARAM_RFKILL_ENABLE,
5621 					param, ar->pdev->pdev_id);
5622 	if (ret) {
5623 		ath11k_warn(ar->ab, "failed to set rfkill enable param %d: %d\n",
5624 			    param, ret);
5625 		return ret;
5626 	}
5627 
5628 	return 0;
5629 }
5630 
5631 static void ath11k_mac_op_tx(struct ieee80211_hw *hw,
5632 			     struct ieee80211_tx_control *control,
5633 			     struct sk_buff *skb)
5634 {
5635 	struct ath11k_skb_cb *skb_cb = ATH11K_SKB_CB(skb);
5636 	struct ath11k *ar = hw->priv;
5637 	struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
5638 	struct ieee80211_vif *vif = info->control.vif;
5639 	struct ath11k_vif *arvif = ath11k_vif_to_arvif(vif);
5640 	struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
5641 	struct ieee80211_key_conf *key = info->control.hw_key;
5642 	struct ath11k_sta *arsta = NULL;
5643 	u32 info_flags = info->flags;
5644 	bool is_prb_rsp;
5645 	int ret;
5646 
5647 	memset(skb_cb, 0, sizeof(*skb_cb));
5648 	skb_cb->vif = vif;
5649 
5650 	if (key) {
5651 		skb_cb->cipher = key->cipher;
5652 		skb_cb->flags |= ATH11K_SKB_CIPHER_SET;
5653 	}
5654 
5655 	if (info_flags & IEEE80211_TX_CTL_HW_80211_ENCAP) {
5656 		skb_cb->flags |= ATH11K_SKB_HW_80211_ENCAP;
5657 	} else if (ieee80211_is_mgmt(hdr->frame_control)) {
5658 		is_prb_rsp = ieee80211_is_probe_resp(hdr->frame_control);
5659 		ret = ath11k_mac_mgmt_tx(ar, skb, is_prb_rsp);
5660 		if (ret) {
5661 			ath11k_warn(ar->ab, "failed to queue management frame %d\n",
5662 				    ret);
5663 			ieee80211_free_txskb(ar->hw, skb);
5664 		}
5665 		return;
5666 	}
5667 
5668 	if (control->sta)
5669 		arsta = (struct ath11k_sta *)control->sta->drv_priv;
5670 
5671 	ret = ath11k_dp_tx(ar, arvif, arsta, skb);
5672 	if (unlikely(ret)) {
5673 		ath11k_warn(ar->ab, "failed to transmit frame %d\n", ret);
5674 		ieee80211_free_txskb(ar->hw, skb);
5675 	}
5676 }
5677 
5678 void ath11k_mac_drain_tx(struct ath11k *ar)
5679 {
5680 	/* make sure rcu-protected mac80211 tx path itself is drained */
5681 	synchronize_net();
5682 
5683 	cancel_work_sync(&ar->wmi_mgmt_tx_work);
5684 	ath11k_mgmt_over_wmi_tx_purge(ar);
5685 }
5686 
5687 static int ath11k_mac_config_mon_status_default(struct ath11k *ar, bool enable)
5688 {
5689 	struct htt_rx_ring_tlv_filter tlv_filter = {0};
5690 	struct ath11k_base *ab = ar->ab;
5691 	int i, ret = 0;
5692 	u32 ring_id;
5693 
5694 	if (enable) {
5695 		tlv_filter = ath11k_mac_mon_status_filter_default;
5696 		if (ath11k_debugfs_rx_filter(ar))
5697 			tlv_filter.rx_filter = ath11k_debugfs_rx_filter(ar);
5698 	}
5699 
5700 	for (i = 0; i < ab->hw_params.num_rxmda_per_pdev; i++) {
5701 		ring_id = ar->dp.rx_mon_status_refill_ring[i].refill_buf_ring.ring_id;
5702 		ret = ath11k_dp_tx_htt_rx_filter_setup(ar->ab, ring_id,
5703 						       ar->dp.mac_id + i,
5704 						       HAL_RXDMA_MONITOR_STATUS,
5705 						       DP_RX_BUFFER_SIZE,
5706 						       &tlv_filter);
5707 	}
5708 
5709 	if (enable && !ar->ab->hw_params.rxdma1_enable)
5710 		mod_timer(&ar->ab->mon_reap_timer, jiffies +
5711 			  msecs_to_jiffies(ATH11K_MON_TIMER_INTERVAL));
5712 
5713 	return ret;
5714 }
5715 
5716 static int ath11k_mac_op_start(struct ieee80211_hw *hw)
5717 {
5718 	struct ath11k *ar = hw->priv;
5719 	struct ath11k_base *ab = ar->ab;
5720 	struct ath11k_pdev *pdev = ar->pdev;
5721 	int ret;
5722 
5723 	ath11k_mac_drain_tx(ar);
5724 	mutex_lock(&ar->conf_mutex);
5725 
5726 	switch (ar->state) {
5727 	case ATH11K_STATE_OFF:
5728 		ar->state = ATH11K_STATE_ON;
5729 		break;
5730 	case ATH11K_STATE_RESTARTING:
5731 		ar->state = ATH11K_STATE_RESTARTED;
5732 		break;
5733 	case ATH11K_STATE_RESTARTED:
5734 	case ATH11K_STATE_WEDGED:
5735 	case ATH11K_STATE_ON:
5736 		WARN_ON(1);
5737 		ret = -EINVAL;
5738 		goto err;
5739 	}
5740 
5741 	ret = ath11k_wmi_pdev_set_param(ar, WMI_PDEV_PARAM_PMF_QOS,
5742 					1, pdev->pdev_id);
5743 
5744 	if (ret) {
5745 		ath11k_err(ar->ab, "failed to enable PMF QOS: (%d\n", ret);
5746 		goto err;
5747 	}
5748 
5749 	ret = ath11k_wmi_pdev_set_param(ar, WMI_PDEV_PARAM_DYNAMIC_BW, 1,
5750 					pdev->pdev_id);
5751 	if (ret) {
5752 		ath11k_err(ar->ab, "failed to enable dynamic bw: %d\n", ret);
5753 		goto err;
5754 	}
5755 
5756 	if (test_bit(WMI_TLV_SERVICE_SPOOF_MAC_SUPPORT, ar->wmi->wmi_ab->svc_map)) {
5757 		ret = ath11k_wmi_scan_prob_req_oui(ar, ar->mac_addr);
5758 		if (ret) {
5759 			ath11k_err(ab, "failed to set prob req oui: %i\n", ret);
5760 			goto err;
5761 		}
5762 	}
5763 
5764 	ret = ath11k_wmi_pdev_set_param(ar, WMI_PDEV_PARAM_ARP_AC_OVERRIDE,
5765 					0, pdev->pdev_id);
5766 	if (ret) {
5767 		ath11k_err(ab, "failed to set ac override for ARP: %d\n",
5768 			   ret);
5769 		goto err;
5770 	}
5771 
5772 	ret = ath11k_wmi_send_dfs_phyerr_offload_enable_cmd(ar, pdev->pdev_id);
5773 	if (ret) {
5774 		ath11k_err(ab, "failed to offload radar detection: %d\n",
5775 			   ret);
5776 		goto err;
5777 	}
5778 
5779 	ret = ath11k_dp_tx_htt_h2t_ppdu_stats_req(ar,
5780 						  HTT_PPDU_STATS_TAG_DEFAULT);
5781 	if (ret) {
5782 		ath11k_err(ab, "failed to req ppdu stats: %d\n", ret);
5783 		goto err;
5784 	}
5785 
5786 	ret = ath11k_wmi_pdev_set_param(ar, WMI_PDEV_PARAM_MESH_MCAST_ENABLE,
5787 					1, pdev->pdev_id);
5788 
5789 	if (ret) {
5790 		ath11k_err(ar->ab, "failed to enable MESH MCAST ENABLE: (%d\n", ret);
5791 		goto err;
5792 	}
5793 
5794 	__ath11k_set_antenna(ar, ar->cfg_tx_chainmask, ar->cfg_rx_chainmask);
5795 
5796 	/* TODO: Do we need to enable ANI? */
5797 
5798 	ath11k_reg_update_chan_list(ar);
5799 
5800 	ar->num_started_vdevs = 0;
5801 	ar->num_created_vdevs = 0;
5802 	ar->num_peers = 0;
5803 	ar->allocated_vdev_map = 0;
5804 
5805 	/* Configure monitor status ring with default rx_filter to get rx status
5806 	 * such as rssi, rx_duration.
5807 	 */
5808 	ret = ath11k_mac_config_mon_status_default(ar, true);
5809 	if (ret) {
5810 		ath11k_err(ab, "failed to configure monitor status ring with default rx_filter: (%d)\n",
5811 			   ret);
5812 		goto err;
5813 	}
5814 
5815 	/* Configure the hash seed for hash based reo dest ring selection */
5816 	ath11k_wmi_pdev_lro_cfg(ar, ar->pdev->pdev_id);
5817 
5818 	/* allow device to enter IMPS */
5819 	if (ab->hw_params.idle_ps) {
5820 		ret = ath11k_wmi_pdev_set_param(ar, WMI_PDEV_PARAM_IDLE_PS_CONFIG,
5821 						1, pdev->pdev_id);
5822 		if (ret) {
5823 			ath11k_err(ab, "failed to enable idle ps: %d\n", ret);
5824 			goto err;
5825 		}
5826 	}
5827 
5828 	mutex_unlock(&ar->conf_mutex);
5829 
5830 	rcu_assign_pointer(ab->pdevs_active[ar->pdev_idx],
5831 			   &ab->pdevs[ar->pdev_idx]);
5832 
5833 	return 0;
5834 
5835 err:
5836 	ar->state = ATH11K_STATE_OFF;
5837 	mutex_unlock(&ar->conf_mutex);
5838 
5839 	return ret;
5840 }
5841 
5842 static void ath11k_mac_op_stop(struct ieee80211_hw *hw)
5843 {
5844 	struct ath11k *ar = hw->priv;
5845 	struct htt_ppdu_stats_info *ppdu_stats, *tmp;
5846 	int ret;
5847 
5848 	ath11k_mac_drain_tx(ar);
5849 
5850 	mutex_lock(&ar->conf_mutex);
5851 	ret = ath11k_mac_config_mon_status_default(ar, false);
5852 	if (ret)
5853 		ath11k_err(ar->ab, "failed to clear rx_filter for monitor status ring: (%d)\n",
5854 			   ret);
5855 
5856 	clear_bit(ATH11K_CAC_RUNNING, &ar->dev_flags);
5857 	ar->state = ATH11K_STATE_OFF;
5858 	mutex_unlock(&ar->conf_mutex);
5859 
5860 	cancel_delayed_work_sync(&ar->scan.timeout);
5861 	cancel_work_sync(&ar->regd_update_work);
5862 	cancel_work_sync(&ar->ab->update_11d_work);
5863 	cancel_work_sync(&ar->ab->rfkill_work);
5864 
5865 	spin_lock_bh(&ar->data_lock);
5866 	list_for_each_entry_safe(ppdu_stats, tmp, &ar->ppdu_stats_info, list) {
5867 		list_del(&ppdu_stats->list);
5868 		kfree(ppdu_stats);
5869 	}
5870 	spin_unlock_bh(&ar->data_lock);
5871 
5872 	rcu_assign_pointer(ar->ab->pdevs_active[ar->pdev_idx], NULL);
5873 
5874 	synchronize_rcu();
5875 
5876 	atomic_set(&ar->num_pending_mgmt_tx, 0);
5877 }
5878 
5879 static void
5880 ath11k_mac_setup_vdev_create_params(struct ath11k_vif *arvif,
5881 				    struct vdev_create_params *params)
5882 {
5883 	struct ath11k *ar = arvif->ar;
5884 	struct ath11k_pdev *pdev = ar->pdev;
5885 
5886 	params->if_id = arvif->vdev_id;
5887 	params->type = arvif->vdev_type;
5888 	params->subtype = arvif->vdev_subtype;
5889 	params->pdev_id = pdev->pdev_id;
5890 
5891 	if (pdev->cap.supported_bands & WMI_HOST_WLAN_2G_CAP) {
5892 		params->chains[NL80211_BAND_2GHZ].tx = ar->num_tx_chains;
5893 		params->chains[NL80211_BAND_2GHZ].rx = ar->num_rx_chains;
5894 	}
5895 	if (pdev->cap.supported_bands & WMI_HOST_WLAN_5G_CAP) {
5896 		params->chains[NL80211_BAND_5GHZ].tx = ar->num_tx_chains;
5897 		params->chains[NL80211_BAND_5GHZ].rx = ar->num_rx_chains;
5898 	}
5899 	if (pdev->cap.supported_bands & WMI_HOST_WLAN_5G_CAP &&
5900 	    ar->supports_6ghz) {
5901 		params->chains[NL80211_BAND_6GHZ].tx = ar->num_tx_chains;
5902 		params->chains[NL80211_BAND_6GHZ].rx = ar->num_rx_chains;
5903 	}
5904 }
5905 
5906 static u32
5907 ath11k_mac_prepare_he_mode(struct ath11k_pdev *pdev, u32 viftype)
5908 {
5909 	struct ath11k_pdev_cap *pdev_cap = &pdev->cap;
5910 	struct ath11k_band_cap *cap_band = NULL;
5911 	u32 *hecap_phy_ptr = NULL;
5912 	u32 hemode = 0;
5913 
5914 	if (pdev->cap.supported_bands & WMI_HOST_WLAN_2G_CAP)
5915 		cap_band = &pdev_cap->band[NL80211_BAND_2GHZ];
5916 	else
5917 		cap_band = &pdev_cap->band[NL80211_BAND_5GHZ];
5918 
5919 	hecap_phy_ptr = &cap_band->he_cap_phy_info[0];
5920 
5921 	hemode = FIELD_PREP(HE_MODE_SU_TX_BFEE, HE_SU_BFEE_ENABLE) |
5922 		 FIELD_PREP(HE_MODE_SU_TX_BFER, HECAP_PHY_SUBFMR_GET(hecap_phy_ptr)) |
5923 		 FIELD_PREP(HE_MODE_UL_MUMIMO, HECAP_PHY_ULMUMIMO_GET(hecap_phy_ptr));
5924 
5925 	/* TODO WDS and other modes */
5926 	if (viftype == NL80211_IFTYPE_AP) {
5927 		hemode |= FIELD_PREP(HE_MODE_MU_TX_BFER,
5928 			  HECAP_PHY_MUBFMR_GET(hecap_phy_ptr)) |
5929 			  FIELD_PREP(HE_MODE_DL_OFDMA, HE_DL_MUOFDMA_ENABLE) |
5930 			  FIELD_PREP(HE_MODE_UL_OFDMA, HE_UL_MUOFDMA_ENABLE);
5931 	} else {
5932 		hemode |= FIELD_PREP(HE_MODE_MU_TX_BFEE, HE_MU_BFEE_ENABLE);
5933 	}
5934 
5935 	return hemode;
5936 }
5937 
5938 static int ath11k_set_he_mu_sounding_mode(struct ath11k *ar,
5939 					  struct ath11k_vif *arvif)
5940 {
5941 	u32 param_id, param_value;
5942 	struct ath11k_base *ab = ar->ab;
5943 	int ret = 0;
5944 
5945 	param_id = WMI_VDEV_PARAM_SET_HEMU_MODE;
5946 	param_value = ath11k_mac_prepare_he_mode(ar->pdev, arvif->vif->type);
5947 	ret = ath11k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
5948 					    param_id, param_value);
5949 	if (ret) {
5950 		ath11k_warn(ab, "failed to set vdev %d HE MU mode: %d param_value %x\n",
5951 			    arvif->vdev_id, ret, param_value);
5952 		return ret;
5953 	}
5954 	param_id = WMI_VDEV_PARAM_SET_HE_SOUNDING_MODE;
5955 	param_value =
5956 		FIELD_PREP(HE_VHT_SOUNDING_MODE, HE_VHT_SOUNDING_MODE_ENABLE) |
5957 		FIELD_PREP(HE_TRIG_NONTRIG_SOUNDING_MODE,
5958 			   HE_TRIG_NONTRIG_SOUNDING_MODE_ENABLE);
5959 	ret = ath11k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
5960 					    param_id, param_value);
5961 	if (ret) {
5962 		ath11k_warn(ab, "failed to set vdev %d HE MU mode: %d\n",
5963 			    arvif->vdev_id, ret);
5964 		return ret;
5965 	}
5966 	return ret;
5967 }
5968 
5969 static void ath11k_mac_op_update_vif_offload(struct ieee80211_hw *hw,
5970 					     struct ieee80211_vif *vif)
5971 {
5972 	struct ath11k *ar = hw->priv;
5973 	struct ath11k_base *ab = ar->ab;
5974 	struct ath11k_vif *arvif = ath11k_vif_to_arvif(vif);
5975 	u32 param_id, param_value;
5976 	int ret;
5977 
5978 	param_id = WMI_VDEV_PARAM_TX_ENCAP_TYPE;
5979 	if (ath11k_frame_mode != ATH11K_HW_TXRX_ETHERNET ||
5980 	    (vif->type != NL80211_IFTYPE_STATION &&
5981 	     vif->type != NL80211_IFTYPE_AP))
5982 		vif->offload_flags &= ~(IEEE80211_OFFLOAD_ENCAP_ENABLED |
5983 					IEEE80211_OFFLOAD_DECAP_ENABLED);
5984 
5985 	if (vif->offload_flags & IEEE80211_OFFLOAD_ENCAP_ENABLED)
5986 		param_value = ATH11K_HW_TXRX_ETHERNET;
5987 	else if (test_bit(ATH11K_FLAG_RAW_MODE, &ab->dev_flags))
5988 		param_value = ATH11K_HW_TXRX_RAW;
5989 	else
5990 		param_value = ATH11K_HW_TXRX_NATIVE_WIFI;
5991 
5992 	ret = ath11k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
5993 					    param_id, param_value);
5994 	if (ret) {
5995 		ath11k_warn(ab, "failed to set vdev %d tx encap mode: %d\n",
5996 			    arvif->vdev_id, ret);
5997 		vif->offload_flags &= ~IEEE80211_OFFLOAD_ENCAP_ENABLED;
5998 	}
5999 
6000 	param_id = WMI_VDEV_PARAM_RX_DECAP_TYPE;
6001 	if (vif->offload_flags & IEEE80211_OFFLOAD_DECAP_ENABLED)
6002 		param_value = ATH11K_HW_TXRX_ETHERNET;
6003 	else if (test_bit(ATH11K_FLAG_RAW_MODE, &ab->dev_flags))
6004 		param_value = ATH11K_HW_TXRX_RAW;
6005 	else
6006 		param_value = ATH11K_HW_TXRX_NATIVE_WIFI;
6007 
6008 	ret = ath11k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
6009 					    param_id, param_value);
6010 	if (ret) {
6011 		ath11k_warn(ab, "failed to set vdev %d rx decap mode: %d\n",
6012 			    arvif->vdev_id, ret);
6013 		vif->offload_flags &= ~IEEE80211_OFFLOAD_DECAP_ENABLED;
6014 	}
6015 }
6016 
6017 static bool ath11k_mac_vif_ap_active_any(struct ath11k_base *ab)
6018 {
6019 	struct ath11k *ar;
6020 	struct ath11k_pdev *pdev;
6021 	struct ath11k_vif *arvif;
6022 	int i;
6023 
6024 	for (i = 0; i < ab->num_radios; i++) {
6025 		pdev = &ab->pdevs[i];
6026 		ar = pdev->ar;
6027 		list_for_each_entry(arvif, &ar->arvifs, list) {
6028 			if (arvif->is_up && arvif->vdev_type == WMI_VDEV_TYPE_AP)
6029 				return true;
6030 		}
6031 	}
6032 	return false;
6033 }
6034 
6035 void ath11k_mac_11d_scan_start(struct ath11k *ar, u32 vdev_id, bool wait)
6036 {
6037 	struct wmi_11d_scan_start_params param;
6038 	int ret;
6039 
6040 	mutex_lock(&ar->ab->vdev_id_11d_lock);
6041 
6042 	ath11k_dbg(ar->ab, ATH11K_DBG_MAC, "mac vdev id for 11d scan %d\n",
6043 		   ar->vdev_id_11d_scan);
6044 
6045 	if (ar->regdom_set_by_user)
6046 		goto fin;
6047 
6048 	if (ar->vdev_id_11d_scan != ATH11K_11D_INVALID_VDEV_ID)
6049 		goto fin;
6050 
6051 	if (!test_bit(WMI_TLV_SERVICE_11D_OFFLOAD, ar->ab->wmi_ab.svc_map))
6052 		goto fin;
6053 
6054 	if (ath11k_mac_vif_ap_active_any(ar->ab))
6055 		goto fin;
6056 
6057 	param.vdev_id = vdev_id;
6058 	param.start_interval_msec = 0;
6059 	param.scan_period_msec = ATH11K_SCAN_11D_INTERVAL;
6060 
6061 	ath11k_dbg(ar->ab, ATH11K_DBG_MAC, "mac start 11d scan\n");
6062 
6063 	if (wait)
6064 		reinit_completion(&ar->finish_11d_scan);
6065 
6066 	ret = ath11k_wmi_send_11d_scan_start_cmd(ar, &param);
6067 	if (ret) {
6068 		ath11k_warn(ar->ab, "failed to start 11d scan vdev %d ret: %d\n",
6069 			    vdev_id, ret);
6070 	} else {
6071 		ar->vdev_id_11d_scan = vdev_id;
6072 		if (wait) {
6073 			ar->pending_11d = true;
6074 			ret = wait_for_completion_timeout(&ar->finish_11d_scan,
6075 							  5 * HZ);
6076 			ath11k_dbg(ar->ab, ATH11K_DBG_MAC,
6077 				   "mac 11d scan left time %d\n", ret);
6078 
6079 			if (!ret)
6080 				ar->pending_11d = false;
6081 		}
6082 	}
6083 
6084 fin:
6085 	mutex_unlock(&ar->ab->vdev_id_11d_lock);
6086 }
6087 
6088 void ath11k_mac_11d_scan_stop(struct ath11k *ar)
6089 {
6090 	int ret;
6091 	u32 vdev_id;
6092 
6093 	if (!test_bit(WMI_TLV_SERVICE_11D_OFFLOAD, ar->ab->wmi_ab.svc_map))
6094 		return;
6095 
6096 	ath11k_dbg(ar->ab, ATH11K_DBG_MAC, "mac stop 11d scan\n");
6097 
6098 	mutex_lock(&ar->ab->vdev_id_11d_lock);
6099 
6100 	ath11k_dbg(ar->ab, ATH11K_DBG_MAC, "mac stop 11d vdev id %d\n",
6101 		   ar->vdev_id_11d_scan);
6102 
6103 	if (ar->vdev_id_11d_scan != ATH11K_11D_INVALID_VDEV_ID) {
6104 		vdev_id = ar->vdev_id_11d_scan;
6105 
6106 		ret = ath11k_wmi_send_11d_scan_stop_cmd(ar, vdev_id);
6107 		if (ret)
6108 			ath11k_warn(ar->ab,
6109 				    "failed to stopt 11d scan vdev %d ret: %d\n",
6110 				    vdev_id, ret);
6111 		else
6112 			ar->vdev_id_11d_scan = ATH11K_11D_INVALID_VDEV_ID;
6113 	}
6114 	mutex_unlock(&ar->ab->vdev_id_11d_lock);
6115 }
6116 
6117 void ath11k_mac_11d_scan_stop_all(struct ath11k_base *ab)
6118 {
6119 	struct ath11k *ar;
6120 	struct ath11k_pdev *pdev;
6121 	int i;
6122 
6123 	ath11k_dbg(ab, ATH11K_DBG_MAC, "mac stop soc 11d scan\n");
6124 
6125 	for (i = 0; i < ab->num_radios; i++) {
6126 		pdev = &ab->pdevs[i];
6127 		ar = pdev->ar;
6128 
6129 		ath11k_mac_11d_scan_stop(ar);
6130 	}
6131 }
6132 
6133 static int ath11k_mac_op_add_interface(struct ieee80211_hw *hw,
6134 				       struct ieee80211_vif *vif)
6135 {
6136 	struct ath11k *ar = hw->priv;
6137 	struct ath11k_base *ab = ar->ab;
6138 	struct ath11k_vif *arvif = ath11k_vif_to_arvif(vif);
6139 	struct vdev_create_params vdev_param = {0};
6140 	struct peer_create_params peer_param;
6141 	u32 param_id, param_value;
6142 	u16 nss;
6143 	int i;
6144 	int ret, fbret;
6145 	int bit;
6146 
6147 	vif->driver_flags |= IEEE80211_VIF_SUPPORTS_UAPSD;
6148 
6149 	mutex_lock(&ar->conf_mutex);
6150 
6151 	if (vif->type == NL80211_IFTYPE_AP &&
6152 	    ar->num_peers > (ar->max_num_peers - 1)) {
6153 		ath11k_warn(ab, "failed to create vdev due to insufficient peer entry resource in firmware\n");
6154 		ret = -ENOBUFS;
6155 		goto err;
6156 	}
6157 
6158 	if (ar->num_created_vdevs > (TARGET_NUM_VDEVS(ab) - 1)) {
6159 		ath11k_warn(ab, "failed to create vdev %u, reached max vdev limit %d\n",
6160 			    ar->num_created_vdevs, TARGET_NUM_VDEVS(ab));
6161 		ret = -EBUSY;
6162 		goto err;
6163 	}
6164 
6165 	memset(arvif, 0, sizeof(*arvif));
6166 
6167 	arvif->ar = ar;
6168 	arvif->vif = vif;
6169 
6170 	INIT_LIST_HEAD(&arvif->list);
6171 	INIT_DELAYED_WORK(&arvif->connection_loss_work,
6172 			  ath11k_mac_vif_sta_connection_loss_work);
6173 
6174 	for (i = 0; i < ARRAY_SIZE(arvif->bitrate_mask.control); i++) {
6175 		arvif->bitrate_mask.control[i].legacy = 0xffffffff;
6176 		arvif->bitrate_mask.control[i].gi = NL80211_TXRATE_FORCE_SGI;
6177 		memset(arvif->bitrate_mask.control[i].ht_mcs, 0xff,
6178 		       sizeof(arvif->bitrate_mask.control[i].ht_mcs));
6179 		memset(arvif->bitrate_mask.control[i].vht_mcs, 0xff,
6180 		       sizeof(arvif->bitrate_mask.control[i].vht_mcs));
6181 		memset(arvif->bitrate_mask.control[i].he_mcs, 0xff,
6182 		       sizeof(arvif->bitrate_mask.control[i].he_mcs));
6183 	}
6184 
6185 	bit = __ffs64(ab->free_vdev_map);
6186 
6187 	arvif->vdev_id = bit;
6188 	arvif->vdev_subtype = WMI_VDEV_SUBTYPE_NONE;
6189 
6190 	switch (vif->type) {
6191 	case NL80211_IFTYPE_UNSPECIFIED:
6192 	case NL80211_IFTYPE_STATION:
6193 		arvif->vdev_type = WMI_VDEV_TYPE_STA;
6194 		break;
6195 	case NL80211_IFTYPE_MESH_POINT:
6196 		arvif->vdev_subtype = WMI_VDEV_SUBTYPE_MESH_11S;
6197 		fallthrough;
6198 	case NL80211_IFTYPE_AP:
6199 		arvif->vdev_type = WMI_VDEV_TYPE_AP;
6200 		break;
6201 	case NL80211_IFTYPE_MONITOR:
6202 		arvif->vdev_type = WMI_VDEV_TYPE_MONITOR;
6203 		ar->monitor_vdev_id = bit;
6204 		break;
6205 	default:
6206 		WARN_ON(1);
6207 		break;
6208 	}
6209 
6210 	ath11k_dbg(ar->ab, ATH11K_DBG_MAC, "mac add interface id %d type %d subtype %d map %llx\n",
6211 		   arvif->vdev_id, arvif->vdev_type, arvif->vdev_subtype,
6212 		   ab->free_vdev_map);
6213 
6214 	vif->cab_queue = arvif->vdev_id % (ATH11K_HW_MAX_QUEUES - 1);
6215 	for (i = 0; i < ARRAY_SIZE(vif->hw_queue); i++)
6216 		vif->hw_queue[i] = i % (ATH11K_HW_MAX_QUEUES - 1);
6217 
6218 	ath11k_mac_setup_vdev_create_params(arvif, &vdev_param);
6219 
6220 	ret = ath11k_wmi_vdev_create(ar, vif->addr, &vdev_param);
6221 	if (ret) {
6222 		ath11k_warn(ab, "failed to create WMI vdev %d: %d\n",
6223 			    arvif->vdev_id, ret);
6224 		goto err;
6225 	}
6226 
6227 	ar->num_created_vdevs++;
6228 	ath11k_dbg(ab, ATH11K_DBG_MAC, "vdev %pM created, vdev_id %d\n",
6229 		   vif->addr, arvif->vdev_id);
6230 	ar->allocated_vdev_map |= 1LL << arvif->vdev_id;
6231 	ab->free_vdev_map &= ~(1LL << arvif->vdev_id);
6232 
6233 	spin_lock_bh(&ar->data_lock);
6234 	list_add(&arvif->list, &ar->arvifs);
6235 	spin_unlock_bh(&ar->data_lock);
6236 
6237 	ath11k_mac_op_update_vif_offload(hw, vif);
6238 
6239 	nss = get_num_chains(ar->cfg_tx_chainmask) ? : 1;
6240 	ret = ath11k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
6241 					    WMI_VDEV_PARAM_NSS, nss);
6242 	if (ret) {
6243 		ath11k_warn(ab, "failed to set vdev %d chainmask 0x%x, nss %d :%d\n",
6244 			    arvif->vdev_id, ar->cfg_tx_chainmask, nss, ret);
6245 		goto err_vdev_del;
6246 	}
6247 
6248 	switch (arvif->vdev_type) {
6249 	case WMI_VDEV_TYPE_AP:
6250 		peer_param.vdev_id = arvif->vdev_id;
6251 		peer_param.peer_addr = vif->addr;
6252 		peer_param.peer_type = WMI_PEER_TYPE_DEFAULT;
6253 		ret = ath11k_peer_create(ar, arvif, NULL, &peer_param);
6254 		if (ret) {
6255 			ath11k_warn(ab, "failed to vdev %d create peer for AP: %d\n",
6256 				    arvif->vdev_id, ret);
6257 			goto err_vdev_del;
6258 		}
6259 
6260 		ret = ath11k_mac_set_kickout(arvif);
6261 		if (ret) {
6262 			ath11k_warn(ar->ab, "failed to set vdev %i kickout parameters: %d\n",
6263 				    arvif->vdev_id, ret);
6264 			goto err_peer_del;
6265 		}
6266 
6267 		ath11k_mac_11d_scan_stop_all(ar->ab);
6268 		break;
6269 	case WMI_VDEV_TYPE_STA:
6270 		param_id = WMI_STA_PS_PARAM_RX_WAKE_POLICY;
6271 		param_value = WMI_STA_PS_RX_WAKE_POLICY_WAKE;
6272 		ret = ath11k_wmi_set_sta_ps_param(ar, arvif->vdev_id,
6273 						  param_id, param_value);
6274 		if (ret) {
6275 			ath11k_warn(ar->ab, "failed to set vdev %d RX wake policy: %d\n",
6276 				    arvif->vdev_id, ret);
6277 			goto err_peer_del;
6278 		}
6279 
6280 		param_id = WMI_STA_PS_PARAM_TX_WAKE_THRESHOLD;
6281 		param_value = WMI_STA_PS_TX_WAKE_THRESHOLD_ALWAYS;
6282 		ret = ath11k_wmi_set_sta_ps_param(ar, arvif->vdev_id,
6283 						  param_id, param_value);
6284 		if (ret) {
6285 			ath11k_warn(ar->ab, "failed to set vdev %d TX wake threshold: %d\n",
6286 				    arvif->vdev_id, ret);
6287 			goto err_peer_del;
6288 		}
6289 
6290 		param_id = WMI_STA_PS_PARAM_PSPOLL_COUNT;
6291 		param_value = WMI_STA_PS_PSPOLL_COUNT_NO_MAX;
6292 		ret = ath11k_wmi_set_sta_ps_param(ar, arvif->vdev_id,
6293 						  param_id, param_value);
6294 		if (ret) {
6295 			ath11k_warn(ar->ab, "failed to set vdev %d pspoll count: %d\n",
6296 				    arvif->vdev_id, ret);
6297 			goto err_peer_del;
6298 		}
6299 
6300 		ret = ath11k_wmi_pdev_set_ps_mode(ar, arvif->vdev_id,
6301 						  WMI_STA_PS_MODE_DISABLED);
6302 		if (ret) {
6303 			ath11k_warn(ar->ab, "failed to disable vdev %d ps mode: %d\n",
6304 				    arvif->vdev_id, ret);
6305 			goto err_peer_del;
6306 		}
6307 
6308 		ath11k_mac_11d_scan_start(ar, arvif->vdev_id, true);
6309 
6310 		break;
6311 	case WMI_VDEV_TYPE_MONITOR:
6312 		set_bit(ATH11K_FLAG_MONITOR_VDEV_CREATED, &ar->monitor_flags);
6313 		break;
6314 	default:
6315 		break;
6316 	}
6317 
6318 	arvif->txpower = vif->bss_conf.txpower;
6319 	ret = ath11k_mac_txpower_recalc(ar);
6320 	if (ret)
6321 		goto err_peer_del;
6322 
6323 	param_id = WMI_VDEV_PARAM_RTS_THRESHOLD;
6324 	param_value = ar->hw->wiphy->rts_threshold;
6325 	ret = ath11k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
6326 					    param_id, param_value);
6327 	if (ret) {
6328 		ath11k_warn(ar->ab, "failed to set rts threshold for vdev %d: %d\n",
6329 			    arvif->vdev_id, ret);
6330 	}
6331 
6332 	ath11k_dp_vdev_tx_attach(ar, arvif);
6333 
6334 	if (vif->type != NL80211_IFTYPE_MONITOR &&
6335 	    test_bit(ATH11K_FLAG_MONITOR_CONF_ENABLED, &ar->monitor_flags)) {
6336 		ret = ath11k_mac_monitor_vdev_create(ar);
6337 		if (ret) {
6338 			ath11k_warn(ar->ab, "failed to create monitor vdev during add interface: %d",
6339 				    ret);
6340 			goto err_peer_del;
6341 		}
6342 	}
6343 
6344 	mutex_unlock(&ar->conf_mutex);
6345 
6346 	return 0;
6347 
6348 err_peer_del:
6349 	if (arvif->vdev_type == WMI_VDEV_TYPE_AP) {
6350 		reinit_completion(&ar->peer_delete_done);
6351 
6352 		fbret = ath11k_wmi_send_peer_delete_cmd(ar, vif->addr,
6353 							arvif->vdev_id);
6354 		if (fbret) {
6355 			ath11k_warn(ar->ab, "failed to delete peer vdev_id %d addr %pM\n",
6356 				    arvif->vdev_id, vif->addr);
6357 			goto err;
6358 		}
6359 
6360 		fbret = ath11k_wait_for_peer_delete_done(ar, arvif->vdev_id,
6361 							 vif->addr);
6362 		if (fbret)
6363 			goto err;
6364 
6365 		ar->num_peers--;
6366 	}
6367 
6368 err_vdev_del:
6369 	ath11k_wmi_vdev_delete(ar, arvif->vdev_id);
6370 	ar->num_created_vdevs--;
6371 	ar->allocated_vdev_map &= ~(1LL << arvif->vdev_id);
6372 	ab->free_vdev_map |= 1LL << arvif->vdev_id;
6373 	spin_lock_bh(&ar->data_lock);
6374 	list_del(&arvif->list);
6375 	spin_unlock_bh(&ar->data_lock);
6376 
6377 err:
6378 	mutex_unlock(&ar->conf_mutex);
6379 
6380 	return ret;
6381 }
6382 
6383 static int ath11k_mac_vif_unref(int buf_id, void *skb, void *ctx)
6384 {
6385 	struct ieee80211_vif *vif = (struct ieee80211_vif *)ctx;
6386 	struct ath11k_skb_cb *skb_cb = ATH11K_SKB_CB((struct sk_buff *)skb);
6387 
6388 	if (skb_cb->vif == vif)
6389 		skb_cb->vif = NULL;
6390 
6391 	return 0;
6392 }
6393 
6394 static void ath11k_mac_op_remove_interface(struct ieee80211_hw *hw,
6395 					   struct ieee80211_vif *vif)
6396 {
6397 	struct ath11k *ar = hw->priv;
6398 	struct ath11k_vif *arvif = ath11k_vif_to_arvif(vif);
6399 	struct ath11k_base *ab = ar->ab;
6400 	unsigned long time_left;
6401 	int ret;
6402 	int i;
6403 
6404 	cancel_delayed_work_sync(&arvif->connection_loss_work);
6405 
6406 	mutex_lock(&ar->conf_mutex);
6407 
6408 	ath11k_dbg(ab, ATH11K_DBG_MAC, "mac remove interface (vdev %d)\n",
6409 		   arvif->vdev_id);
6410 
6411 	if (arvif->vdev_type == WMI_VDEV_TYPE_STA)
6412 		ath11k_mac_11d_scan_stop(ar);
6413 
6414 	if (arvif->vdev_type == WMI_VDEV_TYPE_AP) {
6415 		ret = ath11k_peer_delete(ar, arvif->vdev_id, vif->addr);
6416 		if (ret)
6417 			ath11k_warn(ab, "failed to submit AP self-peer removal on vdev %d: %d\n",
6418 				    arvif->vdev_id, ret);
6419 	}
6420 
6421 	reinit_completion(&ar->vdev_delete_done);
6422 
6423 	ret = ath11k_wmi_vdev_delete(ar, arvif->vdev_id);
6424 	if (ret) {
6425 		ath11k_warn(ab, "failed to delete WMI vdev %d: %d\n",
6426 			    arvif->vdev_id, ret);
6427 		goto err_vdev_del;
6428 	}
6429 
6430 	time_left = wait_for_completion_timeout(&ar->vdev_delete_done,
6431 						ATH11K_VDEV_DELETE_TIMEOUT_HZ);
6432 	if (time_left == 0) {
6433 		ath11k_warn(ab, "Timeout in receiving vdev delete response\n");
6434 		goto err_vdev_del;
6435 	}
6436 
6437 	ab->free_vdev_map |= 1LL << (arvif->vdev_id);
6438 	ar->allocated_vdev_map &= ~(1LL << arvif->vdev_id);
6439 	ar->num_created_vdevs--;
6440 
6441 	ath11k_dbg(ab, ATH11K_DBG_MAC, "vdev %pM deleted, vdev_id %d\n",
6442 		   vif->addr, arvif->vdev_id);
6443 
6444 	if (arvif->vdev_type == WMI_VDEV_TYPE_MONITOR) {
6445 		clear_bit(ATH11K_FLAG_MONITOR_VDEV_CREATED, &ar->monitor_flags);
6446 		ar->monitor_vdev_id = -1;
6447 	} else if (test_bit(ATH11K_FLAG_MONITOR_VDEV_CREATED, &ar->monitor_flags) &&
6448 		   !test_bit(ATH11K_FLAG_MONITOR_STARTED, &ar->monitor_flags)) {
6449 		ret = ath11k_mac_monitor_vdev_delete(ar);
6450 		if (ret)
6451 			/* continue even if there's an error */
6452 			ath11k_warn(ar->ab, "failed to delete vdev monitor during remove interface: %d",
6453 				    ret);
6454 	}
6455 
6456 err_vdev_del:
6457 	spin_lock_bh(&ar->data_lock);
6458 	list_del(&arvif->list);
6459 	spin_unlock_bh(&ar->data_lock);
6460 
6461 	ath11k_peer_cleanup(ar, arvif->vdev_id);
6462 
6463 	idr_for_each(&ar->txmgmt_idr,
6464 		     ath11k_mac_vif_txmgmt_idr_remove, vif);
6465 
6466 	for (i = 0; i < ab->hw_params.max_tx_ring; i++) {
6467 		spin_lock_bh(&ab->dp.tx_ring[i].tx_idr_lock);
6468 		idr_for_each(&ab->dp.tx_ring[i].txbuf_idr,
6469 			     ath11k_mac_vif_unref, vif);
6470 		spin_unlock_bh(&ab->dp.tx_ring[i].tx_idr_lock);
6471 	}
6472 
6473 	/* Recalc txpower for remaining vdev */
6474 	ath11k_mac_txpower_recalc(ar);
6475 
6476 	/* TODO: recal traffic pause state based on the available vdevs */
6477 
6478 	mutex_unlock(&ar->conf_mutex);
6479 }
6480 
6481 /* FIXME: Has to be verified. */
6482 #define SUPPORTED_FILTERS			\
6483 	(FIF_ALLMULTI |				\
6484 	FIF_CONTROL |				\
6485 	FIF_PSPOLL |				\
6486 	FIF_OTHER_BSS |				\
6487 	FIF_BCN_PRBRESP_PROMISC |		\
6488 	FIF_PROBE_REQ |				\
6489 	FIF_FCSFAIL)
6490 
6491 static void ath11k_mac_op_configure_filter(struct ieee80211_hw *hw,
6492 					   unsigned int changed_flags,
6493 					   unsigned int *total_flags,
6494 					   u64 multicast)
6495 {
6496 	struct ath11k *ar = hw->priv;
6497 
6498 	mutex_lock(&ar->conf_mutex);
6499 
6500 	*total_flags &= SUPPORTED_FILTERS;
6501 	ar->filter_flags = *total_flags;
6502 
6503 	mutex_unlock(&ar->conf_mutex);
6504 }
6505 
6506 static int ath11k_mac_op_get_antenna(struct ieee80211_hw *hw, u32 *tx_ant, u32 *rx_ant)
6507 {
6508 	struct ath11k *ar = hw->priv;
6509 
6510 	mutex_lock(&ar->conf_mutex);
6511 
6512 	*tx_ant = ar->cfg_tx_chainmask;
6513 	*rx_ant = ar->cfg_rx_chainmask;
6514 
6515 	mutex_unlock(&ar->conf_mutex);
6516 
6517 	return 0;
6518 }
6519 
6520 static int ath11k_mac_op_set_antenna(struct ieee80211_hw *hw, u32 tx_ant, u32 rx_ant)
6521 {
6522 	struct ath11k *ar = hw->priv;
6523 	int ret;
6524 
6525 	mutex_lock(&ar->conf_mutex);
6526 	ret = __ath11k_set_antenna(ar, tx_ant, rx_ant);
6527 	mutex_unlock(&ar->conf_mutex);
6528 
6529 	return ret;
6530 }
6531 
6532 static int ath11k_mac_op_ampdu_action(struct ieee80211_hw *hw,
6533 				      struct ieee80211_vif *vif,
6534 				      struct ieee80211_ampdu_params *params)
6535 {
6536 	struct ath11k *ar = hw->priv;
6537 	int ret = -EINVAL;
6538 
6539 	mutex_lock(&ar->conf_mutex);
6540 
6541 	switch (params->action) {
6542 	case IEEE80211_AMPDU_RX_START:
6543 		ret = ath11k_dp_rx_ampdu_start(ar, params);
6544 		break;
6545 	case IEEE80211_AMPDU_RX_STOP:
6546 		ret = ath11k_dp_rx_ampdu_stop(ar, params);
6547 		break;
6548 	case IEEE80211_AMPDU_TX_START:
6549 	case IEEE80211_AMPDU_TX_STOP_CONT:
6550 	case IEEE80211_AMPDU_TX_STOP_FLUSH:
6551 	case IEEE80211_AMPDU_TX_STOP_FLUSH_CONT:
6552 	case IEEE80211_AMPDU_TX_OPERATIONAL:
6553 		/* Tx A-MPDU aggregation offloaded to hw/fw so deny mac80211
6554 		 * Tx aggregation requests.
6555 		 */
6556 		ret = -EOPNOTSUPP;
6557 		break;
6558 	}
6559 
6560 	mutex_unlock(&ar->conf_mutex);
6561 
6562 	return ret;
6563 }
6564 
6565 static int ath11k_mac_op_add_chanctx(struct ieee80211_hw *hw,
6566 				     struct ieee80211_chanctx_conf *ctx)
6567 {
6568 	struct ath11k *ar = hw->priv;
6569 	struct ath11k_base *ab = ar->ab;
6570 
6571 	ath11k_dbg(ab, ATH11K_DBG_MAC,
6572 		   "mac chanctx add freq %u width %d ptr %pK\n",
6573 		   ctx->def.chan->center_freq, ctx->def.width, ctx);
6574 
6575 	mutex_lock(&ar->conf_mutex);
6576 
6577 	spin_lock_bh(&ar->data_lock);
6578 	/* TODO: In case of multiple channel context, populate rx_channel from
6579 	 * Rx PPDU desc information.
6580 	 */
6581 	ar->rx_channel = ctx->def.chan;
6582 	spin_unlock_bh(&ar->data_lock);
6583 
6584 	mutex_unlock(&ar->conf_mutex);
6585 
6586 	return 0;
6587 }
6588 
6589 static void ath11k_mac_op_remove_chanctx(struct ieee80211_hw *hw,
6590 					 struct ieee80211_chanctx_conf *ctx)
6591 {
6592 	struct ath11k *ar = hw->priv;
6593 	struct ath11k_base *ab = ar->ab;
6594 
6595 	ath11k_dbg(ab, ATH11K_DBG_MAC,
6596 		   "mac chanctx remove freq %u width %d ptr %pK\n",
6597 		   ctx->def.chan->center_freq, ctx->def.width, ctx);
6598 
6599 	mutex_lock(&ar->conf_mutex);
6600 
6601 	spin_lock_bh(&ar->data_lock);
6602 	/* TODO: In case of there is one more channel context left, populate
6603 	 * rx_channel with the channel of that remaining channel context.
6604 	 */
6605 	ar->rx_channel = NULL;
6606 	spin_unlock_bh(&ar->data_lock);
6607 
6608 	mutex_unlock(&ar->conf_mutex);
6609 }
6610 
6611 static int
6612 ath11k_mac_vdev_start_restart(struct ath11k_vif *arvif,
6613 			      const struct cfg80211_chan_def *chandef,
6614 			      bool restart)
6615 {
6616 	struct ath11k *ar = arvif->ar;
6617 	struct ath11k_base *ab = ar->ab;
6618 	struct wmi_vdev_start_req_arg arg = {};
6619 	int he_support = arvif->vif->bss_conf.he_support;
6620 	int ret = 0;
6621 
6622 	lockdep_assert_held(&ar->conf_mutex);
6623 
6624 	reinit_completion(&ar->vdev_setup_done);
6625 
6626 	arg.vdev_id = arvif->vdev_id;
6627 	arg.dtim_period = arvif->dtim_period;
6628 	arg.bcn_intval = arvif->beacon_interval;
6629 
6630 	arg.channel.freq = chandef->chan->center_freq;
6631 	arg.channel.band_center_freq1 = chandef->center_freq1;
6632 	arg.channel.band_center_freq2 = chandef->center_freq2;
6633 	arg.channel.mode =
6634 		ath11k_phymodes[chandef->chan->band][chandef->width];
6635 
6636 	arg.channel.min_power = 0;
6637 	arg.channel.max_power = chandef->chan->max_power;
6638 	arg.channel.max_reg_power = chandef->chan->max_reg_power;
6639 	arg.channel.max_antenna_gain = chandef->chan->max_antenna_gain;
6640 
6641 	arg.pref_tx_streams = ar->num_tx_chains;
6642 	arg.pref_rx_streams = ar->num_rx_chains;
6643 
6644 	if (arvif->vdev_type == WMI_VDEV_TYPE_AP) {
6645 		arg.ssid = arvif->u.ap.ssid;
6646 		arg.ssid_len = arvif->u.ap.ssid_len;
6647 		arg.hidden_ssid = arvif->u.ap.hidden_ssid;
6648 
6649 		/* For now allow DFS for AP mode */
6650 		arg.channel.chan_radar =
6651 			!!(chandef->chan->flags & IEEE80211_CHAN_RADAR);
6652 
6653 		arg.channel.freq2_radar =
6654 			!!(chandef->chan->flags & IEEE80211_CHAN_RADAR);
6655 
6656 		arg.channel.passive = arg.channel.chan_radar;
6657 
6658 		spin_lock_bh(&ab->base_lock);
6659 		arg.regdomain = ar->ab->dfs_region;
6660 		spin_unlock_bh(&ab->base_lock);
6661 
6662 		if (he_support) {
6663 			ret = ath11k_set_he_mu_sounding_mode(ar, arvif);
6664 			if (ret) {
6665 				ath11k_warn(ar->ab, "failed to set he mode vdev %i\n",
6666 					    arg.vdev_id);
6667 				return ret;
6668 			}
6669 		}
6670 	}
6671 
6672 	arg.channel.passive |= !!(chandef->chan->flags & IEEE80211_CHAN_NO_IR);
6673 
6674 	ath11k_dbg(ab, ATH11K_DBG_MAC,
6675 		   "mac vdev %d start center_freq %d phymode %s\n",
6676 		   arg.vdev_id, arg.channel.freq,
6677 		   ath11k_wmi_phymode_str(arg.channel.mode));
6678 
6679 	ret = ath11k_wmi_vdev_start(ar, &arg, restart);
6680 	if (ret) {
6681 		ath11k_warn(ar->ab, "failed to %s WMI vdev %i\n",
6682 			    restart ? "restart" : "start", arg.vdev_id);
6683 		return ret;
6684 	}
6685 
6686 	ret = ath11k_mac_vdev_setup_sync(ar);
6687 	if (ret) {
6688 		ath11k_warn(ab, "failed to synchronize setup for vdev %i %s: %d\n",
6689 			    arg.vdev_id, restart ? "restart" : "start", ret);
6690 		return ret;
6691 	}
6692 
6693 	if (!restart)
6694 		ar->num_started_vdevs++;
6695 
6696 	ath11k_dbg(ab, ATH11K_DBG_MAC,  "vdev %pM started, vdev_id %d\n",
6697 		   arvif->vif->addr, arvif->vdev_id);
6698 
6699 	/* Enable CAC Flag in the driver by checking the channel DFS cac time,
6700 	 * i.e dfs_cac_ms value which will be valid only for radar channels
6701 	 * and state as NL80211_DFS_USABLE which indicates CAC needs to be
6702 	 * done before channel usage. This flags is used to drop rx packets.
6703 	 * during CAC.
6704 	 */
6705 	/* TODO Set the flag for other interface types as required */
6706 	if (arvif->vdev_type == WMI_VDEV_TYPE_AP &&
6707 	    chandef->chan->dfs_cac_ms &&
6708 	    chandef->chan->dfs_state == NL80211_DFS_USABLE) {
6709 		set_bit(ATH11K_CAC_RUNNING, &ar->dev_flags);
6710 		ath11k_dbg(ab, ATH11K_DBG_MAC,
6711 			   "CAC Started in chan_freq %d for vdev %d\n",
6712 			   arg.channel.freq, arg.vdev_id);
6713 	}
6714 
6715 	ret = ath11k_mac_set_txbf_conf(arvif);
6716 	if (ret)
6717 		ath11k_warn(ab, "failed to set txbf conf for vdev %d: %d\n",
6718 			    arvif->vdev_id, ret);
6719 
6720 	return 0;
6721 }
6722 
6723 static int ath11k_mac_vdev_stop(struct ath11k_vif *arvif)
6724 {
6725 	struct ath11k *ar = arvif->ar;
6726 	int ret;
6727 
6728 	lockdep_assert_held(&ar->conf_mutex);
6729 
6730 	reinit_completion(&ar->vdev_setup_done);
6731 
6732 	ret = ath11k_wmi_vdev_stop(ar, arvif->vdev_id);
6733 	if (ret) {
6734 		ath11k_warn(ar->ab, "failed to stop WMI vdev %i: %d\n",
6735 			    arvif->vdev_id, ret);
6736 		goto err;
6737 	}
6738 
6739 	ret = ath11k_mac_vdev_setup_sync(ar);
6740 	if (ret) {
6741 		ath11k_warn(ar->ab, "failed to synchronize setup for vdev %i: %d\n",
6742 			    arvif->vdev_id, ret);
6743 		goto err;
6744 	}
6745 
6746 	WARN_ON(ar->num_started_vdevs == 0);
6747 
6748 	ar->num_started_vdevs--;
6749 	ath11k_dbg(ar->ab, ATH11K_DBG_MAC, "vdev %pM stopped, vdev_id %d\n",
6750 		   arvif->vif->addr, arvif->vdev_id);
6751 
6752 	if (test_bit(ATH11K_CAC_RUNNING, &ar->dev_flags)) {
6753 		clear_bit(ATH11K_CAC_RUNNING, &ar->dev_flags);
6754 		ath11k_dbg(ar->ab, ATH11K_DBG_MAC, "CAC Stopped for vdev %d\n",
6755 			   arvif->vdev_id);
6756 	}
6757 
6758 	return 0;
6759 err:
6760 	return ret;
6761 }
6762 
6763 static int ath11k_mac_vdev_start(struct ath11k_vif *arvif,
6764 				 const struct cfg80211_chan_def *chandef)
6765 {
6766 	return ath11k_mac_vdev_start_restart(arvif, chandef, false);
6767 }
6768 
6769 static int ath11k_mac_vdev_restart(struct ath11k_vif *arvif,
6770 				   const struct cfg80211_chan_def *chandef)
6771 {
6772 	return ath11k_mac_vdev_start_restart(arvif, chandef, true);
6773 }
6774 
6775 struct ath11k_mac_change_chanctx_arg {
6776 	struct ieee80211_chanctx_conf *ctx;
6777 	struct ieee80211_vif_chanctx_switch *vifs;
6778 	int n_vifs;
6779 	int next_vif;
6780 };
6781 
6782 static void
6783 ath11k_mac_change_chanctx_cnt_iter(void *data, u8 *mac,
6784 				   struct ieee80211_vif *vif)
6785 {
6786 	struct ath11k_mac_change_chanctx_arg *arg = data;
6787 
6788 	if (rcu_access_pointer(vif->chanctx_conf) != arg->ctx)
6789 		return;
6790 
6791 	arg->n_vifs++;
6792 }
6793 
6794 static void
6795 ath11k_mac_change_chanctx_fill_iter(void *data, u8 *mac,
6796 				    struct ieee80211_vif *vif)
6797 {
6798 	struct ath11k_mac_change_chanctx_arg *arg = data;
6799 	struct ieee80211_chanctx_conf *ctx;
6800 
6801 	ctx = rcu_access_pointer(vif->chanctx_conf);
6802 	if (ctx != arg->ctx)
6803 		return;
6804 
6805 	if (WARN_ON(arg->next_vif == arg->n_vifs))
6806 		return;
6807 
6808 	arg->vifs[arg->next_vif].vif = vif;
6809 	arg->vifs[arg->next_vif].old_ctx = ctx;
6810 	arg->vifs[arg->next_vif].new_ctx = ctx;
6811 	arg->next_vif++;
6812 }
6813 
6814 static void
6815 ath11k_mac_update_vif_chan(struct ath11k *ar,
6816 			   struct ieee80211_vif_chanctx_switch *vifs,
6817 			   int n_vifs)
6818 {
6819 	struct ath11k_base *ab = ar->ab;
6820 	struct ath11k_vif *arvif;
6821 	int ret;
6822 	int i;
6823 	bool monitor_vif = false;
6824 
6825 	lockdep_assert_held(&ar->conf_mutex);
6826 
6827 	/* Associated channel resources of all relevant vdevs
6828 	 * should be available for the channel switch now.
6829 	 */
6830 
6831 	/* TODO: Update ar->rx_channel */
6832 
6833 	for (i = 0; i < n_vifs; i++) {
6834 		arvif = (void *)vifs[i].vif->drv_priv;
6835 
6836 		if (WARN_ON(!arvif->is_started))
6837 			continue;
6838 
6839 		if (WARN_ON(!arvif->is_up))
6840 			continue;
6841 
6842 		ret = ath11k_mac_vdev_restart(arvif, &vifs[i].new_ctx->def);
6843 		if (ret) {
6844 			ath11k_warn(ab, "failed to restart vdev %d: %d\n",
6845 				    arvif->vdev_id, ret);
6846 			continue;
6847 		}
6848 
6849 		ret = ath11k_mac_setup_bcn_tmpl(arvif);
6850 		if (ret)
6851 			ath11k_warn(ab, "failed to update bcn tmpl during csa: %d\n",
6852 				    ret);
6853 
6854 		ret = ath11k_wmi_vdev_up(arvif->ar, arvif->vdev_id, arvif->aid,
6855 					 arvif->bssid);
6856 		if (ret) {
6857 			ath11k_warn(ab, "failed to bring vdev up %d: %d\n",
6858 				    arvif->vdev_id, ret);
6859 			continue;
6860 		}
6861 	}
6862 
6863 	/* Restart the internal monitor vdev on new channel */
6864 	if (!monitor_vif &&
6865 	    test_bit(ATH11K_FLAG_MONITOR_VDEV_CREATED, &ar->monitor_flags)) {
6866 		ret = ath11k_mac_monitor_stop(ar);
6867 		if (ret) {
6868 			ath11k_warn(ar->ab, "failed to stop monitor during vif channel update: %d",
6869 				    ret);
6870 			return;
6871 		}
6872 
6873 		ret = ath11k_mac_monitor_start(ar);
6874 		if (ret) {
6875 			ath11k_warn(ar->ab, "failed to start monitor during vif channel update: %d",
6876 				    ret);
6877 			return;
6878 		}
6879 	}
6880 }
6881 
6882 static void
6883 ath11k_mac_update_active_vif_chan(struct ath11k *ar,
6884 				  struct ieee80211_chanctx_conf *ctx)
6885 {
6886 	struct ath11k_mac_change_chanctx_arg arg = { .ctx = ctx };
6887 
6888 	lockdep_assert_held(&ar->conf_mutex);
6889 
6890 	ieee80211_iterate_active_interfaces_atomic(ar->hw,
6891 						   IEEE80211_IFACE_ITER_NORMAL,
6892 						   ath11k_mac_change_chanctx_cnt_iter,
6893 						   &arg);
6894 	if (arg.n_vifs == 0)
6895 		return;
6896 
6897 	arg.vifs = kcalloc(arg.n_vifs, sizeof(arg.vifs[0]), GFP_KERNEL);
6898 	if (!arg.vifs)
6899 		return;
6900 
6901 	ieee80211_iterate_active_interfaces_atomic(ar->hw,
6902 						   IEEE80211_IFACE_ITER_NORMAL,
6903 						   ath11k_mac_change_chanctx_fill_iter,
6904 						   &arg);
6905 
6906 	ath11k_mac_update_vif_chan(ar, arg.vifs, arg.n_vifs);
6907 
6908 	kfree(arg.vifs);
6909 }
6910 
6911 static void ath11k_mac_op_change_chanctx(struct ieee80211_hw *hw,
6912 					 struct ieee80211_chanctx_conf *ctx,
6913 					 u32 changed)
6914 {
6915 	struct ath11k *ar = hw->priv;
6916 	struct ath11k_base *ab = ar->ab;
6917 
6918 	mutex_lock(&ar->conf_mutex);
6919 
6920 	ath11k_dbg(ab, ATH11K_DBG_MAC,
6921 		   "mac chanctx change freq %u width %d ptr %pK changed %x\n",
6922 		   ctx->def.chan->center_freq, ctx->def.width, ctx, changed);
6923 
6924 	/* This shouldn't really happen because channel switching should use
6925 	 * switch_vif_chanctx().
6926 	 */
6927 	if (WARN_ON(changed & IEEE80211_CHANCTX_CHANGE_CHANNEL))
6928 		goto unlock;
6929 
6930 	if (changed & IEEE80211_CHANCTX_CHANGE_WIDTH)
6931 		ath11k_mac_update_active_vif_chan(ar, ctx);
6932 
6933 	/* TODO: Recalc radar detection */
6934 
6935 unlock:
6936 	mutex_unlock(&ar->conf_mutex);
6937 }
6938 
6939 static int ath11k_start_vdev_delay(struct ieee80211_hw *hw,
6940 				   struct ieee80211_vif *vif)
6941 {
6942 	struct ath11k *ar = hw->priv;
6943 	struct ath11k_base *ab = ar->ab;
6944 	struct ath11k_vif *arvif = (void *)vif->drv_priv;
6945 	int ret;
6946 
6947 	if (WARN_ON(arvif->is_started))
6948 		return -EBUSY;
6949 
6950 	ret = ath11k_mac_vdev_start(arvif, &arvif->chanctx.def);
6951 	if (ret) {
6952 		ath11k_warn(ab, "failed to start vdev %i addr %pM on freq %d: %d\n",
6953 			    arvif->vdev_id, vif->addr,
6954 			    arvif->chanctx.def.chan->center_freq, ret);
6955 		return ret;
6956 	}
6957 
6958 	if (arvif->vdev_type == WMI_VDEV_TYPE_MONITOR) {
6959 		ret = ath11k_wmi_vdev_up(ar, arvif->vdev_id, 0, ar->mac_addr);
6960 		if (ret) {
6961 			ath11k_warn(ab, "failed put monitor up: %d\n", ret);
6962 			return ret;
6963 		}
6964 	}
6965 
6966 	arvif->is_started = true;
6967 
6968 	/* TODO: Setup ps and cts/rts protection */
6969 	return 0;
6970 }
6971 
6972 static int
6973 ath11k_mac_op_assign_vif_chanctx(struct ieee80211_hw *hw,
6974 				 struct ieee80211_vif *vif,
6975 				 struct ieee80211_chanctx_conf *ctx)
6976 {
6977 	struct ath11k *ar = hw->priv;
6978 	struct ath11k_base *ab = ar->ab;
6979 	struct ath11k_vif *arvif = (void *)vif->drv_priv;
6980 	int ret;
6981 	struct peer_create_params param;
6982 
6983 	mutex_lock(&ar->conf_mutex);
6984 
6985 	ath11k_dbg(ab, ATH11K_DBG_MAC,
6986 		   "mac chanctx assign ptr %pK vdev_id %i\n",
6987 		   ctx, arvif->vdev_id);
6988 
6989 	/* for QCA6390 bss peer must be created before vdev_start */
6990 	if (ab->hw_params.vdev_start_delay &&
6991 	    arvif->vdev_type != WMI_VDEV_TYPE_AP &&
6992 	    arvif->vdev_type != WMI_VDEV_TYPE_MONITOR &&
6993 	    !ath11k_peer_find_by_vdev_id(ab, arvif->vdev_id)) {
6994 		memcpy(&arvif->chanctx, ctx, sizeof(*ctx));
6995 		ret = 0;
6996 		goto out;
6997 	}
6998 
6999 	if (WARN_ON(arvif->is_started)) {
7000 		ret = -EBUSY;
7001 		goto out;
7002 	}
7003 
7004 	if (ab->hw_params.vdev_start_delay &&
7005 	    arvif->vdev_type != WMI_VDEV_TYPE_AP &&
7006 	    arvif->vdev_type != WMI_VDEV_TYPE_MONITOR) {
7007 		param.vdev_id = arvif->vdev_id;
7008 		param.peer_type = WMI_PEER_TYPE_DEFAULT;
7009 		param.peer_addr = ar->mac_addr;
7010 
7011 		ret = ath11k_peer_create(ar, arvif, NULL, &param);
7012 		if (ret) {
7013 			ath11k_warn(ab, "failed to create peer after vdev start delay: %d",
7014 				    ret);
7015 			goto out;
7016 		}
7017 	}
7018 
7019 	if (arvif->vdev_type == WMI_VDEV_TYPE_MONITOR) {
7020 		ret = ath11k_mac_monitor_start(ar);
7021 		if (ret) {
7022 			ath11k_warn(ar->ab, "failed to start monitor during vif channel context assignment: %d",
7023 				    ret);
7024 			goto out;
7025 		}
7026 
7027 		arvif->is_started = true;
7028 		goto out;
7029 	}
7030 
7031 	ret = ath11k_mac_vdev_start(arvif, &ctx->def);
7032 	if (ret) {
7033 		ath11k_warn(ab, "failed to start vdev %i addr %pM on freq %d: %d\n",
7034 			    arvif->vdev_id, vif->addr,
7035 			    ctx->def.chan->center_freq, ret);
7036 		goto out;
7037 	}
7038 
7039 	arvif->is_started = true;
7040 
7041 	if (arvif->vdev_type != WMI_VDEV_TYPE_MONITOR &&
7042 	    test_bit(ATH11K_FLAG_MONITOR_VDEV_CREATED, &ar->monitor_flags)) {
7043 		ret = ath11k_mac_monitor_start(ar);
7044 		if (ret) {
7045 			ath11k_warn(ar->ab, "failed to start monitor during vif channel context assignment: %d",
7046 				    ret);
7047 			goto out;
7048 		}
7049 	}
7050 
7051 	/* TODO: Setup ps and cts/rts protection */
7052 
7053 	ret = 0;
7054 
7055 out:
7056 	mutex_unlock(&ar->conf_mutex);
7057 
7058 	return ret;
7059 }
7060 
7061 static void
7062 ath11k_mac_op_unassign_vif_chanctx(struct ieee80211_hw *hw,
7063 				   struct ieee80211_vif *vif,
7064 				   struct ieee80211_chanctx_conf *ctx)
7065 {
7066 	struct ath11k *ar = hw->priv;
7067 	struct ath11k_base *ab = ar->ab;
7068 	struct ath11k_vif *arvif = (void *)vif->drv_priv;
7069 	int ret;
7070 
7071 	mutex_lock(&ar->conf_mutex);
7072 
7073 	ath11k_dbg(ab, ATH11K_DBG_MAC,
7074 		   "mac chanctx unassign ptr %pK vdev_id %i\n",
7075 		   ctx, arvif->vdev_id);
7076 
7077 	WARN_ON(!arvif->is_started);
7078 
7079 	if (ab->hw_params.vdev_start_delay &&
7080 	    arvif->vdev_type == WMI_VDEV_TYPE_MONITOR &&
7081 	    ath11k_peer_find_by_addr(ab, ar->mac_addr))
7082 		ath11k_peer_delete(ar, arvif->vdev_id, ar->mac_addr);
7083 
7084 	if (arvif->vdev_type == WMI_VDEV_TYPE_MONITOR) {
7085 		ret = ath11k_mac_monitor_stop(ar);
7086 		if (ret) {
7087 			ath11k_warn(ar->ab, "failed to stop monitor during vif channel context unassignment: %d",
7088 				    ret);
7089 			mutex_unlock(&ar->conf_mutex);
7090 			return;
7091 		}
7092 
7093 		arvif->is_started = false;
7094 		mutex_unlock(&ar->conf_mutex);
7095 		return;
7096 	}
7097 
7098 	ret = ath11k_mac_vdev_stop(arvif);
7099 	if (ret)
7100 		ath11k_warn(ab, "failed to stop vdev %i: %d\n",
7101 			    arvif->vdev_id, ret);
7102 
7103 	arvif->is_started = false;
7104 
7105 	if (ab->hw_params.vdev_start_delay &&
7106 	    arvif->vdev_type == WMI_VDEV_TYPE_STA) {
7107 		ret = ath11k_peer_delete(ar, arvif->vdev_id, arvif->bssid);
7108 		if (ret)
7109 			ath11k_warn(ar->ab,
7110 				    "failed to delete peer %pM for vdev %d: %d\n",
7111 				    arvif->bssid, arvif->vdev_id, ret);
7112 		else
7113 			ath11k_dbg(ar->ab, ATH11K_DBG_MAC,
7114 				   "mac removed peer %pM  vdev %d after vdev stop\n",
7115 				   arvif->bssid, arvif->vdev_id);
7116 	}
7117 
7118 	if (ab->hw_params.vdev_start_delay &&
7119 	    arvif->vdev_type == WMI_VDEV_TYPE_MONITOR)
7120 		ath11k_wmi_vdev_down(ar, arvif->vdev_id);
7121 
7122 	if (arvif->vdev_type != WMI_VDEV_TYPE_MONITOR &&
7123 	    ar->num_started_vdevs == 1 &&
7124 	    test_bit(ATH11K_FLAG_MONITOR_VDEV_CREATED, &ar->monitor_flags)) {
7125 		ret = ath11k_mac_monitor_stop(ar);
7126 		if (ret)
7127 			/* continue even if there's an error */
7128 			ath11k_warn(ar->ab, "failed to stop monitor during vif channel context unassignment: %d",
7129 				    ret);
7130 	}
7131 
7132 	if (arvif->vdev_type == WMI_VDEV_TYPE_STA)
7133 		ath11k_mac_11d_scan_start(ar, arvif->vdev_id, false);
7134 
7135 	mutex_unlock(&ar->conf_mutex);
7136 }
7137 
7138 static int
7139 ath11k_mac_op_switch_vif_chanctx(struct ieee80211_hw *hw,
7140 				 struct ieee80211_vif_chanctx_switch *vifs,
7141 				 int n_vifs,
7142 				 enum ieee80211_chanctx_switch_mode mode)
7143 {
7144 	struct ath11k *ar = hw->priv;
7145 
7146 	mutex_lock(&ar->conf_mutex);
7147 
7148 	ath11k_dbg(ar->ab, ATH11K_DBG_MAC,
7149 		   "mac chanctx switch n_vifs %d mode %d\n",
7150 		   n_vifs, mode);
7151 	ath11k_mac_update_vif_chan(ar, vifs, n_vifs);
7152 
7153 	mutex_unlock(&ar->conf_mutex);
7154 
7155 	return 0;
7156 }
7157 
7158 static int
7159 ath11k_set_vdev_param_to_all_vifs(struct ath11k *ar, int param, u32 value)
7160 {
7161 	struct ath11k_vif *arvif;
7162 	int ret = 0;
7163 
7164 	mutex_lock(&ar->conf_mutex);
7165 	list_for_each_entry(arvif, &ar->arvifs, list) {
7166 		ath11k_dbg(ar->ab, ATH11K_DBG_MAC, "setting mac vdev %d param %d value %d\n",
7167 			   param, arvif->vdev_id, value);
7168 
7169 		ret = ath11k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
7170 						    param, value);
7171 		if (ret) {
7172 			ath11k_warn(ar->ab, "failed to set param %d for vdev %d: %d\n",
7173 				    param, arvif->vdev_id, ret);
7174 			break;
7175 		}
7176 	}
7177 	mutex_unlock(&ar->conf_mutex);
7178 	return ret;
7179 }
7180 
7181 /* mac80211 stores device specific RTS/Fragmentation threshold value,
7182  * this is set interface specific to firmware from ath11k driver
7183  */
7184 static int ath11k_mac_op_set_rts_threshold(struct ieee80211_hw *hw, u32 value)
7185 {
7186 	struct ath11k *ar = hw->priv;
7187 	int param_id = WMI_VDEV_PARAM_RTS_THRESHOLD;
7188 
7189 	return ath11k_set_vdev_param_to_all_vifs(ar, param_id, value);
7190 }
7191 
7192 static int ath11k_mac_op_set_frag_threshold(struct ieee80211_hw *hw, u32 value)
7193 {
7194 	/* Even though there's a WMI vdev param for fragmentation threshold no
7195 	 * known firmware actually implements it. Moreover it is not possible to
7196 	 * rely frame fragmentation to mac80211 because firmware clears the
7197 	 * "more fragments" bit in frame control making it impossible for remote
7198 	 * devices to reassemble frames.
7199 	 *
7200 	 * Hence implement a dummy callback just to say fragmentation isn't
7201 	 * supported. This effectively prevents mac80211 from doing frame
7202 	 * fragmentation in software.
7203 	 */
7204 	return -EOPNOTSUPP;
7205 }
7206 
7207 static void ath11k_mac_op_flush(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
7208 				u32 queues, bool drop)
7209 {
7210 	struct ath11k *ar = hw->priv;
7211 	long time_left;
7212 
7213 	if (drop)
7214 		return;
7215 
7216 	time_left = wait_event_timeout(ar->dp.tx_empty_waitq,
7217 				       (atomic_read(&ar->dp.num_tx_pending) == 0),
7218 				       ATH11K_FLUSH_TIMEOUT);
7219 	if (time_left == 0)
7220 		ath11k_warn(ar->ab, "failed to flush transmit queue %ld\n", time_left);
7221 
7222 	time_left = wait_event_timeout(ar->txmgmt_empty_waitq,
7223 				       (atomic_read(&ar->num_pending_mgmt_tx) == 0),
7224 				       ATH11K_FLUSH_TIMEOUT);
7225 	if (time_left == 0)
7226 		ath11k_warn(ar->ab, "failed to flush mgmt transmit queue %ld\n",
7227 			    time_left);
7228 
7229 	ath11k_dbg(ar->ab, ATH11K_DBG_MAC,
7230 		   "mac mgmt tx flush mgmt pending %d\n",
7231 		   atomic_read(&ar->num_pending_mgmt_tx));
7232 }
7233 
7234 static int
7235 ath11k_mac_bitrate_mask_num_ht_rates(struct ath11k *ar,
7236 				     enum nl80211_band band,
7237 				     const struct cfg80211_bitrate_mask *mask)
7238 {
7239 	int num_rates = 0;
7240 	int i;
7241 
7242 	for (i = 0; i < ARRAY_SIZE(mask->control[band].ht_mcs); i++)
7243 		num_rates += hweight16(mask->control[band].ht_mcs[i]);
7244 
7245 	return num_rates;
7246 }
7247 
7248 static bool
7249 ath11k_mac_has_single_legacy_rate(struct ath11k *ar,
7250 				  enum nl80211_band band,
7251 				  const struct cfg80211_bitrate_mask *mask)
7252 {
7253 	int num_rates = 0;
7254 
7255 	num_rates = hweight32(mask->control[band].legacy);
7256 
7257 	if (ath11k_mac_bitrate_mask_num_ht_rates(ar, band, mask))
7258 		return false;
7259 
7260 	if (ath11k_mac_bitrate_mask_num_vht_rates(ar, band, mask))
7261 		return false;
7262 
7263 	if (ath11k_mac_bitrate_mask_num_he_rates(ar, band, mask))
7264 		return false;
7265 
7266 	return num_rates == 1;
7267 }
7268 
7269 static __le16
7270 ath11k_mac_get_tx_mcs_map(const struct ieee80211_sta_he_cap *he_cap)
7271 {
7272 	if (he_cap->he_cap_elem.phy_cap_info[0] &
7273 	    IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_80PLUS80_MHZ_IN_5G)
7274 		return he_cap->he_mcs_nss_supp.tx_mcs_80p80;
7275 
7276 	if (he_cap->he_cap_elem.phy_cap_info[0] &
7277 	    IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_160MHZ_IN_5G)
7278 		return he_cap->he_mcs_nss_supp.tx_mcs_160;
7279 
7280 	return he_cap->he_mcs_nss_supp.tx_mcs_80;
7281 }
7282 
7283 static bool
7284 ath11k_mac_bitrate_mask_get_single_nss(struct ath11k *ar,
7285 				       enum nl80211_band band,
7286 				       const struct cfg80211_bitrate_mask *mask,
7287 				       int *nss)
7288 {
7289 	struct ieee80211_supported_band *sband = &ar->mac.sbands[band];
7290 	u16 vht_mcs_map = le16_to_cpu(sband->vht_cap.vht_mcs.tx_mcs_map);
7291 	u16 he_mcs_map = 0;
7292 	u8 ht_nss_mask = 0;
7293 	u8 vht_nss_mask = 0;
7294 	u8 he_nss_mask = 0;
7295 	int i;
7296 
7297 	/* No need to consider legacy here. Basic rates are always present
7298 	 * in bitrate mask
7299 	 */
7300 
7301 	for (i = 0; i < ARRAY_SIZE(mask->control[band].ht_mcs); i++) {
7302 		if (mask->control[band].ht_mcs[i] == 0)
7303 			continue;
7304 		else if (mask->control[band].ht_mcs[i] ==
7305 			 sband->ht_cap.mcs.rx_mask[i])
7306 			ht_nss_mask |= BIT(i);
7307 		else
7308 			return false;
7309 	}
7310 
7311 	for (i = 0; i < ARRAY_SIZE(mask->control[band].vht_mcs); i++) {
7312 		if (mask->control[band].vht_mcs[i] == 0)
7313 			continue;
7314 		else if (mask->control[band].vht_mcs[i] ==
7315 			 ath11k_mac_get_max_vht_mcs_map(vht_mcs_map, i))
7316 			vht_nss_mask |= BIT(i);
7317 		else
7318 			return false;
7319 	}
7320 
7321 	he_mcs_map = le16_to_cpu(ath11k_mac_get_tx_mcs_map(&sband->iftype_data->he_cap));
7322 
7323 	for (i = 0; i < ARRAY_SIZE(mask->control[band].he_mcs); i++) {
7324 		if (mask->control[band].he_mcs[i] == 0)
7325 			continue;
7326 
7327 		if (mask->control[band].he_mcs[i] ==
7328 		    ath11k_mac_get_max_he_mcs_map(he_mcs_map, i))
7329 			he_nss_mask |= BIT(i);
7330 		else
7331 			return false;
7332 	}
7333 
7334 	if (ht_nss_mask != vht_nss_mask || ht_nss_mask != he_nss_mask)
7335 		return false;
7336 
7337 	if (ht_nss_mask == 0)
7338 		return false;
7339 
7340 	if (BIT(fls(ht_nss_mask)) - 1 != ht_nss_mask)
7341 		return false;
7342 
7343 	*nss = fls(ht_nss_mask);
7344 
7345 	return true;
7346 }
7347 
7348 static int
7349 ath11k_mac_get_single_legacy_rate(struct ath11k *ar,
7350 				  enum nl80211_band band,
7351 				  const struct cfg80211_bitrate_mask *mask,
7352 				  u32 *rate, u8 *nss)
7353 {
7354 	int rate_idx;
7355 	u16 bitrate;
7356 	u8 preamble;
7357 	u8 hw_rate;
7358 
7359 	if (hweight32(mask->control[band].legacy) != 1)
7360 		return -EINVAL;
7361 
7362 	rate_idx = ffs(mask->control[band].legacy) - 1;
7363 
7364 	if (band == NL80211_BAND_5GHZ || band == NL80211_BAND_6GHZ)
7365 		rate_idx += ATH11K_MAC_FIRST_OFDM_RATE_IDX;
7366 
7367 	hw_rate = ath11k_legacy_rates[rate_idx].hw_value;
7368 	bitrate = ath11k_legacy_rates[rate_idx].bitrate;
7369 
7370 	if (ath11k_mac_bitrate_is_cck(bitrate))
7371 		preamble = WMI_RATE_PREAMBLE_CCK;
7372 	else
7373 		preamble = WMI_RATE_PREAMBLE_OFDM;
7374 
7375 	*nss = 1;
7376 	*rate = ATH11K_HW_RATE_CODE(hw_rate, 0, preamble);
7377 
7378 	return 0;
7379 }
7380 
7381 static int
7382 ath11k_mac_set_fixed_rate_gi_ltf(struct ath11k_vif *arvif, u8 he_gi, u8 he_ltf)
7383 {
7384 	struct ath11k *ar = arvif->ar;
7385 	int ret;
7386 
7387 	/* 0.8 = 0, 1.6 = 2 and 3.2 = 3. */
7388 	if (he_gi && he_gi != 0xFF)
7389 		he_gi += 1;
7390 
7391 	ret = ath11k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
7392 					    WMI_VDEV_PARAM_SGI, he_gi);
7393 	if (ret) {
7394 		ath11k_warn(ar->ab, "failed to set he gi %d: %d\n",
7395 			    he_gi, ret);
7396 		return ret;
7397 	}
7398 	/* start from 1 */
7399 	if (he_ltf != 0xFF)
7400 		he_ltf += 1;
7401 
7402 	ret = ath11k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
7403 					    WMI_VDEV_PARAM_HE_LTF, he_ltf);
7404 	if (ret) {
7405 		ath11k_warn(ar->ab, "failed to set he ltf %d: %d\n",
7406 			    he_ltf, ret);
7407 		return ret;
7408 	}
7409 
7410 	return 0;
7411 }
7412 
7413 static int
7414 ath11k_mac_set_auto_rate_gi_ltf(struct ath11k_vif *arvif, u16 he_gi, u8 he_ltf)
7415 {
7416 	struct ath11k *ar = arvif->ar;
7417 	int ret;
7418 	u32 he_ar_gi_ltf;
7419 
7420 	if (he_gi != 0xFF) {
7421 		switch (he_gi) {
7422 		case NL80211_RATE_INFO_HE_GI_0_8:
7423 			he_gi = WMI_AUTORATE_800NS_GI;
7424 			break;
7425 		case NL80211_RATE_INFO_HE_GI_1_6:
7426 			he_gi = WMI_AUTORATE_1600NS_GI;
7427 			break;
7428 		case NL80211_RATE_INFO_HE_GI_3_2:
7429 			he_gi = WMI_AUTORATE_3200NS_GI;
7430 			break;
7431 		default:
7432 			ath11k_warn(ar->ab, "invalid he gi: %d\n", he_gi);
7433 			return -EINVAL;
7434 		}
7435 	}
7436 
7437 	if (he_ltf != 0xFF) {
7438 		switch (he_ltf) {
7439 		case NL80211_RATE_INFO_HE_1XLTF:
7440 			he_ltf = WMI_HE_AUTORATE_LTF_1X;
7441 			break;
7442 		case NL80211_RATE_INFO_HE_2XLTF:
7443 			he_ltf = WMI_HE_AUTORATE_LTF_2X;
7444 			break;
7445 		case NL80211_RATE_INFO_HE_4XLTF:
7446 			he_ltf = WMI_HE_AUTORATE_LTF_4X;
7447 			break;
7448 		default:
7449 			ath11k_warn(ar->ab, "invalid he ltf: %d\n", he_ltf);
7450 			return -EINVAL;
7451 		}
7452 	}
7453 
7454 	he_ar_gi_ltf = he_gi | he_ltf;
7455 	ret = ath11k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
7456 					    WMI_VDEV_PARAM_AUTORATE_MISC_CFG,
7457 					    he_ar_gi_ltf);
7458 	if (ret) {
7459 		ath11k_warn(ar->ab,
7460 			    "failed to set he autorate gi %u ltf %u: %d\n",
7461 			    he_gi, he_ltf, ret);
7462 		return ret;
7463 	}
7464 
7465 	return 0;
7466 }
7467 
7468 static int ath11k_mac_set_rate_params(struct ath11k_vif *arvif,
7469 				      u32 rate, u8 nss, u8 sgi, u8 ldpc,
7470 				      u8 he_gi, u8 he_ltf, bool he_fixed_rate)
7471 {
7472 	struct ath11k *ar = arvif->ar;
7473 	u32 vdev_param;
7474 	int ret;
7475 
7476 	lockdep_assert_held(&ar->conf_mutex);
7477 
7478 	ath11k_dbg(ar->ab, ATH11K_DBG_MAC,
7479 		   "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",
7480 		   arvif->vdev_id, rate, nss, sgi, ldpc, he_gi,
7481 		   he_ltf, he_fixed_rate);
7482 
7483 	if (!arvif->vif->bss_conf.he_support) {
7484 		vdev_param = WMI_VDEV_PARAM_FIXED_RATE;
7485 		ret = ath11k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
7486 						    vdev_param, rate);
7487 		if (ret) {
7488 			ath11k_warn(ar->ab, "failed to set fixed rate param 0x%02x: %d\n",
7489 				    rate, ret);
7490 			return ret;
7491 		}
7492 	}
7493 
7494 	vdev_param = WMI_VDEV_PARAM_NSS;
7495 	ret = ath11k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
7496 					    vdev_param, nss);
7497 	if (ret) {
7498 		ath11k_warn(ar->ab, "failed to set nss param %d: %d\n",
7499 			    nss, ret);
7500 		return ret;
7501 	}
7502 
7503 	vdev_param = WMI_VDEV_PARAM_LDPC;
7504 	ret = ath11k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
7505 					    vdev_param, ldpc);
7506 	if (ret) {
7507 		ath11k_warn(ar->ab, "failed to set ldpc param %d: %d\n",
7508 			    ldpc, ret);
7509 		return ret;
7510 	}
7511 
7512 	if (arvif->vif->bss_conf.he_support) {
7513 		if (he_fixed_rate) {
7514 			ret = ath11k_mac_set_fixed_rate_gi_ltf(arvif, he_gi,
7515 							       he_ltf);
7516 			if (ret) {
7517 				ath11k_warn(ar->ab, "failed to set fixed rate gi ltf: %d\n",
7518 					    ret);
7519 				return ret;
7520 			}
7521 		} else {
7522 			ret = ath11k_mac_set_auto_rate_gi_ltf(arvif, he_gi,
7523 							      he_ltf);
7524 			if (ret) {
7525 				ath11k_warn(ar->ab, "failed to set auto rate gi ltf: %d\n",
7526 					    ret);
7527 				return ret;
7528 			}
7529 		}
7530 	} else {
7531 		vdev_param = WMI_VDEV_PARAM_SGI;
7532 		ret = ath11k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
7533 						    vdev_param, sgi);
7534 		if (ret) {
7535 			ath11k_warn(ar->ab, "failed to set sgi param %d: %d\n",
7536 				    sgi, ret);
7537 			return ret;
7538 		}
7539 	}
7540 
7541 	return 0;
7542 }
7543 
7544 static bool
7545 ath11k_mac_vht_mcs_range_present(struct ath11k *ar,
7546 				 enum nl80211_band band,
7547 				 const struct cfg80211_bitrate_mask *mask)
7548 {
7549 	int i;
7550 	u16 vht_mcs;
7551 
7552 	for (i = 0; i < NL80211_VHT_NSS_MAX; i++) {
7553 		vht_mcs = mask->control[band].vht_mcs[i];
7554 
7555 		switch (vht_mcs) {
7556 		case 0:
7557 		case BIT(8) - 1:
7558 		case BIT(9) - 1:
7559 		case BIT(10) - 1:
7560 			break;
7561 		default:
7562 			return false;
7563 		}
7564 	}
7565 
7566 	return true;
7567 }
7568 
7569 static bool
7570 ath11k_mac_he_mcs_range_present(struct ath11k *ar,
7571 				enum nl80211_band band,
7572 				const struct cfg80211_bitrate_mask *mask)
7573 {
7574 	int i;
7575 	u16 he_mcs;
7576 
7577 	for (i = 0; i < NL80211_HE_NSS_MAX; i++) {
7578 		he_mcs = mask->control[band].he_mcs[i];
7579 
7580 		switch (he_mcs) {
7581 		case 0:
7582 		case BIT(8) - 1:
7583 		case BIT(10) - 1:
7584 		case BIT(12) - 1:
7585 			break;
7586 		default:
7587 			return false;
7588 		}
7589 	}
7590 
7591 	return true;
7592 }
7593 
7594 static void ath11k_mac_set_bitrate_mask_iter(void *data,
7595 					     struct ieee80211_sta *sta)
7596 {
7597 	struct ath11k_vif *arvif = data;
7598 	struct ath11k_sta *arsta = (struct ath11k_sta *)sta->drv_priv;
7599 	struct ath11k *ar = arvif->ar;
7600 
7601 	spin_lock_bh(&ar->data_lock);
7602 	arsta->changed |= IEEE80211_RC_SUPP_RATES_CHANGED;
7603 	spin_unlock_bh(&ar->data_lock);
7604 
7605 	ieee80211_queue_work(ar->hw, &arsta->update_wk);
7606 }
7607 
7608 static void ath11k_mac_disable_peer_fixed_rate(void *data,
7609 					       struct ieee80211_sta *sta)
7610 {
7611 	struct ath11k_vif *arvif = data;
7612 	struct ath11k *ar = arvif->ar;
7613 	int ret;
7614 
7615 	ret = ath11k_wmi_set_peer_param(ar, sta->addr,
7616 					arvif->vdev_id,
7617 					WMI_PEER_PARAM_FIXED_RATE,
7618 					WMI_FIXED_RATE_NONE);
7619 	if (ret)
7620 		ath11k_warn(ar->ab,
7621 			    "failed to disable peer fixed rate for STA %pM ret %d\n",
7622 			    sta->addr, ret);
7623 }
7624 
7625 static bool
7626 ath11k_mac_validate_vht_he_fixed_rate_settings(struct ath11k *ar, enum nl80211_band band,
7627 					       const struct cfg80211_bitrate_mask *mask)
7628 {
7629 	bool he_fixed_rate = false, vht_fixed_rate = false;
7630 	struct ath11k_peer *peer, *tmp;
7631 	const u16 *vht_mcs_mask, *he_mcs_mask;
7632 	u8 vht_nss, he_nss;
7633 	bool ret = true;
7634 
7635 	vht_mcs_mask = mask->control[band].vht_mcs;
7636 	he_mcs_mask = mask->control[band].he_mcs;
7637 
7638 	if (ath11k_mac_bitrate_mask_num_vht_rates(ar, band, mask) == 1)
7639 		vht_fixed_rate = true;
7640 
7641 	if (ath11k_mac_bitrate_mask_num_he_rates(ar, band, mask) == 1)
7642 		he_fixed_rate = true;
7643 
7644 	if (!vht_fixed_rate && !he_fixed_rate)
7645 		return true;
7646 
7647 	vht_nss = ath11k_mac_max_vht_nss(vht_mcs_mask);
7648 	he_nss =  ath11k_mac_max_he_nss(he_mcs_mask);
7649 
7650 	rcu_read_lock();
7651 	spin_lock_bh(&ar->ab->base_lock);
7652 	list_for_each_entry_safe(peer, tmp, &ar->ab->peers, list) {
7653 		if (peer->sta) {
7654 			if (vht_fixed_rate && (!peer->sta->vht_cap.vht_supported ||
7655 					       peer->sta->rx_nss < vht_nss)) {
7656 				ret = false;
7657 				goto out;
7658 			}
7659 			if (he_fixed_rate && (!peer->sta->he_cap.has_he ||
7660 					      peer->sta->rx_nss < he_nss)) {
7661 				ret = false;
7662 				goto out;
7663 			}
7664 		}
7665 	}
7666 
7667 out:
7668 	spin_unlock_bh(&ar->ab->base_lock);
7669 	rcu_read_unlock();
7670 	return ret;
7671 }
7672 
7673 static int
7674 ath11k_mac_op_set_bitrate_mask(struct ieee80211_hw *hw,
7675 			       struct ieee80211_vif *vif,
7676 			       const struct cfg80211_bitrate_mask *mask)
7677 {
7678 	struct ath11k_vif *arvif = (void *)vif->drv_priv;
7679 	struct cfg80211_chan_def def;
7680 	struct ath11k *ar = arvif->ar;
7681 	enum nl80211_band band;
7682 	const u8 *ht_mcs_mask;
7683 	const u16 *vht_mcs_mask;
7684 	const u16 *he_mcs_mask;
7685 	u8 he_ltf = 0;
7686 	u8 he_gi = 0;
7687 	u32 rate;
7688 	u8 nss;
7689 	u8 sgi;
7690 	u8 ldpc;
7691 	int single_nss;
7692 	int ret;
7693 	int num_rates;
7694 	bool he_fixed_rate = false;
7695 
7696 	if (ath11k_mac_vif_chan(vif, &def))
7697 		return -EPERM;
7698 
7699 	band = def.chan->band;
7700 	ht_mcs_mask = mask->control[band].ht_mcs;
7701 	vht_mcs_mask = mask->control[band].vht_mcs;
7702 	he_mcs_mask = mask->control[band].he_mcs;
7703 	ldpc = !!(ar->ht_cap_info & WMI_HT_CAP_LDPC);
7704 
7705 	sgi = mask->control[band].gi;
7706 	if (sgi == NL80211_TXRATE_FORCE_LGI)
7707 		return -EINVAL;
7708 
7709 	he_gi = mask->control[band].he_gi;
7710 	he_ltf = mask->control[band].he_ltf;
7711 
7712 	/* mac80211 doesn't support sending a fixed HT/VHT MCS alone, rather it
7713 	 * requires passing atleast one of used basic rates along with them.
7714 	 * Fixed rate setting across different preambles(legacy, HT, VHT) is
7715 	 * not supported by the FW. Hence use of FIXED_RATE vdev param is not
7716 	 * suitable for setting single HT/VHT rates.
7717 	 * But, there could be a single basic rate passed from userspace which
7718 	 * can be done through the FIXED_RATE param.
7719 	 */
7720 	if (ath11k_mac_has_single_legacy_rate(ar, band, mask)) {
7721 		ret = ath11k_mac_get_single_legacy_rate(ar, band, mask, &rate,
7722 							&nss);
7723 		if (ret) {
7724 			ath11k_warn(ar->ab, "failed to get single legacy rate for vdev %i: %d\n",
7725 				    arvif->vdev_id, ret);
7726 			return ret;
7727 		}
7728 		ieee80211_iterate_stations_atomic(ar->hw,
7729 						  ath11k_mac_disable_peer_fixed_rate,
7730 						  arvif);
7731 	} else if (ath11k_mac_bitrate_mask_get_single_nss(ar, band, mask,
7732 							  &single_nss)) {
7733 		rate = WMI_FIXED_RATE_NONE;
7734 		nss = single_nss;
7735 		mutex_lock(&ar->conf_mutex);
7736 		arvif->bitrate_mask = *mask;
7737 		ieee80211_iterate_stations_atomic(ar->hw,
7738 						  ath11k_mac_set_bitrate_mask_iter,
7739 						  arvif);
7740 		mutex_unlock(&ar->conf_mutex);
7741 	} else {
7742 		rate = WMI_FIXED_RATE_NONE;
7743 
7744 		if (!ath11k_mac_validate_vht_he_fixed_rate_settings(ar, band, mask))
7745 			ath11k_warn(ar->ab,
7746 				    "could not update fixed rate settings to all peers due to mcs/nss incompatibility\n");
7747 		nss = min_t(u32, ar->num_tx_chains,
7748 			    max(max(ath11k_mac_max_ht_nss(ht_mcs_mask),
7749 				    ath11k_mac_max_vht_nss(vht_mcs_mask)),
7750 				ath11k_mac_max_he_nss(he_mcs_mask)));
7751 
7752 		/* If multiple rates across different preambles are given
7753 		 * we can reconfigure this info with all peers using PEER_ASSOC
7754 		 * command with the below exception cases.
7755 		 * - Single VHT Rate : peer_assoc command accommodates only MCS
7756 		 * range values i.e 0-7, 0-8, 0-9 for VHT. Though mac80211
7757 		 * mandates passing basic rates along with HT/VHT rates, FW
7758 		 * doesn't allow switching from VHT to Legacy. Hence instead of
7759 		 * setting legacy and VHT rates using RATEMASK_CMD vdev cmd,
7760 		 * we could set this VHT rate as peer fixed rate param, which
7761 		 * will override FIXED rate and FW rate control algorithm.
7762 		 * If single VHT rate is passed along with HT rates, we select
7763 		 * the VHT rate as fixed rate for vht peers.
7764 		 * - Multiple VHT Rates : When Multiple VHT rates are given,this
7765 		 * can be set using RATEMASK CMD which uses FW rate-ctl alg.
7766 		 * TODO: Setting multiple VHT MCS and replacing peer_assoc with
7767 		 * RATEMASK_CMDID can cover all use cases of setting rates
7768 		 * across multiple preambles and rates within same type.
7769 		 * But requires more validation of the command at this point.
7770 		 */
7771 
7772 		num_rates = ath11k_mac_bitrate_mask_num_vht_rates(ar, band,
7773 								  mask);
7774 
7775 		if (!ath11k_mac_vht_mcs_range_present(ar, band, mask) &&
7776 		    num_rates > 1) {
7777 			/* TODO: Handle multiple VHT MCS values setting using
7778 			 * RATEMASK CMD
7779 			 */
7780 			ath11k_warn(ar->ab,
7781 				    "setting %d mcs values in bitrate mask not supported\n",
7782 				num_rates);
7783 			return -EINVAL;
7784 		}
7785 
7786 		num_rates = ath11k_mac_bitrate_mask_num_he_rates(ar, band,
7787 								 mask);
7788 		if (num_rates == 1)
7789 			he_fixed_rate = true;
7790 
7791 		if (!ath11k_mac_he_mcs_range_present(ar, band, mask) &&
7792 		    num_rates > 1) {
7793 			ath11k_warn(ar->ab,
7794 				    "Setting more than one HE MCS Value in bitrate mask not supported\n");
7795 			return -EINVAL;
7796 		}
7797 
7798 		mutex_lock(&ar->conf_mutex);
7799 		ieee80211_iterate_stations_atomic(ar->hw,
7800 						  ath11k_mac_disable_peer_fixed_rate,
7801 						  arvif);
7802 
7803 		arvif->bitrate_mask = *mask;
7804 		ieee80211_iterate_stations_atomic(ar->hw,
7805 						  ath11k_mac_set_bitrate_mask_iter,
7806 						  arvif);
7807 
7808 		mutex_unlock(&ar->conf_mutex);
7809 	}
7810 
7811 	mutex_lock(&ar->conf_mutex);
7812 
7813 	ret = ath11k_mac_set_rate_params(arvif, rate, nss, sgi, ldpc, he_gi,
7814 					 he_ltf, he_fixed_rate);
7815 	if (ret) {
7816 		ath11k_warn(ar->ab, "failed to set rate params on vdev %i: %d\n",
7817 			    arvif->vdev_id, ret);
7818 	}
7819 
7820 	mutex_unlock(&ar->conf_mutex);
7821 
7822 	return ret;
7823 }
7824 
7825 static void
7826 ath11k_mac_op_reconfig_complete(struct ieee80211_hw *hw,
7827 				enum ieee80211_reconfig_type reconfig_type)
7828 {
7829 	struct ath11k *ar = hw->priv;
7830 
7831 	if (reconfig_type != IEEE80211_RECONFIG_TYPE_RESTART)
7832 		return;
7833 
7834 	mutex_lock(&ar->conf_mutex);
7835 
7836 	if (ar->state == ATH11K_STATE_RESTARTED) {
7837 		ath11k_warn(ar->ab, "pdev %d successfully recovered\n",
7838 			    ar->pdev->pdev_id);
7839 		ar->state = ATH11K_STATE_ON;
7840 		ieee80211_wake_queues(ar->hw);
7841 	}
7842 
7843 	mutex_unlock(&ar->conf_mutex);
7844 }
7845 
7846 static void
7847 ath11k_mac_update_bss_chan_survey(struct ath11k *ar,
7848 				  struct ieee80211_channel *channel)
7849 {
7850 	int ret;
7851 	enum wmi_bss_chan_info_req_type type = WMI_BSS_SURVEY_REQ_TYPE_READ;
7852 
7853 	lockdep_assert_held(&ar->conf_mutex);
7854 
7855 	if (!test_bit(WMI_TLV_SERVICE_BSS_CHANNEL_INFO_64, ar->ab->wmi_ab.svc_map) ||
7856 	    ar->rx_channel != channel)
7857 		return;
7858 
7859 	if (ar->scan.state != ATH11K_SCAN_IDLE) {
7860 		ath11k_dbg(ar->ab, ATH11K_DBG_MAC,
7861 			   "ignoring bss chan info req while scanning..\n");
7862 		return;
7863 	}
7864 
7865 	reinit_completion(&ar->bss_survey_done);
7866 
7867 	ret = ath11k_wmi_pdev_bss_chan_info_request(ar, type);
7868 	if (ret) {
7869 		ath11k_warn(ar->ab, "failed to send pdev bss chan info request\n");
7870 		return;
7871 	}
7872 
7873 	ret = wait_for_completion_timeout(&ar->bss_survey_done, 3 * HZ);
7874 	if (ret == 0)
7875 		ath11k_warn(ar->ab, "bss channel survey timed out\n");
7876 }
7877 
7878 static int ath11k_mac_op_get_survey(struct ieee80211_hw *hw, int idx,
7879 				    struct survey_info *survey)
7880 {
7881 	struct ath11k *ar = hw->priv;
7882 	struct ieee80211_supported_band *sband;
7883 	struct survey_info *ar_survey;
7884 	int ret = 0;
7885 
7886 	if (idx >= ATH11K_NUM_CHANS)
7887 		return -ENOENT;
7888 
7889 	ar_survey = &ar->survey[idx];
7890 
7891 	mutex_lock(&ar->conf_mutex);
7892 
7893 	sband = hw->wiphy->bands[NL80211_BAND_2GHZ];
7894 	if (sband && idx >= sband->n_channels) {
7895 		idx -= sband->n_channels;
7896 		sband = NULL;
7897 	}
7898 
7899 	if (!sband)
7900 		sband = hw->wiphy->bands[NL80211_BAND_5GHZ];
7901 	if (sband && idx >= sband->n_channels) {
7902 		idx -= sband->n_channels;
7903 		sband = NULL;
7904 	}
7905 
7906 	if (!sband)
7907 		sband = hw->wiphy->bands[NL80211_BAND_6GHZ];
7908 	if (!sband || idx >= sband->n_channels) {
7909 		ret = -ENOENT;
7910 		goto exit;
7911 	}
7912 
7913 	ath11k_mac_update_bss_chan_survey(ar, &sband->channels[idx]);
7914 
7915 	spin_lock_bh(&ar->data_lock);
7916 	memcpy(survey, ar_survey, sizeof(*survey));
7917 	spin_unlock_bh(&ar->data_lock);
7918 
7919 	survey->channel = &sband->channels[idx];
7920 
7921 	if (ar->rx_channel == survey->channel)
7922 		survey->filled |= SURVEY_INFO_IN_USE;
7923 
7924 exit:
7925 	mutex_unlock(&ar->conf_mutex);
7926 	return ret;
7927 }
7928 
7929 static void ath11k_mac_put_chain_rssi(struct station_info *sinfo,
7930 				      struct ath11k_sta *arsta,
7931 				      char *pre,
7932 				      bool clear)
7933 {
7934 	struct ath11k *ar = arsta->arvif->ar;
7935 	int i;
7936 	s8 rssi;
7937 
7938 	for (i = 0; i < ARRAY_SIZE(sinfo->chain_signal); i++) {
7939 		sinfo->chains &= ~BIT(i);
7940 		rssi = arsta->chain_signal[i];
7941 		if (clear)
7942 			arsta->chain_signal[i] = ATH11K_INVALID_RSSI_FULL;
7943 
7944 		ath11k_dbg(ar->ab, ATH11K_DBG_MAC,
7945 			   "mac sta statistics %s rssi[%d] %d\n", pre, i, rssi);
7946 
7947 		if (rssi != ATH11K_DEFAULT_NOISE_FLOOR &&
7948 		    rssi != ATH11K_INVALID_RSSI_FULL &&
7949 		    rssi != ATH11K_INVALID_RSSI_EMPTY &&
7950 		    rssi != 0) {
7951 			sinfo->chain_signal[i] = rssi;
7952 			sinfo->chains |= BIT(i);
7953 			sinfo->filled |= BIT_ULL(NL80211_STA_INFO_CHAIN_SIGNAL);
7954 		}
7955 	}
7956 }
7957 
7958 static void ath11k_mac_op_sta_statistics(struct ieee80211_hw *hw,
7959 					 struct ieee80211_vif *vif,
7960 					 struct ieee80211_sta *sta,
7961 					 struct station_info *sinfo)
7962 {
7963 	struct ath11k_sta *arsta = (struct ath11k_sta *)sta->drv_priv;
7964 	struct ath11k *ar = arsta->arvif->ar;
7965 	s8 signal;
7966 	bool db2dbm = test_bit(WMI_TLV_SERVICE_HW_DB2DBM_CONVERSION_SUPPORT,
7967 			       ar->ab->wmi_ab.svc_map);
7968 
7969 	sinfo->rx_duration = arsta->rx_duration;
7970 	sinfo->filled |= BIT_ULL(NL80211_STA_INFO_RX_DURATION);
7971 
7972 	sinfo->tx_duration = arsta->tx_duration;
7973 	sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_DURATION);
7974 
7975 	if (arsta->txrate.legacy || arsta->txrate.nss) {
7976 		if (arsta->txrate.legacy) {
7977 			sinfo->txrate.legacy = arsta->txrate.legacy;
7978 		} else {
7979 			sinfo->txrate.mcs = arsta->txrate.mcs;
7980 			sinfo->txrate.nss = arsta->txrate.nss;
7981 			sinfo->txrate.bw = arsta->txrate.bw;
7982 			sinfo->txrate.he_gi = arsta->txrate.he_gi;
7983 			sinfo->txrate.he_dcm = arsta->txrate.he_dcm;
7984 			sinfo->txrate.he_ru_alloc = arsta->txrate.he_ru_alloc;
7985 		}
7986 		sinfo->txrate.flags = arsta->txrate.flags;
7987 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_BITRATE);
7988 	}
7989 
7990 	ath11k_mac_put_chain_rssi(sinfo, arsta, "ppdu", false);
7991 
7992 	if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_CHAIN_SIGNAL)) &&
7993 	    arsta->arvif->vdev_type == WMI_VDEV_TYPE_STA &&
7994 	    ar->ab->hw_params.supports_rssi_stats &&
7995 	    !ath11k_debugfs_get_fw_stats(ar, ar->pdev->pdev_id, 0,
7996 					 WMI_REQUEST_RSSI_PER_CHAIN_STAT)) {
7997 		ath11k_mac_put_chain_rssi(sinfo, arsta, "fw stats", true);
7998 	}
7999 
8000 	signal = arsta->rssi_comb;
8001 	if (!signal &&
8002 	    arsta->arvif->vdev_type == WMI_VDEV_TYPE_STA &&
8003 	    ar->ab->hw_params.supports_rssi_stats &&
8004 	    !(ath11k_debugfs_get_fw_stats(ar, ar->pdev->pdev_id, 0,
8005 					WMI_REQUEST_VDEV_STAT)))
8006 		signal = arsta->rssi_beacon;
8007 
8008 	ath11k_dbg(ar->ab, ATH11K_DBG_MAC,
8009 		   "mac sta statistics db2dbm %u rssi comb %d rssi beacon %d\n",
8010 		   db2dbm, arsta->rssi_comb, arsta->rssi_beacon);
8011 
8012 	if (signal) {
8013 		sinfo->signal = db2dbm ? signal : signal + ATH11K_DEFAULT_NOISE_FLOOR;
8014 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_SIGNAL);
8015 	}
8016 }
8017 
8018 static const struct ieee80211_ops ath11k_ops = {
8019 	.tx				= ath11k_mac_op_tx,
8020 	.start                          = ath11k_mac_op_start,
8021 	.stop                           = ath11k_mac_op_stop,
8022 	.reconfig_complete              = ath11k_mac_op_reconfig_complete,
8023 	.add_interface                  = ath11k_mac_op_add_interface,
8024 	.remove_interface		= ath11k_mac_op_remove_interface,
8025 	.update_vif_offload		= ath11k_mac_op_update_vif_offload,
8026 	.config                         = ath11k_mac_op_config,
8027 	.bss_info_changed               = ath11k_mac_op_bss_info_changed,
8028 	.configure_filter		= ath11k_mac_op_configure_filter,
8029 	.hw_scan                        = ath11k_mac_op_hw_scan,
8030 	.cancel_hw_scan                 = ath11k_mac_op_cancel_hw_scan,
8031 	.set_key                        = ath11k_mac_op_set_key,
8032 	.sta_state                      = ath11k_mac_op_sta_state,
8033 	.sta_set_4addr                  = ath11k_mac_op_sta_set_4addr,
8034 	.sta_set_txpwr			= ath11k_mac_op_sta_set_txpwr,
8035 	.sta_rc_update			= ath11k_mac_op_sta_rc_update,
8036 	.conf_tx                        = ath11k_mac_op_conf_tx,
8037 	.set_antenna			= ath11k_mac_op_set_antenna,
8038 	.get_antenna			= ath11k_mac_op_get_antenna,
8039 	.ampdu_action			= ath11k_mac_op_ampdu_action,
8040 	.add_chanctx			= ath11k_mac_op_add_chanctx,
8041 	.remove_chanctx			= ath11k_mac_op_remove_chanctx,
8042 	.change_chanctx			= ath11k_mac_op_change_chanctx,
8043 	.assign_vif_chanctx		= ath11k_mac_op_assign_vif_chanctx,
8044 	.unassign_vif_chanctx		= ath11k_mac_op_unassign_vif_chanctx,
8045 	.switch_vif_chanctx		= ath11k_mac_op_switch_vif_chanctx,
8046 	.set_rts_threshold		= ath11k_mac_op_set_rts_threshold,
8047 	.set_frag_threshold		= ath11k_mac_op_set_frag_threshold,
8048 	.set_bitrate_mask		= ath11k_mac_op_set_bitrate_mask,
8049 	.get_survey			= ath11k_mac_op_get_survey,
8050 	.flush				= ath11k_mac_op_flush,
8051 	.sta_statistics			= ath11k_mac_op_sta_statistics,
8052 	CFG80211_TESTMODE_CMD(ath11k_tm_cmd)
8053 #ifdef CONFIG_ATH11K_DEBUGFS
8054 	.sta_add_debugfs		= ath11k_debugfs_sta_op_add,
8055 #endif
8056 };
8057 
8058 static void ath11k_mac_update_ch_list(struct ath11k *ar,
8059 				      struct ieee80211_supported_band *band,
8060 				      u32 freq_low, u32 freq_high)
8061 {
8062 	int i;
8063 
8064 	if (!(freq_low && freq_high))
8065 		return;
8066 
8067 	for (i = 0; i < band->n_channels; i++) {
8068 		if (band->channels[i].center_freq < freq_low ||
8069 		    band->channels[i].center_freq > freq_high)
8070 			band->channels[i].flags |= IEEE80211_CHAN_DISABLED;
8071 	}
8072 }
8073 
8074 static u32 ath11k_get_phy_id(struct ath11k *ar, u32 band)
8075 {
8076 	struct ath11k_pdev *pdev = ar->pdev;
8077 	struct ath11k_pdev_cap *pdev_cap = &pdev->cap;
8078 
8079 	if (band == WMI_HOST_WLAN_2G_CAP)
8080 		return pdev_cap->band[NL80211_BAND_2GHZ].phy_id;
8081 
8082 	if (band == WMI_HOST_WLAN_5G_CAP)
8083 		return pdev_cap->band[NL80211_BAND_5GHZ].phy_id;
8084 
8085 	ath11k_warn(ar->ab, "unsupported phy cap:%d\n", band);
8086 
8087 	return 0;
8088 }
8089 
8090 static int ath11k_mac_setup_channels_rates(struct ath11k *ar,
8091 					   u32 supported_bands)
8092 {
8093 	struct ieee80211_supported_band *band;
8094 	struct ath11k_hal_reg_capabilities_ext *reg_cap, *temp_reg_cap;
8095 	void *channels;
8096 	u32 phy_id;
8097 
8098 	BUILD_BUG_ON((ARRAY_SIZE(ath11k_2ghz_channels) +
8099 		      ARRAY_SIZE(ath11k_5ghz_channels) +
8100 		      ARRAY_SIZE(ath11k_6ghz_channels)) !=
8101 		     ATH11K_NUM_CHANS);
8102 
8103 	reg_cap = &ar->ab->hal_reg_cap[ar->pdev_idx];
8104 	temp_reg_cap = reg_cap;
8105 
8106 	if (supported_bands & WMI_HOST_WLAN_2G_CAP) {
8107 		channels = kmemdup(ath11k_2ghz_channels,
8108 				   sizeof(ath11k_2ghz_channels),
8109 				   GFP_KERNEL);
8110 		if (!channels)
8111 			return -ENOMEM;
8112 
8113 		band = &ar->mac.sbands[NL80211_BAND_2GHZ];
8114 		band->band = NL80211_BAND_2GHZ;
8115 		band->n_channels = ARRAY_SIZE(ath11k_2ghz_channels);
8116 		band->channels = channels;
8117 		band->n_bitrates = ath11k_g_rates_size;
8118 		band->bitrates = ath11k_g_rates;
8119 		ar->hw->wiphy->bands[NL80211_BAND_2GHZ] = band;
8120 
8121 		if (ar->ab->hw_params.single_pdev_only) {
8122 			phy_id = ath11k_get_phy_id(ar, WMI_HOST_WLAN_2G_CAP);
8123 			temp_reg_cap = &ar->ab->hal_reg_cap[phy_id];
8124 		}
8125 		ath11k_mac_update_ch_list(ar, band,
8126 					  temp_reg_cap->low_2ghz_chan,
8127 					  temp_reg_cap->high_2ghz_chan);
8128 	}
8129 
8130 	if (supported_bands & WMI_HOST_WLAN_5G_CAP) {
8131 		if (reg_cap->high_5ghz_chan >= ATH11K_MAX_6G_FREQ) {
8132 			channels = kmemdup(ath11k_6ghz_channels,
8133 					   sizeof(ath11k_6ghz_channels), GFP_KERNEL);
8134 			if (!channels) {
8135 				kfree(ar->mac.sbands[NL80211_BAND_2GHZ].channels);
8136 				return -ENOMEM;
8137 			}
8138 
8139 			ar->supports_6ghz = true;
8140 			band = &ar->mac.sbands[NL80211_BAND_6GHZ];
8141 			band->band = NL80211_BAND_6GHZ;
8142 			band->n_channels = ARRAY_SIZE(ath11k_6ghz_channels);
8143 			band->channels = channels;
8144 			band->n_bitrates = ath11k_a_rates_size;
8145 			band->bitrates = ath11k_a_rates;
8146 			ar->hw->wiphy->bands[NL80211_BAND_6GHZ] = band;
8147 
8148 			if (ar->ab->hw_params.single_pdev_only) {
8149 				phy_id = ath11k_get_phy_id(ar, WMI_HOST_WLAN_5G_CAP);
8150 				temp_reg_cap = &ar->ab->hal_reg_cap[phy_id];
8151 			}
8152 
8153 			ath11k_mac_update_ch_list(ar, band,
8154 						  temp_reg_cap->low_5ghz_chan,
8155 						  temp_reg_cap->high_5ghz_chan);
8156 		}
8157 
8158 		if (reg_cap->low_5ghz_chan < ATH11K_MIN_6G_FREQ) {
8159 			channels = kmemdup(ath11k_5ghz_channels,
8160 					   sizeof(ath11k_5ghz_channels),
8161 					   GFP_KERNEL);
8162 			if (!channels) {
8163 				kfree(ar->mac.sbands[NL80211_BAND_2GHZ].channels);
8164 				kfree(ar->mac.sbands[NL80211_BAND_6GHZ].channels);
8165 				return -ENOMEM;
8166 			}
8167 
8168 			band = &ar->mac.sbands[NL80211_BAND_5GHZ];
8169 			band->band = NL80211_BAND_5GHZ;
8170 			band->n_channels = ARRAY_SIZE(ath11k_5ghz_channels);
8171 			band->channels = channels;
8172 			band->n_bitrates = ath11k_a_rates_size;
8173 			band->bitrates = ath11k_a_rates;
8174 			ar->hw->wiphy->bands[NL80211_BAND_5GHZ] = band;
8175 
8176 			if (ar->ab->hw_params.single_pdev_only) {
8177 				phy_id = ath11k_get_phy_id(ar, WMI_HOST_WLAN_5G_CAP);
8178 				temp_reg_cap = &ar->ab->hal_reg_cap[phy_id];
8179 			}
8180 
8181 			ath11k_mac_update_ch_list(ar, band,
8182 						  temp_reg_cap->low_5ghz_chan,
8183 						  temp_reg_cap->high_5ghz_chan);
8184 		}
8185 	}
8186 
8187 	return 0;
8188 }
8189 
8190 static int ath11k_mac_setup_iface_combinations(struct ath11k *ar)
8191 {
8192 	struct ath11k_base *ab = ar->ab;
8193 	struct ieee80211_iface_combination *combinations;
8194 	struct ieee80211_iface_limit *limits;
8195 	int n_limits;
8196 
8197 	combinations = kzalloc(sizeof(*combinations), GFP_KERNEL);
8198 	if (!combinations)
8199 		return -ENOMEM;
8200 
8201 	n_limits = 2;
8202 
8203 	limits = kcalloc(n_limits, sizeof(*limits), GFP_KERNEL);
8204 	if (!limits) {
8205 		kfree(combinations);
8206 		return -ENOMEM;
8207 	}
8208 
8209 	limits[0].max = 1;
8210 	limits[0].types |= BIT(NL80211_IFTYPE_STATION);
8211 
8212 	limits[1].max = 16;
8213 	limits[1].types |= BIT(NL80211_IFTYPE_AP);
8214 
8215 	if (IS_ENABLED(CONFIG_MAC80211_MESH) &&
8216 	    ab->hw_params.interface_modes & BIT(NL80211_IFTYPE_MESH_POINT))
8217 		limits[1].types |= BIT(NL80211_IFTYPE_MESH_POINT);
8218 
8219 	combinations[0].limits = limits;
8220 	combinations[0].n_limits = n_limits;
8221 	combinations[0].max_interfaces = 16;
8222 	combinations[0].num_different_channels = 1;
8223 	combinations[0].beacon_int_infra_match = true;
8224 	combinations[0].beacon_int_min_gcd = 100;
8225 	combinations[0].radar_detect_widths = BIT(NL80211_CHAN_WIDTH_20_NOHT) |
8226 						BIT(NL80211_CHAN_WIDTH_20) |
8227 						BIT(NL80211_CHAN_WIDTH_40) |
8228 						BIT(NL80211_CHAN_WIDTH_80) |
8229 						BIT(NL80211_CHAN_WIDTH_80P80) |
8230 						BIT(NL80211_CHAN_WIDTH_160);
8231 
8232 	ar->hw->wiphy->iface_combinations = combinations;
8233 	ar->hw->wiphy->n_iface_combinations = 1;
8234 
8235 	return 0;
8236 }
8237 
8238 static const u8 ath11k_if_types_ext_capa[] = {
8239 	[0] = WLAN_EXT_CAPA1_EXT_CHANNEL_SWITCHING,
8240 	[7] = WLAN_EXT_CAPA8_OPMODE_NOTIF,
8241 };
8242 
8243 static const u8 ath11k_if_types_ext_capa_sta[] = {
8244 	[0] = WLAN_EXT_CAPA1_EXT_CHANNEL_SWITCHING,
8245 	[7] = WLAN_EXT_CAPA8_OPMODE_NOTIF,
8246 	[9] = WLAN_EXT_CAPA10_TWT_REQUESTER_SUPPORT,
8247 };
8248 
8249 static const u8 ath11k_if_types_ext_capa_ap[] = {
8250 	[0] = WLAN_EXT_CAPA1_EXT_CHANNEL_SWITCHING,
8251 	[7] = WLAN_EXT_CAPA8_OPMODE_NOTIF,
8252 	[9] = WLAN_EXT_CAPA10_TWT_RESPONDER_SUPPORT,
8253 };
8254 
8255 static const struct wiphy_iftype_ext_capab ath11k_iftypes_ext_capa[] = {
8256 	{
8257 		.extended_capabilities = ath11k_if_types_ext_capa,
8258 		.extended_capabilities_mask = ath11k_if_types_ext_capa,
8259 		.extended_capabilities_len = sizeof(ath11k_if_types_ext_capa),
8260 	}, {
8261 		.iftype = NL80211_IFTYPE_STATION,
8262 		.extended_capabilities = ath11k_if_types_ext_capa_sta,
8263 		.extended_capabilities_mask = ath11k_if_types_ext_capa_sta,
8264 		.extended_capabilities_len =
8265 				sizeof(ath11k_if_types_ext_capa_sta),
8266 	}, {
8267 		.iftype = NL80211_IFTYPE_AP,
8268 		.extended_capabilities = ath11k_if_types_ext_capa_ap,
8269 		.extended_capabilities_mask = ath11k_if_types_ext_capa_ap,
8270 		.extended_capabilities_len =
8271 				sizeof(ath11k_if_types_ext_capa_ap),
8272 	},
8273 };
8274 
8275 static void __ath11k_mac_unregister(struct ath11k *ar)
8276 {
8277 	cancel_work_sync(&ar->regd_update_work);
8278 
8279 	ieee80211_unregister_hw(ar->hw);
8280 
8281 	idr_for_each(&ar->txmgmt_idr, ath11k_mac_tx_mgmt_pending_free, ar);
8282 	idr_destroy(&ar->txmgmt_idr);
8283 
8284 	kfree(ar->mac.sbands[NL80211_BAND_2GHZ].channels);
8285 	kfree(ar->mac.sbands[NL80211_BAND_5GHZ].channels);
8286 	kfree(ar->mac.sbands[NL80211_BAND_6GHZ].channels);
8287 
8288 	kfree(ar->hw->wiphy->iface_combinations[0].limits);
8289 	kfree(ar->hw->wiphy->iface_combinations);
8290 
8291 	SET_IEEE80211_DEV(ar->hw, NULL);
8292 }
8293 
8294 void ath11k_mac_unregister(struct ath11k_base *ab)
8295 {
8296 	struct ath11k *ar;
8297 	struct ath11k_pdev *pdev;
8298 	int i;
8299 
8300 	for (i = 0; i < ab->num_radios; i++) {
8301 		pdev = &ab->pdevs[i];
8302 		ar = pdev->ar;
8303 		if (!ar)
8304 			continue;
8305 
8306 		__ath11k_mac_unregister(ar);
8307 	}
8308 }
8309 
8310 static int __ath11k_mac_register(struct ath11k *ar)
8311 {
8312 	struct ath11k_base *ab = ar->ab;
8313 	struct ath11k_pdev_cap *cap = &ar->pdev->cap;
8314 	static const u32 cipher_suites[] = {
8315 		WLAN_CIPHER_SUITE_TKIP,
8316 		WLAN_CIPHER_SUITE_CCMP,
8317 		WLAN_CIPHER_SUITE_AES_CMAC,
8318 		WLAN_CIPHER_SUITE_BIP_CMAC_256,
8319 		WLAN_CIPHER_SUITE_BIP_GMAC_128,
8320 		WLAN_CIPHER_SUITE_BIP_GMAC_256,
8321 		WLAN_CIPHER_SUITE_GCMP,
8322 		WLAN_CIPHER_SUITE_GCMP_256,
8323 		WLAN_CIPHER_SUITE_CCMP_256,
8324 	};
8325 	int ret;
8326 	u32 ht_cap = 0;
8327 
8328 	ath11k_pdev_caps_update(ar);
8329 
8330 	SET_IEEE80211_PERM_ADDR(ar->hw, ar->mac_addr);
8331 
8332 	SET_IEEE80211_DEV(ar->hw, ab->dev);
8333 
8334 	ret = ath11k_mac_setup_channels_rates(ar,
8335 					      cap->supported_bands);
8336 	if (ret)
8337 		goto err;
8338 
8339 	ath11k_mac_setup_ht_vht_cap(ar, cap, &ht_cap);
8340 	ath11k_mac_setup_he_cap(ar, cap);
8341 
8342 	ret = ath11k_mac_setup_iface_combinations(ar);
8343 	if (ret) {
8344 		ath11k_err(ar->ab, "failed to setup interface combinations: %d\n", ret);
8345 		goto err_free_channels;
8346 	}
8347 
8348 	ar->hw->wiphy->available_antennas_rx = cap->rx_chain_mask;
8349 	ar->hw->wiphy->available_antennas_tx = cap->tx_chain_mask;
8350 
8351 	ar->hw->wiphy->interface_modes = ab->hw_params.interface_modes;
8352 
8353 	if (ab->hw_params.single_pdev_only && ar->supports_6ghz)
8354 		ieee80211_hw_set(ar->hw, SINGLE_SCAN_ON_ALL_BANDS);
8355 
8356 	ieee80211_hw_set(ar->hw, SIGNAL_DBM);
8357 	ieee80211_hw_set(ar->hw, SUPPORTS_PS);
8358 	ieee80211_hw_set(ar->hw, SUPPORTS_DYNAMIC_PS);
8359 	ieee80211_hw_set(ar->hw, MFP_CAPABLE);
8360 	ieee80211_hw_set(ar->hw, REPORTS_TX_ACK_STATUS);
8361 	ieee80211_hw_set(ar->hw, HAS_RATE_CONTROL);
8362 	ieee80211_hw_set(ar->hw, AP_LINK_PS);
8363 	ieee80211_hw_set(ar->hw, SPECTRUM_MGMT);
8364 	ieee80211_hw_set(ar->hw, CONNECTION_MONITOR);
8365 	ieee80211_hw_set(ar->hw, SUPPORTS_PER_STA_GTK);
8366 	ieee80211_hw_set(ar->hw, WANT_MONITOR_VIF);
8367 	ieee80211_hw_set(ar->hw, CHANCTX_STA_CSA);
8368 	ieee80211_hw_set(ar->hw, QUEUE_CONTROL);
8369 	ieee80211_hw_set(ar->hw, SUPPORTS_TX_FRAG);
8370 	ieee80211_hw_set(ar->hw, REPORTS_LOW_ACK);
8371 
8372 	if (ath11k_frame_mode == ATH11K_HW_TXRX_ETHERNET) {
8373 		ieee80211_hw_set(ar->hw, SUPPORTS_TX_ENCAP_OFFLOAD);
8374 		ieee80211_hw_set(ar->hw, SUPPORTS_RX_DECAP_OFFLOAD);
8375 	}
8376 
8377 	if (cap->nss_ratio_enabled)
8378 		ieee80211_hw_set(ar->hw, SUPPORTS_VHT_EXT_NSS_BW);
8379 
8380 	if ((ht_cap & WMI_HT_CAP_ENABLED) || ar->supports_6ghz) {
8381 		ieee80211_hw_set(ar->hw, AMPDU_AGGREGATION);
8382 		ieee80211_hw_set(ar->hw, TX_AMPDU_SETUP_IN_HW);
8383 		ieee80211_hw_set(ar->hw, SUPPORTS_REORDERING_BUFFER);
8384 		ieee80211_hw_set(ar->hw, SUPPORTS_AMSDU_IN_AMPDU);
8385 		ieee80211_hw_set(ar->hw, USES_RSS);
8386 	}
8387 
8388 	ar->hw->wiphy->features |= NL80211_FEATURE_STATIC_SMPS;
8389 	ar->hw->wiphy->flags |= WIPHY_FLAG_IBSS_RSN;
8390 
8391 	/* TODO: Check if HT capability advertised from firmware is different
8392 	 * for each band for a dual band capable radio. It will be tricky to
8393 	 * handle it when the ht capability different for each band.
8394 	 */
8395 	if (ht_cap & WMI_HT_CAP_DYNAMIC_SMPS ||
8396 	    (ar->supports_6ghz && ab->hw_params.supports_dynamic_smps_6ghz))
8397 		ar->hw->wiphy->features |= NL80211_FEATURE_DYNAMIC_SMPS;
8398 
8399 	ar->hw->wiphy->max_scan_ssids = WLAN_SCAN_PARAMS_MAX_SSID;
8400 	ar->hw->wiphy->max_scan_ie_len = WLAN_SCAN_PARAMS_MAX_IE_LEN;
8401 
8402 	ar->hw->max_listen_interval = ATH11K_MAX_HW_LISTEN_INTERVAL;
8403 
8404 	ar->hw->wiphy->flags |= WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL;
8405 	ar->hw->wiphy->flags |= WIPHY_FLAG_HAS_CHANNEL_SWITCH;
8406 	ar->hw->wiphy->max_remain_on_channel_duration = 5000;
8407 
8408 	ar->hw->wiphy->flags |= WIPHY_FLAG_AP_UAPSD;
8409 	ar->hw->wiphy->features |= NL80211_FEATURE_AP_MODE_CHAN_WIDTH_CHANGE |
8410 				   NL80211_FEATURE_AP_SCAN;
8411 
8412 	ar->max_num_stations = TARGET_NUM_STATIONS(ab);
8413 	ar->max_num_peers = TARGET_NUM_PEERS_PDEV(ab);
8414 
8415 	ar->hw->wiphy->max_ap_assoc_sta = ar->max_num_stations;
8416 
8417 	if (test_bit(WMI_TLV_SERVICE_SPOOF_MAC_SUPPORT, ar->wmi->wmi_ab->svc_map)) {
8418 		ar->hw->wiphy->features |=
8419 			NL80211_FEATURE_SCAN_RANDOM_MAC_ADDR;
8420 	}
8421 
8422 	ar->hw->queues = ATH11K_HW_MAX_QUEUES;
8423 	ar->hw->wiphy->tx_queue_len = ATH11K_QUEUE_LEN;
8424 	ar->hw->offchannel_tx_hw_queue = ATH11K_HW_MAX_QUEUES - 1;
8425 	ar->hw->max_rx_aggregation_subframes = IEEE80211_MAX_AMPDU_BUF;
8426 
8427 	ar->hw->vif_data_size = sizeof(struct ath11k_vif);
8428 	ar->hw->sta_data_size = sizeof(struct ath11k_sta);
8429 
8430 	wiphy_ext_feature_set(ar->hw->wiphy, NL80211_EXT_FEATURE_CQM_RSSI_LIST);
8431 	wiphy_ext_feature_set(ar->hw->wiphy, NL80211_EXT_FEATURE_STA_TX_PWR);
8432 	if (test_bit(WMI_TLV_SERVICE_BSS_COLOR_OFFLOAD, ar->ab->wmi_ab.svc_map))
8433 		wiphy_ext_feature_set(ar->hw->wiphy,
8434 				      NL80211_EXT_FEATURE_BSS_COLOR);
8435 
8436 	ar->hw->wiphy->cipher_suites = cipher_suites;
8437 	ar->hw->wiphy->n_cipher_suites = ARRAY_SIZE(cipher_suites);
8438 
8439 	ar->hw->wiphy->iftype_ext_capab = ath11k_iftypes_ext_capa;
8440 	ar->hw->wiphy->num_iftype_ext_capab =
8441 		ARRAY_SIZE(ath11k_iftypes_ext_capa);
8442 
8443 	if (ar->supports_6ghz) {
8444 		wiphy_ext_feature_set(ar->hw->wiphy,
8445 				      NL80211_EXT_FEATURE_FILS_DISCOVERY);
8446 		wiphy_ext_feature_set(ar->hw->wiphy,
8447 				      NL80211_EXT_FEATURE_UNSOL_BCAST_PROBE_RESP);
8448 	}
8449 
8450 	ath11k_reg_init(ar);
8451 
8452 	if (!test_bit(ATH11K_FLAG_RAW_MODE, &ab->dev_flags)) {
8453 		ar->hw->netdev_features = NETIF_F_HW_CSUM;
8454 		ieee80211_hw_set(ar->hw, SW_CRYPTO_CONTROL);
8455 		ieee80211_hw_set(ar->hw, SUPPORT_FAST_XMIT);
8456 	}
8457 
8458 	ret = ieee80211_register_hw(ar->hw);
8459 	if (ret) {
8460 		ath11k_err(ar->ab, "ieee80211 registration failed: %d\n", ret);
8461 		goto err_free_if_combs;
8462 	}
8463 
8464 	if (!ab->hw_params.supports_monitor)
8465 		/* There's a race between calling ieee80211_register_hw()
8466 		 * and here where the monitor mode is enabled for a little
8467 		 * while. But that time is so short and in practise it make
8468 		 * a difference in real life.
8469 		 */
8470 		ar->hw->wiphy->interface_modes &= ~BIT(NL80211_IFTYPE_MONITOR);
8471 
8472 	/* Apply the regd received during initialization */
8473 	ret = ath11k_regd_update(ar);
8474 	if (ret) {
8475 		ath11k_err(ar->ab, "ath11k regd update failed: %d\n", ret);
8476 		goto err_unregister_hw;
8477 	}
8478 
8479 	ret = ath11k_debugfs_register(ar);
8480 	if (ret) {
8481 		ath11k_err(ar->ab, "debugfs registration failed: %d\n", ret);
8482 		goto err_unregister_hw;
8483 	}
8484 
8485 	return 0;
8486 
8487 err_unregister_hw:
8488 	ieee80211_unregister_hw(ar->hw);
8489 
8490 err_free_if_combs:
8491 	kfree(ar->hw->wiphy->iface_combinations[0].limits);
8492 	kfree(ar->hw->wiphy->iface_combinations);
8493 
8494 err_free_channels:
8495 	kfree(ar->mac.sbands[NL80211_BAND_2GHZ].channels);
8496 	kfree(ar->mac.sbands[NL80211_BAND_5GHZ].channels);
8497 	kfree(ar->mac.sbands[NL80211_BAND_6GHZ].channels);
8498 
8499 err:
8500 	SET_IEEE80211_DEV(ar->hw, NULL);
8501 	return ret;
8502 }
8503 
8504 int ath11k_mac_register(struct ath11k_base *ab)
8505 {
8506 	struct ath11k *ar;
8507 	struct ath11k_pdev *pdev;
8508 	int i;
8509 	int ret;
8510 
8511 	if (test_bit(ATH11K_FLAG_REGISTERED, &ab->dev_flags))
8512 		return 0;
8513 
8514 	/* Initialize channel counters frequency value in hertz */
8515 	ab->cc_freq_hz = IPQ8074_CC_FREQ_HERTZ;
8516 	ab->free_vdev_map = (1LL << (ab->num_radios * TARGET_NUM_VDEVS(ab))) - 1;
8517 
8518 	for (i = 0; i < ab->num_radios; i++) {
8519 		pdev = &ab->pdevs[i];
8520 		ar = pdev->ar;
8521 		if (ab->pdevs_macaddr_valid) {
8522 			ether_addr_copy(ar->mac_addr, pdev->mac_addr);
8523 		} else {
8524 			ether_addr_copy(ar->mac_addr, ab->mac_addr);
8525 			ar->mac_addr[4] += i;
8526 		}
8527 
8528 		idr_init(&ar->txmgmt_idr);
8529 		spin_lock_init(&ar->txmgmt_idr_lock);
8530 
8531 		ret = __ath11k_mac_register(ar);
8532 		if (ret)
8533 			goto err_cleanup;
8534 
8535 		init_waitqueue_head(&ar->txmgmt_empty_waitq);
8536 	}
8537 
8538 	return 0;
8539 
8540 err_cleanup:
8541 	for (i = i - 1; i >= 0; i--) {
8542 		pdev = &ab->pdevs[i];
8543 		ar = pdev->ar;
8544 		__ath11k_mac_unregister(ar);
8545 	}
8546 
8547 	return ret;
8548 }
8549 
8550 int ath11k_mac_allocate(struct ath11k_base *ab)
8551 {
8552 	struct ieee80211_hw *hw;
8553 	struct ath11k *ar;
8554 	struct ath11k_pdev *pdev;
8555 	int ret;
8556 	int i;
8557 
8558 	if (test_bit(ATH11K_FLAG_REGISTERED, &ab->dev_flags))
8559 		return 0;
8560 
8561 	for (i = 0; i < ab->num_radios; i++) {
8562 		pdev = &ab->pdevs[i];
8563 		hw = ieee80211_alloc_hw(sizeof(struct ath11k), &ath11k_ops);
8564 		if (!hw) {
8565 			ath11k_warn(ab, "failed to allocate mac80211 hw device\n");
8566 			ret = -ENOMEM;
8567 			goto err_free_mac;
8568 		}
8569 
8570 		ar = hw->priv;
8571 		ar->hw = hw;
8572 		ar->ab = ab;
8573 		ar->pdev = pdev;
8574 		ar->pdev_idx = i;
8575 		ar->lmac_id = ath11k_hw_get_mac_from_pdev_id(&ab->hw_params, i);
8576 
8577 		ar->wmi = &ab->wmi_ab.wmi[i];
8578 		/* FIXME wmi[0] is already initialized during attach,
8579 		 * Should we do this again?
8580 		 */
8581 		ath11k_wmi_pdev_attach(ab, i);
8582 
8583 		ar->cfg_tx_chainmask = pdev->cap.tx_chain_mask;
8584 		ar->cfg_rx_chainmask = pdev->cap.rx_chain_mask;
8585 		ar->num_tx_chains = get_num_chains(pdev->cap.tx_chain_mask);
8586 		ar->num_rx_chains = get_num_chains(pdev->cap.rx_chain_mask);
8587 
8588 		pdev->ar = ar;
8589 		spin_lock_init(&ar->data_lock);
8590 		INIT_LIST_HEAD(&ar->arvifs);
8591 		INIT_LIST_HEAD(&ar->ppdu_stats_info);
8592 		mutex_init(&ar->conf_mutex);
8593 		init_completion(&ar->vdev_setup_done);
8594 		init_completion(&ar->vdev_delete_done);
8595 		init_completion(&ar->peer_assoc_done);
8596 		init_completion(&ar->peer_delete_done);
8597 		init_completion(&ar->install_key_done);
8598 		init_completion(&ar->bss_survey_done);
8599 		init_completion(&ar->scan.started);
8600 		init_completion(&ar->scan.completed);
8601 		init_completion(&ar->thermal.wmi_sync);
8602 
8603 		INIT_DELAYED_WORK(&ar->scan.timeout, ath11k_scan_timeout_work);
8604 		INIT_WORK(&ar->regd_update_work, ath11k_regd_update_work);
8605 
8606 		INIT_WORK(&ar->wmi_mgmt_tx_work, ath11k_mgmt_over_wmi_tx_work);
8607 		skb_queue_head_init(&ar->wmi_mgmt_tx_queue);
8608 
8609 		clear_bit(ATH11K_FLAG_MONITOR_STARTED, &ar->monitor_flags);
8610 
8611 		ar->monitor_vdev_id = -1;
8612 		clear_bit(ATH11K_FLAG_MONITOR_VDEV_CREATED, &ar->monitor_flags);
8613 		ar->vdev_id_11d_scan = ATH11K_11D_INVALID_VDEV_ID;
8614 		init_completion(&ar->finish_11d_scan);
8615 		init_completion(&ar->finish_11d_ch_list);
8616 	}
8617 
8618 	return 0;
8619 
8620 err_free_mac:
8621 	ath11k_mac_destroy(ab);
8622 
8623 	return ret;
8624 }
8625 
8626 void ath11k_mac_destroy(struct ath11k_base *ab)
8627 {
8628 	struct ath11k *ar;
8629 	struct ath11k_pdev *pdev;
8630 	int i;
8631 
8632 	for (i = 0; i < ab->num_radios; i++) {
8633 		pdev = &ab->pdevs[i];
8634 		ar = pdev->ar;
8635 		if (!ar)
8636 			continue;
8637 
8638 		ieee80211_free_hw(ar->hw);
8639 		pdev->ar = NULL;
8640 	}
8641 }
8642