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