xref: /linux/drivers/net/wireless/ath/ath12k/mac.c (revision 19dd0cc36a52d0d121d7854cbb9b00d6d4dcad0a)
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(sizeof(*peer_arg), GFP_KERNEL);
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(sizeof(*arvif), GFP_KERNEL);
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(sizeof(*ahvif->cache[0]), GFP_KERNEL);
5028 		if (ahvif->cache[link_id])
5029 			INIT_LIST_HEAD(&ahvif->cache[link_id]->key_conf.list);
5030 	}
5031 
5032 	return ahvif->cache[link_id];
5033 }
5034 
5035 static void ath12k_ahvif_put_link_key_cache(struct ath12k_vif_cache *cache)
5036 {
5037 	struct ath12k_key_conf *key_conf, *tmp;
5038 
5039 	if (!cache || list_empty(&cache->key_conf.list))
5040 		return;
5041 	list_for_each_entry_safe(key_conf, tmp, &cache->key_conf.list, list) {
5042 		list_del(&key_conf->list);
5043 		kfree(key_conf);
5044 	}
5045 }
5046 
5047 static void ath12k_ahvif_put_link_cache(struct ath12k_vif *ahvif, u8 link_id)
5048 {
5049 	if (link_id >= IEEE80211_MLD_MAX_NUM_LINKS)
5050 		return;
5051 
5052 	ath12k_ahvif_put_link_key_cache(ahvif->cache[link_id]);
5053 	kfree(ahvif->cache[link_id]);
5054 	ahvif->cache[link_id] = NULL;
5055 }
5056 
5057 void ath12k_mac_op_link_info_changed(struct ieee80211_hw *hw,
5058 				     struct ieee80211_vif *vif,
5059 				     struct ieee80211_bss_conf *info,
5060 				     u64 changed)
5061 {
5062 	struct ath12k *ar;
5063 	struct ath12k_vif *ahvif = ath12k_vif_to_ahvif(vif);
5064 	struct ath12k_vif_cache *cache;
5065 	struct ath12k_link_vif *arvif;
5066 	u8 link_id = info->link_id;
5067 
5068 	lockdep_assert_wiphy(hw->wiphy);
5069 
5070 	arvif = wiphy_dereference(hw->wiphy, ahvif->link[link_id]);
5071 
5072 	/* if the vdev is not created on a certain radio,
5073 	 * cache the info to be updated later on vdev creation
5074 	 */
5075 
5076 	if (!arvif || !arvif->is_created) {
5077 		cache = ath12k_ahvif_get_link_cache(ahvif, link_id);
5078 		if (!cache)
5079 			return;
5080 
5081 		cache->bss_conf_changed |= changed;
5082 
5083 		return;
5084 	}
5085 
5086 	ar = arvif->ar;
5087 
5088 	ath12k_mac_bss_info_changed(ar, arvif, info, changed);
5089 }
5090 EXPORT_SYMBOL(ath12k_mac_op_link_info_changed);
5091 
5092 static struct ath12k*
5093 ath12k_mac_select_scan_device(struct ieee80211_hw *hw,
5094 			      struct ieee80211_vif *vif,
5095 			      u32 center_freq)
5096 {
5097 	struct ath12k_hw *ah = hw->priv;
5098 	enum nl80211_band band;
5099 	struct ath12k *ar;
5100 	int i;
5101 
5102 	if (ah->num_radio == 1)
5103 		return ah->radio;
5104 
5105 	/* Currently mac80211 supports splitting scan requests into
5106 	 * multiple scan requests per band.
5107 	 * Loop through first channel and determine the scan radio
5108 	 * TODO: There could be 5 GHz low/high channels in that case
5109 	 * split the hw request and perform multiple scans
5110 	 */
5111 
5112 	if (center_freq < ATH12K_MIN_5GHZ_FREQ)
5113 		band = NL80211_BAND_2GHZ;
5114 	else if (center_freq < ATH12K_MIN_6GHZ_FREQ)
5115 		band = NL80211_BAND_5GHZ;
5116 	else
5117 		band = NL80211_BAND_6GHZ;
5118 
5119 	for_each_ar(ah, ar, i) {
5120 		if (ar->mac.sbands[band].channels &&
5121 		    center_freq >= KHZ_TO_MHZ(ar->freq_range.start_freq) &&
5122 		    center_freq <= KHZ_TO_MHZ(ar->freq_range.end_freq))
5123 			return ar;
5124 	}
5125 
5126 	return NULL;
5127 }
5128 
5129 void __ath12k_mac_scan_finish(struct ath12k *ar)
5130 {
5131 	struct ieee80211_hw *hw = ath12k_ar_to_hw(ar);
5132 
5133 	lockdep_assert_held(&ar->data_lock);
5134 
5135 	switch (ar->scan.state) {
5136 	case ATH12K_SCAN_IDLE:
5137 		break;
5138 	case ATH12K_SCAN_RUNNING:
5139 	case ATH12K_SCAN_ABORTING:
5140 		if (ar->scan.is_roc && ar->scan.roc_notify)
5141 			ieee80211_remain_on_channel_expired(hw);
5142 		fallthrough;
5143 	case ATH12K_SCAN_STARTING:
5144 		cancel_delayed_work(&ar->scan.timeout);
5145 		complete_all(&ar->scan.completed);
5146 		wiphy_work_queue(ar->ah->hw->wiphy, &ar->scan.vdev_clean_wk);
5147 		break;
5148 	}
5149 }
5150 
5151 void ath12k_mac_scan_finish(struct ath12k *ar)
5152 {
5153 	spin_lock_bh(&ar->data_lock);
5154 	__ath12k_mac_scan_finish(ar);
5155 	spin_unlock_bh(&ar->data_lock);
5156 }
5157 
5158 static int ath12k_scan_stop(struct ath12k *ar)
5159 {
5160 	struct ath12k_wmi_scan_cancel_arg arg = {
5161 		.req_type = WLAN_SCAN_CANCEL_SINGLE,
5162 		.scan_id = ATH12K_SCAN_ID,
5163 	};
5164 	int ret;
5165 
5166 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
5167 
5168 	/* TODO: Fill other STOP Params */
5169 	arg.pdev_id = ar->pdev->pdev_id;
5170 
5171 	ret = ath12k_wmi_send_scan_stop_cmd(ar, &arg);
5172 	if (ret) {
5173 		ath12k_warn(ar->ab, "failed to stop wmi scan: %d\n", ret);
5174 		goto out;
5175 	}
5176 
5177 	ret = wait_for_completion_timeout(&ar->scan.completed, 3 * HZ);
5178 	if (ret == 0) {
5179 		ath12k_warn(ar->ab,
5180 			    "failed to receive scan abort comple: timed out\n");
5181 		ret = -ETIMEDOUT;
5182 	} else if (ret > 0) {
5183 		ret = 0;
5184 	}
5185 
5186 out:
5187 	/* Scan state should be updated in scan completion worker but in
5188 	 * case firmware fails to deliver the event (for whatever reason)
5189 	 * it is desired to clean up scan state anyway. Firmware may have
5190 	 * just dropped the scan completion event delivery due to transport
5191 	 * pipe being overflown with data and/or it can recover on its own
5192 	 * before next scan request is submitted.
5193 	 */
5194 	spin_lock_bh(&ar->data_lock);
5195 	if (ret)
5196 		__ath12k_mac_scan_finish(ar);
5197 	spin_unlock_bh(&ar->data_lock);
5198 
5199 	return ret;
5200 }
5201 
5202 static void ath12k_scan_abort(struct ath12k *ar)
5203 {
5204 	int ret;
5205 
5206 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
5207 
5208 	spin_lock_bh(&ar->data_lock);
5209 
5210 	switch (ar->scan.state) {
5211 	case ATH12K_SCAN_IDLE:
5212 		/* This can happen if timeout worker kicked in and called
5213 		 * abortion while scan completion was being processed.
5214 		 */
5215 		break;
5216 	case ATH12K_SCAN_STARTING:
5217 	case ATH12K_SCAN_ABORTING:
5218 		ath12k_warn(ar->ab, "refusing scan abortion due to invalid scan state: %d\n",
5219 			    ar->scan.state);
5220 		break;
5221 	case ATH12K_SCAN_RUNNING:
5222 		ar->scan.state = ATH12K_SCAN_ABORTING;
5223 		spin_unlock_bh(&ar->data_lock);
5224 
5225 		ret = ath12k_scan_stop(ar);
5226 		if (ret)
5227 			ath12k_warn(ar->ab, "failed to abort scan: %d\n", ret);
5228 
5229 		spin_lock_bh(&ar->data_lock);
5230 		break;
5231 	}
5232 
5233 	spin_unlock_bh(&ar->data_lock);
5234 }
5235 
5236 static void ath12k_scan_timeout_work(struct work_struct *work)
5237 {
5238 	struct ath12k *ar = container_of(work, struct ath12k,
5239 					 scan.timeout.work);
5240 
5241 	wiphy_lock(ath12k_ar_to_hw(ar)->wiphy);
5242 	ath12k_scan_abort(ar);
5243 	wiphy_unlock(ath12k_ar_to_hw(ar)->wiphy);
5244 }
5245 
5246 static void ath12k_mac_scan_send_complete(struct ath12k *ar,
5247 					  struct cfg80211_scan_info *info)
5248 {
5249 	struct ath12k_hw *ah = ar->ah;
5250 	struct ath12k *partner_ar;
5251 	int i;
5252 
5253 	lockdep_assert_wiphy(ah->hw->wiphy);
5254 
5255 	for_each_ar(ah, partner_ar, i)
5256 		if (partner_ar != ar &&
5257 		    partner_ar->scan.state == ATH12K_SCAN_RUNNING)
5258 			return;
5259 
5260 	ieee80211_scan_completed(ah->hw, info);
5261 }
5262 
5263 static void ath12k_scan_vdev_clean_work(struct wiphy *wiphy, struct wiphy_work *work)
5264 {
5265 	struct ath12k *ar = container_of(work, struct ath12k,
5266 					 scan.vdev_clean_wk);
5267 	struct ath12k_hw *ah = ar->ah;
5268 	struct ath12k_link_vif *arvif;
5269 
5270 	lockdep_assert_wiphy(wiphy);
5271 
5272 	arvif = ar->scan.arvif;
5273 
5274 	/* The scan vdev has already been deleted. This can occur when a
5275 	 * new scan request is made on the same vif with a different
5276 	 * frequency, causing the scan arvif to move from one radio to
5277 	 * another. Or, scan was abrupted and via remove interface, the
5278 	 * arvif is already deleted. Alternatively, if the scan vdev is not
5279 	 * being used as an actual vdev, then do not delete it.
5280 	 */
5281 	if (!arvif || arvif->is_started)
5282 		goto work_complete;
5283 
5284 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac clean scan vdev (link id %u)",
5285 		   arvif->link_id);
5286 
5287 	ath12k_mac_remove_link_interface(ah->hw, arvif);
5288 	ath12k_mac_unassign_link_vif(arvif);
5289 
5290 work_complete:
5291 	spin_lock_bh(&ar->data_lock);
5292 	ar->scan.arvif = NULL;
5293 	if (!ar->scan.is_roc) {
5294 		struct cfg80211_scan_info info = {
5295 			.aborted = ((ar->scan.state ==
5296 				    ATH12K_SCAN_ABORTING) ||
5297 				    (ar->scan.state ==
5298 				    ATH12K_SCAN_STARTING)),
5299 		};
5300 
5301 		ath12k_mac_scan_send_complete(ar, &info);
5302 	}
5303 
5304 	ar->scan.state = ATH12K_SCAN_IDLE;
5305 	ar->scan_channel = NULL;
5306 	ar->scan.roc_freq = 0;
5307 	spin_unlock_bh(&ar->data_lock);
5308 }
5309 
5310 static int ath12k_start_scan(struct ath12k *ar,
5311 			     struct ath12k_wmi_scan_req_arg *arg)
5312 {
5313 	int ret;
5314 
5315 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
5316 
5317 	ret = ath12k_wmi_send_scan_start_cmd(ar, arg);
5318 	if (ret)
5319 		return ret;
5320 
5321 	ret = wait_for_completion_timeout(&ar->scan.started, 1 * HZ);
5322 	if (ret == 0) {
5323 		ret = ath12k_scan_stop(ar);
5324 		if (ret)
5325 			ath12k_warn(ar->ab, "failed to stop scan: %d\n", ret);
5326 
5327 		return -ETIMEDOUT;
5328 	}
5329 
5330 	/* If we failed to start the scan, return error code at
5331 	 * this point.  This is probably due to some issue in the
5332 	 * firmware, but no need to wedge the driver due to that...
5333 	 */
5334 	spin_lock_bh(&ar->data_lock);
5335 	if (ar->scan.state == ATH12K_SCAN_IDLE) {
5336 		spin_unlock_bh(&ar->data_lock);
5337 		return -EINVAL;
5338 	}
5339 	spin_unlock_bh(&ar->data_lock);
5340 
5341 	return 0;
5342 }
5343 
5344 int ath12k_mac_get_fw_stats(struct ath12k *ar,
5345 			    struct ath12k_fw_stats_req_params *param)
5346 {
5347 	struct ath12k_base *ab = ar->ab;
5348 	struct ath12k_hw *ah = ath12k_ar_to_ah(ar);
5349 	unsigned long time_left;
5350 	int ret;
5351 
5352 	guard(mutex)(&ah->hw_mutex);
5353 
5354 	if (ah->state != ATH12K_HW_STATE_ON)
5355 		return -ENETDOWN;
5356 
5357 	reinit_completion(&ar->fw_stats_complete);
5358 	reinit_completion(&ar->fw_stats_done);
5359 
5360 	ret = ath12k_wmi_send_stats_request_cmd(ar, param->stats_id,
5361 						param->vdev_id, param->pdev_id);
5362 	if (ret) {
5363 		ath12k_warn(ab, "failed to request fw stats: %d\n", ret);
5364 		return ret;
5365 	}
5366 
5367 	ath12k_dbg(ab, ATH12K_DBG_WMI,
5368 		   "get fw stat pdev id %d vdev id %d stats id 0x%x\n",
5369 		   param->pdev_id, param->vdev_id, param->stats_id);
5370 
5371 	time_left = wait_for_completion_timeout(&ar->fw_stats_complete, 1 * HZ);
5372 	if (!time_left) {
5373 		ath12k_warn(ab, "time out while waiting for get fw stats\n");
5374 		return -ETIMEDOUT;
5375 	}
5376 
5377 	/* Firmware sends WMI_UPDATE_STATS_EVENTID back-to-back
5378 	 * when stats data buffer limit is reached. fw_stats_complete
5379 	 * is completed once host receives first event from firmware, but
5380 	 * still there could be more events following. Below is to wait
5381 	 * until firmware completes sending all the events.
5382 	 */
5383 	time_left = wait_for_completion_timeout(&ar->fw_stats_done, 3 * HZ);
5384 	if (!time_left) {
5385 		ath12k_warn(ab, "time out while waiting for fw stats done\n");
5386 		return -ETIMEDOUT;
5387 	}
5388 
5389 	return 0;
5390 }
5391 
5392 int ath12k_mac_op_get_txpower(struct ieee80211_hw *hw,
5393 			      struct ieee80211_vif *vif,
5394 			      unsigned int link_id,
5395 			      int *dbm)
5396 {
5397 	struct ath12k_vif *ahvif = ath12k_vif_to_ahvif(vif);
5398 	struct ath12k_fw_stats_req_params params = {};
5399 	struct ath12k_fw_stats_pdev *pdev;
5400 	struct ath12k_hw *ah = hw->priv;
5401 	struct ath12k_link_vif *arvif;
5402 	struct ath12k_base *ab;
5403 	struct ath12k *ar;
5404 	int ret;
5405 
5406 	/* Final Tx power is minimum of Target Power, CTL power, Regulatory
5407 	 * Power, PSD EIRP Power. We just know the Regulatory power from the
5408 	 * regulatory rules obtained. FW knows all these power and sets the min
5409 	 * of these. Hence, we request the FW pdev stats in which FW reports
5410 	 * the minimum of all vdev's channel Tx power.
5411 	 */
5412 	lockdep_assert_wiphy(hw->wiphy);
5413 
5414 	arvif = wiphy_dereference(ah->hw->wiphy, ahvif->link[link_id]);
5415 	if (!arvif || !arvif->ar)
5416 		return -EINVAL;
5417 
5418 	ar = arvif->ar;
5419 	ab = ar->ab;
5420 	if (ah->state != ATH12K_HW_STATE_ON)
5421 		goto err_fallback;
5422 
5423 	if (test_bit(ATH12K_FLAG_CAC_RUNNING, &ar->dev_flags))
5424 		return -EAGAIN;
5425 
5426 	/* Limit the requests to Firmware for fetching the tx power */
5427 	if (ar->chan_tx_pwr != ATH12K_PDEV_TX_POWER_INVALID &&
5428 	    time_before(jiffies,
5429 			msecs_to_jiffies(ATH12K_PDEV_TX_POWER_REFRESH_TIME_MSECS) +
5430 					 ar->last_tx_power_update))
5431 		goto send_tx_power;
5432 
5433 	params.pdev_id = ar->pdev->pdev_id;
5434 	params.vdev_id = arvif->vdev_id;
5435 	params.stats_id = WMI_REQUEST_PDEV_STAT;
5436 	ret = ath12k_mac_get_fw_stats(ar, &params);
5437 	if (ret) {
5438 		ath12k_warn(ab, "failed to request fw pdev stats: %d\n", ret);
5439 		goto err_fallback;
5440 	}
5441 
5442 	spin_lock_bh(&ar->data_lock);
5443 	pdev = list_first_entry_or_null(&ar->fw_stats.pdevs,
5444 					struct ath12k_fw_stats_pdev, list);
5445 	if (!pdev) {
5446 		spin_unlock_bh(&ar->data_lock);
5447 		goto err_fallback;
5448 	}
5449 
5450 	/* tx power reported by firmware is in units of 0.5 dBm */
5451 	ar->chan_tx_pwr = pdev->chan_tx_power / 2;
5452 	spin_unlock_bh(&ar->data_lock);
5453 	ar->last_tx_power_update = jiffies;
5454 	ath12k_fw_stats_reset(ar);
5455 
5456 send_tx_power:
5457 	*dbm = ar->chan_tx_pwr;
5458 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "txpower fetched from firmware %d dBm\n",
5459 		   *dbm);
5460 	return 0;
5461 
5462 err_fallback:
5463 	/* We didn't get txpower from FW. Hence, relying on vif->bss_conf.txpower */
5464 	*dbm = vif->bss_conf.txpower;
5465 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "txpower from firmware NaN, reported %d dBm\n",
5466 		   *dbm);
5467 	return 0;
5468 }
5469 EXPORT_SYMBOL(ath12k_mac_op_get_txpower);
5470 
5471 static u8
5472 ath12k_mac_find_link_id_by_ar(struct ath12k_vif *ahvif, struct ath12k *ar)
5473 {
5474 	struct ath12k_link_vif *arvif;
5475 	struct ath12k_hw *ah = ahvif->ah;
5476 	unsigned long links = ahvif->links_map;
5477 	unsigned long scan_links_map;
5478 	u8 link_id;
5479 
5480 	lockdep_assert_wiphy(ah->hw->wiphy);
5481 
5482 	for_each_set_bit(link_id, &links, ATH12K_NUM_MAX_LINKS) {
5483 		arvif = wiphy_dereference(ah->hw->wiphy, ahvif->link[link_id]);
5484 
5485 		if (!arvif || !arvif->is_created)
5486 			continue;
5487 
5488 		if (ar == arvif->ar)
5489 			return link_id;
5490 	}
5491 
5492 	/* input ar is not assigned to any of the links of ML VIF, use next
5493 	 * available scan link for scan vdev creation. There are cases where
5494 	 * single scan req needs to be split in driver and initiate separate
5495 	 * scan requests to firmware based on device.
5496 	 */
5497 
5498 	 /* Unset all non-scan links (0-14) of scan_links_map so that ffs() will
5499 	  * choose an available link among scan links (i.e link id >= 15)
5500 	  */
5501 	scan_links_map = ~ahvif->links_map & ATH12K_SCAN_LINKS_MASK;
5502 	if (scan_links_map)
5503 		return __ffs(scan_links_map);
5504 
5505 	return ATH12K_FIRST_SCAN_LINK;
5506 }
5507 
5508 static int ath12k_mac_initiate_hw_scan(struct ieee80211_hw *hw,
5509 				       struct ieee80211_vif *vif,
5510 				       struct ieee80211_scan_request *hw_req,
5511 				       int n_channels,
5512 				       struct ieee80211_channel **chan_list,
5513 				       struct ath12k *ar)
5514 {
5515 	struct ath12k_hw *ah = ath12k_hw_to_ah(hw);
5516 	struct ath12k_vif *ahvif = ath12k_vif_to_ahvif(vif);
5517 	struct ath12k_link_vif *arvif;
5518 	struct cfg80211_scan_request *req = &hw_req->req;
5519 	struct ath12k_wmi_scan_req_arg *arg = NULL;
5520 	u8 link_id;
5521 	int ret;
5522 	int i;
5523 	bool create = true;
5524 
5525 	lockdep_assert_wiphy(hw->wiphy);
5526 
5527 	arvif = &ahvif->deflink;
5528 
5529 	/* check if any of the links of ML VIF is already started on
5530 	 * radio(ar) corresponding to given scan frequency and use it,
5531 	 * if not use scan link (link id >= 15) for scan purpose.
5532 	 */
5533 	link_id = ath12k_mac_find_link_id_by_ar(ahvif, ar);
5534 	/* All scan links are occupied. ideally this shouldn't happen as
5535 	 * mac80211 won't schedule scan for same band until ongoing scan is
5536 	 * completed, don't try to exceed max links just in case if it happens.
5537 	 */
5538 	if (link_id >= ATH12K_NUM_MAX_LINKS)
5539 		return -EBUSY;
5540 
5541 	arvif = ath12k_mac_assign_link_vif(ah, vif, link_id);
5542 
5543 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac link ID %d selected for scan",
5544 		   arvif->link_id);
5545 
5546 	/* If the vif is already assigned to a specific vdev of an ar,
5547 	 * check whether its already started, vdev which is started
5548 	 * are not allowed to switch to a new radio.
5549 	 * If the vdev is not started, but was earlier created on a
5550 	 * different ar, delete that vdev and create a new one. We don't
5551 	 * delete at the scan stop as an optimization to avoid redundant
5552 	 * delete-create vdev's for the same ar, in case the request is
5553 	 * always on the same band for the vif
5554 	 */
5555 	if (arvif->is_created) {
5556 		if (WARN_ON(!arvif->ar))
5557 			return -EINVAL;
5558 
5559 		if (ar != arvif->ar && arvif->is_started)
5560 			return -EINVAL;
5561 
5562 		if (ar != arvif->ar) {
5563 			ath12k_mac_remove_link_interface(hw, arvif);
5564 			ath12k_mac_unassign_link_vif(arvif);
5565 		} else {
5566 			create = false;
5567 		}
5568 	}
5569 
5570 	if (create) {
5571 		/* Previous arvif would've been cleared in radio switch block
5572 		 * above, assign arvif again for create.
5573 		 */
5574 		arvif = ath12k_mac_assign_link_vif(ah, vif, link_id);
5575 
5576 		ret = ath12k_mac_vdev_create(ar, arvif);
5577 		if (ret) {
5578 			ath12k_warn(ar->ab, "unable to create scan vdev %d\n", ret);
5579 			ath12k_mac_unassign_link_vif(arvif);
5580 			return ret;
5581 		}
5582 	}
5583 
5584 	spin_lock_bh(&ar->data_lock);
5585 	switch (ar->scan.state) {
5586 	case ATH12K_SCAN_IDLE:
5587 		reinit_completion(&ar->scan.started);
5588 		reinit_completion(&ar->scan.completed);
5589 		ar->scan.state = ATH12K_SCAN_STARTING;
5590 		ar->scan.is_roc = false;
5591 		ar->scan.arvif = arvif;
5592 		ret = 0;
5593 		break;
5594 	case ATH12K_SCAN_STARTING:
5595 	case ATH12K_SCAN_RUNNING:
5596 	case ATH12K_SCAN_ABORTING:
5597 		ret = -EBUSY;
5598 		break;
5599 	}
5600 	spin_unlock_bh(&ar->data_lock);
5601 
5602 	if (ret)
5603 		goto exit;
5604 
5605 	arg = kzalloc(sizeof(*arg), GFP_KERNEL);
5606 	if (!arg) {
5607 		ret = -ENOMEM;
5608 		goto exit;
5609 	}
5610 
5611 	ath12k_wmi_start_scan_init(ar, arg);
5612 	arg->vdev_id = arvif->vdev_id;
5613 	arg->scan_id = ATH12K_SCAN_ID;
5614 
5615 	if (req->ie_len) {
5616 		arg->extraie.ptr = kmemdup(req->ie, req->ie_len, GFP_KERNEL);
5617 		if (!arg->extraie.ptr) {
5618 			ret = -ENOMEM;
5619 			goto exit;
5620 		}
5621 		arg->extraie.len = req->ie_len;
5622 	}
5623 
5624 	if (req->n_ssids) {
5625 		arg->num_ssids = req->n_ssids;
5626 		for (i = 0; i < arg->num_ssids; i++)
5627 			arg->ssid[i] = req->ssids[i];
5628 	} else {
5629 		arg->scan_f_passive = 1;
5630 	}
5631 
5632 	if (n_channels) {
5633 		arg->num_chan = n_channels;
5634 		arg->chan_list = kcalloc(arg->num_chan, sizeof(*arg->chan_list),
5635 					 GFP_KERNEL);
5636 		if (!arg->chan_list) {
5637 			ret = -ENOMEM;
5638 			goto exit;
5639 		}
5640 
5641 		for (i = 0; i < arg->num_chan; i++)
5642 			arg->chan_list[i] = chan_list[i]->center_freq;
5643 	}
5644 
5645 	ret = ath12k_start_scan(ar, arg);
5646 	if (ret) {
5647 		if (ret == -EBUSY)
5648 			ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
5649 				   "scan engine is busy 11d state %d\n", ar->state_11d);
5650 		else
5651 			ath12k_warn(ar->ab, "failed to start hw scan: %d\n", ret);
5652 
5653 		spin_lock_bh(&ar->data_lock);
5654 		ar->scan.state = ATH12K_SCAN_IDLE;
5655 		spin_unlock_bh(&ar->data_lock);
5656 		goto exit;
5657 	}
5658 
5659 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac scan started");
5660 
5661 	/* Add a margin to account for event/command processing */
5662 	ieee80211_queue_delayed_work(ath12k_ar_to_hw(ar), &ar->scan.timeout,
5663 				     msecs_to_jiffies(arg->max_scan_time +
5664 						      ATH12K_MAC_SCAN_TIMEOUT_MSECS));
5665 
5666 exit:
5667 	if (arg) {
5668 		kfree(arg->chan_list);
5669 		kfree(arg->extraie.ptr);
5670 		kfree(arg);
5671 	}
5672 
5673 	if (ar->state_11d == ATH12K_11D_PREPARING &&
5674 	    ahvif->vdev_type == WMI_VDEV_TYPE_STA &&
5675 	    ahvif->vdev_subtype == WMI_VDEV_SUBTYPE_NONE)
5676 		ath12k_mac_11d_scan_start(ar, arvif->vdev_id);
5677 
5678 	return ret;
5679 }
5680 
5681 int ath12k_mac_op_hw_scan(struct ieee80211_hw *hw,
5682 			  struct ieee80211_vif *vif,
5683 			  struct ieee80211_scan_request *hw_req)
5684 {
5685 	struct ath12k_vif *ahvif = ath12k_vif_to_ahvif(vif);
5686 	struct ieee80211_channel **chan_list, *chan;
5687 	struct ath12k_hw *ah = ath12k_hw_to_ah(hw);
5688 	unsigned long links_map, link_id;
5689 	struct ath12k_link_vif *arvif;
5690 	struct ath12k *ar, *scan_ar;
5691 	int i, j, ret = 0;
5692 
5693 	lockdep_assert_wiphy(hw->wiphy);
5694 
5695 	chan_list = kcalloc(hw_req->req.n_channels, sizeof(*chan_list), GFP_KERNEL);
5696 	if (!chan_list)
5697 		return -ENOMEM;
5698 
5699 	/* There could be channels that belong to multiple underlying radio
5700 	 * in same scan request as mac80211 sees it as single band. In that
5701 	 * case split the hw_req based on frequency range and schedule scans to
5702 	 * corresponding radio.
5703 	 */
5704 	for_each_ar(ah, ar, i) {
5705 		int n_chans = 0;
5706 
5707 		for (j = 0; j < hw_req->req.n_channels; j++) {
5708 			chan = hw_req->req.channels[j];
5709 			scan_ar = ath12k_mac_select_scan_device(hw, vif,
5710 								chan->center_freq);
5711 			if (!scan_ar) {
5712 				ath12k_hw_warn(ah, "unable to select scan device for freq %d\n",
5713 					       chan->center_freq);
5714 				ret = -EINVAL;
5715 				goto abort;
5716 			}
5717 			if (ar != scan_ar)
5718 				continue;
5719 
5720 			chan_list[n_chans++] = chan;
5721 		}
5722 		if (n_chans) {
5723 			ret = ath12k_mac_initiate_hw_scan(hw, vif, hw_req, n_chans,
5724 							  chan_list, ar);
5725 			if (ret)
5726 				goto abort;
5727 		}
5728 	}
5729 abort:
5730 	/* If any of the parallel scans initiated fails, abort all and
5731 	 * remove the scan interfaces created. Return complete scan
5732 	 * failure as mac80211 assumes this as single scan request.
5733 	 */
5734 	if (ret) {
5735 		ath12k_hw_warn(ah, "Scan failed %d , cleanup all scan vdevs\n", ret);
5736 		links_map = ahvif->links_map;
5737 		for_each_set_bit(link_id, &links_map, ATH12K_NUM_MAX_LINKS) {
5738 			arvif = wiphy_dereference(hw->wiphy, ahvif->link[link_id]);
5739 			if (!arvif)
5740 				continue;
5741 
5742 			ar = arvif->ar;
5743 			if (ar->scan.arvif == arvif) {
5744 				wiphy_work_cancel(hw->wiphy, &ar->scan.vdev_clean_wk);
5745 				spin_lock_bh(&ar->data_lock);
5746 				ar->scan.arvif = NULL;
5747 				ar->scan.state = ATH12K_SCAN_IDLE;
5748 				ar->scan_channel = NULL;
5749 				ar->scan.roc_freq = 0;
5750 				spin_unlock_bh(&ar->data_lock);
5751 			}
5752 			if (link_id >= ATH12K_FIRST_SCAN_LINK) {
5753 				ath12k_mac_remove_link_interface(hw, arvif);
5754 				ath12k_mac_unassign_link_vif(arvif);
5755 			}
5756 		}
5757 	}
5758 	kfree(chan_list);
5759 	return ret;
5760 }
5761 EXPORT_SYMBOL(ath12k_mac_op_hw_scan);
5762 
5763 void ath12k_mac_op_cancel_hw_scan(struct ieee80211_hw *hw,
5764 				  struct ieee80211_vif *vif)
5765 {
5766 	struct ath12k_vif *ahvif = ath12k_vif_to_ahvif(vif);
5767 	unsigned long link_id, links_map = ahvif->links_map;
5768 	struct ath12k_link_vif *arvif;
5769 	struct ath12k *ar;
5770 
5771 	lockdep_assert_wiphy(hw->wiphy);
5772 
5773 	for_each_set_bit(link_id, &links_map, ATH12K_NUM_MAX_LINKS) {
5774 		arvif = wiphy_dereference(hw->wiphy, ahvif->link[link_id]);
5775 		if (!arvif || !arvif->is_created ||
5776 		    arvif->ar->scan.arvif != arvif)
5777 			continue;
5778 
5779 		ar = arvif->ar;
5780 
5781 		ath12k_scan_abort(ar);
5782 
5783 		cancel_delayed_work_sync(&ar->scan.timeout);
5784 	}
5785 }
5786 EXPORT_SYMBOL(ath12k_mac_op_cancel_hw_scan);
5787 
5788 static int ath12k_install_key(struct ath12k_link_vif *arvif,
5789 			      struct ieee80211_key_conf *key,
5790 			      enum set_key_cmd cmd,
5791 			      const u8 *macaddr, u32 flags)
5792 {
5793 	int ret;
5794 	struct ath12k *ar = arvif->ar;
5795 	struct wmi_vdev_install_key_arg arg = {
5796 		.vdev_id = arvif->vdev_id,
5797 		.key_idx = key->keyidx,
5798 		.key_len = key->keylen,
5799 		.key_data = key->key,
5800 		.key_flags = flags,
5801 		.ieee80211_key_cipher = key->cipher,
5802 		.macaddr = macaddr,
5803 	};
5804 	struct ath12k_vif *ahvif = arvif->ahvif;
5805 
5806 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
5807 
5808 	if (test_bit(ATH12K_FLAG_HW_CRYPTO_DISABLED, &ar->ab->dev_flags))
5809 		return 0;
5810 
5811 	if (cmd == DISABLE_KEY) {
5812 		/* TODO: Check if FW expects  value other than NONE for del */
5813 		/* arg.key_cipher = WMI_CIPHER_NONE; */
5814 		arg.key_len = 0;
5815 		arg.key_data = NULL;
5816 		goto check_order;
5817 	}
5818 
5819 	switch (key->cipher) {
5820 	case WLAN_CIPHER_SUITE_CCMP:
5821 	case WLAN_CIPHER_SUITE_CCMP_256:
5822 		arg.key_cipher = WMI_CIPHER_AES_CCM;
5823 		key->flags |= IEEE80211_KEY_FLAG_GENERATE_IV_MGMT;
5824 		break;
5825 	case WLAN_CIPHER_SUITE_TKIP:
5826 		arg.key_cipher = WMI_CIPHER_TKIP;
5827 		arg.key_txmic_len = 8;
5828 		arg.key_rxmic_len = 8;
5829 		break;
5830 	case WLAN_CIPHER_SUITE_GCMP:
5831 	case WLAN_CIPHER_SUITE_GCMP_256:
5832 		arg.key_cipher = WMI_CIPHER_AES_GCM;
5833 		key->flags |= IEEE80211_KEY_FLAG_GENERATE_IV_MGMT;
5834 		break;
5835 	case WLAN_CIPHER_SUITE_AES_CMAC:
5836 		arg.key_cipher = WMI_CIPHER_AES_CMAC;
5837 		break;
5838 	case WLAN_CIPHER_SUITE_BIP_GMAC_128:
5839 	case WLAN_CIPHER_SUITE_BIP_GMAC_256:
5840 		arg.key_cipher = WMI_CIPHER_AES_GMAC;
5841 		break;
5842 	case WLAN_CIPHER_SUITE_BIP_CMAC_256:
5843 		arg.key_cipher = WMI_CIPHER_AES_CMAC;
5844 		break;
5845 	default:
5846 		ath12k_warn(ar->ab, "cipher %d is not supported\n", key->cipher);
5847 		return -EOPNOTSUPP;
5848 	}
5849 
5850 	if (test_bit(ATH12K_FLAG_RAW_MODE, &ar->ab->dev_flags))
5851 		key->flags |= IEEE80211_KEY_FLAG_GENERATE_IV |
5852 			      IEEE80211_KEY_FLAG_RESERVE_TAILROOM;
5853 
5854 check_order:
5855 	if (ahvif->vdev_type == WMI_VDEV_TYPE_STA &&
5856 	    arg.key_flags == WMI_KEY_GROUP) {
5857 		if (cmd == SET_KEY) {
5858 			if (arvif->pairwise_key_done) {
5859 				ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
5860 					   "vdev %u pairwise key done, go install group key\n",
5861 					   arg.vdev_id);
5862 				goto install;
5863 			} else {
5864 				/* WCN7850 firmware requires pairwise key to be installed
5865 				 * before group key. In case group key comes first, cache
5866 				 * it and return. Will revisit it once pairwise key gets
5867 				 * installed.
5868 				 */
5869 				arvif->group_key = arg;
5870 				arvif->group_key_valid = true;
5871 				ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
5872 					   "vdev %u group key before pairwise key, cache and skip\n",
5873 					   arg.vdev_id);
5874 
5875 				ret = 0;
5876 				goto out;
5877 			}
5878 		} else {
5879 			arvif->group_key_valid = false;
5880 		}
5881 	}
5882 
5883 install:
5884 	reinit_completion(&ar->install_key_done);
5885 
5886 	ret = ath12k_wmi_vdev_install_key(arvif->ar, &arg);
5887 	if (ret)
5888 		return ret;
5889 
5890 	if (!wait_for_completion_timeout(&ar->install_key_done, 1 * HZ))
5891 		return -ETIMEDOUT;
5892 
5893 	if (ether_addr_equal(arg.macaddr, arvif->bssid))
5894 		ahvif->dp_vif.key_cipher = arg.ieee80211_key_cipher;
5895 
5896 	if (ar->install_key_status) {
5897 		ret = -EINVAL;
5898 		goto out;
5899 	}
5900 
5901 	if (ahvif->vdev_type == WMI_VDEV_TYPE_STA &&
5902 	    arg.key_flags == WMI_KEY_PAIRWISE) {
5903 		if (cmd == SET_KEY) {
5904 			arvif->pairwise_key_done = true;
5905 			if (arvif->group_key_valid) {
5906 				/* Install cached GTK */
5907 				arvif->group_key_valid = false;
5908 				arg = arvif->group_key;
5909 				ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
5910 					   "vdev %u pairwise key done, group key ready, go install\n",
5911 					   arg.vdev_id);
5912 				goto install;
5913 			}
5914 		} else {
5915 			arvif->pairwise_key_done = false;
5916 		}
5917 	}
5918 
5919 out:
5920 	if (ret) {
5921 		/* In case of failure userspace may not do DISABLE_KEY
5922 		 * but triggers re-connection directly, so manually reset
5923 		 * status here.
5924 		 */
5925 		arvif->group_key_valid = false;
5926 		arvif->pairwise_key_done = false;
5927 	}
5928 
5929 	return ret;
5930 }
5931 
5932 static int ath12k_clear_peer_keys(struct ath12k_link_vif *arvif,
5933 				  const u8 *addr)
5934 {
5935 	struct ath12k *ar = arvif->ar;
5936 	struct ath12k_base *ab = ar->ab;
5937 	struct ath12k_dp_link_peer *peer;
5938 	int first_errno = 0;
5939 	int ret;
5940 	int i, len;
5941 	u32 flags = 0;
5942 	struct ath12k_dp *dp = ath12k_ab_to_dp(ab);
5943 	struct ieee80211_key_conf *keys[WMI_MAX_KEY_INDEX + 1] = {};
5944 
5945 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
5946 
5947 	spin_lock_bh(&dp->dp_lock);
5948 	peer = ath12k_dp_link_peer_find_by_vdev_and_addr(dp, arvif->vdev_id, addr);
5949 	if (!peer || !peer->dp_peer) {
5950 		spin_unlock_bh(&dp->dp_lock);
5951 		return -ENOENT;
5952 	}
5953 
5954 	len = ARRAY_SIZE(peer->dp_peer->keys);
5955 	for (i = 0; i < len; i++) {
5956 		if (!peer->dp_peer->keys[i])
5957 			continue;
5958 
5959 		keys[i] = peer->dp_peer->keys[i];
5960 		peer->dp_peer->keys[i] = NULL;
5961 	}
5962 
5963 	spin_unlock_bh(&dp->dp_lock);
5964 
5965 	for (i = 0; i < len; i++) {
5966 		if (!keys[i])
5967 			continue;
5968 
5969 		/* key flags are not required to delete the key */
5970 		ret = ath12k_install_key(arvif, keys[i],
5971 					 DISABLE_KEY, addr, flags);
5972 		if (ret < 0 && first_errno == 0)
5973 			first_errno = ret;
5974 
5975 		if (ret < 0)
5976 			ath12k_warn(ab, "failed to remove peer key %d: %d\n",
5977 				    i, ret);
5978 	}
5979 
5980 	return first_errno;
5981 }
5982 
5983 static int ath12k_mac_set_key(struct ath12k *ar, enum set_key_cmd cmd,
5984 			      struct ath12k_link_vif *arvif,
5985 			      struct ath12k_link_sta *arsta,
5986 			      struct ieee80211_key_conf *key)
5987 {
5988 	struct ieee80211_sta *sta = NULL;
5989 	struct ath12k_base *ab = ar->ab;
5990 	struct ath12k_dp_link_peer *peer;
5991 	struct ath12k_sta *ahsta;
5992 	const u8 *peer_addr;
5993 	int ret;
5994 	u32 flags = 0;
5995 	struct ath12k_dp *dp = ath12k_ab_to_dp(ab);
5996 
5997 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
5998 
5999 	if (arsta)
6000 		sta = ath12k_ahsta_to_sta(arsta->ahsta);
6001 
6002 	if (test_bit(ATH12K_FLAG_HW_CRYPTO_DISABLED, &ab->dev_flags))
6003 		return 1;
6004 
6005 	if (sta)
6006 		peer_addr = arsta->addr;
6007 	else
6008 		peer_addr = arvif->bssid;
6009 
6010 	key->hw_key_idx = key->keyidx;
6011 
6012 	/* the peer should not disappear in mid-way (unless FW goes awry) since
6013 	 * we already hold wiphy lock. we just make sure its there now.
6014 	 */
6015 	spin_lock_bh(&dp->dp_lock);
6016 	peer = ath12k_dp_link_peer_find_by_vdev_and_addr(dp, arvif->vdev_id,
6017 							 peer_addr);
6018 	if (!peer || !peer->dp_peer) {
6019 		spin_unlock_bh(&dp->dp_lock);
6020 
6021 		if (cmd == SET_KEY) {
6022 			ath12k_warn(ab, "cannot install key for non-existent peer %pM\n",
6023 				    peer_addr);
6024 			return -EOPNOTSUPP;
6025 		}
6026 
6027 		/* if the peer doesn't exist there is no key to disable
6028 		 * anymore
6029 		 */
6030 		return 0;
6031 	}
6032 
6033 	spin_unlock_bh(&dp->dp_lock);
6034 
6035 	if (key->flags & IEEE80211_KEY_FLAG_PAIRWISE)
6036 		flags = WMI_KEY_PAIRWISE;
6037 	else
6038 		flags = WMI_KEY_GROUP;
6039 
6040 	ret = ath12k_install_key(arvif, key, cmd, peer_addr, flags);
6041 	if (ret) {
6042 		ath12k_warn(ab, "ath12k_install_key failed (%d)\n", ret);
6043 		return ret;
6044 	}
6045 
6046 	ret = ath12k_dp_rx_peer_pn_replay_config(arvif, peer_addr, cmd, key);
6047 	if (ret) {
6048 		ath12k_warn(ab, "failed to offload PN replay detection %d\n", ret);
6049 		return ret;
6050 	}
6051 
6052 	spin_lock_bh(&dp->dp_lock);
6053 	peer = ath12k_dp_link_peer_find_by_vdev_and_addr(dp, arvif->vdev_id,
6054 							 peer_addr);
6055 	if (peer && peer->dp_peer && cmd == SET_KEY) {
6056 		peer->dp_peer->keys[key->keyidx] = key;
6057 		if (key->flags & IEEE80211_KEY_FLAG_PAIRWISE) {
6058 			peer->dp_peer->ucast_keyidx = key->keyidx;
6059 			peer->dp_peer->sec_type =
6060 					ath12k_dp_tx_get_encrypt_type(key->cipher);
6061 		} else {
6062 			peer->dp_peer->mcast_keyidx = key->keyidx;
6063 			peer->dp_peer->sec_type_grp =
6064 					ath12k_dp_tx_get_encrypt_type(key->cipher);
6065 		}
6066 	} else if (peer && peer->dp_peer && cmd == DISABLE_KEY) {
6067 		peer->dp_peer->keys[key->keyidx] = NULL;
6068 		if (key->flags & IEEE80211_KEY_FLAG_PAIRWISE)
6069 			peer->dp_peer->ucast_keyidx = 0;
6070 		else
6071 			peer->dp_peer->mcast_keyidx = 0;
6072 	} else if (!peer)
6073 		/* impossible unless FW goes crazy */
6074 		ath12k_warn(ab, "peer %pM disappeared!\n", peer_addr);
6075 
6076 	if (sta) {
6077 		ahsta = ath12k_sta_to_ahsta(sta);
6078 
6079 		switch (key->cipher) {
6080 		case WLAN_CIPHER_SUITE_TKIP:
6081 		case WLAN_CIPHER_SUITE_CCMP:
6082 		case WLAN_CIPHER_SUITE_CCMP_256:
6083 		case WLAN_CIPHER_SUITE_GCMP:
6084 		case WLAN_CIPHER_SUITE_GCMP_256:
6085 			if (cmd == SET_KEY)
6086 				ahsta->pn_type = HAL_PN_TYPE_WPA;
6087 			else
6088 				ahsta->pn_type = HAL_PN_TYPE_NONE;
6089 			break;
6090 		default:
6091 			ahsta->pn_type = HAL_PN_TYPE_NONE;
6092 			break;
6093 		}
6094 	}
6095 
6096 	spin_unlock_bh(&dp->dp_lock);
6097 
6098 	return 0;
6099 }
6100 
6101 static int ath12k_mac_update_key_cache(struct ath12k_vif_cache *cache,
6102 				       enum set_key_cmd cmd,
6103 				       struct ieee80211_sta *sta,
6104 				       struct ieee80211_key_conf *key)
6105 {
6106 	struct ath12k_key_conf *key_conf, *tmp;
6107 
6108 	list_for_each_entry_safe(key_conf, tmp, &cache->key_conf.list, list) {
6109 		if (key_conf->key != key)
6110 			continue;
6111 
6112 		/* If SET key entry is already present in cache, nothing to do,
6113 		 * just return
6114 		 */
6115 		if (cmd == SET_KEY)
6116 			return 0;
6117 
6118 		/* DEL key for an old SET key which driver hasn't flushed yet.
6119 		 */
6120 		list_del(&key_conf->list);
6121 		kfree(key_conf);
6122 	}
6123 
6124 	if (cmd == SET_KEY) {
6125 		key_conf = kzalloc(sizeof(*key_conf), GFP_KERNEL);
6126 
6127 		if (!key_conf)
6128 			return -ENOMEM;
6129 
6130 		key_conf->cmd = cmd;
6131 		key_conf->sta = sta;
6132 		key_conf->key = key;
6133 		list_add_tail(&key_conf->list,
6134 			      &cache->key_conf.list);
6135 	}
6136 
6137 	return 0;
6138 }
6139 
6140 int ath12k_mac_op_set_key(struct ieee80211_hw *hw, enum set_key_cmd cmd,
6141 			  struct ieee80211_vif *vif, struct ieee80211_sta *sta,
6142 			  struct ieee80211_key_conf *key)
6143 {
6144 	struct ath12k_vif *ahvif = ath12k_vif_to_ahvif(vif);
6145 	struct ath12k_link_vif *arvif;
6146 	struct ath12k_link_sta *arsta = NULL;
6147 	struct ath12k_vif_cache *cache;
6148 	struct ath12k_sta *ahsta;
6149 	unsigned long links;
6150 	u8 link_id;
6151 	int ret;
6152 
6153 	lockdep_assert_wiphy(hw->wiphy);
6154 
6155 	/* IGTK needs to be done in host software */
6156 	if (key->keyidx == 4 || key->keyidx == 5)
6157 		return 1;
6158 
6159 	if (key->keyidx > WMI_MAX_KEY_INDEX)
6160 		return -ENOSPC;
6161 
6162 	if (sta) {
6163 		ahsta = ath12k_sta_to_ahsta(sta);
6164 
6165 		/* For an ML STA Pairwise key is same for all associated link Stations,
6166 		 * hence do set key for all link STAs which are active.
6167 		 */
6168 		if (sta->mlo) {
6169 			links = ahsta->links_map;
6170 			for_each_set_bit(link_id, &links, IEEE80211_MLD_MAX_NUM_LINKS) {
6171 				arvif = wiphy_dereference(hw->wiphy,
6172 							  ahvif->link[link_id]);
6173 				arsta = wiphy_dereference(hw->wiphy,
6174 							  ahsta->link[link_id]);
6175 
6176 				if (WARN_ON(!arvif || !arsta))
6177 					/* arvif and arsta are expected to be valid when
6178 					 * STA is present.
6179 					 */
6180 					continue;
6181 
6182 				ret = ath12k_mac_set_key(arvif->ar, cmd, arvif,
6183 							 arsta, key);
6184 				if (ret)
6185 					break;
6186 			}
6187 
6188 			return 0;
6189 		}
6190 
6191 		arsta = &ahsta->deflink;
6192 		arvif = arsta->arvif;
6193 		if (WARN_ON(!arvif))
6194 			return -EINVAL;
6195 
6196 		ret = ath12k_mac_set_key(arvif->ar, cmd, arvif, arsta, key);
6197 		if (ret)
6198 			return ret;
6199 
6200 		return 0;
6201 	}
6202 
6203 	if (key->link_id >= 0 && key->link_id < IEEE80211_MLD_MAX_NUM_LINKS) {
6204 		link_id = key->link_id;
6205 		arvif = wiphy_dereference(hw->wiphy, ahvif->link[link_id]);
6206 	} else {
6207 		link_id = 0;
6208 		arvif = &ahvif->deflink;
6209 	}
6210 
6211 	if (!arvif || !arvif->is_created) {
6212 		cache = ath12k_ahvif_get_link_cache(ahvif, link_id);
6213 		if (!cache)
6214 			return -ENOSPC;
6215 
6216 		ret = ath12k_mac_update_key_cache(cache, cmd, sta, key);
6217 		if (ret)
6218 			return ret;
6219 
6220 		return 0;
6221 	}
6222 
6223 	ret = ath12k_mac_set_key(arvif->ar, cmd, arvif, NULL, key);
6224 	if (ret)
6225 		return ret;
6226 
6227 	return 0;
6228 }
6229 EXPORT_SYMBOL(ath12k_mac_op_set_key);
6230 
6231 static int
6232 ath12k_mac_bitrate_mask_num_vht_rates(struct ath12k *ar,
6233 				      enum nl80211_band band,
6234 				      const struct cfg80211_bitrate_mask *mask)
6235 {
6236 	int num_rates = 0;
6237 	int i;
6238 
6239 	for (i = 0; i < ARRAY_SIZE(mask->control[band].vht_mcs); i++)
6240 		num_rates += hweight16(mask->control[band].vht_mcs[i]);
6241 
6242 	return num_rates;
6243 }
6244 
6245 static int
6246 ath12k_mac_bitrate_mask_num_he_rates(struct ath12k *ar,
6247 				     enum nl80211_band band,
6248 				     const struct cfg80211_bitrate_mask *mask)
6249 {
6250 	int num_rates = 0;
6251 	int i;
6252 
6253 	for (i = 0; i < ARRAY_SIZE(mask->control[band].he_mcs); i++)
6254 		num_rates += hweight16(mask->control[band].he_mcs[i]);
6255 
6256 	return num_rates;
6257 }
6258 
6259 static int
6260 ath12k_mac_bitrate_mask_num_eht_rates(struct ath12k *ar,
6261 				      enum nl80211_band band,
6262 				      const struct cfg80211_bitrate_mask *mask)
6263 {
6264 	int num_rates = 0;
6265 	int i;
6266 
6267 	for (i = 0; i < ARRAY_SIZE(mask->control[band].eht_mcs); i++)
6268 		num_rates += hweight16(mask->control[band].eht_mcs[i]);
6269 
6270 	return num_rates;
6271 }
6272 
6273 static int
6274 ath12k_mac_set_peer_vht_fixed_rate(struct ath12k_link_vif *arvif,
6275 				   struct ath12k_link_sta *arsta,
6276 				   const struct cfg80211_bitrate_mask *mask,
6277 				   enum nl80211_band band)
6278 {
6279 	struct ath12k *ar = arvif->ar;
6280 	u8 vht_rate, nss;
6281 	u32 rate_code;
6282 	int ret, i;
6283 
6284 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
6285 
6286 	nss = 0;
6287 
6288 	for (i = 0; i < ARRAY_SIZE(mask->control[band].vht_mcs); i++) {
6289 		if (hweight16(mask->control[band].vht_mcs[i]) == 1) {
6290 			nss = i + 1;
6291 			vht_rate = ffs(mask->control[band].vht_mcs[i]) - 1;
6292 		}
6293 	}
6294 
6295 	if (!nss) {
6296 		ath12k_warn(ar->ab, "No single VHT Fixed rate found to set for %pM",
6297 			    arsta->addr);
6298 		return -EINVAL;
6299 	}
6300 
6301 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
6302 		   "Setting Fixed VHT Rate for peer %pM. Device will not switch to any other selected rates",
6303 		   arsta->addr);
6304 
6305 	rate_code = ATH12K_HW_RATE_CODE(vht_rate, nss - 1,
6306 					WMI_RATE_PREAMBLE_VHT);
6307 	ret = ath12k_wmi_set_peer_param(ar, arsta->addr,
6308 					arvif->vdev_id,
6309 					WMI_PEER_PARAM_FIXED_RATE,
6310 					rate_code);
6311 	if (ret)
6312 		ath12k_warn(ar->ab,
6313 			    "failed to update STA %pM Fixed Rate %d: %d\n",
6314 			     arsta->addr, rate_code, ret);
6315 
6316 	return ret;
6317 }
6318 
6319 static int
6320 ath12k_mac_set_peer_he_fixed_rate(struct ath12k_link_vif *arvif,
6321 				  struct ath12k_link_sta *arsta,
6322 				  const struct cfg80211_bitrate_mask *mask,
6323 				  enum nl80211_band band)
6324 {
6325 	struct ath12k *ar = arvif->ar;
6326 	u8 he_rate, nss;
6327 	u32 rate_code;
6328 	int ret, i;
6329 	struct ath12k_sta *ahsta = arsta->ahsta;
6330 	struct ieee80211_sta *sta;
6331 
6332 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
6333 
6334 	sta = ath12k_ahsta_to_sta(ahsta);
6335 	nss = 0;
6336 
6337 	for (i = 0; i < ARRAY_SIZE(mask->control[band].he_mcs); i++) {
6338 		if (hweight16(mask->control[band].he_mcs[i]) == 1) {
6339 			nss = i + 1;
6340 			he_rate = ffs(mask->control[band].he_mcs[i]) - 1;
6341 		}
6342 	}
6343 
6344 	if (!nss) {
6345 		ath12k_warn(ar->ab, "No single HE Fixed rate found to set for %pM",
6346 			    arsta->addr);
6347 		return -EINVAL;
6348 	}
6349 
6350 	/* Avoid updating invalid nss as fixed rate*/
6351 	if (nss > sta->deflink.rx_nss)
6352 		return -EINVAL;
6353 
6354 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
6355 		   "Setting Fixed HE Rate for peer %pM. Device will not switch to any other selected rates",
6356 		   arsta->addr);
6357 
6358 	rate_code = ATH12K_HW_RATE_CODE(he_rate, nss - 1,
6359 					WMI_RATE_PREAMBLE_HE);
6360 
6361 	ret = ath12k_wmi_set_peer_param(ar, arsta->addr,
6362 					arvif->vdev_id,
6363 					WMI_PEER_PARAM_FIXED_RATE,
6364 					rate_code);
6365 	if (ret)
6366 		ath12k_warn(ar->ab,
6367 			    "failed to update STA %pM Fixed Rate %d: %d\n",
6368 			    arsta->addr, rate_code, ret);
6369 
6370 	return ret;
6371 }
6372 
6373 static int
6374 ath12k_mac_set_peer_eht_fixed_rate(struct ath12k_link_vif *arvif,
6375 				   struct ath12k_link_sta *arsta,
6376 				   const struct cfg80211_bitrate_mask *mask,
6377 				   enum nl80211_band band)
6378 {
6379 	struct ath12k_sta *ahsta = arsta->ahsta;
6380 	struct ath12k *ar = arvif->ar;
6381 	struct ieee80211_sta *sta;
6382 	struct ieee80211_link_sta *link_sta;
6383 	u8 eht_rate, nss = 0;
6384 	u32 rate_code;
6385 	int ret, i;
6386 
6387 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
6388 
6389 	sta = ath12k_ahsta_to_sta(ahsta);
6390 
6391 	for (i = 0; i < ARRAY_SIZE(mask->control[band].eht_mcs); i++) {
6392 		if (hweight16(mask->control[band].eht_mcs[i]) == 1) {
6393 			nss = i + 1;
6394 			eht_rate = ffs(mask->control[band].eht_mcs[i]) - 1;
6395 		}
6396 	}
6397 
6398 	if (!nss) {
6399 		ath12k_warn(ar->ab, "No single EHT Fixed rate found to set for %pM\n",
6400 			    arsta->addr);
6401 		return -EINVAL;
6402 	}
6403 
6404 	/* Avoid updating invalid nss as fixed rate*/
6405 	link_sta = ath12k_mac_get_link_sta(arsta);
6406 	if (!link_sta || nss > link_sta->rx_nss) {
6407 		ath12k_warn(ar->ab,
6408 			    "unable to access link sta for sta %pM link %u or fixed nss of %u is not supported by sta\n",
6409 			    sta->addr, arsta->link_id, nss);
6410 		return -EINVAL;
6411 	}
6412 
6413 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
6414 		   "Setting Fixed EHT Rate for peer %pM. Device will not switch to any other selected rates\n",
6415 		   arsta->addr);
6416 
6417 	rate_code = ATH12K_HW_RATE_CODE(eht_rate, nss - 1,
6418 					WMI_RATE_PREAMBLE_EHT);
6419 
6420 	ret = ath12k_wmi_set_peer_param(ar, arsta->addr,
6421 					arvif->vdev_id,
6422 					WMI_PEER_PARAM_FIXED_RATE,
6423 					rate_code);
6424 	if (ret)
6425 		ath12k_warn(ar->ab,
6426 			    "failed to update STA %pM Fixed Rate %d: %d\n",
6427 			    arsta->addr, rate_code, ret);
6428 
6429 	return ret;
6430 }
6431 
6432 static int ath12k_mac_station_assoc(struct ath12k *ar,
6433 				    struct ath12k_link_vif *arvif,
6434 				    struct ath12k_link_sta *arsta,
6435 				    bool reassoc)
6436 {
6437 	struct ieee80211_vif *vif = ath12k_ahvif_to_vif(arvif->ahvif);
6438 	struct ieee80211_sta *sta = ath12k_ahsta_to_sta(arsta->ahsta);
6439 	struct ieee80211_link_sta *link_sta;
6440 	int ret;
6441 	struct cfg80211_chan_def def;
6442 	enum nl80211_band band;
6443 	struct cfg80211_bitrate_mask *mask;
6444 	u8 num_vht_rates, num_he_rates, num_eht_rates;
6445 	u8 link_id = arvif->link_id;
6446 
6447 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
6448 
6449 	if (WARN_ON(ath12k_mac_vif_link_chan(vif, arvif->link_id, &def)))
6450 		return -EPERM;
6451 
6452 	if (WARN_ON(!rcu_access_pointer(sta->link[link_id])))
6453 		return -EINVAL;
6454 
6455 	band = def.chan->band;
6456 	mask = &arvif->bitrate_mask;
6457 
6458 	struct ath12k_wmi_peer_assoc_arg *peer_arg __free(kfree) =
6459 		kzalloc(sizeof(*peer_arg), GFP_KERNEL);
6460 	if (!peer_arg)
6461 		return -ENOMEM;
6462 
6463 	ath12k_peer_assoc_prepare(ar, arvif, arsta, peer_arg, reassoc);
6464 
6465 	if (peer_arg->peer_nss < 1) {
6466 		ath12k_warn(ar->ab,
6467 			    "invalid peer NSS %d\n", peer_arg->peer_nss);
6468 		return -EINVAL;
6469 	}
6470 
6471 	peer_arg->is_assoc = true;
6472 	ret = ath12k_wmi_send_peer_assoc_cmd(ar, peer_arg);
6473 	if (ret) {
6474 		ath12k_warn(ar->ab, "failed to run peer assoc for STA %pM vdev %i: %d\n",
6475 			    arsta->addr, arvif->vdev_id, ret);
6476 		return ret;
6477 	}
6478 
6479 	if (!wait_for_completion_timeout(&ar->peer_assoc_done, 1 * HZ)) {
6480 		ath12k_warn(ar->ab, "failed to get peer assoc conf event for %pM vdev %i\n",
6481 			    arsta->addr, arvif->vdev_id);
6482 		return -ETIMEDOUT;
6483 	}
6484 
6485 	num_vht_rates = ath12k_mac_bitrate_mask_num_vht_rates(ar, band, mask);
6486 	num_he_rates = ath12k_mac_bitrate_mask_num_he_rates(ar, band, mask);
6487 	num_eht_rates = ath12k_mac_bitrate_mask_num_eht_rates(ar, band, mask);
6488 
6489 	/* If single VHT/HE/EHT rate is configured (by set_bitrate_mask()),
6490 	 * peer_assoc will disable VHT/HE/EHT. This is now enabled by a peer
6491 	 * specific fixed param.
6492 	 * Note that all other rates and NSS will be disabled for this peer.
6493 	 */
6494 	link_sta = ath12k_mac_get_link_sta(arsta);
6495 	if (!link_sta) {
6496 		ath12k_warn(ar->ab, "unable to access link sta in station assoc\n");
6497 		return -EINVAL;
6498 	}
6499 
6500 	spin_lock_bh(&ar->data_lock);
6501 	arsta->bw = ath12k_mac_ieee80211_sta_bw_to_wmi(ar, link_sta);
6502 	arsta->bw_prev = link_sta->bandwidth;
6503 	spin_unlock_bh(&ar->data_lock);
6504 
6505 	if (link_sta->vht_cap.vht_supported && num_vht_rates == 1) {
6506 		ret = ath12k_mac_set_peer_vht_fixed_rate(arvif, arsta, mask, band);
6507 	} else if (link_sta->he_cap.has_he && num_he_rates == 1) {
6508 		ret = ath12k_mac_set_peer_he_fixed_rate(arvif, arsta, mask, band);
6509 		if (ret)
6510 			return ret;
6511 	} else if (link_sta->eht_cap.has_eht && num_eht_rates == 1) {
6512 		ret = ath12k_mac_set_peer_eht_fixed_rate(arvif, arsta, mask, band);
6513 		if (ret)
6514 			return ret;
6515 	}
6516 
6517 	/* Re-assoc is run only to update supported rates for given station. It
6518 	 * doesn't make much sense to reconfigure the peer completely.
6519 	 */
6520 	if (reassoc)
6521 		return 0;
6522 
6523 	ret = ath12k_setup_peer_smps(ar, arvif, arsta->addr,
6524 				     &link_sta->ht_cap, &link_sta->he_6ghz_capa);
6525 	if (ret) {
6526 		ath12k_warn(ar->ab, "failed to setup peer SMPS for vdev %d: %d\n",
6527 			    arvif->vdev_id, ret);
6528 		return ret;
6529 	}
6530 
6531 	if (!sta->wme) {
6532 		arvif->num_legacy_stations++;
6533 		ret = ath12k_recalc_rtscts_prot(arvif);
6534 		if (ret)
6535 			return ret;
6536 	}
6537 
6538 	if (sta->wme && sta->uapsd_queues) {
6539 		ret = ath12k_peer_assoc_qos_ap(ar, arvif, arsta);
6540 		if (ret) {
6541 			ath12k_warn(ar->ab, "failed to set qos params for STA %pM for vdev %i: %d\n",
6542 				    arsta->addr, arvif->vdev_id, ret);
6543 			return ret;
6544 		}
6545 	}
6546 
6547 	return 0;
6548 }
6549 
6550 static int ath12k_mac_station_disassoc(struct ath12k *ar,
6551 				       struct ath12k_link_vif *arvif,
6552 				       struct ath12k_link_sta *arsta)
6553 {
6554 	struct ieee80211_sta *sta = ath12k_ahsta_to_sta(arsta->ahsta);
6555 
6556 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
6557 
6558 	if (!sta->wme) {
6559 		arvif->num_legacy_stations--;
6560 		return ath12k_recalc_rtscts_prot(arvif);
6561 	}
6562 
6563 	return 0;
6564 }
6565 
6566 static void ath12k_sta_rc_update_wk(struct wiphy *wiphy, struct wiphy_work *wk)
6567 {
6568 	struct ieee80211_link_sta *link_sta;
6569 	struct ath12k *ar;
6570 	struct ath12k_link_vif *arvif;
6571 	struct ieee80211_sta *sta;
6572 	struct cfg80211_chan_def def;
6573 	enum nl80211_band band;
6574 	const u8 *ht_mcs_mask;
6575 	const u16 *vht_mcs_mask;
6576 	const u16 *he_mcs_mask;
6577 	const u16 *eht_mcs_mask;
6578 	u32 changed, bw, nss, mac_nss, smps, bw_prev;
6579 	int err, num_vht_rates, num_he_rates, num_eht_rates;
6580 	const struct cfg80211_bitrate_mask *mask;
6581 	enum wmi_phy_mode peer_phymode;
6582 	struct ath12k_link_sta *arsta;
6583 	struct ieee80211_vif *vif;
6584 
6585 	lockdep_assert_wiphy(wiphy);
6586 
6587 	arsta = container_of(wk, struct ath12k_link_sta, update_wk);
6588 	sta = ath12k_ahsta_to_sta(arsta->ahsta);
6589 	arvif = arsta->arvif;
6590 	vif = ath12k_ahvif_to_vif(arvif->ahvif);
6591 	ar = arvif->ar;
6592 
6593 	if (WARN_ON(ath12k_mac_vif_link_chan(vif, arvif->link_id, &def)))
6594 		return;
6595 
6596 	band = def.chan->band;
6597 	ht_mcs_mask = arvif->bitrate_mask.control[band].ht_mcs;
6598 	vht_mcs_mask = arvif->bitrate_mask.control[band].vht_mcs;
6599 	he_mcs_mask = arvif->bitrate_mask.control[band].he_mcs;
6600 	eht_mcs_mask = arvif->bitrate_mask.control[band].eht_mcs;
6601 
6602 	spin_lock_bh(&ar->data_lock);
6603 
6604 	changed = arsta->changed;
6605 	arsta->changed = 0;
6606 
6607 	bw = arsta->bw;
6608 	bw_prev = arsta->bw_prev;
6609 	nss = arsta->nss;
6610 	smps = arsta->smps;
6611 
6612 	spin_unlock_bh(&ar->data_lock);
6613 
6614 	nss = max_t(u32, 1, nss);
6615 	mac_nss = max3(ath12k_mac_max_ht_nss(ht_mcs_mask),
6616 		       ath12k_mac_max_vht_nss(vht_mcs_mask),
6617 		       ath12k_mac_max_he_nss(he_mcs_mask));
6618 	mac_nss = max(mac_nss, ath12k_mac_max_eht_nss(eht_mcs_mask));
6619 	nss = min(nss, mac_nss);
6620 
6621 	struct ath12k_wmi_peer_assoc_arg *peer_arg __free(kfree) =
6622 					kzalloc(sizeof(*peer_arg), GFP_KERNEL);
6623 	if (!peer_arg)
6624 		return;
6625 
6626 	if (changed & IEEE80211_RC_BW_CHANGED) {
6627 		ath12k_peer_assoc_h_phymode(ar, arvif, arsta, peer_arg);
6628 		peer_phymode = peer_arg->peer_phymode;
6629 
6630 		if (bw > bw_prev) {
6631 			/* Phymode shows maximum supported channel width, if we
6632 			 * upgrade bandwidth then due to sanity check of firmware,
6633 			 * we have to send WMI_PEER_PHYMODE followed by
6634 			 * WMI_PEER_CHWIDTH
6635 			 */
6636 			ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac bandwidth upgrade for sta %pM new %d old %d\n",
6637 				   arsta->addr, bw, bw_prev);
6638 			err = ath12k_wmi_set_peer_param(ar, arsta->addr,
6639 							arvif->vdev_id, WMI_PEER_PHYMODE,
6640 							peer_phymode);
6641 			if (err) {
6642 				ath12k_warn(ar->ab, "failed to update STA %pM to peer phymode %d: %d\n",
6643 					    arsta->addr, peer_phymode, err);
6644 				return;
6645 			}
6646 			err = ath12k_wmi_set_peer_param(ar, arsta->addr,
6647 							arvif->vdev_id, WMI_PEER_CHWIDTH,
6648 							bw);
6649 			if (err)
6650 				ath12k_warn(ar->ab, "failed to update STA %pM to peer bandwidth %d: %d\n",
6651 					    arsta->addr, bw, err);
6652 		} else {
6653 			/* When we downgrade bandwidth this will conflict with phymode
6654 			 * and cause to trigger firmware crash. In this case we send
6655 			 * WMI_PEER_CHWIDTH followed by WMI_PEER_PHYMODE
6656 			 */
6657 			ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac bandwidth downgrade for sta %pM new %d old %d\n",
6658 				   arsta->addr, bw, bw_prev);
6659 			err = ath12k_wmi_set_peer_param(ar, arsta->addr,
6660 							arvif->vdev_id, WMI_PEER_CHWIDTH,
6661 							bw);
6662 			if (err) {
6663 				ath12k_warn(ar->ab, "failed to update STA %pM peer to bandwidth %d: %d\n",
6664 					    arsta->addr, bw, err);
6665 				return;
6666 			}
6667 			err = ath12k_wmi_set_peer_param(ar, arsta->addr,
6668 							arvif->vdev_id, WMI_PEER_PHYMODE,
6669 							peer_phymode);
6670 			if (err)
6671 				ath12k_warn(ar->ab, "failed to update STA %pM to peer phymode %d: %d\n",
6672 					    arsta->addr, peer_phymode, err);
6673 		}
6674 	}
6675 
6676 	if (changed & IEEE80211_RC_NSS_CHANGED) {
6677 		ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac update sta %pM nss %d\n",
6678 			   arsta->addr, nss);
6679 
6680 		err = ath12k_wmi_set_peer_param(ar, arsta->addr, arvif->vdev_id,
6681 						WMI_PEER_NSS, nss);
6682 		if (err)
6683 			ath12k_warn(ar->ab, "failed to update STA %pM nss %d: %d\n",
6684 				    arsta->addr, nss, err);
6685 	}
6686 
6687 	if (changed & IEEE80211_RC_SMPS_CHANGED) {
6688 		ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac update sta %pM smps %d\n",
6689 			   arsta->addr, smps);
6690 
6691 		err = ath12k_wmi_set_peer_param(ar, arsta->addr, arvif->vdev_id,
6692 						WMI_PEER_MIMO_PS_STATE, smps);
6693 		if (err)
6694 			ath12k_warn(ar->ab, "failed to update STA %pM smps %d: %d\n",
6695 				    arsta->addr, smps, err);
6696 	}
6697 
6698 	if (changed & IEEE80211_RC_SUPP_RATES_CHANGED) {
6699 		mask = &arvif->bitrate_mask;
6700 		num_vht_rates = ath12k_mac_bitrate_mask_num_vht_rates(ar, band,
6701 								      mask);
6702 		num_he_rates = ath12k_mac_bitrate_mask_num_he_rates(ar, band,
6703 								    mask);
6704 		num_eht_rates = ath12k_mac_bitrate_mask_num_eht_rates(ar, band,
6705 								      mask);
6706 
6707 		/* Peer_assoc_prepare will reject vht rates in
6708 		 * bitrate_mask if its not available in range format and
6709 		 * sets vht tx_rateset as unsupported. So multiple VHT MCS
6710 		 * setting(eg. MCS 4,5,6) per peer is not supported here.
6711 		 * But, Single rate in VHT mask can be set as per-peer
6712 		 * fixed rate. But even if any HT rates are configured in
6713 		 * the bitrate mask, device will not switch to those rates
6714 		 * when per-peer Fixed rate is set.
6715 		 * TODO: Check RATEMASK_CMDID to support auto rates selection
6716 		 * across HT/VHT and for multiple VHT MCS support.
6717 		 */
6718 		link_sta = ath12k_mac_get_link_sta(arsta);
6719 		if (!link_sta) {
6720 			ath12k_warn(ar->ab, "unable to access link sta in peer assoc he for sta %pM link %u\n",
6721 				    sta->addr, arsta->link_id);
6722 			return;
6723 		}
6724 
6725 		if (link_sta->vht_cap.vht_supported && num_vht_rates == 1) {
6726 			ath12k_mac_set_peer_vht_fixed_rate(arvif, arsta, mask,
6727 							   band);
6728 		} else if (link_sta->he_cap.has_he && num_he_rates == 1) {
6729 			ath12k_mac_set_peer_he_fixed_rate(arvif, arsta, mask, band);
6730 		} else if (link_sta->eht_cap.has_eht && num_eht_rates == 1) {
6731 			err = ath12k_mac_set_peer_eht_fixed_rate(arvif, arsta,
6732 								 mask, band);
6733 			if (err) {
6734 				ath12k_warn(ar->ab,
6735 					    "failed to set peer EHT fixed rate for STA %pM ret %d\n",
6736 					    arsta->addr, err);
6737 				return;
6738 			}
6739 		} else {
6740 			/* If the peer is non-VHT/HE/EHT or no fixed VHT/HE/EHT
6741 			 * rate is provided in the new bitrate mask we set the
6742 			 * other rates using peer_assoc command. Also clear
6743 			 * the peer fixed rate settings as it has higher proprity
6744 			 * than peer assoc
6745 			 */
6746 			err = ath12k_wmi_set_peer_param(ar, arsta->addr,
6747 							arvif->vdev_id,
6748 							WMI_PEER_PARAM_FIXED_RATE,
6749 							WMI_FIXED_RATE_NONE);
6750 			if (err)
6751 				ath12k_warn(ar->ab,
6752 					    "failed to disable peer fixed rate for STA %pM ret %d\n",
6753 					    arsta->addr, err);
6754 
6755 			ath12k_peer_assoc_prepare(ar, arvif, arsta,
6756 						  peer_arg, true);
6757 
6758 			peer_arg->is_assoc = false;
6759 			err = ath12k_wmi_send_peer_assoc_cmd(ar, peer_arg);
6760 			if (err)
6761 				ath12k_warn(ar->ab, "failed to run peer assoc for STA %pM vdev %i: %d\n",
6762 					    arsta->addr, arvif->vdev_id, err);
6763 
6764 			if (!wait_for_completion_timeout(&ar->peer_assoc_done, 1 * HZ))
6765 				ath12k_warn(ar->ab, "failed to get peer assoc conf event for %pM vdev %i\n",
6766 					    arsta->addr, arvif->vdev_id);
6767 		}
6768 	}
6769 }
6770 
6771 static void ath12k_mac_free_unassign_link_sta(struct ath12k_hw *ah,
6772 					      struct ath12k_sta *ahsta,
6773 					      u8 link_id)
6774 {
6775 	struct ath12k_link_sta *arsta;
6776 
6777 	lockdep_assert_wiphy(ah->hw->wiphy);
6778 
6779 	if (WARN_ON(link_id >= IEEE80211_MLD_MAX_NUM_LINKS))
6780 		return;
6781 
6782 	arsta = wiphy_dereference(ah->hw->wiphy, ahsta->link[link_id]);
6783 	if (WARN_ON(!arsta))
6784 		return;
6785 
6786 	ahsta->links_map &= ~BIT(link_id);
6787 	rcu_assign_pointer(ahsta->link[link_id], NULL);
6788 	synchronize_rcu();
6789 
6790 	if (arsta == &ahsta->deflink) {
6791 		arsta->link_id = ATH12K_INVALID_LINK_ID;
6792 		arsta->ahsta = NULL;
6793 		arsta->arvif = NULL;
6794 		return;
6795 	}
6796 
6797 	kfree(arsta);
6798 }
6799 
6800 static int ath12k_mac_inc_num_stations(struct ath12k_link_vif *arvif,
6801 				       struct ath12k_link_sta *arsta)
6802 {
6803 	struct ieee80211_sta *sta = ath12k_ahsta_to_sta(arsta->ahsta);
6804 	struct ath12k *ar = arvif->ar;
6805 
6806 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
6807 
6808 	if (arvif->ahvif->vdev_type == WMI_VDEV_TYPE_STA && !sta->tdls)
6809 		return 0;
6810 
6811 	if (ar->num_stations >= ar->max_num_stations)
6812 		return -ENOBUFS;
6813 
6814 	ar->num_stations++;
6815 	arvif->num_stations++;
6816 
6817 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
6818 		   "mac station %pM connected to vdev %u num_stations %u\n",
6819 		   arsta->addr, arvif->vdev_id, arvif->num_stations);
6820 
6821 	return 0;
6822 }
6823 
6824 static void ath12k_mac_dec_num_stations(struct ath12k_link_vif *arvif,
6825 					struct ath12k_link_sta *arsta)
6826 {
6827 	struct ieee80211_sta *sta = ath12k_ahsta_to_sta(arsta->ahsta);
6828 	struct ath12k *ar = arvif->ar;
6829 
6830 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
6831 
6832 	if (arvif->ahvif->vdev_type == WMI_VDEV_TYPE_STA && !sta->tdls)
6833 		return;
6834 
6835 	ar->num_stations--;
6836 
6837 	if (arvif->num_stations) {
6838 		arvif->num_stations--;
6839 		ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
6840 			   "mac station %pM disconnected from vdev %u num_stations %u\n",
6841 			   arsta->addr, arvif->vdev_id, arvif->num_stations);
6842 	} else {
6843 		ath12k_warn(ar->ab,
6844 			    "mac station %pM disconnect for vdev %u without any connected station\n",
6845 			    arsta->addr, arvif->vdev_id);
6846 	}
6847 }
6848 
6849 static void ath12k_mac_station_post_remove(struct ath12k *ar,
6850 					   struct ath12k_link_vif *arvif,
6851 					   struct ath12k_link_sta *arsta)
6852 {
6853 	struct ieee80211_vif *vif = ath12k_ahvif_to_vif(arvif->ahvif);
6854 	struct ieee80211_sta *sta = ath12k_ahsta_to_sta(arsta->ahsta);
6855 	struct ath12k_dp_link_peer *peer;
6856 	struct ath12k_dp *dp = ath12k_ab_to_dp(ar->ab);
6857 
6858 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
6859 
6860 	ath12k_mac_dec_num_stations(arvif, arsta);
6861 
6862 	spin_lock_bh(&dp->dp_lock);
6863 
6864 	peer = ath12k_dp_link_peer_find_by_vdev_and_addr(dp, arvif->vdev_id,
6865 							 arsta->addr);
6866 	if (peer && peer->sta == sta) {
6867 		ath12k_warn(ar->ab, "Found peer entry %pM n vdev %i after it was supposedly removed\n",
6868 			    vif->addr, arvif->vdev_id);
6869 		peer->sta = NULL;
6870 
6871 		ath12k_dp_link_peer_free(peer);
6872 		ar->num_peers--;
6873 	}
6874 
6875 	spin_unlock_bh(&dp->dp_lock);
6876 }
6877 
6878 static int ath12k_mac_station_unauthorize(struct ath12k *ar,
6879 					  struct ath12k_link_vif *arvif,
6880 					  struct ath12k_link_sta *arsta)
6881 {
6882 	struct ath12k_dp_link_peer *peer;
6883 	int ret;
6884 	struct ath12k_dp *dp = ath12k_ab_to_dp(ar->ab);
6885 
6886 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
6887 
6888 	spin_lock_bh(&dp->dp_lock);
6889 
6890 	peer = ath12k_dp_link_peer_find_by_vdev_and_addr(dp, arvif->vdev_id,
6891 							 arsta->addr);
6892 	if (peer)
6893 		peer->is_authorized = false;
6894 
6895 	spin_unlock_bh(&dp->dp_lock);
6896 
6897 	/* Driver must clear the keys during the state change from
6898 	 * IEEE80211_STA_AUTHORIZED to IEEE80211_STA_ASSOC, since after
6899 	 * returning from here, mac80211 is going to delete the keys
6900 	 * in __sta_info_destroy_part2(). This will ensure that the driver does
6901 	 * not retain stale key references after mac80211 deletes the keys.
6902 	 */
6903 	ret = ath12k_clear_peer_keys(arvif, arsta->addr);
6904 	if (ret) {
6905 		ath12k_warn(ar->ab, "failed to clear all peer keys for vdev %i: %d\n",
6906 			    arvif->vdev_id, ret);
6907 		return ret;
6908 	}
6909 
6910 	return 0;
6911 }
6912 
6913 static int ath12k_mac_station_authorize(struct ath12k *ar,
6914 					struct ath12k_link_vif *arvif,
6915 					struct ath12k_link_sta *arsta)
6916 {
6917 	struct ath12k_dp_link_peer *peer;
6918 	struct ieee80211_vif *vif = ath12k_ahvif_to_vif(arvif->ahvif);
6919 	int ret;
6920 	struct ath12k_dp *dp = ath12k_ab_to_dp(ar->ab);
6921 
6922 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
6923 
6924 	spin_lock_bh(&dp->dp_lock);
6925 
6926 	peer = ath12k_dp_link_peer_find_by_vdev_and_addr(dp, arvif->vdev_id,
6927 							 arsta->addr);
6928 	if (peer)
6929 		peer->is_authorized = true;
6930 
6931 	spin_unlock_bh(&dp->dp_lock);
6932 
6933 	if (vif->type == NL80211_IFTYPE_STATION && arvif->is_up) {
6934 		ret = ath12k_wmi_set_peer_param(ar, arsta->addr,
6935 						arvif->vdev_id,
6936 						WMI_PEER_AUTHORIZE,
6937 						1);
6938 		if (ret) {
6939 			ath12k_warn(ar->ab, "Unable to authorize peer %pM vdev %d: %d\n",
6940 				    arsta->addr, arvif->vdev_id, ret);
6941 			return ret;
6942 		}
6943 	}
6944 
6945 	return 0;
6946 }
6947 
6948 static int ath12k_mac_station_remove(struct ath12k *ar,
6949 				     struct ath12k_link_vif *arvif,
6950 				     struct ath12k_link_sta *arsta)
6951 {
6952 	struct ieee80211_sta *sta = ath12k_ahsta_to_sta(arsta->ahsta);
6953 	struct ath12k_vif *ahvif = arvif->ahvif;
6954 	int ret = 0;
6955 	struct ath12k_link_sta *temp_arsta;
6956 
6957 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
6958 
6959 	wiphy_work_cancel(ar->ah->hw->wiphy, &arsta->update_wk);
6960 
6961 	if (ahvif->vdev_type == WMI_VDEV_TYPE_STA) {
6962 		ath12k_bss_disassoc(ar, arvif);
6963 		ret = ath12k_mac_vdev_stop(arvif);
6964 		if (ret)
6965 			ath12k_warn(ar->ab, "failed to stop vdev %i: %d\n",
6966 				    arvif->vdev_id, ret);
6967 	}
6968 
6969 	if (sta->mlo)
6970 		return ret;
6971 
6972 	ath12k_dp_peer_cleanup(ar, arvif->vdev_id, arsta->addr);
6973 
6974 	ret = ath12k_peer_delete(ar, arvif->vdev_id, arsta->addr);
6975 	if (ret)
6976 		ath12k_warn(ar->ab, "Failed to delete peer: %pM for VDEV: %d\n",
6977 			    arsta->addr, arvif->vdev_id);
6978 	else
6979 		ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "Removed peer: %pM for VDEV: %d\n",
6980 			   arsta->addr, arvif->vdev_id);
6981 
6982 	ath12k_mac_station_post_remove(ar, arvif, arsta);
6983 
6984 	spin_lock_bh(&ar->ab->base_lock);
6985 
6986 	/* To handle roaming and split phy scenario */
6987 	temp_arsta = ath12k_link_sta_find_by_addr(ar->ab, arsta->addr);
6988 	if (temp_arsta && temp_arsta->arvif->ar == ar)
6989 		ath12k_link_sta_rhash_delete(ar->ab, arsta);
6990 
6991 	spin_unlock_bh(&ar->ab->base_lock);
6992 
6993 	if (sta->valid_links)
6994 		ath12k_mac_free_unassign_link_sta(ahvif->ah,
6995 						  arsta->ahsta, arsta->link_id);
6996 
6997 	return ret;
6998 }
6999 
7000 static int ath12k_mac_station_add(struct ath12k *ar,
7001 				  struct ath12k_link_vif *arvif,
7002 				  struct ath12k_link_sta *arsta)
7003 {
7004 	struct ath12k_base *ab = ar->ab;
7005 	struct ieee80211_vif *vif = ath12k_ahvif_to_vif(arvif->ahvif);
7006 	struct ieee80211_sta *sta = ath12k_ahsta_to_sta(arsta->ahsta);
7007 	struct ath12k_wmi_peer_create_arg peer_param = {};
7008 	int ret;
7009 	struct ath12k_link_sta *temp_arsta;
7010 
7011 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
7012 
7013 	ret = ath12k_mac_inc_num_stations(arvif, arsta);
7014 	if (ret) {
7015 		ath12k_warn(ab, "refusing to associate station: too many connected already (%d)\n",
7016 			    ar->max_num_stations);
7017 		goto exit;
7018 	}
7019 
7020 	spin_lock_bh(&ab->base_lock);
7021 
7022 	/*
7023 	 * In case of Split PHY and roaming scenario, pdev idx
7024 	 * might differ but both the pdev will share same rhash
7025 	 * table. In that case update the rhash table if link_sta is
7026 	 * already present
7027 	 */
7028 	temp_arsta = ath12k_link_sta_find_by_addr(ab, arsta->addr);
7029 	if (temp_arsta && temp_arsta->arvif->ar != ar)
7030 		ath12k_link_sta_rhash_delete(ab, temp_arsta);
7031 
7032 	ret = ath12k_link_sta_rhash_add(ab, arsta);
7033 	spin_unlock_bh(&ab->base_lock);
7034 	if (ret) {
7035 		ath12k_warn(ab, "Failed to add arsta: %pM to hash table, ret: %d",
7036 			    arsta->addr, ret);
7037 		goto dec_num_station;
7038 	}
7039 
7040 	peer_param.vdev_id = arvif->vdev_id;
7041 	peer_param.peer_addr = arsta->addr;
7042 	peer_param.peer_type = WMI_PEER_TYPE_DEFAULT;
7043 	peer_param.ml_enabled = sta->mlo;
7044 
7045 	ret = ath12k_peer_create(ar, arvif, sta, &peer_param);
7046 	if (ret) {
7047 		ath12k_warn(ab, "Failed to add peer: %pM for VDEV: %d\n",
7048 			    arsta->addr, arvif->vdev_id);
7049 		goto free_peer;
7050 	}
7051 
7052 	ath12k_dbg(ab, ATH12K_DBG_MAC, "Added peer: %pM for VDEV: %d\n",
7053 		   arsta->addr, arvif->vdev_id);
7054 
7055 	if (ieee80211_vif_is_mesh(vif)) {
7056 		ret = ath12k_wmi_set_peer_param(ar, arsta->addr,
7057 						arvif->vdev_id,
7058 						WMI_PEER_USE_4ADDR, 1);
7059 		if (ret) {
7060 			ath12k_warn(ab, "failed to STA %pM 4addr capability: %d\n",
7061 				    arsta->addr, ret);
7062 			goto free_peer;
7063 		}
7064 	}
7065 
7066 	ret = ath12k_dp_peer_setup(ar, arvif->vdev_id, arsta->addr);
7067 	if (ret) {
7068 		ath12k_warn(ab, "failed to setup dp for peer %pM on vdev %i (%d)\n",
7069 			    arsta->addr, arvif->vdev_id, ret);
7070 		goto free_peer;
7071 	}
7072 
7073 	if (ab->hw_params->vdev_start_delay &&
7074 	    !arvif->is_started &&
7075 	    arvif->ahvif->vdev_type != WMI_VDEV_TYPE_AP) {
7076 		ret = ath12k_start_vdev_delay(ar, arvif);
7077 		if (ret) {
7078 			ath12k_warn(ab, "failed to delay vdev start: %d\n", ret);
7079 			goto free_peer;
7080 		}
7081 	}
7082 
7083 	return 0;
7084 
7085 free_peer:
7086 	ath12k_peer_delete(ar, arvif->vdev_id, arsta->addr);
7087 	spin_lock_bh(&ab->base_lock);
7088 	ath12k_link_sta_rhash_delete(ab, arsta);
7089 	spin_unlock_bh(&ab->base_lock);
7090 dec_num_station:
7091 	ath12k_mac_dec_num_stations(arvif, arsta);
7092 exit:
7093 	return ret;
7094 }
7095 
7096 static int ath12k_mac_assign_link_sta(struct ath12k_hw *ah,
7097 				      struct ath12k_sta *ahsta,
7098 				      struct ath12k_link_sta *arsta,
7099 				      struct ath12k_vif *ahvif,
7100 				      u8 link_id)
7101 {
7102 	struct ieee80211_sta *sta = ath12k_ahsta_to_sta(ahsta);
7103 	struct ieee80211_link_sta *link_sta;
7104 	struct ath12k_link_vif *arvif;
7105 
7106 	lockdep_assert_wiphy(ah->hw->wiphy);
7107 
7108 	if (!arsta || link_id >= IEEE80211_MLD_MAX_NUM_LINKS)
7109 		return -EINVAL;
7110 
7111 	arvif = wiphy_dereference(ah->hw->wiphy, ahvif->link[link_id]);
7112 	if (!arvif)
7113 		return -EINVAL;
7114 
7115 	memset(arsta, 0, sizeof(*arsta));
7116 
7117 	link_sta = wiphy_dereference(ah->hw->wiphy, sta->link[link_id]);
7118 	if (!link_sta)
7119 		return -EINVAL;
7120 
7121 	ether_addr_copy(arsta->addr, link_sta->addr);
7122 
7123 	/* logical index of the link sta in order of creation */
7124 	arsta->link_idx = ahsta->num_peer++;
7125 
7126 	arsta->link_id = link_id;
7127 	ahsta->links_map |= BIT(arsta->link_id);
7128 	arsta->arvif = arvif;
7129 	arsta->ahsta = ahsta;
7130 	ahsta->ahvif = ahvif;
7131 
7132 	wiphy_work_init(&arsta->update_wk, ath12k_sta_rc_update_wk);
7133 
7134 	rcu_assign_pointer(ahsta->link[link_id], arsta);
7135 
7136 	return 0;
7137 }
7138 
7139 static void ath12k_mac_ml_station_remove(struct ath12k_vif *ahvif,
7140 					 struct ath12k_sta *ahsta)
7141 {
7142 	struct ieee80211_sta *sta = ath12k_ahsta_to_sta(ahsta);
7143 	struct ath12k_hw *ah = ahvif->ah;
7144 	struct ath12k_link_vif *arvif;
7145 	struct ath12k_link_sta *arsta;
7146 	unsigned long links;
7147 	struct ath12k *ar;
7148 	u8 link_id;
7149 
7150 	lockdep_assert_wiphy(ah->hw->wiphy);
7151 
7152 	ath12k_peer_mlo_link_peers_delete(ahvif, ahsta);
7153 
7154 	/* validate link station removal and clear arsta links */
7155 	links = ahsta->links_map;
7156 	for_each_set_bit(link_id, &links, IEEE80211_MLD_MAX_NUM_LINKS) {
7157 		arvif = wiphy_dereference(ah->hw->wiphy, ahvif->link[link_id]);
7158 		arsta = wiphy_dereference(ah->hw->wiphy, ahsta->link[link_id]);
7159 		if (!arvif || !arsta)
7160 			continue;
7161 
7162 		ar = arvif->ar;
7163 
7164 		ath12k_mac_station_post_remove(ar, arvif, arsta);
7165 
7166 		spin_lock_bh(&ar->ab->base_lock);
7167 		ath12k_link_sta_rhash_delete(ar->ab, arsta);
7168 		spin_unlock_bh(&ar->ab->base_lock);
7169 
7170 		ath12k_mac_free_unassign_link_sta(ah, ahsta, link_id);
7171 	}
7172 
7173 	if (sta->mlo) {
7174 		clear_bit(ahsta->ml_peer_id, ah->free_ml_peer_id_map);
7175 		ahsta->ml_peer_id = ATH12K_MLO_PEER_ID_INVALID;
7176 	}
7177 }
7178 
7179 static int ath12k_mac_handle_link_sta_state(struct ieee80211_hw *hw,
7180 					    struct ath12k_link_vif *arvif,
7181 					    struct ath12k_link_sta *arsta,
7182 					    enum ieee80211_sta_state old_state,
7183 					    enum ieee80211_sta_state new_state)
7184 {
7185 	struct ieee80211_vif *vif = ath12k_ahvif_to_vif(arvif->ahvif);
7186 	struct ieee80211_bss_conf *link_conf;
7187 	struct ath12k *ar = arvif->ar;
7188 	struct ath12k_reg_info *reg_info;
7189 	struct ath12k_base *ab = ar->ab;
7190 	int ret = 0;
7191 
7192 	lockdep_assert_wiphy(hw->wiphy);
7193 
7194 	ath12k_dbg(ab, ATH12K_DBG_MAC, "mac handle link %u sta %pM state %d -> %d\n",
7195 		   arsta->link_id, arsta->addr, old_state, new_state);
7196 
7197 	/* IEEE80211_STA_NONE -> IEEE80211_STA_NOTEXIST: Remove the station
7198 	 * from driver
7199 	 */
7200 	if ((old_state == IEEE80211_STA_NONE &&
7201 	     new_state == IEEE80211_STA_NOTEXIST)) {
7202 		ret = ath12k_mac_station_remove(ar, arvif, arsta);
7203 		if (ret) {
7204 			ath12k_warn(ab, "Failed to remove station: %pM for VDEV: %d\n",
7205 				    arsta->addr, arvif->vdev_id);
7206 			goto exit;
7207 		}
7208 	}
7209 
7210 	/* IEEE80211_STA_NOTEXIST -> IEEE80211_STA_NONE: Add new station to driver */
7211 	if (old_state == IEEE80211_STA_NOTEXIST &&
7212 	    new_state == IEEE80211_STA_NONE) {
7213 		ret = ath12k_mac_station_add(ar, arvif, arsta);
7214 		if (ret)
7215 			ath12k_warn(ab, "Failed to add station: %pM for VDEV: %d\n",
7216 				    arsta->addr, arvif->vdev_id);
7217 
7218 	/* IEEE80211_STA_AUTH -> IEEE80211_STA_ASSOC: Send station assoc command for
7219 	 * peer associated to AP/Mesh/ADHOC vif type.
7220 	 */
7221 	} else if (old_state == IEEE80211_STA_AUTH &&
7222 		   new_state == IEEE80211_STA_ASSOC &&
7223 		   (vif->type == NL80211_IFTYPE_AP ||
7224 		    vif->type == NL80211_IFTYPE_MESH_POINT ||
7225 		    vif->type == NL80211_IFTYPE_ADHOC)) {
7226 		ret = ath12k_mac_station_assoc(ar, arvif, arsta, false);
7227 		if (ret)
7228 			ath12k_warn(ab, "Failed to associate station: %pM\n",
7229 				    arsta->addr);
7230 
7231 	/* IEEE80211_STA_ASSOC -> IEEE80211_STA_AUTHORIZED: set peer status as
7232 	 * authorized
7233 	 */
7234 	} else if (old_state == IEEE80211_STA_ASSOC &&
7235 		   new_state == IEEE80211_STA_AUTHORIZED) {
7236 		ret = ath12k_mac_station_authorize(ar, arvif, arsta);
7237 		if (ret) {
7238 			ath12k_warn(ab, "Failed to authorize station: %pM\n",
7239 				    arsta->addr);
7240 			goto exit;
7241 		}
7242 
7243 		if (ath12k_wmi_supports_6ghz_cc_ext(ar) &&
7244 		    arvif->ahvif->vdev_type == WMI_VDEV_TYPE_STA) {
7245 			link_conf = ath12k_mac_get_link_bss_conf(arvif);
7246 			reg_info = ab->reg_info[ar->pdev_idx];
7247 			ath12k_dbg(ab, ATH12K_DBG_MAC, "connection done, update reg rules\n");
7248 			ath12k_hw_to_ah(hw)->regd_updated = false;
7249 			ath12k_reg_handle_chan_list(ab, reg_info, arvif->ahvif->vdev_type,
7250 						    link_conf->power_type);
7251 		}
7252 
7253 	/* IEEE80211_STA_AUTHORIZED -> IEEE80211_STA_ASSOC: station may be in removal,
7254 	 * deauthorize it.
7255 	 */
7256 	} else if (old_state == IEEE80211_STA_AUTHORIZED &&
7257 		   new_state == IEEE80211_STA_ASSOC) {
7258 		ath12k_mac_station_unauthorize(ar, arvif, arsta);
7259 
7260 	/* IEEE80211_STA_ASSOC -> IEEE80211_STA_AUTH: disassoc peer connected to
7261 	 * AP/mesh/ADHOC vif type.
7262 	 */
7263 	} else if (old_state == IEEE80211_STA_ASSOC &&
7264 		   new_state == IEEE80211_STA_AUTH &&
7265 		   (vif->type == NL80211_IFTYPE_AP ||
7266 		    vif->type == NL80211_IFTYPE_MESH_POINT ||
7267 		    vif->type == NL80211_IFTYPE_ADHOC)) {
7268 		ret = ath12k_mac_station_disassoc(ar, arvif, arsta);
7269 		if (ret)
7270 			ath12k_warn(ab, "Failed to disassociate station: %pM\n",
7271 				    arsta->addr);
7272 	}
7273 
7274 exit:
7275 	return ret;
7276 }
7277 
7278 static bool ath12k_mac_is_freq_on_mac(struct ath12k_hw_mode_freq_range_arg *freq_range,
7279 				      u32 freq, u8 mac_id)
7280 {
7281 	return (freq >= freq_range[mac_id].low_2ghz_freq &&
7282 		freq <= freq_range[mac_id].high_2ghz_freq) ||
7283 	       (freq >= freq_range[mac_id].low_5ghz_freq &&
7284 		freq <= freq_range[mac_id].high_5ghz_freq);
7285 }
7286 
7287 static bool
7288 ath12k_mac_2_freq_same_mac_in_freq_range(struct ath12k_base *ab,
7289 					 struct ath12k_hw_mode_freq_range_arg *freq_range,
7290 					 u32 freq_link1, u32 freq_link2)
7291 {
7292 	u8 i;
7293 
7294 	for (i = 0; i < MAX_RADIOS; i++) {
7295 		if (ath12k_mac_is_freq_on_mac(freq_range, freq_link1, i) &&
7296 		    ath12k_mac_is_freq_on_mac(freq_range, freq_link2, i))
7297 			return true;
7298 	}
7299 
7300 	return false;
7301 }
7302 
7303 static bool ath12k_mac_is_hw_dbs_capable(struct ath12k_base *ab)
7304 {
7305 	return test_bit(WMI_TLV_SERVICE_DUAL_BAND_SIMULTANEOUS_SUPPORT,
7306 			ab->wmi_ab.svc_map) &&
7307 	       ab->wmi_ab.hw_mode_info.support_dbs;
7308 }
7309 
7310 static bool ath12k_mac_2_freq_same_mac_in_dbs(struct ath12k_base *ab,
7311 					      u32 freq_link1, u32 freq_link2)
7312 {
7313 	struct ath12k_hw_mode_freq_range_arg *freq_range;
7314 
7315 	if (!ath12k_mac_is_hw_dbs_capable(ab))
7316 		return true;
7317 
7318 	freq_range = ab->wmi_ab.hw_mode_info.freq_range_caps[ATH12K_HW_MODE_DBS];
7319 	return ath12k_mac_2_freq_same_mac_in_freq_range(ab, freq_range,
7320 							freq_link1, freq_link2);
7321 }
7322 
7323 static bool ath12k_mac_is_hw_sbs_capable(struct ath12k_base *ab)
7324 {
7325 	return test_bit(WMI_TLV_SERVICE_DUAL_BAND_SIMULTANEOUS_SUPPORT,
7326 			ab->wmi_ab.svc_map) &&
7327 	       ab->wmi_ab.hw_mode_info.support_sbs;
7328 }
7329 
7330 static bool ath12k_mac_2_freq_same_mac_in_sbs(struct ath12k_base *ab,
7331 					      u32 freq_link1, u32 freq_link2)
7332 {
7333 	struct ath12k_hw_mode_info *info = &ab->wmi_ab.hw_mode_info;
7334 	struct ath12k_hw_mode_freq_range_arg *sbs_uppr_share;
7335 	struct ath12k_hw_mode_freq_range_arg *sbs_low_share;
7336 	struct ath12k_hw_mode_freq_range_arg *sbs_range;
7337 
7338 	if (!ath12k_mac_is_hw_sbs_capable(ab))
7339 		return true;
7340 
7341 	if (ab->wmi_ab.sbs_lower_band_end_freq) {
7342 		sbs_uppr_share = info->freq_range_caps[ATH12K_HW_MODE_SBS_UPPER_SHARE];
7343 		sbs_low_share = info->freq_range_caps[ATH12K_HW_MODE_SBS_LOWER_SHARE];
7344 
7345 		return ath12k_mac_2_freq_same_mac_in_freq_range(ab, sbs_low_share,
7346 								freq_link1, freq_link2) ||
7347 		       ath12k_mac_2_freq_same_mac_in_freq_range(ab, sbs_uppr_share,
7348 								freq_link1, freq_link2);
7349 	}
7350 
7351 	sbs_range = info->freq_range_caps[ATH12K_HW_MODE_SBS];
7352 	return ath12k_mac_2_freq_same_mac_in_freq_range(ab, sbs_range,
7353 							freq_link1, freq_link2);
7354 }
7355 
7356 static bool ath12k_mac_freqs_on_same_mac(struct ath12k_base *ab,
7357 					 u32 freq_link1, u32 freq_link2)
7358 {
7359 	return ath12k_mac_2_freq_same_mac_in_dbs(ab, freq_link1, freq_link2) &&
7360 	       ath12k_mac_2_freq_same_mac_in_sbs(ab, freq_link1, freq_link2);
7361 }
7362 
7363 static int ath12k_mac_mlo_sta_set_link_active(struct ath12k_base *ab,
7364 					      enum wmi_mlo_link_force_reason reason,
7365 					      enum wmi_mlo_link_force_mode mode,
7366 					      u8 *mlo_vdev_id_lst,
7367 					      u8 num_mlo_vdev,
7368 					      u8 *mlo_inactive_vdev_lst,
7369 					      u8 num_mlo_inactive_vdev)
7370 {
7371 	struct wmi_mlo_link_set_active_arg param = {};
7372 	u32 entry_idx, entry_offset, vdev_idx;
7373 	u8 vdev_id;
7374 
7375 	param.reason = reason;
7376 	param.force_mode = mode;
7377 
7378 	for (vdev_idx = 0; vdev_idx < num_mlo_vdev; vdev_idx++) {
7379 		vdev_id = mlo_vdev_id_lst[vdev_idx];
7380 		entry_idx = vdev_id / 32;
7381 		entry_offset = vdev_id % 32;
7382 		if (entry_idx >= WMI_MLO_LINK_NUM_SZ) {
7383 			ath12k_warn(ab, "Invalid entry_idx %d num_mlo_vdev %d vdev %d",
7384 				    entry_idx, num_mlo_vdev, vdev_id);
7385 			return -EINVAL;
7386 		}
7387 		param.vdev_bitmap[entry_idx] |= 1 << entry_offset;
7388 		/* update entry number if entry index changed */
7389 		if (param.num_vdev_bitmap < entry_idx + 1)
7390 			param.num_vdev_bitmap = entry_idx + 1;
7391 	}
7392 
7393 	ath12k_dbg(ab, ATH12K_DBG_MAC,
7394 		   "num_vdev_bitmap %d vdev_bitmap[0] = 0x%x, vdev_bitmap[1] = 0x%x",
7395 		   param.num_vdev_bitmap, param.vdev_bitmap[0], param.vdev_bitmap[1]);
7396 
7397 	if (mode == WMI_MLO_LINK_FORCE_MODE_ACTIVE_INACTIVE) {
7398 		for (vdev_idx = 0; vdev_idx < num_mlo_inactive_vdev; vdev_idx++) {
7399 			vdev_id = mlo_inactive_vdev_lst[vdev_idx];
7400 			entry_idx = vdev_id / 32;
7401 			entry_offset = vdev_id % 32;
7402 			if (entry_idx >= WMI_MLO_LINK_NUM_SZ) {
7403 				ath12k_warn(ab, "Invalid entry_idx %d num_mlo_vdev %d vdev %d",
7404 					    entry_idx, num_mlo_inactive_vdev, vdev_id);
7405 				return -EINVAL;
7406 			}
7407 			param.inactive_vdev_bitmap[entry_idx] |= 1 << entry_offset;
7408 			/* update entry number if entry index changed */
7409 			if (param.num_inactive_vdev_bitmap < entry_idx + 1)
7410 				param.num_inactive_vdev_bitmap = entry_idx + 1;
7411 		}
7412 
7413 		ath12k_dbg(ab, ATH12K_DBG_MAC,
7414 			   "num_vdev_bitmap %d inactive_vdev_bitmap[0] = 0x%x, inactive_vdev_bitmap[1] = 0x%x",
7415 			   param.num_inactive_vdev_bitmap,
7416 			   param.inactive_vdev_bitmap[0],
7417 			   param.inactive_vdev_bitmap[1]);
7418 	}
7419 
7420 	if (mode == WMI_MLO_LINK_FORCE_MODE_ACTIVE_LINK_NUM ||
7421 	    mode == WMI_MLO_LINK_FORCE_MODE_INACTIVE_LINK_NUM) {
7422 		param.num_link_entry = 1;
7423 		param.link_num[0].num_of_link = num_mlo_vdev - 1;
7424 	}
7425 
7426 	return ath12k_wmi_send_mlo_link_set_active_cmd(ab, &param);
7427 }
7428 
7429 static int ath12k_mac_mlo_sta_update_link_active(struct ath12k_base *ab,
7430 						 struct ieee80211_hw *hw,
7431 						 struct ath12k_vif *ahvif)
7432 {
7433 	u8 mlo_vdev_id_lst[IEEE80211_MLD_MAX_NUM_LINKS] = {};
7434 	u32 mlo_freq_list[IEEE80211_MLD_MAX_NUM_LINKS] = {};
7435 	unsigned long links = ahvif->links_map;
7436 	enum wmi_mlo_link_force_reason reason;
7437 	struct ieee80211_chanctx_conf *conf;
7438 	enum wmi_mlo_link_force_mode mode;
7439 	struct ieee80211_bss_conf *info;
7440 	struct ath12k_link_vif *arvif;
7441 	u8 num_mlo_vdev = 0;
7442 	u8 link_id;
7443 
7444 	for_each_set_bit(link_id, &links, IEEE80211_MLD_MAX_NUM_LINKS) {
7445 		arvif = wiphy_dereference(hw->wiphy, ahvif->link[link_id]);
7446 		/* make sure vdev is created on this device */
7447 		if (!arvif || !arvif->is_created || arvif->ar->ab != ab)
7448 			continue;
7449 
7450 		info = ath12k_mac_get_link_bss_conf(arvif);
7451 		conf = wiphy_dereference(hw->wiphy, info->chanctx_conf);
7452 		mlo_freq_list[num_mlo_vdev] = conf->def.chan->center_freq;
7453 
7454 		mlo_vdev_id_lst[num_mlo_vdev] = arvif->vdev_id;
7455 		num_mlo_vdev++;
7456 	}
7457 
7458 	/* It is not allowed to activate more links than a single device
7459 	 * supported. Something goes wrong if we reach here.
7460 	 */
7461 	if (num_mlo_vdev > ATH12K_NUM_MAX_ACTIVE_LINKS_PER_DEVICE) {
7462 		WARN_ON_ONCE(1);
7463 		return -EINVAL;
7464 	}
7465 
7466 	/* if 2 links are established and both link channels fall on the
7467 	 * same hardware MAC, send command to firmware to deactivate one
7468 	 * of them.
7469 	 */
7470 	if (num_mlo_vdev == 2 &&
7471 	    ath12k_mac_freqs_on_same_mac(ab, mlo_freq_list[0],
7472 					 mlo_freq_list[1])) {
7473 		mode = WMI_MLO_LINK_FORCE_MODE_INACTIVE_LINK_NUM;
7474 		reason = WMI_MLO_LINK_FORCE_REASON_NEW_CONNECT;
7475 		return ath12k_mac_mlo_sta_set_link_active(ab, reason, mode,
7476 							  mlo_vdev_id_lst, num_mlo_vdev,
7477 							  NULL, 0);
7478 	}
7479 
7480 	return 0;
7481 }
7482 
7483 static bool ath12k_mac_are_sbs_chan(struct ath12k_base *ab, u32 freq_1, u32 freq_2)
7484 {
7485 	if (!ath12k_mac_is_hw_sbs_capable(ab))
7486 		return false;
7487 
7488 	if (ath12k_is_2ghz_channel_freq(freq_1) ||
7489 	    ath12k_is_2ghz_channel_freq(freq_2))
7490 		return false;
7491 
7492 	return !ath12k_mac_2_freq_same_mac_in_sbs(ab, freq_1, freq_2);
7493 }
7494 
7495 static bool ath12k_mac_are_dbs_chan(struct ath12k_base *ab, u32 freq_1, u32 freq_2)
7496 {
7497 	if (!ath12k_mac_is_hw_dbs_capable(ab))
7498 		return false;
7499 
7500 	return !ath12k_mac_2_freq_same_mac_in_dbs(ab, freq_1, freq_2);
7501 }
7502 
7503 static int ath12k_mac_select_links(struct ath12k_base *ab,
7504 				   struct ieee80211_vif *vif,
7505 				   struct ieee80211_hw *hw,
7506 				   u16 *selected_links)
7507 {
7508 	unsigned long useful_links = ieee80211_vif_usable_links(vif);
7509 	struct ath12k_vif *ahvif = ath12k_vif_to_ahvif(vif);
7510 	u8 num_useful_links = hweight_long(useful_links);
7511 	struct ieee80211_chanctx_conf *chanctx;
7512 	struct ath12k_link_vif *assoc_arvif;
7513 	u32 assoc_link_freq, partner_freq;
7514 	u16 sbs_links = 0, dbs_links = 0;
7515 	struct ieee80211_bss_conf *info;
7516 	struct ieee80211_channel *chan;
7517 	struct ieee80211_sta *sta;
7518 	struct ath12k_sta *ahsta;
7519 	u8 link_id;
7520 
7521 	/* activate all useful links if less than max supported */
7522 	if (num_useful_links <= ATH12K_NUM_MAX_ACTIVE_LINKS_PER_DEVICE) {
7523 		*selected_links = useful_links;
7524 		return 0;
7525 	}
7526 
7527 	/* only in station mode we can get here, so it's safe
7528 	 * to use ap_addr
7529 	 */
7530 	rcu_read_lock();
7531 	sta = ieee80211_find_sta(vif, vif->cfg.ap_addr);
7532 	if (!sta) {
7533 		rcu_read_unlock();
7534 		ath12k_warn(ab, "failed to find sta with addr %pM\n", vif->cfg.ap_addr);
7535 		return -EINVAL;
7536 	}
7537 
7538 	ahsta = ath12k_sta_to_ahsta(sta);
7539 	assoc_arvif = wiphy_dereference(hw->wiphy, ahvif->link[ahsta->assoc_link_id]);
7540 	info = ath12k_mac_get_link_bss_conf(assoc_arvif);
7541 	chanctx = rcu_dereference(info->chanctx_conf);
7542 	assoc_link_freq = chanctx->def.chan->center_freq;
7543 	rcu_read_unlock();
7544 	ath12k_dbg(ab, ATH12K_DBG_MAC, "assoc link %u freq %u\n",
7545 		   assoc_arvif->link_id, assoc_link_freq);
7546 
7547 	/* assoc link is already activated and has to be kept active,
7548 	 * only need to select a partner link from others.
7549 	 */
7550 	useful_links &= ~BIT(assoc_arvif->link_id);
7551 	for_each_set_bit(link_id, &useful_links, IEEE80211_MLD_MAX_NUM_LINKS) {
7552 		info = wiphy_dereference(hw->wiphy, vif->link_conf[link_id]);
7553 		if (!info) {
7554 			ath12k_warn(ab, "failed to get link info for link: %u\n",
7555 				    link_id);
7556 			return -ENOLINK;
7557 		}
7558 
7559 		chan = info->chanreq.oper.chan;
7560 		if (!chan) {
7561 			ath12k_warn(ab, "failed to get chan for link: %u\n", link_id);
7562 			return -EINVAL;
7563 		}
7564 
7565 		partner_freq = chan->center_freq;
7566 		if (ath12k_mac_are_sbs_chan(ab, assoc_link_freq, partner_freq)) {
7567 			sbs_links |= BIT(link_id);
7568 			ath12k_dbg(ab, ATH12K_DBG_MAC, "new SBS link %u freq %u\n",
7569 				   link_id, partner_freq);
7570 			continue;
7571 		}
7572 
7573 		if (ath12k_mac_are_dbs_chan(ab, assoc_link_freq, partner_freq)) {
7574 			dbs_links |= BIT(link_id);
7575 			ath12k_dbg(ab, ATH12K_DBG_MAC, "new DBS link %u freq %u\n",
7576 				   link_id, partner_freq);
7577 			continue;
7578 		}
7579 
7580 		ath12k_dbg(ab, ATH12K_DBG_MAC, "non DBS/SBS link %u freq %u\n",
7581 			   link_id, partner_freq);
7582 	}
7583 
7584 	/* choose the first candidate no matter how many is in the list */
7585 	if (sbs_links)
7586 		link_id = __ffs(sbs_links);
7587 	else if (dbs_links)
7588 		link_id = __ffs(dbs_links);
7589 	else
7590 		link_id = ffs(useful_links) - 1;
7591 
7592 	ath12k_dbg(ab, ATH12K_DBG_MAC, "select partner link %u\n", link_id);
7593 
7594 	*selected_links = BIT(assoc_arvif->link_id) | BIT(link_id);
7595 
7596 	return 0;
7597 }
7598 
7599 int ath12k_mac_op_sta_state(struct ieee80211_hw *hw,
7600 			    struct ieee80211_vif *vif,
7601 			    struct ieee80211_sta *sta,
7602 			    enum ieee80211_sta_state old_state,
7603 			    enum ieee80211_sta_state new_state)
7604 {
7605 	struct ath12k_vif *ahvif = ath12k_vif_to_ahvif(vif);
7606 	struct ath12k_sta *ahsta = ath12k_sta_to_ahsta(sta);
7607 	struct ath12k_hw *ah = ath12k_hw_to_ah(hw);
7608 	struct ath12k_base *prev_ab = NULL, *ab;
7609 	struct ath12k_link_vif *arvif;
7610 	struct ath12k_link_sta *arsta;
7611 	unsigned long valid_links;
7612 	u16 selected_links = 0;
7613 	u8 link_id = 0, i;
7614 	struct ath12k *ar;
7615 	int ret = -EINVAL;
7616 	struct ath12k_dp_peer_create_params dp_params = {};
7617 
7618 	lockdep_assert_wiphy(hw->wiphy);
7619 
7620 	if (ieee80211_vif_is_mld(vif) && sta->valid_links) {
7621 		WARN_ON(!sta->mlo && hweight16(sta->valid_links) != 1);
7622 		link_id = ffs(sta->valid_links) - 1;
7623 	}
7624 
7625 	/* IEEE80211_STA_NOTEXIST -> IEEE80211_STA_NONE:
7626 	 * New station add received. If this is a ML station then
7627 	 * ahsta->links_map will be zero and sta->valid_links will be 1.
7628 	 * Assign default link to the first link sta.
7629 	 */
7630 	if (old_state == IEEE80211_STA_NOTEXIST &&
7631 	    new_state == IEEE80211_STA_NONE) {
7632 		memset(ahsta, 0, sizeof(*ahsta));
7633 
7634 		arsta = &ahsta->deflink;
7635 
7636 		/* ML sta */
7637 		if (sta->mlo && !ahsta->links_map &&
7638 		    (hweight16(sta->valid_links) == 1)) {
7639 			ahsta->ml_peer_id = ath12k_peer_ml_alloc(ah);
7640 			if (ahsta->ml_peer_id == ATH12K_MLO_PEER_ID_INVALID) {
7641 				ath12k_hw_warn(ah, "unable to allocate ML peer id for sta %pM",
7642 					       sta->addr);
7643 				goto exit;
7644 			}
7645 
7646 			dp_params.is_mlo = true;
7647 			dp_params.peer_id = ahsta->ml_peer_id | ATH12K_PEER_ML_ID_VALID;
7648 		}
7649 
7650 		dp_params.sta = sta;
7651 
7652 		if (vif->type == NL80211_IFTYPE_AP)
7653 			dp_params.ucast_ra_only = true;
7654 
7655 		ret = ath12k_dp_peer_create(&ah->dp_hw, sta->addr, &dp_params);
7656 		if (ret) {
7657 			ath12k_hw_warn(ah, "unable to create ath12k_dp_peer for sta %pM, ret: %d",
7658 				       sta->addr, ret);
7659 
7660 			goto ml_peer_id_clear;
7661 		}
7662 
7663 		ret = ath12k_mac_assign_link_sta(ah, ahsta, arsta, ahvif,
7664 						 link_id);
7665 		if (ret) {
7666 			ath12k_hw_warn(ah, "unable assign link %d for sta %pM",
7667 				       link_id, sta->addr);
7668 			goto peer_delete;
7669 		}
7670 
7671 		/* above arsta will get memset, hence do this after assign
7672 		 * link sta
7673 		 */
7674 		if (sta->mlo) {
7675 			/* For station mode, arvif->is_sta_assoc_link has been set when
7676 			 * vdev starts. Make sure the arvif/arsta pair have same setting
7677 			 */
7678 			if (vif->type == NL80211_IFTYPE_STATION &&
7679 			    !arsta->arvif->is_sta_assoc_link) {
7680 				ath12k_hw_warn(ah, "failed to verify assoc link setting with link id %u\n",
7681 					       link_id);
7682 				ret = -EINVAL;
7683 				goto exit;
7684 			}
7685 
7686 			arsta->is_assoc_link = true;
7687 			ahsta->assoc_link_id = link_id;
7688 		}
7689 	}
7690 
7691 	/* In the ML station scenario, activate all partner links once the
7692 	 * client is transitioning to the associated state.
7693 	 *
7694 	 * FIXME: Ideally, this activation should occur when the client
7695 	 * transitions to the authorized state. However, there are some
7696 	 * issues with handling this in the firmware. Until the firmware
7697 	 * can manage it properly, activate the links when the client is
7698 	 * about to move to the associated state.
7699 	 */
7700 	if (ieee80211_vif_is_mld(vif) && vif->type == NL80211_IFTYPE_STATION &&
7701 	    old_state == IEEE80211_STA_AUTH && new_state == IEEE80211_STA_ASSOC) {
7702 		/* TODO: for now only do link selection for single device
7703 		 * MLO case. Other cases would be handled in the future.
7704 		 */
7705 		ab = ah->radio[0].ab;
7706 		if (ab->ag->num_devices == 1) {
7707 			ret = ath12k_mac_select_links(ab, vif, hw, &selected_links);
7708 			if (ret) {
7709 				ath12k_warn(ab,
7710 					    "failed to get selected links: %d\n", ret);
7711 				goto exit;
7712 			}
7713 		} else {
7714 			selected_links = ieee80211_vif_usable_links(vif);
7715 		}
7716 
7717 		ieee80211_set_active_links(vif, selected_links);
7718 	}
7719 
7720 	/* Handle all the other state transitions in generic way */
7721 	valid_links = ahsta->links_map;
7722 	for_each_set_bit(link_id, &valid_links, IEEE80211_MLD_MAX_NUM_LINKS) {
7723 		arvif = wiphy_dereference(hw->wiphy, ahvif->link[link_id]);
7724 		arsta = wiphy_dereference(hw->wiphy, ahsta->link[link_id]);
7725 		/* some assumptions went wrong! */
7726 		if (WARN_ON(!arvif || !arsta))
7727 			continue;
7728 
7729 		/* vdev might be in deleted */
7730 		if (WARN_ON(!arvif->ar))
7731 			continue;
7732 
7733 		ret = ath12k_mac_handle_link_sta_state(hw, arvif, arsta,
7734 						       old_state, new_state);
7735 		if (ret) {
7736 			ath12k_hw_warn(ah, "unable to move link sta %d of sta %pM from state %d to %d",
7737 				       link_id, arsta->addr, old_state, new_state);
7738 
7739 			if (old_state == IEEE80211_STA_NOTEXIST &&
7740 			    new_state == IEEE80211_STA_NONE)
7741 				goto peer_delete;
7742 			else
7743 				goto exit;
7744 		}
7745 	}
7746 
7747 	if (ieee80211_vif_is_mld(vif) && vif->type == NL80211_IFTYPE_STATION &&
7748 	    old_state == IEEE80211_STA_ASSOC && new_state == IEEE80211_STA_AUTHORIZED) {
7749 		for_each_ar(ah, ar, i) {
7750 			ab = ar->ab;
7751 			if (prev_ab == ab)
7752 				continue;
7753 
7754 			ret = ath12k_mac_mlo_sta_update_link_active(ab, hw, ahvif);
7755 			if (ret) {
7756 				ath12k_warn(ab,
7757 					    "failed to update link active state on connect %d\n",
7758 					    ret);
7759 				goto exit;
7760 			}
7761 
7762 			prev_ab = ab;
7763 		}
7764 	}
7765 	/* IEEE80211_STA_NONE -> IEEE80211_STA_NOTEXIST:
7766 	 * Remove the station from driver (handle ML sta here since that
7767 	 * needs special handling. Normal sta will be handled in generic
7768 	 * handler below
7769 	 */
7770 	if (old_state == IEEE80211_STA_NONE &&
7771 	    new_state == IEEE80211_STA_NOTEXIST) {
7772 		if (sta->mlo)
7773 			ath12k_mac_ml_station_remove(ahvif, ahsta);
7774 
7775 		ath12k_dp_peer_delete(&ah->dp_hw, sta->addr, sta);
7776 	}
7777 
7778 	ret = 0;
7779 	goto exit;
7780 
7781 peer_delete:
7782 	ath12k_dp_peer_delete(&ah->dp_hw, sta->addr, sta);
7783 ml_peer_id_clear:
7784 	if (sta->mlo) {
7785 		clear_bit(ahsta->ml_peer_id, ah->free_ml_peer_id_map);
7786 		ahsta->ml_peer_id = ATH12K_MLO_PEER_ID_INVALID;
7787 	}
7788 exit:
7789 	/* update the state if everything went well */
7790 	if (!ret)
7791 		ahsta->state = new_state;
7792 
7793 	return ret;
7794 }
7795 EXPORT_SYMBOL(ath12k_mac_op_sta_state);
7796 
7797 int ath12k_mac_op_sta_set_txpwr(struct ieee80211_hw *hw,
7798 				struct ieee80211_vif *vif,
7799 				struct ieee80211_sta *sta)
7800 {
7801 	struct ath12k_sta *ahsta = ath12k_sta_to_ahsta(sta);
7802 	struct ath12k *ar;
7803 	struct ath12k_vif *ahvif = ath12k_vif_to_ahvif(vif);
7804 	struct ath12k_link_vif *arvif;
7805 	struct ath12k_link_sta *arsta;
7806 	u8 link_id;
7807 	int ret;
7808 	s16 txpwr;
7809 
7810 	lockdep_assert_wiphy(hw->wiphy);
7811 
7812 	/* TODO: use link id from mac80211 once that's implemented */
7813 	link_id = 0;
7814 
7815 	arvif = wiphy_dereference(hw->wiphy, ahvif->link[link_id]);
7816 	arsta = wiphy_dereference(hw->wiphy, ahsta->link[link_id]);
7817 
7818 	if (sta->deflink.txpwr.type == NL80211_TX_POWER_AUTOMATIC) {
7819 		txpwr = 0;
7820 	} else {
7821 		txpwr = sta->deflink.txpwr.power;
7822 		if (!txpwr) {
7823 			ret = -EINVAL;
7824 			goto out;
7825 		}
7826 	}
7827 
7828 	if (txpwr > ATH12K_TX_POWER_MAX_VAL || txpwr < ATH12K_TX_POWER_MIN_VAL) {
7829 		ret = -EINVAL;
7830 		goto out;
7831 	}
7832 
7833 	ar = arvif->ar;
7834 
7835 	ret = ath12k_wmi_set_peer_param(ar, arsta->addr, arvif->vdev_id,
7836 					WMI_PEER_USE_FIXED_PWR, txpwr);
7837 	if (ret) {
7838 		ath12k_warn(ar->ab, "failed to set tx power for station ret: %d\n",
7839 			    ret);
7840 		goto out;
7841 	}
7842 
7843 out:
7844 	return ret;
7845 }
7846 EXPORT_SYMBOL(ath12k_mac_op_sta_set_txpwr);
7847 
7848 void ath12k_mac_op_link_sta_rc_update(struct ieee80211_hw *hw,
7849 				      struct ieee80211_vif *vif,
7850 				      struct ieee80211_link_sta *link_sta,
7851 				      u32 changed)
7852 {
7853 	struct ieee80211_sta *sta = link_sta->sta;
7854 	struct ath12k *ar;
7855 	struct ath12k_sta *ahsta = ath12k_sta_to_ahsta(sta);
7856 	struct ath12k_vif *ahvif = ath12k_vif_to_ahvif(vif);
7857 	struct ath12k_hw *ah = ath12k_hw_to_ah(hw);
7858 	struct ath12k_link_sta *arsta;
7859 	struct ath12k_link_vif *arvif;
7860 	struct ath12k_dp_link_peer *peer;
7861 	u32 bw, smps;
7862 	struct ath12k_dp *dp;
7863 
7864 	rcu_read_lock();
7865 	arvif = rcu_dereference(ahvif->link[link_sta->link_id]);
7866 	if (!arvif) {
7867 		ath12k_hw_warn(ah, "mac sta rc update failed to fetch link vif on link id %u for peer %pM\n",
7868 			       link_sta->link_id, sta->addr);
7869 		rcu_read_unlock();
7870 		return;
7871 	}
7872 
7873 	ar = arvif->ar;
7874 	dp = ath12k_ab_to_dp(ar->ab);
7875 
7876 	arsta = rcu_dereference(ahsta->link[link_sta->link_id]);
7877 	if (!arsta) {
7878 		rcu_read_unlock();
7879 		ath12k_warn(ar->ab, "mac sta rc update failed to fetch link sta on link id %u for peer %pM\n",
7880 			    link_sta->link_id, sta->addr);
7881 		return;
7882 	}
7883 	spin_lock_bh(&dp->dp_lock);
7884 
7885 	peer = ath12k_dp_link_peer_find_by_vdev_and_addr(dp, arvif->vdev_id,
7886 							 arsta->addr);
7887 	if (!peer) {
7888 		spin_unlock_bh(&dp->dp_lock);
7889 		rcu_read_unlock();
7890 		ath12k_warn(ar->ab, "mac sta rc update failed to find peer %pM on vdev %i\n",
7891 			    arsta->addr, arvif->vdev_id);
7892 		return;
7893 	}
7894 
7895 	spin_unlock_bh(&dp->dp_lock);
7896 
7897 	if (arsta->link_id >= IEEE80211_MLD_MAX_NUM_LINKS) {
7898 		rcu_read_unlock();
7899 		return;
7900 	}
7901 
7902 	link_sta = rcu_dereference(sta->link[arsta->link_id]);
7903 	if (!link_sta) {
7904 		rcu_read_unlock();
7905 		ath12k_warn(ar->ab, "unable to access link sta in rc update for sta %pM link %u\n",
7906 			    sta->addr, arsta->link_id);
7907 		return;
7908 	}
7909 
7910 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
7911 		   "mac sta rc update for %pM changed %08x bw %d nss %d smps %d\n",
7912 		   arsta->addr, changed, link_sta->bandwidth, link_sta->rx_nss,
7913 		   link_sta->smps_mode);
7914 
7915 	spin_lock_bh(&ar->data_lock);
7916 
7917 	if (changed & IEEE80211_RC_BW_CHANGED) {
7918 		bw = ath12k_mac_ieee80211_sta_bw_to_wmi(ar, link_sta);
7919 		arsta->bw_prev = arsta->bw;
7920 		arsta->bw = bw;
7921 	}
7922 
7923 	if (changed & IEEE80211_RC_NSS_CHANGED)
7924 		arsta->nss = link_sta->rx_nss;
7925 
7926 	if (changed & IEEE80211_RC_SMPS_CHANGED) {
7927 		smps = WMI_PEER_SMPS_PS_NONE;
7928 
7929 		switch (link_sta->smps_mode) {
7930 		case IEEE80211_SMPS_AUTOMATIC:
7931 		case IEEE80211_SMPS_OFF:
7932 			smps = WMI_PEER_SMPS_PS_NONE;
7933 			break;
7934 		case IEEE80211_SMPS_STATIC:
7935 			smps = WMI_PEER_SMPS_STATIC;
7936 			break;
7937 		case IEEE80211_SMPS_DYNAMIC:
7938 			smps = WMI_PEER_SMPS_DYNAMIC;
7939 			break;
7940 		default:
7941 			ath12k_warn(ar->ab, "Invalid smps %d in sta rc update for %pM link %u\n",
7942 				    link_sta->smps_mode, arsta->addr, link_sta->link_id);
7943 			smps = WMI_PEER_SMPS_PS_NONE;
7944 			break;
7945 		}
7946 
7947 		arsta->smps = smps;
7948 	}
7949 
7950 	arsta->changed |= changed;
7951 
7952 	spin_unlock_bh(&ar->data_lock);
7953 
7954 	wiphy_work_queue(hw->wiphy, &arsta->update_wk);
7955 
7956 	rcu_read_unlock();
7957 }
7958 EXPORT_SYMBOL(ath12k_mac_op_link_sta_rc_update);
7959 
7960 static struct ath12k_link_sta *ath12k_mac_alloc_assign_link_sta(struct ath12k_hw *ah,
7961 								struct ath12k_sta *ahsta,
7962 								struct ath12k_vif *ahvif,
7963 								u8 link_id)
7964 {
7965 	struct ath12k_link_sta *arsta;
7966 	int ret;
7967 
7968 	lockdep_assert_wiphy(ah->hw->wiphy);
7969 
7970 	if (link_id >= IEEE80211_MLD_MAX_NUM_LINKS)
7971 		return NULL;
7972 
7973 	arsta = wiphy_dereference(ah->hw->wiphy, ahsta->link[link_id]);
7974 	if (arsta)
7975 		return NULL;
7976 
7977 	arsta = kmalloc(sizeof(*arsta), GFP_KERNEL);
7978 	if (!arsta)
7979 		return NULL;
7980 
7981 	ret = ath12k_mac_assign_link_sta(ah, ahsta, arsta, ahvif, link_id);
7982 	if (ret) {
7983 		kfree(arsta);
7984 		return NULL;
7985 	}
7986 
7987 	return arsta;
7988 }
7989 
7990 int ath12k_mac_op_change_sta_links(struct ieee80211_hw *hw,
7991 				   struct ieee80211_vif *vif,
7992 				   struct ieee80211_sta *sta,
7993 				   u16 old_links, u16 new_links)
7994 {
7995 	struct ath12k_vif *ahvif = ath12k_vif_to_ahvif(vif);
7996 	struct ath12k_sta *ahsta = ath12k_sta_to_ahsta(sta);
7997 	struct ath12k_hw *ah = hw->priv;
7998 	struct ath12k_link_vif *arvif;
7999 	struct ath12k_link_sta *arsta;
8000 	unsigned long valid_links;
8001 	struct ath12k *ar;
8002 	u8 link_id;
8003 	int ret;
8004 
8005 	lockdep_assert_wiphy(hw->wiphy);
8006 
8007 	if (!sta->valid_links)
8008 		return -EINVAL;
8009 
8010 	/* Firmware does not support removal of one of link stas. All sta
8011 	 * would be removed during ML STA delete in sta_state(), hence link
8012 	 * sta removal is not handled here.
8013 	 */
8014 	if (new_links < old_links)
8015 		return 0;
8016 
8017 	if (ahsta->ml_peer_id == ATH12K_MLO_PEER_ID_INVALID) {
8018 		ath12k_hw_warn(ah, "unable to add link for ml sta %pM", sta->addr);
8019 		return -EINVAL;
8020 	}
8021 
8022 	/* this op is expected only after initial sta insertion with default link */
8023 	if (WARN_ON(ahsta->links_map == 0))
8024 		return -EINVAL;
8025 
8026 	valid_links = new_links;
8027 	for_each_set_bit(link_id, &valid_links, IEEE80211_MLD_MAX_NUM_LINKS) {
8028 		if (ahsta->links_map & BIT(link_id))
8029 			continue;
8030 
8031 		arvif = wiphy_dereference(hw->wiphy, ahvif->link[link_id]);
8032 		arsta = ath12k_mac_alloc_assign_link_sta(ah, ahsta, ahvif, link_id);
8033 
8034 		if (!arvif || !arsta) {
8035 			ath12k_hw_warn(ah, "Failed to alloc/assign link sta");
8036 			continue;
8037 		}
8038 
8039 		ar = arvif->ar;
8040 		if (!ar)
8041 			continue;
8042 
8043 		ret = ath12k_mac_station_add(ar, arvif, arsta);
8044 		if (ret) {
8045 			ath12k_warn(ar->ab, "Failed to add station: %pM for VDEV: %d\n",
8046 				    arsta->addr, arvif->vdev_id);
8047 			ath12k_mac_free_unassign_link_sta(ah, ahsta, link_id);
8048 			return ret;
8049 		}
8050 	}
8051 
8052 	return 0;
8053 }
8054 EXPORT_SYMBOL(ath12k_mac_op_change_sta_links);
8055 
8056 bool ath12k_mac_op_can_activate_links(struct ieee80211_hw *hw,
8057 				      struct ieee80211_vif *vif,
8058 				      u16 active_links)
8059 {
8060 	/* TODO: Handle recovery case */
8061 
8062 	return true;
8063 }
8064 EXPORT_SYMBOL(ath12k_mac_op_can_activate_links);
8065 
8066 static int ath12k_conf_tx_uapsd(struct ath12k_link_vif *arvif,
8067 				u16 ac, bool enable)
8068 {
8069 	struct ath12k *ar = arvif->ar;
8070 	struct ath12k_vif *ahvif = arvif->ahvif;
8071 	u32 value;
8072 	int ret;
8073 
8074 	if (ahvif->vdev_type != WMI_VDEV_TYPE_STA)
8075 		return 0;
8076 
8077 	switch (ac) {
8078 	case IEEE80211_AC_VO:
8079 		value = WMI_STA_PS_UAPSD_AC3_DELIVERY_EN |
8080 			WMI_STA_PS_UAPSD_AC3_TRIGGER_EN;
8081 		break;
8082 	case IEEE80211_AC_VI:
8083 		value = WMI_STA_PS_UAPSD_AC2_DELIVERY_EN |
8084 			WMI_STA_PS_UAPSD_AC2_TRIGGER_EN;
8085 		break;
8086 	case IEEE80211_AC_BE:
8087 		value = WMI_STA_PS_UAPSD_AC1_DELIVERY_EN |
8088 			WMI_STA_PS_UAPSD_AC1_TRIGGER_EN;
8089 		break;
8090 	case IEEE80211_AC_BK:
8091 		value = WMI_STA_PS_UAPSD_AC0_DELIVERY_EN |
8092 			WMI_STA_PS_UAPSD_AC0_TRIGGER_EN;
8093 		break;
8094 	}
8095 
8096 	if (enable)
8097 		ahvif->u.sta.uapsd |= value;
8098 	else
8099 		ahvif->u.sta.uapsd &= ~value;
8100 
8101 	ret = ath12k_wmi_set_sta_ps_param(ar, arvif->vdev_id,
8102 					  WMI_STA_PS_PARAM_UAPSD,
8103 					  ahvif->u.sta.uapsd);
8104 	if (ret) {
8105 		ath12k_warn(ar->ab, "could not set uapsd params %d\n", ret);
8106 		goto exit;
8107 	}
8108 
8109 	if (ahvif->u.sta.uapsd)
8110 		value = WMI_STA_PS_RX_WAKE_POLICY_POLL_UAPSD;
8111 	else
8112 		value = WMI_STA_PS_RX_WAKE_POLICY_WAKE;
8113 
8114 	ret = ath12k_wmi_set_sta_ps_param(ar, arvif->vdev_id,
8115 					  WMI_STA_PS_PARAM_RX_WAKE_POLICY,
8116 					  value);
8117 	if (ret)
8118 		ath12k_warn(ar->ab, "could not set rx wake param %d\n", ret);
8119 
8120 exit:
8121 	return ret;
8122 }
8123 
8124 static int ath12k_mac_conf_tx(struct ath12k_link_vif *arvif, u16 ac,
8125 			      const struct ieee80211_tx_queue_params *params)
8126 {
8127 	struct wmi_wmm_params_arg *p = NULL;
8128 	struct ath12k *ar = arvif->ar;
8129 	struct ath12k_base *ab = ar->ab;
8130 	int ret;
8131 
8132 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
8133 
8134 	switch (ac) {
8135 	case IEEE80211_AC_VO:
8136 		p = &arvif->wmm_params.ac_vo;
8137 		break;
8138 	case IEEE80211_AC_VI:
8139 		p = &arvif->wmm_params.ac_vi;
8140 		break;
8141 	case IEEE80211_AC_BE:
8142 		p = &arvif->wmm_params.ac_be;
8143 		break;
8144 	case IEEE80211_AC_BK:
8145 		p = &arvif->wmm_params.ac_bk;
8146 		break;
8147 	}
8148 
8149 	if (WARN_ON(!p)) {
8150 		ret = -EINVAL;
8151 		goto exit;
8152 	}
8153 
8154 	p->cwmin = params->cw_min;
8155 	p->cwmax = params->cw_max;
8156 	p->aifs = params->aifs;
8157 	p->txop = params->txop;
8158 
8159 	ret = ath12k_wmi_send_wmm_update_cmd(ar, arvif->vdev_id,
8160 					     &arvif->wmm_params);
8161 	if (ret) {
8162 		ath12k_warn(ab, "pdev idx %d failed to set wmm params: %d\n",
8163 			    ar->pdev_idx, ret);
8164 		goto exit;
8165 	}
8166 
8167 	ret = ath12k_conf_tx_uapsd(arvif, ac, params->uapsd);
8168 	if (ret)
8169 		ath12k_warn(ab, "pdev idx %d failed to set sta uapsd: %d\n",
8170 			    ar->pdev_idx, ret);
8171 
8172 exit:
8173 	return ret;
8174 }
8175 
8176 int ath12k_mac_op_conf_tx(struct ieee80211_hw *hw,
8177 			  struct ieee80211_vif *vif,
8178 			  unsigned int link_id, u16 ac,
8179 			  const struct ieee80211_tx_queue_params *params)
8180 {
8181 	struct ath12k_vif *ahvif = ath12k_vif_to_ahvif(vif);
8182 	struct ath12k_link_vif *arvif;
8183 	struct ath12k_vif_cache *cache;
8184 	int ret;
8185 
8186 	lockdep_assert_wiphy(hw->wiphy);
8187 
8188 	if (link_id >= IEEE80211_MLD_MAX_NUM_LINKS)
8189 		return -EINVAL;
8190 
8191 	arvif = wiphy_dereference(hw->wiphy, ahvif->link[link_id]);
8192 	if (!arvif || !arvif->is_created) {
8193 		cache = ath12k_ahvif_get_link_cache(ahvif, link_id);
8194 		if (!cache)
8195 			return -ENOSPC;
8196 
8197 		cache->tx_conf.changed = true;
8198 		cache->tx_conf.ac = ac;
8199 		cache->tx_conf.tx_queue_params = *params;
8200 
8201 		return 0;
8202 	}
8203 
8204 	ret = ath12k_mac_conf_tx(arvif, ac, params);
8205 
8206 	return ret;
8207 }
8208 EXPORT_SYMBOL(ath12k_mac_op_conf_tx);
8209 
8210 static struct ieee80211_sta_ht_cap
8211 ath12k_create_ht_cap(struct ath12k *ar, u32 ar_ht_cap, u32 rate_cap_rx_chainmask)
8212 {
8213 	int i;
8214 	struct ieee80211_sta_ht_cap ht_cap = {};
8215 	u32 ar_vht_cap = ar->pdev->cap.vht_cap;
8216 
8217 	if (!(ar_ht_cap & WMI_HT_CAP_ENABLED))
8218 		return ht_cap;
8219 
8220 	ht_cap.ht_supported = 1;
8221 	ht_cap.ampdu_factor = IEEE80211_HT_MAX_AMPDU_64K;
8222 	ht_cap.ampdu_density = IEEE80211_HT_MPDU_DENSITY_NONE;
8223 	ht_cap.cap |= IEEE80211_HT_CAP_SUP_WIDTH_20_40;
8224 	ht_cap.cap |= IEEE80211_HT_CAP_DSSSCCK40;
8225 	ht_cap.cap |= WLAN_HT_CAP_SM_PS_STATIC << IEEE80211_HT_CAP_SM_PS_SHIFT;
8226 
8227 	if (ar_ht_cap & WMI_HT_CAP_HT20_SGI)
8228 		ht_cap.cap |= IEEE80211_HT_CAP_SGI_20;
8229 
8230 	if (ar_ht_cap & WMI_HT_CAP_HT40_SGI)
8231 		ht_cap.cap |= IEEE80211_HT_CAP_SGI_40;
8232 
8233 	if (ar_ht_cap & WMI_HT_CAP_DYNAMIC_SMPS) {
8234 		u32 smps;
8235 
8236 		smps   = WLAN_HT_CAP_SM_PS_DYNAMIC;
8237 		smps <<= IEEE80211_HT_CAP_SM_PS_SHIFT;
8238 
8239 		ht_cap.cap |= smps;
8240 	}
8241 
8242 	if (ar_ht_cap & WMI_HT_CAP_TX_STBC)
8243 		ht_cap.cap |= IEEE80211_HT_CAP_TX_STBC;
8244 
8245 	if (ar_ht_cap & WMI_HT_CAP_RX_STBC) {
8246 		u32 stbc;
8247 
8248 		stbc   = ar_ht_cap;
8249 		stbc  &= WMI_HT_CAP_RX_STBC;
8250 		stbc >>= WMI_HT_CAP_RX_STBC_MASK_SHIFT;
8251 		stbc <<= IEEE80211_HT_CAP_RX_STBC_SHIFT;
8252 		stbc  &= IEEE80211_HT_CAP_RX_STBC;
8253 
8254 		ht_cap.cap |= stbc;
8255 	}
8256 
8257 	if (ar_ht_cap & WMI_HT_CAP_RX_LDPC)
8258 		ht_cap.cap |= IEEE80211_HT_CAP_LDPC_CODING;
8259 
8260 	if (ar_ht_cap & WMI_HT_CAP_L_SIG_TXOP_PROT)
8261 		ht_cap.cap |= IEEE80211_HT_CAP_LSIG_TXOP_PROT;
8262 
8263 	if (ar_vht_cap & WMI_VHT_CAP_MAX_MPDU_LEN_MASK)
8264 		ht_cap.cap |= IEEE80211_HT_CAP_MAX_AMSDU;
8265 
8266 	for (i = 0; i < ar->num_rx_chains; i++) {
8267 		if (rate_cap_rx_chainmask & BIT(i))
8268 			ht_cap.mcs.rx_mask[i] = 0xFF;
8269 	}
8270 
8271 	ht_cap.mcs.tx_params |= IEEE80211_HT_MCS_TX_DEFINED;
8272 
8273 	return ht_cap;
8274 }
8275 
8276 static int ath12k_mac_set_txbf_conf(struct ath12k_link_vif *arvif)
8277 {
8278 	u32 value = 0;
8279 	struct ath12k *ar = arvif->ar;
8280 	struct ath12k_vif *ahvif = arvif->ahvif;
8281 	int nsts;
8282 	int sound_dim;
8283 	u32 vht_cap = ar->pdev->cap.vht_cap;
8284 	u32 vdev_param = WMI_VDEV_PARAM_TXBF;
8285 
8286 	if (vht_cap & (IEEE80211_VHT_CAP_SU_BEAMFORMEE_CAPABLE)) {
8287 		nsts = vht_cap & IEEE80211_VHT_CAP_BEAMFORMEE_STS_MASK;
8288 		nsts >>= IEEE80211_VHT_CAP_BEAMFORMEE_STS_SHIFT;
8289 		value |= SM(nsts, WMI_TXBF_STS_CAP_OFFSET);
8290 	}
8291 
8292 	if (vht_cap & (IEEE80211_VHT_CAP_SU_BEAMFORMER_CAPABLE)) {
8293 		sound_dim = vht_cap &
8294 			    IEEE80211_VHT_CAP_SOUNDING_DIMENSIONS_MASK;
8295 		sound_dim >>= IEEE80211_VHT_CAP_SOUNDING_DIMENSIONS_SHIFT;
8296 		if (sound_dim > (ar->num_tx_chains - 1))
8297 			sound_dim = ar->num_tx_chains - 1;
8298 		value |= SM(sound_dim, WMI_BF_SOUND_DIM_OFFSET);
8299 	}
8300 
8301 	if (!value)
8302 		return 0;
8303 
8304 	if (vht_cap & IEEE80211_VHT_CAP_SU_BEAMFORMER_CAPABLE) {
8305 		value |= WMI_VDEV_PARAM_TXBF_SU_TX_BFER;
8306 
8307 		if ((vht_cap & IEEE80211_VHT_CAP_MU_BEAMFORMER_CAPABLE) &&
8308 		    ahvif->vdev_type == WMI_VDEV_TYPE_AP)
8309 			value |= WMI_VDEV_PARAM_TXBF_MU_TX_BFER;
8310 	}
8311 
8312 	if (vht_cap & IEEE80211_VHT_CAP_SU_BEAMFORMEE_CAPABLE) {
8313 		value |= WMI_VDEV_PARAM_TXBF_SU_TX_BFEE;
8314 
8315 		if ((vht_cap & IEEE80211_VHT_CAP_MU_BEAMFORMEE_CAPABLE) &&
8316 		    ahvif->vdev_type == WMI_VDEV_TYPE_STA)
8317 			value |= WMI_VDEV_PARAM_TXBF_MU_TX_BFEE;
8318 	}
8319 
8320 	return ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
8321 					     vdev_param, value);
8322 }
8323 
8324 static void ath12k_set_vht_txbf_cap(struct ath12k *ar, u32 *vht_cap)
8325 {
8326 	bool subfer, subfee;
8327 	int sound_dim = 0;
8328 
8329 	subfer = !!(*vht_cap & (IEEE80211_VHT_CAP_SU_BEAMFORMER_CAPABLE));
8330 	subfee = !!(*vht_cap & (IEEE80211_VHT_CAP_SU_BEAMFORMEE_CAPABLE));
8331 
8332 	if (ar->num_tx_chains < 2) {
8333 		*vht_cap &= ~(IEEE80211_VHT_CAP_SU_BEAMFORMER_CAPABLE);
8334 		subfer = false;
8335 	}
8336 
8337 	/* If SU Beaformer is not set, then disable MU Beamformer Capability */
8338 	if (!subfer)
8339 		*vht_cap &= ~(IEEE80211_VHT_CAP_MU_BEAMFORMER_CAPABLE);
8340 
8341 	/* If SU Beaformee is not set, then disable MU Beamformee Capability */
8342 	if (!subfee)
8343 		*vht_cap &= ~(IEEE80211_VHT_CAP_MU_BEAMFORMEE_CAPABLE);
8344 
8345 	sound_dim = u32_get_bits(*vht_cap,
8346 				 IEEE80211_VHT_CAP_SOUNDING_DIMENSIONS_MASK);
8347 	*vht_cap = u32_replace_bits(*vht_cap, 0,
8348 				    IEEE80211_VHT_CAP_SOUNDING_DIMENSIONS_MASK);
8349 
8350 	/* TODO: Need to check invalid STS and Sound_dim values set by FW? */
8351 
8352 	/* Enable Sounding Dimension Field only if SU BF is enabled */
8353 	if (subfer) {
8354 		if (sound_dim > (ar->num_tx_chains - 1))
8355 			sound_dim = ar->num_tx_chains - 1;
8356 
8357 		*vht_cap = u32_replace_bits(*vht_cap, sound_dim,
8358 					    IEEE80211_VHT_CAP_SOUNDING_DIMENSIONS_MASK);
8359 	}
8360 
8361 	/* Use the STS advertised by FW unless SU Beamformee is not supported*/
8362 	if (!subfee)
8363 		*vht_cap &= ~(IEEE80211_VHT_CAP_BEAMFORMEE_STS_MASK);
8364 }
8365 
8366 static struct ieee80211_sta_vht_cap
8367 ath12k_create_vht_cap(struct ath12k *ar, u32 rate_cap_tx_chainmask,
8368 		      u32 rate_cap_rx_chainmask)
8369 {
8370 	struct ieee80211_sta_vht_cap vht_cap = {};
8371 	u16 txmcs_map, rxmcs_map;
8372 	int i;
8373 
8374 	vht_cap.vht_supported = 1;
8375 	vht_cap.cap = ar->pdev->cap.vht_cap;
8376 
8377 	ath12k_set_vht_txbf_cap(ar, &vht_cap.cap);
8378 
8379 	/* 80P80 is not supported */
8380 	vht_cap.cap &= ~IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_160_80PLUS80MHZ;
8381 
8382 	rxmcs_map = 0;
8383 	txmcs_map = 0;
8384 	for (i = 0; i < 8; i++) {
8385 		if (i < ar->num_tx_chains && rate_cap_tx_chainmask & BIT(i))
8386 			txmcs_map |= IEEE80211_VHT_MCS_SUPPORT_0_9 << (i * 2);
8387 		else
8388 			txmcs_map |= IEEE80211_VHT_MCS_NOT_SUPPORTED << (i * 2);
8389 
8390 		if (i < ar->num_rx_chains && rate_cap_rx_chainmask & BIT(i))
8391 			rxmcs_map |= IEEE80211_VHT_MCS_SUPPORT_0_9 << (i * 2);
8392 		else
8393 			rxmcs_map |= IEEE80211_VHT_MCS_NOT_SUPPORTED << (i * 2);
8394 	}
8395 
8396 	if (rate_cap_tx_chainmask <= 1)
8397 		vht_cap.cap &= ~IEEE80211_VHT_CAP_TXSTBC;
8398 
8399 	vht_cap.vht_mcs.rx_mcs_map = cpu_to_le16(rxmcs_map);
8400 	vht_cap.vht_mcs.tx_mcs_map = cpu_to_le16(txmcs_map);
8401 
8402 	/* Check if the HW supports 1:1 NSS ratio and reset
8403 	 * EXT NSS BW Support field to 0 to indicate 1:1 ratio
8404 	 */
8405 	if (ar->pdev->cap.nss_ratio_info == WMI_NSS_RATIO_1_NSS)
8406 		vht_cap.cap &= ~IEEE80211_VHT_CAP_EXT_NSS_BW_MASK;
8407 
8408 	return vht_cap;
8409 }
8410 
8411 static void ath12k_mac_setup_ht_vht_cap(struct ath12k *ar,
8412 					struct ath12k_pdev_cap *cap,
8413 					u32 *ht_cap_info)
8414 {
8415 	struct ieee80211_supported_band *band;
8416 	u32 rate_cap_tx_chainmask;
8417 	u32 rate_cap_rx_chainmask;
8418 	u32 ht_cap;
8419 
8420 	rate_cap_tx_chainmask = ar->cfg_tx_chainmask >> cap->tx_chain_mask_shift;
8421 	rate_cap_rx_chainmask = ar->cfg_rx_chainmask >> cap->rx_chain_mask_shift;
8422 
8423 	if (cap->supported_bands & WMI_HOST_WLAN_2GHZ_CAP) {
8424 		band = &ar->mac.sbands[NL80211_BAND_2GHZ];
8425 		ht_cap = cap->band[NL80211_BAND_2GHZ].ht_cap_info;
8426 		if (ht_cap_info)
8427 			*ht_cap_info = ht_cap;
8428 		band->ht_cap = ath12k_create_ht_cap(ar, ht_cap,
8429 						    rate_cap_rx_chainmask);
8430 	}
8431 
8432 	if (cap->supported_bands & WMI_HOST_WLAN_5GHZ_CAP &&
8433 	    (ar->ab->hw_params->single_pdev_only ||
8434 	     !ar->supports_6ghz)) {
8435 		band = &ar->mac.sbands[NL80211_BAND_5GHZ];
8436 		ht_cap = cap->band[NL80211_BAND_5GHZ].ht_cap_info;
8437 		if (ht_cap_info)
8438 			*ht_cap_info = ht_cap;
8439 		band->ht_cap = ath12k_create_ht_cap(ar, ht_cap,
8440 						    rate_cap_rx_chainmask);
8441 		band->vht_cap = ath12k_create_vht_cap(ar, rate_cap_tx_chainmask,
8442 						      rate_cap_rx_chainmask);
8443 	}
8444 }
8445 
8446 static int ath12k_check_chain_mask(struct ath12k *ar, u32 ant, bool is_tx_ant)
8447 {
8448 	/* TODO: Check the request chainmask against the supported
8449 	 * chainmask table which is advertised in extented_service_ready event
8450 	 */
8451 
8452 	return 0;
8453 }
8454 
8455 static void ath12k_gen_ppe_thresh(struct ath12k_wmi_ppe_threshold_arg *fw_ppet,
8456 				  u8 *he_ppet)
8457 {
8458 	int nss, ru;
8459 	u8 bit = 7;
8460 
8461 	he_ppet[0] = fw_ppet->numss_m1 & IEEE80211_PPE_THRES_NSS_MASK;
8462 	he_ppet[0] |= (fw_ppet->ru_bit_mask <<
8463 		       IEEE80211_PPE_THRES_RU_INDEX_BITMASK_POS) &
8464 		      IEEE80211_PPE_THRES_RU_INDEX_BITMASK_MASK;
8465 	for (nss = 0; nss <= fw_ppet->numss_m1; nss++) {
8466 		for (ru = 0; ru < 4; ru++) {
8467 			u8 val;
8468 			int i;
8469 
8470 			if ((fw_ppet->ru_bit_mask & BIT(ru)) == 0)
8471 				continue;
8472 			val = (fw_ppet->ppet16_ppet8_ru3_ru0[nss] >> (ru * 6)) &
8473 			       0x3f;
8474 			val = ((val >> 3) & 0x7) | ((val & 0x7) << 3);
8475 			for (i = 5; i >= 0; i--) {
8476 				he_ppet[bit / 8] |=
8477 					((val >> i) & 0x1) << ((bit % 8));
8478 				bit++;
8479 			}
8480 		}
8481 	}
8482 }
8483 
8484 static void
8485 ath12k_mac_filter_he_cap_mesh(struct ieee80211_he_cap_elem *he_cap_elem)
8486 {
8487 	u8 m;
8488 
8489 	m = IEEE80211_HE_MAC_CAP0_TWT_RES |
8490 	    IEEE80211_HE_MAC_CAP0_TWT_REQ;
8491 	he_cap_elem->mac_cap_info[0] &= ~m;
8492 
8493 	m = IEEE80211_HE_MAC_CAP2_TRS |
8494 	    IEEE80211_HE_MAC_CAP2_BCAST_TWT |
8495 	    IEEE80211_HE_MAC_CAP2_MU_CASCADING;
8496 	he_cap_elem->mac_cap_info[2] &= ~m;
8497 
8498 	m = IEEE80211_HE_MAC_CAP3_FLEX_TWT_SCHED |
8499 	    IEEE80211_HE_MAC_CAP2_BCAST_TWT |
8500 	    IEEE80211_HE_MAC_CAP2_MU_CASCADING;
8501 	he_cap_elem->mac_cap_info[3] &= ~m;
8502 
8503 	m = IEEE80211_HE_MAC_CAP4_BSRP_BQRP_A_MPDU_AGG |
8504 	    IEEE80211_HE_MAC_CAP4_BQR;
8505 	he_cap_elem->mac_cap_info[4] &= ~m;
8506 
8507 	m = IEEE80211_HE_MAC_CAP5_SUBCHAN_SELECTIVE_TRANSMISSION |
8508 	    IEEE80211_HE_MAC_CAP5_UL_2x996_TONE_RU |
8509 	    IEEE80211_HE_MAC_CAP5_PUNCTURED_SOUNDING |
8510 	    IEEE80211_HE_MAC_CAP5_HT_VHT_TRIG_FRAME_RX;
8511 	he_cap_elem->mac_cap_info[5] &= ~m;
8512 
8513 	m = IEEE80211_HE_PHY_CAP2_UL_MU_FULL_MU_MIMO |
8514 	    IEEE80211_HE_PHY_CAP2_UL_MU_PARTIAL_MU_MIMO;
8515 	he_cap_elem->phy_cap_info[2] &= ~m;
8516 
8517 	m = IEEE80211_HE_PHY_CAP3_RX_PARTIAL_BW_SU_IN_20MHZ_MU |
8518 	    IEEE80211_HE_PHY_CAP3_DCM_MAX_CONST_TX_MASK |
8519 	    IEEE80211_HE_PHY_CAP3_DCM_MAX_CONST_RX_MASK;
8520 	he_cap_elem->phy_cap_info[3] &= ~m;
8521 
8522 	m = IEEE80211_HE_PHY_CAP4_MU_BEAMFORMER;
8523 	he_cap_elem->phy_cap_info[4] &= ~m;
8524 
8525 	m = IEEE80211_HE_PHY_CAP5_NG16_MU_FEEDBACK;
8526 	he_cap_elem->phy_cap_info[5] &= ~m;
8527 
8528 	m = IEEE80211_HE_PHY_CAP6_CODEBOOK_SIZE_75_MU |
8529 	    IEEE80211_HE_PHY_CAP6_TRIG_MU_BEAMFORMING_PARTIAL_BW_FB |
8530 	    IEEE80211_HE_PHY_CAP6_TRIG_CQI_FB |
8531 	    IEEE80211_HE_PHY_CAP6_PARTIAL_BANDWIDTH_DL_MUMIMO;
8532 	he_cap_elem->phy_cap_info[6] &= ~m;
8533 
8534 	m = IEEE80211_HE_PHY_CAP7_PSR_BASED_SR |
8535 	    IEEE80211_HE_PHY_CAP7_POWER_BOOST_FACTOR_SUPP |
8536 	    IEEE80211_HE_PHY_CAP7_STBC_TX_ABOVE_80MHZ |
8537 	    IEEE80211_HE_PHY_CAP7_STBC_RX_ABOVE_80MHZ;
8538 	he_cap_elem->phy_cap_info[7] &= ~m;
8539 
8540 	m = IEEE80211_HE_PHY_CAP8_HE_ER_SU_PPDU_4XLTF_AND_08_US_GI |
8541 	    IEEE80211_HE_PHY_CAP8_20MHZ_IN_40MHZ_HE_PPDU_IN_2G |
8542 	    IEEE80211_HE_PHY_CAP8_20MHZ_IN_160MHZ_HE_PPDU |
8543 	    IEEE80211_HE_PHY_CAP8_80MHZ_IN_160MHZ_HE_PPDU;
8544 	he_cap_elem->phy_cap_info[8] &= ~m;
8545 
8546 	m = IEEE80211_HE_PHY_CAP9_LONGER_THAN_16_SIGB_OFDM_SYM |
8547 	    IEEE80211_HE_PHY_CAP9_NON_TRIGGERED_CQI_FEEDBACK |
8548 	    IEEE80211_HE_PHY_CAP9_RX_1024_QAM_LESS_THAN_242_TONE_RU |
8549 	    IEEE80211_HE_PHY_CAP9_TX_1024_QAM_LESS_THAN_242_TONE_RU |
8550 	    IEEE80211_HE_PHY_CAP9_RX_FULL_BW_SU_USING_MU_WITH_COMP_SIGB |
8551 	    IEEE80211_HE_PHY_CAP9_RX_FULL_BW_SU_USING_MU_WITH_NON_COMP_SIGB;
8552 	he_cap_elem->phy_cap_info[9] &= ~m;
8553 }
8554 
8555 static __le16 ath12k_mac_setup_he_6ghz_cap(struct ath12k_pdev_cap *pcap,
8556 					   struct ath12k_band_cap *bcap)
8557 {
8558 	u8 val;
8559 
8560 	bcap->he_6ghz_capa = IEEE80211_HT_MPDU_DENSITY_NONE;
8561 	if (bcap->ht_cap_info & WMI_HT_CAP_DYNAMIC_SMPS)
8562 		bcap->he_6ghz_capa |=
8563 			u32_encode_bits(WLAN_HT_CAP_SM_PS_DYNAMIC,
8564 					IEEE80211_HE_6GHZ_CAP_SM_PS);
8565 	else
8566 		bcap->he_6ghz_capa |=
8567 			u32_encode_bits(WLAN_HT_CAP_SM_PS_DISABLED,
8568 					IEEE80211_HE_6GHZ_CAP_SM_PS);
8569 	val = u32_get_bits(pcap->vht_cap,
8570 			   IEEE80211_VHT_CAP_MAX_A_MPDU_LENGTH_EXPONENT_MASK);
8571 	bcap->he_6ghz_capa |=
8572 		u32_encode_bits(val, IEEE80211_HE_6GHZ_CAP_MAX_AMPDU_LEN_EXP);
8573 	val = u32_get_bits(pcap->vht_cap,
8574 			   IEEE80211_VHT_CAP_MAX_MPDU_MASK);
8575 	bcap->he_6ghz_capa |=
8576 		u32_encode_bits(val, IEEE80211_HE_6GHZ_CAP_MAX_MPDU_LEN);
8577 	if (pcap->vht_cap & IEEE80211_VHT_CAP_RX_ANTENNA_PATTERN)
8578 		bcap->he_6ghz_capa |= IEEE80211_HE_6GHZ_CAP_RX_ANTPAT_CONS;
8579 	if (pcap->vht_cap & IEEE80211_VHT_CAP_TX_ANTENNA_PATTERN)
8580 		bcap->he_6ghz_capa |= IEEE80211_HE_6GHZ_CAP_TX_ANTPAT_CONS;
8581 
8582 	return cpu_to_le16(bcap->he_6ghz_capa);
8583 }
8584 
8585 static void ath12k_mac_set_hemcsmap(struct ath12k *ar,
8586 				    struct ath12k_pdev_cap *cap,
8587 				    struct ieee80211_sta_he_cap *he_cap)
8588 {
8589 	struct ieee80211_he_mcs_nss_supp *mcs_nss = &he_cap->he_mcs_nss_supp;
8590 	u8 maxtxnss_160 = ath12k_get_nss_160mhz(ar, ar->num_tx_chains);
8591 	u8 maxrxnss_160 = ath12k_get_nss_160mhz(ar, ar->num_rx_chains);
8592 	u16 txmcs_map_160 = 0, rxmcs_map_160 = 0;
8593 	u16 txmcs_map = 0, rxmcs_map = 0;
8594 	u32 i;
8595 
8596 	for (i = 0; i < 8; i++) {
8597 		if (i < ar->num_tx_chains &&
8598 		    (ar->cfg_tx_chainmask >> cap->tx_chain_mask_shift) & BIT(i))
8599 			txmcs_map |= IEEE80211_HE_MCS_SUPPORT_0_11 << (i * 2);
8600 		else
8601 			txmcs_map |= IEEE80211_HE_MCS_NOT_SUPPORTED << (i * 2);
8602 
8603 		if (i < ar->num_rx_chains &&
8604 		    (ar->cfg_rx_chainmask >> cap->tx_chain_mask_shift) & BIT(i))
8605 			rxmcs_map |= IEEE80211_HE_MCS_SUPPORT_0_11 << (i * 2);
8606 		else
8607 			rxmcs_map |= IEEE80211_HE_MCS_NOT_SUPPORTED << (i * 2);
8608 
8609 		if (i < maxtxnss_160 &&
8610 		    (ar->cfg_tx_chainmask >> cap->tx_chain_mask_shift) & BIT(i))
8611 			txmcs_map_160 |= IEEE80211_HE_MCS_SUPPORT_0_11 << (i * 2);
8612 		else
8613 			txmcs_map_160 |= IEEE80211_HE_MCS_NOT_SUPPORTED << (i * 2);
8614 
8615 		if (i < maxrxnss_160 &&
8616 		    (ar->cfg_tx_chainmask >> cap->tx_chain_mask_shift) & BIT(i))
8617 			rxmcs_map_160 |= IEEE80211_HE_MCS_SUPPORT_0_11 << (i * 2);
8618 		else
8619 			rxmcs_map_160 |= IEEE80211_HE_MCS_NOT_SUPPORTED << (i * 2);
8620 	}
8621 
8622 	mcs_nss->rx_mcs_80 = cpu_to_le16(rxmcs_map & 0xffff);
8623 	mcs_nss->tx_mcs_80 = cpu_to_le16(txmcs_map & 0xffff);
8624 	mcs_nss->rx_mcs_160 = cpu_to_le16(rxmcs_map_160 & 0xffff);
8625 	mcs_nss->tx_mcs_160 = cpu_to_le16(txmcs_map_160 & 0xffff);
8626 }
8627 
8628 static void ath12k_mac_copy_he_cap(struct ath12k *ar,
8629 				   struct ath12k_band_cap *band_cap,
8630 				   int iftype, u8 num_tx_chains,
8631 				   struct ieee80211_sta_he_cap *he_cap)
8632 {
8633 	struct ieee80211_he_cap_elem *he_cap_elem = &he_cap->he_cap_elem;
8634 
8635 	he_cap->has_he = true;
8636 	memcpy(he_cap_elem->mac_cap_info, band_cap->he_cap_info,
8637 	       sizeof(he_cap_elem->mac_cap_info));
8638 	memcpy(he_cap_elem->phy_cap_info, band_cap->he_cap_phy_info,
8639 	       sizeof(he_cap_elem->phy_cap_info));
8640 
8641 	he_cap_elem->mac_cap_info[1] &=
8642 		IEEE80211_HE_MAC_CAP1_TF_MAC_PAD_DUR_MASK;
8643 	he_cap_elem->phy_cap_info[0] &=
8644 		IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_40MHZ_IN_2G |
8645 		IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_40MHZ_80MHZ_IN_5G |
8646 		IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_160MHZ_IN_5G;
8647 	/* 80PLUS80 is not supported */
8648 	he_cap_elem->phy_cap_info[0] &=
8649 		~IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_80PLUS80_MHZ_IN_5G;
8650 	he_cap_elem->phy_cap_info[5] &=
8651 		~IEEE80211_HE_PHY_CAP5_BEAMFORMEE_NUM_SND_DIM_UNDER_80MHZ_MASK;
8652 	he_cap_elem->phy_cap_info[5] |= num_tx_chains - 1;
8653 
8654 	switch (iftype) {
8655 	case NL80211_IFTYPE_AP:
8656 		he_cap_elem->mac_cap_info[2] &=
8657 			~IEEE80211_HE_MAC_CAP2_BCAST_TWT;
8658 		he_cap_elem->phy_cap_info[3] &=
8659 			~IEEE80211_HE_PHY_CAP3_DCM_MAX_CONST_TX_MASK;
8660 		he_cap_elem->phy_cap_info[9] |=
8661 			IEEE80211_HE_PHY_CAP9_RX_1024_QAM_LESS_THAN_242_TONE_RU;
8662 		break;
8663 	case NL80211_IFTYPE_STATION:
8664 		he_cap_elem->mac_cap_info[0] &= ~IEEE80211_HE_MAC_CAP0_TWT_RES;
8665 		he_cap_elem->mac_cap_info[0] |= IEEE80211_HE_MAC_CAP0_TWT_REQ;
8666 		he_cap_elem->phy_cap_info[9] |=
8667 			IEEE80211_HE_PHY_CAP9_TX_1024_QAM_LESS_THAN_242_TONE_RU;
8668 		break;
8669 	case NL80211_IFTYPE_MESH_POINT:
8670 		ath12k_mac_filter_he_cap_mesh(he_cap_elem);
8671 		break;
8672 	}
8673 
8674 	ath12k_mac_set_hemcsmap(ar, &ar->pdev->cap, he_cap);
8675 	memset(he_cap->ppe_thres, 0, sizeof(he_cap->ppe_thres));
8676 	if (he_cap_elem->phy_cap_info[6] &
8677 	    IEEE80211_HE_PHY_CAP6_PPE_THRESHOLD_PRESENT)
8678 		ath12k_gen_ppe_thresh(&band_cap->he_ppet, he_cap->ppe_thres);
8679 }
8680 
8681 static void
8682 ath12k_mac_copy_eht_mcs_nss(struct ath12k_band_cap *band_cap,
8683 			    struct ieee80211_eht_mcs_nss_supp *mcs_nss,
8684 			    const struct ieee80211_he_cap_elem *he_cap,
8685 			    const struct ieee80211_eht_cap_elem_fixed *eht_cap)
8686 {
8687 	if ((he_cap->phy_cap_info[0] &
8688 	     (IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_40MHZ_IN_2G |
8689 	      IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_40MHZ_80MHZ_IN_5G |
8690 	      IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_160MHZ_IN_5G |
8691 	      IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_80PLUS80_MHZ_IN_5G)) == 0)
8692 		memcpy(&mcs_nss->only_20mhz, &band_cap->eht_mcs_20_only,
8693 		       sizeof(struct ieee80211_eht_mcs_nss_supp_20mhz_only));
8694 
8695 	if (he_cap->phy_cap_info[0] &
8696 	    (IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_40MHZ_IN_2G |
8697 	     IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_40MHZ_80MHZ_IN_5G))
8698 		memcpy(&mcs_nss->bw._80, &band_cap->eht_mcs_80,
8699 		       sizeof(struct ieee80211_eht_mcs_nss_supp_bw));
8700 
8701 	if (he_cap->phy_cap_info[0] &
8702 	    IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_160MHZ_IN_5G)
8703 		memcpy(&mcs_nss->bw._160, &band_cap->eht_mcs_160,
8704 		       sizeof(struct ieee80211_eht_mcs_nss_supp_bw));
8705 
8706 	if (eht_cap->phy_cap_info[0] & IEEE80211_EHT_PHY_CAP0_320MHZ_IN_6GHZ)
8707 		memcpy(&mcs_nss->bw._320, &band_cap->eht_mcs_320,
8708 		       sizeof(struct ieee80211_eht_mcs_nss_supp_bw));
8709 }
8710 
8711 static void ath12k_mac_copy_eht_ppe_thresh(struct ath12k_wmi_ppe_threshold_arg *fw_ppet,
8712 					   struct ieee80211_sta_eht_cap *cap)
8713 {
8714 	u16 bit = IEEE80211_EHT_PPE_THRES_INFO_HEADER_SIZE;
8715 	u8 i, nss, ru, ppet_bit_len_per_ru = IEEE80211_EHT_PPE_THRES_INFO_PPET_SIZE * 2;
8716 
8717 	u8p_replace_bits(&cap->eht_ppe_thres[0], fw_ppet->numss_m1,
8718 			 IEEE80211_EHT_PPE_THRES_NSS_MASK);
8719 
8720 	u16p_replace_bits((u16 *)&cap->eht_ppe_thres[0], fw_ppet->ru_bit_mask,
8721 			  IEEE80211_EHT_PPE_THRES_RU_INDEX_BITMASK_MASK);
8722 
8723 	for (nss = 0; nss <= fw_ppet->numss_m1; nss++) {
8724 		for (ru = 0;
8725 		     ru < hweight16(IEEE80211_EHT_PPE_THRES_RU_INDEX_BITMASK_MASK);
8726 		     ru++) {
8727 			u32 val = 0;
8728 
8729 			if ((fw_ppet->ru_bit_mask & BIT(ru)) == 0)
8730 				continue;
8731 
8732 			u32p_replace_bits(&val, fw_ppet->ppet16_ppet8_ru3_ru0[nss] >>
8733 						(ru * ppet_bit_len_per_ru),
8734 					  GENMASK(ppet_bit_len_per_ru - 1, 0));
8735 
8736 			for (i = 0; i < ppet_bit_len_per_ru; i++) {
8737 				cap->eht_ppe_thres[bit / 8] |=
8738 					(((val >> i) & 0x1) << ((bit % 8)));
8739 				bit++;
8740 			}
8741 		}
8742 	}
8743 }
8744 
8745 static void
8746 ath12k_mac_filter_eht_cap_mesh(struct ieee80211_eht_cap_elem_fixed
8747 			       *eht_cap_elem)
8748 {
8749 	u8 m;
8750 
8751 	m = IEEE80211_EHT_MAC_CAP0_EPCS_PRIO_ACCESS;
8752 	eht_cap_elem->mac_cap_info[0] &= ~m;
8753 
8754 	m = IEEE80211_EHT_PHY_CAP0_PARTIAL_BW_UL_MU_MIMO;
8755 	eht_cap_elem->phy_cap_info[0] &= ~m;
8756 
8757 	m = IEEE80211_EHT_PHY_CAP3_NG_16_MU_FEEDBACK |
8758 	    IEEE80211_EHT_PHY_CAP3_CODEBOOK_7_5_MU_FDBK |
8759 	    IEEE80211_EHT_PHY_CAP3_TRIG_MU_BF_PART_BW_FDBK |
8760 	    IEEE80211_EHT_PHY_CAP3_TRIG_CQI_FDBK;
8761 	eht_cap_elem->phy_cap_info[3] &= ~m;
8762 
8763 	m = IEEE80211_EHT_PHY_CAP4_PART_BW_DL_MU_MIMO |
8764 	    IEEE80211_EHT_PHY_CAP4_PSR_SR_SUPP |
8765 	    IEEE80211_EHT_PHY_CAP4_POWER_BOOST_FACT_SUPP |
8766 	    IEEE80211_EHT_PHY_CAP4_EHT_MU_PPDU_4_EHT_LTF_08_GI;
8767 	eht_cap_elem->phy_cap_info[4] &= ~m;
8768 
8769 	m = IEEE80211_EHT_PHY_CAP5_NON_TRIG_CQI_FEEDBACK |
8770 	    IEEE80211_EHT_PHY_CAP5_TX_LESS_242_TONE_RU_SUPP |
8771 	    IEEE80211_EHT_PHY_CAP5_RX_LESS_242_TONE_RU_SUPP |
8772 	    IEEE80211_EHT_PHY_CAP5_MAX_NUM_SUPP_EHT_LTF_MASK;
8773 	eht_cap_elem->phy_cap_info[5] &= ~m;
8774 
8775 	m = IEEE80211_EHT_PHY_CAP6_MAX_NUM_SUPP_EHT_LTF_MASK;
8776 	eht_cap_elem->phy_cap_info[6] &= ~m;
8777 
8778 	m = IEEE80211_EHT_PHY_CAP7_NON_OFDMA_UL_MU_MIMO_80MHZ |
8779 	    IEEE80211_EHT_PHY_CAP7_NON_OFDMA_UL_MU_MIMO_160MHZ |
8780 	    IEEE80211_EHT_PHY_CAP7_NON_OFDMA_UL_MU_MIMO_320MHZ |
8781 	    IEEE80211_EHT_PHY_CAP7_MU_BEAMFORMER_80MHZ |
8782 	    IEEE80211_EHT_PHY_CAP7_MU_BEAMFORMER_160MHZ |
8783 	    IEEE80211_EHT_PHY_CAP7_MU_BEAMFORMER_320MHZ;
8784 	eht_cap_elem->phy_cap_info[7] &= ~m;
8785 }
8786 
8787 static void ath12k_mac_copy_eht_cap(struct ath12k *ar,
8788 				    struct ath12k_band_cap *band_cap,
8789 				    struct ieee80211_he_cap_elem *he_cap_elem,
8790 				    int iftype,
8791 				    struct ieee80211_sta_eht_cap *eht_cap)
8792 {
8793 	struct ieee80211_eht_cap_elem_fixed *eht_cap_elem = &eht_cap->eht_cap_elem;
8794 
8795 	memset(eht_cap, 0, sizeof(struct ieee80211_sta_eht_cap));
8796 
8797 	if (!(test_bit(WMI_TLV_SERVICE_11BE, ar->ab->wmi_ab.svc_map)) ||
8798 	    ath12k_acpi_get_disable_11be(ar->ab))
8799 		return;
8800 
8801 	eht_cap->has_eht = true;
8802 	memcpy(eht_cap_elem->mac_cap_info, band_cap->eht_cap_mac_info,
8803 	       sizeof(eht_cap_elem->mac_cap_info));
8804 	memcpy(eht_cap_elem->phy_cap_info, band_cap->eht_cap_phy_info,
8805 	       sizeof(eht_cap_elem->phy_cap_info));
8806 
8807 	switch (iftype) {
8808 	case NL80211_IFTYPE_AP:
8809 		eht_cap_elem->phy_cap_info[0] &=
8810 			~IEEE80211_EHT_PHY_CAP0_242_TONE_RU_GT20MHZ;
8811 		eht_cap_elem->phy_cap_info[4] &=
8812 			~IEEE80211_EHT_PHY_CAP4_PART_BW_DL_MU_MIMO;
8813 		eht_cap_elem->phy_cap_info[5] &=
8814 			~IEEE80211_EHT_PHY_CAP5_TX_LESS_242_TONE_RU_SUPP;
8815 		break;
8816 	case NL80211_IFTYPE_STATION:
8817 		eht_cap_elem->phy_cap_info[7] &=
8818 			~(IEEE80211_EHT_PHY_CAP7_NON_OFDMA_UL_MU_MIMO_80MHZ |
8819 			  IEEE80211_EHT_PHY_CAP7_NON_OFDMA_UL_MU_MIMO_160MHZ |
8820 			  IEEE80211_EHT_PHY_CAP7_NON_OFDMA_UL_MU_MIMO_320MHZ);
8821 		eht_cap_elem->phy_cap_info[7] &=
8822 			~(IEEE80211_EHT_PHY_CAP7_MU_BEAMFORMER_80MHZ |
8823 			  IEEE80211_EHT_PHY_CAP7_MU_BEAMFORMER_160MHZ |
8824 			  IEEE80211_EHT_PHY_CAP7_MU_BEAMFORMER_320MHZ);
8825 		break;
8826 	case NL80211_IFTYPE_MESH_POINT:
8827 		ath12k_mac_filter_eht_cap_mesh(eht_cap_elem);
8828 		break;
8829 	default:
8830 		break;
8831 	}
8832 
8833 	ath12k_mac_copy_eht_mcs_nss(band_cap, &eht_cap->eht_mcs_nss_supp,
8834 				    he_cap_elem, eht_cap_elem);
8835 
8836 	if (eht_cap_elem->phy_cap_info[5] &
8837 	    IEEE80211_EHT_PHY_CAP5_PPE_THRESHOLD_PRESENT)
8838 		ath12k_mac_copy_eht_ppe_thresh(&band_cap->eht_ppet, eht_cap);
8839 }
8840 
8841 static int ath12k_mac_copy_sband_iftype_data(struct ath12k *ar,
8842 					     struct ath12k_pdev_cap *cap,
8843 					     struct ieee80211_sband_iftype_data *data,
8844 					     int band)
8845 {
8846 	struct ath12k_band_cap *band_cap = &cap->band[band];
8847 	int i, idx = 0;
8848 
8849 	for (i = 0; i < NUM_NL80211_IFTYPES; i++) {
8850 		struct ieee80211_sta_he_cap *he_cap = &data[idx].he_cap;
8851 
8852 		switch (i) {
8853 		case NL80211_IFTYPE_STATION:
8854 		case NL80211_IFTYPE_AP:
8855 		case NL80211_IFTYPE_MESH_POINT:
8856 			break;
8857 
8858 		default:
8859 			continue;
8860 		}
8861 
8862 		data[idx].types_mask = BIT(i);
8863 
8864 		ath12k_mac_copy_he_cap(ar, band_cap, i, ar->num_tx_chains, he_cap);
8865 		if (band == NL80211_BAND_6GHZ) {
8866 			data[idx].he_6ghz_capa.capa =
8867 				ath12k_mac_setup_he_6ghz_cap(cap, band_cap);
8868 		}
8869 		ath12k_mac_copy_eht_cap(ar, band_cap, &he_cap->he_cap_elem, i,
8870 					&data[idx].eht_cap);
8871 		idx++;
8872 	}
8873 
8874 	return idx;
8875 }
8876 
8877 static void ath12k_mac_setup_sband_iftype_data(struct ath12k *ar,
8878 					       struct ath12k_pdev_cap *cap)
8879 {
8880 	struct ieee80211_supported_band *sband;
8881 	enum nl80211_band band;
8882 	int count;
8883 
8884 	if (cap->supported_bands & WMI_HOST_WLAN_2GHZ_CAP) {
8885 		band = NL80211_BAND_2GHZ;
8886 		count = ath12k_mac_copy_sband_iftype_data(ar, cap,
8887 							  ar->mac.iftype[band],
8888 							  band);
8889 		sband = &ar->mac.sbands[band];
8890 		_ieee80211_set_sband_iftype_data(sband, ar->mac.iftype[band],
8891 						 count);
8892 	}
8893 
8894 	if (cap->supported_bands & WMI_HOST_WLAN_5GHZ_CAP) {
8895 		band = NL80211_BAND_5GHZ;
8896 		count = ath12k_mac_copy_sband_iftype_data(ar, cap,
8897 							  ar->mac.iftype[band],
8898 							  band);
8899 		sband = &ar->mac.sbands[band];
8900 		_ieee80211_set_sband_iftype_data(sband, ar->mac.iftype[band],
8901 						 count);
8902 	}
8903 
8904 	if (cap->supported_bands & WMI_HOST_WLAN_5GHZ_CAP &&
8905 	    ar->supports_6ghz) {
8906 		band = NL80211_BAND_6GHZ;
8907 		count = ath12k_mac_copy_sband_iftype_data(ar, cap,
8908 							  ar->mac.iftype[band],
8909 							  band);
8910 		sband = &ar->mac.sbands[band];
8911 		_ieee80211_set_sband_iftype_data(sband, ar->mac.iftype[band],
8912 						 count);
8913 	}
8914 }
8915 
8916 static int __ath12k_set_antenna(struct ath12k *ar, u32 tx_ant, u32 rx_ant)
8917 {
8918 	struct ath12k_hw *ah = ath12k_ar_to_ah(ar);
8919 	int ret;
8920 
8921 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
8922 
8923 	if (ath12k_check_chain_mask(ar, tx_ant, true))
8924 		return -EINVAL;
8925 
8926 	if (ath12k_check_chain_mask(ar, rx_ant, false))
8927 		return -EINVAL;
8928 
8929 	/* Since we advertised the max cap of all radios combined during wiphy
8930 	 * registration, ensure we don't set the antenna config higher than the
8931 	 * limits
8932 	 */
8933 	tx_ant = min_t(u32, tx_ant, ar->pdev->cap.tx_chain_mask);
8934 	rx_ant = min_t(u32, rx_ant, ar->pdev->cap.rx_chain_mask);
8935 
8936 	ar->cfg_tx_chainmask = tx_ant;
8937 	ar->cfg_rx_chainmask = rx_ant;
8938 
8939 	if (ah->state != ATH12K_HW_STATE_ON &&
8940 	    ah->state != ATH12K_HW_STATE_RESTARTED)
8941 		return 0;
8942 
8943 	ret = ath12k_wmi_pdev_set_param(ar, WMI_PDEV_PARAM_TX_CHAIN_MASK,
8944 					tx_ant, ar->pdev->pdev_id);
8945 	if (ret) {
8946 		ath12k_warn(ar->ab, "failed to set tx-chainmask: %d, req 0x%x\n",
8947 			    ret, tx_ant);
8948 		return ret;
8949 	}
8950 
8951 	ar->num_tx_chains = hweight32(tx_ant);
8952 
8953 	ret = ath12k_wmi_pdev_set_param(ar, WMI_PDEV_PARAM_RX_CHAIN_MASK,
8954 					rx_ant, ar->pdev->pdev_id);
8955 	if (ret) {
8956 		ath12k_warn(ar->ab, "failed to set rx-chainmask: %d, req 0x%x\n",
8957 			    ret, rx_ant);
8958 		return ret;
8959 	}
8960 
8961 	ar->num_rx_chains = hweight32(rx_ant);
8962 
8963 	/* Reload HT/VHT/HE capability */
8964 	ath12k_mac_setup_ht_vht_cap(ar, &ar->pdev->cap, NULL);
8965 	ath12k_mac_setup_sband_iftype_data(ar, &ar->pdev->cap);
8966 
8967 	return 0;
8968 }
8969 
8970 static void ath12k_mgmt_over_wmi_tx_drop(struct ath12k *ar, struct sk_buff *skb)
8971 {
8972 	int num_mgmt;
8973 
8974 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
8975 
8976 	ieee80211_free_txskb(ath12k_ar_to_hw(ar), skb);
8977 
8978 	num_mgmt = atomic_dec_if_positive(&ar->num_pending_mgmt_tx);
8979 
8980 	if (num_mgmt < 0)
8981 		WARN_ON_ONCE(1);
8982 
8983 	if (!num_mgmt)
8984 		wake_up(&ar->txmgmt_empty_waitq);
8985 }
8986 
8987 static void ath12k_mac_tx_mgmt_free(struct ath12k *ar, int buf_id)
8988 {
8989 	struct sk_buff *msdu;
8990 	struct ieee80211_tx_info *info;
8991 
8992 	spin_lock_bh(&ar->txmgmt_idr_lock);
8993 	msdu = idr_remove(&ar->txmgmt_idr, buf_id);
8994 	spin_unlock_bh(&ar->txmgmt_idr_lock);
8995 
8996 	if (!msdu)
8997 		return;
8998 
8999 	dma_unmap_single(ar->ab->dev, ATH12K_SKB_CB(msdu)->paddr, msdu->len,
9000 			 DMA_TO_DEVICE);
9001 
9002 	info = IEEE80211_SKB_CB(msdu);
9003 	memset(&info->status, 0, sizeof(info->status));
9004 
9005 	ath12k_mgmt_over_wmi_tx_drop(ar, msdu);
9006 }
9007 
9008 int ath12k_mac_tx_mgmt_pending_free(int buf_id, void *skb, void *ctx)
9009 {
9010 	struct ath12k *ar = ctx;
9011 
9012 	ath12k_mac_tx_mgmt_free(ar, buf_id);
9013 
9014 	return 0;
9015 }
9016 
9017 static int ath12k_mac_vif_txmgmt_idr_remove(int buf_id, void *skb, void *ctx)
9018 {
9019 	struct ieee80211_vif *vif = ctx;
9020 	struct ath12k_skb_cb *skb_cb = ATH12K_SKB_CB(skb);
9021 	struct ath12k *ar = skb_cb->ar;
9022 
9023 	if (skb_cb->vif == vif)
9024 		ath12k_mac_tx_mgmt_free(ar, buf_id);
9025 
9026 	return 0;
9027 }
9028 
9029 static int ath12k_mac_mgmt_tx_wmi(struct ath12k *ar, struct ath12k_link_vif *arvif,
9030 				  struct sk_buff *skb)
9031 {
9032 	struct ath12k_base *ab = ar->ab;
9033 	struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
9034 	struct ath12k_skb_cb *skb_cb = ATH12K_SKB_CB(skb);
9035 	struct ieee80211_tx_info *info;
9036 	enum hal_encrypt_type enctype;
9037 	unsigned int mic_len;
9038 	dma_addr_t paddr;
9039 	int buf_id;
9040 	int ret;
9041 
9042 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
9043 
9044 	skb_cb->ar = ar;
9045 	spin_lock_bh(&ar->txmgmt_idr_lock);
9046 	buf_id = idr_alloc(&ar->txmgmt_idr, skb, 0,
9047 			   ATH12K_TX_MGMT_NUM_PENDING_MAX, GFP_ATOMIC);
9048 	spin_unlock_bh(&ar->txmgmt_idr_lock);
9049 	if (buf_id < 0)
9050 		return -ENOSPC;
9051 
9052 	info = IEEE80211_SKB_CB(skb);
9053 	if ((skb_cb->flags & ATH12K_SKB_CIPHER_SET) &&
9054 	    !(info->flags & IEEE80211_TX_CTL_HW_80211_ENCAP)) {
9055 		if ((ieee80211_is_action(hdr->frame_control) ||
9056 		     ieee80211_is_deauth(hdr->frame_control) ||
9057 		     ieee80211_is_disassoc(hdr->frame_control)) &&
9058 		     ieee80211_has_protected(hdr->frame_control)) {
9059 			enctype = ath12k_dp_tx_get_encrypt_type(skb_cb->cipher);
9060 			mic_len = ath12k_dp_rx_crypto_mic_len(ab->dp, enctype);
9061 			skb_put(skb, mic_len);
9062 		}
9063 	}
9064 
9065 	paddr = dma_map_single(ab->dev, skb->data, skb->len, DMA_TO_DEVICE);
9066 	if (dma_mapping_error(ab->dev, paddr)) {
9067 		ath12k_warn(ab, "failed to DMA map mgmt Tx buffer\n");
9068 		ret = -EIO;
9069 		goto err_free_idr;
9070 	}
9071 
9072 	skb_cb->paddr = paddr;
9073 
9074 	ret = ath12k_wmi_mgmt_send(arvif, buf_id, skb);
9075 	if (ret) {
9076 		ath12k_warn(ar->ab, "failed to send mgmt frame: %d\n", ret);
9077 		goto err_unmap_buf;
9078 	}
9079 
9080 	return 0;
9081 
9082 err_unmap_buf:
9083 	dma_unmap_single(ab->dev, skb_cb->paddr,
9084 			 skb->len, DMA_TO_DEVICE);
9085 err_free_idr:
9086 	spin_lock_bh(&ar->txmgmt_idr_lock);
9087 	idr_remove(&ar->txmgmt_idr, buf_id);
9088 	spin_unlock_bh(&ar->txmgmt_idr_lock);
9089 
9090 	return ret;
9091 }
9092 
9093 static void ath12k_mgmt_over_wmi_tx_purge(struct ath12k *ar)
9094 {
9095 	struct sk_buff *skb;
9096 
9097 	while ((skb = skb_dequeue(&ar->wmi_mgmt_tx_queue)) != NULL)
9098 		ath12k_mgmt_over_wmi_tx_drop(ar, skb);
9099 }
9100 
9101 static int ath12k_mac_mgmt_action_frame_fill_elem_data(struct ath12k_link_vif *arvif,
9102 						       struct sk_buff *skb)
9103 {
9104 	struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
9105 	u8 category, *buf, iv_len, action_code, dialog_token;
9106 	struct ieee80211_bss_conf *link_conf;
9107 	struct ieee80211_chanctx_conf *conf;
9108 	int cur_tx_power, max_tx_power;
9109 	struct ath12k *ar = arvif->ar;
9110 	struct ieee80211_hw *hw = ath12k_ar_to_hw(ar);
9111 	struct wiphy *wiphy = hw->wiphy;
9112 	struct ath12k_skb_cb *skb_cb;
9113 	struct ieee80211_mgmt *mgmt;
9114 	unsigned int remaining_len;
9115 	bool has_protected;
9116 
9117 	lockdep_assert_wiphy(wiphy);
9118 
9119 	/* make sure category field is present */
9120 	if (skb->len < IEEE80211_MIN_ACTION_SIZE)
9121 		return -EINVAL;
9122 
9123 	remaining_len = skb->len - IEEE80211_MIN_ACTION_SIZE;
9124 	has_protected = ieee80211_has_protected(hdr->frame_control);
9125 
9126 	/* In case of SW crypto and hdr protected (PMF), packet will already be encrypted,
9127 	 * we can't put in data in this case
9128 	 */
9129 	if (test_bit(ATH12K_FLAG_HW_CRYPTO_DISABLED, &ar->ab->dev_flags) &&
9130 	    has_protected)
9131 		return 0;
9132 
9133 	mgmt = (struct ieee80211_mgmt *)hdr;
9134 	buf = (u8 *)&mgmt->u.action;
9135 
9136 	/* FCTL_PROTECTED frame might have extra space added for HDR_LEN. Offset that
9137 	 * many bytes if it is there
9138 	 */
9139 	if (has_protected) {
9140 		skb_cb = ATH12K_SKB_CB(skb);
9141 
9142 		switch (skb_cb->cipher) {
9143 		/* Cipher suite having flag %IEEE80211_KEY_FLAG_GENERATE_IV_MGMT set in
9144 		 * key needs to be processed. See ath12k_install_key()
9145 		 */
9146 		case WLAN_CIPHER_SUITE_CCMP:
9147 		case WLAN_CIPHER_SUITE_CCMP_256:
9148 		case WLAN_CIPHER_SUITE_GCMP:
9149 		case WLAN_CIPHER_SUITE_GCMP_256:
9150 			iv_len = IEEE80211_CCMP_HDR_LEN;
9151 			break;
9152 		case WLAN_CIPHER_SUITE_TKIP:
9153 			iv_len = 0;
9154 			break;
9155 		default:
9156 			return -EINVAL;
9157 		}
9158 
9159 		if (remaining_len < iv_len)
9160 			return -EINVAL;
9161 
9162 		buf += iv_len;
9163 		remaining_len -= iv_len;
9164 	}
9165 
9166 	category = *buf++;
9167 	/* category code is already taken care in %IEEE80211_MIN_ACTION_SIZE hence
9168 	 * no need to adjust remaining_len
9169 	 */
9170 
9171 	switch (category) {
9172 	case WLAN_CATEGORY_RADIO_MEASUREMENT:
9173 		/* need action code and dialog token */
9174 		if (remaining_len < 2)
9175 			return -EINVAL;
9176 
9177 		/* Packet Format:
9178 		 *	Action Code | Dialog Token | Variable Len (based on Action Code)
9179 		 */
9180 		action_code = *buf++;
9181 		dialog_token = *buf++;
9182 		remaining_len -= 2;
9183 
9184 		link_conf = ath12k_mac_get_link_bss_conf(arvif);
9185 		if (!link_conf) {
9186 			ath12k_warn(ar->ab,
9187 				    "failed to get bss link conf for vdev %d in RM handling\n",
9188 				    arvif->vdev_id);
9189 			return -EINVAL;
9190 		}
9191 
9192 		conf = wiphy_dereference(wiphy, link_conf->chanctx_conf);
9193 		if (!conf)
9194 			return -ENOENT;
9195 
9196 		cur_tx_power = link_conf->txpower;
9197 		max_tx_power = min(conf->def.chan->max_reg_power,
9198 				   (int)ar->max_tx_power / 2);
9199 
9200 		ath12k_mac_op_get_txpower(hw, arvif->ahvif->vif, arvif->link_id,
9201 					  &cur_tx_power);
9202 
9203 		switch (action_code) {
9204 		case WLAN_RM_ACTION_LINK_MEASUREMENT_REQUEST:
9205 			/* need variable fields to be present in len */
9206 			if (remaining_len < 2)
9207 				return -EINVAL;
9208 
9209 			/* Variable length format as defined in IEEE 802.11-2024,
9210 			 * Figure 9-1187-Link Measurement Request frame Action field
9211 			 * format.
9212 			 *	Transmit Power | Max Tx Power
9213 			 * We fill both of these.
9214 			 */
9215 			*buf++ = cur_tx_power;
9216 			*buf = max_tx_power;
9217 
9218 			ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
9219 				   "RRM: Link Measurement Req dialog_token %u cur_tx_power %d max_tx_power %d\n",
9220 				   dialog_token, cur_tx_power, max_tx_power);
9221 			break;
9222 		case WLAN_RM_ACTION_LINK_MEASUREMENT_REPORT:
9223 			/* need variable fields to be present in len */
9224 			if (remaining_len < 3)
9225 				return -EINVAL;
9226 
9227 			/* Variable length format as defined in IEEE 802.11-2024,
9228 			 * Figure 9-1188-Link Measurement Report frame Action field format
9229 			 *	TPC Report | Variable Fields
9230 			 *
9231 			 * TPC Report Format:
9232 			 *	Element ID | Len | Tx Power | Link Margin
9233 			 *
9234 			 * We fill Tx power in the TPC Report (2nd index)
9235 			 */
9236 			buf[2] = cur_tx_power;
9237 
9238 			/* TODO: At present, Link margin data is not present so can't
9239 			 * really fill it now. Once it is available, it can be added
9240 			 * here
9241 			 */
9242 			ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
9243 				   "RRM: Link Measurement Report dialog_token %u cur_tx_power %d\n",
9244 				   dialog_token, cur_tx_power);
9245 			break;
9246 		default:
9247 			return -EINVAL;
9248 		}
9249 		break;
9250 	default:
9251 		/* nothing to fill */
9252 		return 0;
9253 	}
9254 
9255 	return 0;
9256 }
9257 
9258 static int ath12k_mac_mgmt_frame_fill_elem_data(struct ath12k_link_vif *arvif,
9259 						struct sk_buff *skb)
9260 {
9261 	struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
9262 
9263 	if (!ieee80211_is_action(hdr->frame_control))
9264 		return 0;
9265 
9266 	return ath12k_mac_mgmt_action_frame_fill_elem_data(arvif, skb);
9267 }
9268 
9269 static void ath12k_mgmt_over_wmi_tx_work(struct wiphy *wiphy, struct wiphy_work *work)
9270 {
9271 	struct ath12k *ar = container_of(work, struct ath12k, wmi_mgmt_tx_work);
9272 	struct ath12k_hw *ah = ar->ah;
9273 	struct ath12k_skb_cb *skb_cb;
9274 	struct ath12k_vif *ahvif;
9275 	struct ath12k_link_vif *arvif;
9276 	struct sk_buff *skb;
9277 	int ret;
9278 
9279 	lockdep_assert_wiphy(wiphy);
9280 
9281 	while ((skb = skb_dequeue(&ar->wmi_mgmt_tx_queue)) != NULL) {
9282 		skb_cb = ATH12K_SKB_CB(skb);
9283 		if (!skb_cb->vif) {
9284 			ath12k_warn(ar->ab, "no vif found for mgmt frame\n");
9285 			ath12k_mgmt_over_wmi_tx_drop(ar, skb);
9286 			continue;
9287 		}
9288 
9289 		ahvif = ath12k_vif_to_ahvif(skb_cb->vif);
9290 		if (!(ahvif->links_map & BIT(skb_cb->link_id))) {
9291 			ath12k_warn(ar->ab,
9292 				    "invalid linkid %u in mgmt over wmi tx with linkmap 0x%x\n",
9293 				    skb_cb->link_id, ahvif->links_map);
9294 			ath12k_mgmt_over_wmi_tx_drop(ar, skb);
9295 			continue;
9296 		}
9297 
9298 		arvif = wiphy_dereference(ah->hw->wiphy, ahvif->link[skb_cb->link_id]);
9299 		if (ar->allocated_vdev_map & (1LL << arvif->vdev_id)) {
9300 			/* Fill in the data which is required to be filled by the driver
9301 			 * For example: Max Tx power in Link Measurement Request/Report
9302 			 */
9303 			ret = ath12k_mac_mgmt_frame_fill_elem_data(arvif, skb);
9304 			if (ret) {
9305 				/* If we couldn't fill the data due to any reason,
9306 				 * let's not discard transmitting the packet.
9307 				 * For example: Software crypto and PMF case
9308 				 */
9309 				ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
9310 					   "Failed to fill the required data for the mgmt packet err %d\n",
9311 					   ret);
9312 			}
9313 
9314 			ret = ath12k_mac_mgmt_tx_wmi(ar, arvif, skb);
9315 			if (ret) {
9316 				ath12k_warn(ar->ab, "failed to tx mgmt frame, vdev_id %d :%d\n",
9317 					    arvif->vdev_id, ret);
9318 				ath12k_mgmt_over_wmi_tx_drop(ar, skb);
9319 			}
9320 		} else {
9321 			ath12k_warn(ar->ab,
9322 				    "dropping mgmt frame for vdev %d link %u is_started %d\n",
9323 				    arvif->vdev_id,
9324 				    skb_cb->link_id,
9325 				    arvif->is_started);
9326 			ath12k_mgmt_over_wmi_tx_drop(ar, skb);
9327 		}
9328 	}
9329 }
9330 
9331 int ath12k_mac_mgmt_tx(struct ath12k *ar, struct sk_buff *skb,
9332 		       bool is_prb_rsp)
9333 {
9334 	struct sk_buff_head *q = &ar->wmi_mgmt_tx_queue;
9335 
9336 	if (test_bit(ATH12K_FLAG_CRASH_FLUSH, &ar->ab->dev_flags))
9337 		return -ESHUTDOWN;
9338 
9339 	/* Drop probe response packets when the pending management tx
9340 	 * count has reached a certain threshold, so as to prioritize
9341 	 * other mgmt packets like auth and assoc to be sent on time
9342 	 * for establishing successful connections.
9343 	 */
9344 	if (is_prb_rsp &&
9345 	    atomic_read(&ar->num_pending_mgmt_tx) > ATH12K_PRB_RSP_DROP_THRESHOLD) {
9346 		ath12k_warn(ar->ab,
9347 			    "dropping probe response as pending queue is almost full\n");
9348 		return -ENOSPC;
9349 	}
9350 
9351 	if (skb_queue_len_lockless(q) >= ATH12K_TX_MGMT_NUM_PENDING_MAX) {
9352 		ath12k_warn(ar->ab, "mgmt tx queue is full\n");
9353 		return -ENOSPC;
9354 	}
9355 
9356 	skb_queue_tail(q, skb);
9357 	atomic_inc(&ar->num_pending_mgmt_tx);
9358 	wiphy_work_queue(ath12k_ar_to_hw(ar)->wiphy, &ar->wmi_mgmt_tx_work);
9359 
9360 	return 0;
9361 }
9362 EXPORT_SYMBOL(ath12k_mac_mgmt_tx);
9363 
9364 void ath12k_mac_add_p2p_noa_ie(struct ath12k *ar,
9365 			       struct ieee80211_vif *vif,
9366 			       struct sk_buff *skb,
9367 			       bool is_prb_rsp)
9368 {
9369 	struct ath12k_vif *ahvif = ath12k_vif_to_ahvif(vif);
9370 
9371 	if (likely(!is_prb_rsp))
9372 		return;
9373 
9374 	spin_lock_bh(&ar->data_lock);
9375 
9376 	if (ahvif->u.ap.noa_data &&
9377 	    !pskb_expand_head(skb, 0, ahvif->u.ap.noa_len,
9378 			      GFP_ATOMIC))
9379 		skb_put_data(skb, ahvif->u.ap.noa_data,
9380 			     ahvif->u.ap.noa_len);
9381 
9382 	spin_unlock_bh(&ar->data_lock);
9383 }
9384 EXPORT_SYMBOL(ath12k_mac_add_p2p_noa_ie);
9385 
9386 /* Note: called under rcu_read_lock() */
9387 void ath12k_mlo_mcast_update_tx_link_address(struct ieee80211_vif *vif,
9388 					     u8 link_id, struct sk_buff *skb,
9389 					     u32 info_flags)
9390 {
9391 	struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
9392 	struct ieee80211_bss_conf *bss_conf;
9393 
9394 	if (info_flags & IEEE80211_TX_CTL_HW_80211_ENCAP)
9395 		return;
9396 
9397 	bss_conf = rcu_dereference(vif->link_conf[link_id]);
9398 	if (bss_conf)
9399 		ether_addr_copy(hdr->addr2, bss_conf->addr);
9400 }
9401 EXPORT_SYMBOL(ath12k_mlo_mcast_update_tx_link_address);
9402 
9403 /* Note: called under rcu_read_lock() */
9404 u8 ath12k_mac_get_tx_link(struct ieee80211_sta *sta, struct ieee80211_vif *vif,
9405 			  u8 link, struct sk_buff *skb, u32 info_flags)
9406 {
9407 	struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
9408 	struct ath12k_vif *ahvif = ath12k_vif_to_ahvif(vif);
9409 	struct ieee80211_link_sta *link_sta;
9410 	struct ieee80211_bss_conf *bss_conf;
9411 	struct ath12k_sta *ahsta;
9412 
9413 	/* Use the link id passed or the default vif link */
9414 	if (!sta) {
9415 		if (link != IEEE80211_LINK_UNSPECIFIED)
9416 			return link;
9417 
9418 		return ahvif->deflink.link_id;
9419 	}
9420 
9421 	ahsta = ath12k_sta_to_ahsta(sta);
9422 
9423 	/* Below translation ensures we pass proper A2 & A3 for non ML clients.
9424 	 * Also it assumes for now support only for MLO AP in this path
9425 	 */
9426 	if (!sta->mlo) {
9427 		link = ahsta->deflink.link_id;
9428 
9429 		if (info_flags & IEEE80211_TX_CTL_HW_80211_ENCAP)
9430 			return link;
9431 
9432 		bss_conf = rcu_dereference(vif->link_conf[link]);
9433 		if (bss_conf) {
9434 			ether_addr_copy(hdr->addr2, bss_conf->addr);
9435 			if (!ieee80211_has_tods(hdr->frame_control) &&
9436 			    !ieee80211_has_fromds(hdr->frame_control))
9437 				ether_addr_copy(hdr->addr3, bss_conf->addr);
9438 		}
9439 
9440 		return link;
9441 	}
9442 
9443 	/* enqueue eth enacap & data frames on primary link, FW does link
9444 	 * selection and address translation.
9445 	 */
9446 	if (info_flags & IEEE80211_TX_CTL_HW_80211_ENCAP ||
9447 	    ieee80211_is_data(hdr->frame_control))
9448 		return ahsta->assoc_link_id;
9449 
9450 	/* 802.11 frame cases */
9451 	if (link == IEEE80211_LINK_UNSPECIFIED)
9452 		link = ahsta->deflink.link_id;
9453 
9454 	if (!ieee80211_is_mgmt(hdr->frame_control))
9455 		return link;
9456 
9457 	/* Perform address conversion for ML STA Tx */
9458 	bss_conf = rcu_dereference(vif->link_conf[link]);
9459 	link_sta = rcu_dereference(sta->link[link]);
9460 
9461 	if (bss_conf && link_sta) {
9462 		ether_addr_copy(hdr->addr1, link_sta->addr);
9463 		ether_addr_copy(hdr->addr2, bss_conf->addr);
9464 
9465 		if (vif->type == NL80211_IFTYPE_STATION && bss_conf->bssid)
9466 			ether_addr_copy(hdr->addr3, bss_conf->bssid);
9467 		else if (vif->type == NL80211_IFTYPE_AP)
9468 			ether_addr_copy(hdr->addr3, bss_conf->addr);
9469 
9470 		return link;
9471 	}
9472 
9473 	if (bss_conf) {
9474 		/* In certain cases where a ML sta associated and added subset of
9475 		 * links on which the ML AP is active, but now sends some frame
9476 		 * (ex. Probe request) on a different link which is active in our
9477 		 * MLD but was not added during previous association, we can
9478 		 * still honor the Tx to that ML STA via the requested link.
9479 		 * The control would reach here in such case only when that link
9480 		 * address is same as the MLD address or in worst case clients
9481 		 * used MLD address at TA wrongly which would have helped
9482 		 * identify the ML sta object and pass it here.
9483 		 * If the link address of that STA is different from MLD address,
9484 		 * then the sta object would be NULL and control won't reach
9485 		 * here but return at the start of the function itself with !sta
9486 		 * check. Also this would not need any translation at hdr->addr1
9487 		 * from MLD to link address since the RA is the MLD address
9488 		 * (same as that link address ideally) already.
9489 		 */
9490 		ether_addr_copy(hdr->addr2, bss_conf->addr);
9491 
9492 		if (vif->type == NL80211_IFTYPE_STATION && bss_conf->bssid)
9493 			ether_addr_copy(hdr->addr3, bss_conf->bssid);
9494 		else if (vif->type == NL80211_IFTYPE_AP)
9495 			ether_addr_copy(hdr->addr3, bss_conf->addr);
9496 	}
9497 
9498 	return link;
9499 }
9500 EXPORT_SYMBOL(ath12k_mac_get_tx_link);
9501 
9502 void ath12k_mac_drain_tx(struct ath12k *ar)
9503 {
9504 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
9505 
9506 	/* make sure rcu-protected mac80211 tx path itself is drained */
9507 	synchronize_net();
9508 
9509 	wiphy_work_cancel(ath12k_ar_to_hw(ar)->wiphy, &ar->wmi_mgmt_tx_work);
9510 	ath12k_mgmt_over_wmi_tx_purge(ar);
9511 }
9512 
9513 static int ath12k_mac_config_mon_status_default(struct ath12k *ar, bool enable)
9514 {
9515 	struct htt_rx_ring_tlv_filter tlv_filter = {};
9516 	struct ath12k_base *ab = ar->ab;
9517 	u32 ring_id, i;
9518 	int ret = 0;
9519 
9520 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
9521 
9522 	if (!ab->hw_params->rxdma1_enable)
9523 		return ret;
9524 
9525 	if (enable) {
9526 		tlv_filter = ath12k_mac_mon_status_filter_default;
9527 
9528 		if (ath12k_debugfs_rx_filter(ar))
9529 			tlv_filter.rx_filter = ath12k_debugfs_rx_filter(ar);
9530 	} else {
9531 		tlv_filter.rxmon_disable = true;
9532 	}
9533 
9534 	for (i = 0; i < ab->hw_params->num_rxdma_per_pdev; i++) {
9535 		ring_id = ar->dp.rxdma_mon_dst_ring[i].ring_id;
9536 		ret = ath12k_dp_tx_htt_rx_filter_setup(ab, ring_id,
9537 						       ar->dp.mac_id + i,
9538 						       HAL_RXDMA_MONITOR_DST,
9539 						       DP_RXDMA_REFILL_RING_SIZE,
9540 						       &tlv_filter);
9541 		if (ret) {
9542 			ath12k_err(ab,
9543 				   "failed to setup filter for monitor buf %d\n",
9544 				   ret);
9545 		}
9546 	}
9547 
9548 	return ret;
9549 }
9550 
9551 static int ath12k_mac_start(struct ath12k *ar)
9552 {
9553 	struct ath12k_hw *ah = ar->ah;
9554 	struct ath12k_base *ab = ar->ab;
9555 	struct ath12k_pdev *pdev = ar->pdev;
9556 	int ret;
9557 
9558 	lockdep_assert_held(&ah->hw_mutex);
9559 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
9560 
9561 	ret = ath12k_wmi_pdev_set_param(ar, WMI_PDEV_PARAM_PMF_QOS,
9562 					1, pdev->pdev_id);
9563 
9564 	if (ret) {
9565 		ath12k_err(ab, "failed to enable PMF QOS: %d\n", ret);
9566 		goto err;
9567 	}
9568 
9569 	ret = ath12k_wmi_pdev_set_param(ar, WMI_PDEV_PARAM_DYNAMIC_BW, 1,
9570 					pdev->pdev_id);
9571 	if (ret) {
9572 		ath12k_err(ab, "failed to enable dynamic bw: %d\n", ret);
9573 		goto err;
9574 	}
9575 
9576 	ret = ath12k_wmi_pdev_set_param(ar, WMI_PDEV_PARAM_ARP_AC_OVERRIDE,
9577 					0, pdev->pdev_id);
9578 	if (ret) {
9579 		ath12k_err(ab, "failed to set ac override for ARP: %d\n",
9580 			   ret);
9581 		goto err;
9582 	}
9583 
9584 	ret = ath12k_wmi_send_dfs_phyerr_offload_enable_cmd(ar, pdev->pdev_id);
9585 	if (ret) {
9586 		ath12k_err(ab, "failed to offload radar detection: %d\n",
9587 			   ret);
9588 		goto err;
9589 	}
9590 
9591 	ret = ath12k_dp_tx_htt_h2t_ppdu_stats_req(ar,
9592 						  HTT_PPDU_STATS_TAG_DEFAULT);
9593 	if (ret) {
9594 		ath12k_err(ab, "failed to req ppdu stats: %d\n", ret);
9595 		goto err;
9596 	}
9597 
9598 	ret = ath12k_wmi_pdev_set_param(ar, WMI_PDEV_PARAM_MESH_MCAST_ENABLE,
9599 					1, pdev->pdev_id);
9600 
9601 	if (ret) {
9602 		ath12k_err(ab, "failed to enable MESH MCAST ENABLE: (%d\n", ret);
9603 		goto err;
9604 	}
9605 
9606 	__ath12k_set_antenna(ar, ar->cfg_tx_chainmask, ar->cfg_rx_chainmask);
9607 
9608 	/* TODO: Do we need to enable ANI? */
9609 
9610 	ret = ath12k_reg_update_chan_list(ar, false);
9611 
9612 	/* The ar state alone can be turned off for non supported country
9613 	 * without returning the error value. As we need to update the channel
9614 	 * for the next ar.
9615 	 */
9616 	if (ret) {
9617 		if (ret == -EINVAL)
9618 			ret = 0;
9619 		goto err;
9620 	}
9621 
9622 	ar->num_started_vdevs = 0;
9623 	ar->num_created_vdevs = 0;
9624 	ar->num_peers = 0;
9625 	ar->allocated_vdev_map = 0;
9626 	ar->chan_tx_pwr = ATH12K_PDEV_TX_POWER_INVALID;
9627 
9628 	/* Configure monitor status ring with default rx_filter to get rx status
9629 	 * such as rssi, rx_duration.
9630 	 */
9631 	ret = ath12k_mac_config_mon_status_default(ar, true);
9632 	if (ret && (ret != -EOPNOTSUPP)) {
9633 		ath12k_err(ab, "failed to configure monitor status ring with default rx_filter: (%d)\n",
9634 			   ret);
9635 		goto err;
9636 	}
9637 
9638 	if (ret == -EOPNOTSUPP)
9639 		ath12k_dbg(ab, ATH12K_DBG_MAC,
9640 			   "monitor status config is not yet supported");
9641 
9642 	/* Configure the hash seed for hash based reo dest ring selection */
9643 	ath12k_wmi_pdev_lro_cfg(ar, ar->pdev->pdev_id);
9644 
9645 	/* allow device to enter IMPS */
9646 	if (ab->hw_params->idle_ps) {
9647 		ret = ath12k_wmi_pdev_set_param(ar, WMI_PDEV_PARAM_IDLE_PS_CONFIG,
9648 						1, pdev->pdev_id);
9649 		if (ret) {
9650 			ath12k_err(ab, "failed to enable idle ps: %d\n", ret);
9651 			goto err;
9652 		}
9653 	}
9654 
9655 	rcu_assign_pointer(ab->pdevs_active[ar->pdev_idx],
9656 			   &ab->pdevs[ar->pdev_idx]);
9657 
9658 	return 0;
9659 err:
9660 
9661 	return ret;
9662 }
9663 
9664 static void ath12k_drain_tx(struct ath12k_hw *ah)
9665 {
9666 	struct ath12k *ar;
9667 	int i;
9668 
9669 	lockdep_assert_wiphy(ah->hw->wiphy);
9670 
9671 	for_each_ar(ah, ar, i)
9672 		ath12k_mac_drain_tx(ar);
9673 }
9674 
9675 int ath12k_mac_op_start(struct ieee80211_hw *hw)
9676 {
9677 	struct ath12k_hw *ah = ath12k_hw_to_ah(hw);
9678 	struct ath12k *ar;
9679 	int ret, i;
9680 
9681 	if (ath12k_ftm_mode)
9682 		return -EPERM;
9683 
9684 	lockdep_assert_wiphy(hw->wiphy);
9685 
9686 	ath12k_drain_tx(ah);
9687 
9688 	guard(mutex)(&ah->hw_mutex);
9689 
9690 	switch (ah->state) {
9691 	case ATH12K_HW_STATE_OFF:
9692 		ah->state = ATH12K_HW_STATE_ON;
9693 		break;
9694 	case ATH12K_HW_STATE_RESTARTING:
9695 		ah->state = ATH12K_HW_STATE_RESTARTED;
9696 		break;
9697 	case ATH12K_HW_STATE_RESTARTED:
9698 	case ATH12K_HW_STATE_WEDGED:
9699 	case ATH12K_HW_STATE_ON:
9700 	case ATH12K_HW_STATE_TM:
9701 		ah->state = ATH12K_HW_STATE_OFF;
9702 
9703 		WARN_ON(1);
9704 		return -EINVAL;
9705 	}
9706 
9707 	for_each_ar(ah, ar, i) {
9708 		ret = ath12k_mac_start(ar);
9709 		if (ret) {
9710 			ah->state = ATH12K_HW_STATE_OFF;
9711 
9712 			ath12k_err(ar->ab, "fail to start mac operations in pdev idx %d ret %d\n",
9713 				   ar->pdev_idx, ret);
9714 			goto fail_start;
9715 		}
9716 	}
9717 
9718 	return 0;
9719 
9720 fail_start:
9721 	for (; i > 0; i--) {
9722 		ar = ath12k_ah_to_ar(ah, i - 1);
9723 		ath12k_mac_stop(ar);
9724 	}
9725 
9726 	return ret;
9727 }
9728 EXPORT_SYMBOL(ath12k_mac_op_start);
9729 
9730 int ath12k_mac_rfkill_config(struct ath12k *ar)
9731 {
9732 	struct ath12k_base *ab = ar->ab;
9733 	u32 param;
9734 	int ret;
9735 
9736 	if (ab->hw_params->rfkill_pin == 0)
9737 		return -EOPNOTSUPP;
9738 
9739 	ath12k_dbg(ab, ATH12K_DBG_MAC,
9740 		   "mac rfkill_pin %d rfkill_cfg %d rfkill_on_level %d",
9741 		   ab->hw_params->rfkill_pin, ab->hw_params->rfkill_cfg,
9742 		   ab->hw_params->rfkill_on_level);
9743 
9744 	param = u32_encode_bits(ab->hw_params->rfkill_on_level,
9745 				WMI_RFKILL_CFG_RADIO_LEVEL) |
9746 		u32_encode_bits(ab->hw_params->rfkill_pin,
9747 				WMI_RFKILL_CFG_GPIO_PIN_NUM) |
9748 		u32_encode_bits(ab->hw_params->rfkill_cfg,
9749 				WMI_RFKILL_CFG_PIN_AS_GPIO);
9750 
9751 	ret = ath12k_wmi_pdev_set_param(ar, WMI_PDEV_PARAM_HW_RFKILL_CONFIG,
9752 					param, ar->pdev->pdev_id);
9753 	if (ret) {
9754 		ath12k_warn(ab,
9755 			    "failed to set rfkill config 0x%x: %d\n",
9756 			    param, ret);
9757 		return ret;
9758 	}
9759 
9760 	return 0;
9761 }
9762 
9763 int ath12k_mac_rfkill_enable_radio(struct ath12k *ar, bool enable)
9764 {
9765 	enum wmi_rfkill_enable_radio param;
9766 	int ret;
9767 
9768 	if (enable)
9769 		param = WMI_RFKILL_ENABLE_RADIO_ON;
9770 	else
9771 		param = WMI_RFKILL_ENABLE_RADIO_OFF;
9772 
9773 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac %d rfkill enable %d",
9774 		   ar->pdev_idx, param);
9775 
9776 	ret = ath12k_wmi_pdev_set_param(ar, WMI_PDEV_PARAM_RFKILL_ENABLE,
9777 					param, ar->pdev->pdev_id);
9778 	if (ret) {
9779 		ath12k_warn(ar->ab, "failed to set rfkill enable param %d: %d\n",
9780 			    param, ret);
9781 		return ret;
9782 	}
9783 
9784 	return 0;
9785 }
9786 
9787 static void ath12k_mac_stop(struct ath12k *ar)
9788 {
9789 	struct ath12k_pdev_dp *dp_pdev = &ar->dp;
9790 	struct ath12k_hw *ah = ar->ah;
9791 	struct htt_ppdu_stats_info *ppdu_stats, *tmp;
9792 	struct ath12k_wmi_scan_chan_list_arg *arg;
9793 	int ret;
9794 
9795 	lockdep_assert_held(&ah->hw_mutex);
9796 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
9797 
9798 	ret = ath12k_mac_config_mon_status_default(ar, false);
9799 	if (ret && (ret != -EOPNOTSUPP))
9800 		ath12k_err(ar->ab, "failed to clear rx_filter for monitor status ring: (%d)\n",
9801 			   ret);
9802 
9803 	clear_bit(ATH12K_FLAG_CAC_RUNNING, &ar->dev_flags);
9804 
9805 	cancel_delayed_work_sync(&ar->scan.timeout);
9806 	wiphy_work_cancel(ath12k_ar_to_hw(ar)->wiphy, &ar->scan.vdev_clean_wk);
9807 	cancel_work_sync(&ar->regd_channel_update_work);
9808 	cancel_work_sync(&ar->regd_update_work);
9809 	cancel_work_sync(&ar->ab->rfkill_work);
9810 	cancel_work_sync(&ar->ab->update_11d_work);
9811 	ar->state_11d = ATH12K_11D_IDLE;
9812 	complete(&ar->completed_11d_scan);
9813 
9814 	spin_lock_bh(&dp_pdev->ppdu_list_lock);
9815 	list_for_each_entry_safe(ppdu_stats, tmp, &dp_pdev->ppdu_stats_info, list) {
9816 		list_del(&ppdu_stats->list);
9817 		kfree(ppdu_stats);
9818 	}
9819 	spin_unlock_bh(&dp_pdev->ppdu_list_lock);
9820 
9821 	spin_lock_bh(&ar->data_lock);
9822 	while ((arg = list_first_entry_or_null(&ar->regd_channel_update_queue,
9823 					       struct ath12k_wmi_scan_chan_list_arg,
9824 					       list))) {
9825 		list_del(&arg->list);
9826 		kfree(arg);
9827 	}
9828 	spin_unlock_bh(&ar->data_lock);
9829 
9830 	rcu_assign_pointer(ar->ab->pdevs_active[ar->pdev_idx], NULL);
9831 
9832 	synchronize_rcu();
9833 
9834 	atomic_set(&ar->num_pending_mgmt_tx, 0);
9835 }
9836 
9837 void ath12k_mac_op_stop(struct ieee80211_hw *hw, bool suspend)
9838 {
9839 	struct ath12k_hw *ah = ath12k_hw_to_ah(hw);
9840 	struct ath12k *ar;
9841 	int i;
9842 
9843 	lockdep_assert_wiphy(hw->wiphy);
9844 
9845 	ath12k_drain_tx(ah);
9846 
9847 	mutex_lock(&ah->hw_mutex);
9848 
9849 	ah->state = ATH12K_HW_STATE_OFF;
9850 
9851 	for_each_ar(ah, ar, i)
9852 		ath12k_mac_stop(ar);
9853 
9854 	mutex_unlock(&ah->hw_mutex);
9855 }
9856 EXPORT_SYMBOL(ath12k_mac_op_stop);
9857 
9858 static u8
9859 ath12k_mac_get_vdev_stats_id(struct ath12k_link_vif *arvif)
9860 {
9861 	struct ath12k_base *ab = arvif->ar->ab;
9862 	u8 vdev_stats_id = 0;
9863 
9864 	do {
9865 		if (ab->free_vdev_stats_id_map & (1LL << vdev_stats_id)) {
9866 			vdev_stats_id++;
9867 			if (vdev_stats_id >= ATH12K_MAX_VDEV_STATS_ID) {
9868 				vdev_stats_id = ATH12K_INVAL_VDEV_STATS_ID;
9869 				break;
9870 			}
9871 		} else {
9872 			ab->free_vdev_stats_id_map |= (1LL << vdev_stats_id);
9873 			break;
9874 		}
9875 	} while (vdev_stats_id);
9876 
9877 	arvif->vdev_stats_id = vdev_stats_id;
9878 	return vdev_stats_id;
9879 }
9880 
9881 static int ath12k_mac_setup_vdev_params_mbssid(struct ath12k_link_vif *arvif,
9882 					       u32 *flags, u32 *tx_vdev_id)
9883 {
9884 	struct ath12k_vif *ahvif = arvif->ahvif;
9885 	struct ieee80211_bss_conf *link_conf;
9886 	struct ath12k *ar = arvif->ar;
9887 	struct ath12k_link_vif *tx_arvif;
9888 
9889 	link_conf = ath12k_mac_get_link_bss_conf(arvif);
9890 	if (!link_conf) {
9891 		ath12k_warn(ar->ab, "unable to access bss link conf in set mbssid params for vif %pM link %u\n",
9892 			    ahvif->vif->addr, arvif->link_id);
9893 		return -ENOLINK;
9894 	}
9895 
9896 	tx_arvif = ath12k_mac_get_tx_arvif(arvif, link_conf);
9897 	if (!tx_arvif)
9898 		return 0;
9899 
9900 	if (link_conf->nontransmitted) {
9901 		if (ath12k_ar_to_hw(ar)->wiphy !=
9902 		    ath12k_ar_to_hw(tx_arvif->ar)->wiphy)
9903 			return -EINVAL;
9904 
9905 		*flags = WMI_VDEV_MBSSID_FLAGS_NON_TRANSMIT_AP;
9906 		*tx_vdev_id = tx_arvif->vdev_id;
9907 	} else if (tx_arvif == arvif) {
9908 		*flags = WMI_VDEV_MBSSID_FLAGS_TRANSMIT_AP;
9909 	} else {
9910 		return -EINVAL;
9911 	}
9912 
9913 	if (link_conf->ema_ap)
9914 		*flags |= WMI_VDEV_MBSSID_FLAGS_EMA_MODE;
9915 
9916 	return 0;
9917 }
9918 
9919 static int ath12k_mac_setup_vdev_create_arg(struct ath12k_link_vif *arvif,
9920 					    struct ath12k_wmi_vdev_create_arg *arg)
9921 {
9922 	struct ath12k *ar = arvif->ar;
9923 	struct ath12k_pdev *pdev = ar->pdev;
9924 	struct ath12k_vif *ahvif = arvif->ahvif;
9925 	int ret;
9926 
9927 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
9928 
9929 	arg->if_id = arvif->vdev_id;
9930 	arg->type = ahvif->vdev_type;
9931 	arg->subtype = ahvif->vdev_subtype;
9932 	arg->pdev_id = pdev->pdev_id;
9933 
9934 	arg->mbssid_flags = WMI_VDEV_MBSSID_FLAGS_NON_MBSSID_AP;
9935 	arg->mbssid_tx_vdev_id = 0;
9936 	if (!test_bit(WMI_TLV_SERVICE_MBSS_PARAM_IN_VDEV_START_SUPPORT,
9937 		      ar->ab->wmi_ab.svc_map)) {
9938 		ret = ath12k_mac_setup_vdev_params_mbssid(arvif,
9939 							  &arg->mbssid_flags,
9940 							  &arg->mbssid_tx_vdev_id);
9941 		if (ret)
9942 			return ret;
9943 	}
9944 
9945 	if (pdev->cap.supported_bands & WMI_HOST_WLAN_2GHZ_CAP) {
9946 		arg->chains[NL80211_BAND_2GHZ].tx = ar->num_tx_chains;
9947 		arg->chains[NL80211_BAND_2GHZ].rx = ar->num_rx_chains;
9948 	}
9949 	if (pdev->cap.supported_bands & WMI_HOST_WLAN_5GHZ_CAP) {
9950 		arg->chains[NL80211_BAND_5GHZ].tx = ar->num_tx_chains;
9951 		arg->chains[NL80211_BAND_5GHZ].rx = ar->num_rx_chains;
9952 	}
9953 	if (pdev->cap.supported_bands & WMI_HOST_WLAN_5GHZ_CAP &&
9954 	    ar->supports_6ghz) {
9955 		arg->chains[NL80211_BAND_6GHZ].tx = ar->num_tx_chains;
9956 		arg->chains[NL80211_BAND_6GHZ].rx = ar->num_rx_chains;
9957 	}
9958 
9959 	arg->if_stats_id = ath12k_mac_get_vdev_stats_id(arvif);
9960 
9961 	if (ath12k_mac_is_ml_arvif(arvif)) {
9962 		if (hweight16(ahvif->vif->valid_links) > ATH12K_WMI_MLO_MAX_LINKS) {
9963 			ath12k_warn(ar->ab, "too many MLO links during setting up vdev: %d",
9964 				    ahvif->vif->valid_links);
9965 			return -EINVAL;
9966 		}
9967 
9968 		ether_addr_copy(arg->mld_addr, ahvif->vif->addr);
9969 	}
9970 
9971 	return 0;
9972 }
9973 
9974 static void ath12k_mac_update_vif_offload(struct ath12k_link_vif *arvif)
9975 {
9976 	struct ath12k_vif *ahvif = arvif->ahvif;
9977 	struct ieee80211_vif *vif = ath12k_ahvif_to_vif(ahvif);
9978 	struct ath12k *ar = arvif->ar;
9979 	struct ath12k_base *ab = ar->ab;
9980 	u32 param_id, param_value;
9981 	int ret;
9982 
9983 	param_id = WMI_VDEV_PARAM_TX_ENCAP_TYPE;
9984 	if (vif->type != NL80211_IFTYPE_STATION &&
9985 	    vif->type != NL80211_IFTYPE_AP)
9986 		vif->offload_flags &= ~(IEEE80211_OFFLOAD_ENCAP_ENABLED |
9987 					IEEE80211_OFFLOAD_DECAP_ENABLED);
9988 
9989 	if (vif->offload_flags & IEEE80211_OFFLOAD_ENCAP_ENABLED)
9990 		ahvif->dp_vif.tx_encap_type = ATH12K_HW_TXRX_ETHERNET;
9991 	else if (test_bit(ATH12K_FLAG_RAW_MODE, &ab->dev_flags))
9992 		ahvif->dp_vif.tx_encap_type = ATH12K_HW_TXRX_RAW;
9993 	else
9994 		ahvif->dp_vif.tx_encap_type = ATH12K_HW_TXRX_NATIVE_WIFI;
9995 
9996 	ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
9997 					    param_id, ahvif->dp_vif.tx_encap_type);
9998 	if (ret) {
9999 		ath12k_warn(ab, "failed to set vdev %d tx encap mode: %d\n",
10000 			    arvif->vdev_id, ret);
10001 		vif->offload_flags &= ~IEEE80211_OFFLOAD_ENCAP_ENABLED;
10002 	}
10003 
10004 	param_id = WMI_VDEV_PARAM_RX_DECAP_TYPE;
10005 	if (vif->offload_flags & IEEE80211_OFFLOAD_DECAP_ENABLED)
10006 		param_value = ATH12K_HW_TXRX_ETHERNET;
10007 	else if (test_bit(ATH12K_FLAG_RAW_MODE, &ab->dev_flags))
10008 		param_value = ATH12K_HW_TXRX_RAW;
10009 	else
10010 		param_value = ATH12K_HW_TXRX_NATIVE_WIFI;
10011 
10012 	ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
10013 					    param_id, param_value);
10014 	if (ret) {
10015 		ath12k_warn(ab, "failed to set vdev %d rx decap mode: %d\n",
10016 			    arvif->vdev_id, ret);
10017 		vif->offload_flags &= ~IEEE80211_OFFLOAD_DECAP_ENABLED;
10018 	}
10019 }
10020 
10021 void ath12k_mac_op_update_vif_offload(struct ieee80211_hw *hw,
10022 				      struct ieee80211_vif *vif)
10023 {
10024 	struct ath12k_vif *ahvif = ath12k_vif_to_ahvif(vif);
10025 	struct ath12k_link_vif *arvif;
10026 	unsigned long links;
10027 	int link_id;
10028 
10029 	lockdep_assert_wiphy(hw->wiphy);
10030 
10031 	if (vif->valid_links) {
10032 		links = vif->valid_links;
10033 		for_each_set_bit(link_id, &links, IEEE80211_MLD_MAX_NUM_LINKS) {
10034 			arvif = wiphy_dereference(hw->wiphy, ahvif->link[link_id]);
10035 			if (!(arvif && arvif->ar))
10036 				continue;
10037 
10038 			ath12k_mac_update_vif_offload(arvif);
10039 		}
10040 
10041 		return;
10042 	}
10043 
10044 	ath12k_mac_update_vif_offload(&ahvif->deflink);
10045 }
10046 EXPORT_SYMBOL(ath12k_mac_op_update_vif_offload);
10047 
10048 static bool ath12k_mac_vif_ap_active_any(struct ath12k_base *ab)
10049 {
10050 	struct ath12k *ar;
10051 	struct ath12k_pdev *pdev;
10052 	struct ath12k_link_vif *arvif;
10053 	int i;
10054 
10055 	for (i = 0; i < ab->num_radios; i++) {
10056 		pdev = &ab->pdevs[i];
10057 		ar = pdev->ar;
10058 		list_for_each_entry(arvif, &ar->arvifs, list) {
10059 			if (arvif->is_up &&
10060 			    arvif->ahvif->vdev_type == WMI_VDEV_TYPE_AP)
10061 				return true;
10062 		}
10063 	}
10064 	return false;
10065 }
10066 
10067 void ath12k_mac_11d_scan_start(struct ath12k *ar, u32 vdev_id)
10068 {
10069 	struct wmi_11d_scan_start_arg arg;
10070 	int ret;
10071 
10072 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
10073 
10074 	if (ar->regdom_set_by_user)
10075 		goto fin;
10076 
10077 	if (ar->vdev_id_11d_scan != ATH12K_11D_INVALID_VDEV_ID)
10078 		goto fin;
10079 
10080 	if (!test_bit(WMI_TLV_SERVICE_11D_OFFLOAD, ar->ab->wmi_ab.svc_map))
10081 		goto fin;
10082 
10083 	if (ath12k_mac_vif_ap_active_any(ar->ab))
10084 		goto fin;
10085 
10086 	arg.vdev_id = vdev_id;
10087 	arg.start_interval_msec = 0;
10088 	arg.scan_period_msec = ATH12K_SCAN_11D_INTERVAL;
10089 
10090 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
10091 		   "mac start 11d scan for vdev %d\n", vdev_id);
10092 
10093 	ret = ath12k_wmi_send_11d_scan_start_cmd(ar, &arg);
10094 	if (ret) {
10095 		ath12k_warn(ar->ab, "failed to start 11d scan vdev %d ret: %d\n",
10096 			    vdev_id, ret);
10097 	} else {
10098 		ar->vdev_id_11d_scan = vdev_id;
10099 		if (ar->state_11d == ATH12K_11D_PREPARING)
10100 			ar->state_11d = ATH12K_11D_RUNNING;
10101 	}
10102 
10103 fin:
10104 	if (ar->state_11d == ATH12K_11D_PREPARING) {
10105 		ar->state_11d = ATH12K_11D_IDLE;
10106 		complete(&ar->completed_11d_scan);
10107 	}
10108 }
10109 
10110 void ath12k_mac_11d_scan_stop(struct ath12k *ar)
10111 {
10112 	int ret;
10113 	u32 vdev_id;
10114 
10115 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
10116 
10117 	if (!test_bit(WMI_TLV_SERVICE_11D_OFFLOAD, ar->ab->wmi_ab.svc_map))
10118 		return;
10119 
10120 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac stop 11d for vdev %d\n",
10121 		   ar->vdev_id_11d_scan);
10122 
10123 	if (ar->state_11d == ATH12K_11D_PREPARING) {
10124 		ar->state_11d = ATH12K_11D_IDLE;
10125 		complete(&ar->completed_11d_scan);
10126 	}
10127 
10128 	if (ar->vdev_id_11d_scan != ATH12K_11D_INVALID_VDEV_ID) {
10129 		vdev_id = ar->vdev_id_11d_scan;
10130 
10131 		ret = ath12k_wmi_send_11d_scan_stop_cmd(ar, vdev_id);
10132 		if (ret) {
10133 			ath12k_warn(ar->ab,
10134 				    "failed to stopt 11d scan vdev %d ret: %d\n",
10135 				    vdev_id, ret);
10136 		} else {
10137 			ar->vdev_id_11d_scan = ATH12K_11D_INVALID_VDEV_ID;
10138 			ar->state_11d = ATH12K_11D_IDLE;
10139 			complete(&ar->completed_11d_scan);
10140 		}
10141 	}
10142 }
10143 
10144 void ath12k_mac_11d_scan_stop_all(struct ath12k_base *ab)
10145 {
10146 	struct ath12k *ar;
10147 	struct ath12k_pdev *pdev;
10148 	int i;
10149 
10150 	ath12k_dbg(ab, ATH12K_DBG_MAC, "mac stop soc 11d scan\n");
10151 
10152 	for (i = 0; i < ab->num_radios; i++) {
10153 		pdev = &ab->pdevs[i];
10154 		ar = pdev->ar;
10155 
10156 		ath12k_mac_11d_scan_stop(ar);
10157 	}
10158 }
10159 
10160 static void ath12k_mac_determine_vdev_type(struct ieee80211_vif *vif,
10161 					   struct ath12k_vif *ahvif)
10162 {
10163 	ahvif->vdev_subtype = WMI_VDEV_SUBTYPE_NONE;
10164 
10165 	switch (vif->type) {
10166 	case NL80211_IFTYPE_UNSPECIFIED:
10167 	case NL80211_IFTYPE_STATION:
10168 		ahvif->vdev_type = WMI_VDEV_TYPE_STA;
10169 
10170 		if (vif->p2p)
10171 			ahvif->vdev_subtype = WMI_VDEV_SUBTYPE_P2P_CLIENT;
10172 
10173 		break;
10174 	case NL80211_IFTYPE_MESH_POINT:
10175 		ahvif->vdev_subtype = WMI_VDEV_SUBTYPE_MESH_11S;
10176 		fallthrough;
10177 	case NL80211_IFTYPE_AP:
10178 		ahvif->vdev_type = WMI_VDEV_TYPE_AP;
10179 
10180 		if (vif->p2p)
10181 			ahvif->vdev_subtype = WMI_VDEV_SUBTYPE_P2P_GO;
10182 
10183 		break;
10184 	case NL80211_IFTYPE_MONITOR:
10185 		ahvif->vdev_type = WMI_VDEV_TYPE_MONITOR;
10186 		break;
10187 	case NL80211_IFTYPE_P2P_DEVICE:
10188 		ahvif->vdev_type = WMI_VDEV_TYPE_STA;
10189 		ahvif->vdev_subtype = WMI_VDEV_SUBTYPE_P2P_DEVICE;
10190 		break;
10191 	default:
10192 		WARN_ON(1);
10193 		break;
10194 	}
10195 }
10196 
10197 int ath12k_mac_vdev_create(struct ath12k *ar, struct ath12k_link_vif *arvif)
10198 {
10199 	struct ath12k_hw *ah = ar->ah;
10200 	struct ath12k_base *ab = ar->ab;
10201 	struct ieee80211_hw *hw = ah->hw;
10202 	struct ath12k_vif *ahvif = arvif->ahvif;
10203 	struct ieee80211_vif *vif = ath12k_ahvif_to_vif(ahvif);
10204 	struct ath12k_wmi_vdev_create_arg vdev_arg = {};
10205 	struct ath12k_wmi_peer_create_arg peer_param = {};
10206 	struct ieee80211_bss_conf *link_conf = NULL;
10207 	u32 param_id, param_value;
10208 	u16 nss;
10209 	int i;
10210 	int ret, vdev_id;
10211 	u8 link_id;
10212 	struct ath12k_dp_link_vif *dp_link_vif = NULL;
10213 	struct ath12k_dp_peer_create_params params = {};
10214 	bool dp_peer_created = false;
10215 
10216 	lockdep_assert_wiphy(hw->wiphy);
10217 
10218 	/* In NO_VIRTUAL_MONITOR, its necessary to restrict only one monitor
10219 	 * interface in each radio
10220 	 */
10221 	if (vif->type == NL80211_IFTYPE_MONITOR && ar->monitor_vdev_created)
10222 		return -EINVAL;
10223 
10224 	if (ar->num_created_vdevs >= TARGET_NUM_VDEVS(ab)) {
10225 		ath12k_warn(ab, "failed to create vdev, reached max vdev limit %d\n",
10226 			    TARGET_NUM_VDEVS(ab));
10227 		return -ENOSPC;
10228 	}
10229 
10230 	link_id = arvif->link_id;
10231 
10232 	if (link_id < IEEE80211_MLD_MAX_NUM_LINKS) {
10233 		link_conf = wiphy_dereference(hw->wiphy, vif->link_conf[link_id]);
10234 		if (!link_conf) {
10235 			ath12k_warn(ar->ab, "unable to access bss link conf in vdev create for vif %pM link %u\n",
10236 				    vif->addr, arvif->link_id);
10237 			return -ENOLINK;
10238 		}
10239 	}
10240 
10241 	if (link_conf)
10242 		memcpy(arvif->bssid, link_conf->addr, ETH_ALEN);
10243 	else
10244 		memcpy(arvif->bssid, vif->addr, ETH_ALEN);
10245 
10246 	arvif->ar = ar;
10247 	vdev_id = __ffs64(ab->free_vdev_map);
10248 	arvif->vdev_id = vdev_id;
10249 	if (vif->type == NL80211_IFTYPE_MONITOR)
10250 		ar->monitor_vdev_id = vdev_id;
10251 
10252 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac vdev create id %d type %d subtype %d map %llx\n",
10253 		   arvif->vdev_id, ahvif->vdev_type, ahvif->vdev_subtype,
10254 		   ab->free_vdev_map);
10255 
10256 	vif->cab_queue = arvif->vdev_id % (ATH12K_HW_MAX_QUEUES - 1);
10257 	for (i = 0; i < ARRAY_SIZE(vif->hw_queue); i++)
10258 		vif->hw_queue[i] = i % (ATH12K_HW_MAX_QUEUES - 1);
10259 
10260 	ret = ath12k_mac_setup_vdev_create_arg(arvif, &vdev_arg);
10261 	if (ret) {
10262 		ath12k_warn(ab, "failed to create vdev parameters %d: %d\n",
10263 			    arvif->vdev_id, ret);
10264 		goto err;
10265 	}
10266 
10267 	ret = ath12k_wmi_vdev_create(ar, arvif->bssid, &vdev_arg);
10268 	if (ret) {
10269 		ath12k_warn(ab, "failed to create WMI vdev %d: %d\n",
10270 			    arvif->vdev_id, ret);
10271 		return ret;
10272 	}
10273 
10274 	ar->num_created_vdevs++;
10275 	arvif->is_created = true;
10276 	ath12k_dbg(ab, ATH12K_DBG_MAC, "vdev %pM created, vdev_id %d\n",
10277 		   vif->addr, arvif->vdev_id);
10278 	ar->allocated_vdev_map |= 1LL << arvif->vdev_id;
10279 	ab->free_vdev_map &= ~(1LL << arvif->vdev_id);
10280 
10281 	spin_lock_bh(&ar->data_lock);
10282 	list_add(&arvif->list, &ar->arvifs);
10283 	spin_unlock_bh(&ar->data_lock);
10284 
10285 	ath12k_mac_update_vif_offload(arvif);
10286 
10287 	nss = hweight32(ar->cfg_tx_chainmask) ? : 1;
10288 	ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
10289 					    WMI_VDEV_PARAM_NSS, nss);
10290 	if (ret) {
10291 		ath12k_warn(ab, "failed to set vdev %d chainmask 0x%x, nss %d :%d\n",
10292 			    arvif->vdev_id, ar->cfg_tx_chainmask, nss, ret);
10293 		goto err_vdev_del;
10294 	}
10295 
10296 	dp_link_vif = ath12k_dp_vif_to_dp_link_vif(&ahvif->dp_vif, arvif->link_id);
10297 
10298 	dp_link_vif->vdev_id = arvif->vdev_id;
10299 	dp_link_vif->lmac_id = ar->lmac_id;
10300 	dp_link_vif->pdev_idx = ar->pdev_idx;
10301 
10302 	switch (ahvif->vdev_type) {
10303 	case WMI_VDEV_TYPE_AP:
10304 		params.ucast_ra_only = true;
10305 
10306 		if (arvif->link_id < IEEE80211_MLD_MAX_NUM_LINKS) {
10307 			ret = ath12k_dp_peer_create(&ah->dp_hw, arvif->bssid, &params);
10308 			if (ret) {
10309 				ath12k_warn(ab, "failed to vdev %d create dp_peer for AP: %d\n",
10310 					    arvif->vdev_id, ret);
10311 				goto err_vdev_del;
10312 			}
10313 			dp_peer_created = true;
10314 		}
10315 
10316 		peer_param.vdev_id = arvif->vdev_id;
10317 		peer_param.peer_addr = arvif->bssid;
10318 		peer_param.peer_type = WMI_PEER_TYPE_DEFAULT;
10319 		ret = ath12k_peer_create(ar, arvif, NULL, &peer_param);
10320 		if (ret) {
10321 			ath12k_warn(ab, "failed to vdev %d create peer for AP: %d\n",
10322 				    arvif->vdev_id, ret);
10323 			goto err_dp_peer_del;
10324 		}
10325 
10326 		ret = ath12k_mac_set_kickout(arvif);
10327 		if (ret) {
10328 			ath12k_warn(ar->ab, "failed to set vdev %i kickout parameters: %d\n",
10329 				    arvif->vdev_id, ret);
10330 			goto err_peer_del;
10331 		}
10332 		ath12k_mac_11d_scan_stop_all(ar->ab);
10333 		break;
10334 	case WMI_VDEV_TYPE_STA:
10335 		param_id = WMI_STA_PS_PARAM_RX_WAKE_POLICY;
10336 		param_value = WMI_STA_PS_RX_WAKE_POLICY_WAKE;
10337 		ret = ath12k_wmi_set_sta_ps_param(ar, arvif->vdev_id,
10338 						  param_id, param_value);
10339 		if (ret) {
10340 			ath12k_warn(ar->ab, "failed to set vdev %d RX wake policy: %d\n",
10341 				    arvif->vdev_id, ret);
10342 			goto err_peer_del;
10343 		}
10344 
10345 		param_id = WMI_STA_PS_PARAM_TX_WAKE_THRESHOLD;
10346 		param_value = WMI_STA_PS_TX_WAKE_THRESHOLD_ALWAYS;
10347 		ret = ath12k_wmi_set_sta_ps_param(ar, arvif->vdev_id,
10348 						  param_id, param_value);
10349 		if (ret) {
10350 			ath12k_warn(ar->ab, "failed to set vdev %d TX wake threshold: %d\n",
10351 				    arvif->vdev_id, ret);
10352 			goto err_peer_del;
10353 		}
10354 
10355 		param_id = WMI_STA_PS_PARAM_PSPOLL_COUNT;
10356 		param_value = WMI_STA_PS_PSPOLL_COUNT_NO_MAX;
10357 		ret = ath12k_wmi_set_sta_ps_param(ar, arvif->vdev_id,
10358 						  param_id, param_value);
10359 		if (ret) {
10360 			ath12k_warn(ar->ab, "failed to set vdev %d pspoll count: %d\n",
10361 				    arvif->vdev_id, ret);
10362 			goto err_peer_del;
10363 		}
10364 
10365 		ret = ath12k_wmi_pdev_set_ps_mode(ar, arvif->vdev_id, false);
10366 		if (ret) {
10367 			ath12k_warn(ar->ab, "failed to disable vdev %d ps mode: %d\n",
10368 				    arvif->vdev_id, ret);
10369 			goto err_peer_del;
10370 		}
10371 
10372 		if (test_bit(WMI_TLV_SERVICE_11D_OFFLOAD, ab->wmi_ab.svc_map) &&
10373 		    ahvif->vdev_type == WMI_VDEV_TYPE_STA &&
10374 		    ahvif->vdev_subtype == WMI_VDEV_SUBTYPE_NONE) {
10375 			reinit_completion(&ar->completed_11d_scan);
10376 			ar->state_11d = ATH12K_11D_PREPARING;
10377 		}
10378 		break;
10379 	case WMI_VDEV_TYPE_MONITOR:
10380 		ar->monitor_vdev_created = true;
10381 		break;
10382 	default:
10383 		break;
10384 	}
10385 
10386 	if (link_conf)
10387 		arvif->txpower = link_conf->txpower;
10388 	else
10389 		arvif->txpower = NL80211_TX_POWER_AUTOMATIC;
10390 
10391 	ret = ath12k_mac_txpower_recalc(ar);
10392 	if (ret)
10393 		goto err_peer_del;
10394 
10395 	param_id = WMI_VDEV_PARAM_RTS_THRESHOLD;
10396 	param_value = hw->wiphy->rts_threshold;
10397 	ar->rts_threshold = param_value;
10398 	ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
10399 					    param_id, param_value);
10400 	if (ret) {
10401 		ath12k_warn(ar->ab, "failed to set rts threshold for vdev %d: %d\n",
10402 			    arvif->vdev_id, ret);
10403 	}
10404 
10405 	ath12k_dp_vdev_tx_attach(ar, arvif);
10406 
10407 	return ret;
10408 
10409 err_peer_del:
10410 	if (ahvif->vdev_type == WMI_VDEV_TYPE_AP) {
10411 		reinit_completion(&ar->peer_delete_done);
10412 
10413 		ret = ath12k_wmi_send_peer_delete_cmd(ar, arvif->bssid,
10414 						      arvif->vdev_id);
10415 		if (ret) {
10416 			ath12k_warn(ar->ab, "failed to delete peer vdev_id %d addr %pM\n",
10417 				    arvif->vdev_id, arvif->bssid);
10418 			goto err;
10419 		}
10420 
10421 		ret = ath12k_wait_for_peer_delete_done(ar, arvif->vdev_id,
10422 						       arvif->bssid);
10423 		if (ret)
10424 			goto err_vdev_del;
10425 
10426 		ar->num_peers--;
10427 	}
10428 
10429 err_dp_peer_del:
10430 	if (dp_peer_created)
10431 		ath12k_dp_peer_delete(&ah->dp_hw, arvif->bssid, NULL);
10432 
10433 err_vdev_del:
10434 	if (ahvif->vdev_type == WMI_VDEV_TYPE_MONITOR) {
10435 		ar->monitor_vdev_id = -1;
10436 		ar->monitor_vdev_created = false;
10437 	}
10438 
10439 	ath12k_wmi_vdev_delete(ar, arvif->vdev_id);
10440 	ar->num_created_vdevs--;
10441 	arvif->is_created = false;
10442 	arvif->ar = NULL;
10443 	ar->allocated_vdev_map &= ~(1LL << arvif->vdev_id);
10444 	ab->free_vdev_map |= 1LL << arvif->vdev_id;
10445 	ab->free_vdev_stats_id_map &= ~(1LL << arvif->vdev_stats_id);
10446 	spin_lock_bh(&ar->data_lock);
10447 	list_del(&arvif->list);
10448 	spin_unlock_bh(&ar->data_lock);
10449 
10450 err:
10451 	arvif->ar = NULL;
10452 	return ret;
10453 }
10454 
10455 static void ath12k_mac_vif_flush_key_cache(struct ath12k_link_vif *arvif)
10456 {
10457 	struct ath12k_key_conf *key_conf, *tmp;
10458 	struct ath12k_vif *ahvif = arvif->ahvif;
10459 	struct ath12k_hw *ah = ahvif->ah;
10460 	struct ath12k_sta *ahsta;
10461 	struct ath12k_link_sta *arsta;
10462 	struct ath12k_vif_cache *cache = ahvif->cache[arvif->link_id];
10463 	int ret;
10464 
10465 	lockdep_assert_wiphy(ah->hw->wiphy);
10466 
10467 	list_for_each_entry_safe(key_conf, tmp, &cache->key_conf.list, list) {
10468 		arsta = NULL;
10469 		if (key_conf->sta) {
10470 			ahsta = ath12k_sta_to_ahsta(key_conf->sta);
10471 			arsta = wiphy_dereference(ah->hw->wiphy,
10472 						  ahsta->link[arvif->link_id]);
10473 			if (!arsta)
10474 				goto free_cache;
10475 		}
10476 
10477 		ret = ath12k_mac_set_key(arvif->ar, key_conf->cmd,
10478 					 arvif, arsta,
10479 					 key_conf->key);
10480 		if (ret)
10481 			ath12k_warn(arvif->ar->ab, "unable to apply set key param to vdev %d ret %d\n",
10482 				    arvif->vdev_id, ret);
10483 free_cache:
10484 		list_del(&key_conf->list);
10485 		kfree(key_conf);
10486 	}
10487 }
10488 
10489 static void ath12k_mac_vif_cache_flush(struct ath12k *ar, struct ath12k_link_vif *arvif)
10490 {
10491 	struct ath12k_vif *ahvif = arvif->ahvif;
10492 	struct ieee80211_vif *vif = ath12k_ahvif_to_vif(ahvif);
10493 	struct ath12k_vif_cache *cache = ahvif->cache[arvif->link_id];
10494 	struct ath12k_base *ab = ar->ab;
10495 	struct ieee80211_bss_conf *link_conf;
10496 
10497 	int ret;
10498 
10499 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
10500 
10501 	if (!cache)
10502 		return;
10503 
10504 	if (cache->tx_conf.changed) {
10505 		ret = ath12k_mac_conf_tx(arvif, cache->tx_conf.ac,
10506 					 &cache->tx_conf.tx_queue_params);
10507 		if (ret)
10508 			ath12k_warn(ab,
10509 				    "unable to apply tx config parameters to vdev %d\n",
10510 				    ret);
10511 	}
10512 
10513 	if (cache->bss_conf_changed) {
10514 		link_conf = ath12k_mac_get_link_bss_conf(arvif);
10515 		if (!link_conf) {
10516 			ath12k_warn(ar->ab, "unable to access bss link conf in cache flush for vif %pM link %u\n",
10517 				    vif->addr, arvif->link_id);
10518 			return;
10519 		}
10520 		ath12k_mac_bss_info_changed(ar, arvif, link_conf,
10521 					    cache->bss_conf_changed);
10522 	}
10523 
10524 	if (!list_empty(&cache->key_conf.list))
10525 		ath12k_mac_vif_flush_key_cache(arvif);
10526 
10527 	ath12k_ahvif_put_link_cache(ahvif, arvif->link_id);
10528 }
10529 
10530 static struct ath12k *ath12k_mac_assign_vif_to_vdev(struct ieee80211_hw *hw,
10531 						    struct ath12k_link_vif *arvif,
10532 						    struct ieee80211_chanctx_conf *ctx)
10533 {
10534 	struct ath12k_vif *ahvif = arvif->ahvif;
10535 	struct ieee80211_vif *vif = ath12k_ahvif_to_vif(ahvif);
10536 	struct ath12k_link_vif *scan_arvif;
10537 	struct ath12k_hw *ah = hw->priv;
10538 	struct ath12k *ar;
10539 	struct ath12k_base *ab;
10540 	u8 link_id = arvif->link_id, scan_link_id;
10541 	unsigned long scan_link_map;
10542 	int ret;
10543 
10544 	lockdep_assert_wiphy(hw->wiphy);
10545 
10546 	if (ah->num_radio == 1)
10547 		ar = ah->radio;
10548 	else if (ctx)
10549 		ar = ath12k_get_ar_by_ctx(hw, ctx);
10550 	else
10551 		return NULL;
10552 
10553 	if (!ar)
10554 		return NULL;
10555 
10556 	/* cleanup the scan vdev if we are done scan on that ar
10557 	 * and now we want to create for actual usage.
10558 	 */
10559 	if (ieee80211_vif_is_mld(vif)) {
10560 		scan_link_map = ahvif->links_map & ATH12K_SCAN_LINKS_MASK;
10561 		for_each_set_bit(scan_link_id, &scan_link_map, ATH12K_NUM_MAX_LINKS) {
10562 			scan_arvif = wiphy_dereference(hw->wiphy,
10563 						       ahvif->link[scan_link_id]);
10564 			if (scan_arvif && scan_arvif->ar == ar) {
10565 				ar->scan.arvif = NULL;
10566 				ath12k_mac_remove_link_interface(hw, scan_arvif);
10567 				ath12k_mac_unassign_link_vif(scan_arvif);
10568 				break;
10569 			}
10570 		}
10571 	}
10572 
10573 	if (arvif->ar) {
10574 		/* This is not expected really */
10575 		if (WARN_ON(!arvif->is_created)) {
10576 			arvif->ar = NULL;
10577 			return NULL;
10578 		}
10579 
10580 		if (ah->num_radio == 1)
10581 			return arvif->ar;
10582 
10583 		/* This can happen as scan vdev gets created during multiple scans
10584 		 * across different radios before a vdev is brought up in
10585 		 * a certain radio.
10586 		 */
10587 		if (ar != arvif->ar) {
10588 			if (WARN_ON(arvif->is_started))
10589 				return NULL;
10590 
10591 			ath12k_mac_remove_link_interface(hw, arvif);
10592 			ath12k_mac_unassign_link_vif(arvif);
10593 		}
10594 	}
10595 
10596 	ab = ar->ab;
10597 
10598 	/* Assign arvif again here since previous radio switch block
10599 	 * would've unassigned and cleared it.
10600 	 */
10601 	arvif = ath12k_mac_assign_link_vif(ah, vif, link_id);
10602 	if (vif->type == NL80211_IFTYPE_AP &&
10603 	    ar->num_peers > (ar->max_num_peers - 1)) {
10604 		ath12k_warn(ab, "failed to create vdev due to insufficient peer entry resource in firmware\n");
10605 		goto unlock;
10606 	}
10607 
10608 	if (arvif->is_created)
10609 		goto flush;
10610 
10611 	ret = ath12k_mac_vdev_create(ar, arvif);
10612 	if (ret) {
10613 		ath12k_warn(ab, "failed to create vdev %pM ret %d", vif->addr, ret);
10614 		goto unlock;
10615 	}
10616 
10617 flush:
10618 	/* If the vdev is created during channel assign and not during
10619 	 * add_interface(), Apply any parameters for the vdev which were received
10620 	 * after add_interface, corresponding to this vif.
10621 	 */
10622 	ath12k_mac_vif_cache_flush(ar, arvif);
10623 unlock:
10624 	return arvif->ar;
10625 }
10626 
10627 int ath12k_mac_op_add_interface(struct ieee80211_hw *hw,
10628 				struct ieee80211_vif *vif)
10629 {
10630 	struct ath12k_hw *ah = ath12k_hw_to_ah(hw);
10631 	struct ath12k_vif *ahvif = ath12k_vif_to_ahvif(vif);
10632 	struct ath12k_reg_info *reg_info;
10633 	struct ath12k_link_vif *arvif;
10634 	struct ath12k_base *ab;
10635 	struct ath12k *ar;
10636 	int i;
10637 
10638 	lockdep_assert_wiphy(hw->wiphy);
10639 
10640 	memset(ahvif, 0, sizeof(*ahvif));
10641 
10642 	ahvif->ah = ah;
10643 	ahvif->vif = vif;
10644 	arvif = &ahvif->deflink;
10645 
10646 	ath12k_mac_init_arvif(ahvif, arvif, -1);
10647 
10648 	/* Allocate Default Queue now and reassign during actual vdev create */
10649 	vif->cab_queue = ATH12K_HW_DEFAULT_QUEUE;
10650 	for (i = 0; i < ARRAY_SIZE(vif->hw_queue); i++)
10651 		vif->hw_queue[i] = ATH12K_HW_DEFAULT_QUEUE;
10652 
10653 	vif->driver_flags |= IEEE80211_VIF_SUPPORTS_UAPSD;
10654 
10655 	ath12k_mac_determine_vdev_type(vif, ahvif);
10656 
10657 	for_each_ar(ah, ar, i) {
10658 		if (!ath12k_wmi_supports_6ghz_cc_ext(ar))
10659 			continue;
10660 
10661 		ab = ar->ab;
10662 		reg_info = ab->reg_info[ar->pdev_idx];
10663 		ath12k_dbg(ab, ATH12K_DBG_MAC, "interface added to change reg rules\n");
10664 		ah->regd_updated = false;
10665 		ath12k_reg_handle_chan_list(ab, reg_info, ahvif->vdev_type,
10666 					    IEEE80211_REG_UNSET_AP);
10667 		break;
10668 	}
10669 
10670 	/* Defer vdev creation until assign_chanctx or hw_scan is initiated as driver
10671 	 * will not know if this interface is an ML vif at this point.
10672 	 */
10673 	return 0;
10674 }
10675 EXPORT_SYMBOL(ath12k_mac_op_add_interface);
10676 
10677 static void ath12k_mac_vif_unref(struct ath12k_dp *dp, struct ieee80211_vif *vif)
10678 {
10679 	struct ath12k_tx_desc_info *tx_desc_info;
10680 	struct ath12k_skb_cb *skb_cb;
10681 	struct sk_buff *skb;
10682 	int i;
10683 
10684 	for (i = 0; i < ATH12K_HW_MAX_QUEUES; i++) {
10685 		spin_lock_bh(&dp->tx_desc_lock[i]);
10686 
10687 		list_for_each_entry(tx_desc_info, &dp->tx_desc_used_list[i],
10688 				    list) {
10689 			skb = tx_desc_info->skb;
10690 			if (!skb)
10691 				continue;
10692 
10693 			skb_cb = ATH12K_SKB_CB(skb);
10694 			if (skb_cb->vif == vif)
10695 				skb_cb->vif = NULL;
10696 		}
10697 
10698 		spin_unlock_bh(&dp->tx_desc_lock[i]);
10699 	}
10700 }
10701 
10702 static int ath12k_mac_vdev_delete(struct ath12k *ar, struct ath12k_link_vif *arvif)
10703 {
10704 	struct ath12k_vif *ahvif = arvif->ahvif;
10705 	struct ieee80211_vif *vif = ath12k_ahvif_to_vif(ahvif);
10706 	struct ath12k_dp_link_vif *dp_link_vif;
10707 	struct ath12k_base *ab = ar->ab;
10708 	unsigned long time_left;
10709 	int ret;
10710 
10711 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
10712 
10713 	reinit_completion(&ar->vdev_delete_done);
10714 
10715 	ret = ath12k_wmi_vdev_delete(ar, arvif->vdev_id);
10716 	if (ret) {
10717 		ath12k_warn(ab, "failed to delete WMI vdev %d: %d\n",
10718 			    arvif->vdev_id, ret);
10719 		goto err_vdev_del;
10720 	}
10721 
10722 	time_left = wait_for_completion_timeout(&ar->vdev_delete_done,
10723 						ATH12K_VDEV_DELETE_TIMEOUT_HZ);
10724 	if (time_left == 0) {
10725 		ath12k_warn(ab, "Timeout in receiving vdev delete response\n");
10726 		goto err_vdev_del;
10727 	}
10728 
10729 	ab->free_vdev_map |= 1LL << arvif->vdev_id;
10730 	ar->allocated_vdev_map &= ~(1LL << arvif->vdev_id);
10731 	ar->num_created_vdevs--;
10732 
10733 	if (ahvif->vdev_type == WMI_VDEV_TYPE_MONITOR) {
10734 		ar->monitor_vdev_id = -1;
10735 		ar->monitor_vdev_created = false;
10736 	}
10737 
10738 	ath12k_dbg(ab, ATH12K_DBG_MAC, "vdev %pM deleted, vdev_id %d\n",
10739 		   vif->addr, arvif->vdev_id);
10740 
10741 err_vdev_del:
10742 	spin_lock_bh(&ar->data_lock);
10743 	list_del(&arvif->list);
10744 	spin_unlock_bh(&ar->data_lock);
10745 
10746 	ath12k_peer_cleanup(ar, arvif->vdev_id);
10747 	ath12k_ahvif_put_link_cache(ahvif, arvif->link_id);
10748 
10749 	idr_for_each(&ar->txmgmt_idr,
10750 		     ath12k_mac_vif_txmgmt_idr_remove, vif);
10751 
10752 	ath12k_mac_vif_unref(ath12k_ab_to_dp(ab), vif);
10753 
10754 	dp_link_vif = ath12k_dp_vif_to_dp_link_vif(&ahvif->dp_vif, arvif->link_id);
10755 	ath12k_dp_tx_put_bank_profile(ath12k_ab_to_dp(ab), dp_link_vif->bank_id);
10756 
10757 	/* Recalc txpower for remaining vdev */
10758 	ath12k_mac_txpower_recalc(ar);
10759 
10760 	/* TODO: recal traffic pause state based on the available vdevs */
10761 	arvif->is_created = false;
10762 	arvif->ar = NULL;
10763 
10764 	return ret;
10765 }
10766 
10767 void ath12k_mac_op_remove_interface(struct ieee80211_hw *hw,
10768 				    struct ieee80211_vif *vif)
10769 {
10770 	struct ath12k_vif *ahvif = ath12k_vif_to_ahvif(vif);
10771 	struct ath12k_link_vif *arvif;
10772 	struct ath12k *ar;
10773 	u8 link_id;
10774 
10775 	lockdep_assert_wiphy(hw->wiphy);
10776 
10777 	for (link_id = 0; link_id < ATH12K_NUM_MAX_LINKS; link_id++) {
10778 		/* if we cached some config but never received assign chanctx,
10779 		 * free the allocated cache.
10780 		 */
10781 		ath12k_ahvif_put_link_cache(ahvif, link_id);
10782 		arvif = wiphy_dereference(hw->wiphy, ahvif->link[link_id]);
10783 		if (!arvif || !arvif->is_created)
10784 			continue;
10785 
10786 		ar = arvif->ar;
10787 
10788 		/* Scan abortion is in progress since before this, cancel_hw_scan()
10789 		 * is expected to be executed. Since link is anyways going to be removed
10790 		 * now, just cancel the worker and send the scan aborted to user space
10791 		 */
10792 		if (ar->scan.arvif == arvif) {
10793 			wiphy_work_cancel(hw->wiphy, &ar->scan.vdev_clean_wk);
10794 
10795 			spin_lock_bh(&ar->data_lock);
10796 			ar->scan.arvif = NULL;
10797 			if (!ar->scan.is_roc) {
10798 				struct cfg80211_scan_info info = {
10799 					.aborted = true,
10800 				};
10801 
10802 				ath12k_mac_scan_send_complete(ar, &info);
10803 			}
10804 
10805 			ar->scan.state = ATH12K_SCAN_IDLE;
10806 			ar->scan_channel = NULL;
10807 			ar->scan.roc_freq = 0;
10808 			spin_unlock_bh(&ar->data_lock);
10809 		}
10810 
10811 		ath12k_mac_remove_link_interface(hw, arvif);
10812 		ath12k_mac_unassign_link_vif(arvif);
10813 	}
10814 }
10815 EXPORT_SYMBOL(ath12k_mac_op_remove_interface);
10816 
10817 /* FIXME: Has to be verified. */
10818 #define SUPPORTED_FILTERS			\
10819 	(FIF_ALLMULTI |				\
10820 	FIF_CONTROL |				\
10821 	FIF_PSPOLL |				\
10822 	FIF_OTHER_BSS |				\
10823 	FIF_BCN_PRBRESP_PROMISC |		\
10824 	FIF_PROBE_REQ |				\
10825 	FIF_FCSFAIL)
10826 
10827 void ath12k_mac_op_configure_filter(struct ieee80211_hw *hw,
10828 				    unsigned int changed_flags,
10829 				    unsigned int *total_flags,
10830 				    u64 multicast)
10831 {
10832 	struct ath12k_hw *ah = ath12k_hw_to_ah(hw);
10833 	struct ath12k *ar;
10834 
10835 	lockdep_assert_wiphy(hw->wiphy);
10836 
10837 	ar = ath12k_ah_to_ar(ah, 0);
10838 
10839 	*total_flags &= SUPPORTED_FILTERS;
10840 	ar->filter_flags = *total_flags;
10841 }
10842 EXPORT_SYMBOL(ath12k_mac_op_configure_filter);
10843 
10844 int ath12k_mac_op_get_antenna(struct ieee80211_hw *hw, int radio_idx,
10845 			      u32 *tx_ant, u32 *rx_ant)
10846 {
10847 	struct ath12k_hw *ah = ath12k_hw_to_ah(hw);
10848 	int antennas_rx = 0, antennas_tx = 0;
10849 	struct ath12k *ar;
10850 	int i;
10851 
10852 	lockdep_assert_wiphy(hw->wiphy);
10853 
10854 	for_each_ar(ah, ar, i) {
10855 		antennas_rx = max_t(u32, antennas_rx, ar->cfg_rx_chainmask);
10856 		antennas_tx = max_t(u32, antennas_tx, ar->cfg_tx_chainmask);
10857 	}
10858 
10859 	*tx_ant = antennas_tx;
10860 	*rx_ant = antennas_rx;
10861 
10862 	return 0;
10863 }
10864 EXPORT_SYMBOL(ath12k_mac_op_get_antenna);
10865 
10866 int ath12k_mac_op_set_antenna(struct ieee80211_hw *hw, int radio_idx,
10867 			      u32 tx_ant, u32 rx_ant)
10868 {
10869 	struct ath12k_hw *ah = ath12k_hw_to_ah(hw);
10870 	struct ath12k *ar;
10871 	int ret = 0;
10872 	int i;
10873 
10874 	lockdep_assert_wiphy(hw->wiphy);
10875 
10876 	for_each_ar(ah, ar, i) {
10877 		ret = __ath12k_set_antenna(ar, tx_ant, rx_ant);
10878 		if (ret)
10879 			break;
10880 	}
10881 
10882 	return ret;
10883 }
10884 EXPORT_SYMBOL(ath12k_mac_op_set_antenna);
10885 
10886 static int ath12k_mac_ampdu_action(struct ieee80211_hw *hw,
10887 				   struct ieee80211_vif *vif,
10888 				   struct ieee80211_ampdu_params *params,
10889 				   u8 link_id)
10890 {
10891 	struct ath12k *ar;
10892 	int ret = -EINVAL;
10893 
10894 	lockdep_assert_wiphy(hw->wiphy);
10895 
10896 	ar = ath12k_get_ar_by_vif(hw, vif, link_id);
10897 	if (!ar)
10898 		return -EINVAL;
10899 
10900 	switch (params->action) {
10901 	case IEEE80211_AMPDU_RX_START:
10902 		ret = ath12k_dp_rx_ampdu_start(ar, params, link_id);
10903 		break;
10904 	case IEEE80211_AMPDU_RX_STOP:
10905 		ret = ath12k_dp_rx_ampdu_stop(ar, params, link_id);
10906 		break;
10907 	case IEEE80211_AMPDU_TX_START:
10908 	case IEEE80211_AMPDU_TX_STOP_CONT:
10909 	case IEEE80211_AMPDU_TX_STOP_FLUSH:
10910 	case IEEE80211_AMPDU_TX_STOP_FLUSH_CONT:
10911 	case IEEE80211_AMPDU_TX_OPERATIONAL:
10912 		/* Tx A-MPDU aggregation offloaded to hw/fw so deny mac80211
10913 		 * Tx aggregation requests.
10914 		 */
10915 		ret = -EOPNOTSUPP;
10916 		break;
10917 	}
10918 
10919 	if (ret)
10920 		ath12k_warn(ar->ab, "unable to perform ampdu action %d for vif %pM link %u ret %d\n",
10921 			    params->action, vif->addr, link_id, ret);
10922 
10923 	return ret;
10924 }
10925 
10926 int ath12k_mac_op_ampdu_action(struct ieee80211_hw *hw,
10927 			       struct ieee80211_vif *vif,
10928 			       struct ieee80211_ampdu_params *params)
10929 {
10930 	struct ieee80211_sta *sta = params->sta;
10931 	struct ath12k_sta *ahsta = ath12k_sta_to_ahsta(sta);
10932 	unsigned long links_map = ahsta->links_map;
10933 	int ret = -EINVAL;
10934 	u8 link_id;
10935 
10936 	lockdep_assert_wiphy(hw->wiphy);
10937 
10938 	if (WARN_ON(!links_map))
10939 		return ret;
10940 
10941 	for_each_set_bit(link_id, &links_map, IEEE80211_MLD_MAX_NUM_LINKS) {
10942 		ret = ath12k_mac_ampdu_action(hw, vif, params, link_id);
10943 		if (ret)
10944 			return ret;
10945 	}
10946 
10947 	return 0;
10948 }
10949 EXPORT_SYMBOL(ath12k_mac_op_ampdu_action);
10950 
10951 int ath12k_mac_op_add_chanctx(struct ieee80211_hw *hw,
10952 			      struct ieee80211_chanctx_conf *ctx)
10953 {
10954 	struct ath12k *ar;
10955 	struct ath12k_base *ab;
10956 
10957 	lockdep_assert_wiphy(hw->wiphy);
10958 
10959 	ar = ath12k_get_ar_by_ctx(hw, ctx);
10960 	if (!ar)
10961 		return -EINVAL;
10962 
10963 	ab = ar->ab;
10964 
10965 	ath12k_dbg(ab, ATH12K_DBG_MAC,
10966 		   "mac chanctx add freq %u width %d ptr %p\n",
10967 		   ctx->def.chan->center_freq, ctx->def.width, ctx);
10968 
10969 	spin_lock_bh(&ar->data_lock);
10970 	/* TODO: In case of multiple channel context, populate rx_channel from
10971 	 * Rx PPDU desc information.
10972 	 */
10973 	ar->rx_channel = ctx->def.chan;
10974 	spin_unlock_bh(&ar->data_lock);
10975 	ar->chan_tx_pwr = ATH12K_PDEV_TX_POWER_INVALID;
10976 
10977 	return 0;
10978 }
10979 EXPORT_SYMBOL(ath12k_mac_op_add_chanctx);
10980 
10981 void ath12k_mac_op_remove_chanctx(struct ieee80211_hw *hw,
10982 				  struct ieee80211_chanctx_conf *ctx)
10983 {
10984 	struct ath12k *ar;
10985 	struct ath12k_base *ab;
10986 
10987 	lockdep_assert_wiphy(hw->wiphy);
10988 
10989 	ar = ath12k_get_ar_by_ctx(hw, ctx);
10990 	if (!ar)
10991 		return;
10992 
10993 	ab = ar->ab;
10994 
10995 	ath12k_dbg(ab, ATH12K_DBG_MAC,
10996 		   "mac chanctx remove freq %u width %d ptr %p\n",
10997 		   ctx->def.chan->center_freq, ctx->def.width, ctx);
10998 
10999 	spin_lock_bh(&ar->data_lock);
11000 	/* TODO: In case of there is one more channel context left, populate
11001 	 * rx_channel with the channel of that remaining channel context.
11002 	 */
11003 	ar->rx_channel = NULL;
11004 	spin_unlock_bh(&ar->data_lock);
11005 	ar->chan_tx_pwr = ATH12K_PDEV_TX_POWER_INVALID;
11006 }
11007 EXPORT_SYMBOL(ath12k_mac_op_remove_chanctx);
11008 
11009 static enum wmi_phy_mode
11010 ath12k_mac_check_down_grade_phy_mode(struct ath12k *ar,
11011 				     enum wmi_phy_mode mode,
11012 				     enum nl80211_band band,
11013 				     enum nl80211_iftype type)
11014 {
11015 	struct ieee80211_sta_eht_cap *eht_cap = NULL;
11016 	enum wmi_phy_mode down_mode;
11017 	int n = ar->mac.sbands[band].n_iftype_data;
11018 	int i;
11019 	struct ieee80211_sband_iftype_data *data;
11020 
11021 	if (mode < MODE_11BE_EHT20)
11022 		return mode;
11023 
11024 	data = ar->mac.iftype[band];
11025 	for (i = 0; i < n; i++) {
11026 		if (data[i].types_mask & BIT(type)) {
11027 			eht_cap = &data[i].eht_cap;
11028 			break;
11029 		}
11030 	}
11031 
11032 	if (eht_cap && eht_cap->has_eht)
11033 		return mode;
11034 
11035 	switch (mode) {
11036 	case MODE_11BE_EHT20:
11037 		down_mode = MODE_11AX_HE20;
11038 		break;
11039 	case MODE_11BE_EHT40:
11040 		down_mode = MODE_11AX_HE40;
11041 		break;
11042 	case MODE_11BE_EHT80:
11043 		down_mode = MODE_11AX_HE80;
11044 		break;
11045 	case MODE_11BE_EHT80_80:
11046 		down_mode = MODE_11AX_HE80_80;
11047 		break;
11048 	case MODE_11BE_EHT160:
11049 	case MODE_11BE_EHT160_160:
11050 	case MODE_11BE_EHT320:
11051 		down_mode = MODE_11AX_HE160;
11052 		break;
11053 	case MODE_11BE_EHT20_2G:
11054 		down_mode = MODE_11AX_HE20_2G;
11055 		break;
11056 	case MODE_11BE_EHT40_2G:
11057 		down_mode = MODE_11AX_HE40_2G;
11058 		break;
11059 	default:
11060 		down_mode = mode;
11061 		break;
11062 	}
11063 
11064 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
11065 		   "mac vdev start phymode %s downgrade to %s\n",
11066 		   ath12k_mac_phymode_str(mode),
11067 		   ath12k_mac_phymode_str(down_mode));
11068 
11069 	return down_mode;
11070 }
11071 
11072 static void
11073 ath12k_mac_mlo_get_vdev_args(struct ath12k_link_vif *arvif,
11074 			     struct wmi_ml_arg *ml_arg)
11075 {
11076 	struct ath12k_vif *ahvif = arvif->ahvif;
11077 	struct wmi_ml_partner_info *partner_info;
11078 	struct ieee80211_bss_conf *link_conf;
11079 	struct ath12k_link_vif *arvif_p;
11080 	unsigned long links;
11081 	u8 link_id;
11082 
11083 	lockdep_assert_wiphy(ahvif->ah->hw->wiphy);
11084 
11085 	if (!ath12k_mac_is_ml_arvif(arvif))
11086 		return;
11087 
11088 	if (hweight16(ahvif->vif->valid_links) > ATH12K_WMI_MLO_MAX_LINKS)
11089 		return;
11090 
11091 	ml_arg->enabled = true;
11092 
11093 	/* Driver always add a new link via VDEV START, FW takes
11094 	 * care of internally adding this link to existing
11095 	 * link vdevs which are advertised as partners below
11096 	 */
11097 	ml_arg->link_add = true;
11098 
11099 	ml_arg->assoc_link = arvif->is_sta_assoc_link;
11100 
11101 	partner_info = ml_arg->partner_info;
11102 
11103 	links = ahvif->links_map;
11104 	for_each_set_bit(link_id, &links, IEEE80211_MLD_MAX_NUM_LINKS) {
11105 		arvif_p = wiphy_dereference(ahvif->ah->hw->wiphy, ahvif->link[link_id]);
11106 
11107 		if (WARN_ON(!arvif_p))
11108 			continue;
11109 
11110 		if (arvif == arvif_p)
11111 			continue;
11112 
11113 		if (!arvif_p->is_created)
11114 			continue;
11115 
11116 		link_conf = wiphy_dereference(ahvif->ah->hw->wiphy,
11117 					      ahvif->vif->link_conf[arvif_p->link_id]);
11118 
11119 		if (!link_conf)
11120 			continue;
11121 
11122 		partner_info->vdev_id = arvif_p->vdev_id;
11123 		partner_info->hw_link_id = arvif_p->ar->pdev->hw_link_id;
11124 		ether_addr_copy(partner_info->addr, link_conf->addr);
11125 		ml_arg->num_partner_links++;
11126 		partner_info++;
11127 	}
11128 }
11129 
11130 static int
11131 ath12k_mac_vdev_start_restart(struct ath12k_link_vif *arvif,
11132 			      struct ieee80211_chanctx_conf *ctx,
11133 			      bool restart)
11134 {
11135 	struct ath12k *ar = arvif->ar;
11136 	struct ath12k_base *ab = ar->ab;
11137 	struct wmi_vdev_start_req_arg arg = {};
11138 	const struct cfg80211_chan_def *chandef = &ctx->def;
11139 	struct ieee80211_hw *hw = ath12k_ar_to_hw(ar);
11140 	struct ath12k_vif *ahvif = arvif->ahvif;
11141 	struct ieee80211_bss_conf *link_conf;
11142 	unsigned int dfs_cac_time;
11143 	int ret;
11144 
11145 	lockdep_assert_wiphy(hw->wiphy);
11146 
11147 	link_conf = ath12k_mac_get_link_bss_conf(arvif);
11148 	if (!link_conf) {
11149 		ath12k_warn(ar->ab, "unable to access bss link conf in vdev start for vif %pM link %u\n",
11150 			    ahvif->vif->addr, arvif->link_id);
11151 		return -ENOLINK;
11152 	}
11153 
11154 	reinit_completion(&ar->vdev_setup_done);
11155 
11156 	arg.vdev_id = arvif->vdev_id;
11157 	arg.dtim_period = arvif->dtim_period;
11158 	arg.bcn_intval = arvif->beacon_interval;
11159 	arg.punct_bitmap = ~arvif->punct_bitmap;
11160 
11161 	arg.freq = chandef->chan->center_freq;
11162 	arg.band_center_freq1 = chandef->center_freq1;
11163 	arg.band_center_freq2 = chandef->center_freq2;
11164 	arg.mode = ath12k_phymodes[chandef->chan->band][chandef->width];
11165 
11166 	arg.mode = ath12k_mac_check_down_grade_phy_mode(ar, arg.mode,
11167 							chandef->chan->band,
11168 							ahvif->vif->type);
11169 	arg.min_power = 0;
11170 	arg.max_power = chandef->chan->max_power;
11171 	arg.max_reg_power = chandef->chan->max_reg_power;
11172 	arg.max_antenna_gain = chandef->chan->max_antenna_gain;
11173 
11174 	arg.pref_tx_streams = ar->num_tx_chains;
11175 	arg.pref_rx_streams = ar->num_rx_chains;
11176 
11177 	arg.mbssid_flags = WMI_VDEV_MBSSID_FLAGS_NON_MBSSID_AP;
11178 	arg.mbssid_tx_vdev_id = 0;
11179 	if (test_bit(WMI_TLV_SERVICE_MBSS_PARAM_IN_VDEV_START_SUPPORT,
11180 		     ar->ab->wmi_ab.svc_map)) {
11181 		ret = ath12k_mac_setup_vdev_params_mbssid(arvif,
11182 							  &arg.mbssid_flags,
11183 							  &arg.mbssid_tx_vdev_id);
11184 		if (ret)
11185 			return ret;
11186 	}
11187 
11188 	if (ahvif->vdev_type == WMI_VDEV_TYPE_AP) {
11189 		arg.ssid = ahvif->u.ap.ssid;
11190 		arg.ssid_len = ahvif->u.ap.ssid_len;
11191 		arg.hidden_ssid = ahvif->u.ap.hidden_ssid;
11192 
11193 		/* For now allow DFS for AP mode */
11194 		arg.chan_radar = !!(chandef->chan->flags & IEEE80211_CHAN_RADAR);
11195 
11196 		arg.freq2_radar = ctx->radar_enabled;
11197 
11198 		arg.passive = arg.chan_radar;
11199 
11200 		spin_lock_bh(&ab->base_lock);
11201 		arg.regdomain = ar->ab->dfs_region;
11202 		spin_unlock_bh(&ab->base_lock);
11203 
11204 		/* TODO: Notify if secondary 80Mhz also needs radar detection */
11205 	}
11206 
11207 	arg.passive |= !!(chandef->chan->flags & IEEE80211_CHAN_NO_IR);
11208 
11209 	if (!restart)
11210 		ath12k_mac_mlo_get_vdev_args(arvif, &arg.ml);
11211 
11212 	ath12k_dbg(ab, ATH12K_DBG_MAC,
11213 		   "mac vdev %d start center_freq %d phymode %s punct_bitmap 0x%x\n",
11214 		   arg.vdev_id, arg.freq,
11215 		   ath12k_mac_phymode_str(arg.mode), arg.punct_bitmap);
11216 
11217 	ret = ath12k_wmi_vdev_start(ar, &arg, restart);
11218 	if (ret) {
11219 		ath12k_warn(ar->ab, "failed to %s WMI vdev %i\n",
11220 			    restart ? "restart" : "start", arg.vdev_id);
11221 		return ret;
11222 	}
11223 
11224 	ret = ath12k_mac_vdev_setup_sync(ar);
11225 	if (ret) {
11226 		ath12k_warn(ab, "failed to synchronize setup for vdev %i %s: %d\n",
11227 			    arg.vdev_id, restart ? "restart" : "start", ret);
11228 		return ret;
11229 	}
11230 
11231 	/* TODO: For now we only set TPC power here. However when
11232 	 * channel changes, say CSA, it should be updated again.
11233 	 */
11234 	if (ath12k_mac_supports_tpc(ar, ahvif, chandef)) {
11235 		ath12k_mac_fill_reg_tpc_info(ar, arvif, ctx);
11236 		ath12k_wmi_send_vdev_set_tpc_power(ar, arvif->vdev_id,
11237 						   &arvif->reg_tpc_info);
11238 	}
11239 
11240 	ar->num_started_vdevs++;
11241 	ath12k_dbg(ab, ATH12K_DBG_MAC,  "vdev %pM started, vdev_id %d\n",
11242 		   ahvif->vif->addr, arvif->vdev_id);
11243 
11244 	/* Enable CAC Running Flag in the driver by checking all sub-channel's DFS
11245 	 * state as NL80211_DFS_USABLE which indicates CAC needs to be
11246 	 * done before channel usage. This flag is used to drop rx packets.
11247 	 * during CAC.
11248 	 */
11249 	/* TODO: Set the flag for other interface types as required */
11250 	if (arvif->ahvif->vdev_type == WMI_VDEV_TYPE_AP && ctx->radar_enabled &&
11251 	    cfg80211_chandef_dfs_usable(hw->wiphy, chandef)) {
11252 		set_bit(ATH12K_FLAG_CAC_RUNNING, &ar->dev_flags);
11253 		dfs_cac_time = cfg80211_chandef_dfs_cac_time(hw->wiphy, chandef);
11254 
11255 		ath12k_dbg(ab, ATH12K_DBG_MAC,
11256 			   "CAC started dfs_cac_time %u center_freq %d center_freq1 %d for vdev %d\n",
11257 			   dfs_cac_time, arg.freq, arg.band_center_freq1, arg.vdev_id);
11258 	}
11259 
11260 	ret = ath12k_mac_set_txbf_conf(arvif);
11261 	if (ret)
11262 		ath12k_warn(ab, "failed to set txbf conf for vdev %d: %d\n",
11263 			    arvif->vdev_id, ret);
11264 
11265 	return 0;
11266 }
11267 
11268 static int ath12k_mac_vdev_start(struct ath12k_link_vif *arvif,
11269 				 struct ieee80211_chanctx_conf *ctx)
11270 {
11271 	return ath12k_mac_vdev_start_restart(arvif, ctx, false);
11272 }
11273 
11274 static int ath12k_mac_vdev_restart(struct ath12k_link_vif *arvif,
11275 				   struct ieee80211_chanctx_conf *ctx)
11276 {
11277 	return ath12k_mac_vdev_start_restart(arvif, ctx, true);
11278 }
11279 
11280 struct ath12k_mac_change_chanctx_arg {
11281 	struct ieee80211_chanctx_conf *ctx;
11282 	struct ieee80211_vif_chanctx_switch *vifs;
11283 	int n_vifs;
11284 	int next_vif;
11285 	struct ath12k *ar;
11286 };
11287 
11288 static void
11289 ath12k_mac_change_chanctx_cnt_iter(void *data, u8 *mac,
11290 				   struct ieee80211_vif *vif)
11291 {
11292 	struct ath12k_vif *ahvif = ath12k_vif_to_ahvif(vif);
11293 	struct ath12k_mac_change_chanctx_arg *arg = data;
11294 	struct ieee80211_bss_conf *link_conf;
11295 	struct ath12k_link_vif *arvif;
11296 	unsigned long links_map;
11297 	u8 link_id;
11298 
11299 	lockdep_assert_wiphy(ahvif->ah->hw->wiphy);
11300 
11301 	links_map = ahvif->links_map;
11302 	for_each_set_bit(link_id, &links_map, IEEE80211_MLD_MAX_NUM_LINKS) {
11303 		arvif = wiphy_dereference(ahvif->ah->hw->wiphy, ahvif->link[link_id]);
11304 		if (WARN_ON(!arvif))
11305 			continue;
11306 
11307 		if (!arvif->is_created || arvif->ar != arg->ar)
11308 			continue;
11309 
11310 		link_conf = wiphy_dereference(ahvif->ah->hw->wiphy,
11311 					      vif->link_conf[link_id]);
11312 		if (WARN_ON(!link_conf))
11313 			continue;
11314 
11315 		if (rcu_access_pointer(link_conf->chanctx_conf) != arg->ctx)
11316 			continue;
11317 
11318 		arg->n_vifs++;
11319 	}
11320 }
11321 
11322 static void
11323 ath12k_mac_change_chanctx_fill_iter(void *data, u8 *mac,
11324 				    struct ieee80211_vif *vif)
11325 {
11326 	struct ath12k_vif *ahvif = ath12k_vif_to_ahvif(vif);
11327 	struct ath12k_mac_change_chanctx_arg *arg = data;
11328 	struct ieee80211_bss_conf *link_conf;
11329 	struct ieee80211_chanctx_conf *ctx;
11330 	struct ath12k_link_vif *arvif;
11331 	unsigned long links_map;
11332 	u8 link_id;
11333 
11334 	lockdep_assert_wiphy(ahvif->ah->hw->wiphy);
11335 
11336 	links_map = ahvif->links_map;
11337 	for_each_set_bit(link_id, &links_map, IEEE80211_MLD_MAX_NUM_LINKS) {
11338 		arvif = wiphy_dereference(ahvif->ah->hw->wiphy, ahvif->link[link_id]);
11339 		if (WARN_ON(!arvif))
11340 			continue;
11341 
11342 		if (!arvif->is_created || arvif->ar != arg->ar)
11343 			continue;
11344 
11345 		link_conf = wiphy_dereference(ahvif->ah->hw->wiphy,
11346 					      vif->link_conf[arvif->link_id]);
11347 		if (WARN_ON(!link_conf))
11348 			continue;
11349 
11350 		ctx = rcu_access_pointer(link_conf->chanctx_conf);
11351 		if (ctx != arg->ctx)
11352 			continue;
11353 
11354 		if (WARN_ON(arg->next_vif == arg->n_vifs))
11355 			return;
11356 
11357 		arg->vifs[arg->next_vif].vif = vif;
11358 		arg->vifs[arg->next_vif].old_ctx = ctx;
11359 		arg->vifs[arg->next_vif].new_ctx = ctx;
11360 		arg->vifs[arg->next_vif].link_conf = link_conf;
11361 		arg->next_vif++;
11362 	}
11363 }
11364 
11365 static u32 ath12k_mac_nlwidth_to_wmiwidth(enum nl80211_chan_width width)
11366 {
11367 	switch (width) {
11368 	case NL80211_CHAN_WIDTH_20:
11369 		return WMI_CHAN_WIDTH_20;
11370 	case NL80211_CHAN_WIDTH_40:
11371 		return WMI_CHAN_WIDTH_40;
11372 	case NL80211_CHAN_WIDTH_80:
11373 		return WMI_CHAN_WIDTH_80;
11374 	case NL80211_CHAN_WIDTH_160:
11375 		return WMI_CHAN_WIDTH_160;
11376 	case NL80211_CHAN_WIDTH_80P80:
11377 		return WMI_CHAN_WIDTH_80P80;
11378 	case NL80211_CHAN_WIDTH_5:
11379 		return WMI_CHAN_WIDTH_5;
11380 	case NL80211_CHAN_WIDTH_10:
11381 		return WMI_CHAN_WIDTH_10;
11382 	case NL80211_CHAN_WIDTH_320:
11383 		return WMI_CHAN_WIDTH_320;
11384 	default:
11385 		WARN_ON(1);
11386 		return WMI_CHAN_WIDTH_20;
11387 	}
11388 }
11389 
11390 static int ath12k_mac_update_peer_puncturing_width(struct ath12k *ar,
11391 						   struct ath12k_link_vif *arvif,
11392 						   struct cfg80211_chan_def def)
11393 {
11394 	u32 param_id, param_value;
11395 	int ret;
11396 
11397 	if (arvif->ahvif->vdev_type != WMI_VDEV_TYPE_STA)
11398 		return 0;
11399 
11400 	param_id = WMI_PEER_CHWIDTH_PUNCTURE_20MHZ_BITMAP;
11401 	param_value = ath12k_mac_nlwidth_to_wmiwidth(def.width) |
11402 		u32_encode_bits((~def.punctured),
11403 				WMI_PEER_PUNCTURE_BITMAP);
11404 
11405 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
11406 		   "punctured bitmap %02x width %d vdev %d\n",
11407 		   def.punctured, def.width, arvif->vdev_id);
11408 
11409 	ret = ath12k_wmi_set_peer_param(ar, arvif->bssid,
11410 					arvif->vdev_id, param_id,
11411 					param_value);
11412 
11413 	return ret;
11414 }
11415 
11416 static void
11417 ath12k_mac_update_vif_chan(struct ath12k *ar,
11418 			   struct ieee80211_vif_chanctx_switch *vifs,
11419 			   int n_vifs)
11420 {
11421 	struct ath12k_wmi_vdev_up_params params = {};
11422 	struct ieee80211_bss_conf *link_conf;
11423 	struct ath12k_base *ab = ar->ab;
11424 	struct ath12k_link_vif *arvif;
11425 	struct ieee80211_vif *vif;
11426 	struct ath12k_vif *ahvif;
11427 	u8 link_id;
11428 	int ret;
11429 	int i;
11430 	bool monitor_vif = false;
11431 
11432 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
11433 
11434 	for (i = 0; i < n_vifs; i++) {
11435 		vif = vifs[i].vif;
11436 		ahvif = ath12k_vif_to_ahvif(vif);
11437 		link_conf = vifs[i].link_conf;
11438 		link_id = link_conf->link_id;
11439 		arvif = wiphy_dereference(ath12k_ar_to_hw(ar)->wiphy,
11440 					  ahvif->link[link_id]);
11441 
11442 		if (vif->type == NL80211_IFTYPE_MONITOR) {
11443 			monitor_vif = true;
11444 			continue;
11445 		}
11446 
11447 		ath12k_dbg(ab, ATH12K_DBG_MAC,
11448 			   "mac chanctx switch vdev_id %i freq %u->%u width %d->%d\n",
11449 			   arvif->vdev_id,
11450 			   vifs[i].old_ctx->def.chan->center_freq,
11451 			   vifs[i].new_ctx->def.chan->center_freq,
11452 			   vifs[i].old_ctx->def.width,
11453 			   vifs[i].new_ctx->def.width);
11454 
11455 		if (WARN_ON(!arvif->is_started))
11456 			continue;
11457 
11458 		arvif->punct_bitmap = vifs[i].new_ctx->def.punctured;
11459 
11460 		/* Firmware expect vdev_restart only if vdev is up.
11461 		 * If vdev is down then it expect vdev_stop->vdev_start.
11462 		 */
11463 		if (arvif->is_up) {
11464 			ret = ath12k_mac_vdev_restart(arvif, vifs[i].new_ctx);
11465 			if (ret) {
11466 				ath12k_warn(ab, "failed to restart vdev %d: %d\n",
11467 					    arvif->vdev_id, ret);
11468 				continue;
11469 			}
11470 		} else {
11471 			ret = ath12k_mac_vdev_stop(arvif);
11472 			if (ret) {
11473 				ath12k_warn(ab, "failed to stop vdev %d: %d\n",
11474 					    arvif->vdev_id, ret);
11475 				continue;
11476 			}
11477 
11478 			ret = ath12k_mac_vdev_start(arvif, vifs[i].new_ctx);
11479 			if (ret)
11480 				ath12k_warn(ab, "failed to start vdev %d: %d\n",
11481 					    arvif->vdev_id, ret);
11482 			continue;
11483 		}
11484 
11485 		ret = ath12k_mac_update_peer_puncturing_width(arvif->ar, arvif,
11486 							      vifs[i].new_ctx->def);
11487 		if (ret) {
11488 			ath12k_warn(ar->ab,
11489 				    "failed to update puncturing bitmap %02x and width %d: %d\n",
11490 				    vifs[i].new_ctx->def.punctured,
11491 				    vifs[i].new_ctx->def.width, ret);
11492 			continue;
11493 		}
11494 
11495 		/* Defer VDEV bring-up during CSA to avoid installing stale
11496 		 * beacon templates. The beacon content is updated only
11497 		 * after CSA finalize, so we mark CSA in progress and skip
11498 		 * VDEV_UP for now. It will be handled later in
11499 		 * bss_info_changed().
11500 		 */
11501 		if (link_conf->csa_active &&
11502 		    arvif->ahvif->vdev_type == WMI_VDEV_TYPE_AP) {
11503 			arvif->is_csa_in_progress = true;
11504 			continue;
11505 		}
11506 
11507 		ret = ath12k_mac_setup_bcn_tmpl(arvif);
11508 		if (ret)
11509 			ath12k_warn(ab, "failed to update bcn tmpl during csa: %d\n",
11510 				    ret);
11511 
11512 		memset(&params, 0, sizeof(params));
11513 		params.vdev_id = arvif->vdev_id;
11514 		params.aid = ahvif->aid;
11515 		params.bssid = arvif->bssid;
11516 		params.tx_bssid = ath12k_mac_get_tx_bssid(arvif);
11517 		if (params.tx_bssid) {
11518 			params.nontx_profile_idx = link_conf->bssid_index;
11519 			params.nontx_profile_cnt = 1 << link_conf->bssid_indicator;
11520 		}
11521 		ret = ath12k_wmi_vdev_up(arvif->ar, &params);
11522 		if (ret) {
11523 			ath12k_warn(ab, "failed to bring vdev up %d: %d\n",
11524 				    arvif->vdev_id, ret);
11525 			continue;
11526 		}
11527 	}
11528 
11529 	/* Restart the internal monitor vdev on new channel */
11530 	if (!monitor_vif && ar->monitor_vdev_created) {
11531 		if (!ath12k_mac_monitor_stop(ar))
11532 			ath12k_mac_monitor_start(ar);
11533 	}
11534 }
11535 
11536 static void
11537 ath12k_mac_update_active_vif_chan(struct ath12k *ar,
11538 				  struct ieee80211_chanctx_conf *ctx)
11539 {
11540 	struct ath12k_mac_change_chanctx_arg arg = { .ctx = ctx, .ar = ar };
11541 	struct ieee80211_hw *hw = ath12k_ar_to_hw(ar);
11542 
11543 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
11544 
11545 	ieee80211_iterate_active_interfaces_atomic(hw,
11546 						   IEEE80211_IFACE_ITER_NORMAL,
11547 						   ath12k_mac_change_chanctx_cnt_iter,
11548 						   &arg);
11549 	if (arg.n_vifs == 0)
11550 		return;
11551 
11552 	arg.vifs = kcalloc(arg.n_vifs, sizeof(arg.vifs[0]), GFP_KERNEL);
11553 	if (!arg.vifs)
11554 		return;
11555 
11556 	ieee80211_iterate_active_interfaces_atomic(hw,
11557 						   IEEE80211_IFACE_ITER_NORMAL,
11558 						   ath12k_mac_change_chanctx_fill_iter,
11559 						   &arg);
11560 
11561 	ath12k_mac_update_vif_chan(ar, arg.vifs, arg.n_vifs);
11562 
11563 	kfree(arg.vifs);
11564 }
11565 
11566 void ath12k_mac_op_change_chanctx(struct ieee80211_hw *hw,
11567 				  struct ieee80211_chanctx_conf *ctx,
11568 				  u32 changed)
11569 {
11570 	struct ath12k *ar;
11571 	struct ath12k_base *ab;
11572 
11573 	lockdep_assert_wiphy(hw->wiphy);
11574 
11575 	ar = ath12k_get_ar_by_ctx(hw, ctx);
11576 	if (!ar)
11577 		return;
11578 
11579 	ab = ar->ab;
11580 
11581 	ath12k_dbg(ab, ATH12K_DBG_MAC,
11582 		   "mac chanctx change freq %u width %d ptr %p changed %x\n",
11583 		   ctx->def.chan->center_freq, ctx->def.width, ctx, changed);
11584 
11585 	/* This shouldn't really happen because channel switching should use
11586 	 * switch_vif_chanctx().
11587 	 */
11588 	if (WARN_ON(changed & IEEE80211_CHANCTX_CHANGE_CHANNEL))
11589 		return;
11590 
11591 	if (changed & IEEE80211_CHANCTX_CHANGE_WIDTH ||
11592 	    changed & IEEE80211_CHANCTX_CHANGE_RADAR ||
11593 	    changed & IEEE80211_CHANCTX_CHANGE_PUNCTURING)
11594 		ath12k_mac_update_active_vif_chan(ar, ctx);
11595 
11596 	/* TODO: Recalc radar detection */
11597 }
11598 EXPORT_SYMBOL(ath12k_mac_op_change_chanctx);
11599 
11600 static int ath12k_start_vdev_delay(struct ath12k *ar,
11601 				   struct ath12k_link_vif *arvif)
11602 {
11603 	struct ath12k_base *ab = ar->ab;
11604 	struct ath12k_vif *ahvif = arvif->ahvif;
11605 	struct ieee80211_vif *vif = ath12k_ahvif_to_vif(arvif->ahvif);
11606 	struct ieee80211_chanctx_conf *chanctx;
11607 	struct ieee80211_bss_conf *link_conf;
11608 	int ret;
11609 
11610 	if (WARN_ON(arvif->is_started))
11611 		return -EBUSY;
11612 
11613 	link_conf = ath12k_mac_get_link_bss_conf(arvif);
11614 	if (!link_conf) {
11615 		ath12k_warn(ab, "failed to get link conf for vdev %u\n", arvif->vdev_id);
11616 		return -EINVAL;
11617 	}
11618 
11619 	chanctx	= wiphy_dereference(ath12k_ar_to_hw(arvif->ar)->wiphy,
11620 				    link_conf->chanctx_conf);
11621 	ret = ath12k_mac_vdev_start(arvif, chanctx);
11622 	if (ret) {
11623 		ath12k_warn(ab, "failed to start vdev %i addr %pM on freq %d: %d\n",
11624 			    arvif->vdev_id, vif->addr,
11625 			    chanctx->def.chan->center_freq, ret);
11626 		return ret;
11627 	}
11628 
11629 	if (ahvif->vdev_type == WMI_VDEV_TYPE_MONITOR) {
11630 		ret = ath12k_monitor_vdev_up(ar, arvif->vdev_id);
11631 		if (ret) {
11632 			ath12k_warn(ab, "failed put monitor up: %d\n", ret);
11633 			return ret;
11634 		}
11635 	}
11636 
11637 	arvif->is_started = true;
11638 
11639 	/* TODO: Setup ps and cts/rts protection */
11640 	return 0;
11641 }
11642 
11643 static u8 ath12k_mac_get_num_pwr_levels(struct cfg80211_chan_def *chan_def)
11644 {
11645 	if (chan_def->chan->flags & IEEE80211_CHAN_PSD) {
11646 		switch (chan_def->width) {
11647 		case NL80211_CHAN_WIDTH_20:
11648 			return 1;
11649 		case NL80211_CHAN_WIDTH_40:
11650 			return 2;
11651 		case NL80211_CHAN_WIDTH_80:
11652 			return 4;
11653 		case NL80211_CHAN_WIDTH_160:
11654 			return 8;
11655 		case NL80211_CHAN_WIDTH_320:
11656 			return 16;
11657 		default:
11658 			return 1;
11659 		}
11660 	} else {
11661 		switch (chan_def->width) {
11662 		case NL80211_CHAN_WIDTH_20:
11663 			return 1;
11664 		case NL80211_CHAN_WIDTH_40:
11665 			return 2;
11666 		case NL80211_CHAN_WIDTH_80:
11667 			return 3;
11668 		case NL80211_CHAN_WIDTH_160:
11669 			return 4;
11670 		case NL80211_CHAN_WIDTH_320:
11671 			return 5;
11672 		default:
11673 			return 1;
11674 		}
11675 	}
11676 }
11677 
11678 static u16 ath12k_mac_get_6ghz_start_frequency(struct cfg80211_chan_def *chan_def)
11679 {
11680 	u16 diff_seq;
11681 
11682 	/* It is to get the lowest channel number's center frequency of the chan.
11683 	 * For example,
11684 	 * bandwidth=40 MHz, center frequency is 5965, lowest channel is 1
11685 	 * with center frequency 5955, its diff is 5965 - 5955 = 10.
11686 	 * bandwidth=80 MHz, center frequency is 5985, lowest channel is 1
11687 	 * with center frequency 5955, its diff is 5985 - 5955 = 30.
11688 	 * bandwidth=160 MHz, center frequency is 6025, lowest channel is 1
11689 	 * with center frequency 5955, its diff is 6025 - 5955 = 70.
11690 	 * bandwidth=320 MHz, center frequency is 6105, lowest channel is 1
11691 	 * with center frequency 5955, its diff is 6105 - 5955 = 70.
11692 	 */
11693 	switch (chan_def->width) {
11694 	case NL80211_CHAN_WIDTH_320:
11695 		diff_seq = 150;
11696 		break;
11697 	case NL80211_CHAN_WIDTH_160:
11698 		diff_seq = 70;
11699 		break;
11700 	case NL80211_CHAN_WIDTH_80:
11701 		diff_seq = 30;
11702 		break;
11703 	case NL80211_CHAN_WIDTH_40:
11704 		diff_seq = 10;
11705 		break;
11706 	default:
11707 		diff_seq = 0;
11708 	}
11709 
11710 	return chan_def->center_freq1 - diff_seq;
11711 }
11712 
11713 static u16 ath12k_mac_get_seg_freq(struct cfg80211_chan_def *chan_def,
11714 				   u16 start_seq, u8 seq)
11715 {
11716 	u16 seg_seq;
11717 
11718 	/* It is to get the center frequency of the specific bandwidth.
11719 	 * start_seq means the lowest channel number's center frequency.
11720 	 * seq 0/1/2/3 means 20 MHz/40 MHz/80 MHz/160 MHz.
11721 	 * For example,
11722 	 * lowest channel is 1, its center frequency 5955,
11723 	 * center frequency is 5955 when bandwidth=20 MHz, its diff is 5955 - 5955 = 0.
11724 	 * lowest channel is 1, its center frequency 5955,
11725 	 * center frequency is 5965 when bandwidth=40 MHz, its diff is 5965 - 5955 = 10.
11726 	 * lowest channel is 1, its center frequency 5955,
11727 	 * center frequency is 5985 when bandwidth=80 MHz, its diff is 5985 - 5955 = 30.
11728 	 * lowest channel is 1, its center frequency 5955,
11729 	 * center frequency is 6025 when bandwidth=160 MHz, its diff is 6025 - 5955 = 70.
11730 	 */
11731 	seg_seq = 10 * (BIT(seq) - 1);
11732 	return seg_seq + start_seq;
11733 }
11734 
11735 static void ath12k_mac_get_psd_channel(struct ath12k *ar,
11736 				       u16 step_freq,
11737 				       u16 *start_freq,
11738 				       u16 *center_freq,
11739 				       u8 i,
11740 				       struct ieee80211_channel **temp_chan,
11741 				       s8 *tx_power)
11742 {
11743 	/* It is to get the center frequency for each 20 MHz.
11744 	 * For example, if the chan is 160 MHz and center frequency is 6025,
11745 	 * then it include 8 channels, they are 1/5/9/13/17/21/25/29,
11746 	 * channel number 1's center frequency is 5955, it is parameter start_freq.
11747 	 * parameter i is the step of the 8 channels. i is 0~7 for the 8 channels.
11748 	 * the channel 1/5/9/13/17/21/25/29 maps i=0/1/2/3/4/5/6/7,
11749 	 * and maps its center frequency is 5955/5975/5995/6015/6035/6055/6075/6095,
11750 	 * the gap is 20 for each channel, parameter step_freq means the gap.
11751 	 * after get the center frequency of each channel, it is easy to find the
11752 	 * struct ieee80211_channel of it and get the max_reg_power.
11753 	 */
11754 	*center_freq = *start_freq + i * step_freq;
11755 	*temp_chan = ieee80211_get_channel(ar->ah->hw->wiphy, *center_freq);
11756 	*tx_power = (*temp_chan)->max_reg_power;
11757 }
11758 
11759 static void ath12k_mac_get_eirp_power(struct ath12k *ar,
11760 				      u16 *start_freq,
11761 				      u16 *center_freq,
11762 				      u8 i,
11763 				      struct ieee80211_channel **temp_chan,
11764 				      struct cfg80211_chan_def *def,
11765 				      s8 *tx_power)
11766 {
11767 	/* It is to get the center frequency for 20 MHz/40 MHz/80 MHz/
11768 	 * 160 MHz bandwidth, and then plus 10 to the center frequency,
11769 	 * it is the center frequency of a channel number.
11770 	 * For example, when configured channel number is 1.
11771 	 * center frequency is 5965 when bandwidth=40 MHz, after plus 10, it is 5975,
11772 	 * then it is channel number 5.
11773 	 * center frequency is 5985 when bandwidth=80 MHz, after plus 10, it is 5995,
11774 	 * then it is channel number 9.
11775 	 * center frequency is 6025 when bandwidth=160 MHz, after plus 10, it is 6035,
11776 	 * then it is channel number 17.
11777 	 * after get the center frequency of each channel, it is easy to find the
11778 	 * struct ieee80211_channel of it and get the max_reg_power.
11779 	 */
11780 	*center_freq = ath12k_mac_get_seg_freq(def, *start_freq, i);
11781 
11782 	/* For the 20 MHz, its center frequency is same with same channel */
11783 	if (i != 0)
11784 		*center_freq += 10;
11785 
11786 	*temp_chan = ieee80211_get_channel(ar->ah->hw->wiphy, *center_freq);
11787 	*tx_power = (*temp_chan)->max_reg_power;
11788 }
11789 
11790 void ath12k_mac_fill_reg_tpc_info(struct ath12k *ar,
11791 				  struct ath12k_link_vif *arvif,
11792 				  struct ieee80211_chanctx_conf *ctx)
11793 {
11794 	struct ath12k_base *ab = ar->ab;
11795 	struct ath12k_reg_tpc_power_info *reg_tpc_info = &arvif->reg_tpc_info;
11796 	struct ieee80211_bss_conf *bss_conf = ath12k_mac_get_link_bss_conf(arvif);
11797 	struct ieee80211_channel *chan, *temp_chan;
11798 	u8 pwr_lvl_idx, num_pwr_levels, pwr_reduction;
11799 	bool is_psd_power = false, is_tpe_present = false;
11800 	s8 max_tx_power[ATH12K_NUM_PWR_LEVELS], psd_power, tx_power;
11801 	s8 eirp_power = 0;
11802 	struct ath12k_vif *ahvif = arvif->ahvif;
11803 	u16 start_freq, center_freq;
11804 	u8 reg_6ghz_power_mode;
11805 
11806 	chan = ctx->def.chan;
11807 	start_freq = ath12k_mac_get_6ghz_start_frequency(&ctx->def);
11808 	pwr_reduction = bss_conf->pwr_reduction;
11809 
11810 	if (arvif->reg_tpc_info.num_pwr_levels) {
11811 		is_tpe_present = true;
11812 		num_pwr_levels = arvif->reg_tpc_info.num_pwr_levels;
11813 	} else {
11814 		num_pwr_levels = ath12k_mac_get_num_pwr_levels(&ctx->def);
11815 	}
11816 
11817 	for (pwr_lvl_idx = 0; pwr_lvl_idx < num_pwr_levels; pwr_lvl_idx++) {
11818 		/* STA received TPE IE*/
11819 		if (is_tpe_present) {
11820 			/* local power is PSD power*/
11821 			if (chan->flags & IEEE80211_CHAN_PSD) {
11822 				/* Connecting AP is psd power */
11823 				if (reg_tpc_info->is_psd_power) {
11824 					is_psd_power = true;
11825 					ath12k_mac_get_psd_channel(ar, 20,
11826 								   &start_freq,
11827 								   &center_freq,
11828 								   pwr_lvl_idx,
11829 								   &temp_chan,
11830 								   &tx_power);
11831 					psd_power = temp_chan->psd;
11832 					eirp_power = tx_power;
11833 					max_tx_power[pwr_lvl_idx] =
11834 						min_t(s8,
11835 						      psd_power,
11836 						      reg_tpc_info->tpe[pwr_lvl_idx]);
11837 				/* Connecting AP is not psd power */
11838 				} else {
11839 					ath12k_mac_get_eirp_power(ar,
11840 								  &start_freq,
11841 								  &center_freq,
11842 								  pwr_lvl_idx,
11843 								  &temp_chan,
11844 								  &ctx->def,
11845 								  &tx_power);
11846 					psd_power = temp_chan->psd;
11847 					/* convert psd power to EIRP power based
11848 					 * on channel width
11849 					 */
11850 					tx_power =
11851 						min_t(s8, tx_power,
11852 						      psd_power + 13 + pwr_lvl_idx * 3);
11853 					max_tx_power[pwr_lvl_idx] =
11854 						min_t(s8,
11855 						      tx_power,
11856 						      reg_tpc_info->tpe[pwr_lvl_idx]);
11857 				}
11858 			/* local power is not PSD power */
11859 			} else {
11860 				/* Connecting AP is psd power */
11861 				if (reg_tpc_info->is_psd_power) {
11862 					is_psd_power = true;
11863 					ath12k_mac_get_psd_channel(ar, 20,
11864 								   &start_freq,
11865 								   &center_freq,
11866 								   pwr_lvl_idx,
11867 								   &temp_chan,
11868 								   &tx_power);
11869 					eirp_power = tx_power;
11870 					max_tx_power[pwr_lvl_idx] =
11871 						reg_tpc_info->tpe[pwr_lvl_idx];
11872 				/* Connecting AP is not psd power */
11873 				} else {
11874 					ath12k_mac_get_eirp_power(ar,
11875 								  &start_freq,
11876 								  &center_freq,
11877 								  pwr_lvl_idx,
11878 								  &temp_chan,
11879 								  &ctx->def,
11880 								  &tx_power);
11881 					max_tx_power[pwr_lvl_idx] =
11882 						min_t(s8,
11883 						      tx_power,
11884 						      reg_tpc_info->tpe[pwr_lvl_idx]);
11885 				}
11886 			}
11887 		/* STA not received TPE IE */
11888 		} else {
11889 			/* local power is PSD power*/
11890 			if (chan->flags & IEEE80211_CHAN_PSD) {
11891 				is_psd_power = true;
11892 				ath12k_mac_get_psd_channel(ar, 20,
11893 							   &start_freq,
11894 							   &center_freq,
11895 							   pwr_lvl_idx,
11896 							   &temp_chan,
11897 							   &tx_power);
11898 				psd_power = temp_chan->psd;
11899 				eirp_power = tx_power;
11900 				max_tx_power[pwr_lvl_idx] = psd_power;
11901 			} else {
11902 				ath12k_mac_get_eirp_power(ar,
11903 							  &start_freq,
11904 							  &center_freq,
11905 							  pwr_lvl_idx,
11906 							  &temp_chan,
11907 							  &ctx->def,
11908 							  &tx_power);
11909 				max_tx_power[pwr_lvl_idx] = tx_power;
11910 			}
11911 		}
11912 
11913 		if (is_psd_power) {
11914 			/* If AP local power constraint is present */
11915 			if (pwr_reduction)
11916 				eirp_power = eirp_power - pwr_reduction;
11917 
11918 			/* If firmware updated max tx power is non zero, then take
11919 			 * the min of firmware updated ap tx power
11920 			 * and max power derived from above mentioned parameters.
11921 			 */
11922 			ath12k_dbg(ab, ATH12K_DBG_MAC,
11923 				   "eirp power : %d firmware report power : %d\n",
11924 				   eirp_power, ar->max_allowed_tx_power);
11925 			/* Firmware reports lower max_allowed_tx_power during vdev
11926 			 * start response. In case of 6 GHz, firmware is not aware
11927 			 * of EIRP power unless driver sets EIRP power through WMI
11928 			 * TPC command. So radio which does not support idle power
11929 			 * save can set maximum calculated EIRP power directly to
11930 			 * firmware through TPC command without min comparison with
11931 			 * vdev start response's max_allowed_tx_power.
11932 			 */
11933 			if (ar->max_allowed_tx_power && ab->hw_params->idle_ps)
11934 				eirp_power = min_t(s8,
11935 						   eirp_power,
11936 						   ar->max_allowed_tx_power);
11937 		} else {
11938 			/* If AP local power constraint is present */
11939 			if (pwr_reduction)
11940 				max_tx_power[pwr_lvl_idx] =
11941 					max_tx_power[pwr_lvl_idx] - pwr_reduction;
11942 			/* If firmware updated max tx power is non zero, then take
11943 			 * the min of firmware updated ap tx power
11944 			 * and max power derived from above mentioned parameters.
11945 			 */
11946 			if (ar->max_allowed_tx_power && ab->hw_params->idle_ps)
11947 				max_tx_power[pwr_lvl_idx] =
11948 					min_t(s8,
11949 					      max_tx_power[pwr_lvl_idx],
11950 					      ar->max_allowed_tx_power);
11951 		}
11952 		reg_tpc_info->chan_power_info[pwr_lvl_idx].chan_cfreq = center_freq;
11953 		reg_tpc_info->chan_power_info[pwr_lvl_idx].tx_power =
11954 			max_tx_power[pwr_lvl_idx];
11955 	}
11956 
11957 	reg_tpc_info->num_pwr_levels = num_pwr_levels;
11958 	reg_tpc_info->is_psd_power = is_psd_power;
11959 	reg_tpc_info->eirp_power = eirp_power;
11960 	if (ahvif->vdev_type == WMI_VDEV_TYPE_STA)
11961 		reg_6ghz_power_mode = bss_conf->power_type;
11962 	else
11963 		/* For now, LPI is the only supported AP power mode */
11964 		reg_6ghz_power_mode = IEEE80211_REG_LPI_AP;
11965 
11966 	reg_tpc_info->ap_power_type =
11967 		ath12k_reg_ap_pwr_convert(reg_6ghz_power_mode);
11968 }
11969 
11970 static void ath12k_mac_parse_tx_pwr_env(struct ath12k *ar,
11971 					struct ath12k_link_vif *arvif)
11972 {
11973 	struct ieee80211_bss_conf *bss_conf = ath12k_mac_get_link_bss_conf(arvif);
11974 	struct ath12k_reg_tpc_power_info *tpc_info = &arvif->reg_tpc_info;
11975 	struct ieee80211_parsed_tpe_eirp *local_non_psd, *reg_non_psd;
11976 	struct ieee80211_parsed_tpe_psd *local_psd, *reg_psd;
11977 	struct ieee80211_parsed_tpe *tpe = &bss_conf->tpe;
11978 	enum wmi_reg_6g_client_type client_type;
11979 	struct ath12k_reg_info *reg_info;
11980 	struct ath12k_base *ab = ar->ab;
11981 	bool psd_valid, non_psd_valid;
11982 	int i;
11983 
11984 	reg_info = ab->reg_info[ar->pdev_idx];
11985 	client_type = reg_info->client_type;
11986 
11987 	local_psd = &tpe->psd_local[client_type];
11988 	reg_psd = &tpe->psd_reg_client[client_type];
11989 	local_non_psd = &tpe->max_local[client_type];
11990 	reg_non_psd = &tpe->max_reg_client[client_type];
11991 
11992 	psd_valid = local_psd->valid | reg_psd->valid;
11993 	non_psd_valid = local_non_psd->valid | reg_non_psd->valid;
11994 
11995 	if (!psd_valid && !non_psd_valid) {
11996 		ath12k_warn(ab,
11997 			    "no transmit power envelope match client power type %d\n",
11998 			    client_type);
11999 		return;
12000 	}
12001 
12002 	if (psd_valid) {
12003 		tpc_info->is_psd_power = true;
12004 
12005 		tpc_info->num_pwr_levels = max(local_psd->count,
12006 					       reg_psd->count);
12007 		tpc_info->num_pwr_levels =
12008 				min3(tpc_info->num_pwr_levels,
12009 				     IEEE80211_TPE_PSD_ENTRIES_320MHZ,
12010 				     ATH12K_NUM_PWR_LEVELS);
12011 
12012 		for (i = 0; i < tpc_info->num_pwr_levels; i++) {
12013 			tpc_info->tpe[i] = min(local_psd->power[i],
12014 					       reg_psd->power[i]) / 2;
12015 			ath12k_dbg(ab, ATH12K_DBG_MAC,
12016 				   "TPE PSD power[%d] : %d\n",
12017 				   i, tpc_info->tpe[i]);
12018 		}
12019 	} else {
12020 		tpc_info->is_psd_power = false;
12021 		tpc_info->eirp_power = 0;
12022 
12023 		tpc_info->num_pwr_levels = max(local_non_psd->count,
12024 					       reg_non_psd->count);
12025 		tpc_info->num_pwr_levels =
12026 				min3(tpc_info->num_pwr_levels,
12027 				     IEEE80211_TPE_EIRP_ENTRIES_320MHZ,
12028 				     ATH12K_NUM_PWR_LEVELS);
12029 
12030 		for (i = 0; i < tpc_info->num_pwr_levels; i++) {
12031 			tpc_info->tpe[i] = min(local_non_psd->power[i],
12032 					       reg_non_psd->power[i]) / 2;
12033 			ath12k_dbg(ab, ATH12K_DBG_MAC,
12034 				   "non PSD power[%d] : %d\n",
12035 				   i, tpc_info->tpe[i]);
12036 		}
12037 	}
12038 }
12039 
12040 int
12041 ath12k_mac_op_assign_vif_chanctx(struct ieee80211_hw *hw,
12042 				 struct ieee80211_vif *vif,
12043 				 struct ieee80211_bss_conf *link_conf,
12044 				 struct ieee80211_chanctx_conf *ctx)
12045 {
12046 	struct ath12k_hw *ah = ath12k_hw_to_ah(hw);
12047 	struct ath12k *ar;
12048 	struct ath12k_base *ab;
12049 	struct ath12k_vif *ahvif = ath12k_vif_to_ahvif(vif);
12050 	u8 link_id = link_conf->link_id;
12051 	struct ath12k_link_vif *arvif;
12052 	int ret;
12053 
12054 	lockdep_assert_wiphy(hw->wiphy);
12055 
12056 	/* For multi radio wiphy, the vdev was not created during add_interface
12057 	 * create now since we have a channel ctx now to assign to a specific ar/fw
12058 	 */
12059 	arvif = ath12k_mac_assign_link_vif(ah, vif, link_id);
12060 	if (!arvif) {
12061 		WARN_ON(1);
12062 		return -ENOMEM;
12063 	}
12064 
12065 	ar = ath12k_mac_assign_vif_to_vdev(hw, arvif, ctx);
12066 	if (!ar) {
12067 		ath12k_hw_warn(ah, "failed to assign chanctx for vif %pM link id %u link vif is already started",
12068 			       vif->addr, link_id);
12069 		return -EINVAL;
12070 	}
12071 
12072 	ab = ar->ab;
12073 
12074 	ath12k_dbg(ab, ATH12K_DBG_MAC,
12075 		   "mac chanctx assign ptr %p vdev_id %i\n",
12076 		   ctx, arvif->vdev_id);
12077 
12078 	if (ath12k_wmi_supports_6ghz_cc_ext(ar) &&
12079 	    ctx->def.chan->band == NL80211_BAND_6GHZ &&
12080 	    ahvif->vdev_type == WMI_VDEV_TYPE_STA)
12081 		ath12k_mac_parse_tx_pwr_env(ar, arvif);
12082 
12083 	arvif->punct_bitmap = ctx->def.punctured;
12084 
12085 	/* for some targets bss peer must be created before vdev_start */
12086 	if (ab->hw_params->vdev_start_delay &&
12087 	    ahvif->vdev_type != WMI_VDEV_TYPE_AP &&
12088 	    ahvif->vdev_type != WMI_VDEV_TYPE_MONITOR &&
12089 	    !ath12k_dp_link_peer_exist_by_vdev_id(ath12k_ab_to_dp(ab), arvif->vdev_id)) {
12090 		ret = 0;
12091 		goto out;
12092 	}
12093 
12094 	if (WARN_ON(arvif->is_started)) {
12095 		ret = -EBUSY;
12096 		goto out;
12097 	}
12098 
12099 	if (ahvif->vdev_type == WMI_VDEV_TYPE_MONITOR) {
12100 		ret = ath12k_mac_monitor_start(ar);
12101 		if (ret) {
12102 			ath12k_mac_monitor_vdev_delete(ar);
12103 			goto out;
12104 		}
12105 
12106 		arvif->is_started = true;
12107 		goto out;
12108 	}
12109 
12110 	ret = ath12k_mac_vdev_start(arvif, ctx);
12111 	if (ret) {
12112 		ath12k_warn(ab, "failed to start vdev %i addr %pM on freq %d: %d\n",
12113 			    arvif->vdev_id, vif->addr,
12114 			    ctx->def.chan->center_freq, ret);
12115 		goto out;
12116 	}
12117 
12118 	arvif->is_started = true;
12119 
12120 	/* TODO: Setup ps and cts/rts protection */
12121 
12122 out:
12123 	return ret;
12124 }
12125 EXPORT_SYMBOL(ath12k_mac_op_assign_vif_chanctx);
12126 
12127 void
12128 ath12k_mac_op_unassign_vif_chanctx(struct ieee80211_hw *hw,
12129 				   struct ieee80211_vif *vif,
12130 				   struct ieee80211_bss_conf *link_conf,
12131 				   struct ieee80211_chanctx_conf *ctx)
12132 {
12133 	struct ath12k *ar;
12134 	struct ath12k_base *ab;
12135 	struct ath12k_vif *ahvif = ath12k_vif_to_ahvif(vif);
12136 	struct ath12k_link_vif *arvif;
12137 	u8 link_id = link_conf->link_id;
12138 	int ret;
12139 
12140 	lockdep_assert_wiphy(hw->wiphy);
12141 
12142 	arvif = wiphy_dereference(hw->wiphy, ahvif->link[link_id]);
12143 
12144 	/* The vif is expected to be attached to an ar's VDEV.
12145 	 * We leave the vif/vdev in this function as is
12146 	 * and not delete the vdev symmetric to assign_vif_chanctx()
12147 	 * the VDEV will be deleted and unassigned either during
12148 	 * remove_interface() or when there is a change in channel
12149 	 * that moves the vif to a new ar
12150 	 */
12151 	if (!arvif || !arvif->is_created)
12152 		return;
12153 
12154 	ar = arvif->ar;
12155 	ab = ar->ab;
12156 
12157 	ath12k_dbg(ab, ATH12K_DBG_MAC,
12158 		   "mac chanctx unassign ptr %p vdev_id %i\n",
12159 		   ctx, arvif->vdev_id);
12160 
12161 	WARN_ON(!arvif->is_started);
12162 
12163 	if (ahvif->vdev_type == WMI_VDEV_TYPE_MONITOR) {
12164 		ret = ath12k_mac_monitor_stop(ar);
12165 		if (ret)
12166 			return;
12167 
12168 		arvif->is_started = false;
12169 	}
12170 
12171 	if (ahvif->vdev_type != WMI_VDEV_TYPE_STA &&
12172 	    ahvif->vdev_type != WMI_VDEV_TYPE_MONITOR) {
12173 		ath12k_bss_disassoc(ar, arvif);
12174 		ret = ath12k_mac_vdev_stop(arvif);
12175 		if (ret)
12176 			ath12k_warn(ab, "failed to stop vdev %i: %d\n",
12177 				    arvif->vdev_id, ret);
12178 	}
12179 	arvif->is_started = false;
12180 
12181 	if (test_bit(WMI_TLV_SERVICE_11D_OFFLOAD, ab->wmi_ab.svc_map) &&
12182 	    ahvif->vdev_type == WMI_VDEV_TYPE_STA &&
12183 	    ahvif->vdev_subtype == WMI_VDEV_SUBTYPE_NONE &&
12184 	    ar->state_11d != ATH12K_11D_PREPARING) {
12185 		reinit_completion(&ar->completed_11d_scan);
12186 		ar->state_11d = ATH12K_11D_PREPARING;
12187 	}
12188 
12189 	if (ar->scan.arvif == arvif && ar->scan.state == ATH12K_SCAN_RUNNING) {
12190 		ath12k_scan_abort(ar);
12191 		ar->scan.arvif = NULL;
12192 	}
12193 }
12194 EXPORT_SYMBOL(ath12k_mac_op_unassign_vif_chanctx);
12195 
12196 int
12197 ath12k_mac_op_switch_vif_chanctx(struct ieee80211_hw *hw,
12198 				 struct ieee80211_vif_chanctx_switch *vifs,
12199 				 int n_vifs,
12200 				 enum ieee80211_chanctx_switch_mode mode)
12201 {
12202 	struct ath12k *ar;
12203 
12204 	lockdep_assert_wiphy(hw->wiphy);
12205 
12206 	ar = ath12k_get_ar_by_ctx(hw, vifs->old_ctx);
12207 	if (!ar)
12208 		return -EINVAL;
12209 
12210 	/* Switching channels across radio is not allowed */
12211 	if (ar != ath12k_get_ar_by_ctx(hw, vifs->new_ctx))
12212 		return -EINVAL;
12213 
12214 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
12215 		   "mac chanctx switch n_vifs %d mode %d\n",
12216 		   n_vifs, mode);
12217 	ath12k_mac_update_vif_chan(ar, vifs, n_vifs);
12218 
12219 	return 0;
12220 }
12221 EXPORT_SYMBOL(ath12k_mac_op_switch_vif_chanctx);
12222 
12223 static int
12224 ath12k_set_vdev_param_to_all_vifs(struct ath12k *ar, int param, u32 value)
12225 {
12226 	struct ath12k_link_vif *arvif;
12227 	int ret = 0;
12228 
12229 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
12230 
12231 	list_for_each_entry(arvif, &ar->arvifs, list) {
12232 		ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "setting mac vdev %d param %d value %d\n",
12233 			   param, arvif->vdev_id, value);
12234 
12235 		ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
12236 						    param, value);
12237 		if (ret) {
12238 			ath12k_warn(ar->ab, "failed to set param %d for vdev %d: %d\n",
12239 				    param, arvif->vdev_id, ret);
12240 			break;
12241 		}
12242 	}
12243 
12244 	return ret;
12245 }
12246 
12247 /* mac80211 stores device specific RTS/Fragmentation threshold value,
12248  * this is set interface specific to firmware from ath12k driver
12249  */
12250 int ath12k_mac_op_set_rts_threshold(struct ieee80211_hw *hw,
12251 				    int radio_idx, u32 value)
12252 {
12253 	struct ath12k_hw *ah = ath12k_hw_to_ah(hw);
12254 	struct wiphy *wiphy = hw->wiphy;
12255 	struct ath12k *ar;
12256 	int param_id = WMI_VDEV_PARAM_RTS_THRESHOLD;
12257 	int ret = 0, ret_err, i;
12258 
12259 	lockdep_assert_wiphy(hw->wiphy);
12260 
12261 	if (radio_idx >= wiphy->n_radio || radio_idx < -1)
12262 		return -EINVAL;
12263 
12264 	if (radio_idx != -1) {
12265 		/* Update RTS threshold in specified radio */
12266 		ar = ath12k_ah_to_ar(ah, radio_idx);
12267 		ret = ath12k_set_vdev_param_to_all_vifs(ar, param_id, value);
12268 		if (ret) {
12269 			ath12k_warn(ar->ab,
12270 				    "failed to set RTS config for all vdevs of pdev %d",
12271 				    ar->pdev->pdev_id);
12272 			return ret;
12273 		}
12274 
12275 		ar->rts_threshold = value;
12276 		return 0;
12277 	}
12278 
12279 	/* Radio_index passed is -1, so set RTS threshold for all radios. */
12280 	for_each_ar(ah, ar, i) {
12281 		ret = ath12k_set_vdev_param_to_all_vifs(ar, param_id, value);
12282 		if (ret) {
12283 			ath12k_warn(ar->ab, "failed to set RTS config for all vdevs of pdev %d",
12284 				    ar->pdev->pdev_id);
12285 			break;
12286 		}
12287 	}
12288 	if (!ret) {
12289 		/* Setting new RTS threshold for vdevs of all radios passed, so update
12290 		 * the RTS threshold value for all radios
12291 		 */
12292 		for_each_ar(ah, ar, i)
12293 			ar->rts_threshold = value;
12294 		return 0;
12295 	}
12296 
12297 	/* RTS threshold config failed, revert to the previous RTS threshold */
12298 	for (i = i - 1; i >= 0; i--) {
12299 		ar = ath12k_ah_to_ar(ah, i);
12300 		ret_err = ath12k_set_vdev_param_to_all_vifs(ar, param_id,
12301 							    ar->rts_threshold);
12302 		if (ret_err)
12303 			ath12k_warn(ar->ab,
12304 				    "failed to restore RTS threshold for all vdevs of pdev %d",
12305 				    ar->pdev->pdev_id);
12306 	}
12307 
12308 	return ret;
12309 }
12310 EXPORT_SYMBOL(ath12k_mac_op_set_rts_threshold);
12311 
12312 int ath12k_mac_op_set_frag_threshold(struct ieee80211_hw *hw,
12313 				     int radio_idx, u32 value)
12314 {
12315 	/* Even though there's a WMI vdev param for fragmentation threshold no
12316 	 * known firmware actually implements it. Moreover it is not possible to
12317 	 * rely frame fragmentation to mac80211 because firmware clears the
12318 	 * "more fragments" bit in frame control making it impossible for remote
12319 	 * devices to reassemble frames.
12320 	 *
12321 	 * Hence implement a dummy callback just to say fragmentation isn't
12322 	 * supported. This effectively prevents mac80211 from doing frame
12323 	 * fragmentation in software.
12324 	 */
12325 
12326 	lockdep_assert_wiphy(hw->wiphy);
12327 
12328 	return -EOPNOTSUPP;
12329 }
12330 EXPORT_SYMBOL(ath12k_mac_op_set_frag_threshold);
12331 
12332 static int ath12k_mac_flush(struct ath12k *ar)
12333 {
12334 	long time_left;
12335 	int ret = 0;
12336 
12337 	time_left = wait_event_timeout(ar->dp.tx_empty_waitq,
12338 				       (atomic_read(&ar->dp.num_tx_pending) == 0),
12339 				       ATH12K_FLUSH_TIMEOUT);
12340 	if (time_left == 0) {
12341 		ath12k_warn(ar->ab,
12342 			    "failed to flush transmit queue, data pkts pending %d\n",
12343 			    atomic_read(&ar->dp.num_tx_pending));
12344 		ret = -ETIMEDOUT;
12345 	}
12346 
12347 	time_left = wait_event_timeout(ar->txmgmt_empty_waitq,
12348 				       (atomic_read(&ar->num_pending_mgmt_tx) == 0),
12349 				       ATH12K_FLUSH_TIMEOUT);
12350 	if (time_left == 0) {
12351 		ath12k_warn(ar->ab,
12352 			    "failed to flush mgmt transmit queue, mgmt pkts pending %d\n",
12353 			    atomic_read(&ar->num_pending_mgmt_tx));
12354 		ret = -ETIMEDOUT;
12355 	}
12356 
12357 	return ret;
12358 }
12359 
12360 int ath12k_mac_wait_tx_complete(struct ath12k *ar)
12361 {
12362 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
12363 
12364 	ath12k_mac_drain_tx(ar);
12365 	return ath12k_mac_flush(ar);
12366 }
12367 
12368 void ath12k_mac_op_flush(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
12369 			 u32 queues, bool drop)
12370 {
12371 	struct ath12k_hw *ah = ath12k_hw_to_ah(hw);
12372 	struct ath12k_link_vif *arvif;
12373 	struct ath12k_vif *ahvif;
12374 	unsigned long links;
12375 	struct ath12k *ar;
12376 	u8 link_id;
12377 	int i;
12378 
12379 	lockdep_assert_wiphy(hw->wiphy);
12380 
12381 	if (drop)
12382 		return;
12383 
12384 	for_each_ar(ah, ar, i)
12385 		wiphy_work_flush(hw->wiphy, &ar->wmi_mgmt_tx_work);
12386 
12387 	/* vif can be NULL when flush() is considered for hw */
12388 	if (!vif) {
12389 		for_each_ar(ah, ar, i)
12390 			ath12k_mac_flush(ar);
12391 		return;
12392 	}
12393 
12394 	ahvif = ath12k_vif_to_ahvif(vif);
12395 	links = ahvif->links_map;
12396 	for_each_set_bit(link_id, &links, IEEE80211_MLD_MAX_NUM_LINKS) {
12397 		arvif = wiphy_dereference(hw->wiphy, ahvif->link[link_id]);
12398 		if (!(arvif && arvif->ar))
12399 			continue;
12400 
12401 		ath12k_mac_flush(arvif->ar);
12402 	}
12403 }
12404 EXPORT_SYMBOL(ath12k_mac_op_flush);
12405 
12406 static int
12407 ath12k_mac_bitrate_mask_num_ht_rates(struct ath12k *ar,
12408 				     enum nl80211_band band,
12409 				     const struct cfg80211_bitrate_mask *mask)
12410 {
12411 	int num_rates = 0;
12412 	int i;
12413 
12414 	for (i = 0; i < ARRAY_SIZE(mask->control[band].ht_mcs); i++)
12415 		num_rates += hweight16(mask->control[band].ht_mcs[i]);
12416 
12417 	return num_rates;
12418 }
12419 
12420 static bool
12421 ath12k_mac_has_single_legacy_rate(struct ath12k *ar,
12422 				  enum nl80211_band band,
12423 				  const struct cfg80211_bitrate_mask *mask)
12424 {
12425 	int num_rates = 0;
12426 
12427 	num_rates = hweight32(mask->control[band].legacy);
12428 
12429 	if (ath12k_mac_bitrate_mask_num_ht_rates(ar, band, mask))
12430 		return false;
12431 
12432 	if (ath12k_mac_bitrate_mask_num_vht_rates(ar, band, mask))
12433 		return false;
12434 
12435 	if (ath12k_mac_bitrate_mask_num_he_rates(ar, band, mask))
12436 		return false;
12437 
12438 	if (ath12k_mac_bitrate_mask_num_eht_rates(ar, band, mask))
12439 		return false;
12440 
12441 	return num_rates == 1;
12442 }
12443 
12444 static __le16
12445 ath12k_mac_get_tx_mcs_map(const struct ieee80211_sta_he_cap *he_cap)
12446 {
12447 	if (he_cap->he_cap_elem.phy_cap_info[0] &
12448 	    IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_160MHZ_IN_5G)
12449 		return he_cap->he_mcs_nss_supp.tx_mcs_160;
12450 
12451 	return he_cap->he_mcs_nss_supp.tx_mcs_80;
12452 }
12453 
12454 static bool
12455 ath12k_mac_bitrate_mask_get_single_nss(struct ath12k *ar,
12456 				       struct ieee80211_vif *vif,
12457 				       enum nl80211_band band,
12458 				       const struct cfg80211_bitrate_mask *mask,
12459 				       int *nss)
12460 {
12461 	struct ieee80211_supported_band *sband = &ar->mac.sbands[band];
12462 	u16 vht_mcs_map = le16_to_cpu(sband->vht_cap.vht_mcs.tx_mcs_map);
12463 	const struct ieee80211_sband_iftype_data *data;
12464 	const struct ieee80211_sta_he_cap *he_cap;
12465 	u16 he_mcs_map = 0;
12466 	u16 eht_mcs_map = 0;
12467 	u8 ht_nss_mask = 0;
12468 	u8 vht_nss_mask = 0;
12469 	u8 he_nss_mask = 0;
12470 	u8 eht_nss_mask = 0;
12471 	u8 mcs_nss_len;
12472 	int i;
12473 
12474 	/* No need to consider legacy here. Basic rates are always present
12475 	 * in bitrate mask
12476 	 */
12477 
12478 	for (i = 0; i < ARRAY_SIZE(mask->control[band].ht_mcs); i++) {
12479 		if (mask->control[band].ht_mcs[i] == 0)
12480 			continue;
12481 		else if (mask->control[band].ht_mcs[i] ==
12482 			 sband->ht_cap.mcs.rx_mask[i])
12483 			ht_nss_mask |= BIT(i);
12484 		else
12485 			return false;
12486 	}
12487 
12488 	for (i = 0; i < ARRAY_SIZE(mask->control[band].vht_mcs); i++) {
12489 		if (mask->control[band].vht_mcs[i] == 0)
12490 			continue;
12491 		else if (mask->control[band].vht_mcs[i] ==
12492 			 ath12k_mac_get_max_vht_mcs_map(vht_mcs_map, i))
12493 			vht_nss_mask |= BIT(i);
12494 		else
12495 			return false;
12496 	}
12497 
12498 	he_cap = ieee80211_get_he_iftype_cap_vif(sband, vif);
12499 	if (!he_cap)
12500 		return false;
12501 
12502 	he_mcs_map = le16_to_cpu(ath12k_mac_get_tx_mcs_map(he_cap));
12503 
12504 	for (i = 0; i < ARRAY_SIZE(mask->control[band].he_mcs); i++) {
12505 		if (mask->control[band].he_mcs[i] == 0)
12506 			continue;
12507 
12508 		if (mask->control[band].he_mcs[i] ==
12509 		    ath12k_mac_get_max_he_mcs_map(he_mcs_map, i))
12510 			he_nss_mask |= BIT(i);
12511 		else
12512 			return false;
12513 	}
12514 
12515 	data = ieee80211_get_sband_iftype_data(sband, vif->type);
12516 
12517 	mcs_nss_len = ieee80211_eht_mcs_nss_size(&data->he_cap.he_cap_elem,
12518 						 &data->eht_cap.eht_cap_elem,
12519 						 false);
12520 	if (mcs_nss_len == 4) {
12521 		/* 20 MHz only STA case */
12522 		const struct ieee80211_eht_mcs_nss_supp_20mhz_only *eht_mcs_nss =
12523 			&data->eht_cap.eht_mcs_nss_supp.only_20mhz;
12524 		if (eht_mcs_nss->rx_tx_mcs13_max_nss)
12525 			eht_mcs_map = 0x1fff;
12526 		else if (eht_mcs_nss->rx_tx_mcs11_max_nss)
12527 			eht_mcs_map = 0x07ff;
12528 		else if (eht_mcs_nss->rx_tx_mcs9_max_nss)
12529 			eht_mcs_map = 0x01ff;
12530 		else
12531 			eht_mcs_map = 0x007f;
12532 	} else {
12533 		const struct ieee80211_eht_mcs_nss_supp_bw *eht_mcs_nss;
12534 
12535 		switch (mcs_nss_len) {
12536 		case 9:
12537 			eht_mcs_nss = &data->eht_cap.eht_mcs_nss_supp.bw._320;
12538 			break;
12539 		case 6:
12540 			eht_mcs_nss = &data->eht_cap.eht_mcs_nss_supp.bw._160;
12541 			break;
12542 		case 3:
12543 			eht_mcs_nss = &data->eht_cap.eht_mcs_nss_supp.bw._80;
12544 			break;
12545 		default:
12546 			return false;
12547 		}
12548 
12549 		if (eht_mcs_nss->rx_tx_mcs13_max_nss)
12550 			eht_mcs_map = 0x1fff;
12551 		else if (eht_mcs_nss->rx_tx_mcs11_max_nss)
12552 			eht_mcs_map = 0x7ff;
12553 		else
12554 			eht_mcs_map = 0x1ff;
12555 	}
12556 
12557 	for (i = 0; i < ARRAY_SIZE(mask->control[band].eht_mcs); i++) {
12558 		if (mask->control[band].eht_mcs[i] == 0)
12559 			continue;
12560 
12561 		if (mask->control[band].eht_mcs[i] < eht_mcs_map)
12562 			eht_nss_mask |= BIT(i);
12563 		else
12564 			return false;
12565 	}
12566 
12567 	if (ht_nss_mask != vht_nss_mask || ht_nss_mask != he_nss_mask ||
12568 	    ht_nss_mask != eht_nss_mask)
12569 		return false;
12570 
12571 	if (ht_nss_mask == 0)
12572 		return false;
12573 
12574 	if (BIT(fls(ht_nss_mask)) - 1 != ht_nss_mask)
12575 		return false;
12576 
12577 	*nss = fls(ht_nss_mask);
12578 
12579 	return true;
12580 }
12581 
12582 static int
12583 ath12k_mac_get_single_legacy_rate(struct ath12k *ar,
12584 				  enum nl80211_band band,
12585 				  const struct cfg80211_bitrate_mask *mask,
12586 				  u32 *rate, u8 *nss)
12587 {
12588 	int rate_idx;
12589 	u16 bitrate;
12590 	u8 preamble;
12591 	u8 hw_rate;
12592 
12593 	if (hweight32(mask->control[band].legacy) != 1)
12594 		return -EINVAL;
12595 
12596 	rate_idx = ffs(mask->control[band].legacy) - 1;
12597 
12598 	if (band == NL80211_BAND_5GHZ || band == NL80211_BAND_6GHZ)
12599 		rate_idx += ATH12K_MAC_FIRST_OFDM_RATE_IDX;
12600 
12601 	hw_rate = ath12k_legacy_rates[rate_idx].hw_value;
12602 	bitrate = ath12k_legacy_rates[rate_idx].bitrate;
12603 
12604 	if (ath12k_mac_bitrate_is_cck(bitrate))
12605 		preamble = WMI_RATE_PREAMBLE_CCK;
12606 	else
12607 		preamble = WMI_RATE_PREAMBLE_OFDM;
12608 
12609 	*nss = 1;
12610 	*rate = ATH12K_HW_RATE_CODE(hw_rate, 0, preamble);
12611 
12612 	return 0;
12613 }
12614 
12615 static int
12616 ath12k_mac_set_fixed_rate_gi_ltf(struct ath12k_link_vif *arvif, u8 gi, u8 ltf,
12617 				 u32 param)
12618 {
12619 	struct ath12k *ar = arvif->ar;
12620 	int ret;
12621 
12622 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
12623 
12624 	/* 0.8 = 0, 1.6 = 2 and 3.2 = 3. */
12625 	if (gi && gi != 0xFF)
12626 		gi += 1;
12627 
12628 	ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
12629 					    WMI_VDEV_PARAM_SGI, gi);
12630 	if (ret) {
12631 		ath12k_warn(ar->ab, "failed to set GI:%d, error:%d\n",
12632 			    gi, ret);
12633 		return ret;
12634 	}
12635 
12636 	if (param == WMI_VDEV_PARAM_HE_LTF) {
12637 		/* HE values start from 1 */
12638 		if (ltf != 0xFF)
12639 			ltf += 1;
12640 	} else {
12641 		/* EHT values start from 5 */
12642 		if (ltf != 0xFF)
12643 			ltf += 4;
12644 	}
12645 
12646 	ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
12647 					    param, ltf);
12648 	if (ret) {
12649 		ath12k_warn(ar->ab, "failed to set LTF:%d, error:%d\n",
12650 			    ltf, ret);
12651 		return ret;
12652 	}
12653 	return 0;
12654 }
12655 
12656 static int
12657 ath12k_mac_set_auto_rate_gi_ltf(struct ath12k_link_vif *arvif, u16 gi, u8 ltf)
12658 {
12659 	struct ath12k *ar = arvif->ar;
12660 	int ret;
12661 	u32 ar_gi_ltf;
12662 
12663 	if (gi != 0xFF) {
12664 		switch (gi) {
12665 		case ATH12K_RATE_INFO_GI_0_8:
12666 			gi = WMI_AUTORATE_800NS_GI;
12667 			break;
12668 		case ATH12K_RATE_INFO_GI_1_6:
12669 			gi = WMI_AUTORATE_1600NS_GI;
12670 			break;
12671 		case ATH12K_RATE_INFO_GI_3_2:
12672 			gi = WMI_AUTORATE_3200NS_GI;
12673 			break;
12674 		default:
12675 			ath12k_warn(ar->ab, "Invalid GI\n");
12676 			return -EINVAL;
12677 		}
12678 	}
12679 
12680 	if (ltf != 0xFF) {
12681 		switch (ltf) {
12682 		case ATH12K_RATE_INFO_1XLTF:
12683 			ltf = WMI_AUTORATE_LTF_1X;
12684 			break;
12685 		case ATH12K_RATE_INFO_2XLTF:
12686 			ltf = WMI_AUTORATE_LTF_2X;
12687 			break;
12688 		case ATH12K_RATE_INFO_4XLTF:
12689 			ltf = WMI_AUTORATE_LTF_4X;
12690 			break;
12691 		default:
12692 			ath12k_warn(ar->ab, "Invalid LTF\n");
12693 			return -EINVAL;
12694 		}
12695 	}
12696 
12697 	ar_gi_ltf = gi | ltf;
12698 
12699 	ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
12700 					    WMI_VDEV_PARAM_AUTORATE_MISC_CFG,
12701 					    ar_gi_ltf);
12702 	if (ret) {
12703 		ath12k_warn(ar->ab,
12704 			    "failed to set autorate GI:%u, LTF:%u params, error:%d\n",
12705 			    gi, ltf, ret);
12706 		return ret;
12707 	}
12708 
12709 	return 0;
12710 }
12711 
12712 static u32 ath12k_mac_nlgi_to_wmigi(enum nl80211_txrate_gi gi)
12713 {
12714 	switch (gi) {
12715 	case NL80211_TXRATE_DEFAULT_GI:
12716 		return WMI_GI_400_NS;
12717 	case NL80211_TXRATE_FORCE_LGI:
12718 		return WMI_GI_800_NS;
12719 	default:
12720 		return WMI_GI_400_NS;
12721 	}
12722 }
12723 
12724 static int ath12k_mac_set_rate_params(struct ath12k_link_vif *arvif,
12725 				      u32 rate, u8 nss, u8 sgi, u8 ldpc,
12726 				      u8 he_gi, u8 he_ltf, bool he_fixed_rate,
12727 				      u8 eht_gi, u8 eht_ltf,
12728 				      bool eht_fixed_rate)
12729 {
12730 	struct ieee80211_bss_conf *link_conf;
12731 	struct ath12k *ar = arvif->ar;
12732 	bool he_support, eht_support, gi_ltf_set = false;
12733 	u32 vdev_param;
12734 	u32 param_value;
12735 	int ret;
12736 
12737 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
12738 
12739 	link_conf = ath12k_mac_get_link_bss_conf(arvif);
12740 	if (!link_conf)
12741 		return -EINVAL;
12742 
12743 	he_support = link_conf->he_support;
12744 	eht_support = link_conf->eht_support;
12745 
12746 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
12747 		   "mac set rate params vdev %i rate 0x%02x nss 0x%02x sgi 0x%02x ldpc 0x%02x\n",
12748 		   arvif->vdev_id, rate, nss, sgi, ldpc);
12749 
12750 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
12751 		   "he_gi 0x%02x he_ltf 0x%02x he_fixed_rate %d\n", he_gi,
12752 		   he_ltf, he_fixed_rate);
12753 
12754 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
12755 		   "eht_gi 0x%02x eht_ltf 0x%02x eht_fixed_rate %d\n",
12756 		   eht_gi, eht_ltf, eht_fixed_rate);
12757 
12758 	if (!he_support && !eht_support) {
12759 		vdev_param = WMI_VDEV_PARAM_FIXED_RATE;
12760 		ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
12761 						    vdev_param, rate);
12762 		if (ret) {
12763 			ath12k_warn(ar->ab, "failed to set fixed rate param 0x%02x: %d\n",
12764 				    rate, ret);
12765 			return ret;
12766 		}
12767 	}
12768 
12769 	vdev_param = WMI_VDEV_PARAM_NSS;
12770 
12771 	ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
12772 					    vdev_param, nss);
12773 	if (ret) {
12774 		ath12k_warn(ar->ab, "failed to set nss param %d: %d\n",
12775 			    nss, ret);
12776 		return ret;
12777 	}
12778 
12779 	ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
12780 					    WMI_VDEV_PARAM_LDPC, ldpc);
12781 	if (ret) {
12782 		ath12k_warn(ar->ab, "failed to set ldpc param %d: %d\n",
12783 			    ldpc, ret);
12784 		return ret;
12785 	}
12786 
12787 	if (eht_support) {
12788 		if (eht_fixed_rate)
12789 			ret = ath12k_mac_set_fixed_rate_gi_ltf(arvif, eht_gi, eht_ltf,
12790 							       WMI_VDEV_PARAM_EHT_LTF);
12791 		else
12792 			ret = ath12k_mac_set_auto_rate_gi_ltf(arvif, eht_gi, eht_ltf);
12793 
12794 		if (ret) {
12795 			ath12k_warn(ar->ab,
12796 				    "failed to set EHT LTF/GI params %d/%d: %d\n",
12797 				    eht_gi, eht_ltf, ret);
12798 			return ret;
12799 		}
12800 		gi_ltf_set = true;
12801 	}
12802 
12803 	if (he_support) {
12804 		if (he_fixed_rate)
12805 			ret = ath12k_mac_set_fixed_rate_gi_ltf(arvif, he_gi, he_ltf,
12806 							       WMI_VDEV_PARAM_HE_LTF);
12807 		else
12808 			ret = ath12k_mac_set_auto_rate_gi_ltf(arvif, he_gi, he_ltf);
12809 		if (ret)
12810 			return ret;
12811 		gi_ltf_set = true;
12812 	}
12813 
12814 	if (!gi_ltf_set) {
12815 		vdev_param = WMI_VDEV_PARAM_SGI;
12816 		param_value = ath12k_mac_nlgi_to_wmigi(sgi);
12817 		ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
12818 						    vdev_param, param_value);
12819 		if (ret) {
12820 			ath12k_warn(ar->ab, "failed to set sgi param %d: %d\n",
12821 				    sgi, ret);
12822 			return ret;
12823 		}
12824 	}
12825 
12826 	return 0;
12827 }
12828 
12829 static bool
12830 ath12k_mac_vht_mcs_range_present(struct ath12k *ar,
12831 				 enum nl80211_band band,
12832 				 const struct cfg80211_bitrate_mask *mask)
12833 {
12834 	int i;
12835 	u16 vht_mcs;
12836 
12837 	for (i = 0; i < NL80211_VHT_NSS_MAX; i++) {
12838 		vht_mcs = mask->control[band].vht_mcs[i];
12839 
12840 		switch (vht_mcs) {
12841 		case 0:
12842 		case BIT(8) - 1:
12843 		case BIT(9) - 1:
12844 		case BIT(10) - 1:
12845 			break;
12846 		default:
12847 			return false;
12848 		}
12849 	}
12850 
12851 	return true;
12852 }
12853 
12854 static bool
12855 ath12k_mac_he_mcs_range_present(struct ath12k *ar,
12856 				enum nl80211_band band,
12857 				const struct cfg80211_bitrate_mask *mask)
12858 {
12859 	int i;
12860 	u16 he_mcs;
12861 
12862 	for (i = 0; i < NL80211_HE_NSS_MAX; i++) {
12863 		he_mcs = mask->control[band].he_mcs[i];
12864 
12865 		switch (he_mcs) {
12866 		case 0:
12867 		case BIT(8) - 1:
12868 		case BIT(10) - 1:
12869 		case BIT(12) - 1:
12870 			break;
12871 		default:
12872 			return false;
12873 		}
12874 	}
12875 
12876 	return true;
12877 }
12878 
12879 static bool
12880 ath12k_mac_eht_mcs_range_present(struct ath12k *ar,
12881 				 enum nl80211_band band,
12882 				 const struct cfg80211_bitrate_mask *mask)
12883 {
12884 	u16 eht_mcs;
12885 	int i;
12886 
12887 	for (i = 0; i < NL80211_EHT_NSS_MAX; i++) {
12888 		eht_mcs = mask->control[band].eht_mcs[i];
12889 
12890 		switch (eht_mcs) {
12891 		case 0:
12892 		case BIT(8) - 1:
12893 		case BIT(10) - 1:
12894 		case BIT(12) - 1:
12895 		case BIT(14) - 1:
12896 			break;
12897 		case BIT(15) - 1:
12898 		case BIT(16) - 1:
12899 		case BIT(16) - BIT(14) - 1:
12900 			if (i != 0)
12901 				return false;
12902 			break;
12903 		default:
12904 			return false;
12905 		}
12906 	}
12907 
12908 	return true;
12909 }
12910 
12911 static void ath12k_mac_set_bitrate_mask_iter(void *data,
12912 					     struct ieee80211_sta *sta)
12913 {
12914 	struct ath12k_link_vif *arvif = data;
12915 	struct ath12k_sta *ahsta = ath12k_sta_to_ahsta(sta);
12916 	struct ath12k_link_sta *arsta;
12917 	struct ath12k *ar = arvif->ar;
12918 
12919 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
12920 
12921 	arsta = wiphy_dereference(ath12k_ar_to_hw(ar)->wiphy,
12922 				  ahsta->link[arvif->link_id]);
12923 	if (!arsta || arsta->arvif != arvif)
12924 		return;
12925 
12926 	spin_lock_bh(&ar->data_lock);
12927 	arsta->changed |= IEEE80211_RC_SUPP_RATES_CHANGED;
12928 	spin_unlock_bh(&ar->data_lock);
12929 
12930 	wiphy_work_queue(ath12k_ar_to_hw(ar)->wiphy, &arsta->update_wk);
12931 }
12932 
12933 static void ath12k_mac_disable_peer_fixed_rate(void *data,
12934 					       struct ieee80211_sta *sta)
12935 {
12936 	struct ath12k_link_vif *arvif = data;
12937 	struct ath12k_sta *ahsta = ath12k_sta_to_ahsta(sta);
12938 	struct ath12k_link_sta *arsta;
12939 	struct ath12k *ar = arvif->ar;
12940 	int ret;
12941 
12942 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
12943 
12944 	arsta = wiphy_dereference(ath12k_ar_to_hw(ar)->wiphy,
12945 				  ahsta->link[arvif->link_id]);
12946 
12947 	if (!arsta || arsta->arvif != arvif)
12948 		return;
12949 
12950 	ret = ath12k_wmi_set_peer_param(ar, arsta->addr,
12951 					arvif->vdev_id,
12952 					WMI_PEER_PARAM_FIXED_RATE,
12953 					WMI_FIXED_RATE_NONE);
12954 	if (ret)
12955 		ath12k_warn(ar->ab,
12956 			    "failed to disable peer fixed rate for STA %pM ret %d\n",
12957 			    arsta->addr, ret);
12958 }
12959 
12960 static bool
12961 ath12k_mac_validate_fixed_rate_settings(struct ath12k *ar, enum nl80211_band band,
12962 					const struct cfg80211_bitrate_mask *mask,
12963 					unsigned int link_id)
12964 {
12965 	bool eht_fixed_rate = false, he_fixed_rate = false, vht_fixed_rate = false;
12966 	const u16 *vht_mcs_mask, *he_mcs_mask, *eht_mcs_mask;
12967 	struct ieee80211_link_sta *link_sta;
12968 	struct ath12k_dp_link_peer *peer, *tmp;
12969 	u8 vht_nss, he_nss, eht_nss;
12970 	int ret = true;
12971 	struct ath12k_base *ab = ar->ab;
12972 	struct ath12k_dp *dp = ath12k_ab_to_dp(ab);
12973 
12974 	vht_mcs_mask = mask->control[band].vht_mcs;
12975 	he_mcs_mask = mask->control[band].he_mcs;
12976 	eht_mcs_mask = mask->control[band].eht_mcs;
12977 
12978 	if (ath12k_mac_bitrate_mask_num_vht_rates(ar, band, mask) == 1)
12979 		vht_fixed_rate = true;
12980 
12981 	if (ath12k_mac_bitrate_mask_num_he_rates(ar, band, mask) == 1)
12982 		he_fixed_rate = true;
12983 
12984 	if (ath12k_mac_bitrate_mask_num_eht_rates(ar, band, mask) == 1)
12985 		eht_fixed_rate = true;
12986 
12987 	if (!vht_fixed_rate && !he_fixed_rate && !eht_fixed_rate)
12988 		return true;
12989 
12990 	vht_nss = ath12k_mac_max_vht_nss(vht_mcs_mask);
12991 	he_nss =  ath12k_mac_max_he_nss(he_mcs_mask);
12992 	eht_nss = ath12k_mac_max_eht_nss(eht_mcs_mask);
12993 
12994 	rcu_read_lock();
12995 	spin_lock_bh(&dp->dp_lock);
12996 	list_for_each_entry_safe(peer, tmp, &dp->peers, list) {
12997 		if (peer->sta) {
12998 			link_sta = rcu_dereference(peer->sta->link[link_id]);
12999 			if (!link_sta) {
13000 				ret = false;
13001 				goto exit;
13002 			}
13003 
13004 			if (vht_fixed_rate && (!link_sta->vht_cap.vht_supported ||
13005 					       link_sta->rx_nss < vht_nss)) {
13006 				ret = false;
13007 				goto exit;
13008 			}
13009 			if (he_fixed_rate && (!link_sta->he_cap.has_he ||
13010 					      link_sta->rx_nss < he_nss)) {
13011 				ret = false;
13012 				goto exit;
13013 			}
13014 			if (eht_fixed_rate && (!link_sta->eht_cap.has_eht ||
13015 					       link_sta->rx_nss < eht_nss)) {
13016 				ret = false;
13017 				goto exit;
13018 			}
13019 		}
13020 	}
13021 exit:
13022 	spin_unlock_bh(&dp->dp_lock);
13023 	rcu_read_unlock();
13024 	return ret;
13025 }
13026 
13027 int
13028 ath12k_mac_op_set_bitrate_mask(struct ieee80211_hw *hw,
13029 			       struct ieee80211_vif *vif,
13030 			       const struct cfg80211_bitrate_mask *mask)
13031 {
13032 	struct ath12k_vif *ahvif = ath12k_vif_to_ahvif(vif);
13033 	struct ath12k_link_vif *arvif;
13034 	struct cfg80211_chan_def def;
13035 	struct ath12k *ar;
13036 	enum nl80211_band band;
13037 	const u8 *ht_mcs_mask;
13038 	const u16 *vht_mcs_mask;
13039 	const u16 *he_mcs_mask;
13040 	const u16 *eht_mcs_mask;
13041 	u8 he_ltf = 0;
13042 	u8 he_gi = 0;
13043 	u8 eht_ltf = 0, eht_gi = 0;
13044 	u32 rate;
13045 	u8 nss, mac_nss;
13046 	u8 sgi;
13047 	u8 ldpc;
13048 	int single_nss;
13049 	int ret;
13050 	int num_rates;
13051 	bool he_fixed_rate = false;
13052 	bool eht_fixed_rate = false;
13053 
13054 	lockdep_assert_wiphy(hw->wiphy);
13055 
13056 	arvif = &ahvif->deflink;
13057 
13058 	ar = arvif->ar;
13059 	if (ath12k_mac_vif_link_chan(vif, arvif->link_id, &def)) {
13060 		ret = -EPERM;
13061 		goto out;
13062 	}
13063 
13064 	band = def.chan->band;
13065 	ht_mcs_mask = mask->control[band].ht_mcs;
13066 	vht_mcs_mask = mask->control[band].vht_mcs;
13067 	he_mcs_mask = mask->control[band].he_mcs;
13068 	eht_mcs_mask = mask->control[band].eht_mcs;
13069 	ldpc = !!(ar->ht_cap_info & WMI_HT_CAP_LDPC);
13070 
13071 	sgi = mask->control[band].gi;
13072 	if (sgi == NL80211_TXRATE_FORCE_SGI) {
13073 		ret = -EINVAL;
13074 		goto out;
13075 	}
13076 
13077 	he_gi = mask->control[band].he_gi;
13078 	he_ltf = mask->control[band].he_ltf;
13079 
13080 	eht_gi = mask->control[band].eht_gi;
13081 	eht_ltf = mask->control[band].eht_ltf;
13082 
13083 	/* mac80211 doesn't support sending a fixed HT/VHT MCS alone, rather it
13084 	 * requires passing at least one of used basic rates along with them.
13085 	 * Fixed rate setting across different preambles(legacy, HT, VHT) is
13086 	 * not supported by the FW. Hence use of FIXED_RATE vdev param is not
13087 	 * suitable for setting single HT/VHT rates.
13088 	 * But, there could be a single basic rate passed from userspace which
13089 	 * can be done through the FIXED_RATE param.
13090 	 */
13091 	if (ath12k_mac_has_single_legacy_rate(ar, band, mask)) {
13092 		ret = ath12k_mac_get_single_legacy_rate(ar, band, mask, &rate,
13093 							&nss);
13094 		if (ret) {
13095 			ath12k_warn(ar->ab, "failed to get single legacy rate for vdev %i: %d\n",
13096 				    arvif->vdev_id, ret);
13097 			goto out;
13098 		}
13099 
13100 		ieee80211_iterate_stations_mtx(hw,
13101 					       ath12k_mac_disable_peer_fixed_rate,
13102 					       arvif);
13103 	} else if (ath12k_mac_bitrate_mask_get_single_nss(ar, vif, band, mask,
13104 							  &single_nss)) {
13105 		rate = WMI_FIXED_RATE_NONE;
13106 		nss = single_nss;
13107 		arvif->bitrate_mask = *mask;
13108 
13109 		ieee80211_iterate_stations_atomic(hw,
13110 						  ath12k_mac_set_bitrate_mask_iter,
13111 						  arvif);
13112 	} else {
13113 		rate = WMI_FIXED_RATE_NONE;
13114 
13115 		if (!ath12k_mac_validate_fixed_rate_settings(ar, band,
13116 							     mask, arvif->link_id))
13117 			ath12k_warn(ar->ab,
13118 				    "failed to update fixed rate settings due to mcs/nss incompatibility\n");
13119 
13120 		mac_nss = max(max3(ath12k_mac_max_ht_nss(ht_mcs_mask),
13121 				   ath12k_mac_max_vht_nss(vht_mcs_mask),
13122 				   ath12k_mac_max_he_nss(he_mcs_mask)),
13123 			       ath12k_mac_max_eht_nss(eht_mcs_mask));
13124 		nss = min_t(u32, ar->num_tx_chains, mac_nss);
13125 
13126 		/* If multiple rates across different preambles are given
13127 		 * we can reconfigure this info with all peers using PEER_ASSOC
13128 		 * command with the below exception cases.
13129 		 * - Single VHT Rate : peer_assoc command accommodates only MCS
13130 		 * range values i.e 0-7, 0-8, 0-9 for VHT. Though mac80211
13131 		 * mandates passing basic rates along with HT/VHT rates, FW
13132 		 * doesn't allow switching from VHT to Legacy. Hence instead of
13133 		 * setting legacy and VHT rates using RATEMASK_CMD vdev cmd,
13134 		 * we could set this VHT rate as peer fixed rate param, which
13135 		 * will override FIXED rate and FW rate control algorithm.
13136 		 * If single VHT rate is passed along with HT rates, we select
13137 		 * the VHT rate as fixed rate for vht peers.
13138 		 * - Multiple VHT Rates : When Multiple VHT rates are given,this
13139 		 * can be set using RATEMASK CMD which uses FW rate-ctl alg.
13140 		 * TODO: Setting multiple VHT MCS and replacing peer_assoc with
13141 		 * RATEMASK_CMDID can cover all use cases of setting rates
13142 		 * across multiple preambles and rates within same type.
13143 		 * But requires more validation of the command at this point.
13144 		 */
13145 
13146 		num_rates = ath12k_mac_bitrate_mask_num_vht_rates(ar, band,
13147 								  mask);
13148 
13149 		if (!ath12k_mac_vht_mcs_range_present(ar, band, mask) &&
13150 		    num_rates > 1) {
13151 			/* TODO: Handle multiple VHT MCS values setting using
13152 			 * RATEMASK CMD
13153 			 */
13154 			ath12k_warn(ar->ab,
13155 				    "Setting more than one MCS Value in bitrate mask not supported\n");
13156 			ret = -EINVAL;
13157 			goto out;
13158 		}
13159 
13160 		num_rates = ath12k_mac_bitrate_mask_num_he_rates(ar, band, mask);
13161 		if (num_rates == 1)
13162 			he_fixed_rate = true;
13163 
13164 		if (!ath12k_mac_he_mcs_range_present(ar, band, mask) &&
13165 		    num_rates > 1) {
13166 			ath12k_warn(ar->ab,
13167 				    "Setting more than one HE MCS Value in bitrate mask not supported\n");
13168 			ret = -EINVAL;
13169 			goto out;
13170 		}
13171 
13172 		num_rates = ath12k_mac_bitrate_mask_num_eht_rates(ar, band,
13173 								  mask);
13174 		if (num_rates == 1)
13175 			eht_fixed_rate = true;
13176 
13177 		if (!ath12k_mac_eht_mcs_range_present(ar, band, mask) &&
13178 		    num_rates > 1) {
13179 			ath12k_warn(ar->ab,
13180 				    "Setting more than one EHT MCS Value in bitrate mask not supported\n");
13181 			ret = -EINVAL;
13182 			goto out;
13183 		}
13184 
13185 		ieee80211_iterate_stations_mtx(hw,
13186 					       ath12k_mac_disable_peer_fixed_rate,
13187 					       arvif);
13188 
13189 		arvif->bitrate_mask = *mask;
13190 		ieee80211_iterate_stations_mtx(hw,
13191 					       ath12k_mac_set_bitrate_mask_iter,
13192 					       arvif);
13193 	}
13194 
13195 	ret = ath12k_mac_set_rate_params(arvif, rate, nss, sgi, ldpc, he_gi,
13196 					 he_ltf, he_fixed_rate, eht_gi, eht_ltf,
13197 					 eht_fixed_rate);
13198 	if (ret) {
13199 		ath12k_warn(ar->ab, "failed to set rate params on vdev %i: %d\n",
13200 			    arvif->vdev_id, ret);
13201 	}
13202 
13203 out:
13204 	return ret;
13205 }
13206 EXPORT_SYMBOL(ath12k_mac_op_set_bitrate_mask);
13207 
13208 void
13209 ath12k_mac_op_reconfig_complete(struct ieee80211_hw *hw,
13210 				enum ieee80211_reconfig_type reconfig_type)
13211 {
13212 	struct ath12k_hw *ah = ath12k_hw_to_ah(hw);
13213 	struct ath12k *ar;
13214 	struct ath12k_base *ab;
13215 	struct ath12k_vif *ahvif;
13216 	struct ath12k_link_vif *arvif;
13217 	int recovery_count, i;
13218 
13219 	lockdep_assert_wiphy(hw->wiphy);
13220 
13221 	if (reconfig_type != IEEE80211_RECONFIG_TYPE_RESTART)
13222 		return;
13223 
13224 	guard(mutex)(&ah->hw_mutex);
13225 
13226 	if (ah->state != ATH12K_HW_STATE_RESTARTED)
13227 		return;
13228 
13229 	ah->state = ATH12K_HW_STATE_ON;
13230 	ieee80211_wake_queues(hw);
13231 
13232 	for_each_ar(ah, ar, i) {
13233 		ab = ar->ab;
13234 
13235 		ath12k_warn(ar->ab, "pdev %d successfully recovered\n",
13236 			    ar->pdev->pdev_id);
13237 
13238 		if (ar->ab->hw_params->current_cc_support &&
13239 		    ar->alpha2[0] != 0 && ar->alpha2[1] != 0) {
13240 			struct wmi_set_current_country_arg arg = {};
13241 
13242 			memcpy(&arg.alpha2, ar->alpha2, 2);
13243 			reinit_completion(&ar->regd_update_completed);
13244 			ath12k_wmi_send_set_current_country_cmd(ar, &arg);
13245 		}
13246 
13247 		if (ab->is_reset) {
13248 			recovery_count = atomic_inc_return(&ab->recovery_count);
13249 
13250 			ath12k_dbg(ab, ATH12K_DBG_BOOT, "recovery count %d\n",
13251 				   recovery_count);
13252 
13253 			/* When there are multiple radios in an SOC,
13254 			 * the recovery has to be done for each radio
13255 			 */
13256 			if (recovery_count == ab->num_radios) {
13257 				atomic_dec(&ab->reset_count);
13258 				complete(&ab->reset_complete);
13259 				ab->is_reset = false;
13260 				atomic_set(&ab->fail_cont_count, 0);
13261 				ath12k_dbg(ab, ATH12K_DBG_BOOT, "reset success\n");
13262 			}
13263 		}
13264 
13265 		list_for_each_entry(arvif, &ar->arvifs, list) {
13266 			ahvif = arvif->ahvif;
13267 			ath12k_dbg(ab, ATH12K_DBG_BOOT,
13268 				   "reconfig cipher %d up %d vdev type %d\n",
13269 				   ahvif->dp_vif.key_cipher,
13270 				   arvif->is_up,
13271 				   ahvif->vdev_type);
13272 
13273 			/* After trigger disconnect, then upper layer will
13274 			 * trigger connect again, then the PN number of
13275 			 * upper layer will be reset to keep up with AP
13276 			 * side, hence PN number mismatch will not happen.
13277 			 */
13278 			if (arvif->is_up &&
13279 			    ahvif->vdev_type == WMI_VDEV_TYPE_STA &&
13280 			    ahvif->vdev_subtype == WMI_VDEV_SUBTYPE_NONE) {
13281 				ieee80211_hw_restart_disconnect(ahvif->vif);
13282 
13283 				ath12k_dbg(ab, ATH12K_DBG_BOOT,
13284 					   "restart disconnect\n");
13285 			}
13286 		}
13287 	}
13288 }
13289 EXPORT_SYMBOL(ath12k_mac_op_reconfig_complete);
13290 
13291 static void
13292 ath12k_mac_update_bss_chan_survey(struct ath12k *ar,
13293 				  struct ieee80211_channel *channel)
13294 {
13295 	int ret;
13296 	enum wmi_bss_chan_info_req_type type = WMI_BSS_SURVEY_REQ_TYPE_READ;
13297 
13298 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
13299 
13300 	if (!test_bit(WMI_TLV_SERVICE_BSS_CHANNEL_INFO_64, ar->ab->wmi_ab.svc_map) ||
13301 	    ar->rx_channel != channel)
13302 		return;
13303 
13304 	if (ar->scan.state != ATH12K_SCAN_IDLE) {
13305 		ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
13306 			   "ignoring bss chan info req while scanning..\n");
13307 		return;
13308 	}
13309 
13310 	reinit_completion(&ar->bss_survey_done);
13311 
13312 	ret = ath12k_wmi_pdev_bss_chan_info_request(ar, type);
13313 	if (ret) {
13314 		ath12k_warn(ar->ab, "failed to send pdev bss chan info request\n");
13315 		return;
13316 	}
13317 
13318 	ret = wait_for_completion_timeout(&ar->bss_survey_done, 3 * HZ);
13319 	if (ret == 0)
13320 		ath12k_warn(ar->ab, "bss channel survey timed out\n");
13321 }
13322 
13323 int ath12k_mac_op_get_survey(struct ieee80211_hw *hw, int idx,
13324 			     struct survey_info *survey)
13325 {
13326 	struct ath12k *ar;
13327 	struct ieee80211_supported_band *sband;
13328 	struct survey_info *ar_survey;
13329 
13330 	lockdep_assert_wiphy(hw->wiphy);
13331 
13332 	if (idx >= ATH12K_NUM_CHANS)
13333 		return -ENOENT;
13334 
13335 	sband = hw->wiphy->bands[NL80211_BAND_2GHZ];
13336 	if (sband && idx >= sband->n_channels) {
13337 		idx -= sband->n_channels;
13338 		sband = NULL;
13339 	}
13340 
13341 	if (!sband)
13342 		sband = hw->wiphy->bands[NL80211_BAND_5GHZ];
13343 	if (sband && idx >= sband->n_channels) {
13344 		idx -= sband->n_channels;
13345 		sband = NULL;
13346 	}
13347 
13348 	if (!sband)
13349 		sband = hw->wiphy->bands[NL80211_BAND_6GHZ];
13350 
13351 	if (!sband || idx >= sband->n_channels)
13352 		return -ENOENT;
13353 
13354 	ar = ath12k_mac_get_ar_by_chan(hw, &sband->channels[idx]);
13355 	if (!ar) {
13356 		if (sband->channels[idx].flags & IEEE80211_CHAN_DISABLED) {
13357 			memset(survey, 0, sizeof(*survey));
13358 			return 0;
13359 		}
13360 		return -ENOENT;
13361 	}
13362 
13363 	ar_survey = &ar->survey[idx];
13364 
13365 	ath12k_mac_update_bss_chan_survey(ar, &sband->channels[idx]);
13366 
13367 	spin_lock_bh(&ar->data_lock);
13368 	memcpy(survey, ar_survey, sizeof(*survey));
13369 	spin_unlock_bh(&ar->data_lock);
13370 
13371 	survey->channel = &sband->channels[idx];
13372 
13373 	if (ar->rx_channel == survey->channel)
13374 		survey->filled |= SURVEY_INFO_IN_USE;
13375 
13376 	return 0;
13377 }
13378 EXPORT_SYMBOL(ath12k_mac_op_get_survey);
13379 
13380 static void ath12k_mac_put_chain_rssi(struct station_info *sinfo,
13381 				      struct ath12k_link_sta *arsta)
13382 {
13383 	s8 rssi;
13384 	int i;
13385 
13386 	for (i = 0; i < ARRAY_SIZE(sinfo->chain_signal); i++) {
13387 		sinfo->chains &= ~BIT(i);
13388 		rssi = arsta->chain_signal[i];
13389 
13390 		if (rssi != ATH12K_DEFAULT_NOISE_FLOOR &&
13391 		    rssi != ATH12K_INVALID_RSSI_FULL &&
13392 		    rssi != ATH12K_INVALID_RSSI_EMPTY &&
13393 		    rssi != 0) {
13394 			sinfo->chain_signal[i] = rssi;
13395 			sinfo->chains |= BIT(i);
13396 			sinfo->filled |= BIT_ULL(NL80211_STA_INFO_CHAIN_SIGNAL);
13397 		}
13398 	}
13399 }
13400 
13401 void ath12k_mac_op_sta_statistics(struct ieee80211_hw *hw,
13402 				  struct ieee80211_vif *vif,
13403 				  struct ieee80211_sta *sta,
13404 				  struct station_info *sinfo)
13405 {
13406 	struct ath12k_sta *ahsta = ath12k_sta_to_ahsta(sta);
13407 	struct ath12k_dp_link_peer_rate_info rate_info = {};
13408 	struct ath12k_fw_stats_req_params params = {};
13409 	struct ath12k_dp_link_peer *peer;
13410 	struct ath12k_link_sta *arsta;
13411 	s8 signal, noise_floor;
13412 	struct ath12k_dp *dp;
13413 	struct ath12k *ar;
13414 	bool db2dbm;
13415 
13416 	lockdep_assert_wiphy(hw->wiphy);
13417 
13418 	arsta = &ahsta->deflink;
13419 	ar = ath12k_get_ar_by_vif(hw, vif, arsta->link_id);
13420 	if (!ar)
13421 		return;
13422 
13423 	dp = ath12k_ab_to_dp(ar->ab);
13424 	ath12k_dp_link_peer_get_sta_rate_info_stats(dp, arsta->addr, &rate_info);
13425 
13426 	db2dbm = test_bit(WMI_TLV_SERVICE_HW_DB2DBM_CONVERSION_SUPPORT,
13427 			  ar->ab->wmi_ab.svc_map);
13428 
13429 	sinfo->rx_duration = rate_info.rx_duration;
13430 	sinfo->filled |= BIT_ULL(NL80211_STA_INFO_RX_DURATION);
13431 
13432 	sinfo->tx_duration = rate_info.tx_duration;
13433 	sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_DURATION);
13434 
13435 	if (rate_info.txrate.legacy || rate_info.txrate.nss) {
13436 		if (rate_info.txrate.legacy) {
13437 			sinfo->txrate.legacy = rate_info.txrate.legacy;
13438 		} else {
13439 			sinfo->txrate.mcs = rate_info.txrate.mcs;
13440 			sinfo->txrate.nss = rate_info.txrate.nss;
13441 			sinfo->txrate.bw = rate_info.txrate.bw;
13442 			sinfo->txrate.he_gi = rate_info.txrate.he_gi;
13443 			sinfo->txrate.he_dcm = rate_info.txrate.he_dcm;
13444 			sinfo->txrate.he_ru_alloc = rate_info.txrate.he_ru_alloc;
13445 			sinfo->txrate.eht_gi = rate_info.txrate.eht_gi;
13446 			sinfo->txrate.eht_ru_alloc = rate_info.txrate.eht_ru_alloc;
13447 		}
13448 		sinfo->txrate.flags = rate_info.txrate.flags;
13449 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_BITRATE);
13450 	}
13451 
13452 	/* TODO: Use real NF instead of default one. */
13453 	signal = rate_info.rssi_comb;
13454 
13455 	params.pdev_id = ar->pdev->pdev_id;
13456 	params.vdev_id = 0;
13457 	params.stats_id = WMI_REQUEST_VDEV_STAT;
13458 
13459 	if (!signal &&
13460 	    ahsta->ahvif->vdev_type == WMI_VDEV_TYPE_STA &&
13461 	    !(ath12k_mac_get_fw_stats(ar, &params))) {
13462 		signal = arsta->rssi_beacon;
13463 		ath12k_fw_stats_reset(ar);
13464 	}
13465 
13466 	params.stats_id = WMI_REQUEST_RSSI_PER_CHAIN_STAT;
13467 	if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_CHAIN_SIGNAL)) &&
13468 	    ahsta->ahvif->vdev_type == WMI_VDEV_TYPE_STA &&
13469 	    !(ath12k_mac_get_fw_stats(ar, &params))) {
13470 		ath12k_mac_put_chain_rssi(sinfo, arsta);
13471 		ath12k_fw_stats_reset(ar);
13472 	}
13473 
13474 	spin_lock_bh(&ar->data_lock);
13475 	noise_floor = ath12k_pdev_get_noise_floor(ar);
13476 	spin_unlock_bh(&ar->data_lock);
13477 
13478 	if (signal) {
13479 		sinfo->signal = db2dbm ? signal : signal + noise_floor;
13480 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_SIGNAL);
13481 	}
13482 
13483 	sinfo->signal_avg = rate_info.signal_avg;
13484 
13485 	if (!db2dbm)
13486 		sinfo->signal_avg += noise_floor;
13487 
13488 	sinfo->filled |= BIT_ULL(NL80211_STA_INFO_SIGNAL_AVG);
13489 
13490 	spin_lock_bh(&dp->dp_lock);
13491 	peer = ath12k_dp_link_peer_find_by_addr(dp, arsta->addr);
13492 	if (!peer) {
13493 		spin_unlock_bh(&dp->dp_lock);
13494 		return;
13495 	}
13496 
13497 	sinfo->tx_retries = peer->tx_retry_count;
13498 	sinfo->tx_failed = peer->tx_retry_failed;
13499 	sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_RETRIES);
13500 	sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_FAILED);
13501 
13502 	spin_unlock_bh(&dp->dp_lock);
13503 }
13504 EXPORT_SYMBOL(ath12k_mac_op_sta_statistics);
13505 
13506 void ath12k_mac_op_link_sta_statistics(struct ieee80211_hw *hw,
13507 				       struct ieee80211_vif *vif,
13508 				       struct ieee80211_link_sta *link_sta,
13509 				       struct link_station_info *link_sinfo)
13510 {
13511 	struct ath12k_sta *ahsta = ath12k_sta_to_ahsta(link_sta->sta);
13512 	struct ath12k_fw_stats_req_params params = {};
13513 	struct ath12k_dp_link_peer *peer;
13514 	struct ath12k_link_sta *arsta;
13515 	struct ath12k *ar;
13516 	s8 signal;
13517 	bool db2dbm;
13518 
13519 	lockdep_assert_wiphy(hw->wiphy);
13520 
13521 	arsta = wiphy_dereference(hw->wiphy, ahsta->link[link_sta->link_id]);
13522 
13523 	if (!arsta)
13524 		return;
13525 
13526 	ar = ath12k_get_ar_by_vif(hw, vif, arsta->link_id);
13527 	if (!ar)
13528 		return;
13529 
13530 	db2dbm = test_bit(WMI_TLV_SERVICE_HW_DB2DBM_CONVERSION_SUPPORT,
13531 			  ar->ab->wmi_ab.svc_map);
13532 
13533 	spin_lock_bh(&ar->ab->dp->dp_lock);
13534 	peer = ath12k_dp_link_peer_find_by_addr(ar->ab->dp, arsta->addr);
13535 	if (!peer) {
13536 		spin_unlock_bh(&ar->ab->dp->dp_lock);
13537 		return;
13538 	}
13539 
13540 	link_sinfo->rx_duration = peer->rx_duration;
13541 	link_sinfo->filled |= BIT_ULL(NL80211_STA_INFO_RX_DURATION);
13542 
13543 	link_sinfo->tx_duration = peer->tx_duration;
13544 	link_sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_DURATION);
13545 
13546 	if (peer->txrate.legacy || peer->txrate.nss) {
13547 		if (peer->txrate.legacy) {
13548 			link_sinfo->txrate.legacy = peer->txrate.legacy;
13549 		} else {
13550 			link_sinfo->txrate.mcs = peer->txrate.mcs;
13551 			link_sinfo->txrate.nss = peer->txrate.nss;
13552 			link_sinfo->txrate.bw = peer->txrate.bw;
13553 			link_sinfo->txrate.he_gi = peer->txrate.he_gi;
13554 			link_sinfo->txrate.he_dcm = peer->txrate.he_dcm;
13555 			link_sinfo->txrate.he_ru_alloc =
13556 				peer->txrate.he_ru_alloc;
13557 			link_sinfo->txrate.eht_gi = peer->txrate.eht_gi;
13558 			link_sinfo->txrate.eht_ru_alloc =
13559 				peer->txrate.eht_ru_alloc;
13560 		}
13561 		link_sinfo->txrate.flags = peer->txrate.flags;
13562 		link_sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_BITRATE);
13563 	}
13564 
13565 	link_sinfo->signal_avg = ewma_avg_rssi_read(&peer->avg_rssi);
13566 
13567 	if (!db2dbm)
13568 		link_sinfo->signal_avg += ATH12K_DEFAULT_NOISE_FLOOR;
13569 
13570 	link_sinfo->filled |= BIT_ULL(NL80211_STA_INFO_SIGNAL_AVG);
13571 
13572 	link_sinfo->tx_retries = peer->tx_retry_count;
13573 	link_sinfo->tx_failed = peer->tx_retry_failed;
13574 	link_sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_RETRIES);
13575 	link_sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_FAILED);
13576 
13577 	/* TODO: Use real NF instead of default one. */
13578 	signal = peer->rssi_comb;
13579 
13580 	spin_unlock_bh(&ar->ab->dp->dp_lock);
13581 
13582 	if (!signal && ahsta->ahvif->vdev_type == WMI_VDEV_TYPE_STA) {
13583 		params.pdev_id = ar->pdev->pdev_id;
13584 		params.vdev_id = 0;
13585 		params.stats_id = WMI_REQUEST_VDEV_STAT;
13586 
13587 		if (!ath12k_mac_get_fw_stats(ar, &params)) {
13588 			signal = arsta->rssi_beacon;
13589 			ath12k_fw_stats_reset(ar);
13590 		}
13591 	}
13592 
13593 	if (signal) {
13594 		link_sinfo->signal =
13595 			db2dbm ? signal : signal + ATH12K_DEFAULT_NOISE_FLOOR;
13596 		link_sinfo->filled |= BIT_ULL(NL80211_STA_INFO_SIGNAL);
13597 	}
13598 }
13599 EXPORT_SYMBOL(ath12k_mac_op_link_sta_statistics);
13600 
13601 int ath12k_mac_op_cancel_remain_on_channel(struct ieee80211_hw *hw,
13602 					   struct ieee80211_vif *vif)
13603 {
13604 	struct ath12k_hw *ah = ath12k_hw_to_ah(hw);
13605 	struct ath12k *ar;
13606 
13607 	ar = ath12k_ah_to_ar(ah, 0);
13608 
13609 	lockdep_assert_wiphy(hw->wiphy);
13610 
13611 	spin_lock_bh(&ar->data_lock);
13612 	ar->scan.roc_notify = false;
13613 	spin_unlock_bh(&ar->data_lock);
13614 
13615 	ath12k_scan_abort(ar);
13616 
13617 	cancel_delayed_work_sync(&ar->scan.timeout);
13618 	wiphy_work_flush(hw->wiphy, &ar->scan.vdev_clean_wk);
13619 
13620 	return 0;
13621 }
13622 EXPORT_SYMBOL(ath12k_mac_op_cancel_remain_on_channel);
13623 
13624 int ath12k_mac_op_remain_on_channel(struct ieee80211_hw *hw,
13625 				    struct ieee80211_vif *vif,
13626 				    struct ieee80211_channel *chan,
13627 				    int duration,
13628 				    enum ieee80211_roc_type type)
13629 {
13630 	struct ath12k_vif *ahvif = ath12k_vif_to_ahvif(vif);
13631 	struct ath12k_hw *ah = ath12k_hw_to_ah(hw);
13632 	struct ath12k_link_vif *arvif;
13633 	struct ath12k *ar;
13634 	u32 scan_time_msec;
13635 	bool create = true;
13636 	u8 link_id;
13637 	int ret;
13638 
13639 	lockdep_assert_wiphy(hw->wiphy);
13640 
13641 	ar = ath12k_mac_select_scan_device(hw, vif, chan->center_freq);
13642 	if (!ar)
13643 		return -EINVAL;
13644 
13645 	/* check if any of the links of ML VIF is already started on
13646 	 * radio(ar) corresponding to given scan frequency and use it,
13647 	 * if not use deflink(link 0) for scan purpose.
13648 	 */
13649 
13650 	link_id = ath12k_mac_find_link_id_by_ar(ahvif, ar);
13651 	arvif = ath12k_mac_assign_link_vif(ah, vif, link_id);
13652 	/* If the vif is already assigned to a specific vdev of an ar,
13653 	 * check whether its already started, vdev which is started
13654 	 * are not allowed to switch to a new radio.
13655 	 * If the vdev is not started, but was earlier created on a
13656 	 * different ar, delete that vdev and create a new one. We don't
13657 	 * delete at the scan stop as an optimization to avoid redundant
13658 	 * delete-create vdev's for the same ar, in case the request is
13659 	 * always on the same band for the vif
13660 	 */
13661 	if (arvif->is_created) {
13662 		if (WARN_ON(!arvif->ar))
13663 			return -EINVAL;
13664 
13665 		if (ar != arvif->ar && arvif->is_started)
13666 			return -EBUSY;
13667 
13668 		if (ar != arvif->ar) {
13669 			ath12k_mac_remove_link_interface(hw, arvif);
13670 			ath12k_mac_unassign_link_vif(arvif);
13671 		} else {
13672 			create = false;
13673 		}
13674 	}
13675 
13676 	if (create) {
13677 		arvif = ath12k_mac_assign_link_vif(ah, vif, link_id);
13678 
13679 		ret = ath12k_mac_vdev_create(ar, arvif);
13680 		if (ret) {
13681 			ath12k_warn(ar->ab, "unable to create scan vdev for roc: %d\n",
13682 				    ret);
13683 			ath12k_mac_unassign_link_vif(arvif);
13684 			return ret;
13685 		}
13686 	}
13687 
13688 	spin_lock_bh(&ar->data_lock);
13689 
13690 	switch (ar->scan.state) {
13691 	case ATH12K_SCAN_IDLE:
13692 		reinit_completion(&ar->scan.started);
13693 		reinit_completion(&ar->scan.completed);
13694 		reinit_completion(&ar->scan.on_channel);
13695 		ar->scan.state = ATH12K_SCAN_STARTING;
13696 		ar->scan.is_roc = true;
13697 		ar->scan.arvif = arvif;
13698 		ar->scan.roc_freq = chan->center_freq;
13699 		ar->scan.roc_notify = true;
13700 		ret = 0;
13701 		break;
13702 	case ATH12K_SCAN_STARTING:
13703 	case ATH12K_SCAN_RUNNING:
13704 	case ATH12K_SCAN_ABORTING:
13705 		ret = -EBUSY;
13706 		break;
13707 	}
13708 
13709 	spin_unlock_bh(&ar->data_lock);
13710 
13711 	if (ret)
13712 		return ret;
13713 
13714 	scan_time_msec = hw->wiphy->max_remain_on_channel_duration * 2;
13715 
13716 	struct ath12k_wmi_scan_req_arg *arg __free(kfree) =
13717 					kzalloc(sizeof(*arg), GFP_KERNEL);
13718 	if (!arg)
13719 		return -ENOMEM;
13720 
13721 	ath12k_wmi_start_scan_init(ar, arg);
13722 	arg->num_chan = 1;
13723 
13724 	u32 *chan_list __free(kfree) = kcalloc(arg->num_chan, sizeof(*chan_list),
13725 					       GFP_KERNEL);
13726 	if (!chan_list)
13727 		return -ENOMEM;
13728 
13729 	arg->chan_list = chan_list;
13730 	arg->vdev_id = arvif->vdev_id;
13731 	arg->scan_id = ATH12K_SCAN_ID;
13732 	arg->chan_list[0] = chan->center_freq;
13733 	arg->dwell_time_active = scan_time_msec;
13734 	arg->dwell_time_passive = scan_time_msec;
13735 	arg->max_scan_time = scan_time_msec;
13736 	arg->scan_f_passive = 1;
13737 	arg->burst_duration = duration;
13738 
13739 	ret = ath12k_start_scan(ar, arg);
13740 	if (ret) {
13741 		ath12k_warn(ar->ab, "failed to start roc scan: %d\n", ret);
13742 
13743 		spin_lock_bh(&ar->data_lock);
13744 		ar->scan.state = ATH12K_SCAN_IDLE;
13745 		spin_unlock_bh(&ar->data_lock);
13746 		return ret;
13747 	}
13748 
13749 	ret = wait_for_completion_timeout(&ar->scan.on_channel, 3 * HZ);
13750 	if (ret == 0) {
13751 		ath12k_warn(ar->ab, "failed to switch to channel for roc scan\n");
13752 		ret = ath12k_scan_stop(ar);
13753 		if (ret)
13754 			ath12k_warn(ar->ab, "failed to stop scan: %d\n", ret);
13755 		return -ETIMEDOUT;
13756 	}
13757 
13758 	ieee80211_queue_delayed_work(hw, &ar->scan.timeout,
13759 				     msecs_to_jiffies(duration));
13760 
13761 	return 0;
13762 }
13763 EXPORT_SYMBOL(ath12k_mac_op_remain_on_channel);
13764 
13765 void ath12k_mac_op_set_rekey_data(struct ieee80211_hw *hw,
13766 				  struct ieee80211_vif *vif,
13767 				  struct cfg80211_gtk_rekey_data *data)
13768 {
13769 	struct ath12k_vif *ahvif = ath12k_vif_to_ahvif(vif);
13770 	struct ath12k_rekey_data *rekey_data;
13771 	struct ath12k_hw *ah = ath12k_hw_to_ah(hw);
13772 	struct ath12k *ar = ath12k_ah_to_ar(ah, 0);
13773 	struct ath12k_link_vif *arvif;
13774 
13775 	lockdep_assert_wiphy(hw->wiphy);
13776 
13777 	arvif = &ahvif->deflink;
13778 	rekey_data = &arvif->rekey_data;
13779 
13780 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac set rekey data vdev %d\n",
13781 		   arvif->vdev_id);
13782 
13783 	memcpy(rekey_data->kck, data->kck, NL80211_KCK_LEN);
13784 	memcpy(rekey_data->kek, data->kek, NL80211_KEK_LEN);
13785 
13786 	/* The supplicant works on big-endian, the firmware expects it on
13787 	 * little endian.
13788 	 */
13789 	rekey_data->replay_ctr = get_unaligned_be64(data->replay_ctr);
13790 
13791 	arvif->rekey_data.enable_offload = true;
13792 
13793 	ath12k_dbg_dump(ar->ab, ATH12K_DBG_MAC, "kck", NULL,
13794 			rekey_data->kck, NL80211_KCK_LEN);
13795 	ath12k_dbg_dump(ar->ab, ATH12K_DBG_MAC, "kek", NULL,
13796 			rekey_data->kck, NL80211_KEK_LEN);
13797 	ath12k_dbg_dump(ar->ab, ATH12K_DBG_MAC, "replay ctr", NULL,
13798 			&rekey_data->replay_ctr, sizeof(rekey_data->replay_ctr));
13799 }
13800 EXPORT_SYMBOL(ath12k_mac_op_set_rekey_data);
13801 
13802 void ath12k_mac_update_freq_range(struct ath12k *ar,
13803 				  u32 freq_low, u32 freq_high)
13804 {
13805 	if (!(freq_low && freq_high))
13806 		return;
13807 
13808 	if (ar->freq_range.start_freq || ar->freq_range.end_freq) {
13809 		ar->freq_range.start_freq = min(ar->freq_range.start_freq,
13810 						MHZ_TO_KHZ(freq_low));
13811 		ar->freq_range.end_freq = max(ar->freq_range.end_freq,
13812 					      MHZ_TO_KHZ(freq_high));
13813 	} else {
13814 		ar->freq_range.start_freq = MHZ_TO_KHZ(freq_low);
13815 		ar->freq_range.end_freq = MHZ_TO_KHZ(freq_high);
13816 	}
13817 
13818 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
13819 		   "mac pdev %u freq limit updated. New range %u->%u MHz\n",
13820 		   ar->pdev->pdev_id, KHZ_TO_MHZ(ar->freq_range.start_freq),
13821 		   KHZ_TO_MHZ(ar->freq_range.end_freq));
13822 }
13823 
13824 static void ath12k_mac_update_ch_list(struct ath12k *ar,
13825 				      struct ieee80211_supported_band *band,
13826 				      u32 freq_low, u32 freq_high)
13827 {
13828 	int i;
13829 
13830 	if (!(freq_low && freq_high))
13831 		return;
13832 
13833 	for (i = 0; i < band->n_channels; i++) {
13834 		if (band->channels[i].center_freq < freq_low ||
13835 		    band->channels[i].center_freq > freq_high)
13836 			band->channels[i].flags |= IEEE80211_CHAN_DISABLED;
13837 	}
13838 }
13839 
13840 static u32 ath12k_get_phy_id(struct ath12k *ar, u32 band)
13841 {
13842 	struct ath12k_pdev *pdev = ar->pdev;
13843 	struct ath12k_pdev_cap *pdev_cap = &pdev->cap;
13844 
13845 	if (band == WMI_HOST_WLAN_2GHZ_CAP)
13846 		return pdev_cap->band[NL80211_BAND_2GHZ].phy_id;
13847 
13848 	if (band == WMI_HOST_WLAN_5GHZ_CAP)
13849 		return pdev_cap->band[NL80211_BAND_5GHZ].phy_id;
13850 
13851 	ath12k_warn(ar->ab, "unsupported phy cap:%d\n", band);
13852 
13853 	return 0;
13854 }
13855 
13856 static int ath12k_mac_update_band(struct ath12k *ar,
13857 				  struct ieee80211_supported_band *orig_band,
13858 				  struct ieee80211_supported_band *new_band)
13859 {
13860 	int i;
13861 
13862 	if (!orig_band || !new_band)
13863 		return -EINVAL;
13864 
13865 	if (orig_band->band != new_band->band)
13866 		return -EINVAL;
13867 
13868 	for (i = 0; i < new_band->n_channels; i++) {
13869 		if (new_band->channels[i].flags & IEEE80211_CHAN_DISABLED)
13870 			continue;
13871 		/* An enabled channel in new_band should not be already enabled
13872 		 * in the orig_band
13873 		 */
13874 		if (WARN_ON(!(orig_band->channels[i].flags &
13875 			      IEEE80211_CHAN_DISABLED)))
13876 			return -EINVAL;
13877 		orig_band->channels[i].flags &= ~IEEE80211_CHAN_DISABLED;
13878 	}
13879 	return 0;
13880 }
13881 
13882 static int ath12k_mac_setup_channels_rates(struct ath12k *ar,
13883 					   u32 supported_bands,
13884 					   struct ieee80211_supported_band *bands[])
13885 {
13886 	struct ieee80211_supported_band *band;
13887 	struct ath12k_wmi_hal_reg_capabilities_ext_arg *reg_cap;
13888 	struct ath12k_base *ab = ar->ab;
13889 	u32 phy_id, freq_low, freq_high;
13890 	struct ath12k_hw *ah = ar->ah;
13891 	void *channels;
13892 	int ret;
13893 
13894 	BUILD_BUG_ON((ARRAY_SIZE(ath12k_2ghz_channels) +
13895 		      ARRAY_SIZE(ath12k_5ghz_channels) +
13896 		      ARRAY_SIZE(ath12k_6ghz_channels)) !=
13897 		     ATH12K_NUM_CHANS);
13898 
13899 	reg_cap = &ab->hal_reg_cap[ar->pdev_idx];
13900 
13901 	if (supported_bands & WMI_HOST_WLAN_2GHZ_CAP) {
13902 		channels = kmemdup(ath12k_2ghz_channels,
13903 				   sizeof(ath12k_2ghz_channels),
13904 				   GFP_KERNEL);
13905 		if (!channels)
13906 			return -ENOMEM;
13907 
13908 		band = &ar->mac.sbands[NL80211_BAND_2GHZ];
13909 		band->band = NL80211_BAND_2GHZ;
13910 		band->n_channels = ARRAY_SIZE(ath12k_2ghz_channels);
13911 		band->channels = channels;
13912 		band->n_bitrates = ath12k_g_rates_size;
13913 		band->bitrates = ath12k_g_rates;
13914 
13915 		if (ab->hw_params->single_pdev_only) {
13916 			phy_id = ath12k_get_phy_id(ar, WMI_HOST_WLAN_2GHZ_CAP);
13917 			reg_cap = &ab->hal_reg_cap[phy_id];
13918 		}
13919 
13920 		freq_low = max(reg_cap->low_2ghz_chan,
13921 			       ab->reg_freq_2ghz.start_freq);
13922 		freq_high = min(reg_cap->high_2ghz_chan,
13923 				ab->reg_freq_2ghz.end_freq);
13924 
13925 		ath12k_mac_update_ch_list(ar, band,
13926 					  reg_cap->low_2ghz_chan,
13927 					  reg_cap->high_2ghz_chan);
13928 
13929 		ath12k_mac_update_freq_range(ar, freq_low, freq_high);
13930 
13931 		if (!bands[NL80211_BAND_2GHZ]) {
13932 			bands[NL80211_BAND_2GHZ] = band;
13933 		} else {
13934 			/* Split mac in same band under same wiphy */
13935 			ret = ath12k_mac_update_band(ar, bands[NL80211_BAND_2GHZ], band);
13936 			if (ret) {
13937 				kfree(channels);
13938 				band->channels = NULL;
13939 				return ret;
13940 			}
13941 			ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac pdev %u identified as 2 GHz split mac with start freq %d end freq %d",
13942 				   ar->pdev->pdev_id,
13943 				   KHZ_TO_MHZ(ar->freq_range.start_freq),
13944 				   KHZ_TO_MHZ(ar->freq_range.end_freq));
13945 		}
13946 	}
13947 
13948 	if (supported_bands & WMI_HOST_WLAN_5GHZ_CAP) {
13949 		if (reg_cap->high_5ghz_chan >= ATH12K_MIN_6GHZ_FREQ) {
13950 			channels = kmemdup(ath12k_6ghz_channels,
13951 					   sizeof(ath12k_6ghz_channels), GFP_KERNEL);
13952 			if (!channels) {
13953 				kfree(ar->mac.sbands[NL80211_BAND_2GHZ].channels);
13954 				return -ENOMEM;
13955 			}
13956 
13957 			ar->supports_6ghz = true;
13958 			band = &ar->mac.sbands[NL80211_BAND_6GHZ];
13959 			band->band = NL80211_BAND_6GHZ;
13960 			band->n_channels = ARRAY_SIZE(ath12k_6ghz_channels);
13961 			band->channels = channels;
13962 			band->n_bitrates = ath12k_a_rates_size;
13963 			band->bitrates = ath12k_a_rates;
13964 
13965 			freq_low = max(reg_cap->low_5ghz_chan,
13966 				       ab->reg_freq_6ghz.start_freq);
13967 			freq_high = min(reg_cap->high_5ghz_chan,
13968 					ab->reg_freq_6ghz.end_freq);
13969 
13970 			ath12k_mac_update_ch_list(ar, band,
13971 						  reg_cap->low_5ghz_chan,
13972 						  reg_cap->high_5ghz_chan);
13973 
13974 			ath12k_mac_update_freq_range(ar, freq_low, freq_high);
13975 			ah->use_6ghz_regd = true;
13976 
13977 			if (!bands[NL80211_BAND_6GHZ]) {
13978 				bands[NL80211_BAND_6GHZ] = band;
13979 			} else {
13980 				/* Split mac in same band under same wiphy */
13981 				ret = ath12k_mac_update_band(ar,
13982 							     bands[NL80211_BAND_6GHZ],
13983 							     band);
13984 				if (ret) {
13985 					kfree(ar->mac.sbands[NL80211_BAND_2GHZ].channels);
13986 					ar->mac.sbands[NL80211_BAND_2GHZ].channels = NULL;
13987 					kfree(channels);
13988 					band->channels = NULL;
13989 					return ret;
13990 				}
13991 				ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac pdev %u identified as 6 GHz split mac with start freq %d end freq %d",
13992 					   ar->pdev->pdev_id,
13993 					   KHZ_TO_MHZ(ar->freq_range.start_freq),
13994 					   KHZ_TO_MHZ(ar->freq_range.end_freq));
13995 			}
13996 		}
13997 
13998 		if (reg_cap->low_5ghz_chan < ATH12K_MIN_6GHZ_FREQ) {
13999 			channels = kmemdup(ath12k_5ghz_channels,
14000 					   sizeof(ath12k_5ghz_channels),
14001 					   GFP_KERNEL);
14002 			if (!channels) {
14003 				kfree(ar->mac.sbands[NL80211_BAND_2GHZ].channels);
14004 				kfree(ar->mac.sbands[NL80211_BAND_6GHZ].channels);
14005 				return -ENOMEM;
14006 			}
14007 
14008 			band = &ar->mac.sbands[NL80211_BAND_5GHZ];
14009 			band->band = NL80211_BAND_5GHZ;
14010 			band->n_channels = ARRAY_SIZE(ath12k_5ghz_channels);
14011 			band->channels = channels;
14012 			band->n_bitrates = ath12k_a_rates_size;
14013 			band->bitrates = ath12k_a_rates;
14014 
14015 			if (ab->hw_params->single_pdev_only) {
14016 				phy_id = ath12k_get_phy_id(ar, WMI_HOST_WLAN_5GHZ_CAP);
14017 				reg_cap = &ab->hal_reg_cap[phy_id];
14018 			}
14019 
14020 			freq_low = max(reg_cap->low_5ghz_chan,
14021 				       ab->reg_freq_5ghz.start_freq);
14022 			freq_high = min(reg_cap->high_5ghz_chan,
14023 					ab->reg_freq_5ghz.end_freq);
14024 
14025 			ath12k_mac_update_ch_list(ar, band,
14026 						  reg_cap->low_5ghz_chan,
14027 						  reg_cap->high_5ghz_chan);
14028 
14029 			ath12k_mac_update_freq_range(ar, freq_low, freq_high);
14030 
14031 			if (!bands[NL80211_BAND_5GHZ]) {
14032 				bands[NL80211_BAND_5GHZ] = band;
14033 			} else {
14034 				/* Split mac in same band under same wiphy */
14035 				ret = ath12k_mac_update_band(ar,
14036 							     bands[NL80211_BAND_5GHZ],
14037 							     band);
14038 				if (ret) {
14039 					kfree(ar->mac.sbands[NL80211_BAND_2GHZ].channels);
14040 					ar->mac.sbands[NL80211_BAND_2GHZ].channels = NULL;
14041 					kfree(ar->mac.sbands[NL80211_BAND_6GHZ].channels);
14042 					ar->mac.sbands[NL80211_BAND_2GHZ].channels = NULL;
14043 					kfree(channels);
14044 					band->channels = NULL;
14045 					return ret;
14046 				}
14047 				ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac pdev %u identified as 5 GHz split mac with start freq %d end freq %d",
14048 					   ar->pdev->pdev_id,
14049 					   KHZ_TO_MHZ(ar->freq_range.start_freq),
14050 					   KHZ_TO_MHZ(ar->freq_range.end_freq));
14051 			}
14052 		}
14053 	}
14054 
14055 	return 0;
14056 }
14057 
14058 static u16 ath12k_mac_get_ifmodes(struct ath12k_hw *ah)
14059 {
14060 	struct ath12k *ar;
14061 	int i;
14062 	u16 interface_modes = U16_MAX;
14063 
14064 	for_each_ar(ah, ar, i)
14065 		interface_modes &= ar->ab->hw_params->interface_modes;
14066 
14067 	return interface_modes == U16_MAX ? 0 : interface_modes;
14068 }
14069 
14070 static bool ath12k_mac_is_iface_mode_enable(struct ath12k_hw *ah,
14071 					    enum nl80211_iftype type)
14072 {
14073 	struct ath12k *ar;
14074 	int i;
14075 	u16 interface_modes, mode = 0;
14076 	bool is_enable = false;
14077 
14078 	if (type == NL80211_IFTYPE_MESH_POINT) {
14079 		if (IS_ENABLED(CONFIG_MAC80211_MESH))
14080 			mode = BIT(type);
14081 	} else {
14082 		mode = BIT(type);
14083 	}
14084 
14085 	for_each_ar(ah, ar, i) {
14086 		interface_modes = ar->ab->hw_params->interface_modes;
14087 		if (interface_modes & mode) {
14088 			is_enable = true;
14089 			break;
14090 		}
14091 	}
14092 
14093 	return is_enable;
14094 }
14095 
14096 static int
14097 ath12k_mac_setup_radio_iface_comb(struct ath12k *ar,
14098 				  struct ieee80211_iface_combination *comb)
14099 {
14100 	u16 interface_modes = ar->ab->hw_params->interface_modes;
14101 	struct ieee80211_iface_limit *limits;
14102 	int n_limits, max_interfaces;
14103 	bool ap, mesh, p2p;
14104 
14105 	ap = interface_modes & BIT(NL80211_IFTYPE_AP);
14106 	p2p = interface_modes & BIT(NL80211_IFTYPE_P2P_DEVICE);
14107 
14108 	mesh = IS_ENABLED(CONFIG_MAC80211_MESH) &&
14109 	       (interface_modes & BIT(NL80211_IFTYPE_MESH_POINT));
14110 
14111 	if ((ap || mesh) && !p2p) {
14112 		n_limits = 2;
14113 		max_interfaces = 16;
14114 	} else if (p2p) {
14115 		n_limits = 3;
14116 		if (ap || mesh)
14117 			max_interfaces = 16;
14118 		else
14119 			max_interfaces = 3;
14120 	} else {
14121 		n_limits = 1;
14122 		max_interfaces = 1;
14123 	}
14124 
14125 	limits = kcalloc(n_limits, sizeof(*limits), GFP_KERNEL);
14126 	if (!limits)
14127 		return -ENOMEM;
14128 
14129 	limits[0].max = 1;
14130 	limits[0].types |= BIT(NL80211_IFTYPE_STATION);
14131 
14132 	if (ap || mesh || p2p)
14133 		limits[1].max = max_interfaces;
14134 
14135 	if (ap)
14136 		limits[1].types |= BIT(NL80211_IFTYPE_AP);
14137 
14138 	if (mesh)
14139 		limits[1].types |= BIT(NL80211_IFTYPE_MESH_POINT);
14140 
14141 	if (p2p) {
14142 		limits[1].types |= BIT(NL80211_IFTYPE_P2P_CLIENT) |
14143 					BIT(NL80211_IFTYPE_P2P_GO);
14144 		limits[2].max = 1;
14145 		limits[2].types |= BIT(NL80211_IFTYPE_P2P_DEVICE);
14146 	}
14147 
14148 	comb[0].limits = limits;
14149 	comb[0].n_limits = n_limits;
14150 	comb[0].max_interfaces = max_interfaces;
14151 	comb[0].beacon_int_infra_match = true;
14152 	comb[0].beacon_int_min_gcd = 100;
14153 
14154 	comb[0].num_different_channels = 1;
14155 	comb[0].radar_detect_widths = BIT(NL80211_CHAN_WIDTH_20_NOHT) |
14156 				      BIT(NL80211_CHAN_WIDTH_20) |
14157 				      BIT(NL80211_CHAN_WIDTH_40) |
14158 				      BIT(NL80211_CHAN_WIDTH_80) |
14159 				      BIT(NL80211_CHAN_WIDTH_160);
14160 
14161 	return 0;
14162 }
14163 
14164 static int
14165 ath12k_mac_setup_global_iface_comb(struct ath12k_hw *ah,
14166 				   struct wiphy_radio *radio,
14167 				   u8 n_radio,
14168 				   struct ieee80211_iface_combination *comb)
14169 {
14170 	const struct ieee80211_iface_combination *iter_comb;
14171 	struct ieee80211_iface_limit *limits;
14172 	int i, j, n_limits;
14173 	bool ap, mesh, p2p;
14174 
14175 	if (!n_radio)
14176 		return 0;
14177 
14178 	ap = ath12k_mac_is_iface_mode_enable(ah, NL80211_IFTYPE_AP);
14179 	p2p = ath12k_mac_is_iface_mode_enable(ah, NL80211_IFTYPE_P2P_DEVICE);
14180 	mesh = ath12k_mac_is_iface_mode_enable(ah, NL80211_IFTYPE_MESH_POINT);
14181 
14182 	if ((ap || mesh) && !p2p)
14183 		n_limits = 2;
14184 	else if (p2p)
14185 		n_limits = 3;
14186 	else
14187 		n_limits = 1;
14188 
14189 	limits = kcalloc(n_limits, sizeof(*limits), GFP_KERNEL);
14190 	if (!limits)
14191 		return -ENOMEM;
14192 
14193 	for (i = 0; i < n_radio; i++) {
14194 		iter_comb = radio[i].iface_combinations;
14195 		for (j = 0; j < iter_comb->n_limits && j < n_limits; j++) {
14196 			limits[j].types |= iter_comb->limits[j].types;
14197 			limits[j].max += iter_comb->limits[j].max;
14198 		}
14199 
14200 		comb->max_interfaces += iter_comb->max_interfaces;
14201 		comb->num_different_channels += iter_comb->num_different_channels;
14202 		comb->radar_detect_widths |= iter_comb->radar_detect_widths;
14203 	}
14204 
14205 	comb->limits = limits;
14206 	comb->n_limits = n_limits;
14207 	comb->beacon_int_infra_match = true;
14208 	comb->beacon_int_min_gcd = 100;
14209 
14210 	return 0;
14211 }
14212 
14213 static
14214 void ath12k_mac_cleanup_iface_comb(const struct ieee80211_iface_combination *iface_comb)
14215 {
14216 	kfree(iface_comb[0].limits);
14217 	kfree(iface_comb);
14218 }
14219 
14220 static void ath12k_mac_cleanup_iface_combinations(struct ath12k_hw *ah)
14221 {
14222 	struct wiphy *wiphy = ah->hw->wiphy;
14223 	const struct wiphy_radio *radio;
14224 	int i;
14225 
14226 	if (wiphy->n_radio > 0) {
14227 		radio = wiphy->radio;
14228 		for (i = 0; i < wiphy->n_radio; i++)
14229 			ath12k_mac_cleanup_iface_comb(radio[i].iface_combinations);
14230 
14231 		kfree(wiphy->radio);
14232 	}
14233 
14234 	ath12k_mac_cleanup_iface_comb(wiphy->iface_combinations);
14235 }
14236 
14237 static int ath12k_mac_setup_iface_combinations(struct ath12k_hw *ah)
14238 {
14239 	struct ieee80211_iface_combination *combinations, *comb;
14240 	struct wiphy *wiphy = ah->hw->wiphy;
14241 	struct wiphy_radio *radio;
14242 	int n_combinations = 1;
14243 	struct ath12k *ar;
14244 	int i, ret;
14245 
14246 	if (ah->num_radio == 1) {
14247 		ar = &ah->radio[0];
14248 
14249 		if (ar->ab->hw_params->single_pdev_only)
14250 			n_combinations = 2;
14251 
14252 		combinations = kcalloc(n_combinations, sizeof(*combinations),
14253 				       GFP_KERNEL);
14254 		if (!combinations)
14255 			return -ENOMEM;
14256 
14257 		ret = ath12k_mac_setup_radio_iface_comb(ar, combinations);
14258 		if (ret) {
14259 			ath12k_hw_warn(ah, "failed to setup radio interface combinations for one radio: %d",
14260 				       ret);
14261 			goto err_free_combinations;
14262 		}
14263 
14264 		if (ar->ab->hw_params->single_pdev_only) {
14265 			comb = combinations + 1;
14266 			memcpy(comb, combinations, sizeof(*comb));
14267 			comb->num_different_channels = 2;
14268 			comb->radar_detect_widths = 0;
14269 		}
14270 
14271 		goto out;
14272 	}
14273 
14274 	combinations = kcalloc(n_combinations, sizeof(*combinations), GFP_KERNEL);
14275 	if (!combinations)
14276 		return -ENOMEM;
14277 
14278 	/* there are multiple radios */
14279 
14280 	radio = kcalloc(ah->num_radio, sizeof(*radio), GFP_KERNEL);
14281 	if (!radio) {
14282 		ret = -ENOMEM;
14283 		goto err_free_combinations;
14284 	}
14285 
14286 	for_each_ar(ah, ar, i) {
14287 		comb = kzalloc(sizeof(*comb), GFP_KERNEL);
14288 		if (!comb) {
14289 			ret = -ENOMEM;
14290 			goto err_free_radios;
14291 		}
14292 
14293 		ret = ath12k_mac_setup_radio_iface_comb(ar, comb);
14294 		if (ret) {
14295 			ath12k_hw_warn(ah, "failed to setup radio interface combinations for radio %d: %d",
14296 				       i, ret);
14297 			kfree(comb);
14298 			goto err_free_radios;
14299 		}
14300 
14301 		radio[i].freq_range = &ar->freq_range;
14302 		radio[i].n_freq_range = 1;
14303 
14304 		radio[i].iface_combinations = comb;
14305 		radio[i].n_iface_combinations = 1;
14306 	}
14307 
14308 	ret = ath12k_mac_setup_global_iface_comb(ah, radio, ah->num_radio, combinations);
14309 	if (ret) {
14310 		ath12k_hw_warn(ah, "failed to setup global interface combinations: %d",
14311 			       ret);
14312 		goto err_free_all_radios;
14313 	}
14314 
14315 	wiphy->radio = radio;
14316 	wiphy->n_radio = ah->num_radio;
14317 
14318 out:
14319 	wiphy->iface_combinations = combinations;
14320 	wiphy->n_iface_combinations = n_combinations;
14321 
14322 	return 0;
14323 
14324 err_free_all_radios:
14325 	i = ah->num_radio;
14326 
14327 err_free_radios:
14328 	while (i--)
14329 		ath12k_mac_cleanup_iface_comb(radio[i].iface_combinations);
14330 
14331 	kfree(radio);
14332 
14333 err_free_combinations:
14334 	kfree(combinations);
14335 
14336 	return ret;
14337 }
14338 
14339 static const u8 ath12k_if_types_ext_capa[] = {
14340 	[0] = WLAN_EXT_CAPA1_EXT_CHANNEL_SWITCHING,
14341 	[2] = WLAN_EXT_CAPA3_MULTI_BSSID_SUPPORT,
14342 	[7] = WLAN_EXT_CAPA8_OPMODE_NOTIF,
14343 };
14344 
14345 static const u8 ath12k_if_types_ext_capa_sta[] = {
14346 	[0] = WLAN_EXT_CAPA1_EXT_CHANNEL_SWITCHING,
14347 	[2] = WLAN_EXT_CAPA3_MULTI_BSSID_SUPPORT,
14348 	[7] = WLAN_EXT_CAPA8_OPMODE_NOTIF,
14349 	[9] = WLAN_EXT_CAPA10_TWT_REQUESTER_SUPPORT,
14350 };
14351 
14352 static const u8 ath12k_if_types_ext_capa_ap[] = {
14353 	[0] = WLAN_EXT_CAPA1_EXT_CHANNEL_SWITCHING,
14354 	[2] = WLAN_EXT_CAPA3_MULTI_BSSID_SUPPORT,
14355 	[7] = WLAN_EXT_CAPA8_OPMODE_NOTIF,
14356 	[9] = WLAN_EXT_CAPA10_TWT_RESPONDER_SUPPORT,
14357 	[10] = WLAN_EXT_CAPA11_EMA_SUPPORT,
14358 };
14359 
14360 static struct wiphy_iftype_ext_capab ath12k_iftypes_ext_capa[] = {
14361 	{
14362 		.extended_capabilities = ath12k_if_types_ext_capa,
14363 		.extended_capabilities_mask = ath12k_if_types_ext_capa,
14364 		.extended_capabilities_len = sizeof(ath12k_if_types_ext_capa),
14365 	}, {
14366 		.iftype = NL80211_IFTYPE_STATION,
14367 		.extended_capabilities = ath12k_if_types_ext_capa_sta,
14368 		.extended_capabilities_mask = ath12k_if_types_ext_capa_sta,
14369 		.extended_capabilities_len =
14370 				sizeof(ath12k_if_types_ext_capa_sta),
14371 	}, {
14372 		.iftype = NL80211_IFTYPE_AP,
14373 		.extended_capabilities = ath12k_if_types_ext_capa_ap,
14374 		.extended_capabilities_mask = ath12k_if_types_ext_capa_ap,
14375 		.extended_capabilities_len =
14376 				sizeof(ath12k_if_types_ext_capa_ap),
14377 		.eml_capabilities = 0,
14378 		.mld_capa_and_ops = 0,
14379 	},
14380 };
14381 
14382 static void ath12k_mac_cleanup_unregister(struct ath12k *ar)
14383 {
14384 	idr_for_each(&ar->txmgmt_idr, ath12k_mac_tx_mgmt_pending_free, ar);
14385 	idr_destroy(&ar->txmgmt_idr);
14386 
14387 	kfree(ar->mac.sbands[NL80211_BAND_2GHZ].channels);
14388 	kfree(ar->mac.sbands[NL80211_BAND_5GHZ].channels);
14389 	kfree(ar->mac.sbands[NL80211_BAND_6GHZ].channels);
14390 }
14391 
14392 static void ath12k_mac_hw_unregister(struct ath12k_hw *ah)
14393 {
14394 	struct ieee80211_hw *hw = ah->hw;
14395 	struct ath12k *ar;
14396 	int i;
14397 
14398 	for_each_ar(ah, ar, i) {
14399 		cancel_work_sync(&ar->regd_channel_update_work);
14400 		cancel_work_sync(&ar->regd_update_work);
14401 		ath12k_debugfs_unregister(ar);
14402 		ath12k_fw_stats_reset(ar);
14403 	}
14404 
14405 	ieee80211_unregister_hw(hw);
14406 
14407 	for_each_ar(ah, ar, i)
14408 		ath12k_mac_cleanup_unregister(ar);
14409 
14410 	ath12k_mac_cleanup_iface_combinations(ah);
14411 
14412 	SET_IEEE80211_DEV(hw, NULL);
14413 }
14414 
14415 static int ath12k_mac_setup_register(struct ath12k *ar,
14416 				     u32 *ht_cap,
14417 				     struct ieee80211_supported_band *bands[])
14418 {
14419 	struct ath12k_pdev_cap *cap = &ar->pdev->cap;
14420 	int ret;
14421 
14422 	init_waitqueue_head(&ar->txmgmt_empty_waitq);
14423 	idr_init(&ar->txmgmt_idr);
14424 	spin_lock_init(&ar->txmgmt_idr_lock);
14425 
14426 	ath12k_pdev_caps_update(ar);
14427 
14428 	ret = ath12k_mac_setup_channels_rates(ar,
14429 					      cap->supported_bands,
14430 					      bands);
14431 	if (ret)
14432 		return ret;
14433 
14434 	ath12k_mac_setup_ht_vht_cap(ar, cap, ht_cap);
14435 	ath12k_mac_setup_sband_iftype_data(ar, cap);
14436 
14437 	ar->max_num_stations = ath12k_core_get_max_station_per_radio(ar->ab);
14438 	ar->max_num_peers = ath12k_core_get_max_peers_per_radio(ar->ab);
14439 
14440 	ar->rssi_info.min_nf_dbm = ATH12K_DEFAULT_NOISE_FLOOR;
14441 	ar->rssi_info.temp_offset = 0;
14442 	ar->rssi_info.noise_floor = ar->rssi_info.min_nf_dbm + ar->rssi_info.temp_offset;
14443 
14444 	return 0;
14445 }
14446 
14447 static int ath12k_mac_hw_register(struct ath12k_hw *ah)
14448 {
14449 	struct ieee80211_hw *hw = ah->hw;
14450 	struct wiphy *wiphy = hw->wiphy;
14451 	struct ath12k *ar = ath12k_ah_to_ar(ah, 0);
14452 	struct ath12k_base *ab = ar->ab;
14453 	struct ath12k_pdev *pdev;
14454 	struct ath12k_pdev_cap *cap;
14455 	static const u32 cipher_suites[] = {
14456 		WLAN_CIPHER_SUITE_TKIP,
14457 		WLAN_CIPHER_SUITE_CCMP,
14458 		WLAN_CIPHER_SUITE_AES_CMAC,
14459 		WLAN_CIPHER_SUITE_BIP_CMAC_256,
14460 		WLAN_CIPHER_SUITE_BIP_GMAC_128,
14461 		WLAN_CIPHER_SUITE_BIP_GMAC_256,
14462 		WLAN_CIPHER_SUITE_GCMP,
14463 		WLAN_CIPHER_SUITE_GCMP_256,
14464 		WLAN_CIPHER_SUITE_CCMP_256,
14465 	};
14466 	int ret, i, j;
14467 	u32 ht_cap = U32_MAX, antennas_rx = 0, antennas_tx = 0;
14468 	bool is_6ghz = false, is_raw_mode = false, is_monitor_disable = false;
14469 	u8 *mac_addr = NULL;
14470 	u8 mbssid_max_interfaces = 0;
14471 
14472 	wiphy->max_ap_assoc_sta = 0;
14473 
14474 	for_each_ar(ah, ar, i) {
14475 		u32 ht_cap_info = 0;
14476 
14477 		pdev = ar->pdev;
14478 		if (ar->ab->pdevs_macaddr_valid) {
14479 			ether_addr_copy(ar->mac_addr, pdev->mac_addr);
14480 		} else {
14481 			ether_addr_copy(ar->mac_addr, ar->ab->mac_addr);
14482 			ar->mac_addr[4] += ar->pdev_idx;
14483 		}
14484 
14485 		ret = ath12k_mac_setup_register(ar, &ht_cap_info, hw->wiphy->bands);
14486 		if (ret)
14487 			goto err_cleanup_unregister;
14488 
14489 		/* 6 GHz does not support HT Cap, hence do not consider it */
14490 		if (!ar->supports_6ghz)
14491 			ht_cap &= ht_cap_info;
14492 
14493 		wiphy->max_ap_assoc_sta += ar->max_num_stations;
14494 
14495 		/* Advertise the max antenna support of all radios, driver can handle
14496 		 * per pdev specific antenna setting based on pdev cap when antenna
14497 		 * changes are made
14498 		 */
14499 		cap = &pdev->cap;
14500 
14501 		antennas_rx = max_t(u32, antennas_rx, cap->rx_chain_mask);
14502 		antennas_tx = max_t(u32, antennas_tx, cap->tx_chain_mask);
14503 
14504 		if (ar->supports_6ghz)
14505 			is_6ghz = true;
14506 
14507 		if (test_bit(ATH12K_FLAG_RAW_MODE, &ar->ab->dev_flags))
14508 			is_raw_mode = true;
14509 
14510 		if (!ar->ab->hw_params->supports_monitor)
14511 			is_monitor_disable = true;
14512 
14513 		if (i == 0)
14514 			mac_addr = ar->mac_addr;
14515 		else
14516 			mac_addr = ab->mac_addr;
14517 
14518 		mbssid_max_interfaces += TARGET_NUM_VDEVS(ar->ab);
14519 	}
14520 
14521 	wiphy->available_antennas_rx = antennas_rx;
14522 	wiphy->available_antennas_tx = antennas_tx;
14523 
14524 	SET_IEEE80211_PERM_ADDR(hw, mac_addr);
14525 	SET_IEEE80211_DEV(hw, ab->dev);
14526 
14527 	ret = ath12k_mac_setup_iface_combinations(ah);
14528 	if (ret) {
14529 		ath12k_err(ab, "failed to setup interface combinations: %d\n", ret);
14530 		goto err_complete_cleanup_unregister;
14531 	}
14532 
14533 	wiphy->interface_modes = ath12k_mac_get_ifmodes(ah);
14534 
14535 	if (ah->num_radio == 1 &&
14536 	    wiphy->bands[NL80211_BAND_2GHZ] &&
14537 	    wiphy->bands[NL80211_BAND_5GHZ] &&
14538 	    wiphy->bands[NL80211_BAND_6GHZ])
14539 		ieee80211_hw_set(hw, SINGLE_SCAN_ON_ALL_BANDS);
14540 
14541 	ieee80211_hw_set(hw, SIGNAL_DBM);
14542 	ieee80211_hw_set(hw, SUPPORTS_PS);
14543 	ieee80211_hw_set(hw, SUPPORTS_DYNAMIC_PS);
14544 	ieee80211_hw_set(hw, MFP_CAPABLE);
14545 	ieee80211_hw_set(hw, REPORTS_TX_ACK_STATUS);
14546 	ieee80211_hw_set(hw, HAS_RATE_CONTROL);
14547 	ieee80211_hw_set(hw, AP_LINK_PS);
14548 	ieee80211_hw_set(hw, SPECTRUM_MGMT);
14549 	ieee80211_hw_set(hw, CONNECTION_MONITOR);
14550 	ieee80211_hw_set(hw, SUPPORTS_PER_STA_GTK);
14551 	ieee80211_hw_set(hw, CHANCTX_STA_CSA);
14552 	ieee80211_hw_set(hw, QUEUE_CONTROL);
14553 	ieee80211_hw_set(hw, SUPPORTS_TX_FRAG);
14554 	ieee80211_hw_set(hw, REPORTS_LOW_ACK);
14555 	ieee80211_hw_set(hw, NO_VIRTUAL_MONITOR);
14556 
14557 	if (test_bit(WMI_TLV_SERVICE_ETH_OFFLOAD, ar->wmi->wmi_ab->svc_map)) {
14558 		ieee80211_hw_set(hw, SUPPORTS_TX_ENCAP_OFFLOAD);
14559 		ieee80211_hw_set(hw, SUPPORTS_RX_DECAP_OFFLOAD);
14560 	}
14561 
14562 	if (cap->nss_ratio_enabled)
14563 		ieee80211_hw_set(hw, SUPPORTS_VHT_EXT_NSS_BW);
14564 
14565 	if ((ht_cap & WMI_HT_CAP_ENABLED) || is_6ghz) {
14566 		ieee80211_hw_set(hw, AMPDU_AGGREGATION);
14567 		ieee80211_hw_set(hw, TX_AMPDU_SETUP_IN_HW);
14568 		ieee80211_hw_set(hw, SUPPORTS_REORDERING_BUFFER);
14569 		ieee80211_hw_set(hw, SUPPORTS_AMSDU_IN_AMPDU);
14570 		ieee80211_hw_set(hw, USES_RSS);
14571 	}
14572 
14573 	wiphy->features |= NL80211_FEATURE_STATIC_SMPS;
14574 	wiphy->flags |= WIPHY_FLAG_IBSS_RSN;
14575 
14576 	/* TODO: Check if HT capability advertised from firmware is different
14577 	 * for each band for a dual band capable radio. It will be tricky to
14578 	 * handle it when the ht capability different for each band.
14579 	 */
14580 	if (ht_cap & WMI_HT_CAP_DYNAMIC_SMPS ||
14581 	    (is_6ghz && ab->hw_params->supports_dynamic_smps_6ghz))
14582 		wiphy->features |= NL80211_FEATURE_DYNAMIC_SMPS;
14583 
14584 	wiphy->max_scan_ssids = WLAN_SCAN_PARAMS_MAX_SSID;
14585 	wiphy->max_scan_ie_len = WLAN_SCAN_PARAMS_MAX_IE_LEN;
14586 
14587 	hw->max_listen_interval = ATH12K_MAX_HW_LISTEN_INTERVAL;
14588 
14589 	wiphy->flags |= WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL;
14590 	wiphy->flags |= WIPHY_FLAG_HAS_CHANNEL_SWITCH;
14591 	wiphy->max_remain_on_channel_duration = 5000;
14592 
14593 	wiphy->flags |= WIPHY_FLAG_AP_UAPSD;
14594 	wiphy->features |= NL80211_FEATURE_AP_MODE_CHAN_WIDTH_CHANGE |
14595 				   NL80211_FEATURE_AP_SCAN;
14596 
14597 	wiphy->features |= NL80211_FEATURE_TX_POWER_INSERTION;
14598 
14599 	/* MLO is not yet supported so disable Wireless Extensions for now
14600 	 * to make sure ath12k users don't use it. This flag can be removed
14601 	 * once WIPHY_FLAG_SUPPORTS_MLO is enabled.
14602 	 */
14603 	wiphy->flags |= WIPHY_FLAG_DISABLE_WEXT;
14604 
14605 	/* Copy over MLO related capabilities received from
14606 	 * WMI_SERVICE_READY_EXT2_EVENT if single_chip_mlo_supp is set.
14607 	 */
14608 	if (ab->ag->mlo_capable) {
14609 		ath12k_iftypes_ext_capa[2].eml_capabilities = cap->eml_cap;
14610 		ath12k_iftypes_ext_capa[2].mld_capa_and_ops = cap->mld_cap;
14611 		wiphy->flags |= WIPHY_FLAG_SUPPORTS_MLO;
14612 
14613 		ieee80211_hw_set(hw, MLO_MCAST_MULTI_LINK_TX);
14614 	}
14615 
14616 	hw->queues = ATH12K_HW_MAX_QUEUES;
14617 	wiphy->tx_queue_len = ATH12K_QUEUE_LEN;
14618 	hw->offchannel_tx_hw_queue = ATH12K_HW_MAX_QUEUES - 1;
14619 	hw->max_rx_aggregation_subframes = IEEE80211_MAX_AMPDU_BUF_EHT;
14620 
14621 	hw->vif_data_size = sizeof(struct ath12k_vif);
14622 	hw->sta_data_size = sizeof(struct ath12k_sta);
14623 	hw->extra_tx_headroom = ab->hw_params->iova_mask;
14624 
14625 	wiphy_ext_feature_set(wiphy, NL80211_EXT_FEATURE_CQM_RSSI_LIST);
14626 	wiphy_ext_feature_set(wiphy, NL80211_EXT_FEATURE_STA_TX_PWR);
14627 	wiphy_ext_feature_set(wiphy, NL80211_EXT_FEATURE_ACK_SIGNAL_SUPPORT);
14628 	if (test_bit(WMI_TLV_SERVICE_BSS_COLOR_OFFLOAD,
14629 		     ab->wmi_ab.svc_map)) {
14630 		wiphy_ext_feature_set(wiphy, NL80211_EXT_FEATURE_BSS_COLOR);
14631 		ieee80211_hw_set(hw, DETECTS_COLOR_COLLISION);
14632 	}
14633 
14634 	wiphy->cipher_suites = cipher_suites;
14635 	wiphy->n_cipher_suites = ARRAY_SIZE(cipher_suites);
14636 
14637 	wiphy->iftype_ext_capab = ath12k_iftypes_ext_capa;
14638 	wiphy->num_iftype_ext_capab = ARRAY_SIZE(ath12k_iftypes_ext_capa);
14639 
14640 	wiphy->mbssid_max_interfaces = mbssid_max_interfaces;
14641 	wiphy->ema_max_profile_periodicity = TARGET_EMA_MAX_PROFILE_PERIOD;
14642 	ieee80211_hw_set(hw, SUPPORTS_MULTI_BSSID);
14643 
14644 	if (is_6ghz) {
14645 		wiphy_ext_feature_set(wiphy,
14646 				      NL80211_EXT_FEATURE_FILS_DISCOVERY);
14647 		wiphy_ext_feature_set(wiphy,
14648 				      NL80211_EXT_FEATURE_UNSOL_BCAST_PROBE_RESP);
14649 	}
14650 
14651 	wiphy_ext_feature_set(wiphy, NL80211_EXT_FEATURE_PUNCT);
14652 	if (test_bit(WMI_TLV_SERVICE_BEACON_PROTECTION_SUPPORT, ab->wmi_ab.svc_map))
14653 		wiphy_ext_feature_set(hw->wiphy, NL80211_EXT_FEATURE_BEACON_PROTECTION);
14654 
14655 	ath12k_reg_init(hw);
14656 
14657 	if (!is_raw_mode) {
14658 		hw->netdev_features = NETIF_F_HW_CSUM;
14659 		ieee80211_hw_set(hw, SW_CRYPTO_CONTROL);
14660 		ieee80211_hw_set(hw, SUPPORT_FAST_XMIT);
14661 	}
14662 
14663 	if (test_bit(WMI_TLV_SERVICE_NLO, ar->wmi->wmi_ab->svc_map)) {
14664 		wiphy->max_sched_scan_ssids = WMI_PNO_MAX_SUPP_NETWORKS;
14665 		wiphy->max_match_sets = WMI_PNO_MAX_SUPP_NETWORKS;
14666 		wiphy->max_sched_scan_ie_len = WMI_PNO_MAX_IE_LENGTH;
14667 		wiphy->max_sched_scan_plans = WMI_PNO_MAX_SCHED_SCAN_PLANS;
14668 		wiphy->max_sched_scan_plan_interval =
14669 					WMI_PNO_MAX_SCHED_SCAN_PLAN_INT;
14670 		wiphy->max_sched_scan_plan_iterations =
14671 					WMI_PNO_MAX_SCHED_SCAN_PLAN_ITRNS;
14672 		wiphy->features |= NL80211_FEATURE_ND_RANDOM_MAC_ADDR;
14673 	}
14674 
14675 	ret = ath12k_wow_init(ar);
14676 	if (ret) {
14677 		ath12k_warn(ar->ab, "failed to init wow: %d\n", ret);
14678 		goto err_cleanup_if_combs;
14679 	}
14680 
14681 	/* Boot-time regulatory updates have already been processed.
14682 	 * Mark them as complete now, because after registration,
14683 	 * cfg80211 will notify us again if there are any pending hints.
14684 	 * We need to wait for those hints to be processed, so it's
14685 	 * important to mark the boot-time updates as complete before
14686 	 * proceeding with registration.
14687 	 */
14688 	for_each_ar(ah, ar, i)
14689 		complete_all(&ar->regd_update_completed);
14690 
14691 	ret = ieee80211_register_hw(hw);
14692 	if (ret) {
14693 		ath12k_err(ab, "ieee80211 registration failed: %d\n", ret);
14694 		goto err_cleanup_if_combs;
14695 	}
14696 
14697 	if (is_monitor_disable)
14698 		/* There's a race between calling ieee80211_register_hw()
14699 		 * and here where the monitor mode is enabled for a little
14700 		 * while. But that time is so short and in practice it doesn't make
14701 		 * a difference in real life.
14702 		 */
14703 		wiphy->interface_modes &= ~BIT(NL80211_IFTYPE_MONITOR);
14704 
14705 	for_each_ar(ah, ar, i) {
14706 		/* Apply the regd received during initialization */
14707 		ret = ath12k_regd_update(ar, true);
14708 		if (ret) {
14709 			ath12k_err(ar->ab, "ath12k regd update failed: %d\n", ret);
14710 			goto err_unregister_hw;
14711 		}
14712 
14713 		if (ar->ab->hw_params->current_cc_support && ab->new_alpha2[0]) {
14714 			struct wmi_set_current_country_arg current_cc = {};
14715 
14716 			memcpy(&current_cc.alpha2, ab->new_alpha2, 2);
14717 			memcpy(&ar->alpha2, ab->new_alpha2, 2);
14718 
14719 			reinit_completion(&ar->regd_update_completed);
14720 
14721 			ret = ath12k_wmi_send_set_current_country_cmd(ar, &current_cc);
14722 			if (ret)
14723 				ath12k_warn(ar->ab,
14724 					    "failed set cc code for mac register: %d\n",
14725 					    ret);
14726 		}
14727 
14728 		ath12k_fw_stats_init(ar);
14729 		ath12k_debugfs_register(ar);
14730 	}
14731 
14732 	return 0;
14733 
14734 err_unregister_hw:
14735 	for_each_ar(ah, ar, i)
14736 		ath12k_debugfs_unregister(ar);
14737 
14738 	ieee80211_unregister_hw(hw);
14739 
14740 err_cleanup_if_combs:
14741 	ath12k_mac_cleanup_iface_combinations(ah);
14742 
14743 err_complete_cleanup_unregister:
14744 	i = ah->num_radio;
14745 
14746 err_cleanup_unregister:
14747 	for (j = 0; j < i; j++) {
14748 		ar = ath12k_ah_to_ar(ah, j);
14749 		ath12k_mac_cleanup_unregister(ar);
14750 	}
14751 
14752 	SET_IEEE80211_DEV(hw, NULL);
14753 
14754 	return ret;
14755 }
14756 
14757 static void ath12k_mac_setup(struct ath12k *ar)
14758 {
14759 	struct ath12k_base *ab = ar->ab;
14760 	struct ath12k_pdev *pdev = ar->pdev;
14761 	u8 pdev_idx = ar->pdev_idx;
14762 
14763 	ar->lmac_id = ath12k_hw_get_mac_from_pdev_id(ab->hw_params, pdev_idx);
14764 
14765 	ar->wmi = &ab->wmi_ab.wmi[pdev_idx];
14766 	/* FIXME: wmi[0] is already initialized during attach,
14767 	 * Should we do this again?
14768 	 */
14769 	ath12k_wmi_pdev_attach(ab, pdev_idx);
14770 
14771 	ar->cfg_tx_chainmask = pdev->cap.tx_chain_mask;
14772 	ar->cfg_rx_chainmask = pdev->cap.rx_chain_mask;
14773 	ar->num_tx_chains = hweight32(pdev->cap.tx_chain_mask);
14774 	ar->num_rx_chains = hweight32(pdev->cap.rx_chain_mask);
14775 	ar->scan.arvif = NULL;
14776 	ar->vdev_id_11d_scan = ATH12K_11D_INVALID_VDEV_ID;
14777 
14778 	spin_lock_init(&ar->data_lock);
14779 	spin_lock_init(&ar->dp.ppdu_list_lock);
14780 	INIT_LIST_HEAD(&ar->arvifs);
14781 	INIT_LIST_HEAD(&ar->dp.ppdu_stats_info);
14782 
14783 	init_completion(&ar->vdev_setup_done);
14784 	init_completion(&ar->vdev_delete_done);
14785 	init_completion(&ar->peer_assoc_done);
14786 	init_completion(&ar->peer_delete_done);
14787 	init_completion(&ar->install_key_done);
14788 	init_completion(&ar->bss_survey_done);
14789 	init_completion(&ar->scan.started);
14790 	init_completion(&ar->scan.completed);
14791 	init_completion(&ar->scan.on_channel);
14792 	init_completion(&ar->mlo_setup_done);
14793 	init_completion(&ar->completed_11d_scan);
14794 	init_completion(&ar->regd_update_completed);
14795 
14796 	INIT_DELAYED_WORK(&ar->scan.timeout, ath12k_scan_timeout_work);
14797 	wiphy_work_init(&ar->scan.vdev_clean_wk, ath12k_scan_vdev_clean_work);
14798 	INIT_WORK(&ar->regd_channel_update_work, ath12k_regd_update_chan_list_work);
14799 	INIT_LIST_HEAD(&ar->regd_channel_update_queue);
14800 	INIT_WORK(&ar->regd_update_work, ath12k_regd_update_work);
14801 
14802 	wiphy_work_init(&ar->wmi_mgmt_tx_work, ath12k_mgmt_over_wmi_tx_work);
14803 	skb_queue_head_init(&ar->wmi_mgmt_tx_queue);
14804 
14805 	ar->monitor_vdev_id = -1;
14806 	ar->monitor_vdev_created = false;
14807 	ar->monitor_started = false;
14808 }
14809 
14810 static int __ath12k_mac_mlo_setup(struct ath12k *ar)
14811 {
14812 	u8 num_link = 0, partner_link_id[ATH12K_GROUP_MAX_RADIO] = {};
14813 	struct ath12k_base *partner_ab, *ab = ar->ab;
14814 	struct ath12k_hw_group *ag = ab->ag;
14815 	struct wmi_mlo_setup_arg mlo = {};
14816 	struct ath12k_pdev *pdev;
14817 	unsigned long time_left;
14818 	int i, j, ret;
14819 
14820 	lockdep_assert_held(&ag->mutex);
14821 
14822 	reinit_completion(&ar->mlo_setup_done);
14823 
14824 	for (i = 0; i < ag->num_devices; i++) {
14825 		partner_ab = ag->ab[i];
14826 
14827 		for (j = 0; j < partner_ab->num_radios; j++) {
14828 			pdev = &partner_ab->pdevs[j];
14829 
14830 			/* Avoid the self link */
14831 			if (ar == pdev->ar)
14832 				continue;
14833 
14834 			partner_link_id[num_link] = pdev->hw_link_id;
14835 			num_link++;
14836 
14837 			ath12k_dbg(ab, ATH12K_DBG_MAC, "device %d pdev %d hw_link_id %d num_link %d\n",
14838 				   i, j, pdev->hw_link_id, num_link);
14839 		}
14840 	}
14841 
14842 	if (num_link == 0)
14843 		return 0;
14844 
14845 	mlo.group_id = cpu_to_le32(ag->id);
14846 	mlo.partner_link_id = partner_link_id;
14847 	mlo.num_partner_links = num_link;
14848 	ar->mlo_setup_status = 0;
14849 
14850 	ath12k_dbg(ab, ATH12K_DBG_MAC, "group id %d num_link %d\n", ag->id, num_link);
14851 
14852 	ret = ath12k_wmi_mlo_setup(ar, &mlo);
14853 	if (ret) {
14854 		ath12k_err(ab, "failed to send  setup MLO WMI command for pdev %d: %d\n",
14855 			   ar->pdev_idx, ret);
14856 		return ret;
14857 	}
14858 
14859 	time_left = wait_for_completion_timeout(&ar->mlo_setup_done,
14860 						WMI_MLO_CMD_TIMEOUT_HZ);
14861 
14862 	if (!time_left || ar->mlo_setup_status)
14863 		return ar->mlo_setup_status ? : -ETIMEDOUT;
14864 
14865 	ath12k_dbg(ab, ATH12K_DBG_MAC, "mlo setup done for pdev %d\n", ar->pdev_idx);
14866 
14867 	return 0;
14868 }
14869 
14870 static int __ath12k_mac_mlo_teardown(struct ath12k *ar)
14871 {
14872 	struct ath12k_base *ab = ar->ab;
14873 	int ret;
14874 	u8 num_link;
14875 
14876 	if (test_bit(ATH12K_FLAG_RECOVERY, &ab->dev_flags))
14877 		return 0;
14878 
14879 	num_link = ath12k_get_num_partner_link(ar);
14880 
14881 	if (num_link == 0)
14882 		return 0;
14883 
14884 	ret = ath12k_wmi_mlo_teardown(ar);
14885 	if (ret) {
14886 		ath12k_warn(ab, "failed to send MLO teardown WMI command for pdev %d: %d\n",
14887 			    ar->pdev_idx, ret);
14888 		return ret;
14889 	}
14890 
14891 	ath12k_dbg(ab, ATH12K_DBG_MAC, "mlo teardown for pdev %d\n", ar->pdev_idx);
14892 
14893 	return 0;
14894 }
14895 
14896 int ath12k_mac_mlo_setup(struct ath12k_hw_group *ag)
14897 {
14898 	struct ath12k_hw *ah;
14899 	struct ath12k *ar;
14900 	int ret;
14901 	int i, j;
14902 
14903 	for (i = 0; i < ag->num_hw; i++) {
14904 		ah = ag->ah[i];
14905 		if (!ah)
14906 			continue;
14907 
14908 		for_each_ar(ah, ar, j) {
14909 			ar = &ah->radio[j];
14910 			ret = __ath12k_mac_mlo_setup(ar);
14911 			if (ret) {
14912 				ath12k_err(ar->ab, "failed to setup MLO: %d\n", ret);
14913 				goto err_setup;
14914 			}
14915 		}
14916 	}
14917 
14918 	return 0;
14919 
14920 err_setup:
14921 	for (i = i - 1; i >= 0; i--) {
14922 		ah = ag->ah[i];
14923 		if (!ah)
14924 			continue;
14925 
14926 		for (j = j - 1; j >= 0; j--) {
14927 			ar = &ah->radio[j];
14928 			if (!ar)
14929 				continue;
14930 
14931 			__ath12k_mac_mlo_teardown(ar);
14932 		}
14933 	}
14934 
14935 	return ret;
14936 }
14937 
14938 void ath12k_mac_mlo_teardown(struct ath12k_hw_group *ag)
14939 {
14940 	struct ath12k_hw *ah;
14941 	struct ath12k *ar;
14942 	int ret, i, j;
14943 
14944 	for (i = 0; i < ag->num_hw; i++) {
14945 		ah = ag->ah[i];
14946 		if (!ah)
14947 			continue;
14948 
14949 		for_each_ar(ah, ar, j) {
14950 			ar = &ah->radio[j];
14951 			ret = __ath12k_mac_mlo_teardown(ar);
14952 			if (ret) {
14953 				ath12k_err(ar->ab, "failed to teardown MLO: %d\n", ret);
14954 				break;
14955 			}
14956 		}
14957 	}
14958 }
14959 
14960 int ath12k_mac_register(struct ath12k_hw_group *ag)
14961 {
14962 	struct ath12k_hw *ah;
14963 	int i;
14964 	int ret;
14965 
14966 	for (i = 0; i < ag->num_hw; i++) {
14967 		ah = ath12k_ag_to_ah(ag, i);
14968 
14969 		ret = ath12k_mac_hw_register(ah);
14970 		if (ret)
14971 			goto err;
14972 	}
14973 
14974 	return 0;
14975 
14976 err:
14977 	for (i = i - 1; i >= 0; i--) {
14978 		ah = ath12k_ag_to_ah(ag, i);
14979 		if (!ah)
14980 			continue;
14981 
14982 		ath12k_mac_hw_unregister(ah);
14983 	}
14984 
14985 	return ret;
14986 }
14987 
14988 void ath12k_mac_unregister(struct ath12k_hw_group *ag)
14989 {
14990 	struct ath12k_hw *ah;
14991 	int i;
14992 
14993 	for (i = ag->num_hw - 1; i >= 0; i--) {
14994 		ah = ath12k_ag_to_ah(ag, i);
14995 		if (!ah)
14996 			continue;
14997 
14998 		ath12k_mac_hw_unregister(ah);
14999 	}
15000 }
15001 
15002 static void ath12k_mac_hw_destroy(struct ath12k_hw *ah)
15003 {
15004 	ieee80211_free_hw(ah->hw);
15005 }
15006 
15007 static struct ath12k_hw *ath12k_mac_hw_allocate(struct ath12k_hw_group *ag,
15008 						struct ath12k_pdev_map *pdev_map,
15009 						u8 num_pdev_map)
15010 {
15011 	struct ieee80211_hw *hw;
15012 	struct ath12k *ar;
15013 	struct ath12k_base *ab;
15014 	struct ath12k_pdev *pdev;
15015 	struct ath12k_hw *ah;
15016 	int i;
15017 	u8 pdev_idx;
15018 
15019 	hw = ieee80211_alloc_hw(struct_size(ah, radio, num_pdev_map),
15020 				pdev_map->ab->ath12k_ops);
15021 	if (!hw)
15022 		return NULL;
15023 
15024 	ah = ath12k_hw_to_ah(hw);
15025 	ah->hw = hw;
15026 	ah->num_radio = num_pdev_map;
15027 
15028 	mutex_init(&ah->hw_mutex);
15029 
15030 	spin_lock_init(&ah->dp_hw.peer_lock);
15031 	INIT_LIST_HEAD(&ah->dp_hw.dp_peers_list);
15032 
15033 	for (i = 0; i < num_pdev_map; i++) {
15034 		ab = pdev_map[i].ab;
15035 		pdev_idx = pdev_map[i].pdev_idx;
15036 		pdev = &ab->pdevs[pdev_idx];
15037 
15038 		ar = ath12k_ah_to_ar(ah, i);
15039 		ar->ah = ah;
15040 		ar->ab = ab;
15041 		ar->hw_link_id = pdev->hw_link_id;
15042 		ar->pdev = pdev;
15043 		ar->pdev_idx = pdev_idx;
15044 		pdev->ar = ar;
15045 
15046 		ag->hw_links[ar->hw_link_id].device_id = ab->device_id;
15047 		ag->hw_links[ar->hw_link_id].pdev_idx = pdev_idx;
15048 
15049 		ath12k_mac_setup(ar);
15050 		ath12k_dp_pdev_pre_alloc(ar);
15051 	}
15052 
15053 	return ah;
15054 }
15055 
15056 void ath12k_mac_destroy(struct ath12k_hw_group *ag)
15057 {
15058 	struct ath12k_pdev *pdev;
15059 	struct ath12k_base *ab = ag->ab[0];
15060 	int i, j;
15061 	struct ath12k_hw *ah;
15062 
15063 	for (i = 0; i < ag->num_devices; i++) {
15064 		ab = ag->ab[i];
15065 		if (!ab)
15066 			continue;
15067 
15068 		for (j = 0; j < ab->num_radios; j++) {
15069 			pdev = &ab->pdevs[j];
15070 			if (!pdev->ar)
15071 				continue;
15072 			pdev->ar = NULL;
15073 		}
15074 	}
15075 
15076 	for (i = 0; i < ag->num_hw; i++) {
15077 		ah = ath12k_ag_to_ah(ag, i);
15078 		if (!ah)
15079 			continue;
15080 
15081 		ath12k_mac_hw_destroy(ah);
15082 		ath12k_ag_set_ah(ag, i, NULL);
15083 	}
15084 }
15085 
15086 static void ath12k_mac_set_device_defaults(struct ath12k_base *ab)
15087 {
15088 	int total_vdev;
15089 
15090 	/* Initialize channel counters frequency value in hertz */
15091 	ab->cc_freq_hz = 320000;
15092 	total_vdev = ab->num_radios * TARGET_NUM_VDEVS(ab);
15093 	ab->free_vdev_map = (1LL << total_vdev) - 1;
15094 }
15095 
15096 int ath12k_mac_allocate(struct ath12k_hw_group *ag)
15097 {
15098 	struct ath12k_pdev_map pdev_map[ATH12K_GROUP_MAX_RADIO];
15099 	int mac_id, device_id, total_radio, num_hw;
15100 	struct ath12k_base *ab;
15101 	struct ath12k_hw *ah;
15102 	int ret, i, j;
15103 	u8 radio_per_hw;
15104 
15105 	total_radio = 0;
15106 	for (i = 0; i < ag->num_devices; i++) {
15107 		ab = ag->ab[i];
15108 		if (!ab)
15109 			continue;
15110 
15111 		ath12k_debugfs_pdev_create(ab);
15112 		ath12k_mac_set_device_defaults(ab);
15113 		total_radio += ab->num_radios;
15114 	}
15115 
15116 	if (!total_radio)
15117 		return -EINVAL;
15118 
15119 	if (WARN_ON(total_radio > ATH12K_GROUP_MAX_RADIO))
15120 		return -ENOSPC;
15121 
15122 	/* All pdev get combined and register as single wiphy based on
15123 	 * hardware group which participate in multi-link operation else
15124 	 * each pdev get register separately.
15125 	 */
15126 	if (ag->mlo_capable)
15127 		radio_per_hw = total_radio;
15128 	else
15129 		radio_per_hw = 1;
15130 
15131 	num_hw = total_radio / radio_per_hw;
15132 
15133 	ag->num_hw = 0;
15134 	device_id = 0;
15135 	mac_id = 0;
15136 	for (i = 0; i < num_hw; i++) {
15137 		for (j = 0; j < radio_per_hw; j++) {
15138 			if (device_id >= ag->num_devices || !ag->ab[device_id]) {
15139 				ret = -ENOSPC;
15140 				goto err;
15141 			}
15142 
15143 			ab = ag->ab[device_id];
15144 			pdev_map[j].ab = ab;
15145 			pdev_map[j].pdev_idx = mac_id;
15146 			mac_id++;
15147 
15148 			/* If mac_id falls beyond the current device MACs then
15149 			 * move to next device
15150 			 */
15151 			if (mac_id >= ab->num_radios) {
15152 				mac_id = 0;
15153 				device_id++;
15154 			}
15155 		}
15156 
15157 		ab = pdev_map->ab;
15158 
15159 		ah = ath12k_mac_hw_allocate(ag, pdev_map, radio_per_hw);
15160 		if (!ah) {
15161 			ath12k_warn(ab, "failed to allocate mac80211 hw device for hw_idx %d\n",
15162 				    i);
15163 			ret = -ENOMEM;
15164 			goto err;
15165 		}
15166 
15167 		ah->dev = ab->dev;
15168 
15169 		ag->ah[i] = ah;
15170 		ag->num_hw++;
15171 	}
15172 
15173 	return 0;
15174 
15175 err:
15176 	for (i = i - 1; i >= 0; i--) {
15177 		ah = ath12k_ag_to_ah(ag, i);
15178 		if (!ah)
15179 			continue;
15180 
15181 		ath12k_mac_hw_destroy(ah);
15182 		ath12k_ag_set_ah(ag, i, NULL);
15183 	}
15184 
15185 	return ret;
15186 }
15187 
15188 int ath12k_mac_vif_set_keepalive(struct ath12k_link_vif *arvif,
15189 				 enum wmi_sta_keepalive_method method,
15190 				 u32 interval)
15191 {
15192 	struct wmi_sta_keepalive_arg arg = {};
15193 	struct ath12k *ar = arvif->ar;
15194 	int ret;
15195 
15196 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
15197 
15198 	if (arvif->ahvif->vdev_type != WMI_VDEV_TYPE_STA)
15199 		return 0;
15200 
15201 	if (!test_bit(WMI_TLV_SERVICE_STA_KEEP_ALIVE, ar->ab->wmi_ab.svc_map))
15202 		return 0;
15203 
15204 	arg.vdev_id = arvif->vdev_id;
15205 	arg.enabled = 1;
15206 	arg.method = method;
15207 	arg.interval = interval;
15208 
15209 	ret = ath12k_wmi_sta_keepalive(ar, &arg);
15210 	if (ret) {
15211 		ath12k_warn(ar->ab, "failed to set keepalive on vdev %i: %d\n",
15212 			    arvif->vdev_id, ret);
15213 		return ret;
15214 	}
15215 
15216 	return 0;
15217 }
15218