xref: /linux/drivers/net/wireless/ath/ath12k/mac.c (revision 945a670236a7effaba57ed5ccf5dabf673002ef3)
1 // SPDX-License-Identifier: BSD-3-Clause-Clear
2 /*
3  * Copyright (c) 2018-2021 The Linux Foundation. All rights reserved.
4  * Copyright (c) Qualcomm Technologies, Inc. and/or its subsidiaries.
5  */
6 
7 #include <net/mac80211.h>
8 #include <net/cfg80211.h>
9 #include <linux/etherdevice.h>
10 
11 #include "mac.h"
12 #include "core.h"
13 #include "debug.h"
14 #include "wmi.h"
15 #include "hw.h"
16 #include "dp_tx.h"
17 #include "dp_rx.h"
18 #include "testmode.h"
19 #include "peer.h"
20 #include "debugfs.h"
21 #include "hif.h"
22 #include "wow.h"
23 #include "debugfs_sta.h"
24 #include "dp.h"
25 #include "dp_cmn.h"
26 
27 #define CHAN2G(_channel, _freq, _flags) { \
28 	.band                   = NL80211_BAND_2GHZ, \
29 	.hw_value               = (_channel), \
30 	.center_freq            = (_freq), \
31 	.flags                  = (_flags), \
32 	.max_antenna_gain       = 0, \
33 	.max_power              = 30, \
34 }
35 
36 #define CHAN5G(_channel, _freq, _flags) { \
37 	.band                   = NL80211_BAND_5GHZ, \
38 	.hw_value               = (_channel), \
39 	.center_freq            = (_freq), \
40 	.flags                  = (_flags), \
41 	.max_antenna_gain       = 0, \
42 	.max_power              = 30, \
43 }
44 
45 #define CHAN6G(_channel, _freq, _flags) { \
46 	.band                   = NL80211_BAND_6GHZ, \
47 	.hw_value               = (_channel), \
48 	.center_freq            = (_freq), \
49 	.flags                  = (_flags), \
50 	.max_antenna_gain       = 0, \
51 	.max_power              = 30, \
52 }
53 
54 #define ATH12K_5_9_GHZ_MIN_FREQ 5845
55 #define ATH12K_5_9_GHZ_MAX_FREQ 5885
56 
57 static const struct ieee80211_channel ath12k_2ghz_channels[] = {
58 	CHAN2G(1, 2412, 0),
59 	CHAN2G(2, 2417, 0),
60 	CHAN2G(3, 2422, 0),
61 	CHAN2G(4, 2427, 0),
62 	CHAN2G(5, 2432, 0),
63 	CHAN2G(6, 2437, 0),
64 	CHAN2G(7, 2442, 0),
65 	CHAN2G(8, 2447, 0),
66 	CHAN2G(9, 2452, 0),
67 	CHAN2G(10, 2457, 0),
68 	CHAN2G(11, 2462, 0),
69 	CHAN2G(12, 2467, 0),
70 	CHAN2G(13, 2472, 0),
71 	CHAN2G(14, 2484, 0),
72 };
73 
74 static const struct ieee80211_channel ath12k_5ghz_channels[] = {
75 	CHAN5G(36, 5180, 0),
76 	CHAN5G(40, 5200, 0),
77 	CHAN5G(44, 5220, 0),
78 	CHAN5G(48, 5240, 0),
79 	CHAN5G(52, 5260, 0),
80 	CHAN5G(56, 5280, 0),
81 	CHAN5G(60, 5300, 0),
82 	CHAN5G(64, 5320, 0),
83 	CHAN5G(100, 5500, 0),
84 	CHAN5G(104, 5520, 0),
85 	CHAN5G(108, 5540, 0),
86 	CHAN5G(112, 5560, 0),
87 	CHAN5G(116, 5580, 0),
88 	CHAN5G(120, 5600, 0),
89 	CHAN5G(124, 5620, 0),
90 	CHAN5G(128, 5640, 0),
91 	CHAN5G(132, 5660, 0),
92 	CHAN5G(136, 5680, 0),
93 	CHAN5G(140, 5700, 0),
94 	CHAN5G(144, 5720, 0),
95 	CHAN5G(149, 5745, 0),
96 	CHAN5G(153, 5765, 0),
97 	CHAN5G(157, 5785, 0),
98 	CHAN5G(161, 5805, 0),
99 	CHAN5G(165, 5825, 0),
100 	CHAN5G(169, 5845, 0),
101 	CHAN5G(173, 5865, 0),
102 	CHAN5G(177, 5885, 0),
103 };
104 
105 static const struct ieee80211_channel ath12k_6ghz_channels[] = {
106 	/* Operating Class 136 */
107 	CHAN6G(2, 5935, 0),
108 
109 	/* Operating Classes 131-135 */
110 	CHAN6G(1, 5955, 0),
111 	CHAN6G(5, 5975, 0),
112 	CHAN6G(9, 5995, 0),
113 	CHAN6G(13, 6015, 0),
114 	CHAN6G(17, 6035, 0),
115 	CHAN6G(21, 6055, 0),
116 	CHAN6G(25, 6075, 0),
117 	CHAN6G(29, 6095, 0),
118 	CHAN6G(33, 6115, 0),
119 	CHAN6G(37, 6135, 0),
120 	CHAN6G(41, 6155, 0),
121 	CHAN6G(45, 6175, 0),
122 	CHAN6G(49, 6195, 0),
123 	CHAN6G(53, 6215, 0),
124 	CHAN6G(57, 6235, 0),
125 	CHAN6G(61, 6255, 0),
126 	CHAN6G(65, 6275, 0),
127 	CHAN6G(69, 6295, 0),
128 	CHAN6G(73, 6315, 0),
129 	CHAN6G(77, 6335, 0),
130 	CHAN6G(81, 6355, 0),
131 	CHAN6G(85, 6375, 0),
132 	CHAN6G(89, 6395, 0),
133 	CHAN6G(93, 6415, 0),
134 	CHAN6G(97, 6435, 0),
135 	CHAN6G(101, 6455, 0),
136 	CHAN6G(105, 6475, 0),
137 	CHAN6G(109, 6495, 0),
138 	CHAN6G(113, 6515, 0),
139 	CHAN6G(117, 6535, 0),
140 	CHAN6G(121, 6555, 0),
141 	CHAN6G(125, 6575, 0),
142 	CHAN6G(129, 6595, 0),
143 	CHAN6G(133, 6615, 0),
144 	CHAN6G(137, 6635, 0),
145 	CHAN6G(141, 6655, 0),
146 	CHAN6G(145, 6675, 0),
147 	CHAN6G(149, 6695, 0),
148 	CHAN6G(153, 6715, 0),
149 	CHAN6G(157, 6735, 0),
150 	CHAN6G(161, 6755, 0),
151 	CHAN6G(165, 6775, 0),
152 	CHAN6G(169, 6795, 0),
153 	CHAN6G(173, 6815, 0),
154 	CHAN6G(177, 6835, 0),
155 	CHAN6G(181, 6855, 0),
156 	CHAN6G(185, 6875, 0),
157 	CHAN6G(189, 6895, 0),
158 	CHAN6G(193, 6915, 0),
159 	CHAN6G(197, 6935, 0),
160 	CHAN6G(201, 6955, 0),
161 	CHAN6G(205, 6975, 0),
162 	CHAN6G(209, 6995, 0),
163 	CHAN6G(213, 7015, 0),
164 	CHAN6G(217, 7035, 0),
165 	CHAN6G(221, 7055, 0),
166 	CHAN6G(225, 7075, 0),
167 	CHAN6G(229, 7095, 0),
168 	CHAN6G(233, 7115, 0),
169 };
170 
171 #define ATH12K_MAC_RATE_A_M(bps, code) \
172 	{ .bitrate = (bps), .hw_value = (code),\
173 	  .flags = IEEE80211_RATE_MANDATORY_A }
174 
175 #define ATH12K_MAC_RATE_B(bps, code, code_short) \
176 	{ .bitrate = (bps), .hw_value = (code), .hw_value_short = (code_short),\
177 	  .flags = IEEE80211_RATE_SHORT_PREAMBLE }
178 
179 static struct ieee80211_rate ath12k_legacy_rates[] = {
180 	{ .bitrate = 10,
181 	  .hw_value = ATH12K_HW_RATE_CCK_LP_1M },
182 	ATH12K_MAC_RATE_B(20, ATH12K_HW_RATE_CCK_LP_2M,
183 			  ATH12K_HW_RATE_CCK_SP_2M),
184 	ATH12K_MAC_RATE_B(55, ATH12K_HW_RATE_CCK_LP_5_5M,
185 			  ATH12K_HW_RATE_CCK_SP_5_5M),
186 	ATH12K_MAC_RATE_B(110, ATH12K_HW_RATE_CCK_LP_11M,
187 			  ATH12K_HW_RATE_CCK_SP_11M),
188 	ATH12K_MAC_RATE_A_M(60, ATH12K_HW_RATE_OFDM_6M),
189 	ATH12K_MAC_RATE_A_M(90, ATH12K_HW_RATE_OFDM_9M),
190 	ATH12K_MAC_RATE_A_M(120, ATH12K_HW_RATE_OFDM_12M),
191 	ATH12K_MAC_RATE_A_M(180, ATH12K_HW_RATE_OFDM_18M),
192 	ATH12K_MAC_RATE_A_M(240, ATH12K_HW_RATE_OFDM_24M),
193 	ATH12K_MAC_RATE_A_M(360, ATH12K_HW_RATE_OFDM_36M),
194 	ATH12K_MAC_RATE_A_M(480, ATH12K_HW_RATE_OFDM_48M),
195 	ATH12K_MAC_RATE_A_M(540, ATH12K_HW_RATE_OFDM_54M),
196 };
197 
198 static const int
199 ath12k_phymodes[NUM_NL80211_BANDS][ATH12K_CHAN_WIDTH_NUM] = {
200 	[NL80211_BAND_2GHZ] = {
201 			[NL80211_CHAN_WIDTH_5] = MODE_UNKNOWN,
202 			[NL80211_CHAN_WIDTH_10] = MODE_UNKNOWN,
203 			[NL80211_CHAN_WIDTH_20_NOHT] = MODE_11BE_EHT20_2G,
204 			[NL80211_CHAN_WIDTH_20] = MODE_11BE_EHT20_2G,
205 			[NL80211_CHAN_WIDTH_40] = MODE_11BE_EHT40_2G,
206 			[NL80211_CHAN_WIDTH_80] = MODE_UNKNOWN,
207 			[NL80211_CHAN_WIDTH_80P80] = MODE_UNKNOWN,
208 			[NL80211_CHAN_WIDTH_160] = MODE_UNKNOWN,
209 			[NL80211_CHAN_WIDTH_320] = MODE_UNKNOWN,
210 	},
211 	[NL80211_BAND_5GHZ] = {
212 			[NL80211_CHAN_WIDTH_5] = MODE_UNKNOWN,
213 			[NL80211_CHAN_WIDTH_10] = MODE_UNKNOWN,
214 			[NL80211_CHAN_WIDTH_20_NOHT] = MODE_11BE_EHT20,
215 			[NL80211_CHAN_WIDTH_20] = MODE_11BE_EHT20,
216 			[NL80211_CHAN_WIDTH_40] = MODE_11BE_EHT40,
217 			[NL80211_CHAN_WIDTH_80] = MODE_11BE_EHT80,
218 			[NL80211_CHAN_WIDTH_160] = MODE_11BE_EHT160,
219 			[NL80211_CHAN_WIDTH_80P80] = MODE_UNKNOWN,
220 			[NL80211_CHAN_WIDTH_320] = MODE_11BE_EHT320,
221 	},
222 	[NL80211_BAND_6GHZ] = {
223 			[NL80211_CHAN_WIDTH_5] = MODE_UNKNOWN,
224 			[NL80211_CHAN_WIDTH_10] = MODE_UNKNOWN,
225 			[NL80211_CHAN_WIDTH_20_NOHT] = MODE_11BE_EHT20,
226 			[NL80211_CHAN_WIDTH_20] = MODE_11BE_EHT20,
227 			[NL80211_CHAN_WIDTH_40] = MODE_11BE_EHT40,
228 			[NL80211_CHAN_WIDTH_80] = MODE_11BE_EHT80,
229 			[NL80211_CHAN_WIDTH_160] = MODE_11BE_EHT160,
230 			[NL80211_CHAN_WIDTH_80P80] = MODE_UNKNOWN,
231 			[NL80211_CHAN_WIDTH_320] = MODE_11BE_EHT320,
232 	},
233 
234 };
235 
236 const struct htt_rx_ring_tlv_filter ath12k_mac_mon_status_filter_default = {
237 	.rx_filter = HTT_RX_FILTER_TLV_FLAGS_MPDU_START |
238 		     HTT_RX_FILTER_TLV_FLAGS_PPDU_END |
239 		     HTT_RX_FILTER_TLV_FLAGS_PPDU_END_STATUS_DONE |
240 		     HTT_RX_FILTER_TLV_FLAGS_PPDU_START_USER_INFO,
241 	.pkt_filter_flags0 = HTT_RX_FP_MGMT_FILTER_FLAGS0,
242 	.pkt_filter_flags1 = HTT_RX_FP_MGMT_FILTER_FLAGS1,
243 	.pkt_filter_flags2 = HTT_RX_FP_CTRL_FILTER_FLASG2,
244 	.pkt_filter_flags3 = HTT_RX_FP_DATA_FILTER_FLASG3 |
245 			     HTT_RX_FP_CTRL_FILTER_FLASG3
246 };
247 
248 #define ATH12K_MAC_FIRST_OFDM_RATE_IDX 4
249 #define ath12k_g_rates ath12k_legacy_rates
250 #define ath12k_g_rates_size (ARRAY_SIZE(ath12k_legacy_rates))
251 #define ath12k_a_rates (ath12k_legacy_rates + 4)
252 #define ath12k_a_rates_size (ARRAY_SIZE(ath12k_legacy_rates) - 4)
253 
254 #define ATH12K_MAC_SCAN_TIMEOUT_MSECS 200 /* in msecs */
255 
256 static const u32 ath12k_smps_map[] = {
257 	[WLAN_HT_CAP_SM_PS_STATIC] = WMI_PEER_SMPS_STATIC,
258 	[WLAN_HT_CAP_SM_PS_DYNAMIC] = WMI_PEER_SMPS_DYNAMIC,
259 	[WLAN_HT_CAP_SM_PS_INVALID] = WMI_PEER_SMPS_PS_NONE,
260 	[WLAN_HT_CAP_SM_PS_DISABLED] = WMI_PEER_SMPS_PS_NONE,
261 };
262 
263 static int ath12k_start_vdev_delay(struct ath12k *ar,
264 				   struct ath12k_link_vif *arvif);
265 static void ath12k_mac_stop(struct ath12k *ar);
266 static int ath12k_mac_vdev_create(struct ath12k *ar, struct ath12k_link_vif *arvif);
267 static int ath12k_mac_vdev_delete(struct ath12k *ar, struct ath12k_link_vif *arvif);
268 
269 static const char *ath12k_mac_phymode_str(enum wmi_phy_mode mode)
270 {
271 	switch (mode) {
272 	case MODE_11A:
273 		return "11a";
274 	case MODE_11G:
275 		return "11g";
276 	case MODE_11B:
277 		return "11b";
278 	case MODE_11GONLY:
279 		return "11gonly";
280 	case MODE_11NA_HT20:
281 		return "11na-ht20";
282 	case MODE_11NG_HT20:
283 		return "11ng-ht20";
284 	case MODE_11NA_HT40:
285 		return "11na-ht40";
286 	case MODE_11NG_HT40:
287 		return "11ng-ht40";
288 	case MODE_11AC_VHT20:
289 		return "11ac-vht20";
290 	case MODE_11AC_VHT40:
291 		return "11ac-vht40";
292 	case MODE_11AC_VHT80:
293 		return "11ac-vht80";
294 	case MODE_11AC_VHT160:
295 		return "11ac-vht160";
296 	case MODE_11AC_VHT80_80:
297 		return "11ac-vht80+80";
298 	case MODE_11AC_VHT20_2G:
299 		return "11ac-vht20-2g";
300 	case MODE_11AC_VHT40_2G:
301 		return "11ac-vht40-2g";
302 	case MODE_11AC_VHT80_2G:
303 		return "11ac-vht80-2g";
304 	case MODE_11AX_HE20:
305 		return "11ax-he20";
306 	case MODE_11AX_HE40:
307 		return "11ax-he40";
308 	case MODE_11AX_HE80:
309 		return "11ax-he80";
310 	case MODE_11AX_HE80_80:
311 		return "11ax-he80+80";
312 	case MODE_11AX_HE160:
313 		return "11ax-he160";
314 	case MODE_11AX_HE20_2G:
315 		return "11ax-he20-2g";
316 	case MODE_11AX_HE40_2G:
317 		return "11ax-he40-2g";
318 	case MODE_11AX_HE80_2G:
319 		return "11ax-he80-2g";
320 	case MODE_11BE_EHT20:
321 		return "11be-eht20";
322 	case MODE_11BE_EHT40:
323 		return "11be-eht40";
324 	case MODE_11BE_EHT80:
325 		return "11be-eht80";
326 	case MODE_11BE_EHT80_80:
327 		return "11be-eht80+80";
328 	case MODE_11BE_EHT160:
329 		return "11be-eht160";
330 	case MODE_11BE_EHT160_160:
331 		return "11be-eht160+160";
332 	case MODE_11BE_EHT320:
333 		return "11be-eht320";
334 	case MODE_11BE_EHT20_2G:
335 		return "11be-eht20-2g";
336 	case MODE_11BE_EHT40_2G:
337 		return "11be-eht40-2g";
338 	case MODE_UNKNOWN:
339 		/* skip */
340 		break;
341 
342 		/* no default handler to allow compiler to check that the
343 		 * enum is fully handled
344 		 */
345 	}
346 
347 	return "<unknown>";
348 }
349 
350 u16 ath12k_mac_he_convert_tones_to_ru_tones(u16 tones)
351 {
352 	switch (tones) {
353 	case 26:
354 		return RU_26;
355 	case 52:
356 		return RU_52;
357 	case 106:
358 		return RU_106;
359 	case 242:
360 		return RU_242;
361 	case 484:
362 		return RU_484;
363 	case 996:
364 		return RU_996;
365 	case (996 * 2):
366 		return RU_2X996;
367 	default:
368 		return RU_26;
369 	}
370 }
371 EXPORT_SYMBOL(ath12k_mac_he_convert_tones_to_ru_tones);
372 
373 enum nl80211_eht_gi ath12k_mac_eht_gi_to_nl80211_eht_gi(u8 sgi)
374 {
375 	switch (sgi) {
376 	case RX_MSDU_START_SGI_0_8_US:
377 		return NL80211_RATE_INFO_EHT_GI_0_8;
378 	case RX_MSDU_START_SGI_1_6_US:
379 		return NL80211_RATE_INFO_EHT_GI_1_6;
380 	case RX_MSDU_START_SGI_3_2_US:
381 		return NL80211_RATE_INFO_EHT_GI_3_2;
382 	default:
383 		return NL80211_RATE_INFO_EHT_GI_0_8;
384 	}
385 }
386 EXPORT_SYMBOL(ath12k_mac_eht_gi_to_nl80211_eht_gi);
387 
388 enum nl80211_eht_ru_alloc ath12k_mac_eht_ru_tones_to_nl80211_eht_ru_alloc(u16 ru_tones)
389 {
390 	switch (ru_tones) {
391 	case 26:
392 		return NL80211_RATE_INFO_EHT_RU_ALLOC_26;
393 	case 52:
394 		return NL80211_RATE_INFO_EHT_RU_ALLOC_52;
395 	case (52 + 26):
396 		return NL80211_RATE_INFO_EHT_RU_ALLOC_52P26;
397 	case 106:
398 		return NL80211_RATE_INFO_EHT_RU_ALLOC_106;
399 	case (106 + 26):
400 		return NL80211_RATE_INFO_EHT_RU_ALLOC_106P26;
401 	case 242:
402 		return NL80211_RATE_INFO_EHT_RU_ALLOC_242;
403 	case 484:
404 		return NL80211_RATE_INFO_EHT_RU_ALLOC_484;
405 	case (484 + 242):
406 		return NL80211_RATE_INFO_EHT_RU_ALLOC_484P242;
407 	case 996:
408 		return NL80211_RATE_INFO_EHT_RU_ALLOC_996;
409 	case (996 + 484):
410 		return NL80211_RATE_INFO_EHT_RU_ALLOC_996P484;
411 	case (996 + 484 + 242):
412 		return NL80211_RATE_INFO_EHT_RU_ALLOC_996P484P242;
413 	case (2 * 996):
414 		return NL80211_RATE_INFO_EHT_RU_ALLOC_2x996;
415 	case (2 * 996 + 484):
416 		return NL80211_RATE_INFO_EHT_RU_ALLOC_2x996P484;
417 	case (3 * 996):
418 		return NL80211_RATE_INFO_EHT_RU_ALLOC_3x996;
419 	case (3 * 996 + 484):
420 		return NL80211_RATE_INFO_EHT_RU_ALLOC_3x996P484;
421 	case (4 * 996):
422 		return NL80211_RATE_INFO_EHT_RU_ALLOC_4x996;
423 	default:
424 		return NL80211_RATE_INFO_EHT_RU_ALLOC_26;
425 	}
426 }
427 EXPORT_SYMBOL(ath12k_mac_eht_ru_tones_to_nl80211_eht_ru_alloc);
428 
429 enum rate_info_bw
430 ath12k_mac_bw_to_mac80211_bw(enum ath12k_supported_bw bw)
431 {
432 	u8 ret = RATE_INFO_BW_20;
433 
434 	switch (bw) {
435 	case ATH12K_BW_20:
436 		ret = RATE_INFO_BW_20;
437 		break;
438 	case ATH12K_BW_40:
439 		ret = RATE_INFO_BW_40;
440 		break;
441 	case ATH12K_BW_80:
442 		ret = RATE_INFO_BW_80;
443 		break;
444 	case ATH12K_BW_160:
445 		ret = RATE_INFO_BW_160;
446 		break;
447 	case ATH12K_BW_320:
448 		ret = RATE_INFO_BW_320;
449 		break;
450 	}
451 
452 	return ret;
453 }
454 EXPORT_SYMBOL(ath12k_mac_bw_to_mac80211_bw);
455 
456 enum ath12k_supported_bw ath12k_mac_mac80211_bw_to_ath12k_bw(enum rate_info_bw bw)
457 {
458 	switch (bw) {
459 	case RATE_INFO_BW_20:
460 		return ATH12K_BW_20;
461 	case RATE_INFO_BW_40:
462 		return ATH12K_BW_40;
463 	case RATE_INFO_BW_80:
464 		return ATH12K_BW_80;
465 	case RATE_INFO_BW_160:
466 		return ATH12K_BW_160;
467 	case RATE_INFO_BW_320:
468 		return ATH12K_BW_320;
469 	default:
470 		return ATH12K_BW_20;
471 	}
472 }
473 
474 int ath12k_mac_hw_ratecode_to_legacy_rate(u8 hw_rc, u8 preamble, u8 *rateidx,
475 					  u16 *rate)
476 {
477 	/* As default, it is OFDM rates */
478 	int i = ATH12K_MAC_FIRST_OFDM_RATE_IDX;
479 	int max_rates_idx = ath12k_g_rates_size;
480 
481 	if (preamble == WMI_RATE_PREAMBLE_CCK) {
482 		hw_rc &= ~ATH12K_HW_RATECODE_CCK_SHORT_PREAM_MASK;
483 		i = 0;
484 		max_rates_idx = ATH12K_MAC_FIRST_OFDM_RATE_IDX;
485 	}
486 
487 	while (i < max_rates_idx) {
488 		if (hw_rc == ath12k_legacy_rates[i].hw_value) {
489 			*rateidx = i;
490 			*rate = ath12k_legacy_rates[i].bitrate;
491 			return 0;
492 		}
493 		i++;
494 	}
495 
496 	return -EINVAL;
497 }
498 EXPORT_SYMBOL(ath12k_mac_hw_ratecode_to_legacy_rate);
499 
500 u8 ath12k_mac_bitrate_to_idx(const struct ieee80211_supported_band *sband,
501 			     u32 bitrate)
502 {
503 	int i;
504 
505 	for (i = 0; i < sband->n_bitrates; i++)
506 		if (sband->bitrates[i].bitrate == bitrate)
507 			return i;
508 
509 	return 0;
510 }
511 
512 static u32
513 ath12k_mac_max_ht_nss(const u8 *ht_mcs_mask)
514 {
515 	int nss;
516 
517 	for (nss = IEEE80211_HT_MCS_MASK_LEN - 1; nss >= 0; nss--)
518 		if (ht_mcs_mask[nss])
519 			return nss + 1;
520 
521 	return 1;
522 }
523 
524 static u32
525 ath12k_mac_max_vht_nss(const u16 *vht_mcs_mask)
526 {
527 	int nss;
528 
529 	for (nss = NL80211_VHT_NSS_MAX - 1; nss >= 0; nss--)
530 		if (vht_mcs_mask[nss])
531 			return nss + 1;
532 
533 	return 1;
534 }
535 
536 static u32
537 ath12k_mac_max_he_nss(const u16 he_mcs_mask[NL80211_HE_NSS_MAX])
538 {
539 	int nss;
540 
541 	for (nss = NL80211_HE_NSS_MAX - 1; nss >= 0; nss--)
542 		if (he_mcs_mask[nss])
543 			return nss + 1;
544 
545 	return 1;
546 }
547 
548 static u32
549 ath12k_mac_max_eht_nss(const u16 eht_mcs_mask[NL80211_EHT_NSS_MAX])
550 {
551 	int nss;
552 
553 	for (nss = NL80211_EHT_NSS_MAX - 1; nss >= 0; nss--)
554 		if (eht_mcs_mask[nss])
555 			return nss + 1;
556 
557 	return 1;
558 }
559 
560 static u32
561 ath12k_mac_max_eht_mcs_nss(const u8 *eht_mcs, int eht_mcs_set_size)
562 {
563 	int i;
564 	u8 nss = 0;
565 
566 	for (i = 0; i < eht_mcs_set_size; i++)
567 		nss = max(nss, u8_get_bits(eht_mcs[i], IEEE80211_EHT_MCS_NSS_RX));
568 
569 	return nss;
570 }
571 
572 static u8 ath12k_parse_mpdudensity(u8 mpdudensity)
573 {
574 /*  From IEEE Std 802.11-2020 defined values for "Minimum MPDU Start Spacing":
575  *   0 for no restriction
576  *   1 for 1/4 us
577  *   2 for 1/2 us
578  *   3 for 1 us
579  *   4 for 2 us
580  *   5 for 4 us
581  *   6 for 8 us
582  *   7 for 16 us
583  */
584 	switch (mpdudensity) {
585 	case 0:
586 		return 0;
587 	case 1:
588 	case 2:
589 	case 3:
590 	/* Our lower layer calculations limit our precision to
591 	 * 1 microsecond
592 	 */
593 		return 1;
594 	case 4:
595 		return 2;
596 	case 5:
597 		return 4;
598 	case 6:
599 		return 8;
600 	case 7:
601 		return 16;
602 	default:
603 		return 0;
604 	}
605 }
606 
607 static int ath12k_mac_vif_link_chan(struct ieee80211_vif *vif, u8 link_id,
608 				    struct cfg80211_chan_def *def)
609 {
610 	struct ieee80211_bss_conf *link_conf;
611 	struct ieee80211_chanctx_conf *conf;
612 
613 	rcu_read_lock();
614 	link_conf = rcu_dereference(vif->link_conf[link_id]);
615 
616 	if (!link_conf) {
617 		rcu_read_unlock();
618 		return -ENOLINK;
619 	}
620 
621 	conf = rcu_dereference(link_conf->chanctx_conf);
622 	if (!conf) {
623 		rcu_read_unlock();
624 		return -ENOENT;
625 	}
626 	*def = conf->def;
627 	rcu_read_unlock();
628 
629 	return 0;
630 }
631 
632 static struct ath12k_link_vif *
633 ath12k_mac_get_tx_arvif(struct ath12k_link_vif *arvif,
634 			struct ieee80211_bss_conf *link_conf)
635 {
636 	struct ieee80211_bss_conf *tx_bss_conf;
637 	struct ath12k *ar = arvif->ar;
638 	struct ath12k_vif *tx_ahvif;
639 
640 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
641 
642 	tx_bss_conf = wiphy_dereference(ath12k_ar_to_hw(ar)->wiphy,
643 					link_conf->tx_bss_conf);
644 	if (tx_bss_conf) {
645 		tx_ahvif = ath12k_vif_to_ahvif(tx_bss_conf->vif);
646 		return wiphy_dereference(tx_ahvif->ah->hw->wiphy,
647 					 tx_ahvif->link[tx_bss_conf->link_id]);
648 	}
649 
650 	return NULL;
651 }
652 
653 static const u8 *ath12k_mac_get_tx_bssid(struct ath12k_link_vif *arvif)
654 {
655 	struct ieee80211_bss_conf *link_conf;
656 	struct ath12k_link_vif *tx_arvif;
657 	struct ath12k *ar = arvif->ar;
658 
659 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
660 
661 	link_conf = ath12k_mac_get_link_bss_conf(arvif);
662 	if (!link_conf) {
663 		ath12k_warn(ar->ab,
664 			    "unable to access bss link conf for link %u required to retrieve transmitting link conf\n",
665 			    arvif->link_id);
666 		return NULL;
667 	}
668 	if (link_conf->vif->type == NL80211_IFTYPE_STATION) {
669 		if (link_conf->nontransmitted)
670 			return link_conf->transmitter_bssid;
671 	} else {
672 		tx_arvif = ath12k_mac_get_tx_arvif(arvif, link_conf);
673 		if (tx_arvif)
674 			return tx_arvif->bssid;
675 	}
676 
677 	return NULL;
678 }
679 
680 struct ieee80211_bss_conf *
681 ath12k_mac_get_link_bss_conf(struct ath12k_link_vif *arvif)
682 {
683 	struct ieee80211_vif *vif = arvif->ahvif->vif;
684 	struct ieee80211_bss_conf *link_conf;
685 	struct ath12k *ar = arvif->ar;
686 
687 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
688 
689 	if (arvif->link_id >= IEEE80211_MLD_MAX_NUM_LINKS)
690 		return NULL;
691 
692 	link_conf = wiphy_dereference(ath12k_ar_to_hw(ar)->wiphy,
693 				      vif->link_conf[arvif->link_id]);
694 
695 	return link_conf;
696 }
697 
698 static struct ieee80211_link_sta *ath12k_mac_get_link_sta(struct ath12k_link_sta *arsta)
699 {
700 	struct ath12k_sta *ahsta = arsta->ahsta;
701 	struct ieee80211_sta *sta = ath12k_ahsta_to_sta(ahsta);
702 	struct ieee80211_link_sta *link_sta;
703 
704 	lockdep_assert_wiphy(ahsta->ahvif->ah->hw->wiphy);
705 
706 	if (arsta->link_id >= IEEE80211_MLD_MAX_NUM_LINKS)
707 		return NULL;
708 
709 	link_sta = wiphy_dereference(ahsta->ahvif->ah->hw->wiphy,
710 				     sta->link[arsta->link_id]);
711 
712 	return link_sta;
713 }
714 
715 static bool ath12k_mac_bitrate_is_cck(int bitrate)
716 {
717 	switch (bitrate) {
718 	case 10:
719 	case 20:
720 	case 55:
721 	case 110:
722 		return true;
723 	}
724 
725 	return false;
726 }
727 
728 u8 ath12k_mac_hw_rate_to_idx(const struct ieee80211_supported_band *sband,
729 			     u8 hw_rate, bool cck)
730 {
731 	const struct ieee80211_rate *rate;
732 	int i;
733 
734 	for (i = 0; i < sband->n_bitrates; i++) {
735 		rate = &sband->bitrates[i];
736 
737 		if (ath12k_mac_bitrate_is_cck(rate->bitrate) != cck)
738 			continue;
739 
740 		/* To handle 802.11a PPDU type */
741 		if ((!cck) && (rate->hw_value == hw_rate) &&
742 		    (rate->flags & IEEE80211_RATE_MANDATORY_A))
743 			return i;
744 		/* To handle 802.11b short PPDU type */
745 		else if (rate->flags & IEEE80211_RATE_SHORT_PREAMBLE &&
746 			 rate->hw_value_short == hw_rate)
747 			return i;
748 		/* To handle 802.11b long PPDU type */
749 		else if (rate->hw_value == hw_rate)
750 			return i;
751 	}
752 
753 	return 0;
754 }
755 
756 static u8 ath12k_mac_bitrate_to_rate(int bitrate)
757 {
758 	return DIV_ROUND_UP(bitrate, 5) |
759 	       (ath12k_mac_bitrate_is_cck(bitrate) ? BIT(7) : 0);
760 }
761 
762 static void ath12k_get_arvif_iter(void *data, u8 *mac,
763 				  struct ieee80211_vif *vif)
764 {
765 	struct ath12k_vif_iter *arvif_iter = data;
766 	struct ath12k_vif *ahvif = ath12k_vif_to_ahvif(vif);
767 	unsigned long links_map = ahvif->links_map;
768 	struct ath12k_link_vif *arvif;
769 	u8 link_id;
770 
771 	for_each_set_bit(link_id, &links_map, IEEE80211_MLD_MAX_NUM_LINKS) {
772 		arvif = rcu_dereference(ahvif->link[link_id]);
773 
774 		if (WARN_ON(!arvif))
775 			continue;
776 
777 		if (!arvif->is_created)
778 			continue;
779 
780 		if (arvif->vdev_id == arvif_iter->vdev_id &&
781 		    arvif->ar == arvif_iter->ar) {
782 			arvif_iter->arvif = arvif;
783 			break;
784 		}
785 	}
786 }
787 
788 struct ath12k_link_vif *ath12k_mac_get_arvif(struct ath12k *ar, u32 vdev_id)
789 {
790 	struct ath12k_vif_iter arvif_iter = {};
791 	u32 flags;
792 
793 	/* To use the arvif returned, caller must have held rcu read lock.
794 	 */
795 	lockdep_assert_in_rcu_read_lock();
796 	arvif_iter.vdev_id = vdev_id;
797 	arvif_iter.ar = ar;
798 
799 	flags = IEEE80211_IFACE_ITER_RESUME_ALL;
800 	ieee80211_iterate_active_interfaces_atomic(ath12k_ar_to_hw(ar),
801 						   flags,
802 						   ath12k_get_arvif_iter,
803 						   &arvif_iter);
804 	if (!arvif_iter.arvif) {
805 		ath12k_warn(ar->ab, "No VIF found for vdev %d\n", vdev_id);
806 		return NULL;
807 	}
808 
809 	return arvif_iter.arvif;
810 }
811 
812 struct ath12k_link_vif *ath12k_mac_get_arvif_by_vdev_id(struct ath12k_base *ab,
813 							u32 vdev_id)
814 {
815 	int i;
816 	struct ath12k_pdev *pdev;
817 	struct ath12k_link_vif *arvif;
818 
819 	for (i = 0; i < ab->num_radios; i++) {
820 		pdev = rcu_dereference(ab->pdevs_active[i]);
821 		if (pdev && pdev->ar &&
822 		    (pdev->ar->allocated_vdev_map & (1LL << vdev_id))) {
823 			arvif = ath12k_mac_get_arvif(pdev->ar, vdev_id);
824 			if (arvif)
825 				return arvif;
826 		}
827 	}
828 
829 	return NULL;
830 }
831 
832 struct ath12k *ath12k_mac_get_ar_by_vdev_id(struct ath12k_base *ab, u32 vdev_id)
833 {
834 	int i;
835 	struct ath12k_pdev *pdev;
836 
837 	for (i = 0; i < ab->num_radios; i++) {
838 		pdev = rcu_dereference(ab->pdevs_active[i]);
839 		if (pdev && pdev->ar) {
840 			if (pdev->ar->allocated_vdev_map & (1LL << vdev_id))
841 				return pdev->ar;
842 		}
843 	}
844 
845 	return NULL;
846 }
847 
848 struct ath12k *ath12k_mac_get_ar_by_pdev_id(struct ath12k_base *ab, u32 pdev_id)
849 {
850 	int i;
851 	struct ath12k_pdev *pdev;
852 
853 	if (ab->hw_params->single_pdev_only) {
854 		pdev = rcu_dereference(ab->pdevs_active[0]);
855 		return pdev ? pdev->ar : NULL;
856 	}
857 
858 	if (WARN_ON(pdev_id > ab->num_radios))
859 		return NULL;
860 
861 	for (i = 0; i < ab->num_radios; i++) {
862 		if (ab->fw_mode == ATH12K_FIRMWARE_MODE_FTM)
863 			pdev = &ab->pdevs[i];
864 		else
865 			pdev = rcu_dereference(ab->pdevs_active[i]);
866 
867 		if (pdev && pdev->pdev_id == pdev_id)
868 			return (pdev->ar ? pdev->ar : NULL);
869 	}
870 
871 	return NULL;
872 }
873 
874 static bool ath12k_mac_is_ml_arvif(struct ath12k_link_vif *arvif)
875 {
876 	struct ath12k_vif *ahvif = arvif->ahvif;
877 
878 	lockdep_assert_wiphy(ahvif->ah->hw->wiphy);
879 
880 	if (ahvif->vif->valid_links & BIT(arvif->link_id))
881 		return true;
882 
883 	return false;
884 }
885 
886 static struct ath12k *ath12k_mac_get_ar_by_chan(struct ieee80211_hw *hw,
887 						struct ieee80211_channel *channel)
888 {
889 	struct ath12k_hw *ah = hw->priv;
890 	struct ath12k *ar;
891 	int i;
892 
893 	ar = ah->radio;
894 
895 	if (ah->num_radio == 1)
896 		return ar;
897 
898 	for_each_ar(ah, ar, i) {
899 		if (channel->center_freq >= KHZ_TO_MHZ(ar->freq_range.start_freq) &&
900 		    channel->center_freq <= KHZ_TO_MHZ(ar->freq_range.end_freq))
901 			return ar;
902 	}
903 	return NULL;
904 }
905 
906 static struct ath12k *ath12k_get_ar_by_ctx(struct ieee80211_hw *hw,
907 					   struct ieee80211_chanctx_conf *ctx)
908 {
909 	if (!ctx)
910 		return NULL;
911 
912 	return ath12k_mac_get_ar_by_chan(hw, ctx->def.chan);
913 }
914 
915 struct ath12k *ath12k_get_ar_by_vif(struct ieee80211_hw *hw,
916 				    struct ieee80211_vif *vif,
917 				    u8 link_id)
918 {
919 	struct ath12k_vif *ahvif = ath12k_vif_to_ahvif(vif);
920 	struct ath12k_hw *ah = ath12k_hw_to_ah(hw);
921 	struct ath12k_link_vif *arvif;
922 
923 	lockdep_assert_wiphy(hw->wiphy);
924 
925 	/* If there is one pdev within ah, then we return
926 	 * ar directly.
927 	 */
928 	if (ah->num_radio == 1)
929 		return ah->radio;
930 
931 	if (!(ahvif->links_map & BIT(link_id)))
932 		return NULL;
933 
934 	arvif = wiphy_dereference(hw->wiphy, ahvif->link[link_id]);
935 	if (arvif && arvif->is_created)
936 		return arvif->ar;
937 
938 	return NULL;
939 }
940 
941 void ath12k_mac_get_any_chanctx_conf_iter(struct ieee80211_hw *hw,
942 					  struct ieee80211_chanctx_conf *conf,
943 					  void *data)
944 {
945 	struct ath12k_mac_get_any_chanctx_conf_arg *arg = data;
946 	struct ath12k *ctx_ar = ath12k_get_ar_by_ctx(hw, conf);
947 
948 	if (ctx_ar == arg->ar)
949 		arg->chanctx_conf = conf;
950 }
951 
952 static struct ath12k_link_vif *ath12k_mac_get_vif_up(struct ath12k *ar)
953 {
954 	struct ath12k_link_vif *arvif;
955 
956 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
957 
958 	list_for_each_entry(arvif, &ar->arvifs, list) {
959 		if (arvif->is_up)
960 			return arvif;
961 	}
962 
963 	return NULL;
964 }
965 
966 static bool ath12k_mac_band_match(enum nl80211_band band1, enum WMI_HOST_WLAN_BAND band2)
967 {
968 	switch (band1) {
969 	case NL80211_BAND_2GHZ:
970 		if (band2 & WMI_HOST_WLAN_2GHZ_CAP)
971 			return true;
972 		break;
973 	case NL80211_BAND_5GHZ:
974 	case NL80211_BAND_6GHZ:
975 		if (band2 & WMI_HOST_WLAN_5GHZ_CAP)
976 			return true;
977 		break;
978 	default:
979 		return false;
980 	}
981 
982 	return false;
983 }
984 
985 static u8 ath12k_mac_get_target_pdev_id_from_vif(struct ath12k_link_vif *arvif)
986 {
987 	struct ath12k *ar = arvif->ar;
988 	struct ath12k_base *ab = ar->ab;
989 	struct ieee80211_vif *vif = arvif->ahvif->vif;
990 	struct cfg80211_chan_def def;
991 	enum nl80211_band band;
992 	u8 pdev_id = ab->fw_pdev[0].pdev_id;
993 	int i;
994 
995 	if (WARN_ON(ath12k_mac_vif_link_chan(vif, arvif->link_id, &def)))
996 		return pdev_id;
997 
998 	band = def.chan->band;
999 
1000 	for (i = 0; i < ab->fw_pdev_count; i++) {
1001 		if (ath12k_mac_band_match(band, ab->fw_pdev[i].supported_bands))
1002 			return ab->fw_pdev[i].pdev_id;
1003 	}
1004 
1005 	return pdev_id;
1006 }
1007 
1008 u8 ath12k_mac_get_target_pdev_id(struct ath12k *ar)
1009 {
1010 	struct ath12k_link_vif *arvif;
1011 	struct ath12k_base *ab = ar->ab;
1012 
1013 	if (!ab->hw_params->single_pdev_only)
1014 		return ar->pdev->pdev_id;
1015 
1016 	arvif = ath12k_mac_get_vif_up(ar);
1017 
1018 	/* fw_pdev array has pdev ids derived from phy capability
1019 	 * service ready event (pdev_and_hw_link_ids).
1020 	 * If no vif is active, return default first index.
1021 	 */
1022 	if (!arvif)
1023 		return ar->ab->fw_pdev[0].pdev_id;
1024 
1025 	/* If active vif is found, return the pdev id matching chandef band */
1026 	return ath12k_mac_get_target_pdev_id_from_vif(arvif);
1027 }
1028 
1029 static void ath12k_pdev_caps_update(struct ath12k *ar)
1030 {
1031 	struct ath12k_base *ab = ar->ab;
1032 
1033 	ar->max_tx_power = ab->target_caps.hw_max_tx_power;
1034 
1035 	/* FIXME: Set min_tx_power to ab->target_caps.hw_min_tx_power.
1036 	 * But since the received value in svcrdy is same as hw_max_tx_power,
1037 	 * we can set ar->min_tx_power to 0 currently until
1038 	 * this is fixed in firmware
1039 	 */
1040 	ar->min_tx_power = 0;
1041 
1042 	ar->txpower_limit_2g = ar->max_tx_power;
1043 	ar->txpower_limit_5g = ar->max_tx_power;
1044 	ar->txpower_scale = WMI_HOST_TP_SCALE_MAX;
1045 }
1046 
1047 static int ath12k_mac_txpower_recalc(struct ath12k *ar)
1048 {
1049 	struct ath12k_pdev *pdev = ar->pdev;
1050 	struct ath12k_link_vif *arvif;
1051 	int ret, txpower = -1;
1052 	u32 param;
1053 
1054 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
1055 
1056 	list_for_each_entry(arvif, &ar->arvifs, list) {
1057 		if (arvif->txpower <= 0)
1058 			continue;
1059 
1060 		if (txpower == -1)
1061 			txpower = arvif->txpower;
1062 		else
1063 			txpower = min(txpower, arvif->txpower);
1064 	}
1065 
1066 	if (txpower == -1)
1067 		return 0;
1068 
1069 	/* txpwr is set as 2 units per dBm in FW*/
1070 	txpower = min_t(u32, max_t(u32, ar->min_tx_power, txpower),
1071 			ar->max_tx_power) * 2;
1072 
1073 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "txpower to set in hw %d\n",
1074 		   txpower / 2);
1075 
1076 	if ((pdev->cap.supported_bands & WMI_HOST_WLAN_2GHZ_CAP) &&
1077 	    ar->txpower_limit_2g != txpower) {
1078 		param = WMI_PDEV_PARAM_TXPOWER_LIMIT2G;
1079 		ret = ath12k_wmi_pdev_set_param(ar, param,
1080 						txpower, ar->pdev->pdev_id);
1081 		if (ret)
1082 			goto fail;
1083 		ar->txpower_limit_2g = txpower;
1084 	}
1085 
1086 	if ((pdev->cap.supported_bands & WMI_HOST_WLAN_5GHZ_CAP) &&
1087 	    ar->txpower_limit_5g != txpower) {
1088 		param = WMI_PDEV_PARAM_TXPOWER_LIMIT5G;
1089 		ret = ath12k_wmi_pdev_set_param(ar, param,
1090 						txpower, ar->pdev->pdev_id);
1091 		if (ret)
1092 			goto fail;
1093 		ar->txpower_limit_5g = txpower;
1094 	}
1095 
1096 	return 0;
1097 
1098 fail:
1099 	ath12k_warn(ar->ab, "failed to recalc txpower limit %d using pdev param %d: %d\n",
1100 		    txpower / 2, param, ret);
1101 	return ret;
1102 }
1103 
1104 static int ath12k_recalc_rtscts_prot(struct ath12k_link_vif *arvif)
1105 {
1106 	struct ath12k *ar = arvif->ar;
1107 	u32 vdev_param, rts_cts;
1108 	int ret;
1109 
1110 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
1111 
1112 	vdev_param = WMI_VDEV_PARAM_ENABLE_RTSCTS;
1113 
1114 	/* Enable RTS/CTS protection for sw retries (when legacy stations
1115 	 * are in BSS) or by default only for second rate series.
1116 	 * TODO: Check if we need to enable CTS 2 Self in any case
1117 	 */
1118 	rts_cts = WMI_USE_RTS_CTS;
1119 
1120 	if (arvif->num_legacy_stations > 0)
1121 		rts_cts |= WMI_RTSCTS_ACROSS_SW_RETRIES << 4;
1122 	else
1123 		rts_cts |= WMI_RTSCTS_FOR_SECOND_RATESERIES << 4;
1124 
1125 	/* Need not send duplicate param value to firmware */
1126 	if (arvif->rtscts_prot_mode == rts_cts)
1127 		return 0;
1128 
1129 	arvif->rtscts_prot_mode = rts_cts;
1130 
1131 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac vdev %d recalc rts/cts prot %d\n",
1132 		   arvif->vdev_id, rts_cts);
1133 
1134 	ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
1135 					    vdev_param, rts_cts);
1136 	if (ret)
1137 		ath12k_warn(ar->ab, "failed to recalculate rts/cts prot for vdev %d: %d\n",
1138 			    arvif->vdev_id, ret);
1139 
1140 	return ret;
1141 }
1142 
1143 static int ath12k_mac_set_kickout(struct ath12k_link_vif *arvif)
1144 {
1145 	struct ath12k *ar = arvif->ar;
1146 	u32 param;
1147 	int ret;
1148 
1149 	ret = ath12k_wmi_pdev_set_param(ar, WMI_PDEV_PARAM_STA_KICKOUT_TH,
1150 					ATH12K_KICKOUT_THRESHOLD,
1151 					ar->pdev->pdev_id);
1152 	if (ret) {
1153 		ath12k_warn(ar->ab, "failed to set kickout threshold on vdev %i: %d\n",
1154 			    arvif->vdev_id, ret);
1155 		return ret;
1156 	}
1157 
1158 	param = WMI_VDEV_PARAM_AP_KEEPALIVE_MIN_IDLE_INACTIVE_TIME_SECS;
1159 	ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id, param,
1160 					    ATH12K_KEEPALIVE_MIN_IDLE);
1161 	if (ret) {
1162 		ath12k_warn(ar->ab, "failed to set keepalive minimum idle time on vdev %i: %d\n",
1163 			    arvif->vdev_id, ret);
1164 		return ret;
1165 	}
1166 
1167 	param = WMI_VDEV_PARAM_AP_KEEPALIVE_MAX_IDLE_INACTIVE_TIME_SECS;
1168 	ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id, param,
1169 					    ATH12K_KEEPALIVE_MAX_IDLE);
1170 	if (ret) {
1171 		ath12k_warn(ar->ab, "failed to set keepalive maximum idle time on vdev %i: %d\n",
1172 			    arvif->vdev_id, ret);
1173 		return ret;
1174 	}
1175 
1176 	param = WMI_VDEV_PARAM_AP_KEEPALIVE_MAX_UNRESPONSIVE_TIME_SECS;
1177 	ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id, param,
1178 					    ATH12K_KEEPALIVE_MAX_UNRESPONSIVE);
1179 	if (ret) {
1180 		ath12k_warn(ar->ab, "failed to set keepalive maximum unresponsive time on vdev %i: %d\n",
1181 			    arvif->vdev_id, ret);
1182 		return ret;
1183 	}
1184 
1185 	return 0;
1186 }
1187 
1188 static void ath12k_mac_link_sta_rhash_cleanup(void *data, struct ieee80211_sta *sta)
1189 {
1190 	u8 link_id;
1191 	unsigned long links_map;
1192 	struct ath12k_sta *ahsta;
1193 	struct ath12k *ar = data;
1194 	struct ath12k_link_sta *arsta;
1195 	struct ath12k_link_vif *arvif;
1196 	struct ath12k_base *ab = ar->ab;
1197 
1198 	ahsta = ath12k_sta_to_ahsta(sta);
1199 	links_map = ahsta->links_map;
1200 
1201 	rcu_read_lock();
1202 	for_each_set_bit(link_id, &links_map, IEEE80211_MLD_MAX_NUM_LINKS) {
1203 		arsta = rcu_dereference(ahsta->link[link_id]);
1204 		if (!arsta)
1205 			continue;
1206 		arvif = arsta->arvif;
1207 		if (!(arvif->ar == ar))
1208 			continue;
1209 
1210 		spin_lock_bh(&ab->base_lock);
1211 		ath12k_link_sta_rhash_delete(ab, arsta);
1212 		spin_unlock_bh(&ab->base_lock);
1213 	}
1214 	rcu_read_unlock();
1215 }
1216 
1217 void ath12k_mac_peer_cleanup_all(struct ath12k *ar)
1218 {
1219 	struct ath12k_dp_link_peer *peer, *tmp;
1220 	struct ath12k_base *ab = ar->ab;
1221 	struct ath12k_dp *dp = ath12k_ab_to_dp(ab);
1222 	struct ath12k_link_vif *arvif, *tmp_vif;
1223 	struct ath12k_dp_hw *dp_hw = &ar->ah->dp_hw;
1224 	struct ath12k_dp_peer *dp_peer = NULL;
1225 	u16 peerid_index;
1226 	struct list_head peers;
1227 
1228 	INIT_LIST_HEAD(&peers);
1229 
1230 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
1231 
1232 	spin_lock_bh(&dp->dp_lock);
1233 	list_for_each_entry_safe(peer, tmp, &dp->peers, list) {
1234 		/* Skip Rx TID cleanup for self peer */
1235 		if (peer->sta && peer->dp_peer)
1236 			ath12k_dp_rx_peer_tid_cleanup(ar, peer);
1237 
1238 		/* cleanup dp peer */
1239 		spin_lock_bh(&dp_hw->peer_lock);
1240 		dp_peer = peer->dp_peer;
1241 		if (dp_peer) {
1242 			peerid_index = ath12k_dp_peer_get_peerid_index(dp, peer->peer_id);
1243 			rcu_assign_pointer(dp_peer->link_peers[peer->link_id], NULL);
1244 			WRITE_ONCE(dp_peer->link_peers_map,
1245 				   READ_ONCE(dp_peer->link_peers_map) & ~BIT(peer->link_id));
1246 			rcu_assign_pointer(dp_hw->dp_peers[peerid_index], NULL);
1247 		}
1248 		spin_unlock_bh(&dp_hw->peer_lock);
1249 
1250 		ath12k_dp_link_peer_rhash_delete(dp, peer);
1251 
1252 		list_move(&peer->list, &peers);
1253 	}
1254 	spin_unlock_bh(&dp->dp_lock);
1255 
1256 	synchronize_rcu();
1257 
1258 	list_for_each_entry_safe(peer, tmp, &peers, list) {
1259 		ath12k_dp_link_peer_free(peer);
1260 	}
1261 
1262 	ar->num_peers = 0;
1263 	ar->num_stations = 0;
1264 
1265 	/* Cleanup rhash table maintained for arsta by iterating over sta */
1266 	ieee80211_iterate_stations_mtx(ar->ah->hw, ath12k_mac_link_sta_rhash_cleanup,
1267 				       ar);
1268 
1269 	/* Delete all the self dp_peers on asserted radio */
1270 	list_for_each_entry_safe_reverse(arvif, tmp_vif, &ar->arvifs, list) {
1271 		if ((arvif->ahvif->vdev_type == WMI_VDEV_TYPE_AP) &&
1272 		    (arvif->link_id < IEEE80211_MLD_MAX_NUM_LINKS)) {
1273 			ath12k_dp_peer_delete(dp_hw, arvif->bssid, NULL);
1274 			arvif->num_stations = 0;
1275 		}
1276 	}
1277 }
1278 
1279 void ath12k_mac_dp_peer_cleanup(struct ath12k_hw *ah)
1280 {
1281 	struct list_head peers;
1282 	struct ath12k_dp_peer *dp_peer, *tmp;
1283 	struct ath12k_dp_hw *dp_hw = &ah->dp_hw;
1284 
1285 	INIT_LIST_HEAD(&peers);
1286 
1287 	spin_lock_bh(&dp_hw->peer_lock);
1288 	list_for_each_entry_safe(dp_peer, tmp, &dp_hw->dp_peers_list, list) {
1289 		if (dp_peer->is_mlo) {
1290 			rcu_assign_pointer(dp_hw->dp_peers[dp_peer->peer_id], NULL);
1291 			clear_bit(dp_peer->peer_id, ah->free_ml_peer_id_map);
1292 		}
1293 
1294 		list_move(&dp_peer->list, &peers);
1295 	}
1296 
1297 	spin_unlock_bh(&dp_hw->peer_lock);
1298 
1299 	synchronize_rcu();
1300 
1301 	list_for_each_entry_safe(dp_peer, tmp, &peers, list) {
1302 		list_del(&dp_peer->list);
1303 		kfree(dp_peer);
1304 	}
1305 }
1306 
1307 static int ath12k_mac_vdev_setup_sync(struct ath12k *ar)
1308 {
1309 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
1310 
1311 	if (test_bit(ATH12K_FLAG_CRASH_FLUSH, &ar->ab->dev_flags))
1312 		return -ESHUTDOWN;
1313 
1314 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "vdev setup timeout %d\n",
1315 		   ATH12K_VDEV_SETUP_TIMEOUT_HZ);
1316 
1317 	if (!wait_for_completion_timeout(&ar->vdev_setup_done,
1318 					 ATH12K_VDEV_SETUP_TIMEOUT_HZ))
1319 		return -ETIMEDOUT;
1320 
1321 	return ar->last_wmi_vdev_start_status ? -EINVAL : 0;
1322 }
1323 
1324 static int ath12k_monitor_vdev_up(struct ath12k *ar, int vdev_id)
1325 {
1326 	struct ath12k_wmi_vdev_up_params params = {};
1327 	int ret;
1328 
1329 	params.vdev_id = vdev_id;
1330 	params.bssid = ar->mac_addr;
1331 	ret = ath12k_wmi_vdev_up(ar, &params);
1332 	if (ret) {
1333 		ath12k_warn(ar->ab, "failed to put up monitor vdev %i: %d\n",
1334 			    vdev_id, ret);
1335 		return ret;
1336 	}
1337 
1338 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac monitor vdev %i started\n",
1339 		   vdev_id);
1340 	return 0;
1341 }
1342 
1343 static int ath12k_mac_monitor_vdev_start(struct ath12k *ar, int vdev_id,
1344 					 struct cfg80211_chan_def *chandef)
1345 {
1346 	struct ieee80211_channel *channel;
1347 	struct wmi_vdev_start_req_arg arg = {};
1348 	struct ath12k_wmi_vdev_up_params params = {};
1349 	int ret;
1350 
1351 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
1352 
1353 	channel = chandef->chan;
1354 	arg.vdev_id = vdev_id;
1355 	arg.freq = channel->center_freq;
1356 	arg.band_center_freq1 = chandef->center_freq1;
1357 	arg.band_center_freq2 = chandef->center_freq2;
1358 	arg.mode = ath12k_phymodes[chandef->chan->band][chandef->width];
1359 	arg.chan_radar = !!(channel->flags & IEEE80211_CHAN_RADAR);
1360 
1361 	arg.min_power = 0;
1362 	arg.max_power = channel->max_power;
1363 	arg.max_reg_power = channel->max_reg_power;
1364 	arg.max_antenna_gain = channel->max_antenna_gain;
1365 
1366 	arg.pref_tx_streams = ar->num_tx_chains;
1367 	arg.pref_rx_streams = ar->num_rx_chains;
1368 	arg.punct_bitmap = 0xFFFFFFFF;
1369 
1370 	arg.passive |= !!(chandef->chan->flags & IEEE80211_CHAN_NO_IR);
1371 
1372 	reinit_completion(&ar->vdev_setup_done);
1373 	reinit_completion(&ar->vdev_delete_done);
1374 
1375 	ret = ath12k_wmi_vdev_start(ar, &arg, false);
1376 	if (ret) {
1377 		ath12k_warn(ar->ab, "failed to request monitor vdev %i start: %d\n",
1378 			    vdev_id, ret);
1379 		return ret;
1380 	}
1381 
1382 	ret = ath12k_mac_vdev_setup_sync(ar);
1383 	if (ret) {
1384 		ath12k_warn(ar->ab, "failed to synchronize setup for monitor vdev %i start: %d\n",
1385 			    vdev_id, ret);
1386 		return ret;
1387 	}
1388 
1389 	params.vdev_id = vdev_id;
1390 	params.bssid = ar->mac_addr;
1391 	ret = ath12k_wmi_vdev_up(ar, &params);
1392 	if (ret) {
1393 		ath12k_warn(ar->ab, "failed to put up monitor vdev %i: %d\n",
1394 			    vdev_id, ret);
1395 		goto vdev_stop;
1396 	}
1397 
1398 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac monitor vdev %i started\n",
1399 		   vdev_id);
1400 	return 0;
1401 
1402 vdev_stop:
1403 	ret = ath12k_wmi_vdev_stop(ar, vdev_id);
1404 	if (ret)
1405 		ath12k_warn(ar->ab, "failed to stop monitor vdev %i after start failure: %d\n",
1406 			    vdev_id, ret);
1407 	return ret;
1408 }
1409 
1410 static int ath12k_mac_monitor_vdev_stop(struct ath12k *ar)
1411 {
1412 	int ret;
1413 
1414 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
1415 
1416 	reinit_completion(&ar->vdev_setup_done);
1417 
1418 	ret = ath12k_wmi_vdev_stop(ar, ar->monitor_vdev_id);
1419 	if (ret)
1420 		ath12k_warn(ar->ab, "failed to request monitor vdev %i stop: %d\n",
1421 			    ar->monitor_vdev_id, ret);
1422 
1423 	ret = ath12k_mac_vdev_setup_sync(ar);
1424 	if (ret)
1425 		ath12k_warn(ar->ab, "failed to synchronize monitor vdev %i stop: %d\n",
1426 			    ar->monitor_vdev_id, ret);
1427 
1428 	ret = ath12k_wmi_vdev_down(ar, ar->monitor_vdev_id);
1429 	if (ret)
1430 		ath12k_warn(ar->ab, "failed to put down monitor vdev %i: %d\n",
1431 			    ar->monitor_vdev_id, ret);
1432 
1433 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac monitor vdev %i stopped\n",
1434 		   ar->monitor_vdev_id);
1435 	return ret;
1436 }
1437 
1438 static int ath12k_mac_monitor_vdev_delete(struct ath12k *ar)
1439 {
1440 	int ret;
1441 	unsigned long time_left;
1442 
1443 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
1444 
1445 	if (!ar->monitor_vdev_created)
1446 		return 0;
1447 
1448 	reinit_completion(&ar->vdev_delete_done);
1449 
1450 	ret = ath12k_wmi_vdev_delete(ar, ar->monitor_vdev_id);
1451 	if (ret) {
1452 		ath12k_warn(ar->ab, "failed to request wmi monitor vdev %i removal: %d\n",
1453 			    ar->monitor_vdev_id, ret);
1454 		return ret;
1455 	}
1456 
1457 	time_left = wait_for_completion_timeout(&ar->vdev_delete_done,
1458 						ATH12K_VDEV_DELETE_TIMEOUT_HZ);
1459 	if (time_left == 0) {
1460 		ath12k_warn(ar->ab, "Timeout in receiving vdev delete response\n");
1461 	} else {
1462 		ar->allocated_vdev_map &= ~(1LL << ar->monitor_vdev_id);
1463 		ar->ab->free_vdev_map |= 1LL << (ar->monitor_vdev_id);
1464 		ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac monitor vdev %d deleted\n",
1465 			   ar->monitor_vdev_id);
1466 		ar->num_created_vdevs--;
1467 		ar->monitor_vdev_id = -1;
1468 		ar->monitor_vdev_created = false;
1469 	}
1470 
1471 	return ret;
1472 }
1473 
1474 static int ath12k_mac_monitor_start(struct ath12k *ar)
1475 {
1476 	struct ath12k_mac_get_any_chanctx_conf_arg arg;
1477 	int ret;
1478 
1479 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
1480 
1481 	if (ar->monitor_started)
1482 		return 0;
1483 
1484 	arg.ar = ar;
1485 	arg.chanctx_conf = NULL;
1486 	ieee80211_iter_chan_contexts_atomic(ath12k_ar_to_hw(ar),
1487 					    ath12k_mac_get_any_chanctx_conf_iter,
1488 					    &arg);
1489 	if (!arg.chanctx_conf)
1490 		return 0;
1491 
1492 	ret = ath12k_mac_monitor_vdev_start(ar, ar->monitor_vdev_id,
1493 					    &arg.chanctx_conf->def);
1494 	if (ret) {
1495 		ath12k_warn(ar->ab, "failed to start monitor vdev: %d\n", ret);
1496 		return ret;
1497 	}
1498 
1499 	ret = ath12k_dp_tx_htt_monitor_mode_ring_config(ar, false);
1500 	if (ret) {
1501 		ath12k_warn(ar->ab, "fail to set monitor filter: %d\n", ret);
1502 		return ret;
1503 	}
1504 
1505 	ar->monitor_started = true;
1506 	ar->num_started_vdevs++;
1507 
1508 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac monitor started\n");
1509 
1510 	return 0;
1511 }
1512 
1513 static int ath12k_mac_monitor_stop(struct ath12k *ar)
1514 {
1515 	int ret;
1516 
1517 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
1518 
1519 	if (!ar->monitor_started)
1520 		return 0;
1521 
1522 	ret = ath12k_mac_monitor_vdev_stop(ar);
1523 	if (ret) {
1524 		ath12k_warn(ar->ab, "failed to stop monitor vdev: %d\n", ret);
1525 		return ret;
1526 	}
1527 
1528 	ar->monitor_started = false;
1529 	ar->num_started_vdevs--;
1530 	ret = ath12k_dp_tx_htt_monitor_mode_ring_config(ar, true);
1531 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac monitor stopped ret %d\n", ret);
1532 	return ret;
1533 }
1534 
1535 int ath12k_mac_vdev_stop(struct ath12k_link_vif *arvif)
1536 {
1537 	struct ath12k_vif *ahvif = arvif->ahvif;
1538 	struct ath12k *ar = arvif->ar;
1539 	int ret;
1540 
1541 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
1542 
1543 	reinit_completion(&ar->vdev_setup_done);
1544 
1545 	ret = ath12k_wmi_vdev_stop(ar, arvif->vdev_id);
1546 	if (ret) {
1547 		ath12k_warn(ar->ab, "failed to stop WMI vdev %i: %d\n",
1548 			    arvif->vdev_id, ret);
1549 		goto err;
1550 	}
1551 
1552 	ret = ath12k_mac_vdev_setup_sync(ar);
1553 	if (ret) {
1554 		ath12k_warn(ar->ab, "failed to synchronize setup for vdev %i: %d\n",
1555 			    arvif->vdev_id, ret);
1556 		goto err;
1557 	}
1558 
1559 	WARN_ON(ar->num_started_vdevs == 0);
1560 
1561 	ar->num_started_vdevs--;
1562 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "vdev %pM stopped, vdev_id %d\n",
1563 		   ahvif->vif->addr, arvif->vdev_id);
1564 
1565 	if (test_bit(ATH12K_FLAG_CAC_RUNNING, &ar->dev_flags)) {
1566 		clear_bit(ATH12K_FLAG_CAC_RUNNING, &ar->dev_flags);
1567 		ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "CAC Stopped for vdev %d\n",
1568 			   arvif->vdev_id);
1569 	}
1570 
1571 	return 0;
1572 err:
1573 	return ret;
1574 }
1575 
1576 int ath12k_mac_op_config(struct ieee80211_hw *hw, int radio_idx, u32 changed)
1577 {
1578 	return 0;
1579 }
1580 EXPORT_SYMBOL(ath12k_mac_op_config);
1581 
1582 static int ath12k_mac_setup_bcn_p2p_ie(struct ath12k_link_vif *arvif,
1583 				       struct sk_buff *bcn)
1584 {
1585 	struct ath12k *ar = arvif->ar;
1586 	struct ieee80211_mgmt *mgmt;
1587 	const u8 *p2p_ie;
1588 	int ret;
1589 
1590 	mgmt = (void *)bcn->data;
1591 	p2p_ie = cfg80211_find_vendor_ie(WLAN_OUI_WFA, WLAN_OUI_TYPE_WFA_P2P,
1592 					 mgmt->u.beacon.variable,
1593 					 bcn->len - (mgmt->u.beacon.variable -
1594 						     bcn->data));
1595 	if (!p2p_ie) {
1596 		ath12k_warn(ar->ab, "no P2P ie found in beacon\n");
1597 		return -ENOENT;
1598 	}
1599 
1600 	ret = ath12k_wmi_p2p_go_bcn_ie(ar, arvif->vdev_id, p2p_ie);
1601 	if (ret) {
1602 		ath12k_warn(ar->ab, "failed to submit P2P GO bcn ie for vdev %i: %d\n",
1603 			    arvif->vdev_id, ret);
1604 		return ret;
1605 	}
1606 
1607 	return 0;
1608 }
1609 
1610 static int ath12k_mac_remove_vendor_ie(struct sk_buff *skb, unsigned int oui,
1611 				       u8 oui_type, size_t ie_offset)
1612 {
1613 	const u8 *next, *end;
1614 	size_t len;
1615 	u8 *ie;
1616 
1617 	if (WARN_ON(skb->len < ie_offset))
1618 		return -EINVAL;
1619 
1620 	ie = (u8 *)cfg80211_find_vendor_ie(oui, oui_type,
1621 					   skb->data + ie_offset,
1622 					   skb->len - ie_offset);
1623 	if (!ie)
1624 		return -ENOENT;
1625 
1626 	len = ie[1] + 2;
1627 	end = skb->data + skb->len;
1628 	next = ie + len;
1629 
1630 	if (WARN_ON(next > end))
1631 		return -EINVAL;
1632 
1633 	memmove(ie, next, end - next);
1634 	skb_trim(skb, skb->len - len);
1635 
1636 	return 0;
1637 }
1638 
1639 static void ath12k_mac_set_arvif_ies(struct ath12k_link_vif *arvif,
1640 				     struct ath12k_link_vif *tx_arvif,
1641 				     struct sk_buff *bcn,
1642 				     u8 bssid_index, bool *nontx_profile_found)
1643 {
1644 	struct ieee80211_mgmt *mgmt = (struct ieee80211_mgmt *)bcn->data;
1645 	const struct element *elem, *nontx, *index, *nie, *ext_cap_ie;
1646 	const u8 *start, *tail;
1647 	u16 rem_len;
1648 	u8 i;
1649 
1650 	start = bcn->data + ieee80211_get_hdrlen_from_skb(bcn) + sizeof(mgmt->u.beacon);
1651 	tail = skb_tail_pointer(bcn);
1652 	rem_len = tail - start;
1653 
1654 	arvif->rsnie_present = false;
1655 	arvif->wpaie_present = false;
1656 
1657 	if (cfg80211_find_ie(WLAN_EID_RSN, start, rem_len))
1658 		arvif->rsnie_present = true;
1659 	if (cfg80211_find_vendor_ie(WLAN_OUI_MICROSOFT, WLAN_OUI_TYPE_MICROSOFT_WPA,
1660 				    start, rem_len))
1661 		arvif->wpaie_present = true;
1662 
1663 	ext_cap_ie = cfg80211_find_elem(WLAN_EID_EXT_CAPABILITY, start, rem_len);
1664 	if (ext_cap_ie && ext_cap_ie->datalen >= 11 &&
1665 	    (ext_cap_ie->data[10] & WLAN_EXT_CAPA11_BCN_PROTECT))
1666 		tx_arvif->beacon_prot = true;
1667 
1668 	/* Return from here for the transmitted profile */
1669 	if (!bssid_index)
1670 		return;
1671 
1672 	/* Initial rsnie_present for the nontransmitted profile is set to be same as that
1673 	 * of the transmitted profile. It will be changed if security configurations are
1674 	 * different.
1675 	 */
1676 	*nontx_profile_found = false;
1677 	for_each_element_id(elem, WLAN_EID_MULTIPLE_BSSID, start, rem_len) {
1678 		/* Fixed minimum MBSSID element length with at least one
1679 		 * nontransmitted BSSID profile is 12 bytes as given below;
1680 		 * 1 (max BSSID indicator) +
1681 		 * 2 (Nontransmitted BSSID profile: Subelement ID + length) +
1682 		 * 4 (Nontransmitted BSSID Capabilities: tag + length + info)
1683 		 * 2 (Nontransmitted BSSID SSID: tag + length)
1684 		 * 3 (Nontransmitted BSSID Index: tag + length + BSSID index
1685 		 */
1686 		if (elem->datalen < 12 || elem->data[0] < 1)
1687 			continue; /* Max BSSID indicator must be >=1 */
1688 
1689 		for_each_element(nontx, elem->data + 1, elem->datalen - 1) {
1690 			start = nontx->data;
1691 
1692 			if (nontx->id != 0 || nontx->datalen < 4)
1693 				continue; /* Invalid nontransmitted profile */
1694 
1695 			if (nontx->data[0] != WLAN_EID_NON_TX_BSSID_CAP ||
1696 			    nontx->data[1] != 2) {
1697 				continue; /* Missing nontransmitted BSS capabilities */
1698 			}
1699 
1700 			if (nontx->data[4] != WLAN_EID_SSID)
1701 				continue; /* Missing SSID for nontransmitted BSS */
1702 
1703 			index = cfg80211_find_elem(WLAN_EID_MULTI_BSSID_IDX,
1704 						   start, nontx->datalen);
1705 			if (!index || index->datalen < 1 || index->data[0] == 0)
1706 				continue; /* Invalid MBSSID Index element */
1707 
1708 			if (index->data[0] == bssid_index) {
1709 				*nontx_profile_found = true;
1710 
1711 				/* Check if nontx BSS has beacon protection enabled */
1712 				if (!tx_arvif->beacon_prot) {
1713 					ext_cap_ie =
1714 					    cfg80211_find_elem(WLAN_EID_EXT_CAPABILITY,
1715 							       nontx->data,
1716 							       nontx->datalen);
1717 					if (ext_cap_ie && ext_cap_ie->datalen >= 11 &&
1718 					    (ext_cap_ie->data[10] &
1719 					     WLAN_EXT_CAPA11_BCN_PROTECT))
1720 						tx_arvif->beacon_prot = true;
1721 				}
1722 
1723 				if (cfg80211_find_ie(WLAN_EID_RSN,
1724 						     nontx->data,
1725 						     nontx->datalen)) {
1726 					arvif->rsnie_present = true;
1727 					return;
1728 				} else if (!arvif->rsnie_present) {
1729 					return; /* Both tx and nontx BSS are open */
1730 				}
1731 
1732 				nie = cfg80211_find_ext_elem(WLAN_EID_EXT_NON_INHERITANCE,
1733 							     nontx->data,
1734 							     nontx->datalen);
1735 				if (!nie || nie->datalen < 2)
1736 					return; /* Invalid non-inheritance element */
1737 
1738 				for (i = 1; i < nie->datalen - 1; i++) {
1739 					if (nie->data[i] == WLAN_EID_RSN) {
1740 						arvif->rsnie_present = false;
1741 						break;
1742 					}
1743 				}
1744 
1745 				return;
1746 			}
1747 		}
1748 	}
1749 }
1750 
1751 static int ath12k_mac_setup_bcn_tmpl_ema(struct ath12k_link_vif *arvif,
1752 					 struct ath12k_link_vif *tx_arvif,
1753 					 u8 bssid_index)
1754 {
1755 	struct ath12k_wmi_bcn_tmpl_ema_arg ema_args;
1756 	struct ieee80211_ema_beacons *beacons;
1757 	bool nontx_profile_found = false;
1758 	int ret = 0;
1759 	u8 i;
1760 
1761 	beacons = ieee80211_beacon_get_template_ema_list(ath12k_ar_to_hw(tx_arvif->ar),
1762 							 tx_arvif->ahvif->vif,
1763 							 tx_arvif->link_id);
1764 	if (!beacons || !beacons->cnt) {
1765 		ath12k_warn(arvif->ar->ab,
1766 			    "failed to get ema beacon templates from mac80211\n");
1767 		return -EPERM;
1768 	}
1769 
1770 	if (tx_arvif == arvif)
1771 		ath12k_mac_set_arvif_ies(arvif, tx_arvif, beacons->bcn[0].skb, 0, NULL);
1772 
1773 	for (i = 0; i < beacons->cnt; i++) {
1774 		if (tx_arvif != arvif && !nontx_profile_found)
1775 			ath12k_mac_set_arvif_ies(arvif, tx_arvif, beacons->bcn[i].skb,
1776 						 bssid_index,
1777 						 &nontx_profile_found);
1778 
1779 		ema_args.bcn_cnt = beacons->cnt;
1780 		ema_args.bcn_index = i;
1781 		ret = ath12k_wmi_bcn_tmpl(tx_arvif, &beacons->bcn[i].offs,
1782 					  beacons->bcn[i].skb, &ema_args);
1783 		if (ret) {
1784 			ath12k_warn(tx_arvif->ar->ab,
1785 				    "failed to set ema beacon template id %i error %d\n",
1786 				    i, ret);
1787 			break;
1788 		}
1789 	}
1790 
1791 	if (tx_arvif != arvif && !nontx_profile_found)
1792 		ath12k_warn(arvif->ar->ab,
1793 			    "nontransmitted bssid index %u not found in beacon template\n",
1794 			    bssid_index);
1795 
1796 	ieee80211_beacon_free_ema_list(beacons);
1797 	return ret;
1798 }
1799 
1800 static int ath12k_mac_setup_bcn_tmpl(struct ath12k_link_vif *arvif)
1801 {
1802 	struct ath12k_vif *ahvif = arvif->ahvif;
1803 	struct ieee80211_vif *vif = ath12k_ahvif_to_vif(ahvif);
1804 	struct ieee80211_bss_conf *link_conf;
1805 	struct ath12k_link_vif *tx_arvif;
1806 	struct ath12k *ar = arvif->ar;
1807 	struct ath12k_base *ab = ar->ab;
1808 	struct ieee80211_mutable_offsets offs = {};
1809 	bool nontx_profile_found = false;
1810 	struct sk_buff *bcn;
1811 	int ret;
1812 
1813 	if (ahvif->vdev_type != WMI_VDEV_TYPE_AP)
1814 		return 0;
1815 
1816 	link_conf = ath12k_mac_get_link_bss_conf(arvif);
1817 	if (!link_conf) {
1818 		ath12k_warn(ar->ab, "unable to access bss link conf to set bcn tmpl for vif %pM link %u\n",
1819 			    vif->addr, arvif->link_id);
1820 		return -ENOLINK;
1821 	}
1822 
1823 	tx_arvif = ath12k_mac_get_tx_arvif(arvif, link_conf);
1824 	if (tx_arvif) {
1825 		if (tx_arvif != arvif && arvif->is_up)
1826 			return 0;
1827 
1828 		if (link_conf->ema_ap)
1829 			return ath12k_mac_setup_bcn_tmpl_ema(arvif, tx_arvif,
1830 							     link_conf->bssid_index);
1831 	} else {
1832 		tx_arvif = arvif;
1833 	}
1834 
1835 	bcn = ieee80211_beacon_get_template(ath12k_ar_to_hw(tx_arvif->ar),
1836 					    tx_arvif->ahvif->vif,
1837 					    &offs, tx_arvif->link_id);
1838 	if (!bcn) {
1839 		ath12k_warn(ab, "failed to get beacon template from mac80211\n");
1840 		return -EPERM;
1841 	}
1842 
1843 	if (tx_arvif == arvif) {
1844 		ath12k_mac_set_arvif_ies(arvif, tx_arvif, bcn, 0, NULL);
1845 	} else {
1846 		ath12k_mac_set_arvif_ies(arvif, tx_arvif, bcn,
1847 					 link_conf->bssid_index,
1848 					 &nontx_profile_found);
1849 		if (!nontx_profile_found)
1850 			ath12k_warn(ab,
1851 				    "nontransmitted profile not found in beacon template\n");
1852 	}
1853 
1854 	if (ahvif->vif->type == NL80211_IFTYPE_AP && ahvif->vif->p2p) {
1855 		ret = ath12k_mac_setup_bcn_p2p_ie(arvif, bcn);
1856 		if (ret) {
1857 			ath12k_warn(ab, "failed to setup P2P GO bcn ie: %d\n",
1858 				    ret);
1859 			goto free_bcn_skb;
1860 		}
1861 
1862 		/* P2P IE is inserted by firmware automatically (as
1863 		 * configured above) so remove it from the base beacon
1864 		 * template to avoid duplicate P2P IEs in beacon frames.
1865 		 */
1866 		ret = ath12k_mac_remove_vendor_ie(bcn, WLAN_OUI_WFA,
1867 						  WLAN_OUI_TYPE_WFA_P2P,
1868 						  offsetof(struct ieee80211_mgmt,
1869 							   u.beacon.variable));
1870 		if (ret) {
1871 			ath12k_warn(ab, "failed to remove P2P vendor ie: %d\n",
1872 				    ret);
1873 			goto free_bcn_skb;
1874 		}
1875 	}
1876 
1877 	ret = ath12k_wmi_bcn_tmpl(arvif, &offs, bcn, NULL);
1878 
1879 	if (ret)
1880 		ath12k_warn(ab, "failed to submit beacon template command: %d\n",
1881 			    ret);
1882 
1883 free_bcn_skb:
1884 	kfree_skb(bcn);
1885 	return ret;
1886 }
1887 
1888 static void ath12k_control_beaconing(struct ath12k_link_vif *arvif,
1889 				     struct ieee80211_bss_conf *info)
1890 {
1891 	struct ath12k_wmi_vdev_up_params params = {};
1892 	struct ath12k_vif *ahvif = arvif->ahvif;
1893 	struct ath12k *ar = arvif->ar;
1894 	int ret;
1895 
1896 	lockdep_assert_wiphy(ath12k_ar_to_hw(arvif->ar)->wiphy);
1897 
1898 	if (!info->enable_beacon) {
1899 		ret = ath12k_wmi_vdev_down(ar, arvif->vdev_id);
1900 		if (ret)
1901 			ath12k_warn(ar->ab, "failed to down vdev_id %i: %d\n",
1902 				    arvif->vdev_id, ret);
1903 
1904 		arvif->is_up = false;
1905 		return;
1906 	}
1907 
1908 	/* Install the beacon template to the FW */
1909 	ret = ath12k_mac_setup_bcn_tmpl(arvif);
1910 	if (ret) {
1911 		ath12k_warn(ar->ab, "failed to update bcn tmpl during vdev up: %d\n",
1912 			    ret);
1913 		return;
1914 	}
1915 
1916 	ahvif->aid = 0;
1917 
1918 	ether_addr_copy(arvif->bssid, info->addr);
1919 
1920 	params.vdev_id = arvif->vdev_id;
1921 	params.aid = ahvif->aid;
1922 	params.bssid = arvif->bssid;
1923 	params.tx_bssid = ath12k_mac_get_tx_bssid(arvif);
1924 	if (params.tx_bssid) {
1925 		params.nontx_profile_idx = info->bssid_index;
1926 		params.nontx_profile_cnt = 1 << info->bssid_indicator;
1927 	}
1928 	ret = ath12k_wmi_vdev_up(arvif->ar, &params);
1929 	if (ret) {
1930 		ath12k_warn(ar->ab, "failed to bring up vdev %d: %i\n",
1931 			    arvif->vdev_id, ret);
1932 		return;
1933 	}
1934 
1935 	arvif->is_up = true;
1936 
1937 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac vdev %d up\n", arvif->vdev_id);
1938 }
1939 
1940 static void ath12k_mac_handle_beacon_iter(void *data, u8 *mac,
1941 					  struct ieee80211_vif *vif)
1942 {
1943 	struct sk_buff *skb = data;
1944 	struct ieee80211_mgmt *mgmt = (void *)skb->data;
1945 	struct ath12k_vif *ahvif = ath12k_vif_to_ahvif(vif);
1946 	struct ath12k_link_vif *arvif = &ahvif->deflink;
1947 
1948 	if (vif->type != NL80211_IFTYPE_STATION || !arvif->is_created)
1949 		return;
1950 
1951 	if (!ether_addr_equal(mgmt->bssid, vif->bss_conf.bssid))
1952 		return;
1953 
1954 	cancel_delayed_work(&arvif->connection_loss_work);
1955 }
1956 
1957 void ath12k_mac_handle_beacon(struct ath12k *ar, struct sk_buff *skb)
1958 {
1959 	ieee80211_iterate_active_interfaces_atomic(ath12k_ar_to_hw(ar),
1960 						   IEEE80211_IFACE_ITER_NORMAL,
1961 						   ath12k_mac_handle_beacon_iter,
1962 						   skb);
1963 }
1964 
1965 void ath12k_mac_handle_beacon_miss(struct ath12k *ar,
1966 				   struct ath12k_link_vif *arvif)
1967 {
1968 	struct ieee80211_hw *hw = ath12k_ar_to_hw(ar);
1969 	struct ieee80211_vif *vif = ath12k_ahvif_to_vif(arvif->ahvif);
1970 
1971 	if (!(arvif->is_created && arvif->is_up))
1972 		return;
1973 
1974 	ieee80211_beacon_loss(vif);
1975 
1976 	/* Firmware doesn't report beacon loss events repeatedly. If AP probe
1977 	 * (done by mac80211) succeeds but beacons do not resume then it
1978 	 * doesn't make sense to continue operation. Queue connection loss work
1979 	 * which can be cancelled when beacon is received.
1980 	 */
1981 	ieee80211_queue_delayed_work(hw, &arvif->connection_loss_work,
1982 				     ATH12K_CONNECTION_LOSS_HZ);
1983 }
1984 
1985 static void ath12k_mac_vif_sta_connection_loss_work(struct work_struct *work)
1986 {
1987 	struct ath12k_link_vif *arvif = container_of(work, struct ath12k_link_vif,
1988 						     connection_loss_work.work);
1989 	struct ieee80211_vif *vif = arvif->ahvif->vif;
1990 
1991 	if (!arvif->is_up)
1992 		return;
1993 
1994 	ieee80211_connection_loss(vif);
1995 }
1996 
1997 static void ath12k_peer_assoc_h_basic(struct ath12k *ar,
1998 				      struct ath12k_link_vif *arvif,
1999 				      struct ath12k_link_sta *arsta,
2000 				      struct ath12k_wmi_peer_assoc_arg *arg)
2001 {
2002 	struct ieee80211_vif *vif = ath12k_ahvif_to_vif(arvif->ahvif);
2003 	struct ieee80211_sta *sta = ath12k_ahsta_to_sta(arsta->ahsta);
2004 	struct ieee80211_hw *hw = ath12k_ar_to_hw(ar);
2005 	struct ieee80211_bss_conf *bss_conf;
2006 	u32 aid;
2007 
2008 	lockdep_assert_wiphy(hw->wiphy);
2009 
2010 	if (vif->type == NL80211_IFTYPE_STATION)
2011 		aid = vif->cfg.aid;
2012 	else
2013 		aid = sta->aid;
2014 
2015 	ether_addr_copy(arg->peer_mac, arsta->addr);
2016 	arg->vdev_id = arvif->vdev_id;
2017 	arg->peer_associd = aid;
2018 	arg->auth_flag = true;
2019 	/* TODO: STA WAR in ath10k for listen interval required? */
2020 	arg->peer_listen_intval = hw->conf.listen_interval;
2021 	arg->peer_nss = 1;
2022 
2023 	bss_conf = ath12k_mac_get_link_bss_conf(arvif);
2024 	if (!bss_conf) {
2025 		ath12k_warn(ar->ab, "unable to access bss link conf in peer assoc for vif %pM link %u\n",
2026 			    vif->addr, arvif->link_id);
2027 		return;
2028 	}
2029 
2030 	arg->peer_caps = bss_conf->assoc_capability;
2031 }
2032 
2033 static void ath12k_peer_assoc_h_crypto(struct ath12k *ar,
2034 				       struct ath12k_link_vif *arvif,
2035 				       struct ath12k_link_sta *arsta,
2036 				       struct ath12k_wmi_peer_assoc_arg *arg)
2037 {
2038 	struct ieee80211_vif *vif = ath12k_ahvif_to_vif(arvif->ahvif);
2039 	struct ieee80211_sta *sta = ath12k_ahsta_to_sta(arsta->ahsta);
2040 	struct ieee80211_bss_conf *info;
2041 	struct cfg80211_chan_def def;
2042 	struct cfg80211_bss *bss;
2043 	struct ieee80211_hw *hw = ath12k_ar_to_hw(ar);
2044 	const u8 *rsnie = NULL;
2045 	const u8 *wpaie = NULL;
2046 
2047 	lockdep_assert_wiphy(hw->wiphy);
2048 
2049 	info = ath12k_mac_get_link_bss_conf(arvif);
2050 	if (!info) {
2051 		ath12k_warn(ar->ab, "unable to access bss link conf for peer assoc crypto for vif %pM link %u\n",
2052 			    vif->addr, arvif->link_id);
2053 		return;
2054 	}
2055 
2056 	if (WARN_ON(ath12k_mac_vif_link_chan(vif, arvif->link_id, &def)))
2057 		return;
2058 
2059 	bss = cfg80211_get_bss(hw->wiphy, def.chan, info->bssid, NULL, 0,
2060 			       IEEE80211_BSS_TYPE_ANY, IEEE80211_PRIVACY_ANY);
2061 
2062 	if (arvif->rsnie_present || arvif->wpaie_present) {
2063 		arg->need_ptk_4_way = true;
2064 		if (arvif->wpaie_present)
2065 			arg->need_gtk_2_way = true;
2066 	} else if (bss) {
2067 		const struct cfg80211_bss_ies *ies;
2068 
2069 		rcu_read_lock();
2070 		rsnie = ieee80211_bss_get_ie(bss, WLAN_EID_RSN);
2071 
2072 		ies = rcu_dereference(bss->ies);
2073 
2074 		wpaie = cfg80211_find_vendor_ie(WLAN_OUI_MICROSOFT,
2075 						WLAN_OUI_TYPE_MICROSOFT_WPA,
2076 						ies->data,
2077 						ies->len);
2078 		rcu_read_unlock();
2079 		cfg80211_put_bss(hw->wiphy, bss);
2080 	}
2081 
2082 	/* FIXME: base on RSN IE/WPA IE is a correct idea? */
2083 	if (rsnie || wpaie) {
2084 		ath12k_dbg(ar->ab, ATH12K_DBG_WMI,
2085 			   "%s: rsn ie found\n", __func__);
2086 		arg->need_ptk_4_way = true;
2087 	}
2088 
2089 	if (wpaie) {
2090 		ath12k_dbg(ar->ab, ATH12K_DBG_WMI,
2091 			   "%s: wpa ie found\n", __func__);
2092 		arg->need_gtk_2_way = true;
2093 	}
2094 
2095 	if (sta->mfp) {
2096 		/* TODO: Need to check if FW supports PMF? */
2097 		arg->is_pmf_enabled = true;
2098 	}
2099 
2100 	/* TODO: safe_mode_enabled (bypass 4-way handshake) flag req? */
2101 }
2102 
2103 static void ath12k_peer_assoc_h_rates(struct ath12k *ar,
2104 				      struct ath12k_link_vif *arvif,
2105 				      struct ath12k_link_sta *arsta,
2106 				      struct ath12k_wmi_peer_assoc_arg *arg)
2107 {
2108 	struct ieee80211_vif *vif = ath12k_ahvif_to_vif(arvif->ahvif);
2109 	struct ieee80211_sta *sta = ath12k_ahsta_to_sta(arsta->ahsta);
2110 	struct wmi_rate_set_arg *rateset = &arg->peer_legacy_rates;
2111 	struct ieee80211_link_sta *link_sta;
2112 	struct cfg80211_chan_def def;
2113 	const struct ieee80211_supported_band *sband;
2114 	const struct ieee80211_rate *rates;
2115 	struct ieee80211_hw *hw = ath12k_ar_to_hw(ar);
2116 	enum nl80211_band band;
2117 	u32 ratemask;
2118 	u8 rate;
2119 	int i;
2120 
2121 	lockdep_assert_wiphy(hw->wiphy);
2122 
2123 	if (WARN_ON(ath12k_mac_vif_link_chan(vif, arvif->link_id, &def)))
2124 		return;
2125 
2126 	link_sta = ath12k_mac_get_link_sta(arsta);
2127 	if (!link_sta) {
2128 		ath12k_warn(ar->ab, "unable to access link sta in peer assoc rates for sta %pM link %u\n",
2129 			    sta->addr, arsta->link_id);
2130 		return;
2131 	}
2132 
2133 	band = def.chan->band;
2134 	sband = hw->wiphy->bands[band];
2135 	ratemask = link_sta->supp_rates[band];
2136 	ratemask &= arvif->bitrate_mask.control[band].legacy;
2137 	rates = sband->bitrates;
2138 
2139 	rateset->num_rates = 0;
2140 
2141 	for (i = 0; i < 32; i++, ratemask >>= 1, rates++) {
2142 		if (!(ratemask & 1))
2143 			continue;
2144 
2145 		rate = ath12k_mac_bitrate_to_rate(rates->bitrate);
2146 		rateset->rates[rateset->num_rates] = rate;
2147 		rateset->num_rates++;
2148 	}
2149 }
2150 
2151 static bool
2152 ath12k_peer_assoc_h_ht_masked(const u8 *ht_mcs_mask)
2153 {
2154 	int nss;
2155 
2156 	for (nss = 0; nss < IEEE80211_HT_MCS_MASK_LEN; nss++)
2157 		if (ht_mcs_mask[nss])
2158 			return false;
2159 
2160 	return true;
2161 }
2162 
2163 static bool
2164 ath12k_peer_assoc_h_vht_masked(const u16 *vht_mcs_mask)
2165 {
2166 	int nss;
2167 
2168 	for (nss = 0; nss < NL80211_VHT_NSS_MAX; nss++)
2169 		if (vht_mcs_mask[nss])
2170 			return false;
2171 
2172 	return true;
2173 }
2174 
2175 static void ath12k_peer_assoc_h_ht(struct ath12k *ar,
2176 				   struct ath12k_link_vif *arvif,
2177 				   struct ath12k_link_sta *arsta,
2178 				   struct ath12k_wmi_peer_assoc_arg *arg)
2179 {
2180 	struct ieee80211_vif *vif = ath12k_ahvif_to_vif(arvif->ahvif);
2181 	struct ieee80211_sta *sta = ath12k_ahsta_to_sta(arsta->ahsta);
2182 	const struct ieee80211_sta_ht_cap *ht_cap;
2183 	struct ieee80211_link_sta *link_sta;
2184 	struct cfg80211_chan_def def;
2185 	enum nl80211_band band;
2186 	const u8 *ht_mcs_mask;
2187 	int i, n;
2188 	u8 max_nss;
2189 	u32 stbc;
2190 
2191 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
2192 
2193 	if (WARN_ON(ath12k_mac_vif_link_chan(vif, arvif->link_id, &def)))
2194 		return;
2195 
2196 	link_sta = ath12k_mac_get_link_sta(arsta);
2197 	if (!link_sta) {
2198 		ath12k_warn(ar->ab, "unable to access link sta in peer assoc ht for sta %pM link %u\n",
2199 			    sta->addr, arsta->link_id);
2200 		return;
2201 	}
2202 
2203 	ht_cap = &link_sta->ht_cap;
2204 	if (!ht_cap->ht_supported)
2205 		return;
2206 
2207 	band = def.chan->band;
2208 	ht_mcs_mask = arvif->bitrate_mask.control[band].ht_mcs;
2209 
2210 	if (ath12k_peer_assoc_h_ht_masked(ht_mcs_mask))
2211 		return;
2212 
2213 	arg->ht_flag = true;
2214 
2215 	arg->peer_max_mpdu = (1 << (IEEE80211_HT_MAX_AMPDU_FACTOR +
2216 				    ht_cap->ampdu_factor)) - 1;
2217 
2218 	arg->peer_mpdu_density =
2219 		ath12k_parse_mpdudensity(ht_cap->ampdu_density);
2220 
2221 	arg->peer_ht_caps = ht_cap->cap;
2222 	arg->peer_rate_caps |= WMI_HOST_RC_HT_FLAG;
2223 
2224 	if (ht_cap->cap & IEEE80211_HT_CAP_LDPC_CODING)
2225 		arg->ldpc_flag = true;
2226 
2227 	if (link_sta->bandwidth >= IEEE80211_STA_RX_BW_40) {
2228 		arg->bw_40 = true;
2229 		arg->peer_rate_caps |= WMI_HOST_RC_CW40_FLAG;
2230 	}
2231 
2232 	/* As firmware handles these two flags (IEEE80211_HT_CAP_SGI_20
2233 	 * and IEEE80211_HT_CAP_SGI_40) for enabling SGI, reset both
2234 	 * flags if guard interval is to force Long GI
2235 	 */
2236 	if (arvif->bitrate_mask.control[band].gi == NL80211_TXRATE_FORCE_LGI) {
2237 		arg->peer_ht_caps &= ~(IEEE80211_HT_CAP_SGI_20 | IEEE80211_HT_CAP_SGI_40);
2238 	} else {
2239 		/* Enable SGI flag if either SGI_20 or SGI_40 is supported */
2240 		if (ht_cap->cap & (IEEE80211_HT_CAP_SGI_20 | IEEE80211_HT_CAP_SGI_40))
2241 			arg->peer_rate_caps |= WMI_HOST_RC_SGI_FLAG;
2242 	}
2243 
2244 	if (ht_cap->cap & IEEE80211_HT_CAP_TX_STBC) {
2245 		arg->peer_rate_caps |= WMI_HOST_RC_TX_STBC_FLAG;
2246 		arg->stbc_flag = true;
2247 	}
2248 
2249 	if (ht_cap->cap & IEEE80211_HT_CAP_RX_STBC) {
2250 		stbc = ht_cap->cap & IEEE80211_HT_CAP_RX_STBC;
2251 		stbc = stbc >> IEEE80211_HT_CAP_RX_STBC_SHIFT;
2252 		stbc = stbc << WMI_HOST_RC_RX_STBC_FLAG_S;
2253 		arg->peer_rate_caps |= stbc;
2254 		arg->stbc_flag = true;
2255 	}
2256 
2257 	if (ht_cap->mcs.rx_mask[1] && ht_cap->mcs.rx_mask[2])
2258 		arg->peer_rate_caps |= WMI_HOST_RC_TS_FLAG;
2259 	else if (ht_cap->mcs.rx_mask[1])
2260 		arg->peer_rate_caps |= WMI_HOST_RC_DS_FLAG;
2261 
2262 	for (i = 0, n = 0, max_nss = 0; i < IEEE80211_HT_MCS_MASK_LEN * 8; i++)
2263 		if ((ht_cap->mcs.rx_mask[i / 8] & BIT(i % 8)) &&
2264 		    (ht_mcs_mask[i / 8] & BIT(i % 8))) {
2265 			max_nss = (i / 8) + 1;
2266 			arg->peer_ht_rates.rates[n++] = i;
2267 		}
2268 
2269 	/* This is a workaround for HT-enabled STAs which break the spec
2270 	 * and have no HT capabilities RX mask (no HT RX MCS map).
2271 	 *
2272 	 * As per spec, in section 20.3.5 Modulation and coding scheme (MCS),
2273 	 * MCS 0 through 7 are mandatory in 20MHz with 800 ns GI at all STAs.
2274 	 *
2275 	 * Firmware asserts if such situation occurs.
2276 	 */
2277 	if (n == 0) {
2278 		arg->peer_ht_rates.num_rates = 8;
2279 		for (i = 0; i < arg->peer_ht_rates.num_rates; i++)
2280 			arg->peer_ht_rates.rates[i] = i;
2281 	} else {
2282 		arg->peer_ht_rates.num_rates = n;
2283 		arg->peer_nss = min(link_sta->rx_nss, max_nss);
2284 	}
2285 
2286 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac ht peer %pM mcs cnt %d nss %d\n",
2287 		   arg->peer_mac,
2288 		   arg->peer_ht_rates.num_rates,
2289 		   arg->peer_nss);
2290 }
2291 
2292 static int ath12k_mac_get_max_vht_mcs_map(u16 mcs_map, int nss)
2293 {
2294 	switch ((mcs_map >> (2 * nss)) & 0x3) {
2295 	case IEEE80211_VHT_MCS_SUPPORT_0_7: return BIT(8) - 1;
2296 	case IEEE80211_VHT_MCS_SUPPORT_0_8: return BIT(9) - 1;
2297 	case IEEE80211_VHT_MCS_SUPPORT_0_9: return BIT(10) - 1;
2298 	}
2299 	return 0;
2300 }
2301 
2302 static u16
2303 ath12k_peer_assoc_h_vht_limit(u16 tx_mcs_set,
2304 			      const u16 vht_mcs_limit[NL80211_VHT_NSS_MAX])
2305 {
2306 	int idx_limit;
2307 	int nss;
2308 	u16 mcs_map;
2309 	u16 mcs;
2310 
2311 	for (nss = 0; nss < NL80211_VHT_NSS_MAX; nss++) {
2312 		mcs_map = ath12k_mac_get_max_vht_mcs_map(tx_mcs_set, nss) &
2313 			  vht_mcs_limit[nss];
2314 
2315 		if (mcs_map)
2316 			idx_limit = fls(mcs_map) - 1;
2317 		else
2318 			idx_limit = -1;
2319 
2320 		switch (idx_limit) {
2321 		case 0:
2322 		case 1:
2323 		case 2:
2324 		case 3:
2325 		case 4:
2326 		case 5:
2327 		case 6:
2328 		case 7:
2329 			mcs = IEEE80211_VHT_MCS_SUPPORT_0_7;
2330 			break;
2331 		case 8:
2332 			mcs = IEEE80211_VHT_MCS_SUPPORT_0_8;
2333 			break;
2334 		case 9:
2335 			mcs = IEEE80211_VHT_MCS_SUPPORT_0_9;
2336 			break;
2337 		default:
2338 			WARN_ON(1);
2339 			fallthrough;
2340 		case -1:
2341 			mcs = IEEE80211_VHT_MCS_NOT_SUPPORTED;
2342 			break;
2343 		}
2344 
2345 		tx_mcs_set &= ~(0x3 << (nss * 2));
2346 		tx_mcs_set |= mcs << (nss * 2);
2347 	}
2348 
2349 	return tx_mcs_set;
2350 }
2351 
2352 static u8 ath12k_get_nss_160mhz(struct ath12k *ar,
2353 				u8 max_nss)
2354 {
2355 	u8 nss_ratio_info = ar->pdev->cap.nss_ratio_info;
2356 	u8 max_sup_nss = 0;
2357 
2358 	switch (nss_ratio_info) {
2359 	case WMI_NSS_RATIO_1BY2_NSS:
2360 		max_sup_nss = max_nss >> 1;
2361 		break;
2362 	case WMI_NSS_RATIO_3BY4_NSS:
2363 		ath12k_warn(ar->ab, "WMI_NSS_RATIO_3BY4_NSS not supported\n");
2364 		break;
2365 	case WMI_NSS_RATIO_1_NSS:
2366 		max_sup_nss = max_nss;
2367 		break;
2368 	case WMI_NSS_RATIO_2_NSS:
2369 		ath12k_warn(ar->ab, "WMI_NSS_RATIO_2_NSS not supported\n");
2370 		break;
2371 	default:
2372 		ath12k_warn(ar->ab, "invalid nss ratio received from fw: %d\n",
2373 			    nss_ratio_info);
2374 		break;
2375 	}
2376 
2377 	return max_sup_nss;
2378 }
2379 
2380 static void ath12k_peer_assoc_h_vht(struct ath12k *ar,
2381 				    struct ath12k_link_vif *arvif,
2382 				    struct ath12k_link_sta *arsta,
2383 				    struct ath12k_wmi_peer_assoc_arg *arg)
2384 {
2385 	struct ieee80211_vif *vif = ath12k_ahvif_to_vif(arvif->ahvif);
2386 	struct ieee80211_sta *sta = ath12k_ahsta_to_sta(arsta->ahsta);
2387 	const struct ieee80211_sta_vht_cap *vht_cap;
2388 	struct ieee80211_link_sta *link_sta;
2389 	struct cfg80211_chan_def def;
2390 	enum nl80211_band band;
2391 	u16 *vht_mcs_mask;
2392 	u8 ampdu_factor;
2393 	u8 max_nss, vht_mcs;
2394 	int i, vht_nss, nss_idx;
2395 	bool user_rate_valid = true;
2396 	u32 rx_nss, tx_nss, nss_160;
2397 
2398 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
2399 
2400 	if (WARN_ON(ath12k_mac_vif_link_chan(vif, arvif->link_id, &def)))
2401 		return;
2402 
2403 	link_sta = ath12k_mac_get_link_sta(arsta);
2404 	if (!link_sta) {
2405 		ath12k_warn(ar->ab, "unable to access link sta in peer assoc vht for sta %pM link %u\n",
2406 			    sta->addr, arsta->link_id);
2407 		return;
2408 	}
2409 
2410 	vht_cap = &link_sta->vht_cap;
2411 	if (!vht_cap->vht_supported)
2412 		return;
2413 
2414 	band = def.chan->band;
2415 	vht_mcs_mask = arvif->bitrate_mask.control[band].vht_mcs;
2416 
2417 	if (ath12k_peer_assoc_h_vht_masked(vht_mcs_mask))
2418 		return;
2419 
2420 	arg->vht_flag = true;
2421 
2422 	/* TODO: similar flags required? */
2423 	arg->vht_capable = true;
2424 
2425 	if (def.chan->band == NL80211_BAND_2GHZ)
2426 		arg->vht_ng_flag = true;
2427 
2428 	arg->peer_vht_caps = vht_cap->cap;
2429 
2430 	ampdu_factor = (vht_cap->cap &
2431 			IEEE80211_VHT_CAP_MAX_A_MPDU_LENGTH_EXPONENT_MASK) >>
2432 		       IEEE80211_VHT_CAP_MAX_A_MPDU_LENGTH_EXPONENT_SHIFT;
2433 
2434 	/* Workaround: Some Netgear/Linksys 11ac APs set Rx A-MPDU factor to
2435 	 * zero in VHT IE. Using it would result in degraded throughput.
2436 	 * arg->peer_max_mpdu at this point contains HT max_mpdu so keep
2437 	 * it if VHT max_mpdu is smaller.
2438 	 */
2439 	arg->peer_max_mpdu = max(arg->peer_max_mpdu,
2440 				 (1U << (IEEE80211_HT_MAX_AMPDU_FACTOR +
2441 					ampdu_factor)) - 1);
2442 
2443 	if (link_sta->bandwidth == IEEE80211_STA_RX_BW_80)
2444 		arg->bw_80 = true;
2445 
2446 	if (link_sta->bandwidth == IEEE80211_STA_RX_BW_160)
2447 		arg->bw_160 = true;
2448 
2449 	vht_nss =  ath12k_mac_max_vht_nss(vht_mcs_mask);
2450 
2451 	if (vht_nss > link_sta->rx_nss) {
2452 		user_rate_valid = false;
2453 		for (nss_idx = link_sta->rx_nss - 1; nss_idx >= 0; nss_idx--) {
2454 			if (vht_mcs_mask[nss_idx]) {
2455 				user_rate_valid = true;
2456 				break;
2457 			}
2458 		}
2459 	}
2460 
2461 	if (!user_rate_valid) {
2462 		ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
2463 			   "Setting vht range MCS value to peer supported nss:%d for peer %pM\n",
2464 			   link_sta->rx_nss, arsta->addr);
2465 		vht_mcs_mask[link_sta->rx_nss - 1] = vht_mcs_mask[vht_nss - 1];
2466 	}
2467 
2468 	/* Calculate peer NSS capability from VHT capabilities if STA
2469 	 * supports VHT.
2470 	 */
2471 	for (i = 0, max_nss = 0, vht_mcs = 0; i < NL80211_VHT_NSS_MAX; i++) {
2472 		vht_mcs = __le16_to_cpu(vht_cap->vht_mcs.rx_mcs_map) >>
2473 			  (2 * i) & 3;
2474 
2475 		if (vht_mcs != IEEE80211_VHT_MCS_NOT_SUPPORTED &&
2476 		    vht_mcs_mask[i])
2477 			max_nss = i + 1;
2478 	}
2479 	arg->peer_nss = min(link_sta->rx_nss, max_nss);
2480 	arg->rx_max_rate = __le16_to_cpu(vht_cap->vht_mcs.rx_highest);
2481 	arg->rx_mcs_set = __le16_to_cpu(vht_cap->vht_mcs.rx_mcs_map);
2482 	arg->rx_mcs_set = ath12k_peer_assoc_h_vht_limit(arg->rx_mcs_set, vht_mcs_mask);
2483 
2484 	arg->tx_max_rate = __le16_to_cpu(vht_cap->vht_mcs.tx_highest);
2485 	arg->tx_mcs_set = __le16_to_cpu(vht_cap->vht_mcs.tx_mcs_map);
2486 
2487 	/* In QCN9274 platform, VHT MCS rate 10 and 11 is enabled by default.
2488 	 * VHT MCS rate 10 and 11 is not supported in 11ac standard.
2489 	 * so explicitly disable the VHT MCS rate 10 and 11 in 11ac mode.
2490 	 */
2491 	arg->tx_mcs_set &= ~IEEE80211_VHT_MCS_SUPPORT_0_11_MASK;
2492 	arg->tx_mcs_set |= IEEE80211_DISABLE_VHT_MCS_SUPPORT_0_11;
2493 
2494 	if ((arg->tx_mcs_set & IEEE80211_VHT_MCS_NOT_SUPPORTED) ==
2495 			IEEE80211_VHT_MCS_NOT_SUPPORTED)
2496 		arg->peer_vht_caps &= ~IEEE80211_VHT_CAP_MU_BEAMFORMEE_CAPABLE;
2497 
2498 	/* TODO:  Check */
2499 	arg->tx_max_mcs_nss = 0xFF;
2500 
2501 	if (arg->peer_phymode == MODE_11AC_VHT160) {
2502 		tx_nss = ath12k_get_nss_160mhz(ar, max_nss);
2503 		rx_nss = min(arg->peer_nss, tx_nss);
2504 		arg->peer_bw_rxnss_override = ATH12K_BW_NSS_MAP_ENABLE;
2505 
2506 		if (!rx_nss) {
2507 			ath12k_warn(ar->ab, "invalid max_nss\n");
2508 			return;
2509 		}
2510 
2511 		nss_160 = u32_encode_bits(rx_nss - 1, ATH12K_PEER_RX_NSS_160MHZ);
2512 		arg->peer_bw_rxnss_override |= nss_160;
2513 	}
2514 
2515 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
2516 		   "mac vht peer %pM max_mpdu %d flags 0x%x nss_override 0x%x\n",
2517 		   arsta->addr, arg->peer_max_mpdu, arg->peer_flags,
2518 		   arg->peer_bw_rxnss_override);
2519 }
2520 
2521 static int ath12k_mac_get_max_he_mcs_map(u16 mcs_map, int nss)
2522 {
2523 	switch ((mcs_map >> (2 * nss)) & 0x3) {
2524 	case IEEE80211_HE_MCS_SUPPORT_0_7: return BIT(8) - 1;
2525 	case IEEE80211_HE_MCS_SUPPORT_0_9: return BIT(10) - 1;
2526 	case IEEE80211_HE_MCS_SUPPORT_0_11: return BIT(12) - 1;
2527 	}
2528 	return 0;
2529 }
2530 
2531 static u16 ath12k_peer_assoc_h_he_limit(u16 tx_mcs_set,
2532 					const u16 *he_mcs_limit)
2533 {
2534 	int idx_limit;
2535 	int nss;
2536 	u16 mcs_map;
2537 	u16 mcs;
2538 
2539 	for (nss = 0; nss < NL80211_HE_NSS_MAX; nss++) {
2540 		mcs_map = ath12k_mac_get_max_he_mcs_map(tx_mcs_set, nss) &
2541 			he_mcs_limit[nss];
2542 
2543 		if (mcs_map)
2544 			idx_limit = fls(mcs_map) - 1;
2545 		else
2546 			idx_limit = -1;
2547 
2548 		switch (idx_limit) {
2549 		case 0 ... 7:
2550 			mcs = IEEE80211_HE_MCS_SUPPORT_0_7;
2551 			break;
2552 		case 8:
2553 		case 9:
2554 			mcs = IEEE80211_HE_MCS_SUPPORT_0_9;
2555 			break;
2556 		case 10:
2557 		case 11:
2558 			mcs = IEEE80211_HE_MCS_SUPPORT_0_11;
2559 			break;
2560 		default:
2561 			WARN_ON(1);
2562 			fallthrough;
2563 		case -1:
2564 			mcs = IEEE80211_HE_MCS_NOT_SUPPORTED;
2565 			break;
2566 		}
2567 
2568 		tx_mcs_set &= ~(0x3 << (nss * 2));
2569 		tx_mcs_set |= mcs << (nss * 2);
2570 	}
2571 
2572 	return tx_mcs_set;
2573 }
2574 
2575 static bool
2576 ath12k_peer_assoc_h_he_masked(const u16 he_mcs_mask[NL80211_HE_NSS_MAX])
2577 {
2578 	int nss;
2579 
2580 	for (nss = 0; nss < NL80211_HE_NSS_MAX; nss++)
2581 		if (he_mcs_mask[nss])
2582 			return false;
2583 
2584 	return true;
2585 }
2586 
2587 static void ath12k_peer_assoc_h_he(struct ath12k *ar,
2588 				   struct ath12k_link_vif *arvif,
2589 				   struct ath12k_link_sta *arsta,
2590 				   struct ath12k_wmi_peer_assoc_arg *arg)
2591 {
2592 	struct ieee80211_vif *vif = ath12k_ahvif_to_vif(arvif->ahvif);
2593 	struct ieee80211_sta *sta = ath12k_ahsta_to_sta(arsta->ahsta);
2594 	const struct ieee80211_sta_he_cap *he_cap;
2595 	struct ieee80211_bss_conf *link_conf;
2596 	struct ieee80211_link_sta *link_sta;
2597 	struct cfg80211_chan_def def;
2598 	int i;
2599 	u8 ampdu_factor, max_nss;
2600 	u8 rx_mcs_80 = IEEE80211_HE_MCS_NOT_SUPPORTED;
2601 	u8 rx_mcs_160 = IEEE80211_HE_MCS_NOT_SUPPORTED;
2602 	u16 mcs_160_map, mcs_80_map;
2603 	u8 link_id = arvif->link_id;
2604 	bool support_160;
2605 	enum nl80211_band band;
2606 	u16 *he_mcs_mask;
2607 	u8 he_mcs;
2608 	u16 he_tx_mcs = 0, v = 0;
2609 	int he_nss, nss_idx;
2610 	bool user_rate_valid = true;
2611 	u32 rx_nss, tx_nss, nss_160;
2612 
2613 	if (WARN_ON(ath12k_mac_vif_link_chan(vif, link_id, &def)))
2614 		return;
2615 
2616 	link_conf = ath12k_mac_get_link_bss_conf(arvif);
2617 	if (!link_conf) {
2618 		ath12k_warn(ar->ab, "unable to access bss link conf in peer assoc he for vif %pM link %u",
2619 			    vif->addr, link_id);
2620 		return;
2621 	}
2622 
2623 	link_sta = ath12k_mac_get_link_sta(arsta);
2624 	if (!link_sta) {
2625 		ath12k_warn(ar->ab, "unable to access link sta in peer assoc he for sta %pM link %u\n",
2626 			    sta->addr, arsta->link_id);
2627 		return;
2628 	}
2629 
2630 	he_cap = &link_sta->he_cap;
2631 	if (!he_cap->has_he)
2632 		return;
2633 
2634 	band = def.chan->band;
2635 	he_mcs_mask = arvif->bitrate_mask.control[band].he_mcs;
2636 
2637 	if (ath12k_peer_assoc_h_he_masked(he_mcs_mask))
2638 		return;
2639 
2640 	arg->he_flag = true;
2641 
2642 	support_160 = !!(he_cap->he_cap_elem.phy_cap_info[0] &
2643 		  IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_160MHZ_IN_5G);
2644 
2645 	/* Supported HE-MCS and NSS Set of peer he_cap is intersection with self he_cp */
2646 	mcs_160_map = le16_to_cpu(he_cap->he_mcs_nss_supp.rx_mcs_160);
2647 	mcs_80_map = le16_to_cpu(he_cap->he_mcs_nss_supp.rx_mcs_80);
2648 
2649 	if (support_160) {
2650 		for (i = 7; i >= 0; i--) {
2651 			u8 mcs_160 = (mcs_160_map >> (2 * i)) & 3;
2652 
2653 			if (mcs_160 != IEEE80211_HE_MCS_NOT_SUPPORTED) {
2654 				rx_mcs_160 = i + 1;
2655 				break;
2656 			}
2657 		}
2658 	}
2659 
2660 	for (i = 7; i >= 0; i--) {
2661 		u8 mcs_80 = (mcs_80_map >> (2 * i)) & 3;
2662 
2663 		if (mcs_80 != IEEE80211_HE_MCS_NOT_SUPPORTED) {
2664 			rx_mcs_80 = i + 1;
2665 			break;
2666 		}
2667 	}
2668 
2669 	if (support_160)
2670 		max_nss = min(rx_mcs_80, rx_mcs_160);
2671 	else
2672 		max_nss = rx_mcs_80;
2673 
2674 	arg->peer_nss = min(link_sta->rx_nss, max_nss);
2675 
2676 	memcpy(&arg->peer_he_cap_macinfo, he_cap->he_cap_elem.mac_cap_info,
2677 	       sizeof(he_cap->he_cap_elem.mac_cap_info));
2678 	memcpy(&arg->peer_he_cap_phyinfo, he_cap->he_cap_elem.phy_cap_info,
2679 	       sizeof(he_cap->he_cap_elem.phy_cap_info));
2680 	arg->peer_he_ops = link_conf->he_oper.params;
2681 
2682 	/* the top most byte is used to indicate BSS color info */
2683 	arg->peer_he_ops &= 0xffffff;
2684 
2685 	/* As per section 26.6.1 IEEE Std 802.11ax‐2022, if the Max AMPDU
2686 	 * Exponent Extension in HE cap is zero, use the arg->peer_max_mpdu
2687 	 * as calculated while parsing VHT caps(if VHT caps is present)
2688 	 * or HT caps (if VHT caps is not present).
2689 	 *
2690 	 * For non-zero value of Max AMPDU Exponent Extension in HE MAC caps,
2691 	 * if a HE STA sends VHT cap and HE cap IE in assoc request then, use
2692 	 * MAX_AMPDU_LEN_FACTOR as 20 to calculate max_ampdu length.
2693 	 * If a HE STA that does not send VHT cap, but HE and HT cap in assoc
2694 	 * request, then use MAX_AMPDU_LEN_FACTOR as 16 to calculate max_ampdu
2695 	 * length.
2696 	 */
2697 	ampdu_factor = u8_get_bits(he_cap->he_cap_elem.mac_cap_info[3],
2698 				   IEEE80211_HE_MAC_CAP3_MAX_AMPDU_LEN_EXP_MASK);
2699 
2700 	if (ampdu_factor) {
2701 		if (link_sta->vht_cap.vht_supported)
2702 			arg->peer_max_mpdu = (1 << (IEEE80211_HE_VHT_MAX_AMPDU_FACTOR +
2703 						    ampdu_factor)) - 1;
2704 		else if (link_sta->ht_cap.ht_supported)
2705 			arg->peer_max_mpdu = (1 << (IEEE80211_HE_HT_MAX_AMPDU_FACTOR +
2706 						    ampdu_factor)) - 1;
2707 	}
2708 
2709 	if (he_cap->he_cap_elem.phy_cap_info[6] &
2710 	    IEEE80211_HE_PHY_CAP6_PPE_THRESHOLD_PRESENT) {
2711 		int bit = 7;
2712 		int nss, ru;
2713 
2714 		arg->peer_ppet.numss_m1 = he_cap->ppe_thres[0] &
2715 					  IEEE80211_PPE_THRES_NSS_MASK;
2716 		arg->peer_ppet.ru_bit_mask =
2717 			(he_cap->ppe_thres[0] &
2718 			 IEEE80211_PPE_THRES_RU_INDEX_BITMASK_MASK) >>
2719 			IEEE80211_PPE_THRES_RU_INDEX_BITMASK_POS;
2720 
2721 		for (nss = 0; nss <= arg->peer_ppet.numss_m1; nss++) {
2722 			for (ru = 0; ru < 4; ru++) {
2723 				u32 val = 0;
2724 				int i;
2725 
2726 				if ((arg->peer_ppet.ru_bit_mask & BIT(ru)) == 0)
2727 					continue;
2728 				for (i = 0; i < 6; i++) {
2729 					val >>= 1;
2730 					val |= ((he_cap->ppe_thres[bit / 8] >>
2731 						 (bit % 8)) & 0x1) << 5;
2732 					bit++;
2733 				}
2734 				arg->peer_ppet.ppet16_ppet8_ru3_ru0[nss] |=
2735 								val << (ru * 6);
2736 			}
2737 		}
2738 	}
2739 
2740 	if (he_cap->he_cap_elem.mac_cap_info[0] & IEEE80211_HE_MAC_CAP0_TWT_RES)
2741 		arg->twt_responder = true;
2742 	if (he_cap->he_cap_elem.mac_cap_info[0] & IEEE80211_HE_MAC_CAP0_TWT_REQ)
2743 		arg->twt_requester = true;
2744 
2745 	he_nss = ath12k_mac_max_he_nss(he_mcs_mask);
2746 
2747 	if (he_nss > link_sta->rx_nss) {
2748 		user_rate_valid = false;
2749 		for (nss_idx = link_sta->rx_nss - 1; nss_idx >= 0; nss_idx--) {
2750 			if (he_mcs_mask[nss_idx]) {
2751 				user_rate_valid = true;
2752 				break;
2753 			}
2754 		}
2755 	}
2756 
2757 	if (!user_rate_valid) {
2758 		ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
2759 			   "Setting he range MCS value to peer supported nss:%d for peer %pM\n",
2760 			   link_sta->rx_nss, arsta->addr);
2761 		he_mcs_mask[link_sta->rx_nss - 1] = he_mcs_mask[he_nss - 1];
2762 	}
2763 
2764 	switch (link_sta->bandwidth) {
2765 	case IEEE80211_STA_RX_BW_160:
2766 		v = le16_to_cpu(he_cap->he_mcs_nss_supp.rx_mcs_160);
2767 		v = ath12k_peer_assoc_h_he_limit(v, he_mcs_mask);
2768 		arg->peer_he_rx_mcs_set[WMI_HECAP_TXRX_MCS_NSS_IDX_160] = v;
2769 
2770 		v = le16_to_cpu(he_cap->he_mcs_nss_supp.tx_mcs_160);
2771 		arg->peer_he_tx_mcs_set[WMI_HECAP_TXRX_MCS_NSS_IDX_160] = v;
2772 
2773 		arg->peer_he_mcs_count++;
2774 		if (!he_tx_mcs)
2775 			he_tx_mcs = v;
2776 		fallthrough;
2777 
2778 	default:
2779 		v = le16_to_cpu(he_cap->he_mcs_nss_supp.rx_mcs_80);
2780 		v = ath12k_peer_assoc_h_he_limit(v, he_mcs_mask);
2781 		arg->peer_he_rx_mcs_set[WMI_HECAP_TXRX_MCS_NSS_IDX_80] = v;
2782 
2783 		v = le16_to_cpu(he_cap->he_mcs_nss_supp.tx_mcs_80);
2784 		arg->peer_he_tx_mcs_set[WMI_HECAP_TXRX_MCS_NSS_IDX_80] = v;
2785 
2786 		arg->peer_he_mcs_count++;
2787 		if (!he_tx_mcs)
2788 			he_tx_mcs = v;
2789 		break;
2790 	}
2791 
2792 	/* Calculate peer NSS capability from HE capabilities if STA
2793 	 * supports HE.
2794 	 */
2795 	for (i = 0, max_nss = 0, he_mcs = 0; i < NL80211_HE_NSS_MAX; i++) {
2796 		he_mcs = he_tx_mcs >> (2 * i) & 3;
2797 
2798 		/* In case of fixed rates, MCS Range in he_tx_mcs might have
2799 		 * unsupported range, with he_mcs_mask set, so check either of them
2800 		 * to find nss.
2801 		 */
2802 		if (he_mcs != IEEE80211_HE_MCS_NOT_SUPPORTED ||
2803 		    he_mcs_mask[i])
2804 			max_nss = i + 1;
2805 	}
2806 
2807 	max_nss = min(max_nss, ar->num_tx_chains);
2808 	arg->peer_nss = min(link_sta->rx_nss, max_nss);
2809 
2810 	if (arg->peer_phymode == MODE_11AX_HE160) {
2811 		tx_nss = ath12k_get_nss_160mhz(ar, ar->num_tx_chains);
2812 		rx_nss = min(arg->peer_nss, tx_nss);
2813 
2814 		arg->peer_nss = min(link_sta->rx_nss, ar->num_rx_chains);
2815 		arg->peer_bw_rxnss_override = ATH12K_BW_NSS_MAP_ENABLE;
2816 
2817 		if (!rx_nss) {
2818 			ath12k_warn(ar->ab, "invalid max_nss\n");
2819 			return;
2820 		}
2821 
2822 		nss_160 = u32_encode_bits(rx_nss - 1, ATH12K_PEER_RX_NSS_160MHZ);
2823 		arg->peer_bw_rxnss_override |= nss_160;
2824 	}
2825 
2826 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
2827 		   "mac he peer %pM nss %d mcs cnt %d nss_override 0x%x\n",
2828 		   arsta->addr, arg->peer_nss,
2829 		   arg->peer_he_mcs_count,
2830 		   arg->peer_bw_rxnss_override);
2831 }
2832 
2833 static void ath12k_peer_assoc_h_he_6ghz(struct ath12k *ar,
2834 					struct ath12k_link_vif *arvif,
2835 					struct ath12k_link_sta *arsta,
2836 					struct ath12k_wmi_peer_assoc_arg *arg)
2837 {
2838 	struct ieee80211_vif *vif = ath12k_ahvif_to_vif(arvif->ahvif);
2839 	struct ieee80211_sta *sta = ath12k_ahsta_to_sta(arsta->ahsta);
2840 	const struct ieee80211_sta_he_cap *he_cap;
2841 	struct ieee80211_link_sta *link_sta;
2842 	struct cfg80211_chan_def def;
2843 	enum nl80211_band band;
2844 	u8 ampdu_factor, mpdu_density;
2845 
2846 	if (WARN_ON(ath12k_mac_vif_link_chan(vif, arvif->link_id, &def)))
2847 		return;
2848 
2849 	band = def.chan->band;
2850 
2851 	link_sta = ath12k_mac_get_link_sta(arsta);
2852 	if (!link_sta) {
2853 		ath12k_warn(ar->ab, "unable to access link sta in peer assoc he 6ghz for sta %pM link %u\n",
2854 			    sta->addr, arsta->link_id);
2855 		return;
2856 	}
2857 
2858 	he_cap = &link_sta->he_cap;
2859 
2860 	if (!arg->he_flag || band != NL80211_BAND_6GHZ || !link_sta->he_6ghz_capa.capa)
2861 		return;
2862 
2863 	if (link_sta->bandwidth == IEEE80211_STA_RX_BW_40)
2864 		arg->bw_40 = true;
2865 
2866 	if (link_sta->bandwidth == IEEE80211_STA_RX_BW_80)
2867 		arg->bw_80 = true;
2868 
2869 	if (link_sta->bandwidth == IEEE80211_STA_RX_BW_160)
2870 		arg->bw_160 = true;
2871 
2872 	if (link_sta->bandwidth == IEEE80211_STA_RX_BW_320)
2873 		arg->bw_320 = true;
2874 
2875 	arg->peer_he_caps_6ghz = le16_to_cpu(link_sta->he_6ghz_capa.capa);
2876 
2877 	mpdu_density = u32_get_bits(arg->peer_he_caps_6ghz,
2878 				    IEEE80211_HE_6GHZ_CAP_MIN_MPDU_START);
2879 	arg->peer_mpdu_density = ath12k_parse_mpdudensity(mpdu_density);
2880 
2881 	/* From IEEE Std 802.11ax-2021 - Section 10.12.2: An HE STA shall be capable of
2882 	 * receiving A-MPDU where the A-MPDU pre-EOF padding length is up to the value
2883 	 * indicated by the Maximum A-MPDU Length Exponent Extension field in the HE
2884 	 * Capabilities element and the Maximum A-MPDU Length Exponent field in HE 6 GHz
2885 	 * Band Capabilities element in the 6 GHz band.
2886 	 *
2887 	 * Here, we are extracting the Max A-MPDU Exponent Extension from HE caps and
2888 	 * factor is the Maximum A-MPDU Length Exponent from HE 6 GHZ Band capability.
2889 	 */
2890 	ampdu_factor = u8_get_bits(he_cap->he_cap_elem.mac_cap_info[3],
2891 				   IEEE80211_HE_MAC_CAP3_MAX_AMPDU_LEN_EXP_MASK) +
2892 			u32_get_bits(arg->peer_he_caps_6ghz,
2893 				     IEEE80211_HE_6GHZ_CAP_MAX_AMPDU_LEN_EXP);
2894 
2895 	arg->peer_max_mpdu = (1u << (IEEE80211_HE_6GHZ_MAX_AMPDU_FACTOR +
2896 				     ampdu_factor)) - 1;
2897 }
2898 
2899 static int ath12k_get_smps_from_capa(const struct ieee80211_sta_ht_cap *ht_cap,
2900 				     const struct ieee80211_he_6ghz_capa *he_6ghz_capa,
2901 				     int *smps)
2902 {
2903 	if (ht_cap->ht_supported)
2904 		*smps = u16_get_bits(ht_cap->cap, IEEE80211_HT_CAP_SM_PS);
2905 	else
2906 		*smps = le16_get_bits(he_6ghz_capa->capa,
2907 				      IEEE80211_HE_6GHZ_CAP_SM_PS);
2908 
2909 	if (*smps >= ARRAY_SIZE(ath12k_smps_map))
2910 		return -EINVAL;
2911 
2912 	return 0;
2913 }
2914 
2915 static void ath12k_peer_assoc_h_smps(struct ath12k_link_sta *arsta,
2916 				     struct ath12k_wmi_peer_assoc_arg *arg)
2917 {
2918 	struct ieee80211_sta *sta = ath12k_ahsta_to_sta(arsta->ahsta);
2919 	const struct ieee80211_he_6ghz_capa *he_6ghz_capa;
2920 	struct ath12k_link_vif *arvif = arsta->arvif;
2921 	const struct ieee80211_sta_ht_cap *ht_cap;
2922 	struct ieee80211_link_sta *link_sta;
2923 	struct ath12k *ar = arvif->ar;
2924 	int smps;
2925 
2926 	link_sta = ath12k_mac_get_link_sta(arsta);
2927 	if (!link_sta) {
2928 		ath12k_warn(ar->ab, "unable to access link sta in peer assoc he for sta %pM link %u\n",
2929 			    sta->addr, arsta->link_id);
2930 		return;
2931 	}
2932 
2933 	he_6ghz_capa = &link_sta->he_6ghz_capa;
2934 	ht_cap = &link_sta->ht_cap;
2935 
2936 	if (!ht_cap->ht_supported && !he_6ghz_capa->capa)
2937 		return;
2938 
2939 	if (ath12k_get_smps_from_capa(ht_cap, he_6ghz_capa, &smps))
2940 		return;
2941 
2942 	switch (smps) {
2943 	case WLAN_HT_CAP_SM_PS_STATIC:
2944 		arg->static_mimops_flag = true;
2945 		break;
2946 	case WLAN_HT_CAP_SM_PS_DYNAMIC:
2947 		arg->dynamic_mimops_flag = true;
2948 		break;
2949 	case WLAN_HT_CAP_SM_PS_DISABLED:
2950 		arg->spatial_mux_flag = true;
2951 		break;
2952 	default:
2953 		break;
2954 	}
2955 }
2956 
2957 static void ath12k_peer_assoc_h_qos(struct ath12k *ar,
2958 				    struct ath12k_link_vif *arvif,
2959 				    struct ath12k_link_sta *arsta,
2960 				    struct ath12k_wmi_peer_assoc_arg *arg)
2961 {
2962 	struct ieee80211_sta *sta = ath12k_ahsta_to_sta(arsta->ahsta);
2963 
2964 	switch (arvif->ahvif->vdev_type) {
2965 	case WMI_VDEV_TYPE_AP:
2966 		if (sta->wme) {
2967 			/* TODO: Check WME vs QoS */
2968 			arg->is_wme_set = true;
2969 			arg->qos_flag = true;
2970 		}
2971 
2972 		if (sta->wme && sta->uapsd_queues) {
2973 			/* TODO: Check WME vs QoS */
2974 			arg->is_wme_set = true;
2975 			arg->apsd_flag = true;
2976 			arg->peer_rate_caps |= WMI_HOST_RC_UAPSD_FLAG;
2977 		}
2978 		break;
2979 	case WMI_VDEV_TYPE_STA:
2980 		if (sta->wme) {
2981 			arg->is_wme_set = true;
2982 			arg->qos_flag = true;
2983 		}
2984 		break;
2985 	default:
2986 		break;
2987 	}
2988 
2989 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac peer %pM qos %d\n",
2990 		   arsta->addr, arg->qos_flag);
2991 }
2992 
2993 static int ath12k_peer_assoc_qos_ap(struct ath12k *ar,
2994 				    struct ath12k_link_vif *arvif,
2995 				    struct ath12k_link_sta *arsta)
2996 {
2997 	struct ieee80211_sta *sta = ath12k_ahsta_to_sta(arsta->ahsta);
2998 	struct ath12k_wmi_ap_ps_arg arg;
2999 	u32 max_sp;
3000 	u32 uapsd;
3001 	int ret;
3002 
3003 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
3004 
3005 	arg.vdev_id = arvif->vdev_id;
3006 
3007 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac uapsd_queues 0x%x max_sp %d\n",
3008 		   sta->uapsd_queues, sta->max_sp);
3009 
3010 	uapsd = 0;
3011 	if (sta->uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_VO)
3012 		uapsd |= WMI_AP_PS_UAPSD_AC3_DELIVERY_EN |
3013 			 WMI_AP_PS_UAPSD_AC3_TRIGGER_EN;
3014 	if (sta->uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_VI)
3015 		uapsd |= WMI_AP_PS_UAPSD_AC2_DELIVERY_EN |
3016 			 WMI_AP_PS_UAPSD_AC2_TRIGGER_EN;
3017 	if (sta->uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_BK)
3018 		uapsd |= WMI_AP_PS_UAPSD_AC1_DELIVERY_EN |
3019 			 WMI_AP_PS_UAPSD_AC1_TRIGGER_EN;
3020 	if (sta->uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_BE)
3021 		uapsd |= WMI_AP_PS_UAPSD_AC0_DELIVERY_EN |
3022 			 WMI_AP_PS_UAPSD_AC0_TRIGGER_EN;
3023 
3024 	max_sp = 0;
3025 	if (sta->max_sp < MAX_WMI_AP_PS_PEER_PARAM_MAX_SP)
3026 		max_sp = sta->max_sp;
3027 
3028 	arg.param = WMI_AP_PS_PEER_PARAM_UAPSD;
3029 	arg.value = uapsd;
3030 	ret = ath12k_wmi_send_set_ap_ps_param_cmd(ar, arsta->addr, &arg);
3031 	if (ret)
3032 		goto err;
3033 
3034 	arg.param = WMI_AP_PS_PEER_PARAM_MAX_SP;
3035 	arg.value = max_sp;
3036 	ret = ath12k_wmi_send_set_ap_ps_param_cmd(ar, arsta->addr, &arg);
3037 	if (ret)
3038 		goto err;
3039 
3040 	/* TODO: revisit during testing */
3041 	arg.param = WMI_AP_PS_PEER_PARAM_SIFS_RESP_FRMTYPE;
3042 	arg.value = DISABLE_SIFS_RESPONSE_TRIGGER;
3043 	ret = ath12k_wmi_send_set_ap_ps_param_cmd(ar, arsta->addr, &arg);
3044 	if (ret)
3045 		goto err;
3046 
3047 	arg.param = WMI_AP_PS_PEER_PARAM_SIFS_RESP_UAPSD;
3048 	arg.value = DISABLE_SIFS_RESPONSE_TRIGGER;
3049 	ret = ath12k_wmi_send_set_ap_ps_param_cmd(ar, arsta->addr, &arg);
3050 	if (ret)
3051 		goto err;
3052 
3053 	return 0;
3054 
3055 err:
3056 	ath12k_warn(ar->ab, "failed to set ap ps peer param %d for vdev %i: %d\n",
3057 		    arg.param, arvif->vdev_id, ret);
3058 	return ret;
3059 }
3060 
3061 static bool ath12k_mac_sta_has_ofdm_only(struct ieee80211_link_sta *sta)
3062 {
3063 	return sta->supp_rates[NL80211_BAND_2GHZ] >>
3064 	       ATH12K_MAC_FIRST_OFDM_RATE_IDX;
3065 }
3066 
3067 static enum wmi_phy_mode ath12k_mac_get_phymode_vht(struct ath12k *ar,
3068 						    struct ieee80211_link_sta *link_sta)
3069 {
3070 	if (link_sta->bandwidth == IEEE80211_STA_RX_BW_160) {
3071 		if (link_sta->vht_cap.cap & (IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_160MHZ |
3072 		    IEEE80211_VHT_CAP_EXT_NSS_BW_MASK))
3073 			return MODE_11AC_VHT160;
3074 
3075 		/* Allow STA to connect even if it does not explicitly advertise 160 MHz
3076 		 * support
3077 		 */
3078 		return MODE_11AC_VHT160;
3079 	}
3080 
3081 	if (link_sta->bandwidth == IEEE80211_STA_RX_BW_80)
3082 		return MODE_11AC_VHT80;
3083 
3084 	if (link_sta->bandwidth == IEEE80211_STA_RX_BW_40)
3085 		return MODE_11AC_VHT40;
3086 
3087 	if (link_sta->bandwidth == IEEE80211_STA_RX_BW_20)
3088 		return MODE_11AC_VHT20;
3089 
3090 	return MODE_UNKNOWN;
3091 }
3092 
3093 static enum wmi_phy_mode ath12k_mac_get_phymode_he(struct ath12k *ar,
3094 						   struct ieee80211_link_sta *link_sta)
3095 {
3096 	if (link_sta->bandwidth == IEEE80211_STA_RX_BW_160) {
3097 		if (link_sta->he_cap.he_cap_elem.phy_cap_info[0] &
3098 		     IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_160MHZ_IN_5G)
3099 			return MODE_11AX_HE160;
3100 
3101 		return MODE_UNKNOWN;
3102 	}
3103 
3104 	if (link_sta->bandwidth == IEEE80211_STA_RX_BW_80)
3105 		return MODE_11AX_HE80;
3106 
3107 	if (link_sta->bandwidth == IEEE80211_STA_RX_BW_40)
3108 		return MODE_11AX_HE40;
3109 
3110 	if (link_sta->bandwidth == IEEE80211_STA_RX_BW_20)
3111 		return MODE_11AX_HE20;
3112 
3113 	return MODE_UNKNOWN;
3114 }
3115 
3116 static enum wmi_phy_mode ath12k_mac_get_phymode_eht(struct ath12k *ar,
3117 						    struct ieee80211_link_sta *link_sta)
3118 {
3119 	if (link_sta->bandwidth == IEEE80211_STA_RX_BW_320)
3120 		if (link_sta->eht_cap.eht_cap_elem.phy_cap_info[0] &
3121 		    IEEE80211_EHT_PHY_CAP0_320MHZ_IN_6GHZ)
3122 			return MODE_11BE_EHT320;
3123 
3124 	if (link_sta->bandwidth == IEEE80211_STA_RX_BW_160) {
3125 		if (link_sta->he_cap.he_cap_elem.phy_cap_info[0] &
3126 		    IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_160MHZ_IN_5G)
3127 			return MODE_11BE_EHT160;
3128 
3129 		ath12k_warn(ar->ab, "invalid EHT PHY capability info for 160 Mhz: %d\n",
3130 			    link_sta->he_cap.he_cap_elem.phy_cap_info[0]);
3131 
3132 		return MODE_UNKNOWN;
3133 	}
3134 
3135 	if (link_sta->bandwidth == IEEE80211_STA_RX_BW_80)
3136 		return MODE_11BE_EHT80;
3137 
3138 	if (link_sta->bandwidth == IEEE80211_STA_RX_BW_40)
3139 		return MODE_11BE_EHT40;
3140 
3141 	if (link_sta->bandwidth == IEEE80211_STA_RX_BW_20)
3142 		return MODE_11BE_EHT20;
3143 
3144 	return MODE_UNKNOWN;
3145 }
3146 
3147 static bool
3148 ath12k_peer_assoc_h_eht_masked(const u16 eht_mcs_mask[NL80211_EHT_NSS_MAX])
3149 {
3150 	int nss;
3151 
3152 	for (nss = 0; nss < NL80211_EHT_NSS_MAX; nss++)
3153 		if (eht_mcs_mask[nss])
3154 			return false;
3155 
3156 	return true;
3157 }
3158 
3159 static void ath12k_peer_assoc_h_phymode(struct ath12k *ar,
3160 					struct ath12k_link_vif *arvif,
3161 					struct ath12k_link_sta *arsta,
3162 					struct ath12k_wmi_peer_assoc_arg *arg)
3163 {
3164 	struct ieee80211_link_sta *link_sta;
3165 	struct cfg80211_chan_def def;
3166 	enum nl80211_band band;
3167 	const u8 *ht_mcs_mask;
3168 	const u16 *vht_mcs_mask;
3169 	const u16 *he_mcs_mask;
3170 	const u16 *eht_mcs_mask;
3171 	enum wmi_phy_mode phymode = MODE_UNKNOWN;
3172 
3173 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
3174 
3175 	struct ieee80211_vif *vif = ath12k_ahvif_to_vif(arvif->ahvif);
3176 	struct ieee80211_sta *sta = ath12k_ahsta_to_sta(arsta->ahsta);
3177 
3178 	if (WARN_ON(ath12k_mac_vif_link_chan(vif, arvif->link_id, &def)))
3179 		return;
3180 
3181 	band = def.chan->band;
3182 	ht_mcs_mask = arvif->bitrate_mask.control[band].ht_mcs;
3183 	vht_mcs_mask = arvif->bitrate_mask.control[band].vht_mcs;
3184 	he_mcs_mask = arvif->bitrate_mask.control[band].he_mcs;
3185 	eht_mcs_mask = arvif->bitrate_mask.control[band].eht_mcs;
3186 
3187 	link_sta = ath12k_mac_get_link_sta(arsta);
3188 	if (!link_sta) {
3189 		ath12k_warn(ar->ab, "unable to access link sta in peer assoc he for sta %pM link %u\n",
3190 			    sta->addr, arsta->link_id);
3191 		return;
3192 	}
3193 
3194 	switch (band) {
3195 	case NL80211_BAND_2GHZ:
3196 		if (link_sta->eht_cap.has_eht &&
3197 		    !ath12k_peer_assoc_h_eht_masked(eht_mcs_mask)) {
3198 			if (link_sta->bandwidth == IEEE80211_STA_RX_BW_40)
3199 				phymode = MODE_11BE_EHT40_2G;
3200 			else
3201 				phymode = MODE_11BE_EHT20_2G;
3202 		} else if (link_sta->he_cap.has_he &&
3203 			   !ath12k_peer_assoc_h_he_masked(he_mcs_mask)) {
3204 			if (link_sta->bandwidth == IEEE80211_STA_RX_BW_80)
3205 				phymode = MODE_11AX_HE80_2G;
3206 			else if (link_sta->bandwidth == IEEE80211_STA_RX_BW_40)
3207 				phymode = MODE_11AX_HE40_2G;
3208 			else
3209 				phymode = MODE_11AX_HE20_2G;
3210 		} else if (link_sta->vht_cap.vht_supported &&
3211 		    !ath12k_peer_assoc_h_vht_masked(vht_mcs_mask)) {
3212 			if (link_sta->bandwidth == IEEE80211_STA_RX_BW_40)
3213 				phymode = MODE_11AC_VHT40;
3214 			else
3215 				phymode = MODE_11AC_VHT20;
3216 		} else if (link_sta->ht_cap.ht_supported &&
3217 			   !ath12k_peer_assoc_h_ht_masked(ht_mcs_mask)) {
3218 			if (link_sta->bandwidth == IEEE80211_STA_RX_BW_40)
3219 				phymode = MODE_11NG_HT40;
3220 			else
3221 				phymode = MODE_11NG_HT20;
3222 		} else if (ath12k_mac_sta_has_ofdm_only(link_sta)) {
3223 			phymode = MODE_11G;
3224 		} else {
3225 			phymode = MODE_11B;
3226 		}
3227 		break;
3228 	case NL80211_BAND_5GHZ:
3229 	case NL80211_BAND_6GHZ:
3230 		/* Check EHT first */
3231 		if (link_sta->eht_cap.has_eht) {
3232 			phymode = ath12k_mac_get_phymode_eht(ar, link_sta);
3233 		} else if (link_sta->he_cap.has_he &&
3234 			   !ath12k_peer_assoc_h_he_masked(he_mcs_mask)) {
3235 			phymode = ath12k_mac_get_phymode_he(ar, link_sta);
3236 		} else if (link_sta->vht_cap.vht_supported &&
3237 		    !ath12k_peer_assoc_h_vht_masked(vht_mcs_mask)) {
3238 			phymode = ath12k_mac_get_phymode_vht(ar, link_sta);
3239 		} else if (link_sta->ht_cap.ht_supported &&
3240 			   !ath12k_peer_assoc_h_ht_masked(ht_mcs_mask)) {
3241 			if (link_sta->bandwidth >= IEEE80211_STA_RX_BW_40)
3242 				phymode = MODE_11NA_HT40;
3243 			else
3244 				phymode = MODE_11NA_HT20;
3245 		} else {
3246 			phymode = MODE_11A;
3247 		}
3248 		break;
3249 	default:
3250 		break;
3251 	}
3252 
3253 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac peer %pM phymode %s\n",
3254 		   arsta->addr, ath12k_mac_phymode_str(phymode));
3255 
3256 	arg->peer_phymode = phymode;
3257 	WARN_ON(phymode == MODE_UNKNOWN);
3258 }
3259 
3260 #define ATH12K_EHT_MCS_7_ENABLED	0x00FF
3261 #define ATH12K_EHT_MCS_9_ENABLED	0x0300
3262 #define ATH12K_EHT_MCS_11_ENABLED	0x0C00
3263 #define ATH12K_EHT_MCS_13_ENABLED	0x3000
3264 
3265 static void ath12k_mac_set_eht_mcs(u8 rx_tx_mcs7, u8 rx_tx_mcs9,
3266 				   u8 rx_tx_mcs11, u8 rx_tx_mcs13,
3267 				   u32 *rx_mcs, u32 *tx_mcs,
3268 				   const u16 eht_mcs_limit[NL80211_EHT_NSS_MAX])
3269 {
3270 	int nss;
3271 	u8 mcs_7 = 0, mcs_9 = 0, mcs_11 = 0, mcs_13 = 0;
3272 	u8 peer_mcs_7, peer_mcs_9, peer_mcs_11, peer_mcs_13;
3273 
3274 	for (nss = 0; nss < NL80211_EHT_NSS_MAX; nss++) {
3275 		if (eht_mcs_limit[nss] & ATH12K_EHT_MCS_7_ENABLED)
3276 			mcs_7++;
3277 		if (eht_mcs_limit[nss] & ATH12K_EHT_MCS_9_ENABLED)
3278 			mcs_9++;
3279 		if (eht_mcs_limit[nss] & ATH12K_EHT_MCS_11_ENABLED)
3280 			mcs_11++;
3281 		if (eht_mcs_limit[nss] & ATH12K_EHT_MCS_13_ENABLED)
3282 			mcs_13++;
3283 	}
3284 
3285 	peer_mcs_7 = u8_get_bits(rx_tx_mcs7, IEEE80211_EHT_MCS_NSS_RX);
3286 	peer_mcs_9 = u8_get_bits(rx_tx_mcs9, IEEE80211_EHT_MCS_NSS_RX);
3287 	peer_mcs_11 = u8_get_bits(rx_tx_mcs11, IEEE80211_EHT_MCS_NSS_RX);
3288 	peer_mcs_13 = u8_get_bits(rx_tx_mcs13, IEEE80211_EHT_MCS_NSS_RX);
3289 
3290 	*rx_mcs = u32_encode_bits(min(peer_mcs_7, mcs_7), WMI_EHT_MCS_NSS_0_7) |
3291 		  u32_encode_bits(min(peer_mcs_9, mcs_9), WMI_EHT_MCS_NSS_8_9) |
3292 		  u32_encode_bits(min(peer_mcs_11, mcs_11), WMI_EHT_MCS_NSS_10_11) |
3293 		  u32_encode_bits(min(peer_mcs_13, mcs_13), WMI_EHT_MCS_NSS_12_13);
3294 
3295 	peer_mcs_7 = u8_get_bits(rx_tx_mcs7, IEEE80211_EHT_MCS_NSS_TX);
3296 	peer_mcs_9 = u8_get_bits(rx_tx_mcs9, IEEE80211_EHT_MCS_NSS_TX);
3297 	peer_mcs_11 = u8_get_bits(rx_tx_mcs11, IEEE80211_EHT_MCS_NSS_TX);
3298 	peer_mcs_13 = u8_get_bits(rx_tx_mcs13, IEEE80211_EHT_MCS_NSS_TX);
3299 
3300 	*tx_mcs = u32_encode_bits(min(peer_mcs_7, mcs_7), WMI_EHT_MCS_NSS_0_7) |
3301 		  u32_encode_bits(min(peer_mcs_9, mcs_9), WMI_EHT_MCS_NSS_8_9) |
3302 		  u32_encode_bits(min(peer_mcs_11, mcs_11), WMI_EHT_MCS_NSS_10_11) |
3303 		  u32_encode_bits(min(peer_mcs_13, mcs_13), WMI_EHT_MCS_NSS_12_13);
3304 }
3305 
3306 static void ath12k_mac_set_eht_ppe_threshold(const u8 *ppe_thres,
3307 					     struct ath12k_wmi_ppe_threshold_arg *ppet)
3308 {
3309 	u32 bit_pos = IEEE80211_EHT_PPE_THRES_INFO_HEADER_SIZE, val;
3310 	u8 nss, ru, i;
3311 	u8 ppet_bit_len_per_ru = IEEE80211_EHT_PPE_THRES_INFO_PPET_SIZE * 2;
3312 
3313 	ppet->numss_m1 = u8_get_bits(ppe_thres[0], IEEE80211_EHT_PPE_THRES_NSS_MASK);
3314 	ppet->ru_bit_mask = u16_get_bits(get_unaligned_le16(ppe_thres),
3315 					 IEEE80211_EHT_PPE_THRES_RU_INDEX_BITMASK_MASK);
3316 
3317 	for (nss = 0; nss <= ppet->numss_m1; nss++) {
3318 		for (ru = 0;
3319 		     ru < hweight16(IEEE80211_EHT_PPE_THRES_RU_INDEX_BITMASK_MASK);
3320 		     ru++) {
3321 			if ((ppet->ru_bit_mask & BIT(ru)) == 0)
3322 				continue;
3323 
3324 			val = 0;
3325 			for (i = 0; i < ppet_bit_len_per_ru; i++) {
3326 				val |= (((ppe_thres[bit_pos / 8] >>
3327 					  (bit_pos % 8)) & 0x1) << i);
3328 				bit_pos++;
3329 			}
3330 			ppet->ppet16_ppet8_ru3_ru0[nss] |=
3331 					(val << (ru * ppet_bit_len_per_ru));
3332 		}
3333 	}
3334 }
3335 
3336 static void ath12k_peer_assoc_h_eht(struct ath12k *ar,
3337 				    struct ath12k_link_vif *arvif,
3338 				    struct ath12k_link_sta *arsta,
3339 				    struct ath12k_wmi_peer_assoc_arg *arg)
3340 {
3341 	struct ieee80211_sta *sta = ath12k_ahsta_to_sta(arsta->ahsta);
3342 	struct ieee80211_vif *vif = ath12k_ahvif_to_vif(arvif->ahvif);
3343 	const struct ieee80211_eht_mcs_nss_supp *own_eht_mcs_nss_supp;
3344 	const struct ieee80211_eht_mcs_nss_supp_20mhz_only *bw_20;
3345 	const struct ieee80211_sta_eht_cap *eht_cap, *own_eht_cap;
3346 	const struct ieee80211_sband_iftype_data *iftd;
3347 	const struct ieee80211_eht_mcs_nss_supp_bw *bw;
3348 	const struct ieee80211_sta_he_cap *he_cap;
3349 	struct ieee80211_link_sta *link_sta;
3350 	struct ieee80211_bss_conf *link_conf;
3351 	struct cfg80211_chan_def def;
3352 	bool user_rate_valid = true;
3353 	enum nl80211_band band;
3354 	int eht_nss, nss_idx;
3355 	u32 *rx_mcs, *tx_mcs;
3356 	u16 *eht_mcs_mask;
3357 	u8 max_nss = 0;
3358 
3359 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
3360 
3361 	link_sta = ath12k_mac_get_link_sta(arsta);
3362 	if (!link_sta) {
3363 		ath12k_warn(ar->ab, "unable to access link sta in peer assoc eht for sta %pM link %u\n",
3364 			    sta->addr, arsta->link_id);
3365 		return;
3366 	}
3367 
3368 	link_conf = ath12k_mac_get_link_bss_conf(arvif);
3369 	if (!link_conf) {
3370 		ath12k_warn(ar->ab, "unable to access link_conf in peer assoc eht set\n");
3371 		return;
3372 	}
3373 
3374 	eht_cap = &link_sta->eht_cap;
3375 	he_cap = &link_sta->he_cap;
3376 	if (!he_cap->has_he || !eht_cap->has_eht)
3377 		return;
3378 
3379 	if (WARN_ON(ath12k_mac_vif_link_chan(vif, arvif->link_id, &def)))
3380 		return;
3381 
3382 	band = def.chan->band;
3383 	eht_mcs_mask = arvif->bitrate_mask.control[band].eht_mcs;
3384 
3385 	iftd = ieee80211_get_sband_iftype_data(&ar->mac.sbands[band], vif->type);
3386 	if (!iftd) {
3387 		ath12k_warn(ar->ab,
3388 			    "unable to access iftype_data in struct ieee80211_supported_band\n");
3389 		return;
3390 	}
3391 
3392 	own_eht_cap = &iftd->eht_cap;
3393 	own_eht_mcs_nss_supp = &own_eht_cap->eht_mcs_nss_supp;
3394 
3395 	arg->eht_flag = true;
3396 
3397 	if ((eht_cap->eht_cap_elem.phy_cap_info[5] &
3398 	     IEEE80211_EHT_PHY_CAP5_PPE_THRESHOLD_PRESENT) &&
3399 	    eht_cap->eht_ppe_thres[0] != 0)
3400 		ath12k_mac_set_eht_ppe_threshold(eht_cap->eht_ppe_thres,
3401 						 &arg->peer_eht_ppet);
3402 
3403 	memcpy(arg->peer_eht_cap_mac, eht_cap->eht_cap_elem.mac_cap_info,
3404 	       sizeof(eht_cap->eht_cap_elem.mac_cap_info));
3405 	memcpy(arg->peer_eht_cap_phy, eht_cap->eht_cap_elem.phy_cap_info,
3406 	       sizeof(eht_cap->eht_cap_elem.phy_cap_info));
3407 
3408 	rx_mcs = arg->peer_eht_rx_mcs_set;
3409 	tx_mcs = arg->peer_eht_tx_mcs_set;
3410 
3411 	eht_nss = ath12k_mac_max_eht_mcs_nss((void *)own_eht_mcs_nss_supp,
3412 					     sizeof(*own_eht_mcs_nss_supp));
3413 	if (eht_nss > link_sta->rx_nss) {
3414 		user_rate_valid = false;
3415 		for (nss_idx = (link_sta->rx_nss - 1); nss_idx >= 0; nss_idx--) {
3416 			if (eht_mcs_mask[nss_idx]) {
3417 				user_rate_valid = true;
3418 				break;
3419 			}
3420 		}
3421 	}
3422 
3423 	if (!user_rate_valid) {
3424 		ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
3425 			   "Setting eht range MCS value to peer supported nss %d for peer %pM\n",
3426 			   link_sta->rx_nss, arsta->addr);
3427 		eht_mcs_mask[link_sta->rx_nss - 1] = eht_mcs_mask[eht_nss - 1];
3428 	}
3429 
3430 	bw_20 = &eht_cap->eht_mcs_nss_supp.only_20mhz;
3431 	bw = &eht_cap->eht_mcs_nss_supp.bw._80;
3432 
3433 	switch (link_sta->bandwidth) {
3434 	case IEEE80211_STA_RX_BW_320:
3435 		bw = &eht_cap->eht_mcs_nss_supp.bw._320;
3436 		ath12k_mac_set_eht_mcs(bw->rx_tx_mcs9_max_nss,
3437 				       bw->rx_tx_mcs9_max_nss,
3438 				       bw->rx_tx_mcs11_max_nss,
3439 				       bw->rx_tx_mcs13_max_nss,
3440 				       &rx_mcs[WMI_EHTCAP_TXRX_MCS_NSS_IDX_320],
3441 				       &tx_mcs[WMI_EHTCAP_TXRX_MCS_NSS_IDX_320],
3442 				       eht_mcs_mask);
3443 		arg->peer_eht_mcs_count++;
3444 		fallthrough;
3445 	case IEEE80211_STA_RX_BW_160:
3446 		bw = &eht_cap->eht_mcs_nss_supp.bw._160;
3447 		ath12k_mac_set_eht_mcs(bw->rx_tx_mcs9_max_nss,
3448 				       bw->rx_tx_mcs9_max_nss,
3449 				       bw->rx_tx_mcs11_max_nss,
3450 				       bw->rx_tx_mcs13_max_nss,
3451 				       &rx_mcs[WMI_EHTCAP_TXRX_MCS_NSS_IDX_160],
3452 				       &tx_mcs[WMI_EHTCAP_TXRX_MCS_NSS_IDX_160],
3453 				       eht_mcs_mask);
3454 		arg->peer_eht_mcs_count++;
3455 		fallthrough;
3456 	default:
3457 		if ((vif->type == NL80211_IFTYPE_AP ||
3458 		     vif->type == NL80211_IFTYPE_MESH_POINT) &&
3459 		    !(link_sta->he_cap.he_cap_elem.phy_cap_info[0] &
3460 		      IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_MASK_ALL)) {
3461 			bw_20 = &eht_cap->eht_mcs_nss_supp.only_20mhz;
3462 
3463 			ath12k_mac_set_eht_mcs(bw_20->rx_tx_mcs7_max_nss,
3464 					       bw_20->rx_tx_mcs9_max_nss,
3465 					       bw_20->rx_tx_mcs11_max_nss,
3466 					       bw_20->rx_tx_mcs13_max_nss,
3467 					       &rx_mcs[WMI_EHTCAP_TXRX_MCS_NSS_IDX_80],
3468 					       &tx_mcs[WMI_EHTCAP_TXRX_MCS_NSS_IDX_80],
3469 					       eht_mcs_mask);
3470 		} else {
3471 			bw = &eht_cap->eht_mcs_nss_supp.bw._80;
3472 			ath12k_mac_set_eht_mcs(bw->rx_tx_mcs9_max_nss,
3473 					       bw->rx_tx_mcs9_max_nss,
3474 					       bw->rx_tx_mcs11_max_nss,
3475 					       bw->rx_tx_mcs13_max_nss,
3476 					       &rx_mcs[WMI_EHTCAP_TXRX_MCS_NSS_IDX_80],
3477 					       &tx_mcs[WMI_EHTCAP_TXRX_MCS_NSS_IDX_80],
3478 					       eht_mcs_mask);
3479 		}
3480 
3481 		arg->peer_eht_mcs_count++;
3482 		break;
3483 	}
3484 
3485 	arg->punct_bitmap = ~arvif->punct_bitmap;
3486 	arg->eht_disable_mcs15 = link_conf->eht_disable_mcs15;
3487 
3488 	if ((vif->type == NL80211_IFTYPE_AP ||
3489 	     vif->type == NL80211_IFTYPE_MESH_POINT) &&
3490 	    !(link_sta->he_cap.he_cap_elem.phy_cap_info[0] &
3491 	      IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_MASK_ALL)) {
3492 		if (bw_20->rx_tx_mcs13_max_nss)
3493 			max_nss = max(max_nss, u8_get_bits(bw_20->rx_tx_mcs13_max_nss,
3494 							   IEEE80211_EHT_MCS_NSS_RX));
3495 		if (bw_20->rx_tx_mcs11_max_nss)
3496 			max_nss = max(max_nss, u8_get_bits(bw_20->rx_tx_mcs11_max_nss,
3497 							   IEEE80211_EHT_MCS_NSS_RX));
3498 		if (bw_20->rx_tx_mcs9_max_nss)
3499 			max_nss = max(max_nss, u8_get_bits(bw_20->rx_tx_mcs9_max_nss,
3500 							   IEEE80211_EHT_MCS_NSS_RX));
3501 		if (bw_20->rx_tx_mcs7_max_nss)
3502 			max_nss = max(max_nss, u8_get_bits(bw_20->rx_tx_mcs7_max_nss,
3503 							   IEEE80211_EHT_MCS_NSS_RX));
3504 	} else {
3505 		if (bw->rx_tx_mcs13_max_nss)
3506 			max_nss = max(max_nss, u8_get_bits(bw->rx_tx_mcs13_max_nss,
3507 							   IEEE80211_EHT_MCS_NSS_RX));
3508 		if (bw->rx_tx_mcs11_max_nss)
3509 			max_nss = max(max_nss, u8_get_bits(bw->rx_tx_mcs11_max_nss,
3510 							   IEEE80211_EHT_MCS_NSS_RX));
3511 		if (bw->rx_tx_mcs9_max_nss)
3512 			max_nss = max(max_nss, u8_get_bits(bw->rx_tx_mcs9_max_nss,
3513 							   IEEE80211_EHT_MCS_NSS_RX));
3514 	}
3515 
3516 	max_nss = min(max_nss, (uint8_t)eht_nss);
3517 
3518 	arg->peer_nss = min(link_sta->rx_nss, max_nss);
3519 
3520 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
3521 		   "mac eht peer %pM nss %d mcs cnt %d ru_punct_bitmap 0x%x\n",
3522 		   arsta->addr, arg->peer_nss, arg->peer_eht_mcs_count,
3523 		   arg->punct_bitmap);
3524 }
3525 
3526 static void ath12k_peer_assoc_h_mlo(struct ath12k_link_sta *arsta,
3527 				    struct ath12k_wmi_peer_assoc_arg *arg)
3528 {
3529 	struct ieee80211_sta *sta = ath12k_ahsta_to_sta(arsta->ahsta);
3530 	struct peer_assoc_mlo_params *ml = &arg->ml;
3531 	struct ath12k_sta *ahsta = arsta->ahsta;
3532 	struct ath12k_link_sta *arsta_p;
3533 	struct ath12k_link_vif *arvif;
3534 	unsigned long links;
3535 	u8 link_id;
3536 	int i;
3537 
3538 	if (!sta->mlo || ahsta->ml_peer_id == ATH12K_MLO_PEER_ID_INVALID)
3539 		return;
3540 
3541 	ml->enabled = true;
3542 	ml->assoc_link = arsta->is_assoc_link;
3543 
3544 	/* For now considering the primary umac based on assoc link */
3545 	ml->primary_umac = arsta->is_assoc_link;
3546 	ml->peer_id_valid = true;
3547 	ml->logical_link_idx_valid = true;
3548 
3549 	ether_addr_copy(ml->mld_addr, sta->addr);
3550 	ml->logical_link_idx = arsta->link_idx;
3551 	ml->ml_peer_id = ahsta->ml_peer_id;
3552 	ml->ieee_link_id = arsta->link_id;
3553 	ml->num_partner_links = 0;
3554 	ml->eml_cap = sta->eml_cap;
3555 	links = ahsta->links_map;
3556 
3557 	rcu_read_lock();
3558 
3559 	i = 0;
3560 
3561 	for_each_set_bit(link_id, &links, IEEE80211_MLD_MAX_NUM_LINKS) {
3562 		if (i >= ATH12K_WMI_MLO_MAX_LINKS)
3563 			break;
3564 
3565 		arsta_p = rcu_dereference(ahsta->link[link_id]);
3566 		arvif = rcu_dereference(ahsta->ahvif->link[link_id]);
3567 
3568 		if (arsta_p == arsta)
3569 			continue;
3570 
3571 		if (!arvif->is_started)
3572 			continue;
3573 
3574 		ml->partner_info[i].vdev_id = arvif->vdev_id;
3575 		ml->partner_info[i].hw_link_id = arvif->ar->pdev->hw_link_id;
3576 		ml->partner_info[i].assoc_link = arsta_p->is_assoc_link;
3577 		ml->partner_info[i].primary_umac = arsta_p->is_assoc_link;
3578 		ml->partner_info[i].logical_link_idx_valid = true;
3579 		ml->partner_info[i].logical_link_idx = arsta_p->link_idx;
3580 		ml->num_partner_links++;
3581 
3582 		i++;
3583 	}
3584 
3585 	rcu_read_unlock();
3586 }
3587 
3588 static void ath12k_peer_assoc_prepare(struct ath12k *ar,
3589 				      struct ath12k_link_vif *arvif,
3590 				      struct ath12k_link_sta *arsta,
3591 				      struct ath12k_wmi_peer_assoc_arg *arg,
3592 				      bool reassoc)
3593 {
3594 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
3595 
3596 	memset(arg, 0, sizeof(*arg));
3597 
3598 	reinit_completion(&ar->peer_assoc_done);
3599 
3600 	arg->peer_new_assoc = !reassoc;
3601 	ath12k_peer_assoc_h_basic(ar, arvif, arsta, arg);
3602 	ath12k_peer_assoc_h_crypto(ar, arvif, arsta, arg);
3603 	ath12k_peer_assoc_h_rates(ar, arvif, arsta, arg);
3604 	ath12k_peer_assoc_h_ht(ar, arvif, arsta, arg);
3605 	ath12k_peer_assoc_h_vht(ar, arvif, arsta, arg);
3606 	ath12k_peer_assoc_h_he(ar, arvif, arsta, arg);
3607 	ath12k_peer_assoc_h_he_6ghz(ar, arvif, arsta, arg);
3608 	ath12k_peer_assoc_h_eht(ar, arvif, arsta, arg);
3609 	ath12k_peer_assoc_h_qos(ar, arvif, arsta, arg);
3610 	ath12k_peer_assoc_h_phymode(ar, arvif, arsta, arg);
3611 	ath12k_peer_assoc_h_smps(arsta, arg);
3612 	ath12k_peer_assoc_h_mlo(arsta, arg);
3613 
3614 	arsta->peer_nss = arg->peer_nss;
3615 	/* TODO: amsdu_disable req? */
3616 }
3617 
3618 static int ath12k_setup_peer_smps(struct ath12k *ar, struct ath12k_link_vif *arvif,
3619 				  const u8 *addr,
3620 				  const struct ieee80211_sta_ht_cap *ht_cap,
3621 				  const struct ieee80211_he_6ghz_capa *he_6ghz_capa)
3622 {
3623 	int smps, ret = 0;
3624 
3625 	if (!ht_cap->ht_supported && !he_6ghz_capa)
3626 		return 0;
3627 
3628 	ret = ath12k_get_smps_from_capa(ht_cap, he_6ghz_capa, &smps);
3629 	if (ret < 0)
3630 		return ret;
3631 
3632 	return ath12k_wmi_set_peer_param(ar, addr, arvif->vdev_id,
3633 					 WMI_PEER_MIMO_PS_STATE,
3634 					 ath12k_smps_map[smps]);
3635 }
3636 
3637 static int ath12k_mac_set_he_txbf_conf(struct ath12k_link_vif *arvif)
3638 {
3639 	struct ath12k_vif *ahvif = arvif->ahvif;
3640 	struct ath12k *ar = arvif->ar;
3641 	u32 param = WMI_VDEV_PARAM_SET_HEMU_MODE;
3642 	u32 value = 0;
3643 	int ret;
3644 	struct ieee80211_bss_conf *link_conf;
3645 
3646 	link_conf = ath12k_mac_get_link_bss_conf(arvif);
3647 	if (!link_conf) {
3648 		ath12k_warn(ar->ab, "unable to access bss link conf in txbf conf\n");
3649 		return -EINVAL;
3650 	}
3651 
3652 	if (!link_conf->he_support)
3653 		return 0;
3654 
3655 	if (link_conf->he_su_beamformer) {
3656 		value |= u32_encode_bits(HE_SU_BFER_ENABLE, HE_MODE_SU_TX_BFER);
3657 		if (link_conf->he_mu_beamformer &&
3658 		    ahvif->vdev_type == WMI_VDEV_TYPE_AP)
3659 			value |= u32_encode_bits(HE_MU_BFER_ENABLE, HE_MODE_MU_TX_BFER);
3660 	}
3661 
3662 	if (ahvif->vif->type != NL80211_IFTYPE_MESH_POINT) {
3663 		value |= u32_encode_bits(HE_DL_MUOFDMA_ENABLE, HE_MODE_DL_OFDMA) |
3664 			 u32_encode_bits(HE_UL_MUOFDMA_ENABLE, HE_MODE_UL_OFDMA);
3665 
3666 		if (link_conf->he_full_ul_mumimo)
3667 			value |= u32_encode_bits(HE_UL_MUMIMO_ENABLE, HE_MODE_UL_MUMIMO);
3668 
3669 		if (link_conf->he_su_beamformee)
3670 			value |= u32_encode_bits(HE_SU_BFEE_ENABLE, HE_MODE_SU_TX_BFEE);
3671 	}
3672 
3673 	ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id, param, value);
3674 	if (ret) {
3675 		ath12k_warn(ar->ab, "failed to set vdev %d HE MU mode: %d\n",
3676 			    arvif->vdev_id, ret);
3677 		return ret;
3678 	}
3679 
3680 	param = WMI_VDEV_PARAM_SET_HE_SOUNDING_MODE;
3681 	value =	u32_encode_bits(HE_VHT_SOUNDING_MODE_ENABLE, HE_VHT_SOUNDING_MODE) |
3682 		u32_encode_bits(HE_TRIG_NONTRIG_SOUNDING_MODE_ENABLE,
3683 				HE_TRIG_NONTRIG_SOUNDING_MODE);
3684 	ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
3685 					    param, value);
3686 	if (ret) {
3687 		ath12k_warn(ar->ab, "failed to set vdev %d sounding mode: %d\n",
3688 			    arvif->vdev_id, ret);
3689 		return ret;
3690 	}
3691 
3692 	return 0;
3693 }
3694 
3695 static int ath12k_mac_vif_recalc_sta_he_txbf(struct ath12k *ar,
3696 					     struct ath12k_link_vif *arvif,
3697 					     struct ieee80211_sta_he_cap *he_cap,
3698 					     int *hemode)
3699 {
3700 	struct ieee80211_vif *vif = arvif->ahvif->vif;
3701 	struct ieee80211_he_cap_elem he_cap_elem = {};
3702 	struct ieee80211_sta_he_cap *cap_band;
3703 	struct cfg80211_chan_def def;
3704 	u8 link_id = arvif->link_id;
3705 	struct ieee80211_bss_conf *link_conf;
3706 
3707 	link_conf = ath12k_mac_get_link_bss_conf(arvif);
3708 	if (!link_conf) {
3709 		ath12k_warn(ar->ab, "unable to access bss link conf in recalc txbf conf\n");
3710 		return -EINVAL;
3711 	}
3712 
3713 	if (!link_conf->he_support)
3714 		return 0;
3715 
3716 	if (vif->type != NL80211_IFTYPE_STATION)
3717 		return -EINVAL;
3718 
3719 	if (WARN_ON(ath12k_mac_vif_link_chan(vif, link_id, &def)))
3720 		return -EINVAL;
3721 
3722 	if (def.chan->band == NL80211_BAND_2GHZ)
3723 		cap_band = &ar->mac.iftype[NL80211_BAND_2GHZ][vif->type].he_cap;
3724 	else
3725 		cap_band = &ar->mac.iftype[NL80211_BAND_5GHZ][vif->type].he_cap;
3726 
3727 	memcpy(&he_cap_elem, &cap_band->he_cap_elem, sizeof(he_cap_elem));
3728 
3729 	*hemode = 0;
3730 	if (HECAP_PHY_SUBFME_GET(he_cap_elem.phy_cap_info)) {
3731 		if (HECAP_PHY_SUBFMR_GET(he_cap->he_cap_elem.phy_cap_info))
3732 			*hemode |= u32_encode_bits(HE_SU_BFEE_ENABLE, HE_MODE_SU_TX_BFEE);
3733 		if (HECAP_PHY_MUBFMR_GET(he_cap->he_cap_elem.phy_cap_info))
3734 			*hemode |= u32_encode_bits(HE_MU_BFEE_ENABLE, HE_MODE_MU_TX_BFEE);
3735 	}
3736 
3737 	if (vif->type != NL80211_IFTYPE_MESH_POINT) {
3738 		*hemode |= u32_encode_bits(HE_DL_MUOFDMA_ENABLE, HE_MODE_DL_OFDMA) |
3739 			  u32_encode_bits(HE_UL_MUOFDMA_ENABLE, HE_MODE_UL_OFDMA);
3740 
3741 		if (HECAP_PHY_ULMUMIMO_GET(he_cap_elem.phy_cap_info))
3742 			if (HECAP_PHY_ULMUMIMO_GET(he_cap->he_cap_elem.phy_cap_info))
3743 				*hemode |= u32_encode_bits(HE_UL_MUMIMO_ENABLE,
3744 							  HE_MODE_UL_MUMIMO);
3745 
3746 		if (u32_get_bits(*hemode, HE_MODE_MU_TX_BFEE))
3747 			*hemode |= u32_encode_bits(HE_SU_BFEE_ENABLE, HE_MODE_SU_TX_BFEE);
3748 
3749 		if (u32_get_bits(*hemode, HE_MODE_MU_TX_BFER))
3750 			*hemode |= u32_encode_bits(HE_SU_BFER_ENABLE, HE_MODE_SU_TX_BFER);
3751 	}
3752 
3753 	return 0;
3754 }
3755 
3756 static int ath12k_mac_set_eht_txbf_conf(struct ath12k_link_vif *arvif)
3757 {
3758 	struct ath12k_vif *ahvif = arvif->ahvif;
3759 	struct ath12k *ar = arvif->ar;
3760 	u32 param = WMI_VDEV_PARAM_SET_EHT_MU_MODE;
3761 	u32 value = 0;
3762 	int ret;
3763 	struct ieee80211_bss_conf *link_conf;
3764 
3765 	link_conf = ath12k_mac_get_link_bss_conf(arvif);
3766 	if (!link_conf) {
3767 		ath12k_warn(ar->ab, "unable to access bss link conf in eht txbf conf\n");
3768 		return -ENOENT;
3769 	}
3770 
3771 	if (!link_conf->eht_support)
3772 		return 0;
3773 
3774 	if (link_conf->eht_su_beamformer) {
3775 		value |= u32_encode_bits(EHT_SU_BFER_ENABLE, EHT_MODE_SU_TX_BFER);
3776 		if (link_conf->eht_mu_beamformer &&
3777 		    ahvif->vdev_type == WMI_VDEV_TYPE_AP)
3778 			value |= u32_encode_bits(EHT_MU_BFER_ENABLE,
3779 						 EHT_MODE_MU_TX_BFER) |
3780 				 u32_encode_bits(EHT_DL_MUOFDMA_ENABLE,
3781 						 EHT_MODE_DL_OFDMA_MUMIMO) |
3782 				 u32_encode_bits(EHT_UL_MUOFDMA_ENABLE,
3783 						 EHT_MODE_UL_OFDMA_MUMIMO);
3784 	}
3785 
3786 	if (ahvif->vif->type != NL80211_IFTYPE_MESH_POINT) {
3787 		value |= u32_encode_bits(EHT_DL_MUOFDMA_ENABLE, EHT_MODE_DL_OFDMA) |
3788 			 u32_encode_bits(EHT_UL_MUOFDMA_ENABLE, EHT_MODE_UL_OFDMA);
3789 
3790 		if (link_conf->eht_80mhz_full_bw_ul_mumimo)
3791 			value |= u32_encode_bits(EHT_UL_MUMIMO_ENABLE, EHT_MODE_MUMIMO);
3792 
3793 		if (link_conf->eht_su_beamformee)
3794 			value |= u32_encode_bits(EHT_SU_BFEE_ENABLE,
3795 						 EHT_MODE_SU_TX_BFEE);
3796 	}
3797 
3798 	ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id, param, value);
3799 	if (ret) {
3800 		ath12k_warn(ar->ab, "failed to set vdev %d EHT MU mode: %d\n",
3801 			    arvif->vdev_id, ret);
3802 		return ret;
3803 	}
3804 
3805 	return 0;
3806 }
3807 
3808 static u32 ath12k_mac_ieee80211_sta_bw_to_wmi(struct ath12k *ar,
3809 					      struct ieee80211_link_sta *link_sta)
3810 {
3811 	u32 bw;
3812 
3813 	switch (link_sta->bandwidth) {
3814 	case IEEE80211_STA_RX_BW_20:
3815 		bw = WMI_PEER_CHWIDTH_20MHZ;
3816 		break;
3817 	case IEEE80211_STA_RX_BW_40:
3818 		bw = WMI_PEER_CHWIDTH_40MHZ;
3819 		break;
3820 	case IEEE80211_STA_RX_BW_80:
3821 		bw = WMI_PEER_CHWIDTH_80MHZ;
3822 		break;
3823 	case IEEE80211_STA_RX_BW_160:
3824 		bw = WMI_PEER_CHWIDTH_160MHZ;
3825 		break;
3826 	case IEEE80211_STA_RX_BW_320:
3827 		bw = WMI_PEER_CHWIDTH_320MHZ;
3828 		break;
3829 	default:
3830 		ath12k_warn(ar->ab, "Invalid bandwidth %d for link station %pM\n",
3831 			    link_sta->bandwidth, link_sta->addr);
3832 		bw = WMI_PEER_CHWIDTH_20MHZ;
3833 		break;
3834 	}
3835 
3836 	return bw;
3837 }
3838 
3839 static void ath12k_bss_assoc(struct ath12k *ar,
3840 			     struct ath12k_link_vif *arvif,
3841 			     struct ieee80211_bss_conf *bss_conf)
3842 {
3843 	struct ath12k_vif *ahvif = arvif->ahvif;
3844 	struct ieee80211_vif *vif = ath12k_ahvif_to_vif(ahvif);
3845 	struct ath12k_wmi_vdev_up_params params = {};
3846 	struct ieee80211_link_sta *link_sta;
3847 	u8 link_id = bss_conf->link_id;
3848 	struct ath12k_link_sta *arsta;
3849 	struct ieee80211_sta *ap_sta;
3850 	struct ath12k_sta *ahsta;
3851 	struct ath12k_dp_link_peer *peer;
3852 	bool is_auth = false;
3853 	u32 hemode = 0;
3854 	int ret;
3855 	struct ath12k_dp *dp = ath12k_ab_to_dp(ar->ab);
3856 
3857 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
3858 
3859 	struct ath12k_wmi_peer_assoc_arg *peer_arg __free(kfree) =
3860 					kzalloc_obj(*peer_arg);
3861 	if (!peer_arg)
3862 		return;
3863 
3864 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
3865 		   "mac vdev %i link id %u assoc bssid %pM aid %d\n",
3866 		   arvif->vdev_id, link_id, arvif->bssid, ahvif->aid);
3867 
3868 	rcu_read_lock();
3869 
3870 	/* During ML connection, cfg.ap_addr has the MLD address. For
3871 	 * non-ML connection, it has the BSSID.
3872 	 */
3873 	ap_sta = ieee80211_find_sta(vif, vif->cfg.ap_addr);
3874 	if (!ap_sta) {
3875 		ath12k_warn(ar->ab, "failed to find station entry for bss %pM vdev %i\n",
3876 			    vif->cfg.ap_addr, arvif->vdev_id);
3877 		rcu_read_unlock();
3878 		return;
3879 	}
3880 
3881 	ahsta = ath12k_sta_to_ahsta(ap_sta);
3882 
3883 	arsta = wiphy_dereference(ath12k_ar_to_hw(ar)->wiphy,
3884 				  ahsta->link[link_id]);
3885 	if (WARN_ON(!arsta)) {
3886 		rcu_read_unlock();
3887 		return;
3888 	}
3889 
3890 	link_sta = ath12k_mac_get_link_sta(arsta);
3891 	if (WARN_ON(!link_sta)) {
3892 		rcu_read_unlock();
3893 		return;
3894 	}
3895 
3896 	ath12k_peer_assoc_prepare(ar, arvif, arsta, peer_arg, false);
3897 
3898 	/* link_sta->he_cap must be protected by rcu_read_lock */
3899 	ret = ath12k_mac_vif_recalc_sta_he_txbf(ar, arvif, &link_sta->he_cap, &hemode);
3900 	if (ret) {
3901 		ath12k_warn(ar->ab, "failed to recalc he txbf for vdev %i on bss %pM: %d\n",
3902 			    arvif->vdev_id, bss_conf->bssid, ret);
3903 		rcu_read_unlock();
3904 		return;
3905 	}
3906 
3907 	rcu_read_unlock();
3908 
3909 	/* keep this before ath12k_wmi_send_peer_assoc_cmd() */
3910 	ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
3911 					    WMI_VDEV_PARAM_SET_HEMU_MODE, hemode);
3912 	if (ret) {
3913 		ath12k_warn(ar->ab, "failed to submit vdev param txbf 0x%x: %d\n",
3914 			    hemode, ret);
3915 		return;
3916 	}
3917 
3918 	peer_arg->is_assoc = true;
3919 	ret = ath12k_wmi_send_peer_assoc_cmd(ar, peer_arg);
3920 	if (ret) {
3921 		ath12k_warn(ar->ab, "failed to run peer assoc for %pM vdev %i: %d\n",
3922 			    bss_conf->bssid, arvif->vdev_id, ret);
3923 		return;
3924 	}
3925 
3926 	if (!wait_for_completion_timeout(&ar->peer_assoc_done, 1 * HZ)) {
3927 		ath12k_warn(ar->ab, "failed to get peer assoc conf event for %pM vdev %i\n",
3928 			    bss_conf->bssid, arvif->vdev_id);
3929 		return;
3930 	}
3931 
3932 	ret = ath12k_setup_peer_smps(ar, arvif, bss_conf->bssid,
3933 				     &link_sta->ht_cap, &link_sta->he_6ghz_capa);
3934 	if (ret) {
3935 		ath12k_warn(ar->ab, "failed to setup peer SMPS for vdev %d: %d\n",
3936 			    arvif->vdev_id, ret);
3937 		return;
3938 	}
3939 
3940 	WARN_ON(arvif->is_up);
3941 
3942 	ahvif->aid = vif->cfg.aid;
3943 	ether_addr_copy(arvif->bssid, bss_conf->bssid);
3944 
3945 	params.vdev_id = arvif->vdev_id;
3946 	params.aid = ahvif->aid;
3947 	params.bssid = arvif->bssid;
3948 	params.tx_bssid = ath12k_mac_get_tx_bssid(arvif);
3949 	if (params.tx_bssid) {
3950 		params.nontx_profile_idx = bss_conf->bssid_index;
3951 		params.nontx_profile_cnt = 1 << bss_conf->bssid_indicator;
3952 	}
3953 	ret = ath12k_wmi_vdev_up(ar, &params);
3954 	if (ret) {
3955 		ath12k_warn(ar->ab, "failed to set vdev %d up: %d\n",
3956 			    arvif->vdev_id, ret);
3957 		return;
3958 	}
3959 
3960 	arvif->is_up = true;
3961 	arvif->rekey_data.enable_offload = false;
3962 
3963 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
3964 		   "mac vdev %d up (associated) bssid %pM aid %d\n",
3965 		   arvif->vdev_id, bss_conf->bssid, vif->cfg.aid);
3966 
3967 	spin_lock_bh(&dp->dp_lock);
3968 
3969 	peer = ath12k_dp_link_peer_find_by_vdev_and_addr(dp, arvif->vdev_id,
3970 							 arvif->bssid);
3971 	if (peer && peer->is_authorized)
3972 		is_auth = true;
3973 
3974 	spin_unlock_bh(&dp->dp_lock);
3975 
3976 	/* Authorize BSS Peer */
3977 	if (is_auth) {
3978 		ret = ath12k_wmi_set_peer_param(ar, arvif->bssid,
3979 						arvif->vdev_id,
3980 						WMI_PEER_AUTHORIZE,
3981 						1);
3982 		if (ret)
3983 			ath12k_warn(ar->ab, "Unable to authorize BSS peer: %d\n", ret);
3984 	}
3985 
3986 	ret = ath12k_wmi_send_obss_spr_cmd(ar, arvif->vdev_id,
3987 					   &bss_conf->he_obss_pd);
3988 	if (ret)
3989 		ath12k_warn(ar->ab, "failed to set vdev %i OBSS PD parameters: %d\n",
3990 			    arvif->vdev_id, ret);
3991 
3992 	if (test_bit(WMI_TLV_SERVICE_11D_OFFLOAD, ar->ab->wmi_ab.svc_map) &&
3993 	    ahvif->vdev_type == WMI_VDEV_TYPE_STA &&
3994 	    ahvif->vdev_subtype == WMI_VDEV_SUBTYPE_NONE)
3995 		ath12k_mac_11d_scan_stop_all(ar->ab);
3996 }
3997 
3998 static void ath12k_bss_disassoc(struct ath12k *ar,
3999 				struct ath12k_link_vif *arvif)
4000 {
4001 	int ret;
4002 
4003 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
4004 
4005 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac vdev %i disassoc bssid %pM\n",
4006 		   arvif->vdev_id, arvif->bssid);
4007 
4008 	ret = ath12k_wmi_vdev_down(ar, arvif->vdev_id);
4009 	if (ret)
4010 		ath12k_warn(ar->ab, "failed to down vdev %i: %d\n",
4011 			    arvif->vdev_id, ret);
4012 
4013 	arvif->is_up = false;
4014 
4015 	memset(&arvif->rekey_data, 0, sizeof(arvif->rekey_data));
4016 
4017 	cancel_delayed_work(&arvif->connection_loss_work);
4018 }
4019 
4020 static u32 ath12k_mac_get_rate_hw_value(int bitrate)
4021 {
4022 	u32 preamble;
4023 	u16 hw_value;
4024 	int rate;
4025 	size_t i;
4026 
4027 	if (ath12k_mac_bitrate_is_cck(bitrate))
4028 		preamble = WMI_RATE_PREAMBLE_CCK;
4029 	else
4030 		preamble = WMI_RATE_PREAMBLE_OFDM;
4031 
4032 	for (i = 0; i < ARRAY_SIZE(ath12k_legacy_rates); i++) {
4033 		if (ath12k_legacy_rates[i].bitrate != bitrate)
4034 			continue;
4035 
4036 		hw_value = ath12k_legacy_rates[i].hw_value;
4037 		rate = ATH12K_HW_RATE_CODE(hw_value, 0, preamble);
4038 
4039 		return rate;
4040 	}
4041 
4042 	return -EINVAL;
4043 }
4044 
4045 static void ath12k_recalculate_mgmt_rate(struct ath12k *ar,
4046 					 struct ath12k_link_vif *arvif,
4047 					 struct cfg80211_chan_def *def)
4048 {
4049 	struct ieee80211_vif *vif = ath12k_ahvif_to_vif(arvif->ahvif);
4050 	struct ieee80211_hw *hw = ath12k_ar_to_hw(ar);
4051 	const struct ieee80211_supported_band *sband;
4052 	struct ieee80211_bss_conf *bss_conf;
4053 	u8 basic_rate_idx;
4054 	int hw_rate_code;
4055 	u32 vdev_param;
4056 	u16 bitrate;
4057 	int ret;
4058 
4059 	lockdep_assert_wiphy(hw->wiphy);
4060 
4061 	bss_conf = ath12k_mac_get_link_bss_conf(arvif);
4062 	if (!bss_conf) {
4063 		ath12k_warn(ar->ab, "unable to access bss link conf in mgmt rate calc for vif %pM link %u\n",
4064 			    vif->addr, arvif->link_id);
4065 		return;
4066 	}
4067 
4068 	sband = hw->wiphy->bands[def->chan->band];
4069 	if (bss_conf->basic_rates)
4070 		basic_rate_idx = __ffs(bss_conf->basic_rates);
4071 	else
4072 		basic_rate_idx = 0;
4073 	bitrate = sband->bitrates[basic_rate_idx].bitrate;
4074 
4075 	hw_rate_code = ath12k_mac_get_rate_hw_value(bitrate);
4076 	if (hw_rate_code < 0) {
4077 		ath12k_warn(ar->ab, "bitrate not supported %d\n", bitrate);
4078 		return;
4079 	}
4080 
4081 	vdev_param = WMI_VDEV_PARAM_MGMT_RATE;
4082 	ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id, vdev_param,
4083 					    hw_rate_code);
4084 	if (ret)
4085 		ath12k_warn(ar->ab, "failed to set mgmt tx rate %d\n", ret);
4086 
4087 	vdev_param = WMI_VDEV_PARAM_BEACON_RATE;
4088 	ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id, vdev_param,
4089 					    hw_rate_code);
4090 	if (ret)
4091 		ath12k_warn(ar->ab, "failed to set beacon tx rate %d\n", ret);
4092 }
4093 
4094 static void ath12k_mac_bcn_tx_event(struct ath12k_link_vif *arvif)
4095 {
4096 	struct ieee80211_vif *vif = ath12k_ahvif_to_vif(arvif->ahvif);
4097 	struct ieee80211_bss_conf *link_conf;
4098 
4099 	link_conf = ath12k_mac_get_link_bss_conf(arvif);
4100 	if (!link_conf) {
4101 		ath12k_warn(arvif->ar->ab, "failed to get link conf for vdev %u\n",
4102 			    arvif->vdev_id);
4103 		return;
4104 	}
4105 
4106 	if (link_conf->color_change_active) {
4107 		if (ieee80211_beacon_cntdwn_is_complete(vif, arvif->link_id)) {
4108 			ieee80211_color_change_finish(vif, arvif->link_id);
4109 			return;
4110 		}
4111 
4112 		ieee80211_beacon_update_cntdwn(vif, arvif->link_id);
4113 		ath12k_mac_setup_bcn_tmpl(arvif);
4114 	}
4115 }
4116 
4117 static void ath12k_mac_bcn_tx_work(struct wiphy *wiphy, struct wiphy_work *work)
4118 {
4119 	struct ath12k_link_vif *arvif = container_of(work, struct ath12k_link_vif,
4120 						     bcn_tx_work);
4121 
4122 	lockdep_assert_wiphy(wiphy);
4123 	ath12k_mac_bcn_tx_event(arvif);
4124 }
4125 
4126 static void ath12k_mac_init_arvif(struct ath12k_vif *ahvif,
4127 				  struct ath12k_link_vif *arvif, int link_id)
4128 {
4129 	struct ath12k_hw *ah = ahvif->ah;
4130 	u8 _link_id;
4131 	int i;
4132 
4133 	lockdep_assert_wiphy(ah->hw->wiphy);
4134 
4135 	if (WARN_ON(!arvif))
4136 		return;
4137 
4138 	if (WARN_ON(link_id >= ATH12K_NUM_MAX_LINKS))
4139 		return;
4140 
4141 	if (link_id < 0)
4142 		_link_id = 0;
4143 	else
4144 		_link_id = link_id;
4145 
4146 	arvif->ahvif = ahvif;
4147 	arvif->link_id = _link_id;
4148 
4149 	/* Protects the datapath stats update on a per link basis */
4150 	spin_lock_init(&arvif->link_stats_lock);
4151 
4152 	INIT_LIST_HEAD(&arvif->list);
4153 	INIT_DELAYED_WORK(&arvif->connection_loss_work,
4154 			  ath12k_mac_vif_sta_connection_loss_work);
4155 	wiphy_work_init(&arvif->bcn_tx_work, ath12k_mac_bcn_tx_work);
4156 
4157 	arvif->num_stations = 0;
4158 
4159 	for (i = 0; i < ARRAY_SIZE(arvif->bitrate_mask.control); i++) {
4160 		arvif->bitrate_mask.control[i].legacy = 0xffffffff;
4161 		arvif->bitrate_mask.control[i].gi = NL80211_TXRATE_DEFAULT_GI;
4162 		memset(arvif->bitrate_mask.control[i].ht_mcs, 0xff,
4163 		       sizeof(arvif->bitrate_mask.control[i].ht_mcs));
4164 		memset(arvif->bitrate_mask.control[i].vht_mcs, 0xff,
4165 		       sizeof(arvif->bitrate_mask.control[i].vht_mcs));
4166 		memset(arvif->bitrate_mask.control[i].he_mcs, 0xff,
4167 		       sizeof(arvif->bitrate_mask.control[i].he_mcs));
4168 		memset(arvif->bitrate_mask.control[i].eht_mcs, 0xff,
4169 		       sizeof(arvif->bitrate_mask.control[i].eht_mcs));
4170 	}
4171 
4172 	/* Handle MLO related assignments */
4173 	if (link_id >= 0) {
4174 		rcu_assign_pointer(ahvif->link[arvif->link_id], arvif);
4175 		ahvif->links_map |= BIT(_link_id);
4176 	}
4177 
4178 	ath12k_generic_dbg(ATH12K_DBG_MAC,
4179 			   "mac init link arvif (link_id %d%s) for vif %pM. links_map 0x%x",
4180 			   _link_id, (link_id < 0) ? " deflink" : "", ahvif->vif->addr,
4181 			   ahvif->links_map);
4182 }
4183 
4184 static void ath12k_mac_remove_link_interface(struct ieee80211_hw *hw,
4185 					     struct ath12k_link_vif *arvif)
4186 {
4187 	struct ath12k_vif *ahvif = arvif->ahvif;
4188 	struct ath12k_hw *ah = hw->priv;
4189 	struct ath12k *ar = arvif->ar;
4190 	int ret;
4191 
4192 	lockdep_assert_wiphy(ah->hw->wiphy);
4193 
4194 	cancel_delayed_work_sync(&arvif->connection_loss_work);
4195 	wiphy_work_cancel(ath12k_ar_to_hw(ar)->wiphy, &arvif->bcn_tx_work);
4196 
4197 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac remove link interface (vdev %d link id %d)",
4198 		   arvif->vdev_id, arvif->link_id);
4199 
4200 	if (test_bit(WMI_TLV_SERVICE_11D_OFFLOAD, ar->ab->wmi_ab.svc_map) &&
4201 	    ahvif->vdev_type == WMI_VDEV_TYPE_STA &&
4202 	    ahvif->vdev_subtype == WMI_VDEV_SUBTYPE_NONE)
4203 		ath12k_mac_11d_scan_stop(ar);
4204 
4205 	if (ahvif->vdev_type == WMI_VDEV_TYPE_AP) {
4206 		ret = ath12k_peer_delete(ar, arvif->vdev_id, arvif->bssid);
4207 		if (ret)
4208 			ath12k_warn(ar->ab, "failed to submit AP self-peer removal on vdev %d link id %d: %d",
4209 				    arvif->vdev_id, arvif->link_id, ret);
4210 
4211 		if (arvif->link_id < IEEE80211_MLD_MAX_NUM_LINKS)
4212 			ath12k_dp_peer_delete(&ah->dp_hw, arvif->bssid, NULL);
4213 	}
4214 	ath12k_mac_vdev_delete(ar, arvif);
4215 }
4216 
4217 static struct ath12k_link_vif *ath12k_mac_assign_link_vif(struct ath12k_hw *ah,
4218 							  struct ieee80211_vif *vif,
4219 							  u8 link_id)
4220 {
4221 	struct ath12k_vif *ahvif = ath12k_vif_to_ahvif(vif);
4222 	struct ath12k_link_vif *arvif;
4223 
4224 	lockdep_assert_wiphy(ah->hw->wiphy);
4225 
4226 	arvif = wiphy_dereference(ah->hw->wiphy, ahvif->link[link_id]);
4227 	if (arvif)
4228 		return arvif;
4229 
4230 	/* If this is the first link arvif being created for an ML VIF
4231 	 * use the preallocated deflink memory except for scan arvifs
4232 	 */
4233 	if (!ahvif->links_map && link_id < ATH12K_FIRST_SCAN_LINK) {
4234 		arvif = &ahvif->deflink;
4235 
4236 		if (vif->type == NL80211_IFTYPE_STATION)
4237 			arvif->is_sta_assoc_link = true;
4238 	} else {
4239 		arvif = kzalloc_obj(*arvif);
4240 		if (!arvif)
4241 			return NULL;
4242 	}
4243 
4244 	ath12k_mac_init_arvif(ahvif, arvif, link_id);
4245 
4246 	return arvif;
4247 }
4248 
4249 static void ath12k_mac_unassign_link_vif(struct ath12k_link_vif *arvif)
4250 {
4251 	struct ath12k_vif *ahvif = arvif->ahvif;
4252 	struct ath12k_hw *ah = ahvif->ah;
4253 
4254 	lockdep_assert_wiphy(ah->hw->wiphy);
4255 
4256 	rcu_assign_pointer(ahvif->link[arvif->link_id], NULL);
4257 	synchronize_rcu();
4258 	ahvif->links_map &= ~BIT(arvif->link_id);
4259 
4260 	if (arvif != &ahvif->deflink)
4261 		kfree(arvif);
4262 	else
4263 		memset(arvif, 0, sizeof(*arvif));
4264 }
4265 
4266 int
4267 ath12k_mac_op_change_vif_links(struct ieee80211_hw *hw,
4268 			       struct ieee80211_vif *vif,
4269 			       u16 old_links, u16 new_links,
4270 			       struct ieee80211_bss_conf *ol[IEEE80211_MLD_MAX_NUM_LINKS])
4271 {
4272 	struct ath12k_vif *ahvif = ath12k_vif_to_ahvif(vif);
4273 	unsigned long to_remove = old_links & ~new_links;
4274 	unsigned long to_add = ~old_links & new_links;
4275 	struct ath12k_hw *ah = ath12k_hw_to_ah(hw);
4276 	struct ath12k_link_vif *arvif;
4277 	u8 link_id;
4278 
4279 	lockdep_assert_wiphy(hw->wiphy);
4280 
4281 	ath12k_generic_dbg(ATH12K_DBG_MAC,
4282 			   "mac vif link changed for MLD %pM old_links 0x%x new_links 0x%x\n",
4283 			   vif->addr, old_links, new_links);
4284 
4285 	for_each_set_bit(link_id, &to_add, IEEE80211_MLD_MAX_NUM_LINKS) {
4286 		arvif = wiphy_dereference(hw->wiphy, ahvif->link[link_id]);
4287 		/* mac80211 wants to add link but driver already has the
4288 		 * link. This should not happen ideally.
4289 		 */
4290 		if (WARN_ON(arvif))
4291 			return -EINVAL;
4292 
4293 		arvif = ath12k_mac_assign_link_vif(ah, vif, link_id);
4294 		if (WARN_ON(!arvif))
4295 			return -EINVAL;
4296 	}
4297 
4298 	for_each_set_bit(link_id, &to_remove, IEEE80211_MLD_MAX_NUM_LINKS) {
4299 		arvif = wiphy_dereference(hw->wiphy, ahvif->link[link_id]);
4300 		if (WARN_ON(!arvif))
4301 			return -EINVAL;
4302 
4303 		if (!arvif->is_created) {
4304 			ath12k_mac_unassign_link_vif(arvif);
4305 			continue;
4306 		}
4307 
4308 		if (WARN_ON(!arvif->ar))
4309 			return -EINVAL;
4310 
4311 		ath12k_mac_remove_link_interface(hw, arvif);
4312 		ath12k_mac_unassign_link_vif(arvif);
4313 	}
4314 
4315 	return 0;
4316 }
4317 EXPORT_SYMBOL(ath12k_mac_op_change_vif_links);
4318 
4319 static int ath12k_mac_fils_discovery(struct ath12k_link_vif *arvif,
4320 				     struct ieee80211_bss_conf *info)
4321 {
4322 	struct ieee80211_vif *vif = ath12k_ahvif_to_vif(arvif->ahvif);
4323 	struct ath12k *ar = arvif->ar;
4324 	struct ieee80211_hw *hw = ath12k_ar_to_hw(ar);
4325 	struct sk_buff *tmpl;
4326 	int ret;
4327 	u32 interval;
4328 	bool unsol_bcast_probe_resp_enabled = false;
4329 
4330 	if (info->fils_discovery.max_interval) {
4331 		interval = info->fils_discovery.max_interval;
4332 
4333 		tmpl = ieee80211_get_fils_discovery_tmpl(hw, vif,
4334 							 info->link_id);
4335 		if (tmpl)
4336 			ret = ath12k_wmi_fils_discovery_tmpl(ar, arvif->vdev_id,
4337 							     tmpl);
4338 	} else if (info->unsol_bcast_probe_resp_interval) {
4339 		unsol_bcast_probe_resp_enabled = 1;
4340 		interval = info->unsol_bcast_probe_resp_interval;
4341 
4342 		tmpl = ieee80211_get_unsol_bcast_probe_resp_tmpl(hw, vif,
4343 								 info->link_id);
4344 		if (tmpl)
4345 			ret = ath12k_wmi_probe_resp_tmpl(ar, arvif->vdev_id,
4346 							 tmpl);
4347 	} else { /* Disable */
4348 		return ath12k_wmi_fils_discovery(ar, arvif->vdev_id, 0, false);
4349 	}
4350 
4351 	if (!tmpl) {
4352 		ath12k_warn(ar->ab,
4353 			    "mac vdev %i failed to retrieve %s template\n",
4354 			    arvif->vdev_id, (unsol_bcast_probe_resp_enabled ?
4355 			    "unsolicited broadcast probe response" :
4356 			    "FILS discovery"));
4357 		return -EPERM;
4358 	}
4359 	kfree_skb(tmpl);
4360 
4361 	if (!ret)
4362 		ret = ath12k_wmi_fils_discovery(ar, arvif->vdev_id, interval,
4363 						unsol_bcast_probe_resp_enabled);
4364 
4365 	return ret;
4366 }
4367 
4368 void ath12k_mac_op_vif_cfg_changed(struct ieee80211_hw *hw,
4369 				   struct ieee80211_vif *vif,
4370 				   u64 changed)
4371 {
4372 	struct ath12k_vif *ahvif = ath12k_vif_to_ahvif(vif);
4373 	unsigned long links = ahvif->links_map;
4374 	struct ieee80211_bss_conf *info;
4375 	struct ath12k_link_vif *arvif;
4376 	struct ieee80211_sta *sta;
4377 	struct ath12k_sta *ahsta;
4378 	struct ath12k *ar;
4379 	u8 link_id;
4380 
4381 	lockdep_assert_wiphy(hw->wiphy);
4382 
4383 	if (changed & BSS_CHANGED_SSID && vif->type == NL80211_IFTYPE_AP) {
4384 		ahvif->u.ap.ssid_len = vif->cfg.ssid_len;
4385 		if (vif->cfg.ssid_len)
4386 			memcpy(ahvif->u.ap.ssid, vif->cfg.ssid, vif->cfg.ssid_len);
4387 	}
4388 
4389 	if (changed & BSS_CHANGED_ASSOC) {
4390 		if (vif->cfg.assoc) {
4391 			/* only in station mode we can get here, so it's safe
4392 			 * to use ap_addr
4393 			 */
4394 			rcu_read_lock();
4395 			sta = ieee80211_find_sta(vif, vif->cfg.ap_addr);
4396 			if (!sta) {
4397 				rcu_read_unlock();
4398 				WARN_ONCE(1, "failed to find sta with addr %pM\n",
4399 					  vif->cfg.ap_addr);
4400 				return;
4401 			}
4402 
4403 			ahsta = ath12k_sta_to_ahsta(sta);
4404 			arvif = wiphy_dereference(hw->wiphy,
4405 						  ahvif->link[ahsta->assoc_link_id]);
4406 			rcu_read_unlock();
4407 
4408 			ar = arvif->ar;
4409 			/* there is no reason for which an assoc link's
4410 			 * bss info does not exist
4411 			 */
4412 			info = ath12k_mac_get_link_bss_conf(arvif);
4413 			ath12k_bss_assoc(ar, arvif, info);
4414 
4415 			/* exclude assoc link as it is done above */
4416 			links &= ~BIT(ahsta->assoc_link_id);
4417 		}
4418 
4419 		for_each_set_bit(link_id, &links, IEEE80211_MLD_MAX_NUM_LINKS) {
4420 			arvif = wiphy_dereference(hw->wiphy, ahvif->link[link_id]);
4421 			if (!arvif || !arvif->ar)
4422 				continue;
4423 
4424 			ar = arvif->ar;
4425 
4426 			if (vif->cfg.assoc) {
4427 				info = ath12k_mac_get_link_bss_conf(arvif);
4428 				if (!info)
4429 					continue;
4430 
4431 				ath12k_bss_assoc(ar, arvif, info);
4432 			} else {
4433 				ath12k_bss_disassoc(ar, arvif);
4434 			}
4435 		}
4436 	}
4437 }
4438 EXPORT_SYMBOL(ath12k_mac_op_vif_cfg_changed);
4439 
4440 static void ath12k_mac_vif_setup_ps(struct ath12k_link_vif *arvif)
4441 {
4442 	struct ath12k *ar = arvif->ar;
4443 	struct ieee80211_vif *vif = arvif->ahvif->vif;
4444 	struct ieee80211_conf *conf = &ath12k_ar_to_hw(ar)->conf;
4445 	enum wmi_sta_powersave_param param;
4446 	struct ieee80211_bss_conf *info;
4447 	enum wmi_sta_ps_mode psmode;
4448 	int ret;
4449 	int timeout;
4450 	bool enable_ps;
4451 
4452 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
4453 
4454 	if (vif->type != NL80211_IFTYPE_STATION)
4455 		return;
4456 
4457 	enable_ps = arvif->ahvif->ps;
4458 	if (enable_ps) {
4459 		psmode = WMI_STA_PS_MODE_ENABLED;
4460 		param = WMI_STA_PS_PARAM_INACTIVITY_TIME;
4461 
4462 		timeout = conf->dynamic_ps_timeout;
4463 		if (timeout == 0) {
4464 			info = ath12k_mac_get_link_bss_conf(arvif);
4465 			if (!info) {
4466 				ath12k_warn(ar->ab, "unable to access bss link conf in setup ps for vif %pM link %u\n",
4467 					    vif->addr, arvif->link_id);
4468 				return;
4469 			}
4470 
4471 			/* firmware doesn't like 0 */
4472 			timeout = ieee80211_tu_to_usec(info->beacon_int) / 1000;
4473 		}
4474 
4475 		ret = ath12k_wmi_set_sta_ps_param(ar, arvif->vdev_id, param,
4476 						  timeout);
4477 		if (ret) {
4478 			ath12k_warn(ar->ab, "failed to set inactivity time for vdev %d: %i\n",
4479 				    arvif->vdev_id, ret);
4480 			return;
4481 		}
4482 	} else {
4483 		psmode = WMI_STA_PS_MODE_DISABLED;
4484 	}
4485 
4486 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac vdev %d psmode %s\n",
4487 		   arvif->vdev_id, psmode ? "enable" : "disable");
4488 
4489 	ret = ath12k_wmi_pdev_set_ps_mode(ar, arvif->vdev_id, psmode);
4490 	if (ret)
4491 		ath12k_warn(ar->ab, "failed to set sta power save mode %d for vdev %d: %d\n",
4492 			    psmode, arvif->vdev_id, ret);
4493 }
4494 
4495 static bool ath12k_mac_supports_tpc(struct ath12k *ar, struct ath12k_vif *ahvif,
4496 				    const struct cfg80211_chan_def *chandef)
4497 {
4498 	return ath12k_wmi_supports_6ghz_cc_ext(ar) &&
4499 		test_bit(WMI_TLV_SERVICE_EXT_TPC_REG_SUPPORT, ar->ab->wmi_ab.svc_map) &&
4500 		(ahvif->vdev_type == WMI_VDEV_TYPE_STA  ||
4501 		 ahvif->vdev_type == WMI_VDEV_TYPE_AP) &&
4502 		ahvif->vdev_subtype == WMI_VDEV_SUBTYPE_NONE &&
4503 		chandef->chan &&
4504 		chandef->chan->band == NL80211_BAND_6GHZ;
4505 }
4506 
4507 static void ath12k_wmi_vdev_params_up(struct ath12k *ar,
4508 				      struct ath12k_link_vif *arvif,
4509 				      struct ath12k_link_vif *tx_arvif,
4510 				      struct ieee80211_bss_conf *info, u16 aid)
4511 {
4512 	struct ath12k_wmi_vdev_up_params params = {
4513 		.vdev_id = arvif->vdev_id,
4514 		.aid = aid,
4515 		.bssid = arvif->bssid
4516 	};
4517 	int ret;
4518 
4519 	if (tx_arvif) {
4520 		params.tx_bssid = tx_arvif->bssid;
4521 		params.nontx_profile_idx = info->bssid_index;
4522 		params.nontx_profile_cnt = 1 << info->bssid_indicator;
4523 	}
4524 
4525 	ret = ath12k_wmi_vdev_up(arvif->ar, &params);
4526 	if (ret)
4527 		ath12k_warn(ar->ab, "failed to bring vdev up %d: %d\n",
4528 			    arvif->vdev_id, ret);
4529 }
4530 
4531 static int ath12k_mac_config_obss_pd(struct ath12k_link_vif *arvif,
4532 				     const struct ieee80211_he_obss_pd *he_obss_pd)
4533 {
4534 	struct ath12k_wmi_obss_pd_arg obss_pd_arg = {};
4535 	u32 srg_bitmap[2], non_srg_bitmap[2];
4536 	struct ath12k *ar = arvif->ar;
4537 	u32 param_id, pdev_id;
4538 	u32 param_val;
4539 	int ret;
4540 
4541 	if (ar->ab->hw_params->single_pdev_only)
4542 		pdev_id = ath12k_mac_get_target_pdev_id_from_vif(arvif);
4543 	else
4544 		pdev_id = ar->pdev->pdev_id;
4545 
4546 	/* Set and enable SRG/non-SRG OBSS PD threshold */
4547 	param_id = WMI_PDEV_PARAM_SET_CMD_OBSS_PD_THRESHOLD;
4548 	if (ar->monitor_started || !he_obss_pd->enable) {
4549 		ret = ath12k_wmi_pdev_set_param(ar, param_id, 0, pdev_id);
4550 		if (ret)
4551 			ath12k_warn(ar->ab,
4552 				    "failed to set OBSS PD threshold for pdev %u: %d\n",
4553 				    pdev_id, ret);
4554 		return ret;
4555 	}
4556 
4557 	/*
4558 	 * This service flag indicates firmware support for SRG/SRP-based
4559 	 * spatial reuse. It also specifies whether OBSS PD threshold values
4560 	 * should be interpreted as dB (offset) or dBm (absolute) units.
4561 	 */
4562 	obss_pd_arg.srp_support = test_bit(WMI_TLV_SERVICE_SRG_SRP_SPATIAL_REUSE_SUPPORT,
4563 					   ar->ab->wmi_ab.svc_map);
4564 
4565 	if (!(he_obss_pd->sr_ctrl &
4566 	      IEEE80211_HE_SPR_NON_SRG_OBSS_PD_SR_DISALLOWED)) {
4567 		if (he_obss_pd->sr_ctrl & IEEE80211_HE_SPR_NON_SRG_OFFSET_PRESENT)
4568 			obss_pd_arg.non_srg_th = ATH12K_OBSS_PD_MAX_THRESHOLD +
4569 						 he_obss_pd->non_srg_max_offset;
4570 		else
4571 			obss_pd_arg.non_srg_th = ATH12K_OBSS_PD_NON_SRG_MAX_THRESHOLD;
4572 
4573 		if (!obss_pd_arg.srp_support)
4574 			obss_pd_arg.non_srg_th -= ATH12K_DEFAULT_NOISE_FLOOR;
4575 
4576 		obss_pd_arg.non_srg_enabled = true;
4577 	}
4578 
4579 	if (he_obss_pd->sr_ctrl & IEEE80211_HE_SPR_SRG_INFORMATION_PRESENT) {
4580 		obss_pd_arg.srg_th = ATH12K_OBSS_PD_MAX_THRESHOLD +
4581 				     he_obss_pd->max_offset;
4582 		obss_pd_arg.srg_enabled = true;
4583 	}
4584 
4585 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
4586 		   "pdev %u OBSS PD sr_ctrl 0x%x srg_th %d dBm non_srg_th %d dBm\n",
4587 		   pdev_id, he_obss_pd->sr_ctrl,
4588 		   obss_pd_arg.srg_th, obss_pd_arg.non_srg_th);
4589 
4590 	param_val = ath12k_wmi_build_obss_pd(&obss_pd_arg);
4591 	ret = ath12k_wmi_pdev_set_param(ar, param_id, param_val, pdev_id);
4592 	if (ret) {
4593 		ath12k_warn(ar->ab,
4594 			    "failed to set OBSS PD threshold for pdev %u: %d\n",
4595 			    pdev_id, ret);
4596 		return ret;
4597 	}
4598 
4599 	/* Enable OBSS PD for all access category */
4600 	param_id  = WMI_PDEV_PARAM_SET_CMD_OBSS_PD_PER_AC;
4601 	param_val = 0xf;
4602 	ret = ath12k_wmi_pdev_set_param(ar, param_id, param_val, pdev_id);
4603 	if (ret) {
4604 		ath12k_warn(ar->ab,
4605 			    "failed to set OBSS PD per ac for pdev %u: %d\n",
4606 			    pdev_id, ret);
4607 		return ret;
4608 	}
4609 
4610 	/* Set SR prohibit */
4611 	param_id  = WMI_PDEV_PARAM_ENABLE_SR_PROHIBIT;
4612 	param_val = !!(he_obss_pd->sr_ctrl &
4613 		       IEEE80211_HE_SPR_HESIGA_SR_VAL15_ALLOWED);
4614 	ret = ath12k_wmi_pdev_set_param(ar, param_id, param_val, pdev_id);
4615 	if (ret) {
4616 		ath12k_warn(ar->ab, "failed to set SR prohibit for pdev %u: %d\n",
4617 			    pdev_id, ret);
4618 		return ret;
4619 	}
4620 
4621 	if (!obss_pd_arg.srp_support)
4622 		return 0;
4623 
4624 	memcpy(srg_bitmap, he_obss_pd->bss_color_bitmap, sizeof(srg_bitmap));
4625 	/* Set SRG BSS color bitmap */
4626 	ret = ath12k_wmi_pdev_set_srg_bss_color_bitmap(ar, pdev_id, srg_bitmap);
4627 	if (ret) {
4628 		ath12k_warn(ar->ab,
4629 			    "failed to set SRG bss color bitmap for pdev %u: %d\n",
4630 			    pdev_id, ret);
4631 		return ret;
4632 	}
4633 
4634 	/* Enable BSS colors for SRG */
4635 	ret = ath12k_wmi_pdev_srg_obss_color_enable_bitmap(ar, pdev_id, srg_bitmap);
4636 	if (ret) {
4637 		ath12k_warn(ar->ab,
4638 			    "failed to enable SRG bss color bitmap pdev %u: %d\n",
4639 			    pdev_id, ret);
4640 		return ret;
4641 	}
4642 
4643 	memcpy(srg_bitmap, he_obss_pd->partial_bssid_bitmap, sizeof(srg_bitmap));
4644 	/* Set SRG partial bssid bitmap */
4645 	ret = ath12k_wmi_pdev_set_srg_partial_bssid_bitmap(ar, pdev_id, srg_bitmap);
4646 	if (ret) {
4647 		ath12k_warn(ar->ab,
4648 			    "failed to set SRG partial bssid bitmap for pdev %u: %d\n",
4649 			    pdev_id, ret);
4650 		return ret;
4651 	}
4652 
4653 	/* Enable partial bssid mask for SRG */
4654 	ret = ath12k_wmi_pdev_srg_obss_bssid_enable_bitmap(ar, pdev_id, srg_bitmap);
4655 	if (ret) {
4656 		ath12k_warn(ar->ab,
4657 			    "failed to enable SRG bssid bitmap pdev %u: %d\n",
4658 			    pdev_id, ret);
4659 		return ret;
4660 	}
4661 
4662 	/*
4663 	 * No explicit non-SRG bitmap from mac80211; enable all colors/bssids
4664 	 * as non-SRG candidates. Actual SRG members are filtered by SRG bitmaps.
4665 	 */
4666 	memset(non_srg_bitmap, 0xff, sizeof(non_srg_bitmap));
4667 
4668 	/* Enable BSS colors for non-SRG */
4669 	ret = ath12k_wmi_pdev_non_srg_obss_color_enable_bitmap(ar, pdev_id,
4670 							       non_srg_bitmap);
4671 	if (ret) {
4672 		ath12k_warn(ar->ab,
4673 			    "failed to enable non SRG color bitmap pdev %u: %d\n",
4674 			    pdev_id, ret);
4675 		return ret;
4676 	}
4677 
4678 	/* Enable partial bssid mask for non-SRG */
4679 	ret = ath12k_wmi_pdev_non_srg_obss_bssid_enable_bitmap(ar, pdev_id,
4680 							       non_srg_bitmap);
4681 	if (ret) {
4682 		ath12k_warn(ar->ab,
4683 			    "failed to enable non SRG bssid bitmap pdev %u: %d\n",
4684 			    pdev_id, ret);
4685 		return ret;
4686 	}
4687 
4688 	return 0;
4689 }
4690 
4691 static void ath12k_mac_bss_info_changed(struct ath12k *ar,
4692 					struct ath12k_link_vif *arvif,
4693 					struct ieee80211_bss_conf *info,
4694 					u64 changed)
4695 {
4696 	struct ath12k_vif *ahvif = arvif->ahvif;
4697 	struct ieee80211_vif *vif = ath12k_ahvif_to_vif(ahvif);
4698 	struct ieee80211_vif_cfg *vif_cfg = &vif->cfg;
4699 	struct ath12k_link_vif *tx_arvif;
4700 	struct cfg80211_chan_def def;
4701 	u32 param_id, param_value;
4702 	enum nl80211_band band;
4703 	u32 vdev_param;
4704 	int mcast_rate;
4705 	u32 preamble;
4706 	u16 hw_value;
4707 	u16 bitrate;
4708 	u8 rateidx;
4709 	u32 rate;
4710 	int ret;
4711 
4712 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
4713 
4714 	if (changed & BSS_CHANGED_BEACON_INT) {
4715 		arvif->beacon_interval = info->beacon_int;
4716 
4717 		param_id = WMI_VDEV_PARAM_BEACON_INTERVAL;
4718 		ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
4719 						    param_id,
4720 						    arvif->beacon_interval);
4721 		if (ret)
4722 			ath12k_warn(ar->ab, "Failed to set beacon interval for VDEV: %d\n",
4723 				    arvif->vdev_id);
4724 		else
4725 			ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
4726 				   "Beacon interval: %d set for VDEV: %d\n",
4727 				   arvif->beacon_interval, arvif->vdev_id);
4728 	}
4729 
4730 	if (changed & BSS_CHANGED_BEACON) {
4731 		param_id = WMI_PDEV_PARAM_BEACON_TX_MODE;
4732 		param_value = WMI_BEACON_BURST_MODE;
4733 		ret = ath12k_wmi_pdev_set_param(ar, param_id,
4734 						param_value, ar->pdev->pdev_id);
4735 		if (ret)
4736 			ath12k_warn(ar->ab, "Failed to set beacon mode for VDEV: %d\n",
4737 				    arvif->vdev_id);
4738 		else
4739 			ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
4740 				   "Set burst beacon mode for VDEV: %d\n",
4741 				   arvif->vdev_id);
4742 
4743 		/* In MBSSID case, need to install transmitting VIF's template first */
4744 
4745 		ret = ath12k_mac_setup_bcn_tmpl(arvif);
4746 		if (ret)
4747 			ath12k_warn(ar->ab, "failed to update bcn template: %d\n",
4748 				    ret);
4749 
4750 		if (!arvif->is_csa_in_progress)
4751 			goto skip_vdev_up;
4752 
4753 		tx_arvif = ath12k_mac_get_tx_arvif(arvif, info);
4754 		if (tx_arvif && arvif != tx_arvif && tx_arvif->is_csa_in_progress)
4755 			/* skip non tx vif's */
4756 			goto skip_vdev_up;
4757 
4758 		ath12k_wmi_vdev_params_up(ar, arvif, tx_arvif, info, ahvif->aid);
4759 
4760 		arvif->is_csa_in_progress = false;
4761 
4762 		if (tx_arvif && arvif == tx_arvif) {
4763 			struct ath12k_link_vif *arvif_itr;
4764 
4765 			list_for_each_entry(arvif_itr, &ar->arvifs, list) {
4766 				if (!arvif_itr->is_csa_in_progress)
4767 					continue;
4768 
4769 				ath12k_wmi_vdev_params_up(ar, arvif, tx_arvif,
4770 							  info, ahvif->aid);
4771 				arvif_itr->is_csa_in_progress = false;
4772 			}
4773 		}
4774 	}
4775 
4776 skip_vdev_up:
4777 
4778 	if (changed & (BSS_CHANGED_BEACON_INFO | BSS_CHANGED_BEACON)) {
4779 		arvif->dtim_period = info->dtim_period;
4780 
4781 		param_id = WMI_VDEV_PARAM_DTIM_PERIOD;
4782 		ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
4783 						    param_id,
4784 						    arvif->dtim_period);
4785 
4786 		if (ret)
4787 			ath12k_warn(ar->ab, "Failed to set dtim period for VDEV %d: %i\n",
4788 				    arvif->vdev_id, ret);
4789 		else
4790 			ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
4791 				   "DTIM period: %d set for VDEV: %d\n",
4792 				   arvif->dtim_period, arvif->vdev_id);
4793 	}
4794 
4795 	if (changed & BSS_CHANGED_SSID &&
4796 	    vif->type == NL80211_IFTYPE_AP) {
4797 		ahvif->u.ap.ssid_len = vif->cfg.ssid_len;
4798 		if (vif->cfg.ssid_len)
4799 			memcpy(ahvif->u.ap.ssid, vif->cfg.ssid, vif->cfg.ssid_len);
4800 		ahvif->u.ap.hidden_ssid = info->hidden_ssid;
4801 	}
4802 
4803 	if (changed & BSS_CHANGED_BSSID && !is_zero_ether_addr(info->bssid))
4804 		ether_addr_copy(arvif->bssid, info->bssid);
4805 
4806 	if (changed & BSS_CHANGED_BEACON_ENABLED) {
4807 		if (info->enable_beacon) {
4808 			ret = ath12k_mac_set_he_txbf_conf(arvif);
4809 			if (ret)
4810 				ath12k_warn(ar->ab,
4811 					    "failed to set HE TXBF config for vdev: %d\n",
4812 					    arvif->vdev_id);
4813 
4814 			ret = ath12k_mac_set_eht_txbf_conf(arvif);
4815 			if (ret)
4816 				ath12k_warn(ar->ab,
4817 					    "failed to set EHT TXBF config for vdev: %d\n",
4818 					    arvif->vdev_id);
4819 		}
4820 		ath12k_control_beaconing(arvif, info);
4821 
4822 		if (arvif->is_up && info->he_support &&
4823 		    info->he_oper.params) {
4824 			/* TODO: Extend to support 1024 BA Bitmap size */
4825 			ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
4826 							    WMI_VDEV_PARAM_BA_MODE,
4827 							    WMI_BA_MODE_BUFFER_SIZE_256);
4828 			if (ret)
4829 				ath12k_warn(ar->ab,
4830 					    "failed to set BA BUFFER SIZE 256 for vdev: %d\n",
4831 					    arvif->vdev_id);
4832 
4833 			param_id = WMI_VDEV_PARAM_HEOPS_0_31;
4834 			param_value = info->he_oper.params;
4835 			ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
4836 							    param_id, param_value);
4837 			ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
4838 				   "he oper param: %x set for VDEV: %d\n",
4839 				   param_value, arvif->vdev_id);
4840 
4841 			if (ret)
4842 				ath12k_warn(ar->ab, "Failed to set he oper params %x for VDEV %d: %i\n",
4843 					    param_value, arvif->vdev_id, ret);
4844 		}
4845 	}
4846 
4847 	if (changed & BSS_CHANGED_ERP_CTS_PROT) {
4848 		u32 cts_prot;
4849 
4850 		cts_prot = !!(info->use_cts_prot);
4851 		param_id = WMI_VDEV_PARAM_PROTECTION_MODE;
4852 
4853 		if (arvif->is_started) {
4854 			ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
4855 							    param_id, cts_prot);
4856 			if (ret)
4857 				ath12k_warn(ar->ab, "Failed to set CTS prot for VDEV: %d\n",
4858 					    arvif->vdev_id);
4859 			else
4860 				ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "Set CTS prot: %d for VDEV: %d\n",
4861 					   cts_prot, arvif->vdev_id);
4862 		} else {
4863 			ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "defer protection mode setup, vdev is not ready yet\n");
4864 		}
4865 	}
4866 
4867 	if (changed & BSS_CHANGED_ERP_SLOT) {
4868 		u32 slottime;
4869 
4870 		if (info->use_short_slot)
4871 			slottime = WMI_VDEV_SLOT_TIME_SHORT; /* 9us */
4872 
4873 		else
4874 			slottime = WMI_VDEV_SLOT_TIME_LONG; /* 20us */
4875 
4876 		param_id = WMI_VDEV_PARAM_SLOT_TIME;
4877 		ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
4878 						    param_id, slottime);
4879 		if (ret)
4880 			ath12k_warn(ar->ab, "Failed to set erp slot for VDEV: %d\n",
4881 				    arvif->vdev_id);
4882 		else
4883 			ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
4884 				   "Set slottime: %d for VDEV: %d\n",
4885 				   slottime, arvif->vdev_id);
4886 	}
4887 
4888 	if (changed & BSS_CHANGED_ERP_PREAMBLE) {
4889 		u32 preamble;
4890 
4891 		if (info->use_short_preamble)
4892 			preamble = WMI_VDEV_PREAMBLE_SHORT;
4893 		else
4894 			preamble = WMI_VDEV_PREAMBLE_LONG;
4895 
4896 		param_id = WMI_VDEV_PARAM_PREAMBLE;
4897 		ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
4898 						    param_id, preamble);
4899 		if (ret)
4900 			ath12k_warn(ar->ab, "Failed to set preamble for VDEV: %d\n",
4901 				    arvif->vdev_id);
4902 		else
4903 			ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
4904 				   "Set preamble: %d for VDEV: %d\n",
4905 				   preamble, arvif->vdev_id);
4906 	}
4907 
4908 	if (changed & BSS_CHANGED_ASSOC) {
4909 		if (vif->cfg.assoc)
4910 			ath12k_bss_assoc(ar, arvif, info);
4911 		else
4912 			ath12k_bss_disassoc(ar, arvif);
4913 	}
4914 
4915 	if (changed & BSS_CHANGED_TXPOWER) {
4916 		ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac vdev_id %i txpower %d\n",
4917 			   arvif->vdev_id, info->txpower);
4918 
4919 		arvif->txpower = info->txpower;
4920 		ath12k_mac_txpower_recalc(ar);
4921 	}
4922 
4923 	if (changed & BSS_CHANGED_MCAST_RATE &&
4924 	    !ath12k_mac_vif_link_chan(vif, arvif->link_id, &def)) {
4925 		band = def.chan->band;
4926 		mcast_rate = info->mcast_rate[band];
4927 
4928 		if (mcast_rate > 0) {
4929 			rateidx = mcast_rate - 1;
4930 		} else {
4931 			if (info->basic_rates)
4932 				rateidx = __ffs(info->basic_rates);
4933 			else
4934 				rateidx = 0;
4935 		}
4936 
4937 		if (ar->pdev->cap.supported_bands & WMI_HOST_WLAN_5GHZ_CAP)
4938 			rateidx += ATH12K_MAC_FIRST_OFDM_RATE_IDX;
4939 
4940 		bitrate = ath12k_legacy_rates[rateidx].bitrate;
4941 		hw_value = ath12k_legacy_rates[rateidx].hw_value;
4942 
4943 		if (ath12k_mac_bitrate_is_cck(bitrate))
4944 			preamble = WMI_RATE_PREAMBLE_CCK;
4945 		else
4946 			preamble = WMI_RATE_PREAMBLE_OFDM;
4947 
4948 		rate = ATH12K_HW_RATE_CODE(hw_value, 0, preamble);
4949 
4950 		ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
4951 			   "mac vdev %d mcast_rate %x\n",
4952 			   arvif->vdev_id, rate);
4953 
4954 		vdev_param = WMI_VDEV_PARAM_MCAST_DATA_RATE;
4955 		ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
4956 						    vdev_param, rate);
4957 		if (ret)
4958 			ath12k_warn(ar->ab,
4959 				    "failed to set mcast rate on vdev %i: %d\n",
4960 				    arvif->vdev_id,  ret);
4961 
4962 		vdev_param = WMI_VDEV_PARAM_BCAST_DATA_RATE;
4963 		ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
4964 						    vdev_param, rate);
4965 		if (ret)
4966 			ath12k_warn(ar->ab,
4967 				    "failed to set bcast rate on vdev %i: %d\n",
4968 				    arvif->vdev_id,  ret);
4969 	}
4970 
4971 	if (changed & BSS_CHANGED_BASIC_RATES &&
4972 	    !ath12k_mac_vif_link_chan(vif, arvif->link_id, &def))
4973 		ath12k_recalculate_mgmt_rate(ar, arvif, &def);
4974 
4975 	if (changed & BSS_CHANGED_TWT) {
4976 		if (info->twt_requester || info->twt_responder)
4977 			ath12k_wmi_send_twt_enable_cmd(ar, ar->pdev->pdev_id);
4978 		else
4979 			ath12k_wmi_send_twt_disable_cmd(ar, ar->pdev->pdev_id);
4980 	}
4981 
4982 	if (changed & BSS_CHANGED_HE_OBSS_PD) {
4983 		if (vif->type == NL80211_IFTYPE_AP)
4984 			ath12k_mac_config_obss_pd(arvif, &info->he_obss_pd);
4985 		else
4986 			ath12k_wmi_send_obss_spr_cmd(ar, arvif->vdev_id,
4987 						     &info->he_obss_pd);
4988 	}
4989 
4990 	if (changed & BSS_CHANGED_HE_BSS_COLOR) {
4991 		if (vif->type == NL80211_IFTYPE_AP) {
4992 			ret = ath12k_wmi_obss_color_cfg_cmd(ar,
4993 							    arvif->vdev_id,
4994 							    info->he_bss_color.color,
4995 							    ATH12K_BSS_COLOR_AP_PERIODS,
4996 							    info->he_bss_color.enabled);
4997 			if (ret)
4998 				ath12k_warn(ar->ab, "failed to set bss color collision on vdev %u: %d\n",
4999 					    arvif->vdev_id,  ret);
5000 
5001 			param_id = WMI_VDEV_PARAM_BSS_COLOR;
5002 			if (info->he_bss_color.enabled)
5003 				param_value = info->he_bss_color.color <<
5004 					      IEEE80211_HE_OPERATION_BSS_COLOR_OFFSET;
5005 			else
5006 				param_value = IEEE80211_HE_OPERATION_BSS_COLOR_DISABLED;
5007 
5008 			ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
5009 							    param_id,
5010 							    param_value);
5011 			if (ret)
5012 				ath12k_warn(ar->ab, "failed to set bss color param on vdev %u: %d\n",
5013 					    arvif->vdev_id,  ret);
5014 			else
5015 				ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "bss color param 0x%x set on vdev %u\n",
5016 					   param_value, arvif->vdev_id);
5017 		} else if (vif->type == NL80211_IFTYPE_STATION) {
5018 			ret = ath12k_wmi_send_bss_color_change_enable_cmd(ar,
5019 									  arvif->vdev_id,
5020 									  1);
5021 			if (ret)
5022 				ath12k_warn(ar->ab, "failed to enable bss color change on vdev %i: %d\n",
5023 					    arvif->vdev_id,  ret);
5024 			ret = ath12k_wmi_obss_color_cfg_cmd(ar,
5025 							    arvif->vdev_id,
5026 							    0,
5027 							    ATH12K_BSS_COLOR_STA_PERIODS,
5028 							    1);
5029 			if (ret)
5030 				ath12k_warn(ar->ab, "failed to set bss color collision on vdev %i: %d\n",
5031 					    arvif->vdev_id,  ret);
5032 		}
5033 	}
5034 
5035 	ath12k_mac_fils_discovery(arvif, info);
5036 
5037 	if (changed & BSS_CHANGED_PS &&
5038 	    ar->ab->hw_params->supports_sta_ps) {
5039 		ahvif->ps = vif_cfg->ps;
5040 		ath12k_mac_vif_setup_ps(arvif);
5041 	}
5042 }
5043 
5044 static struct ath12k_vif_cache *ath12k_ahvif_get_link_cache(struct ath12k_vif *ahvif,
5045 							    u8 link_id)
5046 {
5047 	if (!ahvif->cache[link_id]) {
5048 		ahvif->cache[link_id] = kzalloc_obj(*ahvif->cache[0]);
5049 		if (ahvif->cache[link_id])
5050 			INIT_LIST_HEAD(&ahvif->cache[link_id]->key_conf.list);
5051 	}
5052 
5053 	return ahvif->cache[link_id];
5054 }
5055 
5056 static void ath12k_ahvif_put_link_key_cache(struct ath12k_vif_cache *cache)
5057 {
5058 	struct ath12k_key_conf *key_conf, *tmp;
5059 
5060 	if (!cache || list_empty(&cache->key_conf.list))
5061 		return;
5062 	list_for_each_entry_safe(key_conf, tmp, &cache->key_conf.list, list) {
5063 		list_del(&key_conf->list);
5064 		kfree(key_conf);
5065 	}
5066 }
5067 
5068 static void ath12k_ahvif_put_link_cache(struct ath12k_vif *ahvif, u8 link_id)
5069 {
5070 	if (link_id >= IEEE80211_MLD_MAX_NUM_LINKS)
5071 		return;
5072 
5073 	ath12k_ahvif_put_link_key_cache(ahvif->cache[link_id]);
5074 	kfree(ahvif->cache[link_id]);
5075 	ahvif->cache[link_id] = NULL;
5076 }
5077 
5078 void ath12k_mac_op_link_info_changed(struct ieee80211_hw *hw,
5079 				     struct ieee80211_vif *vif,
5080 				     struct ieee80211_bss_conf *info,
5081 				     u64 changed)
5082 {
5083 	struct ath12k *ar;
5084 	struct ath12k_vif *ahvif = ath12k_vif_to_ahvif(vif);
5085 	struct ath12k_vif_cache *cache;
5086 	struct ath12k_link_vif *arvif;
5087 	u8 link_id = info->link_id;
5088 
5089 	lockdep_assert_wiphy(hw->wiphy);
5090 
5091 	arvif = wiphy_dereference(hw->wiphy, ahvif->link[link_id]);
5092 
5093 	/* if the vdev is not created on a certain radio,
5094 	 * cache the info to be updated later on vdev creation
5095 	 */
5096 
5097 	if (!arvif || !arvif->is_created) {
5098 		cache = ath12k_ahvif_get_link_cache(ahvif, link_id);
5099 		if (!cache)
5100 			return;
5101 
5102 		cache->bss_conf_changed |= changed;
5103 
5104 		return;
5105 	}
5106 
5107 	ar = arvif->ar;
5108 
5109 	ath12k_mac_bss_info_changed(ar, arvif, info, changed);
5110 }
5111 EXPORT_SYMBOL(ath12k_mac_op_link_info_changed);
5112 
5113 static struct ath12k*
5114 ath12k_mac_select_scan_device(struct ieee80211_hw *hw,
5115 			      struct ieee80211_vif *vif,
5116 			      u32 center_freq)
5117 {
5118 	struct ath12k_hw *ah = hw->priv;
5119 	enum nl80211_band band;
5120 	struct ath12k *ar;
5121 	int i;
5122 
5123 	if (ah->num_radio == 1)
5124 		return ah->radio;
5125 
5126 	/* Currently mac80211 supports splitting scan requests into
5127 	 * multiple scan requests per band.
5128 	 * Loop through first channel and determine the scan radio
5129 	 * TODO: There could be 5 GHz low/high channels in that case
5130 	 * split the hw request and perform multiple scans
5131 	 */
5132 
5133 	if (center_freq < ATH12K_MIN_5GHZ_FREQ)
5134 		band = NL80211_BAND_2GHZ;
5135 	else if (center_freq < ATH12K_MIN_6GHZ_FREQ)
5136 		band = NL80211_BAND_5GHZ;
5137 	else
5138 		band = NL80211_BAND_6GHZ;
5139 
5140 	for_each_ar(ah, ar, i) {
5141 		if (ar->mac.sbands[band].channels &&
5142 		    center_freq >= KHZ_TO_MHZ(ar->freq_range.start_freq) &&
5143 		    center_freq <= KHZ_TO_MHZ(ar->freq_range.end_freq))
5144 			return ar;
5145 	}
5146 
5147 	return NULL;
5148 }
5149 
5150 void __ath12k_mac_scan_finish(struct ath12k *ar)
5151 {
5152 	struct ieee80211_hw *hw = ath12k_ar_to_hw(ar);
5153 
5154 	lockdep_assert_held(&ar->data_lock);
5155 
5156 	switch (ar->scan.state) {
5157 	case ATH12K_SCAN_IDLE:
5158 		break;
5159 	case ATH12K_SCAN_RUNNING:
5160 	case ATH12K_SCAN_ABORTING:
5161 		if (ar->scan.is_roc && ar->scan.roc_notify)
5162 			ieee80211_remain_on_channel_expired(hw);
5163 		fallthrough;
5164 	case ATH12K_SCAN_STARTING:
5165 		cancel_delayed_work(&ar->scan.timeout);
5166 		complete_all(&ar->scan.completed);
5167 		wiphy_work_queue(ar->ah->hw->wiphy, &ar->scan.vdev_clean_wk);
5168 		break;
5169 	}
5170 }
5171 
5172 void ath12k_mac_scan_finish(struct ath12k *ar)
5173 {
5174 	spin_lock_bh(&ar->data_lock);
5175 	__ath12k_mac_scan_finish(ar);
5176 	spin_unlock_bh(&ar->data_lock);
5177 }
5178 
5179 static int ath12k_scan_stop(struct ath12k *ar)
5180 {
5181 	struct ath12k_wmi_scan_cancel_arg arg = {
5182 		.req_type = WLAN_SCAN_CANCEL_SINGLE,
5183 		.scan_id = ATH12K_SCAN_ID,
5184 	};
5185 	int ret;
5186 
5187 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
5188 
5189 	/* TODO: Fill other STOP Params */
5190 	arg.pdev_id = ar->pdev->pdev_id;
5191 
5192 	ret = ath12k_wmi_send_scan_stop_cmd(ar, &arg);
5193 	if (ret) {
5194 		ath12k_warn(ar->ab, "failed to stop wmi scan: %d\n", ret);
5195 		goto out;
5196 	}
5197 
5198 	ret = wait_for_completion_timeout(&ar->scan.completed, 3 * HZ);
5199 	if (ret == 0) {
5200 		ath12k_warn(ar->ab,
5201 			    "failed to receive scan abort comple: timed out\n");
5202 		ret = -ETIMEDOUT;
5203 	} else if (ret > 0) {
5204 		ret = 0;
5205 	}
5206 
5207 out:
5208 	/* Scan state should be updated in scan completion worker but in
5209 	 * case firmware fails to deliver the event (for whatever reason)
5210 	 * it is desired to clean up scan state anyway. Firmware may have
5211 	 * just dropped the scan completion event delivery due to transport
5212 	 * pipe being overflown with data and/or it can recover on its own
5213 	 * before next scan request is submitted.
5214 	 */
5215 	spin_lock_bh(&ar->data_lock);
5216 	if (ret)
5217 		__ath12k_mac_scan_finish(ar);
5218 	spin_unlock_bh(&ar->data_lock);
5219 
5220 	return ret;
5221 }
5222 
5223 static void ath12k_scan_abort(struct ath12k *ar)
5224 {
5225 	int ret;
5226 
5227 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
5228 
5229 	spin_lock_bh(&ar->data_lock);
5230 
5231 	switch (ar->scan.state) {
5232 	case ATH12K_SCAN_IDLE:
5233 		/* This can happen if timeout worker kicked in and called
5234 		 * abortion while scan completion was being processed.
5235 		 */
5236 		break;
5237 	case ATH12K_SCAN_STARTING:
5238 	case ATH12K_SCAN_ABORTING:
5239 		ath12k_warn(ar->ab, "refusing scan abortion due to invalid scan state: %d\n",
5240 			    ar->scan.state);
5241 		break;
5242 	case ATH12K_SCAN_RUNNING:
5243 		ar->scan.state = ATH12K_SCAN_ABORTING;
5244 		spin_unlock_bh(&ar->data_lock);
5245 
5246 		ret = ath12k_scan_stop(ar);
5247 		if (ret)
5248 			ath12k_warn(ar->ab, "failed to abort scan: %d\n", ret);
5249 
5250 		spin_lock_bh(&ar->data_lock);
5251 		break;
5252 	}
5253 
5254 	spin_unlock_bh(&ar->data_lock);
5255 }
5256 
5257 static void ath12k_scan_timeout_work(struct work_struct *work)
5258 {
5259 	struct ath12k *ar = container_of(work, struct ath12k,
5260 					 scan.timeout.work);
5261 
5262 	wiphy_lock(ath12k_ar_to_hw(ar)->wiphy);
5263 	ath12k_scan_abort(ar);
5264 	wiphy_unlock(ath12k_ar_to_hw(ar)->wiphy);
5265 }
5266 
5267 static void ath12k_mac_scan_send_complete(struct ath12k *ar,
5268 					  struct cfg80211_scan_info *info)
5269 {
5270 	struct ath12k_hw *ah = ar->ah;
5271 	struct ath12k *partner_ar;
5272 	int i;
5273 
5274 	lockdep_assert_wiphy(ah->hw->wiphy);
5275 
5276 	for_each_ar(ah, partner_ar, i)
5277 		if (partner_ar != ar &&
5278 		    partner_ar->scan.state == ATH12K_SCAN_RUNNING)
5279 			return;
5280 
5281 	ieee80211_scan_completed(ah->hw, info);
5282 }
5283 
5284 static void ath12k_scan_vdev_clean_work(struct wiphy *wiphy, struct wiphy_work *work)
5285 {
5286 	struct ath12k *ar = container_of(work, struct ath12k,
5287 					 scan.vdev_clean_wk);
5288 	struct ath12k_hw *ah = ar->ah;
5289 	struct ath12k_link_vif *arvif;
5290 
5291 	lockdep_assert_wiphy(wiphy);
5292 
5293 	arvif = ar->scan.arvif;
5294 
5295 	/* The scan vdev has already been deleted. This can occur when a
5296 	 * new scan request is made on the same vif with a different
5297 	 * frequency, causing the scan arvif to move from one radio to
5298 	 * another. Or, scan was abrupted and via remove interface, the
5299 	 * arvif is already deleted. Alternatively, if the scan vdev is not
5300 	 * being used as an actual vdev, then do not delete it.
5301 	 */
5302 	if (!arvif || arvif->is_started)
5303 		goto work_complete;
5304 
5305 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac clean scan vdev (link id %u)",
5306 		   arvif->link_id);
5307 
5308 	ath12k_mac_remove_link_interface(ah->hw, arvif);
5309 	ath12k_mac_unassign_link_vif(arvif);
5310 
5311 work_complete:
5312 	spin_lock_bh(&ar->data_lock);
5313 	ar->scan.arvif = NULL;
5314 	if (!ar->scan.is_roc) {
5315 		struct cfg80211_scan_info info = {
5316 			.aborted = ((ar->scan.state ==
5317 				    ATH12K_SCAN_ABORTING) ||
5318 				    (ar->scan.state ==
5319 				    ATH12K_SCAN_STARTING)),
5320 		};
5321 
5322 		ath12k_mac_scan_send_complete(ar, &info);
5323 	}
5324 
5325 	ar->scan.state = ATH12K_SCAN_IDLE;
5326 	ar->scan_channel = NULL;
5327 	ar->scan.roc_freq = 0;
5328 	spin_unlock_bh(&ar->data_lock);
5329 }
5330 
5331 static int ath12k_start_scan(struct ath12k *ar,
5332 			     struct ath12k_wmi_scan_req_arg *arg)
5333 {
5334 	int ret;
5335 
5336 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
5337 
5338 	ret = ath12k_wmi_send_scan_start_cmd(ar, arg);
5339 	if (ret)
5340 		return ret;
5341 
5342 	ret = wait_for_completion_timeout(&ar->scan.started, 1 * HZ);
5343 	if (ret == 0) {
5344 		ret = ath12k_scan_stop(ar);
5345 		if (ret)
5346 			ath12k_warn(ar->ab, "failed to stop scan: %d\n", ret);
5347 
5348 		return -ETIMEDOUT;
5349 	}
5350 
5351 	/* If we failed to start the scan, return error code at
5352 	 * this point.  This is probably due to some issue in the
5353 	 * firmware, but no need to wedge the driver due to that...
5354 	 */
5355 	spin_lock_bh(&ar->data_lock);
5356 	if (ar->scan.state == ATH12K_SCAN_IDLE) {
5357 		spin_unlock_bh(&ar->data_lock);
5358 		return -EINVAL;
5359 	}
5360 	spin_unlock_bh(&ar->data_lock);
5361 
5362 	return 0;
5363 }
5364 
5365 int ath12k_mac_get_fw_stats(struct ath12k *ar,
5366 			    struct ath12k_fw_stats_req_params *param)
5367 {
5368 	struct ath12k_base *ab = ar->ab;
5369 	struct ath12k_hw *ah = ath12k_ar_to_ah(ar);
5370 	unsigned long time_left;
5371 	int ret;
5372 
5373 	guard(mutex)(&ah->hw_mutex);
5374 
5375 	if (ah->state != ATH12K_HW_STATE_ON)
5376 		return -ENETDOWN;
5377 
5378 	reinit_completion(&ar->fw_stats_complete);
5379 	reinit_completion(&ar->fw_stats_done);
5380 
5381 	ret = ath12k_wmi_send_stats_request_cmd(ar, param->stats_id,
5382 						param->vdev_id, param->pdev_id);
5383 	if (ret) {
5384 		ath12k_warn(ab, "failed to request fw stats: %d\n", ret);
5385 		return ret;
5386 	}
5387 
5388 	ath12k_dbg(ab, ATH12K_DBG_WMI,
5389 		   "get fw stat pdev id %d vdev id %d stats id 0x%x\n",
5390 		   param->pdev_id, param->vdev_id, param->stats_id);
5391 
5392 	time_left = wait_for_completion_timeout(&ar->fw_stats_complete, 1 * HZ);
5393 	if (!time_left) {
5394 		ath12k_warn(ab, "time out while waiting for get fw stats\n");
5395 		return -ETIMEDOUT;
5396 	}
5397 
5398 	/* Firmware sends WMI_UPDATE_STATS_EVENTID back-to-back
5399 	 * when stats data buffer limit is reached. fw_stats_complete
5400 	 * is completed once host receives first event from firmware, but
5401 	 * still there could be more events following. Below is to wait
5402 	 * until firmware completes sending all the events.
5403 	 */
5404 	time_left = wait_for_completion_timeout(&ar->fw_stats_done, 3 * HZ);
5405 	if (!time_left) {
5406 		ath12k_warn(ab, "time out while waiting for fw stats done\n");
5407 		return -ETIMEDOUT;
5408 	}
5409 
5410 	return 0;
5411 }
5412 
5413 int ath12k_mac_op_get_txpower(struct ieee80211_hw *hw,
5414 			      struct ieee80211_vif *vif,
5415 			      unsigned int link_id,
5416 			      int *dbm)
5417 {
5418 	struct ath12k_vif *ahvif = ath12k_vif_to_ahvif(vif);
5419 	struct ath12k_fw_stats_req_params params = {};
5420 	struct ath12k_fw_stats_pdev *pdev;
5421 	struct ath12k_hw *ah = hw->priv;
5422 	struct ath12k_link_vif *arvif;
5423 	struct ath12k_base *ab;
5424 	struct ath12k *ar;
5425 	int ret;
5426 
5427 	/* Final Tx power is minimum of Target Power, CTL power, Regulatory
5428 	 * Power, PSD EIRP Power. We just know the Regulatory power from the
5429 	 * regulatory rules obtained. FW knows all these power and sets the min
5430 	 * of these. Hence, we request the FW pdev stats in which FW reports
5431 	 * the minimum of all vdev's channel Tx power.
5432 	 */
5433 	lockdep_assert_wiphy(hw->wiphy);
5434 
5435 	arvif = wiphy_dereference(ah->hw->wiphy, ahvif->link[link_id]);
5436 	if (!arvif || !arvif->ar)
5437 		return -EINVAL;
5438 
5439 	ar = arvif->ar;
5440 	ab = ar->ab;
5441 	if (ah->state != ATH12K_HW_STATE_ON)
5442 		goto err_fallback;
5443 
5444 	if (test_bit(ATH12K_FLAG_CAC_RUNNING, &ar->dev_flags))
5445 		return -EAGAIN;
5446 
5447 	/* Limit the requests to Firmware for fetching the tx power */
5448 	if (ar->chan_tx_pwr != ATH12K_PDEV_TX_POWER_INVALID &&
5449 	    time_before(jiffies,
5450 			msecs_to_jiffies(ATH12K_PDEV_TX_POWER_REFRESH_TIME_MSECS) +
5451 					 ar->last_tx_power_update))
5452 		goto send_tx_power;
5453 
5454 	params.pdev_id = ath12k_mac_get_target_pdev_id(ar);
5455 	params.vdev_id = arvif->vdev_id;
5456 	params.stats_id = WMI_REQUEST_PDEV_STAT;
5457 	ret = ath12k_mac_get_fw_stats(ar, &params);
5458 	if (ret) {
5459 		ath12k_warn(ab, "failed to request fw pdev stats: %d\n", ret);
5460 		goto err_fallback;
5461 	}
5462 
5463 	spin_lock_bh(&ar->data_lock);
5464 	pdev = list_first_entry_or_null(&ar->fw_stats.pdevs,
5465 					struct ath12k_fw_stats_pdev, list);
5466 	if (!pdev) {
5467 		spin_unlock_bh(&ar->data_lock);
5468 		goto err_fallback;
5469 	}
5470 
5471 	/* tx power reported by firmware is in units of 0.5 dBm */
5472 	ar->chan_tx_pwr = pdev->chan_tx_power / 2;
5473 	spin_unlock_bh(&ar->data_lock);
5474 	ar->last_tx_power_update = jiffies;
5475 	ath12k_fw_stats_reset(ar);
5476 
5477 send_tx_power:
5478 	*dbm = ar->chan_tx_pwr;
5479 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "txpower fetched from firmware %d dBm\n",
5480 		   *dbm);
5481 	return 0;
5482 
5483 err_fallback:
5484 	/* We didn't get txpower from FW. Hence, relying on vif->bss_conf.txpower */
5485 	*dbm = vif->bss_conf.txpower;
5486 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "txpower from firmware NaN, reported %d dBm\n",
5487 		   *dbm);
5488 	return 0;
5489 }
5490 EXPORT_SYMBOL(ath12k_mac_op_get_txpower);
5491 
5492 static u8
5493 ath12k_mac_find_link_id_by_ar(struct ath12k_vif *ahvif, struct ath12k *ar)
5494 {
5495 	struct ath12k_link_vif *arvif;
5496 	struct ath12k_hw *ah = ahvif->ah;
5497 	unsigned long links = ahvif->links_map;
5498 	unsigned long scan_links_map;
5499 	u8 link_id;
5500 
5501 	lockdep_assert_wiphy(ah->hw->wiphy);
5502 
5503 	for_each_set_bit(link_id, &links, ATH12K_NUM_MAX_LINKS) {
5504 		arvif = wiphy_dereference(ah->hw->wiphy, ahvif->link[link_id]);
5505 
5506 		if (!arvif || !arvif->is_created)
5507 			continue;
5508 
5509 		if (ar == arvif->ar)
5510 			return link_id;
5511 	}
5512 
5513 	/* input ar is not assigned to any of the links of ML VIF, use next
5514 	 * available scan link for scan vdev creation. There are cases where
5515 	 * single scan req needs to be split in driver and initiate separate
5516 	 * scan requests to firmware based on device.
5517 	 */
5518 
5519 	 /* Unset all non-scan links (0-14) of scan_links_map so that ffs() will
5520 	  * choose an available link among scan links (i.e link id >= 15)
5521 	  */
5522 	scan_links_map = ~ahvif->links_map & ATH12K_SCAN_LINKS_MASK;
5523 	if (scan_links_map)
5524 		return __ffs(scan_links_map);
5525 
5526 	return ATH12K_FIRST_SCAN_LINK;
5527 }
5528 
5529 static int ath12k_mac_initiate_hw_scan(struct ieee80211_hw *hw,
5530 				       struct ieee80211_vif *vif,
5531 				       struct ieee80211_scan_request *hw_req,
5532 				       int n_channels,
5533 				       struct ieee80211_channel **chan_list,
5534 				       struct ath12k *ar)
5535 {
5536 	struct ath12k_hw *ah = ath12k_hw_to_ah(hw);
5537 	struct ath12k_vif *ahvif = ath12k_vif_to_ahvif(vif);
5538 	struct ath12k_link_vif *arvif;
5539 	struct cfg80211_scan_request *req = &hw_req->req;
5540 	struct ath12k_wmi_scan_req_arg *arg = NULL;
5541 	u8 link_id;
5542 	int ret;
5543 	int i;
5544 	bool create = true;
5545 
5546 	lockdep_assert_wiphy(hw->wiphy);
5547 
5548 	arvif = &ahvif->deflink;
5549 
5550 	/* check if any of the links of ML VIF is already started on
5551 	 * radio(ar) corresponding to given scan frequency and use it,
5552 	 * if not use scan link (link id >= 15) for scan purpose.
5553 	 */
5554 	link_id = ath12k_mac_find_link_id_by_ar(ahvif, ar);
5555 	/* All scan links are occupied. ideally this shouldn't happen as
5556 	 * mac80211 won't schedule scan for same band until ongoing scan is
5557 	 * completed, don't try to exceed max links just in case if it happens.
5558 	 */
5559 	if (link_id >= ATH12K_NUM_MAX_LINKS)
5560 		return -EBUSY;
5561 
5562 	arvif = ath12k_mac_assign_link_vif(ah, vif, link_id);
5563 
5564 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac link ID %d selected for scan",
5565 		   arvif->link_id);
5566 
5567 	/* If the vif is already assigned to a specific vdev of an ar,
5568 	 * check whether its already started, vdev which is started
5569 	 * are not allowed to switch to a new radio.
5570 	 * If the vdev is not started, but was earlier created on a
5571 	 * different ar, delete that vdev and create a new one. We don't
5572 	 * delete at the scan stop as an optimization to avoid redundant
5573 	 * delete-create vdev's for the same ar, in case the request is
5574 	 * always on the same band for the vif
5575 	 */
5576 	if (arvif->is_created) {
5577 		if (WARN_ON(!arvif->ar))
5578 			return -EINVAL;
5579 
5580 		if (ar != arvif->ar && arvif->is_started)
5581 			return -EINVAL;
5582 
5583 		if (ar != arvif->ar) {
5584 			ath12k_mac_remove_link_interface(hw, arvif);
5585 			ath12k_mac_unassign_link_vif(arvif);
5586 		} else {
5587 			create = false;
5588 		}
5589 	}
5590 
5591 	if (create) {
5592 		/* Previous arvif would've been cleared in radio switch block
5593 		 * above, assign arvif again for create.
5594 		 */
5595 		arvif = ath12k_mac_assign_link_vif(ah, vif, link_id);
5596 
5597 		ret = ath12k_mac_vdev_create(ar, arvif);
5598 		if (ret) {
5599 			ath12k_warn(ar->ab, "unable to create scan vdev %d\n", ret);
5600 			ath12k_mac_unassign_link_vif(arvif);
5601 			return ret;
5602 		}
5603 	}
5604 
5605 	spin_lock_bh(&ar->data_lock);
5606 	switch (ar->scan.state) {
5607 	case ATH12K_SCAN_IDLE:
5608 		reinit_completion(&ar->scan.started);
5609 		reinit_completion(&ar->scan.completed);
5610 		ar->scan.state = ATH12K_SCAN_STARTING;
5611 		ar->scan.is_roc = false;
5612 		ar->scan.arvif = arvif;
5613 		ret = 0;
5614 		break;
5615 	case ATH12K_SCAN_STARTING:
5616 	case ATH12K_SCAN_RUNNING:
5617 	case ATH12K_SCAN_ABORTING:
5618 		ret = -EBUSY;
5619 		break;
5620 	}
5621 	spin_unlock_bh(&ar->data_lock);
5622 
5623 	if (ret)
5624 		goto exit;
5625 
5626 	arg = kzalloc_flex(*arg, chan_list, n_channels);
5627 	if (!arg) {
5628 		ret = -ENOMEM;
5629 		goto exit;
5630 	}
5631 
5632 	arg->num_chan = n_channels;
5633 
5634 	ath12k_wmi_start_scan_init(ar, arg);
5635 	arg->vdev_id = arvif->vdev_id;
5636 	arg->scan_id = ATH12K_SCAN_ID;
5637 
5638 	if (req->ie_len) {
5639 		arg->extraie.ptr = kmemdup(req->ie, req->ie_len, GFP_KERNEL);
5640 		if (!arg->extraie.ptr) {
5641 			ret = -ENOMEM;
5642 			goto exit;
5643 		}
5644 		arg->extraie.len = req->ie_len;
5645 	}
5646 
5647 	if (req->n_ssids) {
5648 		arg->num_ssids = req->n_ssids;
5649 		for (i = 0; i < arg->num_ssids; i++)
5650 			arg->ssid[i] = req->ssids[i];
5651 	} else {
5652 		arg->scan_f_passive = 1;
5653 	}
5654 
5655 	for (i = 0; i < arg->num_chan; i++)
5656 		arg->chan_list[i] = chan_list[i]->center_freq;
5657 
5658 	ret = ath12k_start_scan(ar, arg);
5659 	if (ret) {
5660 		if (ret == -EBUSY)
5661 			ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
5662 				   "scan engine is busy 11d state %d\n", ar->state_11d);
5663 		else
5664 			ath12k_warn(ar->ab, "failed to start hw scan: %d\n", ret);
5665 
5666 		spin_lock_bh(&ar->data_lock);
5667 		ar->scan.state = ATH12K_SCAN_IDLE;
5668 		spin_unlock_bh(&ar->data_lock);
5669 		goto exit;
5670 	}
5671 
5672 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac scan started");
5673 
5674 	/* Add a margin to account for event/command processing */
5675 	ieee80211_queue_delayed_work(ath12k_ar_to_hw(ar), &ar->scan.timeout,
5676 				     msecs_to_jiffies(arg->max_scan_time +
5677 						      ATH12K_MAC_SCAN_TIMEOUT_MSECS));
5678 
5679 exit:
5680 	if (arg) {
5681 		kfree(arg->extraie.ptr);
5682 		kfree(arg);
5683 	}
5684 
5685 	if (ar->state_11d == ATH12K_11D_PREPARING &&
5686 	    ahvif->vdev_type == WMI_VDEV_TYPE_STA &&
5687 	    ahvif->vdev_subtype == WMI_VDEV_SUBTYPE_NONE)
5688 		ath12k_mac_11d_scan_start(ar, arvif->vdev_id);
5689 
5690 	return ret;
5691 }
5692 
5693 int ath12k_mac_op_hw_scan(struct ieee80211_hw *hw,
5694 			  struct ieee80211_vif *vif,
5695 			  struct ieee80211_scan_request *hw_req)
5696 {
5697 	struct ath12k_vif *ahvif = ath12k_vif_to_ahvif(vif);
5698 	struct ieee80211_channel **chan_list, *chan;
5699 	struct ath12k_hw *ah = ath12k_hw_to_ah(hw);
5700 	unsigned long links_map, link_id;
5701 	struct ath12k_link_vif *arvif;
5702 	struct ath12k *ar, *scan_ar;
5703 	int i, j, ret = 0;
5704 
5705 	lockdep_assert_wiphy(hw->wiphy);
5706 
5707 	chan_list = kzalloc_objs(*chan_list, hw_req->req.n_channels);
5708 	if (!chan_list)
5709 		return -ENOMEM;
5710 
5711 	/* There could be channels that belong to multiple underlying radio
5712 	 * in same scan request as mac80211 sees it as single band. In that
5713 	 * case split the hw_req based on frequency range and schedule scans to
5714 	 * corresponding radio.
5715 	 */
5716 	for_each_ar(ah, ar, i) {
5717 		int n_chans = 0;
5718 
5719 		for (j = 0; j < hw_req->req.n_channels; j++) {
5720 			chan = hw_req->req.channels[j];
5721 			scan_ar = ath12k_mac_select_scan_device(hw, vif,
5722 								chan->center_freq);
5723 			if (!scan_ar) {
5724 				ath12k_hw_warn(ah, "unable to select scan device for freq %d\n",
5725 					       chan->center_freq);
5726 				ret = -EINVAL;
5727 				goto abort;
5728 			}
5729 			if (ar != scan_ar)
5730 				continue;
5731 
5732 			chan_list[n_chans++] = chan;
5733 		}
5734 		if (n_chans) {
5735 			ret = ath12k_mac_initiate_hw_scan(hw, vif, hw_req, n_chans,
5736 							  chan_list, ar);
5737 			if (ret)
5738 				goto abort;
5739 		}
5740 	}
5741 abort:
5742 	/* If any of the parallel scans initiated fails, abort all and
5743 	 * remove the scan interfaces created. Return complete scan
5744 	 * failure as mac80211 assumes this as single scan request.
5745 	 */
5746 	if (ret) {
5747 		ath12k_hw_warn(ah, "Scan failed %d , cleanup all scan vdevs\n", ret);
5748 		links_map = ahvif->links_map;
5749 		for_each_set_bit(link_id, &links_map, ATH12K_NUM_MAX_LINKS) {
5750 			arvif = wiphy_dereference(hw->wiphy, ahvif->link[link_id]);
5751 			if (!arvif)
5752 				continue;
5753 
5754 			ar = arvif->ar;
5755 			if (ar->scan.arvif == arvif) {
5756 				wiphy_work_cancel(hw->wiphy, &ar->scan.vdev_clean_wk);
5757 				spin_lock_bh(&ar->data_lock);
5758 				ar->scan.arvif = NULL;
5759 				ar->scan.state = ATH12K_SCAN_IDLE;
5760 				ar->scan_channel = NULL;
5761 				ar->scan.roc_freq = 0;
5762 				spin_unlock_bh(&ar->data_lock);
5763 			}
5764 			if (link_id >= ATH12K_FIRST_SCAN_LINK) {
5765 				ath12k_mac_remove_link_interface(hw, arvif);
5766 				ath12k_mac_unassign_link_vif(arvif);
5767 			}
5768 		}
5769 	}
5770 	kfree(chan_list);
5771 	return ret;
5772 }
5773 EXPORT_SYMBOL(ath12k_mac_op_hw_scan);
5774 
5775 void ath12k_mac_op_cancel_hw_scan(struct ieee80211_hw *hw,
5776 				  struct ieee80211_vif *vif)
5777 {
5778 	struct ath12k_vif *ahvif = ath12k_vif_to_ahvif(vif);
5779 	unsigned long link_id, links_map = ahvif->links_map;
5780 	struct ath12k_link_vif *arvif;
5781 	struct ath12k *ar;
5782 
5783 	lockdep_assert_wiphy(hw->wiphy);
5784 
5785 	for_each_set_bit(link_id, &links_map, ATH12K_NUM_MAX_LINKS) {
5786 		arvif = wiphy_dereference(hw->wiphy, ahvif->link[link_id]);
5787 		if (!arvif || !arvif->is_created ||
5788 		    arvif->ar->scan.arvif != arvif)
5789 			continue;
5790 
5791 		ar = arvif->ar;
5792 
5793 		ath12k_scan_abort(ar);
5794 
5795 		cancel_delayed_work_sync(&ar->scan.timeout);
5796 	}
5797 }
5798 EXPORT_SYMBOL(ath12k_mac_op_cancel_hw_scan);
5799 
5800 static int ath12k_install_key(struct ath12k_link_vif *arvif,
5801 			      struct ieee80211_key_conf *key,
5802 			      enum set_key_cmd cmd,
5803 			      const u8 *macaddr, u32 flags)
5804 {
5805 	int ret;
5806 	struct ath12k *ar = arvif->ar;
5807 	struct wmi_vdev_install_key_arg arg = {
5808 		.vdev_id = arvif->vdev_id,
5809 		.key_idx = key->keyidx,
5810 		.key_len = key->keylen,
5811 		.key_data = key->key,
5812 		.key_flags = flags,
5813 		.ieee80211_key_cipher = key->cipher,
5814 		.macaddr = macaddr,
5815 	};
5816 	struct ath12k_vif *ahvif = arvif->ahvif;
5817 
5818 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
5819 
5820 	if (test_bit(ATH12K_FLAG_HW_CRYPTO_DISABLED, &ar->ab->dev_flags))
5821 		return 0;
5822 
5823 	if (cmd == DISABLE_KEY) {
5824 		/* TODO: Check if FW expects  value other than NONE for del */
5825 		/* arg.key_cipher = WMI_CIPHER_NONE; */
5826 		arg.key_len = 0;
5827 		arg.key_data = NULL;
5828 		goto check_order;
5829 	}
5830 
5831 	switch (key->cipher) {
5832 	case WLAN_CIPHER_SUITE_CCMP:
5833 	case WLAN_CIPHER_SUITE_CCMP_256:
5834 		arg.key_cipher = WMI_CIPHER_AES_CCM;
5835 		key->flags |= IEEE80211_KEY_FLAG_GENERATE_IV_MGMT;
5836 		break;
5837 	case WLAN_CIPHER_SUITE_TKIP:
5838 		arg.key_cipher = WMI_CIPHER_TKIP;
5839 		arg.key_txmic_len = 8;
5840 		arg.key_rxmic_len = 8;
5841 		break;
5842 	case WLAN_CIPHER_SUITE_GCMP:
5843 	case WLAN_CIPHER_SUITE_GCMP_256:
5844 		arg.key_cipher = WMI_CIPHER_AES_GCM;
5845 		key->flags |= IEEE80211_KEY_FLAG_GENERATE_IV_MGMT;
5846 		break;
5847 	case WLAN_CIPHER_SUITE_AES_CMAC:
5848 		arg.key_cipher = WMI_CIPHER_AES_CMAC;
5849 		break;
5850 	case WLAN_CIPHER_SUITE_BIP_GMAC_128:
5851 	case WLAN_CIPHER_SUITE_BIP_GMAC_256:
5852 		arg.key_cipher = WMI_CIPHER_AES_GMAC;
5853 		break;
5854 	case WLAN_CIPHER_SUITE_BIP_CMAC_256:
5855 		arg.key_cipher = WMI_CIPHER_AES_CMAC;
5856 		break;
5857 	default:
5858 		ath12k_warn(ar->ab, "cipher %d is not supported\n", key->cipher);
5859 		return -EOPNOTSUPP;
5860 	}
5861 
5862 	if (test_bit(ATH12K_FLAG_RAW_MODE, &ar->ab->dev_flags))
5863 		key->flags |= IEEE80211_KEY_FLAG_GENERATE_IV |
5864 			      IEEE80211_KEY_FLAG_RESERVE_TAILROOM;
5865 
5866 check_order:
5867 	if (ahvif->vdev_type == WMI_VDEV_TYPE_STA &&
5868 	    arg.key_flags == WMI_KEY_GROUP) {
5869 		if (cmd == SET_KEY) {
5870 			if (arvif->pairwise_key_done) {
5871 				ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
5872 					   "vdev %u pairwise key done, go install group key\n",
5873 					   arg.vdev_id);
5874 				goto install;
5875 			} else {
5876 				/* WCN7850 firmware requires pairwise key to be installed
5877 				 * before group key. In case group key comes first, cache
5878 				 * it and return. Will revisit it once pairwise key gets
5879 				 * installed.
5880 				 */
5881 				arvif->group_key = arg;
5882 				arvif->group_key_valid = true;
5883 				ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
5884 					   "vdev %u group key before pairwise key, cache and skip\n",
5885 					   arg.vdev_id);
5886 
5887 				ret = 0;
5888 				goto out;
5889 			}
5890 		} else {
5891 			arvif->group_key_valid = false;
5892 		}
5893 	}
5894 
5895 install:
5896 	reinit_completion(&ar->install_key_done);
5897 
5898 	ret = ath12k_wmi_vdev_install_key(arvif->ar, &arg);
5899 	if (ret)
5900 		return ret;
5901 
5902 	if (!wait_for_completion_timeout(&ar->install_key_done, 1 * HZ))
5903 		return -ETIMEDOUT;
5904 
5905 	if (ether_addr_equal(arg.macaddr, arvif->bssid))
5906 		ahvif->dp_vif.key_cipher = arg.ieee80211_key_cipher;
5907 
5908 	if (ar->install_key_status) {
5909 		ret = -EINVAL;
5910 		goto out;
5911 	}
5912 
5913 	if (ahvif->vdev_type == WMI_VDEV_TYPE_STA &&
5914 	    arg.key_flags == WMI_KEY_PAIRWISE) {
5915 		if (cmd == SET_KEY) {
5916 			arvif->pairwise_key_done = true;
5917 			if (arvif->group_key_valid) {
5918 				/* Install cached GTK */
5919 				arvif->group_key_valid = false;
5920 				arg = arvif->group_key;
5921 				ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
5922 					   "vdev %u pairwise key done, group key ready, go install\n",
5923 					   arg.vdev_id);
5924 				goto install;
5925 			}
5926 		} else {
5927 			arvif->pairwise_key_done = false;
5928 		}
5929 	}
5930 
5931 out:
5932 	if (ret) {
5933 		/* In case of failure userspace may not do DISABLE_KEY
5934 		 * but triggers re-connection directly, so manually reset
5935 		 * status here.
5936 		 */
5937 		arvif->group_key_valid = false;
5938 		arvif->pairwise_key_done = false;
5939 	}
5940 
5941 	return ret;
5942 }
5943 
5944 static int ath12k_clear_peer_keys(struct ath12k_link_vif *arvif,
5945 				  const u8 *addr)
5946 {
5947 	struct ath12k *ar = arvif->ar;
5948 	struct ath12k_base *ab = ar->ab;
5949 	struct ath12k_dp_link_peer *peer;
5950 	int first_errno = 0;
5951 	int ret;
5952 	int i, len;
5953 	u32 flags = 0;
5954 	struct ath12k_dp *dp = ath12k_ab_to_dp(ab);
5955 	struct ieee80211_key_conf *keys[WMI_MAX_KEY_INDEX + 1] = {};
5956 
5957 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
5958 
5959 	spin_lock_bh(&dp->dp_lock);
5960 	peer = ath12k_dp_link_peer_find_by_vdev_and_addr(dp, arvif->vdev_id, addr);
5961 	if (!peer || !peer->dp_peer) {
5962 		spin_unlock_bh(&dp->dp_lock);
5963 		return -ENOENT;
5964 	}
5965 
5966 	len = ARRAY_SIZE(peer->dp_peer->keys);
5967 	for (i = 0; i < len; i++) {
5968 		if (!peer->dp_peer->keys[i])
5969 			continue;
5970 
5971 		keys[i] = peer->dp_peer->keys[i];
5972 		peer->dp_peer->keys[i] = NULL;
5973 	}
5974 
5975 	spin_unlock_bh(&dp->dp_lock);
5976 
5977 	for (i = 0; i < len; i++) {
5978 		if (!keys[i])
5979 			continue;
5980 
5981 		/* key flags are not required to delete the key */
5982 		ret = ath12k_install_key(arvif, keys[i],
5983 					 DISABLE_KEY, addr, flags);
5984 		if (ret < 0 && first_errno == 0)
5985 			first_errno = ret;
5986 
5987 		if (ret < 0)
5988 			ath12k_warn(ab, "failed to remove peer key %d: %d\n",
5989 				    i, ret);
5990 	}
5991 
5992 	return first_errno;
5993 }
5994 
5995 static int ath12k_mac_set_key(struct ath12k *ar, enum set_key_cmd cmd,
5996 			      struct ath12k_link_vif *arvif,
5997 			      struct ath12k_link_sta *arsta,
5998 			      struct ieee80211_key_conf *key)
5999 {
6000 	struct ieee80211_sta *sta = NULL;
6001 	struct ath12k_base *ab = ar->ab;
6002 	struct ath12k_dp_link_peer *peer;
6003 	struct ath12k_sta *ahsta;
6004 	const u8 *peer_addr;
6005 	int ret;
6006 	u32 flags = 0;
6007 	struct ath12k_dp *dp = ath12k_ab_to_dp(ab);
6008 
6009 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
6010 
6011 	if (arsta)
6012 		sta = ath12k_ahsta_to_sta(arsta->ahsta);
6013 
6014 	if (test_bit(ATH12K_FLAG_HW_CRYPTO_DISABLED, &ab->dev_flags))
6015 		return 1;
6016 
6017 	if (sta)
6018 		peer_addr = arsta->addr;
6019 	else
6020 		peer_addr = arvif->bssid;
6021 
6022 	key->hw_key_idx = key->keyidx;
6023 
6024 	/* the peer should not disappear in mid-way (unless FW goes awry) since
6025 	 * we already hold wiphy lock. we just make sure its there now.
6026 	 */
6027 	spin_lock_bh(&dp->dp_lock);
6028 	peer = ath12k_dp_link_peer_find_by_vdev_and_addr(dp, arvif->vdev_id,
6029 							 peer_addr);
6030 	if (!peer || !peer->dp_peer) {
6031 		spin_unlock_bh(&dp->dp_lock);
6032 
6033 		if (cmd == SET_KEY) {
6034 			ath12k_warn(ab, "cannot install key for non-existent peer %pM\n",
6035 				    peer_addr);
6036 			return -EOPNOTSUPP;
6037 		}
6038 
6039 		/* if the peer doesn't exist there is no key to disable
6040 		 * anymore
6041 		 */
6042 		return 0;
6043 	}
6044 
6045 	spin_unlock_bh(&dp->dp_lock);
6046 
6047 	if (key->flags & IEEE80211_KEY_FLAG_PAIRWISE)
6048 		flags = WMI_KEY_PAIRWISE;
6049 	else
6050 		flags = WMI_KEY_GROUP;
6051 
6052 	ret = ath12k_install_key(arvif, key, cmd, peer_addr, flags);
6053 	if (ret) {
6054 		ath12k_warn(ab, "ath12k_install_key failed (%d)\n", ret);
6055 		return ret;
6056 	}
6057 
6058 	ret = ath12k_dp_rx_peer_pn_replay_config(arvif, peer_addr, cmd, key);
6059 	if (ret) {
6060 		ath12k_warn(ab, "failed to offload PN replay detection %d\n", ret);
6061 		return ret;
6062 	}
6063 
6064 	spin_lock_bh(&dp->dp_lock);
6065 	peer = ath12k_dp_link_peer_find_by_vdev_and_addr(dp, arvif->vdev_id,
6066 							 peer_addr);
6067 	if (peer && peer->dp_peer && cmd == SET_KEY) {
6068 		peer->dp_peer->keys[key->keyidx] = key;
6069 		if (key->flags & IEEE80211_KEY_FLAG_PAIRWISE) {
6070 			peer->dp_peer->ucast_keyidx = key->keyidx;
6071 			peer->dp_peer->sec_type =
6072 					ath12k_dp_tx_get_encrypt_type(key->cipher);
6073 		} else {
6074 			peer->dp_peer->mcast_keyidx = key->keyidx;
6075 			peer->dp_peer->sec_type_grp =
6076 					ath12k_dp_tx_get_encrypt_type(key->cipher);
6077 		}
6078 	} else if (peer && peer->dp_peer && cmd == DISABLE_KEY) {
6079 		peer->dp_peer->keys[key->keyidx] = NULL;
6080 		if (key->flags & IEEE80211_KEY_FLAG_PAIRWISE)
6081 			peer->dp_peer->ucast_keyidx = 0;
6082 		else
6083 			peer->dp_peer->mcast_keyidx = 0;
6084 	} else if (!peer)
6085 		/* impossible unless FW goes crazy */
6086 		ath12k_warn(ab, "peer %pM disappeared!\n", peer_addr);
6087 
6088 	if (sta) {
6089 		ahsta = ath12k_sta_to_ahsta(sta);
6090 
6091 		switch (key->cipher) {
6092 		case WLAN_CIPHER_SUITE_TKIP:
6093 		case WLAN_CIPHER_SUITE_CCMP:
6094 		case WLAN_CIPHER_SUITE_CCMP_256:
6095 		case WLAN_CIPHER_SUITE_GCMP:
6096 		case WLAN_CIPHER_SUITE_GCMP_256:
6097 			if (cmd == SET_KEY)
6098 				ahsta->pn_type = HAL_PN_TYPE_WPA;
6099 			else
6100 				ahsta->pn_type = HAL_PN_TYPE_NONE;
6101 			break;
6102 		default:
6103 			ahsta->pn_type = HAL_PN_TYPE_NONE;
6104 			break;
6105 		}
6106 	}
6107 
6108 	spin_unlock_bh(&dp->dp_lock);
6109 
6110 	return 0;
6111 }
6112 
6113 static int ath12k_mac_update_key_cache(struct ath12k_vif_cache *cache,
6114 				       enum set_key_cmd cmd,
6115 				       struct ieee80211_sta *sta,
6116 				       struct ieee80211_key_conf *key)
6117 {
6118 	struct ath12k_key_conf *key_conf, *tmp;
6119 
6120 	list_for_each_entry_safe(key_conf, tmp, &cache->key_conf.list, list) {
6121 		if (key_conf->key != key)
6122 			continue;
6123 
6124 		/* If SET key entry is already present in cache, nothing to do,
6125 		 * just return
6126 		 */
6127 		if (cmd == SET_KEY)
6128 			return 0;
6129 
6130 		/* DEL key for an old SET key which driver hasn't flushed yet.
6131 		 */
6132 		list_del(&key_conf->list);
6133 		kfree(key_conf);
6134 	}
6135 
6136 	if (cmd == SET_KEY) {
6137 		key_conf = kzalloc_obj(*key_conf);
6138 
6139 		if (!key_conf)
6140 			return -ENOMEM;
6141 
6142 		key_conf->cmd = cmd;
6143 		key_conf->sta = sta;
6144 		key_conf->key = key;
6145 		list_add_tail(&key_conf->list,
6146 			      &cache->key_conf.list);
6147 	}
6148 
6149 	return 0;
6150 }
6151 
6152 int ath12k_mac_op_set_key(struct ieee80211_hw *hw, enum set_key_cmd cmd,
6153 			  struct ieee80211_vif *vif, struct ieee80211_sta *sta,
6154 			  struct ieee80211_key_conf *key)
6155 {
6156 	struct ath12k_vif *ahvif = ath12k_vif_to_ahvif(vif);
6157 	struct ath12k_link_vif *arvif;
6158 	struct ath12k_link_sta *arsta = NULL;
6159 	struct ath12k_vif_cache *cache;
6160 	struct ath12k_sta *ahsta;
6161 	unsigned long links;
6162 	u8 link_id;
6163 	int ret;
6164 
6165 	lockdep_assert_wiphy(hw->wiphy);
6166 
6167 	/* IGTK needs to be done in host software */
6168 	if (key->keyidx == 4 || key->keyidx == 5)
6169 		return 1;
6170 
6171 	if (key->keyidx > WMI_MAX_KEY_INDEX)
6172 		return -ENOSPC;
6173 
6174 	if (sta) {
6175 		ahsta = ath12k_sta_to_ahsta(sta);
6176 
6177 		/* For an ML STA Pairwise key is same for all associated link Stations,
6178 		 * hence do set key for all link STAs which are active.
6179 		 */
6180 		if (sta->mlo) {
6181 			links = ahsta->links_map;
6182 			for_each_set_bit(link_id, &links, IEEE80211_MLD_MAX_NUM_LINKS) {
6183 				arvif = wiphy_dereference(hw->wiphy,
6184 							  ahvif->link[link_id]);
6185 				arsta = wiphy_dereference(hw->wiphy,
6186 							  ahsta->link[link_id]);
6187 
6188 				if (WARN_ON(!arvif || !arsta))
6189 					/* arvif and arsta are expected to be valid when
6190 					 * STA is present.
6191 					 */
6192 					continue;
6193 
6194 				ret = ath12k_mac_set_key(arvif->ar, cmd, arvif,
6195 							 arsta, key);
6196 				if (ret)
6197 					break;
6198 			}
6199 
6200 			return 0;
6201 		}
6202 
6203 		arsta = &ahsta->deflink;
6204 		arvif = arsta->arvif;
6205 		if (WARN_ON(!arvif))
6206 			return -EINVAL;
6207 
6208 		ret = ath12k_mac_set_key(arvif->ar, cmd, arvif, arsta, key);
6209 		if (ret)
6210 			return ret;
6211 
6212 		return 0;
6213 	}
6214 
6215 	if (key->link_id >= 0 && key->link_id < IEEE80211_MLD_MAX_NUM_LINKS) {
6216 		link_id = key->link_id;
6217 		arvif = wiphy_dereference(hw->wiphy, ahvif->link[link_id]);
6218 	} else {
6219 		link_id = 0;
6220 		arvif = &ahvif->deflink;
6221 	}
6222 
6223 	if (!arvif || !arvif->is_created) {
6224 		cache = ath12k_ahvif_get_link_cache(ahvif, link_id);
6225 		if (!cache)
6226 			return -ENOSPC;
6227 
6228 		ret = ath12k_mac_update_key_cache(cache, cmd, sta, key);
6229 		if (ret)
6230 			return ret;
6231 
6232 		return 0;
6233 	}
6234 
6235 	ret = ath12k_mac_set_key(arvif->ar, cmd, arvif, NULL, key);
6236 	if (ret)
6237 		return ret;
6238 
6239 	return 0;
6240 }
6241 EXPORT_SYMBOL(ath12k_mac_op_set_key);
6242 
6243 static int
6244 ath12k_mac_bitrate_mask_num_vht_rates(struct ath12k *ar,
6245 				      enum nl80211_band band,
6246 				      const struct cfg80211_bitrate_mask *mask)
6247 {
6248 	int num_rates = 0;
6249 	int i;
6250 
6251 	for (i = 0; i < ARRAY_SIZE(mask->control[band].vht_mcs); i++)
6252 		num_rates += hweight16(mask->control[band].vht_mcs[i]);
6253 
6254 	return num_rates;
6255 }
6256 
6257 static int
6258 ath12k_mac_bitrate_mask_num_he_rates(struct ath12k *ar,
6259 				     enum nl80211_band band,
6260 				     const struct cfg80211_bitrate_mask *mask)
6261 {
6262 	int num_rates = 0;
6263 	int i;
6264 
6265 	for (i = 0; i < ARRAY_SIZE(mask->control[band].he_mcs); i++)
6266 		num_rates += hweight16(mask->control[band].he_mcs[i]);
6267 
6268 	return num_rates;
6269 }
6270 
6271 static int
6272 ath12k_mac_bitrate_mask_num_eht_rates(struct ath12k *ar,
6273 				      enum nl80211_band band,
6274 				      const struct cfg80211_bitrate_mask *mask)
6275 {
6276 	int num_rates = 0;
6277 	int i;
6278 
6279 	for (i = 0; i < ARRAY_SIZE(mask->control[band].eht_mcs); i++)
6280 		num_rates += hweight16(mask->control[band].eht_mcs[i]);
6281 
6282 	return num_rates;
6283 }
6284 
6285 static int
6286 ath12k_mac_set_peer_vht_fixed_rate(struct ath12k_link_vif *arvif,
6287 				   struct ath12k_link_sta *arsta,
6288 				   const struct cfg80211_bitrate_mask *mask,
6289 				   enum nl80211_band band)
6290 {
6291 	struct ath12k *ar = arvif->ar;
6292 	u8 vht_rate, nss;
6293 	u32 rate_code;
6294 	int ret, i;
6295 
6296 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
6297 
6298 	nss = 0;
6299 
6300 	for (i = 0; i < ARRAY_SIZE(mask->control[band].vht_mcs); i++) {
6301 		if (hweight16(mask->control[band].vht_mcs[i]) == 1) {
6302 			nss = i + 1;
6303 			vht_rate = ffs(mask->control[band].vht_mcs[i]) - 1;
6304 		}
6305 	}
6306 
6307 	if (!nss) {
6308 		ath12k_warn(ar->ab, "No single VHT Fixed rate found to set for %pM",
6309 			    arsta->addr);
6310 		return -EINVAL;
6311 	}
6312 
6313 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
6314 		   "Setting Fixed VHT Rate for peer %pM. Device will not switch to any other selected rates",
6315 		   arsta->addr);
6316 
6317 	rate_code = ATH12K_HW_RATE_CODE(vht_rate, nss - 1,
6318 					WMI_RATE_PREAMBLE_VHT);
6319 	ret = ath12k_wmi_set_peer_param(ar, arsta->addr,
6320 					arvif->vdev_id,
6321 					WMI_PEER_PARAM_FIXED_RATE,
6322 					rate_code);
6323 	if (ret)
6324 		ath12k_warn(ar->ab,
6325 			    "failed to update STA %pM Fixed Rate %d: %d\n",
6326 			     arsta->addr, rate_code, ret);
6327 
6328 	return ret;
6329 }
6330 
6331 static int
6332 ath12k_mac_set_peer_he_fixed_rate(struct ath12k_link_vif *arvif,
6333 				  struct ath12k_link_sta *arsta,
6334 				  const struct cfg80211_bitrate_mask *mask,
6335 				  enum nl80211_band band)
6336 {
6337 	struct ath12k *ar = arvif->ar;
6338 	u8 he_rate, nss;
6339 	u32 rate_code;
6340 	int ret, i;
6341 	struct ath12k_sta *ahsta = arsta->ahsta;
6342 	struct ieee80211_sta *sta;
6343 
6344 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
6345 
6346 	sta = ath12k_ahsta_to_sta(ahsta);
6347 	nss = 0;
6348 
6349 	for (i = 0; i < ARRAY_SIZE(mask->control[band].he_mcs); i++) {
6350 		if (hweight16(mask->control[band].he_mcs[i]) == 1) {
6351 			nss = i + 1;
6352 			he_rate = ffs(mask->control[band].he_mcs[i]) - 1;
6353 		}
6354 	}
6355 
6356 	if (!nss) {
6357 		ath12k_warn(ar->ab, "No single HE Fixed rate found to set for %pM",
6358 			    arsta->addr);
6359 		return -EINVAL;
6360 	}
6361 
6362 	/* Avoid updating invalid nss as fixed rate*/
6363 	if (nss > sta->deflink.rx_nss)
6364 		return -EINVAL;
6365 
6366 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
6367 		   "Setting Fixed HE Rate for peer %pM. Device will not switch to any other selected rates",
6368 		   arsta->addr);
6369 
6370 	rate_code = ATH12K_HW_RATE_CODE(he_rate, nss - 1,
6371 					WMI_RATE_PREAMBLE_HE);
6372 
6373 	ret = ath12k_wmi_set_peer_param(ar, arsta->addr,
6374 					arvif->vdev_id,
6375 					WMI_PEER_PARAM_FIXED_RATE,
6376 					rate_code);
6377 	if (ret)
6378 		ath12k_warn(ar->ab,
6379 			    "failed to update STA %pM Fixed Rate %d: %d\n",
6380 			    arsta->addr, rate_code, ret);
6381 
6382 	return ret;
6383 }
6384 
6385 static int
6386 ath12k_mac_set_peer_eht_fixed_rate(struct ath12k_link_vif *arvif,
6387 				   struct ath12k_link_sta *arsta,
6388 				   const struct cfg80211_bitrate_mask *mask,
6389 				   enum nl80211_band band)
6390 {
6391 	struct ath12k_sta *ahsta = arsta->ahsta;
6392 	struct ath12k *ar = arvif->ar;
6393 	struct ieee80211_sta *sta;
6394 	struct ieee80211_link_sta *link_sta;
6395 	u8 eht_rate, nss = 0;
6396 	u32 rate_code;
6397 	int ret, i;
6398 
6399 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
6400 
6401 	sta = ath12k_ahsta_to_sta(ahsta);
6402 
6403 	for (i = 0; i < ARRAY_SIZE(mask->control[band].eht_mcs); i++) {
6404 		if (hweight16(mask->control[band].eht_mcs[i]) == 1) {
6405 			nss = i + 1;
6406 			eht_rate = ffs(mask->control[band].eht_mcs[i]) - 1;
6407 		}
6408 	}
6409 
6410 	if (!nss) {
6411 		ath12k_warn(ar->ab, "No single EHT Fixed rate found to set for %pM\n",
6412 			    arsta->addr);
6413 		return -EINVAL;
6414 	}
6415 
6416 	/* Avoid updating invalid nss as fixed rate*/
6417 	link_sta = ath12k_mac_get_link_sta(arsta);
6418 	if (!link_sta || nss > link_sta->rx_nss) {
6419 		ath12k_warn(ar->ab,
6420 			    "unable to access link sta for sta %pM link %u or fixed nss of %u is not supported by sta\n",
6421 			    sta->addr, arsta->link_id, nss);
6422 		return -EINVAL;
6423 	}
6424 
6425 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
6426 		   "Setting Fixed EHT Rate for peer %pM. Device will not switch to any other selected rates\n",
6427 		   arsta->addr);
6428 
6429 	rate_code = ATH12K_HW_RATE_CODE(eht_rate, nss - 1,
6430 					WMI_RATE_PREAMBLE_EHT);
6431 
6432 	ret = ath12k_wmi_set_peer_param(ar, arsta->addr,
6433 					arvif->vdev_id,
6434 					WMI_PEER_PARAM_FIXED_RATE,
6435 					rate_code);
6436 	if (ret)
6437 		ath12k_warn(ar->ab,
6438 			    "failed to update STA %pM Fixed Rate %d: %d\n",
6439 			    arsta->addr, rate_code, ret);
6440 
6441 	return ret;
6442 }
6443 
6444 static int ath12k_mac_station_assoc(struct ath12k *ar,
6445 				    struct ath12k_link_vif *arvif,
6446 				    struct ath12k_link_sta *arsta,
6447 				    bool reassoc)
6448 {
6449 	struct ieee80211_vif *vif = ath12k_ahvif_to_vif(arvif->ahvif);
6450 	struct ieee80211_sta *sta = ath12k_ahsta_to_sta(arsta->ahsta);
6451 	struct ieee80211_link_sta *link_sta;
6452 	int ret;
6453 	struct cfg80211_chan_def def;
6454 	enum nl80211_band band;
6455 	struct cfg80211_bitrate_mask *mask;
6456 	u8 num_vht_rates, num_he_rates, num_eht_rates;
6457 	u8 link_id = arvif->link_id;
6458 
6459 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
6460 
6461 	if (WARN_ON(ath12k_mac_vif_link_chan(vif, arvif->link_id, &def)))
6462 		return -EPERM;
6463 
6464 	if (WARN_ON(!rcu_access_pointer(sta->link[link_id])))
6465 		return -EINVAL;
6466 
6467 	band = def.chan->band;
6468 	mask = &arvif->bitrate_mask;
6469 
6470 	struct ath12k_wmi_peer_assoc_arg *peer_arg __free(kfree) =
6471 		kzalloc_obj(*peer_arg);
6472 	if (!peer_arg)
6473 		return -ENOMEM;
6474 
6475 	ath12k_peer_assoc_prepare(ar, arvif, arsta, peer_arg, reassoc);
6476 
6477 	if (peer_arg->peer_nss < 1) {
6478 		ath12k_warn(ar->ab,
6479 			    "invalid peer NSS %d\n", peer_arg->peer_nss);
6480 		return -EINVAL;
6481 	}
6482 
6483 	peer_arg->is_assoc = true;
6484 	ret = ath12k_wmi_send_peer_assoc_cmd(ar, peer_arg);
6485 	if (ret) {
6486 		ath12k_warn(ar->ab, "failed to run peer assoc for STA %pM vdev %i: %d\n",
6487 			    arsta->addr, arvif->vdev_id, ret);
6488 		return ret;
6489 	}
6490 
6491 	if (!wait_for_completion_timeout(&ar->peer_assoc_done, 1 * HZ)) {
6492 		ath12k_warn(ar->ab, "failed to get peer assoc conf event for %pM vdev %i\n",
6493 			    arsta->addr, arvif->vdev_id);
6494 		return -ETIMEDOUT;
6495 	}
6496 
6497 	num_vht_rates = ath12k_mac_bitrate_mask_num_vht_rates(ar, band, mask);
6498 	num_he_rates = ath12k_mac_bitrate_mask_num_he_rates(ar, band, mask);
6499 	num_eht_rates = ath12k_mac_bitrate_mask_num_eht_rates(ar, band, mask);
6500 
6501 	/* If single VHT/HE/EHT rate is configured (by set_bitrate_mask()),
6502 	 * peer_assoc will disable VHT/HE/EHT. This is now enabled by a peer
6503 	 * specific fixed param.
6504 	 * Note that all other rates and NSS will be disabled for this peer.
6505 	 */
6506 	link_sta = ath12k_mac_get_link_sta(arsta);
6507 	if (!link_sta) {
6508 		ath12k_warn(ar->ab, "unable to access link sta in station assoc\n");
6509 		return -EINVAL;
6510 	}
6511 
6512 	spin_lock_bh(&ar->data_lock);
6513 	arsta->bw = ath12k_mac_ieee80211_sta_bw_to_wmi(ar, link_sta);
6514 	arsta->bw_prev = link_sta->bandwidth;
6515 	spin_unlock_bh(&ar->data_lock);
6516 
6517 	if (link_sta->vht_cap.vht_supported && num_vht_rates == 1) {
6518 		ret = ath12k_mac_set_peer_vht_fixed_rate(arvif, arsta, mask, band);
6519 	} else if (link_sta->he_cap.has_he && num_he_rates == 1) {
6520 		ret = ath12k_mac_set_peer_he_fixed_rate(arvif, arsta, mask, band);
6521 		if (ret)
6522 			return ret;
6523 	} else if (link_sta->eht_cap.has_eht && num_eht_rates == 1) {
6524 		ret = ath12k_mac_set_peer_eht_fixed_rate(arvif, arsta, mask, band);
6525 		if (ret)
6526 			return ret;
6527 	}
6528 
6529 	/* Re-assoc is run only to update supported rates for given station. It
6530 	 * doesn't make much sense to reconfigure the peer completely.
6531 	 */
6532 	if (reassoc)
6533 		return 0;
6534 
6535 	ret = ath12k_setup_peer_smps(ar, arvif, arsta->addr,
6536 				     &link_sta->ht_cap, &link_sta->he_6ghz_capa);
6537 	if (ret) {
6538 		ath12k_warn(ar->ab, "failed to setup peer SMPS for vdev %d: %d\n",
6539 			    arvif->vdev_id, ret);
6540 		return ret;
6541 	}
6542 
6543 	if (!sta->wme) {
6544 		arvif->num_legacy_stations++;
6545 		ret = ath12k_recalc_rtscts_prot(arvif);
6546 		if (ret)
6547 			return ret;
6548 	}
6549 
6550 	if (sta->wme && sta->uapsd_queues) {
6551 		ret = ath12k_peer_assoc_qos_ap(ar, arvif, arsta);
6552 		if (ret) {
6553 			ath12k_warn(ar->ab, "failed to set qos params for STA %pM for vdev %i: %d\n",
6554 				    arsta->addr, arvif->vdev_id, ret);
6555 			return ret;
6556 		}
6557 	}
6558 
6559 	return 0;
6560 }
6561 
6562 static int ath12k_mac_station_disassoc(struct ath12k *ar,
6563 				       struct ath12k_link_vif *arvif,
6564 				       struct ath12k_link_sta *arsta)
6565 {
6566 	struct ieee80211_sta *sta = ath12k_ahsta_to_sta(arsta->ahsta);
6567 
6568 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
6569 
6570 	if (!sta->wme) {
6571 		arvif->num_legacy_stations--;
6572 		return ath12k_recalc_rtscts_prot(arvif);
6573 	}
6574 
6575 	return 0;
6576 }
6577 
6578 static int ath12k_mac_sta_set_4addr(struct wiphy *wiphy, struct ath12k_sta *ahsta)
6579 {
6580 	struct ath12k_dp_link_peer *peer;
6581 	struct ath12k_link_vif *arvif;
6582 	struct ath12k_link_sta *arsta;
6583 	struct ath12k_vif *ahvif;
6584 	struct ath12k_dp *dp;
6585 	unsigned long links;
6586 	struct ath12k *ar;
6587 	u8 link_id;
6588 	int ret;
6589 
6590 	links = ahsta->links_map;
6591 	for_each_set_bit(link_id, &links, IEEE80211_MLD_MAX_NUM_LINKS) {
6592 		arsta = wiphy_dereference(wiphy, ahsta->link[link_id]);
6593 		if (!arsta)
6594 			continue;
6595 
6596 		arvif = arsta->arvif;
6597 		ahvif = arvif->ahvif;
6598 		ar = arvif->ar;
6599 
6600 		if (arvif->set_wds_vdev_param)
6601 			goto skip_nawds;
6602 
6603 		ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
6604 			   "setting USE_4ADDR for peer %pM\n", arsta->addr);
6605 
6606 		ret = ath12k_wmi_set_peer_param(ar, arsta->addr,
6607 						arvif->vdev_id,
6608 						WMI_PEER_USE_4ADDR,
6609 						WMI_PEER_4ADDR_ALLOW_EAPOL_DATA_FRAME);
6610 		if (ret) {
6611 			ath12k_warn(ar->ab, "failed to set peer %pM 4addr capability: %d\n",
6612 				    arsta->addr, ret);
6613 			return ret;
6614 		}
6615 
6616 		if (ahvif->dp_vif.tx_encap_type != ATH12K_HW_TXRX_ETHERNET)
6617 			goto skip_nawds;
6618 
6619 		ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
6620 						    WMI_VDEV_PARAM_AP_ENABLE_NAWDS,
6621 						    WDS_EXT_ENABLE);
6622 		if (ret) {
6623 			ath12k_warn(ar->ab, "failed to set vdev %d nawds parameter: %d\n",
6624 				    arvif->vdev_id, ret);
6625 			return ret;
6626 		}
6627 
6628 		arvif->nawds_enabled = true;
6629 
6630 skip_nawds:
6631 		dp = ath12k_ab_to_dp(ar->ab);
6632 		spin_lock_bh(&dp->dp_lock);
6633 		peer = ath12k_dp_link_peer_find_by_vdev_and_addr(dp, arvif->vdev_id,
6634 								 arsta->addr);
6635 		if (peer && peer->dp_peer) {
6636 			peer->dp_peer->ucast_ra_only = true;
6637 			peer->dp_peer->use_4addr = true;
6638 		} else {
6639 			spin_unlock_bh(&dp->dp_lock);
6640 			ath12k_warn(ar->ab, "failed to find DP peer for %pM\n",
6641 				    arsta->addr);
6642 			return -ENOENT;
6643 		}
6644 
6645 		spin_unlock_bh(&dp->dp_lock);
6646 	}
6647 
6648 	return 0;
6649 }
6650 
6651 static void ath12k_sta_rc_update_wk(struct wiphy *wiphy, struct wiphy_work *wk)
6652 {
6653 	struct ieee80211_link_sta *link_sta;
6654 	struct ath12k *ar;
6655 	struct ath12k_link_vif *arvif;
6656 	struct ieee80211_sta *sta;
6657 	struct cfg80211_chan_def def;
6658 	enum nl80211_band band;
6659 	const u8 *ht_mcs_mask;
6660 	const u16 *vht_mcs_mask;
6661 	const u16 *he_mcs_mask;
6662 	const u16 *eht_mcs_mask;
6663 	u32 changed, bw, nss, mac_nss, smps, bw_prev;
6664 	int err, num_vht_rates, num_he_rates, num_eht_rates;
6665 	const struct cfg80211_bitrate_mask *mask;
6666 	enum wmi_phy_mode peer_phymode;
6667 	struct ath12k_link_sta *arsta;
6668 	struct ieee80211_vif *vif;
6669 
6670 	lockdep_assert_wiphy(wiphy);
6671 
6672 	arsta = container_of(wk, struct ath12k_link_sta, update_wk);
6673 	sta = ath12k_ahsta_to_sta(arsta->ahsta);
6674 	arvif = arsta->arvif;
6675 	vif = ath12k_ahvif_to_vif(arvif->ahvif);
6676 	ar = arvif->ar;
6677 
6678 	if (WARN_ON(ath12k_mac_vif_link_chan(vif, arvif->link_id, &def)))
6679 		return;
6680 
6681 	band = def.chan->band;
6682 	ht_mcs_mask = arvif->bitrate_mask.control[band].ht_mcs;
6683 	vht_mcs_mask = arvif->bitrate_mask.control[band].vht_mcs;
6684 	he_mcs_mask = arvif->bitrate_mask.control[band].he_mcs;
6685 	eht_mcs_mask = arvif->bitrate_mask.control[band].eht_mcs;
6686 
6687 	spin_lock_bh(&ar->data_lock);
6688 
6689 	changed = arsta->changed;
6690 	arsta->changed = 0;
6691 
6692 	bw = arsta->bw;
6693 	bw_prev = arsta->bw_prev;
6694 	nss = arsta->nss;
6695 	smps = arsta->smps;
6696 
6697 	spin_unlock_bh(&ar->data_lock);
6698 
6699 	nss = max_t(u32, 1, nss);
6700 	mac_nss = max3(ath12k_mac_max_ht_nss(ht_mcs_mask),
6701 		       ath12k_mac_max_vht_nss(vht_mcs_mask),
6702 		       ath12k_mac_max_he_nss(he_mcs_mask));
6703 	mac_nss = max(mac_nss, ath12k_mac_max_eht_nss(eht_mcs_mask));
6704 	nss = min(nss, mac_nss);
6705 
6706 	struct ath12k_wmi_peer_assoc_arg *peer_arg __free(kfree) =
6707 					kzalloc_obj(*peer_arg);
6708 	if (!peer_arg)
6709 		return;
6710 
6711 	if (changed & IEEE80211_RC_BW_CHANGED) {
6712 		ath12k_peer_assoc_h_phymode(ar, arvif, arsta, peer_arg);
6713 		peer_phymode = peer_arg->peer_phymode;
6714 
6715 		if (bw > bw_prev) {
6716 			/* Phymode shows maximum supported channel width, if we
6717 			 * upgrade bandwidth then due to sanity check of firmware,
6718 			 * we have to send WMI_PEER_PHYMODE followed by
6719 			 * WMI_PEER_CHWIDTH
6720 			 */
6721 			ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac bandwidth upgrade for sta %pM new %d old %d\n",
6722 				   arsta->addr, bw, bw_prev);
6723 			err = ath12k_wmi_set_peer_param(ar, arsta->addr,
6724 							arvif->vdev_id, WMI_PEER_PHYMODE,
6725 							peer_phymode);
6726 			if (err) {
6727 				ath12k_warn(ar->ab, "failed to update STA %pM to peer phymode %d: %d\n",
6728 					    arsta->addr, peer_phymode, err);
6729 				return;
6730 			}
6731 			err = ath12k_wmi_set_peer_param(ar, arsta->addr,
6732 							arvif->vdev_id, WMI_PEER_CHWIDTH,
6733 							bw);
6734 			if (err)
6735 				ath12k_warn(ar->ab, "failed to update STA %pM to peer bandwidth %d: %d\n",
6736 					    arsta->addr, bw, err);
6737 		} else {
6738 			/* When we downgrade bandwidth this will conflict with phymode
6739 			 * and cause to trigger firmware crash. In this case we send
6740 			 * WMI_PEER_CHWIDTH followed by WMI_PEER_PHYMODE
6741 			 */
6742 			ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac bandwidth downgrade for sta %pM new %d old %d\n",
6743 				   arsta->addr, bw, bw_prev);
6744 			err = ath12k_wmi_set_peer_param(ar, arsta->addr,
6745 							arvif->vdev_id, WMI_PEER_CHWIDTH,
6746 							bw);
6747 			if (err) {
6748 				ath12k_warn(ar->ab, "failed to update STA %pM peer to bandwidth %d: %d\n",
6749 					    arsta->addr, bw, err);
6750 				return;
6751 			}
6752 			err = ath12k_wmi_set_peer_param(ar, arsta->addr,
6753 							arvif->vdev_id, WMI_PEER_PHYMODE,
6754 							peer_phymode);
6755 			if (err)
6756 				ath12k_warn(ar->ab, "failed to update STA %pM to peer phymode %d: %d\n",
6757 					    arsta->addr, peer_phymode, err);
6758 		}
6759 	}
6760 
6761 	if (changed & IEEE80211_RC_NSS_CHANGED) {
6762 		ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac update sta %pM nss %d\n",
6763 			   arsta->addr, nss);
6764 
6765 		err = ath12k_wmi_set_peer_param(ar, arsta->addr, arvif->vdev_id,
6766 						WMI_PEER_NSS, nss);
6767 		if (err)
6768 			ath12k_warn(ar->ab, "failed to update STA %pM nss %d: %d\n",
6769 				    arsta->addr, nss, err);
6770 	}
6771 
6772 	if (changed & IEEE80211_RC_SMPS_CHANGED) {
6773 		ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac update sta %pM smps %d\n",
6774 			   arsta->addr, smps);
6775 
6776 		err = ath12k_wmi_set_peer_param(ar, arsta->addr, arvif->vdev_id,
6777 						WMI_PEER_MIMO_PS_STATE, smps);
6778 		if (err)
6779 			ath12k_warn(ar->ab, "failed to update STA %pM smps %d: %d\n",
6780 				    arsta->addr, smps, err);
6781 	}
6782 
6783 	if (changed & IEEE80211_RC_SUPP_RATES_CHANGED) {
6784 		mask = &arvif->bitrate_mask;
6785 		num_vht_rates = ath12k_mac_bitrate_mask_num_vht_rates(ar, band,
6786 								      mask);
6787 		num_he_rates = ath12k_mac_bitrate_mask_num_he_rates(ar, band,
6788 								    mask);
6789 		num_eht_rates = ath12k_mac_bitrate_mask_num_eht_rates(ar, band,
6790 								      mask);
6791 
6792 		/* Peer_assoc_prepare will reject vht rates in
6793 		 * bitrate_mask if its not available in range format and
6794 		 * sets vht tx_rateset as unsupported. So multiple VHT MCS
6795 		 * setting(eg. MCS 4,5,6) per peer is not supported here.
6796 		 * But, Single rate in VHT mask can be set as per-peer
6797 		 * fixed rate. But even if any HT rates are configured in
6798 		 * the bitrate mask, device will not switch to those rates
6799 		 * when per-peer Fixed rate is set.
6800 		 * TODO: Check RATEMASK_CMDID to support auto rates selection
6801 		 * across HT/VHT and for multiple VHT MCS support.
6802 		 */
6803 		link_sta = ath12k_mac_get_link_sta(arsta);
6804 		if (!link_sta) {
6805 			ath12k_warn(ar->ab, "unable to access link sta in peer assoc he for sta %pM link %u\n",
6806 				    sta->addr, arsta->link_id);
6807 			return;
6808 		}
6809 
6810 		if (link_sta->vht_cap.vht_supported && num_vht_rates == 1) {
6811 			ath12k_mac_set_peer_vht_fixed_rate(arvif, arsta, mask,
6812 							   band);
6813 		} else if (link_sta->he_cap.has_he && num_he_rates == 1) {
6814 			ath12k_mac_set_peer_he_fixed_rate(arvif, arsta, mask, band);
6815 		} else if (link_sta->eht_cap.has_eht && num_eht_rates == 1) {
6816 			err = ath12k_mac_set_peer_eht_fixed_rate(arvif, arsta,
6817 								 mask, band);
6818 			if (err) {
6819 				ath12k_warn(ar->ab,
6820 					    "failed to set peer EHT fixed rate for STA %pM ret %d\n",
6821 					    arsta->addr, err);
6822 				return;
6823 			}
6824 		} else {
6825 			/* If the peer is non-VHT/HE/EHT or no fixed VHT/HE/EHT
6826 			 * rate is provided in the new bitrate mask we set the
6827 			 * other rates using peer_assoc command. Also clear
6828 			 * the peer fixed rate settings as it has higher proprity
6829 			 * than peer assoc
6830 			 */
6831 			err = ath12k_wmi_set_peer_param(ar, arsta->addr,
6832 							arvif->vdev_id,
6833 							WMI_PEER_PARAM_FIXED_RATE,
6834 							WMI_FIXED_RATE_NONE);
6835 			if (err)
6836 				ath12k_warn(ar->ab,
6837 					    "failed to disable peer fixed rate for STA %pM ret %d\n",
6838 					    arsta->addr, err);
6839 
6840 			ath12k_peer_assoc_prepare(ar, arvif, arsta,
6841 						  peer_arg, true);
6842 
6843 			peer_arg->is_assoc = false;
6844 			err = ath12k_wmi_send_peer_assoc_cmd(ar, peer_arg);
6845 			if (err)
6846 				ath12k_warn(ar->ab, "failed to run peer assoc for STA %pM vdev %i: %d\n",
6847 					    arsta->addr, arvif->vdev_id, err);
6848 
6849 			if (!wait_for_completion_timeout(&ar->peer_assoc_done, 1 * HZ))
6850 				ath12k_warn(ar->ab, "failed to get peer assoc conf event for %pM vdev %i\n",
6851 					    arsta->addr, arvif->vdev_id);
6852 		}
6853 	}
6854 }
6855 
6856 static void ath12k_mac_free_unassign_link_sta(struct ath12k_hw *ah,
6857 					      struct ath12k_sta *ahsta,
6858 					      u8 link_id)
6859 {
6860 	struct ath12k_link_sta *arsta;
6861 
6862 	lockdep_assert_wiphy(ah->hw->wiphy);
6863 
6864 	if (WARN_ON(link_id >= IEEE80211_MLD_MAX_NUM_LINKS))
6865 		return;
6866 
6867 	arsta = wiphy_dereference(ah->hw->wiphy, ahsta->link[link_id]);
6868 	if (WARN_ON(!arsta))
6869 		return;
6870 
6871 	ahsta->links_map &= ~BIT(link_id);
6872 	ahsta->free_logical_link_idx_map |= BIT(arsta->link_idx);
6873 
6874 	rcu_assign_pointer(ahsta->link[link_id], NULL);
6875 	synchronize_rcu();
6876 
6877 	if (arsta == &ahsta->deflink) {
6878 		arsta->link_id = ATH12K_INVALID_LINK_ID;
6879 		arsta->ahsta = NULL;
6880 		arsta->arvif = NULL;
6881 		return;
6882 	}
6883 
6884 	kfree(arsta);
6885 }
6886 
6887 static int ath12k_mac_inc_num_stations(struct ath12k_link_vif *arvif,
6888 				       struct ath12k_link_sta *arsta)
6889 {
6890 	struct ieee80211_sta *sta = ath12k_ahsta_to_sta(arsta->ahsta);
6891 	struct ath12k *ar = arvif->ar;
6892 
6893 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
6894 
6895 	if (arvif->ahvif->vdev_type == WMI_VDEV_TYPE_STA && !sta->tdls)
6896 		return 0;
6897 
6898 	if (ar->num_stations >= ar->max_num_stations)
6899 		return -ENOBUFS;
6900 
6901 	ar->num_stations++;
6902 	arvif->num_stations++;
6903 
6904 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
6905 		   "mac station %pM connected to vdev %u num_stations %u\n",
6906 		   arsta->addr, arvif->vdev_id, arvif->num_stations);
6907 
6908 	return 0;
6909 }
6910 
6911 static void ath12k_mac_dec_num_stations(struct ath12k_link_vif *arvif,
6912 					struct ath12k_link_sta *arsta)
6913 {
6914 	struct ieee80211_sta *sta = ath12k_ahsta_to_sta(arsta->ahsta);
6915 	struct ath12k *ar = arvif->ar;
6916 
6917 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
6918 
6919 	if (arvif->ahvif->vdev_type == WMI_VDEV_TYPE_STA && !sta->tdls)
6920 		return;
6921 
6922 	ar->num_stations--;
6923 
6924 	if (arvif->num_stations) {
6925 		arvif->num_stations--;
6926 		ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
6927 			   "mac station %pM disconnected from vdev %u num_stations %u\n",
6928 			   arsta->addr, arvif->vdev_id, arvif->num_stations);
6929 	} else {
6930 		ath12k_warn(ar->ab,
6931 			    "mac station %pM disconnect for vdev %u without any connected station\n",
6932 			    arsta->addr, arvif->vdev_id);
6933 	}
6934 }
6935 
6936 static void ath12k_mac_station_post_remove(struct ath12k *ar,
6937 					   struct ath12k_link_vif *arvif,
6938 					   struct ath12k_link_sta *arsta)
6939 {
6940 	struct ieee80211_vif *vif = ath12k_ahvif_to_vif(arvif->ahvif);
6941 	struct ieee80211_sta *sta = ath12k_ahsta_to_sta(arsta->ahsta);
6942 	struct ath12k_dp_link_peer *peer;
6943 	struct ath12k_dp *dp = ath12k_ab_to_dp(ar->ab);
6944 
6945 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
6946 
6947 	ath12k_mac_dec_num_stations(arvif, arsta);
6948 
6949 	spin_lock_bh(&dp->dp_lock);
6950 
6951 	peer = ath12k_dp_link_peer_find_by_vdev_and_addr(dp, arvif->vdev_id,
6952 							 arsta->addr);
6953 	if (peer && peer->sta == sta) {
6954 		ath12k_warn(ar->ab, "Found peer entry %pM n vdev %i after it was supposedly removed\n",
6955 			    vif->addr, arvif->vdev_id);
6956 		peer->sta = NULL;
6957 
6958 		ath12k_dp_link_peer_free(peer);
6959 		ar->num_peers--;
6960 	}
6961 
6962 	spin_unlock_bh(&dp->dp_lock);
6963 }
6964 
6965 static int ath12k_mac_station_unauthorize(struct ath12k *ar,
6966 					  struct ath12k_link_vif *arvif,
6967 					  struct ath12k_link_sta *arsta)
6968 {
6969 	struct ath12k_dp_link_peer *peer;
6970 	int ret;
6971 	struct ath12k_dp *dp = ath12k_ab_to_dp(ar->ab);
6972 
6973 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
6974 
6975 	spin_lock_bh(&dp->dp_lock);
6976 
6977 	peer = ath12k_dp_link_peer_find_by_vdev_and_addr(dp, arvif->vdev_id,
6978 							 arsta->addr);
6979 	if (peer)
6980 		peer->is_authorized = false;
6981 
6982 	spin_unlock_bh(&dp->dp_lock);
6983 
6984 	/* Driver must clear the keys during the state change from
6985 	 * IEEE80211_STA_AUTHORIZED to IEEE80211_STA_ASSOC, since after
6986 	 * returning from here, mac80211 is going to delete the keys
6987 	 * in __sta_info_destroy_part2(). This will ensure that the driver does
6988 	 * not retain stale key references after mac80211 deletes the keys.
6989 	 */
6990 	ret = ath12k_clear_peer_keys(arvif, arsta->addr);
6991 	if (ret) {
6992 		ath12k_warn(ar->ab, "failed to clear all peer keys for vdev %i: %d\n",
6993 			    arvif->vdev_id, ret);
6994 		return ret;
6995 	}
6996 
6997 	return 0;
6998 }
6999 
7000 static int ath12k_mac_station_authorize(struct ath12k *ar,
7001 					struct ath12k_link_vif *arvif,
7002 					struct ath12k_link_sta *arsta)
7003 {
7004 	struct ath12k_dp_link_peer *peer;
7005 	struct ieee80211_vif *vif = ath12k_ahvif_to_vif(arvif->ahvif);
7006 	int ret;
7007 	struct ath12k_dp *dp = ath12k_ab_to_dp(ar->ab);
7008 
7009 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
7010 
7011 	spin_lock_bh(&dp->dp_lock);
7012 
7013 	peer = ath12k_dp_link_peer_find_by_vdev_and_addr(dp, arvif->vdev_id,
7014 							 arsta->addr);
7015 	if (peer)
7016 		peer->is_authorized = true;
7017 
7018 	spin_unlock_bh(&dp->dp_lock);
7019 
7020 	if (vif->type == NL80211_IFTYPE_STATION && arvif->is_up) {
7021 		ret = ath12k_wmi_set_peer_param(ar, arsta->addr,
7022 						arvif->vdev_id,
7023 						WMI_PEER_AUTHORIZE,
7024 						1);
7025 		if (ret) {
7026 			ath12k_warn(ar->ab, "Unable to authorize peer %pM vdev %d: %d\n",
7027 				    arsta->addr, arvif->vdev_id, ret);
7028 			return ret;
7029 		}
7030 	}
7031 
7032 	return 0;
7033 }
7034 
7035 static int ath12k_mac_station_remove(struct ath12k *ar,
7036 				     struct ath12k_link_vif *arvif,
7037 				     struct ath12k_link_sta *arsta)
7038 {
7039 	struct ieee80211_sta *sta = ath12k_ahsta_to_sta(arsta->ahsta);
7040 	struct ath12k_vif *ahvif = arvif->ahvif;
7041 	int ret = 0;
7042 	struct ath12k_link_sta *temp_arsta;
7043 
7044 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
7045 
7046 	wiphy_work_cancel(ar->ah->hw->wiphy, &arsta->update_wk);
7047 
7048 	if (ahvif->vdev_type == WMI_VDEV_TYPE_STA) {
7049 		ath12k_bss_disassoc(ar, arvif);
7050 		ret = ath12k_mac_vdev_stop(arvif);
7051 		if (ret)
7052 			ath12k_warn(ar->ab, "failed to stop vdev %i: %d\n",
7053 				    arvif->vdev_id, ret);
7054 	}
7055 
7056 	if (sta->mlo)
7057 		return ret;
7058 
7059 	ath12k_dp_peer_cleanup(ar, arvif->vdev_id, arsta->addr);
7060 
7061 	ret = ath12k_peer_delete(ar, arvif->vdev_id, arsta->addr);
7062 	if (ret)
7063 		ath12k_warn(ar->ab, "Failed to delete peer: %pM for VDEV: %d\n",
7064 			    arsta->addr, arvif->vdev_id);
7065 	else
7066 		ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "Removed peer: %pM for VDEV: %d\n",
7067 			   arsta->addr, arvif->vdev_id);
7068 
7069 	ath12k_mac_station_post_remove(ar, arvif, arsta);
7070 
7071 	spin_lock_bh(&ar->ab->base_lock);
7072 
7073 	/* To handle roaming and split phy scenario */
7074 	temp_arsta = ath12k_link_sta_find_by_addr(ar->ab, arsta->addr);
7075 	if (temp_arsta && temp_arsta->arvif->ar == ar)
7076 		ath12k_link_sta_rhash_delete(ar->ab, arsta);
7077 
7078 	spin_unlock_bh(&ar->ab->base_lock);
7079 
7080 	if (sta->valid_links)
7081 		ath12k_mac_free_unassign_link_sta(ahvif->ah,
7082 						  arsta->ahsta, arsta->link_id);
7083 
7084 	return ret;
7085 }
7086 
7087 static int ath12k_mac_station_add(struct ath12k *ar,
7088 				  struct ath12k_link_vif *arvif,
7089 				  struct ath12k_link_sta *arsta)
7090 {
7091 	struct ath12k_base *ab = ar->ab;
7092 	struct ieee80211_vif *vif = ath12k_ahvif_to_vif(arvif->ahvif);
7093 	struct ieee80211_sta *sta = ath12k_ahsta_to_sta(arsta->ahsta);
7094 	struct ath12k_wmi_peer_create_arg peer_param = {};
7095 	int ret;
7096 	struct ath12k_link_sta *temp_arsta;
7097 
7098 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
7099 
7100 	ret = ath12k_mac_inc_num_stations(arvif, arsta);
7101 	if (ret) {
7102 		ath12k_warn(ab, "refusing to associate station: too many connected already (%d)\n",
7103 			    ar->max_num_stations);
7104 		goto exit;
7105 	}
7106 
7107 	spin_lock_bh(&ab->base_lock);
7108 
7109 	/*
7110 	 * In case of Split PHY and roaming scenario, pdev idx
7111 	 * might differ but both the pdev will share same rhash
7112 	 * table. In that case update the rhash table if link_sta is
7113 	 * already present
7114 	 */
7115 	temp_arsta = ath12k_link_sta_find_by_addr(ab, arsta->addr);
7116 	if (temp_arsta && temp_arsta->arvif->ar != ar)
7117 		ath12k_link_sta_rhash_delete(ab, temp_arsta);
7118 
7119 	ret = ath12k_link_sta_rhash_add(ab, arsta);
7120 	spin_unlock_bh(&ab->base_lock);
7121 	if (ret) {
7122 		ath12k_warn(ab, "Failed to add arsta: %pM to hash table, ret: %d",
7123 			    arsta->addr, ret);
7124 		goto dec_num_station;
7125 	}
7126 
7127 	peer_param.vdev_id = arvif->vdev_id;
7128 	peer_param.peer_addr = arsta->addr;
7129 	peer_param.peer_type = WMI_PEER_TYPE_DEFAULT;
7130 	peer_param.ml_enabled = sta->mlo;
7131 
7132 	ret = ath12k_peer_create(ar, arvif, sta, &peer_param);
7133 	if (ret) {
7134 		ath12k_warn(ab, "Failed to add peer: %pM for VDEV: %d\n",
7135 			    arsta->addr, arvif->vdev_id);
7136 		goto free_peer;
7137 	}
7138 
7139 	ath12k_dbg(ab, ATH12K_DBG_MAC, "Added peer: %pM for VDEV: %d\n",
7140 		   arsta->addr, arvif->vdev_id);
7141 
7142 	if (ieee80211_vif_is_mesh(vif)) {
7143 		ret = ath12k_wmi_set_peer_param(ar, arsta->addr,
7144 						arvif->vdev_id,
7145 						WMI_PEER_USE_4ADDR, 1);
7146 		if (ret) {
7147 			ath12k_warn(ab, "failed to STA %pM 4addr capability: %d\n",
7148 				    arsta->addr, ret);
7149 			goto free_peer;
7150 		}
7151 	}
7152 
7153 	ret = ath12k_dp_peer_setup(ar, arvif->vdev_id, arsta->addr);
7154 	if (ret) {
7155 		ath12k_warn(ab, "failed to setup dp for peer %pM on vdev %i (%d)\n",
7156 			    arsta->addr, arvif->vdev_id, ret);
7157 		goto free_peer;
7158 	}
7159 
7160 	if (ab->hw_params->vdev_start_delay &&
7161 	    !arvif->is_started &&
7162 	    arvif->ahvif->vdev_type != WMI_VDEV_TYPE_AP) {
7163 		ret = ath12k_start_vdev_delay(ar, arvif);
7164 		if (ret) {
7165 			ath12k_warn(ab, "failed to delay vdev start: %d\n", ret);
7166 			goto free_peer;
7167 		}
7168 	}
7169 
7170 	return 0;
7171 
7172 free_peer:
7173 	ath12k_peer_delete(ar, arvif->vdev_id, arsta->addr);
7174 	spin_lock_bh(&ab->base_lock);
7175 	ath12k_link_sta_rhash_delete(ab, arsta);
7176 	spin_unlock_bh(&ab->base_lock);
7177 dec_num_station:
7178 	ath12k_mac_dec_num_stations(arvif, arsta);
7179 exit:
7180 	return ret;
7181 }
7182 
7183 static int ath12k_mac_assign_link_sta(struct ath12k_hw *ah,
7184 				      struct ath12k_sta *ahsta,
7185 				      struct ath12k_link_sta *arsta,
7186 				      struct ath12k_vif *ahvif,
7187 				      u8 link_id)
7188 {
7189 	struct ieee80211_sta *sta = ath12k_ahsta_to_sta(ahsta);
7190 	struct ieee80211_link_sta *link_sta;
7191 	struct ath12k_link_vif *arvif;
7192 	int link_idx;
7193 
7194 	lockdep_assert_wiphy(ah->hw->wiphy);
7195 
7196 	if (!arsta || link_id >= IEEE80211_MLD_MAX_NUM_LINKS)
7197 		return -EINVAL;
7198 
7199 	arvif = wiphy_dereference(ah->hw->wiphy, ahvif->link[link_id]);
7200 	if (!arvif)
7201 		return -EINVAL;
7202 
7203 	memset(arsta, 0, sizeof(*arsta));
7204 
7205 	link_sta = wiphy_dereference(ah->hw->wiphy, sta->link[link_id]);
7206 	if (!link_sta)
7207 		return -EINVAL;
7208 
7209 	ether_addr_copy(arsta->addr, link_sta->addr);
7210 
7211 	if (!ahsta->free_logical_link_idx_map)
7212 		return -ENOSPC;
7213 
7214 	/*
7215 	 * Allocate a logical link index by selecting the first available bit
7216 	 * from the free logical index map
7217 	 */
7218 	link_idx = __ffs(ahsta->free_logical_link_idx_map);
7219 	ahsta->free_logical_link_idx_map &= ~BIT(link_idx);
7220 	arsta->link_idx = link_idx;
7221 
7222 	arsta->link_id = link_id;
7223 	ahsta->links_map |= BIT(arsta->link_id);
7224 	arsta->arvif = arvif;
7225 	arsta->ahsta = ahsta;
7226 	ahsta->ahvif = ahvif;
7227 
7228 	wiphy_work_init(&arsta->update_wk, ath12k_sta_rc_update_wk);
7229 
7230 	rcu_assign_pointer(ahsta->link[link_id], arsta);
7231 
7232 	return 0;
7233 }
7234 
7235 static void ath12k_mac_ml_station_remove(struct ath12k_vif *ahvif,
7236 					 struct ath12k_sta *ahsta)
7237 {
7238 	struct ieee80211_sta *sta = ath12k_ahsta_to_sta(ahsta);
7239 	struct ath12k_hw *ah = ahvif->ah;
7240 	struct ath12k_link_vif *arvif;
7241 	struct ath12k_link_sta *arsta;
7242 	unsigned long links;
7243 	struct ath12k *ar;
7244 	u8 link_id;
7245 
7246 	lockdep_assert_wiphy(ah->hw->wiphy);
7247 
7248 	ath12k_peer_mlo_link_peers_delete(ahvif, ahsta);
7249 
7250 	/* validate link station removal and clear arsta links */
7251 	links = ahsta->links_map;
7252 	for_each_set_bit(link_id, &links, IEEE80211_MLD_MAX_NUM_LINKS) {
7253 		arvif = wiphy_dereference(ah->hw->wiphy, ahvif->link[link_id]);
7254 		arsta = wiphy_dereference(ah->hw->wiphy, ahsta->link[link_id]);
7255 		if (!arvif || !arsta)
7256 			continue;
7257 
7258 		ar = arvif->ar;
7259 
7260 		ath12k_mac_station_post_remove(ar, arvif, arsta);
7261 
7262 		spin_lock_bh(&ar->ab->base_lock);
7263 		ath12k_link_sta_rhash_delete(ar->ab, arsta);
7264 		spin_unlock_bh(&ar->ab->base_lock);
7265 
7266 		ath12k_mac_free_unassign_link_sta(ah, ahsta, link_id);
7267 	}
7268 
7269 	if (sta->mlo) {
7270 		clear_bit(ahsta->ml_peer_id, ah->free_ml_peer_id_map);
7271 		ahsta->ml_peer_id = ATH12K_MLO_PEER_ID_INVALID;
7272 	}
7273 }
7274 
7275 static int ath12k_mac_handle_link_sta_state(struct ieee80211_hw *hw,
7276 					    struct ath12k_link_vif *arvif,
7277 					    struct ath12k_link_sta *arsta,
7278 					    enum ieee80211_sta_state old_state,
7279 					    enum ieee80211_sta_state new_state)
7280 {
7281 	struct ieee80211_vif *vif = ath12k_ahvif_to_vif(arvif->ahvif);
7282 	struct ieee80211_bss_conf *link_conf;
7283 	struct ath12k *ar = arvif->ar;
7284 	struct ath12k_reg_info *reg_info;
7285 	struct ath12k_base *ab = ar->ab;
7286 	int ret = 0;
7287 
7288 	lockdep_assert_wiphy(hw->wiphy);
7289 
7290 	ath12k_dbg(ab, ATH12K_DBG_MAC, "mac handle link %u sta %pM state %d -> %d\n",
7291 		   arsta->link_id, arsta->addr, old_state, new_state);
7292 
7293 	/* IEEE80211_STA_NONE -> IEEE80211_STA_NOTEXIST: Remove the station
7294 	 * from driver
7295 	 */
7296 	if ((old_state == IEEE80211_STA_NONE &&
7297 	     new_state == IEEE80211_STA_NOTEXIST)) {
7298 		ret = ath12k_mac_station_remove(ar, arvif, arsta);
7299 		if (ret) {
7300 			ath12k_warn(ab, "Failed to remove station: %pM for VDEV: %d\n",
7301 				    arsta->addr, arvif->vdev_id);
7302 			goto exit;
7303 		}
7304 	}
7305 
7306 	/* IEEE80211_STA_NOTEXIST -> IEEE80211_STA_NONE: Add new station to driver */
7307 	if (old_state == IEEE80211_STA_NOTEXIST &&
7308 	    new_state == IEEE80211_STA_NONE) {
7309 		ret = ath12k_mac_station_add(ar, arvif, arsta);
7310 		if (ret)
7311 			ath12k_warn(ab, "Failed to add station: %pM for VDEV: %d\n",
7312 				    arsta->addr, arvif->vdev_id);
7313 
7314 	/* IEEE80211_STA_AUTH -> IEEE80211_STA_ASSOC: Send station assoc command for
7315 	 * peer associated to AP/Mesh/ADHOC vif type.
7316 	 */
7317 	} else if (old_state == IEEE80211_STA_AUTH &&
7318 		   new_state == IEEE80211_STA_ASSOC &&
7319 		   (vif->type == NL80211_IFTYPE_AP ||
7320 		    vif->type == NL80211_IFTYPE_MESH_POINT ||
7321 		    vif->type == NL80211_IFTYPE_ADHOC)) {
7322 		ret = ath12k_mac_station_assoc(ar, arvif, arsta, false);
7323 		if (ret)
7324 			ath12k_warn(ab, "Failed to associate station: %pM\n",
7325 				    arsta->addr);
7326 
7327 	/* IEEE80211_STA_ASSOC -> IEEE80211_STA_AUTHORIZED: set peer status as
7328 	 * authorized
7329 	 */
7330 	} else if (old_state == IEEE80211_STA_ASSOC &&
7331 		   new_state == IEEE80211_STA_AUTHORIZED) {
7332 		ret = ath12k_mac_station_authorize(ar, arvif, arsta);
7333 		if (ret) {
7334 			ath12k_warn(ab, "Failed to authorize station: %pM\n",
7335 				    arsta->addr);
7336 			goto exit;
7337 		}
7338 
7339 		if (ath12k_wmi_supports_6ghz_cc_ext(ar) &&
7340 		    arvif->ahvif->vdev_type == WMI_VDEV_TYPE_STA) {
7341 			link_conf = ath12k_mac_get_link_bss_conf(arvif);
7342 			reg_info = ab->reg_info[ar->pdev_idx];
7343 			ath12k_dbg(ab, ATH12K_DBG_MAC, "connection done, update reg rules\n");
7344 			ath12k_hw_to_ah(hw)->regd_updated = false;
7345 			ath12k_reg_handle_chan_list(ab, reg_info, arvif->ahvif->vdev_type,
7346 						    link_conf->power_type);
7347 		}
7348 
7349 	/* IEEE80211_STA_AUTHORIZED -> IEEE80211_STA_ASSOC: station may be in removal,
7350 	 * deauthorize it.
7351 	 */
7352 	} else if (old_state == IEEE80211_STA_AUTHORIZED &&
7353 		   new_state == IEEE80211_STA_ASSOC) {
7354 		ath12k_mac_station_unauthorize(ar, arvif, arsta);
7355 
7356 	/* IEEE80211_STA_ASSOC -> IEEE80211_STA_AUTH: disassoc peer connected to
7357 	 * AP/mesh/ADHOC vif type.
7358 	 */
7359 	} else if (old_state == IEEE80211_STA_ASSOC &&
7360 		   new_state == IEEE80211_STA_AUTH &&
7361 		   (vif->type == NL80211_IFTYPE_AP ||
7362 		    vif->type == NL80211_IFTYPE_MESH_POINT ||
7363 		    vif->type == NL80211_IFTYPE_ADHOC)) {
7364 		ret = ath12k_mac_station_disassoc(ar, arvif, arsta);
7365 		if (ret)
7366 			ath12k_warn(ab, "Failed to disassociate station: %pM\n",
7367 				    arsta->addr);
7368 	}
7369 
7370 exit:
7371 	return ret;
7372 }
7373 
7374 static bool ath12k_mac_is_freq_on_mac(struct ath12k_hw_mode_freq_range_arg *freq_range,
7375 				      u32 freq, u8 mac_id)
7376 {
7377 	return (freq >= freq_range[mac_id].low_2ghz_freq &&
7378 		freq <= freq_range[mac_id].high_2ghz_freq) ||
7379 	       (freq >= freq_range[mac_id].low_5ghz_freq &&
7380 		freq <= freq_range[mac_id].high_5ghz_freq);
7381 }
7382 
7383 static bool
7384 ath12k_mac_2_freq_same_mac_in_freq_range(struct ath12k_base *ab,
7385 					 struct ath12k_hw_mode_freq_range_arg *freq_range,
7386 					 u32 freq_link1, u32 freq_link2)
7387 {
7388 	u8 i;
7389 
7390 	for (i = 0; i < MAX_RADIOS; i++) {
7391 		if (ath12k_mac_is_freq_on_mac(freq_range, freq_link1, i) &&
7392 		    ath12k_mac_is_freq_on_mac(freq_range, freq_link2, i))
7393 			return true;
7394 	}
7395 
7396 	return false;
7397 }
7398 
7399 static bool ath12k_mac_is_hw_dbs_capable(struct ath12k_base *ab)
7400 {
7401 	return test_bit(WMI_TLV_SERVICE_DUAL_BAND_SIMULTANEOUS_SUPPORT,
7402 			ab->wmi_ab.svc_map) &&
7403 	       ab->wmi_ab.hw_mode_info.support_dbs;
7404 }
7405 
7406 static bool ath12k_mac_2_freq_same_mac_in_dbs(struct ath12k_base *ab,
7407 					      u32 freq_link1, u32 freq_link2)
7408 {
7409 	struct ath12k_hw_mode_freq_range_arg *freq_range;
7410 
7411 	if (!ath12k_mac_is_hw_dbs_capable(ab))
7412 		return true;
7413 
7414 	freq_range = ab->wmi_ab.hw_mode_info.freq_range_caps[ATH12K_HW_MODE_DBS];
7415 	return ath12k_mac_2_freq_same_mac_in_freq_range(ab, freq_range,
7416 							freq_link1, freq_link2);
7417 }
7418 
7419 static bool ath12k_mac_is_hw_sbs_capable(struct ath12k_base *ab)
7420 {
7421 	return test_bit(WMI_TLV_SERVICE_DUAL_BAND_SIMULTANEOUS_SUPPORT,
7422 			ab->wmi_ab.svc_map) &&
7423 	       ab->wmi_ab.hw_mode_info.support_sbs;
7424 }
7425 
7426 static bool ath12k_mac_2_freq_same_mac_in_sbs(struct ath12k_base *ab,
7427 					      u32 freq_link1, u32 freq_link2)
7428 {
7429 	struct ath12k_hw_mode_info *info = &ab->wmi_ab.hw_mode_info;
7430 	struct ath12k_hw_mode_freq_range_arg *sbs_uppr_share;
7431 	struct ath12k_hw_mode_freq_range_arg *sbs_low_share;
7432 	struct ath12k_hw_mode_freq_range_arg *sbs_range;
7433 
7434 	if (!ath12k_mac_is_hw_sbs_capable(ab))
7435 		return true;
7436 
7437 	if (ab->wmi_ab.sbs_lower_band_end_freq) {
7438 		sbs_uppr_share = info->freq_range_caps[ATH12K_HW_MODE_SBS_UPPER_SHARE];
7439 		sbs_low_share = info->freq_range_caps[ATH12K_HW_MODE_SBS_LOWER_SHARE];
7440 
7441 		return ath12k_mac_2_freq_same_mac_in_freq_range(ab, sbs_low_share,
7442 								freq_link1, freq_link2) ||
7443 		       ath12k_mac_2_freq_same_mac_in_freq_range(ab, sbs_uppr_share,
7444 								freq_link1, freq_link2);
7445 	}
7446 
7447 	sbs_range = info->freq_range_caps[ATH12K_HW_MODE_SBS];
7448 	return ath12k_mac_2_freq_same_mac_in_freq_range(ab, sbs_range,
7449 							freq_link1, freq_link2);
7450 }
7451 
7452 static bool ath12k_mac_freqs_on_same_mac(struct ath12k_base *ab,
7453 					 u32 freq_link1, u32 freq_link2)
7454 {
7455 	return ath12k_mac_2_freq_same_mac_in_dbs(ab, freq_link1, freq_link2) &&
7456 	       ath12k_mac_2_freq_same_mac_in_sbs(ab, freq_link1, freq_link2);
7457 }
7458 
7459 static int ath12k_mac_mlo_sta_set_link_active(struct ath12k_base *ab,
7460 					      enum wmi_mlo_link_force_reason reason,
7461 					      enum wmi_mlo_link_force_mode mode,
7462 					      u8 *mlo_vdev_id_lst,
7463 					      u8 num_mlo_vdev,
7464 					      u8 *mlo_inactive_vdev_lst,
7465 					      u8 num_mlo_inactive_vdev)
7466 {
7467 	struct wmi_mlo_link_set_active_arg param = {};
7468 	u32 entry_idx, entry_offset, vdev_idx;
7469 	u8 vdev_id;
7470 
7471 	param.reason = reason;
7472 	param.force_mode = mode;
7473 
7474 	for (vdev_idx = 0; vdev_idx < num_mlo_vdev; vdev_idx++) {
7475 		vdev_id = mlo_vdev_id_lst[vdev_idx];
7476 		entry_idx = vdev_id / 32;
7477 		entry_offset = vdev_id % 32;
7478 		if (entry_idx >= WMI_MLO_LINK_NUM_SZ) {
7479 			ath12k_warn(ab, "Invalid entry_idx %d num_mlo_vdev %d vdev %d",
7480 				    entry_idx, num_mlo_vdev, vdev_id);
7481 			return -EINVAL;
7482 		}
7483 		param.vdev_bitmap[entry_idx] |= 1 << entry_offset;
7484 		/* update entry number if entry index changed */
7485 		if (param.num_vdev_bitmap < entry_idx + 1)
7486 			param.num_vdev_bitmap = entry_idx + 1;
7487 	}
7488 
7489 	ath12k_dbg(ab, ATH12K_DBG_MAC,
7490 		   "num_vdev_bitmap %d vdev_bitmap[0] = 0x%x, vdev_bitmap[1] = 0x%x",
7491 		   param.num_vdev_bitmap, param.vdev_bitmap[0], param.vdev_bitmap[1]);
7492 
7493 	if (mode == WMI_MLO_LINK_FORCE_MODE_ACTIVE_INACTIVE) {
7494 		for (vdev_idx = 0; vdev_idx < num_mlo_inactive_vdev; vdev_idx++) {
7495 			vdev_id = mlo_inactive_vdev_lst[vdev_idx];
7496 			entry_idx = vdev_id / 32;
7497 			entry_offset = vdev_id % 32;
7498 			if (entry_idx >= WMI_MLO_LINK_NUM_SZ) {
7499 				ath12k_warn(ab, "Invalid entry_idx %d num_mlo_vdev %d vdev %d",
7500 					    entry_idx, num_mlo_inactive_vdev, vdev_id);
7501 				return -EINVAL;
7502 			}
7503 			param.inactive_vdev_bitmap[entry_idx] |= 1 << entry_offset;
7504 			/* update entry number if entry index changed */
7505 			if (param.num_inactive_vdev_bitmap < entry_idx + 1)
7506 				param.num_inactive_vdev_bitmap = entry_idx + 1;
7507 		}
7508 
7509 		ath12k_dbg(ab, ATH12K_DBG_MAC,
7510 			   "num_vdev_bitmap %d inactive_vdev_bitmap[0] = 0x%x, inactive_vdev_bitmap[1] = 0x%x",
7511 			   param.num_inactive_vdev_bitmap,
7512 			   param.inactive_vdev_bitmap[0],
7513 			   param.inactive_vdev_bitmap[1]);
7514 	}
7515 
7516 	if (mode == WMI_MLO_LINK_FORCE_MODE_ACTIVE_LINK_NUM ||
7517 	    mode == WMI_MLO_LINK_FORCE_MODE_INACTIVE_LINK_NUM) {
7518 		param.num_link_entry = 1;
7519 		param.link_num[0].num_of_link = num_mlo_vdev - 1;
7520 	}
7521 
7522 	return ath12k_wmi_send_mlo_link_set_active_cmd(ab, &param);
7523 }
7524 
7525 static int ath12k_mac_mlo_sta_update_link_active(struct ath12k_base *ab,
7526 						 struct ieee80211_hw *hw,
7527 						 struct ath12k_vif *ahvif)
7528 {
7529 	u8 mlo_vdev_id_lst[IEEE80211_MLD_MAX_NUM_LINKS] = {};
7530 	u32 mlo_freq_list[IEEE80211_MLD_MAX_NUM_LINKS] = {};
7531 	unsigned long links = ahvif->links_map;
7532 	enum wmi_mlo_link_force_reason reason;
7533 	struct ieee80211_chanctx_conf *conf;
7534 	enum wmi_mlo_link_force_mode mode;
7535 	struct ieee80211_bss_conf *info;
7536 	struct ath12k_link_vif *arvif;
7537 	u8 num_mlo_vdev = 0;
7538 	u8 link_id;
7539 
7540 	for_each_set_bit(link_id, &links, IEEE80211_MLD_MAX_NUM_LINKS) {
7541 		arvif = wiphy_dereference(hw->wiphy, ahvif->link[link_id]);
7542 		/* make sure vdev is created on this device */
7543 		if (!arvif || !arvif->is_created || arvif->ar->ab != ab)
7544 			continue;
7545 
7546 		info = ath12k_mac_get_link_bss_conf(arvif);
7547 		conf = wiphy_dereference(hw->wiphy, info->chanctx_conf);
7548 		mlo_freq_list[num_mlo_vdev] = conf->def.chan->center_freq;
7549 
7550 		mlo_vdev_id_lst[num_mlo_vdev] = arvif->vdev_id;
7551 		num_mlo_vdev++;
7552 	}
7553 
7554 	/* It is not allowed to activate more links than a single device
7555 	 * supported. Something goes wrong if we reach here.
7556 	 */
7557 	if (num_mlo_vdev > ATH12K_NUM_MAX_ACTIVE_LINKS_PER_DEVICE) {
7558 		WARN_ON_ONCE(1);
7559 		return -EINVAL;
7560 	}
7561 
7562 	/* if 2 links are established and both link channels fall on the
7563 	 * same hardware MAC, send command to firmware to deactivate one
7564 	 * of them.
7565 	 */
7566 	if (num_mlo_vdev == 2 &&
7567 	    ath12k_mac_freqs_on_same_mac(ab, mlo_freq_list[0],
7568 					 mlo_freq_list[1])) {
7569 		mode = WMI_MLO_LINK_FORCE_MODE_INACTIVE_LINK_NUM;
7570 		reason = WMI_MLO_LINK_FORCE_REASON_NEW_CONNECT;
7571 		return ath12k_mac_mlo_sta_set_link_active(ab, reason, mode,
7572 							  mlo_vdev_id_lst, num_mlo_vdev,
7573 							  NULL, 0);
7574 	}
7575 
7576 	return 0;
7577 }
7578 
7579 static bool ath12k_mac_are_sbs_chan(struct ath12k_base *ab, u32 freq_1, u32 freq_2)
7580 {
7581 	if (!ath12k_mac_is_hw_sbs_capable(ab))
7582 		return false;
7583 
7584 	if (ath12k_is_2ghz_channel_freq(freq_1) ||
7585 	    ath12k_is_2ghz_channel_freq(freq_2))
7586 		return false;
7587 
7588 	return !ath12k_mac_2_freq_same_mac_in_sbs(ab, freq_1, freq_2);
7589 }
7590 
7591 static bool ath12k_mac_are_dbs_chan(struct ath12k_base *ab, u32 freq_1, u32 freq_2)
7592 {
7593 	if (!ath12k_mac_is_hw_dbs_capable(ab))
7594 		return false;
7595 
7596 	return !ath12k_mac_2_freq_same_mac_in_dbs(ab, freq_1, freq_2);
7597 }
7598 
7599 static int ath12k_mac_select_links(struct ath12k_base *ab,
7600 				   struct ieee80211_vif *vif,
7601 				   struct ieee80211_hw *hw,
7602 				   u16 *selected_links)
7603 {
7604 	unsigned long useful_links = ieee80211_vif_usable_links(vif);
7605 	struct ath12k_vif *ahvif = ath12k_vif_to_ahvif(vif);
7606 	u8 num_useful_links = hweight_long(useful_links);
7607 	struct ieee80211_chanctx_conf *chanctx;
7608 	struct ath12k_link_vif *assoc_arvif;
7609 	u32 assoc_link_freq, partner_freq;
7610 	u16 sbs_links = 0, dbs_links = 0;
7611 	struct ieee80211_bss_conf *info;
7612 	struct ieee80211_channel *chan;
7613 	struct ieee80211_sta *sta;
7614 	struct ath12k_sta *ahsta;
7615 	u8 link_id;
7616 
7617 	/* activate all useful links if less than max supported */
7618 	if (num_useful_links <= ATH12K_NUM_MAX_ACTIVE_LINKS_PER_DEVICE) {
7619 		*selected_links = useful_links;
7620 		return 0;
7621 	}
7622 
7623 	/* only in station mode we can get here, so it's safe
7624 	 * to use ap_addr
7625 	 */
7626 	rcu_read_lock();
7627 	sta = ieee80211_find_sta(vif, vif->cfg.ap_addr);
7628 	if (!sta) {
7629 		rcu_read_unlock();
7630 		ath12k_warn(ab, "failed to find sta with addr %pM\n", vif->cfg.ap_addr);
7631 		return -EINVAL;
7632 	}
7633 
7634 	ahsta = ath12k_sta_to_ahsta(sta);
7635 	assoc_arvif = wiphy_dereference(hw->wiphy, ahvif->link[ahsta->assoc_link_id]);
7636 	info = ath12k_mac_get_link_bss_conf(assoc_arvif);
7637 	chanctx = rcu_dereference(info->chanctx_conf);
7638 	assoc_link_freq = chanctx->def.chan->center_freq;
7639 	rcu_read_unlock();
7640 	ath12k_dbg(ab, ATH12K_DBG_MAC, "assoc link %u freq %u\n",
7641 		   assoc_arvif->link_id, assoc_link_freq);
7642 
7643 	/* assoc link is already activated and has to be kept active,
7644 	 * only need to select a partner link from others.
7645 	 */
7646 	useful_links &= ~BIT(assoc_arvif->link_id);
7647 	for_each_set_bit(link_id, &useful_links, IEEE80211_MLD_MAX_NUM_LINKS) {
7648 		info = wiphy_dereference(hw->wiphy, vif->link_conf[link_id]);
7649 		if (!info) {
7650 			ath12k_warn(ab, "failed to get link info for link: %u\n",
7651 				    link_id);
7652 			return -ENOLINK;
7653 		}
7654 
7655 		chan = info->chanreq.oper.chan;
7656 		if (!chan) {
7657 			ath12k_warn(ab, "failed to get chan for link: %u\n", link_id);
7658 			return -EINVAL;
7659 		}
7660 
7661 		partner_freq = chan->center_freq;
7662 		if (ath12k_mac_are_sbs_chan(ab, assoc_link_freq, partner_freq)) {
7663 			sbs_links |= BIT(link_id);
7664 			ath12k_dbg(ab, ATH12K_DBG_MAC, "new SBS link %u freq %u\n",
7665 				   link_id, partner_freq);
7666 			continue;
7667 		}
7668 
7669 		if (ath12k_mac_are_dbs_chan(ab, assoc_link_freq, partner_freq)) {
7670 			dbs_links |= BIT(link_id);
7671 			ath12k_dbg(ab, ATH12K_DBG_MAC, "new DBS link %u freq %u\n",
7672 				   link_id, partner_freq);
7673 			continue;
7674 		}
7675 
7676 		ath12k_dbg(ab, ATH12K_DBG_MAC, "non DBS/SBS link %u freq %u\n",
7677 			   link_id, partner_freq);
7678 	}
7679 
7680 	/* choose the first candidate no matter how many is in the list */
7681 	if (sbs_links)
7682 		link_id = __ffs(sbs_links);
7683 	else if (dbs_links)
7684 		link_id = __ffs(dbs_links);
7685 	else
7686 		link_id = ffs(useful_links) - 1;
7687 
7688 	ath12k_dbg(ab, ATH12K_DBG_MAC, "select partner link %u\n", link_id);
7689 
7690 	*selected_links = BIT(assoc_arvif->link_id) | BIT(link_id);
7691 
7692 	return 0;
7693 }
7694 
7695 int ath12k_mac_op_sta_state(struct ieee80211_hw *hw,
7696 			    struct ieee80211_vif *vif,
7697 			    struct ieee80211_sta *sta,
7698 			    enum ieee80211_sta_state old_state,
7699 			    enum ieee80211_sta_state new_state)
7700 {
7701 	struct ath12k_vif *ahvif = ath12k_vif_to_ahvif(vif);
7702 	struct ath12k_sta *ahsta = ath12k_sta_to_ahsta(sta);
7703 	struct ath12k_hw *ah = ath12k_hw_to_ah(hw);
7704 	struct ath12k_base *prev_ab = NULL, *ab;
7705 	struct ath12k_link_vif *arvif;
7706 	struct ath12k_link_sta *arsta;
7707 	unsigned long valid_links;
7708 	u16 selected_links = 0;
7709 	u8 link_id = 0, i;
7710 	struct ath12k *ar;
7711 	int ret = -EINVAL;
7712 	struct ath12k_dp_peer_create_params dp_params = {};
7713 
7714 	lockdep_assert_wiphy(hw->wiphy);
7715 
7716 	if (ieee80211_vif_is_mld(vif) && sta->valid_links) {
7717 		WARN_ON(!sta->mlo && hweight16(sta->valid_links) != 1);
7718 		link_id = ffs(sta->valid_links) - 1;
7719 	}
7720 
7721 	/* IEEE80211_STA_NOTEXIST -> IEEE80211_STA_NONE:
7722 	 * New station add received. If this is a ML station then
7723 	 * ahsta->links_map will be zero and sta->valid_links will be 1.
7724 	 * Assign default link to the first link sta.
7725 	 */
7726 	if (old_state == IEEE80211_STA_NOTEXIST &&
7727 	    new_state == IEEE80211_STA_NONE) {
7728 		memset(ahsta, 0, sizeof(*ahsta));
7729 		ahsta->free_logical_link_idx_map = U16_MAX;
7730 
7731 		arsta = &ahsta->deflink;
7732 
7733 		/* ML sta */
7734 		if (sta->mlo && !ahsta->links_map &&
7735 		    (hweight16(sta->valid_links) == 1)) {
7736 			ahsta->ml_peer_id = ath12k_peer_ml_alloc(ah);
7737 			if (ahsta->ml_peer_id == ATH12K_MLO_PEER_ID_INVALID) {
7738 				ath12k_hw_warn(ah, "unable to allocate ML peer id for sta %pM",
7739 					       sta->addr);
7740 				goto exit;
7741 			}
7742 
7743 			dp_params.is_mlo = true;
7744 			dp_params.peer_id = ahsta->ml_peer_id | ATH12K_PEER_ML_ID_VALID;
7745 		}
7746 
7747 		dp_params.sta = sta;
7748 
7749 		if (vif->type == NL80211_IFTYPE_AP)
7750 			dp_params.ucast_ra_only = true;
7751 
7752 		ret = ath12k_dp_peer_create(&ah->dp_hw, sta->addr, &dp_params);
7753 		if (ret) {
7754 			ath12k_hw_warn(ah, "unable to create ath12k_dp_peer for sta %pM, ret: %d",
7755 				       sta->addr, ret);
7756 
7757 			goto ml_peer_id_clear;
7758 		}
7759 
7760 		ret = ath12k_mac_assign_link_sta(ah, ahsta, arsta, ahvif,
7761 						 link_id);
7762 		if (ret) {
7763 			ath12k_hw_warn(ah, "unable assign link %d for sta %pM",
7764 				       link_id, sta->addr);
7765 			goto peer_delete;
7766 		}
7767 
7768 		/* above arsta will get memset, hence do this after assign
7769 		 * link sta
7770 		 */
7771 		if (sta->mlo) {
7772 			/* For station mode, arvif->is_sta_assoc_link has been set when
7773 			 * vdev starts. Make sure the arvif/arsta pair have same setting
7774 			 */
7775 			if (vif->type == NL80211_IFTYPE_STATION &&
7776 			    !arsta->arvif->is_sta_assoc_link) {
7777 				ath12k_hw_warn(ah, "failed to verify assoc link setting with link id %u\n",
7778 					       link_id);
7779 				ret = -EINVAL;
7780 				goto exit;
7781 			}
7782 
7783 			arsta->is_assoc_link = true;
7784 			ahsta->assoc_link_id = link_id;
7785 		}
7786 	}
7787 
7788 	/* In the ML station scenario, activate all partner links once the
7789 	 * client is transitioning to the associated state.
7790 	 *
7791 	 * FIXME: Ideally, this activation should occur when the client
7792 	 * transitions to the authorized state. However, there are some
7793 	 * issues with handling this in the firmware. Until the firmware
7794 	 * can manage it properly, activate the links when the client is
7795 	 * about to move to the associated state.
7796 	 */
7797 	if (ieee80211_vif_is_mld(vif) && vif->type == NL80211_IFTYPE_STATION &&
7798 	    old_state == IEEE80211_STA_AUTH && new_state == IEEE80211_STA_ASSOC) {
7799 		/* TODO: for now only do link selection for single device
7800 		 * MLO case. Other cases would be handled in the future.
7801 		 */
7802 		ab = ah->radio[0].ab;
7803 		if (ab->ag->num_devices == 1) {
7804 			ret = ath12k_mac_select_links(ab, vif, hw, &selected_links);
7805 			if (ret) {
7806 				ath12k_warn(ab,
7807 					    "failed to get selected links: %d\n", ret);
7808 				goto exit;
7809 			}
7810 		} else {
7811 			selected_links = ieee80211_vif_usable_links(vif);
7812 		}
7813 
7814 		ieee80211_set_active_links(vif, selected_links);
7815 	}
7816 
7817 	/* Handle all the other state transitions in generic way */
7818 	valid_links = ahsta->links_map;
7819 	for_each_set_bit(link_id, &valid_links, IEEE80211_MLD_MAX_NUM_LINKS) {
7820 		arvif = wiphy_dereference(hw->wiphy, ahvif->link[link_id]);
7821 		arsta = wiphy_dereference(hw->wiphy, ahsta->link[link_id]);
7822 		/* some assumptions went wrong! */
7823 		if (WARN_ON(!arvif || !arsta))
7824 			continue;
7825 
7826 		/* vdev might be in deleted */
7827 		if (WARN_ON(!arvif->ar))
7828 			continue;
7829 
7830 		ret = ath12k_mac_handle_link_sta_state(hw, arvif, arsta,
7831 						       old_state, new_state);
7832 		if (ret) {
7833 			ath12k_hw_warn(ah, "unable to move link sta %d of sta %pM from state %d to %d",
7834 				       link_id, arsta->addr, old_state, new_state);
7835 
7836 			if (old_state == IEEE80211_STA_NOTEXIST &&
7837 			    new_state == IEEE80211_STA_NONE)
7838 				goto peer_delete;
7839 			else
7840 				goto exit;
7841 		}
7842 	}
7843 
7844 	if (ieee80211_vif_is_mld(vif) && vif->type == NL80211_IFTYPE_STATION &&
7845 	    old_state == IEEE80211_STA_ASSOC && new_state == IEEE80211_STA_AUTHORIZED) {
7846 		for_each_ar(ah, ar, i) {
7847 			ab = ar->ab;
7848 			if (prev_ab == ab)
7849 				continue;
7850 
7851 			ret = ath12k_mac_mlo_sta_update_link_active(ab, hw, ahvif);
7852 			if (ret) {
7853 				ath12k_warn(ab,
7854 					    "failed to update link active state on connect %d\n",
7855 					    ret);
7856 				goto exit;
7857 			}
7858 
7859 			prev_ab = ab;
7860 		}
7861 	}
7862 	/* IEEE80211_STA_NONE -> IEEE80211_STA_NOTEXIST:
7863 	 * Remove the station from driver (handle ML sta here since that
7864 	 * needs special handling. Normal sta will be handled in generic
7865 	 * handler below
7866 	 */
7867 	if (old_state == IEEE80211_STA_NONE &&
7868 	    new_state == IEEE80211_STA_NOTEXIST) {
7869 		if (sta->mlo)
7870 			ath12k_mac_ml_station_remove(ahvif, ahsta);
7871 
7872 		ath12k_dp_peer_delete(&ah->dp_hw, sta->addr, sta);
7873 	}
7874 
7875 	ret = 0;
7876 	goto exit;
7877 
7878 peer_delete:
7879 	ath12k_dp_peer_delete(&ah->dp_hw, sta->addr, sta);
7880 ml_peer_id_clear:
7881 	if (sta->mlo) {
7882 		clear_bit(ahsta->ml_peer_id, ah->free_ml_peer_id_map);
7883 		ahsta->ml_peer_id = ATH12K_MLO_PEER_ID_INVALID;
7884 	}
7885 exit:
7886 	/* update the state if everything went well */
7887 	if (!ret)
7888 		ahsta->state = new_state;
7889 
7890 	return ret;
7891 }
7892 EXPORT_SYMBOL(ath12k_mac_op_sta_state);
7893 
7894 int ath12k_mac_op_sta_set_txpwr(struct ieee80211_hw *hw,
7895 				struct ieee80211_vif *vif,
7896 				struct ieee80211_sta *sta)
7897 {
7898 	struct ath12k_sta *ahsta = ath12k_sta_to_ahsta(sta);
7899 	struct ath12k *ar;
7900 	struct ath12k_vif *ahvif = ath12k_vif_to_ahvif(vif);
7901 	struct ath12k_link_vif *arvif;
7902 	struct ath12k_link_sta *arsta;
7903 	u8 link_id;
7904 	int ret;
7905 	s16 txpwr;
7906 
7907 	lockdep_assert_wiphy(hw->wiphy);
7908 
7909 	/* TODO: use link id from mac80211 once that's implemented */
7910 	link_id = 0;
7911 
7912 	arvif = wiphy_dereference(hw->wiphy, ahvif->link[link_id]);
7913 	arsta = wiphy_dereference(hw->wiphy, ahsta->link[link_id]);
7914 
7915 	if (sta->deflink.txpwr.type == NL80211_TX_POWER_AUTOMATIC) {
7916 		txpwr = 0;
7917 	} else {
7918 		txpwr = sta->deflink.txpwr.power;
7919 		if (!txpwr) {
7920 			ret = -EINVAL;
7921 			goto out;
7922 		}
7923 	}
7924 
7925 	if (txpwr > ATH12K_TX_POWER_MAX_VAL || txpwr < ATH12K_TX_POWER_MIN_VAL) {
7926 		ret = -EINVAL;
7927 		goto out;
7928 	}
7929 
7930 	ar = arvif->ar;
7931 
7932 	ret = ath12k_wmi_set_peer_param(ar, arsta->addr, arvif->vdev_id,
7933 					WMI_PEER_USE_FIXED_PWR, txpwr);
7934 	if (ret) {
7935 		ath12k_warn(ar->ab, "failed to set tx power for station ret: %d\n",
7936 			    ret);
7937 		goto out;
7938 	}
7939 
7940 out:
7941 	return ret;
7942 }
7943 EXPORT_SYMBOL(ath12k_mac_op_sta_set_txpwr);
7944 
7945 void ath12k_mac_op_sta_set_4addr(struct ieee80211_hw *hw,
7946 				 struct ieee80211_vif *vif,
7947 				 struct ieee80211_sta *sta, bool enabled)
7948 {
7949 	struct ath12k_sta *ahsta = ath12k_sta_to_ahsta(sta);
7950 
7951 	lockdep_assert_wiphy(hw->wiphy);
7952 
7953 	/*
7954 	 * 4-address mode disabled option is available only for station
7955 	 * interface from mac80211, and we have wds_vdev_param for station
7956 	 * interface and target will not allow to disable the wds_vdev_param
7957 	 * during run time. So, add support only for enable case, for
7958 	 * disable case station interface needs to be reconnect.
7959 	 */
7960 	if (enabled && !ahsta->enable_4addr) {
7961 		if (!ath12k_mac_sta_set_4addr(hw->wiphy, ahsta))
7962 			ahsta->enable_4addr = true;
7963 	}
7964 }
7965 EXPORT_SYMBOL(ath12k_mac_op_sta_set_4addr);
7966 
7967 void ath12k_mac_op_link_sta_rc_update(struct ieee80211_hw *hw,
7968 				      struct ieee80211_vif *vif,
7969 				      struct ieee80211_link_sta *link_sta,
7970 				      u32 changed)
7971 {
7972 	struct ieee80211_sta *sta = link_sta->sta;
7973 	struct ath12k *ar;
7974 	struct ath12k_sta *ahsta = ath12k_sta_to_ahsta(sta);
7975 	struct ath12k_vif *ahvif = ath12k_vif_to_ahvif(vif);
7976 	struct ath12k_hw *ah = ath12k_hw_to_ah(hw);
7977 	struct ath12k_link_sta *arsta;
7978 	struct ath12k_link_vif *arvif;
7979 	struct ath12k_dp_link_peer *peer;
7980 	u32 bw, smps;
7981 	struct ath12k_dp *dp;
7982 
7983 	rcu_read_lock();
7984 	arvif = rcu_dereference(ahvif->link[link_sta->link_id]);
7985 	if (!arvif) {
7986 		ath12k_hw_warn(ah, "mac sta rc update failed to fetch link vif on link id %u for peer %pM\n",
7987 			       link_sta->link_id, sta->addr);
7988 		rcu_read_unlock();
7989 		return;
7990 	}
7991 
7992 	ar = arvif->ar;
7993 	dp = ath12k_ab_to_dp(ar->ab);
7994 
7995 	arsta = rcu_dereference(ahsta->link[link_sta->link_id]);
7996 	if (!arsta) {
7997 		rcu_read_unlock();
7998 		ath12k_warn(ar->ab, "mac sta rc update failed to fetch link sta on link id %u for peer %pM\n",
7999 			    link_sta->link_id, sta->addr);
8000 		return;
8001 	}
8002 	spin_lock_bh(&dp->dp_lock);
8003 
8004 	peer = ath12k_dp_link_peer_find_by_vdev_and_addr(dp, arvif->vdev_id,
8005 							 arsta->addr);
8006 	if (!peer) {
8007 		spin_unlock_bh(&dp->dp_lock);
8008 		rcu_read_unlock();
8009 		ath12k_warn(ar->ab, "mac sta rc update failed to find peer %pM on vdev %i\n",
8010 			    arsta->addr, arvif->vdev_id);
8011 		return;
8012 	}
8013 
8014 	spin_unlock_bh(&dp->dp_lock);
8015 
8016 	if (arsta->link_id >= IEEE80211_MLD_MAX_NUM_LINKS) {
8017 		rcu_read_unlock();
8018 		return;
8019 	}
8020 
8021 	link_sta = rcu_dereference(sta->link[arsta->link_id]);
8022 	if (!link_sta) {
8023 		rcu_read_unlock();
8024 		ath12k_warn(ar->ab, "unable to access link sta in rc update for sta %pM link %u\n",
8025 			    sta->addr, arsta->link_id);
8026 		return;
8027 	}
8028 
8029 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
8030 		   "mac sta rc update for %pM changed %08x bw %d nss %d smps %d\n",
8031 		   arsta->addr, changed, link_sta->bandwidth, link_sta->rx_nss,
8032 		   link_sta->smps_mode);
8033 
8034 	spin_lock_bh(&ar->data_lock);
8035 
8036 	if (changed & IEEE80211_RC_BW_CHANGED) {
8037 		bw = ath12k_mac_ieee80211_sta_bw_to_wmi(ar, link_sta);
8038 		arsta->bw_prev = arsta->bw;
8039 		arsta->bw = bw;
8040 	}
8041 
8042 	if (changed & IEEE80211_RC_NSS_CHANGED)
8043 		arsta->nss = link_sta->rx_nss;
8044 
8045 	if (changed & IEEE80211_RC_SMPS_CHANGED) {
8046 		smps = WMI_PEER_SMPS_PS_NONE;
8047 
8048 		switch (link_sta->smps_mode) {
8049 		case IEEE80211_SMPS_AUTOMATIC:
8050 		case IEEE80211_SMPS_OFF:
8051 			smps = WMI_PEER_SMPS_PS_NONE;
8052 			break;
8053 		case IEEE80211_SMPS_STATIC:
8054 			smps = WMI_PEER_SMPS_STATIC;
8055 			break;
8056 		case IEEE80211_SMPS_DYNAMIC:
8057 			smps = WMI_PEER_SMPS_DYNAMIC;
8058 			break;
8059 		default:
8060 			ath12k_warn(ar->ab, "Invalid smps %d in sta rc update for %pM link %u\n",
8061 				    link_sta->smps_mode, arsta->addr, link_sta->link_id);
8062 			smps = WMI_PEER_SMPS_PS_NONE;
8063 			break;
8064 		}
8065 
8066 		arsta->smps = smps;
8067 	}
8068 
8069 	arsta->changed |= changed;
8070 
8071 	spin_unlock_bh(&ar->data_lock);
8072 
8073 	wiphy_work_queue(hw->wiphy, &arsta->update_wk);
8074 
8075 	rcu_read_unlock();
8076 }
8077 EXPORT_SYMBOL(ath12k_mac_op_link_sta_rc_update);
8078 
8079 static struct ath12k_link_sta *ath12k_mac_alloc_assign_link_sta(struct ath12k_hw *ah,
8080 								struct ath12k_sta *ahsta,
8081 								struct ath12k_vif *ahvif,
8082 								u8 link_id)
8083 {
8084 	struct ath12k_link_sta *arsta;
8085 	int ret;
8086 
8087 	lockdep_assert_wiphy(ah->hw->wiphy);
8088 
8089 	if (link_id >= IEEE80211_MLD_MAX_NUM_LINKS)
8090 		return NULL;
8091 
8092 	arsta = wiphy_dereference(ah->hw->wiphy, ahsta->link[link_id]);
8093 	if (arsta)
8094 		return NULL;
8095 
8096 	arsta = kmalloc_obj(*arsta);
8097 	if (!arsta)
8098 		return NULL;
8099 
8100 	ret = ath12k_mac_assign_link_sta(ah, ahsta, arsta, ahvif, link_id);
8101 	if (ret) {
8102 		kfree(arsta);
8103 		return NULL;
8104 	}
8105 
8106 	return arsta;
8107 }
8108 
8109 int ath12k_mac_op_change_sta_links(struct ieee80211_hw *hw,
8110 				   struct ieee80211_vif *vif,
8111 				   struct ieee80211_sta *sta,
8112 				   u16 old_links, u16 new_links)
8113 {
8114 	struct ath12k_vif *ahvif = ath12k_vif_to_ahvif(vif);
8115 	struct ath12k_sta *ahsta = ath12k_sta_to_ahsta(sta);
8116 	struct ath12k_hw *ah = hw->priv;
8117 	struct ath12k_link_vif *arvif;
8118 	struct ath12k_link_sta *arsta;
8119 	unsigned long valid_links;
8120 	struct ath12k *ar;
8121 	u8 link_id;
8122 	int ret;
8123 
8124 	lockdep_assert_wiphy(hw->wiphy);
8125 
8126 	if (!sta->valid_links)
8127 		return -EINVAL;
8128 
8129 	/* Firmware does not support removal of one of link stas. All sta
8130 	 * would be removed during ML STA delete in sta_state(), hence link
8131 	 * sta removal is not handled here.
8132 	 */
8133 	if (new_links < old_links)
8134 		return 0;
8135 
8136 	if (ahsta->ml_peer_id == ATH12K_MLO_PEER_ID_INVALID) {
8137 		ath12k_hw_warn(ah, "unable to add link for ml sta %pM", sta->addr);
8138 		return -EINVAL;
8139 	}
8140 
8141 	/* this op is expected only after initial sta insertion with default link */
8142 	if (WARN_ON(ahsta->links_map == 0))
8143 		return -EINVAL;
8144 
8145 	valid_links = new_links;
8146 	for_each_set_bit(link_id, &valid_links, IEEE80211_MLD_MAX_NUM_LINKS) {
8147 		if (ahsta->links_map & BIT(link_id))
8148 			continue;
8149 
8150 		arvif = wiphy_dereference(hw->wiphy, ahvif->link[link_id]);
8151 		if (!arvif || !arvif->is_created)
8152 			continue;
8153 
8154 		arsta = ath12k_mac_alloc_assign_link_sta(ah, ahsta, ahvif, link_id);
8155 		if (!arsta) {
8156 			ath12k_hw_warn(ah, "Failed to alloc/assign link sta");
8157 			continue;
8158 		}
8159 
8160 		ar = arvif->ar;
8161 
8162 		ret = ath12k_mac_station_add(ar, arvif, arsta);
8163 		if (ret) {
8164 			ath12k_warn(ar->ab, "Failed to add station: %pM for VDEV: %d\n",
8165 				    arsta->addr, arvif->vdev_id);
8166 			ath12k_mac_free_unassign_link_sta(ah, ahsta, link_id);
8167 			return ret;
8168 		}
8169 	}
8170 
8171 	return 0;
8172 }
8173 EXPORT_SYMBOL(ath12k_mac_op_change_sta_links);
8174 
8175 bool ath12k_mac_op_can_activate_links(struct ieee80211_hw *hw,
8176 				      struct ieee80211_vif *vif,
8177 				      u16 active_links)
8178 {
8179 	/* TODO: Handle recovery case */
8180 
8181 	return true;
8182 }
8183 EXPORT_SYMBOL(ath12k_mac_op_can_activate_links);
8184 
8185 static int ath12k_conf_tx_uapsd(struct ath12k_link_vif *arvif,
8186 				u16 ac, bool enable)
8187 {
8188 	struct ath12k *ar = arvif->ar;
8189 	struct ath12k_vif *ahvif = arvif->ahvif;
8190 	u32 value;
8191 	int ret;
8192 
8193 	if (ahvif->vdev_type != WMI_VDEV_TYPE_STA)
8194 		return 0;
8195 
8196 	switch (ac) {
8197 	case IEEE80211_AC_VO:
8198 		value = WMI_STA_PS_UAPSD_AC3_DELIVERY_EN |
8199 			WMI_STA_PS_UAPSD_AC3_TRIGGER_EN;
8200 		break;
8201 	case IEEE80211_AC_VI:
8202 		value = WMI_STA_PS_UAPSD_AC2_DELIVERY_EN |
8203 			WMI_STA_PS_UAPSD_AC2_TRIGGER_EN;
8204 		break;
8205 	case IEEE80211_AC_BE:
8206 		value = WMI_STA_PS_UAPSD_AC1_DELIVERY_EN |
8207 			WMI_STA_PS_UAPSD_AC1_TRIGGER_EN;
8208 		break;
8209 	case IEEE80211_AC_BK:
8210 		value = WMI_STA_PS_UAPSD_AC0_DELIVERY_EN |
8211 			WMI_STA_PS_UAPSD_AC0_TRIGGER_EN;
8212 		break;
8213 	}
8214 
8215 	if (enable)
8216 		ahvif->u.sta.uapsd |= value;
8217 	else
8218 		ahvif->u.sta.uapsd &= ~value;
8219 
8220 	ret = ath12k_wmi_set_sta_ps_param(ar, arvif->vdev_id,
8221 					  WMI_STA_PS_PARAM_UAPSD,
8222 					  ahvif->u.sta.uapsd);
8223 	if (ret) {
8224 		ath12k_warn(ar->ab, "could not set uapsd params %d\n", ret);
8225 		goto exit;
8226 	}
8227 
8228 	if (ahvif->u.sta.uapsd)
8229 		value = WMI_STA_PS_RX_WAKE_POLICY_POLL_UAPSD;
8230 	else
8231 		value = WMI_STA_PS_RX_WAKE_POLICY_WAKE;
8232 
8233 	ret = ath12k_wmi_set_sta_ps_param(ar, arvif->vdev_id,
8234 					  WMI_STA_PS_PARAM_RX_WAKE_POLICY,
8235 					  value);
8236 	if (ret)
8237 		ath12k_warn(ar->ab, "could not set rx wake param %d\n", ret);
8238 
8239 exit:
8240 	return ret;
8241 }
8242 
8243 static int ath12k_mac_conf_tx(struct ath12k_link_vif *arvif, u16 ac,
8244 			      const struct ieee80211_tx_queue_params *params)
8245 {
8246 	struct wmi_wmm_params_arg *p = NULL;
8247 	struct ath12k *ar = arvif->ar;
8248 	struct ath12k_base *ab = ar->ab;
8249 	int ret;
8250 
8251 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
8252 
8253 	switch (ac) {
8254 	case IEEE80211_AC_VO:
8255 		p = &arvif->wmm_params.ac_vo;
8256 		break;
8257 	case IEEE80211_AC_VI:
8258 		p = &arvif->wmm_params.ac_vi;
8259 		break;
8260 	case IEEE80211_AC_BE:
8261 		p = &arvif->wmm_params.ac_be;
8262 		break;
8263 	case IEEE80211_AC_BK:
8264 		p = &arvif->wmm_params.ac_bk;
8265 		break;
8266 	}
8267 
8268 	if (WARN_ON(!p)) {
8269 		ret = -EINVAL;
8270 		goto exit;
8271 	}
8272 
8273 	p->cwmin = params->cw_min;
8274 	p->cwmax = params->cw_max;
8275 	p->aifs = params->aifs;
8276 	p->txop = params->txop;
8277 
8278 	ret = ath12k_wmi_send_wmm_update_cmd(ar, arvif->vdev_id,
8279 					     &arvif->wmm_params);
8280 	if (ret) {
8281 		ath12k_warn(ab, "pdev idx %d failed to set wmm params: %d\n",
8282 			    ar->pdev_idx, ret);
8283 		goto exit;
8284 	}
8285 
8286 	ret = ath12k_conf_tx_uapsd(arvif, ac, params->uapsd);
8287 	if (ret)
8288 		ath12k_warn(ab, "pdev idx %d failed to set sta uapsd: %d\n",
8289 			    ar->pdev_idx, ret);
8290 
8291 exit:
8292 	return ret;
8293 }
8294 
8295 int ath12k_mac_op_conf_tx(struct ieee80211_hw *hw,
8296 			  struct ieee80211_vif *vif,
8297 			  unsigned int link_id, u16 ac,
8298 			  const struct ieee80211_tx_queue_params *params)
8299 {
8300 	struct ath12k_vif *ahvif = ath12k_vif_to_ahvif(vif);
8301 	struct ath12k_link_vif *arvif;
8302 	struct ath12k_vif_cache *cache;
8303 	int ret;
8304 
8305 	lockdep_assert_wiphy(hw->wiphy);
8306 
8307 	if (link_id >= IEEE80211_MLD_MAX_NUM_LINKS)
8308 		return -EINVAL;
8309 
8310 	arvif = wiphy_dereference(hw->wiphy, ahvif->link[link_id]);
8311 	if (!arvif || !arvif->is_created) {
8312 		cache = ath12k_ahvif_get_link_cache(ahvif, link_id);
8313 		if (!cache)
8314 			return -ENOSPC;
8315 
8316 		cache->tx_conf.changed = true;
8317 		cache->tx_conf.ac = ac;
8318 		cache->tx_conf.tx_queue_params = *params;
8319 
8320 		return 0;
8321 	}
8322 
8323 	ret = ath12k_mac_conf_tx(arvif, ac, params);
8324 
8325 	return ret;
8326 }
8327 EXPORT_SYMBOL(ath12k_mac_op_conf_tx);
8328 
8329 static struct ieee80211_sta_ht_cap
8330 ath12k_create_ht_cap(struct ath12k *ar, u32 ar_ht_cap, u32 rate_cap_rx_chainmask)
8331 {
8332 	int i;
8333 	struct ieee80211_sta_ht_cap ht_cap = {};
8334 	u32 ar_vht_cap = ar->pdev->cap.vht_cap;
8335 
8336 	if (!(ar_ht_cap & WMI_HT_CAP_ENABLED))
8337 		return ht_cap;
8338 
8339 	ht_cap.ht_supported = 1;
8340 	ht_cap.ampdu_factor = IEEE80211_HT_MAX_AMPDU_64K;
8341 	ht_cap.ampdu_density = IEEE80211_HT_MPDU_DENSITY_NONE;
8342 	ht_cap.cap |= IEEE80211_HT_CAP_SUP_WIDTH_20_40;
8343 	ht_cap.cap |= IEEE80211_HT_CAP_DSSSCCK40;
8344 	ht_cap.cap |= WLAN_HT_CAP_SM_PS_STATIC << IEEE80211_HT_CAP_SM_PS_SHIFT;
8345 
8346 	if (ar_ht_cap & WMI_HT_CAP_HT20_SGI)
8347 		ht_cap.cap |= IEEE80211_HT_CAP_SGI_20;
8348 
8349 	if (ar_ht_cap & WMI_HT_CAP_HT40_SGI)
8350 		ht_cap.cap |= IEEE80211_HT_CAP_SGI_40;
8351 
8352 	if (ar_ht_cap & WMI_HT_CAP_DYNAMIC_SMPS) {
8353 		u32 smps;
8354 
8355 		smps   = WLAN_HT_CAP_SM_PS_DYNAMIC;
8356 		smps <<= IEEE80211_HT_CAP_SM_PS_SHIFT;
8357 
8358 		ht_cap.cap |= smps;
8359 	}
8360 
8361 	if (ar_ht_cap & WMI_HT_CAP_TX_STBC)
8362 		ht_cap.cap |= IEEE80211_HT_CAP_TX_STBC;
8363 
8364 	if (ar_ht_cap & WMI_HT_CAP_RX_STBC) {
8365 		u32 stbc;
8366 
8367 		stbc   = ar_ht_cap;
8368 		stbc  &= WMI_HT_CAP_RX_STBC;
8369 		stbc >>= WMI_HT_CAP_RX_STBC_MASK_SHIFT;
8370 		stbc <<= IEEE80211_HT_CAP_RX_STBC_SHIFT;
8371 		stbc  &= IEEE80211_HT_CAP_RX_STBC;
8372 
8373 		ht_cap.cap |= stbc;
8374 	}
8375 
8376 	if (ar_ht_cap & WMI_HT_CAP_RX_LDPC)
8377 		ht_cap.cap |= IEEE80211_HT_CAP_LDPC_CODING;
8378 
8379 	if (ar_ht_cap & WMI_HT_CAP_L_SIG_TXOP_PROT)
8380 		ht_cap.cap |= IEEE80211_HT_CAP_LSIG_TXOP_PROT;
8381 
8382 	if (ar_vht_cap & WMI_VHT_CAP_MAX_MPDU_LEN_MASK)
8383 		ht_cap.cap |= IEEE80211_HT_CAP_MAX_AMSDU;
8384 
8385 	for (i = 0; i < ar->num_rx_chains; i++) {
8386 		if (rate_cap_rx_chainmask & BIT(i))
8387 			ht_cap.mcs.rx_mask[i] = 0xFF;
8388 	}
8389 
8390 	ht_cap.mcs.tx_params |= IEEE80211_HT_MCS_TX_DEFINED;
8391 
8392 	return ht_cap;
8393 }
8394 
8395 static int ath12k_mac_set_txbf_conf(struct ath12k_link_vif *arvif)
8396 {
8397 	u32 value = 0;
8398 	struct ath12k *ar = arvif->ar;
8399 	struct ath12k_vif *ahvif = arvif->ahvif;
8400 	int nsts;
8401 	int sound_dim;
8402 	u32 vht_cap = ar->pdev->cap.vht_cap;
8403 	u32 vdev_param = WMI_VDEV_PARAM_TXBF;
8404 
8405 	if (vht_cap & (IEEE80211_VHT_CAP_SU_BEAMFORMEE_CAPABLE)) {
8406 		nsts = vht_cap & IEEE80211_VHT_CAP_BEAMFORMEE_STS_MASK;
8407 		nsts >>= IEEE80211_VHT_CAP_BEAMFORMEE_STS_SHIFT;
8408 		value |= SM(nsts, WMI_TXBF_STS_CAP_OFFSET);
8409 	}
8410 
8411 	if (vht_cap & (IEEE80211_VHT_CAP_SU_BEAMFORMER_CAPABLE)) {
8412 		sound_dim = vht_cap &
8413 			    IEEE80211_VHT_CAP_SOUNDING_DIMENSIONS_MASK;
8414 		sound_dim >>= IEEE80211_VHT_CAP_SOUNDING_DIMENSIONS_SHIFT;
8415 		if (sound_dim > (ar->num_tx_chains - 1))
8416 			sound_dim = ar->num_tx_chains - 1;
8417 		value |= SM(sound_dim, WMI_BF_SOUND_DIM_OFFSET);
8418 	}
8419 
8420 	if (!value)
8421 		return 0;
8422 
8423 	if (vht_cap & IEEE80211_VHT_CAP_SU_BEAMFORMER_CAPABLE) {
8424 		value |= WMI_VDEV_PARAM_TXBF_SU_TX_BFER;
8425 
8426 		if ((vht_cap & IEEE80211_VHT_CAP_MU_BEAMFORMER_CAPABLE) &&
8427 		    ahvif->vdev_type == WMI_VDEV_TYPE_AP)
8428 			value |= WMI_VDEV_PARAM_TXBF_MU_TX_BFER;
8429 	}
8430 
8431 	if (vht_cap & IEEE80211_VHT_CAP_SU_BEAMFORMEE_CAPABLE) {
8432 		value |= WMI_VDEV_PARAM_TXBF_SU_TX_BFEE;
8433 
8434 		if ((vht_cap & IEEE80211_VHT_CAP_MU_BEAMFORMEE_CAPABLE) &&
8435 		    ahvif->vdev_type == WMI_VDEV_TYPE_STA)
8436 			value |= WMI_VDEV_PARAM_TXBF_MU_TX_BFEE;
8437 	}
8438 
8439 	return ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
8440 					     vdev_param, value);
8441 }
8442 
8443 static void ath12k_set_vht_txbf_cap(struct ath12k *ar, u32 *vht_cap)
8444 {
8445 	bool subfer, subfee;
8446 	int sound_dim = 0;
8447 
8448 	subfer = !!(*vht_cap & (IEEE80211_VHT_CAP_SU_BEAMFORMER_CAPABLE));
8449 	subfee = !!(*vht_cap & (IEEE80211_VHT_CAP_SU_BEAMFORMEE_CAPABLE));
8450 
8451 	if (ar->num_tx_chains < 2) {
8452 		*vht_cap &= ~(IEEE80211_VHT_CAP_SU_BEAMFORMER_CAPABLE);
8453 		subfer = false;
8454 	}
8455 
8456 	/* If SU Beaformer is not set, then disable MU Beamformer Capability */
8457 	if (!subfer)
8458 		*vht_cap &= ~(IEEE80211_VHT_CAP_MU_BEAMFORMER_CAPABLE);
8459 
8460 	/* If SU Beaformee is not set, then disable MU Beamformee Capability */
8461 	if (!subfee)
8462 		*vht_cap &= ~(IEEE80211_VHT_CAP_MU_BEAMFORMEE_CAPABLE);
8463 
8464 	sound_dim = u32_get_bits(*vht_cap,
8465 				 IEEE80211_VHT_CAP_SOUNDING_DIMENSIONS_MASK);
8466 	*vht_cap = u32_replace_bits(*vht_cap, 0,
8467 				    IEEE80211_VHT_CAP_SOUNDING_DIMENSIONS_MASK);
8468 
8469 	/* TODO: Need to check invalid STS and Sound_dim values set by FW? */
8470 
8471 	/* Enable Sounding Dimension Field only if SU BF is enabled */
8472 	if (subfer) {
8473 		if (sound_dim > (ar->num_tx_chains - 1))
8474 			sound_dim = ar->num_tx_chains - 1;
8475 
8476 		*vht_cap = u32_replace_bits(*vht_cap, sound_dim,
8477 					    IEEE80211_VHT_CAP_SOUNDING_DIMENSIONS_MASK);
8478 	}
8479 
8480 	/* Use the STS advertised by FW unless SU Beamformee is not supported*/
8481 	if (!subfee)
8482 		*vht_cap &= ~(IEEE80211_VHT_CAP_BEAMFORMEE_STS_MASK);
8483 }
8484 
8485 static struct ieee80211_sta_vht_cap
8486 ath12k_create_vht_cap(struct ath12k *ar, u32 rate_cap_tx_chainmask,
8487 		      u32 rate_cap_rx_chainmask)
8488 {
8489 	struct ieee80211_sta_vht_cap vht_cap = {};
8490 	u16 txmcs_map, rxmcs_map;
8491 	int i;
8492 
8493 	vht_cap.vht_supported = 1;
8494 	vht_cap.cap = ar->pdev->cap.vht_cap;
8495 
8496 	if (ar->pdev->cap.nss_ratio_enabled)
8497 		vht_cap.vht_mcs.tx_highest |=
8498 			cpu_to_le16(IEEE80211_VHT_EXT_NSS_BW_CAPABLE);
8499 
8500 	ath12k_set_vht_txbf_cap(ar, &vht_cap.cap);
8501 
8502 	/* 80P80 is not supported */
8503 	vht_cap.cap &= ~IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_160_80PLUS80MHZ;
8504 
8505 	rxmcs_map = 0;
8506 	txmcs_map = 0;
8507 	for (i = 0; i < 8; i++) {
8508 		if (i < ar->num_tx_chains && rate_cap_tx_chainmask & BIT(i))
8509 			txmcs_map |= IEEE80211_VHT_MCS_SUPPORT_0_9 << (i * 2);
8510 		else
8511 			txmcs_map |= IEEE80211_VHT_MCS_NOT_SUPPORTED << (i * 2);
8512 
8513 		if (i < ar->num_rx_chains && rate_cap_rx_chainmask & BIT(i))
8514 			rxmcs_map |= IEEE80211_VHT_MCS_SUPPORT_0_9 << (i * 2);
8515 		else
8516 			rxmcs_map |= IEEE80211_VHT_MCS_NOT_SUPPORTED << (i * 2);
8517 	}
8518 
8519 	if (rate_cap_tx_chainmask <= 1)
8520 		vht_cap.cap &= ~IEEE80211_VHT_CAP_TXSTBC;
8521 
8522 	vht_cap.vht_mcs.rx_mcs_map = cpu_to_le16(rxmcs_map);
8523 	vht_cap.vht_mcs.tx_mcs_map = cpu_to_le16(txmcs_map);
8524 
8525 	/* Check if the HW supports 1:1 NSS ratio and reset
8526 	 * EXT NSS BW Support field to 0 to indicate 1:1 ratio
8527 	 */
8528 	if (ar->pdev->cap.nss_ratio_info == WMI_NSS_RATIO_1_NSS)
8529 		vht_cap.cap &= ~IEEE80211_VHT_CAP_EXT_NSS_BW_MASK;
8530 
8531 	return vht_cap;
8532 }
8533 
8534 static void ath12k_mac_setup_ht_vht_cap(struct ath12k *ar,
8535 					struct ath12k_pdev_cap *cap,
8536 					u32 *ht_cap_info)
8537 {
8538 	struct ieee80211_supported_band *band;
8539 	u32 rate_cap_tx_chainmask;
8540 	u32 rate_cap_rx_chainmask;
8541 	u32 ht_cap;
8542 
8543 	rate_cap_tx_chainmask = ar->cfg_tx_chainmask >> cap->tx_chain_mask_shift;
8544 	rate_cap_rx_chainmask = ar->cfg_rx_chainmask >> cap->rx_chain_mask_shift;
8545 
8546 	if (cap->supported_bands & WMI_HOST_WLAN_2GHZ_CAP) {
8547 		band = &ar->mac.sbands[NL80211_BAND_2GHZ];
8548 		ht_cap = cap->band[NL80211_BAND_2GHZ].ht_cap_info;
8549 		if (ht_cap_info)
8550 			*ht_cap_info = ht_cap;
8551 		band->ht_cap = ath12k_create_ht_cap(ar, ht_cap,
8552 						    rate_cap_rx_chainmask);
8553 	}
8554 
8555 	if (cap->supported_bands & WMI_HOST_WLAN_5GHZ_CAP &&
8556 	    (ar->ab->hw_params->single_pdev_only ||
8557 	     !ar->supports_6ghz)) {
8558 		band = &ar->mac.sbands[NL80211_BAND_5GHZ];
8559 		ht_cap = cap->band[NL80211_BAND_5GHZ].ht_cap_info;
8560 		if (ht_cap_info)
8561 			*ht_cap_info = ht_cap;
8562 		band->ht_cap = ath12k_create_ht_cap(ar, ht_cap,
8563 						    rate_cap_rx_chainmask);
8564 		band->vht_cap = ath12k_create_vht_cap(ar, rate_cap_tx_chainmask,
8565 						      rate_cap_rx_chainmask);
8566 	}
8567 }
8568 
8569 static int ath12k_check_chain_mask(struct ath12k *ar, u32 ant, bool is_tx_ant)
8570 {
8571 	/* TODO: Check the request chainmask against the supported
8572 	 * chainmask table which is advertised in extented_service_ready event
8573 	 */
8574 
8575 	return 0;
8576 }
8577 
8578 static void ath12k_gen_ppe_thresh(struct ath12k_wmi_ppe_threshold_arg *fw_ppet,
8579 				  u8 *he_ppet)
8580 {
8581 	int nss, ru;
8582 	u8 bit = 7;
8583 
8584 	he_ppet[0] = fw_ppet->numss_m1 & IEEE80211_PPE_THRES_NSS_MASK;
8585 	he_ppet[0] |= (fw_ppet->ru_bit_mask <<
8586 		       IEEE80211_PPE_THRES_RU_INDEX_BITMASK_POS) &
8587 		      IEEE80211_PPE_THRES_RU_INDEX_BITMASK_MASK;
8588 	for (nss = 0; nss <= fw_ppet->numss_m1; nss++) {
8589 		for (ru = 0; ru < 4; ru++) {
8590 			u8 val;
8591 			int i;
8592 
8593 			if ((fw_ppet->ru_bit_mask & BIT(ru)) == 0)
8594 				continue;
8595 			val = (fw_ppet->ppet16_ppet8_ru3_ru0[nss] >> (ru * 6)) &
8596 			       0x3f;
8597 			val = ((val >> 3) & 0x7) | ((val & 0x7) << 3);
8598 			for (i = 5; i >= 0; i--) {
8599 				he_ppet[bit / 8] |=
8600 					((val >> i) & 0x1) << ((bit % 8));
8601 				bit++;
8602 			}
8603 		}
8604 	}
8605 }
8606 
8607 static void
8608 ath12k_mac_filter_he_cap_mesh(struct ieee80211_he_cap_elem *he_cap_elem)
8609 {
8610 	u8 m;
8611 
8612 	m = IEEE80211_HE_MAC_CAP0_TWT_RES |
8613 	    IEEE80211_HE_MAC_CAP0_TWT_REQ;
8614 	he_cap_elem->mac_cap_info[0] &= ~m;
8615 
8616 	m = IEEE80211_HE_MAC_CAP2_TRS |
8617 	    IEEE80211_HE_MAC_CAP2_BCAST_TWT |
8618 	    IEEE80211_HE_MAC_CAP2_MU_CASCADING;
8619 	he_cap_elem->mac_cap_info[2] &= ~m;
8620 
8621 	m = IEEE80211_HE_MAC_CAP3_FLEX_TWT_SCHED |
8622 	    IEEE80211_HE_MAC_CAP2_BCAST_TWT |
8623 	    IEEE80211_HE_MAC_CAP2_MU_CASCADING;
8624 	he_cap_elem->mac_cap_info[3] &= ~m;
8625 
8626 	m = IEEE80211_HE_MAC_CAP4_BSRP_BQRP_A_MPDU_AGG |
8627 	    IEEE80211_HE_MAC_CAP4_BQR;
8628 	he_cap_elem->mac_cap_info[4] &= ~m;
8629 
8630 	m = IEEE80211_HE_MAC_CAP5_SUBCHAN_SELECTIVE_TRANSMISSION |
8631 	    IEEE80211_HE_MAC_CAP5_UL_2x996_TONE_RU |
8632 	    IEEE80211_HE_MAC_CAP5_PUNCTURED_SOUNDING |
8633 	    IEEE80211_HE_MAC_CAP5_HT_VHT_TRIG_FRAME_RX;
8634 	he_cap_elem->mac_cap_info[5] &= ~m;
8635 
8636 	m = IEEE80211_HE_PHY_CAP2_UL_MU_FULL_MU_MIMO |
8637 	    IEEE80211_HE_PHY_CAP2_UL_MU_PARTIAL_MU_MIMO;
8638 	he_cap_elem->phy_cap_info[2] &= ~m;
8639 
8640 	m = IEEE80211_HE_PHY_CAP3_RX_PARTIAL_BW_SU_IN_20MHZ_MU |
8641 	    IEEE80211_HE_PHY_CAP3_DCM_MAX_CONST_TX_MASK |
8642 	    IEEE80211_HE_PHY_CAP3_DCM_MAX_CONST_RX_MASK;
8643 	he_cap_elem->phy_cap_info[3] &= ~m;
8644 
8645 	m = IEEE80211_HE_PHY_CAP4_MU_BEAMFORMER;
8646 	he_cap_elem->phy_cap_info[4] &= ~m;
8647 
8648 	m = IEEE80211_HE_PHY_CAP5_NG16_MU_FEEDBACK;
8649 	he_cap_elem->phy_cap_info[5] &= ~m;
8650 
8651 	m = IEEE80211_HE_PHY_CAP6_CODEBOOK_SIZE_75_MU |
8652 	    IEEE80211_HE_PHY_CAP6_TRIG_MU_BEAMFORMING_PARTIAL_BW_FB |
8653 	    IEEE80211_HE_PHY_CAP6_TRIG_CQI_FB |
8654 	    IEEE80211_HE_PHY_CAP6_PARTIAL_BANDWIDTH_DL_MUMIMO;
8655 	he_cap_elem->phy_cap_info[6] &= ~m;
8656 
8657 	m = IEEE80211_HE_PHY_CAP7_PSR_BASED_SR |
8658 	    IEEE80211_HE_PHY_CAP7_POWER_BOOST_FACTOR_SUPP |
8659 	    IEEE80211_HE_PHY_CAP7_STBC_TX_ABOVE_80MHZ |
8660 	    IEEE80211_HE_PHY_CAP7_STBC_RX_ABOVE_80MHZ;
8661 	he_cap_elem->phy_cap_info[7] &= ~m;
8662 
8663 	m = IEEE80211_HE_PHY_CAP8_HE_ER_SU_PPDU_4XLTF_AND_08_US_GI |
8664 	    IEEE80211_HE_PHY_CAP8_20MHZ_IN_40MHZ_HE_PPDU_IN_2G |
8665 	    IEEE80211_HE_PHY_CAP8_20MHZ_IN_160MHZ_HE_PPDU |
8666 	    IEEE80211_HE_PHY_CAP8_80MHZ_IN_160MHZ_HE_PPDU;
8667 	he_cap_elem->phy_cap_info[8] &= ~m;
8668 
8669 	m = IEEE80211_HE_PHY_CAP9_LONGER_THAN_16_SIGB_OFDM_SYM |
8670 	    IEEE80211_HE_PHY_CAP9_NON_TRIGGERED_CQI_FEEDBACK |
8671 	    IEEE80211_HE_PHY_CAP9_RX_1024_QAM_LESS_THAN_242_TONE_RU |
8672 	    IEEE80211_HE_PHY_CAP9_TX_1024_QAM_LESS_THAN_242_TONE_RU |
8673 	    IEEE80211_HE_PHY_CAP9_RX_FULL_BW_SU_USING_MU_WITH_COMP_SIGB |
8674 	    IEEE80211_HE_PHY_CAP9_RX_FULL_BW_SU_USING_MU_WITH_NON_COMP_SIGB;
8675 	he_cap_elem->phy_cap_info[9] &= ~m;
8676 }
8677 
8678 static __le16 ath12k_mac_setup_he_6ghz_cap(struct ath12k_pdev_cap *pcap,
8679 					   struct ath12k_band_cap *bcap)
8680 {
8681 	u8 val;
8682 
8683 	bcap->he_6ghz_capa = IEEE80211_HT_MPDU_DENSITY_NONE;
8684 	if (bcap->ht_cap_info & WMI_HT_CAP_DYNAMIC_SMPS)
8685 		bcap->he_6ghz_capa |=
8686 			u32_encode_bits(WLAN_HT_CAP_SM_PS_DYNAMIC,
8687 					IEEE80211_HE_6GHZ_CAP_SM_PS);
8688 	else
8689 		bcap->he_6ghz_capa |=
8690 			u32_encode_bits(WLAN_HT_CAP_SM_PS_DISABLED,
8691 					IEEE80211_HE_6GHZ_CAP_SM_PS);
8692 	val = u32_get_bits(pcap->vht_cap,
8693 			   IEEE80211_VHT_CAP_MAX_A_MPDU_LENGTH_EXPONENT_MASK);
8694 	bcap->he_6ghz_capa |=
8695 		u32_encode_bits(val, IEEE80211_HE_6GHZ_CAP_MAX_AMPDU_LEN_EXP);
8696 	val = u32_get_bits(pcap->vht_cap,
8697 			   IEEE80211_VHT_CAP_MAX_MPDU_MASK);
8698 	bcap->he_6ghz_capa |=
8699 		u32_encode_bits(val, IEEE80211_HE_6GHZ_CAP_MAX_MPDU_LEN);
8700 	if (pcap->vht_cap & IEEE80211_VHT_CAP_RX_ANTENNA_PATTERN)
8701 		bcap->he_6ghz_capa |= IEEE80211_HE_6GHZ_CAP_RX_ANTPAT_CONS;
8702 	if (pcap->vht_cap & IEEE80211_VHT_CAP_TX_ANTENNA_PATTERN)
8703 		bcap->he_6ghz_capa |= IEEE80211_HE_6GHZ_CAP_TX_ANTPAT_CONS;
8704 
8705 	return cpu_to_le16(bcap->he_6ghz_capa);
8706 }
8707 
8708 static void ath12k_mac_set_hemcsmap(struct ath12k *ar,
8709 				    struct ath12k_pdev_cap *cap,
8710 				    struct ieee80211_sta_he_cap *he_cap)
8711 {
8712 	struct ieee80211_he_mcs_nss_supp *mcs_nss = &he_cap->he_mcs_nss_supp;
8713 	u8 maxtxnss_160 = ath12k_get_nss_160mhz(ar, ar->num_tx_chains);
8714 	u8 maxrxnss_160 = ath12k_get_nss_160mhz(ar, ar->num_rx_chains);
8715 	u16 txmcs_map_160 = 0, rxmcs_map_160 = 0;
8716 	u16 txmcs_map = 0, rxmcs_map = 0;
8717 	u32 i;
8718 
8719 	for (i = 0; i < 8; i++) {
8720 		if (i < ar->num_tx_chains &&
8721 		    (ar->cfg_tx_chainmask >> cap->tx_chain_mask_shift) & BIT(i))
8722 			txmcs_map |= IEEE80211_HE_MCS_SUPPORT_0_11 << (i * 2);
8723 		else
8724 			txmcs_map |= IEEE80211_HE_MCS_NOT_SUPPORTED << (i * 2);
8725 
8726 		if (i < ar->num_rx_chains &&
8727 		    (ar->cfg_rx_chainmask >> cap->tx_chain_mask_shift) & BIT(i))
8728 			rxmcs_map |= IEEE80211_HE_MCS_SUPPORT_0_11 << (i * 2);
8729 		else
8730 			rxmcs_map |= IEEE80211_HE_MCS_NOT_SUPPORTED << (i * 2);
8731 
8732 		if (i < maxtxnss_160 &&
8733 		    (ar->cfg_tx_chainmask >> cap->tx_chain_mask_shift) & BIT(i))
8734 			txmcs_map_160 |= IEEE80211_HE_MCS_SUPPORT_0_11 << (i * 2);
8735 		else
8736 			txmcs_map_160 |= IEEE80211_HE_MCS_NOT_SUPPORTED << (i * 2);
8737 
8738 		if (i < maxrxnss_160 &&
8739 		    (ar->cfg_tx_chainmask >> cap->tx_chain_mask_shift) & BIT(i))
8740 			rxmcs_map_160 |= IEEE80211_HE_MCS_SUPPORT_0_11 << (i * 2);
8741 		else
8742 			rxmcs_map_160 |= IEEE80211_HE_MCS_NOT_SUPPORTED << (i * 2);
8743 	}
8744 
8745 	mcs_nss->rx_mcs_80 = cpu_to_le16(rxmcs_map & 0xffff);
8746 	mcs_nss->tx_mcs_80 = cpu_to_le16(txmcs_map & 0xffff);
8747 	mcs_nss->rx_mcs_160 = cpu_to_le16(rxmcs_map_160 & 0xffff);
8748 	mcs_nss->tx_mcs_160 = cpu_to_le16(txmcs_map_160 & 0xffff);
8749 }
8750 
8751 static void ath12k_mac_copy_he_cap(struct ath12k *ar,
8752 				   struct ath12k_band_cap *band_cap,
8753 				   int iftype, u8 num_tx_chains,
8754 				   struct ieee80211_sta_he_cap *he_cap)
8755 {
8756 	struct ieee80211_he_cap_elem *he_cap_elem = &he_cap->he_cap_elem;
8757 
8758 	he_cap->has_he = true;
8759 	memcpy(he_cap_elem->mac_cap_info, band_cap->he_cap_info,
8760 	       sizeof(he_cap_elem->mac_cap_info));
8761 	memcpy(he_cap_elem->phy_cap_info, band_cap->he_cap_phy_info,
8762 	       sizeof(he_cap_elem->phy_cap_info));
8763 
8764 	he_cap_elem->mac_cap_info[1] &=
8765 		IEEE80211_HE_MAC_CAP1_TF_MAC_PAD_DUR_MASK;
8766 	he_cap_elem->phy_cap_info[0] &=
8767 		IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_40MHZ_IN_2G |
8768 		IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_40MHZ_80MHZ_IN_5G |
8769 		IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_160MHZ_IN_5G;
8770 	/* 80PLUS80 is not supported */
8771 	he_cap_elem->phy_cap_info[0] &=
8772 		~IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_80PLUS80_MHZ_IN_5G;
8773 	he_cap_elem->phy_cap_info[5] &=
8774 		~IEEE80211_HE_PHY_CAP5_BEAMFORMEE_NUM_SND_DIM_UNDER_80MHZ_MASK;
8775 	he_cap_elem->phy_cap_info[5] |= num_tx_chains - 1;
8776 
8777 	switch (iftype) {
8778 	case NL80211_IFTYPE_AP:
8779 		he_cap_elem->mac_cap_info[2] &=
8780 			~IEEE80211_HE_MAC_CAP2_BCAST_TWT;
8781 		he_cap_elem->phy_cap_info[3] &=
8782 			~IEEE80211_HE_PHY_CAP3_DCM_MAX_CONST_TX_MASK;
8783 		he_cap_elem->phy_cap_info[9] |=
8784 			IEEE80211_HE_PHY_CAP9_RX_1024_QAM_LESS_THAN_242_TONE_RU;
8785 		break;
8786 	case NL80211_IFTYPE_STATION:
8787 		he_cap_elem->mac_cap_info[0] &= ~IEEE80211_HE_MAC_CAP0_TWT_RES;
8788 		he_cap_elem->mac_cap_info[0] |= IEEE80211_HE_MAC_CAP0_TWT_REQ;
8789 		he_cap_elem->phy_cap_info[9] |=
8790 			IEEE80211_HE_PHY_CAP9_TX_1024_QAM_LESS_THAN_242_TONE_RU;
8791 		break;
8792 	case NL80211_IFTYPE_MESH_POINT:
8793 		ath12k_mac_filter_he_cap_mesh(he_cap_elem);
8794 		break;
8795 	}
8796 
8797 	ath12k_mac_set_hemcsmap(ar, &ar->pdev->cap, he_cap);
8798 	memset(he_cap->ppe_thres, 0, sizeof(he_cap->ppe_thres));
8799 	if (he_cap_elem->phy_cap_info[6] &
8800 	    IEEE80211_HE_PHY_CAP6_PPE_THRESHOLD_PRESENT)
8801 		ath12k_gen_ppe_thresh(&band_cap->he_ppet, he_cap->ppe_thres);
8802 }
8803 
8804 static void
8805 ath12k_mac_copy_eht_mcs_nss(struct ath12k_band_cap *band_cap,
8806 			    struct ieee80211_eht_mcs_nss_supp *mcs_nss,
8807 			    const struct ieee80211_he_cap_elem *he_cap,
8808 			    const struct ieee80211_eht_cap_elem_fixed *eht_cap)
8809 {
8810 	if ((he_cap->phy_cap_info[0] &
8811 	     (IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_40MHZ_IN_2G |
8812 	      IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_40MHZ_80MHZ_IN_5G |
8813 	      IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_160MHZ_IN_5G |
8814 	      IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_80PLUS80_MHZ_IN_5G)) == 0)
8815 		memcpy(&mcs_nss->only_20mhz, &band_cap->eht_mcs_20_only,
8816 		       sizeof(struct ieee80211_eht_mcs_nss_supp_20mhz_only));
8817 
8818 	if (he_cap->phy_cap_info[0] &
8819 	    (IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_40MHZ_IN_2G |
8820 	     IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_40MHZ_80MHZ_IN_5G))
8821 		memcpy(&mcs_nss->bw._80, &band_cap->eht_mcs_80,
8822 		       sizeof(struct ieee80211_eht_mcs_nss_supp_bw));
8823 
8824 	if (he_cap->phy_cap_info[0] &
8825 	    IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_160MHZ_IN_5G)
8826 		memcpy(&mcs_nss->bw._160, &band_cap->eht_mcs_160,
8827 		       sizeof(struct ieee80211_eht_mcs_nss_supp_bw));
8828 
8829 	if (eht_cap->phy_cap_info[0] & IEEE80211_EHT_PHY_CAP0_320MHZ_IN_6GHZ)
8830 		memcpy(&mcs_nss->bw._320, &band_cap->eht_mcs_320,
8831 		       sizeof(struct ieee80211_eht_mcs_nss_supp_bw));
8832 }
8833 
8834 static void ath12k_mac_copy_eht_ppe_thresh(struct ath12k_wmi_ppe_threshold_arg *fw_ppet,
8835 					   struct ieee80211_sta_eht_cap *cap)
8836 {
8837 	u16 bit = IEEE80211_EHT_PPE_THRES_INFO_HEADER_SIZE;
8838 	u8 i, nss, ru, ppet_bit_len_per_ru = IEEE80211_EHT_PPE_THRES_INFO_PPET_SIZE * 2;
8839 
8840 	u8p_replace_bits(&cap->eht_ppe_thres[0], fw_ppet->numss_m1,
8841 			 IEEE80211_EHT_PPE_THRES_NSS_MASK);
8842 
8843 	u16p_replace_bits((u16 *)&cap->eht_ppe_thres[0], fw_ppet->ru_bit_mask,
8844 			  IEEE80211_EHT_PPE_THRES_RU_INDEX_BITMASK_MASK);
8845 
8846 	for (nss = 0; nss <= fw_ppet->numss_m1; nss++) {
8847 		for (ru = 0;
8848 		     ru < hweight16(IEEE80211_EHT_PPE_THRES_RU_INDEX_BITMASK_MASK);
8849 		     ru++) {
8850 			u32 val = 0;
8851 
8852 			if ((fw_ppet->ru_bit_mask & BIT(ru)) == 0)
8853 				continue;
8854 
8855 			u32p_replace_bits(&val, fw_ppet->ppet16_ppet8_ru3_ru0[nss] >>
8856 						(ru * ppet_bit_len_per_ru),
8857 					  GENMASK(ppet_bit_len_per_ru - 1, 0));
8858 
8859 			for (i = 0; i < ppet_bit_len_per_ru; i++) {
8860 				cap->eht_ppe_thres[bit / 8] |=
8861 					(((val >> i) & 0x1) << ((bit % 8)));
8862 				bit++;
8863 			}
8864 		}
8865 	}
8866 }
8867 
8868 static void
8869 ath12k_mac_filter_eht_cap_mesh(struct ieee80211_eht_cap_elem_fixed
8870 			       *eht_cap_elem)
8871 {
8872 	u8 m;
8873 
8874 	m = IEEE80211_EHT_MAC_CAP0_EPCS_PRIO_ACCESS;
8875 	eht_cap_elem->mac_cap_info[0] &= ~m;
8876 
8877 	m = IEEE80211_EHT_PHY_CAP0_PARTIAL_BW_UL_MU_MIMO;
8878 	eht_cap_elem->phy_cap_info[0] &= ~m;
8879 
8880 	m = IEEE80211_EHT_PHY_CAP3_NG_16_MU_FEEDBACK |
8881 	    IEEE80211_EHT_PHY_CAP3_CODEBOOK_7_5_MU_FDBK |
8882 	    IEEE80211_EHT_PHY_CAP3_TRIG_MU_BF_PART_BW_FDBK |
8883 	    IEEE80211_EHT_PHY_CAP3_TRIG_CQI_FDBK;
8884 	eht_cap_elem->phy_cap_info[3] &= ~m;
8885 
8886 	m = IEEE80211_EHT_PHY_CAP4_PART_BW_DL_MU_MIMO |
8887 	    IEEE80211_EHT_PHY_CAP4_PSR_SR_SUPP |
8888 	    IEEE80211_EHT_PHY_CAP4_POWER_BOOST_FACT_SUPP |
8889 	    IEEE80211_EHT_PHY_CAP4_EHT_MU_PPDU_4_EHT_LTF_08_GI;
8890 	eht_cap_elem->phy_cap_info[4] &= ~m;
8891 
8892 	m = IEEE80211_EHT_PHY_CAP5_NON_TRIG_CQI_FEEDBACK |
8893 	    IEEE80211_EHT_PHY_CAP5_TX_LESS_242_TONE_RU_SUPP |
8894 	    IEEE80211_EHT_PHY_CAP5_RX_LESS_242_TONE_RU_SUPP |
8895 	    IEEE80211_EHT_PHY_CAP5_MAX_NUM_SUPP_EHT_LTF_MASK;
8896 	eht_cap_elem->phy_cap_info[5] &= ~m;
8897 
8898 	m = IEEE80211_EHT_PHY_CAP6_MAX_NUM_SUPP_EHT_LTF_MASK;
8899 	eht_cap_elem->phy_cap_info[6] &= ~m;
8900 
8901 	m = IEEE80211_EHT_PHY_CAP7_NON_OFDMA_UL_MU_MIMO_80MHZ |
8902 	    IEEE80211_EHT_PHY_CAP7_NON_OFDMA_UL_MU_MIMO_160MHZ |
8903 	    IEEE80211_EHT_PHY_CAP7_NON_OFDMA_UL_MU_MIMO_320MHZ |
8904 	    IEEE80211_EHT_PHY_CAP7_MU_BEAMFORMER_80MHZ |
8905 	    IEEE80211_EHT_PHY_CAP7_MU_BEAMFORMER_160MHZ |
8906 	    IEEE80211_EHT_PHY_CAP7_MU_BEAMFORMER_320MHZ;
8907 	eht_cap_elem->phy_cap_info[7] &= ~m;
8908 }
8909 
8910 static void ath12k_mac_copy_eht_cap(struct ath12k *ar,
8911 				    struct ath12k_band_cap *band_cap,
8912 				    struct ieee80211_he_cap_elem *he_cap_elem,
8913 				    int iftype,
8914 				    struct ieee80211_sta_eht_cap *eht_cap)
8915 {
8916 	struct ieee80211_eht_cap_elem_fixed *eht_cap_elem = &eht_cap->eht_cap_elem;
8917 
8918 	memset(eht_cap, 0, sizeof(struct ieee80211_sta_eht_cap));
8919 
8920 	if (!(test_bit(WMI_TLV_SERVICE_11BE, ar->ab->wmi_ab.svc_map)) ||
8921 	    ath12k_acpi_get_disable_11be(ar->ab))
8922 		return;
8923 
8924 	eht_cap->has_eht = true;
8925 	memcpy(eht_cap_elem->mac_cap_info, band_cap->eht_cap_mac_info,
8926 	       sizeof(eht_cap_elem->mac_cap_info));
8927 	memcpy(eht_cap_elem->phy_cap_info, band_cap->eht_cap_phy_info,
8928 	       sizeof(eht_cap_elem->phy_cap_info));
8929 
8930 	switch (iftype) {
8931 	case NL80211_IFTYPE_AP:
8932 		eht_cap_elem->phy_cap_info[0] &=
8933 			~IEEE80211_EHT_PHY_CAP0_242_TONE_RU_GT20MHZ;
8934 		eht_cap_elem->phy_cap_info[4] &=
8935 			~IEEE80211_EHT_PHY_CAP4_PART_BW_DL_MU_MIMO;
8936 		eht_cap_elem->phy_cap_info[5] &=
8937 			~IEEE80211_EHT_PHY_CAP5_TX_LESS_242_TONE_RU_SUPP;
8938 		break;
8939 	case NL80211_IFTYPE_STATION:
8940 		eht_cap_elem->phy_cap_info[7] &=
8941 			~(IEEE80211_EHT_PHY_CAP7_NON_OFDMA_UL_MU_MIMO_80MHZ |
8942 			  IEEE80211_EHT_PHY_CAP7_NON_OFDMA_UL_MU_MIMO_160MHZ |
8943 			  IEEE80211_EHT_PHY_CAP7_NON_OFDMA_UL_MU_MIMO_320MHZ);
8944 		eht_cap_elem->phy_cap_info[7] &=
8945 			~(IEEE80211_EHT_PHY_CAP7_MU_BEAMFORMER_80MHZ |
8946 			  IEEE80211_EHT_PHY_CAP7_MU_BEAMFORMER_160MHZ |
8947 			  IEEE80211_EHT_PHY_CAP7_MU_BEAMFORMER_320MHZ);
8948 		break;
8949 	case NL80211_IFTYPE_MESH_POINT:
8950 		ath12k_mac_filter_eht_cap_mesh(eht_cap_elem);
8951 		break;
8952 	default:
8953 		break;
8954 	}
8955 
8956 	ath12k_mac_copy_eht_mcs_nss(band_cap, &eht_cap->eht_mcs_nss_supp,
8957 				    he_cap_elem, eht_cap_elem);
8958 
8959 	if (eht_cap_elem->phy_cap_info[5] &
8960 	    IEEE80211_EHT_PHY_CAP5_PPE_THRESHOLD_PRESENT)
8961 		ath12k_mac_copy_eht_ppe_thresh(&band_cap->eht_ppet, eht_cap);
8962 }
8963 
8964 static int ath12k_mac_copy_sband_iftype_data(struct ath12k *ar,
8965 					     struct ath12k_pdev_cap *cap,
8966 					     struct ieee80211_sband_iftype_data *data,
8967 					     int band)
8968 {
8969 	struct ath12k_band_cap *band_cap = &cap->band[band];
8970 	int i, idx = 0;
8971 
8972 	for (i = 0; i < NUM_NL80211_IFTYPES; i++) {
8973 		struct ieee80211_sta_he_cap *he_cap = &data[idx].he_cap;
8974 
8975 		switch (i) {
8976 		case NL80211_IFTYPE_STATION:
8977 		case NL80211_IFTYPE_AP:
8978 		case NL80211_IFTYPE_MESH_POINT:
8979 			break;
8980 
8981 		default:
8982 			continue;
8983 		}
8984 
8985 		data[idx].types_mask = BIT(i);
8986 
8987 		ath12k_mac_copy_he_cap(ar, band_cap, i, ar->num_tx_chains, he_cap);
8988 		if (band == NL80211_BAND_6GHZ) {
8989 			data[idx].he_6ghz_capa.capa =
8990 				ath12k_mac_setup_he_6ghz_cap(cap, band_cap);
8991 		}
8992 		ath12k_mac_copy_eht_cap(ar, band_cap, &he_cap->he_cap_elem, i,
8993 					&data[idx].eht_cap);
8994 		idx++;
8995 	}
8996 
8997 	return idx;
8998 }
8999 
9000 static void ath12k_mac_setup_sband_iftype_data(struct ath12k *ar,
9001 					       struct ath12k_pdev_cap *cap)
9002 {
9003 	struct ieee80211_supported_band *sband;
9004 	enum nl80211_band band;
9005 	int count;
9006 
9007 	if (cap->supported_bands & WMI_HOST_WLAN_2GHZ_CAP) {
9008 		band = NL80211_BAND_2GHZ;
9009 		count = ath12k_mac_copy_sband_iftype_data(ar, cap,
9010 							  ar->mac.iftype[band],
9011 							  band);
9012 		sband = &ar->mac.sbands[band];
9013 		_ieee80211_set_sband_iftype_data(sband, ar->mac.iftype[band],
9014 						 count);
9015 	}
9016 
9017 	if (cap->supported_bands & WMI_HOST_WLAN_5GHZ_CAP) {
9018 		band = NL80211_BAND_5GHZ;
9019 		count = ath12k_mac_copy_sband_iftype_data(ar, cap,
9020 							  ar->mac.iftype[band],
9021 							  band);
9022 		sband = &ar->mac.sbands[band];
9023 		_ieee80211_set_sband_iftype_data(sband, ar->mac.iftype[band],
9024 						 count);
9025 	}
9026 
9027 	if (cap->supported_bands & WMI_HOST_WLAN_5GHZ_CAP &&
9028 	    ar->supports_6ghz) {
9029 		band = NL80211_BAND_6GHZ;
9030 		count = ath12k_mac_copy_sband_iftype_data(ar, cap,
9031 							  ar->mac.iftype[band],
9032 							  band);
9033 		sband = &ar->mac.sbands[band];
9034 		_ieee80211_set_sband_iftype_data(sband, ar->mac.iftype[band],
9035 						 count);
9036 	}
9037 }
9038 
9039 static int __ath12k_set_antenna(struct ath12k *ar, u32 tx_ant, u32 rx_ant)
9040 {
9041 	struct ath12k_hw *ah = ath12k_ar_to_ah(ar);
9042 	int ret;
9043 
9044 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
9045 
9046 	if (ath12k_check_chain_mask(ar, tx_ant, true))
9047 		return -EINVAL;
9048 
9049 	if (ath12k_check_chain_mask(ar, rx_ant, false))
9050 		return -EINVAL;
9051 
9052 	/* Since we advertised the max cap of all radios combined during wiphy
9053 	 * registration, ensure we don't set the antenna config higher than the
9054 	 * limits
9055 	 */
9056 	tx_ant = min_t(u32, tx_ant, ar->pdev->cap.tx_chain_mask);
9057 	rx_ant = min_t(u32, rx_ant, ar->pdev->cap.rx_chain_mask);
9058 
9059 	ar->cfg_tx_chainmask = tx_ant;
9060 	ar->cfg_rx_chainmask = rx_ant;
9061 
9062 	if (ah->state != ATH12K_HW_STATE_ON &&
9063 	    ah->state != ATH12K_HW_STATE_RESTARTED)
9064 		return 0;
9065 
9066 	ret = ath12k_wmi_pdev_set_param(ar, WMI_PDEV_PARAM_TX_CHAIN_MASK,
9067 					tx_ant, ar->pdev->pdev_id);
9068 	if (ret) {
9069 		ath12k_warn(ar->ab, "failed to set tx-chainmask: %d, req 0x%x\n",
9070 			    ret, tx_ant);
9071 		return ret;
9072 	}
9073 
9074 	ar->num_tx_chains = hweight32(tx_ant);
9075 
9076 	ret = ath12k_wmi_pdev_set_param(ar, WMI_PDEV_PARAM_RX_CHAIN_MASK,
9077 					rx_ant, ar->pdev->pdev_id);
9078 	if (ret) {
9079 		ath12k_warn(ar->ab, "failed to set rx-chainmask: %d, req 0x%x\n",
9080 			    ret, rx_ant);
9081 		return ret;
9082 	}
9083 
9084 	ar->num_rx_chains = hweight32(rx_ant);
9085 
9086 	/* Reload HT/VHT/HE capability */
9087 	ath12k_mac_setup_ht_vht_cap(ar, &ar->pdev->cap, NULL);
9088 	ath12k_mac_setup_sband_iftype_data(ar, &ar->pdev->cap);
9089 
9090 	return 0;
9091 }
9092 
9093 static void ath12k_mgmt_over_wmi_tx_drop(struct ath12k *ar, struct sk_buff *skb)
9094 {
9095 	int num_mgmt;
9096 
9097 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
9098 
9099 	ieee80211_free_txskb(ath12k_ar_to_hw(ar), skb);
9100 
9101 	num_mgmt = atomic_dec_if_positive(&ar->num_pending_mgmt_tx);
9102 
9103 	if (num_mgmt < 0)
9104 		WARN_ON_ONCE(1);
9105 
9106 	if (!num_mgmt)
9107 		wake_up(&ar->txmgmt_empty_waitq);
9108 }
9109 
9110 static void ath12k_mac_tx_mgmt_free(struct ath12k *ar, int buf_id)
9111 {
9112 	struct sk_buff *msdu;
9113 	struct ieee80211_tx_info *info;
9114 
9115 	spin_lock_bh(&ar->txmgmt_idr_lock);
9116 	msdu = idr_remove(&ar->txmgmt_idr, buf_id);
9117 	spin_unlock_bh(&ar->txmgmt_idr_lock);
9118 
9119 	if (!msdu)
9120 		return;
9121 
9122 	dma_unmap_single(ar->ab->dev, ATH12K_SKB_CB(msdu)->paddr, msdu->len,
9123 			 DMA_TO_DEVICE);
9124 
9125 	info = IEEE80211_SKB_CB(msdu);
9126 	memset(&info->status, 0, sizeof(info->status));
9127 
9128 	ath12k_mgmt_over_wmi_tx_drop(ar, msdu);
9129 }
9130 
9131 int ath12k_mac_tx_mgmt_pending_free(int buf_id, void *skb, void *ctx)
9132 {
9133 	struct ath12k *ar = ctx;
9134 
9135 	ath12k_mac_tx_mgmt_free(ar, buf_id);
9136 
9137 	return 0;
9138 }
9139 
9140 static int ath12k_mac_vif_txmgmt_idr_remove(int buf_id, void *skb, void *ctx)
9141 {
9142 	struct ieee80211_vif *vif = ctx;
9143 	struct ath12k_skb_cb *skb_cb = ATH12K_SKB_CB(skb);
9144 	struct ath12k *ar = skb_cb->ar;
9145 
9146 	if (skb_cb->vif == vif)
9147 		ath12k_mac_tx_mgmt_free(ar, buf_id);
9148 
9149 	return 0;
9150 }
9151 
9152 static int ath12k_mac_mgmt_tx_wmi(struct ath12k *ar, struct ath12k_link_vif *arvif,
9153 				  struct sk_buff *skb)
9154 {
9155 	struct ath12k_base *ab = ar->ab;
9156 	struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
9157 	struct ath12k_skb_cb *skb_cb = ATH12K_SKB_CB(skb);
9158 	struct ieee80211_tx_info *info;
9159 	enum hal_encrypt_type enctype;
9160 	unsigned int mic_len;
9161 	dma_addr_t paddr;
9162 	int buf_id;
9163 	int ret;
9164 
9165 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
9166 
9167 	skb_cb->ar = ar;
9168 	spin_lock_bh(&ar->txmgmt_idr_lock);
9169 	buf_id = idr_alloc(&ar->txmgmt_idr, skb, 0,
9170 			   ATH12K_TX_MGMT_NUM_PENDING_MAX, GFP_ATOMIC);
9171 	spin_unlock_bh(&ar->txmgmt_idr_lock);
9172 	if (buf_id < 0)
9173 		return -ENOSPC;
9174 
9175 	info = IEEE80211_SKB_CB(skb);
9176 	if ((skb_cb->flags & ATH12K_SKB_CIPHER_SET) &&
9177 	    !(info->flags & IEEE80211_TX_CTL_HW_80211_ENCAP)) {
9178 		if ((ieee80211_is_action(hdr->frame_control) ||
9179 		     ieee80211_is_deauth(hdr->frame_control) ||
9180 		     ieee80211_is_disassoc(hdr->frame_control)) &&
9181 		     ieee80211_has_protected(hdr->frame_control)) {
9182 			enctype = ath12k_dp_tx_get_encrypt_type(skb_cb->cipher);
9183 			mic_len = ath12k_dp_rx_crypto_mic_len(ab->dp, enctype);
9184 			skb_put(skb, mic_len);
9185 		}
9186 	}
9187 
9188 	paddr = dma_map_single(ab->dev, skb->data, skb->len, DMA_TO_DEVICE);
9189 	if (dma_mapping_error(ab->dev, paddr)) {
9190 		ath12k_warn(ab, "failed to DMA map mgmt Tx buffer\n");
9191 		ret = -EIO;
9192 		goto err_free_idr;
9193 	}
9194 
9195 	skb_cb->paddr = paddr;
9196 
9197 	ret = ath12k_wmi_mgmt_send(arvif, buf_id, skb);
9198 	if (ret) {
9199 		ath12k_warn(ar->ab, "failed to send mgmt frame: %d\n", ret);
9200 		goto err_unmap_buf;
9201 	}
9202 
9203 	return 0;
9204 
9205 err_unmap_buf:
9206 	dma_unmap_single(ab->dev, skb_cb->paddr,
9207 			 skb->len, DMA_TO_DEVICE);
9208 err_free_idr:
9209 	spin_lock_bh(&ar->txmgmt_idr_lock);
9210 	idr_remove(&ar->txmgmt_idr, buf_id);
9211 	spin_unlock_bh(&ar->txmgmt_idr_lock);
9212 
9213 	return ret;
9214 }
9215 
9216 static void ath12k_mgmt_over_wmi_tx_purge(struct ath12k *ar)
9217 {
9218 	struct sk_buff *skb;
9219 
9220 	while ((skb = skb_dequeue(&ar->wmi_mgmt_tx_queue)) != NULL)
9221 		ath12k_mgmt_over_wmi_tx_drop(ar, skb);
9222 }
9223 
9224 static int ath12k_mac_mgmt_action_frame_fill_elem_data(struct ath12k_link_vif *arvif,
9225 						       struct sk_buff *skb)
9226 {
9227 	struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
9228 	u8 category, *buf, iv_len, action_code, dialog_token;
9229 	struct ieee80211_bss_conf *link_conf;
9230 	struct ieee80211_chanctx_conf *conf;
9231 	int cur_tx_power, max_tx_power;
9232 	struct ath12k *ar = arvif->ar;
9233 	struct ieee80211_hw *hw = ath12k_ar_to_hw(ar);
9234 	struct wiphy *wiphy = hw->wiphy;
9235 	struct ath12k_skb_cb *skb_cb;
9236 	struct ieee80211_mgmt *mgmt;
9237 	unsigned int remaining_len;
9238 	bool has_protected;
9239 
9240 	lockdep_assert_wiphy(wiphy);
9241 
9242 	/* make sure category field is present */
9243 	if (skb->len < IEEE80211_MIN_ACTION_SIZE(category))
9244 		return -EINVAL;
9245 
9246 	remaining_len = skb->len - IEEE80211_MIN_ACTION_SIZE(category);
9247 	has_protected = ieee80211_has_protected(hdr->frame_control);
9248 
9249 	/* In case of SW crypto and hdr protected (PMF), packet will already be encrypted,
9250 	 * we can't put in data in this case
9251 	 */
9252 	if (test_bit(ATH12K_FLAG_HW_CRYPTO_DISABLED, &ar->ab->dev_flags) &&
9253 	    has_protected)
9254 		return 0;
9255 
9256 	mgmt = (struct ieee80211_mgmt *)hdr;
9257 	buf = (u8 *)&mgmt->u.action;
9258 
9259 	/* FCTL_PROTECTED frame might have extra space added for HDR_LEN. Offset that
9260 	 * many bytes if it is there
9261 	 */
9262 	if (has_protected) {
9263 		skb_cb = ATH12K_SKB_CB(skb);
9264 
9265 		switch (skb_cb->cipher) {
9266 		/* Cipher suite having flag %IEEE80211_KEY_FLAG_GENERATE_IV_MGMT set in
9267 		 * key needs to be processed. See ath12k_install_key()
9268 		 */
9269 		case WLAN_CIPHER_SUITE_CCMP:
9270 		case WLAN_CIPHER_SUITE_CCMP_256:
9271 		case WLAN_CIPHER_SUITE_GCMP:
9272 		case WLAN_CIPHER_SUITE_GCMP_256:
9273 			iv_len = IEEE80211_CCMP_HDR_LEN;
9274 			break;
9275 		case WLAN_CIPHER_SUITE_TKIP:
9276 			iv_len = 0;
9277 			break;
9278 		default:
9279 			return -EINVAL;
9280 		}
9281 
9282 		if (remaining_len < iv_len)
9283 			return -EINVAL;
9284 
9285 		buf += iv_len;
9286 		remaining_len -= iv_len;
9287 	}
9288 
9289 	category = *buf++;
9290 	/* category code is already taken care in %IEEE80211_MIN_ACTION_SIZE hence
9291 	 * no need to adjust remaining_len
9292 	 */
9293 
9294 	switch (category) {
9295 	case WLAN_CATEGORY_RADIO_MEASUREMENT:
9296 		/* need action code and dialog token */
9297 		if (remaining_len < 2)
9298 			return -EINVAL;
9299 
9300 		/* Packet Format:
9301 		 *	Action Code | Dialog Token | Variable Len (based on Action Code)
9302 		 */
9303 		action_code = *buf++;
9304 		dialog_token = *buf++;
9305 		remaining_len -= 2;
9306 
9307 		link_conf = ath12k_mac_get_link_bss_conf(arvif);
9308 		if (!link_conf) {
9309 			ath12k_warn(ar->ab,
9310 				    "failed to get bss link conf for vdev %d in RM handling\n",
9311 				    arvif->vdev_id);
9312 			return -EINVAL;
9313 		}
9314 
9315 		conf = wiphy_dereference(wiphy, link_conf->chanctx_conf);
9316 		if (!conf)
9317 			return -ENOENT;
9318 
9319 		cur_tx_power = link_conf->txpower;
9320 		max_tx_power = min(conf->def.chan->max_reg_power,
9321 				   (int)ar->max_tx_power / 2);
9322 
9323 		ath12k_mac_op_get_txpower(hw, arvif->ahvif->vif, arvif->link_id,
9324 					  &cur_tx_power);
9325 
9326 		switch (action_code) {
9327 		case WLAN_RM_ACTION_LINK_MEASUREMENT_REQUEST:
9328 			/* need variable fields to be present in len */
9329 			if (remaining_len < 2)
9330 				return -EINVAL;
9331 
9332 			/* Variable length format as defined in IEEE 802.11-2024,
9333 			 * Figure 9-1187-Link Measurement Request frame Action field
9334 			 * format.
9335 			 *	Transmit Power | Max Tx Power
9336 			 * We fill both of these.
9337 			 */
9338 			*buf++ = cur_tx_power;
9339 			*buf = max_tx_power;
9340 
9341 			ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
9342 				   "RRM: Link Measurement Req dialog_token %u cur_tx_power %d max_tx_power %d\n",
9343 				   dialog_token, cur_tx_power, max_tx_power);
9344 			break;
9345 		case WLAN_RM_ACTION_LINK_MEASUREMENT_REPORT:
9346 			/* need variable fields to be present in len */
9347 			if (remaining_len < 3)
9348 				return -EINVAL;
9349 
9350 			/* Variable length format as defined in IEEE 802.11-2024,
9351 			 * Figure 9-1188-Link Measurement Report frame Action field format
9352 			 *	TPC Report | Variable Fields
9353 			 *
9354 			 * TPC Report Format:
9355 			 *	Element ID | Len | Tx Power | Link Margin
9356 			 *
9357 			 * We fill Tx power in the TPC Report (2nd index)
9358 			 */
9359 			buf[2] = cur_tx_power;
9360 
9361 			/* TODO: At present, Link margin data is not present so can't
9362 			 * really fill it now. Once it is available, it can be added
9363 			 * here
9364 			 */
9365 			ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
9366 				   "RRM: Link Measurement Report dialog_token %u cur_tx_power %d\n",
9367 				   dialog_token, cur_tx_power);
9368 			break;
9369 		default:
9370 			return -EINVAL;
9371 		}
9372 		break;
9373 	default:
9374 		/* nothing to fill */
9375 		return 0;
9376 	}
9377 
9378 	return 0;
9379 }
9380 
9381 static int ath12k_mac_mgmt_frame_fill_elem_data(struct ath12k_link_vif *arvif,
9382 						struct sk_buff *skb)
9383 {
9384 	struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
9385 
9386 	if (!ieee80211_is_action(hdr->frame_control))
9387 		return 0;
9388 
9389 	return ath12k_mac_mgmt_action_frame_fill_elem_data(arvif, skb);
9390 }
9391 
9392 static void ath12k_mgmt_over_wmi_tx_work(struct wiphy *wiphy, struct wiphy_work *work)
9393 {
9394 	struct ath12k *ar = container_of(work, struct ath12k, wmi_mgmt_tx_work);
9395 	struct ath12k_hw *ah = ar->ah;
9396 	struct ath12k_skb_cb *skb_cb;
9397 	struct ath12k_vif *ahvif;
9398 	struct ath12k_link_vif *arvif;
9399 	struct sk_buff *skb;
9400 	int ret;
9401 
9402 	lockdep_assert_wiphy(wiphy);
9403 
9404 	while ((skb = skb_dequeue(&ar->wmi_mgmt_tx_queue)) != NULL) {
9405 		skb_cb = ATH12K_SKB_CB(skb);
9406 		if (!skb_cb->vif) {
9407 			ath12k_warn(ar->ab, "no vif found for mgmt frame\n");
9408 			ath12k_mgmt_over_wmi_tx_drop(ar, skb);
9409 			continue;
9410 		}
9411 
9412 		ahvif = ath12k_vif_to_ahvif(skb_cb->vif);
9413 		if (!(ahvif->links_map & BIT(skb_cb->link_id))) {
9414 			ath12k_warn(ar->ab,
9415 				    "invalid linkid %u in mgmt over wmi tx with linkmap 0x%x\n",
9416 				    skb_cb->link_id, ahvif->links_map);
9417 			ath12k_mgmt_over_wmi_tx_drop(ar, skb);
9418 			continue;
9419 		}
9420 
9421 		arvif = wiphy_dereference(ah->hw->wiphy, ahvif->link[skb_cb->link_id]);
9422 		if (ar->allocated_vdev_map & (1LL << arvif->vdev_id)) {
9423 			/* Fill in the data which is required to be filled by the driver
9424 			 * For example: Max Tx power in Link Measurement Request/Report
9425 			 */
9426 			ret = ath12k_mac_mgmt_frame_fill_elem_data(arvif, skb);
9427 			if (ret) {
9428 				/* If we couldn't fill the data due to any reason,
9429 				 * let's not discard transmitting the packet.
9430 				 * For example: Software crypto and PMF case
9431 				 */
9432 				ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
9433 					   "Failed to fill the required data for the mgmt packet err %d\n",
9434 					   ret);
9435 			}
9436 
9437 			ret = ath12k_mac_mgmt_tx_wmi(ar, arvif, skb);
9438 			if (ret) {
9439 				ath12k_warn(ar->ab, "failed to tx mgmt frame, vdev_id %d :%d\n",
9440 					    arvif->vdev_id, ret);
9441 				ath12k_mgmt_over_wmi_tx_drop(ar, skb);
9442 			}
9443 		} else {
9444 			ath12k_warn(ar->ab,
9445 				    "dropping mgmt frame for vdev %d link %u is_started %d\n",
9446 				    arvif->vdev_id,
9447 				    skb_cb->link_id,
9448 				    arvif->is_started);
9449 			ath12k_mgmt_over_wmi_tx_drop(ar, skb);
9450 		}
9451 	}
9452 }
9453 
9454 int ath12k_mac_mgmt_tx(struct ath12k *ar, struct sk_buff *skb,
9455 		       bool is_prb_rsp)
9456 {
9457 	struct sk_buff_head *q = &ar->wmi_mgmt_tx_queue;
9458 
9459 	if (test_bit(ATH12K_FLAG_CRASH_FLUSH, &ar->ab->dev_flags))
9460 		return -ESHUTDOWN;
9461 
9462 	/* Drop probe response packets when the pending management tx
9463 	 * count has reached a certain threshold, so as to prioritize
9464 	 * other mgmt packets like auth and assoc to be sent on time
9465 	 * for establishing successful connections.
9466 	 */
9467 	if (is_prb_rsp &&
9468 	    atomic_read(&ar->num_pending_mgmt_tx) > ATH12K_PRB_RSP_DROP_THRESHOLD) {
9469 		ath12k_warn(ar->ab,
9470 			    "dropping probe response as pending queue is almost full\n");
9471 		return -ENOSPC;
9472 	}
9473 
9474 	if (skb_queue_len_lockless(q) >= ATH12K_TX_MGMT_NUM_PENDING_MAX) {
9475 		ath12k_warn(ar->ab, "mgmt tx queue is full\n");
9476 		return -ENOSPC;
9477 	}
9478 
9479 	skb_queue_tail(q, skb);
9480 	atomic_inc(&ar->num_pending_mgmt_tx);
9481 	wiphy_work_queue(ath12k_ar_to_hw(ar)->wiphy, &ar->wmi_mgmt_tx_work);
9482 
9483 	return 0;
9484 }
9485 EXPORT_SYMBOL(ath12k_mac_mgmt_tx);
9486 
9487 void ath12k_mac_add_p2p_noa_ie(struct ath12k *ar,
9488 			       struct ieee80211_vif *vif,
9489 			       struct sk_buff *skb,
9490 			       bool is_prb_rsp)
9491 {
9492 	struct ath12k_vif *ahvif = ath12k_vif_to_ahvif(vif);
9493 
9494 	if (likely(!is_prb_rsp))
9495 		return;
9496 
9497 	spin_lock_bh(&ar->data_lock);
9498 
9499 	if (ahvif->u.ap.noa_data &&
9500 	    !pskb_expand_head(skb, 0, ahvif->u.ap.noa_len,
9501 			      GFP_ATOMIC))
9502 		skb_put_data(skb, ahvif->u.ap.noa_data,
9503 			     ahvif->u.ap.noa_len);
9504 
9505 	spin_unlock_bh(&ar->data_lock);
9506 }
9507 EXPORT_SYMBOL(ath12k_mac_add_p2p_noa_ie);
9508 
9509 /* Note: called under rcu_read_lock() */
9510 void ath12k_mlo_mcast_update_tx_link_address(struct ieee80211_vif *vif,
9511 					     u8 link_id, struct sk_buff *skb,
9512 					     u32 info_flags)
9513 {
9514 	struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
9515 	struct ieee80211_bss_conf *bss_conf;
9516 
9517 	if (info_flags & IEEE80211_TX_CTL_HW_80211_ENCAP)
9518 		return;
9519 
9520 	bss_conf = rcu_dereference(vif->link_conf[link_id]);
9521 	if (bss_conf)
9522 		ether_addr_copy(hdr->addr2, bss_conf->addr);
9523 }
9524 EXPORT_SYMBOL(ath12k_mlo_mcast_update_tx_link_address);
9525 
9526 /* Note: called under rcu_read_lock() */
9527 u8 ath12k_mac_get_tx_link(struct ieee80211_sta *sta, struct ieee80211_vif *vif,
9528 			  u8 link, struct sk_buff *skb, u32 info_flags)
9529 {
9530 	struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
9531 	struct ath12k_vif *ahvif = ath12k_vif_to_ahvif(vif);
9532 	struct ieee80211_link_sta *link_sta;
9533 	struct ieee80211_bss_conf *bss_conf;
9534 	struct ath12k_sta *ahsta;
9535 
9536 	/* Use the link id passed or the default vif link */
9537 	if (!sta) {
9538 		if (link != IEEE80211_LINK_UNSPECIFIED)
9539 			return link;
9540 
9541 		return ahvif->deflink.link_id;
9542 	}
9543 
9544 	ahsta = ath12k_sta_to_ahsta(sta);
9545 
9546 	/* Below translation ensures we pass proper A2 & A3 for non ML clients.
9547 	 * Also it assumes for now support only for MLO AP in this path
9548 	 */
9549 	if (!sta->mlo) {
9550 		link = ahsta->deflink.link_id;
9551 
9552 		if (info_flags & IEEE80211_TX_CTL_HW_80211_ENCAP)
9553 			return link;
9554 
9555 		bss_conf = rcu_dereference(vif->link_conf[link]);
9556 		if (bss_conf) {
9557 			ether_addr_copy(hdr->addr2, bss_conf->addr);
9558 			if (!ieee80211_has_tods(hdr->frame_control) &&
9559 			    !ieee80211_has_fromds(hdr->frame_control))
9560 				ether_addr_copy(hdr->addr3, bss_conf->addr);
9561 		}
9562 
9563 		return link;
9564 	}
9565 
9566 	/* enqueue eth enacap & data frames on primary link, FW does link
9567 	 * selection and address translation.
9568 	 */
9569 	if (info_flags & IEEE80211_TX_CTL_HW_80211_ENCAP ||
9570 	    ieee80211_is_data(hdr->frame_control))
9571 		return ahsta->assoc_link_id;
9572 
9573 	/* 802.11 frame cases */
9574 	if (link == IEEE80211_LINK_UNSPECIFIED)
9575 		link = ahsta->deflink.link_id;
9576 
9577 	if (!ieee80211_is_mgmt(hdr->frame_control))
9578 		return link;
9579 
9580 	/* Perform address conversion for ML STA Tx */
9581 	bss_conf = rcu_dereference(vif->link_conf[link]);
9582 	link_sta = rcu_dereference(sta->link[link]);
9583 
9584 	if (bss_conf && link_sta) {
9585 		ether_addr_copy(hdr->addr1, link_sta->addr);
9586 		ether_addr_copy(hdr->addr2, bss_conf->addr);
9587 
9588 		if (vif->type == NL80211_IFTYPE_STATION && bss_conf->bssid)
9589 			ether_addr_copy(hdr->addr3, bss_conf->bssid);
9590 		else if (vif->type == NL80211_IFTYPE_AP)
9591 			ether_addr_copy(hdr->addr3, bss_conf->addr);
9592 
9593 		return link;
9594 	}
9595 
9596 	if (bss_conf) {
9597 		/* In certain cases where a ML sta associated and added subset of
9598 		 * links on which the ML AP is active, but now sends some frame
9599 		 * (ex. Probe request) on a different link which is active in our
9600 		 * MLD but was not added during previous association, we can
9601 		 * still honor the Tx to that ML STA via the requested link.
9602 		 * The control would reach here in such case only when that link
9603 		 * address is same as the MLD address or in worst case clients
9604 		 * used MLD address at TA wrongly which would have helped
9605 		 * identify the ML sta object and pass it here.
9606 		 * If the link address of that STA is different from MLD address,
9607 		 * then the sta object would be NULL and control won't reach
9608 		 * here but return at the start of the function itself with !sta
9609 		 * check. Also this would not need any translation at hdr->addr1
9610 		 * from MLD to link address since the RA is the MLD address
9611 		 * (same as that link address ideally) already.
9612 		 */
9613 		ether_addr_copy(hdr->addr2, bss_conf->addr);
9614 
9615 		if (vif->type == NL80211_IFTYPE_STATION && bss_conf->bssid)
9616 			ether_addr_copy(hdr->addr3, bss_conf->bssid);
9617 		else if (vif->type == NL80211_IFTYPE_AP)
9618 			ether_addr_copy(hdr->addr3, bss_conf->addr);
9619 	}
9620 
9621 	return link;
9622 }
9623 EXPORT_SYMBOL(ath12k_mac_get_tx_link);
9624 
9625 void ath12k_mac_drain_tx(struct ath12k *ar)
9626 {
9627 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
9628 
9629 	/* make sure rcu-protected mac80211 tx path itself is drained */
9630 	synchronize_net();
9631 
9632 	wiphy_work_cancel(ath12k_ar_to_hw(ar)->wiphy, &ar->wmi_mgmt_tx_work);
9633 	ath12k_mgmt_over_wmi_tx_purge(ar);
9634 }
9635 
9636 static int ath12k_mac_config_mon_status_default(struct ath12k *ar, bool enable)
9637 {
9638 	struct htt_rx_ring_tlv_filter tlv_filter = {};
9639 	struct ath12k_base *ab = ar->ab;
9640 	u32 ring_id, i;
9641 	int ret = 0;
9642 
9643 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
9644 
9645 	if (!ab->hw_params->rxdma1_enable)
9646 		return ret;
9647 
9648 	if (enable) {
9649 		tlv_filter = ath12k_mac_mon_status_filter_default;
9650 
9651 		if (ath12k_debugfs_rx_filter(ar))
9652 			tlv_filter.rx_filter = ath12k_debugfs_rx_filter(ar);
9653 	} else {
9654 		tlv_filter.rxmon_disable = true;
9655 	}
9656 
9657 	for (i = 0; i < ab->hw_params->num_rxdma_per_pdev; i++) {
9658 		ring_id = ar->dp.rxdma_mon_dst_ring[i].ring_id;
9659 		ret = ath12k_dp_tx_htt_rx_filter_setup(ab, ring_id,
9660 						       ar->dp.mac_id + i,
9661 						       HAL_RXDMA_MONITOR_DST,
9662 						       DP_RXDMA_REFILL_RING_SIZE,
9663 						       &tlv_filter);
9664 		if (ret) {
9665 			ath12k_err(ab,
9666 				   "failed to setup filter for monitor buf %d\n",
9667 				   ret);
9668 		}
9669 	}
9670 
9671 	return ret;
9672 }
9673 
9674 static int ath12k_mac_start(struct ath12k *ar)
9675 {
9676 	struct ath12k_hw *ah = ar->ah;
9677 	struct ath12k_base *ab = ar->ab;
9678 	struct ath12k_pdev *pdev = ar->pdev;
9679 	int ret;
9680 
9681 	lockdep_assert_held(&ah->hw_mutex);
9682 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
9683 
9684 	ret = ath12k_wmi_pdev_set_param(ar, WMI_PDEV_PARAM_PMF_QOS,
9685 					1, pdev->pdev_id);
9686 
9687 	if (ret) {
9688 		ath12k_err(ab, "failed to enable PMF QOS: %d\n", ret);
9689 		goto err;
9690 	}
9691 
9692 	ret = ath12k_wmi_pdev_set_param(ar, WMI_PDEV_PARAM_DYNAMIC_BW, 1,
9693 					pdev->pdev_id);
9694 	if (ret) {
9695 		ath12k_err(ab, "failed to enable dynamic bw: %d\n", ret);
9696 		goto err;
9697 	}
9698 
9699 	ret = ath12k_wmi_pdev_set_param(ar, WMI_PDEV_PARAM_ARP_AC_OVERRIDE,
9700 					0, pdev->pdev_id);
9701 	if (ret) {
9702 		ath12k_err(ab, "failed to set ac override for ARP: %d\n",
9703 			   ret);
9704 		goto err;
9705 	}
9706 
9707 	ret = ath12k_wmi_send_dfs_phyerr_offload_enable_cmd(ar, pdev->pdev_id);
9708 	if (ret) {
9709 		ath12k_err(ab, "failed to offload radar detection: %d\n",
9710 			   ret);
9711 		goto err;
9712 	}
9713 
9714 	ret = ath12k_dp_tx_htt_h2t_ppdu_stats_req(ar,
9715 						  HTT_PPDU_STATS_TAG_DEFAULT);
9716 	if (ret) {
9717 		ath12k_err(ab, "failed to req ppdu stats: %d\n", ret);
9718 		goto err;
9719 	}
9720 
9721 	ret = ath12k_wmi_pdev_set_param(ar, WMI_PDEV_PARAM_MESH_MCAST_ENABLE,
9722 					1, pdev->pdev_id);
9723 
9724 	if (ret) {
9725 		ath12k_err(ab, "failed to enable MESH MCAST ENABLE: (%d\n", ret);
9726 		goto err;
9727 	}
9728 
9729 	__ath12k_set_antenna(ar, ar->cfg_tx_chainmask, ar->cfg_rx_chainmask);
9730 
9731 	/* TODO: Do we need to enable ANI? */
9732 
9733 	ret = ath12k_reg_update_chan_list(ar, false);
9734 
9735 	/* The ar state alone can be turned off for non supported country
9736 	 * without returning the error value. As we need to update the channel
9737 	 * for the next ar.
9738 	 */
9739 	if (ret) {
9740 		if (ret == -EINVAL)
9741 			ret = 0;
9742 		goto err;
9743 	}
9744 
9745 	ar->num_started_vdevs = 0;
9746 	ar->num_created_vdevs = 0;
9747 	ar->num_peers = 0;
9748 	ar->allocated_vdev_map = 0;
9749 	ar->chan_tx_pwr = ATH12K_PDEV_TX_POWER_INVALID;
9750 
9751 	spin_lock_bh(&ar->data_lock);
9752 	ar->incumbent_signal_interference.handling_in_progress = false;
9753 	spin_unlock_bh(&ar->data_lock);
9754 
9755 	/* Configure monitor status ring with default rx_filter to get rx status
9756 	 * such as rssi, rx_duration.
9757 	 */
9758 	ret = ath12k_mac_config_mon_status_default(ar, true);
9759 	if (ret && (ret != -EOPNOTSUPP)) {
9760 		ath12k_err(ab, "failed to configure monitor status ring with default rx_filter: (%d)\n",
9761 			   ret);
9762 		goto err;
9763 	}
9764 
9765 	if (ret == -EOPNOTSUPP)
9766 		ath12k_dbg(ab, ATH12K_DBG_MAC,
9767 			   "monitor status config is not yet supported");
9768 
9769 	/* Configure the hash seed for hash based reo dest ring selection */
9770 	ath12k_wmi_pdev_lro_cfg(ar, ar->pdev->pdev_id);
9771 
9772 	/* allow device to enter IMPS */
9773 	if (ab->hw_params->idle_ps) {
9774 		ret = ath12k_wmi_pdev_set_param(ar, WMI_PDEV_PARAM_IDLE_PS_CONFIG,
9775 						1, pdev->pdev_id);
9776 		if (ret) {
9777 			ath12k_err(ab, "failed to enable idle ps: %d\n", ret);
9778 			goto err;
9779 		}
9780 	}
9781 
9782 	ret = ath12k_thermal_throttling_config_default(ar);
9783 	if (ret) {
9784 		ath12k_err(ab, "failed to set thermal throttle: %d\n", ret);
9785 		goto err;
9786 	}
9787 
9788 	rcu_assign_pointer(ab->pdevs_active[ar->pdev_idx],
9789 			   &ab->pdevs[ar->pdev_idx]);
9790 
9791 	return 0;
9792 err:
9793 
9794 	return ret;
9795 }
9796 
9797 static void ath12k_drain_tx(struct ath12k_hw *ah)
9798 {
9799 	struct ath12k *ar;
9800 	int i;
9801 
9802 	lockdep_assert_wiphy(ah->hw->wiphy);
9803 
9804 	for_each_ar(ah, ar, i)
9805 		ath12k_mac_drain_tx(ar);
9806 }
9807 
9808 int ath12k_mac_op_start(struct ieee80211_hw *hw)
9809 {
9810 	struct ath12k_hw *ah = ath12k_hw_to_ah(hw);
9811 	struct ath12k *ar;
9812 	int ret, i;
9813 
9814 	if (ath12k_ftm_mode)
9815 		return -EPERM;
9816 
9817 	lockdep_assert_wiphy(hw->wiphy);
9818 
9819 	ath12k_drain_tx(ah);
9820 
9821 	guard(mutex)(&ah->hw_mutex);
9822 
9823 	switch (ah->state) {
9824 	case ATH12K_HW_STATE_OFF:
9825 		ah->state = ATH12K_HW_STATE_ON;
9826 		break;
9827 	case ATH12K_HW_STATE_RESTARTING:
9828 		ah->state = ATH12K_HW_STATE_RESTARTED;
9829 		break;
9830 	case ATH12K_HW_STATE_RESTARTED:
9831 	case ATH12K_HW_STATE_WEDGED:
9832 	case ATH12K_HW_STATE_ON:
9833 	case ATH12K_HW_STATE_TM:
9834 		ah->state = ATH12K_HW_STATE_OFF;
9835 
9836 		WARN_ON(1);
9837 		return -EINVAL;
9838 	}
9839 
9840 	for_each_ar(ah, ar, i) {
9841 		ret = ath12k_mac_start(ar);
9842 		if (ret) {
9843 			ah->state = ATH12K_HW_STATE_OFF;
9844 
9845 			ath12k_err(ar->ab, "fail to start mac operations in pdev idx %d ret %d\n",
9846 				   ar->pdev_idx, ret);
9847 			goto fail_start;
9848 		}
9849 	}
9850 
9851 	return 0;
9852 
9853 fail_start:
9854 	for (; i > 0; i--) {
9855 		ar = ath12k_ah_to_ar(ah, i - 1);
9856 		ath12k_mac_stop(ar);
9857 	}
9858 
9859 	return ret;
9860 }
9861 EXPORT_SYMBOL(ath12k_mac_op_start);
9862 
9863 int ath12k_mac_rfkill_config(struct ath12k *ar)
9864 {
9865 	struct ath12k_base *ab = ar->ab;
9866 	u32 param;
9867 	int ret;
9868 
9869 	if (ab->hw_params->rfkill_pin == 0)
9870 		return -EOPNOTSUPP;
9871 
9872 	ath12k_dbg(ab, ATH12K_DBG_MAC,
9873 		   "mac rfkill_pin %d rfkill_cfg %d rfkill_on_level %d",
9874 		   ab->hw_params->rfkill_pin, ab->hw_params->rfkill_cfg,
9875 		   ab->hw_params->rfkill_on_level);
9876 
9877 	param = u32_encode_bits(ab->hw_params->rfkill_on_level,
9878 				WMI_RFKILL_CFG_RADIO_LEVEL) |
9879 		u32_encode_bits(ab->hw_params->rfkill_pin,
9880 				WMI_RFKILL_CFG_GPIO_PIN_NUM) |
9881 		u32_encode_bits(ab->hw_params->rfkill_cfg,
9882 				WMI_RFKILL_CFG_PIN_AS_GPIO);
9883 
9884 	ret = ath12k_wmi_pdev_set_param(ar, WMI_PDEV_PARAM_HW_RFKILL_CONFIG,
9885 					param, ar->pdev->pdev_id);
9886 	if (ret) {
9887 		ath12k_warn(ab,
9888 			    "failed to set rfkill config 0x%x: %d\n",
9889 			    param, ret);
9890 		return ret;
9891 	}
9892 
9893 	return 0;
9894 }
9895 
9896 int ath12k_mac_rfkill_enable_radio(struct ath12k *ar, bool enable)
9897 {
9898 	enum wmi_rfkill_enable_radio param;
9899 	int ret;
9900 
9901 	if (enable)
9902 		param = WMI_RFKILL_ENABLE_RADIO_ON;
9903 	else
9904 		param = WMI_RFKILL_ENABLE_RADIO_OFF;
9905 
9906 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac %d rfkill enable %d",
9907 		   ar->pdev_idx, param);
9908 
9909 	ret = ath12k_wmi_pdev_set_param(ar, WMI_PDEV_PARAM_RFKILL_ENABLE,
9910 					param, ar->pdev->pdev_id);
9911 	if (ret) {
9912 		ath12k_warn(ar->ab, "failed to set rfkill enable param %d: %d\n",
9913 			    param, ret);
9914 		return ret;
9915 	}
9916 
9917 	return 0;
9918 }
9919 
9920 static void ath12k_mac_stop(struct ath12k *ar)
9921 {
9922 	struct ath12k_pdev_dp *dp_pdev = &ar->dp;
9923 	struct ath12k_hw *ah = ar->ah;
9924 	struct htt_ppdu_stats_info *ppdu_stats, *tmp;
9925 	struct ath12k_wmi_scan_chan_list_arg *arg;
9926 	int ret;
9927 
9928 	lockdep_assert_held(&ah->hw_mutex);
9929 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
9930 
9931 	ret = ath12k_mac_config_mon_status_default(ar, false);
9932 	if (ret && (ret != -EOPNOTSUPP))
9933 		ath12k_err(ar->ab, "failed to clear rx_filter for monitor status ring: (%d)\n",
9934 			   ret);
9935 
9936 	clear_bit(ATH12K_FLAG_CAC_RUNNING, &ar->dev_flags);
9937 
9938 	cancel_delayed_work_sync(&ar->scan.timeout);
9939 	wiphy_work_cancel(ath12k_ar_to_hw(ar)->wiphy, &ar->scan.vdev_clean_wk);
9940 	cancel_work_sync(&ar->regd_channel_update_work);
9941 	cancel_work_sync(&ar->regd_update_work);
9942 	cancel_work_sync(&ar->ab->rfkill_work);
9943 	cancel_work_sync(&ar->ab->update_11d_work);
9944 	ar->state_11d = ATH12K_11D_IDLE;
9945 	complete(&ar->completed_11d_scan);
9946 
9947 	spin_lock_bh(&dp_pdev->ppdu_list_lock);
9948 	list_for_each_entry_safe(ppdu_stats, tmp, &dp_pdev->ppdu_stats_info, list) {
9949 		list_del(&ppdu_stats->list);
9950 		kfree(ppdu_stats);
9951 	}
9952 	spin_unlock_bh(&dp_pdev->ppdu_list_lock);
9953 
9954 	spin_lock_bh(&ar->data_lock);
9955 	while ((arg = list_first_entry_or_null(&ar->regd_channel_update_queue,
9956 					       struct ath12k_wmi_scan_chan_list_arg,
9957 					       list))) {
9958 		list_del(&arg->list);
9959 		kfree(arg);
9960 	}
9961 	spin_unlock_bh(&ar->data_lock);
9962 
9963 	rcu_assign_pointer(ar->ab->pdevs_active[ar->pdev_idx], NULL);
9964 
9965 	synchronize_rcu();
9966 
9967 	atomic_set(&ar->num_pending_mgmt_tx, 0);
9968 
9969 	spin_lock_bh(&ar->data_lock);
9970 	ar->incumbent_signal_interference.handling_in_progress = false;
9971 	spin_unlock_bh(&ar->data_lock);
9972 }
9973 
9974 void ath12k_mac_op_stop(struct ieee80211_hw *hw, bool suspend)
9975 {
9976 	struct ath12k_hw *ah = ath12k_hw_to_ah(hw);
9977 	struct ath12k *ar;
9978 	int i;
9979 
9980 	lockdep_assert_wiphy(hw->wiphy);
9981 
9982 	ath12k_drain_tx(ah);
9983 
9984 	mutex_lock(&ah->hw_mutex);
9985 
9986 	ah->state = ATH12K_HW_STATE_OFF;
9987 
9988 	for_each_ar(ah, ar, i)
9989 		ath12k_mac_stop(ar);
9990 
9991 	mutex_unlock(&ah->hw_mutex);
9992 }
9993 EXPORT_SYMBOL(ath12k_mac_op_stop);
9994 
9995 static u8
9996 ath12k_mac_get_vdev_stats_id(struct ath12k_link_vif *arvif)
9997 {
9998 	struct ath12k_base *ab = arvif->ar->ab;
9999 	u8 vdev_stats_id = 0;
10000 
10001 	do {
10002 		if (ab->free_vdev_stats_id_map & (1LL << vdev_stats_id)) {
10003 			vdev_stats_id++;
10004 			if (vdev_stats_id >= ATH12K_MAX_VDEV_STATS_ID) {
10005 				vdev_stats_id = ATH12K_INVAL_VDEV_STATS_ID;
10006 				break;
10007 			}
10008 		} else {
10009 			ab->free_vdev_stats_id_map |= (1LL << vdev_stats_id);
10010 			break;
10011 		}
10012 	} while (vdev_stats_id);
10013 
10014 	arvif->vdev_stats_id = vdev_stats_id;
10015 	return vdev_stats_id;
10016 }
10017 
10018 static int ath12k_mac_setup_vdev_params_mbssid(struct ath12k_link_vif *arvif,
10019 					       u32 *flags, u32 *tx_vdev_id)
10020 {
10021 	struct ath12k_vif *ahvif = arvif->ahvif;
10022 	struct ieee80211_bss_conf *link_conf;
10023 	struct ath12k *ar = arvif->ar;
10024 	struct ath12k_link_vif *tx_arvif;
10025 
10026 	link_conf = ath12k_mac_get_link_bss_conf(arvif);
10027 	if (!link_conf) {
10028 		ath12k_warn(ar->ab, "unable to access bss link conf in set mbssid params for vif %pM link %u\n",
10029 			    ahvif->vif->addr, arvif->link_id);
10030 		return -ENOLINK;
10031 	}
10032 
10033 	tx_arvif = ath12k_mac_get_tx_arvif(arvif, link_conf);
10034 	if (!tx_arvif)
10035 		return 0;
10036 
10037 	if (link_conf->nontransmitted) {
10038 		if (ath12k_ar_to_hw(ar)->wiphy !=
10039 		    ath12k_ar_to_hw(tx_arvif->ar)->wiphy)
10040 			return -EINVAL;
10041 
10042 		*flags = WMI_VDEV_MBSSID_FLAGS_NON_TRANSMIT_AP;
10043 		*tx_vdev_id = tx_arvif->vdev_id;
10044 	} else if (tx_arvif == arvif) {
10045 		*flags = WMI_VDEV_MBSSID_FLAGS_TRANSMIT_AP;
10046 	} else {
10047 		return -EINVAL;
10048 	}
10049 
10050 	if (link_conf->ema_ap)
10051 		*flags |= WMI_VDEV_MBSSID_FLAGS_EMA_MODE;
10052 
10053 	return 0;
10054 }
10055 
10056 static int ath12k_mac_setup_vdev_create_arg(struct ath12k_link_vif *arvif,
10057 					    struct ath12k_wmi_vdev_create_arg *arg)
10058 {
10059 	struct ath12k *ar = arvif->ar;
10060 	struct ath12k_pdev *pdev = ar->pdev;
10061 	struct ath12k_vif *ahvif = arvif->ahvif;
10062 	int ret;
10063 
10064 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
10065 
10066 	arg->if_id = arvif->vdev_id;
10067 	arg->type = ahvif->vdev_type;
10068 	arg->subtype = ahvif->vdev_subtype;
10069 	arg->pdev_id = pdev->pdev_id;
10070 
10071 	arg->mbssid_flags = WMI_VDEV_MBSSID_FLAGS_NON_MBSSID_AP;
10072 	arg->mbssid_tx_vdev_id = 0;
10073 	if (!test_bit(WMI_TLV_SERVICE_MBSS_PARAM_IN_VDEV_START_SUPPORT,
10074 		      ar->ab->wmi_ab.svc_map)) {
10075 		ret = ath12k_mac_setup_vdev_params_mbssid(arvif,
10076 							  &arg->mbssid_flags,
10077 							  &arg->mbssid_tx_vdev_id);
10078 		if (ret)
10079 			return ret;
10080 	}
10081 
10082 	if (pdev->cap.supported_bands & WMI_HOST_WLAN_2GHZ_CAP) {
10083 		arg->chains[NL80211_BAND_2GHZ].tx = ar->num_tx_chains;
10084 		arg->chains[NL80211_BAND_2GHZ].rx = ar->num_rx_chains;
10085 	}
10086 	if (pdev->cap.supported_bands & WMI_HOST_WLAN_5GHZ_CAP) {
10087 		arg->chains[NL80211_BAND_5GHZ].tx = ar->num_tx_chains;
10088 		arg->chains[NL80211_BAND_5GHZ].rx = ar->num_rx_chains;
10089 	}
10090 	if (pdev->cap.supported_bands & WMI_HOST_WLAN_5GHZ_CAP &&
10091 	    ar->supports_6ghz) {
10092 		arg->chains[NL80211_BAND_6GHZ].tx = ar->num_tx_chains;
10093 		arg->chains[NL80211_BAND_6GHZ].rx = ar->num_rx_chains;
10094 	}
10095 
10096 	arg->if_stats_id = ath12k_mac_get_vdev_stats_id(arvif);
10097 
10098 	if (ath12k_mac_is_ml_arvif(arvif)) {
10099 		if (hweight16(ahvif->vif->valid_links) > ATH12K_WMI_MLO_MAX_LINKS) {
10100 			ath12k_warn(ar->ab, "too many MLO links during setting up vdev: %d",
10101 				    ahvif->vif->valid_links);
10102 			return -EINVAL;
10103 		}
10104 
10105 		ether_addr_copy(arg->mld_addr, ahvif->vif->addr);
10106 	}
10107 
10108 	return 0;
10109 }
10110 
10111 static void ath12k_mac_update_vif_offload(struct ath12k_link_vif *arvif)
10112 {
10113 	struct ath12k_vif *ahvif = arvif->ahvif;
10114 	struct ieee80211_vif *vif = ath12k_ahvif_to_vif(ahvif);
10115 	struct ath12k *ar = arvif->ar;
10116 	struct ath12k_base *ab = ar->ab;
10117 	u32 param_id, param_value;
10118 	int ret;
10119 
10120 	param_id = WMI_VDEV_PARAM_TX_ENCAP_TYPE;
10121 	if (vif->type != NL80211_IFTYPE_STATION &&
10122 	    vif->type != NL80211_IFTYPE_AP)
10123 		vif->offload_flags &= ~(IEEE80211_OFFLOAD_ENCAP_ENABLED |
10124 					IEEE80211_OFFLOAD_DECAP_ENABLED);
10125 
10126 	if (vif->offload_flags & IEEE80211_OFFLOAD_ENCAP_ENABLED) {
10127 		ahvif->dp_vif.tx_encap_type = ATH12K_HW_TXRX_ETHERNET;
10128 		vif->offload_flags |= IEEE80211_OFFLOAD_ENCAP_4ADDR;
10129 	} else if (test_bit(ATH12K_FLAG_RAW_MODE, &ab->dev_flags)) {
10130 		ahvif->dp_vif.tx_encap_type = ATH12K_HW_TXRX_RAW;
10131 	} else {
10132 		ahvif->dp_vif.tx_encap_type = ATH12K_HW_TXRX_NATIVE_WIFI;
10133 	}
10134 
10135 	ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
10136 					    param_id, ahvif->dp_vif.tx_encap_type);
10137 	if (ret) {
10138 		ath12k_warn(ab, "failed to set vdev %d tx encap mode: %d\n",
10139 			    arvif->vdev_id, ret);
10140 		vif->offload_flags &= ~IEEE80211_OFFLOAD_ENCAP_ENABLED;
10141 	}
10142 
10143 	param_id = WMI_VDEV_PARAM_RX_DECAP_TYPE;
10144 	if (vif->offload_flags & IEEE80211_OFFLOAD_DECAP_ENABLED)
10145 		param_value = ATH12K_HW_TXRX_ETHERNET;
10146 	else if (test_bit(ATH12K_FLAG_RAW_MODE, &ab->dev_flags))
10147 		param_value = ATH12K_HW_TXRX_RAW;
10148 	else
10149 		param_value = ATH12K_HW_TXRX_NATIVE_WIFI;
10150 
10151 	ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
10152 					    param_id, param_value);
10153 	if (ret) {
10154 		ath12k_warn(ab, "failed to set vdev %d rx decap mode: %d\n",
10155 			    arvif->vdev_id, ret);
10156 		vif->offload_flags &= ~IEEE80211_OFFLOAD_DECAP_ENABLED;
10157 	}
10158 }
10159 
10160 void ath12k_mac_op_update_vif_offload(struct ieee80211_hw *hw,
10161 				      struct ieee80211_vif *vif)
10162 {
10163 	struct ath12k_vif *ahvif = ath12k_vif_to_ahvif(vif);
10164 	struct ath12k_link_vif *arvif;
10165 	unsigned long links;
10166 	int link_id;
10167 
10168 	lockdep_assert_wiphy(hw->wiphy);
10169 
10170 	if (vif->valid_links) {
10171 		links = vif->valid_links;
10172 		for_each_set_bit(link_id, &links, IEEE80211_MLD_MAX_NUM_LINKS) {
10173 			arvif = wiphy_dereference(hw->wiphy, ahvif->link[link_id]);
10174 			if (!(arvif && arvif->ar))
10175 				continue;
10176 
10177 			ath12k_mac_update_vif_offload(arvif);
10178 		}
10179 
10180 		return;
10181 	}
10182 
10183 	ath12k_mac_update_vif_offload(&ahvif->deflink);
10184 }
10185 EXPORT_SYMBOL(ath12k_mac_op_update_vif_offload);
10186 
10187 static bool ath12k_mac_vif_ap_active_any(struct ath12k_base *ab)
10188 {
10189 	struct ath12k *ar;
10190 	struct ath12k_pdev *pdev;
10191 	struct ath12k_link_vif *arvif;
10192 	int i;
10193 
10194 	for (i = 0; i < ab->num_radios; i++) {
10195 		pdev = &ab->pdevs[i];
10196 		ar = pdev->ar;
10197 		list_for_each_entry(arvif, &ar->arvifs, list) {
10198 			if (arvif->is_up &&
10199 			    arvif->ahvif->vdev_type == WMI_VDEV_TYPE_AP)
10200 				return true;
10201 		}
10202 	}
10203 	return false;
10204 }
10205 
10206 void ath12k_mac_11d_scan_start(struct ath12k *ar, u32 vdev_id)
10207 {
10208 	struct wmi_11d_scan_start_arg arg;
10209 	int ret;
10210 
10211 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
10212 
10213 	if (ar->regdom_set_by_user)
10214 		goto fin;
10215 
10216 	if (ar->vdev_id_11d_scan != ATH12K_11D_INVALID_VDEV_ID)
10217 		goto fin;
10218 
10219 	if (!test_bit(WMI_TLV_SERVICE_11D_OFFLOAD, ar->ab->wmi_ab.svc_map))
10220 		goto fin;
10221 
10222 	if (ath12k_mac_vif_ap_active_any(ar->ab))
10223 		goto fin;
10224 
10225 	arg.vdev_id = vdev_id;
10226 	arg.start_interval_msec = 0;
10227 	arg.scan_period_msec = ATH12K_SCAN_11D_INTERVAL;
10228 
10229 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
10230 		   "mac start 11d scan for vdev %d\n", vdev_id);
10231 
10232 	ret = ath12k_wmi_send_11d_scan_start_cmd(ar, &arg);
10233 	if (ret) {
10234 		ath12k_warn(ar->ab, "failed to start 11d scan vdev %d ret: %d\n",
10235 			    vdev_id, ret);
10236 	} else {
10237 		ar->vdev_id_11d_scan = vdev_id;
10238 		if (ar->state_11d == ATH12K_11D_PREPARING)
10239 			ar->state_11d = ATH12K_11D_RUNNING;
10240 	}
10241 
10242 fin:
10243 	if (ar->state_11d == ATH12K_11D_PREPARING) {
10244 		ar->state_11d = ATH12K_11D_IDLE;
10245 		complete(&ar->completed_11d_scan);
10246 	}
10247 }
10248 
10249 void ath12k_mac_11d_scan_stop(struct ath12k *ar)
10250 {
10251 	int ret;
10252 	u32 vdev_id;
10253 
10254 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
10255 
10256 	if (!test_bit(WMI_TLV_SERVICE_11D_OFFLOAD, ar->ab->wmi_ab.svc_map))
10257 		return;
10258 
10259 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac stop 11d for vdev %d\n",
10260 		   ar->vdev_id_11d_scan);
10261 
10262 	if (ar->state_11d == ATH12K_11D_PREPARING) {
10263 		ar->state_11d = ATH12K_11D_IDLE;
10264 		complete(&ar->completed_11d_scan);
10265 	}
10266 
10267 	if (ar->vdev_id_11d_scan != ATH12K_11D_INVALID_VDEV_ID) {
10268 		vdev_id = ar->vdev_id_11d_scan;
10269 
10270 		ret = ath12k_wmi_send_11d_scan_stop_cmd(ar, vdev_id);
10271 		if (ret) {
10272 			ath12k_warn(ar->ab,
10273 				    "failed to stopt 11d scan vdev %d ret: %d\n",
10274 				    vdev_id, ret);
10275 		} else {
10276 			ar->vdev_id_11d_scan = ATH12K_11D_INVALID_VDEV_ID;
10277 			ar->state_11d = ATH12K_11D_IDLE;
10278 			complete(&ar->completed_11d_scan);
10279 		}
10280 	}
10281 }
10282 
10283 void ath12k_mac_11d_scan_stop_all(struct ath12k_base *ab)
10284 {
10285 	struct ath12k *ar;
10286 	struct ath12k_pdev *pdev;
10287 	int i;
10288 
10289 	ath12k_dbg(ab, ATH12K_DBG_MAC, "mac stop soc 11d scan\n");
10290 
10291 	for (i = 0; i < ab->num_radios; i++) {
10292 		pdev = &ab->pdevs[i];
10293 		ar = pdev->ar;
10294 
10295 		ath12k_mac_11d_scan_stop(ar);
10296 	}
10297 }
10298 
10299 static void ath12k_mac_determine_vdev_type(struct ieee80211_vif *vif,
10300 					   struct ath12k_vif *ahvif)
10301 {
10302 	ahvif->vdev_subtype = WMI_VDEV_SUBTYPE_NONE;
10303 
10304 	switch (vif->type) {
10305 	case NL80211_IFTYPE_UNSPECIFIED:
10306 	case NL80211_IFTYPE_STATION:
10307 		ahvif->vdev_type = WMI_VDEV_TYPE_STA;
10308 
10309 		if (vif->p2p)
10310 			ahvif->vdev_subtype = WMI_VDEV_SUBTYPE_P2P_CLIENT;
10311 
10312 		break;
10313 	case NL80211_IFTYPE_MESH_POINT:
10314 		ahvif->vdev_subtype = WMI_VDEV_SUBTYPE_MESH_11S;
10315 		fallthrough;
10316 	case NL80211_IFTYPE_AP:
10317 		ahvif->vdev_type = WMI_VDEV_TYPE_AP;
10318 
10319 		if (vif->p2p)
10320 			ahvif->vdev_subtype = WMI_VDEV_SUBTYPE_P2P_GO;
10321 
10322 		break;
10323 	case NL80211_IFTYPE_MONITOR:
10324 		ahvif->vdev_type = WMI_VDEV_TYPE_MONITOR;
10325 		break;
10326 	case NL80211_IFTYPE_P2P_DEVICE:
10327 		ahvif->vdev_type = WMI_VDEV_TYPE_STA;
10328 		ahvif->vdev_subtype = WMI_VDEV_SUBTYPE_P2P_DEVICE;
10329 		break;
10330 	default:
10331 		WARN_ON(1);
10332 		break;
10333 	}
10334 }
10335 
10336 int ath12k_mac_vdev_create(struct ath12k *ar, struct ath12k_link_vif *arvif)
10337 {
10338 	struct ath12k_hw *ah = ar->ah;
10339 	struct ath12k_base *ab = ar->ab;
10340 	struct ieee80211_hw *hw = ah->hw;
10341 	struct ath12k_vif *ahvif = arvif->ahvif;
10342 	struct ieee80211_vif *vif = ath12k_ahvif_to_vif(ahvif);
10343 	struct wireless_dev *wdev = ieee80211_vif_to_wdev(vif);
10344 	struct ath12k_wmi_vdev_create_arg vdev_arg = {};
10345 	struct ath12k_wmi_peer_create_arg peer_param = {};
10346 	struct ieee80211_bss_conf *link_conf = NULL;
10347 	u32 param_id, param_value;
10348 	u16 nss;
10349 	int i;
10350 	int ret, vdev_id;
10351 	u8 link_id;
10352 	struct ath12k_dp_link_vif *dp_link_vif = NULL;
10353 	struct ath12k_dp_peer_create_params params = {};
10354 	bool dp_peer_created = false;
10355 
10356 	lockdep_assert_wiphy(hw->wiphy);
10357 
10358 	/* In NO_VIRTUAL_MONITOR, its necessary to restrict only one monitor
10359 	 * interface in each radio
10360 	 */
10361 	if (vif->type == NL80211_IFTYPE_MONITOR && ar->monitor_vdev_created)
10362 		return -EINVAL;
10363 
10364 	if (ar->num_created_vdevs >= TARGET_NUM_VDEVS(ab)) {
10365 		ath12k_warn(ab, "failed to create vdev, reached max vdev limit %d\n",
10366 			    TARGET_NUM_VDEVS(ab));
10367 		return -ENOSPC;
10368 	}
10369 
10370 	link_id = arvif->link_id;
10371 
10372 	if (link_id < IEEE80211_MLD_MAX_NUM_LINKS) {
10373 		link_conf = wiphy_dereference(hw->wiphy, vif->link_conf[link_id]);
10374 		if (!link_conf) {
10375 			ath12k_warn(ar->ab, "unable to access bss link conf in vdev create for vif %pM link %u\n",
10376 				    vif->addr, arvif->link_id);
10377 			return -ENOLINK;
10378 		}
10379 	}
10380 
10381 	if (link_conf)
10382 		memcpy(arvif->bssid, link_conf->addr, ETH_ALEN);
10383 	else
10384 		memcpy(arvif->bssid, vif->addr, ETH_ALEN);
10385 
10386 	arvif->ar = ar;
10387 	vdev_id = __ffs64(ab->free_vdev_map);
10388 	arvif->vdev_id = vdev_id;
10389 	if (vif->type == NL80211_IFTYPE_MONITOR)
10390 		ar->monitor_vdev_id = vdev_id;
10391 
10392 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac vdev create id %d type %d subtype %d map %llx\n",
10393 		   arvif->vdev_id, ahvif->vdev_type, ahvif->vdev_subtype,
10394 		   ab->free_vdev_map);
10395 
10396 	vif->cab_queue = arvif->vdev_id % (ATH12K_HW_MAX_QUEUES - 1);
10397 	for (i = 0; i < ARRAY_SIZE(vif->hw_queue); i++)
10398 		vif->hw_queue[i] = i % (ATH12K_HW_MAX_QUEUES - 1);
10399 
10400 	ret = ath12k_mac_setup_vdev_create_arg(arvif, &vdev_arg);
10401 	if (ret) {
10402 		ath12k_warn(ab, "failed to create vdev parameters %d: %d\n",
10403 			    arvif->vdev_id, ret);
10404 		goto err;
10405 	}
10406 
10407 	ret = ath12k_wmi_vdev_create(ar, arvif->bssid, &vdev_arg);
10408 	if (ret) {
10409 		ath12k_warn(ab, "failed to create WMI vdev %d: %d\n",
10410 			    arvif->vdev_id, ret);
10411 		goto err;
10412 	}
10413 
10414 	ar->num_created_vdevs++;
10415 	arvif->is_created = true;
10416 	ath12k_dbg(ab, ATH12K_DBG_MAC, "vdev %pM created, vdev_id %d\n",
10417 		   vif->addr, arvif->vdev_id);
10418 	ar->allocated_vdev_map |= 1LL << arvif->vdev_id;
10419 	ab->free_vdev_map &= ~(1LL << arvif->vdev_id);
10420 
10421 	spin_lock_bh(&ar->data_lock);
10422 	list_add(&arvif->list, &ar->arvifs);
10423 	spin_unlock_bh(&ar->data_lock);
10424 
10425 	ath12k_mac_update_vif_offload(arvif);
10426 
10427 	nss = hweight32(ar->cfg_tx_chainmask) ? : 1;
10428 	ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
10429 					    WMI_VDEV_PARAM_NSS, nss);
10430 	if (ret) {
10431 		ath12k_warn(ab, "failed to set vdev %d chainmask 0x%x, nss %d :%d\n",
10432 			    arvif->vdev_id, ar->cfg_tx_chainmask, nss, ret);
10433 		goto err_vdev_del;
10434 	}
10435 
10436 	dp_link_vif = ath12k_dp_vif_to_dp_link_vif(&ahvif->dp_vif, arvif->link_id);
10437 
10438 	dp_link_vif->vdev_id = arvif->vdev_id;
10439 	dp_link_vif->lmac_id = ar->lmac_id;
10440 	dp_link_vif->pdev_idx = ar->pdev_idx;
10441 
10442 	switch (ahvif->vdev_type) {
10443 	case WMI_VDEV_TYPE_AP:
10444 		params.ucast_ra_only = true;
10445 
10446 		if (arvif->link_id < IEEE80211_MLD_MAX_NUM_LINKS) {
10447 			ret = ath12k_dp_peer_create(&ah->dp_hw, arvif->bssid, &params);
10448 			if (ret) {
10449 				ath12k_warn(ab, "failed to vdev %d create dp_peer for AP: %d\n",
10450 					    arvif->vdev_id, ret);
10451 				goto err_vdev_del;
10452 			}
10453 			dp_peer_created = true;
10454 		}
10455 
10456 		peer_param.vdev_id = arvif->vdev_id;
10457 		peer_param.peer_addr = arvif->bssid;
10458 		peer_param.peer_type = WMI_PEER_TYPE_DEFAULT;
10459 		ret = ath12k_peer_create(ar, arvif, NULL, &peer_param);
10460 		if (ret) {
10461 			ath12k_warn(ab, "failed to vdev %d create peer for AP: %d\n",
10462 				    arvif->vdev_id, ret);
10463 			goto err_dp_peer_del;
10464 		}
10465 
10466 		ret = ath12k_mac_set_kickout(arvif);
10467 		if (ret) {
10468 			ath12k_warn(ar->ab, "failed to set vdev %i kickout parameters: %d\n",
10469 				    arvif->vdev_id, ret);
10470 			goto err_peer_del;
10471 		}
10472 		ath12k_mac_11d_scan_stop_all(ar->ab);
10473 		break;
10474 	case WMI_VDEV_TYPE_STA:
10475 		param_id = WMI_STA_PS_PARAM_RX_WAKE_POLICY;
10476 		param_value = WMI_STA_PS_RX_WAKE_POLICY_WAKE;
10477 		ret = ath12k_wmi_set_sta_ps_param(ar, arvif->vdev_id,
10478 						  param_id, param_value);
10479 		if (ret) {
10480 			ath12k_warn(ar->ab, "failed to set vdev %d RX wake policy: %d\n",
10481 				    arvif->vdev_id, ret);
10482 			goto err_peer_del;
10483 		}
10484 
10485 		param_id = WMI_STA_PS_PARAM_TX_WAKE_THRESHOLD;
10486 		param_value = WMI_STA_PS_TX_WAKE_THRESHOLD_ALWAYS;
10487 		ret = ath12k_wmi_set_sta_ps_param(ar, arvif->vdev_id,
10488 						  param_id, param_value);
10489 		if (ret) {
10490 			ath12k_warn(ar->ab, "failed to set vdev %d TX wake threshold: %d\n",
10491 				    arvif->vdev_id, ret);
10492 			goto err_peer_del;
10493 		}
10494 
10495 		param_id = WMI_STA_PS_PARAM_PSPOLL_COUNT;
10496 		param_value = WMI_STA_PS_PSPOLL_COUNT_NO_MAX;
10497 		ret = ath12k_wmi_set_sta_ps_param(ar, arvif->vdev_id,
10498 						  param_id, param_value);
10499 		if (ret) {
10500 			ath12k_warn(ar->ab, "failed to set vdev %d pspoll count: %d\n",
10501 				    arvif->vdev_id, ret);
10502 			goto err_peer_del;
10503 		}
10504 
10505 		ret = ath12k_wmi_pdev_set_ps_mode(ar, arvif->vdev_id, false);
10506 		if (ret) {
10507 			ath12k_warn(ar->ab, "failed to disable vdev %d ps mode: %d\n",
10508 				    arvif->vdev_id, ret);
10509 			goto err_peer_del;
10510 		}
10511 
10512 		/*
10513 		 * There could be race condition in firmware for the station
10514 		 * interface between enabling 4-address peer WMI param and
10515 		 * sending 4-address frame (NULL or EAPOL via TCL).
10516 		 * Make the station as WDS while bringup itself
10517 		 * to avoid the race condition
10518 		 */
10519 		if (vif->type == NL80211_IFTYPE_STATION &&
10520 		    (wdev && wdev->use_4addr)) {
10521 			ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
10522 							    WMI_VDEV_PARAM_WDS,
10523 							    1);
10524 			if (ret) {
10525 				ath12k_warn(ar->ab, "failed to set WDS vdev param: %d\n",
10526 					    ret);
10527 				goto err_peer_del;
10528 			}
10529 			arvif->set_wds_vdev_param = true;
10530 		}
10531 
10532 		if (test_bit(WMI_TLV_SERVICE_11D_OFFLOAD, ab->wmi_ab.svc_map) &&
10533 		    ahvif->vdev_type == WMI_VDEV_TYPE_STA &&
10534 		    ahvif->vdev_subtype == WMI_VDEV_SUBTYPE_NONE) {
10535 			reinit_completion(&ar->completed_11d_scan);
10536 			ar->state_11d = ATH12K_11D_PREPARING;
10537 		}
10538 		break;
10539 	case WMI_VDEV_TYPE_MONITOR:
10540 		ar->monitor_vdev_created = true;
10541 		break;
10542 	default:
10543 		break;
10544 	}
10545 
10546 	if (link_conf)
10547 		arvif->txpower = link_conf->txpower;
10548 	else
10549 		arvif->txpower = NL80211_TX_POWER_AUTOMATIC;
10550 
10551 	ret = ath12k_mac_txpower_recalc(ar);
10552 	if (ret)
10553 		goto err_peer_del;
10554 
10555 	param_id = WMI_VDEV_PARAM_RTS_THRESHOLD;
10556 	param_value = hw->wiphy->rts_threshold;
10557 	ar->rts_threshold = param_value;
10558 	ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
10559 					    param_id, param_value);
10560 	if (ret) {
10561 		ath12k_warn(ar->ab, "failed to set rts threshold for vdev %d: %d\n",
10562 			    arvif->vdev_id, ret);
10563 	}
10564 
10565 	ath12k_dp_vdev_tx_attach(ar, arvif);
10566 
10567 	return ret;
10568 
10569 err_peer_del:
10570 	if (ahvif->vdev_type == WMI_VDEV_TYPE_AP) {
10571 		reinit_completion(&ar->peer_delete_done);
10572 
10573 		ret = ath12k_wmi_send_peer_delete_cmd(ar, arvif->bssid,
10574 						      arvif->vdev_id);
10575 		if (ret) {
10576 			ath12k_warn(ar->ab, "failed to delete peer vdev_id %d addr %pM\n",
10577 				    arvif->vdev_id, arvif->bssid);
10578 			goto err_dp_peer_del;
10579 		}
10580 
10581 		ret = ath12k_wait_for_peer_delete_done(ar, arvif->vdev_id,
10582 						       arvif->bssid);
10583 		if (ret)
10584 			goto err_dp_peer_del;
10585 
10586 		ar->num_peers--;
10587 	}
10588 
10589 err_dp_peer_del:
10590 	if (dp_peer_created)
10591 		ath12k_dp_peer_delete(&ah->dp_hw, arvif->bssid, NULL);
10592 
10593 err_vdev_del:
10594 	if (ahvif->vdev_type == WMI_VDEV_TYPE_MONITOR) {
10595 		ar->monitor_vdev_id = -1;
10596 		ar->monitor_vdev_created = false;
10597 	}
10598 
10599 	ath12k_wmi_vdev_delete(ar, arvif->vdev_id);
10600 	ar->num_created_vdevs--;
10601 	ar->allocated_vdev_map &= ~(1LL << arvif->vdev_id);
10602 	ab->free_vdev_map |= 1LL << arvif->vdev_id;
10603 	ab->free_vdev_stats_id_map &= ~(1LL << arvif->vdev_stats_id);
10604 	spin_lock_bh(&ar->data_lock);
10605 	list_del(&arvif->list);
10606 	spin_unlock_bh(&ar->data_lock);
10607 
10608 err:
10609 	arvif->is_created = false;
10610 	arvif->ar = NULL;
10611 	return ret;
10612 }
10613 
10614 static void ath12k_mac_vif_flush_key_cache(struct ath12k_link_vif *arvif)
10615 {
10616 	struct ath12k_key_conf *key_conf, *tmp;
10617 	struct ath12k_vif *ahvif = arvif->ahvif;
10618 	struct ath12k_hw *ah = ahvif->ah;
10619 	struct ath12k_sta *ahsta;
10620 	struct ath12k_link_sta *arsta;
10621 	struct ath12k_vif_cache *cache = ahvif->cache[arvif->link_id];
10622 	int ret;
10623 
10624 	lockdep_assert_wiphy(ah->hw->wiphy);
10625 
10626 	list_for_each_entry_safe(key_conf, tmp, &cache->key_conf.list, list) {
10627 		arsta = NULL;
10628 		if (key_conf->sta) {
10629 			ahsta = ath12k_sta_to_ahsta(key_conf->sta);
10630 			arsta = wiphy_dereference(ah->hw->wiphy,
10631 						  ahsta->link[arvif->link_id]);
10632 			if (!arsta)
10633 				goto free_cache;
10634 		}
10635 
10636 		ret = ath12k_mac_set_key(arvif->ar, key_conf->cmd,
10637 					 arvif, arsta,
10638 					 key_conf->key);
10639 		if (ret)
10640 			ath12k_warn(arvif->ar->ab, "unable to apply set key param to vdev %d ret %d\n",
10641 				    arvif->vdev_id, ret);
10642 free_cache:
10643 		list_del(&key_conf->list);
10644 		kfree(key_conf);
10645 	}
10646 }
10647 
10648 static void ath12k_mac_vif_cache_flush(struct ath12k *ar, struct ath12k_link_vif *arvif)
10649 {
10650 	struct ath12k_vif *ahvif = arvif->ahvif;
10651 	struct ieee80211_vif *vif = ath12k_ahvif_to_vif(ahvif);
10652 	struct ath12k_vif_cache *cache = ahvif->cache[arvif->link_id];
10653 	struct ath12k_base *ab = ar->ab;
10654 	struct ieee80211_bss_conf *link_conf;
10655 
10656 	int ret;
10657 
10658 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
10659 
10660 	if (!cache)
10661 		return;
10662 
10663 	if (cache->tx_conf.changed) {
10664 		ret = ath12k_mac_conf_tx(arvif, cache->tx_conf.ac,
10665 					 &cache->tx_conf.tx_queue_params);
10666 		if (ret)
10667 			ath12k_warn(ab,
10668 				    "unable to apply tx config parameters to vdev %d\n",
10669 				    ret);
10670 	}
10671 
10672 	if (cache->bss_conf_changed) {
10673 		link_conf = ath12k_mac_get_link_bss_conf(arvif);
10674 		if (!link_conf) {
10675 			ath12k_warn(ar->ab, "unable to access bss link conf in cache flush for vif %pM link %u\n",
10676 				    vif->addr, arvif->link_id);
10677 			return;
10678 		}
10679 		ath12k_mac_bss_info_changed(ar, arvif, link_conf,
10680 					    cache->bss_conf_changed);
10681 	}
10682 
10683 	if (!list_empty(&cache->key_conf.list))
10684 		ath12k_mac_vif_flush_key_cache(arvif);
10685 
10686 	ath12k_ahvif_put_link_cache(ahvif, arvif->link_id);
10687 }
10688 
10689 static struct ath12k *ath12k_mac_assign_vif_to_vdev(struct ieee80211_hw *hw,
10690 						    struct ath12k_link_vif *arvif,
10691 						    struct ieee80211_chanctx_conf *ctx)
10692 {
10693 	struct ath12k_vif *ahvif = arvif->ahvif;
10694 	struct ieee80211_vif *vif = ath12k_ahvif_to_vif(ahvif);
10695 	struct ath12k_link_vif *scan_arvif;
10696 	struct ath12k_hw *ah = hw->priv;
10697 	struct ath12k *ar;
10698 	struct ath12k_base *ab;
10699 	u8 link_id = arvif->link_id, scan_link_id;
10700 	unsigned long scan_link_map;
10701 	int ret;
10702 
10703 	lockdep_assert_wiphy(hw->wiphy);
10704 
10705 	if (ah->num_radio == 1)
10706 		ar = ah->radio;
10707 	else if (ctx)
10708 		ar = ath12k_get_ar_by_ctx(hw, ctx);
10709 	else
10710 		return NULL;
10711 
10712 	if (!ar)
10713 		return NULL;
10714 
10715 	/* cleanup the scan vdev if we are done scan on that ar
10716 	 * and now we want to create for actual usage.
10717 	 */
10718 	if (ieee80211_vif_is_mld(vif)) {
10719 		scan_link_map = ahvif->links_map & ATH12K_SCAN_LINKS_MASK;
10720 		for_each_set_bit(scan_link_id, &scan_link_map, ATH12K_NUM_MAX_LINKS) {
10721 			scan_arvif = wiphy_dereference(hw->wiphy,
10722 						       ahvif->link[scan_link_id]);
10723 			if (scan_arvif && scan_arvif->ar == ar) {
10724 				ar->scan.arvif = NULL;
10725 				ath12k_mac_remove_link_interface(hw, scan_arvif);
10726 				ath12k_mac_unassign_link_vif(scan_arvif);
10727 				break;
10728 			}
10729 		}
10730 	}
10731 
10732 	if (arvif->ar) {
10733 		/* This is not expected really */
10734 		if (WARN_ON(!arvif->is_created)) {
10735 			arvif->ar = NULL;
10736 			return NULL;
10737 		}
10738 
10739 		if (ah->num_radio == 1)
10740 			return arvif->ar;
10741 
10742 		/* This can happen as scan vdev gets created during multiple scans
10743 		 * across different radios before a vdev is brought up in
10744 		 * a certain radio.
10745 		 */
10746 		if (ar != arvif->ar) {
10747 			if (WARN_ON(arvif->is_started))
10748 				return NULL;
10749 
10750 			ath12k_mac_remove_link_interface(hw, arvif);
10751 			ath12k_mac_unassign_link_vif(arvif);
10752 		}
10753 	}
10754 
10755 	ab = ar->ab;
10756 
10757 	/* Assign arvif again here since previous radio switch block
10758 	 * would've unassigned and cleared it.
10759 	 */
10760 	arvif = ath12k_mac_assign_link_vif(ah, vif, link_id);
10761 	if (vif->type == NL80211_IFTYPE_AP &&
10762 	    ar->num_peers > (ar->max_num_peers - 1)) {
10763 		ath12k_warn(ab, "failed to create vdev due to insufficient peer entry resource in firmware\n");
10764 		goto unlock;
10765 	}
10766 
10767 	if (arvif->is_created)
10768 		goto flush;
10769 
10770 	ret = ath12k_mac_vdev_create(ar, arvif);
10771 	if (ret) {
10772 		ath12k_warn(ab, "failed to create vdev %pM ret %d", vif->addr, ret);
10773 		goto unlock;
10774 	}
10775 
10776 flush:
10777 	/* If the vdev is created during channel assign and not during
10778 	 * add_interface(), Apply any parameters for the vdev which were received
10779 	 * after add_interface, corresponding to this vif.
10780 	 */
10781 	ath12k_mac_vif_cache_flush(ar, arvif);
10782 unlock:
10783 	return arvif->ar;
10784 }
10785 
10786 int ath12k_mac_op_add_interface(struct ieee80211_hw *hw,
10787 				struct ieee80211_vif *vif)
10788 {
10789 	struct ath12k_hw *ah = ath12k_hw_to_ah(hw);
10790 	struct ath12k_vif *ahvif = ath12k_vif_to_ahvif(vif);
10791 	struct ath12k_reg_info *reg_info;
10792 	struct ath12k_link_vif *arvif;
10793 	struct ath12k_base *ab;
10794 	struct ath12k *ar;
10795 	int i;
10796 
10797 	lockdep_assert_wiphy(hw->wiphy);
10798 
10799 	memset(ahvif, 0, sizeof(*ahvif));
10800 
10801 	ahvif->ah = ah;
10802 	ahvif->vif = vif;
10803 	arvif = &ahvif->deflink;
10804 
10805 	ath12k_mac_init_arvif(ahvif, arvif, -1);
10806 
10807 	/* Allocate Default Queue now and reassign during actual vdev create */
10808 	vif->cab_queue = ATH12K_HW_DEFAULT_QUEUE;
10809 	for (i = 0; i < ARRAY_SIZE(vif->hw_queue); i++)
10810 		vif->hw_queue[i] = ATH12K_HW_DEFAULT_QUEUE;
10811 
10812 	vif->driver_flags |= IEEE80211_VIF_SUPPORTS_UAPSD;
10813 
10814 	ath12k_mac_determine_vdev_type(vif, ahvif);
10815 
10816 	for_each_ar(ah, ar, i) {
10817 		if (!ath12k_wmi_supports_6ghz_cc_ext(ar))
10818 			continue;
10819 
10820 		ab = ar->ab;
10821 		reg_info = ab->reg_info[ar->pdev_idx];
10822 		ath12k_dbg(ab, ATH12K_DBG_MAC, "interface added to change reg rules\n");
10823 		ah->regd_updated = false;
10824 		ath12k_reg_handle_chan_list(ab, reg_info, ahvif->vdev_type,
10825 					    IEEE80211_REG_UNSET_AP);
10826 		break;
10827 	}
10828 
10829 	/* Defer vdev creation until assign_chanctx or hw_scan is initiated as driver
10830 	 * will not know if this interface is an ML vif at this point.
10831 	 */
10832 	return 0;
10833 }
10834 EXPORT_SYMBOL(ath12k_mac_op_add_interface);
10835 
10836 static void ath12k_mac_vif_unref(struct ath12k_dp *dp, struct ieee80211_vif *vif)
10837 {
10838 	struct ath12k_tx_desc_info *tx_desc_info;
10839 	struct ath12k_skb_cb *skb_cb;
10840 	struct sk_buff *skb;
10841 	int i;
10842 
10843 	for (i = 0; i < ATH12K_HW_MAX_QUEUES; i++) {
10844 		spin_lock_bh(&dp->tx_desc_lock[i]);
10845 
10846 		list_for_each_entry(tx_desc_info, &dp->tx_desc_used_list[i],
10847 				    list) {
10848 			skb = tx_desc_info->skb;
10849 			if (!skb)
10850 				continue;
10851 
10852 			skb_cb = ATH12K_SKB_CB(skb);
10853 			if (skb_cb->vif == vif)
10854 				skb_cb->vif = NULL;
10855 		}
10856 
10857 		spin_unlock_bh(&dp->tx_desc_lock[i]);
10858 	}
10859 }
10860 
10861 static int ath12k_mac_vdev_delete(struct ath12k *ar, struct ath12k_link_vif *arvif)
10862 {
10863 	struct ath12k_vif *ahvif = arvif->ahvif;
10864 	struct ieee80211_vif *vif = ath12k_ahvif_to_vif(ahvif);
10865 	struct ath12k_dp_link_vif *dp_link_vif;
10866 	struct ath12k_base *ab = ar->ab;
10867 	unsigned long time_left;
10868 	int ret;
10869 
10870 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
10871 
10872 	reinit_completion(&ar->vdev_delete_done);
10873 
10874 	ret = ath12k_wmi_vdev_delete(ar, arvif->vdev_id);
10875 	if (ret) {
10876 		ath12k_warn(ab, "failed to delete WMI vdev %d: %d\n",
10877 			    arvif->vdev_id, ret);
10878 		goto err_vdev_del;
10879 	}
10880 
10881 	time_left = wait_for_completion_timeout(&ar->vdev_delete_done,
10882 						ATH12K_VDEV_DELETE_TIMEOUT_HZ);
10883 	if (time_left == 0) {
10884 		ath12k_warn(ab, "Timeout in receiving vdev delete response\n");
10885 		goto err_vdev_del;
10886 	}
10887 
10888 	ab->free_vdev_map |= 1LL << arvif->vdev_id;
10889 	ar->allocated_vdev_map &= ~(1LL << arvif->vdev_id);
10890 	ar->num_created_vdevs--;
10891 
10892 	if (ahvif->vdev_type == WMI_VDEV_TYPE_MONITOR) {
10893 		ar->monitor_vdev_id = -1;
10894 		ar->monitor_vdev_created = false;
10895 	}
10896 
10897 	ath12k_dbg(ab, ATH12K_DBG_MAC, "vdev %pM deleted, vdev_id %d\n",
10898 		   vif->addr, arvif->vdev_id);
10899 
10900 err_vdev_del:
10901 	spin_lock_bh(&ar->data_lock);
10902 	list_del(&arvif->list);
10903 	spin_unlock_bh(&ar->data_lock);
10904 
10905 	ath12k_peer_cleanup(ar, arvif->vdev_id);
10906 	ath12k_ahvif_put_link_cache(ahvif, arvif->link_id);
10907 
10908 	idr_for_each(&ar->txmgmt_idr,
10909 		     ath12k_mac_vif_txmgmt_idr_remove, vif);
10910 
10911 	ath12k_mac_vif_unref(ath12k_ab_to_dp(ab), vif);
10912 
10913 	dp_link_vif = ath12k_dp_vif_to_dp_link_vif(&ahvif->dp_vif, arvif->link_id);
10914 	ath12k_dp_tx_put_bank_profile(ath12k_ab_to_dp(ab), dp_link_vif->bank_id);
10915 
10916 	/* Recalc txpower for remaining vdev */
10917 	ath12k_mac_txpower_recalc(ar);
10918 
10919 	/* TODO: recal traffic pause state based on the available vdevs */
10920 	arvif->is_created = false;
10921 	arvif->ar = NULL;
10922 
10923 	return ret;
10924 }
10925 
10926 void ath12k_mac_op_remove_interface(struct ieee80211_hw *hw,
10927 				    struct ieee80211_vif *vif)
10928 {
10929 	struct ath12k_vif *ahvif = ath12k_vif_to_ahvif(vif);
10930 	struct ath12k_link_vif *arvif;
10931 	struct ath12k *ar;
10932 	u8 link_id;
10933 
10934 	lockdep_assert_wiphy(hw->wiphy);
10935 
10936 	for (link_id = 0; link_id < ATH12K_NUM_MAX_LINKS; link_id++) {
10937 		/* if we cached some config but never received assign chanctx,
10938 		 * free the allocated cache.
10939 		 */
10940 		ath12k_ahvif_put_link_cache(ahvif, link_id);
10941 		arvif = wiphy_dereference(hw->wiphy, ahvif->link[link_id]);
10942 		if (!arvif || !arvif->is_created)
10943 			continue;
10944 
10945 		ar = arvif->ar;
10946 
10947 		/* Scan abortion is in progress since before this, cancel_hw_scan()
10948 		 * is expected to be executed. Since link is anyways going to be removed
10949 		 * now, just cancel the worker and send the scan aborted to user space
10950 		 */
10951 		if (ar->scan.arvif == arvif) {
10952 			wiphy_work_cancel(hw->wiphy, &ar->scan.vdev_clean_wk);
10953 
10954 			spin_lock_bh(&ar->data_lock);
10955 			ar->scan.arvif = NULL;
10956 			if (!ar->scan.is_roc) {
10957 				struct cfg80211_scan_info info = {
10958 					.aborted = true,
10959 				};
10960 
10961 				ath12k_mac_scan_send_complete(ar, &info);
10962 			}
10963 
10964 			ar->scan.state = ATH12K_SCAN_IDLE;
10965 			ar->scan_channel = NULL;
10966 			ar->scan.roc_freq = 0;
10967 			spin_unlock_bh(&ar->data_lock);
10968 		}
10969 
10970 		ath12k_mac_remove_link_interface(hw, arvif);
10971 		ath12k_mac_unassign_link_vif(arvif);
10972 	}
10973 }
10974 EXPORT_SYMBOL(ath12k_mac_op_remove_interface);
10975 
10976 /* FIXME: Has to be verified. */
10977 #define SUPPORTED_FILTERS			\
10978 	(FIF_ALLMULTI |				\
10979 	FIF_CONTROL |				\
10980 	FIF_PSPOLL |				\
10981 	FIF_OTHER_BSS |				\
10982 	FIF_BCN_PRBRESP_PROMISC |		\
10983 	FIF_PROBE_REQ |				\
10984 	FIF_FCSFAIL)
10985 
10986 void ath12k_mac_op_configure_filter(struct ieee80211_hw *hw,
10987 				    unsigned int changed_flags,
10988 				    unsigned int *total_flags,
10989 				    u64 multicast)
10990 {
10991 	struct ath12k_hw *ah = ath12k_hw_to_ah(hw);
10992 	struct ath12k *ar;
10993 
10994 	lockdep_assert_wiphy(hw->wiphy);
10995 
10996 	ar = ath12k_ah_to_ar(ah, 0);
10997 
10998 	*total_flags &= SUPPORTED_FILTERS;
10999 	ar->filter_flags = *total_flags;
11000 }
11001 EXPORT_SYMBOL(ath12k_mac_op_configure_filter);
11002 
11003 int ath12k_mac_op_get_antenna(struct ieee80211_hw *hw, int radio_idx,
11004 			      u32 *tx_ant, u32 *rx_ant)
11005 {
11006 	struct ath12k_hw *ah = ath12k_hw_to_ah(hw);
11007 	int antennas_rx = 0, antennas_tx = 0;
11008 	struct ath12k *ar;
11009 	int i;
11010 
11011 	lockdep_assert_wiphy(hw->wiphy);
11012 
11013 	for_each_ar(ah, ar, i) {
11014 		antennas_rx = max_t(u32, antennas_rx, ar->cfg_rx_chainmask);
11015 		antennas_tx = max_t(u32, antennas_tx, ar->cfg_tx_chainmask);
11016 	}
11017 
11018 	*tx_ant = antennas_tx;
11019 	*rx_ant = antennas_rx;
11020 
11021 	return 0;
11022 }
11023 EXPORT_SYMBOL(ath12k_mac_op_get_antenna);
11024 
11025 int ath12k_mac_op_set_antenna(struct ieee80211_hw *hw, int radio_idx,
11026 			      u32 tx_ant, u32 rx_ant)
11027 {
11028 	struct ath12k_hw *ah = ath12k_hw_to_ah(hw);
11029 	struct ath12k *ar;
11030 	int ret = 0;
11031 	int i;
11032 
11033 	lockdep_assert_wiphy(hw->wiphy);
11034 
11035 	for_each_ar(ah, ar, i) {
11036 		ret = __ath12k_set_antenna(ar, tx_ant, rx_ant);
11037 		if (ret)
11038 			break;
11039 	}
11040 
11041 	return ret;
11042 }
11043 EXPORT_SYMBOL(ath12k_mac_op_set_antenna);
11044 
11045 static int ath12k_mac_ampdu_action(struct ieee80211_hw *hw,
11046 				   struct ieee80211_vif *vif,
11047 				   struct ieee80211_ampdu_params *params,
11048 				   u8 link_id)
11049 {
11050 	struct ath12k *ar;
11051 	int ret = -EINVAL;
11052 
11053 	lockdep_assert_wiphy(hw->wiphy);
11054 
11055 	ar = ath12k_get_ar_by_vif(hw, vif, link_id);
11056 	if (!ar)
11057 		return -EINVAL;
11058 
11059 	switch (params->action) {
11060 	case IEEE80211_AMPDU_RX_START:
11061 		ret = ath12k_dp_rx_ampdu_start(ar, params, link_id);
11062 		break;
11063 	case IEEE80211_AMPDU_RX_STOP:
11064 		ret = ath12k_dp_rx_ampdu_stop(ar, params, link_id);
11065 		break;
11066 	case IEEE80211_AMPDU_TX_START:
11067 	case IEEE80211_AMPDU_TX_STOP_CONT:
11068 	case IEEE80211_AMPDU_TX_STOP_FLUSH:
11069 	case IEEE80211_AMPDU_TX_STOP_FLUSH_CONT:
11070 	case IEEE80211_AMPDU_TX_OPERATIONAL:
11071 		/* Tx A-MPDU aggregation offloaded to hw/fw so deny mac80211
11072 		 * Tx aggregation requests.
11073 		 */
11074 		ret = -EOPNOTSUPP;
11075 		break;
11076 	}
11077 
11078 	if (ret)
11079 		ath12k_warn(ar->ab, "unable to perform ampdu action %d for vif %pM link %u ret %d\n",
11080 			    params->action, vif->addr, link_id, ret);
11081 
11082 	return ret;
11083 }
11084 
11085 int ath12k_mac_op_ampdu_action(struct ieee80211_hw *hw,
11086 			       struct ieee80211_vif *vif,
11087 			       struct ieee80211_ampdu_params *params)
11088 {
11089 	struct ieee80211_sta *sta = params->sta;
11090 	struct ath12k_sta *ahsta = ath12k_sta_to_ahsta(sta);
11091 	unsigned long links_map = ahsta->links_map;
11092 	int ret = -EINVAL;
11093 	u8 link_id;
11094 
11095 	lockdep_assert_wiphy(hw->wiphy);
11096 
11097 	if (WARN_ON(!links_map))
11098 		return ret;
11099 
11100 	for_each_set_bit(link_id, &links_map, IEEE80211_MLD_MAX_NUM_LINKS) {
11101 		ret = ath12k_mac_ampdu_action(hw, vif, params, link_id);
11102 		if (ret)
11103 			return ret;
11104 	}
11105 
11106 	return 0;
11107 }
11108 EXPORT_SYMBOL(ath12k_mac_op_ampdu_action);
11109 
11110 int ath12k_mac_op_add_chanctx(struct ieee80211_hw *hw,
11111 			      struct ieee80211_chanctx_conf *ctx)
11112 {
11113 	struct ath12k *ar;
11114 	struct ath12k_base *ab;
11115 
11116 	lockdep_assert_wiphy(hw->wiphy);
11117 
11118 	ar = ath12k_get_ar_by_ctx(hw, ctx);
11119 	if (!ar)
11120 		return -EINVAL;
11121 
11122 	ab = ar->ab;
11123 
11124 	ath12k_dbg(ab, ATH12K_DBG_MAC,
11125 		   "mac chanctx add freq %u width %d ptr %p\n",
11126 		   ctx->def.chan->center_freq, ctx->def.width, ctx);
11127 
11128 	spin_lock_bh(&ar->data_lock);
11129 	/* TODO: In case of multiple channel context, populate rx_channel from
11130 	 * Rx PPDU desc information.
11131 	 */
11132 	ar->rx_channel = ctx->def.chan;
11133 	spin_unlock_bh(&ar->data_lock);
11134 	ar->chan_tx_pwr = ATH12K_PDEV_TX_POWER_INVALID;
11135 
11136 	return 0;
11137 }
11138 EXPORT_SYMBOL(ath12k_mac_op_add_chanctx);
11139 
11140 void ath12k_mac_op_remove_chanctx(struct ieee80211_hw *hw,
11141 				  struct ieee80211_chanctx_conf *ctx)
11142 {
11143 	struct ath12k *ar;
11144 	struct ath12k_base *ab;
11145 
11146 	lockdep_assert_wiphy(hw->wiphy);
11147 
11148 	ar = ath12k_get_ar_by_ctx(hw, ctx);
11149 	if (!ar)
11150 		return;
11151 
11152 	ab = ar->ab;
11153 
11154 	ath12k_dbg(ab, ATH12K_DBG_MAC,
11155 		   "mac chanctx remove freq %u width %d ptr %p\n",
11156 		   ctx->def.chan->center_freq, ctx->def.width, ctx);
11157 
11158 	spin_lock_bh(&ar->data_lock);
11159 	/* TODO: In case of there is one more channel context left, populate
11160 	 * rx_channel with the channel of that remaining channel context.
11161 	 */
11162 	ar->rx_channel = NULL;
11163 	spin_unlock_bh(&ar->data_lock);
11164 	ar->chan_tx_pwr = ATH12K_PDEV_TX_POWER_INVALID;
11165 }
11166 EXPORT_SYMBOL(ath12k_mac_op_remove_chanctx);
11167 
11168 static enum wmi_phy_mode
11169 ath12k_mac_check_down_grade_phy_mode(struct ath12k *ar,
11170 				     enum wmi_phy_mode mode,
11171 				     enum nl80211_band band,
11172 				     enum nl80211_iftype type)
11173 {
11174 	struct ieee80211_sta_eht_cap *eht_cap = NULL;
11175 	enum wmi_phy_mode down_mode;
11176 	int n = ar->mac.sbands[band].n_iftype_data;
11177 	int i;
11178 	struct ieee80211_sband_iftype_data *data;
11179 
11180 	if (mode < MODE_11BE_EHT20)
11181 		return mode;
11182 
11183 	data = ar->mac.iftype[band];
11184 	for (i = 0; i < n; i++) {
11185 		if (data[i].types_mask & BIT(type)) {
11186 			eht_cap = &data[i].eht_cap;
11187 			break;
11188 		}
11189 	}
11190 
11191 	if (eht_cap && eht_cap->has_eht)
11192 		return mode;
11193 
11194 	switch (mode) {
11195 	case MODE_11BE_EHT20:
11196 		down_mode = MODE_11AX_HE20;
11197 		break;
11198 	case MODE_11BE_EHT40:
11199 		down_mode = MODE_11AX_HE40;
11200 		break;
11201 	case MODE_11BE_EHT80:
11202 		down_mode = MODE_11AX_HE80;
11203 		break;
11204 	case MODE_11BE_EHT80_80:
11205 		down_mode = MODE_11AX_HE80_80;
11206 		break;
11207 	case MODE_11BE_EHT160:
11208 	case MODE_11BE_EHT160_160:
11209 	case MODE_11BE_EHT320:
11210 		down_mode = MODE_11AX_HE160;
11211 		break;
11212 	case MODE_11BE_EHT20_2G:
11213 		down_mode = MODE_11AX_HE20_2G;
11214 		break;
11215 	case MODE_11BE_EHT40_2G:
11216 		down_mode = MODE_11AX_HE40_2G;
11217 		break;
11218 	default:
11219 		down_mode = mode;
11220 		break;
11221 	}
11222 
11223 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
11224 		   "mac vdev start phymode %s downgrade to %s\n",
11225 		   ath12k_mac_phymode_str(mode),
11226 		   ath12k_mac_phymode_str(down_mode));
11227 
11228 	return down_mode;
11229 }
11230 
11231 static void
11232 ath12k_mac_mlo_get_vdev_args(struct ath12k_link_vif *arvif,
11233 			     struct wmi_ml_arg *ml_arg)
11234 {
11235 	struct ath12k_vif *ahvif = arvif->ahvif;
11236 	struct wmi_ml_partner_info *partner_info;
11237 	struct ieee80211_bss_conf *link_conf;
11238 	struct ath12k_link_vif *arvif_p;
11239 	unsigned long links;
11240 	u8 link_id;
11241 
11242 	lockdep_assert_wiphy(ahvif->ah->hw->wiphy);
11243 
11244 	if (!ath12k_mac_is_ml_arvif(arvif))
11245 		return;
11246 
11247 	if (hweight16(ahvif->vif->valid_links) > ATH12K_WMI_MLO_MAX_LINKS)
11248 		return;
11249 
11250 	ml_arg->enabled = true;
11251 
11252 	/* Driver always add a new link via VDEV START, FW takes
11253 	 * care of internally adding this link to existing
11254 	 * link vdevs which are advertised as partners below
11255 	 */
11256 	ml_arg->link_add = true;
11257 
11258 	ml_arg->assoc_link = arvif->is_sta_assoc_link;
11259 
11260 	partner_info = ml_arg->partner_info;
11261 
11262 	links = ahvif->links_map;
11263 	for_each_set_bit(link_id, &links, IEEE80211_MLD_MAX_NUM_LINKS) {
11264 		arvif_p = wiphy_dereference(ahvif->ah->hw->wiphy, ahvif->link[link_id]);
11265 
11266 		if (WARN_ON(!arvif_p))
11267 			continue;
11268 
11269 		if (arvif == arvif_p)
11270 			continue;
11271 
11272 		if (!arvif_p->is_started)
11273 			continue;
11274 
11275 		link_conf = wiphy_dereference(ahvif->ah->hw->wiphy,
11276 					      ahvif->vif->link_conf[arvif_p->link_id]);
11277 
11278 		if (!link_conf)
11279 			continue;
11280 
11281 		partner_info->vdev_id = arvif_p->vdev_id;
11282 		partner_info->hw_link_id = arvif_p->ar->pdev->hw_link_id;
11283 		ether_addr_copy(partner_info->addr, link_conf->addr);
11284 		ml_arg->num_partner_links++;
11285 		partner_info++;
11286 	}
11287 }
11288 
11289 static int
11290 ath12k_mac_vdev_start_restart(struct ath12k_link_vif *arvif,
11291 			      struct ieee80211_chanctx_conf *ctx,
11292 			      bool restart)
11293 {
11294 	struct ath12k *ar = arvif->ar;
11295 	struct ath12k_base *ab = ar->ab;
11296 	struct wmi_vdev_start_req_arg arg = {};
11297 	const struct cfg80211_chan_def *chandef = &ctx->def;
11298 	struct ieee80211_hw *hw = ath12k_ar_to_hw(ar);
11299 	struct ath12k_vif *ahvif = arvif->ahvif;
11300 	struct ieee80211_bss_conf *link_conf;
11301 	unsigned int dfs_cac_time;
11302 	int ret;
11303 
11304 	lockdep_assert_wiphy(hw->wiphy);
11305 
11306 	link_conf = ath12k_mac_get_link_bss_conf(arvif);
11307 	if (!link_conf) {
11308 		ath12k_warn(ar->ab, "unable to access bss link conf in vdev start for vif %pM link %u\n",
11309 			    ahvif->vif->addr, arvif->link_id);
11310 		return -ENOLINK;
11311 	}
11312 
11313 	reinit_completion(&ar->vdev_setup_done);
11314 
11315 	arg.vdev_id = arvif->vdev_id;
11316 	arg.dtim_period = arvif->dtim_period;
11317 	arg.bcn_intval = arvif->beacon_interval;
11318 	arg.punct_bitmap = ~arvif->punct_bitmap;
11319 
11320 	arg.freq = chandef->chan->center_freq;
11321 	arg.band_center_freq1 = chandef->center_freq1;
11322 	arg.band_center_freq2 = chandef->center_freq2;
11323 	arg.mode = ath12k_phymodes[chandef->chan->band][chandef->width];
11324 
11325 	arg.mode = ath12k_mac_check_down_grade_phy_mode(ar, arg.mode,
11326 							chandef->chan->band,
11327 							ahvif->vif->type);
11328 	arg.min_power = 0;
11329 	arg.max_power = chandef->chan->max_power;
11330 	arg.max_reg_power = chandef->chan->max_reg_power;
11331 	arg.max_antenna_gain = chandef->chan->max_antenna_gain;
11332 
11333 	arg.pref_tx_streams = ar->num_tx_chains;
11334 	arg.pref_rx_streams = ar->num_rx_chains;
11335 
11336 	arg.mbssid_flags = WMI_VDEV_MBSSID_FLAGS_NON_MBSSID_AP;
11337 	arg.mbssid_tx_vdev_id = 0;
11338 	if (test_bit(WMI_TLV_SERVICE_MBSS_PARAM_IN_VDEV_START_SUPPORT,
11339 		     ar->ab->wmi_ab.svc_map)) {
11340 		ret = ath12k_mac_setup_vdev_params_mbssid(arvif,
11341 							  &arg.mbssid_flags,
11342 							  &arg.mbssid_tx_vdev_id);
11343 		if (ret)
11344 			return ret;
11345 	}
11346 
11347 	if (ahvif->vdev_type == WMI_VDEV_TYPE_AP) {
11348 		arg.ssid = ahvif->u.ap.ssid;
11349 		arg.ssid_len = ahvif->u.ap.ssid_len;
11350 		arg.hidden_ssid = ahvif->u.ap.hidden_ssid;
11351 
11352 		/* For now allow DFS for AP mode */
11353 		arg.chan_radar = !!(chandef->chan->flags & IEEE80211_CHAN_RADAR);
11354 
11355 		arg.freq2_radar = ctx->radar_enabled;
11356 
11357 		arg.passive = arg.chan_radar;
11358 
11359 		spin_lock_bh(&ab->base_lock);
11360 		arg.regdomain = ar->ab->dfs_region;
11361 		spin_unlock_bh(&ab->base_lock);
11362 
11363 		/* TODO: Notify if secondary 80Mhz also needs radar detection */
11364 	}
11365 
11366 	arg.passive |= !!(chandef->chan->flags & IEEE80211_CHAN_NO_IR);
11367 
11368 	if (!restart)
11369 		ath12k_mac_mlo_get_vdev_args(arvif, &arg.ml);
11370 
11371 	ath12k_dbg(ab, ATH12K_DBG_MAC,
11372 		   "mac vdev %d start center_freq %d phymode %s punct_bitmap 0x%x\n",
11373 		   arg.vdev_id, arg.freq,
11374 		   ath12k_mac_phymode_str(arg.mode), arg.punct_bitmap);
11375 
11376 	ret = ath12k_wmi_vdev_start(ar, &arg, restart);
11377 	if (ret) {
11378 		ath12k_warn(ar->ab, "failed to %s WMI vdev %i\n",
11379 			    restart ? "restart" : "start", arg.vdev_id);
11380 		return ret;
11381 	}
11382 
11383 	ret = ath12k_mac_vdev_setup_sync(ar);
11384 	if (ret) {
11385 		ath12k_warn(ab, "failed to synchronize setup for vdev %i %s: %d\n",
11386 			    arg.vdev_id, restart ? "restart" : "start", ret);
11387 		return ret;
11388 	}
11389 
11390 	/* TODO: For now we only set TPC power here. However when
11391 	 * channel changes, say CSA, it should be updated again.
11392 	 */
11393 	if (ath12k_mac_supports_tpc(ar, ahvif, chandef)) {
11394 		ath12k_mac_fill_reg_tpc_info(ar, arvif, ctx);
11395 		ath12k_wmi_send_vdev_set_tpc_power(ar, arvif->vdev_id,
11396 						   &arvif->reg_tpc_info);
11397 	}
11398 
11399 	ar->num_started_vdevs++;
11400 	ath12k_dbg(ab, ATH12K_DBG_MAC,  "vdev %pM started, vdev_id %d\n",
11401 		   ahvif->vif->addr, arvif->vdev_id);
11402 
11403 	/* Enable CAC Running Flag in the driver by checking all sub-channel's DFS
11404 	 * state as NL80211_DFS_USABLE which indicates CAC needs to be
11405 	 * done before channel usage. This flag is used to drop rx packets.
11406 	 * during CAC.
11407 	 */
11408 	/* TODO: Set the flag for other interface types as required */
11409 	if (arvif->ahvif->vdev_type == WMI_VDEV_TYPE_AP && ctx->radar_enabled &&
11410 	    cfg80211_chandef_dfs_usable(hw->wiphy, chandef)) {
11411 		set_bit(ATH12K_FLAG_CAC_RUNNING, &ar->dev_flags);
11412 		dfs_cac_time = cfg80211_chandef_dfs_cac_time(hw->wiphy, chandef);
11413 
11414 		ath12k_dbg(ab, ATH12K_DBG_MAC,
11415 			   "CAC started dfs_cac_time %u center_freq %d center_freq1 %d for vdev %d\n",
11416 			   dfs_cac_time, arg.freq, arg.band_center_freq1, arg.vdev_id);
11417 	}
11418 
11419 	ret = ath12k_mac_set_txbf_conf(arvif);
11420 	if (ret)
11421 		ath12k_warn(ab, "failed to set txbf conf for vdev %d: %d\n",
11422 			    arvif->vdev_id, ret);
11423 
11424 	return 0;
11425 }
11426 
11427 static int ath12k_mac_vdev_start(struct ath12k_link_vif *arvif,
11428 				 struct ieee80211_chanctx_conf *ctx)
11429 {
11430 	return ath12k_mac_vdev_start_restart(arvif, ctx, false);
11431 }
11432 
11433 static int ath12k_mac_vdev_restart(struct ath12k_link_vif *arvif,
11434 				   struct ieee80211_chanctx_conf *ctx)
11435 {
11436 	return ath12k_mac_vdev_start_restart(arvif, ctx, true);
11437 }
11438 
11439 struct ath12k_mac_change_chanctx_arg {
11440 	struct ieee80211_chanctx_conf *ctx;
11441 	struct ieee80211_vif_chanctx_switch *vifs;
11442 	int n_vifs;
11443 	int next_vif;
11444 	struct ath12k *ar;
11445 };
11446 
11447 static void
11448 ath12k_mac_change_chanctx_cnt_iter(void *data, u8 *mac,
11449 				   struct ieee80211_vif *vif)
11450 {
11451 	struct ath12k_vif *ahvif = ath12k_vif_to_ahvif(vif);
11452 	struct ath12k_mac_change_chanctx_arg *arg = data;
11453 	struct ieee80211_bss_conf *link_conf;
11454 	struct ath12k_link_vif *arvif;
11455 	unsigned long links_map;
11456 	u8 link_id;
11457 
11458 	lockdep_assert_wiphy(ahvif->ah->hw->wiphy);
11459 
11460 	links_map = ahvif->links_map;
11461 	for_each_set_bit(link_id, &links_map, IEEE80211_MLD_MAX_NUM_LINKS) {
11462 		arvif = wiphy_dereference(ahvif->ah->hw->wiphy, ahvif->link[link_id]);
11463 		if (WARN_ON(!arvif))
11464 			continue;
11465 
11466 		if (!arvif->is_created || arvif->ar != arg->ar)
11467 			continue;
11468 
11469 		link_conf = wiphy_dereference(ahvif->ah->hw->wiphy,
11470 					      vif->link_conf[link_id]);
11471 		if (WARN_ON(!link_conf))
11472 			continue;
11473 
11474 		if (rcu_access_pointer(link_conf->chanctx_conf) != arg->ctx)
11475 			continue;
11476 
11477 		arg->n_vifs++;
11478 	}
11479 }
11480 
11481 static void
11482 ath12k_mac_change_chanctx_fill_iter(void *data, u8 *mac,
11483 				    struct ieee80211_vif *vif)
11484 {
11485 	struct ath12k_vif *ahvif = ath12k_vif_to_ahvif(vif);
11486 	struct ath12k_mac_change_chanctx_arg *arg = data;
11487 	struct ieee80211_bss_conf *link_conf;
11488 	struct ieee80211_chanctx_conf *ctx;
11489 	struct ath12k_link_vif *arvif;
11490 	unsigned long links_map;
11491 	u8 link_id;
11492 
11493 	lockdep_assert_wiphy(ahvif->ah->hw->wiphy);
11494 
11495 	links_map = ahvif->links_map;
11496 	for_each_set_bit(link_id, &links_map, IEEE80211_MLD_MAX_NUM_LINKS) {
11497 		arvif = wiphy_dereference(ahvif->ah->hw->wiphy, ahvif->link[link_id]);
11498 		if (WARN_ON(!arvif))
11499 			continue;
11500 
11501 		if (!arvif->is_created || arvif->ar != arg->ar)
11502 			continue;
11503 
11504 		link_conf = wiphy_dereference(ahvif->ah->hw->wiphy,
11505 					      vif->link_conf[arvif->link_id]);
11506 		if (WARN_ON(!link_conf))
11507 			continue;
11508 
11509 		ctx = rcu_access_pointer(link_conf->chanctx_conf);
11510 		if (ctx != arg->ctx)
11511 			continue;
11512 
11513 		if (WARN_ON(arg->next_vif == arg->n_vifs))
11514 			return;
11515 
11516 		arg->vifs[arg->next_vif].vif = vif;
11517 		arg->vifs[arg->next_vif].old_ctx = ctx;
11518 		arg->vifs[arg->next_vif].new_ctx = ctx;
11519 		arg->vifs[arg->next_vif].link_conf = link_conf;
11520 		arg->next_vif++;
11521 	}
11522 }
11523 
11524 static u32 ath12k_mac_nlwidth_to_wmiwidth(enum nl80211_chan_width width)
11525 {
11526 	switch (width) {
11527 	case NL80211_CHAN_WIDTH_20:
11528 		return WMI_CHAN_WIDTH_20;
11529 	case NL80211_CHAN_WIDTH_40:
11530 		return WMI_CHAN_WIDTH_40;
11531 	case NL80211_CHAN_WIDTH_80:
11532 		return WMI_CHAN_WIDTH_80;
11533 	case NL80211_CHAN_WIDTH_160:
11534 		return WMI_CHAN_WIDTH_160;
11535 	case NL80211_CHAN_WIDTH_80P80:
11536 		return WMI_CHAN_WIDTH_80P80;
11537 	case NL80211_CHAN_WIDTH_5:
11538 		return WMI_CHAN_WIDTH_5;
11539 	case NL80211_CHAN_WIDTH_10:
11540 		return WMI_CHAN_WIDTH_10;
11541 	case NL80211_CHAN_WIDTH_320:
11542 		return WMI_CHAN_WIDTH_320;
11543 	default:
11544 		WARN_ON(1);
11545 		return WMI_CHAN_WIDTH_20;
11546 	}
11547 }
11548 
11549 static int ath12k_mac_update_peer_puncturing_width(struct ath12k *ar,
11550 						   struct ath12k_link_vif *arvif,
11551 						   struct cfg80211_chan_def def)
11552 {
11553 	u32 param_id, param_value;
11554 	int ret;
11555 
11556 	if (arvif->ahvif->vdev_type != WMI_VDEV_TYPE_STA)
11557 		return 0;
11558 
11559 	param_id = WMI_PEER_CHWIDTH_PUNCTURE_20MHZ_BITMAP;
11560 	param_value = ath12k_mac_nlwidth_to_wmiwidth(def.width) |
11561 		u32_encode_bits((~def.punctured),
11562 				WMI_PEER_PUNCTURE_BITMAP);
11563 
11564 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
11565 		   "punctured bitmap %02x width %d vdev %d\n",
11566 		   def.punctured, def.width, arvif->vdev_id);
11567 
11568 	ret = ath12k_wmi_set_peer_param(ar, arvif->bssid,
11569 					arvif->vdev_id, param_id,
11570 					param_value);
11571 
11572 	return ret;
11573 }
11574 
11575 static void
11576 ath12k_mac_update_vif_chan(struct ath12k *ar,
11577 			   struct ieee80211_vif_chanctx_switch *vifs,
11578 			   int n_vifs)
11579 {
11580 	struct ath12k_incumbent_signal_interference *incumbent;
11581 	struct ath12k_wmi_vdev_up_params params = {};
11582 	struct ieee80211_bss_conf *link_conf;
11583 	struct cfg80211_chan_def *chandef;
11584 	struct ath12k_base *ab = ar->ab;
11585 	struct ath12k_link_vif *arvif;
11586 	struct ieee80211_vif *vif;
11587 	struct ath12k_vif *ahvif;
11588 	u8 link_id;
11589 	int ret;
11590 	int i;
11591 	bool monitor_vif = false;
11592 
11593 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
11594 
11595 	for (i = 0; i < n_vifs; i++) {
11596 		vif = vifs[i].vif;
11597 		ahvif = ath12k_vif_to_ahvif(vif);
11598 		link_conf = vifs[i].link_conf;
11599 		link_id = link_conf->link_id;
11600 		arvif = wiphy_dereference(ath12k_ar_to_hw(ar)->wiphy,
11601 					  ahvif->link[link_id]);
11602 
11603 		if (vif->type == NL80211_IFTYPE_MONITOR) {
11604 			monitor_vif = true;
11605 			continue;
11606 		}
11607 
11608 		if (WARN_ON(!arvif))
11609 			continue;
11610 
11611 		ath12k_dbg(ab, ATH12K_DBG_MAC,
11612 			   "mac chanctx switch vdev_id %i freq %u->%u width %d->%d\n",
11613 			   arvif->vdev_id,
11614 			   vifs[i].old_ctx->def.chan->center_freq,
11615 			   vifs[i].new_ctx->def.chan->center_freq,
11616 			   vifs[i].old_ctx->def.width,
11617 			   vifs[i].new_ctx->def.width);
11618 
11619 		if (WARN_ON(!arvif->is_started))
11620 			continue;
11621 
11622 		arvif->punct_bitmap = vifs[i].new_ctx->def.punctured;
11623 
11624 		/* Firmware expect vdev_restart only if vdev is up.
11625 		 * If vdev is down then it expect vdev_stop->vdev_start.
11626 		 */
11627 		if (arvif->is_up) {
11628 			ret = ath12k_mac_vdev_restart(arvif, vifs[i].new_ctx);
11629 			if (ret) {
11630 				ath12k_warn(ab, "failed to restart vdev %d: %d\n",
11631 					    arvif->vdev_id, ret);
11632 				continue;
11633 			}
11634 		} else {
11635 			ret = ath12k_mac_vdev_stop(arvif);
11636 			if (ret) {
11637 				ath12k_warn(ab, "failed to stop vdev %d: %d\n",
11638 					    arvif->vdev_id, ret);
11639 				continue;
11640 			}
11641 
11642 			ret = ath12k_mac_vdev_start(arvif, vifs[i].new_ctx);
11643 			if (ret)
11644 				ath12k_warn(ab, "failed to start vdev %d: %d\n",
11645 					    arvif->vdev_id, ret);
11646 			continue;
11647 		}
11648 
11649 		ret = ath12k_mac_update_peer_puncturing_width(arvif->ar, arvif,
11650 							      vifs[i].new_ctx->def);
11651 		if (ret) {
11652 			ath12k_warn(ar->ab,
11653 				    "failed to update puncturing bitmap %02x and width %d: %d\n",
11654 				    vifs[i].new_ctx->def.punctured,
11655 				    vifs[i].new_ctx->def.width, ret);
11656 			continue;
11657 		}
11658 
11659 		/* Defer VDEV bring-up during CSA to avoid installing stale
11660 		 * beacon templates. The beacon content is updated only
11661 		 * after CSA finalize, so we mark CSA in progress and skip
11662 		 * VDEV_UP for now. It will be handled later in
11663 		 * bss_info_changed().
11664 		 */
11665 		if (link_conf->csa_active &&
11666 		    arvif->ahvif->vdev_type == WMI_VDEV_TYPE_AP) {
11667 			arvif->is_csa_in_progress = true;
11668 			continue;
11669 		}
11670 
11671 		ret = ath12k_mac_setup_bcn_tmpl(arvif);
11672 		if (ret)
11673 			ath12k_warn(ab, "failed to update bcn tmpl during csa: %d\n",
11674 				    ret);
11675 
11676 		memset(&params, 0, sizeof(params));
11677 		params.vdev_id = arvif->vdev_id;
11678 		params.aid = ahvif->aid;
11679 		params.bssid = arvif->bssid;
11680 		params.tx_bssid = ath12k_mac_get_tx_bssid(arvif);
11681 		if (params.tx_bssid) {
11682 			params.nontx_profile_idx = link_conf->bssid_index;
11683 			params.nontx_profile_cnt = 1 << link_conf->bssid_indicator;
11684 		}
11685 		ret = ath12k_wmi_vdev_up(arvif->ar, &params);
11686 		if (ret) {
11687 			ath12k_warn(ab, "failed to bring vdev up %d: %d\n",
11688 				    arvif->vdev_id, ret);
11689 			continue;
11690 		}
11691 	}
11692 
11693 	/* Restart the internal monitor vdev on new channel */
11694 	if (!monitor_vif && ar->monitor_vdev_created) {
11695 		if (!ath12k_mac_monitor_stop(ar))
11696 			ath12k_mac_monitor_start(ar);
11697 	}
11698 
11699 	incumbent = &ar->incumbent_signal_interference;
11700 	spin_lock_bh(&ar->data_lock);
11701 	if (incumbent->handling_in_progress) {
11702 		chandef = &vifs[0].new_ctx->def;
11703 		if (incumbent->chan_bw_interference_bitmap &
11704 		    ATH12K_WMI_DCS_SEG_PRI20) {
11705 			if (incumbent->center_freq !=
11706 			    chandef->chan->center_freq) {
11707 				incumbent->chan_bw_interference_bitmap = 0;
11708 				incumbent->handling_in_progress = false;
11709 				ath12k_dbg(ab, ATH12K_DBG_MAC,
11710 					   "incumbent signal interference chan switch completed\n");
11711 			} else {
11712 				ath12k_warn(ab,
11713 					    "incumbent signal interference chan switch not done, freq %u\n",
11714 					    incumbent->center_freq);
11715 			}
11716 		} else {
11717 			if (incumbent->center_freq !=
11718 			    chandef->chan->center_freq ||
11719 			    incumbent->width != chandef->width) {
11720 				incumbent->chan_bw_interference_bitmap = 0;
11721 				incumbent->handling_in_progress = false;
11722 				ath12k_dbg(ab, ATH12K_DBG_MAC,
11723 					   "Bandwidth/channel change due to incumbent signal interference completed\n");
11724 			} else {
11725 				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",
11726 					    incumbent->center_freq,
11727 					    chandef->chan->center_freq,
11728 					    incumbent->width,
11729 					    chandef->width);
11730 			}
11731 		}
11732 	}
11733 	spin_unlock_bh(&ar->data_lock);
11734 }
11735 
11736 static void
11737 ath12k_mac_update_active_vif_chan(struct ath12k *ar,
11738 				  struct ieee80211_chanctx_conf *ctx)
11739 {
11740 	struct ath12k_mac_change_chanctx_arg arg = { .ctx = ctx, .ar = ar };
11741 	struct ieee80211_hw *hw = ath12k_ar_to_hw(ar);
11742 
11743 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
11744 
11745 	ieee80211_iterate_active_interfaces_atomic(hw,
11746 						   IEEE80211_IFACE_ITER_NORMAL,
11747 						   ath12k_mac_change_chanctx_cnt_iter,
11748 						   &arg);
11749 	if (arg.n_vifs == 0)
11750 		return;
11751 
11752 	arg.vifs = kzalloc_objs(arg.vifs[0], arg.n_vifs);
11753 	if (!arg.vifs)
11754 		return;
11755 
11756 	ieee80211_iterate_active_interfaces_atomic(hw,
11757 						   IEEE80211_IFACE_ITER_NORMAL,
11758 						   ath12k_mac_change_chanctx_fill_iter,
11759 						   &arg);
11760 
11761 	ath12k_mac_update_vif_chan(ar, arg.vifs, arg.n_vifs);
11762 
11763 	kfree(arg.vifs);
11764 }
11765 
11766 void ath12k_mac_op_change_chanctx(struct ieee80211_hw *hw,
11767 				  struct ieee80211_chanctx_conf *ctx,
11768 				  u32 changed)
11769 {
11770 	struct ath12k *ar;
11771 	struct ath12k_base *ab;
11772 
11773 	lockdep_assert_wiphy(hw->wiphy);
11774 
11775 	ar = ath12k_get_ar_by_ctx(hw, ctx);
11776 	if (!ar)
11777 		return;
11778 
11779 	ab = ar->ab;
11780 
11781 	ath12k_dbg(ab, ATH12K_DBG_MAC,
11782 		   "mac chanctx change freq %u width %d ptr %p changed %x\n",
11783 		   ctx->def.chan->center_freq, ctx->def.width, ctx, changed);
11784 
11785 	/* This shouldn't really happen because channel switching should use
11786 	 * switch_vif_chanctx().
11787 	 */
11788 	if (WARN_ON(changed & IEEE80211_CHANCTX_CHANGE_CHANNEL))
11789 		return;
11790 
11791 	if (changed & IEEE80211_CHANCTX_CHANGE_WIDTH ||
11792 	    changed & IEEE80211_CHANCTX_CHANGE_RADAR ||
11793 	    changed & IEEE80211_CHANCTX_CHANGE_PUNCTURING)
11794 		ath12k_mac_update_active_vif_chan(ar, ctx);
11795 
11796 	/* TODO: Recalc radar detection */
11797 }
11798 EXPORT_SYMBOL(ath12k_mac_op_change_chanctx);
11799 
11800 static int ath12k_start_vdev_delay(struct ath12k *ar,
11801 				   struct ath12k_link_vif *arvif)
11802 {
11803 	struct ath12k_base *ab = ar->ab;
11804 	struct ath12k_vif *ahvif = arvif->ahvif;
11805 	struct ieee80211_vif *vif = ath12k_ahvif_to_vif(arvif->ahvif);
11806 	struct ieee80211_chanctx_conf *chanctx;
11807 	struct ieee80211_bss_conf *link_conf;
11808 	int ret;
11809 
11810 	if (WARN_ON(arvif->is_started))
11811 		return -EBUSY;
11812 
11813 	link_conf = ath12k_mac_get_link_bss_conf(arvif);
11814 	if (!link_conf) {
11815 		ath12k_warn(ab, "failed to get link conf for vdev %u\n", arvif->vdev_id);
11816 		return -EINVAL;
11817 	}
11818 
11819 	chanctx	= wiphy_dereference(ath12k_ar_to_hw(arvif->ar)->wiphy,
11820 				    link_conf->chanctx_conf);
11821 	ret = ath12k_mac_vdev_start(arvif, chanctx);
11822 	if (ret) {
11823 		ath12k_warn(ab, "failed to start vdev %i addr %pM on freq %d: %d\n",
11824 			    arvif->vdev_id, vif->addr,
11825 			    chanctx->def.chan->center_freq, ret);
11826 		return ret;
11827 	}
11828 
11829 	if (ahvif->vdev_type == WMI_VDEV_TYPE_MONITOR) {
11830 		ret = ath12k_monitor_vdev_up(ar, arvif->vdev_id);
11831 		if (ret) {
11832 			ath12k_warn(ab, "failed put monitor up: %d\n", ret);
11833 			return ret;
11834 		}
11835 	}
11836 
11837 	arvif->is_started = true;
11838 
11839 	/* TODO: Setup ps and cts/rts protection */
11840 	return 0;
11841 }
11842 
11843 static u8 ath12k_mac_get_num_pwr_levels(struct cfg80211_chan_def *chan_def)
11844 {
11845 	if (chan_def->chan->flags & IEEE80211_CHAN_PSD) {
11846 		switch (chan_def->width) {
11847 		case NL80211_CHAN_WIDTH_20:
11848 			return 1;
11849 		case NL80211_CHAN_WIDTH_40:
11850 			return 2;
11851 		case NL80211_CHAN_WIDTH_80:
11852 			return 4;
11853 		case NL80211_CHAN_WIDTH_160:
11854 			return 8;
11855 		case NL80211_CHAN_WIDTH_320:
11856 			return 16;
11857 		default:
11858 			return 1;
11859 		}
11860 	} else {
11861 		switch (chan_def->width) {
11862 		case NL80211_CHAN_WIDTH_20:
11863 			return 1;
11864 		case NL80211_CHAN_WIDTH_40:
11865 			return 2;
11866 		case NL80211_CHAN_WIDTH_80:
11867 			return 3;
11868 		case NL80211_CHAN_WIDTH_160:
11869 			return 4;
11870 		case NL80211_CHAN_WIDTH_320:
11871 			return 5;
11872 		default:
11873 			return 1;
11874 		}
11875 	}
11876 }
11877 
11878 static u16 ath12k_mac_get_6ghz_start_frequency(struct cfg80211_chan_def *chan_def)
11879 {
11880 	u16 diff_seq;
11881 
11882 	/* It is to get the lowest channel number's center frequency of the chan.
11883 	 * For example,
11884 	 * bandwidth=40 MHz, center frequency is 5965, lowest channel is 1
11885 	 * with center frequency 5955, its diff is 5965 - 5955 = 10.
11886 	 * bandwidth=80 MHz, center frequency is 5985, lowest channel is 1
11887 	 * with center frequency 5955, its diff is 5985 - 5955 = 30.
11888 	 * bandwidth=160 MHz, center frequency is 6025, lowest channel is 1
11889 	 * with center frequency 5955, its diff is 6025 - 5955 = 70.
11890 	 * bandwidth=320 MHz, center frequency is 6105, lowest channel is 1
11891 	 * with center frequency 5955, its diff is 6105 - 5955 = 70.
11892 	 */
11893 	switch (chan_def->width) {
11894 	case NL80211_CHAN_WIDTH_320:
11895 		diff_seq = 150;
11896 		break;
11897 	case NL80211_CHAN_WIDTH_160:
11898 		diff_seq = 70;
11899 		break;
11900 	case NL80211_CHAN_WIDTH_80:
11901 		diff_seq = 30;
11902 		break;
11903 	case NL80211_CHAN_WIDTH_40:
11904 		diff_seq = 10;
11905 		break;
11906 	default:
11907 		diff_seq = 0;
11908 	}
11909 
11910 	return chan_def->center_freq1 - diff_seq;
11911 }
11912 
11913 static u16 ath12k_mac_get_seg_freq(struct cfg80211_chan_def *chan_def,
11914 				   u16 start_seq, u8 seq)
11915 {
11916 	u16 seg_seq;
11917 
11918 	/* It is to get the center frequency of the specific bandwidth.
11919 	 * start_seq means the lowest channel number's center frequency.
11920 	 * seq 0/1/2/3 means 20 MHz/40 MHz/80 MHz/160 MHz.
11921 	 * For example,
11922 	 * lowest channel is 1, its center frequency 5955,
11923 	 * center frequency is 5955 when bandwidth=20 MHz, its diff is 5955 - 5955 = 0.
11924 	 * lowest channel is 1, its center frequency 5955,
11925 	 * center frequency is 5965 when bandwidth=40 MHz, its diff is 5965 - 5955 = 10.
11926 	 * lowest channel is 1, its center frequency 5955,
11927 	 * center frequency is 5985 when bandwidth=80 MHz, its diff is 5985 - 5955 = 30.
11928 	 * lowest channel is 1, its center frequency 5955,
11929 	 * center frequency is 6025 when bandwidth=160 MHz, its diff is 6025 - 5955 = 70.
11930 	 */
11931 	seg_seq = 10 * (BIT(seq) - 1);
11932 	return seg_seq + start_seq;
11933 }
11934 
11935 static void ath12k_mac_get_psd_channel(struct ath12k *ar,
11936 				       u16 step_freq,
11937 				       u16 *start_freq,
11938 				       u16 *center_freq,
11939 				       u8 i,
11940 				       struct ieee80211_channel **temp_chan,
11941 				       s8 *tx_power)
11942 {
11943 	/* It is to get the center frequency for each 20 MHz.
11944 	 * For example, if the chan is 160 MHz and center frequency is 6025,
11945 	 * then it include 8 channels, they are 1/5/9/13/17/21/25/29,
11946 	 * channel number 1's center frequency is 5955, it is parameter start_freq.
11947 	 * parameter i is the step of the 8 channels. i is 0~7 for the 8 channels.
11948 	 * the channel 1/5/9/13/17/21/25/29 maps i=0/1/2/3/4/5/6/7,
11949 	 * and maps its center frequency is 5955/5975/5995/6015/6035/6055/6075/6095,
11950 	 * the gap is 20 for each channel, parameter step_freq means the gap.
11951 	 * after get the center frequency of each channel, it is easy to find the
11952 	 * struct ieee80211_channel of it and get the max_reg_power.
11953 	 */
11954 	*center_freq = *start_freq + i * step_freq;
11955 	*temp_chan = ieee80211_get_channel(ar->ah->hw->wiphy, *center_freq);
11956 	*tx_power = (*temp_chan)->max_reg_power;
11957 }
11958 
11959 static void ath12k_mac_get_eirp_power(struct ath12k *ar,
11960 				      u16 *start_freq,
11961 				      u16 *center_freq,
11962 				      u8 i,
11963 				      struct ieee80211_channel **temp_chan,
11964 				      struct cfg80211_chan_def *def,
11965 				      s8 *tx_power)
11966 {
11967 	/* It is to get the center frequency for 20 MHz/40 MHz/80 MHz/
11968 	 * 160 MHz bandwidth, and then plus 10 to the center frequency,
11969 	 * it is the center frequency of a channel number.
11970 	 * For example, when configured channel number is 1.
11971 	 * center frequency is 5965 when bandwidth=40 MHz, after plus 10, it is 5975,
11972 	 * then it is channel number 5.
11973 	 * center frequency is 5985 when bandwidth=80 MHz, after plus 10, it is 5995,
11974 	 * then it is channel number 9.
11975 	 * center frequency is 6025 when bandwidth=160 MHz, after plus 10, it is 6035,
11976 	 * then it is channel number 17.
11977 	 * after get the center frequency of each channel, it is easy to find the
11978 	 * struct ieee80211_channel of it and get the max_reg_power.
11979 	 */
11980 	*center_freq = ath12k_mac_get_seg_freq(def, *start_freq, i);
11981 
11982 	/* For the 20 MHz, its center frequency is same with same channel */
11983 	if (i != 0)
11984 		*center_freq += 10;
11985 
11986 	*temp_chan = ieee80211_get_channel(ar->ah->hw->wiphy, *center_freq);
11987 	*tx_power = (*temp_chan)->max_reg_power;
11988 }
11989 
11990 void ath12k_mac_fill_reg_tpc_info(struct ath12k *ar,
11991 				  struct ath12k_link_vif *arvif,
11992 				  struct ieee80211_chanctx_conf *ctx)
11993 {
11994 	struct ath12k_base *ab = ar->ab;
11995 	struct ath12k_reg_tpc_power_info *reg_tpc_info = &arvif->reg_tpc_info;
11996 	struct ieee80211_bss_conf *bss_conf = ath12k_mac_get_link_bss_conf(arvif);
11997 	struct ieee80211_channel *chan, *temp_chan;
11998 	u8 pwr_lvl_idx, num_pwr_levels, pwr_reduction;
11999 	bool is_psd_power = false, is_tpe_present = false;
12000 	s8 max_tx_power[ATH12K_NUM_PWR_LEVELS], psd_power, tx_power;
12001 	s8 eirp_power = 0;
12002 	struct ath12k_vif *ahvif = arvif->ahvif;
12003 	u16 start_freq, center_freq;
12004 	u8 reg_6ghz_power_mode;
12005 
12006 	chan = ctx->def.chan;
12007 	start_freq = ath12k_mac_get_6ghz_start_frequency(&ctx->def);
12008 	pwr_reduction = bss_conf->pwr_reduction;
12009 
12010 	if (arvif->reg_tpc_info.num_pwr_levels) {
12011 		is_tpe_present = true;
12012 		num_pwr_levels = arvif->reg_tpc_info.num_pwr_levels;
12013 	} else {
12014 		num_pwr_levels = ath12k_mac_get_num_pwr_levels(&ctx->def);
12015 	}
12016 
12017 	for (pwr_lvl_idx = 0; pwr_lvl_idx < num_pwr_levels; pwr_lvl_idx++) {
12018 		/* STA received TPE IE*/
12019 		if (is_tpe_present) {
12020 			/* local power is PSD power*/
12021 			if (chan->flags & IEEE80211_CHAN_PSD) {
12022 				/* Connecting AP is psd power */
12023 				if (reg_tpc_info->is_psd_power) {
12024 					is_psd_power = true;
12025 					ath12k_mac_get_psd_channel(ar, 20,
12026 								   &start_freq,
12027 								   &center_freq,
12028 								   pwr_lvl_idx,
12029 								   &temp_chan,
12030 								   &tx_power);
12031 					psd_power = temp_chan->psd;
12032 					eirp_power = tx_power;
12033 					max_tx_power[pwr_lvl_idx] =
12034 						min_t(s8,
12035 						      psd_power,
12036 						      reg_tpc_info->tpe[pwr_lvl_idx]);
12037 				/* Connecting AP is not psd power */
12038 				} else {
12039 					ath12k_mac_get_eirp_power(ar,
12040 								  &start_freq,
12041 								  &center_freq,
12042 								  pwr_lvl_idx,
12043 								  &temp_chan,
12044 								  &ctx->def,
12045 								  &tx_power);
12046 					psd_power = temp_chan->psd;
12047 					/* convert psd power to EIRP power based
12048 					 * on channel width
12049 					 */
12050 					tx_power =
12051 						min_t(s8, tx_power,
12052 						      psd_power + 13 + pwr_lvl_idx * 3);
12053 					max_tx_power[pwr_lvl_idx] =
12054 						min_t(s8,
12055 						      tx_power,
12056 						      reg_tpc_info->tpe[pwr_lvl_idx]);
12057 				}
12058 			/* local power is not PSD power */
12059 			} else {
12060 				/* Connecting AP is psd power */
12061 				if (reg_tpc_info->is_psd_power) {
12062 					is_psd_power = true;
12063 					ath12k_mac_get_psd_channel(ar, 20,
12064 								   &start_freq,
12065 								   &center_freq,
12066 								   pwr_lvl_idx,
12067 								   &temp_chan,
12068 								   &tx_power);
12069 					eirp_power = tx_power;
12070 					max_tx_power[pwr_lvl_idx] =
12071 						reg_tpc_info->tpe[pwr_lvl_idx];
12072 				/* Connecting AP is not psd power */
12073 				} else {
12074 					ath12k_mac_get_eirp_power(ar,
12075 								  &start_freq,
12076 								  &center_freq,
12077 								  pwr_lvl_idx,
12078 								  &temp_chan,
12079 								  &ctx->def,
12080 								  &tx_power);
12081 					max_tx_power[pwr_lvl_idx] =
12082 						min_t(s8,
12083 						      tx_power,
12084 						      reg_tpc_info->tpe[pwr_lvl_idx]);
12085 				}
12086 			}
12087 		/* STA not received TPE IE */
12088 		} else {
12089 			/* local power is PSD power*/
12090 			if (chan->flags & IEEE80211_CHAN_PSD) {
12091 				is_psd_power = true;
12092 				ath12k_mac_get_psd_channel(ar, 20,
12093 							   &start_freq,
12094 							   &center_freq,
12095 							   pwr_lvl_idx,
12096 							   &temp_chan,
12097 							   &tx_power);
12098 				psd_power = temp_chan->psd;
12099 				eirp_power = tx_power;
12100 				max_tx_power[pwr_lvl_idx] = psd_power;
12101 			} else {
12102 				ath12k_mac_get_eirp_power(ar,
12103 							  &start_freq,
12104 							  &center_freq,
12105 							  pwr_lvl_idx,
12106 							  &temp_chan,
12107 							  &ctx->def,
12108 							  &tx_power);
12109 				max_tx_power[pwr_lvl_idx] = tx_power;
12110 			}
12111 		}
12112 
12113 		if (is_psd_power) {
12114 			/* If AP local power constraint is present */
12115 			if (pwr_reduction)
12116 				eirp_power = eirp_power - pwr_reduction;
12117 
12118 			/* If firmware updated max tx power is non zero, then take
12119 			 * the min of firmware updated ap tx power
12120 			 * and max power derived from above mentioned parameters.
12121 			 */
12122 			ath12k_dbg(ab, ATH12K_DBG_MAC,
12123 				   "eirp power : %d firmware report power : %d\n",
12124 				   eirp_power, ar->max_allowed_tx_power);
12125 			/* Firmware reports lower max_allowed_tx_power during vdev
12126 			 * start response. In case of 6 GHz, firmware is not aware
12127 			 * of EIRP power unless driver sets EIRP power through WMI
12128 			 * TPC command. So radio which does not support idle power
12129 			 * save can set maximum calculated EIRP power directly to
12130 			 * firmware through TPC command without min comparison with
12131 			 * vdev start response's max_allowed_tx_power.
12132 			 */
12133 			if (ar->max_allowed_tx_power && ab->hw_params->idle_ps)
12134 				eirp_power = min_t(s8,
12135 						   eirp_power,
12136 						   ar->max_allowed_tx_power);
12137 		} else {
12138 			/* If AP local power constraint is present */
12139 			if (pwr_reduction)
12140 				max_tx_power[pwr_lvl_idx] =
12141 					max_tx_power[pwr_lvl_idx] - pwr_reduction;
12142 			/* If firmware updated max tx power is non zero, then take
12143 			 * the min of firmware updated ap tx power
12144 			 * and max power derived from above mentioned parameters.
12145 			 */
12146 			if (ar->max_allowed_tx_power && ab->hw_params->idle_ps)
12147 				max_tx_power[pwr_lvl_idx] =
12148 					min_t(s8,
12149 					      max_tx_power[pwr_lvl_idx],
12150 					      ar->max_allowed_tx_power);
12151 		}
12152 		reg_tpc_info->chan_power_info[pwr_lvl_idx].chan_cfreq = center_freq;
12153 		reg_tpc_info->chan_power_info[pwr_lvl_idx].tx_power =
12154 			max_tx_power[pwr_lvl_idx];
12155 	}
12156 
12157 	reg_tpc_info->num_pwr_levels = num_pwr_levels;
12158 	reg_tpc_info->is_psd_power = is_psd_power;
12159 	reg_tpc_info->eirp_power = eirp_power;
12160 	if (ahvif->vdev_type == WMI_VDEV_TYPE_STA)
12161 		reg_6ghz_power_mode = bss_conf->power_type;
12162 	else
12163 		/* For now, LPI is the only supported AP power mode */
12164 		reg_6ghz_power_mode = IEEE80211_REG_LPI_AP;
12165 
12166 	reg_tpc_info->ap_power_type =
12167 		ath12k_reg_ap_pwr_convert(reg_6ghz_power_mode);
12168 }
12169 
12170 static void ath12k_mac_parse_tx_pwr_env(struct ath12k *ar,
12171 					struct ath12k_link_vif *arvif)
12172 {
12173 	struct ieee80211_bss_conf *bss_conf = ath12k_mac_get_link_bss_conf(arvif);
12174 	struct ath12k_reg_tpc_power_info *tpc_info = &arvif->reg_tpc_info;
12175 	struct ieee80211_parsed_tpe_eirp *local_non_psd, *reg_non_psd;
12176 	struct ieee80211_parsed_tpe_psd *local_psd, *reg_psd;
12177 	struct ieee80211_parsed_tpe *tpe = &bss_conf->tpe;
12178 	enum wmi_reg_6g_client_type client_type;
12179 	struct ath12k_reg_info *reg_info;
12180 	struct ath12k_base *ab = ar->ab;
12181 	bool psd_valid, non_psd_valid;
12182 	int i;
12183 
12184 	reg_info = ab->reg_info[ar->pdev_idx];
12185 	client_type = reg_info->client_type;
12186 
12187 	local_psd = &tpe->psd_local[client_type];
12188 	reg_psd = &tpe->psd_reg_client[client_type];
12189 	local_non_psd = &tpe->max_local[client_type];
12190 	reg_non_psd = &tpe->max_reg_client[client_type];
12191 
12192 	psd_valid = local_psd->valid | reg_psd->valid;
12193 	non_psd_valid = local_non_psd->valid | reg_non_psd->valid;
12194 
12195 	if (!psd_valid && !non_psd_valid) {
12196 		ath12k_warn(ab,
12197 			    "no transmit power envelope match client power type %d\n",
12198 			    client_type);
12199 		return;
12200 	}
12201 
12202 	if (psd_valid) {
12203 		tpc_info->is_psd_power = true;
12204 
12205 		tpc_info->num_pwr_levels = max(local_psd->count,
12206 					       reg_psd->count);
12207 		tpc_info->num_pwr_levels =
12208 				min3(tpc_info->num_pwr_levels,
12209 				     IEEE80211_TPE_PSD_ENTRIES_320MHZ,
12210 				     ATH12K_NUM_PWR_LEVELS);
12211 
12212 		for (i = 0; i < tpc_info->num_pwr_levels; i++) {
12213 			tpc_info->tpe[i] = min(local_psd->power[i],
12214 					       reg_psd->power[i]) / 2;
12215 			ath12k_dbg(ab, ATH12K_DBG_MAC,
12216 				   "TPE PSD power[%d] : %d\n",
12217 				   i, tpc_info->tpe[i]);
12218 		}
12219 	} else {
12220 		tpc_info->is_psd_power = false;
12221 		tpc_info->eirp_power = 0;
12222 
12223 		tpc_info->num_pwr_levels = max(local_non_psd->count,
12224 					       reg_non_psd->count);
12225 		tpc_info->num_pwr_levels =
12226 				min3(tpc_info->num_pwr_levels,
12227 				     IEEE80211_TPE_EIRP_ENTRIES_320MHZ,
12228 				     ATH12K_NUM_PWR_LEVELS);
12229 
12230 		for (i = 0; i < tpc_info->num_pwr_levels; i++) {
12231 			tpc_info->tpe[i] = min(local_non_psd->power[i],
12232 					       reg_non_psd->power[i]) / 2;
12233 			ath12k_dbg(ab, ATH12K_DBG_MAC,
12234 				   "non PSD power[%d] : %d\n",
12235 				   i, tpc_info->tpe[i]);
12236 		}
12237 	}
12238 }
12239 
12240 int
12241 ath12k_mac_op_assign_vif_chanctx(struct ieee80211_hw *hw,
12242 				 struct ieee80211_vif *vif,
12243 				 struct ieee80211_bss_conf *link_conf,
12244 				 struct ieee80211_chanctx_conf *ctx)
12245 {
12246 	struct ath12k_hw *ah = ath12k_hw_to_ah(hw);
12247 	struct ath12k *ar;
12248 	struct ath12k_base *ab;
12249 	struct ath12k_vif *ahvif = ath12k_vif_to_ahvif(vif);
12250 	u8 link_id = link_conf->link_id;
12251 	struct ath12k_link_vif *arvif;
12252 	int ret;
12253 
12254 	lockdep_assert_wiphy(hw->wiphy);
12255 
12256 	/* For multi radio wiphy, the vdev was not created during add_interface
12257 	 * create now since we have a channel ctx now to assign to a specific ar/fw
12258 	 */
12259 	arvif = ath12k_mac_assign_link_vif(ah, vif, link_id);
12260 	if (!arvif) {
12261 		WARN_ON(1);
12262 		return -ENOMEM;
12263 	}
12264 
12265 	ar = ath12k_mac_assign_vif_to_vdev(hw, arvif, ctx);
12266 	if (!ar) {
12267 		ath12k_hw_warn(ah, "failed to assign chanctx for vif %pM link id %u link vif is already started",
12268 			       vif->addr, link_id);
12269 		return -EINVAL;
12270 	}
12271 
12272 	ab = ar->ab;
12273 
12274 	ath12k_dbg(ab, ATH12K_DBG_MAC,
12275 		   "mac chanctx assign ptr %p vdev_id %i\n",
12276 		   ctx, arvif->vdev_id);
12277 
12278 	if (ath12k_wmi_supports_6ghz_cc_ext(ar) &&
12279 	    ctx->def.chan->band == NL80211_BAND_6GHZ &&
12280 	    ahvif->vdev_type == WMI_VDEV_TYPE_STA)
12281 		ath12k_mac_parse_tx_pwr_env(ar, arvif);
12282 
12283 	arvif->punct_bitmap = ctx->def.punctured;
12284 
12285 	/* for some targets bss peer must be created before vdev_start */
12286 	if (ab->hw_params->vdev_start_delay &&
12287 	    ahvif->vdev_type != WMI_VDEV_TYPE_AP &&
12288 	    ahvif->vdev_type != WMI_VDEV_TYPE_MONITOR &&
12289 	    !ath12k_dp_link_peer_exist_by_vdev_id(ath12k_ab_to_dp(ab), arvif->vdev_id)) {
12290 		ret = 0;
12291 		goto out;
12292 	}
12293 
12294 	if (WARN_ON(arvif->is_started)) {
12295 		ret = -EBUSY;
12296 		goto out;
12297 	}
12298 
12299 	if (ahvif->vdev_type == WMI_VDEV_TYPE_MONITOR) {
12300 		ret = ath12k_mac_monitor_start(ar);
12301 		if (ret) {
12302 			ath12k_mac_monitor_vdev_delete(ar);
12303 			goto out;
12304 		}
12305 
12306 		arvif->is_started = true;
12307 		goto out;
12308 	}
12309 
12310 	ret = ath12k_mac_vdev_start(arvif, ctx);
12311 	if (ret) {
12312 		ath12k_warn(ab, "failed to start vdev %i addr %pM on freq %d: %d\n",
12313 			    arvif->vdev_id, vif->addr,
12314 			    ctx->def.chan->center_freq, ret);
12315 		goto out;
12316 	}
12317 
12318 	arvif->is_started = true;
12319 
12320 	/* TODO: Setup ps and cts/rts protection */
12321 
12322 out:
12323 	return ret;
12324 }
12325 EXPORT_SYMBOL(ath12k_mac_op_assign_vif_chanctx);
12326 
12327 void
12328 ath12k_mac_op_unassign_vif_chanctx(struct ieee80211_hw *hw,
12329 				   struct ieee80211_vif *vif,
12330 				   struct ieee80211_bss_conf *link_conf,
12331 				   struct ieee80211_chanctx_conf *ctx)
12332 {
12333 	struct ath12k *ar;
12334 	struct ath12k_base *ab;
12335 	struct ath12k_vif *ahvif = ath12k_vif_to_ahvif(vif);
12336 	struct ath12k_link_vif *arvif;
12337 	u8 link_id = link_conf->link_id;
12338 	int ret;
12339 
12340 	lockdep_assert_wiphy(hw->wiphy);
12341 
12342 	arvif = wiphy_dereference(hw->wiphy, ahvif->link[link_id]);
12343 
12344 	/* The vif is expected to be attached to an ar's VDEV.
12345 	 * We leave the vif/vdev in this function as is
12346 	 * and not delete the vdev symmetric to assign_vif_chanctx()
12347 	 * the VDEV will be deleted and unassigned either during
12348 	 * remove_interface() or when there is a change in channel
12349 	 * that moves the vif to a new ar
12350 	 */
12351 	if (!arvif || !arvif->is_created)
12352 		return;
12353 
12354 	ar = arvif->ar;
12355 	ab = ar->ab;
12356 
12357 	ath12k_dbg(ab, ATH12K_DBG_MAC,
12358 		   "mac chanctx unassign ptr %p vdev_id %i\n",
12359 		   ctx, arvif->vdev_id);
12360 
12361 	WARN_ON(!arvif->is_started);
12362 
12363 	if (ahvif->vdev_type == WMI_VDEV_TYPE_MONITOR) {
12364 		ret = ath12k_mac_monitor_stop(ar);
12365 		if (ret)
12366 			return;
12367 
12368 		arvif->is_started = false;
12369 	}
12370 
12371 	if (ahvif->vdev_type != WMI_VDEV_TYPE_STA &&
12372 	    ahvif->vdev_type != WMI_VDEV_TYPE_MONITOR) {
12373 		ath12k_bss_disassoc(ar, arvif);
12374 		ret = ath12k_mac_vdev_stop(arvif);
12375 		if (ret)
12376 			ath12k_warn(ab, "failed to stop vdev %i: %d\n",
12377 				    arvif->vdev_id, ret);
12378 	}
12379 	arvif->is_started = false;
12380 
12381 	if (test_bit(WMI_TLV_SERVICE_11D_OFFLOAD, ab->wmi_ab.svc_map) &&
12382 	    ahvif->vdev_type == WMI_VDEV_TYPE_STA &&
12383 	    ahvif->vdev_subtype == WMI_VDEV_SUBTYPE_NONE &&
12384 	    ar->state_11d != ATH12K_11D_PREPARING) {
12385 		reinit_completion(&ar->completed_11d_scan);
12386 		ar->state_11d = ATH12K_11D_PREPARING;
12387 	}
12388 
12389 	if (ar->scan.arvif == arvif && ar->scan.state == ATH12K_SCAN_RUNNING) {
12390 		ath12k_scan_abort(ar);
12391 		ar->scan.arvif = NULL;
12392 	}
12393 }
12394 EXPORT_SYMBOL(ath12k_mac_op_unassign_vif_chanctx);
12395 
12396 int
12397 ath12k_mac_op_switch_vif_chanctx(struct ieee80211_hw *hw,
12398 				 struct ieee80211_vif_chanctx_switch *vifs,
12399 				 int n_vifs,
12400 				 enum ieee80211_chanctx_switch_mode mode)
12401 {
12402 	struct ath12k *curr_ar, *new_ar, *group_ar;
12403 	struct ieee80211_vif_chanctx_switch *v;
12404 	int i, j, count = 0;
12405 
12406 	lockdep_assert_wiphy(hw->wiphy);
12407 
12408 	if (n_vifs == 0)
12409 		return 0;
12410 
12411 	struct ath12k **ar_map __free(kfree) = kzalloc_objs(*ar_map, n_vifs);
12412 
12413 	if (!ar_map)
12414 		return -ENOMEM;
12415 
12416 	for (i = 0; i < n_vifs; i++) {
12417 		v = &vifs[i];
12418 
12419 		if (v->old_ctx->def.chan->band != v->new_ctx->def.chan->band) {
12420 			ath12k_generic_dbg(ATH12K_DBG_MAC,
12421 					   "mac chanctx switch band change not supported\n");
12422 			return -EOPNOTSUPP;
12423 		}
12424 
12425 		curr_ar = ath12k_get_ar_by_ctx(hw, v->old_ctx);
12426 		new_ar = ath12k_get_ar_by_ctx(hw, v->new_ctx);
12427 
12428 		if (!curr_ar || !new_ar) {
12429 			ath12k_generic_dbg(ATH12K_DBG_MAC,
12430 					   "unable to determine device for the passed channel ctx\n");
12431 			ath12k_generic_dbg(ATH12K_DBG_MAC,
12432 					   "Old freq %d MHz (device %s) to new freq %d MHz (device %s)\n",
12433 					   v->old_ctx->def.chan->center_freq,
12434 					   curr_ar ? "valid" : "invalid",
12435 					   v->new_ctx->def.chan->center_freq,
12436 					   new_ar ? "valid" : "invalid");
12437 			return -EINVAL;
12438 		}
12439 
12440 		/* Switching a vif between two radios is not allowed */
12441 		if (curr_ar != new_ar) {
12442 			ath12k_dbg(curr_ar->ab, ATH12K_DBG_MAC,
12443 				   "mac chanctx switch to another radio not supported\n");
12444 			return -EOPNOTSUPP;
12445 		}
12446 
12447 		ar_map[i] = curr_ar;
12448 	}
12449 
12450 	/* Group vifs by radio (ar) and process each group independently. */
12451 	bool *processed __free(kfree) = kzalloc_objs(*processed, n_vifs);
12452 
12453 	if (!processed)
12454 		return -ENOMEM;
12455 
12456 	struct ieee80211_vif_chanctx_switch *group_vifs __free(kfree) =
12457 						kzalloc_objs(*group_vifs, n_vifs);
12458 
12459 	if (!group_vifs)
12460 		return -ENOMEM;
12461 
12462 	for (i = 0; i < n_vifs; i++) {
12463 		if (processed[i])
12464 			continue;
12465 
12466 		group_ar = ar_map[i];
12467 
12468 		count = 0;
12469 		for (j = 0; j < n_vifs; j++) {
12470 			if (!processed[j] && ar_map[j] == group_ar) {
12471 				group_vifs[count++] = vifs[j];
12472 				processed[j] = true;
12473 			}
12474 		}
12475 
12476 		ath12k_dbg(group_ar->ab, ATH12K_DBG_MAC,
12477 			   "mac chanctx switch n_vifs %d mode %d\n",
12478 			   count, mode);
12479 		ath12k_mac_update_vif_chan(group_ar, group_vifs, count);
12480 	}
12481 	return 0;
12482 }
12483 EXPORT_SYMBOL(ath12k_mac_op_switch_vif_chanctx);
12484 
12485 static int
12486 ath12k_set_vdev_param_to_all_vifs(struct ath12k *ar, int param, u32 value)
12487 {
12488 	struct ath12k_link_vif *arvif;
12489 	int ret = 0;
12490 
12491 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
12492 
12493 	list_for_each_entry(arvif, &ar->arvifs, list) {
12494 		ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "setting mac vdev %d param %d value %d\n",
12495 			   param, arvif->vdev_id, value);
12496 
12497 		ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
12498 						    param, value);
12499 		if (ret) {
12500 			ath12k_warn(ar->ab, "failed to set param %d for vdev %d: %d\n",
12501 				    param, arvif->vdev_id, ret);
12502 			break;
12503 		}
12504 	}
12505 
12506 	return ret;
12507 }
12508 
12509 /* mac80211 stores device specific RTS/Fragmentation threshold value,
12510  * this is set interface specific to firmware from ath12k driver
12511  */
12512 int ath12k_mac_op_set_rts_threshold(struct ieee80211_hw *hw,
12513 				    int radio_idx, u32 value)
12514 {
12515 	struct ath12k_hw *ah = ath12k_hw_to_ah(hw);
12516 	struct wiphy *wiphy = hw->wiphy;
12517 	struct ath12k *ar;
12518 	int param_id = WMI_VDEV_PARAM_RTS_THRESHOLD;
12519 	int ret = 0, ret_err, i;
12520 
12521 	lockdep_assert_wiphy(hw->wiphy);
12522 
12523 	if (radio_idx >= wiphy->n_radio || radio_idx < -1)
12524 		return -EINVAL;
12525 
12526 	if (radio_idx != -1) {
12527 		/* Update RTS threshold in specified radio */
12528 		ar = ath12k_ah_to_ar(ah, radio_idx);
12529 		ret = ath12k_set_vdev_param_to_all_vifs(ar, param_id, value);
12530 		if (ret) {
12531 			ath12k_warn(ar->ab,
12532 				    "failed to set RTS config for all vdevs of pdev %d",
12533 				    ar->pdev->pdev_id);
12534 			return ret;
12535 		}
12536 
12537 		ar->rts_threshold = value;
12538 		return 0;
12539 	}
12540 
12541 	/* Radio_index passed is -1, so set RTS threshold for all radios. */
12542 	for_each_ar(ah, ar, i) {
12543 		ret = ath12k_set_vdev_param_to_all_vifs(ar, param_id, value);
12544 		if (ret) {
12545 			ath12k_warn(ar->ab, "failed to set RTS config for all vdevs of pdev %d",
12546 				    ar->pdev->pdev_id);
12547 			break;
12548 		}
12549 	}
12550 	if (!ret) {
12551 		/* Setting new RTS threshold for vdevs of all radios passed, so update
12552 		 * the RTS threshold value for all radios
12553 		 */
12554 		for_each_ar(ah, ar, i)
12555 			ar->rts_threshold = value;
12556 		return 0;
12557 	}
12558 
12559 	/* RTS threshold config failed, revert to the previous RTS threshold */
12560 	for (i = i - 1; i >= 0; i--) {
12561 		ar = ath12k_ah_to_ar(ah, i);
12562 		ret_err = ath12k_set_vdev_param_to_all_vifs(ar, param_id,
12563 							    ar->rts_threshold);
12564 		if (ret_err)
12565 			ath12k_warn(ar->ab,
12566 				    "failed to restore RTS threshold for all vdevs of pdev %d",
12567 				    ar->pdev->pdev_id);
12568 	}
12569 
12570 	return ret;
12571 }
12572 EXPORT_SYMBOL(ath12k_mac_op_set_rts_threshold);
12573 
12574 int ath12k_mac_op_set_frag_threshold(struct ieee80211_hw *hw,
12575 				     int radio_idx, u32 value)
12576 {
12577 	/* Even though there's a WMI vdev param for fragmentation threshold no
12578 	 * known firmware actually implements it. Moreover it is not possible to
12579 	 * rely frame fragmentation to mac80211 because firmware clears the
12580 	 * "more fragments" bit in frame control making it impossible for remote
12581 	 * devices to reassemble frames.
12582 	 *
12583 	 * Hence implement a dummy callback just to say fragmentation isn't
12584 	 * supported. This effectively prevents mac80211 from doing frame
12585 	 * fragmentation in software.
12586 	 */
12587 
12588 	lockdep_assert_wiphy(hw->wiphy);
12589 
12590 	return -EOPNOTSUPP;
12591 }
12592 EXPORT_SYMBOL(ath12k_mac_op_set_frag_threshold);
12593 
12594 static int ath12k_mac_flush(struct ath12k *ar)
12595 {
12596 	long time_left;
12597 	int ret = 0;
12598 
12599 	time_left = wait_event_timeout(ar->dp.tx_empty_waitq,
12600 				       (atomic_read(&ar->dp.num_tx_pending) == 0),
12601 				       ATH12K_FLUSH_TIMEOUT);
12602 	if (time_left == 0) {
12603 		ath12k_warn(ar->ab,
12604 			    "failed to flush transmit queue, data pkts pending %d\n",
12605 			    atomic_read(&ar->dp.num_tx_pending));
12606 		ret = -ETIMEDOUT;
12607 	}
12608 
12609 	time_left = wait_event_timeout(ar->txmgmt_empty_waitq,
12610 				       (atomic_read(&ar->num_pending_mgmt_tx) == 0),
12611 				       ATH12K_FLUSH_TIMEOUT);
12612 	if (time_left == 0) {
12613 		ath12k_warn(ar->ab,
12614 			    "failed to flush mgmt transmit queue, mgmt pkts pending %d\n",
12615 			    atomic_read(&ar->num_pending_mgmt_tx));
12616 		ret = -ETIMEDOUT;
12617 	}
12618 
12619 	return ret;
12620 }
12621 
12622 int ath12k_mac_wait_tx_complete(struct ath12k *ar)
12623 {
12624 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
12625 
12626 	ath12k_mac_drain_tx(ar);
12627 	return ath12k_mac_flush(ar);
12628 }
12629 
12630 void ath12k_mac_op_flush(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
12631 			 u32 queues, bool drop)
12632 {
12633 	struct ath12k_hw *ah = ath12k_hw_to_ah(hw);
12634 	struct ath12k_link_vif *arvif;
12635 	struct ath12k_vif *ahvif;
12636 	unsigned long links;
12637 	struct ath12k *ar;
12638 	u8 link_id;
12639 	int i;
12640 
12641 	lockdep_assert_wiphy(hw->wiphy);
12642 
12643 	if (drop)
12644 		return;
12645 
12646 	for_each_ar(ah, ar, i)
12647 		wiphy_work_flush(hw->wiphy, &ar->wmi_mgmt_tx_work);
12648 
12649 	/* vif can be NULL when flush() is considered for hw */
12650 	if (!vif) {
12651 		for_each_ar(ah, ar, i)
12652 			ath12k_mac_flush(ar);
12653 		return;
12654 	}
12655 
12656 	ahvif = ath12k_vif_to_ahvif(vif);
12657 	links = ahvif->links_map;
12658 	for_each_set_bit(link_id, &links, IEEE80211_MLD_MAX_NUM_LINKS) {
12659 		arvif = wiphy_dereference(hw->wiphy, ahvif->link[link_id]);
12660 		if (!(arvif && arvif->ar))
12661 			continue;
12662 
12663 		ath12k_mac_flush(arvif->ar);
12664 	}
12665 }
12666 EXPORT_SYMBOL(ath12k_mac_op_flush);
12667 
12668 static int
12669 ath12k_mac_bitrate_mask_num_ht_rates(struct ath12k *ar,
12670 				     enum nl80211_band band,
12671 				     const struct cfg80211_bitrate_mask *mask)
12672 {
12673 	int num_rates = 0;
12674 	int i;
12675 
12676 	for (i = 0; i < ARRAY_SIZE(mask->control[band].ht_mcs); i++)
12677 		num_rates += hweight16(mask->control[band].ht_mcs[i]);
12678 
12679 	return num_rates;
12680 }
12681 
12682 static bool
12683 ath12k_mac_has_single_legacy_rate(struct ath12k *ar,
12684 				  enum nl80211_band band,
12685 				  const struct cfg80211_bitrate_mask *mask)
12686 {
12687 	int num_rates = 0;
12688 
12689 	num_rates = hweight32(mask->control[band].legacy);
12690 
12691 	if (ath12k_mac_bitrate_mask_num_ht_rates(ar, band, mask))
12692 		return false;
12693 
12694 	if (ath12k_mac_bitrate_mask_num_vht_rates(ar, band, mask))
12695 		return false;
12696 
12697 	if (ath12k_mac_bitrate_mask_num_he_rates(ar, band, mask))
12698 		return false;
12699 
12700 	if (ath12k_mac_bitrate_mask_num_eht_rates(ar, band, mask))
12701 		return false;
12702 
12703 	return num_rates == 1;
12704 }
12705 
12706 static __le16
12707 ath12k_mac_get_tx_mcs_map(const struct ieee80211_sta_he_cap *he_cap)
12708 {
12709 	if (he_cap->he_cap_elem.phy_cap_info[0] &
12710 	    IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_160MHZ_IN_5G)
12711 		return he_cap->he_mcs_nss_supp.tx_mcs_160;
12712 
12713 	return he_cap->he_mcs_nss_supp.tx_mcs_80;
12714 }
12715 
12716 static bool
12717 ath12k_mac_bitrate_mask_get_single_nss(struct ath12k *ar,
12718 				       struct ieee80211_vif *vif,
12719 				       enum nl80211_band band,
12720 				       const struct cfg80211_bitrate_mask *mask,
12721 				       int *nss)
12722 {
12723 	struct ieee80211_supported_band *sband = &ar->mac.sbands[band];
12724 	u16 vht_mcs_map = le16_to_cpu(sband->vht_cap.vht_mcs.tx_mcs_map);
12725 	const struct ieee80211_sband_iftype_data *data;
12726 	const struct ieee80211_sta_he_cap *he_cap;
12727 	u16 he_mcs_map = 0;
12728 	u16 eht_mcs_map = 0;
12729 	u8 ht_nss_mask = 0;
12730 	u8 vht_nss_mask = 0;
12731 	u8 he_nss_mask = 0;
12732 	u8 eht_nss_mask = 0;
12733 	u8 mcs_nss_len;
12734 	int i;
12735 
12736 	/* No need to consider legacy here. Basic rates are always present
12737 	 * in bitrate mask
12738 	 */
12739 
12740 	for (i = 0; i < ARRAY_SIZE(mask->control[band].ht_mcs); i++) {
12741 		if (mask->control[band].ht_mcs[i] == 0)
12742 			continue;
12743 		else if (mask->control[band].ht_mcs[i] ==
12744 			 sband->ht_cap.mcs.rx_mask[i])
12745 			ht_nss_mask |= BIT(i);
12746 		else
12747 			return false;
12748 	}
12749 
12750 	for (i = 0; i < ARRAY_SIZE(mask->control[band].vht_mcs); i++) {
12751 		if (mask->control[band].vht_mcs[i] == 0)
12752 			continue;
12753 		else if (mask->control[band].vht_mcs[i] ==
12754 			 ath12k_mac_get_max_vht_mcs_map(vht_mcs_map, i))
12755 			vht_nss_mask |= BIT(i);
12756 		else
12757 			return false;
12758 	}
12759 
12760 	he_cap = ieee80211_get_he_iftype_cap_vif(sband, vif);
12761 	if (!he_cap)
12762 		return false;
12763 
12764 	he_mcs_map = le16_to_cpu(ath12k_mac_get_tx_mcs_map(he_cap));
12765 
12766 	for (i = 0; i < ARRAY_SIZE(mask->control[band].he_mcs); i++) {
12767 		if (mask->control[band].he_mcs[i] == 0)
12768 			continue;
12769 
12770 		if (mask->control[band].he_mcs[i] ==
12771 		    ath12k_mac_get_max_he_mcs_map(he_mcs_map, i))
12772 			he_nss_mask |= BIT(i);
12773 		else
12774 			return false;
12775 	}
12776 
12777 	data = ieee80211_get_sband_iftype_data(sband, vif->type);
12778 
12779 	mcs_nss_len = ieee80211_eht_mcs_nss_size(&data->he_cap.he_cap_elem,
12780 						 &data->eht_cap.eht_cap_elem,
12781 						 false);
12782 	if (mcs_nss_len == 4) {
12783 		/* 20 MHz only STA case */
12784 		const struct ieee80211_eht_mcs_nss_supp_20mhz_only *eht_mcs_nss =
12785 			&data->eht_cap.eht_mcs_nss_supp.only_20mhz;
12786 		if (eht_mcs_nss->rx_tx_mcs13_max_nss)
12787 			eht_mcs_map = 0x1fff;
12788 		else if (eht_mcs_nss->rx_tx_mcs11_max_nss)
12789 			eht_mcs_map = 0x07ff;
12790 		else if (eht_mcs_nss->rx_tx_mcs9_max_nss)
12791 			eht_mcs_map = 0x01ff;
12792 		else
12793 			eht_mcs_map = 0x007f;
12794 	} else {
12795 		const struct ieee80211_eht_mcs_nss_supp_bw *eht_mcs_nss;
12796 
12797 		switch (mcs_nss_len) {
12798 		case 9:
12799 			eht_mcs_nss = &data->eht_cap.eht_mcs_nss_supp.bw._320;
12800 			break;
12801 		case 6:
12802 			eht_mcs_nss = &data->eht_cap.eht_mcs_nss_supp.bw._160;
12803 			break;
12804 		case 3:
12805 			eht_mcs_nss = &data->eht_cap.eht_mcs_nss_supp.bw._80;
12806 			break;
12807 		default:
12808 			return false;
12809 		}
12810 
12811 		if (eht_mcs_nss->rx_tx_mcs13_max_nss)
12812 			eht_mcs_map = 0x1fff;
12813 		else if (eht_mcs_nss->rx_tx_mcs11_max_nss)
12814 			eht_mcs_map = 0x7ff;
12815 		else
12816 			eht_mcs_map = 0x1ff;
12817 	}
12818 
12819 	for (i = 0; i < ARRAY_SIZE(mask->control[band].eht_mcs); i++) {
12820 		if (mask->control[band].eht_mcs[i] == 0)
12821 			continue;
12822 
12823 		if (mask->control[band].eht_mcs[i] < eht_mcs_map)
12824 			eht_nss_mask |= BIT(i);
12825 		else
12826 			return false;
12827 	}
12828 
12829 	if (ht_nss_mask != vht_nss_mask || ht_nss_mask != he_nss_mask ||
12830 	    ht_nss_mask != eht_nss_mask)
12831 		return false;
12832 
12833 	if (ht_nss_mask == 0)
12834 		return false;
12835 
12836 	if (BIT(fls(ht_nss_mask)) - 1 != ht_nss_mask)
12837 		return false;
12838 
12839 	*nss = fls(ht_nss_mask);
12840 
12841 	return true;
12842 }
12843 
12844 static int
12845 ath12k_mac_get_single_legacy_rate(struct ath12k *ar,
12846 				  enum nl80211_band band,
12847 				  const struct cfg80211_bitrate_mask *mask,
12848 				  u32 *rate, u8 *nss)
12849 {
12850 	int rate_idx;
12851 	u16 bitrate;
12852 	u8 preamble;
12853 	u8 hw_rate;
12854 
12855 	if (hweight32(mask->control[band].legacy) != 1)
12856 		return -EINVAL;
12857 
12858 	rate_idx = ffs(mask->control[band].legacy) - 1;
12859 
12860 	if (band == NL80211_BAND_5GHZ || band == NL80211_BAND_6GHZ)
12861 		rate_idx += ATH12K_MAC_FIRST_OFDM_RATE_IDX;
12862 
12863 	hw_rate = ath12k_legacy_rates[rate_idx].hw_value;
12864 	bitrate = ath12k_legacy_rates[rate_idx].bitrate;
12865 
12866 	if (ath12k_mac_bitrate_is_cck(bitrate))
12867 		preamble = WMI_RATE_PREAMBLE_CCK;
12868 	else
12869 		preamble = WMI_RATE_PREAMBLE_OFDM;
12870 
12871 	*nss = 1;
12872 	*rate = ATH12K_HW_RATE_CODE(hw_rate, 0, preamble);
12873 
12874 	return 0;
12875 }
12876 
12877 static int
12878 ath12k_mac_set_fixed_rate_gi_ltf(struct ath12k_link_vif *arvif, u8 gi, u8 ltf,
12879 				 u32 param)
12880 {
12881 	struct ath12k *ar = arvif->ar;
12882 	int ret;
12883 
12884 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
12885 
12886 	/* 0.8 = 0, 1.6 = 2 and 3.2 = 3. */
12887 	if (gi && gi != 0xFF)
12888 		gi += 1;
12889 
12890 	ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
12891 					    WMI_VDEV_PARAM_SGI, gi);
12892 	if (ret) {
12893 		ath12k_warn(ar->ab, "failed to set GI:%d, error:%d\n",
12894 			    gi, ret);
12895 		return ret;
12896 	}
12897 
12898 	if (param == WMI_VDEV_PARAM_HE_LTF) {
12899 		/* HE values start from 1 */
12900 		if (ltf != 0xFF)
12901 			ltf += 1;
12902 	} else {
12903 		/* EHT values start from 5 */
12904 		if (ltf != 0xFF)
12905 			ltf += 4;
12906 	}
12907 
12908 	ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
12909 					    param, ltf);
12910 	if (ret) {
12911 		ath12k_warn(ar->ab, "failed to set LTF:%d, error:%d\n",
12912 			    ltf, ret);
12913 		return ret;
12914 	}
12915 	return 0;
12916 }
12917 
12918 static int
12919 ath12k_mac_set_auto_rate_gi_ltf(struct ath12k_link_vif *arvif, u16 gi, u8 ltf)
12920 {
12921 	struct ath12k *ar = arvif->ar;
12922 	int ret;
12923 	u32 ar_gi_ltf;
12924 
12925 	if (gi != 0xFF) {
12926 		switch (gi) {
12927 		case ATH12K_RATE_INFO_GI_0_8:
12928 			gi = WMI_AUTORATE_800NS_GI;
12929 			break;
12930 		case ATH12K_RATE_INFO_GI_1_6:
12931 			gi = WMI_AUTORATE_1600NS_GI;
12932 			break;
12933 		case ATH12K_RATE_INFO_GI_3_2:
12934 			gi = WMI_AUTORATE_3200NS_GI;
12935 			break;
12936 		default:
12937 			ath12k_warn(ar->ab, "Invalid GI\n");
12938 			return -EINVAL;
12939 		}
12940 	}
12941 
12942 	if (ltf != 0xFF) {
12943 		switch (ltf) {
12944 		case ATH12K_RATE_INFO_1XLTF:
12945 			ltf = WMI_AUTORATE_LTF_1X;
12946 			break;
12947 		case ATH12K_RATE_INFO_2XLTF:
12948 			ltf = WMI_AUTORATE_LTF_2X;
12949 			break;
12950 		case ATH12K_RATE_INFO_4XLTF:
12951 			ltf = WMI_AUTORATE_LTF_4X;
12952 			break;
12953 		default:
12954 			ath12k_warn(ar->ab, "Invalid LTF\n");
12955 			return -EINVAL;
12956 		}
12957 	}
12958 
12959 	ar_gi_ltf = gi | ltf;
12960 
12961 	ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
12962 					    WMI_VDEV_PARAM_AUTORATE_MISC_CFG,
12963 					    ar_gi_ltf);
12964 	if (ret) {
12965 		ath12k_warn(ar->ab,
12966 			    "failed to set autorate GI:%u, LTF:%u params, error:%d\n",
12967 			    gi, ltf, ret);
12968 		return ret;
12969 	}
12970 
12971 	return 0;
12972 }
12973 
12974 static u32 ath12k_mac_nlgi_to_wmigi(enum nl80211_txrate_gi gi)
12975 {
12976 	switch (gi) {
12977 	case NL80211_TXRATE_DEFAULT_GI:
12978 		return WMI_GI_400_NS;
12979 	case NL80211_TXRATE_FORCE_LGI:
12980 		return WMI_GI_800_NS;
12981 	default:
12982 		return WMI_GI_400_NS;
12983 	}
12984 }
12985 
12986 static int ath12k_mac_set_rate_params(struct ath12k_link_vif *arvif,
12987 				      u32 rate, u8 nss, u8 sgi, u8 ldpc,
12988 				      u8 he_gi, u8 he_ltf, bool he_fixed_rate,
12989 				      u8 eht_gi, u8 eht_ltf,
12990 				      bool eht_fixed_rate)
12991 {
12992 	struct ieee80211_bss_conf *link_conf;
12993 	struct ath12k *ar = arvif->ar;
12994 	bool he_support, eht_support, gi_ltf_set = false;
12995 	u32 vdev_param;
12996 	u32 param_value;
12997 	int ret;
12998 
12999 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
13000 
13001 	link_conf = ath12k_mac_get_link_bss_conf(arvif);
13002 	if (!link_conf)
13003 		return -EINVAL;
13004 
13005 	he_support = link_conf->he_support;
13006 	eht_support = link_conf->eht_support;
13007 
13008 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
13009 		   "mac set rate params vdev %i rate 0x%02x nss 0x%02x sgi 0x%02x ldpc 0x%02x\n",
13010 		   arvif->vdev_id, rate, nss, sgi, ldpc);
13011 
13012 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
13013 		   "he_gi 0x%02x he_ltf 0x%02x he_fixed_rate %d\n", he_gi,
13014 		   he_ltf, he_fixed_rate);
13015 
13016 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
13017 		   "eht_gi 0x%02x eht_ltf 0x%02x eht_fixed_rate %d\n",
13018 		   eht_gi, eht_ltf, eht_fixed_rate);
13019 
13020 	if (!he_support && !eht_support) {
13021 		vdev_param = WMI_VDEV_PARAM_FIXED_RATE;
13022 		ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
13023 						    vdev_param, rate);
13024 		if (ret) {
13025 			ath12k_warn(ar->ab, "failed to set fixed rate param 0x%02x: %d\n",
13026 				    rate, ret);
13027 			return ret;
13028 		}
13029 	}
13030 
13031 	vdev_param = WMI_VDEV_PARAM_NSS;
13032 
13033 	ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
13034 					    vdev_param, nss);
13035 	if (ret) {
13036 		ath12k_warn(ar->ab, "failed to set nss param %d: %d\n",
13037 			    nss, ret);
13038 		return ret;
13039 	}
13040 
13041 	ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
13042 					    WMI_VDEV_PARAM_LDPC, ldpc);
13043 	if (ret) {
13044 		ath12k_warn(ar->ab, "failed to set ldpc param %d: %d\n",
13045 			    ldpc, ret);
13046 		return ret;
13047 	}
13048 
13049 	if (eht_support) {
13050 		if (eht_fixed_rate)
13051 			ret = ath12k_mac_set_fixed_rate_gi_ltf(arvif, eht_gi, eht_ltf,
13052 							       WMI_VDEV_PARAM_EHT_LTF);
13053 		else
13054 			ret = ath12k_mac_set_auto_rate_gi_ltf(arvif, eht_gi, eht_ltf);
13055 
13056 		if (ret) {
13057 			ath12k_warn(ar->ab,
13058 				    "failed to set EHT LTF/GI params %d/%d: %d\n",
13059 				    eht_gi, eht_ltf, ret);
13060 			return ret;
13061 		}
13062 		gi_ltf_set = true;
13063 	}
13064 
13065 	if (he_support) {
13066 		if (he_fixed_rate)
13067 			ret = ath12k_mac_set_fixed_rate_gi_ltf(arvif, he_gi, he_ltf,
13068 							       WMI_VDEV_PARAM_HE_LTF);
13069 		else
13070 			ret = ath12k_mac_set_auto_rate_gi_ltf(arvif, he_gi, he_ltf);
13071 		if (ret)
13072 			return ret;
13073 		gi_ltf_set = true;
13074 	}
13075 
13076 	if (!gi_ltf_set) {
13077 		vdev_param = WMI_VDEV_PARAM_SGI;
13078 		param_value = ath12k_mac_nlgi_to_wmigi(sgi);
13079 		ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
13080 						    vdev_param, param_value);
13081 		if (ret) {
13082 			ath12k_warn(ar->ab, "failed to set sgi param %d: %d\n",
13083 				    sgi, ret);
13084 			return ret;
13085 		}
13086 	}
13087 
13088 	return 0;
13089 }
13090 
13091 static bool
13092 ath12k_mac_vht_mcs_range_present(struct ath12k *ar,
13093 				 enum nl80211_band band,
13094 				 const struct cfg80211_bitrate_mask *mask)
13095 {
13096 	int i;
13097 	u16 vht_mcs;
13098 
13099 	for (i = 0; i < NL80211_VHT_NSS_MAX; i++) {
13100 		vht_mcs = mask->control[band].vht_mcs[i];
13101 
13102 		switch (vht_mcs) {
13103 		case 0:
13104 		case BIT(8) - 1:
13105 		case BIT(9) - 1:
13106 		case BIT(10) - 1:
13107 			break;
13108 		default:
13109 			return false;
13110 		}
13111 	}
13112 
13113 	return true;
13114 }
13115 
13116 static bool
13117 ath12k_mac_he_mcs_range_present(struct ath12k *ar,
13118 				enum nl80211_band band,
13119 				const struct cfg80211_bitrate_mask *mask)
13120 {
13121 	int i;
13122 	u16 he_mcs;
13123 
13124 	for (i = 0; i < NL80211_HE_NSS_MAX; i++) {
13125 		he_mcs = mask->control[band].he_mcs[i];
13126 
13127 		switch (he_mcs) {
13128 		case 0:
13129 		case BIT(8) - 1:
13130 		case BIT(10) - 1:
13131 		case BIT(12) - 1:
13132 			break;
13133 		default:
13134 			return false;
13135 		}
13136 	}
13137 
13138 	return true;
13139 }
13140 
13141 static bool
13142 ath12k_mac_eht_mcs_range_present(struct ath12k *ar,
13143 				 enum nl80211_band band,
13144 				 const struct cfg80211_bitrate_mask *mask)
13145 {
13146 	u16 eht_mcs;
13147 	int i;
13148 
13149 	for (i = 0; i < NL80211_EHT_NSS_MAX; i++) {
13150 		eht_mcs = mask->control[band].eht_mcs[i];
13151 
13152 		switch (eht_mcs) {
13153 		case 0:
13154 		case BIT(8) - 1:
13155 		case BIT(10) - 1:
13156 		case BIT(12) - 1:
13157 		case BIT(14) - 1:
13158 			break;
13159 		case BIT(15) - 1:
13160 		case BIT(16) - 1:
13161 		case BIT(16) - BIT(14) - 1:
13162 			if (i != 0)
13163 				return false;
13164 			break;
13165 		default:
13166 			return false;
13167 		}
13168 	}
13169 
13170 	return true;
13171 }
13172 
13173 static void ath12k_mac_set_bitrate_mask_iter(void *data,
13174 					     struct ieee80211_sta *sta)
13175 {
13176 	struct ath12k_link_vif *arvif = data;
13177 	struct ath12k_sta *ahsta = ath12k_sta_to_ahsta(sta);
13178 	struct ath12k_link_sta *arsta;
13179 	struct ath12k *ar = arvif->ar;
13180 
13181 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
13182 
13183 	arsta = wiphy_dereference(ath12k_ar_to_hw(ar)->wiphy,
13184 				  ahsta->link[arvif->link_id]);
13185 	if (!arsta || arsta->arvif != arvif)
13186 		return;
13187 
13188 	spin_lock_bh(&ar->data_lock);
13189 	arsta->changed |= IEEE80211_RC_SUPP_RATES_CHANGED;
13190 	spin_unlock_bh(&ar->data_lock);
13191 
13192 	wiphy_work_queue(ath12k_ar_to_hw(ar)->wiphy, &arsta->update_wk);
13193 }
13194 
13195 static void ath12k_mac_disable_peer_fixed_rate(void *data,
13196 					       struct ieee80211_sta *sta)
13197 {
13198 	struct ath12k_link_vif *arvif = data;
13199 	struct ath12k_sta *ahsta = ath12k_sta_to_ahsta(sta);
13200 	struct ath12k_link_sta *arsta;
13201 	struct ath12k *ar = arvif->ar;
13202 	int ret;
13203 
13204 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
13205 
13206 	arsta = wiphy_dereference(ath12k_ar_to_hw(ar)->wiphy,
13207 				  ahsta->link[arvif->link_id]);
13208 
13209 	if (!arsta || arsta->arvif != arvif)
13210 		return;
13211 
13212 	ret = ath12k_wmi_set_peer_param(ar, arsta->addr,
13213 					arvif->vdev_id,
13214 					WMI_PEER_PARAM_FIXED_RATE,
13215 					WMI_FIXED_RATE_NONE);
13216 	if (ret)
13217 		ath12k_warn(ar->ab,
13218 			    "failed to disable peer fixed rate for STA %pM ret %d\n",
13219 			    arsta->addr, ret);
13220 }
13221 
13222 static bool
13223 ath12k_mac_validate_fixed_rate_settings(struct ath12k *ar, enum nl80211_band band,
13224 					const struct cfg80211_bitrate_mask *mask,
13225 					unsigned int link_id)
13226 {
13227 	bool eht_fixed_rate = false, he_fixed_rate = false, vht_fixed_rate = false;
13228 	const u16 *vht_mcs_mask, *he_mcs_mask, *eht_mcs_mask;
13229 	struct ieee80211_link_sta *link_sta;
13230 	struct ath12k_dp_link_peer *peer, *tmp;
13231 	u8 vht_nss, he_nss, eht_nss;
13232 	int ret = true;
13233 	struct ath12k_base *ab = ar->ab;
13234 	struct ath12k_dp *dp = ath12k_ab_to_dp(ab);
13235 
13236 	vht_mcs_mask = mask->control[band].vht_mcs;
13237 	he_mcs_mask = mask->control[band].he_mcs;
13238 	eht_mcs_mask = mask->control[band].eht_mcs;
13239 
13240 	if (ath12k_mac_bitrate_mask_num_vht_rates(ar, band, mask) == 1)
13241 		vht_fixed_rate = true;
13242 
13243 	if (ath12k_mac_bitrate_mask_num_he_rates(ar, band, mask) == 1)
13244 		he_fixed_rate = true;
13245 
13246 	if (ath12k_mac_bitrate_mask_num_eht_rates(ar, band, mask) == 1)
13247 		eht_fixed_rate = true;
13248 
13249 	if (!vht_fixed_rate && !he_fixed_rate && !eht_fixed_rate)
13250 		return true;
13251 
13252 	vht_nss = ath12k_mac_max_vht_nss(vht_mcs_mask);
13253 	he_nss =  ath12k_mac_max_he_nss(he_mcs_mask);
13254 	eht_nss = ath12k_mac_max_eht_nss(eht_mcs_mask);
13255 
13256 	rcu_read_lock();
13257 	spin_lock_bh(&dp->dp_lock);
13258 	list_for_each_entry_safe(peer, tmp, &dp->peers, list) {
13259 		if (peer->sta) {
13260 			link_sta = rcu_dereference(peer->sta->link[link_id]);
13261 			if (!link_sta) {
13262 				ret = false;
13263 				goto exit;
13264 			}
13265 
13266 			if (vht_fixed_rate && (!link_sta->vht_cap.vht_supported ||
13267 					       link_sta->rx_nss < vht_nss)) {
13268 				ret = false;
13269 				goto exit;
13270 			}
13271 			if (he_fixed_rate && (!link_sta->he_cap.has_he ||
13272 					      link_sta->rx_nss < he_nss)) {
13273 				ret = false;
13274 				goto exit;
13275 			}
13276 			if (eht_fixed_rate && (!link_sta->eht_cap.has_eht ||
13277 					       link_sta->rx_nss < eht_nss)) {
13278 				ret = false;
13279 				goto exit;
13280 			}
13281 		}
13282 	}
13283 exit:
13284 	spin_unlock_bh(&dp->dp_lock);
13285 	rcu_read_unlock();
13286 	return ret;
13287 }
13288 
13289 int
13290 ath12k_mac_op_set_bitrate_mask(struct ieee80211_hw *hw,
13291 			       struct ieee80211_vif *vif,
13292 			       const struct cfg80211_bitrate_mask *mask)
13293 {
13294 	struct ath12k_vif *ahvif = ath12k_vif_to_ahvif(vif);
13295 	struct ath12k_link_vif *arvif;
13296 	struct cfg80211_chan_def def;
13297 	struct ath12k *ar;
13298 	enum nl80211_band band;
13299 	const u8 *ht_mcs_mask;
13300 	const u16 *vht_mcs_mask;
13301 	const u16 *he_mcs_mask;
13302 	const u16 *eht_mcs_mask;
13303 	u8 he_ltf = 0;
13304 	u8 he_gi = 0;
13305 	u8 eht_ltf = 0, eht_gi = 0;
13306 	u32 rate;
13307 	u8 nss, mac_nss;
13308 	u8 sgi;
13309 	u8 ldpc;
13310 	int single_nss;
13311 	int ret;
13312 	int num_rates;
13313 	bool he_fixed_rate = false;
13314 	bool eht_fixed_rate = false;
13315 
13316 	lockdep_assert_wiphy(hw->wiphy);
13317 
13318 	arvif = &ahvif->deflink;
13319 
13320 	ar = arvif->ar;
13321 	if (ath12k_mac_vif_link_chan(vif, arvif->link_id, &def)) {
13322 		ret = -EPERM;
13323 		goto out;
13324 	}
13325 
13326 	band = def.chan->band;
13327 	ht_mcs_mask = mask->control[band].ht_mcs;
13328 	vht_mcs_mask = mask->control[band].vht_mcs;
13329 	he_mcs_mask = mask->control[band].he_mcs;
13330 	eht_mcs_mask = mask->control[band].eht_mcs;
13331 	ldpc = !!(ar->ht_cap_info & WMI_HT_CAP_LDPC);
13332 
13333 	sgi = mask->control[band].gi;
13334 	if (sgi == NL80211_TXRATE_FORCE_SGI) {
13335 		ret = -EINVAL;
13336 		goto out;
13337 	}
13338 
13339 	he_gi = mask->control[band].he_gi;
13340 	he_ltf = mask->control[band].he_ltf;
13341 
13342 	eht_gi = mask->control[band].eht_gi;
13343 	eht_ltf = mask->control[band].eht_ltf;
13344 
13345 	/* mac80211 doesn't support sending a fixed HT/VHT MCS alone, rather it
13346 	 * requires passing at least one of used basic rates along with them.
13347 	 * Fixed rate setting across different preambles(legacy, HT, VHT) is
13348 	 * not supported by the FW. Hence use of FIXED_RATE vdev param is not
13349 	 * suitable for setting single HT/VHT rates.
13350 	 * But, there could be a single basic rate passed from userspace which
13351 	 * can be done through the FIXED_RATE param.
13352 	 */
13353 	if (ath12k_mac_has_single_legacy_rate(ar, band, mask)) {
13354 		ret = ath12k_mac_get_single_legacy_rate(ar, band, mask, &rate,
13355 							&nss);
13356 		if (ret) {
13357 			ath12k_warn(ar->ab, "failed to get single legacy rate for vdev %i: %d\n",
13358 				    arvif->vdev_id, ret);
13359 			goto out;
13360 		}
13361 
13362 		ieee80211_iterate_stations_mtx(hw,
13363 					       ath12k_mac_disable_peer_fixed_rate,
13364 					       arvif);
13365 	} else if (ath12k_mac_bitrate_mask_get_single_nss(ar, vif, band, mask,
13366 							  &single_nss)) {
13367 		rate = WMI_FIXED_RATE_NONE;
13368 		nss = single_nss;
13369 		arvif->bitrate_mask = *mask;
13370 
13371 		ieee80211_iterate_stations_atomic(hw,
13372 						  ath12k_mac_set_bitrate_mask_iter,
13373 						  arvif);
13374 	} else {
13375 		rate = WMI_FIXED_RATE_NONE;
13376 
13377 		if (!ath12k_mac_validate_fixed_rate_settings(ar, band,
13378 							     mask, arvif->link_id))
13379 			ath12k_warn(ar->ab,
13380 				    "failed to update fixed rate settings due to mcs/nss incompatibility\n");
13381 
13382 		mac_nss = max(max3(ath12k_mac_max_ht_nss(ht_mcs_mask),
13383 				   ath12k_mac_max_vht_nss(vht_mcs_mask),
13384 				   ath12k_mac_max_he_nss(he_mcs_mask)),
13385 			       ath12k_mac_max_eht_nss(eht_mcs_mask));
13386 		nss = min_t(u32, ar->num_tx_chains, mac_nss);
13387 
13388 		/* If multiple rates across different preambles are given
13389 		 * we can reconfigure this info with all peers using PEER_ASSOC
13390 		 * command with the below exception cases.
13391 		 * - Single VHT Rate : peer_assoc command accommodates only MCS
13392 		 * range values i.e 0-7, 0-8, 0-9 for VHT. Though mac80211
13393 		 * mandates passing basic rates along with HT/VHT rates, FW
13394 		 * doesn't allow switching from VHT to Legacy. Hence instead of
13395 		 * setting legacy and VHT rates using RATEMASK_CMD vdev cmd,
13396 		 * we could set this VHT rate as peer fixed rate param, which
13397 		 * will override FIXED rate and FW rate control algorithm.
13398 		 * If single VHT rate is passed along with HT rates, we select
13399 		 * the VHT rate as fixed rate for vht peers.
13400 		 * - Multiple VHT Rates : When Multiple VHT rates are given,this
13401 		 * can be set using RATEMASK CMD which uses FW rate-ctl alg.
13402 		 * TODO: Setting multiple VHT MCS and replacing peer_assoc with
13403 		 * RATEMASK_CMDID can cover all use cases of setting rates
13404 		 * across multiple preambles and rates within same type.
13405 		 * But requires more validation of the command at this point.
13406 		 */
13407 
13408 		num_rates = ath12k_mac_bitrate_mask_num_vht_rates(ar, band,
13409 								  mask);
13410 
13411 		if (!ath12k_mac_vht_mcs_range_present(ar, band, mask) &&
13412 		    num_rates > 1) {
13413 			/* TODO: Handle multiple VHT MCS values setting using
13414 			 * RATEMASK CMD
13415 			 */
13416 			ath12k_warn(ar->ab,
13417 				    "Setting more than one MCS Value in bitrate mask not supported\n");
13418 			ret = -EINVAL;
13419 			goto out;
13420 		}
13421 
13422 		num_rates = ath12k_mac_bitrate_mask_num_he_rates(ar, band, mask);
13423 		if (num_rates == 1)
13424 			he_fixed_rate = true;
13425 
13426 		if (!ath12k_mac_he_mcs_range_present(ar, band, mask) &&
13427 		    num_rates > 1) {
13428 			ath12k_warn(ar->ab,
13429 				    "Setting more than one HE MCS Value in bitrate mask not supported\n");
13430 			ret = -EINVAL;
13431 			goto out;
13432 		}
13433 
13434 		num_rates = ath12k_mac_bitrate_mask_num_eht_rates(ar, band,
13435 								  mask);
13436 		if (num_rates == 1)
13437 			eht_fixed_rate = true;
13438 
13439 		if (!ath12k_mac_eht_mcs_range_present(ar, band, mask) &&
13440 		    num_rates > 1) {
13441 			ath12k_warn(ar->ab,
13442 				    "Setting more than one EHT MCS Value in bitrate mask not supported\n");
13443 			ret = -EINVAL;
13444 			goto out;
13445 		}
13446 
13447 		ieee80211_iterate_stations_mtx(hw,
13448 					       ath12k_mac_disable_peer_fixed_rate,
13449 					       arvif);
13450 
13451 		arvif->bitrate_mask = *mask;
13452 		ieee80211_iterate_stations_mtx(hw,
13453 					       ath12k_mac_set_bitrate_mask_iter,
13454 					       arvif);
13455 	}
13456 
13457 	ret = ath12k_mac_set_rate_params(arvif, rate, nss, sgi, ldpc, he_gi,
13458 					 he_ltf, he_fixed_rate, eht_gi, eht_ltf,
13459 					 eht_fixed_rate);
13460 	if (ret) {
13461 		ath12k_warn(ar->ab, "failed to set rate params on vdev %i: %d\n",
13462 			    arvif->vdev_id, ret);
13463 	}
13464 
13465 out:
13466 	return ret;
13467 }
13468 EXPORT_SYMBOL(ath12k_mac_op_set_bitrate_mask);
13469 
13470 void
13471 ath12k_mac_op_reconfig_complete(struct ieee80211_hw *hw,
13472 				enum ieee80211_reconfig_type reconfig_type)
13473 {
13474 	struct ath12k_hw *ah = ath12k_hw_to_ah(hw);
13475 	struct ath12k *ar;
13476 	struct ath12k_base *ab;
13477 	struct ath12k_vif *ahvif;
13478 	struct ath12k_link_vif *arvif;
13479 	int recovery_count, i;
13480 
13481 	lockdep_assert_wiphy(hw->wiphy);
13482 
13483 	if (reconfig_type != IEEE80211_RECONFIG_TYPE_RESTART)
13484 		return;
13485 
13486 	guard(mutex)(&ah->hw_mutex);
13487 
13488 	if (ah->state != ATH12K_HW_STATE_RESTARTED)
13489 		return;
13490 
13491 	ah->state = ATH12K_HW_STATE_ON;
13492 	ieee80211_wake_queues(hw);
13493 
13494 	for_each_ar(ah, ar, i) {
13495 		ab = ar->ab;
13496 
13497 		ath12k_warn(ar->ab, "pdev %d successfully recovered\n",
13498 			    ar->pdev->pdev_id);
13499 
13500 		if (ar->ab->hw_params->current_cc_support &&
13501 		    ar->alpha2[0] != 0 && ar->alpha2[1] != 0) {
13502 			struct wmi_set_current_country_arg arg = {};
13503 
13504 			memcpy(&arg.alpha2, ar->alpha2, 2);
13505 			reinit_completion(&ar->regd_update_completed);
13506 			ath12k_wmi_send_set_current_country_cmd(ar, &arg);
13507 		}
13508 
13509 		if (ab->is_reset) {
13510 			recovery_count = atomic_inc_return(&ab->recovery_count);
13511 
13512 			ath12k_dbg(ab, ATH12K_DBG_BOOT, "recovery count %d\n",
13513 				   recovery_count);
13514 
13515 			/* When there are multiple radios in an SOC,
13516 			 * the recovery has to be done for each radio
13517 			 */
13518 			if (recovery_count == ab->num_radios) {
13519 				atomic_dec(&ab->reset_count);
13520 				complete(&ab->reset_complete);
13521 				ab->is_reset = false;
13522 				atomic_set(&ab->fail_cont_count, 0);
13523 				ath12k_dbg(ab, ATH12K_DBG_BOOT, "reset success\n");
13524 			}
13525 		}
13526 
13527 		list_for_each_entry(arvif, &ar->arvifs, list) {
13528 			ahvif = arvif->ahvif;
13529 			ath12k_dbg(ab, ATH12K_DBG_BOOT,
13530 				   "reconfig cipher %d up %d vdev type %d\n",
13531 				   ahvif->dp_vif.key_cipher,
13532 				   arvif->is_up,
13533 				   ahvif->vdev_type);
13534 
13535 			/* After trigger disconnect, then upper layer will
13536 			 * trigger connect again, then the PN number of
13537 			 * upper layer will be reset to keep up with AP
13538 			 * side, hence PN number mismatch will not happen.
13539 			 */
13540 			if (arvif->is_up &&
13541 			    ahvif->vdev_type == WMI_VDEV_TYPE_STA &&
13542 			    ahvif->vdev_subtype == WMI_VDEV_SUBTYPE_NONE) {
13543 				ieee80211_hw_restart_disconnect(ahvif->vif);
13544 
13545 				ath12k_dbg(ab, ATH12K_DBG_BOOT,
13546 					   "restart disconnect\n");
13547 			}
13548 		}
13549 	}
13550 }
13551 EXPORT_SYMBOL(ath12k_mac_op_reconfig_complete);
13552 
13553 static void
13554 ath12k_mac_update_bss_chan_survey(struct ath12k *ar,
13555 				  struct ieee80211_channel *channel)
13556 {
13557 	int ret;
13558 	enum wmi_bss_chan_info_req_type type = WMI_BSS_SURVEY_REQ_TYPE_READ;
13559 
13560 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
13561 
13562 	if (!test_bit(WMI_TLV_SERVICE_BSS_CHANNEL_INFO_64, ar->ab->wmi_ab.svc_map) ||
13563 	    ar->rx_channel != channel)
13564 		return;
13565 
13566 	if (ar->scan.state != ATH12K_SCAN_IDLE) {
13567 		ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
13568 			   "ignoring bss chan info req while scanning..\n");
13569 		return;
13570 	}
13571 
13572 	reinit_completion(&ar->bss_survey_done);
13573 
13574 	ret = ath12k_wmi_pdev_bss_chan_info_request(ar, type);
13575 	if (ret) {
13576 		ath12k_warn(ar->ab, "failed to send pdev bss chan info request\n");
13577 		return;
13578 	}
13579 
13580 	ret = wait_for_completion_timeout(&ar->bss_survey_done, 3 * HZ);
13581 	if (ret == 0)
13582 		ath12k_warn(ar->ab, "bss channel survey timed out\n");
13583 }
13584 
13585 int ath12k_mac_op_get_survey(struct ieee80211_hw *hw, int idx,
13586 			     struct survey_info *survey)
13587 {
13588 	struct ath12k *ar;
13589 	struct ieee80211_supported_band *sband;
13590 	struct survey_info *ar_survey;
13591 
13592 	lockdep_assert_wiphy(hw->wiphy);
13593 
13594 	if (idx >= ATH12K_NUM_CHANS)
13595 		return -ENOENT;
13596 
13597 	sband = hw->wiphy->bands[NL80211_BAND_2GHZ];
13598 	if (sband && idx >= sband->n_channels) {
13599 		idx -= sband->n_channels;
13600 		sband = NULL;
13601 	}
13602 
13603 	if (!sband)
13604 		sband = hw->wiphy->bands[NL80211_BAND_5GHZ];
13605 	if (sband && idx >= sband->n_channels) {
13606 		idx -= sband->n_channels;
13607 		sband = NULL;
13608 	}
13609 
13610 	if (!sband)
13611 		sband = hw->wiphy->bands[NL80211_BAND_6GHZ];
13612 
13613 	if (!sband || idx >= sband->n_channels)
13614 		return -ENOENT;
13615 
13616 	ar = ath12k_mac_get_ar_by_chan(hw, &sband->channels[idx]);
13617 	if (!ar) {
13618 		if (sband->channels[idx].flags & IEEE80211_CHAN_DISABLED) {
13619 			memset(survey, 0, sizeof(*survey));
13620 			return 0;
13621 		}
13622 		return -ENOENT;
13623 	}
13624 
13625 	ar_survey = &ar->survey[idx];
13626 
13627 	ath12k_mac_update_bss_chan_survey(ar, &sband->channels[idx]);
13628 
13629 	spin_lock_bh(&ar->data_lock);
13630 	memcpy(survey, ar_survey, sizeof(*survey));
13631 	spin_unlock_bh(&ar->data_lock);
13632 
13633 	survey->channel = &sband->channels[idx];
13634 
13635 	if (ar->rx_channel == survey->channel)
13636 		survey->filled |= SURVEY_INFO_IN_USE;
13637 
13638 	return 0;
13639 }
13640 EXPORT_SYMBOL(ath12k_mac_op_get_survey);
13641 
13642 static void ath12k_mac_put_chain_rssi(struct station_info *sinfo,
13643 				      struct ath12k_link_sta *arsta)
13644 {
13645 	s8 rssi;
13646 	int i;
13647 
13648 	for (i = 0; i < ARRAY_SIZE(sinfo->chain_signal); i++) {
13649 		sinfo->chains &= ~BIT(i);
13650 		rssi = arsta->chain_signal[i];
13651 
13652 		if (rssi != ATH12K_DEFAULT_NOISE_FLOOR &&
13653 		    rssi != ATH12K_INVALID_RSSI_FULL &&
13654 		    rssi != ATH12K_INVALID_RSSI_EMPTY &&
13655 		    rssi != 0) {
13656 			sinfo->chain_signal[i] = rssi;
13657 			sinfo->chains |= BIT(i);
13658 			sinfo->filled |= BIT_ULL(NL80211_STA_INFO_CHAIN_SIGNAL);
13659 		}
13660 	}
13661 }
13662 
13663 void ath12k_mac_op_sta_statistics(struct ieee80211_hw *hw,
13664 				  struct ieee80211_vif *vif,
13665 				  struct ieee80211_sta *sta,
13666 				  struct station_info *sinfo)
13667 {
13668 	struct ath12k_sta *ahsta = ath12k_sta_to_ahsta(sta);
13669 	struct ath12k_dp_link_peer_rate_info rate_info = {};
13670 	struct ath12k_fw_stats_req_params params = {};
13671 	struct ath12k_dp_link_peer *peer;
13672 	struct ath12k_link_sta *arsta;
13673 	s8 signal, noise_floor;
13674 	struct ath12k_dp *dp;
13675 	struct ath12k *ar;
13676 	bool db2dbm;
13677 
13678 	lockdep_assert_wiphy(hw->wiphy);
13679 
13680 	arsta = &ahsta->deflink;
13681 	ar = ath12k_get_ar_by_vif(hw, vif, arsta->link_id);
13682 	if (!ar)
13683 		return;
13684 
13685 	dp = ath12k_ab_to_dp(ar->ab);
13686 	ath12k_dp_link_peer_get_sta_rate_info_stats(dp, arsta->addr, &rate_info);
13687 
13688 	db2dbm = test_bit(WMI_TLV_SERVICE_HW_DB2DBM_CONVERSION_SUPPORT,
13689 			  ar->ab->wmi_ab.svc_map);
13690 
13691 	sinfo->rx_duration = rate_info.rx_duration;
13692 	sinfo->filled |= BIT_ULL(NL80211_STA_INFO_RX_DURATION);
13693 
13694 	sinfo->tx_duration = rate_info.tx_duration;
13695 	sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_DURATION);
13696 
13697 	if (rate_info.txrate.legacy || rate_info.txrate.nss) {
13698 		if (rate_info.txrate.legacy) {
13699 			sinfo->txrate.legacy = rate_info.txrate.legacy;
13700 		} else {
13701 			sinfo->txrate.mcs = rate_info.txrate.mcs;
13702 			sinfo->txrate.nss = rate_info.txrate.nss;
13703 			sinfo->txrate.bw = rate_info.txrate.bw;
13704 			sinfo->txrate.he_gi = rate_info.txrate.he_gi;
13705 			sinfo->txrate.he_dcm = rate_info.txrate.he_dcm;
13706 			sinfo->txrate.he_ru_alloc = rate_info.txrate.he_ru_alloc;
13707 			sinfo->txrate.eht_gi = rate_info.txrate.eht_gi;
13708 			sinfo->txrate.eht_ru_alloc = rate_info.txrate.eht_ru_alloc;
13709 		}
13710 		sinfo->txrate.flags = rate_info.txrate.flags;
13711 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_BITRATE);
13712 	}
13713 
13714 	/* TODO: Use real NF instead of default one. */
13715 	signal = rate_info.rssi_comb;
13716 
13717 	params.pdev_id = ath12k_mac_get_target_pdev_id(ar);
13718 	params.vdev_id = 0;
13719 	params.stats_id = WMI_REQUEST_VDEV_STAT;
13720 
13721 	if (!signal &&
13722 	    ahsta->ahvif->vdev_type == WMI_VDEV_TYPE_STA &&
13723 	    !(ath12k_mac_get_fw_stats(ar, &params))) {
13724 		signal = arsta->rssi_beacon;
13725 		ath12k_fw_stats_reset(ar);
13726 	}
13727 
13728 	params.stats_id = WMI_REQUEST_RSSI_PER_CHAIN_STAT;
13729 	if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_CHAIN_SIGNAL)) &&
13730 	    ahsta->ahvif->vdev_type == WMI_VDEV_TYPE_STA &&
13731 	    !(ath12k_mac_get_fw_stats(ar, &params))) {
13732 		ath12k_mac_put_chain_rssi(sinfo, arsta);
13733 		ath12k_fw_stats_reset(ar);
13734 	}
13735 
13736 	spin_lock_bh(&ar->data_lock);
13737 	noise_floor = ath12k_pdev_get_noise_floor(ar);
13738 	spin_unlock_bh(&ar->data_lock);
13739 
13740 	if (signal) {
13741 		sinfo->signal = db2dbm ? signal : signal + noise_floor;
13742 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_SIGNAL);
13743 	}
13744 
13745 	sinfo->signal_avg = rate_info.signal_avg;
13746 
13747 	if (!db2dbm)
13748 		sinfo->signal_avg += noise_floor;
13749 
13750 	sinfo->filled |= BIT_ULL(NL80211_STA_INFO_SIGNAL_AVG);
13751 
13752 	spin_lock_bh(&dp->dp_lock);
13753 	peer = ath12k_dp_link_peer_find_by_addr(dp, arsta->addr);
13754 	if (!peer) {
13755 		spin_unlock_bh(&dp->dp_lock);
13756 		return;
13757 	}
13758 
13759 	sinfo->tx_retries = peer->tx_retry_count;
13760 	sinfo->tx_failed = peer->tx_retry_failed;
13761 	sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_RETRIES);
13762 	sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_FAILED);
13763 
13764 	spin_unlock_bh(&dp->dp_lock);
13765 }
13766 EXPORT_SYMBOL(ath12k_mac_op_sta_statistics);
13767 
13768 void ath12k_mac_op_link_sta_statistics(struct ieee80211_hw *hw,
13769 				       struct ieee80211_vif *vif,
13770 				       struct ieee80211_link_sta *link_sta,
13771 				       struct link_station_info *link_sinfo)
13772 {
13773 	struct ath12k_sta *ahsta = ath12k_sta_to_ahsta(link_sta->sta);
13774 	struct ath12k_fw_stats_req_params params = {};
13775 	struct ath12k_dp_link_peer *peer;
13776 	struct ath12k_link_sta *arsta;
13777 	struct ath12k *ar;
13778 	s8 signal;
13779 	bool db2dbm;
13780 
13781 	lockdep_assert_wiphy(hw->wiphy);
13782 
13783 	arsta = wiphy_dereference(hw->wiphy, ahsta->link[link_sta->link_id]);
13784 
13785 	if (!arsta)
13786 		return;
13787 
13788 	ar = ath12k_get_ar_by_vif(hw, vif, arsta->link_id);
13789 	if (!ar)
13790 		return;
13791 
13792 	db2dbm = test_bit(WMI_TLV_SERVICE_HW_DB2DBM_CONVERSION_SUPPORT,
13793 			  ar->ab->wmi_ab.svc_map);
13794 
13795 	spin_lock_bh(&ar->ab->dp->dp_lock);
13796 	peer = ath12k_dp_link_peer_find_by_addr(ar->ab->dp, arsta->addr);
13797 	if (!peer) {
13798 		spin_unlock_bh(&ar->ab->dp->dp_lock);
13799 		return;
13800 	}
13801 
13802 	link_sinfo->rx_duration = peer->rx_duration;
13803 	link_sinfo->filled |= BIT_ULL(NL80211_STA_INFO_RX_DURATION);
13804 
13805 	link_sinfo->tx_duration = peer->tx_duration;
13806 	link_sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_DURATION);
13807 
13808 	if (peer->txrate.legacy || peer->txrate.nss) {
13809 		if (peer->txrate.legacy) {
13810 			link_sinfo->txrate.legacy = peer->txrate.legacy;
13811 		} else {
13812 			link_sinfo->txrate.mcs = peer->txrate.mcs;
13813 			link_sinfo->txrate.nss = peer->txrate.nss;
13814 			link_sinfo->txrate.bw = peer->txrate.bw;
13815 			link_sinfo->txrate.he_gi = peer->txrate.he_gi;
13816 			link_sinfo->txrate.he_dcm = peer->txrate.he_dcm;
13817 			link_sinfo->txrate.he_ru_alloc =
13818 				peer->txrate.he_ru_alloc;
13819 			link_sinfo->txrate.eht_gi = peer->txrate.eht_gi;
13820 			link_sinfo->txrate.eht_ru_alloc =
13821 				peer->txrate.eht_ru_alloc;
13822 		}
13823 		link_sinfo->txrate.flags = peer->txrate.flags;
13824 		link_sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_BITRATE);
13825 	}
13826 
13827 	link_sinfo->signal_avg = ewma_avg_rssi_read(&peer->avg_rssi);
13828 
13829 	if (!db2dbm)
13830 		link_sinfo->signal_avg += ATH12K_DEFAULT_NOISE_FLOOR;
13831 
13832 	link_sinfo->filled |= BIT_ULL(NL80211_STA_INFO_SIGNAL_AVG);
13833 
13834 	link_sinfo->tx_retries = peer->tx_retry_count;
13835 	link_sinfo->tx_failed = peer->tx_retry_failed;
13836 	link_sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_RETRIES);
13837 	link_sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_FAILED);
13838 
13839 	/* TODO: Use real NF instead of default one. */
13840 	signal = peer->rssi_comb;
13841 
13842 	spin_unlock_bh(&ar->ab->dp->dp_lock);
13843 
13844 	if (!signal && ahsta->ahvif->vdev_type == WMI_VDEV_TYPE_STA) {
13845 		params.pdev_id = ath12k_mac_get_target_pdev_id(ar);
13846 		params.vdev_id = 0;
13847 		params.stats_id = WMI_REQUEST_VDEV_STAT;
13848 
13849 		if (!ath12k_mac_get_fw_stats(ar, &params)) {
13850 			signal = arsta->rssi_beacon;
13851 			ath12k_fw_stats_reset(ar);
13852 		}
13853 	}
13854 
13855 	if (signal) {
13856 		link_sinfo->signal =
13857 			db2dbm ? signal : signal + ATH12K_DEFAULT_NOISE_FLOOR;
13858 		link_sinfo->filled |= BIT_ULL(NL80211_STA_INFO_SIGNAL);
13859 	}
13860 }
13861 EXPORT_SYMBOL(ath12k_mac_op_link_sta_statistics);
13862 
13863 int ath12k_mac_op_cancel_remain_on_channel(struct ieee80211_hw *hw,
13864 					   struct ieee80211_vif *vif)
13865 {
13866 	struct ath12k_hw *ah = ath12k_hw_to_ah(hw);
13867 	struct ath12k *ar;
13868 
13869 	ar = ath12k_ah_to_ar(ah, 0);
13870 
13871 	lockdep_assert_wiphy(hw->wiphy);
13872 
13873 	spin_lock_bh(&ar->data_lock);
13874 	ar->scan.roc_notify = false;
13875 	spin_unlock_bh(&ar->data_lock);
13876 
13877 	ath12k_scan_abort(ar);
13878 
13879 	cancel_delayed_work_sync(&ar->scan.timeout);
13880 	wiphy_work_flush(hw->wiphy, &ar->scan.vdev_clean_wk);
13881 
13882 	return 0;
13883 }
13884 EXPORT_SYMBOL(ath12k_mac_op_cancel_remain_on_channel);
13885 
13886 int ath12k_mac_op_remain_on_channel(struct ieee80211_hw *hw,
13887 				    struct ieee80211_vif *vif,
13888 				    struct ieee80211_channel *chan,
13889 				    int duration,
13890 				    enum ieee80211_roc_type type)
13891 {
13892 	struct ath12k_vif *ahvif = ath12k_vif_to_ahvif(vif);
13893 	struct ath12k_hw *ah = ath12k_hw_to_ah(hw);
13894 	struct ath12k_link_vif *arvif;
13895 	struct ath12k *ar;
13896 	u32 scan_time_msec;
13897 	bool create = true;
13898 	u8 link_id;
13899 	int ret;
13900 
13901 	lockdep_assert_wiphy(hw->wiphy);
13902 
13903 	ar = ath12k_mac_select_scan_device(hw, vif, chan->center_freq);
13904 	if (!ar)
13905 		return -EINVAL;
13906 
13907 	/* check if any of the links of ML VIF is already started on
13908 	 * radio(ar) corresponding to given scan frequency and use it,
13909 	 * if not use deflink(link 0) for scan purpose.
13910 	 */
13911 
13912 	link_id = ath12k_mac_find_link_id_by_ar(ahvif, ar);
13913 	arvif = ath12k_mac_assign_link_vif(ah, vif, link_id);
13914 	/* If the vif is already assigned to a specific vdev of an ar,
13915 	 * check whether its already started, vdev which is started
13916 	 * are not allowed to switch to a new radio.
13917 	 * If the vdev is not started, but was earlier created on a
13918 	 * different ar, delete that vdev and create a new one. We don't
13919 	 * delete at the scan stop as an optimization to avoid redundant
13920 	 * delete-create vdev's for the same ar, in case the request is
13921 	 * always on the same band for the vif
13922 	 */
13923 	if (arvif->is_created) {
13924 		if (WARN_ON(!arvif->ar))
13925 			return -EINVAL;
13926 
13927 		if (ar != arvif->ar && arvif->is_started)
13928 			return -EBUSY;
13929 
13930 		if (ar != arvif->ar) {
13931 			ath12k_mac_remove_link_interface(hw, arvif);
13932 			ath12k_mac_unassign_link_vif(arvif);
13933 		} else {
13934 			create = false;
13935 		}
13936 	}
13937 
13938 	if (create) {
13939 		arvif = ath12k_mac_assign_link_vif(ah, vif, link_id);
13940 
13941 		ret = ath12k_mac_vdev_create(ar, arvif);
13942 		if (ret) {
13943 			ath12k_warn(ar->ab, "unable to create scan vdev for roc: %d\n",
13944 				    ret);
13945 			ath12k_mac_unassign_link_vif(arvif);
13946 			return ret;
13947 		}
13948 	}
13949 
13950 	spin_lock_bh(&ar->data_lock);
13951 
13952 	switch (ar->scan.state) {
13953 	case ATH12K_SCAN_IDLE:
13954 		reinit_completion(&ar->scan.started);
13955 		reinit_completion(&ar->scan.completed);
13956 		reinit_completion(&ar->scan.on_channel);
13957 		ar->scan.state = ATH12K_SCAN_STARTING;
13958 		ar->scan.is_roc = true;
13959 		ar->scan.arvif = arvif;
13960 		ar->scan.roc_freq = chan->center_freq;
13961 		ar->scan.roc_notify = true;
13962 		ret = 0;
13963 		break;
13964 	case ATH12K_SCAN_STARTING:
13965 	case ATH12K_SCAN_RUNNING:
13966 	case ATH12K_SCAN_ABORTING:
13967 		ret = -EBUSY;
13968 		break;
13969 	}
13970 
13971 	spin_unlock_bh(&ar->data_lock);
13972 
13973 	if (ret)
13974 		return ret;
13975 
13976 	scan_time_msec = hw->wiphy->max_remain_on_channel_duration * 2;
13977 
13978 	struct ath12k_wmi_scan_req_arg *arg __free(kfree) =
13979 					kzalloc_flex(*arg, chan_list, 1);
13980 	if (!arg)
13981 		return -ENOMEM;
13982 
13983 	arg->num_chan = 1;
13984 	ath12k_wmi_start_scan_init(ar, arg);
13985 
13986 	arg->vdev_id = arvif->vdev_id;
13987 	arg->scan_id = ATH12K_SCAN_ID;
13988 	arg->chan_list[0] = chan->center_freq;
13989 	arg->dwell_time_active = scan_time_msec;
13990 	arg->dwell_time_passive = scan_time_msec;
13991 	arg->max_scan_time = scan_time_msec;
13992 	arg->scan_f_passive = 1;
13993 	arg->burst_duration = duration;
13994 
13995 	ret = ath12k_start_scan(ar, arg);
13996 	if (ret) {
13997 		ath12k_warn(ar->ab, "failed to start roc scan: %d\n", ret);
13998 
13999 		spin_lock_bh(&ar->data_lock);
14000 		ar->scan.state = ATH12K_SCAN_IDLE;
14001 		spin_unlock_bh(&ar->data_lock);
14002 		return ret;
14003 	}
14004 
14005 	ret = wait_for_completion_timeout(&ar->scan.on_channel, 3 * HZ);
14006 	if (ret == 0) {
14007 		ath12k_warn(ar->ab, "failed to switch to channel for roc scan\n");
14008 		ret = ath12k_scan_stop(ar);
14009 		if (ret)
14010 			ath12k_warn(ar->ab, "failed to stop scan: %d\n", ret);
14011 		return -ETIMEDOUT;
14012 	}
14013 
14014 	ieee80211_queue_delayed_work(hw, &ar->scan.timeout,
14015 				     msecs_to_jiffies(duration));
14016 
14017 	return 0;
14018 }
14019 EXPORT_SYMBOL(ath12k_mac_op_remain_on_channel);
14020 
14021 void ath12k_mac_op_set_rekey_data(struct ieee80211_hw *hw,
14022 				  struct ieee80211_vif *vif,
14023 				  struct cfg80211_gtk_rekey_data *data)
14024 {
14025 	struct ath12k_vif *ahvif = ath12k_vif_to_ahvif(vif);
14026 	struct ath12k_rekey_data *rekey_data;
14027 	struct ath12k_hw *ah = ath12k_hw_to_ah(hw);
14028 	struct ath12k *ar = ath12k_ah_to_ar(ah, 0);
14029 	struct ath12k_link_vif *arvif;
14030 
14031 	lockdep_assert_wiphy(hw->wiphy);
14032 
14033 	arvif = &ahvif->deflink;
14034 	rekey_data = &arvif->rekey_data;
14035 
14036 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac set rekey data vdev %d\n",
14037 		   arvif->vdev_id);
14038 
14039 	memcpy(rekey_data->kck, data->kck, NL80211_KCK_LEN);
14040 	memcpy(rekey_data->kek, data->kek, NL80211_KEK_LEN);
14041 
14042 	/* The supplicant works on big-endian, the firmware expects it on
14043 	 * little endian.
14044 	 */
14045 	rekey_data->replay_ctr = get_unaligned_be64(data->replay_ctr);
14046 
14047 	arvif->rekey_data.enable_offload = true;
14048 
14049 	ath12k_dbg_dump(ar->ab, ATH12K_DBG_MAC, "kck", NULL,
14050 			rekey_data->kck, NL80211_KCK_LEN);
14051 	ath12k_dbg_dump(ar->ab, ATH12K_DBG_MAC, "kek", NULL,
14052 			rekey_data->kck, NL80211_KEK_LEN);
14053 	ath12k_dbg_dump(ar->ab, ATH12K_DBG_MAC, "replay ctr", NULL,
14054 			&rekey_data->replay_ctr, sizeof(rekey_data->replay_ctr));
14055 }
14056 EXPORT_SYMBOL(ath12k_mac_op_set_rekey_data);
14057 
14058 void ath12k_mac_update_freq_range(struct ath12k *ar,
14059 				  u32 freq_low, u32 freq_high)
14060 {
14061 	if (!(freq_low && freq_high))
14062 		return;
14063 
14064 	if (ar->freq_range.start_freq || ar->freq_range.end_freq) {
14065 		ar->freq_range.start_freq = min(ar->freq_range.start_freq,
14066 						MHZ_TO_KHZ(freq_low));
14067 		ar->freq_range.end_freq = max(ar->freq_range.end_freq,
14068 					      MHZ_TO_KHZ(freq_high));
14069 	} else {
14070 		ar->freq_range.start_freq = MHZ_TO_KHZ(freq_low);
14071 		ar->freq_range.end_freq = MHZ_TO_KHZ(freq_high);
14072 	}
14073 
14074 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
14075 		   "mac pdev %u freq limit updated. New range %u->%u MHz\n",
14076 		   ar->pdev->pdev_id, KHZ_TO_MHZ(ar->freq_range.start_freq),
14077 		   KHZ_TO_MHZ(ar->freq_range.end_freq));
14078 }
14079 
14080 static void ath12k_mac_update_ch_list(struct ath12k *ar,
14081 				      struct ieee80211_supported_band *band,
14082 				      u32 freq_low, u32 freq_high)
14083 {
14084 	int i;
14085 
14086 	if (!(freq_low && freq_high))
14087 		return;
14088 
14089 	for (i = 0; i < band->n_channels; i++) {
14090 		if (band->channels[i].center_freq < freq_low ||
14091 		    band->channels[i].center_freq > freq_high)
14092 			band->channels[i].flags |= IEEE80211_CHAN_DISABLED;
14093 	}
14094 }
14095 
14096 static u32 ath12k_get_phy_id(struct ath12k *ar, u32 band)
14097 {
14098 	struct ath12k_pdev *pdev = ar->pdev;
14099 	struct ath12k_pdev_cap *pdev_cap = &pdev->cap;
14100 
14101 	if (band == WMI_HOST_WLAN_2GHZ_CAP)
14102 		return pdev_cap->band[NL80211_BAND_2GHZ].phy_id;
14103 
14104 	if (band == WMI_HOST_WLAN_5GHZ_CAP)
14105 		return pdev_cap->band[NL80211_BAND_5GHZ].phy_id;
14106 
14107 	ath12k_warn(ar->ab, "unsupported phy cap:%d\n", band);
14108 
14109 	return 0;
14110 }
14111 
14112 static int ath12k_mac_update_band(struct ath12k *ar,
14113 				  struct ieee80211_supported_band *orig_band,
14114 				  struct ieee80211_supported_band *new_band)
14115 {
14116 	int i;
14117 
14118 	if (!orig_band || !new_band)
14119 		return -EINVAL;
14120 
14121 	if (orig_band->band != new_band->band)
14122 		return -EINVAL;
14123 
14124 	for (i = 0; i < new_band->n_channels; i++) {
14125 		if (new_band->channels[i].flags & IEEE80211_CHAN_DISABLED)
14126 			continue;
14127 		/* An enabled channel in new_band should not be already enabled
14128 		 * in the orig_band
14129 		 */
14130 		if (WARN_ON(!(orig_band->channels[i].flags &
14131 			      IEEE80211_CHAN_DISABLED)))
14132 			return -EINVAL;
14133 		orig_band->channels[i].flags &= ~IEEE80211_CHAN_DISABLED;
14134 	}
14135 	return 0;
14136 }
14137 
14138 static void ath12k_mac_update_5_9_ghz_ch_list(struct ath12k *ar,
14139 					      struct ieee80211_supported_band *band)
14140 {
14141 	int i;
14142 
14143 	if (test_bit(WMI_TLV_SERVICE_5_9GHZ_SUPPORT,
14144 		     ar->ab->wmi_ab.svc_map))
14145 		return;
14146 
14147 	guard(spinlock_bh)(&ar->ab->base_lock);
14148 	if (ar->ab->dfs_region != ATH12K_DFS_REG_FCC)
14149 		return;
14150 
14151 	for (i = 0; i < band->n_channels; i++) {
14152 		if (band->channels[i].center_freq >= ATH12K_5_9_GHZ_MIN_FREQ &&
14153 		    band->channels[i].center_freq <= ATH12K_5_9_GHZ_MAX_FREQ)
14154 			band->channels[i].flags |= IEEE80211_CHAN_DISABLED;
14155 	}
14156 }
14157 
14158 static int ath12k_mac_setup_channels_rates(struct ath12k *ar,
14159 					   u32 supported_bands,
14160 					   struct ieee80211_supported_band *bands[])
14161 {
14162 	struct ieee80211_supported_band *band;
14163 	struct ath12k_wmi_hal_reg_capabilities_ext_arg *reg_cap;
14164 	struct ath12k_base *ab = ar->ab;
14165 	u32 phy_id, freq_low, freq_high;
14166 	struct ath12k_hw *ah = ar->ah;
14167 	void *channels;
14168 	int ret;
14169 
14170 	BUILD_BUG_ON((ARRAY_SIZE(ath12k_2ghz_channels) +
14171 		      ARRAY_SIZE(ath12k_5ghz_channels) +
14172 		      ARRAY_SIZE(ath12k_6ghz_channels)) !=
14173 		     ATH12K_NUM_CHANS);
14174 
14175 	reg_cap = &ab->hal_reg_cap[ar->pdev_idx];
14176 
14177 	if (supported_bands & WMI_HOST_WLAN_2GHZ_CAP) {
14178 		channels = kmemdup(ath12k_2ghz_channels,
14179 				   sizeof(ath12k_2ghz_channels),
14180 				   GFP_KERNEL);
14181 		if (!channels)
14182 			return -ENOMEM;
14183 
14184 		band = &ar->mac.sbands[NL80211_BAND_2GHZ];
14185 		band->band = NL80211_BAND_2GHZ;
14186 		band->n_channels = ARRAY_SIZE(ath12k_2ghz_channels);
14187 		band->channels = channels;
14188 		band->n_bitrates = ath12k_g_rates_size;
14189 		band->bitrates = ath12k_g_rates;
14190 
14191 		if (ab->hw_params->single_pdev_only) {
14192 			phy_id = ath12k_get_phy_id(ar, WMI_HOST_WLAN_2GHZ_CAP);
14193 			reg_cap = &ab->hal_reg_cap[phy_id];
14194 		}
14195 
14196 		freq_low = max(reg_cap->low_2ghz_chan,
14197 			       ab->reg_freq_2ghz.start_freq);
14198 		freq_high = min(reg_cap->high_2ghz_chan,
14199 				ab->reg_freq_2ghz.end_freq);
14200 
14201 		ath12k_mac_update_ch_list(ar, band,
14202 					  reg_cap->low_2ghz_chan,
14203 					  reg_cap->high_2ghz_chan);
14204 
14205 		ath12k_mac_update_freq_range(ar, freq_low, freq_high);
14206 
14207 		if (!bands[NL80211_BAND_2GHZ]) {
14208 			bands[NL80211_BAND_2GHZ] = band;
14209 		} else {
14210 			/* Split mac in same band under same wiphy */
14211 			ret = ath12k_mac_update_band(ar, bands[NL80211_BAND_2GHZ], band);
14212 			if (ret) {
14213 				kfree(channels);
14214 				band->channels = NULL;
14215 				return ret;
14216 			}
14217 			ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac pdev %u identified as 2 GHz split mac with start freq %d end freq %d",
14218 				   ar->pdev->pdev_id,
14219 				   KHZ_TO_MHZ(ar->freq_range.start_freq),
14220 				   KHZ_TO_MHZ(ar->freq_range.end_freq));
14221 		}
14222 	}
14223 
14224 	if (supported_bands & WMI_HOST_WLAN_5GHZ_CAP) {
14225 		if (reg_cap->high_5ghz_chan >= ATH12K_MIN_6GHZ_FREQ) {
14226 			channels = kmemdup(ath12k_6ghz_channels,
14227 					   sizeof(ath12k_6ghz_channels), GFP_KERNEL);
14228 			if (!channels) {
14229 				kfree(ar->mac.sbands[NL80211_BAND_2GHZ].channels);
14230 				return -ENOMEM;
14231 			}
14232 
14233 			ar->supports_6ghz = true;
14234 			band = &ar->mac.sbands[NL80211_BAND_6GHZ];
14235 			band->band = NL80211_BAND_6GHZ;
14236 			band->n_channels = ARRAY_SIZE(ath12k_6ghz_channels);
14237 			band->channels = channels;
14238 			band->n_bitrates = ath12k_a_rates_size;
14239 			band->bitrates = ath12k_a_rates;
14240 
14241 			freq_low = max(reg_cap->low_5ghz_chan,
14242 				       ab->reg_freq_6ghz.start_freq);
14243 			freq_high = min(reg_cap->high_5ghz_chan,
14244 					ab->reg_freq_6ghz.end_freq);
14245 
14246 			ath12k_mac_update_ch_list(ar, band,
14247 						  reg_cap->low_5ghz_chan,
14248 						  reg_cap->high_5ghz_chan);
14249 
14250 			ath12k_mac_update_freq_range(ar, freq_low, freq_high);
14251 			ah->use_6ghz_regd = true;
14252 
14253 			if (!bands[NL80211_BAND_6GHZ]) {
14254 				bands[NL80211_BAND_6GHZ] = band;
14255 			} else {
14256 				/* Split mac in same band under same wiphy */
14257 				ret = ath12k_mac_update_band(ar,
14258 							     bands[NL80211_BAND_6GHZ],
14259 							     band);
14260 				if (ret) {
14261 					kfree(ar->mac.sbands[NL80211_BAND_2GHZ].channels);
14262 					ar->mac.sbands[NL80211_BAND_2GHZ].channels = NULL;
14263 					kfree(channels);
14264 					band->channels = NULL;
14265 					return ret;
14266 				}
14267 				ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac pdev %u identified as 6 GHz split mac with start freq %d end freq %d",
14268 					   ar->pdev->pdev_id,
14269 					   KHZ_TO_MHZ(ar->freq_range.start_freq),
14270 					   KHZ_TO_MHZ(ar->freq_range.end_freq));
14271 			}
14272 		}
14273 
14274 		if (reg_cap->low_5ghz_chan < ATH12K_MIN_6GHZ_FREQ) {
14275 			channels = kmemdup(ath12k_5ghz_channels,
14276 					   sizeof(ath12k_5ghz_channels),
14277 					   GFP_KERNEL);
14278 			if (!channels) {
14279 				kfree(ar->mac.sbands[NL80211_BAND_2GHZ].channels);
14280 				kfree(ar->mac.sbands[NL80211_BAND_6GHZ].channels);
14281 				return -ENOMEM;
14282 			}
14283 
14284 			band = &ar->mac.sbands[NL80211_BAND_5GHZ];
14285 			band->band = NL80211_BAND_5GHZ;
14286 			band->n_channels = ARRAY_SIZE(ath12k_5ghz_channels);
14287 			band->channels = channels;
14288 			band->n_bitrates = ath12k_a_rates_size;
14289 			band->bitrates = ath12k_a_rates;
14290 
14291 			ath12k_mac_update_5_9_ghz_ch_list(ar, band);
14292 
14293 			if (ab->hw_params->single_pdev_only) {
14294 				phy_id = ath12k_get_phy_id(ar, WMI_HOST_WLAN_5GHZ_CAP);
14295 				reg_cap = &ab->hal_reg_cap[phy_id];
14296 			}
14297 
14298 			freq_low = max(reg_cap->low_5ghz_chan,
14299 				       ab->reg_freq_5ghz.start_freq);
14300 			freq_high = min(reg_cap->high_5ghz_chan,
14301 					ab->reg_freq_5ghz.end_freq);
14302 
14303 			ath12k_mac_update_ch_list(ar, band,
14304 						  reg_cap->low_5ghz_chan,
14305 						  reg_cap->high_5ghz_chan);
14306 
14307 			ath12k_mac_update_freq_range(ar, freq_low, freq_high);
14308 
14309 			if (!bands[NL80211_BAND_5GHZ]) {
14310 				bands[NL80211_BAND_5GHZ] = band;
14311 			} else {
14312 				/* Split mac in same band under same wiphy */
14313 				ret = ath12k_mac_update_band(ar,
14314 							     bands[NL80211_BAND_5GHZ],
14315 							     band);
14316 				if (ret) {
14317 					kfree(ar->mac.sbands[NL80211_BAND_2GHZ].channels);
14318 					ar->mac.sbands[NL80211_BAND_2GHZ].channels = NULL;
14319 					kfree(ar->mac.sbands[NL80211_BAND_6GHZ].channels);
14320 					ar->mac.sbands[NL80211_BAND_2GHZ].channels = NULL;
14321 					kfree(channels);
14322 					band->channels = NULL;
14323 					return ret;
14324 				}
14325 				ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac pdev %u identified as 5 GHz split mac with start freq %d end freq %d",
14326 					   ar->pdev->pdev_id,
14327 					   KHZ_TO_MHZ(ar->freq_range.start_freq),
14328 					   KHZ_TO_MHZ(ar->freq_range.end_freq));
14329 			}
14330 		}
14331 	}
14332 
14333 	return 0;
14334 }
14335 
14336 static u16 ath12k_mac_get_ifmodes(struct ath12k_hw *ah)
14337 {
14338 	struct ath12k *ar;
14339 	int i;
14340 	u16 interface_modes = U16_MAX;
14341 
14342 	for_each_ar(ah, ar, i)
14343 		interface_modes &= ar->ab->hw_params->interface_modes;
14344 
14345 	return interface_modes == U16_MAX ? 0 : interface_modes;
14346 }
14347 
14348 static bool ath12k_mac_is_iface_mode_enable(struct ath12k_hw *ah,
14349 					    enum nl80211_iftype type)
14350 {
14351 	struct ath12k *ar;
14352 	int i;
14353 	u16 interface_modes, mode = 0;
14354 	bool is_enable = false;
14355 
14356 	if (type == NL80211_IFTYPE_MESH_POINT) {
14357 		if (IS_ENABLED(CONFIG_MAC80211_MESH))
14358 			mode = BIT(type);
14359 	} else {
14360 		mode = BIT(type);
14361 	}
14362 
14363 	for_each_ar(ah, ar, i) {
14364 		interface_modes = ar->ab->hw_params->interface_modes;
14365 		if (interface_modes & mode) {
14366 			is_enable = true;
14367 			break;
14368 		}
14369 	}
14370 
14371 	return is_enable;
14372 }
14373 
14374 static int
14375 ath12k_mac_setup_radio_iface_comb(struct ath12k *ar,
14376 				  struct ieee80211_iface_combination *comb)
14377 {
14378 	u16 interface_modes = ar->ab->hw_params->interface_modes;
14379 	struct ieee80211_iface_limit *limits;
14380 	int n_limits, max_interfaces;
14381 	bool ap, mesh, p2p;
14382 
14383 	ap = interface_modes & BIT(NL80211_IFTYPE_AP);
14384 	p2p = interface_modes & BIT(NL80211_IFTYPE_P2P_DEVICE);
14385 
14386 	mesh = IS_ENABLED(CONFIG_MAC80211_MESH) &&
14387 	       (interface_modes & BIT(NL80211_IFTYPE_MESH_POINT));
14388 
14389 	if ((ap || mesh) && !p2p) {
14390 		n_limits = 2;
14391 		max_interfaces = 16;
14392 	} else if (p2p) {
14393 		n_limits = 3;
14394 		if (ap || mesh)
14395 			max_interfaces = 16;
14396 		else
14397 			max_interfaces = 3;
14398 	} else {
14399 		n_limits = 1;
14400 		max_interfaces = 1;
14401 	}
14402 
14403 	limits = kzalloc_objs(*limits, n_limits);
14404 	if (!limits)
14405 		return -ENOMEM;
14406 
14407 	limits[0].max = 1;
14408 	limits[0].types |= BIT(NL80211_IFTYPE_STATION);
14409 
14410 	if (ap || mesh || p2p)
14411 		limits[1].max = max_interfaces;
14412 
14413 	if (ap)
14414 		limits[1].types |= BIT(NL80211_IFTYPE_AP);
14415 
14416 	if (mesh)
14417 		limits[1].types |= BIT(NL80211_IFTYPE_MESH_POINT);
14418 
14419 	if (p2p) {
14420 		limits[1].types |= BIT(NL80211_IFTYPE_P2P_CLIENT) |
14421 					BIT(NL80211_IFTYPE_P2P_GO);
14422 		limits[2].max = 1;
14423 		limits[2].types |= BIT(NL80211_IFTYPE_P2P_DEVICE);
14424 	}
14425 
14426 	comb[0].limits = limits;
14427 	comb[0].n_limits = n_limits;
14428 	comb[0].max_interfaces = max_interfaces;
14429 	comb[0].beacon_int_infra_match = true;
14430 	comb[0].beacon_int_min_gcd = 100;
14431 
14432 	comb[0].num_different_channels = 1;
14433 	comb[0].radar_detect_widths = BIT(NL80211_CHAN_WIDTH_20_NOHT) |
14434 				      BIT(NL80211_CHAN_WIDTH_20) |
14435 				      BIT(NL80211_CHAN_WIDTH_40) |
14436 				      BIT(NL80211_CHAN_WIDTH_80) |
14437 				      BIT(NL80211_CHAN_WIDTH_160);
14438 
14439 	return 0;
14440 }
14441 
14442 static int
14443 ath12k_mac_setup_global_iface_comb(struct ath12k_hw *ah,
14444 				   struct wiphy_radio *radio,
14445 				   u8 n_radio,
14446 				   struct ieee80211_iface_combination *comb)
14447 {
14448 	const struct ieee80211_iface_combination *iter_comb;
14449 	struct ieee80211_iface_limit *limits;
14450 	int i, j, n_limits;
14451 	bool ap, mesh, p2p;
14452 
14453 	if (!n_radio)
14454 		return 0;
14455 
14456 	ap = ath12k_mac_is_iface_mode_enable(ah, NL80211_IFTYPE_AP);
14457 	p2p = ath12k_mac_is_iface_mode_enable(ah, NL80211_IFTYPE_P2P_DEVICE);
14458 	mesh = ath12k_mac_is_iface_mode_enable(ah, NL80211_IFTYPE_MESH_POINT);
14459 
14460 	if ((ap || mesh) && !p2p)
14461 		n_limits = 2;
14462 	else if (p2p)
14463 		n_limits = 3;
14464 	else
14465 		n_limits = 1;
14466 
14467 	limits = kzalloc_objs(*limits, n_limits);
14468 	if (!limits)
14469 		return -ENOMEM;
14470 
14471 	for (i = 0; i < n_radio; i++) {
14472 		iter_comb = radio[i].iface_combinations;
14473 		for (j = 0; j < iter_comb->n_limits && j < n_limits; j++) {
14474 			limits[j].types |= iter_comb->limits[j].types;
14475 			limits[j].max += iter_comb->limits[j].max;
14476 		}
14477 
14478 		comb->max_interfaces += iter_comb->max_interfaces;
14479 		comb->num_different_channels += iter_comb->num_different_channels;
14480 		comb->radar_detect_widths |= iter_comb->radar_detect_widths;
14481 	}
14482 
14483 	comb->limits = limits;
14484 	comb->n_limits = n_limits;
14485 	comb->beacon_int_infra_match = true;
14486 	comb->beacon_int_min_gcd = 100;
14487 
14488 	return 0;
14489 }
14490 
14491 static
14492 void ath12k_mac_cleanup_iface_comb(const struct ieee80211_iface_combination *iface_comb)
14493 {
14494 	kfree(iface_comb[0].limits);
14495 	kfree(iface_comb);
14496 }
14497 
14498 static void ath12k_mac_cleanup_iface_combinations(struct ath12k_hw *ah)
14499 {
14500 	struct wiphy *wiphy = ah->hw->wiphy;
14501 	const struct wiphy_radio *radio;
14502 	int i;
14503 
14504 	if (wiphy->n_radio > 0) {
14505 		radio = wiphy->radio;
14506 		for (i = 0; i < wiphy->n_radio; i++)
14507 			ath12k_mac_cleanup_iface_comb(radio[i].iface_combinations);
14508 
14509 		kfree(wiphy->radio);
14510 	}
14511 
14512 	ath12k_mac_cleanup_iface_comb(wiphy->iface_combinations);
14513 }
14514 
14515 static int ath12k_mac_setup_iface_combinations(struct ath12k_hw *ah)
14516 {
14517 	struct ieee80211_iface_combination *combinations, *comb;
14518 	struct wiphy *wiphy = ah->hw->wiphy;
14519 	struct wiphy_radio *radio;
14520 	int n_combinations = 1;
14521 	struct ath12k *ar;
14522 	int i, ret;
14523 
14524 	if (ah->num_radio == 1) {
14525 		ar = &ah->radio[0];
14526 
14527 		if (ar->ab->hw_params->single_pdev_only)
14528 			n_combinations = 2;
14529 
14530 		combinations = kzalloc_objs(*combinations, n_combinations);
14531 		if (!combinations)
14532 			return -ENOMEM;
14533 
14534 		ret = ath12k_mac_setup_radio_iface_comb(ar, combinations);
14535 		if (ret) {
14536 			ath12k_hw_warn(ah, "failed to setup radio interface combinations for one radio: %d",
14537 				       ret);
14538 			goto err_free_combinations;
14539 		}
14540 
14541 		if (ar->ab->hw_params->single_pdev_only) {
14542 			comb = combinations + 1;
14543 			memcpy(comb, combinations, sizeof(*comb));
14544 			comb->num_different_channels = 2;
14545 			comb->radar_detect_widths = 0;
14546 		}
14547 
14548 		goto out;
14549 	}
14550 
14551 	combinations = kzalloc_objs(*combinations, n_combinations);
14552 	if (!combinations)
14553 		return -ENOMEM;
14554 
14555 	/* there are multiple radios */
14556 
14557 	radio = kzalloc_objs(*radio, ah->num_radio);
14558 	if (!radio) {
14559 		ret = -ENOMEM;
14560 		goto err_free_combinations;
14561 	}
14562 
14563 	for_each_ar(ah, ar, i) {
14564 		comb = kzalloc_obj(*comb);
14565 		if (!comb) {
14566 			ret = -ENOMEM;
14567 			goto err_free_radios;
14568 		}
14569 
14570 		ret = ath12k_mac_setup_radio_iface_comb(ar, comb);
14571 		if (ret) {
14572 			ath12k_hw_warn(ah, "failed to setup radio interface combinations for radio %d: %d",
14573 				       i, ret);
14574 			kfree(comb);
14575 			goto err_free_radios;
14576 		}
14577 
14578 		radio[i].freq_range = &ar->freq_range;
14579 		radio[i].n_freq_range = 1;
14580 
14581 		radio[i].iface_combinations = comb;
14582 		radio[i].n_iface_combinations = 1;
14583 	}
14584 
14585 	ret = ath12k_mac_setup_global_iface_comb(ah, radio, ah->num_radio, combinations);
14586 	if (ret) {
14587 		ath12k_hw_warn(ah, "failed to setup global interface combinations: %d",
14588 			       ret);
14589 		goto err_free_all_radios;
14590 	}
14591 
14592 	wiphy->radio = radio;
14593 	wiphy->n_radio = ah->num_radio;
14594 
14595 out:
14596 	wiphy->iface_combinations = combinations;
14597 	wiphy->n_iface_combinations = n_combinations;
14598 
14599 	return 0;
14600 
14601 err_free_all_radios:
14602 	i = ah->num_radio;
14603 
14604 err_free_radios:
14605 	while (i--)
14606 		ath12k_mac_cleanup_iface_comb(radio[i].iface_combinations);
14607 
14608 	kfree(radio);
14609 
14610 err_free_combinations:
14611 	kfree(combinations);
14612 
14613 	return ret;
14614 }
14615 
14616 static const u8 ath12k_if_types_ext_capa[] = {
14617 	[0] = WLAN_EXT_CAPA1_EXT_CHANNEL_SWITCHING,
14618 	[2] = WLAN_EXT_CAPA3_MULTI_BSSID_SUPPORT,
14619 	[7] = WLAN_EXT_CAPA8_OPMODE_NOTIF,
14620 };
14621 
14622 static const u8 ath12k_if_types_ext_capa_sta[] = {
14623 	[0] = WLAN_EXT_CAPA1_EXT_CHANNEL_SWITCHING,
14624 	[2] = WLAN_EXT_CAPA3_MULTI_BSSID_SUPPORT,
14625 	[7] = WLAN_EXT_CAPA8_OPMODE_NOTIF,
14626 	[9] = WLAN_EXT_CAPA10_TWT_REQUESTER_SUPPORT,
14627 };
14628 
14629 static const u8 ath12k_if_types_ext_capa_ap[] = {
14630 	[0] = WLAN_EXT_CAPA1_EXT_CHANNEL_SWITCHING,
14631 	[2] = WLAN_EXT_CAPA3_MULTI_BSSID_SUPPORT,
14632 	[7] = WLAN_EXT_CAPA8_OPMODE_NOTIF,
14633 	[9] = WLAN_EXT_CAPA10_TWT_RESPONDER_SUPPORT,
14634 	[10] = WLAN_EXT_CAPA11_EMA_SUPPORT,
14635 };
14636 
14637 static struct wiphy_iftype_ext_capab ath12k_iftypes_ext_capa[] = {
14638 	{
14639 		.extended_capabilities = ath12k_if_types_ext_capa,
14640 		.extended_capabilities_mask = ath12k_if_types_ext_capa,
14641 		.extended_capabilities_len = sizeof(ath12k_if_types_ext_capa),
14642 	}, {
14643 		.iftype = NL80211_IFTYPE_STATION,
14644 		.extended_capabilities = ath12k_if_types_ext_capa_sta,
14645 		.extended_capabilities_mask = ath12k_if_types_ext_capa_sta,
14646 		.extended_capabilities_len =
14647 				sizeof(ath12k_if_types_ext_capa_sta),
14648 	}, {
14649 		.iftype = NL80211_IFTYPE_AP,
14650 		.extended_capabilities = ath12k_if_types_ext_capa_ap,
14651 		.extended_capabilities_mask = ath12k_if_types_ext_capa_ap,
14652 		.extended_capabilities_len =
14653 				sizeof(ath12k_if_types_ext_capa_ap),
14654 		.eml_capabilities = 0,
14655 		.mld_capa_and_ops = 0,
14656 	},
14657 };
14658 
14659 static void ath12k_mac_cleanup_unregister(struct ath12k *ar)
14660 {
14661 	idr_for_each(&ar->txmgmt_idr, ath12k_mac_tx_mgmt_pending_free, ar);
14662 	idr_destroy(&ar->txmgmt_idr);
14663 
14664 	kfree(ar->mac.sbands[NL80211_BAND_2GHZ].channels);
14665 	kfree(ar->mac.sbands[NL80211_BAND_5GHZ].channels);
14666 	kfree(ar->mac.sbands[NL80211_BAND_6GHZ].channels);
14667 }
14668 
14669 static void ath12k_mac_hw_unregister(struct ath12k_hw *ah)
14670 {
14671 	struct ieee80211_hw *hw = ah->hw;
14672 	struct ath12k *ar;
14673 	int i;
14674 
14675 	for_each_ar(ah, ar, i) {
14676 		cancel_work_sync(&ar->regd_channel_update_work);
14677 		cancel_work_sync(&ar->regd_update_work);
14678 		ath12k_debugfs_unregister(ar);
14679 		ath12k_fw_stats_reset(ar);
14680 	}
14681 
14682 	ieee80211_unregister_hw(hw);
14683 
14684 	for_each_ar(ah, ar, i)
14685 		ath12k_mac_cleanup_unregister(ar);
14686 
14687 	ath12k_mac_cleanup_iface_combinations(ah);
14688 
14689 	SET_IEEE80211_DEV(hw, NULL);
14690 }
14691 
14692 static int ath12k_mac_setup_register(struct ath12k *ar,
14693 				     u32 *ht_cap,
14694 				     struct ieee80211_supported_band *bands[])
14695 {
14696 	struct ath12k_pdev_cap *cap = &ar->pdev->cap;
14697 	int ret;
14698 
14699 	init_waitqueue_head(&ar->txmgmt_empty_waitq);
14700 	idr_init(&ar->txmgmt_idr);
14701 	spin_lock_init(&ar->txmgmt_idr_lock);
14702 
14703 	ath12k_pdev_caps_update(ar);
14704 
14705 	ret = ath12k_mac_setup_channels_rates(ar,
14706 					      cap->supported_bands,
14707 					      bands);
14708 	if (ret)
14709 		return ret;
14710 
14711 	ath12k_mac_setup_ht_vht_cap(ar, cap, ht_cap);
14712 	ath12k_mac_setup_sband_iftype_data(ar, cap);
14713 
14714 	ar->max_num_stations = ath12k_core_get_max_station_per_radio(ar->ab);
14715 	ar->max_num_peers = ath12k_core_get_max_peers_per_radio(ar->ab);
14716 
14717 	ar->rssi_info.min_nf_dbm = ATH12K_DEFAULT_NOISE_FLOOR;
14718 	ar->rssi_info.temp_offset = 0;
14719 	ar->rssi_info.noise_floor = ar->rssi_info.min_nf_dbm + ar->rssi_info.temp_offset;
14720 
14721 	ath12k_thermal_init_configs(ar);
14722 
14723 	return 0;
14724 }
14725 
14726 static int ath12k_mac_hw_register(struct ath12k_hw *ah)
14727 {
14728 	struct ieee80211_hw *hw = ah->hw;
14729 	struct wiphy *wiphy = hw->wiphy;
14730 	struct ath12k *ar = ath12k_ah_to_ar(ah, 0);
14731 	struct ath12k_base *ab = ar->ab;
14732 	struct ath12k_pdev *pdev;
14733 	struct ath12k_pdev_cap *cap;
14734 	static const u32 cipher_suites[] = {
14735 		WLAN_CIPHER_SUITE_TKIP,
14736 		WLAN_CIPHER_SUITE_CCMP,
14737 		WLAN_CIPHER_SUITE_AES_CMAC,
14738 		WLAN_CIPHER_SUITE_BIP_CMAC_256,
14739 		WLAN_CIPHER_SUITE_BIP_GMAC_128,
14740 		WLAN_CIPHER_SUITE_BIP_GMAC_256,
14741 		WLAN_CIPHER_SUITE_GCMP,
14742 		WLAN_CIPHER_SUITE_GCMP_256,
14743 		WLAN_CIPHER_SUITE_CCMP_256,
14744 	};
14745 	int ret, i, j;
14746 	u32 ht_cap = U32_MAX, antennas_rx = 0, antennas_tx = 0;
14747 	bool is_6ghz = false, is_raw_mode = false, is_monitor_disable = false;
14748 	u8 *mac_addr = NULL;
14749 	u8 mbssid_max_interfaces = 0;
14750 
14751 	wiphy->max_ap_assoc_sta = 0;
14752 
14753 	for_each_ar(ah, ar, i) {
14754 		u32 ht_cap_info = 0;
14755 
14756 		pdev = ar->pdev;
14757 		if (ar->ab->pdevs_macaddr_valid) {
14758 			ether_addr_copy(ar->mac_addr, pdev->mac_addr);
14759 		} else {
14760 			ether_addr_copy(ar->mac_addr, ar->ab->mac_addr);
14761 			ar->mac_addr[4] += ar->pdev_idx;
14762 		}
14763 
14764 		ret = ath12k_mac_setup_register(ar, &ht_cap_info, hw->wiphy->bands);
14765 		if (ret)
14766 			goto err_cleanup_unregister;
14767 
14768 		/* 6 GHz does not support HT Cap, hence do not consider it */
14769 		if (!ar->supports_6ghz)
14770 			ht_cap &= ht_cap_info;
14771 
14772 		wiphy->max_ap_assoc_sta += ar->max_num_stations;
14773 
14774 		/* Advertise the max antenna support of all radios, driver can handle
14775 		 * per pdev specific antenna setting based on pdev cap when antenna
14776 		 * changes are made
14777 		 */
14778 		cap = &pdev->cap;
14779 
14780 		antennas_rx = max_t(u32, antennas_rx, cap->rx_chain_mask);
14781 		antennas_tx = max_t(u32, antennas_tx, cap->tx_chain_mask);
14782 
14783 		if (ar->supports_6ghz)
14784 			is_6ghz = true;
14785 
14786 		if (test_bit(ATH12K_FLAG_RAW_MODE, &ar->ab->dev_flags))
14787 			is_raw_mode = true;
14788 
14789 		if (!ar->ab->hw_params->supports_monitor)
14790 			is_monitor_disable = true;
14791 
14792 		if (i == 0)
14793 			mac_addr = ar->mac_addr;
14794 		else
14795 			mac_addr = ab->mac_addr;
14796 
14797 		mbssid_max_interfaces += TARGET_NUM_VDEVS(ar->ab);
14798 	}
14799 
14800 	wiphy->available_antennas_rx = antennas_rx;
14801 	wiphy->available_antennas_tx = antennas_tx;
14802 
14803 	SET_IEEE80211_PERM_ADDR(hw, mac_addr);
14804 	SET_IEEE80211_DEV(hw, ab->dev);
14805 
14806 	ret = ath12k_mac_setup_iface_combinations(ah);
14807 	if (ret) {
14808 		ath12k_err(ab, "failed to setup interface combinations: %d\n", ret);
14809 		goto err_complete_cleanup_unregister;
14810 	}
14811 
14812 	wiphy->interface_modes = ath12k_mac_get_ifmodes(ah);
14813 
14814 	if (ah->num_radio == 1 &&
14815 	    wiphy->bands[NL80211_BAND_2GHZ] &&
14816 	    wiphy->bands[NL80211_BAND_5GHZ] &&
14817 	    wiphy->bands[NL80211_BAND_6GHZ])
14818 		ieee80211_hw_set(hw, SINGLE_SCAN_ON_ALL_BANDS);
14819 
14820 	ieee80211_hw_set(hw, SIGNAL_DBM);
14821 	ieee80211_hw_set(hw, SUPPORTS_PS);
14822 	ieee80211_hw_set(hw, SUPPORTS_DYNAMIC_PS);
14823 	ieee80211_hw_set(hw, MFP_CAPABLE);
14824 	ieee80211_hw_set(hw, REPORTS_TX_ACK_STATUS);
14825 	ieee80211_hw_set(hw, HAS_RATE_CONTROL);
14826 	ieee80211_hw_set(hw, AP_LINK_PS);
14827 	ieee80211_hw_set(hw, SPECTRUM_MGMT);
14828 	ieee80211_hw_set(hw, CONNECTION_MONITOR);
14829 	ieee80211_hw_set(hw, SUPPORTS_PER_STA_GTK);
14830 	ieee80211_hw_set(hw, CHANCTX_STA_CSA);
14831 	ieee80211_hw_set(hw, QUEUE_CONTROL);
14832 	ieee80211_hw_set(hw, SUPPORTS_TX_FRAG);
14833 	ieee80211_hw_set(hw, REPORTS_LOW_ACK);
14834 	ieee80211_hw_set(hw, NO_VIRTUAL_MONITOR);
14835 
14836 	if (test_bit(WMI_TLV_SERVICE_ETH_OFFLOAD, ar->wmi->wmi_ab->svc_map)) {
14837 		ieee80211_hw_set(hw, SUPPORTS_TX_ENCAP_OFFLOAD);
14838 		ieee80211_hw_set(hw, SUPPORTS_RX_DECAP_OFFLOAD);
14839 	}
14840 
14841 	if (cap->nss_ratio_enabled)
14842 		ieee80211_hw_set(hw, SUPPORTS_VHT_EXT_NSS_BW);
14843 
14844 	if ((ht_cap & WMI_HT_CAP_ENABLED) || is_6ghz) {
14845 		ieee80211_hw_set(hw, AMPDU_AGGREGATION);
14846 		ieee80211_hw_set(hw, TX_AMPDU_SETUP_IN_HW);
14847 		ieee80211_hw_set(hw, SUPPORTS_REORDERING_BUFFER);
14848 		ieee80211_hw_set(hw, SUPPORTS_AMSDU_IN_AMPDU);
14849 		ieee80211_hw_set(hw, USES_RSS);
14850 	}
14851 
14852 	wiphy->features |= NL80211_FEATURE_STATIC_SMPS;
14853 	wiphy->flags |= WIPHY_FLAG_IBSS_RSN;
14854 
14855 	/* TODO: Check if HT capability advertised from firmware is different
14856 	 * for each band for a dual band capable radio. It will be tricky to
14857 	 * handle it when the ht capability different for each band.
14858 	 */
14859 	if (ht_cap & WMI_HT_CAP_DYNAMIC_SMPS ||
14860 	    (is_6ghz && ab->hw_params->supports_dynamic_smps_6ghz))
14861 		wiphy->features |= NL80211_FEATURE_DYNAMIC_SMPS;
14862 
14863 	wiphy->max_scan_ssids = WLAN_SCAN_PARAMS_MAX_SSID;
14864 	wiphy->max_scan_ie_len = WLAN_SCAN_PARAMS_MAX_IE_LEN;
14865 
14866 	hw->max_listen_interval = ATH12K_MAX_HW_LISTEN_INTERVAL;
14867 
14868 	wiphy->flags |= WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL;
14869 	wiphy->flags |= WIPHY_FLAG_HAS_CHANNEL_SWITCH;
14870 	wiphy->max_remain_on_channel_duration = 5000;
14871 
14872 	wiphy->flags |= WIPHY_FLAG_AP_UAPSD;
14873 	wiphy->features |= NL80211_FEATURE_AP_MODE_CHAN_WIDTH_CHANGE |
14874 				   NL80211_FEATURE_AP_SCAN;
14875 
14876 	wiphy->features |= NL80211_FEATURE_TX_POWER_INSERTION;
14877 
14878 	/* MLO is not yet supported so disable Wireless Extensions for now
14879 	 * to make sure ath12k users don't use it. This flag can be removed
14880 	 * once WIPHY_FLAG_SUPPORTS_MLO is enabled.
14881 	 */
14882 	wiphy->flags |= WIPHY_FLAG_DISABLE_WEXT;
14883 
14884 	/* Copy over MLO related capabilities received from
14885 	 * WMI_SERVICE_READY_EXT2_EVENT if single_chip_mlo_supp is set.
14886 	 */
14887 	if (ab->ag->mlo_capable) {
14888 		ath12k_iftypes_ext_capa[2].eml_capabilities = cap->eml_cap;
14889 		ath12k_iftypes_ext_capa[2].mld_capa_and_ops = cap->mld_cap;
14890 		wiphy->flags |= WIPHY_FLAG_SUPPORTS_MLO;
14891 
14892 		ieee80211_hw_set(hw, MLO_MCAST_MULTI_LINK_TX);
14893 	}
14894 
14895 	hw->queues = ATH12K_HW_MAX_QUEUES;
14896 	wiphy->tx_queue_len = ATH12K_QUEUE_LEN;
14897 	hw->offchannel_tx_hw_queue = ATH12K_HW_MAX_QUEUES - 1;
14898 	hw->max_rx_aggregation_subframes = IEEE80211_MAX_AMPDU_BUF_EHT;
14899 
14900 	hw->vif_data_size = sizeof(struct ath12k_vif);
14901 	hw->sta_data_size = sizeof(struct ath12k_sta);
14902 	hw->extra_tx_headroom = ab->hw_params->iova_mask;
14903 
14904 	wiphy_ext_feature_set(wiphy, NL80211_EXT_FEATURE_CQM_RSSI_LIST);
14905 	wiphy_ext_feature_set(wiphy, NL80211_EXT_FEATURE_STA_TX_PWR);
14906 	wiphy_ext_feature_set(wiphy, NL80211_EXT_FEATURE_ACK_SIGNAL_SUPPORT);
14907 	if (test_bit(WMI_TLV_SERVICE_BSS_COLOR_OFFLOAD,
14908 		     ab->wmi_ab.svc_map)) {
14909 		wiphy_ext_feature_set(wiphy, NL80211_EXT_FEATURE_BSS_COLOR);
14910 		ieee80211_hw_set(hw, DETECTS_COLOR_COLLISION);
14911 	}
14912 
14913 	wiphy->cipher_suites = cipher_suites;
14914 	wiphy->n_cipher_suites = ARRAY_SIZE(cipher_suites);
14915 
14916 	wiphy->iftype_ext_capab = ath12k_iftypes_ext_capa;
14917 	wiphy->num_iftype_ext_capab = ARRAY_SIZE(ath12k_iftypes_ext_capa);
14918 
14919 	wiphy->mbssid_max_interfaces = mbssid_max_interfaces;
14920 	wiphy->ema_max_profile_periodicity = TARGET_EMA_MAX_PROFILE_PERIOD;
14921 	ieee80211_hw_set(hw, SUPPORTS_MULTI_BSSID);
14922 
14923 	if (is_6ghz) {
14924 		wiphy_ext_feature_set(wiphy,
14925 				      NL80211_EXT_FEATURE_FILS_DISCOVERY);
14926 		wiphy_ext_feature_set(wiphy,
14927 				      NL80211_EXT_FEATURE_UNSOL_BCAST_PROBE_RESP);
14928 	}
14929 
14930 	wiphy_ext_feature_set(wiphy, NL80211_EXT_FEATURE_PUNCT);
14931 	if (test_bit(WMI_TLV_SERVICE_BEACON_PROTECTION_SUPPORT, ab->wmi_ab.svc_map))
14932 		wiphy_ext_feature_set(hw->wiphy, NL80211_EXT_FEATURE_BEACON_PROTECTION);
14933 
14934 	ath12k_reg_init(hw);
14935 
14936 	if (!is_raw_mode) {
14937 		hw->netdev_features = NETIF_F_HW_CSUM;
14938 		ieee80211_hw_set(hw, SW_CRYPTO_CONTROL);
14939 		ieee80211_hw_set(hw, SUPPORT_FAST_XMIT);
14940 	}
14941 
14942 	if (test_bit(WMI_TLV_SERVICE_NLO, ar->wmi->wmi_ab->svc_map)) {
14943 		wiphy->max_sched_scan_ssids = WMI_PNO_MAX_SUPP_NETWORKS;
14944 		wiphy->max_match_sets = WMI_PNO_MAX_SUPP_NETWORKS;
14945 		wiphy->max_sched_scan_ie_len = WMI_PNO_MAX_IE_LENGTH;
14946 		wiphy->max_sched_scan_plans = WMI_PNO_MAX_SCHED_SCAN_PLANS;
14947 		wiphy->max_sched_scan_plan_interval =
14948 					WMI_PNO_MAX_SCHED_SCAN_PLAN_INT;
14949 		wiphy->max_sched_scan_plan_iterations =
14950 					WMI_PNO_MAX_SCHED_SCAN_PLAN_ITRNS;
14951 		wiphy->features |= NL80211_FEATURE_ND_RANDOM_MAC_ADDR;
14952 	}
14953 
14954 	ret = ath12k_wow_init(ar);
14955 	if (ret) {
14956 		ath12k_warn(ar->ab, "failed to init wow: %d\n", ret);
14957 		goto err_cleanup_if_combs;
14958 	}
14959 
14960 	/* Boot-time regulatory updates have already been processed.
14961 	 * Mark them as complete now, because after registration,
14962 	 * cfg80211 will notify us again if there are any pending hints.
14963 	 * We need to wait for those hints to be processed, so it's
14964 	 * important to mark the boot-time updates as complete before
14965 	 * proceeding with registration.
14966 	 */
14967 	for_each_ar(ah, ar, i)
14968 		complete_all(&ar->regd_update_completed);
14969 
14970 	ret = ieee80211_register_hw(hw);
14971 	if (ret) {
14972 		ath12k_err(ab, "ieee80211 registration failed: %d\n", ret);
14973 		goto err_cleanup_if_combs;
14974 	}
14975 
14976 	if (is_monitor_disable)
14977 		/* There's a race between calling ieee80211_register_hw()
14978 		 * and here where the monitor mode is enabled for a little
14979 		 * while. But that time is so short and in practice it doesn't make
14980 		 * a difference in real life.
14981 		 */
14982 		wiphy->interface_modes &= ~BIT(NL80211_IFTYPE_MONITOR);
14983 
14984 	for_each_ar(ah, ar, i) {
14985 		/* Apply the regd received during initialization */
14986 		ret = ath12k_regd_update(ar, true);
14987 		if (ret) {
14988 			ath12k_err(ar->ab, "ath12k regd update failed: %d\n", ret);
14989 			goto err_unregister_hw;
14990 		}
14991 
14992 		if (ar->ab->hw_params->current_cc_support && ab->new_alpha2[0]) {
14993 			struct wmi_set_current_country_arg current_cc = {};
14994 
14995 			memcpy(&current_cc.alpha2, ab->new_alpha2, 2);
14996 			memcpy(&ar->alpha2, ab->new_alpha2, 2);
14997 
14998 			reinit_completion(&ar->regd_update_completed);
14999 
15000 			ret = ath12k_wmi_send_set_current_country_cmd(ar, &current_cc);
15001 			if (ret)
15002 				ath12k_warn(ar->ab,
15003 					    "failed set cc code for mac register: %d\n",
15004 					    ret);
15005 		}
15006 
15007 		ath12k_fw_stats_init(ar);
15008 		ath12k_debugfs_register(ar);
15009 	}
15010 
15011 	return 0;
15012 
15013 err_unregister_hw:
15014 	for_each_ar(ah, ar, i)
15015 		ath12k_debugfs_unregister(ar);
15016 
15017 	ieee80211_unregister_hw(hw);
15018 
15019 err_cleanup_if_combs:
15020 	ath12k_mac_cleanup_iface_combinations(ah);
15021 
15022 err_complete_cleanup_unregister:
15023 	i = ah->num_radio;
15024 
15025 err_cleanup_unregister:
15026 	for (j = 0; j < i; j++) {
15027 		ar = ath12k_ah_to_ar(ah, j);
15028 		ath12k_mac_cleanup_unregister(ar);
15029 	}
15030 
15031 	SET_IEEE80211_DEV(hw, NULL);
15032 
15033 	return ret;
15034 }
15035 
15036 static void ath12k_mac_setup(struct ath12k *ar)
15037 {
15038 	struct ath12k_base *ab = ar->ab;
15039 	struct ath12k_pdev *pdev = ar->pdev;
15040 	u8 pdev_idx = ar->pdev_idx;
15041 
15042 	ar->lmac_id = ath12k_hw_get_mac_from_pdev_id(ab->hw_params, pdev_idx);
15043 
15044 	ar->wmi = &ab->wmi_ab.wmi[pdev_idx];
15045 	/* FIXME: wmi[0] is already initialized during attach,
15046 	 * Should we do this again?
15047 	 */
15048 	ath12k_wmi_pdev_attach(ab, pdev_idx);
15049 
15050 	ar->cfg_tx_chainmask = pdev->cap.tx_chain_mask;
15051 	ar->cfg_rx_chainmask = pdev->cap.rx_chain_mask;
15052 	ar->num_tx_chains = hweight32(pdev->cap.tx_chain_mask);
15053 	ar->num_rx_chains = hweight32(pdev->cap.rx_chain_mask);
15054 	ar->scan.arvif = NULL;
15055 	ar->vdev_id_11d_scan = ATH12K_11D_INVALID_VDEV_ID;
15056 
15057 	spin_lock_init(&ar->data_lock);
15058 	spin_lock_init(&ar->dp.ppdu_list_lock);
15059 	INIT_LIST_HEAD(&ar->arvifs);
15060 	INIT_LIST_HEAD(&ar->dp.ppdu_stats_info);
15061 
15062 	init_completion(&ar->vdev_setup_done);
15063 	init_completion(&ar->vdev_delete_done);
15064 	init_completion(&ar->peer_assoc_done);
15065 	init_completion(&ar->peer_delete_done);
15066 	init_completion(&ar->install_key_done);
15067 	init_completion(&ar->bss_survey_done);
15068 	init_completion(&ar->scan.started);
15069 	init_completion(&ar->scan.completed);
15070 	init_completion(&ar->scan.on_channel);
15071 	init_completion(&ar->mlo_setup_done);
15072 	init_completion(&ar->completed_11d_scan);
15073 	init_completion(&ar->regd_update_completed);
15074 	init_completion(&ar->thermal.wmi_sync);
15075 	mutex_init(&ar->thermal.lock);
15076 
15077 	ar->thermal.temperature = 0;
15078 	ar->thermal.hwmon_dev = NULL;
15079 
15080 	INIT_DELAYED_WORK(&ar->scan.timeout, ath12k_scan_timeout_work);
15081 	wiphy_work_init(&ar->scan.vdev_clean_wk, ath12k_scan_vdev_clean_work);
15082 	INIT_WORK(&ar->regd_channel_update_work, ath12k_regd_update_chan_list_work);
15083 	INIT_LIST_HEAD(&ar->regd_channel_update_queue);
15084 	INIT_WORK(&ar->regd_update_work, ath12k_regd_update_work);
15085 
15086 	wiphy_work_init(&ar->wmi_mgmt_tx_work, ath12k_mgmt_over_wmi_tx_work);
15087 	skb_queue_head_init(&ar->wmi_mgmt_tx_queue);
15088 
15089 	ar->monitor_vdev_id = -1;
15090 	ar->monitor_vdev_created = false;
15091 	ar->monitor_started = false;
15092 }
15093 
15094 static int __ath12k_mac_mlo_setup(struct ath12k *ar)
15095 {
15096 	u8 num_link = 0, partner_link_id[ATH12K_GROUP_MAX_RADIO] = {};
15097 	struct ath12k_base *partner_ab, *ab = ar->ab;
15098 	struct ath12k_hw_group *ag = ab->ag;
15099 	struct wmi_mlo_setup_arg mlo = {};
15100 	struct ath12k_pdev *pdev;
15101 	unsigned long time_left;
15102 	int i, j, ret;
15103 
15104 	lockdep_assert_held(&ag->mutex);
15105 
15106 	reinit_completion(&ar->mlo_setup_done);
15107 
15108 	for (i = 0; i < ag->num_devices; i++) {
15109 		partner_ab = ag->ab[i];
15110 
15111 		for (j = 0; j < partner_ab->num_radios; j++) {
15112 			pdev = &partner_ab->pdevs[j];
15113 
15114 			/* Avoid the self link */
15115 			if (ar == pdev->ar)
15116 				continue;
15117 
15118 			partner_link_id[num_link] = pdev->hw_link_id;
15119 			num_link++;
15120 
15121 			ath12k_dbg(ab, ATH12K_DBG_MAC, "device %d pdev %d hw_link_id %d num_link %d\n",
15122 				   i, j, pdev->hw_link_id, num_link);
15123 		}
15124 	}
15125 
15126 	if (num_link == 0)
15127 		return 0;
15128 
15129 	mlo.group_id = cpu_to_le32(ag->id);
15130 	mlo.partner_link_id = partner_link_id;
15131 	mlo.num_partner_links = num_link;
15132 	ar->mlo_setup_status = 0;
15133 
15134 	ath12k_dbg(ab, ATH12K_DBG_MAC, "group id %d num_link %d\n", ag->id, num_link);
15135 
15136 	ret = ath12k_wmi_mlo_setup(ar, &mlo);
15137 	if (ret) {
15138 		ath12k_err(ab, "failed to send  setup MLO WMI command for pdev %d: %d\n",
15139 			   ar->pdev_idx, ret);
15140 		return ret;
15141 	}
15142 
15143 	time_left = wait_for_completion_timeout(&ar->mlo_setup_done,
15144 						WMI_MLO_CMD_TIMEOUT_HZ);
15145 
15146 	if (!time_left || ar->mlo_setup_status)
15147 		return ar->mlo_setup_status ? : -ETIMEDOUT;
15148 
15149 	ath12k_dbg(ab, ATH12K_DBG_MAC, "mlo setup done for pdev %d\n", ar->pdev_idx);
15150 
15151 	return 0;
15152 }
15153 
15154 static int __ath12k_mac_mlo_teardown(struct ath12k *ar)
15155 {
15156 	struct ath12k_base *ab = ar->ab;
15157 	int ret;
15158 	u8 num_link;
15159 
15160 	if (test_bit(ATH12K_FLAG_RECOVERY, &ab->dev_flags))
15161 		return 0;
15162 
15163 	num_link = ath12k_get_num_partner_link(ar);
15164 
15165 	if (num_link == 0)
15166 		return 0;
15167 
15168 	ret = ath12k_wmi_mlo_teardown(ar);
15169 	if (ret) {
15170 		ath12k_warn(ab, "failed to send MLO teardown WMI command for pdev %d: %d\n",
15171 			    ar->pdev_idx, ret);
15172 		return ret;
15173 	}
15174 
15175 	ath12k_dbg(ab, ATH12K_DBG_MAC, "mlo teardown for pdev %d\n", ar->pdev_idx);
15176 
15177 	return 0;
15178 }
15179 
15180 int ath12k_mac_mlo_setup(struct ath12k_hw_group *ag)
15181 {
15182 	struct ath12k_hw *ah;
15183 	struct ath12k *ar;
15184 	int ret;
15185 	int i, j;
15186 
15187 	for (i = 0; i < ag->num_hw; i++) {
15188 		ah = ag->ah[i];
15189 		if (!ah)
15190 			continue;
15191 
15192 		for_each_ar(ah, ar, j) {
15193 			ar = &ah->radio[j];
15194 			ret = __ath12k_mac_mlo_setup(ar);
15195 			if (ret) {
15196 				ath12k_err(ar->ab, "failed to setup MLO: %d\n", ret);
15197 				goto err_setup;
15198 			}
15199 		}
15200 	}
15201 
15202 	return 0;
15203 
15204 err_setup:
15205 	for (i = i - 1; i >= 0; i--) {
15206 		ah = ag->ah[i];
15207 		if (!ah)
15208 			continue;
15209 
15210 		for (j = j - 1; j >= 0; j--) {
15211 			ar = &ah->radio[j];
15212 			if (!ar)
15213 				continue;
15214 
15215 			__ath12k_mac_mlo_teardown(ar);
15216 		}
15217 	}
15218 
15219 	return ret;
15220 }
15221 
15222 void ath12k_mac_mlo_teardown(struct ath12k_hw_group *ag)
15223 {
15224 	struct ath12k_hw *ah;
15225 	struct ath12k *ar;
15226 	int ret, i, j;
15227 
15228 	for (i = 0; i < ag->num_hw; i++) {
15229 		ah = ag->ah[i];
15230 		if (!ah)
15231 			continue;
15232 
15233 		for_each_ar(ah, ar, j) {
15234 			ar = &ah->radio[j];
15235 			ret = __ath12k_mac_mlo_teardown(ar);
15236 			if (ret) {
15237 				ath12k_err(ar->ab, "failed to teardown MLO: %d\n", ret);
15238 				break;
15239 			}
15240 		}
15241 	}
15242 }
15243 
15244 int ath12k_mac_register(struct ath12k_hw_group *ag)
15245 {
15246 	struct ath12k_hw *ah;
15247 	int i;
15248 	int ret;
15249 
15250 	for (i = 0; i < ag->num_hw; i++) {
15251 		ah = ath12k_ag_to_ah(ag, i);
15252 
15253 		ret = ath12k_mac_hw_register(ah);
15254 		if (ret)
15255 			goto err;
15256 	}
15257 
15258 	return 0;
15259 
15260 err:
15261 	for (i = i - 1; i >= 0; i--) {
15262 		ah = ath12k_ag_to_ah(ag, i);
15263 		if (!ah)
15264 			continue;
15265 
15266 		ath12k_mac_hw_unregister(ah);
15267 	}
15268 
15269 	return ret;
15270 }
15271 
15272 void ath12k_mac_unregister(struct ath12k_hw_group *ag)
15273 {
15274 	struct ath12k_hw *ah;
15275 	int i;
15276 
15277 	for (i = ag->num_hw - 1; i >= 0; i--) {
15278 		ah = ath12k_ag_to_ah(ag, i);
15279 		if (!ah)
15280 			continue;
15281 
15282 		ath12k_mac_hw_unregister(ah);
15283 	}
15284 }
15285 
15286 static void ath12k_mac_hw_destroy(struct ath12k_hw *ah)
15287 {
15288 	ieee80211_free_hw(ah->hw);
15289 }
15290 
15291 static struct ath12k_hw *ath12k_mac_hw_allocate(struct ath12k_hw_group *ag,
15292 						struct ath12k_pdev_map *pdev_map,
15293 						u8 num_pdev_map)
15294 {
15295 	struct ieee80211_hw *hw;
15296 	struct ath12k *ar;
15297 	struct ath12k_base *ab;
15298 	struct ath12k_pdev *pdev;
15299 	struct ath12k_hw *ah;
15300 	int i;
15301 	u8 pdev_idx;
15302 
15303 	hw = ieee80211_alloc_hw(struct_size(ah, radio, num_pdev_map),
15304 				pdev_map->ab->ath12k_ops);
15305 	if (!hw)
15306 		return NULL;
15307 
15308 	ah = ath12k_hw_to_ah(hw);
15309 	ah->hw = hw;
15310 	ah->num_radio = num_pdev_map;
15311 
15312 	mutex_init(&ah->hw_mutex);
15313 
15314 	spin_lock_init(&ah->dp_hw.peer_lock);
15315 	INIT_LIST_HEAD(&ah->dp_hw.dp_peers_list);
15316 
15317 	for (i = 0; i < num_pdev_map; i++) {
15318 		ab = pdev_map[i].ab;
15319 		pdev_idx = pdev_map[i].pdev_idx;
15320 		pdev = &ab->pdevs[pdev_idx];
15321 
15322 		ar = ath12k_ah_to_ar(ah, i);
15323 		ar->ah = ah;
15324 		ar->ab = ab;
15325 		ar->hw_link_id = pdev->hw_link_id;
15326 		ar->pdev = pdev;
15327 		ar->pdev_idx = pdev_idx;
15328 		ar->radio_idx = i;
15329 		pdev->ar = ar;
15330 
15331 		ag->hw_links[ar->hw_link_id].device_id = ab->device_id;
15332 		ag->hw_links[ar->hw_link_id].pdev_idx = pdev_idx;
15333 
15334 		ath12k_mac_setup(ar);
15335 		ath12k_dp_pdev_pre_alloc(ar);
15336 	}
15337 
15338 	return ah;
15339 }
15340 
15341 void ath12k_mac_destroy(struct ath12k_hw_group *ag)
15342 {
15343 	struct ath12k_pdev *pdev;
15344 	struct ath12k_base *ab = ag->ab[0];
15345 	int i, j;
15346 	struct ath12k_hw *ah;
15347 
15348 	for (i = 0; i < ag->num_devices; i++) {
15349 		ab = ag->ab[i];
15350 		if (!ab)
15351 			continue;
15352 
15353 		for (j = 0; j < ab->num_radios; j++) {
15354 			pdev = &ab->pdevs[j];
15355 			if (!pdev->ar)
15356 				continue;
15357 			pdev->ar = NULL;
15358 		}
15359 	}
15360 
15361 	for (i = 0; i < ag->num_hw; i++) {
15362 		ah = ath12k_ag_to_ah(ag, i);
15363 		if (!ah)
15364 			continue;
15365 
15366 		ath12k_mac_hw_destroy(ah);
15367 		ath12k_ag_set_ah(ag, i, NULL);
15368 	}
15369 }
15370 
15371 static void ath12k_mac_set_device_defaults(struct ath12k_base *ab)
15372 {
15373 	int total_vdev;
15374 
15375 	/* Initialize channel counters frequency value in hertz */
15376 	ab->cc_freq_hz = 320000;
15377 	total_vdev = ab->num_radios * TARGET_NUM_VDEVS(ab);
15378 	ab->free_vdev_map = (1LL << total_vdev) - 1;
15379 }
15380 
15381 int ath12k_mac_allocate(struct ath12k_hw_group *ag)
15382 {
15383 	struct ath12k_pdev_map pdev_map[ATH12K_GROUP_MAX_RADIO];
15384 	int mac_id, device_id, total_radio, num_hw;
15385 	struct ath12k_base *ab;
15386 	struct ath12k_hw *ah;
15387 	int ret, i, j;
15388 	u8 radio_per_hw;
15389 
15390 	total_radio = 0;
15391 	for (i = 0; i < ag->num_devices; i++) {
15392 		ab = ag->ab[i];
15393 		if (!ab)
15394 			continue;
15395 
15396 		ath12k_mac_set_device_defaults(ab);
15397 		total_radio += ab->num_radios;
15398 	}
15399 
15400 	if (!total_radio)
15401 		return -EINVAL;
15402 
15403 	if (WARN_ON(total_radio > ATH12K_GROUP_MAX_RADIO))
15404 		return -ENOSPC;
15405 
15406 	/* All pdev get combined and register as single wiphy based on
15407 	 * hardware group which participate in multi-link operation else
15408 	 * each pdev get register separately.
15409 	 */
15410 	if (ag->mlo_capable)
15411 		radio_per_hw = total_radio;
15412 	else
15413 		radio_per_hw = 1;
15414 
15415 	num_hw = total_radio / radio_per_hw;
15416 
15417 	ag->num_hw = 0;
15418 	device_id = 0;
15419 	mac_id = 0;
15420 	for (i = 0; i < num_hw; i++) {
15421 		for (j = 0; j < radio_per_hw; j++) {
15422 			if (device_id >= ag->num_devices || !ag->ab[device_id]) {
15423 				ret = -ENOSPC;
15424 				goto err;
15425 			}
15426 
15427 			ab = ag->ab[device_id];
15428 			pdev_map[j].ab = ab;
15429 			pdev_map[j].pdev_idx = mac_id;
15430 			mac_id++;
15431 
15432 			/* If mac_id falls beyond the current device MACs then
15433 			 * move to next device
15434 			 */
15435 			if (mac_id >= ab->num_radios) {
15436 				mac_id = 0;
15437 				device_id++;
15438 			}
15439 		}
15440 
15441 		ab = pdev_map->ab;
15442 
15443 		ah = ath12k_mac_hw_allocate(ag, pdev_map, radio_per_hw);
15444 		if (!ah) {
15445 			ath12k_warn(ab, "failed to allocate mac80211 hw device for hw_idx %d\n",
15446 				    i);
15447 			ret = -ENOMEM;
15448 			goto err;
15449 		}
15450 
15451 		ah->dev = ab->dev;
15452 
15453 		ag->ah[i] = ah;
15454 		ag->num_hw++;
15455 	}
15456 
15457 	return 0;
15458 
15459 err:
15460 	for (i = i - 1; i >= 0; i--) {
15461 		ah = ath12k_ag_to_ah(ag, i);
15462 		if (!ah)
15463 			continue;
15464 
15465 		ath12k_mac_hw_destroy(ah);
15466 		ath12k_ag_set_ah(ag, i, NULL);
15467 	}
15468 
15469 	return ret;
15470 }
15471 
15472 int ath12k_mac_vif_set_keepalive(struct ath12k_link_vif *arvif,
15473 				 enum wmi_sta_keepalive_method method,
15474 				 u32 interval)
15475 {
15476 	struct wmi_sta_keepalive_arg arg = {};
15477 	struct ath12k *ar = arvif->ar;
15478 	int ret;
15479 
15480 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
15481 
15482 	if (arvif->ahvif->vdev_type != WMI_VDEV_TYPE_STA)
15483 		return 0;
15484 
15485 	if (!test_bit(WMI_TLV_SERVICE_STA_KEEP_ALIVE, ar->ab->wmi_ab.svc_map))
15486 		return 0;
15487 
15488 	arg.vdev_id = arvif->vdev_id;
15489 	arg.enabled = 1;
15490 	arg.method = method;
15491 	arg.interval = interval;
15492 
15493 	ret = ath12k_wmi_sta_keepalive(ar, &arg);
15494 	if (ret) {
15495 		ath12k_warn(ar->ab, "failed to set keepalive on vdev %i: %d\n",
15496 			    arvif->vdev_id, ret);
15497 		return ret;
15498 	}
15499 
15500 	return 0;
15501 }
15502