xref: /linux/drivers/net/wireless/ath/ath12k/mac.c (revision 2bee2e6c983baa3605765621f26173ff0fa40365)
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 #define ATH12K_5_9_GHZ_MIN_FREQ 5845
55 #define ATH12K_5_9_GHZ_MAX_FREQ 5885
56 
57 static const struct ieee80211_channel ath12k_2ghz_channels[] = {
58 	CHAN2G(1, 2412, 0),
59 	CHAN2G(2, 2417, 0),
60 	CHAN2G(3, 2422, 0),
61 	CHAN2G(4, 2427, 0),
62 	CHAN2G(5, 2432, 0),
63 	CHAN2G(6, 2437, 0),
64 	CHAN2G(7, 2442, 0),
65 	CHAN2G(8, 2447, 0),
66 	CHAN2G(9, 2452, 0),
67 	CHAN2G(10, 2457, 0),
68 	CHAN2G(11, 2462, 0),
69 	CHAN2G(12, 2467, 0),
70 	CHAN2G(13, 2472, 0),
71 	CHAN2G(14, 2484, 0),
72 };
73 
74 static const struct ieee80211_channel ath12k_5ghz_channels[] = {
75 	CHAN5G(36, 5180, 0),
76 	CHAN5G(40, 5200, 0),
77 	CHAN5G(44, 5220, 0),
78 	CHAN5G(48, 5240, 0),
79 	CHAN5G(52, 5260, 0),
80 	CHAN5G(56, 5280, 0),
81 	CHAN5G(60, 5300, 0),
82 	CHAN5G(64, 5320, 0),
83 	CHAN5G(100, 5500, 0),
84 	CHAN5G(104, 5520, 0),
85 	CHAN5G(108, 5540, 0),
86 	CHAN5G(112, 5560, 0),
87 	CHAN5G(116, 5580, 0),
88 	CHAN5G(120, 5600, 0),
89 	CHAN5G(124, 5620, 0),
90 	CHAN5G(128, 5640, 0),
91 	CHAN5G(132, 5660, 0),
92 	CHAN5G(136, 5680, 0),
93 	CHAN5G(140, 5700, 0),
94 	CHAN5G(144, 5720, 0),
95 	CHAN5G(149, 5745, 0),
96 	CHAN5G(153, 5765, 0),
97 	CHAN5G(157, 5785, 0),
98 	CHAN5G(161, 5805, 0),
99 	CHAN5G(165, 5825, 0),
100 	CHAN5G(169, 5845, 0),
101 	CHAN5G(173, 5865, 0),
102 	CHAN5G(177, 5885, 0),
103 };
104 
105 static const struct ieee80211_channel ath12k_6ghz_channels[] = {
106 	/* Operating Class 136 */
107 	CHAN6G(2, 5935, 0),
108 
109 	/* Operating Classes 131-135 */
110 	CHAN6G(1, 5955, 0),
111 	CHAN6G(5, 5975, 0),
112 	CHAN6G(9, 5995, 0),
113 	CHAN6G(13, 6015, 0),
114 	CHAN6G(17, 6035, 0),
115 	CHAN6G(21, 6055, 0),
116 	CHAN6G(25, 6075, 0),
117 	CHAN6G(29, 6095, 0),
118 	CHAN6G(33, 6115, 0),
119 	CHAN6G(37, 6135, 0),
120 	CHAN6G(41, 6155, 0),
121 	CHAN6G(45, 6175, 0),
122 	CHAN6G(49, 6195, 0),
123 	CHAN6G(53, 6215, 0),
124 	CHAN6G(57, 6235, 0),
125 	CHAN6G(61, 6255, 0),
126 	CHAN6G(65, 6275, 0),
127 	CHAN6G(69, 6295, 0),
128 	CHAN6G(73, 6315, 0),
129 	CHAN6G(77, 6335, 0),
130 	CHAN6G(81, 6355, 0),
131 	CHAN6G(85, 6375, 0),
132 	CHAN6G(89, 6395, 0),
133 	CHAN6G(93, 6415, 0),
134 	CHAN6G(97, 6435, 0),
135 	CHAN6G(101, 6455, 0),
136 	CHAN6G(105, 6475, 0),
137 	CHAN6G(109, 6495, 0),
138 	CHAN6G(113, 6515, 0),
139 	CHAN6G(117, 6535, 0),
140 	CHAN6G(121, 6555, 0),
141 	CHAN6G(125, 6575, 0),
142 	CHAN6G(129, 6595, 0),
143 	CHAN6G(133, 6615, 0),
144 	CHAN6G(137, 6635, 0),
145 	CHAN6G(141, 6655, 0),
146 	CHAN6G(145, 6675, 0),
147 	CHAN6G(149, 6695, 0),
148 	CHAN6G(153, 6715, 0),
149 	CHAN6G(157, 6735, 0),
150 	CHAN6G(161, 6755, 0),
151 	CHAN6G(165, 6775, 0),
152 	CHAN6G(169, 6795, 0),
153 	CHAN6G(173, 6815, 0),
154 	CHAN6G(177, 6835, 0),
155 	CHAN6G(181, 6855, 0),
156 	CHAN6G(185, 6875, 0),
157 	CHAN6G(189, 6895, 0),
158 	CHAN6G(193, 6915, 0),
159 	CHAN6G(197, 6935, 0),
160 	CHAN6G(201, 6955, 0),
161 	CHAN6G(205, 6975, 0),
162 	CHAN6G(209, 6995, 0),
163 	CHAN6G(213, 7015, 0),
164 	CHAN6G(217, 7035, 0),
165 	CHAN6G(221, 7055, 0),
166 	CHAN6G(225, 7075, 0),
167 	CHAN6G(229, 7095, 0),
168 	CHAN6G(233, 7115, 0),
169 };
170 
171 #define ATH12K_MAC_RATE_A_M(bps, code) \
172 	{ .bitrate = (bps), .hw_value = (code),\
173 	  .flags = IEEE80211_RATE_MANDATORY_A }
174 
175 #define ATH12K_MAC_RATE_B(bps, code, code_short) \
176 	{ .bitrate = (bps), .hw_value = (code), .hw_value_short = (code_short),\
177 	  .flags = IEEE80211_RATE_SHORT_PREAMBLE }
178 
179 static struct ieee80211_rate ath12k_legacy_rates[] = {
180 	{ .bitrate = 10,
181 	  .hw_value = ATH12K_HW_RATE_CCK_LP_1M },
182 	ATH12K_MAC_RATE_B(20, ATH12K_HW_RATE_CCK_LP_2M,
183 			  ATH12K_HW_RATE_CCK_SP_2M),
184 	ATH12K_MAC_RATE_B(55, ATH12K_HW_RATE_CCK_LP_5_5M,
185 			  ATH12K_HW_RATE_CCK_SP_5_5M),
186 	ATH12K_MAC_RATE_B(110, ATH12K_HW_RATE_CCK_LP_11M,
187 			  ATH12K_HW_RATE_CCK_SP_11M),
188 	ATH12K_MAC_RATE_A_M(60, ATH12K_HW_RATE_OFDM_6M),
189 	ATH12K_MAC_RATE_A_M(90, ATH12K_HW_RATE_OFDM_9M),
190 	ATH12K_MAC_RATE_A_M(120, ATH12K_HW_RATE_OFDM_12M),
191 	ATH12K_MAC_RATE_A_M(180, ATH12K_HW_RATE_OFDM_18M),
192 	ATH12K_MAC_RATE_A_M(240, ATH12K_HW_RATE_OFDM_24M),
193 	ATH12K_MAC_RATE_A_M(360, ATH12K_HW_RATE_OFDM_36M),
194 	ATH12K_MAC_RATE_A_M(480, ATH12K_HW_RATE_OFDM_48M),
195 	ATH12K_MAC_RATE_A_M(540, ATH12K_HW_RATE_OFDM_54M),
196 };
197 
198 static const int
199 ath12k_phymodes[NUM_NL80211_BANDS][ATH12K_CHAN_WIDTH_NUM] = {
200 	[NL80211_BAND_2GHZ] = {
201 			[NL80211_CHAN_WIDTH_5] = MODE_UNKNOWN,
202 			[NL80211_CHAN_WIDTH_10] = MODE_UNKNOWN,
203 			[NL80211_CHAN_WIDTH_20_NOHT] = MODE_11BE_EHT20_2G,
204 			[NL80211_CHAN_WIDTH_20] = MODE_11BE_EHT20_2G,
205 			[NL80211_CHAN_WIDTH_40] = MODE_11BE_EHT40_2G,
206 			[NL80211_CHAN_WIDTH_80] = MODE_UNKNOWN,
207 			[NL80211_CHAN_WIDTH_80P80] = MODE_UNKNOWN,
208 			[NL80211_CHAN_WIDTH_160] = MODE_UNKNOWN,
209 			[NL80211_CHAN_WIDTH_320] = MODE_UNKNOWN,
210 	},
211 	[NL80211_BAND_5GHZ] = {
212 			[NL80211_CHAN_WIDTH_5] = MODE_UNKNOWN,
213 			[NL80211_CHAN_WIDTH_10] = MODE_UNKNOWN,
214 			[NL80211_CHAN_WIDTH_20_NOHT] = MODE_11BE_EHT20,
215 			[NL80211_CHAN_WIDTH_20] = MODE_11BE_EHT20,
216 			[NL80211_CHAN_WIDTH_40] = MODE_11BE_EHT40,
217 			[NL80211_CHAN_WIDTH_80] = MODE_11BE_EHT80,
218 			[NL80211_CHAN_WIDTH_160] = MODE_11BE_EHT160,
219 			[NL80211_CHAN_WIDTH_80P80] = MODE_UNKNOWN,
220 			[NL80211_CHAN_WIDTH_320] = MODE_11BE_EHT320,
221 	},
222 	[NL80211_BAND_6GHZ] = {
223 			[NL80211_CHAN_WIDTH_5] = MODE_UNKNOWN,
224 			[NL80211_CHAN_WIDTH_10] = MODE_UNKNOWN,
225 			[NL80211_CHAN_WIDTH_20_NOHT] = MODE_11BE_EHT20,
226 			[NL80211_CHAN_WIDTH_20] = MODE_11BE_EHT20,
227 			[NL80211_CHAN_WIDTH_40] = MODE_11BE_EHT40,
228 			[NL80211_CHAN_WIDTH_80] = MODE_11BE_EHT80,
229 			[NL80211_CHAN_WIDTH_160] = MODE_11BE_EHT160,
230 			[NL80211_CHAN_WIDTH_80P80] = MODE_UNKNOWN,
231 			[NL80211_CHAN_WIDTH_320] = MODE_11BE_EHT320,
232 	},
233 
234 };
235 
236 const struct htt_rx_ring_tlv_filter ath12k_mac_mon_status_filter_default = {
237 	.rx_filter = HTT_RX_FILTER_TLV_FLAGS_MPDU_START |
238 		     HTT_RX_FILTER_TLV_FLAGS_PPDU_END |
239 		     HTT_RX_FILTER_TLV_FLAGS_PPDU_END_STATUS_DONE |
240 		     HTT_RX_FILTER_TLV_FLAGS_PPDU_START_USER_INFO,
241 	.pkt_filter_flags0 = HTT_RX_FP_MGMT_FILTER_FLAGS0,
242 	.pkt_filter_flags1 = HTT_RX_FP_MGMT_FILTER_FLAGS1,
243 	.pkt_filter_flags2 = HTT_RX_FP_CTRL_FILTER_FLASG2,
244 	.pkt_filter_flags3 = HTT_RX_FP_DATA_FILTER_FLASG3 |
245 			     HTT_RX_FP_CTRL_FILTER_FLASG3
246 };
247 
248 #define ATH12K_MAC_FIRST_OFDM_RATE_IDX 4
249 #define ath12k_g_rates ath12k_legacy_rates
250 #define ath12k_g_rates_size (ARRAY_SIZE(ath12k_legacy_rates))
251 #define ath12k_a_rates (ath12k_legacy_rates + 4)
252 #define ath12k_a_rates_size (ARRAY_SIZE(ath12k_legacy_rates) - 4)
253 
254 #define ATH12K_MAC_SCAN_TIMEOUT_MSECS 200 /* in msecs */
255 
256 static const u32 ath12k_smps_map[] = {
257 	[WLAN_HT_CAP_SM_PS_STATIC] = WMI_PEER_SMPS_STATIC,
258 	[WLAN_HT_CAP_SM_PS_DYNAMIC] = WMI_PEER_SMPS_DYNAMIC,
259 	[WLAN_HT_CAP_SM_PS_INVALID] = WMI_PEER_SMPS_PS_NONE,
260 	[WLAN_HT_CAP_SM_PS_DISABLED] = WMI_PEER_SMPS_PS_NONE,
261 };
262 
263 static int ath12k_start_vdev_delay(struct ath12k *ar,
264 				   struct ath12k_link_vif *arvif);
265 static void ath12k_mac_stop(struct ath12k *ar);
266 static int ath12k_mac_vdev_create(struct ath12k *ar, struct ath12k_link_vif *arvif);
267 static int ath12k_mac_vdev_delete(struct ath12k *ar, struct ath12k_link_vif *arvif);
268 
269 static const char *ath12k_mac_phymode_str(enum wmi_phy_mode mode)
270 {
271 	switch (mode) {
272 	case MODE_11A:
273 		return "11a";
274 	case MODE_11G:
275 		return "11g";
276 	case MODE_11B:
277 		return "11b";
278 	case MODE_11GONLY:
279 		return "11gonly";
280 	case MODE_11NA_HT20:
281 		return "11na-ht20";
282 	case MODE_11NG_HT20:
283 		return "11ng-ht20";
284 	case MODE_11NA_HT40:
285 		return "11na-ht40";
286 	case MODE_11NG_HT40:
287 		return "11ng-ht40";
288 	case MODE_11AC_VHT20:
289 		return "11ac-vht20";
290 	case MODE_11AC_VHT40:
291 		return "11ac-vht40";
292 	case MODE_11AC_VHT80:
293 		return "11ac-vht80";
294 	case MODE_11AC_VHT160:
295 		return "11ac-vht160";
296 	case MODE_11AC_VHT80_80:
297 		return "11ac-vht80+80";
298 	case MODE_11AC_VHT20_2G:
299 		return "11ac-vht20-2g";
300 	case MODE_11AC_VHT40_2G:
301 		return "11ac-vht40-2g";
302 	case MODE_11AC_VHT80_2G:
303 		return "11ac-vht80-2g";
304 	case MODE_11AX_HE20:
305 		return "11ax-he20";
306 	case MODE_11AX_HE40:
307 		return "11ax-he40";
308 	case MODE_11AX_HE80:
309 		return "11ax-he80";
310 	case MODE_11AX_HE80_80:
311 		return "11ax-he80+80";
312 	case MODE_11AX_HE160:
313 		return "11ax-he160";
314 	case MODE_11AX_HE20_2G:
315 		return "11ax-he20-2g";
316 	case MODE_11AX_HE40_2G:
317 		return "11ax-he40-2g";
318 	case MODE_11AX_HE80_2G:
319 		return "11ax-he80-2g";
320 	case MODE_11BE_EHT20:
321 		return "11be-eht20";
322 	case MODE_11BE_EHT40:
323 		return "11be-eht40";
324 	case MODE_11BE_EHT80:
325 		return "11be-eht80";
326 	case MODE_11BE_EHT80_80:
327 		return "11be-eht80+80";
328 	case MODE_11BE_EHT160:
329 		return "11be-eht160";
330 	case MODE_11BE_EHT160_160:
331 		return "11be-eht160+160";
332 	case MODE_11BE_EHT320:
333 		return "11be-eht320";
334 	case MODE_11BE_EHT20_2G:
335 		return "11be-eht20-2g";
336 	case MODE_11BE_EHT40_2G:
337 		return "11be-eht40-2g";
338 	case MODE_UNKNOWN:
339 		/* skip */
340 		break;
341 
342 		/* no default handler to allow compiler to check that the
343 		 * enum is fully handled
344 		 */
345 	}
346 
347 	return "<unknown>";
348 }
349 
350 u16 ath12k_mac_he_convert_tones_to_ru_tones(u16 tones)
351 {
352 	switch (tones) {
353 	case 26:
354 		return RU_26;
355 	case 52:
356 		return RU_52;
357 	case 106:
358 		return RU_106;
359 	case 242:
360 		return RU_242;
361 	case 484:
362 		return RU_484;
363 	case 996:
364 		return RU_996;
365 	case (996 * 2):
366 		return RU_2X996;
367 	default:
368 		return RU_26;
369 	}
370 }
371 EXPORT_SYMBOL(ath12k_mac_he_convert_tones_to_ru_tones);
372 
373 enum nl80211_eht_gi ath12k_mac_eht_gi_to_nl80211_eht_gi(u8 sgi)
374 {
375 	switch (sgi) {
376 	case RX_MSDU_START_SGI_0_8_US:
377 		return NL80211_RATE_INFO_EHT_GI_0_8;
378 	case RX_MSDU_START_SGI_1_6_US:
379 		return NL80211_RATE_INFO_EHT_GI_1_6;
380 	case RX_MSDU_START_SGI_3_2_US:
381 		return NL80211_RATE_INFO_EHT_GI_3_2;
382 	default:
383 		return NL80211_RATE_INFO_EHT_GI_0_8;
384 	}
385 }
386 EXPORT_SYMBOL(ath12k_mac_eht_gi_to_nl80211_eht_gi);
387 
388 enum nl80211_eht_ru_alloc ath12k_mac_eht_ru_tones_to_nl80211_eht_ru_alloc(u16 ru_tones)
389 {
390 	switch (ru_tones) {
391 	case 26:
392 		return NL80211_RATE_INFO_EHT_RU_ALLOC_26;
393 	case 52:
394 		return NL80211_RATE_INFO_EHT_RU_ALLOC_52;
395 	case (52 + 26):
396 		return NL80211_RATE_INFO_EHT_RU_ALLOC_52P26;
397 	case 106:
398 		return NL80211_RATE_INFO_EHT_RU_ALLOC_106;
399 	case (106 + 26):
400 		return NL80211_RATE_INFO_EHT_RU_ALLOC_106P26;
401 	case 242:
402 		return NL80211_RATE_INFO_EHT_RU_ALLOC_242;
403 	case 484:
404 		return NL80211_RATE_INFO_EHT_RU_ALLOC_484;
405 	case (484 + 242):
406 		return NL80211_RATE_INFO_EHT_RU_ALLOC_484P242;
407 	case 996:
408 		return NL80211_RATE_INFO_EHT_RU_ALLOC_996;
409 	case (996 + 484):
410 		return NL80211_RATE_INFO_EHT_RU_ALLOC_996P484;
411 	case (996 + 484 + 242):
412 		return NL80211_RATE_INFO_EHT_RU_ALLOC_996P484P242;
413 	case (2 * 996):
414 		return NL80211_RATE_INFO_EHT_RU_ALLOC_2x996;
415 	case (2 * 996 + 484):
416 		return NL80211_RATE_INFO_EHT_RU_ALLOC_2x996P484;
417 	case (3 * 996):
418 		return NL80211_RATE_INFO_EHT_RU_ALLOC_3x996;
419 	case (3 * 996 + 484):
420 		return NL80211_RATE_INFO_EHT_RU_ALLOC_3x996P484;
421 	case (4 * 996):
422 		return NL80211_RATE_INFO_EHT_RU_ALLOC_4x996;
423 	default:
424 		return NL80211_RATE_INFO_EHT_RU_ALLOC_26;
425 	}
426 }
427 EXPORT_SYMBOL(ath12k_mac_eht_ru_tones_to_nl80211_eht_ru_alloc);
428 
429 enum rate_info_bw
430 ath12k_mac_bw_to_mac80211_bw(enum ath12k_supported_bw bw)
431 {
432 	u8 ret = RATE_INFO_BW_20;
433 
434 	switch (bw) {
435 	case ATH12K_BW_20:
436 		ret = RATE_INFO_BW_20;
437 		break;
438 	case ATH12K_BW_40:
439 		ret = RATE_INFO_BW_40;
440 		break;
441 	case ATH12K_BW_80:
442 		ret = RATE_INFO_BW_80;
443 		break;
444 	case ATH12K_BW_160:
445 		ret = RATE_INFO_BW_160;
446 		break;
447 	case ATH12K_BW_320:
448 		ret = RATE_INFO_BW_320;
449 		break;
450 	}
451 
452 	return ret;
453 }
454 EXPORT_SYMBOL(ath12k_mac_bw_to_mac80211_bw);
455 
456 enum ath12k_supported_bw ath12k_mac_mac80211_bw_to_ath12k_bw(enum rate_info_bw bw)
457 {
458 	switch (bw) {
459 	case RATE_INFO_BW_20:
460 		return ATH12K_BW_20;
461 	case RATE_INFO_BW_40:
462 		return ATH12K_BW_40;
463 	case RATE_INFO_BW_80:
464 		return ATH12K_BW_80;
465 	case RATE_INFO_BW_160:
466 		return ATH12K_BW_160;
467 	case RATE_INFO_BW_320:
468 		return ATH12K_BW_320;
469 	default:
470 		return ATH12K_BW_20;
471 	}
472 }
473 
474 int ath12k_mac_hw_ratecode_to_legacy_rate(u8 hw_rc, u8 preamble, u8 *rateidx,
475 					  u16 *rate)
476 {
477 	/* As default, it is OFDM rates */
478 	int i = ATH12K_MAC_FIRST_OFDM_RATE_IDX;
479 	int max_rates_idx = ath12k_g_rates_size;
480 
481 	if (preamble == WMI_RATE_PREAMBLE_CCK) {
482 		hw_rc &= ~ATH12K_HW_RATECODE_CCK_SHORT_PREAM_MASK;
483 		i = 0;
484 		max_rates_idx = ATH12K_MAC_FIRST_OFDM_RATE_IDX;
485 	}
486 
487 	while (i < max_rates_idx) {
488 		if (hw_rc == ath12k_legacy_rates[i].hw_value) {
489 			*rateidx = i;
490 			*rate = ath12k_legacy_rates[i].bitrate;
491 			return 0;
492 		}
493 		i++;
494 	}
495 
496 	return -EINVAL;
497 }
498 EXPORT_SYMBOL(ath12k_mac_hw_ratecode_to_legacy_rate);
499 
500 u8 ath12k_mac_bitrate_to_idx(const struct ieee80211_supported_band *sband,
501 			     u32 bitrate)
502 {
503 	int i;
504 
505 	for (i = 0; i < sband->n_bitrates; i++)
506 		if (sband->bitrates[i].bitrate == bitrate)
507 			return i;
508 
509 	return 0;
510 }
511 
512 static u32
513 ath12k_mac_max_ht_nss(const u8 *ht_mcs_mask)
514 {
515 	int nss;
516 
517 	for (nss = IEEE80211_HT_MCS_MASK_LEN - 1; nss >= 0; nss--)
518 		if (ht_mcs_mask[nss])
519 			return nss + 1;
520 
521 	return 1;
522 }
523 
524 static u32
525 ath12k_mac_max_vht_nss(const u16 *vht_mcs_mask)
526 {
527 	int nss;
528 
529 	for (nss = NL80211_VHT_NSS_MAX - 1; nss >= 0; nss--)
530 		if (vht_mcs_mask[nss])
531 			return nss + 1;
532 
533 	return 1;
534 }
535 
536 static u32
537 ath12k_mac_max_he_nss(const u16 he_mcs_mask[NL80211_HE_NSS_MAX])
538 {
539 	int nss;
540 
541 	for (nss = NL80211_HE_NSS_MAX - 1; nss >= 0; nss--)
542 		if (he_mcs_mask[nss])
543 			return nss + 1;
544 
545 	return 1;
546 }
547 
548 static u32
549 ath12k_mac_max_eht_nss(const u16 eht_mcs_mask[NL80211_EHT_NSS_MAX])
550 {
551 	int nss;
552 
553 	for (nss = NL80211_EHT_NSS_MAX - 1; nss >= 0; nss--)
554 		if (eht_mcs_mask[nss])
555 			return nss + 1;
556 
557 	return 1;
558 }
559 
560 static u32
561 ath12k_mac_max_eht_mcs_nss(const u8 *eht_mcs, int eht_mcs_set_size)
562 {
563 	int i;
564 	u8 nss = 0;
565 
566 	for (i = 0; i < eht_mcs_set_size; i++)
567 		nss = max(nss, u8_get_bits(eht_mcs[i], IEEE80211_EHT_MCS_NSS_RX));
568 
569 	return nss;
570 }
571 
572 static u8 ath12k_parse_mpdudensity(u8 mpdudensity)
573 {
574 /*  From IEEE Std 802.11-2020 defined values for "Minimum MPDU Start Spacing":
575  *   0 for no restriction
576  *   1 for 1/4 us
577  *   2 for 1/2 us
578  *   3 for 1 us
579  *   4 for 2 us
580  *   5 for 4 us
581  *   6 for 8 us
582  *   7 for 16 us
583  */
584 	switch (mpdudensity) {
585 	case 0:
586 		return 0;
587 	case 1:
588 	case 2:
589 	case 3:
590 	/* Our lower layer calculations limit our precision to
591 	 * 1 microsecond
592 	 */
593 		return 1;
594 	case 4:
595 		return 2;
596 	case 5:
597 		return 4;
598 	case 6:
599 		return 8;
600 	case 7:
601 		return 16;
602 	default:
603 		return 0;
604 	}
605 }
606 
607 static int ath12k_mac_vif_link_chan(struct ieee80211_vif *vif, u8 link_id,
608 				    struct cfg80211_chan_def *def)
609 {
610 	struct ieee80211_bss_conf *link_conf;
611 	struct ieee80211_chanctx_conf *conf;
612 
613 	rcu_read_lock();
614 	link_conf = rcu_dereference(vif->link_conf[link_id]);
615 
616 	if (!link_conf) {
617 		rcu_read_unlock();
618 		return -ENOLINK;
619 	}
620 
621 	conf = rcu_dereference(link_conf->chanctx_conf);
622 	if (!conf) {
623 		rcu_read_unlock();
624 		return -ENOENT;
625 	}
626 	*def = conf->def;
627 	rcu_read_unlock();
628 
629 	return 0;
630 }
631 
632 static struct ath12k_link_vif *
633 ath12k_mac_get_tx_arvif(struct ath12k_link_vif *arvif,
634 			struct ieee80211_bss_conf *link_conf)
635 {
636 	struct ieee80211_bss_conf *tx_bss_conf;
637 	struct ath12k *ar = arvif->ar;
638 	struct ath12k_vif *tx_ahvif;
639 
640 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
641 
642 	tx_bss_conf = wiphy_dereference(ath12k_ar_to_hw(ar)->wiphy,
643 					link_conf->tx_bss_conf);
644 	if (tx_bss_conf) {
645 		tx_ahvif = ath12k_vif_to_ahvif(tx_bss_conf->vif);
646 		return wiphy_dereference(tx_ahvif->ah->hw->wiphy,
647 					 tx_ahvif->link[tx_bss_conf->link_id]);
648 	}
649 
650 	return NULL;
651 }
652 
653 static const u8 *ath12k_mac_get_tx_bssid(struct ath12k_link_vif *arvif)
654 {
655 	struct ieee80211_bss_conf *link_conf;
656 	struct ath12k_link_vif *tx_arvif;
657 	struct ath12k *ar = arvif->ar;
658 
659 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
660 
661 	link_conf = ath12k_mac_get_link_bss_conf(arvif);
662 	if (!link_conf) {
663 		ath12k_warn(ar->ab,
664 			    "unable to access bss link conf for link %u required to retrieve transmitting link conf\n",
665 			    arvif->link_id);
666 		return NULL;
667 	}
668 	if (link_conf->vif->type == NL80211_IFTYPE_STATION) {
669 		if (link_conf->nontransmitted)
670 			return link_conf->transmitter_bssid;
671 	} else {
672 		tx_arvif = ath12k_mac_get_tx_arvif(arvif, link_conf);
673 		if (tx_arvif)
674 			return tx_arvif->bssid;
675 	}
676 
677 	return NULL;
678 }
679 
680 struct ieee80211_bss_conf *
681 ath12k_mac_get_link_bss_conf(struct ath12k_link_vif *arvif)
682 {
683 	struct ieee80211_vif *vif = arvif->ahvif->vif;
684 	struct ieee80211_bss_conf *link_conf;
685 	struct ath12k *ar = arvif->ar;
686 
687 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
688 
689 	if (arvif->link_id >= IEEE80211_MLD_MAX_NUM_LINKS)
690 		return NULL;
691 
692 	link_conf = wiphy_dereference(ath12k_ar_to_hw(ar)->wiphy,
693 				      vif->link_conf[arvif->link_id]);
694 
695 	return link_conf;
696 }
697 
698 static struct ieee80211_link_sta *ath12k_mac_get_link_sta(struct ath12k_link_sta *arsta)
699 {
700 	struct ath12k_sta *ahsta = arsta->ahsta;
701 	struct ieee80211_sta *sta = ath12k_ahsta_to_sta(ahsta);
702 	struct ieee80211_link_sta *link_sta;
703 
704 	lockdep_assert_wiphy(ahsta->ahvif->ah->hw->wiphy);
705 
706 	if (arsta->link_id >= IEEE80211_MLD_MAX_NUM_LINKS)
707 		return NULL;
708 
709 	link_sta = wiphy_dereference(ahsta->ahvif->ah->hw->wiphy,
710 				     sta->link[arsta->link_id]);
711 
712 	return link_sta;
713 }
714 
715 static bool ath12k_mac_bitrate_is_cck(int bitrate)
716 {
717 	switch (bitrate) {
718 	case 10:
719 	case 20:
720 	case 55:
721 	case 110:
722 		return true;
723 	}
724 
725 	return false;
726 }
727 
728 u8 ath12k_mac_hw_rate_to_idx(const struct ieee80211_supported_band *sband,
729 			     u8 hw_rate, bool cck)
730 {
731 	const struct ieee80211_rate *rate;
732 	int i;
733 
734 	for (i = 0; i < sband->n_bitrates; i++) {
735 		rate = &sband->bitrates[i];
736 
737 		if (ath12k_mac_bitrate_is_cck(rate->bitrate) != cck)
738 			continue;
739 
740 		/* To handle 802.11a PPDU type */
741 		if ((!cck) && (rate->hw_value == hw_rate) &&
742 		    (rate->flags & IEEE80211_RATE_MANDATORY_A))
743 			return i;
744 		/* To handle 802.11b short PPDU type */
745 		else if (rate->flags & IEEE80211_RATE_SHORT_PREAMBLE &&
746 			 rate->hw_value_short == hw_rate)
747 			return i;
748 		/* To handle 802.11b long PPDU type */
749 		else if (rate->hw_value == hw_rate)
750 			return i;
751 	}
752 
753 	return 0;
754 }
755 
756 static u8 ath12k_mac_bitrate_to_rate(int bitrate)
757 {
758 	return DIV_ROUND_UP(bitrate, 5) |
759 	       (ath12k_mac_bitrate_is_cck(bitrate) ? BIT(7) : 0);
760 }
761 
762 static void ath12k_get_arvif_iter(void *data, u8 *mac,
763 				  struct ieee80211_vif *vif)
764 {
765 	struct ath12k_vif_iter *arvif_iter = data;
766 	struct ath12k_vif *ahvif = ath12k_vif_to_ahvif(vif);
767 	unsigned long links_map = ahvif->links_map;
768 	struct ath12k_link_vif *arvif;
769 	u8 link_id;
770 
771 	for_each_set_bit(link_id, &links_map, IEEE80211_MLD_MAX_NUM_LINKS) {
772 		arvif = rcu_dereference(ahvif->link[link_id]);
773 
774 		if (WARN_ON(!arvif))
775 			continue;
776 
777 		if (!arvif->is_created)
778 			continue;
779 
780 		if (arvif->vdev_id == arvif_iter->vdev_id &&
781 		    arvif->ar == arvif_iter->ar) {
782 			arvif_iter->arvif = arvif;
783 			break;
784 		}
785 	}
786 }
787 
788 struct ath12k_link_vif *ath12k_mac_get_arvif(struct ath12k *ar, u32 vdev_id)
789 {
790 	struct ath12k_vif_iter arvif_iter = {};
791 	u32 flags;
792 
793 	/* To use the arvif returned, caller must have held rcu read lock.
794 	 */
795 	lockdep_assert_in_rcu_read_lock();
796 	arvif_iter.vdev_id = vdev_id;
797 	arvif_iter.ar = ar;
798 
799 	flags = IEEE80211_IFACE_ITER_RESUME_ALL;
800 	ieee80211_iterate_active_interfaces_atomic(ath12k_ar_to_hw(ar),
801 						   flags,
802 						   ath12k_get_arvif_iter,
803 						   &arvif_iter);
804 	if (!arvif_iter.arvif) {
805 		ath12k_warn(ar->ab, "No VIF found for vdev %d\n", vdev_id);
806 		return NULL;
807 	}
808 
809 	return arvif_iter.arvif;
810 }
811 
812 struct ath12k_link_vif *ath12k_mac_get_arvif_by_vdev_id(struct ath12k_base *ab,
813 							u32 vdev_id)
814 {
815 	int i;
816 	struct ath12k_pdev *pdev;
817 	struct ath12k_link_vif *arvif;
818 
819 	for (i = 0; i < ab->num_radios; i++) {
820 		pdev = rcu_dereference(ab->pdevs_active[i]);
821 		if (pdev && pdev->ar &&
822 		    (pdev->ar->allocated_vdev_map & (1LL << vdev_id))) {
823 			arvif = ath12k_mac_get_arvif(pdev->ar, vdev_id);
824 			if (arvif)
825 				return arvif;
826 		}
827 	}
828 
829 	return NULL;
830 }
831 
832 struct ath12k *ath12k_mac_get_ar_by_vdev_id(struct ath12k_base *ab, u32 vdev_id)
833 {
834 	int i;
835 	struct ath12k_pdev *pdev;
836 
837 	for (i = 0; i < ab->num_radios; i++) {
838 		pdev = rcu_dereference(ab->pdevs_active[i]);
839 		if (pdev && pdev->ar) {
840 			if (pdev->ar->allocated_vdev_map & (1LL << vdev_id))
841 				return pdev->ar;
842 		}
843 	}
844 
845 	return NULL;
846 }
847 
848 struct ath12k *ath12k_mac_get_ar_by_pdev_id(struct ath12k_base *ab, u32 pdev_id)
849 {
850 	int i;
851 	struct ath12k_pdev *pdev;
852 
853 	if (ab->hw_params->single_pdev_only) {
854 		pdev = rcu_dereference(ab->pdevs_active[0]);
855 		return pdev ? pdev->ar : NULL;
856 	}
857 
858 	if (WARN_ON(pdev_id > ab->num_radios))
859 		return NULL;
860 
861 	for (i = 0; i < ab->num_radios; i++) {
862 		if (ab->fw_mode == ATH12K_FIRMWARE_MODE_FTM)
863 			pdev = &ab->pdevs[i];
864 		else
865 			pdev = rcu_dereference(ab->pdevs_active[i]);
866 
867 		if (pdev && pdev->pdev_id == pdev_id)
868 			return (pdev->ar ? pdev->ar : NULL);
869 	}
870 
871 	return NULL;
872 }
873 
874 static bool ath12k_mac_is_ml_arvif(struct ath12k_link_vif *arvif)
875 {
876 	struct ath12k_vif *ahvif = arvif->ahvif;
877 
878 	lockdep_assert_wiphy(ahvif->ah->hw->wiphy);
879 
880 	if (ahvif->vif->valid_links & BIT(arvif->link_id))
881 		return true;
882 
883 	return false;
884 }
885 
886 static struct ath12k *ath12k_mac_get_ar_by_chan(struct ieee80211_hw *hw,
887 						struct ieee80211_channel *channel)
888 {
889 	struct ath12k_hw *ah = hw->priv;
890 	struct ath12k *ar;
891 	int i;
892 
893 	ar = ah->radio;
894 
895 	if (ah->num_radio == 1)
896 		return ar;
897 
898 	for_each_ar(ah, ar, i) {
899 		if (channel->center_freq >= KHZ_TO_MHZ(ar->freq_range.start_freq) &&
900 		    channel->center_freq <= KHZ_TO_MHZ(ar->freq_range.end_freq))
901 			return ar;
902 	}
903 	return NULL;
904 }
905 
906 static struct ath12k *ath12k_get_ar_by_ctx(struct ieee80211_hw *hw,
907 					   struct ieee80211_chanctx_conf *ctx)
908 {
909 	if (!ctx)
910 		return NULL;
911 
912 	return ath12k_mac_get_ar_by_chan(hw, ctx->def.chan);
913 }
914 
915 struct ath12k *ath12k_get_ar_by_vif(struct ieee80211_hw *hw,
916 				    struct ieee80211_vif *vif,
917 				    u8 link_id)
918 {
919 	struct ath12k_vif *ahvif = ath12k_vif_to_ahvif(vif);
920 	struct ath12k_hw *ah = ath12k_hw_to_ah(hw);
921 	struct ath12k_link_vif *arvif;
922 
923 	lockdep_assert_wiphy(hw->wiphy);
924 
925 	/* If there is one pdev within ah, then we return
926 	 * ar directly.
927 	 */
928 	if (ah->num_radio == 1)
929 		return ah->radio;
930 
931 	if (!(ahvif->links_map & BIT(link_id)))
932 		return NULL;
933 
934 	arvif = wiphy_dereference(hw->wiphy, ahvif->link[link_id]);
935 	if (arvif && arvif->is_created)
936 		return arvif->ar;
937 
938 	return NULL;
939 }
940 
941 void ath12k_mac_get_any_chanctx_conf_iter(struct ieee80211_hw *hw,
942 					  struct ieee80211_chanctx_conf *conf,
943 					  void *data)
944 {
945 	struct ath12k_mac_get_any_chanctx_conf_arg *arg = data;
946 	struct ath12k *ctx_ar = ath12k_get_ar_by_ctx(hw, conf);
947 
948 	if (ctx_ar == arg->ar)
949 		arg->chanctx_conf = conf;
950 }
951 
952 static struct ath12k_link_vif *ath12k_mac_get_vif_up(struct ath12k *ar)
953 {
954 	struct ath12k_link_vif *arvif;
955 
956 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
957 
958 	list_for_each_entry(arvif, &ar->arvifs, list) {
959 		if (arvif->is_up)
960 			return arvif;
961 	}
962 
963 	return NULL;
964 }
965 
966 static bool ath12k_mac_band_match(enum nl80211_band band1, enum WMI_HOST_WLAN_BAND band2)
967 {
968 	switch (band1) {
969 	case NL80211_BAND_2GHZ:
970 		if (band2 & WMI_HOST_WLAN_2GHZ_CAP)
971 			return true;
972 		break;
973 	case NL80211_BAND_5GHZ:
974 	case NL80211_BAND_6GHZ:
975 		if (band2 & WMI_HOST_WLAN_5GHZ_CAP)
976 			return true;
977 		break;
978 	default:
979 		return false;
980 	}
981 
982 	return false;
983 }
984 
985 static u8 ath12k_mac_get_target_pdev_id_from_vif(struct ath12k_link_vif *arvif)
986 {
987 	struct ath12k *ar = arvif->ar;
988 	struct ath12k_base *ab = ar->ab;
989 	struct ieee80211_vif *vif = arvif->ahvif->vif;
990 	struct cfg80211_chan_def def;
991 	enum nl80211_band band;
992 	u8 pdev_id = ab->fw_pdev[0].pdev_id;
993 	int i;
994 
995 	if (WARN_ON(ath12k_mac_vif_link_chan(vif, arvif->link_id, &def)))
996 		return pdev_id;
997 
998 	band = def.chan->band;
999 
1000 	for (i = 0; i < ab->fw_pdev_count; i++) {
1001 		if (ath12k_mac_band_match(band, ab->fw_pdev[i].supported_bands))
1002 			return ab->fw_pdev[i].pdev_id;
1003 	}
1004 
1005 	return pdev_id;
1006 }
1007 
1008 u8 ath12k_mac_get_target_pdev_id(struct ath12k *ar)
1009 {
1010 	struct ath12k_link_vif *arvif;
1011 	struct ath12k_base *ab = ar->ab;
1012 
1013 	if (!ab->hw_params->single_pdev_only)
1014 		return ar->pdev->pdev_id;
1015 
1016 	arvif = ath12k_mac_get_vif_up(ar);
1017 
1018 	/* fw_pdev array has pdev ids derived from phy capability
1019 	 * service ready event (pdev_and_hw_link_ids).
1020 	 * If no vif is active, return default first index.
1021 	 */
1022 	if (!arvif)
1023 		return ar->ab->fw_pdev[0].pdev_id;
1024 
1025 	/* If active vif is found, return the pdev id matching chandef band */
1026 	return ath12k_mac_get_target_pdev_id_from_vif(arvif);
1027 }
1028 
1029 static void ath12k_pdev_caps_update(struct ath12k *ar)
1030 {
1031 	struct ath12k_base *ab = ar->ab;
1032 
1033 	ar->max_tx_power = ab->target_caps.hw_max_tx_power;
1034 
1035 	/* FIXME: Set min_tx_power to ab->target_caps.hw_min_tx_power.
1036 	 * But since the received value in svcrdy is same as hw_max_tx_power,
1037 	 * we can set ar->min_tx_power to 0 currently until
1038 	 * this is fixed in firmware
1039 	 */
1040 	ar->min_tx_power = 0;
1041 
1042 	ar->txpower_limit_2g = ar->max_tx_power;
1043 	ar->txpower_limit_5g = ar->max_tx_power;
1044 	ar->txpower_scale = WMI_HOST_TP_SCALE_MAX;
1045 }
1046 
1047 static int ath12k_mac_txpower_recalc(struct ath12k *ar)
1048 {
1049 	struct ath12k_pdev *pdev = ar->pdev;
1050 	struct ath12k_link_vif *arvif;
1051 	int ret, txpower = -1;
1052 	u32 param;
1053 
1054 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
1055 
1056 	list_for_each_entry(arvif, &ar->arvifs, list) {
1057 		if (arvif->txpower <= 0)
1058 			continue;
1059 
1060 		if (txpower == -1)
1061 			txpower = arvif->txpower;
1062 		else
1063 			txpower = min(txpower, arvif->txpower);
1064 	}
1065 
1066 	if (txpower == -1)
1067 		return 0;
1068 
1069 	/* txpwr is set as 2 units per dBm in FW*/
1070 	txpower = min_t(u32, max_t(u32, ar->min_tx_power, txpower),
1071 			ar->max_tx_power) * 2;
1072 
1073 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "txpower to set in hw %d\n",
1074 		   txpower / 2);
1075 
1076 	if ((pdev->cap.supported_bands & WMI_HOST_WLAN_2GHZ_CAP) &&
1077 	    ar->txpower_limit_2g != txpower) {
1078 		param = WMI_PDEV_PARAM_TXPOWER_LIMIT2G;
1079 		ret = ath12k_wmi_pdev_set_param(ar, param,
1080 						txpower, ar->pdev->pdev_id);
1081 		if (ret)
1082 			goto fail;
1083 		ar->txpower_limit_2g = txpower;
1084 	}
1085 
1086 	if ((pdev->cap.supported_bands & WMI_HOST_WLAN_5GHZ_CAP) &&
1087 	    ar->txpower_limit_5g != txpower) {
1088 		param = WMI_PDEV_PARAM_TXPOWER_LIMIT5G;
1089 		ret = ath12k_wmi_pdev_set_param(ar, param,
1090 						txpower, ar->pdev->pdev_id);
1091 		if (ret)
1092 			goto fail;
1093 		ar->txpower_limit_5g = txpower;
1094 	}
1095 
1096 	return 0;
1097 
1098 fail:
1099 	ath12k_warn(ar->ab, "failed to recalc txpower limit %d using pdev param %d: %d\n",
1100 		    txpower / 2, param, ret);
1101 	return ret;
1102 }
1103 
1104 static int ath12k_recalc_rtscts_prot(struct ath12k_link_vif *arvif)
1105 {
1106 	struct ath12k *ar = arvif->ar;
1107 	u32 vdev_param, rts_cts;
1108 	int ret;
1109 
1110 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
1111 
1112 	vdev_param = WMI_VDEV_PARAM_ENABLE_RTSCTS;
1113 
1114 	/* Enable RTS/CTS protection for sw retries (when legacy stations
1115 	 * are in BSS) or by default only for second rate series.
1116 	 * TODO: Check if we need to enable CTS 2 Self in any case
1117 	 */
1118 	rts_cts = WMI_USE_RTS_CTS;
1119 
1120 	if (arvif->num_legacy_stations > 0)
1121 		rts_cts |= WMI_RTSCTS_ACROSS_SW_RETRIES << 4;
1122 	else
1123 		rts_cts |= WMI_RTSCTS_FOR_SECOND_RATESERIES << 4;
1124 
1125 	/* Need not send duplicate param value to firmware */
1126 	if (arvif->rtscts_prot_mode == rts_cts)
1127 		return 0;
1128 
1129 	arvif->rtscts_prot_mode = rts_cts;
1130 
1131 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac vdev %d recalc rts/cts prot %d\n",
1132 		   arvif->vdev_id, rts_cts);
1133 
1134 	ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
1135 					    vdev_param, rts_cts);
1136 	if (ret)
1137 		ath12k_warn(ar->ab, "failed to recalculate rts/cts prot for vdev %d: %d\n",
1138 			    arvif->vdev_id, ret);
1139 
1140 	return ret;
1141 }
1142 
1143 static int ath12k_mac_set_kickout(struct ath12k_link_vif *arvif)
1144 {
1145 	struct ath12k *ar = arvif->ar;
1146 	u32 param;
1147 	int ret;
1148 
1149 	ret = ath12k_wmi_pdev_set_param(ar, WMI_PDEV_PARAM_STA_KICKOUT_TH,
1150 					ATH12K_KICKOUT_THRESHOLD,
1151 					ar->pdev->pdev_id);
1152 	if (ret) {
1153 		ath12k_warn(ar->ab, "failed to set kickout threshold on vdev %i: %d\n",
1154 			    arvif->vdev_id, ret);
1155 		return ret;
1156 	}
1157 
1158 	param = WMI_VDEV_PARAM_AP_KEEPALIVE_MIN_IDLE_INACTIVE_TIME_SECS;
1159 	ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id, param,
1160 					    ATH12K_KEEPALIVE_MIN_IDLE);
1161 	if (ret) {
1162 		ath12k_warn(ar->ab, "failed to set keepalive minimum idle time on vdev %i: %d\n",
1163 			    arvif->vdev_id, ret);
1164 		return ret;
1165 	}
1166 
1167 	param = WMI_VDEV_PARAM_AP_KEEPALIVE_MAX_IDLE_INACTIVE_TIME_SECS;
1168 	ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id, param,
1169 					    ATH12K_KEEPALIVE_MAX_IDLE);
1170 	if (ret) {
1171 		ath12k_warn(ar->ab, "failed to set keepalive maximum idle time on vdev %i: %d\n",
1172 			    arvif->vdev_id, ret);
1173 		return ret;
1174 	}
1175 
1176 	param = WMI_VDEV_PARAM_AP_KEEPALIVE_MAX_UNRESPONSIVE_TIME_SECS;
1177 	ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id, param,
1178 					    ATH12K_KEEPALIVE_MAX_UNRESPONSIVE);
1179 	if (ret) {
1180 		ath12k_warn(ar->ab, "failed to set keepalive maximum unresponsive time on vdev %i: %d\n",
1181 			    arvif->vdev_id, ret);
1182 		return ret;
1183 	}
1184 
1185 	return 0;
1186 }
1187 
1188 static void ath12k_mac_link_sta_rhash_cleanup(void *data, struct ieee80211_sta *sta)
1189 {
1190 	u8 link_id;
1191 	unsigned long links_map;
1192 	struct ath12k_sta *ahsta;
1193 	struct ath12k *ar = data;
1194 	struct ath12k_link_sta *arsta;
1195 	struct ath12k_link_vif *arvif;
1196 	struct ath12k_base *ab = ar->ab;
1197 
1198 	ahsta = ath12k_sta_to_ahsta(sta);
1199 	links_map = ahsta->links_map;
1200 
1201 	rcu_read_lock();
1202 	for_each_set_bit(link_id, &links_map, IEEE80211_MLD_MAX_NUM_LINKS) {
1203 		arsta = rcu_dereference(ahsta->link[link_id]);
1204 		if (!arsta)
1205 			continue;
1206 		arvif = arsta->arvif;
1207 		if (!(arvif->ar == ar))
1208 			continue;
1209 
1210 		spin_lock_bh(&ab->base_lock);
1211 		ath12k_link_sta_rhash_delete(ab, arsta);
1212 		spin_unlock_bh(&ab->base_lock);
1213 	}
1214 	rcu_read_unlock();
1215 }
1216 
1217 void ath12k_mac_peer_cleanup_all(struct ath12k *ar)
1218 {
1219 	struct ath12k_dp_link_peer *peer, *tmp;
1220 	struct ath12k_base *ab = ar->ab;
1221 	struct ath12k_dp *dp = ath12k_ab_to_dp(ab);
1222 	struct ath12k_link_vif *arvif, *tmp_vif;
1223 	struct ath12k_dp_hw *dp_hw = &ar->ah->dp_hw;
1224 	struct ath12k_dp_peer *dp_peer = NULL;
1225 	u16 peerid_index;
1226 	struct list_head peers;
1227 
1228 	INIT_LIST_HEAD(&peers);
1229 
1230 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
1231 
1232 	spin_lock_bh(&dp->dp_lock);
1233 	list_for_each_entry_safe(peer, tmp, &dp->peers, list) {
1234 		/* Skip Rx TID cleanup for self peer */
1235 		if (peer->sta && peer->dp_peer)
1236 			ath12k_dp_rx_peer_tid_cleanup(ar, peer);
1237 
1238 		/* cleanup dp peer */
1239 		spin_lock_bh(&dp_hw->peer_lock);
1240 		dp_peer = peer->dp_peer;
1241 		if (dp_peer) {
1242 			peerid_index = ath12k_dp_peer_get_peerid_index(dp, peer->peer_id);
1243 			rcu_assign_pointer(dp_peer->link_peers[peer->link_id], NULL);
1244 			WRITE_ONCE(dp_peer->link_peers_map,
1245 				   READ_ONCE(dp_peer->link_peers_map) & ~BIT(peer->link_id));
1246 			rcu_assign_pointer(dp_hw->dp_peers[peerid_index], NULL);
1247 		}
1248 		spin_unlock_bh(&dp_hw->peer_lock);
1249 
1250 		ath12k_dp_link_peer_rhash_delete(dp, peer);
1251 
1252 		list_move(&peer->list, &peers);
1253 	}
1254 	spin_unlock_bh(&dp->dp_lock);
1255 
1256 	synchronize_rcu();
1257 
1258 	list_for_each_entry_safe(peer, tmp, &peers, list) {
1259 		ath12k_dp_link_peer_free(peer);
1260 	}
1261 
1262 	ar->num_peers = 0;
1263 	ar->num_stations = 0;
1264 
1265 	/* Cleanup rhash table maintained for arsta by iterating over sta */
1266 	ieee80211_iterate_stations_mtx(ar->ah->hw, ath12k_mac_link_sta_rhash_cleanup,
1267 				       ar);
1268 
1269 	/* Delete all the self dp_peers on asserted radio */
1270 	list_for_each_entry_safe_reverse(arvif, tmp_vif, &ar->arvifs, list) {
1271 		if ((arvif->ahvif->vdev_type == WMI_VDEV_TYPE_AP) &&
1272 		    (arvif->link_id < IEEE80211_MLD_MAX_NUM_LINKS)) {
1273 			ath12k_dp_peer_delete(dp_hw, arvif->bssid, NULL);
1274 			arvif->num_stations = 0;
1275 		}
1276 	}
1277 }
1278 
1279 void ath12k_mac_dp_peer_cleanup(struct ath12k_hw *ah)
1280 {
1281 	struct list_head peers;
1282 	struct ath12k_dp_peer *dp_peer, *tmp;
1283 	struct ath12k_dp_hw *dp_hw = &ah->dp_hw;
1284 
1285 	lockdep_assert_wiphy(ah->hw->wiphy);
1286 
1287 	INIT_LIST_HEAD(&peers);
1288 
1289 	spin_lock_bh(&dp_hw->peer_lock);
1290 	list_for_each_entry_safe(dp_peer, tmp, &dp_hw->dp_peers_list, list) {
1291 		if (dp_peer->is_mlo) {
1292 			if (dp_peer->peer_id != ATH12K_MLO_PEER_ID_PENDING)
1293 				rcu_assign_pointer(dp_hw->dp_peers[dp_peer->peer_id],
1294 						   NULL);
1295 			ath12k_peer_ml_free(ah, ath12k_sta_to_ahsta(dp_peer->sta));
1296 		}
1297 
1298 		list_move(&dp_peer->list, &peers);
1299 	}
1300 
1301 	spin_unlock_bh(&dp_hw->peer_lock);
1302 
1303 	synchronize_rcu();
1304 
1305 	list_for_each_entry_safe(dp_peer, tmp, &peers, list) {
1306 		list_del(&dp_peer->list);
1307 		kfree(dp_peer);
1308 	}
1309 }
1310 
1311 static int ath12k_mac_vdev_setup_sync(struct ath12k *ar)
1312 {
1313 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
1314 
1315 	if (test_bit(ATH12K_FLAG_CRASH_FLUSH, &ar->ab->dev_flags))
1316 		return -ESHUTDOWN;
1317 
1318 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "vdev setup timeout %d\n",
1319 		   ATH12K_VDEV_SETUP_TIMEOUT_HZ);
1320 
1321 	if (!wait_for_completion_timeout(&ar->vdev_setup_done,
1322 					 ATH12K_VDEV_SETUP_TIMEOUT_HZ))
1323 		return -ETIMEDOUT;
1324 
1325 	return ar->last_wmi_vdev_start_status ? -EINVAL : 0;
1326 }
1327 
1328 static int ath12k_monitor_vdev_up(struct ath12k *ar, int vdev_id)
1329 {
1330 	struct ath12k_wmi_vdev_up_params params = {};
1331 	int ret;
1332 
1333 	params.vdev_id = vdev_id;
1334 	params.bssid = ar->mac_addr;
1335 	ret = ath12k_wmi_vdev_up(ar, &params);
1336 	if (ret) {
1337 		ath12k_warn(ar->ab, "failed to put up monitor vdev %i: %d\n",
1338 			    vdev_id, ret);
1339 		return ret;
1340 	}
1341 
1342 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac monitor vdev %i started\n",
1343 		   vdev_id);
1344 	return 0;
1345 }
1346 
1347 static int ath12k_mac_monitor_vdev_start(struct ath12k *ar, int vdev_id,
1348 					 struct cfg80211_chan_def *chandef)
1349 {
1350 	struct ieee80211_channel *channel;
1351 	struct wmi_vdev_start_req_arg arg = {};
1352 	struct ath12k_wmi_vdev_up_params params = {};
1353 	int ret;
1354 
1355 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
1356 
1357 	channel = chandef->chan;
1358 	arg.vdev_id = vdev_id;
1359 	arg.freq = channel->center_freq;
1360 	arg.band_center_freq1 = chandef->center_freq1;
1361 	arg.band_center_freq2 = chandef->center_freq2;
1362 	arg.mode = ath12k_phymodes[chandef->chan->band][chandef->width];
1363 	arg.chan_radar = !!(channel->flags & IEEE80211_CHAN_RADAR);
1364 
1365 	arg.min_power = 0;
1366 	arg.max_power = channel->max_power;
1367 	arg.max_reg_power = channel->max_reg_power;
1368 	arg.max_antenna_gain = channel->max_antenna_gain;
1369 
1370 	arg.pref_tx_streams = ar->num_tx_chains;
1371 	arg.pref_rx_streams = ar->num_rx_chains;
1372 	arg.punct_bitmap = 0xFFFFFFFF;
1373 
1374 	arg.passive |= !!(chandef->chan->flags & IEEE80211_CHAN_NO_IR);
1375 
1376 	reinit_completion(&ar->vdev_setup_done);
1377 	reinit_completion(&ar->vdev_delete_done);
1378 
1379 	ret = ath12k_wmi_vdev_start(ar, &arg, false);
1380 	if (ret) {
1381 		ath12k_warn(ar->ab, "failed to request monitor vdev %i start: %d\n",
1382 			    vdev_id, ret);
1383 		return ret;
1384 	}
1385 
1386 	ret = ath12k_mac_vdev_setup_sync(ar);
1387 	if (ret) {
1388 		ath12k_warn(ar->ab, "failed to synchronize setup for monitor vdev %i start: %d\n",
1389 			    vdev_id, ret);
1390 		return ret;
1391 	}
1392 
1393 	params.vdev_id = vdev_id;
1394 	params.bssid = ar->mac_addr;
1395 	ret = ath12k_wmi_vdev_up(ar, &params);
1396 	if (ret) {
1397 		ath12k_warn(ar->ab, "failed to put up monitor vdev %i: %d\n",
1398 			    vdev_id, ret);
1399 		goto vdev_stop;
1400 	}
1401 
1402 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac monitor vdev %i started\n",
1403 		   vdev_id);
1404 	return 0;
1405 
1406 vdev_stop:
1407 	ret = ath12k_wmi_vdev_stop(ar, vdev_id);
1408 	if (ret)
1409 		ath12k_warn(ar->ab, "failed to stop monitor vdev %i after start failure: %d\n",
1410 			    vdev_id, ret);
1411 	return ret;
1412 }
1413 
1414 static int ath12k_mac_monitor_vdev_stop(struct ath12k *ar)
1415 {
1416 	int ret;
1417 
1418 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
1419 
1420 	reinit_completion(&ar->vdev_setup_done);
1421 
1422 	ret = ath12k_wmi_vdev_stop(ar, ar->monitor_vdev_id);
1423 	if (ret)
1424 		ath12k_warn(ar->ab, "failed to request monitor vdev %i stop: %d\n",
1425 			    ar->monitor_vdev_id, ret);
1426 
1427 	ret = ath12k_mac_vdev_setup_sync(ar);
1428 	if (ret)
1429 		ath12k_warn(ar->ab, "failed to synchronize monitor vdev %i stop: %d\n",
1430 			    ar->monitor_vdev_id, ret);
1431 
1432 	ret = ath12k_wmi_vdev_down(ar, ar->monitor_vdev_id);
1433 	if (ret)
1434 		ath12k_warn(ar->ab, "failed to put down monitor vdev %i: %d\n",
1435 			    ar->monitor_vdev_id, ret);
1436 
1437 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac monitor vdev %i stopped\n",
1438 		   ar->monitor_vdev_id);
1439 	return ret;
1440 }
1441 
1442 static int ath12k_mac_monitor_vdev_delete(struct ath12k *ar)
1443 {
1444 	int ret;
1445 	unsigned long time_left;
1446 
1447 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
1448 
1449 	if (!ar->monitor_vdev_created)
1450 		return 0;
1451 
1452 	reinit_completion(&ar->vdev_delete_done);
1453 
1454 	ret = ath12k_wmi_vdev_delete(ar, ar->monitor_vdev_id);
1455 	if (ret) {
1456 		ath12k_warn(ar->ab, "failed to request wmi monitor vdev %i removal: %d\n",
1457 			    ar->monitor_vdev_id, ret);
1458 		return ret;
1459 	}
1460 
1461 	time_left = wait_for_completion_timeout(&ar->vdev_delete_done,
1462 						ATH12K_VDEV_DELETE_TIMEOUT_HZ);
1463 	if (time_left == 0) {
1464 		ath12k_warn(ar->ab, "Timeout in receiving vdev delete response\n");
1465 	} else {
1466 		ar->allocated_vdev_map &= ~(1LL << ar->monitor_vdev_id);
1467 		ar->ab->free_vdev_map |= 1LL << (ar->monitor_vdev_id);
1468 		ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac monitor vdev %d deleted\n",
1469 			   ar->monitor_vdev_id);
1470 		ar->num_created_vdevs--;
1471 		ar->monitor_vdev_id = -1;
1472 		ar->monitor_vdev_created = false;
1473 	}
1474 
1475 	return ret;
1476 }
1477 
1478 static int ath12k_mac_monitor_start(struct ath12k *ar)
1479 {
1480 	struct ath12k_mac_get_any_chanctx_conf_arg arg;
1481 	int ret;
1482 
1483 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
1484 
1485 	if (ar->monitor_started)
1486 		return 0;
1487 
1488 	arg.ar = ar;
1489 	arg.chanctx_conf = NULL;
1490 	ieee80211_iter_chan_contexts_atomic(ath12k_ar_to_hw(ar),
1491 					    ath12k_mac_get_any_chanctx_conf_iter,
1492 					    &arg);
1493 	if (!arg.chanctx_conf)
1494 		return 0;
1495 
1496 	ret = ath12k_mac_monitor_vdev_start(ar, ar->monitor_vdev_id,
1497 					    &arg.chanctx_conf->def);
1498 	if (ret) {
1499 		ath12k_warn(ar->ab, "failed to start monitor vdev: %d\n", ret);
1500 		return ret;
1501 	}
1502 
1503 	ret = ath12k_dp_tx_htt_monitor_mode_ring_config(ar, false);
1504 	if (ret) {
1505 		ath12k_warn(ar->ab, "fail to set monitor filter: %d\n", ret);
1506 		return ret;
1507 	}
1508 
1509 	ar->monitor_started = true;
1510 	ar->num_started_vdevs++;
1511 
1512 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac monitor started\n");
1513 
1514 	return 0;
1515 }
1516 
1517 static int ath12k_mac_monitor_stop(struct ath12k *ar)
1518 {
1519 	int ret;
1520 
1521 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
1522 
1523 	if (!ar->monitor_started)
1524 		return 0;
1525 
1526 	ret = ath12k_mac_monitor_vdev_stop(ar);
1527 	if (ret) {
1528 		ath12k_warn(ar->ab, "failed to stop monitor vdev: %d\n", ret);
1529 		return ret;
1530 	}
1531 
1532 	ar->monitor_started = false;
1533 	ar->num_started_vdevs--;
1534 	ret = ath12k_dp_tx_htt_monitor_mode_ring_config(ar, true);
1535 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac monitor stopped ret %d\n", ret);
1536 	return ret;
1537 }
1538 
1539 int ath12k_mac_vdev_stop(struct ath12k_link_vif *arvif)
1540 {
1541 	struct ath12k_vif *ahvif = arvif->ahvif;
1542 	struct ath12k *ar = arvif->ar;
1543 	int ret;
1544 
1545 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
1546 
1547 	reinit_completion(&ar->vdev_setup_done);
1548 
1549 	ret = ath12k_wmi_vdev_stop(ar, arvif->vdev_id);
1550 	if (ret) {
1551 		ath12k_warn(ar->ab, "failed to stop WMI vdev %i: %d\n",
1552 			    arvif->vdev_id, ret);
1553 		goto err;
1554 	}
1555 
1556 	ret = ath12k_mac_vdev_setup_sync(ar);
1557 	if (ret) {
1558 		ath12k_warn(ar->ab, "failed to synchronize setup for vdev %i: %d\n",
1559 			    arvif->vdev_id, ret);
1560 		goto err;
1561 	}
1562 
1563 	WARN_ON(ar->num_started_vdevs == 0);
1564 
1565 	ar->num_started_vdevs--;
1566 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "vdev %pM stopped, vdev_id %d\n",
1567 		   ahvif->vif->addr, arvif->vdev_id);
1568 
1569 	if (test_bit(ATH12K_FLAG_CAC_RUNNING, &ar->dev_flags)) {
1570 		clear_bit(ATH12K_FLAG_CAC_RUNNING, &ar->dev_flags);
1571 		ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "CAC Stopped for vdev %d\n",
1572 			   arvif->vdev_id);
1573 	}
1574 
1575 	return 0;
1576 err:
1577 	return ret;
1578 }
1579 
1580 int ath12k_mac_op_config(struct ieee80211_hw *hw, int radio_idx, u32 changed)
1581 {
1582 	return 0;
1583 }
1584 EXPORT_SYMBOL(ath12k_mac_op_config);
1585 
1586 static int ath12k_mac_setup_bcn_p2p_ie(struct ath12k_link_vif *arvif,
1587 				       struct sk_buff *bcn)
1588 {
1589 	struct ath12k *ar = arvif->ar;
1590 	struct ieee80211_mgmt *mgmt;
1591 	const u8 *p2p_ie;
1592 	int ret;
1593 
1594 	mgmt = (void *)bcn->data;
1595 	p2p_ie = cfg80211_find_vendor_ie(WLAN_OUI_WFA, WLAN_OUI_TYPE_WFA_P2P,
1596 					 mgmt->u.beacon.variable,
1597 					 bcn->len - (mgmt->u.beacon.variable -
1598 						     bcn->data));
1599 	if (!p2p_ie) {
1600 		ath12k_warn(ar->ab, "no P2P ie found in beacon\n");
1601 		return -ENOENT;
1602 	}
1603 
1604 	ret = ath12k_wmi_p2p_go_bcn_ie(ar, arvif->vdev_id, p2p_ie);
1605 	if (ret) {
1606 		ath12k_warn(ar->ab, "failed to submit P2P GO bcn ie for vdev %i: %d\n",
1607 			    arvif->vdev_id, ret);
1608 		return ret;
1609 	}
1610 
1611 	return 0;
1612 }
1613 
1614 static int ath12k_mac_remove_vendor_ie(struct sk_buff *skb, unsigned int oui,
1615 				       u8 oui_type, size_t ie_offset)
1616 {
1617 	const u8 *next, *end;
1618 	size_t len;
1619 	u8 *ie;
1620 
1621 	if (WARN_ON(skb->len < ie_offset))
1622 		return -EINVAL;
1623 
1624 	ie = (u8 *)cfg80211_find_vendor_ie(oui, oui_type,
1625 					   skb->data + ie_offset,
1626 					   skb->len - ie_offset);
1627 	if (!ie)
1628 		return -ENOENT;
1629 
1630 	len = ie[1] + 2;
1631 	end = skb->data + skb->len;
1632 	next = ie + len;
1633 
1634 	if (WARN_ON(next > end))
1635 		return -EINVAL;
1636 
1637 	memmove(ie, next, end - next);
1638 	skb_trim(skb, skb->len - len);
1639 
1640 	return 0;
1641 }
1642 
1643 static void ath12k_mac_set_arvif_ies(struct ath12k_link_vif *arvif,
1644 				     struct ath12k_link_vif *tx_arvif,
1645 				     struct sk_buff *bcn,
1646 				     u8 bssid_index, bool *nontx_profile_found)
1647 {
1648 	struct ieee80211_mgmt *mgmt = (struct ieee80211_mgmt *)bcn->data;
1649 	const struct element *elem, *nontx, *index, *nie, *ext_cap_ie;
1650 	const u8 *start, *tail;
1651 	u16 rem_len;
1652 	u8 i;
1653 
1654 	start = bcn->data + ieee80211_get_hdrlen_from_skb(bcn) + sizeof(mgmt->u.beacon);
1655 	tail = skb_tail_pointer(bcn);
1656 	rem_len = tail - start;
1657 
1658 	arvif->rsnie_present = false;
1659 	arvif->wpaie_present = false;
1660 
1661 	if (cfg80211_find_ie(WLAN_EID_RSN, start, rem_len))
1662 		arvif->rsnie_present = true;
1663 	if (cfg80211_find_vendor_ie(WLAN_OUI_MICROSOFT, WLAN_OUI_TYPE_MICROSOFT_WPA,
1664 				    start, rem_len))
1665 		arvif->wpaie_present = true;
1666 
1667 	ext_cap_ie = cfg80211_find_elem(WLAN_EID_EXT_CAPABILITY, start, rem_len);
1668 	if (ext_cap_ie && ext_cap_ie->datalen >= 11 &&
1669 	    (ext_cap_ie->data[10] & WLAN_EXT_CAPA11_BCN_PROTECT))
1670 		tx_arvif->beacon_prot = true;
1671 
1672 	/* Return from here for the transmitted profile */
1673 	if (!bssid_index)
1674 		return;
1675 
1676 	/* Initial rsnie_present for the nontransmitted profile is set to be same as that
1677 	 * of the transmitted profile. It will be changed if security configurations are
1678 	 * different.
1679 	 */
1680 	*nontx_profile_found = false;
1681 	for_each_element_id(elem, WLAN_EID_MULTIPLE_BSSID, start, rem_len) {
1682 		/* Fixed minimum MBSSID element length with at least one
1683 		 * nontransmitted BSSID profile is 12 bytes as given below;
1684 		 * 1 (max BSSID indicator) +
1685 		 * 2 (Nontransmitted BSSID profile: Subelement ID + length) +
1686 		 * 4 (Nontransmitted BSSID Capabilities: tag + length + info)
1687 		 * 2 (Nontransmitted BSSID SSID: tag + length)
1688 		 * 3 (Nontransmitted BSSID Index: tag + length + BSSID index
1689 		 */
1690 		if (elem->datalen < 12 || elem->data[0] < 1)
1691 			continue; /* Max BSSID indicator must be >=1 */
1692 
1693 		for_each_element(nontx, elem->data + 1, elem->datalen - 1) {
1694 			start = nontx->data;
1695 
1696 			if (nontx->id != 0 || nontx->datalen < 4)
1697 				continue; /* Invalid nontransmitted profile */
1698 
1699 			if (nontx->data[0] != WLAN_EID_NON_TX_BSSID_CAP ||
1700 			    nontx->data[1] != 2) {
1701 				continue; /* Missing nontransmitted BSS capabilities */
1702 			}
1703 
1704 			if (nontx->data[4] != WLAN_EID_SSID)
1705 				continue; /* Missing SSID for nontransmitted BSS */
1706 
1707 			index = cfg80211_find_elem(WLAN_EID_MULTI_BSSID_IDX,
1708 						   start, nontx->datalen);
1709 			if (!index || index->datalen < 1 || index->data[0] == 0)
1710 				continue; /* Invalid MBSSID Index element */
1711 
1712 			if (index->data[0] == bssid_index) {
1713 				*nontx_profile_found = true;
1714 
1715 				/* Check if nontx BSS has beacon protection enabled */
1716 				if (!tx_arvif->beacon_prot) {
1717 					ext_cap_ie =
1718 					    cfg80211_find_elem(WLAN_EID_EXT_CAPABILITY,
1719 							       nontx->data,
1720 							       nontx->datalen);
1721 					if (ext_cap_ie && ext_cap_ie->datalen >= 11 &&
1722 					    (ext_cap_ie->data[10] &
1723 					     WLAN_EXT_CAPA11_BCN_PROTECT))
1724 						tx_arvif->beacon_prot = true;
1725 				}
1726 
1727 				if (cfg80211_find_ie(WLAN_EID_RSN,
1728 						     nontx->data,
1729 						     nontx->datalen)) {
1730 					arvif->rsnie_present = true;
1731 					return;
1732 				} else if (!arvif->rsnie_present) {
1733 					return; /* Both tx and nontx BSS are open */
1734 				}
1735 
1736 				nie = cfg80211_find_ext_elem(WLAN_EID_EXT_NON_INHERITANCE,
1737 							     nontx->data,
1738 							     nontx->datalen);
1739 				if (!nie || nie->datalen < 2)
1740 					return; /* Invalid non-inheritance element */
1741 
1742 				for (i = 1; i < nie->datalen - 1; i++) {
1743 					if (nie->data[i] == WLAN_EID_RSN) {
1744 						arvif->rsnie_present = false;
1745 						break;
1746 					}
1747 				}
1748 
1749 				return;
1750 			}
1751 		}
1752 	}
1753 }
1754 
1755 static int ath12k_mac_setup_bcn_tmpl_ema(struct ath12k_link_vif *arvif,
1756 					 struct ath12k_link_vif *tx_arvif,
1757 					 u8 bssid_index)
1758 {
1759 	struct ath12k_wmi_bcn_tmpl_ema_arg ema_args;
1760 	struct ieee80211_ema_beacons *beacons;
1761 	bool nontx_profile_found = false;
1762 	int ret = 0;
1763 	u8 i;
1764 
1765 	beacons = ieee80211_beacon_get_template_ema_list(ath12k_ar_to_hw(tx_arvif->ar),
1766 							 tx_arvif->ahvif->vif,
1767 							 tx_arvif->link_id);
1768 	if (!beacons || !beacons->cnt) {
1769 		ath12k_warn(arvif->ar->ab,
1770 			    "failed to get ema beacon templates from mac80211\n");
1771 		return -EPERM;
1772 	}
1773 
1774 	if (tx_arvif == arvif)
1775 		ath12k_mac_set_arvif_ies(arvif, tx_arvif, beacons->bcn[0].skb, 0, NULL);
1776 
1777 	for (i = 0; i < beacons->cnt; i++) {
1778 		if (tx_arvif != arvif && !nontx_profile_found)
1779 			ath12k_mac_set_arvif_ies(arvif, tx_arvif, beacons->bcn[i].skb,
1780 						 bssid_index,
1781 						 &nontx_profile_found);
1782 
1783 		ema_args.bcn_cnt = beacons->cnt;
1784 		ema_args.bcn_index = i;
1785 		ret = ath12k_wmi_bcn_tmpl(tx_arvif, &beacons->bcn[i].offs,
1786 					  beacons->bcn[i].skb, &ema_args);
1787 		if (ret) {
1788 			ath12k_warn(tx_arvif->ar->ab,
1789 				    "failed to set ema beacon template id %i error %d\n",
1790 				    i, ret);
1791 			break;
1792 		}
1793 	}
1794 
1795 	if (tx_arvif != arvif && !nontx_profile_found)
1796 		ath12k_warn(arvif->ar->ab,
1797 			    "nontransmitted bssid index %u not found in beacon template\n",
1798 			    bssid_index);
1799 
1800 	ieee80211_beacon_free_ema_list(beacons);
1801 	return ret;
1802 }
1803 
1804 static int ath12k_mac_setup_bcn_tmpl(struct ath12k_link_vif *arvif)
1805 {
1806 	struct ath12k_vif *ahvif = arvif->ahvif;
1807 	struct ieee80211_vif *vif = ath12k_ahvif_to_vif(ahvif);
1808 	struct ieee80211_bss_conf *link_conf;
1809 	struct ath12k_link_vif *tx_arvif;
1810 	struct ath12k *ar = arvif->ar;
1811 	struct ath12k_base *ab = ar->ab;
1812 	struct ieee80211_mutable_offsets offs = {};
1813 	bool nontx_profile_found = false;
1814 	struct sk_buff *bcn;
1815 	int ret;
1816 
1817 	if (ahvif->vdev_type != WMI_VDEV_TYPE_AP)
1818 		return 0;
1819 
1820 	link_conf = ath12k_mac_get_link_bss_conf(arvif);
1821 	if (!link_conf) {
1822 		ath12k_warn(ar->ab, "unable to access bss link conf to set bcn tmpl for vif %pM link %u\n",
1823 			    vif->addr, arvif->link_id);
1824 		return -ENOLINK;
1825 	}
1826 
1827 	tx_arvif = ath12k_mac_get_tx_arvif(arvif, link_conf);
1828 	if (tx_arvif) {
1829 		if (tx_arvif != arvif && arvif->is_up)
1830 			return 0;
1831 
1832 		if (link_conf->ema_ap)
1833 			return ath12k_mac_setup_bcn_tmpl_ema(arvif, tx_arvif,
1834 							     link_conf->bssid_index);
1835 	} else {
1836 		tx_arvif = arvif;
1837 	}
1838 
1839 	bcn = ieee80211_beacon_get_template(ath12k_ar_to_hw(tx_arvif->ar),
1840 					    tx_arvif->ahvif->vif,
1841 					    &offs, tx_arvif->link_id);
1842 	if (!bcn) {
1843 		ath12k_warn(ab, "failed to get beacon template from mac80211\n");
1844 		return -EPERM;
1845 	}
1846 
1847 	if (tx_arvif == arvif) {
1848 		ath12k_mac_set_arvif_ies(arvif, tx_arvif, bcn, 0, NULL);
1849 	} else {
1850 		ath12k_mac_set_arvif_ies(arvif, tx_arvif, bcn,
1851 					 link_conf->bssid_index,
1852 					 &nontx_profile_found);
1853 		if (!nontx_profile_found)
1854 			ath12k_warn(ab,
1855 				    "nontransmitted profile not found in beacon template\n");
1856 	}
1857 
1858 	if (ahvif->vif->type == NL80211_IFTYPE_AP && ahvif->vif->p2p) {
1859 		ret = ath12k_mac_setup_bcn_p2p_ie(arvif, bcn);
1860 		if (ret) {
1861 			ath12k_warn(ab, "failed to setup P2P GO bcn ie: %d\n",
1862 				    ret);
1863 			goto free_bcn_skb;
1864 		}
1865 
1866 		/* P2P IE is inserted by firmware automatically (as
1867 		 * configured above) so remove it from the base beacon
1868 		 * template to avoid duplicate P2P IEs in beacon frames.
1869 		 */
1870 		ret = ath12k_mac_remove_vendor_ie(bcn, WLAN_OUI_WFA,
1871 						  WLAN_OUI_TYPE_WFA_P2P,
1872 						  offsetof(struct ieee80211_mgmt,
1873 							   u.beacon.variable));
1874 		if (ret) {
1875 			ath12k_warn(ab, "failed to remove P2P vendor ie: %d\n",
1876 				    ret);
1877 			goto free_bcn_skb;
1878 		}
1879 	}
1880 
1881 	ret = ath12k_wmi_bcn_tmpl(arvif, &offs, bcn, NULL);
1882 
1883 	if (ret)
1884 		ath12k_warn(ab, "failed to submit beacon template command: %d\n",
1885 			    ret);
1886 
1887 free_bcn_skb:
1888 	kfree_skb(bcn);
1889 	return ret;
1890 }
1891 
1892 static void ath12k_control_beaconing(struct ath12k_link_vif *arvif,
1893 				     struct ieee80211_bss_conf *info)
1894 {
1895 	struct ath12k_wmi_vdev_up_params params = {};
1896 	struct ath12k_vif *ahvif = arvif->ahvif;
1897 	struct ath12k *ar = arvif->ar;
1898 	int ret;
1899 
1900 	lockdep_assert_wiphy(ath12k_ar_to_hw(arvif->ar)->wiphy);
1901 
1902 	if (!info->enable_beacon) {
1903 		ret = ath12k_wmi_vdev_down(ar, arvif->vdev_id);
1904 		if (ret)
1905 			ath12k_warn(ar->ab, "failed to down vdev_id %i: %d\n",
1906 				    arvif->vdev_id, ret);
1907 
1908 		arvif->is_up = false;
1909 		return;
1910 	}
1911 
1912 	/* Install the beacon template to the FW */
1913 	ret = ath12k_mac_setup_bcn_tmpl(arvif);
1914 	if (ret) {
1915 		ath12k_warn(ar->ab, "failed to update bcn tmpl during vdev up: %d\n",
1916 			    ret);
1917 		return;
1918 	}
1919 
1920 	ahvif->aid = 0;
1921 
1922 	ether_addr_copy(arvif->bssid, info->addr);
1923 
1924 	params.vdev_id = arvif->vdev_id;
1925 	params.aid = ahvif->aid;
1926 	params.bssid = arvif->bssid;
1927 	params.tx_bssid = ath12k_mac_get_tx_bssid(arvif);
1928 	if (params.tx_bssid) {
1929 		params.nontx_profile_idx = info->bssid_index;
1930 		params.nontx_profile_cnt = 1 << info->bssid_indicator;
1931 	}
1932 	ret = ath12k_wmi_vdev_up(arvif->ar, &params);
1933 	if (ret) {
1934 		ath12k_warn(ar->ab, "failed to bring up vdev %d: %i\n",
1935 			    arvif->vdev_id, ret);
1936 		return;
1937 	}
1938 
1939 	arvif->is_up = true;
1940 
1941 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac vdev %d up\n", arvif->vdev_id);
1942 }
1943 
1944 static void ath12k_mac_handle_beacon_iter(void *data, u8 *mac,
1945 					  struct ieee80211_vif *vif)
1946 {
1947 	struct sk_buff *skb = data;
1948 	struct ieee80211_mgmt *mgmt = (void *)skb->data;
1949 	struct ath12k_vif *ahvif = ath12k_vif_to_ahvif(vif);
1950 	struct ath12k_link_vif *arvif = &ahvif->deflink;
1951 
1952 	if (vif->type != NL80211_IFTYPE_STATION || !arvif->is_created)
1953 		return;
1954 
1955 	if (!ether_addr_equal(mgmt->bssid, vif->bss_conf.bssid))
1956 		return;
1957 
1958 	cancel_delayed_work(&arvif->connection_loss_work);
1959 }
1960 
1961 void ath12k_mac_handle_beacon(struct ath12k *ar, struct sk_buff *skb)
1962 {
1963 	ieee80211_iterate_active_interfaces_atomic(ath12k_ar_to_hw(ar),
1964 						   IEEE80211_IFACE_ITER_NORMAL,
1965 						   ath12k_mac_handle_beacon_iter,
1966 						   skb);
1967 }
1968 
1969 void ath12k_mac_handle_beacon_miss(struct ath12k *ar,
1970 				   struct ath12k_link_vif *arvif)
1971 {
1972 	struct ieee80211_hw *hw = ath12k_ar_to_hw(ar);
1973 	struct ieee80211_vif *vif = ath12k_ahvif_to_vif(arvif->ahvif);
1974 
1975 	if (!(arvif->is_created && arvif->is_up))
1976 		return;
1977 
1978 	ieee80211_beacon_loss(vif);
1979 
1980 	/* Firmware doesn't report beacon loss events repeatedly. If AP probe
1981 	 * (done by mac80211) succeeds but beacons do not resume then it
1982 	 * doesn't make sense to continue operation. Queue connection loss work
1983 	 * which can be cancelled when beacon is received.
1984 	 */
1985 	ieee80211_queue_delayed_work(hw, &arvif->connection_loss_work,
1986 				     ATH12K_CONNECTION_LOSS_HZ);
1987 }
1988 
1989 static void ath12k_mac_vif_sta_connection_loss_work(struct work_struct *work)
1990 {
1991 	struct ath12k_link_vif *arvif = container_of(work, struct ath12k_link_vif,
1992 						     connection_loss_work.work);
1993 	struct ieee80211_vif *vif = arvif->ahvif->vif;
1994 
1995 	if (!arvif->is_up)
1996 		return;
1997 
1998 	ieee80211_connection_loss(vif);
1999 }
2000 
2001 static void ath12k_peer_assoc_h_basic(struct ath12k *ar,
2002 				      struct ath12k_link_vif *arvif,
2003 				      struct ath12k_link_sta *arsta,
2004 				      struct ath12k_wmi_peer_assoc_arg *arg)
2005 {
2006 	struct ieee80211_vif *vif = ath12k_ahvif_to_vif(arvif->ahvif);
2007 	struct ieee80211_sta *sta = ath12k_ahsta_to_sta(arsta->ahsta);
2008 	struct ieee80211_hw *hw = ath12k_ar_to_hw(ar);
2009 	struct ieee80211_bss_conf *bss_conf;
2010 	u32 aid;
2011 
2012 	lockdep_assert_wiphy(hw->wiphy);
2013 
2014 	if (vif->type == NL80211_IFTYPE_STATION)
2015 		aid = vif->cfg.aid;
2016 	else
2017 		aid = sta->aid;
2018 
2019 	ether_addr_copy(arg->peer_mac, arsta->addr);
2020 	arg->vdev_id = arvif->vdev_id;
2021 	arg->peer_associd = aid;
2022 	arg->auth_flag = true;
2023 	/* TODO: STA WAR in ath10k for listen interval required? */
2024 	arg->peer_listen_intval = hw->conf.listen_interval;
2025 	arg->peer_nss = 1;
2026 
2027 	bss_conf = ath12k_mac_get_link_bss_conf(arvif);
2028 	if (!bss_conf) {
2029 		ath12k_warn(ar->ab, "unable to access bss link conf in peer assoc for vif %pM link %u\n",
2030 			    vif->addr, arvif->link_id);
2031 		return;
2032 	}
2033 
2034 	arg->peer_caps = bss_conf->assoc_capability;
2035 }
2036 
2037 static void ath12k_peer_assoc_h_crypto(struct ath12k *ar,
2038 				       struct ath12k_link_vif *arvif,
2039 				       struct ath12k_link_sta *arsta,
2040 				       struct ath12k_wmi_peer_assoc_arg *arg)
2041 {
2042 	struct ieee80211_vif *vif = ath12k_ahvif_to_vif(arvif->ahvif);
2043 	struct ieee80211_sta *sta = ath12k_ahsta_to_sta(arsta->ahsta);
2044 	struct ieee80211_bss_conf *info;
2045 	struct cfg80211_chan_def def;
2046 	struct cfg80211_bss *bss;
2047 	struct ieee80211_hw *hw = ath12k_ar_to_hw(ar);
2048 	const u8 *rsnie = NULL;
2049 	const u8 *wpaie = NULL;
2050 
2051 	lockdep_assert_wiphy(hw->wiphy);
2052 
2053 	info = ath12k_mac_get_link_bss_conf(arvif);
2054 	if (!info) {
2055 		ath12k_warn(ar->ab, "unable to access bss link conf for peer assoc crypto for vif %pM link %u\n",
2056 			    vif->addr, arvif->link_id);
2057 		return;
2058 	}
2059 
2060 	if (WARN_ON(ath12k_mac_vif_link_chan(vif, arvif->link_id, &def)))
2061 		return;
2062 
2063 	bss = cfg80211_get_bss(hw->wiphy, def.chan, info->bssid, NULL, 0,
2064 			       IEEE80211_BSS_TYPE_ANY, IEEE80211_PRIVACY_ANY);
2065 
2066 	if (arvif->rsnie_present || arvif->wpaie_present) {
2067 		arg->need_ptk_4_way = true;
2068 		if (arvif->wpaie_present)
2069 			arg->need_gtk_2_way = true;
2070 	} else if (bss) {
2071 		const struct cfg80211_bss_ies *ies;
2072 
2073 		rcu_read_lock();
2074 		rsnie = ieee80211_bss_get_ie(bss, WLAN_EID_RSN);
2075 
2076 		ies = rcu_dereference(bss->ies);
2077 
2078 		wpaie = cfg80211_find_vendor_ie(WLAN_OUI_MICROSOFT,
2079 						WLAN_OUI_TYPE_MICROSOFT_WPA,
2080 						ies->data,
2081 						ies->len);
2082 		rcu_read_unlock();
2083 		cfg80211_put_bss(hw->wiphy, bss);
2084 	}
2085 
2086 	/* FIXME: base on RSN IE/WPA IE is a correct idea? */
2087 	if (rsnie || wpaie) {
2088 		ath12k_dbg(ar->ab, ATH12K_DBG_WMI,
2089 			   "%s: rsn ie found\n", __func__);
2090 		arg->need_ptk_4_way = true;
2091 	}
2092 
2093 	if (wpaie) {
2094 		ath12k_dbg(ar->ab, ATH12K_DBG_WMI,
2095 			   "%s: wpa ie found\n", __func__);
2096 		arg->need_gtk_2_way = true;
2097 	}
2098 
2099 	if (sta->mfp) {
2100 		/* TODO: Need to check if FW supports PMF? */
2101 		arg->is_pmf_enabled = true;
2102 	}
2103 
2104 	/* TODO: safe_mode_enabled (bypass 4-way handshake) flag req? */
2105 }
2106 
2107 static void ath12k_peer_assoc_h_rates(struct ath12k *ar,
2108 				      struct ath12k_link_vif *arvif,
2109 				      struct ath12k_link_sta *arsta,
2110 				      struct ath12k_wmi_peer_assoc_arg *arg)
2111 {
2112 	struct ieee80211_vif *vif = ath12k_ahvif_to_vif(arvif->ahvif);
2113 	struct ieee80211_sta *sta = ath12k_ahsta_to_sta(arsta->ahsta);
2114 	struct wmi_rate_set_arg *rateset = &arg->peer_legacy_rates;
2115 	struct ieee80211_link_sta *link_sta;
2116 	struct cfg80211_chan_def def;
2117 	const struct ieee80211_supported_band *sband;
2118 	const struct ieee80211_rate *rates;
2119 	struct ieee80211_hw *hw = ath12k_ar_to_hw(ar);
2120 	enum nl80211_band band;
2121 	u32 ratemask;
2122 	u8 rate;
2123 	int i;
2124 
2125 	lockdep_assert_wiphy(hw->wiphy);
2126 
2127 	if (WARN_ON(ath12k_mac_vif_link_chan(vif, arvif->link_id, &def)))
2128 		return;
2129 
2130 	link_sta = ath12k_mac_get_link_sta(arsta);
2131 	if (!link_sta) {
2132 		ath12k_warn(ar->ab, "unable to access link sta in peer assoc rates for sta %pM link %u\n",
2133 			    sta->addr, arsta->link_id);
2134 		return;
2135 	}
2136 
2137 	band = def.chan->band;
2138 	sband = hw->wiphy->bands[band];
2139 	ratemask = link_sta->supp_rates[band];
2140 	ratemask &= arvif->bitrate_mask.control[band].legacy;
2141 	rates = sband->bitrates;
2142 
2143 	rateset->num_rates = 0;
2144 
2145 	for (i = 0; i < 32; i++, ratemask >>= 1, rates++) {
2146 		if (!(ratemask & 1))
2147 			continue;
2148 
2149 		rate = ath12k_mac_bitrate_to_rate(rates->bitrate);
2150 		rateset->rates[rateset->num_rates] = rate;
2151 		rateset->num_rates++;
2152 	}
2153 }
2154 
2155 static bool
2156 ath12k_peer_assoc_h_ht_masked(const u8 *ht_mcs_mask)
2157 {
2158 	int nss;
2159 
2160 	for (nss = 0; nss < IEEE80211_HT_MCS_MASK_LEN; nss++)
2161 		if (ht_mcs_mask[nss])
2162 			return false;
2163 
2164 	return true;
2165 }
2166 
2167 static bool
2168 ath12k_peer_assoc_h_vht_masked(const u16 *vht_mcs_mask)
2169 {
2170 	int nss;
2171 
2172 	for (nss = 0; nss < NL80211_VHT_NSS_MAX; nss++)
2173 		if (vht_mcs_mask[nss])
2174 			return false;
2175 
2176 	return true;
2177 }
2178 
2179 static void ath12k_peer_assoc_h_ht(struct ath12k *ar,
2180 				   struct ath12k_link_vif *arvif,
2181 				   struct ath12k_link_sta *arsta,
2182 				   struct ath12k_wmi_peer_assoc_arg *arg)
2183 {
2184 	struct ieee80211_vif *vif = ath12k_ahvif_to_vif(arvif->ahvif);
2185 	struct ieee80211_sta *sta = ath12k_ahsta_to_sta(arsta->ahsta);
2186 	const struct ieee80211_sta_ht_cap *ht_cap;
2187 	struct ieee80211_link_sta *link_sta;
2188 	struct cfg80211_chan_def def;
2189 	enum nl80211_band band;
2190 	const u8 *ht_mcs_mask;
2191 	int i, n;
2192 	u8 max_nss;
2193 	u32 stbc;
2194 
2195 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
2196 
2197 	if (WARN_ON(ath12k_mac_vif_link_chan(vif, arvif->link_id, &def)))
2198 		return;
2199 
2200 	link_sta = ath12k_mac_get_link_sta(arsta);
2201 	if (!link_sta) {
2202 		ath12k_warn(ar->ab, "unable to access link sta in peer assoc ht for sta %pM link %u\n",
2203 			    sta->addr, arsta->link_id);
2204 		return;
2205 	}
2206 
2207 	ht_cap = &link_sta->ht_cap;
2208 	if (!ht_cap->ht_supported)
2209 		return;
2210 
2211 	band = def.chan->band;
2212 	ht_mcs_mask = arvif->bitrate_mask.control[band].ht_mcs;
2213 
2214 	if (ath12k_peer_assoc_h_ht_masked(ht_mcs_mask))
2215 		return;
2216 
2217 	arg->ht_flag = true;
2218 
2219 	arg->peer_max_mpdu = (1 << (IEEE80211_HT_MAX_AMPDU_FACTOR +
2220 				    ht_cap->ampdu_factor)) - 1;
2221 
2222 	arg->peer_mpdu_density =
2223 		ath12k_parse_mpdudensity(ht_cap->ampdu_density);
2224 
2225 	arg->peer_ht_caps = ht_cap->cap;
2226 	arg->peer_rate_caps |= WMI_HOST_RC_HT_FLAG;
2227 
2228 	if (ht_cap->cap & IEEE80211_HT_CAP_LDPC_CODING)
2229 		arg->ldpc_flag = true;
2230 
2231 	if (link_sta->bandwidth >= IEEE80211_STA_RX_BW_40) {
2232 		arg->bw_40 = true;
2233 		arg->peer_rate_caps |= WMI_HOST_RC_CW40_FLAG;
2234 	}
2235 
2236 	/* As firmware handles these two flags (IEEE80211_HT_CAP_SGI_20
2237 	 * and IEEE80211_HT_CAP_SGI_40) for enabling SGI, reset both
2238 	 * flags if guard interval is to force Long GI
2239 	 */
2240 	if (arvif->bitrate_mask.control[band].gi == NL80211_TXRATE_FORCE_LGI) {
2241 		arg->peer_ht_caps &= ~(IEEE80211_HT_CAP_SGI_20 | IEEE80211_HT_CAP_SGI_40);
2242 	} else {
2243 		/* Enable SGI flag if either SGI_20 or SGI_40 is supported */
2244 		if (ht_cap->cap & (IEEE80211_HT_CAP_SGI_20 | IEEE80211_HT_CAP_SGI_40))
2245 			arg->peer_rate_caps |= WMI_HOST_RC_SGI_FLAG;
2246 	}
2247 
2248 	if (ht_cap->cap & IEEE80211_HT_CAP_TX_STBC) {
2249 		arg->peer_rate_caps |= WMI_HOST_RC_TX_STBC_FLAG;
2250 		arg->stbc_flag = true;
2251 	}
2252 
2253 	if (ht_cap->cap & IEEE80211_HT_CAP_RX_STBC) {
2254 		stbc = ht_cap->cap & IEEE80211_HT_CAP_RX_STBC;
2255 		stbc = stbc >> IEEE80211_HT_CAP_RX_STBC_SHIFT;
2256 		stbc = stbc << WMI_HOST_RC_RX_STBC_FLAG_S;
2257 		arg->peer_rate_caps |= stbc;
2258 		arg->stbc_flag = true;
2259 	}
2260 
2261 	if (ht_cap->mcs.rx_mask[1] && ht_cap->mcs.rx_mask[2])
2262 		arg->peer_rate_caps |= WMI_HOST_RC_TS_FLAG;
2263 	else if (ht_cap->mcs.rx_mask[1])
2264 		arg->peer_rate_caps |= WMI_HOST_RC_DS_FLAG;
2265 
2266 	for (i = 0, n = 0, max_nss = 0; i < IEEE80211_HT_MCS_MASK_LEN * 8; i++)
2267 		if ((ht_cap->mcs.rx_mask[i / 8] & BIT(i % 8)) &&
2268 		    (ht_mcs_mask[i / 8] & BIT(i % 8))) {
2269 			max_nss = (i / 8) + 1;
2270 			arg->peer_ht_rates.rates[n++] = i;
2271 		}
2272 
2273 	/* This is a workaround for HT-enabled STAs which break the spec
2274 	 * and have no HT capabilities RX mask (no HT RX MCS map).
2275 	 *
2276 	 * As per spec, in section 20.3.5 Modulation and coding scheme (MCS),
2277 	 * MCS 0 through 7 are mandatory in 20MHz with 800 ns GI at all STAs.
2278 	 *
2279 	 * Firmware asserts if such situation occurs.
2280 	 */
2281 	if (n == 0) {
2282 		arg->peer_ht_rates.num_rates = 8;
2283 		for (i = 0; i < arg->peer_ht_rates.num_rates; i++)
2284 			arg->peer_ht_rates.rates[i] = i;
2285 	} else {
2286 		arg->peer_ht_rates.num_rates = n;
2287 		arg->peer_nss = min(link_sta->rx_nss, max_nss);
2288 	}
2289 
2290 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac ht peer %pM mcs cnt %d nss %d\n",
2291 		   arg->peer_mac,
2292 		   arg->peer_ht_rates.num_rates,
2293 		   arg->peer_nss);
2294 }
2295 
2296 static int ath12k_mac_get_max_vht_mcs_map(u16 mcs_map, int nss)
2297 {
2298 	switch ((mcs_map >> (2 * nss)) & 0x3) {
2299 	case IEEE80211_VHT_MCS_SUPPORT_0_7: return BIT(8) - 1;
2300 	case IEEE80211_VHT_MCS_SUPPORT_0_8: return BIT(9) - 1;
2301 	case IEEE80211_VHT_MCS_SUPPORT_0_9: return BIT(10) - 1;
2302 	}
2303 	return 0;
2304 }
2305 
2306 static u16
2307 ath12k_peer_assoc_h_vht_limit(u16 tx_mcs_set,
2308 			      const u16 vht_mcs_limit[NL80211_VHT_NSS_MAX])
2309 {
2310 	int idx_limit;
2311 	int nss;
2312 	u16 mcs_map;
2313 	u16 mcs;
2314 
2315 	for (nss = 0; nss < NL80211_VHT_NSS_MAX; nss++) {
2316 		mcs_map = ath12k_mac_get_max_vht_mcs_map(tx_mcs_set, nss) &
2317 			  vht_mcs_limit[nss];
2318 
2319 		if (mcs_map)
2320 			idx_limit = fls(mcs_map) - 1;
2321 		else
2322 			idx_limit = -1;
2323 
2324 		switch (idx_limit) {
2325 		case 0:
2326 		case 1:
2327 		case 2:
2328 		case 3:
2329 		case 4:
2330 		case 5:
2331 		case 6:
2332 		case 7:
2333 			mcs = IEEE80211_VHT_MCS_SUPPORT_0_7;
2334 			break;
2335 		case 8:
2336 			mcs = IEEE80211_VHT_MCS_SUPPORT_0_8;
2337 			break;
2338 		case 9:
2339 			mcs = IEEE80211_VHT_MCS_SUPPORT_0_9;
2340 			break;
2341 		default:
2342 			WARN_ON(1);
2343 			fallthrough;
2344 		case -1:
2345 			mcs = IEEE80211_VHT_MCS_NOT_SUPPORTED;
2346 			break;
2347 		}
2348 
2349 		tx_mcs_set &= ~(0x3 << (nss * 2));
2350 		tx_mcs_set |= mcs << (nss * 2);
2351 	}
2352 
2353 	return tx_mcs_set;
2354 }
2355 
2356 static u8 ath12k_get_nss_160mhz(struct ath12k *ar,
2357 				u8 max_nss)
2358 {
2359 	u8 nss_ratio_info = ar->pdev->cap.nss_ratio_info;
2360 	u8 max_sup_nss = 0;
2361 
2362 	switch (nss_ratio_info) {
2363 	case WMI_NSS_RATIO_1BY2_NSS:
2364 		max_sup_nss = max_nss >> 1;
2365 		break;
2366 	case WMI_NSS_RATIO_3BY4_NSS:
2367 		ath12k_warn(ar->ab, "WMI_NSS_RATIO_3BY4_NSS not supported\n");
2368 		break;
2369 	case WMI_NSS_RATIO_1_NSS:
2370 		max_sup_nss = max_nss;
2371 		break;
2372 	case WMI_NSS_RATIO_2_NSS:
2373 		ath12k_warn(ar->ab, "WMI_NSS_RATIO_2_NSS not supported\n");
2374 		break;
2375 	default:
2376 		ath12k_warn(ar->ab, "invalid nss ratio received from fw: %d\n",
2377 			    nss_ratio_info);
2378 		break;
2379 	}
2380 
2381 	return max_sup_nss;
2382 }
2383 
2384 static void ath12k_peer_assoc_h_vht(struct ath12k *ar,
2385 				    struct ath12k_link_vif *arvif,
2386 				    struct ath12k_link_sta *arsta,
2387 				    struct ath12k_wmi_peer_assoc_arg *arg)
2388 {
2389 	struct ieee80211_vif *vif = ath12k_ahvif_to_vif(arvif->ahvif);
2390 	struct ieee80211_sta *sta = ath12k_ahsta_to_sta(arsta->ahsta);
2391 	const struct ieee80211_sta_vht_cap *vht_cap;
2392 	struct ieee80211_link_sta *link_sta;
2393 	struct cfg80211_chan_def def;
2394 	enum nl80211_band band;
2395 	u16 *vht_mcs_mask;
2396 	u8 ampdu_factor;
2397 	u8 max_nss, vht_mcs;
2398 	int i, vht_nss, nss_idx;
2399 	bool user_rate_valid = true;
2400 	u32 rx_nss, tx_nss, nss_160;
2401 
2402 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
2403 
2404 	if (WARN_ON(ath12k_mac_vif_link_chan(vif, arvif->link_id, &def)))
2405 		return;
2406 
2407 	link_sta = ath12k_mac_get_link_sta(arsta);
2408 	if (!link_sta) {
2409 		ath12k_warn(ar->ab, "unable to access link sta in peer assoc vht for sta %pM link %u\n",
2410 			    sta->addr, arsta->link_id);
2411 		return;
2412 	}
2413 
2414 	vht_cap = &link_sta->vht_cap;
2415 	if (!vht_cap->vht_supported)
2416 		return;
2417 
2418 	band = def.chan->band;
2419 	vht_mcs_mask = arvif->bitrate_mask.control[band].vht_mcs;
2420 
2421 	if (ath12k_peer_assoc_h_vht_masked(vht_mcs_mask))
2422 		return;
2423 
2424 	arg->vht_flag = true;
2425 
2426 	/* TODO: similar flags required? */
2427 	arg->vht_capable = true;
2428 
2429 	if (def.chan->band == NL80211_BAND_2GHZ)
2430 		arg->vht_ng_flag = true;
2431 
2432 	arg->peer_vht_caps = vht_cap->cap;
2433 
2434 	ampdu_factor = (vht_cap->cap &
2435 			IEEE80211_VHT_CAP_MAX_A_MPDU_LENGTH_EXPONENT_MASK) >>
2436 		       IEEE80211_VHT_CAP_MAX_A_MPDU_LENGTH_EXPONENT_SHIFT;
2437 
2438 	/* Workaround: Some Netgear/Linksys 11ac APs set Rx A-MPDU factor to
2439 	 * zero in VHT IE. Using it would result in degraded throughput.
2440 	 * arg->peer_max_mpdu at this point contains HT max_mpdu so keep
2441 	 * it if VHT max_mpdu is smaller.
2442 	 */
2443 	arg->peer_max_mpdu = max(arg->peer_max_mpdu,
2444 				 (1U << (IEEE80211_HT_MAX_AMPDU_FACTOR +
2445 					ampdu_factor)) - 1);
2446 
2447 	if (link_sta->bandwidth == IEEE80211_STA_RX_BW_80)
2448 		arg->bw_80 = true;
2449 
2450 	if (link_sta->bandwidth == IEEE80211_STA_RX_BW_160)
2451 		arg->bw_160 = true;
2452 
2453 	vht_nss =  ath12k_mac_max_vht_nss(vht_mcs_mask);
2454 
2455 	if (vht_nss > link_sta->rx_nss) {
2456 		user_rate_valid = false;
2457 		for (nss_idx = link_sta->rx_nss - 1; nss_idx >= 0; nss_idx--) {
2458 			if (vht_mcs_mask[nss_idx]) {
2459 				user_rate_valid = true;
2460 				break;
2461 			}
2462 		}
2463 	}
2464 
2465 	if (!user_rate_valid) {
2466 		ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
2467 			   "Setting vht range MCS value to peer supported nss:%d for peer %pM\n",
2468 			   link_sta->rx_nss, arsta->addr);
2469 		vht_mcs_mask[link_sta->rx_nss - 1] = vht_mcs_mask[vht_nss - 1];
2470 	}
2471 
2472 	/* Calculate peer NSS capability from VHT capabilities if STA
2473 	 * supports VHT.
2474 	 */
2475 	for (i = 0, max_nss = 0, vht_mcs = 0; i < NL80211_VHT_NSS_MAX; i++) {
2476 		vht_mcs = __le16_to_cpu(vht_cap->vht_mcs.rx_mcs_map) >>
2477 			  (2 * i) & 3;
2478 
2479 		if (vht_mcs != IEEE80211_VHT_MCS_NOT_SUPPORTED &&
2480 		    vht_mcs_mask[i])
2481 			max_nss = i + 1;
2482 	}
2483 	arg->peer_nss = min(link_sta->rx_nss, max_nss);
2484 	arg->rx_max_rate = __le16_to_cpu(vht_cap->vht_mcs.rx_highest);
2485 	arg->rx_mcs_set = __le16_to_cpu(vht_cap->vht_mcs.rx_mcs_map);
2486 	arg->rx_mcs_set = ath12k_peer_assoc_h_vht_limit(arg->rx_mcs_set, vht_mcs_mask);
2487 
2488 	arg->tx_max_rate = __le16_to_cpu(vht_cap->vht_mcs.tx_highest);
2489 	arg->tx_mcs_set = __le16_to_cpu(vht_cap->vht_mcs.tx_mcs_map);
2490 
2491 	/* In QCN9274 platform, VHT MCS rate 10 and 11 is enabled by default.
2492 	 * VHT MCS rate 10 and 11 is not supported in 11ac standard.
2493 	 * so explicitly disable the VHT MCS rate 10 and 11 in 11ac mode.
2494 	 */
2495 	arg->tx_mcs_set &= ~IEEE80211_VHT_MCS_SUPPORT_0_11_MASK;
2496 	arg->tx_mcs_set |= IEEE80211_DISABLE_VHT_MCS_SUPPORT_0_11;
2497 
2498 	if ((arg->tx_mcs_set & IEEE80211_VHT_MCS_NOT_SUPPORTED) ==
2499 			IEEE80211_VHT_MCS_NOT_SUPPORTED)
2500 		arg->peer_vht_caps &= ~IEEE80211_VHT_CAP_MU_BEAMFORMEE_CAPABLE;
2501 
2502 	/* TODO:  Check */
2503 	arg->tx_max_mcs_nss = 0xFF;
2504 
2505 	if (arg->peer_phymode == MODE_11AC_VHT160) {
2506 		tx_nss = ath12k_get_nss_160mhz(ar, max_nss);
2507 		rx_nss = min(arg->peer_nss, tx_nss);
2508 		arg->peer_bw_rxnss_override = ATH12K_BW_NSS_MAP_ENABLE;
2509 
2510 		if (!rx_nss) {
2511 			ath12k_warn(ar->ab, "invalid max_nss\n");
2512 			return;
2513 		}
2514 
2515 		nss_160 = u32_encode_bits(rx_nss - 1, ATH12K_PEER_RX_NSS_160MHZ);
2516 		arg->peer_bw_rxnss_override |= nss_160;
2517 	}
2518 
2519 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
2520 		   "mac vht peer %pM max_mpdu %d flags 0x%x nss_override 0x%x\n",
2521 		   arsta->addr, arg->peer_max_mpdu, arg->peer_flags,
2522 		   arg->peer_bw_rxnss_override);
2523 }
2524 
2525 static int ath12k_mac_get_max_he_mcs_map(u16 mcs_map, int nss)
2526 {
2527 	switch ((mcs_map >> (2 * nss)) & 0x3) {
2528 	case IEEE80211_HE_MCS_SUPPORT_0_7: return BIT(8) - 1;
2529 	case IEEE80211_HE_MCS_SUPPORT_0_9: return BIT(10) - 1;
2530 	case IEEE80211_HE_MCS_SUPPORT_0_11: return BIT(12) - 1;
2531 	}
2532 	return 0;
2533 }
2534 
2535 static u16 ath12k_peer_assoc_h_he_limit(u16 tx_mcs_set,
2536 					const u16 *he_mcs_limit)
2537 {
2538 	int idx_limit;
2539 	int nss;
2540 	u16 mcs_map;
2541 	u16 mcs;
2542 
2543 	for (nss = 0; nss < NL80211_HE_NSS_MAX; nss++) {
2544 		mcs_map = ath12k_mac_get_max_he_mcs_map(tx_mcs_set, nss) &
2545 			he_mcs_limit[nss];
2546 
2547 		if (mcs_map)
2548 			idx_limit = fls(mcs_map) - 1;
2549 		else
2550 			idx_limit = -1;
2551 
2552 		switch (idx_limit) {
2553 		case 0 ... 7:
2554 			mcs = IEEE80211_HE_MCS_SUPPORT_0_7;
2555 			break;
2556 		case 8:
2557 		case 9:
2558 			mcs = IEEE80211_HE_MCS_SUPPORT_0_9;
2559 			break;
2560 		case 10:
2561 		case 11:
2562 			mcs = IEEE80211_HE_MCS_SUPPORT_0_11;
2563 			break;
2564 		default:
2565 			WARN_ON(1);
2566 			fallthrough;
2567 		case -1:
2568 			mcs = IEEE80211_HE_MCS_NOT_SUPPORTED;
2569 			break;
2570 		}
2571 
2572 		tx_mcs_set &= ~(0x3 << (nss * 2));
2573 		tx_mcs_set |= mcs << (nss * 2);
2574 	}
2575 
2576 	return tx_mcs_set;
2577 }
2578 
2579 static bool
2580 ath12k_peer_assoc_h_he_masked(const u16 he_mcs_mask[NL80211_HE_NSS_MAX])
2581 {
2582 	int nss;
2583 
2584 	for (nss = 0; nss < NL80211_HE_NSS_MAX; nss++)
2585 		if (he_mcs_mask[nss])
2586 			return false;
2587 
2588 	return true;
2589 }
2590 
2591 static void ath12k_peer_assoc_h_he(struct ath12k *ar,
2592 				   struct ath12k_link_vif *arvif,
2593 				   struct ath12k_link_sta *arsta,
2594 				   struct ath12k_wmi_peer_assoc_arg *arg)
2595 {
2596 	struct ieee80211_vif *vif = ath12k_ahvif_to_vif(arvif->ahvif);
2597 	struct ieee80211_sta *sta = ath12k_ahsta_to_sta(arsta->ahsta);
2598 	const struct ieee80211_sta_he_cap *he_cap;
2599 	struct ieee80211_bss_conf *link_conf;
2600 	struct ieee80211_link_sta *link_sta;
2601 	struct cfg80211_chan_def def;
2602 	int i;
2603 	u8 ampdu_factor, max_nss;
2604 	u8 rx_mcs_80 = IEEE80211_HE_MCS_NOT_SUPPORTED;
2605 	u8 rx_mcs_160 = IEEE80211_HE_MCS_NOT_SUPPORTED;
2606 	u16 mcs_160_map, mcs_80_map;
2607 	u8 link_id = arvif->link_id;
2608 	bool support_160;
2609 	enum nl80211_band band;
2610 	u16 *he_mcs_mask;
2611 	u8 he_mcs;
2612 	u16 he_tx_mcs = 0, v = 0;
2613 	int he_nss, nss_idx;
2614 	bool user_rate_valid = true;
2615 	u32 rx_nss, tx_nss, nss_160;
2616 
2617 	if (WARN_ON(ath12k_mac_vif_link_chan(vif, link_id, &def)))
2618 		return;
2619 
2620 	link_conf = ath12k_mac_get_link_bss_conf(arvif);
2621 	if (!link_conf) {
2622 		ath12k_warn(ar->ab, "unable to access bss link conf in peer assoc he for vif %pM link %u",
2623 			    vif->addr, link_id);
2624 		return;
2625 	}
2626 
2627 	link_sta = ath12k_mac_get_link_sta(arsta);
2628 	if (!link_sta) {
2629 		ath12k_warn(ar->ab, "unable to access link sta in peer assoc he for sta %pM link %u\n",
2630 			    sta->addr, arsta->link_id);
2631 		return;
2632 	}
2633 
2634 	he_cap = &link_sta->he_cap;
2635 	if (!he_cap->has_he)
2636 		return;
2637 
2638 	band = def.chan->band;
2639 	he_mcs_mask = arvif->bitrate_mask.control[band].he_mcs;
2640 
2641 	if (ath12k_peer_assoc_h_he_masked(he_mcs_mask))
2642 		return;
2643 
2644 	arg->he_flag = true;
2645 
2646 	support_160 = !!(he_cap->he_cap_elem.phy_cap_info[0] &
2647 		  IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_160MHZ_IN_5G);
2648 
2649 	/* Supported HE-MCS and NSS Set of peer he_cap is intersection with self he_cp */
2650 	mcs_160_map = le16_to_cpu(he_cap->he_mcs_nss_supp.rx_mcs_160);
2651 	mcs_80_map = le16_to_cpu(he_cap->he_mcs_nss_supp.rx_mcs_80);
2652 
2653 	if (support_160) {
2654 		for (i = 7; i >= 0; i--) {
2655 			u8 mcs_160 = (mcs_160_map >> (2 * i)) & 3;
2656 
2657 			if (mcs_160 != IEEE80211_HE_MCS_NOT_SUPPORTED) {
2658 				rx_mcs_160 = i + 1;
2659 				break;
2660 			}
2661 		}
2662 	}
2663 
2664 	for (i = 7; i >= 0; i--) {
2665 		u8 mcs_80 = (mcs_80_map >> (2 * i)) & 3;
2666 
2667 		if (mcs_80 != IEEE80211_HE_MCS_NOT_SUPPORTED) {
2668 			rx_mcs_80 = i + 1;
2669 			break;
2670 		}
2671 	}
2672 
2673 	if (support_160)
2674 		max_nss = min(rx_mcs_80, rx_mcs_160);
2675 	else
2676 		max_nss = rx_mcs_80;
2677 
2678 	arg->peer_nss = min(link_sta->rx_nss, max_nss);
2679 
2680 	memcpy(&arg->peer_he_cap_macinfo, he_cap->he_cap_elem.mac_cap_info,
2681 	       sizeof(he_cap->he_cap_elem.mac_cap_info));
2682 	memcpy(&arg->peer_he_cap_phyinfo, he_cap->he_cap_elem.phy_cap_info,
2683 	       sizeof(he_cap->he_cap_elem.phy_cap_info));
2684 	arg->peer_he_ops = link_conf->he_oper.params;
2685 
2686 	/* the top most byte is used to indicate BSS color info */
2687 	arg->peer_he_ops &= 0xffffff;
2688 
2689 	/* As per section 26.6.1 IEEE Std 802.11ax‐2022, if the Max AMPDU
2690 	 * Exponent Extension in HE cap is zero, use the arg->peer_max_mpdu
2691 	 * as calculated while parsing VHT caps(if VHT caps is present)
2692 	 * or HT caps (if VHT caps is not present).
2693 	 *
2694 	 * For non-zero value of Max AMPDU Exponent Extension in HE MAC caps,
2695 	 * if a HE STA sends VHT cap and HE cap IE in assoc request then, use
2696 	 * MAX_AMPDU_LEN_FACTOR as 20 to calculate max_ampdu length.
2697 	 * If a HE STA that does not send VHT cap, but HE and HT cap in assoc
2698 	 * request, then use MAX_AMPDU_LEN_FACTOR as 16 to calculate max_ampdu
2699 	 * length.
2700 	 */
2701 	ampdu_factor = u8_get_bits(he_cap->he_cap_elem.mac_cap_info[3],
2702 				   IEEE80211_HE_MAC_CAP3_MAX_AMPDU_LEN_EXP_MASK);
2703 
2704 	if (ampdu_factor) {
2705 		if (link_sta->vht_cap.vht_supported)
2706 			arg->peer_max_mpdu = (1 << (IEEE80211_HE_VHT_MAX_AMPDU_FACTOR +
2707 						    ampdu_factor)) - 1;
2708 		else if (link_sta->ht_cap.ht_supported)
2709 			arg->peer_max_mpdu = (1 << (IEEE80211_HE_HT_MAX_AMPDU_FACTOR +
2710 						    ampdu_factor)) - 1;
2711 	}
2712 
2713 	if (he_cap->he_cap_elem.phy_cap_info[6] &
2714 	    IEEE80211_HE_PHY_CAP6_PPE_THRESHOLD_PRESENT) {
2715 		int bit = 7;
2716 		int nss, ru;
2717 
2718 		arg->peer_ppet.numss_m1 = he_cap->ppe_thres[0] &
2719 					  IEEE80211_PPE_THRES_NSS_MASK;
2720 		arg->peer_ppet.ru_bit_mask =
2721 			(he_cap->ppe_thres[0] &
2722 			 IEEE80211_PPE_THRES_RU_INDEX_BITMASK_MASK) >>
2723 			IEEE80211_PPE_THRES_RU_INDEX_BITMASK_POS;
2724 
2725 		for (nss = 0; nss <= arg->peer_ppet.numss_m1; nss++) {
2726 			for (ru = 0; ru < 4; ru++) {
2727 				u32 val = 0;
2728 				int i;
2729 
2730 				if ((arg->peer_ppet.ru_bit_mask & BIT(ru)) == 0)
2731 					continue;
2732 				for (i = 0; i < 6; i++) {
2733 					val >>= 1;
2734 					val |= ((he_cap->ppe_thres[bit / 8] >>
2735 						 (bit % 8)) & 0x1) << 5;
2736 					bit++;
2737 				}
2738 				arg->peer_ppet.ppet16_ppet8_ru3_ru0[nss] |=
2739 								val << (ru * 6);
2740 			}
2741 		}
2742 	}
2743 
2744 	if (he_cap->he_cap_elem.mac_cap_info[0] & IEEE80211_HE_MAC_CAP0_TWT_RES)
2745 		arg->twt_responder = true;
2746 	if (he_cap->he_cap_elem.mac_cap_info[0] & IEEE80211_HE_MAC_CAP0_TWT_REQ)
2747 		arg->twt_requester = true;
2748 
2749 	he_nss = ath12k_mac_max_he_nss(he_mcs_mask);
2750 
2751 	if (he_nss > link_sta->rx_nss) {
2752 		user_rate_valid = false;
2753 		for (nss_idx = link_sta->rx_nss - 1; nss_idx >= 0; nss_idx--) {
2754 			if (he_mcs_mask[nss_idx]) {
2755 				user_rate_valid = true;
2756 				break;
2757 			}
2758 		}
2759 	}
2760 
2761 	if (!user_rate_valid) {
2762 		ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
2763 			   "Setting he range MCS value to peer supported nss:%d for peer %pM\n",
2764 			   link_sta->rx_nss, arsta->addr);
2765 		he_mcs_mask[link_sta->rx_nss - 1] = he_mcs_mask[he_nss - 1];
2766 	}
2767 
2768 	switch (link_sta->bandwidth) {
2769 	case IEEE80211_STA_RX_BW_160:
2770 		v = le16_to_cpu(he_cap->he_mcs_nss_supp.rx_mcs_160);
2771 		v = ath12k_peer_assoc_h_he_limit(v, he_mcs_mask);
2772 		arg->peer_he_rx_mcs_set[WMI_HECAP_TXRX_MCS_NSS_IDX_160] = v;
2773 
2774 		v = le16_to_cpu(he_cap->he_mcs_nss_supp.tx_mcs_160);
2775 		arg->peer_he_tx_mcs_set[WMI_HECAP_TXRX_MCS_NSS_IDX_160] = v;
2776 
2777 		arg->peer_he_mcs_count++;
2778 		if (!he_tx_mcs)
2779 			he_tx_mcs = v;
2780 		fallthrough;
2781 
2782 	default:
2783 		v = le16_to_cpu(he_cap->he_mcs_nss_supp.rx_mcs_80);
2784 		v = ath12k_peer_assoc_h_he_limit(v, he_mcs_mask);
2785 		arg->peer_he_rx_mcs_set[WMI_HECAP_TXRX_MCS_NSS_IDX_80] = v;
2786 
2787 		v = le16_to_cpu(he_cap->he_mcs_nss_supp.tx_mcs_80);
2788 		arg->peer_he_tx_mcs_set[WMI_HECAP_TXRX_MCS_NSS_IDX_80] = v;
2789 
2790 		arg->peer_he_mcs_count++;
2791 		if (!he_tx_mcs)
2792 			he_tx_mcs = v;
2793 		break;
2794 	}
2795 
2796 	/* Calculate peer NSS capability from HE capabilities if STA
2797 	 * supports HE.
2798 	 */
2799 	for (i = 0, max_nss = 0, he_mcs = 0; i < NL80211_HE_NSS_MAX; i++) {
2800 		he_mcs = he_tx_mcs >> (2 * i) & 3;
2801 
2802 		/* In case of fixed rates, MCS Range in he_tx_mcs might have
2803 		 * unsupported range, with he_mcs_mask set, so check either of them
2804 		 * to find nss.
2805 		 */
2806 		if (he_mcs != IEEE80211_HE_MCS_NOT_SUPPORTED ||
2807 		    he_mcs_mask[i])
2808 			max_nss = i + 1;
2809 	}
2810 
2811 	max_nss = min(max_nss, ar->num_tx_chains);
2812 	arg->peer_nss = min(link_sta->rx_nss, max_nss);
2813 
2814 	if (arg->peer_phymode == MODE_11AX_HE160) {
2815 		tx_nss = ath12k_get_nss_160mhz(ar, ar->num_tx_chains);
2816 		rx_nss = min(arg->peer_nss, tx_nss);
2817 
2818 		arg->peer_nss = min(link_sta->rx_nss, ar->num_rx_chains);
2819 		arg->peer_bw_rxnss_override = ATH12K_BW_NSS_MAP_ENABLE;
2820 
2821 		if (!rx_nss) {
2822 			ath12k_warn(ar->ab, "invalid max_nss\n");
2823 			return;
2824 		}
2825 
2826 		nss_160 = u32_encode_bits(rx_nss - 1, ATH12K_PEER_RX_NSS_160MHZ);
2827 		arg->peer_bw_rxnss_override |= nss_160;
2828 	}
2829 
2830 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
2831 		   "mac he peer %pM nss %d mcs cnt %d nss_override 0x%x\n",
2832 		   arsta->addr, arg->peer_nss,
2833 		   arg->peer_he_mcs_count,
2834 		   arg->peer_bw_rxnss_override);
2835 }
2836 
2837 static void ath12k_peer_assoc_h_he_6ghz(struct ath12k *ar,
2838 					struct ath12k_link_vif *arvif,
2839 					struct ath12k_link_sta *arsta,
2840 					struct ath12k_wmi_peer_assoc_arg *arg)
2841 {
2842 	struct ieee80211_vif *vif = ath12k_ahvif_to_vif(arvif->ahvif);
2843 	struct ieee80211_sta *sta = ath12k_ahsta_to_sta(arsta->ahsta);
2844 	const struct ieee80211_sta_he_cap *he_cap;
2845 	struct ieee80211_link_sta *link_sta;
2846 	struct cfg80211_chan_def def;
2847 	enum nl80211_band band;
2848 	u8 ampdu_factor, mpdu_density;
2849 
2850 	if (WARN_ON(ath12k_mac_vif_link_chan(vif, arvif->link_id, &def)))
2851 		return;
2852 
2853 	band = def.chan->band;
2854 
2855 	link_sta = ath12k_mac_get_link_sta(arsta);
2856 	if (!link_sta) {
2857 		ath12k_warn(ar->ab, "unable to access link sta in peer assoc he 6ghz for sta %pM link %u\n",
2858 			    sta->addr, arsta->link_id);
2859 		return;
2860 	}
2861 
2862 	he_cap = &link_sta->he_cap;
2863 
2864 	if (!arg->he_flag || band != NL80211_BAND_6GHZ || !link_sta->he_6ghz_capa.capa)
2865 		return;
2866 
2867 	if (link_sta->bandwidth == IEEE80211_STA_RX_BW_40)
2868 		arg->bw_40 = true;
2869 
2870 	if (link_sta->bandwidth == IEEE80211_STA_RX_BW_80)
2871 		arg->bw_80 = true;
2872 
2873 	if (link_sta->bandwidth == IEEE80211_STA_RX_BW_160)
2874 		arg->bw_160 = true;
2875 
2876 	if (link_sta->bandwidth == IEEE80211_STA_RX_BW_320)
2877 		arg->bw_320 = true;
2878 
2879 	arg->peer_he_caps_6ghz = le16_to_cpu(link_sta->he_6ghz_capa.capa);
2880 
2881 	mpdu_density = u32_get_bits(arg->peer_he_caps_6ghz,
2882 				    IEEE80211_HE_6GHZ_CAP_MIN_MPDU_START);
2883 	arg->peer_mpdu_density = ath12k_parse_mpdudensity(mpdu_density);
2884 
2885 	/* From IEEE Std 802.11ax-2021 - Section 10.12.2: An HE STA shall be capable of
2886 	 * receiving A-MPDU where the A-MPDU pre-EOF padding length is up to the value
2887 	 * indicated by the Maximum A-MPDU Length Exponent Extension field in the HE
2888 	 * Capabilities element and the Maximum A-MPDU Length Exponent field in HE 6 GHz
2889 	 * Band Capabilities element in the 6 GHz band.
2890 	 *
2891 	 * Here, we are extracting the Max A-MPDU Exponent Extension from HE caps and
2892 	 * factor is the Maximum A-MPDU Length Exponent from HE 6 GHZ Band capability.
2893 	 */
2894 	ampdu_factor = u8_get_bits(he_cap->he_cap_elem.mac_cap_info[3],
2895 				   IEEE80211_HE_MAC_CAP3_MAX_AMPDU_LEN_EXP_MASK) +
2896 			u32_get_bits(arg->peer_he_caps_6ghz,
2897 				     IEEE80211_HE_6GHZ_CAP_MAX_AMPDU_LEN_EXP);
2898 
2899 	arg->peer_max_mpdu = (1u << (IEEE80211_HE_6GHZ_MAX_AMPDU_FACTOR +
2900 				     ampdu_factor)) - 1;
2901 }
2902 
2903 static int ath12k_get_smps_from_capa(const struct ieee80211_sta_ht_cap *ht_cap,
2904 				     const struct ieee80211_he_6ghz_capa *he_6ghz_capa,
2905 				     int *smps)
2906 {
2907 	if (ht_cap->ht_supported)
2908 		*smps = u16_get_bits(ht_cap->cap, IEEE80211_HT_CAP_SM_PS);
2909 	else
2910 		*smps = le16_get_bits(he_6ghz_capa->capa,
2911 				      IEEE80211_HE_6GHZ_CAP_SM_PS);
2912 
2913 	if (*smps >= ARRAY_SIZE(ath12k_smps_map))
2914 		return -EINVAL;
2915 
2916 	return 0;
2917 }
2918 
2919 static void ath12k_peer_assoc_h_smps(struct ath12k_link_sta *arsta,
2920 				     struct ath12k_wmi_peer_assoc_arg *arg)
2921 {
2922 	struct ieee80211_sta *sta = ath12k_ahsta_to_sta(arsta->ahsta);
2923 	const struct ieee80211_he_6ghz_capa *he_6ghz_capa;
2924 	struct ath12k_link_vif *arvif = arsta->arvif;
2925 	const struct ieee80211_sta_ht_cap *ht_cap;
2926 	struct ieee80211_link_sta *link_sta;
2927 	struct ath12k *ar = arvif->ar;
2928 	int smps;
2929 
2930 	link_sta = ath12k_mac_get_link_sta(arsta);
2931 	if (!link_sta) {
2932 		ath12k_warn(ar->ab, "unable to access link sta in peer assoc he for sta %pM link %u\n",
2933 			    sta->addr, arsta->link_id);
2934 		return;
2935 	}
2936 
2937 	he_6ghz_capa = &link_sta->he_6ghz_capa;
2938 	ht_cap = &link_sta->ht_cap;
2939 
2940 	if (!ht_cap->ht_supported && !he_6ghz_capa->capa)
2941 		return;
2942 
2943 	if (ath12k_get_smps_from_capa(ht_cap, he_6ghz_capa, &smps))
2944 		return;
2945 
2946 	switch (smps) {
2947 	case WLAN_HT_CAP_SM_PS_STATIC:
2948 		arg->static_mimops_flag = true;
2949 		break;
2950 	case WLAN_HT_CAP_SM_PS_DYNAMIC:
2951 		arg->dynamic_mimops_flag = true;
2952 		break;
2953 	case WLAN_HT_CAP_SM_PS_DISABLED:
2954 		arg->spatial_mux_flag = true;
2955 		break;
2956 	default:
2957 		break;
2958 	}
2959 }
2960 
2961 static void ath12k_peer_assoc_h_qos(struct ath12k *ar,
2962 				    struct ath12k_link_vif *arvif,
2963 				    struct ath12k_link_sta *arsta,
2964 				    struct ath12k_wmi_peer_assoc_arg *arg)
2965 {
2966 	struct ieee80211_sta *sta = ath12k_ahsta_to_sta(arsta->ahsta);
2967 
2968 	switch (arvif->ahvif->vdev_type) {
2969 	case WMI_VDEV_TYPE_AP:
2970 		if (sta->wme) {
2971 			/* TODO: Check WME vs QoS */
2972 			arg->is_wme_set = true;
2973 			arg->qos_flag = true;
2974 		}
2975 
2976 		if (sta->wme && sta->uapsd_queues) {
2977 			/* TODO: Check WME vs QoS */
2978 			arg->is_wme_set = true;
2979 			arg->apsd_flag = true;
2980 			arg->peer_rate_caps |= WMI_HOST_RC_UAPSD_FLAG;
2981 		}
2982 		break;
2983 	case WMI_VDEV_TYPE_STA:
2984 		if (sta->wme) {
2985 			arg->is_wme_set = true;
2986 			arg->qos_flag = true;
2987 		}
2988 		break;
2989 	default:
2990 		break;
2991 	}
2992 
2993 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac peer %pM qos %d\n",
2994 		   arsta->addr, arg->qos_flag);
2995 }
2996 
2997 static int ath12k_peer_assoc_qos_ap(struct ath12k *ar,
2998 				    struct ath12k_link_vif *arvif,
2999 				    struct ath12k_link_sta *arsta)
3000 {
3001 	struct ieee80211_sta *sta = ath12k_ahsta_to_sta(arsta->ahsta);
3002 	struct ath12k_wmi_ap_ps_arg arg;
3003 	u32 max_sp;
3004 	u32 uapsd;
3005 	int ret;
3006 
3007 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
3008 
3009 	arg.vdev_id = arvif->vdev_id;
3010 
3011 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac uapsd_queues 0x%x max_sp %d\n",
3012 		   sta->uapsd_queues, sta->max_sp);
3013 
3014 	uapsd = 0;
3015 	if (sta->uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_VO)
3016 		uapsd |= WMI_AP_PS_UAPSD_AC3_DELIVERY_EN |
3017 			 WMI_AP_PS_UAPSD_AC3_TRIGGER_EN;
3018 	if (sta->uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_VI)
3019 		uapsd |= WMI_AP_PS_UAPSD_AC2_DELIVERY_EN |
3020 			 WMI_AP_PS_UAPSD_AC2_TRIGGER_EN;
3021 	if (sta->uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_BK)
3022 		uapsd |= WMI_AP_PS_UAPSD_AC1_DELIVERY_EN |
3023 			 WMI_AP_PS_UAPSD_AC1_TRIGGER_EN;
3024 	if (sta->uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_BE)
3025 		uapsd |= WMI_AP_PS_UAPSD_AC0_DELIVERY_EN |
3026 			 WMI_AP_PS_UAPSD_AC0_TRIGGER_EN;
3027 
3028 	max_sp = 0;
3029 	if (sta->max_sp < MAX_WMI_AP_PS_PEER_PARAM_MAX_SP)
3030 		max_sp = sta->max_sp;
3031 
3032 	arg.param = WMI_AP_PS_PEER_PARAM_UAPSD;
3033 	arg.value = uapsd;
3034 	ret = ath12k_wmi_send_set_ap_ps_param_cmd(ar, arsta->addr, &arg);
3035 	if (ret)
3036 		goto err;
3037 
3038 	arg.param = WMI_AP_PS_PEER_PARAM_MAX_SP;
3039 	arg.value = max_sp;
3040 	ret = ath12k_wmi_send_set_ap_ps_param_cmd(ar, arsta->addr, &arg);
3041 	if (ret)
3042 		goto err;
3043 
3044 	/* TODO: revisit during testing */
3045 	arg.param = WMI_AP_PS_PEER_PARAM_SIFS_RESP_FRMTYPE;
3046 	arg.value = DISABLE_SIFS_RESPONSE_TRIGGER;
3047 	ret = ath12k_wmi_send_set_ap_ps_param_cmd(ar, arsta->addr, &arg);
3048 	if (ret)
3049 		goto err;
3050 
3051 	arg.param = WMI_AP_PS_PEER_PARAM_SIFS_RESP_UAPSD;
3052 	arg.value = DISABLE_SIFS_RESPONSE_TRIGGER;
3053 	ret = ath12k_wmi_send_set_ap_ps_param_cmd(ar, arsta->addr, &arg);
3054 	if (ret)
3055 		goto err;
3056 
3057 	return 0;
3058 
3059 err:
3060 	ath12k_warn(ar->ab, "failed to set ap ps peer param %d for vdev %i: %d\n",
3061 		    arg.param, arvif->vdev_id, ret);
3062 	return ret;
3063 }
3064 
3065 static bool ath12k_mac_sta_has_ofdm_only(struct ieee80211_link_sta *sta)
3066 {
3067 	return sta->supp_rates[NL80211_BAND_2GHZ] >>
3068 	       ATH12K_MAC_FIRST_OFDM_RATE_IDX;
3069 }
3070 
3071 static enum wmi_phy_mode ath12k_mac_get_phymode_vht(struct ath12k *ar,
3072 						    struct ieee80211_link_sta *link_sta)
3073 {
3074 	if (link_sta->bandwidth == IEEE80211_STA_RX_BW_160) {
3075 		if (link_sta->vht_cap.cap & (IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_160MHZ |
3076 		    IEEE80211_VHT_CAP_EXT_NSS_BW_MASK))
3077 			return MODE_11AC_VHT160;
3078 
3079 		/* Allow STA to connect even if it does not explicitly advertise 160 MHz
3080 		 * support
3081 		 */
3082 		return MODE_11AC_VHT160;
3083 	}
3084 
3085 	if (link_sta->bandwidth == IEEE80211_STA_RX_BW_80)
3086 		return MODE_11AC_VHT80;
3087 
3088 	if (link_sta->bandwidth == IEEE80211_STA_RX_BW_40)
3089 		return MODE_11AC_VHT40;
3090 
3091 	if (link_sta->bandwidth == IEEE80211_STA_RX_BW_20)
3092 		return MODE_11AC_VHT20;
3093 
3094 	return MODE_UNKNOWN;
3095 }
3096 
3097 static enum wmi_phy_mode ath12k_mac_get_phymode_he(struct ath12k *ar,
3098 						   struct ieee80211_link_sta *link_sta)
3099 {
3100 	if (link_sta->bandwidth == IEEE80211_STA_RX_BW_160) {
3101 		if (link_sta->he_cap.he_cap_elem.phy_cap_info[0] &
3102 		     IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_160MHZ_IN_5G)
3103 			return MODE_11AX_HE160;
3104 
3105 		return MODE_UNKNOWN;
3106 	}
3107 
3108 	if (link_sta->bandwidth == IEEE80211_STA_RX_BW_80)
3109 		return MODE_11AX_HE80;
3110 
3111 	if (link_sta->bandwidth == IEEE80211_STA_RX_BW_40)
3112 		return MODE_11AX_HE40;
3113 
3114 	if (link_sta->bandwidth == IEEE80211_STA_RX_BW_20)
3115 		return MODE_11AX_HE20;
3116 
3117 	return MODE_UNKNOWN;
3118 }
3119 
3120 static enum wmi_phy_mode ath12k_mac_get_phymode_eht(struct ath12k *ar,
3121 						    struct ieee80211_link_sta *link_sta)
3122 {
3123 	if (link_sta->bandwidth == IEEE80211_STA_RX_BW_320)
3124 		if (link_sta->eht_cap.eht_cap_elem.phy_cap_info[0] &
3125 		    IEEE80211_EHT_PHY_CAP0_320MHZ_IN_6GHZ)
3126 			return MODE_11BE_EHT320;
3127 
3128 	if (link_sta->bandwidth == IEEE80211_STA_RX_BW_160) {
3129 		if (link_sta->he_cap.he_cap_elem.phy_cap_info[0] &
3130 		    IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_160MHZ_IN_5G)
3131 			return MODE_11BE_EHT160;
3132 
3133 		ath12k_warn(ar->ab, "invalid EHT PHY capability info for 160 Mhz: %d\n",
3134 			    link_sta->he_cap.he_cap_elem.phy_cap_info[0]);
3135 
3136 		return MODE_UNKNOWN;
3137 	}
3138 
3139 	if (link_sta->bandwidth == IEEE80211_STA_RX_BW_80)
3140 		return MODE_11BE_EHT80;
3141 
3142 	if (link_sta->bandwidth == IEEE80211_STA_RX_BW_40)
3143 		return MODE_11BE_EHT40;
3144 
3145 	if (link_sta->bandwidth == IEEE80211_STA_RX_BW_20)
3146 		return MODE_11BE_EHT20;
3147 
3148 	return MODE_UNKNOWN;
3149 }
3150 
3151 static bool
3152 ath12k_peer_assoc_h_eht_masked(const u16 eht_mcs_mask[NL80211_EHT_NSS_MAX])
3153 {
3154 	int nss;
3155 
3156 	for (nss = 0; nss < NL80211_EHT_NSS_MAX; nss++)
3157 		if (eht_mcs_mask[nss])
3158 			return false;
3159 
3160 	return true;
3161 }
3162 
3163 static void ath12k_peer_assoc_h_phymode(struct ath12k *ar,
3164 					struct ath12k_link_vif *arvif,
3165 					struct ath12k_link_sta *arsta,
3166 					struct ath12k_wmi_peer_assoc_arg *arg)
3167 {
3168 	struct ieee80211_link_sta *link_sta;
3169 	struct cfg80211_chan_def def;
3170 	enum nl80211_band band;
3171 	const u8 *ht_mcs_mask;
3172 	const u16 *vht_mcs_mask;
3173 	const u16 *he_mcs_mask;
3174 	const u16 *eht_mcs_mask;
3175 	enum wmi_phy_mode phymode = MODE_UNKNOWN;
3176 
3177 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
3178 
3179 	struct ieee80211_vif *vif = ath12k_ahvif_to_vif(arvif->ahvif);
3180 	struct ieee80211_sta *sta = ath12k_ahsta_to_sta(arsta->ahsta);
3181 
3182 	if (WARN_ON(ath12k_mac_vif_link_chan(vif, arvif->link_id, &def)))
3183 		return;
3184 
3185 	band = def.chan->band;
3186 	ht_mcs_mask = arvif->bitrate_mask.control[band].ht_mcs;
3187 	vht_mcs_mask = arvif->bitrate_mask.control[band].vht_mcs;
3188 	he_mcs_mask = arvif->bitrate_mask.control[band].he_mcs;
3189 	eht_mcs_mask = arvif->bitrate_mask.control[band].eht_mcs;
3190 
3191 	link_sta = ath12k_mac_get_link_sta(arsta);
3192 	if (!link_sta) {
3193 		ath12k_warn(ar->ab, "unable to access link sta in peer assoc he for sta %pM link %u\n",
3194 			    sta->addr, arsta->link_id);
3195 		return;
3196 	}
3197 
3198 	switch (band) {
3199 	case NL80211_BAND_2GHZ:
3200 		if (link_sta->eht_cap.has_eht &&
3201 		    !ath12k_peer_assoc_h_eht_masked(eht_mcs_mask)) {
3202 			if (link_sta->bandwidth == IEEE80211_STA_RX_BW_40)
3203 				phymode = MODE_11BE_EHT40_2G;
3204 			else
3205 				phymode = MODE_11BE_EHT20_2G;
3206 		} else if (link_sta->he_cap.has_he &&
3207 			   !ath12k_peer_assoc_h_he_masked(he_mcs_mask)) {
3208 			if (link_sta->bandwidth == IEEE80211_STA_RX_BW_80)
3209 				phymode = MODE_11AX_HE80_2G;
3210 			else if (link_sta->bandwidth == IEEE80211_STA_RX_BW_40)
3211 				phymode = MODE_11AX_HE40_2G;
3212 			else
3213 				phymode = MODE_11AX_HE20_2G;
3214 		} else if (link_sta->vht_cap.vht_supported &&
3215 		    !ath12k_peer_assoc_h_vht_masked(vht_mcs_mask)) {
3216 			if (link_sta->bandwidth == IEEE80211_STA_RX_BW_40)
3217 				phymode = MODE_11AC_VHT40;
3218 			else
3219 				phymode = MODE_11AC_VHT20;
3220 		} else if (link_sta->ht_cap.ht_supported &&
3221 			   !ath12k_peer_assoc_h_ht_masked(ht_mcs_mask)) {
3222 			if (link_sta->bandwidth == IEEE80211_STA_RX_BW_40)
3223 				phymode = MODE_11NG_HT40;
3224 			else
3225 				phymode = MODE_11NG_HT20;
3226 		} else if (ath12k_mac_sta_has_ofdm_only(link_sta)) {
3227 			phymode = MODE_11G;
3228 		} else {
3229 			phymode = MODE_11B;
3230 		}
3231 		break;
3232 	case NL80211_BAND_5GHZ:
3233 	case NL80211_BAND_6GHZ:
3234 		/* Check EHT first */
3235 		if (link_sta->eht_cap.has_eht) {
3236 			phymode = ath12k_mac_get_phymode_eht(ar, link_sta);
3237 		} else if (link_sta->he_cap.has_he &&
3238 			   !ath12k_peer_assoc_h_he_masked(he_mcs_mask)) {
3239 			phymode = ath12k_mac_get_phymode_he(ar, link_sta);
3240 		} else if (link_sta->vht_cap.vht_supported &&
3241 		    !ath12k_peer_assoc_h_vht_masked(vht_mcs_mask)) {
3242 			phymode = ath12k_mac_get_phymode_vht(ar, link_sta);
3243 		} else if (link_sta->ht_cap.ht_supported &&
3244 			   !ath12k_peer_assoc_h_ht_masked(ht_mcs_mask)) {
3245 			if (link_sta->bandwidth >= IEEE80211_STA_RX_BW_40)
3246 				phymode = MODE_11NA_HT40;
3247 			else
3248 				phymode = MODE_11NA_HT20;
3249 		} else {
3250 			phymode = MODE_11A;
3251 		}
3252 		break;
3253 	default:
3254 		break;
3255 	}
3256 
3257 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac peer %pM phymode %s\n",
3258 		   arsta->addr, ath12k_mac_phymode_str(phymode));
3259 
3260 	arg->peer_phymode = phymode;
3261 	WARN_ON(phymode == MODE_UNKNOWN);
3262 }
3263 
3264 #define ATH12K_EHT_MCS_7_ENABLED	0x00FF
3265 #define ATH12K_EHT_MCS_9_ENABLED	0x0300
3266 #define ATH12K_EHT_MCS_11_ENABLED	0x0C00
3267 #define ATH12K_EHT_MCS_13_ENABLED	0x3000
3268 
3269 static void ath12k_mac_set_eht_mcs(u8 rx_tx_mcs7, u8 rx_tx_mcs9,
3270 				   u8 rx_tx_mcs11, u8 rx_tx_mcs13,
3271 				   u32 *rx_mcs, u32 *tx_mcs,
3272 				   const u16 eht_mcs_limit[NL80211_EHT_NSS_MAX])
3273 {
3274 	int nss;
3275 	u8 mcs_7 = 0, mcs_9 = 0, mcs_11 = 0, mcs_13 = 0;
3276 	u8 peer_mcs_7, peer_mcs_9, peer_mcs_11, peer_mcs_13;
3277 
3278 	for (nss = 0; nss < NL80211_EHT_NSS_MAX; nss++) {
3279 		if (eht_mcs_limit[nss] & ATH12K_EHT_MCS_7_ENABLED)
3280 			mcs_7++;
3281 		if (eht_mcs_limit[nss] & ATH12K_EHT_MCS_9_ENABLED)
3282 			mcs_9++;
3283 		if (eht_mcs_limit[nss] & ATH12K_EHT_MCS_11_ENABLED)
3284 			mcs_11++;
3285 		if (eht_mcs_limit[nss] & ATH12K_EHT_MCS_13_ENABLED)
3286 			mcs_13++;
3287 	}
3288 
3289 	peer_mcs_7 = u8_get_bits(rx_tx_mcs7, IEEE80211_EHT_MCS_NSS_RX);
3290 	peer_mcs_9 = u8_get_bits(rx_tx_mcs9, IEEE80211_EHT_MCS_NSS_RX);
3291 	peer_mcs_11 = u8_get_bits(rx_tx_mcs11, IEEE80211_EHT_MCS_NSS_RX);
3292 	peer_mcs_13 = u8_get_bits(rx_tx_mcs13, IEEE80211_EHT_MCS_NSS_RX);
3293 
3294 	*rx_mcs = u32_encode_bits(min(peer_mcs_7, mcs_7), WMI_EHT_MCS_NSS_0_7) |
3295 		  u32_encode_bits(min(peer_mcs_9, mcs_9), WMI_EHT_MCS_NSS_8_9) |
3296 		  u32_encode_bits(min(peer_mcs_11, mcs_11), WMI_EHT_MCS_NSS_10_11) |
3297 		  u32_encode_bits(min(peer_mcs_13, mcs_13), WMI_EHT_MCS_NSS_12_13);
3298 
3299 	peer_mcs_7 = u8_get_bits(rx_tx_mcs7, IEEE80211_EHT_MCS_NSS_TX);
3300 	peer_mcs_9 = u8_get_bits(rx_tx_mcs9, IEEE80211_EHT_MCS_NSS_TX);
3301 	peer_mcs_11 = u8_get_bits(rx_tx_mcs11, IEEE80211_EHT_MCS_NSS_TX);
3302 	peer_mcs_13 = u8_get_bits(rx_tx_mcs13, IEEE80211_EHT_MCS_NSS_TX);
3303 
3304 	*tx_mcs = u32_encode_bits(min(peer_mcs_7, mcs_7), WMI_EHT_MCS_NSS_0_7) |
3305 		  u32_encode_bits(min(peer_mcs_9, mcs_9), WMI_EHT_MCS_NSS_8_9) |
3306 		  u32_encode_bits(min(peer_mcs_11, mcs_11), WMI_EHT_MCS_NSS_10_11) |
3307 		  u32_encode_bits(min(peer_mcs_13, mcs_13), WMI_EHT_MCS_NSS_12_13);
3308 }
3309 
3310 static void ath12k_mac_set_eht_ppe_threshold(const u8 *ppe_thres,
3311 					     struct ath12k_wmi_ppe_threshold_arg *ppet)
3312 {
3313 	u32 bit_pos = IEEE80211_EHT_PPE_THRES_INFO_HEADER_SIZE, val;
3314 	u8 nss, ru, i;
3315 	u8 ppet_bit_len_per_ru = IEEE80211_EHT_PPE_THRES_INFO_PPET_SIZE * 2;
3316 
3317 	ppet->numss_m1 = u8_get_bits(ppe_thres[0], IEEE80211_EHT_PPE_THRES_NSS_MASK);
3318 	ppet->ru_bit_mask = u16_get_bits(get_unaligned_le16(ppe_thres),
3319 					 IEEE80211_EHT_PPE_THRES_RU_INDEX_BITMASK_MASK);
3320 
3321 	for (nss = 0; nss <= ppet->numss_m1; nss++) {
3322 		for (ru = 0;
3323 		     ru < hweight16(IEEE80211_EHT_PPE_THRES_RU_INDEX_BITMASK_MASK);
3324 		     ru++) {
3325 			if ((ppet->ru_bit_mask & BIT(ru)) == 0)
3326 				continue;
3327 
3328 			val = 0;
3329 			for (i = 0; i < ppet_bit_len_per_ru; i++) {
3330 				val |= (((ppe_thres[bit_pos / 8] >>
3331 					  (bit_pos % 8)) & 0x1) << i);
3332 				bit_pos++;
3333 			}
3334 			ppet->ppet16_ppet8_ru3_ru0[nss] |=
3335 					(val << (ru * ppet_bit_len_per_ru));
3336 		}
3337 	}
3338 }
3339 
3340 static void ath12k_peer_assoc_h_eht(struct ath12k *ar,
3341 				    struct ath12k_link_vif *arvif,
3342 				    struct ath12k_link_sta *arsta,
3343 				    struct ath12k_wmi_peer_assoc_arg *arg)
3344 {
3345 	struct ieee80211_sta *sta = ath12k_ahsta_to_sta(arsta->ahsta);
3346 	struct ieee80211_vif *vif = ath12k_ahvif_to_vif(arvif->ahvif);
3347 	const struct ieee80211_eht_mcs_nss_supp *own_eht_mcs_nss_supp;
3348 	const struct ieee80211_eht_mcs_nss_supp_20mhz_only *bw_20;
3349 	const struct ieee80211_sta_eht_cap *eht_cap, *own_eht_cap;
3350 	const struct ieee80211_sband_iftype_data *iftd;
3351 	const struct ieee80211_eht_mcs_nss_supp_bw *bw;
3352 	const struct ieee80211_sta_he_cap *he_cap;
3353 	struct ieee80211_link_sta *link_sta;
3354 	struct ieee80211_bss_conf *link_conf;
3355 	struct cfg80211_chan_def def;
3356 	bool user_rate_valid = true;
3357 	enum nl80211_band band;
3358 	int eht_nss, nss_idx;
3359 	u32 *rx_mcs, *tx_mcs;
3360 	u16 *eht_mcs_mask;
3361 	u8 max_nss = 0;
3362 
3363 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
3364 
3365 	link_sta = ath12k_mac_get_link_sta(arsta);
3366 	if (!link_sta) {
3367 		ath12k_warn(ar->ab, "unable to access link sta in peer assoc eht for sta %pM link %u\n",
3368 			    sta->addr, arsta->link_id);
3369 		return;
3370 	}
3371 
3372 	link_conf = ath12k_mac_get_link_bss_conf(arvif);
3373 	if (!link_conf) {
3374 		ath12k_warn(ar->ab, "unable to access link_conf in peer assoc eht set\n");
3375 		return;
3376 	}
3377 
3378 	eht_cap = &link_sta->eht_cap;
3379 	he_cap = &link_sta->he_cap;
3380 	if (!he_cap->has_he || !eht_cap->has_eht)
3381 		return;
3382 
3383 	if (WARN_ON(ath12k_mac_vif_link_chan(vif, arvif->link_id, &def)))
3384 		return;
3385 
3386 	band = def.chan->band;
3387 	eht_mcs_mask = arvif->bitrate_mask.control[band].eht_mcs;
3388 
3389 	iftd = ieee80211_get_sband_iftype_data(&ar->mac.sbands[band], vif->type);
3390 	if (!iftd) {
3391 		ath12k_warn(ar->ab,
3392 			    "unable to access iftype_data in struct ieee80211_supported_band\n");
3393 		return;
3394 	}
3395 
3396 	own_eht_cap = &iftd->eht_cap;
3397 	own_eht_mcs_nss_supp = &own_eht_cap->eht_mcs_nss_supp;
3398 
3399 	arg->eht_flag = true;
3400 
3401 	if ((eht_cap->eht_cap_elem.phy_cap_info[5] &
3402 	     IEEE80211_EHT_PHY_CAP5_PPE_THRESHOLD_PRESENT) &&
3403 	    eht_cap->eht_ppe_thres[0] != 0)
3404 		ath12k_mac_set_eht_ppe_threshold(eht_cap->eht_ppe_thres,
3405 						 &arg->peer_eht_ppet);
3406 
3407 	memcpy(arg->peer_eht_cap_mac, eht_cap->eht_cap_elem.mac_cap_info,
3408 	       sizeof(eht_cap->eht_cap_elem.mac_cap_info));
3409 	memcpy(arg->peer_eht_cap_phy, eht_cap->eht_cap_elem.phy_cap_info,
3410 	       sizeof(eht_cap->eht_cap_elem.phy_cap_info));
3411 
3412 	rx_mcs = arg->peer_eht_rx_mcs_set;
3413 	tx_mcs = arg->peer_eht_tx_mcs_set;
3414 
3415 	eht_nss = ath12k_mac_max_eht_mcs_nss((void *)own_eht_mcs_nss_supp,
3416 					     sizeof(*own_eht_mcs_nss_supp));
3417 	if (eht_nss > link_sta->rx_nss) {
3418 		user_rate_valid = false;
3419 		for (nss_idx = (link_sta->rx_nss - 1); nss_idx >= 0; nss_idx--) {
3420 			if (eht_mcs_mask[nss_idx]) {
3421 				user_rate_valid = true;
3422 				break;
3423 			}
3424 		}
3425 	}
3426 
3427 	if (!user_rate_valid) {
3428 		ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
3429 			   "Setting eht range MCS value to peer supported nss %d for peer %pM\n",
3430 			   link_sta->rx_nss, arsta->addr);
3431 		eht_mcs_mask[link_sta->rx_nss - 1] = eht_mcs_mask[eht_nss - 1];
3432 	}
3433 
3434 	bw_20 = &eht_cap->eht_mcs_nss_supp.only_20mhz;
3435 	bw = &eht_cap->eht_mcs_nss_supp.bw._80;
3436 
3437 	switch (link_sta->bandwidth) {
3438 	case IEEE80211_STA_RX_BW_320:
3439 		bw = &eht_cap->eht_mcs_nss_supp.bw._320;
3440 		ath12k_mac_set_eht_mcs(bw->rx_tx_mcs9_max_nss,
3441 				       bw->rx_tx_mcs9_max_nss,
3442 				       bw->rx_tx_mcs11_max_nss,
3443 				       bw->rx_tx_mcs13_max_nss,
3444 				       &rx_mcs[WMI_EHTCAP_TXRX_MCS_NSS_IDX_320],
3445 				       &tx_mcs[WMI_EHTCAP_TXRX_MCS_NSS_IDX_320],
3446 				       eht_mcs_mask);
3447 		arg->peer_eht_mcs_count++;
3448 		fallthrough;
3449 	case IEEE80211_STA_RX_BW_160:
3450 		bw = &eht_cap->eht_mcs_nss_supp.bw._160;
3451 		ath12k_mac_set_eht_mcs(bw->rx_tx_mcs9_max_nss,
3452 				       bw->rx_tx_mcs9_max_nss,
3453 				       bw->rx_tx_mcs11_max_nss,
3454 				       bw->rx_tx_mcs13_max_nss,
3455 				       &rx_mcs[WMI_EHTCAP_TXRX_MCS_NSS_IDX_160],
3456 				       &tx_mcs[WMI_EHTCAP_TXRX_MCS_NSS_IDX_160],
3457 				       eht_mcs_mask);
3458 		arg->peer_eht_mcs_count++;
3459 		fallthrough;
3460 	default:
3461 		if ((vif->type == NL80211_IFTYPE_AP ||
3462 		     vif->type == NL80211_IFTYPE_MESH_POINT) &&
3463 		    !(link_sta->he_cap.he_cap_elem.phy_cap_info[0] &
3464 		      IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_MASK_ALL)) {
3465 			bw_20 = &eht_cap->eht_mcs_nss_supp.only_20mhz;
3466 
3467 			ath12k_mac_set_eht_mcs(bw_20->rx_tx_mcs7_max_nss,
3468 					       bw_20->rx_tx_mcs9_max_nss,
3469 					       bw_20->rx_tx_mcs11_max_nss,
3470 					       bw_20->rx_tx_mcs13_max_nss,
3471 					       &rx_mcs[WMI_EHTCAP_TXRX_MCS_NSS_IDX_80],
3472 					       &tx_mcs[WMI_EHTCAP_TXRX_MCS_NSS_IDX_80],
3473 					       eht_mcs_mask);
3474 		} else {
3475 			bw = &eht_cap->eht_mcs_nss_supp.bw._80;
3476 			ath12k_mac_set_eht_mcs(bw->rx_tx_mcs9_max_nss,
3477 					       bw->rx_tx_mcs9_max_nss,
3478 					       bw->rx_tx_mcs11_max_nss,
3479 					       bw->rx_tx_mcs13_max_nss,
3480 					       &rx_mcs[WMI_EHTCAP_TXRX_MCS_NSS_IDX_80],
3481 					       &tx_mcs[WMI_EHTCAP_TXRX_MCS_NSS_IDX_80],
3482 					       eht_mcs_mask);
3483 		}
3484 
3485 		arg->peer_eht_mcs_count++;
3486 		break;
3487 	}
3488 
3489 	arg->punct_bitmap = ~arvif->punct_bitmap;
3490 	arg->eht_disable_mcs15 = link_conf->eht_disable_mcs15;
3491 
3492 	if ((vif->type == NL80211_IFTYPE_AP ||
3493 	     vif->type == NL80211_IFTYPE_MESH_POINT) &&
3494 	    !(link_sta->he_cap.he_cap_elem.phy_cap_info[0] &
3495 	      IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_MASK_ALL)) {
3496 		if (bw_20->rx_tx_mcs13_max_nss)
3497 			max_nss = max(max_nss, u8_get_bits(bw_20->rx_tx_mcs13_max_nss,
3498 							   IEEE80211_EHT_MCS_NSS_RX));
3499 		if (bw_20->rx_tx_mcs11_max_nss)
3500 			max_nss = max(max_nss, u8_get_bits(bw_20->rx_tx_mcs11_max_nss,
3501 							   IEEE80211_EHT_MCS_NSS_RX));
3502 		if (bw_20->rx_tx_mcs9_max_nss)
3503 			max_nss = max(max_nss, u8_get_bits(bw_20->rx_tx_mcs9_max_nss,
3504 							   IEEE80211_EHT_MCS_NSS_RX));
3505 		if (bw_20->rx_tx_mcs7_max_nss)
3506 			max_nss = max(max_nss, u8_get_bits(bw_20->rx_tx_mcs7_max_nss,
3507 							   IEEE80211_EHT_MCS_NSS_RX));
3508 	} else {
3509 		if (bw->rx_tx_mcs13_max_nss)
3510 			max_nss = max(max_nss, u8_get_bits(bw->rx_tx_mcs13_max_nss,
3511 							   IEEE80211_EHT_MCS_NSS_RX));
3512 		if (bw->rx_tx_mcs11_max_nss)
3513 			max_nss = max(max_nss, u8_get_bits(bw->rx_tx_mcs11_max_nss,
3514 							   IEEE80211_EHT_MCS_NSS_RX));
3515 		if (bw->rx_tx_mcs9_max_nss)
3516 			max_nss = max(max_nss, u8_get_bits(bw->rx_tx_mcs9_max_nss,
3517 							   IEEE80211_EHT_MCS_NSS_RX));
3518 	}
3519 
3520 	max_nss = min(max_nss, (uint8_t)eht_nss);
3521 
3522 	arg->peer_nss = min(link_sta->rx_nss, max_nss);
3523 
3524 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
3525 		   "mac eht peer %pM nss %d mcs cnt %d ru_punct_bitmap 0x%x\n",
3526 		   arsta->addr, arg->peer_nss, arg->peer_eht_mcs_count,
3527 		   arg->punct_bitmap);
3528 }
3529 
3530 static void ath12k_peer_assoc_h_mlo(struct ath12k_link_sta *arsta,
3531 				    struct ath12k_wmi_peer_assoc_arg *arg)
3532 {
3533 	struct ieee80211_sta *sta = ath12k_ahsta_to_sta(arsta->ahsta);
3534 	struct peer_assoc_mlo_params *ml = &arg->ml;
3535 	struct ath12k_sta *ahsta = arsta->ahsta;
3536 	struct ath12k_link_sta *arsta_p;
3537 	struct ath12k_link_vif *arvif;
3538 	struct ath12k_hw *ah = arsta->arvif->ar->ah;
3539 	unsigned long links;
3540 	u8 link_id;
3541 	int i;
3542 
3543 	if (!sta->mlo)
3544 		return;
3545 
3546 	if (ah->host_alloc_ml_id &&
3547 	    ahsta->ml_peer_id == ATH12K_MLO_PEER_ID_INVALID)
3548 		return;
3549 
3550 	ml->enabled = true;
3551 	ml->assoc_link = arsta->is_assoc_link;
3552 
3553 	/* For now considering the primary umac based on assoc link */
3554 	ml->primary_umac = arsta->is_assoc_link;
3555 	/*
3556 	 * Only chips that allocate the MLD peer ID on the host send a valid
3557 	 * ml_peer_id in WMI_PEER_ASSOC_CMDID. For chips where the firmware
3558 	 * picks the ID, leave peer_id_valid false to avoid unexpected issues.
3559 	 */
3560 	ml->peer_id_valid = ah->host_alloc_ml_id;
3561 	ml->logical_link_idx_valid = true;
3562 
3563 	ether_addr_copy(ml->mld_addr, sta->addr);
3564 	ml->logical_link_idx = arsta->link_idx;
3565 	/*
3566 	 * WMI_MLO_PEER_ASSOC_PARAMS expects the raw ML peer ID without
3567 	 * the host-side ATH12K_PEER_ML_ID_VALID bookkeeping bit. For chips
3568 	 * where the firmware allocates the ID, the field is unused (the
3569 	 * firmware always allocates regardless of the value here); send 0
3570 	 * to make that intent explicit.
3571 	 */
3572 	ml->ml_peer_id = ah->host_alloc_ml_id ?
3573 			 (ahsta->ml_peer_id & ~ATH12K_PEER_ML_ID_VALID) : 0;
3574 	ml->ieee_link_id = arsta->link_id;
3575 	ml->num_partner_links = 0;
3576 	ml->eml_cap = sta->eml_cap;
3577 	links = ahsta->links_map;
3578 
3579 	rcu_read_lock();
3580 
3581 	i = 0;
3582 
3583 	for_each_set_bit(link_id, &links, IEEE80211_MLD_MAX_NUM_LINKS) {
3584 		if (i >= ATH12K_WMI_MLO_MAX_LINKS)
3585 			break;
3586 
3587 		arsta_p = rcu_dereference(ahsta->link[link_id]);
3588 		arvif = rcu_dereference(ahsta->ahvif->link[link_id]);
3589 
3590 		if (arsta_p == arsta)
3591 			continue;
3592 
3593 		if (!arvif->is_started)
3594 			continue;
3595 
3596 		ml->partner_info[i].vdev_id = arvif->vdev_id;
3597 		ml->partner_info[i].hw_link_id = arvif->ar->pdev->hw_link_id;
3598 		ml->partner_info[i].assoc_link = arsta_p->is_assoc_link;
3599 		ml->partner_info[i].primary_umac = arsta_p->is_assoc_link;
3600 		ml->partner_info[i].logical_link_idx_valid = true;
3601 		ml->partner_info[i].logical_link_idx = arsta_p->link_idx;
3602 		ml->num_partner_links++;
3603 
3604 		i++;
3605 	}
3606 
3607 	rcu_read_unlock();
3608 }
3609 
3610 static void ath12k_peer_assoc_prepare(struct ath12k *ar,
3611 				      struct ath12k_link_vif *arvif,
3612 				      struct ath12k_link_sta *arsta,
3613 				      struct ath12k_wmi_peer_assoc_arg *arg,
3614 				      bool reassoc)
3615 {
3616 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
3617 
3618 	memset(arg, 0, sizeof(*arg));
3619 
3620 	arg->peer_new_assoc = !reassoc;
3621 	ath12k_peer_assoc_h_basic(ar, arvif, arsta, arg);
3622 	ath12k_peer_assoc_h_crypto(ar, arvif, arsta, arg);
3623 	ath12k_peer_assoc_h_rates(ar, arvif, arsta, arg);
3624 	ath12k_peer_assoc_h_ht(ar, arvif, arsta, arg);
3625 	ath12k_peer_assoc_h_vht(ar, arvif, arsta, arg);
3626 	ath12k_peer_assoc_h_he(ar, arvif, arsta, arg);
3627 	ath12k_peer_assoc_h_he_6ghz(ar, arvif, arsta, arg);
3628 	ath12k_peer_assoc_h_eht(ar, arvif, arsta, arg);
3629 	ath12k_peer_assoc_h_qos(ar, arvif, arsta, arg);
3630 	ath12k_peer_assoc_h_phymode(ar, arvif, arsta, arg);
3631 	ath12k_peer_assoc_h_smps(arsta, arg);
3632 	ath12k_peer_assoc_h_mlo(arsta, arg);
3633 
3634 	arsta->peer_nss = arg->peer_nss;
3635 	/* TODO: amsdu_disable req? */
3636 }
3637 
3638 static int ath12k_setup_peer_smps(struct ath12k *ar, struct ath12k_link_vif *arvif,
3639 				  const u8 *addr,
3640 				  const struct ieee80211_sta_ht_cap *ht_cap,
3641 				  const struct ieee80211_he_6ghz_capa *he_6ghz_capa)
3642 {
3643 	int smps, ret = 0;
3644 
3645 	if (!ht_cap->ht_supported && !he_6ghz_capa)
3646 		return 0;
3647 
3648 	ret = ath12k_get_smps_from_capa(ht_cap, he_6ghz_capa, &smps);
3649 	if (ret < 0)
3650 		return ret;
3651 
3652 	return ath12k_wmi_set_peer_param(ar, addr, arvif->vdev_id,
3653 					 WMI_PEER_MIMO_PS_STATE,
3654 					 ath12k_smps_map[smps]);
3655 }
3656 
3657 static int ath12k_mac_set_he_txbf_conf(struct ath12k_link_vif *arvif)
3658 {
3659 	struct ath12k_vif *ahvif = arvif->ahvif;
3660 	struct ath12k *ar = arvif->ar;
3661 	u32 param = WMI_VDEV_PARAM_SET_HEMU_MODE;
3662 	u32 value = 0;
3663 	int ret;
3664 	struct ieee80211_bss_conf *link_conf;
3665 
3666 	link_conf = ath12k_mac_get_link_bss_conf(arvif);
3667 	if (!link_conf) {
3668 		ath12k_warn(ar->ab, "unable to access bss link conf in txbf conf\n");
3669 		return -EINVAL;
3670 	}
3671 
3672 	if (!link_conf->he_support)
3673 		return 0;
3674 
3675 	if (link_conf->he_su_beamformer) {
3676 		value |= u32_encode_bits(HE_SU_BFER_ENABLE, HE_MODE_SU_TX_BFER);
3677 		if (link_conf->he_mu_beamformer &&
3678 		    ahvif->vdev_type == WMI_VDEV_TYPE_AP)
3679 			value |= u32_encode_bits(HE_MU_BFER_ENABLE, HE_MODE_MU_TX_BFER);
3680 	}
3681 
3682 	if (ahvif->vif->type != NL80211_IFTYPE_MESH_POINT) {
3683 		value |= u32_encode_bits(HE_DL_MUOFDMA_ENABLE, HE_MODE_DL_OFDMA) |
3684 			 u32_encode_bits(HE_UL_MUOFDMA_ENABLE, HE_MODE_UL_OFDMA);
3685 
3686 		if (link_conf->he_full_ul_mumimo)
3687 			value |= u32_encode_bits(HE_UL_MUMIMO_ENABLE, HE_MODE_UL_MUMIMO);
3688 
3689 		if (link_conf->he_su_beamformee)
3690 			value |= u32_encode_bits(HE_SU_BFEE_ENABLE, HE_MODE_SU_TX_BFEE);
3691 	}
3692 
3693 	ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id, param, value);
3694 	if (ret) {
3695 		ath12k_warn(ar->ab, "failed to set vdev %d HE MU mode: %d\n",
3696 			    arvif->vdev_id, ret);
3697 		return ret;
3698 	}
3699 
3700 	param = WMI_VDEV_PARAM_SET_HE_SOUNDING_MODE;
3701 	value =	u32_encode_bits(HE_VHT_SOUNDING_MODE_ENABLE, HE_VHT_SOUNDING_MODE) |
3702 		u32_encode_bits(HE_TRIG_NONTRIG_SOUNDING_MODE_ENABLE,
3703 				HE_TRIG_NONTRIG_SOUNDING_MODE);
3704 	ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
3705 					    param, value);
3706 	if (ret) {
3707 		ath12k_warn(ar->ab, "failed to set vdev %d sounding mode: %d\n",
3708 			    arvif->vdev_id, ret);
3709 		return ret;
3710 	}
3711 
3712 	return 0;
3713 }
3714 
3715 static int ath12k_mac_vif_recalc_sta_he_txbf(struct ath12k *ar,
3716 					     struct ath12k_link_vif *arvif,
3717 					     struct ieee80211_sta_he_cap *he_cap,
3718 					     int *hemode)
3719 {
3720 	struct ieee80211_vif *vif = arvif->ahvif->vif;
3721 	struct ieee80211_he_cap_elem he_cap_elem = {};
3722 	struct ieee80211_sta_he_cap *cap_band;
3723 	struct cfg80211_chan_def def;
3724 	u8 link_id = arvif->link_id;
3725 	struct ieee80211_bss_conf *link_conf;
3726 
3727 	link_conf = ath12k_mac_get_link_bss_conf(arvif);
3728 	if (!link_conf) {
3729 		ath12k_warn(ar->ab, "unable to access bss link conf in recalc txbf conf\n");
3730 		return -EINVAL;
3731 	}
3732 
3733 	if (!link_conf->he_support)
3734 		return 0;
3735 
3736 	if (vif->type != NL80211_IFTYPE_STATION)
3737 		return -EINVAL;
3738 
3739 	if (WARN_ON(ath12k_mac_vif_link_chan(vif, link_id, &def)))
3740 		return -EINVAL;
3741 
3742 	if (def.chan->band == NL80211_BAND_2GHZ)
3743 		cap_band = &ar->mac.iftype[NL80211_BAND_2GHZ][vif->type].he_cap;
3744 	else
3745 		cap_band = &ar->mac.iftype[NL80211_BAND_5GHZ][vif->type].he_cap;
3746 
3747 	memcpy(&he_cap_elem, &cap_band->he_cap_elem, sizeof(he_cap_elem));
3748 
3749 	*hemode = 0;
3750 	if (HECAP_PHY_SUBFME_GET(he_cap_elem.phy_cap_info)) {
3751 		if (HECAP_PHY_SUBFMR_GET(he_cap->he_cap_elem.phy_cap_info))
3752 			*hemode |= u32_encode_bits(HE_SU_BFEE_ENABLE, HE_MODE_SU_TX_BFEE);
3753 		if (HECAP_PHY_MUBFMR_GET(he_cap->he_cap_elem.phy_cap_info))
3754 			*hemode |= u32_encode_bits(HE_MU_BFEE_ENABLE, HE_MODE_MU_TX_BFEE);
3755 	}
3756 
3757 	if (vif->type != NL80211_IFTYPE_MESH_POINT) {
3758 		*hemode |= u32_encode_bits(HE_DL_MUOFDMA_ENABLE, HE_MODE_DL_OFDMA) |
3759 			  u32_encode_bits(HE_UL_MUOFDMA_ENABLE, HE_MODE_UL_OFDMA);
3760 
3761 		if (HECAP_PHY_ULMUMIMO_GET(he_cap_elem.phy_cap_info))
3762 			if (HECAP_PHY_ULMUMIMO_GET(he_cap->he_cap_elem.phy_cap_info))
3763 				*hemode |= u32_encode_bits(HE_UL_MUMIMO_ENABLE,
3764 							  HE_MODE_UL_MUMIMO);
3765 
3766 		if (u32_get_bits(*hemode, HE_MODE_MU_TX_BFEE))
3767 			*hemode |= u32_encode_bits(HE_SU_BFEE_ENABLE, HE_MODE_SU_TX_BFEE);
3768 
3769 		if (u32_get_bits(*hemode, HE_MODE_MU_TX_BFER))
3770 			*hemode |= u32_encode_bits(HE_SU_BFER_ENABLE, HE_MODE_SU_TX_BFER);
3771 	}
3772 
3773 	return 0;
3774 }
3775 
3776 static int ath12k_mac_set_eht_txbf_conf(struct ath12k_link_vif *arvif)
3777 {
3778 	struct ath12k_vif *ahvif = arvif->ahvif;
3779 	struct ath12k *ar = arvif->ar;
3780 	u32 param = WMI_VDEV_PARAM_SET_EHT_MU_MODE;
3781 	u32 value = 0;
3782 	int ret;
3783 	struct ieee80211_bss_conf *link_conf;
3784 
3785 	link_conf = ath12k_mac_get_link_bss_conf(arvif);
3786 	if (!link_conf) {
3787 		ath12k_warn(ar->ab, "unable to access bss link conf in eht txbf conf\n");
3788 		return -ENOENT;
3789 	}
3790 
3791 	if (!link_conf->eht_support)
3792 		return 0;
3793 
3794 	if (link_conf->eht_su_beamformer) {
3795 		value |= u32_encode_bits(EHT_SU_BFER_ENABLE, EHT_MODE_SU_TX_BFER);
3796 		if (link_conf->eht_mu_beamformer &&
3797 		    ahvif->vdev_type == WMI_VDEV_TYPE_AP)
3798 			value |= u32_encode_bits(EHT_MU_BFER_ENABLE,
3799 						 EHT_MODE_MU_TX_BFER) |
3800 				 u32_encode_bits(EHT_DL_MUOFDMA_ENABLE,
3801 						 EHT_MODE_DL_OFDMA_MUMIMO) |
3802 				 u32_encode_bits(EHT_UL_MUOFDMA_ENABLE,
3803 						 EHT_MODE_UL_OFDMA_MUMIMO);
3804 	}
3805 
3806 	if (ahvif->vif->type != NL80211_IFTYPE_MESH_POINT) {
3807 		value |= u32_encode_bits(EHT_DL_MUOFDMA_ENABLE, EHT_MODE_DL_OFDMA) |
3808 			 u32_encode_bits(EHT_UL_MUOFDMA_ENABLE, EHT_MODE_UL_OFDMA);
3809 
3810 		if (link_conf->eht_80mhz_full_bw_ul_mumimo)
3811 			value |= u32_encode_bits(EHT_UL_MUMIMO_ENABLE, EHT_MODE_MUMIMO);
3812 
3813 		if (link_conf->eht_su_beamformee)
3814 			value |= u32_encode_bits(EHT_SU_BFEE_ENABLE,
3815 						 EHT_MODE_SU_TX_BFEE);
3816 	}
3817 
3818 	ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id, param, value);
3819 	if (ret) {
3820 		ath12k_warn(ar->ab, "failed to set vdev %d EHT MU mode: %d\n",
3821 			    arvif->vdev_id, ret);
3822 		return ret;
3823 	}
3824 
3825 	return 0;
3826 }
3827 
3828 static u32 ath12k_mac_ieee80211_sta_bw_to_wmi(struct ath12k *ar,
3829 					      struct ieee80211_link_sta *link_sta)
3830 {
3831 	u32 bw;
3832 
3833 	switch (link_sta->bandwidth) {
3834 	case IEEE80211_STA_RX_BW_20:
3835 		bw = WMI_PEER_CHWIDTH_20MHZ;
3836 		break;
3837 	case IEEE80211_STA_RX_BW_40:
3838 		bw = WMI_PEER_CHWIDTH_40MHZ;
3839 		break;
3840 	case IEEE80211_STA_RX_BW_80:
3841 		bw = WMI_PEER_CHWIDTH_80MHZ;
3842 		break;
3843 	case IEEE80211_STA_RX_BW_160:
3844 		bw = WMI_PEER_CHWIDTH_160MHZ;
3845 		break;
3846 	case IEEE80211_STA_RX_BW_320:
3847 		bw = WMI_PEER_CHWIDTH_320MHZ;
3848 		break;
3849 	default:
3850 		ath12k_warn(ar->ab, "Invalid bandwidth %d for link station %pM\n",
3851 			    link_sta->bandwidth, link_sta->addr);
3852 		bw = WMI_PEER_CHWIDTH_20MHZ;
3853 		break;
3854 	}
3855 
3856 	return bw;
3857 }
3858 
3859 static int ath12k_mac_peer_assoc(struct ath12k *ar,
3860 				 struct ath12k_wmi_peer_assoc_arg *peer_arg)
3861 {
3862 	struct ath12k_hw *ah = ath12k_ar_to_ah(ar);
3863 	int ret;
3864 
3865 	reinit_completion(&ar->peer_assoc_done);
3866 	reinit_completion(&ah->peer_ml_id_done);
3867 
3868 	ret = ath12k_wmi_send_peer_assoc_cmd(ar, peer_arg);
3869 	if (ret) {
3870 		ath12k_warn(ar->ab, "failed to run peer assoc for %pM vdev %i: %d\n",
3871 			    peer_arg->peer_mac, peer_arg->vdev_id, ret);
3872 		return ret;
3873 	}
3874 
3875 	if (!wait_for_completion_timeout(&ar->peer_assoc_done, 1 * HZ)) {
3876 		ath12k_warn(ar->ab, "failed to get peer assoc conf event for %pM vdev %i\n",
3877 			    peer_arg->peer_mac, peer_arg->vdev_id);
3878 		return -ETIMEDOUT;
3879 	}
3880 
3881 	/*
3882 	 * For devices where the firmware allocates the MLD peer ID, the host
3883 	 * learns the real ID only from the MLO_RX_PEER_MAP HTT event, which is
3884 	 * handled in a softirq (BH workqueue) context that cannot take the
3885 	 * wiphy lock. Block here, while still holding the wiphy lock, until
3886 	 * that event has fixed up the ID. This serialises the fixup against
3887 	 * all other wiphy-locked ml_peer_id accesses.
3888 	 *
3889 	 * The firmware sends the event only once, in response to the assoc-link
3890 	 * peer assoc, so block only for that link.
3891 	 */
3892 	if (!ah->host_alloc_ml_id &&
3893 	    peer_arg->is_assoc &&
3894 	    peer_arg->ml.enabled &&
3895 	    peer_arg->ml.assoc_link &&
3896 	    !wait_for_completion_timeout(&ah->peer_ml_id_done, 1 * HZ)) {
3897 		ath12k_warn(ar->ab, "failed to get MLO peer map event for %pM vdev %i\n",
3898 			    peer_arg->peer_mac, peer_arg->vdev_id);
3899 		return -ETIMEDOUT;
3900 	}
3901 
3902 	return 0;
3903 }
3904 
3905 static void ath12k_bss_assoc(struct ath12k *ar,
3906 			     struct ath12k_link_vif *arvif,
3907 			     struct ieee80211_bss_conf *bss_conf)
3908 {
3909 	struct ath12k_vif *ahvif = arvif->ahvif;
3910 	struct ieee80211_vif *vif = ath12k_ahvif_to_vif(ahvif);
3911 	struct ath12k_wmi_vdev_up_params params = {};
3912 	struct ieee80211_link_sta *link_sta;
3913 	u8 link_id = bss_conf->link_id;
3914 	struct ath12k_link_sta *arsta;
3915 	struct ieee80211_sta *ap_sta;
3916 	struct ath12k_sta *ahsta;
3917 	struct ath12k_dp_link_peer *peer;
3918 	bool is_auth = false;
3919 	u32 hemode = 0;
3920 	int ret;
3921 	struct ath12k_dp *dp = ath12k_ab_to_dp(ar->ab);
3922 
3923 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
3924 
3925 	struct ath12k_wmi_peer_assoc_arg *peer_arg __free(kfree) =
3926 					kzalloc_obj(*peer_arg);
3927 	if (!peer_arg)
3928 		return;
3929 
3930 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
3931 		   "mac vdev %i link id %u assoc bssid %pM aid %d\n",
3932 		   arvif->vdev_id, link_id, arvif->bssid, ahvif->aid);
3933 
3934 	rcu_read_lock();
3935 
3936 	/* During ML connection, cfg.ap_addr has the MLD address. For
3937 	 * non-ML connection, it has the BSSID.
3938 	 */
3939 	ap_sta = ieee80211_find_sta(vif, vif->cfg.ap_addr);
3940 	if (!ap_sta) {
3941 		ath12k_warn(ar->ab, "failed to find station entry for bss %pM vdev %i\n",
3942 			    vif->cfg.ap_addr, arvif->vdev_id);
3943 		rcu_read_unlock();
3944 		return;
3945 	}
3946 
3947 	ahsta = ath12k_sta_to_ahsta(ap_sta);
3948 
3949 	arsta = wiphy_dereference(ath12k_ar_to_hw(ar)->wiphy,
3950 				  ahsta->link[link_id]);
3951 	if (WARN_ON(!arsta)) {
3952 		rcu_read_unlock();
3953 		return;
3954 	}
3955 
3956 	link_sta = ath12k_mac_get_link_sta(arsta);
3957 	if (WARN_ON(!link_sta)) {
3958 		rcu_read_unlock();
3959 		return;
3960 	}
3961 
3962 	ath12k_peer_assoc_prepare(ar, arvif, arsta, peer_arg, false);
3963 
3964 	/* link_sta->he_cap must be protected by rcu_read_lock */
3965 	ret = ath12k_mac_vif_recalc_sta_he_txbf(ar, arvif, &link_sta->he_cap, &hemode);
3966 	if (ret) {
3967 		ath12k_warn(ar->ab, "failed to recalc he txbf for vdev %i on bss %pM: %d\n",
3968 			    arvif->vdev_id, bss_conf->bssid, ret);
3969 		rcu_read_unlock();
3970 		return;
3971 	}
3972 
3973 	rcu_read_unlock();
3974 
3975 	/* keep this before ath12k_wmi_send_peer_assoc_cmd() */
3976 	ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
3977 					    WMI_VDEV_PARAM_SET_HEMU_MODE, hemode);
3978 	if (ret) {
3979 		ath12k_warn(ar->ab, "failed to submit vdev param txbf 0x%x: %d\n",
3980 			    hemode, ret);
3981 		return;
3982 	}
3983 
3984 	peer_arg->is_assoc = true;
3985 
3986 	ret = ath12k_mac_peer_assoc(ar, peer_arg);
3987 	if (ret)
3988 		return;
3989 
3990 	ret = ath12k_setup_peer_smps(ar, arvif, bss_conf->bssid,
3991 				     &link_sta->ht_cap, &link_sta->he_6ghz_capa);
3992 	if (ret) {
3993 		ath12k_warn(ar->ab, "failed to setup peer SMPS for vdev %d: %d\n",
3994 			    arvif->vdev_id, ret);
3995 		return;
3996 	}
3997 
3998 	WARN_ON(arvif->is_up);
3999 
4000 	ahvif->aid = vif->cfg.aid;
4001 	ether_addr_copy(arvif->bssid, bss_conf->bssid);
4002 
4003 	params.vdev_id = arvif->vdev_id;
4004 	params.aid = ahvif->aid;
4005 	params.bssid = arvif->bssid;
4006 	params.tx_bssid = ath12k_mac_get_tx_bssid(arvif);
4007 	if (params.tx_bssid) {
4008 		params.nontx_profile_idx = bss_conf->bssid_index;
4009 		params.nontx_profile_cnt = 1 << bss_conf->bssid_indicator;
4010 	}
4011 	ret = ath12k_wmi_vdev_up(ar, &params);
4012 	if (ret) {
4013 		ath12k_warn(ar->ab, "failed to set vdev %d up: %d\n",
4014 			    arvif->vdev_id, ret);
4015 		return;
4016 	}
4017 
4018 	arvif->is_up = true;
4019 	arvif->rekey_data.enable_offload = false;
4020 
4021 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
4022 		   "mac vdev %d up (associated) bssid %pM aid %d\n",
4023 		   arvif->vdev_id, bss_conf->bssid, vif->cfg.aid);
4024 
4025 	spin_lock_bh(&dp->dp_lock);
4026 
4027 	peer = ath12k_dp_link_peer_find_by_vdev_and_addr(dp, arvif->vdev_id,
4028 							 arvif->bssid);
4029 	if (peer && peer->is_authorized)
4030 		is_auth = true;
4031 
4032 	spin_unlock_bh(&dp->dp_lock);
4033 
4034 	/* Authorize BSS Peer */
4035 	if (is_auth) {
4036 		ret = ath12k_wmi_set_peer_param(ar, arvif->bssid,
4037 						arvif->vdev_id,
4038 						WMI_PEER_AUTHORIZE,
4039 						1);
4040 		if (ret)
4041 			ath12k_warn(ar->ab, "Unable to authorize BSS peer: %d\n", ret);
4042 	}
4043 
4044 	ret = ath12k_wmi_send_obss_spr_cmd(ar, arvif->vdev_id,
4045 					   &bss_conf->he_obss_pd);
4046 	if (ret)
4047 		ath12k_warn(ar->ab, "failed to set vdev %i OBSS PD parameters: %d\n",
4048 			    arvif->vdev_id, ret);
4049 
4050 	if (test_bit(WMI_TLV_SERVICE_11D_OFFLOAD, ar->ab->wmi_ab.svc_map) &&
4051 	    ahvif->vdev_type == WMI_VDEV_TYPE_STA &&
4052 	    ahvif->vdev_subtype == WMI_VDEV_SUBTYPE_NONE)
4053 		ath12k_mac_11d_scan_stop_all(ar->ab);
4054 }
4055 
4056 static void ath12k_bss_disassoc(struct ath12k *ar,
4057 				struct ath12k_link_vif *arvif)
4058 {
4059 	int ret;
4060 
4061 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
4062 
4063 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac vdev %i disassoc bssid %pM\n",
4064 		   arvif->vdev_id, arvif->bssid);
4065 
4066 	ret = ath12k_wmi_vdev_down(ar, arvif->vdev_id);
4067 	if (ret)
4068 		ath12k_warn(ar->ab, "failed to down vdev %i: %d\n",
4069 			    arvif->vdev_id, ret);
4070 
4071 	arvif->is_up = false;
4072 
4073 	memset(&arvif->rekey_data, 0, sizeof(arvif->rekey_data));
4074 
4075 	cancel_delayed_work(&arvif->connection_loss_work);
4076 }
4077 
4078 static u32 ath12k_mac_get_rate_hw_value(int bitrate)
4079 {
4080 	u32 preamble;
4081 	u16 hw_value;
4082 	int rate;
4083 	size_t i;
4084 
4085 	if (ath12k_mac_bitrate_is_cck(bitrate))
4086 		preamble = WMI_RATE_PREAMBLE_CCK;
4087 	else
4088 		preamble = WMI_RATE_PREAMBLE_OFDM;
4089 
4090 	for (i = 0; i < ARRAY_SIZE(ath12k_legacy_rates); i++) {
4091 		if (ath12k_legacy_rates[i].bitrate != bitrate)
4092 			continue;
4093 
4094 		hw_value = ath12k_legacy_rates[i].hw_value;
4095 		rate = ATH12K_HW_RATE_CODE(hw_value, 0, preamble);
4096 
4097 		return rate;
4098 	}
4099 
4100 	return -EINVAL;
4101 }
4102 
4103 static void ath12k_recalculate_mgmt_rate(struct ath12k *ar,
4104 					 struct ath12k_link_vif *arvif,
4105 					 struct cfg80211_chan_def *def)
4106 {
4107 	struct ieee80211_vif *vif = ath12k_ahvif_to_vif(arvif->ahvif);
4108 	struct ieee80211_hw *hw = ath12k_ar_to_hw(ar);
4109 	const struct ieee80211_supported_band *sband;
4110 	struct ieee80211_bss_conf *bss_conf;
4111 	u8 basic_rate_idx;
4112 	int hw_rate_code;
4113 	u32 vdev_param;
4114 	u16 bitrate;
4115 	int ret;
4116 
4117 	lockdep_assert_wiphy(hw->wiphy);
4118 
4119 	bss_conf = ath12k_mac_get_link_bss_conf(arvif);
4120 	if (!bss_conf) {
4121 		ath12k_warn(ar->ab, "unable to access bss link conf in mgmt rate calc for vif %pM link %u\n",
4122 			    vif->addr, arvif->link_id);
4123 		return;
4124 	}
4125 
4126 	sband = hw->wiphy->bands[def->chan->band];
4127 	if (bss_conf->basic_rates)
4128 		basic_rate_idx = __ffs(bss_conf->basic_rates);
4129 	else
4130 		basic_rate_idx = 0;
4131 	bitrate = sband->bitrates[basic_rate_idx].bitrate;
4132 
4133 	hw_rate_code = ath12k_mac_get_rate_hw_value(bitrate);
4134 	if (hw_rate_code < 0) {
4135 		ath12k_warn(ar->ab, "bitrate not supported %d\n", bitrate);
4136 		return;
4137 	}
4138 
4139 	vdev_param = WMI_VDEV_PARAM_MGMT_RATE;
4140 	ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id, vdev_param,
4141 					    hw_rate_code);
4142 	if (ret)
4143 		ath12k_warn(ar->ab, "failed to set mgmt tx rate %d\n", ret);
4144 
4145 	vdev_param = WMI_VDEV_PARAM_BEACON_RATE;
4146 	ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id, vdev_param,
4147 					    hw_rate_code);
4148 	if (ret)
4149 		ath12k_warn(ar->ab, "failed to set beacon tx rate %d\n", ret);
4150 }
4151 
4152 static void ath12k_mac_bcn_tx_event(struct ath12k_link_vif *arvif)
4153 {
4154 	struct ieee80211_vif *vif = ath12k_ahvif_to_vif(arvif->ahvif);
4155 	struct ieee80211_bss_conf *link_conf;
4156 
4157 	link_conf = ath12k_mac_get_link_bss_conf(arvif);
4158 	if (!link_conf) {
4159 		ath12k_warn(arvif->ar->ab, "failed to get link conf for vdev %u\n",
4160 			    arvif->vdev_id);
4161 		return;
4162 	}
4163 
4164 	if (link_conf->color_change_active) {
4165 		if (ieee80211_beacon_cntdwn_is_complete(vif, arvif->link_id)) {
4166 			ieee80211_color_change_finish(vif, arvif->link_id);
4167 			return;
4168 		}
4169 
4170 		ieee80211_beacon_update_cntdwn(vif, arvif->link_id);
4171 		ath12k_mac_setup_bcn_tmpl(arvif);
4172 	}
4173 }
4174 
4175 static void ath12k_mac_bcn_tx_work(struct wiphy *wiphy, struct wiphy_work *work)
4176 {
4177 	struct ath12k_link_vif *arvif = container_of(work, struct ath12k_link_vif,
4178 						     bcn_tx_work);
4179 
4180 	lockdep_assert_wiphy(wiphy);
4181 	ath12k_mac_bcn_tx_event(arvif);
4182 }
4183 
4184 static void ath12k_mac_init_arvif(struct ath12k_vif *ahvif,
4185 				  struct ath12k_link_vif *arvif, int link_id)
4186 {
4187 	struct ath12k_hw *ah = ahvif->ah;
4188 	u8 _link_id;
4189 	int i;
4190 
4191 	lockdep_assert_wiphy(ah->hw->wiphy);
4192 
4193 	if (WARN_ON(!arvif))
4194 		return;
4195 
4196 	if (WARN_ON(link_id >= ATH12K_NUM_MAX_LINKS))
4197 		return;
4198 
4199 	if (link_id < 0)
4200 		_link_id = 0;
4201 	else
4202 		_link_id = link_id;
4203 
4204 	arvif->ahvif = ahvif;
4205 	arvif->link_id = _link_id;
4206 
4207 	/* Protects the datapath stats update on a per link basis */
4208 	spin_lock_init(&arvif->link_stats_lock);
4209 
4210 	INIT_LIST_HEAD(&arvif->list);
4211 	INIT_DELAYED_WORK(&arvif->connection_loss_work,
4212 			  ath12k_mac_vif_sta_connection_loss_work);
4213 	wiphy_work_init(&arvif->bcn_tx_work, ath12k_mac_bcn_tx_work);
4214 
4215 	arvif->num_stations = 0;
4216 
4217 	for (i = 0; i < ARRAY_SIZE(arvif->bitrate_mask.control); i++) {
4218 		arvif->bitrate_mask.control[i].legacy = 0xffffffff;
4219 		arvif->bitrate_mask.control[i].gi = NL80211_TXRATE_DEFAULT_GI;
4220 		memset(arvif->bitrate_mask.control[i].ht_mcs, 0xff,
4221 		       sizeof(arvif->bitrate_mask.control[i].ht_mcs));
4222 		memset(arvif->bitrate_mask.control[i].vht_mcs, 0xff,
4223 		       sizeof(arvif->bitrate_mask.control[i].vht_mcs));
4224 		memset(arvif->bitrate_mask.control[i].he_mcs, 0xff,
4225 		       sizeof(arvif->bitrate_mask.control[i].he_mcs));
4226 		memset(arvif->bitrate_mask.control[i].eht_mcs, 0xff,
4227 		       sizeof(arvif->bitrate_mask.control[i].eht_mcs));
4228 	}
4229 
4230 	/* Handle MLO related assignments */
4231 	if (link_id >= 0) {
4232 		rcu_assign_pointer(ahvif->link[arvif->link_id], arvif);
4233 		ahvif->links_map |= BIT(_link_id);
4234 	}
4235 
4236 	ath12k_generic_dbg(ATH12K_DBG_MAC,
4237 			   "mac init link arvif (link_id %d%s) for vif %pM. links_map 0x%x",
4238 			   _link_id, (link_id < 0) ? " deflink" : "", ahvif->vif->addr,
4239 			   ahvif->links_map);
4240 }
4241 
4242 static void ath12k_mac_remove_link_interface(struct ieee80211_hw *hw,
4243 					     struct ath12k_link_vif *arvif)
4244 {
4245 	struct ath12k_vif *ahvif = arvif->ahvif;
4246 	struct ath12k_hw *ah = hw->priv;
4247 	struct ath12k *ar = arvif->ar;
4248 	int ret;
4249 
4250 	lockdep_assert_wiphy(ah->hw->wiphy);
4251 
4252 	cancel_delayed_work_sync(&arvif->connection_loss_work);
4253 	wiphy_work_cancel(ath12k_ar_to_hw(ar)->wiphy, &arvif->bcn_tx_work);
4254 
4255 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac remove link interface (vdev %d link id %d)",
4256 		   arvif->vdev_id, arvif->link_id);
4257 
4258 	if (test_bit(WMI_TLV_SERVICE_11D_OFFLOAD, ar->ab->wmi_ab.svc_map) &&
4259 	    ahvif->vdev_type == WMI_VDEV_TYPE_STA &&
4260 	    ahvif->vdev_subtype == WMI_VDEV_SUBTYPE_NONE)
4261 		ath12k_mac_11d_scan_stop(ar);
4262 
4263 	if (ahvif->vdev_type == WMI_VDEV_TYPE_AP) {
4264 		ret = ath12k_peer_delete(ar, arvif->vdev_id, arvif->bssid);
4265 		if (ret)
4266 			ath12k_warn(ar->ab, "failed to submit AP self-peer removal on vdev %d link id %d: %d",
4267 				    arvif->vdev_id, arvif->link_id, ret);
4268 
4269 		if (arvif->link_id < IEEE80211_MLD_MAX_NUM_LINKS)
4270 			ath12k_dp_peer_delete(&ah->dp_hw, arvif->bssid, NULL);
4271 	}
4272 	ath12k_mac_vdev_delete(ar, arvif);
4273 }
4274 
4275 static struct ath12k_link_vif *ath12k_mac_assign_link_vif(struct ath12k_hw *ah,
4276 							  struct ieee80211_vif *vif,
4277 							  u8 link_id)
4278 {
4279 	struct ath12k_vif *ahvif = ath12k_vif_to_ahvif(vif);
4280 	struct ath12k_link_vif *arvif;
4281 
4282 	lockdep_assert_wiphy(ah->hw->wiphy);
4283 
4284 	arvif = wiphy_dereference(ah->hw->wiphy, ahvif->link[link_id]);
4285 	if (arvif)
4286 		return arvif;
4287 
4288 	/* If this is the first link arvif being created for an ML VIF
4289 	 * use the preallocated deflink memory except for scan arvifs
4290 	 */
4291 	if (!ahvif->links_map && link_id < ATH12K_FIRST_SCAN_LINK) {
4292 		arvif = &ahvif->deflink;
4293 
4294 		if (vif->type == NL80211_IFTYPE_STATION)
4295 			arvif->is_sta_assoc_link = true;
4296 	} else {
4297 		arvif = kzalloc_obj(*arvif);
4298 		if (!arvif)
4299 			return NULL;
4300 	}
4301 
4302 	ath12k_mac_init_arvif(ahvif, arvif, link_id);
4303 
4304 	return arvif;
4305 }
4306 
4307 static void ath12k_mac_unassign_link_vif(struct ath12k_link_vif *arvif)
4308 {
4309 	struct ath12k_vif *ahvif = arvif->ahvif;
4310 	struct ath12k_hw *ah = ahvif->ah;
4311 
4312 	lockdep_assert_wiphy(ah->hw->wiphy);
4313 
4314 	rcu_assign_pointer(ahvif->link[arvif->link_id], NULL);
4315 	synchronize_rcu();
4316 	ahvif->links_map &= ~BIT(arvif->link_id);
4317 
4318 	if (arvif != &ahvif->deflink)
4319 		kfree(arvif);
4320 	else
4321 		memset(arvif, 0, sizeof(*arvif));
4322 }
4323 
4324 int
4325 ath12k_mac_op_change_vif_links(struct ieee80211_hw *hw,
4326 			       struct ieee80211_vif *vif,
4327 			       u16 old_links, u16 new_links,
4328 			       struct ieee80211_bss_conf *ol[IEEE80211_MLD_MAX_NUM_LINKS])
4329 {
4330 	struct ath12k_vif *ahvif = ath12k_vif_to_ahvif(vif);
4331 	unsigned long to_remove = old_links & ~new_links;
4332 	unsigned long to_add = ~old_links & new_links;
4333 	struct ath12k_hw *ah = ath12k_hw_to_ah(hw);
4334 	struct ath12k_link_vif *arvif;
4335 	u8 link_id;
4336 
4337 	lockdep_assert_wiphy(hw->wiphy);
4338 
4339 	ath12k_generic_dbg(ATH12K_DBG_MAC,
4340 			   "mac vif link changed for MLD %pM old_links 0x%x new_links 0x%x\n",
4341 			   vif->addr, old_links, new_links);
4342 
4343 	for_each_set_bit(link_id, &to_add, IEEE80211_MLD_MAX_NUM_LINKS) {
4344 		arvif = wiphy_dereference(hw->wiphy, ahvif->link[link_id]);
4345 		/* mac80211 wants to add link but driver already has the
4346 		 * link. This should not happen ideally.
4347 		 */
4348 		if (WARN_ON(arvif))
4349 			return -EINVAL;
4350 
4351 		arvif = ath12k_mac_assign_link_vif(ah, vif, link_id);
4352 		if (WARN_ON(!arvif))
4353 			return -EINVAL;
4354 	}
4355 
4356 	for_each_set_bit(link_id, &to_remove, IEEE80211_MLD_MAX_NUM_LINKS) {
4357 		arvif = wiphy_dereference(hw->wiphy, ahvif->link[link_id]);
4358 		if (WARN_ON(!arvif))
4359 			return -EINVAL;
4360 
4361 		if (!arvif->is_created) {
4362 			ath12k_mac_unassign_link_vif(arvif);
4363 			continue;
4364 		}
4365 
4366 		if (WARN_ON(!arvif->ar))
4367 			return -EINVAL;
4368 
4369 		ath12k_mac_remove_link_interface(hw, arvif);
4370 		ath12k_mac_unassign_link_vif(arvif);
4371 	}
4372 
4373 	return 0;
4374 }
4375 EXPORT_SYMBOL(ath12k_mac_op_change_vif_links);
4376 
4377 static int ath12k_mac_fils_discovery(struct ath12k_link_vif *arvif,
4378 				     struct ieee80211_bss_conf *info)
4379 {
4380 	struct ieee80211_vif *vif = ath12k_ahvif_to_vif(arvif->ahvif);
4381 	struct ath12k *ar = arvif->ar;
4382 	struct ieee80211_hw *hw = ath12k_ar_to_hw(ar);
4383 	struct sk_buff *tmpl;
4384 	int ret;
4385 	u32 interval;
4386 	bool unsol_bcast_probe_resp_enabled = false;
4387 
4388 	if (info->fils_discovery.max_interval) {
4389 		interval = info->fils_discovery.max_interval;
4390 
4391 		tmpl = ieee80211_get_fils_discovery_tmpl(hw, vif,
4392 							 info->link_id);
4393 		if (tmpl)
4394 			ret = ath12k_wmi_fils_discovery_tmpl(ar, arvif->vdev_id,
4395 							     tmpl);
4396 	} else if (info->unsol_bcast_probe_resp_interval) {
4397 		unsol_bcast_probe_resp_enabled = 1;
4398 		interval = info->unsol_bcast_probe_resp_interval;
4399 
4400 		tmpl = ieee80211_get_unsol_bcast_probe_resp_tmpl(hw, vif,
4401 								 info->link_id);
4402 		if (tmpl)
4403 			ret = ath12k_wmi_probe_resp_tmpl(ar, arvif->vdev_id,
4404 							 tmpl);
4405 	} else { /* Disable */
4406 		return ath12k_wmi_fils_discovery(ar, arvif->vdev_id, 0, false);
4407 	}
4408 
4409 	if (!tmpl) {
4410 		ath12k_warn(ar->ab,
4411 			    "mac vdev %i failed to retrieve %s template\n",
4412 			    arvif->vdev_id, (unsol_bcast_probe_resp_enabled ?
4413 			    "unsolicited broadcast probe response" :
4414 			    "FILS discovery"));
4415 		return -EPERM;
4416 	}
4417 	kfree_skb(tmpl);
4418 
4419 	if (!ret)
4420 		ret = ath12k_wmi_fils_discovery(ar, arvif->vdev_id, interval,
4421 						unsol_bcast_probe_resp_enabled);
4422 
4423 	return ret;
4424 }
4425 
4426 void ath12k_mac_op_vif_cfg_changed(struct ieee80211_hw *hw,
4427 				   struct ieee80211_vif *vif,
4428 				   u64 changed)
4429 {
4430 	struct ath12k_vif *ahvif = ath12k_vif_to_ahvif(vif);
4431 	unsigned long links = ahvif->links_map;
4432 	struct ieee80211_bss_conf *info;
4433 	struct ath12k_link_vif *arvif;
4434 	struct ieee80211_sta *sta;
4435 	struct ath12k_sta *ahsta;
4436 	struct ath12k *ar;
4437 	u8 link_id;
4438 
4439 	lockdep_assert_wiphy(hw->wiphy);
4440 
4441 	if (changed & BSS_CHANGED_SSID && vif->type == NL80211_IFTYPE_AP) {
4442 		ahvif->u.ap.ssid_len = vif->cfg.ssid_len;
4443 		if (vif->cfg.ssid_len)
4444 			memcpy(ahvif->u.ap.ssid, vif->cfg.ssid, vif->cfg.ssid_len);
4445 	}
4446 
4447 	if (changed & BSS_CHANGED_ASSOC) {
4448 		if (vif->cfg.assoc) {
4449 			/* only in station mode we can get here, so it's safe
4450 			 * to use ap_addr
4451 			 */
4452 			rcu_read_lock();
4453 			sta = ieee80211_find_sta(vif, vif->cfg.ap_addr);
4454 			if (!sta) {
4455 				rcu_read_unlock();
4456 				WARN_ONCE(1, "failed to find sta with addr %pM\n",
4457 					  vif->cfg.ap_addr);
4458 				return;
4459 			}
4460 
4461 			ahsta = ath12k_sta_to_ahsta(sta);
4462 			arvif = wiphy_dereference(hw->wiphy,
4463 						  ahvif->link[ahsta->assoc_link_id]);
4464 			rcu_read_unlock();
4465 
4466 			ar = arvif->ar;
4467 			/* there is no reason for which an assoc link's
4468 			 * bss info does not exist
4469 			 */
4470 			info = ath12k_mac_get_link_bss_conf(arvif);
4471 			ath12k_bss_assoc(ar, arvif, info);
4472 
4473 			/* exclude assoc link as it is done above */
4474 			links &= ~BIT(ahsta->assoc_link_id);
4475 		}
4476 
4477 		for_each_set_bit(link_id, &links, IEEE80211_MLD_MAX_NUM_LINKS) {
4478 			arvif = wiphy_dereference(hw->wiphy, ahvif->link[link_id]);
4479 			if (!arvif || !arvif->ar)
4480 				continue;
4481 
4482 			ar = arvif->ar;
4483 
4484 			if (vif->cfg.assoc) {
4485 				info = ath12k_mac_get_link_bss_conf(arvif);
4486 				if (!info)
4487 					continue;
4488 
4489 				ath12k_bss_assoc(ar, arvif, info);
4490 			} else {
4491 				ath12k_bss_disassoc(ar, arvif);
4492 			}
4493 		}
4494 	}
4495 }
4496 EXPORT_SYMBOL(ath12k_mac_op_vif_cfg_changed);
4497 
4498 static void ath12k_mac_vif_setup_ps(struct ath12k_link_vif *arvif)
4499 {
4500 	struct ath12k *ar = arvif->ar;
4501 	struct ieee80211_vif *vif = arvif->ahvif->vif;
4502 	struct ieee80211_conf *conf = &ath12k_ar_to_hw(ar)->conf;
4503 	enum wmi_sta_powersave_param param;
4504 	struct ieee80211_bss_conf *info;
4505 	enum wmi_sta_ps_mode psmode;
4506 	int ret;
4507 	int timeout;
4508 	bool enable_ps;
4509 
4510 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
4511 
4512 	if (vif->type != NL80211_IFTYPE_STATION)
4513 		return;
4514 
4515 	enable_ps = arvif->ahvif->ps;
4516 	if (enable_ps) {
4517 		psmode = WMI_STA_PS_MODE_ENABLED;
4518 		param = WMI_STA_PS_PARAM_INACTIVITY_TIME;
4519 
4520 		timeout = conf->dynamic_ps_timeout;
4521 		if (timeout == 0) {
4522 			info = ath12k_mac_get_link_bss_conf(arvif);
4523 			if (!info) {
4524 				ath12k_warn(ar->ab, "unable to access bss link conf in setup ps for vif %pM link %u\n",
4525 					    vif->addr, arvif->link_id);
4526 				return;
4527 			}
4528 
4529 			/* firmware doesn't like 0 */
4530 			timeout = ieee80211_tu_to_usec(info->beacon_int) / 1000;
4531 		}
4532 
4533 		ret = ath12k_wmi_set_sta_ps_param(ar, arvif->vdev_id, param,
4534 						  timeout);
4535 		if (ret) {
4536 			ath12k_warn(ar->ab, "failed to set inactivity time for vdev %d: %i\n",
4537 				    arvif->vdev_id, ret);
4538 			return;
4539 		}
4540 	} else {
4541 		psmode = WMI_STA_PS_MODE_DISABLED;
4542 	}
4543 
4544 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac vdev %d psmode %s\n",
4545 		   arvif->vdev_id, psmode ? "enable" : "disable");
4546 
4547 	ret = ath12k_wmi_pdev_set_ps_mode(ar, arvif->vdev_id, psmode);
4548 	if (ret)
4549 		ath12k_warn(ar->ab, "failed to set sta power save mode %d for vdev %d: %d\n",
4550 			    psmode, arvif->vdev_id, ret);
4551 }
4552 
4553 static bool ath12k_mac_supports_tpc(struct ath12k *ar, struct ath12k_vif *ahvif,
4554 				    const struct cfg80211_chan_def *chandef)
4555 {
4556 	return ath12k_wmi_supports_6ghz_cc_ext(ar) &&
4557 		test_bit(WMI_TLV_SERVICE_EXT_TPC_REG_SUPPORT, ar->ab->wmi_ab.svc_map) &&
4558 		(ahvif->vdev_type == WMI_VDEV_TYPE_STA  ||
4559 		 ahvif->vdev_type == WMI_VDEV_TYPE_AP) &&
4560 		ahvif->vdev_subtype == WMI_VDEV_SUBTYPE_NONE &&
4561 		chandef->chan &&
4562 		chandef->chan->band == NL80211_BAND_6GHZ;
4563 }
4564 
4565 static void ath12k_wmi_vdev_params_up(struct ath12k *ar,
4566 				      struct ath12k_link_vif *arvif,
4567 				      struct ath12k_link_vif *tx_arvif,
4568 				      struct ieee80211_bss_conf *info, u16 aid)
4569 {
4570 	struct ath12k_wmi_vdev_up_params params = {
4571 		.vdev_id = arvif->vdev_id,
4572 		.aid = aid,
4573 		.bssid = arvif->bssid
4574 	};
4575 	int ret;
4576 
4577 	if (tx_arvif) {
4578 		params.tx_bssid = tx_arvif->bssid;
4579 		params.nontx_profile_idx = info->bssid_index;
4580 		params.nontx_profile_cnt = 1 << info->bssid_indicator;
4581 	}
4582 
4583 	ret = ath12k_wmi_vdev_up(arvif->ar, &params);
4584 	if (ret)
4585 		ath12k_warn(ar->ab, "failed to bring vdev up %d: %d\n",
4586 			    arvif->vdev_id, ret);
4587 }
4588 
4589 static int ath12k_mac_config_obss_pd(struct ath12k_link_vif *arvif,
4590 				     const struct ieee80211_he_obss_pd *he_obss_pd)
4591 {
4592 	struct ath12k_wmi_obss_pd_arg obss_pd_arg = {};
4593 	u32 srg_bitmap[2], non_srg_bitmap[2];
4594 	struct ath12k *ar = arvif->ar;
4595 	u32 param_id, pdev_id;
4596 	u32 param_val;
4597 	int ret;
4598 
4599 	if (ar->ab->hw_params->single_pdev_only)
4600 		pdev_id = ath12k_mac_get_target_pdev_id_from_vif(arvif);
4601 	else
4602 		pdev_id = ar->pdev->pdev_id;
4603 
4604 	/* Set and enable SRG/non-SRG OBSS PD threshold */
4605 	param_id = WMI_PDEV_PARAM_SET_CMD_OBSS_PD_THRESHOLD;
4606 	if (ar->monitor_started || !he_obss_pd->enable) {
4607 		ret = ath12k_wmi_pdev_set_param(ar, param_id, 0, pdev_id);
4608 		if (ret)
4609 			ath12k_warn(ar->ab,
4610 				    "failed to set OBSS PD threshold for pdev %u: %d\n",
4611 				    pdev_id, ret);
4612 		return ret;
4613 	}
4614 
4615 	/*
4616 	 * This service flag indicates firmware support for SRG/SRP-based
4617 	 * spatial reuse. It also specifies whether OBSS PD threshold values
4618 	 * should be interpreted as dB (offset) or dBm (absolute) units.
4619 	 */
4620 	obss_pd_arg.srp_support = test_bit(WMI_TLV_SERVICE_SRG_SRP_SPATIAL_REUSE_SUPPORT,
4621 					   ar->ab->wmi_ab.svc_map);
4622 
4623 	if (!(he_obss_pd->sr_ctrl &
4624 	      IEEE80211_HE_SPR_NON_SRG_OBSS_PD_SR_DISALLOWED)) {
4625 		if (he_obss_pd->sr_ctrl & IEEE80211_HE_SPR_NON_SRG_OFFSET_PRESENT)
4626 			obss_pd_arg.non_srg_th = ATH12K_OBSS_PD_MAX_THRESHOLD +
4627 						 he_obss_pd->non_srg_max_offset;
4628 		else
4629 			obss_pd_arg.non_srg_th = ATH12K_OBSS_PD_NON_SRG_MAX_THRESHOLD;
4630 
4631 		if (!obss_pd_arg.srp_support)
4632 			obss_pd_arg.non_srg_th -= ATH12K_DEFAULT_NOISE_FLOOR;
4633 
4634 		obss_pd_arg.non_srg_enabled = true;
4635 	}
4636 
4637 	if (he_obss_pd->sr_ctrl & IEEE80211_HE_SPR_SRG_INFORMATION_PRESENT) {
4638 		obss_pd_arg.srg_th = ATH12K_OBSS_PD_MAX_THRESHOLD +
4639 				     he_obss_pd->max_offset;
4640 		obss_pd_arg.srg_enabled = true;
4641 	}
4642 
4643 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
4644 		   "pdev %u OBSS PD sr_ctrl 0x%x srg_th %d dBm non_srg_th %d dBm\n",
4645 		   pdev_id, he_obss_pd->sr_ctrl,
4646 		   obss_pd_arg.srg_th, obss_pd_arg.non_srg_th);
4647 
4648 	param_val = ath12k_wmi_build_obss_pd(&obss_pd_arg);
4649 	ret = ath12k_wmi_pdev_set_param(ar, param_id, param_val, pdev_id);
4650 	if (ret) {
4651 		ath12k_warn(ar->ab,
4652 			    "failed to set OBSS PD threshold for pdev %u: %d\n",
4653 			    pdev_id, ret);
4654 		return ret;
4655 	}
4656 
4657 	/* Enable OBSS PD for all access category */
4658 	param_id  = WMI_PDEV_PARAM_SET_CMD_OBSS_PD_PER_AC;
4659 	param_val = 0xf;
4660 	ret = ath12k_wmi_pdev_set_param(ar, param_id, param_val, pdev_id);
4661 	if (ret) {
4662 		ath12k_warn(ar->ab,
4663 			    "failed to set OBSS PD per ac for pdev %u: %d\n",
4664 			    pdev_id, ret);
4665 		return ret;
4666 	}
4667 
4668 	/* Set SR prohibit */
4669 	param_id  = WMI_PDEV_PARAM_ENABLE_SR_PROHIBIT;
4670 	param_val = !!(he_obss_pd->sr_ctrl &
4671 		       IEEE80211_HE_SPR_HESIGA_SR_VAL15_ALLOWED);
4672 	ret = ath12k_wmi_pdev_set_param(ar, param_id, param_val, pdev_id);
4673 	if (ret) {
4674 		ath12k_warn(ar->ab, "failed to set SR prohibit for pdev %u: %d\n",
4675 			    pdev_id, ret);
4676 		return ret;
4677 	}
4678 
4679 	if (!obss_pd_arg.srp_support)
4680 		return 0;
4681 
4682 	memcpy(srg_bitmap, he_obss_pd->bss_color_bitmap, sizeof(srg_bitmap));
4683 	/* Set SRG BSS color bitmap */
4684 	ret = ath12k_wmi_pdev_set_srg_bss_color_bitmap(ar, pdev_id, srg_bitmap);
4685 	if (ret) {
4686 		ath12k_warn(ar->ab,
4687 			    "failed to set SRG bss color bitmap for pdev %u: %d\n",
4688 			    pdev_id, ret);
4689 		return ret;
4690 	}
4691 
4692 	/* Enable BSS colors for SRG */
4693 	ret = ath12k_wmi_pdev_srg_obss_color_enable_bitmap(ar, pdev_id, srg_bitmap);
4694 	if (ret) {
4695 		ath12k_warn(ar->ab,
4696 			    "failed to enable SRG bss color bitmap pdev %u: %d\n",
4697 			    pdev_id, ret);
4698 		return ret;
4699 	}
4700 
4701 	memcpy(srg_bitmap, he_obss_pd->partial_bssid_bitmap, sizeof(srg_bitmap));
4702 	/* Set SRG partial bssid bitmap */
4703 	ret = ath12k_wmi_pdev_set_srg_partial_bssid_bitmap(ar, pdev_id, srg_bitmap);
4704 	if (ret) {
4705 		ath12k_warn(ar->ab,
4706 			    "failed to set SRG partial bssid bitmap for pdev %u: %d\n",
4707 			    pdev_id, ret);
4708 		return ret;
4709 	}
4710 
4711 	/* Enable partial bssid mask for SRG */
4712 	ret = ath12k_wmi_pdev_srg_obss_bssid_enable_bitmap(ar, pdev_id, srg_bitmap);
4713 	if (ret) {
4714 		ath12k_warn(ar->ab,
4715 			    "failed to enable SRG bssid bitmap pdev %u: %d\n",
4716 			    pdev_id, ret);
4717 		return ret;
4718 	}
4719 
4720 	/*
4721 	 * No explicit non-SRG bitmap from mac80211; enable all colors/bssids
4722 	 * as non-SRG candidates. Actual SRG members are filtered by SRG bitmaps.
4723 	 */
4724 	memset(non_srg_bitmap, 0xff, sizeof(non_srg_bitmap));
4725 
4726 	/* Enable BSS colors for non-SRG */
4727 	ret = ath12k_wmi_pdev_non_srg_obss_color_enable_bitmap(ar, pdev_id,
4728 							       non_srg_bitmap);
4729 	if (ret) {
4730 		ath12k_warn(ar->ab,
4731 			    "failed to enable non SRG color bitmap pdev %u: %d\n",
4732 			    pdev_id, ret);
4733 		return ret;
4734 	}
4735 
4736 	/* Enable partial bssid mask for non-SRG */
4737 	ret = ath12k_wmi_pdev_non_srg_obss_bssid_enable_bitmap(ar, pdev_id,
4738 							       non_srg_bitmap);
4739 	if (ret) {
4740 		ath12k_warn(ar->ab,
4741 			    "failed to enable non SRG bssid bitmap pdev %u: %d\n",
4742 			    pdev_id, ret);
4743 		return ret;
4744 	}
4745 
4746 	return 0;
4747 }
4748 
4749 static void ath12k_mac_bss_info_changed(struct ath12k *ar,
4750 					struct ath12k_link_vif *arvif,
4751 					struct ieee80211_bss_conf *info,
4752 					u64 changed)
4753 {
4754 	struct ath12k_vif *ahvif = arvif->ahvif;
4755 	struct ieee80211_vif *vif = ath12k_ahvif_to_vif(ahvif);
4756 	struct ieee80211_vif_cfg *vif_cfg = &vif->cfg;
4757 	struct ath12k_link_vif *tx_arvif;
4758 	struct cfg80211_chan_def def;
4759 	u32 param_id, param_value;
4760 	enum nl80211_band band;
4761 	u32 vdev_param;
4762 	int mcast_rate;
4763 	u32 preamble;
4764 	u16 hw_value;
4765 	u16 bitrate;
4766 	u8 rateidx;
4767 	u32 rate;
4768 	int ret;
4769 
4770 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
4771 
4772 	if (changed & BSS_CHANGED_BEACON_INT) {
4773 		arvif->beacon_interval = info->beacon_int;
4774 
4775 		param_id = WMI_VDEV_PARAM_BEACON_INTERVAL;
4776 		ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
4777 						    param_id,
4778 						    arvif->beacon_interval);
4779 		if (ret)
4780 			ath12k_warn(ar->ab, "Failed to set beacon interval for VDEV: %d\n",
4781 				    arvif->vdev_id);
4782 		else
4783 			ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
4784 				   "Beacon interval: %d set for VDEV: %d\n",
4785 				   arvif->beacon_interval, arvif->vdev_id);
4786 	}
4787 
4788 	if (changed & BSS_CHANGED_BEACON) {
4789 		param_id = WMI_PDEV_PARAM_BEACON_TX_MODE;
4790 		param_value = WMI_BEACON_BURST_MODE;
4791 		ret = ath12k_wmi_pdev_set_param(ar, param_id,
4792 						param_value, ar->pdev->pdev_id);
4793 		if (ret)
4794 			ath12k_warn(ar->ab, "Failed to set beacon mode for VDEV: %d\n",
4795 				    arvif->vdev_id);
4796 		else
4797 			ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
4798 				   "Set burst beacon mode for VDEV: %d\n",
4799 				   arvif->vdev_id);
4800 
4801 		/* In MBSSID case, need to install transmitting VIF's template first */
4802 
4803 		ret = ath12k_mac_setup_bcn_tmpl(arvif);
4804 		if (ret)
4805 			ath12k_warn(ar->ab, "failed to update bcn template: %d\n",
4806 				    ret);
4807 
4808 		if (!arvif->is_csa_in_progress)
4809 			goto skip_vdev_up;
4810 
4811 		tx_arvif = ath12k_mac_get_tx_arvif(arvif, info);
4812 		if (tx_arvif && arvif != tx_arvif && tx_arvif->is_csa_in_progress)
4813 			/* skip non tx vif's */
4814 			goto skip_vdev_up;
4815 
4816 		ath12k_wmi_vdev_params_up(ar, arvif, tx_arvif, info, ahvif->aid);
4817 
4818 		arvif->is_csa_in_progress = false;
4819 
4820 		if (tx_arvif && arvif == tx_arvif) {
4821 			struct ath12k_link_vif *arvif_itr;
4822 
4823 			list_for_each_entry(arvif_itr, &ar->arvifs, list) {
4824 				if (!arvif_itr->is_csa_in_progress)
4825 					continue;
4826 
4827 				ath12k_wmi_vdev_params_up(ar, arvif, tx_arvif,
4828 							  info, ahvif->aid);
4829 				arvif_itr->is_csa_in_progress = false;
4830 			}
4831 		}
4832 	}
4833 
4834 skip_vdev_up:
4835 
4836 	if (changed & (BSS_CHANGED_BEACON_INFO | BSS_CHANGED_BEACON)) {
4837 		arvif->dtim_period = info->dtim_period;
4838 
4839 		param_id = WMI_VDEV_PARAM_DTIM_PERIOD;
4840 		ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
4841 						    param_id,
4842 						    arvif->dtim_period);
4843 
4844 		if (ret)
4845 			ath12k_warn(ar->ab, "Failed to set dtim period for VDEV %d: %i\n",
4846 				    arvif->vdev_id, ret);
4847 		else
4848 			ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
4849 				   "DTIM period: %d set for VDEV: %d\n",
4850 				   arvif->dtim_period, arvif->vdev_id);
4851 	}
4852 
4853 	if (changed & BSS_CHANGED_SSID &&
4854 	    vif->type == NL80211_IFTYPE_AP) {
4855 		ahvif->u.ap.ssid_len = vif->cfg.ssid_len;
4856 		if (vif->cfg.ssid_len)
4857 			memcpy(ahvif->u.ap.ssid, vif->cfg.ssid, vif->cfg.ssid_len);
4858 		ahvif->u.ap.hidden_ssid = info->hidden_ssid;
4859 	}
4860 
4861 	if (changed & BSS_CHANGED_BSSID && !is_zero_ether_addr(info->bssid))
4862 		ether_addr_copy(arvif->bssid, info->bssid);
4863 
4864 	if (changed & BSS_CHANGED_BEACON_ENABLED) {
4865 		if (info->enable_beacon) {
4866 			ret = ath12k_mac_set_he_txbf_conf(arvif);
4867 			if (ret)
4868 				ath12k_warn(ar->ab,
4869 					    "failed to set HE TXBF config for vdev: %d\n",
4870 					    arvif->vdev_id);
4871 
4872 			ret = ath12k_mac_set_eht_txbf_conf(arvif);
4873 			if (ret)
4874 				ath12k_warn(ar->ab,
4875 					    "failed to set EHT TXBF config for vdev: %d\n",
4876 					    arvif->vdev_id);
4877 		}
4878 		ath12k_control_beaconing(arvif, info);
4879 
4880 		if (arvif->is_up && info->he_support &&
4881 		    info->he_oper.params) {
4882 			/* TODO: Extend to support 1024 BA Bitmap size */
4883 			ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
4884 							    WMI_VDEV_PARAM_BA_MODE,
4885 							    WMI_BA_MODE_BUFFER_SIZE_256);
4886 			if (ret)
4887 				ath12k_warn(ar->ab,
4888 					    "failed to set BA BUFFER SIZE 256 for vdev: %d\n",
4889 					    arvif->vdev_id);
4890 
4891 			param_id = WMI_VDEV_PARAM_HEOPS_0_31;
4892 			param_value = info->he_oper.params;
4893 			ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
4894 							    param_id, param_value);
4895 			ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
4896 				   "he oper param: %x set for VDEV: %d\n",
4897 				   param_value, arvif->vdev_id);
4898 
4899 			if (ret)
4900 				ath12k_warn(ar->ab, "Failed to set he oper params %x for VDEV %d: %i\n",
4901 					    param_value, arvif->vdev_id, ret);
4902 		}
4903 	}
4904 
4905 	if (changed & BSS_CHANGED_ERP_CTS_PROT) {
4906 		u32 cts_prot;
4907 
4908 		cts_prot = !!(info->use_cts_prot);
4909 		param_id = WMI_VDEV_PARAM_PROTECTION_MODE;
4910 
4911 		if (arvif->is_started) {
4912 			ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
4913 							    param_id, cts_prot);
4914 			if (ret)
4915 				ath12k_warn(ar->ab, "Failed to set CTS prot for VDEV: %d\n",
4916 					    arvif->vdev_id);
4917 			else
4918 				ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "Set CTS prot: %d for VDEV: %d\n",
4919 					   cts_prot, arvif->vdev_id);
4920 		} else {
4921 			ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "defer protection mode setup, vdev is not ready yet\n");
4922 		}
4923 	}
4924 
4925 	if (changed & BSS_CHANGED_ERP_SLOT) {
4926 		u32 slottime;
4927 
4928 		if (info->use_short_slot)
4929 			slottime = WMI_VDEV_SLOT_TIME_SHORT; /* 9us */
4930 
4931 		else
4932 			slottime = WMI_VDEV_SLOT_TIME_LONG; /* 20us */
4933 
4934 		param_id = WMI_VDEV_PARAM_SLOT_TIME;
4935 		ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
4936 						    param_id, slottime);
4937 		if (ret)
4938 			ath12k_warn(ar->ab, "Failed to set erp slot for VDEV: %d\n",
4939 				    arvif->vdev_id);
4940 		else
4941 			ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
4942 				   "Set slottime: %d for VDEV: %d\n",
4943 				   slottime, arvif->vdev_id);
4944 	}
4945 
4946 	if (changed & BSS_CHANGED_ERP_PREAMBLE) {
4947 		u32 preamble;
4948 
4949 		if (info->use_short_preamble)
4950 			preamble = WMI_VDEV_PREAMBLE_SHORT;
4951 		else
4952 			preamble = WMI_VDEV_PREAMBLE_LONG;
4953 
4954 		param_id = WMI_VDEV_PARAM_PREAMBLE;
4955 		ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
4956 						    param_id, preamble);
4957 		if (ret)
4958 			ath12k_warn(ar->ab, "Failed to set preamble for VDEV: %d\n",
4959 				    arvif->vdev_id);
4960 		else
4961 			ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
4962 				   "Set preamble: %d for VDEV: %d\n",
4963 				   preamble, arvif->vdev_id);
4964 	}
4965 
4966 	if (changed & BSS_CHANGED_ASSOC) {
4967 		if (vif->cfg.assoc)
4968 			ath12k_bss_assoc(ar, arvif, info);
4969 		else
4970 			ath12k_bss_disassoc(ar, arvif);
4971 	}
4972 
4973 	if (changed & BSS_CHANGED_TXPOWER) {
4974 		ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac vdev_id %i txpower %d\n",
4975 			   arvif->vdev_id, info->txpower);
4976 
4977 		arvif->txpower = info->txpower;
4978 		ath12k_mac_txpower_recalc(ar);
4979 	}
4980 
4981 	if (changed & BSS_CHANGED_MCAST_RATE &&
4982 	    !ath12k_mac_vif_link_chan(vif, arvif->link_id, &def)) {
4983 		band = def.chan->band;
4984 		mcast_rate = info->mcast_rate[band];
4985 
4986 		if (mcast_rate > 0) {
4987 			rateidx = mcast_rate - 1;
4988 		} else {
4989 			if (info->basic_rates)
4990 				rateidx = __ffs(info->basic_rates);
4991 			else
4992 				rateidx = 0;
4993 		}
4994 
4995 		if (ar->pdev->cap.supported_bands & WMI_HOST_WLAN_5GHZ_CAP)
4996 			rateidx += ATH12K_MAC_FIRST_OFDM_RATE_IDX;
4997 
4998 		bitrate = ath12k_legacy_rates[rateidx].bitrate;
4999 		hw_value = ath12k_legacy_rates[rateidx].hw_value;
5000 
5001 		if (ath12k_mac_bitrate_is_cck(bitrate))
5002 			preamble = WMI_RATE_PREAMBLE_CCK;
5003 		else
5004 			preamble = WMI_RATE_PREAMBLE_OFDM;
5005 
5006 		rate = ATH12K_HW_RATE_CODE(hw_value, 0, preamble);
5007 
5008 		ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
5009 			   "mac vdev %d mcast_rate %x\n",
5010 			   arvif->vdev_id, rate);
5011 
5012 		vdev_param = WMI_VDEV_PARAM_MCAST_DATA_RATE;
5013 		ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
5014 						    vdev_param, rate);
5015 		if (ret)
5016 			ath12k_warn(ar->ab,
5017 				    "failed to set mcast rate on vdev %i: %d\n",
5018 				    arvif->vdev_id,  ret);
5019 
5020 		vdev_param = WMI_VDEV_PARAM_BCAST_DATA_RATE;
5021 		ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
5022 						    vdev_param, rate);
5023 		if (ret)
5024 			ath12k_warn(ar->ab,
5025 				    "failed to set bcast rate on vdev %i: %d\n",
5026 				    arvif->vdev_id,  ret);
5027 	}
5028 
5029 	if (changed & BSS_CHANGED_BASIC_RATES &&
5030 	    !ath12k_mac_vif_link_chan(vif, arvif->link_id, &def))
5031 		ath12k_recalculate_mgmt_rate(ar, arvif, &def);
5032 
5033 	if (changed & BSS_CHANGED_TWT) {
5034 		if (info->twt_requester || info->twt_responder)
5035 			ath12k_wmi_send_twt_enable_cmd(ar, ar->pdev->pdev_id);
5036 		else
5037 			ath12k_wmi_send_twt_disable_cmd(ar, ar->pdev->pdev_id);
5038 	}
5039 
5040 	if (changed & BSS_CHANGED_HE_OBSS_PD) {
5041 		if (vif->type == NL80211_IFTYPE_AP)
5042 			ath12k_mac_config_obss_pd(arvif, &info->he_obss_pd);
5043 		else
5044 			ath12k_wmi_send_obss_spr_cmd(ar, arvif->vdev_id,
5045 						     &info->he_obss_pd);
5046 	}
5047 
5048 	if (changed & BSS_CHANGED_HE_BSS_COLOR) {
5049 		if (vif->type == NL80211_IFTYPE_AP) {
5050 			ret = ath12k_wmi_obss_color_cfg_cmd(ar,
5051 							    arvif->vdev_id,
5052 							    info->he_bss_color.color,
5053 							    ATH12K_BSS_COLOR_AP_PERIODS,
5054 							    info->he_bss_color.enabled);
5055 			if (ret)
5056 				ath12k_warn(ar->ab, "failed to set bss color collision on vdev %u: %d\n",
5057 					    arvif->vdev_id,  ret);
5058 
5059 			param_id = WMI_VDEV_PARAM_BSS_COLOR;
5060 			if (info->he_bss_color.enabled)
5061 				param_value = info->he_bss_color.color <<
5062 					      IEEE80211_HE_OPERATION_BSS_COLOR_OFFSET;
5063 			else
5064 				param_value = IEEE80211_HE_OPERATION_BSS_COLOR_DISABLED;
5065 
5066 			ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
5067 							    param_id,
5068 							    param_value);
5069 			if (ret)
5070 				ath12k_warn(ar->ab, "failed to set bss color param on vdev %u: %d\n",
5071 					    arvif->vdev_id,  ret);
5072 			else
5073 				ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "bss color param 0x%x set on vdev %u\n",
5074 					   param_value, arvif->vdev_id);
5075 		} else if (vif->type == NL80211_IFTYPE_STATION) {
5076 			ret = ath12k_wmi_send_bss_color_change_enable_cmd(ar,
5077 									  arvif->vdev_id,
5078 									  1);
5079 			if (ret)
5080 				ath12k_warn(ar->ab, "failed to enable bss color change on vdev %i: %d\n",
5081 					    arvif->vdev_id,  ret);
5082 			ret = ath12k_wmi_obss_color_cfg_cmd(ar,
5083 							    arvif->vdev_id,
5084 							    0,
5085 							    ATH12K_BSS_COLOR_STA_PERIODS,
5086 							    1);
5087 			if (ret)
5088 				ath12k_warn(ar->ab, "failed to set bss color collision on vdev %i: %d\n",
5089 					    arvif->vdev_id,  ret);
5090 		}
5091 	}
5092 
5093 	ath12k_mac_fils_discovery(arvif, info);
5094 
5095 	if (changed & BSS_CHANGED_PS &&
5096 	    ar->ab->hw_params->supports_sta_ps) {
5097 		ahvif->ps = vif_cfg->ps;
5098 		ath12k_mac_vif_setup_ps(arvif);
5099 	}
5100 }
5101 
5102 static struct ath12k_vif_cache *ath12k_ahvif_get_link_cache(struct ath12k_vif *ahvif,
5103 							    u8 link_id)
5104 {
5105 	if (!ahvif->cache[link_id]) {
5106 		ahvif->cache[link_id] = kzalloc_obj(*ahvif->cache[0]);
5107 		if (ahvif->cache[link_id])
5108 			INIT_LIST_HEAD(&ahvif->cache[link_id]->key_conf.list);
5109 	}
5110 
5111 	return ahvif->cache[link_id];
5112 }
5113 
5114 static void ath12k_ahvif_put_link_key_cache(struct ath12k_vif_cache *cache)
5115 {
5116 	struct ath12k_key_conf *key_conf, *tmp;
5117 
5118 	if (!cache || list_empty(&cache->key_conf.list))
5119 		return;
5120 	list_for_each_entry_safe(key_conf, tmp, &cache->key_conf.list, list) {
5121 		list_del(&key_conf->list);
5122 		kfree(key_conf);
5123 	}
5124 }
5125 
5126 static void ath12k_ahvif_put_link_cache(struct ath12k_vif *ahvif, u8 link_id)
5127 {
5128 	if (link_id >= IEEE80211_MLD_MAX_NUM_LINKS)
5129 		return;
5130 
5131 	ath12k_ahvif_put_link_key_cache(ahvif->cache[link_id]);
5132 	kfree(ahvif->cache[link_id]);
5133 	ahvif->cache[link_id] = NULL;
5134 }
5135 
5136 void ath12k_mac_op_link_info_changed(struct ieee80211_hw *hw,
5137 				     struct ieee80211_vif *vif,
5138 				     struct ieee80211_bss_conf *info,
5139 				     u64 changed)
5140 {
5141 	struct ath12k *ar;
5142 	struct ath12k_vif *ahvif = ath12k_vif_to_ahvif(vif);
5143 	struct ath12k_vif_cache *cache;
5144 	struct ath12k_link_vif *arvif;
5145 	u8 link_id = info->link_id;
5146 
5147 	lockdep_assert_wiphy(hw->wiphy);
5148 
5149 	arvif = wiphy_dereference(hw->wiphy, ahvif->link[link_id]);
5150 
5151 	/* if the vdev is not created on a certain radio,
5152 	 * cache the info to be updated later on vdev creation
5153 	 */
5154 
5155 	if (!arvif || !arvif->is_created) {
5156 		cache = ath12k_ahvif_get_link_cache(ahvif, link_id);
5157 		if (!cache)
5158 			return;
5159 
5160 		cache->bss_conf_changed |= changed;
5161 
5162 		return;
5163 	}
5164 
5165 	ar = arvif->ar;
5166 
5167 	ath12k_mac_bss_info_changed(ar, arvif, info, changed);
5168 }
5169 EXPORT_SYMBOL(ath12k_mac_op_link_info_changed);
5170 
5171 static struct ath12k*
5172 ath12k_mac_select_scan_device(struct ieee80211_hw *hw,
5173 			      struct ieee80211_vif *vif,
5174 			      u32 center_freq)
5175 {
5176 	struct ath12k_hw *ah = hw->priv;
5177 	enum nl80211_band band;
5178 	struct ath12k *ar;
5179 	int i;
5180 
5181 	if (ah->num_radio == 1)
5182 		return ah->radio;
5183 
5184 	/* Currently mac80211 supports splitting scan requests into
5185 	 * multiple scan requests per band.
5186 	 * Loop through first channel and determine the scan radio
5187 	 * TODO: There could be 5 GHz low/high channels in that case
5188 	 * split the hw request and perform multiple scans
5189 	 */
5190 
5191 	if (center_freq < ATH12K_MIN_5GHZ_FREQ)
5192 		band = NL80211_BAND_2GHZ;
5193 	else if (center_freq < ATH12K_MIN_6GHZ_FREQ)
5194 		band = NL80211_BAND_5GHZ;
5195 	else
5196 		band = NL80211_BAND_6GHZ;
5197 
5198 	for_each_ar(ah, ar, i) {
5199 		if (ar->mac.sbands[band].channels &&
5200 		    center_freq >= KHZ_TO_MHZ(ar->freq_range.start_freq) &&
5201 		    center_freq <= KHZ_TO_MHZ(ar->freq_range.end_freq))
5202 			return ar;
5203 	}
5204 
5205 	return NULL;
5206 }
5207 
5208 void __ath12k_mac_scan_finish(struct ath12k *ar)
5209 {
5210 	struct ieee80211_hw *hw = ath12k_ar_to_hw(ar);
5211 
5212 	lockdep_assert_held(&ar->data_lock);
5213 
5214 	switch (ar->scan.state) {
5215 	case ATH12K_SCAN_IDLE:
5216 		break;
5217 	case ATH12K_SCAN_RUNNING:
5218 	case ATH12K_SCAN_ABORTING:
5219 		if (ar->scan.is_roc && ar->scan.roc_notify)
5220 			ieee80211_remain_on_channel_expired(hw);
5221 		fallthrough;
5222 	case ATH12K_SCAN_STARTING:
5223 		cancel_delayed_work(&ar->scan.timeout);
5224 		complete_all(&ar->scan.completed);
5225 		wiphy_work_queue(ar->ah->hw->wiphy, &ar->scan.vdev_clean_wk);
5226 		break;
5227 	}
5228 }
5229 
5230 void ath12k_mac_scan_finish(struct ath12k *ar)
5231 {
5232 	spin_lock_bh(&ar->data_lock);
5233 	__ath12k_mac_scan_finish(ar);
5234 	spin_unlock_bh(&ar->data_lock);
5235 }
5236 
5237 static int ath12k_scan_stop(struct ath12k *ar)
5238 {
5239 	struct ath12k_wmi_scan_cancel_arg arg = {
5240 		.req_type = WLAN_SCAN_CANCEL_SINGLE,
5241 		.scan_id = ATH12K_SCAN_ID,
5242 	};
5243 	int ret;
5244 
5245 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
5246 
5247 	/* TODO: Fill other STOP Params */
5248 	arg.pdev_id = ar->pdev->pdev_id;
5249 
5250 	ret = ath12k_wmi_send_scan_stop_cmd(ar, &arg);
5251 	if (ret) {
5252 		ath12k_warn(ar->ab, "failed to stop wmi scan: %d\n", ret);
5253 		goto out;
5254 	}
5255 
5256 	ret = wait_for_completion_timeout(&ar->scan.completed, 3 * HZ);
5257 	if (ret == 0) {
5258 		ath12k_warn(ar->ab,
5259 			    "failed to receive scan abort comple: timed out\n");
5260 		ret = -ETIMEDOUT;
5261 	} else if (ret > 0) {
5262 		ret = 0;
5263 	}
5264 
5265 out:
5266 	/* Scan state should be updated in scan completion worker but in
5267 	 * case firmware fails to deliver the event (for whatever reason)
5268 	 * it is desired to clean up scan state anyway. Firmware may have
5269 	 * just dropped the scan completion event delivery due to transport
5270 	 * pipe being overflown with data and/or it can recover on its own
5271 	 * before next scan request is submitted.
5272 	 */
5273 	spin_lock_bh(&ar->data_lock);
5274 	if (ret)
5275 		__ath12k_mac_scan_finish(ar);
5276 	spin_unlock_bh(&ar->data_lock);
5277 
5278 	return ret;
5279 }
5280 
5281 static void ath12k_scan_abort(struct ath12k *ar)
5282 {
5283 	int ret;
5284 
5285 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
5286 
5287 	spin_lock_bh(&ar->data_lock);
5288 
5289 	switch (ar->scan.state) {
5290 	case ATH12K_SCAN_IDLE:
5291 		/* This can happen if timeout worker kicked in and called
5292 		 * abortion while scan completion was being processed.
5293 		 */
5294 		break;
5295 	case ATH12K_SCAN_STARTING:
5296 	case ATH12K_SCAN_ABORTING:
5297 		ath12k_warn(ar->ab, "refusing scan abortion due to invalid scan state: %d\n",
5298 			    ar->scan.state);
5299 		break;
5300 	case ATH12K_SCAN_RUNNING:
5301 		ar->scan.state = ATH12K_SCAN_ABORTING;
5302 		spin_unlock_bh(&ar->data_lock);
5303 
5304 		ret = ath12k_scan_stop(ar);
5305 		if (ret)
5306 			ath12k_warn(ar->ab, "failed to abort scan: %d\n", ret);
5307 
5308 		spin_lock_bh(&ar->data_lock);
5309 		break;
5310 	}
5311 
5312 	spin_unlock_bh(&ar->data_lock);
5313 }
5314 
5315 static void ath12k_scan_timeout_work(struct work_struct *work)
5316 {
5317 	struct ath12k *ar = container_of(work, struct ath12k,
5318 					 scan.timeout.work);
5319 
5320 	wiphy_lock(ath12k_ar_to_hw(ar)->wiphy);
5321 	ath12k_scan_abort(ar);
5322 	wiphy_unlock(ath12k_ar_to_hw(ar)->wiphy);
5323 }
5324 
5325 static void ath12k_mac_scan_send_complete(struct ath12k *ar,
5326 					  struct cfg80211_scan_info *info)
5327 {
5328 	struct ath12k_hw *ah = ar->ah;
5329 	struct ath12k *partner_ar;
5330 	int i;
5331 
5332 	lockdep_assert_wiphy(ah->hw->wiphy);
5333 
5334 	for_each_ar(ah, partner_ar, i)
5335 		if (partner_ar != ar &&
5336 		    partner_ar->scan.state == ATH12K_SCAN_RUNNING)
5337 			return;
5338 
5339 	ieee80211_scan_completed(ah->hw, info);
5340 }
5341 
5342 static void ath12k_scan_vdev_clean_work(struct wiphy *wiphy, struct wiphy_work *work)
5343 {
5344 	struct ath12k *ar = container_of(work, struct ath12k,
5345 					 scan.vdev_clean_wk);
5346 	struct ath12k_hw *ah = ar->ah;
5347 	struct ath12k_link_vif *arvif;
5348 
5349 	lockdep_assert_wiphy(wiphy);
5350 
5351 	arvif = ar->scan.arvif;
5352 
5353 	/* The scan vdev has already been deleted. This can occur when a
5354 	 * new scan request is made on the same vif with a different
5355 	 * frequency, causing the scan arvif to move from one radio to
5356 	 * another. Or, scan was abrupted and via remove interface, the
5357 	 * arvif is already deleted. Alternatively, if the scan vdev is not
5358 	 * being used as an actual vdev, then do not delete it.
5359 	 */
5360 	if (!arvif || arvif->is_started)
5361 		goto work_complete;
5362 
5363 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac clean scan vdev (link id %u)",
5364 		   arvif->link_id);
5365 
5366 	ath12k_mac_remove_link_interface(ah->hw, arvif);
5367 	ath12k_mac_unassign_link_vif(arvif);
5368 
5369 work_complete:
5370 	spin_lock_bh(&ar->data_lock);
5371 	ar->scan.arvif = NULL;
5372 	if (!ar->scan.is_roc) {
5373 		struct cfg80211_scan_info info = {
5374 			.aborted = ((ar->scan.state ==
5375 				    ATH12K_SCAN_ABORTING) ||
5376 				    (ar->scan.state ==
5377 				    ATH12K_SCAN_STARTING)),
5378 		};
5379 
5380 		ath12k_mac_scan_send_complete(ar, &info);
5381 	}
5382 
5383 	ar->scan.state = ATH12K_SCAN_IDLE;
5384 	ar->scan_channel = NULL;
5385 	ar->scan.roc_freq = 0;
5386 	spin_unlock_bh(&ar->data_lock);
5387 }
5388 
5389 static int ath12k_start_scan(struct ath12k *ar,
5390 			     struct ath12k_wmi_scan_req_arg *arg)
5391 {
5392 	int ret;
5393 
5394 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
5395 
5396 	ret = ath12k_wmi_send_scan_start_cmd(ar, arg);
5397 	if (ret)
5398 		return ret;
5399 
5400 	ret = wait_for_completion_timeout(&ar->scan.started, 1 * HZ);
5401 	if (ret == 0) {
5402 		ret = ath12k_scan_stop(ar);
5403 		if (ret)
5404 			ath12k_warn(ar->ab, "failed to stop scan: %d\n", ret);
5405 
5406 		return -ETIMEDOUT;
5407 	}
5408 
5409 	/* If we failed to start the scan, return error code at
5410 	 * this point.  This is probably due to some issue in the
5411 	 * firmware, but no need to wedge the driver due to that...
5412 	 */
5413 	spin_lock_bh(&ar->data_lock);
5414 	if (ar->scan.state == ATH12K_SCAN_IDLE) {
5415 		spin_unlock_bh(&ar->data_lock);
5416 		return -EINVAL;
5417 	}
5418 	spin_unlock_bh(&ar->data_lock);
5419 
5420 	return 0;
5421 }
5422 
5423 int ath12k_mac_get_fw_stats(struct ath12k *ar,
5424 			    struct ath12k_fw_stats_req_params *param)
5425 {
5426 	struct ath12k_base *ab = ar->ab;
5427 	struct ath12k_hw *ah = ath12k_ar_to_ah(ar);
5428 	unsigned long time_left;
5429 	int ret;
5430 
5431 	guard(mutex)(&ah->hw_mutex);
5432 
5433 	if (ah->state != ATH12K_HW_STATE_ON)
5434 		return -ENETDOWN;
5435 
5436 	reinit_completion(&ar->fw_stats_complete);
5437 	reinit_completion(&ar->fw_stats_done);
5438 
5439 	ret = ath12k_wmi_send_stats_request_cmd(ar, param->stats_id,
5440 						param->vdev_id, param->pdev_id);
5441 	if (ret) {
5442 		ath12k_warn(ab, "failed to request fw stats: %d\n", ret);
5443 		return ret;
5444 	}
5445 
5446 	ath12k_dbg(ab, ATH12K_DBG_WMI,
5447 		   "get fw stat pdev id %d vdev id %d stats id 0x%x\n",
5448 		   param->pdev_id, param->vdev_id, param->stats_id);
5449 
5450 	time_left = wait_for_completion_timeout(&ar->fw_stats_complete, 1 * HZ);
5451 	if (!time_left) {
5452 		ath12k_warn(ab, "time out while waiting for get fw stats\n");
5453 		return -ETIMEDOUT;
5454 	}
5455 
5456 	/* Firmware sends WMI_UPDATE_STATS_EVENTID back-to-back
5457 	 * when stats data buffer limit is reached. fw_stats_complete
5458 	 * is completed once host receives first event from firmware, but
5459 	 * still there could be more events following. Below is to wait
5460 	 * until firmware completes sending all the events.
5461 	 */
5462 	time_left = wait_for_completion_timeout(&ar->fw_stats_done, 3 * HZ);
5463 	if (!time_left) {
5464 		ath12k_warn(ab, "time out while waiting for fw stats done\n");
5465 		return -ETIMEDOUT;
5466 	}
5467 
5468 	return 0;
5469 }
5470 
5471 int ath12k_mac_op_get_txpower(struct ieee80211_hw *hw,
5472 			      struct ieee80211_vif *vif,
5473 			      unsigned int link_id,
5474 			      int *dbm)
5475 {
5476 	struct ath12k_vif *ahvif = ath12k_vif_to_ahvif(vif);
5477 	struct ath12k_fw_stats_req_params params = {};
5478 	struct ath12k_fw_stats_pdev *pdev;
5479 	struct ath12k_hw *ah = hw->priv;
5480 	struct ath12k_link_vif *arvif;
5481 	struct ath12k_base *ab;
5482 	struct ath12k *ar;
5483 	int ret;
5484 
5485 	/* Final Tx power is minimum of Target Power, CTL power, Regulatory
5486 	 * Power, PSD EIRP Power. We just know the Regulatory power from the
5487 	 * regulatory rules obtained. FW knows all these power and sets the min
5488 	 * of these. Hence, we request the FW pdev stats in which FW reports
5489 	 * the minimum of all vdev's channel Tx power.
5490 	 */
5491 	lockdep_assert_wiphy(hw->wiphy);
5492 
5493 	arvif = wiphy_dereference(ah->hw->wiphy, ahvif->link[link_id]);
5494 	if (!arvif || !arvif->ar)
5495 		return -EINVAL;
5496 
5497 	ar = arvif->ar;
5498 	ab = ar->ab;
5499 	if (ah->state != ATH12K_HW_STATE_ON)
5500 		goto err_fallback;
5501 
5502 	if (test_bit(ATH12K_FLAG_CAC_RUNNING, &ar->dev_flags))
5503 		return -EAGAIN;
5504 
5505 	/* Limit the requests to Firmware for fetching the tx power */
5506 	if (ar->chan_tx_pwr != ATH12K_PDEV_TX_POWER_INVALID &&
5507 	    time_before(jiffies,
5508 			msecs_to_jiffies(ATH12K_PDEV_TX_POWER_REFRESH_TIME_MSECS) +
5509 					 ar->last_tx_power_update))
5510 		goto send_tx_power;
5511 
5512 	params.pdev_id = ath12k_mac_get_target_pdev_id(ar);
5513 	params.vdev_id = arvif->vdev_id;
5514 	params.stats_id = WMI_REQUEST_PDEV_STAT;
5515 	ret = ath12k_mac_get_fw_stats(ar, &params);
5516 	if (ret) {
5517 		ath12k_warn(ab, "failed to request fw pdev stats: %d\n", ret);
5518 		goto err_fallback;
5519 	}
5520 
5521 	spin_lock_bh(&ar->data_lock);
5522 	pdev = list_first_entry_or_null(&ar->fw_stats.pdevs,
5523 					struct ath12k_fw_stats_pdev, list);
5524 	if (!pdev) {
5525 		spin_unlock_bh(&ar->data_lock);
5526 		goto err_fallback;
5527 	}
5528 
5529 	/* tx power reported by firmware is in units of 0.5 dBm */
5530 	ar->chan_tx_pwr = pdev->chan_tx_power / 2;
5531 	spin_unlock_bh(&ar->data_lock);
5532 	ar->last_tx_power_update = jiffies;
5533 	ath12k_fw_stats_reset(ar);
5534 
5535 send_tx_power:
5536 	*dbm = ar->chan_tx_pwr;
5537 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "txpower fetched from firmware %d dBm\n",
5538 		   *dbm);
5539 	return 0;
5540 
5541 err_fallback:
5542 	/* We didn't get txpower from FW. Hence, relying on vif->bss_conf.txpower */
5543 	*dbm = vif->bss_conf.txpower;
5544 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "txpower from firmware NaN, reported %d dBm\n",
5545 		   *dbm);
5546 	return 0;
5547 }
5548 EXPORT_SYMBOL(ath12k_mac_op_get_txpower);
5549 
5550 static u8
5551 ath12k_mac_find_link_id_by_ar(struct ath12k_vif *ahvif, struct ath12k *ar)
5552 {
5553 	struct ath12k_link_vif *arvif;
5554 	struct ath12k_hw *ah = ahvif->ah;
5555 	unsigned long links = ahvif->links_map;
5556 	unsigned long scan_links_map;
5557 	u8 link_id;
5558 
5559 	lockdep_assert_wiphy(ah->hw->wiphy);
5560 
5561 	for_each_set_bit(link_id, &links, ATH12K_NUM_MAX_LINKS) {
5562 		arvif = wiphy_dereference(ah->hw->wiphy, ahvif->link[link_id]);
5563 
5564 		if (!arvif || !arvif->is_created)
5565 			continue;
5566 
5567 		if (ar == arvif->ar)
5568 			return link_id;
5569 	}
5570 
5571 	/* input ar is not assigned to any of the links of ML VIF, use next
5572 	 * available scan link for scan vdev creation. There are cases where
5573 	 * single scan req needs to be split in driver and initiate separate
5574 	 * scan requests to firmware based on device.
5575 	 */
5576 
5577 	 /* Unset all non-scan links (0-14) of scan_links_map so that ffs() will
5578 	  * choose an available link among scan links (i.e link id >= 15)
5579 	  */
5580 	scan_links_map = ~ahvif->links_map & ATH12K_SCAN_LINKS_MASK;
5581 	if (scan_links_map)
5582 		return __ffs(scan_links_map);
5583 
5584 	return ATH12K_FIRST_SCAN_LINK;
5585 }
5586 
5587 static int ath12k_mac_initiate_hw_scan(struct ieee80211_hw *hw,
5588 				       struct ieee80211_vif *vif,
5589 				       struct ieee80211_scan_request *hw_req,
5590 				       int n_channels,
5591 				       struct ieee80211_channel **chan_list,
5592 				       struct ath12k *ar)
5593 {
5594 	struct ath12k_hw *ah = ath12k_hw_to_ah(hw);
5595 	struct ath12k_vif *ahvif = ath12k_vif_to_ahvif(vif);
5596 	struct ath12k_link_vif *arvif;
5597 	struct cfg80211_scan_request *req = &hw_req->req;
5598 	struct ath12k_wmi_scan_req_arg *arg = NULL;
5599 	u8 link_id;
5600 	int ret;
5601 	int i;
5602 	bool create = true;
5603 
5604 	lockdep_assert_wiphy(hw->wiphy);
5605 
5606 	arvif = &ahvif->deflink;
5607 
5608 	/* check if any of the links of ML VIF is already started on
5609 	 * radio(ar) corresponding to given scan frequency and use it,
5610 	 * if not use scan link (link id >= 15) for scan purpose.
5611 	 */
5612 	link_id = ath12k_mac_find_link_id_by_ar(ahvif, ar);
5613 	/* All scan links are occupied. ideally this shouldn't happen as
5614 	 * mac80211 won't schedule scan for same band until ongoing scan is
5615 	 * completed, don't try to exceed max links just in case if it happens.
5616 	 */
5617 	if (link_id >= ATH12K_NUM_MAX_LINKS)
5618 		return -EBUSY;
5619 
5620 	arvif = ath12k_mac_assign_link_vif(ah, vif, link_id);
5621 
5622 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac link ID %d selected for scan",
5623 		   arvif->link_id);
5624 
5625 	/* If the vif is already assigned to a specific vdev of an ar,
5626 	 * check whether its already started, vdev which is started
5627 	 * are not allowed to switch to a new radio.
5628 	 * If the vdev is not started, but was earlier created on a
5629 	 * different ar, delete that vdev and create a new one. We don't
5630 	 * delete at the scan stop as an optimization to avoid redundant
5631 	 * delete-create vdev's for the same ar, in case the request is
5632 	 * always on the same band for the vif
5633 	 */
5634 	if (arvif->is_created) {
5635 		if (WARN_ON(!arvif->ar))
5636 			return -EINVAL;
5637 
5638 		if (ar != arvif->ar && arvif->is_started)
5639 			return -EINVAL;
5640 
5641 		if (ar != arvif->ar) {
5642 			ath12k_mac_remove_link_interface(hw, arvif);
5643 			ath12k_mac_unassign_link_vif(arvif);
5644 		} else {
5645 			create = false;
5646 		}
5647 	}
5648 
5649 	if (create) {
5650 		/* Previous arvif would've been cleared in radio switch block
5651 		 * above, assign arvif again for create.
5652 		 */
5653 		arvif = ath12k_mac_assign_link_vif(ah, vif, link_id);
5654 
5655 		ret = ath12k_mac_vdev_create(ar, arvif);
5656 		if (ret) {
5657 			ath12k_warn(ar->ab, "unable to create scan vdev %d\n", ret);
5658 			ath12k_mac_unassign_link_vif(arvif);
5659 			return ret;
5660 		}
5661 	}
5662 
5663 	spin_lock_bh(&ar->data_lock);
5664 	switch (ar->scan.state) {
5665 	case ATH12K_SCAN_IDLE:
5666 		reinit_completion(&ar->scan.started);
5667 		reinit_completion(&ar->scan.completed);
5668 		ar->scan.state = ATH12K_SCAN_STARTING;
5669 		ar->scan.is_roc = false;
5670 		ar->scan.arvif = arvif;
5671 		ret = 0;
5672 		break;
5673 	case ATH12K_SCAN_STARTING:
5674 	case ATH12K_SCAN_RUNNING:
5675 	case ATH12K_SCAN_ABORTING:
5676 		ret = -EBUSY;
5677 		break;
5678 	}
5679 	spin_unlock_bh(&ar->data_lock);
5680 
5681 	if (ret)
5682 		goto exit;
5683 
5684 	arg = kzalloc_flex(*arg, chan_list, n_channels);
5685 	if (!arg) {
5686 		ret = -ENOMEM;
5687 		goto exit;
5688 	}
5689 
5690 	arg->num_chan = n_channels;
5691 
5692 	ath12k_wmi_start_scan_init(ar, arg);
5693 	arg->vdev_id = arvif->vdev_id;
5694 	arg->scan_id = ATH12K_SCAN_ID;
5695 
5696 	if (req->ie_len) {
5697 		arg->extraie.ptr = kmemdup(req->ie, req->ie_len, GFP_KERNEL);
5698 		if (!arg->extraie.ptr) {
5699 			ret = -ENOMEM;
5700 			goto exit;
5701 		}
5702 		arg->extraie.len = req->ie_len;
5703 	}
5704 
5705 	if (req->n_ssids) {
5706 		arg->num_ssids = req->n_ssids;
5707 		for (i = 0; i < arg->num_ssids; i++)
5708 			arg->ssid[i] = req->ssids[i];
5709 	} else {
5710 		arg->scan_f_passive = 1;
5711 	}
5712 
5713 	for (i = 0; i < arg->num_chan; i++)
5714 		arg->chan_list[i] = chan_list[i]->center_freq;
5715 
5716 	ret = ath12k_start_scan(ar, arg);
5717 	if (ret) {
5718 		if (ret == -EBUSY)
5719 			ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
5720 				   "scan engine is busy 11d state %d\n", ar->state_11d);
5721 		else
5722 			ath12k_warn(ar->ab, "failed to start hw scan: %d\n", ret);
5723 
5724 		spin_lock_bh(&ar->data_lock);
5725 		ar->scan.state = ATH12K_SCAN_IDLE;
5726 		spin_unlock_bh(&ar->data_lock);
5727 		goto exit;
5728 	}
5729 
5730 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac scan started");
5731 
5732 	/* Add a margin to account for event/command processing */
5733 	ieee80211_queue_delayed_work(ath12k_ar_to_hw(ar), &ar->scan.timeout,
5734 				     msecs_to_jiffies(arg->max_scan_time +
5735 						      ATH12K_MAC_SCAN_TIMEOUT_MSECS));
5736 
5737 exit:
5738 	if (arg) {
5739 		kfree(arg->extraie.ptr);
5740 		kfree(arg);
5741 	}
5742 
5743 	if (ar->state_11d == ATH12K_11D_PREPARING &&
5744 	    ahvif->vdev_type == WMI_VDEV_TYPE_STA &&
5745 	    ahvif->vdev_subtype == WMI_VDEV_SUBTYPE_NONE)
5746 		ath12k_mac_11d_scan_start(ar, arvif->vdev_id);
5747 
5748 	return ret;
5749 }
5750 
5751 int ath12k_mac_op_hw_scan(struct ieee80211_hw *hw,
5752 			  struct ieee80211_vif *vif,
5753 			  struct ieee80211_scan_request *hw_req)
5754 {
5755 	struct ath12k_vif *ahvif = ath12k_vif_to_ahvif(vif);
5756 	struct ieee80211_channel **chan_list, *chan;
5757 	struct ath12k_hw *ah = ath12k_hw_to_ah(hw);
5758 	unsigned long links_map, link_id;
5759 	struct ath12k_link_vif *arvif;
5760 	struct ath12k *ar, *scan_ar;
5761 	int i, j, ret = 0;
5762 
5763 	lockdep_assert_wiphy(hw->wiphy);
5764 
5765 	chan_list = kzalloc_objs(*chan_list, hw_req->req.n_channels);
5766 	if (!chan_list)
5767 		return -ENOMEM;
5768 
5769 	/* There could be channels that belong to multiple underlying radio
5770 	 * in same scan request as mac80211 sees it as single band. In that
5771 	 * case split the hw_req based on frequency range and schedule scans to
5772 	 * corresponding radio.
5773 	 */
5774 	for_each_ar(ah, ar, i) {
5775 		int n_chans = 0;
5776 
5777 		for (j = 0; j < hw_req->req.n_channels; j++) {
5778 			chan = hw_req->req.channels[j];
5779 			scan_ar = ath12k_mac_select_scan_device(hw, vif,
5780 								chan->center_freq);
5781 			if (!scan_ar) {
5782 				ath12k_hw_warn(ah, "unable to select scan device for freq %d\n",
5783 					       chan->center_freq);
5784 				ret = -EINVAL;
5785 				goto abort;
5786 			}
5787 			if (ar != scan_ar)
5788 				continue;
5789 
5790 			chan_list[n_chans++] = chan;
5791 		}
5792 		if (n_chans) {
5793 			ret = ath12k_mac_initiate_hw_scan(hw, vif, hw_req, n_chans,
5794 							  chan_list, ar);
5795 			if (ret)
5796 				goto abort;
5797 		}
5798 	}
5799 abort:
5800 	/* If any of the parallel scans initiated fails, abort all and
5801 	 * remove the scan interfaces created. Return complete scan
5802 	 * failure as mac80211 assumes this as single scan request.
5803 	 */
5804 	if (ret) {
5805 		ath12k_hw_warn(ah, "Scan failed %d , cleanup all scan vdevs\n", ret);
5806 		links_map = ahvif->links_map;
5807 		for_each_set_bit(link_id, &links_map, ATH12K_NUM_MAX_LINKS) {
5808 			arvif = wiphy_dereference(hw->wiphy, ahvif->link[link_id]);
5809 			if (!arvif)
5810 				continue;
5811 
5812 			ar = arvif->ar;
5813 			if (ar->scan.arvif == arvif) {
5814 				wiphy_work_cancel(hw->wiphy, &ar->scan.vdev_clean_wk);
5815 				spin_lock_bh(&ar->data_lock);
5816 				ar->scan.arvif = NULL;
5817 				ar->scan.state = ATH12K_SCAN_IDLE;
5818 				ar->scan_channel = NULL;
5819 				ar->scan.roc_freq = 0;
5820 				spin_unlock_bh(&ar->data_lock);
5821 			}
5822 			if (link_id >= ATH12K_FIRST_SCAN_LINK) {
5823 				ath12k_mac_remove_link_interface(hw, arvif);
5824 				ath12k_mac_unassign_link_vif(arvif);
5825 			}
5826 		}
5827 	}
5828 	kfree(chan_list);
5829 	return ret;
5830 }
5831 EXPORT_SYMBOL(ath12k_mac_op_hw_scan);
5832 
5833 void ath12k_mac_op_cancel_hw_scan(struct ieee80211_hw *hw,
5834 				  struct ieee80211_vif *vif)
5835 {
5836 	struct ath12k_vif *ahvif = ath12k_vif_to_ahvif(vif);
5837 	unsigned long link_id, links_map = ahvif->links_map;
5838 	struct ath12k_link_vif *arvif;
5839 	struct ath12k *ar;
5840 
5841 	lockdep_assert_wiphy(hw->wiphy);
5842 
5843 	for_each_set_bit(link_id, &links_map, ATH12K_NUM_MAX_LINKS) {
5844 		arvif = wiphy_dereference(hw->wiphy, ahvif->link[link_id]);
5845 		if (!arvif || !arvif->is_created ||
5846 		    arvif->ar->scan.arvif != arvif)
5847 			continue;
5848 
5849 		ar = arvif->ar;
5850 
5851 		ath12k_scan_abort(ar);
5852 
5853 		cancel_delayed_work_sync(&ar->scan.timeout);
5854 	}
5855 }
5856 EXPORT_SYMBOL(ath12k_mac_op_cancel_hw_scan);
5857 
5858 static int ath12k_install_key(struct ath12k_link_vif *arvif,
5859 			      struct ieee80211_key_conf *key,
5860 			      enum set_key_cmd cmd,
5861 			      const u8 *macaddr, u32 flags)
5862 {
5863 	int ret;
5864 	struct ath12k *ar = arvif->ar;
5865 	struct wmi_vdev_install_key_arg arg = {
5866 		.vdev_id = arvif->vdev_id,
5867 		.key_idx = key->keyidx,
5868 		.key_len = key->keylen,
5869 		.key_data = key->key,
5870 		.key_flags = flags,
5871 		.ieee80211_key_cipher = key->cipher,
5872 		.macaddr = macaddr,
5873 	};
5874 	struct ath12k_vif *ahvif = arvif->ahvif;
5875 
5876 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
5877 
5878 	if (test_bit(ATH12K_FLAG_HW_CRYPTO_DISABLED, &ar->ab->dev_flags))
5879 		return 0;
5880 
5881 	if (cmd == DISABLE_KEY) {
5882 		/* TODO: Check if FW expects  value other than NONE for del */
5883 		/* arg.key_cipher = WMI_CIPHER_NONE; */
5884 		arg.key_len = 0;
5885 		arg.key_data = NULL;
5886 		goto check_order;
5887 	}
5888 
5889 	switch (key->cipher) {
5890 	case WLAN_CIPHER_SUITE_CCMP:
5891 	case WLAN_CIPHER_SUITE_CCMP_256:
5892 		arg.key_cipher = WMI_CIPHER_AES_CCM;
5893 		key->flags |= IEEE80211_KEY_FLAG_GENERATE_IV_MGMT;
5894 		break;
5895 	case WLAN_CIPHER_SUITE_TKIP:
5896 		arg.key_cipher = WMI_CIPHER_TKIP;
5897 		arg.key_txmic_len = 8;
5898 		arg.key_rxmic_len = 8;
5899 		break;
5900 	case WLAN_CIPHER_SUITE_GCMP:
5901 	case WLAN_CIPHER_SUITE_GCMP_256:
5902 		arg.key_cipher = WMI_CIPHER_AES_GCM;
5903 		key->flags |= IEEE80211_KEY_FLAG_GENERATE_IV_MGMT;
5904 		break;
5905 	case WLAN_CIPHER_SUITE_AES_CMAC:
5906 		arg.key_cipher = WMI_CIPHER_AES_CMAC;
5907 		break;
5908 	case WLAN_CIPHER_SUITE_BIP_GMAC_128:
5909 	case WLAN_CIPHER_SUITE_BIP_GMAC_256:
5910 		arg.key_cipher = WMI_CIPHER_AES_GMAC;
5911 		break;
5912 	case WLAN_CIPHER_SUITE_BIP_CMAC_256:
5913 		arg.key_cipher = WMI_CIPHER_AES_CMAC;
5914 		break;
5915 	default:
5916 		ath12k_warn(ar->ab, "cipher %d is not supported\n", key->cipher);
5917 		return -EOPNOTSUPP;
5918 	}
5919 
5920 	if (test_bit(ATH12K_FLAG_RAW_MODE, &ar->ab->dev_flags))
5921 		key->flags |= IEEE80211_KEY_FLAG_GENERATE_IV |
5922 			      IEEE80211_KEY_FLAG_RESERVE_TAILROOM;
5923 
5924 check_order:
5925 	if (ahvif->vdev_type == WMI_VDEV_TYPE_STA &&
5926 	    arg.key_flags == WMI_KEY_GROUP) {
5927 		if (cmd == SET_KEY) {
5928 			if (arvif->pairwise_key_done) {
5929 				ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
5930 					   "vdev %u pairwise key done, go install group key\n",
5931 					   arg.vdev_id);
5932 				goto install;
5933 			} else {
5934 				/* WCN7850 firmware requires pairwise key to be installed
5935 				 * before group key. In case group key comes first, cache
5936 				 * it and return. Will revisit it once pairwise key gets
5937 				 * installed.
5938 				 */
5939 				arvif->group_key = arg;
5940 				arvif->group_key_valid = true;
5941 				ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
5942 					   "vdev %u group key before pairwise key, cache and skip\n",
5943 					   arg.vdev_id);
5944 
5945 				ret = 0;
5946 				goto out;
5947 			}
5948 		} else {
5949 			arvif->group_key_valid = false;
5950 		}
5951 	}
5952 
5953 install:
5954 	reinit_completion(&ar->install_key_done);
5955 
5956 	ret = ath12k_wmi_vdev_install_key(arvif->ar, &arg);
5957 	if (ret)
5958 		return ret;
5959 
5960 	if (!wait_for_completion_timeout(&ar->install_key_done, 1 * HZ))
5961 		return -ETIMEDOUT;
5962 
5963 	if (ether_addr_equal(arg.macaddr, arvif->bssid))
5964 		ahvif->dp_vif.key_cipher = arg.ieee80211_key_cipher;
5965 
5966 	if (ar->install_key_status) {
5967 		ret = -EINVAL;
5968 		goto out;
5969 	}
5970 
5971 	if (ahvif->vdev_type == WMI_VDEV_TYPE_STA &&
5972 	    arg.key_flags == WMI_KEY_PAIRWISE) {
5973 		if (cmd == SET_KEY) {
5974 			arvif->pairwise_key_done = true;
5975 			if (arvif->group_key_valid) {
5976 				/* Install cached GTK */
5977 				arvif->group_key_valid = false;
5978 				arg = arvif->group_key;
5979 				ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
5980 					   "vdev %u pairwise key done, group key ready, go install\n",
5981 					   arg.vdev_id);
5982 				goto install;
5983 			}
5984 		} else {
5985 			arvif->pairwise_key_done = false;
5986 		}
5987 	}
5988 
5989 out:
5990 	if (ret) {
5991 		/* In case of failure userspace may not do DISABLE_KEY
5992 		 * but triggers re-connection directly, so manually reset
5993 		 * status here.
5994 		 */
5995 		arvif->group_key_valid = false;
5996 		arvif->pairwise_key_done = false;
5997 	}
5998 
5999 	return ret;
6000 }
6001 
6002 static int ath12k_clear_peer_keys(struct ath12k_link_vif *arvif,
6003 				  const u8 *addr)
6004 {
6005 	struct ath12k *ar = arvif->ar;
6006 	struct ath12k_base *ab = ar->ab;
6007 	struct ath12k_dp_link_peer *peer;
6008 	int first_errno = 0;
6009 	int ret;
6010 	int i, len;
6011 	u32 flags = 0;
6012 	struct ath12k_dp *dp = ath12k_ab_to_dp(ab);
6013 	struct ieee80211_key_conf *keys[WMI_MAX_KEY_INDEX + 1] = {};
6014 
6015 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
6016 
6017 	spin_lock_bh(&dp->dp_lock);
6018 	peer = ath12k_dp_link_peer_find_by_vdev_and_addr(dp, arvif->vdev_id, addr);
6019 	if (!peer || !peer->dp_peer) {
6020 		spin_unlock_bh(&dp->dp_lock);
6021 		return -ENOENT;
6022 	}
6023 
6024 	len = ARRAY_SIZE(peer->dp_peer->keys);
6025 	for (i = 0; i < len; i++) {
6026 		if (!peer->dp_peer->keys[i])
6027 			continue;
6028 
6029 		keys[i] = peer->dp_peer->keys[i];
6030 		peer->dp_peer->keys[i] = NULL;
6031 	}
6032 
6033 	spin_unlock_bh(&dp->dp_lock);
6034 
6035 	for (i = 0; i < len; i++) {
6036 		if (!keys[i])
6037 			continue;
6038 
6039 		/* key flags are not required to delete the key */
6040 		ret = ath12k_install_key(arvif, keys[i],
6041 					 DISABLE_KEY, addr, flags);
6042 		if (ret < 0 && first_errno == 0)
6043 			first_errno = ret;
6044 
6045 		if (ret < 0)
6046 			ath12k_warn(ab, "failed to remove peer key %d: %d\n",
6047 				    i, ret);
6048 	}
6049 
6050 	return first_errno;
6051 }
6052 
6053 static int ath12k_mac_set_key(struct ath12k *ar, enum set_key_cmd cmd,
6054 			      struct ath12k_link_vif *arvif,
6055 			      struct ath12k_link_sta *arsta,
6056 			      struct ieee80211_key_conf *key)
6057 {
6058 	struct ieee80211_sta *sta = NULL;
6059 	struct ath12k_base *ab = ar->ab;
6060 	struct ath12k_dp_link_peer *peer;
6061 	struct ath12k_sta *ahsta;
6062 	const u8 *peer_addr;
6063 	int ret;
6064 	u32 flags = 0;
6065 	struct ath12k_dp *dp = ath12k_ab_to_dp(ab);
6066 
6067 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
6068 
6069 	if (arsta)
6070 		sta = ath12k_ahsta_to_sta(arsta->ahsta);
6071 
6072 	if (test_bit(ATH12K_FLAG_HW_CRYPTO_DISABLED, &ab->dev_flags))
6073 		return 1;
6074 
6075 	if (sta)
6076 		peer_addr = arsta->addr;
6077 	else
6078 		peer_addr = arvif->bssid;
6079 
6080 	key->hw_key_idx = key->keyidx;
6081 
6082 	/* the peer should not disappear in mid-way (unless FW goes awry) since
6083 	 * we already hold wiphy lock. we just make sure its there now.
6084 	 */
6085 	spin_lock_bh(&dp->dp_lock);
6086 	peer = ath12k_dp_link_peer_find_by_vdev_and_addr(dp, arvif->vdev_id,
6087 							 peer_addr);
6088 	if (!peer || !peer->dp_peer) {
6089 		spin_unlock_bh(&dp->dp_lock);
6090 
6091 		if (cmd == SET_KEY) {
6092 			ath12k_warn(ab, "cannot install key for non-existent peer %pM\n",
6093 				    peer_addr);
6094 			return -EOPNOTSUPP;
6095 		}
6096 
6097 		/* if the peer doesn't exist there is no key to disable
6098 		 * anymore
6099 		 */
6100 		return 0;
6101 	}
6102 
6103 	spin_unlock_bh(&dp->dp_lock);
6104 
6105 	if (key->flags & IEEE80211_KEY_FLAG_PAIRWISE)
6106 		flags = WMI_KEY_PAIRWISE;
6107 	else
6108 		flags = WMI_KEY_GROUP;
6109 
6110 	ret = ath12k_install_key(arvif, key, cmd, peer_addr, flags);
6111 	if (ret) {
6112 		ath12k_warn(ab, "ath12k_install_key failed (%d)\n", ret);
6113 		return ret;
6114 	}
6115 
6116 	ret = ath12k_dp_rx_peer_pn_replay_config(arvif, peer_addr, cmd, key);
6117 	if (ret) {
6118 		ath12k_warn(ab, "failed to offload PN replay detection %d\n", ret);
6119 		return ret;
6120 	}
6121 
6122 	spin_lock_bh(&dp->dp_lock);
6123 	peer = ath12k_dp_link_peer_find_by_vdev_and_addr(dp, arvif->vdev_id,
6124 							 peer_addr);
6125 	if (peer && peer->dp_peer && cmd == SET_KEY) {
6126 		peer->dp_peer->keys[key->keyidx] = key;
6127 		if (key->flags & IEEE80211_KEY_FLAG_PAIRWISE) {
6128 			peer->dp_peer->ucast_keyidx = key->keyidx;
6129 			peer->dp_peer->sec_type =
6130 					ath12k_dp_tx_get_encrypt_type(key->cipher);
6131 		} else {
6132 			peer->dp_peer->mcast_keyidx = key->keyidx;
6133 			peer->dp_peer->sec_type_grp =
6134 					ath12k_dp_tx_get_encrypt_type(key->cipher);
6135 		}
6136 	} else if (peer && peer->dp_peer && cmd == DISABLE_KEY) {
6137 		peer->dp_peer->keys[key->keyidx] = NULL;
6138 		if (key->flags & IEEE80211_KEY_FLAG_PAIRWISE)
6139 			peer->dp_peer->ucast_keyidx = 0;
6140 		else
6141 			peer->dp_peer->mcast_keyidx = 0;
6142 	} else if (!peer)
6143 		/* impossible unless FW goes crazy */
6144 		ath12k_warn(ab, "peer %pM disappeared!\n", peer_addr);
6145 
6146 	if (sta) {
6147 		ahsta = ath12k_sta_to_ahsta(sta);
6148 
6149 		switch (key->cipher) {
6150 		case WLAN_CIPHER_SUITE_TKIP:
6151 		case WLAN_CIPHER_SUITE_CCMP:
6152 		case WLAN_CIPHER_SUITE_CCMP_256:
6153 		case WLAN_CIPHER_SUITE_GCMP:
6154 		case WLAN_CIPHER_SUITE_GCMP_256:
6155 			if (cmd == SET_KEY)
6156 				ahsta->pn_type = HAL_PN_TYPE_WPA;
6157 			else
6158 				ahsta->pn_type = HAL_PN_TYPE_NONE;
6159 			break;
6160 		default:
6161 			ahsta->pn_type = HAL_PN_TYPE_NONE;
6162 			break;
6163 		}
6164 	}
6165 
6166 	spin_unlock_bh(&dp->dp_lock);
6167 
6168 	return 0;
6169 }
6170 
6171 static int ath12k_mac_update_key_cache(struct ath12k_vif_cache *cache,
6172 				       enum set_key_cmd cmd,
6173 				       struct ieee80211_sta *sta,
6174 				       struct ieee80211_key_conf *key)
6175 {
6176 	struct ath12k_key_conf *key_conf, *tmp;
6177 
6178 	list_for_each_entry_safe(key_conf, tmp, &cache->key_conf.list, list) {
6179 		if (key_conf->key != key)
6180 			continue;
6181 
6182 		/* If SET key entry is already present in cache, nothing to do,
6183 		 * just return
6184 		 */
6185 		if (cmd == SET_KEY)
6186 			return 0;
6187 
6188 		/* DEL key for an old SET key which driver hasn't flushed yet.
6189 		 */
6190 		list_del(&key_conf->list);
6191 		kfree(key_conf);
6192 	}
6193 
6194 	if (cmd == SET_KEY) {
6195 		key_conf = kzalloc_obj(*key_conf);
6196 
6197 		if (!key_conf)
6198 			return -ENOMEM;
6199 
6200 		key_conf->cmd = cmd;
6201 		key_conf->sta = sta;
6202 		key_conf->key = key;
6203 		list_add_tail(&key_conf->list,
6204 			      &cache->key_conf.list);
6205 	}
6206 
6207 	return 0;
6208 }
6209 
6210 int ath12k_mac_op_set_key(struct ieee80211_hw *hw, enum set_key_cmd cmd,
6211 			  struct ieee80211_vif *vif, struct ieee80211_sta *sta,
6212 			  struct ieee80211_key_conf *key)
6213 {
6214 	struct ath12k_vif *ahvif = ath12k_vif_to_ahvif(vif);
6215 	struct ath12k_link_vif *arvif;
6216 	struct ath12k_link_sta *arsta = NULL;
6217 	struct ath12k_vif_cache *cache;
6218 	struct ath12k_sta *ahsta;
6219 	unsigned long links;
6220 	u8 link_id;
6221 	int ret;
6222 
6223 	lockdep_assert_wiphy(hw->wiphy);
6224 
6225 	/* IGTK needs to be done in host software */
6226 	if (key->keyidx == 4 || key->keyidx == 5)
6227 		return 1;
6228 
6229 	if (key->keyidx > WMI_MAX_KEY_INDEX)
6230 		return -ENOSPC;
6231 
6232 	if (sta) {
6233 		ahsta = ath12k_sta_to_ahsta(sta);
6234 
6235 		/* For an ML STA Pairwise key is same for all associated link Stations,
6236 		 * hence do set key for all link STAs which are active.
6237 		 */
6238 		if (sta->mlo) {
6239 			links = ahsta->links_map;
6240 			for_each_set_bit(link_id, &links, IEEE80211_MLD_MAX_NUM_LINKS) {
6241 				arvif = wiphy_dereference(hw->wiphy,
6242 							  ahvif->link[link_id]);
6243 				arsta = wiphy_dereference(hw->wiphy,
6244 							  ahsta->link[link_id]);
6245 
6246 				if (WARN_ON(!arvif || !arsta))
6247 					/* arvif and arsta are expected to be valid when
6248 					 * STA is present.
6249 					 */
6250 					continue;
6251 
6252 				ret = ath12k_mac_set_key(arvif->ar, cmd, arvif,
6253 							 arsta, key);
6254 				if (ret)
6255 					break;
6256 			}
6257 
6258 			return 0;
6259 		}
6260 
6261 		arsta = &ahsta->deflink;
6262 		arvif = arsta->arvif;
6263 		if (WARN_ON(!arvif))
6264 			return -EINVAL;
6265 
6266 		ret = ath12k_mac_set_key(arvif->ar, cmd, arvif, arsta, key);
6267 		if (ret)
6268 			return ret;
6269 
6270 		return 0;
6271 	}
6272 
6273 	if (key->link_id >= 0 && key->link_id < IEEE80211_MLD_MAX_NUM_LINKS) {
6274 		link_id = key->link_id;
6275 		arvif = wiphy_dereference(hw->wiphy, ahvif->link[link_id]);
6276 	} else {
6277 		link_id = 0;
6278 		arvif = &ahvif->deflink;
6279 	}
6280 
6281 	if (!arvif || !arvif->is_created) {
6282 		cache = ath12k_ahvif_get_link_cache(ahvif, link_id);
6283 		if (!cache)
6284 			return -ENOSPC;
6285 
6286 		ret = ath12k_mac_update_key_cache(cache, cmd, sta, key);
6287 		if (ret)
6288 			return ret;
6289 
6290 		return 0;
6291 	}
6292 
6293 	ret = ath12k_mac_set_key(arvif->ar, cmd, arvif, NULL, key);
6294 	if (ret)
6295 		return ret;
6296 
6297 	return 0;
6298 }
6299 EXPORT_SYMBOL(ath12k_mac_op_set_key);
6300 
6301 static int
6302 ath12k_mac_bitrate_mask_num_vht_rates(struct ath12k *ar,
6303 				      enum nl80211_band band,
6304 				      const struct cfg80211_bitrate_mask *mask)
6305 {
6306 	int num_rates = 0;
6307 	int i;
6308 
6309 	for (i = 0; i < ARRAY_SIZE(mask->control[band].vht_mcs); i++)
6310 		num_rates += hweight16(mask->control[band].vht_mcs[i]);
6311 
6312 	return num_rates;
6313 }
6314 
6315 static int
6316 ath12k_mac_bitrate_mask_num_he_rates(struct ath12k *ar,
6317 				     enum nl80211_band band,
6318 				     const struct cfg80211_bitrate_mask *mask)
6319 {
6320 	int num_rates = 0;
6321 	int i;
6322 
6323 	for (i = 0; i < ARRAY_SIZE(mask->control[band].he_mcs); i++)
6324 		num_rates += hweight16(mask->control[band].he_mcs[i]);
6325 
6326 	return num_rates;
6327 }
6328 
6329 static int
6330 ath12k_mac_bitrate_mask_num_eht_rates(struct ath12k *ar,
6331 				      enum nl80211_band band,
6332 				      const struct cfg80211_bitrate_mask *mask)
6333 {
6334 	int num_rates = 0;
6335 	int i;
6336 
6337 	for (i = 0; i < ARRAY_SIZE(mask->control[band].eht_mcs); i++)
6338 		num_rates += hweight16(mask->control[band].eht_mcs[i]);
6339 
6340 	return num_rates;
6341 }
6342 
6343 static int
6344 ath12k_mac_set_peer_vht_fixed_rate(struct ath12k_link_vif *arvif,
6345 				   struct ath12k_link_sta *arsta,
6346 				   const struct cfg80211_bitrate_mask *mask,
6347 				   enum nl80211_band band)
6348 {
6349 	struct ath12k *ar = arvif->ar;
6350 	u8 vht_rate, nss;
6351 	u32 rate_code;
6352 	int ret, i;
6353 
6354 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
6355 
6356 	nss = 0;
6357 
6358 	for (i = 0; i < ARRAY_SIZE(mask->control[band].vht_mcs); i++) {
6359 		if (hweight16(mask->control[band].vht_mcs[i]) == 1) {
6360 			nss = i + 1;
6361 			vht_rate = ffs(mask->control[band].vht_mcs[i]) - 1;
6362 		}
6363 	}
6364 
6365 	if (!nss) {
6366 		ath12k_warn(ar->ab, "No single VHT Fixed rate found to set for %pM",
6367 			    arsta->addr);
6368 		return -EINVAL;
6369 	}
6370 
6371 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
6372 		   "Setting Fixed VHT Rate for peer %pM. Device will not switch to any other selected rates",
6373 		   arsta->addr);
6374 
6375 	rate_code = ATH12K_HW_RATE_CODE(vht_rate, nss - 1,
6376 					WMI_RATE_PREAMBLE_VHT);
6377 	ret = ath12k_wmi_set_peer_param(ar, arsta->addr,
6378 					arvif->vdev_id,
6379 					WMI_PEER_PARAM_FIXED_RATE,
6380 					rate_code);
6381 	if (ret)
6382 		ath12k_warn(ar->ab,
6383 			    "failed to update STA %pM Fixed Rate %d: %d\n",
6384 			     arsta->addr, rate_code, ret);
6385 
6386 	return ret;
6387 }
6388 
6389 static int
6390 ath12k_mac_set_peer_he_fixed_rate(struct ath12k_link_vif *arvif,
6391 				  struct ath12k_link_sta *arsta,
6392 				  const struct cfg80211_bitrate_mask *mask,
6393 				  enum nl80211_band band)
6394 {
6395 	struct ath12k *ar = arvif->ar;
6396 	u8 he_rate, nss;
6397 	u32 rate_code;
6398 	int ret, i;
6399 	struct ath12k_sta *ahsta = arsta->ahsta;
6400 	struct ieee80211_sta *sta;
6401 
6402 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
6403 
6404 	sta = ath12k_ahsta_to_sta(ahsta);
6405 	nss = 0;
6406 
6407 	for (i = 0; i < ARRAY_SIZE(mask->control[band].he_mcs); i++) {
6408 		if (hweight16(mask->control[band].he_mcs[i]) == 1) {
6409 			nss = i + 1;
6410 			he_rate = ffs(mask->control[band].he_mcs[i]) - 1;
6411 		}
6412 	}
6413 
6414 	if (!nss) {
6415 		ath12k_warn(ar->ab, "No single HE Fixed rate found to set for %pM",
6416 			    arsta->addr);
6417 		return -EINVAL;
6418 	}
6419 
6420 	/* Avoid updating invalid nss as fixed rate*/
6421 	if (nss > sta->deflink.rx_nss)
6422 		return -EINVAL;
6423 
6424 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
6425 		   "Setting Fixed HE Rate for peer %pM. Device will not switch to any other selected rates",
6426 		   arsta->addr);
6427 
6428 	rate_code = ATH12K_HW_RATE_CODE(he_rate, nss - 1,
6429 					WMI_RATE_PREAMBLE_HE);
6430 
6431 	ret = ath12k_wmi_set_peer_param(ar, arsta->addr,
6432 					arvif->vdev_id,
6433 					WMI_PEER_PARAM_FIXED_RATE,
6434 					rate_code);
6435 	if (ret)
6436 		ath12k_warn(ar->ab,
6437 			    "failed to update STA %pM Fixed Rate %d: %d\n",
6438 			    arsta->addr, rate_code, ret);
6439 
6440 	return ret;
6441 }
6442 
6443 static int
6444 ath12k_mac_set_peer_eht_fixed_rate(struct ath12k_link_vif *arvif,
6445 				   struct ath12k_link_sta *arsta,
6446 				   const struct cfg80211_bitrate_mask *mask,
6447 				   enum nl80211_band band)
6448 {
6449 	struct ath12k_sta *ahsta = arsta->ahsta;
6450 	struct ath12k *ar = arvif->ar;
6451 	struct ieee80211_sta *sta;
6452 	struct ieee80211_link_sta *link_sta;
6453 	u8 eht_rate, nss = 0;
6454 	u32 rate_code;
6455 	int ret, i;
6456 
6457 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
6458 
6459 	sta = ath12k_ahsta_to_sta(ahsta);
6460 
6461 	for (i = 0; i < ARRAY_SIZE(mask->control[band].eht_mcs); i++) {
6462 		if (hweight16(mask->control[band].eht_mcs[i]) == 1) {
6463 			nss = i + 1;
6464 			eht_rate = ffs(mask->control[band].eht_mcs[i]) - 1;
6465 		}
6466 	}
6467 
6468 	if (!nss) {
6469 		ath12k_warn(ar->ab, "No single EHT Fixed rate found to set for %pM\n",
6470 			    arsta->addr);
6471 		return -EINVAL;
6472 	}
6473 
6474 	/* Avoid updating invalid nss as fixed rate*/
6475 	link_sta = ath12k_mac_get_link_sta(arsta);
6476 	if (!link_sta || nss > link_sta->rx_nss) {
6477 		ath12k_warn(ar->ab,
6478 			    "unable to access link sta for sta %pM link %u or fixed nss of %u is not supported by sta\n",
6479 			    sta->addr, arsta->link_id, nss);
6480 		return -EINVAL;
6481 	}
6482 
6483 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
6484 		   "Setting Fixed EHT Rate for peer %pM. Device will not switch to any other selected rates\n",
6485 		   arsta->addr);
6486 
6487 	rate_code = ATH12K_HW_RATE_CODE(eht_rate, nss - 1,
6488 					WMI_RATE_PREAMBLE_EHT);
6489 
6490 	ret = ath12k_wmi_set_peer_param(ar, arsta->addr,
6491 					arvif->vdev_id,
6492 					WMI_PEER_PARAM_FIXED_RATE,
6493 					rate_code);
6494 	if (ret)
6495 		ath12k_warn(ar->ab,
6496 			    "failed to update STA %pM Fixed Rate %d: %d\n",
6497 			    arsta->addr, rate_code, ret);
6498 
6499 	return ret;
6500 }
6501 
6502 static int ath12k_mac_station_assoc(struct ath12k *ar,
6503 				    struct ath12k_link_vif *arvif,
6504 				    struct ath12k_link_sta *arsta,
6505 				    bool reassoc)
6506 {
6507 	struct ieee80211_vif *vif = ath12k_ahvif_to_vif(arvif->ahvif);
6508 	struct ieee80211_sta *sta = ath12k_ahsta_to_sta(arsta->ahsta);
6509 	struct ieee80211_link_sta *link_sta;
6510 	int ret;
6511 	struct cfg80211_chan_def def;
6512 	enum nl80211_band band;
6513 	struct cfg80211_bitrate_mask *mask;
6514 	u8 num_vht_rates, num_he_rates, num_eht_rates;
6515 	u8 link_id = arvif->link_id;
6516 
6517 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
6518 
6519 	if (WARN_ON(ath12k_mac_vif_link_chan(vif, arvif->link_id, &def)))
6520 		return -EPERM;
6521 
6522 	if (WARN_ON(!rcu_access_pointer(sta->link[link_id])))
6523 		return -EINVAL;
6524 
6525 	band = def.chan->band;
6526 	mask = &arvif->bitrate_mask;
6527 
6528 	struct ath12k_wmi_peer_assoc_arg *peer_arg __free(kfree) =
6529 		kzalloc_obj(*peer_arg);
6530 	if (!peer_arg)
6531 		return -ENOMEM;
6532 
6533 	ath12k_peer_assoc_prepare(ar, arvif, arsta, peer_arg, reassoc);
6534 
6535 	if (peer_arg->peer_nss < 1) {
6536 		ath12k_warn(ar->ab,
6537 			    "invalid peer NSS %d\n", peer_arg->peer_nss);
6538 		return -EINVAL;
6539 	}
6540 
6541 	peer_arg->is_assoc = true;
6542 
6543 	ret = ath12k_mac_peer_assoc(ar, peer_arg);
6544 	if (ret)
6545 		return ret;
6546 
6547 	num_vht_rates = ath12k_mac_bitrate_mask_num_vht_rates(ar, band, mask);
6548 	num_he_rates = ath12k_mac_bitrate_mask_num_he_rates(ar, band, mask);
6549 	num_eht_rates = ath12k_mac_bitrate_mask_num_eht_rates(ar, band, mask);
6550 
6551 	/* If single VHT/HE/EHT rate is configured (by set_bitrate_mask()),
6552 	 * peer_assoc will disable VHT/HE/EHT. This is now enabled by a peer
6553 	 * specific fixed param.
6554 	 * Note that all other rates and NSS will be disabled for this peer.
6555 	 */
6556 	link_sta = ath12k_mac_get_link_sta(arsta);
6557 	if (!link_sta) {
6558 		ath12k_warn(ar->ab, "unable to access link sta in station assoc\n");
6559 		return -EINVAL;
6560 	}
6561 
6562 	spin_lock_bh(&ar->data_lock);
6563 	arsta->bw = ath12k_mac_ieee80211_sta_bw_to_wmi(ar, link_sta);
6564 	arsta->bw_prev = link_sta->bandwidth;
6565 	spin_unlock_bh(&ar->data_lock);
6566 
6567 	if (link_sta->vht_cap.vht_supported && num_vht_rates == 1) {
6568 		ret = ath12k_mac_set_peer_vht_fixed_rate(arvif, arsta, mask, band);
6569 	} else if (link_sta->he_cap.has_he && num_he_rates == 1) {
6570 		ret = ath12k_mac_set_peer_he_fixed_rate(arvif, arsta, mask, band);
6571 		if (ret)
6572 			return ret;
6573 	} else if (link_sta->eht_cap.has_eht && num_eht_rates == 1) {
6574 		ret = ath12k_mac_set_peer_eht_fixed_rate(arvif, arsta, mask, band);
6575 		if (ret)
6576 			return ret;
6577 	}
6578 
6579 	/* Re-assoc is run only to update supported rates for given station. It
6580 	 * doesn't make much sense to reconfigure the peer completely.
6581 	 */
6582 	if (reassoc)
6583 		return 0;
6584 
6585 	ret = ath12k_setup_peer_smps(ar, arvif, arsta->addr,
6586 				     &link_sta->ht_cap, &link_sta->he_6ghz_capa);
6587 	if (ret) {
6588 		ath12k_warn(ar->ab, "failed to setup peer SMPS for vdev %d: %d\n",
6589 			    arvif->vdev_id, ret);
6590 		return ret;
6591 	}
6592 
6593 	if (!sta->wme) {
6594 		arvif->num_legacy_stations++;
6595 		ret = ath12k_recalc_rtscts_prot(arvif);
6596 		if (ret)
6597 			return ret;
6598 	}
6599 
6600 	if (sta->wme && sta->uapsd_queues) {
6601 		ret = ath12k_peer_assoc_qos_ap(ar, arvif, arsta);
6602 		if (ret) {
6603 			ath12k_warn(ar->ab, "failed to set qos params for STA %pM for vdev %i: %d\n",
6604 				    arsta->addr, arvif->vdev_id, ret);
6605 			return ret;
6606 		}
6607 	}
6608 
6609 	return 0;
6610 }
6611 
6612 static int ath12k_mac_station_disassoc(struct ath12k *ar,
6613 				       struct ath12k_link_vif *arvif,
6614 				       struct ath12k_link_sta *arsta)
6615 {
6616 	struct ieee80211_sta *sta = ath12k_ahsta_to_sta(arsta->ahsta);
6617 
6618 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
6619 
6620 	if (!sta->wme) {
6621 		arvif->num_legacy_stations--;
6622 		return ath12k_recalc_rtscts_prot(arvif);
6623 	}
6624 
6625 	return 0;
6626 }
6627 
6628 static int ath12k_mac_sta_set_4addr(struct wiphy *wiphy, struct ath12k_sta *ahsta)
6629 {
6630 	struct ath12k_dp_link_peer *peer;
6631 	struct ath12k_link_vif *arvif;
6632 	struct ath12k_link_sta *arsta;
6633 	struct ath12k_vif *ahvif;
6634 	struct ath12k_dp *dp;
6635 	unsigned long links;
6636 	struct ath12k *ar;
6637 	u8 link_id;
6638 	int ret;
6639 
6640 	links = ahsta->links_map;
6641 	for_each_set_bit(link_id, &links, IEEE80211_MLD_MAX_NUM_LINKS) {
6642 		arsta = wiphy_dereference(wiphy, ahsta->link[link_id]);
6643 		if (!arsta)
6644 			continue;
6645 
6646 		arvif = arsta->arvif;
6647 		ahvif = arvif->ahvif;
6648 		ar = arvif->ar;
6649 
6650 		if (arvif->set_wds_vdev_param)
6651 			goto skip_nawds;
6652 
6653 		ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
6654 			   "setting USE_4ADDR for peer %pM\n", arsta->addr);
6655 
6656 		ret = ath12k_wmi_set_peer_param(ar, arsta->addr,
6657 						arvif->vdev_id,
6658 						WMI_PEER_USE_4ADDR,
6659 						WMI_PEER_4ADDR_ALLOW_EAPOL_DATA_FRAME);
6660 		if (ret) {
6661 			ath12k_warn(ar->ab, "failed to set peer %pM 4addr capability: %d\n",
6662 				    arsta->addr, ret);
6663 			return ret;
6664 		}
6665 
6666 		if (ahvif->dp_vif.tx_encap_type != ATH12K_HW_TXRX_ETHERNET)
6667 			goto skip_nawds;
6668 
6669 		ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
6670 						    WMI_VDEV_PARAM_AP_ENABLE_NAWDS,
6671 						    WDS_EXT_ENABLE);
6672 		if (ret) {
6673 			ath12k_warn(ar->ab, "failed to set vdev %d nawds parameter: %d\n",
6674 				    arvif->vdev_id, ret);
6675 			return ret;
6676 		}
6677 
6678 		arvif->nawds_enabled = true;
6679 
6680 skip_nawds:
6681 		dp = ath12k_ab_to_dp(ar->ab);
6682 		spin_lock_bh(&dp->dp_lock);
6683 		peer = ath12k_dp_link_peer_find_by_vdev_and_addr(dp, arvif->vdev_id,
6684 								 arsta->addr);
6685 		if (peer && peer->dp_peer) {
6686 			peer->dp_peer->ucast_ra_only = true;
6687 			peer->dp_peer->use_4addr = true;
6688 		} else {
6689 			spin_unlock_bh(&dp->dp_lock);
6690 			ath12k_warn(ar->ab, "failed to find DP peer for %pM\n",
6691 				    arsta->addr);
6692 			return -ENOENT;
6693 		}
6694 
6695 		spin_unlock_bh(&dp->dp_lock);
6696 	}
6697 
6698 	return 0;
6699 }
6700 
6701 static void ath12k_sta_rc_update_wk(struct wiphy *wiphy, struct wiphy_work *wk)
6702 {
6703 	struct ieee80211_link_sta *link_sta;
6704 	struct ath12k *ar;
6705 	struct ath12k_link_vif *arvif;
6706 	struct ieee80211_sta *sta;
6707 	struct cfg80211_chan_def def;
6708 	enum nl80211_band band;
6709 	const u8 *ht_mcs_mask;
6710 	const u16 *vht_mcs_mask;
6711 	const u16 *he_mcs_mask;
6712 	const u16 *eht_mcs_mask;
6713 	u32 changed, bw, nss, mac_nss, smps, bw_prev;
6714 	int err, num_vht_rates, num_he_rates, num_eht_rates;
6715 	const struct cfg80211_bitrate_mask *mask;
6716 	enum wmi_phy_mode peer_phymode;
6717 	struct ath12k_link_sta *arsta;
6718 	struct ieee80211_vif *vif;
6719 
6720 	lockdep_assert_wiphy(wiphy);
6721 
6722 	arsta = container_of(wk, struct ath12k_link_sta, update_wk);
6723 	sta = ath12k_ahsta_to_sta(arsta->ahsta);
6724 	arvif = arsta->arvif;
6725 	vif = ath12k_ahvif_to_vif(arvif->ahvif);
6726 	ar = arvif->ar;
6727 
6728 	if (WARN_ON(ath12k_mac_vif_link_chan(vif, arvif->link_id, &def)))
6729 		return;
6730 
6731 	band = def.chan->band;
6732 	ht_mcs_mask = arvif->bitrate_mask.control[band].ht_mcs;
6733 	vht_mcs_mask = arvif->bitrate_mask.control[band].vht_mcs;
6734 	he_mcs_mask = arvif->bitrate_mask.control[band].he_mcs;
6735 	eht_mcs_mask = arvif->bitrate_mask.control[band].eht_mcs;
6736 
6737 	spin_lock_bh(&ar->data_lock);
6738 
6739 	changed = arsta->changed;
6740 	arsta->changed = 0;
6741 
6742 	bw = arsta->bw;
6743 	bw_prev = arsta->bw_prev;
6744 	nss = arsta->nss;
6745 	smps = arsta->smps;
6746 
6747 	spin_unlock_bh(&ar->data_lock);
6748 
6749 	nss = max_t(u32, 1, nss);
6750 	mac_nss = max3(ath12k_mac_max_ht_nss(ht_mcs_mask),
6751 		       ath12k_mac_max_vht_nss(vht_mcs_mask),
6752 		       ath12k_mac_max_he_nss(he_mcs_mask));
6753 	mac_nss = max(mac_nss, ath12k_mac_max_eht_nss(eht_mcs_mask));
6754 	nss = min(nss, mac_nss);
6755 
6756 	struct ath12k_wmi_peer_assoc_arg *peer_arg __free(kfree) =
6757 					kzalloc_obj(*peer_arg);
6758 	if (!peer_arg)
6759 		return;
6760 
6761 	if (changed & IEEE80211_RC_BW_CHANGED) {
6762 		ath12k_peer_assoc_h_phymode(ar, arvif, arsta, peer_arg);
6763 		peer_phymode = peer_arg->peer_phymode;
6764 
6765 		if (bw > bw_prev) {
6766 			/* Phymode shows maximum supported channel width, if we
6767 			 * upgrade bandwidth then due to sanity check of firmware,
6768 			 * we have to send WMI_PEER_PHYMODE followed by
6769 			 * WMI_PEER_CHWIDTH
6770 			 */
6771 			ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac bandwidth upgrade for sta %pM new %d old %d\n",
6772 				   arsta->addr, bw, bw_prev);
6773 			err = ath12k_wmi_set_peer_param(ar, arsta->addr,
6774 							arvif->vdev_id, WMI_PEER_PHYMODE,
6775 							peer_phymode);
6776 			if (err) {
6777 				ath12k_warn(ar->ab, "failed to update STA %pM to peer phymode %d: %d\n",
6778 					    arsta->addr, peer_phymode, err);
6779 				return;
6780 			}
6781 			err = ath12k_wmi_set_peer_param(ar, arsta->addr,
6782 							arvif->vdev_id, WMI_PEER_CHWIDTH,
6783 							bw);
6784 			if (err)
6785 				ath12k_warn(ar->ab, "failed to update STA %pM to peer bandwidth %d: %d\n",
6786 					    arsta->addr, bw, err);
6787 		} else {
6788 			/* When we downgrade bandwidth this will conflict with phymode
6789 			 * and cause to trigger firmware crash. In this case we send
6790 			 * WMI_PEER_CHWIDTH followed by WMI_PEER_PHYMODE
6791 			 */
6792 			ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac bandwidth downgrade for sta %pM new %d old %d\n",
6793 				   arsta->addr, bw, bw_prev);
6794 			err = ath12k_wmi_set_peer_param(ar, arsta->addr,
6795 							arvif->vdev_id, WMI_PEER_CHWIDTH,
6796 							bw);
6797 			if (err) {
6798 				ath12k_warn(ar->ab, "failed to update STA %pM peer to bandwidth %d: %d\n",
6799 					    arsta->addr, bw, err);
6800 				return;
6801 			}
6802 			err = ath12k_wmi_set_peer_param(ar, arsta->addr,
6803 							arvif->vdev_id, WMI_PEER_PHYMODE,
6804 							peer_phymode);
6805 			if (err)
6806 				ath12k_warn(ar->ab, "failed to update STA %pM to peer phymode %d: %d\n",
6807 					    arsta->addr, peer_phymode, err);
6808 		}
6809 	}
6810 
6811 	if (changed & IEEE80211_RC_NSS_CHANGED) {
6812 		ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac update sta %pM nss %d\n",
6813 			   arsta->addr, nss);
6814 
6815 		err = ath12k_wmi_set_peer_param(ar, arsta->addr, arvif->vdev_id,
6816 						WMI_PEER_NSS, nss);
6817 		if (err)
6818 			ath12k_warn(ar->ab, "failed to update STA %pM nss %d: %d\n",
6819 				    arsta->addr, nss, err);
6820 	}
6821 
6822 	if (changed & IEEE80211_RC_SMPS_CHANGED) {
6823 		ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac update sta %pM smps %d\n",
6824 			   arsta->addr, smps);
6825 
6826 		err = ath12k_wmi_set_peer_param(ar, arsta->addr, arvif->vdev_id,
6827 						WMI_PEER_MIMO_PS_STATE, smps);
6828 		if (err)
6829 			ath12k_warn(ar->ab, "failed to update STA %pM smps %d: %d\n",
6830 				    arsta->addr, smps, err);
6831 	}
6832 
6833 	if (changed & IEEE80211_RC_SUPP_RATES_CHANGED) {
6834 		mask = &arvif->bitrate_mask;
6835 		num_vht_rates = ath12k_mac_bitrate_mask_num_vht_rates(ar, band,
6836 								      mask);
6837 		num_he_rates = ath12k_mac_bitrate_mask_num_he_rates(ar, band,
6838 								    mask);
6839 		num_eht_rates = ath12k_mac_bitrate_mask_num_eht_rates(ar, band,
6840 								      mask);
6841 
6842 		/* Peer_assoc_prepare will reject vht rates in
6843 		 * bitrate_mask if its not available in range format and
6844 		 * sets vht tx_rateset as unsupported. So multiple VHT MCS
6845 		 * setting(eg. MCS 4,5,6) per peer is not supported here.
6846 		 * But, Single rate in VHT mask can be set as per-peer
6847 		 * fixed rate. But even if any HT rates are configured in
6848 		 * the bitrate mask, device will not switch to those rates
6849 		 * when per-peer Fixed rate is set.
6850 		 * TODO: Check RATEMASK_CMDID to support auto rates selection
6851 		 * across HT/VHT and for multiple VHT MCS support.
6852 		 */
6853 		link_sta = ath12k_mac_get_link_sta(arsta);
6854 		if (!link_sta) {
6855 			ath12k_warn(ar->ab, "unable to access link sta in peer assoc he for sta %pM link %u\n",
6856 				    sta->addr, arsta->link_id);
6857 			return;
6858 		}
6859 
6860 		if (link_sta->vht_cap.vht_supported && num_vht_rates == 1) {
6861 			ath12k_mac_set_peer_vht_fixed_rate(arvif, arsta, mask,
6862 							   band);
6863 		} else if (link_sta->he_cap.has_he && num_he_rates == 1) {
6864 			ath12k_mac_set_peer_he_fixed_rate(arvif, arsta, mask, band);
6865 		} else if (link_sta->eht_cap.has_eht && num_eht_rates == 1) {
6866 			err = ath12k_mac_set_peer_eht_fixed_rate(arvif, arsta,
6867 								 mask, band);
6868 			if (err) {
6869 				ath12k_warn(ar->ab,
6870 					    "failed to set peer EHT fixed rate for STA %pM ret %d\n",
6871 					    arsta->addr, err);
6872 				return;
6873 			}
6874 		} else {
6875 			/* If the peer is non-VHT/HE/EHT or no fixed VHT/HE/EHT
6876 			 * rate is provided in the new bitrate mask we set the
6877 			 * other rates using peer_assoc command. Also clear
6878 			 * the peer fixed rate settings as it has higher proprity
6879 			 * than peer assoc
6880 			 */
6881 			err = ath12k_wmi_set_peer_param(ar, arsta->addr,
6882 							arvif->vdev_id,
6883 							WMI_PEER_PARAM_FIXED_RATE,
6884 							WMI_FIXED_RATE_NONE);
6885 			if (err)
6886 				ath12k_warn(ar->ab,
6887 					    "failed to disable peer fixed rate for STA %pM ret %d\n",
6888 					    arsta->addr, err);
6889 
6890 			ath12k_peer_assoc_prepare(ar, arvif, arsta,
6891 						  peer_arg, true);
6892 
6893 			peer_arg->is_assoc = false;
6894 
6895 			ath12k_mac_peer_assoc(ar, peer_arg);
6896 		}
6897 	}
6898 }
6899 
6900 static void ath12k_mac_free_unassign_link_sta(struct ath12k_hw *ah,
6901 					      struct ath12k_sta *ahsta,
6902 					      u8 link_id)
6903 {
6904 	struct ath12k_link_sta *arsta;
6905 
6906 	lockdep_assert_wiphy(ah->hw->wiphy);
6907 
6908 	if (WARN_ON(link_id >= IEEE80211_MLD_MAX_NUM_LINKS))
6909 		return;
6910 
6911 	arsta = wiphy_dereference(ah->hw->wiphy, ahsta->link[link_id]);
6912 	if (WARN_ON(!arsta))
6913 		return;
6914 
6915 	ahsta->links_map &= ~BIT(link_id);
6916 	ahsta->free_logical_link_idx_map |= BIT(arsta->link_idx);
6917 
6918 	rcu_assign_pointer(ahsta->link[link_id], NULL);
6919 	synchronize_rcu();
6920 
6921 	if (arsta == &ahsta->deflink) {
6922 		arsta->link_id = ATH12K_INVALID_LINK_ID;
6923 		arsta->ahsta = NULL;
6924 		arsta->arvif = NULL;
6925 		return;
6926 	}
6927 
6928 	kfree(arsta);
6929 }
6930 
6931 static int ath12k_mac_inc_num_stations(struct ath12k_link_vif *arvif,
6932 				       struct ath12k_link_sta *arsta)
6933 {
6934 	struct ieee80211_sta *sta = ath12k_ahsta_to_sta(arsta->ahsta);
6935 	struct ath12k *ar = arvif->ar;
6936 
6937 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
6938 
6939 	if (arvif->ahvif->vdev_type == WMI_VDEV_TYPE_STA && !sta->tdls)
6940 		return 0;
6941 
6942 	if (ar->num_stations >= ar->max_num_stations)
6943 		return -ENOBUFS;
6944 
6945 	ar->num_stations++;
6946 	arvif->num_stations++;
6947 
6948 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
6949 		   "mac station %pM connected to vdev %u num_stations %u\n",
6950 		   arsta->addr, arvif->vdev_id, arvif->num_stations);
6951 
6952 	return 0;
6953 }
6954 
6955 static void ath12k_mac_dec_num_stations(struct ath12k_link_vif *arvif,
6956 					struct ath12k_link_sta *arsta)
6957 {
6958 	struct ieee80211_sta *sta = ath12k_ahsta_to_sta(arsta->ahsta);
6959 	struct ath12k *ar = arvif->ar;
6960 
6961 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
6962 
6963 	if (arvif->ahvif->vdev_type == WMI_VDEV_TYPE_STA && !sta->tdls)
6964 		return;
6965 
6966 	ar->num_stations--;
6967 
6968 	if (arvif->num_stations) {
6969 		arvif->num_stations--;
6970 		ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
6971 			   "mac station %pM disconnected from vdev %u num_stations %u\n",
6972 			   arsta->addr, arvif->vdev_id, arvif->num_stations);
6973 	} else {
6974 		ath12k_warn(ar->ab,
6975 			    "mac station %pM disconnect for vdev %u without any connected station\n",
6976 			    arsta->addr, arvif->vdev_id);
6977 	}
6978 }
6979 
6980 static void ath12k_mac_station_post_remove(struct ath12k *ar,
6981 					   struct ath12k_link_vif *arvif,
6982 					   struct ath12k_link_sta *arsta)
6983 {
6984 	struct ieee80211_vif *vif = ath12k_ahvif_to_vif(arvif->ahvif);
6985 	struct ieee80211_sta *sta = ath12k_ahsta_to_sta(arsta->ahsta);
6986 	struct ath12k_dp_link_peer *peer;
6987 	struct ath12k_dp *dp = ath12k_ab_to_dp(ar->ab);
6988 
6989 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
6990 
6991 	ath12k_mac_dec_num_stations(arvif, arsta);
6992 
6993 	spin_lock_bh(&dp->dp_lock);
6994 
6995 	peer = ath12k_dp_link_peer_find_by_vdev_and_addr(dp, arvif->vdev_id,
6996 							 arsta->addr);
6997 	if (peer && peer->sta == sta) {
6998 		ath12k_warn(ar->ab, "Found peer entry %pM n vdev %i after it was supposedly removed\n",
6999 			    vif->addr, arvif->vdev_id);
7000 		peer->sta = NULL;
7001 
7002 		ath12k_dp_link_peer_free(peer);
7003 		ar->num_peers--;
7004 	}
7005 
7006 	spin_unlock_bh(&dp->dp_lock);
7007 }
7008 
7009 static int ath12k_mac_station_unauthorize(struct ath12k *ar,
7010 					  struct ath12k_link_vif *arvif,
7011 					  struct ath12k_link_sta *arsta)
7012 {
7013 	struct ath12k_dp_link_peer *peer;
7014 	int ret;
7015 	struct ath12k_dp *dp = ath12k_ab_to_dp(ar->ab);
7016 
7017 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
7018 
7019 	spin_lock_bh(&dp->dp_lock);
7020 
7021 	peer = ath12k_dp_link_peer_find_by_vdev_and_addr(dp, arvif->vdev_id,
7022 							 arsta->addr);
7023 	if (peer)
7024 		peer->is_authorized = false;
7025 
7026 	spin_unlock_bh(&dp->dp_lock);
7027 
7028 	/* Driver must clear the keys during the state change from
7029 	 * IEEE80211_STA_AUTHORIZED to IEEE80211_STA_ASSOC, since after
7030 	 * returning from here, mac80211 is going to delete the keys
7031 	 * in __sta_info_destroy_part2(). This will ensure that the driver does
7032 	 * not retain stale key references after mac80211 deletes the keys.
7033 	 */
7034 	ret = ath12k_clear_peer_keys(arvif, arsta->addr);
7035 	if (ret) {
7036 		ath12k_warn(ar->ab, "failed to clear all peer keys for vdev %i: %d\n",
7037 			    arvif->vdev_id, ret);
7038 		return ret;
7039 	}
7040 
7041 	return 0;
7042 }
7043 
7044 static int ath12k_mac_station_authorize(struct ath12k *ar,
7045 					struct ath12k_link_vif *arvif,
7046 					struct ath12k_link_sta *arsta)
7047 {
7048 	struct ath12k_dp_link_peer *peer;
7049 	struct ieee80211_vif *vif = ath12k_ahvif_to_vif(arvif->ahvif);
7050 	int ret;
7051 	struct ath12k_dp *dp = ath12k_ab_to_dp(ar->ab);
7052 
7053 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
7054 
7055 	spin_lock_bh(&dp->dp_lock);
7056 
7057 	peer = ath12k_dp_link_peer_find_by_vdev_and_addr(dp, arvif->vdev_id,
7058 							 arsta->addr);
7059 	if (peer)
7060 		peer->is_authorized = true;
7061 
7062 	spin_unlock_bh(&dp->dp_lock);
7063 
7064 	if (vif->type == NL80211_IFTYPE_STATION && arvif->is_up) {
7065 		ret = ath12k_wmi_set_peer_param(ar, arsta->addr,
7066 						arvif->vdev_id,
7067 						WMI_PEER_AUTHORIZE,
7068 						1);
7069 		if (ret) {
7070 			ath12k_warn(ar->ab, "Unable to authorize peer %pM vdev %d: %d\n",
7071 				    arsta->addr, arvif->vdev_id, ret);
7072 			return ret;
7073 		}
7074 	}
7075 
7076 	return 0;
7077 }
7078 
7079 static int ath12k_mac_station_remove(struct ath12k *ar,
7080 				     struct ath12k_link_vif *arvif,
7081 				     struct ath12k_link_sta *arsta)
7082 {
7083 	struct ieee80211_sta *sta = ath12k_ahsta_to_sta(arsta->ahsta);
7084 	struct ath12k_vif *ahvif = arvif->ahvif;
7085 	int ret = 0;
7086 	struct ath12k_link_sta *temp_arsta;
7087 
7088 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
7089 
7090 	wiphy_work_cancel(ar->ah->hw->wiphy, &arsta->update_wk);
7091 
7092 	if (ahvif->vdev_type == WMI_VDEV_TYPE_STA) {
7093 		ath12k_bss_disassoc(ar, arvif);
7094 		ret = ath12k_mac_vdev_stop(arvif);
7095 		if (ret)
7096 			ath12k_warn(ar->ab, "failed to stop vdev %i: %d\n",
7097 				    arvif->vdev_id, ret);
7098 	}
7099 
7100 	if (sta->mlo)
7101 		return ret;
7102 
7103 	ath12k_dp_peer_cleanup(ar, arvif->vdev_id, arsta->addr);
7104 
7105 	ret = ath12k_peer_delete(ar, arvif->vdev_id, arsta->addr);
7106 	if (ret)
7107 		ath12k_warn(ar->ab, "Failed to delete peer: %pM for VDEV: %d\n",
7108 			    arsta->addr, arvif->vdev_id);
7109 	else
7110 		ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "Removed peer: %pM for VDEV: %d\n",
7111 			   arsta->addr, arvif->vdev_id);
7112 
7113 	ath12k_mac_station_post_remove(ar, arvif, arsta);
7114 
7115 	spin_lock_bh(&ar->ab->base_lock);
7116 
7117 	/* To handle roaming and split phy scenario */
7118 	temp_arsta = ath12k_link_sta_find_by_addr(ar->ab, arsta->addr);
7119 	if (temp_arsta && temp_arsta->arvif->ar == ar)
7120 		ath12k_link_sta_rhash_delete(ar->ab, arsta);
7121 
7122 	spin_unlock_bh(&ar->ab->base_lock);
7123 
7124 	if (sta->valid_links)
7125 		ath12k_mac_free_unassign_link_sta(ahvif->ah,
7126 						  arsta->ahsta, arsta->link_id);
7127 
7128 	return ret;
7129 }
7130 
7131 static int ath12k_mac_station_add(struct ath12k *ar,
7132 				  struct ath12k_link_vif *arvif,
7133 				  struct ath12k_link_sta *arsta)
7134 {
7135 	struct ath12k_base *ab = ar->ab;
7136 	struct ieee80211_vif *vif = ath12k_ahvif_to_vif(arvif->ahvif);
7137 	struct ieee80211_sta *sta = ath12k_ahsta_to_sta(arsta->ahsta);
7138 	struct ath12k_wmi_peer_create_arg peer_param = {};
7139 	int ret;
7140 	struct ath12k_link_sta *temp_arsta;
7141 
7142 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
7143 
7144 	ret = ath12k_mac_inc_num_stations(arvif, arsta);
7145 	if (ret) {
7146 		ath12k_warn(ab, "refusing to associate station: too many connected already (%d)\n",
7147 			    ar->max_num_stations);
7148 		goto exit;
7149 	}
7150 
7151 	spin_lock_bh(&ab->base_lock);
7152 
7153 	/*
7154 	 * In case of Split PHY and roaming scenario, pdev idx
7155 	 * might differ but both the pdev will share same rhash
7156 	 * table. In that case update the rhash table if link_sta is
7157 	 * already present
7158 	 */
7159 	temp_arsta = ath12k_link_sta_find_by_addr(ab, arsta->addr);
7160 	if (temp_arsta && temp_arsta->arvif->ar != ar)
7161 		ath12k_link_sta_rhash_delete(ab, temp_arsta);
7162 
7163 	ret = ath12k_link_sta_rhash_add(ab, arsta);
7164 	spin_unlock_bh(&ab->base_lock);
7165 	if (ret) {
7166 		ath12k_warn(ab, "Failed to add arsta: %pM to hash table, ret: %d",
7167 			    arsta->addr, ret);
7168 		goto dec_num_station;
7169 	}
7170 
7171 	peer_param.vdev_id = arvif->vdev_id;
7172 	peer_param.peer_addr = arsta->addr;
7173 	peer_param.peer_type = WMI_PEER_TYPE_DEFAULT;
7174 	peer_param.ml_enabled = sta->mlo;
7175 
7176 	ret = ath12k_peer_create(ar, arvif, sta, &peer_param);
7177 	if (ret) {
7178 		ath12k_warn(ab, "Failed to add peer: %pM for VDEV: %d\n",
7179 			    arsta->addr, arvif->vdev_id);
7180 		goto free_peer;
7181 	}
7182 
7183 	ath12k_dbg(ab, ATH12K_DBG_MAC, "Added peer: %pM for VDEV: %d\n",
7184 		   arsta->addr, arvif->vdev_id);
7185 
7186 	if (ieee80211_vif_is_mesh(vif)) {
7187 		ret = ath12k_wmi_set_peer_param(ar, arsta->addr,
7188 						arvif->vdev_id,
7189 						WMI_PEER_USE_4ADDR, 1);
7190 		if (ret) {
7191 			ath12k_warn(ab, "failed to STA %pM 4addr capability: %d\n",
7192 				    arsta->addr, ret);
7193 			goto free_peer;
7194 		}
7195 	}
7196 
7197 	ret = ath12k_dp_peer_setup(ar, arvif->vdev_id, arsta->addr);
7198 	if (ret) {
7199 		ath12k_warn(ab, "failed to setup dp for peer %pM on vdev %i (%d)\n",
7200 			    arsta->addr, arvif->vdev_id, ret);
7201 		goto free_peer;
7202 	}
7203 
7204 	if (ab->hw_params->vdev_start_delay &&
7205 	    !arvif->is_started &&
7206 	    arvif->ahvif->vdev_type != WMI_VDEV_TYPE_AP) {
7207 		ret = ath12k_start_vdev_delay(ar, arvif);
7208 		if (ret) {
7209 			ath12k_warn(ab, "failed to delay vdev start: %d\n", ret);
7210 			goto free_peer;
7211 		}
7212 	}
7213 
7214 	return 0;
7215 
7216 free_peer:
7217 	ath12k_peer_delete(ar, arvif->vdev_id, arsta->addr);
7218 	spin_lock_bh(&ab->base_lock);
7219 	ath12k_link_sta_rhash_delete(ab, arsta);
7220 	spin_unlock_bh(&ab->base_lock);
7221 dec_num_station:
7222 	ath12k_mac_dec_num_stations(arvif, arsta);
7223 exit:
7224 	return ret;
7225 }
7226 
7227 static int ath12k_mac_assign_link_sta(struct ath12k_hw *ah,
7228 				      struct ath12k_sta *ahsta,
7229 				      struct ath12k_link_sta *arsta,
7230 				      struct ath12k_vif *ahvif,
7231 				      u8 link_id)
7232 {
7233 	struct ieee80211_sta *sta = ath12k_ahsta_to_sta(ahsta);
7234 	struct ieee80211_link_sta *link_sta;
7235 	struct ath12k_link_vif *arvif;
7236 	int link_idx;
7237 
7238 	lockdep_assert_wiphy(ah->hw->wiphy);
7239 
7240 	if (!arsta || link_id >= IEEE80211_MLD_MAX_NUM_LINKS)
7241 		return -EINVAL;
7242 
7243 	arvif = wiphy_dereference(ah->hw->wiphy, ahvif->link[link_id]);
7244 	if (!arvif)
7245 		return -EINVAL;
7246 
7247 	memset(arsta, 0, sizeof(*arsta));
7248 
7249 	link_sta = wiphy_dereference(ah->hw->wiphy, sta->link[link_id]);
7250 	if (!link_sta)
7251 		return -EINVAL;
7252 
7253 	ether_addr_copy(arsta->addr, link_sta->addr);
7254 
7255 	if (!ahsta->free_logical_link_idx_map)
7256 		return -ENOSPC;
7257 
7258 	/*
7259 	 * Allocate a logical link index by selecting the first available bit
7260 	 * from the free logical index map
7261 	 */
7262 	link_idx = __ffs(ahsta->free_logical_link_idx_map);
7263 	ahsta->free_logical_link_idx_map &= ~BIT(link_idx);
7264 	arsta->link_idx = link_idx;
7265 
7266 	arsta->link_id = link_id;
7267 	ahsta->links_map |= BIT(arsta->link_id);
7268 	arsta->arvif = arvif;
7269 	arsta->ahsta = ahsta;
7270 	ahsta->ahvif = ahvif;
7271 
7272 	wiphy_work_init(&arsta->update_wk, ath12k_sta_rc_update_wk);
7273 
7274 	rcu_assign_pointer(ahsta->link[link_id], arsta);
7275 
7276 	return 0;
7277 }
7278 
7279 static void ath12k_mac_ml_station_remove(struct ath12k_vif *ahvif,
7280 					 struct ath12k_sta *ahsta)
7281 {
7282 	struct ieee80211_sta *sta = ath12k_ahsta_to_sta(ahsta);
7283 	struct ath12k_hw *ah = ahvif->ah;
7284 	struct ath12k_link_vif *arvif;
7285 	struct ath12k_link_sta *arsta;
7286 	unsigned long links;
7287 	struct ath12k *ar;
7288 	u8 link_id;
7289 
7290 	lockdep_assert_wiphy(ah->hw->wiphy);
7291 
7292 	ath12k_peer_mlo_link_peers_delete(ahvif, ahsta);
7293 
7294 	/* validate link station removal and clear arsta links */
7295 	links = ahsta->links_map;
7296 	for_each_set_bit(link_id, &links, IEEE80211_MLD_MAX_NUM_LINKS) {
7297 		arvif = wiphy_dereference(ah->hw->wiphy, ahvif->link[link_id]);
7298 		arsta = wiphy_dereference(ah->hw->wiphy, ahsta->link[link_id]);
7299 		if (!arvif || !arsta)
7300 			continue;
7301 
7302 		ar = arvif->ar;
7303 
7304 		ath12k_mac_station_post_remove(ar, arvif, arsta);
7305 
7306 		spin_lock_bh(&ar->ab->base_lock);
7307 		ath12k_link_sta_rhash_delete(ar->ab, arsta);
7308 		spin_unlock_bh(&ar->ab->base_lock);
7309 
7310 		ath12k_mac_free_unassign_link_sta(ah, ahsta, link_id);
7311 	}
7312 
7313 	if (sta->mlo)
7314 		ath12k_peer_ml_free(ah, ahsta);
7315 }
7316 
7317 static int ath12k_mac_handle_link_sta_state(struct ieee80211_hw *hw,
7318 					    struct ath12k_link_vif *arvif,
7319 					    struct ath12k_link_sta *arsta,
7320 					    enum ieee80211_sta_state old_state,
7321 					    enum ieee80211_sta_state new_state)
7322 {
7323 	struct ieee80211_vif *vif = ath12k_ahvif_to_vif(arvif->ahvif);
7324 	struct ieee80211_bss_conf *link_conf;
7325 	struct ath12k *ar = arvif->ar;
7326 	struct ath12k_reg_info *reg_info;
7327 	struct ath12k_base *ab = ar->ab;
7328 	int ret = 0;
7329 
7330 	lockdep_assert_wiphy(hw->wiphy);
7331 
7332 	ath12k_dbg(ab, ATH12K_DBG_MAC, "mac handle link %u sta %pM state %d -> %d\n",
7333 		   arsta->link_id, arsta->addr, old_state, new_state);
7334 
7335 	/* IEEE80211_STA_NONE -> IEEE80211_STA_NOTEXIST: Remove the station
7336 	 * from driver
7337 	 */
7338 	if ((old_state == IEEE80211_STA_NONE &&
7339 	     new_state == IEEE80211_STA_NOTEXIST)) {
7340 		ret = ath12k_mac_station_remove(ar, arvif, arsta);
7341 		if (ret) {
7342 			ath12k_warn(ab, "Failed to remove station: %pM for VDEV: %d\n",
7343 				    arsta->addr, arvif->vdev_id);
7344 			goto exit;
7345 		}
7346 	}
7347 
7348 	/* IEEE80211_STA_NOTEXIST -> IEEE80211_STA_NONE: Add new station to driver */
7349 	if (old_state == IEEE80211_STA_NOTEXIST &&
7350 	    new_state == IEEE80211_STA_NONE) {
7351 		ret = ath12k_mac_station_add(ar, arvif, arsta);
7352 		if (ret)
7353 			ath12k_warn(ab, "Failed to add station: %pM for VDEV: %d\n",
7354 				    arsta->addr, arvif->vdev_id);
7355 
7356 	/* IEEE80211_STA_AUTH -> IEEE80211_STA_ASSOC: Send station assoc command for
7357 	 * peer associated to AP/Mesh/ADHOC vif type.
7358 	 */
7359 	} else if (old_state == IEEE80211_STA_AUTH &&
7360 		   new_state == IEEE80211_STA_ASSOC &&
7361 		   (vif->type == NL80211_IFTYPE_AP ||
7362 		    vif->type == NL80211_IFTYPE_MESH_POINT ||
7363 		    vif->type == NL80211_IFTYPE_ADHOC)) {
7364 		ret = ath12k_mac_station_assoc(ar, arvif, arsta, false);
7365 		if (ret)
7366 			ath12k_warn(ab, "Failed to associate station: %pM\n",
7367 				    arsta->addr);
7368 
7369 	/* IEEE80211_STA_ASSOC -> IEEE80211_STA_AUTHORIZED: set peer status as
7370 	 * authorized
7371 	 */
7372 	} else if (old_state == IEEE80211_STA_ASSOC &&
7373 		   new_state == IEEE80211_STA_AUTHORIZED) {
7374 		ret = ath12k_mac_station_authorize(ar, arvif, arsta);
7375 		if (ret) {
7376 			ath12k_warn(ab, "Failed to authorize station: %pM\n",
7377 				    arsta->addr);
7378 			goto exit;
7379 		}
7380 
7381 		if (ath12k_wmi_supports_6ghz_cc_ext(ar) &&
7382 		    arvif->ahvif->vdev_type == WMI_VDEV_TYPE_STA) {
7383 			link_conf = ath12k_mac_get_link_bss_conf(arvif);
7384 			reg_info = ab->reg_info[ar->pdev_idx];
7385 			ath12k_dbg(ab, ATH12K_DBG_MAC, "connection done, update reg rules\n");
7386 			ath12k_hw_to_ah(hw)->regd_updated = false;
7387 			ath12k_reg_handle_chan_list(ab, reg_info, arvif->ahvif->vdev_type,
7388 						    link_conf->power_type);
7389 		}
7390 
7391 	/* IEEE80211_STA_AUTHORIZED -> IEEE80211_STA_ASSOC: station may be in removal,
7392 	 * deauthorize it.
7393 	 */
7394 	} else if (old_state == IEEE80211_STA_AUTHORIZED &&
7395 		   new_state == IEEE80211_STA_ASSOC) {
7396 		ath12k_mac_station_unauthorize(ar, arvif, arsta);
7397 
7398 	/* IEEE80211_STA_ASSOC -> IEEE80211_STA_AUTH: disassoc peer connected to
7399 	 * AP/mesh/ADHOC vif type.
7400 	 */
7401 	} else if (old_state == IEEE80211_STA_ASSOC &&
7402 		   new_state == IEEE80211_STA_AUTH &&
7403 		   (vif->type == NL80211_IFTYPE_AP ||
7404 		    vif->type == NL80211_IFTYPE_MESH_POINT ||
7405 		    vif->type == NL80211_IFTYPE_ADHOC)) {
7406 		ret = ath12k_mac_station_disassoc(ar, arvif, arsta);
7407 		if (ret)
7408 			ath12k_warn(ab, "Failed to disassociate station: %pM\n",
7409 				    arsta->addr);
7410 	}
7411 
7412 exit:
7413 	return ret;
7414 }
7415 
7416 static bool ath12k_mac_is_freq_on_mac(struct ath12k_hw_mode_freq_range_arg *freq_range,
7417 				      u32 freq, u8 mac_id)
7418 {
7419 	return (freq >= freq_range[mac_id].low_2ghz_freq &&
7420 		freq <= freq_range[mac_id].high_2ghz_freq) ||
7421 	       (freq >= freq_range[mac_id].low_5ghz_freq &&
7422 		freq <= freq_range[mac_id].high_5ghz_freq);
7423 }
7424 
7425 static bool
7426 ath12k_mac_2_freq_same_mac_in_freq_range(struct ath12k_base *ab,
7427 					 struct ath12k_hw_mode_freq_range_arg *freq_range,
7428 					 u32 freq_link1, u32 freq_link2)
7429 {
7430 	u8 i;
7431 
7432 	for (i = 0; i < MAX_RADIOS; i++) {
7433 		if (ath12k_mac_is_freq_on_mac(freq_range, freq_link1, i) &&
7434 		    ath12k_mac_is_freq_on_mac(freq_range, freq_link2, i))
7435 			return true;
7436 	}
7437 
7438 	return false;
7439 }
7440 
7441 static bool ath12k_mac_is_hw_dbs_capable(struct ath12k_base *ab)
7442 {
7443 	return test_bit(WMI_TLV_SERVICE_DUAL_BAND_SIMULTANEOUS_SUPPORT,
7444 			ab->wmi_ab.svc_map) &&
7445 	       ab->wmi_ab.hw_mode_info.support_dbs;
7446 }
7447 
7448 static bool ath12k_mac_2_freq_same_mac_in_dbs(struct ath12k_base *ab,
7449 					      u32 freq_link1, u32 freq_link2)
7450 {
7451 	struct ath12k_hw_mode_freq_range_arg *freq_range;
7452 
7453 	if (!ath12k_mac_is_hw_dbs_capable(ab))
7454 		return true;
7455 
7456 	freq_range = ab->wmi_ab.hw_mode_info.freq_range_caps[ATH12K_HW_MODE_DBS];
7457 	return ath12k_mac_2_freq_same_mac_in_freq_range(ab, freq_range,
7458 							freq_link1, freq_link2);
7459 }
7460 
7461 static bool ath12k_mac_is_hw_sbs_capable(struct ath12k_base *ab)
7462 {
7463 	return test_bit(WMI_TLV_SERVICE_DUAL_BAND_SIMULTANEOUS_SUPPORT,
7464 			ab->wmi_ab.svc_map) &&
7465 	       ab->wmi_ab.hw_mode_info.support_sbs;
7466 }
7467 
7468 static bool ath12k_mac_2_freq_same_mac_in_sbs(struct ath12k_base *ab,
7469 					      u32 freq_link1, u32 freq_link2)
7470 {
7471 	struct ath12k_hw_mode_info *info = &ab->wmi_ab.hw_mode_info;
7472 	struct ath12k_hw_mode_freq_range_arg *sbs_uppr_share;
7473 	struct ath12k_hw_mode_freq_range_arg *sbs_low_share;
7474 	struct ath12k_hw_mode_freq_range_arg *sbs_range;
7475 
7476 	if (!ath12k_mac_is_hw_sbs_capable(ab))
7477 		return true;
7478 
7479 	if (ab->wmi_ab.sbs_lower_band_end_freq) {
7480 		sbs_uppr_share = info->freq_range_caps[ATH12K_HW_MODE_SBS_UPPER_SHARE];
7481 		sbs_low_share = info->freq_range_caps[ATH12K_HW_MODE_SBS_LOWER_SHARE];
7482 
7483 		return ath12k_mac_2_freq_same_mac_in_freq_range(ab, sbs_low_share,
7484 								freq_link1, freq_link2) ||
7485 		       ath12k_mac_2_freq_same_mac_in_freq_range(ab, sbs_uppr_share,
7486 								freq_link1, freq_link2);
7487 	}
7488 
7489 	sbs_range = info->freq_range_caps[ATH12K_HW_MODE_SBS];
7490 	return ath12k_mac_2_freq_same_mac_in_freq_range(ab, sbs_range,
7491 							freq_link1, freq_link2);
7492 }
7493 
7494 static bool ath12k_mac_freqs_on_same_mac(struct ath12k_base *ab,
7495 					 u32 freq_link1, u32 freq_link2)
7496 {
7497 	return ath12k_mac_2_freq_same_mac_in_dbs(ab, freq_link1, freq_link2) &&
7498 	       ath12k_mac_2_freq_same_mac_in_sbs(ab, freq_link1, freq_link2);
7499 }
7500 
7501 static int ath12k_mac_mlo_sta_set_link_active(struct ath12k_base *ab,
7502 					      enum wmi_mlo_link_force_reason reason,
7503 					      enum wmi_mlo_link_force_mode mode,
7504 					      u8 *mlo_vdev_id_lst,
7505 					      u8 num_mlo_vdev,
7506 					      u8 *mlo_inactive_vdev_lst,
7507 					      u8 num_mlo_inactive_vdev)
7508 {
7509 	struct wmi_mlo_link_set_active_arg param = {};
7510 	u32 entry_idx, entry_offset, vdev_idx;
7511 	u8 vdev_id;
7512 
7513 	param.reason = reason;
7514 	param.force_mode = mode;
7515 
7516 	for (vdev_idx = 0; vdev_idx < num_mlo_vdev; vdev_idx++) {
7517 		vdev_id = mlo_vdev_id_lst[vdev_idx];
7518 		entry_idx = vdev_id / 32;
7519 		entry_offset = vdev_id % 32;
7520 		if (entry_idx >= WMI_MLO_LINK_NUM_SZ) {
7521 			ath12k_warn(ab, "Invalid entry_idx %d num_mlo_vdev %d vdev %d",
7522 				    entry_idx, num_mlo_vdev, vdev_id);
7523 			return -EINVAL;
7524 		}
7525 		param.vdev_bitmap[entry_idx] |= 1 << entry_offset;
7526 		/* update entry number if entry index changed */
7527 		if (param.num_vdev_bitmap < entry_idx + 1)
7528 			param.num_vdev_bitmap = entry_idx + 1;
7529 	}
7530 
7531 	ath12k_dbg(ab, ATH12K_DBG_MAC,
7532 		   "num_vdev_bitmap %d vdev_bitmap[0] = 0x%x, vdev_bitmap[1] = 0x%x",
7533 		   param.num_vdev_bitmap, param.vdev_bitmap[0], param.vdev_bitmap[1]);
7534 
7535 	if (mode == WMI_MLO_LINK_FORCE_MODE_ACTIVE_INACTIVE) {
7536 		for (vdev_idx = 0; vdev_idx < num_mlo_inactive_vdev; vdev_idx++) {
7537 			vdev_id = mlo_inactive_vdev_lst[vdev_idx];
7538 			entry_idx = vdev_id / 32;
7539 			entry_offset = vdev_id % 32;
7540 			if (entry_idx >= WMI_MLO_LINK_NUM_SZ) {
7541 				ath12k_warn(ab, "Invalid entry_idx %d num_mlo_vdev %d vdev %d",
7542 					    entry_idx, num_mlo_inactive_vdev, vdev_id);
7543 				return -EINVAL;
7544 			}
7545 			param.inactive_vdev_bitmap[entry_idx] |= 1 << entry_offset;
7546 			/* update entry number if entry index changed */
7547 			if (param.num_inactive_vdev_bitmap < entry_idx + 1)
7548 				param.num_inactive_vdev_bitmap = entry_idx + 1;
7549 		}
7550 
7551 		ath12k_dbg(ab, ATH12K_DBG_MAC,
7552 			   "num_vdev_bitmap %d inactive_vdev_bitmap[0] = 0x%x, inactive_vdev_bitmap[1] = 0x%x",
7553 			   param.num_inactive_vdev_bitmap,
7554 			   param.inactive_vdev_bitmap[0],
7555 			   param.inactive_vdev_bitmap[1]);
7556 	}
7557 
7558 	if (mode == WMI_MLO_LINK_FORCE_MODE_ACTIVE_LINK_NUM ||
7559 	    mode == WMI_MLO_LINK_FORCE_MODE_INACTIVE_LINK_NUM) {
7560 		param.num_link_entry = 1;
7561 		param.link_num[0].num_of_link = num_mlo_vdev - 1;
7562 	}
7563 
7564 	return ath12k_wmi_send_mlo_link_set_active_cmd(ab, &param);
7565 }
7566 
7567 static int ath12k_mac_mlo_sta_update_link_active(struct ath12k_base *ab,
7568 						 struct ieee80211_hw *hw,
7569 						 struct ath12k_vif *ahvif)
7570 {
7571 	u8 mlo_vdev_id_lst[IEEE80211_MLD_MAX_NUM_LINKS] = {};
7572 	u32 mlo_freq_list[IEEE80211_MLD_MAX_NUM_LINKS] = {};
7573 	unsigned long links = ahvif->links_map;
7574 	enum wmi_mlo_link_force_reason reason;
7575 	struct ieee80211_chanctx_conf *conf;
7576 	enum wmi_mlo_link_force_mode mode;
7577 	struct ieee80211_bss_conf *info;
7578 	struct ath12k_link_vif *arvif;
7579 	u8 num_mlo_vdev = 0;
7580 	u8 link_id;
7581 
7582 	for_each_set_bit(link_id, &links, IEEE80211_MLD_MAX_NUM_LINKS) {
7583 		arvif = wiphy_dereference(hw->wiphy, ahvif->link[link_id]);
7584 		/* make sure vdev is created on this device */
7585 		if (!arvif || !arvif->is_created || arvif->ar->ab != ab)
7586 			continue;
7587 
7588 		info = ath12k_mac_get_link_bss_conf(arvif);
7589 		conf = wiphy_dereference(hw->wiphy, info->chanctx_conf);
7590 		mlo_freq_list[num_mlo_vdev] = conf->def.chan->center_freq;
7591 
7592 		mlo_vdev_id_lst[num_mlo_vdev] = arvif->vdev_id;
7593 		num_mlo_vdev++;
7594 	}
7595 
7596 	/* It is not allowed to activate more links than a single device
7597 	 * supported. Something goes wrong if we reach here.
7598 	 */
7599 	if (num_mlo_vdev > ATH12K_NUM_MAX_ACTIVE_LINKS_PER_DEVICE) {
7600 		WARN_ON_ONCE(1);
7601 		return -EINVAL;
7602 	}
7603 
7604 	/* if 2 links are established and both link channels fall on the
7605 	 * same hardware MAC, send command to firmware to deactivate one
7606 	 * of them.
7607 	 */
7608 	if (num_mlo_vdev == 2 &&
7609 	    ath12k_mac_freqs_on_same_mac(ab, mlo_freq_list[0],
7610 					 mlo_freq_list[1])) {
7611 		mode = WMI_MLO_LINK_FORCE_MODE_INACTIVE_LINK_NUM;
7612 		reason = WMI_MLO_LINK_FORCE_REASON_NEW_CONNECT;
7613 		return ath12k_mac_mlo_sta_set_link_active(ab, reason, mode,
7614 							  mlo_vdev_id_lst, num_mlo_vdev,
7615 							  NULL, 0);
7616 	}
7617 
7618 	return 0;
7619 }
7620 
7621 static bool ath12k_mac_are_sbs_chan(struct ath12k_base *ab, u32 freq_1, u32 freq_2)
7622 {
7623 	if (!ath12k_mac_is_hw_sbs_capable(ab))
7624 		return false;
7625 
7626 	if (ath12k_is_2ghz_channel_freq(freq_1) ||
7627 	    ath12k_is_2ghz_channel_freq(freq_2))
7628 		return false;
7629 
7630 	return !ath12k_mac_2_freq_same_mac_in_sbs(ab, freq_1, freq_2);
7631 }
7632 
7633 static bool ath12k_mac_are_dbs_chan(struct ath12k_base *ab, u32 freq_1, u32 freq_2)
7634 {
7635 	if (!ath12k_mac_is_hw_dbs_capable(ab))
7636 		return false;
7637 
7638 	return !ath12k_mac_2_freq_same_mac_in_dbs(ab, freq_1, freq_2);
7639 }
7640 
7641 static int ath12k_mac_select_links(struct ath12k_base *ab,
7642 				   struct ieee80211_vif *vif,
7643 				   struct ieee80211_hw *hw,
7644 				   u16 *selected_links)
7645 {
7646 	unsigned long useful_links = ieee80211_vif_usable_links(vif);
7647 	struct ath12k_vif *ahvif = ath12k_vif_to_ahvif(vif);
7648 	u8 num_useful_links = hweight_long(useful_links);
7649 	struct ieee80211_chanctx_conf *chanctx;
7650 	struct ath12k_link_vif *assoc_arvif;
7651 	u32 assoc_link_freq, partner_freq;
7652 	u16 sbs_links = 0, dbs_links = 0;
7653 	struct ieee80211_bss_conf *info;
7654 	struct ieee80211_channel *chan;
7655 	struct ieee80211_sta *sta;
7656 	struct ath12k_sta *ahsta;
7657 	u8 link_id;
7658 
7659 	/* activate all useful links if less than max supported */
7660 	if (num_useful_links <= ATH12K_NUM_MAX_ACTIVE_LINKS_PER_DEVICE) {
7661 		*selected_links = useful_links;
7662 		return 0;
7663 	}
7664 
7665 	/* only in station mode we can get here, so it's safe
7666 	 * to use ap_addr
7667 	 */
7668 	rcu_read_lock();
7669 	sta = ieee80211_find_sta(vif, vif->cfg.ap_addr);
7670 	if (!sta) {
7671 		rcu_read_unlock();
7672 		ath12k_warn(ab, "failed to find sta with addr %pM\n", vif->cfg.ap_addr);
7673 		return -EINVAL;
7674 	}
7675 
7676 	ahsta = ath12k_sta_to_ahsta(sta);
7677 	assoc_arvif = wiphy_dereference(hw->wiphy, ahvif->link[ahsta->assoc_link_id]);
7678 	info = ath12k_mac_get_link_bss_conf(assoc_arvif);
7679 	chanctx = rcu_dereference(info->chanctx_conf);
7680 	assoc_link_freq = chanctx->def.chan->center_freq;
7681 	rcu_read_unlock();
7682 	ath12k_dbg(ab, ATH12K_DBG_MAC, "assoc link %u freq %u\n",
7683 		   assoc_arvif->link_id, assoc_link_freq);
7684 
7685 	/* assoc link is already activated and has to be kept active,
7686 	 * only need to select a partner link from others.
7687 	 */
7688 	useful_links &= ~BIT(assoc_arvif->link_id);
7689 	for_each_set_bit(link_id, &useful_links, IEEE80211_MLD_MAX_NUM_LINKS) {
7690 		info = wiphy_dereference(hw->wiphy, vif->link_conf[link_id]);
7691 		if (!info) {
7692 			ath12k_warn(ab, "failed to get link info for link: %u\n",
7693 				    link_id);
7694 			return -ENOLINK;
7695 		}
7696 
7697 		chan = info->chanreq.oper.chan;
7698 		if (!chan) {
7699 			ath12k_warn(ab, "failed to get chan for link: %u\n", link_id);
7700 			return -EINVAL;
7701 		}
7702 
7703 		partner_freq = chan->center_freq;
7704 		if (ath12k_mac_are_sbs_chan(ab, assoc_link_freq, partner_freq)) {
7705 			sbs_links |= BIT(link_id);
7706 			ath12k_dbg(ab, ATH12K_DBG_MAC, "new SBS link %u freq %u\n",
7707 				   link_id, partner_freq);
7708 			continue;
7709 		}
7710 
7711 		if (ath12k_mac_are_dbs_chan(ab, assoc_link_freq, partner_freq)) {
7712 			dbs_links |= BIT(link_id);
7713 			ath12k_dbg(ab, ATH12K_DBG_MAC, "new DBS link %u freq %u\n",
7714 				   link_id, partner_freq);
7715 			continue;
7716 		}
7717 
7718 		ath12k_dbg(ab, ATH12K_DBG_MAC, "non DBS/SBS link %u freq %u\n",
7719 			   link_id, partner_freq);
7720 	}
7721 
7722 	/* choose the first candidate no matter how many is in the list */
7723 	if (sbs_links)
7724 		link_id = __ffs(sbs_links);
7725 	else if (dbs_links)
7726 		link_id = __ffs(dbs_links);
7727 	else
7728 		link_id = ffs(useful_links) - 1;
7729 
7730 	ath12k_dbg(ab, ATH12K_DBG_MAC, "select partner link %u\n", link_id);
7731 
7732 	*selected_links = BIT(assoc_arvif->link_id) | BIT(link_id);
7733 
7734 	return 0;
7735 }
7736 
7737 int ath12k_mac_op_sta_state(struct ieee80211_hw *hw,
7738 			    struct ieee80211_vif *vif,
7739 			    struct ieee80211_sta *sta,
7740 			    enum ieee80211_sta_state old_state,
7741 			    enum ieee80211_sta_state new_state)
7742 {
7743 	struct ath12k_vif *ahvif = ath12k_vif_to_ahvif(vif);
7744 	struct ath12k_sta *ahsta = ath12k_sta_to_ahsta(sta);
7745 	struct ath12k_hw *ah = ath12k_hw_to_ah(hw);
7746 	struct ath12k_base *prev_ab = NULL, *ab;
7747 	struct ath12k_link_vif *arvif;
7748 	struct ath12k_link_sta *arsta;
7749 	unsigned long valid_links;
7750 	u16 selected_links = 0;
7751 	u8 link_id = 0, i;
7752 	struct ath12k *ar;
7753 	int ret = -EINVAL;
7754 	struct ath12k_dp_peer_create_params dp_params = {};
7755 
7756 	lockdep_assert_wiphy(hw->wiphy);
7757 
7758 	if (ieee80211_vif_is_mld(vif) && sta->valid_links) {
7759 		WARN_ON(!sta->mlo && hweight16(sta->valid_links) != 1);
7760 		link_id = ffs(sta->valid_links) - 1;
7761 	}
7762 
7763 	/* IEEE80211_STA_NOTEXIST -> IEEE80211_STA_NONE:
7764 	 * New station add received. If this is a ML station then
7765 	 * ahsta->links_map will be zero and sta->valid_links will be 1.
7766 	 * Assign default link to the first link sta.
7767 	 */
7768 	if (old_state == IEEE80211_STA_NOTEXIST &&
7769 	    new_state == IEEE80211_STA_NONE) {
7770 		memset(ahsta, 0, sizeof(*ahsta));
7771 		ahsta->free_logical_link_idx_map = U16_MAX;
7772 
7773 		arsta = &ahsta->deflink;
7774 
7775 		/* ML sta */
7776 		if (sta->mlo && !ahsta->links_map &&
7777 		    (hweight16(sta->valid_links) == 1)) {
7778 			if (ah->host_alloc_ml_id) {
7779 				ahsta->ml_peer_id = ath12k_peer_ml_alloc(ah);
7780 				if (ahsta->ml_peer_id == ATH12K_MLO_PEER_ID_INVALID) {
7781 					ath12k_hw_warn(ah, "unable to allocate ML peer id for sta %pM",
7782 						       sta->addr);
7783 					goto exit;
7784 				}
7785 			} else {
7786 				/*
7787 				 * firmware allocates the ML peer ID and notifies
7788 				 * the host via HTT_T2H_MSG_TYPE_MLO_RX_PEER_MAP
7789 				 */
7790 				ahsta->ml_peer_id = ATH12K_MLO_PEER_ID_PENDING;
7791 			}
7792 
7793 			dp_params.is_mlo = true;
7794 			dp_params.peer_id = ahsta->ml_peer_id;
7795 		}
7796 
7797 		dp_params.sta = sta;
7798 
7799 		if (vif->type == NL80211_IFTYPE_AP)
7800 			dp_params.ucast_ra_only = true;
7801 
7802 		ret = ath12k_dp_peer_create(&ah->dp_hw, sta->addr, &dp_params);
7803 		if (ret) {
7804 			ath12k_hw_warn(ah, "unable to create ath12k_dp_peer for sta %pM, ret: %d",
7805 				       sta->addr, ret);
7806 
7807 			goto ml_peer_id_clear;
7808 		}
7809 
7810 		ret = ath12k_mac_assign_link_sta(ah, ahsta, arsta, ahvif,
7811 						 link_id);
7812 		if (ret) {
7813 			ath12k_hw_warn(ah, "unable assign link %d for sta %pM",
7814 				       link_id, sta->addr);
7815 			goto peer_delete;
7816 		}
7817 
7818 		/* above arsta will get memset, hence do this after assign
7819 		 * link sta
7820 		 */
7821 		if (sta->mlo) {
7822 			/* For station mode, arvif->is_sta_assoc_link has been set when
7823 			 * vdev starts. Make sure the arvif/arsta pair have same setting
7824 			 */
7825 			if (vif->type == NL80211_IFTYPE_STATION &&
7826 			    !arsta->arvif->is_sta_assoc_link) {
7827 				ath12k_hw_warn(ah, "failed to verify assoc link setting with link id %u\n",
7828 					       link_id);
7829 				ret = -EINVAL;
7830 				goto exit;
7831 			}
7832 
7833 			arsta->is_assoc_link = true;
7834 			ahsta->assoc_link_id = link_id;
7835 		}
7836 	}
7837 
7838 	/* In the ML station scenario, activate all partner links once the
7839 	 * client is transitioning to the associated state.
7840 	 *
7841 	 * FIXME: Ideally, this activation should occur when the client
7842 	 * transitions to the authorized state. However, there are some
7843 	 * issues with handling this in the firmware. Until the firmware
7844 	 * can manage it properly, activate the links when the client is
7845 	 * about to move to the associated state.
7846 	 */
7847 	if (ieee80211_vif_is_mld(vif) && vif->type == NL80211_IFTYPE_STATION &&
7848 	    old_state == IEEE80211_STA_AUTH && new_state == IEEE80211_STA_ASSOC) {
7849 		/* TODO: for now only do link selection for single device
7850 		 * MLO case. Other cases would be handled in the future.
7851 		 */
7852 		ab = ah->radio[0].ab;
7853 		if (ab->ag->num_devices == 1) {
7854 			ret = ath12k_mac_select_links(ab, vif, hw, &selected_links);
7855 			if (ret) {
7856 				ath12k_warn(ab,
7857 					    "failed to get selected links: %d\n", ret);
7858 				goto exit;
7859 			}
7860 		} else {
7861 			selected_links = ieee80211_vif_usable_links(vif);
7862 		}
7863 
7864 		ieee80211_set_active_links(vif, selected_links);
7865 	}
7866 
7867 	/* Handle all the other state transitions in generic way */
7868 	valid_links = ahsta->links_map;
7869 	for_each_set_bit(link_id, &valid_links, IEEE80211_MLD_MAX_NUM_LINKS) {
7870 		arvif = wiphy_dereference(hw->wiphy, ahvif->link[link_id]);
7871 		arsta = wiphy_dereference(hw->wiphy, ahsta->link[link_id]);
7872 		/* some assumptions went wrong! */
7873 		if (WARN_ON(!arvif || !arsta))
7874 			continue;
7875 
7876 		/* vdev might be in deleted */
7877 		if (WARN_ON(!arvif->ar))
7878 			continue;
7879 
7880 		ret = ath12k_mac_handle_link_sta_state(hw, arvif, arsta,
7881 						       old_state, new_state);
7882 		if (ret) {
7883 			ath12k_hw_warn(ah, "unable to move link sta %d of sta %pM from state %d to %d",
7884 				       link_id, arsta->addr, old_state, new_state);
7885 
7886 			if (old_state == IEEE80211_STA_NOTEXIST &&
7887 			    new_state == IEEE80211_STA_NONE)
7888 				goto peer_delete;
7889 			else
7890 				goto exit;
7891 		}
7892 	}
7893 
7894 	if (ieee80211_vif_is_mld(vif) && vif->type == NL80211_IFTYPE_STATION &&
7895 	    old_state == IEEE80211_STA_ASSOC && new_state == IEEE80211_STA_AUTHORIZED) {
7896 		for_each_ar(ah, ar, i) {
7897 			ab = ar->ab;
7898 			if (prev_ab == ab)
7899 				continue;
7900 
7901 			ret = ath12k_mac_mlo_sta_update_link_active(ab, hw, ahvif);
7902 			if (ret) {
7903 				ath12k_warn(ab,
7904 					    "failed to update link active state on connect %d\n",
7905 					    ret);
7906 				goto exit;
7907 			}
7908 
7909 			prev_ab = ab;
7910 		}
7911 	}
7912 	/* IEEE80211_STA_NONE -> IEEE80211_STA_NOTEXIST:
7913 	 * Remove the station from driver (handle ML sta here since that
7914 	 * needs special handling. Normal sta will be handled in generic
7915 	 * handler below
7916 	 */
7917 	if (old_state == IEEE80211_STA_NONE &&
7918 	    new_state == IEEE80211_STA_NOTEXIST) {
7919 		if (sta->mlo)
7920 			ath12k_mac_ml_station_remove(ahvif, ahsta);
7921 
7922 		ath12k_dp_peer_delete(&ah->dp_hw, sta->addr, sta);
7923 	}
7924 
7925 	ret = 0;
7926 	goto exit;
7927 
7928 peer_delete:
7929 	ath12k_dp_peer_delete(&ah->dp_hw, sta->addr, sta);
7930 ml_peer_id_clear:
7931 	if (sta->mlo)
7932 		ath12k_peer_ml_free(ah, ahsta);
7933 exit:
7934 	/* update the state if everything went well */
7935 	if (!ret)
7936 		ahsta->state = new_state;
7937 
7938 	return ret;
7939 }
7940 EXPORT_SYMBOL(ath12k_mac_op_sta_state);
7941 
7942 int ath12k_mac_op_sta_set_txpwr(struct ieee80211_hw *hw,
7943 				struct ieee80211_vif *vif,
7944 				struct ieee80211_sta *sta)
7945 {
7946 	struct ath12k_sta *ahsta = ath12k_sta_to_ahsta(sta);
7947 	struct ath12k *ar;
7948 	struct ath12k_vif *ahvif = ath12k_vif_to_ahvif(vif);
7949 	struct ath12k_link_vif *arvif;
7950 	struct ath12k_link_sta *arsta;
7951 	u8 link_id;
7952 	int ret;
7953 	s16 txpwr;
7954 
7955 	lockdep_assert_wiphy(hw->wiphy);
7956 
7957 	/* TODO: use link id from mac80211 once that's implemented */
7958 	link_id = 0;
7959 
7960 	arvif = wiphy_dereference(hw->wiphy, ahvif->link[link_id]);
7961 	arsta = wiphy_dereference(hw->wiphy, ahsta->link[link_id]);
7962 
7963 	if (sta->deflink.txpwr.type == NL80211_TX_POWER_AUTOMATIC) {
7964 		txpwr = 0;
7965 	} else {
7966 		txpwr = sta->deflink.txpwr.power;
7967 		if (!txpwr) {
7968 			ret = -EINVAL;
7969 			goto out;
7970 		}
7971 	}
7972 
7973 	if (txpwr > ATH12K_TX_POWER_MAX_VAL || txpwr < ATH12K_TX_POWER_MIN_VAL) {
7974 		ret = -EINVAL;
7975 		goto out;
7976 	}
7977 
7978 	ar = arvif->ar;
7979 
7980 	ret = ath12k_wmi_set_peer_param(ar, arsta->addr, arvif->vdev_id,
7981 					WMI_PEER_USE_FIXED_PWR, txpwr);
7982 	if (ret) {
7983 		ath12k_warn(ar->ab, "failed to set tx power for station ret: %d\n",
7984 			    ret);
7985 		goto out;
7986 	}
7987 
7988 out:
7989 	return ret;
7990 }
7991 EXPORT_SYMBOL(ath12k_mac_op_sta_set_txpwr);
7992 
7993 void ath12k_mac_op_sta_set_4addr(struct ieee80211_hw *hw,
7994 				 struct ieee80211_vif *vif,
7995 				 struct ieee80211_sta *sta, bool enabled)
7996 {
7997 	struct ath12k_sta *ahsta = ath12k_sta_to_ahsta(sta);
7998 
7999 	lockdep_assert_wiphy(hw->wiphy);
8000 
8001 	/*
8002 	 * 4-address mode disabled option is available only for station
8003 	 * interface from mac80211, and we have wds_vdev_param for station
8004 	 * interface and target will not allow to disable the wds_vdev_param
8005 	 * during run time. So, add support only for enable case, for
8006 	 * disable case station interface needs to be reconnect.
8007 	 */
8008 	if (enabled && !ahsta->enable_4addr) {
8009 		if (!ath12k_mac_sta_set_4addr(hw->wiphy, ahsta))
8010 			ahsta->enable_4addr = true;
8011 	}
8012 }
8013 EXPORT_SYMBOL(ath12k_mac_op_sta_set_4addr);
8014 
8015 void ath12k_mac_op_link_sta_rc_update(struct ieee80211_hw *hw,
8016 				      struct ieee80211_vif *vif,
8017 				      struct ieee80211_link_sta *link_sta,
8018 				      u32 changed)
8019 {
8020 	struct ieee80211_sta *sta = link_sta->sta;
8021 	struct ath12k *ar;
8022 	struct ath12k_sta *ahsta = ath12k_sta_to_ahsta(sta);
8023 	struct ath12k_vif *ahvif = ath12k_vif_to_ahvif(vif);
8024 	struct ath12k_hw *ah = ath12k_hw_to_ah(hw);
8025 	struct ath12k_link_sta *arsta;
8026 	struct ath12k_link_vif *arvif;
8027 	struct ath12k_dp_link_peer *peer;
8028 	u32 bw, smps;
8029 	struct ath12k_dp *dp;
8030 
8031 	rcu_read_lock();
8032 	arvif = rcu_dereference(ahvif->link[link_sta->link_id]);
8033 	if (!arvif) {
8034 		ath12k_hw_warn(ah, "mac sta rc update failed to fetch link vif on link id %u for peer %pM\n",
8035 			       link_sta->link_id, sta->addr);
8036 		rcu_read_unlock();
8037 		return;
8038 	}
8039 
8040 	ar = arvif->ar;
8041 	dp = ath12k_ab_to_dp(ar->ab);
8042 
8043 	arsta = rcu_dereference(ahsta->link[link_sta->link_id]);
8044 	if (!arsta) {
8045 		rcu_read_unlock();
8046 		ath12k_warn(ar->ab, "mac sta rc update failed to fetch link sta on link id %u for peer %pM\n",
8047 			    link_sta->link_id, sta->addr);
8048 		return;
8049 	}
8050 	spin_lock_bh(&dp->dp_lock);
8051 
8052 	peer = ath12k_dp_link_peer_find_by_vdev_and_addr(dp, arvif->vdev_id,
8053 							 arsta->addr);
8054 	if (!peer) {
8055 		spin_unlock_bh(&dp->dp_lock);
8056 		rcu_read_unlock();
8057 		ath12k_warn(ar->ab, "mac sta rc update failed to find peer %pM on vdev %i\n",
8058 			    arsta->addr, arvif->vdev_id);
8059 		return;
8060 	}
8061 
8062 	spin_unlock_bh(&dp->dp_lock);
8063 
8064 	if (arsta->link_id >= IEEE80211_MLD_MAX_NUM_LINKS) {
8065 		rcu_read_unlock();
8066 		return;
8067 	}
8068 
8069 	link_sta = rcu_dereference(sta->link[arsta->link_id]);
8070 	if (!link_sta) {
8071 		rcu_read_unlock();
8072 		ath12k_warn(ar->ab, "unable to access link sta in rc update for sta %pM link %u\n",
8073 			    sta->addr, arsta->link_id);
8074 		return;
8075 	}
8076 
8077 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
8078 		   "mac sta rc update for %pM changed %08x bw %d nss %d smps %d\n",
8079 		   arsta->addr, changed, link_sta->bandwidth, link_sta->rx_nss,
8080 		   link_sta->smps_mode);
8081 
8082 	spin_lock_bh(&ar->data_lock);
8083 
8084 	if (changed & IEEE80211_RC_BW_CHANGED) {
8085 		bw = ath12k_mac_ieee80211_sta_bw_to_wmi(ar, link_sta);
8086 		arsta->bw_prev = arsta->bw;
8087 		arsta->bw = bw;
8088 	}
8089 
8090 	if (changed & IEEE80211_RC_NSS_CHANGED)
8091 		arsta->nss = link_sta->rx_nss;
8092 
8093 	if (changed & IEEE80211_RC_SMPS_CHANGED) {
8094 		smps = WMI_PEER_SMPS_PS_NONE;
8095 
8096 		switch (link_sta->smps_mode) {
8097 		case IEEE80211_SMPS_AUTOMATIC:
8098 		case IEEE80211_SMPS_OFF:
8099 			smps = WMI_PEER_SMPS_PS_NONE;
8100 			break;
8101 		case IEEE80211_SMPS_STATIC:
8102 			smps = WMI_PEER_SMPS_STATIC;
8103 			break;
8104 		case IEEE80211_SMPS_DYNAMIC:
8105 			smps = WMI_PEER_SMPS_DYNAMIC;
8106 			break;
8107 		default:
8108 			ath12k_warn(ar->ab, "Invalid smps %d in sta rc update for %pM link %u\n",
8109 				    link_sta->smps_mode, arsta->addr, link_sta->link_id);
8110 			smps = WMI_PEER_SMPS_PS_NONE;
8111 			break;
8112 		}
8113 
8114 		arsta->smps = smps;
8115 	}
8116 
8117 	arsta->changed |= changed;
8118 
8119 	spin_unlock_bh(&ar->data_lock);
8120 
8121 	wiphy_work_queue(hw->wiphy, &arsta->update_wk);
8122 
8123 	rcu_read_unlock();
8124 }
8125 EXPORT_SYMBOL(ath12k_mac_op_link_sta_rc_update);
8126 
8127 static struct ath12k_link_sta *ath12k_mac_alloc_assign_link_sta(struct ath12k_hw *ah,
8128 								struct ath12k_sta *ahsta,
8129 								struct ath12k_vif *ahvif,
8130 								u8 link_id)
8131 {
8132 	struct ath12k_link_sta *arsta;
8133 	int ret;
8134 
8135 	lockdep_assert_wiphy(ah->hw->wiphy);
8136 
8137 	if (link_id >= IEEE80211_MLD_MAX_NUM_LINKS)
8138 		return NULL;
8139 
8140 	arsta = wiphy_dereference(ah->hw->wiphy, ahsta->link[link_id]);
8141 	if (arsta)
8142 		return NULL;
8143 
8144 	arsta = kmalloc_obj(*arsta);
8145 	if (!arsta)
8146 		return NULL;
8147 
8148 	ret = ath12k_mac_assign_link_sta(ah, ahsta, arsta, ahvif, link_id);
8149 	if (ret) {
8150 		kfree(arsta);
8151 		return NULL;
8152 	}
8153 
8154 	return arsta;
8155 }
8156 
8157 int ath12k_mac_op_change_sta_links(struct ieee80211_hw *hw,
8158 				   struct ieee80211_vif *vif,
8159 				   struct ieee80211_sta *sta,
8160 				   u16 old_links, u16 new_links)
8161 {
8162 	struct ath12k_vif *ahvif = ath12k_vif_to_ahvif(vif);
8163 	struct ath12k_sta *ahsta = ath12k_sta_to_ahsta(sta);
8164 	struct ath12k_hw *ah = hw->priv;
8165 	struct ath12k_link_vif *arvif;
8166 	struct ath12k_link_sta *arsta;
8167 	unsigned long valid_links;
8168 	struct ath12k *ar;
8169 	u8 link_id;
8170 	int ret;
8171 
8172 	lockdep_assert_wiphy(hw->wiphy);
8173 
8174 	if (!sta->valid_links)
8175 		return -EINVAL;
8176 
8177 	/* Firmware does not support removal of one of link stas. All sta
8178 	 * would be removed during ML STA delete in sta_state(), hence link
8179 	 * sta removal is not handled here.
8180 	 */
8181 	if (new_links < old_links)
8182 		return 0;
8183 
8184 	if (ahsta->ml_peer_id == ATH12K_MLO_PEER_ID_INVALID) {
8185 		ath12k_hw_warn(ah, "unable to add link for ml sta %pM", sta->addr);
8186 		return -EINVAL;
8187 	}
8188 
8189 	/* this op is expected only after initial sta insertion with default link */
8190 	if (WARN_ON(ahsta->links_map == 0))
8191 		return -EINVAL;
8192 
8193 	valid_links = new_links;
8194 	for_each_set_bit(link_id, &valid_links, IEEE80211_MLD_MAX_NUM_LINKS) {
8195 		if (ahsta->links_map & BIT(link_id))
8196 			continue;
8197 
8198 		arvif = wiphy_dereference(hw->wiphy, ahvif->link[link_id]);
8199 		if (!arvif || !arvif->is_created)
8200 			continue;
8201 
8202 		arsta = ath12k_mac_alloc_assign_link_sta(ah, ahsta, ahvif, link_id);
8203 		if (!arsta) {
8204 			ath12k_hw_warn(ah, "Failed to alloc/assign link sta");
8205 			continue;
8206 		}
8207 
8208 		ar = arvif->ar;
8209 
8210 		ret = ath12k_mac_station_add(ar, arvif, arsta);
8211 		if (ret) {
8212 			ath12k_warn(ar->ab, "Failed to add station: %pM for VDEV: %d\n",
8213 				    arsta->addr, arvif->vdev_id);
8214 			ath12k_mac_free_unassign_link_sta(ah, ahsta, link_id);
8215 			return ret;
8216 		}
8217 	}
8218 
8219 	return 0;
8220 }
8221 EXPORT_SYMBOL(ath12k_mac_op_change_sta_links);
8222 
8223 bool ath12k_mac_op_can_activate_links(struct ieee80211_hw *hw,
8224 				      struct ieee80211_vif *vif,
8225 				      u16 active_links)
8226 {
8227 	/* TODO: Handle recovery case */
8228 
8229 	return true;
8230 }
8231 EXPORT_SYMBOL(ath12k_mac_op_can_activate_links);
8232 
8233 static int ath12k_conf_tx_uapsd(struct ath12k_link_vif *arvif,
8234 				u16 ac, bool enable)
8235 {
8236 	struct ath12k *ar = arvif->ar;
8237 	struct ath12k_vif *ahvif = arvif->ahvif;
8238 	u32 value;
8239 	int ret;
8240 
8241 	if (ahvif->vdev_type != WMI_VDEV_TYPE_STA)
8242 		return 0;
8243 
8244 	switch (ac) {
8245 	case IEEE80211_AC_VO:
8246 		value = WMI_STA_PS_UAPSD_AC3_DELIVERY_EN |
8247 			WMI_STA_PS_UAPSD_AC3_TRIGGER_EN;
8248 		break;
8249 	case IEEE80211_AC_VI:
8250 		value = WMI_STA_PS_UAPSD_AC2_DELIVERY_EN |
8251 			WMI_STA_PS_UAPSD_AC2_TRIGGER_EN;
8252 		break;
8253 	case IEEE80211_AC_BE:
8254 		value = WMI_STA_PS_UAPSD_AC1_DELIVERY_EN |
8255 			WMI_STA_PS_UAPSD_AC1_TRIGGER_EN;
8256 		break;
8257 	case IEEE80211_AC_BK:
8258 		value = WMI_STA_PS_UAPSD_AC0_DELIVERY_EN |
8259 			WMI_STA_PS_UAPSD_AC0_TRIGGER_EN;
8260 		break;
8261 	}
8262 
8263 	if (enable)
8264 		ahvif->u.sta.uapsd |= value;
8265 	else
8266 		ahvif->u.sta.uapsd &= ~value;
8267 
8268 	ret = ath12k_wmi_set_sta_ps_param(ar, arvif->vdev_id,
8269 					  WMI_STA_PS_PARAM_UAPSD,
8270 					  ahvif->u.sta.uapsd);
8271 	if (ret) {
8272 		ath12k_warn(ar->ab, "could not set uapsd params %d\n", ret);
8273 		goto exit;
8274 	}
8275 
8276 	if (ahvif->u.sta.uapsd)
8277 		value = WMI_STA_PS_RX_WAKE_POLICY_POLL_UAPSD;
8278 	else
8279 		value = WMI_STA_PS_RX_WAKE_POLICY_WAKE;
8280 
8281 	ret = ath12k_wmi_set_sta_ps_param(ar, arvif->vdev_id,
8282 					  WMI_STA_PS_PARAM_RX_WAKE_POLICY,
8283 					  value);
8284 	if (ret)
8285 		ath12k_warn(ar->ab, "could not set rx wake param %d\n", ret);
8286 
8287 exit:
8288 	return ret;
8289 }
8290 
8291 static int ath12k_mac_conf_tx(struct ath12k_link_vif *arvif, u16 ac,
8292 			      const struct ieee80211_tx_queue_params *params)
8293 {
8294 	struct wmi_wmm_params_arg *p = NULL;
8295 	struct ath12k *ar = arvif->ar;
8296 	struct ath12k_base *ab = ar->ab;
8297 	int ret;
8298 
8299 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
8300 
8301 	switch (ac) {
8302 	case IEEE80211_AC_VO:
8303 		p = &arvif->wmm_params.ac_vo;
8304 		break;
8305 	case IEEE80211_AC_VI:
8306 		p = &arvif->wmm_params.ac_vi;
8307 		break;
8308 	case IEEE80211_AC_BE:
8309 		p = &arvif->wmm_params.ac_be;
8310 		break;
8311 	case IEEE80211_AC_BK:
8312 		p = &arvif->wmm_params.ac_bk;
8313 		break;
8314 	}
8315 
8316 	if (WARN_ON(!p)) {
8317 		ret = -EINVAL;
8318 		goto exit;
8319 	}
8320 
8321 	p->cwmin = params->cw_min;
8322 	p->cwmax = params->cw_max;
8323 	p->aifs = params->aifs;
8324 	p->txop = params->txop;
8325 
8326 	ret = ath12k_wmi_send_wmm_update_cmd(ar, arvif->vdev_id,
8327 					     &arvif->wmm_params);
8328 	if (ret) {
8329 		ath12k_warn(ab, "pdev idx %d failed to set wmm params: %d\n",
8330 			    ar->pdev_idx, ret);
8331 		goto exit;
8332 	}
8333 
8334 	ret = ath12k_conf_tx_uapsd(arvif, ac, params->uapsd);
8335 	if (ret)
8336 		ath12k_warn(ab, "pdev idx %d failed to set sta uapsd: %d\n",
8337 			    ar->pdev_idx, ret);
8338 
8339 exit:
8340 	return ret;
8341 }
8342 
8343 int ath12k_mac_op_conf_tx(struct ieee80211_hw *hw,
8344 			  struct ieee80211_vif *vif,
8345 			  unsigned int link_id, u16 ac,
8346 			  const struct ieee80211_tx_queue_params *params)
8347 {
8348 	struct ath12k_vif *ahvif = ath12k_vif_to_ahvif(vif);
8349 	struct ath12k_link_vif *arvif;
8350 	struct ath12k_vif_cache *cache;
8351 	int ret;
8352 
8353 	lockdep_assert_wiphy(hw->wiphy);
8354 
8355 	if (link_id >= IEEE80211_MLD_MAX_NUM_LINKS)
8356 		return -EINVAL;
8357 
8358 	arvif = wiphy_dereference(hw->wiphy, ahvif->link[link_id]);
8359 	if (!arvif || !arvif->is_created) {
8360 		cache = ath12k_ahvif_get_link_cache(ahvif, link_id);
8361 		if (!cache)
8362 			return -ENOSPC;
8363 
8364 		cache->tx_conf.changed = true;
8365 		cache->tx_conf.ac = ac;
8366 		cache->tx_conf.tx_queue_params = *params;
8367 
8368 		return 0;
8369 	}
8370 
8371 	ret = ath12k_mac_conf_tx(arvif, ac, params);
8372 
8373 	return ret;
8374 }
8375 EXPORT_SYMBOL(ath12k_mac_op_conf_tx);
8376 
8377 static struct ieee80211_sta_ht_cap
8378 ath12k_create_ht_cap(struct ath12k *ar, u32 ar_ht_cap, u32 rate_cap_rx_chainmask)
8379 {
8380 	int i;
8381 	struct ieee80211_sta_ht_cap ht_cap = {};
8382 	u32 ar_vht_cap = ar->pdev->cap.vht_cap;
8383 
8384 	if (!(ar_ht_cap & WMI_HT_CAP_ENABLED))
8385 		return ht_cap;
8386 
8387 	ht_cap.ht_supported = 1;
8388 	ht_cap.ampdu_factor = IEEE80211_HT_MAX_AMPDU_64K;
8389 	ht_cap.ampdu_density = IEEE80211_HT_MPDU_DENSITY_NONE;
8390 	ht_cap.cap |= IEEE80211_HT_CAP_SUP_WIDTH_20_40;
8391 	ht_cap.cap |= IEEE80211_HT_CAP_DSSSCCK40;
8392 	ht_cap.cap |= WLAN_HT_CAP_SM_PS_STATIC << IEEE80211_HT_CAP_SM_PS_SHIFT;
8393 
8394 	if (ar_ht_cap & WMI_HT_CAP_HT20_SGI)
8395 		ht_cap.cap |= IEEE80211_HT_CAP_SGI_20;
8396 
8397 	if (ar_ht_cap & WMI_HT_CAP_HT40_SGI)
8398 		ht_cap.cap |= IEEE80211_HT_CAP_SGI_40;
8399 
8400 	if (ar_ht_cap & WMI_HT_CAP_DYNAMIC_SMPS) {
8401 		u32 smps;
8402 
8403 		smps   = WLAN_HT_CAP_SM_PS_DYNAMIC;
8404 		smps <<= IEEE80211_HT_CAP_SM_PS_SHIFT;
8405 
8406 		ht_cap.cap |= smps;
8407 	}
8408 
8409 	if (ar_ht_cap & WMI_HT_CAP_TX_STBC)
8410 		ht_cap.cap |= IEEE80211_HT_CAP_TX_STBC;
8411 
8412 	if (ar_ht_cap & WMI_HT_CAP_RX_STBC) {
8413 		u32 stbc;
8414 
8415 		stbc   = ar_ht_cap;
8416 		stbc  &= WMI_HT_CAP_RX_STBC;
8417 		stbc >>= WMI_HT_CAP_RX_STBC_MASK_SHIFT;
8418 		stbc <<= IEEE80211_HT_CAP_RX_STBC_SHIFT;
8419 		stbc  &= IEEE80211_HT_CAP_RX_STBC;
8420 
8421 		ht_cap.cap |= stbc;
8422 	}
8423 
8424 	if (ar_ht_cap & WMI_HT_CAP_RX_LDPC)
8425 		ht_cap.cap |= IEEE80211_HT_CAP_LDPC_CODING;
8426 
8427 	if (ar_ht_cap & WMI_HT_CAP_L_SIG_TXOP_PROT)
8428 		ht_cap.cap |= IEEE80211_HT_CAP_LSIG_TXOP_PROT;
8429 
8430 	if (ar_vht_cap & WMI_VHT_CAP_MAX_MPDU_LEN_MASK)
8431 		ht_cap.cap |= IEEE80211_HT_CAP_MAX_AMSDU;
8432 
8433 	for (i = 0; i < ar->num_rx_chains; i++) {
8434 		if (rate_cap_rx_chainmask & BIT(i))
8435 			ht_cap.mcs.rx_mask[i] = 0xFF;
8436 	}
8437 
8438 	ht_cap.mcs.tx_params |= IEEE80211_HT_MCS_TX_DEFINED;
8439 
8440 	return ht_cap;
8441 }
8442 
8443 static int ath12k_mac_set_txbf_conf(struct ath12k_link_vif *arvif)
8444 {
8445 	u32 value = 0;
8446 	struct ath12k *ar = arvif->ar;
8447 	struct ath12k_vif *ahvif = arvif->ahvif;
8448 	int nsts;
8449 	int sound_dim;
8450 	u32 vht_cap = ar->pdev->cap.vht_cap;
8451 	u32 vdev_param = WMI_VDEV_PARAM_TXBF;
8452 
8453 	if (vht_cap & (IEEE80211_VHT_CAP_SU_BEAMFORMEE_CAPABLE)) {
8454 		nsts = vht_cap & IEEE80211_VHT_CAP_BEAMFORMEE_STS_MASK;
8455 		nsts >>= IEEE80211_VHT_CAP_BEAMFORMEE_STS_SHIFT;
8456 		value |= SM(nsts, WMI_TXBF_STS_CAP_OFFSET);
8457 	}
8458 
8459 	if (vht_cap & (IEEE80211_VHT_CAP_SU_BEAMFORMER_CAPABLE)) {
8460 		sound_dim = vht_cap &
8461 			    IEEE80211_VHT_CAP_SOUNDING_DIMENSIONS_MASK;
8462 		sound_dim >>= IEEE80211_VHT_CAP_SOUNDING_DIMENSIONS_SHIFT;
8463 		if (sound_dim > (ar->num_tx_chains - 1))
8464 			sound_dim = ar->num_tx_chains - 1;
8465 		value |= SM(sound_dim, WMI_BF_SOUND_DIM_OFFSET);
8466 	}
8467 
8468 	if (!value)
8469 		return 0;
8470 
8471 	if (vht_cap & IEEE80211_VHT_CAP_SU_BEAMFORMER_CAPABLE) {
8472 		value |= WMI_VDEV_PARAM_TXBF_SU_TX_BFER;
8473 
8474 		if ((vht_cap & IEEE80211_VHT_CAP_MU_BEAMFORMER_CAPABLE) &&
8475 		    ahvif->vdev_type == WMI_VDEV_TYPE_AP)
8476 			value |= WMI_VDEV_PARAM_TXBF_MU_TX_BFER;
8477 	}
8478 
8479 	if (vht_cap & IEEE80211_VHT_CAP_SU_BEAMFORMEE_CAPABLE) {
8480 		value |= WMI_VDEV_PARAM_TXBF_SU_TX_BFEE;
8481 
8482 		if ((vht_cap & IEEE80211_VHT_CAP_MU_BEAMFORMEE_CAPABLE) &&
8483 		    ahvif->vdev_type == WMI_VDEV_TYPE_STA)
8484 			value |= WMI_VDEV_PARAM_TXBF_MU_TX_BFEE;
8485 	}
8486 
8487 	return ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
8488 					     vdev_param, value);
8489 }
8490 
8491 static void ath12k_set_vht_txbf_cap(struct ath12k *ar, u32 *vht_cap)
8492 {
8493 	bool subfer, subfee;
8494 	int sound_dim = 0;
8495 
8496 	subfer = !!(*vht_cap & (IEEE80211_VHT_CAP_SU_BEAMFORMER_CAPABLE));
8497 	subfee = !!(*vht_cap & (IEEE80211_VHT_CAP_SU_BEAMFORMEE_CAPABLE));
8498 
8499 	if (ar->num_tx_chains < 2) {
8500 		*vht_cap &= ~(IEEE80211_VHT_CAP_SU_BEAMFORMER_CAPABLE);
8501 		subfer = false;
8502 	}
8503 
8504 	/* If SU Beaformer is not set, then disable MU Beamformer Capability */
8505 	if (!subfer)
8506 		*vht_cap &= ~(IEEE80211_VHT_CAP_MU_BEAMFORMER_CAPABLE);
8507 
8508 	/* If SU Beaformee is not set, then disable MU Beamformee Capability */
8509 	if (!subfee)
8510 		*vht_cap &= ~(IEEE80211_VHT_CAP_MU_BEAMFORMEE_CAPABLE);
8511 
8512 	sound_dim = u32_get_bits(*vht_cap,
8513 				 IEEE80211_VHT_CAP_SOUNDING_DIMENSIONS_MASK);
8514 	*vht_cap = u32_replace_bits(*vht_cap, 0,
8515 				    IEEE80211_VHT_CAP_SOUNDING_DIMENSIONS_MASK);
8516 
8517 	/* TODO: Need to check invalid STS and Sound_dim values set by FW? */
8518 
8519 	/* Enable Sounding Dimension Field only if SU BF is enabled */
8520 	if (subfer) {
8521 		if (sound_dim > (ar->num_tx_chains - 1))
8522 			sound_dim = ar->num_tx_chains - 1;
8523 
8524 		*vht_cap = u32_replace_bits(*vht_cap, sound_dim,
8525 					    IEEE80211_VHT_CAP_SOUNDING_DIMENSIONS_MASK);
8526 	}
8527 
8528 	/* Use the STS advertised by FW unless SU Beamformee is not supported*/
8529 	if (!subfee)
8530 		*vht_cap &= ~(IEEE80211_VHT_CAP_BEAMFORMEE_STS_MASK);
8531 }
8532 
8533 static struct ieee80211_sta_vht_cap
8534 ath12k_create_vht_cap(struct ath12k *ar, u32 rate_cap_tx_chainmask,
8535 		      u32 rate_cap_rx_chainmask)
8536 {
8537 	struct ieee80211_sta_vht_cap vht_cap = {};
8538 	u16 txmcs_map, rxmcs_map;
8539 	int i;
8540 
8541 	vht_cap.vht_supported = 1;
8542 	vht_cap.cap = ar->pdev->cap.vht_cap;
8543 
8544 	if (ar->pdev->cap.nss_ratio_enabled)
8545 		vht_cap.vht_mcs.tx_highest |=
8546 			cpu_to_le16(IEEE80211_VHT_EXT_NSS_BW_CAPABLE);
8547 
8548 	ath12k_set_vht_txbf_cap(ar, &vht_cap.cap);
8549 
8550 	/* 80P80 is not supported */
8551 	vht_cap.cap &= ~IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_160_80PLUS80MHZ;
8552 
8553 	rxmcs_map = 0;
8554 	txmcs_map = 0;
8555 	for (i = 0; i < 8; i++) {
8556 		if (i < ar->num_tx_chains && rate_cap_tx_chainmask & BIT(i))
8557 			txmcs_map |= IEEE80211_VHT_MCS_SUPPORT_0_9 << (i * 2);
8558 		else
8559 			txmcs_map |= IEEE80211_VHT_MCS_NOT_SUPPORTED << (i * 2);
8560 
8561 		if (i < ar->num_rx_chains && rate_cap_rx_chainmask & BIT(i))
8562 			rxmcs_map |= IEEE80211_VHT_MCS_SUPPORT_0_9 << (i * 2);
8563 		else
8564 			rxmcs_map |= IEEE80211_VHT_MCS_NOT_SUPPORTED << (i * 2);
8565 	}
8566 
8567 	if (rate_cap_tx_chainmask <= 1)
8568 		vht_cap.cap &= ~IEEE80211_VHT_CAP_TXSTBC;
8569 
8570 	vht_cap.vht_mcs.rx_mcs_map = cpu_to_le16(rxmcs_map);
8571 	vht_cap.vht_mcs.tx_mcs_map = cpu_to_le16(txmcs_map);
8572 
8573 	/* Check if the HW supports 1:1 NSS ratio and reset
8574 	 * EXT NSS BW Support field to 0 to indicate 1:1 ratio
8575 	 */
8576 	if (ar->pdev->cap.nss_ratio_info == WMI_NSS_RATIO_1_NSS)
8577 		vht_cap.cap &= ~IEEE80211_VHT_CAP_EXT_NSS_BW_MASK;
8578 
8579 	return vht_cap;
8580 }
8581 
8582 static void ath12k_mac_setup_ht_vht_cap(struct ath12k *ar,
8583 					struct ath12k_pdev_cap *cap,
8584 					u32 *ht_cap_info)
8585 {
8586 	struct ieee80211_supported_band *band;
8587 	u32 rate_cap_tx_chainmask;
8588 	u32 rate_cap_rx_chainmask;
8589 	u32 ht_cap;
8590 
8591 	rate_cap_tx_chainmask = ar->cfg_tx_chainmask >> cap->tx_chain_mask_shift;
8592 	rate_cap_rx_chainmask = ar->cfg_rx_chainmask >> cap->rx_chain_mask_shift;
8593 
8594 	if (cap->supported_bands & WMI_HOST_WLAN_2GHZ_CAP) {
8595 		band = &ar->mac.sbands[NL80211_BAND_2GHZ];
8596 		ht_cap = cap->band[NL80211_BAND_2GHZ].ht_cap_info;
8597 		if (ht_cap_info)
8598 			*ht_cap_info = ht_cap;
8599 		band->ht_cap = ath12k_create_ht_cap(ar, ht_cap,
8600 						    rate_cap_rx_chainmask);
8601 	}
8602 
8603 	if (cap->supported_bands & WMI_HOST_WLAN_5GHZ_CAP &&
8604 	    (ar->ab->hw_params->single_pdev_only ||
8605 	     !ar->supports_6ghz)) {
8606 		band = &ar->mac.sbands[NL80211_BAND_5GHZ];
8607 		ht_cap = cap->band[NL80211_BAND_5GHZ].ht_cap_info;
8608 		if (ht_cap_info)
8609 			*ht_cap_info = ht_cap;
8610 		band->ht_cap = ath12k_create_ht_cap(ar, ht_cap,
8611 						    rate_cap_rx_chainmask);
8612 		band->vht_cap = ath12k_create_vht_cap(ar, rate_cap_tx_chainmask,
8613 						      rate_cap_rx_chainmask);
8614 	}
8615 }
8616 
8617 static int ath12k_check_chain_mask(struct ath12k *ar, u32 ant, bool is_tx_ant)
8618 {
8619 	/* TODO: Check the request chainmask against the supported
8620 	 * chainmask table which is advertised in extented_service_ready event
8621 	 */
8622 
8623 	return 0;
8624 }
8625 
8626 static void ath12k_gen_ppe_thresh(struct ath12k_wmi_ppe_threshold_arg *fw_ppet,
8627 				  u8 *he_ppet)
8628 {
8629 	int nss, ru;
8630 	u8 bit = 7;
8631 
8632 	he_ppet[0] = fw_ppet->numss_m1 & IEEE80211_PPE_THRES_NSS_MASK;
8633 	he_ppet[0] |= (fw_ppet->ru_bit_mask <<
8634 		       IEEE80211_PPE_THRES_RU_INDEX_BITMASK_POS) &
8635 		      IEEE80211_PPE_THRES_RU_INDEX_BITMASK_MASK;
8636 	for (nss = 0; nss <= fw_ppet->numss_m1; nss++) {
8637 		for (ru = 0; ru < 4; ru++) {
8638 			u8 val;
8639 			int i;
8640 
8641 			if ((fw_ppet->ru_bit_mask & BIT(ru)) == 0)
8642 				continue;
8643 			val = (fw_ppet->ppet16_ppet8_ru3_ru0[nss] >> (ru * 6)) &
8644 			       0x3f;
8645 			val = ((val >> 3) & 0x7) | ((val & 0x7) << 3);
8646 			for (i = 5; i >= 0; i--) {
8647 				he_ppet[bit / 8] |=
8648 					((val >> i) & 0x1) << ((bit % 8));
8649 				bit++;
8650 			}
8651 		}
8652 	}
8653 }
8654 
8655 static void
8656 ath12k_mac_filter_he_cap_mesh(struct ieee80211_he_cap_elem *he_cap_elem)
8657 {
8658 	u8 m;
8659 
8660 	m = IEEE80211_HE_MAC_CAP0_TWT_RES |
8661 	    IEEE80211_HE_MAC_CAP0_TWT_REQ;
8662 	he_cap_elem->mac_cap_info[0] &= ~m;
8663 
8664 	m = IEEE80211_HE_MAC_CAP2_TRS |
8665 	    IEEE80211_HE_MAC_CAP2_BCAST_TWT |
8666 	    IEEE80211_HE_MAC_CAP2_MU_CASCADING;
8667 	he_cap_elem->mac_cap_info[2] &= ~m;
8668 
8669 	m = IEEE80211_HE_MAC_CAP3_FLEX_TWT_SCHED |
8670 	    IEEE80211_HE_MAC_CAP2_BCAST_TWT |
8671 	    IEEE80211_HE_MAC_CAP2_MU_CASCADING;
8672 	he_cap_elem->mac_cap_info[3] &= ~m;
8673 
8674 	m = IEEE80211_HE_MAC_CAP4_BSRP_BQRP_A_MPDU_AGG |
8675 	    IEEE80211_HE_MAC_CAP4_BQR;
8676 	he_cap_elem->mac_cap_info[4] &= ~m;
8677 
8678 	m = IEEE80211_HE_MAC_CAP5_SUBCHAN_SELECTIVE_TRANSMISSION |
8679 	    IEEE80211_HE_MAC_CAP5_UL_2x996_TONE_RU |
8680 	    IEEE80211_HE_MAC_CAP5_PUNCTURED_SOUNDING |
8681 	    IEEE80211_HE_MAC_CAP5_HT_VHT_TRIG_FRAME_RX;
8682 	he_cap_elem->mac_cap_info[5] &= ~m;
8683 
8684 	m = IEEE80211_HE_PHY_CAP2_UL_MU_FULL_MU_MIMO |
8685 	    IEEE80211_HE_PHY_CAP2_UL_MU_PARTIAL_MU_MIMO;
8686 	he_cap_elem->phy_cap_info[2] &= ~m;
8687 
8688 	m = IEEE80211_HE_PHY_CAP3_RX_PARTIAL_BW_SU_IN_20MHZ_MU |
8689 	    IEEE80211_HE_PHY_CAP3_DCM_MAX_CONST_TX_MASK |
8690 	    IEEE80211_HE_PHY_CAP3_DCM_MAX_CONST_RX_MASK;
8691 	he_cap_elem->phy_cap_info[3] &= ~m;
8692 
8693 	m = IEEE80211_HE_PHY_CAP4_MU_BEAMFORMER;
8694 	he_cap_elem->phy_cap_info[4] &= ~m;
8695 
8696 	m = IEEE80211_HE_PHY_CAP5_NG16_MU_FEEDBACK;
8697 	he_cap_elem->phy_cap_info[5] &= ~m;
8698 
8699 	m = IEEE80211_HE_PHY_CAP6_CODEBOOK_SIZE_75_MU |
8700 	    IEEE80211_HE_PHY_CAP6_TRIG_MU_BEAMFORMING_PARTIAL_BW_FB |
8701 	    IEEE80211_HE_PHY_CAP6_TRIG_CQI_FB |
8702 	    IEEE80211_HE_PHY_CAP6_PARTIAL_BANDWIDTH_DL_MUMIMO;
8703 	he_cap_elem->phy_cap_info[6] &= ~m;
8704 
8705 	m = IEEE80211_HE_PHY_CAP7_PSR_BASED_SR |
8706 	    IEEE80211_HE_PHY_CAP7_POWER_BOOST_FACTOR_SUPP |
8707 	    IEEE80211_HE_PHY_CAP7_STBC_TX_ABOVE_80MHZ |
8708 	    IEEE80211_HE_PHY_CAP7_STBC_RX_ABOVE_80MHZ;
8709 	he_cap_elem->phy_cap_info[7] &= ~m;
8710 
8711 	m = IEEE80211_HE_PHY_CAP8_HE_ER_SU_PPDU_4XLTF_AND_08_US_GI |
8712 	    IEEE80211_HE_PHY_CAP8_20MHZ_IN_40MHZ_HE_PPDU_IN_2G |
8713 	    IEEE80211_HE_PHY_CAP8_20MHZ_IN_160MHZ_HE_PPDU |
8714 	    IEEE80211_HE_PHY_CAP8_80MHZ_IN_160MHZ_HE_PPDU;
8715 	he_cap_elem->phy_cap_info[8] &= ~m;
8716 
8717 	m = IEEE80211_HE_PHY_CAP9_LONGER_THAN_16_SIGB_OFDM_SYM |
8718 	    IEEE80211_HE_PHY_CAP9_NON_TRIGGERED_CQI_FEEDBACK |
8719 	    IEEE80211_HE_PHY_CAP9_RX_1024_QAM_LESS_THAN_242_TONE_RU |
8720 	    IEEE80211_HE_PHY_CAP9_TX_1024_QAM_LESS_THAN_242_TONE_RU |
8721 	    IEEE80211_HE_PHY_CAP9_RX_FULL_BW_SU_USING_MU_WITH_COMP_SIGB |
8722 	    IEEE80211_HE_PHY_CAP9_RX_FULL_BW_SU_USING_MU_WITH_NON_COMP_SIGB;
8723 	he_cap_elem->phy_cap_info[9] &= ~m;
8724 }
8725 
8726 static __le16 ath12k_mac_setup_he_6ghz_cap(struct ath12k_pdev_cap *pcap,
8727 					   struct ath12k_band_cap *bcap)
8728 {
8729 	u8 val;
8730 
8731 	bcap->he_6ghz_capa = IEEE80211_HT_MPDU_DENSITY_NONE;
8732 	if (bcap->ht_cap_info & WMI_HT_CAP_DYNAMIC_SMPS)
8733 		bcap->he_6ghz_capa |=
8734 			u32_encode_bits(WLAN_HT_CAP_SM_PS_DYNAMIC,
8735 					IEEE80211_HE_6GHZ_CAP_SM_PS);
8736 	else
8737 		bcap->he_6ghz_capa |=
8738 			u32_encode_bits(WLAN_HT_CAP_SM_PS_DISABLED,
8739 					IEEE80211_HE_6GHZ_CAP_SM_PS);
8740 	val = u32_get_bits(pcap->vht_cap,
8741 			   IEEE80211_VHT_CAP_MAX_A_MPDU_LENGTH_EXPONENT_MASK);
8742 	bcap->he_6ghz_capa |=
8743 		u32_encode_bits(val, IEEE80211_HE_6GHZ_CAP_MAX_AMPDU_LEN_EXP);
8744 	val = u32_get_bits(pcap->vht_cap,
8745 			   IEEE80211_VHT_CAP_MAX_MPDU_MASK);
8746 	bcap->he_6ghz_capa |=
8747 		u32_encode_bits(val, IEEE80211_HE_6GHZ_CAP_MAX_MPDU_LEN);
8748 	if (pcap->vht_cap & IEEE80211_VHT_CAP_RX_ANTENNA_PATTERN)
8749 		bcap->he_6ghz_capa |= IEEE80211_HE_6GHZ_CAP_RX_ANTPAT_CONS;
8750 	if (pcap->vht_cap & IEEE80211_VHT_CAP_TX_ANTENNA_PATTERN)
8751 		bcap->he_6ghz_capa |= IEEE80211_HE_6GHZ_CAP_TX_ANTPAT_CONS;
8752 
8753 	return cpu_to_le16(bcap->he_6ghz_capa);
8754 }
8755 
8756 static void ath12k_mac_set_hemcsmap(struct ath12k *ar,
8757 				    struct ath12k_pdev_cap *cap,
8758 				    struct ieee80211_sta_he_cap *he_cap)
8759 {
8760 	struct ieee80211_he_mcs_nss_supp *mcs_nss = &he_cap->he_mcs_nss_supp;
8761 	u8 maxtxnss_160 = ath12k_get_nss_160mhz(ar, ar->num_tx_chains);
8762 	u8 maxrxnss_160 = ath12k_get_nss_160mhz(ar, ar->num_rx_chains);
8763 	u16 txmcs_map_160 = 0, rxmcs_map_160 = 0;
8764 	u16 txmcs_map = 0, rxmcs_map = 0;
8765 	u32 i;
8766 
8767 	for (i = 0; i < 8; i++) {
8768 		if (i < ar->num_tx_chains &&
8769 		    (ar->cfg_tx_chainmask >> cap->tx_chain_mask_shift) & BIT(i))
8770 			txmcs_map |= IEEE80211_HE_MCS_SUPPORT_0_11 << (i * 2);
8771 		else
8772 			txmcs_map |= IEEE80211_HE_MCS_NOT_SUPPORTED << (i * 2);
8773 
8774 		if (i < ar->num_rx_chains &&
8775 		    (ar->cfg_rx_chainmask >> cap->tx_chain_mask_shift) & BIT(i))
8776 			rxmcs_map |= IEEE80211_HE_MCS_SUPPORT_0_11 << (i * 2);
8777 		else
8778 			rxmcs_map |= IEEE80211_HE_MCS_NOT_SUPPORTED << (i * 2);
8779 
8780 		if (i < maxtxnss_160 &&
8781 		    (ar->cfg_tx_chainmask >> cap->tx_chain_mask_shift) & BIT(i))
8782 			txmcs_map_160 |= IEEE80211_HE_MCS_SUPPORT_0_11 << (i * 2);
8783 		else
8784 			txmcs_map_160 |= IEEE80211_HE_MCS_NOT_SUPPORTED << (i * 2);
8785 
8786 		if (i < maxrxnss_160 &&
8787 		    (ar->cfg_tx_chainmask >> cap->tx_chain_mask_shift) & BIT(i))
8788 			rxmcs_map_160 |= IEEE80211_HE_MCS_SUPPORT_0_11 << (i * 2);
8789 		else
8790 			rxmcs_map_160 |= IEEE80211_HE_MCS_NOT_SUPPORTED << (i * 2);
8791 	}
8792 
8793 	mcs_nss->rx_mcs_80 = cpu_to_le16(rxmcs_map & 0xffff);
8794 	mcs_nss->tx_mcs_80 = cpu_to_le16(txmcs_map & 0xffff);
8795 	mcs_nss->rx_mcs_160 = cpu_to_le16(rxmcs_map_160 & 0xffff);
8796 	mcs_nss->tx_mcs_160 = cpu_to_le16(txmcs_map_160 & 0xffff);
8797 }
8798 
8799 static void ath12k_mac_copy_he_cap(struct ath12k *ar,
8800 				   struct ath12k_band_cap *band_cap,
8801 				   int iftype, u8 num_tx_chains,
8802 				   struct ieee80211_sta_he_cap *he_cap)
8803 {
8804 	struct ieee80211_he_cap_elem *he_cap_elem = &he_cap->he_cap_elem;
8805 
8806 	he_cap->has_he = true;
8807 	memcpy(he_cap_elem->mac_cap_info, band_cap->he_cap_info,
8808 	       sizeof(he_cap_elem->mac_cap_info));
8809 	memcpy(he_cap_elem->phy_cap_info, band_cap->he_cap_phy_info,
8810 	       sizeof(he_cap_elem->phy_cap_info));
8811 
8812 	he_cap_elem->mac_cap_info[1] &=
8813 		IEEE80211_HE_MAC_CAP1_TF_MAC_PAD_DUR_MASK;
8814 	he_cap_elem->phy_cap_info[0] &=
8815 		IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_40MHZ_IN_2G |
8816 		IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_40MHZ_80MHZ_IN_5G |
8817 		IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_160MHZ_IN_5G;
8818 	/* 80PLUS80 is not supported */
8819 	he_cap_elem->phy_cap_info[0] &=
8820 		~IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_80PLUS80_MHZ_IN_5G;
8821 	he_cap_elem->phy_cap_info[5] &=
8822 		~IEEE80211_HE_PHY_CAP5_BEAMFORMEE_NUM_SND_DIM_UNDER_80MHZ_MASK;
8823 	he_cap_elem->phy_cap_info[5] |= num_tx_chains - 1;
8824 
8825 	switch (iftype) {
8826 	case NL80211_IFTYPE_AP:
8827 		he_cap_elem->mac_cap_info[2] &=
8828 			~IEEE80211_HE_MAC_CAP2_BCAST_TWT;
8829 		he_cap_elem->phy_cap_info[3] &=
8830 			~IEEE80211_HE_PHY_CAP3_DCM_MAX_CONST_TX_MASK;
8831 		he_cap_elem->phy_cap_info[9] |=
8832 			IEEE80211_HE_PHY_CAP9_RX_1024_QAM_LESS_THAN_242_TONE_RU;
8833 		break;
8834 	case NL80211_IFTYPE_STATION:
8835 		he_cap_elem->mac_cap_info[0] &= ~IEEE80211_HE_MAC_CAP0_TWT_RES;
8836 		he_cap_elem->mac_cap_info[0] |= IEEE80211_HE_MAC_CAP0_TWT_REQ;
8837 		he_cap_elem->phy_cap_info[9] |=
8838 			IEEE80211_HE_PHY_CAP9_TX_1024_QAM_LESS_THAN_242_TONE_RU;
8839 		break;
8840 	case NL80211_IFTYPE_MESH_POINT:
8841 		ath12k_mac_filter_he_cap_mesh(he_cap_elem);
8842 		break;
8843 	}
8844 
8845 	ath12k_mac_set_hemcsmap(ar, &ar->pdev->cap, he_cap);
8846 	memset(he_cap->ppe_thres, 0, sizeof(he_cap->ppe_thres));
8847 	if (he_cap_elem->phy_cap_info[6] &
8848 	    IEEE80211_HE_PHY_CAP6_PPE_THRESHOLD_PRESENT)
8849 		ath12k_gen_ppe_thresh(&band_cap->he_ppet, he_cap->ppe_thres);
8850 }
8851 
8852 static void
8853 ath12k_mac_copy_eht_mcs_nss(struct ath12k_band_cap *band_cap,
8854 			    struct ieee80211_eht_mcs_nss_supp *mcs_nss,
8855 			    const struct ieee80211_he_cap_elem *he_cap,
8856 			    const struct ieee80211_eht_cap_elem_fixed *eht_cap)
8857 {
8858 	if ((he_cap->phy_cap_info[0] &
8859 	     (IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_40MHZ_IN_2G |
8860 	      IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_40MHZ_80MHZ_IN_5G |
8861 	      IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_160MHZ_IN_5G |
8862 	      IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_80PLUS80_MHZ_IN_5G)) == 0)
8863 		memcpy(&mcs_nss->only_20mhz, &band_cap->eht_mcs_20_only,
8864 		       sizeof(struct ieee80211_eht_mcs_nss_supp_20mhz_only));
8865 
8866 	if (he_cap->phy_cap_info[0] &
8867 	    (IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_40MHZ_IN_2G |
8868 	     IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_40MHZ_80MHZ_IN_5G))
8869 		memcpy(&mcs_nss->bw._80, &band_cap->eht_mcs_80,
8870 		       sizeof(struct ieee80211_eht_mcs_nss_supp_bw));
8871 
8872 	if (he_cap->phy_cap_info[0] &
8873 	    IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_160MHZ_IN_5G)
8874 		memcpy(&mcs_nss->bw._160, &band_cap->eht_mcs_160,
8875 		       sizeof(struct ieee80211_eht_mcs_nss_supp_bw));
8876 
8877 	if (eht_cap->phy_cap_info[0] & IEEE80211_EHT_PHY_CAP0_320MHZ_IN_6GHZ)
8878 		memcpy(&mcs_nss->bw._320, &band_cap->eht_mcs_320,
8879 		       sizeof(struct ieee80211_eht_mcs_nss_supp_bw));
8880 }
8881 
8882 static void ath12k_mac_copy_eht_ppe_thresh(struct ath12k_wmi_ppe_threshold_arg *fw_ppet,
8883 					   struct ieee80211_sta_eht_cap *cap)
8884 {
8885 	u16 bit = IEEE80211_EHT_PPE_THRES_INFO_HEADER_SIZE;
8886 	u8 i, nss, ru, ppet_bit_len_per_ru = IEEE80211_EHT_PPE_THRES_INFO_PPET_SIZE * 2;
8887 
8888 	u8p_replace_bits(&cap->eht_ppe_thres[0], fw_ppet->numss_m1,
8889 			 IEEE80211_EHT_PPE_THRES_NSS_MASK);
8890 
8891 	u16p_replace_bits((u16 *)&cap->eht_ppe_thres[0], fw_ppet->ru_bit_mask,
8892 			  IEEE80211_EHT_PPE_THRES_RU_INDEX_BITMASK_MASK);
8893 
8894 	for (nss = 0; nss <= fw_ppet->numss_m1; nss++) {
8895 		for (ru = 0;
8896 		     ru < hweight16(IEEE80211_EHT_PPE_THRES_RU_INDEX_BITMASK_MASK);
8897 		     ru++) {
8898 			u32 val = 0;
8899 
8900 			if ((fw_ppet->ru_bit_mask & BIT(ru)) == 0)
8901 				continue;
8902 
8903 			u32p_replace_bits(&val, fw_ppet->ppet16_ppet8_ru3_ru0[nss] >>
8904 						(ru * ppet_bit_len_per_ru),
8905 					  GENMASK(ppet_bit_len_per_ru - 1, 0));
8906 
8907 			for (i = 0; i < ppet_bit_len_per_ru; i++) {
8908 				cap->eht_ppe_thres[bit / 8] |=
8909 					(((val >> i) & 0x1) << ((bit % 8)));
8910 				bit++;
8911 			}
8912 		}
8913 	}
8914 }
8915 
8916 static void
8917 ath12k_mac_filter_eht_cap_mesh(struct ieee80211_eht_cap_elem_fixed
8918 			       *eht_cap_elem)
8919 {
8920 	u8 m;
8921 
8922 	m = IEEE80211_EHT_MAC_CAP0_EPCS_PRIO_ACCESS;
8923 	eht_cap_elem->mac_cap_info[0] &= ~m;
8924 
8925 	m = IEEE80211_EHT_PHY_CAP0_PARTIAL_BW_UL_MU_MIMO;
8926 	eht_cap_elem->phy_cap_info[0] &= ~m;
8927 
8928 	m = IEEE80211_EHT_PHY_CAP3_NG_16_MU_FEEDBACK |
8929 	    IEEE80211_EHT_PHY_CAP3_CODEBOOK_7_5_MU_FDBK |
8930 	    IEEE80211_EHT_PHY_CAP3_TRIG_MU_BF_PART_BW_FDBK |
8931 	    IEEE80211_EHT_PHY_CAP3_TRIG_CQI_FDBK;
8932 	eht_cap_elem->phy_cap_info[3] &= ~m;
8933 
8934 	m = IEEE80211_EHT_PHY_CAP4_PART_BW_DL_MU_MIMO |
8935 	    IEEE80211_EHT_PHY_CAP4_PSR_SR_SUPP |
8936 	    IEEE80211_EHT_PHY_CAP4_POWER_BOOST_FACT_SUPP |
8937 	    IEEE80211_EHT_PHY_CAP4_EHT_MU_PPDU_4_EHT_LTF_08_GI;
8938 	eht_cap_elem->phy_cap_info[4] &= ~m;
8939 
8940 	m = IEEE80211_EHT_PHY_CAP5_NON_TRIG_CQI_FEEDBACK |
8941 	    IEEE80211_EHT_PHY_CAP5_TX_LESS_242_TONE_RU_SUPP |
8942 	    IEEE80211_EHT_PHY_CAP5_RX_LESS_242_TONE_RU_SUPP |
8943 	    IEEE80211_EHT_PHY_CAP5_MAX_NUM_SUPP_EHT_LTF_MASK;
8944 	eht_cap_elem->phy_cap_info[5] &= ~m;
8945 
8946 	m = IEEE80211_EHT_PHY_CAP6_MAX_NUM_SUPP_EHT_LTF_MASK;
8947 	eht_cap_elem->phy_cap_info[6] &= ~m;
8948 
8949 	m = IEEE80211_EHT_PHY_CAP7_NON_OFDMA_UL_MU_MIMO_80MHZ |
8950 	    IEEE80211_EHT_PHY_CAP7_NON_OFDMA_UL_MU_MIMO_160MHZ |
8951 	    IEEE80211_EHT_PHY_CAP7_NON_OFDMA_UL_MU_MIMO_320MHZ |
8952 	    IEEE80211_EHT_PHY_CAP7_MU_BEAMFORMER_80MHZ |
8953 	    IEEE80211_EHT_PHY_CAP7_MU_BEAMFORMER_160MHZ |
8954 	    IEEE80211_EHT_PHY_CAP7_MU_BEAMFORMER_320MHZ;
8955 	eht_cap_elem->phy_cap_info[7] &= ~m;
8956 }
8957 
8958 static void ath12k_mac_copy_eht_cap(struct ath12k *ar,
8959 				    struct ath12k_band_cap *band_cap,
8960 				    struct ieee80211_he_cap_elem *he_cap_elem,
8961 				    int iftype,
8962 				    struct ieee80211_sta_eht_cap *eht_cap)
8963 {
8964 	struct ieee80211_eht_cap_elem_fixed *eht_cap_elem = &eht_cap->eht_cap_elem;
8965 
8966 	memset(eht_cap, 0, sizeof(struct ieee80211_sta_eht_cap));
8967 
8968 	if (!(test_bit(WMI_TLV_SERVICE_11BE, ar->ab->wmi_ab.svc_map)) ||
8969 	    ath12k_acpi_get_disable_11be(ar->ab))
8970 		return;
8971 
8972 	eht_cap->has_eht = true;
8973 	memcpy(eht_cap_elem->mac_cap_info, band_cap->eht_cap_mac_info,
8974 	       sizeof(eht_cap_elem->mac_cap_info));
8975 	memcpy(eht_cap_elem->phy_cap_info, band_cap->eht_cap_phy_info,
8976 	       sizeof(eht_cap_elem->phy_cap_info));
8977 
8978 	switch (iftype) {
8979 	case NL80211_IFTYPE_AP:
8980 		eht_cap_elem->phy_cap_info[0] &=
8981 			~IEEE80211_EHT_PHY_CAP0_242_TONE_RU_GT20MHZ;
8982 		eht_cap_elem->phy_cap_info[4] &=
8983 			~IEEE80211_EHT_PHY_CAP4_PART_BW_DL_MU_MIMO;
8984 		eht_cap_elem->phy_cap_info[5] &=
8985 			~IEEE80211_EHT_PHY_CAP5_TX_LESS_242_TONE_RU_SUPP;
8986 		break;
8987 	case NL80211_IFTYPE_STATION:
8988 		eht_cap_elem->phy_cap_info[7] &=
8989 			~(IEEE80211_EHT_PHY_CAP7_NON_OFDMA_UL_MU_MIMO_80MHZ |
8990 			  IEEE80211_EHT_PHY_CAP7_NON_OFDMA_UL_MU_MIMO_160MHZ |
8991 			  IEEE80211_EHT_PHY_CAP7_NON_OFDMA_UL_MU_MIMO_320MHZ);
8992 		eht_cap_elem->phy_cap_info[7] &=
8993 			~(IEEE80211_EHT_PHY_CAP7_MU_BEAMFORMER_80MHZ |
8994 			  IEEE80211_EHT_PHY_CAP7_MU_BEAMFORMER_160MHZ |
8995 			  IEEE80211_EHT_PHY_CAP7_MU_BEAMFORMER_320MHZ);
8996 		break;
8997 	case NL80211_IFTYPE_MESH_POINT:
8998 		ath12k_mac_filter_eht_cap_mesh(eht_cap_elem);
8999 		break;
9000 	default:
9001 		break;
9002 	}
9003 
9004 	ath12k_mac_copy_eht_mcs_nss(band_cap, &eht_cap->eht_mcs_nss_supp,
9005 				    he_cap_elem, eht_cap_elem);
9006 
9007 	if (eht_cap_elem->phy_cap_info[5] &
9008 	    IEEE80211_EHT_PHY_CAP5_PPE_THRESHOLD_PRESENT)
9009 		ath12k_mac_copy_eht_ppe_thresh(&band_cap->eht_ppet, eht_cap);
9010 }
9011 
9012 static int ath12k_mac_copy_sband_iftype_data(struct ath12k *ar,
9013 					     struct ath12k_pdev_cap *cap,
9014 					     struct ieee80211_sband_iftype_data *data,
9015 					     int band)
9016 {
9017 	struct ath12k_band_cap *band_cap = &cap->band[band];
9018 	int i, idx = 0;
9019 
9020 	for (i = 0; i < NUM_NL80211_IFTYPES; i++) {
9021 		struct ieee80211_sta_he_cap *he_cap = &data[idx].he_cap;
9022 
9023 		switch (i) {
9024 		case NL80211_IFTYPE_STATION:
9025 		case NL80211_IFTYPE_AP:
9026 		case NL80211_IFTYPE_MESH_POINT:
9027 			break;
9028 
9029 		default:
9030 			continue;
9031 		}
9032 
9033 		data[idx].types_mask = BIT(i);
9034 
9035 		ath12k_mac_copy_he_cap(ar, band_cap, i, ar->num_tx_chains, he_cap);
9036 		if (band == NL80211_BAND_6GHZ) {
9037 			data[idx].he_6ghz_capa.capa =
9038 				ath12k_mac_setup_he_6ghz_cap(cap, band_cap);
9039 		}
9040 		ath12k_mac_copy_eht_cap(ar, band_cap, &he_cap->he_cap_elem, i,
9041 					&data[idx].eht_cap);
9042 		idx++;
9043 	}
9044 
9045 	return idx;
9046 }
9047 
9048 static void ath12k_mac_setup_sband_iftype_data(struct ath12k *ar,
9049 					       struct ath12k_pdev_cap *cap)
9050 {
9051 	struct ieee80211_supported_band *sband;
9052 	enum nl80211_band band;
9053 	int count;
9054 
9055 	if (cap->supported_bands & WMI_HOST_WLAN_2GHZ_CAP) {
9056 		band = NL80211_BAND_2GHZ;
9057 		count = ath12k_mac_copy_sband_iftype_data(ar, cap,
9058 							  ar->mac.iftype[band],
9059 							  band);
9060 		sband = &ar->mac.sbands[band];
9061 		_ieee80211_set_sband_iftype_data(sband, ar->mac.iftype[band],
9062 						 count);
9063 	}
9064 
9065 	if (cap->supported_bands & WMI_HOST_WLAN_5GHZ_CAP) {
9066 		band = NL80211_BAND_5GHZ;
9067 		count = ath12k_mac_copy_sband_iftype_data(ar, cap,
9068 							  ar->mac.iftype[band],
9069 							  band);
9070 		sband = &ar->mac.sbands[band];
9071 		_ieee80211_set_sband_iftype_data(sband, ar->mac.iftype[band],
9072 						 count);
9073 	}
9074 
9075 	if (cap->supported_bands & WMI_HOST_WLAN_5GHZ_CAP &&
9076 	    ar->supports_6ghz) {
9077 		band = NL80211_BAND_6GHZ;
9078 		count = ath12k_mac_copy_sband_iftype_data(ar, cap,
9079 							  ar->mac.iftype[band],
9080 							  band);
9081 		sband = &ar->mac.sbands[band];
9082 		_ieee80211_set_sband_iftype_data(sband, ar->mac.iftype[band],
9083 						 count);
9084 	}
9085 }
9086 
9087 static int __ath12k_set_antenna(struct ath12k *ar, u32 tx_ant, u32 rx_ant)
9088 {
9089 	struct ath12k_hw *ah = ath12k_ar_to_ah(ar);
9090 	int ret;
9091 
9092 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
9093 
9094 	if (ath12k_check_chain_mask(ar, tx_ant, true))
9095 		return -EINVAL;
9096 
9097 	if (ath12k_check_chain_mask(ar, rx_ant, false))
9098 		return -EINVAL;
9099 
9100 	/* Since we advertised the max cap of all radios combined during wiphy
9101 	 * registration, ensure we don't set the antenna config higher than the
9102 	 * limits
9103 	 */
9104 	tx_ant = min_t(u32, tx_ant, ar->pdev->cap.tx_chain_mask);
9105 	rx_ant = min_t(u32, rx_ant, ar->pdev->cap.rx_chain_mask);
9106 
9107 	ar->cfg_tx_chainmask = tx_ant;
9108 	ar->cfg_rx_chainmask = rx_ant;
9109 
9110 	if (ah->state != ATH12K_HW_STATE_ON &&
9111 	    ah->state != ATH12K_HW_STATE_RESTARTED)
9112 		return 0;
9113 
9114 	ret = ath12k_wmi_pdev_set_param(ar, WMI_PDEV_PARAM_TX_CHAIN_MASK,
9115 					tx_ant, ar->pdev->pdev_id);
9116 	if (ret) {
9117 		ath12k_warn(ar->ab, "failed to set tx-chainmask: %d, req 0x%x\n",
9118 			    ret, tx_ant);
9119 		return ret;
9120 	}
9121 
9122 	ar->num_tx_chains = hweight32(tx_ant);
9123 
9124 	ret = ath12k_wmi_pdev_set_param(ar, WMI_PDEV_PARAM_RX_CHAIN_MASK,
9125 					rx_ant, ar->pdev->pdev_id);
9126 	if (ret) {
9127 		ath12k_warn(ar->ab, "failed to set rx-chainmask: %d, req 0x%x\n",
9128 			    ret, rx_ant);
9129 		return ret;
9130 	}
9131 
9132 	ar->num_rx_chains = hweight32(rx_ant);
9133 
9134 	/* Reload HT/VHT/HE capability */
9135 	ath12k_mac_setup_ht_vht_cap(ar, &ar->pdev->cap, NULL);
9136 	ath12k_mac_setup_sband_iftype_data(ar, &ar->pdev->cap);
9137 
9138 	return 0;
9139 }
9140 
9141 static void ath12k_mgmt_over_wmi_tx_drop(struct ath12k *ar, struct sk_buff *skb)
9142 {
9143 	int num_mgmt;
9144 
9145 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
9146 
9147 	ieee80211_free_txskb(ath12k_ar_to_hw(ar), skb);
9148 
9149 	num_mgmt = atomic_dec_if_positive(&ar->num_pending_mgmt_tx);
9150 
9151 	if (num_mgmt < 0)
9152 		WARN_ON_ONCE(1);
9153 
9154 	if (!num_mgmt)
9155 		wake_up(&ar->txmgmt_empty_waitq);
9156 }
9157 
9158 static void ath12k_mac_tx_mgmt_free(struct ath12k *ar, int buf_id)
9159 {
9160 	struct sk_buff *msdu;
9161 	struct ieee80211_tx_info *info;
9162 
9163 	spin_lock_bh(&ar->txmgmt_idr_lock);
9164 	msdu = idr_remove(&ar->txmgmt_idr, buf_id);
9165 	spin_unlock_bh(&ar->txmgmt_idr_lock);
9166 
9167 	if (!msdu)
9168 		return;
9169 
9170 	dma_unmap_single(ar->ab->dev, ATH12K_SKB_CB(msdu)->paddr, msdu->len,
9171 			 DMA_TO_DEVICE);
9172 
9173 	info = IEEE80211_SKB_CB(msdu);
9174 	memset(&info->status, 0, sizeof(info->status));
9175 
9176 	ath12k_mgmt_over_wmi_tx_drop(ar, msdu);
9177 }
9178 
9179 int ath12k_mac_tx_mgmt_pending_free(int buf_id, void *skb, void *ctx)
9180 {
9181 	struct ath12k *ar = ctx;
9182 
9183 	ath12k_mac_tx_mgmt_free(ar, buf_id);
9184 
9185 	return 0;
9186 }
9187 
9188 static int ath12k_mac_vif_txmgmt_idr_remove(int buf_id, void *skb, void *ctx)
9189 {
9190 	struct ieee80211_vif *vif = ctx;
9191 	struct ath12k_skb_cb *skb_cb = ATH12K_SKB_CB(skb);
9192 	struct ath12k *ar = skb_cb->ar;
9193 
9194 	if (skb_cb->vif == vif)
9195 		ath12k_mac_tx_mgmt_free(ar, buf_id);
9196 
9197 	return 0;
9198 }
9199 
9200 static int ath12k_mac_mgmt_tx_wmi(struct ath12k *ar, struct ath12k_link_vif *arvif,
9201 				  struct sk_buff *skb)
9202 {
9203 	struct ath12k_base *ab = ar->ab;
9204 	struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
9205 	struct ath12k_skb_cb *skb_cb = ATH12K_SKB_CB(skb);
9206 	struct ieee80211_tx_info *info;
9207 	enum hal_encrypt_type enctype;
9208 	unsigned int mic_len;
9209 	dma_addr_t paddr;
9210 	int buf_id;
9211 	int ret;
9212 
9213 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
9214 
9215 	skb_cb->ar = ar;
9216 	spin_lock_bh(&ar->txmgmt_idr_lock);
9217 	buf_id = idr_alloc(&ar->txmgmt_idr, skb, 0,
9218 			   ATH12K_TX_MGMT_NUM_PENDING_MAX, GFP_ATOMIC);
9219 	spin_unlock_bh(&ar->txmgmt_idr_lock);
9220 	if (buf_id < 0)
9221 		return -ENOSPC;
9222 
9223 	info = IEEE80211_SKB_CB(skb);
9224 	if ((skb_cb->flags & ATH12K_SKB_CIPHER_SET) &&
9225 	    !(info->flags & IEEE80211_TX_CTL_HW_80211_ENCAP)) {
9226 		if ((ieee80211_is_action(hdr->frame_control) ||
9227 		     ieee80211_is_deauth(hdr->frame_control) ||
9228 		     ieee80211_is_disassoc(hdr->frame_control)) &&
9229 		     ieee80211_has_protected(hdr->frame_control)) {
9230 			enctype = ath12k_dp_tx_get_encrypt_type(skb_cb->cipher);
9231 			mic_len = ath12k_dp_rx_crypto_mic_len(ab->dp, enctype);
9232 			skb_put(skb, mic_len);
9233 		}
9234 	}
9235 
9236 	paddr = dma_map_single(ab->dev, skb->data, skb->len, DMA_TO_DEVICE);
9237 	if (dma_mapping_error(ab->dev, paddr)) {
9238 		ath12k_warn(ab, "failed to DMA map mgmt Tx buffer\n");
9239 		ret = -EIO;
9240 		goto err_free_idr;
9241 	}
9242 
9243 	skb_cb->paddr = paddr;
9244 
9245 	ret = ath12k_wmi_mgmt_send(arvif, buf_id, skb);
9246 	if (ret) {
9247 		ath12k_warn(ar->ab, "failed to send mgmt frame: %d\n", ret);
9248 		goto err_unmap_buf;
9249 	}
9250 
9251 	return 0;
9252 
9253 err_unmap_buf:
9254 	dma_unmap_single(ab->dev, skb_cb->paddr,
9255 			 skb->len, DMA_TO_DEVICE);
9256 err_free_idr:
9257 	spin_lock_bh(&ar->txmgmt_idr_lock);
9258 	idr_remove(&ar->txmgmt_idr, buf_id);
9259 	spin_unlock_bh(&ar->txmgmt_idr_lock);
9260 
9261 	return ret;
9262 }
9263 
9264 static void ath12k_mgmt_over_wmi_tx_purge(struct ath12k *ar)
9265 {
9266 	struct sk_buff *skb;
9267 
9268 	while ((skb = skb_dequeue(&ar->wmi_mgmt_tx_queue)) != NULL)
9269 		ath12k_mgmt_over_wmi_tx_drop(ar, skb);
9270 }
9271 
9272 static int ath12k_mac_mgmt_action_frame_fill_elem_data(struct ath12k_link_vif *arvif,
9273 						       struct sk_buff *skb)
9274 {
9275 	struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
9276 	u8 category, *buf, iv_len, action_code, dialog_token;
9277 	struct ieee80211_bss_conf *link_conf;
9278 	struct ieee80211_chanctx_conf *conf;
9279 	int cur_tx_power, max_tx_power;
9280 	struct ath12k *ar = arvif->ar;
9281 	struct ieee80211_hw *hw = ath12k_ar_to_hw(ar);
9282 	struct wiphy *wiphy = hw->wiphy;
9283 	struct ath12k_skb_cb *skb_cb;
9284 	struct ieee80211_mgmt *mgmt;
9285 	unsigned int remaining_len;
9286 	bool has_protected;
9287 
9288 	lockdep_assert_wiphy(wiphy);
9289 
9290 	/* make sure category field is present */
9291 	if (skb->len < IEEE80211_MIN_ACTION_SIZE(category))
9292 		return -EINVAL;
9293 
9294 	remaining_len = skb->len - IEEE80211_MIN_ACTION_SIZE(category);
9295 	has_protected = ieee80211_has_protected(hdr->frame_control);
9296 
9297 	/* In case of SW crypto and hdr protected (PMF), packet will already be encrypted,
9298 	 * we can't put in data in this case
9299 	 */
9300 	if (test_bit(ATH12K_FLAG_HW_CRYPTO_DISABLED, &ar->ab->dev_flags) &&
9301 	    has_protected)
9302 		return 0;
9303 
9304 	mgmt = (struct ieee80211_mgmt *)hdr;
9305 	buf = (u8 *)&mgmt->u.action;
9306 
9307 	/* FCTL_PROTECTED frame might have extra space added for HDR_LEN. Offset that
9308 	 * many bytes if it is there
9309 	 */
9310 	if (has_protected) {
9311 		skb_cb = ATH12K_SKB_CB(skb);
9312 
9313 		switch (skb_cb->cipher) {
9314 		/* Cipher suite having flag %IEEE80211_KEY_FLAG_GENERATE_IV_MGMT set in
9315 		 * key needs to be processed. See ath12k_install_key()
9316 		 */
9317 		case WLAN_CIPHER_SUITE_CCMP:
9318 		case WLAN_CIPHER_SUITE_CCMP_256:
9319 		case WLAN_CIPHER_SUITE_GCMP:
9320 		case WLAN_CIPHER_SUITE_GCMP_256:
9321 			iv_len = IEEE80211_CCMP_HDR_LEN;
9322 			break;
9323 		case WLAN_CIPHER_SUITE_TKIP:
9324 			iv_len = 0;
9325 			break;
9326 		default:
9327 			return -EINVAL;
9328 		}
9329 
9330 		if (remaining_len < iv_len)
9331 			return -EINVAL;
9332 
9333 		buf += iv_len;
9334 		remaining_len -= iv_len;
9335 	}
9336 
9337 	category = *buf++;
9338 	/* category code is already taken care in %IEEE80211_MIN_ACTION_SIZE hence
9339 	 * no need to adjust remaining_len
9340 	 */
9341 
9342 	switch (category) {
9343 	case WLAN_CATEGORY_RADIO_MEASUREMENT:
9344 		/* need action code and dialog token */
9345 		if (remaining_len < 2)
9346 			return -EINVAL;
9347 
9348 		/* Packet Format:
9349 		 *	Action Code | Dialog Token | Variable Len (based on Action Code)
9350 		 */
9351 		action_code = *buf++;
9352 		dialog_token = *buf++;
9353 		remaining_len -= 2;
9354 
9355 		link_conf = ath12k_mac_get_link_bss_conf(arvif);
9356 		if (!link_conf) {
9357 			ath12k_warn(ar->ab,
9358 				    "failed to get bss link conf for vdev %d in RM handling\n",
9359 				    arvif->vdev_id);
9360 			return -EINVAL;
9361 		}
9362 
9363 		conf = wiphy_dereference(wiphy, link_conf->chanctx_conf);
9364 		if (!conf)
9365 			return -ENOENT;
9366 
9367 		cur_tx_power = link_conf->txpower;
9368 		max_tx_power = min(conf->def.chan->max_reg_power,
9369 				   (int)ar->max_tx_power / 2);
9370 
9371 		ath12k_mac_op_get_txpower(hw, arvif->ahvif->vif, arvif->link_id,
9372 					  &cur_tx_power);
9373 
9374 		switch (action_code) {
9375 		case WLAN_RM_ACTION_LINK_MEASUREMENT_REQUEST:
9376 			/* need variable fields to be present in len */
9377 			if (remaining_len < 2)
9378 				return -EINVAL;
9379 
9380 			/* Variable length format as defined in IEEE 802.11-2024,
9381 			 * Figure 9-1187-Link Measurement Request frame Action field
9382 			 * format.
9383 			 *	Transmit Power | Max Tx Power
9384 			 * We fill both of these.
9385 			 */
9386 			*buf++ = cur_tx_power;
9387 			*buf = max_tx_power;
9388 
9389 			ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
9390 				   "RRM: Link Measurement Req dialog_token %u cur_tx_power %d max_tx_power %d\n",
9391 				   dialog_token, cur_tx_power, max_tx_power);
9392 			break;
9393 		case WLAN_RM_ACTION_LINK_MEASUREMENT_REPORT:
9394 			/* need variable fields to be present in len */
9395 			if (remaining_len < 3)
9396 				return -EINVAL;
9397 
9398 			/* Variable length format as defined in IEEE 802.11-2024,
9399 			 * Figure 9-1188-Link Measurement Report frame Action field format
9400 			 *	TPC Report | Variable Fields
9401 			 *
9402 			 * TPC Report Format:
9403 			 *	Element ID | Len | Tx Power | Link Margin
9404 			 *
9405 			 * We fill Tx power in the TPC Report (2nd index)
9406 			 */
9407 			buf[2] = cur_tx_power;
9408 
9409 			/* TODO: At present, Link margin data is not present so can't
9410 			 * really fill it now. Once it is available, it can be added
9411 			 * here
9412 			 */
9413 			ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
9414 				   "RRM: Link Measurement Report dialog_token %u cur_tx_power %d\n",
9415 				   dialog_token, cur_tx_power);
9416 			break;
9417 		default:
9418 			return -EINVAL;
9419 		}
9420 		break;
9421 	default:
9422 		/* nothing to fill */
9423 		return 0;
9424 	}
9425 
9426 	return 0;
9427 }
9428 
9429 static int ath12k_mac_mgmt_frame_fill_elem_data(struct ath12k_link_vif *arvif,
9430 						struct sk_buff *skb)
9431 {
9432 	struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
9433 
9434 	if (!ieee80211_is_action(hdr->frame_control))
9435 		return 0;
9436 
9437 	return ath12k_mac_mgmt_action_frame_fill_elem_data(arvif, skb);
9438 }
9439 
9440 static void ath12k_mgmt_over_wmi_tx_work(struct wiphy *wiphy, struct wiphy_work *work)
9441 {
9442 	struct ath12k *ar = container_of(work, struct ath12k, wmi_mgmt_tx_work);
9443 	struct ath12k_hw *ah = ar->ah;
9444 	struct ath12k_skb_cb *skb_cb;
9445 	struct ath12k_vif *ahvif;
9446 	struct ath12k_link_vif *arvif;
9447 	struct sk_buff *skb;
9448 	int ret;
9449 
9450 	lockdep_assert_wiphy(wiphy);
9451 
9452 	while ((skb = skb_dequeue(&ar->wmi_mgmt_tx_queue)) != NULL) {
9453 		skb_cb = ATH12K_SKB_CB(skb);
9454 		if (!skb_cb->vif) {
9455 			ath12k_warn(ar->ab, "no vif found for mgmt frame\n");
9456 			ath12k_mgmt_over_wmi_tx_drop(ar, skb);
9457 			continue;
9458 		}
9459 
9460 		ahvif = ath12k_vif_to_ahvif(skb_cb->vif);
9461 		if (!(ahvif->links_map & BIT(skb_cb->link_id))) {
9462 			ath12k_warn(ar->ab,
9463 				    "invalid linkid %u in mgmt over wmi tx with linkmap 0x%x\n",
9464 				    skb_cb->link_id, ahvif->links_map);
9465 			ath12k_mgmt_over_wmi_tx_drop(ar, skb);
9466 			continue;
9467 		}
9468 
9469 		arvif = wiphy_dereference(ah->hw->wiphy, ahvif->link[skb_cb->link_id]);
9470 		if (ar->allocated_vdev_map & (1LL << arvif->vdev_id)) {
9471 			/* Fill in the data which is required to be filled by the driver
9472 			 * For example: Max Tx power in Link Measurement Request/Report
9473 			 */
9474 			ret = ath12k_mac_mgmt_frame_fill_elem_data(arvif, skb);
9475 			if (ret) {
9476 				/* If we couldn't fill the data due to any reason,
9477 				 * let's not discard transmitting the packet.
9478 				 * For example: Software crypto and PMF case
9479 				 */
9480 				ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
9481 					   "Failed to fill the required data for the mgmt packet err %d\n",
9482 					   ret);
9483 			}
9484 
9485 			ret = ath12k_mac_mgmt_tx_wmi(ar, arvif, skb);
9486 			if (ret) {
9487 				ath12k_warn(ar->ab, "failed to tx mgmt frame, vdev_id %d :%d\n",
9488 					    arvif->vdev_id, ret);
9489 				ath12k_mgmt_over_wmi_tx_drop(ar, skb);
9490 			}
9491 		} else {
9492 			ath12k_warn(ar->ab,
9493 				    "dropping mgmt frame for vdev %d link %u is_started %d\n",
9494 				    arvif->vdev_id,
9495 				    skb_cb->link_id,
9496 				    arvif->is_started);
9497 			ath12k_mgmt_over_wmi_tx_drop(ar, skb);
9498 		}
9499 	}
9500 }
9501 
9502 int ath12k_mac_mgmt_tx(struct ath12k *ar, struct sk_buff *skb,
9503 		       bool is_prb_rsp)
9504 {
9505 	struct sk_buff_head *q = &ar->wmi_mgmt_tx_queue;
9506 
9507 	if (test_bit(ATH12K_FLAG_CRASH_FLUSH, &ar->ab->dev_flags))
9508 		return -ESHUTDOWN;
9509 
9510 	/* Drop probe response packets when the pending management tx
9511 	 * count has reached a certain threshold, so as to prioritize
9512 	 * other mgmt packets like auth and assoc to be sent on time
9513 	 * for establishing successful connections.
9514 	 */
9515 	if (is_prb_rsp &&
9516 	    atomic_read(&ar->num_pending_mgmt_tx) > ATH12K_PRB_RSP_DROP_THRESHOLD) {
9517 		ath12k_warn(ar->ab,
9518 			    "dropping probe response as pending queue is almost full\n");
9519 		return -ENOSPC;
9520 	}
9521 
9522 	if (skb_queue_len_lockless(q) >= ATH12K_TX_MGMT_NUM_PENDING_MAX) {
9523 		ath12k_warn(ar->ab, "mgmt tx queue is full\n");
9524 		return -ENOSPC;
9525 	}
9526 
9527 	skb_queue_tail(q, skb);
9528 	atomic_inc(&ar->num_pending_mgmt_tx);
9529 	wiphy_work_queue(ath12k_ar_to_hw(ar)->wiphy, &ar->wmi_mgmt_tx_work);
9530 
9531 	return 0;
9532 }
9533 EXPORT_SYMBOL(ath12k_mac_mgmt_tx);
9534 
9535 void ath12k_mac_add_p2p_noa_ie(struct ath12k *ar,
9536 			       struct ieee80211_vif *vif,
9537 			       struct sk_buff *skb,
9538 			       bool is_prb_rsp)
9539 {
9540 	struct ath12k_vif *ahvif = ath12k_vif_to_ahvif(vif);
9541 
9542 	if (likely(!is_prb_rsp))
9543 		return;
9544 
9545 	spin_lock_bh(&ar->data_lock);
9546 
9547 	if (ahvif->u.ap.noa_data &&
9548 	    !pskb_expand_head(skb, 0, ahvif->u.ap.noa_len,
9549 			      GFP_ATOMIC))
9550 		skb_put_data(skb, ahvif->u.ap.noa_data,
9551 			     ahvif->u.ap.noa_len);
9552 
9553 	spin_unlock_bh(&ar->data_lock);
9554 }
9555 EXPORT_SYMBOL(ath12k_mac_add_p2p_noa_ie);
9556 
9557 /* Note: called under rcu_read_lock() */
9558 void ath12k_mlo_mcast_update_tx_link_address(struct ieee80211_vif *vif,
9559 					     u8 link_id, struct sk_buff *skb,
9560 					     u32 info_flags)
9561 {
9562 	struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
9563 	struct ieee80211_bss_conf *bss_conf;
9564 
9565 	if (info_flags & IEEE80211_TX_CTL_HW_80211_ENCAP)
9566 		return;
9567 
9568 	bss_conf = rcu_dereference(vif->link_conf[link_id]);
9569 	if (bss_conf)
9570 		ether_addr_copy(hdr->addr2, bss_conf->addr);
9571 }
9572 EXPORT_SYMBOL(ath12k_mlo_mcast_update_tx_link_address);
9573 
9574 /* Note: called under rcu_read_lock() */
9575 u8 ath12k_mac_get_tx_link(struct ieee80211_sta *sta, struct ieee80211_vif *vif,
9576 			  u8 link, struct sk_buff *skb, u32 info_flags)
9577 {
9578 	struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
9579 	struct ath12k_vif *ahvif = ath12k_vif_to_ahvif(vif);
9580 	struct ieee80211_link_sta *link_sta;
9581 	struct ieee80211_bss_conf *bss_conf;
9582 	struct ath12k_sta *ahsta;
9583 
9584 	/* Use the link id passed or the default vif link */
9585 	if (!sta) {
9586 		if (link != IEEE80211_LINK_UNSPECIFIED)
9587 			return link;
9588 
9589 		return ahvif->deflink.link_id;
9590 	}
9591 
9592 	ahsta = ath12k_sta_to_ahsta(sta);
9593 
9594 	/* Below translation ensures we pass proper A2 & A3 for non ML clients.
9595 	 * Also it assumes for now support only for MLO AP in this path
9596 	 */
9597 	if (!sta->mlo) {
9598 		link = ahsta->deflink.link_id;
9599 
9600 		if (info_flags & IEEE80211_TX_CTL_HW_80211_ENCAP)
9601 			return link;
9602 
9603 		bss_conf = rcu_dereference(vif->link_conf[link]);
9604 		if (bss_conf) {
9605 			ether_addr_copy(hdr->addr2, bss_conf->addr);
9606 			if (!ieee80211_has_tods(hdr->frame_control) &&
9607 			    !ieee80211_has_fromds(hdr->frame_control))
9608 				ether_addr_copy(hdr->addr3, bss_conf->addr);
9609 		}
9610 
9611 		return link;
9612 	}
9613 
9614 	/* enqueue eth enacap & data frames on primary link, FW does link
9615 	 * selection and address translation.
9616 	 */
9617 	if (info_flags & IEEE80211_TX_CTL_HW_80211_ENCAP ||
9618 	    ieee80211_is_data(hdr->frame_control))
9619 		return ahsta->assoc_link_id;
9620 
9621 	/* 802.11 frame cases */
9622 	if (link == IEEE80211_LINK_UNSPECIFIED)
9623 		link = ahsta->deflink.link_id;
9624 
9625 	if (!ieee80211_is_mgmt(hdr->frame_control))
9626 		return link;
9627 
9628 	/* Perform address conversion for ML STA Tx */
9629 	bss_conf = rcu_dereference(vif->link_conf[link]);
9630 	link_sta = rcu_dereference(sta->link[link]);
9631 
9632 	if (bss_conf && link_sta) {
9633 		ether_addr_copy(hdr->addr1, link_sta->addr);
9634 		ether_addr_copy(hdr->addr2, bss_conf->addr);
9635 
9636 		if (vif->type == NL80211_IFTYPE_STATION && bss_conf->bssid)
9637 			ether_addr_copy(hdr->addr3, bss_conf->bssid);
9638 		else if (vif->type == NL80211_IFTYPE_AP)
9639 			ether_addr_copy(hdr->addr3, bss_conf->addr);
9640 
9641 		return link;
9642 	}
9643 
9644 	if (bss_conf) {
9645 		/* In certain cases where a ML sta associated and added subset of
9646 		 * links on which the ML AP is active, but now sends some frame
9647 		 * (ex. Probe request) on a different link which is active in our
9648 		 * MLD but was not added during previous association, we can
9649 		 * still honor the Tx to that ML STA via the requested link.
9650 		 * The control would reach here in such case only when that link
9651 		 * address is same as the MLD address or in worst case clients
9652 		 * used MLD address at TA wrongly which would have helped
9653 		 * identify the ML sta object and pass it here.
9654 		 * If the link address of that STA is different from MLD address,
9655 		 * then the sta object would be NULL and control won't reach
9656 		 * here but return at the start of the function itself with !sta
9657 		 * check. Also this would not need any translation at hdr->addr1
9658 		 * from MLD to link address since the RA is the MLD address
9659 		 * (same as that link address ideally) already.
9660 		 */
9661 		ether_addr_copy(hdr->addr2, bss_conf->addr);
9662 
9663 		if (vif->type == NL80211_IFTYPE_STATION && bss_conf->bssid)
9664 			ether_addr_copy(hdr->addr3, bss_conf->bssid);
9665 		else if (vif->type == NL80211_IFTYPE_AP)
9666 			ether_addr_copy(hdr->addr3, bss_conf->addr);
9667 	}
9668 
9669 	return link;
9670 }
9671 EXPORT_SYMBOL(ath12k_mac_get_tx_link);
9672 
9673 void ath12k_mac_drain_tx(struct ath12k *ar)
9674 {
9675 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
9676 
9677 	/* make sure rcu-protected mac80211 tx path itself is drained */
9678 	synchronize_net();
9679 
9680 	wiphy_work_cancel(ath12k_ar_to_hw(ar)->wiphy, &ar->wmi_mgmt_tx_work);
9681 	ath12k_mgmt_over_wmi_tx_purge(ar);
9682 }
9683 
9684 static int ath12k_mac_config_mon_status_default(struct ath12k *ar, bool enable)
9685 {
9686 	struct htt_rx_ring_tlv_filter tlv_filter = {};
9687 	struct ath12k_base *ab = ar->ab;
9688 	u32 ring_id, i;
9689 	int ret = 0;
9690 
9691 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
9692 
9693 	if (!ab->hw_params->rxdma1_enable)
9694 		return ret;
9695 
9696 	if (enable) {
9697 		tlv_filter = ath12k_mac_mon_status_filter_default;
9698 
9699 		if (ath12k_debugfs_rx_filter(ar))
9700 			tlv_filter.rx_filter = ath12k_debugfs_rx_filter(ar);
9701 	} else {
9702 		tlv_filter.rxmon_disable = true;
9703 	}
9704 
9705 	for (i = 0; i < ab->hw_params->num_rxdma_per_pdev; i++) {
9706 		ring_id = ar->dp.rxdma_mon_dst_ring[i].ring_id;
9707 		ret = ath12k_dp_tx_htt_rx_filter_setup(ab, ring_id,
9708 						       ar->dp.mac_id + i,
9709 						       HAL_RXDMA_MONITOR_DST,
9710 						       DP_RXDMA_REFILL_RING_SIZE,
9711 						       &tlv_filter);
9712 		if (ret) {
9713 			ath12k_err(ab,
9714 				   "failed to setup filter for monitor buf %d\n",
9715 				   ret);
9716 		}
9717 	}
9718 
9719 	return ret;
9720 }
9721 
9722 static int ath12k_mac_start(struct ath12k *ar)
9723 {
9724 	struct ath12k_hw *ah = ar->ah;
9725 	struct ath12k_base *ab = ar->ab;
9726 	struct ath12k_pdev *pdev = ar->pdev;
9727 	int ret;
9728 
9729 	lockdep_assert_held(&ah->hw_mutex);
9730 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
9731 
9732 	ret = ath12k_wmi_pdev_set_param(ar, WMI_PDEV_PARAM_PMF_QOS,
9733 					1, pdev->pdev_id);
9734 
9735 	if (ret) {
9736 		ath12k_err(ab, "failed to enable PMF QOS: %d\n", ret);
9737 		goto err;
9738 	}
9739 
9740 	ret = ath12k_wmi_pdev_set_param(ar, WMI_PDEV_PARAM_DYNAMIC_BW, 1,
9741 					pdev->pdev_id);
9742 	if (ret) {
9743 		ath12k_err(ab, "failed to enable dynamic bw: %d\n", ret);
9744 		goto err;
9745 	}
9746 
9747 	ret = ath12k_wmi_pdev_set_param(ar, WMI_PDEV_PARAM_ARP_AC_OVERRIDE,
9748 					0, pdev->pdev_id);
9749 	if (ret) {
9750 		ath12k_err(ab, "failed to set ac override for ARP: %d\n",
9751 			   ret);
9752 		goto err;
9753 	}
9754 
9755 	ret = ath12k_wmi_send_dfs_phyerr_offload_enable_cmd(ar, pdev->pdev_id);
9756 	if (ret) {
9757 		ath12k_err(ab, "failed to offload radar detection: %d\n",
9758 			   ret);
9759 		goto err;
9760 	}
9761 
9762 	ret = ath12k_dp_tx_htt_h2t_ppdu_stats_req(ar,
9763 						  HTT_PPDU_STATS_TAG_DEFAULT);
9764 	if (ret) {
9765 		ath12k_err(ab, "failed to req ppdu stats: %d\n", ret);
9766 		goto err;
9767 	}
9768 
9769 	ret = ath12k_wmi_pdev_set_param(ar, WMI_PDEV_PARAM_MESH_MCAST_ENABLE,
9770 					1, pdev->pdev_id);
9771 
9772 	if (ret) {
9773 		ath12k_err(ab, "failed to enable MESH MCAST ENABLE: (%d\n", ret);
9774 		goto err;
9775 	}
9776 
9777 	__ath12k_set_antenna(ar, ar->cfg_tx_chainmask, ar->cfg_rx_chainmask);
9778 
9779 	/* TODO: Do we need to enable ANI? */
9780 
9781 	ret = ath12k_reg_update_chan_list(ar, false);
9782 
9783 	/* The ar state alone can be turned off for non supported country
9784 	 * without returning the error value. As we need to update the channel
9785 	 * for the next ar.
9786 	 */
9787 	if (ret) {
9788 		if (ret == -EINVAL)
9789 			ret = 0;
9790 		goto err;
9791 	}
9792 
9793 	ar->num_started_vdevs = 0;
9794 	ar->num_created_vdevs = 0;
9795 	ar->num_peers = 0;
9796 	ar->allocated_vdev_map = 0;
9797 	ar->chan_tx_pwr = ATH12K_PDEV_TX_POWER_INVALID;
9798 
9799 	spin_lock_bh(&ar->data_lock);
9800 	ar->incumbent_signal_interference.handling_in_progress = false;
9801 	spin_unlock_bh(&ar->data_lock);
9802 
9803 	/* Configure monitor status ring with default rx_filter to get rx status
9804 	 * such as rssi, rx_duration.
9805 	 */
9806 	ret = ath12k_mac_config_mon_status_default(ar, true);
9807 	if (ret && (ret != -EOPNOTSUPP)) {
9808 		ath12k_err(ab, "failed to configure monitor status ring with default rx_filter: (%d)\n",
9809 			   ret);
9810 		goto err;
9811 	}
9812 
9813 	if (ret == -EOPNOTSUPP)
9814 		ath12k_dbg(ab, ATH12K_DBG_MAC,
9815 			   "monitor status config is not yet supported");
9816 
9817 	/* Configure the hash seed for hash based reo dest ring selection */
9818 	ath12k_wmi_pdev_lro_cfg(ar, ar->pdev->pdev_id);
9819 
9820 	/* allow device to enter IMPS */
9821 	if (ab->hw_params->idle_ps) {
9822 		ret = ath12k_wmi_pdev_set_param(ar, WMI_PDEV_PARAM_IDLE_PS_CONFIG,
9823 						1, pdev->pdev_id);
9824 		if (ret) {
9825 			ath12k_err(ab, "failed to enable idle ps: %d\n", ret);
9826 			goto err;
9827 		}
9828 	}
9829 
9830 	ret = ath12k_thermal_throttling_config_default(ar);
9831 	if (ret) {
9832 		ath12k_err(ab, "failed to set thermal throttle: %d\n", ret);
9833 		goto err;
9834 	}
9835 
9836 	rcu_assign_pointer(ab->pdevs_active[ar->pdev_idx],
9837 			   &ab->pdevs[ar->pdev_idx]);
9838 
9839 	return 0;
9840 err:
9841 
9842 	return ret;
9843 }
9844 
9845 static void ath12k_drain_tx(struct ath12k_hw *ah)
9846 {
9847 	struct ath12k *ar;
9848 	int i;
9849 
9850 	lockdep_assert_wiphy(ah->hw->wiphy);
9851 
9852 	for_each_ar(ah, ar, i)
9853 		ath12k_mac_drain_tx(ar);
9854 }
9855 
9856 int ath12k_mac_op_start(struct ieee80211_hw *hw)
9857 {
9858 	struct ath12k_hw *ah = ath12k_hw_to_ah(hw);
9859 	struct ath12k *ar;
9860 	int ret, i;
9861 
9862 	if (ath12k_ftm_mode)
9863 		return -EPERM;
9864 
9865 	lockdep_assert_wiphy(hw->wiphy);
9866 
9867 	ath12k_drain_tx(ah);
9868 
9869 	guard(mutex)(&ah->hw_mutex);
9870 
9871 	switch (ah->state) {
9872 	case ATH12K_HW_STATE_OFF:
9873 		ah->state = ATH12K_HW_STATE_ON;
9874 		break;
9875 	case ATH12K_HW_STATE_RESTARTING:
9876 		ah->state = ATH12K_HW_STATE_RESTARTED;
9877 		break;
9878 	case ATH12K_HW_STATE_RESTARTED:
9879 	case ATH12K_HW_STATE_WEDGED:
9880 	case ATH12K_HW_STATE_ON:
9881 	case ATH12K_HW_STATE_TM:
9882 		ah->state = ATH12K_HW_STATE_OFF;
9883 
9884 		WARN_ON(1);
9885 		return -EINVAL;
9886 	}
9887 
9888 	for_each_ar(ah, ar, i) {
9889 		ret = ath12k_mac_start(ar);
9890 		if (ret) {
9891 			ah->state = ATH12K_HW_STATE_OFF;
9892 
9893 			ath12k_err(ar->ab, "fail to start mac operations in pdev idx %d ret %d\n",
9894 				   ar->pdev_idx, ret);
9895 			goto fail_start;
9896 		}
9897 	}
9898 
9899 	return 0;
9900 
9901 fail_start:
9902 	for (; i > 0; i--) {
9903 		ar = ath12k_ah_to_ar(ah, i - 1);
9904 		ath12k_mac_stop(ar);
9905 	}
9906 
9907 	return ret;
9908 }
9909 EXPORT_SYMBOL(ath12k_mac_op_start);
9910 
9911 int ath12k_mac_rfkill_config(struct ath12k *ar)
9912 {
9913 	struct ath12k_base *ab = ar->ab;
9914 	u32 param;
9915 	int ret;
9916 
9917 	if (ab->hw_params->rfkill_pin == 0)
9918 		return -EOPNOTSUPP;
9919 
9920 	ath12k_dbg(ab, ATH12K_DBG_MAC,
9921 		   "mac rfkill_pin %d rfkill_cfg %d rfkill_on_level %d",
9922 		   ab->hw_params->rfkill_pin, ab->hw_params->rfkill_cfg,
9923 		   ab->hw_params->rfkill_on_level);
9924 
9925 	param = u32_encode_bits(ab->hw_params->rfkill_on_level,
9926 				WMI_RFKILL_CFG_RADIO_LEVEL) |
9927 		u32_encode_bits(ab->hw_params->rfkill_pin,
9928 				WMI_RFKILL_CFG_GPIO_PIN_NUM) |
9929 		u32_encode_bits(ab->hw_params->rfkill_cfg,
9930 				WMI_RFKILL_CFG_PIN_AS_GPIO);
9931 
9932 	ret = ath12k_wmi_pdev_set_param(ar, WMI_PDEV_PARAM_HW_RFKILL_CONFIG,
9933 					param, ar->pdev->pdev_id);
9934 	if (ret) {
9935 		ath12k_warn(ab,
9936 			    "failed to set rfkill config 0x%x: %d\n",
9937 			    param, ret);
9938 		return ret;
9939 	}
9940 
9941 	return 0;
9942 }
9943 
9944 int ath12k_mac_rfkill_enable_radio(struct ath12k *ar, bool enable)
9945 {
9946 	enum wmi_rfkill_enable_radio param;
9947 	int ret;
9948 
9949 	if (enable)
9950 		param = WMI_RFKILL_ENABLE_RADIO_ON;
9951 	else
9952 		param = WMI_RFKILL_ENABLE_RADIO_OFF;
9953 
9954 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac %d rfkill enable %d",
9955 		   ar->pdev_idx, param);
9956 
9957 	ret = ath12k_wmi_pdev_set_param(ar, WMI_PDEV_PARAM_RFKILL_ENABLE,
9958 					param, ar->pdev->pdev_id);
9959 	if (ret) {
9960 		ath12k_warn(ar->ab, "failed to set rfkill enable param %d: %d\n",
9961 			    param, ret);
9962 		return ret;
9963 	}
9964 
9965 	return 0;
9966 }
9967 
9968 static void ath12k_mac_stop(struct ath12k *ar)
9969 {
9970 	struct ath12k_pdev_dp *dp_pdev = &ar->dp;
9971 	struct ath12k_hw *ah = ar->ah;
9972 	struct htt_ppdu_stats_info *ppdu_stats, *tmp;
9973 	struct ath12k_wmi_scan_chan_list_arg *arg;
9974 	int ret;
9975 
9976 	lockdep_assert_held(&ah->hw_mutex);
9977 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
9978 
9979 	ret = ath12k_mac_config_mon_status_default(ar, false);
9980 	if (ret && (ret != -EOPNOTSUPP))
9981 		ath12k_err(ar->ab, "failed to clear rx_filter for monitor status ring: (%d)\n",
9982 			   ret);
9983 
9984 	clear_bit(ATH12K_FLAG_CAC_RUNNING, &ar->dev_flags);
9985 
9986 	cancel_delayed_work_sync(&ar->scan.timeout);
9987 	wiphy_work_cancel(ath12k_ar_to_hw(ar)->wiphy, &ar->scan.vdev_clean_wk);
9988 	cancel_work_sync(&ar->regd_channel_update_work);
9989 	cancel_work_sync(&ar->regd_update_work);
9990 	cancel_work_sync(&ar->ab->rfkill_work);
9991 	cancel_work_sync(&ar->ab->update_11d_work);
9992 	ar->state_11d = ATH12K_11D_IDLE;
9993 	complete(&ar->completed_11d_scan);
9994 
9995 	spin_lock_bh(&dp_pdev->ppdu_list_lock);
9996 	list_for_each_entry_safe(ppdu_stats, tmp, &dp_pdev->ppdu_stats_info, list) {
9997 		list_del(&ppdu_stats->list);
9998 		kfree(ppdu_stats);
9999 	}
10000 	spin_unlock_bh(&dp_pdev->ppdu_list_lock);
10001 
10002 	spin_lock_bh(&ar->data_lock);
10003 	while ((arg = list_first_entry_or_null(&ar->regd_channel_update_queue,
10004 					       struct ath12k_wmi_scan_chan_list_arg,
10005 					       list))) {
10006 		list_del(&arg->list);
10007 		kfree(arg);
10008 	}
10009 	spin_unlock_bh(&ar->data_lock);
10010 
10011 	rcu_assign_pointer(ar->ab->pdevs_active[ar->pdev_idx], NULL);
10012 
10013 	synchronize_rcu();
10014 
10015 	atomic_set(&ar->num_pending_mgmt_tx, 0);
10016 
10017 	spin_lock_bh(&ar->data_lock);
10018 	ar->incumbent_signal_interference.handling_in_progress = false;
10019 	spin_unlock_bh(&ar->data_lock);
10020 }
10021 
10022 void ath12k_mac_op_stop(struct ieee80211_hw *hw, bool suspend)
10023 {
10024 	struct ath12k_hw *ah = ath12k_hw_to_ah(hw);
10025 	struct ath12k *ar;
10026 	int i;
10027 
10028 	lockdep_assert_wiphy(hw->wiphy);
10029 
10030 	ath12k_drain_tx(ah);
10031 
10032 	mutex_lock(&ah->hw_mutex);
10033 
10034 	ah->state = ATH12K_HW_STATE_OFF;
10035 
10036 	for_each_ar(ah, ar, i)
10037 		ath12k_mac_stop(ar);
10038 
10039 	mutex_unlock(&ah->hw_mutex);
10040 }
10041 EXPORT_SYMBOL(ath12k_mac_op_stop);
10042 
10043 static u8
10044 ath12k_mac_get_vdev_stats_id(struct ath12k_link_vif *arvif)
10045 {
10046 	struct ath12k_base *ab = arvif->ar->ab;
10047 	u8 vdev_stats_id = 0;
10048 
10049 	do {
10050 		if (ab->free_vdev_stats_id_map & (1LL << vdev_stats_id)) {
10051 			vdev_stats_id++;
10052 			if (vdev_stats_id >= ATH12K_MAX_VDEV_STATS_ID) {
10053 				vdev_stats_id = ATH12K_INVAL_VDEV_STATS_ID;
10054 				break;
10055 			}
10056 		} else {
10057 			ab->free_vdev_stats_id_map |= (1LL << vdev_stats_id);
10058 			break;
10059 		}
10060 	} while (vdev_stats_id);
10061 
10062 	arvif->vdev_stats_id = vdev_stats_id;
10063 	return vdev_stats_id;
10064 }
10065 
10066 static int ath12k_mac_setup_vdev_params_mbssid(struct ath12k_link_vif *arvif,
10067 					       u32 *flags, u32 *tx_vdev_id)
10068 {
10069 	struct ath12k_vif *ahvif = arvif->ahvif;
10070 	struct ieee80211_bss_conf *link_conf;
10071 	struct ath12k *ar = arvif->ar;
10072 	struct ath12k_link_vif *tx_arvif;
10073 
10074 	link_conf = ath12k_mac_get_link_bss_conf(arvif);
10075 	if (!link_conf) {
10076 		ath12k_warn(ar->ab, "unable to access bss link conf in set mbssid params for vif %pM link %u\n",
10077 			    ahvif->vif->addr, arvif->link_id);
10078 		return -ENOLINK;
10079 	}
10080 
10081 	tx_arvif = ath12k_mac_get_tx_arvif(arvif, link_conf);
10082 	if (!tx_arvif)
10083 		return 0;
10084 
10085 	if (link_conf->nontransmitted) {
10086 		if (ath12k_ar_to_hw(ar)->wiphy !=
10087 		    ath12k_ar_to_hw(tx_arvif->ar)->wiphy)
10088 			return -EINVAL;
10089 
10090 		*flags = WMI_VDEV_MBSSID_FLAGS_NON_TRANSMIT_AP;
10091 		*tx_vdev_id = tx_arvif->vdev_id;
10092 	} else if (tx_arvif == arvif) {
10093 		*flags = WMI_VDEV_MBSSID_FLAGS_TRANSMIT_AP;
10094 	} else {
10095 		return -EINVAL;
10096 	}
10097 
10098 	if (link_conf->ema_ap)
10099 		*flags |= WMI_VDEV_MBSSID_FLAGS_EMA_MODE;
10100 
10101 	return 0;
10102 }
10103 
10104 static int ath12k_mac_setup_vdev_create_arg(struct ath12k_link_vif *arvif,
10105 					    struct ath12k_wmi_vdev_create_arg *arg)
10106 {
10107 	struct ath12k *ar = arvif->ar;
10108 	struct ath12k_pdev *pdev = ar->pdev;
10109 	struct ath12k_vif *ahvif = arvif->ahvif;
10110 	int ret;
10111 
10112 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
10113 
10114 	arg->if_id = arvif->vdev_id;
10115 	arg->type = ahvif->vdev_type;
10116 	arg->subtype = ahvif->vdev_subtype;
10117 	arg->pdev_id = pdev->pdev_id;
10118 
10119 	arg->mbssid_flags = WMI_VDEV_MBSSID_FLAGS_NON_MBSSID_AP;
10120 	arg->mbssid_tx_vdev_id = 0;
10121 	if (!test_bit(WMI_TLV_SERVICE_MBSS_PARAM_IN_VDEV_START_SUPPORT,
10122 		      ar->ab->wmi_ab.svc_map)) {
10123 		ret = ath12k_mac_setup_vdev_params_mbssid(arvif,
10124 							  &arg->mbssid_flags,
10125 							  &arg->mbssid_tx_vdev_id);
10126 		if (ret)
10127 			return ret;
10128 	}
10129 
10130 	if (pdev->cap.supported_bands & WMI_HOST_WLAN_2GHZ_CAP) {
10131 		arg->chains[NL80211_BAND_2GHZ].tx = ar->num_tx_chains;
10132 		arg->chains[NL80211_BAND_2GHZ].rx = ar->num_rx_chains;
10133 	}
10134 	if (pdev->cap.supported_bands & WMI_HOST_WLAN_5GHZ_CAP) {
10135 		arg->chains[NL80211_BAND_5GHZ].tx = ar->num_tx_chains;
10136 		arg->chains[NL80211_BAND_5GHZ].rx = ar->num_rx_chains;
10137 	}
10138 	if (pdev->cap.supported_bands & WMI_HOST_WLAN_5GHZ_CAP &&
10139 	    ar->supports_6ghz) {
10140 		arg->chains[NL80211_BAND_6GHZ].tx = ar->num_tx_chains;
10141 		arg->chains[NL80211_BAND_6GHZ].rx = ar->num_rx_chains;
10142 	}
10143 
10144 	arg->if_stats_id = ath12k_mac_get_vdev_stats_id(arvif);
10145 
10146 	if (ath12k_mac_is_ml_arvif(arvif)) {
10147 		if (hweight16(ahvif->vif->valid_links) > ATH12K_WMI_MLO_MAX_LINKS) {
10148 			ath12k_warn(ar->ab, "too many MLO links during setting up vdev: %d",
10149 				    ahvif->vif->valid_links);
10150 			return -EINVAL;
10151 		}
10152 
10153 		ether_addr_copy(arg->mld_addr, ahvif->vif->addr);
10154 	}
10155 
10156 	return 0;
10157 }
10158 
10159 static void ath12k_mac_update_vif_offload(struct ath12k_link_vif *arvif)
10160 {
10161 	struct ath12k_vif *ahvif = arvif->ahvif;
10162 	struct ieee80211_vif *vif = ath12k_ahvif_to_vif(ahvif);
10163 	struct ath12k *ar = arvif->ar;
10164 	struct ath12k_base *ab = ar->ab;
10165 	u32 param_id, param_value;
10166 	int ret;
10167 
10168 	param_id = WMI_VDEV_PARAM_TX_ENCAP_TYPE;
10169 	if (vif->type != NL80211_IFTYPE_STATION &&
10170 	    vif->type != NL80211_IFTYPE_AP)
10171 		vif->offload_flags &= ~(IEEE80211_OFFLOAD_ENCAP_ENABLED |
10172 					IEEE80211_OFFLOAD_DECAP_ENABLED);
10173 
10174 	if (vif->offload_flags & IEEE80211_OFFLOAD_ENCAP_ENABLED) {
10175 		ahvif->dp_vif.tx_encap_type = ATH12K_HW_TXRX_ETHERNET;
10176 		vif->offload_flags |= IEEE80211_OFFLOAD_ENCAP_4ADDR;
10177 	} else if (test_bit(ATH12K_FLAG_RAW_MODE, &ab->dev_flags)) {
10178 		ahvif->dp_vif.tx_encap_type = ATH12K_HW_TXRX_RAW;
10179 	} else {
10180 		ahvif->dp_vif.tx_encap_type = ATH12K_HW_TXRX_NATIVE_WIFI;
10181 	}
10182 
10183 	ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
10184 					    param_id, ahvif->dp_vif.tx_encap_type);
10185 	if (ret) {
10186 		ath12k_warn(ab, "failed to set vdev %d tx encap mode: %d\n",
10187 			    arvif->vdev_id, ret);
10188 		vif->offload_flags &= ~IEEE80211_OFFLOAD_ENCAP_ENABLED;
10189 	}
10190 
10191 	param_id = WMI_VDEV_PARAM_RX_DECAP_TYPE;
10192 	if (vif->offload_flags & IEEE80211_OFFLOAD_DECAP_ENABLED)
10193 		param_value = ATH12K_HW_TXRX_ETHERNET;
10194 	else if (test_bit(ATH12K_FLAG_RAW_MODE, &ab->dev_flags))
10195 		param_value = ATH12K_HW_TXRX_RAW;
10196 	else
10197 		param_value = ATH12K_HW_TXRX_NATIVE_WIFI;
10198 
10199 	ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
10200 					    param_id, param_value);
10201 	if (ret) {
10202 		ath12k_warn(ab, "failed to set vdev %d rx decap mode: %d\n",
10203 			    arvif->vdev_id, ret);
10204 		vif->offload_flags &= ~IEEE80211_OFFLOAD_DECAP_ENABLED;
10205 	}
10206 }
10207 
10208 void ath12k_mac_op_update_vif_offload(struct ieee80211_hw *hw,
10209 				      struct ieee80211_vif *vif)
10210 {
10211 	struct ath12k_vif *ahvif = ath12k_vif_to_ahvif(vif);
10212 	struct ath12k_link_vif *arvif;
10213 	unsigned long links;
10214 	int link_id;
10215 
10216 	lockdep_assert_wiphy(hw->wiphy);
10217 
10218 	if (vif->valid_links) {
10219 		links = vif->valid_links;
10220 		for_each_set_bit(link_id, &links, IEEE80211_MLD_MAX_NUM_LINKS) {
10221 			arvif = wiphy_dereference(hw->wiphy, ahvif->link[link_id]);
10222 			if (!(arvif && arvif->ar))
10223 				continue;
10224 
10225 			ath12k_mac_update_vif_offload(arvif);
10226 		}
10227 
10228 		return;
10229 	}
10230 
10231 	ath12k_mac_update_vif_offload(&ahvif->deflink);
10232 }
10233 EXPORT_SYMBOL(ath12k_mac_op_update_vif_offload);
10234 
10235 static bool ath12k_mac_vif_ap_active_any(struct ath12k_base *ab)
10236 {
10237 	struct ath12k *ar;
10238 	struct ath12k_pdev *pdev;
10239 	struct ath12k_link_vif *arvif;
10240 	int i;
10241 
10242 	for (i = 0; i < ab->num_radios; i++) {
10243 		pdev = &ab->pdevs[i];
10244 		ar = pdev->ar;
10245 		list_for_each_entry(arvif, &ar->arvifs, list) {
10246 			if (arvif->is_up &&
10247 			    arvif->ahvif->vdev_type == WMI_VDEV_TYPE_AP)
10248 				return true;
10249 		}
10250 	}
10251 	return false;
10252 }
10253 
10254 void ath12k_mac_11d_scan_start(struct ath12k *ar, u32 vdev_id)
10255 {
10256 	struct wmi_11d_scan_start_arg arg;
10257 	int ret;
10258 
10259 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
10260 
10261 	if (ar->regdom_set_by_user)
10262 		goto fin;
10263 
10264 	if (ar->vdev_id_11d_scan != ATH12K_11D_INVALID_VDEV_ID)
10265 		goto fin;
10266 
10267 	if (!test_bit(WMI_TLV_SERVICE_11D_OFFLOAD, ar->ab->wmi_ab.svc_map))
10268 		goto fin;
10269 
10270 	if (ath12k_mac_vif_ap_active_any(ar->ab))
10271 		goto fin;
10272 
10273 	arg.vdev_id = vdev_id;
10274 	arg.start_interval_msec = 0;
10275 	arg.scan_period_msec = ATH12K_SCAN_11D_INTERVAL;
10276 
10277 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
10278 		   "mac start 11d scan for vdev %d\n", vdev_id);
10279 
10280 	ret = ath12k_wmi_send_11d_scan_start_cmd(ar, &arg);
10281 	if (ret) {
10282 		ath12k_warn(ar->ab, "failed to start 11d scan vdev %d ret: %d\n",
10283 			    vdev_id, ret);
10284 	} else {
10285 		ar->vdev_id_11d_scan = vdev_id;
10286 		if (ar->state_11d == ATH12K_11D_PREPARING)
10287 			ar->state_11d = ATH12K_11D_RUNNING;
10288 	}
10289 
10290 fin:
10291 	if (ar->state_11d == ATH12K_11D_PREPARING) {
10292 		ar->state_11d = ATH12K_11D_IDLE;
10293 		complete(&ar->completed_11d_scan);
10294 	}
10295 }
10296 
10297 void ath12k_mac_11d_scan_stop(struct ath12k *ar)
10298 {
10299 	int ret;
10300 	u32 vdev_id;
10301 
10302 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
10303 
10304 	if (!test_bit(WMI_TLV_SERVICE_11D_OFFLOAD, ar->ab->wmi_ab.svc_map))
10305 		return;
10306 
10307 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac stop 11d for vdev %d\n",
10308 		   ar->vdev_id_11d_scan);
10309 
10310 	if (ar->state_11d == ATH12K_11D_PREPARING) {
10311 		ar->state_11d = ATH12K_11D_IDLE;
10312 		complete(&ar->completed_11d_scan);
10313 	}
10314 
10315 	if (ar->vdev_id_11d_scan != ATH12K_11D_INVALID_VDEV_ID) {
10316 		vdev_id = ar->vdev_id_11d_scan;
10317 
10318 		ret = ath12k_wmi_send_11d_scan_stop_cmd(ar, vdev_id);
10319 		if (ret) {
10320 			ath12k_warn(ar->ab,
10321 				    "failed to stopt 11d scan vdev %d ret: %d\n",
10322 				    vdev_id, ret);
10323 		} else {
10324 			ar->vdev_id_11d_scan = ATH12K_11D_INVALID_VDEV_ID;
10325 			ar->state_11d = ATH12K_11D_IDLE;
10326 			complete(&ar->completed_11d_scan);
10327 		}
10328 	}
10329 }
10330 
10331 void ath12k_mac_11d_scan_stop_all(struct ath12k_base *ab)
10332 {
10333 	struct ath12k *ar;
10334 	struct ath12k_pdev *pdev;
10335 	int i;
10336 
10337 	ath12k_dbg(ab, ATH12K_DBG_MAC, "mac stop soc 11d scan\n");
10338 
10339 	for (i = 0; i < ab->num_radios; i++) {
10340 		pdev = &ab->pdevs[i];
10341 		ar = pdev->ar;
10342 
10343 		ath12k_mac_11d_scan_stop(ar);
10344 	}
10345 }
10346 
10347 static void ath12k_mac_determine_vdev_type(struct ieee80211_vif *vif,
10348 					   struct ath12k_vif *ahvif)
10349 {
10350 	ahvif->vdev_subtype = WMI_VDEV_SUBTYPE_NONE;
10351 
10352 	switch (vif->type) {
10353 	case NL80211_IFTYPE_UNSPECIFIED:
10354 	case NL80211_IFTYPE_STATION:
10355 		ahvif->vdev_type = WMI_VDEV_TYPE_STA;
10356 
10357 		if (vif->p2p)
10358 			ahvif->vdev_subtype = WMI_VDEV_SUBTYPE_P2P_CLIENT;
10359 
10360 		break;
10361 	case NL80211_IFTYPE_MESH_POINT:
10362 		ahvif->vdev_subtype = WMI_VDEV_SUBTYPE_MESH_11S;
10363 		fallthrough;
10364 	case NL80211_IFTYPE_AP:
10365 		ahvif->vdev_type = WMI_VDEV_TYPE_AP;
10366 
10367 		if (vif->p2p)
10368 			ahvif->vdev_subtype = WMI_VDEV_SUBTYPE_P2P_GO;
10369 
10370 		break;
10371 	case NL80211_IFTYPE_MONITOR:
10372 		ahvif->vdev_type = WMI_VDEV_TYPE_MONITOR;
10373 		break;
10374 	case NL80211_IFTYPE_P2P_DEVICE:
10375 		ahvif->vdev_type = WMI_VDEV_TYPE_STA;
10376 		ahvif->vdev_subtype = WMI_VDEV_SUBTYPE_P2P_DEVICE;
10377 		break;
10378 	default:
10379 		WARN_ON(1);
10380 		break;
10381 	}
10382 }
10383 
10384 int ath12k_mac_vdev_create(struct ath12k *ar, struct ath12k_link_vif *arvif)
10385 {
10386 	struct ath12k_hw *ah = ar->ah;
10387 	struct ath12k_base *ab = ar->ab;
10388 	struct ieee80211_hw *hw = ah->hw;
10389 	struct ath12k_vif *ahvif = arvif->ahvif;
10390 	struct ieee80211_vif *vif = ath12k_ahvif_to_vif(ahvif);
10391 	struct wireless_dev *wdev = ieee80211_vif_to_wdev(vif);
10392 	struct ath12k_wmi_vdev_create_arg vdev_arg = {};
10393 	struct ath12k_wmi_peer_create_arg peer_param = {};
10394 	struct ieee80211_bss_conf *link_conf = NULL;
10395 	u32 param_id, param_value;
10396 	u16 nss;
10397 	int i;
10398 	int ret, vdev_id;
10399 	u8 link_id;
10400 	struct ath12k_dp_link_vif *dp_link_vif = NULL;
10401 	struct ath12k_dp_peer_create_params params = {};
10402 	bool dp_peer_created = false;
10403 
10404 	lockdep_assert_wiphy(hw->wiphy);
10405 
10406 	/* In NO_VIRTUAL_MONITOR, its necessary to restrict only one monitor
10407 	 * interface in each radio
10408 	 */
10409 	if (vif->type == NL80211_IFTYPE_MONITOR && ar->monitor_vdev_created)
10410 		return -EINVAL;
10411 
10412 	if (ar->num_created_vdevs >= TARGET_NUM_VDEVS(ab)) {
10413 		ath12k_warn(ab, "failed to create vdev, reached max vdev limit %d\n",
10414 			    TARGET_NUM_VDEVS(ab));
10415 		return -ENOSPC;
10416 	}
10417 
10418 	link_id = arvif->link_id;
10419 
10420 	if (link_id < IEEE80211_MLD_MAX_NUM_LINKS) {
10421 		link_conf = wiphy_dereference(hw->wiphy, vif->link_conf[link_id]);
10422 		if (!link_conf) {
10423 			ath12k_warn(ar->ab, "unable to access bss link conf in vdev create for vif %pM link %u\n",
10424 				    vif->addr, arvif->link_id);
10425 			return -ENOLINK;
10426 		}
10427 	}
10428 
10429 	if (link_conf)
10430 		memcpy(arvif->bssid, link_conf->addr, ETH_ALEN);
10431 	else
10432 		memcpy(arvif->bssid, vif->addr, ETH_ALEN);
10433 
10434 	arvif->ar = ar;
10435 	vdev_id = __ffs64(ab->free_vdev_map);
10436 	arvif->vdev_id = vdev_id;
10437 	if (vif->type == NL80211_IFTYPE_MONITOR)
10438 		ar->monitor_vdev_id = vdev_id;
10439 
10440 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac vdev create id %d type %d subtype %d map %llx\n",
10441 		   arvif->vdev_id, ahvif->vdev_type, ahvif->vdev_subtype,
10442 		   ab->free_vdev_map);
10443 
10444 	vif->cab_queue = arvif->vdev_id % (ATH12K_HW_MAX_QUEUES - 1);
10445 	for (i = 0; i < ARRAY_SIZE(vif->hw_queue); i++)
10446 		vif->hw_queue[i] = i % (ATH12K_HW_MAX_QUEUES - 1);
10447 
10448 	ret = ath12k_mac_setup_vdev_create_arg(arvif, &vdev_arg);
10449 	if (ret) {
10450 		ath12k_warn(ab, "failed to create vdev parameters %d: %d\n",
10451 			    arvif->vdev_id, ret);
10452 		goto err;
10453 	}
10454 
10455 	ret = ath12k_wmi_vdev_create(ar, arvif->bssid, &vdev_arg);
10456 	if (ret) {
10457 		ath12k_warn(ab, "failed to create WMI vdev %d: %d\n",
10458 			    arvif->vdev_id, ret);
10459 		goto err;
10460 	}
10461 
10462 	ar->num_created_vdevs++;
10463 	arvif->is_created = true;
10464 	ath12k_dbg(ab, ATH12K_DBG_MAC, "vdev %pM created, vdev_id %d\n",
10465 		   vif->addr, arvif->vdev_id);
10466 	ar->allocated_vdev_map |= 1LL << arvif->vdev_id;
10467 	ab->free_vdev_map &= ~(1LL << arvif->vdev_id);
10468 
10469 	spin_lock_bh(&ar->data_lock);
10470 	list_add(&arvif->list, &ar->arvifs);
10471 	spin_unlock_bh(&ar->data_lock);
10472 
10473 	ath12k_mac_update_vif_offload(arvif);
10474 
10475 	nss = hweight32(ar->cfg_tx_chainmask) ? : 1;
10476 	ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
10477 					    WMI_VDEV_PARAM_NSS, nss);
10478 	if (ret) {
10479 		ath12k_warn(ab, "failed to set vdev %d chainmask 0x%x, nss %d :%d\n",
10480 			    arvif->vdev_id, ar->cfg_tx_chainmask, nss, ret);
10481 		goto err_vdev_del;
10482 	}
10483 
10484 	dp_link_vif = ath12k_dp_vif_to_dp_link_vif(&ahvif->dp_vif, arvif->link_id);
10485 
10486 	dp_link_vif->vdev_id = arvif->vdev_id;
10487 	dp_link_vif->lmac_id = ar->lmac_id;
10488 	dp_link_vif->pdev_idx = ar->pdev_idx;
10489 
10490 	switch (ahvif->vdev_type) {
10491 	case WMI_VDEV_TYPE_AP:
10492 		params.ucast_ra_only = true;
10493 
10494 		if (arvif->link_id < IEEE80211_MLD_MAX_NUM_LINKS) {
10495 			ret = ath12k_dp_peer_create(&ah->dp_hw, arvif->bssid, &params);
10496 			if (ret) {
10497 				ath12k_warn(ab, "failed to vdev %d create dp_peer for AP: %d\n",
10498 					    arvif->vdev_id, ret);
10499 				goto err_vdev_del;
10500 			}
10501 			dp_peer_created = true;
10502 		}
10503 
10504 		peer_param.vdev_id = arvif->vdev_id;
10505 		peer_param.peer_addr = arvif->bssid;
10506 		peer_param.peer_type = WMI_PEER_TYPE_DEFAULT;
10507 		ret = ath12k_peer_create(ar, arvif, NULL, &peer_param);
10508 		if (ret) {
10509 			ath12k_warn(ab, "failed to vdev %d create peer for AP: %d\n",
10510 				    arvif->vdev_id, ret);
10511 			goto err_dp_peer_del;
10512 		}
10513 
10514 		ret = ath12k_mac_set_kickout(arvif);
10515 		if (ret) {
10516 			ath12k_warn(ar->ab, "failed to set vdev %i kickout parameters: %d\n",
10517 				    arvif->vdev_id, ret);
10518 			goto err_peer_del;
10519 		}
10520 		ath12k_mac_11d_scan_stop_all(ar->ab);
10521 		break;
10522 	case WMI_VDEV_TYPE_STA:
10523 		param_id = WMI_STA_PS_PARAM_RX_WAKE_POLICY;
10524 		param_value = WMI_STA_PS_RX_WAKE_POLICY_WAKE;
10525 		ret = ath12k_wmi_set_sta_ps_param(ar, arvif->vdev_id,
10526 						  param_id, param_value);
10527 		if (ret) {
10528 			ath12k_warn(ar->ab, "failed to set vdev %d RX wake policy: %d\n",
10529 				    arvif->vdev_id, ret);
10530 			goto err_peer_del;
10531 		}
10532 
10533 		param_id = WMI_STA_PS_PARAM_TX_WAKE_THRESHOLD;
10534 		param_value = WMI_STA_PS_TX_WAKE_THRESHOLD_ALWAYS;
10535 		ret = ath12k_wmi_set_sta_ps_param(ar, arvif->vdev_id,
10536 						  param_id, param_value);
10537 		if (ret) {
10538 			ath12k_warn(ar->ab, "failed to set vdev %d TX wake threshold: %d\n",
10539 				    arvif->vdev_id, ret);
10540 			goto err_peer_del;
10541 		}
10542 
10543 		param_id = WMI_STA_PS_PARAM_PSPOLL_COUNT;
10544 		param_value = WMI_STA_PS_PSPOLL_COUNT_NO_MAX;
10545 		ret = ath12k_wmi_set_sta_ps_param(ar, arvif->vdev_id,
10546 						  param_id, param_value);
10547 		if (ret) {
10548 			ath12k_warn(ar->ab, "failed to set vdev %d pspoll count: %d\n",
10549 				    arvif->vdev_id, ret);
10550 			goto err_peer_del;
10551 		}
10552 
10553 		ret = ath12k_wmi_pdev_set_ps_mode(ar, arvif->vdev_id, false);
10554 		if (ret) {
10555 			ath12k_warn(ar->ab, "failed to disable vdev %d ps mode: %d\n",
10556 				    arvif->vdev_id, ret);
10557 			goto err_peer_del;
10558 		}
10559 
10560 		/*
10561 		 * There could be race condition in firmware for the station
10562 		 * interface between enabling 4-address peer WMI param and
10563 		 * sending 4-address frame (NULL or EAPOL via TCL).
10564 		 * Make the station as WDS while bringup itself
10565 		 * to avoid the race condition
10566 		 */
10567 		if (vif->type == NL80211_IFTYPE_STATION &&
10568 		    (wdev && wdev->use_4addr)) {
10569 			ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
10570 							    WMI_VDEV_PARAM_WDS,
10571 							    1);
10572 			if (ret) {
10573 				ath12k_warn(ar->ab, "failed to set WDS vdev param: %d\n",
10574 					    ret);
10575 				goto err_peer_del;
10576 			}
10577 			arvif->set_wds_vdev_param = true;
10578 		}
10579 
10580 		if (test_bit(WMI_TLV_SERVICE_11D_OFFLOAD, ab->wmi_ab.svc_map) &&
10581 		    ahvif->vdev_type == WMI_VDEV_TYPE_STA &&
10582 		    ahvif->vdev_subtype == WMI_VDEV_SUBTYPE_NONE) {
10583 			reinit_completion(&ar->completed_11d_scan);
10584 			ar->state_11d = ATH12K_11D_PREPARING;
10585 		}
10586 		break;
10587 	case WMI_VDEV_TYPE_MONITOR:
10588 		ar->monitor_vdev_created = true;
10589 		break;
10590 	default:
10591 		break;
10592 	}
10593 
10594 	if (link_conf)
10595 		arvif->txpower = link_conf->txpower;
10596 	else
10597 		arvif->txpower = NL80211_TX_POWER_AUTOMATIC;
10598 
10599 	ret = ath12k_mac_txpower_recalc(ar);
10600 	if (ret)
10601 		goto err_peer_del;
10602 
10603 	param_id = WMI_VDEV_PARAM_RTS_THRESHOLD;
10604 	param_value = hw->wiphy->rts_threshold;
10605 	ar->rts_threshold = param_value;
10606 	ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
10607 					    param_id, param_value);
10608 	if (ret) {
10609 		ath12k_warn(ar->ab, "failed to set rts threshold for vdev %d: %d\n",
10610 			    arvif->vdev_id, ret);
10611 	}
10612 
10613 	ath12k_dp_vdev_tx_attach(ar, arvif);
10614 
10615 	return ret;
10616 
10617 err_peer_del:
10618 	if (ahvif->vdev_type == WMI_VDEV_TYPE_AP) {
10619 		reinit_completion(&ar->peer_delete_done);
10620 
10621 		ret = ath12k_wmi_send_peer_delete_cmd(ar, arvif->bssid,
10622 						      arvif->vdev_id);
10623 		if (ret) {
10624 			ath12k_warn(ar->ab, "failed to delete peer vdev_id %d addr %pM\n",
10625 				    arvif->vdev_id, arvif->bssid);
10626 			goto err_dp_peer_del;
10627 		}
10628 
10629 		ret = ath12k_wait_for_peer_delete_done(ar, arvif->vdev_id,
10630 						       arvif->bssid);
10631 		if (ret)
10632 			goto err_dp_peer_del;
10633 
10634 		ar->num_peers--;
10635 	}
10636 
10637 err_dp_peer_del:
10638 	if (dp_peer_created)
10639 		ath12k_dp_peer_delete(&ah->dp_hw, arvif->bssid, NULL);
10640 
10641 err_vdev_del:
10642 	if (ahvif->vdev_type == WMI_VDEV_TYPE_MONITOR) {
10643 		ar->monitor_vdev_id = -1;
10644 		ar->monitor_vdev_created = false;
10645 	}
10646 
10647 	ath12k_wmi_vdev_delete(ar, arvif->vdev_id);
10648 	ar->num_created_vdevs--;
10649 	ar->allocated_vdev_map &= ~(1LL << arvif->vdev_id);
10650 	ab->free_vdev_map |= 1LL << arvif->vdev_id;
10651 	ab->free_vdev_stats_id_map &= ~(1LL << arvif->vdev_stats_id);
10652 	spin_lock_bh(&ar->data_lock);
10653 	list_del(&arvif->list);
10654 	spin_unlock_bh(&ar->data_lock);
10655 
10656 err:
10657 	arvif->is_created = false;
10658 	arvif->ar = NULL;
10659 	return ret;
10660 }
10661 
10662 static void ath12k_mac_vif_flush_key_cache(struct ath12k_link_vif *arvif)
10663 {
10664 	struct ath12k_key_conf *key_conf, *tmp;
10665 	struct ath12k_vif *ahvif = arvif->ahvif;
10666 	struct ath12k_hw *ah = ahvif->ah;
10667 	struct ath12k_sta *ahsta;
10668 	struct ath12k_link_sta *arsta;
10669 	struct ath12k_vif_cache *cache = ahvif->cache[arvif->link_id];
10670 	int ret;
10671 
10672 	lockdep_assert_wiphy(ah->hw->wiphy);
10673 
10674 	list_for_each_entry_safe(key_conf, tmp, &cache->key_conf.list, list) {
10675 		arsta = NULL;
10676 		if (key_conf->sta) {
10677 			ahsta = ath12k_sta_to_ahsta(key_conf->sta);
10678 			arsta = wiphy_dereference(ah->hw->wiphy,
10679 						  ahsta->link[arvif->link_id]);
10680 			if (!arsta)
10681 				goto free_cache;
10682 		}
10683 
10684 		ret = ath12k_mac_set_key(arvif->ar, key_conf->cmd,
10685 					 arvif, arsta,
10686 					 key_conf->key);
10687 		if (ret)
10688 			ath12k_warn(arvif->ar->ab, "unable to apply set key param to vdev %d ret %d\n",
10689 				    arvif->vdev_id, ret);
10690 free_cache:
10691 		list_del(&key_conf->list);
10692 		kfree(key_conf);
10693 	}
10694 }
10695 
10696 static void ath12k_mac_vif_cache_flush(struct ath12k *ar, struct ath12k_link_vif *arvif)
10697 {
10698 	struct ath12k_vif *ahvif = arvif->ahvif;
10699 	struct ieee80211_vif *vif = ath12k_ahvif_to_vif(ahvif);
10700 	struct ath12k_vif_cache *cache = ahvif->cache[arvif->link_id];
10701 	struct ath12k_base *ab = ar->ab;
10702 	struct ieee80211_bss_conf *link_conf;
10703 
10704 	int ret;
10705 
10706 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
10707 
10708 	if (!cache)
10709 		return;
10710 
10711 	if (cache->tx_conf.changed) {
10712 		ret = ath12k_mac_conf_tx(arvif, cache->tx_conf.ac,
10713 					 &cache->tx_conf.tx_queue_params);
10714 		if (ret)
10715 			ath12k_warn(ab,
10716 				    "unable to apply tx config parameters to vdev %d\n",
10717 				    ret);
10718 	}
10719 
10720 	if (cache->bss_conf_changed) {
10721 		link_conf = ath12k_mac_get_link_bss_conf(arvif);
10722 		if (!link_conf) {
10723 			ath12k_warn(ar->ab, "unable to access bss link conf in cache flush for vif %pM link %u\n",
10724 				    vif->addr, arvif->link_id);
10725 			return;
10726 		}
10727 		ath12k_mac_bss_info_changed(ar, arvif, link_conf,
10728 					    cache->bss_conf_changed);
10729 	}
10730 
10731 	if (!list_empty(&cache->key_conf.list))
10732 		ath12k_mac_vif_flush_key_cache(arvif);
10733 
10734 	ath12k_ahvif_put_link_cache(ahvif, arvif->link_id);
10735 }
10736 
10737 static struct ath12k *ath12k_mac_assign_vif_to_vdev(struct ieee80211_hw *hw,
10738 						    struct ath12k_link_vif *arvif,
10739 						    struct ieee80211_chanctx_conf *ctx)
10740 {
10741 	struct ath12k_vif *ahvif = arvif->ahvif;
10742 	struct ieee80211_vif *vif = ath12k_ahvif_to_vif(ahvif);
10743 	struct ath12k_link_vif *scan_arvif;
10744 	struct ath12k_hw *ah = hw->priv;
10745 	struct ath12k *ar;
10746 	struct ath12k_base *ab;
10747 	u8 link_id = arvif->link_id, scan_link_id;
10748 	unsigned long scan_link_map;
10749 	int ret;
10750 
10751 	lockdep_assert_wiphy(hw->wiphy);
10752 
10753 	if (ah->num_radio == 1)
10754 		ar = ah->radio;
10755 	else if (ctx)
10756 		ar = ath12k_get_ar_by_ctx(hw, ctx);
10757 	else
10758 		return NULL;
10759 
10760 	if (!ar)
10761 		return NULL;
10762 
10763 	/* cleanup the scan vdev if we are done scan on that ar
10764 	 * and now we want to create for actual usage.
10765 	 */
10766 	if (ieee80211_vif_is_mld(vif)) {
10767 		scan_link_map = ahvif->links_map & ATH12K_SCAN_LINKS_MASK;
10768 		for_each_set_bit(scan_link_id, &scan_link_map, ATH12K_NUM_MAX_LINKS) {
10769 			scan_arvif = wiphy_dereference(hw->wiphy,
10770 						       ahvif->link[scan_link_id]);
10771 			if (scan_arvif && scan_arvif->ar == ar) {
10772 				ar->scan.arvif = NULL;
10773 				ath12k_mac_remove_link_interface(hw, scan_arvif);
10774 				ath12k_mac_unassign_link_vif(scan_arvif);
10775 				break;
10776 			}
10777 		}
10778 	}
10779 
10780 	if (arvif->ar) {
10781 		/* This is not expected really */
10782 		if (WARN_ON(!arvif->is_created)) {
10783 			arvif->ar = NULL;
10784 			return NULL;
10785 		}
10786 
10787 		if (ah->num_radio == 1)
10788 			return arvif->ar;
10789 
10790 		/* This can happen as scan vdev gets created during multiple scans
10791 		 * across different radios before a vdev is brought up in
10792 		 * a certain radio.
10793 		 */
10794 		if (ar != arvif->ar) {
10795 			if (WARN_ON(arvif->is_started))
10796 				return NULL;
10797 
10798 			ath12k_mac_remove_link_interface(hw, arvif);
10799 			ath12k_mac_unassign_link_vif(arvif);
10800 		}
10801 	}
10802 
10803 	ab = ar->ab;
10804 
10805 	/* Assign arvif again here since previous radio switch block
10806 	 * would've unassigned and cleared it.
10807 	 */
10808 	arvif = ath12k_mac_assign_link_vif(ah, vif, link_id);
10809 	if (vif->type == NL80211_IFTYPE_AP &&
10810 	    ar->num_peers > (ar->max_num_peers - 1)) {
10811 		ath12k_warn(ab, "failed to create vdev due to insufficient peer entry resource in firmware\n");
10812 		goto unlock;
10813 	}
10814 
10815 	if (arvif->is_created)
10816 		goto flush;
10817 
10818 	ret = ath12k_mac_vdev_create(ar, arvif);
10819 	if (ret) {
10820 		ath12k_warn(ab, "failed to create vdev %pM ret %d", vif->addr, ret);
10821 		goto unlock;
10822 	}
10823 
10824 flush:
10825 	/* If the vdev is created during channel assign and not during
10826 	 * add_interface(), Apply any parameters for the vdev which were received
10827 	 * after add_interface, corresponding to this vif.
10828 	 */
10829 	ath12k_mac_vif_cache_flush(ar, arvif);
10830 unlock:
10831 	return arvif->ar;
10832 }
10833 
10834 int ath12k_mac_op_add_interface(struct ieee80211_hw *hw,
10835 				struct ieee80211_vif *vif)
10836 {
10837 	struct ath12k_hw *ah = ath12k_hw_to_ah(hw);
10838 	struct ath12k_vif *ahvif = ath12k_vif_to_ahvif(vif);
10839 	struct ath12k_reg_info *reg_info;
10840 	struct ath12k_link_vif *arvif;
10841 	struct ath12k_base *ab;
10842 	struct ath12k *ar;
10843 	int i;
10844 
10845 	lockdep_assert_wiphy(hw->wiphy);
10846 
10847 	memset(ahvif, 0, sizeof(*ahvif));
10848 
10849 	ahvif->ah = ah;
10850 	ahvif->vif = vif;
10851 	arvif = &ahvif->deflink;
10852 
10853 	ath12k_mac_init_arvif(ahvif, arvif, -1);
10854 
10855 	/* Allocate Default Queue now and reassign during actual vdev create */
10856 	vif->cab_queue = ATH12K_HW_DEFAULT_QUEUE;
10857 	for (i = 0; i < ARRAY_SIZE(vif->hw_queue); i++)
10858 		vif->hw_queue[i] = ATH12K_HW_DEFAULT_QUEUE;
10859 
10860 	vif->driver_flags |= IEEE80211_VIF_SUPPORTS_UAPSD;
10861 
10862 	ath12k_mac_determine_vdev_type(vif, ahvif);
10863 
10864 	for_each_ar(ah, ar, i) {
10865 		if (!ath12k_wmi_supports_6ghz_cc_ext(ar))
10866 			continue;
10867 
10868 		ab = ar->ab;
10869 		reg_info = ab->reg_info[ar->pdev_idx];
10870 		ath12k_dbg(ab, ATH12K_DBG_MAC, "interface added to change reg rules\n");
10871 		ah->regd_updated = false;
10872 		ath12k_reg_handle_chan_list(ab, reg_info, ahvif->vdev_type,
10873 					    IEEE80211_REG_UNSET_AP);
10874 		break;
10875 	}
10876 
10877 	/* Defer vdev creation until assign_chanctx or hw_scan is initiated as driver
10878 	 * will not know if this interface is an ML vif at this point.
10879 	 */
10880 	return 0;
10881 }
10882 EXPORT_SYMBOL(ath12k_mac_op_add_interface);
10883 
10884 static void ath12k_mac_vif_unref(struct ath12k_dp *dp, struct ieee80211_vif *vif)
10885 {
10886 	struct ath12k_tx_desc_info *tx_desc_info;
10887 	struct ath12k_skb_cb *skb_cb;
10888 	struct sk_buff *skb;
10889 	int i;
10890 
10891 	for (i = 0; i < ATH12K_HW_MAX_QUEUES; i++) {
10892 		spin_lock_bh(&dp->tx_desc_lock[i]);
10893 
10894 		list_for_each_entry(tx_desc_info, &dp->tx_desc_used_list[i],
10895 				    list) {
10896 			skb = tx_desc_info->skb;
10897 			if (!skb)
10898 				continue;
10899 
10900 			skb_cb = ATH12K_SKB_CB(skb);
10901 			if (skb_cb->vif == vif)
10902 				skb_cb->vif = NULL;
10903 		}
10904 
10905 		spin_unlock_bh(&dp->tx_desc_lock[i]);
10906 	}
10907 }
10908 
10909 static int ath12k_mac_vdev_delete(struct ath12k *ar, struct ath12k_link_vif *arvif)
10910 {
10911 	struct ath12k_vif *ahvif = arvif->ahvif;
10912 	struct ieee80211_vif *vif = ath12k_ahvif_to_vif(ahvif);
10913 	struct ath12k_dp_link_vif *dp_link_vif;
10914 	struct ath12k_base *ab = ar->ab;
10915 	unsigned long time_left;
10916 	int ret;
10917 
10918 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
10919 
10920 	reinit_completion(&ar->vdev_delete_done);
10921 
10922 	ret = ath12k_wmi_vdev_delete(ar, arvif->vdev_id);
10923 	if (ret) {
10924 		ath12k_warn(ab, "failed to delete WMI vdev %d: %d\n",
10925 			    arvif->vdev_id, ret);
10926 		goto err_vdev_del;
10927 	}
10928 
10929 	time_left = wait_for_completion_timeout(&ar->vdev_delete_done,
10930 						ATH12K_VDEV_DELETE_TIMEOUT_HZ);
10931 	if (time_left == 0) {
10932 		ath12k_warn(ab, "Timeout in receiving vdev delete response\n");
10933 		goto err_vdev_del;
10934 	}
10935 
10936 	ab->free_vdev_map |= 1LL << arvif->vdev_id;
10937 	ar->allocated_vdev_map &= ~(1LL << arvif->vdev_id);
10938 	ar->num_created_vdevs--;
10939 
10940 	if (ahvif->vdev_type == WMI_VDEV_TYPE_MONITOR) {
10941 		ar->monitor_vdev_id = -1;
10942 		ar->monitor_vdev_created = false;
10943 	}
10944 
10945 	ath12k_dbg(ab, ATH12K_DBG_MAC, "vdev %pM deleted, vdev_id %d\n",
10946 		   vif->addr, arvif->vdev_id);
10947 
10948 err_vdev_del:
10949 	spin_lock_bh(&ar->data_lock);
10950 	list_del(&arvif->list);
10951 	spin_unlock_bh(&ar->data_lock);
10952 
10953 	ath12k_peer_cleanup(ar, arvif->vdev_id);
10954 	ath12k_ahvif_put_link_cache(ahvif, arvif->link_id);
10955 
10956 	idr_for_each(&ar->txmgmt_idr,
10957 		     ath12k_mac_vif_txmgmt_idr_remove, vif);
10958 
10959 	ath12k_mac_vif_unref(ath12k_ab_to_dp(ab), vif);
10960 
10961 	dp_link_vif = ath12k_dp_vif_to_dp_link_vif(&ahvif->dp_vif, arvif->link_id);
10962 	ath12k_dp_tx_put_bank_profile(ath12k_ab_to_dp(ab), dp_link_vif->bank_id);
10963 
10964 	/* Recalc txpower for remaining vdev */
10965 	ath12k_mac_txpower_recalc(ar);
10966 
10967 	/* TODO: recal traffic pause state based on the available vdevs */
10968 	arvif->is_created = false;
10969 	arvif->ar = NULL;
10970 
10971 	return ret;
10972 }
10973 
10974 void ath12k_mac_op_remove_interface(struct ieee80211_hw *hw,
10975 				    struct ieee80211_vif *vif)
10976 {
10977 	struct ath12k_vif *ahvif = ath12k_vif_to_ahvif(vif);
10978 	struct ath12k_link_vif *arvif;
10979 	struct ath12k *ar;
10980 	u8 link_id;
10981 
10982 	lockdep_assert_wiphy(hw->wiphy);
10983 
10984 	for (link_id = 0; link_id < ATH12K_NUM_MAX_LINKS; link_id++) {
10985 		/* if we cached some config but never received assign chanctx,
10986 		 * free the allocated cache.
10987 		 */
10988 		ath12k_ahvif_put_link_cache(ahvif, link_id);
10989 		arvif = wiphy_dereference(hw->wiphy, ahvif->link[link_id]);
10990 		if (!arvif || !arvif->is_created)
10991 			continue;
10992 
10993 		ar = arvif->ar;
10994 
10995 		/* Scan abortion is in progress since before this, cancel_hw_scan()
10996 		 * is expected to be executed. Since link is anyways going to be removed
10997 		 * now, just cancel the worker and send the scan aborted to user space
10998 		 */
10999 		if (ar->scan.arvif == arvif) {
11000 			wiphy_work_cancel(hw->wiphy, &ar->scan.vdev_clean_wk);
11001 
11002 			spin_lock_bh(&ar->data_lock);
11003 			ar->scan.arvif = NULL;
11004 			if (!ar->scan.is_roc) {
11005 				struct cfg80211_scan_info info = {
11006 					.aborted = true,
11007 				};
11008 
11009 				ath12k_mac_scan_send_complete(ar, &info);
11010 			}
11011 
11012 			ar->scan.state = ATH12K_SCAN_IDLE;
11013 			ar->scan_channel = NULL;
11014 			ar->scan.roc_freq = 0;
11015 			spin_unlock_bh(&ar->data_lock);
11016 		}
11017 
11018 		ath12k_mac_remove_link_interface(hw, arvif);
11019 		ath12k_mac_unassign_link_vif(arvif);
11020 	}
11021 }
11022 EXPORT_SYMBOL(ath12k_mac_op_remove_interface);
11023 
11024 /* FIXME: Has to be verified. */
11025 #define SUPPORTED_FILTERS			\
11026 	(FIF_ALLMULTI |				\
11027 	FIF_CONTROL |				\
11028 	FIF_PSPOLL |				\
11029 	FIF_OTHER_BSS |				\
11030 	FIF_BCN_PRBRESP_PROMISC |		\
11031 	FIF_PROBE_REQ |				\
11032 	FIF_FCSFAIL)
11033 
11034 void ath12k_mac_op_configure_filter(struct ieee80211_hw *hw,
11035 				    unsigned int changed_flags,
11036 				    unsigned int *total_flags,
11037 				    u64 multicast)
11038 {
11039 	struct ath12k_hw *ah = ath12k_hw_to_ah(hw);
11040 	struct ath12k *ar;
11041 
11042 	lockdep_assert_wiphy(hw->wiphy);
11043 
11044 	ar = ath12k_ah_to_ar(ah, 0);
11045 
11046 	*total_flags &= SUPPORTED_FILTERS;
11047 	ar->filter_flags = *total_flags;
11048 }
11049 EXPORT_SYMBOL(ath12k_mac_op_configure_filter);
11050 
11051 int ath12k_mac_op_get_antenna(struct ieee80211_hw *hw, int radio_idx,
11052 			      u32 *tx_ant, u32 *rx_ant)
11053 {
11054 	struct ath12k_hw *ah = ath12k_hw_to_ah(hw);
11055 	int antennas_rx = 0, antennas_tx = 0;
11056 	struct ath12k *ar;
11057 	int i;
11058 
11059 	lockdep_assert_wiphy(hw->wiphy);
11060 
11061 	for_each_ar(ah, ar, i) {
11062 		antennas_rx = max_t(u32, antennas_rx, ar->cfg_rx_chainmask);
11063 		antennas_tx = max_t(u32, antennas_tx, ar->cfg_tx_chainmask);
11064 	}
11065 
11066 	*tx_ant = antennas_tx;
11067 	*rx_ant = antennas_rx;
11068 
11069 	return 0;
11070 }
11071 EXPORT_SYMBOL(ath12k_mac_op_get_antenna);
11072 
11073 int ath12k_mac_op_set_antenna(struct ieee80211_hw *hw, int radio_idx,
11074 			      u32 tx_ant, u32 rx_ant)
11075 {
11076 	struct ath12k_hw *ah = ath12k_hw_to_ah(hw);
11077 	struct ath12k *ar;
11078 	int ret = 0;
11079 	int i;
11080 
11081 	lockdep_assert_wiphy(hw->wiphy);
11082 
11083 	for_each_ar(ah, ar, i) {
11084 		ret = __ath12k_set_antenna(ar, tx_ant, rx_ant);
11085 		if (ret)
11086 			break;
11087 	}
11088 
11089 	return ret;
11090 }
11091 EXPORT_SYMBOL(ath12k_mac_op_set_antenna);
11092 
11093 static int ath12k_mac_ampdu_action(struct ieee80211_hw *hw,
11094 				   struct ieee80211_vif *vif,
11095 				   struct ieee80211_ampdu_params *params,
11096 				   u8 link_id)
11097 {
11098 	struct ath12k *ar;
11099 	int ret = -EINVAL;
11100 
11101 	lockdep_assert_wiphy(hw->wiphy);
11102 
11103 	ar = ath12k_get_ar_by_vif(hw, vif, link_id);
11104 	if (!ar)
11105 		return -EINVAL;
11106 
11107 	switch (params->action) {
11108 	case IEEE80211_AMPDU_RX_START:
11109 		ret = ath12k_dp_rx_ampdu_start(ar, params, link_id);
11110 		break;
11111 	case IEEE80211_AMPDU_RX_STOP:
11112 		ret = ath12k_dp_rx_ampdu_stop(ar, params, link_id);
11113 		break;
11114 	case IEEE80211_AMPDU_TX_START:
11115 	case IEEE80211_AMPDU_TX_STOP_CONT:
11116 	case IEEE80211_AMPDU_TX_STOP_FLUSH:
11117 	case IEEE80211_AMPDU_TX_STOP_FLUSH_CONT:
11118 	case IEEE80211_AMPDU_TX_OPERATIONAL:
11119 		/* Tx A-MPDU aggregation offloaded to hw/fw so deny mac80211
11120 		 * Tx aggregation requests.
11121 		 */
11122 		ret = -EOPNOTSUPP;
11123 		break;
11124 	}
11125 
11126 	if (ret)
11127 		ath12k_warn(ar->ab, "unable to perform ampdu action %d for vif %pM link %u ret %d\n",
11128 			    params->action, vif->addr, link_id, ret);
11129 
11130 	return ret;
11131 }
11132 
11133 int ath12k_mac_op_ampdu_action(struct ieee80211_hw *hw,
11134 			       struct ieee80211_vif *vif,
11135 			       struct ieee80211_ampdu_params *params)
11136 {
11137 	struct ieee80211_sta *sta = params->sta;
11138 	struct ath12k_sta *ahsta = ath12k_sta_to_ahsta(sta);
11139 	unsigned long links_map = ahsta->links_map;
11140 	int ret = -EINVAL;
11141 	u8 link_id;
11142 
11143 	lockdep_assert_wiphy(hw->wiphy);
11144 
11145 	if (WARN_ON(!links_map))
11146 		return ret;
11147 
11148 	for_each_set_bit(link_id, &links_map, IEEE80211_MLD_MAX_NUM_LINKS) {
11149 		ret = ath12k_mac_ampdu_action(hw, vif, params, link_id);
11150 		if (ret)
11151 			return ret;
11152 	}
11153 
11154 	return 0;
11155 }
11156 EXPORT_SYMBOL(ath12k_mac_op_ampdu_action);
11157 
11158 int ath12k_mac_op_add_chanctx(struct ieee80211_hw *hw,
11159 			      struct ieee80211_chanctx_conf *ctx)
11160 {
11161 	struct ath12k *ar;
11162 	struct ath12k_base *ab;
11163 
11164 	lockdep_assert_wiphy(hw->wiphy);
11165 
11166 	ar = ath12k_get_ar_by_ctx(hw, ctx);
11167 	if (!ar)
11168 		return -EINVAL;
11169 
11170 	ab = ar->ab;
11171 
11172 	ath12k_dbg(ab, ATH12K_DBG_MAC,
11173 		   "mac chanctx add freq %u width %d ptr %p\n",
11174 		   ctx->def.chan->center_freq, ctx->def.width, ctx);
11175 
11176 	spin_lock_bh(&ar->data_lock);
11177 	/* TODO: In case of multiple channel context, populate rx_channel from
11178 	 * Rx PPDU desc information.
11179 	 */
11180 	ar->rx_channel = ctx->def.chan;
11181 	spin_unlock_bh(&ar->data_lock);
11182 	ar->chan_tx_pwr = ATH12K_PDEV_TX_POWER_INVALID;
11183 
11184 	return 0;
11185 }
11186 EXPORT_SYMBOL(ath12k_mac_op_add_chanctx);
11187 
11188 void ath12k_mac_op_remove_chanctx(struct ieee80211_hw *hw,
11189 				  struct ieee80211_chanctx_conf *ctx)
11190 {
11191 	struct ath12k *ar;
11192 	struct ath12k_base *ab;
11193 
11194 	lockdep_assert_wiphy(hw->wiphy);
11195 
11196 	ar = ath12k_get_ar_by_ctx(hw, ctx);
11197 	if (!ar)
11198 		return;
11199 
11200 	ab = ar->ab;
11201 
11202 	ath12k_dbg(ab, ATH12K_DBG_MAC,
11203 		   "mac chanctx remove freq %u width %d ptr %p\n",
11204 		   ctx->def.chan->center_freq, ctx->def.width, ctx);
11205 
11206 	spin_lock_bh(&ar->data_lock);
11207 	/* TODO: In case of there is one more channel context left, populate
11208 	 * rx_channel with the channel of that remaining channel context.
11209 	 */
11210 	ar->rx_channel = NULL;
11211 	spin_unlock_bh(&ar->data_lock);
11212 	ar->chan_tx_pwr = ATH12K_PDEV_TX_POWER_INVALID;
11213 }
11214 EXPORT_SYMBOL(ath12k_mac_op_remove_chanctx);
11215 
11216 static enum wmi_phy_mode
11217 ath12k_mac_check_down_grade_phy_mode(struct ath12k *ar,
11218 				     enum wmi_phy_mode mode,
11219 				     enum nl80211_band band,
11220 				     enum nl80211_iftype type)
11221 {
11222 	struct ieee80211_sta_eht_cap *eht_cap = NULL;
11223 	enum wmi_phy_mode down_mode;
11224 	int n = ar->mac.sbands[band].n_iftype_data;
11225 	int i;
11226 	struct ieee80211_sband_iftype_data *data;
11227 
11228 	if (mode < MODE_11BE_EHT20)
11229 		return mode;
11230 
11231 	data = ar->mac.iftype[band];
11232 	for (i = 0; i < n; i++) {
11233 		if (data[i].types_mask & BIT(type)) {
11234 			eht_cap = &data[i].eht_cap;
11235 			break;
11236 		}
11237 	}
11238 
11239 	if (eht_cap && eht_cap->has_eht)
11240 		return mode;
11241 
11242 	switch (mode) {
11243 	case MODE_11BE_EHT20:
11244 		down_mode = MODE_11AX_HE20;
11245 		break;
11246 	case MODE_11BE_EHT40:
11247 		down_mode = MODE_11AX_HE40;
11248 		break;
11249 	case MODE_11BE_EHT80:
11250 		down_mode = MODE_11AX_HE80;
11251 		break;
11252 	case MODE_11BE_EHT80_80:
11253 		down_mode = MODE_11AX_HE80_80;
11254 		break;
11255 	case MODE_11BE_EHT160:
11256 	case MODE_11BE_EHT160_160:
11257 	case MODE_11BE_EHT320:
11258 		down_mode = MODE_11AX_HE160;
11259 		break;
11260 	case MODE_11BE_EHT20_2G:
11261 		down_mode = MODE_11AX_HE20_2G;
11262 		break;
11263 	case MODE_11BE_EHT40_2G:
11264 		down_mode = MODE_11AX_HE40_2G;
11265 		break;
11266 	default:
11267 		down_mode = mode;
11268 		break;
11269 	}
11270 
11271 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
11272 		   "mac vdev start phymode %s downgrade to %s\n",
11273 		   ath12k_mac_phymode_str(mode),
11274 		   ath12k_mac_phymode_str(down_mode));
11275 
11276 	return down_mode;
11277 }
11278 
11279 static void
11280 ath12k_mac_mlo_get_vdev_args(struct ath12k_link_vif *arvif,
11281 			     struct wmi_ml_arg *ml_arg)
11282 {
11283 	struct ath12k_vif *ahvif = arvif->ahvif;
11284 	struct wmi_ml_partner_info *partner_info;
11285 	struct ieee80211_bss_conf *link_conf;
11286 	struct ath12k_link_vif *arvif_p;
11287 	unsigned long links;
11288 	u8 link_id;
11289 
11290 	lockdep_assert_wiphy(ahvif->ah->hw->wiphy);
11291 
11292 	if (!ath12k_mac_is_ml_arvif(arvif))
11293 		return;
11294 
11295 	if (hweight16(ahvif->vif->valid_links) > ATH12K_WMI_MLO_MAX_LINKS)
11296 		return;
11297 
11298 	ml_arg->enabled = true;
11299 
11300 	/* Driver always add a new link via VDEV START, FW takes
11301 	 * care of internally adding this link to existing
11302 	 * link vdevs which are advertised as partners below
11303 	 */
11304 	ml_arg->link_add = true;
11305 
11306 	ml_arg->assoc_link = arvif->is_sta_assoc_link;
11307 
11308 	partner_info = ml_arg->partner_info;
11309 
11310 	links = ahvif->links_map;
11311 	for_each_set_bit(link_id, &links, IEEE80211_MLD_MAX_NUM_LINKS) {
11312 		arvif_p = wiphy_dereference(ahvif->ah->hw->wiphy, ahvif->link[link_id]);
11313 
11314 		if (WARN_ON(!arvif_p))
11315 			continue;
11316 
11317 		if (arvif == arvif_p)
11318 			continue;
11319 
11320 		if (!arvif_p->is_started)
11321 			continue;
11322 
11323 		link_conf = wiphy_dereference(ahvif->ah->hw->wiphy,
11324 					      ahvif->vif->link_conf[arvif_p->link_id]);
11325 
11326 		if (!link_conf)
11327 			continue;
11328 
11329 		partner_info->vdev_id = arvif_p->vdev_id;
11330 		partner_info->hw_link_id = arvif_p->ar->pdev->hw_link_id;
11331 		ether_addr_copy(partner_info->addr, link_conf->addr);
11332 		ml_arg->num_partner_links++;
11333 		partner_info++;
11334 	}
11335 }
11336 
11337 static int
11338 ath12k_mac_vdev_start_restart(struct ath12k_link_vif *arvif,
11339 			      struct ieee80211_chanctx_conf *ctx,
11340 			      bool restart)
11341 {
11342 	struct ath12k *ar = arvif->ar;
11343 	struct ath12k_base *ab = ar->ab;
11344 	struct wmi_vdev_start_req_arg arg = {};
11345 	const struct cfg80211_chan_def *chandef = &ctx->def;
11346 	struct ieee80211_hw *hw = ath12k_ar_to_hw(ar);
11347 	struct ath12k_vif *ahvif = arvif->ahvif;
11348 	struct ieee80211_bss_conf *link_conf;
11349 	unsigned int dfs_cac_time;
11350 	int ret;
11351 
11352 	lockdep_assert_wiphy(hw->wiphy);
11353 
11354 	link_conf = ath12k_mac_get_link_bss_conf(arvif);
11355 	if (!link_conf) {
11356 		ath12k_warn(ar->ab, "unable to access bss link conf in vdev start for vif %pM link %u\n",
11357 			    ahvif->vif->addr, arvif->link_id);
11358 		return -ENOLINK;
11359 	}
11360 
11361 	reinit_completion(&ar->vdev_setup_done);
11362 
11363 	arg.vdev_id = arvif->vdev_id;
11364 	arg.dtim_period = arvif->dtim_period;
11365 	arg.bcn_intval = arvif->beacon_interval;
11366 	arg.punct_bitmap = ~arvif->punct_bitmap;
11367 
11368 	arg.freq = chandef->chan->center_freq;
11369 	arg.band_center_freq1 = chandef->center_freq1;
11370 	arg.band_center_freq2 = chandef->center_freq2;
11371 	arg.mode = ath12k_phymodes[chandef->chan->band][chandef->width];
11372 
11373 	arg.mode = ath12k_mac_check_down_grade_phy_mode(ar, arg.mode,
11374 							chandef->chan->band,
11375 							ahvif->vif->type);
11376 	arg.min_power = 0;
11377 	arg.max_power = chandef->chan->max_power;
11378 	arg.max_reg_power = chandef->chan->max_reg_power;
11379 	arg.max_antenna_gain = chandef->chan->max_antenna_gain;
11380 
11381 	arg.pref_tx_streams = ar->num_tx_chains;
11382 	arg.pref_rx_streams = ar->num_rx_chains;
11383 
11384 	arg.mbssid_flags = WMI_VDEV_MBSSID_FLAGS_NON_MBSSID_AP;
11385 	arg.mbssid_tx_vdev_id = 0;
11386 	if (test_bit(WMI_TLV_SERVICE_MBSS_PARAM_IN_VDEV_START_SUPPORT,
11387 		     ar->ab->wmi_ab.svc_map)) {
11388 		ret = ath12k_mac_setup_vdev_params_mbssid(arvif,
11389 							  &arg.mbssid_flags,
11390 							  &arg.mbssid_tx_vdev_id);
11391 		if (ret)
11392 			return ret;
11393 	}
11394 
11395 	if (ahvif->vdev_type == WMI_VDEV_TYPE_AP) {
11396 		arg.ssid = ahvif->u.ap.ssid;
11397 		arg.ssid_len = ahvif->u.ap.ssid_len;
11398 		arg.hidden_ssid = ahvif->u.ap.hidden_ssid;
11399 
11400 		/* For now allow DFS for AP mode */
11401 		arg.chan_radar = !!(chandef->chan->flags & IEEE80211_CHAN_RADAR);
11402 
11403 		arg.freq2_radar = ctx->radar_enabled;
11404 
11405 		arg.passive = arg.chan_radar;
11406 
11407 		spin_lock_bh(&ab->base_lock);
11408 		arg.regdomain = ar->ab->dfs_region;
11409 		spin_unlock_bh(&ab->base_lock);
11410 
11411 		/* TODO: Notify if secondary 80Mhz also needs radar detection */
11412 	}
11413 
11414 	arg.passive |= !!(chandef->chan->flags & IEEE80211_CHAN_NO_IR);
11415 
11416 	if (!restart)
11417 		ath12k_mac_mlo_get_vdev_args(arvif, &arg.ml);
11418 
11419 	ath12k_dbg(ab, ATH12K_DBG_MAC,
11420 		   "mac vdev %d start center_freq %d phymode %s punct_bitmap 0x%x\n",
11421 		   arg.vdev_id, arg.freq,
11422 		   ath12k_mac_phymode_str(arg.mode), arg.punct_bitmap);
11423 
11424 	ret = ath12k_wmi_vdev_start(ar, &arg, restart);
11425 	if (ret) {
11426 		ath12k_warn(ar->ab, "failed to %s WMI vdev %i\n",
11427 			    restart ? "restart" : "start", arg.vdev_id);
11428 		return ret;
11429 	}
11430 
11431 	ret = ath12k_mac_vdev_setup_sync(ar);
11432 	if (ret) {
11433 		ath12k_warn(ab, "failed to synchronize setup for vdev %i %s: %d\n",
11434 			    arg.vdev_id, restart ? "restart" : "start", ret);
11435 		return ret;
11436 	}
11437 
11438 	/* TODO: For now we only set TPC power here. However when
11439 	 * channel changes, say CSA, it should be updated again.
11440 	 */
11441 	if (ath12k_mac_supports_tpc(ar, ahvif, chandef)) {
11442 		ath12k_mac_fill_reg_tpc_info(ar, arvif, ctx);
11443 		ath12k_wmi_send_vdev_set_tpc_power(ar, arvif->vdev_id,
11444 						   &arvif->reg_tpc_info);
11445 	}
11446 
11447 	ar->num_started_vdevs++;
11448 	ath12k_dbg(ab, ATH12K_DBG_MAC,  "vdev %pM started, vdev_id %d\n",
11449 		   ahvif->vif->addr, arvif->vdev_id);
11450 
11451 	/* Enable CAC Running Flag in the driver by checking all sub-channel's DFS
11452 	 * state as NL80211_DFS_USABLE which indicates CAC needs to be
11453 	 * done before channel usage. This flag is used to drop rx packets.
11454 	 * during CAC.
11455 	 */
11456 	/* TODO: Set the flag for other interface types as required */
11457 	if (arvif->ahvif->vdev_type == WMI_VDEV_TYPE_AP && ctx->radar_enabled &&
11458 	    cfg80211_chandef_dfs_usable(hw->wiphy, chandef)) {
11459 		set_bit(ATH12K_FLAG_CAC_RUNNING, &ar->dev_flags);
11460 		dfs_cac_time = cfg80211_chandef_dfs_cac_time(hw->wiphy, chandef);
11461 
11462 		ath12k_dbg(ab, ATH12K_DBG_MAC,
11463 			   "CAC started dfs_cac_time %u center_freq %d center_freq1 %d for vdev %d\n",
11464 			   dfs_cac_time, arg.freq, arg.band_center_freq1, arg.vdev_id);
11465 	}
11466 
11467 	ret = ath12k_mac_set_txbf_conf(arvif);
11468 	if (ret)
11469 		ath12k_warn(ab, "failed to set txbf conf for vdev %d: %d\n",
11470 			    arvif->vdev_id, ret);
11471 
11472 	return 0;
11473 }
11474 
11475 static int ath12k_mac_vdev_start(struct ath12k_link_vif *arvif,
11476 				 struct ieee80211_chanctx_conf *ctx)
11477 {
11478 	return ath12k_mac_vdev_start_restart(arvif, ctx, false);
11479 }
11480 
11481 static int ath12k_mac_vdev_restart(struct ath12k_link_vif *arvif,
11482 				   struct ieee80211_chanctx_conf *ctx)
11483 {
11484 	return ath12k_mac_vdev_start_restart(arvif, ctx, true);
11485 }
11486 
11487 struct ath12k_mac_change_chanctx_arg {
11488 	struct ieee80211_chanctx_conf *ctx;
11489 	struct ieee80211_vif_chanctx_switch *vifs;
11490 	int n_vifs;
11491 	int next_vif;
11492 	struct ath12k *ar;
11493 };
11494 
11495 static void
11496 ath12k_mac_change_chanctx_cnt_iter(void *data, u8 *mac,
11497 				   struct ieee80211_vif *vif)
11498 {
11499 	struct ath12k_vif *ahvif = ath12k_vif_to_ahvif(vif);
11500 	struct ath12k_mac_change_chanctx_arg *arg = data;
11501 	struct ieee80211_bss_conf *link_conf;
11502 	struct ath12k_link_vif *arvif;
11503 	unsigned long links_map;
11504 	u8 link_id;
11505 
11506 	lockdep_assert_wiphy(ahvif->ah->hw->wiphy);
11507 
11508 	links_map = ahvif->links_map;
11509 	for_each_set_bit(link_id, &links_map, IEEE80211_MLD_MAX_NUM_LINKS) {
11510 		arvif = wiphy_dereference(ahvif->ah->hw->wiphy, ahvif->link[link_id]);
11511 		if (WARN_ON(!arvif))
11512 			continue;
11513 
11514 		if (!arvif->is_created || arvif->ar != arg->ar)
11515 			continue;
11516 
11517 		link_conf = wiphy_dereference(ahvif->ah->hw->wiphy,
11518 					      vif->link_conf[link_id]);
11519 		if (WARN_ON(!link_conf))
11520 			continue;
11521 
11522 		if (rcu_access_pointer(link_conf->chanctx_conf) != arg->ctx)
11523 			continue;
11524 
11525 		arg->n_vifs++;
11526 	}
11527 }
11528 
11529 static void
11530 ath12k_mac_change_chanctx_fill_iter(void *data, u8 *mac,
11531 				    struct ieee80211_vif *vif)
11532 {
11533 	struct ath12k_vif *ahvif = ath12k_vif_to_ahvif(vif);
11534 	struct ath12k_mac_change_chanctx_arg *arg = data;
11535 	struct ieee80211_bss_conf *link_conf;
11536 	struct ieee80211_chanctx_conf *ctx;
11537 	struct ath12k_link_vif *arvif;
11538 	unsigned long links_map;
11539 	u8 link_id;
11540 
11541 	lockdep_assert_wiphy(ahvif->ah->hw->wiphy);
11542 
11543 	links_map = ahvif->links_map;
11544 	for_each_set_bit(link_id, &links_map, IEEE80211_MLD_MAX_NUM_LINKS) {
11545 		arvif = wiphy_dereference(ahvif->ah->hw->wiphy, ahvif->link[link_id]);
11546 		if (WARN_ON(!arvif))
11547 			continue;
11548 
11549 		if (!arvif->is_created || arvif->ar != arg->ar)
11550 			continue;
11551 
11552 		link_conf = wiphy_dereference(ahvif->ah->hw->wiphy,
11553 					      vif->link_conf[arvif->link_id]);
11554 		if (WARN_ON(!link_conf))
11555 			continue;
11556 
11557 		ctx = rcu_access_pointer(link_conf->chanctx_conf);
11558 		if (ctx != arg->ctx)
11559 			continue;
11560 
11561 		if (WARN_ON(arg->next_vif == arg->n_vifs))
11562 			return;
11563 
11564 		arg->vifs[arg->next_vif].vif = vif;
11565 		arg->vifs[arg->next_vif].old_ctx = ctx;
11566 		arg->vifs[arg->next_vif].new_ctx = ctx;
11567 		arg->vifs[arg->next_vif].link_conf = link_conf;
11568 		arg->next_vif++;
11569 	}
11570 }
11571 
11572 static u32 ath12k_mac_nlwidth_to_wmiwidth(enum nl80211_chan_width width)
11573 {
11574 	switch (width) {
11575 	case NL80211_CHAN_WIDTH_20:
11576 		return WMI_CHAN_WIDTH_20;
11577 	case NL80211_CHAN_WIDTH_40:
11578 		return WMI_CHAN_WIDTH_40;
11579 	case NL80211_CHAN_WIDTH_80:
11580 		return WMI_CHAN_WIDTH_80;
11581 	case NL80211_CHAN_WIDTH_160:
11582 		return WMI_CHAN_WIDTH_160;
11583 	case NL80211_CHAN_WIDTH_80P80:
11584 		return WMI_CHAN_WIDTH_80P80;
11585 	case NL80211_CHAN_WIDTH_5:
11586 		return WMI_CHAN_WIDTH_5;
11587 	case NL80211_CHAN_WIDTH_10:
11588 		return WMI_CHAN_WIDTH_10;
11589 	case NL80211_CHAN_WIDTH_320:
11590 		return WMI_CHAN_WIDTH_320;
11591 	default:
11592 		WARN_ON(1);
11593 		return WMI_CHAN_WIDTH_20;
11594 	}
11595 }
11596 
11597 static int ath12k_mac_update_peer_puncturing_width(struct ath12k *ar,
11598 						   struct ath12k_link_vif *arvif,
11599 						   struct cfg80211_chan_def def)
11600 {
11601 	u32 param_id, param_value;
11602 	int ret;
11603 
11604 	if (arvif->ahvif->vdev_type != WMI_VDEV_TYPE_STA)
11605 		return 0;
11606 
11607 	param_id = WMI_PEER_CHWIDTH_PUNCTURE_20MHZ_BITMAP;
11608 	param_value = ath12k_mac_nlwidth_to_wmiwidth(def.width) |
11609 		u32_encode_bits((~def.punctured),
11610 				WMI_PEER_PUNCTURE_BITMAP);
11611 
11612 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
11613 		   "punctured bitmap %02x width %d vdev %d\n",
11614 		   def.punctured, def.width, arvif->vdev_id);
11615 
11616 	ret = ath12k_wmi_set_peer_param(ar, arvif->bssid,
11617 					arvif->vdev_id, param_id,
11618 					param_value);
11619 
11620 	return ret;
11621 }
11622 
11623 static void
11624 ath12k_mac_update_vif_chan(struct ath12k *ar,
11625 			   struct ieee80211_vif_chanctx_switch *vifs,
11626 			   int n_vifs)
11627 {
11628 	struct ath12k_incumbent_signal_interference *incumbent;
11629 	struct ath12k_wmi_vdev_up_params params = {};
11630 	struct ieee80211_bss_conf *link_conf;
11631 	struct cfg80211_chan_def *chandef;
11632 	struct ath12k_base *ab = ar->ab;
11633 	struct ath12k_link_vif *arvif;
11634 	struct ieee80211_vif *vif;
11635 	struct ath12k_vif *ahvif;
11636 	u8 link_id;
11637 	int ret;
11638 	int i;
11639 	bool monitor_vif = false;
11640 
11641 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
11642 
11643 	for (i = 0; i < n_vifs; i++) {
11644 		vif = vifs[i].vif;
11645 		ahvif = ath12k_vif_to_ahvif(vif);
11646 		link_conf = vifs[i].link_conf;
11647 		link_id = link_conf->link_id;
11648 		arvif = wiphy_dereference(ath12k_ar_to_hw(ar)->wiphy,
11649 					  ahvif->link[link_id]);
11650 
11651 		if (vif->type == NL80211_IFTYPE_MONITOR) {
11652 			monitor_vif = true;
11653 			continue;
11654 		}
11655 
11656 		if (WARN_ON(!arvif))
11657 			continue;
11658 
11659 		ath12k_dbg(ab, ATH12K_DBG_MAC,
11660 			   "mac chanctx switch vdev_id %i freq %u->%u width %d->%d\n",
11661 			   arvif->vdev_id,
11662 			   vifs[i].old_ctx->def.chan->center_freq,
11663 			   vifs[i].new_ctx->def.chan->center_freq,
11664 			   vifs[i].old_ctx->def.width,
11665 			   vifs[i].new_ctx->def.width);
11666 
11667 		if (WARN_ON(!arvif->is_started))
11668 			continue;
11669 
11670 		arvif->punct_bitmap = vifs[i].new_ctx->def.punctured;
11671 
11672 		/* Firmware expect vdev_restart only if vdev is up.
11673 		 * If vdev is down then it expect vdev_stop->vdev_start.
11674 		 */
11675 		if (arvif->is_up) {
11676 			ret = ath12k_mac_vdev_restart(arvif, vifs[i].new_ctx);
11677 			if (ret) {
11678 				ath12k_warn(ab, "failed to restart vdev %d: %d\n",
11679 					    arvif->vdev_id, ret);
11680 				continue;
11681 			}
11682 		} else {
11683 			ret = ath12k_mac_vdev_stop(arvif);
11684 			if (ret) {
11685 				ath12k_warn(ab, "failed to stop vdev %d: %d\n",
11686 					    arvif->vdev_id, ret);
11687 				continue;
11688 			}
11689 
11690 			ret = ath12k_mac_vdev_start(arvif, vifs[i].new_ctx);
11691 			if (ret)
11692 				ath12k_warn(ab, "failed to start vdev %d: %d\n",
11693 					    arvif->vdev_id, ret);
11694 			continue;
11695 		}
11696 
11697 		ret = ath12k_mac_update_peer_puncturing_width(arvif->ar, arvif,
11698 							      vifs[i].new_ctx->def);
11699 		if (ret) {
11700 			ath12k_warn(ar->ab,
11701 				    "failed to update puncturing bitmap %02x and width %d: %d\n",
11702 				    vifs[i].new_ctx->def.punctured,
11703 				    vifs[i].new_ctx->def.width, ret);
11704 			continue;
11705 		}
11706 
11707 		/* Defer VDEV bring-up during CSA to avoid installing stale
11708 		 * beacon templates. The beacon content is updated only
11709 		 * after CSA finalize, so we mark CSA in progress and skip
11710 		 * VDEV_UP for now. It will be handled later in
11711 		 * bss_info_changed().
11712 		 */
11713 		if (link_conf->csa_active &&
11714 		    arvif->ahvif->vdev_type == WMI_VDEV_TYPE_AP) {
11715 			arvif->is_csa_in_progress = true;
11716 			continue;
11717 		}
11718 
11719 		ret = ath12k_mac_setup_bcn_tmpl(arvif);
11720 		if (ret)
11721 			ath12k_warn(ab, "failed to update bcn tmpl during csa: %d\n",
11722 				    ret);
11723 
11724 		memset(&params, 0, sizeof(params));
11725 		params.vdev_id = arvif->vdev_id;
11726 		params.aid = ahvif->aid;
11727 		params.bssid = arvif->bssid;
11728 		params.tx_bssid = ath12k_mac_get_tx_bssid(arvif);
11729 		if (params.tx_bssid) {
11730 			params.nontx_profile_idx = link_conf->bssid_index;
11731 			params.nontx_profile_cnt = 1 << link_conf->bssid_indicator;
11732 		}
11733 		ret = ath12k_wmi_vdev_up(arvif->ar, &params);
11734 		if (ret) {
11735 			ath12k_warn(ab, "failed to bring vdev up %d: %d\n",
11736 				    arvif->vdev_id, ret);
11737 			continue;
11738 		}
11739 	}
11740 
11741 	/* Restart the internal monitor vdev on new channel */
11742 	if (!monitor_vif && ar->monitor_vdev_created) {
11743 		if (!ath12k_mac_monitor_stop(ar))
11744 			ath12k_mac_monitor_start(ar);
11745 	}
11746 
11747 	incumbent = &ar->incumbent_signal_interference;
11748 	spin_lock_bh(&ar->data_lock);
11749 	if (incumbent->handling_in_progress) {
11750 		chandef = &vifs[0].new_ctx->def;
11751 		if (incumbent->chan_bw_interference_bitmap &
11752 		    ATH12K_WMI_DCS_SEG_PRI20) {
11753 			if (incumbent->center_freq !=
11754 			    chandef->chan->center_freq) {
11755 				incumbent->chan_bw_interference_bitmap = 0;
11756 				incumbent->handling_in_progress = false;
11757 				ath12k_dbg(ab, ATH12K_DBG_MAC,
11758 					   "incumbent signal interference chan switch completed\n");
11759 			} else {
11760 				ath12k_warn(ab,
11761 					    "incumbent signal interference chan switch not done, freq %u\n",
11762 					    incumbent->center_freq);
11763 			}
11764 		} else {
11765 			if (incumbent->center_freq !=
11766 			    chandef->chan->center_freq ||
11767 			    incumbent->width != chandef->width) {
11768 				incumbent->chan_bw_interference_bitmap = 0;
11769 				incumbent->handling_in_progress = false;
11770 				ath12k_dbg(ab, ATH12K_DBG_MAC,
11771 					   "Bandwidth/channel change due to incumbent signal interference completed\n");
11772 			} else {
11773 				ath12k_warn(ab, "Bandwidth/channel change due to incumbent sig intf not done intf_freq %u chan_freq %u intf_width %u chan_width %u\n",
11774 					    incumbent->center_freq,
11775 					    chandef->chan->center_freq,
11776 					    incumbent->width,
11777 					    chandef->width);
11778 			}
11779 		}
11780 	}
11781 	spin_unlock_bh(&ar->data_lock);
11782 }
11783 
11784 static void
11785 ath12k_mac_update_active_vif_chan(struct ath12k *ar,
11786 				  struct ieee80211_chanctx_conf *ctx)
11787 {
11788 	struct ath12k_mac_change_chanctx_arg arg = { .ctx = ctx, .ar = ar };
11789 	struct ieee80211_hw *hw = ath12k_ar_to_hw(ar);
11790 
11791 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
11792 
11793 	ieee80211_iterate_active_interfaces_atomic(hw,
11794 						   IEEE80211_IFACE_ITER_NORMAL,
11795 						   ath12k_mac_change_chanctx_cnt_iter,
11796 						   &arg);
11797 	if (arg.n_vifs == 0)
11798 		return;
11799 
11800 	arg.vifs = kzalloc_objs(arg.vifs[0], arg.n_vifs);
11801 	if (!arg.vifs)
11802 		return;
11803 
11804 	ieee80211_iterate_active_interfaces_atomic(hw,
11805 						   IEEE80211_IFACE_ITER_NORMAL,
11806 						   ath12k_mac_change_chanctx_fill_iter,
11807 						   &arg);
11808 
11809 	ath12k_mac_update_vif_chan(ar, arg.vifs, arg.n_vifs);
11810 
11811 	kfree(arg.vifs);
11812 }
11813 
11814 void ath12k_mac_op_change_chanctx(struct ieee80211_hw *hw,
11815 				  struct ieee80211_chanctx_conf *ctx,
11816 				  u32 changed)
11817 {
11818 	struct ath12k *ar;
11819 	struct ath12k_base *ab;
11820 
11821 	lockdep_assert_wiphy(hw->wiphy);
11822 
11823 	ar = ath12k_get_ar_by_ctx(hw, ctx);
11824 	if (!ar)
11825 		return;
11826 
11827 	ab = ar->ab;
11828 
11829 	ath12k_dbg(ab, ATH12K_DBG_MAC,
11830 		   "mac chanctx change freq %u width %d ptr %p changed %x\n",
11831 		   ctx->def.chan->center_freq, ctx->def.width, ctx, changed);
11832 
11833 	/* This shouldn't really happen because channel switching should use
11834 	 * switch_vif_chanctx().
11835 	 */
11836 	if (WARN_ON(changed & IEEE80211_CHANCTX_CHANGE_CHANNEL))
11837 		return;
11838 
11839 	if (changed & IEEE80211_CHANCTX_CHANGE_WIDTH ||
11840 	    changed & IEEE80211_CHANCTX_CHANGE_RADAR ||
11841 	    changed & IEEE80211_CHANCTX_CHANGE_PUNCTURING)
11842 		ath12k_mac_update_active_vif_chan(ar, ctx);
11843 
11844 	/* TODO: Recalc radar detection */
11845 }
11846 EXPORT_SYMBOL(ath12k_mac_op_change_chanctx);
11847 
11848 static int ath12k_start_vdev_delay(struct ath12k *ar,
11849 				   struct ath12k_link_vif *arvif)
11850 {
11851 	struct ath12k_base *ab = ar->ab;
11852 	struct ath12k_vif *ahvif = arvif->ahvif;
11853 	struct ieee80211_vif *vif = ath12k_ahvif_to_vif(arvif->ahvif);
11854 	struct ieee80211_chanctx_conf *chanctx;
11855 	struct ieee80211_bss_conf *link_conf;
11856 	int ret;
11857 
11858 	if (WARN_ON(arvif->is_started))
11859 		return -EBUSY;
11860 
11861 	link_conf = ath12k_mac_get_link_bss_conf(arvif);
11862 	if (!link_conf) {
11863 		ath12k_warn(ab, "failed to get link conf for vdev %u\n", arvif->vdev_id);
11864 		return -EINVAL;
11865 	}
11866 
11867 	chanctx	= wiphy_dereference(ath12k_ar_to_hw(arvif->ar)->wiphy,
11868 				    link_conf->chanctx_conf);
11869 	ret = ath12k_mac_vdev_start(arvif, chanctx);
11870 	if (ret) {
11871 		ath12k_warn(ab, "failed to start vdev %i addr %pM on freq %d: %d\n",
11872 			    arvif->vdev_id, vif->addr,
11873 			    chanctx->def.chan->center_freq, ret);
11874 		return ret;
11875 	}
11876 
11877 	if (ahvif->vdev_type == WMI_VDEV_TYPE_MONITOR) {
11878 		ret = ath12k_monitor_vdev_up(ar, arvif->vdev_id);
11879 		if (ret) {
11880 			ath12k_warn(ab, "failed put monitor up: %d\n", ret);
11881 			return ret;
11882 		}
11883 	}
11884 
11885 	arvif->is_started = true;
11886 
11887 	/* TODO: Setup ps and cts/rts protection */
11888 	return 0;
11889 }
11890 
11891 static u8 ath12k_mac_get_num_pwr_levels(struct cfg80211_chan_def *chan_def)
11892 {
11893 	if (chan_def->chan->flags & IEEE80211_CHAN_PSD) {
11894 		switch (chan_def->width) {
11895 		case NL80211_CHAN_WIDTH_20:
11896 			return 1;
11897 		case NL80211_CHAN_WIDTH_40:
11898 			return 2;
11899 		case NL80211_CHAN_WIDTH_80:
11900 			return 4;
11901 		case NL80211_CHAN_WIDTH_160:
11902 			return 8;
11903 		case NL80211_CHAN_WIDTH_320:
11904 			return 16;
11905 		default:
11906 			return 1;
11907 		}
11908 	} else {
11909 		switch (chan_def->width) {
11910 		case NL80211_CHAN_WIDTH_20:
11911 			return 1;
11912 		case NL80211_CHAN_WIDTH_40:
11913 			return 2;
11914 		case NL80211_CHAN_WIDTH_80:
11915 			return 3;
11916 		case NL80211_CHAN_WIDTH_160:
11917 			return 4;
11918 		case NL80211_CHAN_WIDTH_320:
11919 			return 5;
11920 		default:
11921 			return 1;
11922 		}
11923 	}
11924 }
11925 
11926 static u16 ath12k_mac_get_6ghz_start_frequency(struct cfg80211_chan_def *chan_def)
11927 {
11928 	u16 diff_seq;
11929 
11930 	/* It is to get the lowest channel number's center frequency of the chan.
11931 	 * For example,
11932 	 * bandwidth=40 MHz, center frequency is 5965, lowest channel is 1
11933 	 * with center frequency 5955, its diff is 5965 - 5955 = 10.
11934 	 * bandwidth=80 MHz, center frequency is 5985, lowest channel is 1
11935 	 * with center frequency 5955, its diff is 5985 - 5955 = 30.
11936 	 * bandwidth=160 MHz, center frequency is 6025, lowest channel is 1
11937 	 * with center frequency 5955, its diff is 6025 - 5955 = 70.
11938 	 * bandwidth=320 MHz, center frequency is 6105, lowest channel is 1
11939 	 * with center frequency 5955, its diff is 6105 - 5955 = 70.
11940 	 */
11941 	switch (chan_def->width) {
11942 	case NL80211_CHAN_WIDTH_320:
11943 		diff_seq = 150;
11944 		break;
11945 	case NL80211_CHAN_WIDTH_160:
11946 		diff_seq = 70;
11947 		break;
11948 	case NL80211_CHAN_WIDTH_80:
11949 		diff_seq = 30;
11950 		break;
11951 	case NL80211_CHAN_WIDTH_40:
11952 		diff_seq = 10;
11953 		break;
11954 	default:
11955 		diff_seq = 0;
11956 	}
11957 
11958 	return chan_def->center_freq1 - diff_seq;
11959 }
11960 
11961 static u16 ath12k_mac_get_seg_freq(struct cfg80211_chan_def *chan_def,
11962 				   u16 start_seq, u8 seq)
11963 {
11964 	u16 seg_seq;
11965 
11966 	/* It is to get the center frequency of the specific bandwidth.
11967 	 * start_seq means the lowest channel number's center frequency.
11968 	 * seq 0/1/2/3 means 20 MHz/40 MHz/80 MHz/160 MHz.
11969 	 * For example,
11970 	 * lowest channel is 1, its center frequency 5955,
11971 	 * center frequency is 5955 when bandwidth=20 MHz, its diff is 5955 - 5955 = 0.
11972 	 * lowest channel is 1, its center frequency 5955,
11973 	 * center frequency is 5965 when bandwidth=40 MHz, its diff is 5965 - 5955 = 10.
11974 	 * lowest channel is 1, its center frequency 5955,
11975 	 * center frequency is 5985 when bandwidth=80 MHz, its diff is 5985 - 5955 = 30.
11976 	 * lowest channel is 1, its center frequency 5955,
11977 	 * center frequency is 6025 when bandwidth=160 MHz, its diff is 6025 - 5955 = 70.
11978 	 */
11979 	seg_seq = 10 * (BIT(seq) - 1);
11980 	return seg_seq + start_seq;
11981 }
11982 
11983 static void ath12k_mac_get_psd_channel(struct ath12k *ar,
11984 				       u16 step_freq,
11985 				       u16 *start_freq,
11986 				       u16 *center_freq,
11987 				       u8 i,
11988 				       struct ieee80211_channel **temp_chan,
11989 				       s8 *tx_power)
11990 {
11991 	/* It is to get the center frequency for each 20 MHz.
11992 	 * For example, if the chan is 160 MHz and center frequency is 6025,
11993 	 * then it include 8 channels, they are 1/5/9/13/17/21/25/29,
11994 	 * channel number 1's center frequency is 5955, it is parameter start_freq.
11995 	 * parameter i is the step of the 8 channels. i is 0~7 for the 8 channels.
11996 	 * the channel 1/5/9/13/17/21/25/29 maps i=0/1/2/3/4/5/6/7,
11997 	 * and maps its center frequency is 5955/5975/5995/6015/6035/6055/6075/6095,
11998 	 * the gap is 20 for each channel, parameter step_freq means the gap.
11999 	 * after get the center frequency of each channel, it is easy to find the
12000 	 * struct ieee80211_channel of it and get the max_reg_power.
12001 	 */
12002 	*center_freq = *start_freq + i * step_freq;
12003 	*temp_chan = ieee80211_get_channel(ar->ah->hw->wiphy, *center_freq);
12004 	*tx_power = (*temp_chan)->max_reg_power;
12005 }
12006 
12007 static void ath12k_mac_get_eirp_power(struct ath12k *ar,
12008 				      u16 *start_freq,
12009 				      u16 *center_freq,
12010 				      u8 i,
12011 				      struct ieee80211_channel **temp_chan,
12012 				      struct cfg80211_chan_def *def,
12013 				      s8 *tx_power)
12014 {
12015 	/* It is to get the center frequency for 20 MHz/40 MHz/80 MHz/
12016 	 * 160 MHz bandwidth, and then plus 10 to the center frequency,
12017 	 * it is the center frequency of a channel number.
12018 	 * For example, when configured channel number is 1.
12019 	 * center frequency is 5965 when bandwidth=40 MHz, after plus 10, it is 5975,
12020 	 * then it is channel number 5.
12021 	 * center frequency is 5985 when bandwidth=80 MHz, after plus 10, it is 5995,
12022 	 * then it is channel number 9.
12023 	 * center frequency is 6025 when bandwidth=160 MHz, after plus 10, it is 6035,
12024 	 * then it is channel number 17.
12025 	 * after get the center frequency of each channel, it is easy to find the
12026 	 * struct ieee80211_channel of it and get the max_reg_power.
12027 	 */
12028 	*center_freq = ath12k_mac_get_seg_freq(def, *start_freq, i);
12029 
12030 	/* For the 20 MHz, its center frequency is same with same channel */
12031 	if (i != 0)
12032 		*center_freq += 10;
12033 
12034 	*temp_chan = ieee80211_get_channel(ar->ah->hw->wiphy, *center_freq);
12035 	*tx_power = (*temp_chan)->max_reg_power;
12036 }
12037 
12038 void ath12k_mac_fill_reg_tpc_info(struct ath12k *ar,
12039 				  struct ath12k_link_vif *arvif,
12040 				  struct ieee80211_chanctx_conf *ctx)
12041 {
12042 	struct ath12k_base *ab = ar->ab;
12043 	struct ath12k_reg_tpc_power_info *reg_tpc_info = &arvif->reg_tpc_info;
12044 	struct ieee80211_bss_conf *bss_conf = ath12k_mac_get_link_bss_conf(arvif);
12045 	struct ieee80211_channel *chan, *temp_chan;
12046 	u8 pwr_lvl_idx, num_pwr_levels, pwr_reduction;
12047 	bool is_psd_power = false, is_tpe_present = false;
12048 	s8 max_tx_power[ATH12K_NUM_PWR_LEVELS], psd_power, tx_power;
12049 	s8 eirp_power = 0;
12050 	struct ath12k_vif *ahvif = arvif->ahvif;
12051 	u16 start_freq, center_freq;
12052 	u8 reg_6ghz_power_mode;
12053 
12054 	chan = ctx->def.chan;
12055 	start_freq = ath12k_mac_get_6ghz_start_frequency(&ctx->def);
12056 	pwr_reduction = bss_conf->pwr_reduction;
12057 
12058 	if (arvif->reg_tpc_info.num_pwr_levels) {
12059 		is_tpe_present = true;
12060 		num_pwr_levels = arvif->reg_tpc_info.num_pwr_levels;
12061 	} else {
12062 		num_pwr_levels = ath12k_mac_get_num_pwr_levels(&ctx->def);
12063 	}
12064 
12065 	for (pwr_lvl_idx = 0; pwr_lvl_idx < num_pwr_levels; pwr_lvl_idx++) {
12066 		/* STA received TPE IE*/
12067 		if (is_tpe_present) {
12068 			/* local power is PSD power*/
12069 			if (chan->flags & IEEE80211_CHAN_PSD) {
12070 				/* Connecting AP is psd power */
12071 				if (reg_tpc_info->is_psd_power) {
12072 					is_psd_power = true;
12073 					ath12k_mac_get_psd_channel(ar, 20,
12074 								   &start_freq,
12075 								   &center_freq,
12076 								   pwr_lvl_idx,
12077 								   &temp_chan,
12078 								   &tx_power);
12079 					psd_power = temp_chan->psd;
12080 					eirp_power = tx_power;
12081 					max_tx_power[pwr_lvl_idx] =
12082 						min_t(s8,
12083 						      psd_power,
12084 						      reg_tpc_info->tpe[pwr_lvl_idx]);
12085 				/* Connecting AP is not psd power */
12086 				} else {
12087 					ath12k_mac_get_eirp_power(ar,
12088 								  &start_freq,
12089 								  &center_freq,
12090 								  pwr_lvl_idx,
12091 								  &temp_chan,
12092 								  &ctx->def,
12093 								  &tx_power);
12094 					psd_power = temp_chan->psd;
12095 					/* convert psd power to EIRP power based
12096 					 * on channel width
12097 					 */
12098 					tx_power =
12099 						min_t(s8, tx_power,
12100 						      psd_power + 13 + pwr_lvl_idx * 3);
12101 					max_tx_power[pwr_lvl_idx] =
12102 						min_t(s8,
12103 						      tx_power,
12104 						      reg_tpc_info->tpe[pwr_lvl_idx]);
12105 				}
12106 			/* local power is not PSD power */
12107 			} else {
12108 				/* Connecting AP is psd power */
12109 				if (reg_tpc_info->is_psd_power) {
12110 					is_psd_power = true;
12111 					ath12k_mac_get_psd_channel(ar, 20,
12112 								   &start_freq,
12113 								   &center_freq,
12114 								   pwr_lvl_idx,
12115 								   &temp_chan,
12116 								   &tx_power);
12117 					eirp_power = tx_power;
12118 					max_tx_power[pwr_lvl_idx] =
12119 						reg_tpc_info->tpe[pwr_lvl_idx];
12120 				/* Connecting AP is not psd power */
12121 				} else {
12122 					ath12k_mac_get_eirp_power(ar,
12123 								  &start_freq,
12124 								  &center_freq,
12125 								  pwr_lvl_idx,
12126 								  &temp_chan,
12127 								  &ctx->def,
12128 								  &tx_power);
12129 					max_tx_power[pwr_lvl_idx] =
12130 						min_t(s8,
12131 						      tx_power,
12132 						      reg_tpc_info->tpe[pwr_lvl_idx]);
12133 				}
12134 			}
12135 		/* STA not received TPE IE */
12136 		} else {
12137 			/* local power is PSD power*/
12138 			if (chan->flags & IEEE80211_CHAN_PSD) {
12139 				is_psd_power = true;
12140 				ath12k_mac_get_psd_channel(ar, 20,
12141 							   &start_freq,
12142 							   &center_freq,
12143 							   pwr_lvl_idx,
12144 							   &temp_chan,
12145 							   &tx_power);
12146 				psd_power = temp_chan->psd;
12147 				eirp_power = tx_power;
12148 				max_tx_power[pwr_lvl_idx] = psd_power;
12149 			} else {
12150 				ath12k_mac_get_eirp_power(ar,
12151 							  &start_freq,
12152 							  &center_freq,
12153 							  pwr_lvl_idx,
12154 							  &temp_chan,
12155 							  &ctx->def,
12156 							  &tx_power);
12157 				max_tx_power[pwr_lvl_idx] = tx_power;
12158 			}
12159 		}
12160 
12161 		if (is_psd_power) {
12162 			/* If AP local power constraint is present */
12163 			if (pwr_reduction)
12164 				eirp_power = eirp_power - pwr_reduction;
12165 
12166 			/* If firmware updated max tx power is non zero, then take
12167 			 * the min of firmware updated ap tx power
12168 			 * and max power derived from above mentioned parameters.
12169 			 */
12170 			ath12k_dbg(ab, ATH12K_DBG_MAC,
12171 				   "eirp power : %d firmware report power : %d\n",
12172 				   eirp_power, ar->max_allowed_tx_power);
12173 			/* Firmware reports lower max_allowed_tx_power during vdev
12174 			 * start response. In case of 6 GHz, firmware is not aware
12175 			 * of EIRP power unless driver sets EIRP power through WMI
12176 			 * TPC command. So radio which does not support idle power
12177 			 * save can set maximum calculated EIRP power directly to
12178 			 * firmware through TPC command without min comparison with
12179 			 * vdev start response's max_allowed_tx_power.
12180 			 */
12181 			if (ar->max_allowed_tx_power && ab->hw_params->idle_ps)
12182 				eirp_power = min_t(s8,
12183 						   eirp_power,
12184 						   ar->max_allowed_tx_power);
12185 		} else {
12186 			/* If AP local power constraint is present */
12187 			if (pwr_reduction)
12188 				max_tx_power[pwr_lvl_idx] =
12189 					max_tx_power[pwr_lvl_idx] - pwr_reduction;
12190 			/* If firmware updated max tx power is non zero, then take
12191 			 * the min of firmware updated ap tx power
12192 			 * and max power derived from above mentioned parameters.
12193 			 */
12194 			if (ar->max_allowed_tx_power && ab->hw_params->idle_ps)
12195 				max_tx_power[pwr_lvl_idx] =
12196 					min_t(s8,
12197 					      max_tx_power[pwr_lvl_idx],
12198 					      ar->max_allowed_tx_power);
12199 		}
12200 		reg_tpc_info->chan_power_info[pwr_lvl_idx].chan_cfreq = center_freq;
12201 		reg_tpc_info->chan_power_info[pwr_lvl_idx].tx_power =
12202 			max_tx_power[pwr_lvl_idx];
12203 	}
12204 
12205 	reg_tpc_info->num_pwr_levels = num_pwr_levels;
12206 	reg_tpc_info->is_psd_power = is_psd_power;
12207 	reg_tpc_info->eirp_power = eirp_power;
12208 	if (ahvif->vdev_type == WMI_VDEV_TYPE_STA)
12209 		reg_6ghz_power_mode = bss_conf->power_type;
12210 	else
12211 		/* For now, LPI is the only supported AP power mode */
12212 		reg_6ghz_power_mode = IEEE80211_REG_LPI_AP;
12213 
12214 	reg_tpc_info->ap_power_type =
12215 		ath12k_reg_ap_pwr_convert(reg_6ghz_power_mode);
12216 }
12217 
12218 static void ath12k_mac_parse_tx_pwr_env(struct ath12k *ar,
12219 					struct ath12k_link_vif *arvif)
12220 {
12221 	struct ieee80211_bss_conf *bss_conf = ath12k_mac_get_link_bss_conf(arvif);
12222 	struct ath12k_reg_tpc_power_info *tpc_info = &arvif->reg_tpc_info;
12223 	struct ieee80211_parsed_tpe_eirp *local_non_psd, *reg_non_psd;
12224 	struct ieee80211_parsed_tpe_psd *local_psd, *reg_psd;
12225 	struct ieee80211_parsed_tpe *tpe = &bss_conf->tpe;
12226 	enum wmi_reg_6g_client_type client_type;
12227 	struct ath12k_reg_info *reg_info;
12228 	struct ath12k_base *ab = ar->ab;
12229 	bool psd_valid, non_psd_valid;
12230 	int i;
12231 
12232 	reg_info = ab->reg_info[ar->pdev_idx];
12233 	client_type = reg_info->client_type;
12234 
12235 	local_psd = &tpe->psd_local[client_type];
12236 	reg_psd = &tpe->psd_reg_client[client_type];
12237 	local_non_psd = &tpe->max_local[client_type];
12238 	reg_non_psd = &tpe->max_reg_client[client_type];
12239 
12240 	psd_valid = local_psd->valid | reg_psd->valid;
12241 	non_psd_valid = local_non_psd->valid | reg_non_psd->valid;
12242 
12243 	if (!psd_valid && !non_psd_valid) {
12244 		ath12k_warn(ab,
12245 			    "no transmit power envelope match client power type %d\n",
12246 			    client_type);
12247 		return;
12248 	}
12249 
12250 	if (psd_valid) {
12251 		tpc_info->is_psd_power = true;
12252 
12253 		tpc_info->num_pwr_levels = max(local_psd->count,
12254 					       reg_psd->count);
12255 		tpc_info->num_pwr_levels =
12256 				min3(tpc_info->num_pwr_levels,
12257 				     IEEE80211_TPE_PSD_ENTRIES_320MHZ,
12258 				     ATH12K_NUM_PWR_LEVELS);
12259 
12260 		for (i = 0; i < tpc_info->num_pwr_levels; i++) {
12261 			tpc_info->tpe[i] = min(local_psd->power[i],
12262 					       reg_psd->power[i]) / 2;
12263 			ath12k_dbg(ab, ATH12K_DBG_MAC,
12264 				   "TPE PSD power[%d] : %d\n",
12265 				   i, tpc_info->tpe[i]);
12266 		}
12267 	} else {
12268 		tpc_info->is_psd_power = false;
12269 		tpc_info->eirp_power = 0;
12270 
12271 		tpc_info->num_pwr_levels = max(local_non_psd->count,
12272 					       reg_non_psd->count);
12273 		tpc_info->num_pwr_levels =
12274 				min3(tpc_info->num_pwr_levels,
12275 				     IEEE80211_TPE_EIRP_ENTRIES_320MHZ,
12276 				     ATH12K_NUM_PWR_LEVELS);
12277 
12278 		for (i = 0; i < tpc_info->num_pwr_levels; i++) {
12279 			tpc_info->tpe[i] = min(local_non_psd->power[i],
12280 					       reg_non_psd->power[i]) / 2;
12281 			ath12k_dbg(ab, ATH12K_DBG_MAC,
12282 				   "non PSD power[%d] : %d\n",
12283 				   i, tpc_info->tpe[i]);
12284 		}
12285 	}
12286 }
12287 
12288 int
12289 ath12k_mac_op_assign_vif_chanctx(struct ieee80211_hw *hw,
12290 				 struct ieee80211_vif *vif,
12291 				 struct ieee80211_bss_conf *link_conf,
12292 				 struct ieee80211_chanctx_conf *ctx)
12293 {
12294 	struct ath12k_hw *ah = ath12k_hw_to_ah(hw);
12295 	struct ath12k *ar;
12296 	struct ath12k_base *ab;
12297 	struct ath12k_vif *ahvif = ath12k_vif_to_ahvif(vif);
12298 	u8 link_id = link_conf->link_id;
12299 	struct ath12k_link_vif *arvif;
12300 	int ret;
12301 
12302 	lockdep_assert_wiphy(hw->wiphy);
12303 
12304 	/* For multi radio wiphy, the vdev was not created during add_interface
12305 	 * create now since we have a channel ctx now to assign to a specific ar/fw
12306 	 */
12307 	arvif = ath12k_mac_assign_link_vif(ah, vif, link_id);
12308 	if (!arvif) {
12309 		WARN_ON(1);
12310 		return -ENOMEM;
12311 	}
12312 
12313 	ar = ath12k_mac_assign_vif_to_vdev(hw, arvif, ctx);
12314 	if (!ar) {
12315 		ath12k_hw_warn(ah, "failed to assign chanctx for vif %pM link id %u link vif is already started",
12316 			       vif->addr, link_id);
12317 		return -EINVAL;
12318 	}
12319 
12320 	ab = ar->ab;
12321 
12322 	ath12k_dbg(ab, ATH12K_DBG_MAC,
12323 		   "mac chanctx assign ptr %p vdev_id %i\n",
12324 		   ctx, arvif->vdev_id);
12325 
12326 	if (ath12k_wmi_supports_6ghz_cc_ext(ar) &&
12327 	    ctx->def.chan->band == NL80211_BAND_6GHZ &&
12328 	    ahvif->vdev_type == WMI_VDEV_TYPE_STA)
12329 		ath12k_mac_parse_tx_pwr_env(ar, arvif);
12330 
12331 	arvif->punct_bitmap = ctx->def.punctured;
12332 
12333 	/* for some targets bss peer must be created before vdev_start */
12334 	if (ab->hw_params->vdev_start_delay &&
12335 	    ahvif->vdev_type != WMI_VDEV_TYPE_AP &&
12336 	    ahvif->vdev_type != WMI_VDEV_TYPE_MONITOR &&
12337 	    !ath12k_dp_link_peer_exist_by_vdev_id(ath12k_ab_to_dp(ab), arvif->vdev_id)) {
12338 		ret = 0;
12339 		goto out;
12340 	}
12341 
12342 	if (WARN_ON(arvif->is_started)) {
12343 		ret = -EBUSY;
12344 		goto out;
12345 	}
12346 
12347 	if (ahvif->vdev_type == WMI_VDEV_TYPE_MONITOR) {
12348 		ret = ath12k_mac_monitor_start(ar);
12349 		if (ret) {
12350 			ath12k_mac_monitor_vdev_delete(ar);
12351 			goto out;
12352 		}
12353 
12354 		arvif->is_started = true;
12355 		goto out;
12356 	}
12357 
12358 	ret = ath12k_mac_vdev_start(arvif, ctx);
12359 	if (ret) {
12360 		ath12k_warn(ab, "failed to start vdev %i addr %pM on freq %d: %d\n",
12361 			    arvif->vdev_id, vif->addr,
12362 			    ctx->def.chan->center_freq, ret);
12363 		goto out;
12364 	}
12365 
12366 	arvif->is_started = true;
12367 
12368 	/* TODO: Setup ps and cts/rts protection */
12369 
12370 out:
12371 	return ret;
12372 }
12373 EXPORT_SYMBOL(ath12k_mac_op_assign_vif_chanctx);
12374 
12375 void
12376 ath12k_mac_op_unassign_vif_chanctx(struct ieee80211_hw *hw,
12377 				   struct ieee80211_vif *vif,
12378 				   struct ieee80211_bss_conf *link_conf,
12379 				   struct ieee80211_chanctx_conf *ctx)
12380 {
12381 	struct ath12k *ar;
12382 	struct ath12k_base *ab;
12383 	struct ath12k_vif *ahvif = ath12k_vif_to_ahvif(vif);
12384 	struct ath12k_link_vif *arvif;
12385 	u8 link_id = link_conf->link_id;
12386 	int ret;
12387 
12388 	lockdep_assert_wiphy(hw->wiphy);
12389 
12390 	arvif = wiphy_dereference(hw->wiphy, ahvif->link[link_id]);
12391 
12392 	/* The vif is expected to be attached to an ar's VDEV.
12393 	 * We leave the vif/vdev in this function as is
12394 	 * and not delete the vdev symmetric to assign_vif_chanctx()
12395 	 * the VDEV will be deleted and unassigned either during
12396 	 * remove_interface() or when there is a change in channel
12397 	 * that moves the vif to a new ar
12398 	 */
12399 	if (!arvif || !arvif->is_created)
12400 		return;
12401 
12402 	ar = arvif->ar;
12403 	ab = ar->ab;
12404 
12405 	ath12k_dbg(ab, ATH12K_DBG_MAC,
12406 		   "mac chanctx unassign ptr %p vdev_id %i\n",
12407 		   ctx, arvif->vdev_id);
12408 
12409 	WARN_ON(!arvif->is_started);
12410 
12411 	if (ahvif->vdev_type == WMI_VDEV_TYPE_MONITOR) {
12412 		ret = ath12k_mac_monitor_stop(ar);
12413 		if (ret)
12414 			return;
12415 
12416 		arvif->is_started = false;
12417 	}
12418 
12419 	if (ahvif->vdev_type != WMI_VDEV_TYPE_STA &&
12420 	    ahvif->vdev_type != WMI_VDEV_TYPE_MONITOR) {
12421 		ath12k_bss_disassoc(ar, arvif);
12422 		ret = ath12k_mac_vdev_stop(arvif);
12423 		if (ret)
12424 			ath12k_warn(ab, "failed to stop vdev %i: %d\n",
12425 				    arvif->vdev_id, ret);
12426 	}
12427 	arvif->is_started = false;
12428 
12429 	if (test_bit(WMI_TLV_SERVICE_11D_OFFLOAD, ab->wmi_ab.svc_map) &&
12430 	    ahvif->vdev_type == WMI_VDEV_TYPE_STA &&
12431 	    ahvif->vdev_subtype == WMI_VDEV_SUBTYPE_NONE &&
12432 	    ar->state_11d != ATH12K_11D_PREPARING) {
12433 		reinit_completion(&ar->completed_11d_scan);
12434 		ar->state_11d = ATH12K_11D_PREPARING;
12435 	}
12436 
12437 	if (ar->scan.arvif == arvif && ar->scan.state == ATH12K_SCAN_RUNNING) {
12438 		ath12k_scan_abort(ar);
12439 		ar->scan.arvif = NULL;
12440 	}
12441 }
12442 EXPORT_SYMBOL(ath12k_mac_op_unassign_vif_chanctx);
12443 
12444 int
12445 ath12k_mac_op_switch_vif_chanctx(struct ieee80211_hw *hw,
12446 				 struct ieee80211_vif_chanctx_switch *vifs,
12447 				 int n_vifs,
12448 				 enum ieee80211_chanctx_switch_mode mode)
12449 {
12450 	struct ath12k *curr_ar, *new_ar, *group_ar;
12451 	struct ieee80211_vif_chanctx_switch *v;
12452 	int i, j, count = 0;
12453 
12454 	lockdep_assert_wiphy(hw->wiphy);
12455 
12456 	if (n_vifs == 0)
12457 		return 0;
12458 
12459 	struct ath12k **ar_map __free(kfree) = kzalloc_objs(*ar_map, n_vifs);
12460 
12461 	if (!ar_map)
12462 		return -ENOMEM;
12463 
12464 	for (i = 0; i < n_vifs; i++) {
12465 		v = &vifs[i];
12466 
12467 		if (v->old_ctx->def.chan->band != v->new_ctx->def.chan->band) {
12468 			ath12k_generic_dbg(ATH12K_DBG_MAC,
12469 					   "mac chanctx switch band change not supported\n");
12470 			return -EOPNOTSUPP;
12471 		}
12472 
12473 		curr_ar = ath12k_get_ar_by_ctx(hw, v->old_ctx);
12474 		new_ar = ath12k_get_ar_by_ctx(hw, v->new_ctx);
12475 
12476 		if (!curr_ar || !new_ar) {
12477 			ath12k_generic_dbg(ATH12K_DBG_MAC,
12478 					   "unable to determine device for the passed channel ctx\n");
12479 			ath12k_generic_dbg(ATH12K_DBG_MAC,
12480 					   "Old freq %d MHz (device %s) to new freq %d MHz (device %s)\n",
12481 					   v->old_ctx->def.chan->center_freq,
12482 					   curr_ar ? "valid" : "invalid",
12483 					   v->new_ctx->def.chan->center_freq,
12484 					   new_ar ? "valid" : "invalid");
12485 			return -EINVAL;
12486 		}
12487 
12488 		/* Switching a vif between two radios is not allowed */
12489 		if (curr_ar != new_ar) {
12490 			ath12k_dbg(curr_ar->ab, ATH12K_DBG_MAC,
12491 				   "mac chanctx switch to another radio not supported\n");
12492 			return -EOPNOTSUPP;
12493 		}
12494 
12495 		ar_map[i] = curr_ar;
12496 	}
12497 
12498 	/* Group vifs by radio (ar) and process each group independently. */
12499 	bool *processed __free(kfree) = kzalloc_objs(*processed, n_vifs);
12500 
12501 	if (!processed)
12502 		return -ENOMEM;
12503 
12504 	struct ieee80211_vif_chanctx_switch *group_vifs __free(kfree) =
12505 						kzalloc_objs(*group_vifs, n_vifs);
12506 
12507 	if (!group_vifs)
12508 		return -ENOMEM;
12509 
12510 	for (i = 0; i < n_vifs; i++) {
12511 		if (processed[i])
12512 			continue;
12513 
12514 		group_ar = ar_map[i];
12515 
12516 		count = 0;
12517 		for (j = 0; j < n_vifs; j++) {
12518 			if (!processed[j] && ar_map[j] == group_ar) {
12519 				group_vifs[count++] = vifs[j];
12520 				processed[j] = true;
12521 			}
12522 		}
12523 
12524 		ath12k_dbg(group_ar->ab, ATH12K_DBG_MAC,
12525 			   "mac chanctx switch n_vifs %d mode %d\n",
12526 			   count, mode);
12527 		ath12k_mac_update_vif_chan(group_ar, group_vifs, count);
12528 	}
12529 	return 0;
12530 }
12531 EXPORT_SYMBOL(ath12k_mac_op_switch_vif_chanctx);
12532 
12533 static int
12534 ath12k_set_vdev_param_to_all_vifs(struct ath12k *ar, int param, u32 value)
12535 {
12536 	struct ath12k_link_vif *arvif;
12537 	int ret = 0;
12538 
12539 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
12540 
12541 	list_for_each_entry(arvif, &ar->arvifs, list) {
12542 		ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "setting mac vdev %d param %d value %d\n",
12543 			   param, arvif->vdev_id, value);
12544 
12545 		ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
12546 						    param, value);
12547 		if (ret) {
12548 			ath12k_warn(ar->ab, "failed to set param %d for vdev %d: %d\n",
12549 				    param, arvif->vdev_id, ret);
12550 			break;
12551 		}
12552 	}
12553 
12554 	return ret;
12555 }
12556 
12557 /* mac80211 stores device specific RTS/Fragmentation threshold value,
12558  * this is set interface specific to firmware from ath12k driver
12559  */
12560 int ath12k_mac_op_set_rts_threshold(struct ieee80211_hw *hw,
12561 				    int radio_idx, u32 value)
12562 {
12563 	struct ath12k_hw *ah = ath12k_hw_to_ah(hw);
12564 	struct wiphy *wiphy = hw->wiphy;
12565 	struct ath12k *ar;
12566 	int param_id = WMI_VDEV_PARAM_RTS_THRESHOLD;
12567 	int ret = 0, ret_err, i;
12568 
12569 	lockdep_assert_wiphy(hw->wiphy);
12570 
12571 	if (radio_idx >= wiphy->n_radio || radio_idx < -1)
12572 		return -EINVAL;
12573 
12574 	if (radio_idx != -1) {
12575 		/* Update RTS threshold in specified radio */
12576 		ar = ath12k_ah_to_ar(ah, radio_idx);
12577 		ret = ath12k_set_vdev_param_to_all_vifs(ar, param_id, value);
12578 		if (ret) {
12579 			ath12k_warn(ar->ab,
12580 				    "failed to set RTS config for all vdevs of pdev %d",
12581 				    ar->pdev->pdev_id);
12582 			return ret;
12583 		}
12584 
12585 		ar->rts_threshold = value;
12586 		return 0;
12587 	}
12588 
12589 	/* Radio_index passed is -1, so set RTS threshold for all radios. */
12590 	for_each_ar(ah, ar, i) {
12591 		ret = ath12k_set_vdev_param_to_all_vifs(ar, param_id, value);
12592 		if (ret) {
12593 			ath12k_warn(ar->ab, "failed to set RTS config for all vdevs of pdev %d",
12594 				    ar->pdev->pdev_id);
12595 			break;
12596 		}
12597 	}
12598 	if (!ret) {
12599 		/* Setting new RTS threshold for vdevs of all radios passed, so update
12600 		 * the RTS threshold value for all radios
12601 		 */
12602 		for_each_ar(ah, ar, i)
12603 			ar->rts_threshold = value;
12604 		return 0;
12605 	}
12606 
12607 	/* RTS threshold config failed, revert to the previous RTS threshold */
12608 	for (i = i - 1; i >= 0; i--) {
12609 		ar = ath12k_ah_to_ar(ah, i);
12610 		ret_err = ath12k_set_vdev_param_to_all_vifs(ar, param_id,
12611 							    ar->rts_threshold);
12612 		if (ret_err)
12613 			ath12k_warn(ar->ab,
12614 				    "failed to restore RTS threshold for all vdevs of pdev %d",
12615 				    ar->pdev->pdev_id);
12616 	}
12617 
12618 	return ret;
12619 }
12620 EXPORT_SYMBOL(ath12k_mac_op_set_rts_threshold);
12621 
12622 int ath12k_mac_op_set_frag_threshold(struct ieee80211_hw *hw,
12623 				     int radio_idx, u32 value)
12624 {
12625 	/* Even though there's a WMI vdev param for fragmentation threshold no
12626 	 * known firmware actually implements it. Moreover it is not possible to
12627 	 * rely frame fragmentation to mac80211 because firmware clears the
12628 	 * "more fragments" bit in frame control making it impossible for remote
12629 	 * devices to reassemble frames.
12630 	 *
12631 	 * Hence implement a dummy callback just to say fragmentation isn't
12632 	 * supported. This effectively prevents mac80211 from doing frame
12633 	 * fragmentation in software.
12634 	 */
12635 
12636 	lockdep_assert_wiphy(hw->wiphy);
12637 
12638 	return -EOPNOTSUPP;
12639 }
12640 EXPORT_SYMBOL(ath12k_mac_op_set_frag_threshold);
12641 
12642 static int ath12k_mac_flush(struct ath12k *ar)
12643 {
12644 	long time_left;
12645 	int ret = 0;
12646 
12647 	time_left = wait_event_timeout(ar->dp.tx_empty_waitq,
12648 				       (atomic_read(&ar->dp.num_tx_pending) == 0),
12649 				       ATH12K_FLUSH_TIMEOUT);
12650 	if (time_left == 0) {
12651 		ath12k_warn(ar->ab,
12652 			    "failed to flush transmit queue, data pkts pending %d\n",
12653 			    atomic_read(&ar->dp.num_tx_pending));
12654 		ret = -ETIMEDOUT;
12655 	}
12656 
12657 	time_left = wait_event_timeout(ar->txmgmt_empty_waitq,
12658 				       (atomic_read(&ar->num_pending_mgmt_tx) == 0),
12659 				       ATH12K_FLUSH_TIMEOUT);
12660 	if (time_left == 0) {
12661 		ath12k_warn(ar->ab,
12662 			    "failed to flush mgmt transmit queue, mgmt pkts pending %d\n",
12663 			    atomic_read(&ar->num_pending_mgmt_tx));
12664 		ret = -ETIMEDOUT;
12665 	}
12666 
12667 	return ret;
12668 }
12669 
12670 int ath12k_mac_wait_tx_complete(struct ath12k *ar)
12671 {
12672 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
12673 
12674 	ath12k_mac_drain_tx(ar);
12675 	return ath12k_mac_flush(ar);
12676 }
12677 
12678 void ath12k_mac_op_flush(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
12679 			 u32 queues, bool drop)
12680 {
12681 	struct ath12k_hw *ah = ath12k_hw_to_ah(hw);
12682 	struct ath12k_link_vif *arvif;
12683 	struct ath12k_vif *ahvif;
12684 	unsigned long links;
12685 	struct ath12k *ar;
12686 	u8 link_id;
12687 	int i;
12688 
12689 	lockdep_assert_wiphy(hw->wiphy);
12690 
12691 	if (drop)
12692 		return;
12693 
12694 	for_each_ar(ah, ar, i)
12695 		wiphy_work_flush(hw->wiphy, &ar->wmi_mgmt_tx_work);
12696 
12697 	/* vif can be NULL when flush() is considered for hw */
12698 	if (!vif) {
12699 		for_each_ar(ah, ar, i)
12700 			ath12k_mac_flush(ar);
12701 		return;
12702 	}
12703 
12704 	ahvif = ath12k_vif_to_ahvif(vif);
12705 	links = ahvif->links_map;
12706 	for_each_set_bit(link_id, &links, IEEE80211_MLD_MAX_NUM_LINKS) {
12707 		arvif = wiphy_dereference(hw->wiphy, ahvif->link[link_id]);
12708 		if (!(arvif && arvif->ar))
12709 			continue;
12710 
12711 		ath12k_mac_flush(arvif->ar);
12712 	}
12713 }
12714 EXPORT_SYMBOL(ath12k_mac_op_flush);
12715 
12716 static int
12717 ath12k_mac_bitrate_mask_num_ht_rates(struct ath12k *ar,
12718 				     enum nl80211_band band,
12719 				     const struct cfg80211_bitrate_mask *mask)
12720 {
12721 	int num_rates = 0;
12722 	int i;
12723 
12724 	for (i = 0; i < ARRAY_SIZE(mask->control[band].ht_mcs); i++)
12725 		num_rates += hweight16(mask->control[band].ht_mcs[i]);
12726 
12727 	return num_rates;
12728 }
12729 
12730 static bool
12731 ath12k_mac_has_single_legacy_rate(struct ath12k *ar,
12732 				  enum nl80211_band band,
12733 				  const struct cfg80211_bitrate_mask *mask)
12734 {
12735 	int num_rates = 0;
12736 
12737 	num_rates = hweight32(mask->control[band].legacy);
12738 
12739 	if (ath12k_mac_bitrate_mask_num_ht_rates(ar, band, mask))
12740 		return false;
12741 
12742 	if (ath12k_mac_bitrate_mask_num_vht_rates(ar, band, mask))
12743 		return false;
12744 
12745 	if (ath12k_mac_bitrate_mask_num_he_rates(ar, band, mask))
12746 		return false;
12747 
12748 	if (ath12k_mac_bitrate_mask_num_eht_rates(ar, band, mask))
12749 		return false;
12750 
12751 	return num_rates == 1;
12752 }
12753 
12754 static __le16
12755 ath12k_mac_get_tx_mcs_map(const struct ieee80211_sta_he_cap *he_cap)
12756 {
12757 	if (he_cap->he_cap_elem.phy_cap_info[0] &
12758 	    IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_160MHZ_IN_5G)
12759 		return he_cap->he_mcs_nss_supp.tx_mcs_160;
12760 
12761 	return he_cap->he_mcs_nss_supp.tx_mcs_80;
12762 }
12763 
12764 static bool
12765 ath12k_mac_bitrate_mask_get_single_nss(struct ath12k *ar,
12766 				       struct ieee80211_vif *vif,
12767 				       enum nl80211_band band,
12768 				       const struct cfg80211_bitrate_mask *mask,
12769 				       int *nss)
12770 {
12771 	struct ieee80211_supported_band *sband = &ar->mac.sbands[band];
12772 	u16 vht_mcs_map = le16_to_cpu(sband->vht_cap.vht_mcs.tx_mcs_map);
12773 	const struct ieee80211_sband_iftype_data *data;
12774 	const struct ieee80211_sta_he_cap *he_cap;
12775 	u16 he_mcs_map = 0;
12776 	u16 eht_mcs_map = 0;
12777 	u8 ht_nss_mask = 0;
12778 	u8 vht_nss_mask = 0;
12779 	u8 he_nss_mask = 0;
12780 	u8 eht_nss_mask = 0;
12781 	u8 mcs_nss_len;
12782 	int i;
12783 
12784 	/* No need to consider legacy here. Basic rates are always present
12785 	 * in bitrate mask
12786 	 */
12787 
12788 	for (i = 0; i < ARRAY_SIZE(mask->control[band].ht_mcs); i++) {
12789 		if (mask->control[band].ht_mcs[i] == 0)
12790 			continue;
12791 		else if (mask->control[band].ht_mcs[i] ==
12792 			 sband->ht_cap.mcs.rx_mask[i])
12793 			ht_nss_mask |= BIT(i);
12794 		else
12795 			return false;
12796 	}
12797 
12798 	for (i = 0; i < ARRAY_SIZE(mask->control[band].vht_mcs); i++) {
12799 		if (mask->control[band].vht_mcs[i] == 0)
12800 			continue;
12801 		else if (mask->control[band].vht_mcs[i] ==
12802 			 ath12k_mac_get_max_vht_mcs_map(vht_mcs_map, i))
12803 			vht_nss_mask |= BIT(i);
12804 		else
12805 			return false;
12806 	}
12807 
12808 	he_cap = ieee80211_get_he_iftype_cap_vif(sband, vif);
12809 	if (!he_cap)
12810 		return false;
12811 
12812 	he_mcs_map = le16_to_cpu(ath12k_mac_get_tx_mcs_map(he_cap));
12813 
12814 	for (i = 0; i < ARRAY_SIZE(mask->control[band].he_mcs); i++) {
12815 		if (mask->control[band].he_mcs[i] == 0)
12816 			continue;
12817 
12818 		if (mask->control[band].he_mcs[i] ==
12819 		    ath12k_mac_get_max_he_mcs_map(he_mcs_map, i))
12820 			he_nss_mask |= BIT(i);
12821 		else
12822 			return false;
12823 	}
12824 
12825 	data = ieee80211_get_sband_iftype_data(sband, vif->type);
12826 
12827 	mcs_nss_len = ieee80211_eht_mcs_nss_size(&data->he_cap.he_cap_elem,
12828 						 &data->eht_cap.eht_cap_elem,
12829 						 false);
12830 	if (mcs_nss_len == 4) {
12831 		/* 20 MHz only STA case */
12832 		const struct ieee80211_eht_mcs_nss_supp_20mhz_only *eht_mcs_nss =
12833 			&data->eht_cap.eht_mcs_nss_supp.only_20mhz;
12834 		if (eht_mcs_nss->rx_tx_mcs13_max_nss)
12835 			eht_mcs_map = 0x1fff;
12836 		else if (eht_mcs_nss->rx_tx_mcs11_max_nss)
12837 			eht_mcs_map = 0x07ff;
12838 		else if (eht_mcs_nss->rx_tx_mcs9_max_nss)
12839 			eht_mcs_map = 0x01ff;
12840 		else
12841 			eht_mcs_map = 0x007f;
12842 	} else {
12843 		const struct ieee80211_eht_mcs_nss_supp_bw *eht_mcs_nss;
12844 
12845 		switch (mcs_nss_len) {
12846 		case 9:
12847 			eht_mcs_nss = &data->eht_cap.eht_mcs_nss_supp.bw._320;
12848 			break;
12849 		case 6:
12850 			eht_mcs_nss = &data->eht_cap.eht_mcs_nss_supp.bw._160;
12851 			break;
12852 		case 3:
12853 			eht_mcs_nss = &data->eht_cap.eht_mcs_nss_supp.bw._80;
12854 			break;
12855 		default:
12856 			return false;
12857 		}
12858 
12859 		if (eht_mcs_nss->rx_tx_mcs13_max_nss)
12860 			eht_mcs_map = 0x1fff;
12861 		else if (eht_mcs_nss->rx_tx_mcs11_max_nss)
12862 			eht_mcs_map = 0x7ff;
12863 		else
12864 			eht_mcs_map = 0x1ff;
12865 	}
12866 
12867 	for (i = 0; i < ARRAY_SIZE(mask->control[band].eht_mcs); i++) {
12868 		if (mask->control[band].eht_mcs[i] == 0)
12869 			continue;
12870 
12871 		if (mask->control[band].eht_mcs[i] < eht_mcs_map)
12872 			eht_nss_mask |= BIT(i);
12873 		else
12874 			return false;
12875 	}
12876 
12877 	if (ht_nss_mask != vht_nss_mask || ht_nss_mask != he_nss_mask ||
12878 	    ht_nss_mask != eht_nss_mask)
12879 		return false;
12880 
12881 	if (ht_nss_mask == 0)
12882 		return false;
12883 
12884 	if (BIT(fls(ht_nss_mask)) - 1 != ht_nss_mask)
12885 		return false;
12886 
12887 	*nss = fls(ht_nss_mask);
12888 
12889 	return true;
12890 }
12891 
12892 static int
12893 ath12k_mac_get_single_legacy_rate(struct ath12k *ar,
12894 				  enum nl80211_band band,
12895 				  const struct cfg80211_bitrate_mask *mask,
12896 				  u32 *rate, u8 *nss)
12897 {
12898 	int rate_idx;
12899 	u16 bitrate;
12900 	u8 preamble;
12901 	u8 hw_rate;
12902 
12903 	if (hweight32(mask->control[band].legacy) != 1)
12904 		return -EINVAL;
12905 
12906 	rate_idx = ffs(mask->control[band].legacy) - 1;
12907 
12908 	if (band == NL80211_BAND_5GHZ || band == NL80211_BAND_6GHZ)
12909 		rate_idx += ATH12K_MAC_FIRST_OFDM_RATE_IDX;
12910 
12911 	hw_rate = ath12k_legacy_rates[rate_idx].hw_value;
12912 	bitrate = ath12k_legacy_rates[rate_idx].bitrate;
12913 
12914 	if (ath12k_mac_bitrate_is_cck(bitrate))
12915 		preamble = WMI_RATE_PREAMBLE_CCK;
12916 	else
12917 		preamble = WMI_RATE_PREAMBLE_OFDM;
12918 
12919 	*nss = 1;
12920 	*rate = ATH12K_HW_RATE_CODE(hw_rate, 0, preamble);
12921 
12922 	return 0;
12923 }
12924 
12925 static int
12926 ath12k_mac_set_fixed_rate_gi_ltf(struct ath12k_link_vif *arvif, u8 gi, u8 ltf,
12927 				 u32 param)
12928 {
12929 	struct ath12k *ar = arvif->ar;
12930 	int ret;
12931 
12932 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
12933 
12934 	/* 0.8 = 0, 1.6 = 2 and 3.2 = 3. */
12935 	if (gi && gi != 0xFF)
12936 		gi += 1;
12937 
12938 	ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
12939 					    WMI_VDEV_PARAM_SGI, gi);
12940 	if (ret) {
12941 		ath12k_warn(ar->ab, "failed to set GI:%d, error:%d\n",
12942 			    gi, ret);
12943 		return ret;
12944 	}
12945 
12946 	if (param == WMI_VDEV_PARAM_HE_LTF) {
12947 		/* HE values start from 1 */
12948 		if (ltf != 0xFF)
12949 			ltf += 1;
12950 	} else {
12951 		/* EHT values start from 5 */
12952 		if (ltf != 0xFF)
12953 			ltf += 4;
12954 	}
12955 
12956 	ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
12957 					    param, ltf);
12958 	if (ret) {
12959 		ath12k_warn(ar->ab, "failed to set LTF:%d, error:%d\n",
12960 			    ltf, ret);
12961 		return ret;
12962 	}
12963 	return 0;
12964 }
12965 
12966 static int
12967 ath12k_mac_set_auto_rate_gi_ltf(struct ath12k_link_vif *arvif, u16 gi, u8 ltf)
12968 {
12969 	struct ath12k *ar = arvif->ar;
12970 	int ret;
12971 	u32 ar_gi_ltf;
12972 
12973 	if (gi != 0xFF) {
12974 		switch (gi) {
12975 		case ATH12K_RATE_INFO_GI_0_8:
12976 			gi = WMI_AUTORATE_800NS_GI;
12977 			break;
12978 		case ATH12K_RATE_INFO_GI_1_6:
12979 			gi = WMI_AUTORATE_1600NS_GI;
12980 			break;
12981 		case ATH12K_RATE_INFO_GI_3_2:
12982 			gi = WMI_AUTORATE_3200NS_GI;
12983 			break;
12984 		default:
12985 			ath12k_warn(ar->ab, "Invalid GI\n");
12986 			return -EINVAL;
12987 		}
12988 	}
12989 
12990 	if (ltf != 0xFF) {
12991 		switch (ltf) {
12992 		case ATH12K_RATE_INFO_1XLTF:
12993 			ltf = WMI_AUTORATE_LTF_1X;
12994 			break;
12995 		case ATH12K_RATE_INFO_2XLTF:
12996 			ltf = WMI_AUTORATE_LTF_2X;
12997 			break;
12998 		case ATH12K_RATE_INFO_4XLTF:
12999 			ltf = WMI_AUTORATE_LTF_4X;
13000 			break;
13001 		default:
13002 			ath12k_warn(ar->ab, "Invalid LTF\n");
13003 			return -EINVAL;
13004 		}
13005 	}
13006 
13007 	ar_gi_ltf = gi | ltf;
13008 
13009 	ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
13010 					    WMI_VDEV_PARAM_AUTORATE_MISC_CFG,
13011 					    ar_gi_ltf);
13012 	if (ret) {
13013 		ath12k_warn(ar->ab,
13014 			    "failed to set autorate GI:%u, LTF:%u params, error:%d\n",
13015 			    gi, ltf, ret);
13016 		return ret;
13017 	}
13018 
13019 	return 0;
13020 }
13021 
13022 static u32 ath12k_mac_nlgi_to_wmigi(enum nl80211_txrate_gi gi)
13023 {
13024 	switch (gi) {
13025 	case NL80211_TXRATE_DEFAULT_GI:
13026 		return WMI_GI_400_NS;
13027 	case NL80211_TXRATE_FORCE_LGI:
13028 		return WMI_GI_800_NS;
13029 	default:
13030 		return WMI_GI_400_NS;
13031 	}
13032 }
13033 
13034 static int ath12k_mac_set_rate_params(struct ath12k_link_vif *arvif,
13035 				      u32 rate, u8 nss, u8 sgi, u8 ldpc,
13036 				      u8 he_gi, u8 he_ltf, bool he_fixed_rate,
13037 				      u8 eht_gi, u8 eht_ltf,
13038 				      bool eht_fixed_rate)
13039 {
13040 	struct ieee80211_bss_conf *link_conf;
13041 	struct ath12k *ar = arvif->ar;
13042 	bool he_support, eht_support, gi_ltf_set = false;
13043 	u32 vdev_param;
13044 	u32 param_value;
13045 	int ret;
13046 
13047 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
13048 
13049 	link_conf = ath12k_mac_get_link_bss_conf(arvif);
13050 	if (!link_conf)
13051 		return -EINVAL;
13052 
13053 	he_support = link_conf->he_support;
13054 	eht_support = link_conf->eht_support;
13055 
13056 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
13057 		   "mac set rate params vdev %i rate 0x%02x nss 0x%02x sgi 0x%02x ldpc 0x%02x\n",
13058 		   arvif->vdev_id, rate, nss, sgi, ldpc);
13059 
13060 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
13061 		   "he_gi 0x%02x he_ltf 0x%02x he_fixed_rate %d\n", he_gi,
13062 		   he_ltf, he_fixed_rate);
13063 
13064 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
13065 		   "eht_gi 0x%02x eht_ltf 0x%02x eht_fixed_rate %d\n",
13066 		   eht_gi, eht_ltf, eht_fixed_rate);
13067 
13068 	if (!he_support && !eht_support) {
13069 		vdev_param = WMI_VDEV_PARAM_FIXED_RATE;
13070 		ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
13071 						    vdev_param, rate);
13072 		if (ret) {
13073 			ath12k_warn(ar->ab, "failed to set fixed rate param 0x%02x: %d\n",
13074 				    rate, ret);
13075 			return ret;
13076 		}
13077 	}
13078 
13079 	vdev_param = WMI_VDEV_PARAM_NSS;
13080 
13081 	ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
13082 					    vdev_param, nss);
13083 	if (ret) {
13084 		ath12k_warn(ar->ab, "failed to set nss param %d: %d\n",
13085 			    nss, ret);
13086 		return ret;
13087 	}
13088 
13089 	ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
13090 					    WMI_VDEV_PARAM_LDPC, ldpc);
13091 	if (ret) {
13092 		ath12k_warn(ar->ab, "failed to set ldpc param %d: %d\n",
13093 			    ldpc, ret);
13094 		return ret;
13095 	}
13096 
13097 	if (eht_support) {
13098 		if (eht_fixed_rate)
13099 			ret = ath12k_mac_set_fixed_rate_gi_ltf(arvif, eht_gi, eht_ltf,
13100 							       WMI_VDEV_PARAM_EHT_LTF);
13101 		else
13102 			ret = ath12k_mac_set_auto_rate_gi_ltf(arvif, eht_gi, eht_ltf);
13103 
13104 		if (ret) {
13105 			ath12k_warn(ar->ab,
13106 				    "failed to set EHT LTF/GI params %d/%d: %d\n",
13107 				    eht_gi, eht_ltf, ret);
13108 			return ret;
13109 		}
13110 		gi_ltf_set = true;
13111 	}
13112 
13113 	if (he_support) {
13114 		if (he_fixed_rate)
13115 			ret = ath12k_mac_set_fixed_rate_gi_ltf(arvif, he_gi, he_ltf,
13116 							       WMI_VDEV_PARAM_HE_LTF);
13117 		else
13118 			ret = ath12k_mac_set_auto_rate_gi_ltf(arvif, he_gi, he_ltf);
13119 		if (ret)
13120 			return ret;
13121 		gi_ltf_set = true;
13122 	}
13123 
13124 	if (!gi_ltf_set) {
13125 		vdev_param = WMI_VDEV_PARAM_SGI;
13126 		param_value = ath12k_mac_nlgi_to_wmigi(sgi);
13127 		ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
13128 						    vdev_param, param_value);
13129 		if (ret) {
13130 			ath12k_warn(ar->ab, "failed to set sgi param %d: %d\n",
13131 				    sgi, ret);
13132 			return ret;
13133 		}
13134 	}
13135 
13136 	return 0;
13137 }
13138 
13139 static bool
13140 ath12k_mac_vht_mcs_range_present(struct ath12k *ar,
13141 				 enum nl80211_band band,
13142 				 const struct cfg80211_bitrate_mask *mask)
13143 {
13144 	int i;
13145 	u16 vht_mcs;
13146 
13147 	for (i = 0; i < NL80211_VHT_NSS_MAX; i++) {
13148 		vht_mcs = mask->control[band].vht_mcs[i];
13149 
13150 		switch (vht_mcs) {
13151 		case 0:
13152 		case BIT(8) - 1:
13153 		case BIT(9) - 1:
13154 		case BIT(10) - 1:
13155 			break;
13156 		default:
13157 			return false;
13158 		}
13159 	}
13160 
13161 	return true;
13162 }
13163 
13164 static bool
13165 ath12k_mac_he_mcs_range_present(struct ath12k *ar,
13166 				enum nl80211_band band,
13167 				const struct cfg80211_bitrate_mask *mask)
13168 {
13169 	int i;
13170 	u16 he_mcs;
13171 
13172 	for (i = 0; i < NL80211_HE_NSS_MAX; i++) {
13173 		he_mcs = mask->control[band].he_mcs[i];
13174 
13175 		switch (he_mcs) {
13176 		case 0:
13177 		case BIT(8) - 1:
13178 		case BIT(10) - 1:
13179 		case BIT(12) - 1:
13180 			break;
13181 		default:
13182 			return false;
13183 		}
13184 	}
13185 
13186 	return true;
13187 }
13188 
13189 static bool
13190 ath12k_mac_eht_mcs_range_present(struct ath12k *ar,
13191 				 enum nl80211_band band,
13192 				 const struct cfg80211_bitrate_mask *mask)
13193 {
13194 	u16 eht_mcs;
13195 	int i;
13196 
13197 	for (i = 0; i < NL80211_EHT_NSS_MAX; i++) {
13198 		eht_mcs = mask->control[band].eht_mcs[i];
13199 
13200 		switch (eht_mcs) {
13201 		case 0:
13202 		case BIT(8) - 1:
13203 		case BIT(10) - 1:
13204 		case BIT(12) - 1:
13205 		case BIT(14) - 1:
13206 			break;
13207 		case BIT(15) - 1:
13208 		case BIT(16) - 1:
13209 		case BIT(16) - BIT(14) - 1:
13210 			if (i != 0)
13211 				return false;
13212 			break;
13213 		default:
13214 			return false;
13215 		}
13216 	}
13217 
13218 	return true;
13219 }
13220 
13221 static void ath12k_mac_set_bitrate_mask_iter(void *data,
13222 					     struct ieee80211_sta *sta)
13223 {
13224 	struct ath12k_link_vif *arvif = data;
13225 	struct ath12k_sta *ahsta = ath12k_sta_to_ahsta(sta);
13226 	struct ath12k_link_sta *arsta;
13227 	struct ath12k *ar = arvif->ar;
13228 
13229 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
13230 
13231 	arsta = wiphy_dereference(ath12k_ar_to_hw(ar)->wiphy,
13232 				  ahsta->link[arvif->link_id]);
13233 	if (!arsta || arsta->arvif != arvif)
13234 		return;
13235 
13236 	spin_lock_bh(&ar->data_lock);
13237 	arsta->changed |= IEEE80211_RC_SUPP_RATES_CHANGED;
13238 	spin_unlock_bh(&ar->data_lock);
13239 
13240 	wiphy_work_queue(ath12k_ar_to_hw(ar)->wiphy, &arsta->update_wk);
13241 }
13242 
13243 static void ath12k_mac_disable_peer_fixed_rate(void *data,
13244 					       struct ieee80211_sta *sta)
13245 {
13246 	struct ath12k_link_vif *arvif = data;
13247 	struct ath12k_sta *ahsta = ath12k_sta_to_ahsta(sta);
13248 	struct ath12k_link_sta *arsta;
13249 	struct ath12k *ar = arvif->ar;
13250 	int ret;
13251 
13252 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
13253 
13254 	arsta = wiphy_dereference(ath12k_ar_to_hw(ar)->wiphy,
13255 				  ahsta->link[arvif->link_id]);
13256 
13257 	if (!arsta || arsta->arvif != arvif)
13258 		return;
13259 
13260 	ret = ath12k_wmi_set_peer_param(ar, arsta->addr,
13261 					arvif->vdev_id,
13262 					WMI_PEER_PARAM_FIXED_RATE,
13263 					WMI_FIXED_RATE_NONE);
13264 	if (ret)
13265 		ath12k_warn(ar->ab,
13266 			    "failed to disable peer fixed rate for STA %pM ret %d\n",
13267 			    arsta->addr, ret);
13268 }
13269 
13270 static bool
13271 ath12k_mac_validate_fixed_rate_settings(struct ath12k *ar, enum nl80211_band band,
13272 					const struct cfg80211_bitrate_mask *mask,
13273 					unsigned int link_id)
13274 {
13275 	bool eht_fixed_rate = false, he_fixed_rate = false, vht_fixed_rate = false;
13276 	const u16 *vht_mcs_mask, *he_mcs_mask, *eht_mcs_mask;
13277 	struct ieee80211_link_sta *link_sta;
13278 	struct ath12k_dp_link_peer *peer, *tmp;
13279 	u8 vht_nss, he_nss, eht_nss;
13280 	int ret = true;
13281 	struct ath12k_base *ab = ar->ab;
13282 	struct ath12k_dp *dp = ath12k_ab_to_dp(ab);
13283 
13284 	vht_mcs_mask = mask->control[band].vht_mcs;
13285 	he_mcs_mask = mask->control[band].he_mcs;
13286 	eht_mcs_mask = mask->control[band].eht_mcs;
13287 
13288 	if (ath12k_mac_bitrate_mask_num_vht_rates(ar, band, mask) == 1)
13289 		vht_fixed_rate = true;
13290 
13291 	if (ath12k_mac_bitrate_mask_num_he_rates(ar, band, mask) == 1)
13292 		he_fixed_rate = true;
13293 
13294 	if (ath12k_mac_bitrate_mask_num_eht_rates(ar, band, mask) == 1)
13295 		eht_fixed_rate = true;
13296 
13297 	if (!vht_fixed_rate && !he_fixed_rate && !eht_fixed_rate)
13298 		return true;
13299 
13300 	vht_nss = ath12k_mac_max_vht_nss(vht_mcs_mask);
13301 	he_nss =  ath12k_mac_max_he_nss(he_mcs_mask);
13302 	eht_nss = ath12k_mac_max_eht_nss(eht_mcs_mask);
13303 
13304 	rcu_read_lock();
13305 	spin_lock_bh(&dp->dp_lock);
13306 	list_for_each_entry_safe(peer, tmp, &dp->peers, list) {
13307 		if (peer->sta) {
13308 			link_sta = rcu_dereference(peer->sta->link[link_id]);
13309 			if (!link_sta) {
13310 				ret = false;
13311 				goto exit;
13312 			}
13313 
13314 			if (vht_fixed_rate && (!link_sta->vht_cap.vht_supported ||
13315 					       link_sta->rx_nss < vht_nss)) {
13316 				ret = false;
13317 				goto exit;
13318 			}
13319 			if (he_fixed_rate && (!link_sta->he_cap.has_he ||
13320 					      link_sta->rx_nss < he_nss)) {
13321 				ret = false;
13322 				goto exit;
13323 			}
13324 			if (eht_fixed_rate && (!link_sta->eht_cap.has_eht ||
13325 					       link_sta->rx_nss < eht_nss)) {
13326 				ret = false;
13327 				goto exit;
13328 			}
13329 		}
13330 	}
13331 exit:
13332 	spin_unlock_bh(&dp->dp_lock);
13333 	rcu_read_unlock();
13334 	return ret;
13335 }
13336 
13337 int
13338 ath12k_mac_op_set_bitrate_mask(struct ieee80211_hw *hw,
13339 			       struct ieee80211_vif *vif,
13340 			       const struct cfg80211_bitrate_mask *mask)
13341 {
13342 	struct ath12k_vif *ahvif = ath12k_vif_to_ahvif(vif);
13343 	struct ath12k_link_vif *arvif;
13344 	struct cfg80211_chan_def def;
13345 	struct ath12k *ar;
13346 	enum nl80211_band band;
13347 	const u8 *ht_mcs_mask;
13348 	const u16 *vht_mcs_mask;
13349 	const u16 *he_mcs_mask;
13350 	const u16 *eht_mcs_mask;
13351 	u8 he_ltf = 0;
13352 	u8 he_gi = 0;
13353 	u8 eht_ltf = 0, eht_gi = 0;
13354 	u32 rate;
13355 	u8 nss, mac_nss;
13356 	u8 sgi;
13357 	u8 ldpc;
13358 	int single_nss;
13359 	int ret;
13360 	int num_rates;
13361 	bool he_fixed_rate = false;
13362 	bool eht_fixed_rate = false;
13363 
13364 	lockdep_assert_wiphy(hw->wiphy);
13365 
13366 	arvif = &ahvif->deflink;
13367 
13368 	ar = arvif->ar;
13369 	if (ath12k_mac_vif_link_chan(vif, arvif->link_id, &def)) {
13370 		ret = -EPERM;
13371 		goto out;
13372 	}
13373 
13374 	band = def.chan->band;
13375 	ht_mcs_mask = mask->control[band].ht_mcs;
13376 	vht_mcs_mask = mask->control[band].vht_mcs;
13377 	he_mcs_mask = mask->control[band].he_mcs;
13378 	eht_mcs_mask = mask->control[band].eht_mcs;
13379 	ldpc = !!(ar->ht_cap_info & WMI_HT_CAP_LDPC);
13380 
13381 	sgi = mask->control[band].gi;
13382 	if (sgi == NL80211_TXRATE_FORCE_SGI) {
13383 		ret = -EINVAL;
13384 		goto out;
13385 	}
13386 
13387 	he_gi = mask->control[band].he_gi;
13388 	he_ltf = mask->control[band].he_ltf;
13389 
13390 	eht_gi = mask->control[band].eht_gi;
13391 	eht_ltf = mask->control[band].eht_ltf;
13392 
13393 	/* mac80211 doesn't support sending a fixed HT/VHT MCS alone, rather it
13394 	 * requires passing at least one of used basic rates along with them.
13395 	 * Fixed rate setting across different preambles(legacy, HT, VHT) is
13396 	 * not supported by the FW. Hence use of FIXED_RATE vdev param is not
13397 	 * suitable for setting single HT/VHT rates.
13398 	 * But, there could be a single basic rate passed from userspace which
13399 	 * can be done through the FIXED_RATE param.
13400 	 */
13401 	if (ath12k_mac_has_single_legacy_rate(ar, band, mask)) {
13402 		ret = ath12k_mac_get_single_legacy_rate(ar, band, mask, &rate,
13403 							&nss);
13404 		if (ret) {
13405 			ath12k_warn(ar->ab, "failed to get single legacy rate for vdev %i: %d\n",
13406 				    arvif->vdev_id, ret);
13407 			goto out;
13408 		}
13409 
13410 		ieee80211_iterate_stations_mtx(hw,
13411 					       ath12k_mac_disable_peer_fixed_rate,
13412 					       arvif);
13413 	} else if (ath12k_mac_bitrate_mask_get_single_nss(ar, vif, band, mask,
13414 							  &single_nss)) {
13415 		rate = WMI_FIXED_RATE_NONE;
13416 		nss = single_nss;
13417 		arvif->bitrate_mask = *mask;
13418 
13419 		ieee80211_iterate_stations_atomic(hw,
13420 						  ath12k_mac_set_bitrate_mask_iter,
13421 						  arvif);
13422 	} else {
13423 		rate = WMI_FIXED_RATE_NONE;
13424 
13425 		if (!ath12k_mac_validate_fixed_rate_settings(ar, band,
13426 							     mask, arvif->link_id))
13427 			ath12k_warn(ar->ab,
13428 				    "failed to update fixed rate settings due to mcs/nss incompatibility\n");
13429 
13430 		mac_nss = max(max3(ath12k_mac_max_ht_nss(ht_mcs_mask),
13431 				   ath12k_mac_max_vht_nss(vht_mcs_mask),
13432 				   ath12k_mac_max_he_nss(he_mcs_mask)),
13433 			       ath12k_mac_max_eht_nss(eht_mcs_mask));
13434 		nss = min_t(u32, ar->num_tx_chains, mac_nss);
13435 
13436 		/* If multiple rates across different preambles are given
13437 		 * we can reconfigure this info with all peers using PEER_ASSOC
13438 		 * command with the below exception cases.
13439 		 * - Single VHT Rate : peer_assoc command accommodates only MCS
13440 		 * range values i.e 0-7, 0-8, 0-9 for VHT. Though mac80211
13441 		 * mandates passing basic rates along with HT/VHT rates, FW
13442 		 * doesn't allow switching from VHT to Legacy. Hence instead of
13443 		 * setting legacy and VHT rates using RATEMASK_CMD vdev cmd,
13444 		 * we could set this VHT rate as peer fixed rate param, which
13445 		 * will override FIXED rate and FW rate control algorithm.
13446 		 * If single VHT rate is passed along with HT rates, we select
13447 		 * the VHT rate as fixed rate for vht peers.
13448 		 * - Multiple VHT Rates : When Multiple VHT rates are given,this
13449 		 * can be set using RATEMASK CMD which uses FW rate-ctl alg.
13450 		 * TODO: Setting multiple VHT MCS and replacing peer_assoc with
13451 		 * RATEMASK_CMDID can cover all use cases of setting rates
13452 		 * across multiple preambles and rates within same type.
13453 		 * But requires more validation of the command at this point.
13454 		 */
13455 
13456 		num_rates = ath12k_mac_bitrate_mask_num_vht_rates(ar, band,
13457 								  mask);
13458 
13459 		if (!ath12k_mac_vht_mcs_range_present(ar, band, mask) &&
13460 		    num_rates > 1) {
13461 			/* TODO: Handle multiple VHT MCS values setting using
13462 			 * RATEMASK CMD
13463 			 */
13464 			ath12k_warn(ar->ab,
13465 				    "Setting more than one MCS Value in bitrate mask not supported\n");
13466 			ret = -EINVAL;
13467 			goto out;
13468 		}
13469 
13470 		num_rates = ath12k_mac_bitrate_mask_num_he_rates(ar, band, mask);
13471 		if (num_rates == 1)
13472 			he_fixed_rate = true;
13473 
13474 		if (!ath12k_mac_he_mcs_range_present(ar, band, mask) &&
13475 		    num_rates > 1) {
13476 			ath12k_warn(ar->ab,
13477 				    "Setting more than one HE MCS Value in bitrate mask not supported\n");
13478 			ret = -EINVAL;
13479 			goto out;
13480 		}
13481 
13482 		num_rates = ath12k_mac_bitrate_mask_num_eht_rates(ar, band,
13483 								  mask);
13484 		if (num_rates == 1)
13485 			eht_fixed_rate = true;
13486 
13487 		if (!ath12k_mac_eht_mcs_range_present(ar, band, mask) &&
13488 		    num_rates > 1) {
13489 			ath12k_warn(ar->ab,
13490 				    "Setting more than one EHT MCS Value in bitrate mask not supported\n");
13491 			ret = -EINVAL;
13492 			goto out;
13493 		}
13494 
13495 		ieee80211_iterate_stations_mtx(hw,
13496 					       ath12k_mac_disable_peer_fixed_rate,
13497 					       arvif);
13498 
13499 		arvif->bitrate_mask = *mask;
13500 		ieee80211_iterate_stations_mtx(hw,
13501 					       ath12k_mac_set_bitrate_mask_iter,
13502 					       arvif);
13503 	}
13504 
13505 	ret = ath12k_mac_set_rate_params(arvif, rate, nss, sgi, ldpc, he_gi,
13506 					 he_ltf, he_fixed_rate, eht_gi, eht_ltf,
13507 					 eht_fixed_rate);
13508 	if (ret) {
13509 		ath12k_warn(ar->ab, "failed to set rate params on vdev %i: %d\n",
13510 			    arvif->vdev_id, ret);
13511 	}
13512 
13513 out:
13514 	return ret;
13515 }
13516 EXPORT_SYMBOL(ath12k_mac_op_set_bitrate_mask);
13517 
13518 void
13519 ath12k_mac_op_reconfig_complete(struct ieee80211_hw *hw,
13520 				enum ieee80211_reconfig_type reconfig_type)
13521 {
13522 	struct ath12k_hw *ah = ath12k_hw_to_ah(hw);
13523 	struct ath12k *ar;
13524 	struct ath12k_base *ab;
13525 	struct ath12k_vif *ahvif;
13526 	struct ath12k_link_vif *arvif;
13527 	int recovery_count, i;
13528 
13529 	lockdep_assert_wiphy(hw->wiphy);
13530 
13531 	if (reconfig_type != IEEE80211_RECONFIG_TYPE_RESTART)
13532 		return;
13533 
13534 	guard(mutex)(&ah->hw_mutex);
13535 
13536 	if (ah->state != ATH12K_HW_STATE_RESTARTED)
13537 		return;
13538 
13539 	ah->state = ATH12K_HW_STATE_ON;
13540 	ieee80211_wake_queues(hw);
13541 
13542 	for_each_ar(ah, ar, i) {
13543 		ab = ar->ab;
13544 
13545 		ath12k_warn(ar->ab, "pdev %d successfully recovered\n",
13546 			    ar->pdev->pdev_id);
13547 
13548 		if (ar->ab->hw_params->current_cc_support &&
13549 		    ar->alpha2[0] != 0 && ar->alpha2[1] != 0) {
13550 			struct wmi_set_current_country_arg arg = {};
13551 
13552 			memcpy(&arg.alpha2, ar->alpha2, 2);
13553 			reinit_completion(&ar->regd_update_completed);
13554 			ath12k_wmi_send_set_current_country_cmd(ar, &arg);
13555 		}
13556 
13557 		if (ab->is_reset) {
13558 			recovery_count = atomic_inc_return(&ab->recovery_count);
13559 
13560 			ath12k_dbg(ab, ATH12K_DBG_BOOT, "recovery count %d\n",
13561 				   recovery_count);
13562 
13563 			/* When there are multiple radios in an SOC,
13564 			 * the recovery has to be done for each radio
13565 			 */
13566 			if (recovery_count == ab->num_radios) {
13567 				atomic_dec(&ab->reset_count);
13568 				complete(&ab->reset_complete);
13569 				ab->is_reset = false;
13570 				atomic_set(&ab->fail_cont_count, 0);
13571 				ath12k_dbg(ab, ATH12K_DBG_BOOT, "reset success\n");
13572 			}
13573 		}
13574 
13575 		list_for_each_entry(arvif, &ar->arvifs, list) {
13576 			ahvif = arvif->ahvif;
13577 			ath12k_dbg(ab, ATH12K_DBG_BOOT,
13578 				   "reconfig cipher %d up %d vdev type %d\n",
13579 				   ahvif->dp_vif.key_cipher,
13580 				   arvif->is_up,
13581 				   ahvif->vdev_type);
13582 
13583 			/* After trigger disconnect, then upper layer will
13584 			 * trigger connect again, then the PN number of
13585 			 * upper layer will be reset to keep up with AP
13586 			 * side, hence PN number mismatch will not happen.
13587 			 */
13588 			if (arvif->is_up &&
13589 			    ahvif->vdev_type == WMI_VDEV_TYPE_STA &&
13590 			    ahvif->vdev_subtype == WMI_VDEV_SUBTYPE_NONE) {
13591 				ieee80211_hw_restart_disconnect(ahvif->vif);
13592 
13593 				ath12k_dbg(ab, ATH12K_DBG_BOOT,
13594 					   "restart disconnect\n");
13595 			}
13596 		}
13597 	}
13598 }
13599 EXPORT_SYMBOL(ath12k_mac_op_reconfig_complete);
13600 
13601 static void
13602 ath12k_mac_update_bss_chan_survey(struct ath12k *ar,
13603 				  struct ieee80211_channel *channel)
13604 {
13605 	int ret;
13606 	enum wmi_bss_chan_info_req_type type = WMI_BSS_SURVEY_REQ_TYPE_READ;
13607 
13608 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
13609 
13610 	if (!test_bit(WMI_TLV_SERVICE_BSS_CHANNEL_INFO_64, ar->ab->wmi_ab.svc_map) ||
13611 	    ar->rx_channel != channel)
13612 		return;
13613 
13614 	if (ar->scan.state != ATH12K_SCAN_IDLE) {
13615 		ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
13616 			   "ignoring bss chan info req while scanning..\n");
13617 		return;
13618 	}
13619 
13620 	reinit_completion(&ar->bss_survey_done);
13621 
13622 	ret = ath12k_wmi_pdev_bss_chan_info_request(ar, type);
13623 	if (ret) {
13624 		ath12k_warn(ar->ab, "failed to send pdev bss chan info request\n");
13625 		return;
13626 	}
13627 
13628 	ret = wait_for_completion_timeout(&ar->bss_survey_done, 3 * HZ);
13629 	if (ret == 0)
13630 		ath12k_warn(ar->ab, "bss channel survey timed out\n");
13631 }
13632 
13633 int ath12k_mac_op_get_survey(struct ieee80211_hw *hw, int idx,
13634 			     struct survey_info *survey)
13635 {
13636 	struct ath12k *ar;
13637 	struct ieee80211_supported_band *sband;
13638 	struct survey_info *ar_survey;
13639 
13640 	lockdep_assert_wiphy(hw->wiphy);
13641 
13642 	if (idx >= ATH12K_NUM_CHANS)
13643 		return -ENOENT;
13644 
13645 	sband = hw->wiphy->bands[NL80211_BAND_2GHZ];
13646 	if (sband && idx >= sband->n_channels) {
13647 		idx -= sband->n_channels;
13648 		sband = NULL;
13649 	}
13650 
13651 	if (!sband)
13652 		sband = hw->wiphy->bands[NL80211_BAND_5GHZ];
13653 	if (sband && idx >= sband->n_channels) {
13654 		idx -= sband->n_channels;
13655 		sband = NULL;
13656 	}
13657 
13658 	if (!sband)
13659 		sband = hw->wiphy->bands[NL80211_BAND_6GHZ];
13660 
13661 	if (!sband || idx >= sband->n_channels)
13662 		return -ENOENT;
13663 
13664 	ar = ath12k_mac_get_ar_by_chan(hw, &sband->channels[idx]);
13665 	if (!ar) {
13666 		if (sband->channels[idx].flags & IEEE80211_CHAN_DISABLED) {
13667 			memset(survey, 0, sizeof(*survey));
13668 			return 0;
13669 		}
13670 		return -ENOENT;
13671 	}
13672 
13673 	ar_survey = &ar->survey[idx];
13674 
13675 	ath12k_mac_update_bss_chan_survey(ar, &sband->channels[idx]);
13676 
13677 	spin_lock_bh(&ar->data_lock);
13678 	memcpy(survey, ar_survey, sizeof(*survey));
13679 	spin_unlock_bh(&ar->data_lock);
13680 
13681 	survey->channel = &sband->channels[idx];
13682 
13683 	if (ar->rx_channel == survey->channel)
13684 		survey->filled |= SURVEY_INFO_IN_USE;
13685 
13686 	return 0;
13687 }
13688 EXPORT_SYMBOL(ath12k_mac_op_get_survey);
13689 
13690 static void ath12k_mac_put_chain_rssi(struct station_info *sinfo,
13691 				      struct ath12k_link_sta *arsta)
13692 {
13693 	s8 rssi;
13694 	int i;
13695 
13696 	for (i = 0; i < ARRAY_SIZE(sinfo->chain_signal); i++) {
13697 		sinfo->chains &= ~BIT(i);
13698 		rssi = arsta->chain_signal[i];
13699 
13700 		if (rssi != ATH12K_DEFAULT_NOISE_FLOOR &&
13701 		    rssi != ATH12K_INVALID_RSSI_FULL &&
13702 		    rssi != ATH12K_INVALID_RSSI_EMPTY &&
13703 		    rssi != 0) {
13704 			sinfo->chain_signal[i] = rssi;
13705 			sinfo->chains |= BIT(i);
13706 			sinfo->filled |= BIT_ULL(NL80211_STA_INFO_CHAIN_SIGNAL);
13707 		}
13708 	}
13709 }
13710 
13711 void ath12k_mac_op_sta_statistics(struct ieee80211_hw *hw,
13712 				  struct ieee80211_vif *vif,
13713 				  struct ieee80211_sta *sta,
13714 				  struct station_info *sinfo)
13715 {
13716 	struct ath12k_sta *ahsta = ath12k_sta_to_ahsta(sta);
13717 	struct ath12k_dp_link_peer_rate_info rate_info = {};
13718 	struct ath12k_fw_stats_req_params params = {};
13719 	struct ath12k_dp_link_peer *peer;
13720 	struct ath12k_link_sta *arsta;
13721 	s8 signal, noise_floor;
13722 	struct ath12k_dp *dp;
13723 	struct ath12k *ar;
13724 	bool db2dbm;
13725 
13726 	lockdep_assert_wiphy(hw->wiphy);
13727 
13728 	arsta = &ahsta->deflink;
13729 	ar = ath12k_get_ar_by_vif(hw, vif, arsta->link_id);
13730 	if (!ar)
13731 		return;
13732 
13733 	dp = ath12k_ab_to_dp(ar->ab);
13734 	ath12k_dp_link_peer_get_sta_rate_info_stats(dp, arsta->addr, &rate_info);
13735 
13736 	db2dbm = test_bit(WMI_TLV_SERVICE_HW_DB2DBM_CONVERSION_SUPPORT,
13737 			  ar->ab->wmi_ab.svc_map);
13738 
13739 	sinfo->rx_duration = rate_info.rx_duration;
13740 	sinfo->filled |= BIT_ULL(NL80211_STA_INFO_RX_DURATION);
13741 
13742 	sinfo->tx_duration = rate_info.tx_duration;
13743 	sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_DURATION);
13744 
13745 	if (rate_info.txrate.legacy || rate_info.txrate.nss) {
13746 		if (rate_info.txrate.legacy) {
13747 			sinfo->txrate.legacy = rate_info.txrate.legacy;
13748 		} else {
13749 			sinfo->txrate.mcs = rate_info.txrate.mcs;
13750 			sinfo->txrate.nss = rate_info.txrate.nss;
13751 			sinfo->txrate.bw = rate_info.txrate.bw;
13752 			sinfo->txrate.he_gi = rate_info.txrate.he_gi;
13753 			sinfo->txrate.he_dcm = rate_info.txrate.he_dcm;
13754 			sinfo->txrate.he_ru_alloc = rate_info.txrate.he_ru_alloc;
13755 			sinfo->txrate.eht_gi = rate_info.txrate.eht_gi;
13756 			sinfo->txrate.eht_ru_alloc = rate_info.txrate.eht_ru_alloc;
13757 		}
13758 		sinfo->txrate.flags = rate_info.txrate.flags;
13759 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_BITRATE);
13760 	}
13761 
13762 	/* TODO: Use real NF instead of default one. */
13763 	signal = rate_info.rssi_comb;
13764 
13765 	params.pdev_id = ath12k_mac_get_target_pdev_id(ar);
13766 	params.vdev_id = 0;
13767 	params.stats_id = WMI_REQUEST_VDEV_STAT;
13768 
13769 	if (!signal &&
13770 	    ahsta->ahvif->vdev_type == WMI_VDEV_TYPE_STA &&
13771 	    !(ath12k_mac_get_fw_stats(ar, &params))) {
13772 		signal = arsta->rssi_beacon;
13773 		ath12k_fw_stats_reset(ar);
13774 	}
13775 
13776 	params.stats_id = WMI_REQUEST_RSSI_PER_CHAIN_STAT;
13777 	if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_CHAIN_SIGNAL)) &&
13778 	    ahsta->ahvif->vdev_type == WMI_VDEV_TYPE_STA &&
13779 	    !(ath12k_mac_get_fw_stats(ar, &params))) {
13780 		ath12k_mac_put_chain_rssi(sinfo, arsta);
13781 		ath12k_fw_stats_reset(ar);
13782 	}
13783 
13784 	spin_lock_bh(&ar->data_lock);
13785 	noise_floor = ath12k_pdev_get_noise_floor(ar);
13786 	spin_unlock_bh(&ar->data_lock);
13787 
13788 	if (signal) {
13789 		sinfo->signal = db2dbm ? signal : signal + noise_floor;
13790 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_SIGNAL);
13791 	}
13792 
13793 	sinfo->signal_avg = rate_info.signal_avg;
13794 
13795 	if (!db2dbm)
13796 		sinfo->signal_avg += noise_floor;
13797 
13798 	sinfo->filled |= BIT_ULL(NL80211_STA_INFO_SIGNAL_AVG);
13799 
13800 	spin_lock_bh(&dp->dp_lock);
13801 	peer = ath12k_dp_link_peer_find_by_addr(dp, arsta->addr);
13802 	if (!peer) {
13803 		spin_unlock_bh(&dp->dp_lock);
13804 		return;
13805 	}
13806 
13807 	sinfo->tx_retries = peer->tx_retry_count;
13808 	sinfo->tx_failed = peer->tx_retry_failed;
13809 	sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_RETRIES);
13810 	sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_FAILED);
13811 
13812 	spin_unlock_bh(&dp->dp_lock);
13813 }
13814 EXPORT_SYMBOL(ath12k_mac_op_sta_statistics);
13815 
13816 void ath12k_mac_op_link_sta_statistics(struct ieee80211_hw *hw,
13817 				       struct ieee80211_vif *vif,
13818 				       struct ieee80211_link_sta *link_sta,
13819 				       struct link_station_info *link_sinfo)
13820 {
13821 	struct ath12k_sta *ahsta = ath12k_sta_to_ahsta(link_sta->sta);
13822 	struct ath12k_fw_stats_req_params params = {};
13823 	struct ath12k_dp_link_peer *peer;
13824 	struct ath12k_link_sta *arsta;
13825 	struct ath12k *ar;
13826 	s8 signal;
13827 	bool db2dbm;
13828 
13829 	lockdep_assert_wiphy(hw->wiphy);
13830 
13831 	arsta = wiphy_dereference(hw->wiphy, ahsta->link[link_sta->link_id]);
13832 
13833 	if (!arsta)
13834 		return;
13835 
13836 	ar = ath12k_get_ar_by_vif(hw, vif, arsta->link_id);
13837 	if (!ar)
13838 		return;
13839 
13840 	db2dbm = test_bit(WMI_TLV_SERVICE_HW_DB2DBM_CONVERSION_SUPPORT,
13841 			  ar->ab->wmi_ab.svc_map);
13842 
13843 	spin_lock_bh(&ar->ab->dp->dp_lock);
13844 	peer = ath12k_dp_link_peer_find_by_addr(ar->ab->dp, arsta->addr);
13845 	if (!peer) {
13846 		spin_unlock_bh(&ar->ab->dp->dp_lock);
13847 		return;
13848 	}
13849 
13850 	link_sinfo->rx_duration = peer->rx_duration;
13851 	link_sinfo->filled |= BIT_ULL(NL80211_STA_INFO_RX_DURATION);
13852 
13853 	link_sinfo->tx_duration = peer->tx_duration;
13854 	link_sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_DURATION);
13855 
13856 	if (peer->txrate.legacy || peer->txrate.nss) {
13857 		if (peer->txrate.legacy) {
13858 			link_sinfo->txrate.legacy = peer->txrate.legacy;
13859 		} else {
13860 			link_sinfo->txrate.mcs = peer->txrate.mcs;
13861 			link_sinfo->txrate.nss = peer->txrate.nss;
13862 			link_sinfo->txrate.bw = peer->txrate.bw;
13863 			link_sinfo->txrate.he_gi = peer->txrate.he_gi;
13864 			link_sinfo->txrate.he_dcm = peer->txrate.he_dcm;
13865 			link_sinfo->txrate.he_ru_alloc =
13866 				peer->txrate.he_ru_alloc;
13867 			link_sinfo->txrate.eht_gi = peer->txrate.eht_gi;
13868 			link_sinfo->txrate.eht_ru_alloc =
13869 				peer->txrate.eht_ru_alloc;
13870 		}
13871 		link_sinfo->txrate.flags = peer->txrate.flags;
13872 		link_sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_BITRATE);
13873 	}
13874 
13875 	link_sinfo->signal_avg = ewma_avg_rssi_read(&peer->avg_rssi);
13876 
13877 	if (!db2dbm)
13878 		link_sinfo->signal_avg += ATH12K_DEFAULT_NOISE_FLOOR;
13879 
13880 	link_sinfo->filled |= BIT_ULL(NL80211_STA_INFO_SIGNAL_AVG);
13881 
13882 	link_sinfo->tx_retries = peer->tx_retry_count;
13883 	link_sinfo->tx_failed = peer->tx_retry_failed;
13884 	link_sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_RETRIES);
13885 	link_sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_FAILED);
13886 
13887 	/* TODO: Use real NF instead of default one. */
13888 	signal = peer->rssi_comb;
13889 
13890 	spin_unlock_bh(&ar->ab->dp->dp_lock);
13891 
13892 	if (!signal && ahsta->ahvif->vdev_type == WMI_VDEV_TYPE_STA) {
13893 		params.pdev_id = ath12k_mac_get_target_pdev_id(ar);
13894 		params.vdev_id = 0;
13895 		params.stats_id = WMI_REQUEST_VDEV_STAT;
13896 
13897 		if (!ath12k_mac_get_fw_stats(ar, &params)) {
13898 			signal = arsta->rssi_beacon;
13899 			ath12k_fw_stats_reset(ar);
13900 		}
13901 	}
13902 
13903 	if (signal) {
13904 		link_sinfo->signal =
13905 			db2dbm ? signal : signal + ATH12K_DEFAULT_NOISE_FLOOR;
13906 		link_sinfo->filled |= BIT_ULL(NL80211_STA_INFO_SIGNAL);
13907 	}
13908 }
13909 EXPORT_SYMBOL(ath12k_mac_op_link_sta_statistics);
13910 
13911 int ath12k_mac_op_cancel_remain_on_channel(struct ieee80211_hw *hw,
13912 					   struct ieee80211_vif *vif)
13913 {
13914 	struct ath12k_hw *ah = ath12k_hw_to_ah(hw);
13915 	struct ath12k *ar;
13916 
13917 	ar = ath12k_ah_to_ar(ah, 0);
13918 
13919 	lockdep_assert_wiphy(hw->wiphy);
13920 
13921 	spin_lock_bh(&ar->data_lock);
13922 	ar->scan.roc_notify = false;
13923 	spin_unlock_bh(&ar->data_lock);
13924 
13925 	ath12k_scan_abort(ar);
13926 
13927 	cancel_delayed_work_sync(&ar->scan.timeout);
13928 	wiphy_work_flush(hw->wiphy, &ar->scan.vdev_clean_wk);
13929 
13930 	return 0;
13931 }
13932 EXPORT_SYMBOL(ath12k_mac_op_cancel_remain_on_channel);
13933 
13934 int ath12k_mac_op_remain_on_channel(struct ieee80211_hw *hw,
13935 				    struct ieee80211_vif *vif,
13936 				    struct ieee80211_channel *chan,
13937 				    int duration,
13938 				    enum ieee80211_roc_type type)
13939 {
13940 	struct ath12k_vif *ahvif = ath12k_vif_to_ahvif(vif);
13941 	struct ath12k_hw *ah = ath12k_hw_to_ah(hw);
13942 	struct ath12k_link_vif *arvif;
13943 	struct ath12k *ar;
13944 	u32 scan_time_msec;
13945 	bool create = true;
13946 	u8 link_id;
13947 	int ret;
13948 
13949 	lockdep_assert_wiphy(hw->wiphy);
13950 
13951 	ar = ath12k_mac_select_scan_device(hw, vif, chan->center_freq);
13952 	if (!ar)
13953 		return -EINVAL;
13954 
13955 	/* check if any of the links of ML VIF is already started on
13956 	 * radio(ar) corresponding to given scan frequency and use it,
13957 	 * if not use deflink(link 0) for scan purpose.
13958 	 */
13959 
13960 	link_id = ath12k_mac_find_link_id_by_ar(ahvif, ar);
13961 	arvif = ath12k_mac_assign_link_vif(ah, vif, link_id);
13962 	/* If the vif is already assigned to a specific vdev of an ar,
13963 	 * check whether its already started, vdev which is started
13964 	 * are not allowed to switch to a new radio.
13965 	 * If the vdev is not started, but was earlier created on a
13966 	 * different ar, delete that vdev and create a new one. We don't
13967 	 * delete at the scan stop as an optimization to avoid redundant
13968 	 * delete-create vdev's for the same ar, in case the request is
13969 	 * always on the same band for the vif
13970 	 */
13971 	if (arvif->is_created) {
13972 		if (WARN_ON(!arvif->ar))
13973 			return -EINVAL;
13974 
13975 		if (ar != arvif->ar && arvif->is_started)
13976 			return -EBUSY;
13977 
13978 		if (ar != arvif->ar) {
13979 			ath12k_mac_remove_link_interface(hw, arvif);
13980 			ath12k_mac_unassign_link_vif(arvif);
13981 		} else {
13982 			create = false;
13983 		}
13984 	}
13985 
13986 	if (create) {
13987 		arvif = ath12k_mac_assign_link_vif(ah, vif, link_id);
13988 
13989 		ret = ath12k_mac_vdev_create(ar, arvif);
13990 		if (ret) {
13991 			ath12k_warn(ar->ab, "unable to create scan vdev for roc: %d\n",
13992 				    ret);
13993 			ath12k_mac_unassign_link_vif(arvif);
13994 			return ret;
13995 		}
13996 	}
13997 
13998 	spin_lock_bh(&ar->data_lock);
13999 
14000 	switch (ar->scan.state) {
14001 	case ATH12K_SCAN_IDLE:
14002 		reinit_completion(&ar->scan.started);
14003 		reinit_completion(&ar->scan.completed);
14004 		reinit_completion(&ar->scan.on_channel);
14005 		ar->scan.state = ATH12K_SCAN_STARTING;
14006 		ar->scan.is_roc = true;
14007 		ar->scan.arvif = arvif;
14008 		ar->scan.roc_freq = chan->center_freq;
14009 		ar->scan.roc_notify = true;
14010 		ret = 0;
14011 		break;
14012 	case ATH12K_SCAN_STARTING:
14013 	case ATH12K_SCAN_RUNNING:
14014 	case ATH12K_SCAN_ABORTING:
14015 		ret = -EBUSY;
14016 		break;
14017 	}
14018 
14019 	spin_unlock_bh(&ar->data_lock);
14020 
14021 	if (ret)
14022 		return ret;
14023 
14024 	scan_time_msec = hw->wiphy->max_remain_on_channel_duration * 2;
14025 
14026 	struct ath12k_wmi_scan_req_arg *arg __free(kfree) =
14027 					kzalloc_flex(*arg, chan_list, 1);
14028 	if (!arg)
14029 		return -ENOMEM;
14030 
14031 	arg->num_chan = 1;
14032 	ath12k_wmi_start_scan_init(ar, arg);
14033 
14034 	arg->vdev_id = arvif->vdev_id;
14035 	arg->scan_id = ATH12K_SCAN_ID;
14036 	arg->chan_list[0] = chan->center_freq;
14037 	arg->dwell_time_active = scan_time_msec;
14038 	arg->dwell_time_passive = scan_time_msec;
14039 	arg->max_scan_time = scan_time_msec;
14040 	arg->scan_f_passive = 1;
14041 	arg->burst_duration = duration;
14042 
14043 	ret = ath12k_start_scan(ar, arg);
14044 	if (ret) {
14045 		ath12k_warn(ar->ab, "failed to start roc scan: %d\n", ret);
14046 
14047 		spin_lock_bh(&ar->data_lock);
14048 		ar->scan.state = ATH12K_SCAN_IDLE;
14049 		spin_unlock_bh(&ar->data_lock);
14050 		return ret;
14051 	}
14052 
14053 	ret = wait_for_completion_timeout(&ar->scan.on_channel, 3 * HZ);
14054 	if (ret == 0) {
14055 		ath12k_warn(ar->ab, "failed to switch to channel for roc scan\n");
14056 		ret = ath12k_scan_stop(ar);
14057 		if (ret)
14058 			ath12k_warn(ar->ab, "failed to stop scan: %d\n", ret);
14059 		return -ETIMEDOUT;
14060 	}
14061 
14062 	ieee80211_queue_delayed_work(hw, &ar->scan.timeout,
14063 				     msecs_to_jiffies(duration));
14064 
14065 	return 0;
14066 }
14067 EXPORT_SYMBOL(ath12k_mac_op_remain_on_channel);
14068 
14069 void ath12k_mac_op_set_rekey_data(struct ieee80211_hw *hw,
14070 				  struct ieee80211_vif *vif,
14071 				  struct cfg80211_gtk_rekey_data *data)
14072 {
14073 	struct ath12k_vif *ahvif = ath12k_vif_to_ahvif(vif);
14074 	struct ath12k_rekey_data *rekey_data;
14075 	struct ath12k_hw *ah = ath12k_hw_to_ah(hw);
14076 	struct ath12k *ar = ath12k_ah_to_ar(ah, 0);
14077 	struct ath12k_link_vif *arvif;
14078 
14079 	lockdep_assert_wiphy(hw->wiphy);
14080 
14081 	arvif = &ahvif->deflink;
14082 	rekey_data = &arvif->rekey_data;
14083 
14084 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac set rekey data vdev %d\n",
14085 		   arvif->vdev_id);
14086 
14087 	memcpy(rekey_data->kck, data->kck, NL80211_KCK_LEN);
14088 	memcpy(rekey_data->kek, data->kek, NL80211_KEK_LEN);
14089 
14090 	/* The supplicant works on big-endian, the firmware expects it on
14091 	 * little endian.
14092 	 */
14093 	rekey_data->replay_ctr = get_unaligned_be64(data->replay_ctr);
14094 
14095 	arvif->rekey_data.enable_offload = true;
14096 
14097 	ath12k_dbg_dump(ar->ab, ATH12K_DBG_MAC, "kck", NULL,
14098 			rekey_data->kck, NL80211_KCK_LEN);
14099 	ath12k_dbg_dump(ar->ab, ATH12K_DBG_MAC, "kek", NULL,
14100 			rekey_data->kck, NL80211_KEK_LEN);
14101 	ath12k_dbg_dump(ar->ab, ATH12K_DBG_MAC, "replay ctr", NULL,
14102 			&rekey_data->replay_ctr, sizeof(rekey_data->replay_ctr));
14103 }
14104 EXPORT_SYMBOL(ath12k_mac_op_set_rekey_data);
14105 
14106 void ath12k_mac_update_freq_range(struct ath12k *ar,
14107 				  u32 freq_low, u32 freq_high)
14108 {
14109 	if (!(freq_low && freq_high))
14110 		return;
14111 
14112 	if (ar->freq_range.start_freq || ar->freq_range.end_freq) {
14113 		ar->freq_range.start_freq = min(ar->freq_range.start_freq,
14114 						MHZ_TO_KHZ(freq_low));
14115 		ar->freq_range.end_freq = max(ar->freq_range.end_freq,
14116 					      MHZ_TO_KHZ(freq_high));
14117 	} else {
14118 		ar->freq_range.start_freq = MHZ_TO_KHZ(freq_low);
14119 		ar->freq_range.end_freq = MHZ_TO_KHZ(freq_high);
14120 	}
14121 
14122 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
14123 		   "mac pdev %u freq limit updated. New range %u->%u MHz\n",
14124 		   ar->pdev->pdev_id, KHZ_TO_MHZ(ar->freq_range.start_freq),
14125 		   KHZ_TO_MHZ(ar->freq_range.end_freq));
14126 }
14127 
14128 static void ath12k_mac_update_ch_list(struct ath12k *ar,
14129 				      struct ieee80211_supported_band *band,
14130 				      u32 freq_low, u32 freq_high)
14131 {
14132 	int i;
14133 
14134 	if (!(freq_low && freq_high))
14135 		return;
14136 
14137 	for (i = 0; i < band->n_channels; i++) {
14138 		if (band->channels[i].center_freq < freq_low ||
14139 		    band->channels[i].center_freq > freq_high)
14140 			band->channels[i].flags |= IEEE80211_CHAN_DISABLED;
14141 	}
14142 }
14143 
14144 static u32 ath12k_get_phy_id(struct ath12k *ar, u32 band)
14145 {
14146 	struct ath12k_pdev *pdev = ar->pdev;
14147 	struct ath12k_pdev_cap *pdev_cap = &pdev->cap;
14148 
14149 	if (band == WMI_HOST_WLAN_2GHZ_CAP)
14150 		return pdev_cap->band[NL80211_BAND_2GHZ].phy_id;
14151 
14152 	if (band == WMI_HOST_WLAN_5GHZ_CAP)
14153 		return pdev_cap->band[NL80211_BAND_5GHZ].phy_id;
14154 
14155 	ath12k_warn(ar->ab, "unsupported phy cap:%d\n", band);
14156 
14157 	return 0;
14158 }
14159 
14160 static int ath12k_mac_update_band(struct ath12k *ar,
14161 				  struct ieee80211_supported_band *orig_band,
14162 				  struct ieee80211_supported_band *new_band)
14163 {
14164 	int i;
14165 
14166 	if (!orig_band || !new_band)
14167 		return -EINVAL;
14168 
14169 	if (orig_band->band != new_band->band)
14170 		return -EINVAL;
14171 
14172 	for (i = 0; i < new_band->n_channels; i++) {
14173 		if (new_band->channels[i].flags & IEEE80211_CHAN_DISABLED)
14174 			continue;
14175 		/* An enabled channel in new_band should not be already enabled
14176 		 * in the orig_band
14177 		 */
14178 		if (WARN_ON(!(orig_band->channels[i].flags &
14179 			      IEEE80211_CHAN_DISABLED)))
14180 			return -EINVAL;
14181 		orig_band->channels[i].flags &= ~IEEE80211_CHAN_DISABLED;
14182 	}
14183 	return 0;
14184 }
14185 
14186 static void ath12k_mac_update_5_9_ghz_ch_list(struct ath12k *ar,
14187 					      struct ieee80211_supported_band *band)
14188 {
14189 	int i;
14190 
14191 	if (test_bit(WMI_TLV_SERVICE_5_9GHZ_SUPPORT,
14192 		     ar->ab->wmi_ab.svc_map))
14193 		return;
14194 
14195 	guard(spinlock_bh)(&ar->ab->base_lock);
14196 	if (ar->ab->dfs_region != ATH12K_DFS_REG_FCC)
14197 		return;
14198 
14199 	for (i = 0; i < band->n_channels; i++) {
14200 		if (band->channels[i].center_freq >= ATH12K_5_9_GHZ_MIN_FREQ &&
14201 		    band->channels[i].center_freq <= ATH12K_5_9_GHZ_MAX_FREQ)
14202 			band->channels[i].flags |= IEEE80211_CHAN_DISABLED;
14203 	}
14204 }
14205 
14206 static int ath12k_mac_setup_channels_rates(struct ath12k *ar,
14207 					   u32 supported_bands,
14208 					   struct ieee80211_supported_band *bands[])
14209 {
14210 	struct ieee80211_supported_band *band;
14211 	struct ath12k_wmi_hal_reg_capabilities_ext_arg *reg_cap;
14212 	struct ath12k_base *ab = ar->ab;
14213 	u32 phy_id, freq_low, freq_high;
14214 	struct ath12k_hw *ah = ar->ah;
14215 	void *channels;
14216 	int ret;
14217 
14218 	BUILD_BUG_ON((ARRAY_SIZE(ath12k_2ghz_channels) +
14219 		      ARRAY_SIZE(ath12k_5ghz_channels) +
14220 		      ARRAY_SIZE(ath12k_6ghz_channels)) !=
14221 		     ATH12K_NUM_CHANS);
14222 
14223 	reg_cap = &ab->hal_reg_cap[ar->pdev_idx];
14224 
14225 	if (supported_bands & WMI_HOST_WLAN_2GHZ_CAP) {
14226 		channels = kmemdup(ath12k_2ghz_channels,
14227 				   sizeof(ath12k_2ghz_channels),
14228 				   GFP_KERNEL);
14229 		if (!channels)
14230 			return -ENOMEM;
14231 
14232 		band = &ar->mac.sbands[NL80211_BAND_2GHZ];
14233 		band->band = NL80211_BAND_2GHZ;
14234 		band->n_channels = ARRAY_SIZE(ath12k_2ghz_channels);
14235 		band->channels = channels;
14236 		band->n_bitrates = ath12k_g_rates_size;
14237 		band->bitrates = ath12k_g_rates;
14238 
14239 		if (ab->hw_params->single_pdev_only) {
14240 			phy_id = ath12k_get_phy_id(ar, WMI_HOST_WLAN_2GHZ_CAP);
14241 			reg_cap = &ab->hal_reg_cap[phy_id];
14242 		}
14243 
14244 		freq_low = max(reg_cap->low_2ghz_chan,
14245 			       ab->reg_freq_2ghz.start_freq);
14246 		freq_high = min(reg_cap->high_2ghz_chan,
14247 				ab->reg_freq_2ghz.end_freq);
14248 
14249 		ath12k_mac_update_ch_list(ar, band,
14250 					  reg_cap->low_2ghz_chan,
14251 					  reg_cap->high_2ghz_chan);
14252 
14253 		ath12k_mac_update_freq_range(ar, freq_low, freq_high);
14254 
14255 		if (!bands[NL80211_BAND_2GHZ]) {
14256 			bands[NL80211_BAND_2GHZ] = band;
14257 		} else {
14258 			/* Split mac in same band under same wiphy */
14259 			ret = ath12k_mac_update_band(ar, bands[NL80211_BAND_2GHZ], band);
14260 			if (ret) {
14261 				kfree(channels);
14262 				band->channels = NULL;
14263 				return ret;
14264 			}
14265 			ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac pdev %u identified as 2 GHz split mac with start freq %d end freq %d",
14266 				   ar->pdev->pdev_id,
14267 				   KHZ_TO_MHZ(ar->freq_range.start_freq),
14268 				   KHZ_TO_MHZ(ar->freq_range.end_freq));
14269 		}
14270 	}
14271 
14272 	if (supported_bands & WMI_HOST_WLAN_5GHZ_CAP) {
14273 		if (reg_cap->high_5ghz_chan >= ATH12K_MIN_6GHZ_FREQ) {
14274 			channels = kmemdup(ath12k_6ghz_channels,
14275 					   sizeof(ath12k_6ghz_channels), GFP_KERNEL);
14276 			if (!channels) {
14277 				kfree(ar->mac.sbands[NL80211_BAND_2GHZ].channels);
14278 				return -ENOMEM;
14279 			}
14280 
14281 			ar->supports_6ghz = true;
14282 			band = &ar->mac.sbands[NL80211_BAND_6GHZ];
14283 			band->band = NL80211_BAND_6GHZ;
14284 			band->n_channels = ARRAY_SIZE(ath12k_6ghz_channels);
14285 			band->channels = channels;
14286 			band->n_bitrates = ath12k_a_rates_size;
14287 			band->bitrates = ath12k_a_rates;
14288 
14289 			freq_low = max(reg_cap->low_5ghz_chan,
14290 				       ab->reg_freq_6ghz.start_freq);
14291 			freq_high = min(reg_cap->high_5ghz_chan,
14292 					ab->reg_freq_6ghz.end_freq);
14293 
14294 			ath12k_mac_update_ch_list(ar, band,
14295 						  reg_cap->low_5ghz_chan,
14296 						  reg_cap->high_5ghz_chan);
14297 
14298 			ath12k_mac_update_freq_range(ar, freq_low, freq_high);
14299 			ah->use_6ghz_regd = true;
14300 
14301 			if (!bands[NL80211_BAND_6GHZ]) {
14302 				bands[NL80211_BAND_6GHZ] = band;
14303 			} else {
14304 				/* Split mac in same band under same wiphy */
14305 				ret = ath12k_mac_update_band(ar,
14306 							     bands[NL80211_BAND_6GHZ],
14307 							     band);
14308 				if (ret) {
14309 					kfree(ar->mac.sbands[NL80211_BAND_2GHZ].channels);
14310 					ar->mac.sbands[NL80211_BAND_2GHZ].channels = NULL;
14311 					kfree(channels);
14312 					band->channels = NULL;
14313 					return ret;
14314 				}
14315 				ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac pdev %u identified as 6 GHz split mac with start freq %d end freq %d",
14316 					   ar->pdev->pdev_id,
14317 					   KHZ_TO_MHZ(ar->freq_range.start_freq),
14318 					   KHZ_TO_MHZ(ar->freq_range.end_freq));
14319 			}
14320 		}
14321 
14322 		if (reg_cap->low_5ghz_chan < ATH12K_MIN_6GHZ_FREQ) {
14323 			channels = kmemdup(ath12k_5ghz_channels,
14324 					   sizeof(ath12k_5ghz_channels),
14325 					   GFP_KERNEL);
14326 			if (!channels) {
14327 				kfree(ar->mac.sbands[NL80211_BAND_2GHZ].channels);
14328 				kfree(ar->mac.sbands[NL80211_BAND_6GHZ].channels);
14329 				return -ENOMEM;
14330 			}
14331 
14332 			band = &ar->mac.sbands[NL80211_BAND_5GHZ];
14333 			band->band = NL80211_BAND_5GHZ;
14334 			band->n_channels = ARRAY_SIZE(ath12k_5ghz_channels);
14335 			band->channels = channels;
14336 			band->n_bitrates = ath12k_a_rates_size;
14337 			band->bitrates = ath12k_a_rates;
14338 
14339 			ath12k_mac_update_5_9_ghz_ch_list(ar, band);
14340 
14341 			if (ab->hw_params->single_pdev_only) {
14342 				phy_id = ath12k_get_phy_id(ar, WMI_HOST_WLAN_5GHZ_CAP);
14343 				reg_cap = &ab->hal_reg_cap[phy_id];
14344 			}
14345 
14346 			freq_low = max(reg_cap->low_5ghz_chan,
14347 				       ab->reg_freq_5ghz.start_freq);
14348 			freq_high = min(reg_cap->high_5ghz_chan,
14349 					ab->reg_freq_5ghz.end_freq);
14350 
14351 			ath12k_mac_update_ch_list(ar, band,
14352 						  reg_cap->low_5ghz_chan,
14353 						  reg_cap->high_5ghz_chan);
14354 
14355 			ath12k_mac_update_freq_range(ar, freq_low, freq_high);
14356 
14357 			if (!bands[NL80211_BAND_5GHZ]) {
14358 				bands[NL80211_BAND_5GHZ] = band;
14359 			} else {
14360 				/* Split mac in same band under same wiphy */
14361 				ret = ath12k_mac_update_band(ar,
14362 							     bands[NL80211_BAND_5GHZ],
14363 							     band);
14364 				if (ret) {
14365 					kfree(ar->mac.sbands[NL80211_BAND_2GHZ].channels);
14366 					ar->mac.sbands[NL80211_BAND_2GHZ].channels = NULL;
14367 					kfree(ar->mac.sbands[NL80211_BAND_6GHZ].channels);
14368 					ar->mac.sbands[NL80211_BAND_2GHZ].channels = NULL;
14369 					kfree(channels);
14370 					band->channels = NULL;
14371 					return ret;
14372 				}
14373 				ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac pdev %u identified as 5 GHz split mac with start freq %d end freq %d",
14374 					   ar->pdev->pdev_id,
14375 					   KHZ_TO_MHZ(ar->freq_range.start_freq),
14376 					   KHZ_TO_MHZ(ar->freq_range.end_freq));
14377 			}
14378 		}
14379 	}
14380 
14381 	return 0;
14382 }
14383 
14384 static u16 ath12k_mac_get_ifmodes(struct ath12k_hw *ah)
14385 {
14386 	struct ath12k *ar;
14387 	int i;
14388 	u16 interface_modes = U16_MAX;
14389 
14390 	for_each_ar(ah, ar, i)
14391 		interface_modes &= ar->ab->hw_params->interface_modes;
14392 
14393 	return interface_modes == U16_MAX ? 0 : interface_modes;
14394 }
14395 
14396 static bool ath12k_mac_is_iface_mode_enable(struct ath12k_hw *ah,
14397 					    enum nl80211_iftype type)
14398 {
14399 	struct ath12k *ar;
14400 	int i;
14401 	u16 interface_modes, mode = 0;
14402 	bool is_enable = false;
14403 
14404 	if (type == NL80211_IFTYPE_MESH_POINT) {
14405 		if (IS_ENABLED(CONFIG_MAC80211_MESH))
14406 			mode = BIT(type);
14407 	} else {
14408 		mode = BIT(type);
14409 	}
14410 
14411 	for_each_ar(ah, ar, i) {
14412 		interface_modes = ar->ab->hw_params->interface_modes;
14413 		if (interface_modes & mode) {
14414 			is_enable = true;
14415 			break;
14416 		}
14417 	}
14418 
14419 	return is_enable;
14420 }
14421 
14422 static int
14423 ath12k_mac_setup_radio_iface_comb(struct ath12k *ar,
14424 				  struct ieee80211_iface_combination *comb)
14425 {
14426 	u16 interface_modes = ar->ab->hw_params->interface_modes;
14427 	struct ieee80211_iface_limit *limits;
14428 	int n_limits, max_interfaces;
14429 	bool ap, mesh, p2p;
14430 
14431 	ap = interface_modes & BIT(NL80211_IFTYPE_AP);
14432 	p2p = interface_modes & BIT(NL80211_IFTYPE_P2P_DEVICE);
14433 
14434 	mesh = IS_ENABLED(CONFIG_MAC80211_MESH) &&
14435 	       (interface_modes & BIT(NL80211_IFTYPE_MESH_POINT));
14436 
14437 	if ((ap || mesh) && !p2p) {
14438 		n_limits = 2;
14439 		max_interfaces = 16;
14440 	} else if (p2p) {
14441 		n_limits = 3;
14442 		if (ap || mesh)
14443 			max_interfaces = 16;
14444 		else
14445 			max_interfaces = 3;
14446 	} else {
14447 		n_limits = 1;
14448 		max_interfaces = 1;
14449 	}
14450 
14451 	limits = kzalloc_objs(*limits, n_limits);
14452 	if (!limits)
14453 		return -ENOMEM;
14454 
14455 	limits[0].max = 1;
14456 	limits[0].types |= BIT(NL80211_IFTYPE_STATION);
14457 
14458 	if (ap || mesh || p2p)
14459 		limits[1].max = max_interfaces;
14460 
14461 	if (ap)
14462 		limits[1].types |= BIT(NL80211_IFTYPE_AP);
14463 
14464 	if (mesh)
14465 		limits[1].types |= BIT(NL80211_IFTYPE_MESH_POINT);
14466 
14467 	if (p2p) {
14468 		limits[1].types |= BIT(NL80211_IFTYPE_P2P_CLIENT) |
14469 					BIT(NL80211_IFTYPE_P2P_GO);
14470 		limits[2].max = 1;
14471 		limits[2].types |= BIT(NL80211_IFTYPE_P2P_DEVICE);
14472 	}
14473 
14474 	comb[0].limits = limits;
14475 	comb[0].n_limits = n_limits;
14476 	comb[0].max_interfaces = max_interfaces;
14477 	comb[0].beacon_int_infra_match = true;
14478 	comb[0].beacon_int_min_gcd = 100;
14479 
14480 	comb[0].num_different_channels = 1;
14481 	comb[0].radar_detect_widths = BIT(NL80211_CHAN_WIDTH_20_NOHT) |
14482 				      BIT(NL80211_CHAN_WIDTH_20) |
14483 				      BIT(NL80211_CHAN_WIDTH_40) |
14484 				      BIT(NL80211_CHAN_WIDTH_80) |
14485 				      BIT(NL80211_CHAN_WIDTH_160);
14486 
14487 	return 0;
14488 }
14489 
14490 static int
14491 ath12k_mac_setup_global_iface_comb(struct ath12k_hw *ah,
14492 				   struct wiphy_radio *radio,
14493 				   u8 n_radio,
14494 				   struct ieee80211_iface_combination *comb)
14495 {
14496 	const struct ieee80211_iface_combination *iter_comb;
14497 	struct ieee80211_iface_limit *limits;
14498 	int i, j, n_limits;
14499 	bool ap, mesh, p2p;
14500 
14501 	if (!n_radio)
14502 		return 0;
14503 
14504 	ap = ath12k_mac_is_iface_mode_enable(ah, NL80211_IFTYPE_AP);
14505 	p2p = ath12k_mac_is_iface_mode_enable(ah, NL80211_IFTYPE_P2P_DEVICE);
14506 	mesh = ath12k_mac_is_iface_mode_enable(ah, NL80211_IFTYPE_MESH_POINT);
14507 
14508 	if ((ap || mesh) && !p2p)
14509 		n_limits = 2;
14510 	else if (p2p)
14511 		n_limits = 3;
14512 	else
14513 		n_limits = 1;
14514 
14515 	limits = kzalloc_objs(*limits, n_limits);
14516 	if (!limits)
14517 		return -ENOMEM;
14518 
14519 	for (i = 0; i < n_radio; i++) {
14520 		iter_comb = radio[i].iface_combinations;
14521 		for (j = 0; j < iter_comb->n_limits && j < n_limits; j++) {
14522 			limits[j].types |= iter_comb->limits[j].types;
14523 			limits[j].max += iter_comb->limits[j].max;
14524 		}
14525 
14526 		comb->max_interfaces += iter_comb->max_interfaces;
14527 		comb->num_different_channels += iter_comb->num_different_channels;
14528 		comb->radar_detect_widths |= iter_comb->radar_detect_widths;
14529 	}
14530 
14531 	comb->limits = limits;
14532 	comb->n_limits = n_limits;
14533 	comb->beacon_int_infra_match = true;
14534 	comb->beacon_int_min_gcd = 100;
14535 
14536 	return 0;
14537 }
14538 
14539 static
14540 void ath12k_mac_cleanup_iface_comb(const struct ieee80211_iface_combination *iface_comb)
14541 {
14542 	kfree(iface_comb[0].limits);
14543 	kfree(iface_comb);
14544 }
14545 
14546 static void ath12k_mac_cleanup_iface_combinations(struct ath12k_hw *ah)
14547 {
14548 	struct wiphy *wiphy = ah->hw->wiphy;
14549 	const struct wiphy_radio *radio;
14550 	int i;
14551 
14552 	if (wiphy->n_radio > 0) {
14553 		radio = wiphy->radio;
14554 		for (i = 0; i < wiphy->n_radio; i++)
14555 			ath12k_mac_cleanup_iface_comb(radio[i].iface_combinations);
14556 
14557 		kfree(wiphy->radio);
14558 	}
14559 
14560 	ath12k_mac_cleanup_iface_comb(wiphy->iface_combinations);
14561 }
14562 
14563 static int ath12k_mac_setup_iface_combinations(struct ath12k_hw *ah)
14564 {
14565 	struct ieee80211_iface_combination *combinations, *comb;
14566 	struct wiphy *wiphy = ah->hw->wiphy;
14567 	struct wiphy_radio *radio;
14568 	int n_combinations = 1;
14569 	struct ath12k *ar;
14570 	int i, ret;
14571 
14572 	if (ah->num_radio == 1) {
14573 		ar = &ah->radio[0];
14574 
14575 		if (ar->ab->hw_params->single_pdev_only)
14576 			n_combinations = 2;
14577 
14578 		combinations = kzalloc_objs(*combinations, n_combinations);
14579 		if (!combinations)
14580 			return -ENOMEM;
14581 
14582 		ret = ath12k_mac_setup_radio_iface_comb(ar, combinations);
14583 		if (ret) {
14584 			ath12k_hw_warn(ah, "failed to setup radio interface combinations for one radio: %d",
14585 				       ret);
14586 			goto err_free_combinations;
14587 		}
14588 
14589 		if (ar->ab->hw_params->single_pdev_only) {
14590 			comb = combinations + 1;
14591 			memcpy(comb, combinations, sizeof(*comb));
14592 			comb->num_different_channels = 2;
14593 			comb->radar_detect_widths = 0;
14594 		}
14595 
14596 		goto out;
14597 	}
14598 
14599 	combinations = kzalloc_objs(*combinations, n_combinations);
14600 	if (!combinations)
14601 		return -ENOMEM;
14602 
14603 	/* there are multiple radios */
14604 
14605 	radio = kzalloc_objs(*radio, ah->num_radio);
14606 	if (!radio) {
14607 		ret = -ENOMEM;
14608 		goto err_free_combinations;
14609 	}
14610 
14611 	for_each_ar(ah, ar, i) {
14612 		comb = kzalloc_obj(*comb);
14613 		if (!comb) {
14614 			ret = -ENOMEM;
14615 			goto err_free_radios;
14616 		}
14617 
14618 		ret = ath12k_mac_setup_radio_iface_comb(ar, comb);
14619 		if (ret) {
14620 			ath12k_hw_warn(ah, "failed to setup radio interface combinations for radio %d: %d",
14621 				       i, ret);
14622 			kfree(comb);
14623 			goto err_free_radios;
14624 		}
14625 
14626 		radio[i].freq_range = &ar->freq_range;
14627 		radio[i].n_freq_range = 1;
14628 
14629 		radio[i].iface_combinations = comb;
14630 		radio[i].n_iface_combinations = 1;
14631 	}
14632 
14633 	ret = ath12k_mac_setup_global_iface_comb(ah, radio, ah->num_radio, combinations);
14634 	if (ret) {
14635 		ath12k_hw_warn(ah, "failed to setup global interface combinations: %d",
14636 			       ret);
14637 		goto err_free_all_radios;
14638 	}
14639 
14640 	wiphy->radio = radio;
14641 	wiphy->n_radio = ah->num_radio;
14642 
14643 out:
14644 	wiphy->iface_combinations = combinations;
14645 	wiphy->n_iface_combinations = n_combinations;
14646 
14647 	return 0;
14648 
14649 err_free_all_radios:
14650 	i = ah->num_radio;
14651 
14652 err_free_radios:
14653 	while (i--)
14654 		ath12k_mac_cleanup_iface_comb(radio[i].iface_combinations);
14655 
14656 	kfree(radio);
14657 
14658 err_free_combinations:
14659 	kfree(combinations);
14660 
14661 	return ret;
14662 }
14663 
14664 static const u8 ath12k_if_types_ext_capa[] = {
14665 	[0] = WLAN_EXT_CAPA1_EXT_CHANNEL_SWITCHING,
14666 	[2] = WLAN_EXT_CAPA3_MULTI_BSSID_SUPPORT,
14667 	[7] = WLAN_EXT_CAPA8_OPMODE_NOTIF,
14668 };
14669 
14670 static const u8 ath12k_if_types_ext_capa_sta[] = {
14671 	[0] = WLAN_EXT_CAPA1_EXT_CHANNEL_SWITCHING,
14672 	[2] = WLAN_EXT_CAPA3_MULTI_BSSID_SUPPORT,
14673 	[7] = WLAN_EXT_CAPA8_OPMODE_NOTIF,
14674 	[9] = WLAN_EXT_CAPA10_TWT_REQUESTER_SUPPORT,
14675 };
14676 
14677 static const u8 ath12k_if_types_ext_capa_ap[] = {
14678 	[0] = WLAN_EXT_CAPA1_EXT_CHANNEL_SWITCHING,
14679 	[2] = WLAN_EXT_CAPA3_MULTI_BSSID_SUPPORT,
14680 	[7] = WLAN_EXT_CAPA8_OPMODE_NOTIF,
14681 	[9] = WLAN_EXT_CAPA10_TWT_RESPONDER_SUPPORT,
14682 	[10] = WLAN_EXT_CAPA11_EMA_SUPPORT,
14683 };
14684 
14685 static struct wiphy_iftype_ext_capab ath12k_iftypes_ext_capa[] = {
14686 	{
14687 		.extended_capabilities = ath12k_if_types_ext_capa,
14688 		.extended_capabilities_mask = ath12k_if_types_ext_capa,
14689 		.extended_capabilities_len = sizeof(ath12k_if_types_ext_capa),
14690 	}, {
14691 		.iftype = NL80211_IFTYPE_STATION,
14692 		.extended_capabilities = ath12k_if_types_ext_capa_sta,
14693 		.extended_capabilities_mask = ath12k_if_types_ext_capa_sta,
14694 		.extended_capabilities_len =
14695 				sizeof(ath12k_if_types_ext_capa_sta),
14696 	}, {
14697 		.iftype = NL80211_IFTYPE_AP,
14698 		.extended_capabilities = ath12k_if_types_ext_capa_ap,
14699 		.extended_capabilities_mask = ath12k_if_types_ext_capa_ap,
14700 		.extended_capabilities_len =
14701 				sizeof(ath12k_if_types_ext_capa_ap),
14702 		.eml_capabilities = 0,
14703 		.mld_capa_and_ops = 0,
14704 	},
14705 };
14706 
14707 static void ath12k_mac_cleanup_unregister(struct ath12k *ar)
14708 {
14709 	idr_for_each(&ar->txmgmt_idr, ath12k_mac_tx_mgmt_pending_free, ar);
14710 	idr_destroy(&ar->txmgmt_idr);
14711 
14712 	kfree(ar->mac.sbands[NL80211_BAND_2GHZ].channels);
14713 	kfree(ar->mac.sbands[NL80211_BAND_5GHZ].channels);
14714 	kfree(ar->mac.sbands[NL80211_BAND_6GHZ].channels);
14715 }
14716 
14717 static void ath12k_mac_hw_unregister(struct ath12k_hw *ah)
14718 {
14719 	struct ieee80211_hw *hw = ah->hw;
14720 	struct ath12k *ar;
14721 	int i;
14722 
14723 	for_each_ar(ah, ar, i) {
14724 		cancel_work_sync(&ar->regd_channel_update_work);
14725 		cancel_work_sync(&ar->regd_update_work);
14726 		ath12k_debugfs_unregister(ar);
14727 		ath12k_fw_stats_reset(ar);
14728 	}
14729 
14730 	ieee80211_unregister_hw(hw);
14731 
14732 	for_each_ar(ah, ar, i)
14733 		ath12k_mac_cleanup_unregister(ar);
14734 
14735 	ath12k_mac_cleanup_iface_combinations(ah);
14736 
14737 	SET_IEEE80211_DEV(hw, NULL);
14738 }
14739 
14740 static int ath12k_mac_setup_register(struct ath12k *ar,
14741 				     u32 *ht_cap,
14742 				     struct ieee80211_supported_band *bands[])
14743 {
14744 	struct ath12k_pdev_cap *cap = &ar->pdev->cap;
14745 	int ret;
14746 
14747 	init_waitqueue_head(&ar->txmgmt_empty_waitq);
14748 	idr_init(&ar->txmgmt_idr);
14749 	spin_lock_init(&ar->txmgmt_idr_lock);
14750 
14751 	ath12k_pdev_caps_update(ar);
14752 
14753 	ret = ath12k_mac_setup_channels_rates(ar,
14754 					      cap->supported_bands,
14755 					      bands);
14756 	if (ret)
14757 		return ret;
14758 
14759 	ath12k_mac_setup_ht_vht_cap(ar, cap, ht_cap);
14760 	ath12k_mac_setup_sband_iftype_data(ar, cap);
14761 
14762 	ar->max_num_stations = ath12k_core_get_max_station_per_radio(ar->ab);
14763 	ar->max_num_peers = ath12k_core_get_max_peers_per_radio(ar->ab);
14764 
14765 	ar->rssi_info.min_nf_dbm = ATH12K_DEFAULT_NOISE_FLOOR;
14766 	ar->rssi_info.temp_offset = 0;
14767 	ar->rssi_info.noise_floor = ar->rssi_info.min_nf_dbm + ar->rssi_info.temp_offset;
14768 
14769 	ath12k_thermal_init_configs(ar);
14770 
14771 	return 0;
14772 }
14773 
14774 static int ath12k_mac_hw_register(struct ath12k_hw *ah)
14775 {
14776 	struct ieee80211_hw *hw = ah->hw;
14777 	struct wiphy *wiphy = hw->wiphy;
14778 	struct ath12k *ar = ath12k_ah_to_ar(ah, 0);
14779 	struct ath12k_base *ab = ar->ab;
14780 	struct ath12k_pdev *pdev;
14781 	struct ath12k_pdev_cap *cap;
14782 	static const u32 cipher_suites[] = {
14783 		WLAN_CIPHER_SUITE_TKIP,
14784 		WLAN_CIPHER_SUITE_CCMP,
14785 		WLAN_CIPHER_SUITE_AES_CMAC,
14786 		WLAN_CIPHER_SUITE_BIP_CMAC_256,
14787 		WLAN_CIPHER_SUITE_BIP_GMAC_128,
14788 		WLAN_CIPHER_SUITE_BIP_GMAC_256,
14789 		WLAN_CIPHER_SUITE_GCMP,
14790 		WLAN_CIPHER_SUITE_GCMP_256,
14791 		WLAN_CIPHER_SUITE_CCMP_256,
14792 	};
14793 	int ret, i, j;
14794 	u32 ht_cap = U32_MAX, antennas_rx = 0, antennas_tx = 0;
14795 	bool is_6ghz = false, is_raw_mode = false, is_monitor_disable = false;
14796 	u8 *mac_addr = NULL;
14797 	u8 mbssid_max_interfaces = 0;
14798 
14799 	wiphy->max_ap_assoc_sta = 0;
14800 
14801 	for_each_ar(ah, ar, i) {
14802 		u32 ht_cap_info = 0;
14803 
14804 		pdev = ar->pdev;
14805 		if (ar->ab->pdevs_macaddr_valid) {
14806 			ether_addr_copy(ar->mac_addr, pdev->mac_addr);
14807 		} else {
14808 			ether_addr_copy(ar->mac_addr, ar->ab->mac_addr);
14809 			ar->mac_addr[4] += ar->pdev_idx;
14810 		}
14811 
14812 		ret = ath12k_mac_setup_register(ar, &ht_cap_info, hw->wiphy->bands);
14813 		if (ret)
14814 			goto err_cleanup_unregister;
14815 
14816 		/* 6 GHz does not support HT Cap, hence do not consider it */
14817 		if (!ar->supports_6ghz)
14818 			ht_cap &= ht_cap_info;
14819 
14820 		wiphy->max_ap_assoc_sta += ar->max_num_stations;
14821 
14822 		/* Advertise the max antenna support of all radios, driver can handle
14823 		 * per pdev specific antenna setting based on pdev cap when antenna
14824 		 * changes are made
14825 		 */
14826 		cap = &pdev->cap;
14827 
14828 		antennas_rx = max_t(u32, antennas_rx, cap->rx_chain_mask);
14829 		antennas_tx = max_t(u32, antennas_tx, cap->tx_chain_mask);
14830 
14831 		if (ar->supports_6ghz)
14832 			is_6ghz = true;
14833 
14834 		if (test_bit(ATH12K_FLAG_RAW_MODE, &ar->ab->dev_flags))
14835 			is_raw_mode = true;
14836 
14837 		if (!ar->ab->hw_params->supports_monitor)
14838 			is_monitor_disable = true;
14839 
14840 		if (i == 0)
14841 			mac_addr = ar->mac_addr;
14842 		else
14843 			mac_addr = ab->mac_addr;
14844 
14845 		mbssid_max_interfaces += TARGET_NUM_VDEVS(ar->ab);
14846 	}
14847 
14848 	wiphy->available_antennas_rx = antennas_rx;
14849 	wiphy->available_antennas_tx = antennas_tx;
14850 
14851 	SET_IEEE80211_PERM_ADDR(hw, mac_addr);
14852 	SET_IEEE80211_DEV(hw, ab->dev);
14853 
14854 	ret = ath12k_mac_setup_iface_combinations(ah);
14855 	if (ret) {
14856 		ath12k_err(ab, "failed to setup interface combinations: %d\n", ret);
14857 		goto err_complete_cleanup_unregister;
14858 	}
14859 
14860 	wiphy->interface_modes = ath12k_mac_get_ifmodes(ah);
14861 
14862 	if (ah->num_radio == 1 &&
14863 	    wiphy->bands[NL80211_BAND_2GHZ] &&
14864 	    wiphy->bands[NL80211_BAND_5GHZ] &&
14865 	    wiphy->bands[NL80211_BAND_6GHZ])
14866 		ieee80211_hw_set(hw, SINGLE_SCAN_ON_ALL_BANDS);
14867 
14868 	ieee80211_hw_set(hw, SIGNAL_DBM);
14869 	ieee80211_hw_set(hw, SUPPORTS_PS);
14870 	ieee80211_hw_set(hw, SUPPORTS_DYNAMIC_PS);
14871 	ieee80211_hw_set(hw, MFP_CAPABLE);
14872 	ieee80211_hw_set(hw, REPORTS_TX_ACK_STATUS);
14873 	ieee80211_hw_set(hw, HAS_RATE_CONTROL);
14874 	ieee80211_hw_set(hw, AP_LINK_PS);
14875 	ieee80211_hw_set(hw, SPECTRUM_MGMT);
14876 	ieee80211_hw_set(hw, CONNECTION_MONITOR);
14877 	ieee80211_hw_set(hw, SUPPORTS_PER_STA_GTK);
14878 	ieee80211_hw_set(hw, CHANCTX_STA_CSA);
14879 	ieee80211_hw_set(hw, QUEUE_CONTROL);
14880 	ieee80211_hw_set(hw, SUPPORTS_TX_FRAG);
14881 	ieee80211_hw_set(hw, REPORTS_LOW_ACK);
14882 	ieee80211_hw_set(hw, NO_VIRTUAL_MONITOR);
14883 
14884 	if (test_bit(WMI_TLV_SERVICE_ETH_OFFLOAD, ar->wmi->wmi_ab->svc_map)) {
14885 		ieee80211_hw_set(hw, SUPPORTS_TX_ENCAP_OFFLOAD);
14886 		ieee80211_hw_set(hw, SUPPORTS_RX_DECAP_OFFLOAD);
14887 	}
14888 
14889 	if (cap->nss_ratio_enabled)
14890 		ieee80211_hw_set(hw, SUPPORTS_VHT_EXT_NSS_BW);
14891 
14892 	if ((ht_cap & WMI_HT_CAP_ENABLED) || is_6ghz) {
14893 		ieee80211_hw_set(hw, AMPDU_AGGREGATION);
14894 		ieee80211_hw_set(hw, TX_AMPDU_SETUP_IN_HW);
14895 		ieee80211_hw_set(hw, SUPPORTS_REORDERING_BUFFER);
14896 		ieee80211_hw_set(hw, SUPPORTS_AMSDU_IN_AMPDU);
14897 		ieee80211_hw_set(hw, USES_RSS);
14898 	}
14899 
14900 	wiphy->features |= NL80211_FEATURE_STATIC_SMPS;
14901 	wiphy->flags |= WIPHY_FLAG_IBSS_RSN;
14902 
14903 	/* TODO: Check if HT capability advertised from firmware is different
14904 	 * for each band for a dual band capable radio. It will be tricky to
14905 	 * handle it when the ht capability different for each band.
14906 	 */
14907 	if (ht_cap & WMI_HT_CAP_DYNAMIC_SMPS ||
14908 	    (is_6ghz && ab->hw_params->supports_dynamic_smps_6ghz))
14909 		wiphy->features |= NL80211_FEATURE_DYNAMIC_SMPS;
14910 
14911 	wiphy->max_scan_ssids = WLAN_SCAN_PARAMS_MAX_SSID;
14912 	wiphy->max_scan_ie_len = WLAN_SCAN_PARAMS_MAX_IE_LEN;
14913 
14914 	hw->max_listen_interval = ATH12K_MAX_HW_LISTEN_INTERVAL;
14915 
14916 	wiphy->flags |= WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL;
14917 	wiphy->flags |= WIPHY_FLAG_HAS_CHANNEL_SWITCH;
14918 	wiphy->max_remain_on_channel_duration = 5000;
14919 
14920 	wiphy->flags |= WIPHY_FLAG_AP_UAPSD;
14921 	wiphy->features |= NL80211_FEATURE_AP_MODE_CHAN_WIDTH_CHANGE |
14922 				   NL80211_FEATURE_AP_SCAN;
14923 
14924 	wiphy->features |= NL80211_FEATURE_TX_POWER_INSERTION;
14925 
14926 	/* MLO is not yet supported so disable Wireless Extensions for now
14927 	 * to make sure ath12k users don't use it. This flag can be removed
14928 	 * once WIPHY_FLAG_SUPPORTS_MLO is enabled.
14929 	 */
14930 	wiphy->flags |= WIPHY_FLAG_DISABLE_WEXT;
14931 
14932 	/* Copy over MLO related capabilities received from
14933 	 * WMI_SERVICE_READY_EXT2_EVENT if single_chip_mlo_supp is set.
14934 	 */
14935 	if (ab->ag->mlo_capable) {
14936 		ath12k_iftypes_ext_capa[2].eml_capabilities = cap->eml_cap;
14937 		ath12k_iftypes_ext_capa[2].mld_capa_and_ops = cap->mld_cap;
14938 		wiphy->flags |= WIPHY_FLAG_SUPPORTS_MLO;
14939 
14940 		ieee80211_hw_set(hw, MLO_MCAST_MULTI_LINK_TX);
14941 	}
14942 
14943 	hw->queues = ATH12K_HW_MAX_QUEUES;
14944 	wiphy->tx_queue_len = ATH12K_QUEUE_LEN;
14945 	hw->offchannel_tx_hw_queue = ATH12K_HW_MAX_QUEUES - 1;
14946 	hw->max_rx_aggregation_subframes = IEEE80211_MAX_AMPDU_BUF_EHT;
14947 
14948 	hw->vif_data_size = sizeof(struct ath12k_vif);
14949 	hw->sta_data_size = sizeof(struct ath12k_sta);
14950 	hw->extra_tx_headroom = ab->hw_params->iova_mask;
14951 
14952 	wiphy_ext_feature_set(wiphy, NL80211_EXT_FEATURE_CQM_RSSI_LIST);
14953 	wiphy_ext_feature_set(wiphy, NL80211_EXT_FEATURE_STA_TX_PWR);
14954 	wiphy_ext_feature_set(wiphy, NL80211_EXT_FEATURE_ACK_SIGNAL_SUPPORT);
14955 	if (test_bit(WMI_TLV_SERVICE_BSS_COLOR_OFFLOAD,
14956 		     ab->wmi_ab.svc_map)) {
14957 		wiphy_ext_feature_set(wiphy, NL80211_EXT_FEATURE_BSS_COLOR);
14958 		ieee80211_hw_set(hw, DETECTS_COLOR_COLLISION);
14959 	}
14960 
14961 	wiphy->cipher_suites = cipher_suites;
14962 	wiphy->n_cipher_suites = ARRAY_SIZE(cipher_suites);
14963 
14964 	wiphy->iftype_ext_capab = ath12k_iftypes_ext_capa;
14965 	wiphy->num_iftype_ext_capab = ARRAY_SIZE(ath12k_iftypes_ext_capa);
14966 
14967 	wiphy->mbssid_max_interfaces = mbssid_max_interfaces;
14968 	wiphy->ema_max_profile_periodicity = TARGET_EMA_MAX_PROFILE_PERIOD;
14969 	ieee80211_hw_set(hw, SUPPORTS_MULTI_BSSID);
14970 
14971 	if (is_6ghz) {
14972 		wiphy_ext_feature_set(wiphy,
14973 				      NL80211_EXT_FEATURE_FILS_DISCOVERY);
14974 		wiphy_ext_feature_set(wiphy,
14975 				      NL80211_EXT_FEATURE_UNSOL_BCAST_PROBE_RESP);
14976 	}
14977 
14978 	wiphy_ext_feature_set(wiphy, NL80211_EXT_FEATURE_PUNCT);
14979 	if (test_bit(WMI_TLV_SERVICE_BEACON_PROTECTION_SUPPORT, ab->wmi_ab.svc_map))
14980 		wiphy_ext_feature_set(hw->wiphy, NL80211_EXT_FEATURE_BEACON_PROTECTION);
14981 
14982 	ath12k_reg_init(hw);
14983 
14984 	if (!is_raw_mode) {
14985 		hw->netdev_features = NETIF_F_HW_CSUM;
14986 		ieee80211_hw_set(hw, SW_CRYPTO_CONTROL);
14987 		ieee80211_hw_set(hw, SUPPORT_FAST_XMIT);
14988 	}
14989 
14990 	if (test_bit(WMI_TLV_SERVICE_NLO, ar->wmi->wmi_ab->svc_map)) {
14991 		wiphy->max_sched_scan_ssids = WMI_PNO_MAX_SUPP_NETWORKS;
14992 		wiphy->max_match_sets = WMI_PNO_MAX_SUPP_NETWORKS;
14993 		wiphy->max_sched_scan_ie_len = WMI_PNO_MAX_IE_LENGTH;
14994 		wiphy->max_sched_scan_plans = WMI_PNO_MAX_SCHED_SCAN_PLANS;
14995 		wiphy->max_sched_scan_plan_interval =
14996 					WMI_PNO_MAX_SCHED_SCAN_PLAN_INT;
14997 		wiphy->max_sched_scan_plan_iterations =
14998 					WMI_PNO_MAX_SCHED_SCAN_PLAN_ITRNS;
14999 		wiphy->features |= NL80211_FEATURE_ND_RANDOM_MAC_ADDR;
15000 	}
15001 
15002 	ret = ath12k_wow_init(ar);
15003 	if (ret) {
15004 		ath12k_warn(ar->ab, "failed to init wow: %d\n", ret);
15005 		goto err_cleanup_if_combs;
15006 	}
15007 
15008 	/* Boot-time regulatory updates have already been processed.
15009 	 * Mark them as complete now, because after registration,
15010 	 * cfg80211 will notify us again if there are any pending hints.
15011 	 * We need to wait for those hints to be processed, so it's
15012 	 * important to mark the boot-time updates as complete before
15013 	 * proceeding with registration.
15014 	 */
15015 	for_each_ar(ah, ar, i)
15016 		complete_all(&ar->regd_update_completed);
15017 
15018 	ret = ieee80211_register_hw(hw);
15019 	if (ret) {
15020 		ath12k_err(ab, "ieee80211 registration failed: %d\n", ret);
15021 		goto err_cleanup_if_combs;
15022 	}
15023 
15024 	if (is_monitor_disable)
15025 		/* There's a race between calling ieee80211_register_hw()
15026 		 * and here where the monitor mode is enabled for a little
15027 		 * while. But that time is so short and in practice it doesn't make
15028 		 * a difference in real life.
15029 		 */
15030 		wiphy->interface_modes &= ~BIT(NL80211_IFTYPE_MONITOR);
15031 
15032 	for_each_ar(ah, ar, i) {
15033 		/* Apply the regd received during initialization */
15034 		ret = ath12k_regd_update(ar, true);
15035 		if (ret) {
15036 			ath12k_err(ar->ab, "ath12k regd update failed: %d\n", ret);
15037 			goto err_unregister_hw;
15038 		}
15039 
15040 		if (ar->ab->hw_params->current_cc_support && ab->new_alpha2[0]) {
15041 			struct wmi_set_current_country_arg current_cc = {};
15042 
15043 			memcpy(&current_cc.alpha2, ab->new_alpha2, 2);
15044 			memcpy(&ar->alpha2, ab->new_alpha2, 2);
15045 
15046 			reinit_completion(&ar->regd_update_completed);
15047 
15048 			ret = ath12k_wmi_send_set_current_country_cmd(ar, &current_cc);
15049 			if (ret)
15050 				ath12k_warn(ar->ab,
15051 					    "failed set cc code for mac register: %d\n",
15052 					    ret);
15053 		}
15054 
15055 		ath12k_fw_stats_init(ar);
15056 		ath12k_debugfs_register(ar);
15057 	}
15058 
15059 	return 0;
15060 
15061 err_unregister_hw:
15062 	for_each_ar(ah, ar, i)
15063 		ath12k_debugfs_unregister(ar);
15064 
15065 	ieee80211_unregister_hw(hw);
15066 
15067 err_cleanup_if_combs:
15068 	ath12k_mac_cleanup_iface_combinations(ah);
15069 
15070 err_complete_cleanup_unregister:
15071 	i = ah->num_radio;
15072 
15073 err_cleanup_unregister:
15074 	for (j = 0; j < i; j++) {
15075 		ar = ath12k_ah_to_ar(ah, j);
15076 		ath12k_mac_cleanup_unregister(ar);
15077 	}
15078 
15079 	SET_IEEE80211_DEV(hw, NULL);
15080 
15081 	return ret;
15082 }
15083 
15084 static void ath12k_mac_setup(struct ath12k *ar)
15085 {
15086 	struct ath12k_base *ab = ar->ab;
15087 	struct ath12k_pdev *pdev = ar->pdev;
15088 	u8 pdev_idx = ar->pdev_idx;
15089 
15090 	ar->lmac_id = ath12k_hw_get_mac_from_pdev_id(ab->hw_params, pdev_idx);
15091 
15092 	ar->wmi = &ab->wmi_ab.wmi[pdev_idx];
15093 	/* FIXME: wmi[0] is already initialized during attach,
15094 	 * Should we do this again?
15095 	 */
15096 	ath12k_wmi_pdev_attach(ab, pdev_idx);
15097 
15098 	ar->cfg_tx_chainmask = pdev->cap.tx_chain_mask;
15099 	ar->cfg_rx_chainmask = pdev->cap.rx_chain_mask;
15100 	ar->num_tx_chains = hweight32(pdev->cap.tx_chain_mask);
15101 	ar->num_rx_chains = hweight32(pdev->cap.rx_chain_mask);
15102 	ar->scan.arvif = NULL;
15103 	ar->vdev_id_11d_scan = ATH12K_11D_INVALID_VDEV_ID;
15104 
15105 	spin_lock_init(&ar->data_lock);
15106 	spin_lock_init(&ar->dp.ppdu_list_lock);
15107 	INIT_LIST_HEAD(&ar->arvifs);
15108 	INIT_LIST_HEAD(&ar->dp.ppdu_stats_info);
15109 
15110 	init_completion(&ar->vdev_setup_done);
15111 	init_completion(&ar->vdev_delete_done);
15112 	init_completion(&ar->peer_assoc_done);
15113 	init_completion(&ar->peer_delete_done);
15114 	init_completion(&ar->install_key_done);
15115 	init_completion(&ar->bss_survey_done);
15116 	init_completion(&ar->scan.started);
15117 	init_completion(&ar->scan.completed);
15118 	init_completion(&ar->scan.on_channel);
15119 	init_completion(&ar->mlo_setup_done);
15120 	init_completion(&ar->completed_11d_scan);
15121 	init_completion(&ar->regd_update_completed);
15122 	init_completion(&ar->thermal.wmi_sync);
15123 	mutex_init(&ar->thermal.lock);
15124 
15125 	ar->thermal.temperature = 0;
15126 	ar->thermal.hwmon_dev = NULL;
15127 
15128 	INIT_DELAYED_WORK(&ar->scan.timeout, ath12k_scan_timeout_work);
15129 	wiphy_work_init(&ar->scan.vdev_clean_wk, ath12k_scan_vdev_clean_work);
15130 	INIT_WORK(&ar->regd_channel_update_work, ath12k_regd_update_chan_list_work);
15131 	INIT_LIST_HEAD(&ar->regd_channel_update_queue);
15132 	INIT_WORK(&ar->regd_update_work, ath12k_regd_update_work);
15133 
15134 	wiphy_work_init(&ar->wmi_mgmt_tx_work, ath12k_mgmt_over_wmi_tx_work);
15135 	skb_queue_head_init(&ar->wmi_mgmt_tx_queue);
15136 
15137 	ar->monitor_vdev_id = -1;
15138 	ar->monitor_vdev_created = false;
15139 	ar->monitor_started = false;
15140 }
15141 
15142 static int __ath12k_mac_mlo_setup(struct ath12k *ar)
15143 {
15144 	u8 num_link = 0, partner_link_id[ATH12K_GROUP_MAX_RADIO] = {};
15145 	struct ath12k_base *partner_ab, *ab = ar->ab;
15146 	struct ath12k_hw_group *ag = ab->ag;
15147 	struct wmi_mlo_setup_arg mlo = {};
15148 	struct ath12k_pdev *pdev;
15149 	unsigned long time_left;
15150 	int i, j, ret;
15151 
15152 	lockdep_assert_held(&ag->mutex);
15153 
15154 	reinit_completion(&ar->mlo_setup_done);
15155 
15156 	for (i = 0; i < ag->num_devices; i++) {
15157 		partner_ab = ag->ab[i];
15158 
15159 		for (j = 0; j < partner_ab->num_radios; j++) {
15160 			pdev = &partner_ab->pdevs[j];
15161 
15162 			/* Avoid the self link */
15163 			if (ar == pdev->ar)
15164 				continue;
15165 
15166 			partner_link_id[num_link] = pdev->hw_link_id;
15167 			num_link++;
15168 
15169 			ath12k_dbg(ab, ATH12K_DBG_MAC, "device %d pdev %d hw_link_id %d num_link %d\n",
15170 				   i, j, pdev->hw_link_id, num_link);
15171 		}
15172 	}
15173 
15174 	if (num_link == 0)
15175 		return 0;
15176 
15177 	mlo.group_id = cpu_to_le32(ag->id);
15178 	mlo.partner_link_id = partner_link_id;
15179 	mlo.num_partner_links = num_link;
15180 	ar->mlo_setup_status = 0;
15181 
15182 	ath12k_dbg(ab, ATH12K_DBG_MAC, "group id %d num_link %d\n", ag->id, num_link);
15183 
15184 	ret = ath12k_wmi_mlo_setup(ar, &mlo);
15185 	if (ret) {
15186 		ath12k_err(ab, "failed to send  setup MLO WMI command for pdev %d: %d\n",
15187 			   ar->pdev_idx, ret);
15188 		return ret;
15189 	}
15190 
15191 	time_left = wait_for_completion_timeout(&ar->mlo_setup_done,
15192 						WMI_MLO_CMD_TIMEOUT_HZ);
15193 
15194 	if (!time_left || ar->mlo_setup_status)
15195 		return ar->mlo_setup_status ? : -ETIMEDOUT;
15196 
15197 	ath12k_dbg(ab, ATH12K_DBG_MAC, "mlo setup done for pdev %d\n", ar->pdev_idx);
15198 
15199 	return 0;
15200 }
15201 
15202 static int __ath12k_mac_mlo_teardown(struct ath12k *ar)
15203 {
15204 	struct ath12k_base *ab = ar->ab;
15205 	int ret;
15206 	u8 num_link;
15207 
15208 	if (test_bit(ATH12K_FLAG_RECOVERY, &ab->dev_flags))
15209 		return 0;
15210 
15211 	num_link = ath12k_get_num_partner_link(ar);
15212 
15213 	if (num_link == 0)
15214 		return 0;
15215 
15216 	ret = ath12k_wmi_mlo_teardown(ar);
15217 	if (ret) {
15218 		ath12k_warn(ab, "failed to send MLO teardown WMI command for pdev %d: %d\n",
15219 			    ar->pdev_idx, ret);
15220 		return ret;
15221 	}
15222 
15223 	ath12k_dbg(ab, ATH12K_DBG_MAC, "mlo teardown for pdev %d\n", ar->pdev_idx);
15224 
15225 	return 0;
15226 }
15227 
15228 int ath12k_mac_mlo_setup(struct ath12k_hw_group *ag)
15229 {
15230 	struct ath12k_hw *ah;
15231 	struct ath12k *ar;
15232 	int ret;
15233 	int i, j;
15234 
15235 	for (i = 0; i < ag->num_hw; i++) {
15236 		ah = ag->ah[i];
15237 		if (!ah)
15238 			continue;
15239 
15240 		for_each_ar(ah, ar, j) {
15241 			ar = &ah->radio[j];
15242 			ret = __ath12k_mac_mlo_setup(ar);
15243 			if (ret) {
15244 				ath12k_err(ar->ab, "failed to setup MLO: %d\n", ret);
15245 				goto err_setup;
15246 			}
15247 		}
15248 	}
15249 
15250 	return 0;
15251 
15252 err_setup:
15253 	for (i = i - 1; i >= 0; i--) {
15254 		ah = ag->ah[i];
15255 		if (!ah)
15256 			continue;
15257 
15258 		for (j = j - 1; j >= 0; j--) {
15259 			ar = &ah->radio[j];
15260 			if (!ar)
15261 				continue;
15262 
15263 			__ath12k_mac_mlo_teardown(ar);
15264 		}
15265 	}
15266 
15267 	return ret;
15268 }
15269 
15270 void ath12k_mac_mlo_teardown(struct ath12k_hw_group *ag)
15271 {
15272 	struct ath12k_hw *ah;
15273 	struct ath12k *ar;
15274 	int ret, i, j;
15275 
15276 	for (i = 0; i < ag->num_hw; i++) {
15277 		ah = ag->ah[i];
15278 		if (!ah)
15279 			continue;
15280 
15281 		for_each_ar(ah, ar, j) {
15282 			ar = &ah->radio[j];
15283 			ret = __ath12k_mac_mlo_teardown(ar);
15284 			if (ret) {
15285 				ath12k_err(ar->ab, "failed to teardown MLO: %d\n", ret);
15286 				break;
15287 			}
15288 		}
15289 	}
15290 }
15291 
15292 int ath12k_mac_register(struct ath12k_hw_group *ag)
15293 {
15294 	struct ath12k_hw *ah;
15295 	int i;
15296 	int ret;
15297 
15298 	for (i = 0; i < ag->num_hw; i++) {
15299 		ah = ath12k_ag_to_ah(ag, i);
15300 
15301 		ret = ath12k_mac_hw_register(ah);
15302 		if (ret)
15303 			goto err;
15304 	}
15305 
15306 	return 0;
15307 
15308 err:
15309 	for (i = i - 1; i >= 0; i--) {
15310 		ah = ath12k_ag_to_ah(ag, i);
15311 		if (!ah)
15312 			continue;
15313 
15314 		ath12k_mac_hw_unregister(ah);
15315 	}
15316 
15317 	return ret;
15318 }
15319 
15320 void ath12k_mac_unregister(struct ath12k_hw_group *ag)
15321 {
15322 	struct ath12k_hw *ah;
15323 	int i;
15324 
15325 	for (i = ag->num_hw - 1; i >= 0; i--) {
15326 		ah = ath12k_ag_to_ah(ag, i);
15327 		if (!ah)
15328 			continue;
15329 
15330 		ath12k_mac_hw_unregister(ah);
15331 	}
15332 }
15333 
15334 static void ath12k_mac_hw_destroy(struct ath12k_hw *ah)
15335 {
15336 	ieee80211_free_hw(ah->hw);
15337 }
15338 
15339 static struct ath12k_hw *ath12k_mac_hw_allocate(struct ath12k_hw_group *ag,
15340 						struct ath12k_pdev_map *pdev_map,
15341 						u8 num_pdev_map)
15342 {
15343 	struct ieee80211_hw *hw;
15344 	struct ath12k *ar;
15345 	struct ath12k_base *ab;
15346 	struct ath12k_pdev *pdev;
15347 	struct ath12k_hw *ah;
15348 	int i;
15349 	u8 pdev_idx;
15350 
15351 	hw = ieee80211_alloc_hw(struct_size(ah, radio, num_pdev_map),
15352 				pdev_map->ab->ath12k_ops);
15353 	if (!hw)
15354 		return NULL;
15355 
15356 	ah = ath12k_hw_to_ah(hw);
15357 	ah->hw = hw;
15358 	ah->num_radio = num_pdev_map;
15359 
15360 	mutex_init(&ah->hw_mutex);
15361 	init_completion(&ah->peer_ml_id_done);
15362 
15363 	spin_lock_init(&ah->dp_hw.peer_lock);
15364 	INIT_LIST_HEAD(&ah->dp_hw.dp_peers_list);
15365 
15366 	for (i = 0; i < num_pdev_map; i++) {
15367 		ab = pdev_map[i].ab;
15368 		pdev_idx = pdev_map[i].pdev_idx;
15369 		pdev = &ab->pdevs[pdev_idx];
15370 
15371 		ar = ath12k_ah_to_ar(ah, i);
15372 		ar->ah = ah;
15373 		ar->ab = ab;
15374 		ar->hw_link_id = pdev->hw_link_id;
15375 		ar->pdev = pdev;
15376 		ar->pdev_idx = pdev_idx;
15377 		ar->radio_idx = i;
15378 		pdev->ar = ar;
15379 
15380 		ag->hw_links[ar->hw_link_id].device_id = ab->device_id;
15381 		ag->hw_links[ar->hw_link_id].pdev_idx = pdev_idx;
15382 
15383 		ath12k_mac_setup(ar);
15384 		ath12k_dp_pdev_pre_alloc(ar);
15385 	}
15386 
15387 	return ah;
15388 }
15389 
15390 void ath12k_mac_destroy(struct ath12k_hw_group *ag)
15391 {
15392 	struct ath12k_pdev *pdev;
15393 	struct ath12k_base *ab = ag->ab[0];
15394 	int i, j;
15395 	struct ath12k_hw *ah;
15396 
15397 	for (i = 0; i < ag->num_devices; i++) {
15398 		ab = ag->ab[i];
15399 		if (!ab)
15400 			continue;
15401 
15402 		for (j = 0; j < ab->num_radios; j++) {
15403 			pdev = &ab->pdevs[j];
15404 			if (!pdev->ar)
15405 				continue;
15406 			pdev->ar = NULL;
15407 		}
15408 	}
15409 
15410 	for (i = 0; i < ag->num_hw; i++) {
15411 		ah = ath12k_ag_to_ah(ag, i);
15412 		if (!ah)
15413 			continue;
15414 
15415 		ath12k_mac_hw_destroy(ah);
15416 		ath12k_ag_set_ah(ag, i, NULL);
15417 	}
15418 }
15419 
15420 static void ath12k_mac_set_device_defaults(struct ath12k_base *ab)
15421 {
15422 	int total_vdev;
15423 
15424 	/* Initialize channel counters frequency value in hertz */
15425 	ab->cc_freq_hz = 320000;
15426 	total_vdev = ab->num_radios * TARGET_NUM_VDEVS(ab);
15427 	ab->free_vdev_map = (1LL << total_vdev) - 1;
15428 }
15429 
15430 int ath12k_mac_allocate(struct ath12k_hw_group *ag)
15431 {
15432 	struct ath12k_pdev_map pdev_map[ATH12K_GROUP_MAX_RADIO];
15433 	int mac_id, device_id, total_radio, num_hw;
15434 	struct ath12k_base *ab;
15435 	struct ath12k_hw *ah;
15436 	bool conf = false;
15437 	u8 radio_per_hw;
15438 	int ret, i, j;
15439 
15440 	total_radio = 0;
15441 	for (i = 0; i < ag->num_devices; i++) {
15442 		ab = ag->ab[i];
15443 		if (!ab)
15444 			continue;
15445 
15446 		ath12k_mac_set_device_defaults(ab);
15447 		total_radio += ab->num_radios;
15448 	}
15449 
15450 	if (!total_radio)
15451 		return -EINVAL;
15452 
15453 	if (WARN_ON(total_radio > ATH12K_GROUP_MAX_RADIO))
15454 		return -ENOSPC;
15455 
15456 	/* All pdev get combined and register as single wiphy based on
15457 	 * hardware group which participate in multi-link operation else
15458 	 * each pdev get register separately.
15459 	 */
15460 	if (ag->mlo_capable)
15461 		radio_per_hw = total_radio;
15462 	else
15463 		radio_per_hw = 1;
15464 
15465 	num_hw = total_radio / radio_per_hw;
15466 
15467 	ag->num_hw = 0;
15468 	device_id = 0;
15469 	mac_id = 0;
15470 	for (i = 0; i < num_hw; i++) {
15471 		for (j = 0; j < radio_per_hw; j++) {
15472 			if (device_id >= ag->num_devices || !ag->ab[device_id]) {
15473 				ret = -ENOSPC;
15474 				goto err;
15475 			}
15476 
15477 			ab = ag->ab[device_id];
15478 
15479 			/*
15480 			 * the assumption is all devices within an ah
15481 			 * share the same host_alloc_ml_id configuration
15482 			 */
15483 			if (j == 0) {
15484 				conf = ab->hw_params->host_alloc_ml_id;
15485 			} else if (conf != ab->hw_params->host_alloc_ml_id) {
15486 				ath12k_warn(ab, "inconsistent ML ID config within ah, device 0 uses %s allocated ID, while device %u doesn't\n",
15487 					    conf ? "host" : "firmware", device_id);
15488 				ret = -EINVAL;
15489 				goto err;
15490 			}
15491 
15492 			pdev_map[j].ab = ab;
15493 			pdev_map[j].pdev_idx = mac_id;
15494 			mac_id++;
15495 
15496 			/* If mac_id falls beyond the current device MACs then
15497 			 * move to next device
15498 			 */
15499 			if (mac_id >= ab->num_radios) {
15500 				mac_id = 0;
15501 				device_id++;
15502 			}
15503 		}
15504 
15505 		ab = pdev_map->ab;
15506 
15507 		ah = ath12k_mac_hw_allocate(ag, pdev_map, radio_per_hw);
15508 		if (!ah) {
15509 			ath12k_warn(ab, "failed to allocate mac80211 hw device for hw_idx %d\n",
15510 				    i);
15511 			ret = -ENOMEM;
15512 			goto err;
15513 		}
15514 
15515 		ah->dev = ab->dev;
15516 		ah->host_alloc_ml_id = conf;
15517 
15518 		ag->ah[i] = ah;
15519 		ag->num_hw++;
15520 	}
15521 
15522 	return 0;
15523 
15524 err:
15525 	for (i = i - 1; i >= 0; i--) {
15526 		ah = ath12k_ag_to_ah(ag, i);
15527 		if (!ah)
15528 			continue;
15529 
15530 		ath12k_mac_hw_destroy(ah);
15531 		ath12k_ag_set_ah(ag, i, NULL);
15532 	}
15533 
15534 	return ret;
15535 }
15536 
15537 int ath12k_mac_vif_set_keepalive(struct ath12k_link_vif *arvif,
15538 				 enum wmi_sta_keepalive_method method,
15539 				 u32 interval)
15540 {
15541 	struct wmi_sta_keepalive_arg arg = {};
15542 	struct ath12k *ar = arvif->ar;
15543 	int ret;
15544 
15545 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
15546 
15547 	if (arvif->ahvif->vdev_type != WMI_VDEV_TYPE_STA)
15548 		return 0;
15549 
15550 	if (!test_bit(WMI_TLV_SERVICE_STA_KEEP_ALIVE, ar->ab->wmi_ab.svc_map))
15551 		return 0;
15552 
15553 	arg.vdev_id = arvif->vdev_id;
15554 	arg.enabled = 1;
15555 	arg.method = method;
15556 	arg.interval = interval;
15557 
15558 	ret = ath12k_wmi_sta_keepalive(ar, &arg);
15559 	if (ret) {
15560 		ath12k_warn(ar->ab, "failed to set keepalive on vdev %i: %d\n",
15561 			    arvif->vdev_id, ret);
15562 		return ret;
15563 	}
15564 
15565 	return 0;
15566 }
15567