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