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