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