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