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