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