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