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