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