xref: /linux/drivers/net/wireless/ath/ath12k/mac.c (revision 63abe299b12b317dfee5bcd09037da4668a4431a)
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 		peerid_index = ath12k_dp_peer_get_peerid_index(dp, peer->peer_id);
1242 		rcu_assign_pointer(dp_peer->link_peers[peer->link_id], NULL);
1243 		rcu_assign_pointer(dp_hw->dp_peers[peerid_index], NULL);
1244 		spin_unlock_bh(&dp_hw->peer_lock);
1245 
1246 		ath12k_dp_link_peer_rhash_delete(dp, peer);
1247 
1248 		list_move(&peer->list, &peers);
1249 	}
1250 	spin_unlock_bh(&dp->dp_lock);
1251 
1252 	synchronize_rcu();
1253 
1254 	list_for_each_entry_safe(peer, tmp, &peers, list) {
1255 		ath12k_dp_link_peer_free(peer);
1256 	}
1257 
1258 	ar->num_peers = 0;
1259 	ar->num_stations = 0;
1260 
1261 	/* Cleanup rhash table maintained for arsta by iterating over sta */
1262 	ieee80211_iterate_stations_mtx(ar->ah->hw, ath12k_mac_link_sta_rhash_cleanup,
1263 				       ar);
1264 
1265 	/* Delete all the self dp_peers on asserted radio */
1266 	list_for_each_entry_safe_reverse(arvif, tmp_vif, &ar->arvifs, list) {
1267 		if ((arvif->ahvif->vdev_type == WMI_VDEV_TYPE_AP) &&
1268 		    (arvif->link_id < IEEE80211_MLD_MAX_NUM_LINKS)) {
1269 			ath12k_dp_peer_delete(dp_hw, arvif->bssid, NULL);
1270 			arvif->num_stations = 0;
1271 		}
1272 	}
1273 }
1274 
1275 void ath12k_mac_dp_peer_cleanup(struct ath12k_hw *ah)
1276 {
1277 	struct list_head peers;
1278 	struct ath12k_dp_peer *dp_peer, *tmp;
1279 	struct ath12k_dp_hw *dp_hw = &ah->dp_hw;
1280 
1281 	INIT_LIST_HEAD(&peers);
1282 
1283 	spin_lock_bh(&dp_hw->peer_lock);
1284 	list_for_each_entry_safe(dp_peer, tmp, &dp_hw->dp_peers_list, list) {
1285 		if (dp_peer->is_mlo) {
1286 			rcu_assign_pointer(dp_hw->dp_peers[dp_peer->peer_id], NULL);
1287 			clear_bit(dp_peer->peer_id, ah->free_ml_peer_id_map);
1288 		}
1289 
1290 		list_move(&dp_peer->list, &peers);
1291 	}
1292 
1293 	spin_unlock_bh(&dp_hw->peer_lock);
1294 
1295 	synchronize_rcu();
1296 
1297 	list_for_each_entry_safe(dp_peer, tmp, &peers, list) {
1298 		list_del(&dp_peer->list);
1299 		kfree(dp_peer);
1300 	}
1301 }
1302 
1303 static int ath12k_mac_vdev_setup_sync(struct ath12k *ar)
1304 {
1305 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
1306 
1307 	if (test_bit(ATH12K_FLAG_CRASH_FLUSH, &ar->ab->dev_flags))
1308 		return -ESHUTDOWN;
1309 
1310 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "vdev setup timeout %d\n",
1311 		   ATH12K_VDEV_SETUP_TIMEOUT_HZ);
1312 
1313 	if (!wait_for_completion_timeout(&ar->vdev_setup_done,
1314 					 ATH12K_VDEV_SETUP_TIMEOUT_HZ))
1315 		return -ETIMEDOUT;
1316 
1317 	return ar->last_wmi_vdev_start_status ? -EINVAL : 0;
1318 }
1319 
1320 static int ath12k_monitor_vdev_up(struct ath12k *ar, int vdev_id)
1321 {
1322 	struct ath12k_wmi_vdev_up_params params = {};
1323 	int ret;
1324 
1325 	params.vdev_id = vdev_id;
1326 	params.bssid = ar->mac_addr;
1327 	ret = ath12k_wmi_vdev_up(ar, &params);
1328 	if (ret) {
1329 		ath12k_warn(ar->ab, "failed to put up monitor vdev %i: %d\n",
1330 			    vdev_id, ret);
1331 		return ret;
1332 	}
1333 
1334 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac monitor vdev %i started\n",
1335 		   vdev_id);
1336 	return 0;
1337 }
1338 
1339 static int ath12k_mac_monitor_vdev_start(struct ath12k *ar, int vdev_id,
1340 					 struct cfg80211_chan_def *chandef)
1341 {
1342 	struct ieee80211_channel *channel;
1343 	struct wmi_vdev_start_req_arg arg = {};
1344 	struct ath12k_wmi_vdev_up_params params = {};
1345 	int ret;
1346 
1347 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
1348 
1349 	channel = chandef->chan;
1350 	arg.vdev_id = vdev_id;
1351 	arg.freq = channel->center_freq;
1352 	arg.band_center_freq1 = chandef->center_freq1;
1353 	arg.band_center_freq2 = chandef->center_freq2;
1354 	arg.mode = ath12k_phymodes[chandef->chan->band][chandef->width];
1355 	arg.chan_radar = !!(channel->flags & IEEE80211_CHAN_RADAR);
1356 
1357 	arg.min_power = 0;
1358 	arg.max_power = channel->max_power;
1359 	arg.max_reg_power = channel->max_reg_power;
1360 	arg.max_antenna_gain = channel->max_antenna_gain;
1361 
1362 	arg.pref_tx_streams = ar->num_tx_chains;
1363 	arg.pref_rx_streams = ar->num_rx_chains;
1364 	arg.punct_bitmap = 0xFFFFFFFF;
1365 
1366 	arg.passive |= !!(chandef->chan->flags & IEEE80211_CHAN_NO_IR);
1367 
1368 	reinit_completion(&ar->vdev_setup_done);
1369 	reinit_completion(&ar->vdev_delete_done);
1370 
1371 	ret = ath12k_wmi_vdev_start(ar, &arg, false);
1372 	if (ret) {
1373 		ath12k_warn(ar->ab, "failed to request monitor vdev %i start: %d\n",
1374 			    vdev_id, ret);
1375 		return ret;
1376 	}
1377 
1378 	ret = ath12k_mac_vdev_setup_sync(ar);
1379 	if (ret) {
1380 		ath12k_warn(ar->ab, "failed to synchronize setup for monitor vdev %i start: %d\n",
1381 			    vdev_id, ret);
1382 		return ret;
1383 	}
1384 
1385 	params.vdev_id = vdev_id;
1386 	params.bssid = ar->mac_addr;
1387 	ret = ath12k_wmi_vdev_up(ar, &params);
1388 	if (ret) {
1389 		ath12k_warn(ar->ab, "failed to put up monitor vdev %i: %d\n",
1390 			    vdev_id, ret);
1391 		goto vdev_stop;
1392 	}
1393 
1394 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac monitor vdev %i started\n",
1395 		   vdev_id);
1396 	return 0;
1397 
1398 vdev_stop:
1399 	ret = ath12k_wmi_vdev_stop(ar, vdev_id);
1400 	if (ret)
1401 		ath12k_warn(ar->ab, "failed to stop monitor vdev %i after start failure: %d\n",
1402 			    vdev_id, ret);
1403 	return ret;
1404 }
1405 
1406 static int ath12k_mac_monitor_vdev_stop(struct ath12k *ar)
1407 {
1408 	int ret;
1409 
1410 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
1411 
1412 	reinit_completion(&ar->vdev_setup_done);
1413 
1414 	ret = ath12k_wmi_vdev_stop(ar, ar->monitor_vdev_id);
1415 	if (ret)
1416 		ath12k_warn(ar->ab, "failed to request monitor vdev %i stop: %d\n",
1417 			    ar->monitor_vdev_id, ret);
1418 
1419 	ret = ath12k_mac_vdev_setup_sync(ar);
1420 	if (ret)
1421 		ath12k_warn(ar->ab, "failed to synchronize monitor vdev %i stop: %d\n",
1422 			    ar->monitor_vdev_id, ret);
1423 
1424 	ret = ath12k_wmi_vdev_down(ar, ar->monitor_vdev_id);
1425 	if (ret)
1426 		ath12k_warn(ar->ab, "failed to put down monitor vdev %i: %d\n",
1427 			    ar->monitor_vdev_id, ret);
1428 
1429 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac monitor vdev %i stopped\n",
1430 		   ar->monitor_vdev_id);
1431 	return ret;
1432 }
1433 
1434 static int ath12k_mac_monitor_vdev_delete(struct ath12k *ar)
1435 {
1436 	int ret;
1437 	unsigned long time_left;
1438 
1439 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
1440 
1441 	if (!ar->monitor_vdev_created)
1442 		return 0;
1443 
1444 	reinit_completion(&ar->vdev_delete_done);
1445 
1446 	ret = ath12k_wmi_vdev_delete(ar, ar->monitor_vdev_id);
1447 	if (ret) {
1448 		ath12k_warn(ar->ab, "failed to request wmi monitor vdev %i removal: %d\n",
1449 			    ar->monitor_vdev_id, ret);
1450 		return ret;
1451 	}
1452 
1453 	time_left = wait_for_completion_timeout(&ar->vdev_delete_done,
1454 						ATH12K_VDEV_DELETE_TIMEOUT_HZ);
1455 	if (time_left == 0) {
1456 		ath12k_warn(ar->ab, "Timeout in receiving vdev delete response\n");
1457 	} else {
1458 		ar->allocated_vdev_map &= ~(1LL << ar->monitor_vdev_id);
1459 		ar->ab->free_vdev_map |= 1LL << (ar->monitor_vdev_id);
1460 		ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac monitor vdev %d deleted\n",
1461 			   ar->monitor_vdev_id);
1462 		ar->num_created_vdevs--;
1463 		ar->monitor_vdev_id = -1;
1464 		ar->monitor_vdev_created = false;
1465 	}
1466 
1467 	return ret;
1468 }
1469 
1470 static int ath12k_mac_monitor_start(struct ath12k *ar)
1471 {
1472 	struct ath12k_mac_get_any_chanctx_conf_arg arg;
1473 	int ret;
1474 
1475 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
1476 
1477 	if (ar->monitor_started)
1478 		return 0;
1479 
1480 	arg.ar = ar;
1481 	arg.chanctx_conf = NULL;
1482 	ieee80211_iter_chan_contexts_atomic(ath12k_ar_to_hw(ar),
1483 					    ath12k_mac_get_any_chanctx_conf_iter,
1484 					    &arg);
1485 	if (!arg.chanctx_conf)
1486 		return 0;
1487 
1488 	ret = ath12k_mac_monitor_vdev_start(ar, ar->monitor_vdev_id,
1489 					    &arg.chanctx_conf->def);
1490 	if (ret) {
1491 		ath12k_warn(ar->ab, "failed to start monitor vdev: %d\n", ret);
1492 		return ret;
1493 	}
1494 
1495 	ret = ath12k_dp_tx_htt_monitor_mode_ring_config(ar, false);
1496 	if (ret) {
1497 		ath12k_warn(ar->ab, "fail to set monitor filter: %d\n", ret);
1498 		return ret;
1499 	}
1500 
1501 	ar->monitor_started = true;
1502 	ar->num_started_vdevs++;
1503 
1504 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac monitor started\n");
1505 
1506 	return 0;
1507 }
1508 
1509 static int ath12k_mac_monitor_stop(struct ath12k *ar)
1510 {
1511 	int ret;
1512 
1513 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
1514 
1515 	if (!ar->monitor_started)
1516 		return 0;
1517 
1518 	ret = ath12k_mac_monitor_vdev_stop(ar);
1519 	if (ret) {
1520 		ath12k_warn(ar->ab, "failed to stop monitor vdev: %d\n", ret);
1521 		return ret;
1522 	}
1523 
1524 	ar->monitor_started = false;
1525 	ar->num_started_vdevs--;
1526 	ret = ath12k_dp_tx_htt_monitor_mode_ring_config(ar, true);
1527 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac monitor stopped ret %d\n", ret);
1528 	return ret;
1529 }
1530 
1531 int ath12k_mac_vdev_stop(struct ath12k_link_vif *arvif)
1532 {
1533 	struct ath12k_vif *ahvif = arvif->ahvif;
1534 	struct ath12k *ar = arvif->ar;
1535 	int ret;
1536 
1537 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
1538 
1539 	reinit_completion(&ar->vdev_setup_done);
1540 
1541 	ret = ath12k_wmi_vdev_stop(ar, arvif->vdev_id);
1542 	if (ret) {
1543 		ath12k_warn(ar->ab, "failed to stop WMI vdev %i: %d\n",
1544 			    arvif->vdev_id, ret);
1545 		goto err;
1546 	}
1547 
1548 	ret = ath12k_mac_vdev_setup_sync(ar);
1549 	if (ret) {
1550 		ath12k_warn(ar->ab, "failed to synchronize setup for vdev %i: %d\n",
1551 			    arvif->vdev_id, ret);
1552 		goto err;
1553 	}
1554 
1555 	WARN_ON(ar->num_started_vdevs == 0);
1556 
1557 	ar->num_started_vdevs--;
1558 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "vdev %pM stopped, vdev_id %d\n",
1559 		   ahvif->vif->addr, arvif->vdev_id);
1560 
1561 	if (test_bit(ATH12K_FLAG_CAC_RUNNING, &ar->dev_flags)) {
1562 		clear_bit(ATH12K_FLAG_CAC_RUNNING, &ar->dev_flags);
1563 		ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "CAC Stopped for vdev %d\n",
1564 			   arvif->vdev_id);
1565 	}
1566 
1567 	return 0;
1568 err:
1569 	return ret;
1570 }
1571 
1572 int ath12k_mac_op_config(struct ieee80211_hw *hw, int radio_idx, u32 changed)
1573 {
1574 	return 0;
1575 }
1576 EXPORT_SYMBOL(ath12k_mac_op_config);
1577 
1578 static int ath12k_mac_setup_bcn_p2p_ie(struct ath12k_link_vif *arvif,
1579 				       struct sk_buff *bcn)
1580 {
1581 	struct ath12k *ar = arvif->ar;
1582 	struct ieee80211_mgmt *mgmt;
1583 	const u8 *p2p_ie;
1584 	int ret;
1585 
1586 	mgmt = (void *)bcn->data;
1587 	p2p_ie = cfg80211_find_vendor_ie(WLAN_OUI_WFA, WLAN_OUI_TYPE_WFA_P2P,
1588 					 mgmt->u.beacon.variable,
1589 					 bcn->len - (mgmt->u.beacon.variable -
1590 						     bcn->data));
1591 	if (!p2p_ie) {
1592 		ath12k_warn(ar->ab, "no P2P ie found in beacon\n");
1593 		return -ENOENT;
1594 	}
1595 
1596 	ret = ath12k_wmi_p2p_go_bcn_ie(ar, arvif->vdev_id, p2p_ie);
1597 	if (ret) {
1598 		ath12k_warn(ar->ab, "failed to submit P2P GO bcn ie for vdev %i: %d\n",
1599 			    arvif->vdev_id, ret);
1600 		return ret;
1601 	}
1602 
1603 	return 0;
1604 }
1605 
1606 static int ath12k_mac_remove_vendor_ie(struct sk_buff *skb, unsigned int oui,
1607 				       u8 oui_type, size_t ie_offset)
1608 {
1609 	const u8 *next, *end;
1610 	size_t len;
1611 	u8 *ie;
1612 
1613 	if (WARN_ON(skb->len < ie_offset))
1614 		return -EINVAL;
1615 
1616 	ie = (u8 *)cfg80211_find_vendor_ie(oui, oui_type,
1617 					   skb->data + ie_offset,
1618 					   skb->len - ie_offset);
1619 	if (!ie)
1620 		return -ENOENT;
1621 
1622 	len = ie[1] + 2;
1623 	end = skb->data + skb->len;
1624 	next = ie + len;
1625 
1626 	if (WARN_ON(next > end))
1627 		return -EINVAL;
1628 
1629 	memmove(ie, next, end - next);
1630 	skb_trim(skb, skb->len - len);
1631 
1632 	return 0;
1633 }
1634 
1635 static void ath12k_mac_set_arvif_ies(struct ath12k_link_vif *arvif,
1636 				     struct ath12k_link_vif *tx_arvif,
1637 				     struct sk_buff *bcn,
1638 				     u8 bssid_index, bool *nontx_profile_found)
1639 {
1640 	struct ieee80211_mgmt *mgmt = (struct ieee80211_mgmt *)bcn->data;
1641 	const struct element *elem, *nontx, *index, *nie, *ext_cap_ie;
1642 	const u8 *start, *tail;
1643 	u16 rem_len;
1644 	u8 i;
1645 
1646 	start = bcn->data + ieee80211_get_hdrlen_from_skb(bcn) + sizeof(mgmt->u.beacon);
1647 	tail = skb_tail_pointer(bcn);
1648 	rem_len = tail - start;
1649 
1650 	arvif->rsnie_present = false;
1651 	arvif->wpaie_present = false;
1652 
1653 	if (cfg80211_find_ie(WLAN_EID_RSN, start, rem_len))
1654 		arvif->rsnie_present = true;
1655 	if (cfg80211_find_vendor_ie(WLAN_OUI_MICROSOFT, WLAN_OUI_TYPE_MICROSOFT_WPA,
1656 				    start, rem_len))
1657 		arvif->wpaie_present = true;
1658 
1659 	ext_cap_ie = cfg80211_find_elem(WLAN_EID_EXT_CAPABILITY, start, rem_len);
1660 	if (ext_cap_ie && ext_cap_ie->datalen >= 11 &&
1661 	    (ext_cap_ie->data[10] & WLAN_EXT_CAPA11_BCN_PROTECT))
1662 		tx_arvif->beacon_prot = true;
1663 
1664 	/* Return from here for the transmitted profile */
1665 	if (!bssid_index)
1666 		return;
1667 
1668 	/* Initial rsnie_present for the nontransmitted profile is set to be same as that
1669 	 * of the transmitted profile. It will be changed if security configurations are
1670 	 * different.
1671 	 */
1672 	*nontx_profile_found = false;
1673 	for_each_element_id(elem, WLAN_EID_MULTIPLE_BSSID, start, rem_len) {
1674 		/* Fixed minimum MBSSID element length with at least one
1675 		 * nontransmitted BSSID profile is 12 bytes as given below;
1676 		 * 1 (max BSSID indicator) +
1677 		 * 2 (Nontransmitted BSSID profile: Subelement ID + length) +
1678 		 * 4 (Nontransmitted BSSID Capabilities: tag + length + info)
1679 		 * 2 (Nontransmitted BSSID SSID: tag + length)
1680 		 * 3 (Nontransmitted BSSID Index: tag + length + BSSID index
1681 		 */
1682 		if (elem->datalen < 12 || elem->data[0] < 1)
1683 			continue; /* Max BSSID indicator must be >=1 */
1684 
1685 		for_each_element(nontx, elem->data + 1, elem->datalen - 1) {
1686 			start = nontx->data;
1687 
1688 			if (nontx->id != 0 || nontx->datalen < 4)
1689 				continue; /* Invalid nontransmitted profile */
1690 
1691 			if (nontx->data[0] != WLAN_EID_NON_TX_BSSID_CAP ||
1692 			    nontx->data[1] != 2) {
1693 				continue; /* Missing nontransmitted BSS capabilities */
1694 			}
1695 
1696 			if (nontx->data[4] != WLAN_EID_SSID)
1697 				continue; /* Missing SSID for nontransmitted BSS */
1698 
1699 			index = cfg80211_find_elem(WLAN_EID_MULTI_BSSID_IDX,
1700 						   start, nontx->datalen);
1701 			if (!index || index->datalen < 1 || index->data[0] == 0)
1702 				continue; /* Invalid MBSSID Index element */
1703 
1704 			if (index->data[0] == bssid_index) {
1705 				*nontx_profile_found = true;
1706 
1707 				/* Check if nontx BSS has beacon protection enabled */
1708 				if (!tx_arvif->beacon_prot) {
1709 					ext_cap_ie =
1710 					    cfg80211_find_elem(WLAN_EID_EXT_CAPABILITY,
1711 							       nontx->data,
1712 							       nontx->datalen);
1713 					if (ext_cap_ie && ext_cap_ie->datalen >= 11 &&
1714 					    (ext_cap_ie->data[10] &
1715 					     WLAN_EXT_CAPA11_BCN_PROTECT))
1716 						tx_arvif->beacon_prot = true;
1717 				}
1718 
1719 				if (cfg80211_find_ie(WLAN_EID_RSN,
1720 						     nontx->data,
1721 						     nontx->datalen)) {
1722 					arvif->rsnie_present = true;
1723 					return;
1724 				} else if (!arvif->rsnie_present) {
1725 					return; /* Both tx and nontx BSS are open */
1726 				}
1727 
1728 				nie = cfg80211_find_ext_elem(WLAN_EID_EXT_NON_INHERITANCE,
1729 							     nontx->data,
1730 							     nontx->datalen);
1731 				if (!nie || nie->datalen < 2)
1732 					return; /* Invalid non-inheritance element */
1733 
1734 				for (i = 1; i < nie->datalen - 1; i++) {
1735 					if (nie->data[i] == WLAN_EID_RSN) {
1736 						arvif->rsnie_present = false;
1737 						break;
1738 					}
1739 				}
1740 
1741 				return;
1742 			}
1743 		}
1744 	}
1745 }
1746 
1747 static int ath12k_mac_setup_bcn_tmpl_ema(struct ath12k_link_vif *arvif,
1748 					 struct ath12k_link_vif *tx_arvif,
1749 					 u8 bssid_index)
1750 {
1751 	struct ath12k_wmi_bcn_tmpl_ema_arg ema_args;
1752 	struct ieee80211_ema_beacons *beacons;
1753 	bool nontx_profile_found = false;
1754 	int ret = 0;
1755 	u8 i;
1756 
1757 	beacons = ieee80211_beacon_get_template_ema_list(ath12k_ar_to_hw(tx_arvif->ar),
1758 							 tx_arvif->ahvif->vif,
1759 							 tx_arvif->link_id);
1760 	if (!beacons || !beacons->cnt) {
1761 		ath12k_warn(arvif->ar->ab,
1762 			    "failed to get ema beacon templates from mac80211\n");
1763 		return -EPERM;
1764 	}
1765 
1766 	if (tx_arvif == arvif)
1767 		ath12k_mac_set_arvif_ies(arvif, tx_arvif, beacons->bcn[0].skb, 0, NULL);
1768 
1769 	for (i = 0; i < beacons->cnt; i++) {
1770 		if (tx_arvif != arvif && !nontx_profile_found)
1771 			ath12k_mac_set_arvif_ies(arvif, tx_arvif, beacons->bcn[i].skb,
1772 						 bssid_index,
1773 						 &nontx_profile_found);
1774 
1775 		ema_args.bcn_cnt = beacons->cnt;
1776 		ema_args.bcn_index = i;
1777 		ret = ath12k_wmi_bcn_tmpl(tx_arvif, &beacons->bcn[i].offs,
1778 					  beacons->bcn[i].skb, &ema_args);
1779 		if (ret) {
1780 			ath12k_warn(tx_arvif->ar->ab,
1781 				    "failed to set ema beacon template id %i error %d\n",
1782 				    i, ret);
1783 			break;
1784 		}
1785 	}
1786 
1787 	if (tx_arvif != arvif && !nontx_profile_found)
1788 		ath12k_warn(arvif->ar->ab,
1789 			    "nontransmitted bssid index %u not found in beacon template\n",
1790 			    bssid_index);
1791 
1792 	ieee80211_beacon_free_ema_list(beacons);
1793 	return ret;
1794 }
1795 
1796 static int ath12k_mac_setup_bcn_tmpl(struct ath12k_link_vif *arvif)
1797 {
1798 	struct ath12k_vif *ahvif = arvif->ahvif;
1799 	struct ieee80211_vif *vif = ath12k_ahvif_to_vif(ahvif);
1800 	struct ieee80211_bss_conf *link_conf;
1801 	struct ath12k_link_vif *tx_arvif;
1802 	struct ath12k *ar = arvif->ar;
1803 	struct ath12k_base *ab = ar->ab;
1804 	struct ieee80211_mutable_offsets offs = {};
1805 	bool nontx_profile_found = false;
1806 	struct sk_buff *bcn;
1807 	int ret;
1808 
1809 	if (ahvif->vdev_type != WMI_VDEV_TYPE_AP)
1810 		return 0;
1811 
1812 	link_conf = ath12k_mac_get_link_bss_conf(arvif);
1813 	if (!link_conf) {
1814 		ath12k_warn(ar->ab, "unable to access bss link conf to set bcn tmpl for vif %pM link %u\n",
1815 			    vif->addr, arvif->link_id);
1816 		return -ENOLINK;
1817 	}
1818 
1819 	tx_arvif = ath12k_mac_get_tx_arvif(arvif, link_conf);
1820 	if (tx_arvif) {
1821 		if (tx_arvif != arvif && arvif->is_up)
1822 			return 0;
1823 
1824 		if (link_conf->ema_ap)
1825 			return ath12k_mac_setup_bcn_tmpl_ema(arvif, tx_arvif,
1826 							     link_conf->bssid_index);
1827 	} else {
1828 		tx_arvif = arvif;
1829 	}
1830 
1831 	bcn = ieee80211_beacon_get_template(ath12k_ar_to_hw(tx_arvif->ar),
1832 					    tx_arvif->ahvif->vif,
1833 					    &offs, tx_arvif->link_id);
1834 	if (!bcn) {
1835 		ath12k_warn(ab, "failed to get beacon template from mac80211\n");
1836 		return -EPERM;
1837 	}
1838 
1839 	if (tx_arvif == arvif) {
1840 		ath12k_mac_set_arvif_ies(arvif, tx_arvif, bcn, 0, NULL);
1841 	} else {
1842 		ath12k_mac_set_arvif_ies(arvif, tx_arvif, bcn,
1843 					 link_conf->bssid_index,
1844 					 &nontx_profile_found);
1845 		if (!nontx_profile_found)
1846 			ath12k_warn(ab,
1847 				    "nontransmitted profile not found in beacon template\n");
1848 	}
1849 
1850 	if (ahvif->vif->type == NL80211_IFTYPE_AP && ahvif->vif->p2p) {
1851 		ret = ath12k_mac_setup_bcn_p2p_ie(arvif, bcn);
1852 		if (ret) {
1853 			ath12k_warn(ab, "failed to setup P2P GO bcn ie: %d\n",
1854 				    ret);
1855 			goto free_bcn_skb;
1856 		}
1857 
1858 		/* P2P IE is inserted by firmware automatically (as
1859 		 * configured above) so remove it from the base beacon
1860 		 * template to avoid duplicate P2P IEs in beacon frames.
1861 		 */
1862 		ret = ath12k_mac_remove_vendor_ie(bcn, WLAN_OUI_WFA,
1863 						  WLAN_OUI_TYPE_WFA_P2P,
1864 						  offsetof(struct ieee80211_mgmt,
1865 							   u.beacon.variable));
1866 		if (ret) {
1867 			ath12k_warn(ab, "failed to remove P2P vendor ie: %d\n",
1868 				    ret);
1869 			goto free_bcn_skb;
1870 		}
1871 	}
1872 
1873 	ret = ath12k_wmi_bcn_tmpl(arvif, &offs, bcn, NULL);
1874 
1875 	if (ret)
1876 		ath12k_warn(ab, "failed to submit beacon template command: %d\n",
1877 			    ret);
1878 
1879 free_bcn_skb:
1880 	kfree_skb(bcn);
1881 	return ret;
1882 }
1883 
1884 static void ath12k_control_beaconing(struct ath12k_link_vif *arvif,
1885 				     struct ieee80211_bss_conf *info)
1886 {
1887 	struct ath12k_wmi_vdev_up_params params = {};
1888 	struct ath12k_vif *ahvif = arvif->ahvif;
1889 	struct ath12k *ar = arvif->ar;
1890 	int ret;
1891 
1892 	lockdep_assert_wiphy(ath12k_ar_to_hw(arvif->ar)->wiphy);
1893 
1894 	if (!info->enable_beacon) {
1895 		ret = ath12k_wmi_vdev_down(ar, arvif->vdev_id);
1896 		if (ret)
1897 			ath12k_warn(ar->ab, "failed to down vdev_id %i: %d\n",
1898 				    arvif->vdev_id, ret);
1899 
1900 		arvif->is_up = false;
1901 		return;
1902 	}
1903 
1904 	/* Install the beacon template to the FW */
1905 	ret = ath12k_mac_setup_bcn_tmpl(arvif);
1906 	if (ret) {
1907 		ath12k_warn(ar->ab, "failed to update bcn tmpl during vdev up: %d\n",
1908 			    ret);
1909 		return;
1910 	}
1911 
1912 	ahvif->aid = 0;
1913 
1914 	ether_addr_copy(arvif->bssid, info->addr);
1915 
1916 	params.vdev_id = arvif->vdev_id;
1917 	params.aid = ahvif->aid;
1918 	params.bssid = arvif->bssid;
1919 	params.tx_bssid = ath12k_mac_get_tx_bssid(arvif);
1920 	if (params.tx_bssid) {
1921 		params.nontx_profile_idx = info->bssid_index;
1922 		params.nontx_profile_cnt = 1 << info->bssid_indicator;
1923 	}
1924 	ret = ath12k_wmi_vdev_up(arvif->ar, &params);
1925 	if (ret) {
1926 		ath12k_warn(ar->ab, "failed to bring up vdev %d: %i\n",
1927 			    arvif->vdev_id, ret);
1928 		return;
1929 	}
1930 
1931 	arvif->is_up = true;
1932 
1933 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac vdev %d up\n", arvif->vdev_id);
1934 }
1935 
1936 static void ath12k_mac_handle_beacon_iter(void *data, u8 *mac,
1937 					  struct ieee80211_vif *vif)
1938 {
1939 	struct sk_buff *skb = data;
1940 	struct ieee80211_mgmt *mgmt = (void *)skb->data;
1941 	struct ath12k_vif *ahvif = ath12k_vif_to_ahvif(vif);
1942 	struct ath12k_link_vif *arvif = &ahvif->deflink;
1943 
1944 	if (vif->type != NL80211_IFTYPE_STATION || !arvif->is_created)
1945 		return;
1946 
1947 	if (!ether_addr_equal(mgmt->bssid, vif->bss_conf.bssid))
1948 		return;
1949 
1950 	cancel_delayed_work(&arvif->connection_loss_work);
1951 }
1952 
1953 void ath12k_mac_handle_beacon(struct ath12k *ar, struct sk_buff *skb)
1954 {
1955 	ieee80211_iterate_active_interfaces_atomic(ath12k_ar_to_hw(ar),
1956 						   IEEE80211_IFACE_ITER_NORMAL,
1957 						   ath12k_mac_handle_beacon_iter,
1958 						   skb);
1959 }
1960 
1961 void ath12k_mac_handle_beacon_miss(struct ath12k *ar,
1962 				   struct ath12k_link_vif *arvif)
1963 {
1964 	struct ieee80211_hw *hw = ath12k_ar_to_hw(ar);
1965 	struct ieee80211_vif *vif = ath12k_ahvif_to_vif(arvif->ahvif);
1966 
1967 	if (!(arvif->is_created && arvif->is_up))
1968 		return;
1969 
1970 	ieee80211_beacon_loss(vif);
1971 
1972 	/* Firmware doesn't report beacon loss events repeatedly. If AP probe
1973 	 * (done by mac80211) succeeds but beacons do not resume then it
1974 	 * doesn't make sense to continue operation. Queue connection loss work
1975 	 * which can be cancelled when beacon is received.
1976 	 */
1977 	ieee80211_queue_delayed_work(hw, &arvif->connection_loss_work,
1978 				     ATH12K_CONNECTION_LOSS_HZ);
1979 }
1980 
1981 static void ath12k_mac_vif_sta_connection_loss_work(struct work_struct *work)
1982 {
1983 	struct ath12k_link_vif *arvif = container_of(work, struct ath12k_link_vif,
1984 						     connection_loss_work.work);
1985 	struct ieee80211_vif *vif = arvif->ahvif->vif;
1986 
1987 	if (!arvif->is_up)
1988 		return;
1989 
1990 	ieee80211_connection_loss(vif);
1991 }
1992 
1993 static void ath12k_peer_assoc_h_basic(struct ath12k *ar,
1994 				      struct ath12k_link_vif *arvif,
1995 				      struct ath12k_link_sta *arsta,
1996 				      struct ath12k_wmi_peer_assoc_arg *arg)
1997 {
1998 	struct ieee80211_vif *vif = ath12k_ahvif_to_vif(arvif->ahvif);
1999 	struct ieee80211_sta *sta = ath12k_ahsta_to_sta(arsta->ahsta);
2000 	struct ieee80211_hw *hw = ath12k_ar_to_hw(ar);
2001 	struct ieee80211_bss_conf *bss_conf;
2002 	u32 aid;
2003 
2004 	lockdep_assert_wiphy(hw->wiphy);
2005 
2006 	if (vif->type == NL80211_IFTYPE_STATION)
2007 		aid = vif->cfg.aid;
2008 	else
2009 		aid = sta->aid;
2010 
2011 	ether_addr_copy(arg->peer_mac, arsta->addr);
2012 	arg->vdev_id = arvif->vdev_id;
2013 	arg->peer_associd = aid;
2014 	arg->auth_flag = true;
2015 	/* TODO: STA WAR in ath10k for listen interval required? */
2016 	arg->peer_listen_intval = hw->conf.listen_interval;
2017 	arg->peer_nss = 1;
2018 
2019 	bss_conf = ath12k_mac_get_link_bss_conf(arvif);
2020 	if (!bss_conf) {
2021 		ath12k_warn(ar->ab, "unable to access bss link conf in peer assoc for vif %pM link %u\n",
2022 			    vif->addr, arvif->link_id);
2023 		return;
2024 	}
2025 
2026 	arg->peer_caps = bss_conf->assoc_capability;
2027 }
2028 
2029 static void ath12k_peer_assoc_h_crypto(struct ath12k *ar,
2030 				       struct ath12k_link_vif *arvif,
2031 				       struct ath12k_link_sta *arsta,
2032 				       struct ath12k_wmi_peer_assoc_arg *arg)
2033 {
2034 	struct ieee80211_vif *vif = ath12k_ahvif_to_vif(arvif->ahvif);
2035 	struct ieee80211_sta *sta = ath12k_ahsta_to_sta(arsta->ahsta);
2036 	struct ieee80211_bss_conf *info;
2037 	struct cfg80211_chan_def def;
2038 	struct cfg80211_bss *bss;
2039 	struct ieee80211_hw *hw = ath12k_ar_to_hw(ar);
2040 	const u8 *rsnie = NULL;
2041 	const u8 *wpaie = NULL;
2042 
2043 	lockdep_assert_wiphy(hw->wiphy);
2044 
2045 	info = ath12k_mac_get_link_bss_conf(arvif);
2046 	if (!info) {
2047 		ath12k_warn(ar->ab, "unable to access bss link conf for peer assoc crypto for vif %pM link %u\n",
2048 			    vif->addr, arvif->link_id);
2049 		return;
2050 	}
2051 
2052 	if (WARN_ON(ath12k_mac_vif_link_chan(vif, arvif->link_id, &def)))
2053 		return;
2054 
2055 	bss = cfg80211_get_bss(hw->wiphy, def.chan, info->bssid, NULL, 0,
2056 			       IEEE80211_BSS_TYPE_ANY, IEEE80211_PRIVACY_ANY);
2057 
2058 	if (arvif->rsnie_present || arvif->wpaie_present) {
2059 		arg->need_ptk_4_way = true;
2060 		if (arvif->wpaie_present)
2061 			arg->need_gtk_2_way = true;
2062 	} else if (bss) {
2063 		const struct cfg80211_bss_ies *ies;
2064 
2065 		rcu_read_lock();
2066 		rsnie = ieee80211_bss_get_ie(bss, WLAN_EID_RSN);
2067 
2068 		ies = rcu_dereference(bss->ies);
2069 
2070 		wpaie = cfg80211_find_vendor_ie(WLAN_OUI_MICROSOFT,
2071 						WLAN_OUI_TYPE_MICROSOFT_WPA,
2072 						ies->data,
2073 						ies->len);
2074 		rcu_read_unlock();
2075 		cfg80211_put_bss(hw->wiphy, bss);
2076 	}
2077 
2078 	/* FIXME: base on RSN IE/WPA IE is a correct idea? */
2079 	if (rsnie || wpaie) {
2080 		ath12k_dbg(ar->ab, ATH12K_DBG_WMI,
2081 			   "%s: rsn ie found\n", __func__);
2082 		arg->need_ptk_4_way = true;
2083 	}
2084 
2085 	if (wpaie) {
2086 		ath12k_dbg(ar->ab, ATH12K_DBG_WMI,
2087 			   "%s: wpa ie found\n", __func__);
2088 		arg->need_gtk_2_way = true;
2089 	}
2090 
2091 	if (sta->mfp) {
2092 		/* TODO: Need to check if FW supports PMF? */
2093 		arg->is_pmf_enabled = true;
2094 	}
2095 
2096 	/* TODO: safe_mode_enabled (bypass 4-way handshake) flag req? */
2097 }
2098 
2099 static void ath12k_peer_assoc_h_rates(struct ath12k *ar,
2100 				      struct ath12k_link_vif *arvif,
2101 				      struct ath12k_link_sta *arsta,
2102 				      struct ath12k_wmi_peer_assoc_arg *arg)
2103 {
2104 	struct ieee80211_vif *vif = ath12k_ahvif_to_vif(arvif->ahvif);
2105 	struct ieee80211_sta *sta = ath12k_ahsta_to_sta(arsta->ahsta);
2106 	struct wmi_rate_set_arg *rateset = &arg->peer_legacy_rates;
2107 	struct ieee80211_link_sta *link_sta;
2108 	struct cfg80211_chan_def def;
2109 	const struct ieee80211_supported_band *sband;
2110 	const struct ieee80211_rate *rates;
2111 	struct ieee80211_hw *hw = ath12k_ar_to_hw(ar);
2112 	enum nl80211_band band;
2113 	u32 ratemask;
2114 	u8 rate;
2115 	int i;
2116 
2117 	lockdep_assert_wiphy(hw->wiphy);
2118 
2119 	if (WARN_ON(ath12k_mac_vif_link_chan(vif, arvif->link_id, &def)))
2120 		return;
2121 
2122 	link_sta = ath12k_mac_get_link_sta(arsta);
2123 	if (!link_sta) {
2124 		ath12k_warn(ar->ab, "unable to access link sta in peer assoc rates for sta %pM link %u\n",
2125 			    sta->addr, arsta->link_id);
2126 		return;
2127 	}
2128 
2129 	band = def.chan->band;
2130 	sband = hw->wiphy->bands[band];
2131 	ratemask = link_sta->supp_rates[band];
2132 	ratemask &= arvif->bitrate_mask.control[band].legacy;
2133 	rates = sband->bitrates;
2134 
2135 	rateset->num_rates = 0;
2136 
2137 	for (i = 0; i < 32; i++, ratemask >>= 1, rates++) {
2138 		if (!(ratemask & 1))
2139 			continue;
2140 
2141 		rate = ath12k_mac_bitrate_to_rate(rates->bitrate);
2142 		rateset->rates[rateset->num_rates] = rate;
2143 		rateset->num_rates++;
2144 	}
2145 }
2146 
2147 static bool
2148 ath12k_peer_assoc_h_ht_masked(const u8 *ht_mcs_mask)
2149 {
2150 	int nss;
2151 
2152 	for (nss = 0; nss < IEEE80211_HT_MCS_MASK_LEN; nss++)
2153 		if (ht_mcs_mask[nss])
2154 			return false;
2155 
2156 	return true;
2157 }
2158 
2159 static bool
2160 ath12k_peer_assoc_h_vht_masked(const u16 *vht_mcs_mask)
2161 {
2162 	int nss;
2163 
2164 	for (nss = 0; nss < NL80211_VHT_NSS_MAX; nss++)
2165 		if (vht_mcs_mask[nss])
2166 			return false;
2167 
2168 	return true;
2169 }
2170 
2171 static void ath12k_peer_assoc_h_ht(struct ath12k *ar,
2172 				   struct ath12k_link_vif *arvif,
2173 				   struct ath12k_link_sta *arsta,
2174 				   struct ath12k_wmi_peer_assoc_arg *arg)
2175 {
2176 	struct ieee80211_vif *vif = ath12k_ahvif_to_vif(arvif->ahvif);
2177 	struct ieee80211_sta *sta = ath12k_ahsta_to_sta(arsta->ahsta);
2178 	const struct ieee80211_sta_ht_cap *ht_cap;
2179 	struct ieee80211_link_sta *link_sta;
2180 	struct cfg80211_chan_def def;
2181 	enum nl80211_band band;
2182 	const u8 *ht_mcs_mask;
2183 	int i, n;
2184 	u8 max_nss;
2185 	u32 stbc;
2186 
2187 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
2188 
2189 	if (WARN_ON(ath12k_mac_vif_link_chan(vif, arvif->link_id, &def)))
2190 		return;
2191 
2192 	link_sta = ath12k_mac_get_link_sta(arsta);
2193 	if (!link_sta) {
2194 		ath12k_warn(ar->ab, "unable to access link sta in peer assoc ht for sta %pM link %u\n",
2195 			    sta->addr, arsta->link_id);
2196 		return;
2197 	}
2198 
2199 	ht_cap = &link_sta->ht_cap;
2200 	if (!ht_cap->ht_supported)
2201 		return;
2202 
2203 	band = def.chan->band;
2204 	ht_mcs_mask = arvif->bitrate_mask.control[band].ht_mcs;
2205 
2206 	if (ath12k_peer_assoc_h_ht_masked(ht_mcs_mask))
2207 		return;
2208 
2209 	arg->ht_flag = true;
2210 
2211 	arg->peer_max_mpdu = (1 << (IEEE80211_HT_MAX_AMPDU_FACTOR +
2212 				    ht_cap->ampdu_factor)) - 1;
2213 
2214 	arg->peer_mpdu_density =
2215 		ath12k_parse_mpdudensity(ht_cap->ampdu_density);
2216 
2217 	arg->peer_ht_caps = ht_cap->cap;
2218 	arg->peer_rate_caps |= WMI_HOST_RC_HT_FLAG;
2219 
2220 	if (ht_cap->cap & IEEE80211_HT_CAP_LDPC_CODING)
2221 		arg->ldpc_flag = true;
2222 
2223 	if (link_sta->bandwidth >= IEEE80211_STA_RX_BW_40) {
2224 		arg->bw_40 = true;
2225 		arg->peer_rate_caps |= WMI_HOST_RC_CW40_FLAG;
2226 	}
2227 
2228 	/* As firmware handles these two flags (IEEE80211_HT_CAP_SGI_20
2229 	 * and IEEE80211_HT_CAP_SGI_40) for enabling SGI, reset both
2230 	 * flags if guard interval is to force Long GI
2231 	 */
2232 	if (arvif->bitrate_mask.control[band].gi == NL80211_TXRATE_FORCE_LGI) {
2233 		arg->peer_ht_caps &= ~(IEEE80211_HT_CAP_SGI_20 | IEEE80211_HT_CAP_SGI_40);
2234 	} else {
2235 		/* Enable SGI flag if either SGI_20 or SGI_40 is supported */
2236 		if (ht_cap->cap & (IEEE80211_HT_CAP_SGI_20 | IEEE80211_HT_CAP_SGI_40))
2237 			arg->peer_rate_caps |= WMI_HOST_RC_SGI_FLAG;
2238 	}
2239 
2240 	if (ht_cap->cap & IEEE80211_HT_CAP_TX_STBC) {
2241 		arg->peer_rate_caps |= WMI_HOST_RC_TX_STBC_FLAG;
2242 		arg->stbc_flag = true;
2243 	}
2244 
2245 	if (ht_cap->cap & IEEE80211_HT_CAP_RX_STBC) {
2246 		stbc = ht_cap->cap & IEEE80211_HT_CAP_RX_STBC;
2247 		stbc = stbc >> IEEE80211_HT_CAP_RX_STBC_SHIFT;
2248 		stbc = stbc << WMI_HOST_RC_RX_STBC_FLAG_S;
2249 		arg->peer_rate_caps |= stbc;
2250 		arg->stbc_flag = true;
2251 	}
2252 
2253 	if (ht_cap->mcs.rx_mask[1] && ht_cap->mcs.rx_mask[2])
2254 		arg->peer_rate_caps |= WMI_HOST_RC_TS_FLAG;
2255 	else if (ht_cap->mcs.rx_mask[1])
2256 		arg->peer_rate_caps |= WMI_HOST_RC_DS_FLAG;
2257 
2258 	for (i = 0, n = 0, max_nss = 0; i < IEEE80211_HT_MCS_MASK_LEN * 8; i++)
2259 		if ((ht_cap->mcs.rx_mask[i / 8] & BIT(i % 8)) &&
2260 		    (ht_mcs_mask[i / 8] & BIT(i % 8))) {
2261 			max_nss = (i / 8) + 1;
2262 			arg->peer_ht_rates.rates[n++] = i;
2263 		}
2264 
2265 	/* This is a workaround for HT-enabled STAs which break the spec
2266 	 * and have no HT capabilities RX mask (no HT RX MCS map).
2267 	 *
2268 	 * As per spec, in section 20.3.5 Modulation and coding scheme (MCS),
2269 	 * MCS 0 through 7 are mandatory in 20MHz with 800 ns GI at all STAs.
2270 	 *
2271 	 * Firmware asserts if such situation occurs.
2272 	 */
2273 	if (n == 0) {
2274 		arg->peer_ht_rates.num_rates = 8;
2275 		for (i = 0; i < arg->peer_ht_rates.num_rates; i++)
2276 			arg->peer_ht_rates.rates[i] = i;
2277 	} else {
2278 		arg->peer_ht_rates.num_rates = n;
2279 		arg->peer_nss = min(link_sta->rx_nss, max_nss);
2280 	}
2281 
2282 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac ht peer %pM mcs cnt %d nss %d\n",
2283 		   arg->peer_mac,
2284 		   arg->peer_ht_rates.num_rates,
2285 		   arg->peer_nss);
2286 }
2287 
2288 static int ath12k_mac_get_max_vht_mcs_map(u16 mcs_map, int nss)
2289 {
2290 	switch ((mcs_map >> (2 * nss)) & 0x3) {
2291 	case IEEE80211_VHT_MCS_SUPPORT_0_7: return BIT(8) - 1;
2292 	case IEEE80211_VHT_MCS_SUPPORT_0_8: return BIT(9) - 1;
2293 	case IEEE80211_VHT_MCS_SUPPORT_0_9: return BIT(10) - 1;
2294 	}
2295 	return 0;
2296 }
2297 
2298 static u16
2299 ath12k_peer_assoc_h_vht_limit(u16 tx_mcs_set,
2300 			      const u16 vht_mcs_limit[NL80211_VHT_NSS_MAX])
2301 {
2302 	int idx_limit;
2303 	int nss;
2304 	u16 mcs_map;
2305 	u16 mcs;
2306 
2307 	for (nss = 0; nss < NL80211_VHT_NSS_MAX; nss++) {
2308 		mcs_map = ath12k_mac_get_max_vht_mcs_map(tx_mcs_set, nss) &
2309 			  vht_mcs_limit[nss];
2310 
2311 		if (mcs_map)
2312 			idx_limit = fls(mcs_map) - 1;
2313 		else
2314 			idx_limit = -1;
2315 
2316 		switch (idx_limit) {
2317 		case 0:
2318 		case 1:
2319 		case 2:
2320 		case 3:
2321 		case 4:
2322 		case 5:
2323 		case 6:
2324 		case 7:
2325 			mcs = IEEE80211_VHT_MCS_SUPPORT_0_7;
2326 			break;
2327 		case 8:
2328 			mcs = IEEE80211_VHT_MCS_SUPPORT_0_8;
2329 			break;
2330 		case 9:
2331 			mcs = IEEE80211_VHT_MCS_SUPPORT_0_9;
2332 			break;
2333 		default:
2334 			WARN_ON(1);
2335 			fallthrough;
2336 		case -1:
2337 			mcs = IEEE80211_VHT_MCS_NOT_SUPPORTED;
2338 			break;
2339 		}
2340 
2341 		tx_mcs_set &= ~(0x3 << (nss * 2));
2342 		tx_mcs_set |= mcs << (nss * 2);
2343 	}
2344 
2345 	return tx_mcs_set;
2346 }
2347 
2348 static u8 ath12k_get_nss_160mhz(struct ath12k *ar,
2349 				u8 max_nss)
2350 {
2351 	u8 nss_ratio_info = ar->pdev->cap.nss_ratio_info;
2352 	u8 max_sup_nss = 0;
2353 
2354 	switch (nss_ratio_info) {
2355 	case WMI_NSS_RATIO_1BY2_NSS:
2356 		max_sup_nss = max_nss >> 1;
2357 		break;
2358 	case WMI_NSS_RATIO_3BY4_NSS:
2359 		ath12k_warn(ar->ab, "WMI_NSS_RATIO_3BY4_NSS not supported\n");
2360 		break;
2361 	case WMI_NSS_RATIO_1_NSS:
2362 		max_sup_nss = max_nss;
2363 		break;
2364 	case WMI_NSS_RATIO_2_NSS:
2365 		ath12k_warn(ar->ab, "WMI_NSS_RATIO_2_NSS not supported\n");
2366 		break;
2367 	default:
2368 		ath12k_warn(ar->ab, "invalid nss ratio received from fw: %d\n",
2369 			    nss_ratio_info);
2370 		break;
2371 	}
2372 
2373 	return max_sup_nss;
2374 }
2375 
2376 static void ath12k_peer_assoc_h_vht(struct ath12k *ar,
2377 				    struct ath12k_link_vif *arvif,
2378 				    struct ath12k_link_sta *arsta,
2379 				    struct ath12k_wmi_peer_assoc_arg *arg)
2380 {
2381 	struct ieee80211_vif *vif = ath12k_ahvif_to_vif(arvif->ahvif);
2382 	struct ieee80211_sta *sta = ath12k_ahsta_to_sta(arsta->ahsta);
2383 	const struct ieee80211_sta_vht_cap *vht_cap;
2384 	struct ieee80211_link_sta *link_sta;
2385 	struct cfg80211_chan_def def;
2386 	enum nl80211_band band;
2387 	u16 *vht_mcs_mask;
2388 	u8 ampdu_factor;
2389 	u8 max_nss, vht_mcs;
2390 	int i, vht_nss, nss_idx;
2391 	bool user_rate_valid = true;
2392 	u32 rx_nss, tx_nss, nss_160;
2393 
2394 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
2395 
2396 	if (WARN_ON(ath12k_mac_vif_link_chan(vif, arvif->link_id, &def)))
2397 		return;
2398 
2399 	link_sta = ath12k_mac_get_link_sta(arsta);
2400 	if (!link_sta) {
2401 		ath12k_warn(ar->ab, "unable to access link sta in peer assoc vht for sta %pM link %u\n",
2402 			    sta->addr, arsta->link_id);
2403 		return;
2404 	}
2405 
2406 	vht_cap = &link_sta->vht_cap;
2407 	if (!vht_cap->vht_supported)
2408 		return;
2409 
2410 	band = def.chan->band;
2411 	vht_mcs_mask = arvif->bitrate_mask.control[band].vht_mcs;
2412 
2413 	if (ath12k_peer_assoc_h_vht_masked(vht_mcs_mask))
2414 		return;
2415 
2416 	arg->vht_flag = true;
2417 
2418 	/* TODO: similar flags required? */
2419 	arg->vht_capable = true;
2420 
2421 	if (def.chan->band == NL80211_BAND_2GHZ)
2422 		arg->vht_ng_flag = true;
2423 
2424 	arg->peer_vht_caps = vht_cap->cap;
2425 
2426 	ampdu_factor = (vht_cap->cap &
2427 			IEEE80211_VHT_CAP_MAX_A_MPDU_LENGTH_EXPONENT_MASK) >>
2428 		       IEEE80211_VHT_CAP_MAX_A_MPDU_LENGTH_EXPONENT_SHIFT;
2429 
2430 	/* Workaround: Some Netgear/Linksys 11ac APs set Rx A-MPDU factor to
2431 	 * zero in VHT IE. Using it would result in degraded throughput.
2432 	 * arg->peer_max_mpdu at this point contains HT max_mpdu so keep
2433 	 * it if VHT max_mpdu is smaller.
2434 	 */
2435 	arg->peer_max_mpdu = max(arg->peer_max_mpdu,
2436 				 (1U << (IEEE80211_HT_MAX_AMPDU_FACTOR +
2437 					ampdu_factor)) - 1);
2438 
2439 	if (link_sta->bandwidth == IEEE80211_STA_RX_BW_80)
2440 		arg->bw_80 = true;
2441 
2442 	if (link_sta->bandwidth == IEEE80211_STA_RX_BW_160)
2443 		arg->bw_160 = true;
2444 
2445 	vht_nss =  ath12k_mac_max_vht_nss(vht_mcs_mask);
2446 
2447 	if (vht_nss > link_sta->rx_nss) {
2448 		user_rate_valid = false;
2449 		for (nss_idx = link_sta->rx_nss - 1; nss_idx >= 0; nss_idx--) {
2450 			if (vht_mcs_mask[nss_idx]) {
2451 				user_rate_valid = true;
2452 				break;
2453 			}
2454 		}
2455 	}
2456 
2457 	if (!user_rate_valid) {
2458 		ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
2459 			   "Setting vht range MCS value to peer supported nss:%d for peer %pM\n",
2460 			   link_sta->rx_nss, arsta->addr);
2461 		vht_mcs_mask[link_sta->rx_nss - 1] = vht_mcs_mask[vht_nss - 1];
2462 	}
2463 
2464 	/* Calculate peer NSS capability from VHT capabilities if STA
2465 	 * supports VHT.
2466 	 */
2467 	for (i = 0, max_nss = 0, vht_mcs = 0; i < NL80211_VHT_NSS_MAX; i++) {
2468 		vht_mcs = __le16_to_cpu(vht_cap->vht_mcs.rx_mcs_map) >>
2469 			  (2 * i) & 3;
2470 
2471 		if (vht_mcs != IEEE80211_VHT_MCS_NOT_SUPPORTED &&
2472 		    vht_mcs_mask[i])
2473 			max_nss = i + 1;
2474 	}
2475 	arg->peer_nss = min(link_sta->rx_nss, max_nss);
2476 	arg->rx_max_rate = __le16_to_cpu(vht_cap->vht_mcs.rx_highest);
2477 	arg->rx_mcs_set = __le16_to_cpu(vht_cap->vht_mcs.rx_mcs_map);
2478 	arg->rx_mcs_set = ath12k_peer_assoc_h_vht_limit(arg->rx_mcs_set, vht_mcs_mask);
2479 
2480 	arg->tx_max_rate = __le16_to_cpu(vht_cap->vht_mcs.tx_highest);
2481 	arg->tx_mcs_set = __le16_to_cpu(vht_cap->vht_mcs.tx_mcs_map);
2482 
2483 	/* In QCN9274 platform, VHT MCS rate 10 and 11 is enabled by default.
2484 	 * VHT MCS rate 10 and 11 is not supported in 11ac standard.
2485 	 * so explicitly disable the VHT MCS rate 10 and 11 in 11ac mode.
2486 	 */
2487 	arg->tx_mcs_set &= ~IEEE80211_VHT_MCS_SUPPORT_0_11_MASK;
2488 	arg->tx_mcs_set |= IEEE80211_DISABLE_VHT_MCS_SUPPORT_0_11;
2489 
2490 	if ((arg->tx_mcs_set & IEEE80211_VHT_MCS_NOT_SUPPORTED) ==
2491 			IEEE80211_VHT_MCS_NOT_SUPPORTED)
2492 		arg->peer_vht_caps &= ~IEEE80211_VHT_CAP_MU_BEAMFORMEE_CAPABLE;
2493 
2494 	/* TODO:  Check */
2495 	arg->tx_max_mcs_nss = 0xFF;
2496 
2497 	if (arg->peer_phymode == MODE_11AC_VHT160) {
2498 		tx_nss = ath12k_get_nss_160mhz(ar, max_nss);
2499 		rx_nss = min(arg->peer_nss, tx_nss);
2500 		arg->peer_bw_rxnss_override = ATH12K_BW_NSS_MAP_ENABLE;
2501 
2502 		if (!rx_nss) {
2503 			ath12k_warn(ar->ab, "invalid max_nss\n");
2504 			return;
2505 		}
2506 
2507 		nss_160 = u32_encode_bits(rx_nss - 1, ATH12K_PEER_RX_NSS_160MHZ);
2508 		arg->peer_bw_rxnss_override |= nss_160;
2509 	}
2510 
2511 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
2512 		   "mac vht peer %pM max_mpdu %d flags 0x%x nss_override 0x%x\n",
2513 		   arsta->addr, arg->peer_max_mpdu, arg->peer_flags,
2514 		   arg->peer_bw_rxnss_override);
2515 }
2516 
2517 static int ath12k_mac_get_max_he_mcs_map(u16 mcs_map, int nss)
2518 {
2519 	switch ((mcs_map >> (2 * nss)) & 0x3) {
2520 	case IEEE80211_HE_MCS_SUPPORT_0_7: return BIT(8) - 1;
2521 	case IEEE80211_HE_MCS_SUPPORT_0_9: return BIT(10) - 1;
2522 	case IEEE80211_HE_MCS_SUPPORT_0_11: return BIT(12) - 1;
2523 	}
2524 	return 0;
2525 }
2526 
2527 static u16 ath12k_peer_assoc_h_he_limit(u16 tx_mcs_set,
2528 					const u16 *he_mcs_limit)
2529 {
2530 	int idx_limit;
2531 	int nss;
2532 	u16 mcs_map;
2533 	u16 mcs;
2534 
2535 	for (nss = 0; nss < NL80211_HE_NSS_MAX; nss++) {
2536 		mcs_map = ath12k_mac_get_max_he_mcs_map(tx_mcs_set, nss) &
2537 			he_mcs_limit[nss];
2538 
2539 		if (mcs_map)
2540 			idx_limit = fls(mcs_map) - 1;
2541 		else
2542 			idx_limit = -1;
2543 
2544 		switch (idx_limit) {
2545 		case 0 ... 7:
2546 			mcs = IEEE80211_HE_MCS_SUPPORT_0_7;
2547 			break;
2548 		case 8:
2549 		case 9:
2550 			mcs = IEEE80211_HE_MCS_SUPPORT_0_9;
2551 			break;
2552 		case 10:
2553 		case 11:
2554 			mcs = IEEE80211_HE_MCS_SUPPORT_0_11;
2555 			break;
2556 		default:
2557 			WARN_ON(1);
2558 			fallthrough;
2559 		case -1:
2560 			mcs = IEEE80211_HE_MCS_NOT_SUPPORTED;
2561 			break;
2562 		}
2563 
2564 		tx_mcs_set &= ~(0x3 << (nss * 2));
2565 		tx_mcs_set |= mcs << (nss * 2);
2566 	}
2567 
2568 	return tx_mcs_set;
2569 }
2570 
2571 static bool
2572 ath12k_peer_assoc_h_he_masked(const u16 he_mcs_mask[NL80211_HE_NSS_MAX])
2573 {
2574 	int nss;
2575 
2576 	for (nss = 0; nss < NL80211_HE_NSS_MAX; nss++)
2577 		if (he_mcs_mask[nss])
2578 			return false;
2579 
2580 	return true;
2581 }
2582 
2583 static void ath12k_peer_assoc_h_he(struct ath12k *ar,
2584 				   struct ath12k_link_vif *arvif,
2585 				   struct ath12k_link_sta *arsta,
2586 				   struct ath12k_wmi_peer_assoc_arg *arg)
2587 {
2588 	struct ieee80211_vif *vif = ath12k_ahvif_to_vif(arvif->ahvif);
2589 	struct ieee80211_sta *sta = ath12k_ahsta_to_sta(arsta->ahsta);
2590 	const struct ieee80211_sta_he_cap *he_cap;
2591 	struct ieee80211_bss_conf *link_conf;
2592 	struct ieee80211_link_sta *link_sta;
2593 	struct cfg80211_chan_def def;
2594 	int i;
2595 	u8 ampdu_factor, max_nss;
2596 	u8 rx_mcs_80 = IEEE80211_HE_MCS_NOT_SUPPORTED;
2597 	u8 rx_mcs_160 = IEEE80211_HE_MCS_NOT_SUPPORTED;
2598 	u16 mcs_160_map, mcs_80_map;
2599 	u8 link_id = arvif->link_id;
2600 	bool support_160;
2601 	enum nl80211_band band;
2602 	u16 *he_mcs_mask;
2603 	u8 he_mcs;
2604 	u16 he_tx_mcs = 0, v = 0;
2605 	int he_nss, nss_idx;
2606 	bool user_rate_valid = true;
2607 	u32 rx_nss, tx_nss, nss_160;
2608 
2609 	if (WARN_ON(ath12k_mac_vif_link_chan(vif, link_id, &def)))
2610 		return;
2611 
2612 	link_conf = ath12k_mac_get_link_bss_conf(arvif);
2613 	if (!link_conf) {
2614 		ath12k_warn(ar->ab, "unable to access bss link conf in peer assoc he for vif %pM link %u",
2615 			    vif->addr, link_id);
2616 		return;
2617 	}
2618 
2619 	link_sta = ath12k_mac_get_link_sta(arsta);
2620 	if (!link_sta) {
2621 		ath12k_warn(ar->ab, "unable to access link sta in peer assoc he for sta %pM link %u\n",
2622 			    sta->addr, arsta->link_id);
2623 		return;
2624 	}
2625 
2626 	he_cap = &link_sta->he_cap;
2627 	if (!he_cap->has_he)
2628 		return;
2629 
2630 	band = def.chan->band;
2631 	he_mcs_mask = arvif->bitrate_mask.control[band].he_mcs;
2632 
2633 	if (ath12k_peer_assoc_h_he_masked(he_mcs_mask))
2634 		return;
2635 
2636 	arg->he_flag = true;
2637 
2638 	support_160 = !!(he_cap->he_cap_elem.phy_cap_info[0] &
2639 		  IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_160MHZ_IN_5G);
2640 
2641 	/* Supported HE-MCS and NSS Set of peer he_cap is intersection with self he_cp */
2642 	mcs_160_map = le16_to_cpu(he_cap->he_mcs_nss_supp.rx_mcs_160);
2643 	mcs_80_map = le16_to_cpu(he_cap->he_mcs_nss_supp.rx_mcs_80);
2644 
2645 	if (support_160) {
2646 		for (i = 7; i >= 0; i--) {
2647 			u8 mcs_160 = (mcs_160_map >> (2 * i)) & 3;
2648 
2649 			if (mcs_160 != IEEE80211_HE_MCS_NOT_SUPPORTED) {
2650 				rx_mcs_160 = i + 1;
2651 				break;
2652 			}
2653 		}
2654 	}
2655 
2656 	for (i = 7; i >= 0; i--) {
2657 		u8 mcs_80 = (mcs_80_map >> (2 * i)) & 3;
2658 
2659 		if (mcs_80 != IEEE80211_HE_MCS_NOT_SUPPORTED) {
2660 			rx_mcs_80 = i + 1;
2661 			break;
2662 		}
2663 	}
2664 
2665 	if (support_160)
2666 		max_nss = min(rx_mcs_80, rx_mcs_160);
2667 	else
2668 		max_nss = rx_mcs_80;
2669 
2670 	arg->peer_nss = min(link_sta->rx_nss, max_nss);
2671 
2672 	memcpy(&arg->peer_he_cap_macinfo, he_cap->he_cap_elem.mac_cap_info,
2673 	       sizeof(he_cap->he_cap_elem.mac_cap_info));
2674 	memcpy(&arg->peer_he_cap_phyinfo, he_cap->he_cap_elem.phy_cap_info,
2675 	       sizeof(he_cap->he_cap_elem.phy_cap_info));
2676 	arg->peer_he_ops = link_conf->he_oper.params;
2677 
2678 	/* the top most byte is used to indicate BSS color info */
2679 	arg->peer_he_ops &= 0xffffff;
2680 
2681 	/* As per section 26.6.1 IEEE Std 802.11ax‐2022, if the Max AMPDU
2682 	 * Exponent Extension in HE cap is zero, use the arg->peer_max_mpdu
2683 	 * as calculated while parsing VHT caps(if VHT caps is present)
2684 	 * or HT caps (if VHT caps is not present).
2685 	 *
2686 	 * For non-zero value of Max AMPDU Exponent Extension in HE MAC caps,
2687 	 * if a HE STA sends VHT cap and HE cap IE in assoc request then, use
2688 	 * MAX_AMPDU_LEN_FACTOR as 20 to calculate max_ampdu length.
2689 	 * If a HE STA that does not send VHT cap, but HE and HT cap in assoc
2690 	 * request, then use MAX_AMPDU_LEN_FACTOR as 16 to calculate max_ampdu
2691 	 * length.
2692 	 */
2693 	ampdu_factor = u8_get_bits(he_cap->he_cap_elem.mac_cap_info[3],
2694 				   IEEE80211_HE_MAC_CAP3_MAX_AMPDU_LEN_EXP_MASK);
2695 
2696 	if (ampdu_factor) {
2697 		if (link_sta->vht_cap.vht_supported)
2698 			arg->peer_max_mpdu = (1 << (IEEE80211_HE_VHT_MAX_AMPDU_FACTOR +
2699 						    ampdu_factor)) - 1;
2700 		else if (link_sta->ht_cap.ht_supported)
2701 			arg->peer_max_mpdu = (1 << (IEEE80211_HE_HT_MAX_AMPDU_FACTOR +
2702 						    ampdu_factor)) - 1;
2703 	}
2704 
2705 	if (he_cap->he_cap_elem.phy_cap_info[6] &
2706 	    IEEE80211_HE_PHY_CAP6_PPE_THRESHOLD_PRESENT) {
2707 		int bit = 7;
2708 		int nss, ru;
2709 
2710 		arg->peer_ppet.numss_m1 = he_cap->ppe_thres[0] &
2711 					  IEEE80211_PPE_THRES_NSS_MASK;
2712 		arg->peer_ppet.ru_bit_mask =
2713 			(he_cap->ppe_thres[0] &
2714 			 IEEE80211_PPE_THRES_RU_INDEX_BITMASK_MASK) >>
2715 			IEEE80211_PPE_THRES_RU_INDEX_BITMASK_POS;
2716 
2717 		for (nss = 0; nss <= arg->peer_ppet.numss_m1; nss++) {
2718 			for (ru = 0; ru < 4; ru++) {
2719 				u32 val = 0;
2720 				int i;
2721 
2722 				if ((arg->peer_ppet.ru_bit_mask & BIT(ru)) == 0)
2723 					continue;
2724 				for (i = 0; i < 6; i++) {
2725 					val >>= 1;
2726 					val |= ((he_cap->ppe_thres[bit / 8] >>
2727 						 (bit % 8)) & 0x1) << 5;
2728 					bit++;
2729 				}
2730 				arg->peer_ppet.ppet16_ppet8_ru3_ru0[nss] |=
2731 								val << (ru * 6);
2732 			}
2733 		}
2734 	}
2735 
2736 	if (he_cap->he_cap_elem.mac_cap_info[0] & IEEE80211_HE_MAC_CAP0_TWT_RES)
2737 		arg->twt_responder = true;
2738 	if (he_cap->he_cap_elem.mac_cap_info[0] & IEEE80211_HE_MAC_CAP0_TWT_REQ)
2739 		arg->twt_requester = true;
2740 
2741 	he_nss = ath12k_mac_max_he_nss(he_mcs_mask);
2742 
2743 	if (he_nss > link_sta->rx_nss) {
2744 		user_rate_valid = false;
2745 		for (nss_idx = link_sta->rx_nss - 1; nss_idx >= 0; nss_idx--) {
2746 			if (he_mcs_mask[nss_idx]) {
2747 				user_rate_valid = true;
2748 				break;
2749 			}
2750 		}
2751 	}
2752 
2753 	if (!user_rate_valid) {
2754 		ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
2755 			   "Setting he range MCS value to peer supported nss:%d for peer %pM\n",
2756 			   link_sta->rx_nss, arsta->addr);
2757 		he_mcs_mask[link_sta->rx_nss - 1] = he_mcs_mask[he_nss - 1];
2758 	}
2759 
2760 	switch (link_sta->bandwidth) {
2761 	case IEEE80211_STA_RX_BW_160:
2762 		v = le16_to_cpu(he_cap->he_mcs_nss_supp.rx_mcs_160);
2763 		v = ath12k_peer_assoc_h_he_limit(v, he_mcs_mask);
2764 		arg->peer_he_rx_mcs_set[WMI_HECAP_TXRX_MCS_NSS_IDX_160] = v;
2765 
2766 		v = le16_to_cpu(he_cap->he_mcs_nss_supp.tx_mcs_160);
2767 		arg->peer_he_tx_mcs_set[WMI_HECAP_TXRX_MCS_NSS_IDX_160] = v;
2768 
2769 		arg->peer_he_mcs_count++;
2770 		if (!he_tx_mcs)
2771 			he_tx_mcs = v;
2772 		fallthrough;
2773 
2774 	default:
2775 		v = le16_to_cpu(he_cap->he_mcs_nss_supp.rx_mcs_80);
2776 		v = ath12k_peer_assoc_h_he_limit(v, he_mcs_mask);
2777 		arg->peer_he_rx_mcs_set[WMI_HECAP_TXRX_MCS_NSS_IDX_80] = v;
2778 
2779 		v = le16_to_cpu(he_cap->he_mcs_nss_supp.tx_mcs_80);
2780 		arg->peer_he_tx_mcs_set[WMI_HECAP_TXRX_MCS_NSS_IDX_80] = v;
2781 
2782 		arg->peer_he_mcs_count++;
2783 		if (!he_tx_mcs)
2784 			he_tx_mcs = v;
2785 		break;
2786 	}
2787 
2788 	/* Calculate peer NSS capability from HE capabilities if STA
2789 	 * supports HE.
2790 	 */
2791 	for (i = 0, max_nss = 0, he_mcs = 0; i < NL80211_HE_NSS_MAX; i++) {
2792 		he_mcs = he_tx_mcs >> (2 * i) & 3;
2793 
2794 		/* In case of fixed rates, MCS Range in he_tx_mcs might have
2795 		 * unsupported range, with he_mcs_mask set, so check either of them
2796 		 * to find nss.
2797 		 */
2798 		if (he_mcs != IEEE80211_HE_MCS_NOT_SUPPORTED ||
2799 		    he_mcs_mask[i])
2800 			max_nss = i + 1;
2801 	}
2802 
2803 	max_nss = min(max_nss, ar->num_tx_chains);
2804 	arg->peer_nss = min(link_sta->rx_nss, max_nss);
2805 
2806 	if (arg->peer_phymode == MODE_11AX_HE160) {
2807 		tx_nss = ath12k_get_nss_160mhz(ar, ar->num_tx_chains);
2808 		rx_nss = min(arg->peer_nss, tx_nss);
2809 
2810 		arg->peer_nss = min(link_sta->rx_nss, ar->num_rx_chains);
2811 		arg->peer_bw_rxnss_override = ATH12K_BW_NSS_MAP_ENABLE;
2812 
2813 		if (!rx_nss) {
2814 			ath12k_warn(ar->ab, "invalid max_nss\n");
2815 			return;
2816 		}
2817 
2818 		nss_160 = u32_encode_bits(rx_nss - 1, ATH12K_PEER_RX_NSS_160MHZ);
2819 		arg->peer_bw_rxnss_override |= nss_160;
2820 	}
2821 
2822 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
2823 		   "mac he peer %pM nss %d mcs cnt %d nss_override 0x%x\n",
2824 		   arsta->addr, arg->peer_nss,
2825 		   arg->peer_he_mcs_count,
2826 		   arg->peer_bw_rxnss_override);
2827 }
2828 
2829 static void ath12k_peer_assoc_h_he_6ghz(struct ath12k *ar,
2830 					struct ath12k_link_vif *arvif,
2831 					struct ath12k_link_sta *arsta,
2832 					struct ath12k_wmi_peer_assoc_arg *arg)
2833 {
2834 	struct ieee80211_vif *vif = ath12k_ahvif_to_vif(arvif->ahvif);
2835 	struct ieee80211_sta *sta = ath12k_ahsta_to_sta(arsta->ahsta);
2836 	const struct ieee80211_sta_he_cap *he_cap;
2837 	struct ieee80211_link_sta *link_sta;
2838 	struct cfg80211_chan_def def;
2839 	enum nl80211_band band;
2840 	u8 ampdu_factor, mpdu_density;
2841 
2842 	if (WARN_ON(ath12k_mac_vif_link_chan(vif, arvif->link_id, &def)))
2843 		return;
2844 
2845 	band = def.chan->band;
2846 
2847 	link_sta = ath12k_mac_get_link_sta(arsta);
2848 	if (!link_sta) {
2849 		ath12k_warn(ar->ab, "unable to access link sta in peer assoc he 6ghz for sta %pM link %u\n",
2850 			    sta->addr, arsta->link_id);
2851 		return;
2852 	}
2853 
2854 	he_cap = &link_sta->he_cap;
2855 
2856 	if (!arg->he_flag || band != NL80211_BAND_6GHZ || !link_sta->he_6ghz_capa.capa)
2857 		return;
2858 
2859 	if (link_sta->bandwidth == IEEE80211_STA_RX_BW_40)
2860 		arg->bw_40 = true;
2861 
2862 	if (link_sta->bandwidth == IEEE80211_STA_RX_BW_80)
2863 		arg->bw_80 = true;
2864 
2865 	if (link_sta->bandwidth == IEEE80211_STA_RX_BW_160)
2866 		arg->bw_160 = true;
2867 
2868 	if (link_sta->bandwidth == IEEE80211_STA_RX_BW_320)
2869 		arg->bw_320 = true;
2870 
2871 	arg->peer_he_caps_6ghz = le16_to_cpu(link_sta->he_6ghz_capa.capa);
2872 
2873 	mpdu_density = u32_get_bits(arg->peer_he_caps_6ghz,
2874 				    IEEE80211_HE_6GHZ_CAP_MIN_MPDU_START);
2875 	arg->peer_mpdu_density = ath12k_parse_mpdudensity(mpdu_density);
2876 
2877 	/* From IEEE Std 802.11ax-2021 - Section 10.12.2: An HE STA shall be capable of
2878 	 * receiving A-MPDU where the A-MPDU pre-EOF padding length is up to the value
2879 	 * indicated by the Maximum A-MPDU Length Exponent Extension field in the HE
2880 	 * Capabilities element and the Maximum A-MPDU Length Exponent field in HE 6 GHz
2881 	 * Band Capabilities element in the 6 GHz band.
2882 	 *
2883 	 * Here, we are extracting the Max A-MPDU Exponent Extension from HE caps and
2884 	 * factor is the Maximum A-MPDU Length Exponent from HE 6 GHZ Band capability.
2885 	 */
2886 	ampdu_factor = u8_get_bits(he_cap->he_cap_elem.mac_cap_info[3],
2887 				   IEEE80211_HE_MAC_CAP3_MAX_AMPDU_LEN_EXP_MASK) +
2888 			u32_get_bits(arg->peer_he_caps_6ghz,
2889 				     IEEE80211_HE_6GHZ_CAP_MAX_AMPDU_LEN_EXP);
2890 
2891 	arg->peer_max_mpdu = (1u << (IEEE80211_HE_6GHZ_MAX_AMPDU_FACTOR +
2892 				     ampdu_factor)) - 1;
2893 }
2894 
2895 static int ath12k_get_smps_from_capa(const struct ieee80211_sta_ht_cap *ht_cap,
2896 				     const struct ieee80211_he_6ghz_capa *he_6ghz_capa,
2897 				     int *smps)
2898 {
2899 	if (ht_cap->ht_supported)
2900 		*smps = u16_get_bits(ht_cap->cap, IEEE80211_HT_CAP_SM_PS);
2901 	else
2902 		*smps = le16_get_bits(he_6ghz_capa->capa,
2903 				      IEEE80211_HE_6GHZ_CAP_SM_PS);
2904 
2905 	if (*smps >= ARRAY_SIZE(ath12k_smps_map))
2906 		return -EINVAL;
2907 
2908 	return 0;
2909 }
2910 
2911 static void ath12k_peer_assoc_h_smps(struct ath12k_link_sta *arsta,
2912 				     struct ath12k_wmi_peer_assoc_arg *arg)
2913 {
2914 	struct ieee80211_sta *sta = ath12k_ahsta_to_sta(arsta->ahsta);
2915 	const struct ieee80211_he_6ghz_capa *he_6ghz_capa;
2916 	struct ath12k_link_vif *arvif = arsta->arvif;
2917 	const struct ieee80211_sta_ht_cap *ht_cap;
2918 	struct ieee80211_link_sta *link_sta;
2919 	struct ath12k *ar = arvif->ar;
2920 	int smps;
2921 
2922 	link_sta = ath12k_mac_get_link_sta(arsta);
2923 	if (!link_sta) {
2924 		ath12k_warn(ar->ab, "unable to access link sta in peer assoc he for sta %pM link %u\n",
2925 			    sta->addr, arsta->link_id);
2926 		return;
2927 	}
2928 
2929 	he_6ghz_capa = &link_sta->he_6ghz_capa;
2930 	ht_cap = &link_sta->ht_cap;
2931 
2932 	if (!ht_cap->ht_supported && !he_6ghz_capa->capa)
2933 		return;
2934 
2935 	if (ath12k_get_smps_from_capa(ht_cap, he_6ghz_capa, &smps))
2936 		return;
2937 
2938 	switch (smps) {
2939 	case WLAN_HT_CAP_SM_PS_STATIC:
2940 		arg->static_mimops_flag = true;
2941 		break;
2942 	case WLAN_HT_CAP_SM_PS_DYNAMIC:
2943 		arg->dynamic_mimops_flag = true;
2944 		break;
2945 	case WLAN_HT_CAP_SM_PS_DISABLED:
2946 		arg->spatial_mux_flag = true;
2947 		break;
2948 	default:
2949 		break;
2950 	}
2951 }
2952 
2953 static void ath12k_peer_assoc_h_qos(struct ath12k *ar,
2954 				    struct ath12k_link_vif *arvif,
2955 				    struct ath12k_link_sta *arsta,
2956 				    struct ath12k_wmi_peer_assoc_arg *arg)
2957 {
2958 	struct ieee80211_sta *sta = ath12k_ahsta_to_sta(arsta->ahsta);
2959 
2960 	switch (arvif->ahvif->vdev_type) {
2961 	case WMI_VDEV_TYPE_AP:
2962 		if (sta->wme) {
2963 			/* TODO: Check WME vs QoS */
2964 			arg->is_wme_set = true;
2965 			arg->qos_flag = true;
2966 		}
2967 
2968 		if (sta->wme && sta->uapsd_queues) {
2969 			/* TODO: Check WME vs QoS */
2970 			arg->is_wme_set = true;
2971 			arg->apsd_flag = true;
2972 			arg->peer_rate_caps |= WMI_HOST_RC_UAPSD_FLAG;
2973 		}
2974 		break;
2975 	case WMI_VDEV_TYPE_STA:
2976 		if (sta->wme) {
2977 			arg->is_wme_set = true;
2978 			arg->qos_flag = true;
2979 		}
2980 		break;
2981 	default:
2982 		break;
2983 	}
2984 
2985 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac peer %pM qos %d\n",
2986 		   arsta->addr, arg->qos_flag);
2987 }
2988 
2989 static int ath12k_peer_assoc_qos_ap(struct ath12k *ar,
2990 				    struct ath12k_link_vif *arvif,
2991 				    struct ath12k_link_sta *arsta)
2992 {
2993 	struct ieee80211_sta *sta = ath12k_ahsta_to_sta(arsta->ahsta);
2994 	struct ath12k_wmi_ap_ps_arg arg;
2995 	u32 max_sp;
2996 	u32 uapsd;
2997 	int ret;
2998 
2999 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
3000 
3001 	arg.vdev_id = arvif->vdev_id;
3002 
3003 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac uapsd_queues 0x%x max_sp %d\n",
3004 		   sta->uapsd_queues, sta->max_sp);
3005 
3006 	uapsd = 0;
3007 	if (sta->uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_VO)
3008 		uapsd |= WMI_AP_PS_UAPSD_AC3_DELIVERY_EN |
3009 			 WMI_AP_PS_UAPSD_AC3_TRIGGER_EN;
3010 	if (sta->uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_VI)
3011 		uapsd |= WMI_AP_PS_UAPSD_AC2_DELIVERY_EN |
3012 			 WMI_AP_PS_UAPSD_AC2_TRIGGER_EN;
3013 	if (sta->uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_BK)
3014 		uapsd |= WMI_AP_PS_UAPSD_AC1_DELIVERY_EN |
3015 			 WMI_AP_PS_UAPSD_AC1_TRIGGER_EN;
3016 	if (sta->uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_BE)
3017 		uapsd |= WMI_AP_PS_UAPSD_AC0_DELIVERY_EN |
3018 			 WMI_AP_PS_UAPSD_AC0_TRIGGER_EN;
3019 
3020 	max_sp = 0;
3021 	if (sta->max_sp < MAX_WMI_AP_PS_PEER_PARAM_MAX_SP)
3022 		max_sp = sta->max_sp;
3023 
3024 	arg.param = WMI_AP_PS_PEER_PARAM_UAPSD;
3025 	arg.value = uapsd;
3026 	ret = ath12k_wmi_send_set_ap_ps_param_cmd(ar, arsta->addr, &arg);
3027 	if (ret)
3028 		goto err;
3029 
3030 	arg.param = WMI_AP_PS_PEER_PARAM_MAX_SP;
3031 	arg.value = max_sp;
3032 	ret = ath12k_wmi_send_set_ap_ps_param_cmd(ar, arsta->addr, &arg);
3033 	if (ret)
3034 		goto err;
3035 
3036 	/* TODO: revisit during testing */
3037 	arg.param = WMI_AP_PS_PEER_PARAM_SIFS_RESP_FRMTYPE;
3038 	arg.value = DISABLE_SIFS_RESPONSE_TRIGGER;
3039 	ret = ath12k_wmi_send_set_ap_ps_param_cmd(ar, arsta->addr, &arg);
3040 	if (ret)
3041 		goto err;
3042 
3043 	arg.param = WMI_AP_PS_PEER_PARAM_SIFS_RESP_UAPSD;
3044 	arg.value = DISABLE_SIFS_RESPONSE_TRIGGER;
3045 	ret = ath12k_wmi_send_set_ap_ps_param_cmd(ar, arsta->addr, &arg);
3046 	if (ret)
3047 		goto err;
3048 
3049 	return 0;
3050 
3051 err:
3052 	ath12k_warn(ar->ab, "failed to set ap ps peer param %d for vdev %i: %d\n",
3053 		    arg.param, arvif->vdev_id, ret);
3054 	return ret;
3055 }
3056 
3057 static bool ath12k_mac_sta_has_ofdm_only(struct ieee80211_link_sta *sta)
3058 {
3059 	return sta->supp_rates[NL80211_BAND_2GHZ] >>
3060 	       ATH12K_MAC_FIRST_OFDM_RATE_IDX;
3061 }
3062 
3063 static enum wmi_phy_mode ath12k_mac_get_phymode_vht(struct ath12k *ar,
3064 						    struct ieee80211_link_sta *link_sta)
3065 {
3066 	if (link_sta->bandwidth == IEEE80211_STA_RX_BW_160) {
3067 		if (link_sta->vht_cap.cap & (IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_160MHZ |
3068 		    IEEE80211_VHT_CAP_EXT_NSS_BW_MASK))
3069 			return MODE_11AC_VHT160;
3070 
3071 		/* Allow STA to connect even if it does not explicitly advertise 160 MHz
3072 		 * support
3073 		 */
3074 		return MODE_11AC_VHT160;
3075 	}
3076 
3077 	if (link_sta->bandwidth == IEEE80211_STA_RX_BW_80)
3078 		return MODE_11AC_VHT80;
3079 
3080 	if (link_sta->bandwidth == IEEE80211_STA_RX_BW_40)
3081 		return MODE_11AC_VHT40;
3082 
3083 	if (link_sta->bandwidth == IEEE80211_STA_RX_BW_20)
3084 		return MODE_11AC_VHT20;
3085 
3086 	return MODE_UNKNOWN;
3087 }
3088 
3089 static enum wmi_phy_mode ath12k_mac_get_phymode_he(struct ath12k *ar,
3090 						   struct ieee80211_link_sta *link_sta)
3091 {
3092 	if (link_sta->bandwidth == IEEE80211_STA_RX_BW_160) {
3093 		if (link_sta->he_cap.he_cap_elem.phy_cap_info[0] &
3094 		     IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_160MHZ_IN_5G)
3095 			return MODE_11AX_HE160;
3096 
3097 		return MODE_UNKNOWN;
3098 	}
3099 
3100 	if (link_sta->bandwidth == IEEE80211_STA_RX_BW_80)
3101 		return MODE_11AX_HE80;
3102 
3103 	if (link_sta->bandwidth == IEEE80211_STA_RX_BW_40)
3104 		return MODE_11AX_HE40;
3105 
3106 	if (link_sta->bandwidth == IEEE80211_STA_RX_BW_20)
3107 		return MODE_11AX_HE20;
3108 
3109 	return MODE_UNKNOWN;
3110 }
3111 
3112 static enum wmi_phy_mode ath12k_mac_get_phymode_eht(struct ath12k *ar,
3113 						    struct ieee80211_link_sta *link_sta)
3114 {
3115 	if (link_sta->bandwidth == IEEE80211_STA_RX_BW_320)
3116 		if (link_sta->eht_cap.eht_cap_elem.phy_cap_info[0] &
3117 		    IEEE80211_EHT_PHY_CAP0_320MHZ_IN_6GHZ)
3118 			return MODE_11BE_EHT320;
3119 
3120 	if (link_sta->bandwidth == IEEE80211_STA_RX_BW_160) {
3121 		if (link_sta->he_cap.he_cap_elem.phy_cap_info[0] &
3122 		    IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_160MHZ_IN_5G)
3123 			return MODE_11BE_EHT160;
3124 
3125 		ath12k_warn(ar->ab, "invalid EHT PHY capability info for 160 Mhz: %d\n",
3126 			    link_sta->he_cap.he_cap_elem.phy_cap_info[0]);
3127 
3128 		return MODE_UNKNOWN;
3129 	}
3130 
3131 	if (link_sta->bandwidth == IEEE80211_STA_RX_BW_80)
3132 		return MODE_11BE_EHT80;
3133 
3134 	if (link_sta->bandwidth == IEEE80211_STA_RX_BW_40)
3135 		return MODE_11BE_EHT40;
3136 
3137 	if (link_sta->bandwidth == IEEE80211_STA_RX_BW_20)
3138 		return MODE_11BE_EHT20;
3139 
3140 	return MODE_UNKNOWN;
3141 }
3142 
3143 static bool
3144 ath12k_peer_assoc_h_eht_masked(const u16 eht_mcs_mask[NL80211_EHT_NSS_MAX])
3145 {
3146 	int nss;
3147 
3148 	for (nss = 0; nss < NL80211_EHT_NSS_MAX; nss++)
3149 		if (eht_mcs_mask[nss])
3150 			return false;
3151 
3152 	return true;
3153 }
3154 
3155 static void ath12k_peer_assoc_h_phymode(struct ath12k *ar,
3156 					struct ath12k_link_vif *arvif,
3157 					struct ath12k_link_sta *arsta,
3158 					struct ath12k_wmi_peer_assoc_arg *arg)
3159 {
3160 	struct ieee80211_link_sta *link_sta;
3161 	struct cfg80211_chan_def def;
3162 	enum nl80211_band band;
3163 	const u8 *ht_mcs_mask;
3164 	const u16 *vht_mcs_mask;
3165 	const u16 *he_mcs_mask;
3166 	const u16 *eht_mcs_mask;
3167 	enum wmi_phy_mode phymode = MODE_UNKNOWN;
3168 
3169 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
3170 
3171 	struct ieee80211_vif *vif = ath12k_ahvif_to_vif(arvif->ahvif);
3172 	struct ieee80211_sta *sta = ath12k_ahsta_to_sta(arsta->ahsta);
3173 
3174 	if (WARN_ON(ath12k_mac_vif_link_chan(vif, arvif->link_id, &def)))
3175 		return;
3176 
3177 	band = def.chan->band;
3178 	ht_mcs_mask = arvif->bitrate_mask.control[band].ht_mcs;
3179 	vht_mcs_mask = arvif->bitrate_mask.control[band].vht_mcs;
3180 	he_mcs_mask = arvif->bitrate_mask.control[band].he_mcs;
3181 	eht_mcs_mask = arvif->bitrate_mask.control[band].eht_mcs;
3182 
3183 	link_sta = ath12k_mac_get_link_sta(arsta);
3184 	if (!link_sta) {
3185 		ath12k_warn(ar->ab, "unable to access link sta in peer assoc he for sta %pM link %u\n",
3186 			    sta->addr, arsta->link_id);
3187 		return;
3188 	}
3189 
3190 	switch (band) {
3191 	case NL80211_BAND_2GHZ:
3192 		if (link_sta->eht_cap.has_eht &&
3193 		    !ath12k_peer_assoc_h_eht_masked(eht_mcs_mask)) {
3194 			if (link_sta->bandwidth == IEEE80211_STA_RX_BW_40)
3195 				phymode = MODE_11BE_EHT40_2G;
3196 			else
3197 				phymode = MODE_11BE_EHT20_2G;
3198 		} else if (link_sta->he_cap.has_he &&
3199 			   !ath12k_peer_assoc_h_he_masked(he_mcs_mask)) {
3200 			if (link_sta->bandwidth == IEEE80211_STA_RX_BW_80)
3201 				phymode = MODE_11AX_HE80_2G;
3202 			else if (link_sta->bandwidth == IEEE80211_STA_RX_BW_40)
3203 				phymode = MODE_11AX_HE40_2G;
3204 			else
3205 				phymode = MODE_11AX_HE20_2G;
3206 		} else if (link_sta->vht_cap.vht_supported &&
3207 		    !ath12k_peer_assoc_h_vht_masked(vht_mcs_mask)) {
3208 			if (link_sta->bandwidth == IEEE80211_STA_RX_BW_40)
3209 				phymode = MODE_11AC_VHT40;
3210 			else
3211 				phymode = MODE_11AC_VHT20;
3212 		} else if (link_sta->ht_cap.ht_supported &&
3213 			   !ath12k_peer_assoc_h_ht_masked(ht_mcs_mask)) {
3214 			if (link_sta->bandwidth == IEEE80211_STA_RX_BW_40)
3215 				phymode = MODE_11NG_HT40;
3216 			else
3217 				phymode = MODE_11NG_HT20;
3218 		} else if (ath12k_mac_sta_has_ofdm_only(link_sta)) {
3219 			phymode = MODE_11G;
3220 		} else {
3221 			phymode = MODE_11B;
3222 		}
3223 		break;
3224 	case NL80211_BAND_5GHZ:
3225 	case NL80211_BAND_6GHZ:
3226 		/* Check EHT first */
3227 		if (link_sta->eht_cap.has_eht) {
3228 			phymode = ath12k_mac_get_phymode_eht(ar, link_sta);
3229 		} else if (link_sta->he_cap.has_he &&
3230 			   !ath12k_peer_assoc_h_he_masked(he_mcs_mask)) {
3231 			phymode = ath12k_mac_get_phymode_he(ar, link_sta);
3232 		} else if (link_sta->vht_cap.vht_supported &&
3233 		    !ath12k_peer_assoc_h_vht_masked(vht_mcs_mask)) {
3234 			phymode = ath12k_mac_get_phymode_vht(ar, link_sta);
3235 		} else if (link_sta->ht_cap.ht_supported &&
3236 			   !ath12k_peer_assoc_h_ht_masked(ht_mcs_mask)) {
3237 			if (link_sta->bandwidth >= IEEE80211_STA_RX_BW_40)
3238 				phymode = MODE_11NA_HT40;
3239 			else
3240 				phymode = MODE_11NA_HT20;
3241 		} else {
3242 			phymode = MODE_11A;
3243 		}
3244 		break;
3245 	default:
3246 		break;
3247 	}
3248 
3249 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac peer %pM phymode %s\n",
3250 		   arsta->addr, ath12k_mac_phymode_str(phymode));
3251 
3252 	arg->peer_phymode = phymode;
3253 	WARN_ON(phymode == MODE_UNKNOWN);
3254 }
3255 
3256 #define ATH12K_EHT_MCS_7_ENABLED	0x00FF
3257 #define ATH12K_EHT_MCS_9_ENABLED	0x0300
3258 #define ATH12K_EHT_MCS_11_ENABLED	0x0C00
3259 #define ATH12K_EHT_MCS_13_ENABLED	0x3000
3260 
3261 static void ath12k_mac_set_eht_mcs(u8 rx_tx_mcs7, u8 rx_tx_mcs9,
3262 				   u8 rx_tx_mcs11, u8 rx_tx_mcs13,
3263 				   u32 *rx_mcs, u32 *tx_mcs,
3264 				   const u16 eht_mcs_limit[NL80211_EHT_NSS_MAX])
3265 {
3266 	int nss;
3267 	u8 mcs_7 = 0, mcs_9 = 0, mcs_11 = 0, mcs_13 = 0;
3268 	u8 peer_mcs_7, peer_mcs_9, peer_mcs_11, peer_mcs_13;
3269 
3270 	for (nss = 0; nss < NL80211_EHT_NSS_MAX; nss++) {
3271 		if (eht_mcs_limit[nss] & ATH12K_EHT_MCS_7_ENABLED)
3272 			mcs_7++;
3273 		if (eht_mcs_limit[nss] & ATH12K_EHT_MCS_9_ENABLED)
3274 			mcs_9++;
3275 		if (eht_mcs_limit[nss] & ATH12K_EHT_MCS_11_ENABLED)
3276 			mcs_11++;
3277 		if (eht_mcs_limit[nss] & ATH12K_EHT_MCS_13_ENABLED)
3278 			mcs_13++;
3279 	}
3280 
3281 	peer_mcs_7 = u8_get_bits(rx_tx_mcs7, IEEE80211_EHT_MCS_NSS_RX);
3282 	peer_mcs_9 = u8_get_bits(rx_tx_mcs9, IEEE80211_EHT_MCS_NSS_RX);
3283 	peer_mcs_11 = u8_get_bits(rx_tx_mcs11, IEEE80211_EHT_MCS_NSS_RX);
3284 	peer_mcs_13 = u8_get_bits(rx_tx_mcs13, IEEE80211_EHT_MCS_NSS_RX);
3285 
3286 	*rx_mcs = u32_encode_bits(min(peer_mcs_7, mcs_7), WMI_EHT_MCS_NSS_0_7) |
3287 		  u32_encode_bits(min(peer_mcs_9, mcs_9), WMI_EHT_MCS_NSS_8_9) |
3288 		  u32_encode_bits(min(peer_mcs_11, mcs_11), WMI_EHT_MCS_NSS_10_11) |
3289 		  u32_encode_bits(min(peer_mcs_13, mcs_13), WMI_EHT_MCS_NSS_12_13);
3290 
3291 	peer_mcs_7 = u8_get_bits(rx_tx_mcs7, IEEE80211_EHT_MCS_NSS_TX);
3292 	peer_mcs_9 = u8_get_bits(rx_tx_mcs9, IEEE80211_EHT_MCS_NSS_TX);
3293 	peer_mcs_11 = u8_get_bits(rx_tx_mcs11, IEEE80211_EHT_MCS_NSS_TX);
3294 	peer_mcs_13 = u8_get_bits(rx_tx_mcs13, IEEE80211_EHT_MCS_NSS_TX);
3295 
3296 	*tx_mcs = u32_encode_bits(min(peer_mcs_7, mcs_7), WMI_EHT_MCS_NSS_0_7) |
3297 		  u32_encode_bits(min(peer_mcs_9, mcs_9), WMI_EHT_MCS_NSS_8_9) |
3298 		  u32_encode_bits(min(peer_mcs_11, mcs_11), WMI_EHT_MCS_NSS_10_11) |
3299 		  u32_encode_bits(min(peer_mcs_13, mcs_13), WMI_EHT_MCS_NSS_12_13);
3300 }
3301 
3302 static void ath12k_mac_set_eht_ppe_threshold(const u8 *ppe_thres,
3303 					     struct ath12k_wmi_ppe_threshold_arg *ppet)
3304 {
3305 	u32 bit_pos = IEEE80211_EHT_PPE_THRES_INFO_HEADER_SIZE, val;
3306 	u8 nss, ru, i;
3307 	u8 ppet_bit_len_per_ru = IEEE80211_EHT_PPE_THRES_INFO_PPET_SIZE * 2;
3308 
3309 	ppet->numss_m1 = u8_get_bits(ppe_thres[0], IEEE80211_EHT_PPE_THRES_NSS_MASK);
3310 	ppet->ru_bit_mask = u16_get_bits(get_unaligned_le16(ppe_thres),
3311 					 IEEE80211_EHT_PPE_THRES_RU_INDEX_BITMASK_MASK);
3312 
3313 	for (nss = 0; nss <= ppet->numss_m1; nss++) {
3314 		for (ru = 0;
3315 		     ru < hweight16(IEEE80211_EHT_PPE_THRES_RU_INDEX_BITMASK_MASK);
3316 		     ru++) {
3317 			if ((ppet->ru_bit_mask & BIT(ru)) == 0)
3318 				continue;
3319 
3320 			val = 0;
3321 			for (i = 0; i < ppet_bit_len_per_ru; i++) {
3322 				val |= (((ppe_thres[bit_pos / 8] >>
3323 					  (bit_pos % 8)) & 0x1) << i);
3324 				bit_pos++;
3325 			}
3326 			ppet->ppet16_ppet8_ru3_ru0[nss] |=
3327 					(val << (ru * ppet_bit_len_per_ru));
3328 		}
3329 	}
3330 }
3331 
3332 static void ath12k_peer_assoc_h_eht(struct ath12k *ar,
3333 				    struct ath12k_link_vif *arvif,
3334 				    struct ath12k_link_sta *arsta,
3335 				    struct ath12k_wmi_peer_assoc_arg *arg)
3336 {
3337 	struct ieee80211_sta *sta = ath12k_ahsta_to_sta(arsta->ahsta);
3338 	struct ieee80211_vif *vif = ath12k_ahvif_to_vif(arvif->ahvif);
3339 	const struct ieee80211_eht_mcs_nss_supp *own_eht_mcs_nss_supp;
3340 	const struct ieee80211_eht_mcs_nss_supp_20mhz_only *bw_20;
3341 	const struct ieee80211_sta_eht_cap *eht_cap, *own_eht_cap;
3342 	const struct ieee80211_sband_iftype_data *iftd;
3343 	const struct ieee80211_eht_mcs_nss_supp_bw *bw;
3344 	const struct ieee80211_sta_he_cap *he_cap;
3345 	struct ieee80211_link_sta *link_sta;
3346 	struct ieee80211_bss_conf *link_conf;
3347 	struct cfg80211_chan_def def;
3348 	bool user_rate_valid = true;
3349 	enum nl80211_band band;
3350 	int eht_nss, nss_idx;
3351 	u32 *rx_mcs, *tx_mcs;
3352 	u16 *eht_mcs_mask;
3353 	u8 max_nss = 0;
3354 
3355 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
3356 
3357 	link_sta = ath12k_mac_get_link_sta(arsta);
3358 	if (!link_sta) {
3359 		ath12k_warn(ar->ab, "unable to access link sta in peer assoc eht for sta %pM link %u\n",
3360 			    sta->addr, arsta->link_id);
3361 		return;
3362 	}
3363 
3364 	link_conf = ath12k_mac_get_link_bss_conf(arvif);
3365 	if (!link_conf) {
3366 		ath12k_warn(ar->ab, "unable to access link_conf in peer assoc eht set\n");
3367 		return;
3368 	}
3369 
3370 	eht_cap = &link_sta->eht_cap;
3371 	he_cap = &link_sta->he_cap;
3372 	if (!he_cap->has_he || !eht_cap->has_eht)
3373 		return;
3374 
3375 	if (WARN_ON(ath12k_mac_vif_link_chan(vif, arvif->link_id, &def)))
3376 		return;
3377 
3378 	band = def.chan->band;
3379 	eht_mcs_mask = arvif->bitrate_mask.control[band].eht_mcs;
3380 
3381 	iftd = ieee80211_get_sband_iftype_data(&ar->mac.sbands[band], vif->type);
3382 	if (!iftd) {
3383 		ath12k_warn(ar->ab,
3384 			    "unable to access iftype_data in struct ieee80211_supported_band\n");
3385 		return;
3386 	}
3387 
3388 	own_eht_cap = &iftd->eht_cap;
3389 	own_eht_mcs_nss_supp = &own_eht_cap->eht_mcs_nss_supp;
3390 
3391 	arg->eht_flag = true;
3392 
3393 	if ((eht_cap->eht_cap_elem.phy_cap_info[5] &
3394 	     IEEE80211_EHT_PHY_CAP5_PPE_THRESHOLD_PRESENT) &&
3395 	    eht_cap->eht_ppe_thres[0] != 0)
3396 		ath12k_mac_set_eht_ppe_threshold(eht_cap->eht_ppe_thres,
3397 						 &arg->peer_eht_ppet);
3398 
3399 	memcpy(arg->peer_eht_cap_mac, eht_cap->eht_cap_elem.mac_cap_info,
3400 	       sizeof(eht_cap->eht_cap_elem.mac_cap_info));
3401 	memcpy(arg->peer_eht_cap_phy, eht_cap->eht_cap_elem.phy_cap_info,
3402 	       sizeof(eht_cap->eht_cap_elem.phy_cap_info));
3403 
3404 	rx_mcs = arg->peer_eht_rx_mcs_set;
3405 	tx_mcs = arg->peer_eht_tx_mcs_set;
3406 
3407 	eht_nss = ath12k_mac_max_eht_mcs_nss((void *)own_eht_mcs_nss_supp,
3408 					     sizeof(*own_eht_mcs_nss_supp));
3409 	if (eht_nss > link_sta->rx_nss) {
3410 		user_rate_valid = false;
3411 		for (nss_idx = (link_sta->rx_nss - 1); nss_idx >= 0; nss_idx--) {
3412 			if (eht_mcs_mask[nss_idx]) {
3413 				user_rate_valid = true;
3414 				break;
3415 			}
3416 		}
3417 	}
3418 
3419 	if (!user_rate_valid) {
3420 		ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
3421 			   "Setting eht range MCS value to peer supported nss %d for peer %pM\n",
3422 			   link_sta->rx_nss, arsta->addr);
3423 		eht_mcs_mask[link_sta->rx_nss - 1] = eht_mcs_mask[eht_nss - 1];
3424 	}
3425 
3426 	bw_20 = &eht_cap->eht_mcs_nss_supp.only_20mhz;
3427 	bw = &eht_cap->eht_mcs_nss_supp.bw._80;
3428 
3429 	switch (link_sta->bandwidth) {
3430 	case IEEE80211_STA_RX_BW_320:
3431 		bw = &eht_cap->eht_mcs_nss_supp.bw._320;
3432 		ath12k_mac_set_eht_mcs(bw->rx_tx_mcs9_max_nss,
3433 				       bw->rx_tx_mcs9_max_nss,
3434 				       bw->rx_tx_mcs11_max_nss,
3435 				       bw->rx_tx_mcs13_max_nss,
3436 				       &rx_mcs[WMI_EHTCAP_TXRX_MCS_NSS_IDX_320],
3437 				       &tx_mcs[WMI_EHTCAP_TXRX_MCS_NSS_IDX_320],
3438 				       eht_mcs_mask);
3439 		arg->peer_eht_mcs_count++;
3440 		fallthrough;
3441 	case IEEE80211_STA_RX_BW_160:
3442 		bw = &eht_cap->eht_mcs_nss_supp.bw._160;
3443 		ath12k_mac_set_eht_mcs(bw->rx_tx_mcs9_max_nss,
3444 				       bw->rx_tx_mcs9_max_nss,
3445 				       bw->rx_tx_mcs11_max_nss,
3446 				       bw->rx_tx_mcs13_max_nss,
3447 				       &rx_mcs[WMI_EHTCAP_TXRX_MCS_NSS_IDX_160],
3448 				       &tx_mcs[WMI_EHTCAP_TXRX_MCS_NSS_IDX_160],
3449 				       eht_mcs_mask);
3450 		arg->peer_eht_mcs_count++;
3451 		fallthrough;
3452 	default:
3453 		if ((vif->type == NL80211_IFTYPE_AP ||
3454 		     vif->type == NL80211_IFTYPE_MESH_POINT) &&
3455 		    !(link_sta->he_cap.he_cap_elem.phy_cap_info[0] &
3456 		      IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_MASK_ALL)) {
3457 			bw_20 = &eht_cap->eht_mcs_nss_supp.only_20mhz;
3458 
3459 			ath12k_mac_set_eht_mcs(bw_20->rx_tx_mcs7_max_nss,
3460 					       bw_20->rx_tx_mcs9_max_nss,
3461 					       bw_20->rx_tx_mcs11_max_nss,
3462 					       bw_20->rx_tx_mcs13_max_nss,
3463 					       &rx_mcs[WMI_EHTCAP_TXRX_MCS_NSS_IDX_80],
3464 					       &tx_mcs[WMI_EHTCAP_TXRX_MCS_NSS_IDX_80],
3465 					       eht_mcs_mask);
3466 		} else {
3467 			bw = &eht_cap->eht_mcs_nss_supp.bw._80;
3468 			ath12k_mac_set_eht_mcs(bw->rx_tx_mcs9_max_nss,
3469 					       bw->rx_tx_mcs9_max_nss,
3470 					       bw->rx_tx_mcs11_max_nss,
3471 					       bw->rx_tx_mcs13_max_nss,
3472 					       &rx_mcs[WMI_EHTCAP_TXRX_MCS_NSS_IDX_80],
3473 					       &tx_mcs[WMI_EHTCAP_TXRX_MCS_NSS_IDX_80],
3474 					       eht_mcs_mask);
3475 		}
3476 
3477 		arg->peer_eht_mcs_count++;
3478 		break;
3479 	}
3480 
3481 	arg->punct_bitmap = ~arvif->punct_bitmap;
3482 	arg->eht_disable_mcs15 = link_conf->eht_disable_mcs15;
3483 
3484 	if ((vif->type == NL80211_IFTYPE_AP ||
3485 	     vif->type == NL80211_IFTYPE_MESH_POINT) &&
3486 	    !(link_sta->he_cap.he_cap_elem.phy_cap_info[0] &
3487 	      IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_MASK_ALL)) {
3488 		if (bw_20->rx_tx_mcs13_max_nss)
3489 			max_nss = max(max_nss, u8_get_bits(bw_20->rx_tx_mcs13_max_nss,
3490 							   IEEE80211_EHT_MCS_NSS_RX));
3491 		if (bw_20->rx_tx_mcs11_max_nss)
3492 			max_nss = max(max_nss, u8_get_bits(bw_20->rx_tx_mcs11_max_nss,
3493 							   IEEE80211_EHT_MCS_NSS_RX));
3494 		if (bw_20->rx_tx_mcs9_max_nss)
3495 			max_nss = max(max_nss, u8_get_bits(bw_20->rx_tx_mcs9_max_nss,
3496 							   IEEE80211_EHT_MCS_NSS_RX));
3497 		if (bw_20->rx_tx_mcs7_max_nss)
3498 			max_nss = max(max_nss, u8_get_bits(bw_20->rx_tx_mcs7_max_nss,
3499 							   IEEE80211_EHT_MCS_NSS_RX));
3500 	} else {
3501 		if (bw->rx_tx_mcs13_max_nss)
3502 			max_nss = max(max_nss, u8_get_bits(bw->rx_tx_mcs13_max_nss,
3503 							   IEEE80211_EHT_MCS_NSS_RX));
3504 		if (bw->rx_tx_mcs11_max_nss)
3505 			max_nss = max(max_nss, u8_get_bits(bw->rx_tx_mcs11_max_nss,
3506 							   IEEE80211_EHT_MCS_NSS_RX));
3507 		if (bw->rx_tx_mcs9_max_nss)
3508 			max_nss = max(max_nss, u8_get_bits(bw->rx_tx_mcs9_max_nss,
3509 							   IEEE80211_EHT_MCS_NSS_RX));
3510 	}
3511 
3512 	max_nss = min(max_nss, (uint8_t)eht_nss);
3513 
3514 	arg->peer_nss = min(link_sta->rx_nss, max_nss);
3515 
3516 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
3517 		   "mac eht peer %pM nss %d mcs cnt %d ru_punct_bitmap 0x%x\n",
3518 		   arsta->addr, arg->peer_nss, arg->peer_eht_mcs_count,
3519 		   arg->punct_bitmap);
3520 }
3521 
3522 static void ath12k_peer_assoc_h_mlo(struct ath12k_link_sta *arsta,
3523 				    struct ath12k_wmi_peer_assoc_arg *arg)
3524 {
3525 	struct ieee80211_sta *sta = ath12k_ahsta_to_sta(arsta->ahsta);
3526 	struct peer_assoc_mlo_params *ml = &arg->ml;
3527 	struct ath12k_sta *ahsta = arsta->ahsta;
3528 	struct ath12k_link_sta *arsta_p;
3529 	struct ath12k_link_vif *arvif;
3530 	unsigned long links;
3531 	u8 link_id;
3532 	int i;
3533 
3534 	if (!sta->mlo || ahsta->ml_peer_id == ATH12K_MLO_PEER_ID_INVALID)
3535 		return;
3536 
3537 	ml->enabled = true;
3538 	ml->assoc_link = arsta->is_assoc_link;
3539 
3540 	/* For now considering the primary umac based on assoc link */
3541 	ml->primary_umac = arsta->is_assoc_link;
3542 	ml->peer_id_valid = true;
3543 	ml->logical_link_idx_valid = true;
3544 
3545 	ether_addr_copy(ml->mld_addr, sta->addr);
3546 	ml->logical_link_idx = arsta->link_idx;
3547 	ml->ml_peer_id = ahsta->ml_peer_id;
3548 	ml->ieee_link_id = arsta->link_id;
3549 	ml->num_partner_links = 0;
3550 	ml->eml_cap = sta->eml_cap;
3551 	links = ahsta->links_map;
3552 
3553 	rcu_read_lock();
3554 
3555 	i = 0;
3556 
3557 	for_each_set_bit(link_id, &links, IEEE80211_MLD_MAX_NUM_LINKS) {
3558 		if (i >= ATH12K_WMI_MLO_MAX_LINKS)
3559 			break;
3560 
3561 		arsta_p = rcu_dereference(ahsta->link[link_id]);
3562 		arvif = rcu_dereference(ahsta->ahvif->link[link_id]);
3563 
3564 		if (arsta_p == arsta)
3565 			continue;
3566 
3567 		if (!arvif->is_started)
3568 			continue;
3569 
3570 		ml->partner_info[i].vdev_id = arvif->vdev_id;
3571 		ml->partner_info[i].hw_link_id = arvif->ar->pdev->hw_link_id;
3572 		ml->partner_info[i].assoc_link = arsta_p->is_assoc_link;
3573 		ml->partner_info[i].primary_umac = arsta_p->is_assoc_link;
3574 		ml->partner_info[i].logical_link_idx_valid = true;
3575 		ml->partner_info[i].logical_link_idx = arsta_p->link_idx;
3576 		ml->num_partner_links++;
3577 
3578 		i++;
3579 	}
3580 
3581 	rcu_read_unlock();
3582 }
3583 
3584 static void ath12k_peer_assoc_prepare(struct ath12k *ar,
3585 				      struct ath12k_link_vif *arvif,
3586 				      struct ath12k_link_sta *arsta,
3587 				      struct ath12k_wmi_peer_assoc_arg *arg,
3588 				      bool reassoc)
3589 {
3590 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
3591 
3592 	memset(arg, 0, sizeof(*arg));
3593 
3594 	reinit_completion(&ar->peer_assoc_done);
3595 
3596 	arg->peer_new_assoc = !reassoc;
3597 	ath12k_peer_assoc_h_basic(ar, arvif, arsta, arg);
3598 	ath12k_peer_assoc_h_crypto(ar, arvif, arsta, arg);
3599 	ath12k_peer_assoc_h_rates(ar, arvif, arsta, arg);
3600 	ath12k_peer_assoc_h_ht(ar, arvif, arsta, arg);
3601 	ath12k_peer_assoc_h_vht(ar, arvif, arsta, arg);
3602 	ath12k_peer_assoc_h_he(ar, arvif, arsta, arg);
3603 	ath12k_peer_assoc_h_he_6ghz(ar, arvif, arsta, arg);
3604 	ath12k_peer_assoc_h_eht(ar, arvif, arsta, arg);
3605 	ath12k_peer_assoc_h_qos(ar, arvif, arsta, arg);
3606 	ath12k_peer_assoc_h_phymode(ar, arvif, arsta, arg);
3607 	ath12k_peer_assoc_h_smps(arsta, arg);
3608 	ath12k_peer_assoc_h_mlo(arsta, arg);
3609 
3610 	arsta->peer_nss = arg->peer_nss;
3611 	/* TODO: amsdu_disable req? */
3612 }
3613 
3614 static int ath12k_setup_peer_smps(struct ath12k *ar, struct ath12k_link_vif *arvif,
3615 				  const u8 *addr,
3616 				  const struct ieee80211_sta_ht_cap *ht_cap,
3617 				  const struct ieee80211_he_6ghz_capa *he_6ghz_capa)
3618 {
3619 	int smps, ret = 0;
3620 
3621 	if (!ht_cap->ht_supported && !he_6ghz_capa)
3622 		return 0;
3623 
3624 	ret = ath12k_get_smps_from_capa(ht_cap, he_6ghz_capa, &smps);
3625 	if (ret < 0)
3626 		return ret;
3627 
3628 	return ath12k_wmi_set_peer_param(ar, addr, arvif->vdev_id,
3629 					 WMI_PEER_MIMO_PS_STATE,
3630 					 ath12k_smps_map[smps]);
3631 }
3632 
3633 static int ath12k_mac_set_he_txbf_conf(struct ath12k_link_vif *arvif)
3634 {
3635 	struct ath12k_vif *ahvif = arvif->ahvif;
3636 	struct ath12k *ar = arvif->ar;
3637 	u32 param = WMI_VDEV_PARAM_SET_HEMU_MODE;
3638 	u32 value = 0;
3639 	int ret;
3640 	struct ieee80211_bss_conf *link_conf;
3641 
3642 	link_conf = ath12k_mac_get_link_bss_conf(arvif);
3643 	if (!link_conf) {
3644 		ath12k_warn(ar->ab, "unable to access bss link conf in txbf conf\n");
3645 		return -EINVAL;
3646 	}
3647 
3648 	if (!link_conf->he_support)
3649 		return 0;
3650 
3651 	if (link_conf->he_su_beamformer) {
3652 		value |= u32_encode_bits(HE_SU_BFER_ENABLE, HE_MODE_SU_TX_BFER);
3653 		if (link_conf->he_mu_beamformer &&
3654 		    ahvif->vdev_type == WMI_VDEV_TYPE_AP)
3655 			value |= u32_encode_bits(HE_MU_BFER_ENABLE, HE_MODE_MU_TX_BFER);
3656 	}
3657 
3658 	if (ahvif->vif->type != NL80211_IFTYPE_MESH_POINT) {
3659 		value |= u32_encode_bits(HE_DL_MUOFDMA_ENABLE, HE_MODE_DL_OFDMA) |
3660 			 u32_encode_bits(HE_UL_MUOFDMA_ENABLE, HE_MODE_UL_OFDMA);
3661 
3662 		if (link_conf->he_full_ul_mumimo)
3663 			value |= u32_encode_bits(HE_UL_MUMIMO_ENABLE, HE_MODE_UL_MUMIMO);
3664 
3665 		if (link_conf->he_su_beamformee)
3666 			value |= u32_encode_bits(HE_SU_BFEE_ENABLE, HE_MODE_SU_TX_BFEE);
3667 	}
3668 
3669 	ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id, param, value);
3670 	if (ret) {
3671 		ath12k_warn(ar->ab, "failed to set vdev %d HE MU mode: %d\n",
3672 			    arvif->vdev_id, ret);
3673 		return ret;
3674 	}
3675 
3676 	param = WMI_VDEV_PARAM_SET_HE_SOUNDING_MODE;
3677 	value =	u32_encode_bits(HE_VHT_SOUNDING_MODE_ENABLE, HE_VHT_SOUNDING_MODE) |
3678 		u32_encode_bits(HE_TRIG_NONTRIG_SOUNDING_MODE_ENABLE,
3679 				HE_TRIG_NONTRIG_SOUNDING_MODE);
3680 	ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
3681 					    param, value);
3682 	if (ret) {
3683 		ath12k_warn(ar->ab, "failed to set vdev %d sounding mode: %d\n",
3684 			    arvif->vdev_id, ret);
3685 		return ret;
3686 	}
3687 
3688 	return 0;
3689 }
3690 
3691 static int ath12k_mac_vif_recalc_sta_he_txbf(struct ath12k *ar,
3692 					     struct ath12k_link_vif *arvif,
3693 					     struct ieee80211_sta_he_cap *he_cap,
3694 					     int *hemode)
3695 {
3696 	struct ieee80211_vif *vif = arvif->ahvif->vif;
3697 	struct ieee80211_he_cap_elem he_cap_elem = {};
3698 	struct ieee80211_sta_he_cap *cap_band;
3699 	struct cfg80211_chan_def def;
3700 	u8 link_id = arvif->link_id;
3701 	struct ieee80211_bss_conf *link_conf;
3702 
3703 	link_conf = ath12k_mac_get_link_bss_conf(arvif);
3704 	if (!link_conf) {
3705 		ath12k_warn(ar->ab, "unable to access bss link conf in recalc txbf conf\n");
3706 		return -EINVAL;
3707 	}
3708 
3709 	if (!link_conf->he_support)
3710 		return 0;
3711 
3712 	if (vif->type != NL80211_IFTYPE_STATION)
3713 		return -EINVAL;
3714 
3715 	if (WARN_ON(ath12k_mac_vif_link_chan(vif, link_id, &def)))
3716 		return -EINVAL;
3717 
3718 	if (def.chan->band == NL80211_BAND_2GHZ)
3719 		cap_band = &ar->mac.iftype[NL80211_BAND_2GHZ][vif->type].he_cap;
3720 	else
3721 		cap_band = &ar->mac.iftype[NL80211_BAND_5GHZ][vif->type].he_cap;
3722 
3723 	memcpy(&he_cap_elem, &cap_band->he_cap_elem, sizeof(he_cap_elem));
3724 
3725 	*hemode = 0;
3726 	if (HECAP_PHY_SUBFME_GET(he_cap_elem.phy_cap_info)) {
3727 		if (HECAP_PHY_SUBFMR_GET(he_cap->he_cap_elem.phy_cap_info))
3728 			*hemode |= u32_encode_bits(HE_SU_BFEE_ENABLE, HE_MODE_SU_TX_BFEE);
3729 		if (HECAP_PHY_MUBFMR_GET(he_cap->he_cap_elem.phy_cap_info))
3730 			*hemode |= u32_encode_bits(HE_MU_BFEE_ENABLE, HE_MODE_MU_TX_BFEE);
3731 	}
3732 
3733 	if (vif->type != NL80211_IFTYPE_MESH_POINT) {
3734 		*hemode |= u32_encode_bits(HE_DL_MUOFDMA_ENABLE, HE_MODE_DL_OFDMA) |
3735 			  u32_encode_bits(HE_UL_MUOFDMA_ENABLE, HE_MODE_UL_OFDMA);
3736 
3737 		if (HECAP_PHY_ULMUMIMO_GET(he_cap_elem.phy_cap_info))
3738 			if (HECAP_PHY_ULMUMIMO_GET(he_cap->he_cap_elem.phy_cap_info))
3739 				*hemode |= u32_encode_bits(HE_UL_MUMIMO_ENABLE,
3740 							  HE_MODE_UL_MUMIMO);
3741 
3742 		if (u32_get_bits(*hemode, HE_MODE_MU_TX_BFEE))
3743 			*hemode |= u32_encode_bits(HE_SU_BFEE_ENABLE, HE_MODE_SU_TX_BFEE);
3744 
3745 		if (u32_get_bits(*hemode, HE_MODE_MU_TX_BFER))
3746 			*hemode |= u32_encode_bits(HE_SU_BFER_ENABLE, HE_MODE_SU_TX_BFER);
3747 	}
3748 
3749 	return 0;
3750 }
3751 
3752 static int ath12k_mac_set_eht_txbf_conf(struct ath12k_link_vif *arvif)
3753 {
3754 	struct ath12k_vif *ahvif = arvif->ahvif;
3755 	struct ath12k *ar = arvif->ar;
3756 	u32 param = WMI_VDEV_PARAM_SET_EHT_MU_MODE;
3757 	u32 value = 0;
3758 	int ret;
3759 	struct ieee80211_bss_conf *link_conf;
3760 
3761 	link_conf = ath12k_mac_get_link_bss_conf(arvif);
3762 	if (!link_conf) {
3763 		ath12k_warn(ar->ab, "unable to access bss link conf in eht txbf conf\n");
3764 		return -ENOENT;
3765 	}
3766 
3767 	if (!link_conf->eht_support)
3768 		return 0;
3769 
3770 	if (link_conf->eht_su_beamformer) {
3771 		value |= u32_encode_bits(EHT_SU_BFER_ENABLE, EHT_MODE_SU_TX_BFER);
3772 		if (link_conf->eht_mu_beamformer &&
3773 		    ahvif->vdev_type == WMI_VDEV_TYPE_AP)
3774 			value |= u32_encode_bits(EHT_MU_BFER_ENABLE,
3775 						 EHT_MODE_MU_TX_BFER) |
3776 				 u32_encode_bits(EHT_DL_MUOFDMA_ENABLE,
3777 						 EHT_MODE_DL_OFDMA_MUMIMO) |
3778 				 u32_encode_bits(EHT_UL_MUOFDMA_ENABLE,
3779 						 EHT_MODE_UL_OFDMA_MUMIMO);
3780 	}
3781 
3782 	if (ahvif->vif->type != NL80211_IFTYPE_MESH_POINT) {
3783 		value |= u32_encode_bits(EHT_DL_MUOFDMA_ENABLE, EHT_MODE_DL_OFDMA) |
3784 			 u32_encode_bits(EHT_UL_MUOFDMA_ENABLE, EHT_MODE_UL_OFDMA);
3785 
3786 		if (link_conf->eht_80mhz_full_bw_ul_mumimo)
3787 			value |= u32_encode_bits(EHT_UL_MUMIMO_ENABLE, EHT_MODE_MUMIMO);
3788 
3789 		if (link_conf->eht_su_beamformee)
3790 			value |= u32_encode_bits(EHT_SU_BFEE_ENABLE,
3791 						 EHT_MODE_SU_TX_BFEE);
3792 	}
3793 
3794 	ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id, param, value);
3795 	if (ret) {
3796 		ath12k_warn(ar->ab, "failed to set vdev %d EHT MU mode: %d\n",
3797 			    arvif->vdev_id, ret);
3798 		return ret;
3799 	}
3800 
3801 	return 0;
3802 }
3803 
3804 static u32 ath12k_mac_ieee80211_sta_bw_to_wmi(struct ath12k *ar,
3805 					      struct ieee80211_link_sta *link_sta)
3806 {
3807 	u32 bw;
3808 
3809 	switch (link_sta->bandwidth) {
3810 	case IEEE80211_STA_RX_BW_20:
3811 		bw = WMI_PEER_CHWIDTH_20MHZ;
3812 		break;
3813 	case IEEE80211_STA_RX_BW_40:
3814 		bw = WMI_PEER_CHWIDTH_40MHZ;
3815 		break;
3816 	case IEEE80211_STA_RX_BW_80:
3817 		bw = WMI_PEER_CHWIDTH_80MHZ;
3818 		break;
3819 	case IEEE80211_STA_RX_BW_160:
3820 		bw = WMI_PEER_CHWIDTH_160MHZ;
3821 		break;
3822 	case IEEE80211_STA_RX_BW_320:
3823 		bw = WMI_PEER_CHWIDTH_320MHZ;
3824 		break;
3825 	default:
3826 		ath12k_warn(ar->ab, "Invalid bandwidth %d for link station %pM\n",
3827 			    link_sta->bandwidth, link_sta->addr);
3828 		bw = WMI_PEER_CHWIDTH_20MHZ;
3829 		break;
3830 	}
3831 
3832 	return bw;
3833 }
3834 
3835 static void ath12k_bss_assoc(struct ath12k *ar,
3836 			     struct ath12k_link_vif *arvif,
3837 			     struct ieee80211_bss_conf *bss_conf)
3838 {
3839 	struct ath12k_vif *ahvif = arvif->ahvif;
3840 	struct ieee80211_vif *vif = ath12k_ahvif_to_vif(ahvif);
3841 	struct ath12k_wmi_vdev_up_params params = {};
3842 	struct ieee80211_link_sta *link_sta;
3843 	u8 link_id = bss_conf->link_id;
3844 	struct ath12k_link_sta *arsta;
3845 	struct ieee80211_sta *ap_sta;
3846 	struct ath12k_sta *ahsta;
3847 	struct ath12k_dp_link_peer *peer;
3848 	bool is_auth = false;
3849 	u32 hemode = 0;
3850 	int ret;
3851 	struct ath12k_dp *dp = ath12k_ab_to_dp(ar->ab);
3852 
3853 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
3854 
3855 	struct ath12k_wmi_peer_assoc_arg *peer_arg __free(kfree) =
3856 					kzalloc_obj(*peer_arg);
3857 	if (!peer_arg)
3858 		return;
3859 
3860 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
3861 		   "mac vdev %i link id %u assoc bssid %pM aid %d\n",
3862 		   arvif->vdev_id, link_id, arvif->bssid, ahvif->aid);
3863 
3864 	rcu_read_lock();
3865 
3866 	/* During ML connection, cfg.ap_addr has the MLD address. For
3867 	 * non-ML connection, it has the BSSID.
3868 	 */
3869 	ap_sta = ieee80211_find_sta(vif, vif->cfg.ap_addr);
3870 	if (!ap_sta) {
3871 		ath12k_warn(ar->ab, "failed to find station entry for bss %pM vdev %i\n",
3872 			    vif->cfg.ap_addr, arvif->vdev_id);
3873 		rcu_read_unlock();
3874 		return;
3875 	}
3876 
3877 	ahsta = ath12k_sta_to_ahsta(ap_sta);
3878 
3879 	arsta = wiphy_dereference(ath12k_ar_to_hw(ar)->wiphy,
3880 				  ahsta->link[link_id]);
3881 	if (WARN_ON(!arsta)) {
3882 		rcu_read_unlock();
3883 		return;
3884 	}
3885 
3886 	link_sta = ath12k_mac_get_link_sta(arsta);
3887 	if (WARN_ON(!link_sta)) {
3888 		rcu_read_unlock();
3889 		return;
3890 	}
3891 
3892 	ath12k_peer_assoc_prepare(ar, arvif, arsta, peer_arg, false);
3893 
3894 	/* link_sta->he_cap must be protected by rcu_read_lock */
3895 	ret = ath12k_mac_vif_recalc_sta_he_txbf(ar, arvif, &link_sta->he_cap, &hemode);
3896 	if (ret) {
3897 		ath12k_warn(ar->ab, "failed to recalc he txbf for vdev %i on bss %pM: %d\n",
3898 			    arvif->vdev_id, bss_conf->bssid, ret);
3899 		rcu_read_unlock();
3900 		return;
3901 	}
3902 
3903 	rcu_read_unlock();
3904 
3905 	/* keep this before ath12k_wmi_send_peer_assoc_cmd() */
3906 	ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
3907 					    WMI_VDEV_PARAM_SET_HEMU_MODE, hemode);
3908 	if (ret) {
3909 		ath12k_warn(ar->ab, "failed to submit vdev param txbf 0x%x: %d\n",
3910 			    hemode, ret);
3911 		return;
3912 	}
3913 
3914 	peer_arg->is_assoc = true;
3915 	ret = ath12k_wmi_send_peer_assoc_cmd(ar, peer_arg);
3916 	if (ret) {
3917 		ath12k_warn(ar->ab, "failed to run peer assoc for %pM vdev %i: %d\n",
3918 			    bss_conf->bssid, arvif->vdev_id, ret);
3919 		return;
3920 	}
3921 
3922 	if (!wait_for_completion_timeout(&ar->peer_assoc_done, 1 * HZ)) {
3923 		ath12k_warn(ar->ab, "failed to get peer assoc conf event for %pM vdev %i\n",
3924 			    bss_conf->bssid, arvif->vdev_id);
3925 		return;
3926 	}
3927 
3928 	ret = ath12k_setup_peer_smps(ar, arvif, bss_conf->bssid,
3929 				     &link_sta->ht_cap, &link_sta->he_6ghz_capa);
3930 	if (ret) {
3931 		ath12k_warn(ar->ab, "failed to setup peer SMPS for vdev %d: %d\n",
3932 			    arvif->vdev_id, ret);
3933 		return;
3934 	}
3935 
3936 	WARN_ON(arvif->is_up);
3937 
3938 	ahvif->aid = vif->cfg.aid;
3939 	ether_addr_copy(arvif->bssid, bss_conf->bssid);
3940 
3941 	params.vdev_id = arvif->vdev_id;
3942 	params.aid = ahvif->aid;
3943 	params.bssid = arvif->bssid;
3944 	params.tx_bssid = ath12k_mac_get_tx_bssid(arvif);
3945 	if (params.tx_bssid) {
3946 		params.nontx_profile_idx = bss_conf->bssid_index;
3947 		params.nontx_profile_cnt = 1 << bss_conf->bssid_indicator;
3948 	}
3949 	ret = ath12k_wmi_vdev_up(ar, &params);
3950 	if (ret) {
3951 		ath12k_warn(ar->ab, "failed to set vdev %d up: %d\n",
3952 			    arvif->vdev_id, ret);
3953 		return;
3954 	}
3955 
3956 	arvif->is_up = true;
3957 	arvif->rekey_data.enable_offload = false;
3958 
3959 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
3960 		   "mac vdev %d up (associated) bssid %pM aid %d\n",
3961 		   arvif->vdev_id, bss_conf->bssid, vif->cfg.aid);
3962 
3963 	spin_lock_bh(&dp->dp_lock);
3964 
3965 	peer = ath12k_dp_link_peer_find_by_vdev_and_addr(dp, arvif->vdev_id,
3966 							 arvif->bssid);
3967 	if (peer && peer->is_authorized)
3968 		is_auth = true;
3969 
3970 	spin_unlock_bh(&dp->dp_lock);
3971 
3972 	/* Authorize BSS Peer */
3973 	if (is_auth) {
3974 		ret = ath12k_wmi_set_peer_param(ar, arvif->bssid,
3975 						arvif->vdev_id,
3976 						WMI_PEER_AUTHORIZE,
3977 						1);
3978 		if (ret)
3979 			ath12k_warn(ar->ab, "Unable to authorize BSS peer: %d\n", ret);
3980 	}
3981 
3982 	ret = ath12k_wmi_send_obss_spr_cmd(ar, arvif->vdev_id,
3983 					   &bss_conf->he_obss_pd);
3984 	if (ret)
3985 		ath12k_warn(ar->ab, "failed to set vdev %i OBSS PD parameters: %d\n",
3986 			    arvif->vdev_id, ret);
3987 
3988 	if (test_bit(WMI_TLV_SERVICE_11D_OFFLOAD, ar->ab->wmi_ab.svc_map) &&
3989 	    ahvif->vdev_type == WMI_VDEV_TYPE_STA &&
3990 	    ahvif->vdev_subtype == WMI_VDEV_SUBTYPE_NONE)
3991 		ath12k_mac_11d_scan_stop_all(ar->ab);
3992 }
3993 
3994 static void ath12k_bss_disassoc(struct ath12k *ar,
3995 				struct ath12k_link_vif *arvif)
3996 {
3997 	int ret;
3998 
3999 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
4000 
4001 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac vdev %i disassoc bssid %pM\n",
4002 		   arvif->vdev_id, arvif->bssid);
4003 
4004 	ret = ath12k_wmi_vdev_down(ar, arvif->vdev_id);
4005 	if (ret)
4006 		ath12k_warn(ar->ab, "failed to down vdev %i: %d\n",
4007 			    arvif->vdev_id, ret);
4008 
4009 	arvif->is_up = false;
4010 
4011 	memset(&arvif->rekey_data, 0, sizeof(arvif->rekey_data));
4012 
4013 	cancel_delayed_work(&arvif->connection_loss_work);
4014 }
4015 
4016 static u32 ath12k_mac_get_rate_hw_value(int bitrate)
4017 {
4018 	u32 preamble;
4019 	u16 hw_value;
4020 	int rate;
4021 	size_t i;
4022 
4023 	if (ath12k_mac_bitrate_is_cck(bitrate))
4024 		preamble = WMI_RATE_PREAMBLE_CCK;
4025 	else
4026 		preamble = WMI_RATE_PREAMBLE_OFDM;
4027 
4028 	for (i = 0; i < ARRAY_SIZE(ath12k_legacy_rates); i++) {
4029 		if (ath12k_legacy_rates[i].bitrate != bitrate)
4030 			continue;
4031 
4032 		hw_value = ath12k_legacy_rates[i].hw_value;
4033 		rate = ATH12K_HW_RATE_CODE(hw_value, 0, preamble);
4034 
4035 		return rate;
4036 	}
4037 
4038 	return -EINVAL;
4039 }
4040 
4041 static void ath12k_recalculate_mgmt_rate(struct ath12k *ar,
4042 					 struct ath12k_link_vif *arvif,
4043 					 struct cfg80211_chan_def *def)
4044 {
4045 	struct ieee80211_vif *vif = ath12k_ahvif_to_vif(arvif->ahvif);
4046 	struct ieee80211_hw *hw = ath12k_ar_to_hw(ar);
4047 	const struct ieee80211_supported_band *sband;
4048 	struct ieee80211_bss_conf *bss_conf;
4049 	u8 basic_rate_idx;
4050 	int hw_rate_code;
4051 	u32 vdev_param;
4052 	u16 bitrate;
4053 	int ret;
4054 
4055 	lockdep_assert_wiphy(hw->wiphy);
4056 
4057 	bss_conf = ath12k_mac_get_link_bss_conf(arvif);
4058 	if (!bss_conf) {
4059 		ath12k_warn(ar->ab, "unable to access bss link conf in mgmt rate calc for vif %pM link %u\n",
4060 			    vif->addr, arvif->link_id);
4061 		return;
4062 	}
4063 
4064 	sband = hw->wiphy->bands[def->chan->band];
4065 	if (bss_conf->basic_rates)
4066 		basic_rate_idx = __ffs(bss_conf->basic_rates);
4067 	else
4068 		basic_rate_idx = 0;
4069 	bitrate = sband->bitrates[basic_rate_idx].bitrate;
4070 
4071 	hw_rate_code = ath12k_mac_get_rate_hw_value(bitrate);
4072 	if (hw_rate_code < 0) {
4073 		ath12k_warn(ar->ab, "bitrate not supported %d\n", bitrate);
4074 		return;
4075 	}
4076 
4077 	vdev_param = WMI_VDEV_PARAM_MGMT_RATE;
4078 	ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id, vdev_param,
4079 					    hw_rate_code);
4080 	if (ret)
4081 		ath12k_warn(ar->ab, "failed to set mgmt tx rate %d\n", ret);
4082 
4083 	vdev_param = WMI_VDEV_PARAM_BEACON_RATE;
4084 	ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id, vdev_param,
4085 					    hw_rate_code);
4086 	if (ret)
4087 		ath12k_warn(ar->ab, "failed to set beacon tx rate %d\n", ret);
4088 }
4089 
4090 static void ath12k_mac_bcn_tx_event(struct ath12k_link_vif *arvif)
4091 {
4092 	struct ieee80211_vif *vif = ath12k_ahvif_to_vif(arvif->ahvif);
4093 	struct ieee80211_bss_conf *link_conf;
4094 
4095 	link_conf = ath12k_mac_get_link_bss_conf(arvif);
4096 	if (!link_conf) {
4097 		ath12k_warn(arvif->ar->ab, "failed to get link conf for vdev %u\n",
4098 			    arvif->vdev_id);
4099 		return;
4100 	}
4101 
4102 	if (link_conf->color_change_active) {
4103 		if (ieee80211_beacon_cntdwn_is_complete(vif, arvif->link_id)) {
4104 			ieee80211_color_change_finish(vif, arvif->link_id);
4105 			return;
4106 		}
4107 
4108 		ieee80211_beacon_update_cntdwn(vif, arvif->link_id);
4109 		ath12k_mac_setup_bcn_tmpl(arvif);
4110 	}
4111 }
4112 
4113 static void ath12k_mac_bcn_tx_work(struct wiphy *wiphy, struct wiphy_work *work)
4114 {
4115 	struct ath12k_link_vif *arvif = container_of(work, struct ath12k_link_vif,
4116 						     bcn_tx_work);
4117 
4118 	lockdep_assert_wiphy(wiphy);
4119 	ath12k_mac_bcn_tx_event(arvif);
4120 }
4121 
4122 static void ath12k_mac_init_arvif(struct ath12k_vif *ahvif,
4123 				  struct ath12k_link_vif *arvif, int link_id)
4124 {
4125 	struct ath12k_hw *ah = ahvif->ah;
4126 	u8 _link_id;
4127 	int i;
4128 
4129 	lockdep_assert_wiphy(ah->hw->wiphy);
4130 
4131 	if (WARN_ON(!arvif))
4132 		return;
4133 
4134 	if (WARN_ON(link_id >= ATH12K_NUM_MAX_LINKS))
4135 		return;
4136 
4137 	if (link_id < 0)
4138 		_link_id = 0;
4139 	else
4140 		_link_id = link_id;
4141 
4142 	arvif->ahvif = ahvif;
4143 	arvif->link_id = _link_id;
4144 
4145 	/* Protects the datapath stats update on a per link basis */
4146 	spin_lock_init(&arvif->link_stats_lock);
4147 
4148 	INIT_LIST_HEAD(&arvif->list);
4149 	INIT_DELAYED_WORK(&arvif->connection_loss_work,
4150 			  ath12k_mac_vif_sta_connection_loss_work);
4151 	wiphy_work_init(&arvif->bcn_tx_work, ath12k_mac_bcn_tx_work);
4152 
4153 	arvif->num_stations = 0;
4154 
4155 	for (i = 0; i < ARRAY_SIZE(arvif->bitrate_mask.control); i++) {
4156 		arvif->bitrate_mask.control[i].legacy = 0xffffffff;
4157 		arvif->bitrate_mask.control[i].gi = NL80211_TXRATE_DEFAULT_GI;
4158 		memset(arvif->bitrate_mask.control[i].ht_mcs, 0xff,
4159 		       sizeof(arvif->bitrate_mask.control[i].ht_mcs));
4160 		memset(arvif->bitrate_mask.control[i].vht_mcs, 0xff,
4161 		       sizeof(arvif->bitrate_mask.control[i].vht_mcs));
4162 		memset(arvif->bitrate_mask.control[i].he_mcs, 0xff,
4163 		       sizeof(arvif->bitrate_mask.control[i].he_mcs));
4164 		memset(arvif->bitrate_mask.control[i].eht_mcs, 0xff,
4165 		       sizeof(arvif->bitrate_mask.control[i].eht_mcs));
4166 	}
4167 
4168 	/* Handle MLO related assignments */
4169 	if (link_id >= 0) {
4170 		rcu_assign_pointer(ahvif->link[arvif->link_id], arvif);
4171 		ahvif->links_map |= BIT(_link_id);
4172 	}
4173 
4174 	ath12k_generic_dbg(ATH12K_DBG_MAC,
4175 			   "mac init link arvif (link_id %d%s) for vif %pM. links_map 0x%x",
4176 			   _link_id, (link_id < 0) ? " deflink" : "", ahvif->vif->addr,
4177 			   ahvif->links_map);
4178 }
4179 
4180 static void ath12k_mac_remove_link_interface(struct ieee80211_hw *hw,
4181 					     struct ath12k_link_vif *arvif)
4182 {
4183 	struct ath12k_vif *ahvif = arvif->ahvif;
4184 	struct ath12k_hw *ah = hw->priv;
4185 	struct ath12k *ar = arvif->ar;
4186 	int ret;
4187 
4188 	lockdep_assert_wiphy(ah->hw->wiphy);
4189 
4190 	cancel_delayed_work_sync(&arvif->connection_loss_work);
4191 	wiphy_work_cancel(ath12k_ar_to_hw(ar)->wiphy, &arvif->bcn_tx_work);
4192 
4193 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac remove link interface (vdev %d link id %d)",
4194 		   arvif->vdev_id, arvif->link_id);
4195 
4196 	if (test_bit(WMI_TLV_SERVICE_11D_OFFLOAD, ar->ab->wmi_ab.svc_map) &&
4197 	    ahvif->vdev_type == WMI_VDEV_TYPE_STA &&
4198 	    ahvif->vdev_subtype == WMI_VDEV_SUBTYPE_NONE)
4199 		ath12k_mac_11d_scan_stop(ar);
4200 
4201 	if (ahvif->vdev_type == WMI_VDEV_TYPE_AP) {
4202 		ret = ath12k_peer_delete(ar, arvif->vdev_id, arvif->bssid);
4203 		if (ret)
4204 			ath12k_warn(ar->ab, "failed to submit AP self-peer removal on vdev %d link id %d: %d",
4205 				    arvif->vdev_id, arvif->link_id, ret);
4206 
4207 		if (arvif->link_id < IEEE80211_MLD_MAX_NUM_LINKS)
4208 			ath12k_dp_peer_delete(&ah->dp_hw, arvif->bssid, NULL);
4209 	}
4210 	ath12k_mac_vdev_delete(ar, arvif);
4211 }
4212 
4213 static struct ath12k_link_vif *ath12k_mac_assign_link_vif(struct ath12k_hw *ah,
4214 							  struct ieee80211_vif *vif,
4215 							  u8 link_id)
4216 {
4217 	struct ath12k_vif *ahvif = ath12k_vif_to_ahvif(vif);
4218 	struct ath12k_link_vif *arvif;
4219 
4220 	lockdep_assert_wiphy(ah->hw->wiphy);
4221 
4222 	arvif = wiphy_dereference(ah->hw->wiphy, ahvif->link[link_id]);
4223 	if (arvif)
4224 		return arvif;
4225 
4226 	/* If this is the first link arvif being created for an ML VIF
4227 	 * use the preallocated deflink memory except for scan arvifs
4228 	 */
4229 	if (!ahvif->links_map && link_id < ATH12K_FIRST_SCAN_LINK) {
4230 		arvif = &ahvif->deflink;
4231 
4232 		if (vif->type == NL80211_IFTYPE_STATION)
4233 			arvif->is_sta_assoc_link = true;
4234 	} else {
4235 		arvif = kzalloc_obj(*arvif);
4236 		if (!arvif)
4237 			return NULL;
4238 	}
4239 
4240 	ath12k_mac_init_arvif(ahvif, arvif, link_id);
4241 
4242 	return arvif;
4243 }
4244 
4245 static void ath12k_mac_unassign_link_vif(struct ath12k_link_vif *arvif)
4246 {
4247 	struct ath12k_vif *ahvif = arvif->ahvif;
4248 	struct ath12k_hw *ah = ahvif->ah;
4249 
4250 	lockdep_assert_wiphy(ah->hw->wiphy);
4251 
4252 	rcu_assign_pointer(ahvif->link[arvif->link_id], NULL);
4253 	synchronize_rcu();
4254 	ahvif->links_map &= ~BIT(arvif->link_id);
4255 
4256 	if (arvif != &ahvif->deflink)
4257 		kfree(arvif);
4258 	else
4259 		memset(arvif, 0, sizeof(*arvif));
4260 }
4261 
4262 int
4263 ath12k_mac_op_change_vif_links(struct ieee80211_hw *hw,
4264 			       struct ieee80211_vif *vif,
4265 			       u16 old_links, u16 new_links,
4266 			       struct ieee80211_bss_conf *ol[IEEE80211_MLD_MAX_NUM_LINKS])
4267 {
4268 	struct ath12k_vif *ahvif = ath12k_vif_to_ahvif(vif);
4269 	unsigned long to_remove = old_links & ~new_links;
4270 	unsigned long to_add = ~old_links & new_links;
4271 	struct ath12k_hw *ah = ath12k_hw_to_ah(hw);
4272 	struct ath12k_link_vif *arvif;
4273 	u8 link_id;
4274 
4275 	lockdep_assert_wiphy(hw->wiphy);
4276 
4277 	ath12k_generic_dbg(ATH12K_DBG_MAC,
4278 			   "mac vif link changed for MLD %pM old_links 0x%x new_links 0x%x\n",
4279 			   vif->addr, old_links, new_links);
4280 
4281 	for_each_set_bit(link_id, &to_add, IEEE80211_MLD_MAX_NUM_LINKS) {
4282 		arvif = wiphy_dereference(hw->wiphy, ahvif->link[link_id]);
4283 		/* mac80211 wants to add link but driver already has the
4284 		 * link. This should not happen ideally.
4285 		 */
4286 		if (WARN_ON(arvif))
4287 			return -EINVAL;
4288 
4289 		arvif = ath12k_mac_assign_link_vif(ah, vif, link_id);
4290 		if (WARN_ON(!arvif))
4291 			return -EINVAL;
4292 	}
4293 
4294 	for_each_set_bit(link_id, &to_remove, IEEE80211_MLD_MAX_NUM_LINKS) {
4295 		arvif = wiphy_dereference(hw->wiphy, ahvif->link[link_id]);
4296 		if (WARN_ON(!arvif))
4297 			return -EINVAL;
4298 
4299 		if (!arvif->is_created) {
4300 			ath12k_mac_unassign_link_vif(arvif);
4301 			continue;
4302 		}
4303 
4304 		if (WARN_ON(!arvif->ar))
4305 			return -EINVAL;
4306 
4307 		ath12k_mac_remove_link_interface(hw, arvif);
4308 		ath12k_mac_unassign_link_vif(arvif);
4309 	}
4310 
4311 	return 0;
4312 }
4313 EXPORT_SYMBOL(ath12k_mac_op_change_vif_links);
4314 
4315 static int ath12k_mac_fils_discovery(struct ath12k_link_vif *arvif,
4316 				     struct ieee80211_bss_conf *info)
4317 {
4318 	struct ieee80211_vif *vif = ath12k_ahvif_to_vif(arvif->ahvif);
4319 	struct ath12k *ar = arvif->ar;
4320 	struct ieee80211_hw *hw = ath12k_ar_to_hw(ar);
4321 	struct sk_buff *tmpl;
4322 	int ret;
4323 	u32 interval;
4324 	bool unsol_bcast_probe_resp_enabled = false;
4325 
4326 	if (info->fils_discovery.max_interval) {
4327 		interval = info->fils_discovery.max_interval;
4328 
4329 		tmpl = ieee80211_get_fils_discovery_tmpl(hw, vif,
4330 							 info->link_id);
4331 		if (tmpl)
4332 			ret = ath12k_wmi_fils_discovery_tmpl(ar, arvif->vdev_id,
4333 							     tmpl);
4334 	} else if (info->unsol_bcast_probe_resp_interval) {
4335 		unsol_bcast_probe_resp_enabled = 1;
4336 		interval = info->unsol_bcast_probe_resp_interval;
4337 
4338 		tmpl = ieee80211_get_unsol_bcast_probe_resp_tmpl(hw, vif,
4339 								 info->link_id);
4340 		if (tmpl)
4341 			ret = ath12k_wmi_probe_resp_tmpl(ar, arvif->vdev_id,
4342 							 tmpl);
4343 	} else { /* Disable */
4344 		return ath12k_wmi_fils_discovery(ar, arvif->vdev_id, 0, false);
4345 	}
4346 
4347 	if (!tmpl) {
4348 		ath12k_warn(ar->ab,
4349 			    "mac vdev %i failed to retrieve %s template\n",
4350 			    arvif->vdev_id, (unsol_bcast_probe_resp_enabled ?
4351 			    "unsolicited broadcast probe response" :
4352 			    "FILS discovery"));
4353 		return -EPERM;
4354 	}
4355 	kfree_skb(tmpl);
4356 
4357 	if (!ret)
4358 		ret = ath12k_wmi_fils_discovery(ar, arvif->vdev_id, interval,
4359 						unsol_bcast_probe_resp_enabled);
4360 
4361 	return ret;
4362 }
4363 
4364 void ath12k_mac_op_vif_cfg_changed(struct ieee80211_hw *hw,
4365 				   struct ieee80211_vif *vif,
4366 				   u64 changed)
4367 {
4368 	struct ath12k_vif *ahvif = ath12k_vif_to_ahvif(vif);
4369 	unsigned long links = ahvif->links_map;
4370 	struct ieee80211_bss_conf *info;
4371 	struct ath12k_link_vif *arvif;
4372 	struct ieee80211_sta *sta;
4373 	struct ath12k_sta *ahsta;
4374 	struct ath12k *ar;
4375 	u8 link_id;
4376 
4377 	lockdep_assert_wiphy(hw->wiphy);
4378 
4379 	if (changed & BSS_CHANGED_SSID && vif->type == NL80211_IFTYPE_AP) {
4380 		ahvif->u.ap.ssid_len = vif->cfg.ssid_len;
4381 		if (vif->cfg.ssid_len)
4382 			memcpy(ahvif->u.ap.ssid, vif->cfg.ssid, vif->cfg.ssid_len);
4383 	}
4384 
4385 	if (changed & BSS_CHANGED_ASSOC) {
4386 		if (vif->cfg.assoc) {
4387 			/* only in station mode we can get here, so it's safe
4388 			 * to use ap_addr
4389 			 */
4390 			rcu_read_lock();
4391 			sta = ieee80211_find_sta(vif, vif->cfg.ap_addr);
4392 			if (!sta) {
4393 				rcu_read_unlock();
4394 				WARN_ONCE(1, "failed to find sta with addr %pM\n",
4395 					  vif->cfg.ap_addr);
4396 				return;
4397 			}
4398 
4399 			ahsta = ath12k_sta_to_ahsta(sta);
4400 			arvif = wiphy_dereference(hw->wiphy,
4401 						  ahvif->link[ahsta->assoc_link_id]);
4402 			rcu_read_unlock();
4403 
4404 			ar = arvif->ar;
4405 			/* there is no reason for which an assoc link's
4406 			 * bss info does not exist
4407 			 */
4408 			info = ath12k_mac_get_link_bss_conf(arvif);
4409 			ath12k_bss_assoc(ar, arvif, info);
4410 
4411 			/* exclude assoc link as it is done above */
4412 			links &= ~BIT(ahsta->assoc_link_id);
4413 		}
4414 
4415 		for_each_set_bit(link_id, &links, IEEE80211_MLD_MAX_NUM_LINKS) {
4416 			arvif = wiphy_dereference(hw->wiphy, ahvif->link[link_id]);
4417 			if (!arvif || !arvif->ar)
4418 				continue;
4419 
4420 			ar = arvif->ar;
4421 
4422 			if (vif->cfg.assoc) {
4423 				info = ath12k_mac_get_link_bss_conf(arvif);
4424 				if (!info)
4425 					continue;
4426 
4427 				ath12k_bss_assoc(ar, arvif, info);
4428 			} else {
4429 				ath12k_bss_disassoc(ar, arvif);
4430 			}
4431 		}
4432 	}
4433 }
4434 EXPORT_SYMBOL(ath12k_mac_op_vif_cfg_changed);
4435 
4436 static void ath12k_mac_vif_setup_ps(struct ath12k_link_vif *arvif)
4437 {
4438 	struct ath12k *ar = arvif->ar;
4439 	struct ieee80211_vif *vif = arvif->ahvif->vif;
4440 	struct ieee80211_conf *conf = &ath12k_ar_to_hw(ar)->conf;
4441 	enum wmi_sta_powersave_param param;
4442 	struct ieee80211_bss_conf *info;
4443 	enum wmi_sta_ps_mode psmode;
4444 	int ret;
4445 	int timeout;
4446 	bool enable_ps;
4447 
4448 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
4449 
4450 	if (vif->type != NL80211_IFTYPE_STATION)
4451 		return;
4452 
4453 	enable_ps = arvif->ahvif->ps;
4454 	if (enable_ps) {
4455 		psmode = WMI_STA_PS_MODE_ENABLED;
4456 		param = WMI_STA_PS_PARAM_INACTIVITY_TIME;
4457 
4458 		timeout = conf->dynamic_ps_timeout;
4459 		if (timeout == 0) {
4460 			info = ath12k_mac_get_link_bss_conf(arvif);
4461 			if (!info) {
4462 				ath12k_warn(ar->ab, "unable to access bss link conf in setup ps for vif %pM link %u\n",
4463 					    vif->addr, arvif->link_id);
4464 				return;
4465 			}
4466 
4467 			/* firmware doesn't like 0 */
4468 			timeout = ieee80211_tu_to_usec(info->beacon_int) / 1000;
4469 		}
4470 
4471 		ret = ath12k_wmi_set_sta_ps_param(ar, arvif->vdev_id, param,
4472 						  timeout);
4473 		if (ret) {
4474 			ath12k_warn(ar->ab, "failed to set inactivity time for vdev %d: %i\n",
4475 				    arvif->vdev_id, ret);
4476 			return;
4477 		}
4478 	} else {
4479 		psmode = WMI_STA_PS_MODE_DISABLED;
4480 	}
4481 
4482 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac vdev %d psmode %s\n",
4483 		   arvif->vdev_id, psmode ? "enable" : "disable");
4484 
4485 	ret = ath12k_wmi_pdev_set_ps_mode(ar, arvif->vdev_id, psmode);
4486 	if (ret)
4487 		ath12k_warn(ar->ab, "failed to set sta power save mode %d for vdev %d: %d\n",
4488 			    psmode, arvif->vdev_id, ret);
4489 }
4490 
4491 static bool ath12k_mac_supports_tpc(struct ath12k *ar, struct ath12k_vif *ahvif,
4492 				    const struct cfg80211_chan_def *chandef)
4493 {
4494 	return ath12k_wmi_supports_6ghz_cc_ext(ar) &&
4495 		test_bit(WMI_TLV_SERVICE_EXT_TPC_REG_SUPPORT, ar->ab->wmi_ab.svc_map) &&
4496 		(ahvif->vdev_type == WMI_VDEV_TYPE_STA  ||
4497 		 ahvif->vdev_type == WMI_VDEV_TYPE_AP) &&
4498 		ahvif->vdev_subtype == WMI_VDEV_SUBTYPE_NONE &&
4499 		chandef->chan &&
4500 		chandef->chan->band == NL80211_BAND_6GHZ;
4501 }
4502 
4503 static void ath12k_wmi_vdev_params_up(struct ath12k *ar,
4504 				      struct ath12k_link_vif *arvif,
4505 				      struct ath12k_link_vif *tx_arvif,
4506 				      struct ieee80211_bss_conf *info, u16 aid)
4507 {
4508 	struct ath12k_wmi_vdev_up_params params = {
4509 		.vdev_id = arvif->vdev_id,
4510 		.aid = aid,
4511 		.bssid = arvif->bssid
4512 	};
4513 	int ret;
4514 
4515 	if (tx_arvif) {
4516 		params.tx_bssid = tx_arvif->bssid;
4517 		params.nontx_profile_idx = info->bssid_index;
4518 		params.nontx_profile_cnt = 1 << info->bssid_indicator;
4519 	}
4520 
4521 	ret = ath12k_wmi_vdev_up(arvif->ar, &params);
4522 	if (ret)
4523 		ath12k_warn(ar->ab, "failed to bring vdev up %d: %d\n",
4524 			    arvif->vdev_id, ret);
4525 }
4526 
4527 static int ath12k_mac_config_obss_pd(struct ath12k_link_vif *arvif,
4528 				     const struct ieee80211_he_obss_pd *he_obss_pd)
4529 {
4530 	struct ath12k_wmi_obss_pd_arg obss_pd_arg = {};
4531 	u32 srg_bitmap[2], non_srg_bitmap[2];
4532 	struct ath12k *ar = arvif->ar;
4533 	u32 param_id, pdev_id;
4534 	u32 param_val;
4535 	int ret;
4536 
4537 	if (ar->ab->hw_params->single_pdev_only)
4538 		pdev_id = ath12k_mac_get_target_pdev_id_from_vif(arvif);
4539 	else
4540 		pdev_id = ar->pdev->pdev_id;
4541 
4542 	/* Set and enable SRG/non-SRG OBSS PD threshold */
4543 	param_id = WMI_PDEV_PARAM_SET_CMD_OBSS_PD_THRESHOLD;
4544 	if (ar->monitor_started || !he_obss_pd->enable) {
4545 		ret = ath12k_wmi_pdev_set_param(ar, param_id, 0, pdev_id);
4546 		if (ret)
4547 			ath12k_warn(ar->ab,
4548 				    "failed to set OBSS PD threshold for pdev %u: %d\n",
4549 				    pdev_id, ret);
4550 		return ret;
4551 	}
4552 
4553 	/*
4554 	 * This service flag indicates firmware support for SRG/SRP-based
4555 	 * spatial reuse. It also specifies whether OBSS PD threshold values
4556 	 * should be interpreted as dB (offset) or dBm (absolute) units.
4557 	 */
4558 	obss_pd_arg.srp_support = test_bit(WMI_TLV_SERVICE_SRG_SRP_SPATIAL_REUSE_SUPPORT,
4559 					   ar->ab->wmi_ab.svc_map);
4560 
4561 	if (!(he_obss_pd->sr_ctrl &
4562 	      IEEE80211_HE_SPR_NON_SRG_OBSS_PD_SR_DISALLOWED)) {
4563 		if (he_obss_pd->sr_ctrl & IEEE80211_HE_SPR_NON_SRG_OFFSET_PRESENT)
4564 			obss_pd_arg.non_srg_th = ATH12K_OBSS_PD_MAX_THRESHOLD +
4565 						 he_obss_pd->non_srg_max_offset;
4566 		else
4567 			obss_pd_arg.non_srg_th = ATH12K_OBSS_PD_NON_SRG_MAX_THRESHOLD;
4568 
4569 		if (!obss_pd_arg.srp_support)
4570 			obss_pd_arg.non_srg_th -= ATH12K_DEFAULT_NOISE_FLOOR;
4571 
4572 		obss_pd_arg.non_srg_enabled = true;
4573 	}
4574 
4575 	if (he_obss_pd->sr_ctrl & IEEE80211_HE_SPR_SRG_INFORMATION_PRESENT) {
4576 		obss_pd_arg.srg_th = ATH12K_OBSS_PD_MAX_THRESHOLD +
4577 				     he_obss_pd->max_offset;
4578 		obss_pd_arg.srg_enabled = true;
4579 	}
4580 
4581 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
4582 		   "pdev %u OBSS PD sr_ctrl 0x%x srg_th %d dBm non_srg_th %d dBm\n",
4583 		   pdev_id, he_obss_pd->sr_ctrl,
4584 		   obss_pd_arg.srg_th, obss_pd_arg.non_srg_th);
4585 
4586 	param_val = ath12k_wmi_build_obss_pd(&obss_pd_arg);
4587 	ret = ath12k_wmi_pdev_set_param(ar, param_id, param_val, pdev_id);
4588 	if (ret) {
4589 		ath12k_warn(ar->ab,
4590 			    "failed to set OBSS PD threshold for pdev %u: %d\n",
4591 			    pdev_id, ret);
4592 		return ret;
4593 	}
4594 
4595 	/* Enable OBSS PD for all access category */
4596 	param_id  = WMI_PDEV_PARAM_SET_CMD_OBSS_PD_PER_AC;
4597 	param_val = 0xf;
4598 	ret = ath12k_wmi_pdev_set_param(ar, param_id, param_val, pdev_id);
4599 	if (ret) {
4600 		ath12k_warn(ar->ab,
4601 			    "failed to set OBSS PD per ac for pdev %u: %d\n",
4602 			    pdev_id, ret);
4603 		return ret;
4604 	}
4605 
4606 	/* Set SR prohibit */
4607 	param_id  = WMI_PDEV_PARAM_ENABLE_SR_PROHIBIT;
4608 	param_val = !!(he_obss_pd->sr_ctrl &
4609 		       IEEE80211_HE_SPR_HESIGA_SR_VAL15_ALLOWED);
4610 	ret = ath12k_wmi_pdev_set_param(ar, param_id, param_val, pdev_id);
4611 	if (ret) {
4612 		ath12k_warn(ar->ab, "failed to set SR prohibit for pdev %u: %d\n",
4613 			    pdev_id, ret);
4614 		return ret;
4615 	}
4616 
4617 	if (!obss_pd_arg.srp_support)
4618 		return 0;
4619 
4620 	memcpy(srg_bitmap, he_obss_pd->bss_color_bitmap, sizeof(srg_bitmap));
4621 	/* Set SRG BSS color bitmap */
4622 	ret = ath12k_wmi_pdev_set_srg_bss_color_bitmap(ar, pdev_id, srg_bitmap);
4623 	if (ret) {
4624 		ath12k_warn(ar->ab,
4625 			    "failed to set SRG bss color bitmap for pdev %u: %d\n",
4626 			    pdev_id, ret);
4627 		return ret;
4628 	}
4629 
4630 	/* Enable BSS colors for SRG */
4631 	ret = ath12k_wmi_pdev_srg_obss_color_enable_bitmap(ar, pdev_id, srg_bitmap);
4632 	if (ret) {
4633 		ath12k_warn(ar->ab,
4634 			    "failed to enable SRG bss color bitmap pdev %u: %d\n",
4635 			    pdev_id, ret);
4636 		return ret;
4637 	}
4638 
4639 	memcpy(srg_bitmap, he_obss_pd->partial_bssid_bitmap, sizeof(srg_bitmap));
4640 	/* Set SRG partial bssid bitmap */
4641 	ret = ath12k_wmi_pdev_set_srg_partial_bssid_bitmap(ar, pdev_id, srg_bitmap);
4642 	if (ret) {
4643 		ath12k_warn(ar->ab,
4644 			    "failed to set SRG partial bssid bitmap for pdev %u: %d\n",
4645 			    pdev_id, ret);
4646 		return ret;
4647 	}
4648 
4649 	/* Enable partial bssid mask for SRG */
4650 	ret = ath12k_wmi_pdev_srg_obss_bssid_enable_bitmap(ar, pdev_id, srg_bitmap);
4651 	if (ret) {
4652 		ath12k_warn(ar->ab,
4653 			    "failed to enable SRG bssid bitmap pdev %u: %d\n",
4654 			    pdev_id, ret);
4655 		return ret;
4656 	}
4657 
4658 	/*
4659 	 * No explicit non-SRG bitmap from mac80211; enable all colors/bssids
4660 	 * as non-SRG candidates. Actual SRG members are filtered by SRG bitmaps.
4661 	 */
4662 	memset(non_srg_bitmap, 0xff, sizeof(non_srg_bitmap));
4663 
4664 	/* Enable BSS colors for non-SRG */
4665 	ret = ath12k_wmi_pdev_non_srg_obss_color_enable_bitmap(ar, pdev_id,
4666 							       non_srg_bitmap);
4667 	if (ret) {
4668 		ath12k_warn(ar->ab,
4669 			    "failed to enable non SRG color bitmap pdev %u: %d\n",
4670 			    pdev_id, ret);
4671 		return ret;
4672 	}
4673 
4674 	/* Enable partial bssid mask for non-SRG */
4675 	ret = ath12k_wmi_pdev_non_srg_obss_bssid_enable_bitmap(ar, pdev_id,
4676 							       non_srg_bitmap);
4677 	if (ret) {
4678 		ath12k_warn(ar->ab,
4679 			    "failed to enable non SRG bssid bitmap pdev %u: %d\n",
4680 			    pdev_id, ret);
4681 		return ret;
4682 	}
4683 
4684 	return 0;
4685 }
4686 
4687 static void ath12k_mac_bss_info_changed(struct ath12k *ar,
4688 					struct ath12k_link_vif *arvif,
4689 					struct ieee80211_bss_conf *info,
4690 					u64 changed)
4691 {
4692 	struct ath12k_vif *ahvif = arvif->ahvif;
4693 	struct ieee80211_vif *vif = ath12k_ahvif_to_vif(ahvif);
4694 	struct ieee80211_vif_cfg *vif_cfg = &vif->cfg;
4695 	struct ath12k_link_vif *tx_arvif;
4696 	struct cfg80211_chan_def def;
4697 	u32 param_id, param_value;
4698 	enum nl80211_band band;
4699 	u32 vdev_param;
4700 	int mcast_rate;
4701 	u32 preamble;
4702 	u16 hw_value;
4703 	u16 bitrate;
4704 	u8 rateidx;
4705 	u32 rate;
4706 	int ret;
4707 
4708 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
4709 
4710 	if (changed & BSS_CHANGED_BEACON_INT) {
4711 		arvif->beacon_interval = info->beacon_int;
4712 
4713 		param_id = WMI_VDEV_PARAM_BEACON_INTERVAL;
4714 		ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
4715 						    param_id,
4716 						    arvif->beacon_interval);
4717 		if (ret)
4718 			ath12k_warn(ar->ab, "Failed to set beacon interval for VDEV: %d\n",
4719 				    arvif->vdev_id);
4720 		else
4721 			ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
4722 				   "Beacon interval: %d set for VDEV: %d\n",
4723 				   arvif->beacon_interval, arvif->vdev_id);
4724 	}
4725 
4726 	if (changed & BSS_CHANGED_BEACON) {
4727 		param_id = WMI_PDEV_PARAM_BEACON_TX_MODE;
4728 		param_value = WMI_BEACON_BURST_MODE;
4729 		ret = ath12k_wmi_pdev_set_param(ar, param_id,
4730 						param_value, ar->pdev->pdev_id);
4731 		if (ret)
4732 			ath12k_warn(ar->ab, "Failed to set beacon mode for VDEV: %d\n",
4733 				    arvif->vdev_id);
4734 		else
4735 			ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
4736 				   "Set burst beacon mode for VDEV: %d\n",
4737 				   arvif->vdev_id);
4738 
4739 		/* In MBSSID case, need to install transmitting VIF's template first */
4740 
4741 		ret = ath12k_mac_setup_bcn_tmpl(arvif);
4742 		if (ret)
4743 			ath12k_warn(ar->ab, "failed to update bcn template: %d\n",
4744 				    ret);
4745 
4746 		if (!arvif->is_csa_in_progress)
4747 			goto skip_vdev_up;
4748 
4749 		tx_arvif = ath12k_mac_get_tx_arvif(arvif, info);
4750 		if (tx_arvif && arvif != tx_arvif && tx_arvif->is_csa_in_progress)
4751 			/* skip non tx vif's */
4752 			goto skip_vdev_up;
4753 
4754 		ath12k_wmi_vdev_params_up(ar, arvif, tx_arvif, info, ahvif->aid);
4755 
4756 		arvif->is_csa_in_progress = false;
4757 
4758 		if (tx_arvif && arvif == tx_arvif) {
4759 			struct ath12k_link_vif *arvif_itr;
4760 
4761 			list_for_each_entry(arvif_itr, &ar->arvifs, list) {
4762 				if (!arvif_itr->is_csa_in_progress)
4763 					continue;
4764 
4765 				ath12k_wmi_vdev_params_up(ar, arvif, tx_arvif,
4766 							  info, ahvif->aid);
4767 				arvif_itr->is_csa_in_progress = false;
4768 			}
4769 		}
4770 	}
4771 
4772 skip_vdev_up:
4773 
4774 	if (changed & (BSS_CHANGED_BEACON_INFO | BSS_CHANGED_BEACON)) {
4775 		arvif->dtim_period = info->dtim_period;
4776 
4777 		param_id = WMI_VDEV_PARAM_DTIM_PERIOD;
4778 		ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
4779 						    param_id,
4780 						    arvif->dtim_period);
4781 
4782 		if (ret)
4783 			ath12k_warn(ar->ab, "Failed to set dtim period for VDEV %d: %i\n",
4784 				    arvif->vdev_id, ret);
4785 		else
4786 			ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
4787 				   "DTIM period: %d set for VDEV: %d\n",
4788 				   arvif->dtim_period, arvif->vdev_id);
4789 	}
4790 
4791 	if (changed & BSS_CHANGED_SSID &&
4792 	    vif->type == NL80211_IFTYPE_AP) {
4793 		ahvif->u.ap.ssid_len = vif->cfg.ssid_len;
4794 		if (vif->cfg.ssid_len)
4795 			memcpy(ahvif->u.ap.ssid, vif->cfg.ssid, vif->cfg.ssid_len);
4796 		ahvif->u.ap.hidden_ssid = info->hidden_ssid;
4797 	}
4798 
4799 	if (changed & BSS_CHANGED_BSSID && !is_zero_ether_addr(info->bssid))
4800 		ether_addr_copy(arvif->bssid, info->bssid);
4801 
4802 	if (changed & BSS_CHANGED_BEACON_ENABLED) {
4803 		if (info->enable_beacon) {
4804 			ret = ath12k_mac_set_he_txbf_conf(arvif);
4805 			if (ret)
4806 				ath12k_warn(ar->ab,
4807 					    "failed to set HE TXBF config for vdev: %d\n",
4808 					    arvif->vdev_id);
4809 
4810 			ret = ath12k_mac_set_eht_txbf_conf(arvif);
4811 			if (ret)
4812 				ath12k_warn(ar->ab,
4813 					    "failed to set EHT TXBF config for vdev: %d\n",
4814 					    arvif->vdev_id);
4815 		}
4816 		ath12k_control_beaconing(arvif, info);
4817 
4818 		if (arvif->is_up && info->he_support &&
4819 		    info->he_oper.params) {
4820 			/* TODO: Extend to support 1024 BA Bitmap size */
4821 			ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
4822 							    WMI_VDEV_PARAM_BA_MODE,
4823 							    WMI_BA_MODE_BUFFER_SIZE_256);
4824 			if (ret)
4825 				ath12k_warn(ar->ab,
4826 					    "failed to set BA BUFFER SIZE 256 for vdev: %d\n",
4827 					    arvif->vdev_id);
4828 
4829 			param_id = WMI_VDEV_PARAM_HEOPS_0_31;
4830 			param_value = info->he_oper.params;
4831 			ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
4832 							    param_id, param_value);
4833 			ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
4834 				   "he oper param: %x set for VDEV: %d\n",
4835 				   param_value, arvif->vdev_id);
4836 
4837 			if (ret)
4838 				ath12k_warn(ar->ab, "Failed to set he oper params %x for VDEV %d: %i\n",
4839 					    param_value, arvif->vdev_id, ret);
4840 		}
4841 	}
4842 
4843 	if (changed & BSS_CHANGED_ERP_CTS_PROT) {
4844 		u32 cts_prot;
4845 
4846 		cts_prot = !!(info->use_cts_prot);
4847 		param_id = WMI_VDEV_PARAM_PROTECTION_MODE;
4848 
4849 		if (arvif->is_started) {
4850 			ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
4851 							    param_id, cts_prot);
4852 			if (ret)
4853 				ath12k_warn(ar->ab, "Failed to set CTS prot for VDEV: %d\n",
4854 					    arvif->vdev_id);
4855 			else
4856 				ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "Set CTS prot: %d for VDEV: %d\n",
4857 					   cts_prot, arvif->vdev_id);
4858 		} else {
4859 			ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "defer protection mode setup, vdev is not ready yet\n");
4860 		}
4861 	}
4862 
4863 	if (changed & BSS_CHANGED_ERP_SLOT) {
4864 		u32 slottime;
4865 
4866 		if (info->use_short_slot)
4867 			slottime = WMI_VDEV_SLOT_TIME_SHORT; /* 9us */
4868 
4869 		else
4870 			slottime = WMI_VDEV_SLOT_TIME_LONG; /* 20us */
4871 
4872 		param_id = WMI_VDEV_PARAM_SLOT_TIME;
4873 		ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
4874 						    param_id, slottime);
4875 		if (ret)
4876 			ath12k_warn(ar->ab, "Failed to set erp slot for VDEV: %d\n",
4877 				    arvif->vdev_id);
4878 		else
4879 			ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
4880 				   "Set slottime: %d for VDEV: %d\n",
4881 				   slottime, arvif->vdev_id);
4882 	}
4883 
4884 	if (changed & BSS_CHANGED_ERP_PREAMBLE) {
4885 		u32 preamble;
4886 
4887 		if (info->use_short_preamble)
4888 			preamble = WMI_VDEV_PREAMBLE_SHORT;
4889 		else
4890 			preamble = WMI_VDEV_PREAMBLE_LONG;
4891 
4892 		param_id = WMI_VDEV_PARAM_PREAMBLE;
4893 		ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
4894 						    param_id, preamble);
4895 		if (ret)
4896 			ath12k_warn(ar->ab, "Failed to set preamble for VDEV: %d\n",
4897 				    arvif->vdev_id);
4898 		else
4899 			ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
4900 				   "Set preamble: %d for VDEV: %d\n",
4901 				   preamble, arvif->vdev_id);
4902 	}
4903 
4904 	if (changed & BSS_CHANGED_ASSOC) {
4905 		if (vif->cfg.assoc)
4906 			ath12k_bss_assoc(ar, arvif, info);
4907 		else
4908 			ath12k_bss_disassoc(ar, arvif);
4909 	}
4910 
4911 	if (changed & BSS_CHANGED_TXPOWER) {
4912 		ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac vdev_id %i txpower %d\n",
4913 			   arvif->vdev_id, info->txpower);
4914 
4915 		arvif->txpower = info->txpower;
4916 		ath12k_mac_txpower_recalc(ar);
4917 	}
4918 
4919 	if (changed & BSS_CHANGED_MCAST_RATE &&
4920 	    !ath12k_mac_vif_link_chan(vif, arvif->link_id, &def)) {
4921 		band = def.chan->band;
4922 		mcast_rate = info->mcast_rate[band];
4923 
4924 		if (mcast_rate > 0) {
4925 			rateidx = mcast_rate - 1;
4926 		} else {
4927 			if (info->basic_rates)
4928 				rateidx = __ffs(info->basic_rates);
4929 			else
4930 				rateidx = 0;
4931 		}
4932 
4933 		if (ar->pdev->cap.supported_bands & WMI_HOST_WLAN_5GHZ_CAP)
4934 			rateidx += ATH12K_MAC_FIRST_OFDM_RATE_IDX;
4935 
4936 		bitrate = ath12k_legacy_rates[rateidx].bitrate;
4937 		hw_value = ath12k_legacy_rates[rateidx].hw_value;
4938 
4939 		if (ath12k_mac_bitrate_is_cck(bitrate))
4940 			preamble = WMI_RATE_PREAMBLE_CCK;
4941 		else
4942 			preamble = WMI_RATE_PREAMBLE_OFDM;
4943 
4944 		rate = ATH12K_HW_RATE_CODE(hw_value, 0, preamble);
4945 
4946 		ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
4947 			   "mac vdev %d mcast_rate %x\n",
4948 			   arvif->vdev_id, rate);
4949 
4950 		vdev_param = WMI_VDEV_PARAM_MCAST_DATA_RATE;
4951 		ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
4952 						    vdev_param, rate);
4953 		if (ret)
4954 			ath12k_warn(ar->ab,
4955 				    "failed to set mcast rate on vdev %i: %d\n",
4956 				    arvif->vdev_id,  ret);
4957 
4958 		vdev_param = WMI_VDEV_PARAM_BCAST_DATA_RATE;
4959 		ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
4960 						    vdev_param, rate);
4961 		if (ret)
4962 			ath12k_warn(ar->ab,
4963 				    "failed to set bcast rate on vdev %i: %d\n",
4964 				    arvif->vdev_id,  ret);
4965 	}
4966 
4967 	if (changed & BSS_CHANGED_BASIC_RATES &&
4968 	    !ath12k_mac_vif_link_chan(vif, arvif->link_id, &def))
4969 		ath12k_recalculate_mgmt_rate(ar, arvif, &def);
4970 
4971 	if (changed & BSS_CHANGED_TWT) {
4972 		if (info->twt_requester || info->twt_responder)
4973 			ath12k_wmi_send_twt_enable_cmd(ar, ar->pdev->pdev_id);
4974 		else
4975 			ath12k_wmi_send_twt_disable_cmd(ar, ar->pdev->pdev_id);
4976 	}
4977 
4978 	if (changed & BSS_CHANGED_HE_OBSS_PD) {
4979 		if (vif->type == NL80211_IFTYPE_AP)
4980 			ath12k_mac_config_obss_pd(arvif, &info->he_obss_pd);
4981 		else
4982 			ath12k_wmi_send_obss_spr_cmd(ar, arvif->vdev_id,
4983 						     &info->he_obss_pd);
4984 	}
4985 
4986 	if (changed & BSS_CHANGED_HE_BSS_COLOR) {
4987 		if (vif->type == NL80211_IFTYPE_AP) {
4988 			ret = ath12k_wmi_obss_color_cfg_cmd(ar,
4989 							    arvif->vdev_id,
4990 							    info->he_bss_color.color,
4991 							    ATH12K_BSS_COLOR_AP_PERIODS,
4992 							    info->he_bss_color.enabled);
4993 			if (ret)
4994 				ath12k_warn(ar->ab, "failed to set bss color collision on vdev %u: %d\n",
4995 					    arvif->vdev_id,  ret);
4996 
4997 			param_id = WMI_VDEV_PARAM_BSS_COLOR;
4998 			if (info->he_bss_color.enabled)
4999 				param_value = info->he_bss_color.color <<
5000 					      IEEE80211_HE_OPERATION_BSS_COLOR_OFFSET;
5001 			else
5002 				param_value = IEEE80211_HE_OPERATION_BSS_COLOR_DISABLED;
5003 
5004 			ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
5005 							    param_id,
5006 							    param_value);
5007 			if (ret)
5008 				ath12k_warn(ar->ab, "failed to set bss color param on vdev %u: %d\n",
5009 					    arvif->vdev_id,  ret);
5010 			else
5011 				ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "bss color param 0x%x set on vdev %u\n",
5012 					   param_value, arvif->vdev_id);
5013 		} else if (vif->type == NL80211_IFTYPE_STATION) {
5014 			ret = ath12k_wmi_send_bss_color_change_enable_cmd(ar,
5015 									  arvif->vdev_id,
5016 									  1);
5017 			if (ret)
5018 				ath12k_warn(ar->ab, "failed to enable bss color change on vdev %i: %d\n",
5019 					    arvif->vdev_id,  ret);
5020 			ret = ath12k_wmi_obss_color_cfg_cmd(ar,
5021 							    arvif->vdev_id,
5022 							    0,
5023 							    ATH12K_BSS_COLOR_STA_PERIODS,
5024 							    1);
5025 			if (ret)
5026 				ath12k_warn(ar->ab, "failed to set bss color collision on vdev %i: %d\n",
5027 					    arvif->vdev_id,  ret);
5028 		}
5029 	}
5030 
5031 	ath12k_mac_fils_discovery(arvif, info);
5032 
5033 	if (changed & BSS_CHANGED_PS &&
5034 	    ar->ab->hw_params->supports_sta_ps) {
5035 		ahvif->ps = vif_cfg->ps;
5036 		ath12k_mac_vif_setup_ps(arvif);
5037 	}
5038 }
5039 
5040 static struct ath12k_vif_cache *ath12k_ahvif_get_link_cache(struct ath12k_vif *ahvif,
5041 							    u8 link_id)
5042 {
5043 	if (!ahvif->cache[link_id]) {
5044 		ahvif->cache[link_id] = kzalloc_obj(*ahvif->cache[0]);
5045 		if (ahvif->cache[link_id])
5046 			INIT_LIST_HEAD(&ahvif->cache[link_id]->key_conf.list);
5047 	}
5048 
5049 	return ahvif->cache[link_id];
5050 }
5051 
5052 static void ath12k_ahvif_put_link_key_cache(struct ath12k_vif_cache *cache)
5053 {
5054 	struct ath12k_key_conf *key_conf, *tmp;
5055 
5056 	if (!cache || list_empty(&cache->key_conf.list))
5057 		return;
5058 	list_for_each_entry_safe(key_conf, tmp, &cache->key_conf.list, list) {
5059 		list_del(&key_conf->list);
5060 		kfree(key_conf);
5061 	}
5062 }
5063 
5064 static void ath12k_ahvif_put_link_cache(struct ath12k_vif *ahvif, u8 link_id)
5065 {
5066 	if (link_id >= IEEE80211_MLD_MAX_NUM_LINKS)
5067 		return;
5068 
5069 	ath12k_ahvif_put_link_key_cache(ahvif->cache[link_id]);
5070 	kfree(ahvif->cache[link_id]);
5071 	ahvif->cache[link_id] = NULL;
5072 }
5073 
5074 void ath12k_mac_op_link_info_changed(struct ieee80211_hw *hw,
5075 				     struct ieee80211_vif *vif,
5076 				     struct ieee80211_bss_conf *info,
5077 				     u64 changed)
5078 {
5079 	struct ath12k *ar;
5080 	struct ath12k_vif *ahvif = ath12k_vif_to_ahvif(vif);
5081 	struct ath12k_vif_cache *cache;
5082 	struct ath12k_link_vif *arvif;
5083 	u8 link_id = info->link_id;
5084 
5085 	lockdep_assert_wiphy(hw->wiphy);
5086 
5087 	arvif = wiphy_dereference(hw->wiphy, ahvif->link[link_id]);
5088 
5089 	/* if the vdev is not created on a certain radio,
5090 	 * cache the info to be updated later on vdev creation
5091 	 */
5092 
5093 	if (!arvif || !arvif->is_created) {
5094 		cache = ath12k_ahvif_get_link_cache(ahvif, link_id);
5095 		if (!cache)
5096 			return;
5097 
5098 		cache->bss_conf_changed |= changed;
5099 
5100 		return;
5101 	}
5102 
5103 	ar = arvif->ar;
5104 
5105 	ath12k_mac_bss_info_changed(ar, arvif, info, changed);
5106 }
5107 EXPORT_SYMBOL(ath12k_mac_op_link_info_changed);
5108 
5109 static struct ath12k*
5110 ath12k_mac_select_scan_device(struct ieee80211_hw *hw,
5111 			      struct ieee80211_vif *vif,
5112 			      u32 center_freq)
5113 {
5114 	struct ath12k_hw *ah = hw->priv;
5115 	enum nl80211_band band;
5116 	struct ath12k *ar;
5117 	int i;
5118 
5119 	if (ah->num_radio == 1)
5120 		return ah->radio;
5121 
5122 	/* Currently mac80211 supports splitting scan requests into
5123 	 * multiple scan requests per band.
5124 	 * Loop through first channel and determine the scan radio
5125 	 * TODO: There could be 5 GHz low/high channels in that case
5126 	 * split the hw request and perform multiple scans
5127 	 */
5128 
5129 	if (center_freq < ATH12K_MIN_5GHZ_FREQ)
5130 		band = NL80211_BAND_2GHZ;
5131 	else if (center_freq < ATH12K_MIN_6GHZ_FREQ)
5132 		band = NL80211_BAND_5GHZ;
5133 	else
5134 		band = NL80211_BAND_6GHZ;
5135 
5136 	for_each_ar(ah, ar, i) {
5137 		if (ar->mac.sbands[band].channels &&
5138 		    center_freq >= KHZ_TO_MHZ(ar->freq_range.start_freq) &&
5139 		    center_freq <= KHZ_TO_MHZ(ar->freq_range.end_freq))
5140 			return ar;
5141 	}
5142 
5143 	return NULL;
5144 }
5145 
5146 void __ath12k_mac_scan_finish(struct ath12k *ar)
5147 {
5148 	struct ieee80211_hw *hw = ath12k_ar_to_hw(ar);
5149 
5150 	lockdep_assert_held(&ar->data_lock);
5151 
5152 	switch (ar->scan.state) {
5153 	case ATH12K_SCAN_IDLE:
5154 		break;
5155 	case ATH12K_SCAN_RUNNING:
5156 	case ATH12K_SCAN_ABORTING:
5157 		if (ar->scan.is_roc && ar->scan.roc_notify)
5158 			ieee80211_remain_on_channel_expired(hw);
5159 		fallthrough;
5160 	case ATH12K_SCAN_STARTING:
5161 		cancel_delayed_work(&ar->scan.timeout);
5162 		complete_all(&ar->scan.completed);
5163 		wiphy_work_queue(ar->ah->hw->wiphy, &ar->scan.vdev_clean_wk);
5164 		break;
5165 	}
5166 }
5167 
5168 void ath12k_mac_scan_finish(struct ath12k *ar)
5169 {
5170 	spin_lock_bh(&ar->data_lock);
5171 	__ath12k_mac_scan_finish(ar);
5172 	spin_unlock_bh(&ar->data_lock);
5173 }
5174 
5175 static int ath12k_scan_stop(struct ath12k *ar)
5176 {
5177 	struct ath12k_wmi_scan_cancel_arg arg = {
5178 		.req_type = WLAN_SCAN_CANCEL_SINGLE,
5179 		.scan_id = ATH12K_SCAN_ID,
5180 	};
5181 	int ret;
5182 
5183 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
5184 
5185 	/* TODO: Fill other STOP Params */
5186 	arg.pdev_id = ar->pdev->pdev_id;
5187 
5188 	ret = ath12k_wmi_send_scan_stop_cmd(ar, &arg);
5189 	if (ret) {
5190 		ath12k_warn(ar->ab, "failed to stop wmi scan: %d\n", ret);
5191 		goto out;
5192 	}
5193 
5194 	ret = wait_for_completion_timeout(&ar->scan.completed, 3 * HZ);
5195 	if (ret == 0) {
5196 		ath12k_warn(ar->ab,
5197 			    "failed to receive scan abort comple: timed out\n");
5198 		ret = -ETIMEDOUT;
5199 	} else if (ret > 0) {
5200 		ret = 0;
5201 	}
5202 
5203 out:
5204 	/* Scan state should be updated in scan completion worker but in
5205 	 * case firmware fails to deliver the event (for whatever reason)
5206 	 * it is desired to clean up scan state anyway. Firmware may have
5207 	 * just dropped the scan completion event delivery due to transport
5208 	 * pipe being overflown with data and/or it can recover on its own
5209 	 * before next scan request is submitted.
5210 	 */
5211 	spin_lock_bh(&ar->data_lock);
5212 	if (ret)
5213 		__ath12k_mac_scan_finish(ar);
5214 	spin_unlock_bh(&ar->data_lock);
5215 
5216 	return ret;
5217 }
5218 
5219 static void ath12k_scan_abort(struct ath12k *ar)
5220 {
5221 	int ret;
5222 
5223 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
5224 
5225 	spin_lock_bh(&ar->data_lock);
5226 
5227 	switch (ar->scan.state) {
5228 	case ATH12K_SCAN_IDLE:
5229 		/* This can happen if timeout worker kicked in and called
5230 		 * abortion while scan completion was being processed.
5231 		 */
5232 		break;
5233 	case ATH12K_SCAN_STARTING:
5234 	case ATH12K_SCAN_ABORTING:
5235 		ath12k_warn(ar->ab, "refusing scan abortion due to invalid scan state: %d\n",
5236 			    ar->scan.state);
5237 		break;
5238 	case ATH12K_SCAN_RUNNING:
5239 		ar->scan.state = ATH12K_SCAN_ABORTING;
5240 		spin_unlock_bh(&ar->data_lock);
5241 
5242 		ret = ath12k_scan_stop(ar);
5243 		if (ret)
5244 			ath12k_warn(ar->ab, "failed to abort scan: %d\n", ret);
5245 
5246 		spin_lock_bh(&ar->data_lock);
5247 		break;
5248 	}
5249 
5250 	spin_unlock_bh(&ar->data_lock);
5251 }
5252 
5253 static void ath12k_scan_timeout_work(struct work_struct *work)
5254 {
5255 	struct ath12k *ar = container_of(work, struct ath12k,
5256 					 scan.timeout.work);
5257 
5258 	wiphy_lock(ath12k_ar_to_hw(ar)->wiphy);
5259 	ath12k_scan_abort(ar);
5260 	wiphy_unlock(ath12k_ar_to_hw(ar)->wiphy);
5261 }
5262 
5263 static void ath12k_mac_scan_send_complete(struct ath12k *ar,
5264 					  struct cfg80211_scan_info *info)
5265 {
5266 	struct ath12k_hw *ah = ar->ah;
5267 	struct ath12k *partner_ar;
5268 	int i;
5269 
5270 	lockdep_assert_wiphy(ah->hw->wiphy);
5271 
5272 	for_each_ar(ah, partner_ar, i)
5273 		if (partner_ar != ar &&
5274 		    partner_ar->scan.state == ATH12K_SCAN_RUNNING)
5275 			return;
5276 
5277 	ieee80211_scan_completed(ah->hw, info);
5278 }
5279 
5280 static void ath12k_scan_vdev_clean_work(struct wiphy *wiphy, struct wiphy_work *work)
5281 {
5282 	struct ath12k *ar = container_of(work, struct ath12k,
5283 					 scan.vdev_clean_wk);
5284 	struct ath12k_hw *ah = ar->ah;
5285 	struct ath12k_link_vif *arvif;
5286 
5287 	lockdep_assert_wiphy(wiphy);
5288 
5289 	arvif = ar->scan.arvif;
5290 
5291 	/* The scan vdev has already been deleted. This can occur when a
5292 	 * new scan request is made on the same vif with a different
5293 	 * frequency, causing the scan arvif to move from one radio to
5294 	 * another. Or, scan was abrupted and via remove interface, the
5295 	 * arvif is already deleted. Alternatively, if the scan vdev is not
5296 	 * being used as an actual vdev, then do not delete it.
5297 	 */
5298 	if (!arvif || arvif->is_started)
5299 		goto work_complete;
5300 
5301 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac clean scan vdev (link id %u)",
5302 		   arvif->link_id);
5303 
5304 	ath12k_mac_remove_link_interface(ah->hw, arvif);
5305 	ath12k_mac_unassign_link_vif(arvif);
5306 
5307 work_complete:
5308 	spin_lock_bh(&ar->data_lock);
5309 	ar->scan.arvif = NULL;
5310 	if (!ar->scan.is_roc) {
5311 		struct cfg80211_scan_info info = {
5312 			.aborted = ((ar->scan.state ==
5313 				    ATH12K_SCAN_ABORTING) ||
5314 				    (ar->scan.state ==
5315 				    ATH12K_SCAN_STARTING)),
5316 		};
5317 
5318 		ath12k_mac_scan_send_complete(ar, &info);
5319 	}
5320 
5321 	ar->scan.state = ATH12K_SCAN_IDLE;
5322 	ar->scan_channel = NULL;
5323 	ar->scan.roc_freq = 0;
5324 	spin_unlock_bh(&ar->data_lock);
5325 }
5326 
5327 static int ath12k_start_scan(struct ath12k *ar,
5328 			     struct ath12k_wmi_scan_req_arg *arg)
5329 {
5330 	int ret;
5331 
5332 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
5333 
5334 	ret = ath12k_wmi_send_scan_start_cmd(ar, arg);
5335 	if (ret)
5336 		return ret;
5337 
5338 	ret = wait_for_completion_timeout(&ar->scan.started, 1 * HZ);
5339 	if (ret == 0) {
5340 		ret = ath12k_scan_stop(ar);
5341 		if (ret)
5342 			ath12k_warn(ar->ab, "failed to stop scan: %d\n", ret);
5343 
5344 		return -ETIMEDOUT;
5345 	}
5346 
5347 	/* If we failed to start the scan, return error code at
5348 	 * this point.  This is probably due to some issue in the
5349 	 * firmware, but no need to wedge the driver due to that...
5350 	 */
5351 	spin_lock_bh(&ar->data_lock);
5352 	if (ar->scan.state == ATH12K_SCAN_IDLE) {
5353 		spin_unlock_bh(&ar->data_lock);
5354 		return -EINVAL;
5355 	}
5356 	spin_unlock_bh(&ar->data_lock);
5357 
5358 	return 0;
5359 }
5360 
5361 int ath12k_mac_get_fw_stats(struct ath12k *ar,
5362 			    struct ath12k_fw_stats_req_params *param)
5363 {
5364 	struct ath12k_base *ab = ar->ab;
5365 	struct ath12k_hw *ah = ath12k_ar_to_ah(ar);
5366 	unsigned long time_left;
5367 	int ret;
5368 
5369 	guard(mutex)(&ah->hw_mutex);
5370 
5371 	if (ah->state != ATH12K_HW_STATE_ON)
5372 		return -ENETDOWN;
5373 
5374 	reinit_completion(&ar->fw_stats_complete);
5375 	reinit_completion(&ar->fw_stats_done);
5376 
5377 	ret = ath12k_wmi_send_stats_request_cmd(ar, param->stats_id,
5378 						param->vdev_id, param->pdev_id);
5379 	if (ret) {
5380 		ath12k_warn(ab, "failed to request fw stats: %d\n", ret);
5381 		return ret;
5382 	}
5383 
5384 	ath12k_dbg(ab, ATH12K_DBG_WMI,
5385 		   "get fw stat pdev id %d vdev id %d stats id 0x%x\n",
5386 		   param->pdev_id, param->vdev_id, param->stats_id);
5387 
5388 	time_left = wait_for_completion_timeout(&ar->fw_stats_complete, 1 * HZ);
5389 	if (!time_left) {
5390 		ath12k_warn(ab, "time out while waiting for get fw stats\n");
5391 		return -ETIMEDOUT;
5392 	}
5393 
5394 	/* Firmware sends WMI_UPDATE_STATS_EVENTID back-to-back
5395 	 * when stats data buffer limit is reached. fw_stats_complete
5396 	 * is completed once host receives first event from firmware, but
5397 	 * still there could be more events following. Below is to wait
5398 	 * until firmware completes sending all the events.
5399 	 */
5400 	time_left = wait_for_completion_timeout(&ar->fw_stats_done, 3 * HZ);
5401 	if (!time_left) {
5402 		ath12k_warn(ab, "time out while waiting for fw stats done\n");
5403 		return -ETIMEDOUT;
5404 	}
5405 
5406 	return 0;
5407 }
5408 
5409 int ath12k_mac_op_get_txpower(struct ieee80211_hw *hw,
5410 			      struct ieee80211_vif *vif,
5411 			      unsigned int link_id,
5412 			      int *dbm)
5413 {
5414 	struct ath12k_vif *ahvif = ath12k_vif_to_ahvif(vif);
5415 	struct ath12k_fw_stats_req_params params = {};
5416 	struct ath12k_fw_stats_pdev *pdev;
5417 	struct ath12k_hw *ah = hw->priv;
5418 	struct ath12k_link_vif *arvif;
5419 	struct ath12k_base *ab;
5420 	struct ath12k *ar;
5421 	int ret;
5422 
5423 	/* Final Tx power is minimum of Target Power, CTL power, Regulatory
5424 	 * Power, PSD EIRP Power. We just know the Regulatory power from the
5425 	 * regulatory rules obtained. FW knows all these power and sets the min
5426 	 * of these. Hence, we request the FW pdev stats in which FW reports
5427 	 * the minimum of all vdev's channel Tx power.
5428 	 */
5429 	lockdep_assert_wiphy(hw->wiphy);
5430 
5431 	arvif = wiphy_dereference(ah->hw->wiphy, ahvif->link[link_id]);
5432 	if (!arvif || !arvif->ar)
5433 		return -EINVAL;
5434 
5435 	ar = arvif->ar;
5436 	ab = ar->ab;
5437 	if (ah->state != ATH12K_HW_STATE_ON)
5438 		goto err_fallback;
5439 
5440 	if (test_bit(ATH12K_FLAG_CAC_RUNNING, &ar->dev_flags))
5441 		return -EAGAIN;
5442 
5443 	/* Limit the requests to Firmware for fetching the tx power */
5444 	if (ar->chan_tx_pwr != ATH12K_PDEV_TX_POWER_INVALID &&
5445 	    time_before(jiffies,
5446 			msecs_to_jiffies(ATH12K_PDEV_TX_POWER_REFRESH_TIME_MSECS) +
5447 					 ar->last_tx_power_update))
5448 		goto send_tx_power;
5449 
5450 	params.pdev_id = ath12k_mac_get_target_pdev_id(ar);
5451 	params.vdev_id = arvif->vdev_id;
5452 	params.stats_id = WMI_REQUEST_PDEV_STAT;
5453 	ret = ath12k_mac_get_fw_stats(ar, &params);
5454 	if (ret) {
5455 		ath12k_warn(ab, "failed to request fw pdev stats: %d\n", ret);
5456 		goto err_fallback;
5457 	}
5458 
5459 	spin_lock_bh(&ar->data_lock);
5460 	pdev = list_first_entry_or_null(&ar->fw_stats.pdevs,
5461 					struct ath12k_fw_stats_pdev, list);
5462 	if (!pdev) {
5463 		spin_unlock_bh(&ar->data_lock);
5464 		goto err_fallback;
5465 	}
5466 
5467 	/* tx power reported by firmware is in units of 0.5 dBm */
5468 	ar->chan_tx_pwr = pdev->chan_tx_power / 2;
5469 	spin_unlock_bh(&ar->data_lock);
5470 	ar->last_tx_power_update = jiffies;
5471 	ath12k_fw_stats_reset(ar);
5472 
5473 send_tx_power:
5474 	*dbm = ar->chan_tx_pwr;
5475 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "txpower fetched from firmware %d dBm\n",
5476 		   *dbm);
5477 	return 0;
5478 
5479 err_fallback:
5480 	/* We didn't get txpower from FW. Hence, relying on vif->bss_conf.txpower */
5481 	*dbm = vif->bss_conf.txpower;
5482 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "txpower from firmware NaN, reported %d dBm\n",
5483 		   *dbm);
5484 	return 0;
5485 }
5486 EXPORT_SYMBOL(ath12k_mac_op_get_txpower);
5487 
5488 static u8
5489 ath12k_mac_find_link_id_by_ar(struct ath12k_vif *ahvif, struct ath12k *ar)
5490 {
5491 	struct ath12k_link_vif *arvif;
5492 	struct ath12k_hw *ah = ahvif->ah;
5493 	unsigned long links = ahvif->links_map;
5494 	unsigned long scan_links_map;
5495 	u8 link_id;
5496 
5497 	lockdep_assert_wiphy(ah->hw->wiphy);
5498 
5499 	for_each_set_bit(link_id, &links, ATH12K_NUM_MAX_LINKS) {
5500 		arvif = wiphy_dereference(ah->hw->wiphy, ahvif->link[link_id]);
5501 
5502 		if (!arvif || !arvif->is_created)
5503 			continue;
5504 
5505 		if (ar == arvif->ar)
5506 			return link_id;
5507 	}
5508 
5509 	/* input ar is not assigned to any of the links of ML VIF, use next
5510 	 * available scan link for scan vdev creation. There are cases where
5511 	 * single scan req needs to be split in driver and initiate separate
5512 	 * scan requests to firmware based on device.
5513 	 */
5514 
5515 	 /* Unset all non-scan links (0-14) of scan_links_map so that ffs() will
5516 	  * choose an available link among scan links (i.e link id >= 15)
5517 	  */
5518 	scan_links_map = ~ahvif->links_map & ATH12K_SCAN_LINKS_MASK;
5519 	if (scan_links_map)
5520 		return __ffs(scan_links_map);
5521 
5522 	return ATH12K_FIRST_SCAN_LINK;
5523 }
5524 
5525 static int ath12k_mac_initiate_hw_scan(struct ieee80211_hw *hw,
5526 				       struct ieee80211_vif *vif,
5527 				       struct ieee80211_scan_request *hw_req,
5528 				       int n_channels,
5529 				       struct ieee80211_channel **chan_list,
5530 				       struct ath12k *ar)
5531 {
5532 	struct ath12k_hw *ah = ath12k_hw_to_ah(hw);
5533 	struct ath12k_vif *ahvif = ath12k_vif_to_ahvif(vif);
5534 	struct ath12k_link_vif *arvif;
5535 	struct cfg80211_scan_request *req = &hw_req->req;
5536 	struct ath12k_wmi_scan_req_arg *arg = NULL;
5537 	u8 link_id;
5538 	int ret;
5539 	int i;
5540 	bool create = true;
5541 
5542 	lockdep_assert_wiphy(hw->wiphy);
5543 
5544 	arvif = &ahvif->deflink;
5545 
5546 	/* check if any of the links of ML VIF is already started on
5547 	 * radio(ar) corresponding to given scan frequency and use it,
5548 	 * if not use scan link (link id >= 15) for scan purpose.
5549 	 */
5550 	link_id = ath12k_mac_find_link_id_by_ar(ahvif, ar);
5551 	/* All scan links are occupied. ideally this shouldn't happen as
5552 	 * mac80211 won't schedule scan for same band until ongoing scan is
5553 	 * completed, don't try to exceed max links just in case if it happens.
5554 	 */
5555 	if (link_id >= ATH12K_NUM_MAX_LINKS)
5556 		return -EBUSY;
5557 
5558 	arvif = ath12k_mac_assign_link_vif(ah, vif, link_id);
5559 
5560 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac link ID %d selected for scan",
5561 		   arvif->link_id);
5562 
5563 	/* If the vif is already assigned to a specific vdev of an ar,
5564 	 * check whether its already started, vdev which is started
5565 	 * are not allowed to switch to a new radio.
5566 	 * If the vdev is not started, but was earlier created on a
5567 	 * different ar, delete that vdev and create a new one. We don't
5568 	 * delete at the scan stop as an optimization to avoid redundant
5569 	 * delete-create vdev's for the same ar, in case the request is
5570 	 * always on the same band for the vif
5571 	 */
5572 	if (arvif->is_created) {
5573 		if (WARN_ON(!arvif->ar))
5574 			return -EINVAL;
5575 
5576 		if (ar != arvif->ar && arvif->is_started)
5577 			return -EINVAL;
5578 
5579 		if (ar != arvif->ar) {
5580 			ath12k_mac_remove_link_interface(hw, arvif);
5581 			ath12k_mac_unassign_link_vif(arvif);
5582 		} else {
5583 			create = false;
5584 		}
5585 	}
5586 
5587 	if (create) {
5588 		/* Previous arvif would've been cleared in radio switch block
5589 		 * above, assign arvif again for create.
5590 		 */
5591 		arvif = ath12k_mac_assign_link_vif(ah, vif, link_id);
5592 
5593 		ret = ath12k_mac_vdev_create(ar, arvif);
5594 		if (ret) {
5595 			ath12k_warn(ar->ab, "unable to create scan vdev %d\n", ret);
5596 			ath12k_mac_unassign_link_vif(arvif);
5597 			return ret;
5598 		}
5599 	}
5600 
5601 	spin_lock_bh(&ar->data_lock);
5602 	switch (ar->scan.state) {
5603 	case ATH12K_SCAN_IDLE:
5604 		reinit_completion(&ar->scan.started);
5605 		reinit_completion(&ar->scan.completed);
5606 		ar->scan.state = ATH12K_SCAN_STARTING;
5607 		ar->scan.is_roc = false;
5608 		ar->scan.arvif = arvif;
5609 		ret = 0;
5610 		break;
5611 	case ATH12K_SCAN_STARTING:
5612 	case ATH12K_SCAN_RUNNING:
5613 	case ATH12K_SCAN_ABORTING:
5614 		ret = -EBUSY;
5615 		break;
5616 	}
5617 	spin_unlock_bh(&ar->data_lock);
5618 
5619 	if (ret)
5620 		goto exit;
5621 
5622 	arg = kzalloc_flex(*arg, chan_list, n_channels);
5623 	if (!arg) {
5624 		ret = -ENOMEM;
5625 		goto exit;
5626 	}
5627 
5628 	arg->num_chan = n_channels;
5629 
5630 	ath12k_wmi_start_scan_init(ar, arg);
5631 	arg->vdev_id = arvif->vdev_id;
5632 	arg->scan_id = ATH12K_SCAN_ID;
5633 
5634 	if (req->ie_len) {
5635 		arg->extraie.ptr = kmemdup(req->ie, req->ie_len, GFP_KERNEL);
5636 		if (!arg->extraie.ptr) {
5637 			ret = -ENOMEM;
5638 			goto exit;
5639 		}
5640 		arg->extraie.len = req->ie_len;
5641 	}
5642 
5643 	if (req->n_ssids) {
5644 		arg->num_ssids = req->n_ssids;
5645 		for (i = 0; i < arg->num_ssids; i++)
5646 			arg->ssid[i] = req->ssids[i];
5647 	} else {
5648 		arg->scan_f_passive = 1;
5649 	}
5650 
5651 	for (i = 0; i < arg->num_chan; i++)
5652 		arg->chan_list[i] = chan_list[i]->center_freq;
5653 
5654 	ret = ath12k_start_scan(ar, arg);
5655 	if (ret) {
5656 		if (ret == -EBUSY)
5657 			ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
5658 				   "scan engine is busy 11d state %d\n", ar->state_11d);
5659 		else
5660 			ath12k_warn(ar->ab, "failed to start hw scan: %d\n", ret);
5661 
5662 		spin_lock_bh(&ar->data_lock);
5663 		ar->scan.state = ATH12K_SCAN_IDLE;
5664 		spin_unlock_bh(&ar->data_lock);
5665 		goto exit;
5666 	}
5667 
5668 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac scan started");
5669 
5670 	/* Add a margin to account for event/command processing */
5671 	ieee80211_queue_delayed_work(ath12k_ar_to_hw(ar), &ar->scan.timeout,
5672 				     msecs_to_jiffies(arg->max_scan_time +
5673 						      ATH12K_MAC_SCAN_TIMEOUT_MSECS));
5674 
5675 exit:
5676 	if (arg) {
5677 		kfree(arg->extraie.ptr);
5678 		kfree(arg);
5679 	}
5680 
5681 	if (ar->state_11d == ATH12K_11D_PREPARING &&
5682 	    ahvif->vdev_type == WMI_VDEV_TYPE_STA &&
5683 	    ahvif->vdev_subtype == WMI_VDEV_SUBTYPE_NONE)
5684 		ath12k_mac_11d_scan_start(ar, arvif->vdev_id);
5685 
5686 	return ret;
5687 }
5688 
5689 int ath12k_mac_op_hw_scan(struct ieee80211_hw *hw,
5690 			  struct ieee80211_vif *vif,
5691 			  struct ieee80211_scan_request *hw_req)
5692 {
5693 	struct ath12k_vif *ahvif = ath12k_vif_to_ahvif(vif);
5694 	struct ieee80211_channel **chan_list, *chan;
5695 	struct ath12k_hw *ah = ath12k_hw_to_ah(hw);
5696 	unsigned long links_map, link_id;
5697 	struct ath12k_link_vif *arvif;
5698 	struct ath12k *ar, *scan_ar;
5699 	int i, j, ret = 0;
5700 
5701 	lockdep_assert_wiphy(hw->wiphy);
5702 
5703 	chan_list = kzalloc_objs(*chan_list, hw_req->req.n_channels);
5704 	if (!chan_list)
5705 		return -ENOMEM;
5706 
5707 	/* There could be channels that belong to multiple underlying radio
5708 	 * in same scan request as mac80211 sees it as single band. In that
5709 	 * case split the hw_req based on frequency range and schedule scans to
5710 	 * corresponding radio.
5711 	 */
5712 	for_each_ar(ah, ar, i) {
5713 		int n_chans = 0;
5714 
5715 		for (j = 0; j < hw_req->req.n_channels; j++) {
5716 			chan = hw_req->req.channels[j];
5717 			scan_ar = ath12k_mac_select_scan_device(hw, vif,
5718 								chan->center_freq);
5719 			if (!scan_ar) {
5720 				ath12k_hw_warn(ah, "unable to select scan device for freq %d\n",
5721 					       chan->center_freq);
5722 				ret = -EINVAL;
5723 				goto abort;
5724 			}
5725 			if (ar != scan_ar)
5726 				continue;
5727 
5728 			chan_list[n_chans++] = chan;
5729 		}
5730 		if (n_chans) {
5731 			ret = ath12k_mac_initiate_hw_scan(hw, vif, hw_req, n_chans,
5732 							  chan_list, ar);
5733 			if (ret)
5734 				goto abort;
5735 		}
5736 	}
5737 abort:
5738 	/* If any of the parallel scans initiated fails, abort all and
5739 	 * remove the scan interfaces created. Return complete scan
5740 	 * failure as mac80211 assumes this as single scan request.
5741 	 */
5742 	if (ret) {
5743 		ath12k_hw_warn(ah, "Scan failed %d , cleanup all scan vdevs\n", ret);
5744 		links_map = ahvif->links_map;
5745 		for_each_set_bit(link_id, &links_map, ATH12K_NUM_MAX_LINKS) {
5746 			arvif = wiphy_dereference(hw->wiphy, ahvif->link[link_id]);
5747 			if (!arvif)
5748 				continue;
5749 
5750 			ar = arvif->ar;
5751 			if (ar->scan.arvif == arvif) {
5752 				wiphy_work_cancel(hw->wiphy, &ar->scan.vdev_clean_wk);
5753 				spin_lock_bh(&ar->data_lock);
5754 				ar->scan.arvif = NULL;
5755 				ar->scan.state = ATH12K_SCAN_IDLE;
5756 				ar->scan_channel = NULL;
5757 				ar->scan.roc_freq = 0;
5758 				spin_unlock_bh(&ar->data_lock);
5759 			}
5760 			if (link_id >= ATH12K_FIRST_SCAN_LINK) {
5761 				ath12k_mac_remove_link_interface(hw, arvif);
5762 				ath12k_mac_unassign_link_vif(arvif);
5763 			}
5764 		}
5765 	}
5766 	kfree(chan_list);
5767 	return ret;
5768 }
5769 EXPORT_SYMBOL(ath12k_mac_op_hw_scan);
5770 
5771 void ath12k_mac_op_cancel_hw_scan(struct ieee80211_hw *hw,
5772 				  struct ieee80211_vif *vif)
5773 {
5774 	struct ath12k_vif *ahvif = ath12k_vif_to_ahvif(vif);
5775 	unsigned long link_id, links_map = ahvif->links_map;
5776 	struct ath12k_link_vif *arvif;
5777 	struct ath12k *ar;
5778 
5779 	lockdep_assert_wiphy(hw->wiphy);
5780 
5781 	for_each_set_bit(link_id, &links_map, ATH12K_NUM_MAX_LINKS) {
5782 		arvif = wiphy_dereference(hw->wiphy, ahvif->link[link_id]);
5783 		if (!arvif || !arvif->is_created ||
5784 		    arvif->ar->scan.arvif != arvif)
5785 			continue;
5786 
5787 		ar = arvif->ar;
5788 
5789 		ath12k_scan_abort(ar);
5790 
5791 		cancel_delayed_work_sync(&ar->scan.timeout);
5792 	}
5793 }
5794 EXPORT_SYMBOL(ath12k_mac_op_cancel_hw_scan);
5795 
5796 static int ath12k_install_key(struct ath12k_link_vif *arvif,
5797 			      struct ieee80211_key_conf *key,
5798 			      enum set_key_cmd cmd,
5799 			      const u8 *macaddr, u32 flags)
5800 {
5801 	int ret;
5802 	struct ath12k *ar = arvif->ar;
5803 	struct wmi_vdev_install_key_arg arg = {
5804 		.vdev_id = arvif->vdev_id,
5805 		.key_idx = key->keyidx,
5806 		.key_len = key->keylen,
5807 		.key_data = key->key,
5808 		.key_flags = flags,
5809 		.ieee80211_key_cipher = key->cipher,
5810 		.macaddr = macaddr,
5811 	};
5812 	struct ath12k_vif *ahvif = arvif->ahvif;
5813 
5814 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
5815 
5816 	if (test_bit(ATH12K_FLAG_HW_CRYPTO_DISABLED, &ar->ab->dev_flags))
5817 		return 0;
5818 
5819 	if (cmd == DISABLE_KEY) {
5820 		/* TODO: Check if FW expects  value other than NONE for del */
5821 		/* arg.key_cipher = WMI_CIPHER_NONE; */
5822 		arg.key_len = 0;
5823 		arg.key_data = NULL;
5824 		goto check_order;
5825 	}
5826 
5827 	switch (key->cipher) {
5828 	case WLAN_CIPHER_SUITE_CCMP:
5829 	case WLAN_CIPHER_SUITE_CCMP_256:
5830 		arg.key_cipher = WMI_CIPHER_AES_CCM;
5831 		key->flags |= IEEE80211_KEY_FLAG_GENERATE_IV_MGMT;
5832 		break;
5833 	case WLAN_CIPHER_SUITE_TKIP:
5834 		arg.key_cipher = WMI_CIPHER_TKIP;
5835 		arg.key_txmic_len = 8;
5836 		arg.key_rxmic_len = 8;
5837 		break;
5838 	case WLAN_CIPHER_SUITE_GCMP:
5839 	case WLAN_CIPHER_SUITE_GCMP_256:
5840 		arg.key_cipher = WMI_CIPHER_AES_GCM;
5841 		key->flags |= IEEE80211_KEY_FLAG_GENERATE_IV_MGMT;
5842 		break;
5843 	case WLAN_CIPHER_SUITE_AES_CMAC:
5844 		arg.key_cipher = WMI_CIPHER_AES_CMAC;
5845 		break;
5846 	case WLAN_CIPHER_SUITE_BIP_GMAC_128:
5847 	case WLAN_CIPHER_SUITE_BIP_GMAC_256:
5848 		arg.key_cipher = WMI_CIPHER_AES_GMAC;
5849 		break;
5850 	case WLAN_CIPHER_SUITE_BIP_CMAC_256:
5851 		arg.key_cipher = WMI_CIPHER_AES_CMAC;
5852 		break;
5853 	default:
5854 		ath12k_warn(ar->ab, "cipher %d is not supported\n", key->cipher);
5855 		return -EOPNOTSUPP;
5856 	}
5857 
5858 	if (test_bit(ATH12K_FLAG_RAW_MODE, &ar->ab->dev_flags))
5859 		key->flags |= IEEE80211_KEY_FLAG_GENERATE_IV |
5860 			      IEEE80211_KEY_FLAG_RESERVE_TAILROOM;
5861 
5862 check_order:
5863 	if (ahvif->vdev_type == WMI_VDEV_TYPE_STA &&
5864 	    arg.key_flags == WMI_KEY_GROUP) {
5865 		if (cmd == SET_KEY) {
5866 			if (arvif->pairwise_key_done) {
5867 				ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
5868 					   "vdev %u pairwise key done, go install group key\n",
5869 					   arg.vdev_id);
5870 				goto install;
5871 			} else {
5872 				/* WCN7850 firmware requires pairwise key to be installed
5873 				 * before group key. In case group key comes first, cache
5874 				 * it and return. Will revisit it once pairwise key gets
5875 				 * installed.
5876 				 */
5877 				arvif->group_key = arg;
5878 				arvif->group_key_valid = true;
5879 				ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
5880 					   "vdev %u group key before pairwise key, cache and skip\n",
5881 					   arg.vdev_id);
5882 
5883 				ret = 0;
5884 				goto out;
5885 			}
5886 		} else {
5887 			arvif->group_key_valid = false;
5888 		}
5889 	}
5890 
5891 install:
5892 	reinit_completion(&ar->install_key_done);
5893 
5894 	ret = ath12k_wmi_vdev_install_key(arvif->ar, &arg);
5895 	if (ret)
5896 		return ret;
5897 
5898 	if (!wait_for_completion_timeout(&ar->install_key_done, 1 * HZ))
5899 		return -ETIMEDOUT;
5900 
5901 	if (ether_addr_equal(arg.macaddr, arvif->bssid))
5902 		ahvif->dp_vif.key_cipher = arg.ieee80211_key_cipher;
5903 
5904 	if (ar->install_key_status) {
5905 		ret = -EINVAL;
5906 		goto out;
5907 	}
5908 
5909 	if (ahvif->vdev_type == WMI_VDEV_TYPE_STA &&
5910 	    arg.key_flags == WMI_KEY_PAIRWISE) {
5911 		if (cmd == SET_KEY) {
5912 			arvif->pairwise_key_done = true;
5913 			if (arvif->group_key_valid) {
5914 				/* Install cached GTK */
5915 				arvif->group_key_valid = false;
5916 				arg = arvif->group_key;
5917 				ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
5918 					   "vdev %u pairwise key done, group key ready, go install\n",
5919 					   arg.vdev_id);
5920 				goto install;
5921 			}
5922 		} else {
5923 			arvif->pairwise_key_done = false;
5924 		}
5925 	}
5926 
5927 out:
5928 	if (ret) {
5929 		/* In case of failure userspace may not do DISABLE_KEY
5930 		 * but triggers re-connection directly, so manually reset
5931 		 * status here.
5932 		 */
5933 		arvif->group_key_valid = false;
5934 		arvif->pairwise_key_done = false;
5935 	}
5936 
5937 	return ret;
5938 }
5939 
5940 static int ath12k_clear_peer_keys(struct ath12k_link_vif *arvif,
5941 				  const u8 *addr)
5942 {
5943 	struct ath12k *ar = arvif->ar;
5944 	struct ath12k_base *ab = ar->ab;
5945 	struct ath12k_dp_link_peer *peer;
5946 	int first_errno = 0;
5947 	int ret;
5948 	int i, len;
5949 	u32 flags = 0;
5950 	struct ath12k_dp *dp = ath12k_ab_to_dp(ab);
5951 	struct ieee80211_key_conf *keys[WMI_MAX_KEY_INDEX + 1] = {};
5952 
5953 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
5954 
5955 	spin_lock_bh(&dp->dp_lock);
5956 	peer = ath12k_dp_link_peer_find_by_vdev_and_addr(dp, arvif->vdev_id, addr);
5957 	if (!peer || !peer->dp_peer) {
5958 		spin_unlock_bh(&dp->dp_lock);
5959 		return -ENOENT;
5960 	}
5961 
5962 	len = ARRAY_SIZE(peer->dp_peer->keys);
5963 	for (i = 0; i < len; i++) {
5964 		if (!peer->dp_peer->keys[i])
5965 			continue;
5966 
5967 		keys[i] = peer->dp_peer->keys[i];
5968 		peer->dp_peer->keys[i] = NULL;
5969 	}
5970 
5971 	spin_unlock_bh(&dp->dp_lock);
5972 
5973 	for (i = 0; i < len; i++) {
5974 		if (!keys[i])
5975 			continue;
5976 
5977 		/* key flags are not required to delete the key */
5978 		ret = ath12k_install_key(arvif, keys[i],
5979 					 DISABLE_KEY, addr, flags);
5980 		if (ret < 0 && first_errno == 0)
5981 			first_errno = ret;
5982 
5983 		if (ret < 0)
5984 			ath12k_warn(ab, "failed to remove peer key %d: %d\n",
5985 				    i, ret);
5986 	}
5987 
5988 	return first_errno;
5989 }
5990 
5991 static int ath12k_mac_set_key(struct ath12k *ar, enum set_key_cmd cmd,
5992 			      struct ath12k_link_vif *arvif,
5993 			      struct ath12k_link_sta *arsta,
5994 			      struct ieee80211_key_conf *key)
5995 {
5996 	struct ieee80211_sta *sta = NULL;
5997 	struct ath12k_base *ab = ar->ab;
5998 	struct ath12k_dp_link_peer *peer;
5999 	struct ath12k_sta *ahsta;
6000 	const u8 *peer_addr;
6001 	int ret;
6002 	u32 flags = 0;
6003 	struct ath12k_dp *dp = ath12k_ab_to_dp(ab);
6004 
6005 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
6006 
6007 	if (arsta)
6008 		sta = ath12k_ahsta_to_sta(arsta->ahsta);
6009 
6010 	if (test_bit(ATH12K_FLAG_HW_CRYPTO_DISABLED, &ab->dev_flags))
6011 		return 1;
6012 
6013 	if (sta)
6014 		peer_addr = arsta->addr;
6015 	else
6016 		peer_addr = arvif->bssid;
6017 
6018 	key->hw_key_idx = key->keyidx;
6019 
6020 	/* the peer should not disappear in mid-way (unless FW goes awry) since
6021 	 * we already hold wiphy lock. we just make sure its there now.
6022 	 */
6023 	spin_lock_bh(&dp->dp_lock);
6024 	peer = ath12k_dp_link_peer_find_by_vdev_and_addr(dp, arvif->vdev_id,
6025 							 peer_addr);
6026 	if (!peer || !peer->dp_peer) {
6027 		spin_unlock_bh(&dp->dp_lock);
6028 
6029 		if (cmd == SET_KEY) {
6030 			ath12k_warn(ab, "cannot install key for non-existent peer %pM\n",
6031 				    peer_addr);
6032 			return -EOPNOTSUPP;
6033 		}
6034 
6035 		/* if the peer doesn't exist there is no key to disable
6036 		 * anymore
6037 		 */
6038 		return 0;
6039 	}
6040 
6041 	spin_unlock_bh(&dp->dp_lock);
6042 
6043 	if (key->flags & IEEE80211_KEY_FLAG_PAIRWISE)
6044 		flags = WMI_KEY_PAIRWISE;
6045 	else
6046 		flags = WMI_KEY_GROUP;
6047 
6048 	ret = ath12k_install_key(arvif, key, cmd, peer_addr, flags);
6049 	if (ret) {
6050 		ath12k_warn(ab, "ath12k_install_key failed (%d)\n", ret);
6051 		return ret;
6052 	}
6053 
6054 	ret = ath12k_dp_rx_peer_pn_replay_config(arvif, peer_addr, cmd, key);
6055 	if (ret) {
6056 		ath12k_warn(ab, "failed to offload PN replay detection %d\n", ret);
6057 		return ret;
6058 	}
6059 
6060 	spin_lock_bh(&dp->dp_lock);
6061 	peer = ath12k_dp_link_peer_find_by_vdev_and_addr(dp, arvif->vdev_id,
6062 							 peer_addr);
6063 	if (peer && peer->dp_peer && cmd == SET_KEY) {
6064 		peer->dp_peer->keys[key->keyidx] = key;
6065 		if (key->flags & IEEE80211_KEY_FLAG_PAIRWISE) {
6066 			peer->dp_peer->ucast_keyidx = key->keyidx;
6067 			peer->dp_peer->sec_type =
6068 					ath12k_dp_tx_get_encrypt_type(key->cipher);
6069 		} else {
6070 			peer->dp_peer->mcast_keyidx = key->keyidx;
6071 			peer->dp_peer->sec_type_grp =
6072 					ath12k_dp_tx_get_encrypt_type(key->cipher);
6073 		}
6074 	} else if (peer && peer->dp_peer && cmd == DISABLE_KEY) {
6075 		peer->dp_peer->keys[key->keyidx] = NULL;
6076 		if (key->flags & IEEE80211_KEY_FLAG_PAIRWISE)
6077 			peer->dp_peer->ucast_keyidx = 0;
6078 		else
6079 			peer->dp_peer->mcast_keyidx = 0;
6080 	} else if (!peer)
6081 		/* impossible unless FW goes crazy */
6082 		ath12k_warn(ab, "peer %pM disappeared!\n", peer_addr);
6083 
6084 	if (sta) {
6085 		ahsta = ath12k_sta_to_ahsta(sta);
6086 
6087 		switch (key->cipher) {
6088 		case WLAN_CIPHER_SUITE_TKIP:
6089 		case WLAN_CIPHER_SUITE_CCMP:
6090 		case WLAN_CIPHER_SUITE_CCMP_256:
6091 		case WLAN_CIPHER_SUITE_GCMP:
6092 		case WLAN_CIPHER_SUITE_GCMP_256:
6093 			if (cmd == SET_KEY)
6094 				ahsta->pn_type = HAL_PN_TYPE_WPA;
6095 			else
6096 				ahsta->pn_type = HAL_PN_TYPE_NONE;
6097 			break;
6098 		default:
6099 			ahsta->pn_type = HAL_PN_TYPE_NONE;
6100 			break;
6101 		}
6102 	}
6103 
6104 	spin_unlock_bh(&dp->dp_lock);
6105 
6106 	return 0;
6107 }
6108 
6109 static int ath12k_mac_update_key_cache(struct ath12k_vif_cache *cache,
6110 				       enum set_key_cmd cmd,
6111 				       struct ieee80211_sta *sta,
6112 				       struct ieee80211_key_conf *key)
6113 {
6114 	struct ath12k_key_conf *key_conf, *tmp;
6115 
6116 	list_for_each_entry_safe(key_conf, tmp, &cache->key_conf.list, list) {
6117 		if (key_conf->key != key)
6118 			continue;
6119 
6120 		/* If SET key entry is already present in cache, nothing to do,
6121 		 * just return
6122 		 */
6123 		if (cmd == SET_KEY)
6124 			return 0;
6125 
6126 		/* DEL key for an old SET key which driver hasn't flushed yet.
6127 		 */
6128 		list_del(&key_conf->list);
6129 		kfree(key_conf);
6130 	}
6131 
6132 	if (cmd == SET_KEY) {
6133 		key_conf = kzalloc_obj(*key_conf);
6134 
6135 		if (!key_conf)
6136 			return -ENOMEM;
6137 
6138 		key_conf->cmd = cmd;
6139 		key_conf->sta = sta;
6140 		key_conf->key = key;
6141 		list_add_tail(&key_conf->list,
6142 			      &cache->key_conf.list);
6143 	}
6144 
6145 	return 0;
6146 }
6147 
6148 int ath12k_mac_op_set_key(struct ieee80211_hw *hw, enum set_key_cmd cmd,
6149 			  struct ieee80211_vif *vif, struct ieee80211_sta *sta,
6150 			  struct ieee80211_key_conf *key)
6151 {
6152 	struct ath12k_vif *ahvif = ath12k_vif_to_ahvif(vif);
6153 	struct ath12k_link_vif *arvif;
6154 	struct ath12k_link_sta *arsta = NULL;
6155 	struct ath12k_vif_cache *cache;
6156 	struct ath12k_sta *ahsta;
6157 	unsigned long links;
6158 	u8 link_id;
6159 	int ret;
6160 
6161 	lockdep_assert_wiphy(hw->wiphy);
6162 
6163 	/* IGTK needs to be done in host software */
6164 	if (key->keyidx == 4 || key->keyidx == 5)
6165 		return 1;
6166 
6167 	if (key->keyidx > WMI_MAX_KEY_INDEX)
6168 		return -ENOSPC;
6169 
6170 	if (sta) {
6171 		ahsta = ath12k_sta_to_ahsta(sta);
6172 
6173 		/* For an ML STA Pairwise key is same for all associated link Stations,
6174 		 * hence do set key for all link STAs which are active.
6175 		 */
6176 		if (sta->mlo) {
6177 			links = ahsta->links_map;
6178 			for_each_set_bit(link_id, &links, IEEE80211_MLD_MAX_NUM_LINKS) {
6179 				arvif = wiphy_dereference(hw->wiphy,
6180 							  ahvif->link[link_id]);
6181 				arsta = wiphy_dereference(hw->wiphy,
6182 							  ahsta->link[link_id]);
6183 
6184 				if (WARN_ON(!arvif || !arsta))
6185 					/* arvif and arsta are expected to be valid when
6186 					 * STA is present.
6187 					 */
6188 					continue;
6189 
6190 				ret = ath12k_mac_set_key(arvif->ar, cmd, arvif,
6191 							 arsta, key);
6192 				if (ret)
6193 					break;
6194 			}
6195 
6196 			return 0;
6197 		}
6198 
6199 		arsta = &ahsta->deflink;
6200 		arvif = arsta->arvif;
6201 		if (WARN_ON(!arvif))
6202 			return -EINVAL;
6203 
6204 		ret = ath12k_mac_set_key(arvif->ar, cmd, arvif, arsta, key);
6205 		if (ret)
6206 			return ret;
6207 
6208 		return 0;
6209 	}
6210 
6211 	if (key->link_id >= 0 && key->link_id < IEEE80211_MLD_MAX_NUM_LINKS) {
6212 		link_id = key->link_id;
6213 		arvif = wiphy_dereference(hw->wiphy, ahvif->link[link_id]);
6214 	} else {
6215 		link_id = 0;
6216 		arvif = &ahvif->deflink;
6217 	}
6218 
6219 	if (!arvif || !arvif->is_created) {
6220 		cache = ath12k_ahvif_get_link_cache(ahvif, link_id);
6221 		if (!cache)
6222 			return -ENOSPC;
6223 
6224 		ret = ath12k_mac_update_key_cache(cache, cmd, sta, key);
6225 		if (ret)
6226 			return ret;
6227 
6228 		return 0;
6229 	}
6230 
6231 	ret = ath12k_mac_set_key(arvif->ar, cmd, arvif, NULL, key);
6232 	if (ret)
6233 		return ret;
6234 
6235 	return 0;
6236 }
6237 EXPORT_SYMBOL(ath12k_mac_op_set_key);
6238 
6239 static int
6240 ath12k_mac_bitrate_mask_num_vht_rates(struct ath12k *ar,
6241 				      enum nl80211_band band,
6242 				      const struct cfg80211_bitrate_mask *mask)
6243 {
6244 	int num_rates = 0;
6245 	int i;
6246 
6247 	for (i = 0; i < ARRAY_SIZE(mask->control[band].vht_mcs); i++)
6248 		num_rates += hweight16(mask->control[band].vht_mcs[i]);
6249 
6250 	return num_rates;
6251 }
6252 
6253 static int
6254 ath12k_mac_bitrate_mask_num_he_rates(struct ath12k *ar,
6255 				     enum nl80211_band band,
6256 				     const struct cfg80211_bitrate_mask *mask)
6257 {
6258 	int num_rates = 0;
6259 	int i;
6260 
6261 	for (i = 0; i < ARRAY_SIZE(mask->control[band].he_mcs); i++)
6262 		num_rates += hweight16(mask->control[band].he_mcs[i]);
6263 
6264 	return num_rates;
6265 }
6266 
6267 static int
6268 ath12k_mac_bitrate_mask_num_eht_rates(struct ath12k *ar,
6269 				      enum nl80211_band band,
6270 				      const struct cfg80211_bitrate_mask *mask)
6271 {
6272 	int num_rates = 0;
6273 	int i;
6274 
6275 	for (i = 0; i < ARRAY_SIZE(mask->control[band].eht_mcs); i++)
6276 		num_rates += hweight16(mask->control[band].eht_mcs[i]);
6277 
6278 	return num_rates;
6279 }
6280 
6281 static int
6282 ath12k_mac_set_peer_vht_fixed_rate(struct ath12k_link_vif *arvif,
6283 				   struct ath12k_link_sta *arsta,
6284 				   const struct cfg80211_bitrate_mask *mask,
6285 				   enum nl80211_band band)
6286 {
6287 	struct ath12k *ar = arvif->ar;
6288 	u8 vht_rate, nss;
6289 	u32 rate_code;
6290 	int ret, i;
6291 
6292 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
6293 
6294 	nss = 0;
6295 
6296 	for (i = 0; i < ARRAY_SIZE(mask->control[band].vht_mcs); i++) {
6297 		if (hweight16(mask->control[band].vht_mcs[i]) == 1) {
6298 			nss = i + 1;
6299 			vht_rate = ffs(mask->control[band].vht_mcs[i]) - 1;
6300 		}
6301 	}
6302 
6303 	if (!nss) {
6304 		ath12k_warn(ar->ab, "No single VHT Fixed rate found to set for %pM",
6305 			    arsta->addr);
6306 		return -EINVAL;
6307 	}
6308 
6309 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
6310 		   "Setting Fixed VHT Rate for peer %pM. Device will not switch to any other selected rates",
6311 		   arsta->addr);
6312 
6313 	rate_code = ATH12K_HW_RATE_CODE(vht_rate, nss - 1,
6314 					WMI_RATE_PREAMBLE_VHT);
6315 	ret = ath12k_wmi_set_peer_param(ar, arsta->addr,
6316 					arvif->vdev_id,
6317 					WMI_PEER_PARAM_FIXED_RATE,
6318 					rate_code);
6319 	if (ret)
6320 		ath12k_warn(ar->ab,
6321 			    "failed to update STA %pM Fixed Rate %d: %d\n",
6322 			     arsta->addr, rate_code, ret);
6323 
6324 	return ret;
6325 }
6326 
6327 static int
6328 ath12k_mac_set_peer_he_fixed_rate(struct ath12k_link_vif *arvif,
6329 				  struct ath12k_link_sta *arsta,
6330 				  const struct cfg80211_bitrate_mask *mask,
6331 				  enum nl80211_band band)
6332 {
6333 	struct ath12k *ar = arvif->ar;
6334 	u8 he_rate, nss;
6335 	u32 rate_code;
6336 	int ret, i;
6337 	struct ath12k_sta *ahsta = arsta->ahsta;
6338 	struct ieee80211_sta *sta;
6339 
6340 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
6341 
6342 	sta = ath12k_ahsta_to_sta(ahsta);
6343 	nss = 0;
6344 
6345 	for (i = 0; i < ARRAY_SIZE(mask->control[band].he_mcs); i++) {
6346 		if (hweight16(mask->control[band].he_mcs[i]) == 1) {
6347 			nss = i + 1;
6348 			he_rate = ffs(mask->control[band].he_mcs[i]) - 1;
6349 		}
6350 	}
6351 
6352 	if (!nss) {
6353 		ath12k_warn(ar->ab, "No single HE Fixed rate found to set for %pM",
6354 			    arsta->addr);
6355 		return -EINVAL;
6356 	}
6357 
6358 	/* Avoid updating invalid nss as fixed rate*/
6359 	if (nss > sta->deflink.rx_nss)
6360 		return -EINVAL;
6361 
6362 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
6363 		   "Setting Fixed HE Rate for peer %pM. Device will not switch to any other selected rates",
6364 		   arsta->addr);
6365 
6366 	rate_code = ATH12K_HW_RATE_CODE(he_rate, nss - 1,
6367 					WMI_RATE_PREAMBLE_HE);
6368 
6369 	ret = ath12k_wmi_set_peer_param(ar, arsta->addr,
6370 					arvif->vdev_id,
6371 					WMI_PEER_PARAM_FIXED_RATE,
6372 					rate_code);
6373 	if (ret)
6374 		ath12k_warn(ar->ab,
6375 			    "failed to update STA %pM Fixed Rate %d: %d\n",
6376 			    arsta->addr, rate_code, ret);
6377 
6378 	return ret;
6379 }
6380 
6381 static int
6382 ath12k_mac_set_peer_eht_fixed_rate(struct ath12k_link_vif *arvif,
6383 				   struct ath12k_link_sta *arsta,
6384 				   const struct cfg80211_bitrate_mask *mask,
6385 				   enum nl80211_band band)
6386 {
6387 	struct ath12k_sta *ahsta = arsta->ahsta;
6388 	struct ath12k *ar = arvif->ar;
6389 	struct ieee80211_sta *sta;
6390 	struct ieee80211_link_sta *link_sta;
6391 	u8 eht_rate, nss = 0;
6392 	u32 rate_code;
6393 	int ret, i;
6394 
6395 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
6396 
6397 	sta = ath12k_ahsta_to_sta(ahsta);
6398 
6399 	for (i = 0; i < ARRAY_SIZE(mask->control[band].eht_mcs); i++) {
6400 		if (hweight16(mask->control[band].eht_mcs[i]) == 1) {
6401 			nss = i + 1;
6402 			eht_rate = ffs(mask->control[band].eht_mcs[i]) - 1;
6403 		}
6404 	}
6405 
6406 	if (!nss) {
6407 		ath12k_warn(ar->ab, "No single EHT Fixed rate found to set for %pM\n",
6408 			    arsta->addr);
6409 		return -EINVAL;
6410 	}
6411 
6412 	/* Avoid updating invalid nss as fixed rate*/
6413 	link_sta = ath12k_mac_get_link_sta(arsta);
6414 	if (!link_sta || nss > link_sta->rx_nss) {
6415 		ath12k_warn(ar->ab,
6416 			    "unable to access link sta for sta %pM link %u or fixed nss of %u is not supported by sta\n",
6417 			    sta->addr, arsta->link_id, nss);
6418 		return -EINVAL;
6419 	}
6420 
6421 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
6422 		   "Setting Fixed EHT Rate for peer %pM. Device will not switch to any other selected rates\n",
6423 		   arsta->addr);
6424 
6425 	rate_code = ATH12K_HW_RATE_CODE(eht_rate, nss - 1,
6426 					WMI_RATE_PREAMBLE_EHT);
6427 
6428 	ret = ath12k_wmi_set_peer_param(ar, arsta->addr,
6429 					arvif->vdev_id,
6430 					WMI_PEER_PARAM_FIXED_RATE,
6431 					rate_code);
6432 	if (ret)
6433 		ath12k_warn(ar->ab,
6434 			    "failed to update STA %pM Fixed Rate %d: %d\n",
6435 			    arsta->addr, rate_code, ret);
6436 
6437 	return ret;
6438 }
6439 
6440 static int ath12k_mac_station_assoc(struct ath12k *ar,
6441 				    struct ath12k_link_vif *arvif,
6442 				    struct ath12k_link_sta *arsta,
6443 				    bool reassoc)
6444 {
6445 	struct ieee80211_vif *vif = ath12k_ahvif_to_vif(arvif->ahvif);
6446 	struct ieee80211_sta *sta = ath12k_ahsta_to_sta(arsta->ahsta);
6447 	struct ieee80211_link_sta *link_sta;
6448 	int ret;
6449 	struct cfg80211_chan_def def;
6450 	enum nl80211_band band;
6451 	struct cfg80211_bitrate_mask *mask;
6452 	u8 num_vht_rates, num_he_rates, num_eht_rates;
6453 	u8 link_id = arvif->link_id;
6454 
6455 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
6456 
6457 	if (WARN_ON(ath12k_mac_vif_link_chan(vif, arvif->link_id, &def)))
6458 		return -EPERM;
6459 
6460 	if (WARN_ON(!rcu_access_pointer(sta->link[link_id])))
6461 		return -EINVAL;
6462 
6463 	band = def.chan->band;
6464 	mask = &arvif->bitrate_mask;
6465 
6466 	struct ath12k_wmi_peer_assoc_arg *peer_arg __free(kfree) =
6467 		kzalloc_obj(*peer_arg);
6468 	if (!peer_arg)
6469 		return -ENOMEM;
6470 
6471 	ath12k_peer_assoc_prepare(ar, arvif, arsta, peer_arg, reassoc);
6472 
6473 	if (peer_arg->peer_nss < 1) {
6474 		ath12k_warn(ar->ab,
6475 			    "invalid peer NSS %d\n", peer_arg->peer_nss);
6476 		return -EINVAL;
6477 	}
6478 
6479 	peer_arg->is_assoc = true;
6480 	ret = ath12k_wmi_send_peer_assoc_cmd(ar, peer_arg);
6481 	if (ret) {
6482 		ath12k_warn(ar->ab, "failed to run peer assoc for STA %pM vdev %i: %d\n",
6483 			    arsta->addr, arvif->vdev_id, ret);
6484 		return ret;
6485 	}
6486 
6487 	if (!wait_for_completion_timeout(&ar->peer_assoc_done, 1 * HZ)) {
6488 		ath12k_warn(ar->ab, "failed to get peer assoc conf event for %pM vdev %i\n",
6489 			    arsta->addr, arvif->vdev_id);
6490 		return -ETIMEDOUT;
6491 	}
6492 
6493 	num_vht_rates = ath12k_mac_bitrate_mask_num_vht_rates(ar, band, mask);
6494 	num_he_rates = ath12k_mac_bitrate_mask_num_he_rates(ar, band, mask);
6495 	num_eht_rates = ath12k_mac_bitrate_mask_num_eht_rates(ar, band, mask);
6496 
6497 	/* If single VHT/HE/EHT rate is configured (by set_bitrate_mask()),
6498 	 * peer_assoc will disable VHT/HE/EHT. This is now enabled by a peer
6499 	 * specific fixed param.
6500 	 * Note that all other rates and NSS will be disabled for this peer.
6501 	 */
6502 	link_sta = ath12k_mac_get_link_sta(arsta);
6503 	if (!link_sta) {
6504 		ath12k_warn(ar->ab, "unable to access link sta in station assoc\n");
6505 		return -EINVAL;
6506 	}
6507 
6508 	spin_lock_bh(&ar->data_lock);
6509 	arsta->bw = ath12k_mac_ieee80211_sta_bw_to_wmi(ar, link_sta);
6510 	arsta->bw_prev = link_sta->bandwidth;
6511 	spin_unlock_bh(&ar->data_lock);
6512 
6513 	if (link_sta->vht_cap.vht_supported && num_vht_rates == 1) {
6514 		ret = ath12k_mac_set_peer_vht_fixed_rate(arvif, arsta, mask, band);
6515 	} else if (link_sta->he_cap.has_he && num_he_rates == 1) {
6516 		ret = ath12k_mac_set_peer_he_fixed_rate(arvif, arsta, mask, band);
6517 		if (ret)
6518 			return ret;
6519 	} else if (link_sta->eht_cap.has_eht && num_eht_rates == 1) {
6520 		ret = ath12k_mac_set_peer_eht_fixed_rate(arvif, arsta, mask, band);
6521 		if (ret)
6522 			return ret;
6523 	}
6524 
6525 	/* Re-assoc is run only to update supported rates for given station. It
6526 	 * doesn't make much sense to reconfigure the peer completely.
6527 	 */
6528 	if (reassoc)
6529 		return 0;
6530 
6531 	ret = ath12k_setup_peer_smps(ar, arvif, arsta->addr,
6532 				     &link_sta->ht_cap, &link_sta->he_6ghz_capa);
6533 	if (ret) {
6534 		ath12k_warn(ar->ab, "failed to setup peer SMPS for vdev %d: %d\n",
6535 			    arvif->vdev_id, ret);
6536 		return ret;
6537 	}
6538 
6539 	if (!sta->wme) {
6540 		arvif->num_legacy_stations++;
6541 		ret = ath12k_recalc_rtscts_prot(arvif);
6542 		if (ret)
6543 			return ret;
6544 	}
6545 
6546 	if (sta->wme && sta->uapsd_queues) {
6547 		ret = ath12k_peer_assoc_qos_ap(ar, arvif, arsta);
6548 		if (ret) {
6549 			ath12k_warn(ar->ab, "failed to set qos params for STA %pM for vdev %i: %d\n",
6550 				    arsta->addr, arvif->vdev_id, ret);
6551 			return ret;
6552 		}
6553 	}
6554 
6555 	return 0;
6556 }
6557 
6558 static int ath12k_mac_station_disassoc(struct ath12k *ar,
6559 				       struct ath12k_link_vif *arvif,
6560 				       struct ath12k_link_sta *arsta)
6561 {
6562 	struct ieee80211_sta *sta = ath12k_ahsta_to_sta(arsta->ahsta);
6563 
6564 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
6565 
6566 	if (!sta->wme) {
6567 		arvif->num_legacy_stations--;
6568 		return ath12k_recalc_rtscts_prot(arvif);
6569 	}
6570 
6571 	return 0;
6572 }
6573 
6574 static int ath12k_mac_sta_set_4addr(struct wiphy *wiphy, struct ath12k_sta *ahsta)
6575 {
6576 	struct ath12k_dp_link_peer *peer;
6577 	struct ath12k_link_vif *arvif;
6578 	struct ath12k_link_sta *arsta;
6579 	struct ath12k_vif *ahvif;
6580 	struct ath12k_dp *dp;
6581 	unsigned long links;
6582 	struct ath12k *ar;
6583 	u8 link_id;
6584 	int ret;
6585 
6586 	links = ahsta->links_map;
6587 	for_each_set_bit(link_id, &links, IEEE80211_MLD_MAX_NUM_LINKS) {
6588 		arsta = wiphy_dereference(wiphy, ahsta->link[link_id]);
6589 		if (!arsta)
6590 			continue;
6591 
6592 		arvif = arsta->arvif;
6593 		ahvif = arvif->ahvif;
6594 		ar = arvif->ar;
6595 
6596 		if (arvif->set_wds_vdev_param)
6597 			goto skip_nawds;
6598 
6599 		ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
6600 			   "setting USE_4ADDR for peer %pM\n", arsta->addr);
6601 
6602 		ret = ath12k_wmi_set_peer_param(ar, arsta->addr,
6603 						arvif->vdev_id,
6604 						WMI_PEER_USE_4ADDR,
6605 						WMI_PEER_4ADDR_ALLOW_EAPOL_DATA_FRAME);
6606 		if (ret) {
6607 			ath12k_warn(ar->ab, "failed to set peer %pM 4addr capability: %d\n",
6608 				    arsta->addr, ret);
6609 			return ret;
6610 		}
6611 
6612 		if (ahvif->dp_vif.tx_encap_type != ATH12K_HW_TXRX_ETHERNET)
6613 			goto skip_nawds;
6614 
6615 		ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
6616 						    WMI_VDEV_PARAM_AP_ENABLE_NAWDS,
6617 						    WDS_EXT_ENABLE);
6618 		if (ret) {
6619 			ath12k_warn(ar->ab, "failed to set vdev %d nawds parameter: %d\n",
6620 				    arvif->vdev_id, ret);
6621 			return ret;
6622 		}
6623 
6624 		arvif->nawds_enabled = true;
6625 
6626 skip_nawds:
6627 		dp = ath12k_ab_to_dp(ar->ab);
6628 		spin_lock_bh(&dp->dp_lock);
6629 		peer = ath12k_dp_link_peer_find_by_vdev_and_addr(dp, arvif->vdev_id,
6630 								 arsta->addr);
6631 		if (peer && peer->dp_peer) {
6632 			peer->dp_peer->ucast_ra_only = true;
6633 			peer->dp_peer->use_4addr = true;
6634 		} else {
6635 			spin_unlock_bh(&dp->dp_lock);
6636 			ath12k_warn(ar->ab, "failed to find DP peer for %pM\n",
6637 				    arsta->addr);
6638 			return -ENOENT;
6639 		}
6640 
6641 		spin_unlock_bh(&dp->dp_lock);
6642 	}
6643 
6644 	return 0;
6645 }
6646 
6647 static void ath12k_sta_rc_update_wk(struct wiphy *wiphy, struct wiphy_work *wk)
6648 {
6649 	struct ieee80211_link_sta *link_sta;
6650 	struct ath12k *ar;
6651 	struct ath12k_link_vif *arvif;
6652 	struct ieee80211_sta *sta;
6653 	struct cfg80211_chan_def def;
6654 	enum nl80211_band band;
6655 	const u8 *ht_mcs_mask;
6656 	const u16 *vht_mcs_mask;
6657 	const u16 *he_mcs_mask;
6658 	const u16 *eht_mcs_mask;
6659 	u32 changed, bw, nss, mac_nss, smps, bw_prev;
6660 	int err, num_vht_rates, num_he_rates, num_eht_rates;
6661 	const struct cfg80211_bitrate_mask *mask;
6662 	enum wmi_phy_mode peer_phymode;
6663 	struct ath12k_link_sta *arsta;
6664 	struct ieee80211_vif *vif;
6665 
6666 	lockdep_assert_wiphy(wiphy);
6667 
6668 	arsta = container_of(wk, struct ath12k_link_sta, update_wk);
6669 	sta = ath12k_ahsta_to_sta(arsta->ahsta);
6670 	arvif = arsta->arvif;
6671 	vif = ath12k_ahvif_to_vif(arvif->ahvif);
6672 	ar = arvif->ar;
6673 
6674 	if (WARN_ON(ath12k_mac_vif_link_chan(vif, arvif->link_id, &def)))
6675 		return;
6676 
6677 	band = def.chan->band;
6678 	ht_mcs_mask = arvif->bitrate_mask.control[band].ht_mcs;
6679 	vht_mcs_mask = arvif->bitrate_mask.control[band].vht_mcs;
6680 	he_mcs_mask = arvif->bitrate_mask.control[band].he_mcs;
6681 	eht_mcs_mask = arvif->bitrate_mask.control[band].eht_mcs;
6682 
6683 	spin_lock_bh(&ar->data_lock);
6684 
6685 	changed = arsta->changed;
6686 	arsta->changed = 0;
6687 
6688 	bw = arsta->bw;
6689 	bw_prev = arsta->bw_prev;
6690 	nss = arsta->nss;
6691 	smps = arsta->smps;
6692 
6693 	spin_unlock_bh(&ar->data_lock);
6694 
6695 	nss = max_t(u32, 1, nss);
6696 	mac_nss = max3(ath12k_mac_max_ht_nss(ht_mcs_mask),
6697 		       ath12k_mac_max_vht_nss(vht_mcs_mask),
6698 		       ath12k_mac_max_he_nss(he_mcs_mask));
6699 	mac_nss = max(mac_nss, ath12k_mac_max_eht_nss(eht_mcs_mask));
6700 	nss = min(nss, mac_nss);
6701 
6702 	struct ath12k_wmi_peer_assoc_arg *peer_arg __free(kfree) =
6703 					kzalloc_obj(*peer_arg);
6704 	if (!peer_arg)
6705 		return;
6706 
6707 	if (changed & IEEE80211_RC_BW_CHANGED) {
6708 		ath12k_peer_assoc_h_phymode(ar, arvif, arsta, peer_arg);
6709 		peer_phymode = peer_arg->peer_phymode;
6710 
6711 		if (bw > bw_prev) {
6712 			/* Phymode shows maximum supported channel width, if we
6713 			 * upgrade bandwidth then due to sanity check of firmware,
6714 			 * we have to send WMI_PEER_PHYMODE followed by
6715 			 * WMI_PEER_CHWIDTH
6716 			 */
6717 			ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac bandwidth upgrade for sta %pM new %d old %d\n",
6718 				   arsta->addr, bw, bw_prev);
6719 			err = ath12k_wmi_set_peer_param(ar, arsta->addr,
6720 							arvif->vdev_id, WMI_PEER_PHYMODE,
6721 							peer_phymode);
6722 			if (err) {
6723 				ath12k_warn(ar->ab, "failed to update STA %pM to peer phymode %d: %d\n",
6724 					    arsta->addr, peer_phymode, err);
6725 				return;
6726 			}
6727 			err = ath12k_wmi_set_peer_param(ar, arsta->addr,
6728 							arvif->vdev_id, WMI_PEER_CHWIDTH,
6729 							bw);
6730 			if (err)
6731 				ath12k_warn(ar->ab, "failed to update STA %pM to peer bandwidth %d: %d\n",
6732 					    arsta->addr, bw, err);
6733 		} else {
6734 			/* When we downgrade bandwidth this will conflict with phymode
6735 			 * and cause to trigger firmware crash. In this case we send
6736 			 * WMI_PEER_CHWIDTH followed by WMI_PEER_PHYMODE
6737 			 */
6738 			ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac bandwidth downgrade for sta %pM new %d old %d\n",
6739 				   arsta->addr, bw, bw_prev);
6740 			err = ath12k_wmi_set_peer_param(ar, arsta->addr,
6741 							arvif->vdev_id, WMI_PEER_CHWIDTH,
6742 							bw);
6743 			if (err) {
6744 				ath12k_warn(ar->ab, "failed to update STA %pM peer to bandwidth %d: %d\n",
6745 					    arsta->addr, bw, err);
6746 				return;
6747 			}
6748 			err = ath12k_wmi_set_peer_param(ar, arsta->addr,
6749 							arvif->vdev_id, WMI_PEER_PHYMODE,
6750 							peer_phymode);
6751 			if (err)
6752 				ath12k_warn(ar->ab, "failed to update STA %pM to peer phymode %d: %d\n",
6753 					    arsta->addr, peer_phymode, err);
6754 		}
6755 	}
6756 
6757 	if (changed & IEEE80211_RC_NSS_CHANGED) {
6758 		ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac update sta %pM nss %d\n",
6759 			   arsta->addr, nss);
6760 
6761 		err = ath12k_wmi_set_peer_param(ar, arsta->addr, arvif->vdev_id,
6762 						WMI_PEER_NSS, nss);
6763 		if (err)
6764 			ath12k_warn(ar->ab, "failed to update STA %pM nss %d: %d\n",
6765 				    arsta->addr, nss, err);
6766 	}
6767 
6768 	if (changed & IEEE80211_RC_SMPS_CHANGED) {
6769 		ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac update sta %pM smps %d\n",
6770 			   arsta->addr, smps);
6771 
6772 		err = ath12k_wmi_set_peer_param(ar, arsta->addr, arvif->vdev_id,
6773 						WMI_PEER_MIMO_PS_STATE, smps);
6774 		if (err)
6775 			ath12k_warn(ar->ab, "failed to update STA %pM smps %d: %d\n",
6776 				    arsta->addr, smps, err);
6777 	}
6778 
6779 	if (changed & IEEE80211_RC_SUPP_RATES_CHANGED) {
6780 		mask = &arvif->bitrate_mask;
6781 		num_vht_rates = ath12k_mac_bitrate_mask_num_vht_rates(ar, band,
6782 								      mask);
6783 		num_he_rates = ath12k_mac_bitrate_mask_num_he_rates(ar, band,
6784 								    mask);
6785 		num_eht_rates = ath12k_mac_bitrate_mask_num_eht_rates(ar, band,
6786 								      mask);
6787 
6788 		/* Peer_assoc_prepare will reject vht rates in
6789 		 * bitrate_mask if its not available in range format and
6790 		 * sets vht tx_rateset as unsupported. So multiple VHT MCS
6791 		 * setting(eg. MCS 4,5,6) per peer is not supported here.
6792 		 * But, Single rate in VHT mask can be set as per-peer
6793 		 * fixed rate. But even if any HT rates are configured in
6794 		 * the bitrate mask, device will not switch to those rates
6795 		 * when per-peer Fixed rate is set.
6796 		 * TODO: Check RATEMASK_CMDID to support auto rates selection
6797 		 * across HT/VHT and for multiple VHT MCS support.
6798 		 */
6799 		link_sta = ath12k_mac_get_link_sta(arsta);
6800 		if (!link_sta) {
6801 			ath12k_warn(ar->ab, "unable to access link sta in peer assoc he for sta %pM link %u\n",
6802 				    sta->addr, arsta->link_id);
6803 			return;
6804 		}
6805 
6806 		if (link_sta->vht_cap.vht_supported && num_vht_rates == 1) {
6807 			ath12k_mac_set_peer_vht_fixed_rate(arvif, arsta, mask,
6808 							   band);
6809 		} else if (link_sta->he_cap.has_he && num_he_rates == 1) {
6810 			ath12k_mac_set_peer_he_fixed_rate(arvif, arsta, mask, band);
6811 		} else if (link_sta->eht_cap.has_eht && num_eht_rates == 1) {
6812 			err = ath12k_mac_set_peer_eht_fixed_rate(arvif, arsta,
6813 								 mask, band);
6814 			if (err) {
6815 				ath12k_warn(ar->ab,
6816 					    "failed to set peer EHT fixed rate for STA %pM ret %d\n",
6817 					    arsta->addr, err);
6818 				return;
6819 			}
6820 		} else {
6821 			/* If the peer is non-VHT/HE/EHT or no fixed VHT/HE/EHT
6822 			 * rate is provided in the new bitrate mask we set the
6823 			 * other rates using peer_assoc command. Also clear
6824 			 * the peer fixed rate settings as it has higher proprity
6825 			 * than peer assoc
6826 			 */
6827 			err = ath12k_wmi_set_peer_param(ar, arsta->addr,
6828 							arvif->vdev_id,
6829 							WMI_PEER_PARAM_FIXED_RATE,
6830 							WMI_FIXED_RATE_NONE);
6831 			if (err)
6832 				ath12k_warn(ar->ab,
6833 					    "failed to disable peer fixed rate for STA %pM ret %d\n",
6834 					    arsta->addr, err);
6835 
6836 			ath12k_peer_assoc_prepare(ar, arvif, arsta,
6837 						  peer_arg, true);
6838 
6839 			peer_arg->is_assoc = false;
6840 			err = ath12k_wmi_send_peer_assoc_cmd(ar, peer_arg);
6841 			if (err)
6842 				ath12k_warn(ar->ab, "failed to run peer assoc for STA %pM vdev %i: %d\n",
6843 					    arsta->addr, arvif->vdev_id, err);
6844 
6845 			if (!wait_for_completion_timeout(&ar->peer_assoc_done, 1 * HZ))
6846 				ath12k_warn(ar->ab, "failed to get peer assoc conf event for %pM vdev %i\n",
6847 					    arsta->addr, arvif->vdev_id);
6848 		}
6849 	}
6850 }
6851 
6852 static void ath12k_mac_free_unassign_link_sta(struct ath12k_hw *ah,
6853 					      struct ath12k_sta *ahsta,
6854 					      u8 link_id)
6855 {
6856 	struct ath12k_link_sta *arsta;
6857 
6858 	lockdep_assert_wiphy(ah->hw->wiphy);
6859 
6860 	if (WARN_ON(link_id >= IEEE80211_MLD_MAX_NUM_LINKS))
6861 		return;
6862 
6863 	arsta = wiphy_dereference(ah->hw->wiphy, ahsta->link[link_id]);
6864 	if (WARN_ON(!arsta))
6865 		return;
6866 
6867 	ahsta->links_map &= ~BIT(link_id);
6868 	ahsta->free_logical_link_idx_map |= BIT(arsta->link_idx);
6869 
6870 	rcu_assign_pointer(ahsta->link[link_id], NULL);
6871 	synchronize_rcu();
6872 
6873 	if (arsta == &ahsta->deflink) {
6874 		arsta->link_id = ATH12K_INVALID_LINK_ID;
6875 		arsta->ahsta = NULL;
6876 		arsta->arvif = NULL;
6877 		return;
6878 	}
6879 
6880 	kfree(arsta);
6881 }
6882 
6883 static int ath12k_mac_inc_num_stations(struct ath12k_link_vif *arvif,
6884 				       struct ath12k_link_sta *arsta)
6885 {
6886 	struct ieee80211_sta *sta = ath12k_ahsta_to_sta(arsta->ahsta);
6887 	struct ath12k *ar = arvif->ar;
6888 
6889 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
6890 
6891 	if (arvif->ahvif->vdev_type == WMI_VDEV_TYPE_STA && !sta->tdls)
6892 		return 0;
6893 
6894 	if (ar->num_stations >= ar->max_num_stations)
6895 		return -ENOBUFS;
6896 
6897 	ar->num_stations++;
6898 	arvif->num_stations++;
6899 
6900 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
6901 		   "mac station %pM connected to vdev %u num_stations %u\n",
6902 		   arsta->addr, arvif->vdev_id, arvif->num_stations);
6903 
6904 	return 0;
6905 }
6906 
6907 static void ath12k_mac_dec_num_stations(struct ath12k_link_vif *arvif,
6908 					struct ath12k_link_sta *arsta)
6909 {
6910 	struct ieee80211_sta *sta = ath12k_ahsta_to_sta(arsta->ahsta);
6911 	struct ath12k *ar = arvif->ar;
6912 
6913 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
6914 
6915 	if (arvif->ahvif->vdev_type == WMI_VDEV_TYPE_STA && !sta->tdls)
6916 		return;
6917 
6918 	ar->num_stations--;
6919 
6920 	if (arvif->num_stations) {
6921 		arvif->num_stations--;
6922 		ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
6923 			   "mac station %pM disconnected from vdev %u num_stations %u\n",
6924 			   arsta->addr, arvif->vdev_id, arvif->num_stations);
6925 	} else {
6926 		ath12k_warn(ar->ab,
6927 			    "mac station %pM disconnect for vdev %u without any connected station\n",
6928 			    arsta->addr, arvif->vdev_id);
6929 	}
6930 }
6931 
6932 static void ath12k_mac_station_post_remove(struct ath12k *ar,
6933 					   struct ath12k_link_vif *arvif,
6934 					   struct ath12k_link_sta *arsta)
6935 {
6936 	struct ieee80211_vif *vif = ath12k_ahvif_to_vif(arvif->ahvif);
6937 	struct ieee80211_sta *sta = ath12k_ahsta_to_sta(arsta->ahsta);
6938 	struct ath12k_dp_link_peer *peer;
6939 	struct ath12k_dp *dp = ath12k_ab_to_dp(ar->ab);
6940 
6941 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
6942 
6943 	ath12k_mac_dec_num_stations(arvif, arsta);
6944 
6945 	spin_lock_bh(&dp->dp_lock);
6946 
6947 	peer = ath12k_dp_link_peer_find_by_vdev_and_addr(dp, arvif->vdev_id,
6948 							 arsta->addr);
6949 	if (peer && peer->sta == sta) {
6950 		ath12k_warn(ar->ab, "Found peer entry %pM n vdev %i after it was supposedly removed\n",
6951 			    vif->addr, arvif->vdev_id);
6952 		peer->sta = NULL;
6953 
6954 		ath12k_dp_link_peer_free(peer);
6955 		ar->num_peers--;
6956 	}
6957 
6958 	spin_unlock_bh(&dp->dp_lock);
6959 }
6960 
6961 static int ath12k_mac_station_unauthorize(struct ath12k *ar,
6962 					  struct ath12k_link_vif *arvif,
6963 					  struct ath12k_link_sta *arsta)
6964 {
6965 	struct ath12k_dp_link_peer *peer;
6966 	int ret;
6967 	struct ath12k_dp *dp = ath12k_ab_to_dp(ar->ab);
6968 
6969 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
6970 
6971 	spin_lock_bh(&dp->dp_lock);
6972 
6973 	peer = ath12k_dp_link_peer_find_by_vdev_and_addr(dp, arvif->vdev_id,
6974 							 arsta->addr);
6975 	if (peer)
6976 		peer->is_authorized = false;
6977 
6978 	spin_unlock_bh(&dp->dp_lock);
6979 
6980 	/* Driver must clear the keys during the state change from
6981 	 * IEEE80211_STA_AUTHORIZED to IEEE80211_STA_ASSOC, since after
6982 	 * returning from here, mac80211 is going to delete the keys
6983 	 * in __sta_info_destroy_part2(). This will ensure that the driver does
6984 	 * not retain stale key references after mac80211 deletes the keys.
6985 	 */
6986 	ret = ath12k_clear_peer_keys(arvif, arsta->addr);
6987 	if (ret) {
6988 		ath12k_warn(ar->ab, "failed to clear all peer keys for vdev %i: %d\n",
6989 			    arvif->vdev_id, ret);
6990 		return ret;
6991 	}
6992 
6993 	return 0;
6994 }
6995 
6996 static int ath12k_mac_station_authorize(struct ath12k *ar,
6997 					struct ath12k_link_vif *arvif,
6998 					struct ath12k_link_sta *arsta)
6999 {
7000 	struct ath12k_dp_link_peer *peer;
7001 	struct ieee80211_vif *vif = ath12k_ahvif_to_vif(arvif->ahvif);
7002 	int ret;
7003 	struct ath12k_dp *dp = ath12k_ab_to_dp(ar->ab);
7004 
7005 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
7006 
7007 	spin_lock_bh(&dp->dp_lock);
7008 
7009 	peer = ath12k_dp_link_peer_find_by_vdev_and_addr(dp, arvif->vdev_id,
7010 							 arsta->addr);
7011 	if (peer)
7012 		peer->is_authorized = true;
7013 
7014 	spin_unlock_bh(&dp->dp_lock);
7015 
7016 	if (vif->type == NL80211_IFTYPE_STATION && arvif->is_up) {
7017 		ret = ath12k_wmi_set_peer_param(ar, arsta->addr,
7018 						arvif->vdev_id,
7019 						WMI_PEER_AUTHORIZE,
7020 						1);
7021 		if (ret) {
7022 			ath12k_warn(ar->ab, "Unable to authorize peer %pM vdev %d: %d\n",
7023 				    arsta->addr, arvif->vdev_id, ret);
7024 			return ret;
7025 		}
7026 	}
7027 
7028 	return 0;
7029 }
7030 
7031 static int ath12k_mac_station_remove(struct ath12k *ar,
7032 				     struct ath12k_link_vif *arvif,
7033 				     struct ath12k_link_sta *arsta)
7034 {
7035 	struct ieee80211_sta *sta = ath12k_ahsta_to_sta(arsta->ahsta);
7036 	struct ath12k_vif *ahvif = arvif->ahvif;
7037 	int ret = 0;
7038 	struct ath12k_link_sta *temp_arsta;
7039 
7040 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
7041 
7042 	wiphy_work_cancel(ar->ah->hw->wiphy, &arsta->update_wk);
7043 
7044 	if (ahvif->vdev_type == WMI_VDEV_TYPE_STA) {
7045 		ath12k_bss_disassoc(ar, arvif);
7046 		ret = ath12k_mac_vdev_stop(arvif);
7047 		if (ret)
7048 			ath12k_warn(ar->ab, "failed to stop vdev %i: %d\n",
7049 				    arvif->vdev_id, ret);
7050 	}
7051 
7052 	if (sta->mlo)
7053 		return ret;
7054 
7055 	ath12k_dp_peer_cleanup(ar, arvif->vdev_id, arsta->addr);
7056 
7057 	ret = ath12k_peer_delete(ar, arvif->vdev_id, arsta->addr);
7058 	if (ret)
7059 		ath12k_warn(ar->ab, "Failed to delete peer: %pM for VDEV: %d\n",
7060 			    arsta->addr, arvif->vdev_id);
7061 	else
7062 		ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "Removed peer: %pM for VDEV: %d\n",
7063 			   arsta->addr, arvif->vdev_id);
7064 
7065 	ath12k_mac_station_post_remove(ar, arvif, arsta);
7066 
7067 	spin_lock_bh(&ar->ab->base_lock);
7068 
7069 	/* To handle roaming and split phy scenario */
7070 	temp_arsta = ath12k_link_sta_find_by_addr(ar->ab, arsta->addr);
7071 	if (temp_arsta && temp_arsta->arvif->ar == ar)
7072 		ath12k_link_sta_rhash_delete(ar->ab, arsta);
7073 
7074 	spin_unlock_bh(&ar->ab->base_lock);
7075 
7076 	if (sta->valid_links)
7077 		ath12k_mac_free_unassign_link_sta(ahvif->ah,
7078 						  arsta->ahsta, arsta->link_id);
7079 
7080 	return ret;
7081 }
7082 
7083 static int ath12k_mac_station_add(struct ath12k *ar,
7084 				  struct ath12k_link_vif *arvif,
7085 				  struct ath12k_link_sta *arsta)
7086 {
7087 	struct ath12k_base *ab = ar->ab;
7088 	struct ieee80211_vif *vif = ath12k_ahvif_to_vif(arvif->ahvif);
7089 	struct ieee80211_sta *sta = ath12k_ahsta_to_sta(arsta->ahsta);
7090 	struct ath12k_wmi_peer_create_arg peer_param = {};
7091 	int ret;
7092 	struct ath12k_link_sta *temp_arsta;
7093 
7094 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
7095 
7096 	ret = ath12k_mac_inc_num_stations(arvif, arsta);
7097 	if (ret) {
7098 		ath12k_warn(ab, "refusing to associate station: too many connected already (%d)\n",
7099 			    ar->max_num_stations);
7100 		goto exit;
7101 	}
7102 
7103 	spin_lock_bh(&ab->base_lock);
7104 
7105 	/*
7106 	 * In case of Split PHY and roaming scenario, pdev idx
7107 	 * might differ but both the pdev will share same rhash
7108 	 * table. In that case update the rhash table if link_sta is
7109 	 * already present
7110 	 */
7111 	temp_arsta = ath12k_link_sta_find_by_addr(ab, arsta->addr);
7112 	if (temp_arsta && temp_arsta->arvif->ar != ar)
7113 		ath12k_link_sta_rhash_delete(ab, temp_arsta);
7114 
7115 	ret = ath12k_link_sta_rhash_add(ab, arsta);
7116 	spin_unlock_bh(&ab->base_lock);
7117 	if (ret) {
7118 		ath12k_warn(ab, "Failed to add arsta: %pM to hash table, ret: %d",
7119 			    arsta->addr, ret);
7120 		goto dec_num_station;
7121 	}
7122 
7123 	peer_param.vdev_id = arvif->vdev_id;
7124 	peer_param.peer_addr = arsta->addr;
7125 	peer_param.peer_type = WMI_PEER_TYPE_DEFAULT;
7126 	peer_param.ml_enabled = sta->mlo;
7127 
7128 	ret = ath12k_peer_create(ar, arvif, sta, &peer_param);
7129 	if (ret) {
7130 		ath12k_warn(ab, "Failed to add peer: %pM for VDEV: %d\n",
7131 			    arsta->addr, arvif->vdev_id);
7132 		goto free_peer;
7133 	}
7134 
7135 	ath12k_dbg(ab, ATH12K_DBG_MAC, "Added peer: %pM for VDEV: %d\n",
7136 		   arsta->addr, arvif->vdev_id);
7137 
7138 	if (ieee80211_vif_is_mesh(vif)) {
7139 		ret = ath12k_wmi_set_peer_param(ar, arsta->addr,
7140 						arvif->vdev_id,
7141 						WMI_PEER_USE_4ADDR, 1);
7142 		if (ret) {
7143 			ath12k_warn(ab, "failed to STA %pM 4addr capability: %d\n",
7144 				    arsta->addr, ret);
7145 			goto free_peer;
7146 		}
7147 	}
7148 
7149 	ret = ath12k_dp_peer_setup(ar, arvif->vdev_id, arsta->addr);
7150 	if (ret) {
7151 		ath12k_warn(ab, "failed to setup dp for peer %pM on vdev %i (%d)\n",
7152 			    arsta->addr, arvif->vdev_id, ret);
7153 		goto free_peer;
7154 	}
7155 
7156 	if (ab->hw_params->vdev_start_delay &&
7157 	    !arvif->is_started &&
7158 	    arvif->ahvif->vdev_type != WMI_VDEV_TYPE_AP) {
7159 		ret = ath12k_start_vdev_delay(ar, arvif);
7160 		if (ret) {
7161 			ath12k_warn(ab, "failed to delay vdev start: %d\n", ret);
7162 			goto free_peer;
7163 		}
7164 	}
7165 
7166 	return 0;
7167 
7168 free_peer:
7169 	ath12k_peer_delete(ar, arvif->vdev_id, arsta->addr);
7170 	spin_lock_bh(&ab->base_lock);
7171 	ath12k_link_sta_rhash_delete(ab, arsta);
7172 	spin_unlock_bh(&ab->base_lock);
7173 dec_num_station:
7174 	ath12k_mac_dec_num_stations(arvif, arsta);
7175 exit:
7176 	return ret;
7177 }
7178 
7179 static int ath12k_mac_assign_link_sta(struct ath12k_hw *ah,
7180 				      struct ath12k_sta *ahsta,
7181 				      struct ath12k_link_sta *arsta,
7182 				      struct ath12k_vif *ahvif,
7183 				      u8 link_id)
7184 {
7185 	struct ieee80211_sta *sta = ath12k_ahsta_to_sta(ahsta);
7186 	struct ieee80211_link_sta *link_sta;
7187 	struct ath12k_link_vif *arvif;
7188 	int link_idx;
7189 
7190 	lockdep_assert_wiphy(ah->hw->wiphy);
7191 
7192 	if (!arsta || link_id >= IEEE80211_MLD_MAX_NUM_LINKS)
7193 		return -EINVAL;
7194 
7195 	arvif = wiphy_dereference(ah->hw->wiphy, ahvif->link[link_id]);
7196 	if (!arvif)
7197 		return -EINVAL;
7198 
7199 	memset(arsta, 0, sizeof(*arsta));
7200 
7201 	link_sta = wiphy_dereference(ah->hw->wiphy, sta->link[link_id]);
7202 	if (!link_sta)
7203 		return -EINVAL;
7204 
7205 	ether_addr_copy(arsta->addr, link_sta->addr);
7206 
7207 	if (!ahsta->free_logical_link_idx_map)
7208 		return -ENOSPC;
7209 
7210 	/*
7211 	 * Allocate a logical link index by selecting the first available bit
7212 	 * from the free logical index map
7213 	 */
7214 	link_idx = __ffs(ahsta->free_logical_link_idx_map);
7215 	ahsta->free_logical_link_idx_map &= ~BIT(link_idx);
7216 	arsta->link_idx = link_idx;
7217 
7218 	arsta->link_id = link_id;
7219 	ahsta->links_map |= BIT(arsta->link_id);
7220 	arsta->arvif = arvif;
7221 	arsta->ahsta = ahsta;
7222 	ahsta->ahvif = ahvif;
7223 
7224 	wiphy_work_init(&arsta->update_wk, ath12k_sta_rc_update_wk);
7225 
7226 	rcu_assign_pointer(ahsta->link[link_id], arsta);
7227 
7228 	return 0;
7229 }
7230 
7231 static void ath12k_mac_ml_station_remove(struct ath12k_vif *ahvif,
7232 					 struct ath12k_sta *ahsta)
7233 {
7234 	struct ieee80211_sta *sta = ath12k_ahsta_to_sta(ahsta);
7235 	struct ath12k_hw *ah = ahvif->ah;
7236 	struct ath12k_link_vif *arvif;
7237 	struct ath12k_link_sta *arsta;
7238 	unsigned long links;
7239 	struct ath12k *ar;
7240 	u8 link_id;
7241 
7242 	lockdep_assert_wiphy(ah->hw->wiphy);
7243 
7244 	ath12k_peer_mlo_link_peers_delete(ahvif, ahsta);
7245 
7246 	/* validate link station removal and clear arsta links */
7247 	links = ahsta->links_map;
7248 	for_each_set_bit(link_id, &links, IEEE80211_MLD_MAX_NUM_LINKS) {
7249 		arvif = wiphy_dereference(ah->hw->wiphy, ahvif->link[link_id]);
7250 		arsta = wiphy_dereference(ah->hw->wiphy, ahsta->link[link_id]);
7251 		if (!arvif || !arsta)
7252 			continue;
7253 
7254 		ar = arvif->ar;
7255 
7256 		ath12k_mac_station_post_remove(ar, arvif, arsta);
7257 
7258 		spin_lock_bh(&ar->ab->base_lock);
7259 		ath12k_link_sta_rhash_delete(ar->ab, arsta);
7260 		spin_unlock_bh(&ar->ab->base_lock);
7261 
7262 		ath12k_mac_free_unassign_link_sta(ah, ahsta, link_id);
7263 	}
7264 
7265 	if (sta->mlo) {
7266 		clear_bit(ahsta->ml_peer_id, ah->free_ml_peer_id_map);
7267 		ahsta->ml_peer_id = ATH12K_MLO_PEER_ID_INVALID;
7268 	}
7269 }
7270 
7271 static int ath12k_mac_handle_link_sta_state(struct ieee80211_hw *hw,
7272 					    struct ath12k_link_vif *arvif,
7273 					    struct ath12k_link_sta *arsta,
7274 					    enum ieee80211_sta_state old_state,
7275 					    enum ieee80211_sta_state new_state)
7276 {
7277 	struct ieee80211_vif *vif = ath12k_ahvif_to_vif(arvif->ahvif);
7278 	struct ieee80211_bss_conf *link_conf;
7279 	struct ath12k *ar = arvif->ar;
7280 	struct ath12k_reg_info *reg_info;
7281 	struct ath12k_base *ab = ar->ab;
7282 	int ret = 0;
7283 
7284 	lockdep_assert_wiphy(hw->wiphy);
7285 
7286 	ath12k_dbg(ab, ATH12K_DBG_MAC, "mac handle link %u sta %pM state %d -> %d\n",
7287 		   arsta->link_id, arsta->addr, old_state, new_state);
7288 
7289 	/* IEEE80211_STA_NONE -> IEEE80211_STA_NOTEXIST: Remove the station
7290 	 * from driver
7291 	 */
7292 	if ((old_state == IEEE80211_STA_NONE &&
7293 	     new_state == IEEE80211_STA_NOTEXIST)) {
7294 		ret = ath12k_mac_station_remove(ar, arvif, arsta);
7295 		if (ret) {
7296 			ath12k_warn(ab, "Failed to remove station: %pM for VDEV: %d\n",
7297 				    arsta->addr, arvif->vdev_id);
7298 			goto exit;
7299 		}
7300 	}
7301 
7302 	/* IEEE80211_STA_NOTEXIST -> IEEE80211_STA_NONE: Add new station to driver */
7303 	if (old_state == IEEE80211_STA_NOTEXIST &&
7304 	    new_state == IEEE80211_STA_NONE) {
7305 		ret = ath12k_mac_station_add(ar, arvif, arsta);
7306 		if (ret)
7307 			ath12k_warn(ab, "Failed to add station: %pM for VDEV: %d\n",
7308 				    arsta->addr, arvif->vdev_id);
7309 
7310 	/* IEEE80211_STA_AUTH -> IEEE80211_STA_ASSOC: Send station assoc command for
7311 	 * peer associated to AP/Mesh/ADHOC vif type.
7312 	 */
7313 	} else if (old_state == IEEE80211_STA_AUTH &&
7314 		   new_state == IEEE80211_STA_ASSOC &&
7315 		   (vif->type == NL80211_IFTYPE_AP ||
7316 		    vif->type == NL80211_IFTYPE_MESH_POINT ||
7317 		    vif->type == NL80211_IFTYPE_ADHOC)) {
7318 		ret = ath12k_mac_station_assoc(ar, arvif, arsta, false);
7319 		if (ret)
7320 			ath12k_warn(ab, "Failed to associate station: %pM\n",
7321 				    arsta->addr);
7322 
7323 	/* IEEE80211_STA_ASSOC -> IEEE80211_STA_AUTHORIZED: set peer status as
7324 	 * authorized
7325 	 */
7326 	} else if (old_state == IEEE80211_STA_ASSOC &&
7327 		   new_state == IEEE80211_STA_AUTHORIZED) {
7328 		ret = ath12k_mac_station_authorize(ar, arvif, arsta);
7329 		if (ret) {
7330 			ath12k_warn(ab, "Failed to authorize station: %pM\n",
7331 				    arsta->addr);
7332 			goto exit;
7333 		}
7334 
7335 		if (ath12k_wmi_supports_6ghz_cc_ext(ar) &&
7336 		    arvif->ahvif->vdev_type == WMI_VDEV_TYPE_STA) {
7337 			link_conf = ath12k_mac_get_link_bss_conf(arvif);
7338 			reg_info = ab->reg_info[ar->pdev_idx];
7339 			ath12k_dbg(ab, ATH12K_DBG_MAC, "connection done, update reg rules\n");
7340 			ath12k_hw_to_ah(hw)->regd_updated = false;
7341 			ath12k_reg_handle_chan_list(ab, reg_info, arvif->ahvif->vdev_type,
7342 						    link_conf->power_type);
7343 		}
7344 
7345 	/* IEEE80211_STA_AUTHORIZED -> IEEE80211_STA_ASSOC: station may be in removal,
7346 	 * deauthorize it.
7347 	 */
7348 	} else if (old_state == IEEE80211_STA_AUTHORIZED &&
7349 		   new_state == IEEE80211_STA_ASSOC) {
7350 		ath12k_mac_station_unauthorize(ar, arvif, arsta);
7351 
7352 	/* IEEE80211_STA_ASSOC -> IEEE80211_STA_AUTH: disassoc peer connected to
7353 	 * AP/mesh/ADHOC vif type.
7354 	 */
7355 	} else if (old_state == IEEE80211_STA_ASSOC &&
7356 		   new_state == IEEE80211_STA_AUTH &&
7357 		   (vif->type == NL80211_IFTYPE_AP ||
7358 		    vif->type == NL80211_IFTYPE_MESH_POINT ||
7359 		    vif->type == NL80211_IFTYPE_ADHOC)) {
7360 		ret = ath12k_mac_station_disassoc(ar, arvif, arsta);
7361 		if (ret)
7362 			ath12k_warn(ab, "Failed to disassociate station: %pM\n",
7363 				    arsta->addr);
7364 	}
7365 
7366 exit:
7367 	return ret;
7368 }
7369 
7370 static bool ath12k_mac_is_freq_on_mac(struct ath12k_hw_mode_freq_range_arg *freq_range,
7371 				      u32 freq, u8 mac_id)
7372 {
7373 	return (freq >= freq_range[mac_id].low_2ghz_freq &&
7374 		freq <= freq_range[mac_id].high_2ghz_freq) ||
7375 	       (freq >= freq_range[mac_id].low_5ghz_freq &&
7376 		freq <= freq_range[mac_id].high_5ghz_freq);
7377 }
7378 
7379 static bool
7380 ath12k_mac_2_freq_same_mac_in_freq_range(struct ath12k_base *ab,
7381 					 struct ath12k_hw_mode_freq_range_arg *freq_range,
7382 					 u32 freq_link1, u32 freq_link2)
7383 {
7384 	u8 i;
7385 
7386 	for (i = 0; i < MAX_RADIOS; i++) {
7387 		if (ath12k_mac_is_freq_on_mac(freq_range, freq_link1, i) &&
7388 		    ath12k_mac_is_freq_on_mac(freq_range, freq_link2, i))
7389 			return true;
7390 	}
7391 
7392 	return false;
7393 }
7394 
7395 static bool ath12k_mac_is_hw_dbs_capable(struct ath12k_base *ab)
7396 {
7397 	return test_bit(WMI_TLV_SERVICE_DUAL_BAND_SIMULTANEOUS_SUPPORT,
7398 			ab->wmi_ab.svc_map) &&
7399 	       ab->wmi_ab.hw_mode_info.support_dbs;
7400 }
7401 
7402 static bool ath12k_mac_2_freq_same_mac_in_dbs(struct ath12k_base *ab,
7403 					      u32 freq_link1, u32 freq_link2)
7404 {
7405 	struct ath12k_hw_mode_freq_range_arg *freq_range;
7406 
7407 	if (!ath12k_mac_is_hw_dbs_capable(ab))
7408 		return true;
7409 
7410 	freq_range = ab->wmi_ab.hw_mode_info.freq_range_caps[ATH12K_HW_MODE_DBS];
7411 	return ath12k_mac_2_freq_same_mac_in_freq_range(ab, freq_range,
7412 							freq_link1, freq_link2);
7413 }
7414 
7415 static bool ath12k_mac_is_hw_sbs_capable(struct ath12k_base *ab)
7416 {
7417 	return test_bit(WMI_TLV_SERVICE_DUAL_BAND_SIMULTANEOUS_SUPPORT,
7418 			ab->wmi_ab.svc_map) &&
7419 	       ab->wmi_ab.hw_mode_info.support_sbs;
7420 }
7421 
7422 static bool ath12k_mac_2_freq_same_mac_in_sbs(struct ath12k_base *ab,
7423 					      u32 freq_link1, u32 freq_link2)
7424 {
7425 	struct ath12k_hw_mode_info *info = &ab->wmi_ab.hw_mode_info;
7426 	struct ath12k_hw_mode_freq_range_arg *sbs_uppr_share;
7427 	struct ath12k_hw_mode_freq_range_arg *sbs_low_share;
7428 	struct ath12k_hw_mode_freq_range_arg *sbs_range;
7429 
7430 	if (!ath12k_mac_is_hw_sbs_capable(ab))
7431 		return true;
7432 
7433 	if (ab->wmi_ab.sbs_lower_band_end_freq) {
7434 		sbs_uppr_share = info->freq_range_caps[ATH12K_HW_MODE_SBS_UPPER_SHARE];
7435 		sbs_low_share = info->freq_range_caps[ATH12K_HW_MODE_SBS_LOWER_SHARE];
7436 
7437 		return ath12k_mac_2_freq_same_mac_in_freq_range(ab, sbs_low_share,
7438 								freq_link1, freq_link2) ||
7439 		       ath12k_mac_2_freq_same_mac_in_freq_range(ab, sbs_uppr_share,
7440 								freq_link1, freq_link2);
7441 	}
7442 
7443 	sbs_range = info->freq_range_caps[ATH12K_HW_MODE_SBS];
7444 	return ath12k_mac_2_freq_same_mac_in_freq_range(ab, sbs_range,
7445 							freq_link1, freq_link2);
7446 }
7447 
7448 static bool ath12k_mac_freqs_on_same_mac(struct ath12k_base *ab,
7449 					 u32 freq_link1, u32 freq_link2)
7450 {
7451 	return ath12k_mac_2_freq_same_mac_in_dbs(ab, freq_link1, freq_link2) &&
7452 	       ath12k_mac_2_freq_same_mac_in_sbs(ab, freq_link1, freq_link2);
7453 }
7454 
7455 static int ath12k_mac_mlo_sta_set_link_active(struct ath12k_base *ab,
7456 					      enum wmi_mlo_link_force_reason reason,
7457 					      enum wmi_mlo_link_force_mode mode,
7458 					      u8 *mlo_vdev_id_lst,
7459 					      u8 num_mlo_vdev,
7460 					      u8 *mlo_inactive_vdev_lst,
7461 					      u8 num_mlo_inactive_vdev)
7462 {
7463 	struct wmi_mlo_link_set_active_arg param = {};
7464 	u32 entry_idx, entry_offset, vdev_idx;
7465 	u8 vdev_id;
7466 
7467 	param.reason = reason;
7468 	param.force_mode = mode;
7469 
7470 	for (vdev_idx = 0; vdev_idx < num_mlo_vdev; vdev_idx++) {
7471 		vdev_id = mlo_vdev_id_lst[vdev_idx];
7472 		entry_idx = vdev_id / 32;
7473 		entry_offset = vdev_id % 32;
7474 		if (entry_idx >= WMI_MLO_LINK_NUM_SZ) {
7475 			ath12k_warn(ab, "Invalid entry_idx %d num_mlo_vdev %d vdev %d",
7476 				    entry_idx, num_mlo_vdev, vdev_id);
7477 			return -EINVAL;
7478 		}
7479 		param.vdev_bitmap[entry_idx] |= 1 << entry_offset;
7480 		/* update entry number if entry index changed */
7481 		if (param.num_vdev_bitmap < entry_idx + 1)
7482 			param.num_vdev_bitmap = entry_idx + 1;
7483 	}
7484 
7485 	ath12k_dbg(ab, ATH12K_DBG_MAC,
7486 		   "num_vdev_bitmap %d vdev_bitmap[0] = 0x%x, vdev_bitmap[1] = 0x%x",
7487 		   param.num_vdev_bitmap, param.vdev_bitmap[0], param.vdev_bitmap[1]);
7488 
7489 	if (mode == WMI_MLO_LINK_FORCE_MODE_ACTIVE_INACTIVE) {
7490 		for (vdev_idx = 0; vdev_idx < num_mlo_inactive_vdev; vdev_idx++) {
7491 			vdev_id = mlo_inactive_vdev_lst[vdev_idx];
7492 			entry_idx = vdev_id / 32;
7493 			entry_offset = vdev_id % 32;
7494 			if (entry_idx >= WMI_MLO_LINK_NUM_SZ) {
7495 				ath12k_warn(ab, "Invalid entry_idx %d num_mlo_vdev %d vdev %d",
7496 					    entry_idx, num_mlo_inactive_vdev, vdev_id);
7497 				return -EINVAL;
7498 			}
7499 			param.inactive_vdev_bitmap[entry_idx] |= 1 << entry_offset;
7500 			/* update entry number if entry index changed */
7501 			if (param.num_inactive_vdev_bitmap < entry_idx + 1)
7502 				param.num_inactive_vdev_bitmap = entry_idx + 1;
7503 		}
7504 
7505 		ath12k_dbg(ab, ATH12K_DBG_MAC,
7506 			   "num_vdev_bitmap %d inactive_vdev_bitmap[0] = 0x%x, inactive_vdev_bitmap[1] = 0x%x",
7507 			   param.num_inactive_vdev_bitmap,
7508 			   param.inactive_vdev_bitmap[0],
7509 			   param.inactive_vdev_bitmap[1]);
7510 	}
7511 
7512 	if (mode == WMI_MLO_LINK_FORCE_MODE_ACTIVE_LINK_NUM ||
7513 	    mode == WMI_MLO_LINK_FORCE_MODE_INACTIVE_LINK_NUM) {
7514 		param.num_link_entry = 1;
7515 		param.link_num[0].num_of_link = num_mlo_vdev - 1;
7516 	}
7517 
7518 	return ath12k_wmi_send_mlo_link_set_active_cmd(ab, &param);
7519 }
7520 
7521 static int ath12k_mac_mlo_sta_update_link_active(struct ath12k_base *ab,
7522 						 struct ieee80211_hw *hw,
7523 						 struct ath12k_vif *ahvif)
7524 {
7525 	u8 mlo_vdev_id_lst[IEEE80211_MLD_MAX_NUM_LINKS] = {};
7526 	u32 mlo_freq_list[IEEE80211_MLD_MAX_NUM_LINKS] = {};
7527 	unsigned long links = ahvif->links_map;
7528 	enum wmi_mlo_link_force_reason reason;
7529 	struct ieee80211_chanctx_conf *conf;
7530 	enum wmi_mlo_link_force_mode mode;
7531 	struct ieee80211_bss_conf *info;
7532 	struct ath12k_link_vif *arvif;
7533 	u8 num_mlo_vdev = 0;
7534 	u8 link_id;
7535 
7536 	for_each_set_bit(link_id, &links, IEEE80211_MLD_MAX_NUM_LINKS) {
7537 		arvif = wiphy_dereference(hw->wiphy, ahvif->link[link_id]);
7538 		/* make sure vdev is created on this device */
7539 		if (!arvif || !arvif->is_created || arvif->ar->ab != ab)
7540 			continue;
7541 
7542 		info = ath12k_mac_get_link_bss_conf(arvif);
7543 		conf = wiphy_dereference(hw->wiphy, info->chanctx_conf);
7544 		mlo_freq_list[num_mlo_vdev] = conf->def.chan->center_freq;
7545 
7546 		mlo_vdev_id_lst[num_mlo_vdev] = arvif->vdev_id;
7547 		num_mlo_vdev++;
7548 	}
7549 
7550 	/* It is not allowed to activate more links than a single device
7551 	 * supported. Something goes wrong if we reach here.
7552 	 */
7553 	if (num_mlo_vdev > ATH12K_NUM_MAX_ACTIVE_LINKS_PER_DEVICE) {
7554 		WARN_ON_ONCE(1);
7555 		return -EINVAL;
7556 	}
7557 
7558 	/* if 2 links are established and both link channels fall on the
7559 	 * same hardware MAC, send command to firmware to deactivate one
7560 	 * of them.
7561 	 */
7562 	if (num_mlo_vdev == 2 &&
7563 	    ath12k_mac_freqs_on_same_mac(ab, mlo_freq_list[0],
7564 					 mlo_freq_list[1])) {
7565 		mode = WMI_MLO_LINK_FORCE_MODE_INACTIVE_LINK_NUM;
7566 		reason = WMI_MLO_LINK_FORCE_REASON_NEW_CONNECT;
7567 		return ath12k_mac_mlo_sta_set_link_active(ab, reason, mode,
7568 							  mlo_vdev_id_lst, num_mlo_vdev,
7569 							  NULL, 0);
7570 	}
7571 
7572 	return 0;
7573 }
7574 
7575 static bool ath12k_mac_are_sbs_chan(struct ath12k_base *ab, u32 freq_1, u32 freq_2)
7576 {
7577 	if (!ath12k_mac_is_hw_sbs_capable(ab))
7578 		return false;
7579 
7580 	if (ath12k_is_2ghz_channel_freq(freq_1) ||
7581 	    ath12k_is_2ghz_channel_freq(freq_2))
7582 		return false;
7583 
7584 	return !ath12k_mac_2_freq_same_mac_in_sbs(ab, freq_1, freq_2);
7585 }
7586 
7587 static bool ath12k_mac_are_dbs_chan(struct ath12k_base *ab, u32 freq_1, u32 freq_2)
7588 {
7589 	if (!ath12k_mac_is_hw_dbs_capable(ab))
7590 		return false;
7591 
7592 	return !ath12k_mac_2_freq_same_mac_in_dbs(ab, freq_1, freq_2);
7593 }
7594 
7595 static int ath12k_mac_select_links(struct ath12k_base *ab,
7596 				   struct ieee80211_vif *vif,
7597 				   struct ieee80211_hw *hw,
7598 				   u16 *selected_links)
7599 {
7600 	unsigned long useful_links = ieee80211_vif_usable_links(vif);
7601 	struct ath12k_vif *ahvif = ath12k_vif_to_ahvif(vif);
7602 	u8 num_useful_links = hweight_long(useful_links);
7603 	struct ieee80211_chanctx_conf *chanctx;
7604 	struct ath12k_link_vif *assoc_arvif;
7605 	u32 assoc_link_freq, partner_freq;
7606 	u16 sbs_links = 0, dbs_links = 0;
7607 	struct ieee80211_bss_conf *info;
7608 	struct ieee80211_channel *chan;
7609 	struct ieee80211_sta *sta;
7610 	struct ath12k_sta *ahsta;
7611 	u8 link_id;
7612 
7613 	/* activate all useful links if less than max supported */
7614 	if (num_useful_links <= ATH12K_NUM_MAX_ACTIVE_LINKS_PER_DEVICE) {
7615 		*selected_links = useful_links;
7616 		return 0;
7617 	}
7618 
7619 	/* only in station mode we can get here, so it's safe
7620 	 * to use ap_addr
7621 	 */
7622 	rcu_read_lock();
7623 	sta = ieee80211_find_sta(vif, vif->cfg.ap_addr);
7624 	if (!sta) {
7625 		rcu_read_unlock();
7626 		ath12k_warn(ab, "failed to find sta with addr %pM\n", vif->cfg.ap_addr);
7627 		return -EINVAL;
7628 	}
7629 
7630 	ahsta = ath12k_sta_to_ahsta(sta);
7631 	assoc_arvif = wiphy_dereference(hw->wiphy, ahvif->link[ahsta->assoc_link_id]);
7632 	info = ath12k_mac_get_link_bss_conf(assoc_arvif);
7633 	chanctx = rcu_dereference(info->chanctx_conf);
7634 	assoc_link_freq = chanctx->def.chan->center_freq;
7635 	rcu_read_unlock();
7636 	ath12k_dbg(ab, ATH12K_DBG_MAC, "assoc link %u freq %u\n",
7637 		   assoc_arvif->link_id, assoc_link_freq);
7638 
7639 	/* assoc link is already activated and has to be kept active,
7640 	 * only need to select a partner link from others.
7641 	 */
7642 	useful_links &= ~BIT(assoc_arvif->link_id);
7643 	for_each_set_bit(link_id, &useful_links, IEEE80211_MLD_MAX_NUM_LINKS) {
7644 		info = wiphy_dereference(hw->wiphy, vif->link_conf[link_id]);
7645 		if (!info) {
7646 			ath12k_warn(ab, "failed to get link info for link: %u\n",
7647 				    link_id);
7648 			return -ENOLINK;
7649 		}
7650 
7651 		chan = info->chanreq.oper.chan;
7652 		if (!chan) {
7653 			ath12k_warn(ab, "failed to get chan for link: %u\n", link_id);
7654 			return -EINVAL;
7655 		}
7656 
7657 		partner_freq = chan->center_freq;
7658 		if (ath12k_mac_are_sbs_chan(ab, assoc_link_freq, partner_freq)) {
7659 			sbs_links |= BIT(link_id);
7660 			ath12k_dbg(ab, ATH12K_DBG_MAC, "new SBS link %u freq %u\n",
7661 				   link_id, partner_freq);
7662 			continue;
7663 		}
7664 
7665 		if (ath12k_mac_are_dbs_chan(ab, assoc_link_freq, partner_freq)) {
7666 			dbs_links |= BIT(link_id);
7667 			ath12k_dbg(ab, ATH12K_DBG_MAC, "new DBS link %u freq %u\n",
7668 				   link_id, partner_freq);
7669 			continue;
7670 		}
7671 
7672 		ath12k_dbg(ab, ATH12K_DBG_MAC, "non DBS/SBS link %u freq %u\n",
7673 			   link_id, partner_freq);
7674 	}
7675 
7676 	/* choose the first candidate no matter how many is in the list */
7677 	if (sbs_links)
7678 		link_id = __ffs(sbs_links);
7679 	else if (dbs_links)
7680 		link_id = __ffs(dbs_links);
7681 	else
7682 		link_id = ffs(useful_links) - 1;
7683 
7684 	ath12k_dbg(ab, ATH12K_DBG_MAC, "select partner link %u\n", link_id);
7685 
7686 	*selected_links = BIT(assoc_arvif->link_id) | BIT(link_id);
7687 
7688 	return 0;
7689 }
7690 
7691 int ath12k_mac_op_sta_state(struct ieee80211_hw *hw,
7692 			    struct ieee80211_vif *vif,
7693 			    struct ieee80211_sta *sta,
7694 			    enum ieee80211_sta_state old_state,
7695 			    enum ieee80211_sta_state new_state)
7696 {
7697 	struct ath12k_vif *ahvif = ath12k_vif_to_ahvif(vif);
7698 	struct ath12k_sta *ahsta = ath12k_sta_to_ahsta(sta);
7699 	struct ath12k_hw *ah = ath12k_hw_to_ah(hw);
7700 	struct ath12k_base *prev_ab = NULL, *ab;
7701 	struct ath12k_link_vif *arvif;
7702 	struct ath12k_link_sta *arsta;
7703 	unsigned long valid_links;
7704 	u16 selected_links = 0;
7705 	u8 link_id = 0, i;
7706 	struct ath12k *ar;
7707 	int ret = -EINVAL;
7708 	struct ath12k_dp_peer_create_params dp_params = {};
7709 
7710 	lockdep_assert_wiphy(hw->wiphy);
7711 
7712 	if (ieee80211_vif_is_mld(vif) && sta->valid_links) {
7713 		WARN_ON(!sta->mlo && hweight16(sta->valid_links) != 1);
7714 		link_id = ffs(sta->valid_links) - 1;
7715 	}
7716 
7717 	/* IEEE80211_STA_NOTEXIST -> IEEE80211_STA_NONE:
7718 	 * New station add received. If this is a ML station then
7719 	 * ahsta->links_map will be zero and sta->valid_links will be 1.
7720 	 * Assign default link to the first link sta.
7721 	 */
7722 	if (old_state == IEEE80211_STA_NOTEXIST &&
7723 	    new_state == IEEE80211_STA_NONE) {
7724 		memset(ahsta, 0, sizeof(*ahsta));
7725 		ahsta->free_logical_link_idx_map = U16_MAX;
7726 
7727 		arsta = &ahsta->deflink;
7728 
7729 		/* ML sta */
7730 		if (sta->mlo && !ahsta->links_map &&
7731 		    (hweight16(sta->valid_links) == 1)) {
7732 			ahsta->ml_peer_id = ath12k_peer_ml_alloc(ah);
7733 			if (ahsta->ml_peer_id == ATH12K_MLO_PEER_ID_INVALID) {
7734 				ath12k_hw_warn(ah, "unable to allocate ML peer id for sta %pM",
7735 					       sta->addr);
7736 				goto exit;
7737 			}
7738 
7739 			dp_params.is_mlo = true;
7740 			dp_params.peer_id = ahsta->ml_peer_id | ATH12K_PEER_ML_ID_VALID;
7741 		}
7742 
7743 		dp_params.sta = sta;
7744 
7745 		if (vif->type == NL80211_IFTYPE_AP)
7746 			dp_params.ucast_ra_only = true;
7747 
7748 		ret = ath12k_dp_peer_create(&ah->dp_hw, sta->addr, &dp_params);
7749 		if (ret) {
7750 			ath12k_hw_warn(ah, "unable to create ath12k_dp_peer for sta %pM, ret: %d",
7751 				       sta->addr, ret);
7752 
7753 			goto ml_peer_id_clear;
7754 		}
7755 
7756 		ret = ath12k_mac_assign_link_sta(ah, ahsta, arsta, ahvif,
7757 						 link_id);
7758 		if (ret) {
7759 			ath12k_hw_warn(ah, "unable assign link %d for sta %pM",
7760 				       link_id, sta->addr);
7761 			goto peer_delete;
7762 		}
7763 
7764 		/* above arsta will get memset, hence do this after assign
7765 		 * link sta
7766 		 */
7767 		if (sta->mlo) {
7768 			/* For station mode, arvif->is_sta_assoc_link has been set when
7769 			 * vdev starts. Make sure the arvif/arsta pair have same setting
7770 			 */
7771 			if (vif->type == NL80211_IFTYPE_STATION &&
7772 			    !arsta->arvif->is_sta_assoc_link) {
7773 				ath12k_hw_warn(ah, "failed to verify assoc link setting with link id %u\n",
7774 					       link_id);
7775 				ret = -EINVAL;
7776 				goto exit;
7777 			}
7778 
7779 			arsta->is_assoc_link = true;
7780 			ahsta->assoc_link_id = link_id;
7781 		}
7782 	}
7783 
7784 	/* In the ML station scenario, activate all partner links once the
7785 	 * client is transitioning to the associated state.
7786 	 *
7787 	 * FIXME: Ideally, this activation should occur when the client
7788 	 * transitions to the authorized state. However, there are some
7789 	 * issues with handling this in the firmware. Until the firmware
7790 	 * can manage it properly, activate the links when the client is
7791 	 * about to move to the associated state.
7792 	 */
7793 	if (ieee80211_vif_is_mld(vif) && vif->type == NL80211_IFTYPE_STATION &&
7794 	    old_state == IEEE80211_STA_AUTH && new_state == IEEE80211_STA_ASSOC) {
7795 		/* TODO: for now only do link selection for single device
7796 		 * MLO case. Other cases would be handled in the future.
7797 		 */
7798 		ab = ah->radio[0].ab;
7799 		if (ab->ag->num_devices == 1) {
7800 			ret = ath12k_mac_select_links(ab, vif, hw, &selected_links);
7801 			if (ret) {
7802 				ath12k_warn(ab,
7803 					    "failed to get selected links: %d\n", ret);
7804 				goto exit;
7805 			}
7806 		} else {
7807 			selected_links = ieee80211_vif_usable_links(vif);
7808 		}
7809 
7810 		ieee80211_set_active_links(vif, selected_links);
7811 	}
7812 
7813 	/* Handle all the other state transitions in generic way */
7814 	valid_links = ahsta->links_map;
7815 	for_each_set_bit(link_id, &valid_links, IEEE80211_MLD_MAX_NUM_LINKS) {
7816 		arvif = wiphy_dereference(hw->wiphy, ahvif->link[link_id]);
7817 		arsta = wiphy_dereference(hw->wiphy, ahsta->link[link_id]);
7818 		/* some assumptions went wrong! */
7819 		if (WARN_ON(!arvif || !arsta))
7820 			continue;
7821 
7822 		/* vdev might be in deleted */
7823 		if (WARN_ON(!arvif->ar))
7824 			continue;
7825 
7826 		ret = ath12k_mac_handle_link_sta_state(hw, arvif, arsta,
7827 						       old_state, new_state);
7828 		if (ret) {
7829 			ath12k_hw_warn(ah, "unable to move link sta %d of sta %pM from state %d to %d",
7830 				       link_id, arsta->addr, old_state, new_state);
7831 
7832 			if (old_state == IEEE80211_STA_NOTEXIST &&
7833 			    new_state == IEEE80211_STA_NONE)
7834 				goto peer_delete;
7835 			else
7836 				goto exit;
7837 		}
7838 	}
7839 
7840 	if (ieee80211_vif_is_mld(vif) && vif->type == NL80211_IFTYPE_STATION &&
7841 	    old_state == IEEE80211_STA_ASSOC && new_state == IEEE80211_STA_AUTHORIZED) {
7842 		for_each_ar(ah, ar, i) {
7843 			ab = ar->ab;
7844 			if (prev_ab == ab)
7845 				continue;
7846 
7847 			ret = ath12k_mac_mlo_sta_update_link_active(ab, hw, ahvif);
7848 			if (ret) {
7849 				ath12k_warn(ab,
7850 					    "failed to update link active state on connect %d\n",
7851 					    ret);
7852 				goto exit;
7853 			}
7854 
7855 			prev_ab = ab;
7856 		}
7857 	}
7858 	/* IEEE80211_STA_NONE -> IEEE80211_STA_NOTEXIST:
7859 	 * Remove the station from driver (handle ML sta here since that
7860 	 * needs special handling. Normal sta will be handled in generic
7861 	 * handler below
7862 	 */
7863 	if (old_state == IEEE80211_STA_NONE &&
7864 	    new_state == IEEE80211_STA_NOTEXIST) {
7865 		if (sta->mlo)
7866 			ath12k_mac_ml_station_remove(ahvif, ahsta);
7867 
7868 		ath12k_dp_peer_delete(&ah->dp_hw, sta->addr, sta);
7869 	}
7870 
7871 	ret = 0;
7872 	goto exit;
7873 
7874 peer_delete:
7875 	ath12k_dp_peer_delete(&ah->dp_hw, sta->addr, sta);
7876 ml_peer_id_clear:
7877 	if (sta->mlo) {
7878 		clear_bit(ahsta->ml_peer_id, ah->free_ml_peer_id_map);
7879 		ahsta->ml_peer_id = ATH12K_MLO_PEER_ID_INVALID;
7880 	}
7881 exit:
7882 	/* update the state if everything went well */
7883 	if (!ret)
7884 		ahsta->state = new_state;
7885 
7886 	return ret;
7887 }
7888 EXPORT_SYMBOL(ath12k_mac_op_sta_state);
7889 
7890 int ath12k_mac_op_sta_set_txpwr(struct ieee80211_hw *hw,
7891 				struct ieee80211_vif *vif,
7892 				struct ieee80211_sta *sta)
7893 {
7894 	struct ath12k_sta *ahsta = ath12k_sta_to_ahsta(sta);
7895 	struct ath12k *ar;
7896 	struct ath12k_vif *ahvif = ath12k_vif_to_ahvif(vif);
7897 	struct ath12k_link_vif *arvif;
7898 	struct ath12k_link_sta *arsta;
7899 	u8 link_id;
7900 	int ret;
7901 	s16 txpwr;
7902 
7903 	lockdep_assert_wiphy(hw->wiphy);
7904 
7905 	/* TODO: use link id from mac80211 once that's implemented */
7906 	link_id = 0;
7907 
7908 	arvif = wiphy_dereference(hw->wiphy, ahvif->link[link_id]);
7909 	arsta = wiphy_dereference(hw->wiphy, ahsta->link[link_id]);
7910 
7911 	if (sta->deflink.txpwr.type == NL80211_TX_POWER_AUTOMATIC) {
7912 		txpwr = 0;
7913 	} else {
7914 		txpwr = sta->deflink.txpwr.power;
7915 		if (!txpwr) {
7916 			ret = -EINVAL;
7917 			goto out;
7918 		}
7919 	}
7920 
7921 	if (txpwr > ATH12K_TX_POWER_MAX_VAL || txpwr < ATH12K_TX_POWER_MIN_VAL) {
7922 		ret = -EINVAL;
7923 		goto out;
7924 	}
7925 
7926 	ar = arvif->ar;
7927 
7928 	ret = ath12k_wmi_set_peer_param(ar, arsta->addr, arvif->vdev_id,
7929 					WMI_PEER_USE_FIXED_PWR, txpwr);
7930 	if (ret) {
7931 		ath12k_warn(ar->ab, "failed to set tx power for station ret: %d\n",
7932 			    ret);
7933 		goto out;
7934 	}
7935 
7936 out:
7937 	return ret;
7938 }
7939 EXPORT_SYMBOL(ath12k_mac_op_sta_set_txpwr);
7940 
7941 void ath12k_mac_op_sta_set_4addr(struct ieee80211_hw *hw,
7942 				 struct ieee80211_vif *vif,
7943 				 struct ieee80211_sta *sta, bool enabled)
7944 {
7945 	struct ath12k_sta *ahsta = ath12k_sta_to_ahsta(sta);
7946 
7947 	lockdep_assert_wiphy(hw->wiphy);
7948 
7949 	/*
7950 	 * 4-address mode disabled option is available only for station
7951 	 * interface from mac80211, and we have wds_vdev_param for station
7952 	 * interface and target will not allow to disable the wds_vdev_param
7953 	 * during run time. So, add support only for enable case, for
7954 	 * disable case station interface needs to be reconnect.
7955 	 */
7956 	if (enabled && !ahsta->enable_4addr) {
7957 		if (!ath12k_mac_sta_set_4addr(hw->wiphy, ahsta))
7958 			ahsta->enable_4addr = true;
7959 	}
7960 }
7961 EXPORT_SYMBOL(ath12k_mac_op_sta_set_4addr);
7962 
7963 void ath12k_mac_op_link_sta_rc_update(struct ieee80211_hw *hw,
7964 				      struct ieee80211_vif *vif,
7965 				      struct ieee80211_link_sta *link_sta,
7966 				      u32 changed)
7967 {
7968 	struct ieee80211_sta *sta = link_sta->sta;
7969 	struct ath12k *ar;
7970 	struct ath12k_sta *ahsta = ath12k_sta_to_ahsta(sta);
7971 	struct ath12k_vif *ahvif = ath12k_vif_to_ahvif(vif);
7972 	struct ath12k_hw *ah = ath12k_hw_to_ah(hw);
7973 	struct ath12k_link_sta *arsta;
7974 	struct ath12k_link_vif *arvif;
7975 	struct ath12k_dp_link_peer *peer;
7976 	u32 bw, smps;
7977 	struct ath12k_dp *dp;
7978 
7979 	rcu_read_lock();
7980 	arvif = rcu_dereference(ahvif->link[link_sta->link_id]);
7981 	if (!arvif) {
7982 		ath12k_hw_warn(ah, "mac sta rc update failed to fetch link vif on link id %u for peer %pM\n",
7983 			       link_sta->link_id, sta->addr);
7984 		rcu_read_unlock();
7985 		return;
7986 	}
7987 
7988 	ar = arvif->ar;
7989 	dp = ath12k_ab_to_dp(ar->ab);
7990 
7991 	arsta = rcu_dereference(ahsta->link[link_sta->link_id]);
7992 	if (!arsta) {
7993 		rcu_read_unlock();
7994 		ath12k_warn(ar->ab, "mac sta rc update failed to fetch link sta on link id %u for peer %pM\n",
7995 			    link_sta->link_id, sta->addr);
7996 		return;
7997 	}
7998 	spin_lock_bh(&dp->dp_lock);
7999 
8000 	peer = ath12k_dp_link_peer_find_by_vdev_and_addr(dp, arvif->vdev_id,
8001 							 arsta->addr);
8002 	if (!peer) {
8003 		spin_unlock_bh(&dp->dp_lock);
8004 		rcu_read_unlock();
8005 		ath12k_warn(ar->ab, "mac sta rc update failed to find peer %pM on vdev %i\n",
8006 			    arsta->addr, arvif->vdev_id);
8007 		return;
8008 	}
8009 
8010 	spin_unlock_bh(&dp->dp_lock);
8011 
8012 	if (arsta->link_id >= IEEE80211_MLD_MAX_NUM_LINKS) {
8013 		rcu_read_unlock();
8014 		return;
8015 	}
8016 
8017 	link_sta = rcu_dereference(sta->link[arsta->link_id]);
8018 	if (!link_sta) {
8019 		rcu_read_unlock();
8020 		ath12k_warn(ar->ab, "unable to access link sta in rc update for sta %pM link %u\n",
8021 			    sta->addr, arsta->link_id);
8022 		return;
8023 	}
8024 
8025 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
8026 		   "mac sta rc update for %pM changed %08x bw %d nss %d smps %d\n",
8027 		   arsta->addr, changed, link_sta->bandwidth, link_sta->rx_nss,
8028 		   link_sta->smps_mode);
8029 
8030 	spin_lock_bh(&ar->data_lock);
8031 
8032 	if (changed & IEEE80211_RC_BW_CHANGED) {
8033 		bw = ath12k_mac_ieee80211_sta_bw_to_wmi(ar, link_sta);
8034 		arsta->bw_prev = arsta->bw;
8035 		arsta->bw = bw;
8036 	}
8037 
8038 	if (changed & IEEE80211_RC_NSS_CHANGED)
8039 		arsta->nss = link_sta->rx_nss;
8040 
8041 	if (changed & IEEE80211_RC_SMPS_CHANGED) {
8042 		smps = WMI_PEER_SMPS_PS_NONE;
8043 
8044 		switch (link_sta->smps_mode) {
8045 		case IEEE80211_SMPS_AUTOMATIC:
8046 		case IEEE80211_SMPS_OFF:
8047 			smps = WMI_PEER_SMPS_PS_NONE;
8048 			break;
8049 		case IEEE80211_SMPS_STATIC:
8050 			smps = WMI_PEER_SMPS_STATIC;
8051 			break;
8052 		case IEEE80211_SMPS_DYNAMIC:
8053 			smps = WMI_PEER_SMPS_DYNAMIC;
8054 			break;
8055 		default:
8056 			ath12k_warn(ar->ab, "Invalid smps %d in sta rc update for %pM link %u\n",
8057 				    link_sta->smps_mode, arsta->addr, link_sta->link_id);
8058 			smps = WMI_PEER_SMPS_PS_NONE;
8059 			break;
8060 		}
8061 
8062 		arsta->smps = smps;
8063 	}
8064 
8065 	arsta->changed |= changed;
8066 
8067 	spin_unlock_bh(&ar->data_lock);
8068 
8069 	wiphy_work_queue(hw->wiphy, &arsta->update_wk);
8070 
8071 	rcu_read_unlock();
8072 }
8073 EXPORT_SYMBOL(ath12k_mac_op_link_sta_rc_update);
8074 
8075 static struct ath12k_link_sta *ath12k_mac_alloc_assign_link_sta(struct ath12k_hw *ah,
8076 								struct ath12k_sta *ahsta,
8077 								struct ath12k_vif *ahvif,
8078 								u8 link_id)
8079 {
8080 	struct ath12k_link_sta *arsta;
8081 	int ret;
8082 
8083 	lockdep_assert_wiphy(ah->hw->wiphy);
8084 
8085 	if (link_id >= IEEE80211_MLD_MAX_NUM_LINKS)
8086 		return NULL;
8087 
8088 	arsta = wiphy_dereference(ah->hw->wiphy, ahsta->link[link_id]);
8089 	if (arsta)
8090 		return NULL;
8091 
8092 	arsta = kmalloc_obj(*arsta);
8093 	if (!arsta)
8094 		return NULL;
8095 
8096 	ret = ath12k_mac_assign_link_sta(ah, ahsta, arsta, ahvif, link_id);
8097 	if (ret) {
8098 		kfree(arsta);
8099 		return NULL;
8100 	}
8101 
8102 	return arsta;
8103 }
8104 
8105 int ath12k_mac_op_change_sta_links(struct ieee80211_hw *hw,
8106 				   struct ieee80211_vif *vif,
8107 				   struct ieee80211_sta *sta,
8108 				   u16 old_links, u16 new_links)
8109 {
8110 	struct ath12k_vif *ahvif = ath12k_vif_to_ahvif(vif);
8111 	struct ath12k_sta *ahsta = ath12k_sta_to_ahsta(sta);
8112 	struct ath12k_hw *ah = hw->priv;
8113 	struct ath12k_link_vif *arvif;
8114 	struct ath12k_link_sta *arsta;
8115 	unsigned long valid_links;
8116 	struct ath12k *ar;
8117 	u8 link_id;
8118 	int ret;
8119 
8120 	lockdep_assert_wiphy(hw->wiphy);
8121 
8122 	if (!sta->valid_links)
8123 		return -EINVAL;
8124 
8125 	/* Firmware does not support removal of one of link stas. All sta
8126 	 * would be removed during ML STA delete in sta_state(), hence link
8127 	 * sta removal is not handled here.
8128 	 */
8129 	if (new_links < old_links)
8130 		return 0;
8131 
8132 	if (ahsta->ml_peer_id == ATH12K_MLO_PEER_ID_INVALID) {
8133 		ath12k_hw_warn(ah, "unable to add link for ml sta %pM", sta->addr);
8134 		return -EINVAL;
8135 	}
8136 
8137 	/* this op is expected only after initial sta insertion with default link */
8138 	if (WARN_ON(ahsta->links_map == 0))
8139 		return -EINVAL;
8140 
8141 	valid_links = new_links;
8142 	for_each_set_bit(link_id, &valid_links, IEEE80211_MLD_MAX_NUM_LINKS) {
8143 		if (ahsta->links_map & BIT(link_id))
8144 			continue;
8145 
8146 		arvif = wiphy_dereference(hw->wiphy, ahvif->link[link_id]);
8147 		if (!arvif || !arvif->is_created)
8148 			continue;
8149 
8150 		arsta = ath12k_mac_alloc_assign_link_sta(ah, ahsta, ahvif, link_id);
8151 		if (!arsta) {
8152 			ath12k_hw_warn(ah, "Failed to alloc/assign link sta");
8153 			continue;
8154 		}
8155 
8156 		ar = arvif->ar;
8157 
8158 		ret = ath12k_mac_station_add(ar, arvif, arsta);
8159 		if (ret) {
8160 			ath12k_warn(ar->ab, "Failed to add station: %pM for VDEV: %d\n",
8161 				    arsta->addr, arvif->vdev_id);
8162 			ath12k_mac_free_unassign_link_sta(ah, ahsta, link_id);
8163 			return ret;
8164 		}
8165 	}
8166 
8167 	return 0;
8168 }
8169 EXPORT_SYMBOL(ath12k_mac_op_change_sta_links);
8170 
8171 bool ath12k_mac_op_can_activate_links(struct ieee80211_hw *hw,
8172 				      struct ieee80211_vif *vif,
8173 				      u16 active_links)
8174 {
8175 	/* TODO: Handle recovery case */
8176 
8177 	return true;
8178 }
8179 EXPORT_SYMBOL(ath12k_mac_op_can_activate_links);
8180 
8181 static int ath12k_conf_tx_uapsd(struct ath12k_link_vif *arvif,
8182 				u16 ac, bool enable)
8183 {
8184 	struct ath12k *ar = arvif->ar;
8185 	struct ath12k_vif *ahvif = arvif->ahvif;
8186 	u32 value;
8187 	int ret;
8188 
8189 	if (ahvif->vdev_type != WMI_VDEV_TYPE_STA)
8190 		return 0;
8191 
8192 	switch (ac) {
8193 	case IEEE80211_AC_VO:
8194 		value = WMI_STA_PS_UAPSD_AC3_DELIVERY_EN |
8195 			WMI_STA_PS_UAPSD_AC3_TRIGGER_EN;
8196 		break;
8197 	case IEEE80211_AC_VI:
8198 		value = WMI_STA_PS_UAPSD_AC2_DELIVERY_EN |
8199 			WMI_STA_PS_UAPSD_AC2_TRIGGER_EN;
8200 		break;
8201 	case IEEE80211_AC_BE:
8202 		value = WMI_STA_PS_UAPSD_AC1_DELIVERY_EN |
8203 			WMI_STA_PS_UAPSD_AC1_TRIGGER_EN;
8204 		break;
8205 	case IEEE80211_AC_BK:
8206 		value = WMI_STA_PS_UAPSD_AC0_DELIVERY_EN |
8207 			WMI_STA_PS_UAPSD_AC0_TRIGGER_EN;
8208 		break;
8209 	}
8210 
8211 	if (enable)
8212 		ahvif->u.sta.uapsd |= value;
8213 	else
8214 		ahvif->u.sta.uapsd &= ~value;
8215 
8216 	ret = ath12k_wmi_set_sta_ps_param(ar, arvif->vdev_id,
8217 					  WMI_STA_PS_PARAM_UAPSD,
8218 					  ahvif->u.sta.uapsd);
8219 	if (ret) {
8220 		ath12k_warn(ar->ab, "could not set uapsd params %d\n", ret);
8221 		goto exit;
8222 	}
8223 
8224 	if (ahvif->u.sta.uapsd)
8225 		value = WMI_STA_PS_RX_WAKE_POLICY_POLL_UAPSD;
8226 	else
8227 		value = WMI_STA_PS_RX_WAKE_POLICY_WAKE;
8228 
8229 	ret = ath12k_wmi_set_sta_ps_param(ar, arvif->vdev_id,
8230 					  WMI_STA_PS_PARAM_RX_WAKE_POLICY,
8231 					  value);
8232 	if (ret)
8233 		ath12k_warn(ar->ab, "could not set rx wake param %d\n", ret);
8234 
8235 exit:
8236 	return ret;
8237 }
8238 
8239 static int ath12k_mac_conf_tx(struct ath12k_link_vif *arvif, u16 ac,
8240 			      const struct ieee80211_tx_queue_params *params)
8241 {
8242 	struct wmi_wmm_params_arg *p = NULL;
8243 	struct ath12k *ar = arvif->ar;
8244 	struct ath12k_base *ab = ar->ab;
8245 	int ret;
8246 
8247 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
8248 
8249 	switch (ac) {
8250 	case IEEE80211_AC_VO:
8251 		p = &arvif->wmm_params.ac_vo;
8252 		break;
8253 	case IEEE80211_AC_VI:
8254 		p = &arvif->wmm_params.ac_vi;
8255 		break;
8256 	case IEEE80211_AC_BE:
8257 		p = &arvif->wmm_params.ac_be;
8258 		break;
8259 	case IEEE80211_AC_BK:
8260 		p = &arvif->wmm_params.ac_bk;
8261 		break;
8262 	}
8263 
8264 	if (WARN_ON(!p)) {
8265 		ret = -EINVAL;
8266 		goto exit;
8267 	}
8268 
8269 	p->cwmin = params->cw_min;
8270 	p->cwmax = params->cw_max;
8271 	p->aifs = params->aifs;
8272 	p->txop = params->txop;
8273 
8274 	ret = ath12k_wmi_send_wmm_update_cmd(ar, arvif->vdev_id,
8275 					     &arvif->wmm_params);
8276 	if (ret) {
8277 		ath12k_warn(ab, "pdev idx %d failed to set wmm params: %d\n",
8278 			    ar->pdev_idx, ret);
8279 		goto exit;
8280 	}
8281 
8282 	ret = ath12k_conf_tx_uapsd(arvif, ac, params->uapsd);
8283 	if (ret)
8284 		ath12k_warn(ab, "pdev idx %d failed to set sta uapsd: %d\n",
8285 			    ar->pdev_idx, ret);
8286 
8287 exit:
8288 	return ret;
8289 }
8290 
8291 int ath12k_mac_op_conf_tx(struct ieee80211_hw *hw,
8292 			  struct ieee80211_vif *vif,
8293 			  unsigned int link_id, u16 ac,
8294 			  const struct ieee80211_tx_queue_params *params)
8295 {
8296 	struct ath12k_vif *ahvif = ath12k_vif_to_ahvif(vif);
8297 	struct ath12k_link_vif *arvif;
8298 	struct ath12k_vif_cache *cache;
8299 	int ret;
8300 
8301 	lockdep_assert_wiphy(hw->wiphy);
8302 
8303 	if (link_id >= IEEE80211_MLD_MAX_NUM_LINKS)
8304 		return -EINVAL;
8305 
8306 	arvif = wiphy_dereference(hw->wiphy, ahvif->link[link_id]);
8307 	if (!arvif || !arvif->is_created) {
8308 		cache = ath12k_ahvif_get_link_cache(ahvif, link_id);
8309 		if (!cache)
8310 			return -ENOSPC;
8311 
8312 		cache->tx_conf.changed = true;
8313 		cache->tx_conf.ac = ac;
8314 		cache->tx_conf.tx_queue_params = *params;
8315 
8316 		return 0;
8317 	}
8318 
8319 	ret = ath12k_mac_conf_tx(arvif, ac, params);
8320 
8321 	return ret;
8322 }
8323 EXPORT_SYMBOL(ath12k_mac_op_conf_tx);
8324 
8325 static struct ieee80211_sta_ht_cap
8326 ath12k_create_ht_cap(struct ath12k *ar, u32 ar_ht_cap, u32 rate_cap_rx_chainmask)
8327 {
8328 	int i;
8329 	struct ieee80211_sta_ht_cap ht_cap = {};
8330 	u32 ar_vht_cap = ar->pdev->cap.vht_cap;
8331 
8332 	if (!(ar_ht_cap & WMI_HT_CAP_ENABLED))
8333 		return ht_cap;
8334 
8335 	ht_cap.ht_supported = 1;
8336 	ht_cap.ampdu_factor = IEEE80211_HT_MAX_AMPDU_64K;
8337 	ht_cap.ampdu_density = IEEE80211_HT_MPDU_DENSITY_NONE;
8338 	ht_cap.cap |= IEEE80211_HT_CAP_SUP_WIDTH_20_40;
8339 	ht_cap.cap |= IEEE80211_HT_CAP_DSSSCCK40;
8340 	ht_cap.cap |= WLAN_HT_CAP_SM_PS_STATIC << IEEE80211_HT_CAP_SM_PS_SHIFT;
8341 
8342 	if (ar_ht_cap & WMI_HT_CAP_HT20_SGI)
8343 		ht_cap.cap |= IEEE80211_HT_CAP_SGI_20;
8344 
8345 	if (ar_ht_cap & WMI_HT_CAP_HT40_SGI)
8346 		ht_cap.cap |= IEEE80211_HT_CAP_SGI_40;
8347 
8348 	if (ar_ht_cap & WMI_HT_CAP_DYNAMIC_SMPS) {
8349 		u32 smps;
8350 
8351 		smps   = WLAN_HT_CAP_SM_PS_DYNAMIC;
8352 		smps <<= IEEE80211_HT_CAP_SM_PS_SHIFT;
8353 
8354 		ht_cap.cap |= smps;
8355 	}
8356 
8357 	if (ar_ht_cap & WMI_HT_CAP_TX_STBC)
8358 		ht_cap.cap |= IEEE80211_HT_CAP_TX_STBC;
8359 
8360 	if (ar_ht_cap & WMI_HT_CAP_RX_STBC) {
8361 		u32 stbc;
8362 
8363 		stbc   = ar_ht_cap;
8364 		stbc  &= WMI_HT_CAP_RX_STBC;
8365 		stbc >>= WMI_HT_CAP_RX_STBC_MASK_SHIFT;
8366 		stbc <<= IEEE80211_HT_CAP_RX_STBC_SHIFT;
8367 		stbc  &= IEEE80211_HT_CAP_RX_STBC;
8368 
8369 		ht_cap.cap |= stbc;
8370 	}
8371 
8372 	if (ar_ht_cap & WMI_HT_CAP_RX_LDPC)
8373 		ht_cap.cap |= IEEE80211_HT_CAP_LDPC_CODING;
8374 
8375 	if (ar_ht_cap & WMI_HT_CAP_L_SIG_TXOP_PROT)
8376 		ht_cap.cap |= IEEE80211_HT_CAP_LSIG_TXOP_PROT;
8377 
8378 	if (ar_vht_cap & WMI_VHT_CAP_MAX_MPDU_LEN_MASK)
8379 		ht_cap.cap |= IEEE80211_HT_CAP_MAX_AMSDU;
8380 
8381 	for (i = 0; i < ar->num_rx_chains; i++) {
8382 		if (rate_cap_rx_chainmask & BIT(i))
8383 			ht_cap.mcs.rx_mask[i] = 0xFF;
8384 	}
8385 
8386 	ht_cap.mcs.tx_params |= IEEE80211_HT_MCS_TX_DEFINED;
8387 
8388 	return ht_cap;
8389 }
8390 
8391 static int ath12k_mac_set_txbf_conf(struct ath12k_link_vif *arvif)
8392 {
8393 	u32 value = 0;
8394 	struct ath12k *ar = arvif->ar;
8395 	struct ath12k_vif *ahvif = arvif->ahvif;
8396 	int nsts;
8397 	int sound_dim;
8398 	u32 vht_cap = ar->pdev->cap.vht_cap;
8399 	u32 vdev_param = WMI_VDEV_PARAM_TXBF;
8400 
8401 	if (vht_cap & (IEEE80211_VHT_CAP_SU_BEAMFORMEE_CAPABLE)) {
8402 		nsts = vht_cap & IEEE80211_VHT_CAP_BEAMFORMEE_STS_MASK;
8403 		nsts >>= IEEE80211_VHT_CAP_BEAMFORMEE_STS_SHIFT;
8404 		value |= SM(nsts, WMI_TXBF_STS_CAP_OFFSET);
8405 	}
8406 
8407 	if (vht_cap & (IEEE80211_VHT_CAP_SU_BEAMFORMER_CAPABLE)) {
8408 		sound_dim = vht_cap &
8409 			    IEEE80211_VHT_CAP_SOUNDING_DIMENSIONS_MASK;
8410 		sound_dim >>= IEEE80211_VHT_CAP_SOUNDING_DIMENSIONS_SHIFT;
8411 		if (sound_dim > (ar->num_tx_chains - 1))
8412 			sound_dim = ar->num_tx_chains - 1;
8413 		value |= SM(sound_dim, WMI_BF_SOUND_DIM_OFFSET);
8414 	}
8415 
8416 	if (!value)
8417 		return 0;
8418 
8419 	if (vht_cap & IEEE80211_VHT_CAP_SU_BEAMFORMER_CAPABLE) {
8420 		value |= WMI_VDEV_PARAM_TXBF_SU_TX_BFER;
8421 
8422 		if ((vht_cap & IEEE80211_VHT_CAP_MU_BEAMFORMER_CAPABLE) &&
8423 		    ahvif->vdev_type == WMI_VDEV_TYPE_AP)
8424 			value |= WMI_VDEV_PARAM_TXBF_MU_TX_BFER;
8425 	}
8426 
8427 	if (vht_cap & IEEE80211_VHT_CAP_SU_BEAMFORMEE_CAPABLE) {
8428 		value |= WMI_VDEV_PARAM_TXBF_SU_TX_BFEE;
8429 
8430 		if ((vht_cap & IEEE80211_VHT_CAP_MU_BEAMFORMEE_CAPABLE) &&
8431 		    ahvif->vdev_type == WMI_VDEV_TYPE_STA)
8432 			value |= WMI_VDEV_PARAM_TXBF_MU_TX_BFEE;
8433 	}
8434 
8435 	return ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
8436 					     vdev_param, value);
8437 }
8438 
8439 static void ath12k_set_vht_txbf_cap(struct ath12k *ar, u32 *vht_cap)
8440 {
8441 	bool subfer, subfee;
8442 	int sound_dim = 0;
8443 
8444 	subfer = !!(*vht_cap & (IEEE80211_VHT_CAP_SU_BEAMFORMER_CAPABLE));
8445 	subfee = !!(*vht_cap & (IEEE80211_VHT_CAP_SU_BEAMFORMEE_CAPABLE));
8446 
8447 	if (ar->num_tx_chains < 2) {
8448 		*vht_cap &= ~(IEEE80211_VHT_CAP_SU_BEAMFORMER_CAPABLE);
8449 		subfer = false;
8450 	}
8451 
8452 	/* If SU Beaformer is not set, then disable MU Beamformer Capability */
8453 	if (!subfer)
8454 		*vht_cap &= ~(IEEE80211_VHT_CAP_MU_BEAMFORMER_CAPABLE);
8455 
8456 	/* If SU Beaformee is not set, then disable MU Beamformee Capability */
8457 	if (!subfee)
8458 		*vht_cap &= ~(IEEE80211_VHT_CAP_MU_BEAMFORMEE_CAPABLE);
8459 
8460 	sound_dim = u32_get_bits(*vht_cap,
8461 				 IEEE80211_VHT_CAP_SOUNDING_DIMENSIONS_MASK);
8462 	*vht_cap = u32_replace_bits(*vht_cap, 0,
8463 				    IEEE80211_VHT_CAP_SOUNDING_DIMENSIONS_MASK);
8464 
8465 	/* TODO: Need to check invalid STS and Sound_dim values set by FW? */
8466 
8467 	/* Enable Sounding Dimension Field only if SU BF is enabled */
8468 	if (subfer) {
8469 		if (sound_dim > (ar->num_tx_chains - 1))
8470 			sound_dim = ar->num_tx_chains - 1;
8471 
8472 		*vht_cap = u32_replace_bits(*vht_cap, sound_dim,
8473 					    IEEE80211_VHT_CAP_SOUNDING_DIMENSIONS_MASK);
8474 	}
8475 
8476 	/* Use the STS advertised by FW unless SU Beamformee is not supported*/
8477 	if (!subfee)
8478 		*vht_cap &= ~(IEEE80211_VHT_CAP_BEAMFORMEE_STS_MASK);
8479 }
8480 
8481 static struct ieee80211_sta_vht_cap
8482 ath12k_create_vht_cap(struct ath12k *ar, u32 rate_cap_tx_chainmask,
8483 		      u32 rate_cap_rx_chainmask)
8484 {
8485 	struct ieee80211_sta_vht_cap vht_cap = {};
8486 	u16 txmcs_map, rxmcs_map;
8487 	int i;
8488 
8489 	vht_cap.vht_supported = 1;
8490 	vht_cap.cap = ar->pdev->cap.vht_cap;
8491 
8492 	if (ar->pdev->cap.nss_ratio_enabled)
8493 		vht_cap.vht_mcs.tx_highest |=
8494 			cpu_to_le16(IEEE80211_VHT_EXT_NSS_BW_CAPABLE);
8495 
8496 	ath12k_set_vht_txbf_cap(ar, &vht_cap.cap);
8497 
8498 	/* 80P80 is not supported */
8499 	vht_cap.cap &= ~IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_160_80PLUS80MHZ;
8500 
8501 	rxmcs_map = 0;
8502 	txmcs_map = 0;
8503 	for (i = 0; i < 8; i++) {
8504 		if (i < ar->num_tx_chains && rate_cap_tx_chainmask & BIT(i))
8505 			txmcs_map |= IEEE80211_VHT_MCS_SUPPORT_0_9 << (i * 2);
8506 		else
8507 			txmcs_map |= IEEE80211_VHT_MCS_NOT_SUPPORTED << (i * 2);
8508 
8509 		if (i < ar->num_rx_chains && rate_cap_rx_chainmask & BIT(i))
8510 			rxmcs_map |= IEEE80211_VHT_MCS_SUPPORT_0_9 << (i * 2);
8511 		else
8512 			rxmcs_map |= IEEE80211_VHT_MCS_NOT_SUPPORTED << (i * 2);
8513 	}
8514 
8515 	if (rate_cap_tx_chainmask <= 1)
8516 		vht_cap.cap &= ~IEEE80211_VHT_CAP_TXSTBC;
8517 
8518 	vht_cap.vht_mcs.rx_mcs_map = cpu_to_le16(rxmcs_map);
8519 	vht_cap.vht_mcs.tx_mcs_map = cpu_to_le16(txmcs_map);
8520 
8521 	/* Check if the HW supports 1:1 NSS ratio and reset
8522 	 * EXT NSS BW Support field to 0 to indicate 1:1 ratio
8523 	 */
8524 	if (ar->pdev->cap.nss_ratio_info == WMI_NSS_RATIO_1_NSS)
8525 		vht_cap.cap &= ~IEEE80211_VHT_CAP_EXT_NSS_BW_MASK;
8526 
8527 	return vht_cap;
8528 }
8529 
8530 static void ath12k_mac_setup_ht_vht_cap(struct ath12k *ar,
8531 					struct ath12k_pdev_cap *cap,
8532 					u32 *ht_cap_info)
8533 {
8534 	struct ieee80211_supported_band *band;
8535 	u32 rate_cap_tx_chainmask;
8536 	u32 rate_cap_rx_chainmask;
8537 	u32 ht_cap;
8538 
8539 	rate_cap_tx_chainmask = ar->cfg_tx_chainmask >> cap->tx_chain_mask_shift;
8540 	rate_cap_rx_chainmask = ar->cfg_rx_chainmask >> cap->rx_chain_mask_shift;
8541 
8542 	if (cap->supported_bands & WMI_HOST_WLAN_2GHZ_CAP) {
8543 		band = &ar->mac.sbands[NL80211_BAND_2GHZ];
8544 		ht_cap = cap->band[NL80211_BAND_2GHZ].ht_cap_info;
8545 		if (ht_cap_info)
8546 			*ht_cap_info = ht_cap;
8547 		band->ht_cap = ath12k_create_ht_cap(ar, ht_cap,
8548 						    rate_cap_rx_chainmask);
8549 	}
8550 
8551 	if (cap->supported_bands & WMI_HOST_WLAN_5GHZ_CAP &&
8552 	    (ar->ab->hw_params->single_pdev_only ||
8553 	     !ar->supports_6ghz)) {
8554 		band = &ar->mac.sbands[NL80211_BAND_5GHZ];
8555 		ht_cap = cap->band[NL80211_BAND_5GHZ].ht_cap_info;
8556 		if (ht_cap_info)
8557 			*ht_cap_info = ht_cap;
8558 		band->ht_cap = ath12k_create_ht_cap(ar, ht_cap,
8559 						    rate_cap_rx_chainmask);
8560 		band->vht_cap = ath12k_create_vht_cap(ar, rate_cap_tx_chainmask,
8561 						      rate_cap_rx_chainmask);
8562 	}
8563 }
8564 
8565 static int ath12k_check_chain_mask(struct ath12k *ar, u32 ant, bool is_tx_ant)
8566 {
8567 	/* TODO: Check the request chainmask against the supported
8568 	 * chainmask table which is advertised in extented_service_ready event
8569 	 */
8570 
8571 	return 0;
8572 }
8573 
8574 static void ath12k_gen_ppe_thresh(struct ath12k_wmi_ppe_threshold_arg *fw_ppet,
8575 				  u8 *he_ppet)
8576 {
8577 	int nss, ru;
8578 	u8 bit = 7;
8579 
8580 	he_ppet[0] = fw_ppet->numss_m1 & IEEE80211_PPE_THRES_NSS_MASK;
8581 	he_ppet[0] |= (fw_ppet->ru_bit_mask <<
8582 		       IEEE80211_PPE_THRES_RU_INDEX_BITMASK_POS) &
8583 		      IEEE80211_PPE_THRES_RU_INDEX_BITMASK_MASK;
8584 	for (nss = 0; nss <= fw_ppet->numss_m1; nss++) {
8585 		for (ru = 0; ru < 4; ru++) {
8586 			u8 val;
8587 			int i;
8588 
8589 			if ((fw_ppet->ru_bit_mask & BIT(ru)) == 0)
8590 				continue;
8591 			val = (fw_ppet->ppet16_ppet8_ru3_ru0[nss] >> (ru * 6)) &
8592 			       0x3f;
8593 			val = ((val >> 3) & 0x7) | ((val & 0x7) << 3);
8594 			for (i = 5; i >= 0; i--) {
8595 				he_ppet[bit / 8] |=
8596 					((val >> i) & 0x1) << ((bit % 8));
8597 				bit++;
8598 			}
8599 		}
8600 	}
8601 }
8602 
8603 static void
8604 ath12k_mac_filter_he_cap_mesh(struct ieee80211_he_cap_elem *he_cap_elem)
8605 {
8606 	u8 m;
8607 
8608 	m = IEEE80211_HE_MAC_CAP0_TWT_RES |
8609 	    IEEE80211_HE_MAC_CAP0_TWT_REQ;
8610 	he_cap_elem->mac_cap_info[0] &= ~m;
8611 
8612 	m = IEEE80211_HE_MAC_CAP2_TRS |
8613 	    IEEE80211_HE_MAC_CAP2_BCAST_TWT |
8614 	    IEEE80211_HE_MAC_CAP2_MU_CASCADING;
8615 	he_cap_elem->mac_cap_info[2] &= ~m;
8616 
8617 	m = IEEE80211_HE_MAC_CAP3_FLEX_TWT_SCHED |
8618 	    IEEE80211_HE_MAC_CAP2_BCAST_TWT |
8619 	    IEEE80211_HE_MAC_CAP2_MU_CASCADING;
8620 	he_cap_elem->mac_cap_info[3] &= ~m;
8621 
8622 	m = IEEE80211_HE_MAC_CAP4_BSRP_BQRP_A_MPDU_AGG |
8623 	    IEEE80211_HE_MAC_CAP4_BQR;
8624 	he_cap_elem->mac_cap_info[4] &= ~m;
8625 
8626 	m = IEEE80211_HE_MAC_CAP5_SUBCHAN_SELECTIVE_TRANSMISSION |
8627 	    IEEE80211_HE_MAC_CAP5_UL_2x996_TONE_RU |
8628 	    IEEE80211_HE_MAC_CAP5_PUNCTURED_SOUNDING |
8629 	    IEEE80211_HE_MAC_CAP5_HT_VHT_TRIG_FRAME_RX;
8630 	he_cap_elem->mac_cap_info[5] &= ~m;
8631 
8632 	m = IEEE80211_HE_PHY_CAP2_UL_MU_FULL_MU_MIMO |
8633 	    IEEE80211_HE_PHY_CAP2_UL_MU_PARTIAL_MU_MIMO;
8634 	he_cap_elem->phy_cap_info[2] &= ~m;
8635 
8636 	m = IEEE80211_HE_PHY_CAP3_RX_PARTIAL_BW_SU_IN_20MHZ_MU |
8637 	    IEEE80211_HE_PHY_CAP3_DCM_MAX_CONST_TX_MASK |
8638 	    IEEE80211_HE_PHY_CAP3_DCM_MAX_CONST_RX_MASK;
8639 	he_cap_elem->phy_cap_info[3] &= ~m;
8640 
8641 	m = IEEE80211_HE_PHY_CAP4_MU_BEAMFORMER;
8642 	he_cap_elem->phy_cap_info[4] &= ~m;
8643 
8644 	m = IEEE80211_HE_PHY_CAP5_NG16_MU_FEEDBACK;
8645 	he_cap_elem->phy_cap_info[5] &= ~m;
8646 
8647 	m = IEEE80211_HE_PHY_CAP6_CODEBOOK_SIZE_75_MU |
8648 	    IEEE80211_HE_PHY_CAP6_TRIG_MU_BEAMFORMING_PARTIAL_BW_FB |
8649 	    IEEE80211_HE_PHY_CAP6_TRIG_CQI_FB |
8650 	    IEEE80211_HE_PHY_CAP6_PARTIAL_BANDWIDTH_DL_MUMIMO;
8651 	he_cap_elem->phy_cap_info[6] &= ~m;
8652 
8653 	m = IEEE80211_HE_PHY_CAP7_PSR_BASED_SR |
8654 	    IEEE80211_HE_PHY_CAP7_POWER_BOOST_FACTOR_SUPP |
8655 	    IEEE80211_HE_PHY_CAP7_STBC_TX_ABOVE_80MHZ |
8656 	    IEEE80211_HE_PHY_CAP7_STBC_RX_ABOVE_80MHZ;
8657 	he_cap_elem->phy_cap_info[7] &= ~m;
8658 
8659 	m = IEEE80211_HE_PHY_CAP8_HE_ER_SU_PPDU_4XLTF_AND_08_US_GI |
8660 	    IEEE80211_HE_PHY_CAP8_20MHZ_IN_40MHZ_HE_PPDU_IN_2G |
8661 	    IEEE80211_HE_PHY_CAP8_20MHZ_IN_160MHZ_HE_PPDU |
8662 	    IEEE80211_HE_PHY_CAP8_80MHZ_IN_160MHZ_HE_PPDU;
8663 	he_cap_elem->phy_cap_info[8] &= ~m;
8664 
8665 	m = IEEE80211_HE_PHY_CAP9_LONGER_THAN_16_SIGB_OFDM_SYM |
8666 	    IEEE80211_HE_PHY_CAP9_NON_TRIGGERED_CQI_FEEDBACK |
8667 	    IEEE80211_HE_PHY_CAP9_RX_1024_QAM_LESS_THAN_242_TONE_RU |
8668 	    IEEE80211_HE_PHY_CAP9_TX_1024_QAM_LESS_THAN_242_TONE_RU |
8669 	    IEEE80211_HE_PHY_CAP9_RX_FULL_BW_SU_USING_MU_WITH_COMP_SIGB |
8670 	    IEEE80211_HE_PHY_CAP9_RX_FULL_BW_SU_USING_MU_WITH_NON_COMP_SIGB;
8671 	he_cap_elem->phy_cap_info[9] &= ~m;
8672 }
8673 
8674 static __le16 ath12k_mac_setup_he_6ghz_cap(struct ath12k_pdev_cap *pcap,
8675 					   struct ath12k_band_cap *bcap)
8676 {
8677 	u8 val;
8678 
8679 	bcap->he_6ghz_capa = IEEE80211_HT_MPDU_DENSITY_NONE;
8680 	if (bcap->ht_cap_info & WMI_HT_CAP_DYNAMIC_SMPS)
8681 		bcap->he_6ghz_capa |=
8682 			u32_encode_bits(WLAN_HT_CAP_SM_PS_DYNAMIC,
8683 					IEEE80211_HE_6GHZ_CAP_SM_PS);
8684 	else
8685 		bcap->he_6ghz_capa |=
8686 			u32_encode_bits(WLAN_HT_CAP_SM_PS_DISABLED,
8687 					IEEE80211_HE_6GHZ_CAP_SM_PS);
8688 	val = u32_get_bits(pcap->vht_cap,
8689 			   IEEE80211_VHT_CAP_MAX_A_MPDU_LENGTH_EXPONENT_MASK);
8690 	bcap->he_6ghz_capa |=
8691 		u32_encode_bits(val, IEEE80211_HE_6GHZ_CAP_MAX_AMPDU_LEN_EXP);
8692 	val = u32_get_bits(pcap->vht_cap,
8693 			   IEEE80211_VHT_CAP_MAX_MPDU_MASK);
8694 	bcap->he_6ghz_capa |=
8695 		u32_encode_bits(val, IEEE80211_HE_6GHZ_CAP_MAX_MPDU_LEN);
8696 	if (pcap->vht_cap & IEEE80211_VHT_CAP_RX_ANTENNA_PATTERN)
8697 		bcap->he_6ghz_capa |= IEEE80211_HE_6GHZ_CAP_RX_ANTPAT_CONS;
8698 	if (pcap->vht_cap & IEEE80211_VHT_CAP_TX_ANTENNA_PATTERN)
8699 		bcap->he_6ghz_capa |= IEEE80211_HE_6GHZ_CAP_TX_ANTPAT_CONS;
8700 
8701 	return cpu_to_le16(bcap->he_6ghz_capa);
8702 }
8703 
8704 static void ath12k_mac_set_hemcsmap(struct ath12k *ar,
8705 				    struct ath12k_pdev_cap *cap,
8706 				    struct ieee80211_sta_he_cap *he_cap)
8707 {
8708 	struct ieee80211_he_mcs_nss_supp *mcs_nss = &he_cap->he_mcs_nss_supp;
8709 	u8 maxtxnss_160 = ath12k_get_nss_160mhz(ar, ar->num_tx_chains);
8710 	u8 maxrxnss_160 = ath12k_get_nss_160mhz(ar, ar->num_rx_chains);
8711 	u16 txmcs_map_160 = 0, rxmcs_map_160 = 0;
8712 	u16 txmcs_map = 0, rxmcs_map = 0;
8713 	u32 i;
8714 
8715 	for (i = 0; i < 8; i++) {
8716 		if (i < ar->num_tx_chains &&
8717 		    (ar->cfg_tx_chainmask >> cap->tx_chain_mask_shift) & BIT(i))
8718 			txmcs_map |= IEEE80211_HE_MCS_SUPPORT_0_11 << (i * 2);
8719 		else
8720 			txmcs_map |= IEEE80211_HE_MCS_NOT_SUPPORTED << (i * 2);
8721 
8722 		if (i < ar->num_rx_chains &&
8723 		    (ar->cfg_rx_chainmask >> cap->tx_chain_mask_shift) & BIT(i))
8724 			rxmcs_map |= IEEE80211_HE_MCS_SUPPORT_0_11 << (i * 2);
8725 		else
8726 			rxmcs_map |= IEEE80211_HE_MCS_NOT_SUPPORTED << (i * 2);
8727 
8728 		if (i < maxtxnss_160 &&
8729 		    (ar->cfg_tx_chainmask >> cap->tx_chain_mask_shift) & BIT(i))
8730 			txmcs_map_160 |= IEEE80211_HE_MCS_SUPPORT_0_11 << (i * 2);
8731 		else
8732 			txmcs_map_160 |= IEEE80211_HE_MCS_NOT_SUPPORTED << (i * 2);
8733 
8734 		if (i < maxrxnss_160 &&
8735 		    (ar->cfg_tx_chainmask >> cap->tx_chain_mask_shift) & BIT(i))
8736 			rxmcs_map_160 |= IEEE80211_HE_MCS_SUPPORT_0_11 << (i * 2);
8737 		else
8738 			rxmcs_map_160 |= IEEE80211_HE_MCS_NOT_SUPPORTED << (i * 2);
8739 	}
8740 
8741 	mcs_nss->rx_mcs_80 = cpu_to_le16(rxmcs_map & 0xffff);
8742 	mcs_nss->tx_mcs_80 = cpu_to_le16(txmcs_map & 0xffff);
8743 	mcs_nss->rx_mcs_160 = cpu_to_le16(rxmcs_map_160 & 0xffff);
8744 	mcs_nss->tx_mcs_160 = cpu_to_le16(txmcs_map_160 & 0xffff);
8745 }
8746 
8747 static void ath12k_mac_copy_he_cap(struct ath12k *ar,
8748 				   struct ath12k_band_cap *band_cap,
8749 				   int iftype, u8 num_tx_chains,
8750 				   struct ieee80211_sta_he_cap *he_cap)
8751 {
8752 	struct ieee80211_he_cap_elem *he_cap_elem = &he_cap->he_cap_elem;
8753 
8754 	he_cap->has_he = true;
8755 	memcpy(he_cap_elem->mac_cap_info, band_cap->he_cap_info,
8756 	       sizeof(he_cap_elem->mac_cap_info));
8757 	memcpy(he_cap_elem->phy_cap_info, band_cap->he_cap_phy_info,
8758 	       sizeof(he_cap_elem->phy_cap_info));
8759 
8760 	he_cap_elem->mac_cap_info[1] &=
8761 		IEEE80211_HE_MAC_CAP1_TF_MAC_PAD_DUR_MASK;
8762 	he_cap_elem->phy_cap_info[0] &=
8763 		IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_40MHZ_IN_2G |
8764 		IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_40MHZ_80MHZ_IN_5G |
8765 		IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_160MHZ_IN_5G;
8766 	/* 80PLUS80 is not supported */
8767 	he_cap_elem->phy_cap_info[0] &=
8768 		~IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_80PLUS80_MHZ_IN_5G;
8769 	he_cap_elem->phy_cap_info[5] &=
8770 		~IEEE80211_HE_PHY_CAP5_BEAMFORMEE_NUM_SND_DIM_UNDER_80MHZ_MASK;
8771 	he_cap_elem->phy_cap_info[5] |= num_tx_chains - 1;
8772 
8773 	switch (iftype) {
8774 	case NL80211_IFTYPE_AP:
8775 		he_cap_elem->mac_cap_info[2] &=
8776 			~IEEE80211_HE_MAC_CAP2_BCAST_TWT;
8777 		he_cap_elem->phy_cap_info[3] &=
8778 			~IEEE80211_HE_PHY_CAP3_DCM_MAX_CONST_TX_MASK;
8779 		he_cap_elem->phy_cap_info[9] |=
8780 			IEEE80211_HE_PHY_CAP9_RX_1024_QAM_LESS_THAN_242_TONE_RU;
8781 		break;
8782 	case NL80211_IFTYPE_STATION:
8783 		he_cap_elem->mac_cap_info[0] &= ~IEEE80211_HE_MAC_CAP0_TWT_RES;
8784 		he_cap_elem->mac_cap_info[0] |= IEEE80211_HE_MAC_CAP0_TWT_REQ;
8785 		he_cap_elem->phy_cap_info[9] |=
8786 			IEEE80211_HE_PHY_CAP9_TX_1024_QAM_LESS_THAN_242_TONE_RU;
8787 		break;
8788 	case NL80211_IFTYPE_MESH_POINT:
8789 		ath12k_mac_filter_he_cap_mesh(he_cap_elem);
8790 		break;
8791 	}
8792 
8793 	ath12k_mac_set_hemcsmap(ar, &ar->pdev->cap, he_cap);
8794 	memset(he_cap->ppe_thres, 0, sizeof(he_cap->ppe_thres));
8795 	if (he_cap_elem->phy_cap_info[6] &
8796 	    IEEE80211_HE_PHY_CAP6_PPE_THRESHOLD_PRESENT)
8797 		ath12k_gen_ppe_thresh(&band_cap->he_ppet, he_cap->ppe_thres);
8798 }
8799 
8800 static void
8801 ath12k_mac_copy_eht_mcs_nss(struct ath12k_band_cap *band_cap,
8802 			    struct ieee80211_eht_mcs_nss_supp *mcs_nss,
8803 			    const struct ieee80211_he_cap_elem *he_cap,
8804 			    const struct ieee80211_eht_cap_elem_fixed *eht_cap)
8805 {
8806 	if ((he_cap->phy_cap_info[0] &
8807 	     (IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_40MHZ_IN_2G |
8808 	      IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_40MHZ_80MHZ_IN_5G |
8809 	      IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_160MHZ_IN_5G |
8810 	      IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_80PLUS80_MHZ_IN_5G)) == 0)
8811 		memcpy(&mcs_nss->only_20mhz, &band_cap->eht_mcs_20_only,
8812 		       sizeof(struct ieee80211_eht_mcs_nss_supp_20mhz_only));
8813 
8814 	if (he_cap->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 		memcpy(&mcs_nss->bw._80, &band_cap->eht_mcs_80,
8818 		       sizeof(struct ieee80211_eht_mcs_nss_supp_bw));
8819 
8820 	if (he_cap->phy_cap_info[0] &
8821 	    IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_160MHZ_IN_5G)
8822 		memcpy(&mcs_nss->bw._160, &band_cap->eht_mcs_160,
8823 		       sizeof(struct ieee80211_eht_mcs_nss_supp_bw));
8824 
8825 	if (eht_cap->phy_cap_info[0] & IEEE80211_EHT_PHY_CAP0_320MHZ_IN_6GHZ)
8826 		memcpy(&mcs_nss->bw._320, &band_cap->eht_mcs_320,
8827 		       sizeof(struct ieee80211_eht_mcs_nss_supp_bw));
8828 }
8829 
8830 static void ath12k_mac_copy_eht_ppe_thresh(struct ath12k_wmi_ppe_threshold_arg *fw_ppet,
8831 					   struct ieee80211_sta_eht_cap *cap)
8832 {
8833 	u16 bit = IEEE80211_EHT_PPE_THRES_INFO_HEADER_SIZE;
8834 	u8 i, nss, ru, ppet_bit_len_per_ru = IEEE80211_EHT_PPE_THRES_INFO_PPET_SIZE * 2;
8835 
8836 	u8p_replace_bits(&cap->eht_ppe_thres[0], fw_ppet->numss_m1,
8837 			 IEEE80211_EHT_PPE_THRES_NSS_MASK);
8838 
8839 	u16p_replace_bits((u16 *)&cap->eht_ppe_thres[0], fw_ppet->ru_bit_mask,
8840 			  IEEE80211_EHT_PPE_THRES_RU_INDEX_BITMASK_MASK);
8841 
8842 	for (nss = 0; nss <= fw_ppet->numss_m1; nss++) {
8843 		for (ru = 0;
8844 		     ru < hweight16(IEEE80211_EHT_PPE_THRES_RU_INDEX_BITMASK_MASK);
8845 		     ru++) {
8846 			u32 val = 0;
8847 
8848 			if ((fw_ppet->ru_bit_mask & BIT(ru)) == 0)
8849 				continue;
8850 
8851 			u32p_replace_bits(&val, fw_ppet->ppet16_ppet8_ru3_ru0[nss] >>
8852 						(ru * ppet_bit_len_per_ru),
8853 					  GENMASK(ppet_bit_len_per_ru - 1, 0));
8854 
8855 			for (i = 0; i < ppet_bit_len_per_ru; i++) {
8856 				cap->eht_ppe_thres[bit / 8] |=
8857 					(((val >> i) & 0x1) << ((bit % 8)));
8858 				bit++;
8859 			}
8860 		}
8861 	}
8862 }
8863 
8864 static void
8865 ath12k_mac_filter_eht_cap_mesh(struct ieee80211_eht_cap_elem_fixed
8866 			       *eht_cap_elem)
8867 {
8868 	u8 m;
8869 
8870 	m = IEEE80211_EHT_MAC_CAP0_EPCS_PRIO_ACCESS;
8871 	eht_cap_elem->mac_cap_info[0] &= ~m;
8872 
8873 	m = IEEE80211_EHT_PHY_CAP0_PARTIAL_BW_UL_MU_MIMO;
8874 	eht_cap_elem->phy_cap_info[0] &= ~m;
8875 
8876 	m = IEEE80211_EHT_PHY_CAP3_NG_16_MU_FEEDBACK |
8877 	    IEEE80211_EHT_PHY_CAP3_CODEBOOK_7_5_MU_FDBK |
8878 	    IEEE80211_EHT_PHY_CAP3_TRIG_MU_BF_PART_BW_FDBK |
8879 	    IEEE80211_EHT_PHY_CAP3_TRIG_CQI_FDBK;
8880 	eht_cap_elem->phy_cap_info[3] &= ~m;
8881 
8882 	m = IEEE80211_EHT_PHY_CAP4_PART_BW_DL_MU_MIMO |
8883 	    IEEE80211_EHT_PHY_CAP4_PSR_SR_SUPP |
8884 	    IEEE80211_EHT_PHY_CAP4_POWER_BOOST_FACT_SUPP |
8885 	    IEEE80211_EHT_PHY_CAP4_EHT_MU_PPDU_4_EHT_LTF_08_GI;
8886 	eht_cap_elem->phy_cap_info[4] &= ~m;
8887 
8888 	m = IEEE80211_EHT_PHY_CAP5_NON_TRIG_CQI_FEEDBACK |
8889 	    IEEE80211_EHT_PHY_CAP5_TX_LESS_242_TONE_RU_SUPP |
8890 	    IEEE80211_EHT_PHY_CAP5_RX_LESS_242_TONE_RU_SUPP |
8891 	    IEEE80211_EHT_PHY_CAP5_MAX_NUM_SUPP_EHT_LTF_MASK;
8892 	eht_cap_elem->phy_cap_info[5] &= ~m;
8893 
8894 	m = IEEE80211_EHT_PHY_CAP6_MAX_NUM_SUPP_EHT_LTF_MASK;
8895 	eht_cap_elem->phy_cap_info[6] &= ~m;
8896 
8897 	m = IEEE80211_EHT_PHY_CAP7_NON_OFDMA_UL_MU_MIMO_80MHZ |
8898 	    IEEE80211_EHT_PHY_CAP7_NON_OFDMA_UL_MU_MIMO_160MHZ |
8899 	    IEEE80211_EHT_PHY_CAP7_NON_OFDMA_UL_MU_MIMO_320MHZ |
8900 	    IEEE80211_EHT_PHY_CAP7_MU_BEAMFORMER_80MHZ |
8901 	    IEEE80211_EHT_PHY_CAP7_MU_BEAMFORMER_160MHZ |
8902 	    IEEE80211_EHT_PHY_CAP7_MU_BEAMFORMER_320MHZ;
8903 	eht_cap_elem->phy_cap_info[7] &= ~m;
8904 }
8905 
8906 static void ath12k_mac_copy_eht_cap(struct ath12k *ar,
8907 				    struct ath12k_band_cap *band_cap,
8908 				    struct ieee80211_he_cap_elem *he_cap_elem,
8909 				    int iftype,
8910 				    struct ieee80211_sta_eht_cap *eht_cap)
8911 {
8912 	struct ieee80211_eht_cap_elem_fixed *eht_cap_elem = &eht_cap->eht_cap_elem;
8913 
8914 	memset(eht_cap, 0, sizeof(struct ieee80211_sta_eht_cap));
8915 
8916 	if (!(test_bit(WMI_TLV_SERVICE_11BE, ar->ab->wmi_ab.svc_map)) ||
8917 	    ath12k_acpi_get_disable_11be(ar->ab))
8918 		return;
8919 
8920 	eht_cap->has_eht = true;
8921 	memcpy(eht_cap_elem->mac_cap_info, band_cap->eht_cap_mac_info,
8922 	       sizeof(eht_cap_elem->mac_cap_info));
8923 	memcpy(eht_cap_elem->phy_cap_info, band_cap->eht_cap_phy_info,
8924 	       sizeof(eht_cap_elem->phy_cap_info));
8925 
8926 	switch (iftype) {
8927 	case NL80211_IFTYPE_AP:
8928 		eht_cap_elem->phy_cap_info[0] &=
8929 			~IEEE80211_EHT_PHY_CAP0_242_TONE_RU_GT20MHZ;
8930 		eht_cap_elem->phy_cap_info[4] &=
8931 			~IEEE80211_EHT_PHY_CAP4_PART_BW_DL_MU_MIMO;
8932 		eht_cap_elem->phy_cap_info[5] &=
8933 			~IEEE80211_EHT_PHY_CAP5_TX_LESS_242_TONE_RU_SUPP;
8934 		break;
8935 	case NL80211_IFTYPE_STATION:
8936 		eht_cap_elem->phy_cap_info[7] &=
8937 			~(IEEE80211_EHT_PHY_CAP7_NON_OFDMA_UL_MU_MIMO_80MHZ |
8938 			  IEEE80211_EHT_PHY_CAP7_NON_OFDMA_UL_MU_MIMO_160MHZ |
8939 			  IEEE80211_EHT_PHY_CAP7_NON_OFDMA_UL_MU_MIMO_320MHZ);
8940 		eht_cap_elem->phy_cap_info[7] &=
8941 			~(IEEE80211_EHT_PHY_CAP7_MU_BEAMFORMER_80MHZ |
8942 			  IEEE80211_EHT_PHY_CAP7_MU_BEAMFORMER_160MHZ |
8943 			  IEEE80211_EHT_PHY_CAP7_MU_BEAMFORMER_320MHZ);
8944 		break;
8945 	case NL80211_IFTYPE_MESH_POINT:
8946 		ath12k_mac_filter_eht_cap_mesh(eht_cap_elem);
8947 		break;
8948 	default:
8949 		break;
8950 	}
8951 
8952 	ath12k_mac_copy_eht_mcs_nss(band_cap, &eht_cap->eht_mcs_nss_supp,
8953 				    he_cap_elem, eht_cap_elem);
8954 
8955 	if (eht_cap_elem->phy_cap_info[5] &
8956 	    IEEE80211_EHT_PHY_CAP5_PPE_THRESHOLD_PRESENT)
8957 		ath12k_mac_copy_eht_ppe_thresh(&band_cap->eht_ppet, eht_cap);
8958 }
8959 
8960 static int ath12k_mac_copy_sband_iftype_data(struct ath12k *ar,
8961 					     struct ath12k_pdev_cap *cap,
8962 					     struct ieee80211_sband_iftype_data *data,
8963 					     int band)
8964 {
8965 	struct ath12k_band_cap *band_cap = &cap->band[band];
8966 	int i, idx = 0;
8967 
8968 	for (i = 0; i < NUM_NL80211_IFTYPES; i++) {
8969 		struct ieee80211_sta_he_cap *he_cap = &data[idx].he_cap;
8970 
8971 		switch (i) {
8972 		case NL80211_IFTYPE_STATION:
8973 		case NL80211_IFTYPE_AP:
8974 		case NL80211_IFTYPE_MESH_POINT:
8975 			break;
8976 
8977 		default:
8978 			continue;
8979 		}
8980 
8981 		data[idx].types_mask = BIT(i);
8982 
8983 		ath12k_mac_copy_he_cap(ar, band_cap, i, ar->num_tx_chains, he_cap);
8984 		if (band == NL80211_BAND_6GHZ) {
8985 			data[idx].he_6ghz_capa.capa =
8986 				ath12k_mac_setup_he_6ghz_cap(cap, band_cap);
8987 		}
8988 		ath12k_mac_copy_eht_cap(ar, band_cap, &he_cap->he_cap_elem, i,
8989 					&data[idx].eht_cap);
8990 		idx++;
8991 	}
8992 
8993 	return idx;
8994 }
8995 
8996 static void ath12k_mac_setup_sband_iftype_data(struct ath12k *ar,
8997 					       struct ath12k_pdev_cap *cap)
8998 {
8999 	struct ieee80211_supported_band *sband;
9000 	enum nl80211_band band;
9001 	int count;
9002 
9003 	if (cap->supported_bands & WMI_HOST_WLAN_2GHZ_CAP) {
9004 		band = NL80211_BAND_2GHZ;
9005 		count = ath12k_mac_copy_sband_iftype_data(ar, cap,
9006 							  ar->mac.iftype[band],
9007 							  band);
9008 		sband = &ar->mac.sbands[band];
9009 		_ieee80211_set_sband_iftype_data(sband, ar->mac.iftype[band],
9010 						 count);
9011 	}
9012 
9013 	if (cap->supported_bands & WMI_HOST_WLAN_5GHZ_CAP) {
9014 		band = NL80211_BAND_5GHZ;
9015 		count = ath12k_mac_copy_sband_iftype_data(ar, cap,
9016 							  ar->mac.iftype[band],
9017 							  band);
9018 		sband = &ar->mac.sbands[band];
9019 		_ieee80211_set_sband_iftype_data(sband, ar->mac.iftype[band],
9020 						 count);
9021 	}
9022 
9023 	if (cap->supported_bands & WMI_HOST_WLAN_5GHZ_CAP &&
9024 	    ar->supports_6ghz) {
9025 		band = NL80211_BAND_6GHZ;
9026 		count = ath12k_mac_copy_sband_iftype_data(ar, cap,
9027 							  ar->mac.iftype[band],
9028 							  band);
9029 		sband = &ar->mac.sbands[band];
9030 		_ieee80211_set_sband_iftype_data(sband, ar->mac.iftype[band],
9031 						 count);
9032 	}
9033 }
9034 
9035 static int __ath12k_set_antenna(struct ath12k *ar, u32 tx_ant, u32 rx_ant)
9036 {
9037 	struct ath12k_hw *ah = ath12k_ar_to_ah(ar);
9038 	int ret;
9039 
9040 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
9041 
9042 	if (ath12k_check_chain_mask(ar, tx_ant, true))
9043 		return -EINVAL;
9044 
9045 	if (ath12k_check_chain_mask(ar, rx_ant, false))
9046 		return -EINVAL;
9047 
9048 	/* Since we advertised the max cap of all radios combined during wiphy
9049 	 * registration, ensure we don't set the antenna config higher than the
9050 	 * limits
9051 	 */
9052 	tx_ant = min_t(u32, tx_ant, ar->pdev->cap.tx_chain_mask);
9053 	rx_ant = min_t(u32, rx_ant, ar->pdev->cap.rx_chain_mask);
9054 
9055 	ar->cfg_tx_chainmask = tx_ant;
9056 	ar->cfg_rx_chainmask = rx_ant;
9057 
9058 	if (ah->state != ATH12K_HW_STATE_ON &&
9059 	    ah->state != ATH12K_HW_STATE_RESTARTED)
9060 		return 0;
9061 
9062 	ret = ath12k_wmi_pdev_set_param(ar, WMI_PDEV_PARAM_TX_CHAIN_MASK,
9063 					tx_ant, ar->pdev->pdev_id);
9064 	if (ret) {
9065 		ath12k_warn(ar->ab, "failed to set tx-chainmask: %d, req 0x%x\n",
9066 			    ret, tx_ant);
9067 		return ret;
9068 	}
9069 
9070 	ar->num_tx_chains = hweight32(tx_ant);
9071 
9072 	ret = ath12k_wmi_pdev_set_param(ar, WMI_PDEV_PARAM_RX_CHAIN_MASK,
9073 					rx_ant, ar->pdev->pdev_id);
9074 	if (ret) {
9075 		ath12k_warn(ar->ab, "failed to set rx-chainmask: %d, req 0x%x\n",
9076 			    ret, rx_ant);
9077 		return ret;
9078 	}
9079 
9080 	ar->num_rx_chains = hweight32(rx_ant);
9081 
9082 	/* Reload HT/VHT/HE capability */
9083 	ath12k_mac_setup_ht_vht_cap(ar, &ar->pdev->cap, NULL);
9084 	ath12k_mac_setup_sband_iftype_data(ar, &ar->pdev->cap);
9085 
9086 	return 0;
9087 }
9088 
9089 static void ath12k_mgmt_over_wmi_tx_drop(struct ath12k *ar, struct sk_buff *skb)
9090 {
9091 	int num_mgmt;
9092 
9093 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
9094 
9095 	ieee80211_free_txskb(ath12k_ar_to_hw(ar), skb);
9096 
9097 	num_mgmt = atomic_dec_if_positive(&ar->num_pending_mgmt_tx);
9098 
9099 	if (num_mgmt < 0)
9100 		WARN_ON_ONCE(1);
9101 
9102 	if (!num_mgmt)
9103 		wake_up(&ar->txmgmt_empty_waitq);
9104 }
9105 
9106 static void ath12k_mac_tx_mgmt_free(struct ath12k *ar, int buf_id)
9107 {
9108 	struct sk_buff *msdu;
9109 	struct ieee80211_tx_info *info;
9110 
9111 	spin_lock_bh(&ar->txmgmt_idr_lock);
9112 	msdu = idr_remove(&ar->txmgmt_idr, buf_id);
9113 	spin_unlock_bh(&ar->txmgmt_idr_lock);
9114 
9115 	if (!msdu)
9116 		return;
9117 
9118 	dma_unmap_single(ar->ab->dev, ATH12K_SKB_CB(msdu)->paddr, msdu->len,
9119 			 DMA_TO_DEVICE);
9120 
9121 	info = IEEE80211_SKB_CB(msdu);
9122 	memset(&info->status, 0, sizeof(info->status));
9123 
9124 	ath12k_mgmt_over_wmi_tx_drop(ar, msdu);
9125 }
9126 
9127 int ath12k_mac_tx_mgmt_pending_free(int buf_id, void *skb, void *ctx)
9128 {
9129 	struct ath12k *ar = ctx;
9130 
9131 	ath12k_mac_tx_mgmt_free(ar, buf_id);
9132 
9133 	return 0;
9134 }
9135 
9136 static int ath12k_mac_vif_txmgmt_idr_remove(int buf_id, void *skb, void *ctx)
9137 {
9138 	struct ieee80211_vif *vif = ctx;
9139 	struct ath12k_skb_cb *skb_cb = ATH12K_SKB_CB(skb);
9140 	struct ath12k *ar = skb_cb->ar;
9141 
9142 	if (skb_cb->vif == vif)
9143 		ath12k_mac_tx_mgmt_free(ar, buf_id);
9144 
9145 	return 0;
9146 }
9147 
9148 static int ath12k_mac_mgmt_tx_wmi(struct ath12k *ar, struct ath12k_link_vif *arvif,
9149 				  struct sk_buff *skb)
9150 {
9151 	struct ath12k_base *ab = ar->ab;
9152 	struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
9153 	struct ath12k_skb_cb *skb_cb = ATH12K_SKB_CB(skb);
9154 	struct ieee80211_tx_info *info;
9155 	enum hal_encrypt_type enctype;
9156 	unsigned int mic_len;
9157 	dma_addr_t paddr;
9158 	int buf_id;
9159 	int ret;
9160 
9161 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
9162 
9163 	skb_cb->ar = ar;
9164 	spin_lock_bh(&ar->txmgmt_idr_lock);
9165 	buf_id = idr_alloc(&ar->txmgmt_idr, skb, 0,
9166 			   ATH12K_TX_MGMT_NUM_PENDING_MAX, GFP_ATOMIC);
9167 	spin_unlock_bh(&ar->txmgmt_idr_lock);
9168 	if (buf_id < 0)
9169 		return -ENOSPC;
9170 
9171 	info = IEEE80211_SKB_CB(skb);
9172 	if ((skb_cb->flags & ATH12K_SKB_CIPHER_SET) &&
9173 	    !(info->flags & IEEE80211_TX_CTL_HW_80211_ENCAP)) {
9174 		if ((ieee80211_is_action(hdr->frame_control) ||
9175 		     ieee80211_is_deauth(hdr->frame_control) ||
9176 		     ieee80211_is_disassoc(hdr->frame_control)) &&
9177 		     ieee80211_has_protected(hdr->frame_control)) {
9178 			enctype = ath12k_dp_tx_get_encrypt_type(skb_cb->cipher);
9179 			mic_len = ath12k_dp_rx_crypto_mic_len(ab->dp, enctype);
9180 			skb_put(skb, mic_len);
9181 		}
9182 	}
9183 
9184 	paddr = dma_map_single(ab->dev, skb->data, skb->len, DMA_TO_DEVICE);
9185 	if (dma_mapping_error(ab->dev, paddr)) {
9186 		ath12k_warn(ab, "failed to DMA map mgmt Tx buffer\n");
9187 		ret = -EIO;
9188 		goto err_free_idr;
9189 	}
9190 
9191 	skb_cb->paddr = paddr;
9192 
9193 	ret = ath12k_wmi_mgmt_send(arvif, buf_id, skb);
9194 	if (ret) {
9195 		ath12k_warn(ar->ab, "failed to send mgmt frame: %d\n", ret);
9196 		goto err_unmap_buf;
9197 	}
9198 
9199 	return 0;
9200 
9201 err_unmap_buf:
9202 	dma_unmap_single(ab->dev, skb_cb->paddr,
9203 			 skb->len, DMA_TO_DEVICE);
9204 err_free_idr:
9205 	spin_lock_bh(&ar->txmgmt_idr_lock);
9206 	idr_remove(&ar->txmgmt_idr, buf_id);
9207 	spin_unlock_bh(&ar->txmgmt_idr_lock);
9208 
9209 	return ret;
9210 }
9211 
9212 static void ath12k_mgmt_over_wmi_tx_purge(struct ath12k *ar)
9213 {
9214 	struct sk_buff *skb;
9215 
9216 	while ((skb = skb_dequeue(&ar->wmi_mgmt_tx_queue)) != NULL)
9217 		ath12k_mgmt_over_wmi_tx_drop(ar, skb);
9218 }
9219 
9220 static int ath12k_mac_mgmt_action_frame_fill_elem_data(struct ath12k_link_vif *arvif,
9221 						       struct sk_buff *skb)
9222 {
9223 	struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
9224 	u8 category, *buf, iv_len, action_code, dialog_token;
9225 	struct ieee80211_bss_conf *link_conf;
9226 	struct ieee80211_chanctx_conf *conf;
9227 	int cur_tx_power, max_tx_power;
9228 	struct ath12k *ar = arvif->ar;
9229 	struct ieee80211_hw *hw = ath12k_ar_to_hw(ar);
9230 	struct wiphy *wiphy = hw->wiphy;
9231 	struct ath12k_skb_cb *skb_cb;
9232 	struct ieee80211_mgmt *mgmt;
9233 	unsigned int remaining_len;
9234 	bool has_protected;
9235 
9236 	lockdep_assert_wiphy(wiphy);
9237 
9238 	/* make sure category field is present */
9239 	if (skb->len < IEEE80211_MIN_ACTION_SIZE(category))
9240 		return -EINVAL;
9241 
9242 	remaining_len = skb->len - IEEE80211_MIN_ACTION_SIZE(category);
9243 	has_protected = ieee80211_has_protected(hdr->frame_control);
9244 
9245 	/* In case of SW crypto and hdr protected (PMF), packet will already be encrypted,
9246 	 * we can't put in data in this case
9247 	 */
9248 	if (test_bit(ATH12K_FLAG_HW_CRYPTO_DISABLED, &ar->ab->dev_flags) &&
9249 	    has_protected)
9250 		return 0;
9251 
9252 	mgmt = (struct ieee80211_mgmt *)hdr;
9253 	buf = (u8 *)&mgmt->u.action;
9254 
9255 	/* FCTL_PROTECTED frame might have extra space added for HDR_LEN. Offset that
9256 	 * many bytes if it is there
9257 	 */
9258 	if (has_protected) {
9259 		skb_cb = ATH12K_SKB_CB(skb);
9260 
9261 		switch (skb_cb->cipher) {
9262 		/* Cipher suite having flag %IEEE80211_KEY_FLAG_GENERATE_IV_MGMT set in
9263 		 * key needs to be processed. See ath12k_install_key()
9264 		 */
9265 		case WLAN_CIPHER_SUITE_CCMP:
9266 		case WLAN_CIPHER_SUITE_CCMP_256:
9267 		case WLAN_CIPHER_SUITE_GCMP:
9268 		case WLAN_CIPHER_SUITE_GCMP_256:
9269 			iv_len = IEEE80211_CCMP_HDR_LEN;
9270 			break;
9271 		case WLAN_CIPHER_SUITE_TKIP:
9272 			iv_len = 0;
9273 			break;
9274 		default:
9275 			return -EINVAL;
9276 		}
9277 
9278 		if (remaining_len < iv_len)
9279 			return -EINVAL;
9280 
9281 		buf += iv_len;
9282 		remaining_len -= iv_len;
9283 	}
9284 
9285 	category = *buf++;
9286 	/* category code is already taken care in %IEEE80211_MIN_ACTION_SIZE hence
9287 	 * no need to adjust remaining_len
9288 	 */
9289 
9290 	switch (category) {
9291 	case WLAN_CATEGORY_RADIO_MEASUREMENT:
9292 		/* need action code and dialog token */
9293 		if (remaining_len < 2)
9294 			return -EINVAL;
9295 
9296 		/* Packet Format:
9297 		 *	Action Code | Dialog Token | Variable Len (based on Action Code)
9298 		 */
9299 		action_code = *buf++;
9300 		dialog_token = *buf++;
9301 		remaining_len -= 2;
9302 
9303 		link_conf = ath12k_mac_get_link_bss_conf(arvif);
9304 		if (!link_conf) {
9305 			ath12k_warn(ar->ab,
9306 				    "failed to get bss link conf for vdev %d in RM handling\n",
9307 				    arvif->vdev_id);
9308 			return -EINVAL;
9309 		}
9310 
9311 		conf = wiphy_dereference(wiphy, link_conf->chanctx_conf);
9312 		if (!conf)
9313 			return -ENOENT;
9314 
9315 		cur_tx_power = link_conf->txpower;
9316 		max_tx_power = min(conf->def.chan->max_reg_power,
9317 				   (int)ar->max_tx_power / 2);
9318 
9319 		ath12k_mac_op_get_txpower(hw, arvif->ahvif->vif, arvif->link_id,
9320 					  &cur_tx_power);
9321 
9322 		switch (action_code) {
9323 		case WLAN_RM_ACTION_LINK_MEASUREMENT_REQUEST:
9324 			/* need variable fields to be present in len */
9325 			if (remaining_len < 2)
9326 				return -EINVAL;
9327 
9328 			/* Variable length format as defined in IEEE 802.11-2024,
9329 			 * Figure 9-1187-Link Measurement Request frame Action field
9330 			 * format.
9331 			 *	Transmit Power | Max Tx Power
9332 			 * We fill both of these.
9333 			 */
9334 			*buf++ = cur_tx_power;
9335 			*buf = max_tx_power;
9336 
9337 			ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
9338 				   "RRM: Link Measurement Req dialog_token %u cur_tx_power %d max_tx_power %d\n",
9339 				   dialog_token, cur_tx_power, max_tx_power);
9340 			break;
9341 		case WLAN_RM_ACTION_LINK_MEASUREMENT_REPORT:
9342 			/* need variable fields to be present in len */
9343 			if (remaining_len < 3)
9344 				return -EINVAL;
9345 
9346 			/* Variable length format as defined in IEEE 802.11-2024,
9347 			 * Figure 9-1188-Link Measurement Report frame Action field format
9348 			 *	TPC Report | Variable Fields
9349 			 *
9350 			 * TPC Report Format:
9351 			 *	Element ID | Len | Tx Power | Link Margin
9352 			 *
9353 			 * We fill Tx power in the TPC Report (2nd index)
9354 			 */
9355 			buf[2] = cur_tx_power;
9356 
9357 			/* TODO: At present, Link margin data is not present so can't
9358 			 * really fill it now. Once it is available, it can be added
9359 			 * here
9360 			 */
9361 			ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
9362 				   "RRM: Link Measurement Report dialog_token %u cur_tx_power %d\n",
9363 				   dialog_token, cur_tx_power);
9364 			break;
9365 		default:
9366 			return -EINVAL;
9367 		}
9368 		break;
9369 	default:
9370 		/* nothing to fill */
9371 		return 0;
9372 	}
9373 
9374 	return 0;
9375 }
9376 
9377 static int ath12k_mac_mgmt_frame_fill_elem_data(struct ath12k_link_vif *arvif,
9378 						struct sk_buff *skb)
9379 {
9380 	struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
9381 
9382 	if (!ieee80211_is_action(hdr->frame_control))
9383 		return 0;
9384 
9385 	return ath12k_mac_mgmt_action_frame_fill_elem_data(arvif, skb);
9386 }
9387 
9388 static void ath12k_mgmt_over_wmi_tx_work(struct wiphy *wiphy, struct wiphy_work *work)
9389 {
9390 	struct ath12k *ar = container_of(work, struct ath12k, wmi_mgmt_tx_work);
9391 	struct ath12k_hw *ah = ar->ah;
9392 	struct ath12k_skb_cb *skb_cb;
9393 	struct ath12k_vif *ahvif;
9394 	struct ath12k_link_vif *arvif;
9395 	struct sk_buff *skb;
9396 	int ret;
9397 
9398 	lockdep_assert_wiphy(wiphy);
9399 
9400 	while ((skb = skb_dequeue(&ar->wmi_mgmt_tx_queue)) != NULL) {
9401 		skb_cb = ATH12K_SKB_CB(skb);
9402 		if (!skb_cb->vif) {
9403 			ath12k_warn(ar->ab, "no vif found for mgmt frame\n");
9404 			ath12k_mgmt_over_wmi_tx_drop(ar, skb);
9405 			continue;
9406 		}
9407 
9408 		ahvif = ath12k_vif_to_ahvif(skb_cb->vif);
9409 		if (!(ahvif->links_map & BIT(skb_cb->link_id))) {
9410 			ath12k_warn(ar->ab,
9411 				    "invalid linkid %u in mgmt over wmi tx with linkmap 0x%x\n",
9412 				    skb_cb->link_id, ahvif->links_map);
9413 			ath12k_mgmt_over_wmi_tx_drop(ar, skb);
9414 			continue;
9415 		}
9416 
9417 		arvif = wiphy_dereference(ah->hw->wiphy, ahvif->link[skb_cb->link_id]);
9418 		if (ar->allocated_vdev_map & (1LL << arvif->vdev_id)) {
9419 			/* Fill in the data which is required to be filled by the driver
9420 			 * For example: Max Tx power in Link Measurement Request/Report
9421 			 */
9422 			ret = ath12k_mac_mgmt_frame_fill_elem_data(arvif, skb);
9423 			if (ret) {
9424 				/* If we couldn't fill the data due to any reason,
9425 				 * let's not discard transmitting the packet.
9426 				 * For example: Software crypto and PMF case
9427 				 */
9428 				ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
9429 					   "Failed to fill the required data for the mgmt packet err %d\n",
9430 					   ret);
9431 			}
9432 
9433 			ret = ath12k_mac_mgmt_tx_wmi(ar, arvif, skb);
9434 			if (ret) {
9435 				ath12k_warn(ar->ab, "failed to tx mgmt frame, vdev_id %d :%d\n",
9436 					    arvif->vdev_id, ret);
9437 				ath12k_mgmt_over_wmi_tx_drop(ar, skb);
9438 			}
9439 		} else {
9440 			ath12k_warn(ar->ab,
9441 				    "dropping mgmt frame for vdev %d link %u is_started %d\n",
9442 				    arvif->vdev_id,
9443 				    skb_cb->link_id,
9444 				    arvif->is_started);
9445 			ath12k_mgmt_over_wmi_tx_drop(ar, skb);
9446 		}
9447 	}
9448 }
9449 
9450 int ath12k_mac_mgmt_tx(struct ath12k *ar, struct sk_buff *skb,
9451 		       bool is_prb_rsp)
9452 {
9453 	struct sk_buff_head *q = &ar->wmi_mgmt_tx_queue;
9454 
9455 	if (test_bit(ATH12K_FLAG_CRASH_FLUSH, &ar->ab->dev_flags))
9456 		return -ESHUTDOWN;
9457 
9458 	/* Drop probe response packets when the pending management tx
9459 	 * count has reached a certain threshold, so as to prioritize
9460 	 * other mgmt packets like auth and assoc to be sent on time
9461 	 * for establishing successful connections.
9462 	 */
9463 	if (is_prb_rsp &&
9464 	    atomic_read(&ar->num_pending_mgmt_tx) > ATH12K_PRB_RSP_DROP_THRESHOLD) {
9465 		ath12k_warn(ar->ab,
9466 			    "dropping probe response as pending queue is almost full\n");
9467 		return -ENOSPC;
9468 	}
9469 
9470 	if (skb_queue_len_lockless(q) >= ATH12K_TX_MGMT_NUM_PENDING_MAX) {
9471 		ath12k_warn(ar->ab, "mgmt tx queue is full\n");
9472 		return -ENOSPC;
9473 	}
9474 
9475 	skb_queue_tail(q, skb);
9476 	atomic_inc(&ar->num_pending_mgmt_tx);
9477 	wiphy_work_queue(ath12k_ar_to_hw(ar)->wiphy, &ar->wmi_mgmt_tx_work);
9478 
9479 	return 0;
9480 }
9481 EXPORT_SYMBOL(ath12k_mac_mgmt_tx);
9482 
9483 void ath12k_mac_add_p2p_noa_ie(struct ath12k *ar,
9484 			       struct ieee80211_vif *vif,
9485 			       struct sk_buff *skb,
9486 			       bool is_prb_rsp)
9487 {
9488 	struct ath12k_vif *ahvif = ath12k_vif_to_ahvif(vif);
9489 
9490 	if (likely(!is_prb_rsp))
9491 		return;
9492 
9493 	spin_lock_bh(&ar->data_lock);
9494 
9495 	if (ahvif->u.ap.noa_data &&
9496 	    !pskb_expand_head(skb, 0, ahvif->u.ap.noa_len,
9497 			      GFP_ATOMIC))
9498 		skb_put_data(skb, ahvif->u.ap.noa_data,
9499 			     ahvif->u.ap.noa_len);
9500 
9501 	spin_unlock_bh(&ar->data_lock);
9502 }
9503 EXPORT_SYMBOL(ath12k_mac_add_p2p_noa_ie);
9504 
9505 /* Note: called under rcu_read_lock() */
9506 void ath12k_mlo_mcast_update_tx_link_address(struct ieee80211_vif *vif,
9507 					     u8 link_id, struct sk_buff *skb,
9508 					     u32 info_flags)
9509 {
9510 	struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
9511 	struct ieee80211_bss_conf *bss_conf;
9512 
9513 	if (info_flags & IEEE80211_TX_CTL_HW_80211_ENCAP)
9514 		return;
9515 
9516 	bss_conf = rcu_dereference(vif->link_conf[link_id]);
9517 	if (bss_conf)
9518 		ether_addr_copy(hdr->addr2, bss_conf->addr);
9519 }
9520 EXPORT_SYMBOL(ath12k_mlo_mcast_update_tx_link_address);
9521 
9522 /* Note: called under rcu_read_lock() */
9523 u8 ath12k_mac_get_tx_link(struct ieee80211_sta *sta, struct ieee80211_vif *vif,
9524 			  u8 link, struct sk_buff *skb, u32 info_flags)
9525 {
9526 	struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
9527 	struct ath12k_vif *ahvif = ath12k_vif_to_ahvif(vif);
9528 	struct ieee80211_link_sta *link_sta;
9529 	struct ieee80211_bss_conf *bss_conf;
9530 	struct ath12k_sta *ahsta;
9531 
9532 	/* Use the link id passed or the default vif link */
9533 	if (!sta) {
9534 		if (link != IEEE80211_LINK_UNSPECIFIED)
9535 			return link;
9536 
9537 		return ahvif->deflink.link_id;
9538 	}
9539 
9540 	ahsta = ath12k_sta_to_ahsta(sta);
9541 
9542 	/* Below translation ensures we pass proper A2 & A3 for non ML clients.
9543 	 * Also it assumes for now support only for MLO AP in this path
9544 	 */
9545 	if (!sta->mlo) {
9546 		link = ahsta->deflink.link_id;
9547 
9548 		if (info_flags & IEEE80211_TX_CTL_HW_80211_ENCAP)
9549 			return link;
9550 
9551 		bss_conf = rcu_dereference(vif->link_conf[link]);
9552 		if (bss_conf) {
9553 			ether_addr_copy(hdr->addr2, bss_conf->addr);
9554 			if (!ieee80211_has_tods(hdr->frame_control) &&
9555 			    !ieee80211_has_fromds(hdr->frame_control))
9556 				ether_addr_copy(hdr->addr3, bss_conf->addr);
9557 		}
9558 
9559 		return link;
9560 	}
9561 
9562 	/* enqueue eth enacap & data frames on primary link, FW does link
9563 	 * selection and address translation.
9564 	 */
9565 	if (info_flags & IEEE80211_TX_CTL_HW_80211_ENCAP ||
9566 	    ieee80211_is_data(hdr->frame_control))
9567 		return ahsta->assoc_link_id;
9568 
9569 	/* 802.11 frame cases */
9570 	if (link == IEEE80211_LINK_UNSPECIFIED)
9571 		link = ahsta->deflink.link_id;
9572 
9573 	if (!ieee80211_is_mgmt(hdr->frame_control))
9574 		return link;
9575 
9576 	/* Perform address conversion for ML STA Tx */
9577 	bss_conf = rcu_dereference(vif->link_conf[link]);
9578 	link_sta = rcu_dereference(sta->link[link]);
9579 
9580 	if (bss_conf && link_sta) {
9581 		ether_addr_copy(hdr->addr1, link_sta->addr);
9582 		ether_addr_copy(hdr->addr2, bss_conf->addr);
9583 
9584 		if (vif->type == NL80211_IFTYPE_STATION && bss_conf->bssid)
9585 			ether_addr_copy(hdr->addr3, bss_conf->bssid);
9586 		else if (vif->type == NL80211_IFTYPE_AP)
9587 			ether_addr_copy(hdr->addr3, bss_conf->addr);
9588 
9589 		return link;
9590 	}
9591 
9592 	if (bss_conf) {
9593 		/* In certain cases where a ML sta associated and added subset of
9594 		 * links on which the ML AP is active, but now sends some frame
9595 		 * (ex. Probe request) on a different link which is active in our
9596 		 * MLD but was not added during previous association, we can
9597 		 * still honor the Tx to that ML STA via the requested link.
9598 		 * The control would reach here in such case only when that link
9599 		 * address is same as the MLD address or in worst case clients
9600 		 * used MLD address at TA wrongly which would have helped
9601 		 * identify the ML sta object and pass it here.
9602 		 * If the link address of that STA is different from MLD address,
9603 		 * then the sta object would be NULL and control won't reach
9604 		 * here but return at the start of the function itself with !sta
9605 		 * check. Also this would not need any translation at hdr->addr1
9606 		 * from MLD to link address since the RA is the MLD address
9607 		 * (same as that link address ideally) already.
9608 		 */
9609 		ether_addr_copy(hdr->addr2, bss_conf->addr);
9610 
9611 		if (vif->type == NL80211_IFTYPE_STATION && bss_conf->bssid)
9612 			ether_addr_copy(hdr->addr3, bss_conf->bssid);
9613 		else if (vif->type == NL80211_IFTYPE_AP)
9614 			ether_addr_copy(hdr->addr3, bss_conf->addr);
9615 	}
9616 
9617 	return link;
9618 }
9619 EXPORT_SYMBOL(ath12k_mac_get_tx_link);
9620 
9621 void ath12k_mac_drain_tx(struct ath12k *ar)
9622 {
9623 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
9624 
9625 	/* make sure rcu-protected mac80211 tx path itself is drained */
9626 	synchronize_net();
9627 
9628 	wiphy_work_cancel(ath12k_ar_to_hw(ar)->wiphy, &ar->wmi_mgmt_tx_work);
9629 	ath12k_mgmt_over_wmi_tx_purge(ar);
9630 }
9631 
9632 static int ath12k_mac_config_mon_status_default(struct ath12k *ar, bool enable)
9633 {
9634 	struct htt_rx_ring_tlv_filter tlv_filter = {};
9635 	struct ath12k_base *ab = ar->ab;
9636 	u32 ring_id, i;
9637 	int ret = 0;
9638 
9639 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
9640 
9641 	if (!ab->hw_params->rxdma1_enable)
9642 		return ret;
9643 
9644 	if (enable) {
9645 		tlv_filter = ath12k_mac_mon_status_filter_default;
9646 
9647 		if (ath12k_debugfs_rx_filter(ar))
9648 			tlv_filter.rx_filter = ath12k_debugfs_rx_filter(ar);
9649 	} else {
9650 		tlv_filter.rxmon_disable = true;
9651 	}
9652 
9653 	for (i = 0; i < ab->hw_params->num_rxdma_per_pdev; i++) {
9654 		ring_id = ar->dp.rxdma_mon_dst_ring[i].ring_id;
9655 		ret = ath12k_dp_tx_htt_rx_filter_setup(ab, ring_id,
9656 						       ar->dp.mac_id + i,
9657 						       HAL_RXDMA_MONITOR_DST,
9658 						       DP_RXDMA_REFILL_RING_SIZE,
9659 						       &tlv_filter);
9660 		if (ret) {
9661 			ath12k_err(ab,
9662 				   "failed to setup filter for monitor buf %d\n",
9663 				   ret);
9664 		}
9665 	}
9666 
9667 	return ret;
9668 }
9669 
9670 static int ath12k_mac_start(struct ath12k *ar)
9671 {
9672 	struct ath12k_hw *ah = ar->ah;
9673 	struct ath12k_base *ab = ar->ab;
9674 	struct ath12k_pdev *pdev = ar->pdev;
9675 	int ret;
9676 
9677 	lockdep_assert_held(&ah->hw_mutex);
9678 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
9679 
9680 	ret = ath12k_wmi_pdev_set_param(ar, WMI_PDEV_PARAM_PMF_QOS,
9681 					1, pdev->pdev_id);
9682 
9683 	if (ret) {
9684 		ath12k_err(ab, "failed to enable PMF QOS: %d\n", ret);
9685 		goto err;
9686 	}
9687 
9688 	ret = ath12k_wmi_pdev_set_param(ar, WMI_PDEV_PARAM_DYNAMIC_BW, 1,
9689 					pdev->pdev_id);
9690 	if (ret) {
9691 		ath12k_err(ab, "failed to enable dynamic bw: %d\n", ret);
9692 		goto err;
9693 	}
9694 
9695 	ret = ath12k_wmi_pdev_set_param(ar, WMI_PDEV_PARAM_ARP_AC_OVERRIDE,
9696 					0, pdev->pdev_id);
9697 	if (ret) {
9698 		ath12k_err(ab, "failed to set ac override for ARP: %d\n",
9699 			   ret);
9700 		goto err;
9701 	}
9702 
9703 	ret = ath12k_wmi_send_dfs_phyerr_offload_enable_cmd(ar, pdev->pdev_id);
9704 	if (ret) {
9705 		ath12k_err(ab, "failed to offload radar detection: %d\n",
9706 			   ret);
9707 		goto err;
9708 	}
9709 
9710 	ret = ath12k_dp_tx_htt_h2t_ppdu_stats_req(ar,
9711 						  HTT_PPDU_STATS_TAG_DEFAULT);
9712 	if (ret) {
9713 		ath12k_err(ab, "failed to req ppdu stats: %d\n", ret);
9714 		goto err;
9715 	}
9716 
9717 	ret = ath12k_wmi_pdev_set_param(ar, WMI_PDEV_PARAM_MESH_MCAST_ENABLE,
9718 					1, pdev->pdev_id);
9719 
9720 	if (ret) {
9721 		ath12k_err(ab, "failed to enable MESH MCAST ENABLE: (%d\n", ret);
9722 		goto err;
9723 	}
9724 
9725 	__ath12k_set_antenna(ar, ar->cfg_tx_chainmask, ar->cfg_rx_chainmask);
9726 
9727 	/* TODO: Do we need to enable ANI? */
9728 
9729 	ret = ath12k_reg_update_chan_list(ar, false);
9730 
9731 	/* The ar state alone can be turned off for non supported country
9732 	 * without returning the error value. As we need to update the channel
9733 	 * for the next ar.
9734 	 */
9735 	if (ret) {
9736 		if (ret == -EINVAL)
9737 			ret = 0;
9738 		goto err;
9739 	}
9740 
9741 	ar->num_started_vdevs = 0;
9742 	ar->num_created_vdevs = 0;
9743 	ar->num_peers = 0;
9744 	ar->allocated_vdev_map = 0;
9745 	ar->chan_tx_pwr = ATH12K_PDEV_TX_POWER_INVALID;
9746 
9747 	spin_lock_bh(&ar->data_lock);
9748 	ar->incumbent_signal_interference.handling_in_progress = false;
9749 	spin_unlock_bh(&ar->data_lock);
9750 
9751 	/* Configure monitor status ring with default rx_filter to get rx status
9752 	 * such as rssi, rx_duration.
9753 	 */
9754 	ret = ath12k_mac_config_mon_status_default(ar, true);
9755 	if (ret && (ret != -EOPNOTSUPP)) {
9756 		ath12k_err(ab, "failed to configure monitor status ring with default rx_filter: (%d)\n",
9757 			   ret);
9758 		goto err;
9759 	}
9760 
9761 	if (ret == -EOPNOTSUPP)
9762 		ath12k_dbg(ab, ATH12K_DBG_MAC,
9763 			   "monitor status config is not yet supported");
9764 
9765 	/* Configure the hash seed for hash based reo dest ring selection */
9766 	ath12k_wmi_pdev_lro_cfg(ar, ar->pdev->pdev_id);
9767 
9768 	/* allow device to enter IMPS */
9769 	if (ab->hw_params->idle_ps) {
9770 		ret = ath12k_wmi_pdev_set_param(ar, WMI_PDEV_PARAM_IDLE_PS_CONFIG,
9771 						1, pdev->pdev_id);
9772 		if (ret) {
9773 			ath12k_err(ab, "failed to enable idle ps: %d\n", ret);
9774 			goto err;
9775 		}
9776 	}
9777 
9778 	ret = ath12k_thermal_throttling_config_default(ar);
9779 	if (ret) {
9780 		ath12k_err(ab, "failed to set thermal throttle: %d\n", ret);
9781 		goto err;
9782 	}
9783 
9784 	rcu_assign_pointer(ab->pdevs_active[ar->pdev_idx],
9785 			   &ab->pdevs[ar->pdev_idx]);
9786 
9787 	return 0;
9788 err:
9789 
9790 	return ret;
9791 }
9792 
9793 static void ath12k_drain_tx(struct ath12k_hw *ah)
9794 {
9795 	struct ath12k *ar;
9796 	int i;
9797 
9798 	lockdep_assert_wiphy(ah->hw->wiphy);
9799 
9800 	for_each_ar(ah, ar, i)
9801 		ath12k_mac_drain_tx(ar);
9802 }
9803 
9804 int ath12k_mac_op_start(struct ieee80211_hw *hw)
9805 {
9806 	struct ath12k_hw *ah = ath12k_hw_to_ah(hw);
9807 	struct ath12k *ar;
9808 	int ret, i;
9809 
9810 	if (ath12k_ftm_mode)
9811 		return -EPERM;
9812 
9813 	lockdep_assert_wiphy(hw->wiphy);
9814 
9815 	ath12k_drain_tx(ah);
9816 
9817 	guard(mutex)(&ah->hw_mutex);
9818 
9819 	switch (ah->state) {
9820 	case ATH12K_HW_STATE_OFF:
9821 		ah->state = ATH12K_HW_STATE_ON;
9822 		break;
9823 	case ATH12K_HW_STATE_RESTARTING:
9824 		ah->state = ATH12K_HW_STATE_RESTARTED;
9825 		break;
9826 	case ATH12K_HW_STATE_RESTARTED:
9827 	case ATH12K_HW_STATE_WEDGED:
9828 	case ATH12K_HW_STATE_ON:
9829 	case ATH12K_HW_STATE_TM:
9830 		ah->state = ATH12K_HW_STATE_OFF;
9831 
9832 		WARN_ON(1);
9833 		return -EINVAL;
9834 	}
9835 
9836 	for_each_ar(ah, ar, i) {
9837 		ret = ath12k_mac_start(ar);
9838 		if (ret) {
9839 			ah->state = ATH12K_HW_STATE_OFF;
9840 
9841 			ath12k_err(ar->ab, "fail to start mac operations in pdev idx %d ret %d\n",
9842 				   ar->pdev_idx, ret);
9843 			goto fail_start;
9844 		}
9845 	}
9846 
9847 	return 0;
9848 
9849 fail_start:
9850 	for (; i > 0; i--) {
9851 		ar = ath12k_ah_to_ar(ah, i - 1);
9852 		ath12k_mac_stop(ar);
9853 	}
9854 
9855 	return ret;
9856 }
9857 EXPORT_SYMBOL(ath12k_mac_op_start);
9858 
9859 int ath12k_mac_rfkill_config(struct ath12k *ar)
9860 {
9861 	struct ath12k_base *ab = ar->ab;
9862 	u32 param;
9863 	int ret;
9864 
9865 	if (ab->hw_params->rfkill_pin == 0)
9866 		return -EOPNOTSUPP;
9867 
9868 	ath12k_dbg(ab, ATH12K_DBG_MAC,
9869 		   "mac rfkill_pin %d rfkill_cfg %d rfkill_on_level %d",
9870 		   ab->hw_params->rfkill_pin, ab->hw_params->rfkill_cfg,
9871 		   ab->hw_params->rfkill_on_level);
9872 
9873 	param = u32_encode_bits(ab->hw_params->rfkill_on_level,
9874 				WMI_RFKILL_CFG_RADIO_LEVEL) |
9875 		u32_encode_bits(ab->hw_params->rfkill_pin,
9876 				WMI_RFKILL_CFG_GPIO_PIN_NUM) |
9877 		u32_encode_bits(ab->hw_params->rfkill_cfg,
9878 				WMI_RFKILL_CFG_PIN_AS_GPIO);
9879 
9880 	ret = ath12k_wmi_pdev_set_param(ar, WMI_PDEV_PARAM_HW_RFKILL_CONFIG,
9881 					param, ar->pdev->pdev_id);
9882 	if (ret) {
9883 		ath12k_warn(ab,
9884 			    "failed to set rfkill config 0x%x: %d\n",
9885 			    param, ret);
9886 		return ret;
9887 	}
9888 
9889 	return 0;
9890 }
9891 
9892 int ath12k_mac_rfkill_enable_radio(struct ath12k *ar, bool enable)
9893 {
9894 	enum wmi_rfkill_enable_radio param;
9895 	int ret;
9896 
9897 	if (enable)
9898 		param = WMI_RFKILL_ENABLE_RADIO_ON;
9899 	else
9900 		param = WMI_RFKILL_ENABLE_RADIO_OFF;
9901 
9902 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac %d rfkill enable %d",
9903 		   ar->pdev_idx, param);
9904 
9905 	ret = ath12k_wmi_pdev_set_param(ar, WMI_PDEV_PARAM_RFKILL_ENABLE,
9906 					param, ar->pdev->pdev_id);
9907 	if (ret) {
9908 		ath12k_warn(ar->ab, "failed to set rfkill enable param %d: %d\n",
9909 			    param, ret);
9910 		return ret;
9911 	}
9912 
9913 	return 0;
9914 }
9915 
9916 static void ath12k_mac_stop(struct ath12k *ar)
9917 {
9918 	struct ath12k_pdev_dp *dp_pdev = &ar->dp;
9919 	struct ath12k_hw *ah = ar->ah;
9920 	struct htt_ppdu_stats_info *ppdu_stats, *tmp;
9921 	struct ath12k_wmi_scan_chan_list_arg *arg;
9922 	int ret;
9923 
9924 	lockdep_assert_held(&ah->hw_mutex);
9925 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
9926 
9927 	ret = ath12k_mac_config_mon_status_default(ar, false);
9928 	if (ret && (ret != -EOPNOTSUPP))
9929 		ath12k_err(ar->ab, "failed to clear rx_filter for monitor status ring: (%d)\n",
9930 			   ret);
9931 
9932 	clear_bit(ATH12K_FLAG_CAC_RUNNING, &ar->dev_flags);
9933 
9934 	cancel_delayed_work_sync(&ar->scan.timeout);
9935 	wiphy_work_cancel(ath12k_ar_to_hw(ar)->wiphy, &ar->scan.vdev_clean_wk);
9936 	cancel_work_sync(&ar->regd_channel_update_work);
9937 	cancel_work_sync(&ar->regd_update_work);
9938 	cancel_work_sync(&ar->ab->rfkill_work);
9939 	cancel_work_sync(&ar->ab->update_11d_work);
9940 	ar->state_11d = ATH12K_11D_IDLE;
9941 	complete(&ar->completed_11d_scan);
9942 
9943 	spin_lock_bh(&dp_pdev->ppdu_list_lock);
9944 	list_for_each_entry_safe(ppdu_stats, tmp, &dp_pdev->ppdu_stats_info, list) {
9945 		list_del(&ppdu_stats->list);
9946 		kfree(ppdu_stats);
9947 	}
9948 	spin_unlock_bh(&dp_pdev->ppdu_list_lock);
9949 
9950 	spin_lock_bh(&ar->data_lock);
9951 	while ((arg = list_first_entry_or_null(&ar->regd_channel_update_queue,
9952 					       struct ath12k_wmi_scan_chan_list_arg,
9953 					       list))) {
9954 		list_del(&arg->list);
9955 		kfree(arg);
9956 	}
9957 	spin_unlock_bh(&ar->data_lock);
9958 
9959 	rcu_assign_pointer(ar->ab->pdevs_active[ar->pdev_idx], NULL);
9960 
9961 	synchronize_rcu();
9962 
9963 	atomic_set(&ar->num_pending_mgmt_tx, 0);
9964 
9965 	spin_lock_bh(&ar->data_lock);
9966 	ar->incumbent_signal_interference.handling_in_progress = false;
9967 	spin_unlock_bh(&ar->data_lock);
9968 }
9969 
9970 void ath12k_mac_op_stop(struct ieee80211_hw *hw, bool suspend)
9971 {
9972 	struct ath12k_hw *ah = ath12k_hw_to_ah(hw);
9973 	struct ath12k *ar;
9974 	int i;
9975 
9976 	lockdep_assert_wiphy(hw->wiphy);
9977 
9978 	ath12k_drain_tx(ah);
9979 
9980 	mutex_lock(&ah->hw_mutex);
9981 
9982 	ah->state = ATH12K_HW_STATE_OFF;
9983 
9984 	for_each_ar(ah, ar, i)
9985 		ath12k_mac_stop(ar);
9986 
9987 	mutex_unlock(&ah->hw_mutex);
9988 }
9989 EXPORT_SYMBOL(ath12k_mac_op_stop);
9990 
9991 static u8
9992 ath12k_mac_get_vdev_stats_id(struct ath12k_link_vif *arvif)
9993 {
9994 	struct ath12k_base *ab = arvif->ar->ab;
9995 	u8 vdev_stats_id = 0;
9996 
9997 	do {
9998 		if (ab->free_vdev_stats_id_map & (1LL << vdev_stats_id)) {
9999 			vdev_stats_id++;
10000 			if (vdev_stats_id >= ATH12K_MAX_VDEV_STATS_ID) {
10001 				vdev_stats_id = ATH12K_INVAL_VDEV_STATS_ID;
10002 				break;
10003 			}
10004 		} else {
10005 			ab->free_vdev_stats_id_map |= (1LL << vdev_stats_id);
10006 			break;
10007 		}
10008 	} while (vdev_stats_id);
10009 
10010 	arvif->vdev_stats_id = vdev_stats_id;
10011 	return vdev_stats_id;
10012 }
10013 
10014 static int ath12k_mac_setup_vdev_params_mbssid(struct ath12k_link_vif *arvif,
10015 					       u32 *flags, u32 *tx_vdev_id)
10016 {
10017 	struct ath12k_vif *ahvif = arvif->ahvif;
10018 	struct ieee80211_bss_conf *link_conf;
10019 	struct ath12k *ar = arvif->ar;
10020 	struct ath12k_link_vif *tx_arvif;
10021 
10022 	link_conf = ath12k_mac_get_link_bss_conf(arvif);
10023 	if (!link_conf) {
10024 		ath12k_warn(ar->ab, "unable to access bss link conf in set mbssid params for vif %pM link %u\n",
10025 			    ahvif->vif->addr, arvif->link_id);
10026 		return -ENOLINK;
10027 	}
10028 
10029 	tx_arvif = ath12k_mac_get_tx_arvif(arvif, link_conf);
10030 	if (!tx_arvif)
10031 		return 0;
10032 
10033 	if (link_conf->nontransmitted) {
10034 		if (ath12k_ar_to_hw(ar)->wiphy !=
10035 		    ath12k_ar_to_hw(tx_arvif->ar)->wiphy)
10036 			return -EINVAL;
10037 
10038 		*flags = WMI_VDEV_MBSSID_FLAGS_NON_TRANSMIT_AP;
10039 		*tx_vdev_id = tx_arvif->vdev_id;
10040 	} else if (tx_arvif == arvif) {
10041 		*flags = WMI_VDEV_MBSSID_FLAGS_TRANSMIT_AP;
10042 	} else {
10043 		return -EINVAL;
10044 	}
10045 
10046 	if (link_conf->ema_ap)
10047 		*flags |= WMI_VDEV_MBSSID_FLAGS_EMA_MODE;
10048 
10049 	return 0;
10050 }
10051 
10052 static int ath12k_mac_setup_vdev_create_arg(struct ath12k_link_vif *arvif,
10053 					    struct ath12k_wmi_vdev_create_arg *arg)
10054 {
10055 	struct ath12k *ar = arvif->ar;
10056 	struct ath12k_pdev *pdev = ar->pdev;
10057 	struct ath12k_vif *ahvif = arvif->ahvif;
10058 	int ret;
10059 
10060 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
10061 
10062 	arg->if_id = arvif->vdev_id;
10063 	arg->type = ahvif->vdev_type;
10064 	arg->subtype = ahvif->vdev_subtype;
10065 	arg->pdev_id = pdev->pdev_id;
10066 
10067 	arg->mbssid_flags = WMI_VDEV_MBSSID_FLAGS_NON_MBSSID_AP;
10068 	arg->mbssid_tx_vdev_id = 0;
10069 	if (!test_bit(WMI_TLV_SERVICE_MBSS_PARAM_IN_VDEV_START_SUPPORT,
10070 		      ar->ab->wmi_ab.svc_map)) {
10071 		ret = ath12k_mac_setup_vdev_params_mbssid(arvif,
10072 							  &arg->mbssid_flags,
10073 							  &arg->mbssid_tx_vdev_id);
10074 		if (ret)
10075 			return ret;
10076 	}
10077 
10078 	if (pdev->cap.supported_bands & WMI_HOST_WLAN_2GHZ_CAP) {
10079 		arg->chains[NL80211_BAND_2GHZ].tx = ar->num_tx_chains;
10080 		arg->chains[NL80211_BAND_2GHZ].rx = ar->num_rx_chains;
10081 	}
10082 	if (pdev->cap.supported_bands & WMI_HOST_WLAN_5GHZ_CAP) {
10083 		arg->chains[NL80211_BAND_5GHZ].tx = ar->num_tx_chains;
10084 		arg->chains[NL80211_BAND_5GHZ].rx = ar->num_rx_chains;
10085 	}
10086 	if (pdev->cap.supported_bands & WMI_HOST_WLAN_5GHZ_CAP &&
10087 	    ar->supports_6ghz) {
10088 		arg->chains[NL80211_BAND_6GHZ].tx = ar->num_tx_chains;
10089 		arg->chains[NL80211_BAND_6GHZ].rx = ar->num_rx_chains;
10090 	}
10091 
10092 	arg->if_stats_id = ath12k_mac_get_vdev_stats_id(arvif);
10093 
10094 	if (ath12k_mac_is_ml_arvif(arvif)) {
10095 		if (hweight16(ahvif->vif->valid_links) > ATH12K_WMI_MLO_MAX_LINKS) {
10096 			ath12k_warn(ar->ab, "too many MLO links during setting up vdev: %d",
10097 				    ahvif->vif->valid_links);
10098 			return -EINVAL;
10099 		}
10100 
10101 		ether_addr_copy(arg->mld_addr, ahvif->vif->addr);
10102 	}
10103 
10104 	return 0;
10105 }
10106 
10107 static void ath12k_mac_update_vif_offload(struct ath12k_link_vif *arvif)
10108 {
10109 	struct ath12k_vif *ahvif = arvif->ahvif;
10110 	struct ieee80211_vif *vif = ath12k_ahvif_to_vif(ahvif);
10111 	struct ath12k *ar = arvif->ar;
10112 	struct ath12k_base *ab = ar->ab;
10113 	u32 param_id, param_value;
10114 	int ret;
10115 
10116 	param_id = WMI_VDEV_PARAM_TX_ENCAP_TYPE;
10117 	if (vif->type != NL80211_IFTYPE_STATION &&
10118 	    vif->type != NL80211_IFTYPE_AP)
10119 		vif->offload_flags &= ~(IEEE80211_OFFLOAD_ENCAP_ENABLED |
10120 					IEEE80211_OFFLOAD_DECAP_ENABLED);
10121 
10122 	if (vif->offload_flags & IEEE80211_OFFLOAD_ENCAP_ENABLED) {
10123 		ahvif->dp_vif.tx_encap_type = ATH12K_HW_TXRX_ETHERNET;
10124 		vif->offload_flags |= IEEE80211_OFFLOAD_ENCAP_4ADDR;
10125 	} else if (test_bit(ATH12K_FLAG_RAW_MODE, &ab->dev_flags)) {
10126 		ahvif->dp_vif.tx_encap_type = ATH12K_HW_TXRX_RAW;
10127 	} else {
10128 		ahvif->dp_vif.tx_encap_type = ATH12K_HW_TXRX_NATIVE_WIFI;
10129 	}
10130 
10131 	ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
10132 					    param_id, ahvif->dp_vif.tx_encap_type);
10133 	if (ret) {
10134 		ath12k_warn(ab, "failed to set vdev %d tx encap mode: %d\n",
10135 			    arvif->vdev_id, ret);
10136 		vif->offload_flags &= ~IEEE80211_OFFLOAD_ENCAP_ENABLED;
10137 	}
10138 
10139 	param_id = WMI_VDEV_PARAM_RX_DECAP_TYPE;
10140 	if (vif->offload_flags & IEEE80211_OFFLOAD_DECAP_ENABLED)
10141 		param_value = ATH12K_HW_TXRX_ETHERNET;
10142 	else if (test_bit(ATH12K_FLAG_RAW_MODE, &ab->dev_flags))
10143 		param_value = ATH12K_HW_TXRX_RAW;
10144 	else
10145 		param_value = ATH12K_HW_TXRX_NATIVE_WIFI;
10146 
10147 	ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
10148 					    param_id, param_value);
10149 	if (ret) {
10150 		ath12k_warn(ab, "failed to set vdev %d rx decap mode: %d\n",
10151 			    arvif->vdev_id, ret);
10152 		vif->offload_flags &= ~IEEE80211_OFFLOAD_DECAP_ENABLED;
10153 	}
10154 }
10155 
10156 void ath12k_mac_op_update_vif_offload(struct ieee80211_hw *hw,
10157 				      struct ieee80211_vif *vif)
10158 {
10159 	struct ath12k_vif *ahvif = ath12k_vif_to_ahvif(vif);
10160 	struct ath12k_link_vif *arvif;
10161 	unsigned long links;
10162 	int link_id;
10163 
10164 	lockdep_assert_wiphy(hw->wiphy);
10165 
10166 	if (vif->valid_links) {
10167 		links = vif->valid_links;
10168 		for_each_set_bit(link_id, &links, IEEE80211_MLD_MAX_NUM_LINKS) {
10169 			arvif = wiphy_dereference(hw->wiphy, ahvif->link[link_id]);
10170 			if (!(arvif && arvif->ar))
10171 				continue;
10172 
10173 			ath12k_mac_update_vif_offload(arvif);
10174 		}
10175 
10176 		return;
10177 	}
10178 
10179 	ath12k_mac_update_vif_offload(&ahvif->deflink);
10180 }
10181 EXPORT_SYMBOL(ath12k_mac_op_update_vif_offload);
10182 
10183 static bool ath12k_mac_vif_ap_active_any(struct ath12k_base *ab)
10184 {
10185 	struct ath12k *ar;
10186 	struct ath12k_pdev *pdev;
10187 	struct ath12k_link_vif *arvif;
10188 	int i;
10189 
10190 	for (i = 0; i < ab->num_radios; i++) {
10191 		pdev = &ab->pdevs[i];
10192 		ar = pdev->ar;
10193 		list_for_each_entry(arvif, &ar->arvifs, list) {
10194 			if (arvif->is_up &&
10195 			    arvif->ahvif->vdev_type == WMI_VDEV_TYPE_AP)
10196 				return true;
10197 		}
10198 	}
10199 	return false;
10200 }
10201 
10202 void ath12k_mac_11d_scan_start(struct ath12k *ar, u32 vdev_id)
10203 {
10204 	struct wmi_11d_scan_start_arg arg;
10205 	int ret;
10206 
10207 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
10208 
10209 	if (ar->regdom_set_by_user)
10210 		goto fin;
10211 
10212 	if (ar->vdev_id_11d_scan != ATH12K_11D_INVALID_VDEV_ID)
10213 		goto fin;
10214 
10215 	if (!test_bit(WMI_TLV_SERVICE_11D_OFFLOAD, ar->ab->wmi_ab.svc_map))
10216 		goto fin;
10217 
10218 	if (ath12k_mac_vif_ap_active_any(ar->ab))
10219 		goto fin;
10220 
10221 	arg.vdev_id = vdev_id;
10222 	arg.start_interval_msec = 0;
10223 	arg.scan_period_msec = ATH12K_SCAN_11D_INTERVAL;
10224 
10225 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
10226 		   "mac start 11d scan for vdev %d\n", vdev_id);
10227 
10228 	ret = ath12k_wmi_send_11d_scan_start_cmd(ar, &arg);
10229 	if (ret) {
10230 		ath12k_warn(ar->ab, "failed to start 11d scan vdev %d ret: %d\n",
10231 			    vdev_id, ret);
10232 	} else {
10233 		ar->vdev_id_11d_scan = vdev_id;
10234 		if (ar->state_11d == ATH12K_11D_PREPARING)
10235 			ar->state_11d = ATH12K_11D_RUNNING;
10236 	}
10237 
10238 fin:
10239 	if (ar->state_11d == ATH12K_11D_PREPARING) {
10240 		ar->state_11d = ATH12K_11D_IDLE;
10241 		complete(&ar->completed_11d_scan);
10242 	}
10243 }
10244 
10245 void ath12k_mac_11d_scan_stop(struct ath12k *ar)
10246 {
10247 	int ret;
10248 	u32 vdev_id;
10249 
10250 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
10251 
10252 	if (!test_bit(WMI_TLV_SERVICE_11D_OFFLOAD, ar->ab->wmi_ab.svc_map))
10253 		return;
10254 
10255 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac stop 11d for vdev %d\n",
10256 		   ar->vdev_id_11d_scan);
10257 
10258 	if (ar->state_11d == ATH12K_11D_PREPARING) {
10259 		ar->state_11d = ATH12K_11D_IDLE;
10260 		complete(&ar->completed_11d_scan);
10261 	}
10262 
10263 	if (ar->vdev_id_11d_scan != ATH12K_11D_INVALID_VDEV_ID) {
10264 		vdev_id = ar->vdev_id_11d_scan;
10265 
10266 		ret = ath12k_wmi_send_11d_scan_stop_cmd(ar, vdev_id);
10267 		if (ret) {
10268 			ath12k_warn(ar->ab,
10269 				    "failed to stopt 11d scan vdev %d ret: %d\n",
10270 				    vdev_id, ret);
10271 		} else {
10272 			ar->vdev_id_11d_scan = ATH12K_11D_INVALID_VDEV_ID;
10273 			ar->state_11d = ATH12K_11D_IDLE;
10274 			complete(&ar->completed_11d_scan);
10275 		}
10276 	}
10277 }
10278 
10279 void ath12k_mac_11d_scan_stop_all(struct ath12k_base *ab)
10280 {
10281 	struct ath12k *ar;
10282 	struct ath12k_pdev *pdev;
10283 	int i;
10284 
10285 	ath12k_dbg(ab, ATH12K_DBG_MAC, "mac stop soc 11d scan\n");
10286 
10287 	for (i = 0; i < ab->num_radios; i++) {
10288 		pdev = &ab->pdevs[i];
10289 		ar = pdev->ar;
10290 
10291 		ath12k_mac_11d_scan_stop(ar);
10292 	}
10293 }
10294 
10295 static void ath12k_mac_determine_vdev_type(struct ieee80211_vif *vif,
10296 					   struct ath12k_vif *ahvif)
10297 {
10298 	ahvif->vdev_subtype = WMI_VDEV_SUBTYPE_NONE;
10299 
10300 	switch (vif->type) {
10301 	case NL80211_IFTYPE_UNSPECIFIED:
10302 	case NL80211_IFTYPE_STATION:
10303 		ahvif->vdev_type = WMI_VDEV_TYPE_STA;
10304 
10305 		if (vif->p2p)
10306 			ahvif->vdev_subtype = WMI_VDEV_SUBTYPE_P2P_CLIENT;
10307 
10308 		break;
10309 	case NL80211_IFTYPE_MESH_POINT:
10310 		ahvif->vdev_subtype = WMI_VDEV_SUBTYPE_MESH_11S;
10311 		fallthrough;
10312 	case NL80211_IFTYPE_AP:
10313 		ahvif->vdev_type = WMI_VDEV_TYPE_AP;
10314 
10315 		if (vif->p2p)
10316 			ahvif->vdev_subtype = WMI_VDEV_SUBTYPE_P2P_GO;
10317 
10318 		break;
10319 	case NL80211_IFTYPE_MONITOR:
10320 		ahvif->vdev_type = WMI_VDEV_TYPE_MONITOR;
10321 		break;
10322 	case NL80211_IFTYPE_P2P_DEVICE:
10323 		ahvif->vdev_type = WMI_VDEV_TYPE_STA;
10324 		ahvif->vdev_subtype = WMI_VDEV_SUBTYPE_P2P_DEVICE;
10325 		break;
10326 	default:
10327 		WARN_ON(1);
10328 		break;
10329 	}
10330 }
10331 
10332 int ath12k_mac_vdev_create(struct ath12k *ar, struct ath12k_link_vif *arvif)
10333 {
10334 	struct ath12k_hw *ah = ar->ah;
10335 	struct ath12k_base *ab = ar->ab;
10336 	struct ieee80211_hw *hw = ah->hw;
10337 	struct ath12k_vif *ahvif = arvif->ahvif;
10338 	struct ieee80211_vif *vif = ath12k_ahvif_to_vif(ahvif);
10339 	struct wireless_dev *wdev = ieee80211_vif_to_wdev(vif);
10340 	struct ath12k_wmi_vdev_create_arg vdev_arg = {};
10341 	struct ath12k_wmi_peer_create_arg peer_param = {};
10342 	struct ieee80211_bss_conf *link_conf = NULL;
10343 	u32 param_id, param_value;
10344 	u16 nss;
10345 	int i;
10346 	int ret, vdev_id;
10347 	u8 link_id;
10348 	struct ath12k_dp_link_vif *dp_link_vif = NULL;
10349 	struct ath12k_dp_peer_create_params params = {};
10350 	bool dp_peer_created = false;
10351 
10352 	lockdep_assert_wiphy(hw->wiphy);
10353 
10354 	/* In NO_VIRTUAL_MONITOR, its necessary to restrict only one monitor
10355 	 * interface in each radio
10356 	 */
10357 	if (vif->type == NL80211_IFTYPE_MONITOR && ar->monitor_vdev_created)
10358 		return -EINVAL;
10359 
10360 	if (ar->num_created_vdevs >= TARGET_NUM_VDEVS(ab)) {
10361 		ath12k_warn(ab, "failed to create vdev, reached max vdev limit %d\n",
10362 			    TARGET_NUM_VDEVS(ab));
10363 		return -ENOSPC;
10364 	}
10365 
10366 	link_id = arvif->link_id;
10367 
10368 	if (link_id < IEEE80211_MLD_MAX_NUM_LINKS) {
10369 		link_conf = wiphy_dereference(hw->wiphy, vif->link_conf[link_id]);
10370 		if (!link_conf) {
10371 			ath12k_warn(ar->ab, "unable to access bss link conf in vdev create for vif %pM link %u\n",
10372 				    vif->addr, arvif->link_id);
10373 			return -ENOLINK;
10374 		}
10375 	}
10376 
10377 	if (link_conf)
10378 		memcpy(arvif->bssid, link_conf->addr, ETH_ALEN);
10379 	else
10380 		memcpy(arvif->bssid, vif->addr, ETH_ALEN);
10381 
10382 	arvif->ar = ar;
10383 	vdev_id = __ffs64(ab->free_vdev_map);
10384 	arvif->vdev_id = vdev_id;
10385 	if (vif->type == NL80211_IFTYPE_MONITOR)
10386 		ar->monitor_vdev_id = vdev_id;
10387 
10388 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac vdev create id %d type %d subtype %d map %llx\n",
10389 		   arvif->vdev_id, ahvif->vdev_type, ahvif->vdev_subtype,
10390 		   ab->free_vdev_map);
10391 
10392 	vif->cab_queue = arvif->vdev_id % (ATH12K_HW_MAX_QUEUES - 1);
10393 	for (i = 0; i < ARRAY_SIZE(vif->hw_queue); i++)
10394 		vif->hw_queue[i] = i % (ATH12K_HW_MAX_QUEUES - 1);
10395 
10396 	ret = ath12k_mac_setup_vdev_create_arg(arvif, &vdev_arg);
10397 	if (ret) {
10398 		ath12k_warn(ab, "failed to create vdev parameters %d: %d\n",
10399 			    arvif->vdev_id, ret);
10400 		goto err;
10401 	}
10402 
10403 	ret = ath12k_wmi_vdev_create(ar, arvif->bssid, &vdev_arg);
10404 	if (ret) {
10405 		ath12k_warn(ab, "failed to create WMI vdev %d: %d\n",
10406 			    arvif->vdev_id, ret);
10407 		goto err;
10408 	}
10409 
10410 	ar->num_created_vdevs++;
10411 	arvif->is_created = true;
10412 	ath12k_dbg(ab, ATH12K_DBG_MAC, "vdev %pM created, vdev_id %d\n",
10413 		   vif->addr, arvif->vdev_id);
10414 	ar->allocated_vdev_map |= 1LL << arvif->vdev_id;
10415 	ab->free_vdev_map &= ~(1LL << arvif->vdev_id);
10416 
10417 	spin_lock_bh(&ar->data_lock);
10418 	list_add(&arvif->list, &ar->arvifs);
10419 	spin_unlock_bh(&ar->data_lock);
10420 
10421 	ath12k_mac_update_vif_offload(arvif);
10422 
10423 	nss = hweight32(ar->cfg_tx_chainmask) ? : 1;
10424 	ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
10425 					    WMI_VDEV_PARAM_NSS, nss);
10426 	if (ret) {
10427 		ath12k_warn(ab, "failed to set vdev %d chainmask 0x%x, nss %d :%d\n",
10428 			    arvif->vdev_id, ar->cfg_tx_chainmask, nss, ret);
10429 		goto err_vdev_del;
10430 	}
10431 
10432 	dp_link_vif = ath12k_dp_vif_to_dp_link_vif(&ahvif->dp_vif, arvif->link_id);
10433 
10434 	dp_link_vif->vdev_id = arvif->vdev_id;
10435 	dp_link_vif->lmac_id = ar->lmac_id;
10436 	dp_link_vif->pdev_idx = ar->pdev_idx;
10437 
10438 	switch (ahvif->vdev_type) {
10439 	case WMI_VDEV_TYPE_AP:
10440 		params.ucast_ra_only = true;
10441 
10442 		if (arvif->link_id < IEEE80211_MLD_MAX_NUM_LINKS) {
10443 			ret = ath12k_dp_peer_create(&ah->dp_hw, arvif->bssid, &params);
10444 			if (ret) {
10445 				ath12k_warn(ab, "failed to vdev %d create dp_peer for AP: %d\n",
10446 					    arvif->vdev_id, ret);
10447 				goto err_vdev_del;
10448 			}
10449 			dp_peer_created = true;
10450 		}
10451 
10452 		peer_param.vdev_id = arvif->vdev_id;
10453 		peer_param.peer_addr = arvif->bssid;
10454 		peer_param.peer_type = WMI_PEER_TYPE_DEFAULT;
10455 		ret = ath12k_peer_create(ar, arvif, NULL, &peer_param);
10456 		if (ret) {
10457 			ath12k_warn(ab, "failed to vdev %d create peer for AP: %d\n",
10458 				    arvif->vdev_id, ret);
10459 			goto err_dp_peer_del;
10460 		}
10461 
10462 		ret = ath12k_mac_set_kickout(arvif);
10463 		if (ret) {
10464 			ath12k_warn(ar->ab, "failed to set vdev %i kickout parameters: %d\n",
10465 				    arvif->vdev_id, ret);
10466 			goto err_peer_del;
10467 		}
10468 		ath12k_mac_11d_scan_stop_all(ar->ab);
10469 		break;
10470 	case WMI_VDEV_TYPE_STA:
10471 		param_id = WMI_STA_PS_PARAM_RX_WAKE_POLICY;
10472 		param_value = WMI_STA_PS_RX_WAKE_POLICY_WAKE;
10473 		ret = ath12k_wmi_set_sta_ps_param(ar, arvif->vdev_id,
10474 						  param_id, param_value);
10475 		if (ret) {
10476 			ath12k_warn(ar->ab, "failed to set vdev %d RX wake policy: %d\n",
10477 				    arvif->vdev_id, ret);
10478 			goto err_peer_del;
10479 		}
10480 
10481 		param_id = WMI_STA_PS_PARAM_TX_WAKE_THRESHOLD;
10482 		param_value = WMI_STA_PS_TX_WAKE_THRESHOLD_ALWAYS;
10483 		ret = ath12k_wmi_set_sta_ps_param(ar, arvif->vdev_id,
10484 						  param_id, param_value);
10485 		if (ret) {
10486 			ath12k_warn(ar->ab, "failed to set vdev %d TX wake threshold: %d\n",
10487 				    arvif->vdev_id, ret);
10488 			goto err_peer_del;
10489 		}
10490 
10491 		param_id = WMI_STA_PS_PARAM_PSPOLL_COUNT;
10492 		param_value = WMI_STA_PS_PSPOLL_COUNT_NO_MAX;
10493 		ret = ath12k_wmi_set_sta_ps_param(ar, arvif->vdev_id,
10494 						  param_id, param_value);
10495 		if (ret) {
10496 			ath12k_warn(ar->ab, "failed to set vdev %d pspoll count: %d\n",
10497 				    arvif->vdev_id, ret);
10498 			goto err_peer_del;
10499 		}
10500 
10501 		ret = ath12k_wmi_pdev_set_ps_mode(ar, arvif->vdev_id, false);
10502 		if (ret) {
10503 			ath12k_warn(ar->ab, "failed to disable vdev %d ps mode: %d\n",
10504 				    arvif->vdev_id, ret);
10505 			goto err_peer_del;
10506 		}
10507 
10508 		/*
10509 		 * There could be race condition in firmware for the station
10510 		 * interface between enabling 4-address peer WMI param and
10511 		 * sending 4-address frame (NULL or EAPOL via TCL).
10512 		 * Make the station as WDS while bringup itself
10513 		 * to avoid the race condition
10514 		 */
10515 		if (vif->type == NL80211_IFTYPE_STATION &&
10516 		    (wdev && wdev->use_4addr)) {
10517 			ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
10518 							    WMI_VDEV_PARAM_WDS,
10519 							    1);
10520 			if (ret) {
10521 				ath12k_warn(ar->ab, "failed to set WDS vdev param: %d\n",
10522 					    ret);
10523 				goto err_peer_del;
10524 			}
10525 			arvif->set_wds_vdev_param = true;
10526 		}
10527 
10528 		if (test_bit(WMI_TLV_SERVICE_11D_OFFLOAD, ab->wmi_ab.svc_map) &&
10529 		    ahvif->vdev_type == WMI_VDEV_TYPE_STA &&
10530 		    ahvif->vdev_subtype == WMI_VDEV_SUBTYPE_NONE) {
10531 			reinit_completion(&ar->completed_11d_scan);
10532 			ar->state_11d = ATH12K_11D_PREPARING;
10533 		}
10534 		break;
10535 	case WMI_VDEV_TYPE_MONITOR:
10536 		ar->monitor_vdev_created = true;
10537 		break;
10538 	default:
10539 		break;
10540 	}
10541 
10542 	if (link_conf)
10543 		arvif->txpower = link_conf->txpower;
10544 	else
10545 		arvif->txpower = NL80211_TX_POWER_AUTOMATIC;
10546 
10547 	ret = ath12k_mac_txpower_recalc(ar);
10548 	if (ret)
10549 		goto err_peer_del;
10550 
10551 	param_id = WMI_VDEV_PARAM_RTS_THRESHOLD;
10552 	param_value = hw->wiphy->rts_threshold;
10553 	ar->rts_threshold = param_value;
10554 	ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
10555 					    param_id, param_value);
10556 	if (ret) {
10557 		ath12k_warn(ar->ab, "failed to set rts threshold for vdev %d: %d\n",
10558 			    arvif->vdev_id, ret);
10559 	}
10560 
10561 	ath12k_dp_vdev_tx_attach(ar, arvif);
10562 
10563 	return ret;
10564 
10565 err_peer_del:
10566 	if (ahvif->vdev_type == WMI_VDEV_TYPE_AP) {
10567 		reinit_completion(&ar->peer_delete_done);
10568 
10569 		ret = ath12k_wmi_send_peer_delete_cmd(ar, arvif->bssid,
10570 						      arvif->vdev_id);
10571 		if (ret) {
10572 			ath12k_warn(ar->ab, "failed to delete peer vdev_id %d addr %pM\n",
10573 				    arvif->vdev_id, arvif->bssid);
10574 			goto err_dp_peer_del;
10575 		}
10576 
10577 		ret = ath12k_wait_for_peer_delete_done(ar, arvif->vdev_id,
10578 						       arvif->bssid);
10579 		if (ret)
10580 			goto err_dp_peer_del;
10581 
10582 		ar->num_peers--;
10583 	}
10584 
10585 err_dp_peer_del:
10586 	if (dp_peer_created)
10587 		ath12k_dp_peer_delete(&ah->dp_hw, arvif->bssid, NULL);
10588 
10589 err_vdev_del:
10590 	if (ahvif->vdev_type == WMI_VDEV_TYPE_MONITOR) {
10591 		ar->monitor_vdev_id = -1;
10592 		ar->monitor_vdev_created = false;
10593 	}
10594 
10595 	ath12k_wmi_vdev_delete(ar, arvif->vdev_id);
10596 	ar->num_created_vdevs--;
10597 	ar->allocated_vdev_map &= ~(1LL << arvif->vdev_id);
10598 	ab->free_vdev_map |= 1LL << arvif->vdev_id;
10599 	ab->free_vdev_stats_id_map &= ~(1LL << arvif->vdev_stats_id);
10600 	spin_lock_bh(&ar->data_lock);
10601 	list_del(&arvif->list);
10602 	spin_unlock_bh(&ar->data_lock);
10603 
10604 err:
10605 	arvif->is_created = false;
10606 	arvif->ar = NULL;
10607 	return ret;
10608 }
10609 
10610 static void ath12k_mac_vif_flush_key_cache(struct ath12k_link_vif *arvif)
10611 {
10612 	struct ath12k_key_conf *key_conf, *tmp;
10613 	struct ath12k_vif *ahvif = arvif->ahvif;
10614 	struct ath12k_hw *ah = ahvif->ah;
10615 	struct ath12k_sta *ahsta;
10616 	struct ath12k_link_sta *arsta;
10617 	struct ath12k_vif_cache *cache = ahvif->cache[arvif->link_id];
10618 	int ret;
10619 
10620 	lockdep_assert_wiphy(ah->hw->wiphy);
10621 
10622 	list_for_each_entry_safe(key_conf, tmp, &cache->key_conf.list, list) {
10623 		arsta = NULL;
10624 		if (key_conf->sta) {
10625 			ahsta = ath12k_sta_to_ahsta(key_conf->sta);
10626 			arsta = wiphy_dereference(ah->hw->wiphy,
10627 						  ahsta->link[arvif->link_id]);
10628 			if (!arsta)
10629 				goto free_cache;
10630 		}
10631 
10632 		ret = ath12k_mac_set_key(arvif->ar, key_conf->cmd,
10633 					 arvif, arsta,
10634 					 key_conf->key);
10635 		if (ret)
10636 			ath12k_warn(arvif->ar->ab, "unable to apply set key param to vdev %d ret %d\n",
10637 				    arvif->vdev_id, ret);
10638 free_cache:
10639 		list_del(&key_conf->list);
10640 		kfree(key_conf);
10641 	}
10642 }
10643 
10644 static void ath12k_mac_vif_cache_flush(struct ath12k *ar, struct ath12k_link_vif *arvif)
10645 {
10646 	struct ath12k_vif *ahvif = arvif->ahvif;
10647 	struct ieee80211_vif *vif = ath12k_ahvif_to_vif(ahvif);
10648 	struct ath12k_vif_cache *cache = ahvif->cache[arvif->link_id];
10649 	struct ath12k_base *ab = ar->ab;
10650 	struct ieee80211_bss_conf *link_conf;
10651 
10652 	int ret;
10653 
10654 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
10655 
10656 	if (!cache)
10657 		return;
10658 
10659 	if (cache->tx_conf.changed) {
10660 		ret = ath12k_mac_conf_tx(arvif, cache->tx_conf.ac,
10661 					 &cache->tx_conf.tx_queue_params);
10662 		if (ret)
10663 			ath12k_warn(ab,
10664 				    "unable to apply tx config parameters to vdev %d\n",
10665 				    ret);
10666 	}
10667 
10668 	if (cache->bss_conf_changed) {
10669 		link_conf = ath12k_mac_get_link_bss_conf(arvif);
10670 		if (!link_conf) {
10671 			ath12k_warn(ar->ab, "unable to access bss link conf in cache flush for vif %pM link %u\n",
10672 				    vif->addr, arvif->link_id);
10673 			return;
10674 		}
10675 		ath12k_mac_bss_info_changed(ar, arvif, link_conf,
10676 					    cache->bss_conf_changed);
10677 	}
10678 
10679 	if (!list_empty(&cache->key_conf.list))
10680 		ath12k_mac_vif_flush_key_cache(arvif);
10681 
10682 	ath12k_ahvif_put_link_cache(ahvif, arvif->link_id);
10683 }
10684 
10685 static struct ath12k *ath12k_mac_assign_vif_to_vdev(struct ieee80211_hw *hw,
10686 						    struct ath12k_link_vif *arvif,
10687 						    struct ieee80211_chanctx_conf *ctx)
10688 {
10689 	struct ath12k_vif *ahvif = arvif->ahvif;
10690 	struct ieee80211_vif *vif = ath12k_ahvif_to_vif(ahvif);
10691 	struct ath12k_link_vif *scan_arvif;
10692 	struct ath12k_hw *ah = hw->priv;
10693 	struct ath12k *ar;
10694 	struct ath12k_base *ab;
10695 	u8 link_id = arvif->link_id, scan_link_id;
10696 	unsigned long scan_link_map;
10697 	int ret;
10698 
10699 	lockdep_assert_wiphy(hw->wiphy);
10700 
10701 	if (ah->num_radio == 1)
10702 		ar = ah->radio;
10703 	else if (ctx)
10704 		ar = ath12k_get_ar_by_ctx(hw, ctx);
10705 	else
10706 		return NULL;
10707 
10708 	if (!ar)
10709 		return NULL;
10710 
10711 	/* cleanup the scan vdev if we are done scan on that ar
10712 	 * and now we want to create for actual usage.
10713 	 */
10714 	if (ieee80211_vif_is_mld(vif)) {
10715 		scan_link_map = ahvif->links_map & ATH12K_SCAN_LINKS_MASK;
10716 		for_each_set_bit(scan_link_id, &scan_link_map, ATH12K_NUM_MAX_LINKS) {
10717 			scan_arvif = wiphy_dereference(hw->wiphy,
10718 						       ahvif->link[scan_link_id]);
10719 			if (scan_arvif && scan_arvif->ar == ar) {
10720 				ar->scan.arvif = NULL;
10721 				ath12k_mac_remove_link_interface(hw, scan_arvif);
10722 				ath12k_mac_unassign_link_vif(scan_arvif);
10723 				break;
10724 			}
10725 		}
10726 	}
10727 
10728 	if (arvif->ar) {
10729 		/* This is not expected really */
10730 		if (WARN_ON(!arvif->is_created)) {
10731 			arvif->ar = NULL;
10732 			return NULL;
10733 		}
10734 
10735 		if (ah->num_radio == 1)
10736 			return arvif->ar;
10737 
10738 		/* This can happen as scan vdev gets created during multiple scans
10739 		 * across different radios before a vdev is brought up in
10740 		 * a certain radio.
10741 		 */
10742 		if (ar != arvif->ar) {
10743 			if (WARN_ON(arvif->is_started))
10744 				return NULL;
10745 
10746 			ath12k_mac_remove_link_interface(hw, arvif);
10747 			ath12k_mac_unassign_link_vif(arvif);
10748 		}
10749 	}
10750 
10751 	ab = ar->ab;
10752 
10753 	/* Assign arvif again here since previous radio switch block
10754 	 * would've unassigned and cleared it.
10755 	 */
10756 	arvif = ath12k_mac_assign_link_vif(ah, vif, link_id);
10757 	if (vif->type == NL80211_IFTYPE_AP &&
10758 	    ar->num_peers > (ar->max_num_peers - 1)) {
10759 		ath12k_warn(ab, "failed to create vdev due to insufficient peer entry resource in firmware\n");
10760 		goto unlock;
10761 	}
10762 
10763 	if (arvif->is_created)
10764 		goto flush;
10765 
10766 	ret = ath12k_mac_vdev_create(ar, arvif);
10767 	if (ret) {
10768 		ath12k_warn(ab, "failed to create vdev %pM ret %d", vif->addr, ret);
10769 		goto unlock;
10770 	}
10771 
10772 flush:
10773 	/* If the vdev is created during channel assign and not during
10774 	 * add_interface(), Apply any parameters for the vdev which were received
10775 	 * after add_interface, corresponding to this vif.
10776 	 */
10777 	ath12k_mac_vif_cache_flush(ar, arvif);
10778 unlock:
10779 	return arvif->ar;
10780 }
10781 
10782 int ath12k_mac_op_add_interface(struct ieee80211_hw *hw,
10783 				struct ieee80211_vif *vif)
10784 {
10785 	struct ath12k_hw *ah = ath12k_hw_to_ah(hw);
10786 	struct ath12k_vif *ahvif = ath12k_vif_to_ahvif(vif);
10787 	struct ath12k_reg_info *reg_info;
10788 	struct ath12k_link_vif *arvif;
10789 	struct ath12k_base *ab;
10790 	struct ath12k *ar;
10791 	int i;
10792 
10793 	lockdep_assert_wiphy(hw->wiphy);
10794 
10795 	memset(ahvif, 0, sizeof(*ahvif));
10796 
10797 	ahvif->ah = ah;
10798 	ahvif->vif = vif;
10799 	arvif = &ahvif->deflink;
10800 
10801 	ath12k_mac_init_arvif(ahvif, arvif, -1);
10802 
10803 	/* Allocate Default Queue now and reassign during actual vdev create */
10804 	vif->cab_queue = ATH12K_HW_DEFAULT_QUEUE;
10805 	for (i = 0; i < ARRAY_SIZE(vif->hw_queue); i++)
10806 		vif->hw_queue[i] = ATH12K_HW_DEFAULT_QUEUE;
10807 
10808 	vif->driver_flags |= IEEE80211_VIF_SUPPORTS_UAPSD;
10809 
10810 	ath12k_mac_determine_vdev_type(vif, ahvif);
10811 
10812 	for_each_ar(ah, ar, i) {
10813 		if (!ath12k_wmi_supports_6ghz_cc_ext(ar))
10814 			continue;
10815 
10816 		ab = ar->ab;
10817 		reg_info = ab->reg_info[ar->pdev_idx];
10818 		ath12k_dbg(ab, ATH12K_DBG_MAC, "interface added to change reg rules\n");
10819 		ah->regd_updated = false;
10820 		ath12k_reg_handle_chan_list(ab, reg_info, ahvif->vdev_type,
10821 					    IEEE80211_REG_UNSET_AP);
10822 		break;
10823 	}
10824 
10825 	/* Defer vdev creation until assign_chanctx or hw_scan is initiated as driver
10826 	 * will not know if this interface is an ML vif at this point.
10827 	 */
10828 	return 0;
10829 }
10830 EXPORT_SYMBOL(ath12k_mac_op_add_interface);
10831 
10832 static void ath12k_mac_vif_unref(struct ath12k_dp *dp, struct ieee80211_vif *vif)
10833 {
10834 	struct ath12k_tx_desc_info *tx_desc_info;
10835 	struct ath12k_skb_cb *skb_cb;
10836 	struct sk_buff *skb;
10837 	int i;
10838 
10839 	for (i = 0; i < ATH12K_HW_MAX_QUEUES; i++) {
10840 		spin_lock_bh(&dp->tx_desc_lock[i]);
10841 
10842 		list_for_each_entry(tx_desc_info, &dp->tx_desc_used_list[i],
10843 				    list) {
10844 			skb = tx_desc_info->skb;
10845 			if (!skb)
10846 				continue;
10847 
10848 			skb_cb = ATH12K_SKB_CB(skb);
10849 			if (skb_cb->vif == vif)
10850 				skb_cb->vif = NULL;
10851 		}
10852 
10853 		spin_unlock_bh(&dp->tx_desc_lock[i]);
10854 	}
10855 }
10856 
10857 static int ath12k_mac_vdev_delete(struct ath12k *ar, struct ath12k_link_vif *arvif)
10858 {
10859 	struct ath12k_vif *ahvif = arvif->ahvif;
10860 	struct ieee80211_vif *vif = ath12k_ahvif_to_vif(ahvif);
10861 	struct ath12k_dp_link_vif *dp_link_vif;
10862 	struct ath12k_base *ab = ar->ab;
10863 	unsigned long time_left;
10864 	int ret;
10865 
10866 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
10867 
10868 	reinit_completion(&ar->vdev_delete_done);
10869 
10870 	ret = ath12k_wmi_vdev_delete(ar, arvif->vdev_id);
10871 	if (ret) {
10872 		ath12k_warn(ab, "failed to delete WMI vdev %d: %d\n",
10873 			    arvif->vdev_id, ret);
10874 		goto err_vdev_del;
10875 	}
10876 
10877 	time_left = wait_for_completion_timeout(&ar->vdev_delete_done,
10878 						ATH12K_VDEV_DELETE_TIMEOUT_HZ);
10879 	if (time_left == 0) {
10880 		ath12k_warn(ab, "Timeout in receiving vdev delete response\n");
10881 		goto err_vdev_del;
10882 	}
10883 
10884 	ab->free_vdev_map |= 1LL << arvif->vdev_id;
10885 	ar->allocated_vdev_map &= ~(1LL << arvif->vdev_id);
10886 	ar->num_created_vdevs--;
10887 
10888 	if (ahvif->vdev_type == WMI_VDEV_TYPE_MONITOR) {
10889 		ar->monitor_vdev_id = -1;
10890 		ar->monitor_vdev_created = false;
10891 	}
10892 
10893 	ath12k_dbg(ab, ATH12K_DBG_MAC, "vdev %pM deleted, vdev_id %d\n",
10894 		   vif->addr, arvif->vdev_id);
10895 
10896 err_vdev_del:
10897 	spin_lock_bh(&ar->data_lock);
10898 	list_del(&arvif->list);
10899 	spin_unlock_bh(&ar->data_lock);
10900 
10901 	ath12k_peer_cleanup(ar, arvif->vdev_id);
10902 	ath12k_ahvif_put_link_cache(ahvif, arvif->link_id);
10903 
10904 	idr_for_each(&ar->txmgmt_idr,
10905 		     ath12k_mac_vif_txmgmt_idr_remove, vif);
10906 
10907 	ath12k_mac_vif_unref(ath12k_ab_to_dp(ab), vif);
10908 
10909 	dp_link_vif = ath12k_dp_vif_to_dp_link_vif(&ahvif->dp_vif, arvif->link_id);
10910 	ath12k_dp_tx_put_bank_profile(ath12k_ab_to_dp(ab), dp_link_vif->bank_id);
10911 
10912 	/* Recalc txpower for remaining vdev */
10913 	ath12k_mac_txpower_recalc(ar);
10914 
10915 	/* TODO: recal traffic pause state based on the available vdevs */
10916 	arvif->is_created = false;
10917 	arvif->ar = NULL;
10918 
10919 	return ret;
10920 }
10921 
10922 void ath12k_mac_op_remove_interface(struct ieee80211_hw *hw,
10923 				    struct ieee80211_vif *vif)
10924 {
10925 	struct ath12k_vif *ahvif = ath12k_vif_to_ahvif(vif);
10926 	struct ath12k_link_vif *arvif;
10927 	struct ath12k *ar;
10928 	u8 link_id;
10929 
10930 	lockdep_assert_wiphy(hw->wiphy);
10931 
10932 	for (link_id = 0; link_id < ATH12K_NUM_MAX_LINKS; link_id++) {
10933 		/* if we cached some config but never received assign chanctx,
10934 		 * free the allocated cache.
10935 		 */
10936 		ath12k_ahvif_put_link_cache(ahvif, link_id);
10937 		arvif = wiphy_dereference(hw->wiphy, ahvif->link[link_id]);
10938 		if (!arvif || !arvif->is_created)
10939 			continue;
10940 
10941 		ar = arvif->ar;
10942 
10943 		/* Scan abortion is in progress since before this, cancel_hw_scan()
10944 		 * is expected to be executed. Since link is anyways going to be removed
10945 		 * now, just cancel the worker and send the scan aborted to user space
10946 		 */
10947 		if (ar->scan.arvif == arvif) {
10948 			wiphy_work_cancel(hw->wiphy, &ar->scan.vdev_clean_wk);
10949 
10950 			spin_lock_bh(&ar->data_lock);
10951 			ar->scan.arvif = NULL;
10952 			if (!ar->scan.is_roc) {
10953 				struct cfg80211_scan_info info = {
10954 					.aborted = true,
10955 				};
10956 
10957 				ath12k_mac_scan_send_complete(ar, &info);
10958 			}
10959 
10960 			ar->scan.state = ATH12K_SCAN_IDLE;
10961 			ar->scan_channel = NULL;
10962 			ar->scan.roc_freq = 0;
10963 			spin_unlock_bh(&ar->data_lock);
10964 		}
10965 
10966 		ath12k_mac_remove_link_interface(hw, arvif);
10967 		ath12k_mac_unassign_link_vif(arvif);
10968 	}
10969 }
10970 EXPORT_SYMBOL(ath12k_mac_op_remove_interface);
10971 
10972 /* FIXME: Has to be verified. */
10973 #define SUPPORTED_FILTERS			\
10974 	(FIF_ALLMULTI |				\
10975 	FIF_CONTROL |				\
10976 	FIF_PSPOLL |				\
10977 	FIF_OTHER_BSS |				\
10978 	FIF_BCN_PRBRESP_PROMISC |		\
10979 	FIF_PROBE_REQ |				\
10980 	FIF_FCSFAIL)
10981 
10982 void ath12k_mac_op_configure_filter(struct ieee80211_hw *hw,
10983 				    unsigned int changed_flags,
10984 				    unsigned int *total_flags,
10985 				    u64 multicast)
10986 {
10987 	struct ath12k_hw *ah = ath12k_hw_to_ah(hw);
10988 	struct ath12k *ar;
10989 
10990 	lockdep_assert_wiphy(hw->wiphy);
10991 
10992 	ar = ath12k_ah_to_ar(ah, 0);
10993 
10994 	*total_flags &= SUPPORTED_FILTERS;
10995 	ar->filter_flags = *total_flags;
10996 }
10997 EXPORT_SYMBOL(ath12k_mac_op_configure_filter);
10998 
10999 int ath12k_mac_op_get_antenna(struct ieee80211_hw *hw, int radio_idx,
11000 			      u32 *tx_ant, u32 *rx_ant)
11001 {
11002 	struct ath12k_hw *ah = ath12k_hw_to_ah(hw);
11003 	int antennas_rx = 0, antennas_tx = 0;
11004 	struct ath12k *ar;
11005 	int i;
11006 
11007 	lockdep_assert_wiphy(hw->wiphy);
11008 
11009 	for_each_ar(ah, ar, i) {
11010 		antennas_rx = max_t(u32, antennas_rx, ar->cfg_rx_chainmask);
11011 		antennas_tx = max_t(u32, antennas_tx, ar->cfg_tx_chainmask);
11012 	}
11013 
11014 	*tx_ant = antennas_tx;
11015 	*rx_ant = antennas_rx;
11016 
11017 	return 0;
11018 }
11019 EXPORT_SYMBOL(ath12k_mac_op_get_antenna);
11020 
11021 int ath12k_mac_op_set_antenna(struct ieee80211_hw *hw, int radio_idx,
11022 			      u32 tx_ant, u32 rx_ant)
11023 {
11024 	struct ath12k_hw *ah = ath12k_hw_to_ah(hw);
11025 	struct ath12k *ar;
11026 	int ret = 0;
11027 	int i;
11028 
11029 	lockdep_assert_wiphy(hw->wiphy);
11030 
11031 	for_each_ar(ah, ar, i) {
11032 		ret = __ath12k_set_antenna(ar, tx_ant, rx_ant);
11033 		if (ret)
11034 			break;
11035 	}
11036 
11037 	return ret;
11038 }
11039 EXPORT_SYMBOL(ath12k_mac_op_set_antenna);
11040 
11041 static int ath12k_mac_ampdu_action(struct ieee80211_hw *hw,
11042 				   struct ieee80211_vif *vif,
11043 				   struct ieee80211_ampdu_params *params,
11044 				   u8 link_id)
11045 {
11046 	struct ath12k *ar;
11047 	int ret = -EINVAL;
11048 
11049 	lockdep_assert_wiphy(hw->wiphy);
11050 
11051 	ar = ath12k_get_ar_by_vif(hw, vif, link_id);
11052 	if (!ar)
11053 		return -EINVAL;
11054 
11055 	switch (params->action) {
11056 	case IEEE80211_AMPDU_RX_START:
11057 		ret = ath12k_dp_rx_ampdu_start(ar, params, link_id);
11058 		break;
11059 	case IEEE80211_AMPDU_RX_STOP:
11060 		ret = ath12k_dp_rx_ampdu_stop(ar, params, link_id);
11061 		break;
11062 	case IEEE80211_AMPDU_TX_START:
11063 	case IEEE80211_AMPDU_TX_STOP_CONT:
11064 	case IEEE80211_AMPDU_TX_STOP_FLUSH:
11065 	case IEEE80211_AMPDU_TX_STOP_FLUSH_CONT:
11066 	case IEEE80211_AMPDU_TX_OPERATIONAL:
11067 		/* Tx A-MPDU aggregation offloaded to hw/fw so deny mac80211
11068 		 * Tx aggregation requests.
11069 		 */
11070 		ret = -EOPNOTSUPP;
11071 		break;
11072 	}
11073 
11074 	if (ret)
11075 		ath12k_warn(ar->ab, "unable to perform ampdu action %d for vif %pM link %u ret %d\n",
11076 			    params->action, vif->addr, link_id, ret);
11077 
11078 	return ret;
11079 }
11080 
11081 int ath12k_mac_op_ampdu_action(struct ieee80211_hw *hw,
11082 			       struct ieee80211_vif *vif,
11083 			       struct ieee80211_ampdu_params *params)
11084 {
11085 	struct ieee80211_sta *sta = params->sta;
11086 	struct ath12k_sta *ahsta = ath12k_sta_to_ahsta(sta);
11087 	unsigned long links_map = ahsta->links_map;
11088 	int ret = -EINVAL;
11089 	u8 link_id;
11090 
11091 	lockdep_assert_wiphy(hw->wiphy);
11092 
11093 	if (WARN_ON(!links_map))
11094 		return ret;
11095 
11096 	for_each_set_bit(link_id, &links_map, IEEE80211_MLD_MAX_NUM_LINKS) {
11097 		ret = ath12k_mac_ampdu_action(hw, vif, params, link_id);
11098 		if (ret)
11099 			return ret;
11100 	}
11101 
11102 	return 0;
11103 }
11104 EXPORT_SYMBOL(ath12k_mac_op_ampdu_action);
11105 
11106 int ath12k_mac_op_add_chanctx(struct ieee80211_hw *hw,
11107 			      struct ieee80211_chanctx_conf *ctx)
11108 {
11109 	struct ath12k *ar;
11110 	struct ath12k_base *ab;
11111 
11112 	lockdep_assert_wiphy(hw->wiphy);
11113 
11114 	ar = ath12k_get_ar_by_ctx(hw, ctx);
11115 	if (!ar)
11116 		return -EINVAL;
11117 
11118 	ab = ar->ab;
11119 
11120 	ath12k_dbg(ab, ATH12K_DBG_MAC,
11121 		   "mac chanctx add freq %u width %d ptr %p\n",
11122 		   ctx->def.chan->center_freq, ctx->def.width, ctx);
11123 
11124 	spin_lock_bh(&ar->data_lock);
11125 	/* TODO: In case of multiple channel context, populate rx_channel from
11126 	 * Rx PPDU desc information.
11127 	 */
11128 	ar->rx_channel = ctx->def.chan;
11129 	spin_unlock_bh(&ar->data_lock);
11130 	ar->chan_tx_pwr = ATH12K_PDEV_TX_POWER_INVALID;
11131 
11132 	return 0;
11133 }
11134 EXPORT_SYMBOL(ath12k_mac_op_add_chanctx);
11135 
11136 void ath12k_mac_op_remove_chanctx(struct ieee80211_hw *hw,
11137 				  struct ieee80211_chanctx_conf *ctx)
11138 {
11139 	struct ath12k *ar;
11140 	struct ath12k_base *ab;
11141 
11142 	lockdep_assert_wiphy(hw->wiphy);
11143 
11144 	ar = ath12k_get_ar_by_ctx(hw, ctx);
11145 	if (!ar)
11146 		return;
11147 
11148 	ab = ar->ab;
11149 
11150 	ath12k_dbg(ab, ATH12K_DBG_MAC,
11151 		   "mac chanctx remove freq %u width %d ptr %p\n",
11152 		   ctx->def.chan->center_freq, ctx->def.width, ctx);
11153 
11154 	spin_lock_bh(&ar->data_lock);
11155 	/* TODO: In case of there is one more channel context left, populate
11156 	 * rx_channel with the channel of that remaining channel context.
11157 	 */
11158 	ar->rx_channel = NULL;
11159 	spin_unlock_bh(&ar->data_lock);
11160 	ar->chan_tx_pwr = ATH12K_PDEV_TX_POWER_INVALID;
11161 }
11162 EXPORT_SYMBOL(ath12k_mac_op_remove_chanctx);
11163 
11164 static enum wmi_phy_mode
11165 ath12k_mac_check_down_grade_phy_mode(struct ath12k *ar,
11166 				     enum wmi_phy_mode mode,
11167 				     enum nl80211_band band,
11168 				     enum nl80211_iftype type)
11169 {
11170 	struct ieee80211_sta_eht_cap *eht_cap = NULL;
11171 	enum wmi_phy_mode down_mode;
11172 	int n = ar->mac.sbands[band].n_iftype_data;
11173 	int i;
11174 	struct ieee80211_sband_iftype_data *data;
11175 
11176 	if (mode < MODE_11BE_EHT20)
11177 		return mode;
11178 
11179 	data = ar->mac.iftype[band];
11180 	for (i = 0; i < n; i++) {
11181 		if (data[i].types_mask & BIT(type)) {
11182 			eht_cap = &data[i].eht_cap;
11183 			break;
11184 		}
11185 	}
11186 
11187 	if (eht_cap && eht_cap->has_eht)
11188 		return mode;
11189 
11190 	switch (mode) {
11191 	case MODE_11BE_EHT20:
11192 		down_mode = MODE_11AX_HE20;
11193 		break;
11194 	case MODE_11BE_EHT40:
11195 		down_mode = MODE_11AX_HE40;
11196 		break;
11197 	case MODE_11BE_EHT80:
11198 		down_mode = MODE_11AX_HE80;
11199 		break;
11200 	case MODE_11BE_EHT80_80:
11201 		down_mode = MODE_11AX_HE80_80;
11202 		break;
11203 	case MODE_11BE_EHT160:
11204 	case MODE_11BE_EHT160_160:
11205 	case MODE_11BE_EHT320:
11206 		down_mode = MODE_11AX_HE160;
11207 		break;
11208 	case MODE_11BE_EHT20_2G:
11209 		down_mode = MODE_11AX_HE20_2G;
11210 		break;
11211 	case MODE_11BE_EHT40_2G:
11212 		down_mode = MODE_11AX_HE40_2G;
11213 		break;
11214 	default:
11215 		down_mode = mode;
11216 		break;
11217 	}
11218 
11219 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
11220 		   "mac vdev start phymode %s downgrade to %s\n",
11221 		   ath12k_mac_phymode_str(mode),
11222 		   ath12k_mac_phymode_str(down_mode));
11223 
11224 	return down_mode;
11225 }
11226 
11227 static void
11228 ath12k_mac_mlo_get_vdev_args(struct ath12k_link_vif *arvif,
11229 			     struct wmi_ml_arg *ml_arg)
11230 {
11231 	struct ath12k_vif *ahvif = arvif->ahvif;
11232 	struct wmi_ml_partner_info *partner_info;
11233 	struct ieee80211_bss_conf *link_conf;
11234 	struct ath12k_link_vif *arvif_p;
11235 	unsigned long links;
11236 	u8 link_id;
11237 
11238 	lockdep_assert_wiphy(ahvif->ah->hw->wiphy);
11239 
11240 	if (!ath12k_mac_is_ml_arvif(arvif))
11241 		return;
11242 
11243 	if (hweight16(ahvif->vif->valid_links) > ATH12K_WMI_MLO_MAX_LINKS)
11244 		return;
11245 
11246 	ml_arg->enabled = true;
11247 
11248 	/* Driver always add a new link via VDEV START, FW takes
11249 	 * care of internally adding this link to existing
11250 	 * link vdevs which are advertised as partners below
11251 	 */
11252 	ml_arg->link_add = true;
11253 
11254 	ml_arg->assoc_link = arvif->is_sta_assoc_link;
11255 
11256 	partner_info = ml_arg->partner_info;
11257 
11258 	links = ahvif->links_map;
11259 	for_each_set_bit(link_id, &links, IEEE80211_MLD_MAX_NUM_LINKS) {
11260 		arvif_p = wiphy_dereference(ahvif->ah->hw->wiphy, ahvif->link[link_id]);
11261 
11262 		if (WARN_ON(!arvif_p))
11263 			continue;
11264 
11265 		if (arvif == arvif_p)
11266 			continue;
11267 
11268 		if (!arvif_p->is_started)
11269 			continue;
11270 
11271 		link_conf = wiphy_dereference(ahvif->ah->hw->wiphy,
11272 					      ahvif->vif->link_conf[arvif_p->link_id]);
11273 
11274 		if (!link_conf)
11275 			continue;
11276 
11277 		partner_info->vdev_id = arvif_p->vdev_id;
11278 		partner_info->hw_link_id = arvif_p->ar->pdev->hw_link_id;
11279 		ether_addr_copy(partner_info->addr, link_conf->addr);
11280 		ml_arg->num_partner_links++;
11281 		partner_info++;
11282 	}
11283 }
11284 
11285 static int
11286 ath12k_mac_vdev_start_restart(struct ath12k_link_vif *arvif,
11287 			      struct ieee80211_chanctx_conf *ctx,
11288 			      bool restart)
11289 {
11290 	struct ath12k *ar = arvif->ar;
11291 	struct ath12k_base *ab = ar->ab;
11292 	struct wmi_vdev_start_req_arg arg = {};
11293 	const struct cfg80211_chan_def *chandef = &ctx->def;
11294 	struct ieee80211_hw *hw = ath12k_ar_to_hw(ar);
11295 	struct ath12k_vif *ahvif = arvif->ahvif;
11296 	struct ieee80211_bss_conf *link_conf;
11297 	unsigned int dfs_cac_time;
11298 	int ret;
11299 
11300 	lockdep_assert_wiphy(hw->wiphy);
11301 
11302 	link_conf = ath12k_mac_get_link_bss_conf(arvif);
11303 	if (!link_conf) {
11304 		ath12k_warn(ar->ab, "unable to access bss link conf in vdev start for vif %pM link %u\n",
11305 			    ahvif->vif->addr, arvif->link_id);
11306 		return -ENOLINK;
11307 	}
11308 
11309 	reinit_completion(&ar->vdev_setup_done);
11310 
11311 	arg.vdev_id = arvif->vdev_id;
11312 	arg.dtim_period = arvif->dtim_period;
11313 	arg.bcn_intval = arvif->beacon_interval;
11314 	arg.punct_bitmap = ~arvif->punct_bitmap;
11315 
11316 	arg.freq = chandef->chan->center_freq;
11317 	arg.band_center_freq1 = chandef->center_freq1;
11318 	arg.band_center_freq2 = chandef->center_freq2;
11319 	arg.mode = ath12k_phymodes[chandef->chan->band][chandef->width];
11320 
11321 	arg.mode = ath12k_mac_check_down_grade_phy_mode(ar, arg.mode,
11322 							chandef->chan->band,
11323 							ahvif->vif->type);
11324 	arg.min_power = 0;
11325 	arg.max_power = chandef->chan->max_power;
11326 	arg.max_reg_power = chandef->chan->max_reg_power;
11327 	arg.max_antenna_gain = chandef->chan->max_antenna_gain;
11328 
11329 	arg.pref_tx_streams = ar->num_tx_chains;
11330 	arg.pref_rx_streams = ar->num_rx_chains;
11331 
11332 	arg.mbssid_flags = WMI_VDEV_MBSSID_FLAGS_NON_MBSSID_AP;
11333 	arg.mbssid_tx_vdev_id = 0;
11334 	if (test_bit(WMI_TLV_SERVICE_MBSS_PARAM_IN_VDEV_START_SUPPORT,
11335 		     ar->ab->wmi_ab.svc_map)) {
11336 		ret = ath12k_mac_setup_vdev_params_mbssid(arvif,
11337 							  &arg.mbssid_flags,
11338 							  &arg.mbssid_tx_vdev_id);
11339 		if (ret)
11340 			return ret;
11341 	}
11342 
11343 	if (ahvif->vdev_type == WMI_VDEV_TYPE_AP) {
11344 		arg.ssid = ahvif->u.ap.ssid;
11345 		arg.ssid_len = ahvif->u.ap.ssid_len;
11346 		arg.hidden_ssid = ahvif->u.ap.hidden_ssid;
11347 
11348 		/* For now allow DFS for AP mode */
11349 		arg.chan_radar = !!(chandef->chan->flags & IEEE80211_CHAN_RADAR);
11350 
11351 		arg.freq2_radar = ctx->radar_enabled;
11352 
11353 		arg.passive = arg.chan_radar;
11354 
11355 		spin_lock_bh(&ab->base_lock);
11356 		arg.regdomain = ar->ab->dfs_region;
11357 		spin_unlock_bh(&ab->base_lock);
11358 
11359 		/* TODO: Notify if secondary 80Mhz also needs radar detection */
11360 	}
11361 
11362 	arg.passive |= !!(chandef->chan->flags & IEEE80211_CHAN_NO_IR);
11363 
11364 	if (!restart)
11365 		ath12k_mac_mlo_get_vdev_args(arvif, &arg.ml);
11366 
11367 	ath12k_dbg(ab, ATH12K_DBG_MAC,
11368 		   "mac vdev %d start center_freq %d phymode %s punct_bitmap 0x%x\n",
11369 		   arg.vdev_id, arg.freq,
11370 		   ath12k_mac_phymode_str(arg.mode), arg.punct_bitmap);
11371 
11372 	ret = ath12k_wmi_vdev_start(ar, &arg, restart);
11373 	if (ret) {
11374 		ath12k_warn(ar->ab, "failed to %s WMI vdev %i\n",
11375 			    restart ? "restart" : "start", arg.vdev_id);
11376 		return ret;
11377 	}
11378 
11379 	ret = ath12k_mac_vdev_setup_sync(ar);
11380 	if (ret) {
11381 		ath12k_warn(ab, "failed to synchronize setup for vdev %i %s: %d\n",
11382 			    arg.vdev_id, restart ? "restart" : "start", ret);
11383 		return ret;
11384 	}
11385 
11386 	/* TODO: For now we only set TPC power here. However when
11387 	 * channel changes, say CSA, it should be updated again.
11388 	 */
11389 	if (ath12k_mac_supports_tpc(ar, ahvif, chandef)) {
11390 		ath12k_mac_fill_reg_tpc_info(ar, arvif, ctx);
11391 		ath12k_wmi_send_vdev_set_tpc_power(ar, arvif->vdev_id,
11392 						   &arvif->reg_tpc_info);
11393 	}
11394 
11395 	ar->num_started_vdevs++;
11396 	ath12k_dbg(ab, ATH12K_DBG_MAC,  "vdev %pM started, vdev_id %d\n",
11397 		   ahvif->vif->addr, arvif->vdev_id);
11398 
11399 	/* Enable CAC Running Flag in the driver by checking all sub-channel's DFS
11400 	 * state as NL80211_DFS_USABLE which indicates CAC needs to be
11401 	 * done before channel usage. This flag is used to drop rx packets.
11402 	 * during CAC.
11403 	 */
11404 	/* TODO: Set the flag for other interface types as required */
11405 	if (arvif->ahvif->vdev_type == WMI_VDEV_TYPE_AP && ctx->radar_enabled &&
11406 	    cfg80211_chandef_dfs_usable(hw->wiphy, chandef)) {
11407 		set_bit(ATH12K_FLAG_CAC_RUNNING, &ar->dev_flags);
11408 		dfs_cac_time = cfg80211_chandef_dfs_cac_time(hw->wiphy, chandef);
11409 
11410 		ath12k_dbg(ab, ATH12K_DBG_MAC,
11411 			   "CAC started dfs_cac_time %u center_freq %d center_freq1 %d for vdev %d\n",
11412 			   dfs_cac_time, arg.freq, arg.band_center_freq1, arg.vdev_id);
11413 	}
11414 
11415 	ret = ath12k_mac_set_txbf_conf(arvif);
11416 	if (ret)
11417 		ath12k_warn(ab, "failed to set txbf conf for vdev %d: %d\n",
11418 			    arvif->vdev_id, ret);
11419 
11420 	return 0;
11421 }
11422 
11423 static int ath12k_mac_vdev_start(struct ath12k_link_vif *arvif,
11424 				 struct ieee80211_chanctx_conf *ctx)
11425 {
11426 	return ath12k_mac_vdev_start_restart(arvif, ctx, false);
11427 }
11428 
11429 static int ath12k_mac_vdev_restart(struct ath12k_link_vif *arvif,
11430 				   struct ieee80211_chanctx_conf *ctx)
11431 {
11432 	return ath12k_mac_vdev_start_restart(arvif, ctx, true);
11433 }
11434 
11435 struct ath12k_mac_change_chanctx_arg {
11436 	struct ieee80211_chanctx_conf *ctx;
11437 	struct ieee80211_vif_chanctx_switch *vifs;
11438 	int n_vifs;
11439 	int next_vif;
11440 	struct ath12k *ar;
11441 };
11442 
11443 static void
11444 ath12k_mac_change_chanctx_cnt_iter(void *data, u8 *mac,
11445 				   struct ieee80211_vif *vif)
11446 {
11447 	struct ath12k_vif *ahvif = ath12k_vif_to_ahvif(vif);
11448 	struct ath12k_mac_change_chanctx_arg *arg = data;
11449 	struct ieee80211_bss_conf *link_conf;
11450 	struct ath12k_link_vif *arvif;
11451 	unsigned long links_map;
11452 	u8 link_id;
11453 
11454 	lockdep_assert_wiphy(ahvif->ah->hw->wiphy);
11455 
11456 	links_map = ahvif->links_map;
11457 	for_each_set_bit(link_id, &links_map, IEEE80211_MLD_MAX_NUM_LINKS) {
11458 		arvif = wiphy_dereference(ahvif->ah->hw->wiphy, ahvif->link[link_id]);
11459 		if (WARN_ON(!arvif))
11460 			continue;
11461 
11462 		if (!arvif->is_created || arvif->ar != arg->ar)
11463 			continue;
11464 
11465 		link_conf = wiphy_dereference(ahvif->ah->hw->wiphy,
11466 					      vif->link_conf[link_id]);
11467 		if (WARN_ON(!link_conf))
11468 			continue;
11469 
11470 		if (rcu_access_pointer(link_conf->chanctx_conf) != arg->ctx)
11471 			continue;
11472 
11473 		arg->n_vifs++;
11474 	}
11475 }
11476 
11477 static void
11478 ath12k_mac_change_chanctx_fill_iter(void *data, u8 *mac,
11479 				    struct ieee80211_vif *vif)
11480 {
11481 	struct ath12k_vif *ahvif = ath12k_vif_to_ahvif(vif);
11482 	struct ath12k_mac_change_chanctx_arg *arg = data;
11483 	struct ieee80211_bss_conf *link_conf;
11484 	struct ieee80211_chanctx_conf *ctx;
11485 	struct ath12k_link_vif *arvif;
11486 	unsigned long links_map;
11487 	u8 link_id;
11488 
11489 	lockdep_assert_wiphy(ahvif->ah->hw->wiphy);
11490 
11491 	links_map = ahvif->links_map;
11492 	for_each_set_bit(link_id, &links_map, IEEE80211_MLD_MAX_NUM_LINKS) {
11493 		arvif = wiphy_dereference(ahvif->ah->hw->wiphy, ahvif->link[link_id]);
11494 		if (WARN_ON(!arvif))
11495 			continue;
11496 
11497 		if (!arvif->is_created || arvif->ar != arg->ar)
11498 			continue;
11499 
11500 		link_conf = wiphy_dereference(ahvif->ah->hw->wiphy,
11501 					      vif->link_conf[arvif->link_id]);
11502 		if (WARN_ON(!link_conf))
11503 			continue;
11504 
11505 		ctx = rcu_access_pointer(link_conf->chanctx_conf);
11506 		if (ctx != arg->ctx)
11507 			continue;
11508 
11509 		if (WARN_ON(arg->next_vif == arg->n_vifs))
11510 			return;
11511 
11512 		arg->vifs[arg->next_vif].vif = vif;
11513 		arg->vifs[arg->next_vif].old_ctx = ctx;
11514 		arg->vifs[arg->next_vif].new_ctx = ctx;
11515 		arg->vifs[arg->next_vif].link_conf = link_conf;
11516 		arg->next_vif++;
11517 	}
11518 }
11519 
11520 static u32 ath12k_mac_nlwidth_to_wmiwidth(enum nl80211_chan_width width)
11521 {
11522 	switch (width) {
11523 	case NL80211_CHAN_WIDTH_20:
11524 		return WMI_CHAN_WIDTH_20;
11525 	case NL80211_CHAN_WIDTH_40:
11526 		return WMI_CHAN_WIDTH_40;
11527 	case NL80211_CHAN_WIDTH_80:
11528 		return WMI_CHAN_WIDTH_80;
11529 	case NL80211_CHAN_WIDTH_160:
11530 		return WMI_CHAN_WIDTH_160;
11531 	case NL80211_CHAN_WIDTH_80P80:
11532 		return WMI_CHAN_WIDTH_80P80;
11533 	case NL80211_CHAN_WIDTH_5:
11534 		return WMI_CHAN_WIDTH_5;
11535 	case NL80211_CHAN_WIDTH_10:
11536 		return WMI_CHAN_WIDTH_10;
11537 	case NL80211_CHAN_WIDTH_320:
11538 		return WMI_CHAN_WIDTH_320;
11539 	default:
11540 		WARN_ON(1);
11541 		return WMI_CHAN_WIDTH_20;
11542 	}
11543 }
11544 
11545 static int ath12k_mac_update_peer_puncturing_width(struct ath12k *ar,
11546 						   struct ath12k_link_vif *arvif,
11547 						   struct cfg80211_chan_def def)
11548 {
11549 	u32 param_id, param_value;
11550 	int ret;
11551 
11552 	if (arvif->ahvif->vdev_type != WMI_VDEV_TYPE_STA)
11553 		return 0;
11554 
11555 	param_id = WMI_PEER_CHWIDTH_PUNCTURE_20MHZ_BITMAP;
11556 	param_value = ath12k_mac_nlwidth_to_wmiwidth(def.width) |
11557 		u32_encode_bits((~def.punctured),
11558 				WMI_PEER_PUNCTURE_BITMAP);
11559 
11560 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
11561 		   "punctured bitmap %02x width %d vdev %d\n",
11562 		   def.punctured, def.width, arvif->vdev_id);
11563 
11564 	ret = ath12k_wmi_set_peer_param(ar, arvif->bssid,
11565 					arvif->vdev_id, param_id,
11566 					param_value);
11567 
11568 	return ret;
11569 }
11570 
11571 static void
11572 ath12k_mac_update_vif_chan(struct ath12k *ar,
11573 			   struct ieee80211_vif_chanctx_switch *vifs,
11574 			   int n_vifs)
11575 {
11576 	struct ath12k_incumbent_signal_interference *incumbent;
11577 	struct ath12k_wmi_vdev_up_params params = {};
11578 	struct ieee80211_bss_conf *link_conf;
11579 	struct cfg80211_chan_def *chandef;
11580 	struct ath12k_base *ab = ar->ab;
11581 	struct ath12k_link_vif *arvif;
11582 	struct ieee80211_vif *vif;
11583 	struct ath12k_vif *ahvif;
11584 	u8 link_id;
11585 	int ret;
11586 	int i;
11587 	bool monitor_vif = false;
11588 
11589 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
11590 
11591 	for (i = 0; i < n_vifs; i++) {
11592 		vif = vifs[i].vif;
11593 		ahvif = ath12k_vif_to_ahvif(vif);
11594 		link_conf = vifs[i].link_conf;
11595 		link_id = link_conf->link_id;
11596 		arvif = wiphy_dereference(ath12k_ar_to_hw(ar)->wiphy,
11597 					  ahvif->link[link_id]);
11598 
11599 		if (vif->type == NL80211_IFTYPE_MONITOR) {
11600 			monitor_vif = true;
11601 			continue;
11602 		}
11603 
11604 		if (WARN_ON(!arvif))
11605 			continue;
11606 
11607 		ath12k_dbg(ab, ATH12K_DBG_MAC,
11608 			   "mac chanctx switch vdev_id %i freq %u->%u width %d->%d\n",
11609 			   arvif->vdev_id,
11610 			   vifs[i].old_ctx->def.chan->center_freq,
11611 			   vifs[i].new_ctx->def.chan->center_freq,
11612 			   vifs[i].old_ctx->def.width,
11613 			   vifs[i].new_ctx->def.width);
11614 
11615 		if (WARN_ON(!arvif->is_started))
11616 			continue;
11617 
11618 		arvif->punct_bitmap = vifs[i].new_ctx->def.punctured;
11619 
11620 		/* Firmware expect vdev_restart only if vdev is up.
11621 		 * If vdev is down then it expect vdev_stop->vdev_start.
11622 		 */
11623 		if (arvif->is_up) {
11624 			ret = ath12k_mac_vdev_restart(arvif, vifs[i].new_ctx);
11625 			if (ret) {
11626 				ath12k_warn(ab, "failed to restart vdev %d: %d\n",
11627 					    arvif->vdev_id, ret);
11628 				continue;
11629 			}
11630 		} else {
11631 			ret = ath12k_mac_vdev_stop(arvif);
11632 			if (ret) {
11633 				ath12k_warn(ab, "failed to stop vdev %d: %d\n",
11634 					    arvif->vdev_id, ret);
11635 				continue;
11636 			}
11637 
11638 			ret = ath12k_mac_vdev_start(arvif, vifs[i].new_ctx);
11639 			if (ret)
11640 				ath12k_warn(ab, "failed to start vdev %d: %d\n",
11641 					    arvif->vdev_id, ret);
11642 			continue;
11643 		}
11644 
11645 		ret = ath12k_mac_update_peer_puncturing_width(arvif->ar, arvif,
11646 							      vifs[i].new_ctx->def);
11647 		if (ret) {
11648 			ath12k_warn(ar->ab,
11649 				    "failed to update puncturing bitmap %02x and width %d: %d\n",
11650 				    vifs[i].new_ctx->def.punctured,
11651 				    vifs[i].new_ctx->def.width, ret);
11652 			continue;
11653 		}
11654 
11655 		/* Defer VDEV bring-up during CSA to avoid installing stale
11656 		 * beacon templates. The beacon content is updated only
11657 		 * after CSA finalize, so we mark CSA in progress and skip
11658 		 * VDEV_UP for now. It will be handled later in
11659 		 * bss_info_changed().
11660 		 */
11661 		if (link_conf->csa_active &&
11662 		    arvif->ahvif->vdev_type == WMI_VDEV_TYPE_AP) {
11663 			arvif->is_csa_in_progress = true;
11664 			continue;
11665 		}
11666 
11667 		ret = ath12k_mac_setup_bcn_tmpl(arvif);
11668 		if (ret)
11669 			ath12k_warn(ab, "failed to update bcn tmpl during csa: %d\n",
11670 				    ret);
11671 
11672 		memset(&params, 0, sizeof(params));
11673 		params.vdev_id = arvif->vdev_id;
11674 		params.aid = ahvif->aid;
11675 		params.bssid = arvif->bssid;
11676 		params.tx_bssid = ath12k_mac_get_tx_bssid(arvif);
11677 		if (params.tx_bssid) {
11678 			params.nontx_profile_idx = link_conf->bssid_index;
11679 			params.nontx_profile_cnt = 1 << link_conf->bssid_indicator;
11680 		}
11681 		ret = ath12k_wmi_vdev_up(arvif->ar, &params);
11682 		if (ret) {
11683 			ath12k_warn(ab, "failed to bring vdev up %d: %d\n",
11684 				    arvif->vdev_id, ret);
11685 			continue;
11686 		}
11687 	}
11688 
11689 	/* Restart the internal monitor vdev on new channel */
11690 	if (!monitor_vif && ar->monitor_vdev_created) {
11691 		if (!ath12k_mac_monitor_stop(ar))
11692 			ath12k_mac_monitor_start(ar);
11693 	}
11694 
11695 	incumbent = &ar->incumbent_signal_interference;
11696 	spin_lock_bh(&ar->data_lock);
11697 	if (incumbent->handling_in_progress) {
11698 		chandef = &vifs[0].new_ctx->def;
11699 		if (incumbent->chan_bw_interference_bitmap &
11700 		    ATH12K_WMI_DCS_SEG_PRI20) {
11701 			if (incumbent->center_freq !=
11702 			    chandef->chan->center_freq) {
11703 				incumbent->chan_bw_interference_bitmap = 0;
11704 				incumbent->handling_in_progress = false;
11705 				ath12k_dbg(ab, ATH12K_DBG_MAC,
11706 					   "incumbent signal interference chan switch completed\n");
11707 			} else {
11708 				ath12k_warn(ab,
11709 					    "incumbent signal interference chan switch not done, freq %u\n",
11710 					    incumbent->center_freq);
11711 			}
11712 		} else {
11713 			if (incumbent->center_freq !=
11714 			    chandef->chan->center_freq ||
11715 			    incumbent->width != chandef->width) {
11716 				incumbent->chan_bw_interference_bitmap = 0;
11717 				incumbent->handling_in_progress = false;
11718 				ath12k_dbg(ab, ATH12K_DBG_MAC,
11719 					   "Bandwidth/channel change due to incumbent signal interference completed\n");
11720 			} else {
11721 				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",
11722 					    incumbent->center_freq,
11723 					    chandef->chan->center_freq,
11724 					    incumbent->width,
11725 					    chandef->width);
11726 			}
11727 		}
11728 	}
11729 	spin_unlock_bh(&ar->data_lock);
11730 }
11731 
11732 static void
11733 ath12k_mac_update_active_vif_chan(struct ath12k *ar,
11734 				  struct ieee80211_chanctx_conf *ctx)
11735 {
11736 	struct ath12k_mac_change_chanctx_arg arg = { .ctx = ctx, .ar = ar };
11737 	struct ieee80211_hw *hw = ath12k_ar_to_hw(ar);
11738 
11739 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
11740 
11741 	ieee80211_iterate_active_interfaces_atomic(hw,
11742 						   IEEE80211_IFACE_ITER_NORMAL,
11743 						   ath12k_mac_change_chanctx_cnt_iter,
11744 						   &arg);
11745 	if (arg.n_vifs == 0)
11746 		return;
11747 
11748 	arg.vifs = kzalloc_objs(arg.vifs[0], arg.n_vifs);
11749 	if (!arg.vifs)
11750 		return;
11751 
11752 	ieee80211_iterate_active_interfaces_atomic(hw,
11753 						   IEEE80211_IFACE_ITER_NORMAL,
11754 						   ath12k_mac_change_chanctx_fill_iter,
11755 						   &arg);
11756 
11757 	ath12k_mac_update_vif_chan(ar, arg.vifs, arg.n_vifs);
11758 
11759 	kfree(arg.vifs);
11760 }
11761 
11762 void ath12k_mac_op_change_chanctx(struct ieee80211_hw *hw,
11763 				  struct ieee80211_chanctx_conf *ctx,
11764 				  u32 changed)
11765 {
11766 	struct ath12k *ar;
11767 	struct ath12k_base *ab;
11768 
11769 	lockdep_assert_wiphy(hw->wiphy);
11770 
11771 	ar = ath12k_get_ar_by_ctx(hw, ctx);
11772 	if (!ar)
11773 		return;
11774 
11775 	ab = ar->ab;
11776 
11777 	ath12k_dbg(ab, ATH12K_DBG_MAC,
11778 		   "mac chanctx change freq %u width %d ptr %p changed %x\n",
11779 		   ctx->def.chan->center_freq, ctx->def.width, ctx, changed);
11780 
11781 	/* This shouldn't really happen because channel switching should use
11782 	 * switch_vif_chanctx().
11783 	 */
11784 	if (WARN_ON(changed & IEEE80211_CHANCTX_CHANGE_CHANNEL))
11785 		return;
11786 
11787 	if (changed & IEEE80211_CHANCTX_CHANGE_WIDTH ||
11788 	    changed & IEEE80211_CHANCTX_CHANGE_RADAR ||
11789 	    changed & IEEE80211_CHANCTX_CHANGE_PUNCTURING)
11790 		ath12k_mac_update_active_vif_chan(ar, ctx);
11791 
11792 	/* TODO: Recalc radar detection */
11793 }
11794 EXPORT_SYMBOL(ath12k_mac_op_change_chanctx);
11795 
11796 static int ath12k_start_vdev_delay(struct ath12k *ar,
11797 				   struct ath12k_link_vif *arvif)
11798 {
11799 	struct ath12k_base *ab = ar->ab;
11800 	struct ath12k_vif *ahvif = arvif->ahvif;
11801 	struct ieee80211_vif *vif = ath12k_ahvif_to_vif(arvif->ahvif);
11802 	struct ieee80211_chanctx_conf *chanctx;
11803 	struct ieee80211_bss_conf *link_conf;
11804 	int ret;
11805 
11806 	if (WARN_ON(arvif->is_started))
11807 		return -EBUSY;
11808 
11809 	link_conf = ath12k_mac_get_link_bss_conf(arvif);
11810 	if (!link_conf) {
11811 		ath12k_warn(ab, "failed to get link conf for vdev %u\n", arvif->vdev_id);
11812 		return -EINVAL;
11813 	}
11814 
11815 	chanctx	= wiphy_dereference(ath12k_ar_to_hw(arvif->ar)->wiphy,
11816 				    link_conf->chanctx_conf);
11817 	ret = ath12k_mac_vdev_start(arvif, chanctx);
11818 	if (ret) {
11819 		ath12k_warn(ab, "failed to start vdev %i addr %pM on freq %d: %d\n",
11820 			    arvif->vdev_id, vif->addr,
11821 			    chanctx->def.chan->center_freq, ret);
11822 		return ret;
11823 	}
11824 
11825 	if (ahvif->vdev_type == WMI_VDEV_TYPE_MONITOR) {
11826 		ret = ath12k_monitor_vdev_up(ar, arvif->vdev_id);
11827 		if (ret) {
11828 			ath12k_warn(ab, "failed put monitor up: %d\n", ret);
11829 			return ret;
11830 		}
11831 	}
11832 
11833 	arvif->is_started = true;
11834 
11835 	/* TODO: Setup ps and cts/rts protection */
11836 	return 0;
11837 }
11838 
11839 static u8 ath12k_mac_get_num_pwr_levels(struct cfg80211_chan_def *chan_def)
11840 {
11841 	if (chan_def->chan->flags & IEEE80211_CHAN_PSD) {
11842 		switch (chan_def->width) {
11843 		case NL80211_CHAN_WIDTH_20:
11844 			return 1;
11845 		case NL80211_CHAN_WIDTH_40:
11846 			return 2;
11847 		case NL80211_CHAN_WIDTH_80:
11848 			return 4;
11849 		case NL80211_CHAN_WIDTH_160:
11850 			return 8;
11851 		case NL80211_CHAN_WIDTH_320:
11852 			return 16;
11853 		default:
11854 			return 1;
11855 		}
11856 	} else {
11857 		switch (chan_def->width) {
11858 		case NL80211_CHAN_WIDTH_20:
11859 			return 1;
11860 		case NL80211_CHAN_WIDTH_40:
11861 			return 2;
11862 		case NL80211_CHAN_WIDTH_80:
11863 			return 3;
11864 		case NL80211_CHAN_WIDTH_160:
11865 			return 4;
11866 		case NL80211_CHAN_WIDTH_320:
11867 			return 5;
11868 		default:
11869 			return 1;
11870 		}
11871 	}
11872 }
11873 
11874 static u16 ath12k_mac_get_6ghz_start_frequency(struct cfg80211_chan_def *chan_def)
11875 {
11876 	u16 diff_seq;
11877 
11878 	/* It is to get the lowest channel number's center frequency of the chan.
11879 	 * For example,
11880 	 * bandwidth=40 MHz, center frequency is 5965, lowest channel is 1
11881 	 * with center frequency 5955, its diff is 5965 - 5955 = 10.
11882 	 * bandwidth=80 MHz, center frequency is 5985, lowest channel is 1
11883 	 * with center frequency 5955, its diff is 5985 - 5955 = 30.
11884 	 * bandwidth=160 MHz, center frequency is 6025, lowest channel is 1
11885 	 * with center frequency 5955, its diff is 6025 - 5955 = 70.
11886 	 * bandwidth=320 MHz, center frequency is 6105, lowest channel is 1
11887 	 * with center frequency 5955, its diff is 6105 - 5955 = 70.
11888 	 */
11889 	switch (chan_def->width) {
11890 	case NL80211_CHAN_WIDTH_320:
11891 		diff_seq = 150;
11892 		break;
11893 	case NL80211_CHAN_WIDTH_160:
11894 		diff_seq = 70;
11895 		break;
11896 	case NL80211_CHAN_WIDTH_80:
11897 		diff_seq = 30;
11898 		break;
11899 	case NL80211_CHAN_WIDTH_40:
11900 		diff_seq = 10;
11901 		break;
11902 	default:
11903 		diff_seq = 0;
11904 	}
11905 
11906 	return chan_def->center_freq1 - diff_seq;
11907 }
11908 
11909 static u16 ath12k_mac_get_seg_freq(struct cfg80211_chan_def *chan_def,
11910 				   u16 start_seq, u8 seq)
11911 {
11912 	u16 seg_seq;
11913 
11914 	/* It is to get the center frequency of the specific bandwidth.
11915 	 * start_seq means the lowest channel number's center frequency.
11916 	 * seq 0/1/2/3 means 20 MHz/40 MHz/80 MHz/160 MHz.
11917 	 * For example,
11918 	 * lowest channel is 1, its center frequency 5955,
11919 	 * center frequency is 5955 when bandwidth=20 MHz, its diff is 5955 - 5955 = 0.
11920 	 * lowest channel is 1, its center frequency 5955,
11921 	 * center frequency is 5965 when bandwidth=40 MHz, its diff is 5965 - 5955 = 10.
11922 	 * lowest channel is 1, its center frequency 5955,
11923 	 * center frequency is 5985 when bandwidth=80 MHz, its diff is 5985 - 5955 = 30.
11924 	 * lowest channel is 1, its center frequency 5955,
11925 	 * center frequency is 6025 when bandwidth=160 MHz, its diff is 6025 - 5955 = 70.
11926 	 */
11927 	seg_seq = 10 * (BIT(seq) - 1);
11928 	return seg_seq + start_seq;
11929 }
11930 
11931 static void ath12k_mac_get_psd_channel(struct ath12k *ar,
11932 				       u16 step_freq,
11933 				       u16 *start_freq,
11934 				       u16 *center_freq,
11935 				       u8 i,
11936 				       struct ieee80211_channel **temp_chan,
11937 				       s8 *tx_power)
11938 {
11939 	/* It is to get the center frequency for each 20 MHz.
11940 	 * For example, if the chan is 160 MHz and center frequency is 6025,
11941 	 * then it include 8 channels, they are 1/5/9/13/17/21/25/29,
11942 	 * channel number 1's center frequency is 5955, it is parameter start_freq.
11943 	 * parameter i is the step of the 8 channels. i is 0~7 for the 8 channels.
11944 	 * the channel 1/5/9/13/17/21/25/29 maps i=0/1/2/3/4/5/6/7,
11945 	 * and maps its center frequency is 5955/5975/5995/6015/6035/6055/6075/6095,
11946 	 * the gap is 20 for each channel, parameter step_freq means the gap.
11947 	 * after get the center frequency of each channel, it is easy to find the
11948 	 * struct ieee80211_channel of it and get the max_reg_power.
11949 	 */
11950 	*center_freq = *start_freq + i * step_freq;
11951 	*temp_chan = ieee80211_get_channel(ar->ah->hw->wiphy, *center_freq);
11952 	*tx_power = (*temp_chan)->max_reg_power;
11953 }
11954 
11955 static void ath12k_mac_get_eirp_power(struct ath12k *ar,
11956 				      u16 *start_freq,
11957 				      u16 *center_freq,
11958 				      u8 i,
11959 				      struct ieee80211_channel **temp_chan,
11960 				      struct cfg80211_chan_def *def,
11961 				      s8 *tx_power)
11962 {
11963 	/* It is to get the center frequency for 20 MHz/40 MHz/80 MHz/
11964 	 * 160 MHz bandwidth, and then plus 10 to the center frequency,
11965 	 * it is the center frequency of a channel number.
11966 	 * For example, when configured channel number is 1.
11967 	 * center frequency is 5965 when bandwidth=40 MHz, after plus 10, it is 5975,
11968 	 * then it is channel number 5.
11969 	 * center frequency is 5985 when bandwidth=80 MHz, after plus 10, it is 5995,
11970 	 * then it is channel number 9.
11971 	 * center frequency is 6025 when bandwidth=160 MHz, after plus 10, it is 6035,
11972 	 * then it is channel number 17.
11973 	 * after get the center frequency of each channel, it is easy to find the
11974 	 * struct ieee80211_channel of it and get the max_reg_power.
11975 	 */
11976 	*center_freq = ath12k_mac_get_seg_freq(def, *start_freq, i);
11977 
11978 	/* For the 20 MHz, its center frequency is same with same channel */
11979 	if (i != 0)
11980 		*center_freq += 10;
11981 
11982 	*temp_chan = ieee80211_get_channel(ar->ah->hw->wiphy, *center_freq);
11983 	*tx_power = (*temp_chan)->max_reg_power;
11984 }
11985 
11986 void ath12k_mac_fill_reg_tpc_info(struct ath12k *ar,
11987 				  struct ath12k_link_vif *arvif,
11988 				  struct ieee80211_chanctx_conf *ctx)
11989 {
11990 	struct ath12k_base *ab = ar->ab;
11991 	struct ath12k_reg_tpc_power_info *reg_tpc_info = &arvif->reg_tpc_info;
11992 	struct ieee80211_bss_conf *bss_conf = ath12k_mac_get_link_bss_conf(arvif);
11993 	struct ieee80211_channel *chan, *temp_chan;
11994 	u8 pwr_lvl_idx, num_pwr_levels, pwr_reduction;
11995 	bool is_psd_power = false, is_tpe_present = false;
11996 	s8 max_tx_power[ATH12K_NUM_PWR_LEVELS], psd_power, tx_power;
11997 	s8 eirp_power = 0;
11998 	struct ath12k_vif *ahvif = arvif->ahvif;
11999 	u16 start_freq, center_freq;
12000 	u8 reg_6ghz_power_mode;
12001 
12002 	chan = ctx->def.chan;
12003 	start_freq = ath12k_mac_get_6ghz_start_frequency(&ctx->def);
12004 	pwr_reduction = bss_conf->pwr_reduction;
12005 
12006 	if (arvif->reg_tpc_info.num_pwr_levels) {
12007 		is_tpe_present = true;
12008 		num_pwr_levels = arvif->reg_tpc_info.num_pwr_levels;
12009 	} else {
12010 		num_pwr_levels = ath12k_mac_get_num_pwr_levels(&ctx->def);
12011 	}
12012 
12013 	for (pwr_lvl_idx = 0; pwr_lvl_idx < num_pwr_levels; pwr_lvl_idx++) {
12014 		/* STA received TPE IE*/
12015 		if (is_tpe_present) {
12016 			/* local power is PSD power*/
12017 			if (chan->flags & IEEE80211_CHAN_PSD) {
12018 				/* Connecting AP is psd power */
12019 				if (reg_tpc_info->is_psd_power) {
12020 					is_psd_power = true;
12021 					ath12k_mac_get_psd_channel(ar, 20,
12022 								   &start_freq,
12023 								   &center_freq,
12024 								   pwr_lvl_idx,
12025 								   &temp_chan,
12026 								   &tx_power);
12027 					psd_power = temp_chan->psd;
12028 					eirp_power = tx_power;
12029 					max_tx_power[pwr_lvl_idx] =
12030 						min_t(s8,
12031 						      psd_power,
12032 						      reg_tpc_info->tpe[pwr_lvl_idx]);
12033 				/* Connecting AP is not psd power */
12034 				} else {
12035 					ath12k_mac_get_eirp_power(ar,
12036 								  &start_freq,
12037 								  &center_freq,
12038 								  pwr_lvl_idx,
12039 								  &temp_chan,
12040 								  &ctx->def,
12041 								  &tx_power);
12042 					psd_power = temp_chan->psd;
12043 					/* convert psd power to EIRP power based
12044 					 * on channel width
12045 					 */
12046 					tx_power =
12047 						min_t(s8, tx_power,
12048 						      psd_power + 13 + pwr_lvl_idx * 3);
12049 					max_tx_power[pwr_lvl_idx] =
12050 						min_t(s8,
12051 						      tx_power,
12052 						      reg_tpc_info->tpe[pwr_lvl_idx]);
12053 				}
12054 			/* local power is not PSD power */
12055 			} else {
12056 				/* Connecting AP is psd power */
12057 				if (reg_tpc_info->is_psd_power) {
12058 					is_psd_power = true;
12059 					ath12k_mac_get_psd_channel(ar, 20,
12060 								   &start_freq,
12061 								   &center_freq,
12062 								   pwr_lvl_idx,
12063 								   &temp_chan,
12064 								   &tx_power);
12065 					eirp_power = tx_power;
12066 					max_tx_power[pwr_lvl_idx] =
12067 						reg_tpc_info->tpe[pwr_lvl_idx];
12068 				/* Connecting AP is not psd power */
12069 				} else {
12070 					ath12k_mac_get_eirp_power(ar,
12071 								  &start_freq,
12072 								  &center_freq,
12073 								  pwr_lvl_idx,
12074 								  &temp_chan,
12075 								  &ctx->def,
12076 								  &tx_power);
12077 					max_tx_power[pwr_lvl_idx] =
12078 						min_t(s8,
12079 						      tx_power,
12080 						      reg_tpc_info->tpe[pwr_lvl_idx]);
12081 				}
12082 			}
12083 		/* STA not received TPE IE */
12084 		} else {
12085 			/* local power is PSD power*/
12086 			if (chan->flags & IEEE80211_CHAN_PSD) {
12087 				is_psd_power = true;
12088 				ath12k_mac_get_psd_channel(ar, 20,
12089 							   &start_freq,
12090 							   &center_freq,
12091 							   pwr_lvl_idx,
12092 							   &temp_chan,
12093 							   &tx_power);
12094 				psd_power = temp_chan->psd;
12095 				eirp_power = tx_power;
12096 				max_tx_power[pwr_lvl_idx] = psd_power;
12097 			} else {
12098 				ath12k_mac_get_eirp_power(ar,
12099 							  &start_freq,
12100 							  &center_freq,
12101 							  pwr_lvl_idx,
12102 							  &temp_chan,
12103 							  &ctx->def,
12104 							  &tx_power);
12105 				max_tx_power[pwr_lvl_idx] = tx_power;
12106 			}
12107 		}
12108 
12109 		if (is_psd_power) {
12110 			/* If AP local power constraint is present */
12111 			if (pwr_reduction)
12112 				eirp_power = eirp_power - pwr_reduction;
12113 
12114 			/* If firmware updated max tx power is non zero, then take
12115 			 * the min of firmware updated ap tx power
12116 			 * and max power derived from above mentioned parameters.
12117 			 */
12118 			ath12k_dbg(ab, ATH12K_DBG_MAC,
12119 				   "eirp power : %d firmware report power : %d\n",
12120 				   eirp_power, ar->max_allowed_tx_power);
12121 			/* Firmware reports lower max_allowed_tx_power during vdev
12122 			 * start response. In case of 6 GHz, firmware is not aware
12123 			 * of EIRP power unless driver sets EIRP power through WMI
12124 			 * TPC command. So radio which does not support idle power
12125 			 * save can set maximum calculated EIRP power directly to
12126 			 * firmware through TPC command without min comparison with
12127 			 * vdev start response's max_allowed_tx_power.
12128 			 */
12129 			if (ar->max_allowed_tx_power && ab->hw_params->idle_ps)
12130 				eirp_power = min_t(s8,
12131 						   eirp_power,
12132 						   ar->max_allowed_tx_power);
12133 		} else {
12134 			/* If AP local power constraint is present */
12135 			if (pwr_reduction)
12136 				max_tx_power[pwr_lvl_idx] =
12137 					max_tx_power[pwr_lvl_idx] - pwr_reduction;
12138 			/* If firmware updated max tx power is non zero, then take
12139 			 * the min of firmware updated ap tx power
12140 			 * and max power derived from above mentioned parameters.
12141 			 */
12142 			if (ar->max_allowed_tx_power && ab->hw_params->idle_ps)
12143 				max_tx_power[pwr_lvl_idx] =
12144 					min_t(s8,
12145 					      max_tx_power[pwr_lvl_idx],
12146 					      ar->max_allowed_tx_power);
12147 		}
12148 		reg_tpc_info->chan_power_info[pwr_lvl_idx].chan_cfreq = center_freq;
12149 		reg_tpc_info->chan_power_info[pwr_lvl_idx].tx_power =
12150 			max_tx_power[pwr_lvl_idx];
12151 	}
12152 
12153 	reg_tpc_info->num_pwr_levels = num_pwr_levels;
12154 	reg_tpc_info->is_psd_power = is_psd_power;
12155 	reg_tpc_info->eirp_power = eirp_power;
12156 	if (ahvif->vdev_type == WMI_VDEV_TYPE_STA)
12157 		reg_6ghz_power_mode = bss_conf->power_type;
12158 	else
12159 		/* For now, LPI is the only supported AP power mode */
12160 		reg_6ghz_power_mode = IEEE80211_REG_LPI_AP;
12161 
12162 	reg_tpc_info->ap_power_type =
12163 		ath12k_reg_ap_pwr_convert(reg_6ghz_power_mode);
12164 }
12165 
12166 static void ath12k_mac_parse_tx_pwr_env(struct ath12k *ar,
12167 					struct ath12k_link_vif *arvif)
12168 {
12169 	struct ieee80211_bss_conf *bss_conf = ath12k_mac_get_link_bss_conf(arvif);
12170 	struct ath12k_reg_tpc_power_info *tpc_info = &arvif->reg_tpc_info;
12171 	struct ieee80211_parsed_tpe_eirp *local_non_psd, *reg_non_psd;
12172 	struct ieee80211_parsed_tpe_psd *local_psd, *reg_psd;
12173 	struct ieee80211_parsed_tpe *tpe = &bss_conf->tpe;
12174 	enum wmi_reg_6g_client_type client_type;
12175 	struct ath12k_reg_info *reg_info;
12176 	struct ath12k_base *ab = ar->ab;
12177 	bool psd_valid, non_psd_valid;
12178 	int i;
12179 
12180 	reg_info = ab->reg_info[ar->pdev_idx];
12181 	client_type = reg_info->client_type;
12182 
12183 	local_psd = &tpe->psd_local[client_type];
12184 	reg_psd = &tpe->psd_reg_client[client_type];
12185 	local_non_psd = &tpe->max_local[client_type];
12186 	reg_non_psd = &tpe->max_reg_client[client_type];
12187 
12188 	psd_valid = local_psd->valid | reg_psd->valid;
12189 	non_psd_valid = local_non_psd->valid | reg_non_psd->valid;
12190 
12191 	if (!psd_valid && !non_psd_valid) {
12192 		ath12k_warn(ab,
12193 			    "no transmit power envelope match client power type %d\n",
12194 			    client_type);
12195 		return;
12196 	}
12197 
12198 	if (psd_valid) {
12199 		tpc_info->is_psd_power = true;
12200 
12201 		tpc_info->num_pwr_levels = max(local_psd->count,
12202 					       reg_psd->count);
12203 		tpc_info->num_pwr_levels =
12204 				min3(tpc_info->num_pwr_levels,
12205 				     IEEE80211_TPE_PSD_ENTRIES_320MHZ,
12206 				     ATH12K_NUM_PWR_LEVELS);
12207 
12208 		for (i = 0; i < tpc_info->num_pwr_levels; i++) {
12209 			tpc_info->tpe[i] = min(local_psd->power[i],
12210 					       reg_psd->power[i]) / 2;
12211 			ath12k_dbg(ab, ATH12K_DBG_MAC,
12212 				   "TPE PSD power[%d] : %d\n",
12213 				   i, tpc_info->tpe[i]);
12214 		}
12215 	} else {
12216 		tpc_info->is_psd_power = false;
12217 		tpc_info->eirp_power = 0;
12218 
12219 		tpc_info->num_pwr_levels = max(local_non_psd->count,
12220 					       reg_non_psd->count);
12221 		tpc_info->num_pwr_levels =
12222 				min3(tpc_info->num_pwr_levels,
12223 				     IEEE80211_TPE_EIRP_ENTRIES_320MHZ,
12224 				     ATH12K_NUM_PWR_LEVELS);
12225 
12226 		for (i = 0; i < tpc_info->num_pwr_levels; i++) {
12227 			tpc_info->tpe[i] = min(local_non_psd->power[i],
12228 					       reg_non_psd->power[i]) / 2;
12229 			ath12k_dbg(ab, ATH12K_DBG_MAC,
12230 				   "non PSD power[%d] : %d\n",
12231 				   i, tpc_info->tpe[i]);
12232 		}
12233 	}
12234 }
12235 
12236 int
12237 ath12k_mac_op_assign_vif_chanctx(struct ieee80211_hw *hw,
12238 				 struct ieee80211_vif *vif,
12239 				 struct ieee80211_bss_conf *link_conf,
12240 				 struct ieee80211_chanctx_conf *ctx)
12241 {
12242 	struct ath12k_hw *ah = ath12k_hw_to_ah(hw);
12243 	struct ath12k *ar;
12244 	struct ath12k_base *ab;
12245 	struct ath12k_vif *ahvif = ath12k_vif_to_ahvif(vif);
12246 	u8 link_id = link_conf->link_id;
12247 	struct ath12k_link_vif *arvif;
12248 	int ret;
12249 
12250 	lockdep_assert_wiphy(hw->wiphy);
12251 
12252 	/* For multi radio wiphy, the vdev was not created during add_interface
12253 	 * create now since we have a channel ctx now to assign to a specific ar/fw
12254 	 */
12255 	arvif = ath12k_mac_assign_link_vif(ah, vif, link_id);
12256 	if (!arvif) {
12257 		WARN_ON(1);
12258 		return -ENOMEM;
12259 	}
12260 
12261 	ar = ath12k_mac_assign_vif_to_vdev(hw, arvif, ctx);
12262 	if (!ar) {
12263 		ath12k_hw_warn(ah, "failed to assign chanctx for vif %pM link id %u link vif is already started",
12264 			       vif->addr, link_id);
12265 		return -EINVAL;
12266 	}
12267 
12268 	ab = ar->ab;
12269 
12270 	ath12k_dbg(ab, ATH12K_DBG_MAC,
12271 		   "mac chanctx assign ptr %p vdev_id %i\n",
12272 		   ctx, arvif->vdev_id);
12273 
12274 	if (ath12k_wmi_supports_6ghz_cc_ext(ar) &&
12275 	    ctx->def.chan->band == NL80211_BAND_6GHZ &&
12276 	    ahvif->vdev_type == WMI_VDEV_TYPE_STA)
12277 		ath12k_mac_parse_tx_pwr_env(ar, arvif);
12278 
12279 	arvif->punct_bitmap = ctx->def.punctured;
12280 
12281 	/* for some targets bss peer must be created before vdev_start */
12282 	if (ab->hw_params->vdev_start_delay &&
12283 	    ahvif->vdev_type != WMI_VDEV_TYPE_AP &&
12284 	    ahvif->vdev_type != WMI_VDEV_TYPE_MONITOR &&
12285 	    !ath12k_dp_link_peer_exist_by_vdev_id(ath12k_ab_to_dp(ab), arvif->vdev_id)) {
12286 		ret = 0;
12287 		goto out;
12288 	}
12289 
12290 	if (WARN_ON(arvif->is_started)) {
12291 		ret = -EBUSY;
12292 		goto out;
12293 	}
12294 
12295 	if (ahvif->vdev_type == WMI_VDEV_TYPE_MONITOR) {
12296 		ret = ath12k_mac_monitor_start(ar);
12297 		if (ret) {
12298 			ath12k_mac_monitor_vdev_delete(ar);
12299 			goto out;
12300 		}
12301 
12302 		arvif->is_started = true;
12303 		goto out;
12304 	}
12305 
12306 	ret = ath12k_mac_vdev_start(arvif, ctx);
12307 	if (ret) {
12308 		ath12k_warn(ab, "failed to start vdev %i addr %pM on freq %d: %d\n",
12309 			    arvif->vdev_id, vif->addr,
12310 			    ctx->def.chan->center_freq, ret);
12311 		goto out;
12312 	}
12313 
12314 	arvif->is_started = true;
12315 
12316 	/* TODO: Setup ps and cts/rts protection */
12317 
12318 out:
12319 	return ret;
12320 }
12321 EXPORT_SYMBOL(ath12k_mac_op_assign_vif_chanctx);
12322 
12323 void
12324 ath12k_mac_op_unassign_vif_chanctx(struct ieee80211_hw *hw,
12325 				   struct ieee80211_vif *vif,
12326 				   struct ieee80211_bss_conf *link_conf,
12327 				   struct ieee80211_chanctx_conf *ctx)
12328 {
12329 	struct ath12k *ar;
12330 	struct ath12k_base *ab;
12331 	struct ath12k_vif *ahvif = ath12k_vif_to_ahvif(vif);
12332 	struct ath12k_link_vif *arvif;
12333 	u8 link_id = link_conf->link_id;
12334 	int ret;
12335 
12336 	lockdep_assert_wiphy(hw->wiphy);
12337 
12338 	arvif = wiphy_dereference(hw->wiphy, ahvif->link[link_id]);
12339 
12340 	/* The vif is expected to be attached to an ar's VDEV.
12341 	 * We leave the vif/vdev in this function as is
12342 	 * and not delete the vdev symmetric to assign_vif_chanctx()
12343 	 * the VDEV will be deleted and unassigned either during
12344 	 * remove_interface() or when there is a change in channel
12345 	 * that moves the vif to a new ar
12346 	 */
12347 	if (!arvif || !arvif->is_created)
12348 		return;
12349 
12350 	ar = arvif->ar;
12351 	ab = ar->ab;
12352 
12353 	ath12k_dbg(ab, ATH12K_DBG_MAC,
12354 		   "mac chanctx unassign ptr %p vdev_id %i\n",
12355 		   ctx, arvif->vdev_id);
12356 
12357 	WARN_ON(!arvif->is_started);
12358 
12359 	if (ahvif->vdev_type == WMI_VDEV_TYPE_MONITOR) {
12360 		ret = ath12k_mac_monitor_stop(ar);
12361 		if (ret)
12362 			return;
12363 
12364 		arvif->is_started = false;
12365 	}
12366 
12367 	if (ahvif->vdev_type != WMI_VDEV_TYPE_STA &&
12368 	    ahvif->vdev_type != WMI_VDEV_TYPE_MONITOR) {
12369 		ath12k_bss_disassoc(ar, arvif);
12370 		ret = ath12k_mac_vdev_stop(arvif);
12371 		if (ret)
12372 			ath12k_warn(ab, "failed to stop vdev %i: %d\n",
12373 				    arvif->vdev_id, ret);
12374 	}
12375 	arvif->is_started = false;
12376 
12377 	if (test_bit(WMI_TLV_SERVICE_11D_OFFLOAD, ab->wmi_ab.svc_map) &&
12378 	    ahvif->vdev_type == WMI_VDEV_TYPE_STA &&
12379 	    ahvif->vdev_subtype == WMI_VDEV_SUBTYPE_NONE &&
12380 	    ar->state_11d != ATH12K_11D_PREPARING) {
12381 		reinit_completion(&ar->completed_11d_scan);
12382 		ar->state_11d = ATH12K_11D_PREPARING;
12383 	}
12384 
12385 	if (ar->scan.arvif == arvif && ar->scan.state == ATH12K_SCAN_RUNNING) {
12386 		ath12k_scan_abort(ar);
12387 		ar->scan.arvif = NULL;
12388 	}
12389 }
12390 EXPORT_SYMBOL(ath12k_mac_op_unassign_vif_chanctx);
12391 
12392 int
12393 ath12k_mac_op_switch_vif_chanctx(struct ieee80211_hw *hw,
12394 				 struct ieee80211_vif_chanctx_switch *vifs,
12395 				 int n_vifs,
12396 				 enum ieee80211_chanctx_switch_mode mode)
12397 {
12398 	struct ath12k *curr_ar, *new_ar, *group_ar;
12399 	struct ieee80211_vif_chanctx_switch *v;
12400 	int i, j, count = 0;
12401 
12402 	lockdep_assert_wiphy(hw->wiphy);
12403 
12404 	if (n_vifs == 0)
12405 		return 0;
12406 
12407 	struct ath12k **ar_map __free(kfree) = kzalloc_objs(*ar_map, n_vifs);
12408 
12409 	if (!ar_map)
12410 		return -ENOMEM;
12411 
12412 	for (i = 0; i < n_vifs; i++) {
12413 		v = &vifs[i];
12414 
12415 		if (v->old_ctx->def.chan->band != v->new_ctx->def.chan->band) {
12416 			ath12k_generic_dbg(ATH12K_DBG_MAC,
12417 					   "mac chanctx switch band change not supported\n");
12418 			return -EOPNOTSUPP;
12419 		}
12420 
12421 		curr_ar = ath12k_get_ar_by_ctx(hw, v->old_ctx);
12422 		new_ar = ath12k_get_ar_by_ctx(hw, v->new_ctx);
12423 
12424 		if (!curr_ar || !new_ar) {
12425 			ath12k_generic_dbg(ATH12K_DBG_MAC,
12426 					   "unable to determine device for the passed channel ctx\n");
12427 			ath12k_generic_dbg(ATH12K_DBG_MAC,
12428 					   "Old freq %d MHz (device %s) to new freq %d MHz (device %s)\n",
12429 					   v->old_ctx->def.chan->center_freq,
12430 					   curr_ar ? "valid" : "invalid",
12431 					   v->new_ctx->def.chan->center_freq,
12432 					   new_ar ? "valid" : "invalid");
12433 			return -EINVAL;
12434 		}
12435 
12436 		/* Switching a vif between two radios is not allowed */
12437 		if (curr_ar != new_ar) {
12438 			ath12k_dbg(curr_ar->ab, ATH12K_DBG_MAC,
12439 				   "mac chanctx switch to another radio not supported\n");
12440 			return -EOPNOTSUPP;
12441 		}
12442 
12443 		ar_map[i] = curr_ar;
12444 	}
12445 
12446 	/* Group vifs by radio (ar) and process each group independently. */
12447 	bool *processed __free(kfree) = kzalloc_objs(*processed, n_vifs);
12448 
12449 	if (!processed)
12450 		return -ENOMEM;
12451 
12452 	struct ieee80211_vif_chanctx_switch *group_vifs __free(kfree) =
12453 						kzalloc_objs(*group_vifs, n_vifs);
12454 
12455 	if (!group_vifs)
12456 		return -ENOMEM;
12457 
12458 	for (i = 0; i < n_vifs; i++) {
12459 		if (processed[i])
12460 			continue;
12461 
12462 		group_ar = ar_map[i];
12463 
12464 		count = 0;
12465 		for (j = 0; j < n_vifs; j++) {
12466 			if (!processed[j] && ar_map[j] == group_ar) {
12467 				group_vifs[count++] = vifs[j];
12468 				processed[j] = true;
12469 			}
12470 		}
12471 
12472 		ath12k_dbg(group_ar->ab, ATH12K_DBG_MAC,
12473 			   "mac chanctx switch n_vifs %d mode %d\n",
12474 			   count, mode);
12475 		ath12k_mac_update_vif_chan(group_ar, group_vifs, count);
12476 	}
12477 	return 0;
12478 }
12479 EXPORT_SYMBOL(ath12k_mac_op_switch_vif_chanctx);
12480 
12481 static int
12482 ath12k_set_vdev_param_to_all_vifs(struct ath12k *ar, int param, u32 value)
12483 {
12484 	struct ath12k_link_vif *arvif;
12485 	int ret = 0;
12486 
12487 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
12488 
12489 	list_for_each_entry(arvif, &ar->arvifs, list) {
12490 		ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "setting mac vdev %d param %d value %d\n",
12491 			   param, arvif->vdev_id, value);
12492 
12493 		ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
12494 						    param, value);
12495 		if (ret) {
12496 			ath12k_warn(ar->ab, "failed to set param %d for vdev %d: %d\n",
12497 				    param, arvif->vdev_id, ret);
12498 			break;
12499 		}
12500 	}
12501 
12502 	return ret;
12503 }
12504 
12505 /* mac80211 stores device specific RTS/Fragmentation threshold value,
12506  * this is set interface specific to firmware from ath12k driver
12507  */
12508 int ath12k_mac_op_set_rts_threshold(struct ieee80211_hw *hw,
12509 				    int radio_idx, u32 value)
12510 {
12511 	struct ath12k_hw *ah = ath12k_hw_to_ah(hw);
12512 	struct wiphy *wiphy = hw->wiphy;
12513 	struct ath12k *ar;
12514 	int param_id = WMI_VDEV_PARAM_RTS_THRESHOLD;
12515 	int ret = 0, ret_err, i;
12516 
12517 	lockdep_assert_wiphy(hw->wiphy);
12518 
12519 	if (radio_idx >= wiphy->n_radio || radio_idx < -1)
12520 		return -EINVAL;
12521 
12522 	if (radio_idx != -1) {
12523 		/* Update RTS threshold in specified radio */
12524 		ar = ath12k_ah_to_ar(ah, radio_idx);
12525 		ret = ath12k_set_vdev_param_to_all_vifs(ar, param_id, value);
12526 		if (ret) {
12527 			ath12k_warn(ar->ab,
12528 				    "failed to set RTS config for all vdevs of pdev %d",
12529 				    ar->pdev->pdev_id);
12530 			return ret;
12531 		}
12532 
12533 		ar->rts_threshold = value;
12534 		return 0;
12535 	}
12536 
12537 	/* Radio_index passed is -1, so set RTS threshold for all radios. */
12538 	for_each_ar(ah, ar, i) {
12539 		ret = ath12k_set_vdev_param_to_all_vifs(ar, param_id, value);
12540 		if (ret) {
12541 			ath12k_warn(ar->ab, "failed to set RTS config for all vdevs of pdev %d",
12542 				    ar->pdev->pdev_id);
12543 			break;
12544 		}
12545 	}
12546 	if (!ret) {
12547 		/* Setting new RTS threshold for vdevs of all radios passed, so update
12548 		 * the RTS threshold value for all radios
12549 		 */
12550 		for_each_ar(ah, ar, i)
12551 			ar->rts_threshold = value;
12552 		return 0;
12553 	}
12554 
12555 	/* RTS threshold config failed, revert to the previous RTS threshold */
12556 	for (i = i - 1; i >= 0; i--) {
12557 		ar = ath12k_ah_to_ar(ah, i);
12558 		ret_err = ath12k_set_vdev_param_to_all_vifs(ar, param_id,
12559 							    ar->rts_threshold);
12560 		if (ret_err)
12561 			ath12k_warn(ar->ab,
12562 				    "failed to restore RTS threshold for all vdevs of pdev %d",
12563 				    ar->pdev->pdev_id);
12564 	}
12565 
12566 	return ret;
12567 }
12568 EXPORT_SYMBOL(ath12k_mac_op_set_rts_threshold);
12569 
12570 int ath12k_mac_op_set_frag_threshold(struct ieee80211_hw *hw,
12571 				     int radio_idx, u32 value)
12572 {
12573 	/* Even though there's a WMI vdev param for fragmentation threshold no
12574 	 * known firmware actually implements it. Moreover it is not possible to
12575 	 * rely frame fragmentation to mac80211 because firmware clears the
12576 	 * "more fragments" bit in frame control making it impossible for remote
12577 	 * devices to reassemble frames.
12578 	 *
12579 	 * Hence implement a dummy callback just to say fragmentation isn't
12580 	 * supported. This effectively prevents mac80211 from doing frame
12581 	 * fragmentation in software.
12582 	 */
12583 
12584 	lockdep_assert_wiphy(hw->wiphy);
12585 
12586 	return -EOPNOTSUPP;
12587 }
12588 EXPORT_SYMBOL(ath12k_mac_op_set_frag_threshold);
12589 
12590 static int ath12k_mac_flush(struct ath12k *ar)
12591 {
12592 	long time_left;
12593 	int ret = 0;
12594 
12595 	time_left = wait_event_timeout(ar->dp.tx_empty_waitq,
12596 				       (atomic_read(&ar->dp.num_tx_pending) == 0),
12597 				       ATH12K_FLUSH_TIMEOUT);
12598 	if (time_left == 0) {
12599 		ath12k_warn(ar->ab,
12600 			    "failed to flush transmit queue, data pkts pending %d\n",
12601 			    atomic_read(&ar->dp.num_tx_pending));
12602 		ret = -ETIMEDOUT;
12603 	}
12604 
12605 	time_left = wait_event_timeout(ar->txmgmt_empty_waitq,
12606 				       (atomic_read(&ar->num_pending_mgmt_tx) == 0),
12607 				       ATH12K_FLUSH_TIMEOUT);
12608 	if (time_left == 0) {
12609 		ath12k_warn(ar->ab,
12610 			    "failed to flush mgmt transmit queue, mgmt pkts pending %d\n",
12611 			    atomic_read(&ar->num_pending_mgmt_tx));
12612 		ret = -ETIMEDOUT;
12613 	}
12614 
12615 	return ret;
12616 }
12617 
12618 int ath12k_mac_wait_tx_complete(struct ath12k *ar)
12619 {
12620 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
12621 
12622 	ath12k_mac_drain_tx(ar);
12623 	return ath12k_mac_flush(ar);
12624 }
12625 
12626 void ath12k_mac_op_flush(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
12627 			 u32 queues, bool drop)
12628 {
12629 	struct ath12k_hw *ah = ath12k_hw_to_ah(hw);
12630 	struct ath12k_link_vif *arvif;
12631 	struct ath12k_vif *ahvif;
12632 	unsigned long links;
12633 	struct ath12k *ar;
12634 	u8 link_id;
12635 	int i;
12636 
12637 	lockdep_assert_wiphy(hw->wiphy);
12638 
12639 	if (drop)
12640 		return;
12641 
12642 	for_each_ar(ah, ar, i)
12643 		wiphy_work_flush(hw->wiphy, &ar->wmi_mgmt_tx_work);
12644 
12645 	/* vif can be NULL when flush() is considered for hw */
12646 	if (!vif) {
12647 		for_each_ar(ah, ar, i)
12648 			ath12k_mac_flush(ar);
12649 		return;
12650 	}
12651 
12652 	ahvif = ath12k_vif_to_ahvif(vif);
12653 	links = ahvif->links_map;
12654 	for_each_set_bit(link_id, &links, IEEE80211_MLD_MAX_NUM_LINKS) {
12655 		arvif = wiphy_dereference(hw->wiphy, ahvif->link[link_id]);
12656 		if (!(arvif && arvif->ar))
12657 			continue;
12658 
12659 		ath12k_mac_flush(arvif->ar);
12660 	}
12661 }
12662 EXPORT_SYMBOL(ath12k_mac_op_flush);
12663 
12664 static int
12665 ath12k_mac_bitrate_mask_num_ht_rates(struct ath12k *ar,
12666 				     enum nl80211_band band,
12667 				     const struct cfg80211_bitrate_mask *mask)
12668 {
12669 	int num_rates = 0;
12670 	int i;
12671 
12672 	for (i = 0; i < ARRAY_SIZE(mask->control[band].ht_mcs); i++)
12673 		num_rates += hweight16(mask->control[band].ht_mcs[i]);
12674 
12675 	return num_rates;
12676 }
12677 
12678 static bool
12679 ath12k_mac_has_single_legacy_rate(struct ath12k *ar,
12680 				  enum nl80211_band band,
12681 				  const struct cfg80211_bitrate_mask *mask)
12682 {
12683 	int num_rates = 0;
12684 
12685 	num_rates = hweight32(mask->control[band].legacy);
12686 
12687 	if (ath12k_mac_bitrate_mask_num_ht_rates(ar, band, mask))
12688 		return false;
12689 
12690 	if (ath12k_mac_bitrate_mask_num_vht_rates(ar, band, mask))
12691 		return false;
12692 
12693 	if (ath12k_mac_bitrate_mask_num_he_rates(ar, band, mask))
12694 		return false;
12695 
12696 	if (ath12k_mac_bitrate_mask_num_eht_rates(ar, band, mask))
12697 		return false;
12698 
12699 	return num_rates == 1;
12700 }
12701 
12702 static __le16
12703 ath12k_mac_get_tx_mcs_map(const struct ieee80211_sta_he_cap *he_cap)
12704 {
12705 	if (he_cap->he_cap_elem.phy_cap_info[0] &
12706 	    IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_160MHZ_IN_5G)
12707 		return he_cap->he_mcs_nss_supp.tx_mcs_160;
12708 
12709 	return he_cap->he_mcs_nss_supp.tx_mcs_80;
12710 }
12711 
12712 static bool
12713 ath12k_mac_bitrate_mask_get_single_nss(struct ath12k *ar,
12714 				       struct ieee80211_vif *vif,
12715 				       enum nl80211_band band,
12716 				       const struct cfg80211_bitrate_mask *mask,
12717 				       int *nss)
12718 {
12719 	struct ieee80211_supported_band *sband = &ar->mac.sbands[band];
12720 	u16 vht_mcs_map = le16_to_cpu(sband->vht_cap.vht_mcs.tx_mcs_map);
12721 	const struct ieee80211_sband_iftype_data *data;
12722 	const struct ieee80211_sta_he_cap *he_cap;
12723 	u16 he_mcs_map = 0;
12724 	u16 eht_mcs_map = 0;
12725 	u8 ht_nss_mask = 0;
12726 	u8 vht_nss_mask = 0;
12727 	u8 he_nss_mask = 0;
12728 	u8 eht_nss_mask = 0;
12729 	u8 mcs_nss_len;
12730 	int i;
12731 
12732 	/* No need to consider legacy here. Basic rates are always present
12733 	 * in bitrate mask
12734 	 */
12735 
12736 	for (i = 0; i < ARRAY_SIZE(mask->control[band].ht_mcs); i++) {
12737 		if (mask->control[band].ht_mcs[i] == 0)
12738 			continue;
12739 		else if (mask->control[band].ht_mcs[i] ==
12740 			 sband->ht_cap.mcs.rx_mask[i])
12741 			ht_nss_mask |= BIT(i);
12742 		else
12743 			return false;
12744 	}
12745 
12746 	for (i = 0; i < ARRAY_SIZE(mask->control[band].vht_mcs); i++) {
12747 		if (mask->control[band].vht_mcs[i] == 0)
12748 			continue;
12749 		else if (mask->control[band].vht_mcs[i] ==
12750 			 ath12k_mac_get_max_vht_mcs_map(vht_mcs_map, i))
12751 			vht_nss_mask |= BIT(i);
12752 		else
12753 			return false;
12754 	}
12755 
12756 	he_cap = ieee80211_get_he_iftype_cap_vif(sband, vif);
12757 	if (!he_cap)
12758 		return false;
12759 
12760 	he_mcs_map = le16_to_cpu(ath12k_mac_get_tx_mcs_map(he_cap));
12761 
12762 	for (i = 0; i < ARRAY_SIZE(mask->control[band].he_mcs); i++) {
12763 		if (mask->control[band].he_mcs[i] == 0)
12764 			continue;
12765 
12766 		if (mask->control[band].he_mcs[i] ==
12767 		    ath12k_mac_get_max_he_mcs_map(he_mcs_map, i))
12768 			he_nss_mask |= BIT(i);
12769 		else
12770 			return false;
12771 	}
12772 
12773 	data = ieee80211_get_sband_iftype_data(sband, vif->type);
12774 
12775 	mcs_nss_len = ieee80211_eht_mcs_nss_size(&data->he_cap.he_cap_elem,
12776 						 &data->eht_cap.eht_cap_elem,
12777 						 false);
12778 	if (mcs_nss_len == 4) {
12779 		/* 20 MHz only STA case */
12780 		const struct ieee80211_eht_mcs_nss_supp_20mhz_only *eht_mcs_nss =
12781 			&data->eht_cap.eht_mcs_nss_supp.only_20mhz;
12782 		if (eht_mcs_nss->rx_tx_mcs13_max_nss)
12783 			eht_mcs_map = 0x1fff;
12784 		else if (eht_mcs_nss->rx_tx_mcs11_max_nss)
12785 			eht_mcs_map = 0x07ff;
12786 		else if (eht_mcs_nss->rx_tx_mcs9_max_nss)
12787 			eht_mcs_map = 0x01ff;
12788 		else
12789 			eht_mcs_map = 0x007f;
12790 	} else {
12791 		const struct ieee80211_eht_mcs_nss_supp_bw *eht_mcs_nss;
12792 
12793 		switch (mcs_nss_len) {
12794 		case 9:
12795 			eht_mcs_nss = &data->eht_cap.eht_mcs_nss_supp.bw._320;
12796 			break;
12797 		case 6:
12798 			eht_mcs_nss = &data->eht_cap.eht_mcs_nss_supp.bw._160;
12799 			break;
12800 		case 3:
12801 			eht_mcs_nss = &data->eht_cap.eht_mcs_nss_supp.bw._80;
12802 			break;
12803 		default:
12804 			return false;
12805 		}
12806 
12807 		if (eht_mcs_nss->rx_tx_mcs13_max_nss)
12808 			eht_mcs_map = 0x1fff;
12809 		else if (eht_mcs_nss->rx_tx_mcs11_max_nss)
12810 			eht_mcs_map = 0x7ff;
12811 		else
12812 			eht_mcs_map = 0x1ff;
12813 	}
12814 
12815 	for (i = 0; i < ARRAY_SIZE(mask->control[band].eht_mcs); i++) {
12816 		if (mask->control[band].eht_mcs[i] == 0)
12817 			continue;
12818 
12819 		if (mask->control[band].eht_mcs[i] < eht_mcs_map)
12820 			eht_nss_mask |= BIT(i);
12821 		else
12822 			return false;
12823 	}
12824 
12825 	if (ht_nss_mask != vht_nss_mask || ht_nss_mask != he_nss_mask ||
12826 	    ht_nss_mask != eht_nss_mask)
12827 		return false;
12828 
12829 	if (ht_nss_mask == 0)
12830 		return false;
12831 
12832 	if (BIT(fls(ht_nss_mask)) - 1 != ht_nss_mask)
12833 		return false;
12834 
12835 	*nss = fls(ht_nss_mask);
12836 
12837 	return true;
12838 }
12839 
12840 static int
12841 ath12k_mac_get_single_legacy_rate(struct ath12k *ar,
12842 				  enum nl80211_band band,
12843 				  const struct cfg80211_bitrate_mask *mask,
12844 				  u32 *rate, u8 *nss)
12845 {
12846 	int rate_idx;
12847 	u16 bitrate;
12848 	u8 preamble;
12849 	u8 hw_rate;
12850 
12851 	if (hweight32(mask->control[band].legacy) != 1)
12852 		return -EINVAL;
12853 
12854 	rate_idx = ffs(mask->control[band].legacy) - 1;
12855 
12856 	if (band == NL80211_BAND_5GHZ || band == NL80211_BAND_6GHZ)
12857 		rate_idx += ATH12K_MAC_FIRST_OFDM_RATE_IDX;
12858 
12859 	hw_rate = ath12k_legacy_rates[rate_idx].hw_value;
12860 	bitrate = ath12k_legacy_rates[rate_idx].bitrate;
12861 
12862 	if (ath12k_mac_bitrate_is_cck(bitrate))
12863 		preamble = WMI_RATE_PREAMBLE_CCK;
12864 	else
12865 		preamble = WMI_RATE_PREAMBLE_OFDM;
12866 
12867 	*nss = 1;
12868 	*rate = ATH12K_HW_RATE_CODE(hw_rate, 0, preamble);
12869 
12870 	return 0;
12871 }
12872 
12873 static int
12874 ath12k_mac_set_fixed_rate_gi_ltf(struct ath12k_link_vif *arvif, u8 gi, u8 ltf,
12875 				 u32 param)
12876 {
12877 	struct ath12k *ar = arvif->ar;
12878 	int ret;
12879 
12880 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
12881 
12882 	/* 0.8 = 0, 1.6 = 2 and 3.2 = 3. */
12883 	if (gi && gi != 0xFF)
12884 		gi += 1;
12885 
12886 	ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
12887 					    WMI_VDEV_PARAM_SGI, gi);
12888 	if (ret) {
12889 		ath12k_warn(ar->ab, "failed to set GI:%d, error:%d\n",
12890 			    gi, ret);
12891 		return ret;
12892 	}
12893 
12894 	if (param == WMI_VDEV_PARAM_HE_LTF) {
12895 		/* HE values start from 1 */
12896 		if (ltf != 0xFF)
12897 			ltf += 1;
12898 	} else {
12899 		/* EHT values start from 5 */
12900 		if (ltf != 0xFF)
12901 			ltf += 4;
12902 	}
12903 
12904 	ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
12905 					    param, ltf);
12906 	if (ret) {
12907 		ath12k_warn(ar->ab, "failed to set LTF:%d, error:%d\n",
12908 			    ltf, ret);
12909 		return ret;
12910 	}
12911 	return 0;
12912 }
12913 
12914 static int
12915 ath12k_mac_set_auto_rate_gi_ltf(struct ath12k_link_vif *arvif, u16 gi, u8 ltf)
12916 {
12917 	struct ath12k *ar = arvif->ar;
12918 	int ret;
12919 	u32 ar_gi_ltf;
12920 
12921 	if (gi != 0xFF) {
12922 		switch (gi) {
12923 		case ATH12K_RATE_INFO_GI_0_8:
12924 			gi = WMI_AUTORATE_800NS_GI;
12925 			break;
12926 		case ATH12K_RATE_INFO_GI_1_6:
12927 			gi = WMI_AUTORATE_1600NS_GI;
12928 			break;
12929 		case ATH12K_RATE_INFO_GI_3_2:
12930 			gi = WMI_AUTORATE_3200NS_GI;
12931 			break;
12932 		default:
12933 			ath12k_warn(ar->ab, "Invalid GI\n");
12934 			return -EINVAL;
12935 		}
12936 	}
12937 
12938 	if (ltf != 0xFF) {
12939 		switch (ltf) {
12940 		case ATH12K_RATE_INFO_1XLTF:
12941 			ltf = WMI_AUTORATE_LTF_1X;
12942 			break;
12943 		case ATH12K_RATE_INFO_2XLTF:
12944 			ltf = WMI_AUTORATE_LTF_2X;
12945 			break;
12946 		case ATH12K_RATE_INFO_4XLTF:
12947 			ltf = WMI_AUTORATE_LTF_4X;
12948 			break;
12949 		default:
12950 			ath12k_warn(ar->ab, "Invalid LTF\n");
12951 			return -EINVAL;
12952 		}
12953 	}
12954 
12955 	ar_gi_ltf = gi | ltf;
12956 
12957 	ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
12958 					    WMI_VDEV_PARAM_AUTORATE_MISC_CFG,
12959 					    ar_gi_ltf);
12960 	if (ret) {
12961 		ath12k_warn(ar->ab,
12962 			    "failed to set autorate GI:%u, LTF:%u params, error:%d\n",
12963 			    gi, ltf, ret);
12964 		return ret;
12965 	}
12966 
12967 	return 0;
12968 }
12969 
12970 static u32 ath12k_mac_nlgi_to_wmigi(enum nl80211_txrate_gi gi)
12971 {
12972 	switch (gi) {
12973 	case NL80211_TXRATE_DEFAULT_GI:
12974 		return WMI_GI_400_NS;
12975 	case NL80211_TXRATE_FORCE_LGI:
12976 		return WMI_GI_800_NS;
12977 	default:
12978 		return WMI_GI_400_NS;
12979 	}
12980 }
12981 
12982 static int ath12k_mac_set_rate_params(struct ath12k_link_vif *arvif,
12983 				      u32 rate, u8 nss, u8 sgi, u8 ldpc,
12984 				      u8 he_gi, u8 he_ltf, bool he_fixed_rate,
12985 				      u8 eht_gi, u8 eht_ltf,
12986 				      bool eht_fixed_rate)
12987 {
12988 	struct ieee80211_bss_conf *link_conf;
12989 	struct ath12k *ar = arvif->ar;
12990 	bool he_support, eht_support, gi_ltf_set = false;
12991 	u32 vdev_param;
12992 	u32 param_value;
12993 	int ret;
12994 
12995 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
12996 
12997 	link_conf = ath12k_mac_get_link_bss_conf(arvif);
12998 	if (!link_conf)
12999 		return -EINVAL;
13000 
13001 	he_support = link_conf->he_support;
13002 	eht_support = link_conf->eht_support;
13003 
13004 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
13005 		   "mac set rate params vdev %i rate 0x%02x nss 0x%02x sgi 0x%02x ldpc 0x%02x\n",
13006 		   arvif->vdev_id, rate, nss, sgi, ldpc);
13007 
13008 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
13009 		   "he_gi 0x%02x he_ltf 0x%02x he_fixed_rate %d\n", he_gi,
13010 		   he_ltf, he_fixed_rate);
13011 
13012 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
13013 		   "eht_gi 0x%02x eht_ltf 0x%02x eht_fixed_rate %d\n",
13014 		   eht_gi, eht_ltf, eht_fixed_rate);
13015 
13016 	if (!he_support && !eht_support) {
13017 		vdev_param = WMI_VDEV_PARAM_FIXED_RATE;
13018 		ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
13019 						    vdev_param, rate);
13020 		if (ret) {
13021 			ath12k_warn(ar->ab, "failed to set fixed rate param 0x%02x: %d\n",
13022 				    rate, ret);
13023 			return ret;
13024 		}
13025 	}
13026 
13027 	vdev_param = WMI_VDEV_PARAM_NSS;
13028 
13029 	ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
13030 					    vdev_param, nss);
13031 	if (ret) {
13032 		ath12k_warn(ar->ab, "failed to set nss param %d: %d\n",
13033 			    nss, ret);
13034 		return ret;
13035 	}
13036 
13037 	ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
13038 					    WMI_VDEV_PARAM_LDPC, ldpc);
13039 	if (ret) {
13040 		ath12k_warn(ar->ab, "failed to set ldpc param %d: %d\n",
13041 			    ldpc, ret);
13042 		return ret;
13043 	}
13044 
13045 	if (eht_support) {
13046 		if (eht_fixed_rate)
13047 			ret = ath12k_mac_set_fixed_rate_gi_ltf(arvif, eht_gi, eht_ltf,
13048 							       WMI_VDEV_PARAM_EHT_LTF);
13049 		else
13050 			ret = ath12k_mac_set_auto_rate_gi_ltf(arvif, eht_gi, eht_ltf);
13051 
13052 		if (ret) {
13053 			ath12k_warn(ar->ab,
13054 				    "failed to set EHT LTF/GI params %d/%d: %d\n",
13055 				    eht_gi, eht_ltf, ret);
13056 			return ret;
13057 		}
13058 		gi_ltf_set = true;
13059 	}
13060 
13061 	if (he_support) {
13062 		if (he_fixed_rate)
13063 			ret = ath12k_mac_set_fixed_rate_gi_ltf(arvif, he_gi, he_ltf,
13064 							       WMI_VDEV_PARAM_HE_LTF);
13065 		else
13066 			ret = ath12k_mac_set_auto_rate_gi_ltf(arvif, he_gi, he_ltf);
13067 		if (ret)
13068 			return ret;
13069 		gi_ltf_set = true;
13070 	}
13071 
13072 	if (!gi_ltf_set) {
13073 		vdev_param = WMI_VDEV_PARAM_SGI;
13074 		param_value = ath12k_mac_nlgi_to_wmigi(sgi);
13075 		ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
13076 						    vdev_param, param_value);
13077 		if (ret) {
13078 			ath12k_warn(ar->ab, "failed to set sgi param %d: %d\n",
13079 				    sgi, ret);
13080 			return ret;
13081 		}
13082 	}
13083 
13084 	return 0;
13085 }
13086 
13087 static bool
13088 ath12k_mac_vht_mcs_range_present(struct ath12k *ar,
13089 				 enum nl80211_band band,
13090 				 const struct cfg80211_bitrate_mask *mask)
13091 {
13092 	int i;
13093 	u16 vht_mcs;
13094 
13095 	for (i = 0; i < NL80211_VHT_NSS_MAX; i++) {
13096 		vht_mcs = mask->control[band].vht_mcs[i];
13097 
13098 		switch (vht_mcs) {
13099 		case 0:
13100 		case BIT(8) - 1:
13101 		case BIT(9) - 1:
13102 		case BIT(10) - 1:
13103 			break;
13104 		default:
13105 			return false;
13106 		}
13107 	}
13108 
13109 	return true;
13110 }
13111 
13112 static bool
13113 ath12k_mac_he_mcs_range_present(struct ath12k *ar,
13114 				enum nl80211_band band,
13115 				const struct cfg80211_bitrate_mask *mask)
13116 {
13117 	int i;
13118 	u16 he_mcs;
13119 
13120 	for (i = 0; i < NL80211_HE_NSS_MAX; i++) {
13121 		he_mcs = mask->control[band].he_mcs[i];
13122 
13123 		switch (he_mcs) {
13124 		case 0:
13125 		case BIT(8) - 1:
13126 		case BIT(10) - 1:
13127 		case BIT(12) - 1:
13128 			break;
13129 		default:
13130 			return false;
13131 		}
13132 	}
13133 
13134 	return true;
13135 }
13136 
13137 static bool
13138 ath12k_mac_eht_mcs_range_present(struct ath12k *ar,
13139 				 enum nl80211_band band,
13140 				 const struct cfg80211_bitrate_mask *mask)
13141 {
13142 	u16 eht_mcs;
13143 	int i;
13144 
13145 	for (i = 0; i < NL80211_EHT_NSS_MAX; i++) {
13146 		eht_mcs = mask->control[band].eht_mcs[i];
13147 
13148 		switch (eht_mcs) {
13149 		case 0:
13150 		case BIT(8) - 1:
13151 		case BIT(10) - 1:
13152 		case BIT(12) - 1:
13153 		case BIT(14) - 1:
13154 			break;
13155 		case BIT(15) - 1:
13156 		case BIT(16) - 1:
13157 		case BIT(16) - BIT(14) - 1:
13158 			if (i != 0)
13159 				return false;
13160 			break;
13161 		default:
13162 			return false;
13163 		}
13164 	}
13165 
13166 	return true;
13167 }
13168 
13169 static void ath12k_mac_set_bitrate_mask_iter(void *data,
13170 					     struct ieee80211_sta *sta)
13171 {
13172 	struct ath12k_link_vif *arvif = data;
13173 	struct ath12k_sta *ahsta = ath12k_sta_to_ahsta(sta);
13174 	struct ath12k_link_sta *arsta;
13175 	struct ath12k *ar = arvif->ar;
13176 
13177 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
13178 
13179 	arsta = wiphy_dereference(ath12k_ar_to_hw(ar)->wiphy,
13180 				  ahsta->link[arvif->link_id]);
13181 	if (!arsta || arsta->arvif != arvif)
13182 		return;
13183 
13184 	spin_lock_bh(&ar->data_lock);
13185 	arsta->changed |= IEEE80211_RC_SUPP_RATES_CHANGED;
13186 	spin_unlock_bh(&ar->data_lock);
13187 
13188 	wiphy_work_queue(ath12k_ar_to_hw(ar)->wiphy, &arsta->update_wk);
13189 }
13190 
13191 static void ath12k_mac_disable_peer_fixed_rate(void *data,
13192 					       struct ieee80211_sta *sta)
13193 {
13194 	struct ath12k_link_vif *arvif = data;
13195 	struct ath12k_sta *ahsta = ath12k_sta_to_ahsta(sta);
13196 	struct ath12k_link_sta *arsta;
13197 	struct ath12k *ar = arvif->ar;
13198 	int ret;
13199 
13200 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
13201 
13202 	arsta = wiphy_dereference(ath12k_ar_to_hw(ar)->wiphy,
13203 				  ahsta->link[arvif->link_id]);
13204 
13205 	if (!arsta || arsta->arvif != arvif)
13206 		return;
13207 
13208 	ret = ath12k_wmi_set_peer_param(ar, arsta->addr,
13209 					arvif->vdev_id,
13210 					WMI_PEER_PARAM_FIXED_RATE,
13211 					WMI_FIXED_RATE_NONE);
13212 	if (ret)
13213 		ath12k_warn(ar->ab,
13214 			    "failed to disable peer fixed rate for STA %pM ret %d\n",
13215 			    arsta->addr, ret);
13216 }
13217 
13218 static bool
13219 ath12k_mac_validate_fixed_rate_settings(struct ath12k *ar, enum nl80211_band band,
13220 					const struct cfg80211_bitrate_mask *mask,
13221 					unsigned int link_id)
13222 {
13223 	bool eht_fixed_rate = false, he_fixed_rate = false, vht_fixed_rate = false;
13224 	const u16 *vht_mcs_mask, *he_mcs_mask, *eht_mcs_mask;
13225 	struct ieee80211_link_sta *link_sta;
13226 	struct ath12k_dp_link_peer *peer, *tmp;
13227 	u8 vht_nss, he_nss, eht_nss;
13228 	int ret = true;
13229 	struct ath12k_base *ab = ar->ab;
13230 	struct ath12k_dp *dp = ath12k_ab_to_dp(ab);
13231 
13232 	vht_mcs_mask = mask->control[band].vht_mcs;
13233 	he_mcs_mask = mask->control[band].he_mcs;
13234 	eht_mcs_mask = mask->control[band].eht_mcs;
13235 
13236 	if (ath12k_mac_bitrate_mask_num_vht_rates(ar, band, mask) == 1)
13237 		vht_fixed_rate = true;
13238 
13239 	if (ath12k_mac_bitrate_mask_num_he_rates(ar, band, mask) == 1)
13240 		he_fixed_rate = true;
13241 
13242 	if (ath12k_mac_bitrate_mask_num_eht_rates(ar, band, mask) == 1)
13243 		eht_fixed_rate = true;
13244 
13245 	if (!vht_fixed_rate && !he_fixed_rate && !eht_fixed_rate)
13246 		return true;
13247 
13248 	vht_nss = ath12k_mac_max_vht_nss(vht_mcs_mask);
13249 	he_nss =  ath12k_mac_max_he_nss(he_mcs_mask);
13250 	eht_nss = ath12k_mac_max_eht_nss(eht_mcs_mask);
13251 
13252 	rcu_read_lock();
13253 	spin_lock_bh(&dp->dp_lock);
13254 	list_for_each_entry_safe(peer, tmp, &dp->peers, list) {
13255 		if (peer->sta) {
13256 			link_sta = rcu_dereference(peer->sta->link[link_id]);
13257 			if (!link_sta) {
13258 				ret = false;
13259 				goto exit;
13260 			}
13261 
13262 			if (vht_fixed_rate && (!link_sta->vht_cap.vht_supported ||
13263 					       link_sta->rx_nss < vht_nss)) {
13264 				ret = false;
13265 				goto exit;
13266 			}
13267 			if (he_fixed_rate && (!link_sta->he_cap.has_he ||
13268 					      link_sta->rx_nss < he_nss)) {
13269 				ret = false;
13270 				goto exit;
13271 			}
13272 			if (eht_fixed_rate && (!link_sta->eht_cap.has_eht ||
13273 					       link_sta->rx_nss < eht_nss)) {
13274 				ret = false;
13275 				goto exit;
13276 			}
13277 		}
13278 	}
13279 exit:
13280 	spin_unlock_bh(&dp->dp_lock);
13281 	rcu_read_unlock();
13282 	return ret;
13283 }
13284 
13285 int
13286 ath12k_mac_op_set_bitrate_mask(struct ieee80211_hw *hw,
13287 			       struct ieee80211_vif *vif,
13288 			       const struct cfg80211_bitrate_mask *mask)
13289 {
13290 	struct ath12k_vif *ahvif = ath12k_vif_to_ahvif(vif);
13291 	struct ath12k_link_vif *arvif;
13292 	struct cfg80211_chan_def def;
13293 	struct ath12k *ar;
13294 	enum nl80211_band band;
13295 	const u8 *ht_mcs_mask;
13296 	const u16 *vht_mcs_mask;
13297 	const u16 *he_mcs_mask;
13298 	const u16 *eht_mcs_mask;
13299 	u8 he_ltf = 0;
13300 	u8 he_gi = 0;
13301 	u8 eht_ltf = 0, eht_gi = 0;
13302 	u32 rate;
13303 	u8 nss, mac_nss;
13304 	u8 sgi;
13305 	u8 ldpc;
13306 	int single_nss;
13307 	int ret;
13308 	int num_rates;
13309 	bool he_fixed_rate = false;
13310 	bool eht_fixed_rate = false;
13311 
13312 	lockdep_assert_wiphy(hw->wiphy);
13313 
13314 	arvif = &ahvif->deflink;
13315 
13316 	ar = arvif->ar;
13317 	if (ath12k_mac_vif_link_chan(vif, arvif->link_id, &def)) {
13318 		ret = -EPERM;
13319 		goto out;
13320 	}
13321 
13322 	band = def.chan->band;
13323 	ht_mcs_mask = mask->control[band].ht_mcs;
13324 	vht_mcs_mask = mask->control[band].vht_mcs;
13325 	he_mcs_mask = mask->control[band].he_mcs;
13326 	eht_mcs_mask = mask->control[band].eht_mcs;
13327 	ldpc = !!(ar->ht_cap_info & WMI_HT_CAP_LDPC);
13328 
13329 	sgi = mask->control[band].gi;
13330 	if (sgi == NL80211_TXRATE_FORCE_SGI) {
13331 		ret = -EINVAL;
13332 		goto out;
13333 	}
13334 
13335 	he_gi = mask->control[band].he_gi;
13336 	he_ltf = mask->control[band].he_ltf;
13337 
13338 	eht_gi = mask->control[band].eht_gi;
13339 	eht_ltf = mask->control[band].eht_ltf;
13340 
13341 	/* mac80211 doesn't support sending a fixed HT/VHT MCS alone, rather it
13342 	 * requires passing at least one of used basic rates along with them.
13343 	 * Fixed rate setting across different preambles(legacy, HT, VHT) is
13344 	 * not supported by the FW. Hence use of FIXED_RATE vdev param is not
13345 	 * suitable for setting single HT/VHT rates.
13346 	 * But, there could be a single basic rate passed from userspace which
13347 	 * can be done through the FIXED_RATE param.
13348 	 */
13349 	if (ath12k_mac_has_single_legacy_rate(ar, band, mask)) {
13350 		ret = ath12k_mac_get_single_legacy_rate(ar, band, mask, &rate,
13351 							&nss);
13352 		if (ret) {
13353 			ath12k_warn(ar->ab, "failed to get single legacy rate for vdev %i: %d\n",
13354 				    arvif->vdev_id, ret);
13355 			goto out;
13356 		}
13357 
13358 		ieee80211_iterate_stations_mtx(hw,
13359 					       ath12k_mac_disable_peer_fixed_rate,
13360 					       arvif);
13361 	} else if (ath12k_mac_bitrate_mask_get_single_nss(ar, vif, band, mask,
13362 							  &single_nss)) {
13363 		rate = WMI_FIXED_RATE_NONE;
13364 		nss = single_nss;
13365 		arvif->bitrate_mask = *mask;
13366 
13367 		ieee80211_iterate_stations_atomic(hw,
13368 						  ath12k_mac_set_bitrate_mask_iter,
13369 						  arvif);
13370 	} else {
13371 		rate = WMI_FIXED_RATE_NONE;
13372 
13373 		if (!ath12k_mac_validate_fixed_rate_settings(ar, band,
13374 							     mask, arvif->link_id))
13375 			ath12k_warn(ar->ab,
13376 				    "failed to update fixed rate settings due to mcs/nss incompatibility\n");
13377 
13378 		mac_nss = max(max3(ath12k_mac_max_ht_nss(ht_mcs_mask),
13379 				   ath12k_mac_max_vht_nss(vht_mcs_mask),
13380 				   ath12k_mac_max_he_nss(he_mcs_mask)),
13381 			       ath12k_mac_max_eht_nss(eht_mcs_mask));
13382 		nss = min_t(u32, ar->num_tx_chains, mac_nss);
13383 
13384 		/* If multiple rates across different preambles are given
13385 		 * we can reconfigure this info with all peers using PEER_ASSOC
13386 		 * command with the below exception cases.
13387 		 * - Single VHT Rate : peer_assoc command accommodates only MCS
13388 		 * range values i.e 0-7, 0-8, 0-9 for VHT. Though mac80211
13389 		 * mandates passing basic rates along with HT/VHT rates, FW
13390 		 * doesn't allow switching from VHT to Legacy. Hence instead of
13391 		 * setting legacy and VHT rates using RATEMASK_CMD vdev cmd,
13392 		 * we could set this VHT rate as peer fixed rate param, which
13393 		 * will override FIXED rate and FW rate control algorithm.
13394 		 * If single VHT rate is passed along with HT rates, we select
13395 		 * the VHT rate as fixed rate for vht peers.
13396 		 * - Multiple VHT Rates : When Multiple VHT rates are given,this
13397 		 * can be set using RATEMASK CMD which uses FW rate-ctl alg.
13398 		 * TODO: Setting multiple VHT MCS and replacing peer_assoc with
13399 		 * RATEMASK_CMDID can cover all use cases of setting rates
13400 		 * across multiple preambles and rates within same type.
13401 		 * But requires more validation of the command at this point.
13402 		 */
13403 
13404 		num_rates = ath12k_mac_bitrate_mask_num_vht_rates(ar, band,
13405 								  mask);
13406 
13407 		if (!ath12k_mac_vht_mcs_range_present(ar, band, mask) &&
13408 		    num_rates > 1) {
13409 			/* TODO: Handle multiple VHT MCS values setting using
13410 			 * RATEMASK CMD
13411 			 */
13412 			ath12k_warn(ar->ab,
13413 				    "Setting more than one MCS Value in bitrate mask not supported\n");
13414 			ret = -EINVAL;
13415 			goto out;
13416 		}
13417 
13418 		num_rates = ath12k_mac_bitrate_mask_num_he_rates(ar, band, mask);
13419 		if (num_rates == 1)
13420 			he_fixed_rate = true;
13421 
13422 		if (!ath12k_mac_he_mcs_range_present(ar, band, mask) &&
13423 		    num_rates > 1) {
13424 			ath12k_warn(ar->ab,
13425 				    "Setting more than one HE MCS Value in bitrate mask not supported\n");
13426 			ret = -EINVAL;
13427 			goto out;
13428 		}
13429 
13430 		num_rates = ath12k_mac_bitrate_mask_num_eht_rates(ar, band,
13431 								  mask);
13432 		if (num_rates == 1)
13433 			eht_fixed_rate = true;
13434 
13435 		if (!ath12k_mac_eht_mcs_range_present(ar, band, mask) &&
13436 		    num_rates > 1) {
13437 			ath12k_warn(ar->ab,
13438 				    "Setting more than one EHT MCS Value in bitrate mask not supported\n");
13439 			ret = -EINVAL;
13440 			goto out;
13441 		}
13442 
13443 		ieee80211_iterate_stations_mtx(hw,
13444 					       ath12k_mac_disable_peer_fixed_rate,
13445 					       arvif);
13446 
13447 		arvif->bitrate_mask = *mask;
13448 		ieee80211_iterate_stations_mtx(hw,
13449 					       ath12k_mac_set_bitrate_mask_iter,
13450 					       arvif);
13451 	}
13452 
13453 	ret = ath12k_mac_set_rate_params(arvif, rate, nss, sgi, ldpc, he_gi,
13454 					 he_ltf, he_fixed_rate, eht_gi, eht_ltf,
13455 					 eht_fixed_rate);
13456 	if (ret) {
13457 		ath12k_warn(ar->ab, "failed to set rate params on vdev %i: %d\n",
13458 			    arvif->vdev_id, ret);
13459 	}
13460 
13461 out:
13462 	return ret;
13463 }
13464 EXPORT_SYMBOL(ath12k_mac_op_set_bitrate_mask);
13465 
13466 void
13467 ath12k_mac_op_reconfig_complete(struct ieee80211_hw *hw,
13468 				enum ieee80211_reconfig_type reconfig_type)
13469 {
13470 	struct ath12k_hw *ah = ath12k_hw_to_ah(hw);
13471 	struct ath12k *ar;
13472 	struct ath12k_base *ab;
13473 	struct ath12k_vif *ahvif;
13474 	struct ath12k_link_vif *arvif;
13475 	int recovery_count, i;
13476 
13477 	lockdep_assert_wiphy(hw->wiphy);
13478 
13479 	if (reconfig_type != IEEE80211_RECONFIG_TYPE_RESTART)
13480 		return;
13481 
13482 	guard(mutex)(&ah->hw_mutex);
13483 
13484 	if (ah->state != ATH12K_HW_STATE_RESTARTED)
13485 		return;
13486 
13487 	ah->state = ATH12K_HW_STATE_ON;
13488 	ieee80211_wake_queues(hw);
13489 
13490 	for_each_ar(ah, ar, i) {
13491 		ab = ar->ab;
13492 
13493 		ath12k_warn(ar->ab, "pdev %d successfully recovered\n",
13494 			    ar->pdev->pdev_id);
13495 
13496 		if (ar->ab->hw_params->current_cc_support &&
13497 		    ar->alpha2[0] != 0 && ar->alpha2[1] != 0) {
13498 			struct wmi_set_current_country_arg arg = {};
13499 
13500 			memcpy(&arg.alpha2, ar->alpha2, 2);
13501 			reinit_completion(&ar->regd_update_completed);
13502 			ath12k_wmi_send_set_current_country_cmd(ar, &arg);
13503 		}
13504 
13505 		if (ab->is_reset) {
13506 			recovery_count = atomic_inc_return(&ab->recovery_count);
13507 
13508 			ath12k_dbg(ab, ATH12K_DBG_BOOT, "recovery count %d\n",
13509 				   recovery_count);
13510 
13511 			/* When there are multiple radios in an SOC,
13512 			 * the recovery has to be done for each radio
13513 			 */
13514 			if (recovery_count == ab->num_radios) {
13515 				atomic_dec(&ab->reset_count);
13516 				complete(&ab->reset_complete);
13517 				ab->is_reset = false;
13518 				atomic_set(&ab->fail_cont_count, 0);
13519 				ath12k_dbg(ab, ATH12K_DBG_BOOT, "reset success\n");
13520 			}
13521 		}
13522 
13523 		list_for_each_entry(arvif, &ar->arvifs, list) {
13524 			ahvif = arvif->ahvif;
13525 			ath12k_dbg(ab, ATH12K_DBG_BOOT,
13526 				   "reconfig cipher %d up %d vdev type %d\n",
13527 				   ahvif->dp_vif.key_cipher,
13528 				   arvif->is_up,
13529 				   ahvif->vdev_type);
13530 
13531 			/* After trigger disconnect, then upper layer will
13532 			 * trigger connect again, then the PN number of
13533 			 * upper layer will be reset to keep up with AP
13534 			 * side, hence PN number mismatch will not happen.
13535 			 */
13536 			if (arvif->is_up &&
13537 			    ahvif->vdev_type == WMI_VDEV_TYPE_STA &&
13538 			    ahvif->vdev_subtype == WMI_VDEV_SUBTYPE_NONE) {
13539 				ieee80211_hw_restart_disconnect(ahvif->vif);
13540 
13541 				ath12k_dbg(ab, ATH12K_DBG_BOOT,
13542 					   "restart disconnect\n");
13543 			}
13544 		}
13545 	}
13546 }
13547 EXPORT_SYMBOL(ath12k_mac_op_reconfig_complete);
13548 
13549 static void
13550 ath12k_mac_update_bss_chan_survey(struct ath12k *ar,
13551 				  struct ieee80211_channel *channel)
13552 {
13553 	int ret;
13554 	enum wmi_bss_chan_info_req_type type = WMI_BSS_SURVEY_REQ_TYPE_READ;
13555 
13556 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
13557 
13558 	if (!test_bit(WMI_TLV_SERVICE_BSS_CHANNEL_INFO_64, ar->ab->wmi_ab.svc_map) ||
13559 	    ar->rx_channel != channel)
13560 		return;
13561 
13562 	if (ar->scan.state != ATH12K_SCAN_IDLE) {
13563 		ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
13564 			   "ignoring bss chan info req while scanning..\n");
13565 		return;
13566 	}
13567 
13568 	reinit_completion(&ar->bss_survey_done);
13569 
13570 	ret = ath12k_wmi_pdev_bss_chan_info_request(ar, type);
13571 	if (ret) {
13572 		ath12k_warn(ar->ab, "failed to send pdev bss chan info request\n");
13573 		return;
13574 	}
13575 
13576 	ret = wait_for_completion_timeout(&ar->bss_survey_done, 3 * HZ);
13577 	if (ret == 0)
13578 		ath12k_warn(ar->ab, "bss channel survey timed out\n");
13579 }
13580 
13581 int ath12k_mac_op_get_survey(struct ieee80211_hw *hw, int idx,
13582 			     struct survey_info *survey)
13583 {
13584 	struct ath12k *ar;
13585 	struct ieee80211_supported_band *sband;
13586 	struct survey_info *ar_survey;
13587 
13588 	lockdep_assert_wiphy(hw->wiphy);
13589 
13590 	if (idx >= ATH12K_NUM_CHANS)
13591 		return -ENOENT;
13592 
13593 	sband = hw->wiphy->bands[NL80211_BAND_2GHZ];
13594 	if (sband && idx >= sband->n_channels) {
13595 		idx -= sband->n_channels;
13596 		sband = NULL;
13597 	}
13598 
13599 	if (!sband)
13600 		sband = hw->wiphy->bands[NL80211_BAND_5GHZ];
13601 	if (sband && idx >= sband->n_channels) {
13602 		idx -= sband->n_channels;
13603 		sband = NULL;
13604 	}
13605 
13606 	if (!sband)
13607 		sband = hw->wiphy->bands[NL80211_BAND_6GHZ];
13608 
13609 	if (!sband || idx >= sband->n_channels)
13610 		return -ENOENT;
13611 
13612 	ar = ath12k_mac_get_ar_by_chan(hw, &sband->channels[idx]);
13613 	if (!ar) {
13614 		if (sband->channels[idx].flags & IEEE80211_CHAN_DISABLED) {
13615 			memset(survey, 0, sizeof(*survey));
13616 			return 0;
13617 		}
13618 		return -ENOENT;
13619 	}
13620 
13621 	ar_survey = &ar->survey[idx];
13622 
13623 	ath12k_mac_update_bss_chan_survey(ar, &sband->channels[idx]);
13624 
13625 	spin_lock_bh(&ar->data_lock);
13626 	memcpy(survey, ar_survey, sizeof(*survey));
13627 	spin_unlock_bh(&ar->data_lock);
13628 
13629 	survey->channel = &sband->channels[idx];
13630 
13631 	if (ar->rx_channel == survey->channel)
13632 		survey->filled |= SURVEY_INFO_IN_USE;
13633 
13634 	return 0;
13635 }
13636 EXPORT_SYMBOL(ath12k_mac_op_get_survey);
13637 
13638 static void ath12k_mac_put_chain_rssi(struct station_info *sinfo,
13639 				      struct ath12k_link_sta *arsta)
13640 {
13641 	s8 rssi;
13642 	int i;
13643 
13644 	for (i = 0; i < ARRAY_SIZE(sinfo->chain_signal); i++) {
13645 		sinfo->chains &= ~BIT(i);
13646 		rssi = arsta->chain_signal[i];
13647 
13648 		if (rssi != ATH12K_DEFAULT_NOISE_FLOOR &&
13649 		    rssi != ATH12K_INVALID_RSSI_FULL &&
13650 		    rssi != ATH12K_INVALID_RSSI_EMPTY &&
13651 		    rssi != 0) {
13652 			sinfo->chain_signal[i] = rssi;
13653 			sinfo->chains |= BIT(i);
13654 			sinfo->filled |= BIT_ULL(NL80211_STA_INFO_CHAIN_SIGNAL);
13655 		}
13656 	}
13657 }
13658 
13659 void ath12k_mac_op_sta_statistics(struct ieee80211_hw *hw,
13660 				  struct ieee80211_vif *vif,
13661 				  struct ieee80211_sta *sta,
13662 				  struct station_info *sinfo)
13663 {
13664 	struct ath12k_sta *ahsta = ath12k_sta_to_ahsta(sta);
13665 	struct ath12k_dp_link_peer_rate_info rate_info = {};
13666 	struct ath12k_fw_stats_req_params params = {};
13667 	struct ath12k_dp_link_peer *peer;
13668 	struct ath12k_link_sta *arsta;
13669 	s8 signal, noise_floor;
13670 	struct ath12k_dp *dp;
13671 	struct ath12k *ar;
13672 	bool db2dbm;
13673 
13674 	lockdep_assert_wiphy(hw->wiphy);
13675 
13676 	arsta = &ahsta->deflink;
13677 	ar = ath12k_get_ar_by_vif(hw, vif, arsta->link_id);
13678 	if (!ar)
13679 		return;
13680 
13681 	dp = ath12k_ab_to_dp(ar->ab);
13682 	ath12k_dp_link_peer_get_sta_rate_info_stats(dp, arsta->addr, &rate_info);
13683 
13684 	db2dbm = test_bit(WMI_TLV_SERVICE_HW_DB2DBM_CONVERSION_SUPPORT,
13685 			  ar->ab->wmi_ab.svc_map);
13686 
13687 	sinfo->rx_duration = rate_info.rx_duration;
13688 	sinfo->filled |= BIT_ULL(NL80211_STA_INFO_RX_DURATION);
13689 
13690 	sinfo->tx_duration = rate_info.tx_duration;
13691 	sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_DURATION);
13692 
13693 	if (rate_info.txrate.legacy || rate_info.txrate.nss) {
13694 		if (rate_info.txrate.legacy) {
13695 			sinfo->txrate.legacy = rate_info.txrate.legacy;
13696 		} else {
13697 			sinfo->txrate.mcs = rate_info.txrate.mcs;
13698 			sinfo->txrate.nss = rate_info.txrate.nss;
13699 			sinfo->txrate.bw = rate_info.txrate.bw;
13700 			sinfo->txrate.he_gi = rate_info.txrate.he_gi;
13701 			sinfo->txrate.he_dcm = rate_info.txrate.he_dcm;
13702 			sinfo->txrate.he_ru_alloc = rate_info.txrate.he_ru_alloc;
13703 			sinfo->txrate.eht_gi = rate_info.txrate.eht_gi;
13704 			sinfo->txrate.eht_ru_alloc = rate_info.txrate.eht_ru_alloc;
13705 		}
13706 		sinfo->txrate.flags = rate_info.txrate.flags;
13707 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_BITRATE);
13708 	}
13709 
13710 	/* TODO: Use real NF instead of default one. */
13711 	signal = rate_info.rssi_comb;
13712 
13713 	params.pdev_id = ath12k_mac_get_target_pdev_id(ar);
13714 	params.vdev_id = 0;
13715 	params.stats_id = WMI_REQUEST_VDEV_STAT;
13716 
13717 	if (!signal &&
13718 	    ahsta->ahvif->vdev_type == WMI_VDEV_TYPE_STA &&
13719 	    !(ath12k_mac_get_fw_stats(ar, &params))) {
13720 		signal = arsta->rssi_beacon;
13721 		ath12k_fw_stats_reset(ar);
13722 	}
13723 
13724 	params.stats_id = WMI_REQUEST_RSSI_PER_CHAIN_STAT;
13725 	if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_CHAIN_SIGNAL)) &&
13726 	    ahsta->ahvif->vdev_type == WMI_VDEV_TYPE_STA &&
13727 	    !(ath12k_mac_get_fw_stats(ar, &params))) {
13728 		ath12k_mac_put_chain_rssi(sinfo, arsta);
13729 		ath12k_fw_stats_reset(ar);
13730 	}
13731 
13732 	spin_lock_bh(&ar->data_lock);
13733 	noise_floor = ath12k_pdev_get_noise_floor(ar);
13734 	spin_unlock_bh(&ar->data_lock);
13735 
13736 	if (signal) {
13737 		sinfo->signal = db2dbm ? signal : signal + noise_floor;
13738 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_SIGNAL);
13739 	}
13740 
13741 	sinfo->signal_avg = rate_info.signal_avg;
13742 
13743 	if (!db2dbm)
13744 		sinfo->signal_avg += noise_floor;
13745 
13746 	sinfo->filled |= BIT_ULL(NL80211_STA_INFO_SIGNAL_AVG);
13747 
13748 	spin_lock_bh(&dp->dp_lock);
13749 	peer = ath12k_dp_link_peer_find_by_addr(dp, arsta->addr);
13750 	if (!peer) {
13751 		spin_unlock_bh(&dp->dp_lock);
13752 		return;
13753 	}
13754 
13755 	sinfo->tx_retries = peer->tx_retry_count;
13756 	sinfo->tx_failed = peer->tx_retry_failed;
13757 	sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_RETRIES);
13758 	sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_FAILED);
13759 
13760 	spin_unlock_bh(&dp->dp_lock);
13761 }
13762 EXPORT_SYMBOL(ath12k_mac_op_sta_statistics);
13763 
13764 void ath12k_mac_op_link_sta_statistics(struct ieee80211_hw *hw,
13765 				       struct ieee80211_vif *vif,
13766 				       struct ieee80211_link_sta *link_sta,
13767 				       struct link_station_info *link_sinfo)
13768 {
13769 	struct ath12k_sta *ahsta = ath12k_sta_to_ahsta(link_sta->sta);
13770 	struct ath12k_fw_stats_req_params params = {};
13771 	struct ath12k_dp_link_peer *peer;
13772 	struct ath12k_link_sta *arsta;
13773 	struct ath12k *ar;
13774 	s8 signal;
13775 	bool db2dbm;
13776 
13777 	lockdep_assert_wiphy(hw->wiphy);
13778 
13779 	arsta = wiphy_dereference(hw->wiphy, ahsta->link[link_sta->link_id]);
13780 
13781 	if (!arsta)
13782 		return;
13783 
13784 	ar = ath12k_get_ar_by_vif(hw, vif, arsta->link_id);
13785 	if (!ar)
13786 		return;
13787 
13788 	db2dbm = test_bit(WMI_TLV_SERVICE_HW_DB2DBM_CONVERSION_SUPPORT,
13789 			  ar->ab->wmi_ab.svc_map);
13790 
13791 	spin_lock_bh(&ar->ab->dp->dp_lock);
13792 	peer = ath12k_dp_link_peer_find_by_addr(ar->ab->dp, arsta->addr);
13793 	if (!peer) {
13794 		spin_unlock_bh(&ar->ab->dp->dp_lock);
13795 		return;
13796 	}
13797 
13798 	link_sinfo->rx_duration = peer->rx_duration;
13799 	link_sinfo->filled |= BIT_ULL(NL80211_STA_INFO_RX_DURATION);
13800 
13801 	link_sinfo->tx_duration = peer->tx_duration;
13802 	link_sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_DURATION);
13803 
13804 	if (peer->txrate.legacy || peer->txrate.nss) {
13805 		if (peer->txrate.legacy) {
13806 			link_sinfo->txrate.legacy = peer->txrate.legacy;
13807 		} else {
13808 			link_sinfo->txrate.mcs = peer->txrate.mcs;
13809 			link_sinfo->txrate.nss = peer->txrate.nss;
13810 			link_sinfo->txrate.bw = peer->txrate.bw;
13811 			link_sinfo->txrate.he_gi = peer->txrate.he_gi;
13812 			link_sinfo->txrate.he_dcm = peer->txrate.he_dcm;
13813 			link_sinfo->txrate.he_ru_alloc =
13814 				peer->txrate.he_ru_alloc;
13815 			link_sinfo->txrate.eht_gi = peer->txrate.eht_gi;
13816 			link_sinfo->txrate.eht_ru_alloc =
13817 				peer->txrate.eht_ru_alloc;
13818 		}
13819 		link_sinfo->txrate.flags = peer->txrate.flags;
13820 		link_sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_BITRATE);
13821 	}
13822 
13823 	link_sinfo->signal_avg = ewma_avg_rssi_read(&peer->avg_rssi);
13824 
13825 	if (!db2dbm)
13826 		link_sinfo->signal_avg += ATH12K_DEFAULT_NOISE_FLOOR;
13827 
13828 	link_sinfo->filled |= BIT_ULL(NL80211_STA_INFO_SIGNAL_AVG);
13829 
13830 	link_sinfo->tx_retries = peer->tx_retry_count;
13831 	link_sinfo->tx_failed = peer->tx_retry_failed;
13832 	link_sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_RETRIES);
13833 	link_sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_FAILED);
13834 
13835 	/* TODO: Use real NF instead of default one. */
13836 	signal = peer->rssi_comb;
13837 
13838 	spin_unlock_bh(&ar->ab->dp->dp_lock);
13839 
13840 	if (!signal && ahsta->ahvif->vdev_type == WMI_VDEV_TYPE_STA) {
13841 		params.pdev_id = ath12k_mac_get_target_pdev_id(ar);
13842 		params.vdev_id = 0;
13843 		params.stats_id = WMI_REQUEST_VDEV_STAT;
13844 
13845 		if (!ath12k_mac_get_fw_stats(ar, &params)) {
13846 			signal = arsta->rssi_beacon;
13847 			ath12k_fw_stats_reset(ar);
13848 		}
13849 	}
13850 
13851 	if (signal) {
13852 		link_sinfo->signal =
13853 			db2dbm ? signal : signal + ATH12K_DEFAULT_NOISE_FLOOR;
13854 		link_sinfo->filled |= BIT_ULL(NL80211_STA_INFO_SIGNAL);
13855 	}
13856 }
13857 EXPORT_SYMBOL(ath12k_mac_op_link_sta_statistics);
13858 
13859 int ath12k_mac_op_cancel_remain_on_channel(struct ieee80211_hw *hw,
13860 					   struct ieee80211_vif *vif)
13861 {
13862 	struct ath12k_hw *ah = ath12k_hw_to_ah(hw);
13863 	struct ath12k *ar;
13864 
13865 	ar = ath12k_ah_to_ar(ah, 0);
13866 
13867 	lockdep_assert_wiphy(hw->wiphy);
13868 
13869 	spin_lock_bh(&ar->data_lock);
13870 	ar->scan.roc_notify = false;
13871 	spin_unlock_bh(&ar->data_lock);
13872 
13873 	ath12k_scan_abort(ar);
13874 
13875 	cancel_delayed_work_sync(&ar->scan.timeout);
13876 	wiphy_work_flush(hw->wiphy, &ar->scan.vdev_clean_wk);
13877 
13878 	return 0;
13879 }
13880 EXPORT_SYMBOL(ath12k_mac_op_cancel_remain_on_channel);
13881 
13882 int ath12k_mac_op_remain_on_channel(struct ieee80211_hw *hw,
13883 				    struct ieee80211_vif *vif,
13884 				    struct ieee80211_channel *chan,
13885 				    int duration,
13886 				    enum ieee80211_roc_type type)
13887 {
13888 	struct ath12k_vif *ahvif = ath12k_vif_to_ahvif(vif);
13889 	struct ath12k_hw *ah = ath12k_hw_to_ah(hw);
13890 	struct ath12k_link_vif *arvif;
13891 	struct ath12k *ar;
13892 	u32 scan_time_msec;
13893 	bool create = true;
13894 	u8 link_id;
13895 	int ret;
13896 
13897 	lockdep_assert_wiphy(hw->wiphy);
13898 
13899 	ar = ath12k_mac_select_scan_device(hw, vif, chan->center_freq);
13900 	if (!ar)
13901 		return -EINVAL;
13902 
13903 	/* check if any of the links of ML VIF is already started on
13904 	 * radio(ar) corresponding to given scan frequency and use it,
13905 	 * if not use deflink(link 0) for scan purpose.
13906 	 */
13907 
13908 	link_id = ath12k_mac_find_link_id_by_ar(ahvif, ar);
13909 	arvif = ath12k_mac_assign_link_vif(ah, vif, link_id);
13910 	/* If the vif is already assigned to a specific vdev of an ar,
13911 	 * check whether its already started, vdev which is started
13912 	 * are not allowed to switch to a new radio.
13913 	 * If the vdev is not started, but was earlier created on a
13914 	 * different ar, delete that vdev and create a new one. We don't
13915 	 * delete at the scan stop as an optimization to avoid redundant
13916 	 * delete-create vdev's for the same ar, in case the request is
13917 	 * always on the same band for the vif
13918 	 */
13919 	if (arvif->is_created) {
13920 		if (WARN_ON(!arvif->ar))
13921 			return -EINVAL;
13922 
13923 		if (ar != arvif->ar && arvif->is_started)
13924 			return -EBUSY;
13925 
13926 		if (ar != arvif->ar) {
13927 			ath12k_mac_remove_link_interface(hw, arvif);
13928 			ath12k_mac_unassign_link_vif(arvif);
13929 		} else {
13930 			create = false;
13931 		}
13932 	}
13933 
13934 	if (create) {
13935 		arvif = ath12k_mac_assign_link_vif(ah, vif, link_id);
13936 
13937 		ret = ath12k_mac_vdev_create(ar, arvif);
13938 		if (ret) {
13939 			ath12k_warn(ar->ab, "unable to create scan vdev for roc: %d\n",
13940 				    ret);
13941 			ath12k_mac_unassign_link_vif(arvif);
13942 			return ret;
13943 		}
13944 	}
13945 
13946 	spin_lock_bh(&ar->data_lock);
13947 
13948 	switch (ar->scan.state) {
13949 	case ATH12K_SCAN_IDLE:
13950 		reinit_completion(&ar->scan.started);
13951 		reinit_completion(&ar->scan.completed);
13952 		reinit_completion(&ar->scan.on_channel);
13953 		ar->scan.state = ATH12K_SCAN_STARTING;
13954 		ar->scan.is_roc = true;
13955 		ar->scan.arvif = arvif;
13956 		ar->scan.roc_freq = chan->center_freq;
13957 		ar->scan.roc_notify = true;
13958 		ret = 0;
13959 		break;
13960 	case ATH12K_SCAN_STARTING:
13961 	case ATH12K_SCAN_RUNNING:
13962 	case ATH12K_SCAN_ABORTING:
13963 		ret = -EBUSY;
13964 		break;
13965 	}
13966 
13967 	spin_unlock_bh(&ar->data_lock);
13968 
13969 	if (ret)
13970 		return ret;
13971 
13972 	scan_time_msec = hw->wiphy->max_remain_on_channel_duration * 2;
13973 
13974 	struct ath12k_wmi_scan_req_arg *arg __free(kfree) =
13975 					kzalloc_flex(*arg, chan_list, 1);
13976 	if (!arg)
13977 		return -ENOMEM;
13978 
13979 	arg->num_chan = 1;
13980 	ath12k_wmi_start_scan_init(ar, arg);
13981 
13982 	arg->vdev_id = arvif->vdev_id;
13983 	arg->scan_id = ATH12K_SCAN_ID;
13984 	arg->chan_list[0] = chan->center_freq;
13985 	arg->dwell_time_active = scan_time_msec;
13986 	arg->dwell_time_passive = scan_time_msec;
13987 	arg->max_scan_time = scan_time_msec;
13988 	arg->scan_f_passive = 1;
13989 	arg->burst_duration = duration;
13990 
13991 	ret = ath12k_start_scan(ar, arg);
13992 	if (ret) {
13993 		ath12k_warn(ar->ab, "failed to start roc scan: %d\n", ret);
13994 
13995 		spin_lock_bh(&ar->data_lock);
13996 		ar->scan.state = ATH12K_SCAN_IDLE;
13997 		spin_unlock_bh(&ar->data_lock);
13998 		return ret;
13999 	}
14000 
14001 	ret = wait_for_completion_timeout(&ar->scan.on_channel, 3 * HZ);
14002 	if (ret == 0) {
14003 		ath12k_warn(ar->ab, "failed to switch to channel for roc scan\n");
14004 		ret = ath12k_scan_stop(ar);
14005 		if (ret)
14006 			ath12k_warn(ar->ab, "failed to stop scan: %d\n", ret);
14007 		return -ETIMEDOUT;
14008 	}
14009 
14010 	ieee80211_queue_delayed_work(hw, &ar->scan.timeout,
14011 				     msecs_to_jiffies(duration));
14012 
14013 	return 0;
14014 }
14015 EXPORT_SYMBOL(ath12k_mac_op_remain_on_channel);
14016 
14017 void ath12k_mac_op_set_rekey_data(struct ieee80211_hw *hw,
14018 				  struct ieee80211_vif *vif,
14019 				  struct cfg80211_gtk_rekey_data *data)
14020 {
14021 	struct ath12k_vif *ahvif = ath12k_vif_to_ahvif(vif);
14022 	struct ath12k_rekey_data *rekey_data;
14023 	struct ath12k_hw *ah = ath12k_hw_to_ah(hw);
14024 	struct ath12k *ar = ath12k_ah_to_ar(ah, 0);
14025 	struct ath12k_link_vif *arvif;
14026 
14027 	lockdep_assert_wiphy(hw->wiphy);
14028 
14029 	arvif = &ahvif->deflink;
14030 	rekey_data = &arvif->rekey_data;
14031 
14032 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac set rekey data vdev %d\n",
14033 		   arvif->vdev_id);
14034 
14035 	memcpy(rekey_data->kck, data->kck, NL80211_KCK_LEN);
14036 	memcpy(rekey_data->kek, data->kek, NL80211_KEK_LEN);
14037 
14038 	/* The supplicant works on big-endian, the firmware expects it on
14039 	 * little endian.
14040 	 */
14041 	rekey_data->replay_ctr = get_unaligned_be64(data->replay_ctr);
14042 
14043 	arvif->rekey_data.enable_offload = true;
14044 
14045 	ath12k_dbg_dump(ar->ab, ATH12K_DBG_MAC, "kck", NULL,
14046 			rekey_data->kck, NL80211_KCK_LEN);
14047 	ath12k_dbg_dump(ar->ab, ATH12K_DBG_MAC, "kek", NULL,
14048 			rekey_data->kck, NL80211_KEK_LEN);
14049 	ath12k_dbg_dump(ar->ab, ATH12K_DBG_MAC, "replay ctr", NULL,
14050 			&rekey_data->replay_ctr, sizeof(rekey_data->replay_ctr));
14051 }
14052 EXPORT_SYMBOL(ath12k_mac_op_set_rekey_data);
14053 
14054 void ath12k_mac_update_freq_range(struct ath12k *ar,
14055 				  u32 freq_low, u32 freq_high)
14056 {
14057 	if (!(freq_low && freq_high))
14058 		return;
14059 
14060 	if (ar->freq_range.start_freq || ar->freq_range.end_freq) {
14061 		ar->freq_range.start_freq = min(ar->freq_range.start_freq,
14062 						MHZ_TO_KHZ(freq_low));
14063 		ar->freq_range.end_freq = max(ar->freq_range.end_freq,
14064 					      MHZ_TO_KHZ(freq_high));
14065 	} else {
14066 		ar->freq_range.start_freq = MHZ_TO_KHZ(freq_low);
14067 		ar->freq_range.end_freq = MHZ_TO_KHZ(freq_high);
14068 	}
14069 
14070 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
14071 		   "mac pdev %u freq limit updated. New range %u->%u MHz\n",
14072 		   ar->pdev->pdev_id, KHZ_TO_MHZ(ar->freq_range.start_freq),
14073 		   KHZ_TO_MHZ(ar->freq_range.end_freq));
14074 }
14075 
14076 static void ath12k_mac_update_ch_list(struct ath12k *ar,
14077 				      struct ieee80211_supported_band *band,
14078 				      u32 freq_low, u32 freq_high)
14079 {
14080 	int i;
14081 
14082 	if (!(freq_low && freq_high))
14083 		return;
14084 
14085 	for (i = 0; i < band->n_channels; i++) {
14086 		if (band->channels[i].center_freq < freq_low ||
14087 		    band->channels[i].center_freq > freq_high)
14088 			band->channels[i].flags |= IEEE80211_CHAN_DISABLED;
14089 	}
14090 }
14091 
14092 static u32 ath12k_get_phy_id(struct ath12k *ar, u32 band)
14093 {
14094 	struct ath12k_pdev *pdev = ar->pdev;
14095 	struct ath12k_pdev_cap *pdev_cap = &pdev->cap;
14096 
14097 	if (band == WMI_HOST_WLAN_2GHZ_CAP)
14098 		return pdev_cap->band[NL80211_BAND_2GHZ].phy_id;
14099 
14100 	if (band == WMI_HOST_WLAN_5GHZ_CAP)
14101 		return pdev_cap->band[NL80211_BAND_5GHZ].phy_id;
14102 
14103 	ath12k_warn(ar->ab, "unsupported phy cap:%d\n", band);
14104 
14105 	return 0;
14106 }
14107 
14108 static int ath12k_mac_update_band(struct ath12k *ar,
14109 				  struct ieee80211_supported_band *orig_band,
14110 				  struct ieee80211_supported_band *new_band)
14111 {
14112 	int i;
14113 
14114 	if (!orig_band || !new_band)
14115 		return -EINVAL;
14116 
14117 	if (orig_band->band != new_band->band)
14118 		return -EINVAL;
14119 
14120 	for (i = 0; i < new_band->n_channels; i++) {
14121 		if (new_band->channels[i].flags & IEEE80211_CHAN_DISABLED)
14122 			continue;
14123 		/* An enabled channel in new_band should not be already enabled
14124 		 * in the orig_band
14125 		 */
14126 		if (WARN_ON(!(orig_band->channels[i].flags &
14127 			      IEEE80211_CHAN_DISABLED)))
14128 			return -EINVAL;
14129 		orig_band->channels[i].flags &= ~IEEE80211_CHAN_DISABLED;
14130 	}
14131 	return 0;
14132 }
14133 
14134 static void ath12k_mac_update_5_9_ghz_ch_list(struct ath12k *ar,
14135 					      struct ieee80211_supported_band *band)
14136 {
14137 	int i;
14138 
14139 	if (test_bit(WMI_TLV_SERVICE_5_9GHZ_SUPPORT,
14140 		     ar->ab->wmi_ab.svc_map))
14141 		return;
14142 
14143 	guard(spinlock_bh)(&ar->ab->base_lock);
14144 	if (ar->ab->dfs_region != ATH12K_DFS_REG_FCC)
14145 		return;
14146 
14147 	for (i = 0; i < band->n_channels; i++) {
14148 		if (band->channels[i].center_freq >= ATH12K_5_9_GHZ_MIN_FREQ &&
14149 		    band->channels[i].center_freq <= ATH12K_5_9_GHZ_MAX_FREQ)
14150 			band->channels[i].flags |= IEEE80211_CHAN_DISABLED;
14151 	}
14152 }
14153 
14154 static int ath12k_mac_setup_channels_rates(struct ath12k *ar,
14155 					   u32 supported_bands,
14156 					   struct ieee80211_supported_band *bands[])
14157 {
14158 	struct ieee80211_supported_band *band;
14159 	struct ath12k_wmi_hal_reg_capabilities_ext_arg *reg_cap;
14160 	struct ath12k_base *ab = ar->ab;
14161 	u32 phy_id, freq_low, freq_high;
14162 	struct ath12k_hw *ah = ar->ah;
14163 	void *channels;
14164 	int ret;
14165 
14166 	BUILD_BUG_ON((ARRAY_SIZE(ath12k_2ghz_channels) +
14167 		      ARRAY_SIZE(ath12k_5ghz_channels) +
14168 		      ARRAY_SIZE(ath12k_6ghz_channels)) !=
14169 		     ATH12K_NUM_CHANS);
14170 
14171 	reg_cap = &ab->hal_reg_cap[ar->pdev_idx];
14172 
14173 	if (supported_bands & WMI_HOST_WLAN_2GHZ_CAP) {
14174 		channels = kmemdup(ath12k_2ghz_channels,
14175 				   sizeof(ath12k_2ghz_channels),
14176 				   GFP_KERNEL);
14177 		if (!channels)
14178 			return -ENOMEM;
14179 
14180 		band = &ar->mac.sbands[NL80211_BAND_2GHZ];
14181 		band->band = NL80211_BAND_2GHZ;
14182 		band->n_channels = ARRAY_SIZE(ath12k_2ghz_channels);
14183 		band->channels = channels;
14184 		band->n_bitrates = ath12k_g_rates_size;
14185 		band->bitrates = ath12k_g_rates;
14186 
14187 		if (ab->hw_params->single_pdev_only) {
14188 			phy_id = ath12k_get_phy_id(ar, WMI_HOST_WLAN_2GHZ_CAP);
14189 			reg_cap = &ab->hal_reg_cap[phy_id];
14190 		}
14191 
14192 		freq_low = max(reg_cap->low_2ghz_chan,
14193 			       ab->reg_freq_2ghz.start_freq);
14194 		freq_high = min(reg_cap->high_2ghz_chan,
14195 				ab->reg_freq_2ghz.end_freq);
14196 
14197 		ath12k_mac_update_ch_list(ar, band,
14198 					  reg_cap->low_2ghz_chan,
14199 					  reg_cap->high_2ghz_chan);
14200 
14201 		ath12k_mac_update_freq_range(ar, freq_low, freq_high);
14202 
14203 		if (!bands[NL80211_BAND_2GHZ]) {
14204 			bands[NL80211_BAND_2GHZ] = band;
14205 		} else {
14206 			/* Split mac in same band under same wiphy */
14207 			ret = ath12k_mac_update_band(ar, bands[NL80211_BAND_2GHZ], band);
14208 			if (ret) {
14209 				kfree(channels);
14210 				band->channels = NULL;
14211 				return ret;
14212 			}
14213 			ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac pdev %u identified as 2 GHz split mac with start freq %d end freq %d",
14214 				   ar->pdev->pdev_id,
14215 				   KHZ_TO_MHZ(ar->freq_range.start_freq),
14216 				   KHZ_TO_MHZ(ar->freq_range.end_freq));
14217 		}
14218 	}
14219 
14220 	if (supported_bands & WMI_HOST_WLAN_5GHZ_CAP) {
14221 		if (reg_cap->high_5ghz_chan >= ATH12K_MIN_6GHZ_FREQ) {
14222 			channels = kmemdup(ath12k_6ghz_channels,
14223 					   sizeof(ath12k_6ghz_channels), GFP_KERNEL);
14224 			if (!channels) {
14225 				kfree(ar->mac.sbands[NL80211_BAND_2GHZ].channels);
14226 				return -ENOMEM;
14227 			}
14228 
14229 			ar->supports_6ghz = true;
14230 			band = &ar->mac.sbands[NL80211_BAND_6GHZ];
14231 			band->band = NL80211_BAND_6GHZ;
14232 			band->n_channels = ARRAY_SIZE(ath12k_6ghz_channels);
14233 			band->channels = channels;
14234 			band->n_bitrates = ath12k_a_rates_size;
14235 			band->bitrates = ath12k_a_rates;
14236 
14237 			freq_low = max(reg_cap->low_5ghz_chan,
14238 				       ab->reg_freq_6ghz.start_freq);
14239 			freq_high = min(reg_cap->high_5ghz_chan,
14240 					ab->reg_freq_6ghz.end_freq);
14241 
14242 			ath12k_mac_update_ch_list(ar, band,
14243 						  reg_cap->low_5ghz_chan,
14244 						  reg_cap->high_5ghz_chan);
14245 
14246 			ath12k_mac_update_freq_range(ar, freq_low, freq_high);
14247 			ah->use_6ghz_regd = true;
14248 
14249 			if (!bands[NL80211_BAND_6GHZ]) {
14250 				bands[NL80211_BAND_6GHZ] = band;
14251 			} else {
14252 				/* Split mac in same band under same wiphy */
14253 				ret = ath12k_mac_update_band(ar,
14254 							     bands[NL80211_BAND_6GHZ],
14255 							     band);
14256 				if (ret) {
14257 					kfree(ar->mac.sbands[NL80211_BAND_2GHZ].channels);
14258 					ar->mac.sbands[NL80211_BAND_2GHZ].channels = NULL;
14259 					kfree(channels);
14260 					band->channels = NULL;
14261 					return ret;
14262 				}
14263 				ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac pdev %u identified as 6 GHz split mac with start freq %d end freq %d",
14264 					   ar->pdev->pdev_id,
14265 					   KHZ_TO_MHZ(ar->freq_range.start_freq),
14266 					   KHZ_TO_MHZ(ar->freq_range.end_freq));
14267 			}
14268 		}
14269 
14270 		if (reg_cap->low_5ghz_chan < ATH12K_MIN_6GHZ_FREQ) {
14271 			channels = kmemdup(ath12k_5ghz_channels,
14272 					   sizeof(ath12k_5ghz_channels),
14273 					   GFP_KERNEL);
14274 			if (!channels) {
14275 				kfree(ar->mac.sbands[NL80211_BAND_2GHZ].channels);
14276 				kfree(ar->mac.sbands[NL80211_BAND_6GHZ].channels);
14277 				return -ENOMEM;
14278 			}
14279 
14280 			band = &ar->mac.sbands[NL80211_BAND_5GHZ];
14281 			band->band = NL80211_BAND_5GHZ;
14282 			band->n_channels = ARRAY_SIZE(ath12k_5ghz_channels);
14283 			band->channels = channels;
14284 			band->n_bitrates = ath12k_a_rates_size;
14285 			band->bitrates = ath12k_a_rates;
14286 
14287 			ath12k_mac_update_5_9_ghz_ch_list(ar, band);
14288 
14289 			if (ab->hw_params->single_pdev_only) {
14290 				phy_id = ath12k_get_phy_id(ar, WMI_HOST_WLAN_5GHZ_CAP);
14291 				reg_cap = &ab->hal_reg_cap[phy_id];
14292 			}
14293 
14294 			freq_low = max(reg_cap->low_5ghz_chan,
14295 				       ab->reg_freq_5ghz.start_freq);
14296 			freq_high = min(reg_cap->high_5ghz_chan,
14297 					ab->reg_freq_5ghz.end_freq);
14298 
14299 			ath12k_mac_update_ch_list(ar, band,
14300 						  reg_cap->low_5ghz_chan,
14301 						  reg_cap->high_5ghz_chan);
14302 
14303 			ath12k_mac_update_freq_range(ar, freq_low, freq_high);
14304 
14305 			if (!bands[NL80211_BAND_5GHZ]) {
14306 				bands[NL80211_BAND_5GHZ] = band;
14307 			} else {
14308 				/* Split mac in same band under same wiphy */
14309 				ret = ath12k_mac_update_band(ar,
14310 							     bands[NL80211_BAND_5GHZ],
14311 							     band);
14312 				if (ret) {
14313 					kfree(ar->mac.sbands[NL80211_BAND_2GHZ].channels);
14314 					ar->mac.sbands[NL80211_BAND_2GHZ].channels = NULL;
14315 					kfree(ar->mac.sbands[NL80211_BAND_6GHZ].channels);
14316 					ar->mac.sbands[NL80211_BAND_2GHZ].channels = NULL;
14317 					kfree(channels);
14318 					band->channels = NULL;
14319 					return ret;
14320 				}
14321 				ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac pdev %u identified as 5 GHz split mac with start freq %d end freq %d",
14322 					   ar->pdev->pdev_id,
14323 					   KHZ_TO_MHZ(ar->freq_range.start_freq),
14324 					   KHZ_TO_MHZ(ar->freq_range.end_freq));
14325 			}
14326 		}
14327 	}
14328 
14329 	return 0;
14330 }
14331 
14332 static u16 ath12k_mac_get_ifmodes(struct ath12k_hw *ah)
14333 {
14334 	struct ath12k *ar;
14335 	int i;
14336 	u16 interface_modes = U16_MAX;
14337 
14338 	for_each_ar(ah, ar, i)
14339 		interface_modes &= ar->ab->hw_params->interface_modes;
14340 
14341 	return interface_modes == U16_MAX ? 0 : interface_modes;
14342 }
14343 
14344 static bool ath12k_mac_is_iface_mode_enable(struct ath12k_hw *ah,
14345 					    enum nl80211_iftype type)
14346 {
14347 	struct ath12k *ar;
14348 	int i;
14349 	u16 interface_modes, mode = 0;
14350 	bool is_enable = false;
14351 
14352 	if (type == NL80211_IFTYPE_MESH_POINT) {
14353 		if (IS_ENABLED(CONFIG_MAC80211_MESH))
14354 			mode = BIT(type);
14355 	} else {
14356 		mode = BIT(type);
14357 	}
14358 
14359 	for_each_ar(ah, ar, i) {
14360 		interface_modes = ar->ab->hw_params->interface_modes;
14361 		if (interface_modes & mode) {
14362 			is_enable = true;
14363 			break;
14364 		}
14365 	}
14366 
14367 	return is_enable;
14368 }
14369 
14370 static int
14371 ath12k_mac_setup_radio_iface_comb(struct ath12k *ar,
14372 				  struct ieee80211_iface_combination *comb)
14373 {
14374 	u16 interface_modes = ar->ab->hw_params->interface_modes;
14375 	struct ieee80211_iface_limit *limits;
14376 	int n_limits, max_interfaces;
14377 	bool ap, mesh, p2p;
14378 
14379 	ap = interface_modes & BIT(NL80211_IFTYPE_AP);
14380 	p2p = interface_modes & BIT(NL80211_IFTYPE_P2P_DEVICE);
14381 
14382 	mesh = IS_ENABLED(CONFIG_MAC80211_MESH) &&
14383 	       (interface_modes & BIT(NL80211_IFTYPE_MESH_POINT));
14384 
14385 	if ((ap || mesh) && !p2p) {
14386 		n_limits = 2;
14387 		max_interfaces = 16;
14388 	} else if (p2p) {
14389 		n_limits = 3;
14390 		if (ap || mesh)
14391 			max_interfaces = 16;
14392 		else
14393 			max_interfaces = 3;
14394 	} else {
14395 		n_limits = 1;
14396 		max_interfaces = 1;
14397 	}
14398 
14399 	limits = kzalloc_objs(*limits, n_limits);
14400 	if (!limits)
14401 		return -ENOMEM;
14402 
14403 	limits[0].max = 1;
14404 	limits[0].types |= BIT(NL80211_IFTYPE_STATION);
14405 
14406 	if (ap || mesh || p2p)
14407 		limits[1].max = max_interfaces;
14408 
14409 	if (ap)
14410 		limits[1].types |= BIT(NL80211_IFTYPE_AP);
14411 
14412 	if (mesh)
14413 		limits[1].types |= BIT(NL80211_IFTYPE_MESH_POINT);
14414 
14415 	if (p2p) {
14416 		limits[1].types |= BIT(NL80211_IFTYPE_P2P_CLIENT) |
14417 					BIT(NL80211_IFTYPE_P2P_GO);
14418 		limits[2].max = 1;
14419 		limits[2].types |= BIT(NL80211_IFTYPE_P2P_DEVICE);
14420 	}
14421 
14422 	comb[0].limits = limits;
14423 	comb[0].n_limits = n_limits;
14424 	comb[0].max_interfaces = max_interfaces;
14425 	comb[0].beacon_int_infra_match = true;
14426 	comb[0].beacon_int_min_gcd = 100;
14427 
14428 	comb[0].num_different_channels = 1;
14429 	comb[0].radar_detect_widths = BIT(NL80211_CHAN_WIDTH_20_NOHT) |
14430 				      BIT(NL80211_CHAN_WIDTH_20) |
14431 				      BIT(NL80211_CHAN_WIDTH_40) |
14432 				      BIT(NL80211_CHAN_WIDTH_80) |
14433 				      BIT(NL80211_CHAN_WIDTH_160);
14434 
14435 	return 0;
14436 }
14437 
14438 static int
14439 ath12k_mac_setup_global_iface_comb(struct ath12k_hw *ah,
14440 				   struct wiphy_radio *radio,
14441 				   u8 n_radio,
14442 				   struct ieee80211_iface_combination *comb)
14443 {
14444 	const struct ieee80211_iface_combination *iter_comb;
14445 	struct ieee80211_iface_limit *limits;
14446 	int i, j, n_limits;
14447 	bool ap, mesh, p2p;
14448 
14449 	if (!n_radio)
14450 		return 0;
14451 
14452 	ap = ath12k_mac_is_iface_mode_enable(ah, NL80211_IFTYPE_AP);
14453 	p2p = ath12k_mac_is_iface_mode_enable(ah, NL80211_IFTYPE_P2P_DEVICE);
14454 	mesh = ath12k_mac_is_iface_mode_enable(ah, NL80211_IFTYPE_MESH_POINT);
14455 
14456 	if ((ap || mesh) && !p2p)
14457 		n_limits = 2;
14458 	else if (p2p)
14459 		n_limits = 3;
14460 	else
14461 		n_limits = 1;
14462 
14463 	limits = kzalloc_objs(*limits, n_limits);
14464 	if (!limits)
14465 		return -ENOMEM;
14466 
14467 	for (i = 0; i < n_radio; i++) {
14468 		iter_comb = radio[i].iface_combinations;
14469 		for (j = 0; j < iter_comb->n_limits && j < n_limits; j++) {
14470 			limits[j].types |= iter_comb->limits[j].types;
14471 			limits[j].max += iter_comb->limits[j].max;
14472 		}
14473 
14474 		comb->max_interfaces += iter_comb->max_interfaces;
14475 		comb->num_different_channels += iter_comb->num_different_channels;
14476 		comb->radar_detect_widths |= iter_comb->radar_detect_widths;
14477 	}
14478 
14479 	comb->limits = limits;
14480 	comb->n_limits = n_limits;
14481 	comb->beacon_int_infra_match = true;
14482 	comb->beacon_int_min_gcd = 100;
14483 
14484 	return 0;
14485 }
14486 
14487 static
14488 void ath12k_mac_cleanup_iface_comb(const struct ieee80211_iface_combination *iface_comb)
14489 {
14490 	kfree(iface_comb[0].limits);
14491 	kfree(iface_comb);
14492 }
14493 
14494 static void ath12k_mac_cleanup_iface_combinations(struct ath12k_hw *ah)
14495 {
14496 	struct wiphy *wiphy = ah->hw->wiphy;
14497 	const struct wiphy_radio *radio;
14498 	int i;
14499 
14500 	if (wiphy->n_radio > 0) {
14501 		radio = wiphy->radio;
14502 		for (i = 0; i < wiphy->n_radio; i++)
14503 			ath12k_mac_cleanup_iface_comb(radio[i].iface_combinations);
14504 
14505 		kfree(wiphy->radio);
14506 	}
14507 
14508 	ath12k_mac_cleanup_iface_comb(wiphy->iface_combinations);
14509 }
14510 
14511 static int ath12k_mac_setup_iface_combinations(struct ath12k_hw *ah)
14512 {
14513 	struct ieee80211_iface_combination *combinations, *comb;
14514 	struct wiphy *wiphy = ah->hw->wiphy;
14515 	struct wiphy_radio *radio;
14516 	int n_combinations = 1;
14517 	struct ath12k *ar;
14518 	int i, ret;
14519 
14520 	if (ah->num_radio == 1) {
14521 		ar = &ah->radio[0];
14522 
14523 		if (ar->ab->hw_params->single_pdev_only)
14524 			n_combinations = 2;
14525 
14526 		combinations = kzalloc_objs(*combinations, n_combinations);
14527 		if (!combinations)
14528 			return -ENOMEM;
14529 
14530 		ret = ath12k_mac_setup_radio_iface_comb(ar, combinations);
14531 		if (ret) {
14532 			ath12k_hw_warn(ah, "failed to setup radio interface combinations for one radio: %d",
14533 				       ret);
14534 			goto err_free_combinations;
14535 		}
14536 
14537 		if (ar->ab->hw_params->single_pdev_only) {
14538 			comb = combinations + 1;
14539 			memcpy(comb, combinations, sizeof(*comb));
14540 			comb->num_different_channels = 2;
14541 			comb->radar_detect_widths = 0;
14542 		}
14543 
14544 		goto out;
14545 	}
14546 
14547 	combinations = kzalloc_objs(*combinations, n_combinations);
14548 	if (!combinations)
14549 		return -ENOMEM;
14550 
14551 	/* there are multiple radios */
14552 
14553 	radio = kzalloc_objs(*radio, ah->num_radio);
14554 	if (!radio) {
14555 		ret = -ENOMEM;
14556 		goto err_free_combinations;
14557 	}
14558 
14559 	for_each_ar(ah, ar, i) {
14560 		comb = kzalloc_obj(*comb);
14561 		if (!comb) {
14562 			ret = -ENOMEM;
14563 			goto err_free_radios;
14564 		}
14565 
14566 		ret = ath12k_mac_setup_radio_iface_comb(ar, comb);
14567 		if (ret) {
14568 			ath12k_hw_warn(ah, "failed to setup radio interface combinations for radio %d: %d",
14569 				       i, ret);
14570 			kfree(comb);
14571 			goto err_free_radios;
14572 		}
14573 
14574 		radio[i].freq_range = &ar->freq_range;
14575 		radio[i].n_freq_range = 1;
14576 
14577 		radio[i].iface_combinations = comb;
14578 		radio[i].n_iface_combinations = 1;
14579 	}
14580 
14581 	ret = ath12k_mac_setup_global_iface_comb(ah, radio, ah->num_radio, combinations);
14582 	if (ret) {
14583 		ath12k_hw_warn(ah, "failed to setup global interface combinations: %d",
14584 			       ret);
14585 		goto err_free_all_radios;
14586 	}
14587 
14588 	wiphy->radio = radio;
14589 	wiphy->n_radio = ah->num_radio;
14590 
14591 out:
14592 	wiphy->iface_combinations = combinations;
14593 	wiphy->n_iface_combinations = n_combinations;
14594 
14595 	return 0;
14596 
14597 err_free_all_radios:
14598 	i = ah->num_radio;
14599 
14600 err_free_radios:
14601 	while (i--)
14602 		ath12k_mac_cleanup_iface_comb(radio[i].iface_combinations);
14603 
14604 	kfree(radio);
14605 
14606 err_free_combinations:
14607 	kfree(combinations);
14608 
14609 	return ret;
14610 }
14611 
14612 static const u8 ath12k_if_types_ext_capa[] = {
14613 	[0] = WLAN_EXT_CAPA1_EXT_CHANNEL_SWITCHING,
14614 	[2] = WLAN_EXT_CAPA3_MULTI_BSSID_SUPPORT,
14615 	[7] = WLAN_EXT_CAPA8_OPMODE_NOTIF,
14616 };
14617 
14618 static const u8 ath12k_if_types_ext_capa_sta[] = {
14619 	[0] = WLAN_EXT_CAPA1_EXT_CHANNEL_SWITCHING,
14620 	[2] = WLAN_EXT_CAPA3_MULTI_BSSID_SUPPORT,
14621 	[7] = WLAN_EXT_CAPA8_OPMODE_NOTIF,
14622 	[9] = WLAN_EXT_CAPA10_TWT_REQUESTER_SUPPORT,
14623 };
14624 
14625 static const u8 ath12k_if_types_ext_capa_ap[] = {
14626 	[0] = WLAN_EXT_CAPA1_EXT_CHANNEL_SWITCHING,
14627 	[2] = WLAN_EXT_CAPA3_MULTI_BSSID_SUPPORT,
14628 	[7] = WLAN_EXT_CAPA8_OPMODE_NOTIF,
14629 	[9] = WLAN_EXT_CAPA10_TWT_RESPONDER_SUPPORT,
14630 	[10] = WLAN_EXT_CAPA11_EMA_SUPPORT,
14631 };
14632 
14633 static struct wiphy_iftype_ext_capab ath12k_iftypes_ext_capa[] = {
14634 	{
14635 		.extended_capabilities = ath12k_if_types_ext_capa,
14636 		.extended_capabilities_mask = ath12k_if_types_ext_capa,
14637 		.extended_capabilities_len = sizeof(ath12k_if_types_ext_capa),
14638 	}, {
14639 		.iftype = NL80211_IFTYPE_STATION,
14640 		.extended_capabilities = ath12k_if_types_ext_capa_sta,
14641 		.extended_capabilities_mask = ath12k_if_types_ext_capa_sta,
14642 		.extended_capabilities_len =
14643 				sizeof(ath12k_if_types_ext_capa_sta),
14644 	}, {
14645 		.iftype = NL80211_IFTYPE_AP,
14646 		.extended_capabilities = ath12k_if_types_ext_capa_ap,
14647 		.extended_capabilities_mask = ath12k_if_types_ext_capa_ap,
14648 		.extended_capabilities_len =
14649 				sizeof(ath12k_if_types_ext_capa_ap),
14650 		.eml_capabilities = 0,
14651 		.mld_capa_and_ops = 0,
14652 	},
14653 };
14654 
14655 static void ath12k_mac_cleanup_unregister(struct ath12k *ar)
14656 {
14657 	idr_for_each(&ar->txmgmt_idr, ath12k_mac_tx_mgmt_pending_free, ar);
14658 	idr_destroy(&ar->txmgmt_idr);
14659 
14660 	kfree(ar->mac.sbands[NL80211_BAND_2GHZ].channels);
14661 	kfree(ar->mac.sbands[NL80211_BAND_5GHZ].channels);
14662 	kfree(ar->mac.sbands[NL80211_BAND_6GHZ].channels);
14663 }
14664 
14665 static void ath12k_mac_hw_unregister(struct ath12k_hw *ah)
14666 {
14667 	struct ieee80211_hw *hw = ah->hw;
14668 	struct ath12k *ar;
14669 	int i;
14670 
14671 	for_each_ar(ah, ar, i) {
14672 		cancel_work_sync(&ar->regd_channel_update_work);
14673 		cancel_work_sync(&ar->regd_update_work);
14674 		ath12k_debugfs_unregister(ar);
14675 		ath12k_fw_stats_reset(ar);
14676 	}
14677 
14678 	ieee80211_unregister_hw(hw);
14679 
14680 	for_each_ar(ah, ar, i)
14681 		ath12k_mac_cleanup_unregister(ar);
14682 
14683 	ath12k_mac_cleanup_iface_combinations(ah);
14684 
14685 	SET_IEEE80211_DEV(hw, NULL);
14686 }
14687 
14688 static int ath12k_mac_setup_register(struct ath12k *ar,
14689 				     u32 *ht_cap,
14690 				     struct ieee80211_supported_band *bands[])
14691 {
14692 	struct ath12k_pdev_cap *cap = &ar->pdev->cap;
14693 	int ret;
14694 
14695 	init_waitqueue_head(&ar->txmgmt_empty_waitq);
14696 	idr_init(&ar->txmgmt_idr);
14697 	spin_lock_init(&ar->txmgmt_idr_lock);
14698 
14699 	ath12k_pdev_caps_update(ar);
14700 
14701 	ret = ath12k_mac_setup_channels_rates(ar,
14702 					      cap->supported_bands,
14703 					      bands);
14704 	if (ret)
14705 		return ret;
14706 
14707 	ath12k_mac_setup_ht_vht_cap(ar, cap, ht_cap);
14708 	ath12k_mac_setup_sband_iftype_data(ar, cap);
14709 
14710 	ar->max_num_stations = ath12k_core_get_max_station_per_radio(ar->ab);
14711 	ar->max_num_peers = ath12k_core_get_max_peers_per_radio(ar->ab);
14712 
14713 	ar->rssi_info.min_nf_dbm = ATH12K_DEFAULT_NOISE_FLOOR;
14714 	ar->rssi_info.temp_offset = 0;
14715 	ar->rssi_info.noise_floor = ar->rssi_info.min_nf_dbm + ar->rssi_info.temp_offset;
14716 
14717 	ath12k_thermal_init_configs(ar);
14718 
14719 	return 0;
14720 }
14721 
14722 static int ath12k_mac_hw_register(struct ath12k_hw *ah)
14723 {
14724 	struct ieee80211_hw *hw = ah->hw;
14725 	struct wiphy *wiphy = hw->wiphy;
14726 	struct ath12k *ar = ath12k_ah_to_ar(ah, 0);
14727 	struct ath12k_base *ab = ar->ab;
14728 	struct ath12k_pdev *pdev;
14729 	struct ath12k_pdev_cap *cap;
14730 	static const u32 cipher_suites[] = {
14731 		WLAN_CIPHER_SUITE_TKIP,
14732 		WLAN_CIPHER_SUITE_CCMP,
14733 		WLAN_CIPHER_SUITE_AES_CMAC,
14734 		WLAN_CIPHER_SUITE_BIP_CMAC_256,
14735 		WLAN_CIPHER_SUITE_BIP_GMAC_128,
14736 		WLAN_CIPHER_SUITE_BIP_GMAC_256,
14737 		WLAN_CIPHER_SUITE_GCMP,
14738 		WLAN_CIPHER_SUITE_GCMP_256,
14739 		WLAN_CIPHER_SUITE_CCMP_256,
14740 	};
14741 	int ret, i, j;
14742 	u32 ht_cap = U32_MAX, antennas_rx = 0, antennas_tx = 0;
14743 	bool is_6ghz = false, is_raw_mode = false, is_monitor_disable = false;
14744 	u8 *mac_addr = NULL;
14745 	u8 mbssid_max_interfaces = 0;
14746 
14747 	wiphy->max_ap_assoc_sta = 0;
14748 
14749 	for_each_ar(ah, ar, i) {
14750 		u32 ht_cap_info = 0;
14751 
14752 		pdev = ar->pdev;
14753 		if (ar->ab->pdevs_macaddr_valid) {
14754 			ether_addr_copy(ar->mac_addr, pdev->mac_addr);
14755 		} else {
14756 			ether_addr_copy(ar->mac_addr, ar->ab->mac_addr);
14757 			ar->mac_addr[4] += ar->pdev_idx;
14758 		}
14759 
14760 		ret = ath12k_mac_setup_register(ar, &ht_cap_info, hw->wiphy->bands);
14761 		if (ret)
14762 			goto err_cleanup_unregister;
14763 
14764 		/* 6 GHz does not support HT Cap, hence do not consider it */
14765 		if (!ar->supports_6ghz)
14766 			ht_cap &= ht_cap_info;
14767 
14768 		wiphy->max_ap_assoc_sta += ar->max_num_stations;
14769 
14770 		/* Advertise the max antenna support of all radios, driver can handle
14771 		 * per pdev specific antenna setting based on pdev cap when antenna
14772 		 * changes are made
14773 		 */
14774 		cap = &pdev->cap;
14775 
14776 		antennas_rx = max_t(u32, antennas_rx, cap->rx_chain_mask);
14777 		antennas_tx = max_t(u32, antennas_tx, cap->tx_chain_mask);
14778 
14779 		if (ar->supports_6ghz)
14780 			is_6ghz = true;
14781 
14782 		if (test_bit(ATH12K_FLAG_RAW_MODE, &ar->ab->dev_flags))
14783 			is_raw_mode = true;
14784 
14785 		if (!ar->ab->hw_params->supports_monitor)
14786 			is_monitor_disable = true;
14787 
14788 		if (i == 0)
14789 			mac_addr = ar->mac_addr;
14790 		else
14791 			mac_addr = ab->mac_addr;
14792 
14793 		mbssid_max_interfaces += TARGET_NUM_VDEVS(ar->ab);
14794 	}
14795 
14796 	wiphy->available_antennas_rx = antennas_rx;
14797 	wiphy->available_antennas_tx = antennas_tx;
14798 
14799 	SET_IEEE80211_PERM_ADDR(hw, mac_addr);
14800 	SET_IEEE80211_DEV(hw, ab->dev);
14801 
14802 	ret = ath12k_mac_setup_iface_combinations(ah);
14803 	if (ret) {
14804 		ath12k_err(ab, "failed to setup interface combinations: %d\n", ret);
14805 		goto err_complete_cleanup_unregister;
14806 	}
14807 
14808 	wiphy->interface_modes = ath12k_mac_get_ifmodes(ah);
14809 
14810 	if (ah->num_radio == 1 &&
14811 	    wiphy->bands[NL80211_BAND_2GHZ] &&
14812 	    wiphy->bands[NL80211_BAND_5GHZ] &&
14813 	    wiphy->bands[NL80211_BAND_6GHZ])
14814 		ieee80211_hw_set(hw, SINGLE_SCAN_ON_ALL_BANDS);
14815 
14816 	ieee80211_hw_set(hw, SIGNAL_DBM);
14817 	ieee80211_hw_set(hw, SUPPORTS_PS);
14818 	ieee80211_hw_set(hw, SUPPORTS_DYNAMIC_PS);
14819 	ieee80211_hw_set(hw, MFP_CAPABLE);
14820 	ieee80211_hw_set(hw, REPORTS_TX_ACK_STATUS);
14821 	ieee80211_hw_set(hw, HAS_RATE_CONTROL);
14822 	ieee80211_hw_set(hw, AP_LINK_PS);
14823 	ieee80211_hw_set(hw, SPECTRUM_MGMT);
14824 	ieee80211_hw_set(hw, CONNECTION_MONITOR);
14825 	ieee80211_hw_set(hw, SUPPORTS_PER_STA_GTK);
14826 	ieee80211_hw_set(hw, CHANCTX_STA_CSA);
14827 	ieee80211_hw_set(hw, QUEUE_CONTROL);
14828 	ieee80211_hw_set(hw, SUPPORTS_TX_FRAG);
14829 	ieee80211_hw_set(hw, REPORTS_LOW_ACK);
14830 	ieee80211_hw_set(hw, NO_VIRTUAL_MONITOR);
14831 
14832 	if (test_bit(WMI_TLV_SERVICE_ETH_OFFLOAD, ar->wmi->wmi_ab->svc_map)) {
14833 		ieee80211_hw_set(hw, SUPPORTS_TX_ENCAP_OFFLOAD);
14834 		ieee80211_hw_set(hw, SUPPORTS_RX_DECAP_OFFLOAD);
14835 	}
14836 
14837 	if (cap->nss_ratio_enabled)
14838 		ieee80211_hw_set(hw, SUPPORTS_VHT_EXT_NSS_BW);
14839 
14840 	if ((ht_cap & WMI_HT_CAP_ENABLED) || is_6ghz) {
14841 		ieee80211_hw_set(hw, AMPDU_AGGREGATION);
14842 		ieee80211_hw_set(hw, TX_AMPDU_SETUP_IN_HW);
14843 		ieee80211_hw_set(hw, SUPPORTS_REORDERING_BUFFER);
14844 		ieee80211_hw_set(hw, SUPPORTS_AMSDU_IN_AMPDU);
14845 		ieee80211_hw_set(hw, USES_RSS);
14846 	}
14847 
14848 	wiphy->features |= NL80211_FEATURE_STATIC_SMPS;
14849 	wiphy->flags |= WIPHY_FLAG_IBSS_RSN;
14850 
14851 	/* TODO: Check if HT capability advertised from firmware is different
14852 	 * for each band for a dual band capable radio. It will be tricky to
14853 	 * handle it when the ht capability different for each band.
14854 	 */
14855 	if (ht_cap & WMI_HT_CAP_DYNAMIC_SMPS ||
14856 	    (is_6ghz && ab->hw_params->supports_dynamic_smps_6ghz))
14857 		wiphy->features |= NL80211_FEATURE_DYNAMIC_SMPS;
14858 
14859 	wiphy->max_scan_ssids = WLAN_SCAN_PARAMS_MAX_SSID;
14860 	wiphy->max_scan_ie_len = WLAN_SCAN_PARAMS_MAX_IE_LEN;
14861 
14862 	hw->max_listen_interval = ATH12K_MAX_HW_LISTEN_INTERVAL;
14863 
14864 	wiphy->flags |= WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL;
14865 	wiphy->flags |= WIPHY_FLAG_HAS_CHANNEL_SWITCH;
14866 	wiphy->max_remain_on_channel_duration = 5000;
14867 
14868 	wiphy->flags |= WIPHY_FLAG_AP_UAPSD;
14869 	wiphy->features |= NL80211_FEATURE_AP_MODE_CHAN_WIDTH_CHANGE |
14870 				   NL80211_FEATURE_AP_SCAN;
14871 
14872 	wiphy->features |= NL80211_FEATURE_TX_POWER_INSERTION;
14873 
14874 	/* MLO is not yet supported so disable Wireless Extensions for now
14875 	 * to make sure ath12k users don't use it. This flag can be removed
14876 	 * once WIPHY_FLAG_SUPPORTS_MLO is enabled.
14877 	 */
14878 	wiphy->flags |= WIPHY_FLAG_DISABLE_WEXT;
14879 
14880 	/* Copy over MLO related capabilities received from
14881 	 * WMI_SERVICE_READY_EXT2_EVENT if single_chip_mlo_supp is set.
14882 	 */
14883 	if (ab->ag->mlo_capable) {
14884 		ath12k_iftypes_ext_capa[2].eml_capabilities = cap->eml_cap;
14885 		ath12k_iftypes_ext_capa[2].mld_capa_and_ops = cap->mld_cap;
14886 		wiphy->flags |= WIPHY_FLAG_SUPPORTS_MLO;
14887 
14888 		ieee80211_hw_set(hw, MLO_MCAST_MULTI_LINK_TX);
14889 	}
14890 
14891 	hw->queues = ATH12K_HW_MAX_QUEUES;
14892 	wiphy->tx_queue_len = ATH12K_QUEUE_LEN;
14893 	hw->offchannel_tx_hw_queue = ATH12K_HW_MAX_QUEUES - 1;
14894 	hw->max_rx_aggregation_subframes = IEEE80211_MAX_AMPDU_BUF_EHT;
14895 
14896 	hw->vif_data_size = sizeof(struct ath12k_vif);
14897 	hw->sta_data_size = sizeof(struct ath12k_sta);
14898 	hw->extra_tx_headroom = ab->hw_params->iova_mask;
14899 
14900 	wiphy_ext_feature_set(wiphy, NL80211_EXT_FEATURE_CQM_RSSI_LIST);
14901 	wiphy_ext_feature_set(wiphy, NL80211_EXT_FEATURE_STA_TX_PWR);
14902 	wiphy_ext_feature_set(wiphy, NL80211_EXT_FEATURE_ACK_SIGNAL_SUPPORT);
14903 	if (test_bit(WMI_TLV_SERVICE_BSS_COLOR_OFFLOAD,
14904 		     ab->wmi_ab.svc_map)) {
14905 		wiphy_ext_feature_set(wiphy, NL80211_EXT_FEATURE_BSS_COLOR);
14906 		ieee80211_hw_set(hw, DETECTS_COLOR_COLLISION);
14907 	}
14908 
14909 	wiphy->cipher_suites = cipher_suites;
14910 	wiphy->n_cipher_suites = ARRAY_SIZE(cipher_suites);
14911 
14912 	wiphy->iftype_ext_capab = ath12k_iftypes_ext_capa;
14913 	wiphy->num_iftype_ext_capab = ARRAY_SIZE(ath12k_iftypes_ext_capa);
14914 
14915 	wiphy->mbssid_max_interfaces = mbssid_max_interfaces;
14916 	wiphy->ema_max_profile_periodicity = TARGET_EMA_MAX_PROFILE_PERIOD;
14917 	ieee80211_hw_set(hw, SUPPORTS_MULTI_BSSID);
14918 
14919 	if (is_6ghz) {
14920 		wiphy_ext_feature_set(wiphy,
14921 				      NL80211_EXT_FEATURE_FILS_DISCOVERY);
14922 		wiphy_ext_feature_set(wiphy,
14923 				      NL80211_EXT_FEATURE_UNSOL_BCAST_PROBE_RESP);
14924 	}
14925 
14926 	wiphy_ext_feature_set(wiphy, NL80211_EXT_FEATURE_PUNCT);
14927 	if (test_bit(WMI_TLV_SERVICE_BEACON_PROTECTION_SUPPORT, ab->wmi_ab.svc_map))
14928 		wiphy_ext_feature_set(hw->wiphy, NL80211_EXT_FEATURE_BEACON_PROTECTION);
14929 
14930 	ath12k_reg_init(hw);
14931 
14932 	if (!is_raw_mode) {
14933 		hw->netdev_features = NETIF_F_HW_CSUM;
14934 		ieee80211_hw_set(hw, SW_CRYPTO_CONTROL);
14935 		ieee80211_hw_set(hw, SUPPORT_FAST_XMIT);
14936 	}
14937 
14938 	if (test_bit(WMI_TLV_SERVICE_NLO, ar->wmi->wmi_ab->svc_map)) {
14939 		wiphy->max_sched_scan_ssids = WMI_PNO_MAX_SUPP_NETWORKS;
14940 		wiphy->max_match_sets = WMI_PNO_MAX_SUPP_NETWORKS;
14941 		wiphy->max_sched_scan_ie_len = WMI_PNO_MAX_IE_LENGTH;
14942 		wiphy->max_sched_scan_plans = WMI_PNO_MAX_SCHED_SCAN_PLANS;
14943 		wiphy->max_sched_scan_plan_interval =
14944 					WMI_PNO_MAX_SCHED_SCAN_PLAN_INT;
14945 		wiphy->max_sched_scan_plan_iterations =
14946 					WMI_PNO_MAX_SCHED_SCAN_PLAN_ITRNS;
14947 		wiphy->features |= NL80211_FEATURE_ND_RANDOM_MAC_ADDR;
14948 	}
14949 
14950 	ret = ath12k_wow_init(ar);
14951 	if (ret) {
14952 		ath12k_warn(ar->ab, "failed to init wow: %d\n", ret);
14953 		goto err_cleanup_if_combs;
14954 	}
14955 
14956 	/* Boot-time regulatory updates have already been processed.
14957 	 * Mark them as complete now, because after registration,
14958 	 * cfg80211 will notify us again if there are any pending hints.
14959 	 * We need to wait for those hints to be processed, so it's
14960 	 * important to mark the boot-time updates as complete before
14961 	 * proceeding with registration.
14962 	 */
14963 	for_each_ar(ah, ar, i)
14964 		complete_all(&ar->regd_update_completed);
14965 
14966 	ret = ieee80211_register_hw(hw);
14967 	if (ret) {
14968 		ath12k_err(ab, "ieee80211 registration failed: %d\n", ret);
14969 		goto err_cleanup_if_combs;
14970 	}
14971 
14972 	if (is_monitor_disable)
14973 		/* There's a race between calling ieee80211_register_hw()
14974 		 * and here where the monitor mode is enabled for a little
14975 		 * while. But that time is so short and in practice it doesn't make
14976 		 * a difference in real life.
14977 		 */
14978 		wiphy->interface_modes &= ~BIT(NL80211_IFTYPE_MONITOR);
14979 
14980 	for_each_ar(ah, ar, i) {
14981 		/* Apply the regd received during initialization */
14982 		ret = ath12k_regd_update(ar, true);
14983 		if (ret) {
14984 			ath12k_err(ar->ab, "ath12k regd update failed: %d\n", ret);
14985 			goto err_unregister_hw;
14986 		}
14987 
14988 		if (ar->ab->hw_params->current_cc_support && ab->new_alpha2[0]) {
14989 			struct wmi_set_current_country_arg current_cc = {};
14990 
14991 			memcpy(&current_cc.alpha2, ab->new_alpha2, 2);
14992 			memcpy(&ar->alpha2, ab->new_alpha2, 2);
14993 
14994 			reinit_completion(&ar->regd_update_completed);
14995 
14996 			ret = ath12k_wmi_send_set_current_country_cmd(ar, &current_cc);
14997 			if (ret)
14998 				ath12k_warn(ar->ab,
14999 					    "failed set cc code for mac register: %d\n",
15000 					    ret);
15001 		}
15002 
15003 		ath12k_fw_stats_init(ar);
15004 		ath12k_debugfs_register(ar);
15005 	}
15006 
15007 	return 0;
15008 
15009 err_unregister_hw:
15010 	for_each_ar(ah, ar, i)
15011 		ath12k_debugfs_unregister(ar);
15012 
15013 	ieee80211_unregister_hw(hw);
15014 
15015 err_cleanup_if_combs:
15016 	ath12k_mac_cleanup_iface_combinations(ah);
15017 
15018 err_complete_cleanup_unregister:
15019 	i = ah->num_radio;
15020 
15021 err_cleanup_unregister:
15022 	for (j = 0; j < i; j++) {
15023 		ar = ath12k_ah_to_ar(ah, j);
15024 		ath12k_mac_cleanup_unregister(ar);
15025 	}
15026 
15027 	SET_IEEE80211_DEV(hw, NULL);
15028 
15029 	return ret;
15030 }
15031 
15032 static void ath12k_mac_setup(struct ath12k *ar)
15033 {
15034 	struct ath12k_base *ab = ar->ab;
15035 	struct ath12k_pdev *pdev = ar->pdev;
15036 	u8 pdev_idx = ar->pdev_idx;
15037 
15038 	ar->lmac_id = ath12k_hw_get_mac_from_pdev_id(ab->hw_params, pdev_idx);
15039 
15040 	ar->wmi = &ab->wmi_ab.wmi[pdev_idx];
15041 	/* FIXME: wmi[0] is already initialized during attach,
15042 	 * Should we do this again?
15043 	 */
15044 	ath12k_wmi_pdev_attach(ab, pdev_idx);
15045 
15046 	ar->cfg_tx_chainmask = pdev->cap.tx_chain_mask;
15047 	ar->cfg_rx_chainmask = pdev->cap.rx_chain_mask;
15048 	ar->num_tx_chains = hweight32(pdev->cap.tx_chain_mask);
15049 	ar->num_rx_chains = hweight32(pdev->cap.rx_chain_mask);
15050 	ar->scan.arvif = NULL;
15051 	ar->vdev_id_11d_scan = ATH12K_11D_INVALID_VDEV_ID;
15052 
15053 	spin_lock_init(&ar->data_lock);
15054 	spin_lock_init(&ar->dp.ppdu_list_lock);
15055 	INIT_LIST_HEAD(&ar->arvifs);
15056 	INIT_LIST_HEAD(&ar->dp.ppdu_stats_info);
15057 
15058 	init_completion(&ar->vdev_setup_done);
15059 	init_completion(&ar->vdev_delete_done);
15060 	init_completion(&ar->peer_assoc_done);
15061 	init_completion(&ar->peer_delete_done);
15062 	init_completion(&ar->install_key_done);
15063 	init_completion(&ar->bss_survey_done);
15064 	init_completion(&ar->scan.started);
15065 	init_completion(&ar->scan.completed);
15066 	init_completion(&ar->scan.on_channel);
15067 	init_completion(&ar->mlo_setup_done);
15068 	init_completion(&ar->completed_11d_scan);
15069 	init_completion(&ar->regd_update_completed);
15070 	init_completion(&ar->thermal.wmi_sync);
15071 	mutex_init(&ar->thermal.lock);
15072 
15073 	ar->thermal.temperature = 0;
15074 	ar->thermal.hwmon_dev = NULL;
15075 
15076 	INIT_DELAYED_WORK(&ar->scan.timeout, ath12k_scan_timeout_work);
15077 	wiphy_work_init(&ar->scan.vdev_clean_wk, ath12k_scan_vdev_clean_work);
15078 	INIT_WORK(&ar->regd_channel_update_work, ath12k_regd_update_chan_list_work);
15079 	INIT_LIST_HEAD(&ar->regd_channel_update_queue);
15080 	INIT_WORK(&ar->regd_update_work, ath12k_regd_update_work);
15081 
15082 	wiphy_work_init(&ar->wmi_mgmt_tx_work, ath12k_mgmt_over_wmi_tx_work);
15083 	skb_queue_head_init(&ar->wmi_mgmt_tx_queue);
15084 
15085 	ar->monitor_vdev_id = -1;
15086 	ar->monitor_vdev_created = false;
15087 	ar->monitor_started = false;
15088 }
15089 
15090 static int __ath12k_mac_mlo_setup(struct ath12k *ar)
15091 {
15092 	u8 num_link = 0, partner_link_id[ATH12K_GROUP_MAX_RADIO] = {};
15093 	struct ath12k_base *partner_ab, *ab = ar->ab;
15094 	struct ath12k_hw_group *ag = ab->ag;
15095 	struct wmi_mlo_setup_arg mlo = {};
15096 	struct ath12k_pdev *pdev;
15097 	unsigned long time_left;
15098 	int i, j, ret;
15099 
15100 	lockdep_assert_held(&ag->mutex);
15101 
15102 	reinit_completion(&ar->mlo_setup_done);
15103 
15104 	for (i = 0; i < ag->num_devices; i++) {
15105 		partner_ab = ag->ab[i];
15106 
15107 		for (j = 0; j < partner_ab->num_radios; j++) {
15108 			pdev = &partner_ab->pdevs[j];
15109 
15110 			/* Avoid the self link */
15111 			if (ar == pdev->ar)
15112 				continue;
15113 
15114 			partner_link_id[num_link] = pdev->hw_link_id;
15115 			num_link++;
15116 
15117 			ath12k_dbg(ab, ATH12K_DBG_MAC, "device %d pdev %d hw_link_id %d num_link %d\n",
15118 				   i, j, pdev->hw_link_id, num_link);
15119 		}
15120 	}
15121 
15122 	if (num_link == 0)
15123 		return 0;
15124 
15125 	mlo.group_id = cpu_to_le32(ag->id);
15126 	mlo.partner_link_id = partner_link_id;
15127 	mlo.num_partner_links = num_link;
15128 	ar->mlo_setup_status = 0;
15129 
15130 	ath12k_dbg(ab, ATH12K_DBG_MAC, "group id %d num_link %d\n", ag->id, num_link);
15131 
15132 	ret = ath12k_wmi_mlo_setup(ar, &mlo);
15133 	if (ret) {
15134 		ath12k_err(ab, "failed to send  setup MLO WMI command for pdev %d: %d\n",
15135 			   ar->pdev_idx, ret);
15136 		return ret;
15137 	}
15138 
15139 	time_left = wait_for_completion_timeout(&ar->mlo_setup_done,
15140 						WMI_MLO_CMD_TIMEOUT_HZ);
15141 
15142 	if (!time_left || ar->mlo_setup_status)
15143 		return ar->mlo_setup_status ? : -ETIMEDOUT;
15144 
15145 	ath12k_dbg(ab, ATH12K_DBG_MAC, "mlo setup done for pdev %d\n", ar->pdev_idx);
15146 
15147 	return 0;
15148 }
15149 
15150 static int __ath12k_mac_mlo_teardown(struct ath12k *ar)
15151 {
15152 	struct ath12k_base *ab = ar->ab;
15153 	int ret;
15154 	u8 num_link;
15155 
15156 	if (test_bit(ATH12K_FLAG_RECOVERY, &ab->dev_flags))
15157 		return 0;
15158 
15159 	num_link = ath12k_get_num_partner_link(ar);
15160 
15161 	if (num_link == 0)
15162 		return 0;
15163 
15164 	ret = ath12k_wmi_mlo_teardown(ar);
15165 	if (ret) {
15166 		ath12k_warn(ab, "failed to send MLO teardown WMI command for pdev %d: %d\n",
15167 			    ar->pdev_idx, ret);
15168 		return ret;
15169 	}
15170 
15171 	ath12k_dbg(ab, ATH12K_DBG_MAC, "mlo teardown for pdev %d\n", ar->pdev_idx);
15172 
15173 	return 0;
15174 }
15175 
15176 int ath12k_mac_mlo_setup(struct ath12k_hw_group *ag)
15177 {
15178 	struct ath12k_hw *ah;
15179 	struct ath12k *ar;
15180 	int ret;
15181 	int i, j;
15182 
15183 	for (i = 0; i < ag->num_hw; i++) {
15184 		ah = ag->ah[i];
15185 		if (!ah)
15186 			continue;
15187 
15188 		for_each_ar(ah, ar, j) {
15189 			ar = &ah->radio[j];
15190 			ret = __ath12k_mac_mlo_setup(ar);
15191 			if (ret) {
15192 				ath12k_err(ar->ab, "failed to setup MLO: %d\n", ret);
15193 				goto err_setup;
15194 			}
15195 		}
15196 	}
15197 
15198 	return 0;
15199 
15200 err_setup:
15201 	for (i = i - 1; i >= 0; i--) {
15202 		ah = ag->ah[i];
15203 		if (!ah)
15204 			continue;
15205 
15206 		for (j = j - 1; j >= 0; j--) {
15207 			ar = &ah->radio[j];
15208 			if (!ar)
15209 				continue;
15210 
15211 			__ath12k_mac_mlo_teardown(ar);
15212 		}
15213 	}
15214 
15215 	return ret;
15216 }
15217 
15218 void ath12k_mac_mlo_teardown(struct ath12k_hw_group *ag)
15219 {
15220 	struct ath12k_hw *ah;
15221 	struct ath12k *ar;
15222 	int ret, i, j;
15223 
15224 	for (i = 0; i < ag->num_hw; i++) {
15225 		ah = ag->ah[i];
15226 		if (!ah)
15227 			continue;
15228 
15229 		for_each_ar(ah, ar, j) {
15230 			ar = &ah->radio[j];
15231 			ret = __ath12k_mac_mlo_teardown(ar);
15232 			if (ret) {
15233 				ath12k_err(ar->ab, "failed to teardown MLO: %d\n", ret);
15234 				break;
15235 			}
15236 		}
15237 	}
15238 }
15239 
15240 int ath12k_mac_register(struct ath12k_hw_group *ag)
15241 {
15242 	struct ath12k_hw *ah;
15243 	int i;
15244 	int ret;
15245 
15246 	for (i = 0; i < ag->num_hw; i++) {
15247 		ah = ath12k_ag_to_ah(ag, i);
15248 
15249 		ret = ath12k_mac_hw_register(ah);
15250 		if (ret)
15251 			goto err;
15252 	}
15253 
15254 	return 0;
15255 
15256 err:
15257 	for (i = i - 1; i >= 0; i--) {
15258 		ah = ath12k_ag_to_ah(ag, i);
15259 		if (!ah)
15260 			continue;
15261 
15262 		ath12k_mac_hw_unregister(ah);
15263 	}
15264 
15265 	return ret;
15266 }
15267 
15268 void ath12k_mac_unregister(struct ath12k_hw_group *ag)
15269 {
15270 	struct ath12k_hw *ah;
15271 	int i;
15272 
15273 	for (i = ag->num_hw - 1; i >= 0; i--) {
15274 		ah = ath12k_ag_to_ah(ag, i);
15275 		if (!ah)
15276 			continue;
15277 
15278 		ath12k_mac_hw_unregister(ah);
15279 	}
15280 }
15281 
15282 static void ath12k_mac_hw_destroy(struct ath12k_hw *ah)
15283 {
15284 	ieee80211_free_hw(ah->hw);
15285 }
15286 
15287 static struct ath12k_hw *ath12k_mac_hw_allocate(struct ath12k_hw_group *ag,
15288 						struct ath12k_pdev_map *pdev_map,
15289 						u8 num_pdev_map)
15290 {
15291 	struct ieee80211_hw *hw;
15292 	struct ath12k *ar;
15293 	struct ath12k_base *ab;
15294 	struct ath12k_pdev *pdev;
15295 	struct ath12k_hw *ah;
15296 	int i;
15297 	u8 pdev_idx;
15298 
15299 	hw = ieee80211_alloc_hw(struct_size(ah, radio, num_pdev_map),
15300 				pdev_map->ab->ath12k_ops);
15301 	if (!hw)
15302 		return NULL;
15303 
15304 	ah = ath12k_hw_to_ah(hw);
15305 	ah->hw = hw;
15306 	ah->num_radio = num_pdev_map;
15307 
15308 	mutex_init(&ah->hw_mutex);
15309 
15310 	spin_lock_init(&ah->dp_hw.peer_lock);
15311 	INIT_LIST_HEAD(&ah->dp_hw.dp_peers_list);
15312 
15313 	for (i = 0; i < num_pdev_map; i++) {
15314 		ab = pdev_map[i].ab;
15315 		pdev_idx = pdev_map[i].pdev_idx;
15316 		pdev = &ab->pdevs[pdev_idx];
15317 
15318 		ar = ath12k_ah_to_ar(ah, i);
15319 		ar->ah = ah;
15320 		ar->ab = ab;
15321 		ar->hw_link_id = pdev->hw_link_id;
15322 		ar->pdev = pdev;
15323 		ar->pdev_idx = pdev_idx;
15324 		ar->radio_idx = i;
15325 		pdev->ar = ar;
15326 
15327 		ag->hw_links[ar->hw_link_id].device_id = ab->device_id;
15328 		ag->hw_links[ar->hw_link_id].pdev_idx = pdev_idx;
15329 
15330 		ath12k_mac_setup(ar);
15331 		ath12k_dp_pdev_pre_alloc(ar);
15332 	}
15333 
15334 	return ah;
15335 }
15336 
15337 void ath12k_mac_destroy(struct ath12k_hw_group *ag)
15338 {
15339 	struct ath12k_pdev *pdev;
15340 	struct ath12k_base *ab = ag->ab[0];
15341 	int i, j;
15342 	struct ath12k_hw *ah;
15343 
15344 	for (i = 0; i < ag->num_devices; i++) {
15345 		ab = ag->ab[i];
15346 		if (!ab)
15347 			continue;
15348 
15349 		for (j = 0; j < ab->num_radios; j++) {
15350 			pdev = &ab->pdevs[j];
15351 			if (!pdev->ar)
15352 				continue;
15353 			pdev->ar = NULL;
15354 		}
15355 	}
15356 
15357 	for (i = 0; i < ag->num_hw; i++) {
15358 		ah = ath12k_ag_to_ah(ag, i);
15359 		if (!ah)
15360 			continue;
15361 
15362 		ath12k_mac_hw_destroy(ah);
15363 		ath12k_ag_set_ah(ag, i, NULL);
15364 	}
15365 }
15366 
15367 static void ath12k_mac_set_device_defaults(struct ath12k_base *ab)
15368 {
15369 	int total_vdev;
15370 
15371 	/* Initialize channel counters frequency value in hertz */
15372 	ab->cc_freq_hz = 320000;
15373 	total_vdev = ab->num_radios * TARGET_NUM_VDEVS(ab);
15374 	ab->free_vdev_map = (1LL << total_vdev) - 1;
15375 }
15376 
15377 int ath12k_mac_allocate(struct ath12k_hw_group *ag)
15378 {
15379 	struct ath12k_pdev_map pdev_map[ATH12K_GROUP_MAX_RADIO];
15380 	int mac_id, device_id, total_radio, num_hw;
15381 	struct ath12k_base *ab;
15382 	struct ath12k_hw *ah;
15383 	int ret, i, j;
15384 	u8 radio_per_hw;
15385 
15386 	total_radio = 0;
15387 	for (i = 0; i < ag->num_devices; i++) {
15388 		ab = ag->ab[i];
15389 		if (!ab)
15390 			continue;
15391 
15392 		ath12k_mac_set_device_defaults(ab);
15393 		total_radio += ab->num_radios;
15394 	}
15395 
15396 	if (!total_radio)
15397 		return -EINVAL;
15398 
15399 	if (WARN_ON(total_radio > ATH12K_GROUP_MAX_RADIO))
15400 		return -ENOSPC;
15401 
15402 	/* All pdev get combined and register as single wiphy based on
15403 	 * hardware group which participate in multi-link operation else
15404 	 * each pdev get register separately.
15405 	 */
15406 	if (ag->mlo_capable)
15407 		radio_per_hw = total_radio;
15408 	else
15409 		radio_per_hw = 1;
15410 
15411 	num_hw = total_radio / radio_per_hw;
15412 
15413 	ag->num_hw = 0;
15414 	device_id = 0;
15415 	mac_id = 0;
15416 	for (i = 0; i < num_hw; i++) {
15417 		for (j = 0; j < radio_per_hw; j++) {
15418 			if (device_id >= ag->num_devices || !ag->ab[device_id]) {
15419 				ret = -ENOSPC;
15420 				goto err;
15421 			}
15422 
15423 			ab = ag->ab[device_id];
15424 			pdev_map[j].ab = ab;
15425 			pdev_map[j].pdev_idx = mac_id;
15426 			mac_id++;
15427 
15428 			/* If mac_id falls beyond the current device MACs then
15429 			 * move to next device
15430 			 */
15431 			if (mac_id >= ab->num_radios) {
15432 				mac_id = 0;
15433 				device_id++;
15434 			}
15435 		}
15436 
15437 		ab = pdev_map->ab;
15438 
15439 		ah = ath12k_mac_hw_allocate(ag, pdev_map, radio_per_hw);
15440 		if (!ah) {
15441 			ath12k_warn(ab, "failed to allocate mac80211 hw device for hw_idx %d\n",
15442 				    i);
15443 			ret = -ENOMEM;
15444 			goto err;
15445 		}
15446 
15447 		ah->dev = ab->dev;
15448 
15449 		ag->ah[i] = ah;
15450 		ag->num_hw++;
15451 	}
15452 
15453 	return 0;
15454 
15455 err:
15456 	for (i = i - 1; i >= 0; i--) {
15457 		ah = ath12k_ag_to_ah(ag, i);
15458 		if (!ah)
15459 			continue;
15460 
15461 		ath12k_mac_hw_destroy(ah);
15462 		ath12k_ag_set_ah(ag, i, NULL);
15463 	}
15464 
15465 	return ret;
15466 }
15467 
15468 int ath12k_mac_vif_set_keepalive(struct ath12k_link_vif *arvif,
15469 				 enum wmi_sta_keepalive_method method,
15470 				 u32 interval)
15471 {
15472 	struct wmi_sta_keepalive_arg arg = {};
15473 	struct ath12k *ar = arvif->ar;
15474 	int ret;
15475 
15476 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
15477 
15478 	if (arvif->ahvif->vdev_type != WMI_VDEV_TYPE_STA)
15479 		return 0;
15480 
15481 	if (!test_bit(WMI_TLV_SERVICE_STA_KEEP_ALIVE, ar->ab->wmi_ab.svc_map))
15482 		return 0;
15483 
15484 	arg.vdev_id = arvif->vdev_id;
15485 	arg.enabled = 1;
15486 	arg.method = method;
15487 	arg.interval = interval;
15488 
15489 	ret = ath12k_wmi_sta_keepalive(ar, &arg);
15490 	if (ret) {
15491 		ath12k_warn(ar->ab, "failed to set keepalive on vdev %i: %d\n",
15492 			    arvif->vdev_id, ret);
15493 		return ret;
15494 	}
15495 
15496 	return 0;
15497 }
15498