xref: /linux/drivers/net/wireless/ath/ath12k/mac.c (revision bf4afc53b77aeaa48b5409da5c8da6bb4eff7f43)
1 // SPDX-License-Identifier: BSD-3-Clause-Clear
2 /*
3  * Copyright (c) 2018-2021 The Linux Foundation. All rights reserved.
4  * Copyright (c) Qualcomm Technologies, Inc. and/or its subsidiaries.
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 #include "dp.h"
25 #include "dp_cmn.h"
26 
27 #define CHAN2G(_channel, _freq, _flags) { \
28 	.band                   = NL80211_BAND_2GHZ, \
29 	.hw_value               = (_channel), \
30 	.center_freq            = (_freq), \
31 	.flags                  = (_flags), \
32 	.max_antenna_gain       = 0, \
33 	.max_power              = 30, \
34 }
35 
36 #define CHAN5G(_channel, _freq, _flags) { \
37 	.band                   = NL80211_BAND_5GHZ, \
38 	.hw_value               = (_channel), \
39 	.center_freq            = (_freq), \
40 	.flags                  = (_flags), \
41 	.max_antenna_gain       = 0, \
42 	.max_power              = 30, \
43 }
44 
45 #define CHAN6G(_channel, _freq, _flags) { \
46 	.band                   = NL80211_BAND_6GHZ, \
47 	.hw_value               = (_channel), \
48 	.center_freq            = (_freq), \
49 	.flags                  = (_flags), \
50 	.max_antenna_gain       = 0, \
51 	.max_power              = 30, \
52 }
53 
54 static const struct ieee80211_channel ath12k_2ghz_channels[] = {
55 	CHAN2G(1, 2412, 0),
56 	CHAN2G(2, 2417, 0),
57 	CHAN2G(3, 2422, 0),
58 	CHAN2G(4, 2427, 0),
59 	CHAN2G(5, 2432, 0),
60 	CHAN2G(6, 2437, 0),
61 	CHAN2G(7, 2442, 0),
62 	CHAN2G(8, 2447, 0),
63 	CHAN2G(9, 2452, 0),
64 	CHAN2G(10, 2457, 0),
65 	CHAN2G(11, 2462, 0),
66 	CHAN2G(12, 2467, 0),
67 	CHAN2G(13, 2472, 0),
68 	CHAN2G(14, 2484, 0),
69 };
70 
71 static const struct ieee80211_channel ath12k_5ghz_channels[] = {
72 	CHAN5G(36, 5180, 0),
73 	CHAN5G(40, 5200, 0),
74 	CHAN5G(44, 5220, 0),
75 	CHAN5G(48, 5240, 0),
76 	CHAN5G(52, 5260, 0),
77 	CHAN5G(56, 5280, 0),
78 	CHAN5G(60, 5300, 0),
79 	CHAN5G(64, 5320, 0),
80 	CHAN5G(100, 5500, 0),
81 	CHAN5G(104, 5520, 0),
82 	CHAN5G(108, 5540, 0),
83 	CHAN5G(112, 5560, 0),
84 	CHAN5G(116, 5580, 0),
85 	CHAN5G(120, 5600, 0),
86 	CHAN5G(124, 5620, 0),
87 	CHAN5G(128, 5640, 0),
88 	CHAN5G(132, 5660, 0),
89 	CHAN5G(136, 5680, 0),
90 	CHAN5G(140, 5700, 0),
91 	CHAN5G(144, 5720, 0),
92 	CHAN5G(149, 5745, 0),
93 	CHAN5G(153, 5765, 0),
94 	CHAN5G(157, 5785, 0),
95 	CHAN5G(161, 5805, 0),
96 	CHAN5G(165, 5825, 0),
97 	CHAN5G(169, 5845, 0),
98 	CHAN5G(173, 5865, 0),
99 };
100 
101 static const struct ieee80211_channel ath12k_6ghz_channels[] = {
102 	/* Operating Class 136 */
103 	CHAN6G(2, 5935, 0),
104 
105 	/* Operating Classes 131-135 */
106 	CHAN6G(1, 5955, 0),
107 	CHAN6G(5, 5975, 0),
108 	CHAN6G(9, 5995, 0),
109 	CHAN6G(13, 6015, 0),
110 	CHAN6G(17, 6035, 0),
111 	CHAN6G(21, 6055, 0),
112 	CHAN6G(25, 6075, 0),
113 	CHAN6G(29, 6095, 0),
114 	CHAN6G(33, 6115, 0),
115 	CHAN6G(37, 6135, 0),
116 	CHAN6G(41, 6155, 0),
117 	CHAN6G(45, 6175, 0),
118 	CHAN6G(49, 6195, 0),
119 	CHAN6G(53, 6215, 0),
120 	CHAN6G(57, 6235, 0),
121 	CHAN6G(61, 6255, 0),
122 	CHAN6G(65, 6275, 0),
123 	CHAN6G(69, 6295, 0),
124 	CHAN6G(73, 6315, 0),
125 	CHAN6G(77, 6335, 0),
126 	CHAN6G(81, 6355, 0),
127 	CHAN6G(85, 6375, 0),
128 	CHAN6G(89, 6395, 0),
129 	CHAN6G(93, 6415, 0),
130 	CHAN6G(97, 6435, 0),
131 	CHAN6G(101, 6455, 0),
132 	CHAN6G(105, 6475, 0),
133 	CHAN6G(109, 6495, 0),
134 	CHAN6G(113, 6515, 0),
135 	CHAN6G(117, 6535, 0),
136 	CHAN6G(121, 6555, 0),
137 	CHAN6G(125, 6575, 0),
138 	CHAN6G(129, 6595, 0),
139 	CHAN6G(133, 6615, 0),
140 	CHAN6G(137, 6635, 0),
141 	CHAN6G(141, 6655, 0),
142 	CHAN6G(145, 6675, 0),
143 	CHAN6G(149, 6695, 0),
144 	CHAN6G(153, 6715, 0),
145 	CHAN6G(157, 6735, 0),
146 	CHAN6G(161, 6755, 0),
147 	CHAN6G(165, 6775, 0),
148 	CHAN6G(169, 6795, 0),
149 	CHAN6G(173, 6815, 0),
150 	CHAN6G(177, 6835, 0),
151 	CHAN6G(181, 6855, 0),
152 	CHAN6G(185, 6875, 0),
153 	CHAN6G(189, 6895, 0),
154 	CHAN6G(193, 6915, 0),
155 	CHAN6G(197, 6935, 0),
156 	CHAN6G(201, 6955, 0),
157 	CHAN6G(205, 6975, 0),
158 	CHAN6G(209, 6995, 0),
159 	CHAN6G(213, 7015, 0),
160 	CHAN6G(217, 7035, 0),
161 	CHAN6G(221, 7055, 0),
162 	CHAN6G(225, 7075, 0),
163 	CHAN6G(229, 7095, 0),
164 	CHAN6G(233, 7115, 0),
165 };
166 
167 static struct ieee80211_rate ath12k_legacy_rates[] = {
168 	{ .bitrate = 10,
169 	  .hw_value = ATH12K_HW_RATE_CCK_LP_1M },
170 	{ .bitrate = 20,
171 	  .hw_value = ATH12K_HW_RATE_CCK_LP_2M,
172 	  .hw_value_short = ATH12K_HW_RATE_CCK_SP_2M,
173 	  .flags = IEEE80211_RATE_SHORT_PREAMBLE },
174 	{ .bitrate = 55,
175 	  .hw_value = ATH12K_HW_RATE_CCK_LP_5_5M,
176 	  .hw_value_short = ATH12K_HW_RATE_CCK_SP_5_5M,
177 	  .flags = IEEE80211_RATE_SHORT_PREAMBLE },
178 	{ .bitrate = 110,
179 	  .hw_value = ATH12K_HW_RATE_CCK_LP_11M,
180 	  .hw_value_short = ATH12K_HW_RATE_CCK_SP_11M,
181 	  .flags = IEEE80211_RATE_SHORT_PREAMBLE },
182 
183 	{ .bitrate = 60, .hw_value = ATH12K_HW_RATE_OFDM_6M },
184 	{ .bitrate = 90, .hw_value = ATH12K_HW_RATE_OFDM_9M },
185 	{ .bitrate = 120, .hw_value = ATH12K_HW_RATE_OFDM_12M },
186 	{ .bitrate = 180, .hw_value = ATH12K_HW_RATE_OFDM_18M },
187 	{ .bitrate = 240, .hw_value = ATH12K_HW_RATE_OFDM_24M },
188 	{ .bitrate = 360, .hw_value = ATH12K_HW_RATE_OFDM_36M },
189 	{ .bitrate = 480, .hw_value = ATH12K_HW_RATE_OFDM_48M },
190 	{ .bitrate = 540, .hw_value = ATH12K_HW_RATE_OFDM_54M },
191 };
192 
193 static const int
194 ath12k_phymodes[NUM_NL80211_BANDS][ATH12K_CHAN_WIDTH_NUM] = {
195 	[NL80211_BAND_2GHZ] = {
196 			[NL80211_CHAN_WIDTH_5] = MODE_UNKNOWN,
197 			[NL80211_CHAN_WIDTH_10] = MODE_UNKNOWN,
198 			[NL80211_CHAN_WIDTH_20_NOHT] = MODE_11BE_EHT20_2G,
199 			[NL80211_CHAN_WIDTH_20] = MODE_11BE_EHT20_2G,
200 			[NL80211_CHAN_WIDTH_40] = MODE_11BE_EHT40_2G,
201 			[NL80211_CHAN_WIDTH_80] = MODE_UNKNOWN,
202 			[NL80211_CHAN_WIDTH_80P80] = MODE_UNKNOWN,
203 			[NL80211_CHAN_WIDTH_160] = MODE_UNKNOWN,
204 			[NL80211_CHAN_WIDTH_320] = MODE_UNKNOWN,
205 	},
206 	[NL80211_BAND_5GHZ] = {
207 			[NL80211_CHAN_WIDTH_5] = MODE_UNKNOWN,
208 			[NL80211_CHAN_WIDTH_10] = MODE_UNKNOWN,
209 			[NL80211_CHAN_WIDTH_20_NOHT] = MODE_11BE_EHT20,
210 			[NL80211_CHAN_WIDTH_20] = MODE_11BE_EHT20,
211 			[NL80211_CHAN_WIDTH_40] = MODE_11BE_EHT40,
212 			[NL80211_CHAN_WIDTH_80] = MODE_11BE_EHT80,
213 			[NL80211_CHAN_WIDTH_160] = MODE_11BE_EHT160,
214 			[NL80211_CHAN_WIDTH_80P80] = MODE_UNKNOWN,
215 			[NL80211_CHAN_WIDTH_320] = MODE_11BE_EHT320,
216 	},
217 	[NL80211_BAND_6GHZ] = {
218 			[NL80211_CHAN_WIDTH_5] = MODE_UNKNOWN,
219 			[NL80211_CHAN_WIDTH_10] = MODE_UNKNOWN,
220 			[NL80211_CHAN_WIDTH_20_NOHT] = MODE_11BE_EHT20,
221 			[NL80211_CHAN_WIDTH_20] = MODE_11BE_EHT20,
222 			[NL80211_CHAN_WIDTH_40] = MODE_11BE_EHT40,
223 			[NL80211_CHAN_WIDTH_80] = MODE_11BE_EHT80,
224 			[NL80211_CHAN_WIDTH_160] = MODE_11BE_EHT160,
225 			[NL80211_CHAN_WIDTH_80P80] = MODE_UNKNOWN,
226 			[NL80211_CHAN_WIDTH_320] = MODE_11BE_EHT320,
227 	},
228 
229 };
230 
231 const struct htt_rx_ring_tlv_filter ath12k_mac_mon_status_filter_default = {
232 	.rx_filter = HTT_RX_FILTER_TLV_FLAGS_MPDU_START |
233 		     HTT_RX_FILTER_TLV_FLAGS_PPDU_END |
234 		     HTT_RX_FILTER_TLV_FLAGS_PPDU_END_STATUS_DONE |
235 		     HTT_RX_FILTER_TLV_FLAGS_PPDU_START_USER_INFO,
236 	.pkt_filter_flags0 = HTT_RX_FP_MGMT_FILTER_FLAGS0,
237 	.pkt_filter_flags1 = HTT_RX_FP_MGMT_FILTER_FLAGS1,
238 	.pkt_filter_flags2 = HTT_RX_FP_CTRL_FILTER_FLASG2,
239 	.pkt_filter_flags3 = HTT_RX_FP_DATA_FILTER_FLASG3 |
240 			     HTT_RX_FP_CTRL_FILTER_FLASG3
241 };
242 
243 #define ATH12K_MAC_FIRST_OFDM_RATE_IDX 4
244 #define ath12k_g_rates ath12k_legacy_rates
245 #define ath12k_g_rates_size (ARRAY_SIZE(ath12k_legacy_rates))
246 #define ath12k_a_rates (ath12k_legacy_rates + 4)
247 #define ath12k_a_rates_size (ARRAY_SIZE(ath12k_legacy_rates) - 4)
248 
249 #define ATH12K_MAC_SCAN_TIMEOUT_MSECS 200 /* in msecs */
250 
251 static const u32 ath12k_smps_map[] = {
252 	[WLAN_HT_CAP_SM_PS_STATIC] = WMI_PEER_SMPS_STATIC,
253 	[WLAN_HT_CAP_SM_PS_DYNAMIC] = WMI_PEER_SMPS_DYNAMIC,
254 	[WLAN_HT_CAP_SM_PS_INVALID] = WMI_PEER_SMPS_PS_NONE,
255 	[WLAN_HT_CAP_SM_PS_DISABLED] = WMI_PEER_SMPS_PS_NONE,
256 };
257 
258 static int ath12k_start_vdev_delay(struct ath12k *ar,
259 				   struct ath12k_link_vif *arvif);
260 static void ath12k_mac_stop(struct ath12k *ar);
261 static int ath12k_mac_vdev_create(struct ath12k *ar, struct ath12k_link_vif *arvif);
262 static int ath12k_mac_vdev_delete(struct ath12k *ar, struct ath12k_link_vif *arvif);
263 
264 static const char *ath12k_mac_phymode_str(enum wmi_phy_mode mode)
265 {
266 	switch (mode) {
267 	case MODE_11A:
268 		return "11a";
269 	case MODE_11G:
270 		return "11g";
271 	case MODE_11B:
272 		return "11b";
273 	case MODE_11GONLY:
274 		return "11gonly";
275 	case MODE_11NA_HT20:
276 		return "11na-ht20";
277 	case MODE_11NG_HT20:
278 		return "11ng-ht20";
279 	case MODE_11NA_HT40:
280 		return "11na-ht40";
281 	case MODE_11NG_HT40:
282 		return "11ng-ht40";
283 	case MODE_11AC_VHT20:
284 		return "11ac-vht20";
285 	case MODE_11AC_VHT40:
286 		return "11ac-vht40";
287 	case MODE_11AC_VHT80:
288 		return "11ac-vht80";
289 	case MODE_11AC_VHT160:
290 		return "11ac-vht160";
291 	case MODE_11AC_VHT80_80:
292 		return "11ac-vht80+80";
293 	case MODE_11AC_VHT20_2G:
294 		return "11ac-vht20-2g";
295 	case MODE_11AC_VHT40_2G:
296 		return "11ac-vht40-2g";
297 	case MODE_11AC_VHT80_2G:
298 		return "11ac-vht80-2g";
299 	case MODE_11AX_HE20:
300 		return "11ax-he20";
301 	case MODE_11AX_HE40:
302 		return "11ax-he40";
303 	case MODE_11AX_HE80:
304 		return "11ax-he80";
305 	case MODE_11AX_HE80_80:
306 		return "11ax-he80+80";
307 	case MODE_11AX_HE160:
308 		return "11ax-he160";
309 	case MODE_11AX_HE20_2G:
310 		return "11ax-he20-2g";
311 	case MODE_11AX_HE40_2G:
312 		return "11ax-he40-2g";
313 	case MODE_11AX_HE80_2G:
314 		return "11ax-he80-2g";
315 	case MODE_11BE_EHT20:
316 		return "11be-eht20";
317 	case MODE_11BE_EHT40:
318 		return "11be-eht40";
319 	case MODE_11BE_EHT80:
320 		return "11be-eht80";
321 	case MODE_11BE_EHT80_80:
322 		return "11be-eht80+80";
323 	case MODE_11BE_EHT160:
324 		return "11be-eht160";
325 	case MODE_11BE_EHT160_160:
326 		return "11be-eht160+160";
327 	case MODE_11BE_EHT320:
328 		return "11be-eht320";
329 	case MODE_11BE_EHT20_2G:
330 		return "11be-eht20-2g";
331 	case MODE_11BE_EHT40_2G:
332 		return "11be-eht40-2g";
333 	case MODE_UNKNOWN:
334 		/* skip */
335 		break;
336 
337 		/* no default handler to allow compiler to check that the
338 		 * enum is fully handled
339 		 */
340 	}
341 
342 	return "<unknown>";
343 }
344 
345 u16 ath12k_mac_he_convert_tones_to_ru_tones(u16 tones)
346 {
347 	switch (tones) {
348 	case 26:
349 		return RU_26;
350 	case 52:
351 		return RU_52;
352 	case 106:
353 		return RU_106;
354 	case 242:
355 		return RU_242;
356 	case 484:
357 		return RU_484;
358 	case 996:
359 		return RU_996;
360 	case (996 * 2):
361 		return RU_2X996;
362 	default:
363 		return RU_26;
364 	}
365 }
366 EXPORT_SYMBOL(ath12k_mac_he_convert_tones_to_ru_tones);
367 
368 enum nl80211_eht_gi ath12k_mac_eht_gi_to_nl80211_eht_gi(u8 sgi)
369 {
370 	switch (sgi) {
371 	case RX_MSDU_START_SGI_0_8_US:
372 		return NL80211_RATE_INFO_EHT_GI_0_8;
373 	case RX_MSDU_START_SGI_1_6_US:
374 		return NL80211_RATE_INFO_EHT_GI_1_6;
375 	case RX_MSDU_START_SGI_3_2_US:
376 		return NL80211_RATE_INFO_EHT_GI_3_2;
377 	default:
378 		return NL80211_RATE_INFO_EHT_GI_0_8;
379 	}
380 }
381 EXPORT_SYMBOL(ath12k_mac_eht_gi_to_nl80211_eht_gi);
382 
383 enum nl80211_eht_ru_alloc ath12k_mac_eht_ru_tones_to_nl80211_eht_ru_alloc(u16 ru_tones)
384 {
385 	switch (ru_tones) {
386 	case 26:
387 		return NL80211_RATE_INFO_EHT_RU_ALLOC_26;
388 	case 52:
389 		return NL80211_RATE_INFO_EHT_RU_ALLOC_52;
390 	case (52 + 26):
391 		return NL80211_RATE_INFO_EHT_RU_ALLOC_52P26;
392 	case 106:
393 		return NL80211_RATE_INFO_EHT_RU_ALLOC_106;
394 	case (106 + 26):
395 		return NL80211_RATE_INFO_EHT_RU_ALLOC_106P26;
396 	case 242:
397 		return NL80211_RATE_INFO_EHT_RU_ALLOC_242;
398 	case 484:
399 		return NL80211_RATE_INFO_EHT_RU_ALLOC_484;
400 	case (484 + 242):
401 		return NL80211_RATE_INFO_EHT_RU_ALLOC_484P242;
402 	case 996:
403 		return NL80211_RATE_INFO_EHT_RU_ALLOC_996;
404 	case (996 + 484):
405 		return NL80211_RATE_INFO_EHT_RU_ALLOC_996P484;
406 	case (996 + 484 + 242):
407 		return NL80211_RATE_INFO_EHT_RU_ALLOC_996P484P242;
408 	case (2 * 996):
409 		return NL80211_RATE_INFO_EHT_RU_ALLOC_2x996;
410 	case (2 * 996 + 484):
411 		return NL80211_RATE_INFO_EHT_RU_ALLOC_2x996P484;
412 	case (3 * 996):
413 		return NL80211_RATE_INFO_EHT_RU_ALLOC_3x996;
414 	case (3 * 996 + 484):
415 		return NL80211_RATE_INFO_EHT_RU_ALLOC_3x996P484;
416 	case (4 * 996):
417 		return NL80211_RATE_INFO_EHT_RU_ALLOC_4x996;
418 	default:
419 		return NL80211_RATE_INFO_EHT_RU_ALLOC_26;
420 	}
421 }
422 EXPORT_SYMBOL(ath12k_mac_eht_ru_tones_to_nl80211_eht_ru_alloc);
423 
424 enum rate_info_bw
425 ath12k_mac_bw_to_mac80211_bw(enum ath12k_supported_bw bw)
426 {
427 	u8 ret = RATE_INFO_BW_20;
428 
429 	switch (bw) {
430 	case ATH12K_BW_20:
431 		ret = RATE_INFO_BW_20;
432 		break;
433 	case ATH12K_BW_40:
434 		ret = RATE_INFO_BW_40;
435 		break;
436 	case ATH12K_BW_80:
437 		ret = RATE_INFO_BW_80;
438 		break;
439 	case ATH12K_BW_160:
440 		ret = RATE_INFO_BW_160;
441 		break;
442 	case ATH12K_BW_320:
443 		ret = RATE_INFO_BW_320;
444 		break;
445 	}
446 
447 	return ret;
448 }
449 EXPORT_SYMBOL(ath12k_mac_bw_to_mac80211_bw);
450 
451 enum ath12k_supported_bw ath12k_mac_mac80211_bw_to_ath12k_bw(enum rate_info_bw bw)
452 {
453 	switch (bw) {
454 	case RATE_INFO_BW_20:
455 		return ATH12K_BW_20;
456 	case RATE_INFO_BW_40:
457 		return ATH12K_BW_40;
458 	case RATE_INFO_BW_80:
459 		return ATH12K_BW_80;
460 	case RATE_INFO_BW_160:
461 		return ATH12K_BW_160;
462 	case RATE_INFO_BW_320:
463 		return ATH12K_BW_320;
464 	default:
465 		return ATH12K_BW_20;
466 	}
467 }
468 
469 int ath12k_mac_hw_ratecode_to_legacy_rate(u8 hw_rc, u8 preamble, u8 *rateidx,
470 					  u16 *rate)
471 {
472 	/* As default, it is OFDM rates */
473 	int i = ATH12K_MAC_FIRST_OFDM_RATE_IDX;
474 	int max_rates_idx = ath12k_g_rates_size;
475 
476 	if (preamble == WMI_RATE_PREAMBLE_CCK) {
477 		hw_rc &= ~ATH12K_HW_RATECODE_CCK_SHORT_PREAM_MASK;
478 		i = 0;
479 		max_rates_idx = ATH12K_MAC_FIRST_OFDM_RATE_IDX;
480 	}
481 
482 	while (i < max_rates_idx) {
483 		if (hw_rc == ath12k_legacy_rates[i].hw_value) {
484 			*rateidx = i;
485 			*rate = ath12k_legacy_rates[i].bitrate;
486 			return 0;
487 		}
488 		i++;
489 	}
490 
491 	return -EINVAL;
492 }
493 EXPORT_SYMBOL(ath12k_mac_hw_ratecode_to_legacy_rate);
494 
495 u8 ath12k_mac_bitrate_to_idx(const struct ieee80211_supported_band *sband,
496 			     u32 bitrate)
497 {
498 	int i;
499 
500 	for (i = 0; i < sband->n_bitrates; i++)
501 		if (sband->bitrates[i].bitrate == bitrate)
502 			return i;
503 
504 	return 0;
505 }
506 
507 static u32
508 ath12k_mac_max_ht_nss(const u8 *ht_mcs_mask)
509 {
510 	int nss;
511 
512 	for (nss = IEEE80211_HT_MCS_MASK_LEN - 1; nss >= 0; nss--)
513 		if (ht_mcs_mask[nss])
514 			return nss + 1;
515 
516 	return 1;
517 }
518 
519 static u32
520 ath12k_mac_max_vht_nss(const u16 *vht_mcs_mask)
521 {
522 	int nss;
523 
524 	for (nss = NL80211_VHT_NSS_MAX - 1; nss >= 0; nss--)
525 		if (vht_mcs_mask[nss])
526 			return nss + 1;
527 
528 	return 1;
529 }
530 
531 static u32
532 ath12k_mac_max_he_nss(const u16 he_mcs_mask[NL80211_HE_NSS_MAX])
533 {
534 	int nss;
535 
536 	for (nss = NL80211_HE_NSS_MAX - 1; nss >= 0; nss--)
537 		if (he_mcs_mask[nss])
538 			return nss + 1;
539 
540 	return 1;
541 }
542 
543 static u32
544 ath12k_mac_max_eht_nss(const u16 eht_mcs_mask[NL80211_EHT_NSS_MAX])
545 {
546 	int nss;
547 
548 	for (nss = NL80211_EHT_NSS_MAX - 1; nss >= 0; nss--)
549 		if (eht_mcs_mask[nss])
550 			return nss + 1;
551 
552 	return 1;
553 }
554 
555 static u32
556 ath12k_mac_max_eht_mcs_nss(const u8 *eht_mcs, int eht_mcs_set_size)
557 {
558 	int i;
559 	u8 nss = 0;
560 
561 	for (i = 0; i < eht_mcs_set_size; i++)
562 		nss = max(nss, u8_get_bits(eht_mcs[i], IEEE80211_EHT_MCS_NSS_RX));
563 
564 	return nss;
565 }
566 
567 static u8 ath12k_parse_mpdudensity(u8 mpdudensity)
568 {
569 /*  From IEEE Std 802.11-2020 defined values for "Minimum MPDU Start Spacing":
570  *   0 for no restriction
571  *   1 for 1/4 us
572  *   2 for 1/2 us
573  *   3 for 1 us
574  *   4 for 2 us
575  *   5 for 4 us
576  *   6 for 8 us
577  *   7 for 16 us
578  */
579 	switch (mpdudensity) {
580 	case 0:
581 		return 0;
582 	case 1:
583 	case 2:
584 	case 3:
585 	/* Our lower layer calculations limit our precision to
586 	 * 1 microsecond
587 	 */
588 		return 1;
589 	case 4:
590 		return 2;
591 	case 5:
592 		return 4;
593 	case 6:
594 		return 8;
595 	case 7:
596 		return 16;
597 	default:
598 		return 0;
599 	}
600 }
601 
602 static int ath12k_mac_vif_link_chan(struct ieee80211_vif *vif, u8 link_id,
603 				    struct cfg80211_chan_def *def)
604 {
605 	struct ieee80211_bss_conf *link_conf;
606 	struct ieee80211_chanctx_conf *conf;
607 
608 	rcu_read_lock();
609 	link_conf = rcu_dereference(vif->link_conf[link_id]);
610 
611 	if (!link_conf) {
612 		rcu_read_unlock();
613 		return -ENOLINK;
614 	}
615 
616 	conf = rcu_dereference(link_conf->chanctx_conf);
617 	if (!conf) {
618 		rcu_read_unlock();
619 		return -ENOENT;
620 	}
621 	*def = conf->def;
622 	rcu_read_unlock();
623 
624 	return 0;
625 }
626 
627 static struct ath12k_link_vif *
628 ath12k_mac_get_tx_arvif(struct ath12k_link_vif *arvif,
629 			struct ieee80211_bss_conf *link_conf)
630 {
631 	struct ieee80211_bss_conf *tx_bss_conf;
632 	struct ath12k *ar = arvif->ar;
633 	struct ath12k_vif *tx_ahvif;
634 
635 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
636 
637 	tx_bss_conf = wiphy_dereference(ath12k_ar_to_hw(ar)->wiphy,
638 					link_conf->tx_bss_conf);
639 	if (tx_bss_conf) {
640 		tx_ahvif = ath12k_vif_to_ahvif(tx_bss_conf->vif);
641 		return wiphy_dereference(tx_ahvif->ah->hw->wiphy,
642 					 tx_ahvif->link[tx_bss_conf->link_id]);
643 	}
644 
645 	return NULL;
646 }
647 
648 static const u8 *ath12k_mac_get_tx_bssid(struct ath12k_link_vif *arvif)
649 {
650 	struct ieee80211_bss_conf *link_conf;
651 	struct ath12k_link_vif *tx_arvif;
652 	struct ath12k *ar = arvif->ar;
653 
654 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
655 
656 	link_conf = ath12k_mac_get_link_bss_conf(arvif);
657 	if (!link_conf) {
658 		ath12k_warn(ar->ab,
659 			    "unable to access bss link conf for link %u required to retrieve transmitting link conf\n",
660 			    arvif->link_id);
661 		return NULL;
662 	}
663 	if (link_conf->vif->type == NL80211_IFTYPE_STATION) {
664 		if (link_conf->nontransmitted)
665 			return link_conf->transmitter_bssid;
666 	} else {
667 		tx_arvif = ath12k_mac_get_tx_arvif(arvif, link_conf);
668 		if (tx_arvif)
669 			return tx_arvif->bssid;
670 	}
671 
672 	return NULL;
673 }
674 
675 struct ieee80211_bss_conf *
676 ath12k_mac_get_link_bss_conf(struct ath12k_link_vif *arvif)
677 {
678 	struct ieee80211_vif *vif = arvif->ahvif->vif;
679 	struct ieee80211_bss_conf *link_conf;
680 	struct ath12k *ar = arvif->ar;
681 
682 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
683 
684 	if (arvif->link_id >= IEEE80211_MLD_MAX_NUM_LINKS)
685 		return NULL;
686 
687 	link_conf = wiphy_dereference(ath12k_ar_to_hw(ar)->wiphy,
688 				      vif->link_conf[arvif->link_id]);
689 
690 	return link_conf;
691 }
692 
693 static struct ieee80211_link_sta *ath12k_mac_get_link_sta(struct ath12k_link_sta *arsta)
694 {
695 	struct ath12k_sta *ahsta = arsta->ahsta;
696 	struct ieee80211_sta *sta = ath12k_ahsta_to_sta(ahsta);
697 	struct ieee80211_link_sta *link_sta;
698 
699 	lockdep_assert_wiphy(ahsta->ahvif->ah->hw->wiphy);
700 
701 	if (arsta->link_id >= IEEE80211_MLD_MAX_NUM_LINKS)
702 		return NULL;
703 
704 	link_sta = wiphy_dereference(ahsta->ahvif->ah->hw->wiphy,
705 				     sta->link[arsta->link_id]);
706 
707 	return link_sta;
708 }
709 
710 static bool ath12k_mac_bitrate_is_cck(int bitrate)
711 {
712 	switch (bitrate) {
713 	case 10:
714 	case 20:
715 	case 55:
716 	case 110:
717 		return true;
718 	}
719 
720 	return false;
721 }
722 
723 u8 ath12k_mac_hw_rate_to_idx(const struct ieee80211_supported_band *sband,
724 			     u8 hw_rate, bool cck)
725 {
726 	const struct ieee80211_rate *rate;
727 	int i;
728 
729 	for (i = 0; i < sband->n_bitrates; i++) {
730 		rate = &sband->bitrates[i];
731 
732 		if (ath12k_mac_bitrate_is_cck(rate->bitrate) != cck)
733 			continue;
734 
735 		if (rate->hw_value == hw_rate)
736 			return i;
737 		else if (rate->flags & IEEE80211_RATE_SHORT_PREAMBLE &&
738 			 rate->hw_value_short == hw_rate)
739 			return i;
740 	}
741 
742 	return 0;
743 }
744 
745 static u8 ath12k_mac_bitrate_to_rate(int bitrate)
746 {
747 	return DIV_ROUND_UP(bitrate, 5) |
748 	       (ath12k_mac_bitrate_is_cck(bitrate) ? BIT(7) : 0);
749 }
750 
751 static void ath12k_get_arvif_iter(void *data, u8 *mac,
752 				  struct ieee80211_vif *vif)
753 {
754 	struct ath12k_vif_iter *arvif_iter = data;
755 	struct ath12k_vif *ahvif = ath12k_vif_to_ahvif(vif);
756 	unsigned long links_map = ahvif->links_map;
757 	struct ath12k_link_vif *arvif;
758 	u8 link_id;
759 
760 	for_each_set_bit(link_id, &links_map, IEEE80211_MLD_MAX_NUM_LINKS) {
761 		arvif = rcu_dereference(ahvif->link[link_id]);
762 
763 		if (WARN_ON(!arvif))
764 			continue;
765 
766 		if (!arvif->is_created)
767 			continue;
768 
769 		if (arvif->vdev_id == arvif_iter->vdev_id &&
770 		    arvif->ar == arvif_iter->ar) {
771 			arvif_iter->arvif = arvif;
772 			break;
773 		}
774 	}
775 }
776 
777 struct ath12k_link_vif *ath12k_mac_get_arvif(struct ath12k *ar, u32 vdev_id)
778 {
779 	struct ath12k_vif_iter arvif_iter = {};
780 	u32 flags;
781 
782 	/* To use the arvif returned, caller must have held rcu read lock.
783 	 */
784 	WARN_ON(!rcu_read_lock_any_held());
785 	arvif_iter.vdev_id = vdev_id;
786 	arvif_iter.ar = ar;
787 
788 	flags = IEEE80211_IFACE_ITER_RESUME_ALL;
789 	ieee80211_iterate_active_interfaces_atomic(ath12k_ar_to_hw(ar),
790 						   flags,
791 						   ath12k_get_arvif_iter,
792 						   &arvif_iter);
793 	if (!arvif_iter.arvif) {
794 		ath12k_warn(ar->ab, "No VIF found for vdev %d\n", vdev_id);
795 		return NULL;
796 	}
797 
798 	return arvif_iter.arvif;
799 }
800 
801 struct ath12k_link_vif *ath12k_mac_get_arvif_by_vdev_id(struct ath12k_base *ab,
802 							u32 vdev_id)
803 {
804 	int i;
805 	struct ath12k_pdev *pdev;
806 	struct ath12k_link_vif *arvif;
807 
808 	for (i = 0; i < ab->num_radios; i++) {
809 		pdev = rcu_dereference(ab->pdevs_active[i]);
810 		if (pdev && pdev->ar &&
811 		    (pdev->ar->allocated_vdev_map & (1LL << vdev_id))) {
812 			arvif = ath12k_mac_get_arvif(pdev->ar, vdev_id);
813 			if (arvif)
814 				return arvif;
815 		}
816 	}
817 
818 	return NULL;
819 }
820 
821 struct ath12k *ath12k_mac_get_ar_by_vdev_id(struct ath12k_base *ab, u32 vdev_id)
822 {
823 	int i;
824 	struct ath12k_pdev *pdev;
825 
826 	for (i = 0; i < ab->num_radios; i++) {
827 		pdev = rcu_dereference(ab->pdevs_active[i]);
828 		if (pdev && pdev->ar) {
829 			if (pdev->ar->allocated_vdev_map & (1LL << vdev_id))
830 				return pdev->ar;
831 		}
832 	}
833 
834 	return NULL;
835 }
836 
837 struct ath12k *ath12k_mac_get_ar_by_pdev_id(struct ath12k_base *ab, u32 pdev_id)
838 {
839 	int i;
840 	struct ath12k_pdev *pdev;
841 
842 	if (ab->hw_params->single_pdev_only) {
843 		pdev = rcu_dereference(ab->pdevs_active[0]);
844 		return pdev ? pdev->ar : NULL;
845 	}
846 
847 	if (WARN_ON(pdev_id > ab->num_radios))
848 		return NULL;
849 
850 	for (i = 0; i < ab->num_radios; i++) {
851 		if (ab->fw_mode == ATH12K_FIRMWARE_MODE_FTM)
852 			pdev = &ab->pdevs[i];
853 		else
854 			pdev = rcu_dereference(ab->pdevs_active[i]);
855 
856 		if (pdev && pdev->pdev_id == pdev_id)
857 			return (pdev->ar ? pdev->ar : NULL);
858 	}
859 
860 	return NULL;
861 }
862 
863 static bool ath12k_mac_is_ml_arvif(struct ath12k_link_vif *arvif)
864 {
865 	struct ath12k_vif *ahvif = arvif->ahvif;
866 
867 	lockdep_assert_wiphy(ahvif->ah->hw->wiphy);
868 
869 	if (ahvif->vif->valid_links & BIT(arvif->link_id))
870 		return true;
871 
872 	return false;
873 }
874 
875 static struct ath12k *ath12k_mac_get_ar_by_chan(struct ieee80211_hw *hw,
876 						struct ieee80211_channel *channel)
877 {
878 	struct ath12k_hw *ah = hw->priv;
879 	struct ath12k *ar;
880 	int i;
881 
882 	ar = ah->radio;
883 
884 	if (ah->num_radio == 1)
885 		return ar;
886 
887 	for_each_ar(ah, ar, i) {
888 		if (channel->center_freq >= KHZ_TO_MHZ(ar->freq_range.start_freq) &&
889 		    channel->center_freq <= KHZ_TO_MHZ(ar->freq_range.end_freq))
890 			return ar;
891 	}
892 	return NULL;
893 }
894 
895 static struct ath12k *ath12k_get_ar_by_ctx(struct ieee80211_hw *hw,
896 					   struct ieee80211_chanctx_conf *ctx)
897 {
898 	if (!ctx)
899 		return NULL;
900 
901 	return ath12k_mac_get_ar_by_chan(hw, ctx->def.chan);
902 }
903 
904 struct ath12k *ath12k_get_ar_by_vif(struct ieee80211_hw *hw,
905 				    struct ieee80211_vif *vif,
906 				    u8 link_id)
907 {
908 	struct ath12k_vif *ahvif = ath12k_vif_to_ahvif(vif);
909 	struct ath12k_hw *ah = ath12k_hw_to_ah(hw);
910 	struct ath12k_link_vif *arvif;
911 
912 	lockdep_assert_wiphy(hw->wiphy);
913 
914 	/* If there is one pdev within ah, then we return
915 	 * ar directly.
916 	 */
917 	if (ah->num_radio == 1)
918 		return ah->radio;
919 
920 	if (!(ahvif->links_map & BIT(link_id)))
921 		return NULL;
922 
923 	arvif = wiphy_dereference(hw->wiphy, ahvif->link[link_id]);
924 	if (arvif && arvif->is_created)
925 		return arvif->ar;
926 
927 	return NULL;
928 }
929 
930 void ath12k_mac_get_any_chanctx_conf_iter(struct ieee80211_hw *hw,
931 					  struct ieee80211_chanctx_conf *conf,
932 					  void *data)
933 {
934 	struct ath12k_mac_get_any_chanctx_conf_arg *arg = data;
935 	struct ath12k *ctx_ar = ath12k_get_ar_by_ctx(hw, conf);
936 
937 	if (ctx_ar == arg->ar)
938 		arg->chanctx_conf = conf;
939 }
940 
941 static struct ath12k_link_vif *ath12k_mac_get_vif_up(struct ath12k *ar)
942 {
943 	struct ath12k_link_vif *arvif;
944 
945 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
946 
947 	list_for_each_entry(arvif, &ar->arvifs, list) {
948 		if (arvif->is_up)
949 			return arvif;
950 	}
951 
952 	return NULL;
953 }
954 
955 static bool ath12k_mac_band_match(enum nl80211_band band1, enum WMI_HOST_WLAN_BAND band2)
956 {
957 	switch (band1) {
958 	case NL80211_BAND_2GHZ:
959 		if (band2 & WMI_HOST_WLAN_2GHZ_CAP)
960 			return true;
961 		break;
962 	case NL80211_BAND_5GHZ:
963 	case NL80211_BAND_6GHZ:
964 		if (band2 & WMI_HOST_WLAN_5GHZ_CAP)
965 			return true;
966 		break;
967 	default:
968 		return false;
969 	}
970 
971 	return false;
972 }
973 
974 static u8 ath12k_mac_get_target_pdev_id_from_vif(struct ath12k_link_vif *arvif)
975 {
976 	struct ath12k *ar = arvif->ar;
977 	struct ath12k_base *ab = ar->ab;
978 	struct ieee80211_vif *vif = arvif->ahvif->vif;
979 	struct cfg80211_chan_def def;
980 	enum nl80211_band band;
981 	u8 pdev_id = ab->fw_pdev[0].pdev_id;
982 	int i;
983 
984 	if (WARN_ON(ath12k_mac_vif_link_chan(vif, arvif->link_id, &def)))
985 		return pdev_id;
986 
987 	band = def.chan->band;
988 
989 	for (i = 0; i < ab->fw_pdev_count; i++) {
990 		if (ath12k_mac_band_match(band, ab->fw_pdev[i].supported_bands))
991 			return ab->fw_pdev[i].pdev_id;
992 	}
993 
994 	return pdev_id;
995 }
996 
997 u8 ath12k_mac_get_target_pdev_id(struct ath12k *ar)
998 {
999 	struct ath12k_link_vif *arvif;
1000 	struct ath12k_base *ab = ar->ab;
1001 
1002 	if (!ab->hw_params->single_pdev_only)
1003 		return ar->pdev->pdev_id;
1004 
1005 	arvif = ath12k_mac_get_vif_up(ar);
1006 
1007 	/* fw_pdev array has pdev ids derived from phy capability
1008 	 * service ready event (pdev_and_hw_link_ids).
1009 	 * If no vif is active, return default first index.
1010 	 */
1011 	if (!arvif)
1012 		return ar->ab->fw_pdev[0].pdev_id;
1013 
1014 	/* If active vif is found, return the pdev id matching chandef band */
1015 	return ath12k_mac_get_target_pdev_id_from_vif(arvif);
1016 }
1017 
1018 static void ath12k_pdev_caps_update(struct ath12k *ar)
1019 {
1020 	struct ath12k_base *ab = ar->ab;
1021 
1022 	ar->max_tx_power = ab->target_caps.hw_max_tx_power;
1023 
1024 	/* FIXME: Set min_tx_power to ab->target_caps.hw_min_tx_power.
1025 	 * But since the received value in svcrdy is same as hw_max_tx_power,
1026 	 * we can set ar->min_tx_power to 0 currently until
1027 	 * this is fixed in firmware
1028 	 */
1029 	ar->min_tx_power = 0;
1030 
1031 	ar->txpower_limit_2g = ar->max_tx_power;
1032 	ar->txpower_limit_5g = ar->max_tx_power;
1033 	ar->txpower_scale = WMI_HOST_TP_SCALE_MAX;
1034 }
1035 
1036 static int ath12k_mac_txpower_recalc(struct ath12k *ar)
1037 {
1038 	struct ath12k_pdev *pdev = ar->pdev;
1039 	struct ath12k_link_vif *arvif;
1040 	int ret, txpower = -1;
1041 	u32 param;
1042 
1043 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
1044 
1045 	list_for_each_entry(arvif, &ar->arvifs, list) {
1046 		if (arvif->txpower <= 0)
1047 			continue;
1048 
1049 		if (txpower == -1)
1050 			txpower = arvif->txpower;
1051 		else
1052 			txpower = min(txpower, arvif->txpower);
1053 	}
1054 
1055 	if (txpower == -1)
1056 		return 0;
1057 
1058 	/* txpwr is set as 2 units per dBm in FW*/
1059 	txpower = min_t(u32, max_t(u32, ar->min_tx_power, txpower),
1060 			ar->max_tx_power) * 2;
1061 
1062 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "txpower to set in hw %d\n",
1063 		   txpower / 2);
1064 
1065 	if ((pdev->cap.supported_bands & WMI_HOST_WLAN_2GHZ_CAP) &&
1066 	    ar->txpower_limit_2g != txpower) {
1067 		param = WMI_PDEV_PARAM_TXPOWER_LIMIT2G;
1068 		ret = ath12k_wmi_pdev_set_param(ar, param,
1069 						txpower, ar->pdev->pdev_id);
1070 		if (ret)
1071 			goto fail;
1072 		ar->txpower_limit_2g = txpower;
1073 	}
1074 
1075 	if ((pdev->cap.supported_bands & WMI_HOST_WLAN_5GHZ_CAP) &&
1076 	    ar->txpower_limit_5g != txpower) {
1077 		param = WMI_PDEV_PARAM_TXPOWER_LIMIT5G;
1078 		ret = ath12k_wmi_pdev_set_param(ar, param,
1079 						txpower, ar->pdev->pdev_id);
1080 		if (ret)
1081 			goto fail;
1082 		ar->txpower_limit_5g = txpower;
1083 	}
1084 
1085 	return 0;
1086 
1087 fail:
1088 	ath12k_warn(ar->ab, "failed to recalc txpower limit %d using pdev param %d: %d\n",
1089 		    txpower / 2, param, ret);
1090 	return ret;
1091 }
1092 
1093 static int ath12k_recalc_rtscts_prot(struct ath12k_link_vif *arvif)
1094 {
1095 	struct ath12k *ar = arvif->ar;
1096 	u32 vdev_param, rts_cts;
1097 	int ret;
1098 
1099 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
1100 
1101 	vdev_param = WMI_VDEV_PARAM_ENABLE_RTSCTS;
1102 
1103 	/* Enable RTS/CTS protection for sw retries (when legacy stations
1104 	 * are in BSS) or by default only for second rate series.
1105 	 * TODO: Check if we need to enable CTS 2 Self in any case
1106 	 */
1107 	rts_cts = WMI_USE_RTS_CTS;
1108 
1109 	if (arvif->num_legacy_stations > 0)
1110 		rts_cts |= WMI_RTSCTS_ACROSS_SW_RETRIES << 4;
1111 	else
1112 		rts_cts |= WMI_RTSCTS_FOR_SECOND_RATESERIES << 4;
1113 
1114 	/* Need not send duplicate param value to firmware */
1115 	if (arvif->rtscts_prot_mode == rts_cts)
1116 		return 0;
1117 
1118 	arvif->rtscts_prot_mode = rts_cts;
1119 
1120 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac vdev %d recalc rts/cts prot %d\n",
1121 		   arvif->vdev_id, rts_cts);
1122 
1123 	ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
1124 					    vdev_param, rts_cts);
1125 	if (ret)
1126 		ath12k_warn(ar->ab, "failed to recalculate rts/cts prot for vdev %d: %d\n",
1127 			    arvif->vdev_id, ret);
1128 
1129 	return ret;
1130 }
1131 
1132 static int ath12k_mac_set_kickout(struct ath12k_link_vif *arvif)
1133 {
1134 	struct ath12k *ar = arvif->ar;
1135 	u32 param;
1136 	int ret;
1137 
1138 	ret = ath12k_wmi_pdev_set_param(ar, WMI_PDEV_PARAM_STA_KICKOUT_TH,
1139 					ATH12K_KICKOUT_THRESHOLD,
1140 					ar->pdev->pdev_id);
1141 	if (ret) {
1142 		ath12k_warn(ar->ab, "failed to set kickout threshold on vdev %i: %d\n",
1143 			    arvif->vdev_id, ret);
1144 		return ret;
1145 	}
1146 
1147 	param = WMI_VDEV_PARAM_AP_KEEPALIVE_MIN_IDLE_INACTIVE_TIME_SECS;
1148 	ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id, param,
1149 					    ATH12K_KEEPALIVE_MIN_IDLE);
1150 	if (ret) {
1151 		ath12k_warn(ar->ab, "failed to set keepalive minimum idle time on vdev %i: %d\n",
1152 			    arvif->vdev_id, ret);
1153 		return ret;
1154 	}
1155 
1156 	param = WMI_VDEV_PARAM_AP_KEEPALIVE_MAX_IDLE_INACTIVE_TIME_SECS;
1157 	ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id, param,
1158 					    ATH12K_KEEPALIVE_MAX_IDLE);
1159 	if (ret) {
1160 		ath12k_warn(ar->ab, "failed to set keepalive maximum idle time on vdev %i: %d\n",
1161 			    arvif->vdev_id, ret);
1162 		return ret;
1163 	}
1164 
1165 	param = WMI_VDEV_PARAM_AP_KEEPALIVE_MAX_UNRESPONSIVE_TIME_SECS;
1166 	ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id, param,
1167 					    ATH12K_KEEPALIVE_MAX_UNRESPONSIVE);
1168 	if (ret) {
1169 		ath12k_warn(ar->ab, "failed to set keepalive maximum unresponsive time on vdev %i: %d\n",
1170 			    arvif->vdev_id, ret);
1171 		return ret;
1172 	}
1173 
1174 	return 0;
1175 }
1176 
1177 static void ath12k_mac_link_sta_rhash_cleanup(void *data, struct ieee80211_sta *sta)
1178 {
1179 	u8 link_id;
1180 	unsigned long links_map;
1181 	struct ath12k_sta *ahsta;
1182 	struct ath12k *ar = data;
1183 	struct ath12k_link_sta *arsta;
1184 	struct ath12k_link_vif *arvif;
1185 	struct ath12k_base *ab = ar->ab;
1186 
1187 	ahsta = ath12k_sta_to_ahsta(sta);
1188 	links_map = ahsta->links_map;
1189 
1190 	rcu_read_lock();
1191 	for_each_set_bit(link_id, &links_map, IEEE80211_MLD_MAX_NUM_LINKS) {
1192 		arsta = rcu_dereference(ahsta->link[link_id]);
1193 		if (!arsta)
1194 			continue;
1195 		arvif = arsta->arvif;
1196 		if (!(arvif->ar == ar))
1197 			continue;
1198 
1199 		spin_lock_bh(&ab->base_lock);
1200 		ath12k_link_sta_rhash_delete(ab, arsta);
1201 		spin_unlock_bh(&ab->base_lock);
1202 	}
1203 	rcu_read_unlock();
1204 }
1205 
1206 void ath12k_mac_peer_cleanup_all(struct ath12k *ar)
1207 {
1208 	struct ath12k_dp_link_peer *peer, *tmp;
1209 	struct ath12k_base *ab = ar->ab;
1210 	struct ath12k_dp *dp = ath12k_ab_to_dp(ab);
1211 	struct ath12k_link_vif *arvif, *tmp_vif;
1212 	struct ath12k_dp_hw *dp_hw = &ar->ah->dp_hw;
1213 	struct ath12k_dp_peer *dp_peer = NULL;
1214 	u16 peerid_index;
1215 	struct list_head peers;
1216 
1217 	INIT_LIST_HEAD(&peers);
1218 
1219 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
1220 
1221 	spin_lock_bh(&dp->dp_lock);
1222 	list_for_each_entry_safe(peer, tmp, &dp->peers, list) {
1223 		/* Skip Rx TID cleanup for self peer */
1224 		if (peer->sta && peer->dp_peer)
1225 			ath12k_dp_rx_peer_tid_cleanup(ar, peer);
1226 
1227 		/* cleanup dp peer */
1228 		spin_lock_bh(&dp_hw->peer_lock);
1229 		dp_peer = peer->dp_peer;
1230 		peerid_index = ath12k_dp_peer_get_peerid_index(dp, peer->peer_id);
1231 		rcu_assign_pointer(dp_peer->link_peers[peer->link_id], NULL);
1232 		rcu_assign_pointer(dp_hw->dp_peers[peerid_index], NULL);
1233 		spin_unlock_bh(&dp_hw->peer_lock);
1234 
1235 		ath12k_dp_link_peer_rhash_delete(dp, peer);
1236 
1237 		list_move(&peer->list, &peers);
1238 	}
1239 	spin_unlock_bh(&dp->dp_lock);
1240 
1241 	synchronize_rcu();
1242 
1243 	list_for_each_entry_safe(peer, tmp, &peers, list) {
1244 		ath12k_dp_link_peer_free(peer);
1245 	}
1246 
1247 	ar->num_peers = 0;
1248 	ar->num_stations = 0;
1249 
1250 	/* Cleanup rhash table maintained for arsta by iterating over sta */
1251 	ieee80211_iterate_stations_mtx(ar->ah->hw, ath12k_mac_link_sta_rhash_cleanup,
1252 				       ar);
1253 
1254 	/* Delete all the self dp_peers on asserted radio */
1255 	list_for_each_entry_safe_reverse(arvif, tmp_vif, &ar->arvifs, list) {
1256 		if ((arvif->ahvif->vdev_type == WMI_VDEV_TYPE_AP) &&
1257 		    (arvif->link_id < IEEE80211_MLD_MAX_NUM_LINKS)) {
1258 			ath12k_dp_peer_delete(dp_hw, arvif->bssid, NULL);
1259 			arvif->num_stations = 0;
1260 		}
1261 	}
1262 }
1263 
1264 void ath12k_mac_dp_peer_cleanup(struct ath12k_hw *ah)
1265 {
1266 	struct list_head peers;
1267 	struct ath12k_dp_peer *dp_peer, *tmp;
1268 	struct ath12k_dp_hw *dp_hw = &ah->dp_hw;
1269 
1270 	INIT_LIST_HEAD(&peers);
1271 
1272 	spin_lock_bh(&dp_hw->peer_lock);
1273 	list_for_each_entry_safe(dp_peer, tmp, &dp_hw->dp_peers_list, list) {
1274 		if (dp_peer->is_mlo) {
1275 			rcu_assign_pointer(dp_hw->dp_peers[dp_peer->peer_id], NULL);
1276 			clear_bit(dp_peer->peer_id, ah->free_ml_peer_id_map);
1277 		}
1278 
1279 		list_move(&dp_peer->list, &peers);
1280 	}
1281 
1282 	spin_unlock_bh(&dp_hw->peer_lock);
1283 
1284 	synchronize_rcu();
1285 
1286 	list_for_each_entry_safe(dp_peer, tmp, &peers, list) {
1287 		list_del(&dp_peer->list);
1288 		kfree(dp_peer);
1289 	}
1290 }
1291 
1292 static int ath12k_mac_vdev_setup_sync(struct ath12k *ar)
1293 {
1294 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
1295 
1296 	if (test_bit(ATH12K_FLAG_CRASH_FLUSH, &ar->ab->dev_flags))
1297 		return -ESHUTDOWN;
1298 
1299 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "vdev setup timeout %d\n",
1300 		   ATH12K_VDEV_SETUP_TIMEOUT_HZ);
1301 
1302 	if (!wait_for_completion_timeout(&ar->vdev_setup_done,
1303 					 ATH12K_VDEV_SETUP_TIMEOUT_HZ))
1304 		return -ETIMEDOUT;
1305 
1306 	return ar->last_wmi_vdev_start_status ? -EINVAL : 0;
1307 }
1308 
1309 static int ath12k_monitor_vdev_up(struct ath12k *ar, int vdev_id)
1310 {
1311 	struct ath12k_wmi_vdev_up_params params = {};
1312 	int ret;
1313 
1314 	params.vdev_id = vdev_id;
1315 	params.bssid = ar->mac_addr;
1316 	ret = ath12k_wmi_vdev_up(ar, &params);
1317 	if (ret) {
1318 		ath12k_warn(ar->ab, "failed to put up monitor vdev %i: %d\n",
1319 			    vdev_id, ret);
1320 		return ret;
1321 	}
1322 
1323 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac monitor vdev %i started\n",
1324 		   vdev_id);
1325 	return 0;
1326 }
1327 
1328 static int ath12k_mac_monitor_vdev_start(struct ath12k *ar, int vdev_id,
1329 					 struct cfg80211_chan_def *chandef)
1330 {
1331 	struct ieee80211_channel *channel;
1332 	struct wmi_vdev_start_req_arg arg = {};
1333 	struct ath12k_wmi_vdev_up_params params = {};
1334 	int ret;
1335 
1336 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
1337 
1338 	channel = chandef->chan;
1339 	arg.vdev_id = vdev_id;
1340 	arg.freq = channel->center_freq;
1341 	arg.band_center_freq1 = chandef->center_freq1;
1342 	arg.band_center_freq2 = chandef->center_freq2;
1343 	arg.mode = ath12k_phymodes[chandef->chan->band][chandef->width];
1344 	arg.chan_radar = !!(channel->flags & IEEE80211_CHAN_RADAR);
1345 
1346 	arg.min_power = 0;
1347 	arg.max_power = channel->max_power;
1348 	arg.max_reg_power = channel->max_reg_power;
1349 	arg.max_antenna_gain = channel->max_antenna_gain;
1350 
1351 	arg.pref_tx_streams = ar->num_tx_chains;
1352 	arg.pref_rx_streams = ar->num_rx_chains;
1353 	arg.punct_bitmap = 0xFFFFFFFF;
1354 
1355 	arg.passive |= !!(chandef->chan->flags & IEEE80211_CHAN_NO_IR);
1356 
1357 	reinit_completion(&ar->vdev_setup_done);
1358 	reinit_completion(&ar->vdev_delete_done);
1359 
1360 	ret = ath12k_wmi_vdev_start(ar, &arg, false);
1361 	if (ret) {
1362 		ath12k_warn(ar->ab, "failed to request monitor vdev %i start: %d\n",
1363 			    vdev_id, ret);
1364 		return ret;
1365 	}
1366 
1367 	ret = ath12k_mac_vdev_setup_sync(ar);
1368 	if (ret) {
1369 		ath12k_warn(ar->ab, "failed to synchronize setup for monitor vdev %i start: %d\n",
1370 			    vdev_id, ret);
1371 		return ret;
1372 	}
1373 
1374 	params.vdev_id = vdev_id;
1375 	params.bssid = ar->mac_addr;
1376 	ret = ath12k_wmi_vdev_up(ar, &params);
1377 	if (ret) {
1378 		ath12k_warn(ar->ab, "failed to put up monitor vdev %i: %d\n",
1379 			    vdev_id, ret);
1380 		goto vdev_stop;
1381 	}
1382 
1383 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac monitor vdev %i started\n",
1384 		   vdev_id);
1385 	return 0;
1386 
1387 vdev_stop:
1388 	ret = ath12k_wmi_vdev_stop(ar, vdev_id);
1389 	if (ret)
1390 		ath12k_warn(ar->ab, "failed to stop monitor vdev %i after start failure: %d\n",
1391 			    vdev_id, ret);
1392 	return ret;
1393 }
1394 
1395 static int ath12k_mac_monitor_vdev_stop(struct ath12k *ar)
1396 {
1397 	int ret;
1398 
1399 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
1400 
1401 	reinit_completion(&ar->vdev_setup_done);
1402 
1403 	ret = ath12k_wmi_vdev_stop(ar, ar->monitor_vdev_id);
1404 	if (ret)
1405 		ath12k_warn(ar->ab, "failed to request monitor vdev %i stop: %d\n",
1406 			    ar->monitor_vdev_id, ret);
1407 
1408 	ret = ath12k_mac_vdev_setup_sync(ar);
1409 	if (ret)
1410 		ath12k_warn(ar->ab, "failed to synchronize monitor vdev %i stop: %d\n",
1411 			    ar->monitor_vdev_id, ret);
1412 
1413 	ret = ath12k_wmi_vdev_down(ar, ar->monitor_vdev_id);
1414 	if (ret)
1415 		ath12k_warn(ar->ab, "failed to put down monitor vdev %i: %d\n",
1416 			    ar->monitor_vdev_id, ret);
1417 
1418 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac monitor vdev %i stopped\n",
1419 		   ar->monitor_vdev_id);
1420 	return ret;
1421 }
1422 
1423 static int ath12k_mac_monitor_vdev_delete(struct ath12k *ar)
1424 {
1425 	int ret;
1426 	unsigned long time_left;
1427 
1428 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
1429 
1430 	if (!ar->monitor_vdev_created)
1431 		return 0;
1432 
1433 	reinit_completion(&ar->vdev_delete_done);
1434 
1435 	ret = ath12k_wmi_vdev_delete(ar, ar->monitor_vdev_id);
1436 	if (ret) {
1437 		ath12k_warn(ar->ab, "failed to request wmi monitor vdev %i removal: %d\n",
1438 			    ar->monitor_vdev_id, ret);
1439 		return ret;
1440 	}
1441 
1442 	time_left = wait_for_completion_timeout(&ar->vdev_delete_done,
1443 						ATH12K_VDEV_DELETE_TIMEOUT_HZ);
1444 	if (time_left == 0) {
1445 		ath12k_warn(ar->ab, "Timeout in receiving vdev delete response\n");
1446 	} else {
1447 		ar->allocated_vdev_map &= ~(1LL << ar->monitor_vdev_id);
1448 		ar->ab->free_vdev_map |= 1LL << (ar->monitor_vdev_id);
1449 		ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac monitor vdev %d deleted\n",
1450 			   ar->monitor_vdev_id);
1451 		ar->num_created_vdevs--;
1452 		ar->monitor_vdev_id = -1;
1453 		ar->monitor_vdev_created = false;
1454 	}
1455 
1456 	return ret;
1457 }
1458 
1459 static int ath12k_mac_monitor_start(struct ath12k *ar)
1460 {
1461 	struct ath12k_mac_get_any_chanctx_conf_arg arg;
1462 	int ret;
1463 
1464 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
1465 
1466 	if (ar->monitor_started)
1467 		return 0;
1468 
1469 	arg.ar = ar;
1470 	arg.chanctx_conf = NULL;
1471 	ieee80211_iter_chan_contexts_atomic(ath12k_ar_to_hw(ar),
1472 					    ath12k_mac_get_any_chanctx_conf_iter,
1473 					    &arg);
1474 	if (!arg.chanctx_conf)
1475 		return 0;
1476 
1477 	ret = ath12k_mac_monitor_vdev_start(ar, ar->monitor_vdev_id,
1478 					    &arg.chanctx_conf->def);
1479 	if (ret) {
1480 		ath12k_warn(ar->ab, "failed to start monitor vdev: %d\n", ret);
1481 		return ret;
1482 	}
1483 
1484 	ret = ath12k_dp_tx_htt_monitor_mode_ring_config(ar, false);
1485 	if (ret) {
1486 		ath12k_warn(ar->ab, "fail to set monitor filter: %d\n", ret);
1487 		return ret;
1488 	}
1489 
1490 	ar->monitor_started = true;
1491 	ar->num_started_vdevs++;
1492 
1493 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac monitor started\n");
1494 
1495 	return 0;
1496 }
1497 
1498 static int ath12k_mac_monitor_stop(struct ath12k *ar)
1499 {
1500 	int ret;
1501 
1502 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
1503 
1504 	if (!ar->monitor_started)
1505 		return 0;
1506 
1507 	ret = ath12k_mac_monitor_vdev_stop(ar);
1508 	if (ret) {
1509 		ath12k_warn(ar->ab, "failed to stop monitor vdev: %d\n", ret);
1510 		return ret;
1511 	}
1512 
1513 	ar->monitor_started = false;
1514 	ar->num_started_vdevs--;
1515 	ret = ath12k_dp_tx_htt_monitor_mode_ring_config(ar, true);
1516 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac monitor stopped ret %d\n", ret);
1517 	return ret;
1518 }
1519 
1520 int ath12k_mac_vdev_stop(struct ath12k_link_vif *arvif)
1521 {
1522 	struct ath12k_vif *ahvif = arvif->ahvif;
1523 	struct ath12k *ar = arvif->ar;
1524 	int ret;
1525 
1526 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
1527 
1528 	reinit_completion(&ar->vdev_setup_done);
1529 
1530 	ret = ath12k_wmi_vdev_stop(ar, arvif->vdev_id);
1531 	if (ret) {
1532 		ath12k_warn(ar->ab, "failed to stop WMI vdev %i: %d\n",
1533 			    arvif->vdev_id, ret);
1534 		goto err;
1535 	}
1536 
1537 	ret = ath12k_mac_vdev_setup_sync(ar);
1538 	if (ret) {
1539 		ath12k_warn(ar->ab, "failed to synchronize setup for vdev %i: %d\n",
1540 			    arvif->vdev_id, ret);
1541 		goto err;
1542 	}
1543 
1544 	WARN_ON(ar->num_started_vdevs == 0);
1545 
1546 	ar->num_started_vdevs--;
1547 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "vdev %pM stopped, vdev_id %d\n",
1548 		   ahvif->vif->addr, arvif->vdev_id);
1549 
1550 	if (test_bit(ATH12K_FLAG_CAC_RUNNING, &ar->dev_flags)) {
1551 		clear_bit(ATH12K_FLAG_CAC_RUNNING, &ar->dev_flags);
1552 		ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "CAC Stopped for vdev %d\n",
1553 			   arvif->vdev_id);
1554 	}
1555 
1556 	return 0;
1557 err:
1558 	return ret;
1559 }
1560 
1561 int ath12k_mac_op_config(struct ieee80211_hw *hw, int radio_idx, u32 changed)
1562 {
1563 	return 0;
1564 }
1565 EXPORT_SYMBOL(ath12k_mac_op_config);
1566 
1567 static int ath12k_mac_setup_bcn_p2p_ie(struct ath12k_link_vif *arvif,
1568 				       struct sk_buff *bcn)
1569 {
1570 	struct ath12k *ar = arvif->ar;
1571 	struct ieee80211_mgmt *mgmt;
1572 	const u8 *p2p_ie;
1573 	int ret;
1574 
1575 	mgmt = (void *)bcn->data;
1576 	p2p_ie = cfg80211_find_vendor_ie(WLAN_OUI_WFA, WLAN_OUI_TYPE_WFA_P2P,
1577 					 mgmt->u.beacon.variable,
1578 					 bcn->len - (mgmt->u.beacon.variable -
1579 						     bcn->data));
1580 	if (!p2p_ie) {
1581 		ath12k_warn(ar->ab, "no P2P ie found in beacon\n");
1582 		return -ENOENT;
1583 	}
1584 
1585 	ret = ath12k_wmi_p2p_go_bcn_ie(ar, arvif->vdev_id, p2p_ie);
1586 	if (ret) {
1587 		ath12k_warn(ar->ab, "failed to submit P2P GO bcn ie for vdev %i: %d\n",
1588 			    arvif->vdev_id, ret);
1589 		return ret;
1590 	}
1591 
1592 	return 0;
1593 }
1594 
1595 static int ath12k_mac_remove_vendor_ie(struct sk_buff *skb, unsigned int oui,
1596 				       u8 oui_type, size_t ie_offset)
1597 {
1598 	const u8 *next, *end;
1599 	size_t len;
1600 	u8 *ie;
1601 
1602 	if (WARN_ON(skb->len < ie_offset))
1603 		return -EINVAL;
1604 
1605 	ie = (u8 *)cfg80211_find_vendor_ie(oui, oui_type,
1606 					   skb->data + ie_offset,
1607 					   skb->len - ie_offset);
1608 	if (!ie)
1609 		return -ENOENT;
1610 
1611 	len = ie[1] + 2;
1612 	end = skb->data + skb->len;
1613 	next = ie + len;
1614 
1615 	if (WARN_ON(next > end))
1616 		return -EINVAL;
1617 
1618 	memmove(ie, next, end - next);
1619 	skb_trim(skb, skb->len - len);
1620 
1621 	return 0;
1622 }
1623 
1624 static void ath12k_mac_set_arvif_ies(struct ath12k_link_vif *arvif,
1625 				     struct ath12k_link_vif *tx_arvif,
1626 				     struct sk_buff *bcn,
1627 				     u8 bssid_index, bool *nontx_profile_found)
1628 {
1629 	struct ieee80211_mgmt *mgmt = (struct ieee80211_mgmt *)bcn->data;
1630 	const struct element *elem, *nontx, *index, *nie, *ext_cap_ie;
1631 	const u8 *start, *tail;
1632 	u16 rem_len;
1633 	u8 i;
1634 
1635 	start = bcn->data + ieee80211_get_hdrlen_from_skb(bcn) + sizeof(mgmt->u.beacon);
1636 	tail = skb_tail_pointer(bcn);
1637 	rem_len = tail - start;
1638 
1639 	arvif->rsnie_present = false;
1640 	arvif->wpaie_present = false;
1641 
1642 	if (cfg80211_find_ie(WLAN_EID_RSN, start, rem_len))
1643 		arvif->rsnie_present = true;
1644 	if (cfg80211_find_vendor_ie(WLAN_OUI_MICROSOFT, WLAN_OUI_TYPE_MICROSOFT_WPA,
1645 				    start, rem_len))
1646 		arvif->wpaie_present = true;
1647 
1648 	ext_cap_ie = cfg80211_find_elem(WLAN_EID_EXT_CAPABILITY, start, rem_len);
1649 	if (ext_cap_ie && ext_cap_ie->datalen >= 11 &&
1650 	    (ext_cap_ie->data[10] & WLAN_EXT_CAPA11_BCN_PROTECT))
1651 		tx_arvif->beacon_prot = true;
1652 
1653 	/* Return from here for the transmitted profile */
1654 	if (!bssid_index)
1655 		return;
1656 
1657 	/* Initial rsnie_present for the nontransmitted profile is set to be same as that
1658 	 * of the transmitted profile. It will be changed if security configurations are
1659 	 * different.
1660 	 */
1661 	*nontx_profile_found = false;
1662 	for_each_element_id(elem, WLAN_EID_MULTIPLE_BSSID, start, rem_len) {
1663 		/* Fixed minimum MBSSID element length with at least one
1664 		 * nontransmitted BSSID profile is 12 bytes as given below;
1665 		 * 1 (max BSSID indicator) +
1666 		 * 2 (Nontransmitted BSSID profile: Subelement ID + length) +
1667 		 * 4 (Nontransmitted BSSID Capabilities: tag + length + info)
1668 		 * 2 (Nontransmitted BSSID SSID: tag + length)
1669 		 * 3 (Nontransmitted BSSID Index: tag + length + BSSID index
1670 		 */
1671 		if (elem->datalen < 12 || elem->data[0] < 1)
1672 			continue; /* Max BSSID indicator must be >=1 */
1673 
1674 		for_each_element(nontx, elem->data + 1, elem->datalen - 1) {
1675 			start = nontx->data;
1676 
1677 			if (nontx->id != 0 || nontx->datalen < 4)
1678 				continue; /* Invalid nontransmitted profile */
1679 
1680 			if (nontx->data[0] != WLAN_EID_NON_TX_BSSID_CAP ||
1681 			    nontx->data[1] != 2) {
1682 				continue; /* Missing nontransmitted BSS capabilities */
1683 			}
1684 
1685 			if (nontx->data[4] != WLAN_EID_SSID)
1686 				continue; /* Missing SSID for nontransmitted BSS */
1687 
1688 			index = cfg80211_find_elem(WLAN_EID_MULTI_BSSID_IDX,
1689 						   start, nontx->datalen);
1690 			if (!index || index->datalen < 1 || index->data[0] == 0)
1691 				continue; /* Invalid MBSSID Index element */
1692 
1693 			if (index->data[0] == bssid_index) {
1694 				*nontx_profile_found = true;
1695 
1696 				/* Check if nontx BSS has beacon protection enabled */
1697 				if (!tx_arvif->beacon_prot) {
1698 					ext_cap_ie =
1699 					    cfg80211_find_elem(WLAN_EID_EXT_CAPABILITY,
1700 							       nontx->data,
1701 							       nontx->datalen);
1702 					if (ext_cap_ie && ext_cap_ie->datalen >= 11 &&
1703 					    (ext_cap_ie->data[10] &
1704 					     WLAN_EXT_CAPA11_BCN_PROTECT))
1705 						tx_arvif->beacon_prot = true;
1706 				}
1707 
1708 				if (cfg80211_find_ie(WLAN_EID_RSN,
1709 						     nontx->data,
1710 						     nontx->datalen)) {
1711 					arvif->rsnie_present = true;
1712 					return;
1713 				} else if (!arvif->rsnie_present) {
1714 					return; /* Both tx and nontx BSS are open */
1715 				}
1716 
1717 				nie = cfg80211_find_ext_elem(WLAN_EID_EXT_NON_INHERITANCE,
1718 							     nontx->data,
1719 							     nontx->datalen);
1720 				if (!nie || nie->datalen < 2)
1721 					return; /* Invalid non-inheritance element */
1722 
1723 				for (i = 1; i < nie->datalen - 1; i++) {
1724 					if (nie->data[i] == WLAN_EID_RSN) {
1725 						arvif->rsnie_present = false;
1726 						break;
1727 					}
1728 				}
1729 
1730 				return;
1731 			}
1732 		}
1733 	}
1734 }
1735 
1736 static int ath12k_mac_setup_bcn_tmpl_ema(struct ath12k_link_vif *arvif,
1737 					 struct ath12k_link_vif *tx_arvif,
1738 					 u8 bssid_index)
1739 {
1740 	struct ath12k_wmi_bcn_tmpl_ema_arg ema_args;
1741 	struct ieee80211_ema_beacons *beacons;
1742 	bool nontx_profile_found = false;
1743 	int ret = 0;
1744 	u8 i;
1745 
1746 	beacons = ieee80211_beacon_get_template_ema_list(ath12k_ar_to_hw(tx_arvif->ar),
1747 							 tx_arvif->ahvif->vif,
1748 							 tx_arvif->link_id);
1749 	if (!beacons || !beacons->cnt) {
1750 		ath12k_warn(arvif->ar->ab,
1751 			    "failed to get ema beacon templates from mac80211\n");
1752 		return -EPERM;
1753 	}
1754 
1755 	if (tx_arvif == arvif)
1756 		ath12k_mac_set_arvif_ies(arvif, tx_arvif, beacons->bcn[0].skb, 0, NULL);
1757 
1758 	for (i = 0; i < beacons->cnt; i++) {
1759 		if (tx_arvif != arvif && !nontx_profile_found)
1760 			ath12k_mac_set_arvif_ies(arvif, tx_arvif, beacons->bcn[i].skb,
1761 						 bssid_index,
1762 						 &nontx_profile_found);
1763 
1764 		ema_args.bcn_cnt = beacons->cnt;
1765 		ema_args.bcn_index = i;
1766 		ret = ath12k_wmi_bcn_tmpl(tx_arvif, &beacons->bcn[i].offs,
1767 					  beacons->bcn[i].skb, &ema_args);
1768 		if (ret) {
1769 			ath12k_warn(tx_arvif->ar->ab,
1770 				    "failed to set ema beacon template id %i error %d\n",
1771 				    i, ret);
1772 			break;
1773 		}
1774 	}
1775 
1776 	if (tx_arvif != arvif && !nontx_profile_found)
1777 		ath12k_warn(arvif->ar->ab,
1778 			    "nontransmitted bssid index %u not found in beacon template\n",
1779 			    bssid_index);
1780 
1781 	ieee80211_beacon_free_ema_list(beacons);
1782 	return ret;
1783 }
1784 
1785 static int ath12k_mac_setup_bcn_tmpl(struct ath12k_link_vif *arvif)
1786 {
1787 	struct ath12k_vif *ahvif = arvif->ahvif;
1788 	struct ieee80211_vif *vif = ath12k_ahvif_to_vif(ahvif);
1789 	struct ieee80211_bss_conf *link_conf;
1790 	struct ath12k_link_vif *tx_arvif;
1791 	struct ath12k *ar = arvif->ar;
1792 	struct ath12k_base *ab = ar->ab;
1793 	struct ieee80211_mutable_offsets offs = {};
1794 	bool nontx_profile_found = false;
1795 	struct sk_buff *bcn;
1796 	int ret;
1797 
1798 	if (ahvif->vdev_type != WMI_VDEV_TYPE_AP)
1799 		return 0;
1800 
1801 	link_conf = ath12k_mac_get_link_bss_conf(arvif);
1802 	if (!link_conf) {
1803 		ath12k_warn(ar->ab, "unable to access bss link conf to set bcn tmpl for vif %pM link %u\n",
1804 			    vif->addr, arvif->link_id);
1805 		return -ENOLINK;
1806 	}
1807 
1808 	tx_arvif = ath12k_mac_get_tx_arvif(arvif, link_conf);
1809 	if (tx_arvif) {
1810 		if (tx_arvif != arvif && arvif->is_up)
1811 			return 0;
1812 
1813 		if (link_conf->ema_ap)
1814 			return ath12k_mac_setup_bcn_tmpl_ema(arvif, tx_arvif,
1815 							     link_conf->bssid_index);
1816 	} else {
1817 		tx_arvif = arvif;
1818 	}
1819 
1820 	bcn = ieee80211_beacon_get_template(ath12k_ar_to_hw(tx_arvif->ar),
1821 					    tx_arvif->ahvif->vif,
1822 					    &offs, tx_arvif->link_id);
1823 	if (!bcn) {
1824 		ath12k_warn(ab, "failed to get beacon template from mac80211\n");
1825 		return -EPERM;
1826 	}
1827 
1828 	if (tx_arvif == arvif) {
1829 		ath12k_mac_set_arvif_ies(arvif, tx_arvif, bcn, 0, NULL);
1830 	} else {
1831 		ath12k_mac_set_arvif_ies(arvif, tx_arvif, bcn,
1832 					 link_conf->bssid_index,
1833 					 &nontx_profile_found);
1834 		if (!nontx_profile_found)
1835 			ath12k_warn(ab,
1836 				    "nontransmitted profile not found in beacon template\n");
1837 	}
1838 
1839 	if (ahvif->vif->type == NL80211_IFTYPE_AP && ahvif->vif->p2p) {
1840 		ret = ath12k_mac_setup_bcn_p2p_ie(arvif, bcn);
1841 		if (ret) {
1842 			ath12k_warn(ab, "failed to setup P2P GO bcn ie: %d\n",
1843 				    ret);
1844 			goto free_bcn_skb;
1845 		}
1846 
1847 		/* P2P IE is inserted by firmware automatically (as
1848 		 * configured above) so remove it from the base beacon
1849 		 * template to avoid duplicate P2P IEs in beacon frames.
1850 		 */
1851 		ret = ath12k_mac_remove_vendor_ie(bcn, WLAN_OUI_WFA,
1852 						  WLAN_OUI_TYPE_WFA_P2P,
1853 						  offsetof(struct ieee80211_mgmt,
1854 							   u.beacon.variable));
1855 		if (ret) {
1856 			ath12k_warn(ab, "failed to remove P2P vendor ie: %d\n",
1857 				    ret);
1858 			goto free_bcn_skb;
1859 		}
1860 	}
1861 
1862 	ret = ath12k_wmi_bcn_tmpl(arvif, &offs, bcn, NULL);
1863 
1864 	if (ret)
1865 		ath12k_warn(ab, "failed to submit beacon template command: %d\n",
1866 			    ret);
1867 
1868 free_bcn_skb:
1869 	kfree_skb(bcn);
1870 	return ret;
1871 }
1872 
1873 static void ath12k_control_beaconing(struct ath12k_link_vif *arvif,
1874 				     struct ieee80211_bss_conf *info)
1875 {
1876 	struct ath12k_wmi_vdev_up_params params = {};
1877 	struct ath12k_vif *ahvif = arvif->ahvif;
1878 	struct ath12k *ar = arvif->ar;
1879 	int ret;
1880 
1881 	lockdep_assert_wiphy(ath12k_ar_to_hw(arvif->ar)->wiphy);
1882 
1883 	if (!info->enable_beacon) {
1884 		ret = ath12k_wmi_vdev_down(ar, arvif->vdev_id);
1885 		if (ret)
1886 			ath12k_warn(ar->ab, "failed to down vdev_id %i: %d\n",
1887 				    arvif->vdev_id, ret);
1888 
1889 		arvif->is_up = false;
1890 		return;
1891 	}
1892 
1893 	/* Install the beacon template to the FW */
1894 	ret = ath12k_mac_setup_bcn_tmpl(arvif);
1895 	if (ret) {
1896 		ath12k_warn(ar->ab, "failed to update bcn tmpl during vdev up: %d\n",
1897 			    ret);
1898 		return;
1899 	}
1900 
1901 	ahvif->aid = 0;
1902 
1903 	ether_addr_copy(arvif->bssid, info->addr);
1904 
1905 	params.vdev_id = arvif->vdev_id;
1906 	params.aid = ahvif->aid;
1907 	params.bssid = arvif->bssid;
1908 	params.tx_bssid = ath12k_mac_get_tx_bssid(arvif);
1909 	if (params.tx_bssid) {
1910 		params.nontx_profile_idx = info->bssid_index;
1911 		params.nontx_profile_cnt = 1 << info->bssid_indicator;
1912 	}
1913 	ret = ath12k_wmi_vdev_up(arvif->ar, &params);
1914 	if (ret) {
1915 		ath12k_warn(ar->ab, "failed to bring up vdev %d: %i\n",
1916 			    arvif->vdev_id, ret);
1917 		return;
1918 	}
1919 
1920 	arvif->is_up = true;
1921 
1922 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac vdev %d up\n", arvif->vdev_id);
1923 }
1924 
1925 static void ath12k_mac_handle_beacon_iter(void *data, u8 *mac,
1926 					  struct ieee80211_vif *vif)
1927 {
1928 	struct sk_buff *skb = data;
1929 	struct ieee80211_mgmt *mgmt = (void *)skb->data;
1930 	struct ath12k_vif *ahvif = ath12k_vif_to_ahvif(vif);
1931 	struct ath12k_link_vif *arvif = &ahvif->deflink;
1932 
1933 	if (vif->type != NL80211_IFTYPE_STATION || !arvif->is_created)
1934 		return;
1935 
1936 	if (!ether_addr_equal(mgmt->bssid, vif->bss_conf.bssid))
1937 		return;
1938 
1939 	cancel_delayed_work(&arvif->connection_loss_work);
1940 }
1941 
1942 void ath12k_mac_handle_beacon(struct ath12k *ar, struct sk_buff *skb)
1943 {
1944 	ieee80211_iterate_active_interfaces_atomic(ath12k_ar_to_hw(ar),
1945 						   IEEE80211_IFACE_ITER_NORMAL,
1946 						   ath12k_mac_handle_beacon_iter,
1947 						   skb);
1948 }
1949 
1950 void ath12k_mac_handle_beacon_miss(struct ath12k *ar,
1951 				   struct ath12k_link_vif *arvif)
1952 {
1953 	struct ieee80211_hw *hw = ath12k_ar_to_hw(ar);
1954 	struct ieee80211_vif *vif = ath12k_ahvif_to_vif(arvif->ahvif);
1955 
1956 	if (!(arvif->is_created && arvif->is_up))
1957 		return;
1958 
1959 	ieee80211_beacon_loss(vif);
1960 
1961 	/* Firmware doesn't report beacon loss events repeatedly. If AP probe
1962 	 * (done by mac80211) succeeds but beacons do not resume then it
1963 	 * doesn't make sense to continue operation. Queue connection loss work
1964 	 * which can be cancelled when beacon is received.
1965 	 */
1966 	ieee80211_queue_delayed_work(hw, &arvif->connection_loss_work,
1967 				     ATH12K_CONNECTION_LOSS_HZ);
1968 }
1969 
1970 static void ath12k_mac_vif_sta_connection_loss_work(struct work_struct *work)
1971 {
1972 	struct ath12k_link_vif *arvif = container_of(work, struct ath12k_link_vif,
1973 						     connection_loss_work.work);
1974 	struct ieee80211_vif *vif = arvif->ahvif->vif;
1975 
1976 	if (!arvif->is_up)
1977 		return;
1978 
1979 	ieee80211_connection_loss(vif);
1980 }
1981 
1982 static void ath12k_peer_assoc_h_basic(struct ath12k *ar,
1983 				      struct ath12k_link_vif *arvif,
1984 				      struct ath12k_link_sta *arsta,
1985 				      struct ath12k_wmi_peer_assoc_arg *arg)
1986 {
1987 	struct ieee80211_vif *vif = ath12k_ahvif_to_vif(arvif->ahvif);
1988 	struct ieee80211_sta *sta = ath12k_ahsta_to_sta(arsta->ahsta);
1989 	struct ieee80211_hw *hw = ath12k_ar_to_hw(ar);
1990 	struct ieee80211_bss_conf *bss_conf;
1991 	u32 aid;
1992 
1993 	lockdep_assert_wiphy(hw->wiphy);
1994 
1995 	if (vif->type == NL80211_IFTYPE_STATION)
1996 		aid = vif->cfg.aid;
1997 	else
1998 		aid = sta->aid;
1999 
2000 	ether_addr_copy(arg->peer_mac, arsta->addr);
2001 	arg->vdev_id = arvif->vdev_id;
2002 	arg->peer_associd = aid;
2003 	arg->auth_flag = true;
2004 	/* TODO: STA WAR in ath10k for listen interval required? */
2005 	arg->peer_listen_intval = hw->conf.listen_interval;
2006 	arg->peer_nss = 1;
2007 
2008 	bss_conf = ath12k_mac_get_link_bss_conf(arvif);
2009 	if (!bss_conf) {
2010 		ath12k_warn(ar->ab, "unable to access bss link conf in peer assoc for vif %pM link %u\n",
2011 			    vif->addr, arvif->link_id);
2012 		return;
2013 	}
2014 
2015 	arg->peer_caps = bss_conf->assoc_capability;
2016 }
2017 
2018 static void ath12k_peer_assoc_h_crypto(struct ath12k *ar,
2019 				       struct ath12k_link_vif *arvif,
2020 				       struct ath12k_link_sta *arsta,
2021 				       struct ath12k_wmi_peer_assoc_arg *arg)
2022 {
2023 	struct ieee80211_vif *vif = ath12k_ahvif_to_vif(arvif->ahvif);
2024 	struct ieee80211_sta *sta = ath12k_ahsta_to_sta(arsta->ahsta);
2025 	struct ieee80211_bss_conf *info;
2026 	struct cfg80211_chan_def def;
2027 	struct cfg80211_bss *bss;
2028 	struct ieee80211_hw *hw = ath12k_ar_to_hw(ar);
2029 	const u8 *rsnie = NULL;
2030 	const u8 *wpaie = NULL;
2031 
2032 	lockdep_assert_wiphy(hw->wiphy);
2033 
2034 	info = ath12k_mac_get_link_bss_conf(arvif);
2035 	if (!info) {
2036 		ath12k_warn(ar->ab, "unable to access bss link conf for peer assoc crypto for vif %pM link %u\n",
2037 			    vif->addr, arvif->link_id);
2038 		return;
2039 	}
2040 
2041 	if (WARN_ON(ath12k_mac_vif_link_chan(vif, arvif->link_id, &def)))
2042 		return;
2043 
2044 	bss = cfg80211_get_bss(hw->wiphy, def.chan, info->bssid, NULL, 0,
2045 			       IEEE80211_BSS_TYPE_ANY, IEEE80211_PRIVACY_ANY);
2046 
2047 	if (arvif->rsnie_present || arvif->wpaie_present) {
2048 		arg->need_ptk_4_way = true;
2049 		if (arvif->wpaie_present)
2050 			arg->need_gtk_2_way = true;
2051 	} else if (bss) {
2052 		const struct cfg80211_bss_ies *ies;
2053 
2054 		rcu_read_lock();
2055 		rsnie = ieee80211_bss_get_ie(bss, WLAN_EID_RSN);
2056 
2057 		ies = rcu_dereference(bss->ies);
2058 
2059 		wpaie = cfg80211_find_vendor_ie(WLAN_OUI_MICROSOFT,
2060 						WLAN_OUI_TYPE_MICROSOFT_WPA,
2061 						ies->data,
2062 						ies->len);
2063 		rcu_read_unlock();
2064 		cfg80211_put_bss(hw->wiphy, bss);
2065 	}
2066 
2067 	/* FIXME: base on RSN IE/WPA IE is a correct idea? */
2068 	if (rsnie || wpaie) {
2069 		ath12k_dbg(ar->ab, ATH12K_DBG_WMI,
2070 			   "%s: rsn ie found\n", __func__);
2071 		arg->need_ptk_4_way = true;
2072 	}
2073 
2074 	if (wpaie) {
2075 		ath12k_dbg(ar->ab, ATH12K_DBG_WMI,
2076 			   "%s: wpa ie found\n", __func__);
2077 		arg->need_gtk_2_way = true;
2078 	}
2079 
2080 	if (sta->mfp) {
2081 		/* TODO: Need to check if FW supports PMF? */
2082 		arg->is_pmf_enabled = true;
2083 	}
2084 
2085 	/* TODO: safe_mode_enabled (bypass 4-way handshake) flag req? */
2086 }
2087 
2088 static void ath12k_peer_assoc_h_rates(struct ath12k *ar,
2089 				      struct ath12k_link_vif *arvif,
2090 				      struct ath12k_link_sta *arsta,
2091 				      struct ath12k_wmi_peer_assoc_arg *arg)
2092 {
2093 	struct ieee80211_vif *vif = ath12k_ahvif_to_vif(arvif->ahvif);
2094 	struct ieee80211_sta *sta = ath12k_ahsta_to_sta(arsta->ahsta);
2095 	struct wmi_rate_set_arg *rateset = &arg->peer_legacy_rates;
2096 	struct ieee80211_link_sta *link_sta;
2097 	struct cfg80211_chan_def def;
2098 	const struct ieee80211_supported_band *sband;
2099 	const struct ieee80211_rate *rates;
2100 	struct ieee80211_hw *hw = ath12k_ar_to_hw(ar);
2101 	enum nl80211_band band;
2102 	u32 ratemask;
2103 	u8 rate;
2104 	int i;
2105 
2106 	lockdep_assert_wiphy(hw->wiphy);
2107 
2108 	if (WARN_ON(ath12k_mac_vif_link_chan(vif, arvif->link_id, &def)))
2109 		return;
2110 
2111 	link_sta = ath12k_mac_get_link_sta(arsta);
2112 	if (!link_sta) {
2113 		ath12k_warn(ar->ab, "unable to access link sta in peer assoc rates for sta %pM link %u\n",
2114 			    sta->addr, arsta->link_id);
2115 		return;
2116 	}
2117 
2118 	band = def.chan->band;
2119 	sband = hw->wiphy->bands[band];
2120 	ratemask = link_sta->supp_rates[band];
2121 	ratemask &= arvif->bitrate_mask.control[band].legacy;
2122 	rates = sband->bitrates;
2123 
2124 	rateset->num_rates = 0;
2125 
2126 	for (i = 0; i < 32; i++, ratemask >>= 1, rates++) {
2127 		if (!(ratemask & 1))
2128 			continue;
2129 
2130 		rate = ath12k_mac_bitrate_to_rate(rates->bitrate);
2131 		rateset->rates[rateset->num_rates] = rate;
2132 		rateset->num_rates++;
2133 	}
2134 }
2135 
2136 static bool
2137 ath12k_peer_assoc_h_ht_masked(const u8 *ht_mcs_mask)
2138 {
2139 	int nss;
2140 
2141 	for (nss = 0; nss < IEEE80211_HT_MCS_MASK_LEN; nss++)
2142 		if (ht_mcs_mask[nss])
2143 			return false;
2144 
2145 	return true;
2146 }
2147 
2148 static bool
2149 ath12k_peer_assoc_h_vht_masked(const u16 *vht_mcs_mask)
2150 {
2151 	int nss;
2152 
2153 	for (nss = 0; nss < NL80211_VHT_NSS_MAX; nss++)
2154 		if (vht_mcs_mask[nss])
2155 			return false;
2156 
2157 	return true;
2158 }
2159 
2160 static void ath12k_peer_assoc_h_ht(struct ath12k *ar,
2161 				   struct ath12k_link_vif *arvif,
2162 				   struct ath12k_link_sta *arsta,
2163 				   struct ath12k_wmi_peer_assoc_arg *arg)
2164 {
2165 	struct ieee80211_vif *vif = ath12k_ahvif_to_vif(arvif->ahvif);
2166 	struct ieee80211_sta *sta = ath12k_ahsta_to_sta(arsta->ahsta);
2167 	const struct ieee80211_sta_ht_cap *ht_cap;
2168 	struct ieee80211_link_sta *link_sta;
2169 	struct cfg80211_chan_def def;
2170 	enum nl80211_band band;
2171 	const u8 *ht_mcs_mask;
2172 	int i, n;
2173 	u8 max_nss;
2174 	u32 stbc;
2175 
2176 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
2177 
2178 	if (WARN_ON(ath12k_mac_vif_link_chan(vif, arvif->link_id, &def)))
2179 		return;
2180 
2181 	link_sta = ath12k_mac_get_link_sta(arsta);
2182 	if (!link_sta) {
2183 		ath12k_warn(ar->ab, "unable to access link sta in peer assoc ht for sta %pM link %u\n",
2184 			    sta->addr, arsta->link_id);
2185 		return;
2186 	}
2187 
2188 	ht_cap = &link_sta->ht_cap;
2189 	if (!ht_cap->ht_supported)
2190 		return;
2191 
2192 	band = def.chan->band;
2193 	ht_mcs_mask = arvif->bitrate_mask.control[band].ht_mcs;
2194 
2195 	if (ath12k_peer_assoc_h_ht_masked(ht_mcs_mask))
2196 		return;
2197 
2198 	arg->ht_flag = true;
2199 
2200 	arg->peer_max_mpdu = (1 << (IEEE80211_HT_MAX_AMPDU_FACTOR +
2201 				    ht_cap->ampdu_factor)) - 1;
2202 
2203 	arg->peer_mpdu_density =
2204 		ath12k_parse_mpdudensity(ht_cap->ampdu_density);
2205 
2206 	arg->peer_ht_caps = ht_cap->cap;
2207 	arg->peer_rate_caps |= WMI_HOST_RC_HT_FLAG;
2208 
2209 	if (ht_cap->cap & IEEE80211_HT_CAP_LDPC_CODING)
2210 		arg->ldpc_flag = true;
2211 
2212 	if (link_sta->bandwidth >= IEEE80211_STA_RX_BW_40) {
2213 		arg->bw_40 = true;
2214 		arg->peer_rate_caps |= WMI_HOST_RC_CW40_FLAG;
2215 	}
2216 
2217 	/* As firmware handles these two flags (IEEE80211_HT_CAP_SGI_20
2218 	 * and IEEE80211_HT_CAP_SGI_40) for enabling SGI, reset both
2219 	 * flags if guard interval is to force Long GI
2220 	 */
2221 	if (arvif->bitrate_mask.control[band].gi == NL80211_TXRATE_FORCE_LGI) {
2222 		arg->peer_ht_caps &= ~(IEEE80211_HT_CAP_SGI_20 | IEEE80211_HT_CAP_SGI_40);
2223 	} else {
2224 		/* Enable SGI flag if either SGI_20 or SGI_40 is supported */
2225 		if (ht_cap->cap & (IEEE80211_HT_CAP_SGI_20 | IEEE80211_HT_CAP_SGI_40))
2226 			arg->peer_rate_caps |= WMI_HOST_RC_SGI_FLAG;
2227 	}
2228 
2229 	if (ht_cap->cap & IEEE80211_HT_CAP_TX_STBC) {
2230 		arg->peer_rate_caps |= WMI_HOST_RC_TX_STBC_FLAG;
2231 		arg->stbc_flag = true;
2232 	}
2233 
2234 	if (ht_cap->cap & IEEE80211_HT_CAP_RX_STBC) {
2235 		stbc = ht_cap->cap & IEEE80211_HT_CAP_RX_STBC;
2236 		stbc = stbc >> IEEE80211_HT_CAP_RX_STBC_SHIFT;
2237 		stbc = stbc << WMI_HOST_RC_RX_STBC_FLAG_S;
2238 		arg->peer_rate_caps |= stbc;
2239 		arg->stbc_flag = true;
2240 	}
2241 
2242 	if (ht_cap->mcs.rx_mask[1] && ht_cap->mcs.rx_mask[2])
2243 		arg->peer_rate_caps |= WMI_HOST_RC_TS_FLAG;
2244 	else if (ht_cap->mcs.rx_mask[1])
2245 		arg->peer_rate_caps |= WMI_HOST_RC_DS_FLAG;
2246 
2247 	for (i = 0, n = 0, max_nss = 0; i < IEEE80211_HT_MCS_MASK_LEN * 8; i++)
2248 		if ((ht_cap->mcs.rx_mask[i / 8] & BIT(i % 8)) &&
2249 		    (ht_mcs_mask[i / 8] & BIT(i % 8))) {
2250 			max_nss = (i / 8) + 1;
2251 			arg->peer_ht_rates.rates[n++] = i;
2252 		}
2253 
2254 	/* This is a workaround for HT-enabled STAs which break the spec
2255 	 * and have no HT capabilities RX mask (no HT RX MCS map).
2256 	 *
2257 	 * As per spec, in section 20.3.5 Modulation and coding scheme (MCS),
2258 	 * MCS 0 through 7 are mandatory in 20MHz with 800 ns GI at all STAs.
2259 	 *
2260 	 * Firmware asserts if such situation occurs.
2261 	 */
2262 	if (n == 0) {
2263 		arg->peer_ht_rates.num_rates = 8;
2264 		for (i = 0; i < arg->peer_ht_rates.num_rates; i++)
2265 			arg->peer_ht_rates.rates[i] = i;
2266 	} else {
2267 		arg->peer_ht_rates.num_rates = n;
2268 		arg->peer_nss = min(link_sta->rx_nss, max_nss);
2269 	}
2270 
2271 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac ht peer %pM mcs cnt %d nss %d\n",
2272 		   arg->peer_mac,
2273 		   arg->peer_ht_rates.num_rates,
2274 		   arg->peer_nss);
2275 }
2276 
2277 static int ath12k_mac_get_max_vht_mcs_map(u16 mcs_map, int nss)
2278 {
2279 	switch ((mcs_map >> (2 * nss)) & 0x3) {
2280 	case IEEE80211_VHT_MCS_SUPPORT_0_7: return BIT(8) - 1;
2281 	case IEEE80211_VHT_MCS_SUPPORT_0_8: return BIT(9) - 1;
2282 	case IEEE80211_VHT_MCS_SUPPORT_0_9: return BIT(10) - 1;
2283 	}
2284 	return 0;
2285 }
2286 
2287 static u16
2288 ath12k_peer_assoc_h_vht_limit(u16 tx_mcs_set,
2289 			      const u16 vht_mcs_limit[NL80211_VHT_NSS_MAX])
2290 {
2291 	int idx_limit;
2292 	int nss;
2293 	u16 mcs_map;
2294 	u16 mcs;
2295 
2296 	for (nss = 0; nss < NL80211_VHT_NSS_MAX; nss++) {
2297 		mcs_map = ath12k_mac_get_max_vht_mcs_map(tx_mcs_set, nss) &
2298 			  vht_mcs_limit[nss];
2299 
2300 		if (mcs_map)
2301 			idx_limit = fls(mcs_map) - 1;
2302 		else
2303 			idx_limit = -1;
2304 
2305 		switch (idx_limit) {
2306 		case 0:
2307 		case 1:
2308 		case 2:
2309 		case 3:
2310 		case 4:
2311 		case 5:
2312 		case 6:
2313 		case 7:
2314 			mcs = IEEE80211_VHT_MCS_SUPPORT_0_7;
2315 			break;
2316 		case 8:
2317 			mcs = IEEE80211_VHT_MCS_SUPPORT_0_8;
2318 			break;
2319 		case 9:
2320 			mcs = IEEE80211_VHT_MCS_SUPPORT_0_9;
2321 			break;
2322 		default:
2323 			WARN_ON(1);
2324 			fallthrough;
2325 		case -1:
2326 			mcs = IEEE80211_VHT_MCS_NOT_SUPPORTED;
2327 			break;
2328 		}
2329 
2330 		tx_mcs_set &= ~(0x3 << (nss * 2));
2331 		tx_mcs_set |= mcs << (nss * 2);
2332 	}
2333 
2334 	return tx_mcs_set;
2335 }
2336 
2337 static u8 ath12k_get_nss_160mhz(struct ath12k *ar,
2338 				u8 max_nss)
2339 {
2340 	u8 nss_ratio_info = ar->pdev->cap.nss_ratio_info;
2341 	u8 max_sup_nss = 0;
2342 
2343 	switch (nss_ratio_info) {
2344 	case WMI_NSS_RATIO_1BY2_NSS:
2345 		max_sup_nss = max_nss >> 1;
2346 		break;
2347 	case WMI_NSS_RATIO_3BY4_NSS:
2348 		ath12k_warn(ar->ab, "WMI_NSS_RATIO_3BY4_NSS not supported\n");
2349 		break;
2350 	case WMI_NSS_RATIO_1_NSS:
2351 		max_sup_nss = max_nss;
2352 		break;
2353 	case WMI_NSS_RATIO_2_NSS:
2354 		ath12k_warn(ar->ab, "WMI_NSS_RATIO_2_NSS not supported\n");
2355 		break;
2356 	default:
2357 		ath12k_warn(ar->ab, "invalid nss ratio received from fw: %d\n",
2358 			    nss_ratio_info);
2359 		break;
2360 	}
2361 
2362 	return max_sup_nss;
2363 }
2364 
2365 static void ath12k_peer_assoc_h_vht(struct ath12k *ar,
2366 				    struct ath12k_link_vif *arvif,
2367 				    struct ath12k_link_sta *arsta,
2368 				    struct ath12k_wmi_peer_assoc_arg *arg)
2369 {
2370 	struct ieee80211_vif *vif = ath12k_ahvif_to_vif(arvif->ahvif);
2371 	struct ieee80211_sta *sta = ath12k_ahsta_to_sta(arsta->ahsta);
2372 	const struct ieee80211_sta_vht_cap *vht_cap;
2373 	struct ieee80211_link_sta *link_sta;
2374 	struct cfg80211_chan_def def;
2375 	enum nl80211_band band;
2376 	u16 *vht_mcs_mask;
2377 	u8 ampdu_factor;
2378 	u8 max_nss, vht_mcs;
2379 	int i, vht_nss, nss_idx;
2380 	bool user_rate_valid = true;
2381 	u32 rx_nss, tx_nss, nss_160;
2382 
2383 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
2384 
2385 	if (WARN_ON(ath12k_mac_vif_link_chan(vif, arvif->link_id, &def)))
2386 		return;
2387 
2388 	link_sta = ath12k_mac_get_link_sta(arsta);
2389 	if (!link_sta) {
2390 		ath12k_warn(ar->ab, "unable to access link sta in peer assoc vht for sta %pM link %u\n",
2391 			    sta->addr, arsta->link_id);
2392 		return;
2393 	}
2394 
2395 	vht_cap = &link_sta->vht_cap;
2396 	if (!vht_cap->vht_supported)
2397 		return;
2398 
2399 	band = def.chan->band;
2400 	vht_mcs_mask = arvif->bitrate_mask.control[band].vht_mcs;
2401 
2402 	if (ath12k_peer_assoc_h_vht_masked(vht_mcs_mask))
2403 		return;
2404 
2405 	arg->vht_flag = true;
2406 
2407 	/* TODO: similar flags required? */
2408 	arg->vht_capable = true;
2409 
2410 	if (def.chan->band == NL80211_BAND_2GHZ)
2411 		arg->vht_ng_flag = true;
2412 
2413 	arg->peer_vht_caps = vht_cap->cap;
2414 
2415 	ampdu_factor = (vht_cap->cap &
2416 			IEEE80211_VHT_CAP_MAX_A_MPDU_LENGTH_EXPONENT_MASK) >>
2417 		       IEEE80211_VHT_CAP_MAX_A_MPDU_LENGTH_EXPONENT_SHIFT;
2418 
2419 	/* Workaround: Some Netgear/Linksys 11ac APs set Rx A-MPDU factor to
2420 	 * zero in VHT IE. Using it would result in degraded throughput.
2421 	 * arg->peer_max_mpdu at this point contains HT max_mpdu so keep
2422 	 * it if VHT max_mpdu is smaller.
2423 	 */
2424 	arg->peer_max_mpdu = max(arg->peer_max_mpdu,
2425 				 (1U << (IEEE80211_HT_MAX_AMPDU_FACTOR +
2426 					ampdu_factor)) - 1);
2427 
2428 	if (link_sta->bandwidth == IEEE80211_STA_RX_BW_80)
2429 		arg->bw_80 = true;
2430 
2431 	if (link_sta->bandwidth == IEEE80211_STA_RX_BW_160)
2432 		arg->bw_160 = true;
2433 
2434 	vht_nss =  ath12k_mac_max_vht_nss(vht_mcs_mask);
2435 
2436 	if (vht_nss > link_sta->rx_nss) {
2437 		user_rate_valid = false;
2438 		for (nss_idx = link_sta->rx_nss - 1; nss_idx >= 0; nss_idx--) {
2439 			if (vht_mcs_mask[nss_idx]) {
2440 				user_rate_valid = true;
2441 				break;
2442 			}
2443 		}
2444 	}
2445 
2446 	if (!user_rate_valid) {
2447 		ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
2448 			   "Setting vht range MCS value to peer supported nss:%d for peer %pM\n",
2449 			   link_sta->rx_nss, arsta->addr);
2450 		vht_mcs_mask[link_sta->rx_nss - 1] = vht_mcs_mask[vht_nss - 1];
2451 	}
2452 
2453 	/* Calculate peer NSS capability from VHT capabilities if STA
2454 	 * supports VHT.
2455 	 */
2456 	for (i = 0, max_nss = 0, vht_mcs = 0; i < NL80211_VHT_NSS_MAX; i++) {
2457 		vht_mcs = __le16_to_cpu(vht_cap->vht_mcs.rx_mcs_map) >>
2458 			  (2 * i) & 3;
2459 
2460 		if (vht_mcs != IEEE80211_VHT_MCS_NOT_SUPPORTED &&
2461 		    vht_mcs_mask[i])
2462 			max_nss = i + 1;
2463 	}
2464 	arg->peer_nss = min(link_sta->rx_nss, max_nss);
2465 	arg->rx_max_rate = __le16_to_cpu(vht_cap->vht_mcs.rx_highest);
2466 	arg->rx_mcs_set = __le16_to_cpu(vht_cap->vht_mcs.rx_mcs_map);
2467 	arg->rx_mcs_set = ath12k_peer_assoc_h_vht_limit(arg->rx_mcs_set, vht_mcs_mask);
2468 
2469 	arg->tx_max_rate = __le16_to_cpu(vht_cap->vht_mcs.tx_highest);
2470 	arg->tx_mcs_set = __le16_to_cpu(vht_cap->vht_mcs.tx_mcs_map);
2471 
2472 	/* In QCN9274 platform, VHT MCS rate 10 and 11 is enabled by default.
2473 	 * VHT MCS rate 10 and 11 is not supported in 11ac standard.
2474 	 * so explicitly disable the VHT MCS rate 10 and 11 in 11ac mode.
2475 	 */
2476 	arg->tx_mcs_set &= ~IEEE80211_VHT_MCS_SUPPORT_0_11_MASK;
2477 	arg->tx_mcs_set |= IEEE80211_DISABLE_VHT_MCS_SUPPORT_0_11;
2478 
2479 	if ((arg->tx_mcs_set & IEEE80211_VHT_MCS_NOT_SUPPORTED) ==
2480 			IEEE80211_VHT_MCS_NOT_SUPPORTED)
2481 		arg->peer_vht_caps &= ~IEEE80211_VHT_CAP_MU_BEAMFORMEE_CAPABLE;
2482 
2483 	/* TODO:  Check */
2484 	arg->tx_max_mcs_nss = 0xFF;
2485 
2486 	if (arg->peer_phymode == MODE_11AC_VHT160) {
2487 		tx_nss = ath12k_get_nss_160mhz(ar, max_nss);
2488 		rx_nss = min(arg->peer_nss, tx_nss);
2489 		arg->peer_bw_rxnss_override = ATH12K_BW_NSS_MAP_ENABLE;
2490 
2491 		if (!rx_nss) {
2492 			ath12k_warn(ar->ab, "invalid max_nss\n");
2493 			return;
2494 		}
2495 
2496 		nss_160 = u32_encode_bits(rx_nss - 1, ATH12K_PEER_RX_NSS_160MHZ);
2497 		arg->peer_bw_rxnss_override |= nss_160;
2498 	}
2499 
2500 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
2501 		   "mac vht peer %pM max_mpdu %d flags 0x%x nss_override 0x%x\n",
2502 		   arsta->addr, arg->peer_max_mpdu, arg->peer_flags,
2503 		   arg->peer_bw_rxnss_override);
2504 }
2505 
2506 static int ath12k_mac_get_max_he_mcs_map(u16 mcs_map, int nss)
2507 {
2508 	switch ((mcs_map >> (2 * nss)) & 0x3) {
2509 	case IEEE80211_HE_MCS_SUPPORT_0_7: return BIT(8) - 1;
2510 	case IEEE80211_HE_MCS_SUPPORT_0_9: return BIT(10) - 1;
2511 	case IEEE80211_HE_MCS_SUPPORT_0_11: return BIT(12) - 1;
2512 	}
2513 	return 0;
2514 }
2515 
2516 static u16 ath12k_peer_assoc_h_he_limit(u16 tx_mcs_set,
2517 					const u16 *he_mcs_limit)
2518 {
2519 	int idx_limit;
2520 	int nss;
2521 	u16 mcs_map;
2522 	u16 mcs;
2523 
2524 	for (nss = 0; nss < NL80211_HE_NSS_MAX; nss++) {
2525 		mcs_map = ath12k_mac_get_max_he_mcs_map(tx_mcs_set, nss) &
2526 			he_mcs_limit[nss];
2527 
2528 		if (mcs_map)
2529 			idx_limit = fls(mcs_map) - 1;
2530 		else
2531 			idx_limit = -1;
2532 
2533 		switch (idx_limit) {
2534 		case 0 ... 7:
2535 			mcs = IEEE80211_HE_MCS_SUPPORT_0_7;
2536 			break;
2537 		case 8:
2538 		case 9:
2539 			mcs = IEEE80211_HE_MCS_SUPPORT_0_9;
2540 			break;
2541 		case 10:
2542 		case 11:
2543 			mcs = IEEE80211_HE_MCS_SUPPORT_0_11;
2544 			break;
2545 		default:
2546 			WARN_ON(1);
2547 			fallthrough;
2548 		case -1:
2549 			mcs = IEEE80211_HE_MCS_NOT_SUPPORTED;
2550 			break;
2551 		}
2552 
2553 		tx_mcs_set &= ~(0x3 << (nss * 2));
2554 		tx_mcs_set |= mcs << (nss * 2);
2555 	}
2556 
2557 	return tx_mcs_set;
2558 }
2559 
2560 static bool
2561 ath12k_peer_assoc_h_he_masked(const u16 he_mcs_mask[NL80211_HE_NSS_MAX])
2562 {
2563 	int nss;
2564 
2565 	for (nss = 0; nss < NL80211_HE_NSS_MAX; nss++)
2566 		if (he_mcs_mask[nss])
2567 			return false;
2568 
2569 	return true;
2570 }
2571 
2572 static void ath12k_peer_assoc_h_he(struct ath12k *ar,
2573 				   struct ath12k_link_vif *arvif,
2574 				   struct ath12k_link_sta *arsta,
2575 				   struct ath12k_wmi_peer_assoc_arg *arg)
2576 {
2577 	struct ieee80211_vif *vif = ath12k_ahvif_to_vif(arvif->ahvif);
2578 	struct ieee80211_sta *sta = ath12k_ahsta_to_sta(arsta->ahsta);
2579 	const struct ieee80211_sta_he_cap *he_cap;
2580 	struct ieee80211_bss_conf *link_conf;
2581 	struct ieee80211_link_sta *link_sta;
2582 	struct cfg80211_chan_def def;
2583 	int i;
2584 	u8 ampdu_factor, max_nss;
2585 	u8 rx_mcs_80 = IEEE80211_HE_MCS_NOT_SUPPORTED;
2586 	u8 rx_mcs_160 = IEEE80211_HE_MCS_NOT_SUPPORTED;
2587 	u16 mcs_160_map, mcs_80_map;
2588 	u8 link_id = arvif->link_id;
2589 	bool support_160;
2590 	enum nl80211_band band;
2591 	u16 *he_mcs_mask;
2592 	u8 he_mcs;
2593 	u16 he_tx_mcs = 0, v = 0;
2594 	int he_nss, nss_idx;
2595 	bool user_rate_valid = true;
2596 	u32 rx_nss, tx_nss, nss_160;
2597 
2598 	if (WARN_ON(ath12k_mac_vif_link_chan(vif, link_id, &def)))
2599 		return;
2600 
2601 	link_conf = ath12k_mac_get_link_bss_conf(arvif);
2602 	if (!link_conf) {
2603 		ath12k_warn(ar->ab, "unable to access bss link conf in peer assoc he for vif %pM link %u",
2604 			    vif->addr, link_id);
2605 		return;
2606 	}
2607 
2608 	link_sta = ath12k_mac_get_link_sta(arsta);
2609 	if (!link_sta) {
2610 		ath12k_warn(ar->ab, "unable to access link sta in peer assoc he for sta %pM link %u\n",
2611 			    sta->addr, arsta->link_id);
2612 		return;
2613 	}
2614 
2615 	he_cap = &link_sta->he_cap;
2616 	if (!he_cap->has_he)
2617 		return;
2618 
2619 	band = def.chan->band;
2620 	he_mcs_mask = arvif->bitrate_mask.control[band].he_mcs;
2621 
2622 	if (ath12k_peer_assoc_h_he_masked(he_mcs_mask))
2623 		return;
2624 
2625 	arg->he_flag = true;
2626 
2627 	support_160 = !!(he_cap->he_cap_elem.phy_cap_info[0] &
2628 		  IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_160MHZ_IN_5G);
2629 
2630 	/* Supported HE-MCS and NSS Set of peer he_cap is intersection with self he_cp */
2631 	mcs_160_map = le16_to_cpu(he_cap->he_mcs_nss_supp.rx_mcs_160);
2632 	mcs_80_map = le16_to_cpu(he_cap->he_mcs_nss_supp.rx_mcs_80);
2633 
2634 	if (support_160) {
2635 		for (i = 7; i >= 0; i--) {
2636 			u8 mcs_160 = (mcs_160_map >> (2 * i)) & 3;
2637 
2638 			if (mcs_160 != IEEE80211_HE_MCS_NOT_SUPPORTED) {
2639 				rx_mcs_160 = i + 1;
2640 				break;
2641 			}
2642 		}
2643 	}
2644 
2645 	for (i = 7; i >= 0; i--) {
2646 		u8 mcs_80 = (mcs_80_map >> (2 * i)) & 3;
2647 
2648 		if (mcs_80 != IEEE80211_HE_MCS_NOT_SUPPORTED) {
2649 			rx_mcs_80 = i + 1;
2650 			break;
2651 		}
2652 	}
2653 
2654 	if (support_160)
2655 		max_nss = min(rx_mcs_80, rx_mcs_160);
2656 	else
2657 		max_nss = rx_mcs_80;
2658 
2659 	arg->peer_nss = min(link_sta->rx_nss, max_nss);
2660 
2661 	memcpy(&arg->peer_he_cap_macinfo, he_cap->he_cap_elem.mac_cap_info,
2662 	       sizeof(he_cap->he_cap_elem.mac_cap_info));
2663 	memcpy(&arg->peer_he_cap_phyinfo, he_cap->he_cap_elem.phy_cap_info,
2664 	       sizeof(he_cap->he_cap_elem.phy_cap_info));
2665 	arg->peer_he_ops = link_conf->he_oper.params;
2666 
2667 	/* the top most byte is used to indicate BSS color info */
2668 	arg->peer_he_ops &= 0xffffff;
2669 
2670 	/* As per section 26.6.1 IEEE Std 802.11ax‐2022, if the Max AMPDU
2671 	 * Exponent Extension in HE cap is zero, use the arg->peer_max_mpdu
2672 	 * as calculated while parsing VHT caps(if VHT caps is present)
2673 	 * or HT caps (if VHT caps is not present).
2674 	 *
2675 	 * For non-zero value of Max AMPDU Exponent Extension in HE MAC caps,
2676 	 * if a HE STA sends VHT cap and HE cap IE in assoc request then, use
2677 	 * MAX_AMPDU_LEN_FACTOR as 20 to calculate max_ampdu length.
2678 	 * If a HE STA that does not send VHT cap, but HE and HT cap in assoc
2679 	 * request, then use MAX_AMPDU_LEN_FACTOR as 16 to calculate max_ampdu
2680 	 * length.
2681 	 */
2682 	ampdu_factor = u8_get_bits(he_cap->he_cap_elem.mac_cap_info[3],
2683 				   IEEE80211_HE_MAC_CAP3_MAX_AMPDU_LEN_EXP_MASK);
2684 
2685 	if (ampdu_factor) {
2686 		if (link_sta->vht_cap.vht_supported)
2687 			arg->peer_max_mpdu = (1 << (IEEE80211_HE_VHT_MAX_AMPDU_FACTOR +
2688 						    ampdu_factor)) - 1;
2689 		else if (link_sta->ht_cap.ht_supported)
2690 			arg->peer_max_mpdu = (1 << (IEEE80211_HE_HT_MAX_AMPDU_FACTOR +
2691 						    ampdu_factor)) - 1;
2692 	}
2693 
2694 	if (he_cap->he_cap_elem.phy_cap_info[6] &
2695 	    IEEE80211_HE_PHY_CAP6_PPE_THRESHOLD_PRESENT) {
2696 		int bit = 7;
2697 		int nss, ru;
2698 
2699 		arg->peer_ppet.numss_m1 = he_cap->ppe_thres[0] &
2700 					  IEEE80211_PPE_THRES_NSS_MASK;
2701 		arg->peer_ppet.ru_bit_mask =
2702 			(he_cap->ppe_thres[0] &
2703 			 IEEE80211_PPE_THRES_RU_INDEX_BITMASK_MASK) >>
2704 			IEEE80211_PPE_THRES_RU_INDEX_BITMASK_POS;
2705 
2706 		for (nss = 0; nss <= arg->peer_ppet.numss_m1; nss++) {
2707 			for (ru = 0; ru < 4; ru++) {
2708 				u32 val = 0;
2709 				int i;
2710 
2711 				if ((arg->peer_ppet.ru_bit_mask & BIT(ru)) == 0)
2712 					continue;
2713 				for (i = 0; i < 6; i++) {
2714 					val >>= 1;
2715 					val |= ((he_cap->ppe_thres[bit / 8] >>
2716 						 (bit % 8)) & 0x1) << 5;
2717 					bit++;
2718 				}
2719 				arg->peer_ppet.ppet16_ppet8_ru3_ru0[nss] |=
2720 								val << (ru * 6);
2721 			}
2722 		}
2723 	}
2724 
2725 	if (he_cap->he_cap_elem.mac_cap_info[0] & IEEE80211_HE_MAC_CAP0_TWT_RES)
2726 		arg->twt_responder = true;
2727 	if (he_cap->he_cap_elem.mac_cap_info[0] & IEEE80211_HE_MAC_CAP0_TWT_REQ)
2728 		arg->twt_requester = true;
2729 
2730 	he_nss = ath12k_mac_max_he_nss(he_mcs_mask);
2731 
2732 	if (he_nss > link_sta->rx_nss) {
2733 		user_rate_valid = false;
2734 		for (nss_idx = link_sta->rx_nss - 1; nss_idx >= 0; nss_idx--) {
2735 			if (he_mcs_mask[nss_idx]) {
2736 				user_rate_valid = true;
2737 				break;
2738 			}
2739 		}
2740 	}
2741 
2742 	if (!user_rate_valid) {
2743 		ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
2744 			   "Setting he range MCS value to peer supported nss:%d for peer %pM\n",
2745 			   link_sta->rx_nss, arsta->addr);
2746 		he_mcs_mask[link_sta->rx_nss - 1] = he_mcs_mask[he_nss - 1];
2747 	}
2748 
2749 	switch (link_sta->bandwidth) {
2750 	case IEEE80211_STA_RX_BW_160:
2751 		v = le16_to_cpu(he_cap->he_mcs_nss_supp.rx_mcs_160);
2752 		v = ath12k_peer_assoc_h_he_limit(v, he_mcs_mask);
2753 		arg->peer_he_rx_mcs_set[WMI_HECAP_TXRX_MCS_NSS_IDX_160] = v;
2754 
2755 		v = le16_to_cpu(he_cap->he_mcs_nss_supp.tx_mcs_160);
2756 		arg->peer_he_tx_mcs_set[WMI_HECAP_TXRX_MCS_NSS_IDX_160] = v;
2757 
2758 		arg->peer_he_mcs_count++;
2759 		if (!he_tx_mcs)
2760 			he_tx_mcs = v;
2761 		fallthrough;
2762 
2763 	default:
2764 		v = le16_to_cpu(he_cap->he_mcs_nss_supp.rx_mcs_80);
2765 		v = ath12k_peer_assoc_h_he_limit(v, he_mcs_mask);
2766 		arg->peer_he_rx_mcs_set[WMI_HECAP_TXRX_MCS_NSS_IDX_80] = v;
2767 
2768 		v = le16_to_cpu(he_cap->he_mcs_nss_supp.tx_mcs_80);
2769 		arg->peer_he_tx_mcs_set[WMI_HECAP_TXRX_MCS_NSS_IDX_80] = v;
2770 
2771 		arg->peer_he_mcs_count++;
2772 		if (!he_tx_mcs)
2773 			he_tx_mcs = v;
2774 		break;
2775 	}
2776 
2777 	/* Calculate peer NSS capability from HE capabilities if STA
2778 	 * supports HE.
2779 	 */
2780 	for (i = 0, max_nss = 0, he_mcs = 0; i < NL80211_HE_NSS_MAX; i++) {
2781 		he_mcs = he_tx_mcs >> (2 * i) & 3;
2782 
2783 		/* In case of fixed rates, MCS Range in he_tx_mcs might have
2784 		 * unsupported range, with he_mcs_mask set, so check either of them
2785 		 * to find nss.
2786 		 */
2787 		if (he_mcs != IEEE80211_HE_MCS_NOT_SUPPORTED ||
2788 		    he_mcs_mask[i])
2789 			max_nss = i + 1;
2790 	}
2791 
2792 	max_nss = min(max_nss, ar->num_tx_chains);
2793 	arg->peer_nss = min(link_sta->rx_nss, max_nss);
2794 
2795 	if (arg->peer_phymode == MODE_11AX_HE160) {
2796 		tx_nss = ath12k_get_nss_160mhz(ar, ar->num_tx_chains);
2797 		rx_nss = min(arg->peer_nss, tx_nss);
2798 
2799 		arg->peer_nss = min(link_sta->rx_nss, ar->num_rx_chains);
2800 		arg->peer_bw_rxnss_override = ATH12K_BW_NSS_MAP_ENABLE;
2801 
2802 		if (!rx_nss) {
2803 			ath12k_warn(ar->ab, "invalid max_nss\n");
2804 			return;
2805 		}
2806 
2807 		nss_160 = u32_encode_bits(rx_nss - 1, ATH12K_PEER_RX_NSS_160MHZ);
2808 		arg->peer_bw_rxnss_override |= nss_160;
2809 	}
2810 
2811 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
2812 		   "mac he peer %pM nss %d mcs cnt %d nss_override 0x%x\n",
2813 		   arsta->addr, arg->peer_nss,
2814 		   arg->peer_he_mcs_count,
2815 		   arg->peer_bw_rxnss_override);
2816 }
2817 
2818 static void ath12k_peer_assoc_h_he_6ghz(struct ath12k *ar,
2819 					struct ath12k_link_vif *arvif,
2820 					struct ath12k_link_sta *arsta,
2821 					struct ath12k_wmi_peer_assoc_arg *arg)
2822 {
2823 	struct ieee80211_vif *vif = ath12k_ahvif_to_vif(arvif->ahvif);
2824 	struct ieee80211_sta *sta = ath12k_ahsta_to_sta(arsta->ahsta);
2825 	const struct ieee80211_sta_he_cap *he_cap;
2826 	struct ieee80211_link_sta *link_sta;
2827 	struct cfg80211_chan_def def;
2828 	enum nl80211_band band;
2829 	u8 ampdu_factor, mpdu_density;
2830 
2831 	if (WARN_ON(ath12k_mac_vif_link_chan(vif, arvif->link_id, &def)))
2832 		return;
2833 
2834 	band = def.chan->band;
2835 
2836 	link_sta = ath12k_mac_get_link_sta(arsta);
2837 	if (!link_sta) {
2838 		ath12k_warn(ar->ab, "unable to access link sta in peer assoc he 6ghz for sta %pM link %u\n",
2839 			    sta->addr, arsta->link_id);
2840 		return;
2841 	}
2842 
2843 	he_cap = &link_sta->he_cap;
2844 
2845 	if (!arg->he_flag || band != NL80211_BAND_6GHZ || !link_sta->he_6ghz_capa.capa)
2846 		return;
2847 
2848 	if (link_sta->bandwidth == IEEE80211_STA_RX_BW_40)
2849 		arg->bw_40 = true;
2850 
2851 	if (link_sta->bandwidth == IEEE80211_STA_RX_BW_80)
2852 		arg->bw_80 = true;
2853 
2854 	if (link_sta->bandwidth == IEEE80211_STA_RX_BW_160)
2855 		arg->bw_160 = true;
2856 
2857 	if (link_sta->bandwidth == IEEE80211_STA_RX_BW_320)
2858 		arg->bw_320 = true;
2859 
2860 	arg->peer_he_caps_6ghz = le16_to_cpu(link_sta->he_6ghz_capa.capa);
2861 
2862 	mpdu_density = u32_get_bits(arg->peer_he_caps_6ghz,
2863 				    IEEE80211_HE_6GHZ_CAP_MIN_MPDU_START);
2864 	arg->peer_mpdu_density = ath12k_parse_mpdudensity(mpdu_density);
2865 
2866 	/* From IEEE Std 802.11ax-2021 - Section 10.12.2: An HE STA shall be capable of
2867 	 * receiving A-MPDU where the A-MPDU pre-EOF padding length is up to the value
2868 	 * indicated by the Maximum A-MPDU Length Exponent Extension field in the HE
2869 	 * Capabilities element and the Maximum A-MPDU Length Exponent field in HE 6 GHz
2870 	 * Band Capabilities element in the 6 GHz band.
2871 	 *
2872 	 * Here, we are extracting the Max A-MPDU Exponent Extension from HE caps and
2873 	 * factor is the Maximum A-MPDU Length Exponent from HE 6 GHZ Band capability.
2874 	 */
2875 	ampdu_factor = u8_get_bits(he_cap->he_cap_elem.mac_cap_info[3],
2876 				   IEEE80211_HE_MAC_CAP3_MAX_AMPDU_LEN_EXP_MASK) +
2877 			u32_get_bits(arg->peer_he_caps_6ghz,
2878 				     IEEE80211_HE_6GHZ_CAP_MAX_AMPDU_LEN_EXP);
2879 
2880 	arg->peer_max_mpdu = (1u << (IEEE80211_HE_6GHZ_MAX_AMPDU_FACTOR +
2881 				     ampdu_factor)) - 1;
2882 }
2883 
2884 static int ath12k_get_smps_from_capa(const struct ieee80211_sta_ht_cap *ht_cap,
2885 				     const struct ieee80211_he_6ghz_capa *he_6ghz_capa,
2886 				     int *smps)
2887 {
2888 	if (ht_cap->ht_supported)
2889 		*smps = u16_get_bits(ht_cap->cap, IEEE80211_HT_CAP_SM_PS);
2890 	else
2891 		*smps = le16_get_bits(he_6ghz_capa->capa,
2892 				      IEEE80211_HE_6GHZ_CAP_SM_PS);
2893 
2894 	if (*smps >= ARRAY_SIZE(ath12k_smps_map))
2895 		return -EINVAL;
2896 
2897 	return 0;
2898 }
2899 
2900 static void ath12k_peer_assoc_h_smps(struct ath12k_link_sta *arsta,
2901 				     struct ath12k_wmi_peer_assoc_arg *arg)
2902 {
2903 	struct ieee80211_sta *sta = ath12k_ahsta_to_sta(arsta->ahsta);
2904 	const struct ieee80211_he_6ghz_capa *he_6ghz_capa;
2905 	struct ath12k_link_vif *arvif = arsta->arvif;
2906 	const struct ieee80211_sta_ht_cap *ht_cap;
2907 	struct ieee80211_link_sta *link_sta;
2908 	struct ath12k *ar = arvif->ar;
2909 	int smps;
2910 
2911 	link_sta = ath12k_mac_get_link_sta(arsta);
2912 	if (!link_sta) {
2913 		ath12k_warn(ar->ab, "unable to access link sta in peer assoc he for sta %pM link %u\n",
2914 			    sta->addr, arsta->link_id);
2915 		return;
2916 	}
2917 
2918 	he_6ghz_capa = &link_sta->he_6ghz_capa;
2919 	ht_cap = &link_sta->ht_cap;
2920 
2921 	if (!ht_cap->ht_supported && !he_6ghz_capa->capa)
2922 		return;
2923 
2924 	if (ath12k_get_smps_from_capa(ht_cap, he_6ghz_capa, &smps))
2925 		return;
2926 
2927 	switch (smps) {
2928 	case WLAN_HT_CAP_SM_PS_STATIC:
2929 		arg->static_mimops_flag = true;
2930 		break;
2931 	case WLAN_HT_CAP_SM_PS_DYNAMIC:
2932 		arg->dynamic_mimops_flag = true;
2933 		break;
2934 	case WLAN_HT_CAP_SM_PS_DISABLED:
2935 		arg->spatial_mux_flag = true;
2936 		break;
2937 	default:
2938 		break;
2939 	}
2940 }
2941 
2942 static void ath12k_peer_assoc_h_qos(struct ath12k *ar,
2943 				    struct ath12k_link_vif *arvif,
2944 				    struct ath12k_link_sta *arsta,
2945 				    struct ath12k_wmi_peer_assoc_arg *arg)
2946 {
2947 	struct ieee80211_sta *sta = ath12k_ahsta_to_sta(arsta->ahsta);
2948 
2949 	switch (arvif->ahvif->vdev_type) {
2950 	case WMI_VDEV_TYPE_AP:
2951 		if (sta->wme) {
2952 			/* TODO: Check WME vs QoS */
2953 			arg->is_wme_set = true;
2954 			arg->qos_flag = true;
2955 		}
2956 
2957 		if (sta->wme && sta->uapsd_queues) {
2958 			/* TODO: Check WME vs QoS */
2959 			arg->is_wme_set = true;
2960 			arg->apsd_flag = true;
2961 			arg->peer_rate_caps |= WMI_HOST_RC_UAPSD_FLAG;
2962 		}
2963 		break;
2964 	case WMI_VDEV_TYPE_STA:
2965 		if (sta->wme) {
2966 			arg->is_wme_set = true;
2967 			arg->qos_flag = true;
2968 		}
2969 		break;
2970 	default:
2971 		break;
2972 	}
2973 
2974 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac peer %pM qos %d\n",
2975 		   arsta->addr, arg->qos_flag);
2976 }
2977 
2978 static int ath12k_peer_assoc_qos_ap(struct ath12k *ar,
2979 				    struct ath12k_link_vif *arvif,
2980 				    struct ath12k_link_sta *arsta)
2981 {
2982 	struct ieee80211_sta *sta = ath12k_ahsta_to_sta(arsta->ahsta);
2983 	struct ath12k_wmi_ap_ps_arg arg;
2984 	u32 max_sp;
2985 	u32 uapsd;
2986 	int ret;
2987 
2988 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
2989 
2990 	arg.vdev_id = arvif->vdev_id;
2991 
2992 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac uapsd_queues 0x%x max_sp %d\n",
2993 		   sta->uapsd_queues, sta->max_sp);
2994 
2995 	uapsd = 0;
2996 	if (sta->uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_VO)
2997 		uapsd |= WMI_AP_PS_UAPSD_AC3_DELIVERY_EN |
2998 			 WMI_AP_PS_UAPSD_AC3_TRIGGER_EN;
2999 	if (sta->uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_VI)
3000 		uapsd |= WMI_AP_PS_UAPSD_AC2_DELIVERY_EN |
3001 			 WMI_AP_PS_UAPSD_AC2_TRIGGER_EN;
3002 	if (sta->uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_BK)
3003 		uapsd |= WMI_AP_PS_UAPSD_AC1_DELIVERY_EN |
3004 			 WMI_AP_PS_UAPSD_AC1_TRIGGER_EN;
3005 	if (sta->uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_BE)
3006 		uapsd |= WMI_AP_PS_UAPSD_AC0_DELIVERY_EN |
3007 			 WMI_AP_PS_UAPSD_AC0_TRIGGER_EN;
3008 
3009 	max_sp = 0;
3010 	if (sta->max_sp < MAX_WMI_AP_PS_PEER_PARAM_MAX_SP)
3011 		max_sp = sta->max_sp;
3012 
3013 	arg.param = WMI_AP_PS_PEER_PARAM_UAPSD;
3014 	arg.value = uapsd;
3015 	ret = ath12k_wmi_send_set_ap_ps_param_cmd(ar, arsta->addr, &arg);
3016 	if (ret)
3017 		goto err;
3018 
3019 	arg.param = WMI_AP_PS_PEER_PARAM_MAX_SP;
3020 	arg.value = max_sp;
3021 	ret = ath12k_wmi_send_set_ap_ps_param_cmd(ar, arsta->addr, &arg);
3022 	if (ret)
3023 		goto err;
3024 
3025 	/* TODO: revisit during testing */
3026 	arg.param = WMI_AP_PS_PEER_PARAM_SIFS_RESP_FRMTYPE;
3027 	arg.value = DISABLE_SIFS_RESPONSE_TRIGGER;
3028 	ret = ath12k_wmi_send_set_ap_ps_param_cmd(ar, arsta->addr, &arg);
3029 	if (ret)
3030 		goto err;
3031 
3032 	arg.param = WMI_AP_PS_PEER_PARAM_SIFS_RESP_UAPSD;
3033 	arg.value = DISABLE_SIFS_RESPONSE_TRIGGER;
3034 	ret = ath12k_wmi_send_set_ap_ps_param_cmd(ar, arsta->addr, &arg);
3035 	if (ret)
3036 		goto err;
3037 
3038 	return 0;
3039 
3040 err:
3041 	ath12k_warn(ar->ab, "failed to set ap ps peer param %d for vdev %i: %d\n",
3042 		    arg.param, arvif->vdev_id, ret);
3043 	return ret;
3044 }
3045 
3046 static bool ath12k_mac_sta_has_ofdm_only(struct ieee80211_link_sta *sta)
3047 {
3048 	return sta->supp_rates[NL80211_BAND_2GHZ] >>
3049 	       ATH12K_MAC_FIRST_OFDM_RATE_IDX;
3050 }
3051 
3052 static enum wmi_phy_mode ath12k_mac_get_phymode_vht(struct ath12k *ar,
3053 						    struct ieee80211_link_sta *link_sta)
3054 {
3055 	if (link_sta->bandwidth == IEEE80211_STA_RX_BW_160) {
3056 		if (link_sta->vht_cap.cap & (IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_160MHZ |
3057 		    IEEE80211_VHT_CAP_EXT_NSS_BW_MASK))
3058 			return MODE_11AC_VHT160;
3059 
3060 		/* Allow STA to connect even if it does not explicitly advertise 160 MHz
3061 		 * support
3062 		 */
3063 		return MODE_11AC_VHT160;
3064 	}
3065 
3066 	if (link_sta->bandwidth == IEEE80211_STA_RX_BW_80)
3067 		return MODE_11AC_VHT80;
3068 
3069 	if (link_sta->bandwidth == IEEE80211_STA_RX_BW_40)
3070 		return MODE_11AC_VHT40;
3071 
3072 	if (link_sta->bandwidth == IEEE80211_STA_RX_BW_20)
3073 		return MODE_11AC_VHT20;
3074 
3075 	return MODE_UNKNOWN;
3076 }
3077 
3078 static enum wmi_phy_mode ath12k_mac_get_phymode_he(struct ath12k *ar,
3079 						   struct ieee80211_link_sta *link_sta)
3080 {
3081 	if (link_sta->bandwidth == IEEE80211_STA_RX_BW_160) {
3082 		if (link_sta->he_cap.he_cap_elem.phy_cap_info[0] &
3083 		     IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_160MHZ_IN_5G)
3084 			return MODE_11AX_HE160;
3085 
3086 		return MODE_UNKNOWN;
3087 	}
3088 
3089 	if (link_sta->bandwidth == IEEE80211_STA_RX_BW_80)
3090 		return MODE_11AX_HE80;
3091 
3092 	if (link_sta->bandwidth == IEEE80211_STA_RX_BW_40)
3093 		return MODE_11AX_HE40;
3094 
3095 	if (link_sta->bandwidth == IEEE80211_STA_RX_BW_20)
3096 		return MODE_11AX_HE20;
3097 
3098 	return MODE_UNKNOWN;
3099 }
3100 
3101 static enum wmi_phy_mode ath12k_mac_get_phymode_eht(struct ath12k *ar,
3102 						    struct ieee80211_link_sta *link_sta)
3103 {
3104 	if (link_sta->bandwidth == IEEE80211_STA_RX_BW_320)
3105 		if (link_sta->eht_cap.eht_cap_elem.phy_cap_info[0] &
3106 		    IEEE80211_EHT_PHY_CAP0_320MHZ_IN_6GHZ)
3107 			return MODE_11BE_EHT320;
3108 
3109 	if (link_sta->bandwidth == IEEE80211_STA_RX_BW_160) {
3110 		if (link_sta->he_cap.he_cap_elem.phy_cap_info[0] &
3111 		    IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_160MHZ_IN_5G)
3112 			return MODE_11BE_EHT160;
3113 
3114 		ath12k_warn(ar->ab, "invalid EHT PHY capability info for 160 Mhz: %d\n",
3115 			    link_sta->he_cap.he_cap_elem.phy_cap_info[0]);
3116 
3117 		return MODE_UNKNOWN;
3118 	}
3119 
3120 	if (link_sta->bandwidth == IEEE80211_STA_RX_BW_80)
3121 		return MODE_11BE_EHT80;
3122 
3123 	if (link_sta->bandwidth == IEEE80211_STA_RX_BW_40)
3124 		return MODE_11BE_EHT40;
3125 
3126 	if (link_sta->bandwidth == IEEE80211_STA_RX_BW_20)
3127 		return MODE_11BE_EHT20;
3128 
3129 	return MODE_UNKNOWN;
3130 }
3131 
3132 static bool
3133 ath12k_peer_assoc_h_eht_masked(const u16 eht_mcs_mask[NL80211_EHT_NSS_MAX])
3134 {
3135 	int nss;
3136 
3137 	for (nss = 0; nss < NL80211_EHT_NSS_MAX; nss++)
3138 		if (eht_mcs_mask[nss])
3139 			return false;
3140 
3141 	return true;
3142 }
3143 
3144 static void ath12k_peer_assoc_h_phymode(struct ath12k *ar,
3145 					struct ath12k_link_vif *arvif,
3146 					struct ath12k_link_sta *arsta,
3147 					struct ath12k_wmi_peer_assoc_arg *arg)
3148 {
3149 	struct ieee80211_link_sta *link_sta;
3150 	struct cfg80211_chan_def def;
3151 	enum nl80211_band band;
3152 	const u8 *ht_mcs_mask;
3153 	const u16 *vht_mcs_mask;
3154 	const u16 *he_mcs_mask;
3155 	const u16 *eht_mcs_mask;
3156 	enum wmi_phy_mode phymode = MODE_UNKNOWN;
3157 
3158 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
3159 
3160 	struct ieee80211_vif *vif = ath12k_ahvif_to_vif(arvif->ahvif);
3161 	struct ieee80211_sta *sta = ath12k_ahsta_to_sta(arsta->ahsta);
3162 
3163 	if (WARN_ON(ath12k_mac_vif_link_chan(vif, arvif->link_id, &def)))
3164 		return;
3165 
3166 	band = def.chan->band;
3167 	ht_mcs_mask = arvif->bitrate_mask.control[band].ht_mcs;
3168 	vht_mcs_mask = arvif->bitrate_mask.control[band].vht_mcs;
3169 	he_mcs_mask = arvif->bitrate_mask.control[band].he_mcs;
3170 	eht_mcs_mask = arvif->bitrate_mask.control[band].eht_mcs;
3171 
3172 	link_sta = ath12k_mac_get_link_sta(arsta);
3173 	if (!link_sta) {
3174 		ath12k_warn(ar->ab, "unable to access link sta in peer assoc he for sta %pM link %u\n",
3175 			    sta->addr, arsta->link_id);
3176 		return;
3177 	}
3178 
3179 	switch (band) {
3180 	case NL80211_BAND_2GHZ:
3181 		if (link_sta->eht_cap.has_eht &&
3182 		    !ath12k_peer_assoc_h_eht_masked(eht_mcs_mask)) {
3183 			if (link_sta->bandwidth == IEEE80211_STA_RX_BW_40)
3184 				phymode = MODE_11BE_EHT40_2G;
3185 			else
3186 				phymode = MODE_11BE_EHT20_2G;
3187 		} else if (link_sta->he_cap.has_he &&
3188 			   !ath12k_peer_assoc_h_he_masked(he_mcs_mask)) {
3189 			if (link_sta->bandwidth == IEEE80211_STA_RX_BW_80)
3190 				phymode = MODE_11AX_HE80_2G;
3191 			else if (link_sta->bandwidth == IEEE80211_STA_RX_BW_40)
3192 				phymode = MODE_11AX_HE40_2G;
3193 			else
3194 				phymode = MODE_11AX_HE20_2G;
3195 		} else if (link_sta->vht_cap.vht_supported &&
3196 		    !ath12k_peer_assoc_h_vht_masked(vht_mcs_mask)) {
3197 			if (link_sta->bandwidth == IEEE80211_STA_RX_BW_40)
3198 				phymode = MODE_11AC_VHT40;
3199 			else
3200 				phymode = MODE_11AC_VHT20;
3201 		} else if (link_sta->ht_cap.ht_supported &&
3202 			   !ath12k_peer_assoc_h_ht_masked(ht_mcs_mask)) {
3203 			if (link_sta->bandwidth == IEEE80211_STA_RX_BW_40)
3204 				phymode = MODE_11NG_HT40;
3205 			else
3206 				phymode = MODE_11NG_HT20;
3207 		} else if (ath12k_mac_sta_has_ofdm_only(link_sta)) {
3208 			phymode = MODE_11G;
3209 		} else {
3210 			phymode = MODE_11B;
3211 		}
3212 		break;
3213 	case NL80211_BAND_5GHZ:
3214 	case NL80211_BAND_6GHZ:
3215 		/* Check EHT first */
3216 		if (link_sta->eht_cap.has_eht) {
3217 			phymode = ath12k_mac_get_phymode_eht(ar, link_sta);
3218 		} else if (link_sta->he_cap.has_he &&
3219 			   !ath12k_peer_assoc_h_he_masked(he_mcs_mask)) {
3220 			phymode = ath12k_mac_get_phymode_he(ar, link_sta);
3221 		} else if (link_sta->vht_cap.vht_supported &&
3222 		    !ath12k_peer_assoc_h_vht_masked(vht_mcs_mask)) {
3223 			phymode = ath12k_mac_get_phymode_vht(ar, link_sta);
3224 		} else if (link_sta->ht_cap.ht_supported &&
3225 			   !ath12k_peer_assoc_h_ht_masked(ht_mcs_mask)) {
3226 			if (link_sta->bandwidth >= IEEE80211_STA_RX_BW_40)
3227 				phymode = MODE_11NA_HT40;
3228 			else
3229 				phymode = MODE_11NA_HT20;
3230 		} else {
3231 			phymode = MODE_11A;
3232 		}
3233 		break;
3234 	default:
3235 		break;
3236 	}
3237 
3238 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac peer %pM phymode %s\n",
3239 		   arsta->addr, ath12k_mac_phymode_str(phymode));
3240 
3241 	arg->peer_phymode = phymode;
3242 	WARN_ON(phymode == MODE_UNKNOWN);
3243 }
3244 
3245 #define ATH12K_EHT_MCS_7_ENABLED	0x00FF
3246 #define ATH12K_EHT_MCS_9_ENABLED	0x0300
3247 #define ATH12K_EHT_MCS_11_ENABLED	0x0C00
3248 #define ATH12K_EHT_MCS_13_ENABLED	0x3000
3249 
3250 static void ath12k_mac_set_eht_mcs(u8 rx_tx_mcs7, u8 rx_tx_mcs9,
3251 				   u8 rx_tx_mcs11, u8 rx_tx_mcs13,
3252 				   u32 *rx_mcs, u32 *tx_mcs,
3253 				   const u16 eht_mcs_limit[NL80211_EHT_NSS_MAX])
3254 {
3255 	int nss;
3256 	u8 mcs_7 = 0, mcs_9 = 0, mcs_11 = 0, mcs_13 = 0;
3257 	u8 peer_mcs_7, peer_mcs_9, peer_mcs_11, peer_mcs_13;
3258 
3259 	for (nss = 0; nss < NL80211_EHT_NSS_MAX; nss++) {
3260 		if (eht_mcs_limit[nss] & ATH12K_EHT_MCS_7_ENABLED)
3261 			mcs_7++;
3262 		if (eht_mcs_limit[nss] & ATH12K_EHT_MCS_9_ENABLED)
3263 			mcs_9++;
3264 		if (eht_mcs_limit[nss] & ATH12K_EHT_MCS_11_ENABLED)
3265 			mcs_11++;
3266 		if (eht_mcs_limit[nss] & ATH12K_EHT_MCS_13_ENABLED)
3267 			mcs_13++;
3268 	}
3269 
3270 	peer_mcs_7 = u8_get_bits(rx_tx_mcs7, IEEE80211_EHT_MCS_NSS_RX);
3271 	peer_mcs_9 = u8_get_bits(rx_tx_mcs9, IEEE80211_EHT_MCS_NSS_RX);
3272 	peer_mcs_11 = u8_get_bits(rx_tx_mcs11, IEEE80211_EHT_MCS_NSS_RX);
3273 	peer_mcs_13 = u8_get_bits(rx_tx_mcs13, IEEE80211_EHT_MCS_NSS_RX);
3274 
3275 	*rx_mcs = u32_encode_bits(min(peer_mcs_7, mcs_7), WMI_EHT_MCS_NSS_0_7) |
3276 		  u32_encode_bits(min(peer_mcs_9, mcs_9), WMI_EHT_MCS_NSS_8_9) |
3277 		  u32_encode_bits(min(peer_mcs_11, mcs_11), WMI_EHT_MCS_NSS_10_11) |
3278 		  u32_encode_bits(min(peer_mcs_13, mcs_13), WMI_EHT_MCS_NSS_12_13);
3279 
3280 	peer_mcs_7 = u8_get_bits(rx_tx_mcs7, IEEE80211_EHT_MCS_NSS_TX);
3281 	peer_mcs_9 = u8_get_bits(rx_tx_mcs9, IEEE80211_EHT_MCS_NSS_TX);
3282 	peer_mcs_11 = u8_get_bits(rx_tx_mcs11, IEEE80211_EHT_MCS_NSS_TX);
3283 	peer_mcs_13 = u8_get_bits(rx_tx_mcs13, IEEE80211_EHT_MCS_NSS_TX);
3284 
3285 	*tx_mcs = u32_encode_bits(min(peer_mcs_7, mcs_7), WMI_EHT_MCS_NSS_0_7) |
3286 		  u32_encode_bits(min(peer_mcs_9, mcs_9), WMI_EHT_MCS_NSS_8_9) |
3287 		  u32_encode_bits(min(peer_mcs_11, mcs_11), WMI_EHT_MCS_NSS_10_11) |
3288 		  u32_encode_bits(min(peer_mcs_13, mcs_13), WMI_EHT_MCS_NSS_12_13);
3289 }
3290 
3291 static void ath12k_mac_set_eht_ppe_threshold(const u8 *ppe_thres,
3292 					     struct ath12k_wmi_ppe_threshold_arg *ppet)
3293 {
3294 	u32 bit_pos = IEEE80211_EHT_PPE_THRES_INFO_HEADER_SIZE, val;
3295 	u8 nss, ru, i;
3296 	u8 ppet_bit_len_per_ru = IEEE80211_EHT_PPE_THRES_INFO_PPET_SIZE * 2;
3297 
3298 	ppet->numss_m1 = u8_get_bits(ppe_thres[0], IEEE80211_EHT_PPE_THRES_NSS_MASK);
3299 	ppet->ru_bit_mask = u16_get_bits(get_unaligned_le16(ppe_thres),
3300 					 IEEE80211_EHT_PPE_THRES_RU_INDEX_BITMASK_MASK);
3301 
3302 	for (nss = 0; nss <= ppet->numss_m1; nss++) {
3303 		for (ru = 0;
3304 		     ru < hweight16(IEEE80211_EHT_PPE_THRES_RU_INDEX_BITMASK_MASK);
3305 		     ru++) {
3306 			if ((ppet->ru_bit_mask & BIT(ru)) == 0)
3307 				continue;
3308 
3309 			val = 0;
3310 			for (i = 0; i < ppet_bit_len_per_ru; i++) {
3311 				val |= (((ppe_thres[bit_pos / 8] >>
3312 					  (bit_pos % 8)) & 0x1) << i);
3313 				bit_pos++;
3314 			}
3315 			ppet->ppet16_ppet8_ru3_ru0[nss] |=
3316 					(val << (ru * ppet_bit_len_per_ru));
3317 		}
3318 	}
3319 }
3320 
3321 static void ath12k_peer_assoc_h_eht(struct ath12k *ar,
3322 				    struct ath12k_link_vif *arvif,
3323 				    struct ath12k_link_sta *arsta,
3324 				    struct ath12k_wmi_peer_assoc_arg *arg)
3325 {
3326 	struct ieee80211_sta *sta = ath12k_ahsta_to_sta(arsta->ahsta);
3327 	struct ieee80211_vif *vif = ath12k_ahvif_to_vif(arvif->ahvif);
3328 	const struct ieee80211_eht_mcs_nss_supp *own_eht_mcs_nss_supp;
3329 	const struct ieee80211_eht_mcs_nss_supp_20mhz_only *bw_20;
3330 	const struct ieee80211_sta_eht_cap *eht_cap, *own_eht_cap;
3331 	const struct ieee80211_sband_iftype_data *iftd;
3332 	const struct ieee80211_eht_mcs_nss_supp_bw *bw;
3333 	const struct ieee80211_sta_he_cap *he_cap;
3334 	struct ieee80211_link_sta *link_sta;
3335 	struct ieee80211_bss_conf *link_conf;
3336 	struct cfg80211_chan_def def;
3337 	bool user_rate_valid = true;
3338 	enum nl80211_band band;
3339 	int eht_nss, nss_idx;
3340 	u32 *rx_mcs, *tx_mcs;
3341 	u16 *eht_mcs_mask;
3342 	u8 max_nss = 0;
3343 
3344 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
3345 
3346 	link_sta = ath12k_mac_get_link_sta(arsta);
3347 	if (!link_sta) {
3348 		ath12k_warn(ar->ab, "unable to access link sta in peer assoc eht for sta %pM link %u\n",
3349 			    sta->addr, arsta->link_id);
3350 		return;
3351 	}
3352 
3353 	link_conf = ath12k_mac_get_link_bss_conf(arvif);
3354 	if (!link_conf) {
3355 		ath12k_warn(ar->ab, "unable to access link_conf in peer assoc eht set\n");
3356 		return;
3357 	}
3358 
3359 	eht_cap = &link_sta->eht_cap;
3360 	he_cap = &link_sta->he_cap;
3361 	if (!he_cap->has_he || !eht_cap->has_eht)
3362 		return;
3363 
3364 	if (WARN_ON(ath12k_mac_vif_link_chan(vif, arvif->link_id, &def)))
3365 		return;
3366 
3367 	band = def.chan->band;
3368 	eht_mcs_mask = arvif->bitrate_mask.control[band].eht_mcs;
3369 
3370 	iftd = ieee80211_get_sband_iftype_data(&ar->mac.sbands[band], vif->type);
3371 	if (!iftd) {
3372 		ath12k_warn(ar->ab,
3373 			    "unable to access iftype_data in struct ieee80211_supported_band\n");
3374 		return;
3375 	}
3376 
3377 	own_eht_cap = &iftd->eht_cap;
3378 	own_eht_mcs_nss_supp = &own_eht_cap->eht_mcs_nss_supp;
3379 
3380 	arg->eht_flag = true;
3381 
3382 	if ((eht_cap->eht_cap_elem.phy_cap_info[5] &
3383 	     IEEE80211_EHT_PHY_CAP5_PPE_THRESHOLD_PRESENT) &&
3384 	    eht_cap->eht_ppe_thres[0] != 0)
3385 		ath12k_mac_set_eht_ppe_threshold(eht_cap->eht_ppe_thres,
3386 						 &arg->peer_eht_ppet);
3387 
3388 	memcpy(arg->peer_eht_cap_mac, eht_cap->eht_cap_elem.mac_cap_info,
3389 	       sizeof(eht_cap->eht_cap_elem.mac_cap_info));
3390 	memcpy(arg->peer_eht_cap_phy, eht_cap->eht_cap_elem.phy_cap_info,
3391 	       sizeof(eht_cap->eht_cap_elem.phy_cap_info));
3392 
3393 	rx_mcs = arg->peer_eht_rx_mcs_set;
3394 	tx_mcs = arg->peer_eht_tx_mcs_set;
3395 
3396 	eht_nss = ath12k_mac_max_eht_mcs_nss((void *)own_eht_mcs_nss_supp,
3397 					     sizeof(*own_eht_mcs_nss_supp));
3398 	if (eht_nss > link_sta->rx_nss) {
3399 		user_rate_valid = false;
3400 		for (nss_idx = (link_sta->rx_nss - 1); nss_idx >= 0; nss_idx--) {
3401 			if (eht_mcs_mask[nss_idx]) {
3402 				user_rate_valid = true;
3403 				break;
3404 			}
3405 		}
3406 	}
3407 
3408 	if (!user_rate_valid) {
3409 		ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
3410 			   "Setting eht range MCS value to peer supported nss %d for peer %pM\n",
3411 			   link_sta->rx_nss, arsta->addr);
3412 		eht_mcs_mask[link_sta->rx_nss - 1] = eht_mcs_mask[eht_nss - 1];
3413 	}
3414 
3415 	bw_20 = &eht_cap->eht_mcs_nss_supp.only_20mhz;
3416 	bw = &eht_cap->eht_mcs_nss_supp.bw._80;
3417 
3418 	switch (link_sta->bandwidth) {
3419 	case IEEE80211_STA_RX_BW_320:
3420 		bw = &eht_cap->eht_mcs_nss_supp.bw._320;
3421 		ath12k_mac_set_eht_mcs(bw->rx_tx_mcs9_max_nss,
3422 				       bw->rx_tx_mcs9_max_nss,
3423 				       bw->rx_tx_mcs11_max_nss,
3424 				       bw->rx_tx_mcs13_max_nss,
3425 				       &rx_mcs[WMI_EHTCAP_TXRX_MCS_NSS_IDX_320],
3426 				       &tx_mcs[WMI_EHTCAP_TXRX_MCS_NSS_IDX_320],
3427 				       eht_mcs_mask);
3428 		arg->peer_eht_mcs_count++;
3429 		fallthrough;
3430 	case IEEE80211_STA_RX_BW_160:
3431 		bw = &eht_cap->eht_mcs_nss_supp.bw._160;
3432 		ath12k_mac_set_eht_mcs(bw->rx_tx_mcs9_max_nss,
3433 				       bw->rx_tx_mcs9_max_nss,
3434 				       bw->rx_tx_mcs11_max_nss,
3435 				       bw->rx_tx_mcs13_max_nss,
3436 				       &rx_mcs[WMI_EHTCAP_TXRX_MCS_NSS_IDX_160],
3437 				       &tx_mcs[WMI_EHTCAP_TXRX_MCS_NSS_IDX_160],
3438 				       eht_mcs_mask);
3439 		arg->peer_eht_mcs_count++;
3440 		fallthrough;
3441 	default:
3442 		if (!(link_sta->he_cap.he_cap_elem.phy_cap_info[0] &
3443 		      IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_MASK_ALL)) {
3444 			bw_20 = &eht_cap->eht_mcs_nss_supp.only_20mhz;
3445 
3446 			ath12k_mac_set_eht_mcs(bw_20->rx_tx_mcs7_max_nss,
3447 					       bw_20->rx_tx_mcs9_max_nss,
3448 					       bw_20->rx_tx_mcs11_max_nss,
3449 					       bw_20->rx_tx_mcs13_max_nss,
3450 					       &rx_mcs[WMI_EHTCAP_TXRX_MCS_NSS_IDX_80],
3451 					       &tx_mcs[WMI_EHTCAP_TXRX_MCS_NSS_IDX_80],
3452 					       eht_mcs_mask);
3453 		} else {
3454 			bw = &eht_cap->eht_mcs_nss_supp.bw._80;
3455 			ath12k_mac_set_eht_mcs(bw->rx_tx_mcs9_max_nss,
3456 					       bw->rx_tx_mcs9_max_nss,
3457 					       bw->rx_tx_mcs11_max_nss,
3458 					       bw->rx_tx_mcs13_max_nss,
3459 					       &rx_mcs[WMI_EHTCAP_TXRX_MCS_NSS_IDX_80],
3460 					       &tx_mcs[WMI_EHTCAP_TXRX_MCS_NSS_IDX_80],
3461 					       eht_mcs_mask);
3462 		}
3463 
3464 		arg->peer_eht_mcs_count++;
3465 		break;
3466 	}
3467 
3468 	arg->punct_bitmap = ~arvif->punct_bitmap;
3469 	arg->eht_disable_mcs15 = link_conf->eht_disable_mcs15;
3470 
3471 	if (!(link_sta->he_cap.he_cap_elem.phy_cap_info[0] &
3472 	      IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_MASK_ALL)) {
3473 		if (bw_20->rx_tx_mcs13_max_nss)
3474 			max_nss = max(max_nss, u8_get_bits(bw_20->rx_tx_mcs13_max_nss,
3475 							   IEEE80211_EHT_MCS_NSS_RX));
3476 		if (bw_20->rx_tx_mcs11_max_nss)
3477 			max_nss = max(max_nss, u8_get_bits(bw_20->rx_tx_mcs11_max_nss,
3478 							   IEEE80211_EHT_MCS_NSS_RX));
3479 		if (bw_20->rx_tx_mcs9_max_nss)
3480 			max_nss = max(max_nss, u8_get_bits(bw_20->rx_tx_mcs9_max_nss,
3481 							   IEEE80211_EHT_MCS_NSS_RX));
3482 		if (bw_20->rx_tx_mcs7_max_nss)
3483 			max_nss = max(max_nss, u8_get_bits(bw_20->rx_tx_mcs7_max_nss,
3484 							   IEEE80211_EHT_MCS_NSS_RX));
3485 	} else {
3486 		if (bw->rx_tx_mcs13_max_nss)
3487 			max_nss = max(max_nss, u8_get_bits(bw->rx_tx_mcs13_max_nss,
3488 							   IEEE80211_EHT_MCS_NSS_RX));
3489 		if (bw->rx_tx_mcs11_max_nss)
3490 			max_nss = max(max_nss, u8_get_bits(bw->rx_tx_mcs11_max_nss,
3491 							   IEEE80211_EHT_MCS_NSS_RX));
3492 		if (bw->rx_tx_mcs9_max_nss)
3493 			max_nss = max(max_nss, u8_get_bits(bw->rx_tx_mcs9_max_nss,
3494 							   IEEE80211_EHT_MCS_NSS_RX));
3495 	}
3496 
3497 	max_nss = min(max_nss, (uint8_t)eht_nss);
3498 
3499 	arg->peer_nss = min(link_sta->rx_nss, max_nss);
3500 
3501 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
3502 		   "mac eht peer %pM nss %d mcs cnt %d ru_punct_bitmap 0x%x\n",
3503 		   arsta->addr, arg->peer_nss, arg->peer_eht_mcs_count,
3504 		   arg->punct_bitmap);
3505 }
3506 
3507 static void ath12k_peer_assoc_h_mlo(struct ath12k_link_sta *arsta,
3508 				    struct ath12k_wmi_peer_assoc_arg *arg)
3509 {
3510 	struct ieee80211_sta *sta = ath12k_ahsta_to_sta(arsta->ahsta);
3511 	struct peer_assoc_mlo_params *ml = &arg->ml;
3512 	struct ath12k_sta *ahsta = arsta->ahsta;
3513 	struct ath12k_link_sta *arsta_p;
3514 	struct ath12k_link_vif *arvif;
3515 	unsigned long links;
3516 	u8 link_id;
3517 	int i;
3518 
3519 	if (!sta->mlo || ahsta->ml_peer_id == ATH12K_MLO_PEER_ID_INVALID)
3520 		return;
3521 
3522 	ml->enabled = true;
3523 	ml->assoc_link = arsta->is_assoc_link;
3524 
3525 	/* For now considering the primary umac based on assoc link */
3526 	ml->primary_umac = arsta->is_assoc_link;
3527 	ml->peer_id_valid = true;
3528 	ml->logical_link_idx_valid = true;
3529 
3530 	ether_addr_copy(ml->mld_addr, sta->addr);
3531 	ml->logical_link_idx = arsta->link_idx;
3532 	ml->ml_peer_id = ahsta->ml_peer_id;
3533 	ml->ieee_link_id = arsta->link_id;
3534 	ml->num_partner_links = 0;
3535 	ml->eml_cap = sta->eml_cap;
3536 	links = ahsta->links_map;
3537 
3538 	rcu_read_lock();
3539 
3540 	i = 0;
3541 
3542 	for_each_set_bit(link_id, &links, IEEE80211_MLD_MAX_NUM_LINKS) {
3543 		if (i >= ATH12K_WMI_MLO_MAX_LINKS)
3544 			break;
3545 
3546 		arsta_p = rcu_dereference(ahsta->link[link_id]);
3547 		arvif = rcu_dereference(ahsta->ahvif->link[link_id]);
3548 
3549 		if (arsta_p == arsta)
3550 			continue;
3551 
3552 		if (!arvif->is_started)
3553 			continue;
3554 
3555 		ml->partner_info[i].vdev_id = arvif->vdev_id;
3556 		ml->partner_info[i].hw_link_id = arvif->ar->pdev->hw_link_id;
3557 		ml->partner_info[i].assoc_link = arsta_p->is_assoc_link;
3558 		ml->partner_info[i].primary_umac = arsta_p->is_assoc_link;
3559 		ml->partner_info[i].logical_link_idx_valid = true;
3560 		ml->partner_info[i].logical_link_idx = arsta_p->link_idx;
3561 		ml->num_partner_links++;
3562 
3563 		i++;
3564 	}
3565 
3566 	rcu_read_unlock();
3567 }
3568 
3569 static void ath12k_peer_assoc_prepare(struct ath12k *ar,
3570 				      struct ath12k_link_vif *arvif,
3571 				      struct ath12k_link_sta *arsta,
3572 				      struct ath12k_wmi_peer_assoc_arg *arg,
3573 				      bool reassoc)
3574 {
3575 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
3576 
3577 	memset(arg, 0, sizeof(*arg));
3578 
3579 	reinit_completion(&ar->peer_assoc_done);
3580 
3581 	arg->peer_new_assoc = !reassoc;
3582 	ath12k_peer_assoc_h_basic(ar, arvif, arsta, arg);
3583 	ath12k_peer_assoc_h_crypto(ar, arvif, arsta, arg);
3584 	ath12k_peer_assoc_h_rates(ar, arvif, arsta, arg);
3585 	ath12k_peer_assoc_h_ht(ar, arvif, arsta, arg);
3586 	ath12k_peer_assoc_h_vht(ar, arvif, arsta, arg);
3587 	ath12k_peer_assoc_h_he(ar, arvif, arsta, arg);
3588 	ath12k_peer_assoc_h_he_6ghz(ar, arvif, arsta, arg);
3589 	ath12k_peer_assoc_h_eht(ar, arvif, arsta, arg);
3590 	ath12k_peer_assoc_h_qos(ar, arvif, arsta, arg);
3591 	ath12k_peer_assoc_h_phymode(ar, arvif, arsta, arg);
3592 	ath12k_peer_assoc_h_smps(arsta, arg);
3593 	ath12k_peer_assoc_h_mlo(arsta, arg);
3594 
3595 	arsta->peer_nss = arg->peer_nss;
3596 	/* TODO: amsdu_disable req? */
3597 }
3598 
3599 static int ath12k_setup_peer_smps(struct ath12k *ar, struct ath12k_link_vif *arvif,
3600 				  const u8 *addr,
3601 				  const struct ieee80211_sta_ht_cap *ht_cap,
3602 				  const struct ieee80211_he_6ghz_capa *he_6ghz_capa)
3603 {
3604 	int smps, ret = 0;
3605 
3606 	if (!ht_cap->ht_supported && !he_6ghz_capa)
3607 		return 0;
3608 
3609 	ret = ath12k_get_smps_from_capa(ht_cap, he_6ghz_capa, &smps);
3610 	if (ret < 0)
3611 		return ret;
3612 
3613 	return ath12k_wmi_set_peer_param(ar, addr, arvif->vdev_id,
3614 					 WMI_PEER_MIMO_PS_STATE,
3615 					 ath12k_smps_map[smps]);
3616 }
3617 
3618 static int ath12k_mac_set_he_txbf_conf(struct ath12k_link_vif *arvif)
3619 {
3620 	struct ath12k_vif *ahvif = arvif->ahvif;
3621 	struct ath12k *ar = arvif->ar;
3622 	u32 param = WMI_VDEV_PARAM_SET_HEMU_MODE;
3623 	u32 value = 0;
3624 	int ret;
3625 	struct ieee80211_bss_conf *link_conf;
3626 
3627 	link_conf = ath12k_mac_get_link_bss_conf(arvif);
3628 	if (!link_conf) {
3629 		ath12k_warn(ar->ab, "unable to access bss link conf in txbf conf\n");
3630 		return -EINVAL;
3631 	}
3632 
3633 	if (!link_conf->he_support)
3634 		return 0;
3635 
3636 	if (link_conf->he_su_beamformer) {
3637 		value |= u32_encode_bits(HE_SU_BFER_ENABLE, HE_MODE_SU_TX_BFER);
3638 		if (link_conf->he_mu_beamformer &&
3639 		    ahvif->vdev_type == WMI_VDEV_TYPE_AP)
3640 			value |= u32_encode_bits(HE_MU_BFER_ENABLE, HE_MODE_MU_TX_BFER);
3641 	}
3642 
3643 	if (ahvif->vif->type != NL80211_IFTYPE_MESH_POINT) {
3644 		value |= u32_encode_bits(HE_DL_MUOFDMA_ENABLE, HE_MODE_DL_OFDMA) |
3645 			 u32_encode_bits(HE_UL_MUOFDMA_ENABLE, HE_MODE_UL_OFDMA);
3646 
3647 		if (link_conf->he_full_ul_mumimo)
3648 			value |= u32_encode_bits(HE_UL_MUMIMO_ENABLE, HE_MODE_UL_MUMIMO);
3649 
3650 		if (link_conf->he_su_beamformee)
3651 			value |= u32_encode_bits(HE_SU_BFEE_ENABLE, HE_MODE_SU_TX_BFEE);
3652 	}
3653 
3654 	ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id, param, value);
3655 	if (ret) {
3656 		ath12k_warn(ar->ab, "failed to set vdev %d HE MU mode: %d\n",
3657 			    arvif->vdev_id, ret);
3658 		return ret;
3659 	}
3660 
3661 	param = WMI_VDEV_PARAM_SET_HE_SOUNDING_MODE;
3662 	value =	u32_encode_bits(HE_VHT_SOUNDING_MODE_ENABLE, HE_VHT_SOUNDING_MODE) |
3663 		u32_encode_bits(HE_TRIG_NONTRIG_SOUNDING_MODE_ENABLE,
3664 				HE_TRIG_NONTRIG_SOUNDING_MODE);
3665 	ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
3666 					    param, value);
3667 	if (ret) {
3668 		ath12k_warn(ar->ab, "failed to set vdev %d sounding mode: %d\n",
3669 			    arvif->vdev_id, ret);
3670 		return ret;
3671 	}
3672 
3673 	return 0;
3674 }
3675 
3676 static int ath12k_mac_vif_recalc_sta_he_txbf(struct ath12k *ar,
3677 					     struct ath12k_link_vif *arvif,
3678 					     struct ieee80211_sta_he_cap *he_cap,
3679 					     int *hemode)
3680 {
3681 	struct ieee80211_vif *vif = arvif->ahvif->vif;
3682 	struct ieee80211_he_cap_elem he_cap_elem = {};
3683 	struct ieee80211_sta_he_cap *cap_band;
3684 	struct cfg80211_chan_def def;
3685 	u8 link_id = arvif->link_id;
3686 	struct ieee80211_bss_conf *link_conf;
3687 
3688 	link_conf = ath12k_mac_get_link_bss_conf(arvif);
3689 	if (!link_conf) {
3690 		ath12k_warn(ar->ab, "unable to access bss link conf in recalc txbf conf\n");
3691 		return -EINVAL;
3692 	}
3693 
3694 	if (!link_conf->he_support)
3695 		return 0;
3696 
3697 	if (vif->type != NL80211_IFTYPE_STATION)
3698 		return -EINVAL;
3699 
3700 	if (WARN_ON(ath12k_mac_vif_link_chan(vif, link_id, &def)))
3701 		return -EINVAL;
3702 
3703 	if (def.chan->band == NL80211_BAND_2GHZ)
3704 		cap_band = &ar->mac.iftype[NL80211_BAND_2GHZ][vif->type].he_cap;
3705 	else
3706 		cap_band = &ar->mac.iftype[NL80211_BAND_5GHZ][vif->type].he_cap;
3707 
3708 	memcpy(&he_cap_elem, &cap_band->he_cap_elem, sizeof(he_cap_elem));
3709 
3710 	*hemode = 0;
3711 	if (HECAP_PHY_SUBFME_GET(he_cap_elem.phy_cap_info)) {
3712 		if (HECAP_PHY_SUBFMR_GET(he_cap->he_cap_elem.phy_cap_info))
3713 			*hemode |= u32_encode_bits(HE_SU_BFEE_ENABLE, HE_MODE_SU_TX_BFEE);
3714 		if (HECAP_PHY_MUBFMR_GET(he_cap->he_cap_elem.phy_cap_info))
3715 			*hemode |= u32_encode_bits(HE_MU_BFEE_ENABLE, HE_MODE_MU_TX_BFEE);
3716 	}
3717 
3718 	if (vif->type != NL80211_IFTYPE_MESH_POINT) {
3719 		*hemode |= u32_encode_bits(HE_DL_MUOFDMA_ENABLE, HE_MODE_DL_OFDMA) |
3720 			  u32_encode_bits(HE_UL_MUOFDMA_ENABLE, HE_MODE_UL_OFDMA);
3721 
3722 		if (HECAP_PHY_ULMUMIMO_GET(he_cap_elem.phy_cap_info))
3723 			if (HECAP_PHY_ULMUMIMO_GET(he_cap->he_cap_elem.phy_cap_info))
3724 				*hemode |= u32_encode_bits(HE_UL_MUMIMO_ENABLE,
3725 							  HE_MODE_UL_MUMIMO);
3726 
3727 		if (u32_get_bits(*hemode, HE_MODE_MU_TX_BFEE))
3728 			*hemode |= u32_encode_bits(HE_SU_BFEE_ENABLE, HE_MODE_SU_TX_BFEE);
3729 
3730 		if (u32_get_bits(*hemode, HE_MODE_MU_TX_BFER))
3731 			*hemode |= u32_encode_bits(HE_SU_BFER_ENABLE, HE_MODE_SU_TX_BFER);
3732 	}
3733 
3734 	return 0;
3735 }
3736 
3737 static int ath12k_mac_set_eht_txbf_conf(struct ath12k_link_vif *arvif)
3738 {
3739 	struct ath12k_vif *ahvif = arvif->ahvif;
3740 	struct ath12k *ar = arvif->ar;
3741 	u32 param = WMI_VDEV_PARAM_SET_EHT_MU_MODE;
3742 	u32 value = 0;
3743 	int ret;
3744 	struct ieee80211_bss_conf *link_conf;
3745 
3746 	link_conf = ath12k_mac_get_link_bss_conf(arvif);
3747 	if (!link_conf) {
3748 		ath12k_warn(ar->ab, "unable to access bss link conf in eht txbf conf\n");
3749 		return -ENOENT;
3750 	}
3751 
3752 	if (!link_conf->eht_support)
3753 		return 0;
3754 
3755 	if (link_conf->eht_su_beamformer) {
3756 		value |= u32_encode_bits(EHT_SU_BFER_ENABLE, EHT_MODE_SU_TX_BFER);
3757 		if (link_conf->eht_mu_beamformer &&
3758 		    ahvif->vdev_type == WMI_VDEV_TYPE_AP)
3759 			value |= u32_encode_bits(EHT_MU_BFER_ENABLE,
3760 						 EHT_MODE_MU_TX_BFER) |
3761 				 u32_encode_bits(EHT_DL_MUOFDMA_ENABLE,
3762 						 EHT_MODE_DL_OFDMA_MUMIMO) |
3763 				 u32_encode_bits(EHT_UL_MUOFDMA_ENABLE,
3764 						 EHT_MODE_UL_OFDMA_MUMIMO);
3765 	}
3766 
3767 	if (ahvif->vif->type != NL80211_IFTYPE_MESH_POINT) {
3768 		value |= u32_encode_bits(EHT_DL_MUOFDMA_ENABLE, EHT_MODE_DL_OFDMA) |
3769 			 u32_encode_bits(EHT_UL_MUOFDMA_ENABLE, EHT_MODE_UL_OFDMA);
3770 
3771 		if (link_conf->eht_80mhz_full_bw_ul_mumimo)
3772 			value |= u32_encode_bits(EHT_UL_MUMIMO_ENABLE, EHT_MODE_MUMIMO);
3773 
3774 		if (link_conf->eht_su_beamformee)
3775 			value |= u32_encode_bits(EHT_SU_BFEE_ENABLE,
3776 						 EHT_MODE_SU_TX_BFEE);
3777 	}
3778 
3779 	ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id, param, value);
3780 	if (ret) {
3781 		ath12k_warn(ar->ab, "failed to set vdev %d EHT MU mode: %d\n",
3782 			    arvif->vdev_id, ret);
3783 		return ret;
3784 	}
3785 
3786 	return 0;
3787 }
3788 
3789 static u32 ath12k_mac_ieee80211_sta_bw_to_wmi(struct ath12k *ar,
3790 					      struct ieee80211_link_sta *link_sta)
3791 {
3792 	u32 bw;
3793 
3794 	switch (link_sta->bandwidth) {
3795 	case IEEE80211_STA_RX_BW_20:
3796 		bw = WMI_PEER_CHWIDTH_20MHZ;
3797 		break;
3798 	case IEEE80211_STA_RX_BW_40:
3799 		bw = WMI_PEER_CHWIDTH_40MHZ;
3800 		break;
3801 	case IEEE80211_STA_RX_BW_80:
3802 		bw = WMI_PEER_CHWIDTH_80MHZ;
3803 		break;
3804 	case IEEE80211_STA_RX_BW_160:
3805 		bw = WMI_PEER_CHWIDTH_160MHZ;
3806 		break;
3807 	case IEEE80211_STA_RX_BW_320:
3808 		bw = WMI_PEER_CHWIDTH_320MHZ;
3809 		break;
3810 	default:
3811 		ath12k_warn(ar->ab, "Invalid bandwidth %d for link station %pM\n",
3812 			    link_sta->bandwidth, link_sta->addr);
3813 		bw = WMI_PEER_CHWIDTH_20MHZ;
3814 		break;
3815 	}
3816 
3817 	return bw;
3818 }
3819 
3820 static void ath12k_bss_assoc(struct ath12k *ar,
3821 			     struct ath12k_link_vif *arvif,
3822 			     struct ieee80211_bss_conf *bss_conf)
3823 {
3824 	struct ath12k_vif *ahvif = arvif->ahvif;
3825 	struct ieee80211_vif *vif = ath12k_ahvif_to_vif(ahvif);
3826 	struct ath12k_wmi_vdev_up_params params = {};
3827 	struct ieee80211_link_sta *link_sta;
3828 	u8 link_id = bss_conf->link_id;
3829 	struct ath12k_link_sta *arsta;
3830 	struct ieee80211_sta *ap_sta;
3831 	struct ath12k_sta *ahsta;
3832 	struct ath12k_dp_link_peer *peer;
3833 	bool is_auth = false;
3834 	u32 hemode = 0;
3835 	int ret;
3836 	struct ath12k_dp *dp = ath12k_ab_to_dp(ar->ab);
3837 
3838 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
3839 
3840 	struct ath12k_wmi_peer_assoc_arg *peer_arg __free(kfree) =
3841 					kzalloc_obj(*peer_arg);
3842 	if (!peer_arg)
3843 		return;
3844 
3845 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
3846 		   "mac vdev %i link id %u assoc bssid %pM aid %d\n",
3847 		   arvif->vdev_id, link_id, arvif->bssid, ahvif->aid);
3848 
3849 	rcu_read_lock();
3850 
3851 	/* During ML connection, cfg.ap_addr has the MLD address. For
3852 	 * non-ML connection, it has the BSSID.
3853 	 */
3854 	ap_sta = ieee80211_find_sta(vif, vif->cfg.ap_addr);
3855 	if (!ap_sta) {
3856 		ath12k_warn(ar->ab, "failed to find station entry for bss %pM vdev %i\n",
3857 			    vif->cfg.ap_addr, arvif->vdev_id);
3858 		rcu_read_unlock();
3859 		return;
3860 	}
3861 
3862 	ahsta = ath12k_sta_to_ahsta(ap_sta);
3863 
3864 	arsta = wiphy_dereference(ath12k_ar_to_hw(ar)->wiphy,
3865 				  ahsta->link[link_id]);
3866 	if (WARN_ON(!arsta)) {
3867 		rcu_read_unlock();
3868 		return;
3869 	}
3870 
3871 	link_sta = ath12k_mac_get_link_sta(arsta);
3872 	if (WARN_ON(!link_sta)) {
3873 		rcu_read_unlock();
3874 		return;
3875 	}
3876 
3877 	ath12k_peer_assoc_prepare(ar, arvif, arsta, peer_arg, false);
3878 
3879 	/* link_sta->he_cap must be protected by rcu_read_lock */
3880 	ret = ath12k_mac_vif_recalc_sta_he_txbf(ar, arvif, &link_sta->he_cap, &hemode);
3881 	if (ret) {
3882 		ath12k_warn(ar->ab, "failed to recalc he txbf for vdev %i on bss %pM: %d\n",
3883 			    arvif->vdev_id, bss_conf->bssid, ret);
3884 		rcu_read_unlock();
3885 		return;
3886 	}
3887 
3888 	rcu_read_unlock();
3889 
3890 	/* keep this before ath12k_wmi_send_peer_assoc_cmd() */
3891 	ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
3892 					    WMI_VDEV_PARAM_SET_HEMU_MODE, hemode);
3893 	if (ret) {
3894 		ath12k_warn(ar->ab, "failed to submit vdev param txbf 0x%x: %d\n",
3895 			    hemode, ret);
3896 		return;
3897 	}
3898 
3899 	peer_arg->is_assoc = true;
3900 	ret = ath12k_wmi_send_peer_assoc_cmd(ar, peer_arg);
3901 	if (ret) {
3902 		ath12k_warn(ar->ab, "failed to run peer assoc for %pM vdev %i: %d\n",
3903 			    bss_conf->bssid, arvif->vdev_id, ret);
3904 		return;
3905 	}
3906 
3907 	if (!wait_for_completion_timeout(&ar->peer_assoc_done, 1 * HZ)) {
3908 		ath12k_warn(ar->ab, "failed to get peer assoc conf event for %pM vdev %i\n",
3909 			    bss_conf->bssid, arvif->vdev_id);
3910 		return;
3911 	}
3912 
3913 	ret = ath12k_setup_peer_smps(ar, arvif, bss_conf->bssid,
3914 				     &link_sta->ht_cap, &link_sta->he_6ghz_capa);
3915 	if (ret) {
3916 		ath12k_warn(ar->ab, "failed to setup peer SMPS for vdev %d: %d\n",
3917 			    arvif->vdev_id, ret);
3918 		return;
3919 	}
3920 
3921 	WARN_ON(arvif->is_up);
3922 
3923 	ahvif->aid = vif->cfg.aid;
3924 	ether_addr_copy(arvif->bssid, bss_conf->bssid);
3925 
3926 	params.vdev_id = arvif->vdev_id;
3927 	params.aid = ahvif->aid;
3928 	params.bssid = arvif->bssid;
3929 	params.tx_bssid = ath12k_mac_get_tx_bssid(arvif);
3930 	if (params.tx_bssid) {
3931 		params.nontx_profile_idx = bss_conf->bssid_index;
3932 		params.nontx_profile_cnt = 1 << bss_conf->bssid_indicator;
3933 	}
3934 	ret = ath12k_wmi_vdev_up(ar, &params);
3935 	if (ret) {
3936 		ath12k_warn(ar->ab, "failed to set vdev %d up: %d\n",
3937 			    arvif->vdev_id, ret);
3938 		return;
3939 	}
3940 
3941 	arvif->is_up = true;
3942 	arvif->rekey_data.enable_offload = false;
3943 
3944 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
3945 		   "mac vdev %d up (associated) bssid %pM aid %d\n",
3946 		   arvif->vdev_id, bss_conf->bssid, vif->cfg.aid);
3947 
3948 	spin_lock_bh(&dp->dp_lock);
3949 
3950 	peer = ath12k_dp_link_peer_find_by_vdev_and_addr(dp, arvif->vdev_id,
3951 							 arvif->bssid);
3952 	if (peer && peer->is_authorized)
3953 		is_auth = true;
3954 
3955 	spin_unlock_bh(&dp->dp_lock);
3956 
3957 	/* Authorize BSS Peer */
3958 	if (is_auth) {
3959 		ret = ath12k_wmi_set_peer_param(ar, arvif->bssid,
3960 						arvif->vdev_id,
3961 						WMI_PEER_AUTHORIZE,
3962 						1);
3963 		if (ret)
3964 			ath12k_warn(ar->ab, "Unable to authorize BSS peer: %d\n", ret);
3965 	}
3966 
3967 	ret = ath12k_wmi_send_obss_spr_cmd(ar, arvif->vdev_id,
3968 					   &bss_conf->he_obss_pd);
3969 	if (ret)
3970 		ath12k_warn(ar->ab, "failed to set vdev %i OBSS PD parameters: %d\n",
3971 			    arvif->vdev_id, ret);
3972 
3973 	if (test_bit(WMI_TLV_SERVICE_11D_OFFLOAD, ar->ab->wmi_ab.svc_map) &&
3974 	    ahvif->vdev_type == WMI_VDEV_TYPE_STA &&
3975 	    ahvif->vdev_subtype == WMI_VDEV_SUBTYPE_NONE)
3976 		ath12k_mac_11d_scan_stop_all(ar->ab);
3977 }
3978 
3979 static void ath12k_bss_disassoc(struct ath12k *ar,
3980 				struct ath12k_link_vif *arvif)
3981 {
3982 	int ret;
3983 
3984 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
3985 
3986 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac vdev %i disassoc bssid %pM\n",
3987 		   arvif->vdev_id, arvif->bssid);
3988 
3989 	ret = ath12k_wmi_vdev_down(ar, arvif->vdev_id);
3990 	if (ret)
3991 		ath12k_warn(ar->ab, "failed to down vdev %i: %d\n",
3992 			    arvif->vdev_id, ret);
3993 
3994 	arvif->is_up = false;
3995 
3996 	memset(&arvif->rekey_data, 0, sizeof(arvif->rekey_data));
3997 
3998 	cancel_delayed_work(&arvif->connection_loss_work);
3999 }
4000 
4001 static u32 ath12k_mac_get_rate_hw_value(int bitrate)
4002 {
4003 	u32 preamble;
4004 	u16 hw_value;
4005 	int rate;
4006 	size_t i;
4007 
4008 	if (ath12k_mac_bitrate_is_cck(bitrate))
4009 		preamble = WMI_RATE_PREAMBLE_CCK;
4010 	else
4011 		preamble = WMI_RATE_PREAMBLE_OFDM;
4012 
4013 	for (i = 0; i < ARRAY_SIZE(ath12k_legacy_rates); i++) {
4014 		if (ath12k_legacy_rates[i].bitrate != bitrate)
4015 			continue;
4016 
4017 		hw_value = ath12k_legacy_rates[i].hw_value;
4018 		rate = ATH12K_HW_RATE_CODE(hw_value, 0, preamble);
4019 
4020 		return rate;
4021 	}
4022 
4023 	return -EINVAL;
4024 }
4025 
4026 static void ath12k_recalculate_mgmt_rate(struct ath12k *ar,
4027 					 struct ath12k_link_vif *arvif,
4028 					 struct cfg80211_chan_def *def)
4029 {
4030 	struct ieee80211_vif *vif = ath12k_ahvif_to_vif(arvif->ahvif);
4031 	struct ieee80211_hw *hw = ath12k_ar_to_hw(ar);
4032 	const struct ieee80211_supported_band *sband;
4033 	struct ieee80211_bss_conf *bss_conf;
4034 	u8 basic_rate_idx;
4035 	int hw_rate_code;
4036 	u32 vdev_param;
4037 	u16 bitrate;
4038 	int ret;
4039 
4040 	lockdep_assert_wiphy(hw->wiphy);
4041 
4042 	bss_conf = ath12k_mac_get_link_bss_conf(arvif);
4043 	if (!bss_conf) {
4044 		ath12k_warn(ar->ab, "unable to access bss link conf in mgmt rate calc for vif %pM link %u\n",
4045 			    vif->addr, arvif->link_id);
4046 		return;
4047 	}
4048 
4049 	sband = hw->wiphy->bands[def->chan->band];
4050 	if (bss_conf->basic_rates)
4051 		basic_rate_idx = __ffs(bss_conf->basic_rates);
4052 	else
4053 		basic_rate_idx = 0;
4054 	bitrate = sband->bitrates[basic_rate_idx].bitrate;
4055 
4056 	hw_rate_code = ath12k_mac_get_rate_hw_value(bitrate);
4057 	if (hw_rate_code < 0) {
4058 		ath12k_warn(ar->ab, "bitrate not supported %d\n", bitrate);
4059 		return;
4060 	}
4061 
4062 	vdev_param = WMI_VDEV_PARAM_MGMT_RATE;
4063 	ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id, vdev_param,
4064 					    hw_rate_code);
4065 	if (ret)
4066 		ath12k_warn(ar->ab, "failed to set mgmt tx rate %d\n", ret);
4067 
4068 	vdev_param = WMI_VDEV_PARAM_BEACON_RATE;
4069 	ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id, vdev_param,
4070 					    hw_rate_code);
4071 	if (ret)
4072 		ath12k_warn(ar->ab, "failed to set beacon tx rate %d\n", ret);
4073 }
4074 
4075 static void ath12k_mac_bcn_tx_event(struct ath12k_link_vif *arvif)
4076 {
4077 	struct ieee80211_vif *vif = ath12k_ahvif_to_vif(arvif->ahvif);
4078 	struct ieee80211_bss_conf *link_conf;
4079 
4080 	link_conf = ath12k_mac_get_link_bss_conf(arvif);
4081 	if (!link_conf) {
4082 		ath12k_warn(arvif->ar->ab, "failed to get link conf for vdev %u\n",
4083 			    arvif->vdev_id);
4084 		return;
4085 	}
4086 
4087 	if (link_conf->color_change_active) {
4088 		if (ieee80211_beacon_cntdwn_is_complete(vif, arvif->link_id)) {
4089 			ieee80211_color_change_finish(vif, arvif->link_id);
4090 			return;
4091 		}
4092 
4093 		ieee80211_beacon_update_cntdwn(vif, arvif->link_id);
4094 		ath12k_mac_setup_bcn_tmpl(arvif);
4095 	}
4096 }
4097 
4098 static void ath12k_mac_bcn_tx_work(struct wiphy *wiphy, struct wiphy_work *work)
4099 {
4100 	struct ath12k_link_vif *arvif = container_of(work, struct ath12k_link_vif,
4101 						     bcn_tx_work);
4102 
4103 	lockdep_assert_wiphy(wiphy);
4104 	ath12k_mac_bcn_tx_event(arvif);
4105 }
4106 
4107 static void ath12k_mac_init_arvif(struct ath12k_vif *ahvif,
4108 				  struct ath12k_link_vif *arvif, int link_id)
4109 {
4110 	struct ath12k_hw *ah = ahvif->ah;
4111 	u8 _link_id;
4112 	int i;
4113 
4114 	lockdep_assert_wiphy(ah->hw->wiphy);
4115 
4116 	if (WARN_ON(!arvif))
4117 		return;
4118 
4119 	if (WARN_ON(link_id >= ATH12K_NUM_MAX_LINKS))
4120 		return;
4121 
4122 	if (link_id < 0)
4123 		_link_id = 0;
4124 	else
4125 		_link_id = link_id;
4126 
4127 	arvif->ahvif = ahvif;
4128 	arvif->link_id = _link_id;
4129 
4130 	/* Protects the datapath stats update on a per link basis */
4131 	spin_lock_init(&arvif->link_stats_lock);
4132 
4133 	INIT_LIST_HEAD(&arvif->list);
4134 	INIT_DELAYED_WORK(&arvif->connection_loss_work,
4135 			  ath12k_mac_vif_sta_connection_loss_work);
4136 	wiphy_work_init(&arvif->bcn_tx_work, ath12k_mac_bcn_tx_work);
4137 
4138 	arvif->num_stations = 0;
4139 
4140 	for (i = 0; i < ARRAY_SIZE(arvif->bitrate_mask.control); i++) {
4141 		arvif->bitrate_mask.control[i].legacy = 0xffffffff;
4142 		arvif->bitrate_mask.control[i].gi = NL80211_TXRATE_DEFAULT_GI;
4143 		memset(arvif->bitrate_mask.control[i].ht_mcs, 0xff,
4144 		       sizeof(arvif->bitrate_mask.control[i].ht_mcs));
4145 		memset(arvif->bitrate_mask.control[i].vht_mcs, 0xff,
4146 		       sizeof(arvif->bitrate_mask.control[i].vht_mcs));
4147 		memset(arvif->bitrate_mask.control[i].he_mcs, 0xff,
4148 		       sizeof(arvif->bitrate_mask.control[i].he_mcs));
4149 		memset(arvif->bitrate_mask.control[i].eht_mcs, 0xff,
4150 		       sizeof(arvif->bitrate_mask.control[i].eht_mcs));
4151 	}
4152 
4153 	/* Handle MLO related assignments */
4154 	if (link_id >= 0) {
4155 		rcu_assign_pointer(ahvif->link[arvif->link_id], arvif);
4156 		ahvif->links_map |= BIT(_link_id);
4157 	}
4158 
4159 	ath12k_generic_dbg(ATH12K_DBG_MAC,
4160 			   "mac init link arvif (link_id %d%s) for vif %pM. links_map 0x%x",
4161 			   _link_id, (link_id < 0) ? " deflink" : "", ahvif->vif->addr,
4162 			   ahvif->links_map);
4163 }
4164 
4165 static void ath12k_mac_remove_link_interface(struct ieee80211_hw *hw,
4166 					     struct ath12k_link_vif *arvif)
4167 {
4168 	struct ath12k_vif *ahvif = arvif->ahvif;
4169 	struct ath12k_hw *ah = hw->priv;
4170 	struct ath12k *ar = arvif->ar;
4171 	int ret;
4172 
4173 	lockdep_assert_wiphy(ah->hw->wiphy);
4174 
4175 	cancel_delayed_work_sync(&arvif->connection_loss_work);
4176 	wiphy_work_cancel(ath12k_ar_to_hw(ar)->wiphy, &arvif->bcn_tx_work);
4177 
4178 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac remove link interface (vdev %d link id %d)",
4179 		   arvif->vdev_id, arvif->link_id);
4180 
4181 	if (test_bit(WMI_TLV_SERVICE_11D_OFFLOAD, ar->ab->wmi_ab.svc_map) &&
4182 	    ahvif->vdev_type == WMI_VDEV_TYPE_STA &&
4183 	    ahvif->vdev_subtype == WMI_VDEV_SUBTYPE_NONE)
4184 		ath12k_mac_11d_scan_stop(ar);
4185 
4186 	if (ahvif->vdev_type == WMI_VDEV_TYPE_AP) {
4187 		ret = ath12k_peer_delete(ar, arvif->vdev_id, arvif->bssid);
4188 		if (ret)
4189 			ath12k_warn(ar->ab, "failed to submit AP self-peer removal on vdev %d link id %d: %d",
4190 				    arvif->vdev_id, arvif->link_id, ret);
4191 
4192 		if (arvif->link_id < IEEE80211_MLD_MAX_NUM_LINKS)
4193 			ath12k_dp_peer_delete(&ah->dp_hw, arvif->bssid, NULL);
4194 	}
4195 	ath12k_mac_vdev_delete(ar, arvif);
4196 }
4197 
4198 static struct ath12k_link_vif *ath12k_mac_assign_link_vif(struct ath12k_hw *ah,
4199 							  struct ieee80211_vif *vif,
4200 							  u8 link_id)
4201 {
4202 	struct ath12k_vif *ahvif = ath12k_vif_to_ahvif(vif);
4203 	struct ath12k_link_vif *arvif;
4204 
4205 	lockdep_assert_wiphy(ah->hw->wiphy);
4206 
4207 	arvif = wiphy_dereference(ah->hw->wiphy, ahvif->link[link_id]);
4208 	if (arvif)
4209 		return arvif;
4210 
4211 	/* If this is the first link arvif being created for an ML VIF
4212 	 * use the preallocated deflink memory except for scan arvifs
4213 	 */
4214 	if (!ahvif->links_map && link_id < ATH12K_FIRST_SCAN_LINK) {
4215 		arvif = &ahvif->deflink;
4216 
4217 		if (vif->type == NL80211_IFTYPE_STATION)
4218 			arvif->is_sta_assoc_link = true;
4219 	} else {
4220 		arvif = kzalloc_obj(*arvif);
4221 		if (!arvif)
4222 			return NULL;
4223 	}
4224 
4225 	ath12k_mac_init_arvif(ahvif, arvif, link_id);
4226 
4227 	return arvif;
4228 }
4229 
4230 static void ath12k_mac_unassign_link_vif(struct ath12k_link_vif *arvif)
4231 {
4232 	struct ath12k_vif *ahvif = arvif->ahvif;
4233 	struct ath12k_hw *ah = ahvif->ah;
4234 
4235 	lockdep_assert_wiphy(ah->hw->wiphy);
4236 
4237 	rcu_assign_pointer(ahvif->link[arvif->link_id], NULL);
4238 	synchronize_rcu();
4239 	ahvif->links_map &= ~BIT(arvif->link_id);
4240 
4241 	if (arvif != &ahvif->deflink)
4242 		kfree(arvif);
4243 	else
4244 		memset(arvif, 0, sizeof(*arvif));
4245 }
4246 
4247 int
4248 ath12k_mac_op_change_vif_links(struct ieee80211_hw *hw,
4249 			       struct ieee80211_vif *vif,
4250 			       u16 old_links, u16 new_links,
4251 			       struct ieee80211_bss_conf *ol[IEEE80211_MLD_MAX_NUM_LINKS])
4252 {
4253 	struct ath12k_vif *ahvif = ath12k_vif_to_ahvif(vif);
4254 	unsigned long to_remove = old_links & ~new_links;
4255 	unsigned long to_add = ~old_links & new_links;
4256 	struct ath12k_hw *ah = ath12k_hw_to_ah(hw);
4257 	struct ath12k_link_vif *arvif;
4258 	u8 link_id;
4259 
4260 	lockdep_assert_wiphy(hw->wiphy);
4261 
4262 	ath12k_generic_dbg(ATH12K_DBG_MAC,
4263 			   "mac vif link changed for MLD %pM old_links 0x%x new_links 0x%x\n",
4264 			   vif->addr, old_links, new_links);
4265 
4266 	for_each_set_bit(link_id, &to_add, IEEE80211_MLD_MAX_NUM_LINKS) {
4267 		arvif = wiphy_dereference(hw->wiphy, ahvif->link[link_id]);
4268 		/* mac80211 wants to add link but driver already has the
4269 		 * link. This should not happen ideally.
4270 		 */
4271 		if (WARN_ON(arvif))
4272 			return -EINVAL;
4273 
4274 		arvif = ath12k_mac_assign_link_vif(ah, vif, link_id);
4275 		if (WARN_ON(!arvif))
4276 			return -EINVAL;
4277 	}
4278 
4279 	for_each_set_bit(link_id, &to_remove, IEEE80211_MLD_MAX_NUM_LINKS) {
4280 		arvif = wiphy_dereference(hw->wiphy, ahvif->link[link_id]);
4281 		if (WARN_ON(!arvif))
4282 			return -EINVAL;
4283 
4284 		if (!arvif->is_created) {
4285 			ath12k_mac_unassign_link_vif(arvif);
4286 			continue;
4287 		}
4288 
4289 		if (WARN_ON(!arvif->ar))
4290 			return -EINVAL;
4291 
4292 		ath12k_mac_remove_link_interface(hw, arvif);
4293 		ath12k_mac_unassign_link_vif(arvif);
4294 	}
4295 
4296 	return 0;
4297 }
4298 EXPORT_SYMBOL(ath12k_mac_op_change_vif_links);
4299 
4300 static int ath12k_mac_fils_discovery(struct ath12k_link_vif *arvif,
4301 				     struct ieee80211_bss_conf *info)
4302 {
4303 	struct ieee80211_vif *vif = ath12k_ahvif_to_vif(arvif->ahvif);
4304 	struct ath12k *ar = arvif->ar;
4305 	struct ieee80211_hw *hw = ath12k_ar_to_hw(ar);
4306 	struct sk_buff *tmpl;
4307 	int ret;
4308 	u32 interval;
4309 	bool unsol_bcast_probe_resp_enabled = false;
4310 
4311 	if (info->fils_discovery.max_interval) {
4312 		interval = info->fils_discovery.max_interval;
4313 
4314 		tmpl = ieee80211_get_fils_discovery_tmpl(hw, vif);
4315 		if (tmpl)
4316 			ret = ath12k_wmi_fils_discovery_tmpl(ar, arvif->vdev_id,
4317 							     tmpl);
4318 	} else if (info->unsol_bcast_probe_resp_interval) {
4319 		unsol_bcast_probe_resp_enabled = 1;
4320 		interval = info->unsol_bcast_probe_resp_interval;
4321 
4322 		tmpl = ieee80211_get_unsol_bcast_probe_resp_tmpl(hw, vif);
4323 		if (tmpl)
4324 			ret = ath12k_wmi_probe_resp_tmpl(ar, arvif->vdev_id,
4325 							 tmpl);
4326 	} else { /* Disable */
4327 		return ath12k_wmi_fils_discovery(ar, arvif->vdev_id, 0, false);
4328 	}
4329 
4330 	if (!tmpl) {
4331 		ath12k_warn(ar->ab,
4332 			    "mac vdev %i failed to retrieve %s template\n",
4333 			    arvif->vdev_id, (unsol_bcast_probe_resp_enabled ?
4334 			    "unsolicited broadcast probe response" :
4335 			    "FILS discovery"));
4336 		return -EPERM;
4337 	}
4338 	kfree_skb(tmpl);
4339 
4340 	if (!ret)
4341 		ret = ath12k_wmi_fils_discovery(ar, arvif->vdev_id, interval,
4342 						unsol_bcast_probe_resp_enabled);
4343 
4344 	return ret;
4345 }
4346 
4347 void ath12k_mac_op_vif_cfg_changed(struct ieee80211_hw *hw,
4348 				   struct ieee80211_vif *vif,
4349 				   u64 changed)
4350 {
4351 	struct ath12k_vif *ahvif = ath12k_vif_to_ahvif(vif);
4352 	unsigned long links = ahvif->links_map;
4353 	struct ieee80211_bss_conf *info;
4354 	struct ath12k_link_vif *arvif;
4355 	struct ieee80211_sta *sta;
4356 	struct ath12k_sta *ahsta;
4357 	struct ath12k *ar;
4358 	u8 link_id;
4359 
4360 	lockdep_assert_wiphy(hw->wiphy);
4361 
4362 	if (changed & BSS_CHANGED_SSID && vif->type == NL80211_IFTYPE_AP) {
4363 		ahvif->u.ap.ssid_len = vif->cfg.ssid_len;
4364 		if (vif->cfg.ssid_len)
4365 			memcpy(ahvif->u.ap.ssid, vif->cfg.ssid, vif->cfg.ssid_len);
4366 	}
4367 
4368 	if (changed & BSS_CHANGED_ASSOC) {
4369 		if (vif->cfg.assoc) {
4370 			/* only in station mode we can get here, so it's safe
4371 			 * to use ap_addr
4372 			 */
4373 			rcu_read_lock();
4374 			sta = ieee80211_find_sta(vif, vif->cfg.ap_addr);
4375 			if (!sta) {
4376 				rcu_read_unlock();
4377 				WARN_ONCE(1, "failed to find sta with addr %pM\n",
4378 					  vif->cfg.ap_addr);
4379 				return;
4380 			}
4381 
4382 			ahsta = ath12k_sta_to_ahsta(sta);
4383 			arvif = wiphy_dereference(hw->wiphy,
4384 						  ahvif->link[ahsta->assoc_link_id]);
4385 			rcu_read_unlock();
4386 
4387 			ar = arvif->ar;
4388 			/* there is no reason for which an assoc link's
4389 			 * bss info does not exist
4390 			 */
4391 			info = ath12k_mac_get_link_bss_conf(arvif);
4392 			ath12k_bss_assoc(ar, arvif, info);
4393 
4394 			/* exclude assoc link as it is done above */
4395 			links &= ~BIT(ahsta->assoc_link_id);
4396 		}
4397 
4398 		for_each_set_bit(link_id, &links, IEEE80211_MLD_MAX_NUM_LINKS) {
4399 			arvif = wiphy_dereference(hw->wiphy, ahvif->link[link_id]);
4400 			if (!arvif || !arvif->ar)
4401 				continue;
4402 
4403 			ar = arvif->ar;
4404 
4405 			if (vif->cfg.assoc) {
4406 				info = ath12k_mac_get_link_bss_conf(arvif);
4407 				if (!info)
4408 					continue;
4409 
4410 				ath12k_bss_assoc(ar, arvif, info);
4411 			} else {
4412 				ath12k_bss_disassoc(ar, arvif);
4413 			}
4414 		}
4415 	}
4416 }
4417 EXPORT_SYMBOL(ath12k_mac_op_vif_cfg_changed);
4418 
4419 static void ath12k_mac_vif_setup_ps(struct ath12k_link_vif *arvif)
4420 {
4421 	struct ath12k *ar = arvif->ar;
4422 	struct ieee80211_vif *vif = arvif->ahvif->vif;
4423 	struct ieee80211_conf *conf = &ath12k_ar_to_hw(ar)->conf;
4424 	enum wmi_sta_powersave_param param;
4425 	struct ieee80211_bss_conf *info;
4426 	enum wmi_sta_ps_mode psmode;
4427 	int ret;
4428 	int timeout;
4429 	bool enable_ps;
4430 
4431 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
4432 
4433 	if (vif->type != NL80211_IFTYPE_STATION)
4434 		return;
4435 
4436 	enable_ps = arvif->ahvif->ps;
4437 	if (enable_ps) {
4438 		psmode = WMI_STA_PS_MODE_ENABLED;
4439 		param = WMI_STA_PS_PARAM_INACTIVITY_TIME;
4440 
4441 		timeout = conf->dynamic_ps_timeout;
4442 		if (timeout == 0) {
4443 			info = ath12k_mac_get_link_bss_conf(arvif);
4444 			if (!info) {
4445 				ath12k_warn(ar->ab, "unable to access bss link conf in setup ps for vif %pM link %u\n",
4446 					    vif->addr, arvif->link_id);
4447 				return;
4448 			}
4449 
4450 			/* firmware doesn't like 0 */
4451 			timeout = ieee80211_tu_to_usec(info->beacon_int) / 1000;
4452 		}
4453 
4454 		ret = ath12k_wmi_set_sta_ps_param(ar, arvif->vdev_id, param,
4455 						  timeout);
4456 		if (ret) {
4457 			ath12k_warn(ar->ab, "failed to set inactivity time for vdev %d: %i\n",
4458 				    arvif->vdev_id, ret);
4459 			return;
4460 		}
4461 	} else {
4462 		psmode = WMI_STA_PS_MODE_DISABLED;
4463 	}
4464 
4465 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac vdev %d psmode %s\n",
4466 		   arvif->vdev_id, psmode ? "enable" : "disable");
4467 
4468 	ret = ath12k_wmi_pdev_set_ps_mode(ar, arvif->vdev_id, psmode);
4469 	if (ret)
4470 		ath12k_warn(ar->ab, "failed to set sta power save mode %d for vdev %d: %d\n",
4471 			    psmode, arvif->vdev_id, ret);
4472 }
4473 
4474 static bool ath12k_mac_supports_tpc(struct ath12k *ar, struct ath12k_vif *ahvif,
4475 				    const struct cfg80211_chan_def *chandef)
4476 {
4477 	return ath12k_wmi_supports_6ghz_cc_ext(ar) &&
4478 		test_bit(WMI_TLV_SERVICE_EXT_TPC_REG_SUPPORT, ar->ab->wmi_ab.svc_map) &&
4479 		(ahvif->vdev_type == WMI_VDEV_TYPE_STA  ||
4480 		 ahvif->vdev_type == WMI_VDEV_TYPE_AP) &&
4481 		ahvif->vdev_subtype == WMI_VDEV_SUBTYPE_NONE &&
4482 		chandef->chan &&
4483 		chandef->chan->band == NL80211_BAND_6GHZ;
4484 }
4485 
4486 static void ath12k_wmi_vdev_params_up(struct ath12k *ar,
4487 				      struct ath12k_link_vif *arvif,
4488 				      struct ath12k_link_vif *tx_arvif,
4489 				      struct ieee80211_bss_conf *info, u16 aid)
4490 {
4491 	struct ath12k_wmi_vdev_up_params params = {
4492 		.vdev_id = arvif->vdev_id,
4493 		.aid = aid,
4494 		.bssid = arvif->bssid
4495 	};
4496 	int ret;
4497 
4498 	if (tx_arvif) {
4499 		params.tx_bssid = tx_arvif->bssid;
4500 		params.nontx_profile_idx = info->bssid_index;
4501 		params.nontx_profile_cnt = 1 << info->bssid_indicator;
4502 	}
4503 
4504 	ret = ath12k_wmi_vdev_up(arvif->ar, &params);
4505 	if (ret)
4506 		ath12k_warn(ar->ab, "failed to bring vdev up %d: %d\n",
4507 			    arvif->vdev_id, ret);
4508 }
4509 
4510 static int ath12k_mac_config_obss_pd(struct ath12k_link_vif *arvif,
4511 				     const struct ieee80211_he_obss_pd *he_obss_pd)
4512 {
4513 	struct ath12k_wmi_obss_pd_arg obss_pd_arg = {};
4514 	u32 srg_bitmap[2], non_srg_bitmap[2];
4515 	struct ath12k *ar = arvif->ar;
4516 	u32 param_id, pdev_id;
4517 	u32 param_val;
4518 	int ret;
4519 
4520 	if (ar->ab->hw_params->single_pdev_only)
4521 		pdev_id = ath12k_mac_get_target_pdev_id_from_vif(arvif);
4522 	else
4523 		pdev_id = ar->pdev->pdev_id;
4524 
4525 	/* Set and enable SRG/non-SRG OBSS PD threshold */
4526 	param_id = WMI_PDEV_PARAM_SET_CMD_OBSS_PD_THRESHOLD;
4527 	if (ar->monitor_started || !he_obss_pd->enable) {
4528 		ret = ath12k_wmi_pdev_set_param(ar, param_id, 0, pdev_id);
4529 		if (ret)
4530 			ath12k_warn(ar->ab,
4531 				    "failed to set OBSS PD threshold for pdev %u: %d\n",
4532 				    pdev_id, ret);
4533 		return ret;
4534 	}
4535 
4536 	/*
4537 	 * This service flag indicates firmware support for SRG/SRP-based
4538 	 * spatial reuse. It also specifies whether OBSS PD threshold values
4539 	 * should be interpreted as dB (offset) or dBm (absolute) units.
4540 	 */
4541 	obss_pd_arg.srp_support = test_bit(WMI_TLV_SERVICE_SRG_SRP_SPATIAL_REUSE_SUPPORT,
4542 					   ar->ab->wmi_ab.svc_map);
4543 
4544 	if (!(he_obss_pd->sr_ctrl &
4545 	      IEEE80211_HE_SPR_NON_SRG_OBSS_PD_SR_DISALLOWED)) {
4546 		if (he_obss_pd->sr_ctrl & IEEE80211_HE_SPR_NON_SRG_OFFSET_PRESENT)
4547 			obss_pd_arg.non_srg_th = ATH12K_OBSS_PD_MAX_THRESHOLD +
4548 						 he_obss_pd->non_srg_max_offset;
4549 		else
4550 			obss_pd_arg.non_srg_th = ATH12K_OBSS_PD_NON_SRG_MAX_THRESHOLD;
4551 
4552 		if (!obss_pd_arg.srp_support)
4553 			obss_pd_arg.non_srg_th -= ATH12K_DEFAULT_NOISE_FLOOR;
4554 
4555 		obss_pd_arg.non_srg_enabled = true;
4556 	}
4557 
4558 	if (he_obss_pd->sr_ctrl & IEEE80211_HE_SPR_SRG_INFORMATION_PRESENT) {
4559 		obss_pd_arg.srg_th = ATH12K_OBSS_PD_MAX_THRESHOLD +
4560 				     he_obss_pd->max_offset;
4561 		obss_pd_arg.srg_enabled = true;
4562 	}
4563 
4564 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
4565 		   "pdev %u OBSS PD sr_ctrl 0x%x srg_th %d dBm non_srg_th %d dBm\n",
4566 		   pdev_id, he_obss_pd->sr_ctrl,
4567 		   obss_pd_arg.srg_th, obss_pd_arg.non_srg_th);
4568 
4569 	param_val = ath12k_wmi_build_obss_pd(&obss_pd_arg);
4570 	ret = ath12k_wmi_pdev_set_param(ar, param_id, param_val, pdev_id);
4571 	if (ret) {
4572 		ath12k_warn(ar->ab,
4573 			    "failed to set OBSS PD threshold for pdev %u: %d\n",
4574 			    pdev_id, ret);
4575 		return ret;
4576 	}
4577 
4578 	/* Enable OBSS PD for all access category */
4579 	param_id  = WMI_PDEV_PARAM_SET_CMD_OBSS_PD_PER_AC;
4580 	param_val = 0xf;
4581 	ret = ath12k_wmi_pdev_set_param(ar, param_id, param_val, pdev_id);
4582 	if (ret) {
4583 		ath12k_warn(ar->ab,
4584 			    "failed to set OBSS PD per ac for pdev %u: %d\n",
4585 			    pdev_id, ret);
4586 		return ret;
4587 	}
4588 
4589 	/* Set SR prohibit */
4590 	param_id  = WMI_PDEV_PARAM_ENABLE_SR_PROHIBIT;
4591 	param_val = !!(he_obss_pd->sr_ctrl &
4592 		       IEEE80211_HE_SPR_HESIGA_SR_VAL15_ALLOWED);
4593 	ret = ath12k_wmi_pdev_set_param(ar, param_id, param_val, pdev_id);
4594 	if (ret) {
4595 		ath12k_warn(ar->ab, "failed to set SR prohibit for pdev %u: %d\n",
4596 			    pdev_id, ret);
4597 		return ret;
4598 	}
4599 
4600 	if (!obss_pd_arg.srp_support)
4601 		return 0;
4602 
4603 	memcpy(srg_bitmap, he_obss_pd->bss_color_bitmap, sizeof(srg_bitmap));
4604 	/* Set SRG BSS color bitmap */
4605 	ret = ath12k_wmi_pdev_set_srg_bss_color_bitmap(ar, pdev_id, srg_bitmap);
4606 	if (ret) {
4607 		ath12k_warn(ar->ab,
4608 			    "failed to set SRG bss color bitmap for pdev %u: %d\n",
4609 			    pdev_id, ret);
4610 		return ret;
4611 	}
4612 
4613 	/* Enable BSS colors for SRG */
4614 	ret = ath12k_wmi_pdev_srg_obss_color_enable_bitmap(ar, pdev_id, srg_bitmap);
4615 	if (ret) {
4616 		ath12k_warn(ar->ab,
4617 			    "failed to enable SRG bss color bitmap pdev %u: %d\n",
4618 			    pdev_id, ret);
4619 		return ret;
4620 	}
4621 
4622 	memcpy(srg_bitmap, he_obss_pd->partial_bssid_bitmap, sizeof(srg_bitmap));
4623 	/* Set SRG partial bssid bitmap */
4624 	ret = ath12k_wmi_pdev_set_srg_partial_bssid_bitmap(ar, pdev_id, srg_bitmap);
4625 	if (ret) {
4626 		ath12k_warn(ar->ab,
4627 			    "failed to set SRG partial bssid bitmap for pdev %u: %d\n",
4628 			    pdev_id, ret);
4629 		return ret;
4630 	}
4631 
4632 	/* Enable partial bssid mask for SRG */
4633 	ret = ath12k_wmi_pdev_srg_obss_bssid_enable_bitmap(ar, pdev_id, srg_bitmap);
4634 	if (ret) {
4635 		ath12k_warn(ar->ab,
4636 			    "failed to enable SRG bssid bitmap pdev %u: %d\n",
4637 			    pdev_id, ret);
4638 		return ret;
4639 	}
4640 
4641 	/*
4642 	 * No explicit non-SRG bitmap from mac80211; enable all colors/bssids
4643 	 * as non-SRG candidates. Actual SRG members are filtered by SRG bitmaps.
4644 	 */
4645 	memset(non_srg_bitmap, 0xff, sizeof(non_srg_bitmap));
4646 
4647 	/* Enable BSS colors for non-SRG */
4648 	ret = ath12k_wmi_pdev_non_srg_obss_color_enable_bitmap(ar, pdev_id,
4649 							       non_srg_bitmap);
4650 	if (ret) {
4651 		ath12k_warn(ar->ab,
4652 			    "failed to enable non SRG color bitmap pdev %u: %d\n",
4653 			    pdev_id, ret);
4654 		return ret;
4655 	}
4656 
4657 	/* Enable partial bssid mask for non-SRG */
4658 	ret = ath12k_wmi_pdev_non_srg_obss_bssid_enable_bitmap(ar, pdev_id,
4659 							       non_srg_bitmap);
4660 	if (ret) {
4661 		ath12k_warn(ar->ab,
4662 			    "failed to enable non SRG bssid bitmap pdev %u: %d\n",
4663 			    pdev_id, ret);
4664 		return ret;
4665 	}
4666 
4667 	return 0;
4668 }
4669 
4670 static void ath12k_mac_bss_info_changed(struct ath12k *ar,
4671 					struct ath12k_link_vif *arvif,
4672 					struct ieee80211_bss_conf *info,
4673 					u64 changed)
4674 {
4675 	struct ath12k_vif *ahvif = arvif->ahvif;
4676 	struct ieee80211_vif *vif = ath12k_ahvif_to_vif(ahvif);
4677 	struct ieee80211_vif_cfg *vif_cfg = &vif->cfg;
4678 	struct ath12k_link_vif *tx_arvif;
4679 	struct cfg80211_chan_def def;
4680 	u32 param_id, param_value;
4681 	enum nl80211_band band;
4682 	u32 vdev_param;
4683 	int mcast_rate;
4684 	u32 preamble;
4685 	u16 hw_value;
4686 	u16 bitrate;
4687 	u8 rateidx;
4688 	u32 rate;
4689 	int ret;
4690 
4691 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
4692 
4693 	if (changed & BSS_CHANGED_BEACON_INT) {
4694 		arvif->beacon_interval = info->beacon_int;
4695 
4696 		param_id = WMI_VDEV_PARAM_BEACON_INTERVAL;
4697 		ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
4698 						    param_id,
4699 						    arvif->beacon_interval);
4700 		if (ret)
4701 			ath12k_warn(ar->ab, "Failed to set beacon interval for VDEV: %d\n",
4702 				    arvif->vdev_id);
4703 		else
4704 			ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
4705 				   "Beacon interval: %d set for VDEV: %d\n",
4706 				   arvif->beacon_interval, arvif->vdev_id);
4707 	}
4708 
4709 	if (changed & BSS_CHANGED_BEACON) {
4710 		param_id = WMI_PDEV_PARAM_BEACON_TX_MODE;
4711 		param_value = WMI_BEACON_BURST_MODE;
4712 		ret = ath12k_wmi_pdev_set_param(ar, param_id,
4713 						param_value, ar->pdev->pdev_id);
4714 		if (ret)
4715 			ath12k_warn(ar->ab, "Failed to set beacon mode for VDEV: %d\n",
4716 				    arvif->vdev_id);
4717 		else
4718 			ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
4719 				   "Set burst beacon mode for VDEV: %d\n",
4720 				   arvif->vdev_id);
4721 
4722 		/* In MBSSID case, need to install transmitting VIF's template first */
4723 
4724 		ret = ath12k_mac_setup_bcn_tmpl(arvif);
4725 		if (ret)
4726 			ath12k_warn(ar->ab, "failed to update bcn template: %d\n",
4727 				    ret);
4728 
4729 		if (!arvif->is_csa_in_progress)
4730 			goto skip_vdev_up;
4731 
4732 		tx_arvif = ath12k_mac_get_tx_arvif(arvif, info);
4733 		if (tx_arvif && arvif != tx_arvif && tx_arvif->is_csa_in_progress)
4734 			/* skip non tx vif's */
4735 			goto skip_vdev_up;
4736 
4737 		ath12k_wmi_vdev_params_up(ar, arvif, tx_arvif, info, ahvif->aid);
4738 
4739 		arvif->is_csa_in_progress = false;
4740 
4741 		if (tx_arvif && arvif == tx_arvif) {
4742 			struct ath12k_link_vif *arvif_itr;
4743 
4744 			list_for_each_entry(arvif_itr, &ar->arvifs, list) {
4745 				if (!arvif_itr->is_csa_in_progress)
4746 					continue;
4747 
4748 				ath12k_wmi_vdev_params_up(ar, arvif, tx_arvif,
4749 							  info, ahvif->aid);
4750 				arvif_itr->is_csa_in_progress = false;
4751 			}
4752 		}
4753 	}
4754 
4755 skip_vdev_up:
4756 
4757 	if (changed & (BSS_CHANGED_BEACON_INFO | BSS_CHANGED_BEACON)) {
4758 		arvif->dtim_period = info->dtim_period;
4759 
4760 		param_id = WMI_VDEV_PARAM_DTIM_PERIOD;
4761 		ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
4762 						    param_id,
4763 						    arvif->dtim_period);
4764 
4765 		if (ret)
4766 			ath12k_warn(ar->ab, "Failed to set dtim period for VDEV %d: %i\n",
4767 				    arvif->vdev_id, ret);
4768 		else
4769 			ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
4770 				   "DTIM period: %d set for VDEV: %d\n",
4771 				   arvif->dtim_period, arvif->vdev_id);
4772 	}
4773 
4774 	if (changed & BSS_CHANGED_SSID &&
4775 	    vif->type == NL80211_IFTYPE_AP) {
4776 		ahvif->u.ap.ssid_len = vif->cfg.ssid_len;
4777 		if (vif->cfg.ssid_len)
4778 			memcpy(ahvif->u.ap.ssid, vif->cfg.ssid, vif->cfg.ssid_len);
4779 		ahvif->u.ap.hidden_ssid = info->hidden_ssid;
4780 	}
4781 
4782 	if (changed & BSS_CHANGED_BSSID && !is_zero_ether_addr(info->bssid))
4783 		ether_addr_copy(arvif->bssid, info->bssid);
4784 
4785 	if (changed & BSS_CHANGED_BEACON_ENABLED) {
4786 		if (info->enable_beacon) {
4787 			ret = ath12k_mac_set_he_txbf_conf(arvif);
4788 			if (ret)
4789 				ath12k_warn(ar->ab,
4790 					    "failed to set HE TXBF config for vdev: %d\n",
4791 					    arvif->vdev_id);
4792 
4793 			ret = ath12k_mac_set_eht_txbf_conf(arvif);
4794 			if (ret)
4795 				ath12k_warn(ar->ab,
4796 					    "failed to set EHT TXBF config for vdev: %d\n",
4797 					    arvif->vdev_id);
4798 		}
4799 		ath12k_control_beaconing(arvif, info);
4800 
4801 		if (arvif->is_up && info->he_support &&
4802 		    info->he_oper.params) {
4803 			/* TODO: Extend to support 1024 BA Bitmap size */
4804 			ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
4805 							    WMI_VDEV_PARAM_BA_MODE,
4806 							    WMI_BA_MODE_BUFFER_SIZE_256);
4807 			if (ret)
4808 				ath12k_warn(ar->ab,
4809 					    "failed to set BA BUFFER SIZE 256 for vdev: %d\n",
4810 					    arvif->vdev_id);
4811 
4812 			param_id = WMI_VDEV_PARAM_HEOPS_0_31;
4813 			param_value = info->he_oper.params;
4814 			ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
4815 							    param_id, param_value);
4816 			ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
4817 				   "he oper param: %x set for VDEV: %d\n",
4818 				   param_value, arvif->vdev_id);
4819 
4820 			if (ret)
4821 				ath12k_warn(ar->ab, "Failed to set he oper params %x for VDEV %d: %i\n",
4822 					    param_value, arvif->vdev_id, ret);
4823 		}
4824 	}
4825 
4826 	if (changed & BSS_CHANGED_ERP_CTS_PROT) {
4827 		u32 cts_prot;
4828 
4829 		cts_prot = !!(info->use_cts_prot);
4830 		param_id = WMI_VDEV_PARAM_PROTECTION_MODE;
4831 
4832 		if (arvif->is_started) {
4833 			ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
4834 							    param_id, cts_prot);
4835 			if (ret)
4836 				ath12k_warn(ar->ab, "Failed to set CTS prot for VDEV: %d\n",
4837 					    arvif->vdev_id);
4838 			else
4839 				ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "Set CTS prot: %d for VDEV: %d\n",
4840 					   cts_prot, arvif->vdev_id);
4841 		} else {
4842 			ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "defer protection mode setup, vdev is not ready yet\n");
4843 		}
4844 	}
4845 
4846 	if (changed & BSS_CHANGED_ERP_SLOT) {
4847 		u32 slottime;
4848 
4849 		if (info->use_short_slot)
4850 			slottime = WMI_VDEV_SLOT_TIME_SHORT; /* 9us */
4851 
4852 		else
4853 			slottime = WMI_VDEV_SLOT_TIME_LONG; /* 20us */
4854 
4855 		param_id = WMI_VDEV_PARAM_SLOT_TIME;
4856 		ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
4857 						    param_id, slottime);
4858 		if (ret)
4859 			ath12k_warn(ar->ab, "Failed to set erp slot for VDEV: %d\n",
4860 				    arvif->vdev_id);
4861 		else
4862 			ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
4863 				   "Set slottime: %d for VDEV: %d\n",
4864 				   slottime, arvif->vdev_id);
4865 	}
4866 
4867 	if (changed & BSS_CHANGED_ERP_PREAMBLE) {
4868 		u32 preamble;
4869 
4870 		if (info->use_short_preamble)
4871 			preamble = WMI_VDEV_PREAMBLE_SHORT;
4872 		else
4873 			preamble = WMI_VDEV_PREAMBLE_LONG;
4874 
4875 		param_id = WMI_VDEV_PARAM_PREAMBLE;
4876 		ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
4877 						    param_id, preamble);
4878 		if (ret)
4879 			ath12k_warn(ar->ab, "Failed to set preamble for VDEV: %d\n",
4880 				    arvif->vdev_id);
4881 		else
4882 			ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
4883 				   "Set preamble: %d for VDEV: %d\n",
4884 				   preamble, arvif->vdev_id);
4885 	}
4886 
4887 	if (changed & BSS_CHANGED_ASSOC) {
4888 		if (vif->cfg.assoc)
4889 			ath12k_bss_assoc(ar, arvif, info);
4890 		else
4891 			ath12k_bss_disassoc(ar, arvif);
4892 	}
4893 
4894 	if (changed & BSS_CHANGED_TXPOWER) {
4895 		ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac vdev_id %i txpower %d\n",
4896 			   arvif->vdev_id, info->txpower);
4897 
4898 		arvif->txpower = info->txpower;
4899 		ath12k_mac_txpower_recalc(ar);
4900 	}
4901 
4902 	if (changed & BSS_CHANGED_MCAST_RATE &&
4903 	    !ath12k_mac_vif_link_chan(vif, arvif->link_id, &def)) {
4904 		band = def.chan->band;
4905 		mcast_rate = info->mcast_rate[band];
4906 
4907 		if (mcast_rate > 0) {
4908 			rateidx = mcast_rate - 1;
4909 		} else {
4910 			if (info->basic_rates)
4911 				rateidx = __ffs(info->basic_rates);
4912 			else
4913 				rateidx = 0;
4914 		}
4915 
4916 		if (ar->pdev->cap.supported_bands & WMI_HOST_WLAN_5GHZ_CAP)
4917 			rateidx += ATH12K_MAC_FIRST_OFDM_RATE_IDX;
4918 
4919 		bitrate = ath12k_legacy_rates[rateidx].bitrate;
4920 		hw_value = ath12k_legacy_rates[rateidx].hw_value;
4921 
4922 		if (ath12k_mac_bitrate_is_cck(bitrate))
4923 			preamble = WMI_RATE_PREAMBLE_CCK;
4924 		else
4925 			preamble = WMI_RATE_PREAMBLE_OFDM;
4926 
4927 		rate = ATH12K_HW_RATE_CODE(hw_value, 0, preamble);
4928 
4929 		ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
4930 			   "mac vdev %d mcast_rate %x\n",
4931 			   arvif->vdev_id, rate);
4932 
4933 		vdev_param = WMI_VDEV_PARAM_MCAST_DATA_RATE;
4934 		ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
4935 						    vdev_param, rate);
4936 		if (ret)
4937 			ath12k_warn(ar->ab,
4938 				    "failed to set mcast rate on vdev %i: %d\n",
4939 				    arvif->vdev_id,  ret);
4940 
4941 		vdev_param = WMI_VDEV_PARAM_BCAST_DATA_RATE;
4942 		ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
4943 						    vdev_param, rate);
4944 		if (ret)
4945 			ath12k_warn(ar->ab,
4946 				    "failed to set bcast rate on vdev %i: %d\n",
4947 				    arvif->vdev_id,  ret);
4948 	}
4949 
4950 	if (changed & BSS_CHANGED_BASIC_RATES &&
4951 	    !ath12k_mac_vif_link_chan(vif, arvif->link_id, &def))
4952 		ath12k_recalculate_mgmt_rate(ar, arvif, &def);
4953 
4954 	if (changed & BSS_CHANGED_TWT) {
4955 		if (info->twt_requester || info->twt_responder)
4956 			ath12k_wmi_send_twt_enable_cmd(ar, ar->pdev->pdev_id);
4957 		else
4958 			ath12k_wmi_send_twt_disable_cmd(ar, ar->pdev->pdev_id);
4959 	}
4960 
4961 	if (changed & BSS_CHANGED_HE_OBSS_PD) {
4962 		if (vif->type == NL80211_IFTYPE_AP)
4963 			ath12k_mac_config_obss_pd(arvif, &info->he_obss_pd);
4964 		else
4965 			ath12k_wmi_send_obss_spr_cmd(ar, arvif->vdev_id,
4966 						     &info->he_obss_pd);
4967 	}
4968 
4969 	if (changed & BSS_CHANGED_HE_BSS_COLOR) {
4970 		if (vif->type == NL80211_IFTYPE_AP) {
4971 			ret = ath12k_wmi_obss_color_cfg_cmd(ar,
4972 							    arvif->vdev_id,
4973 							    info->he_bss_color.color,
4974 							    ATH12K_BSS_COLOR_AP_PERIODS,
4975 							    info->he_bss_color.enabled);
4976 			if (ret)
4977 				ath12k_warn(ar->ab, "failed to set bss color collision on vdev %u: %d\n",
4978 					    arvif->vdev_id,  ret);
4979 
4980 			param_id = WMI_VDEV_PARAM_BSS_COLOR;
4981 			if (info->he_bss_color.enabled)
4982 				param_value = info->he_bss_color.color <<
4983 					      IEEE80211_HE_OPERATION_BSS_COLOR_OFFSET;
4984 			else
4985 				param_value = IEEE80211_HE_OPERATION_BSS_COLOR_DISABLED;
4986 
4987 			ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
4988 							    param_id,
4989 							    param_value);
4990 			if (ret)
4991 				ath12k_warn(ar->ab, "failed to set bss color param on vdev %u: %d\n",
4992 					    arvif->vdev_id,  ret);
4993 			else
4994 				ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "bss color param 0x%x set on vdev %u\n",
4995 					   param_value, arvif->vdev_id);
4996 		} else if (vif->type == NL80211_IFTYPE_STATION) {
4997 			ret = ath12k_wmi_send_bss_color_change_enable_cmd(ar,
4998 									  arvif->vdev_id,
4999 									  1);
5000 			if (ret)
5001 				ath12k_warn(ar->ab, "failed to enable bss color change on vdev %i: %d\n",
5002 					    arvif->vdev_id,  ret);
5003 			ret = ath12k_wmi_obss_color_cfg_cmd(ar,
5004 							    arvif->vdev_id,
5005 							    0,
5006 							    ATH12K_BSS_COLOR_STA_PERIODS,
5007 							    1);
5008 			if (ret)
5009 				ath12k_warn(ar->ab, "failed to set bss color collision on vdev %i: %d\n",
5010 					    arvif->vdev_id,  ret);
5011 		}
5012 	}
5013 
5014 	ath12k_mac_fils_discovery(arvif, info);
5015 
5016 	if (changed & BSS_CHANGED_PS &&
5017 	    ar->ab->hw_params->supports_sta_ps) {
5018 		ahvif->ps = vif_cfg->ps;
5019 		ath12k_mac_vif_setup_ps(arvif);
5020 	}
5021 }
5022 
5023 static struct ath12k_vif_cache *ath12k_ahvif_get_link_cache(struct ath12k_vif *ahvif,
5024 							    u8 link_id)
5025 {
5026 	if (!ahvif->cache[link_id]) {
5027 		ahvif->cache[link_id] = kzalloc_obj(*ahvif->cache[0],
5028 						    GFP_KERNEL);
5029 		if (ahvif->cache[link_id])
5030 			INIT_LIST_HEAD(&ahvif->cache[link_id]->key_conf.list);
5031 	}
5032 
5033 	return ahvif->cache[link_id];
5034 }
5035 
5036 static void ath12k_ahvif_put_link_key_cache(struct ath12k_vif_cache *cache)
5037 {
5038 	struct ath12k_key_conf *key_conf, *tmp;
5039 
5040 	if (!cache || list_empty(&cache->key_conf.list))
5041 		return;
5042 	list_for_each_entry_safe(key_conf, tmp, &cache->key_conf.list, list) {
5043 		list_del(&key_conf->list);
5044 		kfree(key_conf);
5045 	}
5046 }
5047 
5048 static void ath12k_ahvif_put_link_cache(struct ath12k_vif *ahvif, u8 link_id)
5049 {
5050 	if (link_id >= IEEE80211_MLD_MAX_NUM_LINKS)
5051 		return;
5052 
5053 	ath12k_ahvif_put_link_key_cache(ahvif->cache[link_id]);
5054 	kfree(ahvif->cache[link_id]);
5055 	ahvif->cache[link_id] = NULL;
5056 }
5057 
5058 void ath12k_mac_op_link_info_changed(struct ieee80211_hw *hw,
5059 				     struct ieee80211_vif *vif,
5060 				     struct ieee80211_bss_conf *info,
5061 				     u64 changed)
5062 {
5063 	struct ath12k *ar;
5064 	struct ath12k_vif *ahvif = ath12k_vif_to_ahvif(vif);
5065 	struct ath12k_vif_cache *cache;
5066 	struct ath12k_link_vif *arvif;
5067 	u8 link_id = info->link_id;
5068 
5069 	lockdep_assert_wiphy(hw->wiphy);
5070 
5071 	arvif = wiphy_dereference(hw->wiphy, ahvif->link[link_id]);
5072 
5073 	/* if the vdev is not created on a certain radio,
5074 	 * cache the info to be updated later on vdev creation
5075 	 */
5076 
5077 	if (!arvif || !arvif->is_created) {
5078 		cache = ath12k_ahvif_get_link_cache(ahvif, link_id);
5079 		if (!cache)
5080 			return;
5081 
5082 		cache->bss_conf_changed |= changed;
5083 
5084 		return;
5085 	}
5086 
5087 	ar = arvif->ar;
5088 
5089 	ath12k_mac_bss_info_changed(ar, arvif, info, changed);
5090 }
5091 EXPORT_SYMBOL(ath12k_mac_op_link_info_changed);
5092 
5093 static struct ath12k*
5094 ath12k_mac_select_scan_device(struct ieee80211_hw *hw,
5095 			      struct ieee80211_vif *vif,
5096 			      u32 center_freq)
5097 {
5098 	struct ath12k_hw *ah = hw->priv;
5099 	enum nl80211_band band;
5100 	struct ath12k *ar;
5101 	int i;
5102 
5103 	if (ah->num_radio == 1)
5104 		return ah->radio;
5105 
5106 	/* Currently mac80211 supports splitting scan requests into
5107 	 * multiple scan requests per band.
5108 	 * Loop through first channel and determine the scan radio
5109 	 * TODO: There could be 5 GHz low/high channels in that case
5110 	 * split the hw request and perform multiple scans
5111 	 */
5112 
5113 	if (center_freq < ATH12K_MIN_5GHZ_FREQ)
5114 		band = NL80211_BAND_2GHZ;
5115 	else if (center_freq < ATH12K_MIN_6GHZ_FREQ)
5116 		band = NL80211_BAND_5GHZ;
5117 	else
5118 		band = NL80211_BAND_6GHZ;
5119 
5120 	for_each_ar(ah, ar, i) {
5121 		if (ar->mac.sbands[band].channels &&
5122 		    center_freq >= KHZ_TO_MHZ(ar->freq_range.start_freq) &&
5123 		    center_freq <= KHZ_TO_MHZ(ar->freq_range.end_freq))
5124 			return ar;
5125 	}
5126 
5127 	return NULL;
5128 }
5129 
5130 void __ath12k_mac_scan_finish(struct ath12k *ar)
5131 {
5132 	struct ieee80211_hw *hw = ath12k_ar_to_hw(ar);
5133 
5134 	lockdep_assert_held(&ar->data_lock);
5135 
5136 	switch (ar->scan.state) {
5137 	case ATH12K_SCAN_IDLE:
5138 		break;
5139 	case ATH12K_SCAN_RUNNING:
5140 	case ATH12K_SCAN_ABORTING:
5141 		if (ar->scan.is_roc && ar->scan.roc_notify)
5142 			ieee80211_remain_on_channel_expired(hw);
5143 		fallthrough;
5144 	case ATH12K_SCAN_STARTING:
5145 		cancel_delayed_work(&ar->scan.timeout);
5146 		complete_all(&ar->scan.completed);
5147 		wiphy_work_queue(ar->ah->hw->wiphy, &ar->scan.vdev_clean_wk);
5148 		break;
5149 	}
5150 }
5151 
5152 void ath12k_mac_scan_finish(struct ath12k *ar)
5153 {
5154 	spin_lock_bh(&ar->data_lock);
5155 	__ath12k_mac_scan_finish(ar);
5156 	spin_unlock_bh(&ar->data_lock);
5157 }
5158 
5159 static int ath12k_scan_stop(struct ath12k *ar)
5160 {
5161 	struct ath12k_wmi_scan_cancel_arg arg = {
5162 		.req_type = WLAN_SCAN_CANCEL_SINGLE,
5163 		.scan_id = ATH12K_SCAN_ID,
5164 	};
5165 	int ret;
5166 
5167 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
5168 
5169 	/* TODO: Fill other STOP Params */
5170 	arg.pdev_id = ar->pdev->pdev_id;
5171 
5172 	ret = ath12k_wmi_send_scan_stop_cmd(ar, &arg);
5173 	if (ret) {
5174 		ath12k_warn(ar->ab, "failed to stop wmi scan: %d\n", ret);
5175 		goto out;
5176 	}
5177 
5178 	ret = wait_for_completion_timeout(&ar->scan.completed, 3 * HZ);
5179 	if (ret == 0) {
5180 		ath12k_warn(ar->ab,
5181 			    "failed to receive scan abort comple: timed out\n");
5182 		ret = -ETIMEDOUT;
5183 	} else if (ret > 0) {
5184 		ret = 0;
5185 	}
5186 
5187 out:
5188 	/* Scan state should be updated in scan completion worker but in
5189 	 * case firmware fails to deliver the event (for whatever reason)
5190 	 * it is desired to clean up scan state anyway. Firmware may have
5191 	 * just dropped the scan completion event delivery due to transport
5192 	 * pipe being overflown with data and/or it can recover on its own
5193 	 * before next scan request is submitted.
5194 	 */
5195 	spin_lock_bh(&ar->data_lock);
5196 	if (ret)
5197 		__ath12k_mac_scan_finish(ar);
5198 	spin_unlock_bh(&ar->data_lock);
5199 
5200 	return ret;
5201 }
5202 
5203 static void ath12k_scan_abort(struct ath12k *ar)
5204 {
5205 	int ret;
5206 
5207 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
5208 
5209 	spin_lock_bh(&ar->data_lock);
5210 
5211 	switch (ar->scan.state) {
5212 	case ATH12K_SCAN_IDLE:
5213 		/* This can happen if timeout worker kicked in and called
5214 		 * abortion while scan completion was being processed.
5215 		 */
5216 		break;
5217 	case ATH12K_SCAN_STARTING:
5218 	case ATH12K_SCAN_ABORTING:
5219 		ath12k_warn(ar->ab, "refusing scan abortion due to invalid scan state: %d\n",
5220 			    ar->scan.state);
5221 		break;
5222 	case ATH12K_SCAN_RUNNING:
5223 		ar->scan.state = ATH12K_SCAN_ABORTING;
5224 		spin_unlock_bh(&ar->data_lock);
5225 
5226 		ret = ath12k_scan_stop(ar);
5227 		if (ret)
5228 			ath12k_warn(ar->ab, "failed to abort scan: %d\n", ret);
5229 
5230 		spin_lock_bh(&ar->data_lock);
5231 		break;
5232 	}
5233 
5234 	spin_unlock_bh(&ar->data_lock);
5235 }
5236 
5237 static void ath12k_scan_timeout_work(struct work_struct *work)
5238 {
5239 	struct ath12k *ar = container_of(work, struct ath12k,
5240 					 scan.timeout.work);
5241 
5242 	wiphy_lock(ath12k_ar_to_hw(ar)->wiphy);
5243 	ath12k_scan_abort(ar);
5244 	wiphy_unlock(ath12k_ar_to_hw(ar)->wiphy);
5245 }
5246 
5247 static void ath12k_mac_scan_send_complete(struct ath12k *ar,
5248 					  struct cfg80211_scan_info *info)
5249 {
5250 	struct ath12k_hw *ah = ar->ah;
5251 	struct ath12k *partner_ar;
5252 	int i;
5253 
5254 	lockdep_assert_wiphy(ah->hw->wiphy);
5255 
5256 	for_each_ar(ah, partner_ar, i)
5257 		if (partner_ar != ar &&
5258 		    partner_ar->scan.state == ATH12K_SCAN_RUNNING)
5259 			return;
5260 
5261 	ieee80211_scan_completed(ah->hw, info);
5262 }
5263 
5264 static void ath12k_scan_vdev_clean_work(struct wiphy *wiphy, struct wiphy_work *work)
5265 {
5266 	struct ath12k *ar = container_of(work, struct ath12k,
5267 					 scan.vdev_clean_wk);
5268 	struct ath12k_hw *ah = ar->ah;
5269 	struct ath12k_link_vif *arvif;
5270 
5271 	lockdep_assert_wiphy(wiphy);
5272 
5273 	arvif = ar->scan.arvif;
5274 
5275 	/* The scan vdev has already been deleted. This can occur when a
5276 	 * new scan request is made on the same vif with a different
5277 	 * frequency, causing the scan arvif to move from one radio to
5278 	 * another. Or, scan was abrupted and via remove interface, the
5279 	 * arvif is already deleted. Alternatively, if the scan vdev is not
5280 	 * being used as an actual vdev, then do not delete it.
5281 	 */
5282 	if (!arvif || arvif->is_started)
5283 		goto work_complete;
5284 
5285 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac clean scan vdev (link id %u)",
5286 		   arvif->link_id);
5287 
5288 	ath12k_mac_remove_link_interface(ah->hw, arvif);
5289 	ath12k_mac_unassign_link_vif(arvif);
5290 
5291 work_complete:
5292 	spin_lock_bh(&ar->data_lock);
5293 	ar->scan.arvif = NULL;
5294 	if (!ar->scan.is_roc) {
5295 		struct cfg80211_scan_info info = {
5296 			.aborted = ((ar->scan.state ==
5297 				    ATH12K_SCAN_ABORTING) ||
5298 				    (ar->scan.state ==
5299 				    ATH12K_SCAN_STARTING)),
5300 		};
5301 
5302 		ath12k_mac_scan_send_complete(ar, &info);
5303 	}
5304 
5305 	ar->scan.state = ATH12K_SCAN_IDLE;
5306 	ar->scan_channel = NULL;
5307 	ar->scan.roc_freq = 0;
5308 	spin_unlock_bh(&ar->data_lock);
5309 }
5310 
5311 static int ath12k_start_scan(struct ath12k *ar,
5312 			     struct ath12k_wmi_scan_req_arg *arg)
5313 {
5314 	int ret;
5315 
5316 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
5317 
5318 	ret = ath12k_wmi_send_scan_start_cmd(ar, arg);
5319 	if (ret)
5320 		return ret;
5321 
5322 	ret = wait_for_completion_timeout(&ar->scan.started, 1 * HZ);
5323 	if (ret == 0) {
5324 		ret = ath12k_scan_stop(ar);
5325 		if (ret)
5326 			ath12k_warn(ar->ab, "failed to stop scan: %d\n", ret);
5327 
5328 		return -ETIMEDOUT;
5329 	}
5330 
5331 	/* If we failed to start the scan, return error code at
5332 	 * this point.  This is probably due to some issue in the
5333 	 * firmware, but no need to wedge the driver due to that...
5334 	 */
5335 	spin_lock_bh(&ar->data_lock);
5336 	if (ar->scan.state == ATH12K_SCAN_IDLE) {
5337 		spin_unlock_bh(&ar->data_lock);
5338 		return -EINVAL;
5339 	}
5340 	spin_unlock_bh(&ar->data_lock);
5341 
5342 	return 0;
5343 }
5344 
5345 int ath12k_mac_get_fw_stats(struct ath12k *ar,
5346 			    struct ath12k_fw_stats_req_params *param)
5347 {
5348 	struct ath12k_base *ab = ar->ab;
5349 	struct ath12k_hw *ah = ath12k_ar_to_ah(ar);
5350 	unsigned long time_left;
5351 	int ret;
5352 
5353 	guard(mutex)(&ah->hw_mutex);
5354 
5355 	if (ah->state != ATH12K_HW_STATE_ON)
5356 		return -ENETDOWN;
5357 
5358 	reinit_completion(&ar->fw_stats_complete);
5359 	reinit_completion(&ar->fw_stats_done);
5360 
5361 	ret = ath12k_wmi_send_stats_request_cmd(ar, param->stats_id,
5362 						param->vdev_id, param->pdev_id);
5363 	if (ret) {
5364 		ath12k_warn(ab, "failed to request fw stats: %d\n", ret);
5365 		return ret;
5366 	}
5367 
5368 	ath12k_dbg(ab, ATH12K_DBG_WMI,
5369 		   "get fw stat pdev id %d vdev id %d stats id 0x%x\n",
5370 		   param->pdev_id, param->vdev_id, param->stats_id);
5371 
5372 	time_left = wait_for_completion_timeout(&ar->fw_stats_complete, 1 * HZ);
5373 	if (!time_left) {
5374 		ath12k_warn(ab, "time out while waiting for get fw stats\n");
5375 		return -ETIMEDOUT;
5376 	}
5377 
5378 	/* Firmware sends WMI_UPDATE_STATS_EVENTID back-to-back
5379 	 * when stats data buffer limit is reached. fw_stats_complete
5380 	 * is completed once host receives first event from firmware, but
5381 	 * still there could be more events following. Below is to wait
5382 	 * until firmware completes sending all the events.
5383 	 */
5384 	time_left = wait_for_completion_timeout(&ar->fw_stats_done, 3 * HZ);
5385 	if (!time_left) {
5386 		ath12k_warn(ab, "time out while waiting for fw stats done\n");
5387 		return -ETIMEDOUT;
5388 	}
5389 
5390 	return 0;
5391 }
5392 
5393 int ath12k_mac_op_get_txpower(struct ieee80211_hw *hw,
5394 			      struct ieee80211_vif *vif,
5395 			      unsigned int link_id,
5396 			      int *dbm)
5397 {
5398 	struct ath12k_vif *ahvif = ath12k_vif_to_ahvif(vif);
5399 	struct ath12k_fw_stats_req_params params = {};
5400 	struct ath12k_fw_stats_pdev *pdev;
5401 	struct ath12k_hw *ah = hw->priv;
5402 	struct ath12k_link_vif *arvif;
5403 	struct ath12k_base *ab;
5404 	struct ath12k *ar;
5405 	int ret;
5406 
5407 	/* Final Tx power is minimum of Target Power, CTL power, Regulatory
5408 	 * Power, PSD EIRP Power. We just know the Regulatory power from the
5409 	 * regulatory rules obtained. FW knows all these power and sets the min
5410 	 * of these. Hence, we request the FW pdev stats in which FW reports
5411 	 * the minimum of all vdev's channel Tx power.
5412 	 */
5413 	lockdep_assert_wiphy(hw->wiphy);
5414 
5415 	arvif = wiphy_dereference(ah->hw->wiphy, ahvif->link[link_id]);
5416 	if (!arvif || !arvif->ar)
5417 		return -EINVAL;
5418 
5419 	ar = arvif->ar;
5420 	ab = ar->ab;
5421 	if (ah->state != ATH12K_HW_STATE_ON)
5422 		goto err_fallback;
5423 
5424 	if (test_bit(ATH12K_FLAG_CAC_RUNNING, &ar->dev_flags))
5425 		return -EAGAIN;
5426 
5427 	/* Limit the requests to Firmware for fetching the tx power */
5428 	if (ar->chan_tx_pwr != ATH12K_PDEV_TX_POWER_INVALID &&
5429 	    time_before(jiffies,
5430 			msecs_to_jiffies(ATH12K_PDEV_TX_POWER_REFRESH_TIME_MSECS) +
5431 					 ar->last_tx_power_update))
5432 		goto send_tx_power;
5433 
5434 	params.pdev_id = ar->pdev->pdev_id;
5435 	params.vdev_id = arvif->vdev_id;
5436 	params.stats_id = WMI_REQUEST_PDEV_STAT;
5437 	ret = ath12k_mac_get_fw_stats(ar, &params);
5438 	if (ret) {
5439 		ath12k_warn(ab, "failed to request fw pdev stats: %d\n", ret);
5440 		goto err_fallback;
5441 	}
5442 
5443 	spin_lock_bh(&ar->data_lock);
5444 	pdev = list_first_entry_or_null(&ar->fw_stats.pdevs,
5445 					struct ath12k_fw_stats_pdev, list);
5446 	if (!pdev) {
5447 		spin_unlock_bh(&ar->data_lock);
5448 		goto err_fallback;
5449 	}
5450 
5451 	/* tx power reported by firmware is in units of 0.5 dBm */
5452 	ar->chan_tx_pwr = pdev->chan_tx_power / 2;
5453 	spin_unlock_bh(&ar->data_lock);
5454 	ar->last_tx_power_update = jiffies;
5455 	ath12k_fw_stats_reset(ar);
5456 
5457 send_tx_power:
5458 	*dbm = ar->chan_tx_pwr;
5459 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "txpower fetched from firmware %d dBm\n",
5460 		   *dbm);
5461 	return 0;
5462 
5463 err_fallback:
5464 	/* We didn't get txpower from FW. Hence, relying on vif->bss_conf.txpower */
5465 	*dbm = vif->bss_conf.txpower;
5466 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "txpower from firmware NaN, reported %d dBm\n",
5467 		   *dbm);
5468 	return 0;
5469 }
5470 EXPORT_SYMBOL(ath12k_mac_op_get_txpower);
5471 
5472 static u8
5473 ath12k_mac_find_link_id_by_ar(struct ath12k_vif *ahvif, struct ath12k *ar)
5474 {
5475 	struct ath12k_link_vif *arvif;
5476 	struct ath12k_hw *ah = ahvif->ah;
5477 	unsigned long links = ahvif->links_map;
5478 	unsigned long scan_links_map;
5479 	u8 link_id;
5480 
5481 	lockdep_assert_wiphy(ah->hw->wiphy);
5482 
5483 	for_each_set_bit(link_id, &links, ATH12K_NUM_MAX_LINKS) {
5484 		arvif = wiphy_dereference(ah->hw->wiphy, ahvif->link[link_id]);
5485 
5486 		if (!arvif || !arvif->is_created)
5487 			continue;
5488 
5489 		if (ar == arvif->ar)
5490 			return link_id;
5491 	}
5492 
5493 	/* input ar is not assigned to any of the links of ML VIF, use next
5494 	 * available scan link for scan vdev creation. There are cases where
5495 	 * single scan req needs to be split in driver and initiate separate
5496 	 * scan requests to firmware based on device.
5497 	 */
5498 
5499 	 /* Unset all non-scan links (0-14) of scan_links_map so that ffs() will
5500 	  * choose an available link among scan links (i.e link id >= 15)
5501 	  */
5502 	scan_links_map = ~ahvif->links_map & ATH12K_SCAN_LINKS_MASK;
5503 	if (scan_links_map)
5504 		return __ffs(scan_links_map);
5505 
5506 	return ATH12K_FIRST_SCAN_LINK;
5507 }
5508 
5509 static int ath12k_mac_initiate_hw_scan(struct ieee80211_hw *hw,
5510 				       struct ieee80211_vif *vif,
5511 				       struct ieee80211_scan_request *hw_req,
5512 				       int n_channels,
5513 				       struct ieee80211_channel **chan_list,
5514 				       struct ath12k *ar)
5515 {
5516 	struct ath12k_hw *ah = ath12k_hw_to_ah(hw);
5517 	struct ath12k_vif *ahvif = ath12k_vif_to_ahvif(vif);
5518 	struct ath12k_link_vif *arvif;
5519 	struct cfg80211_scan_request *req = &hw_req->req;
5520 	struct ath12k_wmi_scan_req_arg *arg = NULL;
5521 	u8 link_id;
5522 	int ret;
5523 	int i;
5524 	bool create = true;
5525 
5526 	lockdep_assert_wiphy(hw->wiphy);
5527 
5528 	arvif = &ahvif->deflink;
5529 
5530 	/* check if any of the links of ML VIF is already started on
5531 	 * radio(ar) corresponding to given scan frequency and use it,
5532 	 * if not use scan link (link id >= 15) for scan purpose.
5533 	 */
5534 	link_id = ath12k_mac_find_link_id_by_ar(ahvif, ar);
5535 	/* All scan links are occupied. ideally this shouldn't happen as
5536 	 * mac80211 won't schedule scan for same band until ongoing scan is
5537 	 * completed, don't try to exceed max links just in case if it happens.
5538 	 */
5539 	if (link_id >= ATH12K_NUM_MAX_LINKS)
5540 		return -EBUSY;
5541 
5542 	arvif = ath12k_mac_assign_link_vif(ah, vif, link_id);
5543 
5544 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac link ID %d selected for scan",
5545 		   arvif->link_id);
5546 
5547 	/* If the vif is already assigned to a specific vdev of an ar,
5548 	 * check whether its already started, vdev which is started
5549 	 * are not allowed to switch to a new radio.
5550 	 * If the vdev is not started, but was earlier created on a
5551 	 * different ar, delete that vdev and create a new one. We don't
5552 	 * delete at the scan stop as an optimization to avoid redundant
5553 	 * delete-create vdev's for the same ar, in case the request is
5554 	 * always on the same band for the vif
5555 	 */
5556 	if (arvif->is_created) {
5557 		if (WARN_ON(!arvif->ar))
5558 			return -EINVAL;
5559 
5560 		if (ar != arvif->ar && arvif->is_started)
5561 			return -EINVAL;
5562 
5563 		if (ar != arvif->ar) {
5564 			ath12k_mac_remove_link_interface(hw, arvif);
5565 			ath12k_mac_unassign_link_vif(arvif);
5566 		} else {
5567 			create = false;
5568 		}
5569 	}
5570 
5571 	if (create) {
5572 		/* Previous arvif would've been cleared in radio switch block
5573 		 * above, assign arvif again for create.
5574 		 */
5575 		arvif = ath12k_mac_assign_link_vif(ah, vif, link_id);
5576 
5577 		ret = ath12k_mac_vdev_create(ar, arvif);
5578 		if (ret) {
5579 			ath12k_warn(ar->ab, "unable to create scan vdev %d\n", ret);
5580 			ath12k_mac_unassign_link_vif(arvif);
5581 			return ret;
5582 		}
5583 	}
5584 
5585 	spin_lock_bh(&ar->data_lock);
5586 	switch (ar->scan.state) {
5587 	case ATH12K_SCAN_IDLE:
5588 		reinit_completion(&ar->scan.started);
5589 		reinit_completion(&ar->scan.completed);
5590 		ar->scan.state = ATH12K_SCAN_STARTING;
5591 		ar->scan.is_roc = false;
5592 		ar->scan.arvif = arvif;
5593 		ret = 0;
5594 		break;
5595 	case ATH12K_SCAN_STARTING:
5596 	case ATH12K_SCAN_RUNNING:
5597 	case ATH12K_SCAN_ABORTING:
5598 		ret = -EBUSY;
5599 		break;
5600 	}
5601 	spin_unlock_bh(&ar->data_lock);
5602 
5603 	if (ret)
5604 		goto exit;
5605 
5606 	arg = kzalloc_obj(*arg);
5607 	if (!arg) {
5608 		ret = -ENOMEM;
5609 		goto exit;
5610 	}
5611 
5612 	ath12k_wmi_start_scan_init(ar, arg);
5613 	arg->vdev_id = arvif->vdev_id;
5614 	arg->scan_id = ATH12K_SCAN_ID;
5615 
5616 	if (req->ie_len) {
5617 		arg->extraie.ptr = kmemdup(req->ie, req->ie_len, GFP_KERNEL);
5618 		if (!arg->extraie.ptr) {
5619 			ret = -ENOMEM;
5620 			goto exit;
5621 		}
5622 		arg->extraie.len = req->ie_len;
5623 	}
5624 
5625 	if (req->n_ssids) {
5626 		arg->num_ssids = req->n_ssids;
5627 		for (i = 0; i < arg->num_ssids; i++)
5628 			arg->ssid[i] = req->ssids[i];
5629 	} else {
5630 		arg->scan_f_passive = 1;
5631 	}
5632 
5633 	if (n_channels) {
5634 		arg->num_chan = n_channels;
5635 		arg->chan_list = kcalloc(arg->num_chan, sizeof(*arg->chan_list),
5636 					 GFP_KERNEL);
5637 		if (!arg->chan_list) {
5638 			ret = -ENOMEM;
5639 			goto exit;
5640 		}
5641 
5642 		for (i = 0; i < arg->num_chan; i++)
5643 			arg->chan_list[i] = chan_list[i]->center_freq;
5644 	}
5645 
5646 	ret = ath12k_start_scan(ar, arg);
5647 	if (ret) {
5648 		if (ret == -EBUSY)
5649 			ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
5650 				   "scan engine is busy 11d state %d\n", ar->state_11d);
5651 		else
5652 			ath12k_warn(ar->ab, "failed to start hw scan: %d\n", ret);
5653 
5654 		spin_lock_bh(&ar->data_lock);
5655 		ar->scan.state = ATH12K_SCAN_IDLE;
5656 		spin_unlock_bh(&ar->data_lock);
5657 		goto exit;
5658 	}
5659 
5660 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac scan started");
5661 
5662 	/* Add a margin to account for event/command processing */
5663 	ieee80211_queue_delayed_work(ath12k_ar_to_hw(ar), &ar->scan.timeout,
5664 				     msecs_to_jiffies(arg->max_scan_time +
5665 						      ATH12K_MAC_SCAN_TIMEOUT_MSECS));
5666 
5667 exit:
5668 	if (arg) {
5669 		kfree(arg->chan_list);
5670 		kfree(arg->extraie.ptr);
5671 		kfree(arg);
5672 	}
5673 
5674 	if (ar->state_11d == ATH12K_11D_PREPARING &&
5675 	    ahvif->vdev_type == WMI_VDEV_TYPE_STA &&
5676 	    ahvif->vdev_subtype == WMI_VDEV_SUBTYPE_NONE)
5677 		ath12k_mac_11d_scan_start(ar, arvif->vdev_id);
5678 
5679 	return ret;
5680 }
5681 
5682 int ath12k_mac_op_hw_scan(struct ieee80211_hw *hw,
5683 			  struct ieee80211_vif *vif,
5684 			  struct ieee80211_scan_request *hw_req)
5685 {
5686 	struct ath12k_vif *ahvif = ath12k_vif_to_ahvif(vif);
5687 	struct ieee80211_channel **chan_list, *chan;
5688 	struct ath12k_hw *ah = ath12k_hw_to_ah(hw);
5689 	unsigned long links_map, link_id;
5690 	struct ath12k_link_vif *arvif;
5691 	struct ath12k *ar, *scan_ar;
5692 	int i, j, ret = 0;
5693 
5694 	lockdep_assert_wiphy(hw->wiphy);
5695 
5696 	chan_list = kzalloc_objs(*chan_list, hw_req->req.n_channels);
5697 	if (!chan_list)
5698 		return -ENOMEM;
5699 
5700 	/* There could be channels that belong to multiple underlying radio
5701 	 * in same scan request as mac80211 sees it as single band. In that
5702 	 * case split the hw_req based on frequency range and schedule scans to
5703 	 * corresponding radio.
5704 	 */
5705 	for_each_ar(ah, ar, i) {
5706 		int n_chans = 0;
5707 
5708 		for (j = 0; j < hw_req->req.n_channels; j++) {
5709 			chan = hw_req->req.channels[j];
5710 			scan_ar = ath12k_mac_select_scan_device(hw, vif,
5711 								chan->center_freq);
5712 			if (!scan_ar) {
5713 				ath12k_hw_warn(ah, "unable to select scan device for freq %d\n",
5714 					       chan->center_freq);
5715 				ret = -EINVAL;
5716 				goto abort;
5717 			}
5718 			if (ar != scan_ar)
5719 				continue;
5720 
5721 			chan_list[n_chans++] = chan;
5722 		}
5723 		if (n_chans) {
5724 			ret = ath12k_mac_initiate_hw_scan(hw, vif, hw_req, n_chans,
5725 							  chan_list, ar);
5726 			if (ret)
5727 				goto abort;
5728 		}
5729 	}
5730 abort:
5731 	/* If any of the parallel scans initiated fails, abort all and
5732 	 * remove the scan interfaces created. Return complete scan
5733 	 * failure as mac80211 assumes this as single scan request.
5734 	 */
5735 	if (ret) {
5736 		ath12k_hw_warn(ah, "Scan failed %d , cleanup all scan vdevs\n", ret);
5737 		links_map = ahvif->links_map;
5738 		for_each_set_bit(link_id, &links_map, ATH12K_NUM_MAX_LINKS) {
5739 			arvif = wiphy_dereference(hw->wiphy, ahvif->link[link_id]);
5740 			if (!arvif)
5741 				continue;
5742 
5743 			ar = arvif->ar;
5744 			if (ar->scan.arvif == arvif) {
5745 				wiphy_work_cancel(hw->wiphy, &ar->scan.vdev_clean_wk);
5746 				spin_lock_bh(&ar->data_lock);
5747 				ar->scan.arvif = NULL;
5748 				ar->scan.state = ATH12K_SCAN_IDLE;
5749 				ar->scan_channel = NULL;
5750 				ar->scan.roc_freq = 0;
5751 				spin_unlock_bh(&ar->data_lock);
5752 			}
5753 			if (link_id >= ATH12K_FIRST_SCAN_LINK) {
5754 				ath12k_mac_remove_link_interface(hw, arvif);
5755 				ath12k_mac_unassign_link_vif(arvif);
5756 			}
5757 		}
5758 	}
5759 	kfree(chan_list);
5760 	return ret;
5761 }
5762 EXPORT_SYMBOL(ath12k_mac_op_hw_scan);
5763 
5764 void ath12k_mac_op_cancel_hw_scan(struct ieee80211_hw *hw,
5765 				  struct ieee80211_vif *vif)
5766 {
5767 	struct ath12k_vif *ahvif = ath12k_vif_to_ahvif(vif);
5768 	unsigned long link_id, links_map = ahvif->links_map;
5769 	struct ath12k_link_vif *arvif;
5770 	struct ath12k *ar;
5771 
5772 	lockdep_assert_wiphy(hw->wiphy);
5773 
5774 	for_each_set_bit(link_id, &links_map, ATH12K_NUM_MAX_LINKS) {
5775 		arvif = wiphy_dereference(hw->wiphy, ahvif->link[link_id]);
5776 		if (!arvif || !arvif->is_created ||
5777 		    arvif->ar->scan.arvif != arvif)
5778 			continue;
5779 
5780 		ar = arvif->ar;
5781 
5782 		ath12k_scan_abort(ar);
5783 
5784 		cancel_delayed_work_sync(&ar->scan.timeout);
5785 	}
5786 }
5787 EXPORT_SYMBOL(ath12k_mac_op_cancel_hw_scan);
5788 
5789 static int ath12k_install_key(struct ath12k_link_vif *arvif,
5790 			      struct ieee80211_key_conf *key,
5791 			      enum set_key_cmd cmd,
5792 			      const u8 *macaddr, u32 flags)
5793 {
5794 	int ret;
5795 	struct ath12k *ar = arvif->ar;
5796 	struct wmi_vdev_install_key_arg arg = {
5797 		.vdev_id = arvif->vdev_id,
5798 		.key_idx = key->keyidx,
5799 		.key_len = key->keylen,
5800 		.key_data = key->key,
5801 		.key_flags = flags,
5802 		.ieee80211_key_cipher = key->cipher,
5803 		.macaddr = macaddr,
5804 	};
5805 	struct ath12k_vif *ahvif = arvif->ahvif;
5806 
5807 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
5808 
5809 	if (test_bit(ATH12K_FLAG_HW_CRYPTO_DISABLED, &ar->ab->dev_flags))
5810 		return 0;
5811 
5812 	if (cmd == DISABLE_KEY) {
5813 		/* TODO: Check if FW expects  value other than NONE for del */
5814 		/* arg.key_cipher = WMI_CIPHER_NONE; */
5815 		arg.key_len = 0;
5816 		arg.key_data = NULL;
5817 		goto check_order;
5818 	}
5819 
5820 	switch (key->cipher) {
5821 	case WLAN_CIPHER_SUITE_CCMP:
5822 	case WLAN_CIPHER_SUITE_CCMP_256:
5823 		arg.key_cipher = WMI_CIPHER_AES_CCM;
5824 		key->flags |= IEEE80211_KEY_FLAG_GENERATE_IV_MGMT;
5825 		break;
5826 	case WLAN_CIPHER_SUITE_TKIP:
5827 		arg.key_cipher = WMI_CIPHER_TKIP;
5828 		arg.key_txmic_len = 8;
5829 		arg.key_rxmic_len = 8;
5830 		break;
5831 	case WLAN_CIPHER_SUITE_GCMP:
5832 	case WLAN_CIPHER_SUITE_GCMP_256:
5833 		arg.key_cipher = WMI_CIPHER_AES_GCM;
5834 		key->flags |= IEEE80211_KEY_FLAG_GENERATE_IV_MGMT;
5835 		break;
5836 	case WLAN_CIPHER_SUITE_AES_CMAC:
5837 		arg.key_cipher = WMI_CIPHER_AES_CMAC;
5838 		break;
5839 	case WLAN_CIPHER_SUITE_BIP_GMAC_128:
5840 	case WLAN_CIPHER_SUITE_BIP_GMAC_256:
5841 		arg.key_cipher = WMI_CIPHER_AES_GMAC;
5842 		break;
5843 	case WLAN_CIPHER_SUITE_BIP_CMAC_256:
5844 		arg.key_cipher = WMI_CIPHER_AES_CMAC;
5845 		break;
5846 	default:
5847 		ath12k_warn(ar->ab, "cipher %d is not supported\n", key->cipher);
5848 		return -EOPNOTSUPP;
5849 	}
5850 
5851 	if (test_bit(ATH12K_FLAG_RAW_MODE, &ar->ab->dev_flags))
5852 		key->flags |= IEEE80211_KEY_FLAG_GENERATE_IV |
5853 			      IEEE80211_KEY_FLAG_RESERVE_TAILROOM;
5854 
5855 check_order:
5856 	if (ahvif->vdev_type == WMI_VDEV_TYPE_STA &&
5857 	    arg.key_flags == WMI_KEY_GROUP) {
5858 		if (cmd == SET_KEY) {
5859 			if (arvif->pairwise_key_done) {
5860 				ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
5861 					   "vdev %u pairwise key done, go install group key\n",
5862 					   arg.vdev_id);
5863 				goto install;
5864 			} else {
5865 				/* WCN7850 firmware requires pairwise key to be installed
5866 				 * before group key. In case group key comes first, cache
5867 				 * it and return. Will revisit it once pairwise key gets
5868 				 * installed.
5869 				 */
5870 				arvif->group_key = arg;
5871 				arvif->group_key_valid = true;
5872 				ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
5873 					   "vdev %u group key before pairwise key, cache and skip\n",
5874 					   arg.vdev_id);
5875 
5876 				ret = 0;
5877 				goto out;
5878 			}
5879 		} else {
5880 			arvif->group_key_valid = false;
5881 		}
5882 	}
5883 
5884 install:
5885 	reinit_completion(&ar->install_key_done);
5886 
5887 	ret = ath12k_wmi_vdev_install_key(arvif->ar, &arg);
5888 	if (ret)
5889 		return ret;
5890 
5891 	if (!wait_for_completion_timeout(&ar->install_key_done, 1 * HZ))
5892 		return -ETIMEDOUT;
5893 
5894 	if (ether_addr_equal(arg.macaddr, arvif->bssid))
5895 		ahvif->dp_vif.key_cipher = arg.ieee80211_key_cipher;
5896 
5897 	if (ar->install_key_status) {
5898 		ret = -EINVAL;
5899 		goto out;
5900 	}
5901 
5902 	if (ahvif->vdev_type == WMI_VDEV_TYPE_STA &&
5903 	    arg.key_flags == WMI_KEY_PAIRWISE) {
5904 		if (cmd == SET_KEY) {
5905 			arvif->pairwise_key_done = true;
5906 			if (arvif->group_key_valid) {
5907 				/* Install cached GTK */
5908 				arvif->group_key_valid = false;
5909 				arg = arvif->group_key;
5910 				ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
5911 					   "vdev %u pairwise key done, group key ready, go install\n",
5912 					   arg.vdev_id);
5913 				goto install;
5914 			}
5915 		} else {
5916 			arvif->pairwise_key_done = false;
5917 		}
5918 	}
5919 
5920 out:
5921 	if (ret) {
5922 		/* In case of failure userspace may not do DISABLE_KEY
5923 		 * but triggers re-connection directly, so manually reset
5924 		 * status here.
5925 		 */
5926 		arvif->group_key_valid = false;
5927 		arvif->pairwise_key_done = false;
5928 	}
5929 
5930 	return ret;
5931 }
5932 
5933 static int ath12k_clear_peer_keys(struct ath12k_link_vif *arvif,
5934 				  const u8 *addr)
5935 {
5936 	struct ath12k *ar = arvif->ar;
5937 	struct ath12k_base *ab = ar->ab;
5938 	struct ath12k_dp_link_peer *peer;
5939 	int first_errno = 0;
5940 	int ret;
5941 	int i, len;
5942 	u32 flags = 0;
5943 	struct ath12k_dp *dp = ath12k_ab_to_dp(ab);
5944 	struct ieee80211_key_conf *keys[WMI_MAX_KEY_INDEX + 1] = {};
5945 
5946 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
5947 
5948 	spin_lock_bh(&dp->dp_lock);
5949 	peer = ath12k_dp_link_peer_find_by_vdev_and_addr(dp, arvif->vdev_id, addr);
5950 	if (!peer || !peer->dp_peer) {
5951 		spin_unlock_bh(&dp->dp_lock);
5952 		return -ENOENT;
5953 	}
5954 
5955 	len = ARRAY_SIZE(peer->dp_peer->keys);
5956 	for (i = 0; i < len; i++) {
5957 		if (!peer->dp_peer->keys[i])
5958 			continue;
5959 
5960 		keys[i] = peer->dp_peer->keys[i];
5961 		peer->dp_peer->keys[i] = NULL;
5962 	}
5963 
5964 	spin_unlock_bh(&dp->dp_lock);
5965 
5966 	for (i = 0; i < len; i++) {
5967 		if (!keys[i])
5968 			continue;
5969 
5970 		/* key flags are not required to delete the key */
5971 		ret = ath12k_install_key(arvif, keys[i],
5972 					 DISABLE_KEY, addr, flags);
5973 		if (ret < 0 && first_errno == 0)
5974 			first_errno = ret;
5975 
5976 		if (ret < 0)
5977 			ath12k_warn(ab, "failed to remove peer key %d: %d\n",
5978 				    i, ret);
5979 	}
5980 
5981 	return first_errno;
5982 }
5983 
5984 static int ath12k_mac_set_key(struct ath12k *ar, enum set_key_cmd cmd,
5985 			      struct ath12k_link_vif *arvif,
5986 			      struct ath12k_link_sta *arsta,
5987 			      struct ieee80211_key_conf *key)
5988 {
5989 	struct ieee80211_sta *sta = NULL;
5990 	struct ath12k_base *ab = ar->ab;
5991 	struct ath12k_dp_link_peer *peer;
5992 	struct ath12k_sta *ahsta;
5993 	const u8 *peer_addr;
5994 	int ret;
5995 	u32 flags = 0;
5996 	struct ath12k_dp *dp = ath12k_ab_to_dp(ab);
5997 
5998 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
5999 
6000 	if (arsta)
6001 		sta = ath12k_ahsta_to_sta(arsta->ahsta);
6002 
6003 	if (test_bit(ATH12K_FLAG_HW_CRYPTO_DISABLED, &ab->dev_flags))
6004 		return 1;
6005 
6006 	if (sta)
6007 		peer_addr = arsta->addr;
6008 	else
6009 		peer_addr = arvif->bssid;
6010 
6011 	key->hw_key_idx = key->keyidx;
6012 
6013 	/* the peer should not disappear in mid-way (unless FW goes awry) since
6014 	 * we already hold wiphy lock. we just make sure its there now.
6015 	 */
6016 	spin_lock_bh(&dp->dp_lock);
6017 	peer = ath12k_dp_link_peer_find_by_vdev_and_addr(dp, arvif->vdev_id,
6018 							 peer_addr);
6019 	if (!peer || !peer->dp_peer) {
6020 		spin_unlock_bh(&dp->dp_lock);
6021 
6022 		if (cmd == SET_KEY) {
6023 			ath12k_warn(ab, "cannot install key for non-existent peer %pM\n",
6024 				    peer_addr);
6025 			return -EOPNOTSUPP;
6026 		}
6027 
6028 		/* if the peer doesn't exist there is no key to disable
6029 		 * anymore
6030 		 */
6031 		return 0;
6032 	}
6033 
6034 	spin_unlock_bh(&dp->dp_lock);
6035 
6036 	if (key->flags & IEEE80211_KEY_FLAG_PAIRWISE)
6037 		flags = WMI_KEY_PAIRWISE;
6038 	else
6039 		flags = WMI_KEY_GROUP;
6040 
6041 	ret = ath12k_install_key(arvif, key, cmd, peer_addr, flags);
6042 	if (ret) {
6043 		ath12k_warn(ab, "ath12k_install_key failed (%d)\n", ret);
6044 		return ret;
6045 	}
6046 
6047 	ret = ath12k_dp_rx_peer_pn_replay_config(arvif, peer_addr, cmd, key);
6048 	if (ret) {
6049 		ath12k_warn(ab, "failed to offload PN replay detection %d\n", ret);
6050 		return ret;
6051 	}
6052 
6053 	spin_lock_bh(&dp->dp_lock);
6054 	peer = ath12k_dp_link_peer_find_by_vdev_and_addr(dp, arvif->vdev_id,
6055 							 peer_addr);
6056 	if (peer && peer->dp_peer && cmd == SET_KEY) {
6057 		peer->dp_peer->keys[key->keyidx] = key;
6058 		if (key->flags & IEEE80211_KEY_FLAG_PAIRWISE) {
6059 			peer->dp_peer->ucast_keyidx = key->keyidx;
6060 			peer->dp_peer->sec_type =
6061 					ath12k_dp_tx_get_encrypt_type(key->cipher);
6062 		} else {
6063 			peer->dp_peer->mcast_keyidx = key->keyidx;
6064 			peer->dp_peer->sec_type_grp =
6065 					ath12k_dp_tx_get_encrypt_type(key->cipher);
6066 		}
6067 	} else if (peer && peer->dp_peer && cmd == DISABLE_KEY) {
6068 		peer->dp_peer->keys[key->keyidx] = NULL;
6069 		if (key->flags & IEEE80211_KEY_FLAG_PAIRWISE)
6070 			peer->dp_peer->ucast_keyidx = 0;
6071 		else
6072 			peer->dp_peer->mcast_keyidx = 0;
6073 	} else if (!peer)
6074 		/* impossible unless FW goes crazy */
6075 		ath12k_warn(ab, "peer %pM disappeared!\n", peer_addr);
6076 
6077 	if (sta) {
6078 		ahsta = ath12k_sta_to_ahsta(sta);
6079 
6080 		switch (key->cipher) {
6081 		case WLAN_CIPHER_SUITE_TKIP:
6082 		case WLAN_CIPHER_SUITE_CCMP:
6083 		case WLAN_CIPHER_SUITE_CCMP_256:
6084 		case WLAN_CIPHER_SUITE_GCMP:
6085 		case WLAN_CIPHER_SUITE_GCMP_256:
6086 			if (cmd == SET_KEY)
6087 				ahsta->pn_type = HAL_PN_TYPE_WPA;
6088 			else
6089 				ahsta->pn_type = HAL_PN_TYPE_NONE;
6090 			break;
6091 		default:
6092 			ahsta->pn_type = HAL_PN_TYPE_NONE;
6093 			break;
6094 		}
6095 	}
6096 
6097 	spin_unlock_bh(&dp->dp_lock);
6098 
6099 	return 0;
6100 }
6101 
6102 static int ath12k_mac_update_key_cache(struct ath12k_vif_cache *cache,
6103 				       enum set_key_cmd cmd,
6104 				       struct ieee80211_sta *sta,
6105 				       struct ieee80211_key_conf *key)
6106 {
6107 	struct ath12k_key_conf *key_conf, *tmp;
6108 
6109 	list_for_each_entry_safe(key_conf, tmp, &cache->key_conf.list, list) {
6110 		if (key_conf->key != key)
6111 			continue;
6112 
6113 		/* If SET key entry is already present in cache, nothing to do,
6114 		 * just return
6115 		 */
6116 		if (cmd == SET_KEY)
6117 			return 0;
6118 
6119 		/* DEL key for an old SET key which driver hasn't flushed yet.
6120 		 */
6121 		list_del(&key_conf->list);
6122 		kfree(key_conf);
6123 	}
6124 
6125 	if (cmd == SET_KEY) {
6126 		key_conf = kzalloc_obj(*key_conf);
6127 
6128 		if (!key_conf)
6129 			return -ENOMEM;
6130 
6131 		key_conf->cmd = cmd;
6132 		key_conf->sta = sta;
6133 		key_conf->key = key;
6134 		list_add_tail(&key_conf->list,
6135 			      &cache->key_conf.list);
6136 	}
6137 
6138 	return 0;
6139 }
6140 
6141 int ath12k_mac_op_set_key(struct ieee80211_hw *hw, enum set_key_cmd cmd,
6142 			  struct ieee80211_vif *vif, struct ieee80211_sta *sta,
6143 			  struct ieee80211_key_conf *key)
6144 {
6145 	struct ath12k_vif *ahvif = ath12k_vif_to_ahvif(vif);
6146 	struct ath12k_link_vif *arvif;
6147 	struct ath12k_link_sta *arsta = NULL;
6148 	struct ath12k_vif_cache *cache;
6149 	struct ath12k_sta *ahsta;
6150 	unsigned long links;
6151 	u8 link_id;
6152 	int ret;
6153 
6154 	lockdep_assert_wiphy(hw->wiphy);
6155 
6156 	/* IGTK needs to be done in host software */
6157 	if (key->keyidx == 4 || key->keyidx == 5)
6158 		return 1;
6159 
6160 	if (key->keyidx > WMI_MAX_KEY_INDEX)
6161 		return -ENOSPC;
6162 
6163 	if (sta) {
6164 		ahsta = ath12k_sta_to_ahsta(sta);
6165 
6166 		/* For an ML STA Pairwise key is same for all associated link Stations,
6167 		 * hence do set key for all link STAs which are active.
6168 		 */
6169 		if (sta->mlo) {
6170 			links = ahsta->links_map;
6171 			for_each_set_bit(link_id, &links, IEEE80211_MLD_MAX_NUM_LINKS) {
6172 				arvif = wiphy_dereference(hw->wiphy,
6173 							  ahvif->link[link_id]);
6174 				arsta = wiphy_dereference(hw->wiphy,
6175 							  ahsta->link[link_id]);
6176 
6177 				if (WARN_ON(!arvif || !arsta))
6178 					/* arvif and arsta are expected to be valid when
6179 					 * STA is present.
6180 					 */
6181 					continue;
6182 
6183 				ret = ath12k_mac_set_key(arvif->ar, cmd, arvif,
6184 							 arsta, key);
6185 				if (ret)
6186 					break;
6187 			}
6188 
6189 			return 0;
6190 		}
6191 
6192 		arsta = &ahsta->deflink;
6193 		arvif = arsta->arvif;
6194 		if (WARN_ON(!arvif))
6195 			return -EINVAL;
6196 
6197 		ret = ath12k_mac_set_key(arvif->ar, cmd, arvif, arsta, key);
6198 		if (ret)
6199 			return ret;
6200 
6201 		return 0;
6202 	}
6203 
6204 	if (key->link_id >= 0 && key->link_id < IEEE80211_MLD_MAX_NUM_LINKS) {
6205 		link_id = key->link_id;
6206 		arvif = wiphy_dereference(hw->wiphy, ahvif->link[link_id]);
6207 	} else {
6208 		link_id = 0;
6209 		arvif = &ahvif->deflink;
6210 	}
6211 
6212 	if (!arvif || !arvif->is_created) {
6213 		cache = ath12k_ahvif_get_link_cache(ahvif, link_id);
6214 		if (!cache)
6215 			return -ENOSPC;
6216 
6217 		ret = ath12k_mac_update_key_cache(cache, cmd, sta, key);
6218 		if (ret)
6219 			return ret;
6220 
6221 		return 0;
6222 	}
6223 
6224 	ret = ath12k_mac_set_key(arvif->ar, cmd, arvif, NULL, key);
6225 	if (ret)
6226 		return ret;
6227 
6228 	return 0;
6229 }
6230 EXPORT_SYMBOL(ath12k_mac_op_set_key);
6231 
6232 static int
6233 ath12k_mac_bitrate_mask_num_vht_rates(struct ath12k *ar,
6234 				      enum nl80211_band band,
6235 				      const struct cfg80211_bitrate_mask *mask)
6236 {
6237 	int num_rates = 0;
6238 	int i;
6239 
6240 	for (i = 0; i < ARRAY_SIZE(mask->control[band].vht_mcs); i++)
6241 		num_rates += hweight16(mask->control[band].vht_mcs[i]);
6242 
6243 	return num_rates;
6244 }
6245 
6246 static int
6247 ath12k_mac_bitrate_mask_num_he_rates(struct ath12k *ar,
6248 				     enum nl80211_band band,
6249 				     const struct cfg80211_bitrate_mask *mask)
6250 {
6251 	int num_rates = 0;
6252 	int i;
6253 
6254 	for (i = 0; i < ARRAY_SIZE(mask->control[band].he_mcs); i++)
6255 		num_rates += hweight16(mask->control[band].he_mcs[i]);
6256 
6257 	return num_rates;
6258 }
6259 
6260 static int
6261 ath12k_mac_bitrate_mask_num_eht_rates(struct ath12k *ar,
6262 				      enum nl80211_band band,
6263 				      const struct cfg80211_bitrate_mask *mask)
6264 {
6265 	int num_rates = 0;
6266 	int i;
6267 
6268 	for (i = 0; i < ARRAY_SIZE(mask->control[band].eht_mcs); i++)
6269 		num_rates += hweight16(mask->control[band].eht_mcs[i]);
6270 
6271 	return num_rates;
6272 }
6273 
6274 static int
6275 ath12k_mac_set_peer_vht_fixed_rate(struct ath12k_link_vif *arvif,
6276 				   struct ath12k_link_sta *arsta,
6277 				   const struct cfg80211_bitrate_mask *mask,
6278 				   enum nl80211_band band)
6279 {
6280 	struct ath12k *ar = arvif->ar;
6281 	u8 vht_rate, nss;
6282 	u32 rate_code;
6283 	int ret, i;
6284 
6285 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
6286 
6287 	nss = 0;
6288 
6289 	for (i = 0; i < ARRAY_SIZE(mask->control[band].vht_mcs); i++) {
6290 		if (hweight16(mask->control[band].vht_mcs[i]) == 1) {
6291 			nss = i + 1;
6292 			vht_rate = ffs(mask->control[band].vht_mcs[i]) - 1;
6293 		}
6294 	}
6295 
6296 	if (!nss) {
6297 		ath12k_warn(ar->ab, "No single VHT Fixed rate found to set for %pM",
6298 			    arsta->addr);
6299 		return -EINVAL;
6300 	}
6301 
6302 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
6303 		   "Setting Fixed VHT Rate for peer %pM. Device will not switch to any other selected rates",
6304 		   arsta->addr);
6305 
6306 	rate_code = ATH12K_HW_RATE_CODE(vht_rate, nss - 1,
6307 					WMI_RATE_PREAMBLE_VHT);
6308 	ret = ath12k_wmi_set_peer_param(ar, arsta->addr,
6309 					arvif->vdev_id,
6310 					WMI_PEER_PARAM_FIXED_RATE,
6311 					rate_code);
6312 	if (ret)
6313 		ath12k_warn(ar->ab,
6314 			    "failed to update STA %pM Fixed Rate %d: %d\n",
6315 			     arsta->addr, rate_code, ret);
6316 
6317 	return ret;
6318 }
6319 
6320 static int
6321 ath12k_mac_set_peer_he_fixed_rate(struct ath12k_link_vif *arvif,
6322 				  struct ath12k_link_sta *arsta,
6323 				  const struct cfg80211_bitrate_mask *mask,
6324 				  enum nl80211_band band)
6325 {
6326 	struct ath12k *ar = arvif->ar;
6327 	u8 he_rate, nss;
6328 	u32 rate_code;
6329 	int ret, i;
6330 	struct ath12k_sta *ahsta = arsta->ahsta;
6331 	struct ieee80211_sta *sta;
6332 
6333 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
6334 
6335 	sta = ath12k_ahsta_to_sta(ahsta);
6336 	nss = 0;
6337 
6338 	for (i = 0; i < ARRAY_SIZE(mask->control[band].he_mcs); i++) {
6339 		if (hweight16(mask->control[band].he_mcs[i]) == 1) {
6340 			nss = i + 1;
6341 			he_rate = ffs(mask->control[band].he_mcs[i]) - 1;
6342 		}
6343 	}
6344 
6345 	if (!nss) {
6346 		ath12k_warn(ar->ab, "No single HE Fixed rate found to set for %pM",
6347 			    arsta->addr);
6348 		return -EINVAL;
6349 	}
6350 
6351 	/* Avoid updating invalid nss as fixed rate*/
6352 	if (nss > sta->deflink.rx_nss)
6353 		return -EINVAL;
6354 
6355 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
6356 		   "Setting Fixed HE Rate for peer %pM. Device will not switch to any other selected rates",
6357 		   arsta->addr);
6358 
6359 	rate_code = ATH12K_HW_RATE_CODE(he_rate, nss - 1,
6360 					WMI_RATE_PREAMBLE_HE);
6361 
6362 	ret = ath12k_wmi_set_peer_param(ar, arsta->addr,
6363 					arvif->vdev_id,
6364 					WMI_PEER_PARAM_FIXED_RATE,
6365 					rate_code);
6366 	if (ret)
6367 		ath12k_warn(ar->ab,
6368 			    "failed to update STA %pM Fixed Rate %d: %d\n",
6369 			    arsta->addr, rate_code, ret);
6370 
6371 	return ret;
6372 }
6373 
6374 static int
6375 ath12k_mac_set_peer_eht_fixed_rate(struct ath12k_link_vif *arvif,
6376 				   struct ath12k_link_sta *arsta,
6377 				   const struct cfg80211_bitrate_mask *mask,
6378 				   enum nl80211_band band)
6379 {
6380 	struct ath12k_sta *ahsta = arsta->ahsta;
6381 	struct ath12k *ar = arvif->ar;
6382 	struct ieee80211_sta *sta;
6383 	struct ieee80211_link_sta *link_sta;
6384 	u8 eht_rate, nss = 0;
6385 	u32 rate_code;
6386 	int ret, i;
6387 
6388 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
6389 
6390 	sta = ath12k_ahsta_to_sta(ahsta);
6391 
6392 	for (i = 0; i < ARRAY_SIZE(mask->control[band].eht_mcs); i++) {
6393 		if (hweight16(mask->control[band].eht_mcs[i]) == 1) {
6394 			nss = i + 1;
6395 			eht_rate = ffs(mask->control[band].eht_mcs[i]) - 1;
6396 		}
6397 	}
6398 
6399 	if (!nss) {
6400 		ath12k_warn(ar->ab, "No single EHT Fixed rate found to set for %pM\n",
6401 			    arsta->addr);
6402 		return -EINVAL;
6403 	}
6404 
6405 	/* Avoid updating invalid nss as fixed rate*/
6406 	link_sta = ath12k_mac_get_link_sta(arsta);
6407 	if (!link_sta || nss > link_sta->rx_nss) {
6408 		ath12k_warn(ar->ab,
6409 			    "unable to access link sta for sta %pM link %u or fixed nss of %u is not supported by sta\n",
6410 			    sta->addr, arsta->link_id, nss);
6411 		return -EINVAL;
6412 	}
6413 
6414 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
6415 		   "Setting Fixed EHT Rate for peer %pM. Device will not switch to any other selected rates\n",
6416 		   arsta->addr);
6417 
6418 	rate_code = ATH12K_HW_RATE_CODE(eht_rate, nss - 1,
6419 					WMI_RATE_PREAMBLE_EHT);
6420 
6421 	ret = ath12k_wmi_set_peer_param(ar, arsta->addr,
6422 					arvif->vdev_id,
6423 					WMI_PEER_PARAM_FIXED_RATE,
6424 					rate_code);
6425 	if (ret)
6426 		ath12k_warn(ar->ab,
6427 			    "failed to update STA %pM Fixed Rate %d: %d\n",
6428 			    arsta->addr, rate_code, ret);
6429 
6430 	return ret;
6431 }
6432 
6433 static int ath12k_mac_station_assoc(struct ath12k *ar,
6434 				    struct ath12k_link_vif *arvif,
6435 				    struct ath12k_link_sta *arsta,
6436 				    bool reassoc)
6437 {
6438 	struct ieee80211_vif *vif = ath12k_ahvif_to_vif(arvif->ahvif);
6439 	struct ieee80211_sta *sta = ath12k_ahsta_to_sta(arsta->ahsta);
6440 	struct ieee80211_link_sta *link_sta;
6441 	int ret;
6442 	struct cfg80211_chan_def def;
6443 	enum nl80211_band band;
6444 	struct cfg80211_bitrate_mask *mask;
6445 	u8 num_vht_rates, num_he_rates, num_eht_rates;
6446 	u8 link_id = arvif->link_id;
6447 
6448 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
6449 
6450 	if (WARN_ON(ath12k_mac_vif_link_chan(vif, arvif->link_id, &def)))
6451 		return -EPERM;
6452 
6453 	if (WARN_ON(!rcu_access_pointer(sta->link[link_id])))
6454 		return -EINVAL;
6455 
6456 	band = def.chan->band;
6457 	mask = &arvif->bitrate_mask;
6458 
6459 	struct ath12k_wmi_peer_assoc_arg *peer_arg __free(kfree) =
6460 		kzalloc_obj(*peer_arg);
6461 	if (!peer_arg)
6462 		return -ENOMEM;
6463 
6464 	ath12k_peer_assoc_prepare(ar, arvif, arsta, peer_arg, reassoc);
6465 
6466 	if (peer_arg->peer_nss < 1) {
6467 		ath12k_warn(ar->ab,
6468 			    "invalid peer NSS %d\n", peer_arg->peer_nss);
6469 		return -EINVAL;
6470 	}
6471 
6472 	peer_arg->is_assoc = true;
6473 	ret = ath12k_wmi_send_peer_assoc_cmd(ar, peer_arg);
6474 	if (ret) {
6475 		ath12k_warn(ar->ab, "failed to run peer assoc for STA %pM vdev %i: %d\n",
6476 			    arsta->addr, arvif->vdev_id, ret);
6477 		return ret;
6478 	}
6479 
6480 	if (!wait_for_completion_timeout(&ar->peer_assoc_done, 1 * HZ)) {
6481 		ath12k_warn(ar->ab, "failed to get peer assoc conf event for %pM vdev %i\n",
6482 			    arsta->addr, arvif->vdev_id);
6483 		return -ETIMEDOUT;
6484 	}
6485 
6486 	num_vht_rates = ath12k_mac_bitrate_mask_num_vht_rates(ar, band, mask);
6487 	num_he_rates = ath12k_mac_bitrate_mask_num_he_rates(ar, band, mask);
6488 	num_eht_rates = ath12k_mac_bitrate_mask_num_eht_rates(ar, band, mask);
6489 
6490 	/* If single VHT/HE/EHT rate is configured (by set_bitrate_mask()),
6491 	 * peer_assoc will disable VHT/HE/EHT. This is now enabled by a peer
6492 	 * specific fixed param.
6493 	 * Note that all other rates and NSS will be disabled for this peer.
6494 	 */
6495 	link_sta = ath12k_mac_get_link_sta(arsta);
6496 	if (!link_sta) {
6497 		ath12k_warn(ar->ab, "unable to access link sta in station assoc\n");
6498 		return -EINVAL;
6499 	}
6500 
6501 	spin_lock_bh(&ar->data_lock);
6502 	arsta->bw = ath12k_mac_ieee80211_sta_bw_to_wmi(ar, link_sta);
6503 	arsta->bw_prev = link_sta->bandwidth;
6504 	spin_unlock_bh(&ar->data_lock);
6505 
6506 	if (link_sta->vht_cap.vht_supported && num_vht_rates == 1) {
6507 		ret = ath12k_mac_set_peer_vht_fixed_rate(arvif, arsta, mask, band);
6508 	} else if (link_sta->he_cap.has_he && num_he_rates == 1) {
6509 		ret = ath12k_mac_set_peer_he_fixed_rate(arvif, arsta, mask, band);
6510 		if (ret)
6511 			return ret;
6512 	} else if (link_sta->eht_cap.has_eht && num_eht_rates == 1) {
6513 		ret = ath12k_mac_set_peer_eht_fixed_rate(arvif, arsta, mask, band);
6514 		if (ret)
6515 			return ret;
6516 	}
6517 
6518 	/* Re-assoc is run only to update supported rates for given station. It
6519 	 * doesn't make much sense to reconfigure the peer completely.
6520 	 */
6521 	if (reassoc)
6522 		return 0;
6523 
6524 	ret = ath12k_setup_peer_smps(ar, arvif, arsta->addr,
6525 				     &link_sta->ht_cap, &link_sta->he_6ghz_capa);
6526 	if (ret) {
6527 		ath12k_warn(ar->ab, "failed to setup peer SMPS for vdev %d: %d\n",
6528 			    arvif->vdev_id, ret);
6529 		return ret;
6530 	}
6531 
6532 	if (!sta->wme) {
6533 		arvif->num_legacy_stations++;
6534 		ret = ath12k_recalc_rtscts_prot(arvif);
6535 		if (ret)
6536 			return ret;
6537 	}
6538 
6539 	if (sta->wme && sta->uapsd_queues) {
6540 		ret = ath12k_peer_assoc_qos_ap(ar, arvif, arsta);
6541 		if (ret) {
6542 			ath12k_warn(ar->ab, "failed to set qos params for STA %pM for vdev %i: %d\n",
6543 				    arsta->addr, arvif->vdev_id, ret);
6544 			return ret;
6545 		}
6546 	}
6547 
6548 	return 0;
6549 }
6550 
6551 static int ath12k_mac_station_disassoc(struct ath12k *ar,
6552 				       struct ath12k_link_vif *arvif,
6553 				       struct ath12k_link_sta *arsta)
6554 {
6555 	struct ieee80211_sta *sta = ath12k_ahsta_to_sta(arsta->ahsta);
6556 
6557 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
6558 
6559 	if (!sta->wme) {
6560 		arvif->num_legacy_stations--;
6561 		return ath12k_recalc_rtscts_prot(arvif);
6562 	}
6563 
6564 	return 0;
6565 }
6566 
6567 static void ath12k_sta_rc_update_wk(struct wiphy *wiphy, struct wiphy_work *wk)
6568 {
6569 	struct ieee80211_link_sta *link_sta;
6570 	struct ath12k *ar;
6571 	struct ath12k_link_vif *arvif;
6572 	struct ieee80211_sta *sta;
6573 	struct cfg80211_chan_def def;
6574 	enum nl80211_band band;
6575 	const u8 *ht_mcs_mask;
6576 	const u16 *vht_mcs_mask;
6577 	const u16 *he_mcs_mask;
6578 	const u16 *eht_mcs_mask;
6579 	u32 changed, bw, nss, mac_nss, smps, bw_prev;
6580 	int err, num_vht_rates, num_he_rates, num_eht_rates;
6581 	const struct cfg80211_bitrate_mask *mask;
6582 	enum wmi_phy_mode peer_phymode;
6583 	struct ath12k_link_sta *arsta;
6584 	struct ieee80211_vif *vif;
6585 
6586 	lockdep_assert_wiphy(wiphy);
6587 
6588 	arsta = container_of(wk, struct ath12k_link_sta, update_wk);
6589 	sta = ath12k_ahsta_to_sta(arsta->ahsta);
6590 	arvif = arsta->arvif;
6591 	vif = ath12k_ahvif_to_vif(arvif->ahvif);
6592 	ar = arvif->ar;
6593 
6594 	if (WARN_ON(ath12k_mac_vif_link_chan(vif, arvif->link_id, &def)))
6595 		return;
6596 
6597 	band = def.chan->band;
6598 	ht_mcs_mask = arvif->bitrate_mask.control[band].ht_mcs;
6599 	vht_mcs_mask = arvif->bitrate_mask.control[band].vht_mcs;
6600 	he_mcs_mask = arvif->bitrate_mask.control[band].he_mcs;
6601 	eht_mcs_mask = arvif->bitrate_mask.control[band].eht_mcs;
6602 
6603 	spin_lock_bh(&ar->data_lock);
6604 
6605 	changed = arsta->changed;
6606 	arsta->changed = 0;
6607 
6608 	bw = arsta->bw;
6609 	bw_prev = arsta->bw_prev;
6610 	nss = arsta->nss;
6611 	smps = arsta->smps;
6612 
6613 	spin_unlock_bh(&ar->data_lock);
6614 
6615 	nss = max_t(u32, 1, nss);
6616 	mac_nss = max3(ath12k_mac_max_ht_nss(ht_mcs_mask),
6617 		       ath12k_mac_max_vht_nss(vht_mcs_mask),
6618 		       ath12k_mac_max_he_nss(he_mcs_mask));
6619 	mac_nss = max(mac_nss, ath12k_mac_max_eht_nss(eht_mcs_mask));
6620 	nss = min(nss, mac_nss);
6621 
6622 	struct ath12k_wmi_peer_assoc_arg *peer_arg __free(kfree) =
6623 					kzalloc_obj(*peer_arg);
6624 	if (!peer_arg)
6625 		return;
6626 
6627 	if (changed & IEEE80211_RC_BW_CHANGED) {
6628 		ath12k_peer_assoc_h_phymode(ar, arvif, arsta, peer_arg);
6629 		peer_phymode = peer_arg->peer_phymode;
6630 
6631 		if (bw > bw_prev) {
6632 			/* Phymode shows maximum supported channel width, if we
6633 			 * upgrade bandwidth then due to sanity check of firmware,
6634 			 * we have to send WMI_PEER_PHYMODE followed by
6635 			 * WMI_PEER_CHWIDTH
6636 			 */
6637 			ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac bandwidth upgrade for sta %pM new %d old %d\n",
6638 				   arsta->addr, bw, bw_prev);
6639 			err = ath12k_wmi_set_peer_param(ar, arsta->addr,
6640 							arvif->vdev_id, WMI_PEER_PHYMODE,
6641 							peer_phymode);
6642 			if (err) {
6643 				ath12k_warn(ar->ab, "failed to update STA %pM to peer phymode %d: %d\n",
6644 					    arsta->addr, peer_phymode, err);
6645 				return;
6646 			}
6647 			err = ath12k_wmi_set_peer_param(ar, arsta->addr,
6648 							arvif->vdev_id, WMI_PEER_CHWIDTH,
6649 							bw);
6650 			if (err)
6651 				ath12k_warn(ar->ab, "failed to update STA %pM to peer bandwidth %d: %d\n",
6652 					    arsta->addr, bw, err);
6653 		} else {
6654 			/* When we downgrade bandwidth this will conflict with phymode
6655 			 * and cause to trigger firmware crash. In this case we send
6656 			 * WMI_PEER_CHWIDTH followed by WMI_PEER_PHYMODE
6657 			 */
6658 			ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac bandwidth downgrade for sta %pM new %d old %d\n",
6659 				   arsta->addr, bw, bw_prev);
6660 			err = ath12k_wmi_set_peer_param(ar, arsta->addr,
6661 							arvif->vdev_id, WMI_PEER_CHWIDTH,
6662 							bw);
6663 			if (err) {
6664 				ath12k_warn(ar->ab, "failed to update STA %pM peer to bandwidth %d: %d\n",
6665 					    arsta->addr, bw, err);
6666 				return;
6667 			}
6668 			err = ath12k_wmi_set_peer_param(ar, arsta->addr,
6669 							arvif->vdev_id, WMI_PEER_PHYMODE,
6670 							peer_phymode);
6671 			if (err)
6672 				ath12k_warn(ar->ab, "failed to update STA %pM to peer phymode %d: %d\n",
6673 					    arsta->addr, peer_phymode, err);
6674 		}
6675 	}
6676 
6677 	if (changed & IEEE80211_RC_NSS_CHANGED) {
6678 		ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac update sta %pM nss %d\n",
6679 			   arsta->addr, nss);
6680 
6681 		err = ath12k_wmi_set_peer_param(ar, arsta->addr, arvif->vdev_id,
6682 						WMI_PEER_NSS, nss);
6683 		if (err)
6684 			ath12k_warn(ar->ab, "failed to update STA %pM nss %d: %d\n",
6685 				    arsta->addr, nss, err);
6686 	}
6687 
6688 	if (changed & IEEE80211_RC_SMPS_CHANGED) {
6689 		ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac update sta %pM smps %d\n",
6690 			   arsta->addr, smps);
6691 
6692 		err = ath12k_wmi_set_peer_param(ar, arsta->addr, arvif->vdev_id,
6693 						WMI_PEER_MIMO_PS_STATE, smps);
6694 		if (err)
6695 			ath12k_warn(ar->ab, "failed to update STA %pM smps %d: %d\n",
6696 				    arsta->addr, smps, err);
6697 	}
6698 
6699 	if (changed & IEEE80211_RC_SUPP_RATES_CHANGED) {
6700 		mask = &arvif->bitrate_mask;
6701 		num_vht_rates = ath12k_mac_bitrate_mask_num_vht_rates(ar, band,
6702 								      mask);
6703 		num_he_rates = ath12k_mac_bitrate_mask_num_he_rates(ar, band,
6704 								    mask);
6705 		num_eht_rates = ath12k_mac_bitrate_mask_num_eht_rates(ar, band,
6706 								      mask);
6707 
6708 		/* Peer_assoc_prepare will reject vht rates in
6709 		 * bitrate_mask if its not available in range format and
6710 		 * sets vht tx_rateset as unsupported. So multiple VHT MCS
6711 		 * setting(eg. MCS 4,5,6) per peer is not supported here.
6712 		 * But, Single rate in VHT mask can be set as per-peer
6713 		 * fixed rate. But even if any HT rates are configured in
6714 		 * the bitrate mask, device will not switch to those rates
6715 		 * when per-peer Fixed rate is set.
6716 		 * TODO: Check RATEMASK_CMDID to support auto rates selection
6717 		 * across HT/VHT and for multiple VHT MCS support.
6718 		 */
6719 		link_sta = ath12k_mac_get_link_sta(arsta);
6720 		if (!link_sta) {
6721 			ath12k_warn(ar->ab, "unable to access link sta in peer assoc he for sta %pM link %u\n",
6722 				    sta->addr, arsta->link_id);
6723 			return;
6724 		}
6725 
6726 		if (link_sta->vht_cap.vht_supported && num_vht_rates == 1) {
6727 			ath12k_mac_set_peer_vht_fixed_rate(arvif, arsta, mask,
6728 							   band);
6729 		} else if (link_sta->he_cap.has_he && num_he_rates == 1) {
6730 			ath12k_mac_set_peer_he_fixed_rate(arvif, arsta, mask, band);
6731 		} else if (link_sta->eht_cap.has_eht && num_eht_rates == 1) {
6732 			err = ath12k_mac_set_peer_eht_fixed_rate(arvif, arsta,
6733 								 mask, band);
6734 			if (err) {
6735 				ath12k_warn(ar->ab,
6736 					    "failed to set peer EHT fixed rate for STA %pM ret %d\n",
6737 					    arsta->addr, err);
6738 				return;
6739 			}
6740 		} else {
6741 			/* If the peer is non-VHT/HE/EHT or no fixed VHT/HE/EHT
6742 			 * rate is provided in the new bitrate mask we set the
6743 			 * other rates using peer_assoc command. Also clear
6744 			 * the peer fixed rate settings as it has higher proprity
6745 			 * than peer assoc
6746 			 */
6747 			err = ath12k_wmi_set_peer_param(ar, arsta->addr,
6748 							arvif->vdev_id,
6749 							WMI_PEER_PARAM_FIXED_RATE,
6750 							WMI_FIXED_RATE_NONE);
6751 			if (err)
6752 				ath12k_warn(ar->ab,
6753 					    "failed to disable peer fixed rate for STA %pM ret %d\n",
6754 					    arsta->addr, err);
6755 
6756 			ath12k_peer_assoc_prepare(ar, arvif, arsta,
6757 						  peer_arg, true);
6758 
6759 			peer_arg->is_assoc = false;
6760 			err = ath12k_wmi_send_peer_assoc_cmd(ar, peer_arg);
6761 			if (err)
6762 				ath12k_warn(ar->ab, "failed to run peer assoc for STA %pM vdev %i: %d\n",
6763 					    arsta->addr, arvif->vdev_id, err);
6764 
6765 			if (!wait_for_completion_timeout(&ar->peer_assoc_done, 1 * HZ))
6766 				ath12k_warn(ar->ab, "failed to get peer assoc conf event for %pM vdev %i\n",
6767 					    arsta->addr, arvif->vdev_id);
6768 		}
6769 	}
6770 }
6771 
6772 static void ath12k_mac_free_unassign_link_sta(struct ath12k_hw *ah,
6773 					      struct ath12k_sta *ahsta,
6774 					      u8 link_id)
6775 {
6776 	struct ath12k_link_sta *arsta;
6777 
6778 	lockdep_assert_wiphy(ah->hw->wiphy);
6779 
6780 	if (WARN_ON(link_id >= IEEE80211_MLD_MAX_NUM_LINKS))
6781 		return;
6782 
6783 	arsta = wiphy_dereference(ah->hw->wiphy, ahsta->link[link_id]);
6784 	if (WARN_ON(!arsta))
6785 		return;
6786 
6787 	ahsta->links_map &= ~BIT(link_id);
6788 	rcu_assign_pointer(ahsta->link[link_id], NULL);
6789 	synchronize_rcu();
6790 
6791 	if (arsta == &ahsta->deflink) {
6792 		arsta->link_id = ATH12K_INVALID_LINK_ID;
6793 		arsta->ahsta = NULL;
6794 		arsta->arvif = NULL;
6795 		return;
6796 	}
6797 
6798 	kfree(arsta);
6799 }
6800 
6801 static int ath12k_mac_inc_num_stations(struct ath12k_link_vif *arvif,
6802 				       struct ath12k_link_sta *arsta)
6803 {
6804 	struct ieee80211_sta *sta = ath12k_ahsta_to_sta(arsta->ahsta);
6805 	struct ath12k *ar = arvif->ar;
6806 
6807 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
6808 
6809 	if (arvif->ahvif->vdev_type == WMI_VDEV_TYPE_STA && !sta->tdls)
6810 		return 0;
6811 
6812 	if (ar->num_stations >= ar->max_num_stations)
6813 		return -ENOBUFS;
6814 
6815 	ar->num_stations++;
6816 	arvif->num_stations++;
6817 
6818 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
6819 		   "mac station %pM connected to vdev %u num_stations %u\n",
6820 		   arsta->addr, arvif->vdev_id, arvif->num_stations);
6821 
6822 	return 0;
6823 }
6824 
6825 static void ath12k_mac_dec_num_stations(struct ath12k_link_vif *arvif,
6826 					struct ath12k_link_sta *arsta)
6827 {
6828 	struct ieee80211_sta *sta = ath12k_ahsta_to_sta(arsta->ahsta);
6829 	struct ath12k *ar = arvif->ar;
6830 
6831 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
6832 
6833 	if (arvif->ahvif->vdev_type == WMI_VDEV_TYPE_STA && !sta->tdls)
6834 		return;
6835 
6836 	ar->num_stations--;
6837 
6838 	if (arvif->num_stations) {
6839 		arvif->num_stations--;
6840 		ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
6841 			   "mac station %pM disconnected from vdev %u num_stations %u\n",
6842 			   arsta->addr, arvif->vdev_id, arvif->num_stations);
6843 	} else {
6844 		ath12k_warn(ar->ab,
6845 			    "mac station %pM disconnect for vdev %u without any connected station\n",
6846 			    arsta->addr, arvif->vdev_id);
6847 	}
6848 }
6849 
6850 static void ath12k_mac_station_post_remove(struct ath12k *ar,
6851 					   struct ath12k_link_vif *arvif,
6852 					   struct ath12k_link_sta *arsta)
6853 {
6854 	struct ieee80211_vif *vif = ath12k_ahvif_to_vif(arvif->ahvif);
6855 	struct ieee80211_sta *sta = ath12k_ahsta_to_sta(arsta->ahsta);
6856 	struct ath12k_dp_link_peer *peer;
6857 	struct ath12k_dp *dp = ath12k_ab_to_dp(ar->ab);
6858 
6859 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
6860 
6861 	ath12k_mac_dec_num_stations(arvif, arsta);
6862 
6863 	spin_lock_bh(&dp->dp_lock);
6864 
6865 	peer = ath12k_dp_link_peer_find_by_vdev_and_addr(dp, arvif->vdev_id,
6866 							 arsta->addr);
6867 	if (peer && peer->sta == sta) {
6868 		ath12k_warn(ar->ab, "Found peer entry %pM n vdev %i after it was supposedly removed\n",
6869 			    vif->addr, arvif->vdev_id);
6870 		peer->sta = NULL;
6871 
6872 		ath12k_dp_link_peer_free(peer);
6873 		ar->num_peers--;
6874 	}
6875 
6876 	spin_unlock_bh(&dp->dp_lock);
6877 }
6878 
6879 static int ath12k_mac_station_unauthorize(struct ath12k *ar,
6880 					  struct ath12k_link_vif *arvif,
6881 					  struct ath12k_link_sta *arsta)
6882 {
6883 	struct ath12k_dp_link_peer *peer;
6884 	int ret;
6885 	struct ath12k_dp *dp = ath12k_ab_to_dp(ar->ab);
6886 
6887 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
6888 
6889 	spin_lock_bh(&dp->dp_lock);
6890 
6891 	peer = ath12k_dp_link_peer_find_by_vdev_and_addr(dp, arvif->vdev_id,
6892 							 arsta->addr);
6893 	if (peer)
6894 		peer->is_authorized = false;
6895 
6896 	spin_unlock_bh(&dp->dp_lock);
6897 
6898 	/* Driver must clear the keys during the state change from
6899 	 * IEEE80211_STA_AUTHORIZED to IEEE80211_STA_ASSOC, since after
6900 	 * returning from here, mac80211 is going to delete the keys
6901 	 * in __sta_info_destroy_part2(). This will ensure that the driver does
6902 	 * not retain stale key references after mac80211 deletes the keys.
6903 	 */
6904 	ret = ath12k_clear_peer_keys(arvif, arsta->addr);
6905 	if (ret) {
6906 		ath12k_warn(ar->ab, "failed to clear all peer keys for vdev %i: %d\n",
6907 			    arvif->vdev_id, ret);
6908 		return ret;
6909 	}
6910 
6911 	return 0;
6912 }
6913 
6914 static int ath12k_mac_station_authorize(struct ath12k *ar,
6915 					struct ath12k_link_vif *arvif,
6916 					struct ath12k_link_sta *arsta)
6917 {
6918 	struct ath12k_dp_link_peer *peer;
6919 	struct ieee80211_vif *vif = ath12k_ahvif_to_vif(arvif->ahvif);
6920 	int ret;
6921 	struct ath12k_dp *dp = ath12k_ab_to_dp(ar->ab);
6922 
6923 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
6924 
6925 	spin_lock_bh(&dp->dp_lock);
6926 
6927 	peer = ath12k_dp_link_peer_find_by_vdev_and_addr(dp, arvif->vdev_id,
6928 							 arsta->addr);
6929 	if (peer)
6930 		peer->is_authorized = true;
6931 
6932 	spin_unlock_bh(&dp->dp_lock);
6933 
6934 	if (vif->type == NL80211_IFTYPE_STATION && arvif->is_up) {
6935 		ret = ath12k_wmi_set_peer_param(ar, arsta->addr,
6936 						arvif->vdev_id,
6937 						WMI_PEER_AUTHORIZE,
6938 						1);
6939 		if (ret) {
6940 			ath12k_warn(ar->ab, "Unable to authorize peer %pM vdev %d: %d\n",
6941 				    arsta->addr, arvif->vdev_id, ret);
6942 			return ret;
6943 		}
6944 	}
6945 
6946 	return 0;
6947 }
6948 
6949 static int ath12k_mac_station_remove(struct ath12k *ar,
6950 				     struct ath12k_link_vif *arvif,
6951 				     struct ath12k_link_sta *arsta)
6952 {
6953 	struct ieee80211_sta *sta = ath12k_ahsta_to_sta(arsta->ahsta);
6954 	struct ath12k_vif *ahvif = arvif->ahvif;
6955 	int ret = 0;
6956 	struct ath12k_link_sta *temp_arsta;
6957 
6958 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
6959 
6960 	wiphy_work_cancel(ar->ah->hw->wiphy, &arsta->update_wk);
6961 
6962 	if (ahvif->vdev_type == WMI_VDEV_TYPE_STA) {
6963 		ath12k_bss_disassoc(ar, arvif);
6964 		ret = ath12k_mac_vdev_stop(arvif);
6965 		if (ret)
6966 			ath12k_warn(ar->ab, "failed to stop vdev %i: %d\n",
6967 				    arvif->vdev_id, ret);
6968 	}
6969 
6970 	if (sta->mlo)
6971 		return ret;
6972 
6973 	ath12k_dp_peer_cleanup(ar, arvif->vdev_id, arsta->addr);
6974 
6975 	ret = ath12k_peer_delete(ar, arvif->vdev_id, arsta->addr);
6976 	if (ret)
6977 		ath12k_warn(ar->ab, "Failed to delete peer: %pM for VDEV: %d\n",
6978 			    arsta->addr, arvif->vdev_id);
6979 	else
6980 		ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "Removed peer: %pM for VDEV: %d\n",
6981 			   arsta->addr, arvif->vdev_id);
6982 
6983 	ath12k_mac_station_post_remove(ar, arvif, arsta);
6984 
6985 	spin_lock_bh(&ar->ab->base_lock);
6986 
6987 	/* To handle roaming and split phy scenario */
6988 	temp_arsta = ath12k_link_sta_find_by_addr(ar->ab, arsta->addr);
6989 	if (temp_arsta && temp_arsta->arvif->ar == ar)
6990 		ath12k_link_sta_rhash_delete(ar->ab, arsta);
6991 
6992 	spin_unlock_bh(&ar->ab->base_lock);
6993 
6994 	if (sta->valid_links)
6995 		ath12k_mac_free_unassign_link_sta(ahvif->ah,
6996 						  arsta->ahsta, arsta->link_id);
6997 
6998 	return ret;
6999 }
7000 
7001 static int ath12k_mac_station_add(struct ath12k *ar,
7002 				  struct ath12k_link_vif *arvif,
7003 				  struct ath12k_link_sta *arsta)
7004 {
7005 	struct ath12k_base *ab = ar->ab;
7006 	struct ieee80211_vif *vif = ath12k_ahvif_to_vif(arvif->ahvif);
7007 	struct ieee80211_sta *sta = ath12k_ahsta_to_sta(arsta->ahsta);
7008 	struct ath12k_wmi_peer_create_arg peer_param = {};
7009 	int ret;
7010 	struct ath12k_link_sta *temp_arsta;
7011 
7012 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
7013 
7014 	ret = ath12k_mac_inc_num_stations(arvif, arsta);
7015 	if (ret) {
7016 		ath12k_warn(ab, "refusing to associate station: too many connected already (%d)\n",
7017 			    ar->max_num_stations);
7018 		goto exit;
7019 	}
7020 
7021 	spin_lock_bh(&ab->base_lock);
7022 
7023 	/*
7024 	 * In case of Split PHY and roaming scenario, pdev idx
7025 	 * might differ but both the pdev will share same rhash
7026 	 * table. In that case update the rhash table if link_sta is
7027 	 * already present
7028 	 */
7029 	temp_arsta = ath12k_link_sta_find_by_addr(ab, arsta->addr);
7030 	if (temp_arsta && temp_arsta->arvif->ar != ar)
7031 		ath12k_link_sta_rhash_delete(ab, temp_arsta);
7032 
7033 	ret = ath12k_link_sta_rhash_add(ab, arsta);
7034 	spin_unlock_bh(&ab->base_lock);
7035 	if (ret) {
7036 		ath12k_warn(ab, "Failed to add arsta: %pM to hash table, ret: %d",
7037 			    arsta->addr, ret);
7038 		goto dec_num_station;
7039 	}
7040 
7041 	peer_param.vdev_id = arvif->vdev_id;
7042 	peer_param.peer_addr = arsta->addr;
7043 	peer_param.peer_type = WMI_PEER_TYPE_DEFAULT;
7044 	peer_param.ml_enabled = sta->mlo;
7045 
7046 	ret = ath12k_peer_create(ar, arvif, sta, &peer_param);
7047 	if (ret) {
7048 		ath12k_warn(ab, "Failed to add peer: %pM for VDEV: %d\n",
7049 			    arsta->addr, arvif->vdev_id);
7050 		goto free_peer;
7051 	}
7052 
7053 	ath12k_dbg(ab, ATH12K_DBG_MAC, "Added peer: %pM for VDEV: %d\n",
7054 		   arsta->addr, arvif->vdev_id);
7055 
7056 	if (ieee80211_vif_is_mesh(vif)) {
7057 		ret = ath12k_wmi_set_peer_param(ar, arsta->addr,
7058 						arvif->vdev_id,
7059 						WMI_PEER_USE_4ADDR, 1);
7060 		if (ret) {
7061 			ath12k_warn(ab, "failed to STA %pM 4addr capability: %d\n",
7062 				    arsta->addr, ret);
7063 			goto free_peer;
7064 		}
7065 	}
7066 
7067 	ret = ath12k_dp_peer_setup(ar, arvif->vdev_id, arsta->addr);
7068 	if (ret) {
7069 		ath12k_warn(ab, "failed to setup dp for peer %pM on vdev %i (%d)\n",
7070 			    arsta->addr, arvif->vdev_id, ret);
7071 		goto free_peer;
7072 	}
7073 
7074 	if (ab->hw_params->vdev_start_delay &&
7075 	    !arvif->is_started &&
7076 	    arvif->ahvif->vdev_type != WMI_VDEV_TYPE_AP) {
7077 		ret = ath12k_start_vdev_delay(ar, arvif);
7078 		if (ret) {
7079 			ath12k_warn(ab, "failed to delay vdev start: %d\n", ret);
7080 			goto free_peer;
7081 		}
7082 	}
7083 
7084 	return 0;
7085 
7086 free_peer:
7087 	ath12k_peer_delete(ar, arvif->vdev_id, arsta->addr);
7088 	spin_lock_bh(&ab->base_lock);
7089 	ath12k_link_sta_rhash_delete(ab, arsta);
7090 	spin_unlock_bh(&ab->base_lock);
7091 dec_num_station:
7092 	ath12k_mac_dec_num_stations(arvif, arsta);
7093 exit:
7094 	return ret;
7095 }
7096 
7097 static int ath12k_mac_assign_link_sta(struct ath12k_hw *ah,
7098 				      struct ath12k_sta *ahsta,
7099 				      struct ath12k_link_sta *arsta,
7100 				      struct ath12k_vif *ahvif,
7101 				      u8 link_id)
7102 {
7103 	struct ieee80211_sta *sta = ath12k_ahsta_to_sta(ahsta);
7104 	struct ieee80211_link_sta *link_sta;
7105 	struct ath12k_link_vif *arvif;
7106 
7107 	lockdep_assert_wiphy(ah->hw->wiphy);
7108 
7109 	if (!arsta || link_id >= IEEE80211_MLD_MAX_NUM_LINKS)
7110 		return -EINVAL;
7111 
7112 	arvif = wiphy_dereference(ah->hw->wiphy, ahvif->link[link_id]);
7113 	if (!arvif)
7114 		return -EINVAL;
7115 
7116 	memset(arsta, 0, sizeof(*arsta));
7117 
7118 	link_sta = wiphy_dereference(ah->hw->wiphy, sta->link[link_id]);
7119 	if (!link_sta)
7120 		return -EINVAL;
7121 
7122 	ether_addr_copy(arsta->addr, link_sta->addr);
7123 
7124 	/* logical index of the link sta in order of creation */
7125 	arsta->link_idx = ahsta->num_peer++;
7126 
7127 	arsta->link_id = link_id;
7128 	ahsta->links_map |= BIT(arsta->link_id);
7129 	arsta->arvif = arvif;
7130 	arsta->ahsta = ahsta;
7131 	ahsta->ahvif = ahvif;
7132 
7133 	wiphy_work_init(&arsta->update_wk, ath12k_sta_rc_update_wk);
7134 
7135 	rcu_assign_pointer(ahsta->link[link_id], arsta);
7136 
7137 	return 0;
7138 }
7139 
7140 static void ath12k_mac_ml_station_remove(struct ath12k_vif *ahvif,
7141 					 struct ath12k_sta *ahsta)
7142 {
7143 	struct ieee80211_sta *sta = ath12k_ahsta_to_sta(ahsta);
7144 	struct ath12k_hw *ah = ahvif->ah;
7145 	struct ath12k_link_vif *arvif;
7146 	struct ath12k_link_sta *arsta;
7147 	unsigned long links;
7148 	struct ath12k *ar;
7149 	u8 link_id;
7150 
7151 	lockdep_assert_wiphy(ah->hw->wiphy);
7152 
7153 	ath12k_peer_mlo_link_peers_delete(ahvif, ahsta);
7154 
7155 	/* validate link station removal and clear arsta links */
7156 	links = ahsta->links_map;
7157 	for_each_set_bit(link_id, &links, IEEE80211_MLD_MAX_NUM_LINKS) {
7158 		arvif = wiphy_dereference(ah->hw->wiphy, ahvif->link[link_id]);
7159 		arsta = wiphy_dereference(ah->hw->wiphy, ahsta->link[link_id]);
7160 		if (!arvif || !arsta)
7161 			continue;
7162 
7163 		ar = arvif->ar;
7164 
7165 		ath12k_mac_station_post_remove(ar, arvif, arsta);
7166 
7167 		spin_lock_bh(&ar->ab->base_lock);
7168 		ath12k_link_sta_rhash_delete(ar->ab, arsta);
7169 		spin_unlock_bh(&ar->ab->base_lock);
7170 
7171 		ath12k_mac_free_unassign_link_sta(ah, ahsta, link_id);
7172 	}
7173 
7174 	if (sta->mlo) {
7175 		clear_bit(ahsta->ml_peer_id, ah->free_ml_peer_id_map);
7176 		ahsta->ml_peer_id = ATH12K_MLO_PEER_ID_INVALID;
7177 	}
7178 }
7179 
7180 static int ath12k_mac_handle_link_sta_state(struct ieee80211_hw *hw,
7181 					    struct ath12k_link_vif *arvif,
7182 					    struct ath12k_link_sta *arsta,
7183 					    enum ieee80211_sta_state old_state,
7184 					    enum ieee80211_sta_state new_state)
7185 {
7186 	struct ieee80211_vif *vif = ath12k_ahvif_to_vif(arvif->ahvif);
7187 	struct ieee80211_bss_conf *link_conf;
7188 	struct ath12k *ar = arvif->ar;
7189 	struct ath12k_reg_info *reg_info;
7190 	struct ath12k_base *ab = ar->ab;
7191 	int ret = 0;
7192 
7193 	lockdep_assert_wiphy(hw->wiphy);
7194 
7195 	ath12k_dbg(ab, ATH12K_DBG_MAC, "mac handle link %u sta %pM state %d -> %d\n",
7196 		   arsta->link_id, arsta->addr, old_state, new_state);
7197 
7198 	/* IEEE80211_STA_NONE -> IEEE80211_STA_NOTEXIST: Remove the station
7199 	 * from driver
7200 	 */
7201 	if ((old_state == IEEE80211_STA_NONE &&
7202 	     new_state == IEEE80211_STA_NOTEXIST)) {
7203 		ret = ath12k_mac_station_remove(ar, arvif, arsta);
7204 		if (ret) {
7205 			ath12k_warn(ab, "Failed to remove station: %pM for VDEV: %d\n",
7206 				    arsta->addr, arvif->vdev_id);
7207 			goto exit;
7208 		}
7209 	}
7210 
7211 	/* IEEE80211_STA_NOTEXIST -> IEEE80211_STA_NONE: Add new station to driver */
7212 	if (old_state == IEEE80211_STA_NOTEXIST &&
7213 	    new_state == IEEE80211_STA_NONE) {
7214 		ret = ath12k_mac_station_add(ar, arvif, arsta);
7215 		if (ret)
7216 			ath12k_warn(ab, "Failed to add station: %pM for VDEV: %d\n",
7217 				    arsta->addr, arvif->vdev_id);
7218 
7219 	/* IEEE80211_STA_AUTH -> IEEE80211_STA_ASSOC: Send station assoc command for
7220 	 * peer associated to AP/Mesh/ADHOC vif type.
7221 	 */
7222 	} else if (old_state == IEEE80211_STA_AUTH &&
7223 		   new_state == IEEE80211_STA_ASSOC &&
7224 		   (vif->type == NL80211_IFTYPE_AP ||
7225 		    vif->type == NL80211_IFTYPE_MESH_POINT ||
7226 		    vif->type == NL80211_IFTYPE_ADHOC)) {
7227 		ret = ath12k_mac_station_assoc(ar, arvif, arsta, false);
7228 		if (ret)
7229 			ath12k_warn(ab, "Failed to associate station: %pM\n",
7230 				    arsta->addr);
7231 
7232 	/* IEEE80211_STA_ASSOC -> IEEE80211_STA_AUTHORIZED: set peer status as
7233 	 * authorized
7234 	 */
7235 	} else if (old_state == IEEE80211_STA_ASSOC &&
7236 		   new_state == IEEE80211_STA_AUTHORIZED) {
7237 		ret = ath12k_mac_station_authorize(ar, arvif, arsta);
7238 		if (ret) {
7239 			ath12k_warn(ab, "Failed to authorize station: %pM\n",
7240 				    arsta->addr);
7241 			goto exit;
7242 		}
7243 
7244 		if (ath12k_wmi_supports_6ghz_cc_ext(ar) &&
7245 		    arvif->ahvif->vdev_type == WMI_VDEV_TYPE_STA) {
7246 			link_conf = ath12k_mac_get_link_bss_conf(arvif);
7247 			reg_info = ab->reg_info[ar->pdev_idx];
7248 			ath12k_dbg(ab, ATH12K_DBG_MAC, "connection done, update reg rules\n");
7249 			ath12k_hw_to_ah(hw)->regd_updated = false;
7250 			ath12k_reg_handle_chan_list(ab, reg_info, arvif->ahvif->vdev_type,
7251 						    link_conf->power_type);
7252 		}
7253 
7254 	/* IEEE80211_STA_AUTHORIZED -> IEEE80211_STA_ASSOC: station may be in removal,
7255 	 * deauthorize it.
7256 	 */
7257 	} else if (old_state == IEEE80211_STA_AUTHORIZED &&
7258 		   new_state == IEEE80211_STA_ASSOC) {
7259 		ath12k_mac_station_unauthorize(ar, arvif, arsta);
7260 
7261 	/* IEEE80211_STA_ASSOC -> IEEE80211_STA_AUTH: disassoc peer connected to
7262 	 * AP/mesh/ADHOC vif type.
7263 	 */
7264 	} else if (old_state == IEEE80211_STA_ASSOC &&
7265 		   new_state == IEEE80211_STA_AUTH &&
7266 		   (vif->type == NL80211_IFTYPE_AP ||
7267 		    vif->type == NL80211_IFTYPE_MESH_POINT ||
7268 		    vif->type == NL80211_IFTYPE_ADHOC)) {
7269 		ret = ath12k_mac_station_disassoc(ar, arvif, arsta);
7270 		if (ret)
7271 			ath12k_warn(ab, "Failed to disassociate station: %pM\n",
7272 				    arsta->addr);
7273 	}
7274 
7275 exit:
7276 	return ret;
7277 }
7278 
7279 static bool ath12k_mac_is_freq_on_mac(struct ath12k_hw_mode_freq_range_arg *freq_range,
7280 				      u32 freq, u8 mac_id)
7281 {
7282 	return (freq >= freq_range[mac_id].low_2ghz_freq &&
7283 		freq <= freq_range[mac_id].high_2ghz_freq) ||
7284 	       (freq >= freq_range[mac_id].low_5ghz_freq &&
7285 		freq <= freq_range[mac_id].high_5ghz_freq);
7286 }
7287 
7288 static bool
7289 ath12k_mac_2_freq_same_mac_in_freq_range(struct ath12k_base *ab,
7290 					 struct ath12k_hw_mode_freq_range_arg *freq_range,
7291 					 u32 freq_link1, u32 freq_link2)
7292 {
7293 	u8 i;
7294 
7295 	for (i = 0; i < MAX_RADIOS; i++) {
7296 		if (ath12k_mac_is_freq_on_mac(freq_range, freq_link1, i) &&
7297 		    ath12k_mac_is_freq_on_mac(freq_range, freq_link2, i))
7298 			return true;
7299 	}
7300 
7301 	return false;
7302 }
7303 
7304 static bool ath12k_mac_is_hw_dbs_capable(struct ath12k_base *ab)
7305 {
7306 	return test_bit(WMI_TLV_SERVICE_DUAL_BAND_SIMULTANEOUS_SUPPORT,
7307 			ab->wmi_ab.svc_map) &&
7308 	       ab->wmi_ab.hw_mode_info.support_dbs;
7309 }
7310 
7311 static bool ath12k_mac_2_freq_same_mac_in_dbs(struct ath12k_base *ab,
7312 					      u32 freq_link1, u32 freq_link2)
7313 {
7314 	struct ath12k_hw_mode_freq_range_arg *freq_range;
7315 
7316 	if (!ath12k_mac_is_hw_dbs_capable(ab))
7317 		return true;
7318 
7319 	freq_range = ab->wmi_ab.hw_mode_info.freq_range_caps[ATH12K_HW_MODE_DBS];
7320 	return ath12k_mac_2_freq_same_mac_in_freq_range(ab, freq_range,
7321 							freq_link1, freq_link2);
7322 }
7323 
7324 static bool ath12k_mac_is_hw_sbs_capable(struct ath12k_base *ab)
7325 {
7326 	return test_bit(WMI_TLV_SERVICE_DUAL_BAND_SIMULTANEOUS_SUPPORT,
7327 			ab->wmi_ab.svc_map) &&
7328 	       ab->wmi_ab.hw_mode_info.support_sbs;
7329 }
7330 
7331 static bool ath12k_mac_2_freq_same_mac_in_sbs(struct ath12k_base *ab,
7332 					      u32 freq_link1, u32 freq_link2)
7333 {
7334 	struct ath12k_hw_mode_info *info = &ab->wmi_ab.hw_mode_info;
7335 	struct ath12k_hw_mode_freq_range_arg *sbs_uppr_share;
7336 	struct ath12k_hw_mode_freq_range_arg *sbs_low_share;
7337 	struct ath12k_hw_mode_freq_range_arg *sbs_range;
7338 
7339 	if (!ath12k_mac_is_hw_sbs_capable(ab))
7340 		return true;
7341 
7342 	if (ab->wmi_ab.sbs_lower_band_end_freq) {
7343 		sbs_uppr_share = info->freq_range_caps[ATH12K_HW_MODE_SBS_UPPER_SHARE];
7344 		sbs_low_share = info->freq_range_caps[ATH12K_HW_MODE_SBS_LOWER_SHARE];
7345 
7346 		return ath12k_mac_2_freq_same_mac_in_freq_range(ab, sbs_low_share,
7347 								freq_link1, freq_link2) ||
7348 		       ath12k_mac_2_freq_same_mac_in_freq_range(ab, sbs_uppr_share,
7349 								freq_link1, freq_link2);
7350 	}
7351 
7352 	sbs_range = info->freq_range_caps[ATH12K_HW_MODE_SBS];
7353 	return ath12k_mac_2_freq_same_mac_in_freq_range(ab, sbs_range,
7354 							freq_link1, freq_link2);
7355 }
7356 
7357 static bool ath12k_mac_freqs_on_same_mac(struct ath12k_base *ab,
7358 					 u32 freq_link1, u32 freq_link2)
7359 {
7360 	return ath12k_mac_2_freq_same_mac_in_dbs(ab, freq_link1, freq_link2) &&
7361 	       ath12k_mac_2_freq_same_mac_in_sbs(ab, freq_link1, freq_link2);
7362 }
7363 
7364 static int ath12k_mac_mlo_sta_set_link_active(struct ath12k_base *ab,
7365 					      enum wmi_mlo_link_force_reason reason,
7366 					      enum wmi_mlo_link_force_mode mode,
7367 					      u8 *mlo_vdev_id_lst,
7368 					      u8 num_mlo_vdev,
7369 					      u8 *mlo_inactive_vdev_lst,
7370 					      u8 num_mlo_inactive_vdev)
7371 {
7372 	struct wmi_mlo_link_set_active_arg param = {};
7373 	u32 entry_idx, entry_offset, vdev_idx;
7374 	u8 vdev_id;
7375 
7376 	param.reason = reason;
7377 	param.force_mode = mode;
7378 
7379 	for (vdev_idx = 0; vdev_idx < num_mlo_vdev; vdev_idx++) {
7380 		vdev_id = mlo_vdev_id_lst[vdev_idx];
7381 		entry_idx = vdev_id / 32;
7382 		entry_offset = vdev_id % 32;
7383 		if (entry_idx >= WMI_MLO_LINK_NUM_SZ) {
7384 			ath12k_warn(ab, "Invalid entry_idx %d num_mlo_vdev %d vdev %d",
7385 				    entry_idx, num_mlo_vdev, vdev_id);
7386 			return -EINVAL;
7387 		}
7388 		param.vdev_bitmap[entry_idx] |= 1 << entry_offset;
7389 		/* update entry number if entry index changed */
7390 		if (param.num_vdev_bitmap < entry_idx + 1)
7391 			param.num_vdev_bitmap = entry_idx + 1;
7392 	}
7393 
7394 	ath12k_dbg(ab, ATH12K_DBG_MAC,
7395 		   "num_vdev_bitmap %d vdev_bitmap[0] = 0x%x, vdev_bitmap[1] = 0x%x",
7396 		   param.num_vdev_bitmap, param.vdev_bitmap[0], param.vdev_bitmap[1]);
7397 
7398 	if (mode == WMI_MLO_LINK_FORCE_MODE_ACTIVE_INACTIVE) {
7399 		for (vdev_idx = 0; vdev_idx < num_mlo_inactive_vdev; vdev_idx++) {
7400 			vdev_id = mlo_inactive_vdev_lst[vdev_idx];
7401 			entry_idx = vdev_id / 32;
7402 			entry_offset = vdev_id % 32;
7403 			if (entry_idx >= WMI_MLO_LINK_NUM_SZ) {
7404 				ath12k_warn(ab, "Invalid entry_idx %d num_mlo_vdev %d vdev %d",
7405 					    entry_idx, num_mlo_inactive_vdev, vdev_id);
7406 				return -EINVAL;
7407 			}
7408 			param.inactive_vdev_bitmap[entry_idx] |= 1 << entry_offset;
7409 			/* update entry number if entry index changed */
7410 			if (param.num_inactive_vdev_bitmap < entry_idx + 1)
7411 				param.num_inactive_vdev_bitmap = entry_idx + 1;
7412 		}
7413 
7414 		ath12k_dbg(ab, ATH12K_DBG_MAC,
7415 			   "num_vdev_bitmap %d inactive_vdev_bitmap[0] = 0x%x, inactive_vdev_bitmap[1] = 0x%x",
7416 			   param.num_inactive_vdev_bitmap,
7417 			   param.inactive_vdev_bitmap[0],
7418 			   param.inactive_vdev_bitmap[1]);
7419 	}
7420 
7421 	if (mode == WMI_MLO_LINK_FORCE_MODE_ACTIVE_LINK_NUM ||
7422 	    mode == WMI_MLO_LINK_FORCE_MODE_INACTIVE_LINK_NUM) {
7423 		param.num_link_entry = 1;
7424 		param.link_num[0].num_of_link = num_mlo_vdev - 1;
7425 	}
7426 
7427 	return ath12k_wmi_send_mlo_link_set_active_cmd(ab, &param);
7428 }
7429 
7430 static int ath12k_mac_mlo_sta_update_link_active(struct ath12k_base *ab,
7431 						 struct ieee80211_hw *hw,
7432 						 struct ath12k_vif *ahvif)
7433 {
7434 	u8 mlo_vdev_id_lst[IEEE80211_MLD_MAX_NUM_LINKS] = {};
7435 	u32 mlo_freq_list[IEEE80211_MLD_MAX_NUM_LINKS] = {};
7436 	unsigned long links = ahvif->links_map;
7437 	enum wmi_mlo_link_force_reason reason;
7438 	struct ieee80211_chanctx_conf *conf;
7439 	enum wmi_mlo_link_force_mode mode;
7440 	struct ieee80211_bss_conf *info;
7441 	struct ath12k_link_vif *arvif;
7442 	u8 num_mlo_vdev = 0;
7443 	u8 link_id;
7444 
7445 	for_each_set_bit(link_id, &links, IEEE80211_MLD_MAX_NUM_LINKS) {
7446 		arvif = wiphy_dereference(hw->wiphy, ahvif->link[link_id]);
7447 		/* make sure vdev is created on this device */
7448 		if (!arvif || !arvif->is_created || arvif->ar->ab != ab)
7449 			continue;
7450 
7451 		info = ath12k_mac_get_link_bss_conf(arvif);
7452 		conf = wiphy_dereference(hw->wiphy, info->chanctx_conf);
7453 		mlo_freq_list[num_mlo_vdev] = conf->def.chan->center_freq;
7454 
7455 		mlo_vdev_id_lst[num_mlo_vdev] = arvif->vdev_id;
7456 		num_mlo_vdev++;
7457 	}
7458 
7459 	/* It is not allowed to activate more links than a single device
7460 	 * supported. Something goes wrong if we reach here.
7461 	 */
7462 	if (num_mlo_vdev > ATH12K_NUM_MAX_ACTIVE_LINKS_PER_DEVICE) {
7463 		WARN_ON_ONCE(1);
7464 		return -EINVAL;
7465 	}
7466 
7467 	/* if 2 links are established and both link channels fall on the
7468 	 * same hardware MAC, send command to firmware to deactivate one
7469 	 * of them.
7470 	 */
7471 	if (num_mlo_vdev == 2 &&
7472 	    ath12k_mac_freqs_on_same_mac(ab, mlo_freq_list[0],
7473 					 mlo_freq_list[1])) {
7474 		mode = WMI_MLO_LINK_FORCE_MODE_INACTIVE_LINK_NUM;
7475 		reason = WMI_MLO_LINK_FORCE_REASON_NEW_CONNECT;
7476 		return ath12k_mac_mlo_sta_set_link_active(ab, reason, mode,
7477 							  mlo_vdev_id_lst, num_mlo_vdev,
7478 							  NULL, 0);
7479 	}
7480 
7481 	return 0;
7482 }
7483 
7484 static bool ath12k_mac_are_sbs_chan(struct ath12k_base *ab, u32 freq_1, u32 freq_2)
7485 {
7486 	if (!ath12k_mac_is_hw_sbs_capable(ab))
7487 		return false;
7488 
7489 	if (ath12k_is_2ghz_channel_freq(freq_1) ||
7490 	    ath12k_is_2ghz_channel_freq(freq_2))
7491 		return false;
7492 
7493 	return !ath12k_mac_2_freq_same_mac_in_sbs(ab, freq_1, freq_2);
7494 }
7495 
7496 static bool ath12k_mac_are_dbs_chan(struct ath12k_base *ab, u32 freq_1, u32 freq_2)
7497 {
7498 	if (!ath12k_mac_is_hw_dbs_capable(ab))
7499 		return false;
7500 
7501 	return !ath12k_mac_2_freq_same_mac_in_dbs(ab, freq_1, freq_2);
7502 }
7503 
7504 static int ath12k_mac_select_links(struct ath12k_base *ab,
7505 				   struct ieee80211_vif *vif,
7506 				   struct ieee80211_hw *hw,
7507 				   u16 *selected_links)
7508 {
7509 	unsigned long useful_links = ieee80211_vif_usable_links(vif);
7510 	struct ath12k_vif *ahvif = ath12k_vif_to_ahvif(vif);
7511 	u8 num_useful_links = hweight_long(useful_links);
7512 	struct ieee80211_chanctx_conf *chanctx;
7513 	struct ath12k_link_vif *assoc_arvif;
7514 	u32 assoc_link_freq, partner_freq;
7515 	u16 sbs_links = 0, dbs_links = 0;
7516 	struct ieee80211_bss_conf *info;
7517 	struct ieee80211_channel *chan;
7518 	struct ieee80211_sta *sta;
7519 	struct ath12k_sta *ahsta;
7520 	u8 link_id;
7521 
7522 	/* activate all useful links if less than max supported */
7523 	if (num_useful_links <= ATH12K_NUM_MAX_ACTIVE_LINKS_PER_DEVICE) {
7524 		*selected_links = useful_links;
7525 		return 0;
7526 	}
7527 
7528 	/* only in station mode we can get here, so it's safe
7529 	 * to use ap_addr
7530 	 */
7531 	rcu_read_lock();
7532 	sta = ieee80211_find_sta(vif, vif->cfg.ap_addr);
7533 	if (!sta) {
7534 		rcu_read_unlock();
7535 		ath12k_warn(ab, "failed to find sta with addr %pM\n", vif->cfg.ap_addr);
7536 		return -EINVAL;
7537 	}
7538 
7539 	ahsta = ath12k_sta_to_ahsta(sta);
7540 	assoc_arvif = wiphy_dereference(hw->wiphy, ahvif->link[ahsta->assoc_link_id]);
7541 	info = ath12k_mac_get_link_bss_conf(assoc_arvif);
7542 	chanctx = rcu_dereference(info->chanctx_conf);
7543 	assoc_link_freq = chanctx->def.chan->center_freq;
7544 	rcu_read_unlock();
7545 	ath12k_dbg(ab, ATH12K_DBG_MAC, "assoc link %u freq %u\n",
7546 		   assoc_arvif->link_id, assoc_link_freq);
7547 
7548 	/* assoc link is already activated and has to be kept active,
7549 	 * only need to select a partner link from others.
7550 	 */
7551 	useful_links &= ~BIT(assoc_arvif->link_id);
7552 	for_each_set_bit(link_id, &useful_links, IEEE80211_MLD_MAX_NUM_LINKS) {
7553 		info = wiphy_dereference(hw->wiphy, vif->link_conf[link_id]);
7554 		if (!info) {
7555 			ath12k_warn(ab, "failed to get link info for link: %u\n",
7556 				    link_id);
7557 			return -ENOLINK;
7558 		}
7559 
7560 		chan = info->chanreq.oper.chan;
7561 		if (!chan) {
7562 			ath12k_warn(ab, "failed to get chan for link: %u\n", link_id);
7563 			return -EINVAL;
7564 		}
7565 
7566 		partner_freq = chan->center_freq;
7567 		if (ath12k_mac_are_sbs_chan(ab, assoc_link_freq, partner_freq)) {
7568 			sbs_links |= BIT(link_id);
7569 			ath12k_dbg(ab, ATH12K_DBG_MAC, "new SBS link %u freq %u\n",
7570 				   link_id, partner_freq);
7571 			continue;
7572 		}
7573 
7574 		if (ath12k_mac_are_dbs_chan(ab, assoc_link_freq, partner_freq)) {
7575 			dbs_links |= BIT(link_id);
7576 			ath12k_dbg(ab, ATH12K_DBG_MAC, "new DBS link %u freq %u\n",
7577 				   link_id, partner_freq);
7578 			continue;
7579 		}
7580 
7581 		ath12k_dbg(ab, ATH12K_DBG_MAC, "non DBS/SBS link %u freq %u\n",
7582 			   link_id, partner_freq);
7583 	}
7584 
7585 	/* choose the first candidate no matter how many is in the list */
7586 	if (sbs_links)
7587 		link_id = __ffs(sbs_links);
7588 	else if (dbs_links)
7589 		link_id = __ffs(dbs_links);
7590 	else
7591 		link_id = ffs(useful_links) - 1;
7592 
7593 	ath12k_dbg(ab, ATH12K_DBG_MAC, "select partner link %u\n", link_id);
7594 
7595 	*selected_links = BIT(assoc_arvif->link_id) | BIT(link_id);
7596 
7597 	return 0;
7598 }
7599 
7600 int ath12k_mac_op_sta_state(struct ieee80211_hw *hw,
7601 			    struct ieee80211_vif *vif,
7602 			    struct ieee80211_sta *sta,
7603 			    enum ieee80211_sta_state old_state,
7604 			    enum ieee80211_sta_state new_state)
7605 {
7606 	struct ath12k_vif *ahvif = ath12k_vif_to_ahvif(vif);
7607 	struct ath12k_sta *ahsta = ath12k_sta_to_ahsta(sta);
7608 	struct ath12k_hw *ah = ath12k_hw_to_ah(hw);
7609 	struct ath12k_base *prev_ab = NULL, *ab;
7610 	struct ath12k_link_vif *arvif;
7611 	struct ath12k_link_sta *arsta;
7612 	unsigned long valid_links;
7613 	u16 selected_links = 0;
7614 	u8 link_id = 0, i;
7615 	struct ath12k *ar;
7616 	int ret = -EINVAL;
7617 	struct ath12k_dp_peer_create_params dp_params = {};
7618 
7619 	lockdep_assert_wiphy(hw->wiphy);
7620 
7621 	if (ieee80211_vif_is_mld(vif) && sta->valid_links) {
7622 		WARN_ON(!sta->mlo && hweight16(sta->valid_links) != 1);
7623 		link_id = ffs(sta->valid_links) - 1;
7624 	}
7625 
7626 	/* IEEE80211_STA_NOTEXIST -> IEEE80211_STA_NONE:
7627 	 * New station add received. If this is a ML station then
7628 	 * ahsta->links_map will be zero and sta->valid_links will be 1.
7629 	 * Assign default link to the first link sta.
7630 	 */
7631 	if (old_state == IEEE80211_STA_NOTEXIST &&
7632 	    new_state == IEEE80211_STA_NONE) {
7633 		memset(ahsta, 0, sizeof(*ahsta));
7634 
7635 		arsta = &ahsta->deflink;
7636 
7637 		/* ML sta */
7638 		if (sta->mlo && !ahsta->links_map &&
7639 		    (hweight16(sta->valid_links) == 1)) {
7640 			ahsta->ml_peer_id = ath12k_peer_ml_alloc(ah);
7641 			if (ahsta->ml_peer_id == ATH12K_MLO_PEER_ID_INVALID) {
7642 				ath12k_hw_warn(ah, "unable to allocate ML peer id for sta %pM",
7643 					       sta->addr);
7644 				goto exit;
7645 			}
7646 
7647 			dp_params.is_mlo = true;
7648 			dp_params.peer_id = ahsta->ml_peer_id | ATH12K_PEER_ML_ID_VALID;
7649 		}
7650 
7651 		dp_params.sta = sta;
7652 
7653 		if (vif->type == NL80211_IFTYPE_AP)
7654 			dp_params.ucast_ra_only = true;
7655 
7656 		ret = ath12k_dp_peer_create(&ah->dp_hw, sta->addr, &dp_params);
7657 		if (ret) {
7658 			ath12k_hw_warn(ah, "unable to create ath12k_dp_peer for sta %pM, ret: %d",
7659 				       sta->addr, ret);
7660 
7661 			goto ml_peer_id_clear;
7662 		}
7663 
7664 		ret = ath12k_mac_assign_link_sta(ah, ahsta, arsta, ahvif,
7665 						 link_id);
7666 		if (ret) {
7667 			ath12k_hw_warn(ah, "unable assign link %d for sta %pM",
7668 				       link_id, sta->addr);
7669 			goto peer_delete;
7670 		}
7671 
7672 		/* above arsta will get memset, hence do this after assign
7673 		 * link sta
7674 		 */
7675 		if (sta->mlo) {
7676 			/* For station mode, arvif->is_sta_assoc_link has been set when
7677 			 * vdev starts. Make sure the arvif/arsta pair have same setting
7678 			 */
7679 			if (vif->type == NL80211_IFTYPE_STATION &&
7680 			    !arsta->arvif->is_sta_assoc_link) {
7681 				ath12k_hw_warn(ah, "failed to verify assoc link setting with link id %u\n",
7682 					       link_id);
7683 				ret = -EINVAL;
7684 				goto exit;
7685 			}
7686 
7687 			arsta->is_assoc_link = true;
7688 			ahsta->assoc_link_id = link_id;
7689 		}
7690 	}
7691 
7692 	/* In the ML station scenario, activate all partner links once the
7693 	 * client is transitioning to the associated state.
7694 	 *
7695 	 * FIXME: Ideally, this activation should occur when the client
7696 	 * transitions to the authorized state. However, there are some
7697 	 * issues with handling this in the firmware. Until the firmware
7698 	 * can manage it properly, activate the links when the client is
7699 	 * about to move to the associated state.
7700 	 */
7701 	if (ieee80211_vif_is_mld(vif) && vif->type == NL80211_IFTYPE_STATION &&
7702 	    old_state == IEEE80211_STA_AUTH && new_state == IEEE80211_STA_ASSOC) {
7703 		/* TODO: for now only do link selection for single device
7704 		 * MLO case. Other cases would be handled in the future.
7705 		 */
7706 		ab = ah->radio[0].ab;
7707 		if (ab->ag->num_devices == 1) {
7708 			ret = ath12k_mac_select_links(ab, vif, hw, &selected_links);
7709 			if (ret) {
7710 				ath12k_warn(ab,
7711 					    "failed to get selected links: %d\n", ret);
7712 				goto exit;
7713 			}
7714 		} else {
7715 			selected_links = ieee80211_vif_usable_links(vif);
7716 		}
7717 
7718 		ieee80211_set_active_links(vif, selected_links);
7719 	}
7720 
7721 	/* Handle all the other state transitions in generic way */
7722 	valid_links = ahsta->links_map;
7723 	for_each_set_bit(link_id, &valid_links, IEEE80211_MLD_MAX_NUM_LINKS) {
7724 		arvif = wiphy_dereference(hw->wiphy, ahvif->link[link_id]);
7725 		arsta = wiphy_dereference(hw->wiphy, ahsta->link[link_id]);
7726 		/* some assumptions went wrong! */
7727 		if (WARN_ON(!arvif || !arsta))
7728 			continue;
7729 
7730 		/* vdev might be in deleted */
7731 		if (WARN_ON(!arvif->ar))
7732 			continue;
7733 
7734 		ret = ath12k_mac_handle_link_sta_state(hw, arvif, arsta,
7735 						       old_state, new_state);
7736 		if (ret) {
7737 			ath12k_hw_warn(ah, "unable to move link sta %d of sta %pM from state %d to %d",
7738 				       link_id, arsta->addr, old_state, new_state);
7739 
7740 			if (old_state == IEEE80211_STA_NOTEXIST &&
7741 			    new_state == IEEE80211_STA_NONE)
7742 				goto peer_delete;
7743 			else
7744 				goto exit;
7745 		}
7746 	}
7747 
7748 	if (ieee80211_vif_is_mld(vif) && vif->type == NL80211_IFTYPE_STATION &&
7749 	    old_state == IEEE80211_STA_ASSOC && new_state == IEEE80211_STA_AUTHORIZED) {
7750 		for_each_ar(ah, ar, i) {
7751 			ab = ar->ab;
7752 			if (prev_ab == ab)
7753 				continue;
7754 
7755 			ret = ath12k_mac_mlo_sta_update_link_active(ab, hw, ahvif);
7756 			if (ret) {
7757 				ath12k_warn(ab,
7758 					    "failed to update link active state on connect %d\n",
7759 					    ret);
7760 				goto exit;
7761 			}
7762 
7763 			prev_ab = ab;
7764 		}
7765 	}
7766 	/* IEEE80211_STA_NONE -> IEEE80211_STA_NOTEXIST:
7767 	 * Remove the station from driver (handle ML sta here since that
7768 	 * needs special handling. Normal sta will be handled in generic
7769 	 * handler below
7770 	 */
7771 	if (old_state == IEEE80211_STA_NONE &&
7772 	    new_state == IEEE80211_STA_NOTEXIST) {
7773 		if (sta->mlo)
7774 			ath12k_mac_ml_station_remove(ahvif, ahsta);
7775 
7776 		ath12k_dp_peer_delete(&ah->dp_hw, sta->addr, sta);
7777 	}
7778 
7779 	ret = 0;
7780 	goto exit;
7781 
7782 peer_delete:
7783 	ath12k_dp_peer_delete(&ah->dp_hw, sta->addr, sta);
7784 ml_peer_id_clear:
7785 	if (sta->mlo) {
7786 		clear_bit(ahsta->ml_peer_id, ah->free_ml_peer_id_map);
7787 		ahsta->ml_peer_id = ATH12K_MLO_PEER_ID_INVALID;
7788 	}
7789 exit:
7790 	/* update the state if everything went well */
7791 	if (!ret)
7792 		ahsta->state = new_state;
7793 
7794 	return ret;
7795 }
7796 EXPORT_SYMBOL(ath12k_mac_op_sta_state);
7797 
7798 int ath12k_mac_op_sta_set_txpwr(struct ieee80211_hw *hw,
7799 				struct ieee80211_vif *vif,
7800 				struct ieee80211_sta *sta)
7801 {
7802 	struct ath12k_sta *ahsta = ath12k_sta_to_ahsta(sta);
7803 	struct ath12k *ar;
7804 	struct ath12k_vif *ahvif = ath12k_vif_to_ahvif(vif);
7805 	struct ath12k_link_vif *arvif;
7806 	struct ath12k_link_sta *arsta;
7807 	u8 link_id;
7808 	int ret;
7809 	s16 txpwr;
7810 
7811 	lockdep_assert_wiphy(hw->wiphy);
7812 
7813 	/* TODO: use link id from mac80211 once that's implemented */
7814 	link_id = 0;
7815 
7816 	arvif = wiphy_dereference(hw->wiphy, ahvif->link[link_id]);
7817 	arsta = wiphy_dereference(hw->wiphy, ahsta->link[link_id]);
7818 
7819 	if (sta->deflink.txpwr.type == NL80211_TX_POWER_AUTOMATIC) {
7820 		txpwr = 0;
7821 	} else {
7822 		txpwr = sta->deflink.txpwr.power;
7823 		if (!txpwr) {
7824 			ret = -EINVAL;
7825 			goto out;
7826 		}
7827 	}
7828 
7829 	if (txpwr > ATH12K_TX_POWER_MAX_VAL || txpwr < ATH12K_TX_POWER_MIN_VAL) {
7830 		ret = -EINVAL;
7831 		goto out;
7832 	}
7833 
7834 	ar = arvif->ar;
7835 
7836 	ret = ath12k_wmi_set_peer_param(ar, arsta->addr, arvif->vdev_id,
7837 					WMI_PEER_USE_FIXED_PWR, txpwr);
7838 	if (ret) {
7839 		ath12k_warn(ar->ab, "failed to set tx power for station ret: %d\n",
7840 			    ret);
7841 		goto out;
7842 	}
7843 
7844 out:
7845 	return ret;
7846 }
7847 EXPORT_SYMBOL(ath12k_mac_op_sta_set_txpwr);
7848 
7849 void ath12k_mac_op_link_sta_rc_update(struct ieee80211_hw *hw,
7850 				      struct ieee80211_vif *vif,
7851 				      struct ieee80211_link_sta *link_sta,
7852 				      u32 changed)
7853 {
7854 	struct ieee80211_sta *sta = link_sta->sta;
7855 	struct ath12k *ar;
7856 	struct ath12k_sta *ahsta = ath12k_sta_to_ahsta(sta);
7857 	struct ath12k_vif *ahvif = ath12k_vif_to_ahvif(vif);
7858 	struct ath12k_hw *ah = ath12k_hw_to_ah(hw);
7859 	struct ath12k_link_sta *arsta;
7860 	struct ath12k_link_vif *arvif;
7861 	struct ath12k_dp_link_peer *peer;
7862 	u32 bw, smps;
7863 	struct ath12k_dp *dp;
7864 
7865 	rcu_read_lock();
7866 	arvif = rcu_dereference(ahvif->link[link_sta->link_id]);
7867 	if (!arvif) {
7868 		ath12k_hw_warn(ah, "mac sta rc update failed to fetch link vif on link id %u for peer %pM\n",
7869 			       link_sta->link_id, sta->addr);
7870 		rcu_read_unlock();
7871 		return;
7872 	}
7873 
7874 	ar = arvif->ar;
7875 	dp = ath12k_ab_to_dp(ar->ab);
7876 
7877 	arsta = rcu_dereference(ahsta->link[link_sta->link_id]);
7878 	if (!arsta) {
7879 		rcu_read_unlock();
7880 		ath12k_warn(ar->ab, "mac sta rc update failed to fetch link sta on link id %u for peer %pM\n",
7881 			    link_sta->link_id, sta->addr);
7882 		return;
7883 	}
7884 	spin_lock_bh(&dp->dp_lock);
7885 
7886 	peer = ath12k_dp_link_peer_find_by_vdev_and_addr(dp, arvif->vdev_id,
7887 							 arsta->addr);
7888 	if (!peer) {
7889 		spin_unlock_bh(&dp->dp_lock);
7890 		rcu_read_unlock();
7891 		ath12k_warn(ar->ab, "mac sta rc update failed to find peer %pM on vdev %i\n",
7892 			    arsta->addr, arvif->vdev_id);
7893 		return;
7894 	}
7895 
7896 	spin_unlock_bh(&dp->dp_lock);
7897 
7898 	if (arsta->link_id >= IEEE80211_MLD_MAX_NUM_LINKS) {
7899 		rcu_read_unlock();
7900 		return;
7901 	}
7902 
7903 	link_sta = rcu_dereference(sta->link[arsta->link_id]);
7904 	if (!link_sta) {
7905 		rcu_read_unlock();
7906 		ath12k_warn(ar->ab, "unable to access link sta in rc update for sta %pM link %u\n",
7907 			    sta->addr, arsta->link_id);
7908 		return;
7909 	}
7910 
7911 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
7912 		   "mac sta rc update for %pM changed %08x bw %d nss %d smps %d\n",
7913 		   arsta->addr, changed, link_sta->bandwidth, link_sta->rx_nss,
7914 		   link_sta->smps_mode);
7915 
7916 	spin_lock_bh(&ar->data_lock);
7917 
7918 	if (changed & IEEE80211_RC_BW_CHANGED) {
7919 		bw = ath12k_mac_ieee80211_sta_bw_to_wmi(ar, link_sta);
7920 		arsta->bw_prev = arsta->bw;
7921 		arsta->bw = bw;
7922 	}
7923 
7924 	if (changed & IEEE80211_RC_NSS_CHANGED)
7925 		arsta->nss = link_sta->rx_nss;
7926 
7927 	if (changed & IEEE80211_RC_SMPS_CHANGED) {
7928 		smps = WMI_PEER_SMPS_PS_NONE;
7929 
7930 		switch (link_sta->smps_mode) {
7931 		case IEEE80211_SMPS_AUTOMATIC:
7932 		case IEEE80211_SMPS_OFF:
7933 			smps = WMI_PEER_SMPS_PS_NONE;
7934 			break;
7935 		case IEEE80211_SMPS_STATIC:
7936 			smps = WMI_PEER_SMPS_STATIC;
7937 			break;
7938 		case IEEE80211_SMPS_DYNAMIC:
7939 			smps = WMI_PEER_SMPS_DYNAMIC;
7940 			break;
7941 		default:
7942 			ath12k_warn(ar->ab, "Invalid smps %d in sta rc update for %pM link %u\n",
7943 				    link_sta->smps_mode, arsta->addr, link_sta->link_id);
7944 			smps = WMI_PEER_SMPS_PS_NONE;
7945 			break;
7946 		}
7947 
7948 		arsta->smps = smps;
7949 	}
7950 
7951 	arsta->changed |= changed;
7952 
7953 	spin_unlock_bh(&ar->data_lock);
7954 
7955 	wiphy_work_queue(hw->wiphy, &arsta->update_wk);
7956 
7957 	rcu_read_unlock();
7958 }
7959 EXPORT_SYMBOL(ath12k_mac_op_link_sta_rc_update);
7960 
7961 static struct ath12k_link_sta *ath12k_mac_alloc_assign_link_sta(struct ath12k_hw *ah,
7962 								struct ath12k_sta *ahsta,
7963 								struct ath12k_vif *ahvif,
7964 								u8 link_id)
7965 {
7966 	struct ath12k_link_sta *arsta;
7967 	int ret;
7968 
7969 	lockdep_assert_wiphy(ah->hw->wiphy);
7970 
7971 	if (link_id >= IEEE80211_MLD_MAX_NUM_LINKS)
7972 		return NULL;
7973 
7974 	arsta = wiphy_dereference(ah->hw->wiphy, ahsta->link[link_id]);
7975 	if (arsta)
7976 		return NULL;
7977 
7978 	arsta = kmalloc_obj(*arsta);
7979 	if (!arsta)
7980 		return NULL;
7981 
7982 	ret = ath12k_mac_assign_link_sta(ah, ahsta, arsta, ahvif, link_id);
7983 	if (ret) {
7984 		kfree(arsta);
7985 		return NULL;
7986 	}
7987 
7988 	return arsta;
7989 }
7990 
7991 int ath12k_mac_op_change_sta_links(struct ieee80211_hw *hw,
7992 				   struct ieee80211_vif *vif,
7993 				   struct ieee80211_sta *sta,
7994 				   u16 old_links, u16 new_links)
7995 {
7996 	struct ath12k_vif *ahvif = ath12k_vif_to_ahvif(vif);
7997 	struct ath12k_sta *ahsta = ath12k_sta_to_ahsta(sta);
7998 	struct ath12k_hw *ah = hw->priv;
7999 	struct ath12k_link_vif *arvif;
8000 	struct ath12k_link_sta *arsta;
8001 	unsigned long valid_links;
8002 	struct ath12k *ar;
8003 	u8 link_id;
8004 	int ret;
8005 
8006 	lockdep_assert_wiphy(hw->wiphy);
8007 
8008 	if (!sta->valid_links)
8009 		return -EINVAL;
8010 
8011 	/* Firmware does not support removal of one of link stas. All sta
8012 	 * would be removed during ML STA delete in sta_state(), hence link
8013 	 * sta removal is not handled here.
8014 	 */
8015 	if (new_links < old_links)
8016 		return 0;
8017 
8018 	if (ahsta->ml_peer_id == ATH12K_MLO_PEER_ID_INVALID) {
8019 		ath12k_hw_warn(ah, "unable to add link for ml sta %pM", sta->addr);
8020 		return -EINVAL;
8021 	}
8022 
8023 	/* this op is expected only after initial sta insertion with default link */
8024 	if (WARN_ON(ahsta->links_map == 0))
8025 		return -EINVAL;
8026 
8027 	valid_links = new_links;
8028 	for_each_set_bit(link_id, &valid_links, IEEE80211_MLD_MAX_NUM_LINKS) {
8029 		if (ahsta->links_map & BIT(link_id))
8030 			continue;
8031 
8032 		arvif = wiphy_dereference(hw->wiphy, ahvif->link[link_id]);
8033 		arsta = ath12k_mac_alloc_assign_link_sta(ah, ahsta, ahvif, link_id);
8034 
8035 		if (!arvif || !arsta) {
8036 			ath12k_hw_warn(ah, "Failed to alloc/assign link sta");
8037 			continue;
8038 		}
8039 
8040 		ar = arvif->ar;
8041 		if (!ar)
8042 			continue;
8043 
8044 		ret = ath12k_mac_station_add(ar, arvif, arsta);
8045 		if (ret) {
8046 			ath12k_warn(ar->ab, "Failed to add station: %pM for VDEV: %d\n",
8047 				    arsta->addr, arvif->vdev_id);
8048 			ath12k_mac_free_unassign_link_sta(ah, ahsta, link_id);
8049 			return ret;
8050 		}
8051 	}
8052 
8053 	return 0;
8054 }
8055 EXPORT_SYMBOL(ath12k_mac_op_change_sta_links);
8056 
8057 bool ath12k_mac_op_can_activate_links(struct ieee80211_hw *hw,
8058 				      struct ieee80211_vif *vif,
8059 				      u16 active_links)
8060 {
8061 	/* TODO: Handle recovery case */
8062 
8063 	return true;
8064 }
8065 EXPORT_SYMBOL(ath12k_mac_op_can_activate_links);
8066 
8067 static int ath12k_conf_tx_uapsd(struct ath12k_link_vif *arvif,
8068 				u16 ac, bool enable)
8069 {
8070 	struct ath12k *ar = arvif->ar;
8071 	struct ath12k_vif *ahvif = arvif->ahvif;
8072 	u32 value;
8073 	int ret;
8074 
8075 	if (ahvif->vdev_type != WMI_VDEV_TYPE_STA)
8076 		return 0;
8077 
8078 	switch (ac) {
8079 	case IEEE80211_AC_VO:
8080 		value = WMI_STA_PS_UAPSD_AC3_DELIVERY_EN |
8081 			WMI_STA_PS_UAPSD_AC3_TRIGGER_EN;
8082 		break;
8083 	case IEEE80211_AC_VI:
8084 		value = WMI_STA_PS_UAPSD_AC2_DELIVERY_EN |
8085 			WMI_STA_PS_UAPSD_AC2_TRIGGER_EN;
8086 		break;
8087 	case IEEE80211_AC_BE:
8088 		value = WMI_STA_PS_UAPSD_AC1_DELIVERY_EN |
8089 			WMI_STA_PS_UAPSD_AC1_TRIGGER_EN;
8090 		break;
8091 	case IEEE80211_AC_BK:
8092 		value = WMI_STA_PS_UAPSD_AC0_DELIVERY_EN |
8093 			WMI_STA_PS_UAPSD_AC0_TRIGGER_EN;
8094 		break;
8095 	}
8096 
8097 	if (enable)
8098 		ahvif->u.sta.uapsd |= value;
8099 	else
8100 		ahvif->u.sta.uapsd &= ~value;
8101 
8102 	ret = ath12k_wmi_set_sta_ps_param(ar, arvif->vdev_id,
8103 					  WMI_STA_PS_PARAM_UAPSD,
8104 					  ahvif->u.sta.uapsd);
8105 	if (ret) {
8106 		ath12k_warn(ar->ab, "could not set uapsd params %d\n", ret);
8107 		goto exit;
8108 	}
8109 
8110 	if (ahvif->u.sta.uapsd)
8111 		value = WMI_STA_PS_RX_WAKE_POLICY_POLL_UAPSD;
8112 	else
8113 		value = WMI_STA_PS_RX_WAKE_POLICY_WAKE;
8114 
8115 	ret = ath12k_wmi_set_sta_ps_param(ar, arvif->vdev_id,
8116 					  WMI_STA_PS_PARAM_RX_WAKE_POLICY,
8117 					  value);
8118 	if (ret)
8119 		ath12k_warn(ar->ab, "could not set rx wake param %d\n", ret);
8120 
8121 exit:
8122 	return ret;
8123 }
8124 
8125 static int ath12k_mac_conf_tx(struct ath12k_link_vif *arvif, u16 ac,
8126 			      const struct ieee80211_tx_queue_params *params)
8127 {
8128 	struct wmi_wmm_params_arg *p = NULL;
8129 	struct ath12k *ar = arvif->ar;
8130 	struct ath12k_base *ab = ar->ab;
8131 	int ret;
8132 
8133 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
8134 
8135 	switch (ac) {
8136 	case IEEE80211_AC_VO:
8137 		p = &arvif->wmm_params.ac_vo;
8138 		break;
8139 	case IEEE80211_AC_VI:
8140 		p = &arvif->wmm_params.ac_vi;
8141 		break;
8142 	case IEEE80211_AC_BE:
8143 		p = &arvif->wmm_params.ac_be;
8144 		break;
8145 	case IEEE80211_AC_BK:
8146 		p = &arvif->wmm_params.ac_bk;
8147 		break;
8148 	}
8149 
8150 	if (WARN_ON(!p)) {
8151 		ret = -EINVAL;
8152 		goto exit;
8153 	}
8154 
8155 	p->cwmin = params->cw_min;
8156 	p->cwmax = params->cw_max;
8157 	p->aifs = params->aifs;
8158 	p->txop = params->txop;
8159 
8160 	ret = ath12k_wmi_send_wmm_update_cmd(ar, arvif->vdev_id,
8161 					     &arvif->wmm_params);
8162 	if (ret) {
8163 		ath12k_warn(ab, "pdev idx %d failed to set wmm params: %d\n",
8164 			    ar->pdev_idx, ret);
8165 		goto exit;
8166 	}
8167 
8168 	ret = ath12k_conf_tx_uapsd(arvif, ac, params->uapsd);
8169 	if (ret)
8170 		ath12k_warn(ab, "pdev idx %d failed to set sta uapsd: %d\n",
8171 			    ar->pdev_idx, ret);
8172 
8173 exit:
8174 	return ret;
8175 }
8176 
8177 int ath12k_mac_op_conf_tx(struct ieee80211_hw *hw,
8178 			  struct ieee80211_vif *vif,
8179 			  unsigned int link_id, u16 ac,
8180 			  const struct ieee80211_tx_queue_params *params)
8181 {
8182 	struct ath12k_vif *ahvif = ath12k_vif_to_ahvif(vif);
8183 	struct ath12k_link_vif *arvif;
8184 	struct ath12k_vif_cache *cache;
8185 	int ret;
8186 
8187 	lockdep_assert_wiphy(hw->wiphy);
8188 
8189 	if (link_id >= IEEE80211_MLD_MAX_NUM_LINKS)
8190 		return -EINVAL;
8191 
8192 	arvif = wiphy_dereference(hw->wiphy, ahvif->link[link_id]);
8193 	if (!arvif || !arvif->is_created) {
8194 		cache = ath12k_ahvif_get_link_cache(ahvif, link_id);
8195 		if (!cache)
8196 			return -ENOSPC;
8197 
8198 		cache->tx_conf.changed = true;
8199 		cache->tx_conf.ac = ac;
8200 		cache->tx_conf.tx_queue_params = *params;
8201 
8202 		return 0;
8203 	}
8204 
8205 	ret = ath12k_mac_conf_tx(arvif, ac, params);
8206 
8207 	return ret;
8208 }
8209 EXPORT_SYMBOL(ath12k_mac_op_conf_tx);
8210 
8211 static struct ieee80211_sta_ht_cap
8212 ath12k_create_ht_cap(struct ath12k *ar, u32 ar_ht_cap, u32 rate_cap_rx_chainmask)
8213 {
8214 	int i;
8215 	struct ieee80211_sta_ht_cap ht_cap = {};
8216 	u32 ar_vht_cap = ar->pdev->cap.vht_cap;
8217 
8218 	if (!(ar_ht_cap & WMI_HT_CAP_ENABLED))
8219 		return ht_cap;
8220 
8221 	ht_cap.ht_supported = 1;
8222 	ht_cap.ampdu_factor = IEEE80211_HT_MAX_AMPDU_64K;
8223 	ht_cap.ampdu_density = IEEE80211_HT_MPDU_DENSITY_NONE;
8224 	ht_cap.cap |= IEEE80211_HT_CAP_SUP_WIDTH_20_40;
8225 	ht_cap.cap |= IEEE80211_HT_CAP_DSSSCCK40;
8226 	ht_cap.cap |= WLAN_HT_CAP_SM_PS_STATIC << IEEE80211_HT_CAP_SM_PS_SHIFT;
8227 
8228 	if (ar_ht_cap & WMI_HT_CAP_HT20_SGI)
8229 		ht_cap.cap |= IEEE80211_HT_CAP_SGI_20;
8230 
8231 	if (ar_ht_cap & WMI_HT_CAP_HT40_SGI)
8232 		ht_cap.cap |= IEEE80211_HT_CAP_SGI_40;
8233 
8234 	if (ar_ht_cap & WMI_HT_CAP_DYNAMIC_SMPS) {
8235 		u32 smps;
8236 
8237 		smps   = WLAN_HT_CAP_SM_PS_DYNAMIC;
8238 		smps <<= IEEE80211_HT_CAP_SM_PS_SHIFT;
8239 
8240 		ht_cap.cap |= smps;
8241 	}
8242 
8243 	if (ar_ht_cap & WMI_HT_CAP_TX_STBC)
8244 		ht_cap.cap |= IEEE80211_HT_CAP_TX_STBC;
8245 
8246 	if (ar_ht_cap & WMI_HT_CAP_RX_STBC) {
8247 		u32 stbc;
8248 
8249 		stbc   = ar_ht_cap;
8250 		stbc  &= WMI_HT_CAP_RX_STBC;
8251 		stbc >>= WMI_HT_CAP_RX_STBC_MASK_SHIFT;
8252 		stbc <<= IEEE80211_HT_CAP_RX_STBC_SHIFT;
8253 		stbc  &= IEEE80211_HT_CAP_RX_STBC;
8254 
8255 		ht_cap.cap |= stbc;
8256 	}
8257 
8258 	if (ar_ht_cap & WMI_HT_CAP_RX_LDPC)
8259 		ht_cap.cap |= IEEE80211_HT_CAP_LDPC_CODING;
8260 
8261 	if (ar_ht_cap & WMI_HT_CAP_L_SIG_TXOP_PROT)
8262 		ht_cap.cap |= IEEE80211_HT_CAP_LSIG_TXOP_PROT;
8263 
8264 	if (ar_vht_cap & WMI_VHT_CAP_MAX_MPDU_LEN_MASK)
8265 		ht_cap.cap |= IEEE80211_HT_CAP_MAX_AMSDU;
8266 
8267 	for (i = 0; i < ar->num_rx_chains; i++) {
8268 		if (rate_cap_rx_chainmask & BIT(i))
8269 			ht_cap.mcs.rx_mask[i] = 0xFF;
8270 	}
8271 
8272 	ht_cap.mcs.tx_params |= IEEE80211_HT_MCS_TX_DEFINED;
8273 
8274 	return ht_cap;
8275 }
8276 
8277 static int ath12k_mac_set_txbf_conf(struct ath12k_link_vif *arvif)
8278 {
8279 	u32 value = 0;
8280 	struct ath12k *ar = arvif->ar;
8281 	struct ath12k_vif *ahvif = arvif->ahvif;
8282 	int nsts;
8283 	int sound_dim;
8284 	u32 vht_cap = ar->pdev->cap.vht_cap;
8285 	u32 vdev_param = WMI_VDEV_PARAM_TXBF;
8286 
8287 	if (vht_cap & (IEEE80211_VHT_CAP_SU_BEAMFORMEE_CAPABLE)) {
8288 		nsts = vht_cap & IEEE80211_VHT_CAP_BEAMFORMEE_STS_MASK;
8289 		nsts >>= IEEE80211_VHT_CAP_BEAMFORMEE_STS_SHIFT;
8290 		value |= SM(nsts, WMI_TXBF_STS_CAP_OFFSET);
8291 	}
8292 
8293 	if (vht_cap & (IEEE80211_VHT_CAP_SU_BEAMFORMER_CAPABLE)) {
8294 		sound_dim = vht_cap &
8295 			    IEEE80211_VHT_CAP_SOUNDING_DIMENSIONS_MASK;
8296 		sound_dim >>= IEEE80211_VHT_CAP_SOUNDING_DIMENSIONS_SHIFT;
8297 		if (sound_dim > (ar->num_tx_chains - 1))
8298 			sound_dim = ar->num_tx_chains - 1;
8299 		value |= SM(sound_dim, WMI_BF_SOUND_DIM_OFFSET);
8300 	}
8301 
8302 	if (!value)
8303 		return 0;
8304 
8305 	if (vht_cap & IEEE80211_VHT_CAP_SU_BEAMFORMER_CAPABLE) {
8306 		value |= WMI_VDEV_PARAM_TXBF_SU_TX_BFER;
8307 
8308 		if ((vht_cap & IEEE80211_VHT_CAP_MU_BEAMFORMER_CAPABLE) &&
8309 		    ahvif->vdev_type == WMI_VDEV_TYPE_AP)
8310 			value |= WMI_VDEV_PARAM_TXBF_MU_TX_BFER;
8311 	}
8312 
8313 	if (vht_cap & IEEE80211_VHT_CAP_SU_BEAMFORMEE_CAPABLE) {
8314 		value |= WMI_VDEV_PARAM_TXBF_SU_TX_BFEE;
8315 
8316 		if ((vht_cap & IEEE80211_VHT_CAP_MU_BEAMFORMEE_CAPABLE) &&
8317 		    ahvif->vdev_type == WMI_VDEV_TYPE_STA)
8318 			value |= WMI_VDEV_PARAM_TXBF_MU_TX_BFEE;
8319 	}
8320 
8321 	return ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
8322 					     vdev_param, value);
8323 }
8324 
8325 static void ath12k_set_vht_txbf_cap(struct ath12k *ar, u32 *vht_cap)
8326 {
8327 	bool subfer, subfee;
8328 	int sound_dim = 0;
8329 
8330 	subfer = !!(*vht_cap & (IEEE80211_VHT_CAP_SU_BEAMFORMER_CAPABLE));
8331 	subfee = !!(*vht_cap & (IEEE80211_VHT_CAP_SU_BEAMFORMEE_CAPABLE));
8332 
8333 	if (ar->num_tx_chains < 2) {
8334 		*vht_cap &= ~(IEEE80211_VHT_CAP_SU_BEAMFORMER_CAPABLE);
8335 		subfer = false;
8336 	}
8337 
8338 	/* If SU Beaformer is not set, then disable MU Beamformer Capability */
8339 	if (!subfer)
8340 		*vht_cap &= ~(IEEE80211_VHT_CAP_MU_BEAMFORMER_CAPABLE);
8341 
8342 	/* If SU Beaformee is not set, then disable MU Beamformee Capability */
8343 	if (!subfee)
8344 		*vht_cap &= ~(IEEE80211_VHT_CAP_MU_BEAMFORMEE_CAPABLE);
8345 
8346 	sound_dim = u32_get_bits(*vht_cap,
8347 				 IEEE80211_VHT_CAP_SOUNDING_DIMENSIONS_MASK);
8348 	*vht_cap = u32_replace_bits(*vht_cap, 0,
8349 				    IEEE80211_VHT_CAP_SOUNDING_DIMENSIONS_MASK);
8350 
8351 	/* TODO: Need to check invalid STS and Sound_dim values set by FW? */
8352 
8353 	/* Enable Sounding Dimension Field only if SU BF is enabled */
8354 	if (subfer) {
8355 		if (sound_dim > (ar->num_tx_chains - 1))
8356 			sound_dim = ar->num_tx_chains - 1;
8357 
8358 		*vht_cap = u32_replace_bits(*vht_cap, sound_dim,
8359 					    IEEE80211_VHT_CAP_SOUNDING_DIMENSIONS_MASK);
8360 	}
8361 
8362 	/* Use the STS advertised by FW unless SU Beamformee is not supported*/
8363 	if (!subfee)
8364 		*vht_cap &= ~(IEEE80211_VHT_CAP_BEAMFORMEE_STS_MASK);
8365 }
8366 
8367 static struct ieee80211_sta_vht_cap
8368 ath12k_create_vht_cap(struct ath12k *ar, u32 rate_cap_tx_chainmask,
8369 		      u32 rate_cap_rx_chainmask)
8370 {
8371 	struct ieee80211_sta_vht_cap vht_cap = {};
8372 	u16 txmcs_map, rxmcs_map;
8373 	int i;
8374 
8375 	vht_cap.vht_supported = 1;
8376 	vht_cap.cap = ar->pdev->cap.vht_cap;
8377 
8378 	ath12k_set_vht_txbf_cap(ar, &vht_cap.cap);
8379 
8380 	/* 80P80 is not supported */
8381 	vht_cap.cap &= ~IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_160_80PLUS80MHZ;
8382 
8383 	rxmcs_map = 0;
8384 	txmcs_map = 0;
8385 	for (i = 0; i < 8; i++) {
8386 		if (i < ar->num_tx_chains && rate_cap_tx_chainmask & BIT(i))
8387 			txmcs_map |= IEEE80211_VHT_MCS_SUPPORT_0_9 << (i * 2);
8388 		else
8389 			txmcs_map |= IEEE80211_VHT_MCS_NOT_SUPPORTED << (i * 2);
8390 
8391 		if (i < ar->num_rx_chains && rate_cap_rx_chainmask & BIT(i))
8392 			rxmcs_map |= IEEE80211_VHT_MCS_SUPPORT_0_9 << (i * 2);
8393 		else
8394 			rxmcs_map |= IEEE80211_VHT_MCS_NOT_SUPPORTED << (i * 2);
8395 	}
8396 
8397 	if (rate_cap_tx_chainmask <= 1)
8398 		vht_cap.cap &= ~IEEE80211_VHT_CAP_TXSTBC;
8399 
8400 	vht_cap.vht_mcs.rx_mcs_map = cpu_to_le16(rxmcs_map);
8401 	vht_cap.vht_mcs.tx_mcs_map = cpu_to_le16(txmcs_map);
8402 
8403 	/* Check if the HW supports 1:1 NSS ratio and reset
8404 	 * EXT NSS BW Support field to 0 to indicate 1:1 ratio
8405 	 */
8406 	if (ar->pdev->cap.nss_ratio_info == WMI_NSS_RATIO_1_NSS)
8407 		vht_cap.cap &= ~IEEE80211_VHT_CAP_EXT_NSS_BW_MASK;
8408 
8409 	return vht_cap;
8410 }
8411 
8412 static void ath12k_mac_setup_ht_vht_cap(struct ath12k *ar,
8413 					struct ath12k_pdev_cap *cap,
8414 					u32 *ht_cap_info)
8415 {
8416 	struct ieee80211_supported_band *band;
8417 	u32 rate_cap_tx_chainmask;
8418 	u32 rate_cap_rx_chainmask;
8419 	u32 ht_cap;
8420 
8421 	rate_cap_tx_chainmask = ar->cfg_tx_chainmask >> cap->tx_chain_mask_shift;
8422 	rate_cap_rx_chainmask = ar->cfg_rx_chainmask >> cap->rx_chain_mask_shift;
8423 
8424 	if (cap->supported_bands & WMI_HOST_WLAN_2GHZ_CAP) {
8425 		band = &ar->mac.sbands[NL80211_BAND_2GHZ];
8426 		ht_cap = cap->band[NL80211_BAND_2GHZ].ht_cap_info;
8427 		if (ht_cap_info)
8428 			*ht_cap_info = ht_cap;
8429 		band->ht_cap = ath12k_create_ht_cap(ar, ht_cap,
8430 						    rate_cap_rx_chainmask);
8431 	}
8432 
8433 	if (cap->supported_bands & WMI_HOST_WLAN_5GHZ_CAP &&
8434 	    (ar->ab->hw_params->single_pdev_only ||
8435 	     !ar->supports_6ghz)) {
8436 		band = &ar->mac.sbands[NL80211_BAND_5GHZ];
8437 		ht_cap = cap->band[NL80211_BAND_5GHZ].ht_cap_info;
8438 		if (ht_cap_info)
8439 			*ht_cap_info = ht_cap;
8440 		band->ht_cap = ath12k_create_ht_cap(ar, ht_cap,
8441 						    rate_cap_rx_chainmask);
8442 		band->vht_cap = ath12k_create_vht_cap(ar, rate_cap_tx_chainmask,
8443 						      rate_cap_rx_chainmask);
8444 	}
8445 }
8446 
8447 static int ath12k_check_chain_mask(struct ath12k *ar, u32 ant, bool is_tx_ant)
8448 {
8449 	/* TODO: Check the request chainmask against the supported
8450 	 * chainmask table which is advertised in extented_service_ready event
8451 	 */
8452 
8453 	return 0;
8454 }
8455 
8456 static void ath12k_gen_ppe_thresh(struct ath12k_wmi_ppe_threshold_arg *fw_ppet,
8457 				  u8 *he_ppet)
8458 {
8459 	int nss, ru;
8460 	u8 bit = 7;
8461 
8462 	he_ppet[0] = fw_ppet->numss_m1 & IEEE80211_PPE_THRES_NSS_MASK;
8463 	he_ppet[0] |= (fw_ppet->ru_bit_mask <<
8464 		       IEEE80211_PPE_THRES_RU_INDEX_BITMASK_POS) &
8465 		      IEEE80211_PPE_THRES_RU_INDEX_BITMASK_MASK;
8466 	for (nss = 0; nss <= fw_ppet->numss_m1; nss++) {
8467 		for (ru = 0; ru < 4; ru++) {
8468 			u8 val;
8469 			int i;
8470 
8471 			if ((fw_ppet->ru_bit_mask & BIT(ru)) == 0)
8472 				continue;
8473 			val = (fw_ppet->ppet16_ppet8_ru3_ru0[nss] >> (ru * 6)) &
8474 			       0x3f;
8475 			val = ((val >> 3) & 0x7) | ((val & 0x7) << 3);
8476 			for (i = 5; i >= 0; i--) {
8477 				he_ppet[bit / 8] |=
8478 					((val >> i) & 0x1) << ((bit % 8));
8479 				bit++;
8480 			}
8481 		}
8482 	}
8483 }
8484 
8485 static void
8486 ath12k_mac_filter_he_cap_mesh(struct ieee80211_he_cap_elem *he_cap_elem)
8487 {
8488 	u8 m;
8489 
8490 	m = IEEE80211_HE_MAC_CAP0_TWT_RES |
8491 	    IEEE80211_HE_MAC_CAP0_TWT_REQ;
8492 	he_cap_elem->mac_cap_info[0] &= ~m;
8493 
8494 	m = IEEE80211_HE_MAC_CAP2_TRS |
8495 	    IEEE80211_HE_MAC_CAP2_BCAST_TWT |
8496 	    IEEE80211_HE_MAC_CAP2_MU_CASCADING;
8497 	he_cap_elem->mac_cap_info[2] &= ~m;
8498 
8499 	m = IEEE80211_HE_MAC_CAP3_FLEX_TWT_SCHED |
8500 	    IEEE80211_HE_MAC_CAP2_BCAST_TWT |
8501 	    IEEE80211_HE_MAC_CAP2_MU_CASCADING;
8502 	he_cap_elem->mac_cap_info[3] &= ~m;
8503 
8504 	m = IEEE80211_HE_MAC_CAP4_BSRP_BQRP_A_MPDU_AGG |
8505 	    IEEE80211_HE_MAC_CAP4_BQR;
8506 	he_cap_elem->mac_cap_info[4] &= ~m;
8507 
8508 	m = IEEE80211_HE_MAC_CAP5_SUBCHAN_SELECTIVE_TRANSMISSION |
8509 	    IEEE80211_HE_MAC_CAP5_UL_2x996_TONE_RU |
8510 	    IEEE80211_HE_MAC_CAP5_PUNCTURED_SOUNDING |
8511 	    IEEE80211_HE_MAC_CAP5_HT_VHT_TRIG_FRAME_RX;
8512 	he_cap_elem->mac_cap_info[5] &= ~m;
8513 
8514 	m = IEEE80211_HE_PHY_CAP2_UL_MU_FULL_MU_MIMO |
8515 	    IEEE80211_HE_PHY_CAP2_UL_MU_PARTIAL_MU_MIMO;
8516 	he_cap_elem->phy_cap_info[2] &= ~m;
8517 
8518 	m = IEEE80211_HE_PHY_CAP3_RX_PARTIAL_BW_SU_IN_20MHZ_MU |
8519 	    IEEE80211_HE_PHY_CAP3_DCM_MAX_CONST_TX_MASK |
8520 	    IEEE80211_HE_PHY_CAP3_DCM_MAX_CONST_RX_MASK;
8521 	he_cap_elem->phy_cap_info[3] &= ~m;
8522 
8523 	m = IEEE80211_HE_PHY_CAP4_MU_BEAMFORMER;
8524 	he_cap_elem->phy_cap_info[4] &= ~m;
8525 
8526 	m = IEEE80211_HE_PHY_CAP5_NG16_MU_FEEDBACK;
8527 	he_cap_elem->phy_cap_info[5] &= ~m;
8528 
8529 	m = IEEE80211_HE_PHY_CAP6_CODEBOOK_SIZE_75_MU |
8530 	    IEEE80211_HE_PHY_CAP6_TRIG_MU_BEAMFORMING_PARTIAL_BW_FB |
8531 	    IEEE80211_HE_PHY_CAP6_TRIG_CQI_FB |
8532 	    IEEE80211_HE_PHY_CAP6_PARTIAL_BANDWIDTH_DL_MUMIMO;
8533 	he_cap_elem->phy_cap_info[6] &= ~m;
8534 
8535 	m = IEEE80211_HE_PHY_CAP7_PSR_BASED_SR |
8536 	    IEEE80211_HE_PHY_CAP7_POWER_BOOST_FACTOR_SUPP |
8537 	    IEEE80211_HE_PHY_CAP7_STBC_TX_ABOVE_80MHZ |
8538 	    IEEE80211_HE_PHY_CAP7_STBC_RX_ABOVE_80MHZ;
8539 	he_cap_elem->phy_cap_info[7] &= ~m;
8540 
8541 	m = IEEE80211_HE_PHY_CAP8_HE_ER_SU_PPDU_4XLTF_AND_08_US_GI |
8542 	    IEEE80211_HE_PHY_CAP8_20MHZ_IN_40MHZ_HE_PPDU_IN_2G |
8543 	    IEEE80211_HE_PHY_CAP8_20MHZ_IN_160MHZ_HE_PPDU |
8544 	    IEEE80211_HE_PHY_CAP8_80MHZ_IN_160MHZ_HE_PPDU;
8545 	he_cap_elem->phy_cap_info[8] &= ~m;
8546 
8547 	m = IEEE80211_HE_PHY_CAP9_LONGER_THAN_16_SIGB_OFDM_SYM |
8548 	    IEEE80211_HE_PHY_CAP9_NON_TRIGGERED_CQI_FEEDBACK |
8549 	    IEEE80211_HE_PHY_CAP9_RX_1024_QAM_LESS_THAN_242_TONE_RU |
8550 	    IEEE80211_HE_PHY_CAP9_TX_1024_QAM_LESS_THAN_242_TONE_RU |
8551 	    IEEE80211_HE_PHY_CAP9_RX_FULL_BW_SU_USING_MU_WITH_COMP_SIGB |
8552 	    IEEE80211_HE_PHY_CAP9_RX_FULL_BW_SU_USING_MU_WITH_NON_COMP_SIGB;
8553 	he_cap_elem->phy_cap_info[9] &= ~m;
8554 }
8555 
8556 static __le16 ath12k_mac_setup_he_6ghz_cap(struct ath12k_pdev_cap *pcap,
8557 					   struct ath12k_band_cap *bcap)
8558 {
8559 	u8 val;
8560 
8561 	bcap->he_6ghz_capa = IEEE80211_HT_MPDU_DENSITY_NONE;
8562 	if (bcap->ht_cap_info & WMI_HT_CAP_DYNAMIC_SMPS)
8563 		bcap->he_6ghz_capa |=
8564 			u32_encode_bits(WLAN_HT_CAP_SM_PS_DYNAMIC,
8565 					IEEE80211_HE_6GHZ_CAP_SM_PS);
8566 	else
8567 		bcap->he_6ghz_capa |=
8568 			u32_encode_bits(WLAN_HT_CAP_SM_PS_DISABLED,
8569 					IEEE80211_HE_6GHZ_CAP_SM_PS);
8570 	val = u32_get_bits(pcap->vht_cap,
8571 			   IEEE80211_VHT_CAP_MAX_A_MPDU_LENGTH_EXPONENT_MASK);
8572 	bcap->he_6ghz_capa |=
8573 		u32_encode_bits(val, IEEE80211_HE_6GHZ_CAP_MAX_AMPDU_LEN_EXP);
8574 	val = u32_get_bits(pcap->vht_cap,
8575 			   IEEE80211_VHT_CAP_MAX_MPDU_MASK);
8576 	bcap->he_6ghz_capa |=
8577 		u32_encode_bits(val, IEEE80211_HE_6GHZ_CAP_MAX_MPDU_LEN);
8578 	if (pcap->vht_cap & IEEE80211_VHT_CAP_RX_ANTENNA_PATTERN)
8579 		bcap->he_6ghz_capa |= IEEE80211_HE_6GHZ_CAP_RX_ANTPAT_CONS;
8580 	if (pcap->vht_cap & IEEE80211_VHT_CAP_TX_ANTENNA_PATTERN)
8581 		bcap->he_6ghz_capa |= IEEE80211_HE_6GHZ_CAP_TX_ANTPAT_CONS;
8582 
8583 	return cpu_to_le16(bcap->he_6ghz_capa);
8584 }
8585 
8586 static void ath12k_mac_set_hemcsmap(struct ath12k *ar,
8587 				    struct ath12k_pdev_cap *cap,
8588 				    struct ieee80211_sta_he_cap *he_cap)
8589 {
8590 	struct ieee80211_he_mcs_nss_supp *mcs_nss = &he_cap->he_mcs_nss_supp;
8591 	u8 maxtxnss_160 = ath12k_get_nss_160mhz(ar, ar->num_tx_chains);
8592 	u8 maxrxnss_160 = ath12k_get_nss_160mhz(ar, ar->num_rx_chains);
8593 	u16 txmcs_map_160 = 0, rxmcs_map_160 = 0;
8594 	u16 txmcs_map = 0, rxmcs_map = 0;
8595 	u32 i;
8596 
8597 	for (i = 0; i < 8; i++) {
8598 		if (i < ar->num_tx_chains &&
8599 		    (ar->cfg_tx_chainmask >> cap->tx_chain_mask_shift) & BIT(i))
8600 			txmcs_map |= IEEE80211_HE_MCS_SUPPORT_0_11 << (i * 2);
8601 		else
8602 			txmcs_map |= IEEE80211_HE_MCS_NOT_SUPPORTED << (i * 2);
8603 
8604 		if (i < ar->num_rx_chains &&
8605 		    (ar->cfg_rx_chainmask >> cap->tx_chain_mask_shift) & BIT(i))
8606 			rxmcs_map |= IEEE80211_HE_MCS_SUPPORT_0_11 << (i * 2);
8607 		else
8608 			rxmcs_map |= IEEE80211_HE_MCS_NOT_SUPPORTED << (i * 2);
8609 
8610 		if (i < maxtxnss_160 &&
8611 		    (ar->cfg_tx_chainmask >> cap->tx_chain_mask_shift) & BIT(i))
8612 			txmcs_map_160 |= IEEE80211_HE_MCS_SUPPORT_0_11 << (i * 2);
8613 		else
8614 			txmcs_map_160 |= IEEE80211_HE_MCS_NOT_SUPPORTED << (i * 2);
8615 
8616 		if (i < maxrxnss_160 &&
8617 		    (ar->cfg_tx_chainmask >> cap->tx_chain_mask_shift) & BIT(i))
8618 			rxmcs_map_160 |= IEEE80211_HE_MCS_SUPPORT_0_11 << (i * 2);
8619 		else
8620 			rxmcs_map_160 |= IEEE80211_HE_MCS_NOT_SUPPORTED << (i * 2);
8621 	}
8622 
8623 	mcs_nss->rx_mcs_80 = cpu_to_le16(rxmcs_map & 0xffff);
8624 	mcs_nss->tx_mcs_80 = cpu_to_le16(txmcs_map & 0xffff);
8625 	mcs_nss->rx_mcs_160 = cpu_to_le16(rxmcs_map_160 & 0xffff);
8626 	mcs_nss->tx_mcs_160 = cpu_to_le16(txmcs_map_160 & 0xffff);
8627 }
8628 
8629 static void ath12k_mac_copy_he_cap(struct ath12k *ar,
8630 				   struct ath12k_band_cap *band_cap,
8631 				   int iftype, u8 num_tx_chains,
8632 				   struct ieee80211_sta_he_cap *he_cap)
8633 {
8634 	struct ieee80211_he_cap_elem *he_cap_elem = &he_cap->he_cap_elem;
8635 
8636 	he_cap->has_he = true;
8637 	memcpy(he_cap_elem->mac_cap_info, band_cap->he_cap_info,
8638 	       sizeof(he_cap_elem->mac_cap_info));
8639 	memcpy(he_cap_elem->phy_cap_info, band_cap->he_cap_phy_info,
8640 	       sizeof(he_cap_elem->phy_cap_info));
8641 
8642 	he_cap_elem->mac_cap_info[1] &=
8643 		IEEE80211_HE_MAC_CAP1_TF_MAC_PAD_DUR_MASK;
8644 	he_cap_elem->phy_cap_info[0] &=
8645 		IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_40MHZ_IN_2G |
8646 		IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_40MHZ_80MHZ_IN_5G |
8647 		IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_160MHZ_IN_5G;
8648 	/* 80PLUS80 is not supported */
8649 	he_cap_elem->phy_cap_info[0] &=
8650 		~IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_80PLUS80_MHZ_IN_5G;
8651 	he_cap_elem->phy_cap_info[5] &=
8652 		~IEEE80211_HE_PHY_CAP5_BEAMFORMEE_NUM_SND_DIM_UNDER_80MHZ_MASK;
8653 	he_cap_elem->phy_cap_info[5] |= num_tx_chains - 1;
8654 
8655 	switch (iftype) {
8656 	case NL80211_IFTYPE_AP:
8657 		he_cap_elem->mac_cap_info[2] &=
8658 			~IEEE80211_HE_MAC_CAP2_BCAST_TWT;
8659 		he_cap_elem->phy_cap_info[3] &=
8660 			~IEEE80211_HE_PHY_CAP3_DCM_MAX_CONST_TX_MASK;
8661 		he_cap_elem->phy_cap_info[9] |=
8662 			IEEE80211_HE_PHY_CAP9_RX_1024_QAM_LESS_THAN_242_TONE_RU;
8663 		break;
8664 	case NL80211_IFTYPE_STATION:
8665 		he_cap_elem->mac_cap_info[0] &= ~IEEE80211_HE_MAC_CAP0_TWT_RES;
8666 		he_cap_elem->mac_cap_info[0] |= IEEE80211_HE_MAC_CAP0_TWT_REQ;
8667 		he_cap_elem->phy_cap_info[9] |=
8668 			IEEE80211_HE_PHY_CAP9_TX_1024_QAM_LESS_THAN_242_TONE_RU;
8669 		break;
8670 	case NL80211_IFTYPE_MESH_POINT:
8671 		ath12k_mac_filter_he_cap_mesh(he_cap_elem);
8672 		break;
8673 	}
8674 
8675 	ath12k_mac_set_hemcsmap(ar, &ar->pdev->cap, he_cap);
8676 	memset(he_cap->ppe_thres, 0, sizeof(he_cap->ppe_thres));
8677 	if (he_cap_elem->phy_cap_info[6] &
8678 	    IEEE80211_HE_PHY_CAP6_PPE_THRESHOLD_PRESENT)
8679 		ath12k_gen_ppe_thresh(&band_cap->he_ppet, he_cap->ppe_thres);
8680 }
8681 
8682 static void
8683 ath12k_mac_copy_eht_mcs_nss(struct ath12k_band_cap *band_cap,
8684 			    struct ieee80211_eht_mcs_nss_supp *mcs_nss,
8685 			    const struct ieee80211_he_cap_elem *he_cap,
8686 			    const struct ieee80211_eht_cap_elem_fixed *eht_cap)
8687 {
8688 	if ((he_cap->phy_cap_info[0] &
8689 	     (IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_40MHZ_IN_2G |
8690 	      IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_40MHZ_80MHZ_IN_5G |
8691 	      IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_160MHZ_IN_5G |
8692 	      IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_80PLUS80_MHZ_IN_5G)) == 0)
8693 		memcpy(&mcs_nss->only_20mhz, &band_cap->eht_mcs_20_only,
8694 		       sizeof(struct ieee80211_eht_mcs_nss_supp_20mhz_only));
8695 
8696 	if (he_cap->phy_cap_info[0] &
8697 	    (IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_40MHZ_IN_2G |
8698 	     IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_40MHZ_80MHZ_IN_5G))
8699 		memcpy(&mcs_nss->bw._80, &band_cap->eht_mcs_80,
8700 		       sizeof(struct ieee80211_eht_mcs_nss_supp_bw));
8701 
8702 	if (he_cap->phy_cap_info[0] &
8703 	    IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_160MHZ_IN_5G)
8704 		memcpy(&mcs_nss->bw._160, &band_cap->eht_mcs_160,
8705 		       sizeof(struct ieee80211_eht_mcs_nss_supp_bw));
8706 
8707 	if (eht_cap->phy_cap_info[0] & IEEE80211_EHT_PHY_CAP0_320MHZ_IN_6GHZ)
8708 		memcpy(&mcs_nss->bw._320, &band_cap->eht_mcs_320,
8709 		       sizeof(struct ieee80211_eht_mcs_nss_supp_bw));
8710 }
8711 
8712 static void ath12k_mac_copy_eht_ppe_thresh(struct ath12k_wmi_ppe_threshold_arg *fw_ppet,
8713 					   struct ieee80211_sta_eht_cap *cap)
8714 {
8715 	u16 bit = IEEE80211_EHT_PPE_THRES_INFO_HEADER_SIZE;
8716 	u8 i, nss, ru, ppet_bit_len_per_ru = IEEE80211_EHT_PPE_THRES_INFO_PPET_SIZE * 2;
8717 
8718 	u8p_replace_bits(&cap->eht_ppe_thres[0], fw_ppet->numss_m1,
8719 			 IEEE80211_EHT_PPE_THRES_NSS_MASK);
8720 
8721 	u16p_replace_bits((u16 *)&cap->eht_ppe_thres[0], fw_ppet->ru_bit_mask,
8722 			  IEEE80211_EHT_PPE_THRES_RU_INDEX_BITMASK_MASK);
8723 
8724 	for (nss = 0; nss <= fw_ppet->numss_m1; nss++) {
8725 		for (ru = 0;
8726 		     ru < hweight16(IEEE80211_EHT_PPE_THRES_RU_INDEX_BITMASK_MASK);
8727 		     ru++) {
8728 			u32 val = 0;
8729 
8730 			if ((fw_ppet->ru_bit_mask & BIT(ru)) == 0)
8731 				continue;
8732 
8733 			u32p_replace_bits(&val, fw_ppet->ppet16_ppet8_ru3_ru0[nss] >>
8734 						(ru * ppet_bit_len_per_ru),
8735 					  GENMASK(ppet_bit_len_per_ru - 1, 0));
8736 
8737 			for (i = 0; i < ppet_bit_len_per_ru; i++) {
8738 				cap->eht_ppe_thres[bit / 8] |=
8739 					(((val >> i) & 0x1) << ((bit % 8)));
8740 				bit++;
8741 			}
8742 		}
8743 	}
8744 }
8745 
8746 static void
8747 ath12k_mac_filter_eht_cap_mesh(struct ieee80211_eht_cap_elem_fixed
8748 			       *eht_cap_elem)
8749 {
8750 	u8 m;
8751 
8752 	m = IEEE80211_EHT_MAC_CAP0_EPCS_PRIO_ACCESS;
8753 	eht_cap_elem->mac_cap_info[0] &= ~m;
8754 
8755 	m = IEEE80211_EHT_PHY_CAP0_PARTIAL_BW_UL_MU_MIMO;
8756 	eht_cap_elem->phy_cap_info[0] &= ~m;
8757 
8758 	m = IEEE80211_EHT_PHY_CAP3_NG_16_MU_FEEDBACK |
8759 	    IEEE80211_EHT_PHY_CAP3_CODEBOOK_7_5_MU_FDBK |
8760 	    IEEE80211_EHT_PHY_CAP3_TRIG_MU_BF_PART_BW_FDBK |
8761 	    IEEE80211_EHT_PHY_CAP3_TRIG_CQI_FDBK;
8762 	eht_cap_elem->phy_cap_info[3] &= ~m;
8763 
8764 	m = IEEE80211_EHT_PHY_CAP4_PART_BW_DL_MU_MIMO |
8765 	    IEEE80211_EHT_PHY_CAP4_PSR_SR_SUPP |
8766 	    IEEE80211_EHT_PHY_CAP4_POWER_BOOST_FACT_SUPP |
8767 	    IEEE80211_EHT_PHY_CAP4_EHT_MU_PPDU_4_EHT_LTF_08_GI;
8768 	eht_cap_elem->phy_cap_info[4] &= ~m;
8769 
8770 	m = IEEE80211_EHT_PHY_CAP5_NON_TRIG_CQI_FEEDBACK |
8771 	    IEEE80211_EHT_PHY_CAP5_TX_LESS_242_TONE_RU_SUPP |
8772 	    IEEE80211_EHT_PHY_CAP5_RX_LESS_242_TONE_RU_SUPP |
8773 	    IEEE80211_EHT_PHY_CAP5_MAX_NUM_SUPP_EHT_LTF_MASK;
8774 	eht_cap_elem->phy_cap_info[5] &= ~m;
8775 
8776 	m = IEEE80211_EHT_PHY_CAP6_MAX_NUM_SUPP_EHT_LTF_MASK;
8777 	eht_cap_elem->phy_cap_info[6] &= ~m;
8778 
8779 	m = IEEE80211_EHT_PHY_CAP7_NON_OFDMA_UL_MU_MIMO_80MHZ |
8780 	    IEEE80211_EHT_PHY_CAP7_NON_OFDMA_UL_MU_MIMO_160MHZ |
8781 	    IEEE80211_EHT_PHY_CAP7_NON_OFDMA_UL_MU_MIMO_320MHZ |
8782 	    IEEE80211_EHT_PHY_CAP7_MU_BEAMFORMER_80MHZ |
8783 	    IEEE80211_EHT_PHY_CAP7_MU_BEAMFORMER_160MHZ |
8784 	    IEEE80211_EHT_PHY_CAP7_MU_BEAMFORMER_320MHZ;
8785 	eht_cap_elem->phy_cap_info[7] &= ~m;
8786 }
8787 
8788 static void ath12k_mac_copy_eht_cap(struct ath12k *ar,
8789 				    struct ath12k_band_cap *band_cap,
8790 				    struct ieee80211_he_cap_elem *he_cap_elem,
8791 				    int iftype,
8792 				    struct ieee80211_sta_eht_cap *eht_cap)
8793 {
8794 	struct ieee80211_eht_cap_elem_fixed *eht_cap_elem = &eht_cap->eht_cap_elem;
8795 
8796 	memset(eht_cap, 0, sizeof(struct ieee80211_sta_eht_cap));
8797 
8798 	if (!(test_bit(WMI_TLV_SERVICE_11BE, ar->ab->wmi_ab.svc_map)) ||
8799 	    ath12k_acpi_get_disable_11be(ar->ab))
8800 		return;
8801 
8802 	eht_cap->has_eht = true;
8803 	memcpy(eht_cap_elem->mac_cap_info, band_cap->eht_cap_mac_info,
8804 	       sizeof(eht_cap_elem->mac_cap_info));
8805 	memcpy(eht_cap_elem->phy_cap_info, band_cap->eht_cap_phy_info,
8806 	       sizeof(eht_cap_elem->phy_cap_info));
8807 
8808 	switch (iftype) {
8809 	case NL80211_IFTYPE_AP:
8810 		eht_cap_elem->phy_cap_info[0] &=
8811 			~IEEE80211_EHT_PHY_CAP0_242_TONE_RU_GT20MHZ;
8812 		eht_cap_elem->phy_cap_info[4] &=
8813 			~IEEE80211_EHT_PHY_CAP4_PART_BW_DL_MU_MIMO;
8814 		eht_cap_elem->phy_cap_info[5] &=
8815 			~IEEE80211_EHT_PHY_CAP5_TX_LESS_242_TONE_RU_SUPP;
8816 		break;
8817 	case NL80211_IFTYPE_STATION:
8818 		eht_cap_elem->phy_cap_info[7] &=
8819 			~(IEEE80211_EHT_PHY_CAP7_NON_OFDMA_UL_MU_MIMO_80MHZ |
8820 			  IEEE80211_EHT_PHY_CAP7_NON_OFDMA_UL_MU_MIMO_160MHZ |
8821 			  IEEE80211_EHT_PHY_CAP7_NON_OFDMA_UL_MU_MIMO_320MHZ);
8822 		eht_cap_elem->phy_cap_info[7] &=
8823 			~(IEEE80211_EHT_PHY_CAP7_MU_BEAMFORMER_80MHZ |
8824 			  IEEE80211_EHT_PHY_CAP7_MU_BEAMFORMER_160MHZ |
8825 			  IEEE80211_EHT_PHY_CAP7_MU_BEAMFORMER_320MHZ);
8826 		break;
8827 	case NL80211_IFTYPE_MESH_POINT:
8828 		ath12k_mac_filter_eht_cap_mesh(eht_cap_elem);
8829 		break;
8830 	default:
8831 		break;
8832 	}
8833 
8834 	ath12k_mac_copy_eht_mcs_nss(band_cap, &eht_cap->eht_mcs_nss_supp,
8835 				    he_cap_elem, eht_cap_elem);
8836 
8837 	if (eht_cap_elem->phy_cap_info[5] &
8838 	    IEEE80211_EHT_PHY_CAP5_PPE_THRESHOLD_PRESENT)
8839 		ath12k_mac_copy_eht_ppe_thresh(&band_cap->eht_ppet, eht_cap);
8840 }
8841 
8842 static int ath12k_mac_copy_sband_iftype_data(struct ath12k *ar,
8843 					     struct ath12k_pdev_cap *cap,
8844 					     struct ieee80211_sband_iftype_data *data,
8845 					     int band)
8846 {
8847 	struct ath12k_band_cap *band_cap = &cap->band[band];
8848 	int i, idx = 0;
8849 
8850 	for (i = 0; i < NUM_NL80211_IFTYPES; i++) {
8851 		struct ieee80211_sta_he_cap *he_cap = &data[idx].he_cap;
8852 
8853 		switch (i) {
8854 		case NL80211_IFTYPE_STATION:
8855 		case NL80211_IFTYPE_AP:
8856 		case NL80211_IFTYPE_MESH_POINT:
8857 			break;
8858 
8859 		default:
8860 			continue;
8861 		}
8862 
8863 		data[idx].types_mask = BIT(i);
8864 
8865 		ath12k_mac_copy_he_cap(ar, band_cap, i, ar->num_tx_chains, he_cap);
8866 		if (band == NL80211_BAND_6GHZ) {
8867 			data[idx].he_6ghz_capa.capa =
8868 				ath12k_mac_setup_he_6ghz_cap(cap, band_cap);
8869 		}
8870 		ath12k_mac_copy_eht_cap(ar, band_cap, &he_cap->he_cap_elem, i,
8871 					&data[idx].eht_cap);
8872 		idx++;
8873 	}
8874 
8875 	return idx;
8876 }
8877 
8878 static void ath12k_mac_setup_sband_iftype_data(struct ath12k *ar,
8879 					       struct ath12k_pdev_cap *cap)
8880 {
8881 	struct ieee80211_supported_band *sband;
8882 	enum nl80211_band band;
8883 	int count;
8884 
8885 	if (cap->supported_bands & WMI_HOST_WLAN_2GHZ_CAP) {
8886 		band = NL80211_BAND_2GHZ;
8887 		count = ath12k_mac_copy_sband_iftype_data(ar, cap,
8888 							  ar->mac.iftype[band],
8889 							  band);
8890 		sband = &ar->mac.sbands[band];
8891 		_ieee80211_set_sband_iftype_data(sband, ar->mac.iftype[band],
8892 						 count);
8893 	}
8894 
8895 	if (cap->supported_bands & WMI_HOST_WLAN_5GHZ_CAP) {
8896 		band = NL80211_BAND_5GHZ;
8897 		count = ath12k_mac_copy_sband_iftype_data(ar, cap,
8898 							  ar->mac.iftype[band],
8899 							  band);
8900 		sband = &ar->mac.sbands[band];
8901 		_ieee80211_set_sband_iftype_data(sband, ar->mac.iftype[band],
8902 						 count);
8903 	}
8904 
8905 	if (cap->supported_bands & WMI_HOST_WLAN_5GHZ_CAP &&
8906 	    ar->supports_6ghz) {
8907 		band = NL80211_BAND_6GHZ;
8908 		count = ath12k_mac_copy_sband_iftype_data(ar, cap,
8909 							  ar->mac.iftype[band],
8910 							  band);
8911 		sband = &ar->mac.sbands[band];
8912 		_ieee80211_set_sband_iftype_data(sband, ar->mac.iftype[band],
8913 						 count);
8914 	}
8915 }
8916 
8917 static int __ath12k_set_antenna(struct ath12k *ar, u32 tx_ant, u32 rx_ant)
8918 {
8919 	struct ath12k_hw *ah = ath12k_ar_to_ah(ar);
8920 	int ret;
8921 
8922 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
8923 
8924 	if (ath12k_check_chain_mask(ar, tx_ant, true))
8925 		return -EINVAL;
8926 
8927 	if (ath12k_check_chain_mask(ar, rx_ant, false))
8928 		return -EINVAL;
8929 
8930 	/* Since we advertised the max cap of all radios combined during wiphy
8931 	 * registration, ensure we don't set the antenna config higher than the
8932 	 * limits
8933 	 */
8934 	tx_ant = min_t(u32, tx_ant, ar->pdev->cap.tx_chain_mask);
8935 	rx_ant = min_t(u32, rx_ant, ar->pdev->cap.rx_chain_mask);
8936 
8937 	ar->cfg_tx_chainmask = tx_ant;
8938 	ar->cfg_rx_chainmask = rx_ant;
8939 
8940 	if (ah->state != ATH12K_HW_STATE_ON &&
8941 	    ah->state != ATH12K_HW_STATE_RESTARTED)
8942 		return 0;
8943 
8944 	ret = ath12k_wmi_pdev_set_param(ar, WMI_PDEV_PARAM_TX_CHAIN_MASK,
8945 					tx_ant, ar->pdev->pdev_id);
8946 	if (ret) {
8947 		ath12k_warn(ar->ab, "failed to set tx-chainmask: %d, req 0x%x\n",
8948 			    ret, tx_ant);
8949 		return ret;
8950 	}
8951 
8952 	ar->num_tx_chains = hweight32(tx_ant);
8953 
8954 	ret = ath12k_wmi_pdev_set_param(ar, WMI_PDEV_PARAM_RX_CHAIN_MASK,
8955 					rx_ant, ar->pdev->pdev_id);
8956 	if (ret) {
8957 		ath12k_warn(ar->ab, "failed to set rx-chainmask: %d, req 0x%x\n",
8958 			    ret, rx_ant);
8959 		return ret;
8960 	}
8961 
8962 	ar->num_rx_chains = hweight32(rx_ant);
8963 
8964 	/* Reload HT/VHT/HE capability */
8965 	ath12k_mac_setup_ht_vht_cap(ar, &ar->pdev->cap, NULL);
8966 	ath12k_mac_setup_sband_iftype_data(ar, &ar->pdev->cap);
8967 
8968 	return 0;
8969 }
8970 
8971 static void ath12k_mgmt_over_wmi_tx_drop(struct ath12k *ar, struct sk_buff *skb)
8972 {
8973 	int num_mgmt;
8974 
8975 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
8976 
8977 	ieee80211_free_txskb(ath12k_ar_to_hw(ar), skb);
8978 
8979 	num_mgmt = atomic_dec_if_positive(&ar->num_pending_mgmt_tx);
8980 
8981 	if (num_mgmt < 0)
8982 		WARN_ON_ONCE(1);
8983 
8984 	if (!num_mgmt)
8985 		wake_up(&ar->txmgmt_empty_waitq);
8986 }
8987 
8988 static void ath12k_mac_tx_mgmt_free(struct ath12k *ar, int buf_id)
8989 {
8990 	struct sk_buff *msdu;
8991 	struct ieee80211_tx_info *info;
8992 
8993 	spin_lock_bh(&ar->txmgmt_idr_lock);
8994 	msdu = idr_remove(&ar->txmgmt_idr, buf_id);
8995 	spin_unlock_bh(&ar->txmgmt_idr_lock);
8996 
8997 	if (!msdu)
8998 		return;
8999 
9000 	dma_unmap_single(ar->ab->dev, ATH12K_SKB_CB(msdu)->paddr, msdu->len,
9001 			 DMA_TO_DEVICE);
9002 
9003 	info = IEEE80211_SKB_CB(msdu);
9004 	memset(&info->status, 0, sizeof(info->status));
9005 
9006 	ath12k_mgmt_over_wmi_tx_drop(ar, msdu);
9007 }
9008 
9009 int ath12k_mac_tx_mgmt_pending_free(int buf_id, void *skb, void *ctx)
9010 {
9011 	struct ath12k *ar = ctx;
9012 
9013 	ath12k_mac_tx_mgmt_free(ar, buf_id);
9014 
9015 	return 0;
9016 }
9017 
9018 static int ath12k_mac_vif_txmgmt_idr_remove(int buf_id, void *skb, void *ctx)
9019 {
9020 	struct ieee80211_vif *vif = ctx;
9021 	struct ath12k_skb_cb *skb_cb = ATH12K_SKB_CB(skb);
9022 	struct ath12k *ar = skb_cb->ar;
9023 
9024 	if (skb_cb->vif == vif)
9025 		ath12k_mac_tx_mgmt_free(ar, buf_id);
9026 
9027 	return 0;
9028 }
9029 
9030 static int ath12k_mac_mgmt_tx_wmi(struct ath12k *ar, struct ath12k_link_vif *arvif,
9031 				  struct sk_buff *skb)
9032 {
9033 	struct ath12k_base *ab = ar->ab;
9034 	struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
9035 	struct ath12k_skb_cb *skb_cb = ATH12K_SKB_CB(skb);
9036 	struct ieee80211_tx_info *info;
9037 	enum hal_encrypt_type enctype;
9038 	unsigned int mic_len;
9039 	dma_addr_t paddr;
9040 	int buf_id;
9041 	int ret;
9042 
9043 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
9044 
9045 	skb_cb->ar = ar;
9046 	spin_lock_bh(&ar->txmgmt_idr_lock);
9047 	buf_id = idr_alloc(&ar->txmgmt_idr, skb, 0,
9048 			   ATH12K_TX_MGMT_NUM_PENDING_MAX, GFP_ATOMIC);
9049 	spin_unlock_bh(&ar->txmgmt_idr_lock);
9050 	if (buf_id < 0)
9051 		return -ENOSPC;
9052 
9053 	info = IEEE80211_SKB_CB(skb);
9054 	if ((skb_cb->flags & ATH12K_SKB_CIPHER_SET) &&
9055 	    !(info->flags & IEEE80211_TX_CTL_HW_80211_ENCAP)) {
9056 		if ((ieee80211_is_action(hdr->frame_control) ||
9057 		     ieee80211_is_deauth(hdr->frame_control) ||
9058 		     ieee80211_is_disassoc(hdr->frame_control)) &&
9059 		     ieee80211_has_protected(hdr->frame_control)) {
9060 			enctype = ath12k_dp_tx_get_encrypt_type(skb_cb->cipher);
9061 			mic_len = ath12k_dp_rx_crypto_mic_len(ab->dp, enctype);
9062 			skb_put(skb, mic_len);
9063 		}
9064 	}
9065 
9066 	paddr = dma_map_single(ab->dev, skb->data, skb->len, DMA_TO_DEVICE);
9067 	if (dma_mapping_error(ab->dev, paddr)) {
9068 		ath12k_warn(ab, "failed to DMA map mgmt Tx buffer\n");
9069 		ret = -EIO;
9070 		goto err_free_idr;
9071 	}
9072 
9073 	skb_cb->paddr = paddr;
9074 
9075 	ret = ath12k_wmi_mgmt_send(arvif, buf_id, skb);
9076 	if (ret) {
9077 		ath12k_warn(ar->ab, "failed to send mgmt frame: %d\n", ret);
9078 		goto err_unmap_buf;
9079 	}
9080 
9081 	return 0;
9082 
9083 err_unmap_buf:
9084 	dma_unmap_single(ab->dev, skb_cb->paddr,
9085 			 skb->len, DMA_TO_DEVICE);
9086 err_free_idr:
9087 	spin_lock_bh(&ar->txmgmt_idr_lock);
9088 	idr_remove(&ar->txmgmt_idr, buf_id);
9089 	spin_unlock_bh(&ar->txmgmt_idr_lock);
9090 
9091 	return ret;
9092 }
9093 
9094 static void ath12k_mgmt_over_wmi_tx_purge(struct ath12k *ar)
9095 {
9096 	struct sk_buff *skb;
9097 
9098 	while ((skb = skb_dequeue(&ar->wmi_mgmt_tx_queue)) != NULL)
9099 		ath12k_mgmt_over_wmi_tx_drop(ar, skb);
9100 }
9101 
9102 static int ath12k_mac_mgmt_action_frame_fill_elem_data(struct ath12k_link_vif *arvif,
9103 						       struct sk_buff *skb)
9104 {
9105 	struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
9106 	u8 category, *buf, iv_len, action_code, dialog_token;
9107 	struct ieee80211_bss_conf *link_conf;
9108 	struct ieee80211_chanctx_conf *conf;
9109 	int cur_tx_power, max_tx_power;
9110 	struct ath12k *ar = arvif->ar;
9111 	struct ieee80211_hw *hw = ath12k_ar_to_hw(ar);
9112 	struct wiphy *wiphy = hw->wiphy;
9113 	struct ath12k_skb_cb *skb_cb;
9114 	struct ieee80211_mgmt *mgmt;
9115 	unsigned int remaining_len;
9116 	bool has_protected;
9117 
9118 	lockdep_assert_wiphy(wiphy);
9119 
9120 	/* make sure category field is present */
9121 	if (skb->len < IEEE80211_MIN_ACTION_SIZE)
9122 		return -EINVAL;
9123 
9124 	remaining_len = skb->len - IEEE80211_MIN_ACTION_SIZE;
9125 	has_protected = ieee80211_has_protected(hdr->frame_control);
9126 
9127 	/* In case of SW crypto and hdr protected (PMF), packet will already be encrypted,
9128 	 * we can't put in data in this case
9129 	 */
9130 	if (test_bit(ATH12K_FLAG_HW_CRYPTO_DISABLED, &ar->ab->dev_flags) &&
9131 	    has_protected)
9132 		return 0;
9133 
9134 	mgmt = (struct ieee80211_mgmt *)hdr;
9135 	buf = (u8 *)&mgmt->u.action;
9136 
9137 	/* FCTL_PROTECTED frame might have extra space added for HDR_LEN. Offset that
9138 	 * many bytes if it is there
9139 	 */
9140 	if (has_protected) {
9141 		skb_cb = ATH12K_SKB_CB(skb);
9142 
9143 		switch (skb_cb->cipher) {
9144 		/* Cipher suite having flag %IEEE80211_KEY_FLAG_GENERATE_IV_MGMT set in
9145 		 * key needs to be processed. See ath12k_install_key()
9146 		 */
9147 		case WLAN_CIPHER_SUITE_CCMP:
9148 		case WLAN_CIPHER_SUITE_CCMP_256:
9149 		case WLAN_CIPHER_SUITE_GCMP:
9150 		case WLAN_CIPHER_SUITE_GCMP_256:
9151 			iv_len = IEEE80211_CCMP_HDR_LEN;
9152 			break;
9153 		case WLAN_CIPHER_SUITE_TKIP:
9154 			iv_len = 0;
9155 			break;
9156 		default:
9157 			return -EINVAL;
9158 		}
9159 
9160 		if (remaining_len < iv_len)
9161 			return -EINVAL;
9162 
9163 		buf += iv_len;
9164 		remaining_len -= iv_len;
9165 	}
9166 
9167 	category = *buf++;
9168 	/* category code is already taken care in %IEEE80211_MIN_ACTION_SIZE hence
9169 	 * no need to adjust remaining_len
9170 	 */
9171 
9172 	switch (category) {
9173 	case WLAN_CATEGORY_RADIO_MEASUREMENT:
9174 		/* need action code and dialog token */
9175 		if (remaining_len < 2)
9176 			return -EINVAL;
9177 
9178 		/* Packet Format:
9179 		 *	Action Code | Dialog Token | Variable Len (based on Action Code)
9180 		 */
9181 		action_code = *buf++;
9182 		dialog_token = *buf++;
9183 		remaining_len -= 2;
9184 
9185 		link_conf = ath12k_mac_get_link_bss_conf(arvif);
9186 		if (!link_conf) {
9187 			ath12k_warn(ar->ab,
9188 				    "failed to get bss link conf for vdev %d in RM handling\n",
9189 				    arvif->vdev_id);
9190 			return -EINVAL;
9191 		}
9192 
9193 		conf = wiphy_dereference(wiphy, link_conf->chanctx_conf);
9194 		if (!conf)
9195 			return -ENOENT;
9196 
9197 		cur_tx_power = link_conf->txpower;
9198 		max_tx_power = min(conf->def.chan->max_reg_power,
9199 				   (int)ar->max_tx_power / 2);
9200 
9201 		ath12k_mac_op_get_txpower(hw, arvif->ahvif->vif, arvif->link_id,
9202 					  &cur_tx_power);
9203 
9204 		switch (action_code) {
9205 		case WLAN_RM_ACTION_LINK_MEASUREMENT_REQUEST:
9206 			/* need variable fields to be present in len */
9207 			if (remaining_len < 2)
9208 				return -EINVAL;
9209 
9210 			/* Variable length format as defined in IEEE 802.11-2024,
9211 			 * Figure 9-1187-Link Measurement Request frame Action field
9212 			 * format.
9213 			 *	Transmit Power | Max Tx Power
9214 			 * We fill both of these.
9215 			 */
9216 			*buf++ = cur_tx_power;
9217 			*buf = max_tx_power;
9218 
9219 			ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
9220 				   "RRM: Link Measurement Req dialog_token %u cur_tx_power %d max_tx_power %d\n",
9221 				   dialog_token, cur_tx_power, max_tx_power);
9222 			break;
9223 		case WLAN_RM_ACTION_LINK_MEASUREMENT_REPORT:
9224 			/* need variable fields to be present in len */
9225 			if (remaining_len < 3)
9226 				return -EINVAL;
9227 
9228 			/* Variable length format as defined in IEEE 802.11-2024,
9229 			 * Figure 9-1188-Link Measurement Report frame Action field format
9230 			 *	TPC Report | Variable Fields
9231 			 *
9232 			 * TPC Report Format:
9233 			 *	Element ID | Len | Tx Power | Link Margin
9234 			 *
9235 			 * We fill Tx power in the TPC Report (2nd index)
9236 			 */
9237 			buf[2] = cur_tx_power;
9238 
9239 			/* TODO: At present, Link margin data is not present so can't
9240 			 * really fill it now. Once it is available, it can be added
9241 			 * here
9242 			 */
9243 			ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
9244 				   "RRM: Link Measurement Report dialog_token %u cur_tx_power %d\n",
9245 				   dialog_token, cur_tx_power);
9246 			break;
9247 		default:
9248 			return -EINVAL;
9249 		}
9250 		break;
9251 	default:
9252 		/* nothing to fill */
9253 		return 0;
9254 	}
9255 
9256 	return 0;
9257 }
9258 
9259 static int ath12k_mac_mgmt_frame_fill_elem_data(struct ath12k_link_vif *arvif,
9260 						struct sk_buff *skb)
9261 {
9262 	struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
9263 
9264 	if (!ieee80211_is_action(hdr->frame_control))
9265 		return 0;
9266 
9267 	return ath12k_mac_mgmt_action_frame_fill_elem_data(arvif, skb);
9268 }
9269 
9270 static void ath12k_mgmt_over_wmi_tx_work(struct wiphy *wiphy, struct wiphy_work *work)
9271 {
9272 	struct ath12k *ar = container_of(work, struct ath12k, wmi_mgmt_tx_work);
9273 	struct ath12k_hw *ah = ar->ah;
9274 	struct ath12k_skb_cb *skb_cb;
9275 	struct ath12k_vif *ahvif;
9276 	struct ath12k_link_vif *arvif;
9277 	struct sk_buff *skb;
9278 	int ret;
9279 
9280 	lockdep_assert_wiphy(wiphy);
9281 
9282 	while ((skb = skb_dequeue(&ar->wmi_mgmt_tx_queue)) != NULL) {
9283 		skb_cb = ATH12K_SKB_CB(skb);
9284 		if (!skb_cb->vif) {
9285 			ath12k_warn(ar->ab, "no vif found for mgmt frame\n");
9286 			ath12k_mgmt_over_wmi_tx_drop(ar, skb);
9287 			continue;
9288 		}
9289 
9290 		ahvif = ath12k_vif_to_ahvif(skb_cb->vif);
9291 		if (!(ahvif->links_map & BIT(skb_cb->link_id))) {
9292 			ath12k_warn(ar->ab,
9293 				    "invalid linkid %u in mgmt over wmi tx with linkmap 0x%x\n",
9294 				    skb_cb->link_id, ahvif->links_map);
9295 			ath12k_mgmt_over_wmi_tx_drop(ar, skb);
9296 			continue;
9297 		}
9298 
9299 		arvif = wiphy_dereference(ah->hw->wiphy, ahvif->link[skb_cb->link_id]);
9300 		if (ar->allocated_vdev_map & (1LL << arvif->vdev_id)) {
9301 			/* Fill in the data which is required to be filled by the driver
9302 			 * For example: Max Tx power in Link Measurement Request/Report
9303 			 */
9304 			ret = ath12k_mac_mgmt_frame_fill_elem_data(arvif, skb);
9305 			if (ret) {
9306 				/* If we couldn't fill the data due to any reason,
9307 				 * let's not discard transmitting the packet.
9308 				 * For example: Software crypto and PMF case
9309 				 */
9310 				ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
9311 					   "Failed to fill the required data for the mgmt packet err %d\n",
9312 					   ret);
9313 			}
9314 
9315 			ret = ath12k_mac_mgmt_tx_wmi(ar, arvif, skb);
9316 			if (ret) {
9317 				ath12k_warn(ar->ab, "failed to tx mgmt frame, vdev_id %d :%d\n",
9318 					    arvif->vdev_id, ret);
9319 				ath12k_mgmt_over_wmi_tx_drop(ar, skb);
9320 			}
9321 		} else {
9322 			ath12k_warn(ar->ab,
9323 				    "dropping mgmt frame for vdev %d link %u is_started %d\n",
9324 				    arvif->vdev_id,
9325 				    skb_cb->link_id,
9326 				    arvif->is_started);
9327 			ath12k_mgmt_over_wmi_tx_drop(ar, skb);
9328 		}
9329 	}
9330 }
9331 
9332 int ath12k_mac_mgmt_tx(struct ath12k *ar, struct sk_buff *skb,
9333 		       bool is_prb_rsp)
9334 {
9335 	struct sk_buff_head *q = &ar->wmi_mgmt_tx_queue;
9336 
9337 	if (test_bit(ATH12K_FLAG_CRASH_FLUSH, &ar->ab->dev_flags))
9338 		return -ESHUTDOWN;
9339 
9340 	/* Drop probe response packets when the pending management tx
9341 	 * count has reached a certain threshold, so as to prioritize
9342 	 * other mgmt packets like auth and assoc to be sent on time
9343 	 * for establishing successful connections.
9344 	 */
9345 	if (is_prb_rsp &&
9346 	    atomic_read(&ar->num_pending_mgmt_tx) > ATH12K_PRB_RSP_DROP_THRESHOLD) {
9347 		ath12k_warn(ar->ab,
9348 			    "dropping probe response as pending queue is almost full\n");
9349 		return -ENOSPC;
9350 	}
9351 
9352 	if (skb_queue_len_lockless(q) >= ATH12K_TX_MGMT_NUM_PENDING_MAX) {
9353 		ath12k_warn(ar->ab, "mgmt tx queue is full\n");
9354 		return -ENOSPC;
9355 	}
9356 
9357 	skb_queue_tail(q, skb);
9358 	atomic_inc(&ar->num_pending_mgmt_tx);
9359 	wiphy_work_queue(ath12k_ar_to_hw(ar)->wiphy, &ar->wmi_mgmt_tx_work);
9360 
9361 	return 0;
9362 }
9363 EXPORT_SYMBOL(ath12k_mac_mgmt_tx);
9364 
9365 void ath12k_mac_add_p2p_noa_ie(struct ath12k *ar,
9366 			       struct ieee80211_vif *vif,
9367 			       struct sk_buff *skb,
9368 			       bool is_prb_rsp)
9369 {
9370 	struct ath12k_vif *ahvif = ath12k_vif_to_ahvif(vif);
9371 
9372 	if (likely(!is_prb_rsp))
9373 		return;
9374 
9375 	spin_lock_bh(&ar->data_lock);
9376 
9377 	if (ahvif->u.ap.noa_data &&
9378 	    !pskb_expand_head(skb, 0, ahvif->u.ap.noa_len,
9379 			      GFP_ATOMIC))
9380 		skb_put_data(skb, ahvif->u.ap.noa_data,
9381 			     ahvif->u.ap.noa_len);
9382 
9383 	spin_unlock_bh(&ar->data_lock);
9384 }
9385 EXPORT_SYMBOL(ath12k_mac_add_p2p_noa_ie);
9386 
9387 /* Note: called under rcu_read_lock() */
9388 void ath12k_mlo_mcast_update_tx_link_address(struct ieee80211_vif *vif,
9389 					     u8 link_id, struct sk_buff *skb,
9390 					     u32 info_flags)
9391 {
9392 	struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
9393 	struct ieee80211_bss_conf *bss_conf;
9394 
9395 	if (info_flags & IEEE80211_TX_CTL_HW_80211_ENCAP)
9396 		return;
9397 
9398 	bss_conf = rcu_dereference(vif->link_conf[link_id]);
9399 	if (bss_conf)
9400 		ether_addr_copy(hdr->addr2, bss_conf->addr);
9401 }
9402 EXPORT_SYMBOL(ath12k_mlo_mcast_update_tx_link_address);
9403 
9404 /* Note: called under rcu_read_lock() */
9405 u8 ath12k_mac_get_tx_link(struct ieee80211_sta *sta, struct ieee80211_vif *vif,
9406 			  u8 link, struct sk_buff *skb, u32 info_flags)
9407 {
9408 	struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
9409 	struct ath12k_vif *ahvif = ath12k_vif_to_ahvif(vif);
9410 	struct ieee80211_link_sta *link_sta;
9411 	struct ieee80211_bss_conf *bss_conf;
9412 	struct ath12k_sta *ahsta;
9413 
9414 	/* Use the link id passed or the default vif link */
9415 	if (!sta) {
9416 		if (link != IEEE80211_LINK_UNSPECIFIED)
9417 			return link;
9418 
9419 		return ahvif->deflink.link_id;
9420 	}
9421 
9422 	ahsta = ath12k_sta_to_ahsta(sta);
9423 
9424 	/* Below translation ensures we pass proper A2 & A3 for non ML clients.
9425 	 * Also it assumes for now support only for MLO AP in this path
9426 	 */
9427 	if (!sta->mlo) {
9428 		link = ahsta->deflink.link_id;
9429 
9430 		if (info_flags & IEEE80211_TX_CTL_HW_80211_ENCAP)
9431 			return link;
9432 
9433 		bss_conf = rcu_dereference(vif->link_conf[link]);
9434 		if (bss_conf) {
9435 			ether_addr_copy(hdr->addr2, bss_conf->addr);
9436 			if (!ieee80211_has_tods(hdr->frame_control) &&
9437 			    !ieee80211_has_fromds(hdr->frame_control))
9438 				ether_addr_copy(hdr->addr3, bss_conf->addr);
9439 		}
9440 
9441 		return link;
9442 	}
9443 
9444 	/* enqueue eth enacap & data frames on primary link, FW does link
9445 	 * selection and address translation.
9446 	 */
9447 	if (info_flags & IEEE80211_TX_CTL_HW_80211_ENCAP ||
9448 	    ieee80211_is_data(hdr->frame_control))
9449 		return ahsta->assoc_link_id;
9450 
9451 	/* 802.11 frame cases */
9452 	if (link == IEEE80211_LINK_UNSPECIFIED)
9453 		link = ahsta->deflink.link_id;
9454 
9455 	if (!ieee80211_is_mgmt(hdr->frame_control))
9456 		return link;
9457 
9458 	/* Perform address conversion for ML STA Tx */
9459 	bss_conf = rcu_dereference(vif->link_conf[link]);
9460 	link_sta = rcu_dereference(sta->link[link]);
9461 
9462 	if (bss_conf && link_sta) {
9463 		ether_addr_copy(hdr->addr1, link_sta->addr);
9464 		ether_addr_copy(hdr->addr2, bss_conf->addr);
9465 
9466 		if (vif->type == NL80211_IFTYPE_STATION && bss_conf->bssid)
9467 			ether_addr_copy(hdr->addr3, bss_conf->bssid);
9468 		else if (vif->type == NL80211_IFTYPE_AP)
9469 			ether_addr_copy(hdr->addr3, bss_conf->addr);
9470 
9471 		return link;
9472 	}
9473 
9474 	if (bss_conf) {
9475 		/* In certain cases where a ML sta associated and added subset of
9476 		 * links on which the ML AP is active, but now sends some frame
9477 		 * (ex. Probe request) on a different link which is active in our
9478 		 * MLD but was not added during previous association, we can
9479 		 * still honor the Tx to that ML STA via the requested link.
9480 		 * The control would reach here in such case only when that link
9481 		 * address is same as the MLD address or in worst case clients
9482 		 * used MLD address at TA wrongly which would have helped
9483 		 * identify the ML sta object and pass it here.
9484 		 * If the link address of that STA is different from MLD address,
9485 		 * then the sta object would be NULL and control won't reach
9486 		 * here but return at the start of the function itself with !sta
9487 		 * check. Also this would not need any translation at hdr->addr1
9488 		 * from MLD to link address since the RA is the MLD address
9489 		 * (same as that link address ideally) already.
9490 		 */
9491 		ether_addr_copy(hdr->addr2, bss_conf->addr);
9492 
9493 		if (vif->type == NL80211_IFTYPE_STATION && bss_conf->bssid)
9494 			ether_addr_copy(hdr->addr3, bss_conf->bssid);
9495 		else if (vif->type == NL80211_IFTYPE_AP)
9496 			ether_addr_copy(hdr->addr3, bss_conf->addr);
9497 	}
9498 
9499 	return link;
9500 }
9501 EXPORT_SYMBOL(ath12k_mac_get_tx_link);
9502 
9503 void ath12k_mac_drain_tx(struct ath12k *ar)
9504 {
9505 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
9506 
9507 	/* make sure rcu-protected mac80211 tx path itself is drained */
9508 	synchronize_net();
9509 
9510 	wiphy_work_cancel(ath12k_ar_to_hw(ar)->wiphy, &ar->wmi_mgmt_tx_work);
9511 	ath12k_mgmt_over_wmi_tx_purge(ar);
9512 }
9513 
9514 static int ath12k_mac_config_mon_status_default(struct ath12k *ar, bool enable)
9515 {
9516 	struct htt_rx_ring_tlv_filter tlv_filter = {};
9517 	struct ath12k_base *ab = ar->ab;
9518 	u32 ring_id, i;
9519 	int ret = 0;
9520 
9521 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
9522 
9523 	if (!ab->hw_params->rxdma1_enable)
9524 		return ret;
9525 
9526 	if (enable) {
9527 		tlv_filter = ath12k_mac_mon_status_filter_default;
9528 
9529 		if (ath12k_debugfs_rx_filter(ar))
9530 			tlv_filter.rx_filter = ath12k_debugfs_rx_filter(ar);
9531 	} else {
9532 		tlv_filter.rxmon_disable = true;
9533 	}
9534 
9535 	for (i = 0; i < ab->hw_params->num_rxdma_per_pdev; i++) {
9536 		ring_id = ar->dp.rxdma_mon_dst_ring[i].ring_id;
9537 		ret = ath12k_dp_tx_htt_rx_filter_setup(ab, ring_id,
9538 						       ar->dp.mac_id + i,
9539 						       HAL_RXDMA_MONITOR_DST,
9540 						       DP_RXDMA_REFILL_RING_SIZE,
9541 						       &tlv_filter);
9542 		if (ret) {
9543 			ath12k_err(ab,
9544 				   "failed to setup filter for monitor buf %d\n",
9545 				   ret);
9546 		}
9547 	}
9548 
9549 	return ret;
9550 }
9551 
9552 static int ath12k_mac_start(struct ath12k *ar)
9553 {
9554 	struct ath12k_hw *ah = ar->ah;
9555 	struct ath12k_base *ab = ar->ab;
9556 	struct ath12k_pdev *pdev = ar->pdev;
9557 	int ret;
9558 
9559 	lockdep_assert_held(&ah->hw_mutex);
9560 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
9561 
9562 	ret = ath12k_wmi_pdev_set_param(ar, WMI_PDEV_PARAM_PMF_QOS,
9563 					1, pdev->pdev_id);
9564 
9565 	if (ret) {
9566 		ath12k_err(ab, "failed to enable PMF QOS: %d\n", ret);
9567 		goto err;
9568 	}
9569 
9570 	ret = ath12k_wmi_pdev_set_param(ar, WMI_PDEV_PARAM_DYNAMIC_BW, 1,
9571 					pdev->pdev_id);
9572 	if (ret) {
9573 		ath12k_err(ab, "failed to enable dynamic bw: %d\n", ret);
9574 		goto err;
9575 	}
9576 
9577 	ret = ath12k_wmi_pdev_set_param(ar, WMI_PDEV_PARAM_ARP_AC_OVERRIDE,
9578 					0, pdev->pdev_id);
9579 	if (ret) {
9580 		ath12k_err(ab, "failed to set ac override for ARP: %d\n",
9581 			   ret);
9582 		goto err;
9583 	}
9584 
9585 	ret = ath12k_wmi_send_dfs_phyerr_offload_enable_cmd(ar, pdev->pdev_id);
9586 	if (ret) {
9587 		ath12k_err(ab, "failed to offload radar detection: %d\n",
9588 			   ret);
9589 		goto err;
9590 	}
9591 
9592 	ret = ath12k_dp_tx_htt_h2t_ppdu_stats_req(ar,
9593 						  HTT_PPDU_STATS_TAG_DEFAULT);
9594 	if (ret) {
9595 		ath12k_err(ab, "failed to req ppdu stats: %d\n", ret);
9596 		goto err;
9597 	}
9598 
9599 	ret = ath12k_wmi_pdev_set_param(ar, WMI_PDEV_PARAM_MESH_MCAST_ENABLE,
9600 					1, pdev->pdev_id);
9601 
9602 	if (ret) {
9603 		ath12k_err(ab, "failed to enable MESH MCAST ENABLE: (%d\n", ret);
9604 		goto err;
9605 	}
9606 
9607 	__ath12k_set_antenna(ar, ar->cfg_tx_chainmask, ar->cfg_rx_chainmask);
9608 
9609 	/* TODO: Do we need to enable ANI? */
9610 
9611 	ret = ath12k_reg_update_chan_list(ar, false);
9612 
9613 	/* The ar state alone can be turned off for non supported country
9614 	 * without returning the error value. As we need to update the channel
9615 	 * for the next ar.
9616 	 */
9617 	if (ret) {
9618 		if (ret == -EINVAL)
9619 			ret = 0;
9620 		goto err;
9621 	}
9622 
9623 	ar->num_started_vdevs = 0;
9624 	ar->num_created_vdevs = 0;
9625 	ar->num_peers = 0;
9626 	ar->allocated_vdev_map = 0;
9627 	ar->chan_tx_pwr = ATH12K_PDEV_TX_POWER_INVALID;
9628 
9629 	/* Configure monitor status ring with default rx_filter to get rx status
9630 	 * such as rssi, rx_duration.
9631 	 */
9632 	ret = ath12k_mac_config_mon_status_default(ar, true);
9633 	if (ret && (ret != -EOPNOTSUPP)) {
9634 		ath12k_err(ab, "failed to configure monitor status ring with default rx_filter: (%d)\n",
9635 			   ret);
9636 		goto err;
9637 	}
9638 
9639 	if (ret == -EOPNOTSUPP)
9640 		ath12k_dbg(ab, ATH12K_DBG_MAC,
9641 			   "monitor status config is not yet supported");
9642 
9643 	/* Configure the hash seed for hash based reo dest ring selection */
9644 	ath12k_wmi_pdev_lro_cfg(ar, ar->pdev->pdev_id);
9645 
9646 	/* allow device to enter IMPS */
9647 	if (ab->hw_params->idle_ps) {
9648 		ret = ath12k_wmi_pdev_set_param(ar, WMI_PDEV_PARAM_IDLE_PS_CONFIG,
9649 						1, pdev->pdev_id);
9650 		if (ret) {
9651 			ath12k_err(ab, "failed to enable idle ps: %d\n", ret);
9652 			goto err;
9653 		}
9654 	}
9655 
9656 	rcu_assign_pointer(ab->pdevs_active[ar->pdev_idx],
9657 			   &ab->pdevs[ar->pdev_idx]);
9658 
9659 	return 0;
9660 err:
9661 
9662 	return ret;
9663 }
9664 
9665 static void ath12k_drain_tx(struct ath12k_hw *ah)
9666 {
9667 	struct ath12k *ar;
9668 	int i;
9669 
9670 	lockdep_assert_wiphy(ah->hw->wiphy);
9671 
9672 	for_each_ar(ah, ar, i)
9673 		ath12k_mac_drain_tx(ar);
9674 }
9675 
9676 int ath12k_mac_op_start(struct ieee80211_hw *hw)
9677 {
9678 	struct ath12k_hw *ah = ath12k_hw_to_ah(hw);
9679 	struct ath12k *ar;
9680 	int ret, i;
9681 
9682 	if (ath12k_ftm_mode)
9683 		return -EPERM;
9684 
9685 	lockdep_assert_wiphy(hw->wiphy);
9686 
9687 	ath12k_drain_tx(ah);
9688 
9689 	guard(mutex)(&ah->hw_mutex);
9690 
9691 	switch (ah->state) {
9692 	case ATH12K_HW_STATE_OFF:
9693 		ah->state = ATH12K_HW_STATE_ON;
9694 		break;
9695 	case ATH12K_HW_STATE_RESTARTING:
9696 		ah->state = ATH12K_HW_STATE_RESTARTED;
9697 		break;
9698 	case ATH12K_HW_STATE_RESTARTED:
9699 	case ATH12K_HW_STATE_WEDGED:
9700 	case ATH12K_HW_STATE_ON:
9701 	case ATH12K_HW_STATE_TM:
9702 		ah->state = ATH12K_HW_STATE_OFF;
9703 
9704 		WARN_ON(1);
9705 		return -EINVAL;
9706 	}
9707 
9708 	for_each_ar(ah, ar, i) {
9709 		ret = ath12k_mac_start(ar);
9710 		if (ret) {
9711 			ah->state = ATH12K_HW_STATE_OFF;
9712 
9713 			ath12k_err(ar->ab, "fail to start mac operations in pdev idx %d ret %d\n",
9714 				   ar->pdev_idx, ret);
9715 			goto fail_start;
9716 		}
9717 	}
9718 
9719 	return 0;
9720 
9721 fail_start:
9722 	for (; i > 0; i--) {
9723 		ar = ath12k_ah_to_ar(ah, i - 1);
9724 		ath12k_mac_stop(ar);
9725 	}
9726 
9727 	return ret;
9728 }
9729 EXPORT_SYMBOL(ath12k_mac_op_start);
9730 
9731 int ath12k_mac_rfkill_config(struct ath12k *ar)
9732 {
9733 	struct ath12k_base *ab = ar->ab;
9734 	u32 param;
9735 	int ret;
9736 
9737 	if (ab->hw_params->rfkill_pin == 0)
9738 		return -EOPNOTSUPP;
9739 
9740 	ath12k_dbg(ab, ATH12K_DBG_MAC,
9741 		   "mac rfkill_pin %d rfkill_cfg %d rfkill_on_level %d",
9742 		   ab->hw_params->rfkill_pin, ab->hw_params->rfkill_cfg,
9743 		   ab->hw_params->rfkill_on_level);
9744 
9745 	param = u32_encode_bits(ab->hw_params->rfkill_on_level,
9746 				WMI_RFKILL_CFG_RADIO_LEVEL) |
9747 		u32_encode_bits(ab->hw_params->rfkill_pin,
9748 				WMI_RFKILL_CFG_GPIO_PIN_NUM) |
9749 		u32_encode_bits(ab->hw_params->rfkill_cfg,
9750 				WMI_RFKILL_CFG_PIN_AS_GPIO);
9751 
9752 	ret = ath12k_wmi_pdev_set_param(ar, WMI_PDEV_PARAM_HW_RFKILL_CONFIG,
9753 					param, ar->pdev->pdev_id);
9754 	if (ret) {
9755 		ath12k_warn(ab,
9756 			    "failed to set rfkill config 0x%x: %d\n",
9757 			    param, ret);
9758 		return ret;
9759 	}
9760 
9761 	return 0;
9762 }
9763 
9764 int ath12k_mac_rfkill_enable_radio(struct ath12k *ar, bool enable)
9765 {
9766 	enum wmi_rfkill_enable_radio param;
9767 	int ret;
9768 
9769 	if (enable)
9770 		param = WMI_RFKILL_ENABLE_RADIO_ON;
9771 	else
9772 		param = WMI_RFKILL_ENABLE_RADIO_OFF;
9773 
9774 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac %d rfkill enable %d",
9775 		   ar->pdev_idx, param);
9776 
9777 	ret = ath12k_wmi_pdev_set_param(ar, WMI_PDEV_PARAM_RFKILL_ENABLE,
9778 					param, ar->pdev->pdev_id);
9779 	if (ret) {
9780 		ath12k_warn(ar->ab, "failed to set rfkill enable param %d: %d\n",
9781 			    param, ret);
9782 		return ret;
9783 	}
9784 
9785 	return 0;
9786 }
9787 
9788 static void ath12k_mac_stop(struct ath12k *ar)
9789 {
9790 	struct ath12k_pdev_dp *dp_pdev = &ar->dp;
9791 	struct ath12k_hw *ah = ar->ah;
9792 	struct htt_ppdu_stats_info *ppdu_stats, *tmp;
9793 	struct ath12k_wmi_scan_chan_list_arg *arg;
9794 	int ret;
9795 
9796 	lockdep_assert_held(&ah->hw_mutex);
9797 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
9798 
9799 	ret = ath12k_mac_config_mon_status_default(ar, false);
9800 	if (ret && (ret != -EOPNOTSUPP))
9801 		ath12k_err(ar->ab, "failed to clear rx_filter for monitor status ring: (%d)\n",
9802 			   ret);
9803 
9804 	clear_bit(ATH12K_FLAG_CAC_RUNNING, &ar->dev_flags);
9805 
9806 	cancel_delayed_work_sync(&ar->scan.timeout);
9807 	wiphy_work_cancel(ath12k_ar_to_hw(ar)->wiphy, &ar->scan.vdev_clean_wk);
9808 	cancel_work_sync(&ar->regd_channel_update_work);
9809 	cancel_work_sync(&ar->regd_update_work);
9810 	cancel_work_sync(&ar->ab->rfkill_work);
9811 	cancel_work_sync(&ar->ab->update_11d_work);
9812 	ar->state_11d = ATH12K_11D_IDLE;
9813 	complete(&ar->completed_11d_scan);
9814 
9815 	spin_lock_bh(&dp_pdev->ppdu_list_lock);
9816 	list_for_each_entry_safe(ppdu_stats, tmp, &dp_pdev->ppdu_stats_info, list) {
9817 		list_del(&ppdu_stats->list);
9818 		kfree(ppdu_stats);
9819 	}
9820 	spin_unlock_bh(&dp_pdev->ppdu_list_lock);
9821 
9822 	spin_lock_bh(&ar->data_lock);
9823 	while ((arg = list_first_entry_or_null(&ar->regd_channel_update_queue,
9824 					       struct ath12k_wmi_scan_chan_list_arg,
9825 					       list))) {
9826 		list_del(&arg->list);
9827 		kfree(arg);
9828 	}
9829 	spin_unlock_bh(&ar->data_lock);
9830 
9831 	rcu_assign_pointer(ar->ab->pdevs_active[ar->pdev_idx], NULL);
9832 
9833 	synchronize_rcu();
9834 
9835 	atomic_set(&ar->num_pending_mgmt_tx, 0);
9836 }
9837 
9838 void ath12k_mac_op_stop(struct ieee80211_hw *hw, bool suspend)
9839 {
9840 	struct ath12k_hw *ah = ath12k_hw_to_ah(hw);
9841 	struct ath12k *ar;
9842 	int i;
9843 
9844 	lockdep_assert_wiphy(hw->wiphy);
9845 
9846 	ath12k_drain_tx(ah);
9847 
9848 	mutex_lock(&ah->hw_mutex);
9849 
9850 	ah->state = ATH12K_HW_STATE_OFF;
9851 
9852 	for_each_ar(ah, ar, i)
9853 		ath12k_mac_stop(ar);
9854 
9855 	mutex_unlock(&ah->hw_mutex);
9856 }
9857 EXPORT_SYMBOL(ath12k_mac_op_stop);
9858 
9859 static u8
9860 ath12k_mac_get_vdev_stats_id(struct ath12k_link_vif *arvif)
9861 {
9862 	struct ath12k_base *ab = arvif->ar->ab;
9863 	u8 vdev_stats_id = 0;
9864 
9865 	do {
9866 		if (ab->free_vdev_stats_id_map & (1LL << vdev_stats_id)) {
9867 			vdev_stats_id++;
9868 			if (vdev_stats_id >= ATH12K_MAX_VDEV_STATS_ID) {
9869 				vdev_stats_id = ATH12K_INVAL_VDEV_STATS_ID;
9870 				break;
9871 			}
9872 		} else {
9873 			ab->free_vdev_stats_id_map |= (1LL << vdev_stats_id);
9874 			break;
9875 		}
9876 	} while (vdev_stats_id);
9877 
9878 	arvif->vdev_stats_id = vdev_stats_id;
9879 	return vdev_stats_id;
9880 }
9881 
9882 static int ath12k_mac_setup_vdev_params_mbssid(struct ath12k_link_vif *arvif,
9883 					       u32 *flags, u32 *tx_vdev_id)
9884 {
9885 	struct ath12k_vif *ahvif = arvif->ahvif;
9886 	struct ieee80211_bss_conf *link_conf;
9887 	struct ath12k *ar = arvif->ar;
9888 	struct ath12k_link_vif *tx_arvif;
9889 
9890 	link_conf = ath12k_mac_get_link_bss_conf(arvif);
9891 	if (!link_conf) {
9892 		ath12k_warn(ar->ab, "unable to access bss link conf in set mbssid params for vif %pM link %u\n",
9893 			    ahvif->vif->addr, arvif->link_id);
9894 		return -ENOLINK;
9895 	}
9896 
9897 	tx_arvif = ath12k_mac_get_tx_arvif(arvif, link_conf);
9898 	if (!tx_arvif)
9899 		return 0;
9900 
9901 	if (link_conf->nontransmitted) {
9902 		if (ath12k_ar_to_hw(ar)->wiphy !=
9903 		    ath12k_ar_to_hw(tx_arvif->ar)->wiphy)
9904 			return -EINVAL;
9905 
9906 		*flags = WMI_VDEV_MBSSID_FLAGS_NON_TRANSMIT_AP;
9907 		*tx_vdev_id = tx_arvif->vdev_id;
9908 	} else if (tx_arvif == arvif) {
9909 		*flags = WMI_VDEV_MBSSID_FLAGS_TRANSMIT_AP;
9910 	} else {
9911 		return -EINVAL;
9912 	}
9913 
9914 	if (link_conf->ema_ap)
9915 		*flags |= WMI_VDEV_MBSSID_FLAGS_EMA_MODE;
9916 
9917 	return 0;
9918 }
9919 
9920 static int ath12k_mac_setup_vdev_create_arg(struct ath12k_link_vif *arvif,
9921 					    struct ath12k_wmi_vdev_create_arg *arg)
9922 {
9923 	struct ath12k *ar = arvif->ar;
9924 	struct ath12k_pdev *pdev = ar->pdev;
9925 	struct ath12k_vif *ahvif = arvif->ahvif;
9926 	int ret;
9927 
9928 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
9929 
9930 	arg->if_id = arvif->vdev_id;
9931 	arg->type = ahvif->vdev_type;
9932 	arg->subtype = ahvif->vdev_subtype;
9933 	arg->pdev_id = pdev->pdev_id;
9934 
9935 	arg->mbssid_flags = WMI_VDEV_MBSSID_FLAGS_NON_MBSSID_AP;
9936 	arg->mbssid_tx_vdev_id = 0;
9937 	if (!test_bit(WMI_TLV_SERVICE_MBSS_PARAM_IN_VDEV_START_SUPPORT,
9938 		      ar->ab->wmi_ab.svc_map)) {
9939 		ret = ath12k_mac_setup_vdev_params_mbssid(arvif,
9940 							  &arg->mbssid_flags,
9941 							  &arg->mbssid_tx_vdev_id);
9942 		if (ret)
9943 			return ret;
9944 	}
9945 
9946 	if (pdev->cap.supported_bands & WMI_HOST_WLAN_2GHZ_CAP) {
9947 		arg->chains[NL80211_BAND_2GHZ].tx = ar->num_tx_chains;
9948 		arg->chains[NL80211_BAND_2GHZ].rx = ar->num_rx_chains;
9949 	}
9950 	if (pdev->cap.supported_bands & WMI_HOST_WLAN_5GHZ_CAP) {
9951 		arg->chains[NL80211_BAND_5GHZ].tx = ar->num_tx_chains;
9952 		arg->chains[NL80211_BAND_5GHZ].rx = ar->num_rx_chains;
9953 	}
9954 	if (pdev->cap.supported_bands & WMI_HOST_WLAN_5GHZ_CAP &&
9955 	    ar->supports_6ghz) {
9956 		arg->chains[NL80211_BAND_6GHZ].tx = ar->num_tx_chains;
9957 		arg->chains[NL80211_BAND_6GHZ].rx = ar->num_rx_chains;
9958 	}
9959 
9960 	arg->if_stats_id = ath12k_mac_get_vdev_stats_id(arvif);
9961 
9962 	if (ath12k_mac_is_ml_arvif(arvif)) {
9963 		if (hweight16(ahvif->vif->valid_links) > ATH12K_WMI_MLO_MAX_LINKS) {
9964 			ath12k_warn(ar->ab, "too many MLO links during setting up vdev: %d",
9965 				    ahvif->vif->valid_links);
9966 			return -EINVAL;
9967 		}
9968 
9969 		ether_addr_copy(arg->mld_addr, ahvif->vif->addr);
9970 	}
9971 
9972 	return 0;
9973 }
9974 
9975 static void ath12k_mac_update_vif_offload(struct ath12k_link_vif *arvif)
9976 {
9977 	struct ath12k_vif *ahvif = arvif->ahvif;
9978 	struct ieee80211_vif *vif = ath12k_ahvif_to_vif(ahvif);
9979 	struct ath12k *ar = arvif->ar;
9980 	struct ath12k_base *ab = ar->ab;
9981 	u32 param_id, param_value;
9982 	int ret;
9983 
9984 	param_id = WMI_VDEV_PARAM_TX_ENCAP_TYPE;
9985 	if (vif->type != NL80211_IFTYPE_STATION &&
9986 	    vif->type != NL80211_IFTYPE_AP)
9987 		vif->offload_flags &= ~(IEEE80211_OFFLOAD_ENCAP_ENABLED |
9988 					IEEE80211_OFFLOAD_DECAP_ENABLED);
9989 
9990 	if (vif->offload_flags & IEEE80211_OFFLOAD_ENCAP_ENABLED)
9991 		ahvif->dp_vif.tx_encap_type = ATH12K_HW_TXRX_ETHERNET;
9992 	else if (test_bit(ATH12K_FLAG_RAW_MODE, &ab->dev_flags))
9993 		ahvif->dp_vif.tx_encap_type = ATH12K_HW_TXRX_RAW;
9994 	else
9995 		ahvif->dp_vif.tx_encap_type = ATH12K_HW_TXRX_NATIVE_WIFI;
9996 
9997 	ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
9998 					    param_id, ahvif->dp_vif.tx_encap_type);
9999 	if (ret) {
10000 		ath12k_warn(ab, "failed to set vdev %d tx encap mode: %d\n",
10001 			    arvif->vdev_id, ret);
10002 		vif->offload_flags &= ~IEEE80211_OFFLOAD_ENCAP_ENABLED;
10003 	}
10004 
10005 	param_id = WMI_VDEV_PARAM_RX_DECAP_TYPE;
10006 	if (vif->offload_flags & IEEE80211_OFFLOAD_DECAP_ENABLED)
10007 		param_value = ATH12K_HW_TXRX_ETHERNET;
10008 	else if (test_bit(ATH12K_FLAG_RAW_MODE, &ab->dev_flags))
10009 		param_value = ATH12K_HW_TXRX_RAW;
10010 	else
10011 		param_value = ATH12K_HW_TXRX_NATIVE_WIFI;
10012 
10013 	ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
10014 					    param_id, param_value);
10015 	if (ret) {
10016 		ath12k_warn(ab, "failed to set vdev %d rx decap mode: %d\n",
10017 			    arvif->vdev_id, ret);
10018 		vif->offload_flags &= ~IEEE80211_OFFLOAD_DECAP_ENABLED;
10019 	}
10020 }
10021 
10022 void ath12k_mac_op_update_vif_offload(struct ieee80211_hw *hw,
10023 				      struct ieee80211_vif *vif)
10024 {
10025 	struct ath12k_vif *ahvif = ath12k_vif_to_ahvif(vif);
10026 	struct ath12k_link_vif *arvif;
10027 	unsigned long links;
10028 	int link_id;
10029 
10030 	lockdep_assert_wiphy(hw->wiphy);
10031 
10032 	if (vif->valid_links) {
10033 		links = vif->valid_links;
10034 		for_each_set_bit(link_id, &links, IEEE80211_MLD_MAX_NUM_LINKS) {
10035 			arvif = wiphy_dereference(hw->wiphy, ahvif->link[link_id]);
10036 			if (!(arvif && arvif->ar))
10037 				continue;
10038 
10039 			ath12k_mac_update_vif_offload(arvif);
10040 		}
10041 
10042 		return;
10043 	}
10044 
10045 	ath12k_mac_update_vif_offload(&ahvif->deflink);
10046 }
10047 EXPORT_SYMBOL(ath12k_mac_op_update_vif_offload);
10048 
10049 static bool ath12k_mac_vif_ap_active_any(struct ath12k_base *ab)
10050 {
10051 	struct ath12k *ar;
10052 	struct ath12k_pdev *pdev;
10053 	struct ath12k_link_vif *arvif;
10054 	int i;
10055 
10056 	for (i = 0; i < ab->num_radios; i++) {
10057 		pdev = &ab->pdevs[i];
10058 		ar = pdev->ar;
10059 		list_for_each_entry(arvif, &ar->arvifs, list) {
10060 			if (arvif->is_up &&
10061 			    arvif->ahvif->vdev_type == WMI_VDEV_TYPE_AP)
10062 				return true;
10063 		}
10064 	}
10065 	return false;
10066 }
10067 
10068 void ath12k_mac_11d_scan_start(struct ath12k *ar, u32 vdev_id)
10069 {
10070 	struct wmi_11d_scan_start_arg arg;
10071 	int ret;
10072 
10073 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
10074 
10075 	if (ar->regdom_set_by_user)
10076 		goto fin;
10077 
10078 	if (ar->vdev_id_11d_scan != ATH12K_11D_INVALID_VDEV_ID)
10079 		goto fin;
10080 
10081 	if (!test_bit(WMI_TLV_SERVICE_11D_OFFLOAD, ar->ab->wmi_ab.svc_map))
10082 		goto fin;
10083 
10084 	if (ath12k_mac_vif_ap_active_any(ar->ab))
10085 		goto fin;
10086 
10087 	arg.vdev_id = vdev_id;
10088 	arg.start_interval_msec = 0;
10089 	arg.scan_period_msec = ATH12K_SCAN_11D_INTERVAL;
10090 
10091 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
10092 		   "mac start 11d scan for vdev %d\n", vdev_id);
10093 
10094 	ret = ath12k_wmi_send_11d_scan_start_cmd(ar, &arg);
10095 	if (ret) {
10096 		ath12k_warn(ar->ab, "failed to start 11d scan vdev %d ret: %d\n",
10097 			    vdev_id, ret);
10098 	} else {
10099 		ar->vdev_id_11d_scan = vdev_id;
10100 		if (ar->state_11d == ATH12K_11D_PREPARING)
10101 			ar->state_11d = ATH12K_11D_RUNNING;
10102 	}
10103 
10104 fin:
10105 	if (ar->state_11d == ATH12K_11D_PREPARING) {
10106 		ar->state_11d = ATH12K_11D_IDLE;
10107 		complete(&ar->completed_11d_scan);
10108 	}
10109 }
10110 
10111 void ath12k_mac_11d_scan_stop(struct ath12k *ar)
10112 {
10113 	int ret;
10114 	u32 vdev_id;
10115 
10116 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
10117 
10118 	if (!test_bit(WMI_TLV_SERVICE_11D_OFFLOAD, ar->ab->wmi_ab.svc_map))
10119 		return;
10120 
10121 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac stop 11d for vdev %d\n",
10122 		   ar->vdev_id_11d_scan);
10123 
10124 	if (ar->state_11d == ATH12K_11D_PREPARING) {
10125 		ar->state_11d = ATH12K_11D_IDLE;
10126 		complete(&ar->completed_11d_scan);
10127 	}
10128 
10129 	if (ar->vdev_id_11d_scan != ATH12K_11D_INVALID_VDEV_ID) {
10130 		vdev_id = ar->vdev_id_11d_scan;
10131 
10132 		ret = ath12k_wmi_send_11d_scan_stop_cmd(ar, vdev_id);
10133 		if (ret) {
10134 			ath12k_warn(ar->ab,
10135 				    "failed to stopt 11d scan vdev %d ret: %d\n",
10136 				    vdev_id, ret);
10137 		} else {
10138 			ar->vdev_id_11d_scan = ATH12K_11D_INVALID_VDEV_ID;
10139 			ar->state_11d = ATH12K_11D_IDLE;
10140 			complete(&ar->completed_11d_scan);
10141 		}
10142 	}
10143 }
10144 
10145 void ath12k_mac_11d_scan_stop_all(struct ath12k_base *ab)
10146 {
10147 	struct ath12k *ar;
10148 	struct ath12k_pdev *pdev;
10149 	int i;
10150 
10151 	ath12k_dbg(ab, ATH12K_DBG_MAC, "mac stop soc 11d scan\n");
10152 
10153 	for (i = 0; i < ab->num_radios; i++) {
10154 		pdev = &ab->pdevs[i];
10155 		ar = pdev->ar;
10156 
10157 		ath12k_mac_11d_scan_stop(ar);
10158 	}
10159 }
10160 
10161 static void ath12k_mac_determine_vdev_type(struct ieee80211_vif *vif,
10162 					   struct ath12k_vif *ahvif)
10163 {
10164 	ahvif->vdev_subtype = WMI_VDEV_SUBTYPE_NONE;
10165 
10166 	switch (vif->type) {
10167 	case NL80211_IFTYPE_UNSPECIFIED:
10168 	case NL80211_IFTYPE_STATION:
10169 		ahvif->vdev_type = WMI_VDEV_TYPE_STA;
10170 
10171 		if (vif->p2p)
10172 			ahvif->vdev_subtype = WMI_VDEV_SUBTYPE_P2P_CLIENT;
10173 
10174 		break;
10175 	case NL80211_IFTYPE_MESH_POINT:
10176 		ahvif->vdev_subtype = WMI_VDEV_SUBTYPE_MESH_11S;
10177 		fallthrough;
10178 	case NL80211_IFTYPE_AP:
10179 		ahvif->vdev_type = WMI_VDEV_TYPE_AP;
10180 
10181 		if (vif->p2p)
10182 			ahvif->vdev_subtype = WMI_VDEV_SUBTYPE_P2P_GO;
10183 
10184 		break;
10185 	case NL80211_IFTYPE_MONITOR:
10186 		ahvif->vdev_type = WMI_VDEV_TYPE_MONITOR;
10187 		break;
10188 	case NL80211_IFTYPE_P2P_DEVICE:
10189 		ahvif->vdev_type = WMI_VDEV_TYPE_STA;
10190 		ahvif->vdev_subtype = WMI_VDEV_SUBTYPE_P2P_DEVICE;
10191 		break;
10192 	default:
10193 		WARN_ON(1);
10194 		break;
10195 	}
10196 }
10197 
10198 int ath12k_mac_vdev_create(struct ath12k *ar, struct ath12k_link_vif *arvif)
10199 {
10200 	struct ath12k_hw *ah = ar->ah;
10201 	struct ath12k_base *ab = ar->ab;
10202 	struct ieee80211_hw *hw = ah->hw;
10203 	struct ath12k_vif *ahvif = arvif->ahvif;
10204 	struct ieee80211_vif *vif = ath12k_ahvif_to_vif(ahvif);
10205 	struct ath12k_wmi_vdev_create_arg vdev_arg = {};
10206 	struct ath12k_wmi_peer_create_arg peer_param = {};
10207 	struct ieee80211_bss_conf *link_conf = NULL;
10208 	u32 param_id, param_value;
10209 	u16 nss;
10210 	int i;
10211 	int ret, vdev_id;
10212 	u8 link_id;
10213 	struct ath12k_dp_link_vif *dp_link_vif = NULL;
10214 	struct ath12k_dp_peer_create_params params = {};
10215 	bool dp_peer_created = false;
10216 
10217 	lockdep_assert_wiphy(hw->wiphy);
10218 
10219 	/* In NO_VIRTUAL_MONITOR, its necessary to restrict only one monitor
10220 	 * interface in each radio
10221 	 */
10222 	if (vif->type == NL80211_IFTYPE_MONITOR && ar->monitor_vdev_created)
10223 		return -EINVAL;
10224 
10225 	if (ar->num_created_vdevs >= TARGET_NUM_VDEVS(ab)) {
10226 		ath12k_warn(ab, "failed to create vdev, reached max vdev limit %d\n",
10227 			    TARGET_NUM_VDEVS(ab));
10228 		return -ENOSPC;
10229 	}
10230 
10231 	link_id = arvif->link_id;
10232 
10233 	if (link_id < IEEE80211_MLD_MAX_NUM_LINKS) {
10234 		link_conf = wiphy_dereference(hw->wiphy, vif->link_conf[link_id]);
10235 		if (!link_conf) {
10236 			ath12k_warn(ar->ab, "unable to access bss link conf in vdev create for vif %pM link %u\n",
10237 				    vif->addr, arvif->link_id);
10238 			return -ENOLINK;
10239 		}
10240 	}
10241 
10242 	if (link_conf)
10243 		memcpy(arvif->bssid, link_conf->addr, ETH_ALEN);
10244 	else
10245 		memcpy(arvif->bssid, vif->addr, ETH_ALEN);
10246 
10247 	arvif->ar = ar;
10248 	vdev_id = __ffs64(ab->free_vdev_map);
10249 	arvif->vdev_id = vdev_id;
10250 	if (vif->type == NL80211_IFTYPE_MONITOR)
10251 		ar->monitor_vdev_id = vdev_id;
10252 
10253 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac vdev create id %d type %d subtype %d map %llx\n",
10254 		   arvif->vdev_id, ahvif->vdev_type, ahvif->vdev_subtype,
10255 		   ab->free_vdev_map);
10256 
10257 	vif->cab_queue = arvif->vdev_id % (ATH12K_HW_MAX_QUEUES - 1);
10258 	for (i = 0; i < ARRAY_SIZE(vif->hw_queue); i++)
10259 		vif->hw_queue[i] = i % (ATH12K_HW_MAX_QUEUES - 1);
10260 
10261 	ret = ath12k_mac_setup_vdev_create_arg(arvif, &vdev_arg);
10262 	if (ret) {
10263 		ath12k_warn(ab, "failed to create vdev parameters %d: %d\n",
10264 			    arvif->vdev_id, ret);
10265 		goto err;
10266 	}
10267 
10268 	ret = ath12k_wmi_vdev_create(ar, arvif->bssid, &vdev_arg);
10269 	if (ret) {
10270 		ath12k_warn(ab, "failed to create WMI vdev %d: %d\n",
10271 			    arvif->vdev_id, ret);
10272 		return ret;
10273 	}
10274 
10275 	ar->num_created_vdevs++;
10276 	arvif->is_created = true;
10277 	ath12k_dbg(ab, ATH12K_DBG_MAC, "vdev %pM created, vdev_id %d\n",
10278 		   vif->addr, arvif->vdev_id);
10279 	ar->allocated_vdev_map |= 1LL << arvif->vdev_id;
10280 	ab->free_vdev_map &= ~(1LL << arvif->vdev_id);
10281 
10282 	spin_lock_bh(&ar->data_lock);
10283 	list_add(&arvif->list, &ar->arvifs);
10284 	spin_unlock_bh(&ar->data_lock);
10285 
10286 	ath12k_mac_update_vif_offload(arvif);
10287 
10288 	nss = hweight32(ar->cfg_tx_chainmask) ? : 1;
10289 	ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
10290 					    WMI_VDEV_PARAM_NSS, nss);
10291 	if (ret) {
10292 		ath12k_warn(ab, "failed to set vdev %d chainmask 0x%x, nss %d :%d\n",
10293 			    arvif->vdev_id, ar->cfg_tx_chainmask, nss, ret);
10294 		goto err_vdev_del;
10295 	}
10296 
10297 	dp_link_vif = ath12k_dp_vif_to_dp_link_vif(&ahvif->dp_vif, arvif->link_id);
10298 
10299 	dp_link_vif->vdev_id = arvif->vdev_id;
10300 	dp_link_vif->lmac_id = ar->lmac_id;
10301 	dp_link_vif->pdev_idx = ar->pdev_idx;
10302 
10303 	switch (ahvif->vdev_type) {
10304 	case WMI_VDEV_TYPE_AP:
10305 		params.ucast_ra_only = true;
10306 
10307 		if (arvif->link_id < IEEE80211_MLD_MAX_NUM_LINKS) {
10308 			ret = ath12k_dp_peer_create(&ah->dp_hw, arvif->bssid, &params);
10309 			if (ret) {
10310 				ath12k_warn(ab, "failed to vdev %d create dp_peer for AP: %d\n",
10311 					    arvif->vdev_id, ret);
10312 				goto err_vdev_del;
10313 			}
10314 			dp_peer_created = true;
10315 		}
10316 
10317 		peer_param.vdev_id = arvif->vdev_id;
10318 		peer_param.peer_addr = arvif->bssid;
10319 		peer_param.peer_type = WMI_PEER_TYPE_DEFAULT;
10320 		ret = ath12k_peer_create(ar, arvif, NULL, &peer_param);
10321 		if (ret) {
10322 			ath12k_warn(ab, "failed to vdev %d create peer for AP: %d\n",
10323 				    arvif->vdev_id, ret);
10324 			goto err_dp_peer_del;
10325 		}
10326 
10327 		ret = ath12k_mac_set_kickout(arvif);
10328 		if (ret) {
10329 			ath12k_warn(ar->ab, "failed to set vdev %i kickout parameters: %d\n",
10330 				    arvif->vdev_id, ret);
10331 			goto err_peer_del;
10332 		}
10333 		ath12k_mac_11d_scan_stop_all(ar->ab);
10334 		break;
10335 	case WMI_VDEV_TYPE_STA:
10336 		param_id = WMI_STA_PS_PARAM_RX_WAKE_POLICY;
10337 		param_value = WMI_STA_PS_RX_WAKE_POLICY_WAKE;
10338 		ret = ath12k_wmi_set_sta_ps_param(ar, arvif->vdev_id,
10339 						  param_id, param_value);
10340 		if (ret) {
10341 			ath12k_warn(ar->ab, "failed to set vdev %d RX wake policy: %d\n",
10342 				    arvif->vdev_id, ret);
10343 			goto err_peer_del;
10344 		}
10345 
10346 		param_id = WMI_STA_PS_PARAM_TX_WAKE_THRESHOLD;
10347 		param_value = WMI_STA_PS_TX_WAKE_THRESHOLD_ALWAYS;
10348 		ret = ath12k_wmi_set_sta_ps_param(ar, arvif->vdev_id,
10349 						  param_id, param_value);
10350 		if (ret) {
10351 			ath12k_warn(ar->ab, "failed to set vdev %d TX wake threshold: %d\n",
10352 				    arvif->vdev_id, ret);
10353 			goto err_peer_del;
10354 		}
10355 
10356 		param_id = WMI_STA_PS_PARAM_PSPOLL_COUNT;
10357 		param_value = WMI_STA_PS_PSPOLL_COUNT_NO_MAX;
10358 		ret = ath12k_wmi_set_sta_ps_param(ar, arvif->vdev_id,
10359 						  param_id, param_value);
10360 		if (ret) {
10361 			ath12k_warn(ar->ab, "failed to set vdev %d pspoll count: %d\n",
10362 				    arvif->vdev_id, ret);
10363 			goto err_peer_del;
10364 		}
10365 
10366 		ret = ath12k_wmi_pdev_set_ps_mode(ar, arvif->vdev_id, false);
10367 		if (ret) {
10368 			ath12k_warn(ar->ab, "failed to disable vdev %d ps mode: %d\n",
10369 				    arvif->vdev_id, ret);
10370 			goto err_peer_del;
10371 		}
10372 
10373 		if (test_bit(WMI_TLV_SERVICE_11D_OFFLOAD, ab->wmi_ab.svc_map) &&
10374 		    ahvif->vdev_type == WMI_VDEV_TYPE_STA &&
10375 		    ahvif->vdev_subtype == WMI_VDEV_SUBTYPE_NONE) {
10376 			reinit_completion(&ar->completed_11d_scan);
10377 			ar->state_11d = ATH12K_11D_PREPARING;
10378 		}
10379 		break;
10380 	case WMI_VDEV_TYPE_MONITOR:
10381 		ar->monitor_vdev_created = true;
10382 		break;
10383 	default:
10384 		break;
10385 	}
10386 
10387 	if (link_conf)
10388 		arvif->txpower = link_conf->txpower;
10389 	else
10390 		arvif->txpower = NL80211_TX_POWER_AUTOMATIC;
10391 
10392 	ret = ath12k_mac_txpower_recalc(ar);
10393 	if (ret)
10394 		goto err_peer_del;
10395 
10396 	param_id = WMI_VDEV_PARAM_RTS_THRESHOLD;
10397 	param_value = hw->wiphy->rts_threshold;
10398 	ar->rts_threshold = param_value;
10399 	ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
10400 					    param_id, param_value);
10401 	if (ret) {
10402 		ath12k_warn(ar->ab, "failed to set rts threshold for vdev %d: %d\n",
10403 			    arvif->vdev_id, ret);
10404 	}
10405 
10406 	ath12k_dp_vdev_tx_attach(ar, arvif);
10407 
10408 	return ret;
10409 
10410 err_peer_del:
10411 	if (ahvif->vdev_type == WMI_VDEV_TYPE_AP) {
10412 		reinit_completion(&ar->peer_delete_done);
10413 
10414 		ret = ath12k_wmi_send_peer_delete_cmd(ar, arvif->bssid,
10415 						      arvif->vdev_id);
10416 		if (ret) {
10417 			ath12k_warn(ar->ab, "failed to delete peer vdev_id %d addr %pM\n",
10418 				    arvif->vdev_id, arvif->bssid);
10419 			goto err;
10420 		}
10421 
10422 		ret = ath12k_wait_for_peer_delete_done(ar, arvif->vdev_id,
10423 						       arvif->bssid);
10424 		if (ret)
10425 			goto err_vdev_del;
10426 
10427 		ar->num_peers--;
10428 	}
10429 
10430 err_dp_peer_del:
10431 	if (dp_peer_created)
10432 		ath12k_dp_peer_delete(&ah->dp_hw, arvif->bssid, NULL);
10433 
10434 err_vdev_del:
10435 	if (ahvif->vdev_type == WMI_VDEV_TYPE_MONITOR) {
10436 		ar->monitor_vdev_id = -1;
10437 		ar->monitor_vdev_created = false;
10438 	}
10439 
10440 	ath12k_wmi_vdev_delete(ar, arvif->vdev_id);
10441 	ar->num_created_vdevs--;
10442 	arvif->is_created = false;
10443 	arvif->ar = NULL;
10444 	ar->allocated_vdev_map &= ~(1LL << arvif->vdev_id);
10445 	ab->free_vdev_map |= 1LL << arvif->vdev_id;
10446 	ab->free_vdev_stats_id_map &= ~(1LL << arvif->vdev_stats_id);
10447 	spin_lock_bh(&ar->data_lock);
10448 	list_del(&arvif->list);
10449 	spin_unlock_bh(&ar->data_lock);
10450 
10451 err:
10452 	arvif->ar = NULL;
10453 	return ret;
10454 }
10455 
10456 static void ath12k_mac_vif_flush_key_cache(struct ath12k_link_vif *arvif)
10457 {
10458 	struct ath12k_key_conf *key_conf, *tmp;
10459 	struct ath12k_vif *ahvif = arvif->ahvif;
10460 	struct ath12k_hw *ah = ahvif->ah;
10461 	struct ath12k_sta *ahsta;
10462 	struct ath12k_link_sta *arsta;
10463 	struct ath12k_vif_cache *cache = ahvif->cache[arvif->link_id];
10464 	int ret;
10465 
10466 	lockdep_assert_wiphy(ah->hw->wiphy);
10467 
10468 	list_for_each_entry_safe(key_conf, tmp, &cache->key_conf.list, list) {
10469 		arsta = NULL;
10470 		if (key_conf->sta) {
10471 			ahsta = ath12k_sta_to_ahsta(key_conf->sta);
10472 			arsta = wiphy_dereference(ah->hw->wiphy,
10473 						  ahsta->link[arvif->link_id]);
10474 			if (!arsta)
10475 				goto free_cache;
10476 		}
10477 
10478 		ret = ath12k_mac_set_key(arvif->ar, key_conf->cmd,
10479 					 arvif, arsta,
10480 					 key_conf->key);
10481 		if (ret)
10482 			ath12k_warn(arvif->ar->ab, "unable to apply set key param to vdev %d ret %d\n",
10483 				    arvif->vdev_id, ret);
10484 free_cache:
10485 		list_del(&key_conf->list);
10486 		kfree(key_conf);
10487 	}
10488 }
10489 
10490 static void ath12k_mac_vif_cache_flush(struct ath12k *ar, struct ath12k_link_vif *arvif)
10491 {
10492 	struct ath12k_vif *ahvif = arvif->ahvif;
10493 	struct ieee80211_vif *vif = ath12k_ahvif_to_vif(ahvif);
10494 	struct ath12k_vif_cache *cache = ahvif->cache[arvif->link_id];
10495 	struct ath12k_base *ab = ar->ab;
10496 	struct ieee80211_bss_conf *link_conf;
10497 
10498 	int ret;
10499 
10500 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
10501 
10502 	if (!cache)
10503 		return;
10504 
10505 	if (cache->tx_conf.changed) {
10506 		ret = ath12k_mac_conf_tx(arvif, cache->tx_conf.ac,
10507 					 &cache->tx_conf.tx_queue_params);
10508 		if (ret)
10509 			ath12k_warn(ab,
10510 				    "unable to apply tx config parameters to vdev %d\n",
10511 				    ret);
10512 	}
10513 
10514 	if (cache->bss_conf_changed) {
10515 		link_conf = ath12k_mac_get_link_bss_conf(arvif);
10516 		if (!link_conf) {
10517 			ath12k_warn(ar->ab, "unable to access bss link conf in cache flush for vif %pM link %u\n",
10518 				    vif->addr, arvif->link_id);
10519 			return;
10520 		}
10521 		ath12k_mac_bss_info_changed(ar, arvif, link_conf,
10522 					    cache->bss_conf_changed);
10523 	}
10524 
10525 	if (!list_empty(&cache->key_conf.list))
10526 		ath12k_mac_vif_flush_key_cache(arvif);
10527 
10528 	ath12k_ahvif_put_link_cache(ahvif, arvif->link_id);
10529 }
10530 
10531 static struct ath12k *ath12k_mac_assign_vif_to_vdev(struct ieee80211_hw *hw,
10532 						    struct ath12k_link_vif *arvif,
10533 						    struct ieee80211_chanctx_conf *ctx)
10534 {
10535 	struct ath12k_vif *ahvif = arvif->ahvif;
10536 	struct ieee80211_vif *vif = ath12k_ahvif_to_vif(ahvif);
10537 	struct ath12k_link_vif *scan_arvif;
10538 	struct ath12k_hw *ah = hw->priv;
10539 	struct ath12k *ar;
10540 	struct ath12k_base *ab;
10541 	u8 link_id = arvif->link_id, scan_link_id;
10542 	unsigned long scan_link_map;
10543 	int ret;
10544 
10545 	lockdep_assert_wiphy(hw->wiphy);
10546 
10547 	if (ah->num_radio == 1)
10548 		ar = ah->radio;
10549 	else if (ctx)
10550 		ar = ath12k_get_ar_by_ctx(hw, ctx);
10551 	else
10552 		return NULL;
10553 
10554 	if (!ar)
10555 		return NULL;
10556 
10557 	/* cleanup the scan vdev if we are done scan on that ar
10558 	 * and now we want to create for actual usage.
10559 	 */
10560 	if (ieee80211_vif_is_mld(vif)) {
10561 		scan_link_map = ahvif->links_map & ATH12K_SCAN_LINKS_MASK;
10562 		for_each_set_bit(scan_link_id, &scan_link_map, ATH12K_NUM_MAX_LINKS) {
10563 			scan_arvif = wiphy_dereference(hw->wiphy,
10564 						       ahvif->link[scan_link_id]);
10565 			if (scan_arvif && scan_arvif->ar == ar) {
10566 				ar->scan.arvif = NULL;
10567 				ath12k_mac_remove_link_interface(hw, scan_arvif);
10568 				ath12k_mac_unassign_link_vif(scan_arvif);
10569 				break;
10570 			}
10571 		}
10572 	}
10573 
10574 	if (arvif->ar) {
10575 		/* This is not expected really */
10576 		if (WARN_ON(!arvif->is_created)) {
10577 			arvif->ar = NULL;
10578 			return NULL;
10579 		}
10580 
10581 		if (ah->num_radio == 1)
10582 			return arvif->ar;
10583 
10584 		/* This can happen as scan vdev gets created during multiple scans
10585 		 * across different radios before a vdev is brought up in
10586 		 * a certain radio.
10587 		 */
10588 		if (ar != arvif->ar) {
10589 			if (WARN_ON(arvif->is_started))
10590 				return NULL;
10591 
10592 			ath12k_mac_remove_link_interface(hw, arvif);
10593 			ath12k_mac_unassign_link_vif(arvif);
10594 		}
10595 	}
10596 
10597 	ab = ar->ab;
10598 
10599 	/* Assign arvif again here since previous radio switch block
10600 	 * would've unassigned and cleared it.
10601 	 */
10602 	arvif = ath12k_mac_assign_link_vif(ah, vif, link_id);
10603 	if (vif->type == NL80211_IFTYPE_AP &&
10604 	    ar->num_peers > (ar->max_num_peers - 1)) {
10605 		ath12k_warn(ab, "failed to create vdev due to insufficient peer entry resource in firmware\n");
10606 		goto unlock;
10607 	}
10608 
10609 	if (arvif->is_created)
10610 		goto flush;
10611 
10612 	ret = ath12k_mac_vdev_create(ar, arvif);
10613 	if (ret) {
10614 		ath12k_warn(ab, "failed to create vdev %pM ret %d", vif->addr, ret);
10615 		goto unlock;
10616 	}
10617 
10618 flush:
10619 	/* If the vdev is created during channel assign and not during
10620 	 * add_interface(), Apply any parameters for the vdev which were received
10621 	 * after add_interface, corresponding to this vif.
10622 	 */
10623 	ath12k_mac_vif_cache_flush(ar, arvif);
10624 unlock:
10625 	return arvif->ar;
10626 }
10627 
10628 int ath12k_mac_op_add_interface(struct ieee80211_hw *hw,
10629 				struct ieee80211_vif *vif)
10630 {
10631 	struct ath12k_hw *ah = ath12k_hw_to_ah(hw);
10632 	struct ath12k_vif *ahvif = ath12k_vif_to_ahvif(vif);
10633 	struct ath12k_reg_info *reg_info;
10634 	struct ath12k_link_vif *arvif;
10635 	struct ath12k_base *ab;
10636 	struct ath12k *ar;
10637 	int i;
10638 
10639 	lockdep_assert_wiphy(hw->wiphy);
10640 
10641 	memset(ahvif, 0, sizeof(*ahvif));
10642 
10643 	ahvif->ah = ah;
10644 	ahvif->vif = vif;
10645 	arvif = &ahvif->deflink;
10646 
10647 	ath12k_mac_init_arvif(ahvif, arvif, -1);
10648 
10649 	/* Allocate Default Queue now and reassign during actual vdev create */
10650 	vif->cab_queue = ATH12K_HW_DEFAULT_QUEUE;
10651 	for (i = 0; i < ARRAY_SIZE(vif->hw_queue); i++)
10652 		vif->hw_queue[i] = ATH12K_HW_DEFAULT_QUEUE;
10653 
10654 	vif->driver_flags |= IEEE80211_VIF_SUPPORTS_UAPSD;
10655 
10656 	ath12k_mac_determine_vdev_type(vif, ahvif);
10657 
10658 	for_each_ar(ah, ar, i) {
10659 		if (!ath12k_wmi_supports_6ghz_cc_ext(ar))
10660 			continue;
10661 
10662 		ab = ar->ab;
10663 		reg_info = ab->reg_info[ar->pdev_idx];
10664 		ath12k_dbg(ab, ATH12K_DBG_MAC, "interface added to change reg rules\n");
10665 		ah->regd_updated = false;
10666 		ath12k_reg_handle_chan_list(ab, reg_info, ahvif->vdev_type,
10667 					    IEEE80211_REG_UNSET_AP);
10668 		break;
10669 	}
10670 
10671 	/* Defer vdev creation until assign_chanctx or hw_scan is initiated as driver
10672 	 * will not know if this interface is an ML vif at this point.
10673 	 */
10674 	return 0;
10675 }
10676 EXPORT_SYMBOL(ath12k_mac_op_add_interface);
10677 
10678 static void ath12k_mac_vif_unref(struct ath12k_dp *dp, struct ieee80211_vif *vif)
10679 {
10680 	struct ath12k_tx_desc_info *tx_desc_info;
10681 	struct ath12k_skb_cb *skb_cb;
10682 	struct sk_buff *skb;
10683 	int i;
10684 
10685 	for (i = 0; i < ATH12K_HW_MAX_QUEUES; i++) {
10686 		spin_lock_bh(&dp->tx_desc_lock[i]);
10687 
10688 		list_for_each_entry(tx_desc_info, &dp->tx_desc_used_list[i],
10689 				    list) {
10690 			skb = tx_desc_info->skb;
10691 			if (!skb)
10692 				continue;
10693 
10694 			skb_cb = ATH12K_SKB_CB(skb);
10695 			if (skb_cb->vif == vif)
10696 				skb_cb->vif = NULL;
10697 		}
10698 
10699 		spin_unlock_bh(&dp->tx_desc_lock[i]);
10700 	}
10701 }
10702 
10703 static int ath12k_mac_vdev_delete(struct ath12k *ar, struct ath12k_link_vif *arvif)
10704 {
10705 	struct ath12k_vif *ahvif = arvif->ahvif;
10706 	struct ieee80211_vif *vif = ath12k_ahvif_to_vif(ahvif);
10707 	struct ath12k_dp_link_vif *dp_link_vif;
10708 	struct ath12k_base *ab = ar->ab;
10709 	unsigned long time_left;
10710 	int ret;
10711 
10712 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
10713 
10714 	reinit_completion(&ar->vdev_delete_done);
10715 
10716 	ret = ath12k_wmi_vdev_delete(ar, arvif->vdev_id);
10717 	if (ret) {
10718 		ath12k_warn(ab, "failed to delete WMI vdev %d: %d\n",
10719 			    arvif->vdev_id, ret);
10720 		goto err_vdev_del;
10721 	}
10722 
10723 	time_left = wait_for_completion_timeout(&ar->vdev_delete_done,
10724 						ATH12K_VDEV_DELETE_TIMEOUT_HZ);
10725 	if (time_left == 0) {
10726 		ath12k_warn(ab, "Timeout in receiving vdev delete response\n");
10727 		goto err_vdev_del;
10728 	}
10729 
10730 	ab->free_vdev_map |= 1LL << arvif->vdev_id;
10731 	ar->allocated_vdev_map &= ~(1LL << arvif->vdev_id);
10732 	ar->num_created_vdevs--;
10733 
10734 	if (ahvif->vdev_type == WMI_VDEV_TYPE_MONITOR) {
10735 		ar->monitor_vdev_id = -1;
10736 		ar->monitor_vdev_created = false;
10737 	}
10738 
10739 	ath12k_dbg(ab, ATH12K_DBG_MAC, "vdev %pM deleted, vdev_id %d\n",
10740 		   vif->addr, arvif->vdev_id);
10741 
10742 err_vdev_del:
10743 	spin_lock_bh(&ar->data_lock);
10744 	list_del(&arvif->list);
10745 	spin_unlock_bh(&ar->data_lock);
10746 
10747 	ath12k_peer_cleanup(ar, arvif->vdev_id);
10748 	ath12k_ahvif_put_link_cache(ahvif, arvif->link_id);
10749 
10750 	idr_for_each(&ar->txmgmt_idr,
10751 		     ath12k_mac_vif_txmgmt_idr_remove, vif);
10752 
10753 	ath12k_mac_vif_unref(ath12k_ab_to_dp(ab), vif);
10754 
10755 	dp_link_vif = ath12k_dp_vif_to_dp_link_vif(&ahvif->dp_vif, arvif->link_id);
10756 	ath12k_dp_tx_put_bank_profile(ath12k_ab_to_dp(ab), dp_link_vif->bank_id);
10757 
10758 	/* Recalc txpower for remaining vdev */
10759 	ath12k_mac_txpower_recalc(ar);
10760 
10761 	/* TODO: recal traffic pause state based on the available vdevs */
10762 	arvif->is_created = false;
10763 	arvif->ar = NULL;
10764 
10765 	return ret;
10766 }
10767 
10768 void ath12k_mac_op_remove_interface(struct ieee80211_hw *hw,
10769 				    struct ieee80211_vif *vif)
10770 {
10771 	struct ath12k_vif *ahvif = ath12k_vif_to_ahvif(vif);
10772 	struct ath12k_link_vif *arvif;
10773 	struct ath12k *ar;
10774 	u8 link_id;
10775 
10776 	lockdep_assert_wiphy(hw->wiphy);
10777 
10778 	for (link_id = 0; link_id < ATH12K_NUM_MAX_LINKS; link_id++) {
10779 		/* if we cached some config but never received assign chanctx,
10780 		 * free the allocated cache.
10781 		 */
10782 		ath12k_ahvif_put_link_cache(ahvif, link_id);
10783 		arvif = wiphy_dereference(hw->wiphy, ahvif->link[link_id]);
10784 		if (!arvif || !arvif->is_created)
10785 			continue;
10786 
10787 		ar = arvif->ar;
10788 
10789 		/* Scan abortion is in progress since before this, cancel_hw_scan()
10790 		 * is expected to be executed. Since link is anyways going to be removed
10791 		 * now, just cancel the worker and send the scan aborted to user space
10792 		 */
10793 		if (ar->scan.arvif == arvif) {
10794 			wiphy_work_cancel(hw->wiphy, &ar->scan.vdev_clean_wk);
10795 
10796 			spin_lock_bh(&ar->data_lock);
10797 			ar->scan.arvif = NULL;
10798 			if (!ar->scan.is_roc) {
10799 				struct cfg80211_scan_info info = {
10800 					.aborted = true,
10801 				};
10802 
10803 				ath12k_mac_scan_send_complete(ar, &info);
10804 			}
10805 
10806 			ar->scan.state = ATH12K_SCAN_IDLE;
10807 			ar->scan_channel = NULL;
10808 			ar->scan.roc_freq = 0;
10809 			spin_unlock_bh(&ar->data_lock);
10810 		}
10811 
10812 		ath12k_mac_remove_link_interface(hw, arvif);
10813 		ath12k_mac_unassign_link_vif(arvif);
10814 	}
10815 }
10816 EXPORT_SYMBOL(ath12k_mac_op_remove_interface);
10817 
10818 /* FIXME: Has to be verified. */
10819 #define SUPPORTED_FILTERS			\
10820 	(FIF_ALLMULTI |				\
10821 	FIF_CONTROL |				\
10822 	FIF_PSPOLL |				\
10823 	FIF_OTHER_BSS |				\
10824 	FIF_BCN_PRBRESP_PROMISC |		\
10825 	FIF_PROBE_REQ |				\
10826 	FIF_FCSFAIL)
10827 
10828 void ath12k_mac_op_configure_filter(struct ieee80211_hw *hw,
10829 				    unsigned int changed_flags,
10830 				    unsigned int *total_flags,
10831 				    u64 multicast)
10832 {
10833 	struct ath12k_hw *ah = ath12k_hw_to_ah(hw);
10834 	struct ath12k *ar;
10835 
10836 	lockdep_assert_wiphy(hw->wiphy);
10837 
10838 	ar = ath12k_ah_to_ar(ah, 0);
10839 
10840 	*total_flags &= SUPPORTED_FILTERS;
10841 	ar->filter_flags = *total_flags;
10842 }
10843 EXPORT_SYMBOL(ath12k_mac_op_configure_filter);
10844 
10845 int ath12k_mac_op_get_antenna(struct ieee80211_hw *hw, int radio_idx,
10846 			      u32 *tx_ant, u32 *rx_ant)
10847 {
10848 	struct ath12k_hw *ah = ath12k_hw_to_ah(hw);
10849 	int antennas_rx = 0, antennas_tx = 0;
10850 	struct ath12k *ar;
10851 	int i;
10852 
10853 	lockdep_assert_wiphy(hw->wiphy);
10854 
10855 	for_each_ar(ah, ar, i) {
10856 		antennas_rx = max_t(u32, antennas_rx, ar->cfg_rx_chainmask);
10857 		antennas_tx = max_t(u32, antennas_tx, ar->cfg_tx_chainmask);
10858 	}
10859 
10860 	*tx_ant = antennas_tx;
10861 	*rx_ant = antennas_rx;
10862 
10863 	return 0;
10864 }
10865 EXPORT_SYMBOL(ath12k_mac_op_get_antenna);
10866 
10867 int ath12k_mac_op_set_antenna(struct ieee80211_hw *hw, int radio_idx,
10868 			      u32 tx_ant, u32 rx_ant)
10869 {
10870 	struct ath12k_hw *ah = ath12k_hw_to_ah(hw);
10871 	struct ath12k *ar;
10872 	int ret = 0;
10873 	int i;
10874 
10875 	lockdep_assert_wiphy(hw->wiphy);
10876 
10877 	for_each_ar(ah, ar, i) {
10878 		ret = __ath12k_set_antenna(ar, tx_ant, rx_ant);
10879 		if (ret)
10880 			break;
10881 	}
10882 
10883 	return ret;
10884 }
10885 EXPORT_SYMBOL(ath12k_mac_op_set_antenna);
10886 
10887 static int ath12k_mac_ampdu_action(struct ieee80211_hw *hw,
10888 				   struct ieee80211_vif *vif,
10889 				   struct ieee80211_ampdu_params *params,
10890 				   u8 link_id)
10891 {
10892 	struct ath12k *ar;
10893 	int ret = -EINVAL;
10894 
10895 	lockdep_assert_wiphy(hw->wiphy);
10896 
10897 	ar = ath12k_get_ar_by_vif(hw, vif, link_id);
10898 	if (!ar)
10899 		return -EINVAL;
10900 
10901 	switch (params->action) {
10902 	case IEEE80211_AMPDU_RX_START:
10903 		ret = ath12k_dp_rx_ampdu_start(ar, params, link_id);
10904 		break;
10905 	case IEEE80211_AMPDU_RX_STOP:
10906 		ret = ath12k_dp_rx_ampdu_stop(ar, params, link_id);
10907 		break;
10908 	case IEEE80211_AMPDU_TX_START:
10909 	case IEEE80211_AMPDU_TX_STOP_CONT:
10910 	case IEEE80211_AMPDU_TX_STOP_FLUSH:
10911 	case IEEE80211_AMPDU_TX_STOP_FLUSH_CONT:
10912 	case IEEE80211_AMPDU_TX_OPERATIONAL:
10913 		/* Tx A-MPDU aggregation offloaded to hw/fw so deny mac80211
10914 		 * Tx aggregation requests.
10915 		 */
10916 		ret = -EOPNOTSUPP;
10917 		break;
10918 	}
10919 
10920 	if (ret)
10921 		ath12k_warn(ar->ab, "unable to perform ampdu action %d for vif %pM link %u ret %d\n",
10922 			    params->action, vif->addr, link_id, ret);
10923 
10924 	return ret;
10925 }
10926 
10927 int ath12k_mac_op_ampdu_action(struct ieee80211_hw *hw,
10928 			       struct ieee80211_vif *vif,
10929 			       struct ieee80211_ampdu_params *params)
10930 {
10931 	struct ieee80211_sta *sta = params->sta;
10932 	struct ath12k_sta *ahsta = ath12k_sta_to_ahsta(sta);
10933 	unsigned long links_map = ahsta->links_map;
10934 	int ret = -EINVAL;
10935 	u8 link_id;
10936 
10937 	lockdep_assert_wiphy(hw->wiphy);
10938 
10939 	if (WARN_ON(!links_map))
10940 		return ret;
10941 
10942 	for_each_set_bit(link_id, &links_map, IEEE80211_MLD_MAX_NUM_LINKS) {
10943 		ret = ath12k_mac_ampdu_action(hw, vif, params, link_id);
10944 		if (ret)
10945 			return ret;
10946 	}
10947 
10948 	return 0;
10949 }
10950 EXPORT_SYMBOL(ath12k_mac_op_ampdu_action);
10951 
10952 int ath12k_mac_op_add_chanctx(struct ieee80211_hw *hw,
10953 			      struct ieee80211_chanctx_conf *ctx)
10954 {
10955 	struct ath12k *ar;
10956 	struct ath12k_base *ab;
10957 
10958 	lockdep_assert_wiphy(hw->wiphy);
10959 
10960 	ar = ath12k_get_ar_by_ctx(hw, ctx);
10961 	if (!ar)
10962 		return -EINVAL;
10963 
10964 	ab = ar->ab;
10965 
10966 	ath12k_dbg(ab, ATH12K_DBG_MAC,
10967 		   "mac chanctx add freq %u width %d ptr %p\n",
10968 		   ctx->def.chan->center_freq, ctx->def.width, ctx);
10969 
10970 	spin_lock_bh(&ar->data_lock);
10971 	/* TODO: In case of multiple channel context, populate rx_channel from
10972 	 * Rx PPDU desc information.
10973 	 */
10974 	ar->rx_channel = ctx->def.chan;
10975 	spin_unlock_bh(&ar->data_lock);
10976 	ar->chan_tx_pwr = ATH12K_PDEV_TX_POWER_INVALID;
10977 
10978 	return 0;
10979 }
10980 EXPORT_SYMBOL(ath12k_mac_op_add_chanctx);
10981 
10982 void ath12k_mac_op_remove_chanctx(struct ieee80211_hw *hw,
10983 				  struct ieee80211_chanctx_conf *ctx)
10984 {
10985 	struct ath12k *ar;
10986 	struct ath12k_base *ab;
10987 
10988 	lockdep_assert_wiphy(hw->wiphy);
10989 
10990 	ar = ath12k_get_ar_by_ctx(hw, ctx);
10991 	if (!ar)
10992 		return;
10993 
10994 	ab = ar->ab;
10995 
10996 	ath12k_dbg(ab, ATH12K_DBG_MAC,
10997 		   "mac chanctx remove freq %u width %d ptr %p\n",
10998 		   ctx->def.chan->center_freq, ctx->def.width, ctx);
10999 
11000 	spin_lock_bh(&ar->data_lock);
11001 	/* TODO: In case of there is one more channel context left, populate
11002 	 * rx_channel with the channel of that remaining channel context.
11003 	 */
11004 	ar->rx_channel = NULL;
11005 	spin_unlock_bh(&ar->data_lock);
11006 	ar->chan_tx_pwr = ATH12K_PDEV_TX_POWER_INVALID;
11007 }
11008 EXPORT_SYMBOL(ath12k_mac_op_remove_chanctx);
11009 
11010 static enum wmi_phy_mode
11011 ath12k_mac_check_down_grade_phy_mode(struct ath12k *ar,
11012 				     enum wmi_phy_mode mode,
11013 				     enum nl80211_band band,
11014 				     enum nl80211_iftype type)
11015 {
11016 	struct ieee80211_sta_eht_cap *eht_cap = NULL;
11017 	enum wmi_phy_mode down_mode;
11018 	int n = ar->mac.sbands[band].n_iftype_data;
11019 	int i;
11020 	struct ieee80211_sband_iftype_data *data;
11021 
11022 	if (mode < MODE_11BE_EHT20)
11023 		return mode;
11024 
11025 	data = ar->mac.iftype[band];
11026 	for (i = 0; i < n; i++) {
11027 		if (data[i].types_mask & BIT(type)) {
11028 			eht_cap = &data[i].eht_cap;
11029 			break;
11030 		}
11031 	}
11032 
11033 	if (eht_cap && eht_cap->has_eht)
11034 		return mode;
11035 
11036 	switch (mode) {
11037 	case MODE_11BE_EHT20:
11038 		down_mode = MODE_11AX_HE20;
11039 		break;
11040 	case MODE_11BE_EHT40:
11041 		down_mode = MODE_11AX_HE40;
11042 		break;
11043 	case MODE_11BE_EHT80:
11044 		down_mode = MODE_11AX_HE80;
11045 		break;
11046 	case MODE_11BE_EHT80_80:
11047 		down_mode = MODE_11AX_HE80_80;
11048 		break;
11049 	case MODE_11BE_EHT160:
11050 	case MODE_11BE_EHT160_160:
11051 	case MODE_11BE_EHT320:
11052 		down_mode = MODE_11AX_HE160;
11053 		break;
11054 	case MODE_11BE_EHT20_2G:
11055 		down_mode = MODE_11AX_HE20_2G;
11056 		break;
11057 	case MODE_11BE_EHT40_2G:
11058 		down_mode = MODE_11AX_HE40_2G;
11059 		break;
11060 	default:
11061 		down_mode = mode;
11062 		break;
11063 	}
11064 
11065 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
11066 		   "mac vdev start phymode %s downgrade to %s\n",
11067 		   ath12k_mac_phymode_str(mode),
11068 		   ath12k_mac_phymode_str(down_mode));
11069 
11070 	return down_mode;
11071 }
11072 
11073 static void
11074 ath12k_mac_mlo_get_vdev_args(struct ath12k_link_vif *arvif,
11075 			     struct wmi_ml_arg *ml_arg)
11076 {
11077 	struct ath12k_vif *ahvif = arvif->ahvif;
11078 	struct wmi_ml_partner_info *partner_info;
11079 	struct ieee80211_bss_conf *link_conf;
11080 	struct ath12k_link_vif *arvif_p;
11081 	unsigned long links;
11082 	u8 link_id;
11083 
11084 	lockdep_assert_wiphy(ahvif->ah->hw->wiphy);
11085 
11086 	if (!ath12k_mac_is_ml_arvif(arvif))
11087 		return;
11088 
11089 	if (hweight16(ahvif->vif->valid_links) > ATH12K_WMI_MLO_MAX_LINKS)
11090 		return;
11091 
11092 	ml_arg->enabled = true;
11093 
11094 	/* Driver always add a new link via VDEV START, FW takes
11095 	 * care of internally adding this link to existing
11096 	 * link vdevs which are advertised as partners below
11097 	 */
11098 	ml_arg->link_add = true;
11099 
11100 	ml_arg->assoc_link = arvif->is_sta_assoc_link;
11101 
11102 	partner_info = ml_arg->partner_info;
11103 
11104 	links = ahvif->links_map;
11105 	for_each_set_bit(link_id, &links, IEEE80211_MLD_MAX_NUM_LINKS) {
11106 		arvif_p = wiphy_dereference(ahvif->ah->hw->wiphy, ahvif->link[link_id]);
11107 
11108 		if (WARN_ON(!arvif_p))
11109 			continue;
11110 
11111 		if (arvif == arvif_p)
11112 			continue;
11113 
11114 		if (!arvif_p->is_created)
11115 			continue;
11116 
11117 		link_conf = wiphy_dereference(ahvif->ah->hw->wiphy,
11118 					      ahvif->vif->link_conf[arvif_p->link_id]);
11119 
11120 		if (!link_conf)
11121 			continue;
11122 
11123 		partner_info->vdev_id = arvif_p->vdev_id;
11124 		partner_info->hw_link_id = arvif_p->ar->pdev->hw_link_id;
11125 		ether_addr_copy(partner_info->addr, link_conf->addr);
11126 		ml_arg->num_partner_links++;
11127 		partner_info++;
11128 	}
11129 }
11130 
11131 static int
11132 ath12k_mac_vdev_start_restart(struct ath12k_link_vif *arvif,
11133 			      struct ieee80211_chanctx_conf *ctx,
11134 			      bool restart)
11135 {
11136 	struct ath12k *ar = arvif->ar;
11137 	struct ath12k_base *ab = ar->ab;
11138 	struct wmi_vdev_start_req_arg arg = {};
11139 	const struct cfg80211_chan_def *chandef = &ctx->def;
11140 	struct ieee80211_hw *hw = ath12k_ar_to_hw(ar);
11141 	struct ath12k_vif *ahvif = arvif->ahvif;
11142 	struct ieee80211_bss_conf *link_conf;
11143 	unsigned int dfs_cac_time;
11144 	int ret;
11145 
11146 	lockdep_assert_wiphy(hw->wiphy);
11147 
11148 	link_conf = ath12k_mac_get_link_bss_conf(arvif);
11149 	if (!link_conf) {
11150 		ath12k_warn(ar->ab, "unable to access bss link conf in vdev start for vif %pM link %u\n",
11151 			    ahvif->vif->addr, arvif->link_id);
11152 		return -ENOLINK;
11153 	}
11154 
11155 	reinit_completion(&ar->vdev_setup_done);
11156 
11157 	arg.vdev_id = arvif->vdev_id;
11158 	arg.dtim_period = arvif->dtim_period;
11159 	arg.bcn_intval = arvif->beacon_interval;
11160 	arg.punct_bitmap = ~arvif->punct_bitmap;
11161 
11162 	arg.freq = chandef->chan->center_freq;
11163 	arg.band_center_freq1 = chandef->center_freq1;
11164 	arg.band_center_freq2 = chandef->center_freq2;
11165 	arg.mode = ath12k_phymodes[chandef->chan->band][chandef->width];
11166 
11167 	arg.mode = ath12k_mac_check_down_grade_phy_mode(ar, arg.mode,
11168 							chandef->chan->band,
11169 							ahvif->vif->type);
11170 	arg.min_power = 0;
11171 	arg.max_power = chandef->chan->max_power;
11172 	arg.max_reg_power = chandef->chan->max_reg_power;
11173 	arg.max_antenna_gain = chandef->chan->max_antenna_gain;
11174 
11175 	arg.pref_tx_streams = ar->num_tx_chains;
11176 	arg.pref_rx_streams = ar->num_rx_chains;
11177 
11178 	arg.mbssid_flags = WMI_VDEV_MBSSID_FLAGS_NON_MBSSID_AP;
11179 	arg.mbssid_tx_vdev_id = 0;
11180 	if (test_bit(WMI_TLV_SERVICE_MBSS_PARAM_IN_VDEV_START_SUPPORT,
11181 		     ar->ab->wmi_ab.svc_map)) {
11182 		ret = ath12k_mac_setup_vdev_params_mbssid(arvif,
11183 							  &arg.mbssid_flags,
11184 							  &arg.mbssid_tx_vdev_id);
11185 		if (ret)
11186 			return ret;
11187 	}
11188 
11189 	if (ahvif->vdev_type == WMI_VDEV_TYPE_AP) {
11190 		arg.ssid = ahvif->u.ap.ssid;
11191 		arg.ssid_len = ahvif->u.ap.ssid_len;
11192 		arg.hidden_ssid = ahvif->u.ap.hidden_ssid;
11193 
11194 		/* For now allow DFS for AP mode */
11195 		arg.chan_radar = !!(chandef->chan->flags & IEEE80211_CHAN_RADAR);
11196 
11197 		arg.freq2_radar = ctx->radar_enabled;
11198 
11199 		arg.passive = arg.chan_radar;
11200 
11201 		spin_lock_bh(&ab->base_lock);
11202 		arg.regdomain = ar->ab->dfs_region;
11203 		spin_unlock_bh(&ab->base_lock);
11204 
11205 		/* TODO: Notify if secondary 80Mhz also needs radar detection */
11206 	}
11207 
11208 	arg.passive |= !!(chandef->chan->flags & IEEE80211_CHAN_NO_IR);
11209 
11210 	if (!restart)
11211 		ath12k_mac_mlo_get_vdev_args(arvif, &arg.ml);
11212 
11213 	ath12k_dbg(ab, ATH12K_DBG_MAC,
11214 		   "mac vdev %d start center_freq %d phymode %s punct_bitmap 0x%x\n",
11215 		   arg.vdev_id, arg.freq,
11216 		   ath12k_mac_phymode_str(arg.mode), arg.punct_bitmap);
11217 
11218 	ret = ath12k_wmi_vdev_start(ar, &arg, restart);
11219 	if (ret) {
11220 		ath12k_warn(ar->ab, "failed to %s WMI vdev %i\n",
11221 			    restart ? "restart" : "start", arg.vdev_id);
11222 		return ret;
11223 	}
11224 
11225 	ret = ath12k_mac_vdev_setup_sync(ar);
11226 	if (ret) {
11227 		ath12k_warn(ab, "failed to synchronize setup for vdev %i %s: %d\n",
11228 			    arg.vdev_id, restart ? "restart" : "start", ret);
11229 		return ret;
11230 	}
11231 
11232 	/* TODO: For now we only set TPC power here. However when
11233 	 * channel changes, say CSA, it should be updated again.
11234 	 */
11235 	if (ath12k_mac_supports_tpc(ar, ahvif, chandef)) {
11236 		ath12k_mac_fill_reg_tpc_info(ar, arvif, ctx);
11237 		ath12k_wmi_send_vdev_set_tpc_power(ar, arvif->vdev_id,
11238 						   &arvif->reg_tpc_info);
11239 	}
11240 
11241 	ar->num_started_vdevs++;
11242 	ath12k_dbg(ab, ATH12K_DBG_MAC,  "vdev %pM started, vdev_id %d\n",
11243 		   ahvif->vif->addr, arvif->vdev_id);
11244 
11245 	/* Enable CAC Running Flag in the driver by checking all sub-channel's DFS
11246 	 * state as NL80211_DFS_USABLE which indicates CAC needs to be
11247 	 * done before channel usage. This flag is used to drop rx packets.
11248 	 * during CAC.
11249 	 */
11250 	/* TODO: Set the flag for other interface types as required */
11251 	if (arvif->ahvif->vdev_type == WMI_VDEV_TYPE_AP && ctx->radar_enabled &&
11252 	    cfg80211_chandef_dfs_usable(hw->wiphy, chandef)) {
11253 		set_bit(ATH12K_FLAG_CAC_RUNNING, &ar->dev_flags);
11254 		dfs_cac_time = cfg80211_chandef_dfs_cac_time(hw->wiphy, chandef);
11255 
11256 		ath12k_dbg(ab, ATH12K_DBG_MAC,
11257 			   "CAC started dfs_cac_time %u center_freq %d center_freq1 %d for vdev %d\n",
11258 			   dfs_cac_time, arg.freq, arg.band_center_freq1, arg.vdev_id);
11259 	}
11260 
11261 	ret = ath12k_mac_set_txbf_conf(arvif);
11262 	if (ret)
11263 		ath12k_warn(ab, "failed to set txbf conf for vdev %d: %d\n",
11264 			    arvif->vdev_id, ret);
11265 
11266 	return 0;
11267 }
11268 
11269 static int ath12k_mac_vdev_start(struct ath12k_link_vif *arvif,
11270 				 struct ieee80211_chanctx_conf *ctx)
11271 {
11272 	return ath12k_mac_vdev_start_restart(arvif, ctx, false);
11273 }
11274 
11275 static int ath12k_mac_vdev_restart(struct ath12k_link_vif *arvif,
11276 				   struct ieee80211_chanctx_conf *ctx)
11277 {
11278 	return ath12k_mac_vdev_start_restart(arvif, ctx, true);
11279 }
11280 
11281 struct ath12k_mac_change_chanctx_arg {
11282 	struct ieee80211_chanctx_conf *ctx;
11283 	struct ieee80211_vif_chanctx_switch *vifs;
11284 	int n_vifs;
11285 	int next_vif;
11286 	struct ath12k *ar;
11287 };
11288 
11289 static void
11290 ath12k_mac_change_chanctx_cnt_iter(void *data, u8 *mac,
11291 				   struct ieee80211_vif *vif)
11292 {
11293 	struct ath12k_vif *ahvif = ath12k_vif_to_ahvif(vif);
11294 	struct ath12k_mac_change_chanctx_arg *arg = data;
11295 	struct ieee80211_bss_conf *link_conf;
11296 	struct ath12k_link_vif *arvif;
11297 	unsigned long links_map;
11298 	u8 link_id;
11299 
11300 	lockdep_assert_wiphy(ahvif->ah->hw->wiphy);
11301 
11302 	links_map = ahvif->links_map;
11303 	for_each_set_bit(link_id, &links_map, IEEE80211_MLD_MAX_NUM_LINKS) {
11304 		arvif = wiphy_dereference(ahvif->ah->hw->wiphy, ahvif->link[link_id]);
11305 		if (WARN_ON(!arvif))
11306 			continue;
11307 
11308 		if (!arvif->is_created || arvif->ar != arg->ar)
11309 			continue;
11310 
11311 		link_conf = wiphy_dereference(ahvif->ah->hw->wiphy,
11312 					      vif->link_conf[link_id]);
11313 		if (WARN_ON(!link_conf))
11314 			continue;
11315 
11316 		if (rcu_access_pointer(link_conf->chanctx_conf) != arg->ctx)
11317 			continue;
11318 
11319 		arg->n_vifs++;
11320 	}
11321 }
11322 
11323 static void
11324 ath12k_mac_change_chanctx_fill_iter(void *data, u8 *mac,
11325 				    struct ieee80211_vif *vif)
11326 {
11327 	struct ath12k_vif *ahvif = ath12k_vif_to_ahvif(vif);
11328 	struct ath12k_mac_change_chanctx_arg *arg = data;
11329 	struct ieee80211_bss_conf *link_conf;
11330 	struct ieee80211_chanctx_conf *ctx;
11331 	struct ath12k_link_vif *arvif;
11332 	unsigned long links_map;
11333 	u8 link_id;
11334 
11335 	lockdep_assert_wiphy(ahvif->ah->hw->wiphy);
11336 
11337 	links_map = ahvif->links_map;
11338 	for_each_set_bit(link_id, &links_map, IEEE80211_MLD_MAX_NUM_LINKS) {
11339 		arvif = wiphy_dereference(ahvif->ah->hw->wiphy, ahvif->link[link_id]);
11340 		if (WARN_ON(!arvif))
11341 			continue;
11342 
11343 		if (!arvif->is_created || arvif->ar != arg->ar)
11344 			continue;
11345 
11346 		link_conf = wiphy_dereference(ahvif->ah->hw->wiphy,
11347 					      vif->link_conf[arvif->link_id]);
11348 		if (WARN_ON(!link_conf))
11349 			continue;
11350 
11351 		ctx = rcu_access_pointer(link_conf->chanctx_conf);
11352 		if (ctx != arg->ctx)
11353 			continue;
11354 
11355 		if (WARN_ON(arg->next_vif == arg->n_vifs))
11356 			return;
11357 
11358 		arg->vifs[arg->next_vif].vif = vif;
11359 		arg->vifs[arg->next_vif].old_ctx = ctx;
11360 		arg->vifs[arg->next_vif].new_ctx = ctx;
11361 		arg->vifs[arg->next_vif].link_conf = link_conf;
11362 		arg->next_vif++;
11363 	}
11364 }
11365 
11366 static u32 ath12k_mac_nlwidth_to_wmiwidth(enum nl80211_chan_width width)
11367 {
11368 	switch (width) {
11369 	case NL80211_CHAN_WIDTH_20:
11370 		return WMI_CHAN_WIDTH_20;
11371 	case NL80211_CHAN_WIDTH_40:
11372 		return WMI_CHAN_WIDTH_40;
11373 	case NL80211_CHAN_WIDTH_80:
11374 		return WMI_CHAN_WIDTH_80;
11375 	case NL80211_CHAN_WIDTH_160:
11376 		return WMI_CHAN_WIDTH_160;
11377 	case NL80211_CHAN_WIDTH_80P80:
11378 		return WMI_CHAN_WIDTH_80P80;
11379 	case NL80211_CHAN_WIDTH_5:
11380 		return WMI_CHAN_WIDTH_5;
11381 	case NL80211_CHAN_WIDTH_10:
11382 		return WMI_CHAN_WIDTH_10;
11383 	case NL80211_CHAN_WIDTH_320:
11384 		return WMI_CHAN_WIDTH_320;
11385 	default:
11386 		WARN_ON(1);
11387 		return WMI_CHAN_WIDTH_20;
11388 	}
11389 }
11390 
11391 static int ath12k_mac_update_peer_puncturing_width(struct ath12k *ar,
11392 						   struct ath12k_link_vif *arvif,
11393 						   struct cfg80211_chan_def def)
11394 {
11395 	u32 param_id, param_value;
11396 	int ret;
11397 
11398 	if (arvif->ahvif->vdev_type != WMI_VDEV_TYPE_STA)
11399 		return 0;
11400 
11401 	param_id = WMI_PEER_CHWIDTH_PUNCTURE_20MHZ_BITMAP;
11402 	param_value = ath12k_mac_nlwidth_to_wmiwidth(def.width) |
11403 		u32_encode_bits((~def.punctured),
11404 				WMI_PEER_PUNCTURE_BITMAP);
11405 
11406 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
11407 		   "punctured bitmap %02x width %d vdev %d\n",
11408 		   def.punctured, def.width, arvif->vdev_id);
11409 
11410 	ret = ath12k_wmi_set_peer_param(ar, arvif->bssid,
11411 					arvif->vdev_id, param_id,
11412 					param_value);
11413 
11414 	return ret;
11415 }
11416 
11417 static void
11418 ath12k_mac_update_vif_chan(struct ath12k *ar,
11419 			   struct ieee80211_vif_chanctx_switch *vifs,
11420 			   int n_vifs)
11421 {
11422 	struct ath12k_wmi_vdev_up_params params = {};
11423 	struct ieee80211_bss_conf *link_conf;
11424 	struct ath12k_base *ab = ar->ab;
11425 	struct ath12k_link_vif *arvif;
11426 	struct ieee80211_vif *vif;
11427 	struct ath12k_vif *ahvif;
11428 	u8 link_id;
11429 	int ret;
11430 	int i;
11431 	bool monitor_vif = false;
11432 
11433 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
11434 
11435 	for (i = 0; i < n_vifs; i++) {
11436 		vif = vifs[i].vif;
11437 		ahvif = ath12k_vif_to_ahvif(vif);
11438 		link_conf = vifs[i].link_conf;
11439 		link_id = link_conf->link_id;
11440 		arvif = wiphy_dereference(ath12k_ar_to_hw(ar)->wiphy,
11441 					  ahvif->link[link_id]);
11442 
11443 		if (vif->type == NL80211_IFTYPE_MONITOR) {
11444 			monitor_vif = true;
11445 			continue;
11446 		}
11447 
11448 		ath12k_dbg(ab, ATH12K_DBG_MAC,
11449 			   "mac chanctx switch vdev_id %i freq %u->%u width %d->%d\n",
11450 			   arvif->vdev_id,
11451 			   vifs[i].old_ctx->def.chan->center_freq,
11452 			   vifs[i].new_ctx->def.chan->center_freq,
11453 			   vifs[i].old_ctx->def.width,
11454 			   vifs[i].new_ctx->def.width);
11455 
11456 		if (WARN_ON(!arvif->is_started))
11457 			continue;
11458 
11459 		arvif->punct_bitmap = vifs[i].new_ctx->def.punctured;
11460 
11461 		/* Firmware expect vdev_restart only if vdev is up.
11462 		 * If vdev is down then it expect vdev_stop->vdev_start.
11463 		 */
11464 		if (arvif->is_up) {
11465 			ret = ath12k_mac_vdev_restart(arvif, vifs[i].new_ctx);
11466 			if (ret) {
11467 				ath12k_warn(ab, "failed to restart vdev %d: %d\n",
11468 					    arvif->vdev_id, ret);
11469 				continue;
11470 			}
11471 		} else {
11472 			ret = ath12k_mac_vdev_stop(arvif);
11473 			if (ret) {
11474 				ath12k_warn(ab, "failed to stop vdev %d: %d\n",
11475 					    arvif->vdev_id, ret);
11476 				continue;
11477 			}
11478 
11479 			ret = ath12k_mac_vdev_start(arvif, vifs[i].new_ctx);
11480 			if (ret)
11481 				ath12k_warn(ab, "failed to start vdev %d: %d\n",
11482 					    arvif->vdev_id, ret);
11483 			continue;
11484 		}
11485 
11486 		ret = ath12k_mac_update_peer_puncturing_width(arvif->ar, arvif,
11487 							      vifs[i].new_ctx->def);
11488 		if (ret) {
11489 			ath12k_warn(ar->ab,
11490 				    "failed to update puncturing bitmap %02x and width %d: %d\n",
11491 				    vifs[i].new_ctx->def.punctured,
11492 				    vifs[i].new_ctx->def.width, ret);
11493 			continue;
11494 		}
11495 
11496 		/* Defer VDEV bring-up during CSA to avoid installing stale
11497 		 * beacon templates. The beacon content is updated only
11498 		 * after CSA finalize, so we mark CSA in progress and skip
11499 		 * VDEV_UP for now. It will be handled later in
11500 		 * bss_info_changed().
11501 		 */
11502 		if (link_conf->csa_active &&
11503 		    arvif->ahvif->vdev_type == WMI_VDEV_TYPE_AP) {
11504 			arvif->is_csa_in_progress = true;
11505 			continue;
11506 		}
11507 
11508 		ret = ath12k_mac_setup_bcn_tmpl(arvif);
11509 		if (ret)
11510 			ath12k_warn(ab, "failed to update bcn tmpl during csa: %d\n",
11511 				    ret);
11512 
11513 		memset(&params, 0, sizeof(params));
11514 		params.vdev_id = arvif->vdev_id;
11515 		params.aid = ahvif->aid;
11516 		params.bssid = arvif->bssid;
11517 		params.tx_bssid = ath12k_mac_get_tx_bssid(arvif);
11518 		if (params.tx_bssid) {
11519 			params.nontx_profile_idx = link_conf->bssid_index;
11520 			params.nontx_profile_cnt = 1 << link_conf->bssid_indicator;
11521 		}
11522 		ret = ath12k_wmi_vdev_up(arvif->ar, &params);
11523 		if (ret) {
11524 			ath12k_warn(ab, "failed to bring vdev up %d: %d\n",
11525 				    arvif->vdev_id, ret);
11526 			continue;
11527 		}
11528 	}
11529 
11530 	/* Restart the internal monitor vdev on new channel */
11531 	if (!monitor_vif && ar->monitor_vdev_created) {
11532 		if (!ath12k_mac_monitor_stop(ar))
11533 			ath12k_mac_monitor_start(ar);
11534 	}
11535 }
11536 
11537 static void
11538 ath12k_mac_update_active_vif_chan(struct ath12k *ar,
11539 				  struct ieee80211_chanctx_conf *ctx)
11540 {
11541 	struct ath12k_mac_change_chanctx_arg arg = { .ctx = ctx, .ar = ar };
11542 	struct ieee80211_hw *hw = ath12k_ar_to_hw(ar);
11543 
11544 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
11545 
11546 	ieee80211_iterate_active_interfaces_atomic(hw,
11547 						   IEEE80211_IFACE_ITER_NORMAL,
11548 						   ath12k_mac_change_chanctx_cnt_iter,
11549 						   &arg);
11550 	if (arg.n_vifs == 0)
11551 		return;
11552 
11553 	arg.vifs = kzalloc_objs(arg.vifs[0], arg.n_vifs);
11554 	if (!arg.vifs)
11555 		return;
11556 
11557 	ieee80211_iterate_active_interfaces_atomic(hw,
11558 						   IEEE80211_IFACE_ITER_NORMAL,
11559 						   ath12k_mac_change_chanctx_fill_iter,
11560 						   &arg);
11561 
11562 	ath12k_mac_update_vif_chan(ar, arg.vifs, arg.n_vifs);
11563 
11564 	kfree(arg.vifs);
11565 }
11566 
11567 void ath12k_mac_op_change_chanctx(struct ieee80211_hw *hw,
11568 				  struct ieee80211_chanctx_conf *ctx,
11569 				  u32 changed)
11570 {
11571 	struct ath12k *ar;
11572 	struct ath12k_base *ab;
11573 
11574 	lockdep_assert_wiphy(hw->wiphy);
11575 
11576 	ar = ath12k_get_ar_by_ctx(hw, ctx);
11577 	if (!ar)
11578 		return;
11579 
11580 	ab = ar->ab;
11581 
11582 	ath12k_dbg(ab, ATH12K_DBG_MAC,
11583 		   "mac chanctx change freq %u width %d ptr %p changed %x\n",
11584 		   ctx->def.chan->center_freq, ctx->def.width, ctx, changed);
11585 
11586 	/* This shouldn't really happen because channel switching should use
11587 	 * switch_vif_chanctx().
11588 	 */
11589 	if (WARN_ON(changed & IEEE80211_CHANCTX_CHANGE_CHANNEL))
11590 		return;
11591 
11592 	if (changed & IEEE80211_CHANCTX_CHANGE_WIDTH ||
11593 	    changed & IEEE80211_CHANCTX_CHANGE_RADAR ||
11594 	    changed & IEEE80211_CHANCTX_CHANGE_PUNCTURING)
11595 		ath12k_mac_update_active_vif_chan(ar, ctx);
11596 
11597 	/* TODO: Recalc radar detection */
11598 }
11599 EXPORT_SYMBOL(ath12k_mac_op_change_chanctx);
11600 
11601 static int ath12k_start_vdev_delay(struct ath12k *ar,
11602 				   struct ath12k_link_vif *arvif)
11603 {
11604 	struct ath12k_base *ab = ar->ab;
11605 	struct ath12k_vif *ahvif = arvif->ahvif;
11606 	struct ieee80211_vif *vif = ath12k_ahvif_to_vif(arvif->ahvif);
11607 	struct ieee80211_chanctx_conf *chanctx;
11608 	struct ieee80211_bss_conf *link_conf;
11609 	int ret;
11610 
11611 	if (WARN_ON(arvif->is_started))
11612 		return -EBUSY;
11613 
11614 	link_conf = ath12k_mac_get_link_bss_conf(arvif);
11615 	if (!link_conf) {
11616 		ath12k_warn(ab, "failed to get link conf for vdev %u\n", arvif->vdev_id);
11617 		return -EINVAL;
11618 	}
11619 
11620 	chanctx	= wiphy_dereference(ath12k_ar_to_hw(arvif->ar)->wiphy,
11621 				    link_conf->chanctx_conf);
11622 	ret = ath12k_mac_vdev_start(arvif, chanctx);
11623 	if (ret) {
11624 		ath12k_warn(ab, "failed to start vdev %i addr %pM on freq %d: %d\n",
11625 			    arvif->vdev_id, vif->addr,
11626 			    chanctx->def.chan->center_freq, ret);
11627 		return ret;
11628 	}
11629 
11630 	if (ahvif->vdev_type == WMI_VDEV_TYPE_MONITOR) {
11631 		ret = ath12k_monitor_vdev_up(ar, arvif->vdev_id);
11632 		if (ret) {
11633 			ath12k_warn(ab, "failed put monitor up: %d\n", ret);
11634 			return ret;
11635 		}
11636 	}
11637 
11638 	arvif->is_started = true;
11639 
11640 	/* TODO: Setup ps and cts/rts protection */
11641 	return 0;
11642 }
11643 
11644 static u8 ath12k_mac_get_num_pwr_levels(struct cfg80211_chan_def *chan_def)
11645 {
11646 	if (chan_def->chan->flags & IEEE80211_CHAN_PSD) {
11647 		switch (chan_def->width) {
11648 		case NL80211_CHAN_WIDTH_20:
11649 			return 1;
11650 		case NL80211_CHAN_WIDTH_40:
11651 			return 2;
11652 		case NL80211_CHAN_WIDTH_80:
11653 			return 4;
11654 		case NL80211_CHAN_WIDTH_160:
11655 			return 8;
11656 		case NL80211_CHAN_WIDTH_320:
11657 			return 16;
11658 		default:
11659 			return 1;
11660 		}
11661 	} else {
11662 		switch (chan_def->width) {
11663 		case NL80211_CHAN_WIDTH_20:
11664 			return 1;
11665 		case NL80211_CHAN_WIDTH_40:
11666 			return 2;
11667 		case NL80211_CHAN_WIDTH_80:
11668 			return 3;
11669 		case NL80211_CHAN_WIDTH_160:
11670 			return 4;
11671 		case NL80211_CHAN_WIDTH_320:
11672 			return 5;
11673 		default:
11674 			return 1;
11675 		}
11676 	}
11677 }
11678 
11679 static u16 ath12k_mac_get_6ghz_start_frequency(struct cfg80211_chan_def *chan_def)
11680 {
11681 	u16 diff_seq;
11682 
11683 	/* It is to get the lowest channel number's center frequency of the chan.
11684 	 * For example,
11685 	 * bandwidth=40 MHz, center frequency is 5965, lowest channel is 1
11686 	 * with center frequency 5955, its diff is 5965 - 5955 = 10.
11687 	 * bandwidth=80 MHz, center frequency is 5985, lowest channel is 1
11688 	 * with center frequency 5955, its diff is 5985 - 5955 = 30.
11689 	 * bandwidth=160 MHz, center frequency is 6025, lowest channel is 1
11690 	 * with center frequency 5955, its diff is 6025 - 5955 = 70.
11691 	 * bandwidth=320 MHz, center frequency is 6105, lowest channel is 1
11692 	 * with center frequency 5955, its diff is 6105 - 5955 = 70.
11693 	 */
11694 	switch (chan_def->width) {
11695 	case NL80211_CHAN_WIDTH_320:
11696 		diff_seq = 150;
11697 		break;
11698 	case NL80211_CHAN_WIDTH_160:
11699 		diff_seq = 70;
11700 		break;
11701 	case NL80211_CHAN_WIDTH_80:
11702 		diff_seq = 30;
11703 		break;
11704 	case NL80211_CHAN_WIDTH_40:
11705 		diff_seq = 10;
11706 		break;
11707 	default:
11708 		diff_seq = 0;
11709 	}
11710 
11711 	return chan_def->center_freq1 - diff_seq;
11712 }
11713 
11714 static u16 ath12k_mac_get_seg_freq(struct cfg80211_chan_def *chan_def,
11715 				   u16 start_seq, u8 seq)
11716 {
11717 	u16 seg_seq;
11718 
11719 	/* It is to get the center frequency of the specific bandwidth.
11720 	 * start_seq means the lowest channel number's center frequency.
11721 	 * seq 0/1/2/3 means 20 MHz/40 MHz/80 MHz/160 MHz.
11722 	 * For example,
11723 	 * lowest channel is 1, its center frequency 5955,
11724 	 * center frequency is 5955 when bandwidth=20 MHz, its diff is 5955 - 5955 = 0.
11725 	 * lowest channel is 1, its center frequency 5955,
11726 	 * center frequency is 5965 when bandwidth=40 MHz, its diff is 5965 - 5955 = 10.
11727 	 * lowest channel is 1, its center frequency 5955,
11728 	 * center frequency is 5985 when bandwidth=80 MHz, its diff is 5985 - 5955 = 30.
11729 	 * lowest channel is 1, its center frequency 5955,
11730 	 * center frequency is 6025 when bandwidth=160 MHz, its diff is 6025 - 5955 = 70.
11731 	 */
11732 	seg_seq = 10 * (BIT(seq) - 1);
11733 	return seg_seq + start_seq;
11734 }
11735 
11736 static void ath12k_mac_get_psd_channel(struct ath12k *ar,
11737 				       u16 step_freq,
11738 				       u16 *start_freq,
11739 				       u16 *center_freq,
11740 				       u8 i,
11741 				       struct ieee80211_channel **temp_chan,
11742 				       s8 *tx_power)
11743 {
11744 	/* It is to get the center frequency for each 20 MHz.
11745 	 * For example, if the chan is 160 MHz and center frequency is 6025,
11746 	 * then it include 8 channels, they are 1/5/9/13/17/21/25/29,
11747 	 * channel number 1's center frequency is 5955, it is parameter start_freq.
11748 	 * parameter i is the step of the 8 channels. i is 0~7 for the 8 channels.
11749 	 * the channel 1/5/9/13/17/21/25/29 maps i=0/1/2/3/4/5/6/7,
11750 	 * and maps its center frequency is 5955/5975/5995/6015/6035/6055/6075/6095,
11751 	 * the gap is 20 for each channel, parameter step_freq means the gap.
11752 	 * after get the center frequency of each channel, it is easy to find the
11753 	 * struct ieee80211_channel of it and get the max_reg_power.
11754 	 */
11755 	*center_freq = *start_freq + i * step_freq;
11756 	*temp_chan = ieee80211_get_channel(ar->ah->hw->wiphy, *center_freq);
11757 	*tx_power = (*temp_chan)->max_reg_power;
11758 }
11759 
11760 static void ath12k_mac_get_eirp_power(struct ath12k *ar,
11761 				      u16 *start_freq,
11762 				      u16 *center_freq,
11763 				      u8 i,
11764 				      struct ieee80211_channel **temp_chan,
11765 				      struct cfg80211_chan_def *def,
11766 				      s8 *tx_power)
11767 {
11768 	/* It is to get the center frequency for 20 MHz/40 MHz/80 MHz/
11769 	 * 160 MHz bandwidth, and then plus 10 to the center frequency,
11770 	 * it is the center frequency of a channel number.
11771 	 * For example, when configured channel number is 1.
11772 	 * center frequency is 5965 when bandwidth=40 MHz, after plus 10, it is 5975,
11773 	 * then it is channel number 5.
11774 	 * center frequency is 5985 when bandwidth=80 MHz, after plus 10, it is 5995,
11775 	 * then it is channel number 9.
11776 	 * center frequency is 6025 when bandwidth=160 MHz, after plus 10, it is 6035,
11777 	 * then it is channel number 17.
11778 	 * after get the center frequency of each channel, it is easy to find the
11779 	 * struct ieee80211_channel of it and get the max_reg_power.
11780 	 */
11781 	*center_freq = ath12k_mac_get_seg_freq(def, *start_freq, i);
11782 
11783 	/* For the 20 MHz, its center frequency is same with same channel */
11784 	if (i != 0)
11785 		*center_freq += 10;
11786 
11787 	*temp_chan = ieee80211_get_channel(ar->ah->hw->wiphy, *center_freq);
11788 	*tx_power = (*temp_chan)->max_reg_power;
11789 }
11790 
11791 void ath12k_mac_fill_reg_tpc_info(struct ath12k *ar,
11792 				  struct ath12k_link_vif *arvif,
11793 				  struct ieee80211_chanctx_conf *ctx)
11794 {
11795 	struct ath12k_base *ab = ar->ab;
11796 	struct ath12k_reg_tpc_power_info *reg_tpc_info = &arvif->reg_tpc_info;
11797 	struct ieee80211_bss_conf *bss_conf = ath12k_mac_get_link_bss_conf(arvif);
11798 	struct ieee80211_channel *chan, *temp_chan;
11799 	u8 pwr_lvl_idx, num_pwr_levels, pwr_reduction;
11800 	bool is_psd_power = false, is_tpe_present = false;
11801 	s8 max_tx_power[ATH12K_NUM_PWR_LEVELS], psd_power, tx_power;
11802 	s8 eirp_power = 0;
11803 	struct ath12k_vif *ahvif = arvif->ahvif;
11804 	u16 start_freq, center_freq;
11805 	u8 reg_6ghz_power_mode;
11806 
11807 	chan = ctx->def.chan;
11808 	start_freq = ath12k_mac_get_6ghz_start_frequency(&ctx->def);
11809 	pwr_reduction = bss_conf->pwr_reduction;
11810 
11811 	if (arvif->reg_tpc_info.num_pwr_levels) {
11812 		is_tpe_present = true;
11813 		num_pwr_levels = arvif->reg_tpc_info.num_pwr_levels;
11814 	} else {
11815 		num_pwr_levels = ath12k_mac_get_num_pwr_levels(&ctx->def);
11816 	}
11817 
11818 	for (pwr_lvl_idx = 0; pwr_lvl_idx < num_pwr_levels; pwr_lvl_idx++) {
11819 		/* STA received TPE IE*/
11820 		if (is_tpe_present) {
11821 			/* local power is PSD power*/
11822 			if (chan->flags & IEEE80211_CHAN_PSD) {
11823 				/* Connecting AP is psd power */
11824 				if (reg_tpc_info->is_psd_power) {
11825 					is_psd_power = true;
11826 					ath12k_mac_get_psd_channel(ar, 20,
11827 								   &start_freq,
11828 								   &center_freq,
11829 								   pwr_lvl_idx,
11830 								   &temp_chan,
11831 								   &tx_power);
11832 					psd_power = temp_chan->psd;
11833 					eirp_power = tx_power;
11834 					max_tx_power[pwr_lvl_idx] =
11835 						min_t(s8,
11836 						      psd_power,
11837 						      reg_tpc_info->tpe[pwr_lvl_idx]);
11838 				/* Connecting AP is not psd power */
11839 				} else {
11840 					ath12k_mac_get_eirp_power(ar,
11841 								  &start_freq,
11842 								  &center_freq,
11843 								  pwr_lvl_idx,
11844 								  &temp_chan,
11845 								  &ctx->def,
11846 								  &tx_power);
11847 					psd_power = temp_chan->psd;
11848 					/* convert psd power to EIRP power based
11849 					 * on channel width
11850 					 */
11851 					tx_power =
11852 						min_t(s8, tx_power,
11853 						      psd_power + 13 + pwr_lvl_idx * 3);
11854 					max_tx_power[pwr_lvl_idx] =
11855 						min_t(s8,
11856 						      tx_power,
11857 						      reg_tpc_info->tpe[pwr_lvl_idx]);
11858 				}
11859 			/* local power is not PSD power */
11860 			} else {
11861 				/* Connecting AP is psd power */
11862 				if (reg_tpc_info->is_psd_power) {
11863 					is_psd_power = true;
11864 					ath12k_mac_get_psd_channel(ar, 20,
11865 								   &start_freq,
11866 								   &center_freq,
11867 								   pwr_lvl_idx,
11868 								   &temp_chan,
11869 								   &tx_power);
11870 					eirp_power = tx_power;
11871 					max_tx_power[pwr_lvl_idx] =
11872 						reg_tpc_info->tpe[pwr_lvl_idx];
11873 				/* Connecting AP is not psd power */
11874 				} else {
11875 					ath12k_mac_get_eirp_power(ar,
11876 								  &start_freq,
11877 								  &center_freq,
11878 								  pwr_lvl_idx,
11879 								  &temp_chan,
11880 								  &ctx->def,
11881 								  &tx_power);
11882 					max_tx_power[pwr_lvl_idx] =
11883 						min_t(s8,
11884 						      tx_power,
11885 						      reg_tpc_info->tpe[pwr_lvl_idx]);
11886 				}
11887 			}
11888 		/* STA not received TPE IE */
11889 		} else {
11890 			/* local power is PSD power*/
11891 			if (chan->flags & IEEE80211_CHAN_PSD) {
11892 				is_psd_power = true;
11893 				ath12k_mac_get_psd_channel(ar, 20,
11894 							   &start_freq,
11895 							   &center_freq,
11896 							   pwr_lvl_idx,
11897 							   &temp_chan,
11898 							   &tx_power);
11899 				psd_power = temp_chan->psd;
11900 				eirp_power = tx_power;
11901 				max_tx_power[pwr_lvl_idx] = psd_power;
11902 			} else {
11903 				ath12k_mac_get_eirp_power(ar,
11904 							  &start_freq,
11905 							  &center_freq,
11906 							  pwr_lvl_idx,
11907 							  &temp_chan,
11908 							  &ctx->def,
11909 							  &tx_power);
11910 				max_tx_power[pwr_lvl_idx] = tx_power;
11911 			}
11912 		}
11913 
11914 		if (is_psd_power) {
11915 			/* If AP local power constraint is present */
11916 			if (pwr_reduction)
11917 				eirp_power = eirp_power - pwr_reduction;
11918 
11919 			/* If firmware updated max tx power is non zero, then take
11920 			 * the min of firmware updated ap tx power
11921 			 * and max power derived from above mentioned parameters.
11922 			 */
11923 			ath12k_dbg(ab, ATH12K_DBG_MAC,
11924 				   "eirp power : %d firmware report power : %d\n",
11925 				   eirp_power, ar->max_allowed_tx_power);
11926 			/* Firmware reports lower max_allowed_tx_power during vdev
11927 			 * start response. In case of 6 GHz, firmware is not aware
11928 			 * of EIRP power unless driver sets EIRP power through WMI
11929 			 * TPC command. So radio which does not support idle power
11930 			 * save can set maximum calculated EIRP power directly to
11931 			 * firmware through TPC command without min comparison with
11932 			 * vdev start response's max_allowed_tx_power.
11933 			 */
11934 			if (ar->max_allowed_tx_power && ab->hw_params->idle_ps)
11935 				eirp_power = min_t(s8,
11936 						   eirp_power,
11937 						   ar->max_allowed_tx_power);
11938 		} else {
11939 			/* If AP local power constraint is present */
11940 			if (pwr_reduction)
11941 				max_tx_power[pwr_lvl_idx] =
11942 					max_tx_power[pwr_lvl_idx] - pwr_reduction;
11943 			/* If firmware updated max tx power is non zero, then take
11944 			 * the min of firmware updated ap tx power
11945 			 * and max power derived from above mentioned parameters.
11946 			 */
11947 			if (ar->max_allowed_tx_power && ab->hw_params->idle_ps)
11948 				max_tx_power[pwr_lvl_idx] =
11949 					min_t(s8,
11950 					      max_tx_power[pwr_lvl_idx],
11951 					      ar->max_allowed_tx_power);
11952 		}
11953 		reg_tpc_info->chan_power_info[pwr_lvl_idx].chan_cfreq = center_freq;
11954 		reg_tpc_info->chan_power_info[pwr_lvl_idx].tx_power =
11955 			max_tx_power[pwr_lvl_idx];
11956 	}
11957 
11958 	reg_tpc_info->num_pwr_levels = num_pwr_levels;
11959 	reg_tpc_info->is_psd_power = is_psd_power;
11960 	reg_tpc_info->eirp_power = eirp_power;
11961 	if (ahvif->vdev_type == WMI_VDEV_TYPE_STA)
11962 		reg_6ghz_power_mode = bss_conf->power_type;
11963 	else
11964 		/* For now, LPI is the only supported AP power mode */
11965 		reg_6ghz_power_mode = IEEE80211_REG_LPI_AP;
11966 
11967 	reg_tpc_info->ap_power_type =
11968 		ath12k_reg_ap_pwr_convert(reg_6ghz_power_mode);
11969 }
11970 
11971 static void ath12k_mac_parse_tx_pwr_env(struct ath12k *ar,
11972 					struct ath12k_link_vif *arvif)
11973 {
11974 	struct ieee80211_bss_conf *bss_conf = ath12k_mac_get_link_bss_conf(arvif);
11975 	struct ath12k_reg_tpc_power_info *tpc_info = &arvif->reg_tpc_info;
11976 	struct ieee80211_parsed_tpe_eirp *local_non_psd, *reg_non_psd;
11977 	struct ieee80211_parsed_tpe_psd *local_psd, *reg_psd;
11978 	struct ieee80211_parsed_tpe *tpe = &bss_conf->tpe;
11979 	enum wmi_reg_6g_client_type client_type;
11980 	struct ath12k_reg_info *reg_info;
11981 	struct ath12k_base *ab = ar->ab;
11982 	bool psd_valid, non_psd_valid;
11983 	int i;
11984 
11985 	reg_info = ab->reg_info[ar->pdev_idx];
11986 	client_type = reg_info->client_type;
11987 
11988 	local_psd = &tpe->psd_local[client_type];
11989 	reg_psd = &tpe->psd_reg_client[client_type];
11990 	local_non_psd = &tpe->max_local[client_type];
11991 	reg_non_psd = &tpe->max_reg_client[client_type];
11992 
11993 	psd_valid = local_psd->valid | reg_psd->valid;
11994 	non_psd_valid = local_non_psd->valid | reg_non_psd->valid;
11995 
11996 	if (!psd_valid && !non_psd_valid) {
11997 		ath12k_warn(ab,
11998 			    "no transmit power envelope match client power type %d\n",
11999 			    client_type);
12000 		return;
12001 	}
12002 
12003 	if (psd_valid) {
12004 		tpc_info->is_psd_power = true;
12005 
12006 		tpc_info->num_pwr_levels = max(local_psd->count,
12007 					       reg_psd->count);
12008 		tpc_info->num_pwr_levels =
12009 				min3(tpc_info->num_pwr_levels,
12010 				     IEEE80211_TPE_PSD_ENTRIES_320MHZ,
12011 				     ATH12K_NUM_PWR_LEVELS);
12012 
12013 		for (i = 0; i < tpc_info->num_pwr_levels; i++) {
12014 			tpc_info->tpe[i] = min(local_psd->power[i],
12015 					       reg_psd->power[i]) / 2;
12016 			ath12k_dbg(ab, ATH12K_DBG_MAC,
12017 				   "TPE PSD power[%d] : %d\n",
12018 				   i, tpc_info->tpe[i]);
12019 		}
12020 	} else {
12021 		tpc_info->is_psd_power = false;
12022 		tpc_info->eirp_power = 0;
12023 
12024 		tpc_info->num_pwr_levels = max(local_non_psd->count,
12025 					       reg_non_psd->count);
12026 		tpc_info->num_pwr_levels =
12027 				min3(tpc_info->num_pwr_levels,
12028 				     IEEE80211_TPE_EIRP_ENTRIES_320MHZ,
12029 				     ATH12K_NUM_PWR_LEVELS);
12030 
12031 		for (i = 0; i < tpc_info->num_pwr_levels; i++) {
12032 			tpc_info->tpe[i] = min(local_non_psd->power[i],
12033 					       reg_non_psd->power[i]) / 2;
12034 			ath12k_dbg(ab, ATH12K_DBG_MAC,
12035 				   "non PSD power[%d] : %d\n",
12036 				   i, tpc_info->tpe[i]);
12037 		}
12038 	}
12039 }
12040 
12041 int
12042 ath12k_mac_op_assign_vif_chanctx(struct ieee80211_hw *hw,
12043 				 struct ieee80211_vif *vif,
12044 				 struct ieee80211_bss_conf *link_conf,
12045 				 struct ieee80211_chanctx_conf *ctx)
12046 {
12047 	struct ath12k_hw *ah = ath12k_hw_to_ah(hw);
12048 	struct ath12k *ar;
12049 	struct ath12k_base *ab;
12050 	struct ath12k_vif *ahvif = ath12k_vif_to_ahvif(vif);
12051 	u8 link_id = link_conf->link_id;
12052 	struct ath12k_link_vif *arvif;
12053 	int ret;
12054 
12055 	lockdep_assert_wiphy(hw->wiphy);
12056 
12057 	/* For multi radio wiphy, the vdev was not created during add_interface
12058 	 * create now since we have a channel ctx now to assign to a specific ar/fw
12059 	 */
12060 	arvif = ath12k_mac_assign_link_vif(ah, vif, link_id);
12061 	if (!arvif) {
12062 		WARN_ON(1);
12063 		return -ENOMEM;
12064 	}
12065 
12066 	ar = ath12k_mac_assign_vif_to_vdev(hw, arvif, ctx);
12067 	if (!ar) {
12068 		ath12k_hw_warn(ah, "failed to assign chanctx for vif %pM link id %u link vif is already started",
12069 			       vif->addr, link_id);
12070 		return -EINVAL;
12071 	}
12072 
12073 	ab = ar->ab;
12074 
12075 	ath12k_dbg(ab, ATH12K_DBG_MAC,
12076 		   "mac chanctx assign ptr %p vdev_id %i\n",
12077 		   ctx, arvif->vdev_id);
12078 
12079 	if (ath12k_wmi_supports_6ghz_cc_ext(ar) &&
12080 	    ctx->def.chan->band == NL80211_BAND_6GHZ &&
12081 	    ahvif->vdev_type == WMI_VDEV_TYPE_STA)
12082 		ath12k_mac_parse_tx_pwr_env(ar, arvif);
12083 
12084 	arvif->punct_bitmap = ctx->def.punctured;
12085 
12086 	/* for some targets bss peer must be created before vdev_start */
12087 	if (ab->hw_params->vdev_start_delay &&
12088 	    ahvif->vdev_type != WMI_VDEV_TYPE_AP &&
12089 	    ahvif->vdev_type != WMI_VDEV_TYPE_MONITOR &&
12090 	    !ath12k_dp_link_peer_exist_by_vdev_id(ath12k_ab_to_dp(ab), arvif->vdev_id)) {
12091 		ret = 0;
12092 		goto out;
12093 	}
12094 
12095 	if (WARN_ON(arvif->is_started)) {
12096 		ret = -EBUSY;
12097 		goto out;
12098 	}
12099 
12100 	if (ahvif->vdev_type == WMI_VDEV_TYPE_MONITOR) {
12101 		ret = ath12k_mac_monitor_start(ar);
12102 		if (ret) {
12103 			ath12k_mac_monitor_vdev_delete(ar);
12104 			goto out;
12105 		}
12106 
12107 		arvif->is_started = true;
12108 		goto out;
12109 	}
12110 
12111 	ret = ath12k_mac_vdev_start(arvif, ctx);
12112 	if (ret) {
12113 		ath12k_warn(ab, "failed to start vdev %i addr %pM on freq %d: %d\n",
12114 			    arvif->vdev_id, vif->addr,
12115 			    ctx->def.chan->center_freq, ret);
12116 		goto out;
12117 	}
12118 
12119 	arvif->is_started = true;
12120 
12121 	/* TODO: Setup ps and cts/rts protection */
12122 
12123 out:
12124 	return ret;
12125 }
12126 EXPORT_SYMBOL(ath12k_mac_op_assign_vif_chanctx);
12127 
12128 void
12129 ath12k_mac_op_unassign_vif_chanctx(struct ieee80211_hw *hw,
12130 				   struct ieee80211_vif *vif,
12131 				   struct ieee80211_bss_conf *link_conf,
12132 				   struct ieee80211_chanctx_conf *ctx)
12133 {
12134 	struct ath12k *ar;
12135 	struct ath12k_base *ab;
12136 	struct ath12k_vif *ahvif = ath12k_vif_to_ahvif(vif);
12137 	struct ath12k_link_vif *arvif;
12138 	u8 link_id = link_conf->link_id;
12139 	int ret;
12140 
12141 	lockdep_assert_wiphy(hw->wiphy);
12142 
12143 	arvif = wiphy_dereference(hw->wiphy, ahvif->link[link_id]);
12144 
12145 	/* The vif is expected to be attached to an ar's VDEV.
12146 	 * We leave the vif/vdev in this function as is
12147 	 * and not delete the vdev symmetric to assign_vif_chanctx()
12148 	 * the VDEV will be deleted and unassigned either during
12149 	 * remove_interface() or when there is a change in channel
12150 	 * that moves the vif to a new ar
12151 	 */
12152 	if (!arvif || !arvif->is_created)
12153 		return;
12154 
12155 	ar = arvif->ar;
12156 	ab = ar->ab;
12157 
12158 	ath12k_dbg(ab, ATH12K_DBG_MAC,
12159 		   "mac chanctx unassign ptr %p vdev_id %i\n",
12160 		   ctx, arvif->vdev_id);
12161 
12162 	WARN_ON(!arvif->is_started);
12163 
12164 	if (ahvif->vdev_type == WMI_VDEV_TYPE_MONITOR) {
12165 		ret = ath12k_mac_monitor_stop(ar);
12166 		if (ret)
12167 			return;
12168 
12169 		arvif->is_started = false;
12170 	}
12171 
12172 	if (ahvif->vdev_type != WMI_VDEV_TYPE_STA &&
12173 	    ahvif->vdev_type != WMI_VDEV_TYPE_MONITOR) {
12174 		ath12k_bss_disassoc(ar, arvif);
12175 		ret = ath12k_mac_vdev_stop(arvif);
12176 		if (ret)
12177 			ath12k_warn(ab, "failed to stop vdev %i: %d\n",
12178 				    arvif->vdev_id, ret);
12179 	}
12180 	arvif->is_started = false;
12181 
12182 	if (test_bit(WMI_TLV_SERVICE_11D_OFFLOAD, ab->wmi_ab.svc_map) &&
12183 	    ahvif->vdev_type == WMI_VDEV_TYPE_STA &&
12184 	    ahvif->vdev_subtype == WMI_VDEV_SUBTYPE_NONE &&
12185 	    ar->state_11d != ATH12K_11D_PREPARING) {
12186 		reinit_completion(&ar->completed_11d_scan);
12187 		ar->state_11d = ATH12K_11D_PREPARING;
12188 	}
12189 
12190 	if (ar->scan.arvif == arvif && ar->scan.state == ATH12K_SCAN_RUNNING) {
12191 		ath12k_scan_abort(ar);
12192 		ar->scan.arvif = NULL;
12193 	}
12194 }
12195 EXPORT_SYMBOL(ath12k_mac_op_unassign_vif_chanctx);
12196 
12197 int
12198 ath12k_mac_op_switch_vif_chanctx(struct ieee80211_hw *hw,
12199 				 struct ieee80211_vif_chanctx_switch *vifs,
12200 				 int n_vifs,
12201 				 enum ieee80211_chanctx_switch_mode mode)
12202 {
12203 	struct ath12k *ar;
12204 
12205 	lockdep_assert_wiphy(hw->wiphy);
12206 
12207 	ar = ath12k_get_ar_by_ctx(hw, vifs->old_ctx);
12208 	if (!ar)
12209 		return -EINVAL;
12210 
12211 	/* Switching channels across radio is not allowed */
12212 	if (ar != ath12k_get_ar_by_ctx(hw, vifs->new_ctx))
12213 		return -EINVAL;
12214 
12215 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
12216 		   "mac chanctx switch n_vifs %d mode %d\n",
12217 		   n_vifs, mode);
12218 	ath12k_mac_update_vif_chan(ar, vifs, n_vifs);
12219 
12220 	return 0;
12221 }
12222 EXPORT_SYMBOL(ath12k_mac_op_switch_vif_chanctx);
12223 
12224 static int
12225 ath12k_set_vdev_param_to_all_vifs(struct ath12k *ar, int param, u32 value)
12226 {
12227 	struct ath12k_link_vif *arvif;
12228 	int ret = 0;
12229 
12230 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
12231 
12232 	list_for_each_entry(arvif, &ar->arvifs, list) {
12233 		ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "setting mac vdev %d param %d value %d\n",
12234 			   param, arvif->vdev_id, value);
12235 
12236 		ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
12237 						    param, value);
12238 		if (ret) {
12239 			ath12k_warn(ar->ab, "failed to set param %d for vdev %d: %d\n",
12240 				    param, arvif->vdev_id, ret);
12241 			break;
12242 		}
12243 	}
12244 
12245 	return ret;
12246 }
12247 
12248 /* mac80211 stores device specific RTS/Fragmentation threshold value,
12249  * this is set interface specific to firmware from ath12k driver
12250  */
12251 int ath12k_mac_op_set_rts_threshold(struct ieee80211_hw *hw,
12252 				    int radio_idx, u32 value)
12253 {
12254 	struct ath12k_hw *ah = ath12k_hw_to_ah(hw);
12255 	struct wiphy *wiphy = hw->wiphy;
12256 	struct ath12k *ar;
12257 	int param_id = WMI_VDEV_PARAM_RTS_THRESHOLD;
12258 	int ret = 0, ret_err, i;
12259 
12260 	lockdep_assert_wiphy(hw->wiphy);
12261 
12262 	if (radio_idx >= wiphy->n_radio || radio_idx < -1)
12263 		return -EINVAL;
12264 
12265 	if (radio_idx != -1) {
12266 		/* Update RTS threshold in specified radio */
12267 		ar = ath12k_ah_to_ar(ah, radio_idx);
12268 		ret = ath12k_set_vdev_param_to_all_vifs(ar, param_id, value);
12269 		if (ret) {
12270 			ath12k_warn(ar->ab,
12271 				    "failed to set RTS config for all vdevs of pdev %d",
12272 				    ar->pdev->pdev_id);
12273 			return ret;
12274 		}
12275 
12276 		ar->rts_threshold = value;
12277 		return 0;
12278 	}
12279 
12280 	/* Radio_index passed is -1, so set RTS threshold for all radios. */
12281 	for_each_ar(ah, ar, i) {
12282 		ret = ath12k_set_vdev_param_to_all_vifs(ar, param_id, value);
12283 		if (ret) {
12284 			ath12k_warn(ar->ab, "failed to set RTS config for all vdevs of pdev %d",
12285 				    ar->pdev->pdev_id);
12286 			break;
12287 		}
12288 	}
12289 	if (!ret) {
12290 		/* Setting new RTS threshold for vdevs of all radios passed, so update
12291 		 * the RTS threshold value for all radios
12292 		 */
12293 		for_each_ar(ah, ar, i)
12294 			ar->rts_threshold = value;
12295 		return 0;
12296 	}
12297 
12298 	/* RTS threshold config failed, revert to the previous RTS threshold */
12299 	for (i = i - 1; i >= 0; i--) {
12300 		ar = ath12k_ah_to_ar(ah, i);
12301 		ret_err = ath12k_set_vdev_param_to_all_vifs(ar, param_id,
12302 							    ar->rts_threshold);
12303 		if (ret_err)
12304 			ath12k_warn(ar->ab,
12305 				    "failed to restore RTS threshold for all vdevs of pdev %d",
12306 				    ar->pdev->pdev_id);
12307 	}
12308 
12309 	return ret;
12310 }
12311 EXPORT_SYMBOL(ath12k_mac_op_set_rts_threshold);
12312 
12313 int ath12k_mac_op_set_frag_threshold(struct ieee80211_hw *hw,
12314 				     int radio_idx, u32 value)
12315 {
12316 	/* Even though there's a WMI vdev param for fragmentation threshold no
12317 	 * known firmware actually implements it. Moreover it is not possible to
12318 	 * rely frame fragmentation to mac80211 because firmware clears the
12319 	 * "more fragments" bit in frame control making it impossible for remote
12320 	 * devices to reassemble frames.
12321 	 *
12322 	 * Hence implement a dummy callback just to say fragmentation isn't
12323 	 * supported. This effectively prevents mac80211 from doing frame
12324 	 * fragmentation in software.
12325 	 */
12326 
12327 	lockdep_assert_wiphy(hw->wiphy);
12328 
12329 	return -EOPNOTSUPP;
12330 }
12331 EXPORT_SYMBOL(ath12k_mac_op_set_frag_threshold);
12332 
12333 static int ath12k_mac_flush(struct ath12k *ar)
12334 {
12335 	long time_left;
12336 	int ret = 0;
12337 
12338 	time_left = wait_event_timeout(ar->dp.tx_empty_waitq,
12339 				       (atomic_read(&ar->dp.num_tx_pending) == 0),
12340 				       ATH12K_FLUSH_TIMEOUT);
12341 	if (time_left == 0) {
12342 		ath12k_warn(ar->ab,
12343 			    "failed to flush transmit queue, data pkts pending %d\n",
12344 			    atomic_read(&ar->dp.num_tx_pending));
12345 		ret = -ETIMEDOUT;
12346 	}
12347 
12348 	time_left = wait_event_timeout(ar->txmgmt_empty_waitq,
12349 				       (atomic_read(&ar->num_pending_mgmt_tx) == 0),
12350 				       ATH12K_FLUSH_TIMEOUT);
12351 	if (time_left == 0) {
12352 		ath12k_warn(ar->ab,
12353 			    "failed to flush mgmt transmit queue, mgmt pkts pending %d\n",
12354 			    atomic_read(&ar->num_pending_mgmt_tx));
12355 		ret = -ETIMEDOUT;
12356 	}
12357 
12358 	return ret;
12359 }
12360 
12361 int ath12k_mac_wait_tx_complete(struct ath12k *ar)
12362 {
12363 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
12364 
12365 	ath12k_mac_drain_tx(ar);
12366 	return ath12k_mac_flush(ar);
12367 }
12368 
12369 void ath12k_mac_op_flush(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
12370 			 u32 queues, bool drop)
12371 {
12372 	struct ath12k_hw *ah = ath12k_hw_to_ah(hw);
12373 	struct ath12k_link_vif *arvif;
12374 	struct ath12k_vif *ahvif;
12375 	unsigned long links;
12376 	struct ath12k *ar;
12377 	u8 link_id;
12378 	int i;
12379 
12380 	lockdep_assert_wiphy(hw->wiphy);
12381 
12382 	if (drop)
12383 		return;
12384 
12385 	for_each_ar(ah, ar, i)
12386 		wiphy_work_flush(hw->wiphy, &ar->wmi_mgmt_tx_work);
12387 
12388 	/* vif can be NULL when flush() is considered for hw */
12389 	if (!vif) {
12390 		for_each_ar(ah, ar, i)
12391 			ath12k_mac_flush(ar);
12392 		return;
12393 	}
12394 
12395 	ahvif = ath12k_vif_to_ahvif(vif);
12396 	links = ahvif->links_map;
12397 	for_each_set_bit(link_id, &links, IEEE80211_MLD_MAX_NUM_LINKS) {
12398 		arvif = wiphy_dereference(hw->wiphy, ahvif->link[link_id]);
12399 		if (!(arvif && arvif->ar))
12400 			continue;
12401 
12402 		ath12k_mac_flush(arvif->ar);
12403 	}
12404 }
12405 EXPORT_SYMBOL(ath12k_mac_op_flush);
12406 
12407 static int
12408 ath12k_mac_bitrate_mask_num_ht_rates(struct ath12k *ar,
12409 				     enum nl80211_band band,
12410 				     const struct cfg80211_bitrate_mask *mask)
12411 {
12412 	int num_rates = 0;
12413 	int i;
12414 
12415 	for (i = 0; i < ARRAY_SIZE(mask->control[band].ht_mcs); i++)
12416 		num_rates += hweight16(mask->control[band].ht_mcs[i]);
12417 
12418 	return num_rates;
12419 }
12420 
12421 static bool
12422 ath12k_mac_has_single_legacy_rate(struct ath12k *ar,
12423 				  enum nl80211_band band,
12424 				  const struct cfg80211_bitrate_mask *mask)
12425 {
12426 	int num_rates = 0;
12427 
12428 	num_rates = hweight32(mask->control[band].legacy);
12429 
12430 	if (ath12k_mac_bitrate_mask_num_ht_rates(ar, band, mask))
12431 		return false;
12432 
12433 	if (ath12k_mac_bitrate_mask_num_vht_rates(ar, band, mask))
12434 		return false;
12435 
12436 	if (ath12k_mac_bitrate_mask_num_he_rates(ar, band, mask))
12437 		return false;
12438 
12439 	if (ath12k_mac_bitrate_mask_num_eht_rates(ar, band, mask))
12440 		return false;
12441 
12442 	return num_rates == 1;
12443 }
12444 
12445 static __le16
12446 ath12k_mac_get_tx_mcs_map(const struct ieee80211_sta_he_cap *he_cap)
12447 {
12448 	if (he_cap->he_cap_elem.phy_cap_info[0] &
12449 	    IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_160MHZ_IN_5G)
12450 		return he_cap->he_mcs_nss_supp.tx_mcs_160;
12451 
12452 	return he_cap->he_mcs_nss_supp.tx_mcs_80;
12453 }
12454 
12455 static bool
12456 ath12k_mac_bitrate_mask_get_single_nss(struct ath12k *ar,
12457 				       struct ieee80211_vif *vif,
12458 				       enum nl80211_band band,
12459 				       const struct cfg80211_bitrate_mask *mask,
12460 				       int *nss)
12461 {
12462 	struct ieee80211_supported_band *sband = &ar->mac.sbands[band];
12463 	u16 vht_mcs_map = le16_to_cpu(sband->vht_cap.vht_mcs.tx_mcs_map);
12464 	const struct ieee80211_sband_iftype_data *data;
12465 	const struct ieee80211_sta_he_cap *he_cap;
12466 	u16 he_mcs_map = 0;
12467 	u16 eht_mcs_map = 0;
12468 	u8 ht_nss_mask = 0;
12469 	u8 vht_nss_mask = 0;
12470 	u8 he_nss_mask = 0;
12471 	u8 eht_nss_mask = 0;
12472 	u8 mcs_nss_len;
12473 	int i;
12474 
12475 	/* No need to consider legacy here. Basic rates are always present
12476 	 * in bitrate mask
12477 	 */
12478 
12479 	for (i = 0; i < ARRAY_SIZE(mask->control[band].ht_mcs); i++) {
12480 		if (mask->control[band].ht_mcs[i] == 0)
12481 			continue;
12482 		else if (mask->control[band].ht_mcs[i] ==
12483 			 sband->ht_cap.mcs.rx_mask[i])
12484 			ht_nss_mask |= BIT(i);
12485 		else
12486 			return false;
12487 	}
12488 
12489 	for (i = 0; i < ARRAY_SIZE(mask->control[band].vht_mcs); i++) {
12490 		if (mask->control[band].vht_mcs[i] == 0)
12491 			continue;
12492 		else if (mask->control[band].vht_mcs[i] ==
12493 			 ath12k_mac_get_max_vht_mcs_map(vht_mcs_map, i))
12494 			vht_nss_mask |= BIT(i);
12495 		else
12496 			return false;
12497 	}
12498 
12499 	he_cap = ieee80211_get_he_iftype_cap_vif(sband, vif);
12500 	if (!he_cap)
12501 		return false;
12502 
12503 	he_mcs_map = le16_to_cpu(ath12k_mac_get_tx_mcs_map(he_cap));
12504 
12505 	for (i = 0; i < ARRAY_SIZE(mask->control[band].he_mcs); i++) {
12506 		if (mask->control[band].he_mcs[i] == 0)
12507 			continue;
12508 
12509 		if (mask->control[band].he_mcs[i] ==
12510 		    ath12k_mac_get_max_he_mcs_map(he_mcs_map, i))
12511 			he_nss_mask |= BIT(i);
12512 		else
12513 			return false;
12514 	}
12515 
12516 	data = ieee80211_get_sband_iftype_data(sband, vif->type);
12517 
12518 	mcs_nss_len = ieee80211_eht_mcs_nss_size(&data->he_cap.he_cap_elem,
12519 						 &data->eht_cap.eht_cap_elem,
12520 						 false);
12521 	if (mcs_nss_len == 4) {
12522 		/* 20 MHz only STA case */
12523 		const struct ieee80211_eht_mcs_nss_supp_20mhz_only *eht_mcs_nss =
12524 			&data->eht_cap.eht_mcs_nss_supp.only_20mhz;
12525 		if (eht_mcs_nss->rx_tx_mcs13_max_nss)
12526 			eht_mcs_map = 0x1fff;
12527 		else if (eht_mcs_nss->rx_tx_mcs11_max_nss)
12528 			eht_mcs_map = 0x07ff;
12529 		else if (eht_mcs_nss->rx_tx_mcs9_max_nss)
12530 			eht_mcs_map = 0x01ff;
12531 		else
12532 			eht_mcs_map = 0x007f;
12533 	} else {
12534 		const struct ieee80211_eht_mcs_nss_supp_bw *eht_mcs_nss;
12535 
12536 		switch (mcs_nss_len) {
12537 		case 9:
12538 			eht_mcs_nss = &data->eht_cap.eht_mcs_nss_supp.bw._320;
12539 			break;
12540 		case 6:
12541 			eht_mcs_nss = &data->eht_cap.eht_mcs_nss_supp.bw._160;
12542 			break;
12543 		case 3:
12544 			eht_mcs_nss = &data->eht_cap.eht_mcs_nss_supp.bw._80;
12545 			break;
12546 		default:
12547 			return false;
12548 		}
12549 
12550 		if (eht_mcs_nss->rx_tx_mcs13_max_nss)
12551 			eht_mcs_map = 0x1fff;
12552 		else if (eht_mcs_nss->rx_tx_mcs11_max_nss)
12553 			eht_mcs_map = 0x7ff;
12554 		else
12555 			eht_mcs_map = 0x1ff;
12556 	}
12557 
12558 	for (i = 0; i < ARRAY_SIZE(mask->control[band].eht_mcs); i++) {
12559 		if (mask->control[band].eht_mcs[i] == 0)
12560 			continue;
12561 
12562 		if (mask->control[band].eht_mcs[i] < eht_mcs_map)
12563 			eht_nss_mask |= BIT(i);
12564 		else
12565 			return false;
12566 	}
12567 
12568 	if (ht_nss_mask != vht_nss_mask || ht_nss_mask != he_nss_mask ||
12569 	    ht_nss_mask != eht_nss_mask)
12570 		return false;
12571 
12572 	if (ht_nss_mask == 0)
12573 		return false;
12574 
12575 	if (BIT(fls(ht_nss_mask)) - 1 != ht_nss_mask)
12576 		return false;
12577 
12578 	*nss = fls(ht_nss_mask);
12579 
12580 	return true;
12581 }
12582 
12583 static int
12584 ath12k_mac_get_single_legacy_rate(struct ath12k *ar,
12585 				  enum nl80211_band band,
12586 				  const struct cfg80211_bitrate_mask *mask,
12587 				  u32 *rate, u8 *nss)
12588 {
12589 	int rate_idx;
12590 	u16 bitrate;
12591 	u8 preamble;
12592 	u8 hw_rate;
12593 
12594 	if (hweight32(mask->control[band].legacy) != 1)
12595 		return -EINVAL;
12596 
12597 	rate_idx = ffs(mask->control[band].legacy) - 1;
12598 
12599 	if (band == NL80211_BAND_5GHZ || band == NL80211_BAND_6GHZ)
12600 		rate_idx += ATH12K_MAC_FIRST_OFDM_RATE_IDX;
12601 
12602 	hw_rate = ath12k_legacy_rates[rate_idx].hw_value;
12603 	bitrate = ath12k_legacy_rates[rate_idx].bitrate;
12604 
12605 	if (ath12k_mac_bitrate_is_cck(bitrate))
12606 		preamble = WMI_RATE_PREAMBLE_CCK;
12607 	else
12608 		preamble = WMI_RATE_PREAMBLE_OFDM;
12609 
12610 	*nss = 1;
12611 	*rate = ATH12K_HW_RATE_CODE(hw_rate, 0, preamble);
12612 
12613 	return 0;
12614 }
12615 
12616 static int
12617 ath12k_mac_set_fixed_rate_gi_ltf(struct ath12k_link_vif *arvif, u8 gi, u8 ltf,
12618 				 u32 param)
12619 {
12620 	struct ath12k *ar = arvif->ar;
12621 	int ret;
12622 
12623 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
12624 
12625 	/* 0.8 = 0, 1.6 = 2 and 3.2 = 3. */
12626 	if (gi && gi != 0xFF)
12627 		gi += 1;
12628 
12629 	ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
12630 					    WMI_VDEV_PARAM_SGI, gi);
12631 	if (ret) {
12632 		ath12k_warn(ar->ab, "failed to set GI:%d, error:%d\n",
12633 			    gi, ret);
12634 		return ret;
12635 	}
12636 
12637 	if (param == WMI_VDEV_PARAM_HE_LTF) {
12638 		/* HE values start from 1 */
12639 		if (ltf != 0xFF)
12640 			ltf += 1;
12641 	} else {
12642 		/* EHT values start from 5 */
12643 		if (ltf != 0xFF)
12644 			ltf += 4;
12645 	}
12646 
12647 	ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
12648 					    param, ltf);
12649 	if (ret) {
12650 		ath12k_warn(ar->ab, "failed to set LTF:%d, error:%d\n",
12651 			    ltf, ret);
12652 		return ret;
12653 	}
12654 	return 0;
12655 }
12656 
12657 static int
12658 ath12k_mac_set_auto_rate_gi_ltf(struct ath12k_link_vif *arvif, u16 gi, u8 ltf)
12659 {
12660 	struct ath12k *ar = arvif->ar;
12661 	int ret;
12662 	u32 ar_gi_ltf;
12663 
12664 	if (gi != 0xFF) {
12665 		switch (gi) {
12666 		case ATH12K_RATE_INFO_GI_0_8:
12667 			gi = WMI_AUTORATE_800NS_GI;
12668 			break;
12669 		case ATH12K_RATE_INFO_GI_1_6:
12670 			gi = WMI_AUTORATE_1600NS_GI;
12671 			break;
12672 		case ATH12K_RATE_INFO_GI_3_2:
12673 			gi = WMI_AUTORATE_3200NS_GI;
12674 			break;
12675 		default:
12676 			ath12k_warn(ar->ab, "Invalid GI\n");
12677 			return -EINVAL;
12678 		}
12679 	}
12680 
12681 	if (ltf != 0xFF) {
12682 		switch (ltf) {
12683 		case ATH12K_RATE_INFO_1XLTF:
12684 			ltf = WMI_AUTORATE_LTF_1X;
12685 			break;
12686 		case ATH12K_RATE_INFO_2XLTF:
12687 			ltf = WMI_AUTORATE_LTF_2X;
12688 			break;
12689 		case ATH12K_RATE_INFO_4XLTF:
12690 			ltf = WMI_AUTORATE_LTF_4X;
12691 			break;
12692 		default:
12693 			ath12k_warn(ar->ab, "Invalid LTF\n");
12694 			return -EINVAL;
12695 		}
12696 	}
12697 
12698 	ar_gi_ltf = gi | ltf;
12699 
12700 	ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
12701 					    WMI_VDEV_PARAM_AUTORATE_MISC_CFG,
12702 					    ar_gi_ltf);
12703 	if (ret) {
12704 		ath12k_warn(ar->ab,
12705 			    "failed to set autorate GI:%u, LTF:%u params, error:%d\n",
12706 			    gi, ltf, ret);
12707 		return ret;
12708 	}
12709 
12710 	return 0;
12711 }
12712 
12713 static u32 ath12k_mac_nlgi_to_wmigi(enum nl80211_txrate_gi gi)
12714 {
12715 	switch (gi) {
12716 	case NL80211_TXRATE_DEFAULT_GI:
12717 		return WMI_GI_400_NS;
12718 	case NL80211_TXRATE_FORCE_LGI:
12719 		return WMI_GI_800_NS;
12720 	default:
12721 		return WMI_GI_400_NS;
12722 	}
12723 }
12724 
12725 static int ath12k_mac_set_rate_params(struct ath12k_link_vif *arvif,
12726 				      u32 rate, u8 nss, u8 sgi, u8 ldpc,
12727 				      u8 he_gi, u8 he_ltf, bool he_fixed_rate,
12728 				      u8 eht_gi, u8 eht_ltf,
12729 				      bool eht_fixed_rate)
12730 {
12731 	struct ieee80211_bss_conf *link_conf;
12732 	struct ath12k *ar = arvif->ar;
12733 	bool he_support, eht_support, gi_ltf_set = false;
12734 	u32 vdev_param;
12735 	u32 param_value;
12736 	int ret;
12737 
12738 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
12739 
12740 	link_conf = ath12k_mac_get_link_bss_conf(arvif);
12741 	if (!link_conf)
12742 		return -EINVAL;
12743 
12744 	he_support = link_conf->he_support;
12745 	eht_support = link_conf->eht_support;
12746 
12747 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
12748 		   "mac set rate params vdev %i rate 0x%02x nss 0x%02x sgi 0x%02x ldpc 0x%02x\n",
12749 		   arvif->vdev_id, rate, nss, sgi, ldpc);
12750 
12751 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
12752 		   "he_gi 0x%02x he_ltf 0x%02x he_fixed_rate %d\n", he_gi,
12753 		   he_ltf, he_fixed_rate);
12754 
12755 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
12756 		   "eht_gi 0x%02x eht_ltf 0x%02x eht_fixed_rate %d\n",
12757 		   eht_gi, eht_ltf, eht_fixed_rate);
12758 
12759 	if (!he_support && !eht_support) {
12760 		vdev_param = WMI_VDEV_PARAM_FIXED_RATE;
12761 		ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
12762 						    vdev_param, rate);
12763 		if (ret) {
12764 			ath12k_warn(ar->ab, "failed to set fixed rate param 0x%02x: %d\n",
12765 				    rate, ret);
12766 			return ret;
12767 		}
12768 	}
12769 
12770 	vdev_param = WMI_VDEV_PARAM_NSS;
12771 
12772 	ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
12773 					    vdev_param, nss);
12774 	if (ret) {
12775 		ath12k_warn(ar->ab, "failed to set nss param %d: %d\n",
12776 			    nss, ret);
12777 		return ret;
12778 	}
12779 
12780 	ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
12781 					    WMI_VDEV_PARAM_LDPC, ldpc);
12782 	if (ret) {
12783 		ath12k_warn(ar->ab, "failed to set ldpc param %d: %d\n",
12784 			    ldpc, ret);
12785 		return ret;
12786 	}
12787 
12788 	if (eht_support) {
12789 		if (eht_fixed_rate)
12790 			ret = ath12k_mac_set_fixed_rate_gi_ltf(arvif, eht_gi, eht_ltf,
12791 							       WMI_VDEV_PARAM_EHT_LTF);
12792 		else
12793 			ret = ath12k_mac_set_auto_rate_gi_ltf(arvif, eht_gi, eht_ltf);
12794 
12795 		if (ret) {
12796 			ath12k_warn(ar->ab,
12797 				    "failed to set EHT LTF/GI params %d/%d: %d\n",
12798 				    eht_gi, eht_ltf, ret);
12799 			return ret;
12800 		}
12801 		gi_ltf_set = true;
12802 	}
12803 
12804 	if (he_support) {
12805 		if (he_fixed_rate)
12806 			ret = ath12k_mac_set_fixed_rate_gi_ltf(arvif, he_gi, he_ltf,
12807 							       WMI_VDEV_PARAM_HE_LTF);
12808 		else
12809 			ret = ath12k_mac_set_auto_rate_gi_ltf(arvif, he_gi, he_ltf);
12810 		if (ret)
12811 			return ret;
12812 		gi_ltf_set = true;
12813 	}
12814 
12815 	if (!gi_ltf_set) {
12816 		vdev_param = WMI_VDEV_PARAM_SGI;
12817 		param_value = ath12k_mac_nlgi_to_wmigi(sgi);
12818 		ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
12819 						    vdev_param, param_value);
12820 		if (ret) {
12821 			ath12k_warn(ar->ab, "failed to set sgi param %d: %d\n",
12822 				    sgi, ret);
12823 			return ret;
12824 		}
12825 	}
12826 
12827 	return 0;
12828 }
12829 
12830 static bool
12831 ath12k_mac_vht_mcs_range_present(struct ath12k *ar,
12832 				 enum nl80211_band band,
12833 				 const struct cfg80211_bitrate_mask *mask)
12834 {
12835 	int i;
12836 	u16 vht_mcs;
12837 
12838 	for (i = 0; i < NL80211_VHT_NSS_MAX; i++) {
12839 		vht_mcs = mask->control[band].vht_mcs[i];
12840 
12841 		switch (vht_mcs) {
12842 		case 0:
12843 		case BIT(8) - 1:
12844 		case BIT(9) - 1:
12845 		case BIT(10) - 1:
12846 			break;
12847 		default:
12848 			return false;
12849 		}
12850 	}
12851 
12852 	return true;
12853 }
12854 
12855 static bool
12856 ath12k_mac_he_mcs_range_present(struct ath12k *ar,
12857 				enum nl80211_band band,
12858 				const struct cfg80211_bitrate_mask *mask)
12859 {
12860 	int i;
12861 	u16 he_mcs;
12862 
12863 	for (i = 0; i < NL80211_HE_NSS_MAX; i++) {
12864 		he_mcs = mask->control[band].he_mcs[i];
12865 
12866 		switch (he_mcs) {
12867 		case 0:
12868 		case BIT(8) - 1:
12869 		case BIT(10) - 1:
12870 		case BIT(12) - 1:
12871 			break;
12872 		default:
12873 			return false;
12874 		}
12875 	}
12876 
12877 	return true;
12878 }
12879 
12880 static bool
12881 ath12k_mac_eht_mcs_range_present(struct ath12k *ar,
12882 				 enum nl80211_band band,
12883 				 const struct cfg80211_bitrate_mask *mask)
12884 {
12885 	u16 eht_mcs;
12886 	int i;
12887 
12888 	for (i = 0; i < NL80211_EHT_NSS_MAX; i++) {
12889 		eht_mcs = mask->control[band].eht_mcs[i];
12890 
12891 		switch (eht_mcs) {
12892 		case 0:
12893 		case BIT(8) - 1:
12894 		case BIT(10) - 1:
12895 		case BIT(12) - 1:
12896 		case BIT(14) - 1:
12897 			break;
12898 		case BIT(15) - 1:
12899 		case BIT(16) - 1:
12900 		case BIT(16) - BIT(14) - 1:
12901 			if (i != 0)
12902 				return false;
12903 			break;
12904 		default:
12905 			return false;
12906 		}
12907 	}
12908 
12909 	return true;
12910 }
12911 
12912 static void ath12k_mac_set_bitrate_mask_iter(void *data,
12913 					     struct ieee80211_sta *sta)
12914 {
12915 	struct ath12k_link_vif *arvif = data;
12916 	struct ath12k_sta *ahsta = ath12k_sta_to_ahsta(sta);
12917 	struct ath12k_link_sta *arsta;
12918 	struct ath12k *ar = arvif->ar;
12919 
12920 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
12921 
12922 	arsta = wiphy_dereference(ath12k_ar_to_hw(ar)->wiphy,
12923 				  ahsta->link[arvif->link_id]);
12924 	if (!arsta || arsta->arvif != arvif)
12925 		return;
12926 
12927 	spin_lock_bh(&ar->data_lock);
12928 	arsta->changed |= IEEE80211_RC_SUPP_RATES_CHANGED;
12929 	spin_unlock_bh(&ar->data_lock);
12930 
12931 	wiphy_work_queue(ath12k_ar_to_hw(ar)->wiphy, &arsta->update_wk);
12932 }
12933 
12934 static void ath12k_mac_disable_peer_fixed_rate(void *data,
12935 					       struct ieee80211_sta *sta)
12936 {
12937 	struct ath12k_link_vif *arvif = data;
12938 	struct ath12k_sta *ahsta = ath12k_sta_to_ahsta(sta);
12939 	struct ath12k_link_sta *arsta;
12940 	struct ath12k *ar = arvif->ar;
12941 	int ret;
12942 
12943 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
12944 
12945 	arsta = wiphy_dereference(ath12k_ar_to_hw(ar)->wiphy,
12946 				  ahsta->link[arvif->link_id]);
12947 
12948 	if (!arsta || arsta->arvif != arvif)
12949 		return;
12950 
12951 	ret = ath12k_wmi_set_peer_param(ar, arsta->addr,
12952 					arvif->vdev_id,
12953 					WMI_PEER_PARAM_FIXED_RATE,
12954 					WMI_FIXED_RATE_NONE);
12955 	if (ret)
12956 		ath12k_warn(ar->ab,
12957 			    "failed to disable peer fixed rate for STA %pM ret %d\n",
12958 			    arsta->addr, ret);
12959 }
12960 
12961 static bool
12962 ath12k_mac_validate_fixed_rate_settings(struct ath12k *ar, enum nl80211_band band,
12963 					const struct cfg80211_bitrate_mask *mask,
12964 					unsigned int link_id)
12965 {
12966 	bool eht_fixed_rate = false, he_fixed_rate = false, vht_fixed_rate = false;
12967 	const u16 *vht_mcs_mask, *he_mcs_mask, *eht_mcs_mask;
12968 	struct ieee80211_link_sta *link_sta;
12969 	struct ath12k_dp_link_peer *peer, *tmp;
12970 	u8 vht_nss, he_nss, eht_nss;
12971 	int ret = true;
12972 	struct ath12k_base *ab = ar->ab;
12973 	struct ath12k_dp *dp = ath12k_ab_to_dp(ab);
12974 
12975 	vht_mcs_mask = mask->control[band].vht_mcs;
12976 	he_mcs_mask = mask->control[band].he_mcs;
12977 	eht_mcs_mask = mask->control[band].eht_mcs;
12978 
12979 	if (ath12k_mac_bitrate_mask_num_vht_rates(ar, band, mask) == 1)
12980 		vht_fixed_rate = true;
12981 
12982 	if (ath12k_mac_bitrate_mask_num_he_rates(ar, band, mask) == 1)
12983 		he_fixed_rate = true;
12984 
12985 	if (ath12k_mac_bitrate_mask_num_eht_rates(ar, band, mask) == 1)
12986 		eht_fixed_rate = true;
12987 
12988 	if (!vht_fixed_rate && !he_fixed_rate && !eht_fixed_rate)
12989 		return true;
12990 
12991 	vht_nss = ath12k_mac_max_vht_nss(vht_mcs_mask);
12992 	he_nss =  ath12k_mac_max_he_nss(he_mcs_mask);
12993 	eht_nss = ath12k_mac_max_eht_nss(eht_mcs_mask);
12994 
12995 	rcu_read_lock();
12996 	spin_lock_bh(&dp->dp_lock);
12997 	list_for_each_entry_safe(peer, tmp, &dp->peers, list) {
12998 		if (peer->sta) {
12999 			link_sta = rcu_dereference(peer->sta->link[link_id]);
13000 			if (!link_sta) {
13001 				ret = false;
13002 				goto exit;
13003 			}
13004 
13005 			if (vht_fixed_rate && (!link_sta->vht_cap.vht_supported ||
13006 					       link_sta->rx_nss < vht_nss)) {
13007 				ret = false;
13008 				goto exit;
13009 			}
13010 			if (he_fixed_rate && (!link_sta->he_cap.has_he ||
13011 					      link_sta->rx_nss < he_nss)) {
13012 				ret = false;
13013 				goto exit;
13014 			}
13015 			if (eht_fixed_rate && (!link_sta->eht_cap.has_eht ||
13016 					       link_sta->rx_nss < eht_nss)) {
13017 				ret = false;
13018 				goto exit;
13019 			}
13020 		}
13021 	}
13022 exit:
13023 	spin_unlock_bh(&dp->dp_lock);
13024 	rcu_read_unlock();
13025 	return ret;
13026 }
13027 
13028 int
13029 ath12k_mac_op_set_bitrate_mask(struct ieee80211_hw *hw,
13030 			       struct ieee80211_vif *vif,
13031 			       const struct cfg80211_bitrate_mask *mask)
13032 {
13033 	struct ath12k_vif *ahvif = ath12k_vif_to_ahvif(vif);
13034 	struct ath12k_link_vif *arvif;
13035 	struct cfg80211_chan_def def;
13036 	struct ath12k *ar;
13037 	enum nl80211_band band;
13038 	const u8 *ht_mcs_mask;
13039 	const u16 *vht_mcs_mask;
13040 	const u16 *he_mcs_mask;
13041 	const u16 *eht_mcs_mask;
13042 	u8 he_ltf = 0;
13043 	u8 he_gi = 0;
13044 	u8 eht_ltf = 0, eht_gi = 0;
13045 	u32 rate;
13046 	u8 nss, mac_nss;
13047 	u8 sgi;
13048 	u8 ldpc;
13049 	int single_nss;
13050 	int ret;
13051 	int num_rates;
13052 	bool he_fixed_rate = false;
13053 	bool eht_fixed_rate = false;
13054 
13055 	lockdep_assert_wiphy(hw->wiphy);
13056 
13057 	arvif = &ahvif->deflink;
13058 
13059 	ar = arvif->ar;
13060 	if (ath12k_mac_vif_link_chan(vif, arvif->link_id, &def)) {
13061 		ret = -EPERM;
13062 		goto out;
13063 	}
13064 
13065 	band = def.chan->band;
13066 	ht_mcs_mask = mask->control[band].ht_mcs;
13067 	vht_mcs_mask = mask->control[band].vht_mcs;
13068 	he_mcs_mask = mask->control[band].he_mcs;
13069 	eht_mcs_mask = mask->control[band].eht_mcs;
13070 	ldpc = !!(ar->ht_cap_info & WMI_HT_CAP_LDPC);
13071 
13072 	sgi = mask->control[band].gi;
13073 	if (sgi == NL80211_TXRATE_FORCE_SGI) {
13074 		ret = -EINVAL;
13075 		goto out;
13076 	}
13077 
13078 	he_gi = mask->control[band].he_gi;
13079 	he_ltf = mask->control[band].he_ltf;
13080 
13081 	eht_gi = mask->control[band].eht_gi;
13082 	eht_ltf = mask->control[band].eht_ltf;
13083 
13084 	/* mac80211 doesn't support sending a fixed HT/VHT MCS alone, rather it
13085 	 * requires passing at least one of used basic rates along with them.
13086 	 * Fixed rate setting across different preambles(legacy, HT, VHT) is
13087 	 * not supported by the FW. Hence use of FIXED_RATE vdev param is not
13088 	 * suitable for setting single HT/VHT rates.
13089 	 * But, there could be a single basic rate passed from userspace which
13090 	 * can be done through the FIXED_RATE param.
13091 	 */
13092 	if (ath12k_mac_has_single_legacy_rate(ar, band, mask)) {
13093 		ret = ath12k_mac_get_single_legacy_rate(ar, band, mask, &rate,
13094 							&nss);
13095 		if (ret) {
13096 			ath12k_warn(ar->ab, "failed to get single legacy rate for vdev %i: %d\n",
13097 				    arvif->vdev_id, ret);
13098 			goto out;
13099 		}
13100 
13101 		ieee80211_iterate_stations_mtx(hw,
13102 					       ath12k_mac_disable_peer_fixed_rate,
13103 					       arvif);
13104 	} else if (ath12k_mac_bitrate_mask_get_single_nss(ar, vif, band, mask,
13105 							  &single_nss)) {
13106 		rate = WMI_FIXED_RATE_NONE;
13107 		nss = single_nss;
13108 		arvif->bitrate_mask = *mask;
13109 
13110 		ieee80211_iterate_stations_atomic(hw,
13111 						  ath12k_mac_set_bitrate_mask_iter,
13112 						  arvif);
13113 	} else {
13114 		rate = WMI_FIXED_RATE_NONE;
13115 
13116 		if (!ath12k_mac_validate_fixed_rate_settings(ar, band,
13117 							     mask, arvif->link_id))
13118 			ath12k_warn(ar->ab,
13119 				    "failed to update fixed rate settings due to mcs/nss incompatibility\n");
13120 
13121 		mac_nss = max(max3(ath12k_mac_max_ht_nss(ht_mcs_mask),
13122 				   ath12k_mac_max_vht_nss(vht_mcs_mask),
13123 				   ath12k_mac_max_he_nss(he_mcs_mask)),
13124 			       ath12k_mac_max_eht_nss(eht_mcs_mask));
13125 		nss = min_t(u32, ar->num_tx_chains, mac_nss);
13126 
13127 		/* If multiple rates across different preambles are given
13128 		 * we can reconfigure this info with all peers using PEER_ASSOC
13129 		 * command with the below exception cases.
13130 		 * - Single VHT Rate : peer_assoc command accommodates only MCS
13131 		 * range values i.e 0-7, 0-8, 0-9 for VHT. Though mac80211
13132 		 * mandates passing basic rates along with HT/VHT rates, FW
13133 		 * doesn't allow switching from VHT to Legacy. Hence instead of
13134 		 * setting legacy and VHT rates using RATEMASK_CMD vdev cmd,
13135 		 * we could set this VHT rate as peer fixed rate param, which
13136 		 * will override FIXED rate and FW rate control algorithm.
13137 		 * If single VHT rate is passed along with HT rates, we select
13138 		 * the VHT rate as fixed rate for vht peers.
13139 		 * - Multiple VHT Rates : When Multiple VHT rates are given,this
13140 		 * can be set using RATEMASK CMD which uses FW rate-ctl alg.
13141 		 * TODO: Setting multiple VHT MCS and replacing peer_assoc with
13142 		 * RATEMASK_CMDID can cover all use cases of setting rates
13143 		 * across multiple preambles and rates within same type.
13144 		 * But requires more validation of the command at this point.
13145 		 */
13146 
13147 		num_rates = ath12k_mac_bitrate_mask_num_vht_rates(ar, band,
13148 								  mask);
13149 
13150 		if (!ath12k_mac_vht_mcs_range_present(ar, band, mask) &&
13151 		    num_rates > 1) {
13152 			/* TODO: Handle multiple VHT MCS values setting using
13153 			 * RATEMASK CMD
13154 			 */
13155 			ath12k_warn(ar->ab,
13156 				    "Setting more than one MCS Value in bitrate mask not supported\n");
13157 			ret = -EINVAL;
13158 			goto out;
13159 		}
13160 
13161 		num_rates = ath12k_mac_bitrate_mask_num_he_rates(ar, band, mask);
13162 		if (num_rates == 1)
13163 			he_fixed_rate = true;
13164 
13165 		if (!ath12k_mac_he_mcs_range_present(ar, band, mask) &&
13166 		    num_rates > 1) {
13167 			ath12k_warn(ar->ab,
13168 				    "Setting more than one HE MCS Value in bitrate mask not supported\n");
13169 			ret = -EINVAL;
13170 			goto out;
13171 		}
13172 
13173 		num_rates = ath12k_mac_bitrate_mask_num_eht_rates(ar, band,
13174 								  mask);
13175 		if (num_rates == 1)
13176 			eht_fixed_rate = true;
13177 
13178 		if (!ath12k_mac_eht_mcs_range_present(ar, band, mask) &&
13179 		    num_rates > 1) {
13180 			ath12k_warn(ar->ab,
13181 				    "Setting more than one EHT MCS Value in bitrate mask not supported\n");
13182 			ret = -EINVAL;
13183 			goto out;
13184 		}
13185 
13186 		ieee80211_iterate_stations_mtx(hw,
13187 					       ath12k_mac_disable_peer_fixed_rate,
13188 					       arvif);
13189 
13190 		arvif->bitrate_mask = *mask;
13191 		ieee80211_iterate_stations_mtx(hw,
13192 					       ath12k_mac_set_bitrate_mask_iter,
13193 					       arvif);
13194 	}
13195 
13196 	ret = ath12k_mac_set_rate_params(arvif, rate, nss, sgi, ldpc, he_gi,
13197 					 he_ltf, he_fixed_rate, eht_gi, eht_ltf,
13198 					 eht_fixed_rate);
13199 	if (ret) {
13200 		ath12k_warn(ar->ab, "failed to set rate params on vdev %i: %d\n",
13201 			    arvif->vdev_id, ret);
13202 	}
13203 
13204 out:
13205 	return ret;
13206 }
13207 EXPORT_SYMBOL(ath12k_mac_op_set_bitrate_mask);
13208 
13209 void
13210 ath12k_mac_op_reconfig_complete(struct ieee80211_hw *hw,
13211 				enum ieee80211_reconfig_type reconfig_type)
13212 {
13213 	struct ath12k_hw *ah = ath12k_hw_to_ah(hw);
13214 	struct ath12k *ar;
13215 	struct ath12k_base *ab;
13216 	struct ath12k_vif *ahvif;
13217 	struct ath12k_link_vif *arvif;
13218 	int recovery_count, i;
13219 
13220 	lockdep_assert_wiphy(hw->wiphy);
13221 
13222 	if (reconfig_type != IEEE80211_RECONFIG_TYPE_RESTART)
13223 		return;
13224 
13225 	guard(mutex)(&ah->hw_mutex);
13226 
13227 	if (ah->state != ATH12K_HW_STATE_RESTARTED)
13228 		return;
13229 
13230 	ah->state = ATH12K_HW_STATE_ON;
13231 	ieee80211_wake_queues(hw);
13232 
13233 	for_each_ar(ah, ar, i) {
13234 		ab = ar->ab;
13235 
13236 		ath12k_warn(ar->ab, "pdev %d successfully recovered\n",
13237 			    ar->pdev->pdev_id);
13238 
13239 		if (ar->ab->hw_params->current_cc_support &&
13240 		    ar->alpha2[0] != 0 && ar->alpha2[1] != 0) {
13241 			struct wmi_set_current_country_arg arg = {};
13242 
13243 			memcpy(&arg.alpha2, ar->alpha2, 2);
13244 			reinit_completion(&ar->regd_update_completed);
13245 			ath12k_wmi_send_set_current_country_cmd(ar, &arg);
13246 		}
13247 
13248 		if (ab->is_reset) {
13249 			recovery_count = atomic_inc_return(&ab->recovery_count);
13250 
13251 			ath12k_dbg(ab, ATH12K_DBG_BOOT, "recovery count %d\n",
13252 				   recovery_count);
13253 
13254 			/* When there are multiple radios in an SOC,
13255 			 * the recovery has to be done for each radio
13256 			 */
13257 			if (recovery_count == ab->num_radios) {
13258 				atomic_dec(&ab->reset_count);
13259 				complete(&ab->reset_complete);
13260 				ab->is_reset = false;
13261 				atomic_set(&ab->fail_cont_count, 0);
13262 				ath12k_dbg(ab, ATH12K_DBG_BOOT, "reset success\n");
13263 			}
13264 		}
13265 
13266 		list_for_each_entry(arvif, &ar->arvifs, list) {
13267 			ahvif = arvif->ahvif;
13268 			ath12k_dbg(ab, ATH12K_DBG_BOOT,
13269 				   "reconfig cipher %d up %d vdev type %d\n",
13270 				   ahvif->dp_vif.key_cipher,
13271 				   arvif->is_up,
13272 				   ahvif->vdev_type);
13273 
13274 			/* After trigger disconnect, then upper layer will
13275 			 * trigger connect again, then the PN number of
13276 			 * upper layer will be reset to keep up with AP
13277 			 * side, hence PN number mismatch will not happen.
13278 			 */
13279 			if (arvif->is_up &&
13280 			    ahvif->vdev_type == WMI_VDEV_TYPE_STA &&
13281 			    ahvif->vdev_subtype == WMI_VDEV_SUBTYPE_NONE) {
13282 				ieee80211_hw_restart_disconnect(ahvif->vif);
13283 
13284 				ath12k_dbg(ab, ATH12K_DBG_BOOT,
13285 					   "restart disconnect\n");
13286 			}
13287 		}
13288 	}
13289 }
13290 EXPORT_SYMBOL(ath12k_mac_op_reconfig_complete);
13291 
13292 static void
13293 ath12k_mac_update_bss_chan_survey(struct ath12k *ar,
13294 				  struct ieee80211_channel *channel)
13295 {
13296 	int ret;
13297 	enum wmi_bss_chan_info_req_type type = WMI_BSS_SURVEY_REQ_TYPE_READ;
13298 
13299 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
13300 
13301 	if (!test_bit(WMI_TLV_SERVICE_BSS_CHANNEL_INFO_64, ar->ab->wmi_ab.svc_map) ||
13302 	    ar->rx_channel != channel)
13303 		return;
13304 
13305 	if (ar->scan.state != ATH12K_SCAN_IDLE) {
13306 		ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
13307 			   "ignoring bss chan info req while scanning..\n");
13308 		return;
13309 	}
13310 
13311 	reinit_completion(&ar->bss_survey_done);
13312 
13313 	ret = ath12k_wmi_pdev_bss_chan_info_request(ar, type);
13314 	if (ret) {
13315 		ath12k_warn(ar->ab, "failed to send pdev bss chan info request\n");
13316 		return;
13317 	}
13318 
13319 	ret = wait_for_completion_timeout(&ar->bss_survey_done, 3 * HZ);
13320 	if (ret == 0)
13321 		ath12k_warn(ar->ab, "bss channel survey timed out\n");
13322 }
13323 
13324 int ath12k_mac_op_get_survey(struct ieee80211_hw *hw, int idx,
13325 			     struct survey_info *survey)
13326 {
13327 	struct ath12k *ar;
13328 	struct ieee80211_supported_band *sband;
13329 	struct survey_info *ar_survey;
13330 
13331 	lockdep_assert_wiphy(hw->wiphy);
13332 
13333 	if (idx >= ATH12K_NUM_CHANS)
13334 		return -ENOENT;
13335 
13336 	sband = hw->wiphy->bands[NL80211_BAND_2GHZ];
13337 	if (sband && idx >= sband->n_channels) {
13338 		idx -= sband->n_channels;
13339 		sband = NULL;
13340 	}
13341 
13342 	if (!sband)
13343 		sband = hw->wiphy->bands[NL80211_BAND_5GHZ];
13344 	if (sband && idx >= sband->n_channels) {
13345 		idx -= sband->n_channels;
13346 		sband = NULL;
13347 	}
13348 
13349 	if (!sband)
13350 		sband = hw->wiphy->bands[NL80211_BAND_6GHZ];
13351 
13352 	if (!sband || idx >= sband->n_channels)
13353 		return -ENOENT;
13354 
13355 	ar = ath12k_mac_get_ar_by_chan(hw, &sband->channels[idx]);
13356 	if (!ar) {
13357 		if (sband->channels[idx].flags & IEEE80211_CHAN_DISABLED) {
13358 			memset(survey, 0, sizeof(*survey));
13359 			return 0;
13360 		}
13361 		return -ENOENT;
13362 	}
13363 
13364 	ar_survey = &ar->survey[idx];
13365 
13366 	ath12k_mac_update_bss_chan_survey(ar, &sband->channels[idx]);
13367 
13368 	spin_lock_bh(&ar->data_lock);
13369 	memcpy(survey, ar_survey, sizeof(*survey));
13370 	spin_unlock_bh(&ar->data_lock);
13371 
13372 	survey->channel = &sband->channels[idx];
13373 
13374 	if (ar->rx_channel == survey->channel)
13375 		survey->filled |= SURVEY_INFO_IN_USE;
13376 
13377 	return 0;
13378 }
13379 EXPORT_SYMBOL(ath12k_mac_op_get_survey);
13380 
13381 static void ath12k_mac_put_chain_rssi(struct station_info *sinfo,
13382 				      struct ath12k_link_sta *arsta)
13383 {
13384 	s8 rssi;
13385 	int i;
13386 
13387 	for (i = 0; i < ARRAY_SIZE(sinfo->chain_signal); i++) {
13388 		sinfo->chains &= ~BIT(i);
13389 		rssi = arsta->chain_signal[i];
13390 
13391 		if (rssi != ATH12K_DEFAULT_NOISE_FLOOR &&
13392 		    rssi != ATH12K_INVALID_RSSI_FULL &&
13393 		    rssi != ATH12K_INVALID_RSSI_EMPTY &&
13394 		    rssi != 0) {
13395 			sinfo->chain_signal[i] = rssi;
13396 			sinfo->chains |= BIT(i);
13397 			sinfo->filled |= BIT_ULL(NL80211_STA_INFO_CHAIN_SIGNAL);
13398 		}
13399 	}
13400 }
13401 
13402 void ath12k_mac_op_sta_statistics(struct ieee80211_hw *hw,
13403 				  struct ieee80211_vif *vif,
13404 				  struct ieee80211_sta *sta,
13405 				  struct station_info *sinfo)
13406 {
13407 	struct ath12k_sta *ahsta = ath12k_sta_to_ahsta(sta);
13408 	struct ath12k_dp_link_peer_rate_info rate_info = {};
13409 	struct ath12k_fw_stats_req_params params = {};
13410 	struct ath12k_dp_link_peer *peer;
13411 	struct ath12k_link_sta *arsta;
13412 	s8 signal, noise_floor;
13413 	struct ath12k_dp *dp;
13414 	struct ath12k *ar;
13415 	bool db2dbm;
13416 
13417 	lockdep_assert_wiphy(hw->wiphy);
13418 
13419 	arsta = &ahsta->deflink;
13420 	ar = ath12k_get_ar_by_vif(hw, vif, arsta->link_id);
13421 	if (!ar)
13422 		return;
13423 
13424 	dp = ath12k_ab_to_dp(ar->ab);
13425 	ath12k_dp_link_peer_get_sta_rate_info_stats(dp, arsta->addr, &rate_info);
13426 
13427 	db2dbm = test_bit(WMI_TLV_SERVICE_HW_DB2DBM_CONVERSION_SUPPORT,
13428 			  ar->ab->wmi_ab.svc_map);
13429 
13430 	sinfo->rx_duration = rate_info.rx_duration;
13431 	sinfo->filled |= BIT_ULL(NL80211_STA_INFO_RX_DURATION);
13432 
13433 	sinfo->tx_duration = rate_info.tx_duration;
13434 	sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_DURATION);
13435 
13436 	if (rate_info.txrate.legacy || rate_info.txrate.nss) {
13437 		if (rate_info.txrate.legacy) {
13438 			sinfo->txrate.legacy = rate_info.txrate.legacy;
13439 		} else {
13440 			sinfo->txrate.mcs = rate_info.txrate.mcs;
13441 			sinfo->txrate.nss = rate_info.txrate.nss;
13442 			sinfo->txrate.bw = rate_info.txrate.bw;
13443 			sinfo->txrate.he_gi = rate_info.txrate.he_gi;
13444 			sinfo->txrate.he_dcm = rate_info.txrate.he_dcm;
13445 			sinfo->txrate.he_ru_alloc = rate_info.txrate.he_ru_alloc;
13446 			sinfo->txrate.eht_gi = rate_info.txrate.eht_gi;
13447 			sinfo->txrate.eht_ru_alloc = rate_info.txrate.eht_ru_alloc;
13448 		}
13449 		sinfo->txrate.flags = rate_info.txrate.flags;
13450 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_BITRATE);
13451 	}
13452 
13453 	/* TODO: Use real NF instead of default one. */
13454 	signal = rate_info.rssi_comb;
13455 
13456 	params.pdev_id = ar->pdev->pdev_id;
13457 	params.vdev_id = 0;
13458 	params.stats_id = WMI_REQUEST_VDEV_STAT;
13459 
13460 	if (!signal &&
13461 	    ahsta->ahvif->vdev_type == WMI_VDEV_TYPE_STA &&
13462 	    !(ath12k_mac_get_fw_stats(ar, &params))) {
13463 		signal = arsta->rssi_beacon;
13464 		ath12k_fw_stats_reset(ar);
13465 	}
13466 
13467 	params.stats_id = WMI_REQUEST_RSSI_PER_CHAIN_STAT;
13468 	if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_CHAIN_SIGNAL)) &&
13469 	    ahsta->ahvif->vdev_type == WMI_VDEV_TYPE_STA &&
13470 	    !(ath12k_mac_get_fw_stats(ar, &params))) {
13471 		ath12k_mac_put_chain_rssi(sinfo, arsta);
13472 		ath12k_fw_stats_reset(ar);
13473 	}
13474 
13475 	spin_lock_bh(&ar->data_lock);
13476 	noise_floor = ath12k_pdev_get_noise_floor(ar);
13477 	spin_unlock_bh(&ar->data_lock);
13478 
13479 	if (signal) {
13480 		sinfo->signal = db2dbm ? signal : signal + noise_floor;
13481 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_SIGNAL);
13482 	}
13483 
13484 	sinfo->signal_avg = rate_info.signal_avg;
13485 
13486 	if (!db2dbm)
13487 		sinfo->signal_avg += noise_floor;
13488 
13489 	sinfo->filled |= BIT_ULL(NL80211_STA_INFO_SIGNAL_AVG);
13490 
13491 	spin_lock_bh(&dp->dp_lock);
13492 	peer = ath12k_dp_link_peer_find_by_addr(dp, arsta->addr);
13493 	if (!peer) {
13494 		spin_unlock_bh(&dp->dp_lock);
13495 		return;
13496 	}
13497 
13498 	sinfo->tx_retries = peer->tx_retry_count;
13499 	sinfo->tx_failed = peer->tx_retry_failed;
13500 	sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_RETRIES);
13501 	sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_FAILED);
13502 
13503 	spin_unlock_bh(&dp->dp_lock);
13504 }
13505 EXPORT_SYMBOL(ath12k_mac_op_sta_statistics);
13506 
13507 void ath12k_mac_op_link_sta_statistics(struct ieee80211_hw *hw,
13508 				       struct ieee80211_vif *vif,
13509 				       struct ieee80211_link_sta *link_sta,
13510 				       struct link_station_info *link_sinfo)
13511 {
13512 	struct ath12k_sta *ahsta = ath12k_sta_to_ahsta(link_sta->sta);
13513 	struct ath12k_fw_stats_req_params params = {};
13514 	struct ath12k_dp_link_peer *peer;
13515 	struct ath12k_link_sta *arsta;
13516 	struct ath12k *ar;
13517 	s8 signal;
13518 	bool db2dbm;
13519 
13520 	lockdep_assert_wiphy(hw->wiphy);
13521 
13522 	arsta = wiphy_dereference(hw->wiphy, ahsta->link[link_sta->link_id]);
13523 
13524 	if (!arsta)
13525 		return;
13526 
13527 	ar = ath12k_get_ar_by_vif(hw, vif, arsta->link_id);
13528 	if (!ar)
13529 		return;
13530 
13531 	db2dbm = test_bit(WMI_TLV_SERVICE_HW_DB2DBM_CONVERSION_SUPPORT,
13532 			  ar->ab->wmi_ab.svc_map);
13533 
13534 	spin_lock_bh(&ar->ab->dp->dp_lock);
13535 	peer = ath12k_dp_link_peer_find_by_addr(ar->ab->dp, arsta->addr);
13536 	if (!peer) {
13537 		spin_unlock_bh(&ar->ab->dp->dp_lock);
13538 		return;
13539 	}
13540 
13541 	link_sinfo->rx_duration = peer->rx_duration;
13542 	link_sinfo->filled |= BIT_ULL(NL80211_STA_INFO_RX_DURATION);
13543 
13544 	link_sinfo->tx_duration = peer->tx_duration;
13545 	link_sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_DURATION);
13546 
13547 	if (peer->txrate.legacy || peer->txrate.nss) {
13548 		if (peer->txrate.legacy) {
13549 			link_sinfo->txrate.legacy = peer->txrate.legacy;
13550 		} else {
13551 			link_sinfo->txrate.mcs = peer->txrate.mcs;
13552 			link_sinfo->txrate.nss = peer->txrate.nss;
13553 			link_sinfo->txrate.bw = peer->txrate.bw;
13554 			link_sinfo->txrate.he_gi = peer->txrate.he_gi;
13555 			link_sinfo->txrate.he_dcm = peer->txrate.he_dcm;
13556 			link_sinfo->txrate.he_ru_alloc =
13557 				peer->txrate.he_ru_alloc;
13558 			link_sinfo->txrate.eht_gi = peer->txrate.eht_gi;
13559 			link_sinfo->txrate.eht_ru_alloc =
13560 				peer->txrate.eht_ru_alloc;
13561 		}
13562 		link_sinfo->txrate.flags = peer->txrate.flags;
13563 		link_sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_BITRATE);
13564 	}
13565 
13566 	link_sinfo->signal_avg = ewma_avg_rssi_read(&peer->avg_rssi);
13567 
13568 	if (!db2dbm)
13569 		link_sinfo->signal_avg += ATH12K_DEFAULT_NOISE_FLOOR;
13570 
13571 	link_sinfo->filled |= BIT_ULL(NL80211_STA_INFO_SIGNAL_AVG);
13572 
13573 	link_sinfo->tx_retries = peer->tx_retry_count;
13574 	link_sinfo->tx_failed = peer->tx_retry_failed;
13575 	link_sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_RETRIES);
13576 	link_sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_FAILED);
13577 
13578 	/* TODO: Use real NF instead of default one. */
13579 	signal = peer->rssi_comb;
13580 
13581 	spin_unlock_bh(&ar->ab->dp->dp_lock);
13582 
13583 	if (!signal && ahsta->ahvif->vdev_type == WMI_VDEV_TYPE_STA) {
13584 		params.pdev_id = ar->pdev->pdev_id;
13585 		params.vdev_id = 0;
13586 		params.stats_id = WMI_REQUEST_VDEV_STAT;
13587 
13588 		if (!ath12k_mac_get_fw_stats(ar, &params)) {
13589 			signal = arsta->rssi_beacon;
13590 			ath12k_fw_stats_reset(ar);
13591 		}
13592 	}
13593 
13594 	if (signal) {
13595 		link_sinfo->signal =
13596 			db2dbm ? signal : signal + ATH12K_DEFAULT_NOISE_FLOOR;
13597 		link_sinfo->filled |= BIT_ULL(NL80211_STA_INFO_SIGNAL);
13598 	}
13599 }
13600 EXPORT_SYMBOL(ath12k_mac_op_link_sta_statistics);
13601 
13602 int ath12k_mac_op_cancel_remain_on_channel(struct ieee80211_hw *hw,
13603 					   struct ieee80211_vif *vif)
13604 {
13605 	struct ath12k_hw *ah = ath12k_hw_to_ah(hw);
13606 	struct ath12k *ar;
13607 
13608 	ar = ath12k_ah_to_ar(ah, 0);
13609 
13610 	lockdep_assert_wiphy(hw->wiphy);
13611 
13612 	spin_lock_bh(&ar->data_lock);
13613 	ar->scan.roc_notify = false;
13614 	spin_unlock_bh(&ar->data_lock);
13615 
13616 	ath12k_scan_abort(ar);
13617 
13618 	cancel_delayed_work_sync(&ar->scan.timeout);
13619 	wiphy_work_flush(hw->wiphy, &ar->scan.vdev_clean_wk);
13620 
13621 	return 0;
13622 }
13623 EXPORT_SYMBOL(ath12k_mac_op_cancel_remain_on_channel);
13624 
13625 int ath12k_mac_op_remain_on_channel(struct ieee80211_hw *hw,
13626 				    struct ieee80211_vif *vif,
13627 				    struct ieee80211_channel *chan,
13628 				    int duration,
13629 				    enum ieee80211_roc_type type)
13630 {
13631 	struct ath12k_vif *ahvif = ath12k_vif_to_ahvif(vif);
13632 	struct ath12k_hw *ah = ath12k_hw_to_ah(hw);
13633 	struct ath12k_link_vif *arvif;
13634 	struct ath12k *ar;
13635 	u32 scan_time_msec;
13636 	bool create = true;
13637 	u8 link_id;
13638 	int ret;
13639 
13640 	lockdep_assert_wiphy(hw->wiphy);
13641 
13642 	ar = ath12k_mac_select_scan_device(hw, vif, chan->center_freq);
13643 	if (!ar)
13644 		return -EINVAL;
13645 
13646 	/* check if any of the links of ML VIF is already started on
13647 	 * radio(ar) corresponding to given scan frequency and use it,
13648 	 * if not use deflink(link 0) for scan purpose.
13649 	 */
13650 
13651 	link_id = ath12k_mac_find_link_id_by_ar(ahvif, ar);
13652 	arvif = ath12k_mac_assign_link_vif(ah, vif, link_id);
13653 	/* If the vif is already assigned to a specific vdev of an ar,
13654 	 * check whether its already started, vdev which is started
13655 	 * are not allowed to switch to a new radio.
13656 	 * If the vdev is not started, but was earlier created on a
13657 	 * different ar, delete that vdev and create a new one. We don't
13658 	 * delete at the scan stop as an optimization to avoid redundant
13659 	 * delete-create vdev's for the same ar, in case the request is
13660 	 * always on the same band for the vif
13661 	 */
13662 	if (arvif->is_created) {
13663 		if (WARN_ON(!arvif->ar))
13664 			return -EINVAL;
13665 
13666 		if (ar != arvif->ar && arvif->is_started)
13667 			return -EBUSY;
13668 
13669 		if (ar != arvif->ar) {
13670 			ath12k_mac_remove_link_interface(hw, arvif);
13671 			ath12k_mac_unassign_link_vif(arvif);
13672 		} else {
13673 			create = false;
13674 		}
13675 	}
13676 
13677 	if (create) {
13678 		arvif = ath12k_mac_assign_link_vif(ah, vif, link_id);
13679 
13680 		ret = ath12k_mac_vdev_create(ar, arvif);
13681 		if (ret) {
13682 			ath12k_warn(ar->ab, "unable to create scan vdev for roc: %d\n",
13683 				    ret);
13684 			ath12k_mac_unassign_link_vif(arvif);
13685 			return ret;
13686 		}
13687 	}
13688 
13689 	spin_lock_bh(&ar->data_lock);
13690 
13691 	switch (ar->scan.state) {
13692 	case ATH12K_SCAN_IDLE:
13693 		reinit_completion(&ar->scan.started);
13694 		reinit_completion(&ar->scan.completed);
13695 		reinit_completion(&ar->scan.on_channel);
13696 		ar->scan.state = ATH12K_SCAN_STARTING;
13697 		ar->scan.is_roc = true;
13698 		ar->scan.arvif = arvif;
13699 		ar->scan.roc_freq = chan->center_freq;
13700 		ar->scan.roc_notify = true;
13701 		ret = 0;
13702 		break;
13703 	case ATH12K_SCAN_STARTING:
13704 	case ATH12K_SCAN_RUNNING:
13705 	case ATH12K_SCAN_ABORTING:
13706 		ret = -EBUSY;
13707 		break;
13708 	}
13709 
13710 	spin_unlock_bh(&ar->data_lock);
13711 
13712 	if (ret)
13713 		return ret;
13714 
13715 	scan_time_msec = hw->wiphy->max_remain_on_channel_duration * 2;
13716 
13717 	struct ath12k_wmi_scan_req_arg *arg __free(kfree) =
13718 					kzalloc_obj(*arg);
13719 	if (!arg)
13720 		return -ENOMEM;
13721 
13722 	ath12k_wmi_start_scan_init(ar, arg);
13723 	arg->num_chan = 1;
13724 
13725 	u32 *chan_list __free(kfree) = kcalloc(arg->num_chan, sizeof(*chan_list),
13726 					       GFP_KERNEL);
13727 	if (!chan_list)
13728 		return -ENOMEM;
13729 
13730 	arg->chan_list = chan_list;
13731 	arg->vdev_id = arvif->vdev_id;
13732 	arg->scan_id = ATH12K_SCAN_ID;
13733 	arg->chan_list[0] = chan->center_freq;
13734 	arg->dwell_time_active = scan_time_msec;
13735 	arg->dwell_time_passive = scan_time_msec;
13736 	arg->max_scan_time = scan_time_msec;
13737 	arg->scan_f_passive = 1;
13738 	arg->burst_duration = duration;
13739 
13740 	ret = ath12k_start_scan(ar, arg);
13741 	if (ret) {
13742 		ath12k_warn(ar->ab, "failed to start roc scan: %d\n", ret);
13743 
13744 		spin_lock_bh(&ar->data_lock);
13745 		ar->scan.state = ATH12K_SCAN_IDLE;
13746 		spin_unlock_bh(&ar->data_lock);
13747 		return ret;
13748 	}
13749 
13750 	ret = wait_for_completion_timeout(&ar->scan.on_channel, 3 * HZ);
13751 	if (ret == 0) {
13752 		ath12k_warn(ar->ab, "failed to switch to channel for roc scan\n");
13753 		ret = ath12k_scan_stop(ar);
13754 		if (ret)
13755 			ath12k_warn(ar->ab, "failed to stop scan: %d\n", ret);
13756 		return -ETIMEDOUT;
13757 	}
13758 
13759 	ieee80211_queue_delayed_work(hw, &ar->scan.timeout,
13760 				     msecs_to_jiffies(duration));
13761 
13762 	return 0;
13763 }
13764 EXPORT_SYMBOL(ath12k_mac_op_remain_on_channel);
13765 
13766 void ath12k_mac_op_set_rekey_data(struct ieee80211_hw *hw,
13767 				  struct ieee80211_vif *vif,
13768 				  struct cfg80211_gtk_rekey_data *data)
13769 {
13770 	struct ath12k_vif *ahvif = ath12k_vif_to_ahvif(vif);
13771 	struct ath12k_rekey_data *rekey_data;
13772 	struct ath12k_hw *ah = ath12k_hw_to_ah(hw);
13773 	struct ath12k *ar = ath12k_ah_to_ar(ah, 0);
13774 	struct ath12k_link_vif *arvif;
13775 
13776 	lockdep_assert_wiphy(hw->wiphy);
13777 
13778 	arvif = &ahvif->deflink;
13779 	rekey_data = &arvif->rekey_data;
13780 
13781 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac set rekey data vdev %d\n",
13782 		   arvif->vdev_id);
13783 
13784 	memcpy(rekey_data->kck, data->kck, NL80211_KCK_LEN);
13785 	memcpy(rekey_data->kek, data->kek, NL80211_KEK_LEN);
13786 
13787 	/* The supplicant works on big-endian, the firmware expects it on
13788 	 * little endian.
13789 	 */
13790 	rekey_data->replay_ctr = get_unaligned_be64(data->replay_ctr);
13791 
13792 	arvif->rekey_data.enable_offload = true;
13793 
13794 	ath12k_dbg_dump(ar->ab, ATH12K_DBG_MAC, "kck", NULL,
13795 			rekey_data->kck, NL80211_KCK_LEN);
13796 	ath12k_dbg_dump(ar->ab, ATH12K_DBG_MAC, "kek", NULL,
13797 			rekey_data->kck, NL80211_KEK_LEN);
13798 	ath12k_dbg_dump(ar->ab, ATH12K_DBG_MAC, "replay ctr", NULL,
13799 			&rekey_data->replay_ctr, sizeof(rekey_data->replay_ctr));
13800 }
13801 EXPORT_SYMBOL(ath12k_mac_op_set_rekey_data);
13802 
13803 void ath12k_mac_update_freq_range(struct ath12k *ar,
13804 				  u32 freq_low, u32 freq_high)
13805 {
13806 	if (!(freq_low && freq_high))
13807 		return;
13808 
13809 	if (ar->freq_range.start_freq || ar->freq_range.end_freq) {
13810 		ar->freq_range.start_freq = min(ar->freq_range.start_freq,
13811 						MHZ_TO_KHZ(freq_low));
13812 		ar->freq_range.end_freq = max(ar->freq_range.end_freq,
13813 					      MHZ_TO_KHZ(freq_high));
13814 	} else {
13815 		ar->freq_range.start_freq = MHZ_TO_KHZ(freq_low);
13816 		ar->freq_range.end_freq = MHZ_TO_KHZ(freq_high);
13817 	}
13818 
13819 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
13820 		   "mac pdev %u freq limit updated. New range %u->%u MHz\n",
13821 		   ar->pdev->pdev_id, KHZ_TO_MHZ(ar->freq_range.start_freq),
13822 		   KHZ_TO_MHZ(ar->freq_range.end_freq));
13823 }
13824 
13825 static void ath12k_mac_update_ch_list(struct ath12k *ar,
13826 				      struct ieee80211_supported_band *band,
13827 				      u32 freq_low, u32 freq_high)
13828 {
13829 	int i;
13830 
13831 	if (!(freq_low && freq_high))
13832 		return;
13833 
13834 	for (i = 0; i < band->n_channels; i++) {
13835 		if (band->channels[i].center_freq < freq_low ||
13836 		    band->channels[i].center_freq > freq_high)
13837 			band->channels[i].flags |= IEEE80211_CHAN_DISABLED;
13838 	}
13839 }
13840 
13841 static u32 ath12k_get_phy_id(struct ath12k *ar, u32 band)
13842 {
13843 	struct ath12k_pdev *pdev = ar->pdev;
13844 	struct ath12k_pdev_cap *pdev_cap = &pdev->cap;
13845 
13846 	if (band == WMI_HOST_WLAN_2GHZ_CAP)
13847 		return pdev_cap->band[NL80211_BAND_2GHZ].phy_id;
13848 
13849 	if (band == WMI_HOST_WLAN_5GHZ_CAP)
13850 		return pdev_cap->band[NL80211_BAND_5GHZ].phy_id;
13851 
13852 	ath12k_warn(ar->ab, "unsupported phy cap:%d\n", band);
13853 
13854 	return 0;
13855 }
13856 
13857 static int ath12k_mac_update_band(struct ath12k *ar,
13858 				  struct ieee80211_supported_band *orig_band,
13859 				  struct ieee80211_supported_band *new_band)
13860 {
13861 	int i;
13862 
13863 	if (!orig_band || !new_band)
13864 		return -EINVAL;
13865 
13866 	if (orig_band->band != new_band->band)
13867 		return -EINVAL;
13868 
13869 	for (i = 0; i < new_band->n_channels; i++) {
13870 		if (new_band->channels[i].flags & IEEE80211_CHAN_DISABLED)
13871 			continue;
13872 		/* An enabled channel in new_band should not be already enabled
13873 		 * in the orig_band
13874 		 */
13875 		if (WARN_ON(!(orig_band->channels[i].flags &
13876 			      IEEE80211_CHAN_DISABLED)))
13877 			return -EINVAL;
13878 		orig_band->channels[i].flags &= ~IEEE80211_CHAN_DISABLED;
13879 	}
13880 	return 0;
13881 }
13882 
13883 static int ath12k_mac_setup_channels_rates(struct ath12k *ar,
13884 					   u32 supported_bands,
13885 					   struct ieee80211_supported_band *bands[])
13886 {
13887 	struct ieee80211_supported_band *band;
13888 	struct ath12k_wmi_hal_reg_capabilities_ext_arg *reg_cap;
13889 	struct ath12k_base *ab = ar->ab;
13890 	u32 phy_id, freq_low, freq_high;
13891 	struct ath12k_hw *ah = ar->ah;
13892 	void *channels;
13893 	int ret;
13894 
13895 	BUILD_BUG_ON((ARRAY_SIZE(ath12k_2ghz_channels) +
13896 		      ARRAY_SIZE(ath12k_5ghz_channels) +
13897 		      ARRAY_SIZE(ath12k_6ghz_channels)) !=
13898 		     ATH12K_NUM_CHANS);
13899 
13900 	reg_cap = &ab->hal_reg_cap[ar->pdev_idx];
13901 
13902 	if (supported_bands & WMI_HOST_WLAN_2GHZ_CAP) {
13903 		channels = kmemdup(ath12k_2ghz_channels,
13904 				   sizeof(ath12k_2ghz_channels),
13905 				   GFP_KERNEL);
13906 		if (!channels)
13907 			return -ENOMEM;
13908 
13909 		band = &ar->mac.sbands[NL80211_BAND_2GHZ];
13910 		band->band = NL80211_BAND_2GHZ;
13911 		band->n_channels = ARRAY_SIZE(ath12k_2ghz_channels);
13912 		band->channels = channels;
13913 		band->n_bitrates = ath12k_g_rates_size;
13914 		band->bitrates = ath12k_g_rates;
13915 
13916 		if (ab->hw_params->single_pdev_only) {
13917 			phy_id = ath12k_get_phy_id(ar, WMI_HOST_WLAN_2GHZ_CAP);
13918 			reg_cap = &ab->hal_reg_cap[phy_id];
13919 		}
13920 
13921 		freq_low = max(reg_cap->low_2ghz_chan,
13922 			       ab->reg_freq_2ghz.start_freq);
13923 		freq_high = min(reg_cap->high_2ghz_chan,
13924 				ab->reg_freq_2ghz.end_freq);
13925 
13926 		ath12k_mac_update_ch_list(ar, band,
13927 					  reg_cap->low_2ghz_chan,
13928 					  reg_cap->high_2ghz_chan);
13929 
13930 		ath12k_mac_update_freq_range(ar, freq_low, freq_high);
13931 
13932 		if (!bands[NL80211_BAND_2GHZ]) {
13933 			bands[NL80211_BAND_2GHZ] = band;
13934 		} else {
13935 			/* Split mac in same band under same wiphy */
13936 			ret = ath12k_mac_update_band(ar, bands[NL80211_BAND_2GHZ], band);
13937 			if (ret) {
13938 				kfree(channels);
13939 				band->channels = NULL;
13940 				return ret;
13941 			}
13942 			ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac pdev %u identified as 2 GHz split mac with start freq %d end freq %d",
13943 				   ar->pdev->pdev_id,
13944 				   KHZ_TO_MHZ(ar->freq_range.start_freq),
13945 				   KHZ_TO_MHZ(ar->freq_range.end_freq));
13946 		}
13947 	}
13948 
13949 	if (supported_bands & WMI_HOST_WLAN_5GHZ_CAP) {
13950 		if (reg_cap->high_5ghz_chan >= ATH12K_MIN_6GHZ_FREQ) {
13951 			channels = kmemdup(ath12k_6ghz_channels,
13952 					   sizeof(ath12k_6ghz_channels), GFP_KERNEL);
13953 			if (!channels) {
13954 				kfree(ar->mac.sbands[NL80211_BAND_2GHZ].channels);
13955 				return -ENOMEM;
13956 			}
13957 
13958 			ar->supports_6ghz = true;
13959 			band = &ar->mac.sbands[NL80211_BAND_6GHZ];
13960 			band->band = NL80211_BAND_6GHZ;
13961 			band->n_channels = ARRAY_SIZE(ath12k_6ghz_channels);
13962 			band->channels = channels;
13963 			band->n_bitrates = ath12k_a_rates_size;
13964 			band->bitrates = ath12k_a_rates;
13965 
13966 			freq_low = max(reg_cap->low_5ghz_chan,
13967 				       ab->reg_freq_6ghz.start_freq);
13968 			freq_high = min(reg_cap->high_5ghz_chan,
13969 					ab->reg_freq_6ghz.end_freq);
13970 
13971 			ath12k_mac_update_ch_list(ar, band,
13972 						  reg_cap->low_5ghz_chan,
13973 						  reg_cap->high_5ghz_chan);
13974 
13975 			ath12k_mac_update_freq_range(ar, freq_low, freq_high);
13976 			ah->use_6ghz_regd = true;
13977 
13978 			if (!bands[NL80211_BAND_6GHZ]) {
13979 				bands[NL80211_BAND_6GHZ] = band;
13980 			} else {
13981 				/* Split mac in same band under same wiphy */
13982 				ret = ath12k_mac_update_band(ar,
13983 							     bands[NL80211_BAND_6GHZ],
13984 							     band);
13985 				if (ret) {
13986 					kfree(ar->mac.sbands[NL80211_BAND_2GHZ].channels);
13987 					ar->mac.sbands[NL80211_BAND_2GHZ].channels = NULL;
13988 					kfree(channels);
13989 					band->channels = NULL;
13990 					return ret;
13991 				}
13992 				ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac pdev %u identified as 6 GHz split mac with start freq %d end freq %d",
13993 					   ar->pdev->pdev_id,
13994 					   KHZ_TO_MHZ(ar->freq_range.start_freq),
13995 					   KHZ_TO_MHZ(ar->freq_range.end_freq));
13996 			}
13997 		}
13998 
13999 		if (reg_cap->low_5ghz_chan < ATH12K_MIN_6GHZ_FREQ) {
14000 			channels = kmemdup(ath12k_5ghz_channels,
14001 					   sizeof(ath12k_5ghz_channels),
14002 					   GFP_KERNEL);
14003 			if (!channels) {
14004 				kfree(ar->mac.sbands[NL80211_BAND_2GHZ].channels);
14005 				kfree(ar->mac.sbands[NL80211_BAND_6GHZ].channels);
14006 				return -ENOMEM;
14007 			}
14008 
14009 			band = &ar->mac.sbands[NL80211_BAND_5GHZ];
14010 			band->band = NL80211_BAND_5GHZ;
14011 			band->n_channels = ARRAY_SIZE(ath12k_5ghz_channels);
14012 			band->channels = channels;
14013 			band->n_bitrates = ath12k_a_rates_size;
14014 			band->bitrates = ath12k_a_rates;
14015 
14016 			if (ab->hw_params->single_pdev_only) {
14017 				phy_id = ath12k_get_phy_id(ar, WMI_HOST_WLAN_5GHZ_CAP);
14018 				reg_cap = &ab->hal_reg_cap[phy_id];
14019 			}
14020 
14021 			freq_low = max(reg_cap->low_5ghz_chan,
14022 				       ab->reg_freq_5ghz.start_freq);
14023 			freq_high = min(reg_cap->high_5ghz_chan,
14024 					ab->reg_freq_5ghz.end_freq);
14025 
14026 			ath12k_mac_update_ch_list(ar, band,
14027 						  reg_cap->low_5ghz_chan,
14028 						  reg_cap->high_5ghz_chan);
14029 
14030 			ath12k_mac_update_freq_range(ar, freq_low, freq_high);
14031 
14032 			if (!bands[NL80211_BAND_5GHZ]) {
14033 				bands[NL80211_BAND_5GHZ] = band;
14034 			} else {
14035 				/* Split mac in same band under same wiphy */
14036 				ret = ath12k_mac_update_band(ar,
14037 							     bands[NL80211_BAND_5GHZ],
14038 							     band);
14039 				if (ret) {
14040 					kfree(ar->mac.sbands[NL80211_BAND_2GHZ].channels);
14041 					ar->mac.sbands[NL80211_BAND_2GHZ].channels = NULL;
14042 					kfree(ar->mac.sbands[NL80211_BAND_6GHZ].channels);
14043 					ar->mac.sbands[NL80211_BAND_2GHZ].channels = NULL;
14044 					kfree(channels);
14045 					band->channels = NULL;
14046 					return ret;
14047 				}
14048 				ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac pdev %u identified as 5 GHz split mac with start freq %d end freq %d",
14049 					   ar->pdev->pdev_id,
14050 					   KHZ_TO_MHZ(ar->freq_range.start_freq),
14051 					   KHZ_TO_MHZ(ar->freq_range.end_freq));
14052 			}
14053 		}
14054 	}
14055 
14056 	return 0;
14057 }
14058 
14059 static u16 ath12k_mac_get_ifmodes(struct ath12k_hw *ah)
14060 {
14061 	struct ath12k *ar;
14062 	int i;
14063 	u16 interface_modes = U16_MAX;
14064 
14065 	for_each_ar(ah, ar, i)
14066 		interface_modes &= ar->ab->hw_params->interface_modes;
14067 
14068 	return interface_modes == U16_MAX ? 0 : interface_modes;
14069 }
14070 
14071 static bool ath12k_mac_is_iface_mode_enable(struct ath12k_hw *ah,
14072 					    enum nl80211_iftype type)
14073 {
14074 	struct ath12k *ar;
14075 	int i;
14076 	u16 interface_modes, mode = 0;
14077 	bool is_enable = false;
14078 
14079 	if (type == NL80211_IFTYPE_MESH_POINT) {
14080 		if (IS_ENABLED(CONFIG_MAC80211_MESH))
14081 			mode = BIT(type);
14082 	} else {
14083 		mode = BIT(type);
14084 	}
14085 
14086 	for_each_ar(ah, ar, i) {
14087 		interface_modes = ar->ab->hw_params->interface_modes;
14088 		if (interface_modes & mode) {
14089 			is_enable = true;
14090 			break;
14091 		}
14092 	}
14093 
14094 	return is_enable;
14095 }
14096 
14097 static int
14098 ath12k_mac_setup_radio_iface_comb(struct ath12k *ar,
14099 				  struct ieee80211_iface_combination *comb)
14100 {
14101 	u16 interface_modes = ar->ab->hw_params->interface_modes;
14102 	struct ieee80211_iface_limit *limits;
14103 	int n_limits, max_interfaces;
14104 	bool ap, mesh, p2p;
14105 
14106 	ap = interface_modes & BIT(NL80211_IFTYPE_AP);
14107 	p2p = interface_modes & BIT(NL80211_IFTYPE_P2P_DEVICE);
14108 
14109 	mesh = IS_ENABLED(CONFIG_MAC80211_MESH) &&
14110 	       (interface_modes & BIT(NL80211_IFTYPE_MESH_POINT));
14111 
14112 	if ((ap || mesh) && !p2p) {
14113 		n_limits = 2;
14114 		max_interfaces = 16;
14115 	} else if (p2p) {
14116 		n_limits = 3;
14117 		if (ap || mesh)
14118 			max_interfaces = 16;
14119 		else
14120 			max_interfaces = 3;
14121 	} else {
14122 		n_limits = 1;
14123 		max_interfaces = 1;
14124 	}
14125 
14126 	limits = kzalloc_objs(*limits, n_limits);
14127 	if (!limits)
14128 		return -ENOMEM;
14129 
14130 	limits[0].max = 1;
14131 	limits[0].types |= BIT(NL80211_IFTYPE_STATION);
14132 
14133 	if (ap || mesh || p2p)
14134 		limits[1].max = max_interfaces;
14135 
14136 	if (ap)
14137 		limits[1].types |= BIT(NL80211_IFTYPE_AP);
14138 
14139 	if (mesh)
14140 		limits[1].types |= BIT(NL80211_IFTYPE_MESH_POINT);
14141 
14142 	if (p2p) {
14143 		limits[1].types |= BIT(NL80211_IFTYPE_P2P_CLIENT) |
14144 					BIT(NL80211_IFTYPE_P2P_GO);
14145 		limits[2].max = 1;
14146 		limits[2].types |= BIT(NL80211_IFTYPE_P2P_DEVICE);
14147 	}
14148 
14149 	comb[0].limits = limits;
14150 	comb[0].n_limits = n_limits;
14151 	comb[0].max_interfaces = max_interfaces;
14152 	comb[0].beacon_int_infra_match = true;
14153 	comb[0].beacon_int_min_gcd = 100;
14154 
14155 	comb[0].num_different_channels = 1;
14156 	comb[0].radar_detect_widths = BIT(NL80211_CHAN_WIDTH_20_NOHT) |
14157 				      BIT(NL80211_CHAN_WIDTH_20) |
14158 				      BIT(NL80211_CHAN_WIDTH_40) |
14159 				      BIT(NL80211_CHAN_WIDTH_80) |
14160 				      BIT(NL80211_CHAN_WIDTH_160);
14161 
14162 	return 0;
14163 }
14164 
14165 static int
14166 ath12k_mac_setup_global_iface_comb(struct ath12k_hw *ah,
14167 				   struct wiphy_radio *radio,
14168 				   u8 n_radio,
14169 				   struct ieee80211_iface_combination *comb)
14170 {
14171 	const struct ieee80211_iface_combination *iter_comb;
14172 	struct ieee80211_iface_limit *limits;
14173 	int i, j, n_limits;
14174 	bool ap, mesh, p2p;
14175 
14176 	if (!n_radio)
14177 		return 0;
14178 
14179 	ap = ath12k_mac_is_iface_mode_enable(ah, NL80211_IFTYPE_AP);
14180 	p2p = ath12k_mac_is_iface_mode_enable(ah, NL80211_IFTYPE_P2P_DEVICE);
14181 	mesh = ath12k_mac_is_iface_mode_enable(ah, NL80211_IFTYPE_MESH_POINT);
14182 
14183 	if ((ap || mesh) && !p2p)
14184 		n_limits = 2;
14185 	else if (p2p)
14186 		n_limits = 3;
14187 	else
14188 		n_limits = 1;
14189 
14190 	limits = kzalloc_objs(*limits, n_limits);
14191 	if (!limits)
14192 		return -ENOMEM;
14193 
14194 	for (i = 0; i < n_radio; i++) {
14195 		iter_comb = radio[i].iface_combinations;
14196 		for (j = 0; j < iter_comb->n_limits && j < n_limits; j++) {
14197 			limits[j].types |= iter_comb->limits[j].types;
14198 			limits[j].max += iter_comb->limits[j].max;
14199 		}
14200 
14201 		comb->max_interfaces += iter_comb->max_interfaces;
14202 		comb->num_different_channels += iter_comb->num_different_channels;
14203 		comb->radar_detect_widths |= iter_comb->radar_detect_widths;
14204 	}
14205 
14206 	comb->limits = limits;
14207 	comb->n_limits = n_limits;
14208 	comb->beacon_int_infra_match = true;
14209 	comb->beacon_int_min_gcd = 100;
14210 
14211 	return 0;
14212 }
14213 
14214 static
14215 void ath12k_mac_cleanup_iface_comb(const struct ieee80211_iface_combination *iface_comb)
14216 {
14217 	kfree(iface_comb[0].limits);
14218 	kfree(iface_comb);
14219 }
14220 
14221 static void ath12k_mac_cleanup_iface_combinations(struct ath12k_hw *ah)
14222 {
14223 	struct wiphy *wiphy = ah->hw->wiphy;
14224 	const struct wiphy_radio *radio;
14225 	int i;
14226 
14227 	if (wiphy->n_radio > 0) {
14228 		radio = wiphy->radio;
14229 		for (i = 0; i < wiphy->n_radio; i++)
14230 			ath12k_mac_cleanup_iface_comb(radio[i].iface_combinations);
14231 
14232 		kfree(wiphy->radio);
14233 	}
14234 
14235 	ath12k_mac_cleanup_iface_comb(wiphy->iface_combinations);
14236 }
14237 
14238 static int ath12k_mac_setup_iface_combinations(struct ath12k_hw *ah)
14239 {
14240 	struct ieee80211_iface_combination *combinations, *comb;
14241 	struct wiphy *wiphy = ah->hw->wiphy;
14242 	struct wiphy_radio *radio;
14243 	int n_combinations = 1;
14244 	struct ath12k *ar;
14245 	int i, ret;
14246 
14247 	if (ah->num_radio == 1) {
14248 		ar = &ah->radio[0];
14249 
14250 		if (ar->ab->hw_params->single_pdev_only)
14251 			n_combinations = 2;
14252 
14253 		combinations = kzalloc_objs(*combinations, n_combinations,
14254 					    GFP_KERNEL);
14255 		if (!combinations)
14256 			return -ENOMEM;
14257 
14258 		ret = ath12k_mac_setup_radio_iface_comb(ar, combinations);
14259 		if (ret) {
14260 			ath12k_hw_warn(ah, "failed to setup radio interface combinations for one radio: %d",
14261 				       ret);
14262 			goto err_free_combinations;
14263 		}
14264 
14265 		if (ar->ab->hw_params->single_pdev_only) {
14266 			comb = combinations + 1;
14267 			memcpy(comb, combinations, sizeof(*comb));
14268 			comb->num_different_channels = 2;
14269 			comb->radar_detect_widths = 0;
14270 		}
14271 
14272 		goto out;
14273 	}
14274 
14275 	combinations = kzalloc_objs(*combinations, n_combinations);
14276 	if (!combinations)
14277 		return -ENOMEM;
14278 
14279 	/* there are multiple radios */
14280 
14281 	radio = kzalloc_objs(*radio, ah->num_radio);
14282 	if (!radio) {
14283 		ret = -ENOMEM;
14284 		goto err_free_combinations;
14285 	}
14286 
14287 	for_each_ar(ah, ar, i) {
14288 		comb = kzalloc_obj(*comb);
14289 		if (!comb) {
14290 			ret = -ENOMEM;
14291 			goto err_free_radios;
14292 		}
14293 
14294 		ret = ath12k_mac_setup_radio_iface_comb(ar, comb);
14295 		if (ret) {
14296 			ath12k_hw_warn(ah, "failed to setup radio interface combinations for radio %d: %d",
14297 				       i, ret);
14298 			kfree(comb);
14299 			goto err_free_radios;
14300 		}
14301 
14302 		radio[i].freq_range = &ar->freq_range;
14303 		radio[i].n_freq_range = 1;
14304 
14305 		radio[i].iface_combinations = comb;
14306 		radio[i].n_iface_combinations = 1;
14307 	}
14308 
14309 	ret = ath12k_mac_setup_global_iface_comb(ah, radio, ah->num_radio, combinations);
14310 	if (ret) {
14311 		ath12k_hw_warn(ah, "failed to setup global interface combinations: %d",
14312 			       ret);
14313 		goto err_free_all_radios;
14314 	}
14315 
14316 	wiphy->radio = radio;
14317 	wiphy->n_radio = ah->num_radio;
14318 
14319 out:
14320 	wiphy->iface_combinations = combinations;
14321 	wiphy->n_iface_combinations = n_combinations;
14322 
14323 	return 0;
14324 
14325 err_free_all_radios:
14326 	i = ah->num_radio;
14327 
14328 err_free_radios:
14329 	while (i--)
14330 		ath12k_mac_cleanup_iface_comb(radio[i].iface_combinations);
14331 
14332 	kfree(radio);
14333 
14334 err_free_combinations:
14335 	kfree(combinations);
14336 
14337 	return ret;
14338 }
14339 
14340 static const u8 ath12k_if_types_ext_capa[] = {
14341 	[0] = WLAN_EXT_CAPA1_EXT_CHANNEL_SWITCHING,
14342 	[2] = WLAN_EXT_CAPA3_MULTI_BSSID_SUPPORT,
14343 	[7] = WLAN_EXT_CAPA8_OPMODE_NOTIF,
14344 };
14345 
14346 static const u8 ath12k_if_types_ext_capa_sta[] = {
14347 	[0] = WLAN_EXT_CAPA1_EXT_CHANNEL_SWITCHING,
14348 	[2] = WLAN_EXT_CAPA3_MULTI_BSSID_SUPPORT,
14349 	[7] = WLAN_EXT_CAPA8_OPMODE_NOTIF,
14350 	[9] = WLAN_EXT_CAPA10_TWT_REQUESTER_SUPPORT,
14351 };
14352 
14353 static const u8 ath12k_if_types_ext_capa_ap[] = {
14354 	[0] = WLAN_EXT_CAPA1_EXT_CHANNEL_SWITCHING,
14355 	[2] = WLAN_EXT_CAPA3_MULTI_BSSID_SUPPORT,
14356 	[7] = WLAN_EXT_CAPA8_OPMODE_NOTIF,
14357 	[9] = WLAN_EXT_CAPA10_TWT_RESPONDER_SUPPORT,
14358 	[10] = WLAN_EXT_CAPA11_EMA_SUPPORT,
14359 };
14360 
14361 static struct wiphy_iftype_ext_capab ath12k_iftypes_ext_capa[] = {
14362 	{
14363 		.extended_capabilities = ath12k_if_types_ext_capa,
14364 		.extended_capabilities_mask = ath12k_if_types_ext_capa,
14365 		.extended_capabilities_len = sizeof(ath12k_if_types_ext_capa),
14366 	}, {
14367 		.iftype = NL80211_IFTYPE_STATION,
14368 		.extended_capabilities = ath12k_if_types_ext_capa_sta,
14369 		.extended_capabilities_mask = ath12k_if_types_ext_capa_sta,
14370 		.extended_capabilities_len =
14371 				sizeof(ath12k_if_types_ext_capa_sta),
14372 	}, {
14373 		.iftype = NL80211_IFTYPE_AP,
14374 		.extended_capabilities = ath12k_if_types_ext_capa_ap,
14375 		.extended_capabilities_mask = ath12k_if_types_ext_capa_ap,
14376 		.extended_capabilities_len =
14377 				sizeof(ath12k_if_types_ext_capa_ap),
14378 		.eml_capabilities = 0,
14379 		.mld_capa_and_ops = 0,
14380 	},
14381 };
14382 
14383 static void ath12k_mac_cleanup_unregister(struct ath12k *ar)
14384 {
14385 	idr_for_each(&ar->txmgmt_idr, ath12k_mac_tx_mgmt_pending_free, ar);
14386 	idr_destroy(&ar->txmgmt_idr);
14387 
14388 	kfree(ar->mac.sbands[NL80211_BAND_2GHZ].channels);
14389 	kfree(ar->mac.sbands[NL80211_BAND_5GHZ].channels);
14390 	kfree(ar->mac.sbands[NL80211_BAND_6GHZ].channels);
14391 }
14392 
14393 static void ath12k_mac_hw_unregister(struct ath12k_hw *ah)
14394 {
14395 	struct ieee80211_hw *hw = ah->hw;
14396 	struct ath12k *ar;
14397 	int i;
14398 
14399 	for_each_ar(ah, ar, i) {
14400 		cancel_work_sync(&ar->regd_channel_update_work);
14401 		cancel_work_sync(&ar->regd_update_work);
14402 		ath12k_debugfs_unregister(ar);
14403 		ath12k_fw_stats_reset(ar);
14404 	}
14405 
14406 	ieee80211_unregister_hw(hw);
14407 
14408 	for_each_ar(ah, ar, i)
14409 		ath12k_mac_cleanup_unregister(ar);
14410 
14411 	ath12k_mac_cleanup_iface_combinations(ah);
14412 
14413 	SET_IEEE80211_DEV(hw, NULL);
14414 }
14415 
14416 static int ath12k_mac_setup_register(struct ath12k *ar,
14417 				     u32 *ht_cap,
14418 				     struct ieee80211_supported_band *bands[])
14419 {
14420 	struct ath12k_pdev_cap *cap = &ar->pdev->cap;
14421 	int ret;
14422 
14423 	init_waitqueue_head(&ar->txmgmt_empty_waitq);
14424 	idr_init(&ar->txmgmt_idr);
14425 	spin_lock_init(&ar->txmgmt_idr_lock);
14426 
14427 	ath12k_pdev_caps_update(ar);
14428 
14429 	ret = ath12k_mac_setup_channels_rates(ar,
14430 					      cap->supported_bands,
14431 					      bands);
14432 	if (ret)
14433 		return ret;
14434 
14435 	ath12k_mac_setup_ht_vht_cap(ar, cap, ht_cap);
14436 	ath12k_mac_setup_sband_iftype_data(ar, cap);
14437 
14438 	ar->max_num_stations = ath12k_core_get_max_station_per_radio(ar->ab);
14439 	ar->max_num_peers = ath12k_core_get_max_peers_per_radio(ar->ab);
14440 
14441 	ar->rssi_info.min_nf_dbm = ATH12K_DEFAULT_NOISE_FLOOR;
14442 	ar->rssi_info.temp_offset = 0;
14443 	ar->rssi_info.noise_floor = ar->rssi_info.min_nf_dbm + ar->rssi_info.temp_offset;
14444 
14445 	return 0;
14446 }
14447 
14448 static int ath12k_mac_hw_register(struct ath12k_hw *ah)
14449 {
14450 	struct ieee80211_hw *hw = ah->hw;
14451 	struct wiphy *wiphy = hw->wiphy;
14452 	struct ath12k *ar = ath12k_ah_to_ar(ah, 0);
14453 	struct ath12k_base *ab = ar->ab;
14454 	struct ath12k_pdev *pdev;
14455 	struct ath12k_pdev_cap *cap;
14456 	static const u32 cipher_suites[] = {
14457 		WLAN_CIPHER_SUITE_TKIP,
14458 		WLAN_CIPHER_SUITE_CCMP,
14459 		WLAN_CIPHER_SUITE_AES_CMAC,
14460 		WLAN_CIPHER_SUITE_BIP_CMAC_256,
14461 		WLAN_CIPHER_SUITE_BIP_GMAC_128,
14462 		WLAN_CIPHER_SUITE_BIP_GMAC_256,
14463 		WLAN_CIPHER_SUITE_GCMP,
14464 		WLAN_CIPHER_SUITE_GCMP_256,
14465 		WLAN_CIPHER_SUITE_CCMP_256,
14466 	};
14467 	int ret, i, j;
14468 	u32 ht_cap = U32_MAX, antennas_rx = 0, antennas_tx = 0;
14469 	bool is_6ghz = false, is_raw_mode = false, is_monitor_disable = false;
14470 	u8 *mac_addr = NULL;
14471 	u8 mbssid_max_interfaces = 0;
14472 
14473 	wiphy->max_ap_assoc_sta = 0;
14474 
14475 	for_each_ar(ah, ar, i) {
14476 		u32 ht_cap_info = 0;
14477 
14478 		pdev = ar->pdev;
14479 		if (ar->ab->pdevs_macaddr_valid) {
14480 			ether_addr_copy(ar->mac_addr, pdev->mac_addr);
14481 		} else {
14482 			ether_addr_copy(ar->mac_addr, ar->ab->mac_addr);
14483 			ar->mac_addr[4] += ar->pdev_idx;
14484 		}
14485 
14486 		ret = ath12k_mac_setup_register(ar, &ht_cap_info, hw->wiphy->bands);
14487 		if (ret)
14488 			goto err_cleanup_unregister;
14489 
14490 		/* 6 GHz does not support HT Cap, hence do not consider it */
14491 		if (!ar->supports_6ghz)
14492 			ht_cap &= ht_cap_info;
14493 
14494 		wiphy->max_ap_assoc_sta += ar->max_num_stations;
14495 
14496 		/* Advertise the max antenna support of all radios, driver can handle
14497 		 * per pdev specific antenna setting based on pdev cap when antenna
14498 		 * changes are made
14499 		 */
14500 		cap = &pdev->cap;
14501 
14502 		antennas_rx = max_t(u32, antennas_rx, cap->rx_chain_mask);
14503 		antennas_tx = max_t(u32, antennas_tx, cap->tx_chain_mask);
14504 
14505 		if (ar->supports_6ghz)
14506 			is_6ghz = true;
14507 
14508 		if (test_bit(ATH12K_FLAG_RAW_MODE, &ar->ab->dev_flags))
14509 			is_raw_mode = true;
14510 
14511 		if (!ar->ab->hw_params->supports_monitor)
14512 			is_monitor_disable = true;
14513 
14514 		if (i == 0)
14515 			mac_addr = ar->mac_addr;
14516 		else
14517 			mac_addr = ab->mac_addr;
14518 
14519 		mbssid_max_interfaces += TARGET_NUM_VDEVS(ar->ab);
14520 	}
14521 
14522 	wiphy->available_antennas_rx = antennas_rx;
14523 	wiphy->available_antennas_tx = antennas_tx;
14524 
14525 	SET_IEEE80211_PERM_ADDR(hw, mac_addr);
14526 	SET_IEEE80211_DEV(hw, ab->dev);
14527 
14528 	ret = ath12k_mac_setup_iface_combinations(ah);
14529 	if (ret) {
14530 		ath12k_err(ab, "failed to setup interface combinations: %d\n", ret);
14531 		goto err_complete_cleanup_unregister;
14532 	}
14533 
14534 	wiphy->interface_modes = ath12k_mac_get_ifmodes(ah);
14535 
14536 	if (ah->num_radio == 1 &&
14537 	    wiphy->bands[NL80211_BAND_2GHZ] &&
14538 	    wiphy->bands[NL80211_BAND_5GHZ] &&
14539 	    wiphy->bands[NL80211_BAND_6GHZ])
14540 		ieee80211_hw_set(hw, SINGLE_SCAN_ON_ALL_BANDS);
14541 
14542 	ieee80211_hw_set(hw, SIGNAL_DBM);
14543 	ieee80211_hw_set(hw, SUPPORTS_PS);
14544 	ieee80211_hw_set(hw, SUPPORTS_DYNAMIC_PS);
14545 	ieee80211_hw_set(hw, MFP_CAPABLE);
14546 	ieee80211_hw_set(hw, REPORTS_TX_ACK_STATUS);
14547 	ieee80211_hw_set(hw, HAS_RATE_CONTROL);
14548 	ieee80211_hw_set(hw, AP_LINK_PS);
14549 	ieee80211_hw_set(hw, SPECTRUM_MGMT);
14550 	ieee80211_hw_set(hw, CONNECTION_MONITOR);
14551 	ieee80211_hw_set(hw, SUPPORTS_PER_STA_GTK);
14552 	ieee80211_hw_set(hw, CHANCTX_STA_CSA);
14553 	ieee80211_hw_set(hw, QUEUE_CONTROL);
14554 	ieee80211_hw_set(hw, SUPPORTS_TX_FRAG);
14555 	ieee80211_hw_set(hw, REPORTS_LOW_ACK);
14556 	ieee80211_hw_set(hw, NO_VIRTUAL_MONITOR);
14557 
14558 	if (test_bit(WMI_TLV_SERVICE_ETH_OFFLOAD, ar->wmi->wmi_ab->svc_map)) {
14559 		ieee80211_hw_set(hw, SUPPORTS_TX_ENCAP_OFFLOAD);
14560 		ieee80211_hw_set(hw, SUPPORTS_RX_DECAP_OFFLOAD);
14561 	}
14562 
14563 	if (cap->nss_ratio_enabled)
14564 		ieee80211_hw_set(hw, SUPPORTS_VHT_EXT_NSS_BW);
14565 
14566 	if ((ht_cap & WMI_HT_CAP_ENABLED) || is_6ghz) {
14567 		ieee80211_hw_set(hw, AMPDU_AGGREGATION);
14568 		ieee80211_hw_set(hw, TX_AMPDU_SETUP_IN_HW);
14569 		ieee80211_hw_set(hw, SUPPORTS_REORDERING_BUFFER);
14570 		ieee80211_hw_set(hw, SUPPORTS_AMSDU_IN_AMPDU);
14571 		ieee80211_hw_set(hw, USES_RSS);
14572 	}
14573 
14574 	wiphy->features |= NL80211_FEATURE_STATIC_SMPS;
14575 	wiphy->flags |= WIPHY_FLAG_IBSS_RSN;
14576 
14577 	/* TODO: Check if HT capability advertised from firmware is different
14578 	 * for each band for a dual band capable radio. It will be tricky to
14579 	 * handle it when the ht capability different for each band.
14580 	 */
14581 	if (ht_cap & WMI_HT_CAP_DYNAMIC_SMPS ||
14582 	    (is_6ghz && ab->hw_params->supports_dynamic_smps_6ghz))
14583 		wiphy->features |= NL80211_FEATURE_DYNAMIC_SMPS;
14584 
14585 	wiphy->max_scan_ssids = WLAN_SCAN_PARAMS_MAX_SSID;
14586 	wiphy->max_scan_ie_len = WLAN_SCAN_PARAMS_MAX_IE_LEN;
14587 
14588 	hw->max_listen_interval = ATH12K_MAX_HW_LISTEN_INTERVAL;
14589 
14590 	wiphy->flags |= WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL;
14591 	wiphy->flags |= WIPHY_FLAG_HAS_CHANNEL_SWITCH;
14592 	wiphy->max_remain_on_channel_duration = 5000;
14593 
14594 	wiphy->flags |= WIPHY_FLAG_AP_UAPSD;
14595 	wiphy->features |= NL80211_FEATURE_AP_MODE_CHAN_WIDTH_CHANGE |
14596 				   NL80211_FEATURE_AP_SCAN;
14597 
14598 	wiphy->features |= NL80211_FEATURE_TX_POWER_INSERTION;
14599 
14600 	/* MLO is not yet supported so disable Wireless Extensions for now
14601 	 * to make sure ath12k users don't use it. This flag can be removed
14602 	 * once WIPHY_FLAG_SUPPORTS_MLO is enabled.
14603 	 */
14604 	wiphy->flags |= WIPHY_FLAG_DISABLE_WEXT;
14605 
14606 	/* Copy over MLO related capabilities received from
14607 	 * WMI_SERVICE_READY_EXT2_EVENT if single_chip_mlo_supp is set.
14608 	 */
14609 	if (ab->ag->mlo_capable) {
14610 		ath12k_iftypes_ext_capa[2].eml_capabilities = cap->eml_cap;
14611 		ath12k_iftypes_ext_capa[2].mld_capa_and_ops = cap->mld_cap;
14612 		wiphy->flags |= WIPHY_FLAG_SUPPORTS_MLO;
14613 
14614 		ieee80211_hw_set(hw, MLO_MCAST_MULTI_LINK_TX);
14615 	}
14616 
14617 	hw->queues = ATH12K_HW_MAX_QUEUES;
14618 	wiphy->tx_queue_len = ATH12K_QUEUE_LEN;
14619 	hw->offchannel_tx_hw_queue = ATH12K_HW_MAX_QUEUES - 1;
14620 	hw->max_rx_aggregation_subframes = IEEE80211_MAX_AMPDU_BUF_EHT;
14621 
14622 	hw->vif_data_size = sizeof(struct ath12k_vif);
14623 	hw->sta_data_size = sizeof(struct ath12k_sta);
14624 	hw->extra_tx_headroom = ab->hw_params->iova_mask;
14625 
14626 	wiphy_ext_feature_set(wiphy, NL80211_EXT_FEATURE_CQM_RSSI_LIST);
14627 	wiphy_ext_feature_set(wiphy, NL80211_EXT_FEATURE_STA_TX_PWR);
14628 	wiphy_ext_feature_set(wiphy, NL80211_EXT_FEATURE_ACK_SIGNAL_SUPPORT);
14629 	if (test_bit(WMI_TLV_SERVICE_BSS_COLOR_OFFLOAD,
14630 		     ab->wmi_ab.svc_map)) {
14631 		wiphy_ext_feature_set(wiphy, NL80211_EXT_FEATURE_BSS_COLOR);
14632 		ieee80211_hw_set(hw, DETECTS_COLOR_COLLISION);
14633 	}
14634 
14635 	wiphy->cipher_suites = cipher_suites;
14636 	wiphy->n_cipher_suites = ARRAY_SIZE(cipher_suites);
14637 
14638 	wiphy->iftype_ext_capab = ath12k_iftypes_ext_capa;
14639 	wiphy->num_iftype_ext_capab = ARRAY_SIZE(ath12k_iftypes_ext_capa);
14640 
14641 	wiphy->mbssid_max_interfaces = mbssid_max_interfaces;
14642 	wiphy->ema_max_profile_periodicity = TARGET_EMA_MAX_PROFILE_PERIOD;
14643 	ieee80211_hw_set(hw, SUPPORTS_MULTI_BSSID);
14644 
14645 	if (is_6ghz) {
14646 		wiphy_ext_feature_set(wiphy,
14647 				      NL80211_EXT_FEATURE_FILS_DISCOVERY);
14648 		wiphy_ext_feature_set(wiphy,
14649 				      NL80211_EXT_FEATURE_UNSOL_BCAST_PROBE_RESP);
14650 	}
14651 
14652 	wiphy_ext_feature_set(wiphy, NL80211_EXT_FEATURE_PUNCT);
14653 	if (test_bit(WMI_TLV_SERVICE_BEACON_PROTECTION_SUPPORT, ab->wmi_ab.svc_map))
14654 		wiphy_ext_feature_set(hw->wiphy, NL80211_EXT_FEATURE_BEACON_PROTECTION);
14655 
14656 	ath12k_reg_init(hw);
14657 
14658 	if (!is_raw_mode) {
14659 		hw->netdev_features = NETIF_F_HW_CSUM;
14660 		ieee80211_hw_set(hw, SW_CRYPTO_CONTROL);
14661 		ieee80211_hw_set(hw, SUPPORT_FAST_XMIT);
14662 	}
14663 
14664 	if (test_bit(WMI_TLV_SERVICE_NLO, ar->wmi->wmi_ab->svc_map)) {
14665 		wiphy->max_sched_scan_ssids = WMI_PNO_MAX_SUPP_NETWORKS;
14666 		wiphy->max_match_sets = WMI_PNO_MAX_SUPP_NETWORKS;
14667 		wiphy->max_sched_scan_ie_len = WMI_PNO_MAX_IE_LENGTH;
14668 		wiphy->max_sched_scan_plans = WMI_PNO_MAX_SCHED_SCAN_PLANS;
14669 		wiphy->max_sched_scan_plan_interval =
14670 					WMI_PNO_MAX_SCHED_SCAN_PLAN_INT;
14671 		wiphy->max_sched_scan_plan_iterations =
14672 					WMI_PNO_MAX_SCHED_SCAN_PLAN_ITRNS;
14673 		wiphy->features |= NL80211_FEATURE_ND_RANDOM_MAC_ADDR;
14674 	}
14675 
14676 	ret = ath12k_wow_init(ar);
14677 	if (ret) {
14678 		ath12k_warn(ar->ab, "failed to init wow: %d\n", ret);
14679 		goto err_cleanup_if_combs;
14680 	}
14681 
14682 	/* Boot-time regulatory updates have already been processed.
14683 	 * Mark them as complete now, because after registration,
14684 	 * cfg80211 will notify us again if there are any pending hints.
14685 	 * We need to wait for those hints to be processed, so it's
14686 	 * important to mark the boot-time updates as complete before
14687 	 * proceeding with registration.
14688 	 */
14689 	for_each_ar(ah, ar, i)
14690 		complete_all(&ar->regd_update_completed);
14691 
14692 	ret = ieee80211_register_hw(hw);
14693 	if (ret) {
14694 		ath12k_err(ab, "ieee80211 registration failed: %d\n", ret);
14695 		goto err_cleanup_if_combs;
14696 	}
14697 
14698 	if (is_monitor_disable)
14699 		/* There's a race between calling ieee80211_register_hw()
14700 		 * and here where the monitor mode is enabled for a little
14701 		 * while. But that time is so short and in practice it doesn't make
14702 		 * a difference in real life.
14703 		 */
14704 		wiphy->interface_modes &= ~BIT(NL80211_IFTYPE_MONITOR);
14705 
14706 	for_each_ar(ah, ar, i) {
14707 		/* Apply the regd received during initialization */
14708 		ret = ath12k_regd_update(ar, true);
14709 		if (ret) {
14710 			ath12k_err(ar->ab, "ath12k regd update failed: %d\n", ret);
14711 			goto err_unregister_hw;
14712 		}
14713 
14714 		if (ar->ab->hw_params->current_cc_support && ab->new_alpha2[0]) {
14715 			struct wmi_set_current_country_arg current_cc = {};
14716 
14717 			memcpy(&current_cc.alpha2, ab->new_alpha2, 2);
14718 			memcpy(&ar->alpha2, ab->new_alpha2, 2);
14719 
14720 			reinit_completion(&ar->regd_update_completed);
14721 
14722 			ret = ath12k_wmi_send_set_current_country_cmd(ar, &current_cc);
14723 			if (ret)
14724 				ath12k_warn(ar->ab,
14725 					    "failed set cc code for mac register: %d\n",
14726 					    ret);
14727 		}
14728 
14729 		ath12k_fw_stats_init(ar);
14730 		ath12k_debugfs_register(ar);
14731 	}
14732 
14733 	return 0;
14734 
14735 err_unregister_hw:
14736 	for_each_ar(ah, ar, i)
14737 		ath12k_debugfs_unregister(ar);
14738 
14739 	ieee80211_unregister_hw(hw);
14740 
14741 err_cleanup_if_combs:
14742 	ath12k_mac_cleanup_iface_combinations(ah);
14743 
14744 err_complete_cleanup_unregister:
14745 	i = ah->num_radio;
14746 
14747 err_cleanup_unregister:
14748 	for (j = 0; j < i; j++) {
14749 		ar = ath12k_ah_to_ar(ah, j);
14750 		ath12k_mac_cleanup_unregister(ar);
14751 	}
14752 
14753 	SET_IEEE80211_DEV(hw, NULL);
14754 
14755 	return ret;
14756 }
14757 
14758 static void ath12k_mac_setup(struct ath12k *ar)
14759 {
14760 	struct ath12k_base *ab = ar->ab;
14761 	struct ath12k_pdev *pdev = ar->pdev;
14762 	u8 pdev_idx = ar->pdev_idx;
14763 
14764 	ar->lmac_id = ath12k_hw_get_mac_from_pdev_id(ab->hw_params, pdev_idx);
14765 
14766 	ar->wmi = &ab->wmi_ab.wmi[pdev_idx];
14767 	/* FIXME: wmi[0] is already initialized during attach,
14768 	 * Should we do this again?
14769 	 */
14770 	ath12k_wmi_pdev_attach(ab, pdev_idx);
14771 
14772 	ar->cfg_tx_chainmask = pdev->cap.tx_chain_mask;
14773 	ar->cfg_rx_chainmask = pdev->cap.rx_chain_mask;
14774 	ar->num_tx_chains = hweight32(pdev->cap.tx_chain_mask);
14775 	ar->num_rx_chains = hweight32(pdev->cap.rx_chain_mask);
14776 	ar->scan.arvif = NULL;
14777 	ar->vdev_id_11d_scan = ATH12K_11D_INVALID_VDEV_ID;
14778 
14779 	spin_lock_init(&ar->data_lock);
14780 	spin_lock_init(&ar->dp.ppdu_list_lock);
14781 	INIT_LIST_HEAD(&ar->arvifs);
14782 	INIT_LIST_HEAD(&ar->dp.ppdu_stats_info);
14783 
14784 	init_completion(&ar->vdev_setup_done);
14785 	init_completion(&ar->vdev_delete_done);
14786 	init_completion(&ar->peer_assoc_done);
14787 	init_completion(&ar->peer_delete_done);
14788 	init_completion(&ar->install_key_done);
14789 	init_completion(&ar->bss_survey_done);
14790 	init_completion(&ar->scan.started);
14791 	init_completion(&ar->scan.completed);
14792 	init_completion(&ar->scan.on_channel);
14793 	init_completion(&ar->mlo_setup_done);
14794 	init_completion(&ar->completed_11d_scan);
14795 	init_completion(&ar->regd_update_completed);
14796 
14797 	INIT_DELAYED_WORK(&ar->scan.timeout, ath12k_scan_timeout_work);
14798 	wiphy_work_init(&ar->scan.vdev_clean_wk, ath12k_scan_vdev_clean_work);
14799 	INIT_WORK(&ar->regd_channel_update_work, ath12k_regd_update_chan_list_work);
14800 	INIT_LIST_HEAD(&ar->regd_channel_update_queue);
14801 	INIT_WORK(&ar->regd_update_work, ath12k_regd_update_work);
14802 
14803 	wiphy_work_init(&ar->wmi_mgmt_tx_work, ath12k_mgmt_over_wmi_tx_work);
14804 	skb_queue_head_init(&ar->wmi_mgmt_tx_queue);
14805 
14806 	ar->monitor_vdev_id = -1;
14807 	ar->monitor_vdev_created = false;
14808 	ar->monitor_started = false;
14809 }
14810 
14811 static int __ath12k_mac_mlo_setup(struct ath12k *ar)
14812 {
14813 	u8 num_link = 0, partner_link_id[ATH12K_GROUP_MAX_RADIO] = {};
14814 	struct ath12k_base *partner_ab, *ab = ar->ab;
14815 	struct ath12k_hw_group *ag = ab->ag;
14816 	struct wmi_mlo_setup_arg mlo = {};
14817 	struct ath12k_pdev *pdev;
14818 	unsigned long time_left;
14819 	int i, j, ret;
14820 
14821 	lockdep_assert_held(&ag->mutex);
14822 
14823 	reinit_completion(&ar->mlo_setup_done);
14824 
14825 	for (i = 0; i < ag->num_devices; i++) {
14826 		partner_ab = ag->ab[i];
14827 
14828 		for (j = 0; j < partner_ab->num_radios; j++) {
14829 			pdev = &partner_ab->pdevs[j];
14830 
14831 			/* Avoid the self link */
14832 			if (ar == pdev->ar)
14833 				continue;
14834 
14835 			partner_link_id[num_link] = pdev->hw_link_id;
14836 			num_link++;
14837 
14838 			ath12k_dbg(ab, ATH12K_DBG_MAC, "device %d pdev %d hw_link_id %d num_link %d\n",
14839 				   i, j, pdev->hw_link_id, num_link);
14840 		}
14841 	}
14842 
14843 	if (num_link == 0)
14844 		return 0;
14845 
14846 	mlo.group_id = cpu_to_le32(ag->id);
14847 	mlo.partner_link_id = partner_link_id;
14848 	mlo.num_partner_links = num_link;
14849 	ar->mlo_setup_status = 0;
14850 
14851 	ath12k_dbg(ab, ATH12K_DBG_MAC, "group id %d num_link %d\n", ag->id, num_link);
14852 
14853 	ret = ath12k_wmi_mlo_setup(ar, &mlo);
14854 	if (ret) {
14855 		ath12k_err(ab, "failed to send  setup MLO WMI command for pdev %d: %d\n",
14856 			   ar->pdev_idx, ret);
14857 		return ret;
14858 	}
14859 
14860 	time_left = wait_for_completion_timeout(&ar->mlo_setup_done,
14861 						WMI_MLO_CMD_TIMEOUT_HZ);
14862 
14863 	if (!time_left || ar->mlo_setup_status)
14864 		return ar->mlo_setup_status ? : -ETIMEDOUT;
14865 
14866 	ath12k_dbg(ab, ATH12K_DBG_MAC, "mlo setup done for pdev %d\n", ar->pdev_idx);
14867 
14868 	return 0;
14869 }
14870 
14871 static int __ath12k_mac_mlo_teardown(struct ath12k *ar)
14872 {
14873 	struct ath12k_base *ab = ar->ab;
14874 	int ret;
14875 	u8 num_link;
14876 
14877 	if (test_bit(ATH12K_FLAG_RECOVERY, &ab->dev_flags))
14878 		return 0;
14879 
14880 	num_link = ath12k_get_num_partner_link(ar);
14881 
14882 	if (num_link == 0)
14883 		return 0;
14884 
14885 	ret = ath12k_wmi_mlo_teardown(ar);
14886 	if (ret) {
14887 		ath12k_warn(ab, "failed to send MLO teardown WMI command for pdev %d: %d\n",
14888 			    ar->pdev_idx, ret);
14889 		return ret;
14890 	}
14891 
14892 	ath12k_dbg(ab, ATH12K_DBG_MAC, "mlo teardown for pdev %d\n", ar->pdev_idx);
14893 
14894 	return 0;
14895 }
14896 
14897 int ath12k_mac_mlo_setup(struct ath12k_hw_group *ag)
14898 {
14899 	struct ath12k_hw *ah;
14900 	struct ath12k *ar;
14901 	int ret;
14902 	int i, j;
14903 
14904 	for (i = 0; i < ag->num_hw; i++) {
14905 		ah = ag->ah[i];
14906 		if (!ah)
14907 			continue;
14908 
14909 		for_each_ar(ah, ar, j) {
14910 			ar = &ah->radio[j];
14911 			ret = __ath12k_mac_mlo_setup(ar);
14912 			if (ret) {
14913 				ath12k_err(ar->ab, "failed to setup MLO: %d\n", ret);
14914 				goto err_setup;
14915 			}
14916 		}
14917 	}
14918 
14919 	return 0;
14920 
14921 err_setup:
14922 	for (i = i - 1; i >= 0; i--) {
14923 		ah = ag->ah[i];
14924 		if (!ah)
14925 			continue;
14926 
14927 		for (j = j - 1; j >= 0; j--) {
14928 			ar = &ah->radio[j];
14929 			if (!ar)
14930 				continue;
14931 
14932 			__ath12k_mac_mlo_teardown(ar);
14933 		}
14934 	}
14935 
14936 	return ret;
14937 }
14938 
14939 void ath12k_mac_mlo_teardown(struct ath12k_hw_group *ag)
14940 {
14941 	struct ath12k_hw *ah;
14942 	struct ath12k *ar;
14943 	int ret, i, j;
14944 
14945 	for (i = 0; i < ag->num_hw; i++) {
14946 		ah = ag->ah[i];
14947 		if (!ah)
14948 			continue;
14949 
14950 		for_each_ar(ah, ar, j) {
14951 			ar = &ah->radio[j];
14952 			ret = __ath12k_mac_mlo_teardown(ar);
14953 			if (ret) {
14954 				ath12k_err(ar->ab, "failed to teardown MLO: %d\n", ret);
14955 				break;
14956 			}
14957 		}
14958 	}
14959 }
14960 
14961 int ath12k_mac_register(struct ath12k_hw_group *ag)
14962 {
14963 	struct ath12k_hw *ah;
14964 	int i;
14965 	int ret;
14966 
14967 	for (i = 0; i < ag->num_hw; i++) {
14968 		ah = ath12k_ag_to_ah(ag, i);
14969 
14970 		ret = ath12k_mac_hw_register(ah);
14971 		if (ret)
14972 			goto err;
14973 	}
14974 
14975 	return 0;
14976 
14977 err:
14978 	for (i = i - 1; i >= 0; i--) {
14979 		ah = ath12k_ag_to_ah(ag, i);
14980 		if (!ah)
14981 			continue;
14982 
14983 		ath12k_mac_hw_unregister(ah);
14984 	}
14985 
14986 	return ret;
14987 }
14988 
14989 void ath12k_mac_unregister(struct ath12k_hw_group *ag)
14990 {
14991 	struct ath12k_hw *ah;
14992 	int i;
14993 
14994 	for (i = ag->num_hw - 1; i >= 0; i--) {
14995 		ah = ath12k_ag_to_ah(ag, i);
14996 		if (!ah)
14997 			continue;
14998 
14999 		ath12k_mac_hw_unregister(ah);
15000 	}
15001 }
15002 
15003 static void ath12k_mac_hw_destroy(struct ath12k_hw *ah)
15004 {
15005 	ieee80211_free_hw(ah->hw);
15006 }
15007 
15008 static struct ath12k_hw *ath12k_mac_hw_allocate(struct ath12k_hw_group *ag,
15009 						struct ath12k_pdev_map *pdev_map,
15010 						u8 num_pdev_map)
15011 {
15012 	struct ieee80211_hw *hw;
15013 	struct ath12k *ar;
15014 	struct ath12k_base *ab;
15015 	struct ath12k_pdev *pdev;
15016 	struct ath12k_hw *ah;
15017 	int i;
15018 	u8 pdev_idx;
15019 
15020 	hw = ieee80211_alloc_hw(struct_size(ah, radio, num_pdev_map),
15021 				pdev_map->ab->ath12k_ops);
15022 	if (!hw)
15023 		return NULL;
15024 
15025 	ah = ath12k_hw_to_ah(hw);
15026 	ah->hw = hw;
15027 	ah->num_radio = num_pdev_map;
15028 
15029 	mutex_init(&ah->hw_mutex);
15030 
15031 	spin_lock_init(&ah->dp_hw.peer_lock);
15032 	INIT_LIST_HEAD(&ah->dp_hw.dp_peers_list);
15033 
15034 	for (i = 0; i < num_pdev_map; i++) {
15035 		ab = pdev_map[i].ab;
15036 		pdev_idx = pdev_map[i].pdev_idx;
15037 		pdev = &ab->pdevs[pdev_idx];
15038 
15039 		ar = ath12k_ah_to_ar(ah, i);
15040 		ar->ah = ah;
15041 		ar->ab = ab;
15042 		ar->hw_link_id = pdev->hw_link_id;
15043 		ar->pdev = pdev;
15044 		ar->pdev_idx = pdev_idx;
15045 		pdev->ar = ar;
15046 
15047 		ag->hw_links[ar->hw_link_id].device_id = ab->device_id;
15048 		ag->hw_links[ar->hw_link_id].pdev_idx = pdev_idx;
15049 
15050 		ath12k_mac_setup(ar);
15051 		ath12k_dp_pdev_pre_alloc(ar);
15052 	}
15053 
15054 	return ah;
15055 }
15056 
15057 void ath12k_mac_destroy(struct ath12k_hw_group *ag)
15058 {
15059 	struct ath12k_pdev *pdev;
15060 	struct ath12k_base *ab = ag->ab[0];
15061 	int i, j;
15062 	struct ath12k_hw *ah;
15063 
15064 	for (i = 0; i < ag->num_devices; i++) {
15065 		ab = ag->ab[i];
15066 		if (!ab)
15067 			continue;
15068 
15069 		for (j = 0; j < ab->num_radios; j++) {
15070 			pdev = &ab->pdevs[j];
15071 			if (!pdev->ar)
15072 				continue;
15073 			pdev->ar = NULL;
15074 		}
15075 	}
15076 
15077 	for (i = 0; i < ag->num_hw; i++) {
15078 		ah = ath12k_ag_to_ah(ag, i);
15079 		if (!ah)
15080 			continue;
15081 
15082 		ath12k_mac_hw_destroy(ah);
15083 		ath12k_ag_set_ah(ag, i, NULL);
15084 	}
15085 }
15086 
15087 static void ath12k_mac_set_device_defaults(struct ath12k_base *ab)
15088 {
15089 	int total_vdev;
15090 
15091 	/* Initialize channel counters frequency value in hertz */
15092 	ab->cc_freq_hz = 320000;
15093 	total_vdev = ab->num_radios * TARGET_NUM_VDEVS(ab);
15094 	ab->free_vdev_map = (1LL << total_vdev) - 1;
15095 }
15096 
15097 int ath12k_mac_allocate(struct ath12k_hw_group *ag)
15098 {
15099 	struct ath12k_pdev_map pdev_map[ATH12K_GROUP_MAX_RADIO];
15100 	int mac_id, device_id, total_radio, num_hw;
15101 	struct ath12k_base *ab;
15102 	struct ath12k_hw *ah;
15103 	int ret, i, j;
15104 	u8 radio_per_hw;
15105 
15106 	total_radio = 0;
15107 	for (i = 0; i < ag->num_devices; i++) {
15108 		ab = ag->ab[i];
15109 		if (!ab)
15110 			continue;
15111 
15112 		ath12k_debugfs_pdev_create(ab);
15113 		ath12k_mac_set_device_defaults(ab);
15114 		total_radio += ab->num_radios;
15115 	}
15116 
15117 	if (!total_radio)
15118 		return -EINVAL;
15119 
15120 	if (WARN_ON(total_radio > ATH12K_GROUP_MAX_RADIO))
15121 		return -ENOSPC;
15122 
15123 	/* All pdev get combined and register as single wiphy based on
15124 	 * hardware group which participate in multi-link operation else
15125 	 * each pdev get register separately.
15126 	 */
15127 	if (ag->mlo_capable)
15128 		radio_per_hw = total_radio;
15129 	else
15130 		radio_per_hw = 1;
15131 
15132 	num_hw = total_radio / radio_per_hw;
15133 
15134 	ag->num_hw = 0;
15135 	device_id = 0;
15136 	mac_id = 0;
15137 	for (i = 0; i < num_hw; i++) {
15138 		for (j = 0; j < radio_per_hw; j++) {
15139 			if (device_id >= ag->num_devices || !ag->ab[device_id]) {
15140 				ret = -ENOSPC;
15141 				goto err;
15142 			}
15143 
15144 			ab = ag->ab[device_id];
15145 			pdev_map[j].ab = ab;
15146 			pdev_map[j].pdev_idx = mac_id;
15147 			mac_id++;
15148 
15149 			/* If mac_id falls beyond the current device MACs then
15150 			 * move to next device
15151 			 */
15152 			if (mac_id >= ab->num_radios) {
15153 				mac_id = 0;
15154 				device_id++;
15155 			}
15156 		}
15157 
15158 		ab = pdev_map->ab;
15159 
15160 		ah = ath12k_mac_hw_allocate(ag, pdev_map, radio_per_hw);
15161 		if (!ah) {
15162 			ath12k_warn(ab, "failed to allocate mac80211 hw device for hw_idx %d\n",
15163 				    i);
15164 			ret = -ENOMEM;
15165 			goto err;
15166 		}
15167 
15168 		ah->dev = ab->dev;
15169 
15170 		ag->ah[i] = ah;
15171 		ag->num_hw++;
15172 	}
15173 
15174 	return 0;
15175 
15176 err:
15177 	for (i = i - 1; i >= 0; i--) {
15178 		ah = ath12k_ag_to_ah(ag, i);
15179 		if (!ah)
15180 			continue;
15181 
15182 		ath12k_mac_hw_destroy(ah);
15183 		ath12k_ag_set_ah(ag, i, NULL);
15184 	}
15185 
15186 	return ret;
15187 }
15188 
15189 int ath12k_mac_vif_set_keepalive(struct ath12k_link_vif *arvif,
15190 				 enum wmi_sta_keepalive_method method,
15191 				 u32 interval)
15192 {
15193 	struct wmi_sta_keepalive_arg arg = {};
15194 	struct ath12k *ar = arvif->ar;
15195 	int ret;
15196 
15197 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
15198 
15199 	if (arvif->ahvif->vdev_type != WMI_VDEV_TYPE_STA)
15200 		return 0;
15201 
15202 	if (!test_bit(WMI_TLV_SERVICE_STA_KEEP_ALIVE, ar->ab->wmi_ab.svc_map))
15203 		return 0;
15204 
15205 	arg.vdev_id = arvif->vdev_id;
15206 	arg.enabled = 1;
15207 	arg.method = method;
15208 	arg.interval = interval;
15209 
15210 	ret = ath12k_wmi_sta_keepalive(ar, &arg);
15211 	if (ret) {
15212 		ath12k_warn(ar->ab, "failed to set keepalive on vdev %i: %d\n",
15213 			    arvif->vdev_id, ret);
15214 		return ret;
15215 	}
15216 
15217 	return 0;
15218 }
15219