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