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