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