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