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