xref: /linux/drivers/net/wireless/ath/ath12k/mac.c (revision 90e63d5354951d37fa2b3b91e6f17b95d2bf9bee)
1 // SPDX-License-Identifier: BSD-3-Clause-Clear
2 /*
3  * Copyright (c) 2018-2021 The Linux Foundation. All rights reserved.
4  * Copyright (c) Qualcomm Technologies, Inc. and/or its subsidiaries.
5  */
6 
7 #include <net/mac80211.h>
8 #include <net/cfg80211.h>
9 #include <linux/etherdevice.h>
10 
11 #include "mac.h"
12 #include "core.h"
13 #include "debug.h"
14 #include "wmi.h"
15 #include "hw.h"
16 #include "dp_tx.h"
17 #include "dp_rx.h"
18 #include "testmode.h"
19 #include "peer.h"
20 #include "debugfs.h"
21 #include "hif.h"
22 #include "wow.h"
23 #include "debugfs_sta.h"
24 #include "dp.h"
25 #include "dp_cmn.h"
26 
27 #define CHAN2G(_channel, _freq, _flags) { \
28 	.band                   = NL80211_BAND_2GHZ, \
29 	.hw_value               = (_channel), \
30 	.center_freq            = (_freq), \
31 	.flags                  = (_flags), \
32 	.max_antenna_gain       = 0, \
33 	.max_power              = 30, \
34 }
35 
36 #define CHAN5G(_channel, _freq, _flags) { \
37 	.band                   = NL80211_BAND_5GHZ, \
38 	.hw_value               = (_channel), \
39 	.center_freq            = (_freq), \
40 	.flags                  = (_flags), \
41 	.max_antenna_gain       = 0, \
42 	.max_power              = 30, \
43 }
44 
45 #define CHAN6G(_channel, _freq, _flags) { \
46 	.band                   = NL80211_BAND_6GHZ, \
47 	.hw_value               = (_channel), \
48 	.center_freq            = (_freq), \
49 	.flags                  = (_flags), \
50 	.max_antenna_gain       = 0, \
51 	.max_power              = 30, \
52 }
53 
54 #define ATH12K_5_9_GHZ_MIN_FREQ 5845
55 #define ATH12K_5_9_GHZ_MAX_FREQ 5885
56 
57 static const struct ieee80211_channel ath12k_2ghz_channels[] = {
58 	CHAN2G(1, 2412, 0),
59 	CHAN2G(2, 2417, 0),
60 	CHAN2G(3, 2422, 0),
61 	CHAN2G(4, 2427, 0),
62 	CHAN2G(5, 2432, 0),
63 	CHAN2G(6, 2437, 0),
64 	CHAN2G(7, 2442, 0),
65 	CHAN2G(8, 2447, 0),
66 	CHAN2G(9, 2452, 0),
67 	CHAN2G(10, 2457, 0),
68 	CHAN2G(11, 2462, 0),
69 	CHAN2G(12, 2467, 0),
70 	CHAN2G(13, 2472, 0),
71 	CHAN2G(14, 2484, 0),
72 };
73 
74 static const struct ieee80211_channel ath12k_5ghz_channels[] = {
75 	CHAN5G(36, 5180, 0),
76 	CHAN5G(40, 5200, 0),
77 	CHAN5G(44, 5220, 0),
78 	CHAN5G(48, 5240, 0),
79 	CHAN5G(52, 5260, 0),
80 	CHAN5G(56, 5280, 0),
81 	CHAN5G(60, 5300, 0),
82 	CHAN5G(64, 5320, 0),
83 	CHAN5G(100, 5500, 0),
84 	CHAN5G(104, 5520, 0),
85 	CHAN5G(108, 5540, 0),
86 	CHAN5G(112, 5560, 0),
87 	CHAN5G(116, 5580, 0),
88 	CHAN5G(120, 5600, 0),
89 	CHAN5G(124, 5620, 0),
90 	CHAN5G(128, 5640, 0),
91 	CHAN5G(132, 5660, 0),
92 	CHAN5G(136, 5680, 0),
93 	CHAN5G(140, 5700, 0),
94 	CHAN5G(144, 5720, 0),
95 	CHAN5G(149, 5745, 0),
96 	CHAN5G(153, 5765, 0),
97 	CHAN5G(157, 5785, 0),
98 	CHAN5G(161, 5805, 0),
99 	CHAN5G(165, 5825, 0),
100 	CHAN5G(169, 5845, 0),
101 	CHAN5G(173, 5865, 0),
102 	CHAN5G(177, 5885, 0),
103 };
104 
105 static const struct ieee80211_channel ath12k_6ghz_channels[] = {
106 	/* Operating Class 136 */
107 	CHAN6G(2, 5935, 0),
108 
109 	/* Operating Classes 131-135 */
110 	CHAN6G(1, 5955, 0),
111 	CHAN6G(5, 5975, 0),
112 	CHAN6G(9, 5995, 0),
113 	CHAN6G(13, 6015, 0),
114 	CHAN6G(17, 6035, 0),
115 	CHAN6G(21, 6055, 0),
116 	CHAN6G(25, 6075, 0),
117 	CHAN6G(29, 6095, 0),
118 	CHAN6G(33, 6115, 0),
119 	CHAN6G(37, 6135, 0),
120 	CHAN6G(41, 6155, 0),
121 	CHAN6G(45, 6175, 0),
122 	CHAN6G(49, 6195, 0),
123 	CHAN6G(53, 6215, 0),
124 	CHAN6G(57, 6235, 0),
125 	CHAN6G(61, 6255, 0),
126 	CHAN6G(65, 6275, 0),
127 	CHAN6G(69, 6295, 0),
128 	CHAN6G(73, 6315, 0),
129 	CHAN6G(77, 6335, 0),
130 	CHAN6G(81, 6355, 0),
131 	CHAN6G(85, 6375, 0),
132 	CHAN6G(89, 6395, 0),
133 	CHAN6G(93, 6415, 0),
134 	CHAN6G(97, 6435, 0),
135 	CHAN6G(101, 6455, 0),
136 	CHAN6G(105, 6475, 0),
137 	CHAN6G(109, 6495, 0),
138 	CHAN6G(113, 6515, 0),
139 	CHAN6G(117, 6535, 0),
140 	CHAN6G(121, 6555, 0),
141 	CHAN6G(125, 6575, 0),
142 	CHAN6G(129, 6595, 0),
143 	CHAN6G(133, 6615, 0),
144 	CHAN6G(137, 6635, 0),
145 	CHAN6G(141, 6655, 0),
146 	CHAN6G(145, 6675, 0),
147 	CHAN6G(149, 6695, 0),
148 	CHAN6G(153, 6715, 0),
149 	CHAN6G(157, 6735, 0),
150 	CHAN6G(161, 6755, 0),
151 	CHAN6G(165, 6775, 0),
152 	CHAN6G(169, 6795, 0),
153 	CHAN6G(173, 6815, 0),
154 	CHAN6G(177, 6835, 0),
155 	CHAN6G(181, 6855, 0),
156 	CHAN6G(185, 6875, 0),
157 	CHAN6G(189, 6895, 0),
158 	CHAN6G(193, 6915, 0),
159 	CHAN6G(197, 6935, 0),
160 	CHAN6G(201, 6955, 0),
161 	CHAN6G(205, 6975, 0),
162 	CHAN6G(209, 6995, 0),
163 	CHAN6G(213, 7015, 0),
164 	CHAN6G(217, 7035, 0),
165 	CHAN6G(221, 7055, 0),
166 	CHAN6G(225, 7075, 0),
167 	CHAN6G(229, 7095, 0),
168 	CHAN6G(233, 7115, 0),
169 };
170 
171 #define ATH12K_MAC_RATE_A_M(bps, code) \
172 	{ .bitrate = (bps), .hw_value = (code),\
173 	  .flags = IEEE80211_RATE_MANDATORY_A }
174 
175 #define ATH12K_MAC_RATE_B(bps, code, code_short) \
176 	{ .bitrate = (bps), .hw_value = (code), .hw_value_short = (code_short),\
177 	  .flags = IEEE80211_RATE_SHORT_PREAMBLE }
178 
179 static struct ieee80211_rate ath12k_legacy_rates[] = {
180 	{ .bitrate = 10,
181 	  .hw_value = ATH12K_HW_RATE_CCK_LP_1M },
182 	ATH12K_MAC_RATE_B(20, ATH12K_HW_RATE_CCK_LP_2M,
183 			  ATH12K_HW_RATE_CCK_SP_2M),
184 	ATH12K_MAC_RATE_B(55, ATH12K_HW_RATE_CCK_LP_5_5M,
185 			  ATH12K_HW_RATE_CCK_SP_5_5M),
186 	ATH12K_MAC_RATE_B(110, ATH12K_HW_RATE_CCK_LP_11M,
187 			  ATH12K_HW_RATE_CCK_SP_11M),
188 	ATH12K_MAC_RATE_A_M(60, ATH12K_HW_RATE_OFDM_6M),
189 	ATH12K_MAC_RATE_A_M(90, ATH12K_HW_RATE_OFDM_9M),
190 	ATH12K_MAC_RATE_A_M(120, ATH12K_HW_RATE_OFDM_12M),
191 	ATH12K_MAC_RATE_A_M(180, ATH12K_HW_RATE_OFDM_18M),
192 	ATH12K_MAC_RATE_A_M(240, ATH12K_HW_RATE_OFDM_24M),
193 	ATH12K_MAC_RATE_A_M(360, ATH12K_HW_RATE_OFDM_36M),
194 	ATH12K_MAC_RATE_A_M(480, ATH12K_HW_RATE_OFDM_48M),
195 	ATH12K_MAC_RATE_A_M(540, ATH12K_HW_RATE_OFDM_54M),
196 };
197 
198 static const int
199 ath12k_phymodes[NUM_NL80211_BANDS][ATH12K_CHAN_WIDTH_NUM] = {
200 	[NL80211_BAND_2GHZ] = {
201 			[NL80211_CHAN_WIDTH_5] = MODE_UNKNOWN,
202 			[NL80211_CHAN_WIDTH_10] = MODE_UNKNOWN,
203 			[NL80211_CHAN_WIDTH_20_NOHT] = MODE_11BE_EHT20_2G,
204 			[NL80211_CHAN_WIDTH_20] = MODE_11BE_EHT20_2G,
205 			[NL80211_CHAN_WIDTH_40] = MODE_11BE_EHT40_2G,
206 			[NL80211_CHAN_WIDTH_80] = MODE_UNKNOWN,
207 			[NL80211_CHAN_WIDTH_80P80] = MODE_UNKNOWN,
208 			[NL80211_CHAN_WIDTH_160] = MODE_UNKNOWN,
209 			[NL80211_CHAN_WIDTH_320] = MODE_UNKNOWN,
210 	},
211 	[NL80211_BAND_5GHZ] = {
212 			[NL80211_CHAN_WIDTH_5] = MODE_UNKNOWN,
213 			[NL80211_CHAN_WIDTH_10] = MODE_UNKNOWN,
214 			[NL80211_CHAN_WIDTH_20_NOHT] = MODE_11BE_EHT20,
215 			[NL80211_CHAN_WIDTH_20] = MODE_11BE_EHT20,
216 			[NL80211_CHAN_WIDTH_40] = MODE_11BE_EHT40,
217 			[NL80211_CHAN_WIDTH_80] = MODE_11BE_EHT80,
218 			[NL80211_CHAN_WIDTH_160] = MODE_11BE_EHT160,
219 			[NL80211_CHAN_WIDTH_80P80] = MODE_UNKNOWN,
220 			[NL80211_CHAN_WIDTH_320] = MODE_11BE_EHT320,
221 	},
222 	[NL80211_BAND_6GHZ] = {
223 			[NL80211_CHAN_WIDTH_5] = MODE_UNKNOWN,
224 			[NL80211_CHAN_WIDTH_10] = MODE_UNKNOWN,
225 			[NL80211_CHAN_WIDTH_20_NOHT] = MODE_11BE_EHT20,
226 			[NL80211_CHAN_WIDTH_20] = MODE_11BE_EHT20,
227 			[NL80211_CHAN_WIDTH_40] = MODE_11BE_EHT40,
228 			[NL80211_CHAN_WIDTH_80] = MODE_11BE_EHT80,
229 			[NL80211_CHAN_WIDTH_160] = MODE_11BE_EHT160,
230 			[NL80211_CHAN_WIDTH_80P80] = MODE_UNKNOWN,
231 			[NL80211_CHAN_WIDTH_320] = MODE_11BE_EHT320,
232 	},
233 
234 };
235 
236 const struct htt_rx_ring_tlv_filter ath12k_mac_mon_status_filter_default = {
237 	.rx_filter = HTT_RX_FILTER_TLV_FLAGS_MPDU_START |
238 		     HTT_RX_FILTER_TLV_FLAGS_PPDU_END |
239 		     HTT_RX_FILTER_TLV_FLAGS_PPDU_END_STATUS_DONE |
240 		     HTT_RX_FILTER_TLV_FLAGS_PPDU_START_USER_INFO,
241 	.pkt_filter_flags0 = HTT_RX_FP_MGMT_FILTER_FLAGS0,
242 	.pkt_filter_flags1 = HTT_RX_FP_MGMT_FILTER_FLAGS1,
243 	.pkt_filter_flags2 = HTT_RX_FP_CTRL_FILTER_FLASG2,
244 	.pkt_filter_flags3 = HTT_RX_FP_DATA_FILTER_FLASG3 |
245 			     HTT_RX_FP_CTRL_FILTER_FLASG3
246 };
247 
248 #define ATH12K_MAC_FIRST_OFDM_RATE_IDX 4
249 #define ath12k_g_rates ath12k_legacy_rates
250 #define ath12k_g_rates_size (ARRAY_SIZE(ath12k_legacy_rates))
251 #define ath12k_a_rates (ath12k_legacy_rates + 4)
252 #define ath12k_a_rates_size (ARRAY_SIZE(ath12k_legacy_rates) - 4)
253 
254 #define ATH12K_MAC_SCAN_TIMEOUT_MSECS 200 /* in msecs */
255 
256 static const u32 ath12k_smps_map[] = {
257 	[WLAN_HT_CAP_SM_PS_STATIC] = WMI_PEER_SMPS_STATIC,
258 	[WLAN_HT_CAP_SM_PS_DYNAMIC] = WMI_PEER_SMPS_DYNAMIC,
259 	[WLAN_HT_CAP_SM_PS_INVALID] = WMI_PEER_SMPS_PS_NONE,
260 	[WLAN_HT_CAP_SM_PS_DISABLED] = WMI_PEER_SMPS_PS_NONE,
261 };
262 
263 static int ath12k_start_vdev_delay(struct ath12k *ar,
264 				   struct ath12k_link_vif *arvif);
265 static void ath12k_mac_stop(struct ath12k *ar);
266 static int ath12k_mac_vdev_create(struct ath12k *ar, struct ath12k_link_vif *arvif);
267 static int ath12k_mac_vdev_delete(struct ath12k *ar, struct ath12k_link_vif *arvif);
268 
269 static const char *ath12k_mac_phymode_str(enum wmi_phy_mode mode)
270 {
271 	switch (mode) {
272 	case MODE_11A:
273 		return "11a";
274 	case MODE_11G:
275 		return "11g";
276 	case MODE_11B:
277 		return "11b";
278 	case MODE_11GONLY:
279 		return "11gonly";
280 	case MODE_11NA_HT20:
281 		return "11na-ht20";
282 	case MODE_11NG_HT20:
283 		return "11ng-ht20";
284 	case MODE_11NA_HT40:
285 		return "11na-ht40";
286 	case MODE_11NG_HT40:
287 		return "11ng-ht40";
288 	case MODE_11AC_VHT20:
289 		return "11ac-vht20";
290 	case MODE_11AC_VHT40:
291 		return "11ac-vht40";
292 	case MODE_11AC_VHT80:
293 		return "11ac-vht80";
294 	case MODE_11AC_VHT160:
295 		return "11ac-vht160";
296 	case MODE_11AC_VHT80_80:
297 		return "11ac-vht80+80";
298 	case MODE_11AC_VHT20_2G:
299 		return "11ac-vht20-2g";
300 	case MODE_11AC_VHT40_2G:
301 		return "11ac-vht40-2g";
302 	case MODE_11AC_VHT80_2G:
303 		return "11ac-vht80-2g";
304 	case MODE_11AX_HE20:
305 		return "11ax-he20";
306 	case MODE_11AX_HE40:
307 		return "11ax-he40";
308 	case MODE_11AX_HE80:
309 		return "11ax-he80";
310 	case MODE_11AX_HE80_80:
311 		return "11ax-he80+80";
312 	case MODE_11AX_HE160:
313 		return "11ax-he160";
314 	case MODE_11AX_HE20_2G:
315 		return "11ax-he20-2g";
316 	case MODE_11AX_HE40_2G:
317 		return "11ax-he40-2g";
318 	case MODE_11AX_HE80_2G:
319 		return "11ax-he80-2g";
320 	case MODE_11BE_EHT20:
321 		return "11be-eht20";
322 	case MODE_11BE_EHT40:
323 		return "11be-eht40";
324 	case MODE_11BE_EHT80:
325 		return "11be-eht80";
326 	case MODE_11BE_EHT80_80:
327 		return "11be-eht80+80";
328 	case MODE_11BE_EHT160:
329 		return "11be-eht160";
330 	case MODE_11BE_EHT160_160:
331 		return "11be-eht160+160";
332 	case MODE_11BE_EHT320:
333 		return "11be-eht320";
334 	case MODE_11BE_EHT20_2G:
335 		return "11be-eht20-2g";
336 	case MODE_11BE_EHT40_2G:
337 		return "11be-eht40-2g";
338 	case MODE_UNKNOWN:
339 		/* skip */
340 		break;
341 
342 		/* no default handler to allow compiler to check that the
343 		 * enum is fully handled
344 		 */
345 	}
346 
347 	return "<unknown>";
348 }
349 
350 u16 ath12k_mac_he_convert_tones_to_ru_tones(u16 tones)
351 {
352 	switch (tones) {
353 	case 26:
354 		return RU_26;
355 	case 52:
356 		return RU_52;
357 	case 106:
358 		return RU_106;
359 	case 242:
360 		return RU_242;
361 	case 484:
362 		return RU_484;
363 	case 996:
364 		return RU_996;
365 	case (996 * 2):
366 		return RU_2X996;
367 	default:
368 		return RU_26;
369 	}
370 }
371 EXPORT_SYMBOL(ath12k_mac_he_convert_tones_to_ru_tones);
372 
373 enum nl80211_eht_gi ath12k_mac_eht_gi_to_nl80211_eht_gi(u8 sgi)
374 {
375 	switch (sgi) {
376 	case RX_MSDU_START_SGI_0_8_US:
377 		return NL80211_RATE_INFO_EHT_GI_0_8;
378 	case RX_MSDU_START_SGI_1_6_US:
379 		return NL80211_RATE_INFO_EHT_GI_1_6;
380 	case RX_MSDU_START_SGI_3_2_US:
381 		return NL80211_RATE_INFO_EHT_GI_3_2;
382 	default:
383 		return NL80211_RATE_INFO_EHT_GI_0_8;
384 	}
385 }
386 EXPORT_SYMBOL(ath12k_mac_eht_gi_to_nl80211_eht_gi);
387 
388 enum nl80211_eht_ru_alloc ath12k_mac_eht_ru_tones_to_nl80211_eht_ru_alloc(u16 ru_tones)
389 {
390 	switch (ru_tones) {
391 	case 26:
392 		return NL80211_RATE_INFO_EHT_RU_ALLOC_26;
393 	case 52:
394 		return NL80211_RATE_INFO_EHT_RU_ALLOC_52;
395 	case (52 + 26):
396 		return NL80211_RATE_INFO_EHT_RU_ALLOC_52P26;
397 	case 106:
398 		return NL80211_RATE_INFO_EHT_RU_ALLOC_106;
399 	case (106 + 26):
400 		return NL80211_RATE_INFO_EHT_RU_ALLOC_106P26;
401 	case 242:
402 		return NL80211_RATE_INFO_EHT_RU_ALLOC_242;
403 	case 484:
404 		return NL80211_RATE_INFO_EHT_RU_ALLOC_484;
405 	case (484 + 242):
406 		return NL80211_RATE_INFO_EHT_RU_ALLOC_484P242;
407 	case 996:
408 		return NL80211_RATE_INFO_EHT_RU_ALLOC_996;
409 	case (996 + 484):
410 		return NL80211_RATE_INFO_EHT_RU_ALLOC_996P484;
411 	case (996 + 484 + 242):
412 		return NL80211_RATE_INFO_EHT_RU_ALLOC_996P484P242;
413 	case (2 * 996):
414 		return NL80211_RATE_INFO_EHT_RU_ALLOC_2x996;
415 	case (2 * 996 + 484):
416 		return NL80211_RATE_INFO_EHT_RU_ALLOC_2x996P484;
417 	case (3 * 996):
418 		return NL80211_RATE_INFO_EHT_RU_ALLOC_3x996;
419 	case (3 * 996 + 484):
420 		return NL80211_RATE_INFO_EHT_RU_ALLOC_3x996P484;
421 	case (4 * 996):
422 		return NL80211_RATE_INFO_EHT_RU_ALLOC_4x996;
423 	default:
424 		return NL80211_RATE_INFO_EHT_RU_ALLOC_26;
425 	}
426 }
427 EXPORT_SYMBOL(ath12k_mac_eht_ru_tones_to_nl80211_eht_ru_alloc);
428 
429 enum rate_info_bw
430 ath12k_mac_bw_to_mac80211_bw(enum ath12k_supported_bw bw)
431 {
432 	u8 ret = RATE_INFO_BW_20;
433 
434 	switch (bw) {
435 	case ATH12K_BW_20:
436 		ret = RATE_INFO_BW_20;
437 		break;
438 	case ATH12K_BW_40:
439 		ret = RATE_INFO_BW_40;
440 		break;
441 	case ATH12K_BW_80:
442 		ret = RATE_INFO_BW_80;
443 		break;
444 	case ATH12K_BW_160:
445 		ret = RATE_INFO_BW_160;
446 		break;
447 	case ATH12K_BW_320:
448 		ret = RATE_INFO_BW_320;
449 		break;
450 	}
451 
452 	return ret;
453 }
454 EXPORT_SYMBOL(ath12k_mac_bw_to_mac80211_bw);
455 
456 enum ath12k_supported_bw ath12k_mac_mac80211_bw_to_ath12k_bw(enum rate_info_bw bw)
457 {
458 	switch (bw) {
459 	case RATE_INFO_BW_20:
460 		return ATH12K_BW_20;
461 	case RATE_INFO_BW_40:
462 		return ATH12K_BW_40;
463 	case RATE_INFO_BW_80:
464 		return ATH12K_BW_80;
465 	case RATE_INFO_BW_160:
466 		return ATH12K_BW_160;
467 	case RATE_INFO_BW_320:
468 		return ATH12K_BW_320;
469 	default:
470 		return ATH12K_BW_20;
471 	}
472 }
473 
474 int ath12k_mac_hw_ratecode_to_legacy_rate(u8 hw_rc, u8 preamble, u8 *rateidx,
475 					  u16 *rate)
476 {
477 	/* As default, it is OFDM rates */
478 	int i = ATH12K_MAC_FIRST_OFDM_RATE_IDX;
479 	int max_rates_idx = ath12k_g_rates_size;
480 
481 	if (preamble == WMI_RATE_PREAMBLE_CCK) {
482 		hw_rc &= ~ATH12K_HW_RATECODE_CCK_SHORT_PREAM_MASK;
483 		i = 0;
484 		max_rates_idx = ATH12K_MAC_FIRST_OFDM_RATE_IDX;
485 	}
486 
487 	while (i < max_rates_idx) {
488 		if (hw_rc == ath12k_legacy_rates[i].hw_value) {
489 			*rateidx = i;
490 			*rate = ath12k_legacy_rates[i].bitrate;
491 			return 0;
492 		}
493 		i++;
494 	}
495 
496 	return -EINVAL;
497 }
498 EXPORT_SYMBOL(ath12k_mac_hw_ratecode_to_legacy_rate);
499 
500 u8 ath12k_mac_bitrate_to_idx(const struct ieee80211_supported_band *sband,
501 			     u32 bitrate)
502 {
503 	int i;
504 
505 	for (i = 0; i < sband->n_bitrates; i++)
506 		if (sband->bitrates[i].bitrate == bitrate)
507 			return i;
508 
509 	return 0;
510 }
511 
512 static u32
513 ath12k_mac_max_ht_nss(const u8 *ht_mcs_mask)
514 {
515 	int nss;
516 
517 	for (nss = IEEE80211_HT_MCS_MASK_LEN - 1; nss >= 0; nss--)
518 		if (ht_mcs_mask[nss])
519 			return nss + 1;
520 
521 	return 1;
522 }
523 
524 static u32
525 ath12k_mac_max_vht_nss(const u16 *vht_mcs_mask)
526 {
527 	int nss;
528 
529 	for (nss = NL80211_VHT_NSS_MAX - 1; nss >= 0; nss--)
530 		if (vht_mcs_mask[nss])
531 			return nss + 1;
532 
533 	return 1;
534 }
535 
536 static u32
537 ath12k_mac_max_he_nss(const u16 he_mcs_mask[NL80211_HE_NSS_MAX])
538 {
539 	int nss;
540 
541 	for (nss = NL80211_HE_NSS_MAX - 1; nss >= 0; nss--)
542 		if (he_mcs_mask[nss])
543 			return nss + 1;
544 
545 	return 1;
546 }
547 
548 static u32
549 ath12k_mac_max_eht_nss(const u16 eht_mcs_mask[NL80211_EHT_NSS_MAX])
550 {
551 	int nss;
552 
553 	for (nss = NL80211_EHT_NSS_MAX - 1; nss >= 0; nss--)
554 		if (eht_mcs_mask[nss])
555 			return nss + 1;
556 
557 	return 1;
558 }
559 
560 static u32
561 ath12k_mac_max_eht_mcs_nss(const u8 *eht_mcs, int eht_mcs_set_size)
562 {
563 	int i;
564 	u8 nss = 0;
565 
566 	for (i = 0; i < eht_mcs_set_size; i++)
567 		nss = max(nss, u8_get_bits(eht_mcs[i], IEEE80211_EHT_MCS_NSS_RX));
568 
569 	return nss;
570 }
571 
572 static u8 ath12k_parse_mpdudensity(u8 mpdudensity)
573 {
574 /*  From IEEE Std 802.11-2020 defined values for "Minimum MPDU Start Spacing":
575  *   0 for no restriction
576  *   1 for 1/4 us
577  *   2 for 1/2 us
578  *   3 for 1 us
579  *   4 for 2 us
580  *   5 for 4 us
581  *   6 for 8 us
582  *   7 for 16 us
583  */
584 	switch (mpdudensity) {
585 	case 0:
586 		return 0;
587 	case 1:
588 	case 2:
589 	case 3:
590 	/* Our lower layer calculations limit our precision to
591 	 * 1 microsecond
592 	 */
593 		return 1;
594 	case 4:
595 		return 2;
596 	case 5:
597 		return 4;
598 	case 6:
599 		return 8;
600 	case 7:
601 		return 16;
602 	default:
603 		return 0;
604 	}
605 }
606 
607 static int ath12k_mac_vif_link_chan(struct ieee80211_vif *vif, u8 link_id,
608 				    struct cfg80211_chan_def *def)
609 {
610 	struct ieee80211_bss_conf *link_conf;
611 	struct ieee80211_chanctx_conf *conf;
612 
613 	rcu_read_lock();
614 	link_conf = rcu_dereference(vif->link_conf[link_id]);
615 
616 	if (!link_conf) {
617 		rcu_read_unlock();
618 		return -ENOLINK;
619 	}
620 
621 	conf = rcu_dereference(link_conf->chanctx_conf);
622 	if (!conf) {
623 		rcu_read_unlock();
624 		return -ENOENT;
625 	}
626 	*def = conf->def;
627 	rcu_read_unlock();
628 
629 	return 0;
630 }
631 
632 static struct ath12k_link_vif *
633 ath12k_mac_get_tx_arvif(struct ath12k_link_vif *arvif,
634 			struct ieee80211_bss_conf *link_conf)
635 {
636 	struct ieee80211_bss_conf *tx_bss_conf;
637 	struct ath12k *ar = arvif->ar;
638 	struct ath12k_vif *tx_ahvif;
639 
640 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
641 
642 	tx_bss_conf = wiphy_dereference(ath12k_ar_to_hw(ar)->wiphy,
643 					link_conf->tx_bss_conf);
644 	if (tx_bss_conf) {
645 		tx_ahvif = ath12k_vif_to_ahvif(tx_bss_conf->vif);
646 		return wiphy_dereference(tx_ahvif->ah->hw->wiphy,
647 					 tx_ahvif->link[tx_bss_conf->link_id]);
648 	}
649 
650 	return NULL;
651 }
652 
653 static const u8 *ath12k_mac_get_tx_bssid(struct ath12k_link_vif *arvif)
654 {
655 	struct ieee80211_bss_conf *link_conf;
656 	struct ath12k_link_vif *tx_arvif;
657 	struct ath12k *ar = arvif->ar;
658 
659 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
660 
661 	link_conf = ath12k_mac_get_link_bss_conf(arvif);
662 	if (!link_conf) {
663 		ath12k_warn(ar->ab,
664 			    "unable to access bss link conf for link %u required to retrieve transmitting link conf\n",
665 			    arvif->link_id);
666 		return NULL;
667 	}
668 	if (link_conf->vif->type == NL80211_IFTYPE_STATION) {
669 		if (link_conf->nontransmitted)
670 			return link_conf->transmitter_bssid;
671 	} else {
672 		tx_arvif = ath12k_mac_get_tx_arvif(arvif, link_conf);
673 		if (tx_arvif)
674 			return tx_arvif->bssid;
675 	}
676 
677 	return NULL;
678 }
679 
680 struct ieee80211_bss_conf *
681 ath12k_mac_get_link_bss_conf(struct ath12k_link_vif *arvif)
682 {
683 	struct ieee80211_vif *vif = arvif->ahvif->vif;
684 	struct ieee80211_bss_conf *link_conf;
685 	struct ath12k *ar = arvif->ar;
686 
687 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
688 
689 	if (arvif->link_id >= IEEE80211_MLD_MAX_NUM_LINKS)
690 		return NULL;
691 
692 	link_conf = wiphy_dereference(ath12k_ar_to_hw(ar)->wiphy,
693 				      vif->link_conf[arvif->link_id]);
694 
695 	return link_conf;
696 }
697 
698 static struct ieee80211_link_sta *ath12k_mac_get_link_sta(struct ath12k_link_sta *arsta)
699 {
700 	struct ath12k_sta *ahsta = arsta->ahsta;
701 	struct ieee80211_sta *sta = ath12k_ahsta_to_sta(ahsta);
702 	struct ieee80211_link_sta *link_sta;
703 
704 	lockdep_assert_wiphy(ahsta->ahvif->ah->hw->wiphy);
705 
706 	if (arsta->link_id >= IEEE80211_MLD_MAX_NUM_LINKS)
707 		return NULL;
708 
709 	link_sta = wiphy_dereference(ahsta->ahvif->ah->hw->wiphy,
710 				     sta->link[arsta->link_id]);
711 
712 	return link_sta;
713 }
714 
715 static bool ath12k_mac_bitrate_is_cck(int bitrate)
716 {
717 	switch (bitrate) {
718 	case 10:
719 	case 20:
720 	case 55:
721 	case 110:
722 		return true;
723 	}
724 
725 	return false;
726 }
727 
728 u8 ath12k_mac_hw_rate_to_idx(const struct ieee80211_supported_band *sband,
729 			     u8 hw_rate, bool cck)
730 {
731 	const struct ieee80211_rate *rate;
732 	int i;
733 
734 	for (i = 0; i < sband->n_bitrates; i++) {
735 		rate = &sband->bitrates[i];
736 
737 		if (ath12k_mac_bitrate_is_cck(rate->bitrate) != cck)
738 			continue;
739 
740 		/* To handle 802.11a PPDU type */
741 		if ((!cck) && (rate->hw_value == hw_rate) &&
742 		    (rate->flags & IEEE80211_RATE_MANDATORY_A))
743 			return i;
744 		/* To handle 802.11b short PPDU type */
745 		else if (rate->flags & IEEE80211_RATE_SHORT_PREAMBLE &&
746 			 rate->hw_value_short == hw_rate)
747 			return i;
748 		/* To handle 802.11b long PPDU type */
749 		else if (rate->hw_value == hw_rate)
750 			return i;
751 	}
752 
753 	return 0;
754 }
755 
756 static u8 ath12k_mac_bitrate_to_rate(int bitrate)
757 {
758 	return DIV_ROUND_UP(bitrate, 5) |
759 	       (ath12k_mac_bitrate_is_cck(bitrate) ? BIT(7) : 0);
760 }
761 
762 static void ath12k_get_arvif_iter(void *data, u8 *mac,
763 				  struct ieee80211_vif *vif)
764 {
765 	struct ath12k_vif_iter *arvif_iter = data;
766 	struct ath12k_vif *ahvif = ath12k_vif_to_ahvif(vif);
767 	unsigned long links_map = ahvif->links_map;
768 	struct ath12k_link_vif *arvif;
769 	u8 link_id;
770 
771 	for_each_set_bit(link_id, &links_map, IEEE80211_MLD_MAX_NUM_LINKS) {
772 		arvif = rcu_dereference(ahvif->link[link_id]);
773 
774 		if (WARN_ON(!arvif))
775 			continue;
776 
777 		if (!arvif->is_created)
778 			continue;
779 
780 		if (arvif->vdev_id == arvif_iter->vdev_id &&
781 		    arvif->ar == arvif_iter->ar) {
782 			arvif_iter->arvif = arvif;
783 			break;
784 		}
785 	}
786 }
787 
788 struct ath12k_link_vif *ath12k_mac_get_arvif(struct ath12k *ar, u32 vdev_id)
789 {
790 	struct ath12k_vif_iter arvif_iter = {};
791 	u32 flags;
792 
793 	/* To use the arvif returned, caller must have held rcu read lock.
794 	 */
795 	lockdep_assert_in_rcu_read_lock();
796 	arvif_iter.vdev_id = vdev_id;
797 	arvif_iter.ar = ar;
798 
799 	flags = IEEE80211_IFACE_ITER_RESUME_ALL;
800 	ieee80211_iterate_active_interfaces_atomic(ath12k_ar_to_hw(ar),
801 						   flags,
802 						   ath12k_get_arvif_iter,
803 						   &arvif_iter);
804 	if (!arvif_iter.arvif) {
805 		ath12k_warn(ar->ab, "No VIF found for vdev %d\n", vdev_id);
806 		return NULL;
807 	}
808 
809 	return arvif_iter.arvif;
810 }
811 
812 struct ath12k_link_vif *ath12k_mac_get_arvif_by_vdev_id(struct ath12k_base *ab,
813 							u32 vdev_id)
814 {
815 	int i;
816 	struct ath12k_pdev *pdev;
817 	struct ath12k_link_vif *arvif;
818 
819 	for (i = 0; i < ab->num_radios; i++) {
820 		pdev = rcu_dereference(ab->pdevs_active[i]);
821 		if (pdev && pdev->ar &&
822 		    (pdev->ar->allocated_vdev_map & (1LL << vdev_id))) {
823 			arvif = ath12k_mac_get_arvif(pdev->ar, vdev_id);
824 			if (arvif)
825 				return arvif;
826 		}
827 	}
828 
829 	return NULL;
830 }
831 
832 struct ath12k *ath12k_mac_get_ar_by_vdev_id(struct ath12k_base *ab, u32 vdev_id)
833 {
834 	int i;
835 	struct ath12k_pdev *pdev;
836 
837 	for (i = 0; i < ab->num_radios; i++) {
838 		pdev = rcu_dereference(ab->pdevs_active[i]);
839 		if (pdev && pdev->ar) {
840 			if (pdev->ar->allocated_vdev_map & (1LL << vdev_id))
841 				return pdev->ar;
842 		}
843 	}
844 
845 	return NULL;
846 }
847 
848 struct ath12k *ath12k_mac_get_ar_by_pdev_id(struct ath12k_base *ab, u32 pdev_id)
849 {
850 	int i;
851 	struct ath12k_pdev *pdev;
852 
853 	if (ab->hw_params->single_pdev_only) {
854 		pdev = rcu_dereference(ab->pdevs_active[0]);
855 		return pdev ? pdev->ar : NULL;
856 	}
857 
858 	if (WARN_ON(pdev_id > ab->num_radios))
859 		return NULL;
860 
861 	for (i = 0; i < ab->num_radios; i++) {
862 		if (ab->fw_mode == ATH12K_FIRMWARE_MODE_FTM)
863 			pdev = &ab->pdevs[i];
864 		else
865 			pdev = rcu_dereference(ab->pdevs_active[i]);
866 
867 		if (pdev && pdev->pdev_id == pdev_id)
868 			return (pdev->ar ? pdev->ar : NULL);
869 	}
870 
871 	return NULL;
872 }
873 
874 static bool ath12k_mac_is_ml_arvif(struct ath12k_link_vif *arvif)
875 {
876 	struct ath12k_vif *ahvif = arvif->ahvif;
877 
878 	lockdep_assert_wiphy(ahvif->ah->hw->wiphy);
879 
880 	if (ahvif->vif->valid_links & BIT(arvif->link_id))
881 		return true;
882 
883 	return false;
884 }
885 
886 static struct ath12k *ath12k_mac_get_ar_by_chan(struct ieee80211_hw *hw,
887 						struct ieee80211_channel *channel)
888 {
889 	struct ath12k_hw *ah = hw->priv;
890 	struct ath12k *ar;
891 	int i;
892 
893 	ar = ah->radio;
894 
895 	if (ah->num_radio == 1)
896 		return ar;
897 
898 	for_each_ar(ah, ar, i) {
899 		if (channel->center_freq >= KHZ_TO_MHZ(ar->freq_range.start_freq) &&
900 		    channel->center_freq <= KHZ_TO_MHZ(ar->freq_range.end_freq))
901 			return ar;
902 	}
903 	return NULL;
904 }
905 
906 static struct ath12k *ath12k_get_ar_by_ctx(struct ieee80211_hw *hw,
907 					   struct ieee80211_chanctx_conf *ctx)
908 {
909 	if (!ctx)
910 		return NULL;
911 
912 	return ath12k_mac_get_ar_by_chan(hw, ctx->def.chan);
913 }
914 
915 struct ath12k *ath12k_get_ar_by_vif(struct ieee80211_hw *hw,
916 				    struct ieee80211_vif *vif,
917 				    u8 link_id)
918 {
919 	struct ath12k_vif *ahvif = ath12k_vif_to_ahvif(vif);
920 	struct ath12k_hw *ah = ath12k_hw_to_ah(hw);
921 	struct ath12k_link_vif *arvif;
922 
923 	lockdep_assert_wiphy(hw->wiphy);
924 
925 	/* If there is one pdev within ah, then we return
926 	 * ar directly.
927 	 */
928 	if (ah->num_radio == 1)
929 		return ah->radio;
930 
931 	if (!(ahvif->links_map & BIT(link_id)))
932 		return NULL;
933 
934 	arvif = wiphy_dereference(hw->wiphy, ahvif->link[link_id]);
935 	if (arvif && arvif->is_created)
936 		return arvif->ar;
937 
938 	return NULL;
939 }
940 
941 void ath12k_mac_get_any_chanctx_conf_iter(struct ieee80211_hw *hw,
942 					  struct ieee80211_chanctx_conf *conf,
943 					  void *data)
944 {
945 	struct ath12k_mac_get_any_chanctx_conf_arg *arg = data;
946 	struct ath12k *ctx_ar = ath12k_get_ar_by_ctx(hw, conf);
947 
948 	if (ctx_ar == arg->ar)
949 		arg->chanctx_conf = conf;
950 }
951 
952 static struct ath12k_link_vif *ath12k_mac_get_vif_up(struct ath12k *ar)
953 {
954 	struct ath12k_link_vif *arvif;
955 
956 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
957 
958 	list_for_each_entry(arvif, &ar->arvifs, list) {
959 		if (arvif->is_up)
960 			return arvif;
961 	}
962 
963 	return NULL;
964 }
965 
966 static bool ath12k_mac_band_match(enum nl80211_band band1, enum WMI_HOST_WLAN_BAND band2)
967 {
968 	switch (band1) {
969 	case NL80211_BAND_2GHZ:
970 		if (band2 & WMI_HOST_WLAN_2GHZ_CAP)
971 			return true;
972 		break;
973 	case NL80211_BAND_5GHZ:
974 	case NL80211_BAND_6GHZ:
975 		if (band2 & WMI_HOST_WLAN_5GHZ_CAP)
976 			return true;
977 		break;
978 	default:
979 		return false;
980 	}
981 
982 	return false;
983 }
984 
985 static u8 ath12k_mac_get_target_pdev_id_from_vif(struct ath12k_link_vif *arvif)
986 {
987 	struct ath12k *ar = arvif->ar;
988 	struct ath12k_base *ab = ar->ab;
989 	struct ieee80211_vif *vif = arvif->ahvif->vif;
990 	struct cfg80211_chan_def def;
991 	enum nl80211_band band;
992 	u8 pdev_id = ab->fw_pdev[0].pdev_id;
993 	int i;
994 
995 	if (WARN_ON(ath12k_mac_vif_link_chan(vif, arvif->link_id, &def)))
996 		return pdev_id;
997 
998 	band = def.chan->band;
999 
1000 	for (i = 0; i < ab->fw_pdev_count; i++) {
1001 		if (ath12k_mac_band_match(band, ab->fw_pdev[i].supported_bands))
1002 			return ab->fw_pdev[i].pdev_id;
1003 	}
1004 
1005 	return pdev_id;
1006 }
1007 
1008 u8 ath12k_mac_get_target_pdev_id(struct ath12k *ar)
1009 {
1010 	struct ath12k_link_vif *arvif;
1011 	struct ath12k_base *ab = ar->ab;
1012 
1013 	if (!ab->hw_params->single_pdev_only)
1014 		return ar->pdev->pdev_id;
1015 
1016 	arvif = ath12k_mac_get_vif_up(ar);
1017 
1018 	/* fw_pdev array has pdev ids derived from phy capability
1019 	 * service ready event (pdev_and_hw_link_ids).
1020 	 * If no vif is active, return default first index.
1021 	 */
1022 	if (!arvif)
1023 		return ar->ab->fw_pdev[0].pdev_id;
1024 
1025 	/* If active vif is found, return the pdev id matching chandef band */
1026 	return ath12k_mac_get_target_pdev_id_from_vif(arvif);
1027 }
1028 
1029 static void ath12k_pdev_caps_update(struct ath12k *ar)
1030 {
1031 	struct ath12k_base *ab = ar->ab;
1032 
1033 	ar->max_tx_power = ab->target_caps.hw_max_tx_power;
1034 
1035 	/* FIXME: Set min_tx_power to ab->target_caps.hw_min_tx_power.
1036 	 * But since the received value in svcrdy is same as hw_max_tx_power,
1037 	 * we can set ar->min_tx_power to 0 currently until
1038 	 * this is fixed in firmware
1039 	 */
1040 	ar->min_tx_power = 0;
1041 
1042 	ar->txpower_limit_2g = ar->max_tx_power;
1043 	ar->txpower_limit_5g = ar->max_tx_power;
1044 	ar->txpower_scale = WMI_HOST_TP_SCALE_MAX;
1045 }
1046 
1047 static int ath12k_mac_txpower_recalc(struct ath12k *ar)
1048 {
1049 	struct ath12k_pdev *pdev = ar->pdev;
1050 	struct ath12k_link_vif *arvif;
1051 	int ret, txpower = -1;
1052 	u32 param;
1053 
1054 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
1055 
1056 	list_for_each_entry(arvif, &ar->arvifs, list) {
1057 		if (arvif->txpower <= 0)
1058 			continue;
1059 
1060 		if (txpower == -1)
1061 			txpower = arvif->txpower;
1062 		else
1063 			txpower = min(txpower, arvif->txpower);
1064 	}
1065 
1066 	if (txpower == -1)
1067 		return 0;
1068 
1069 	/* txpwr is set as 2 units per dBm in FW*/
1070 	txpower = min_t(u32, max_t(u32, ar->min_tx_power, txpower),
1071 			ar->max_tx_power) * 2;
1072 
1073 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "txpower to set in hw %d\n",
1074 		   txpower / 2);
1075 
1076 	if ((pdev->cap.supported_bands & WMI_HOST_WLAN_2GHZ_CAP) &&
1077 	    ar->txpower_limit_2g != txpower) {
1078 		param = WMI_PDEV_PARAM_TXPOWER_LIMIT2G;
1079 		ret = ath12k_wmi_pdev_set_param(ar, param,
1080 						txpower, ar->pdev->pdev_id);
1081 		if (ret)
1082 			goto fail;
1083 		ar->txpower_limit_2g = txpower;
1084 	}
1085 
1086 	if ((pdev->cap.supported_bands & WMI_HOST_WLAN_5GHZ_CAP) &&
1087 	    ar->txpower_limit_5g != txpower) {
1088 		param = WMI_PDEV_PARAM_TXPOWER_LIMIT5G;
1089 		ret = ath12k_wmi_pdev_set_param(ar, param,
1090 						txpower, ar->pdev->pdev_id);
1091 		if (ret)
1092 			goto fail;
1093 		ar->txpower_limit_5g = txpower;
1094 	}
1095 
1096 	return 0;
1097 
1098 fail:
1099 	ath12k_warn(ar->ab, "failed to recalc txpower limit %d using pdev param %d: %d\n",
1100 		    txpower / 2, param, ret);
1101 	return ret;
1102 }
1103 
1104 static int ath12k_recalc_rtscts_prot(struct ath12k_link_vif *arvif)
1105 {
1106 	struct ath12k *ar = arvif->ar;
1107 	u32 vdev_param, rts_cts;
1108 	int ret;
1109 
1110 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
1111 
1112 	vdev_param = WMI_VDEV_PARAM_ENABLE_RTSCTS;
1113 
1114 	/* Enable RTS/CTS protection for sw retries (when legacy stations
1115 	 * are in BSS) or by default only for second rate series.
1116 	 * TODO: Check if we need to enable CTS 2 Self in any case
1117 	 */
1118 	rts_cts = WMI_USE_RTS_CTS;
1119 
1120 	if (arvif->num_legacy_stations > 0)
1121 		rts_cts |= WMI_RTSCTS_ACROSS_SW_RETRIES << 4;
1122 	else
1123 		rts_cts |= WMI_RTSCTS_FOR_SECOND_RATESERIES << 4;
1124 
1125 	/* Need not send duplicate param value to firmware */
1126 	if (arvif->rtscts_prot_mode == rts_cts)
1127 		return 0;
1128 
1129 	arvif->rtscts_prot_mode = rts_cts;
1130 
1131 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac vdev %d recalc rts/cts prot %d\n",
1132 		   arvif->vdev_id, rts_cts);
1133 
1134 	ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
1135 					    vdev_param, rts_cts);
1136 	if (ret)
1137 		ath12k_warn(ar->ab, "failed to recalculate rts/cts prot for vdev %d: %d\n",
1138 			    arvif->vdev_id, ret);
1139 
1140 	return ret;
1141 }
1142 
1143 static int ath12k_mac_set_kickout(struct ath12k_link_vif *arvif)
1144 {
1145 	struct ath12k *ar = arvif->ar;
1146 	u32 param;
1147 	int ret;
1148 
1149 	ret = ath12k_wmi_pdev_set_param(ar, WMI_PDEV_PARAM_STA_KICKOUT_TH,
1150 					ATH12K_KICKOUT_THRESHOLD,
1151 					ar->pdev->pdev_id);
1152 	if (ret) {
1153 		ath12k_warn(ar->ab, "failed to set kickout threshold on vdev %i: %d\n",
1154 			    arvif->vdev_id, ret);
1155 		return ret;
1156 	}
1157 
1158 	param = WMI_VDEV_PARAM_AP_KEEPALIVE_MIN_IDLE_INACTIVE_TIME_SECS;
1159 	ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id, param,
1160 					    ATH12K_KEEPALIVE_MIN_IDLE);
1161 	if (ret) {
1162 		ath12k_warn(ar->ab, "failed to set keepalive minimum idle time on vdev %i: %d\n",
1163 			    arvif->vdev_id, ret);
1164 		return ret;
1165 	}
1166 
1167 	param = WMI_VDEV_PARAM_AP_KEEPALIVE_MAX_IDLE_INACTIVE_TIME_SECS;
1168 	ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id, param,
1169 					    ATH12K_KEEPALIVE_MAX_IDLE);
1170 	if (ret) {
1171 		ath12k_warn(ar->ab, "failed to set keepalive maximum idle time on vdev %i: %d\n",
1172 			    arvif->vdev_id, ret);
1173 		return ret;
1174 	}
1175 
1176 	param = WMI_VDEV_PARAM_AP_KEEPALIVE_MAX_UNRESPONSIVE_TIME_SECS;
1177 	ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id, param,
1178 					    ATH12K_KEEPALIVE_MAX_UNRESPONSIVE);
1179 	if (ret) {
1180 		ath12k_warn(ar->ab, "failed to set keepalive maximum unresponsive time on vdev %i: %d\n",
1181 			    arvif->vdev_id, ret);
1182 		return ret;
1183 	}
1184 
1185 	return 0;
1186 }
1187 
1188 static void ath12k_mac_link_sta_rhash_cleanup(void *data, struct ieee80211_sta *sta)
1189 {
1190 	u8 link_id;
1191 	unsigned long links_map;
1192 	struct ath12k_sta *ahsta;
1193 	struct ath12k *ar = data;
1194 	struct ath12k_link_sta *arsta;
1195 	struct ath12k_link_vif *arvif;
1196 	struct ath12k_base *ab = ar->ab;
1197 
1198 	ahsta = ath12k_sta_to_ahsta(sta);
1199 	links_map = ahsta->links_map;
1200 
1201 	rcu_read_lock();
1202 	for_each_set_bit(link_id, &links_map, IEEE80211_MLD_MAX_NUM_LINKS) {
1203 		arsta = rcu_dereference(ahsta->link[link_id]);
1204 		if (!arsta)
1205 			continue;
1206 		arvif = arsta->arvif;
1207 		if (!(arvif->ar == ar))
1208 			continue;
1209 
1210 		spin_lock_bh(&ab->base_lock);
1211 		ath12k_link_sta_rhash_delete(ab, arsta);
1212 		spin_unlock_bh(&ab->base_lock);
1213 	}
1214 	rcu_read_unlock();
1215 }
1216 
1217 void ath12k_mac_peer_cleanup_all(struct ath12k *ar)
1218 {
1219 	struct ath12k_dp_link_peer *peer, *tmp;
1220 	struct ath12k_base *ab = ar->ab;
1221 	struct ath12k_dp *dp = ath12k_ab_to_dp(ab);
1222 	struct ath12k_link_vif *arvif, *tmp_vif;
1223 	struct ath12k_dp_hw *dp_hw = &ar->ah->dp_hw;
1224 	struct ath12k_dp_peer *dp_peer = NULL;
1225 	u16 peerid_index;
1226 	struct list_head peers;
1227 
1228 	INIT_LIST_HEAD(&peers);
1229 
1230 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
1231 
1232 	spin_lock_bh(&dp->dp_lock);
1233 	list_for_each_entry_safe(peer, tmp, &dp->peers, list) {
1234 		/* Skip Rx TID cleanup for self peer */
1235 		if (peer->sta && peer->dp_peer)
1236 			ath12k_dp_rx_peer_tid_cleanup(ar, peer);
1237 
1238 		/* cleanup dp peer */
1239 		spin_lock_bh(&dp_hw->peer_lock);
1240 		dp_peer = peer->dp_peer;
1241 		peerid_index = ath12k_dp_peer_get_peerid_index(dp, peer->peer_id);
1242 		rcu_assign_pointer(dp_peer->link_peers[peer->link_id], NULL);
1243 		rcu_assign_pointer(dp_hw->dp_peers[peerid_index], NULL);
1244 		spin_unlock_bh(&dp_hw->peer_lock);
1245 
1246 		ath12k_dp_link_peer_rhash_delete(dp, peer);
1247 
1248 		list_move(&peer->list, &peers);
1249 	}
1250 	spin_unlock_bh(&dp->dp_lock);
1251 
1252 	synchronize_rcu();
1253 
1254 	list_for_each_entry_safe(peer, tmp, &peers, list) {
1255 		ath12k_dp_link_peer_free(peer);
1256 	}
1257 
1258 	ar->num_peers = 0;
1259 	ar->num_stations = 0;
1260 
1261 	/* Cleanup rhash table maintained for arsta by iterating over sta */
1262 	ieee80211_iterate_stations_mtx(ar->ah->hw, ath12k_mac_link_sta_rhash_cleanup,
1263 				       ar);
1264 
1265 	/* Delete all the self dp_peers on asserted radio */
1266 	list_for_each_entry_safe_reverse(arvif, tmp_vif, &ar->arvifs, list) {
1267 		if ((arvif->ahvif->vdev_type == WMI_VDEV_TYPE_AP) &&
1268 		    (arvif->link_id < IEEE80211_MLD_MAX_NUM_LINKS)) {
1269 			ath12k_dp_peer_delete(dp_hw, arvif->bssid, NULL);
1270 			arvif->num_stations = 0;
1271 		}
1272 	}
1273 }
1274 
1275 void ath12k_mac_dp_peer_cleanup(struct ath12k_hw *ah)
1276 {
1277 	struct list_head peers;
1278 	struct ath12k_dp_peer *dp_peer, *tmp;
1279 	struct ath12k_dp_hw *dp_hw = &ah->dp_hw;
1280 
1281 	INIT_LIST_HEAD(&peers);
1282 
1283 	spin_lock_bh(&dp_hw->peer_lock);
1284 	list_for_each_entry_safe(dp_peer, tmp, &dp_hw->dp_peers_list, list) {
1285 		if (dp_peer->is_mlo) {
1286 			rcu_assign_pointer(dp_hw->dp_peers[dp_peer->peer_id], NULL);
1287 			clear_bit(dp_peer->peer_id, ah->free_ml_peer_id_map);
1288 		}
1289 
1290 		list_move(&dp_peer->list, &peers);
1291 	}
1292 
1293 	spin_unlock_bh(&dp_hw->peer_lock);
1294 
1295 	synchronize_rcu();
1296 
1297 	list_for_each_entry_safe(dp_peer, tmp, &peers, list) {
1298 		list_del(&dp_peer->list);
1299 		kfree(dp_peer);
1300 	}
1301 }
1302 
1303 static int ath12k_mac_vdev_setup_sync(struct ath12k *ar)
1304 {
1305 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
1306 
1307 	if (test_bit(ATH12K_FLAG_CRASH_FLUSH, &ar->ab->dev_flags))
1308 		return -ESHUTDOWN;
1309 
1310 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "vdev setup timeout %d\n",
1311 		   ATH12K_VDEV_SETUP_TIMEOUT_HZ);
1312 
1313 	if (!wait_for_completion_timeout(&ar->vdev_setup_done,
1314 					 ATH12K_VDEV_SETUP_TIMEOUT_HZ))
1315 		return -ETIMEDOUT;
1316 
1317 	return ar->last_wmi_vdev_start_status ? -EINVAL : 0;
1318 }
1319 
1320 static int ath12k_monitor_vdev_up(struct ath12k *ar, int vdev_id)
1321 {
1322 	struct ath12k_wmi_vdev_up_params params = {};
1323 	int ret;
1324 
1325 	params.vdev_id = vdev_id;
1326 	params.bssid = ar->mac_addr;
1327 	ret = ath12k_wmi_vdev_up(ar, &params);
1328 	if (ret) {
1329 		ath12k_warn(ar->ab, "failed to put up monitor vdev %i: %d\n",
1330 			    vdev_id, ret);
1331 		return ret;
1332 	}
1333 
1334 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac monitor vdev %i started\n",
1335 		   vdev_id);
1336 	return 0;
1337 }
1338 
1339 static int ath12k_mac_monitor_vdev_start(struct ath12k *ar, int vdev_id,
1340 					 struct cfg80211_chan_def *chandef)
1341 {
1342 	struct ieee80211_channel *channel;
1343 	struct wmi_vdev_start_req_arg arg = {};
1344 	struct ath12k_wmi_vdev_up_params params = {};
1345 	int ret;
1346 
1347 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
1348 
1349 	channel = chandef->chan;
1350 	arg.vdev_id = vdev_id;
1351 	arg.freq = channel->center_freq;
1352 	arg.band_center_freq1 = chandef->center_freq1;
1353 	arg.band_center_freq2 = chandef->center_freq2;
1354 	arg.mode = ath12k_phymodes[chandef->chan->band][chandef->width];
1355 	arg.chan_radar = !!(channel->flags & IEEE80211_CHAN_RADAR);
1356 
1357 	arg.min_power = 0;
1358 	arg.max_power = channel->max_power;
1359 	arg.max_reg_power = channel->max_reg_power;
1360 	arg.max_antenna_gain = channel->max_antenna_gain;
1361 
1362 	arg.pref_tx_streams = ar->num_tx_chains;
1363 	arg.pref_rx_streams = ar->num_rx_chains;
1364 	arg.punct_bitmap = 0xFFFFFFFF;
1365 
1366 	arg.passive |= !!(chandef->chan->flags & IEEE80211_CHAN_NO_IR);
1367 
1368 	reinit_completion(&ar->vdev_setup_done);
1369 	reinit_completion(&ar->vdev_delete_done);
1370 
1371 	ret = ath12k_wmi_vdev_start(ar, &arg, false);
1372 	if (ret) {
1373 		ath12k_warn(ar->ab, "failed to request monitor vdev %i start: %d\n",
1374 			    vdev_id, ret);
1375 		return ret;
1376 	}
1377 
1378 	ret = ath12k_mac_vdev_setup_sync(ar);
1379 	if (ret) {
1380 		ath12k_warn(ar->ab, "failed to synchronize setup for monitor vdev %i start: %d\n",
1381 			    vdev_id, ret);
1382 		return ret;
1383 	}
1384 
1385 	params.vdev_id = vdev_id;
1386 	params.bssid = ar->mac_addr;
1387 	ret = ath12k_wmi_vdev_up(ar, &params);
1388 	if (ret) {
1389 		ath12k_warn(ar->ab, "failed to put up monitor vdev %i: %d\n",
1390 			    vdev_id, ret);
1391 		goto vdev_stop;
1392 	}
1393 
1394 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac monitor vdev %i started\n",
1395 		   vdev_id);
1396 	return 0;
1397 
1398 vdev_stop:
1399 	ret = ath12k_wmi_vdev_stop(ar, vdev_id);
1400 	if (ret)
1401 		ath12k_warn(ar->ab, "failed to stop monitor vdev %i after start failure: %d\n",
1402 			    vdev_id, ret);
1403 	return ret;
1404 }
1405 
1406 static int ath12k_mac_monitor_vdev_stop(struct ath12k *ar)
1407 {
1408 	int ret;
1409 
1410 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
1411 
1412 	reinit_completion(&ar->vdev_setup_done);
1413 
1414 	ret = ath12k_wmi_vdev_stop(ar, ar->monitor_vdev_id);
1415 	if (ret)
1416 		ath12k_warn(ar->ab, "failed to request monitor vdev %i stop: %d\n",
1417 			    ar->monitor_vdev_id, ret);
1418 
1419 	ret = ath12k_mac_vdev_setup_sync(ar);
1420 	if (ret)
1421 		ath12k_warn(ar->ab, "failed to synchronize monitor vdev %i stop: %d\n",
1422 			    ar->monitor_vdev_id, ret);
1423 
1424 	ret = ath12k_wmi_vdev_down(ar, ar->monitor_vdev_id);
1425 	if (ret)
1426 		ath12k_warn(ar->ab, "failed to put down monitor vdev %i: %d\n",
1427 			    ar->monitor_vdev_id, ret);
1428 
1429 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac monitor vdev %i stopped\n",
1430 		   ar->monitor_vdev_id);
1431 	return ret;
1432 }
1433 
1434 static int ath12k_mac_monitor_vdev_delete(struct ath12k *ar)
1435 {
1436 	int ret;
1437 	unsigned long time_left;
1438 
1439 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
1440 
1441 	if (!ar->monitor_vdev_created)
1442 		return 0;
1443 
1444 	reinit_completion(&ar->vdev_delete_done);
1445 
1446 	ret = ath12k_wmi_vdev_delete(ar, ar->monitor_vdev_id);
1447 	if (ret) {
1448 		ath12k_warn(ar->ab, "failed to request wmi monitor vdev %i removal: %d\n",
1449 			    ar->monitor_vdev_id, ret);
1450 		return ret;
1451 	}
1452 
1453 	time_left = wait_for_completion_timeout(&ar->vdev_delete_done,
1454 						ATH12K_VDEV_DELETE_TIMEOUT_HZ);
1455 	if (time_left == 0) {
1456 		ath12k_warn(ar->ab, "Timeout in receiving vdev delete response\n");
1457 	} else {
1458 		ar->allocated_vdev_map &= ~(1LL << ar->monitor_vdev_id);
1459 		ar->ab->free_vdev_map |= 1LL << (ar->monitor_vdev_id);
1460 		ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac monitor vdev %d deleted\n",
1461 			   ar->monitor_vdev_id);
1462 		ar->num_created_vdevs--;
1463 		ar->monitor_vdev_id = -1;
1464 		ar->monitor_vdev_created = false;
1465 	}
1466 
1467 	return ret;
1468 }
1469 
1470 static int ath12k_mac_monitor_start(struct ath12k *ar)
1471 {
1472 	struct ath12k_mac_get_any_chanctx_conf_arg arg;
1473 	int ret;
1474 
1475 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
1476 
1477 	if (ar->monitor_started)
1478 		return 0;
1479 
1480 	arg.ar = ar;
1481 	arg.chanctx_conf = NULL;
1482 	ieee80211_iter_chan_contexts_atomic(ath12k_ar_to_hw(ar),
1483 					    ath12k_mac_get_any_chanctx_conf_iter,
1484 					    &arg);
1485 	if (!arg.chanctx_conf)
1486 		return 0;
1487 
1488 	ret = ath12k_mac_monitor_vdev_start(ar, ar->monitor_vdev_id,
1489 					    &arg.chanctx_conf->def);
1490 	if (ret) {
1491 		ath12k_warn(ar->ab, "failed to start monitor vdev: %d\n", ret);
1492 		return ret;
1493 	}
1494 
1495 	ret = ath12k_dp_tx_htt_monitor_mode_ring_config(ar, false);
1496 	if (ret) {
1497 		ath12k_warn(ar->ab, "fail to set monitor filter: %d\n", ret);
1498 		return ret;
1499 	}
1500 
1501 	ar->monitor_started = true;
1502 	ar->num_started_vdevs++;
1503 
1504 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac monitor started\n");
1505 
1506 	return 0;
1507 }
1508 
1509 static int ath12k_mac_monitor_stop(struct ath12k *ar)
1510 {
1511 	int ret;
1512 
1513 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
1514 
1515 	if (!ar->monitor_started)
1516 		return 0;
1517 
1518 	ret = ath12k_mac_monitor_vdev_stop(ar);
1519 	if (ret) {
1520 		ath12k_warn(ar->ab, "failed to stop monitor vdev: %d\n", ret);
1521 		return ret;
1522 	}
1523 
1524 	ar->monitor_started = false;
1525 	ar->num_started_vdevs--;
1526 	ret = ath12k_dp_tx_htt_monitor_mode_ring_config(ar, true);
1527 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac monitor stopped ret %d\n", ret);
1528 	return ret;
1529 }
1530 
1531 int ath12k_mac_vdev_stop(struct ath12k_link_vif *arvif)
1532 {
1533 	struct ath12k_vif *ahvif = arvif->ahvif;
1534 	struct ath12k *ar = arvif->ar;
1535 	int ret;
1536 
1537 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
1538 
1539 	reinit_completion(&ar->vdev_setup_done);
1540 
1541 	ret = ath12k_wmi_vdev_stop(ar, arvif->vdev_id);
1542 	if (ret) {
1543 		ath12k_warn(ar->ab, "failed to stop WMI vdev %i: %d\n",
1544 			    arvif->vdev_id, ret);
1545 		goto err;
1546 	}
1547 
1548 	ret = ath12k_mac_vdev_setup_sync(ar);
1549 	if (ret) {
1550 		ath12k_warn(ar->ab, "failed to synchronize setup for vdev %i: %d\n",
1551 			    arvif->vdev_id, ret);
1552 		goto err;
1553 	}
1554 
1555 	WARN_ON(ar->num_started_vdevs == 0);
1556 
1557 	ar->num_started_vdevs--;
1558 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "vdev %pM stopped, vdev_id %d\n",
1559 		   ahvif->vif->addr, arvif->vdev_id);
1560 
1561 	if (test_bit(ATH12K_FLAG_CAC_RUNNING, &ar->dev_flags)) {
1562 		clear_bit(ATH12K_FLAG_CAC_RUNNING, &ar->dev_flags);
1563 		ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "CAC Stopped for vdev %d\n",
1564 			   arvif->vdev_id);
1565 	}
1566 
1567 	return 0;
1568 err:
1569 	return ret;
1570 }
1571 
1572 int ath12k_mac_op_config(struct ieee80211_hw *hw, int radio_idx, u32 changed)
1573 {
1574 	return 0;
1575 }
1576 EXPORT_SYMBOL(ath12k_mac_op_config);
1577 
1578 static int ath12k_mac_setup_bcn_p2p_ie(struct ath12k_link_vif *arvif,
1579 				       struct sk_buff *bcn)
1580 {
1581 	struct ath12k *ar = arvif->ar;
1582 	struct ieee80211_mgmt *mgmt;
1583 	const u8 *p2p_ie;
1584 	int ret;
1585 
1586 	mgmt = (void *)bcn->data;
1587 	p2p_ie = cfg80211_find_vendor_ie(WLAN_OUI_WFA, WLAN_OUI_TYPE_WFA_P2P,
1588 					 mgmt->u.beacon.variable,
1589 					 bcn->len - (mgmt->u.beacon.variable -
1590 						     bcn->data));
1591 	if (!p2p_ie) {
1592 		ath12k_warn(ar->ab, "no P2P ie found in beacon\n");
1593 		return -ENOENT;
1594 	}
1595 
1596 	ret = ath12k_wmi_p2p_go_bcn_ie(ar, arvif->vdev_id, p2p_ie);
1597 	if (ret) {
1598 		ath12k_warn(ar->ab, "failed to submit P2P GO bcn ie for vdev %i: %d\n",
1599 			    arvif->vdev_id, ret);
1600 		return ret;
1601 	}
1602 
1603 	return 0;
1604 }
1605 
1606 static int ath12k_mac_remove_vendor_ie(struct sk_buff *skb, unsigned int oui,
1607 				       u8 oui_type, size_t ie_offset)
1608 {
1609 	const u8 *next, *end;
1610 	size_t len;
1611 	u8 *ie;
1612 
1613 	if (WARN_ON(skb->len < ie_offset))
1614 		return -EINVAL;
1615 
1616 	ie = (u8 *)cfg80211_find_vendor_ie(oui, oui_type,
1617 					   skb->data + ie_offset,
1618 					   skb->len - ie_offset);
1619 	if (!ie)
1620 		return -ENOENT;
1621 
1622 	len = ie[1] + 2;
1623 	end = skb->data + skb->len;
1624 	next = ie + len;
1625 
1626 	if (WARN_ON(next > end))
1627 		return -EINVAL;
1628 
1629 	memmove(ie, next, end - next);
1630 	skb_trim(skb, skb->len - len);
1631 
1632 	return 0;
1633 }
1634 
1635 static void ath12k_mac_set_arvif_ies(struct ath12k_link_vif *arvif,
1636 				     struct ath12k_link_vif *tx_arvif,
1637 				     struct sk_buff *bcn,
1638 				     u8 bssid_index, bool *nontx_profile_found)
1639 {
1640 	struct ieee80211_mgmt *mgmt = (struct ieee80211_mgmt *)bcn->data;
1641 	const struct element *elem, *nontx, *index, *nie, *ext_cap_ie;
1642 	const u8 *start, *tail;
1643 	u16 rem_len;
1644 	u8 i;
1645 
1646 	start = bcn->data + ieee80211_get_hdrlen_from_skb(bcn) + sizeof(mgmt->u.beacon);
1647 	tail = skb_tail_pointer(bcn);
1648 	rem_len = tail - start;
1649 
1650 	arvif->rsnie_present = false;
1651 	arvif->wpaie_present = false;
1652 
1653 	if (cfg80211_find_ie(WLAN_EID_RSN, start, rem_len))
1654 		arvif->rsnie_present = true;
1655 	if (cfg80211_find_vendor_ie(WLAN_OUI_MICROSOFT, WLAN_OUI_TYPE_MICROSOFT_WPA,
1656 				    start, rem_len))
1657 		arvif->wpaie_present = true;
1658 
1659 	ext_cap_ie = cfg80211_find_elem(WLAN_EID_EXT_CAPABILITY, start, rem_len);
1660 	if (ext_cap_ie && ext_cap_ie->datalen >= 11 &&
1661 	    (ext_cap_ie->data[10] & WLAN_EXT_CAPA11_BCN_PROTECT))
1662 		tx_arvif->beacon_prot = true;
1663 
1664 	/* Return from here for the transmitted profile */
1665 	if (!bssid_index)
1666 		return;
1667 
1668 	/* Initial rsnie_present for the nontransmitted profile is set to be same as that
1669 	 * of the transmitted profile. It will be changed if security configurations are
1670 	 * different.
1671 	 */
1672 	*nontx_profile_found = false;
1673 	for_each_element_id(elem, WLAN_EID_MULTIPLE_BSSID, start, rem_len) {
1674 		/* Fixed minimum MBSSID element length with at least one
1675 		 * nontransmitted BSSID profile is 12 bytes as given below;
1676 		 * 1 (max BSSID indicator) +
1677 		 * 2 (Nontransmitted BSSID profile: Subelement ID + length) +
1678 		 * 4 (Nontransmitted BSSID Capabilities: tag + length + info)
1679 		 * 2 (Nontransmitted BSSID SSID: tag + length)
1680 		 * 3 (Nontransmitted BSSID Index: tag + length + BSSID index
1681 		 */
1682 		if (elem->datalen < 12 || elem->data[0] < 1)
1683 			continue; /* Max BSSID indicator must be >=1 */
1684 
1685 		for_each_element(nontx, elem->data + 1, elem->datalen - 1) {
1686 			start = nontx->data;
1687 
1688 			if (nontx->id != 0 || nontx->datalen < 4)
1689 				continue; /* Invalid nontransmitted profile */
1690 
1691 			if (nontx->data[0] != WLAN_EID_NON_TX_BSSID_CAP ||
1692 			    nontx->data[1] != 2) {
1693 				continue; /* Missing nontransmitted BSS capabilities */
1694 			}
1695 
1696 			if (nontx->data[4] != WLAN_EID_SSID)
1697 				continue; /* Missing SSID for nontransmitted BSS */
1698 
1699 			index = cfg80211_find_elem(WLAN_EID_MULTI_BSSID_IDX,
1700 						   start, nontx->datalen);
1701 			if (!index || index->datalen < 1 || index->data[0] == 0)
1702 				continue; /* Invalid MBSSID Index element */
1703 
1704 			if (index->data[0] == bssid_index) {
1705 				*nontx_profile_found = true;
1706 
1707 				/* Check if nontx BSS has beacon protection enabled */
1708 				if (!tx_arvif->beacon_prot) {
1709 					ext_cap_ie =
1710 					    cfg80211_find_elem(WLAN_EID_EXT_CAPABILITY,
1711 							       nontx->data,
1712 							       nontx->datalen);
1713 					if (ext_cap_ie && ext_cap_ie->datalen >= 11 &&
1714 					    (ext_cap_ie->data[10] &
1715 					     WLAN_EXT_CAPA11_BCN_PROTECT))
1716 						tx_arvif->beacon_prot = true;
1717 				}
1718 
1719 				if (cfg80211_find_ie(WLAN_EID_RSN,
1720 						     nontx->data,
1721 						     nontx->datalen)) {
1722 					arvif->rsnie_present = true;
1723 					return;
1724 				} else if (!arvif->rsnie_present) {
1725 					return; /* Both tx and nontx BSS are open */
1726 				}
1727 
1728 				nie = cfg80211_find_ext_elem(WLAN_EID_EXT_NON_INHERITANCE,
1729 							     nontx->data,
1730 							     nontx->datalen);
1731 				if (!nie || nie->datalen < 2)
1732 					return; /* Invalid non-inheritance element */
1733 
1734 				for (i = 1; i < nie->datalen - 1; i++) {
1735 					if (nie->data[i] == WLAN_EID_RSN) {
1736 						arvif->rsnie_present = false;
1737 						break;
1738 					}
1739 				}
1740 
1741 				return;
1742 			}
1743 		}
1744 	}
1745 }
1746 
1747 static int ath12k_mac_setup_bcn_tmpl_ema(struct ath12k_link_vif *arvif,
1748 					 struct ath12k_link_vif *tx_arvif,
1749 					 u8 bssid_index)
1750 {
1751 	struct ath12k_wmi_bcn_tmpl_ema_arg ema_args;
1752 	struct ieee80211_ema_beacons *beacons;
1753 	bool nontx_profile_found = false;
1754 	int ret = 0;
1755 	u8 i;
1756 
1757 	beacons = ieee80211_beacon_get_template_ema_list(ath12k_ar_to_hw(tx_arvif->ar),
1758 							 tx_arvif->ahvif->vif,
1759 							 tx_arvif->link_id);
1760 	if (!beacons || !beacons->cnt) {
1761 		ath12k_warn(arvif->ar->ab,
1762 			    "failed to get ema beacon templates from mac80211\n");
1763 		return -EPERM;
1764 	}
1765 
1766 	if (tx_arvif == arvif)
1767 		ath12k_mac_set_arvif_ies(arvif, tx_arvif, beacons->bcn[0].skb, 0, NULL);
1768 
1769 	for (i = 0; i < beacons->cnt; i++) {
1770 		if (tx_arvif != arvif && !nontx_profile_found)
1771 			ath12k_mac_set_arvif_ies(arvif, tx_arvif, beacons->bcn[i].skb,
1772 						 bssid_index,
1773 						 &nontx_profile_found);
1774 
1775 		ema_args.bcn_cnt = beacons->cnt;
1776 		ema_args.bcn_index = i;
1777 		ret = ath12k_wmi_bcn_tmpl(tx_arvif, &beacons->bcn[i].offs,
1778 					  beacons->bcn[i].skb, &ema_args);
1779 		if (ret) {
1780 			ath12k_warn(tx_arvif->ar->ab,
1781 				    "failed to set ema beacon template id %i error %d\n",
1782 				    i, ret);
1783 			break;
1784 		}
1785 	}
1786 
1787 	if (tx_arvif != arvif && !nontx_profile_found)
1788 		ath12k_warn(arvif->ar->ab,
1789 			    "nontransmitted bssid index %u not found in beacon template\n",
1790 			    bssid_index);
1791 
1792 	ieee80211_beacon_free_ema_list(beacons);
1793 	return ret;
1794 }
1795 
1796 static int ath12k_mac_setup_bcn_tmpl(struct ath12k_link_vif *arvif)
1797 {
1798 	struct ath12k_vif *ahvif = arvif->ahvif;
1799 	struct ieee80211_vif *vif = ath12k_ahvif_to_vif(ahvif);
1800 	struct ieee80211_bss_conf *link_conf;
1801 	struct ath12k_link_vif *tx_arvif;
1802 	struct ath12k *ar = arvif->ar;
1803 	struct ath12k_base *ab = ar->ab;
1804 	struct ieee80211_mutable_offsets offs = {};
1805 	bool nontx_profile_found = false;
1806 	struct sk_buff *bcn;
1807 	int ret;
1808 
1809 	if (ahvif->vdev_type != WMI_VDEV_TYPE_AP)
1810 		return 0;
1811 
1812 	link_conf = ath12k_mac_get_link_bss_conf(arvif);
1813 	if (!link_conf) {
1814 		ath12k_warn(ar->ab, "unable to access bss link conf to set bcn tmpl for vif %pM link %u\n",
1815 			    vif->addr, arvif->link_id);
1816 		return -ENOLINK;
1817 	}
1818 
1819 	tx_arvif = ath12k_mac_get_tx_arvif(arvif, link_conf);
1820 	if (tx_arvif) {
1821 		if (tx_arvif != arvif && arvif->is_up)
1822 			return 0;
1823 
1824 		if (link_conf->ema_ap)
1825 			return ath12k_mac_setup_bcn_tmpl_ema(arvif, tx_arvif,
1826 							     link_conf->bssid_index);
1827 	} else {
1828 		tx_arvif = arvif;
1829 	}
1830 
1831 	bcn = ieee80211_beacon_get_template(ath12k_ar_to_hw(tx_arvif->ar),
1832 					    tx_arvif->ahvif->vif,
1833 					    &offs, tx_arvif->link_id);
1834 	if (!bcn) {
1835 		ath12k_warn(ab, "failed to get beacon template from mac80211\n");
1836 		return -EPERM;
1837 	}
1838 
1839 	if (tx_arvif == arvif) {
1840 		ath12k_mac_set_arvif_ies(arvif, tx_arvif, bcn, 0, NULL);
1841 	} else {
1842 		ath12k_mac_set_arvif_ies(arvif, tx_arvif, bcn,
1843 					 link_conf->bssid_index,
1844 					 &nontx_profile_found);
1845 		if (!nontx_profile_found)
1846 			ath12k_warn(ab,
1847 				    "nontransmitted profile not found in beacon template\n");
1848 	}
1849 
1850 	if (ahvif->vif->type == NL80211_IFTYPE_AP && ahvif->vif->p2p) {
1851 		ret = ath12k_mac_setup_bcn_p2p_ie(arvif, bcn);
1852 		if (ret) {
1853 			ath12k_warn(ab, "failed to setup P2P GO bcn ie: %d\n",
1854 				    ret);
1855 			goto free_bcn_skb;
1856 		}
1857 
1858 		/* P2P IE is inserted by firmware automatically (as
1859 		 * configured above) so remove it from the base beacon
1860 		 * template to avoid duplicate P2P IEs in beacon frames.
1861 		 */
1862 		ret = ath12k_mac_remove_vendor_ie(bcn, WLAN_OUI_WFA,
1863 						  WLAN_OUI_TYPE_WFA_P2P,
1864 						  offsetof(struct ieee80211_mgmt,
1865 							   u.beacon.variable));
1866 		if (ret) {
1867 			ath12k_warn(ab, "failed to remove P2P vendor ie: %d\n",
1868 				    ret);
1869 			goto free_bcn_skb;
1870 		}
1871 	}
1872 
1873 	ret = ath12k_wmi_bcn_tmpl(arvif, &offs, bcn, NULL);
1874 
1875 	if (ret)
1876 		ath12k_warn(ab, "failed to submit beacon template command: %d\n",
1877 			    ret);
1878 
1879 free_bcn_skb:
1880 	kfree_skb(bcn);
1881 	return ret;
1882 }
1883 
1884 static void ath12k_control_beaconing(struct ath12k_link_vif *arvif,
1885 				     struct ieee80211_bss_conf *info)
1886 {
1887 	struct ath12k_wmi_vdev_up_params params = {};
1888 	struct ath12k_vif *ahvif = arvif->ahvif;
1889 	struct ath12k *ar = arvif->ar;
1890 	int ret;
1891 
1892 	lockdep_assert_wiphy(ath12k_ar_to_hw(arvif->ar)->wiphy);
1893 
1894 	if (!info->enable_beacon) {
1895 		ret = ath12k_wmi_vdev_down(ar, arvif->vdev_id);
1896 		if (ret)
1897 			ath12k_warn(ar->ab, "failed to down vdev_id %i: %d\n",
1898 				    arvif->vdev_id, ret);
1899 
1900 		arvif->is_up = false;
1901 		return;
1902 	}
1903 
1904 	/* Install the beacon template to the FW */
1905 	ret = ath12k_mac_setup_bcn_tmpl(arvif);
1906 	if (ret) {
1907 		ath12k_warn(ar->ab, "failed to update bcn tmpl during vdev up: %d\n",
1908 			    ret);
1909 		return;
1910 	}
1911 
1912 	ahvif->aid = 0;
1913 
1914 	ether_addr_copy(arvif->bssid, info->addr);
1915 
1916 	params.vdev_id = arvif->vdev_id;
1917 	params.aid = ahvif->aid;
1918 	params.bssid = arvif->bssid;
1919 	params.tx_bssid = ath12k_mac_get_tx_bssid(arvif);
1920 	if (params.tx_bssid) {
1921 		params.nontx_profile_idx = info->bssid_index;
1922 		params.nontx_profile_cnt = 1 << info->bssid_indicator;
1923 	}
1924 	ret = ath12k_wmi_vdev_up(arvif->ar, &params);
1925 	if (ret) {
1926 		ath12k_warn(ar->ab, "failed to bring up vdev %d: %i\n",
1927 			    arvif->vdev_id, ret);
1928 		return;
1929 	}
1930 
1931 	arvif->is_up = true;
1932 
1933 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac vdev %d up\n", arvif->vdev_id);
1934 }
1935 
1936 static void ath12k_mac_handle_beacon_iter(void *data, u8 *mac,
1937 					  struct ieee80211_vif *vif)
1938 {
1939 	struct sk_buff *skb = data;
1940 	struct ieee80211_mgmt *mgmt = (void *)skb->data;
1941 	struct ath12k_vif *ahvif = ath12k_vif_to_ahvif(vif);
1942 	struct ath12k_link_vif *arvif = &ahvif->deflink;
1943 
1944 	if (vif->type != NL80211_IFTYPE_STATION || !arvif->is_created)
1945 		return;
1946 
1947 	if (!ether_addr_equal(mgmt->bssid, vif->bss_conf.bssid))
1948 		return;
1949 
1950 	cancel_delayed_work(&arvif->connection_loss_work);
1951 }
1952 
1953 void ath12k_mac_handle_beacon(struct ath12k *ar, struct sk_buff *skb)
1954 {
1955 	ieee80211_iterate_active_interfaces_atomic(ath12k_ar_to_hw(ar),
1956 						   IEEE80211_IFACE_ITER_NORMAL,
1957 						   ath12k_mac_handle_beacon_iter,
1958 						   skb);
1959 }
1960 
1961 void ath12k_mac_handle_beacon_miss(struct ath12k *ar,
1962 				   struct ath12k_link_vif *arvif)
1963 {
1964 	struct ieee80211_hw *hw = ath12k_ar_to_hw(ar);
1965 	struct ieee80211_vif *vif = ath12k_ahvif_to_vif(arvif->ahvif);
1966 
1967 	if (!(arvif->is_created && arvif->is_up))
1968 		return;
1969 
1970 	ieee80211_beacon_loss(vif);
1971 
1972 	/* Firmware doesn't report beacon loss events repeatedly. If AP probe
1973 	 * (done by mac80211) succeeds but beacons do not resume then it
1974 	 * doesn't make sense to continue operation. Queue connection loss work
1975 	 * which can be cancelled when beacon is received.
1976 	 */
1977 	ieee80211_queue_delayed_work(hw, &arvif->connection_loss_work,
1978 				     ATH12K_CONNECTION_LOSS_HZ);
1979 }
1980 
1981 static void ath12k_mac_vif_sta_connection_loss_work(struct work_struct *work)
1982 {
1983 	struct ath12k_link_vif *arvif = container_of(work, struct ath12k_link_vif,
1984 						     connection_loss_work.work);
1985 	struct ieee80211_vif *vif = arvif->ahvif->vif;
1986 
1987 	if (!arvif->is_up)
1988 		return;
1989 
1990 	ieee80211_connection_loss(vif);
1991 }
1992 
1993 static void ath12k_peer_assoc_h_basic(struct ath12k *ar,
1994 				      struct ath12k_link_vif *arvif,
1995 				      struct ath12k_link_sta *arsta,
1996 				      struct ath12k_wmi_peer_assoc_arg *arg)
1997 {
1998 	struct ieee80211_vif *vif = ath12k_ahvif_to_vif(arvif->ahvif);
1999 	struct ieee80211_sta *sta = ath12k_ahsta_to_sta(arsta->ahsta);
2000 	struct ieee80211_hw *hw = ath12k_ar_to_hw(ar);
2001 	struct ieee80211_bss_conf *bss_conf;
2002 	u32 aid;
2003 
2004 	lockdep_assert_wiphy(hw->wiphy);
2005 
2006 	if (vif->type == NL80211_IFTYPE_STATION)
2007 		aid = vif->cfg.aid;
2008 	else
2009 		aid = sta->aid;
2010 
2011 	ether_addr_copy(arg->peer_mac, arsta->addr);
2012 	arg->vdev_id = arvif->vdev_id;
2013 	arg->peer_associd = aid;
2014 	arg->auth_flag = true;
2015 	/* TODO: STA WAR in ath10k for listen interval required? */
2016 	arg->peer_listen_intval = hw->conf.listen_interval;
2017 	arg->peer_nss = 1;
2018 
2019 	bss_conf = ath12k_mac_get_link_bss_conf(arvif);
2020 	if (!bss_conf) {
2021 		ath12k_warn(ar->ab, "unable to access bss link conf in peer assoc for vif %pM link %u\n",
2022 			    vif->addr, arvif->link_id);
2023 		return;
2024 	}
2025 
2026 	arg->peer_caps = bss_conf->assoc_capability;
2027 }
2028 
2029 static void ath12k_peer_assoc_h_crypto(struct ath12k *ar,
2030 				       struct ath12k_link_vif *arvif,
2031 				       struct ath12k_link_sta *arsta,
2032 				       struct ath12k_wmi_peer_assoc_arg *arg)
2033 {
2034 	struct ieee80211_vif *vif = ath12k_ahvif_to_vif(arvif->ahvif);
2035 	struct ieee80211_sta *sta = ath12k_ahsta_to_sta(arsta->ahsta);
2036 	struct ieee80211_bss_conf *info;
2037 	struct cfg80211_chan_def def;
2038 	struct cfg80211_bss *bss;
2039 	struct ieee80211_hw *hw = ath12k_ar_to_hw(ar);
2040 	const u8 *rsnie = NULL;
2041 	const u8 *wpaie = NULL;
2042 
2043 	lockdep_assert_wiphy(hw->wiphy);
2044 
2045 	info = ath12k_mac_get_link_bss_conf(arvif);
2046 	if (!info) {
2047 		ath12k_warn(ar->ab, "unable to access bss link conf for peer assoc crypto for vif %pM link %u\n",
2048 			    vif->addr, arvif->link_id);
2049 		return;
2050 	}
2051 
2052 	if (WARN_ON(ath12k_mac_vif_link_chan(vif, arvif->link_id, &def)))
2053 		return;
2054 
2055 	bss = cfg80211_get_bss(hw->wiphy, def.chan, info->bssid, NULL, 0,
2056 			       IEEE80211_BSS_TYPE_ANY, IEEE80211_PRIVACY_ANY);
2057 
2058 	if (arvif->rsnie_present || arvif->wpaie_present) {
2059 		arg->need_ptk_4_way = true;
2060 		if (arvif->wpaie_present)
2061 			arg->need_gtk_2_way = true;
2062 	} else if (bss) {
2063 		const struct cfg80211_bss_ies *ies;
2064 
2065 		rcu_read_lock();
2066 		rsnie = ieee80211_bss_get_ie(bss, WLAN_EID_RSN);
2067 
2068 		ies = rcu_dereference(bss->ies);
2069 
2070 		wpaie = cfg80211_find_vendor_ie(WLAN_OUI_MICROSOFT,
2071 						WLAN_OUI_TYPE_MICROSOFT_WPA,
2072 						ies->data,
2073 						ies->len);
2074 		rcu_read_unlock();
2075 		cfg80211_put_bss(hw->wiphy, bss);
2076 	}
2077 
2078 	/* FIXME: base on RSN IE/WPA IE is a correct idea? */
2079 	if (rsnie || wpaie) {
2080 		ath12k_dbg(ar->ab, ATH12K_DBG_WMI,
2081 			   "%s: rsn ie found\n", __func__);
2082 		arg->need_ptk_4_way = true;
2083 	}
2084 
2085 	if (wpaie) {
2086 		ath12k_dbg(ar->ab, ATH12K_DBG_WMI,
2087 			   "%s: wpa ie found\n", __func__);
2088 		arg->need_gtk_2_way = true;
2089 	}
2090 
2091 	if (sta->mfp) {
2092 		/* TODO: Need to check if FW supports PMF? */
2093 		arg->is_pmf_enabled = true;
2094 	}
2095 
2096 	/* TODO: safe_mode_enabled (bypass 4-way handshake) flag req? */
2097 }
2098 
2099 static void ath12k_peer_assoc_h_rates(struct ath12k *ar,
2100 				      struct ath12k_link_vif *arvif,
2101 				      struct ath12k_link_sta *arsta,
2102 				      struct ath12k_wmi_peer_assoc_arg *arg)
2103 {
2104 	struct ieee80211_vif *vif = ath12k_ahvif_to_vif(arvif->ahvif);
2105 	struct ieee80211_sta *sta = ath12k_ahsta_to_sta(arsta->ahsta);
2106 	struct wmi_rate_set_arg *rateset = &arg->peer_legacy_rates;
2107 	struct ieee80211_link_sta *link_sta;
2108 	struct cfg80211_chan_def def;
2109 	const struct ieee80211_supported_band *sband;
2110 	const struct ieee80211_rate *rates;
2111 	struct ieee80211_hw *hw = ath12k_ar_to_hw(ar);
2112 	enum nl80211_band band;
2113 	u32 ratemask;
2114 	u8 rate;
2115 	int i;
2116 
2117 	lockdep_assert_wiphy(hw->wiphy);
2118 
2119 	if (WARN_ON(ath12k_mac_vif_link_chan(vif, arvif->link_id, &def)))
2120 		return;
2121 
2122 	link_sta = ath12k_mac_get_link_sta(arsta);
2123 	if (!link_sta) {
2124 		ath12k_warn(ar->ab, "unable to access link sta in peer assoc rates for sta %pM link %u\n",
2125 			    sta->addr, arsta->link_id);
2126 		return;
2127 	}
2128 
2129 	band = def.chan->band;
2130 	sband = hw->wiphy->bands[band];
2131 	ratemask = link_sta->supp_rates[band];
2132 	ratemask &= arvif->bitrate_mask.control[band].legacy;
2133 	rates = sband->bitrates;
2134 
2135 	rateset->num_rates = 0;
2136 
2137 	for (i = 0; i < 32; i++, ratemask >>= 1, rates++) {
2138 		if (!(ratemask & 1))
2139 			continue;
2140 
2141 		rate = ath12k_mac_bitrate_to_rate(rates->bitrate);
2142 		rateset->rates[rateset->num_rates] = rate;
2143 		rateset->num_rates++;
2144 	}
2145 }
2146 
2147 static bool
2148 ath12k_peer_assoc_h_ht_masked(const u8 *ht_mcs_mask)
2149 {
2150 	int nss;
2151 
2152 	for (nss = 0; nss < IEEE80211_HT_MCS_MASK_LEN; nss++)
2153 		if (ht_mcs_mask[nss])
2154 			return false;
2155 
2156 	return true;
2157 }
2158 
2159 static bool
2160 ath12k_peer_assoc_h_vht_masked(const u16 *vht_mcs_mask)
2161 {
2162 	int nss;
2163 
2164 	for (nss = 0; nss < NL80211_VHT_NSS_MAX; nss++)
2165 		if (vht_mcs_mask[nss])
2166 			return false;
2167 
2168 	return true;
2169 }
2170 
2171 static void ath12k_peer_assoc_h_ht(struct ath12k *ar,
2172 				   struct ath12k_link_vif *arvif,
2173 				   struct ath12k_link_sta *arsta,
2174 				   struct ath12k_wmi_peer_assoc_arg *arg)
2175 {
2176 	struct ieee80211_vif *vif = ath12k_ahvif_to_vif(arvif->ahvif);
2177 	struct ieee80211_sta *sta = ath12k_ahsta_to_sta(arsta->ahsta);
2178 	const struct ieee80211_sta_ht_cap *ht_cap;
2179 	struct ieee80211_link_sta *link_sta;
2180 	struct cfg80211_chan_def def;
2181 	enum nl80211_band band;
2182 	const u8 *ht_mcs_mask;
2183 	int i, n;
2184 	u8 max_nss;
2185 	u32 stbc;
2186 
2187 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
2188 
2189 	if (WARN_ON(ath12k_mac_vif_link_chan(vif, arvif->link_id, &def)))
2190 		return;
2191 
2192 	link_sta = ath12k_mac_get_link_sta(arsta);
2193 	if (!link_sta) {
2194 		ath12k_warn(ar->ab, "unable to access link sta in peer assoc ht for sta %pM link %u\n",
2195 			    sta->addr, arsta->link_id);
2196 		return;
2197 	}
2198 
2199 	ht_cap = &link_sta->ht_cap;
2200 	if (!ht_cap->ht_supported)
2201 		return;
2202 
2203 	band = def.chan->band;
2204 	ht_mcs_mask = arvif->bitrate_mask.control[band].ht_mcs;
2205 
2206 	if (ath12k_peer_assoc_h_ht_masked(ht_mcs_mask))
2207 		return;
2208 
2209 	arg->ht_flag = true;
2210 
2211 	arg->peer_max_mpdu = (1 << (IEEE80211_HT_MAX_AMPDU_FACTOR +
2212 				    ht_cap->ampdu_factor)) - 1;
2213 
2214 	arg->peer_mpdu_density =
2215 		ath12k_parse_mpdudensity(ht_cap->ampdu_density);
2216 
2217 	arg->peer_ht_caps = ht_cap->cap;
2218 	arg->peer_rate_caps |= WMI_HOST_RC_HT_FLAG;
2219 
2220 	if (ht_cap->cap & IEEE80211_HT_CAP_LDPC_CODING)
2221 		arg->ldpc_flag = true;
2222 
2223 	if (link_sta->bandwidth >= IEEE80211_STA_RX_BW_40) {
2224 		arg->bw_40 = true;
2225 		arg->peer_rate_caps |= WMI_HOST_RC_CW40_FLAG;
2226 	}
2227 
2228 	/* As firmware handles these two flags (IEEE80211_HT_CAP_SGI_20
2229 	 * and IEEE80211_HT_CAP_SGI_40) for enabling SGI, reset both
2230 	 * flags if guard interval is to force Long GI
2231 	 */
2232 	if (arvif->bitrate_mask.control[band].gi == NL80211_TXRATE_FORCE_LGI) {
2233 		arg->peer_ht_caps &= ~(IEEE80211_HT_CAP_SGI_20 | IEEE80211_HT_CAP_SGI_40);
2234 	} else {
2235 		/* Enable SGI flag if either SGI_20 or SGI_40 is supported */
2236 		if (ht_cap->cap & (IEEE80211_HT_CAP_SGI_20 | IEEE80211_HT_CAP_SGI_40))
2237 			arg->peer_rate_caps |= WMI_HOST_RC_SGI_FLAG;
2238 	}
2239 
2240 	if (ht_cap->cap & IEEE80211_HT_CAP_TX_STBC) {
2241 		arg->peer_rate_caps |= WMI_HOST_RC_TX_STBC_FLAG;
2242 		arg->stbc_flag = true;
2243 	}
2244 
2245 	if (ht_cap->cap & IEEE80211_HT_CAP_RX_STBC) {
2246 		stbc = ht_cap->cap & IEEE80211_HT_CAP_RX_STBC;
2247 		stbc = stbc >> IEEE80211_HT_CAP_RX_STBC_SHIFT;
2248 		stbc = stbc << WMI_HOST_RC_RX_STBC_FLAG_S;
2249 		arg->peer_rate_caps |= stbc;
2250 		arg->stbc_flag = true;
2251 	}
2252 
2253 	if (ht_cap->mcs.rx_mask[1] && ht_cap->mcs.rx_mask[2])
2254 		arg->peer_rate_caps |= WMI_HOST_RC_TS_FLAG;
2255 	else if (ht_cap->mcs.rx_mask[1])
2256 		arg->peer_rate_caps |= WMI_HOST_RC_DS_FLAG;
2257 
2258 	for (i = 0, n = 0, max_nss = 0; i < IEEE80211_HT_MCS_MASK_LEN * 8; i++)
2259 		if ((ht_cap->mcs.rx_mask[i / 8] & BIT(i % 8)) &&
2260 		    (ht_mcs_mask[i / 8] & BIT(i % 8))) {
2261 			max_nss = (i / 8) + 1;
2262 			arg->peer_ht_rates.rates[n++] = i;
2263 		}
2264 
2265 	/* This is a workaround for HT-enabled STAs which break the spec
2266 	 * and have no HT capabilities RX mask (no HT RX MCS map).
2267 	 *
2268 	 * As per spec, in section 20.3.5 Modulation and coding scheme (MCS),
2269 	 * MCS 0 through 7 are mandatory in 20MHz with 800 ns GI at all STAs.
2270 	 *
2271 	 * Firmware asserts if such situation occurs.
2272 	 */
2273 	if (n == 0) {
2274 		arg->peer_ht_rates.num_rates = 8;
2275 		for (i = 0; i < arg->peer_ht_rates.num_rates; i++)
2276 			arg->peer_ht_rates.rates[i] = i;
2277 	} else {
2278 		arg->peer_ht_rates.num_rates = n;
2279 		arg->peer_nss = min(link_sta->rx_nss, max_nss);
2280 	}
2281 
2282 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac ht peer %pM mcs cnt %d nss %d\n",
2283 		   arg->peer_mac,
2284 		   arg->peer_ht_rates.num_rates,
2285 		   arg->peer_nss);
2286 }
2287 
2288 static int ath12k_mac_get_max_vht_mcs_map(u16 mcs_map, int nss)
2289 {
2290 	switch ((mcs_map >> (2 * nss)) & 0x3) {
2291 	case IEEE80211_VHT_MCS_SUPPORT_0_7: return BIT(8) - 1;
2292 	case IEEE80211_VHT_MCS_SUPPORT_0_8: return BIT(9) - 1;
2293 	case IEEE80211_VHT_MCS_SUPPORT_0_9: return BIT(10) - 1;
2294 	}
2295 	return 0;
2296 }
2297 
2298 static u16
2299 ath12k_peer_assoc_h_vht_limit(u16 tx_mcs_set,
2300 			      const u16 vht_mcs_limit[NL80211_VHT_NSS_MAX])
2301 {
2302 	int idx_limit;
2303 	int nss;
2304 	u16 mcs_map;
2305 	u16 mcs;
2306 
2307 	for (nss = 0; nss < NL80211_VHT_NSS_MAX; nss++) {
2308 		mcs_map = ath12k_mac_get_max_vht_mcs_map(tx_mcs_set, nss) &
2309 			  vht_mcs_limit[nss];
2310 
2311 		if (mcs_map)
2312 			idx_limit = fls(mcs_map) - 1;
2313 		else
2314 			idx_limit = -1;
2315 
2316 		switch (idx_limit) {
2317 		case 0:
2318 		case 1:
2319 		case 2:
2320 		case 3:
2321 		case 4:
2322 		case 5:
2323 		case 6:
2324 		case 7:
2325 			mcs = IEEE80211_VHT_MCS_SUPPORT_0_7;
2326 			break;
2327 		case 8:
2328 			mcs = IEEE80211_VHT_MCS_SUPPORT_0_8;
2329 			break;
2330 		case 9:
2331 			mcs = IEEE80211_VHT_MCS_SUPPORT_0_9;
2332 			break;
2333 		default:
2334 			WARN_ON(1);
2335 			fallthrough;
2336 		case -1:
2337 			mcs = IEEE80211_VHT_MCS_NOT_SUPPORTED;
2338 			break;
2339 		}
2340 
2341 		tx_mcs_set &= ~(0x3 << (nss * 2));
2342 		tx_mcs_set |= mcs << (nss * 2);
2343 	}
2344 
2345 	return tx_mcs_set;
2346 }
2347 
2348 static u8 ath12k_get_nss_160mhz(struct ath12k *ar,
2349 				u8 max_nss)
2350 {
2351 	u8 nss_ratio_info = ar->pdev->cap.nss_ratio_info;
2352 	u8 max_sup_nss = 0;
2353 
2354 	switch (nss_ratio_info) {
2355 	case WMI_NSS_RATIO_1BY2_NSS:
2356 		max_sup_nss = max_nss >> 1;
2357 		break;
2358 	case WMI_NSS_RATIO_3BY4_NSS:
2359 		ath12k_warn(ar->ab, "WMI_NSS_RATIO_3BY4_NSS not supported\n");
2360 		break;
2361 	case WMI_NSS_RATIO_1_NSS:
2362 		max_sup_nss = max_nss;
2363 		break;
2364 	case WMI_NSS_RATIO_2_NSS:
2365 		ath12k_warn(ar->ab, "WMI_NSS_RATIO_2_NSS not supported\n");
2366 		break;
2367 	default:
2368 		ath12k_warn(ar->ab, "invalid nss ratio received from fw: %d\n",
2369 			    nss_ratio_info);
2370 		break;
2371 	}
2372 
2373 	return max_sup_nss;
2374 }
2375 
2376 static void ath12k_peer_assoc_h_vht(struct ath12k *ar,
2377 				    struct ath12k_link_vif *arvif,
2378 				    struct ath12k_link_sta *arsta,
2379 				    struct ath12k_wmi_peer_assoc_arg *arg)
2380 {
2381 	struct ieee80211_vif *vif = ath12k_ahvif_to_vif(arvif->ahvif);
2382 	struct ieee80211_sta *sta = ath12k_ahsta_to_sta(arsta->ahsta);
2383 	const struct ieee80211_sta_vht_cap *vht_cap;
2384 	struct ieee80211_link_sta *link_sta;
2385 	struct cfg80211_chan_def def;
2386 	enum nl80211_band band;
2387 	u16 *vht_mcs_mask;
2388 	u8 ampdu_factor;
2389 	u8 max_nss, vht_mcs;
2390 	int i, vht_nss, nss_idx;
2391 	bool user_rate_valid = true;
2392 	u32 rx_nss, tx_nss, nss_160;
2393 
2394 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
2395 
2396 	if (WARN_ON(ath12k_mac_vif_link_chan(vif, arvif->link_id, &def)))
2397 		return;
2398 
2399 	link_sta = ath12k_mac_get_link_sta(arsta);
2400 	if (!link_sta) {
2401 		ath12k_warn(ar->ab, "unable to access link sta in peer assoc vht for sta %pM link %u\n",
2402 			    sta->addr, arsta->link_id);
2403 		return;
2404 	}
2405 
2406 	vht_cap = &link_sta->vht_cap;
2407 	if (!vht_cap->vht_supported)
2408 		return;
2409 
2410 	band = def.chan->band;
2411 	vht_mcs_mask = arvif->bitrate_mask.control[band].vht_mcs;
2412 
2413 	if (ath12k_peer_assoc_h_vht_masked(vht_mcs_mask))
2414 		return;
2415 
2416 	arg->vht_flag = true;
2417 
2418 	/* TODO: similar flags required? */
2419 	arg->vht_capable = true;
2420 
2421 	if (def.chan->band == NL80211_BAND_2GHZ)
2422 		arg->vht_ng_flag = true;
2423 
2424 	arg->peer_vht_caps = vht_cap->cap;
2425 
2426 	ampdu_factor = (vht_cap->cap &
2427 			IEEE80211_VHT_CAP_MAX_A_MPDU_LENGTH_EXPONENT_MASK) >>
2428 		       IEEE80211_VHT_CAP_MAX_A_MPDU_LENGTH_EXPONENT_SHIFT;
2429 
2430 	/* Workaround: Some Netgear/Linksys 11ac APs set Rx A-MPDU factor to
2431 	 * zero in VHT IE. Using it would result in degraded throughput.
2432 	 * arg->peer_max_mpdu at this point contains HT max_mpdu so keep
2433 	 * it if VHT max_mpdu is smaller.
2434 	 */
2435 	arg->peer_max_mpdu = max(arg->peer_max_mpdu,
2436 				 (1U << (IEEE80211_HT_MAX_AMPDU_FACTOR +
2437 					ampdu_factor)) - 1);
2438 
2439 	if (link_sta->bandwidth == IEEE80211_STA_RX_BW_80)
2440 		arg->bw_80 = true;
2441 
2442 	if (link_sta->bandwidth == IEEE80211_STA_RX_BW_160)
2443 		arg->bw_160 = true;
2444 
2445 	vht_nss =  ath12k_mac_max_vht_nss(vht_mcs_mask);
2446 
2447 	if (vht_nss > link_sta->rx_nss) {
2448 		user_rate_valid = false;
2449 		for (nss_idx = link_sta->rx_nss - 1; nss_idx >= 0; nss_idx--) {
2450 			if (vht_mcs_mask[nss_idx]) {
2451 				user_rate_valid = true;
2452 				break;
2453 			}
2454 		}
2455 	}
2456 
2457 	if (!user_rate_valid) {
2458 		ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
2459 			   "Setting vht range MCS value to peer supported nss:%d for peer %pM\n",
2460 			   link_sta->rx_nss, arsta->addr);
2461 		vht_mcs_mask[link_sta->rx_nss - 1] = vht_mcs_mask[vht_nss - 1];
2462 	}
2463 
2464 	/* Calculate peer NSS capability from VHT capabilities if STA
2465 	 * supports VHT.
2466 	 */
2467 	for (i = 0, max_nss = 0, vht_mcs = 0; i < NL80211_VHT_NSS_MAX; i++) {
2468 		vht_mcs = __le16_to_cpu(vht_cap->vht_mcs.rx_mcs_map) >>
2469 			  (2 * i) & 3;
2470 
2471 		if (vht_mcs != IEEE80211_VHT_MCS_NOT_SUPPORTED &&
2472 		    vht_mcs_mask[i])
2473 			max_nss = i + 1;
2474 	}
2475 	arg->peer_nss = min(link_sta->rx_nss, max_nss);
2476 	arg->rx_max_rate = __le16_to_cpu(vht_cap->vht_mcs.rx_highest);
2477 	arg->rx_mcs_set = __le16_to_cpu(vht_cap->vht_mcs.rx_mcs_map);
2478 	arg->rx_mcs_set = ath12k_peer_assoc_h_vht_limit(arg->rx_mcs_set, vht_mcs_mask);
2479 
2480 	arg->tx_max_rate = __le16_to_cpu(vht_cap->vht_mcs.tx_highest);
2481 	arg->tx_mcs_set = __le16_to_cpu(vht_cap->vht_mcs.tx_mcs_map);
2482 
2483 	/* In QCN9274 platform, VHT MCS rate 10 and 11 is enabled by default.
2484 	 * VHT MCS rate 10 and 11 is not supported in 11ac standard.
2485 	 * so explicitly disable the VHT MCS rate 10 and 11 in 11ac mode.
2486 	 */
2487 	arg->tx_mcs_set &= ~IEEE80211_VHT_MCS_SUPPORT_0_11_MASK;
2488 	arg->tx_mcs_set |= IEEE80211_DISABLE_VHT_MCS_SUPPORT_0_11;
2489 
2490 	if ((arg->tx_mcs_set & IEEE80211_VHT_MCS_NOT_SUPPORTED) ==
2491 			IEEE80211_VHT_MCS_NOT_SUPPORTED)
2492 		arg->peer_vht_caps &= ~IEEE80211_VHT_CAP_MU_BEAMFORMEE_CAPABLE;
2493 
2494 	/* TODO:  Check */
2495 	arg->tx_max_mcs_nss = 0xFF;
2496 
2497 	if (arg->peer_phymode == MODE_11AC_VHT160) {
2498 		tx_nss = ath12k_get_nss_160mhz(ar, max_nss);
2499 		rx_nss = min(arg->peer_nss, tx_nss);
2500 		arg->peer_bw_rxnss_override = ATH12K_BW_NSS_MAP_ENABLE;
2501 
2502 		if (!rx_nss) {
2503 			ath12k_warn(ar->ab, "invalid max_nss\n");
2504 			return;
2505 		}
2506 
2507 		nss_160 = u32_encode_bits(rx_nss - 1, ATH12K_PEER_RX_NSS_160MHZ);
2508 		arg->peer_bw_rxnss_override |= nss_160;
2509 	}
2510 
2511 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
2512 		   "mac vht peer %pM max_mpdu %d flags 0x%x nss_override 0x%x\n",
2513 		   arsta->addr, arg->peer_max_mpdu, arg->peer_flags,
2514 		   arg->peer_bw_rxnss_override);
2515 }
2516 
2517 static int ath12k_mac_get_max_he_mcs_map(u16 mcs_map, int nss)
2518 {
2519 	switch ((mcs_map >> (2 * nss)) & 0x3) {
2520 	case IEEE80211_HE_MCS_SUPPORT_0_7: return BIT(8) - 1;
2521 	case IEEE80211_HE_MCS_SUPPORT_0_9: return BIT(10) - 1;
2522 	case IEEE80211_HE_MCS_SUPPORT_0_11: return BIT(12) - 1;
2523 	}
2524 	return 0;
2525 }
2526 
2527 static u16 ath12k_peer_assoc_h_he_limit(u16 tx_mcs_set,
2528 					const u16 *he_mcs_limit)
2529 {
2530 	int idx_limit;
2531 	int nss;
2532 	u16 mcs_map;
2533 	u16 mcs;
2534 
2535 	for (nss = 0; nss < NL80211_HE_NSS_MAX; nss++) {
2536 		mcs_map = ath12k_mac_get_max_he_mcs_map(tx_mcs_set, nss) &
2537 			he_mcs_limit[nss];
2538 
2539 		if (mcs_map)
2540 			idx_limit = fls(mcs_map) - 1;
2541 		else
2542 			idx_limit = -1;
2543 
2544 		switch (idx_limit) {
2545 		case 0 ... 7:
2546 			mcs = IEEE80211_HE_MCS_SUPPORT_0_7;
2547 			break;
2548 		case 8:
2549 		case 9:
2550 			mcs = IEEE80211_HE_MCS_SUPPORT_0_9;
2551 			break;
2552 		case 10:
2553 		case 11:
2554 			mcs = IEEE80211_HE_MCS_SUPPORT_0_11;
2555 			break;
2556 		default:
2557 			WARN_ON(1);
2558 			fallthrough;
2559 		case -1:
2560 			mcs = IEEE80211_HE_MCS_NOT_SUPPORTED;
2561 			break;
2562 		}
2563 
2564 		tx_mcs_set &= ~(0x3 << (nss * 2));
2565 		tx_mcs_set |= mcs << (nss * 2);
2566 	}
2567 
2568 	return tx_mcs_set;
2569 }
2570 
2571 static bool
2572 ath12k_peer_assoc_h_he_masked(const u16 he_mcs_mask[NL80211_HE_NSS_MAX])
2573 {
2574 	int nss;
2575 
2576 	for (nss = 0; nss < NL80211_HE_NSS_MAX; nss++)
2577 		if (he_mcs_mask[nss])
2578 			return false;
2579 
2580 	return true;
2581 }
2582 
2583 static void ath12k_peer_assoc_h_he(struct ath12k *ar,
2584 				   struct ath12k_link_vif *arvif,
2585 				   struct ath12k_link_sta *arsta,
2586 				   struct ath12k_wmi_peer_assoc_arg *arg)
2587 {
2588 	struct ieee80211_vif *vif = ath12k_ahvif_to_vif(arvif->ahvif);
2589 	struct ieee80211_sta *sta = ath12k_ahsta_to_sta(arsta->ahsta);
2590 	const struct ieee80211_sta_he_cap *he_cap;
2591 	struct ieee80211_bss_conf *link_conf;
2592 	struct ieee80211_link_sta *link_sta;
2593 	struct cfg80211_chan_def def;
2594 	int i;
2595 	u8 ampdu_factor, max_nss;
2596 	u8 rx_mcs_80 = IEEE80211_HE_MCS_NOT_SUPPORTED;
2597 	u8 rx_mcs_160 = IEEE80211_HE_MCS_NOT_SUPPORTED;
2598 	u16 mcs_160_map, mcs_80_map;
2599 	u8 link_id = arvif->link_id;
2600 	bool support_160;
2601 	enum nl80211_band band;
2602 	u16 *he_mcs_mask;
2603 	u8 he_mcs;
2604 	u16 he_tx_mcs = 0, v = 0;
2605 	int he_nss, nss_idx;
2606 	bool user_rate_valid = true;
2607 	u32 rx_nss, tx_nss, nss_160;
2608 
2609 	if (WARN_ON(ath12k_mac_vif_link_chan(vif, link_id, &def)))
2610 		return;
2611 
2612 	link_conf = ath12k_mac_get_link_bss_conf(arvif);
2613 	if (!link_conf) {
2614 		ath12k_warn(ar->ab, "unable to access bss link conf in peer assoc he for vif %pM link %u",
2615 			    vif->addr, link_id);
2616 		return;
2617 	}
2618 
2619 	link_sta = ath12k_mac_get_link_sta(arsta);
2620 	if (!link_sta) {
2621 		ath12k_warn(ar->ab, "unable to access link sta in peer assoc he for sta %pM link %u\n",
2622 			    sta->addr, arsta->link_id);
2623 		return;
2624 	}
2625 
2626 	he_cap = &link_sta->he_cap;
2627 	if (!he_cap->has_he)
2628 		return;
2629 
2630 	band = def.chan->band;
2631 	he_mcs_mask = arvif->bitrate_mask.control[band].he_mcs;
2632 
2633 	if (ath12k_peer_assoc_h_he_masked(he_mcs_mask))
2634 		return;
2635 
2636 	arg->he_flag = true;
2637 
2638 	support_160 = !!(he_cap->he_cap_elem.phy_cap_info[0] &
2639 		  IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_160MHZ_IN_5G);
2640 
2641 	/* Supported HE-MCS and NSS Set of peer he_cap is intersection with self he_cp */
2642 	mcs_160_map = le16_to_cpu(he_cap->he_mcs_nss_supp.rx_mcs_160);
2643 	mcs_80_map = le16_to_cpu(he_cap->he_mcs_nss_supp.rx_mcs_80);
2644 
2645 	if (support_160) {
2646 		for (i = 7; i >= 0; i--) {
2647 			u8 mcs_160 = (mcs_160_map >> (2 * i)) & 3;
2648 
2649 			if (mcs_160 != IEEE80211_HE_MCS_NOT_SUPPORTED) {
2650 				rx_mcs_160 = i + 1;
2651 				break;
2652 			}
2653 		}
2654 	}
2655 
2656 	for (i = 7; i >= 0; i--) {
2657 		u8 mcs_80 = (mcs_80_map >> (2 * i)) & 3;
2658 
2659 		if (mcs_80 != IEEE80211_HE_MCS_NOT_SUPPORTED) {
2660 			rx_mcs_80 = i + 1;
2661 			break;
2662 		}
2663 	}
2664 
2665 	if (support_160)
2666 		max_nss = min(rx_mcs_80, rx_mcs_160);
2667 	else
2668 		max_nss = rx_mcs_80;
2669 
2670 	arg->peer_nss = min(link_sta->rx_nss, max_nss);
2671 
2672 	memcpy(&arg->peer_he_cap_macinfo, he_cap->he_cap_elem.mac_cap_info,
2673 	       sizeof(he_cap->he_cap_elem.mac_cap_info));
2674 	memcpy(&arg->peer_he_cap_phyinfo, he_cap->he_cap_elem.phy_cap_info,
2675 	       sizeof(he_cap->he_cap_elem.phy_cap_info));
2676 	arg->peer_he_ops = link_conf->he_oper.params;
2677 
2678 	/* the top most byte is used to indicate BSS color info */
2679 	arg->peer_he_ops &= 0xffffff;
2680 
2681 	/* As per section 26.6.1 IEEE Std 802.11ax‐2022, if the Max AMPDU
2682 	 * Exponent Extension in HE cap is zero, use the arg->peer_max_mpdu
2683 	 * as calculated while parsing VHT caps(if VHT caps is present)
2684 	 * or HT caps (if VHT caps is not present).
2685 	 *
2686 	 * For non-zero value of Max AMPDU Exponent Extension in HE MAC caps,
2687 	 * if a HE STA sends VHT cap and HE cap IE in assoc request then, use
2688 	 * MAX_AMPDU_LEN_FACTOR as 20 to calculate max_ampdu length.
2689 	 * If a HE STA that does not send VHT cap, but HE and HT cap in assoc
2690 	 * request, then use MAX_AMPDU_LEN_FACTOR as 16 to calculate max_ampdu
2691 	 * length.
2692 	 */
2693 	ampdu_factor = u8_get_bits(he_cap->he_cap_elem.mac_cap_info[3],
2694 				   IEEE80211_HE_MAC_CAP3_MAX_AMPDU_LEN_EXP_MASK);
2695 
2696 	if (ampdu_factor) {
2697 		if (link_sta->vht_cap.vht_supported)
2698 			arg->peer_max_mpdu = (1 << (IEEE80211_HE_VHT_MAX_AMPDU_FACTOR +
2699 						    ampdu_factor)) - 1;
2700 		else if (link_sta->ht_cap.ht_supported)
2701 			arg->peer_max_mpdu = (1 << (IEEE80211_HE_HT_MAX_AMPDU_FACTOR +
2702 						    ampdu_factor)) - 1;
2703 	}
2704 
2705 	if (he_cap->he_cap_elem.phy_cap_info[6] &
2706 	    IEEE80211_HE_PHY_CAP6_PPE_THRESHOLD_PRESENT) {
2707 		int bit = 7;
2708 		int nss, ru;
2709 
2710 		arg->peer_ppet.numss_m1 = he_cap->ppe_thres[0] &
2711 					  IEEE80211_PPE_THRES_NSS_MASK;
2712 		arg->peer_ppet.ru_bit_mask =
2713 			(he_cap->ppe_thres[0] &
2714 			 IEEE80211_PPE_THRES_RU_INDEX_BITMASK_MASK) >>
2715 			IEEE80211_PPE_THRES_RU_INDEX_BITMASK_POS;
2716 
2717 		for (nss = 0; nss <= arg->peer_ppet.numss_m1; nss++) {
2718 			for (ru = 0; ru < 4; ru++) {
2719 				u32 val = 0;
2720 				int i;
2721 
2722 				if ((arg->peer_ppet.ru_bit_mask & BIT(ru)) == 0)
2723 					continue;
2724 				for (i = 0; i < 6; i++) {
2725 					val >>= 1;
2726 					val |= ((he_cap->ppe_thres[bit / 8] >>
2727 						 (bit % 8)) & 0x1) << 5;
2728 					bit++;
2729 				}
2730 				arg->peer_ppet.ppet16_ppet8_ru3_ru0[nss] |=
2731 								val << (ru * 6);
2732 			}
2733 		}
2734 	}
2735 
2736 	if (he_cap->he_cap_elem.mac_cap_info[0] & IEEE80211_HE_MAC_CAP0_TWT_RES)
2737 		arg->twt_responder = true;
2738 	if (he_cap->he_cap_elem.mac_cap_info[0] & IEEE80211_HE_MAC_CAP0_TWT_REQ)
2739 		arg->twt_requester = true;
2740 
2741 	he_nss = ath12k_mac_max_he_nss(he_mcs_mask);
2742 
2743 	if (he_nss > link_sta->rx_nss) {
2744 		user_rate_valid = false;
2745 		for (nss_idx = link_sta->rx_nss - 1; nss_idx >= 0; nss_idx--) {
2746 			if (he_mcs_mask[nss_idx]) {
2747 				user_rate_valid = true;
2748 				break;
2749 			}
2750 		}
2751 	}
2752 
2753 	if (!user_rate_valid) {
2754 		ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
2755 			   "Setting he range MCS value to peer supported nss:%d for peer %pM\n",
2756 			   link_sta->rx_nss, arsta->addr);
2757 		he_mcs_mask[link_sta->rx_nss - 1] = he_mcs_mask[he_nss - 1];
2758 	}
2759 
2760 	switch (link_sta->bandwidth) {
2761 	case IEEE80211_STA_RX_BW_160:
2762 		v = le16_to_cpu(he_cap->he_mcs_nss_supp.rx_mcs_160);
2763 		v = ath12k_peer_assoc_h_he_limit(v, he_mcs_mask);
2764 		arg->peer_he_rx_mcs_set[WMI_HECAP_TXRX_MCS_NSS_IDX_160] = v;
2765 
2766 		v = le16_to_cpu(he_cap->he_mcs_nss_supp.tx_mcs_160);
2767 		arg->peer_he_tx_mcs_set[WMI_HECAP_TXRX_MCS_NSS_IDX_160] = v;
2768 
2769 		arg->peer_he_mcs_count++;
2770 		if (!he_tx_mcs)
2771 			he_tx_mcs = v;
2772 		fallthrough;
2773 
2774 	default:
2775 		v = le16_to_cpu(he_cap->he_mcs_nss_supp.rx_mcs_80);
2776 		v = ath12k_peer_assoc_h_he_limit(v, he_mcs_mask);
2777 		arg->peer_he_rx_mcs_set[WMI_HECAP_TXRX_MCS_NSS_IDX_80] = v;
2778 
2779 		v = le16_to_cpu(he_cap->he_mcs_nss_supp.tx_mcs_80);
2780 		arg->peer_he_tx_mcs_set[WMI_HECAP_TXRX_MCS_NSS_IDX_80] = v;
2781 
2782 		arg->peer_he_mcs_count++;
2783 		if (!he_tx_mcs)
2784 			he_tx_mcs = v;
2785 		break;
2786 	}
2787 
2788 	/* Calculate peer NSS capability from HE capabilities if STA
2789 	 * supports HE.
2790 	 */
2791 	for (i = 0, max_nss = 0, he_mcs = 0; i < NL80211_HE_NSS_MAX; i++) {
2792 		he_mcs = he_tx_mcs >> (2 * i) & 3;
2793 
2794 		/* In case of fixed rates, MCS Range in he_tx_mcs might have
2795 		 * unsupported range, with he_mcs_mask set, so check either of them
2796 		 * to find nss.
2797 		 */
2798 		if (he_mcs != IEEE80211_HE_MCS_NOT_SUPPORTED ||
2799 		    he_mcs_mask[i])
2800 			max_nss = i + 1;
2801 	}
2802 
2803 	max_nss = min(max_nss, ar->num_tx_chains);
2804 	arg->peer_nss = min(link_sta->rx_nss, max_nss);
2805 
2806 	if (arg->peer_phymode == MODE_11AX_HE160) {
2807 		tx_nss = ath12k_get_nss_160mhz(ar, ar->num_tx_chains);
2808 		rx_nss = min(arg->peer_nss, tx_nss);
2809 
2810 		arg->peer_nss = min(link_sta->rx_nss, ar->num_rx_chains);
2811 		arg->peer_bw_rxnss_override = ATH12K_BW_NSS_MAP_ENABLE;
2812 
2813 		if (!rx_nss) {
2814 			ath12k_warn(ar->ab, "invalid max_nss\n");
2815 			return;
2816 		}
2817 
2818 		nss_160 = u32_encode_bits(rx_nss - 1, ATH12K_PEER_RX_NSS_160MHZ);
2819 		arg->peer_bw_rxnss_override |= nss_160;
2820 	}
2821 
2822 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
2823 		   "mac he peer %pM nss %d mcs cnt %d nss_override 0x%x\n",
2824 		   arsta->addr, arg->peer_nss,
2825 		   arg->peer_he_mcs_count,
2826 		   arg->peer_bw_rxnss_override);
2827 }
2828 
2829 static void ath12k_peer_assoc_h_he_6ghz(struct ath12k *ar,
2830 					struct ath12k_link_vif *arvif,
2831 					struct ath12k_link_sta *arsta,
2832 					struct ath12k_wmi_peer_assoc_arg *arg)
2833 {
2834 	struct ieee80211_vif *vif = ath12k_ahvif_to_vif(arvif->ahvif);
2835 	struct ieee80211_sta *sta = ath12k_ahsta_to_sta(arsta->ahsta);
2836 	const struct ieee80211_sta_he_cap *he_cap;
2837 	struct ieee80211_link_sta *link_sta;
2838 	struct cfg80211_chan_def def;
2839 	enum nl80211_band band;
2840 	u8 ampdu_factor, mpdu_density;
2841 
2842 	if (WARN_ON(ath12k_mac_vif_link_chan(vif, arvif->link_id, &def)))
2843 		return;
2844 
2845 	band = def.chan->band;
2846 
2847 	link_sta = ath12k_mac_get_link_sta(arsta);
2848 	if (!link_sta) {
2849 		ath12k_warn(ar->ab, "unable to access link sta in peer assoc he 6ghz for sta %pM link %u\n",
2850 			    sta->addr, arsta->link_id);
2851 		return;
2852 	}
2853 
2854 	he_cap = &link_sta->he_cap;
2855 
2856 	if (!arg->he_flag || band != NL80211_BAND_6GHZ || !link_sta->he_6ghz_capa.capa)
2857 		return;
2858 
2859 	if (link_sta->bandwidth == IEEE80211_STA_RX_BW_40)
2860 		arg->bw_40 = true;
2861 
2862 	if (link_sta->bandwidth == IEEE80211_STA_RX_BW_80)
2863 		arg->bw_80 = true;
2864 
2865 	if (link_sta->bandwidth == IEEE80211_STA_RX_BW_160)
2866 		arg->bw_160 = true;
2867 
2868 	if (link_sta->bandwidth == IEEE80211_STA_RX_BW_320)
2869 		arg->bw_320 = true;
2870 
2871 	arg->peer_he_caps_6ghz = le16_to_cpu(link_sta->he_6ghz_capa.capa);
2872 
2873 	mpdu_density = u32_get_bits(arg->peer_he_caps_6ghz,
2874 				    IEEE80211_HE_6GHZ_CAP_MIN_MPDU_START);
2875 	arg->peer_mpdu_density = ath12k_parse_mpdudensity(mpdu_density);
2876 
2877 	/* From IEEE Std 802.11ax-2021 - Section 10.12.2: An HE STA shall be capable of
2878 	 * receiving A-MPDU where the A-MPDU pre-EOF padding length is up to the value
2879 	 * indicated by the Maximum A-MPDU Length Exponent Extension field in the HE
2880 	 * Capabilities element and the Maximum A-MPDU Length Exponent field in HE 6 GHz
2881 	 * Band Capabilities element in the 6 GHz band.
2882 	 *
2883 	 * Here, we are extracting the Max A-MPDU Exponent Extension from HE caps and
2884 	 * factor is the Maximum A-MPDU Length Exponent from HE 6 GHZ Band capability.
2885 	 */
2886 	ampdu_factor = u8_get_bits(he_cap->he_cap_elem.mac_cap_info[3],
2887 				   IEEE80211_HE_MAC_CAP3_MAX_AMPDU_LEN_EXP_MASK) +
2888 			u32_get_bits(arg->peer_he_caps_6ghz,
2889 				     IEEE80211_HE_6GHZ_CAP_MAX_AMPDU_LEN_EXP);
2890 
2891 	arg->peer_max_mpdu = (1u << (IEEE80211_HE_6GHZ_MAX_AMPDU_FACTOR +
2892 				     ampdu_factor)) - 1;
2893 }
2894 
2895 static int ath12k_get_smps_from_capa(const struct ieee80211_sta_ht_cap *ht_cap,
2896 				     const struct ieee80211_he_6ghz_capa *he_6ghz_capa,
2897 				     int *smps)
2898 {
2899 	if (ht_cap->ht_supported)
2900 		*smps = u16_get_bits(ht_cap->cap, IEEE80211_HT_CAP_SM_PS);
2901 	else
2902 		*smps = le16_get_bits(he_6ghz_capa->capa,
2903 				      IEEE80211_HE_6GHZ_CAP_SM_PS);
2904 
2905 	if (*smps >= ARRAY_SIZE(ath12k_smps_map))
2906 		return -EINVAL;
2907 
2908 	return 0;
2909 }
2910 
2911 static void ath12k_peer_assoc_h_smps(struct ath12k_link_sta *arsta,
2912 				     struct ath12k_wmi_peer_assoc_arg *arg)
2913 {
2914 	struct ieee80211_sta *sta = ath12k_ahsta_to_sta(arsta->ahsta);
2915 	const struct ieee80211_he_6ghz_capa *he_6ghz_capa;
2916 	struct ath12k_link_vif *arvif = arsta->arvif;
2917 	const struct ieee80211_sta_ht_cap *ht_cap;
2918 	struct ieee80211_link_sta *link_sta;
2919 	struct ath12k *ar = arvif->ar;
2920 	int smps;
2921 
2922 	link_sta = ath12k_mac_get_link_sta(arsta);
2923 	if (!link_sta) {
2924 		ath12k_warn(ar->ab, "unable to access link sta in peer assoc he for sta %pM link %u\n",
2925 			    sta->addr, arsta->link_id);
2926 		return;
2927 	}
2928 
2929 	he_6ghz_capa = &link_sta->he_6ghz_capa;
2930 	ht_cap = &link_sta->ht_cap;
2931 
2932 	if (!ht_cap->ht_supported && !he_6ghz_capa->capa)
2933 		return;
2934 
2935 	if (ath12k_get_smps_from_capa(ht_cap, he_6ghz_capa, &smps))
2936 		return;
2937 
2938 	switch (smps) {
2939 	case WLAN_HT_CAP_SM_PS_STATIC:
2940 		arg->static_mimops_flag = true;
2941 		break;
2942 	case WLAN_HT_CAP_SM_PS_DYNAMIC:
2943 		arg->dynamic_mimops_flag = true;
2944 		break;
2945 	case WLAN_HT_CAP_SM_PS_DISABLED:
2946 		arg->spatial_mux_flag = true;
2947 		break;
2948 	default:
2949 		break;
2950 	}
2951 }
2952 
2953 static void ath12k_peer_assoc_h_qos(struct ath12k *ar,
2954 				    struct ath12k_link_vif *arvif,
2955 				    struct ath12k_link_sta *arsta,
2956 				    struct ath12k_wmi_peer_assoc_arg *arg)
2957 {
2958 	struct ieee80211_sta *sta = ath12k_ahsta_to_sta(arsta->ahsta);
2959 
2960 	switch (arvif->ahvif->vdev_type) {
2961 	case WMI_VDEV_TYPE_AP:
2962 		if (sta->wme) {
2963 			/* TODO: Check WME vs QoS */
2964 			arg->is_wme_set = true;
2965 			arg->qos_flag = true;
2966 		}
2967 
2968 		if (sta->wme && sta->uapsd_queues) {
2969 			/* TODO: Check WME vs QoS */
2970 			arg->is_wme_set = true;
2971 			arg->apsd_flag = true;
2972 			arg->peer_rate_caps |= WMI_HOST_RC_UAPSD_FLAG;
2973 		}
2974 		break;
2975 	case WMI_VDEV_TYPE_STA:
2976 		if (sta->wme) {
2977 			arg->is_wme_set = true;
2978 			arg->qos_flag = true;
2979 		}
2980 		break;
2981 	default:
2982 		break;
2983 	}
2984 
2985 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac peer %pM qos %d\n",
2986 		   arsta->addr, arg->qos_flag);
2987 }
2988 
2989 static int ath12k_peer_assoc_qos_ap(struct ath12k *ar,
2990 				    struct ath12k_link_vif *arvif,
2991 				    struct ath12k_link_sta *arsta)
2992 {
2993 	struct ieee80211_sta *sta = ath12k_ahsta_to_sta(arsta->ahsta);
2994 	struct ath12k_wmi_ap_ps_arg arg;
2995 	u32 max_sp;
2996 	u32 uapsd;
2997 	int ret;
2998 
2999 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
3000 
3001 	arg.vdev_id = arvif->vdev_id;
3002 
3003 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac uapsd_queues 0x%x max_sp %d\n",
3004 		   sta->uapsd_queues, sta->max_sp);
3005 
3006 	uapsd = 0;
3007 	if (sta->uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_VO)
3008 		uapsd |= WMI_AP_PS_UAPSD_AC3_DELIVERY_EN |
3009 			 WMI_AP_PS_UAPSD_AC3_TRIGGER_EN;
3010 	if (sta->uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_VI)
3011 		uapsd |= WMI_AP_PS_UAPSD_AC2_DELIVERY_EN |
3012 			 WMI_AP_PS_UAPSD_AC2_TRIGGER_EN;
3013 	if (sta->uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_BK)
3014 		uapsd |= WMI_AP_PS_UAPSD_AC1_DELIVERY_EN |
3015 			 WMI_AP_PS_UAPSD_AC1_TRIGGER_EN;
3016 	if (sta->uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_BE)
3017 		uapsd |= WMI_AP_PS_UAPSD_AC0_DELIVERY_EN |
3018 			 WMI_AP_PS_UAPSD_AC0_TRIGGER_EN;
3019 
3020 	max_sp = 0;
3021 	if (sta->max_sp < MAX_WMI_AP_PS_PEER_PARAM_MAX_SP)
3022 		max_sp = sta->max_sp;
3023 
3024 	arg.param = WMI_AP_PS_PEER_PARAM_UAPSD;
3025 	arg.value = uapsd;
3026 	ret = ath12k_wmi_send_set_ap_ps_param_cmd(ar, arsta->addr, &arg);
3027 	if (ret)
3028 		goto err;
3029 
3030 	arg.param = WMI_AP_PS_PEER_PARAM_MAX_SP;
3031 	arg.value = max_sp;
3032 	ret = ath12k_wmi_send_set_ap_ps_param_cmd(ar, arsta->addr, &arg);
3033 	if (ret)
3034 		goto err;
3035 
3036 	/* TODO: revisit during testing */
3037 	arg.param = WMI_AP_PS_PEER_PARAM_SIFS_RESP_FRMTYPE;
3038 	arg.value = DISABLE_SIFS_RESPONSE_TRIGGER;
3039 	ret = ath12k_wmi_send_set_ap_ps_param_cmd(ar, arsta->addr, &arg);
3040 	if (ret)
3041 		goto err;
3042 
3043 	arg.param = WMI_AP_PS_PEER_PARAM_SIFS_RESP_UAPSD;
3044 	arg.value = DISABLE_SIFS_RESPONSE_TRIGGER;
3045 	ret = ath12k_wmi_send_set_ap_ps_param_cmd(ar, arsta->addr, &arg);
3046 	if (ret)
3047 		goto err;
3048 
3049 	return 0;
3050 
3051 err:
3052 	ath12k_warn(ar->ab, "failed to set ap ps peer param %d for vdev %i: %d\n",
3053 		    arg.param, arvif->vdev_id, ret);
3054 	return ret;
3055 }
3056 
3057 static bool ath12k_mac_sta_has_ofdm_only(struct ieee80211_link_sta *sta)
3058 {
3059 	return sta->supp_rates[NL80211_BAND_2GHZ] >>
3060 	       ATH12K_MAC_FIRST_OFDM_RATE_IDX;
3061 }
3062 
3063 static enum wmi_phy_mode ath12k_mac_get_phymode_vht(struct ath12k *ar,
3064 						    struct ieee80211_link_sta *link_sta)
3065 {
3066 	if (link_sta->bandwidth == IEEE80211_STA_RX_BW_160) {
3067 		if (link_sta->vht_cap.cap & (IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_160MHZ |
3068 		    IEEE80211_VHT_CAP_EXT_NSS_BW_MASK))
3069 			return MODE_11AC_VHT160;
3070 
3071 		/* Allow STA to connect even if it does not explicitly advertise 160 MHz
3072 		 * support
3073 		 */
3074 		return MODE_11AC_VHT160;
3075 	}
3076 
3077 	if (link_sta->bandwidth == IEEE80211_STA_RX_BW_80)
3078 		return MODE_11AC_VHT80;
3079 
3080 	if (link_sta->bandwidth == IEEE80211_STA_RX_BW_40)
3081 		return MODE_11AC_VHT40;
3082 
3083 	if (link_sta->bandwidth == IEEE80211_STA_RX_BW_20)
3084 		return MODE_11AC_VHT20;
3085 
3086 	return MODE_UNKNOWN;
3087 }
3088 
3089 static enum wmi_phy_mode ath12k_mac_get_phymode_he(struct ath12k *ar,
3090 						   struct ieee80211_link_sta *link_sta)
3091 {
3092 	if (link_sta->bandwidth == IEEE80211_STA_RX_BW_160) {
3093 		if (link_sta->he_cap.he_cap_elem.phy_cap_info[0] &
3094 		     IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_160MHZ_IN_5G)
3095 			return MODE_11AX_HE160;
3096 
3097 		return MODE_UNKNOWN;
3098 	}
3099 
3100 	if (link_sta->bandwidth == IEEE80211_STA_RX_BW_80)
3101 		return MODE_11AX_HE80;
3102 
3103 	if (link_sta->bandwidth == IEEE80211_STA_RX_BW_40)
3104 		return MODE_11AX_HE40;
3105 
3106 	if (link_sta->bandwidth == IEEE80211_STA_RX_BW_20)
3107 		return MODE_11AX_HE20;
3108 
3109 	return MODE_UNKNOWN;
3110 }
3111 
3112 static enum wmi_phy_mode ath12k_mac_get_phymode_eht(struct ath12k *ar,
3113 						    struct ieee80211_link_sta *link_sta)
3114 {
3115 	if (link_sta->bandwidth == IEEE80211_STA_RX_BW_320)
3116 		if (link_sta->eht_cap.eht_cap_elem.phy_cap_info[0] &
3117 		    IEEE80211_EHT_PHY_CAP0_320MHZ_IN_6GHZ)
3118 			return MODE_11BE_EHT320;
3119 
3120 	if (link_sta->bandwidth == IEEE80211_STA_RX_BW_160) {
3121 		if (link_sta->he_cap.he_cap_elem.phy_cap_info[0] &
3122 		    IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_160MHZ_IN_5G)
3123 			return MODE_11BE_EHT160;
3124 
3125 		ath12k_warn(ar->ab, "invalid EHT PHY capability info for 160 Mhz: %d\n",
3126 			    link_sta->he_cap.he_cap_elem.phy_cap_info[0]);
3127 
3128 		return MODE_UNKNOWN;
3129 	}
3130 
3131 	if (link_sta->bandwidth == IEEE80211_STA_RX_BW_80)
3132 		return MODE_11BE_EHT80;
3133 
3134 	if (link_sta->bandwidth == IEEE80211_STA_RX_BW_40)
3135 		return MODE_11BE_EHT40;
3136 
3137 	if (link_sta->bandwidth == IEEE80211_STA_RX_BW_20)
3138 		return MODE_11BE_EHT20;
3139 
3140 	return MODE_UNKNOWN;
3141 }
3142 
3143 static bool
3144 ath12k_peer_assoc_h_eht_masked(const u16 eht_mcs_mask[NL80211_EHT_NSS_MAX])
3145 {
3146 	int nss;
3147 
3148 	for (nss = 0; nss < NL80211_EHT_NSS_MAX; nss++)
3149 		if (eht_mcs_mask[nss])
3150 			return false;
3151 
3152 	return true;
3153 }
3154 
3155 static void ath12k_peer_assoc_h_phymode(struct ath12k *ar,
3156 					struct ath12k_link_vif *arvif,
3157 					struct ath12k_link_sta *arsta,
3158 					struct ath12k_wmi_peer_assoc_arg *arg)
3159 {
3160 	struct ieee80211_link_sta *link_sta;
3161 	struct cfg80211_chan_def def;
3162 	enum nl80211_band band;
3163 	const u8 *ht_mcs_mask;
3164 	const u16 *vht_mcs_mask;
3165 	const u16 *he_mcs_mask;
3166 	const u16 *eht_mcs_mask;
3167 	enum wmi_phy_mode phymode = MODE_UNKNOWN;
3168 
3169 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
3170 
3171 	struct ieee80211_vif *vif = ath12k_ahvif_to_vif(arvif->ahvif);
3172 	struct ieee80211_sta *sta = ath12k_ahsta_to_sta(arsta->ahsta);
3173 
3174 	if (WARN_ON(ath12k_mac_vif_link_chan(vif, arvif->link_id, &def)))
3175 		return;
3176 
3177 	band = def.chan->band;
3178 	ht_mcs_mask = arvif->bitrate_mask.control[band].ht_mcs;
3179 	vht_mcs_mask = arvif->bitrate_mask.control[band].vht_mcs;
3180 	he_mcs_mask = arvif->bitrate_mask.control[band].he_mcs;
3181 	eht_mcs_mask = arvif->bitrate_mask.control[band].eht_mcs;
3182 
3183 	link_sta = ath12k_mac_get_link_sta(arsta);
3184 	if (!link_sta) {
3185 		ath12k_warn(ar->ab, "unable to access link sta in peer assoc he for sta %pM link %u\n",
3186 			    sta->addr, arsta->link_id);
3187 		return;
3188 	}
3189 
3190 	switch (band) {
3191 	case NL80211_BAND_2GHZ:
3192 		if (link_sta->eht_cap.has_eht &&
3193 		    !ath12k_peer_assoc_h_eht_masked(eht_mcs_mask)) {
3194 			if (link_sta->bandwidth == IEEE80211_STA_RX_BW_40)
3195 				phymode = MODE_11BE_EHT40_2G;
3196 			else
3197 				phymode = MODE_11BE_EHT20_2G;
3198 		} else if (link_sta->he_cap.has_he &&
3199 			   !ath12k_peer_assoc_h_he_masked(he_mcs_mask)) {
3200 			if (link_sta->bandwidth == IEEE80211_STA_RX_BW_80)
3201 				phymode = MODE_11AX_HE80_2G;
3202 			else if (link_sta->bandwidth == IEEE80211_STA_RX_BW_40)
3203 				phymode = MODE_11AX_HE40_2G;
3204 			else
3205 				phymode = MODE_11AX_HE20_2G;
3206 		} else if (link_sta->vht_cap.vht_supported &&
3207 		    !ath12k_peer_assoc_h_vht_masked(vht_mcs_mask)) {
3208 			if (link_sta->bandwidth == IEEE80211_STA_RX_BW_40)
3209 				phymode = MODE_11AC_VHT40;
3210 			else
3211 				phymode = MODE_11AC_VHT20;
3212 		} else if (link_sta->ht_cap.ht_supported &&
3213 			   !ath12k_peer_assoc_h_ht_masked(ht_mcs_mask)) {
3214 			if (link_sta->bandwidth == IEEE80211_STA_RX_BW_40)
3215 				phymode = MODE_11NG_HT40;
3216 			else
3217 				phymode = MODE_11NG_HT20;
3218 		} else if (ath12k_mac_sta_has_ofdm_only(link_sta)) {
3219 			phymode = MODE_11G;
3220 		} else {
3221 			phymode = MODE_11B;
3222 		}
3223 		break;
3224 	case NL80211_BAND_5GHZ:
3225 	case NL80211_BAND_6GHZ:
3226 		/* Check EHT first */
3227 		if (link_sta->eht_cap.has_eht) {
3228 			phymode = ath12k_mac_get_phymode_eht(ar, link_sta);
3229 		} else if (link_sta->he_cap.has_he &&
3230 			   !ath12k_peer_assoc_h_he_masked(he_mcs_mask)) {
3231 			phymode = ath12k_mac_get_phymode_he(ar, link_sta);
3232 		} else if (link_sta->vht_cap.vht_supported &&
3233 		    !ath12k_peer_assoc_h_vht_masked(vht_mcs_mask)) {
3234 			phymode = ath12k_mac_get_phymode_vht(ar, link_sta);
3235 		} else if (link_sta->ht_cap.ht_supported &&
3236 			   !ath12k_peer_assoc_h_ht_masked(ht_mcs_mask)) {
3237 			if (link_sta->bandwidth >= IEEE80211_STA_RX_BW_40)
3238 				phymode = MODE_11NA_HT40;
3239 			else
3240 				phymode = MODE_11NA_HT20;
3241 		} else {
3242 			phymode = MODE_11A;
3243 		}
3244 		break;
3245 	default:
3246 		break;
3247 	}
3248 
3249 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac peer %pM phymode %s\n",
3250 		   arsta->addr, ath12k_mac_phymode_str(phymode));
3251 
3252 	arg->peer_phymode = phymode;
3253 	WARN_ON(phymode == MODE_UNKNOWN);
3254 }
3255 
3256 #define ATH12K_EHT_MCS_7_ENABLED	0x00FF
3257 #define ATH12K_EHT_MCS_9_ENABLED	0x0300
3258 #define ATH12K_EHT_MCS_11_ENABLED	0x0C00
3259 #define ATH12K_EHT_MCS_13_ENABLED	0x3000
3260 
3261 static void ath12k_mac_set_eht_mcs(u8 rx_tx_mcs7, u8 rx_tx_mcs9,
3262 				   u8 rx_tx_mcs11, u8 rx_tx_mcs13,
3263 				   u32 *rx_mcs, u32 *tx_mcs,
3264 				   const u16 eht_mcs_limit[NL80211_EHT_NSS_MAX])
3265 {
3266 	int nss;
3267 	u8 mcs_7 = 0, mcs_9 = 0, mcs_11 = 0, mcs_13 = 0;
3268 	u8 peer_mcs_7, peer_mcs_9, peer_mcs_11, peer_mcs_13;
3269 
3270 	for (nss = 0; nss < NL80211_EHT_NSS_MAX; nss++) {
3271 		if (eht_mcs_limit[nss] & ATH12K_EHT_MCS_7_ENABLED)
3272 			mcs_7++;
3273 		if (eht_mcs_limit[nss] & ATH12K_EHT_MCS_9_ENABLED)
3274 			mcs_9++;
3275 		if (eht_mcs_limit[nss] & ATH12K_EHT_MCS_11_ENABLED)
3276 			mcs_11++;
3277 		if (eht_mcs_limit[nss] & ATH12K_EHT_MCS_13_ENABLED)
3278 			mcs_13++;
3279 	}
3280 
3281 	peer_mcs_7 = u8_get_bits(rx_tx_mcs7, IEEE80211_EHT_MCS_NSS_RX);
3282 	peer_mcs_9 = u8_get_bits(rx_tx_mcs9, IEEE80211_EHT_MCS_NSS_RX);
3283 	peer_mcs_11 = u8_get_bits(rx_tx_mcs11, IEEE80211_EHT_MCS_NSS_RX);
3284 	peer_mcs_13 = u8_get_bits(rx_tx_mcs13, IEEE80211_EHT_MCS_NSS_RX);
3285 
3286 	*rx_mcs = u32_encode_bits(min(peer_mcs_7, mcs_7), WMI_EHT_MCS_NSS_0_7) |
3287 		  u32_encode_bits(min(peer_mcs_9, mcs_9), WMI_EHT_MCS_NSS_8_9) |
3288 		  u32_encode_bits(min(peer_mcs_11, mcs_11), WMI_EHT_MCS_NSS_10_11) |
3289 		  u32_encode_bits(min(peer_mcs_13, mcs_13), WMI_EHT_MCS_NSS_12_13);
3290 
3291 	peer_mcs_7 = u8_get_bits(rx_tx_mcs7, IEEE80211_EHT_MCS_NSS_TX);
3292 	peer_mcs_9 = u8_get_bits(rx_tx_mcs9, IEEE80211_EHT_MCS_NSS_TX);
3293 	peer_mcs_11 = u8_get_bits(rx_tx_mcs11, IEEE80211_EHT_MCS_NSS_TX);
3294 	peer_mcs_13 = u8_get_bits(rx_tx_mcs13, IEEE80211_EHT_MCS_NSS_TX);
3295 
3296 	*tx_mcs = u32_encode_bits(min(peer_mcs_7, mcs_7), WMI_EHT_MCS_NSS_0_7) |
3297 		  u32_encode_bits(min(peer_mcs_9, mcs_9), WMI_EHT_MCS_NSS_8_9) |
3298 		  u32_encode_bits(min(peer_mcs_11, mcs_11), WMI_EHT_MCS_NSS_10_11) |
3299 		  u32_encode_bits(min(peer_mcs_13, mcs_13), WMI_EHT_MCS_NSS_12_13);
3300 }
3301 
3302 static void ath12k_mac_set_eht_ppe_threshold(const u8 *ppe_thres,
3303 					     struct ath12k_wmi_ppe_threshold_arg *ppet)
3304 {
3305 	u32 bit_pos = IEEE80211_EHT_PPE_THRES_INFO_HEADER_SIZE, val;
3306 	u8 nss, ru, i;
3307 	u8 ppet_bit_len_per_ru = IEEE80211_EHT_PPE_THRES_INFO_PPET_SIZE * 2;
3308 
3309 	ppet->numss_m1 = u8_get_bits(ppe_thres[0], IEEE80211_EHT_PPE_THRES_NSS_MASK);
3310 	ppet->ru_bit_mask = u16_get_bits(get_unaligned_le16(ppe_thres),
3311 					 IEEE80211_EHT_PPE_THRES_RU_INDEX_BITMASK_MASK);
3312 
3313 	for (nss = 0; nss <= ppet->numss_m1; nss++) {
3314 		for (ru = 0;
3315 		     ru < hweight16(IEEE80211_EHT_PPE_THRES_RU_INDEX_BITMASK_MASK);
3316 		     ru++) {
3317 			if ((ppet->ru_bit_mask & BIT(ru)) == 0)
3318 				continue;
3319 
3320 			val = 0;
3321 			for (i = 0; i < ppet_bit_len_per_ru; i++) {
3322 				val |= (((ppe_thres[bit_pos / 8] >>
3323 					  (bit_pos % 8)) & 0x1) << i);
3324 				bit_pos++;
3325 			}
3326 			ppet->ppet16_ppet8_ru3_ru0[nss] |=
3327 					(val << (ru * ppet_bit_len_per_ru));
3328 		}
3329 	}
3330 }
3331 
3332 static void ath12k_peer_assoc_h_eht(struct ath12k *ar,
3333 				    struct ath12k_link_vif *arvif,
3334 				    struct ath12k_link_sta *arsta,
3335 				    struct ath12k_wmi_peer_assoc_arg *arg)
3336 {
3337 	struct ieee80211_sta *sta = ath12k_ahsta_to_sta(arsta->ahsta);
3338 	struct ieee80211_vif *vif = ath12k_ahvif_to_vif(arvif->ahvif);
3339 	const struct ieee80211_eht_mcs_nss_supp *own_eht_mcs_nss_supp;
3340 	const struct ieee80211_eht_mcs_nss_supp_20mhz_only *bw_20;
3341 	const struct ieee80211_sta_eht_cap *eht_cap, *own_eht_cap;
3342 	const struct ieee80211_sband_iftype_data *iftd;
3343 	const struct ieee80211_eht_mcs_nss_supp_bw *bw;
3344 	const struct ieee80211_sta_he_cap *he_cap;
3345 	struct ieee80211_link_sta *link_sta;
3346 	struct ieee80211_bss_conf *link_conf;
3347 	struct cfg80211_chan_def def;
3348 	bool user_rate_valid = true;
3349 	enum nl80211_band band;
3350 	int eht_nss, nss_idx;
3351 	u32 *rx_mcs, *tx_mcs;
3352 	u16 *eht_mcs_mask;
3353 	u8 max_nss = 0;
3354 
3355 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
3356 
3357 	link_sta = ath12k_mac_get_link_sta(arsta);
3358 	if (!link_sta) {
3359 		ath12k_warn(ar->ab, "unable to access link sta in peer assoc eht for sta %pM link %u\n",
3360 			    sta->addr, arsta->link_id);
3361 		return;
3362 	}
3363 
3364 	link_conf = ath12k_mac_get_link_bss_conf(arvif);
3365 	if (!link_conf) {
3366 		ath12k_warn(ar->ab, "unable to access link_conf in peer assoc eht set\n");
3367 		return;
3368 	}
3369 
3370 	eht_cap = &link_sta->eht_cap;
3371 	he_cap = &link_sta->he_cap;
3372 	if (!he_cap->has_he || !eht_cap->has_eht)
3373 		return;
3374 
3375 	if (WARN_ON(ath12k_mac_vif_link_chan(vif, arvif->link_id, &def)))
3376 		return;
3377 
3378 	band = def.chan->band;
3379 	eht_mcs_mask = arvif->bitrate_mask.control[band].eht_mcs;
3380 
3381 	iftd = ieee80211_get_sband_iftype_data(&ar->mac.sbands[band], vif->type);
3382 	if (!iftd) {
3383 		ath12k_warn(ar->ab,
3384 			    "unable to access iftype_data in struct ieee80211_supported_band\n");
3385 		return;
3386 	}
3387 
3388 	own_eht_cap = &iftd->eht_cap;
3389 	own_eht_mcs_nss_supp = &own_eht_cap->eht_mcs_nss_supp;
3390 
3391 	arg->eht_flag = true;
3392 
3393 	if ((eht_cap->eht_cap_elem.phy_cap_info[5] &
3394 	     IEEE80211_EHT_PHY_CAP5_PPE_THRESHOLD_PRESENT) &&
3395 	    eht_cap->eht_ppe_thres[0] != 0)
3396 		ath12k_mac_set_eht_ppe_threshold(eht_cap->eht_ppe_thres,
3397 						 &arg->peer_eht_ppet);
3398 
3399 	memcpy(arg->peer_eht_cap_mac, eht_cap->eht_cap_elem.mac_cap_info,
3400 	       sizeof(eht_cap->eht_cap_elem.mac_cap_info));
3401 	memcpy(arg->peer_eht_cap_phy, eht_cap->eht_cap_elem.phy_cap_info,
3402 	       sizeof(eht_cap->eht_cap_elem.phy_cap_info));
3403 
3404 	rx_mcs = arg->peer_eht_rx_mcs_set;
3405 	tx_mcs = arg->peer_eht_tx_mcs_set;
3406 
3407 	eht_nss = ath12k_mac_max_eht_mcs_nss((void *)own_eht_mcs_nss_supp,
3408 					     sizeof(*own_eht_mcs_nss_supp));
3409 	if (eht_nss > link_sta->rx_nss) {
3410 		user_rate_valid = false;
3411 		for (nss_idx = (link_sta->rx_nss - 1); nss_idx >= 0; nss_idx--) {
3412 			if (eht_mcs_mask[nss_idx]) {
3413 				user_rate_valid = true;
3414 				break;
3415 			}
3416 		}
3417 	}
3418 
3419 	if (!user_rate_valid) {
3420 		ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
3421 			   "Setting eht range MCS value to peer supported nss %d for peer %pM\n",
3422 			   link_sta->rx_nss, arsta->addr);
3423 		eht_mcs_mask[link_sta->rx_nss - 1] = eht_mcs_mask[eht_nss - 1];
3424 	}
3425 
3426 	bw_20 = &eht_cap->eht_mcs_nss_supp.only_20mhz;
3427 	bw = &eht_cap->eht_mcs_nss_supp.bw._80;
3428 
3429 	switch (link_sta->bandwidth) {
3430 	case IEEE80211_STA_RX_BW_320:
3431 		bw = &eht_cap->eht_mcs_nss_supp.bw._320;
3432 		ath12k_mac_set_eht_mcs(bw->rx_tx_mcs9_max_nss,
3433 				       bw->rx_tx_mcs9_max_nss,
3434 				       bw->rx_tx_mcs11_max_nss,
3435 				       bw->rx_tx_mcs13_max_nss,
3436 				       &rx_mcs[WMI_EHTCAP_TXRX_MCS_NSS_IDX_320],
3437 				       &tx_mcs[WMI_EHTCAP_TXRX_MCS_NSS_IDX_320],
3438 				       eht_mcs_mask);
3439 		arg->peer_eht_mcs_count++;
3440 		fallthrough;
3441 	case IEEE80211_STA_RX_BW_160:
3442 		bw = &eht_cap->eht_mcs_nss_supp.bw._160;
3443 		ath12k_mac_set_eht_mcs(bw->rx_tx_mcs9_max_nss,
3444 				       bw->rx_tx_mcs9_max_nss,
3445 				       bw->rx_tx_mcs11_max_nss,
3446 				       bw->rx_tx_mcs13_max_nss,
3447 				       &rx_mcs[WMI_EHTCAP_TXRX_MCS_NSS_IDX_160],
3448 				       &tx_mcs[WMI_EHTCAP_TXRX_MCS_NSS_IDX_160],
3449 				       eht_mcs_mask);
3450 		arg->peer_eht_mcs_count++;
3451 		fallthrough;
3452 	default:
3453 		if ((vif->type == NL80211_IFTYPE_AP ||
3454 		     vif->type == NL80211_IFTYPE_MESH_POINT) &&
3455 		    !(link_sta->he_cap.he_cap_elem.phy_cap_info[0] &
3456 		      IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_MASK_ALL)) {
3457 			bw_20 = &eht_cap->eht_mcs_nss_supp.only_20mhz;
3458 
3459 			ath12k_mac_set_eht_mcs(bw_20->rx_tx_mcs7_max_nss,
3460 					       bw_20->rx_tx_mcs9_max_nss,
3461 					       bw_20->rx_tx_mcs11_max_nss,
3462 					       bw_20->rx_tx_mcs13_max_nss,
3463 					       &rx_mcs[WMI_EHTCAP_TXRX_MCS_NSS_IDX_80],
3464 					       &tx_mcs[WMI_EHTCAP_TXRX_MCS_NSS_IDX_80],
3465 					       eht_mcs_mask);
3466 		} else {
3467 			bw = &eht_cap->eht_mcs_nss_supp.bw._80;
3468 			ath12k_mac_set_eht_mcs(bw->rx_tx_mcs9_max_nss,
3469 					       bw->rx_tx_mcs9_max_nss,
3470 					       bw->rx_tx_mcs11_max_nss,
3471 					       bw->rx_tx_mcs13_max_nss,
3472 					       &rx_mcs[WMI_EHTCAP_TXRX_MCS_NSS_IDX_80],
3473 					       &tx_mcs[WMI_EHTCAP_TXRX_MCS_NSS_IDX_80],
3474 					       eht_mcs_mask);
3475 		}
3476 
3477 		arg->peer_eht_mcs_count++;
3478 		break;
3479 	}
3480 
3481 	arg->punct_bitmap = ~arvif->punct_bitmap;
3482 	arg->eht_disable_mcs15 = link_conf->eht_disable_mcs15;
3483 
3484 	if ((vif->type == NL80211_IFTYPE_AP ||
3485 	     vif->type == NL80211_IFTYPE_MESH_POINT) &&
3486 	    !(link_sta->he_cap.he_cap_elem.phy_cap_info[0] &
3487 	      IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_MASK_ALL)) {
3488 		if (bw_20->rx_tx_mcs13_max_nss)
3489 			max_nss = max(max_nss, u8_get_bits(bw_20->rx_tx_mcs13_max_nss,
3490 							   IEEE80211_EHT_MCS_NSS_RX));
3491 		if (bw_20->rx_tx_mcs11_max_nss)
3492 			max_nss = max(max_nss, u8_get_bits(bw_20->rx_tx_mcs11_max_nss,
3493 							   IEEE80211_EHT_MCS_NSS_RX));
3494 		if (bw_20->rx_tx_mcs9_max_nss)
3495 			max_nss = max(max_nss, u8_get_bits(bw_20->rx_tx_mcs9_max_nss,
3496 							   IEEE80211_EHT_MCS_NSS_RX));
3497 		if (bw_20->rx_tx_mcs7_max_nss)
3498 			max_nss = max(max_nss, u8_get_bits(bw_20->rx_tx_mcs7_max_nss,
3499 							   IEEE80211_EHT_MCS_NSS_RX));
3500 	} else {
3501 		if (bw->rx_tx_mcs13_max_nss)
3502 			max_nss = max(max_nss, u8_get_bits(bw->rx_tx_mcs13_max_nss,
3503 							   IEEE80211_EHT_MCS_NSS_RX));
3504 		if (bw->rx_tx_mcs11_max_nss)
3505 			max_nss = max(max_nss, u8_get_bits(bw->rx_tx_mcs11_max_nss,
3506 							   IEEE80211_EHT_MCS_NSS_RX));
3507 		if (bw->rx_tx_mcs9_max_nss)
3508 			max_nss = max(max_nss, u8_get_bits(bw->rx_tx_mcs9_max_nss,
3509 							   IEEE80211_EHT_MCS_NSS_RX));
3510 	}
3511 
3512 	max_nss = min(max_nss, (uint8_t)eht_nss);
3513 
3514 	arg->peer_nss = min(link_sta->rx_nss, max_nss);
3515 
3516 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
3517 		   "mac eht peer %pM nss %d mcs cnt %d ru_punct_bitmap 0x%x\n",
3518 		   arsta->addr, arg->peer_nss, arg->peer_eht_mcs_count,
3519 		   arg->punct_bitmap);
3520 }
3521 
3522 static void ath12k_peer_assoc_h_mlo(struct ath12k_link_sta *arsta,
3523 				    struct ath12k_wmi_peer_assoc_arg *arg)
3524 {
3525 	struct ieee80211_sta *sta = ath12k_ahsta_to_sta(arsta->ahsta);
3526 	struct peer_assoc_mlo_params *ml = &arg->ml;
3527 	struct ath12k_sta *ahsta = arsta->ahsta;
3528 	struct ath12k_link_sta *arsta_p;
3529 	struct ath12k_link_vif *arvif;
3530 	unsigned long links;
3531 	u8 link_id;
3532 	int i;
3533 
3534 	if (!sta->mlo || ahsta->ml_peer_id == ATH12K_MLO_PEER_ID_INVALID)
3535 		return;
3536 
3537 	ml->enabled = true;
3538 	ml->assoc_link = arsta->is_assoc_link;
3539 
3540 	/* For now considering the primary umac based on assoc link */
3541 	ml->primary_umac = arsta->is_assoc_link;
3542 	ml->peer_id_valid = true;
3543 	ml->logical_link_idx_valid = true;
3544 
3545 	ether_addr_copy(ml->mld_addr, sta->addr);
3546 	ml->logical_link_idx = arsta->link_idx;
3547 	ml->ml_peer_id = ahsta->ml_peer_id;
3548 	ml->ieee_link_id = arsta->link_id;
3549 	ml->num_partner_links = 0;
3550 	ml->eml_cap = sta->eml_cap;
3551 	links = ahsta->links_map;
3552 
3553 	rcu_read_lock();
3554 
3555 	i = 0;
3556 
3557 	for_each_set_bit(link_id, &links, IEEE80211_MLD_MAX_NUM_LINKS) {
3558 		if (i >= ATH12K_WMI_MLO_MAX_LINKS)
3559 			break;
3560 
3561 		arsta_p = rcu_dereference(ahsta->link[link_id]);
3562 		arvif = rcu_dereference(ahsta->ahvif->link[link_id]);
3563 
3564 		if (arsta_p == arsta)
3565 			continue;
3566 
3567 		if (!arvif->is_started)
3568 			continue;
3569 
3570 		ml->partner_info[i].vdev_id = arvif->vdev_id;
3571 		ml->partner_info[i].hw_link_id = arvif->ar->pdev->hw_link_id;
3572 		ml->partner_info[i].assoc_link = arsta_p->is_assoc_link;
3573 		ml->partner_info[i].primary_umac = arsta_p->is_assoc_link;
3574 		ml->partner_info[i].logical_link_idx_valid = true;
3575 		ml->partner_info[i].logical_link_idx = arsta_p->link_idx;
3576 		ml->num_partner_links++;
3577 
3578 		i++;
3579 	}
3580 
3581 	rcu_read_unlock();
3582 }
3583 
3584 static void ath12k_peer_assoc_prepare(struct ath12k *ar,
3585 				      struct ath12k_link_vif *arvif,
3586 				      struct ath12k_link_sta *arsta,
3587 				      struct ath12k_wmi_peer_assoc_arg *arg,
3588 				      bool reassoc)
3589 {
3590 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
3591 
3592 	memset(arg, 0, sizeof(*arg));
3593 
3594 	reinit_completion(&ar->peer_assoc_done);
3595 
3596 	arg->peer_new_assoc = !reassoc;
3597 	ath12k_peer_assoc_h_basic(ar, arvif, arsta, arg);
3598 	ath12k_peer_assoc_h_crypto(ar, arvif, arsta, arg);
3599 	ath12k_peer_assoc_h_rates(ar, arvif, arsta, arg);
3600 	ath12k_peer_assoc_h_ht(ar, arvif, arsta, arg);
3601 	ath12k_peer_assoc_h_vht(ar, arvif, arsta, arg);
3602 	ath12k_peer_assoc_h_he(ar, arvif, arsta, arg);
3603 	ath12k_peer_assoc_h_he_6ghz(ar, arvif, arsta, arg);
3604 	ath12k_peer_assoc_h_eht(ar, arvif, arsta, arg);
3605 	ath12k_peer_assoc_h_qos(ar, arvif, arsta, arg);
3606 	ath12k_peer_assoc_h_phymode(ar, arvif, arsta, arg);
3607 	ath12k_peer_assoc_h_smps(arsta, arg);
3608 	ath12k_peer_assoc_h_mlo(arsta, arg);
3609 
3610 	arsta->peer_nss = arg->peer_nss;
3611 	/* TODO: amsdu_disable req? */
3612 }
3613 
3614 static int ath12k_setup_peer_smps(struct ath12k *ar, struct ath12k_link_vif *arvif,
3615 				  const u8 *addr,
3616 				  const struct ieee80211_sta_ht_cap *ht_cap,
3617 				  const struct ieee80211_he_6ghz_capa *he_6ghz_capa)
3618 {
3619 	int smps, ret = 0;
3620 
3621 	if (!ht_cap->ht_supported && !he_6ghz_capa)
3622 		return 0;
3623 
3624 	ret = ath12k_get_smps_from_capa(ht_cap, he_6ghz_capa, &smps);
3625 	if (ret < 0)
3626 		return ret;
3627 
3628 	return ath12k_wmi_set_peer_param(ar, addr, arvif->vdev_id,
3629 					 WMI_PEER_MIMO_PS_STATE,
3630 					 ath12k_smps_map[smps]);
3631 }
3632 
3633 static int ath12k_mac_set_he_txbf_conf(struct ath12k_link_vif *arvif)
3634 {
3635 	struct ath12k_vif *ahvif = arvif->ahvif;
3636 	struct ath12k *ar = arvif->ar;
3637 	u32 param = WMI_VDEV_PARAM_SET_HEMU_MODE;
3638 	u32 value = 0;
3639 	int ret;
3640 	struct ieee80211_bss_conf *link_conf;
3641 
3642 	link_conf = ath12k_mac_get_link_bss_conf(arvif);
3643 	if (!link_conf) {
3644 		ath12k_warn(ar->ab, "unable to access bss link conf in txbf conf\n");
3645 		return -EINVAL;
3646 	}
3647 
3648 	if (!link_conf->he_support)
3649 		return 0;
3650 
3651 	if (link_conf->he_su_beamformer) {
3652 		value |= u32_encode_bits(HE_SU_BFER_ENABLE, HE_MODE_SU_TX_BFER);
3653 		if (link_conf->he_mu_beamformer &&
3654 		    ahvif->vdev_type == WMI_VDEV_TYPE_AP)
3655 			value |= u32_encode_bits(HE_MU_BFER_ENABLE, HE_MODE_MU_TX_BFER);
3656 	}
3657 
3658 	if (ahvif->vif->type != NL80211_IFTYPE_MESH_POINT) {
3659 		value |= u32_encode_bits(HE_DL_MUOFDMA_ENABLE, HE_MODE_DL_OFDMA) |
3660 			 u32_encode_bits(HE_UL_MUOFDMA_ENABLE, HE_MODE_UL_OFDMA);
3661 
3662 		if (link_conf->he_full_ul_mumimo)
3663 			value |= u32_encode_bits(HE_UL_MUMIMO_ENABLE, HE_MODE_UL_MUMIMO);
3664 
3665 		if (link_conf->he_su_beamformee)
3666 			value |= u32_encode_bits(HE_SU_BFEE_ENABLE, HE_MODE_SU_TX_BFEE);
3667 	}
3668 
3669 	ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id, param, value);
3670 	if (ret) {
3671 		ath12k_warn(ar->ab, "failed to set vdev %d HE MU mode: %d\n",
3672 			    arvif->vdev_id, ret);
3673 		return ret;
3674 	}
3675 
3676 	param = WMI_VDEV_PARAM_SET_HE_SOUNDING_MODE;
3677 	value =	u32_encode_bits(HE_VHT_SOUNDING_MODE_ENABLE, HE_VHT_SOUNDING_MODE) |
3678 		u32_encode_bits(HE_TRIG_NONTRIG_SOUNDING_MODE_ENABLE,
3679 				HE_TRIG_NONTRIG_SOUNDING_MODE);
3680 	ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
3681 					    param, value);
3682 	if (ret) {
3683 		ath12k_warn(ar->ab, "failed to set vdev %d sounding mode: %d\n",
3684 			    arvif->vdev_id, ret);
3685 		return ret;
3686 	}
3687 
3688 	return 0;
3689 }
3690 
3691 static int ath12k_mac_vif_recalc_sta_he_txbf(struct ath12k *ar,
3692 					     struct ath12k_link_vif *arvif,
3693 					     struct ieee80211_sta_he_cap *he_cap,
3694 					     int *hemode)
3695 {
3696 	struct ieee80211_vif *vif = arvif->ahvif->vif;
3697 	struct ieee80211_he_cap_elem he_cap_elem = {};
3698 	struct ieee80211_sta_he_cap *cap_band;
3699 	struct cfg80211_chan_def def;
3700 	u8 link_id = arvif->link_id;
3701 	struct ieee80211_bss_conf *link_conf;
3702 
3703 	link_conf = ath12k_mac_get_link_bss_conf(arvif);
3704 	if (!link_conf) {
3705 		ath12k_warn(ar->ab, "unable to access bss link conf in recalc txbf conf\n");
3706 		return -EINVAL;
3707 	}
3708 
3709 	if (!link_conf->he_support)
3710 		return 0;
3711 
3712 	if (vif->type != NL80211_IFTYPE_STATION)
3713 		return -EINVAL;
3714 
3715 	if (WARN_ON(ath12k_mac_vif_link_chan(vif, link_id, &def)))
3716 		return -EINVAL;
3717 
3718 	if (def.chan->band == NL80211_BAND_2GHZ)
3719 		cap_band = &ar->mac.iftype[NL80211_BAND_2GHZ][vif->type].he_cap;
3720 	else
3721 		cap_band = &ar->mac.iftype[NL80211_BAND_5GHZ][vif->type].he_cap;
3722 
3723 	memcpy(&he_cap_elem, &cap_band->he_cap_elem, sizeof(he_cap_elem));
3724 
3725 	*hemode = 0;
3726 	if (HECAP_PHY_SUBFME_GET(he_cap_elem.phy_cap_info)) {
3727 		if (HECAP_PHY_SUBFMR_GET(he_cap->he_cap_elem.phy_cap_info))
3728 			*hemode |= u32_encode_bits(HE_SU_BFEE_ENABLE, HE_MODE_SU_TX_BFEE);
3729 		if (HECAP_PHY_MUBFMR_GET(he_cap->he_cap_elem.phy_cap_info))
3730 			*hemode |= u32_encode_bits(HE_MU_BFEE_ENABLE, HE_MODE_MU_TX_BFEE);
3731 	}
3732 
3733 	if (vif->type != NL80211_IFTYPE_MESH_POINT) {
3734 		*hemode |= u32_encode_bits(HE_DL_MUOFDMA_ENABLE, HE_MODE_DL_OFDMA) |
3735 			  u32_encode_bits(HE_UL_MUOFDMA_ENABLE, HE_MODE_UL_OFDMA);
3736 
3737 		if (HECAP_PHY_ULMUMIMO_GET(he_cap_elem.phy_cap_info))
3738 			if (HECAP_PHY_ULMUMIMO_GET(he_cap->he_cap_elem.phy_cap_info))
3739 				*hemode |= u32_encode_bits(HE_UL_MUMIMO_ENABLE,
3740 							  HE_MODE_UL_MUMIMO);
3741 
3742 		if (u32_get_bits(*hemode, HE_MODE_MU_TX_BFEE))
3743 			*hemode |= u32_encode_bits(HE_SU_BFEE_ENABLE, HE_MODE_SU_TX_BFEE);
3744 
3745 		if (u32_get_bits(*hemode, HE_MODE_MU_TX_BFER))
3746 			*hemode |= u32_encode_bits(HE_SU_BFER_ENABLE, HE_MODE_SU_TX_BFER);
3747 	}
3748 
3749 	return 0;
3750 }
3751 
3752 static int ath12k_mac_set_eht_txbf_conf(struct ath12k_link_vif *arvif)
3753 {
3754 	struct ath12k_vif *ahvif = arvif->ahvif;
3755 	struct ath12k *ar = arvif->ar;
3756 	u32 param = WMI_VDEV_PARAM_SET_EHT_MU_MODE;
3757 	u32 value = 0;
3758 	int ret;
3759 	struct ieee80211_bss_conf *link_conf;
3760 
3761 	link_conf = ath12k_mac_get_link_bss_conf(arvif);
3762 	if (!link_conf) {
3763 		ath12k_warn(ar->ab, "unable to access bss link conf in eht txbf conf\n");
3764 		return -ENOENT;
3765 	}
3766 
3767 	if (!link_conf->eht_support)
3768 		return 0;
3769 
3770 	if (link_conf->eht_su_beamformer) {
3771 		value |= u32_encode_bits(EHT_SU_BFER_ENABLE, EHT_MODE_SU_TX_BFER);
3772 		if (link_conf->eht_mu_beamformer &&
3773 		    ahvif->vdev_type == WMI_VDEV_TYPE_AP)
3774 			value |= u32_encode_bits(EHT_MU_BFER_ENABLE,
3775 						 EHT_MODE_MU_TX_BFER) |
3776 				 u32_encode_bits(EHT_DL_MUOFDMA_ENABLE,
3777 						 EHT_MODE_DL_OFDMA_MUMIMO) |
3778 				 u32_encode_bits(EHT_UL_MUOFDMA_ENABLE,
3779 						 EHT_MODE_UL_OFDMA_MUMIMO);
3780 	}
3781 
3782 	if (ahvif->vif->type != NL80211_IFTYPE_MESH_POINT) {
3783 		value |= u32_encode_bits(EHT_DL_MUOFDMA_ENABLE, EHT_MODE_DL_OFDMA) |
3784 			 u32_encode_bits(EHT_UL_MUOFDMA_ENABLE, EHT_MODE_UL_OFDMA);
3785 
3786 		if (link_conf->eht_80mhz_full_bw_ul_mumimo)
3787 			value |= u32_encode_bits(EHT_UL_MUMIMO_ENABLE, EHT_MODE_MUMIMO);
3788 
3789 		if (link_conf->eht_su_beamformee)
3790 			value |= u32_encode_bits(EHT_SU_BFEE_ENABLE,
3791 						 EHT_MODE_SU_TX_BFEE);
3792 	}
3793 
3794 	ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id, param, value);
3795 	if (ret) {
3796 		ath12k_warn(ar->ab, "failed to set vdev %d EHT MU mode: %d\n",
3797 			    arvif->vdev_id, ret);
3798 		return ret;
3799 	}
3800 
3801 	return 0;
3802 }
3803 
3804 static u32 ath12k_mac_ieee80211_sta_bw_to_wmi(struct ath12k *ar,
3805 					      struct ieee80211_link_sta *link_sta)
3806 {
3807 	u32 bw;
3808 
3809 	switch (link_sta->bandwidth) {
3810 	case IEEE80211_STA_RX_BW_20:
3811 		bw = WMI_PEER_CHWIDTH_20MHZ;
3812 		break;
3813 	case IEEE80211_STA_RX_BW_40:
3814 		bw = WMI_PEER_CHWIDTH_40MHZ;
3815 		break;
3816 	case IEEE80211_STA_RX_BW_80:
3817 		bw = WMI_PEER_CHWIDTH_80MHZ;
3818 		break;
3819 	case IEEE80211_STA_RX_BW_160:
3820 		bw = WMI_PEER_CHWIDTH_160MHZ;
3821 		break;
3822 	case IEEE80211_STA_RX_BW_320:
3823 		bw = WMI_PEER_CHWIDTH_320MHZ;
3824 		break;
3825 	default:
3826 		ath12k_warn(ar->ab, "Invalid bandwidth %d for link station %pM\n",
3827 			    link_sta->bandwidth, link_sta->addr);
3828 		bw = WMI_PEER_CHWIDTH_20MHZ;
3829 		break;
3830 	}
3831 
3832 	return bw;
3833 }
3834 
3835 static void ath12k_bss_assoc(struct ath12k *ar,
3836 			     struct ath12k_link_vif *arvif,
3837 			     struct ieee80211_bss_conf *bss_conf)
3838 {
3839 	struct ath12k_vif *ahvif = arvif->ahvif;
3840 	struct ieee80211_vif *vif = ath12k_ahvif_to_vif(ahvif);
3841 	struct ath12k_wmi_vdev_up_params params = {};
3842 	struct ieee80211_link_sta *link_sta;
3843 	u8 link_id = bss_conf->link_id;
3844 	struct ath12k_link_sta *arsta;
3845 	struct ieee80211_sta *ap_sta;
3846 	struct ath12k_sta *ahsta;
3847 	struct ath12k_dp_link_peer *peer;
3848 	bool is_auth = false;
3849 	u32 hemode = 0;
3850 	int ret;
3851 	struct ath12k_dp *dp = ath12k_ab_to_dp(ar->ab);
3852 
3853 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
3854 
3855 	struct ath12k_wmi_peer_assoc_arg *peer_arg __free(kfree) =
3856 					kzalloc_obj(*peer_arg);
3857 	if (!peer_arg)
3858 		return;
3859 
3860 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
3861 		   "mac vdev %i link id %u assoc bssid %pM aid %d\n",
3862 		   arvif->vdev_id, link_id, arvif->bssid, ahvif->aid);
3863 
3864 	rcu_read_lock();
3865 
3866 	/* During ML connection, cfg.ap_addr has the MLD address. For
3867 	 * non-ML connection, it has the BSSID.
3868 	 */
3869 	ap_sta = ieee80211_find_sta(vif, vif->cfg.ap_addr);
3870 	if (!ap_sta) {
3871 		ath12k_warn(ar->ab, "failed to find station entry for bss %pM vdev %i\n",
3872 			    vif->cfg.ap_addr, arvif->vdev_id);
3873 		rcu_read_unlock();
3874 		return;
3875 	}
3876 
3877 	ahsta = ath12k_sta_to_ahsta(ap_sta);
3878 
3879 	arsta = wiphy_dereference(ath12k_ar_to_hw(ar)->wiphy,
3880 				  ahsta->link[link_id]);
3881 	if (WARN_ON(!arsta)) {
3882 		rcu_read_unlock();
3883 		return;
3884 	}
3885 
3886 	link_sta = ath12k_mac_get_link_sta(arsta);
3887 	if (WARN_ON(!link_sta)) {
3888 		rcu_read_unlock();
3889 		return;
3890 	}
3891 
3892 	ath12k_peer_assoc_prepare(ar, arvif, arsta, peer_arg, false);
3893 
3894 	/* link_sta->he_cap must be protected by rcu_read_lock */
3895 	ret = ath12k_mac_vif_recalc_sta_he_txbf(ar, arvif, &link_sta->he_cap, &hemode);
3896 	if (ret) {
3897 		ath12k_warn(ar->ab, "failed to recalc he txbf for vdev %i on bss %pM: %d\n",
3898 			    arvif->vdev_id, bss_conf->bssid, ret);
3899 		rcu_read_unlock();
3900 		return;
3901 	}
3902 
3903 	rcu_read_unlock();
3904 
3905 	/* keep this before ath12k_wmi_send_peer_assoc_cmd() */
3906 	ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
3907 					    WMI_VDEV_PARAM_SET_HEMU_MODE, hemode);
3908 	if (ret) {
3909 		ath12k_warn(ar->ab, "failed to submit vdev param txbf 0x%x: %d\n",
3910 			    hemode, ret);
3911 		return;
3912 	}
3913 
3914 	peer_arg->is_assoc = true;
3915 	ret = ath12k_wmi_send_peer_assoc_cmd(ar, peer_arg);
3916 	if (ret) {
3917 		ath12k_warn(ar->ab, "failed to run peer assoc for %pM vdev %i: %d\n",
3918 			    bss_conf->bssid, arvif->vdev_id, ret);
3919 		return;
3920 	}
3921 
3922 	if (!wait_for_completion_timeout(&ar->peer_assoc_done, 1 * HZ)) {
3923 		ath12k_warn(ar->ab, "failed to get peer assoc conf event for %pM vdev %i\n",
3924 			    bss_conf->bssid, arvif->vdev_id);
3925 		return;
3926 	}
3927 
3928 	ret = ath12k_setup_peer_smps(ar, arvif, bss_conf->bssid,
3929 				     &link_sta->ht_cap, &link_sta->he_6ghz_capa);
3930 	if (ret) {
3931 		ath12k_warn(ar->ab, "failed to setup peer SMPS for vdev %d: %d\n",
3932 			    arvif->vdev_id, ret);
3933 		return;
3934 	}
3935 
3936 	WARN_ON(arvif->is_up);
3937 
3938 	ahvif->aid = vif->cfg.aid;
3939 	ether_addr_copy(arvif->bssid, bss_conf->bssid);
3940 
3941 	params.vdev_id = arvif->vdev_id;
3942 	params.aid = ahvif->aid;
3943 	params.bssid = arvif->bssid;
3944 	params.tx_bssid = ath12k_mac_get_tx_bssid(arvif);
3945 	if (params.tx_bssid) {
3946 		params.nontx_profile_idx = bss_conf->bssid_index;
3947 		params.nontx_profile_cnt = 1 << bss_conf->bssid_indicator;
3948 	}
3949 	ret = ath12k_wmi_vdev_up(ar, &params);
3950 	if (ret) {
3951 		ath12k_warn(ar->ab, "failed to set vdev %d up: %d\n",
3952 			    arvif->vdev_id, ret);
3953 		return;
3954 	}
3955 
3956 	arvif->is_up = true;
3957 	arvif->rekey_data.enable_offload = false;
3958 
3959 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
3960 		   "mac vdev %d up (associated) bssid %pM aid %d\n",
3961 		   arvif->vdev_id, bss_conf->bssid, vif->cfg.aid);
3962 
3963 	spin_lock_bh(&dp->dp_lock);
3964 
3965 	peer = ath12k_dp_link_peer_find_by_vdev_and_addr(dp, arvif->vdev_id,
3966 							 arvif->bssid);
3967 	if (peer && peer->is_authorized)
3968 		is_auth = true;
3969 
3970 	spin_unlock_bh(&dp->dp_lock);
3971 
3972 	/* Authorize BSS Peer */
3973 	if (is_auth) {
3974 		ret = ath12k_wmi_set_peer_param(ar, arvif->bssid,
3975 						arvif->vdev_id,
3976 						WMI_PEER_AUTHORIZE,
3977 						1);
3978 		if (ret)
3979 			ath12k_warn(ar->ab, "Unable to authorize BSS peer: %d\n", ret);
3980 	}
3981 
3982 	ret = ath12k_wmi_send_obss_spr_cmd(ar, arvif->vdev_id,
3983 					   &bss_conf->he_obss_pd);
3984 	if (ret)
3985 		ath12k_warn(ar->ab, "failed to set vdev %i OBSS PD parameters: %d\n",
3986 			    arvif->vdev_id, ret);
3987 
3988 	if (test_bit(WMI_TLV_SERVICE_11D_OFFLOAD, ar->ab->wmi_ab.svc_map) &&
3989 	    ahvif->vdev_type == WMI_VDEV_TYPE_STA &&
3990 	    ahvif->vdev_subtype == WMI_VDEV_SUBTYPE_NONE)
3991 		ath12k_mac_11d_scan_stop_all(ar->ab);
3992 }
3993 
3994 static void ath12k_bss_disassoc(struct ath12k *ar,
3995 				struct ath12k_link_vif *arvif)
3996 {
3997 	int ret;
3998 
3999 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
4000 
4001 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac vdev %i disassoc bssid %pM\n",
4002 		   arvif->vdev_id, arvif->bssid);
4003 
4004 	ret = ath12k_wmi_vdev_down(ar, arvif->vdev_id);
4005 	if (ret)
4006 		ath12k_warn(ar->ab, "failed to down vdev %i: %d\n",
4007 			    arvif->vdev_id, ret);
4008 
4009 	arvif->is_up = false;
4010 
4011 	memset(&arvif->rekey_data, 0, sizeof(arvif->rekey_data));
4012 
4013 	cancel_delayed_work(&arvif->connection_loss_work);
4014 }
4015 
4016 static u32 ath12k_mac_get_rate_hw_value(int bitrate)
4017 {
4018 	u32 preamble;
4019 	u16 hw_value;
4020 	int rate;
4021 	size_t i;
4022 
4023 	if (ath12k_mac_bitrate_is_cck(bitrate))
4024 		preamble = WMI_RATE_PREAMBLE_CCK;
4025 	else
4026 		preamble = WMI_RATE_PREAMBLE_OFDM;
4027 
4028 	for (i = 0; i < ARRAY_SIZE(ath12k_legacy_rates); i++) {
4029 		if (ath12k_legacy_rates[i].bitrate != bitrate)
4030 			continue;
4031 
4032 		hw_value = ath12k_legacy_rates[i].hw_value;
4033 		rate = ATH12K_HW_RATE_CODE(hw_value, 0, preamble);
4034 
4035 		return rate;
4036 	}
4037 
4038 	return -EINVAL;
4039 }
4040 
4041 static void ath12k_recalculate_mgmt_rate(struct ath12k *ar,
4042 					 struct ath12k_link_vif *arvif,
4043 					 struct cfg80211_chan_def *def)
4044 {
4045 	struct ieee80211_vif *vif = ath12k_ahvif_to_vif(arvif->ahvif);
4046 	struct ieee80211_hw *hw = ath12k_ar_to_hw(ar);
4047 	const struct ieee80211_supported_band *sband;
4048 	struct ieee80211_bss_conf *bss_conf;
4049 	u8 basic_rate_idx;
4050 	int hw_rate_code;
4051 	u32 vdev_param;
4052 	u16 bitrate;
4053 	int ret;
4054 
4055 	lockdep_assert_wiphy(hw->wiphy);
4056 
4057 	bss_conf = ath12k_mac_get_link_bss_conf(arvif);
4058 	if (!bss_conf) {
4059 		ath12k_warn(ar->ab, "unable to access bss link conf in mgmt rate calc for vif %pM link %u\n",
4060 			    vif->addr, arvif->link_id);
4061 		return;
4062 	}
4063 
4064 	sband = hw->wiphy->bands[def->chan->band];
4065 	if (bss_conf->basic_rates)
4066 		basic_rate_idx = __ffs(bss_conf->basic_rates);
4067 	else
4068 		basic_rate_idx = 0;
4069 	bitrate = sband->bitrates[basic_rate_idx].bitrate;
4070 
4071 	hw_rate_code = ath12k_mac_get_rate_hw_value(bitrate);
4072 	if (hw_rate_code < 0) {
4073 		ath12k_warn(ar->ab, "bitrate not supported %d\n", bitrate);
4074 		return;
4075 	}
4076 
4077 	vdev_param = WMI_VDEV_PARAM_MGMT_RATE;
4078 	ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id, vdev_param,
4079 					    hw_rate_code);
4080 	if (ret)
4081 		ath12k_warn(ar->ab, "failed to set mgmt tx rate %d\n", ret);
4082 
4083 	vdev_param = WMI_VDEV_PARAM_BEACON_RATE;
4084 	ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id, vdev_param,
4085 					    hw_rate_code);
4086 	if (ret)
4087 		ath12k_warn(ar->ab, "failed to set beacon tx rate %d\n", ret);
4088 }
4089 
4090 static void ath12k_mac_bcn_tx_event(struct ath12k_link_vif *arvif)
4091 {
4092 	struct ieee80211_vif *vif = ath12k_ahvif_to_vif(arvif->ahvif);
4093 	struct ieee80211_bss_conf *link_conf;
4094 
4095 	link_conf = ath12k_mac_get_link_bss_conf(arvif);
4096 	if (!link_conf) {
4097 		ath12k_warn(arvif->ar->ab, "failed to get link conf for vdev %u\n",
4098 			    arvif->vdev_id);
4099 		return;
4100 	}
4101 
4102 	if (link_conf->color_change_active) {
4103 		if (ieee80211_beacon_cntdwn_is_complete(vif, arvif->link_id)) {
4104 			ieee80211_color_change_finish(vif, arvif->link_id);
4105 			return;
4106 		}
4107 
4108 		ieee80211_beacon_update_cntdwn(vif, arvif->link_id);
4109 		ath12k_mac_setup_bcn_tmpl(arvif);
4110 	}
4111 }
4112 
4113 static void ath12k_mac_bcn_tx_work(struct wiphy *wiphy, struct wiphy_work *work)
4114 {
4115 	struct ath12k_link_vif *arvif = container_of(work, struct ath12k_link_vif,
4116 						     bcn_tx_work);
4117 
4118 	lockdep_assert_wiphy(wiphy);
4119 	ath12k_mac_bcn_tx_event(arvif);
4120 }
4121 
4122 static void ath12k_mac_init_arvif(struct ath12k_vif *ahvif,
4123 				  struct ath12k_link_vif *arvif, int link_id)
4124 {
4125 	struct ath12k_hw *ah = ahvif->ah;
4126 	u8 _link_id;
4127 	int i;
4128 
4129 	lockdep_assert_wiphy(ah->hw->wiphy);
4130 
4131 	if (WARN_ON(!arvif))
4132 		return;
4133 
4134 	if (WARN_ON(link_id >= ATH12K_NUM_MAX_LINKS))
4135 		return;
4136 
4137 	if (link_id < 0)
4138 		_link_id = 0;
4139 	else
4140 		_link_id = link_id;
4141 
4142 	arvif->ahvif = ahvif;
4143 	arvif->link_id = _link_id;
4144 
4145 	/* Protects the datapath stats update on a per link basis */
4146 	spin_lock_init(&arvif->link_stats_lock);
4147 
4148 	INIT_LIST_HEAD(&arvif->list);
4149 	INIT_DELAYED_WORK(&arvif->connection_loss_work,
4150 			  ath12k_mac_vif_sta_connection_loss_work);
4151 	wiphy_work_init(&arvif->bcn_tx_work, ath12k_mac_bcn_tx_work);
4152 
4153 	arvif->num_stations = 0;
4154 
4155 	for (i = 0; i < ARRAY_SIZE(arvif->bitrate_mask.control); i++) {
4156 		arvif->bitrate_mask.control[i].legacy = 0xffffffff;
4157 		arvif->bitrate_mask.control[i].gi = NL80211_TXRATE_DEFAULT_GI;
4158 		memset(arvif->bitrate_mask.control[i].ht_mcs, 0xff,
4159 		       sizeof(arvif->bitrate_mask.control[i].ht_mcs));
4160 		memset(arvif->bitrate_mask.control[i].vht_mcs, 0xff,
4161 		       sizeof(arvif->bitrate_mask.control[i].vht_mcs));
4162 		memset(arvif->bitrate_mask.control[i].he_mcs, 0xff,
4163 		       sizeof(arvif->bitrate_mask.control[i].he_mcs));
4164 		memset(arvif->bitrate_mask.control[i].eht_mcs, 0xff,
4165 		       sizeof(arvif->bitrate_mask.control[i].eht_mcs));
4166 	}
4167 
4168 	/* Handle MLO related assignments */
4169 	if (link_id >= 0) {
4170 		rcu_assign_pointer(ahvif->link[arvif->link_id], arvif);
4171 		ahvif->links_map |= BIT(_link_id);
4172 	}
4173 
4174 	ath12k_generic_dbg(ATH12K_DBG_MAC,
4175 			   "mac init link arvif (link_id %d%s) for vif %pM. links_map 0x%x",
4176 			   _link_id, (link_id < 0) ? " deflink" : "", ahvif->vif->addr,
4177 			   ahvif->links_map);
4178 }
4179 
4180 static void ath12k_mac_remove_link_interface(struct ieee80211_hw *hw,
4181 					     struct ath12k_link_vif *arvif)
4182 {
4183 	struct ath12k_vif *ahvif = arvif->ahvif;
4184 	struct ath12k_hw *ah = hw->priv;
4185 	struct ath12k *ar = arvif->ar;
4186 	int ret;
4187 
4188 	lockdep_assert_wiphy(ah->hw->wiphy);
4189 
4190 	cancel_delayed_work_sync(&arvif->connection_loss_work);
4191 	wiphy_work_cancel(ath12k_ar_to_hw(ar)->wiphy, &arvif->bcn_tx_work);
4192 
4193 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac remove link interface (vdev %d link id %d)",
4194 		   arvif->vdev_id, arvif->link_id);
4195 
4196 	if (test_bit(WMI_TLV_SERVICE_11D_OFFLOAD, ar->ab->wmi_ab.svc_map) &&
4197 	    ahvif->vdev_type == WMI_VDEV_TYPE_STA &&
4198 	    ahvif->vdev_subtype == WMI_VDEV_SUBTYPE_NONE)
4199 		ath12k_mac_11d_scan_stop(ar);
4200 
4201 	if (ahvif->vdev_type == WMI_VDEV_TYPE_AP) {
4202 		ret = ath12k_peer_delete(ar, arvif->vdev_id, arvif->bssid);
4203 		if (ret)
4204 			ath12k_warn(ar->ab, "failed to submit AP self-peer removal on vdev %d link id %d: %d",
4205 				    arvif->vdev_id, arvif->link_id, ret);
4206 
4207 		if (arvif->link_id < IEEE80211_MLD_MAX_NUM_LINKS)
4208 			ath12k_dp_peer_delete(&ah->dp_hw, arvif->bssid, NULL);
4209 	}
4210 	ath12k_mac_vdev_delete(ar, arvif);
4211 }
4212 
4213 static struct ath12k_link_vif *ath12k_mac_assign_link_vif(struct ath12k_hw *ah,
4214 							  struct ieee80211_vif *vif,
4215 							  u8 link_id)
4216 {
4217 	struct ath12k_vif *ahvif = ath12k_vif_to_ahvif(vif);
4218 	struct ath12k_link_vif *arvif;
4219 
4220 	lockdep_assert_wiphy(ah->hw->wiphy);
4221 
4222 	arvif = wiphy_dereference(ah->hw->wiphy, ahvif->link[link_id]);
4223 	if (arvif)
4224 		return arvif;
4225 
4226 	/* If this is the first link arvif being created for an ML VIF
4227 	 * use the preallocated deflink memory except for scan arvifs
4228 	 */
4229 	if (!ahvif->links_map && link_id < ATH12K_FIRST_SCAN_LINK) {
4230 		arvif = &ahvif->deflink;
4231 
4232 		if (vif->type == NL80211_IFTYPE_STATION)
4233 			arvif->is_sta_assoc_link = true;
4234 	} else {
4235 		arvif = kzalloc_obj(*arvif);
4236 		if (!arvif)
4237 			return NULL;
4238 	}
4239 
4240 	ath12k_mac_init_arvif(ahvif, arvif, link_id);
4241 
4242 	return arvif;
4243 }
4244 
4245 static void ath12k_mac_unassign_link_vif(struct ath12k_link_vif *arvif)
4246 {
4247 	struct ath12k_vif *ahvif = arvif->ahvif;
4248 	struct ath12k_hw *ah = ahvif->ah;
4249 
4250 	lockdep_assert_wiphy(ah->hw->wiphy);
4251 
4252 	rcu_assign_pointer(ahvif->link[arvif->link_id], NULL);
4253 	synchronize_rcu();
4254 	ahvif->links_map &= ~BIT(arvif->link_id);
4255 
4256 	if (arvif != &ahvif->deflink)
4257 		kfree(arvif);
4258 	else
4259 		memset(arvif, 0, sizeof(*arvif));
4260 }
4261 
4262 int
4263 ath12k_mac_op_change_vif_links(struct ieee80211_hw *hw,
4264 			       struct ieee80211_vif *vif,
4265 			       u16 old_links, u16 new_links,
4266 			       struct ieee80211_bss_conf *ol[IEEE80211_MLD_MAX_NUM_LINKS])
4267 {
4268 	struct ath12k_vif *ahvif = ath12k_vif_to_ahvif(vif);
4269 	unsigned long to_remove = old_links & ~new_links;
4270 	unsigned long to_add = ~old_links & new_links;
4271 	struct ath12k_hw *ah = ath12k_hw_to_ah(hw);
4272 	struct ath12k_link_vif *arvif;
4273 	u8 link_id;
4274 
4275 	lockdep_assert_wiphy(hw->wiphy);
4276 
4277 	ath12k_generic_dbg(ATH12K_DBG_MAC,
4278 			   "mac vif link changed for MLD %pM old_links 0x%x new_links 0x%x\n",
4279 			   vif->addr, old_links, new_links);
4280 
4281 	for_each_set_bit(link_id, &to_add, IEEE80211_MLD_MAX_NUM_LINKS) {
4282 		arvif = wiphy_dereference(hw->wiphy, ahvif->link[link_id]);
4283 		/* mac80211 wants to add link but driver already has the
4284 		 * link. This should not happen ideally.
4285 		 */
4286 		if (WARN_ON(arvif))
4287 			return -EINVAL;
4288 
4289 		arvif = ath12k_mac_assign_link_vif(ah, vif, link_id);
4290 		if (WARN_ON(!arvif))
4291 			return -EINVAL;
4292 	}
4293 
4294 	for_each_set_bit(link_id, &to_remove, IEEE80211_MLD_MAX_NUM_LINKS) {
4295 		arvif = wiphy_dereference(hw->wiphy, ahvif->link[link_id]);
4296 		if (WARN_ON(!arvif))
4297 			return -EINVAL;
4298 
4299 		if (!arvif->is_created) {
4300 			ath12k_mac_unassign_link_vif(arvif);
4301 			continue;
4302 		}
4303 
4304 		if (WARN_ON(!arvif->ar))
4305 			return -EINVAL;
4306 
4307 		ath12k_mac_remove_link_interface(hw, arvif);
4308 		ath12k_mac_unassign_link_vif(arvif);
4309 	}
4310 
4311 	return 0;
4312 }
4313 EXPORT_SYMBOL(ath12k_mac_op_change_vif_links);
4314 
4315 static int ath12k_mac_fils_discovery(struct ath12k_link_vif *arvif,
4316 				     struct ieee80211_bss_conf *info)
4317 {
4318 	struct ieee80211_vif *vif = ath12k_ahvif_to_vif(arvif->ahvif);
4319 	struct ath12k *ar = arvif->ar;
4320 	struct ieee80211_hw *hw = ath12k_ar_to_hw(ar);
4321 	struct sk_buff *tmpl;
4322 	int ret;
4323 	u32 interval;
4324 	bool unsol_bcast_probe_resp_enabled = false;
4325 
4326 	if (info->fils_discovery.max_interval) {
4327 		interval = info->fils_discovery.max_interval;
4328 
4329 		tmpl = ieee80211_get_fils_discovery_tmpl(hw, vif,
4330 							 info->link_id);
4331 		if (tmpl)
4332 			ret = ath12k_wmi_fils_discovery_tmpl(ar, arvif->vdev_id,
4333 							     tmpl);
4334 	} else if (info->unsol_bcast_probe_resp_interval) {
4335 		unsol_bcast_probe_resp_enabled = 1;
4336 		interval = info->unsol_bcast_probe_resp_interval;
4337 
4338 		tmpl = ieee80211_get_unsol_bcast_probe_resp_tmpl(hw, vif,
4339 								 info->link_id);
4340 		if (tmpl)
4341 			ret = ath12k_wmi_probe_resp_tmpl(ar, arvif->vdev_id,
4342 							 tmpl);
4343 	} else { /* Disable */
4344 		return ath12k_wmi_fils_discovery(ar, arvif->vdev_id, 0, false);
4345 	}
4346 
4347 	if (!tmpl) {
4348 		ath12k_warn(ar->ab,
4349 			    "mac vdev %i failed to retrieve %s template\n",
4350 			    arvif->vdev_id, (unsol_bcast_probe_resp_enabled ?
4351 			    "unsolicited broadcast probe response" :
4352 			    "FILS discovery"));
4353 		return -EPERM;
4354 	}
4355 	kfree_skb(tmpl);
4356 
4357 	if (!ret)
4358 		ret = ath12k_wmi_fils_discovery(ar, arvif->vdev_id, interval,
4359 						unsol_bcast_probe_resp_enabled);
4360 
4361 	return ret;
4362 }
4363 
4364 void ath12k_mac_op_vif_cfg_changed(struct ieee80211_hw *hw,
4365 				   struct ieee80211_vif *vif,
4366 				   u64 changed)
4367 {
4368 	struct ath12k_vif *ahvif = ath12k_vif_to_ahvif(vif);
4369 	unsigned long links = ahvif->links_map;
4370 	struct ieee80211_bss_conf *info;
4371 	struct ath12k_link_vif *arvif;
4372 	struct ieee80211_sta *sta;
4373 	struct ath12k_sta *ahsta;
4374 	struct ath12k *ar;
4375 	u8 link_id;
4376 
4377 	lockdep_assert_wiphy(hw->wiphy);
4378 
4379 	if (changed & BSS_CHANGED_SSID && vif->type == NL80211_IFTYPE_AP) {
4380 		ahvif->u.ap.ssid_len = vif->cfg.ssid_len;
4381 		if (vif->cfg.ssid_len)
4382 			memcpy(ahvif->u.ap.ssid, vif->cfg.ssid, vif->cfg.ssid_len);
4383 	}
4384 
4385 	if (changed & BSS_CHANGED_ASSOC) {
4386 		if (vif->cfg.assoc) {
4387 			/* only in station mode we can get here, so it's safe
4388 			 * to use ap_addr
4389 			 */
4390 			rcu_read_lock();
4391 			sta = ieee80211_find_sta(vif, vif->cfg.ap_addr);
4392 			if (!sta) {
4393 				rcu_read_unlock();
4394 				WARN_ONCE(1, "failed to find sta with addr %pM\n",
4395 					  vif->cfg.ap_addr);
4396 				return;
4397 			}
4398 
4399 			ahsta = ath12k_sta_to_ahsta(sta);
4400 			arvif = wiphy_dereference(hw->wiphy,
4401 						  ahvif->link[ahsta->assoc_link_id]);
4402 			rcu_read_unlock();
4403 
4404 			ar = arvif->ar;
4405 			/* there is no reason for which an assoc link's
4406 			 * bss info does not exist
4407 			 */
4408 			info = ath12k_mac_get_link_bss_conf(arvif);
4409 			ath12k_bss_assoc(ar, arvif, info);
4410 
4411 			/* exclude assoc link as it is done above */
4412 			links &= ~BIT(ahsta->assoc_link_id);
4413 		}
4414 
4415 		for_each_set_bit(link_id, &links, IEEE80211_MLD_MAX_NUM_LINKS) {
4416 			arvif = wiphy_dereference(hw->wiphy, ahvif->link[link_id]);
4417 			if (!arvif || !arvif->ar)
4418 				continue;
4419 
4420 			ar = arvif->ar;
4421 
4422 			if (vif->cfg.assoc) {
4423 				info = ath12k_mac_get_link_bss_conf(arvif);
4424 				if (!info)
4425 					continue;
4426 
4427 				ath12k_bss_assoc(ar, arvif, info);
4428 			} else {
4429 				ath12k_bss_disassoc(ar, arvif);
4430 			}
4431 		}
4432 	}
4433 }
4434 EXPORT_SYMBOL(ath12k_mac_op_vif_cfg_changed);
4435 
4436 static void ath12k_mac_vif_setup_ps(struct ath12k_link_vif *arvif)
4437 {
4438 	struct ath12k *ar = arvif->ar;
4439 	struct ieee80211_vif *vif = arvif->ahvif->vif;
4440 	struct ieee80211_conf *conf = &ath12k_ar_to_hw(ar)->conf;
4441 	enum wmi_sta_powersave_param param;
4442 	struct ieee80211_bss_conf *info;
4443 	enum wmi_sta_ps_mode psmode;
4444 	int ret;
4445 	int timeout;
4446 	bool enable_ps;
4447 
4448 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
4449 
4450 	if (vif->type != NL80211_IFTYPE_STATION)
4451 		return;
4452 
4453 	enable_ps = arvif->ahvif->ps;
4454 	if (enable_ps) {
4455 		psmode = WMI_STA_PS_MODE_ENABLED;
4456 		param = WMI_STA_PS_PARAM_INACTIVITY_TIME;
4457 
4458 		timeout = conf->dynamic_ps_timeout;
4459 		if (timeout == 0) {
4460 			info = ath12k_mac_get_link_bss_conf(arvif);
4461 			if (!info) {
4462 				ath12k_warn(ar->ab, "unable to access bss link conf in setup ps for vif %pM link %u\n",
4463 					    vif->addr, arvif->link_id);
4464 				return;
4465 			}
4466 
4467 			/* firmware doesn't like 0 */
4468 			timeout = ieee80211_tu_to_usec(info->beacon_int) / 1000;
4469 		}
4470 
4471 		ret = ath12k_wmi_set_sta_ps_param(ar, arvif->vdev_id, param,
4472 						  timeout);
4473 		if (ret) {
4474 			ath12k_warn(ar->ab, "failed to set inactivity time for vdev %d: %i\n",
4475 				    arvif->vdev_id, ret);
4476 			return;
4477 		}
4478 	} else {
4479 		psmode = WMI_STA_PS_MODE_DISABLED;
4480 	}
4481 
4482 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac vdev %d psmode %s\n",
4483 		   arvif->vdev_id, psmode ? "enable" : "disable");
4484 
4485 	ret = ath12k_wmi_pdev_set_ps_mode(ar, arvif->vdev_id, psmode);
4486 	if (ret)
4487 		ath12k_warn(ar->ab, "failed to set sta power save mode %d for vdev %d: %d\n",
4488 			    psmode, arvif->vdev_id, ret);
4489 }
4490 
4491 static bool ath12k_mac_supports_tpc(struct ath12k *ar, struct ath12k_vif *ahvif,
4492 				    const struct cfg80211_chan_def *chandef)
4493 {
4494 	return ath12k_wmi_supports_6ghz_cc_ext(ar) &&
4495 		test_bit(WMI_TLV_SERVICE_EXT_TPC_REG_SUPPORT, ar->ab->wmi_ab.svc_map) &&
4496 		(ahvif->vdev_type == WMI_VDEV_TYPE_STA  ||
4497 		 ahvif->vdev_type == WMI_VDEV_TYPE_AP) &&
4498 		ahvif->vdev_subtype == WMI_VDEV_SUBTYPE_NONE &&
4499 		chandef->chan &&
4500 		chandef->chan->band == NL80211_BAND_6GHZ;
4501 }
4502 
4503 static void ath12k_wmi_vdev_params_up(struct ath12k *ar,
4504 				      struct ath12k_link_vif *arvif,
4505 				      struct ath12k_link_vif *tx_arvif,
4506 				      struct ieee80211_bss_conf *info, u16 aid)
4507 {
4508 	struct ath12k_wmi_vdev_up_params params = {
4509 		.vdev_id = arvif->vdev_id,
4510 		.aid = aid,
4511 		.bssid = arvif->bssid
4512 	};
4513 	int ret;
4514 
4515 	if (tx_arvif) {
4516 		params.tx_bssid = tx_arvif->bssid;
4517 		params.nontx_profile_idx = info->bssid_index;
4518 		params.nontx_profile_cnt = 1 << info->bssid_indicator;
4519 	}
4520 
4521 	ret = ath12k_wmi_vdev_up(arvif->ar, &params);
4522 	if (ret)
4523 		ath12k_warn(ar->ab, "failed to bring vdev up %d: %d\n",
4524 			    arvif->vdev_id, ret);
4525 }
4526 
4527 static int ath12k_mac_config_obss_pd(struct ath12k_link_vif *arvif,
4528 				     const struct ieee80211_he_obss_pd *he_obss_pd)
4529 {
4530 	struct ath12k_wmi_obss_pd_arg obss_pd_arg = {};
4531 	u32 srg_bitmap[2], non_srg_bitmap[2];
4532 	struct ath12k *ar = arvif->ar;
4533 	u32 param_id, pdev_id;
4534 	u32 param_val;
4535 	int ret;
4536 
4537 	if (ar->ab->hw_params->single_pdev_only)
4538 		pdev_id = ath12k_mac_get_target_pdev_id_from_vif(arvif);
4539 	else
4540 		pdev_id = ar->pdev->pdev_id;
4541 
4542 	/* Set and enable SRG/non-SRG OBSS PD threshold */
4543 	param_id = WMI_PDEV_PARAM_SET_CMD_OBSS_PD_THRESHOLD;
4544 	if (ar->monitor_started || !he_obss_pd->enable) {
4545 		ret = ath12k_wmi_pdev_set_param(ar, param_id, 0, pdev_id);
4546 		if (ret)
4547 			ath12k_warn(ar->ab,
4548 				    "failed to set OBSS PD threshold for pdev %u: %d\n",
4549 				    pdev_id, ret);
4550 		return ret;
4551 	}
4552 
4553 	/*
4554 	 * This service flag indicates firmware support for SRG/SRP-based
4555 	 * spatial reuse. It also specifies whether OBSS PD threshold values
4556 	 * should be interpreted as dB (offset) or dBm (absolute) units.
4557 	 */
4558 	obss_pd_arg.srp_support = test_bit(WMI_TLV_SERVICE_SRG_SRP_SPATIAL_REUSE_SUPPORT,
4559 					   ar->ab->wmi_ab.svc_map);
4560 
4561 	if (!(he_obss_pd->sr_ctrl &
4562 	      IEEE80211_HE_SPR_NON_SRG_OBSS_PD_SR_DISALLOWED)) {
4563 		if (he_obss_pd->sr_ctrl & IEEE80211_HE_SPR_NON_SRG_OFFSET_PRESENT)
4564 			obss_pd_arg.non_srg_th = ATH12K_OBSS_PD_MAX_THRESHOLD +
4565 						 he_obss_pd->non_srg_max_offset;
4566 		else
4567 			obss_pd_arg.non_srg_th = ATH12K_OBSS_PD_NON_SRG_MAX_THRESHOLD;
4568 
4569 		if (!obss_pd_arg.srp_support)
4570 			obss_pd_arg.non_srg_th -= ATH12K_DEFAULT_NOISE_FLOOR;
4571 
4572 		obss_pd_arg.non_srg_enabled = true;
4573 	}
4574 
4575 	if (he_obss_pd->sr_ctrl & IEEE80211_HE_SPR_SRG_INFORMATION_PRESENT) {
4576 		obss_pd_arg.srg_th = ATH12K_OBSS_PD_MAX_THRESHOLD +
4577 				     he_obss_pd->max_offset;
4578 		obss_pd_arg.srg_enabled = true;
4579 	}
4580 
4581 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
4582 		   "pdev %u OBSS PD sr_ctrl 0x%x srg_th %d dBm non_srg_th %d dBm\n",
4583 		   pdev_id, he_obss_pd->sr_ctrl,
4584 		   obss_pd_arg.srg_th, obss_pd_arg.non_srg_th);
4585 
4586 	param_val = ath12k_wmi_build_obss_pd(&obss_pd_arg);
4587 	ret = ath12k_wmi_pdev_set_param(ar, param_id, param_val, pdev_id);
4588 	if (ret) {
4589 		ath12k_warn(ar->ab,
4590 			    "failed to set OBSS PD threshold for pdev %u: %d\n",
4591 			    pdev_id, ret);
4592 		return ret;
4593 	}
4594 
4595 	/* Enable OBSS PD for all access category */
4596 	param_id  = WMI_PDEV_PARAM_SET_CMD_OBSS_PD_PER_AC;
4597 	param_val = 0xf;
4598 	ret = ath12k_wmi_pdev_set_param(ar, param_id, param_val, pdev_id);
4599 	if (ret) {
4600 		ath12k_warn(ar->ab,
4601 			    "failed to set OBSS PD per ac for pdev %u: %d\n",
4602 			    pdev_id, ret);
4603 		return ret;
4604 	}
4605 
4606 	/* Set SR prohibit */
4607 	param_id  = WMI_PDEV_PARAM_ENABLE_SR_PROHIBIT;
4608 	param_val = !!(he_obss_pd->sr_ctrl &
4609 		       IEEE80211_HE_SPR_HESIGA_SR_VAL15_ALLOWED);
4610 	ret = ath12k_wmi_pdev_set_param(ar, param_id, param_val, pdev_id);
4611 	if (ret) {
4612 		ath12k_warn(ar->ab, "failed to set SR prohibit for pdev %u: %d\n",
4613 			    pdev_id, ret);
4614 		return ret;
4615 	}
4616 
4617 	if (!obss_pd_arg.srp_support)
4618 		return 0;
4619 
4620 	memcpy(srg_bitmap, he_obss_pd->bss_color_bitmap, sizeof(srg_bitmap));
4621 	/* Set SRG BSS color bitmap */
4622 	ret = ath12k_wmi_pdev_set_srg_bss_color_bitmap(ar, pdev_id, srg_bitmap);
4623 	if (ret) {
4624 		ath12k_warn(ar->ab,
4625 			    "failed to set SRG bss color bitmap for pdev %u: %d\n",
4626 			    pdev_id, ret);
4627 		return ret;
4628 	}
4629 
4630 	/* Enable BSS colors for SRG */
4631 	ret = ath12k_wmi_pdev_srg_obss_color_enable_bitmap(ar, pdev_id, srg_bitmap);
4632 	if (ret) {
4633 		ath12k_warn(ar->ab,
4634 			    "failed to enable SRG bss color bitmap pdev %u: %d\n",
4635 			    pdev_id, ret);
4636 		return ret;
4637 	}
4638 
4639 	memcpy(srg_bitmap, he_obss_pd->partial_bssid_bitmap, sizeof(srg_bitmap));
4640 	/* Set SRG partial bssid bitmap */
4641 	ret = ath12k_wmi_pdev_set_srg_partial_bssid_bitmap(ar, pdev_id, srg_bitmap);
4642 	if (ret) {
4643 		ath12k_warn(ar->ab,
4644 			    "failed to set SRG partial bssid bitmap for pdev %u: %d\n",
4645 			    pdev_id, ret);
4646 		return ret;
4647 	}
4648 
4649 	/* Enable partial bssid mask for SRG */
4650 	ret = ath12k_wmi_pdev_srg_obss_bssid_enable_bitmap(ar, pdev_id, srg_bitmap);
4651 	if (ret) {
4652 		ath12k_warn(ar->ab,
4653 			    "failed to enable SRG bssid bitmap pdev %u: %d\n",
4654 			    pdev_id, ret);
4655 		return ret;
4656 	}
4657 
4658 	/*
4659 	 * No explicit non-SRG bitmap from mac80211; enable all colors/bssids
4660 	 * as non-SRG candidates. Actual SRG members are filtered by SRG bitmaps.
4661 	 */
4662 	memset(non_srg_bitmap, 0xff, sizeof(non_srg_bitmap));
4663 
4664 	/* Enable BSS colors for non-SRG */
4665 	ret = ath12k_wmi_pdev_non_srg_obss_color_enable_bitmap(ar, pdev_id,
4666 							       non_srg_bitmap);
4667 	if (ret) {
4668 		ath12k_warn(ar->ab,
4669 			    "failed to enable non SRG color bitmap pdev %u: %d\n",
4670 			    pdev_id, ret);
4671 		return ret;
4672 	}
4673 
4674 	/* Enable partial bssid mask for non-SRG */
4675 	ret = ath12k_wmi_pdev_non_srg_obss_bssid_enable_bitmap(ar, pdev_id,
4676 							       non_srg_bitmap);
4677 	if (ret) {
4678 		ath12k_warn(ar->ab,
4679 			    "failed to enable non SRG bssid bitmap pdev %u: %d\n",
4680 			    pdev_id, ret);
4681 		return ret;
4682 	}
4683 
4684 	return 0;
4685 }
4686 
4687 static void ath12k_mac_bss_info_changed(struct ath12k *ar,
4688 					struct ath12k_link_vif *arvif,
4689 					struct ieee80211_bss_conf *info,
4690 					u64 changed)
4691 {
4692 	struct ath12k_vif *ahvif = arvif->ahvif;
4693 	struct ieee80211_vif *vif = ath12k_ahvif_to_vif(ahvif);
4694 	struct ieee80211_vif_cfg *vif_cfg = &vif->cfg;
4695 	struct ath12k_link_vif *tx_arvif;
4696 	struct cfg80211_chan_def def;
4697 	u32 param_id, param_value;
4698 	enum nl80211_band band;
4699 	u32 vdev_param;
4700 	int mcast_rate;
4701 	u32 preamble;
4702 	u16 hw_value;
4703 	u16 bitrate;
4704 	u8 rateidx;
4705 	u32 rate;
4706 	int ret;
4707 
4708 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
4709 
4710 	if (changed & BSS_CHANGED_BEACON_INT) {
4711 		arvif->beacon_interval = info->beacon_int;
4712 
4713 		param_id = WMI_VDEV_PARAM_BEACON_INTERVAL;
4714 		ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
4715 						    param_id,
4716 						    arvif->beacon_interval);
4717 		if (ret)
4718 			ath12k_warn(ar->ab, "Failed to set beacon interval for VDEV: %d\n",
4719 				    arvif->vdev_id);
4720 		else
4721 			ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
4722 				   "Beacon interval: %d set for VDEV: %d\n",
4723 				   arvif->beacon_interval, arvif->vdev_id);
4724 	}
4725 
4726 	if (changed & BSS_CHANGED_BEACON) {
4727 		param_id = WMI_PDEV_PARAM_BEACON_TX_MODE;
4728 		param_value = WMI_BEACON_BURST_MODE;
4729 		ret = ath12k_wmi_pdev_set_param(ar, param_id,
4730 						param_value, ar->pdev->pdev_id);
4731 		if (ret)
4732 			ath12k_warn(ar->ab, "Failed to set beacon mode for VDEV: %d\n",
4733 				    arvif->vdev_id);
4734 		else
4735 			ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
4736 				   "Set burst beacon mode for VDEV: %d\n",
4737 				   arvif->vdev_id);
4738 
4739 		/* In MBSSID case, need to install transmitting VIF's template first */
4740 
4741 		ret = ath12k_mac_setup_bcn_tmpl(arvif);
4742 		if (ret)
4743 			ath12k_warn(ar->ab, "failed to update bcn template: %d\n",
4744 				    ret);
4745 
4746 		if (!arvif->is_csa_in_progress)
4747 			goto skip_vdev_up;
4748 
4749 		tx_arvif = ath12k_mac_get_tx_arvif(arvif, info);
4750 		if (tx_arvif && arvif != tx_arvif && tx_arvif->is_csa_in_progress)
4751 			/* skip non tx vif's */
4752 			goto skip_vdev_up;
4753 
4754 		ath12k_wmi_vdev_params_up(ar, arvif, tx_arvif, info, ahvif->aid);
4755 
4756 		arvif->is_csa_in_progress = false;
4757 
4758 		if (tx_arvif && arvif == tx_arvif) {
4759 			struct ath12k_link_vif *arvif_itr;
4760 
4761 			list_for_each_entry(arvif_itr, &ar->arvifs, list) {
4762 				if (!arvif_itr->is_csa_in_progress)
4763 					continue;
4764 
4765 				ath12k_wmi_vdev_params_up(ar, arvif, tx_arvif,
4766 							  info, ahvif->aid);
4767 				arvif_itr->is_csa_in_progress = false;
4768 			}
4769 		}
4770 	}
4771 
4772 skip_vdev_up:
4773 
4774 	if (changed & (BSS_CHANGED_BEACON_INFO | BSS_CHANGED_BEACON)) {
4775 		arvif->dtim_period = info->dtim_period;
4776 
4777 		param_id = WMI_VDEV_PARAM_DTIM_PERIOD;
4778 		ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
4779 						    param_id,
4780 						    arvif->dtim_period);
4781 
4782 		if (ret)
4783 			ath12k_warn(ar->ab, "Failed to set dtim period for VDEV %d: %i\n",
4784 				    arvif->vdev_id, ret);
4785 		else
4786 			ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
4787 				   "DTIM period: %d set for VDEV: %d\n",
4788 				   arvif->dtim_period, arvif->vdev_id);
4789 	}
4790 
4791 	if (changed & BSS_CHANGED_SSID &&
4792 	    vif->type == NL80211_IFTYPE_AP) {
4793 		ahvif->u.ap.ssid_len = vif->cfg.ssid_len;
4794 		if (vif->cfg.ssid_len)
4795 			memcpy(ahvif->u.ap.ssid, vif->cfg.ssid, vif->cfg.ssid_len);
4796 		ahvif->u.ap.hidden_ssid = info->hidden_ssid;
4797 	}
4798 
4799 	if (changed & BSS_CHANGED_BSSID && !is_zero_ether_addr(info->bssid))
4800 		ether_addr_copy(arvif->bssid, info->bssid);
4801 
4802 	if (changed & BSS_CHANGED_BEACON_ENABLED) {
4803 		if (info->enable_beacon) {
4804 			ret = ath12k_mac_set_he_txbf_conf(arvif);
4805 			if (ret)
4806 				ath12k_warn(ar->ab,
4807 					    "failed to set HE TXBF config for vdev: %d\n",
4808 					    arvif->vdev_id);
4809 
4810 			ret = ath12k_mac_set_eht_txbf_conf(arvif);
4811 			if (ret)
4812 				ath12k_warn(ar->ab,
4813 					    "failed to set EHT TXBF config for vdev: %d\n",
4814 					    arvif->vdev_id);
4815 		}
4816 		ath12k_control_beaconing(arvif, info);
4817 
4818 		if (arvif->is_up && info->he_support &&
4819 		    info->he_oper.params) {
4820 			/* TODO: Extend to support 1024 BA Bitmap size */
4821 			ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
4822 							    WMI_VDEV_PARAM_BA_MODE,
4823 							    WMI_BA_MODE_BUFFER_SIZE_256);
4824 			if (ret)
4825 				ath12k_warn(ar->ab,
4826 					    "failed to set BA BUFFER SIZE 256 for vdev: %d\n",
4827 					    arvif->vdev_id);
4828 
4829 			param_id = WMI_VDEV_PARAM_HEOPS_0_31;
4830 			param_value = info->he_oper.params;
4831 			ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
4832 							    param_id, param_value);
4833 			ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
4834 				   "he oper param: %x set for VDEV: %d\n",
4835 				   param_value, arvif->vdev_id);
4836 
4837 			if (ret)
4838 				ath12k_warn(ar->ab, "Failed to set he oper params %x for VDEV %d: %i\n",
4839 					    param_value, arvif->vdev_id, ret);
4840 		}
4841 	}
4842 
4843 	if (changed & BSS_CHANGED_ERP_CTS_PROT) {
4844 		u32 cts_prot;
4845 
4846 		cts_prot = !!(info->use_cts_prot);
4847 		param_id = WMI_VDEV_PARAM_PROTECTION_MODE;
4848 
4849 		if (arvif->is_started) {
4850 			ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
4851 							    param_id, cts_prot);
4852 			if (ret)
4853 				ath12k_warn(ar->ab, "Failed to set CTS prot for VDEV: %d\n",
4854 					    arvif->vdev_id);
4855 			else
4856 				ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "Set CTS prot: %d for VDEV: %d\n",
4857 					   cts_prot, arvif->vdev_id);
4858 		} else {
4859 			ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "defer protection mode setup, vdev is not ready yet\n");
4860 		}
4861 	}
4862 
4863 	if (changed & BSS_CHANGED_ERP_SLOT) {
4864 		u32 slottime;
4865 
4866 		if (info->use_short_slot)
4867 			slottime = WMI_VDEV_SLOT_TIME_SHORT; /* 9us */
4868 
4869 		else
4870 			slottime = WMI_VDEV_SLOT_TIME_LONG; /* 20us */
4871 
4872 		param_id = WMI_VDEV_PARAM_SLOT_TIME;
4873 		ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
4874 						    param_id, slottime);
4875 		if (ret)
4876 			ath12k_warn(ar->ab, "Failed to set erp slot for VDEV: %d\n",
4877 				    arvif->vdev_id);
4878 		else
4879 			ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
4880 				   "Set slottime: %d for VDEV: %d\n",
4881 				   slottime, arvif->vdev_id);
4882 	}
4883 
4884 	if (changed & BSS_CHANGED_ERP_PREAMBLE) {
4885 		u32 preamble;
4886 
4887 		if (info->use_short_preamble)
4888 			preamble = WMI_VDEV_PREAMBLE_SHORT;
4889 		else
4890 			preamble = WMI_VDEV_PREAMBLE_LONG;
4891 
4892 		param_id = WMI_VDEV_PARAM_PREAMBLE;
4893 		ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
4894 						    param_id, preamble);
4895 		if (ret)
4896 			ath12k_warn(ar->ab, "Failed to set preamble for VDEV: %d\n",
4897 				    arvif->vdev_id);
4898 		else
4899 			ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
4900 				   "Set preamble: %d for VDEV: %d\n",
4901 				   preamble, arvif->vdev_id);
4902 	}
4903 
4904 	if (changed & BSS_CHANGED_ASSOC) {
4905 		if (vif->cfg.assoc)
4906 			ath12k_bss_assoc(ar, arvif, info);
4907 		else
4908 			ath12k_bss_disassoc(ar, arvif);
4909 	}
4910 
4911 	if (changed & BSS_CHANGED_TXPOWER) {
4912 		ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac vdev_id %i txpower %d\n",
4913 			   arvif->vdev_id, info->txpower);
4914 
4915 		arvif->txpower = info->txpower;
4916 		ath12k_mac_txpower_recalc(ar);
4917 	}
4918 
4919 	if (changed & BSS_CHANGED_MCAST_RATE &&
4920 	    !ath12k_mac_vif_link_chan(vif, arvif->link_id, &def)) {
4921 		band = def.chan->band;
4922 		mcast_rate = info->mcast_rate[band];
4923 
4924 		if (mcast_rate > 0) {
4925 			rateidx = mcast_rate - 1;
4926 		} else {
4927 			if (info->basic_rates)
4928 				rateidx = __ffs(info->basic_rates);
4929 			else
4930 				rateidx = 0;
4931 		}
4932 
4933 		if (ar->pdev->cap.supported_bands & WMI_HOST_WLAN_5GHZ_CAP)
4934 			rateidx += ATH12K_MAC_FIRST_OFDM_RATE_IDX;
4935 
4936 		bitrate = ath12k_legacy_rates[rateidx].bitrate;
4937 		hw_value = ath12k_legacy_rates[rateidx].hw_value;
4938 
4939 		if (ath12k_mac_bitrate_is_cck(bitrate))
4940 			preamble = WMI_RATE_PREAMBLE_CCK;
4941 		else
4942 			preamble = WMI_RATE_PREAMBLE_OFDM;
4943 
4944 		rate = ATH12K_HW_RATE_CODE(hw_value, 0, preamble);
4945 
4946 		ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
4947 			   "mac vdev %d mcast_rate %x\n",
4948 			   arvif->vdev_id, rate);
4949 
4950 		vdev_param = WMI_VDEV_PARAM_MCAST_DATA_RATE;
4951 		ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
4952 						    vdev_param, rate);
4953 		if (ret)
4954 			ath12k_warn(ar->ab,
4955 				    "failed to set mcast rate on vdev %i: %d\n",
4956 				    arvif->vdev_id,  ret);
4957 
4958 		vdev_param = WMI_VDEV_PARAM_BCAST_DATA_RATE;
4959 		ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
4960 						    vdev_param, rate);
4961 		if (ret)
4962 			ath12k_warn(ar->ab,
4963 				    "failed to set bcast rate on vdev %i: %d\n",
4964 				    arvif->vdev_id,  ret);
4965 	}
4966 
4967 	if (changed & BSS_CHANGED_BASIC_RATES &&
4968 	    !ath12k_mac_vif_link_chan(vif, arvif->link_id, &def))
4969 		ath12k_recalculate_mgmt_rate(ar, arvif, &def);
4970 
4971 	if (changed & BSS_CHANGED_TWT) {
4972 		if (info->twt_requester || info->twt_responder)
4973 			ath12k_wmi_send_twt_enable_cmd(ar, ar->pdev->pdev_id);
4974 		else
4975 			ath12k_wmi_send_twt_disable_cmd(ar, ar->pdev->pdev_id);
4976 	}
4977 
4978 	if (changed & BSS_CHANGED_HE_OBSS_PD) {
4979 		if (vif->type == NL80211_IFTYPE_AP)
4980 			ath12k_mac_config_obss_pd(arvif, &info->he_obss_pd);
4981 		else
4982 			ath12k_wmi_send_obss_spr_cmd(ar, arvif->vdev_id,
4983 						     &info->he_obss_pd);
4984 	}
4985 
4986 	if (changed & BSS_CHANGED_HE_BSS_COLOR) {
4987 		if (vif->type == NL80211_IFTYPE_AP) {
4988 			ret = ath12k_wmi_obss_color_cfg_cmd(ar,
4989 							    arvif->vdev_id,
4990 							    info->he_bss_color.color,
4991 							    ATH12K_BSS_COLOR_AP_PERIODS,
4992 							    info->he_bss_color.enabled);
4993 			if (ret)
4994 				ath12k_warn(ar->ab, "failed to set bss color collision on vdev %u: %d\n",
4995 					    arvif->vdev_id,  ret);
4996 
4997 			param_id = WMI_VDEV_PARAM_BSS_COLOR;
4998 			if (info->he_bss_color.enabled)
4999 				param_value = info->he_bss_color.color <<
5000 					      IEEE80211_HE_OPERATION_BSS_COLOR_OFFSET;
5001 			else
5002 				param_value = IEEE80211_HE_OPERATION_BSS_COLOR_DISABLED;
5003 
5004 			ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
5005 							    param_id,
5006 							    param_value);
5007 			if (ret)
5008 				ath12k_warn(ar->ab, "failed to set bss color param on vdev %u: %d\n",
5009 					    arvif->vdev_id,  ret);
5010 			else
5011 				ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "bss color param 0x%x set on vdev %u\n",
5012 					   param_value, arvif->vdev_id);
5013 		} else if (vif->type == NL80211_IFTYPE_STATION) {
5014 			ret = ath12k_wmi_send_bss_color_change_enable_cmd(ar,
5015 									  arvif->vdev_id,
5016 									  1);
5017 			if (ret)
5018 				ath12k_warn(ar->ab, "failed to enable bss color change on vdev %i: %d\n",
5019 					    arvif->vdev_id,  ret);
5020 			ret = ath12k_wmi_obss_color_cfg_cmd(ar,
5021 							    arvif->vdev_id,
5022 							    0,
5023 							    ATH12K_BSS_COLOR_STA_PERIODS,
5024 							    1);
5025 			if (ret)
5026 				ath12k_warn(ar->ab, "failed to set bss color collision on vdev %i: %d\n",
5027 					    arvif->vdev_id,  ret);
5028 		}
5029 	}
5030 
5031 	ath12k_mac_fils_discovery(arvif, info);
5032 
5033 	if (changed & BSS_CHANGED_PS &&
5034 	    ar->ab->hw_params->supports_sta_ps) {
5035 		ahvif->ps = vif_cfg->ps;
5036 		ath12k_mac_vif_setup_ps(arvif);
5037 	}
5038 }
5039 
5040 static struct ath12k_vif_cache *ath12k_ahvif_get_link_cache(struct ath12k_vif *ahvif,
5041 							    u8 link_id)
5042 {
5043 	if (!ahvif->cache[link_id]) {
5044 		ahvif->cache[link_id] = kzalloc_obj(*ahvif->cache[0]);
5045 		if (ahvif->cache[link_id])
5046 			INIT_LIST_HEAD(&ahvif->cache[link_id]->key_conf.list);
5047 	}
5048 
5049 	return ahvif->cache[link_id];
5050 }
5051 
5052 static void ath12k_ahvif_put_link_key_cache(struct ath12k_vif_cache *cache)
5053 {
5054 	struct ath12k_key_conf *key_conf, *tmp;
5055 
5056 	if (!cache || list_empty(&cache->key_conf.list))
5057 		return;
5058 	list_for_each_entry_safe(key_conf, tmp, &cache->key_conf.list, list) {
5059 		list_del(&key_conf->list);
5060 		kfree(key_conf);
5061 	}
5062 }
5063 
5064 static void ath12k_ahvif_put_link_cache(struct ath12k_vif *ahvif, u8 link_id)
5065 {
5066 	if (link_id >= IEEE80211_MLD_MAX_NUM_LINKS)
5067 		return;
5068 
5069 	ath12k_ahvif_put_link_key_cache(ahvif->cache[link_id]);
5070 	kfree(ahvif->cache[link_id]);
5071 	ahvif->cache[link_id] = NULL;
5072 }
5073 
5074 void ath12k_mac_op_link_info_changed(struct ieee80211_hw *hw,
5075 				     struct ieee80211_vif *vif,
5076 				     struct ieee80211_bss_conf *info,
5077 				     u64 changed)
5078 {
5079 	struct ath12k *ar;
5080 	struct ath12k_vif *ahvif = ath12k_vif_to_ahvif(vif);
5081 	struct ath12k_vif_cache *cache;
5082 	struct ath12k_link_vif *arvif;
5083 	u8 link_id = info->link_id;
5084 
5085 	lockdep_assert_wiphy(hw->wiphy);
5086 
5087 	arvif = wiphy_dereference(hw->wiphy, ahvif->link[link_id]);
5088 
5089 	/* if the vdev is not created on a certain radio,
5090 	 * cache the info to be updated later on vdev creation
5091 	 */
5092 
5093 	if (!arvif || !arvif->is_created) {
5094 		cache = ath12k_ahvif_get_link_cache(ahvif, link_id);
5095 		if (!cache)
5096 			return;
5097 
5098 		cache->bss_conf_changed |= changed;
5099 
5100 		return;
5101 	}
5102 
5103 	ar = arvif->ar;
5104 
5105 	ath12k_mac_bss_info_changed(ar, arvif, info, changed);
5106 }
5107 EXPORT_SYMBOL(ath12k_mac_op_link_info_changed);
5108 
5109 static struct ath12k*
5110 ath12k_mac_select_scan_device(struct ieee80211_hw *hw,
5111 			      struct ieee80211_vif *vif,
5112 			      u32 center_freq)
5113 {
5114 	struct ath12k_hw *ah = hw->priv;
5115 	enum nl80211_band band;
5116 	struct ath12k *ar;
5117 	int i;
5118 
5119 	if (ah->num_radio == 1)
5120 		return ah->radio;
5121 
5122 	/* Currently mac80211 supports splitting scan requests into
5123 	 * multiple scan requests per band.
5124 	 * Loop through first channel and determine the scan radio
5125 	 * TODO: There could be 5 GHz low/high channels in that case
5126 	 * split the hw request and perform multiple scans
5127 	 */
5128 
5129 	if (center_freq < ATH12K_MIN_5GHZ_FREQ)
5130 		band = NL80211_BAND_2GHZ;
5131 	else if (center_freq < ATH12K_MIN_6GHZ_FREQ)
5132 		band = NL80211_BAND_5GHZ;
5133 	else
5134 		band = NL80211_BAND_6GHZ;
5135 
5136 	for_each_ar(ah, ar, i) {
5137 		if (ar->mac.sbands[band].channels &&
5138 		    center_freq >= KHZ_TO_MHZ(ar->freq_range.start_freq) &&
5139 		    center_freq <= KHZ_TO_MHZ(ar->freq_range.end_freq))
5140 			return ar;
5141 	}
5142 
5143 	return NULL;
5144 }
5145 
5146 void __ath12k_mac_scan_finish(struct ath12k *ar)
5147 {
5148 	struct ieee80211_hw *hw = ath12k_ar_to_hw(ar);
5149 
5150 	lockdep_assert_held(&ar->data_lock);
5151 
5152 	switch (ar->scan.state) {
5153 	case ATH12K_SCAN_IDLE:
5154 		break;
5155 	case ATH12K_SCAN_RUNNING:
5156 	case ATH12K_SCAN_ABORTING:
5157 		if (ar->scan.is_roc && ar->scan.roc_notify)
5158 			ieee80211_remain_on_channel_expired(hw);
5159 		fallthrough;
5160 	case ATH12K_SCAN_STARTING:
5161 		cancel_delayed_work(&ar->scan.timeout);
5162 		complete_all(&ar->scan.completed);
5163 		wiphy_work_queue(ar->ah->hw->wiphy, &ar->scan.vdev_clean_wk);
5164 		break;
5165 	}
5166 }
5167 
5168 void ath12k_mac_scan_finish(struct ath12k *ar)
5169 {
5170 	spin_lock_bh(&ar->data_lock);
5171 	__ath12k_mac_scan_finish(ar);
5172 	spin_unlock_bh(&ar->data_lock);
5173 }
5174 
5175 static int ath12k_scan_stop(struct ath12k *ar)
5176 {
5177 	struct ath12k_wmi_scan_cancel_arg arg = {
5178 		.req_type = WLAN_SCAN_CANCEL_SINGLE,
5179 		.scan_id = ATH12K_SCAN_ID,
5180 	};
5181 	int ret;
5182 
5183 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
5184 
5185 	/* TODO: Fill other STOP Params */
5186 	arg.pdev_id = ar->pdev->pdev_id;
5187 
5188 	ret = ath12k_wmi_send_scan_stop_cmd(ar, &arg);
5189 	if (ret) {
5190 		ath12k_warn(ar->ab, "failed to stop wmi scan: %d\n", ret);
5191 		goto out;
5192 	}
5193 
5194 	ret = wait_for_completion_timeout(&ar->scan.completed, 3 * HZ);
5195 	if (ret == 0) {
5196 		ath12k_warn(ar->ab,
5197 			    "failed to receive scan abort comple: timed out\n");
5198 		ret = -ETIMEDOUT;
5199 	} else if (ret > 0) {
5200 		ret = 0;
5201 	}
5202 
5203 out:
5204 	/* Scan state should be updated in scan completion worker but in
5205 	 * case firmware fails to deliver the event (for whatever reason)
5206 	 * it is desired to clean up scan state anyway. Firmware may have
5207 	 * just dropped the scan completion event delivery due to transport
5208 	 * pipe being overflown with data and/or it can recover on its own
5209 	 * before next scan request is submitted.
5210 	 */
5211 	spin_lock_bh(&ar->data_lock);
5212 	if (ret)
5213 		__ath12k_mac_scan_finish(ar);
5214 	spin_unlock_bh(&ar->data_lock);
5215 
5216 	return ret;
5217 }
5218 
5219 static void ath12k_scan_abort(struct ath12k *ar)
5220 {
5221 	int ret;
5222 
5223 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
5224 
5225 	spin_lock_bh(&ar->data_lock);
5226 
5227 	switch (ar->scan.state) {
5228 	case ATH12K_SCAN_IDLE:
5229 		/* This can happen if timeout worker kicked in and called
5230 		 * abortion while scan completion was being processed.
5231 		 */
5232 		break;
5233 	case ATH12K_SCAN_STARTING:
5234 	case ATH12K_SCAN_ABORTING:
5235 		ath12k_warn(ar->ab, "refusing scan abortion due to invalid scan state: %d\n",
5236 			    ar->scan.state);
5237 		break;
5238 	case ATH12K_SCAN_RUNNING:
5239 		ar->scan.state = ATH12K_SCAN_ABORTING;
5240 		spin_unlock_bh(&ar->data_lock);
5241 
5242 		ret = ath12k_scan_stop(ar);
5243 		if (ret)
5244 			ath12k_warn(ar->ab, "failed to abort scan: %d\n", ret);
5245 
5246 		spin_lock_bh(&ar->data_lock);
5247 		break;
5248 	}
5249 
5250 	spin_unlock_bh(&ar->data_lock);
5251 }
5252 
5253 static void ath12k_scan_timeout_work(struct work_struct *work)
5254 {
5255 	struct ath12k *ar = container_of(work, struct ath12k,
5256 					 scan.timeout.work);
5257 
5258 	wiphy_lock(ath12k_ar_to_hw(ar)->wiphy);
5259 	ath12k_scan_abort(ar);
5260 	wiphy_unlock(ath12k_ar_to_hw(ar)->wiphy);
5261 }
5262 
5263 static void ath12k_mac_scan_send_complete(struct ath12k *ar,
5264 					  struct cfg80211_scan_info *info)
5265 {
5266 	struct ath12k_hw *ah = ar->ah;
5267 	struct ath12k *partner_ar;
5268 	int i;
5269 
5270 	lockdep_assert_wiphy(ah->hw->wiphy);
5271 
5272 	for_each_ar(ah, partner_ar, i)
5273 		if (partner_ar != ar &&
5274 		    partner_ar->scan.state == ATH12K_SCAN_RUNNING)
5275 			return;
5276 
5277 	ieee80211_scan_completed(ah->hw, info);
5278 }
5279 
5280 static void ath12k_scan_vdev_clean_work(struct wiphy *wiphy, struct wiphy_work *work)
5281 {
5282 	struct ath12k *ar = container_of(work, struct ath12k,
5283 					 scan.vdev_clean_wk);
5284 	struct ath12k_hw *ah = ar->ah;
5285 	struct ath12k_link_vif *arvif;
5286 
5287 	lockdep_assert_wiphy(wiphy);
5288 
5289 	arvif = ar->scan.arvif;
5290 
5291 	/* The scan vdev has already been deleted. This can occur when a
5292 	 * new scan request is made on the same vif with a different
5293 	 * frequency, causing the scan arvif to move from one radio to
5294 	 * another. Or, scan was abrupted and via remove interface, the
5295 	 * arvif is already deleted. Alternatively, if the scan vdev is not
5296 	 * being used as an actual vdev, then do not delete it.
5297 	 */
5298 	if (!arvif || arvif->is_started)
5299 		goto work_complete;
5300 
5301 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac clean scan vdev (link id %u)",
5302 		   arvif->link_id);
5303 
5304 	ath12k_mac_remove_link_interface(ah->hw, arvif);
5305 	ath12k_mac_unassign_link_vif(arvif);
5306 
5307 work_complete:
5308 	spin_lock_bh(&ar->data_lock);
5309 	ar->scan.arvif = NULL;
5310 	if (!ar->scan.is_roc) {
5311 		struct cfg80211_scan_info info = {
5312 			.aborted = ((ar->scan.state ==
5313 				    ATH12K_SCAN_ABORTING) ||
5314 				    (ar->scan.state ==
5315 				    ATH12K_SCAN_STARTING)),
5316 		};
5317 
5318 		ath12k_mac_scan_send_complete(ar, &info);
5319 	}
5320 
5321 	ar->scan.state = ATH12K_SCAN_IDLE;
5322 	ar->scan_channel = NULL;
5323 	ar->scan.roc_freq = 0;
5324 	spin_unlock_bh(&ar->data_lock);
5325 }
5326 
5327 static int ath12k_start_scan(struct ath12k *ar,
5328 			     struct ath12k_wmi_scan_req_arg *arg)
5329 {
5330 	int ret;
5331 
5332 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
5333 
5334 	ret = ath12k_wmi_send_scan_start_cmd(ar, arg);
5335 	if (ret)
5336 		return ret;
5337 
5338 	ret = wait_for_completion_timeout(&ar->scan.started, 1 * HZ);
5339 	if (ret == 0) {
5340 		ret = ath12k_scan_stop(ar);
5341 		if (ret)
5342 			ath12k_warn(ar->ab, "failed to stop scan: %d\n", ret);
5343 
5344 		return -ETIMEDOUT;
5345 	}
5346 
5347 	/* If we failed to start the scan, return error code at
5348 	 * this point.  This is probably due to some issue in the
5349 	 * firmware, but no need to wedge the driver due to that...
5350 	 */
5351 	spin_lock_bh(&ar->data_lock);
5352 	if (ar->scan.state == ATH12K_SCAN_IDLE) {
5353 		spin_unlock_bh(&ar->data_lock);
5354 		return -EINVAL;
5355 	}
5356 	spin_unlock_bh(&ar->data_lock);
5357 
5358 	return 0;
5359 }
5360 
5361 int ath12k_mac_get_fw_stats(struct ath12k *ar,
5362 			    struct ath12k_fw_stats_req_params *param)
5363 {
5364 	struct ath12k_base *ab = ar->ab;
5365 	struct ath12k_hw *ah = ath12k_ar_to_ah(ar);
5366 	unsigned long time_left;
5367 	int ret;
5368 
5369 	guard(mutex)(&ah->hw_mutex);
5370 
5371 	if (ah->state != ATH12K_HW_STATE_ON)
5372 		return -ENETDOWN;
5373 
5374 	reinit_completion(&ar->fw_stats_complete);
5375 	reinit_completion(&ar->fw_stats_done);
5376 
5377 	ret = ath12k_wmi_send_stats_request_cmd(ar, param->stats_id,
5378 						param->vdev_id, param->pdev_id);
5379 	if (ret) {
5380 		ath12k_warn(ab, "failed to request fw stats: %d\n", ret);
5381 		return ret;
5382 	}
5383 
5384 	ath12k_dbg(ab, ATH12K_DBG_WMI,
5385 		   "get fw stat pdev id %d vdev id %d stats id 0x%x\n",
5386 		   param->pdev_id, param->vdev_id, param->stats_id);
5387 
5388 	time_left = wait_for_completion_timeout(&ar->fw_stats_complete, 1 * HZ);
5389 	if (!time_left) {
5390 		ath12k_warn(ab, "time out while waiting for get fw stats\n");
5391 		return -ETIMEDOUT;
5392 	}
5393 
5394 	/* Firmware sends WMI_UPDATE_STATS_EVENTID back-to-back
5395 	 * when stats data buffer limit is reached. fw_stats_complete
5396 	 * is completed once host receives first event from firmware, but
5397 	 * still there could be more events following. Below is to wait
5398 	 * until firmware completes sending all the events.
5399 	 */
5400 	time_left = wait_for_completion_timeout(&ar->fw_stats_done, 3 * HZ);
5401 	if (!time_left) {
5402 		ath12k_warn(ab, "time out while waiting for fw stats done\n");
5403 		return -ETIMEDOUT;
5404 	}
5405 
5406 	return 0;
5407 }
5408 
5409 int ath12k_mac_op_get_txpower(struct ieee80211_hw *hw,
5410 			      struct ieee80211_vif *vif,
5411 			      unsigned int link_id,
5412 			      int *dbm)
5413 {
5414 	struct ath12k_vif *ahvif = ath12k_vif_to_ahvif(vif);
5415 	struct ath12k_fw_stats_req_params params = {};
5416 	struct ath12k_fw_stats_pdev *pdev;
5417 	struct ath12k_hw *ah = hw->priv;
5418 	struct ath12k_link_vif *arvif;
5419 	struct ath12k_base *ab;
5420 	struct ath12k *ar;
5421 	int ret;
5422 
5423 	/* Final Tx power is minimum of Target Power, CTL power, Regulatory
5424 	 * Power, PSD EIRP Power. We just know the Regulatory power from the
5425 	 * regulatory rules obtained. FW knows all these power and sets the min
5426 	 * of these. Hence, we request the FW pdev stats in which FW reports
5427 	 * the minimum of all vdev's channel Tx power.
5428 	 */
5429 	lockdep_assert_wiphy(hw->wiphy);
5430 
5431 	arvif = wiphy_dereference(ah->hw->wiphy, ahvif->link[link_id]);
5432 	if (!arvif || !arvif->ar)
5433 		return -EINVAL;
5434 
5435 	ar = arvif->ar;
5436 	ab = ar->ab;
5437 	if (ah->state != ATH12K_HW_STATE_ON)
5438 		goto err_fallback;
5439 
5440 	if (test_bit(ATH12K_FLAG_CAC_RUNNING, &ar->dev_flags))
5441 		return -EAGAIN;
5442 
5443 	/* Limit the requests to Firmware for fetching the tx power */
5444 	if (ar->chan_tx_pwr != ATH12K_PDEV_TX_POWER_INVALID &&
5445 	    time_before(jiffies,
5446 			msecs_to_jiffies(ATH12K_PDEV_TX_POWER_REFRESH_TIME_MSECS) +
5447 					 ar->last_tx_power_update))
5448 		goto send_tx_power;
5449 
5450 	params.pdev_id = ath12k_mac_get_target_pdev_id(ar);
5451 	params.vdev_id = arvif->vdev_id;
5452 	params.stats_id = WMI_REQUEST_PDEV_STAT;
5453 	ret = ath12k_mac_get_fw_stats(ar, &params);
5454 	if (ret) {
5455 		ath12k_warn(ab, "failed to request fw pdev stats: %d\n", ret);
5456 		goto err_fallback;
5457 	}
5458 
5459 	spin_lock_bh(&ar->data_lock);
5460 	pdev = list_first_entry_or_null(&ar->fw_stats.pdevs,
5461 					struct ath12k_fw_stats_pdev, list);
5462 	if (!pdev) {
5463 		spin_unlock_bh(&ar->data_lock);
5464 		goto err_fallback;
5465 	}
5466 
5467 	/* tx power reported by firmware is in units of 0.5 dBm */
5468 	ar->chan_tx_pwr = pdev->chan_tx_power / 2;
5469 	spin_unlock_bh(&ar->data_lock);
5470 	ar->last_tx_power_update = jiffies;
5471 	ath12k_fw_stats_reset(ar);
5472 
5473 send_tx_power:
5474 	*dbm = ar->chan_tx_pwr;
5475 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "txpower fetched from firmware %d dBm\n",
5476 		   *dbm);
5477 	return 0;
5478 
5479 err_fallback:
5480 	/* We didn't get txpower from FW. Hence, relying on vif->bss_conf.txpower */
5481 	*dbm = vif->bss_conf.txpower;
5482 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "txpower from firmware NaN, reported %d dBm\n",
5483 		   *dbm);
5484 	return 0;
5485 }
5486 EXPORT_SYMBOL(ath12k_mac_op_get_txpower);
5487 
5488 static u8
5489 ath12k_mac_find_link_id_by_ar(struct ath12k_vif *ahvif, struct ath12k *ar)
5490 {
5491 	struct ath12k_link_vif *arvif;
5492 	struct ath12k_hw *ah = ahvif->ah;
5493 	unsigned long links = ahvif->links_map;
5494 	unsigned long scan_links_map;
5495 	u8 link_id;
5496 
5497 	lockdep_assert_wiphy(ah->hw->wiphy);
5498 
5499 	for_each_set_bit(link_id, &links, ATH12K_NUM_MAX_LINKS) {
5500 		arvif = wiphy_dereference(ah->hw->wiphy, ahvif->link[link_id]);
5501 
5502 		if (!arvif || !arvif->is_created)
5503 			continue;
5504 
5505 		if (ar == arvif->ar)
5506 			return link_id;
5507 	}
5508 
5509 	/* input ar is not assigned to any of the links of ML VIF, use next
5510 	 * available scan link for scan vdev creation. There are cases where
5511 	 * single scan req needs to be split in driver and initiate separate
5512 	 * scan requests to firmware based on device.
5513 	 */
5514 
5515 	 /* Unset all non-scan links (0-14) of scan_links_map so that ffs() will
5516 	  * choose an available link among scan links (i.e link id >= 15)
5517 	  */
5518 	scan_links_map = ~ahvif->links_map & ATH12K_SCAN_LINKS_MASK;
5519 	if (scan_links_map)
5520 		return __ffs(scan_links_map);
5521 
5522 	return ATH12K_FIRST_SCAN_LINK;
5523 }
5524 
5525 static int ath12k_mac_initiate_hw_scan(struct ieee80211_hw *hw,
5526 				       struct ieee80211_vif *vif,
5527 				       struct ieee80211_scan_request *hw_req,
5528 				       int n_channels,
5529 				       struct ieee80211_channel **chan_list,
5530 				       struct ath12k *ar)
5531 {
5532 	struct ath12k_hw *ah = ath12k_hw_to_ah(hw);
5533 	struct ath12k_vif *ahvif = ath12k_vif_to_ahvif(vif);
5534 	struct ath12k_link_vif *arvif;
5535 	struct cfg80211_scan_request *req = &hw_req->req;
5536 	struct ath12k_wmi_scan_req_arg *arg = NULL;
5537 	u8 link_id;
5538 	int ret;
5539 	int i;
5540 	bool create = true;
5541 
5542 	lockdep_assert_wiphy(hw->wiphy);
5543 
5544 	arvif = &ahvif->deflink;
5545 
5546 	/* check if any of the links of ML VIF is already started on
5547 	 * radio(ar) corresponding to given scan frequency and use it,
5548 	 * if not use scan link (link id >= 15) for scan purpose.
5549 	 */
5550 	link_id = ath12k_mac_find_link_id_by_ar(ahvif, ar);
5551 	/* All scan links are occupied. ideally this shouldn't happen as
5552 	 * mac80211 won't schedule scan for same band until ongoing scan is
5553 	 * completed, don't try to exceed max links just in case if it happens.
5554 	 */
5555 	if (link_id >= ATH12K_NUM_MAX_LINKS)
5556 		return -EBUSY;
5557 
5558 	arvif = ath12k_mac_assign_link_vif(ah, vif, link_id);
5559 
5560 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac link ID %d selected for scan",
5561 		   arvif->link_id);
5562 
5563 	/* If the vif is already assigned to a specific vdev of an ar,
5564 	 * check whether its already started, vdev which is started
5565 	 * are not allowed to switch to a new radio.
5566 	 * If the vdev is not started, but was earlier created on a
5567 	 * different ar, delete that vdev and create a new one. We don't
5568 	 * delete at the scan stop as an optimization to avoid redundant
5569 	 * delete-create vdev's for the same ar, in case the request is
5570 	 * always on the same band for the vif
5571 	 */
5572 	if (arvif->is_created) {
5573 		if (WARN_ON(!arvif->ar))
5574 			return -EINVAL;
5575 
5576 		if (ar != arvif->ar && arvif->is_started)
5577 			return -EINVAL;
5578 
5579 		if (ar != arvif->ar) {
5580 			ath12k_mac_remove_link_interface(hw, arvif);
5581 			ath12k_mac_unassign_link_vif(arvif);
5582 		} else {
5583 			create = false;
5584 		}
5585 	}
5586 
5587 	if (create) {
5588 		/* Previous arvif would've been cleared in radio switch block
5589 		 * above, assign arvif again for create.
5590 		 */
5591 		arvif = ath12k_mac_assign_link_vif(ah, vif, link_id);
5592 
5593 		ret = ath12k_mac_vdev_create(ar, arvif);
5594 		if (ret) {
5595 			ath12k_warn(ar->ab, "unable to create scan vdev %d\n", ret);
5596 			ath12k_mac_unassign_link_vif(arvif);
5597 			return ret;
5598 		}
5599 	}
5600 
5601 	spin_lock_bh(&ar->data_lock);
5602 	switch (ar->scan.state) {
5603 	case ATH12K_SCAN_IDLE:
5604 		reinit_completion(&ar->scan.started);
5605 		reinit_completion(&ar->scan.completed);
5606 		ar->scan.state = ATH12K_SCAN_STARTING;
5607 		ar->scan.is_roc = false;
5608 		ar->scan.arvif = arvif;
5609 		ret = 0;
5610 		break;
5611 	case ATH12K_SCAN_STARTING:
5612 	case ATH12K_SCAN_RUNNING:
5613 	case ATH12K_SCAN_ABORTING:
5614 		ret = -EBUSY;
5615 		break;
5616 	}
5617 	spin_unlock_bh(&ar->data_lock);
5618 
5619 	if (ret)
5620 		goto exit;
5621 
5622 	arg = kzalloc_flex(*arg, chan_list, n_channels);
5623 	if (!arg) {
5624 		ret = -ENOMEM;
5625 		goto exit;
5626 	}
5627 
5628 	arg->num_chan = n_channels;
5629 
5630 	ath12k_wmi_start_scan_init(ar, arg);
5631 	arg->vdev_id = arvif->vdev_id;
5632 	arg->scan_id = ATH12K_SCAN_ID;
5633 
5634 	if (req->ie_len) {
5635 		arg->extraie.ptr = kmemdup(req->ie, req->ie_len, GFP_KERNEL);
5636 		if (!arg->extraie.ptr) {
5637 			ret = -ENOMEM;
5638 			goto exit;
5639 		}
5640 		arg->extraie.len = req->ie_len;
5641 	}
5642 
5643 	if (req->n_ssids) {
5644 		arg->num_ssids = req->n_ssids;
5645 		for (i = 0; i < arg->num_ssids; i++)
5646 			arg->ssid[i] = req->ssids[i];
5647 	} else {
5648 		arg->scan_f_passive = 1;
5649 	}
5650 
5651 	for (i = 0; i < arg->num_chan; i++)
5652 		arg->chan_list[i] = chan_list[i]->center_freq;
5653 
5654 	ret = ath12k_start_scan(ar, arg);
5655 	if (ret) {
5656 		if (ret == -EBUSY)
5657 			ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
5658 				   "scan engine is busy 11d state %d\n", ar->state_11d);
5659 		else
5660 			ath12k_warn(ar->ab, "failed to start hw scan: %d\n", ret);
5661 
5662 		spin_lock_bh(&ar->data_lock);
5663 		ar->scan.state = ATH12K_SCAN_IDLE;
5664 		spin_unlock_bh(&ar->data_lock);
5665 		goto exit;
5666 	}
5667 
5668 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac scan started");
5669 
5670 	/* Add a margin to account for event/command processing */
5671 	ieee80211_queue_delayed_work(ath12k_ar_to_hw(ar), &ar->scan.timeout,
5672 				     msecs_to_jiffies(arg->max_scan_time +
5673 						      ATH12K_MAC_SCAN_TIMEOUT_MSECS));
5674 
5675 exit:
5676 	if (arg) {
5677 		kfree(arg->extraie.ptr);
5678 		kfree(arg);
5679 	}
5680 
5681 	if (ar->state_11d == ATH12K_11D_PREPARING &&
5682 	    ahvif->vdev_type == WMI_VDEV_TYPE_STA &&
5683 	    ahvif->vdev_subtype == WMI_VDEV_SUBTYPE_NONE)
5684 		ath12k_mac_11d_scan_start(ar, arvif->vdev_id);
5685 
5686 	return ret;
5687 }
5688 
5689 int ath12k_mac_op_hw_scan(struct ieee80211_hw *hw,
5690 			  struct ieee80211_vif *vif,
5691 			  struct ieee80211_scan_request *hw_req)
5692 {
5693 	struct ath12k_vif *ahvif = ath12k_vif_to_ahvif(vif);
5694 	struct ieee80211_channel **chan_list, *chan;
5695 	struct ath12k_hw *ah = ath12k_hw_to_ah(hw);
5696 	unsigned long links_map, link_id;
5697 	struct ath12k_link_vif *arvif;
5698 	struct ath12k *ar, *scan_ar;
5699 	int i, j, ret = 0;
5700 
5701 	lockdep_assert_wiphy(hw->wiphy);
5702 
5703 	chan_list = kzalloc_objs(*chan_list, hw_req->req.n_channels);
5704 	if (!chan_list)
5705 		return -ENOMEM;
5706 
5707 	/* There could be channels that belong to multiple underlying radio
5708 	 * in same scan request as mac80211 sees it as single band. In that
5709 	 * case split the hw_req based on frequency range and schedule scans to
5710 	 * corresponding radio.
5711 	 */
5712 	for_each_ar(ah, ar, i) {
5713 		int n_chans = 0;
5714 
5715 		for (j = 0; j < hw_req->req.n_channels; j++) {
5716 			chan = hw_req->req.channels[j];
5717 			scan_ar = ath12k_mac_select_scan_device(hw, vif,
5718 								chan->center_freq);
5719 			if (!scan_ar) {
5720 				ath12k_hw_warn(ah, "unable to select scan device for freq %d\n",
5721 					       chan->center_freq);
5722 				ret = -EINVAL;
5723 				goto abort;
5724 			}
5725 			if (ar != scan_ar)
5726 				continue;
5727 
5728 			chan_list[n_chans++] = chan;
5729 		}
5730 		if (n_chans) {
5731 			ret = ath12k_mac_initiate_hw_scan(hw, vif, hw_req, n_chans,
5732 							  chan_list, ar);
5733 			if (ret)
5734 				goto abort;
5735 		}
5736 	}
5737 abort:
5738 	/* If any of the parallel scans initiated fails, abort all and
5739 	 * remove the scan interfaces created. Return complete scan
5740 	 * failure as mac80211 assumes this as single scan request.
5741 	 */
5742 	if (ret) {
5743 		ath12k_hw_warn(ah, "Scan failed %d , cleanup all scan vdevs\n", ret);
5744 		links_map = ahvif->links_map;
5745 		for_each_set_bit(link_id, &links_map, ATH12K_NUM_MAX_LINKS) {
5746 			arvif = wiphy_dereference(hw->wiphy, ahvif->link[link_id]);
5747 			if (!arvif)
5748 				continue;
5749 
5750 			ar = arvif->ar;
5751 			if (ar->scan.arvif == arvif) {
5752 				wiphy_work_cancel(hw->wiphy, &ar->scan.vdev_clean_wk);
5753 				spin_lock_bh(&ar->data_lock);
5754 				ar->scan.arvif = NULL;
5755 				ar->scan.state = ATH12K_SCAN_IDLE;
5756 				ar->scan_channel = NULL;
5757 				ar->scan.roc_freq = 0;
5758 				spin_unlock_bh(&ar->data_lock);
5759 			}
5760 			if (link_id >= ATH12K_FIRST_SCAN_LINK) {
5761 				ath12k_mac_remove_link_interface(hw, arvif);
5762 				ath12k_mac_unassign_link_vif(arvif);
5763 			}
5764 		}
5765 	}
5766 	kfree(chan_list);
5767 	return ret;
5768 }
5769 EXPORT_SYMBOL(ath12k_mac_op_hw_scan);
5770 
5771 void ath12k_mac_op_cancel_hw_scan(struct ieee80211_hw *hw,
5772 				  struct ieee80211_vif *vif)
5773 {
5774 	struct ath12k_vif *ahvif = ath12k_vif_to_ahvif(vif);
5775 	unsigned long link_id, links_map = ahvif->links_map;
5776 	struct ath12k_link_vif *arvif;
5777 	struct ath12k *ar;
5778 
5779 	lockdep_assert_wiphy(hw->wiphy);
5780 
5781 	for_each_set_bit(link_id, &links_map, ATH12K_NUM_MAX_LINKS) {
5782 		arvif = wiphy_dereference(hw->wiphy, ahvif->link[link_id]);
5783 		if (!arvif || !arvif->is_created ||
5784 		    arvif->ar->scan.arvif != arvif)
5785 			continue;
5786 
5787 		ar = arvif->ar;
5788 
5789 		ath12k_scan_abort(ar);
5790 
5791 		cancel_delayed_work_sync(&ar->scan.timeout);
5792 	}
5793 }
5794 EXPORT_SYMBOL(ath12k_mac_op_cancel_hw_scan);
5795 
5796 static int ath12k_install_key(struct ath12k_link_vif *arvif,
5797 			      struct ieee80211_key_conf *key,
5798 			      enum set_key_cmd cmd,
5799 			      const u8 *macaddr, u32 flags)
5800 {
5801 	int ret;
5802 	struct ath12k *ar = arvif->ar;
5803 	struct wmi_vdev_install_key_arg arg = {
5804 		.vdev_id = arvif->vdev_id,
5805 		.key_idx = key->keyidx,
5806 		.key_len = key->keylen,
5807 		.key_data = key->key,
5808 		.key_flags = flags,
5809 		.ieee80211_key_cipher = key->cipher,
5810 		.macaddr = macaddr,
5811 	};
5812 	struct ath12k_vif *ahvif = arvif->ahvif;
5813 
5814 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
5815 
5816 	if (test_bit(ATH12K_FLAG_HW_CRYPTO_DISABLED, &ar->ab->dev_flags))
5817 		return 0;
5818 
5819 	if (cmd == DISABLE_KEY) {
5820 		/* TODO: Check if FW expects  value other than NONE for del */
5821 		/* arg.key_cipher = WMI_CIPHER_NONE; */
5822 		arg.key_len = 0;
5823 		arg.key_data = NULL;
5824 		goto check_order;
5825 	}
5826 
5827 	switch (key->cipher) {
5828 	case WLAN_CIPHER_SUITE_CCMP:
5829 	case WLAN_CIPHER_SUITE_CCMP_256:
5830 		arg.key_cipher = WMI_CIPHER_AES_CCM;
5831 		key->flags |= IEEE80211_KEY_FLAG_GENERATE_IV_MGMT;
5832 		break;
5833 	case WLAN_CIPHER_SUITE_TKIP:
5834 		arg.key_cipher = WMI_CIPHER_TKIP;
5835 		arg.key_txmic_len = 8;
5836 		arg.key_rxmic_len = 8;
5837 		break;
5838 	case WLAN_CIPHER_SUITE_GCMP:
5839 	case WLAN_CIPHER_SUITE_GCMP_256:
5840 		arg.key_cipher = WMI_CIPHER_AES_GCM;
5841 		key->flags |= IEEE80211_KEY_FLAG_GENERATE_IV_MGMT;
5842 		break;
5843 	case WLAN_CIPHER_SUITE_AES_CMAC:
5844 		arg.key_cipher = WMI_CIPHER_AES_CMAC;
5845 		break;
5846 	case WLAN_CIPHER_SUITE_BIP_GMAC_128:
5847 	case WLAN_CIPHER_SUITE_BIP_GMAC_256:
5848 		arg.key_cipher = WMI_CIPHER_AES_GMAC;
5849 		break;
5850 	case WLAN_CIPHER_SUITE_BIP_CMAC_256:
5851 		arg.key_cipher = WMI_CIPHER_AES_CMAC;
5852 		break;
5853 	default:
5854 		ath12k_warn(ar->ab, "cipher %d is not supported\n", key->cipher);
5855 		return -EOPNOTSUPP;
5856 	}
5857 
5858 	if (test_bit(ATH12K_FLAG_RAW_MODE, &ar->ab->dev_flags))
5859 		key->flags |= IEEE80211_KEY_FLAG_GENERATE_IV |
5860 			      IEEE80211_KEY_FLAG_RESERVE_TAILROOM;
5861 
5862 check_order:
5863 	if (ahvif->vdev_type == WMI_VDEV_TYPE_STA &&
5864 	    arg.key_flags == WMI_KEY_GROUP) {
5865 		if (cmd == SET_KEY) {
5866 			if (arvif->pairwise_key_done) {
5867 				ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
5868 					   "vdev %u pairwise key done, go install group key\n",
5869 					   arg.vdev_id);
5870 				goto install;
5871 			} else {
5872 				/* WCN7850 firmware requires pairwise key to be installed
5873 				 * before group key. In case group key comes first, cache
5874 				 * it and return. Will revisit it once pairwise key gets
5875 				 * installed.
5876 				 */
5877 				arvif->group_key = arg;
5878 				arvif->group_key_valid = true;
5879 				ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
5880 					   "vdev %u group key before pairwise key, cache and skip\n",
5881 					   arg.vdev_id);
5882 
5883 				ret = 0;
5884 				goto out;
5885 			}
5886 		} else {
5887 			arvif->group_key_valid = false;
5888 		}
5889 	}
5890 
5891 install:
5892 	reinit_completion(&ar->install_key_done);
5893 
5894 	ret = ath12k_wmi_vdev_install_key(arvif->ar, &arg);
5895 	if (ret)
5896 		return ret;
5897 
5898 	if (!wait_for_completion_timeout(&ar->install_key_done, 1 * HZ))
5899 		return -ETIMEDOUT;
5900 
5901 	if (ether_addr_equal(arg.macaddr, arvif->bssid))
5902 		ahvif->dp_vif.key_cipher = arg.ieee80211_key_cipher;
5903 
5904 	if (ar->install_key_status) {
5905 		ret = -EINVAL;
5906 		goto out;
5907 	}
5908 
5909 	if (ahvif->vdev_type == WMI_VDEV_TYPE_STA &&
5910 	    arg.key_flags == WMI_KEY_PAIRWISE) {
5911 		if (cmd == SET_KEY) {
5912 			arvif->pairwise_key_done = true;
5913 			if (arvif->group_key_valid) {
5914 				/* Install cached GTK */
5915 				arvif->group_key_valid = false;
5916 				arg = arvif->group_key;
5917 				ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
5918 					   "vdev %u pairwise key done, group key ready, go install\n",
5919 					   arg.vdev_id);
5920 				goto install;
5921 			}
5922 		} else {
5923 			arvif->pairwise_key_done = false;
5924 		}
5925 	}
5926 
5927 out:
5928 	if (ret) {
5929 		/* In case of failure userspace may not do DISABLE_KEY
5930 		 * but triggers re-connection directly, so manually reset
5931 		 * status here.
5932 		 */
5933 		arvif->group_key_valid = false;
5934 		arvif->pairwise_key_done = false;
5935 	}
5936 
5937 	return ret;
5938 }
5939 
5940 static int ath12k_clear_peer_keys(struct ath12k_link_vif *arvif,
5941 				  const u8 *addr)
5942 {
5943 	struct ath12k *ar = arvif->ar;
5944 	struct ath12k_base *ab = ar->ab;
5945 	struct ath12k_dp_link_peer *peer;
5946 	int first_errno = 0;
5947 	int ret;
5948 	int i, len;
5949 	u32 flags = 0;
5950 	struct ath12k_dp *dp = ath12k_ab_to_dp(ab);
5951 	struct ieee80211_key_conf *keys[WMI_MAX_KEY_INDEX + 1] = {};
5952 
5953 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
5954 
5955 	spin_lock_bh(&dp->dp_lock);
5956 	peer = ath12k_dp_link_peer_find_by_vdev_and_addr(dp, arvif->vdev_id, addr);
5957 	if (!peer || !peer->dp_peer) {
5958 		spin_unlock_bh(&dp->dp_lock);
5959 		return -ENOENT;
5960 	}
5961 
5962 	len = ARRAY_SIZE(peer->dp_peer->keys);
5963 	for (i = 0; i < len; i++) {
5964 		if (!peer->dp_peer->keys[i])
5965 			continue;
5966 
5967 		keys[i] = peer->dp_peer->keys[i];
5968 		peer->dp_peer->keys[i] = NULL;
5969 	}
5970 
5971 	spin_unlock_bh(&dp->dp_lock);
5972 
5973 	for (i = 0; i < len; i++) {
5974 		if (!keys[i])
5975 			continue;
5976 
5977 		/* key flags are not required to delete the key */
5978 		ret = ath12k_install_key(arvif, keys[i],
5979 					 DISABLE_KEY, addr, flags);
5980 		if (ret < 0 && first_errno == 0)
5981 			first_errno = ret;
5982 
5983 		if (ret < 0)
5984 			ath12k_warn(ab, "failed to remove peer key %d: %d\n",
5985 				    i, ret);
5986 	}
5987 
5988 	return first_errno;
5989 }
5990 
5991 static int ath12k_mac_set_key(struct ath12k *ar, enum set_key_cmd cmd,
5992 			      struct ath12k_link_vif *arvif,
5993 			      struct ath12k_link_sta *arsta,
5994 			      struct ieee80211_key_conf *key)
5995 {
5996 	struct ieee80211_sta *sta = NULL;
5997 	struct ath12k_base *ab = ar->ab;
5998 	struct ath12k_dp_link_peer *peer;
5999 	struct ath12k_sta *ahsta;
6000 	const u8 *peer_addr;
6001 	int ret;
6002 	u32 flags = 0;
6003 	struct ath12k_dp *dp = ath12k_ab_to_dp(ab);
6004 
6005 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
6006 
6007 	if (arsta)
6008 		sta = ath12k_ahsta_to_sta(arsta->ahsta);
6009 
6010 	if (test_bit(ATH12K_FLAG_HW_CRYPTO_DISABLED, &ab->dev_flags))
6011 		return 1;
6012 
6013 	if (sta)
6014 		peer_addr = arsta->addr;
6015 	else
6016 		peer_addr = arvif->bssid;
6017 
6018 	key->hw_key_idx = key->keyidx;
6019 
6020 	/* the peer should not disappear in mid-way (unless FW goes awry) since
6021 	 * we already hold wiphy lock. we just make sure its there now.
6022 	 */
6023 	spin_lock_bh(&dp->dp_lock);
6024 	peer = ath12k_dp_link_peer_find_by_vdev_and_addr(dp, arvif->vdev_id,
6025 							 peer_addr);
6026 	if (!peer || !peer->dp_peer) {
6027 		spin_unlock_bh(&dp->dp_lock);
6028 
6029 		if (cmd == SET_KEY) {
6030 			ath12k_warn(ab, "cannot install key for non-existent peer %pM\n",
6031 				    peer_addr);
6032 			return -EOPNOTSUPP;
6033 		}
6034 
6035 		/* if the peer doesn't exist there is no key to disable
6036 		 * anymore
6037 		 */
6038 		return 0;
6039 	}
6040 
6041 	spin_unlock_bh(&dp->dp_lock);
6042 
6043 	if (key->flags & IEEE80211_KEY_FLAG_PAIRWISE)
6044 		flags = WMI_KEY_PAIRWISE;
6045 	else
6046 		flags = WMI_KEY_GROUP;
6047 
6048 	ret = ath12k_install_key(arvif, key, cmd, peer_addr, flags);
6049 	if (ret) {
6050 		ath12k_warn(ab, "ath12k_install_key failed (%d)\n", ret);
6051 		return ret;
6052 	}
6053 
6054 	ret = ath12k_dp_rx_peer_pn_replay_config(arvif, peer_addr, cmd, key);
6055 	if (ret) {
6056 		ath12k_warn(ab, "failed to offload PN replay detection %d\n", ret);
6057 		return ret;
6058 	}
6059 
6060 	spin_lock_bh(&dp->dp_lock);
6061 	peer = ath12k_dp_link_peer_find_by_vdev_and_addr(dp, arvif->vdev_id,
6062 							 peer_addr);
6063 	if (peer && peer->dp_peer && cmd == SET_KEY) {
6064 		peer->dp_peer->keys[key->keyidx] = key;
6065 		if (key->flags & IEEE80211_KEY_FLAG_PAIRWISE) {
6066 			peer->dp_peer->ucast_keyidx = key->keyidx;
6067 			peer->dp_peer->sec_type =
6068 					ath12k_dp_tx_get_encrypt_type(key->cipher);
6069 		} else {
6070 			peer->dp_peer->mcast_keyidx = key->keyidx;
6071 			peer->dp_peer->sec_type_grp =
6072 					ath12k_dp_tx_get_encrypt_type(key->cipher);
6073 		}
6074 	} else if (peer && peer->dp_peer && cmd == DISABLE_KEY) {
6075 		peer->dp_peer->keys[key->keyidx] = NULL;
6076 		if (key->flags & IEEE80211_KEY_FLAG_PAIRWISE)
6077 			peer->dp_peer->ucast_keyidx = 0;
6078 		else
6079 			peer->dp_peer->mcast_keyidx = 0;
6080 	} else if (!peer)
6081 		/* impossible unless FW goes crazy */
6082 		ath12k_warn(ab, "peer %pM disappeared!\n", peer_addr);
6083 
6084 	if (sta) {
6085 		ahsta = ath12k_sta_to_ahsta(sta);
6086 
6087 		switch (key->cipher) {
6088 		case WLAN_CIPHER_SUITE_TKIP:
6089 		case WLAN_CIPHER_SUITE_CCMP:
6090 		case WLAN_CIPHER_SUITE_CCMP_256:
6091 		case WLAN_CIPHER_SUITE_GCMP:
6092 		case WLAN_CIPHER_SUITE_GCMP_256:
6093 			if (cmd == SET_KEY)
6094 				ahsta->pn_type = HAL_PN_TYPE_WPA;
6095 			else
6096 				ahsta->pn_type = HAL_PN_TYPE_NONE;
6097 			break;
6098 		default:
6099 			ahsta->pn_type = HAL_PN_TYPE_NONE;
6100 			break;
6101 		}
6102 	}
6103 
6104 	spin_unlock_bh(&dp->dp_lock);
6105 
6106 	return 0;
6107 }
6108 
6109 static int ath12k_mac_update_key_cache(struct ath12k_vif_cache *cache,
6110 				       enum set_key_cmd cmd,
6111 				       struct ieee80211_sta *sta,
6112 				       struct ieee80211_key_conf *key)
6113 {
6114 	struct ath12k_key_conf *key_conf, *tmp;
6115 
6116 	list_for_each_entry_safe(key_conf, tmp, &cache->key_conf.list, list) {
6117 		if (key_conf->key != key)
6118 			continue;
6119 
6120 		/* If SET key entry is already present in cache, nothing to do,
6121 		 * just return
6122 		 */
6123 		if (cmd == SET_KEY)
6124 			return 0;
6125 
6126 		/* DEL key for an old SET key which driver hasn't flushed yet.
6127 		 */
6128 		list_del(&key_conf->list);
6129 		kfree(key_conf);
6130 	}
6131 
6132 	if (cmd == SET_KEY) {
6133 		key_conf = kzalloc_obj(*key_conf);
6134 
6135 		if (!key_conf)
6136 			return -ENOMEM;
6137 
6138 		key_conf->cmd = cmd;
6139 		key_conf->sta = sta;
6140 		key_conf->key = key;
6141 		list_add_tail(&key_conf->list,
6142 			      &cache->key_conf.list);
6143 	}
6144 
6145 	return 0;
6146 }
6147 
6148 int ath12k_mac_op_set_key(struct ieee80211_hw *hw, enum set_key_cmd cmd,
6149 			  struct ieee80211_vif *vif, struct ieee80211_sta *sta,
6150 			  struct ieee80211_key_conf *key)
6151 {
6152 	struct ath12k_vif *ahvif = ath12k_vif_to_ahvif(vif);
6153 	struct ath12k_link_vif *arvif;
6154 	struct ath12k_link_sta *arsta = NULL;
6155 	struct ath12k_vif_cache *cache;
6156 	struct ath12k_sta *ahsta;
6157 	unsigned long links;
6158 	u8 link_id;
6159 	int ret;
6160 
6161 	lockdep_assert_wiphy(hw->wiphy);
6162 
6163 	/* IGTK needs to be done in host software */
6164 	if (key->keyidx == 4 || key->keyidx == 5)
6165 		return 1;
6166 
6167 	if (key->keyidx > WMI_MAX_KEY_INDEX)
6168 		return -ENOSPC;
6169 
6170 	if (sta) {
6171 		ahsta = ath12k_sta_to_ahsta(sta);
6172 
6173 		/* For an ML STA Pairwise key is same for all associated link Stations,
6174 		 * hence do set key for all link STAs which are active.
6175 		 */
6176 		if (sta->mlo) {
6177 			links = ahsta->links_map;
6178 			for_each_set_bit(link_id, &links, IEEE80211_MLD_MAX_NUM_LINKS) {
6179 				arvif = wiphy_dereference(hw->wiphy,
6180 							  ahvif->link[link_id]);
6181 				arsta = wiphy_dereference(hw->wiphy,
6182 							  ahsta->link[link_id]);
6183 
6184 				if (WARN_ON(!arvif || !arsta))
6185 					/* arvif and arsta are expected to be valid when
6186 					 * STA is present.
6187 					 */
6188 					continue;
6189 
6190 				ret = ath12k_mac_set_key(arvif->ar, cmd, arvif,
6191 							 arsta, key);
6192 				if (ret)
6193 					break;
6194 			}
6195 
6196 			return 0;
6197 		}
6198 
6199 		arsta = &ahsta->deflink;
6200 		arvif = arsta->arvif;
6201 		if (WARN_ON(!arvif))
6202 			return -EINVAL;
6203 
6204 		ret = ath12k_mac_set_key(arvif->ar, cmd, arvif, arsta, key);
6205 		if (ret)
6206 			return ret;
6207 
6208 		return 0;
6209 	}
6210 
6211 	if (key->link_id >= 0 && key->link_id < IEEE80211_MLD_MAX_NUM_LINKS) {
6212 		link_id = key->link_id;
6213 		arvif = wiphy_dereference(hw->wiphy, ahvif->link[link_id]);
6214 	} else {
6215 		link_id = 0;
6216 		arvif = &ahvif->deflink;
6217 	}
6218 
6219 	if (!arvif || !arvif->is_created) {
6220 		cache = ath12k_ahvif_get_link_cache(ahvif, link_id);
6221 		if (!cache)
6222 			return -ENOSPC;
6223 
6224 		ret = ath12k_mac_update_key_cache(cache, cmd, sta, key);
6225 		if (ret)
6226 			return ret;
6227 
6228 		return 0;
6229 	}
6230 
6231 	ret = ath12k_mac_set_key(arvif->ar, cmd, arvif, NULL, key);
6232 	if (ret)
6233 		return ret;
6234 
6235 	return 0;
6236 }
6237 EXPORT_SYMBOL(ath12k_mac_op_set_key);
6238 
6239 static int
6240 ath12k_mac_bitrate_mask_num_vht_rates(struct ath12k *ar,
6241 				      enum nl80211_band band,
6242 				      const struct cfg80211_bitrate_mask *mask)
6243 {
6244 	int num_rates = 0;
6245 	int i;
6246 
6247 	for (i = 0; i < ARRAY_SIZE(mask->control[band].vht_mcs); i++)
6248 		num_rates += hweight16(mask->control[band].vht_mcs[i]);
6249 
6250 	return num_rates;
6251 }
6252 
6253 static int
6254 ath12k_mac_bitrate_mask_num_he_rates(struct ath12k *ar,
6255 				     enum nl80211_band band,
6256 				     const struct cfg80211_bitrate_mask *mask)
6257 {
6258 	int num_rates = 0;
6259 	int i;
6260 
6261 	for (i = 0; i < ARRAY_SIZE(mask->control[band].he_mcs); i++)
6262 		num_rates += hweight16(mask->control[band].he_mcs[i]);
6263 
6264 	return num_rates;
6265 }
6266 
6267 static int
6268 ath12k_mac_bitrate_mask_num_eht_rates(struct ath12k *ar,
6269 				      enum nl80211_band band,
6270 				      const struct cfg80211_bitrate_mask *mask)
6271 {
6272 	int num_rates = 0;
6273 	int i;
6274 
6275 	for (i = 0; i < ARRAY_SIZE(mask->control[band].eht_mcs); i++)
6276 		num_rates += hweight16(mask->control[band].eht_mcs[i]);
6277 
6278 	return num_rates;
6279 }
6280 
6281 static int
6282 ath12k_mac_set_peer_vht_fixed_rate(struct ath12k_link_vif *arvif,
6283 				   struct ath12k_link_sta *arsta,
6284 				   const struct cfg80211_bitrate_mask *mask,
6285 				   enum nl80211_band band)
6286 {
6287 	struct ath12k *ar = arvif->ar;
6288 	u8 vht_rate, nss;
6289 	u32 rate_code;
6290 	int ret, i;
6291 
6292 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
6293 
6294 	nss = 0;
6295 
6296 	for (i = 0; i < ARRAY_SIZE(mask->control[band].vht_mcs); i++) {
6297 		if (hweight16(mask->control[band].vht_mcs[i]) == 1) {
6298 			nss = i + 1;
6299 			vht_rate = ffs(mask->control[band].vht_mcs[i]) - 1;
6300 		}
6301 	}
6302 
6303 	if (!nss) {
6304 		ath12k_warn(ar->ab, "No single VHT Fixed rate found to set for %pM",
6305 			    arsta->addr);
6306 		return -EINVAL;
6307 	}
6308 
6309 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
6310 		   "Setting Fixed VHT Rate for peer %pM. Device will not switch to any other selected rates",
6311 		   arsta->addr);
6312 
6313 	rate_code = ATH12K_HW_RATE_CODE(vht_rate, nss - 1,
6314 					WMI_RATE_PREAMBLE_VHT);
6315 	ret = ath12k_wmi_set_peer_param(ar, arsta->addr,
6316 					arvif->vdev_id,
6317 					WMI_PEER_PARAM_FIXED_RATE,
6318 					rate_code);
6319 	if (ret)
6320 		ath12k_warn(ar->ab,
6321 			    "failed to update STA %pM Fixed Rate %d: %d\n",
6322 			     arsta->addr, rate_code, ret);
6323 
6324 	return ret;
6325 }
6326 
6327 static int
6328 ath12k_mac_set_peer_he_fixed_rate(struct ath12k_link_vif *arvif,
6329 				  struct ath12k_link_sta *arsta,
6330 				  const struct cfg80211_bitrate_mask *mask,
6331 				  enum nl80211_band band)
6332 {
6333 	struct ath12k *ar = arvif->ar;
6334 	u8 he_rate, nss;
6335 	u32 rate_code;
6336 	int ret, i;
6337 	struct ath12k_sta *ahsta = arsta->ahsta;
6338 	struct ieee80211_sta *sta;
6339 
6340 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
6341 
6342 	sta = ath12k_ahsta_to_sta(ahsta);
6343 	nss = 0;
6344 
6345 	for (i = 0; i < ARRAY_SIZE(mask->control[band].he_mcs); i++) {
6346 		if (hweight16(mask->control[band].he_mcs[i]) == 1) {
6347 			nss = i + 1;
6348 			he_rate = ffs(mask->control[band].he_mcs[i]) - 1;
6349 		}
6350 	}
6351 
6352 	if (!nss) {
6353 		ath12k_warn(ar->ab, "No single HE Fixed rate found to set for %pM",
6354 			    arsta->addr);
6355 		return -EINVAL;
6356 	}
6357 
6358 	/* Avoid updating invalid nss as fixed rate*/
6359 	if (nss > sta->deflink.rx_nss)
6360 		return -EINVAL;
6361 
6362 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
6363 		   "Setting Fixed HE Rate for peer %pM. Device will not switch to any other selected rates",
6364 		   arsta->addr);
6365 
6366 	rate_code = ATH12K_HW_RATE_CODE(he_rate, nss - 1,
6367 					WMI_RATE_PREAMBLE_HE);
6368 
6369 	ret = ath12k_wmi_set_peer_param(ar, arsta->addr,
6370 					arvif->vdev_id,
6371 					WMI_PEER_PARAM_FIXED_RATE,
6372 					rate_code);
6373 	if (ret)
6374 		ath12k_warn(ar->ab,
6375 			    "failed to update STA %pM Fixed Rate %d: %d\n",
6376 			    arsta->addr, rate_code, ret);
6377 
6378 	return ret;
6379 }
6380 
6381 static int
6382 ath12k_mac_set_peer_eht_fixed_rate(struct ath12k_link_vif *arvif,
6383 				   struct ath12k_link_sta *arsta,
6384 				   const struct cfg80211_bitrate_mask *mask,
6385 				   enum nl80211_band band)
6386 {
6387 	struct ath12k_sta *ahsta = arsta->ahsta;
6388 	struct ath12k *ar = arvif->ar;
6389 	struct ieee80211_sta *sta;
6390 	struct ieee80211_link_sta *link_sta;
6391 	u8 eht_rate, nss = 0;
6392 	u32 rate_code;
6393 	int ret, i;
6394 
6395 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
6396 
6397 	sta = ath12k_ahsta_to_sta(ahsta);
6398 
6399 	for (i = 0; i < ARRAY_SIZE(mask->control[band].eht_mcs); i++) {
6400 		if (hweight16(mask->control[band].eht_mcs[i]) == 1) {
6401 			nss = i + 1;
6402 			eht_rate = ffs(mask->control[band].eht_mcs[i]) - 1;
6403 		}
6404 	}
6405 
6406 	if (!nss) {
6407 		ath12k_warn(ar->ab, "No single EHT Fixed rate found to set for %pM\n",
6408 			    arsta->addr);
6409 		return -EINVAL;
6410 	}
6411 
6412 	/* Avoid updating invalid nss as fixed rate*/
6413 	link_sta = ath12k_mac_get_link_sta(arsta);
6414 	if (!link_sta || nss > link_sta->rx_nss) {
6415 		ath12k_warn(ar->ab,
6416 			    "unable to access link sta for sta %pM link %u or fixed nss of %u is not supported by sta\n",
6417 			    sta->addr, arsta->link_id, nss);
6418 		return -EINVAL;
6419 	}
6420 
6421 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
6422 		   "Setting Fixed EHT Rate for peer %pM. Device will not switch to any other selected rates\n",
6423 		   arsta->addr);
6424 
6425 	rate_code = ATH12K_HW_RATE_CODE(eht_rate, nss - 1,
6426 					WMI_RATE_PREAMBLE_EHT);
6427 
6428 	ret = ath12k_wmi_set_peer_param(ar, arsta->addr,
6429 					arvif->vdev_id,
6430 					WMI_PEER_PARAM_FIXED_RATE,
6431 					rate_code);
6432 	if (ret)
6433 		ath12k_warn(ar->ab,
6434 			    "failed to update STA %pM Fixed Rate %d: %d\n",
6435 			    arsta->addr, rate_code, ret);
6436 
6437 	return ret;
6438 }
6439 
6440 static int ath12k_mac_station_assoc(struct ath12k *ar,
6441 				    struct ath12k_link_vif *arvif,
6442 				    struct ath12k_link_sta *arsta,
6443 				    bool reassoc)
6444 {
6445 	struct ieee80211_vif *vif = ath12k_ahvif_to_vif(arvif->ahvif);
6446 	struct ieee80211_sta *sta = ath12k_ahsta_to_sta(arsta->ahsta);
6447 	struct ieee80211_link_sta *link_sta;
6448 	int ret;
6449 	struct cfg80211_chan_def def;
6450 	enum nl80211_band band;
6451 	struct cfg80211_bitrate_mask *mask;
6452 	u8 num_vht_rates, num_he_rates, num_eht_rates;
6453 	u8 link_id = arvif->link_id;
6454 
6455 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
6456 
6457 	if (WARN_ON(ath12k_mac_vif_link_chan(vif, arvif->link_id, &def)))
6458 		return -EPERM;
6459 
6460 	if (WARN_ON(!rcu_access_pointer(sta->link[link_id])))
6461 		return -EINVAL;
6462 
6463 	band = def.chan->band;
6464 	mask = &arvif->bitrate_mask;
6465 
6466 	struct ath12k_wmi_peer_assoc_arg *peer_arg __free(kfree) =
6467 		kzalloc_obj(*peer_arg);
6468 	if (!peer_arg)
6469 		return -ENOMEM;
6470 
6471 	ath12k_peer_assoc_prepare(ar, arvif, arsta, peer_arg, reassoc);
6472 
6473 	if (peer_arg->peer_nss < 1) {
6474 		ath12k_warn(ar->ab,
6475 			    "invalid peer NSS %d\n", peer_arg->peer_nss);
6476 		return -EINVAL;
6477 	}
6478 
6479 	peer_arg->is_assoc = true;
6480 	ret = ath12k_wmi_send_peer_assoc_cmd(ar, peer_arg);
6481 	if (ret) {
6482 		ath12k_warn(ar->ab, "failed to run peer assoc for STA %pM vdev %i: %d\n",
6483 			    arsta->addr, arvif->vdev_id, ret);
6484 		return ret;
6485 	}
6486 
6487 	if (!wait_for_completion_timeout(&ar->peer_assoc_done, 1 * HZ)) {
6488 		ath12k_warn(ar->ab, "failed to get peer assoc conf event for %pM vdev %i\n",
6489 			    arsta->addr, arvif->vdev_id);
6490 		return -ETIMEDOUT;
6491 	}
6492 
6493 	num_vht_rates = ath12k_mac_bitrate_mask_num_vht_rates(ar, band, mask);
6494 	num_he_rates = ath12k_mac_bitrate_mask_num_he_rates(ar, band, mask);
6495 	num_eht_rates = ath12k_mac_bitrate_mask_num_eht_rates(ar, band, mask);
6496 
6497 	/* If single VHT/HE/EHT rate is configured (by set_bitrate_mask()),
6498 	 * peer_assoc will disable VHT/HE/EHT. This is now enabled by a peer
6499 	 * specific fixed param.
6500 	 * Note that all other rates and NSS will be disabled for this peer.
6501 	 */
6502 	link_sta = ath12k_mac_get_link_sta(arsta);
6503 	if (!link_sta) {
6504 		ath12k_warn(ar->ab, "unable to access link sta in station assoc\n");
6505 		return -EINVAL;
6506 	}
6507 
6508 	spin_lock_bh(&ar->data_lock);
6509 	arsta->bw = ath12k_mac_ieee80211_sta_bw_to_wmi(ar, link_sta);
6510 	arsta->bw_prev = link_sta->bandwidth;
6511 	spin_unlock_bh(&ar->data_lock);
6512 
6513 	if (link_sta->vht_cap.vht_supported && num_vht_rates == 1) {
6514 		ret = ath12k_mac_set_peer_vht_fixed_rate(arvif, arsta, mask, band);
6515 	} else if (link_sta->he_cap.has_he && num_he_rates == 1) {
6516 		ret = ath12k_mac_set_peer_he_fixed_rate(arvif, arsta, mask, band);
6517 		if (ret)
6518 			return ret;
6519 	} else if (link_sta->eht_cap.has_eht && num_eht_rates == 1) {
6520 		ret = ath12k_mac_set_peer_eht_fixed_rate(arvif, arsta, mask, band);
6521 		if (ret)
6522 			return ret;
6523 	}
6524 
6525 	/* Re-assoc is run only to update supported rates for given station. It
6526 	 * doesn't make much sense to reconfigure the peer completely.
6527 	 */
6528 	if (reassoc)
6529 		return 0;
6530 
6531 	ret = ath12k_setup_peer_smps(ar, arvif, arsta->addr,
6532 				     &link_sta->ht_cap, &link_sta->he_6ghz_capa);
6533 	if (ret) {
6534 		ath12k_warn(ar->ab, "failed to setup peer SMPS for vdev %d: %d\n",
6535 			    arvif->vdev_id, ret);
6536 		return ret;
6537 	}
6538 
6539 	if (!sta->wme) {
6540 		arvif->num_legacy_stations++;
6541 		ret = ath12k_recalc_rtscts_prot(arvif);
6542 		if (ret)
6543 			return ret;
6544 	}
6545 
6546 	if (sta->wme && sta->uapsd_queues) {
6547 		ret = ath12k_peer_assoc_qos_ap(ar, arvif, arsta);
6548 		if (ret) {
6549 			ath12k_warn(ar->ab, "failed to set qos params for STA %pM for vdev %i: %d\n",
6550 				    arsta->addr, arvif->vdev_id, ret);
6551 			return ret;
6552 		}
6553 	}
6554 
6555 	return 0;
6556 }
6557 
6558 static int ath12k_mac_station_disassoc(struct ath12k *ar,
6559 				       struct ath12k_link_vif *arvif,
6560 				       struct ath12k_link_sta *arsta)
6561 {
6562 	struct ieee80211_sta *sta = ath12k_ahsta_to_sta(arsta->ahsta);
6563 
6564 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
6565 
6566 	if (!sta->wme) {
6567 		arvif->num_legacy_stations--;
6568 		return ath12k_recalc_rtscts_prot(arvif);
6569 	}
6570 
6571 	return 0;
6572 }
6573 
6574 static void ath12k_sta_rc_update_wk(struct wiphy *wiphy, struct wiphy_work *wk)
6575 {
6576 	struct ieee80211_link_sta *link_sta;
6577 	struct ath12k *ar;
6578 	struct ath12k_link_vif *arvif;
6579 	struct ieee80211_sta *sta;
6580 	struct cfg80211_chan_def def;
6581 	enum nl80211_band band;
6582 	const u8 *ht_mcs_mask;
6583 	const u16 *vht_mcs_mask;
6584 	const u16 *he_mcs_mask;
6585 	const u16 *eht_mcs_mask;
6586 	u32 changed, bw, nss, mac_nss, smps, bw_prev;
6587 	int err, num_vht_rates, num_he_rates, num_eht_rates;
6588 	const struct cfg80211_bitrate_mask *mask;
6589 	enum wmi_phy_mode peer_phymode;
6590 	struct ath12k_link_sta *arsta;
6591 	struct ieee80211_vif *vif;
6592 
6593 	lockdep_assert_wiphy(wiphy);
6594 
6595 	arsta = container_of(wk, struct ath12k_link_sta, update_wk);
6596 	sta = ath12k_ahsta_to_sta(arsta->ahsta);
6597 	arvif = arsta->arvif;
6598 	vif = ath12k_ahvif_to_vif(arvif->ahvif);
6599 	ar = arvif->ar;
6600 
6601 	if (WARN_ON(ath12k_mac_vif_link_chan(vif, arvif->link_id, &def)))
6602 		return;
6603 
6604 	band = def.chan->band;
6605 	ht_mcs_mask = arvif->bitrate_mask.control[band].ht_mcs;
6606 	vht_mcs_mask = arvif->bitrate_mask.control[band].vht_mcs;
6607 	he_mcs_mask = arvif->bitrate_mask.control[band].he_mcs;
6608 	eht_mcs_mask = arvif->bitrate_mask.control[band].eht_mcs;
6609 
6610 	spin_lock_bh(&ar->data_lock);
6611 
6612 	changed = arsta->changed;
6613 	arsta->changed = 0;
6614 
6615 	bw = arsta->bw;
6616 	bw_prev = arsta->bw_prev;
6617 	nss = arsta->nss;
6618 	smps = arsta->smps;
6619 
6620 	spin_unlock_bh(&ar->data_lock);
6621 
6622 	nss = max_t(u32, 1, nss);
6623 	mac_nss = max3(ath12k_mac_max_ht_nss(ht_mcs_mask),
6624 		       ath12k_mac_max_vht_nss(vht_mcs_mask),
6625 		       ath12k_mac_max_he_nss(he_mcs_mask));
6626 	mac_nss = max(mac_nss, ath12k_mac_max_eht_nss(eht_mcs_mask));
6627 	nss = min(nss, mac_nss);
6628 
6629 	struct ath12k_wmi_peer_assoc_arg *peer_arg __free(kfree) =
6630 					kzalloc_obj(*peer_arg);
6631 	if (!peer_arg)
6632 		return;
6633 
6634 	if (changed & IEEE80211_RC_BW_CHANGED) {
6635 		ath12k_peer_assoc_h_phymode(ar, arvif, arsta, peer_arg);
6636 		peer_phymode = peer_arg->peer_phymode;
6637 
6638 		if (bw > bw_prev) {
6639 			/* Phymode shows maximum supported channel width, if we
6640 			 * upgrade bandwidth then due to sanity check of firmware,
6641 			 * we have to send WMI_PEER_PHYMODE followed by
6642 			 * WMI_PEER_CHWIDTH
6643 			 */
6644 			ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac bandwidth upgrade for sta %pM new %d old %d\n",
6645 				   arsta->addr, bw, bw_prev);
6646 			err = ath12k_wmi_set_peer_param(ar, arsta->addr,
6647 							arvif->vdev_id, WMI_PEER_PHYMODE,
6648 							peer_phymode);
6649 			if (err) {
6650 				ath12k_warn(ar->ab, "failed to update STA %pM to peer phymode %d: %d\n",
6651 					    arsta->addr, peer_phymode, err);
6652 				return;
6653 			}
6654 			err = ath12k_wmi_set_peer_param(ar, arsta->addr,
6655 							arvif->vdev_id, WMI_PEER_CHWIDTH,
6656 							bw);
6657 			if (err)
6658 				ath12k_warn(ar->ab, "failed to update STA %pM to peer bandwidth %d: %d\n",
6659 					    arsta->addr, bw, err);
6660 		} else {
6661 			/* When we downgrade bandwidth this will conflict with phymode
6662 			 * and cause to trigger firmware crash. In this case we send
6663 			 * WMI_PEER_CHWIDTH followed by WMI_PEER_PHYMODE
6664 			 */
6665 			ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac bandwidth downgrade for sta %pM new %d old %d\n",
6666 				   arsta->addr, bw, bw_prev);
6667 			err = ath12k_wmi_set_peer_param(ar, arsta->addr,
6668 							arvif->vdev_id, WMI_PEER_CHWIDTH,
6669 							bw);
6670 			if (err) {
6671 				ath12k_warn(ar->ab, "failed to update STA %pM peer to bandwidth %d: %d\n",
6672 					    arsta->addr, bw, err);
6673 				return;
6674 			}
6675 			err = ath12k_wmi_set_peer_param(ar, arsta->addr,
6676 							arvif->vdev_id, WMI_PEER_PHYMODE,
6677 							peer_phymode);
6678 			if (err)
6679 				ath12k_warn(ar->ab, "failed to update STA %pM to peer phymode %d: %d\n",
6680 					    arsta->addr, peer_phymode, err);
6681 		}
6682 	}
6683 
6684 	if (changed & IEEE80211_RC_NSS_CHANGED) {
6685 		ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac update sta %pM nss %d\n",
6686 			   arsta->addr, nss);
6687 
6688 		err = ath12k_wmi_set_peer_param(ar, arsta->addr, arvif->vdev_id,
6689 						WMI_PEER_NSS, nss);
6690 		if (err)
6691 			ath12k_warn(ar->ab, "failed to update STA %pM nss %d: %d\n",
6692 				    arsta->addr, nss, err);
6693 	}
6694 
6695 	if (changed & IEEE80211_RC_SMPS_CHANGED) {
6696 		ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac update sta %pM smps %d\n",
6697 			   arsta->addr, smps);
6698 
6699 		err = ath12k_wmi_set_peer_param(ar, arsta->addr, arvif->vdev_id,
6700 						WMI_PEER_MIMO_PS_STATE, smps);
6701 		if (err)
6702 			ath12k_warn(ar->ab, "failed to update STA %pM smps %d: %d\n",
6703 				    arsta->addr, smps, err);
6704 	}
6705 
6706 	if (changed & IEEE80211_RC_SUPP_RATES_CHANGED) {
6707 		mask = &arvif->bitrate_mask;
6708 		num_vht_rates = ath12k_mac_bitrate_mask_num_vht_rates(ar, band,
6709 								      mask);
6710 		num_he_rates = ath12k_mac_bitrate_mask_num_he_rates(ar, band,
6711 								    mask);
6712 		num_eht_rates = ath12k_mac_bitrate_mask_num_eht_rates(ar, band,
6713 								      mask);
6714 
6715 		/* Peer_assoc_prepare will reject vht rates in
6716 		 * bitrate_mask if its not available in range format and
6717 		 * sets vht tx_rateset as unsupported. So multiple VHT MCS
6718 		 * setting(eg. MCS 4,5,6) per peer is not supported here.
6719 		 * But, Single rate in VHT mask can be set as per-peer
6720 		 * fixed rate. But even if any HT rates are configured in
6721 		 * the bitrate mask, device will not switch to those rates
6722 		 * when per-peer Fixed rate is set.
6723 		 * TODO: Check RATEMASK_CMDID to support auto rates selection
6724 		 * across HT/VHT and for multiple VHT MCS support.
6725 		 */
6726 		link_sta = ath12k_mac_get_link_sta(arsta);
6727 		if (!link_sta) {
6728 			ath12k_warn(ar->ab, "unable to access link sta in peer assoc he for sta %pM link %u\n",
6729 				    sta->addr, arsta->link_id);
6730 			return;
6731 		}
6732 
6733 		if (link_sta->vht_cap.vht_supported && num_vht_rates == 1) {
6734 			ath12k_mac_set_peer_vht_fixed_rate(arvif, arsta, mask,
6735 							   band);
6736 		} else if (link_sta->he_cap.has_he && num_he_rates == 1) {
6737 			ath12k_mac_set_peer_he_fixed_rate(arvif, arsta, mask, band);
6738 		} else if (link_sta->eht_cap.has_eht && num_eht_rates == 1) {
6739 			err = ath12k_mac_set_peer_eht_fixed_rate(arvif, arsta,
6740 								 mask, band);
6741 			if (err) {
6742 				ath12k_warn(ar->ab,
6743 					    "failed to set peer EHT fixed rate for STA %pM ret %d\n",
6744 					    arsta->addr, err);
6745 				return;
6746 			}
6747 		} else {
6748 			/* If the peer is non-VHT/HE/EHT or no fixed VHT/HE/EHT
6749 			 * rate is provided in the new bitrate mask we set the
6750 			 * other rates using peer_assoc command. Also clear
6751 			 * the peer fixed rate settings as it has higher proprity
6752 			 * than peer assoc
6753 			 */
6754 			err = ath12k_wmi_set_peer_param(ar, arsta->addr,
6755 							arvif->vdev_id,
6756 							WMI_PEER_PARAM_FIXED_RATE,
6757 							WMI_FIXED_RATE_NONE);
6758 			if (err)
6759 				ath12k_warn(ar->ab,
6760 					    "failed to disable peer fixed rate for STA %pM ret %d\n",
6761 					    arsta->addr, err);
6762 
6763 			ath12k_peer_assoc_prepare(ar, arvif, arsta,
6764 						  peer_arg, true);
6765 
6766 			peer_arg->is_assoc = false;
6767 			err = ath12k_wmi_send_peer_assoc_cmd(ar, peer_arg);
6768 			if (err)
6769 				ath12k_warn(ar->ab, "failed to run peer assoc for STA %pM vdev %i: %d\n",
6770 					    arsta->addr, arvif->vdev_id, err);
6771 
6772 			if (!wait_for_completion_timeout(&ar->peer_assoc_done, 1 * HZ))
6773 				ath12k_warn(ar->ab, "failed to get peer assoc conf event for %pM vdev %i\n",
6774 					    arsta->addr, arvif->vdev_id);
6775 		}
6776 	}
6777 }
6778 
6779 static void ath12k_mac_free_unassign_link_sta(struct ath12k_hw *ah,
6780 					      struct ath12k_sta *ahsta,
6781 					      u8 link_id)
6782 {
6783 	struct ath12k_link_sta *arsta;
6784 
6785 	lockdep_assert_wiphy(ah->hw->wiphy);
6786 
6787 	if (WARN_ON(link_id >= IEEE80211_MLD_MAX_NUM_LINKS))
6788 		return;
6789 
6790 	arsta = wiphy_dereference(ah->hw->wiphy, ahsta->link[link_id]);
6791 	if (WARN_ON(!arsta))
6792 		return;
6793 
6794 	ahsta->links_map &= ~BIT(link_id);
6795 	ahsta->free_logical_link_idx_map |= BIT(arsta->link_idx);
6796 
6797 	rcu_assign_pointer(ahsta->link[link_id], NULL);
6798 	synchronize_rcu();
6799 
6800 	if (arsta == &ahsta->deflink) {
6801 		arsta->link_id = ATH12K_INVALID_LINK_ID;
6802 		arsta->ahsta = NULL;
6803 		arsta->arvif = NULL;
6804 		return;
6805 	}
6806 
6807 	kfree(arsta);
6808 }
6809 
6810 static int ath12k_mac_inc_num_stations(struct ath12k_link_vif *arvif,
6811 				       struct ath12k_link_sta *arsta)
6812 {
6813 	struct ieee80211_sta *sta = ath12k_ahsta_to_sta(arsta->ahsta);
6814 	struct ath12k *ar = arvif->ar;
6815 
6816 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
6817 
6818 	if (arvif->ahvif->vdev_type == WMI_VDEV_TYPE_STA && !sta->tdls)
6819 		return 0;
6820 
6821 	if (ar->num_stations >= ar->max_num_stations)
6822 		return -ENOBUFS;
6823 
6824 	ar->num_stations++;
6825 	arvif->num_stations++;
6826 
6827 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
6828 		   "mac station %pM connected to vdev %u num_stations %u\n",
6829 		   arsta->addr, arvif->vdev_id, arvif->num_stations);
6830 
6831 	return 0;
6832 }
6833 
6834 static void ath12k_mac_dec_num_stations(struct ath12k_link_vif *arvif,
6835 					struct ath12k_link_sta *arsta)
6836 {
6837 	struct ieee80211_sta *sta = ath12k_ahsta_to_sta(arsta->ahsta);
6838 	struct ath12k *ar = arvif->ar;
6839 
6840 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
6841 
6842 	if (arvif->ahvif->vdev_type == WMI_VDEV_TYPE_STA && !sta->tdls)
6843 		return;
6844 
6845 	ar->num_stations--;
6846 
6847 	if (arvif->num_stations) {
6848 		arvif->num_stations--;
6849 		ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
6850 			   "mac station %pM disconnected from vdev %u num_stations %u\n",
6851 			   arsta->addr, arvif->vdev_id, arvif->num_stations);
6852 	} else {
6853 		ath12k_warn(ar->ab,
6854 			    "mac station %pM disconnect for vdev %u without any connected station\n",
6855 			    arsta->addr, arvif->vdev_id);
6856 	}
6857 }
6858 
6859 static void ath12k_mac_station_post_remove(struct ath12k *ar,
6860 					   struct ath12k_link_vif *arvif,
6861 					   struct ath12k_link_sta *arsta)
6862 {
6863 	struct ieee80211_vif *vif = ath12k_ahvif_to_vif(arvif->ahvif);
6864 	struct ieee80211_sta *sta = ath12k_ahsta_to_sta(arsta->ahsta);
6865 	struct ath12k_dp_link_peer *peer;
6866 	struct ath12k_dp *dp = ath12k_ab_to_dp(ar->ab);
6867 
6868 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
6869 
6870 	ath12k_mac_dec_num_stations(arvif, arsta);
6871 
6872 	spin_lock_bh(&dp->dp_lock);
6873 
6874 	peer = ath12k_dp_link_peer_find_by_vdev_and_addr(dp, arvif->vdev_id,
6875 							 arsta->addr);
6876 	if (peer && peer->sta == sta) {
6877 		ath12k_warn(ar->ab, "Found peer entry %pM n vdev %i after it was supposedly removed\n",
6878 			    vif->addr, arvif->vdev_id);
6879 		peer->sta = NULL;
6880 
6881 		ath12k_dp_link_peer_free(peer);
6882 		ar->num_peers--;
6883 	}
6884 
6885 	spin_unlock_bh(&dp->dp_lock);
6886 }
6887 
6888 static int ath12k_mac_station_unauthorize(struct ath12k *ar,
6889 					  struct ath12k_link_vif *arvif,
6890 					  struct ath12k_link_sta *arsta)
6891 {
6892 	struct ath12k_dp_link_peer *peer;
6893 	int ret;
6894 	struct ath12k_dp *dp = ath12k_ab_to_dp(ar->ab);
6895 
6896 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
6897 
6898 	spin_lock_bh(&dp->dp_lock);
6899 
6900 	peer = ath12k_dp_link_peer_find_by_vdev_and_addr(dp, arvif->vdev_id,
6901 							 arsta->addr);
6902 	if (peer)
6903 		peer->is_authorized = false;
6904 
6905 	spin_unlock_bh(&dp->dp_lock);
6906 
6907 	/* Driver must clear the keys during the state change from
6908 	 * IEEE80211_STA_AUTHORIZED to IEEE80211_STA_ASSOC, since after
6909 	 * returning from here, mac80211 is going to delete the keys
6910 	 * in __sta_info_destroy_part2(). This will ensure that the driver does
6911 	 * not retain stale key references after mac80211 deletes the keys.
6912 	 */
6913 	ret = ath12k_clear_peer_keys(arvif, arsta->addr);
6914 	if (ret) {
6915 		ath12k_warn(ar->ab, "failed to clear all peer keys for vdev %i: %d\n",
6916 			    arvif->vdev_id, ret);
6917 		return ret;
6918 	}
6919 
6920 	return 0;
6921 }
6922 
6923 static int ath12k_mac_station_authorize(struct ath12k *ar,
6924 					struct ath12k_link_vif *arvif,
6925 					struct ath12k_link_sta *arsta)
6926 {
6927 	struct ath12k_dp_link_peer *peer;
6928 	struct ieee80211_vif *vif = ath12k_ahvif_to_vif(arvif->ahvif);
6929 	int ret;
6930 	struct ath12k_dp *dp = ath12k_ab_to_dp(ar->ab);
6931 
6932 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
6933 
6934 	spin_lock_bh(&dp->dp_lock);
6935 
6936 	peer = ath12k_dp_link_peer_find_by_vdev_and_addr(dp, arvif->vdev_id,
6937 							 arsta->addr);
6938 	if (peer)
6939 		peer->is_authorized = true;
6940 
6941 	spin_unlock_bh(&dp->dp_lock);
6942 
6943 	if (vif->type == NL80211_IFTYPE_STATION && arvif->is_up) {
6944 		ret = ath12k_wmi_set_peer_param(ar, arsta->addr,
6945 						arvif->vdev_id,
6946 						WMI_PEER_AUTHORIZE,
6947 						1);
6948 		if (ret) {
6949 			ath12k_warn(ar->ab, "Unable to authorize peer %pM vdev %d: %d\n",
6950 				    arsta->addr, arvif->vdev_id, ret);
6951 			return ret;
6952 		}
6953 	}
6954 
6955 	return 0;
6956 }
6957 
6958 static int ath12k_mac_station_remove(struct ath12k *ar,
6959 				     struct ath12k_link_vif *arvif,
6960 				     struct ath12k_link_sta *arsta)
6961 {
6962 	struct ieee80211_sta *sta = ath12k_ahsta_to_sta(arsta->ahsta);
6963 	struct ath12k_vif *ahvif = arvif->ahvif;
6964 	int ret = 0;
6965 	struct ath12k_link_sta *temp_arsta;
6966 
6967 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
6968 
6969 	wiphy_work_cancel(ar->ah->hw->wiphy, &arsta->update_wk);
6970 
6971 	if (ahvif->vdev_type == WMI_VDEV_TYPE_STA) {
6972 		ath12k_bss_disassoc(ar, arvif);
6973 		ret = ath12k_mac_vdev_stop(arvif);
6974 		if (ret)
6975 			ath12k_warn(ar->ab, "failed to stop vdev %i: %d\n",
6976 				    arvif->vdev_id, ret);
6977 	}
6978 
6979 	if (sta->mlo)
6980 		return ret;
6981 
6982 	ath12k_dp_peer_cleanup(ar, arvif->vdev_id, arsta->addr);
6983 
6984 	ret = ath12k_peer_delete(ar, arvif->vdev_id, arsta->addr);
6985 	if (ret)
6986 		ath12k_warn(ar->ab, "Failed to delete peer: %pM for VDEV: %d\n",
6987 			    arsta->addr, arvif->vdev_id);
6988 	else
6989 		ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "Removed peer: %pM for VDEV: %d\n",
6990 			   arsta->addr, arvif->vdev_id);
6991 
6992 	ath12k_mac_station_post_remove(ar, arvif, arsta);
6993 
6994 	spin_lock_bh(&ar->ab->base_lock);
6995 
6996 	/* To handle roaming and split phy scenario */
6997 	temp_arsta = ath12k_link_sta_find_by_addr(ar->ab, arsta->addr);
6998 	if (temp_arsta && temp_arsta->arvif->ar == ar)
6999 		ath12k_link_sta_rhash_delete(ar->ab, arsta);
7000 
7001 	spin_unlock_bh(&ar->ab->base_lock);
7002 
7003 	if (sta->valid_links)
7004 		ath12k_mac_free_unassign_link_sta(ahvif->ah,
7005 						  arsta->ahsta, arsta->link_id);
7006 
7007 	return ret;
7008 }
7009 
7010 static int ath12k_mac_station_add(struct ath12k *ar,
7011 				  struct ath12k_link_vif *arvif,
7012 				  struct ath12k_link_sta *arsta)
7013 {
7014 	struct ath12k_base *ab = ar->ab;
7015 	struct ieee80211_vif *vif = ath12k_ahvif_to_vif(arvif->ahvif);
7016 	struct ieee80211_sta *sta = ath12k_ahsta_to_sta(arsta->ahsta);
7017 	struct ath12k_wmi_peer_create_arg peer_param = {};
7018 	int ret;
7019 	struct ath12k_link_sta *temp_arsta;
7020 
7021 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
7022 
7023 	ret = ath12k_mac_inc_num_stations(arvif, arsta);
7024 	if (ret) {
7025 		ath12k_warn(ab, "refusing to associate station: too many connected already (%d)\n",
7026 			    ar->max_num_stations);
7027 		goto exit;
7028 	}
7029 
7030 	spin_lock_bh(&ab->base_lock);
7031 
7032 	/*
7033 	 * In case of Split PHY and roaming scenario, pdev idx
7034 	 * might differ but both the pdev will share same rhash
7035 	 * table. In that case update the rhash table if link_sta is
7036 	 * already present
7037 	 */
7038 	temp_arsta = ath12k_link_sta_find_by_addr(ab, arsta->addr);
7039 	if (temp_arsta && temp_arsta->arvif->ar != ar)
7040 		ath12k_link_sta_rhash_delete(ab, temp_arsta);
7041 
7042 	ret = ath12k_link_sta_rhash_add(ab, arsta);
7043 	spin_unlock_bh(&ab->base_lock);
7044 	if (ret) {
7045 		ath12k_warn(ab, "Failed to add arsta: %pM to hash table, ret: %d",
7046 			    arsta->addr, ret);
7047 		goto dec_num_station;
7048 	}
7049 
7050 	peer_param.vdev_id = arvif->vdev_id;
7051 	peer_param.peer_addr = arsta->addr;
7052 	peer_param.peer_type = WMI_PEER_TYPE_DEFAULT;
7053 	peer_param.ml_enabled = sta->mlo;
7054 
7055 	ret = ath12k_peer_create(ar, arvif, sta, &peer_param);
7056 	if (ret) {
7057 		ath12k_warn(ab, "Failed to add peer: %pM for VDEV: %d\n",
7058 			    arsta->addr, arvif->vdev_id);
7059 		goto free_peer;
7060 	}
7061 
7062 	ath12k_dbg(ab, ATH12K_DBG_MAC, "Added peer: %pM for VDEV: %d\n",
7063 		   arsta->addr, arvif->vdev_id);
7064 
7065 	if (ieee80211_vif_is_mesh(vif)) {
7066 		ret = ath12k_wmi_set_peer_param(ar, arsta->addr,
7067 						arvif->vdev_id,
7068 						WMI_PEER_USE_4ADDR, 1);
7069 		if (ret) {
7070 			ath12k_warn(ab, "failed to STA %pM 4addr capability: %d\n",
7071 				    arsta->addr, ret);
7072 			goto free_peer;
7073 		}
7074 	}
7075 
7076 	ret = ath12k_dp_peer_setup(ar, arvif->vdev_id, arsta->addr);
7077 	if (ret) {
7078 		ath12k_warn(ab, "failed to setup dp for peer %pM on vdev %i (%d)\n",
7079 			    arsta->addr, arvif->vdev_id, ret);
7080 		goto free_peer;
7081 	}
7082 
7083 	if (ab->hw_params->vdev_start_delay &&
7084 	    !arvif->is_started &&
7085 	    arvif->ahvif->vdev_type != WMI_VDEV_TYPE_AP) {
7086 		ret = ath12k_start_vdev_delay(ar, arvif);
7087 		if (ret) {
7088 			ath12k_warn(ab, "failed to delay vdev start: %d\n", ret);
7089 			goto free_peer;
7090 		}
7091 	}
7092 
7093 	return 0;
7094 
7095 free_peer:
7096 	ath12k_peer_delete(ar, arvif->vdev_id, arsta->addr);
7097 	spin_lock_bh(&ab->base_lock);
7098 	ath12k_link_sta_rhash_delete(ab, arsta);
7099 	spin_unlock_bh(&ab->base_lock);
7100 dec_num_station:
7101 	ath12k_mac_dec_num_stations(arvif, arsta);
7102 exit:
7103 	return ret;
7104 }
7105 
7106 static int ath12k_mac_assign_link_sta(struct ath12k_hw *ah,
7107 				      struct ath12k_sta *ahsta,
7108 				      struct ath12k_link_sta *arsta,
7109 				      struct ath12k_vif *ahvif,
7110 				      u8 link_id)
7111 {
7112 	struct ieee80211_sta *sta = ath12k_ahsta_to_sta(ahsta);
7113 	struct ieee80211_link_sta *link_sta;
7114 	struct ath12k_link_vif *arvif;
7115 	int link_idx;
7116 
7117 	lockdep_assert_wiphy(ah->hw->wiphy);
7118 
7119 	if (!arsta || link_id >= IEEE80211_MLD_MAX_NUM_LINKS)
7120 		return -EINVAL;
7121 
7122 	arvif = wiphy_dereference(ah->hw->wiphy, ahvif->link[link_id]);
7123 	if (!arvif)
7124 		return -EINVAL;
7125 
7126 	memset(arsta, 0, sizeof(*arsta));
7127 
7128 	link_sta = wiphy_dereference(ah->hw->wiphy, sta->link[link_id]);
7129 	if (!link_sta)
7130 		return -EINVAL;
7131 
7132 	ether_addr_copy(arsta->addr, link_sta->addr);
7133 
7134 	if (!ahsta->free_logical_link_idx_map)
7135 		return -ENOSPC;
7136 
7137 	/*
7138 	 * Allocate a logical link index by selecting the first available bit
7139 	 * from the free logical index map
7140 	 */
7141 	link_idx = __ffs(ahsta->free_logical_link_idx_map);
7142 	ahsta->free_logical_link_idx_map &= ~BIT(link_idx);
7143 	arsta->link_idx = link_idx;
7144 
7145 	arsta->link_id = link_id;
7146 	ahsta->links_map |= BIT(arsta->link_id);
7147 	arsta->arvif = arvif;
7148 	arsta->ahsta = ahsta;
7149 	ahsta->ahvif = ahvif;
7150 
7151 	wiphy_work_init(&arsta->update_wk, ath12k_sta_rc_update_wk);
7152 
7153 	rcu_assign_pointer(ahsta->link[link_id], arsta);
7154 
7155 	return 0;
7156 }
7157 
7158 static void ath12k_mac_ml_station_remove(struct ath12k_vif *ahvif,
7159 					 struct ath12k_sta *ahsta)
7160 {
7161 	struct ieee80211_sta *sta = ath12k_ahsta_to_sta(ahsta);
7162 	struct ath12k_hw *ah = ahvif->ah;
7163 	struct ath12k_link_vif *arvif;
7164 	struct ath12k_link_sta *arsta;
7165 	unsigned long links;
7166 	struct ath12k *ar;
7167 	u8 link_id;
7168 
7169 	lockdep_assert_wiphy(ah->hw->wiphy);
7170 
7171 	ath12k_peer_mlo_link_peers_delete(ahvif, ahsta);
7172 
7173 	/* validate link station removal and clear arsta links */
7174 	links = ahsta->links_map;
7175 	for_each_set_bit(link_id, &links, IEEE80211_MLD_MAX_NUM_LINKS) {
7176 		arvif = wiphy_dereference(ah->hw->wiphy, ahvif->link[link_id]);
7177 		arsta = wiphy_dereference(ah->hw->wiphy, ahsta->link[link_id]);
7178 		if (!arvif || !arsta)
7179 			continue;
7180 
7181 		ar = arvif->ar;
7182 
7183 		ath12k_mac_station_post_remove(ar, arvif, arsta);
7184 
7185 		spin_lock_bh(&ar->ab->base_lock);
7186 		ath12k_link_sta_rhash_delete(ar->ab, arsta);
7187 		spin_unlock_bh(&ar->ab->base_lock);
7188 
7189 		ath12k_mac_free_unassign_link_sta(ah, ahsta, link_id);
7190 	}
7191 
7192 	if (sta->mlo) {
7193 		clear_bit(ahsta->ml_peer_id, ah->free_ml_peer_id_map);
7194 		ahsta->ml_peer_id = ATH12K_MLO_PEER_ID_INVALID;
7195 	}
7196 }
7197 
7198 static int ath12k_mac_handle_link_sta_state(struct ieee80211_hw *hw,
7199 					    struct ath12k_link_vif *arvif,
7200 					    struct ath12k_link_sta *arsta,
7201 					    enum ieee80211_sta_state old_state,
7202 					    enum ieee80211_sta_state new_state)
7203 {
7204 	struct ieee80211_vif *vif = ath12k_ahvif_to_vif(arvif->ahvif);
7205 	struct ieee80211_bss_conf *link_conf;
7206 	struct ath12k *ar = arvif->ar;
7207 	struct ath12k_reg_info *reg_info;
7208 	struct ath12k_base *ab = ar->ab;
7209 	int ret = 0;
7210 
7211 	lockdep_assert_wiphy(hw->wiphy);
7212 
7213 	ath12k_dbg(ab, ATH12K_DBG_MAC, "mac handle link %u sta %pM state %d -> %d\n",
7214 		   arsta->link_id, arsta->addr, old_state, new_state);
7215 
7216 	/* IEEE80211_STA_NONE -> IEEE80211_STA_NOTEXIST: Remove the station
7217 	 * from driver
7218 	 */
7219 	if ((old_state == IEEE80211_STA_NONE &&
7220 	     new_state == IEEE80211_STA_NOTEXIST)) {
7221 		ret = ath12k_mac_station_remove(ar, arvif, arsta);
7222 		if (ret) {
7223 			ath12k_warn(ab, "Failed to remove station: %pM for VDEV: %d\n",
7224 				    arsta->addr, arvif->vdev_id);
7225 			goto exit;
7226 		}
7227 	}
7228 
7229 	/* IEEE80211_STA_NOTEXIST -> IEEE80211_STA_NONE: Add new station to driver */
7230 	if (old_state == IEEE80211_STA_NOTEXIST &&
7231 	    new_state == IEEE80211_STA_NONE) {
7232 		ret = ath12k_mac_station_add(ar, arvif, arsta);
7233 		if (ret)
7234 			ath12k_warn(ab, "Failed to add station: %pM for VDEV: %d\n",
7235 				    arsta->addr, arvif->vdev_id);
7236 
7237 	/* IEEE80211_STA_AUTH -> IEEE80211_STA_ASSOC: Send station assoc command for
7238 	 * peer associated to AP/Mesh/ADHOC vif type.
7239 	 */
7240 	} else if (old_state == IEEE80211_STA_AUTH &&
7241 		   new_state == IEEE80211_STA_ASSOC &&
7242 		   (vif->type == NL80211_IFTYPE_AP ||
7243 		    vif->type == NL80211_IFTYPE_MESH_POINT ||
7244 		    vif->type == NL80211_IFTYPE_ADHOC)) {
7245 		ret = ath12k_mac_station_assoc(ar, arvif, arsta, false);
7246 		if (ret)
7247 			ath12k_warn(ab, "Failed to associate station: %pM\n",
7248 				    arsta->addr);
7249 
7250 	/* IEEE80211_STA_ASSOC -> IEEE80211_STA_AUTHORIZED: set peer status as
7251 	 * authorized
7252 	 */
7253 	} else if (old_state == IEEE80211_STA_ASSOC &&
7254 		   new_state == IEEE80211_STA_AUTHORIZED) {
7255 		ret = ath12k_mac_station_authorize(ar, arvif, arsta);
7256 		if (ret) {
7257 			ath12k_warn(ab, "Failed to authorize station: %pM\n",
7258 				    arsta->addr);
7259 			goto exit;
7260 		}
7261 
7262 		if (ath12k_wmi_supports_6ghz_cc_ext(ar) &&
7263 		    arvif->ahvif->vdev_type == WMI_VDEV_TYPE_STA) {
7264 			link_conf = ath12k_mac_get_link_bss_conf(arvif);
7265 			reg_info = ab->reg_info[ar->pdev_idx];
7266 			ath12k_dbg(ab, ATH12K_DBG_MAC, "connection done, update reg rules\n");
7267 			ath12k_hw_to_ah(hw)->regd_updated = false;
7268 			ath12k_reg_handle_chan_list(ab, reg_info, arvif->ahvif->vdev_type,
7269 						    link_conf->power_type);
7270 		}
7271 
7272 	/* IEEE80211_STA_AUTHORIZED -> IEEE80211_STA_ASSOC: station may be in removal,
7273 	 * deauthorize it.
7274 	 */
7275 	} else if (old_state == IEEE80211_STA_AUTHORIZED &&
7276 		   new_state == IEEE80211_STA_ASSOC) {
7277 		ath12k_mac_station_unauthorize(ar, arvif, arsta);
7278 
7279 	/* IEEE80211_STA_ASSOC -> IEEE80211_STA_AUTH: disassoc peer connected to
7280 	 * AP/mesh/ADHOC vif type.
7281 	 */
7282 	} else if (old_state == IEEE80211_STA_ASSOC &&
7283 		   new_state == IEEE80211_STA_AUTH &&
7284 		   (vif->type == NL80211_IFTYPE_AP ||
7285 		    vif->type == NL80211_IFTYPE_MESH_POINT ||
7286 		    vif->type == NL80211_IFTYPE_ADHOC)) {
7287 		ret = ath12k_mac_station_disassoc(ar, arvif, arsta);
7288 		if (ret)
7289 			ath12k_warn(ab, "Failed to disassociate station: %pM\n",
7290 				    arsta->addr);
7291 	}
7292 
7293 exit:
7294 	return ret;
7295 }
7296 
7297 static bool ath12k_mac_is_freq_on_mac(struct ath12k_hw_mode_freq_range_arg *freq_range,
7298 				      u32 freq, u8 mac_id)
7299 {
7300 	return (freq >= freq_range[mac_id].low_2ghz_freq &&
7301 		freq <= freq_range[mac_id].high_2ghz_freq) ||
7302 	       (freq >= freq_range[mac_id].low_5ghz_freq &&
7303 		freq <= freq_range[mac_id].high_5ghz_freq);
7304 }
7305 
7306 static bool
7307 ath12k_mac_2_freq_same_mac_in_freq_range(struct ath12k_base *ab,
7308 					 struct ath12k_hw_mode_freq_range_arg *freq_range,
7309 					 u32 freq_link1, u32 freq_link2)
7310 {
7311 	u8 i;
7312 
7313 	for (i = 0; i < MAX_RADIOS; i++) {
7314 		if (ath12k_mac_is_freq_on_mac(freq_range, freq_link1, i) &&
7315 		    ath12k_mac_is_freq_on_mac(freq_range, freq_link2, i))
7316 			return true;
7317 	}
7318 
7319 	return false;
7320 }
7321 
7322 static bool ath12k_mac_is_hw_dbs_capable(struct ath12k_base *ab)
7323 {
7324 	return test_bit(WMI_TLV_SERVICE_DUAL_BAND_SIMULTANEOUS_SUPPORT,
7325 			ab->wmi_ab.svc_map) &&
7326 	       ab->wmi_ab.hw_mode_info.support_dbs;
7327 }
7328 
7329 static bool ath12k_mac_2_freq_same_mac_in_dbs(struct ath12k_base *ab,
7330 					      u32 freq_link1, u32 freq_link2)
7331 {
7332 	struct ath12k_hw_mode_freq_range_arg *freq_range;
7333 
7334 	if (!ath12k_mac_is_hw_dbs_capable(ab))
7335 		return true;
7336 
7337 	freq_range = ab->wmi_ab.hw_mode_info.freq_range_caps[ATH12K_HW_MODE_DBS];
7338 	return ath12k_mac_2_freq_same_mac_in_freq_range(ab, freq_range,
7339 							freq_link1, freq_link2);
7340 }
7341 
7342 static bool ath12k_mac_is_hw_sbs_capable(struct ath12k_base *ab)
7343 {
7344 	return test_bit(WMI_TLV_SERVICE_DUAL_BAND_SIMULTANEOUS_SUPPORT,
7345 			ab->wmi_ab.svc_map) &&
7346 	       ab->wmi_ab.hw_mode_info.support_sbs;
7347 }
7348 
7349 static bool ath12k_mac_2_freq_same_mac_in_sbs(struct ath12k_base *ab,
7350 					      u32 freq_link1, u32 freq_link2)
7351 {
7352 	struct ath12k_hw_mode_info *info = &ab->wmi_ab.hw_mode_info;
7353 	struct ath12k_hw_mode_freq_range_arg *sbs_uppr_share;
7354 	struct ath12k_hw_mode_freq_range_arg *sbs_low_share;
7355 	struct ath12k_hw_mode_freq_range_arg *sbs_range;
7356 
7357 	if (!ath12k_mac_is_hw_sbs_capable(ab))
7358 		return true;
7359 
7360 	if (ab->wmi_ab.sbs_lower_band_end_freq) {
7361 		sbs_uppr_share = info->freq_range_caps[ATH12K_HW_MODE_SBS_UPPER_SHARE];
7362 		sbs_low_share = info->freq_range_caps[ATH12K_HW_MODE_SBS_LOWER_SHARE];
7363 
7364 		return ath12k_mac_2_freq_same_mac_in_freq_range(ab, sbs_low_share,
7365 								freq_link1, freq_link2) ||
7366 		       ath12k_mac_2_freq_same_mac_in_freq_range(ab, sbs_uppr_share,
7367 								freq_link1, freq_link2);
7368 	}
7369 
7370 	sbs_range = info->freq_range_caps[ATH12K_HW_MODE_SBS];
7371 	return ath12k_mac_2_freq_same_mac_in_freq_range(ab, sbs_range,
7372 							freq_link1, freq_link2);
7373 }
7374 
7375 static bool ath12k_mac_freqs_on_same_mac(struct ath12k_base *ab,
7376 					 u32 freq_link1, u32 freq_link2)
7377 {
7378 	return ath12k_mac_2_freq_same_mac_in_dbs(ab, freq_link1, freq_link2) &&
7379 	       ath12k_mac_2_freq_same_mac_in_sbs(ab, freq_link1, freq_link2);
7380 }
7381 
7382 static int ath12k_mac_mlo_sta_set_link_active(struct ath12k_base *ab,
7383 					      enum wmi_mlo_link_force_reason reason,
7384 					      enum wmi_mlo_link_force_mode mode,
7385 					      u8 *mlo_vdev_id_lst,
7386 					      u8 num_mlo_vdev,
7387 					      u8 *mlo_inactive_vdev_lst,
7388 					      u8 num_mlo_inactive_vdev)
7389 {
7390 	struct wmi_mlo_link_set_active_arg param = {};
7391 	u32 entry_idx, entry_offset, vdev_idx;
7392 	u8 vdev_id;
7393 
7394 	param.reason = reason;
7395 	param.force_mode = mode;
7396 
7397 	for (vdev_idx = 0; vdev_idx < num_mlo_vdev; vdev_idx++) {
7398 		vdev_id = mlo_vdev_id_lst[vdev_idx];
7399 		entry_idx = vdev_id / 32;
7400 		entry_offset = vdev_id % 32;
7401 		if (entry_idx >= WMI_MLO_LINK_NUM_SZ) {
7402 			ath12k_warn(ab, "Invalid entry_idx %d num_mlo_vdev %d vdev %d",
7403 				    entry_idx, num_mlo_vdev, vdev_id);
7404 			return -EINVAL;
7405 		}
7406 		param.vdev_bitmap[entry_idx] |= 1 << entry_offset;
7407 		/* update entry number if entry index changed */
7408 		if (param.num_vdev_bitmap < entry_idx + 1)
7409 			param.num_vdev_bitmap = entry_idx + 1;
7410 	}
7411 
7412 	ath12k_dbg(ab, ATH12K_DBG_MAC,
7413 		   "num_vdev_bitmap %d vdev_bitmap[0] = 0x%x, vdev_bitmap[1] = 0x%x",
7414 		   param.num_vdev_bitmap, param.vdev_bitmap[0], param.vdev_bitmap[1]);
7415 
7416 	if (mode == WMI_MLO_LINK_FORCE_MODE_ACTIVE_INACTIVE) {
7417 		for (vdev_idx = 0; vdev_idx < num_mlo_inactive_vdev; vdev_idx++) {
7418 			vdev_id = mlo_inactive_vdev_lst[vdev_idx];
7419 			entry_idx = vdev_id / 32;
7420 			entry_offset = vdev_id % 32;
7421 			if (entry_idx >= WMI_MLO_LINK_NUM_SZ) {
7422 				ath12k_warn(ab, "Invalid entry_idx %d num_mlo_vdev %d vdev %d",
7423 					    entry_idx, num_mlo_inactive_vdev, vdev_id);
7424 				return -EINVAL;
7425 			}
7426 			param.inactive_vdev_bitmap[entry_idx] |= 1 << entry_offset;
7427 			/* update entry number if entry index changed */
7428 			if (param.num_inactive_vdev_bitmap < entry_idx + 1)
7429 				param.num_inactive_vdev_bitmap = entry_idx + 1;
7430 		}
7431 
7432 		ath12k_dbg(ab, ATH12K_DBG_MAC,
7433 			   "num_vdev_bitmap %d inactive_vdev_bitmap[0] = 0x%x, inactive_vdev_bitmap[1] = 0x%x",
7434 			   param.num_inactive_vdev_bitmap,
7435 			   param.inactive_vdev_bitmap[0],
7436 			   param.inactive_vdev_bitmap[1]);
7437 	}
7438 
7439 	if (mode == WMI_MLO_LINK_FORCE_MODE_ACTIVE_LINK_NUM ||
7440 	    mode == WMI_MLO_LINK_FORCE_MODE_INACTIVE_LINK_NUM) {
7441 		param.num_link_entry = 1;
7442 		param.link_num[0].num_of_link = num_mlo_vdev - 1;
7443 	}
7444 
7445 	return ath12k_wmi_send_mlo_link_set_active_cmd(ab, &param);
7446 }
7447 
7448 static int ath12k_mac_mlo_sta_update_link_active(struct ath12k_base *ab,
7449 						 struct ieee80211_hw *hw,
7450 						 struct ath12k_vif *ahvif)
7451 {
7452 	u8 mlo_vdev_id_lst[IEEE80211_MLD_MAX_NUM_LINKS] = {};
7453 	u32 mlo_freq_list[IEEE80211_MLD_MAX_NUM_LINKS] = {};
7454 	unsigned long links = ahvif->links_map;
7455 	enum wmi_mlo_link_force_reason reason;
7456 	struct ieee80211_chanctx_conf *conf;
7457 	enum wmi_mlo_link_force_mode mode;
7458 	struct ieee80211_bss_conf *info;
7459 	struct ath12k_link_vif *arvif;
7460 	u8 num_mlo_vdev = 0;
7461 	u8 link_id;
7462 
7463 	for_each_set_bit(link_id, &links, IEEE80211_MLD_MAX_NUM_LINKS) {
7464 		arvif = wiphy_dereference(hw->wiphy, ahvif->link[link_id]);
7465 		/* make sure vdev is created on this device */
7466 		if (!arvif || !arvif->is_created || arvif->ar->ab != ab)
7467 			continue;
7468 
7469 		info = ath12k_mac_get_link_bss_conf(arvif);
7470 		conf = wiphy_dereference(hw->wiphy, info->chanctx_conf);
7471 		mlo_freq_list[num_mlo_vdev] = conf->def.chan->center_freq;
7472 
7473 		mlo_vdev_id_lst[num_mlo_vdev] = arvif->vdev_id;
7474 		num_mlo_vdev++;
7475 	}
7476 
7477 	/* It is not allowed to activate more links than a single device
7478 	 * supported. Something goes wrong if we reach here.
7479 	 */
7480 	if (num_mlo_vdev > ATH12K_NUM_MAX_ACTIVE_LINKS_PER_DEVICE) {
7481 		WARN_ON_ONCE(1);
7482 		return -EINVAL;
7483 	}
7484 
7485 	/* if 2 links are established and both link channels fall on the
7486 	 * same hardware MAC, send command to firmware to deactivate one
7487 	 * of them.
7488 	 */
7489 	if (num_mlo_vdev == 2 &&
7490 	    ath12k_mac_freqs_on_same_mac(ab, mlo_freq_list[0],
7491 					 mlo_freq_list[1])) {
7492 		mode = WMI_MLO_LINK_FORCE_MODE_INACTIVE_LINK_NUM;
7493 		reason = WMI_MLO_LINK_FORCE_REASON_NEW_CONNECT;
7494 		return ath12k_mac_mlo_sta_set_link_active(ab, reason, mode,
7495 							  mlo_vdev_id_lst, num_mlo_vdev,
7496 							  NULL, 0);
7497 	}
7498 
7499 	return 0;
7500 }
7501 
7502 static bool ath12k_mac_are_sbs_chan(struct ath12k_base *ab, u32 freq_1, u32 freq_2)
7503 {
7504 	if (!ath12k_mac_is_hw_sbs_capable(ab))
7505 		return false;
7506 
7507 	if (ath12k_is_2ghz_channel_freq(freq_1) ||
7508 	    ath12k_is_2ghz_channel_freq(freq_2))
7509 		return false;
7510 
7511 	return !ath12k_mac_2_freq_same_mac_in_sbs(ab, freq_1, freq_2);
7512 }
7513 
7514 static bool ath12k_mac_are_dbs_chan(struct ath12k_base *ab, u32 freq_1, u32 freq_2)
7515 {
7516 	if (!ath12k_mac_is_hw_dbs_capable(ab))
7517 		return false;
7518 
7519 	return !ath12k_mac_2_freq_same_mac_in_dbs(ab, freq_1, freq_2);
7520 }
7521 
7522 static int ath12k_mac_select_links(struct ath12k_base *ab,
7523 				   struct ieee80211_vif *vif,
7524 				   struct ieee80211_hw *hw,
7525 				   u16 *selected_links)
7526 {
7527 	unsigned long useful_links = ieee80211_vif_usable_links(vif);
7528 	struct ath12k_vif *ahvif = ath12k_vif_to_ahvif(vif);
7529 	u8 num_useful_links = hweight_long(useful_links);
7530 	struct ieee80211_chanctx_conf *chanctx;
7531 	struct ath12k_link_vif *assoc_arvif;
7532 	u32 assoc_link_freq, partner_freq;
7533 	u16 sbs_links = 0, dbs_links = 0;
7534 	struct ieee80211_bss_conf *info;
7535 	struct ieee80211_channel *chan;
7536 	struct ieee80211_sta *sta;
7537 	struct ath12k_sta *ahsta;
7538 	u8 link_id;
7539 
7540 	/* activate all useful links if less than max supported */
7541 	if (num_useful_links <= ATH12K_NUM_MAX_ACTIVE_LINKS_PER_DEVICE) {
7542 		*selected_links = useful_links;
7543 		return 0;
7544 	}
7545 
7546 	/* only in station mode we can get here, so it's safe
7547 	 * to use ap_addr
7548 	 */
7549 	rcu_read_lock();
7550 	sta = ieee80211_find_sta(vif, vif->cfg.ap_addr);
7551 	if (!sta) {
7552 		rcu_read_unlock();
7553 		ath12k_warn(ab, "failed to find sta with addr %pM\n", vif->cfg.ap_addr);
7554 		return -EINVAL;
7555 	}
7556 
7557 	ahsta = ath12k_sta_to_ahsta(sta);
7558 	assoc_arvif = wiphy_dereference(hw->wiphy, ahvif->link[ahsta->assoc_link_id]);
7559 	info = ath12k_mac_get_link_bss_conf(assoc_arvif);
7560 	chanctx = rcu_dereference(info->chanctx_conf);
7561 	assoc_link_freq = chanctx->def.chan->center_freq;
7562 	rcu_read_unlock();
7563 	ath12k_dbg(ab, ATH12K_DBG_MAC, "assoc link %u freq %u\n",
7564 		   assoc_arvif->link_id, assoc_link_freq);
7565 
7566 	/* assoc link is already activated and has to be kept active,
7567 	 * only need to select a partner link from others.
7568 	 */
7569 	useful_links &= ~BIT(assoc_arvif->link_id);
7570 	for_each_set_bit(link_id, &useful_links, IEEE80211_MLD_MAX_NUM_LINKS) {
7571 		info = wiphy_dereference(hw->wiphy, vif->link_conf[link_id]);
7572 		if (!info) {
7573 			ath12k_warn(ab, "failed to get link info for link: %u\n",
7574 				    link_id);
7575 			return -ENOLINK;
7576 		}
7577 
7578 		chan = info->chanreq.oper.chan;
7579 		if (!chan) {
7580 			ath12k_warn(ab, "failed to get chan for link: %u\n", link_id);
7581 			return -EINVAL;
7582 		}
7583 
7584 		partner_freq = chan->center_freq;
7585 		if (ath12k_mac_are_sbs_chan(ab, assoc_link_freq, partner_freq)) {
7586 			sbs_links |= BIT(link_id);
7587 			ath12k_dbg(ab, ATH12K_DBG_MAC, "new SBS link %u freq %u\n",
7588 				   link_id, partner_freq);
7589 			continue;
7590 		}
7591 
7592 		if (ath12k_mac_are_dbs_chan(ab, assoc_link_freq, partner_freq)) {
7593 			dbs_links |= BIT(link_id);
7594 			ath12k_dbg(ab, ATH12K_DBG_MAC, "new DBS link %u freq %u\n",
7595 				   link_id, partner_freq);
7596 			continue;
7597 		}
7598 
7599 		ath12k_dbg(ab, ATH12K_DBG_MAC, "non DBS/SBS link %u freq %u\n",
7600 			   link_id, partner_freq);
7601 	}
7602 
7603 	/* choose the first candidate no matter how many is in the list */
7604 	if (sbs_links)
7605 		link_id = __ffs(sbs_links);
7606 	else if (dbs_links)
7607 		link_id = __ffs(dbs_links);
7608 	else
7609 		link_id = ffs(useful_links) - 1;
7610 
7611 	ath12k_dbg(ab, ATH12K_DBG_MAC, "select partner link %u\n", link_id);
7612 
7613 	*selected_links = BIT(assoc_arvif->link_id) | BIT(link_id);
7614 
7615 	return 0;
7616 }
7617 
7618 int ath12k_mac_op_sta_state(struct ieee80211_hw *hw,
7619 			    struct ieee80211_vif *vif,
7620 			    struct ieee80211_sta *sta,
7621 			    enum ieee80211_sta_state old_state,
7622 			    enum ieee80211_sta_state new_state)
7623 {
7624 	struct ath12k_vif *ahvif = ath12k_vif_to_ahvif(vif);
7625 	struct ath12k_sta *ahsta = ath12k_sta_to_ahsta(sta);
7626 	struct ath12k_hw *ah = ath12k_hw_to_ah(hw);
7627 	struct ath12k_base *prev_ab = NULL, *ab;
7628 	struct ath12k_link_vif *arvif;
7629 	struct ath12k_link_sta *arsta;
7630 	unsigned long valid_links;
7631 	u16 selected_links = 0;
7632 	u8 link_id = 0, i;
7633 	struct ath12k *ar;
7634 	int ret = -EINVAL;
7635 	struct ath12k_dp_peer_create_params dp_params = {};
7636 
7637 	lockdep_assert_wiphy(hw->wiphy);
7638 
7639 	if (ieee80211_vif_is_mld(vif) && sta->valid_links) {
7640 		WARN_ON(!sta->mlo && hweight16(sta->valid_links) != 1);
7641 		link_id = ffs(sta->valid_links) - 1;
7642 	}
7643 
7644 	/* IEEE80211_STA_NOTEXIST -> IEEE80211_STA_NONE:
7645 	 * New station add received. If this is a ML station then
7646 	 * ahsta->links_map will be zero and sta->valid_links will be 1.
7647 	 * Assign default link to the first link sta.
7648 	 */
7649 	if (old_state == IEEE80211_STA_NOTEXIST &&
7650 	    new_state == IEEE80211_STA_NONE) {
7651 		memset(ahsta, 0, sizeof(*ahsta));
7652 		ahsta->free_logical_link_idx_map = U16_MAX;
7653 
7654 		arsta = &ahsta->deflink;
7655 
7656 		/* ML sta */
7657 		if (sta->mlo && !ahsta->links_map &&
7658 		    (hweight16(sta->valid_links) == 1)) {
7659 			ahsta->ml_peer_id = ath12k_peer_ml_alloc(ah);
7660 			if (ahsta->ml_peer_id == ATH12K_MLO_PEER_ID_INVALID) {
7661 				ath12k_hw_warn(ah, "unable to allocate ML peer id for sta %pM",
7662 					       sta->addr);
7663 				goto exit;
7664 			}
7665 
7666 			dp_params.is_mlo = true;
7667 			dp_params.peer_id = ahsta->ml_peer_id | ATH12K_PEER_ML_ID_VALID;
7668 		}
7669 
7670 		dp_params.sta = sta;
7671 
7672 		if (vif->type == NL80211_IFTYPE_AP)
7673 			dp_params.ucast_ra_only = true;
7674 
7675 		ret = ath12k_dp_peer_create(&ah->dp_hw, sta->addr, &dp_params);
7676 		if (ret) {
7677 			ath12k_hw_warn(ah, "unable to create ath12k_dp_peer for sta %pM, ret: %d",
7678 				       sta->addr, ret);
7679 
7680 			goto ml_peer_id_clear;
7681 		}
7682 
7683 		ret = ath12k_mac_assign_link_sta(ah, ahsta, arsta, ahvif,
7684 						 link_id);
7685 		if (ret) {
7686 			ath12k_hw_warn(ah, "unable assign link %d for sta %pM",
7687 				       link_id, sta->addr);
7688 			goto peer_delete;
7689 		}
7690 
7691 		/* above arsta will get memset, hence do this after assign
7692 		 * link sta
7693 		 */
7694 		if (sta->mlo) {
7695 			/* For station mode, arvif->is_sta_assoc_link has been set when
7696 			 * vdev starts. Make sure the arvif/arsta pair have same setting
7697 			 */
7698 			if (vif->type == NL80211_IFTYPE_STATION &&
7699 			    !arsta->arvif->is_sta_assoc_link) {
7700 				ath12k_hw_warn(ah, "failed to verify assoc link setting with link id %u\n",
7701 					       link_id);
7702 				ret = -EINVAL;
7703 				goto exit;
7704 			}
7705 
7706 			arsta->is_assoc_link = true;
7707 			ahsta->assoc_link_id = link_id;
7708 		}
7709 	}
7710 
7711 	/* In the ML station scenario, activate all partner links once the
7712 	 * client is transitioning to the associated state.
7713 	 *
7714 	 * FIXME: Ideally, this activation should occur when the client
7715 	 * transitions to the authorized state. However, there are some
7716 	 * issues with handling this in the firmware. Until the firmware
7717 	 * can manage it properly, activate the links when the client is
7718 	 * about to move to the associated state.
7719 	 */
7720 	if (ieee80211_vif_is_mld(vif) && vif->type == NL80211_IFTYPE_STATION &&
7721 	    old_state == IEEE80211_STA_AUTH && new_state == IEEE80211_STA_ASSOC) {
7722 		/* TODO: for now only do link selection for single device
7723 		 * MLO case. Other cases would be handled in the future.
7724 		 */
7725 		ab = ah->radio[0].ab;
7726 		if (ab->ag->num_devices == 1) {
7727 			ret = ath12k_mac_select_links(ab, vif, hw, &selected_links);
7728 			if (ret) {
7729 				ath12k_warn(ab,
7730 					    "failed to get selected links: %d\n", ret);
7731 				goto exit;
7732 			}
7733 		} else {
7734 			selected_links = ieee80211_vif_usable_links(vif);
7735 		}
7736 
7737 		ieee80211_set_active_links(vif, selected_links);
7738 	}
7739 
7740 	/* Handle all the other state transitions in generic way */
7741 	valid_links = ahsta->links_map;
7742 	for_each_set_bit(link_id, &valid_links, IEEE80211_MLD_MAX_NUM_LINKS) {
7743 		arvif = wiphy_dereference(hw->wiphy, ahvif->link[link_id]);
7744 		arsta = wiphy_dereference(hw->wiphy, ahsta->link[link_id]);
7745 		/* some assumptions went wrong! */
7746 		if (WARN_ON(!arvif || !arsta))
7747 			continue;
7748 
7749 		/* vdev might be in deleted */
7750 		if (WARN_ON(!arvif->ar))
7751 			continue;
7752 
7753 		ret = ath12k_mac_handle_link_sta_state(hw, arvif, arsta,
7754 						       old_state, new_state);
7755 		if (ret) {
7756 			ath12k_hw_warn(ah, "unable to move link sta %d of sta %pM from state %d to %d",
7757 				       link_id, arsta->addr, old_state, new_state);
7758 
7759 			if (old_state == IEEE80211_STA_NOTEXIST &&
7760 			    new_state == IEEE80211_STA_NONE)
7761 				goto peer_delete;
7762 			else
7763 				goto exit;
7764 		}
7765 	}
7766 
7767 	if (ieee80211_vif_is_mld(vif) && vif->type == NL80211_IFTYPE_STATION &&
7768 	    old_state == IEEE80211_STA_ASSOC && new_state == IEEE80211_STA_AUTHORIZED) {
7769 		for_each_ar(ah, ar, i) {
7770 			ab = ar->ab;
7771 			if (prev_ab == ab)
7772 				continue;
7773 
7774 			ret = ath12k_mac_mlo_sta_update_link_active(ab, hw, ahvif);
7775 			if (ret) {
7776 				ath12k_warn(ab,
7777 					    "failed to update link active state on connect %d\n",
7778 					    ret);
7779 				goto exit;
7780 			}
7781 
7782 			prev_ab = ab;
7783 		}
7784 	}
7785 	/* IEEE80211_STA_NONE -> IEEE80211_STA_NOTEXIST:
7786 	 * Remove the station from driver (handle ML sta here since that
7787 	 * needs special handling. Normal sta will be handled in generic
7788 	 * handler below
7789 	 */
7790 	if (old_state == IEEE80211_STA_NONE &&
7791 	    new_state == IEEE80211_STA_NOTEXIST) {
7792 		if (sta->mlo)
7793 			ath12k_mac_ml_station_remove(ahvif, ahsta);
7794 
7795 		ath12k_dp_peer_delete(&ah->dp_hw, sta->addr, sta);
7796 	}
7797 
7798 	ret = 0;
7799 	goto exit;
7800 
7801 peer_delete:
7802 	ath12k_dp_peer_delete(&ah->dp_hw, sta->addr, sta);
7803 ml_peer_id_clear:
7804 	if (sta->mlo) {
7805 		clear_bit(ahsta->ml_peer_id, ah->free_ml_peer_id_map);
7806 		ahsta->ml_peer_id = ATH12K_MLO_PEER_ID_INVALID;
7807 	}
7808 exit:
7809 	/* update the state if everything went well */
7810 	if (!ret)
7811 		ahsta->state = new_state;
7812 
7813 	return ret;
7814 }
7815 EXPORT_SYMBOL(ath12k_mac_op_sta_state);
7816 
7817 int ath12k_mac_op_sta_set_txpwr(struct ieee80211_hw *hw,
7818 				struct ieee80211_vif *vif,
7819 				struct ieee80211_sta *sta)
7820 {
7821 	struct ath12k_sta *ahsta = ath12k_sta_to_ahsta(sta);
7822 	struct ath12k *ar;
7823 	struct ath12k_vif *ahvif = ath12k_vif_to_ahvif(vif);
7824 	struct ath12k_link_vif *arvif;
7825 	struct ath12k_link_sta *arsta;
7826 	u8 link_id;
7827 	int ret;
7828 	s16 txpwr;
7829 
7830 	lockdep_assert_wiphy(hw->wiphy);
7831 
7832 	/* TODO: use link id from mac80211 once that's implemented */
7833 	link_id = 0;
7834 
7835 	arvif = wiphy_dereference(hw->wiphy, ahvif->link[link_id]);
7836 	arsta = wiphy_dereference(hw->wiphy, ahsta->link[link_id]);
7837 
7838 	if (sta->deflink.txpwr.type == NL80211_TX_POWER_AUTOMATIC) {
7839 		txpwr = 0;
7840 	} else {
7841 		txpwr = sta->deflink.txpwr.power;
7842 		if (!txpwr) {
7843 			ret = -EINVAL;
7844 			goto out;
7845 		}
7846 	}
7847 
7848 	if (txpwr > ATH12K_TX_POWER_MAX_VAL || txpwr < ATH12K_TX_POWER_MIN_VAL) {
7849 		ret = -EINVAL;
7850 		goto out;
7851 	}
7852 
7853 	ar = arvif->ar;
7854 
7855 	ret = ath12k_wmi_set_peer_param(ar, arsta->addr, arvif->vdev_id,
7856 					WMI_PEER_USE_FIXED_PWR, txpwr);
7857 	if (ret) {
7858 		ath12k_warn(ar->ab, "failed to set tx power for station ret: %d\n",
7859 			    ret);
7860 		goto out;
7861 	}
7862 
7863 out:
7864 	return ret;
7865 }
7866 EXPORT_SYMBOL(ath12k_mac_op_sta_set_txpwr);
7867 
7868 void ath12k_mac_op_link_sta_rc_update(struct ieee80211_hw *hw,
7869 				      struct ieee80211_vif *vif,
7870 				      struct ieee80211_link_sta *link_sta,
7871 				      u32 changed)
7872 {
7873 	struct ieee80211_sta *sta = link_sta->sta;
7874 	struct ath12k *ar;
7875 	struct ath12k_sta *ahsta = ath12k_sta_to_ahsta(sta);
7876 	struct ath12k_vif *ahvif = ath12k_vif_to_ahvif(vif);
7877 	struct ath12k_hw *ah = ath12k_hw_to_ah(hw);
7878 	struct ath12k_link_sta *arsta;
7879 	struct ath12k_link_vif *arvif;
7880 	struct ath12k_dp_link_peer *peer;
7881 	u32 bw, smps;
7882 	struct ath12k_dp *dp;
7883 
7884 	rcu_read_lock();
7885 	arvif = rcu_dereference(ahvif->link[link_sta->link_id]);
7886 	if (!arvif) {
7887 		ath12k_hw_warn(ah, "mac sta rc update failed to fetch link vif on link id %u for peer %pM\n",
7888 			       link_sta->link_id, sta->addr);
7889 		rcu_read_unlock();
7890 		return;
7891 	}
7892 
7893 	ar = arvif->ar;
7894 	dp = ath12k_ab_to_dp(ar->ab);
7895 
7896 	arsta = rcu_dereference(ahsta->link[link_sta->link_id]);
7897 	if (!arsta) {
7898 		rcu_read_unlock();
7899 		ath12k_warn(ar->ab, "mac sta rc update failed to fetch link sta on link id %u for peer %pM\n",
7900 			    link_sta->link_id, sta->addr);
7901 		return;
7902 	}
7903 	spin_lock_bh(&dp->dp_lock);
7904 
7905 	peer = ath12k_dp_link_peer_find_by_vdev_and_addr(dp, arvif->vdev_id,
7906 							 arsta->addr);
7907 	if (!peer) {
7908 		spin_unlock_bh(&dp->dp_lock);
7909 		rcu_read_unlock();
7910 		ath12k_warn(ar->ab, "mac sta rc update failed to find peer %pM on vdev %i\n",
7911 			    arsta->addr, arvif->vdev_id);
7912 		return;
7913 	}
7914 
7915 	spin_unlock_bh(&dp->dp_lock);
7916 
7917 	if (arsta->link_id >= IEEE80211_MLD_MAX_NUM_LINKS) {
7918 		rcu_read_unlock();
7919 		return;
7920 	}
7921 
7922 	link_sta = rcu_dereference(sta->link[arsta->link_id]);
7923 	if (!link_sta) {
7924 		rcu_read_unlock();
7925 		ath12k_warn(ar->ab, "unable to access link sta in rc update for sta %pM link %u\n",
7926 			    sta->addr, arsta->link_id);
7927 		return;
7928 	}
7929 
7930 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
7931 		   "mac sta rc update for %pM changed %08x bw %d nss %d smps %d\n",
7932 		   arsta->addr, changed, link_sta->bandwidth, link_sta->rx_nss,
7933 		   link_sta->smps_mode);
7934 
7935 	spin_lock_bh(&ar->data_lock);
7936 
7937 	if (changed & IEEE80211_RC_BW_CHANGED) {
7938 		bw = ath12k_mac_ieee80211_sta_bw_to_wmi(ar, link_sta);
7939 		arsta->bw_prev = arsta->bw;
7940 		arsta->bw = bw;
7941 	}
7942 
7943 	if (changed & IEEE80211_RC_NSS_CHANGED)
7944 		arsta->nss = link_sta->rx_nss;
7945 
7946 	if (changed & IEEE80211_RC_SMPS_CHANGED) {
7947 		smps = WMI_PEER_SMPS_PS_NONE;
7948 
7949 		switch (link_sta->smps_mode) {
7950 		case IEEE80211_SMPS_AUTOMATIC:
7951 		case IEEE80211_SMPS_OFF:
7952 			smps = WMI_PEER_SMPS_PS_NONE;
7953 			break;
7954 		case IEEE80211_SMPS_STATIC:
7955 			smps = WMI_PEER_SMPS_STATIC;
7956 			break;
7957 		case IEEE80211_SMPS_DYNAMIC:
7958 			smps = WMI_PEER_SMPS_DYNAMIC;
7959 			break;
7960 		default:
7961 			ath12k_warn(ar->ab, "Invalid smps %d in sta rc update for %pM link %u\n",
7962 				    link_sta->smps_mode, arsta->addr, link_sta->link_id);
7963 			smps = WMI_PEER_SMPS_PS_NONE;
7964 			break;
7965 		}
7966 
7967 		arsta->smps = smps;
7968 	}
7969 
7970 	arsta->changed |= changed;
7971 
7972 	spin_unlock_bh(&ar->data_lock);
7973 
7974 	wiphy_work_queue(hw->wiphy, &arsta->update_wk);
7975 
7976 	rcu_read_unlock();
7977 }
7978 EXPORT_SYMBOL(ath12k_mac_op_link_sta_rc_update);
7979 
7980 static struct ath12k_link_sta *ath12k_mac_alloc_assign_link_sta(struct ath12k_hw *ah,
7981 								struct ath12k_sta *ahsta,
7982 								struct ath12k_vif *ahvif,
7983 								u8 link_id)
7984 {
7985 	struct ath12k_link_sta *arsta;
7986 	int ret;
7987 
7988 	lockdep_assert_wiphy(ah->hw->wiphy);
7989 
7990 	if (link_id >= IEEE80211_MLD_MAX_NUM_LINKS)
7991 		return NULL;
7992 
7993 	arsta = wiphy_dereference(ah->hw->wiphy, ahsta->link[link_id]);
7994 	if (arsta)
7995 		return NULL;
7996 
7997 	arsta = kmalloc_obj(*arsta);
7998 	if (!arsta)
7999 		return NULL;
8000 
8001 	ret = ath12k_mac_assign_link_sta(ah, ahsta, arsta, ahvif, link_id);
8002 	if (ret) {
8003 		kfree(arsta);
8004 		return NULL;
8005 	}
8006 
8007 	return arsta;
8008 }
8009 
8010 int ath12k_mac_op_change_sta_links(struct ieee80211_hw *hw,
8011 				   struct ieee80211_vif *vif,
8012 				   struct ieee80211_sta *sta,
8013 				   u16 old_links, u16 new_links)
8014 {
8015 	struct ath12k_vif *ahvif = ath12k_vif_to_ahvif(vif);
8016 	struct ath12k_sta *ahsta = ath12k_sta_to_ahsta(sta);
8017 	struct ath12k_hw *ah = hw->priv;
8018 	struct ath12k_link_vif *arvif;
8019 	struct ath12k_link_sta *arsta;
8020 	unsigned long valid_links;
8021 	struct ath12k *ar;
8022 	u8 link_id;
8023 	int ret;
8024 
8025 	lockdep_assert_wiphy(hw->wiphy);
8026 
8027 	if (!sta->valid_links)
8028 		return -EINVAL;
8029 
8030 	/* Firmware does not support removal of one of link stas. All sta
8031 	 * would be removed during ML STA delete in sta_state(), hence link
8032 	 * sta removal is not handled here.
8033 	 */
8034 	if (new_links < old_links)
8035 		return 0;
8036 
8037 	if (ahsta->ml_peer_id == ATH12K_MLO_PEER_ID_INVALID) {
8038 		ath12k_hw_warn(ah, "unable to add link for ml sta %pM", sta->addr);
8039 		return -EINVAL;
8040 	}
8041 
8042 	/* this op is expected only after initial sta insertion with default link */
8043 	if (WARN_ON(ahsta->links_map == 0))
8044 		return -EINVAL;
8045 
8046 	valid_links = new_links;
8047 	for_each_set_bit(link_id, &valid_links, IEEE80211_MLD_MAX_NUM_LINKS) {
8048 		if (ahsta->links_map & BIT(link_id))
8049 			continue;
8050 
8051 		arvif = wiphy_dereference(hw->wiphy, ahvif->link[link_id]);
8052 		arsta = ath12k_mac_alloc_assign_link_sta(ah, ahsta, ahvif, link_id);
8053 
8054 		if (!arvif || !arsta) {
8055 			ath12k_hw_warn(ah, "Failed to alloc/assign link sta");
8056 			continue;
8057 		}
8058 
8059 		ar = arvif->ar;
8060 		if (!ar)
8061 			continue;
8062 
8063 		ret = ath12k_mac_station_add(ar, arvif, arsta);
8064 		if (ret) {
8065 			ath12k_warn(ar->ab, "Failed to add station: %pM for VDEV: %d\n",
8066 				    arsta->addr, arvif->vdev_id);
8067 			ath12k_mac_free_unassign_link_sta(ah, ahsta, link_id);
8068 			return ret;
8069 		}
8070 	}
8071 
8072 	return 0;
8073 }
8074 EXPORT_SYMBOL(ath12k_mac_op_change_sta_links);
8075 
8076 bool ath12k_mac_op_can_activate_links(struct ieee80211_hw *hw,
8077 				      struct ieee80211_vif *vif,
8078 				      u16 active_links)
8079 {
8080 	/* TODO: Handle recovery case */
8081 
8082 	return true;
8083 }
8084 EXPORT_SYMBOL(ath12k_mac_op_can_activate_links);
8085 
8086 static int ath12k_conf_tx_uapsd(struct ath12k_link_vif *arvif,
8087 				u16 ac, bool enable)
8088 {
8089 	struct ath12k *ar = arvif->ar;
8090 	struct ath12k_vif *ahvif = arvif->ahvif;
8091 	u32 value;
8092 	int ret;
8093 
8094 	if (ahvif->vdev_type != WMI_VDEV_TYPE_STA)
8095 		return 0;
8096 
8097 	switch (ac) {
8098 	case IEEE80211_AC_VO:
8099 		value = WMI_STA_PS_UAPSD_AC3_DELIVERY_EN |
8100 			WMI_STA_PS_UAPSD_AC3_TRIGGER_EN;
8101 		break;
8102 	case IEEE80211_AC_VI:
8103 		value = WMI_STA_PS_UAPSD_AC2_DELIVERY_EN |
8104 			WMI_STA_PS_UAPSD_AC2_TRIGGER_EN;
8105 		break;
8106 	case IEEE80211_AC_BE:
8107 		value = WMI_STA_PS_UAPSD_AC1_DELIVERY_EN |
8108 			WMI_STA_PS_UAPSD_AC1_TRIGGER_EN;
8109 		break;
8110 	case IEEE80211_AC_BK:
8111 		value = WMI_STA_PS_UAPSD_AC0_DELIVERY_EN |
8112 			WMI_STA_PS_UAPSD_AC0_TRIGGER_EN;
8113 		break;
8114 	}
8115 
8116 	if (enable)
8117 		ahvif->u.sta.uapsd |= value;
8118 	else
8119 		ahvif->u.sta.uapsd &= ~value;
8120 
8121 	ret = ath12k_wmi_set_sta_ps_param(ar, arvif->vdev_id,
8122 					  WMI_STA_PS_PARAM_UAPSD,
8123 					  ahvif->u.sta.uapsd);
8124 	if (ret) {
8125 		ath12k_warn(ar->ab, "could not set uapsd params %d\n", ret);
8126 		goto exit;
8127 	}
8128 
8129 	if (ahvif->u.sta.uapsd)
8130 		value = WMI_STA_PS_RX_WAKE_POLICY_POLL_UAPSD;
8131 	else
8132 		value = WMI_STA_PS_RX_WAKE_POLICY_WAKE;
8133 
8134 	ret = ath12k_wmi_set_sta_ps_param(ar, arvif->vdev_id,
8135 					  WMI_STA_PS_PARAM_RX_WAKE_POLICY,
8136 					  value);
8137 	if (ret)
8138 		ath12k_warn(ar->ab, "could not set rx wake param %d\n", ret);
8139 
8140 exit:
8141 	return ret;
8142 }
8143 
8144 static int ath12k_mac_conf_tx(struct ath12k_link_vif *arvif, u16 ac,
8145 			      const struct ieee80211_tx_queue_params *params)
8146 {
8147 	struct wmi_wmm_params_arg *p = NULL;
8148 	struct ath12k *ar = arvif->ar;
8149 	struct ath12k_base *ab = ar->ab;
8150 	int ret;
8151 
8152 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
8153 
8154 	switch (ac) {
8155 	case IEEE80211_AC_VO:
8156 		p = &arvif->wmm_params.ac_vo;
8157 		break;
8158 	case IEEE80211_AC_VI:
8159 		p = &arvif->wmm_params.ac_vi;
8160 		break;
8161 	case IEEE80211_AC_BE:
8162 		p = &arvif->wmm_params.ac_be;
8163 		break;
8164 	case IEEE80211_AC_BK:
8165 		p = &arvif->wmm_params.ac_bk;
8166 		break;
8167 	}
8168 
8169 	if (WARN_ON(!p)) {
8170 		ret = -EINVAL;
8171 		goto exit;
8172 	}
8173 
8174 	p->cwmin = params->cw_min;
8175 	p->cwmax = params->cw_max;
8176 	p->aifs = params->aifs;
8177 	p->txop = params->txop;
8178 
8179 	ret = ath12k_wmi_send_wmm_update_cmd(ar, arvif->vdev_id,
8180 					     &arvif->wmm_params);
8181 	if (ret) {
8182 		ath12k_warn(ab, "pdev idx %d failed to set wmm params: %d\n",
8183 			    ar->pdev_idx, ret);
8184 		goto exit;
8185 	}
8186 
8187 	ret = ath12k_conf_tx_uapsd(arvif, ac, params->uapsd);
8188 	if (ret)
8189 		ath12k_warn(ab, "pdev idx %d failed to set sta uapsd: %d\n",
8190 			    ar->pdev_idx, ret);
8191 
8192 exit:
8193 	return ret;
8194 }
8195 
8196 int ath12k_mac_op_conf_tx(struct ieee80211_hw *hw,
8197 			  struct ieee80211_vif *vif,
8198 			  unsigned int link_id, u16 ac,
8199 			  const struct ieee80211_tx_queue_params *params)
8200 {
8201 	struct ath12k_vif *ahvif = ath12k_vif_to_ahvif(vif);
8202 	struct ath12k_link_vif *arvif;
8203 	struct ath12k_vif_cache *cache;
8204 	int ret;
8205 
8206 	lockdep_assert_wiphy(hw->wiphy);
8207 
8208 	if (link_id >= IEEE80211_MLD_MAX_NUM_LINKS)
8209 		return -EINVAL;
8210 
8211 	arvif = wiphy_dereference(hw->wiphy, ahvif->link[link_id]);
8212 	if (!arvif || !arvif->is_created) {
8213 		cache = ath12k_ahvif_get_link_cache(ahvif, link_id);
8214 		if (!cache)
8215 			return -ENOSPC;
8216 
8217 		cache->tx_conf.changed = true;
8218 		cache->tx_conf.ac = ac;
8219 		cache->tx_conf.tx_queue_params = *params;
8220 
8221 		return 0;
8222 	}
8223 
8224 	ret = ath12k_mac_conf_tx(arvif, ac, params);
8225 
8226 	return ret;
8227 }
8228 EXPORT_SYMBOL(ath12k_mac_op_conf_tx);
8229 
8230 static struct ieee80211_sta_ht_cap
8231 ath12k_create_ht_cap(struct ath12k *ar, u32 ar_ht_cap, u32 rate_cap_rx_chainmask)
8232 {
8233 	int i;
8234 	struct ieee80211_sta_ht_cap ht_cap = {};
8235 	u32 ar_vht_cap = ar->pdev->cap.vht_cap;
8236 
8237 	if (!(ar_ht_cap & WMI_HT_CAP_ENABLED))
8238 		return ht_cap;
8239 
8240 	ht_cap.ht_supported = 1;
8241 	ht_cap.ampdu_factor = IEEE80211_HT_MAX_AMPDU_64K;
8242 	ht_cap.ampdu_density = IEEE80211_HT_MPDU_DENSITY_NONE;
8243 	ht_cap.cap |= IEEE80211_HT_CAP_SUP_WIDTH_20_40;
8244 	ht_cap.cap |= IEEE80211_HT_CAP_DSSSCCK40;
8245 	ht_cap.cap |= WLAN_HT_CAP_SM_PS_STATIC << IEEE80211_HT_CAP_SM_PS_SHIFT;
8246 
8247 	if (ar_ht_cap & WMI_HT_CAP_HT20_SGI)
8248 		ht_cap.cap |= IEEE80211_HT_CAP_SGI_20;
8249 
8250 	if (ar_ht_cap & WMI_HT_CAP_HT40_SGI)
8251 		ht_cap.cap |= IEEE80211_HT_CAP_SGI_40;
8252 
8253 	if (ar_ht_cap & WMI_HT_CAP_DYNAMIC_SMPS) {
8254 		u32 smps;
8255 
8256 		smps   = WLAN_HT_CAP_SM_PS_DYNAMIC;
8257 		smps <<= IEEE80211_HT_CAP_SM_PS_SHIFT;
8258 
8259 		ht_cap.cap |= smps;
8260 	}
8261 
8262 	if (ar_ht_cap & WMI_HT_CAP_TX_STBC)
8263 		ht_cap.cap |= IEEE80211_HT_CAP_TX_STBC;
8264 
8265 	if (ar_ht_cap & WMI_HT_CAP_RX_STBC) {
8266 		u32 stbc;
8267 
8268 		stbc   = ar_ht_cap;
8269 		stbc  &= WMI_HT_CAP_RX_STBC;
8270 		stbc >>= WMI_HT_CAP_RX_STBC_MASK_SHIFT;
8271 		stbc <<= IEEE80211_HT_CAP_RX_STBC_SHIFT;
8272 		stbc  &= IEEE80211_HT_CAP_RX_STBC;
8273 
8274 		ht_cap.cap |= stbc;
8275 	}
8276 
8277 	if (ar_ht_cap & WMI_HT_CAP_RX_LDPC)
8278 		ht_cap.cap |= IEEE80211_HT_CAP_LDPC_CODING;
8279 
8280 	if (ar_ht_cap & WMI_HT_CAP_L_SIG_TXOP_PROT)
8281 		ht_cap.cap |= IEEE80211_HT_CAP_LSIG_TXOP_PROT;
8282 
8283 	if (ar_vht_cap & WMI_VHT_CAP_MAX_MPDU_LEN_MASK)
8284 		ht_cap.cap |= IEEE80211_HT_CAP_MAX_AMSDU;
8285 
8286 	for (i = 0; i < ar->num_rx_chains; i++) {
8287 		if (rate_cap_rx_chainmask & BIT(i))
8288 			ht_cap.mcs.rx_mask[i] = 0xFF;
8289 	}
8290 
8291 	ht_cap.mcs.tx_params |= IEEE80211_HT_MCS_TX_DEFINED;
8292 
8293 	return ht_cap;
8294 }
8295 
8296 static int ath12k_mac_set_txbf_conf(struct ath12k_link_vif *arvif)
8297 {
8298 	u32 value = 0;
8299 	struct ath12k *ar = arvif->ar;
8300 	struct ath12k_vif *ahvif = arvif->ahvif;
8301 	int nsts;
8302 	int sound_dim;
8303 	u32 vht_cap = ar->pdev->cap.vht_cap;
8304 	u32 vdev_param = WMI_VDEV_PARAM_TXBF;
8305 
8306 	if (vht_cap & (IEEE80211_VHT_CAP_SU_BEAMFORMEE_CAPABLE)) {
8307 		nsts = vht_cap & IEEE80211_VHT_CAP_BEAMFORMEE_STS_MASK;
8308 		nsts >>= IEEE80211_VHT_CAP_BEAMFORMEE_STS_SHIFT;
8309 		value |= SM(nsts, WMI_TXBF_STS_CAP_OFFSET);
8310 	}
8311 
8312 	if (vht_cap & (IEEE80211_VHT_CAP_SU_BEAMFORMER_CAPABLE)) {
8313 		sound_dim = vht_cap &
8314 			    IEEE80211_VHT_CAP_SOUNDING_DIMENSIONS_MASK;
8315 		sound_dim >>= IEEE80211_VHT_CAP_SOUNDING_DIMENSIONS_SHIFT;
8316 		if (sound_dim > (ar->num_tx_chains - 1))
8317 			sound_dim = ar->num_tx_chains - 1;
8318 		value |= SM(sound_dim, WMI_BF_SOUND_DIM_OFFSET);
8319 	}
8320 
8321 	if (!value)
8322 		return 0;
8323 
8324 	if (vht_cap & IEEE80211_VHT_CAP_SU_BEAMFORMER_CAPABLE) {
8325 		value |= WMI_VDEV_PARAM_TXBF_SU_TX_BFER;
8326 
8327 		if ((vht_cap & IEEE80211_VHT_CAP_MU_BEAMFORMER_CAPABLE) &&
8328 		    ahvif->vdev_type == WMI_VDEV_TYPE_AP)
8329 			value |= WMI_VDEV_PARAM_TXBF_MU_TX_BFER;
8330 	}
8331 
8332 	if (vht_cap & IEEE80211_VHT_CAP_SU_BEAMFORMEE_CAPABLE) {
8333 		value |= WMI_VDEV_PARAM_TXBF_SU_TX_BFEE;
8334 
8335 		if ((vht_cap & IEEE80211_VHT_CAP_MU_BEAMFORMEE_CAPABLE) &&
8336 		    ahvif->vdev_type == WMI_VDEV_TYPE_STA)
8337 			value |= WMI_VDEV_PARAM_TXBF_MU_TX_BFEE;
8338 	}
8339 
8340 	return ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
8341 					     vdev_param, value);
8342 }
8343 
8344 static void ath12k_set_vht_txbf_cap(struct ath12k *ar, u32 *vht_cap)
8345 {
8346 	bool subfer, subfee;
8347 	int sound_dim = 0;
8348 
8349 	subfer = !!(*vht_cap & (IEEE80211_VHT_CAP_SU_BEAMFORMER_CAPABLE));
8350 	subfee = !!(*vht_cap & (IEEE80211_VHT_CAP_SU_BEAMFORMEE_CAPABLE));
8351 
8352 	if (ar->num_tx_chains < 2) {
8353 		*vht_cap &= ~(IEEE80211_VHT_CAP_SU_BEAMFORMER_CAPABLE);
8354 		subfer = false;
8355 	}
8356 
8357 	/* If SU Beaformer is not set, then disable MU Beamformer Capability */
8358 	if (!subfer)
8359 		*vht_cap &= ~(IEEE80211_VHT_CAP_MU_BEAMFORMER_CAPABLE);
8360 
8361 	/* If SU Beaformee is not set, then disable MU Beamformee Capability */
8362 	if (!subfee)
8363 		*vht_cap &= ~(IEEE80211_VHT_CAP_MU_BEAMFORMEE_CAPABLE);
8364 
8365 	sound_dim = u32_get_bits(*vht_cap,
8366 				 IEEE80211_VHT_CAP_SOUNDING_DIMENSIONS_MASK);
8367 	*vht_cap = u32_replace_bits(*vht_cap, 0,
8368 				    IEEE80211_VHT_CAP_SOUNDING_DIMENSIONS_MASK);
8369 
8370 	/* TODO: Need to check invalid STS and Sound_dim values set by FW? */
8371 
8372 	/* Enable Sounding Dimension Field only if SU BF is enabled */
8373 	if (subfer) {
8374 		if (sound_dim > (ar->num_tx_chains - 1))
8375 			sound_dim = ar->num_tx_chains - 1;
8376 
8377 		*vht_cap = u32_replace_bits(*vht_cap, sound_dim,
8378 					    IEEE80211_VHT_CAP_SOUNDING_DIMENSIONS_MASK);
8379 	}
8380 
8381 	/* Use the STS advertised by FW unless SU Beamformee is not supported*/
8382 	if (!subfee)
8383 		*vht_cap &= ~(IEEE80211_VHT_CAP_BEAMFORMEE_STS_MASK);
8384 }
8385 
8386 static struct ieee80211_sta_vht_cap
8387 ath12k_create_vht_cap(struct ath12k *ar, u32 rate_cap_tx_chainmask,
8388 		      u32 rate_cap_rx_chainmask)
8389 {
8390 	struct ieee80211_sta_vht_cap vht_cap = {};
8391 	u16 txmcs_map, rxmcs_map;
8392 	int i;
8393 
8394 	vht_cap.vht_supported = 1;
8395 	vht_cap.cap = ar->pdev->cap.vht_cap;
8396 
8397 	ath12k_set_vht_txbf_cap(ar, &vht_cap.cap);
8398 
8399 	/* 80P80 is not supported */
8400 	vht_cap.cap &= ~IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_160_80PLUS80MHZ;
8401 
8402 	rxmcs_map = 0;
8403 	txmcs_map = 0;
8404 	for (i = 0; i < 8; i++) {
8405 		if (i < ar->num_tx_chains && rate_cap_tx_chainmask & BIT(i))
8406 			txmcs_map |= IEEE80211_VHT_MCS_SUPPORT_0_9 << (i * 2);
8407 		else
8408 			txmcs_map |= IEEE80211_VHT_MCS_NOT_SUPPORTED << (i * 2);
8409 
8410 		if (i < ar->num_rx_chains && rate_cap_rx_chainmask & BIT(i))
8411 			rxmcs_map |= IEEE80211_VHT_MCS_SUPPORT_0_9 << (i * 2);
8412 		else
8413 			rxmcs_map |= IEEE80211_VHT_MCS_NOT_SUPPORTED << (i * 2);
8414 	}
8415 
8416 	if (rate_cap_tx_chainmask <= 1)
8417 		vht_cap.cap &= ~IEEE80211_VHT_CAP_TXSTBC;
8418 
8419 	vht_cap.vht_mcs.rx_mcs_map = cpu_to_le16(rxmcs_map);
8420 	vht_cap.vht_mcs.tx_mcs_map = cpu_to_le16(txmcs_map);
8421 
8422 	/* Check if the HW supports 1:1 NSS ratio and reset
8423 	 * EXT NSS BW Support field to 0 to indicate 1:1 ratio
8424 	 */
8425 	if (ar->pdev->cap.nss_ratio_info == WMI_NSS_RATIO_1_NSS)
8426 		vht_cap.cap &= ~IEEE80211_VHT_CAP_EXT_NSS_BW_MASK;
8427 
8428 	return vht_cap;
8429 }
8430 
8431 static void ath12k_mac_setup_ht_vht_cap(struct ath12k *ar,
8432 					struct ath12k_pdev_cap *cap,
8433 					u32 *ht_cap_info)
8434 {
8435 	struct ieee80211_supported_band *band;
8436 	u32 rate_cap_tx_chainmask;
8437 	u32 rate_cap_rx_chainmask;
8438 	u32 ht_cap;
8439 
8440 	rate_cap_tx_chainmask = ar->cfg_tx_chainmask >> cap->tx_chain_mask_shift;
8441 	rate_cap_rx_chainmask = ar->cfg_rx_chainmask >> cap->rx_chain_mask_shift;
8442 
8443 	if (cap->supported_bands & WMI_HOST_WLAN_2GHZ_CAP) {
8444 		band = &ar->mac.sbands[NL80211_BAND_2GHZ];
8445 		ht_cap = cap->band[NL80211_BAND_2GHZ].ht_cap_info;
8446 		if (ht_cap_info)
8447 			*ht_cap_info = ht_cap;
8448 		band->ht_cap = ath12k_create_ht_cap(ar, ht_cap,
8449 						    rate_cap_rx_chainmask);
8450 	}
8451 
8452 	if (cap->supported_bands & WMI_HOST_WLAN_5GHZ_CAP &&
8453 	    (ar->ab->hw_params->single_pdev_only ||
8454 	     !ar->supports_6ghz)) {
8455 		band = &ar->mac.sbands[NL80211_BAND_5GHZ];
8456 		ht_cap = cap->band[NL80211_BAND_5GHZ].ht_cap_info;
8457 		if (ht_cap_info)
8458 			*ht_cap_info = ht_cap;
8459 		band->ht_cap = ath12k_create_ht_cap(ar, ht_cap,
8460 						    rate_cap_rx_chainmask);
8461 		band->vht_cap = ath12k_create_vht_cap(ar, rate_cap_tx_chainmask,
8462 						      rate_cap_rx_chainmask);
8463 	}
8464 }
8465 
8466 static int ath12k_check_chain_mask(struct ath12k *ar, u32 ant, bool is_tx_ant)
8467 {
8468 	/* TODO: Check the request chainmask against the supported
8469 	 * chainmask table which is advertised in extented_service_ready event
8470 	 */
8471 
8472 	return 0;
8473 }
8474 
8475 static void ath12k_gen_ppe_thresh(struct ath12k_wmi_ppe_threshold_arg *fw_ppet,
8476 				  u8 *he_ppet)
8477 {
8478 	int nss, ru;
8479 	u8 bit = 7;
8480 
8481 	he_ppet[0] = fw_ppet->numss_m1 & IEEE80211_PPE_THRES_NSS_MASK;
8482 	he_ppet[0] |= (fw_ppet->ru_bit_mask <<
8483 		       IEEE80211_PPE_THRES_RU_INDEX_BITMASK_POS) &
8484 		      IEEE80211_PPE_THRES_RU_INDEX_BITMASK_MASK;
8485 	for (nss = 0; nss <= fw_ppet->numss_m1; nss++) {
8486 		for (ru = 0; ru < 4; ru++) {
8487 			u8 val;
8488 			int i;
8489 
8490 			if ((fw_ppet->ru_bit_mask & BIT(ru)) == 0)
8491 				continue;
8492 			val = (fw_ppet->ppet16_ppet8_ru3_ru0[nss] >> (ru * 6)) &
8493 			       0x3f;
8494 			val = ((val >> 3) & 0x7) | ((val & 0x7) << 3);
8495 			for (i = 5; i >= 0; i--) {
8496 				he_ppet[bit / 8] |=
8497 					((val >> i) & 0x1) << ((bit % 8));
8498 				bit++;
8499 			}
8500 		}
8501 	}
8502 }
8503 
8504 static void
8505 ath12k_mac_filter_he_cap_mesh(struct ieee80211_he_cap_elem *he_cap_elem)
8506 {
8507 	u8 m;
8508 
8509 	m = IEEE80211_HE_MAC_CAP0_TWT_RES |
8510 	    IEEE80211_HE_MAC_CAP0_TWT_REQ;
8511 	he_cap_elem->mac_cap_info[0] &= ~m;
8512 
8513 	m = IEEE80211_HE_MAC_CAP2_TRS |
8514 	    IEEE80211_HE_MAC_CAP2_BCAST_TWT |
8515 	    IEEE80211_HE_MAC_CAP2_MU_CASCADING;
8516 	he_cap_elem->mac_cap_info[2] &= ~m;
8517 
8518 	m = IEEE80211_HE_MAC_CAP3_FLEX_TWT_SCHED |
8519 	    IEEE80211_HE_MAC_CAP2_BCAST_TWT |
8520 	    IEEE80211_HE_MAC_CAP2_MU_CASCADING;
8521 	he_cap_elem->mac_cap_info[3] &= ~m;
8522 
8523 	m = IEEE80211_HE_MAC_CAP4_BSRP_BQRP_A_MPDU_AGG |
8524 	    IEEE80211_HE_MAC_CAP4_BQR;
8525 	he_cap_elem->mac_cap_info[4] &= ~m;
8526 
8527 	m = IEEE80211_HE_MAC_CAP5_SUBCHAN_SELECTIVE_TRANSMISSION |
8528 	    IEEE80211_HE_MAC_CAP5_UL_2x996_TONE_RU |
8529 	    IEEE80211_HE_MAC_CAP5_PUNCTURED_SOUNDING |
8530 	    IEEE80211_HE_MAC_CAP5_HT_VHT_TRIG_FRAME_RX;
8531 	he_cap_elem->mac_cap_info[5] &= ~m;
8532 
8533 	m = IEEE80211_HE_PHY_CAP2_UL_MU_FULL_MU_MIMO |
8534 	    IEEE80211_HE_PHY_CAP2_UL_MU_PARTIAL_MU_MIMO;
8535 	he_cap_elem->phy_cap_info[2] &= ~m;
8536 
8537 	m = IEEE80211_HE_PHY_CAP3_RX_PARTIAL_BW_SU_IN_20MHZ_MU |
8538 	    IEEE80211_HE_PHY_CAP3_DCM_MAX_CONST_TX_MASK |
8539 	    IEEE80211_HE_PHY_CAP3_DCM_MAX_CONST_RX_MASK;
8540 	he_cap_elem->phy_cap_info[3] &= ~m;
8541 
8542 	m = IEEE80211_HE_PHY_CAP4_MU_BEAMFORMER;
8543 	he_cap_elem->phy_cap_info[4] &= ~m;
8544 
8545 	m = IEEE80211_HE_PHY_CAP5_NG16_MU_FEEDBACK;
8546 	he_cap_elem->phy_cap_info[5] &= ~m;
8547 
8548 	m = IEEE80211_HE_PHY_CAP6_CODEBOOK_SIZE_75_MU |
8549 	    IEEE80211_HE_PHY_CAP6_TRIG_MU_BEAMFORMING_PARTIAL_BW_FB |
8550 	    IEEE80211_HE_PHY_CAP6_TRIG_CQI_FB |
8551 	    IEEE80211_HE_PHY_CAP6_PARTIAL_BANDWIDTH_DL_MUMIMO;
8552 	he_cap_elem->phy_cap_info[6] &= ~m;
8553 
8554 	m = IEEE80211_HE_PHY_CAP7_PSR_BASED_SR |
8555 	    IEEE80211_HE_PHY_CAP7_POWER_BOOST_FACTOR_SUPP |
8556 	    IEEE80211_HE_PHY_CAP7_STBC_TX_ABOVE_80MHZ |
8557 	    IEEE80211_HE_PHY_CAP7_STBC_RX_ABOVE_80MHZ;
8558 	he_cap_elem->phy_cap_info[7] &= ~m;
8559 
8560 	m = IEEE80211_HE_PHY_CAP8_HE_ER_SU_PPDU_4XLTF_AND_08_US_GI |
8561 	    IEEE80211_HE_PHY_CAP8_20MHZ_IN_40MHZ_HE_PPDU_IN_2G |
8562 	    IEEE80211_HE_PHY_CAP8_20MHZ_IN_160MHZ_HE_PPDU |
8563 	    IEEE80211_HE_PHY_CAP8_80MHZ_IN_160MHZ_HE_PPDU;
8564 	he_cap_elem->phy_cap_info[8] &= ~m;
8565 
8566 	m = IEEE80211_HE_PHY_CAP9_LONGER_THAN_16_SIGB_OFDM_SYM |
8567 	    IEEE80211_HE_PHY_CAP9_NON_TRIGGERED_CQI_FEEDBACK |
8568 	    IEEE80211_HE_PHY_CAP9_RX_1024_QAM_LESS_THAN_242_TONE_RU |
8569 	    IEEE80211_HE_PHY_CAP9_TX_1024_QAM_LESS_THAN_242_TONE_RU |
8570 	    IEEE80211_HE_PHY_CAP9_RX_FULL_BW_SU_USING_MU_WITH_COMP_SIGB |
8571 	    IEEE80211_HE_PHY_CAP9_RX_FULL_BW_SU_USING_MU_WITH_NON_COMP_SIGB;
8572 	he_cap_elem->phy_cap_info[9] &= ~m;
8573 }
8574 
8575 static __le16 ath12k_mac_setup_he_6ghz_cap(struct ath12k_pdev_cap *pcap,
8576 					   struct ath12k_band_cap *bcap)
8577 {
8578 	u8 val;
8579 
8580 	bcap->he_6ghz_capa = IEEE80211_HT_MPDU_DENSITY_NONE;
8581 	if (bcap->ht_cap_info & WMI_HT_CAP_DYNAMIC_SMPS)
8582 		bcap->he_6ghz_capa |=
8583 			u32_encode_bits(WLAN_HT_CAP_SM_PS_DYNAMIC,
8584 					IEEE80211_HE_6GHZ_CAP_SM_PS);
8585 	else
8586 		bcap->he_6ghz_capa |=
8587 			u32_encode_bits(WLAN_HT_CAP_SM_PS_DISABLED,
8588 					IEEE80211_HE_6GHZ_CAP_SM_PS);
8589 	val = u32_get_bits(pcap->vht_cap,
8590 			   IEEE80211_VHT_CAP_MAX_A_MPDU_LENGTH_EXPONENT_MASK);
8591 	bcap->he_6ghz_capa |=
8592 		u32_encode_bits(val, IEEE80211_HE_6GHZ_CAP_MAX_AMPDU_LEN_EXP);
8593 	val = u32_get_bits(pcap->vht_cap,
8594 			   IEEE80211_VHT_CAP_MAX_MPDU_MASK);
8595 	bcap->he_6ghz_capa |=
8596 		u32_encode_bits(val, IEEE80211_HE_6GHZ_CAP_MAX_MPDU_LEN);
8597 	if (pcap->vht_cap & IEEE80211_VHT_CAP_RX_ANTENNA_PATTERN)
8598 		bcap->he_6ghz_capa |= IEEE80211_HE_6GHZ_CAP_RX_ANTPAT_CONS;
8599 	if (pcap->vht_cap & IEEE80211_VHT_CAP_TX_ANTENNA_PATTERN)
8600 		bcap->he_6ghz_capa |= IEEE80211_HE_6GHZ_CAP_TX_ANTPAT_CONS;
8601 
8602 	return cpu_to_le16(bcap->he_6ghz_capa);
8603 }
8604 
8605 static void ath12k_mac_set_hemcsmap(struct ath12k *ar,
8606 				    struct ath12k_pdev_cap *cap,
8607 				    struct ieee80211_sta_he_cap *he_cap)
8608 {
8609 	struct ieee80211_he_mcs_nss_supp *mcs_nss = &he_cap->he_mcs_nss_supp;
8610 	u8 maxtxnss_160 = ath12k_get_nss_160mhz(ar, ar->num_tx_chains);
8611 	u8 maxrxnss_160 = ath12k_get_nss_160mhz(ar, ar->num_rx_chains);
8612 	u16 txmcs_map_160 = 0, rxmcs_map_160 = 0;
8613 	u16 txmcs_map = 0, rxmcs_map = 0;
8614 	u32 i;
8615 
8616 	for (i = 0; i < 8; i++) {
8617 		if (i < ar->num_tx_chains &&
8618 		    (ar->cfg_tx_chainmask >> cap->tx_chain_mask_shift) & BIT(i))
8619 			txmcs_map |= IEEE80211_HE_MCS_SUPPORT_0_11 << (i * 2);
8620 		else
8621 			txmcs_map |= IEEE80211_HE_MCS_NOT_SUPPORTED << (i * 2);
8622 
8623 		if (i < ar->num_rx_chains &&
8624 		    (ar->cfg_rx_chainmask >> cap->tx_chain_mask_shift) & BIT(i))
8625 			rxmcs_map |= IEEE80211_HE_MCS_SUPPORT_0_11 << (i * 2);
8626 		else
8627 			rxmcs_map |= IEEE80211_HE_MCS_NOT_SUPPORTED << (i * 2);
8628 
8629 		if (i < maxtxnss_160 &&
8630 		    (ar->cfg_tx_chainmask >> cap->tx_chain_mask_shift) & BIT(i))
8631 			txmcs_map_160 |= IEEE80211_HE_MCS_SUPPORT_0_11 << (i * 2);
8632 		else
8633 			txmcs_map_160 |= IEEE80211_HE_MCS_NOT_SUPPORTED << (i * 2);
8634 
8635 		if (i < maxrxnss_160 &&
8636 		    (ar->cfg_tx_chainmask >> cap->tx_chain_mask_shift) & BIT(i))
8637 			rxmcs_map_160 |= IEEE80211_HE_MCS_SUPPORT_0_11 << (i * 2);
8638 		else
8639 			rxmcs_map_160 |= IEEE80211_HE_MCS_NOT_SUPPORTED << (i * 2);
8640 	}
8641 
8642 	mcs_nss->rx_mcs_80 = cpu_to_le16(rxmcs_map & 0xffff);
8643 	mcs_nss->tx_mcs_80 = cpu_to_le16(txmcs_map & 0xffff);
8644 	mcs_nss->rx_mcs_160 = cpu_to_le16(rxmcs_map_160 & 0xffff);
8645 	mcs_nss->tx_mcs_160 = cpu_to_le16(txmcs_map_160 & 0xffff);
8646 }
8647 
8648 static void ath12k_mac_copy_he_cap(struct ath12k *ar,
8649 				   struct ath12k_band_cap *band_cap,
8650 				   int iftype, u8 num_tx_chains,
8651 				   struct ieee80211_sta_he_cap *he_cap)
8652 {
8653 	struct ieee80211_he_cap_elem *he_cap_elem = &he_cap->he_cap_elem;
8654 
8655 	he_cap->has_he = true;
8656 	memcpy(he_cap_elem->mac_cap_info, band_cap->he_cap_info,
8657 	       sizeof(he_cap_elem->mac_cap_info));
8658 	memcpy(he_cap_elem->phy_cap_info, band_cap->he_cap_phy_info,
8659 	       sizeof(he_cap_elem->phy_cap_info));
8660 
8661 	he_cap_elem->mac_cap_info[1] &=
8662 		IEEE80211_HE_MAC_CAP1_TF_MAC_PAD_DUR_MASK;
8663 	he_cap_elem->phy_cap_info[0] &=
8664 		IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_40MHZ_IN_2G |
8665 		IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_40MHZ_80MHZ_IN_5G |
8666 		IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_160MHZ_IN_5G;
8667 	/* 80PLUS80 is not supported */
8668 	he_cap_elem->phy_cap_info[0] &=
8669 		~IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_80PLUS80_MHZ_IN_5G;
8670 	he_cap_elem->phy_cap_info[5] &=
8671 		~IEEE80211_HE_PHY_CAP5_BEAMFORMEE_NUM_SND_DIM_UNDER_80MHZ_MASK;
8672 	he_cap_elem->phy_cap_info[5] |= num_tx_chains - 1;
8673 
8674 	switch (iftype) {
8675 	case NL80211_IFTYPE_AP:
8676 		he_cap_elem->mac_cap_info[2] &=
8677 			~IEEE80211_HE_MAC_CAP2_BCAST_TWT;
8678 		he_cap_elem->phy_cap_info[3] &=
8679 			~IEEE80211_HE_PHY_CAP3_DCM_MAX_CONST_TX_MASK;
8680 		he_cap_elem->phy_cap_info[9] |=
8681 			IEEE80211_HE_PHY_CAP9_RX_1024_QAM_LESS_THAN_242_TONE_RU;
8682 		break;
8683 	case NL80211_IFTYPE_STATION:
8684 		he_cap_elem->mac_cap_info[0] &= ~IEEE80211_HE_MAC_CAP0_TWT_RES;
8685 		he_cap_elem->mac_cap_info[0] |= IEEE80211_HE_MAC_CAP0_TWT_REQ;
8686 		he_cap_elem->phy_cap_info[9] |=
8687 			IEEE80211_HE_PHY_CAP9_TX_1024_QAM_LESS_THAN_242_TONE_RU;
8688 		break;
8689 	case NL80211_IFTYPE_MESH_POINT:
8690 		ath12k_mac_filter_he_cap_mesh(he_cap_elem);
8691 		break;
8692 	}
8693 
8694 	ath12k_mac_set_hemcsmap(ar, &ar->pdev->cap, he_cap);
8695 	memset(he_cap->ppe_thres, 0, sizeof(he_cap->ppe_thres));
8696 	if (he_cap_elem->phy_cap_info[6] &
8697 	    IEEE80211_HE_PHY_CAP6_PPE_THRESHOLD_PRESENT)
8698 		ath12k_gen_ppe_thresh(&band_cap->he_ppet, he_cap->ppe_thres);
8699 }
8700 
8701 static void
8702 ath12k_mac_copy_eht_mcs_nss(struct ath12k_band_cap *band_cap,
8703 			    struct ieee80211_eht_mcs_nss_supp *mcs_nss,
8704 			    const struct ieee80211_he_cap_elem *he_cap,
8705 			    const struct ieee80211_eht_cap_elem_fixed *eht_cap)
8706 {
8707 	if ((he_cap->phy_cap_info[0] &
8708 	     (IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_40MHZ_IN_2G |
8709 	      IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_40MHZ_80MHZ_IN_5G |
8710 	      IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_160MHZ_IN_5G |
8711 	      IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_80PLUS80_MHZ_IN_5G)) == 0)
8712 		memcpy(&mcs_nss->only_20mhz, &band_cap->eht_mcs_20_only,
8713 		       sizeof(struct ieee80211_eht_mcs_nss_supp_20mhz_only));
8714 
8715 	if (he_cap->phy_cap_info[0] &
8716 	    (IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_40MHZ_IN_2G |
8717 	     IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_40MHZ_80MHZ_IN_5G))
8718 		memcpy(&mcs_nss->bw._80, &band_cap->eht_mcs_80,
8719 		       sizeof(struct ieee80211_eht_mcs_nss_supp_bw));
8720 
8721 	if (he_cap->phy_cap_info[0] &
8722 	    IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_160MHZ_IN_5G)
8723 		memcpy(&mcs_nss->bw._160, &band_cap->eht_mcs_160,
8724 		       sizeof(struct ieee80211_eht_mcs_nss_supp_bw));
8725 
8726 	if (eht_cap->phy_cap_info[0] & IEEE80211_EHT_PHY_CAP0_320MHZ_IN_6GHZ)
8727 		memcpy(&mcs_nss->bw._320, &band_cap->eht_mcs_320,
8728 		       sizeof(struct ieee80211_eht_mcs_nss_supp_bw));
8729 }
8730 
8731 static void ath12k_mac_copy_eht_ppe_thresh(struct ath12k_wmi_ppe_threshold_arg *fw_ppet,
8732 					   struct ieee80211_sta_eht_cap *cap)
8733 {
8734 	u16 bit = IEEE80211_EHT_PPE_THRES_INFO_HEADER_SIZE;
8735 	u8 i, nss, ru, ppet_bit_len_per_ru = IEEE80211_EHT_PPE_THRES_INFO_PPET_SIZE * 2;
8736 
8737 	u8p_replace_bits(&cap->eht_ppe_thres[0], fw_ppet->numss_m1,
8738 			 IEEE80211_EHT_PPE_THRES_NSS_MASK);
8739 
8740 	u16p_replace_bits((u16 *)&cap->eht_ppe_thres[0], fw_ppet->ru_bit_mask,
8741 			  IEEE80211_EHT_PPE_THRES_RU_INDEX_BITMASK_MASK);
8742 
8743 	for (nss = 0; nss <= fw_ppet->numss_m1; nss++) {
8744 		for (ru = 0;
8745 		     ru < hweight16(IEEE80211_EHT_PPE_THRES_RU_INDEX_BITMASK_MASK);
8746 		     ru++) {
8747 			u32 val = 0;
8748 
8749 			if ((fw_ppet->ru_bit_mask & BIT(ru)) == 0)
8750 				continue;
8751 
8752 			u32p_replace_bits(&val, fw_ppet->ppet16_ppet8_ru3_ru0[nss] >>
8753 						(ru * ppet_bit_len_per_ru),
8754 					  GENMASK(ppet_bit_len_per_ru - 1, 0));
8755 
8756 			for (i = 0; i < ppet_bit_len_per_ru; i++) {
8757 				cap->eht_ppe_thres[bit / 8] |=
8758 					(((val >> i) & 0x1) << ((bit % 8)));
8759 				bit++;
8760 			}
8761 		}
8762 	}
8763 }
8764 
8765 static void
8766 ath12k_mac_filter_eht_cap_mesh(struct ieee80211_eht_cap_elem_fixed
8767 			       *eht_cap_elem)
8768 {
8769 	u8 m;
8770 
8771 	m = IEEE80211_EHT_MAC_CAP0_EPCS_PRIO_ACCESS;
8772 	eht_cap_elem->mac_cap_info[0] &= ~m;
8773 
8774 	m = IEEE80211_EHT_PHY_CAP0_PARTIAL_BW_UL_MU_MIMO;
8775 	eht_cap_elem->phy_cap_info[0] &= ~m;
8776 
8777 	m = IEEE80211_EHT_PHY_CAP3_NG_16_MU_FEEDBACK |
8778 	    IEEE80211_EHT_PHY_CAP3_CODEBOOK_7_5_MU_FDBK |
8779 	    IEEE80211_EHT_PHY_CAP3_TRIG_MU_BF_PART_BW_FDBK |
8780 	    IEEE80211_EHT_PHY_CAP3_TRIG_CQI_FDBK;
8781 	eht_cap_elem->phy_cap_info[3] &= ~m;
8782 
8783 	m = IEEE80211_EHT_PHY_CAP4_PART_BW_DL_MU_MIMO |
8784 	    IEEE80211_EHT_PHY_CAP4_PSR_SR_SUPP |
8785 	    IEEE80211_EHT_PHY_CAP4_POWER_BOOST_FACT_SUPP |
8786 	    IEEE80211_EHT_PHY_CAP4_EHT_MU_PPDU_4_EHT_LTF_08_GI;
8787 	eht_cap_elem->phy_cap_info[4] &= ~m;
8788 
8789 	m = IEEE80211_EHT_PHY_CAP5_NON_TRIG_CQI_FEEDBACK |
8790 	    IEEE80211_EHT_PHY_CAP5_TX_LESS_242_TONE_RU_SUPP |
8791 	    IEEE80211_EHT_PHY_CAP5_RX_LESS_242_TONE_RU_SUPP |
8792 	    IEEE80211_EHT_PHY_CAP5_MAX_NUM_SUPP_EHT_LTF_MASK;
8793 	eht_cap_elem->phy_cap_info[5] &= ~m;
8794 
8795 	m = IEEE80211_EHT_PHY_CAP6_MAX_NUM_SUPP_EHT_LTF_MASK;
8796 	eht_cap_elem->phy_cap_info[6] &= ~m;
8797 
8798 	m = IEEE80211_EHT_PHY_CAP7_NON_OFDMA_UL_MU_MIMO_80MHZ |
8799 	    IEEE80211_EHT_PHY_CAP7_NON_OFDMA_UL_MU_MIMO_160MHZ |
8800 	    IEEE80211_EHT_PHY_CAP7_NON_OFDMA_UL_MU_MIMO_320MHZ |
8801 	    IEEE80211_EHT_PHY_CAP7_MU_BEAMFORMER_80MHZ |
8802 	    IEEE80211_EHT_PHY_CAP7_MU_BEAMFORMER_160MHZ |
8803 	    IEEE80211_EHT_PHY_CAP7_MU_BEAMFORMER_320MHZ;
8804 	eht_cap_elem->phy_cap_info[7] &= ~m;
8805 }
8806 
8807 static void ath12k_mac_copy_eht_cap(struct ath12k *ar,
8808 				    struct ath12k_band_cap *band_cap,
8809 				    struct ieee80211_he_cap_elem *he_cap_elem,
8810 				    int iftype,
8811 				    struct ieee80211_sta_eht_cap *eht_cap)
8812 {
8813 	struct ieee80211_eht_cap_elem_fixed *eht_cap_elem = &eht_cap->eht_cap_elem;
8814 
8815 	memset(eht_cap, 0, sizeof(struct ieee80211_sta_eht_cap));
8816 
8817 	if (!(test_bit(WMI_TLV_SERVICE_11BE, ar->ab->wmi_ab.svc_map)) ||
8818 	    ath12k_acpi_get_disable_11be(ar->ab))
8819 		return;
8820 
8821 	eht_cap->has_eht = true;
8822 	memcpy(eht_cap_elem->mac_cap_info, band_cap->eht_cap_mac_info,
8823 	       sizeof(eht_cap_elem->mac_cap_info));
8824 	memcpy(eht_cap_elem->phy_cap_info, band_cap->eht_cap_phy_info,
8825 	       sizeof(eht_cap_elem->phy_cap_info));
8826 
8827 	switch (iftype) {
8828 	case NL80211_IFTYPE_AP:
8829 		eht_cap_elem->phy_cap_info[0] &=
8830 			~IEEE80211_EHT_PHY_CAP0_242_TONE_RU_GT20MHZ;
8831 		eht_cap_elem->phy_cap_info[4] &=
8832 			~IEEE80211_EHT_PHY_CAP4_PART_BW_DL_MU_MIMO;
8833 		eht_cap_elem->phy_cap_info[5] &=
8834 			~IEEE80211_EHT_PHY_CAP5_TX_LESS_242_TONE_RU_SUPP;
8835 		break;
8836 	case NL80211_IFTYPE_STATION:
8837 		eht_cap_elem->phy_cap_info[7] &=
8838 			~(IEEE80211_EHT_PHY_CAP7_NON_OFDMA_UL_MU_MIMO_80MHZ |
8839 			  IEEE80211_EHT_PHY_CAP7_NON_OFDMA_UL_MU_MIMO_160MHZ |
8840 			  IEEE80211_EHT_PHY_CAP7_NON_OFDMA_UL_MU_MIMO_320MHZ);
8841 		eht_cap_elem->phy_cap_info[7] &=
8842 			~(IEEE80211_EHT_PHY_CAP7_MU_BEAMFORMER_80MHZ |
8843 			  IEEE80211_EHT_PHY_CAP7_MU_BEAMFORMER_160MHZ |
8844 			  IEEE80211_EHT_PHY_CAP7_MU_BEAMFORMER_320MHZ);
8845 		break;
8846 	case NL80211_IFTYPE_MESH_POINT:
8847 		ath12k_mac_filter_eht_cap_mesh(eht_cap_elem);
8848 		break;
8849 	default:
8850 		break;
8851 	}
8852 
8853 	ath12k_mac_copy_eht_mcs_nss(band_cap, &eht_cap->eht_mcs_nss_supp,
8854 				    he_cap_elem, eht_cap_elem);
8855 
8856 	if (eht_cap_elem->phy_cap_info[5] &
8857 	    IEEE80211_EHT_PHY_CAP5_PPE_THRESHOLD_PRESENT)
8858 		ath12k_mac_copy_eht_ppe_thresh(&band_cap->eht_ppet, eht_cap);
8859 }
8860 
8861 static int ath12k_mac_copy_sband_iftype_data(struct ath12k *ar,
8862 					     struct ath12k_pdev_cap *cap,
8863 					     struct ieee80211_sband_iftype_data *data,
8864 					     int band)
8865 {
8866 	struct ath12k_band_cap *band_cap = &cap->band[band];
8867 	int i, idx = 0;
8868 
8869 	for (i = 0; i < NUM_NL80211_IFTYPES; i++) {
8870 		struct ieee80211_sta_he_cap *he_cap = &data[idx].he_cap;
8871 
8872 		switch (i) {
8873 		case NL80211_IFTYPE_STATION:
8874 		case NL80211_IFTYPE_AP:
8875 		case NL80211_IFTYPE_MESH_POINT:
8876 			break;
8877 
8878 		default:
8879 			continue;
8880 		}
8881 
8882 		data[idx].types_mask = BIT(i);
8883 
8884 		ath12k_mac_copy_he_cap(ar, band_cap, i, ar->num_tx_chains, he_cap);
8885 		if (band == NL80211_BAND_6GHZ) {
8886 			data[idx].he_6ghz_capa.capa =
8887 				ath12k_mac_setup_he_6ghz_cap(cap, band_cap);
8888 		}
8889 		ath12k_mac_copy_eht_cap(ar, band_cap, &he_cap->he_cap_elem, i,
8890 					&data[idx].eht_cap);
8891 		idx++;
8892 	}
8893 
8894 	return idx;
8895 }
8896 
8897 static void ath12k_mac_setup_sband_iftype_data(struct ath12k *ar,
8898 					       struct ath12k_pdev_cap *cap)
8899 {
8900 	struct ieee80211_supported_band *sband;
8901 	enum nl80211_band band;
8902 	int count;
8903 
8904 	if (cap->supported_bands & WMI_HOST_WLAN_2GHZ_CAP) {
8905 		band = NL80211_BAND_2GHZ;
8906 		count = ath12k_mac_copy_sband_iftype_data(ar, cap,
8907 							  ar->mac.iftype[band],
8908 							  band);
8909 		sband = &ar->mac.sbands[band];
8910 		_ieee80211_set_sband_iftype_data(sband, ar->mac.iftype[band],
8911 						 count);
8912 	}
8913 
8914 	if (cap->supported_bands & WMI_HOST_WLAN_5GHZ_CAP) {
8915 		band = NL80211_BAND_5GHZ;
8916 		count = ath12k_mac_copy_sband_iftype_data(ar, cap,
8917 							  ar->mac.iftype[band],
8918 							  band);
8919 		sband = &ar->mac.sbands[band];
8920 		_ieee80211_set_sband_iftype_data(sband, ar->mac.iftype[band],
8921 						 count);
8922 	}
8923 
8924 	if (cap->supported_bands & WMI_HOST_WLAN_5GHZ_CAP &&
8925 	    ar->supports_6ghz) {
8926 		band = NL80211_BAND_6GHZ;
8927 		count = ath12k_mac_copy_sband_iftype_data(ar, cap,
8928 							  ar->mac.iftype[band],
8929 							  band);
8930 		sband = &ar->mac.sbands[band];
8931 		_ieee80211_set_sband_iftype_data(sband, ar->mac.iftype[band],
8932 						 count);
8933 	}
8934 }
8935 
8936 static int __ath12k_set_antenna(struct ath12k *ar, u32 tx_ant, u32 rx_ant)
8937 {
8938 	struct ath12k_hw *ah = ath12k_ar_to_ah(ar);
8939 	int ret;
8940 
8941 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
8942 
8943 	if (ath12k_check_chain_mask(ar, tx_ant, true))
8944 		return -EINVAL;
8945 
8946 	if (ath12k_check_chain_mask(ar, rx_ant, false))
8947 		return -EINVAL;
8948 
8949 	/* Since we advertised the max cap of all radios combined during wiphy
8950 	 * registration, ensure we don't set the antenna config higher than the
8951 	 * limits
8952 	 */
8953 	tx_ant = min_t(u32, tx_ant, ar->pdev->cap.tx_chain_mask);
8954 	rx_ant = min_t(u32, rx_ant, ar->pdev->cap.rx_chain_mask);
8955 
8956 	ar->cfg_tx_chainmask = tx_ant;
8957 	ar->cfg_rx_chainmask = rx_ant;
8958 
8959 	if (ah->state != ATH12K_HW_STATE_ON &&
8960 	    ah->state != ATH12K_HW_STATE_RESTARTED)
8961 		return 0;
8962 
8963 	ret = ath12k_wmi_pdev_set_param(ar, WMI_PDEV_PARAM_TX_CHAIN_MASK,
8964 					tx_ant, ar->pdev->pdev_id);
8965 	if (ret) {
8966 		ath12k_warn(ar->ab, "failed to set tx-chainmask: %d, req 0x%x\n",
8967 			    ret, tx_ant);
8968 		return ret;
8969 	}
8970 
8971 	ar->num_tx_chains = hweight32(tx_ant);
8972 
8973 	ret = ath12k_wmi_pdev_set_param(ar, WMI_PDEV_PARAM_RX_CHAIN_MASK,
8974 					rx_ant, ar->pdev->pdev_id);
8975 	if (ret) {
8976 		ath12k_warn(ar->ab, "failed to set rx-chainmask: %d, req 0x%x\n",
8977 			    ret, rx_ant);
8978 		return ret;
8979 	}
8980 
8981 	ar->num_rx_chains = hweight32(rx_ant);
8982 
8983 	/* Reload HT/VHT/HE capability */
8984 	ath12k_mac_setup_ht_vht_cap(ar, &ar->pdev->cap, NULL);
8985 	ath12k_mac_setup_sband_iftype_data(ar, &ar->pdev->cap);
8986 
8987 	return 0;
8988 }
8989 
8990 static void ath12k_mgmt_over_wmi_tx_drop(struct ath12k *ar, struct sk_buff *skb)
8991 {
8992 	int num_mgmt;
8993 
8994 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
8995 
8996 	ieee80211_free_txskb(ath12k_ar_to_hw(ar), skb);
8997 
8998 	num_mgmt = atomic_dec_if_positive(&ar->num_pending_mgmt_tx);
8999 
9000 	if (num_mgmt < 0)
9001 		WARN_ON_ONCE(1);
9002 
9003 	if (!num_mgmt)
9004 		wake_up(&ar->txmgmt_empty_waitq);
9005 }
9006 
9007 static void ath12k_mac_tx_mgmt_free(struct ath12k *ar, int buf_id)
9008 {
9009 	struct sk_buff *msdu;
9010 	struct ieee80211_tx_info *info;
9011 
9012 	spin_lock_bh(&ar->txmgmt_idr_lock);
9013 	msdu = idr_remove(&ar->txmgmt_idr, buf_id);
9014 	spin_unlock_bh(&ar->txmgmt_idr_lock);
9015 
9016 	if (!msdu)
9017 		return;
9018 
9019 	dma_unmap_single(ar->ab->dev, ATH12K_SKB_CB(msdu)->paddr, msdu->len,
9020 			 DMA_TO_DEVICE);
9021 
9022 	info = IEEE80211_SKB_CB(msdu);
9023 	memset(&info->status, 0, sizeof(info->status));
9024 
9025 	ath12k_mgmt_over_wmi_tx_drop(ar, msdu);
9026 }
9027 
9028 int ath12k_mac_tx_mgmt_pending_free(int buf_id, void *skb, void *ctx)
9029 {
9030 	struct ath12k *ar = ctx;
9031 
9032 	ath12k_mac_tx_mgmt_free(ar, buf_id);
9033 
9034 	return 0;
9035 }
9036 
9037 static int ath12k_mac_vif_txmgmt_idr_remove(int buf_id, void *skb, void *ctx)
9038 {
9039 	struct ieee80211_vif *vif = ctx;
9040 	struct ath12k_skb_cb *skb_cb = ATH12K_SKB_CB(skb);
9041 	struct ath12k *ar = skb_cb->ar;
9042 
9043 	if (skb_cb->vif == vif)
9044 		ath12k_mac_tx_mgmt_free(ar, buf_id);
9045 
9046 	return 0;
9047 }
9048 
9049 static int ath12k_mac_mgmt_tx_wmi(struct ath12k *ar, struct ath12k_link_vif *arvif,
9050 				  struct sk_buff *skb)
9051 {
9052 	struct ath12k_base *ab = ar->ab;
9053 	struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
9054 	struct ath12k_skb_cb *skb_cb = ATH12K_SKB_CB(skb);
9055 	struct ieee80211_tx_info *info;
9056 	enum hal_encrypt_type enctype;
9057 	unsigned int mic_len;
9058 	dma_addr_t paddr;
9059 	int buf_id;
9060 	int ret;
9061 
9062 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
9063 
9064 	skb_cb->ar = ar;
9065 	spin_lock_bh(&ar->txmgmt_idr_lock);
9066 	buf_id = idr_alloc(&ar->txmgmt_idr, skb, 0,
9067 			   ATH12K_TX_MGMT_NUM_PENDING_MAX, GFP_ATOMIC);
9068 	spin_unlock_bh(&ar->txmgmt_idr_lock);
9069 	if (buf_id < 0)
9070 		return -ENOSPC;
9071 
9072 	info = IEEE80211_SKB_CB(skb);
9073 	if ((skb_cb->flags & ATH12K_SKB_CIPHER_SET) &&
9074 	    !(info->flags & IEEE80211_TX_CTL_HW_80211_ENCAP)) {
9075 		if ((ieee80211_is_action(hdr->frame_control) ||
9076 		     ieee80211_is_deauth(hdr->frame_control) ||
9077 		     ieee80211_is_disassoc(hdr->frame_control)) &&
9078 		     ieee80211_has_protected(hdr->frame_control)) {
9079 			enctype = ath12k_dp_tx_get_encrypt_type(skb_cb->cipher);
9080 			mic_len = ath12k_dp_rx_crypto_mic_len(ab->dp, enctype);
9081 			skb_put(skb, mic_len);
9082 		}
9083 	}
9084 
9085 	paddr = dma_map_single(ab->dev, skb->data, skb->len, DMA_TO_DEVICE);
9086 	if (dma_mapping_error(ab->dev, paddr)) {
9087 		ath12k_warn(ab, "failed to DMA map mgmt Tx buffer\n");
9088 		ret = -EIO;
9089 		goto err_free_idr;
9090 	}
9091 
9092 	skb_cb->paddr = paddr;
9093 
9094 	ret = ath12k_wmi_mgmt_send(arvif, buf_id, skb);
9095 	if (ret) {
9096 		ath12k_warn(ar->ab, "failed to send mgmt frame: %d\n", ret);
9097 		goto err_unmap_buf;
9098 	}
9099 
9100 	return 0;
9101 
9102 err_unmap_buf:
9103 	dma_unmap_single(ab->dev, skb_cb->paddr,
9104 			 skb->len, DMA_TO_DEVICE);
9105 err_free_idr:
9106 	spin_lock_bh(&ar->txmgmt_idr_lock);
9107 	idr_remove(&ar->txmgmt_idr, buf_id);
9108 	spin_unlock_bh(&ar->txmgmt_idr_lock);
9109 
9110 	return ret;
9111 }
9112 
9113 static void ath12k_mgmt_over_wmi_tx_purge(struct ath12k *ar)
9114 {
9115 	struct sk_buff *skb;
9116 
9117 	while ((skb = skb_dequeue(&ar->wmi_mgmt_tx_queue)) != NULL)
9118 		ath12k_mgmt_over_wmi_tx_drop(ar, skb);
9119 }
9120 
9121 static int ath12k_mac_mgmt_action_frame_fill_elem_data(struct ath12k_link_vif *arvif,
9122 						       struct sk_buff *skb)
9123 {
9124 	struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
9125 	u8 category, *buf, iv_len, action_code, dialog_token;
9126 	struct ieee80211_bss_conf *link_conf;
9127 	struct ieee80211_chanctx_conf *conf;
9128 	int cur_tx_power, max_tx_power;
9129 	struct ath12k *ar = arvif->ar;
9130 	struct ieee80211_hw *hw = ath12k_ar_to_hw(ar);
9131 	struct wiphy *wiphy = hw->wiphy;
9132 	struct ath12k_skb_cb *skb_cb;
9133 	struct ieee80211_mgmt *mgmt;
9134 	unsigned int remaining_len;
9135 	bool has_protected;
9136 
9137 	lockdep_assert_wiphy(wiphy);
9138 
9139 	/* make sure category field is present */
9140 	if (skb->len < IEEE80211_MIN_ACTION_SIZE(category))
9141 		return -EINVAL;
9142 
9143 	remaining_len = skb->len - IEEE80211_MIN_ACTION_SIZE(category);
9144 	has_protected = ieee80211_has_protected(hdr->frame_control);
9145 
9146 	/* In case of SW crypto and hdr protected (PMF), packet will already be encrypted,
9147 	 * we can't put in data in this case
9148 	 */
9149 	if (test_bit(ATH12K_FLAG_HW_CRYPTO_DISABLED, &ar->ab->dev_flags) &&
9150 	    has_protected)
9151 		return 0;
9152 
9153 	mgmt = (struct ieee80211_mgmt *)hdr;
9154 	buf = (u8 *)&mgmt->u.action;
9155 
9156 	/* FCTL_PROTECTED frame might have extra space added for HDR_LEN. Offset that
9157 	 * many bytes if it is there
9158 	 */
9159 	if (has_protected) {
9160 		skb_cb = ATH12K_SKB_CB(skb);
9161 
9162 		switch (skb_cb->cipher) {
9163 		/* Cipher suite having flag %IEEE80211_KEY_FLAG_GENERATE_IV_MGMT set in
9164 		 * key needs to be processed. See ath12k_install_key()
9165 		 */
9166 		case WLAN_CIPHER_SUITE_CCMP:
9167 		case WLAN_CIPHER_SUITE_CCMP_256:
9168 		case WLAN_CIPHER_SUITE_GCMP:
9169 		case WLAN_CIPHER_SUITE_GCMP_256:
9170 			iv_len = IEEE80211_CCMP_HDR_LEN;
9171 			break;
9172 		case WLAN_CIPHER_SUITE_TKIP:
9173 			iv_len = 0;
9174 			break;
9175 		default:
9176 			return -EINVAL;
9177 		}
9178 
9179 		if (remaining_len < iv_len)
9180 			return -EINVAL;
9181 
9182 		buf += iv_len;
9183 		remaining_len -= iv_len;
9184 	}
9185 
9186 	category = *buf++;
9187 	/* category code is already taken care in %IEEE80211_MIN_ACTION_SIZE hence
9188 	 * no need to adjust remaining_len
9189 	 */
9190 
9191 	switch (category) {
9192 	case WLAN_CATEGORY_RADIO_MEASUREMENT:
9193 		/* need action code and dialog token */
9194 		if (remaining_len < 2)
9195 			return -EINVAL;
9196 
9197 		/* Packet Format:
9198 		 *	Action Code | Dialog Token | Variable Len (based on Action Code)
9199 		 */
9200 		action_code = *buf++;
9201 		dialog_token = *buf++;
9202 		remaining_len -= 2;
9203 
9204 		link_conf = ath12k_mac_get_link_bss_conf(arvif);
9205 		if (!link_conf) {
9206 			ath12k_warn(ar->ab,
9207 				    "failed to get bss link conf for vdev %d in RM handling\n",
9208 				    arvif->vdev_id);
9209 			return -EINVAL;
9210 		}
9211 
9212 		conf = wiphy_dereference(wiphy, link_conf->chanctx_conf);
9213 		if (!conf)
9214 			return -ENOENT;
9215 
9216 		cur_tx_power = link_conf->txpower;
9217 		max_tx_power = min(conf->def.chan->max_reg_power,
9218 				   (int)ar->max_tx_power / 2);
9219 
9220 		ath12k_mac_op_get_txpower(hw, arvif->ahvif->vif, arvif->link_id,
9221 					  &cur_tx_power);
9222 
9223 		switch (action_code) {
9224 		case WLAN_RM_ACTION_LINK_MEASUREMENT_REQUEST:
9225 			/* need variable fields to be present in len */
9226 			if (remaining_len < 2)
9227 				return -EINVAL;
9228 
9229 			/* Variable length format as defined in IEEE 802.11-2024,
9230 			 * Figure 9-1187-Link Measurement Request frame Action field
9231 			 * format.
9232 			 *	Transmit Power | Max Tx Power
9233 			 * We fill both of these.
9234 			 */
9235 			*buf++ = cur_tx_power;
9236 			*buf = max_tx_power;
9237 
9238 			ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
9239 				   "RRM: Link Measurement Req dialog_token %u cur_tx_power %d max_tx_power %d\n",
9240 				   dialog_token, cur_tx_power, max_tx_power);
9241 			break;
9242 		case WLAN_RM_ACTION_LINK_MEASUREMENT_REPORT:
9243 			/* need variable fields to be present in len */
9244 			if (remaining_len < 3)
9245 				return -EINVAL;
9246 
9247 			/* Variable length format as defined in IEEE 802.11-2024,
9248 			 * Figure 9-1188-Link Measurement Report frame Action field format
9249 			 *	TPC Report | Variable Fields
9250 			 *
9251 			 * TPC Report Format:
9252 			 *	Element ID | Len | Tx Power | Link Margin
9253 			 *
9254 			 * We fill Tx power in the TPC Report (2nd index)
9255 			 */
9256 			buf[2] = cur_tx_power;
9257 
9258 			/* TODO: At present, Link margin data is not present so can't
9259 			 * really fill it now. Once it is available, it can be added
9260 			 * here
9261 			 */
9262 			ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
9263 				   "RRM: Link Measurement Report dialog_token %u cur_tx_power %d\n",
9264 				   dialog_token, cur_tx_power);
9265 			break;
9266 		default:
9267 			return -EINVAL;
9268 		}
9269 		break;
9270 	default:
9271 		/* nothing to fill */
9272 		return 0;
9273 	}
9274 
9275 	return 0;
9276 }
9277 
9278 static int ath12k_mac_mgmt_frame_fill_elem_data(struct ath12k_link_vif *arvif,
9279 						struct sk_buff *skb)
9280 {
9281 	struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
9282 
9283 	if (!ieee80211_is_action(hdr->frame_control))
9284 		return 0;
9285 
9286 	return ath12k_mac_mgmt_action_frame_fill_elem_data(arvif, skb);
9287 }
9288 
9289 static void ath12k_mgmt_over_wmi_tx_work(struct wiphy *wiphy, struct wiphy_work *work)
9290 {
9291 	struct ath12k *ar = container_of(work, struct ath12k, wmi_mgmt_tx_work);
9292 	struct ath12k_hw *ah = ar->ah;
9293 	struct ath12k_skb_cb *skb_cb;
9294 	struct ath12k_vif *ahvif;
9295 	struct ath12k_link_vif *arvif;
9296 	struct sk_buff *skb;
9297 	int ret;
9298 
9299 	lockdep_assert_wiphy(wiphy);
9300 
9301 	while ((skb = skb_dequeue(&ar->wmi_mgmt_tx_queue)) != NULL) {
9302 		skb_cb = ATH12K_SKB_CB(skb);
9303 		if (!skb_cb->vif) {
9304 			ath12k_warn(ar->ab, "no vif found for mgmt frame\n");
9305 			ath12k_mgmt_over_wmi_tx_drop(ar, skb);
9306 			continue;
9307 		}
9308 
9309 		ahvif = ath12k_vif_to_ahvif(skb_cb->vif);
9310 		if (!(ahvif->links_map & BIT(skb_cb->link_id))) {
9311 			ath12k_warn(ar->ab,
9312 				    "invalid linkid %u in mgmt over wmi tx with linkmap 0x%x\n",
9313 				    skb_cb->link_id, ahvif->links_map);
9314 			ath12k_mgmt_over_wmi_tx_drop(ar, skb);
9315 			continue;
9316 		}
9317 
9318 		arvif = wiphy_dereference(ah->hw->wiphy, ahvif->link[skb_cb->link_id]);
9319 		if (ar->allocated_vdev_map & (1LL << arvif->vdev_id)) {
9320 			/* Fill in the data which is required to be filled by the driver
9321 			 * For example: Max Tx power in Link Measurement Request/Report
9322 			 */
9323 			ret = ath12k_mac_mgmt_frame_fill_elem_data(arvif, skb);
9324 			if (ret) {
9325 				/* If we couldn't fill the data due to any reason,
9326 				 * let's not discard transmitting the packet.
9327 				 * For example: Software crypto and PMF case
9328 				 */
9329 				ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
9330 					   "Failed to fill the required data for the mgmt packet err %d\n",
9331 					   ret);
9332 			}
9333 
9334 			ret = ath12k_mac_mgmt_tx_wmi(ar, arvif, skb);
9335 			if (ret) {
9336 				ath12k_warn(ar->ab, "failed to tx mgmt frame, vdev_id %d :%d\n",
9337 					    arvif->vdev_id, ret);
9338 				ath12k_mgmt_over_wmi_tx_drop(ar, skb);
9339 			}
9340 		} else {
9341 			ath12k_warn(ar->ab,
9342 				    "dropping mgmt frame for vdev %d link %u is_started %d\n",
9343 				    arvif->vdev_id,
9344 				    skb_cb->link_id,
9345 				    arvif->is_started);
9346 			ath12k_mgmt_over_wmi_tx_drop(ar, skb);
9347 		}
9348 	}
9349 }
9350 
9351 int ath12k_mac_mgmt_tx(struct ath12k *ar, struct sk_buff *skb,
9352 		       bool is_prb_rsp)
9353 {
9354 	struct sk_buff_head *q = &ar->wmi_mgmt_tx_queue;
9355 
9356 	if (test_bit(ATH12K_FLAG_CRASH_FLUSH, &ar->ab->dev_flags))
9357 		return -ESHUTDOWN;
9358 
9359 	/* Drop probe response packets when the pending management tx
9360 	 * count has reached a certain threshold, so as to prioritize
9361 	 * other mgmt packets like auth and assoc to be sent on time
9362 	 * for establishing successful connections.
9363 	 */
9364 	if (is_prb_rsp &&
9365 	    atomic_read(&ar->num_pending_mgmt_tx) > ATH12K_PRB_RSP_DROP_THRESHOLD) {
9366 		ath12k_warn(ar->ab,
9367 			    "dropping probe response as pending queue is almost full\n");
9368 		return -ENOSPC;
9369 	}
9370 
9371 	if (skb_queue_len_lockless(q) >= ATH12K_TX_MGMT_NUM_PENDING_MAX) {
9372 		ath12k_warn(ar->ab, "mgmt tx queue is full\n");
9373 		return -ENOSPC;
9374 	}
9375 
9376 	skb_queue_tail(q, skb);
9377 	atomic_inc(&ar->num_pending_mgmt_tx);
9378 	wiphy_work_queue(ath12k_ar_to_hw(ar)->wiphy, &ar->wmi_mgmt_tx_work);
9379 
9380 	return 0;
9381 }
9382 EXPORT_SYMBOL(ath12k_mac_mgmt_tx);
9383 
9384 void ath12k_mac_add_p2p_noa_ie(struct ath12k *ar,
9385 			       struct ieee80211_vif *vif,
9386 			       struct sk_buff *skb,
9387 			       bool is_prb_rsp)
9388 {
9389 	struct ath12k_vif *ahvif = ath12k_vif_to_ahvif(vif);
9390 
9391 	if (likely(!is_prb_rsp))
9392 		return;
9393 
9394 	spin_lock_bh(&ar->data_lock);
9395 
9396 	if (ahvif->u.ap.noa_data &&
9397 	    !pskb_expand_head(skb, 0, ahvif->u.ap.noa_len,
9398 			      GFP_ATOMIC))
9399 		skb_put_data(skb, ahvif->u.ap.noa_data,
9400 			     ahvif->u.ap.noa_len);
9401 
9402 	spin_unlock_bh(&ar->data_lock);
9403 }
9404 EXPORT_SYMBOL(ath12k_mac_add_p2p_noa_ie);
9405 
9406 /* Note: called under rcu_read_lock() */
9407 void ath12k_mlo_mcast_update_tx_link_address(struct ieee80211_vif *vif,
9408 					     u8 link_id, struct sk_buff *skb,
9409 					     u32 info_flags)
9410 {
9411 	struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
9412 	struct ieee80211_bss_conf *bss_conf;
9413 
9414 	if (info_flags & IEEE80211_TX_CTL_HW_80211_ENCAP)
9415 		return;
9416 
9417 	bss_conf = rcu_dereference(vif->link_conf[link_id]);
9418 	if (bss_conf)
9419 		ether_addr_copy(hdr->addr2, bss_conf->addr);
9420 }
9421 EXPORT_SYMBOL(ath12k_mlo_mcast_update_tx_link_address);
9422 
9423 /* Note: called under rcu_read_lock() */
9424 u8 ath12k_mac_get_tx_link(struct ieee80211_sta *sta, struct ieee80211_vif *vif,
9425 			  u8 link, struct sk_buff *skb, u32 info_flags)
9426 {
9427 	struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
9428 	struct ath12k_vif *ahvif = ath12k_vif_to_ahvif(vif);
9429 	struct ieee80211_link_sta *link_sta;
9430 	struct ieee80211_bss_conf *bss_conf;
9431 	struct ath12k_sta *ahsta;
9432 
9433 	/* Use the link id passed or the default vif link */
9434 	if (!sta) {
9435 		if (link != IEEE80211_LINK_UNSPECIFIED)
9436 			return link;
9437 
9438 		return ahvif->deflink.link_id;
9439 	}
9440 
9441 	ahsta = ath12k_sta_to_ahsta(sta);
9442 
9443 	/* Below translation ensures we pass proper A2 & A3 for non ML clients.
9444 	 * Also it assumes for now support only for MLO AP in this path
9445 	 */
9446 	if (!sta->mlo) {
9447 		link = ahsta->deflink.link_id;
9448 
9449 		if (info_flags & IEEE80211_TX_CTL_HW_80211_ENCAP)
9450 			return link;
9451 
9452 		bss_conf = rcu_dereference(vif->link_conf[link]);
9453 		if (bss_conf) {
9454 			ether_addr_copy(hdr->addr2, bss_conf->addr);
9455 			if (!ieee80211_has_tods(hdr->frame_control) &&
9456 			    !ieee80211_has_fromds(hdr->frame_control))
9457 				ether_addr_copy(hdr->addr3, bss_conf->addr);
9458 		}
9459 
9460 		return link;
9461 	}
9462 
9463 	/* enqueue eth enacap & data frames on primary link, FW does link
9464 	 * selection and address translation.
9465 	 */
9466 	if (info_flags & IEEE80211_TX_CTL_HW_80211_ENCAP ||
9467 	    ieee80211_is_data(hdr->frame_control))
9468 		return ahsta->assoc_link_id;
9469 
9470 	/* 802.11 frame cases */
9471 	if (link == IEEE80211_LINK_UNSPECIFIED)
9472 		link = ahsta->deflink.link_id;
9473 
9474 	if (!ieee80211_is_mgmt(hdr->frame_control))
9475 		return link;
9476 
9477 	/* Perform address conversion for ML STA Tx */
9478 	bss_conf = rcu_dereference(vif->link_conf[link]);
9479 	link_sta = rcu_dereference(sta->link[link]);
9480 
9481 	if (bss_conf && link_sta) {
9482 		ether_addr_copy(hdr->addr1, link_sta->addr);
9483 		ether_addr_copy(hdr->addr2, bss_conf->addr);
9484 
9485 		if (vif->type == NL80211_IFTYPE_STATION && bss_conf->bssid)
9486 			ether_addr_copy(hdr->addr3, bss_conf->bssid);
9487 		else if (vif->type == NL80211_IFTYPE_AP)
9488 			ether_addr_copy(hdr->addr3, bss_conf->addr);
9489 
9490 		return link;
9491 	}
9492 
9493 	if (bss_conf) {
9494 		/* In certain cases where a ML sta associated and added subset of
9495 		 * links on which the ML AP is active, but now sends some frame
9496 		 * (ex. Probe request) on a different link which is active in our
9497 		 * MLD but was not added during previous association, we can
9498 		 * still honor the Tx to that ML STA via the requested link.
9499 		 * The control would reach here in such case only when that link
9500 		 * address is same as the MLD address or in worst case clients
9501 		 * used MLD address at TA wrongly which would have helped
9502 		 * identify the ML sta object and pass it here.
9503 		 * If the link address of that STA is different from MLD address,
9504 		 * then the sta object would be NULL and control won't reach
9505 		 * here but return at the start of the function itself with !sta
9506 		 * check. Also this would not need any translation at hdr->addr1
9507 		 * from MLD to link address since the RA is the MLD address
9508 		 * (same as that link address ideally) already.
9509 		 */
9510 		ether_addr_copy(hdr->addr2, bss_conf->addr);
9511 
9512 		if (vif->type == NL80211_IFTYPE_STATION && bss_conf->bssid)
9513 			ether_addr_copy(hdr->addr3, bss_conf->bssid);
9514 		else if (vif->type == NL80211_IFTYPE_AP)
9515 			ether_addr_copy(hdr->addr3, bss_conf->addr);
9516 	}
9517 
9518 	return link;
9519 }
9520 EXPORT_SYMBOL(ath12k_mac_get_tx_link);
9521 
9522 void ath12k_mac_drain_tx(struct ath12k *ar)
9523 {
9524 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
9525 
9526 	/* make sure rcu-protected mac80211 tx path itself is drained */
9527 	synchronize_net();
9528 
9529 	wiphy_work_cancel(ath12k_ar_to_hw(ar)->wiphy, &ar->wmi_mgmt_tx_work);
9530 	ath12k_mgmt_over_wmi_tx_purge(ar);
9531 }
9532 
9533 static int ath12k_mac_config_mon_status_default(struct ath12k *ar, bool enable)
9534 {
9535 	struct htt_rx_ring_tlv_filter tlv_filter = {};
9536 	struct ath12k_base *ab = ar->ab;
9537 	u32 ring_id, i;
9538 	int ret = 0;
9539 
9540 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
9541 
9542 	if (!ab->hw_params->rxdma1_enable)
9543 		return ret;
9544 
9545 	if (enable) {
9546 		tlv_filter = ath12k_mac_mon_status_filter_default;
9547 
9548 		if (ath12k_debugfs_rx_filter(ar))
9549 			tlv_filter.rx_filter = ath12k_debugfs_rx_filter(ar);
9550 	} else {
9551 		tlv_filter.rxmon_disable = true;
9552 	}
9553 
9554 	for (i = 0; i < ab->hw_params->num_rxdma_per_pdev; i++) {
9555 		ring_id = ar->dp.rxdma_mon_dst_ring[i].ring_id;
9556 		ret = ath12k_dp_tx_htt_rx_filter_setup(ab, ring_id,
9557 						       ar->dp.mac_id + i,
9558 						       HAL_RXDMA_MONITOR_DST,
9559 						       DP_RXDMA_REFILL_RING_SIZE,
9560 						       &tlv_filter);
9561 		if (ret) {
9562 			ath12k_err(ab,
9563 				   "failed to setup filter for monitor buf %d\n",
9564 				   ret);
9565 		}
9566 	}
9567 
9568 	return ret;
9569 }
9570 
9571 static int ath12k_mac_start(struct ath12k *ar)
9572 {
9573 	struct ath12k_hw *ah = ar->ah;
9574 	struct ath12k_base *ab = ar->ab;
9575 	struct ath12k_pdev *pdev = ar->pdev;
9576 	int ret;
9577 
9578 	lockdep_assert_held(&ah->hw_mutex);
9579 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
9580 
9581 	ret = ath12k_wmi_pdev_set_param(ar, WMI_PDEV_PARAM_PMF_QOS,
9582 					1, pdev->pdev_id);
9583 
9584 	if (ret) {
9585 		ath12k_err(ab, "failed to enable PMF QOS: %d\n", ret);
9586 		goto err;
9587 	}
9588 
9589 	ret = ath12k_wmi_pdev_set_param(ar, WMI_PDEV_PARAM_DYNAMIC_BW, 1,
9590 					pdev->pdev_id);
9591 	if (ret) {
9592 		ath12k_err(ab, "failed to enable dynamic bw: %d\n", ret);
9593 		goto err;
9594 	}
9595 
9596 	ret = ath12k_wmi_pdev_set_param(ar, WMI_PDEV_PARAM_ARP_AC_OVERRIDE,
9597 					0, pdev->pdev_id);
9598 	if (ret) {
9599 		ath12k_err(ab, "failed to set ac override for ARP: %d\n",
9600 			   ret);
9601 		goto err;
9602 	}
9603 
9604 	ret = ath12k_wmi_send_dfs_phyerr_offload_enable_cmd(ar, pdev->pdev_id);
9605 	if (ret) {
9606 		ath12k_err(ab, "failed to offload radar detection: %d\n",
9607 			   ret);
9608 		goto err;
9609 	}
9610 
9611 	ret = ath12k_dp_tx_htt_h2t_ppdu_stats_req(ar,
9612 						  HTT_PPDU_STATS_TAG_DEFAULT);
9613 	if (ret) {
9614 		ath12k_err(ab, "failed to req ppdu stats: %d\n", ret);
9615 		goto err;
9616 	}
9617 
9618 	ret = ath12k_wmi_pdev_set_param(ar, WMI_PDEV_PARAM_MESH_MCAST_ENABLE,
9619 					1, pdev->pdev_id);
9620 
9621 	if (ret) {
9622 		ath12k_err(ab, "failed to enable MESH MCAST ENABLE: (%d\n", ret);
9623 		goto err;
9624 	}
9625 
9626 	__ath12k_set_antenna(ar, ar->cfg_tx_chainmask, ar->cfg_rx_chainmask);
9627 
9628 	/* TODO: Do we need to enable ANI? */
9629 
9630 	ret = ath12k_reg_update_chan_list(ar, false);
9631 
9632 	/* The ar state alone can be turned off for non supported country
9633 	 * without returning the error value. As we need to update the channel
9634 	 * for the next ar.
9635 	 */
9636 	if (ret) {
9637 		if (ret == -EINVAL)
9638 			ret = 0;
9639 		goto err;
9640 	}
9641 
9642 	ar->num_started_vdevs = 0;
9643 	ar->num_created_vdevs = 0;
9644 	ar->num_peers = 0;
9645 	ar->allocated_vdev_map = 0;
9646 	ar->chan_tx_pwr = ATH12K_PDEV_TX_POWER_INVALID;
9647 
9648 	spin_lock_bh(&ar->data_lock);
9649 	ar->incumbent_signal_interference.handling_in_progress = false;
9650 	spin_unlock_bh(&ar->data_lock);
9651 
9652 	/* Configure monitor status ring with default rx_filter to get rx status
9653 	 * such as rssi, rx_duration.
9654 	 */
9655 	ret = ath12k_mac_config_mon_status_default(ar, true);
9656 	if (ret && (ret != -EOPNOTSUPP)) {
9657 		ath12k_err(ab, "failed to configure monitor status ring with default rx_filter: (%d)\n",
9658 			   ret);
9659 		goto err;
9660 	}
9661 
9662 	if (ret == -EOPNOTSUPP)
9663 		ath12k_dbg(ab, ATH12K_DBG_MAC,
9664 			   "monitor status config is not yet supported");
9665 
9666 	/* Configure the hash seed for hash based reo dest ring selection */
9667 	ath12k_wmi_pdev_lro_cfg(ar, ar->pdev->pdev_id);
9668 
9669 	/* allow device to enter IMPS */
9670 	if (ab->hw_params->idle_ps) {
9671 		ret = ath12k_wmi_pdev_set_param(ar, WMI_PDEV_PARAM_IDLE_PS_CONFIG,
9672 						1, pdev->pdev_id);
9673 		if (ret) {
9674 			ath12k_err(ab, "failed to enable idle ps: %d\n", ret);
9675 			goto err;
9676 		}
9677 	}
9678 
9679 	ret = ath12k_thermal_throttling_config_default(ar);
9680 	if (ret) {
9681 		ath12k_err(ab, "failed to set thermal throttle: %d\n", ret);
9682 		goto err;
9683 	}
9684 
9685 	rcu_assign_pointer(ab->pdevs_active[ar->pdev_idx],
9686 			   &ab->pdevs[ar->pdev_idx]);
9687 
9688 	return 0;
9689 err:
9690 
9691 	return ret;
9692 }
9693 
9694 static void ath12k_drain_tx(struct ath12k_hw *ah)
9695 {
9696 	struct ath12k *ar;
9697 	int i;
9698 
9699 	lockdep_assert_wiphy(ah->hw->wiphy);
9700 
9701 	for_each_ar(ah, ar, i)
9702 		ath12k_mac_drain_tx(ar);
9703 }
9704 
9705 int ath12k_mac_op_start(struct ieee80211_hw *hw)
9706 {
9707 	struct ath12k_hw *ah = ath12k_hw_to_ah(hw);
9708 	struct ath12k *ar;
9709 	int ret, i;
9710 
9711 	if (ath12k_ftm_mode)
9712 		return -EPERM;
9713 
9714 	lockdep_assert_wiphy(hw->wiphy);
9715 
9716 	ath12k_drain_tx(ah);
9717 
9718 	guard(mutex)(&ah->hw_mutex);
9719 
9720 	switch (ah->state) {
9721 	case ATH12K_HW_STATE_OFF:
9722 		ah->state = ATH12K_HW_STATE_ON;
9723 		break;
9724 	case ATH12K_HW_STATE_RESTARTING:
9725 		ah->state = ATH12K_HW_STATE_RESTARTED;
9726 		break;
9727 	case ATH12K_HW_STATE_RESTARTED:
9728 	case ATH12K_HW_STATE_WEDGED:
9729 	case ATH12K_HW_STATE_ON:
9730 	case ATH12K_HW_STATE_TM:
9731 		ah->state = ATH12K_HW_STATE_OFF;
9732 
9733 		WARN_ON(1);
9734 		return -EINVAL;
9735 	}
9736 
9737 	for_each_ar(ah, ar, i) {
9738 		ret = ath12k_mac_start(ar);
9739 		if (ret) {
9740 			ah->state = ATH12K_HW_STATE_OFF;
9741 
9742 			ath12k_err(ar->ab, "fail to start mac operations in pdev idx %d ret %d\n",
9743 				   ar->pdev_idx, ret);
9744 			goto fail_start;
9745 		}
9746 	}
9747 
9748 	return 0;
9749 
9750 fail_start:
9751 	for (; i > 0; i--) {
9752 		ar = ath12k_ah_to_ar(ah, i - 1);
9753 		ath12k_mac_stop(ar);
9754 	}
9755 
9756 	return ret;
9757 }
9758 EXPORT_SYMBOL(ath12k_mac_op_start);
9759 
9760 int ath12k_mac_rfkill_config(struct ath12k *ar)
9761 {
9762 	struct ath12k_base *ab = ar->ab;
9763 	u32 param;
9764 	int ret;
9765 
9766 	if (ab->hw_params->rfkill_pin == 0)
9767 		return -EOPNOTSUPP;
9768 
9769 	ath12k_dbg(ab, ATH12K_DBG_MAC,
9770 		   "mac rfkill_pin %d rfkill_cfg %d rfkill_on_level %d",
9771 		   ab->hw_params->rfkill_pin, ab->hw_params->rfkill_cfg,
9772 		   ab->hw_params->rfkill_on_level);
9773 
9774 	param = u32_encode_bits(ab->hw_params->rfkill_on_level,
9775 				WMI_RFKILL_CFG_RADIO_LEVEL) |
9776 		u32_encode_bits(ab->hw_params->rfkill_pin,
9777 				WMI_RFKILL_CFG_GPIO_PIN_NUM) |
9778 		u32_encode_bits(ab->hw_params->rfkill_cfg,
9779 				WMI_RFKILL_CFG_PIN_AS_GPIO);
9780 
9781 	ret = ath12k_wmi_pdev_set_param(ar, WMI_PDEV_PARAM_HW_RFKILL_CONFIG,
9782 					param, ar->pdev->pdev_id);
9783 	if (ret) {
9784 		ath12k_warn(ab,
9785 			    "failed to set rfkill config 0x%x: %d\n",
9786 			    param, ret);
9787 		return ret;
9788 	}
9789 
9790 	return 0;
9791 }
9792 
9793 int ath12k_mac_rfkill_enable_radio(struct ath12k *ar, bool enable)
9794 {
9795 	enum wmi_rfkill_enable_radio param;
9796 	int ret;
9797 
9798 	if (enable)
9799 		param = WMI_RFKILL_ENABLE_RADIO_ON;
9800 	else
9801 		param = WMI_RFKILL_ENABLE_RADIO_OFF;
9802 
9803 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac %d rfkill enable %d",
9804 		   ar->pdev_idx, param);
9805 
9806 	ret = ath12k_wmi_pdev_set_param(ar, WMI_PDEV_PARAM_RFKILL_ENABLE,
9807 					param, ar->pdev->pdev_id);
9808 	if (ret) {
9809 		ath12k_warn(ar->ab, "failed to set rfkill enable param %d: %d\n",
9810 			    param, ret);
9811 		return ret;
9812 	}
9813 
9814 	return 0;
9815 }
9816 
9817 static void ath12k_mac_stop(struct ath12k *ar)
9818 {
9819 	struct ath12k_pdev_dp *dp_pdev = &ar->dp;
9820 	struct ath12k_hw *ah = ar->ah;
9821 	struct htt_ppdu_stats_info *ppdu_stats, *tmp;
9822 	struct ath12k_wmi_scan_chan_list_arg *arg;
9823 	int ret;
9824 
9825 	lockdep_assert_held(&ah->hw_mutex);
9826 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
9827 
9828 	ret = ath12k_mac_config_mon_status_default(ar, false);
9829 	if (ret && (ret != -EOPNOTSUPP))
9830 		ath12k_err(ar->ab, "failed to clear rx_filter for monitor status ring: (%d)\n",
9831 			   ret);
9832 
9833 	clear_bit(ATH12K_FLAG_CAC_RUNNING, &ar->dev_flags);
9834 
9835 	cancel_delayed_work_sync(&ar->scan.timeout);
9836 	wiphy_work_cancel(ath12k_ar_to_hw(ar)->wiphy, &ar->scan.vdev_clean_wk);
9837 	cancel_work_sync(&ar->regd_channel_update_work);
9838 	cancel_work_sync(&ar->regd_update_work);
9839 	cancel_work_sync(&ar->ab->rfkill_work);
9840 	cancel_work_sync(&ar->ab->update_11d_work);
9841 	ar->state_11d = ATH12K_11D_IDLE;
9842 	complete(&ar->completed_11d_scan);
9843 
9844 	spin_lock_bh(&dp_pdev->ppdu_list_lock);
9845 	list_for_each_entry_safe(ppdu_stats, tmp, &dp_pdev->ppdu_stats_info, list) {
9846 		list_del(&ppdu_stats->list);
9847 		kfree(ppdu_stats);
9848 	}
9849 	spin_unlock_bh(&dp_pdev->ppdu_list_lock);
9850 
9851 	spin_lock_bh(&ar->data_lock);
9852 	while ((arg = list_first_entry_or_null(&ar->regd_channel_update_queue,
9853 					       struct ath12k_wmi_scan_chan_list_arg,
9854 					       list))) {
9855 		list_del(&arg->list);
9856 		kfree(arg);
9857 	}
9858 	spin_unlock_bh(&ar->data_lock);
9859 
9860 	rcu_assign_pointer(ar->ab->pdevs_active[ar->pdev_idx], NULL);
9861 
9862 	synchronize_rcu();
9863 
9864 	atomic_set(&ar->num_pending_mgmt_tx, 0);
9865 
9866 	spin_lock_bh(&ar->data_lock);
9867 	ar->incumbent_signal_interference.handling_in_progress = false;
9868 	spin_unlock_bh(&ar->data_lock);
9869 }
9870 
9871 void ath12k_mac_op_stop(struct ieee80211_hw *hw, bool suspend)
9872 {
9873 	struct ath12k_hw *ah = ath12k_hw_to_ah(hw);
9874 	struct ath12k *ar;
9875 	int i;
9876 
9877 	lockdep_assert_wiphy(hw->wiphy);
9878 
9879 	ath12k_drain_tx(ah);
9880 
9881 	mutex_lock(&ah->hw_mutex);
9882 
9883 	ah->state = ATH12K_HW_STATE_OFF;
9884 
9885 	for_each_ar(ah, ar, i)
9886 		ath12k_mac_stop(ar);
9887 
9888 	mutex_unlock(&ah->hw_mutex);
9889 }
9890 EXPORT_SYMBOL(ath12k_mac_op_stop);
9891 
9892 static u8
9893 ath12k_mac_get_vdev_stats_id(struct ath12k_link_vif *arvif)
9894 {
9895 	struct ath12k_base *ab = arvif->ar->ab;
9896 	u8 vdev_stats_id = 0;
9897 
9898 	do {
9899 		if (ab->free_vdev_stats_id_map & (1LL << vdev_stats_id)) {
9900 			vdev_stats_id++;
9901 			if (vdev_stats_id >= ATH12K_MAX_VDEV_STATS_ID) {
9902 				vdev_stats_id = ATH12K_INVAL_VDEV_STATS_ID;
9903 				break;
9904 			}
9905 		} else {
9906 			ab->free_vdev_stats_id_map |= (1LL << vdev_stats_id);
9907 			break;
9908 		}
9909 	} while (vdev_stats_id);
9910 
9911 	arvif->vdev_stats_id = vdev_stats_id;
9912 	return vdev_stats_id;
9913 }
9914 
9915 static int ath12k_mac_setup_vdev_params_mbssid(struct ath12k_link_vif *arvif,
9916 					       u32 *flags, u32 *tx_vdev_id)
9917 {
9918 	struct ath12k_vif *ahvif = arvif->ahvif;
9919 	struct ieee80211_bss_conf *link_conf;
9920 	struct ath12k *ar = arvif->ar;
9921 	struct ath12k_link_vif *tx_arvif;
9922 
9923 	link_conf = ath12k_mac_get_link_bss_conf(arvif);
9924 	if (!link_conf) {
9925 		ath12k_warn(ar->ab, "unable to access bss link conf in set mbssid params for vif %pM link %u\n",
9926 			    ahvif->vif->addr, arvif->link_id);
9927 		return -ENOLINK;
9928 	}
9929 
9930 	tx_arvif = ath12k_mac_get_tx_arvif(arvif, link_conf);
9931 	if (!tx_arvif)
9932 		return 0;
9933 
9934 	if (link_conf->nontransmitted) {
9935 		if (ath12k_ar_to_hw(ar)->wiphy !=
9936 		    ath12k_ar_to_hw(tx_arvif->ar)->wiphy)
9937 			return -EINVAL;
9938 
9939 		*flags = WMI_VDEV_MBSSID_FLAGS_NON_TRANSMIT_AP;
9940 		*tx_vdev_id = tx_arvif->vdev_id;
9941 	} else if (tx_arvif == arvif) {
9942 		*flags = WMI_VDEV_MBSSID_FLAGS_TRANSMIT_AP;
9943 	} else {
9944 		return -EINVAL;
9945 	}
9946 
9947 	if (link_conf->ema_ap)
9948 		*flags |= WMI_VDEV_MBSSID_FLAGS_EMA_MODE;
9949 
9950 	return 0;
9951 }
9952 
9953 static int ath12k_mac_setup_vdev_create_arg(struct ath12k_link_vif *arvif,
9954 					    struct ath12k_wmi_vdev_create_arg *arg)
9955 {
9956 	struct ath12k *ar = arvif->ar;
9957 	struct ath12k_pdev *pdev = ar->pdev;
9958 	struct ath12k_vif *ahvif = arvif->ahvif;
9959 	int ret;
9960 
9961 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
9962 
9963 	arg->if_id = arvif->vdev_id;
9964 	arg->type = ahvif->vdev_type;
9965 	arg->subtype = ahvif->vdev_subtype;
9966 	arg->pdev_id = pdev->pdev_id;
9967 
9968 	arg->mbssid_flags = WMI_VDEV_MBSSID_FLAGS_NON_MBSSID_AP;
9969 	arg->mbssid_tx_vdev_id = 0;
9970 	if (!test_bit(WMI_TLV_SERVICE_MBSS_PARAM_IN_VDEV_START_SUPPORT,
9971 		      ar->ab->wmi_ab.svc_map)) {
9972 		ret = ath12k_mac_setup_vdev_params_mbssid(arvif,
9973 							  &arg->mbssid_flags,
9974 							  &arg->mbssid_tx_vdev_id);
9975 		if (ret)
9976 			return ret;
9977 	}
9978 
9979 	if (pdev->cap.supported_bands & WMI_HOST_WLAN_2GHZ_CAP) {
9980 		arg->chains[NL80211_BAND_2GHZ].tx = ar->num_tx_chains;
9981 		arg->chains[NL80211_BAND_2GHZ].rx = ar->num_rx_chains;
9982 	}
9983 	if (pdev->cap.supported_bands & WMI_HOST_WLAN_5GHZ_CAP) {
9984 		arg->chains[NL80211_BAND_5GHZ].tx = ar->num_tx_chains;
9985 		arg->chains[NL80211_BAND_5GHZ].rx = ar->num_rx_chains;
9986 	}
9987 	if (pdev->cap.supported_bands & WMI_HOST_WLAN_5GHZ_CAP &&
9988 	    ar->supports_6ghz) {
9989 		arg->chains[NL80211_BAND_6GHZ].tx = ar->num_tx_chains;
9990 		arg->chains[NL80211_BAND_6GHZ].rx = ar->num_rx_chains;
9991 	}
9992 
9993 	arg->if_stats_id = ath12k_mac_get_vdev_stats_id(arvif);
9994 
9995 	if (ath12k_mac_is_ml_arvif(arvif)) {
9996 		if (hweight16(ahvif->vif->valid_links) > ATH12K_WMI_MLO_MAX_LINKS) {
9997 			ath12k_warn(ar->ab, "too many MLO links during setting up vdev: %d",
9998 				    ahvif->vif->valid_links);
9999 			return -EINVAL;
10000 		}
10001 
10002 		ether_addr_copy(arg->mld_addr, ahvif->vif->addr);
10003 	}
10004 
10005 	return 0;
10006 }
10007 
10008 static void ath12k_mac_update_vif_offload(struct ath12k_link_vif *arvif)
10009 {
10010 	struct ath12k_vif *ahvif = arvif->ahvif;
10011 	struct ieee80211_vif *vif = ath12k_ahvif_to_vif(ahvif);
10012 	struct ath12k *ar = arvif->ar;
10013 	struct ath12k_base *ab = ar->ab;
10014 	u32 param_id, param_value;
10015 	int ret;
10016 
10017 	param_id = WMI_VDEV_PARAM_TX_ENCAP_TYPE;
10018 	if (vif->type != NL80211_IFTYPE_STATION &&
10019 	    vif->type != NL80211_IFTYPE_AP)
10020 		vif->offload_flags &= ~(IEEE80211_OFFLOAD_ENCAP_ENABLED |
10021 					IEEE80211_OFFLOAD_DECAP_ENABLED);
10022 
10023 	if (vif->offload_flags & IEEE80211_OFFLOAD_ENCAP_ENABLED)
10024 		ahvif->dp_vif.tx_encap_type = ATH12K_HW_TXRX_ETHERNET;
10025 	else if (test_bit(ATH12K_FLAG_RAW_MODE, &ab->dev_flags))
10026 		ahvif->dp_vif.tx_encap_type = ATH12K_HW_TXRX_RAW;
10027 	else
10028 		ahvif->dp_vif.tx_encap_type = ATH12K_HW_TXRX_NATIVE_WIFI;
10029 
10030 	ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
10031 					    param_id, ahvif->dp_vif.tx_encap_type);
10032 	if (ret) {
10033 		ath12k_warn(ab, "failed to set vdev %d tx encap mode: %d\n",
10034 			    arvif->vdev_id, ret);
10035 		vif->offload_flags &= ~IEEE80211_OFFLOAD_ENCAP_ENABLED;
10036 	}
10037 
10038 	param_id = WMI_VDEV_PARAM_RX_DECAP_TYPE;
10039 	if (vif->offload_flags & IEEE80211_OFFLOAD_DECAP_ENABLED)
10040 		param_value = ATH12K_HW_TXRX_ETHERNET;
10041 	else if (test_bit(ATH12K_FLAG_RAW_MODE, &ab->dev_flags))
10042 		param_value = ATH12K_HW_TXRX_RAW;
10043 	else
10044 		param_value = ATH12K_HW_TXRX_NATIVE_WIFI;
10045 
10046 	ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
10047 					    param_id, param_value);
10048 	if (ret) {
10049 		ath12k_warn(ab, "failed to set vdev %d rx decap mode: %d\n",
10050 			    arvif->vdev_id, ret);
10051 		vif->offload_flags &= ~IEEE80211_OFFLOAD_DECAP_ENABLED;
10052 	}
10053 }
10054 
10055 void ath12k_mac_op_update_vif_offload(struct ieee80211_hw *hw,
10056 				      struct ieee80211_vif *vif)
10057 {
10058 	struct ath12k_vif *ahvif = ath12k_vif_to_ahvif(vif);
10059 	struct ath12k_link_vif *arvif;
10060 	unsigned long links;
10061 	int link_id;
10062 
10063 	lockdep_assert_wiphy(hw->wiphy);
10064 
10065 	if (vif->valid_links) {
10066 		links = vif->valid_links;
10067 		for_each_set_bit(link_id, &links, IEEE80211_MLD_MAX_NUM_LINKS) {
10068 			arvif = wiphy_dereference(hw->wiphy, ahvif->link[link_id]);
10069 			if (!(arvif && arvif->ar))
10070 				continue;
10071 
10072 			ath12k_mac_update_vif_offload(arvif);
10073 		}
10074 
10075 		return;
10076 	}
10077 
10078 	ath12k_mac_update_vif_offload(&ahvif->deflink);
10079 }
10080 EXPORT_SYMBOL(ath12k_mac_op_update_vif_offload);
10081 
10082 static bool ath12k_mac_vif_ap_active_any(struct ath12k_base *ab)
10083 {
10084 	struct ath12k *ar;
10085 	struct ath12k_pdev *pdev;
10086 	struct ath12k_link_vif *arvif;
10087 	int i;
10088 
10089 	for (i = 0; i < ab->num_radios; i++) {
10090 		pdev = &ab->pdevs[i];
10091 		ar = pdev->ar;
10092 		list_for_each_entry(arvif, &ar->arvifs, list) {
10093 			if (arvif->is_up &&
10094 			    arvif->ahvif->vdev_type == WMI_VDEV_TYPE_AP)
10095 				return true;
10096 		}
10097 	}
10098 	return false;
10099 }
10100 
10101 void ath12k_mac_11d_scan_start(struct ath12k *ar, u32 vdev_id)
10102 {
10103 	struct wmi_11d_scan_start_arg arg;
10104 	int ret;
10105 
10106 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
10107 
10108 	if (ar->regdom_set_by_user)
10109 		goto fin;
10110 
10111 	if (ar->vdev_id_11d_scan != ATH12K_11D_INVALID_VDEV_ID)
10112 		goto fin;
10113 
10114 	if (!test_bit(WMI_TLV_SERVICE_11D_OFFLOAD, ar->ab->wmi_ab.svc_map))
10115 		goto fin;
10116 
10117 	if (ath12k_mac_vif_ap_active_any(ar->ab))
10118 		goto fin;
10119 
10120 	arg.vdev_id = vdev_id;
10121 	arg.start_interval_msec = 0;
10122 	arg.scan_period_msec = ATH12K_SCAN_11D_INTERVAL;
10123 
10124 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
10125 		   "mac start 11d scan for vdev %d\n", vdev_id);
10126 
10127 	ret = ath12k_wmi_send_11d_scan_start_cmd(ar, &arg);
10128 	if (ret) {
10129 		ath12k_warn(ar->ab, "failed to start 11d scan vdev %d ret: %d\n",
10130 			    vdev_id, ret);
10131 	} else {
10132 		ar->vdev_id_11d_scan = vdev_id;
10133 		if (ar->state_11d == ATH12K_11D_PREPARING)
10134 			ar->state_11d = ATH12K_11D_RUNNING;
10135 	}
10136 
10137 fin:
10138 	if (ar->state_11d == ATH12K_11D_PREPARING) {
10139 		ar->state_11d = ATH12K_11D_IDLE;
10140 		complete(&ar->completed_11d_scan);
10141 	}
10142 }
10143 
10144 void ath12k_mac_11d_scan_stop(struct ath12k *ar)
10145 {
10146 	int ret;
10147 	u32 vdev_id;
10148 
10149 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
10150 
10151 	if (!test_bit(WMI_TLV_SERVICE_11D_OFFLOAD, ar->ab->wmi_ab.svc_map))
10152 		return;
10153 
10154 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac stop 11d for vdev %d\n",
10155 		   ar->vdev_id_11d_scan);
10156 
10157 	if (ar->state_11d == ATH12K_11D_PREPARING) {
10158 		ar->state_11d = ATH12K_11D_IDLE;
10159 		complete(&ar->completed_11d_scan);
10160 	}
10161 
10162 	if (ar->vdev_id_11d_scan != ATH12K_11D_INVALID_VDEV_ID) {
10163 		vdev_id = ar->vdev_id_11d_scan;
10164 
10165 		ret = ath12k_wmi_send_11d_scan_stop_cmd(ar, vdev_id);
10166 		if (ret) {
10167 			ath12k_warn(ar->ab,
10168 				    "failed to stopt 11d scan vdev %d ret: %d\n",
10169 				    vdev_id, ret);
10170 		} else {
10171 			ar->vdev_id_11d_scan = ATH12K_11D_INVALID_VDEV_ID;
10172 			ar->state_11d = ATH12K_11D_IDLE;
10173 			complete(&ar->completed_11d_scan);
10174 		}
10175 	}
10176 }
10177 
10178 void ath12k_mac_11d_scan_stop_all(struct ath12k_base *ab)
10179 {
10180 	struct ath12k *ar;
10181 	struct ath12k_pdev *pdev;
10182 	int i;
10183 
10184 	ath12k_dbg(ab, ATH12K_DBG_MAC, "mac stop soc 11d scan\n");
10185 
10186 	for (i = 0; i < ab->num_radios; i++) {
10187 		pdev = &ab->pdevs[i];
10188 		ar = pdev->ar;
10189 
10190 		ath12k_mac_11d_scan_stop(ar);
10191 	}
10192 }
10193 
10194 static void ath12k_mac_determine_vdev_type(struct ieee80211_vif *vif,
10195 					   struct ath12k_vif *ahvif)
10196 {
10197 	ahvif->vdev_subtype = WMI_VDEV_SUBTYPE_NONE;
10198 
10199 	switch (vif->type) {
10200 	case NL80211_IFTYPE_UNSPECIFIED:
10201 	case NL80211_IFTYPE_STATION:
10202 		ahvif->vdev_type = WMI_VDEV_TYPE_STA;
10203 
10204 		if (vif->p2p)
10205 			ahvif->vdev_subtype = WMI_VDEV_SUBTYPE_P2P_CLIENT;
10206 
10207 		break;
10208 	case NL80211_IFTYPE_MESH_POINT:
10209 		ahvif->vdev_subtype = WMI_VDEV_SUBTYPE_MESH_11S;
10210 		fallthrough;
10211 	case NL80211_IFTYPE_AP:
10212 		ahvif->vdev_type = WMI_VDEV_TYPE_AP;
10213 
10214 		if (vif->p2p)
10215 			ahvif->vdev_subtype = WMI_VDEV_SUBTYPE_P2P_GO;
10216 
10217 		break;
10218 	case NL80211_IFTYPE_MONITOR:
10219 		ahvif->vdev_type = WMI_VDEV_TYPE_MONITOR;
10220 		break;
10221 	case NL80211_IFTYPE_P2P_DEVICE:
10222 		ahvif->vdev_type = WMI_VDEV_TYPE_STA;
10223 		ahvif->vdev_subtype = WMI_VDEV_SUBTYPE_P2P_DEVICE;
10224 		break;
10225 	default:
10226 		WARN_ON(1);
10227 		break;
10228 	}
10229 }
10230 
10231 int ath12k_mac_vdev_create(struct ath12k *ar, struct ath12k_link_vif *arvif)
10232 {
10233 	struct ath12k_hw *ah = ar->ah;
10234 	struct ath12k_base *ab = ar->ab;
10235 	struct ieee80211_hw *hw = ah->hw;
10236 	struct ath12k_vif *ahvif = arvif->ahvif;
10237 	struct ieee80211_vif *vif = ath12k_ahvif_to_vif(ahvif);
10238 	struct ath12k_wmi_vdev_create_arg vdev_arg = {};
10239 	struct ath12k_wmi_peer_create_arg peer_param = {};
10240 	struct ieee80211_bss_conf *link_conf = NULL;
10241 	u32 param_id, param_value;
10242 	u16 nss;
10243 	int i;
10244 	int ret, vdev_id;
10245 	u8 link_id;
10246 	struct ath12k_dp_link_vif *dp_link_vif = NULL;
10247 	struct ath12k_dp_peer_create_params params = {};
10248 	bool dp_peer_created = false;
10249 
10250 	lockdep_assert_wiphy(hw->wiphy);
10251 
10252 	/* In NO_VIRTUAL_MONITOR, its necessary to restrict only one monitor
10253 	 * interface in each radio
10254 	 */
10255 	if (vif->type == NL80211_IFTYPE_MONITOR && ar->monitor_vdev_created)
10256 		return -EINVAL;
10257 
10258 	if (ar->num_created_vdevs >= TARGET_NUM_VDEVS(ab)) {
10259 		ath12k_warn(ab, "failed to create vdev, reached max vdev limit %d\n",
10260 			    TARGET_NUM_VDEVS(ab));
10261 		return -ENOSPC;
10262 	}
10263 
10264 	link_id = arvif->link_id;
10265 
10266 	if (link_id < IEEE80211_MLD_MAX_NUM_LINKS) {
10267 		link_conf = wiphy_dereference(hw->wiphy, vif->link_conf[link_id]);
10268 		if (!link_conf) {
10269 			ath12k_warn(ar->ab, "unable to access bss link conf in vdev create for vif %pM link %u\n",
10270 				    vif->addr, arvif->link_id);
10271 			return -ENOLINK;
10272 		}
10273 	}
10274 
10275 	if (link_conf)
10276 		memcpy(arvif->bssid, link_conf->addr, ETH_ALEN);
10277 	else
10278 		memcpy(arvif->bssid, vif->addr, ETH_ALEN);
10279 
10280 	arvif->ar = ar;
10281 	vdev_id = __ffs64(ab->free_vdev_map);
10282 	arvif->vdev_id = vdev_id;
10283 	if (vif->type == NL80211_IFTYPE_MONITOR)
10284 		ar->monitor_vdev_id = vdev_id;
10285 
10286 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac vdev create id %d type %d subtype %d map %llx\n",
10287 		   arvif->vdev_id, ahvif->vdev_type, ahvif->vdev_subtype,
10288 		   ab->free_vdev_map);
10289 
10290 	vif->cab_queue = arvif->vdev_id % (ATH12K_HW_MAX_QUEUES - 1);
10291 	for (i = 0; i < ARRAY_SIZE(vif->hw_queue); i++)
10292 		vif->hw_queue[i] = i % (ATH12K_HW_MAX_QUEUES - 1);
10293 
10294 	ret = ath12k_mac_setup_vdev_create_arg(arvif, &vdev_arg);
10295 	if (ret) {
10296 		ath12k_warn(ab, "failed to create vdev parameters %d: %d\n",
10297 			    arvif->vdev_id, ret);
10298 		goto err;
10299 	}
10300 
10301 	ret = ath12k_wmi_vdev_create(ar, arvif->bssid, &vdev_arg);
10302 	if (ret) {
10303 		ath12k_warn(ab, "failed to create WMI vdev %d: %d\n",
10304 			    arvif->vdev_id, ret);
10305 		return ret;
10306 	}
10307 
10308 	ar->num_created_vdevs++;
10309 	arvif->is_created = true;
10310 	ath12k_dbg(ab, ATH12K_DBG_MAC, "vdev %pM created, vdev_id %d\n",
10311 		   vif->addr, arvif->vdev_id);
10312 	ar->allocated_vdev_map |= 1LL << arvif->vdev_id;
10313 	ab->free_vdev_map &= ~(1LL << arvif->vdev_id);
10314 
10315 	spin_lock_bh(&ar->data_lock);
10316 	list_add(&arvif->list, &ar->arvifs);
10317 	spin_unlock_bh(&ar->data_lock);
10318 
10319 	ath12k_mac_update_vif_offload(arvif);
10320 
10321 	nss = hweight32(ar->cfg_tx_chainmask) ? : 1;
10322 	ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
10323 					    WMI_VDEV_PARAM_NSS, nss);
10324 	if (ret) {
10325 		ath12k_warn(ab, "failed to set vdev %d chainmask 0x%x, nss %d :%d\n",
10326 			    arvif->vdev_id, ar->cfg_tx_chainmask, nss, ret);
10327 		goto err_vdev_del;
10328 	}
10329 
10330 	dp_link_vif = ath12k_dp_vif_to_dp_link_vif(&ahvif->dp_vif, arvif->link_id);
10331 
10332 	dp_link_vif->vdev_id = arvif->vdev_id;
10333 	dp_link_vif->lmac_id = ar->lmac_id;
10334 	dp_link_vif->pdev_idx = ar->pdev_idx;
10335 
10336 	switch (ahvif->vdev_type) {
10337 	case WMI_VDEV_TYPE_AP:
10338 		params.ucast_ra_only = true;
10339 
10340 		if (arvif->link_id < IEEE80211_MLD_MAX_NUM_LINKS) {
10341 			ret = ath12k_dp_peer_create(&ah->dp_hw, arvif->bssid, &params);
10342 			if (ret) {
10343 				ath12k_warn(ab, "failed to vdev %d create dp_peer for AP: %d\n",
10344 					    arvif->vdev_id, ret);
10345 				goto err_vdev_del;
10346 			}
10347 			dp_peer_created = true;
10348 		}
10349 
10350 		peer_param.vdev_id = arvif->vdev_id;
10351 		peer_param.peer_addr = arvif->bssid;
10352 		peer_param.peer_type = WMI_PEER_TYPE_DEFAULT;
10353 		ret = ath12k_peer_create(ar, arvif, NULL, &peer_param);
10354 		if (ret) {
10355 			ath12k_warn(ab, "failed to vdev %d create peer for AP: %d\n",
10356 				    arvif->vdev_id, ret);
10357 			goto err_dp_peer_del;
10358 		}
10359 
10360 		ret = ath12k_mac_set_kickout(arvif);
10361 		if (ret) {
10362 			ath12k_warn(ar->ab, "failed to set vdev %i kickout parameters: %d\n",
10363 				    arvif->vdev_id, ret);
10364 			goto err_peer_del;
10365 		}
10366 		ath12k_mac_11d_scan_stop_all(ar->ab);
10367 		break;
10368 	case WMI_VDEV_TYPE_STA:
10369 		param_id = WMI_STA_PS_PARAM_RX_WAKE_POLICY;
10370 		param_value = WMI_STA_PS_RX_WAKE_POLICY_WAKE;
10371 		ret = ath12k_wmi_set_sta_ps_param(ar, arvif->vdev_id,
10372 						  param_id, param_value);
10373 		if (ret) {
10374 			ath12k_warn(ar->ab, "failed to set vdev %d RX wake policy: %d\n",
10375 				    arvif->vdev_id, ret);
10376 			goto err_peer_del;
10377 		}
10378 
10379 		param_id = WMI_STA_PS_PARAM_TX_WAKE_THRESHOLD;
10380 		param_value = WMI_STA_PS_TX_WAKE_THRESHOLD_ALWAYS;
10381 		ret = ath12k_wmi_set_sta_ps_param(ar, arvif->vdev_id,
10382 						  param_id, param_value);
10383 		if (ret) {
10384 			ath12k_warn(ar->ab, "failed to set vdev %d TX wake threshold: %d\n",
10385 				    arvif->vdev_id, ret);
10386 			goto err_peer_del;
10387 		}
10388 
10389 		param_id = WMI_STA_PS_PARAM_PSPOLL_COUNT;
10390 		param_value = WMI_STA_PS_PSPOLL_COUNT_NO_MAX;
10391 		ret = ath12k_wmi_set_sta_ps_param(ar, arvif->vdev_id,
10392 						  param_id, param_value);
10393 		if (ret) {
10394 			ath12k_warn(ar->ab, "failed to set vdev %d pspoll count: %d\n",
10395 				    arvif->vdev_id, ret);
10396 			goto err_peer_del;
10397 		}
10398 
10399 		ret = ath12k_wmi_pdev_set_ps_mode(ar, arvif->vdev_id, false);
10400 		if (ret) {
10401 			ath12k_warn(ar->ab, "failed to disable vdev %d ps mode: %d\n",
10402 				    arvif->vdev_id, ret);
10403 			goto err_peer_del;
10404 		}
10405 
10406 		if (test_bit(WMI_TLV_SERVICE_11D_OFFLOAD, ab->wmi_ab.svc_map) &&
10407 		    ahvif->vdev_type == WMI_VDEV_TYPE_STA &&
10408 		    ahvif->vdev_subtype == WMI_VDEV_SUBTYPE_NONE) {
10409 			reinit_completion(&ar->completed_11d_scan);
10410 			ar->state_11d = ATH12K_11D_PREPARING;
10411 		}
10412 		break;
10413 	case WMI_VDEV_TYPE_MONITOR:
10414 		ar->monitor_vdev_created = true;
10415 		break;
10416 	default:
10417 		break;
10418 	}
10419 
10420 	if (link_conf)
10421 		arvif->txpower = link_conf->txpower;
10422 	else
10423 		arvif->txpower = NL80211_TX_POWER_AUTOMATIC;
10424 
10425 	ret = ath12k_mac_txpower_recalc(ar);
10426 	if (ret)
10427 		goto err_peer_del;
10428 
10429 	param_id = WMI_VDEV_PARAM_RTS_THRESHOLD;
10430 	param_value = hw->wiphy->rts_threshold;
10431 	ar->rts_threshold = param_value;
10432 	ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
10433 					    param_id, param_value);
10434 	if (ret) {
10435 		ath12k_warn(ar->ab, "failed to set rts threshold for vdev %d: %d\n",
10436 			    arvif->vdev_id, ret);
10437 	}
10438 
10439 	ath12k_dp_vdev_tx_attach(ar, arvif);
10440 
10441 	return ret;
10442 
10443 err_peer_del:
10444 	if (ahvif->vdev_type == WMI_VDEV_TYPE_AP) {
10445 		reinit_completion(&ar->peer_delete_done);
10446 
10447 		ret = ath12k_wmi_send_peer_delete_cmd(ar, arvif->bssid,
10448 						      arvif->vdev_id);
10449 		if (ret) {
10450 			ath12k_warn(ar->ab, "failed to delete peer vdev_id %d addr %pM\n",
10451 				    arvif->vdev_id, arvif->bssid);
10452 			goto err;
10453 		}
10454 
10455 		ret = ath12k_wait_for_peer_delete_done(ar, arvif->vdev_id,
10456 						       arvif->bssid);
10457 		if (ret)
10458 			goto err_vdev_del;
10459 
10460 		ar->num_peers--;
10461 	}
10462 
10463 err_dp_peer_del:
10464 	if (dp_peer_created)
10465 		ath12k_dp_peer_delete(&ah->dp_hw, arvif->bssid, NULL);
10466 
10467 err_vdev_del:
10468 	if (ahvif->vdev_type == WMI_VDEV_TYPE_MONITOR) {
10469 		ar->monitor_vdev_id = -1;
10470 		ar->monitor_vdev_created = false;
10471 	}
10472 
10473 	ath12k_wmi_vdev_delete(ar, arvif->vdev_id);
10474 	ar->num_created_vdevs--;
10475 	arvif->is_created = false;
10476 	arvif->ar = NULL;
10477 	ar->allocated_vdev_map &= ~(1LL << arvif->vdev_id);
10478 	ab->free_vdev_map |= 1LL << arvif->vdev_id;
10479 	ab->free_vdev_stats_id_map &= ~(1LL << arvif->vdev_stats_id);
10480 	spin_lock_bh(&ar->data_lock);
10481 	list_del(&arvif->list);
10482 	spin_unlock_bh(&ar->data_lock);
10483 
10484 err:
10485 	arvif->ar = NULL;
10486 	return ret;
10487 }
10488 
10489 static void ath12k_mac_vif_flush_key_cache(struct ath12k_link_vif *arvif)
10490 {
10491 	struct ath12k_key_conf *key_conf, *tmp;
10492 	struct ath12k_vif *ahvif = arvif->ahvif;
10493 	struct ath12k_hw *ah = ahvif->ah;
10494 	struct ath12k_sta *ahsta;
10495 	struct ath12k_link_sta *arsta;
10496 	struct ath12k_vif_cache *cache = ahvif->cache[arvif->link_id];
10497 	int ret;
10498 
10499 	lockdep_assert_wiphy(ah->hw->wiphy);
10500 
10501 	list_for_each_entry_safe(key_conf, tmp, &cache->key_conf.list, list) {
10502 		arsta = NULL;
10503 		if (key_conf->sta) {
10504 			ahsta = ath12k_sta_to_ahsta(key_conf->sta);
10505 			arsta = wiphy_dereference(ah->hw->wiphy,
10506 						  ahsta->link[arvif->link_id]);
10507 			if (!arsta)
10508 				goto free_cache;
10509 		}
10510 
10511 		ret = ath12k_mac_set_key(arvif->ar, key_conf->cmd,
10512 					 arvif, arsta,
10513 					 key_conf->key);
10514 		if (ret)
10515 			ath12k_warn(arvif->ar->ab, "unable to apply set key param to vdev %d ret %d\n",
10516 				    arvif->vdev_id, ret);
10517 free_cache:
10518 		list_del(&key_conf->list);
10519 		kfree(key_conf);
10520 	}
10521 }
10522 
10523 static void ath12k_mac_vif_cache_flush(struct ath12k *ar, struct ath12k_link_vif *arvif)
10524 {
10525 	struct ath12k_vif *ahvif = arvif->ahvif;
10526 	struct ieee80211_vif *vif = ath12k_ahvif_to_vif(ahvif);
10527 	struct ath12k_vif_cache *cache = ahvif->cache[arvif->link_id];
10528 	struct ath12k_base *ab = ar->ab;
10529 	struct ieee80211_bss_conf *link_conf;
10530 
10531 	int ret;
10532 
10533 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
10534 
10535 	if (!cache)
10536 		return;
10537 
10538 	if (cache->tx_conf.changed) {
10539 		ret = ath12k_mac_conf_tx(arvif, cache->tx_conf.ac,
10540 					 &cache->tx_conf.tx_queue_params);
10541 		if (ret)
10542 			ath12k_warn(ab,
10543 				    "unable to apply tx config parameters to vdev %d\n",
10544 				    ret);
10545 	}
10546 
10547 	if (cache->bss_conf_changed) {
10548 		link_conf = ath12k_mac_get_link_bss_conf(arvif);
10549 		if (!link_conf) {
10550 			ath12k_warn(ar->ab, "unable to access bss link conf in cache flush for vif %pM link %u\n",
10551 				    vif->addr, arvif->link_id);
10552 			return;
10553 		}
10554 		ath12k_mac_bss_info_changed(ar, arvif, link_conf,
10555 					    cache->bss_conf_changed);
10556 	}
10557 
10558 	if (!list_empty(&cache->key_conf.list))
10559 		ath12k_mac_vif_flush_key_cache(arvif);
10560 
10561 	ath12k_ahvif_put_link_cache(ahvif, arvif->link_id);
10562 }
10563 
10564 static struct ath12k *ath12k_mac_assign_vif_to_vdev(struct ieee80211_hw *hw,
10565 						    struct ath12k_link_vif *arvif,
10566 						    struct ieee80211_chanctx_conf *ctx)
10567 {
10568 	struct ath12k_vif *ahvif = arvif->ahvif;
10569 	struct ieee80211_vif *vif = ath12k_ahvif_to_vif(ahvif);
10570 	struct ath12k_link_vif *scan_arvif;
10571 	struct ath12k_hw *ah = hw->priv;
10572 	struct ath12k *ar;
10573 	struct ath12k_base *ab;
10574 	u8 link_id = arvif->link_id, scan_link_id;
10575 	unsigned long scan_link_map;
10576 	int ret;
10577 
10578 	lockdep_assert_wiphy(hw->wiphy);
10579 
10580 	if (ah->num_radio == 1)
10581 		ar = ah->radio;
10582 	else if (ctx)
10583 		ar = ath12k_get_ar_by_ctx(hw, ctx);
10584 	else
10585 		return NULL;
10586 
10587 	if (!ar)
10588 		return NULL;
10589 
10590 	/* cleanup the scan vdev if we are done scan on that ar
10591 	 * and now we want to create for actual usage.
10592 	 */
10593 	if (ieee80211_vif_is_mld(vif)) {
10594 		scan_link_map = ahvif->links_map & ATH12K_SCAN_LINKS_MASK;
10595 		for_each_set_bit(scan_link_id, &scan_link_map, ATH12K_NUM_MAX_LINKS) {
10596 			scan_arvif = wiphy_dereference(hw->wiphy,
10597 						       ahvif->link[scan_link_id]);
10598 			if (scan_arvif && scan_arvif->ar == ar) {
10599 				ar->scan.arvif = NULL;
10600 				ath12k_mac_remove_link_interface(hw, scan_arvif);
10601 				ath12k_mac_unassign_link_vif(scan_arvif);
10602 				break;
10603 			}
10604 		}
10605 	}
10606 
10607 	if (arvif->ar) {
10608 		/* This is not expected really */
10609 		if (WARN_ON(!arvif->is_created)) {
10610 			arvif->ar = NULL;
10611 			return NULL;
10612 		}
10613 
10614 		if (ah->num_radio == 1)
10615 			return arvif->ar;
10616 
10617 		/* This can happen as scan vdev gets created during multiple scans
10618 		 * across different radios before a vdev is brought up in
10619 		 * a certain radio.
10620 		 */
10621 		if (ar != arvif->ar) {
10622 			if (WARN_ON(arvif->is_started))
10623 				return NULL;
10624 
10625 			ath12k_mac_remove_link_interface(hw, arvif);
10626 			ath12k_mac_unassign_link_vif(arvif);
10627 		}
10628 	}
10629 
10630 	ab = ar->ab;
10631 
10632 	/* Assign arvif again here since previous radio switch block
10633 	 * would've unassigned and cleared it.
10634 	 */
10635 	arvif = ath12k_mac_assign_link_vif(ah, vif, link_id);
10636 	if (vif->type == NL80211_IFTYPE_AP &&
10637 	    ar->num_peers > (ar->max_num_peers - 1)) {
10638 		ath12k_warn(ab, "failed to create vdev due to insufficient peer entry resource in firmware\n");
10639 		goto unlock;
10640 	}
10641 
10642 	if (arvif->is_created)
10643 		goto flush;
10644 
10645 	ret = ath12k_mac_vdev_create(ar, arvif);
10646 	if (ret) {
10647 		ath12k_warn(ab, "failed to create vdev %pM ret %d", vif->addr, ret);
10648 		goto unlock;
10649 	}
10650 
10651 flush:
10652 	/* If the vdev is created during channel assign and not during
10653 	 * add_interface(), Apply any parameters for the vdev which were received
10654 	 * after add_interface, corresponding to this vif.
10655 	 */
10656 	ath12k_mac_vif_cache_flush(ar, arvif);
10657 unlock:
10658 	return arvif->ar;
10659 }
10660 
10661 int ath12k_mac_op_add_interface(struct ieee80211_hw *hw,
10662 				struct ieee80211_vif *vif)
10663 {
10664 	struct ath12k_hw *ah = ath12k_hw_to_ah(hw);
10665 	struct ath12k_vif *ahvif = ath12k_vif_to_ahvif(vif);
10666 	struct ath12k_reg_info *reg_info;
10667 	struct ath12k_link_vif *arvif;
10668 	struct ath12k_base *ab;
10669 	struct ath12k *ar;
10670 	int i;
10671 
10672 	lockdep_assert_wiphy(hw->wiphy);
10673 
10674 	memset(ahvif, 0, sizeof(*ahvif));
10675 
10676 	ahvif->ah = ah;
10677 	ahvif->vif = vif;
10678 	arvif = &ahvif->deflink;
10679 
10680 	ath12k_mac_init_arvif(ahvif, arvif, -1);
10681 
10682 	/* Allocate Default Queue now and reassign during actual vdev create */
10683 	vif->cab_queue = ATH12K_HW_DEFAULT_QUEUE;
10684 	for (i = 0; i < ARRAY_SIZE(vif->hw_queue); i++)
10685 		vif->hw_queue[i] = ATH12K_HW_DEFAULT_QUEUE;
10686 
10687 	vif->driver_flags |= IEEE80211_VIF_SUPPORTS_UAPSD;
10688 
10689 	ath12k_mac_determine_vdev_type(vif, ahvif);
10690 
10691 	for_each_ar(ah, ar, i) {
10692 		if (!ath12k_wmi_supports_6ghz_cc_ext(ar))
10693 			continue;
10694 
10695 		ab = ar->ab;
10696 		reg_info = ab->reg_info[ar->pdev_idx];
10697 		ath12k_dbg(ab, ATH12K_DBG_MAC, "interface added to change reg rules\n");
10698 		ah->regd_updated = false;
10699 		ath12k_reg_handle_chan_list(ab, reg_info, ahvif->vdev_type,
10700 					    IEEE80211_REG_UNSET_AP);
10701 		break;
10702 	}
10703 
10704 	/* Defer vdev creation until assign_chanctx or hw_scan is initiated as driver
10705 	 * will not know if this interface is an ML vif at this point.
10706 	 */
10707 	return 0;
10708 }
10709 EXPORT_SYMBOL(ath12k_mac_op_add_interface);
10710 
10711 static void ath12k_mac_vif_unref(struct ath12k_dp *dp, struct ieee80211_vif *vif)
10712 {
10713 	struct ath12k_tx_desc_info *tx_desc_info;
10714 	struct ath12k_skb_cb *skb_cb;
10715 	struct sk_buff *skb;
10716 	int i;
10717 
10718 	for (i = 0; i < ATH12K_HW_MAX_QUEUES; i++) {
10719 		spin_lock_bh(&dp->tx_desc_lock[i]);
10720 
10721 		list_for_each_entry(tx_desc_info, &dp->tx_desc_used_list[i],
10722 				    list) {
10723 			skb = tx_desc_info->skb;
10724 			if (!skb)
10725 				continue;
10726 
10727 			skb_cb = ATH12K_SKB_CB(skb);
10728 			if (skb_cb->vif == vif)
10729 				skb_cb->vif = NULL;
10730 		}
10731 
10732 		spin_unlock_bh(&dp->tx_desc_lock[i]);
10733 	}
10734 }
10735 
10736 static int ath12k_mac_vdev_delete(struct ath12k *ar, struct ath12k_link_vif *arvif)
10737 {
10738 	struct ath12k_vif *ahvif = arvif->ahvif;
10739 	struct ieee80211_vif *vif = ath12k_ahvif_to_vif(ahvif);
10740 	struct ath12k_dp_link_vif *dp_link_vif;
10741 	struct ath12k_base *ab = ar->ab;
10742 	unsigned long time_left;
10743 	int ret;
10744 
10745 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
10746 
10747 	reinit_completion(&ar->vdev_delete_done);
10748 
10749 	ret = ath12k_wmi_vdev_delete(ar, arvif->vdev_id);
10750 	if (ret) {
10751 		ath12k_warn(ab, "failed to delete WMI vdev %d: %d\n",
10752 			    arvif->vdev_id, ret);
10753 		goto err_vdev_del;
10754 	}
10755 
10756 	time_left = wait_for_completion_timeout(&ar->vdev_delete_done,
10757 						ATH12K_VDEV_DELETE_TIMEOUT_HZ);
10758 	if (time_left == 0) {
10759 		ath12k_warn(ab, "Timeout in receiving vdev delete response\n");
10760 		goto err_vdev_del;
10761 	}
10762 
10763 	ab->free_vdev_map |= 1LL << arvif->vdev_id;
10764 	ar->allocated_vdev_map &= ~(1LL << arvif->vdev_id);
10765 	ar->num_created_vdevs--;
10766 
10767 	if (ahvif->vdev_type == WMI_VDEV_TYPE_MONITOR) {
10768 		ar->monitor_vdev_id = -1;
10769 		ar->monitor_vdev_created = false;
10770 	}
10771 
10772 	ath12k_dbg(ab, ATH12K_DBG_MAC, "vdev %pM deleted, vdev_id %d\n",
10773 		   vif->addr, arvif->vdev_id);
10774 
10775 err_vdev_del:
10776 	spin_lock_bh(&ar->data_lock);
10777 	list_del(&arvif->list);
10778 	spin_unlock_bh(&ar->data_lock);
10779 
10780 	ath12k_peer_cleanup(ar, arvif->vdev_id);
10781 	ath12k_ahvif_put_link_cache(ahvif, arvif->link_id);
10782 
10783 	idr_for_each(&ar->txmgmt_idr,
10784 		     ath12k_mac_vif_txmgmt_idr_remove, vif);
10785 
10786 	ath12k_mac_vif_unref(ath12k_ab_to_dp(ab), vif);
10787 
10788 	dp_link_vif = ath12k_dp_vif_to_dp_link_vif(&ahvif->dp_vif, arvif->link_id);
10789 	ath12k_dp_tx_put_bank_profile(ath12k_ab_to_dp(ab), dp_link_vif->bank_id);
10790 
10791 	/* Recalc txpower for remaining vdev */
10792 	ath12k_mac_txpower_recalc(ar);
10793 
10794 	/* TODO: recal traffic pause state based on the available vdevs */
10795 	arvif->is_created = false;
10796 	arvif->ar = NULL;
10797 
10798 	return ret;
10799 }
10800 
10801 void ath12k_mac_op_remove_interface(struct ieee80211_hw *hw,
10802 				    struct ieee80211_vif *vif)
10803 {
10804 	struct ath12k_vif *ahvif = ath12k_vif_to_ahvif(vif);
10805 	struct ath12k_link_vif *arvif;
10806 	struct ath12k *ar;
10807 	u8 link_id;
10808 
10809 	lockdep_assert_wiphy(hw->wiphy);
10810 
10811 	for (link_id = 0; link_id < ATH12K_NUM_MAX_LINKS; link_id++) {
10812 		/* if we cached some config but never received assign chanctx,
10813 		 * free the allocated cache.
10814 		 */
10815 		ath12k_ahvif_put_link_cache(ahvif, link_id);
10816 		arvif = wiphy_dereference(hw->wiphy, ahvif->link[link_id]);
10817 		if (!arvif || !arvif->is_created)
10818 			continue;
10819 
10820 		ar = arvif->ar;
10821 
10822 		/* Scan abortion is in progress since before this, cancel_hw_scan()
10823 		 * is expected to be executed. Since link is anyways going to be removed
10824 		 * now, just cancel the worker and send the scan aborted to user space
10825 		 */
10826 		if (ar->scan.arvif == arvif) {
10827 			wiphy_work_cancel(hw->wiphy, &ar->scan.vdev_clean_wk);
10828 
10829 			spin_lock_bh(&ar->data_lock);
10830 			ar->scan.arvif = NULL;
10831 			if (!ar->scan.is_roc) {
10832 				struct cfg80211_scan_info info = {
10833 					.aborted = true,
10834 				};
10835 
10836 				ath12k_mac_scan_send_complete(ar, &info);
10837 			}
10838 
10839 			ar->scan.state = ATH12K_SCAN_IDLE;
10840 			ar->scan_channel = NULL;
10841 			ar->scan.roc_freq = 0;
10842 			spin_unlock_bh(&ar->data_lock);
10843 		}
10844 
10845 		ath12k_mac_remove_link_interface(hw, arvif);
10846 		ath12k_mac_unassign_link_vif(arvif);
10847 	}
10848 }
10849 EXPORT_SYMBOL(ath12k_mac_op_remove_interface);
10850 
10851 /* FIXME: Has to be verified. */
10852 #define SUPPORTED_FILTERS			\
10853 	(FIF_ALLMULTI |				\
10854 	FIF_CONTROL |				\
10855 	FIF_PSPOLL |				\
10856 	FIF_OTHER_BSS |				\
10857 	FIF_BCN_PRBRESP_PROMISC |		\
10858 	FIF_PROBE_REQ |				\
10859 	FIF_FCSFAIL)
10860 
10861 void ath12k_mac_op_configure_filter(struct ieee80211_hw *hw,
10862 				    unsigned int changed_flags,
10863 				    unsigned int *total_flags,
10864 				    u64 multicast)
10865 {
10866 	struct ath12k_hw *ah = ath12k_hw_to_ah(hw);
10867 	struct ath12k *ar;
10868 
10869 	lockdep_assert_wiphy(hw->wiphy);
10870 
10871 	ar = ath12k_ah_to_ar(ah, 0);
10872 
10873 	*total_flags &= SUPPORTED_FILTERS;
10874 	ar->filter_flags = *total_flags;
10875 }
10876 EXPORT_SYMBOL(ath12k_mac_op_configure_filter);
10877 
10878 int ath12k_mac_op_get_antenna(struct ieee80211_hw *hw, int radio_idx,
10879 			      u32 *tx_ant, u32 *rx_ant)
10880 {
10881 	struct ath12k_hw *ah = ath12k_hw_to_ah(hw);
10882 	int antennas_rx = 0, antennas_tx = 0;
10883 	struct ath12k *ar;
10884 	int i;
10885 
10886 	lockdep_assert_wiphy(hw->wiphy);
10887 
10888 	for_each_ar(ah, ar, i) {
10889 		antennas_rx = max_t(u32, antennas_rx, ar->cfg_rx_chainmask);
10890 		antennas_tx = max_t(u32, antennas_tx, ar->cfg_tx_chainmask);
10891 	}
10892 
10893 	*tx_ant = antennas_tx;
10894 	*rx_ant = antennas_rx;
10895 
10896 	return 0;
10897 }
10898 EXPORT_SYMBOL(ath12k_mac_op_get_antenna);
10899 
10900 int ath12k_mac_op_set_antenna(struct ieee80211_hw *hw, int radio_idx,
10901 			      u32 tx_ant, u32 rx_ant)
10902 {
10903 	struct ath12k_hw *ah = ath12k_hw_to_ah(hw);
10904 	struct ath12k *ar;
10905 	int ret = 0;
10906 	int i;
10907 
10908 	lockdep_assert_wiphy(hw->wiphy);
10909 
10910 	for_each_ar(ah, ar, i) {
10911 		ret = __ath12k_set_antenna(ar, tx_ant, rx_ant);
10912 		if (ret)
10913 			break;
10914 	}
10915 
10916 	return ret;
10917 }
10918 EXPORT_SYMBOL(ath12k_mac_op_set_antenna);
10919 
10920 static int ath12k_mac_ampdu_action(struct ieee80211_hw *hw,
10921 				   struct ieee80211_vif *vif,
10922 				   struct ieee80211_ampdu_params *params,
10923 				   u8 link_id)
10924 {
10925 	struct ath12k *ar;
10926 	int ret = -EINVAL;
10927 
10928 	lockdep_assert_wiphy(hw->wiphy);
10929 
10930 	ar = ath12k_get_ar_by_vif(hw, vif, link_id);
10931 	if (!ar)
10932 		return -EINVAL;
10933 
10934 	switch (params->action) {
10935 	case IEEE80211_AMPDU_RX_START:
10936 		ret = ath12k_dp_rx_ampdu_start(ar, params, link_id);
10937 		break;
10938 	case IEEE80211_AMPDU_RX_STOP:
10939 		ret = ath12k_dp_rx_ampdu_stop(ar, params, link_id);
10940 		break;
10941 	case IEEE80211_AMPDU_TX_START:
10942 	case IEEE80211_AMPDU_TX_STOP_CONT:
10943 	case IEEE80211_AMPDU_TX_STOP_FLUSH:
10944 	case IEEE80211_AMPDU_TX_STOP_FLUSH_CONT:
10945 	case IEEE80211_AMPDU_TX_OPERATIONAL:
10946 		/* Tx A-MPDU aggregation offloaded to hw/fw so deny mac80211
10947 		 * Tx aggregation requests.
10948 		 */
10949 		ret = -EOPNOTSUPP;
10950 		break;
10951 	}
10952 
10953 	if (ret)
10954 		ath12k_warn(ar->ab, "unable to perform ampdu action %d for vif %pM link %u ret %d\n",
10955 			    params->action, vif->addr, link_id, ret);
10956 
10957 	return ret;
10958 }
10959 
10960 int ath12k_mac_op_ampdu_action(struct ieee80211_hw *hw,
10961 			       struct ieee80211_vif *vif,
10962 			       struct ieee80211_ampdu_params *params)
10963 {
10964 	struct ieee80211_sta *sta = params->sta;
10965 	struct ath12k_sta *ahsta = ath12k_sta_to_ahsta(sta);
10966 	unsigned long links_map = ahsta->links_map;
10967 	int ret = -EINVAL;
10968 	u8 link_id;
10969 
10970 	lockdep_assert_wiphy(hw->wiphy);
10971 
10972 	if (WARN_ON(!links_map))
10973 		return ret;
10974 
10975 	for_each_set_bit(link_id, &links_map, IEEE80211_MLD_MAX_NUM_LINKS) {
10976 		ret = ath12k_mac_ampdu_action(hw, vif, params, link_id);
10977 		if (ret)
10978 			return ret;
10979 	}
10980 
10981 	return 0;
10982 }
10983 EXPORT_SYMBOL(ath12k_mac_op_ampdu_action);
10984 
10985 int ath12k_mac_op_add_chanctx(struct ieee80211_hw *hw,
10986 			      struct ieee80211_chanctx_conf *ctx)
10987 {
10988 	struct ath12k *ar;
10989 	struct ath12k_base *ab;
10990 
10991 	lockdep_assert_wiphy(hw->wiphy);
10992 
10993 	ar = ath12k_get_ar_by_ctx(hw, ctx);
10994 	if (!ar)
10995 		return -EINVAL;
10996 
10997 	ab = ar->ab;
10998 
10999 	ath12k_dbg(ab, ATH12K_DBG_MAC,
11000 		   "mac chanctx add freq %u width %d ptr %p\n",
11001 		   ctx->def.chan->center_freq, ctx->def.width, ctx);
11002 
11003 	spin_lock_bh(&ar->data_lock);
11004 	/* TODO: In case of multiple channel context, populate rx_channel from
11005 	 * Rx PPDU desc information.
11006 	 */
11007 	ar->rx_channel = ctx->def.chan;
11008 	spin_unlock_bh(&ar->data_lock);
11009 	ar->chan_tx_pwr = ATH12K_PDEV_TX_POWER_INVALID;
11010 
11011 	return 0;
11012 }
11013 EXPORT_SYMBOL(ath12k_mac_op_add_chanctx);
11014 
11015 void ath12k_mac_op_remove_chanctx(struct ieee80211_hw *hw,
11016 				  struct ieee80211_chanctx_conf *ctx)
11017 {
11018 	struct ath12k *ar;
11019 	struct ath12k_base *ab;
11020 
11021 	lockdep_assert_wiphy(hw->wiphy);
11022 
11023 	ar = ath12k_get_ar_by_ctx(hw, ctx);
11024 	if (!ar)
11025 		return;
11026 
11027 	ab = ar->ab;
11028 
11029 	ath12k_dbg(ab, ATH12K_DBG_MAC,
11030 		   "mac chanctx remove freq %u width %d ptr %p\n",
11031 		   ctx->def.chan->center_freq, ctx->def.width, ctx);
11032 
11033 	spin_lock_bh(&ar->data_lock);
11034 	/* TODO: In case of there is one more channel context left, populate
11035 	 * rx_channel with the channel of that remaining channel context.
11036 	 */
11037 	ar->rx_channel = NULL;
11038 	spin_unlock_bh(&ar->data_lock);
11039 	ar->chan_tx_pwr = ATH12K_PDEV_TX_POWER_INVALID;
11040 }
11041 EXPORT_SYMBOL(ath12k_mac_op_remove_chanctx);
11042 
11043 static enum wmi_phy_mode
11044 ath12k_mac_check_down_grade_phy_mode(struct ath12k *ar,
11045 				     enum wmi_phy_mode mode,
11046 				     enum nl80211_band band,
11047 				     enum nl80211_iftype type)
11048 {
11049 	struct ieee80211_sta_eht_cap *eht_cap = NULL;
11050 	enum wmi_phy_mode down_mode;
11051 	int n = ar->mac.sbands[band].n_iftype_data;
11052 	int i;
11053 	struct ieee80211_sband_iftype_data *data;
11054 
11055 	if (mode < MODE_11BE_EHT20)
11056 		return mode;
11057 
11058 	data = ar->mac.iftype[band];
11059 	for (i = 0; i < n; i++) {
11060 		if (data[i].types_mask & BIT(type)) {
11061 			eht_cap = &data[i].eht_cap;
11062 			break;
11063 		}
11064 	}
11065 
11066 	if (eht_cap && eht_cap->has_eht)
11067 		return mode;
11068 
11069 	switch (mode) {
11070 	case MODE_11BE_EHT20:
11071 		down_mode = MODE_11AX_HE20;
11072 		break;
11073 	case MODE_11BE_EHT40:
11074 		down_mode = MODE_11AX_HE40;
11075 		break;
11076 	case MODE_11BE_EHT80:
11077 		down_mode = MODE_11AX_HE80;
11078 		break;
11079 	case MODE_11BE_EHT80_80:
11080 		down_mode = MODE_11AX_HE80_80;
11081 		break;
11082 	case MODE_11BE_EHT160:
11083 	case MODE_11BE_EHT160_160:
11084 	case MODE_11BE_EHT320:
11085 		down_mode = MODE_11AX_HE160;
11086 		break;
11087 	case MODE_11BE_EHT20_2G:
11088 		down_mode = MODE_11AX_HE20_2G;
11089 		break;
11090 	case MODE_11BE_EHT40_2G:
11091 		down_mode = MODE_11AX_HE40_2G;
11092 		break;
11093 	default:
11094 		down_mode = mode;
11095 		break;
11096 	}
11097 
11098 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
11099 		   "mac vdev start phymode %s downgrade to %s\n",
11100 		   ath12k_mac_phymode_str(mode),
11101 		   ath12k_mac_phymode_str(down_mode));
11102 
11103 	return down_mode;
11104 }
11105 
11106 static void
11107 ath12k_mac_mlo_get_vdev_args(struct ath12k_link_vif *arvif,
11108 			     struct wmi_ml_arg *ml_arg)
11109 {
11110 	struct ath12k_vif *ahvif = arvif->ahvif;
11111 	struct wmi_ml_partner_info *partner_info;
11112 	struct ieee80211_bss_conf *link_conf;
11113 	struct ath12k_link_vif *arvif_p;
11114 	unsigned long links;
11115 	u8 link_id;
11116 
11117 	lockdep_assert_wiphy(ahvif->ah->hw->wiphy);
11118 
11119 	if (!ath12k_mac_is_ml_arvif(arvif))
11120 		return;
11121 
11122 	if (hweight16(ahvif->vif->valid_links) > ATH12K_WMI_MLO_MAX_LINKS)
11123 		return;
11124 
11125 	ml_arg->enabled = true;
11126 
11127 	/* Driver always add a new link via VDEV START, FW takes
11128 	 * care of internally adding this link to existing
11129 	 * link vdevs which are advertised as partners below
11130 	 */
11131 	ml_arg->link_add = true;
11132 
11133 	ml_arg->assoc_link = arvif->is_sta_assoc_link;
11134 
11135 	partner_info = ml_arg->partner_info;
11136 
11137 	links = ahvif->links_map;
11138 	for_each_set_bit(link_id, &links, IEEE80211_MLD_MAX_NUM_LINKS) {
11139 		arvif_p = wiphy_dereference(ahvif->ah->hw->wiphy, ahvif->link[link_id]);
11140 
11141 		if (WARN_ON(!arvif_p))
11142 			continue;
11143 
11144 		if (arvif == arvif_p)
11145 			continue;
11146 
11147 		if (!arvif_p->is_started)
11148 			continue;
11149 
11150 		link_conf = wiphy_dereference(ahvif->ah->hw->wiphy,
11151 					      ahvif->vif->link_conf[arvif_p->link_id]);
11152 
11153 		if (!link_conf)
11154 			continue;
11155 
11156 		partner_info->vdev_id = arvif_p->vdev_id;
11157 		partner_info->hw_link_id = arvif_p->ar->pdev->hw_link_id;
11158 		ether_addr_copy(partner_info->addr, link_conf->addr);
11159 		ml_arg->num_partner_links++;
11160 		partner_info++;
11161 	}
11162 }
11163 
11164 static int
11165 ath12k_mac_vdev_start_restart(struct ath12k_link_vif *arvif,
11166 			      struct ieee80211_chanctx_conf *ctx,
11167 			      bool restart)
11168 {
11169 	struct ath12k *ar = arvif->ar;
11170 	struct ath12k_base *ab = ar->ab;
11171 	struct wmi_vdev_start_req_arg arg = {};
11172 	const struct cfg80211_chan_def *chandef = &ctx->def;
11173 	struct ieee80211_hw *hw = ath12k_ar_to_hw(ar);
11174 	struct ath12k_vif *ahvif = arvif->ahvif;
11175 	struct ieee80211_bss_conf *link_conf;
11176 	unsigned int dfs_cac_time;
11177 	int ret;
11178 
11179 	lockdep_assert_wiphy(hw->wiphy);
11180 
11181 	link_conf = ath12k_mac_get_link_bss_conf(arvif);
11182 	if (!link_conf) {
11183 		ath12k_warn(ar->ab, "unable to access bss link conf in vdev start for vif %pM link %u\n",
11184 			    ahvif->vif->addr, arvif->link_id);
11185 		return -ENOLINK;
11186 	}
11187 
11188 	reinit_completion(&ar->vdev_setup_done);
11189 
11190 	arg.vdev_id = arvif->vdev_id;
11191 	arg.dtim_period = arvif->dtim_period;
11192 	arg.bcn_intval = arvif->beacon_interval;
11193 	arg.punct_bitmap = ~arvif->punct_bitmap;
11194 
11195 	arg.freq = chandef->chan->center_freq;
11196 	arg.band_center_freq1 = chandef->center_freq1;
11197 	arg.band_center_freq2 = chandef->center_freq2;
11198 	arg.mode = ath12k_phymodes[chandef->chan->band][chandef->width];
11199 
11200 	arg.mode = ath12k_mac_check_down_grade_phy_mode(ar, arg.mode,
11201 							chandef->chan->band,
11202 							ahvif->vif->type);
11203 	arg.min_power = 0;
11204 	arg.max_power = chandef->chan->max_power;
11205 	arg.max_reg_power = chandef->chan->max_reg_power;
11206 	arg.max_antenna_gain = chandef->chan->max_antenna_gain;
11207 
11208 	arg.pref_tx_streams = ar->num_tx_chains;
11209 	arg.pref_rx_streams = ar->num_rx_chains;
11210 
11211 	arg.mbssid_flags = WMI_VDEV_MBSSID_FLAGS_NON_MBSSID_AP;
11212 	arg.mbssid_tx_vdev_id = 0;
11213 	if (test_bit(WMI_TLV_SERVICE_MBSS_PARAM_IN_VDEV_START_SUPPORT,
11214 		     ar->ab->wmi_ab.svc_map)) {
11215 		ret = ath12k_mac_setup_vdev_params_mbssid(arvif,
11216 							  &arg.mbssid_flags,
11217 							  &arg.mbssid_tx_vdev_id);
11218 		if (ret)
11219 			return ret;
11220 	}
11221 
11222 	if (ahvif->vdev_type == WMI_VDEV_TYPE_AP) {
11223 		arg.ssid = ahvif->u.ap.ssid;
11224 		arg.ssid_len = ahvif->u.ap.ssid_len;
11225 		arg.hidden_ssid = ahvif->u.ap.hidden_ssid;
11226 
11227 		/* For now allow DFS for AP mode */
11228 		arg.chan_radar = !!(chandef->chan->flags & IEEE80211_CHAN_RADAR);
11229 
11230 		arg.freq2_radar = ctx->radar_enabled;
11231 
11232 		arg.passive = arg.chan_radar;
11233 
11234 		spin_lock_bh(&ab->base_lock);
11235 		arg.regdomain = ar->ab->dfs_region;
11236 		spin_unlock_bh(&ab->base_lock);
11237 
11238 		/* TODO: Notify if secondary 80Mhz also needs radar detection */
11239 	}
11240 
11241 	arg.passive |= !!(chandef->chan->flags & IEEE80211_CHAN_NO_IR);
11242 
11243 	if (!restart)
11244 		ath12k_mac_mlo_get_vdev_args(arvif, &arg.ml);
11245 
11246 	ath12k_dbg(ab, ATH12K_DBG_MAC,
11247 		   "mac vdev %d start center_freq %d phymode %s punct_bitmap 0x%x\n",
11248 		   arg.vdev_id, arg.freq,
11249 		   ath12k_mac_phymode_str(arg.mode), arg.punct_bitmap);
11250 
11251 	ret = ath12k_wmi_vdev_start(ar, &arg, restart);
11252 	if (ret) {
11253 		ath12k_warn(ar->ab, "failed to %s WMI vdev %i\n",
11254 			    restart ? "restart" : "start", arg.vdev_id);
11255 		return ret;
11256 	}
11257 
11258 	ret = ath12k_mac_vdev_setup_sync(ar);
11259 	if (ret) {
11260 		ath12k_warn(ab, "failed to synchronize setup for vdev %i %s: %d\n",
11261 			    arg.vdev_id, restart ? "restart" : "start", ret);
11262 		return ret;
11263 	}
11264 
11265 	/* TODO: For now we only set TPC power here. However when
11266 	 * channel changes, say CSA, it should be updated again.
11267 	 */
11268 	if (ath12k_mac_supports_tpc(ar, ahvif, chandef)) {
11269 		ath12k_mac_fill_reg_tpc_info(ar, arvif, ctx);
11270 		ath12k_wmi_send_vdev_set_tpc_power(ar, arvif->vdev_id,
11271 						   &arvif->reg_tpc_info);
11272 	}
11273 
11274 	ar->num_started_vdevs++;
11275 	ath12k_dbg(ab, ATH12K_DBG_MAC,  "vdev %pM started, vdev_id %d\n",
11276 		   ahvif->vif->addr, arvif->vdev_id);
11277 
11278 	/* Enable CAC Running Flag in the driver by checking all sub-channel's DFS
11279 	 * state as NL80211_DFS_USABLE which indicates CAC needs to be
11280 	 * done before channel usage. This flag is used to drop rx packets.
11281 	 * during CAC.
11282 	 */
11283 	/* TODO: Set the flag for other interface types as required */
11284 	if (arvif->ahvif->vdev_type == WMI_VDEV_TYPE_AP && ctx->radar_enabled &&
11285 	    cfg80211_chandef_dfs_usable(hw->wiphy, chandef)) {
11286 		set_bit(ATH12K_FLAG_CAC_RUNNING, &ar->dev_flags);
11287 		dfs_cac_time = cfg80211_chandef_dfs_cac_time(hw->wiphy, chandef);
11288 
11289 		ath12k_dbg(ab, ATH12K_DBG_MAC,
11290 			   "CAC started dfs_cac_time %u center_freq %d center_freq1 %d for vdev %d\n",
11291 			   dfs_cac_time, arg.freq, arg.band_center_freq1, arg.vdev_id);
11292 	}
11293 
11294 	ret = ath12k_mac_set_txbf_conf(arvif);
11295 	if (ret)
11296 		ath12k_warn(ab, "failed to set txbf conf for vdev %d: %d\n",
11297 			    arvif->vdev_id, ret);
11298 
11299 	return 0;
11300 }
11301 
11302 static int ath12k_mac_vdev_start(struct ath12k_link_vif *arvif,
11303 				 struct ieee80211_chanctx_conf *ctx)
11304 {
11305 	return ath12k_mac_vdev_start_restart(arvif, ctx, false);
11306 }
11307 
11308 static int ath12k_mac_vdev_restart(struct ath12k_link_vif *arvif,
11309 				   struct ieee80211_chanctx_conf *ctx)
11310 {
11311 	return ath12k_mac_vdev_start_restart(arvif, ctx, true);
11312 }
11313 
11314 struct ath12k_mac_change_chanctx_arg {
11315 	struct ieee80211_chanctx_conf *ctx;
11316 	struct ieee80211_vif_chanctx_switch *vifs;
11317 	int n_vifs;
11318 	int next_vif;
11319 	struct ath12k *ar;
11320 };
11321 
11322 static void
11323 ath12k_mac_change_chanctx_cnt_iter(void *data, u8 *mac,
11324 				   struct ieee80211_vif *vif)
11325 {
11326 	struct ath12k_vif *ahvif = ath12k_vif_to_ahvif(vif);
11327 	struct ath12k_mac_change_chanctx_arg *arg = data;
11328 	struct ieee80211_bss_conf *link_conf;
11329 	struct ath12k_link_vif *arvif;
11330 	unsigned long links_map;
11331 	u8 link_id;
11332 
11333 	lockdep_assert_wiphy(ahvif->ah->hw->wiphy);
11334 
11335 	links_map = ahvif->links_map;
11336 	for_each_set_bit(link_id, &links_map, IEEE80211_MLD_MAX_NUM_LINKS) {
11337 		arvif = wiphy_dereference(ahvif->ah->hw->wiphy, ahvif->link[link_id]);
11338 		if (WARN_ON(!arvif))
11339 			continue;
11340 
11341 		if (!arvif->is_created || arvif->ar != arg->ar)
11342 			continue;
11343 
11344 		link_conf = wiphy_dereference(ahvif->ah->hw->wiphy,
11345 					      vif->link_conf[link_id]);
11346 		if (WARN_ON(!link_conf))
11347 			continue;
11348 
11349 		if (rcu_access_pointer(link_conf->chanctx_conf) != arg->ctx)
11350 			continue;
11351 
11352 		arg->n_vifs++;
11353 	}
11354 }
11355 
11356 static void
11357 ath12k_mac_change_chanctx_fill_iter(void *data, u8 *mac,
11358 				    struct ieee80211_vif *vif)
11359 {
11360 	struct ath12k_vif *ahvif = ath12k_vif_to_ahvif(vif);
11361 	struct ath12k_mac_change_chanctx_arg *arg = data;
11362 	struct ieee80211_bss_conf *link_conf;
11363 	struct ieee80211_chanctx_conf *ctx;
11364 	struct ath12k_link_vif *arvif;
11365 	unsigned long links_map;
11366 	u8 link_id;
11367 
11368 	lockdep_assert_wiphy(ahvif->ah->hw->wiphy);
11369 
11370 	links_map = ahvif->links_map;
11371 	for_each_set_bit(link_id, &links_map, IEEE80211_MLD_MAX_NUM_LINKS) {
11372 		arvif = wiphy_dereference(ahvif->ah->hw->wiphy, ahvif->link[link_id]);
11373 		if (WARN_ON(!arvif))
11374 			continue;
11375 
11376 		if (!arvif->is_created || arvif->ar != arg->ar)
11377 			continue;
11378 
11379 		link_conf = wiphy_dereference(ahvif->ah->hw->wiphy,
11380 					      vif->link_conf[arvif->link_id]);
11381 		if (WARN_ON(!link_conf))
11382 			continue;
11383 
11384 		ctx = rcu_access_pointer(link_conf->chanctx_conf);
11385 		if (ctx != arg->ctx)
11386 			continue;
11387 
11388 		if (WARN_ON(arg->next_vif == arg->n_vifs))
11389 			return;
11390 
11391 		arg->vifs[arg->next_vif].vif = vif;
11392 		arg->vifs[arg->next_vif].old_ctx = ctx;
11393 		arg->vifs[arg->next_vif].new_ctx = ctx;
11394 		arg->vifs[arg->next_vif].link_conf = link_conf;
11395 		arg->next_vif++;
11396 	}
11397 }
11398 
11399 static u32 ath12k_mac_nlwidth_to_wmiwidth(enum nl80211_chan_width width)
11400 {
11401 	switch (width) {
11402 	case NL80211_CHAN_WIDTH_20:
11403 		return WMI_CHAN_WIDTH_20;
11404 	case NL80211_CHAN_WIDTH_40:
11405 		return WMI_CHAN_WIDTH_40;
11406 	case NL80211_CHAN_WIDTH_80:
11407 		return WMI_CHAN_WIDTH_80;
11408 	case NL80211_CHAN_WIDTH_160:
11409 		return WMI_CHAN_WIDTH_160;
11410 	case NL80211_CHAN_WIDTH_80P80:
11411 		return WMI_CHAN_WIDTH_80P80;
11412 	case NL80211_CHAN_WIDTH_5:
11413 		return WMI_CHAN_WIDTH_5;
11414 	case NL80211_CHAN_WIDTH_10:
11415 		return WMI_CHAN_WIDTH_10;
11416 	case NL80211_CHAN_WIDTH_320:
11417 		return WMI_CHAN_WIDTH_320;
11418 	default:
11419 		WARN_ON(1);
11420 		return WMI_CHAN_WIDTH_20;
11421 	}
11422 }
11423 
11424 static int ath12k_mac_update_peer_puncturing_width(struct ath12k *ar,
11425 						   struct ath12k_link_vif *arvif,
11426 						   struct cfg80211_chan_def def)
11427 {
11428 	u32 param_id, param_value;
11429 	int ret;
11430 
11431 	if (arvif->ahvif->vdev_type != WMI_VDEV_TYPE_STA)
11432 		return 0;
11433 
11434 	param_id = WMI_PEER_CHWIDTH_PUNCTURE_20MHZ_BITMAP;
11435 	param_value = ath12k_mac_nlwidth_to_wmiwidth(def.width) |
11436 		u32_encode_bits((~def.punctured),
11437 				WMI_PEER_PUNCTURE_BITMAP);
11438 
11439 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
11440 		   "punctured bitmap %02x width %d vdev %d\n",
11441 		   def.punctured, def.width, arvif->vdev_id);
11442 
11443 	ret = ath12k_wmi_set_peer_param(ar, arvif->bssid,
11444 					arvif->vdev_id, param_id,
11445 					param_value);
11446 
11447 	return ret;
11448 }
11449 
11450 static void
11451 ath12k_mac_update_vif_chan(struct ath12k *ar,
11452 			   struct ieee80211_vif_chanctx_switch *vifs,
11453 			   int n_vifs)
11454 {
11455 	struct ath12k_incumbent_signal_interference *incumbent;
11456 	struct ath12k_wmi_vdev_up_params params = {};
11457 	struct ieee80211_bss_conf *link_conf;
11458 	struct cfg80211_chan_def *chandef;
11459 	struct ath12k_base *ab = ar->ab;
11460 	struct ath12k_link_vif *arvif;
11461 	struct ieee80211_vif *vif;
11462 	struct ath12k_vif *ahvif;
11463 	u8 link_id;
11464 	int ret;
11465 	int i;
11466 	bool monitor_vif = false;
11467 
11468 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
11469 
11470 	for (i = 0; i < n_vifs; i++) {
11471 		vif = vifs[i].vif;
11472 		ahvif = ath12k_vif_to_ahvif(vif);
11473 		link_conf = vifs[i].link_conf;
11474 		link_id = link_conf->link_id;
11475 		arvif = wiphy_dereference(ath12k_ar_to_hw(ar)->wiphy,
11476 					  ahvif->link[link_id]);
11477 
11478 		if (vif->type == NL80211_IFTYPE_MONITOR) {
11479 			monitor_vif = true;
11480 			continue;
11481 		}
11482 
11483 		ath12k_dbg(ab, ATH12K_DBG_MAC,
11484 			   "mac chanctx switch vdev_id %i freq %u->%u width %d->%d\n",
11485 			   arvif->vdev_id,
11486 			   vifs[i].old_ctx->def.chan->center_freq,
11487 			   vifs[i].new_ctx->def.chan->center_freq,
11488 			   vifs[i].old_ctx->def.width,
11489 			   vifs[i].new_ctx->def.width);
11490 
11491 		if (WARN_ON(!arvif->is_started))
11492 			continue;
11493 
11494 		arvif->punct_bitmap = vifs[i].new_ctx->def.punctured;
11495 
11496 		/* Firmware expect vdev_restart only if vdev is up.
11497 		 * If vdev is down then it expect vdev_stop->vdev_start.
11498 		 */
11499 		if (arvif->is_up) {
11500 			ret = ath12k_mac_vdev_restart(arvif, vifs[i].new_ctx);
11501 			if (ret) {
11502 				ath12k_warn(ab, "failed to restart vdev %d: %d\n",
11503 					    arvif->vdev_id, ret);
11504 				continue;
11505 			}
11506 		} else {
11507 			ret = ath12k_mac_vdev_stop(arvif);
11508 			if (ret) {
11509 				ath12k_warn(ab, "failed to stop vdev %d: %d\n",
11510 					    arvif->vdev_id, ret);
11511 				continue;
11512 			}
11513 
11514 			ret = ath12k_mac_vdev_start(arvif, vifs[i].new_ctx);
11515 			if (ret)
11516 				ath12k_warn(ab, "failed to start vdev %d: %d\n",
11517 					    arvif->vdev_id, ret);
11518 			continue;
11519 		}
11520 
11521 		ret = ath12k_mac_update_peer_puncturing_width(arvif->ar, arvif,
11522 							      vifs[i].new_ctx->def);
11523 		if (ret) {
11524 			ath12k_warn(ar->ab,
11525 				    "failed to update puncturing bitmap %02x and width %d: %d\n",
11526 				    vifs[i].new_ctx->def.punctured,
11527 				    vifs[i].new_ctx->def.width, ret);
11528 			continue;
11529 		}
11530 
11531 		/* Defer VDEV bring-up during CSA to avoid installing stale
11532 		 * beacon templates. The beacon content is updated only
11533 		 * after CSA finalize, so we mark CSA in progress and skip
11534 		 * VDEV_UP for now. It will be handled later in
11535 		 * bss_info_changed().
11536 		 */
11537 		if (link_conf->csa_active &&
11538 		    arvif->ahvif->vdev_type == WMI_VDEV_TYPE_AP) {
11539 			arvif->is_csa_in_progress = true;
11540 			continue;
11541 		}
11542 
11543 		ret = ath12k_mac_setup_bcn_tmpl(arvif);
11544 		if (ret)
11545 			ath12k_warn(ab, "failed to update bcn tmpl during csa: %d\n",
11546 				    ret);
11547 
11548 		memset(&params, 0, sizeof(params));
11549 		params.vdev_id = arvif->vdev_id;
11550 		params.aid = ahvif->aid;
11551 		params.bssid = arvif->bssid;
11552 		params.tx_bssid = ath12k_mac_get_tx_bssid(arvif);
11553 		if (params.tx_bssid) {
11554 			params.nontx_profile_idx = link_conf->bssid_index;
11555 			params.nontx_profile_cnt = 1 << link_conf->bssid_indicator;
11556 		}
11557 		ret = ath12k_wmi_vdev_up(arvif->ar, &params);
11558 		if (ret) {
11559 			ath12k_warn(ab, "failed to bring vdev up %d: %d\n",
11560 				    arvif->vdev_id, ret);
11561 			continue;
11562 		}
11563 	}
11564 
11565 	/* Restart the internal monitor vdev on new channel */
11566 	if (!monitor_vif && ar->monitor_vdev_created) {
11567 		if (!ath12k_mac_monitor_stop(ar))
11568 			ath12k_mac_monitor_start(ar);
11569 	}
11570 
11571 	incumbent = &ar->incumbent_signal_interference;
11572 	spin_lock_bh(&ar->data_lock);
11573 	if (incumbent->handling_in_progress) {
11574 		chandef = &vifs[0].new_ctx->def;
11575 		if (incumbent->chan_bw_interference_bitmap &
11576 		    ATH12K_WMI_DCS_SEG_PRI20) {
11577 			if (incumbent->center_freq !=
11578 			    chandef->chan->center_freq) {
11579 				incumbent->chan_bw_interference_bitmap = 0;
11580 				incumbent->handling_in_progress = false;
11581 				ath12k_dbg(ab, ATH12K_DBG_MAC,
11582 					   "incumbent signal interference chan switch completed\n");
11583 			} else {
11584 				ath12k_warn(ab,
11585 					    "incumbent signal interference chan switch not done, freq %u\n",
11586 					    incumbent->center_freq);
11587 			}
11588 		} else {
11589 			if (incumbent->center_freq !=
11590 			    chandef->chan->center_freq ||
11591 			    incumbent->width != chandef->width) {
11592 				incumbent->chan_bw_interference_bitmap = 0;
11593 				incumbent->handling_in_progress = false;
11594 				ath12k_dbg(ab, ATH12K_DBG_MAC,
11595 					   "Bandwidth/channel change due to incumbent signal interference completed\n");
11596 			} else {
11597 				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",
11598 					    incumbent->center_freq,
11599 					    chandef->chan->center_freq,
11600 					    incumbent->width,
11601 					    chandef->width);
11602 			}
11603 		}
11604 	}
11605 	spin_unlock_bh(&ar->data_lock);
11606 }
11607 
11608 static void
11609 ath12k_mac_update_active_vif_chan(struct ath12k *ar,
11610 				  struct ieee80211_chanctx_conf *ctx)
11611 {
11612 	struct ath12k_mac_change_chanctx_arg arg = { .ctx = ctx, .ar = ar };
11613 	struct ieee80211_hw *hw = ath12k_ar_to_hw(ar);
11614 
11615 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
11616 
11617 	ieee80211_iterate_active_interfaces_atomic(hw,
11618 						   IEEE80211_IFACE_ITER_NORMAL,
11619 						   ath12k_mac_change_chanctx_cnt_iter,
11620 						   &arg);
11621 	if (arg.n_vifs == 0)
11622 		return;
11623 
11624 	arg.vifs = kzalloc_objs(arg.vifs[0], arg.n_vifs);
11625 	if (!arg.vifs)
11626 		return;
11627 
11628 	ieee80211_iterate_active_interfaces_atomic(hw,
11629 						   IEEE80211_IFACE_ITER_NORMAL,
11630 						   ath12k_mac_change_chanctx_fill_iter,
11631 						   &arg);
11632 
11633 	ath12k_mac_update_vif_chan(ar, arg.vifs, arg.n_vifs);
11634 
11635 	kfree(arg.vifs);
11636 }
11637 
11638 void ath12k_mac_op_change_chanctx(struct ieee80211_hw *hw,
11639 				  struct ieee80211_chanctx_conf *ctx,
11640 				  u32 changed)
11641 {
11642 	struct ath12k *ar;
11643 	struct ath12k_base *ab;
11644 
11645 	lockdep_assert_wiphy(hw->wiphy);
11646 
11647 	ar = ath12k_get_ar_by_ctx(hw, ctx);
11648 	if (!ar)
11649 		return;
11650 
11651 	ab = ar->ab;
11652 
11653 	ath12k_dbg(ab, ATH12K_DBG_MAC,
11654 		   "mac chanctx change freq %u width %d ptr %p changed %x\n",
11655 		   ctx->def.chan->center_freq, ctx->def.width, ctx, changed);
11656 
11657 	/* This shouldn't really happen because channel switching should use
11658 	 * switch_vif_chanctx().
11659 	 */
11660 	if (WARN_ON(changed & IEEE80211_CHANCTX_CHANGE_CHANNEL))
11661 		return;
11662 
11663 	if (changed & IEEE80211_CHANCTX_CHANGE_WIDTH ||
11664 	    changed & IEEE80211_CHANCTX_CHANGE_RADAR ||
11665 	    changed & IEEE80211_CHANCTX_CHANGE_PUNCTURING)
11666 		ath12k_mac_update_active_vif_chan(ar, ctx);
11667 
11668 	/* TODO: Recalc radar detection */
11669 }
11670 EXPORT_SYMBOL(ath12k_mac_op_change_chanctx);
11671 
11672 static int ath12k_start_vdev_delay(struct ath12k *ar,
11673 				   struct ath12k_link_vif *arvif)
11674 {
11675 	struct ath12k_base *ab = ar->ab;
11676 	struct ath12k_vif *ahvif = arvif->ahvif;
11677 	struct ieee80211_vif *vif = ath12k_ahvif_to_vif(arvif->ahvif);
11678 	struct ieee80211_chanctx_conf *chanctx;
11679 	struct ieee80211_bss_conf *link_conf;
11680 	int ret;
11681 
11682 	if (WARN_ON(arvif->is_started))
11683 		return -EBUSY;
11684 
11685 	link_conf = ath12k_mac_get_link_bss_conf(arvif);
11686 	if (!link_conf) {
11687 		ath12k_warn(ab, "failed to get link conf for vdev %u\n", arvif->vdev_id);
11688 		return -EINVAL;
11689 	}
11690 
11691 	chanctx	= wiphy_dereference(ath12k_ar_to_hw(arvif->ar)->wiphy,
11692 				    link_conf->chanctx_conf);
11693 	ret = ath12k_mac_vdev_start(arvif, chanctx);
11694 	if (ret) {
11695 		ath12k_warn(ab, "failed to start vdev %i addr %pM on freq %d: %d\n",
11696 			    arvif->vdev_id, vif->addr,
11697 			    chanctx->def.chan->center_freq, ret);
11698 		return ret;
11699 	}
11700 
11701 	if (ahvif->vdev_type == WMI_VDEV_TYPE_MONITOR) {
11702 		ret = ath12k_monitor_vdev_up(ar, arvif->vdev_id);
11703 		if (ret) {
11704 			ath12k_warn(ab, "failed put monitor up: %d\n", ret);
11705 			return ret;
11706 		}
11707 	}
11708 
11709 	arvif->is_started = true;
11710 
11711 	/* TODO: Setup ps and cts/rts protection */
11712 	return 0;
11713 }
11714 
11715 static u8 ath12k_mac_get_num_pwr_levels(struct cfg80211_chan_def *chan_def)
11716 {
11717 	if (chan_def->chan->flags & IEEE80211_CHAN_PSD) {
11718 		switch (chan_def->width) {
11719 		case NL80211_CHAN_WIDTH_20:
11720 			return 1;
11721 		case NL80211_CHAN_WIDTH_40:
11722 			return 2;
11723 		case NL80211_CHAN_WIDTH_80:
11724 			return 4;
11725 		case NL80211_CHAN_WIDTH_160:
11726 			return 8;
11727 		case NL80211_CHAN_WIDTH_320:
11728 			return 16;
11729 		default:
11730 			return 1;
11731 		}
11732 	} else {
11733 		switch (chan_def->width) {
11734 		case NL80211_CHAN_WIDTH_20:
11735 			return 1;
11736 		case NL80211_CHAN_WIDTH_40:
11737 			return 2;
11738 		case NL80211_CHAN_WIDTH_80:
11739 			return 3;
11740 		case NL80211_CHAN_WIDTH_160:
11741 			return 4;
11742 		case NL80211_CHAN_WIDTH_320:
11743 			return 5;
11744 		default:
11745 			return 1;
11746 		}
11747 	}
11748 }
11749 
11750 static u16 ath12k_mac_get_6ghz_start_frequency(struct cfg80211_chan_def *chan_def)
11751 {
11752 	u16 diff_seq;
11753 
11754 	/* It is to get the lowest channel number's center frequency of the chan.
11755 	 * For example,
11756 	 * bandwidth=40 MHz, center frequency is 5965, lowest channel is 1
11757 	 * with center frequency 5955, its diff is 5965 - 5955 = 10.
11758 	 * bandwidth=80 MHz, center frequency is 5985, lowest channel is 1
11759 	 * with center frequency 5955, its diff is 5985 - 5955 = 30.
11760 	 * bandwidth=160 MHz, center frequency is 6025, lowest channel is 1
11761 	 * with center frequency 5955, its diff is 6025 - 5955 = 70.
11762 	 * bandwidth=320 MHz, center frequency is 6105, lowest channel is 1
11763 	 * with center frequency 5955, its diff is 6105 - 5955 = 70.
11764 	 */
11765 	switch (chan_def->width) {
11766 	case NL80211_CHAN_WIDTH_320:
11767 		diff_seq = 150;
11768 		break;
11769 	case NL80211_CHAN_WIDTH_160:
11770 		diff_seq = 70;
11771 		break;
11772 	case NL80211_CHAN_WIDTH_80:
11773 		diff_seq = 30;
11774 		break;
11775 	case NL80211_CHAN_WIDTH_40:
11776 		diff_seq = 10;
11777 		break;
11778 	default:
11779 		diff_seq = 0;
11780 	}
11781 
11782 	return chan_def->center_freq1 - diff_seq;
11783 }
11784 
11785 static u16 ath12k_mac_get_seg_freq(struct cfg80211_chan_def *chan_def,
11786 				   u16 start_seq, u8 seq)
11787 {
11788 	u16 seg_seq;
11789 
11790 	/* It is to get the center frequency of the specific bandwidth.
11791 	 * start_seq means the lowest channel number's center frequency.
11792 	 * seq 0/1/2/3 means 20 MHz/40 MHz/80 MHz/160 MHz.
11793 	 * For example,
11794 	 * lowest channel is 1, its center frequency 5955,
11795 	 * center frequency is 5955 when bandwidth=20 MHz, its diff is 5955 - 5955 = 0.
11796 	 * lowest channel is 1, its center frequency 5955,
11797 	 * center frequency is 5965 when bandwidth=40 MHz, its diff is 5965 - 5955 = 10.
11798 	 * lowest channel is 1, its center frequency 5955,
11799 	 * center frequency is 5985 when bandwidth=80 MHz, its diff is 5985 - 5955 = 30.
11800 	 * lowest channel is 1, its center frequency 5955,
11801 	 * center frequency is 6025 when bandwidth=160 MHz, its diff is 6025 - 5955 = 70.
11802 	 */
11803 	seg_seq = 10 * (BIT(seq) - 1);
11804 	return seg_seq + start_seq;
11805 }
11806 
11807 static void ath12k_mac_get_psd_channel(struct ath12k *ar,
11808 				       u16 step_freq,
11809 				       u16 *start_freq,
11810 				       u16 *center_freq,
11811 				       u8 i,
11812 				       struct ieee80211_channel **temp_chan,
11813 				       s8 *tx_power)
11814 {
11815 	/* It is to get the center frequency for each 20 MHz.
11816 	 * For example, if the chan is 160 MHz and center frequency is 6025,
11817 	 * then it include 8 channels, they are 1/5/9/13/17/21/25/29,
11818 	 * channel number 1's center frequency is 5955, it is parameter start_freq.
11819 	 * parameter i is the step of the 8 channels. i is 0~7 for the 8 channels.
11820 	 * the channel 1/5/9/13/17/21/25/29 maps i=0/1/2/3/4/5/6/7,
11821 	 * and maps its center frequency is 5955/5975/5995/6015/6035/6055/6075/6095,
11822 	 * the gap is 20 for each channel, parameter step_freq means the gap.
11823 	 * after get the center frequency of each channel, it is easy to find the
11824 	 * struct ieee80211_channel of it and get the max_reg_power.
11825 	 */
11826 	*center_freq = *start_freq + i * step_freq;
11827 	*temp_chan = ieee80211_get_channel(ar->ah->hw->wiphy, *center_freq);
11828 	*tx_power = (*temp_chan)->max_reg_power;
11829 }
11830 
11831 static void ath12k_mac_get_eirp_power(struct ath12k *ar,
11832 				      u16 *start_freq,
11833 				      u16 *center_freq,
11834 				      u8 i,
11835 				      struct ieee80211_channel **temp_chan,
11836 				      struct cfg80211_chan_def *def,
11837 				      s8 *tx_power)
11838 {
11839 	/* It is to get the center frequency for 20 MHz/40 MHz/80 MHz/
11840 	 * 160 MHz bandwidth, and then plus 10 to the center frequency,
11841 	 * it is the center frequency of a channel number.
11842 	 * For example, when configured channel number is 1.
11843 	 * center frequency is 5965 when bandwidth=40 MHz, after plus 10, it is 5975,
11844 	 * then it is channel number 5.
11845 	 * center frequency is 5985 when bandwidth=80 MHz, after plus 10, it is 5995,
11846 	 * then it is channel number 9.
11847 	 * center frequency is 6025 when bandwidth=160 MHz, after plus 10, it is 6035,
11848 	 * then it is channel number 17.
11849 	 * after get the center frequency of each channel, it is easy to find the
11850 	 * struct ieee80211_channel of it and get the max_reg_power.
11851 	 */
11852 	*center_freq = ath12k_mac_get_seg_freq(def, *start_freq, i);
11853 
11854 	/* For the 20 MHz, its center frequency is same with same channel */
11855 	if (i != 0)
11856 		*center_freq += 10;
11857 
11858 	*temp_chan = ieee80211_get_channel(ar->ah->hw->wiphy, *center_freq);
11859 	*tx_power = (*temp_chan)->max_reg_power;
11860 }
11861 
11862 void ath12k_mac_fill_reg_tpc_info(struct ath12k *ar,
11863 				  struct ath12k_link_vif *arvif,
11864 				  struct ieee80211_chanctx_conf *ctx)
11865 {
11866 	struct ath12k_base *ab = ar->ab;
11867 	struct ath12k_reg_tpc_power_info *reg_tpc_info = &arvif->reg_tpc_info;
11868 	struct ieee80211_bss_conf *bss_conf = ath12k_mac_get_link_bss_conf(arvif);
11869 	struct ieee80211_channel *chan, *temp_chan;
11870 	u8 pwr_lvl_idx, num_pwr_levels, pwr_reduction;
11871 	bool is_psd_power = false, is_tpe_present = false;
11872 	s8 max_tx_power[ATH12K_NUM_PWR_LEVELS], psd_power, tx_power;
11873 	s8 eirp_power = 0;
11874 	struct ath12k_vif *ahvif = arvif->ahvif;
11875 	u16 start_freq, center_freq;
11876 	u8 reg_6ghz_power_mode;
11877 
11878 	chan = ctx->def.chan;
11879 	start_freq = ath12k_mac_get_6ghz_start_frequency(&ctx->def);
11880 	pwr_reduction = bss_conf->pwr_reduction;
11881 
11882 	if (arvif->reg_tpc_info.num_pwr_levels) {
11883 		is_tpe_present = true;
11884 		num_pwr_levels = arvif->reg_tpc_info.num_pwr_levels;
11885 	} else {
11886 		num_pwr_levels = ath12k_mac_get_num_pwr_levels(&ctx->def);
11887 	}
11888 
11889 	for (pwr_lvl_idx = 0; pwr_lvl_idx < num_pwr_levels; pwr_lvl_idx++) {
11890 		/* STA received TPE IE*/
11891 		if (is_tpe_present) {
11892 			/* local power is PSD power*/
11893 			if (chan->flags & IEEE80211_CHAN_PSD) {
11894 				/* Connecting AP is psd power */
11895 				if (reg_tpc_info->is_psd_power) {
11896 					is_psd_power = true;
11897 					ath12k_mac_get_psd_channel(ar, 20,
11898 								   &start_freq,
11899 								   &center_freq,
11900 								   pwr_lvl_idx,
11901 								   &temp_chan,
11902 								   &tx_power);
11903 					psd_power = temp_chan->psd;
11904 					eirp_power = tx_power;
11905 					max_tx_power[pwr_lvl_idx] =
11906 						min_t(s8,
11907 						      psd_power,
11908 						      reg_tpc_info->tpe[pwr_lvl_idx]);
11909 				/* Connecting AP is not psd power */
11910 				} else {
11911 					ath12k_mac_get_eirp_power(ar,
11912 								  &start_freq,
11913 								  &center_freq,
11914 								  pwr_lvl_idx,
11915 								  &temp_chan,
11916 								  &ctx->def,
11917 								  &tx_power);
11918 					psd_power = temp_chan->psd;
11919 					/* convert psd power to EIRP power based
11920 					 * on channel width
11921 					 */
11922 					tx_power =
11923 						min_t(s8, tx_power,
11924 						      psd_power + 13 + pwr_lvl_idx * 3);
11925 					max_tx_power[pwr_lvl_idx] =
11926 						min_t(s8,
11927 						      tx_power,
11928 						      reg_tpc_info->tpe[pwr_lvl_idx]);
11929 				}
11930 			/* local power is not PSD power */
11931 			} else {
11932 				/* Connecting AP is psd power */
11933 				if (reg_tpc_info->is_psd_power) {
11934 					is_psd_power = true;
11935 					ath12k_mac_get_psd_channel(ar, 20,
11936 								   &start_freq,
11937 								   &center_freq,
11938 								   pwr_lvl_idx,
11939 								   &temp_chan,
11940 								   &tx_power);
11941 					eirp_power = tx_power;
11942 					max_tx_power[pwr_lvl_idx] =
11943 						reg_tpc_info->tpe[pwr_lvl_idx];
11944 				/* Connecting AP is not psd power */
11945 				} else {
11946 					ath12k_mac_get_eirp_power(ar,
11947 								  &start_freq,
11948 								  &center_freq,
11949 								  pwr_lvl_idx,
11950 								  &temp_chan,
11951 								  &ctx->def,
11952 								  &tx_power);
11953 					max_tx_power[pwr_lvl_idx] =
11954 						min_t(s8,
11955 						      tx_power,
11956 						      reg_tpc_info->tpe[pwr_lvl_idx]);
11957 				}
11958 			}
11959 		/* STA not received TPE IE */
11960 		} else {
11961 			/* local power is PSD power*/
11962 			if (chan->flags & IEEE80211_CHAN_PSD) {
11963 				is_psd_power = true;
11964 				ath12k_mac_get_psd_channel(ar, 20,
11965 							   &start_freq,
11966 							   &center_freq,
11967 							   pwr_lvl_idx,
11968 							   &temp_chan,
11969 							   &tx_power);
11970 				psd_power = temp_chan->psd;
11971 				eirp_power = tx_power;
11972 				max_tx_power[pwr_lvl_idx] = psd_power;
11973 			} else {
11974 				ath12k_mac_get_eirp_power(ar,
11975 							  &start_freq,
11976 							  &center_freq,
11977 							  pwr_lvl_idx,
11978 							  &temp_chan,
11979 							  &ctx->def,
11980 							  &tx_power);
11981 				max_tx_power[pwr_lvl_idx] = tx_power;
11982 			}
11983 		}
11984 
11985 		if (is_psd_power) {
11986 			/* If AP local power constraint is present */
11987 			if (pwr_reduction)
11988 				eirp_power = eirp_power - pwr_reduction;
11989 
11990 			/* If firmware updated max tx power is non zero, then take
11991 			 * the min of firmware updated ap tx power
11992 			 * and max power derived from above mentioned parameters.
11993 			 */
11994 			ath12k_dbg(ab, ATH12K_DBG_MAC,
11995 				   "eirp power : %d firmware report power : %d\n",
11996 				   eirp_power, ar->max_allowed_tx_power);
11997 			/* Firmware reports lower max_allowed_tx_power during vdev
11998 			 * start response. In case of 6 GHz, firmware is not aware
11999 			 * of EIRP power unless driver sets EIRP power through WMI
12000 			 * TPC command. So radio which does not support idle power
12001 			 * save can set maximum calculated EIRP power directly to
12002 			 * firmware through TPC command without min comparison with
12003 			 * vdev start response's max_allowed_tx_power.
12004 			 */
12005 			if (ar->max_allowed_tx_power && ab->hw_params->idle_ps)
12006 				eirp_power = min_t(s8,
12007 						   eirp_power,
12008 						   ar->max_allowed_tx_power);
12009 		} else {
12010 			/* If AP local power constraint is present */
12011 			if (pwr_reduction)
12012 				max_tx_power[pwr_lvl_idx] =
12013 					max_tx_power[pwr_lvl_idx] - pwr_reduction;
12014 			/* If firmware updated max tx power is non zero, then take
12015 			 * the min of firmware updated ap tx power
12016 			 * and max power derived from above mentioned parameters.
12017 			 */
12018 			if (ar->max_allowed_tx_power && ab->hw_params->idle_ps)
12019 				max_tx_power[pwr_lvl_idx] =
12020 					min_t(s8,
12021 					      max_tx_power[pwr_lvl_idx],
12022 					      ar->max_allowed_tx_power);
12023 		}
12024 		reg_tpc_info->chan_power_info[pwr_lvl_idx].chan_cfreq = center_freq;
12025 		reg_tpc_info->chan_power_info[pwr_lvl_idx].tx_power =
12026 			max_tx_power[pwr_lvl_idx];
12027 	}
12028 
12029 	reg_tpc_info->num_pwr_levels = num_pwr_levels;
12030 	reg_tpc_info->is_psd_power = is_psd_power;
12031 	reg_tpc_info->eirp_power = eirp_power;
12032 	if (ahvif->vdev_type == WMI_VDEV_TYPE_STA)
12033 		reg_6ghz_power_mode = bss_conf->power_type;
12034 	else
12035 		/* For now, LPI is the only supported AP power mode */
12036 		reg_6ghz_power_mode = IEEE80211_REG_LPI_AP;
12037 
12038 	reg_tpc_info->ap_power_type =
12039 		ath12k_reg_ap_pwr_convert(reg_6ghz_power_mode);
12040 }
12041 
12042 static void ath12k_mac_parse_tx_pwr_env(struct ath12k *ar,
12043 					struct ath12k_link_vif *arvif)
12044 {
12045 	struct ieee80211_bss_conf *bss_conf = ath12k_mac_get_link_bss_conf(arvif);
12046 	struct ath12k_reg_tpc_power_info *tpc_info = &arvif->reg_tpc_info;
12047 	struct ieee80211_parsed_tpe_eirp *local_non_psd, *reg_non_psd;
12048 	struct ieee80211_parsed_tpe_psd *local_psd, *reg_psd;
12049 	struct ieee80211_parsed_tpe *tpe = &bss_conf->tpe;
12050 	enum wmi_reg_6g_client_type client_type;
12051 	struct ath12k_reg_info *reg_info;
12052 	struct ath12k_base *ab = ar->ab;
12053 	bool psd_valid, non_psd_valid;
12054 	int i;
12055 
12056 	reg_info = ab->reg_info[ar->pdev_idx];
12057 	client_type = reg_info->client_type;
12058 
12059 	local_psd = &tpe->psd_local[client_type];
12060 	reg_psd = &tpe->psd_reg_client[client_type];
12061 	local_non_psd = &tpe->max_local[client_type];
12062 	reg_non_psd = &tpe->max_reg_client[client_type];
12063 
12064 	psd_valid = local_psd->valid | reg_psd->valid;
12065 	non_psd_valid = local_non_psd->valid | reg_non_psd->valid;
12066 
12067 	if (!psd_valid && !non_psd_valid) {
12068 		ath12k_warn(ab,
12069 			    "no transmit power envelope match client power type %d\n",
12070 			    client_type);
12071 		return;
12072 	}
12073 
12074 	if (psd_valid) {
12075 		tpc_info->is_psd_power = true;
12076 
12077 		tpc_info->num_pwr_levels = max(local_psd->count,
12078 					       reg_psd->count);
12079 		tpc_info->num_pwr_levels =
12080 				min3(tpc_info->num_pwr_levels,
12081 				     IEEE80211_TPE_PSD_ENTRIES_320MHZ,
12082 				     ATH12K_NUM_PWR_LEVELS);
12083 
12084 		for (i = 0; i < tpc_info->num_pwr_levels; i++) {
12085 			tpc_info->tpe[i] = min(local_psd->power[i],
12086 					       reg_psd->power[i]) / 2;
12087 			ath12k_dbg(ab, ATH12K_DBG_MAC,
12088 				   "TPE PSD power[%d] : %d\n",
12089 				   i, tpc_info->tpe[i]);
12090 		}
12091 	} else {
12092 		tpc_info->is_psd_power = false;
12093 		tpc_info->eirp_power = 0;
12094 
12095 		tpc_info->num_pwr_levels = max(local_non_psd->count,
12096 					       reg_non_psd->count);
12097 		tpc_info->num_pwr_levels =
12098 				min3(tpc_info->num_pwr_levels,
12099 				     IEEE80211_TPE_EIRP_ENTRIES_320MHZ,
12100 				     ATH12K_NUM_PWR_LEVELS);
12101 
12102 		for (i = 0; i < tpc_info->num_pwr_levels; i++) {
12103 			tpc_info->tpe[i] = min(local_non_psd->power[i],
12104 					       reg_non_psd->power[i]) / 2;
12105 			ath12k_dbg(ab, ATH12K_DBG_MAC,
12106 				   "non PSD power[%d] : %d\n",
12107 				   i, tpc_info->tpe[i]);
12108 		}
12109 	}
12110 }
12111 
12112 int
12113 ath12k_mac_op_assign_vif_chanctx(struct ieee80211_hw *hw,
12114 				 struct ieee80211_vif *vif,
12115 				 struct ieee80211_bss_conf *link_conf,
12116 				 struct ieee80211_chanctx_conf *ctx)
12117 {
12118 	struct ath12k_hw *ah = ath12k_hw_to_ah(hw);
12119 	struct ath12k *ar;
12120 	struct ath12k_base *ab;
12121 	struct ath12k_vif *ahvif = ath12k_vif_to_ahvif(vif);
12122 	u8 link_id = link_conf->link_id;
12123 	struct ath12k_link_vif *arvif;
12124 	int ret;
12125 
12126 	lockdep_assert_wiphy(hw->wiphy);
12127 
12128 	/* For multi radio wiphy, the vdev was not created during add_interface
12129 	 * create now since we have a channel ctx now to assign to a specific ar/fw
12130 	 */
12131 	arvif = ath12k_mac_assign_link_vif(ah, vif, link_id);
12132 	if (!arvif) {
12133 		WARN_ON(1);
12134 		return -ENOMEM;
12135 	}
12136 
12137 	ar = ath12k_mac_assign_vif_to_vdev(hw, arvif, ctx);
12138 	if (!ar) {
12139 		ath12k_hw_warn(ah, "failed to assign chanctx for vif %pM link id %u link vif is already started",
12140 			       vif->addr, link_id);
12141 		return -EINVAL;
12142 	}
12143 
12144 	ab = ar->ab;
12145 
12146 	ath12k_dbg(ab, ATH12K_DBG_MAC,
12147 		   "mac chanctx assign ptr %p vdev_id %i\n",
12148 		   ctx, arvif->vdev_id);
12149 
12150 	if (ath12k_wmi_supports_6ghz_cc_ext(ar) &&
12151 	    ctx->def.chan->band == NL80211_BAND_6GHZ &&
12152 	    ahvif->vdev_type == WMI_VDEV_TYPE_STA)
12153 		ath12k_mac_parse_tx_pwr_env(ar, arvif);
12154 
12155 	arvif->punct_bitmap = ctx->def.punctured;
12156 
12157 	/* for some targets bss peer must be created before vdev_start */
12158 	if (ab->hw_params->vdev_start_delay &&
12159 	    ahvif->vdev_type != WMI_VDEV_TYPE_AP &&
12160 	    ahvif->vdev_type != WMI_VDEV_TYPE_MONITOR &&
12161 	    !ath12k_dp_link_peer_exist_by_vdev_id(ath12k_ab_to_dp(ab), arvif->vdev_id)) {
12162 		ret = 0;
12163 		goto out;
12164 	}
12165 
12166 	if (WARN_ON(arvif->is_started)) {
12167 		ret = -EBUSY;
12168 		goto out;
12169 	}
12170 
12171 	if (ahvif->vdev_type == WMI_VDEV_TYPE_MONITOR) {
12172 		ret = ath12k_mac_monitor_start(ar);
12173 		if (ret) {
12174 			ath12k_mac_monitor_vdev_delete(ar);
12175 			goto out;
12176 		}
12177 
12178 		arvif->is_started = true;
12179 		goto out;
12180 	}
12181 
12182 	ret = ath12k_mac_vdev_start(arvif, ctx);
12183 	if (ret) {
12184 		ath12k_warn(ab, "failed to start vdev %i addr %pM on freq %d: %d\n",
12185 			    arvif->vdev_id, vif->addr,
12186 			    ctx->def.chan->center_freq, ret);
12187 		goto out;
12188 	}
12189 
12190 	arvif->is_started = true;
12191 
12192 	/* TODO: Setup ps and cts/rts protection */
12193 
12194 out:
12195 	return ret;
12196 }
12197 EXPORT_SYMBOL(ath12k_mac_op_assign_vif_chanctx);
12198 
12199 void
12200 ath12k_mac_op_unassign_vif_chanctx(struct ieee80211_hw *hw,
12201 				   struct ieee80211_vif *vif,
12202 				   struct ieee80211_bss_conf *link_conf,
12203 				   struct ieee80211_chanctx_conf *ctx)
12204 {
12205 	struct ath12k *ar;
12206 	struct ath12k_base *ab;
12207 	struct ath12k_vif *ahvif = ath12k_vif_to_ahvif(vif);
12208 	struct ath12k_link_vif *arvif;
12209 	u8 link_id = link_conf->link_id;
12210 	int ret;
12211 
12212 	lockdep_assert_wiphy(hw->wiphy);
12213 
12214 	arvif = wiphy_dereference(hw->wiphy, ahvif->link[link_id]);
12215 
12216 	/* The vif is expected to be attached to an ar's VDEV.
12217 	 * We leave the vif/vdev in this function as is
12218 	 * and not delete the vdev symmetric to assign_vif_chanctx()
12219 	 * the VDEV will be deleted and unassigned either during
12220 	 * remove_interface() or when there is a change in channel
12221 	 * that moves the vif to a new ar
12222 	 */
12223 	if (!arvif || !arvif->is_created)
12224 		return;
12225 
12226 	ar = arvif->ar;
12227 	ab = ar->ab;
12228 
12229 	ath12k_dbg(ab, ATH12K_DBG_MAC,
12230 		   "mac chanctx unassign ptr %p vdev_id %i\n",
12231 		   ctx, arvif->vdev_id);
12232 
12233 	WARN_ON(!arvif->is_started);
12234 
12235 	if (ahvif->vdev_type == WMI_VDEV_TYPE_MONITOR) {
12236 		ret = ath12k_mac_monitor_stop(ar);
12237 		if (ret)
12238 			return;
12239 
12240 		arvif->is_started = false;
12241 	}
12242 
12243 	if (ahvif->vdev_type != WMI_VDEV_TYPE_STA &&
12244 	    ahvif->vdev_type != WMI_VDEV_TYPE_MONITOR) {
12245 		ath12k_bss_disassoc(ar, arvif);
12246 		ret = ath12k_mac_vdev_stop(arvif);
12247 		if (ret)
12248 			ath12k_warn(ab, "failed to stop vdev %i: %d\n",
12249 				    arvif->vdev_id, ret);
12250 	}
12251 	arvif->is_started = false;
12252 
12253 	if (test_bit(WMI_TLV_SERVICE_11D_OFFLOAD, ab->wmi_ab.svc_map) &&
12254 	    ahvif->vdev_type == WMI_VDEV_TYPE_STA &&
12255 	    ahvif->vdev_subtype == WMI_VDEV_SUBTYPE_NONE &&
12256 	    ar->state_11d != ATH12K_11D_PREPARING) {
12257 		reinit_completion(&ar->completed_11d_scan);
12258 		ar->state_11d = ATH12K_11D_PREPARING;
12259 	}
12260 
12261 	if (ar->scan.arvif == arvif && ar->scan.state == ATH12K_SCAN_RUNNING) {
12262 		ath12k_scan_abort(ar);
12263 		ar->scan.arvif = NULL;
12264 	}
12265 }
12266 EXPORT_SYMBOL(ath12k_mac_op_unassign_vif_chanctx);
12267 
12268 int
12269 ath12k_mac_op_switch_vif_chanctx(struct ieee80211_hw *hw,
12270 				 struct ieee80211_vif_chanctx_switch *vifs,
12271 				 int n_vifs,
12272 				 enum ieee80211_chanctx_switch_mode mode)
12273 {
12274 	struct ath12k *ar;
12275 
12276 	lockdep_assert_wiphy(hw->wiphy);
12277 
12278 	ar = ath12k_get_ar_by_ctx(hw, vifs->old_ctx);
12279 	if (!ar)
12280 		return -EINVAL;
12281 
12282 	/* Switching channels across radio is not allowed */
12283 	if (ar != ath12k_get_ar_by_ctx(hw, vifs->new_ctx))
12284 		return -EINVAL;
12285 
12286 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
12287 		   "mac chanctx switch n_vifs %d mode %d\n",
12288 		   n_vifs, mode);
12289 	ath12k_mac_update_vif_chan(ar, vifs, n_vifs);
12290 
12291 	return 0;
12292 }
12293 EXPORT_SYMBOL(ath12k_mac_op_switch_vif_chanctx);
12294 
12295 static int
12296 ath12k_set_vdev_param_to_all_vifs(struct ath12k *ar, int param, u32 value)
12297 {
12298 	struct ath12k_link_vif *arvif;
12299 	int ret = 0;
12300 
12301 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
12302 
12303 	list_for_each_entry(arvif, &ar->arvifs, list) {
12304 		ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "setting mac vdev %d param %d value %d\n",
12305 			   param, arvif->vdev_id, value);
12306 
12307 		ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
12308 						    param, value);
12309 		if (ret) {
12310 			ath12k_warn(ar->ab, "failed to set param %d for vdev %d: %d\n",
12311 				    param, arvif->vdev_id, ret);
12312 			break;
12313 		}
12314 	}
12315 
12316 	return ret;
12317 }
12318 
12319 /* mac80211 stores device specific RTS/Fragmentation threshold value,
12320  * this is set interface specific to firmware from ath12k driver
12321  */
12322 int ath12k_mac_op_set_rts_threshold(struct ieee80211_hw *hw,
12323 				    int radio_idx, u32 value)
12324 {
12325 	struct ath12k_hw *ah = ath12k_hw_to_ah(hw);
12326 	struct wiphy *wiphy = hw->wiphy;
12327 	struct ath12k *ar;
12328 	int param_id = WMI_VDEV_PARAM_RTS_THRESHOLD;
12329 	int ret = 0, ret_err, i;
12330 
12331 	lockdep_assert_wiphy(hw->wiphy);
12332 
12333 	if (radio_idx >= wiphy->n_radio || radio_idx < -1)
12334 		return -EINVAL;
12335 
12336 	if (radio_idx != -1) {
12337 		/* Update RTS threshold in specified radio */
12338 		ar = ath12k_ah_to_ar(ah, radio_idx);
12339 		ret = ath12k_set_vdev_param_to_all_vifs(ar, param_id, value);
12340 		if (ret) {
12341 			ath12k_warn(ar->ab,
12342 				    "failed to set RTS config for all vdevs of pdev %d",
12343 				    ar->pdev->pdev_id);
12344 			return ret;
12345 		}
12346 
12347 		ar->rts_threshold = value;
12348 		return 0;
12349 	}
12350 
12351 	/* Radio_index passed is -1, so set RTS threshold for all radios. */
12352 	for_each_ar(ah, ar, i) {
12353 		ret = ath12k_set_vdev_param_to_all_vifs(ar, param_id, value);
12354 		if (ret) {
12355 			ath12k_warn(ar->ab, "failed to set RTS config for all vdevs of pdev %d",
12356 				    ar->pdev->pdev_id);
12357 			break;
12358 		}
12359 	}
12360 	if (!ret) {
12361 		/* Setting new RTS threshold for vdevs of all radios passed, so update
12362 		 * the RTS threshold value for all radios
12363 		 */
12364 		for_each_ar(ah, ar, i)
12365 			ar->rts_threshold = value;
12366 		return 0;
12367 	}
12368 
12369 	/* RTS threshold config failed, revert to the previous RTS threshold */
12370 	for (i = i - 1; i >= 0; i--) {
12371 		ar = ath12k_ah_to_ar(ah, i);
12372 		ret_err = ath12k_set_vdev_param_to_all_vifs(ar, param_id,
12373 							    ar->rts_threshold);
12374 		if (ret_err)
12375 			ath12k_warn(ar->ab,
12376 				    "failed to restore RTS threshold for all vdevs of pdev %d",
12377 				    ar->pdev->pdev_id);
12378 	}
12379 
12380 	return ret;
12381 }
12382 EXPORT_SYMBOL(ath12k_mac_op_set_rts_threshold);
12383 
12384 int ath12k_mac_op_set_frag_threshold(struct ieee80211_hw *hw,
12385 				     int radio_idx, u32 value)
12386 {
12387 	/* Even though there's a WMI vdev param for fragmentation threshold no
12388 	 * known firmware actually implements it. Moreover it is not possible to
12389 	 * rely frame fragmentation to mac80211 because firmware clears the
12390 	 * "more fragments" bit in frame control making it impossible for remote
12391 	 * devices to reassemble frames.
12392 	 *
12393 	 * Hence implement a dummy callback just to say fragmentation isn't
12394 	 * supported. This effectively prevents mac80211 from doing frame
12395 	 * fragmentation in software.
12396 	 */
12397 
12398 	lockdep_assert_wiphy(hw->wiphy);
12399 
12400 	return -EOPNOTSUPP;
12401 }
12402 EXPORT_SYMBOL(ath12k_mac_op_set_frag_threshold);
12403 
12404 static int ath12k_mac_flush(struct ath12k *ar)
12405 {
12406 	long time_left;
12407 	int ret = 0;
12408 
12409 	time_left = wait_event_timeout(ar->dp.tx_empty_waitq,
12410 				       (atomic_read(&ar->dp.num_tx_pending) == 0),
12411 				       ATH12K_FLUSH_TIMEOUT);
12412 	if (time_left == 0) {
12413 		ath12k_warn(ar->ab,
12414 			    "failed to flush transmit queue, data pkts pending %d\n",
12415 			    atomic_read(&ar->dp.num_tx_pending));
12416 		ret = -ETIMEDOUT;
12417 	}
12418 
12419 	time_left = wait_event_timeout(ar->txmgmt_empty_waitq,
12420 				       (atomic_read(&ar->num_pending_mgmt_tx) == 0),
12421 				       ATH12K_FLUSH_TIMEOUT);
12422 	if (time_left == 0) {
12423 		ath12k_warn(ar->ab,
12424 			    "failed to flush mgmt transmit queue, mgmt pkts pending %d\n",
12425 			    atomic_read(&ar->num_pending_mgmt_tx));
12426 		ret = -ETIMEDOUT;
12427 	}
12428 
12429 	return ret;
12430 }
12431 
12432 int ath12k_mac_wait_tx_complete(struct ath12k *ar)
12433 {
12434 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
12435 
12436 	ath12k_mac_drain_tx(ar);
12437 	return ath12k_mac_flush(ar);
12438 }
12439 
12440 void ath12k_mac_op_flush(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
12441 			 u32 queues, bool drop)
12442 {
12443 	struct ath12k_hw *ah = ath12k_hw_to_ah(hw);
12444 	struct ath12k_link_vif *arvif;
12445 	struct ath12k_vif *ahvif;
12446 	unsigned long links;
12447 	struct ath12k *ar;
12448 	u8 link_id;
12449 	int i;
12450 
12451 	lockdep_assert_wiphy(hw->wiphy);
12452 
12453 	if (drop)
12454 		return;
12455 
12456 	for_each_ar(ah, ar, i)
12457 		wiphy_work_flush(hw->wiphy, &ar->wmi_mgmt_tx_work);
12458 
12459 	/* vif can be NULL when flush() is considered for hw */
12460 	if (!vif) {
12461 		for_each_ar(ah, ar, i)
12462 			ath12k_mac_flush(ar);
12463 		return;
12464 	}
12465 
12466 	ahvif = ath12k_vif_to_ahvif(vif);
12467 	links = ahvif->links_map;
12468 	for_each_set_bit(link_id, &links, IEEE80211_MLD_MAX_NUM_LINKS) {
12469 		arvif = wiphy_dereference(hw->wiphy, ahvif->link[link_id]);
12470 		if (!(arvif && arvif->ar))
12471 			continue;
12472 
12473 		ath12k_mac_flush(arvif->ar);
12474 	}
12475 }
12476 EXPORT_SYMBOL(ath12k_mac_op_flush);
12477 
12478 static int
12479 ath12k_mac_bitrate_mask_num_ht_rates(struct ath12k *ar,
12480 				     enum nl80211_band band,
12481 				     const struct cfg80211_bitrate_mask *mask)
12482 {
12483 	int num_rates = 0;
12484 	int i;
12485 
12486 	for (i = 0; i < ARRAY_SIZE(mask->control[band].ht_mcs); i++)
12487 		num_rates += hweight16(mask->control[band].ht_mcs[i]);
12488 
12489 	return num_rates;
12490 }
12491 
12492 static bool
12493 ath12k_mac_has_single_legacy_rate(struct ath12k *ar,
12494 				  enum nl80211_band band,
12495 				  const struct cfg80211_bitrate_mask *mask)
12496 {
12497 	int num_rates = 0;
12498 
12499 	num_rates = hweight32(mask->control[band].legacy);
12500 
12501 	if (ath12k_mac_bitrate_mask_num_ht_rates(ar, band, mask))
12502 		return false;
12503 
12504 	if (ath12k_mac_bitrate_mask_num_vht_rates(ar, band, mask))
12505 		return false;
12506 
12507 	if (ath12k_mac_bitrate_mask_num_he_rates(ar, band, mask))
12508 		return false;
12509 
12510 	if (ath12k_mac_bitrate_mask_num_eht_rates(ar, band, mask))
12511 		return false;
12512 
12513 	return num_rates == 1;
12514 }
12515 
12516 static __le16
12517 ath12k_mac_get_tx_mcs_map(const struct ieee80211_sta_he_cap *he_cap)
12518 {
12519 	if (he_cap->he_cap_elem.phy_cap_info[0] &
12520 	    IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_160MHZ_IN_5G)
12521 		return he_cap->he_mcs_nss_supp.tx_mcs_160;
12522 
12523 	return he_cap->he_mcs_nss_supp.tx_mcs_80;
12524 }
12525 
12526 static bool
12527 ath12k_mac_bitrate_mask_get_single_nss(struct ath12k *ar,
12528 				       struct ieee80211_vif *vif,
12529 				       enum nl80211_band band,
12530 				       const struct cfg80211_bitrate_mask *mask,
12531 				       int *nss)
12532 {
12533 	struct ieee80211_supported_band *sband = &ar->mac.sbands[band];
12534 	u16 vht_mcs_map = le16_to_cpu(sband->vht_cap.vht_mcs.tx_mcs_map);
12535 	const struct ieee80211_sband_iftype_data *data;
12536 	const struct ieee80211_sta_he_cap *he_cap;
12537 	u16 he_mcs_map = 0;
12538 	u16 eht_mcs_map = 0;
12539 	u8 ht_nss_mask = 0;
12540 	u8 vht_nss_mask = 0;
12541 	u8 he_nss_mask = 0;
12542 	u8 eht_nss_mask = 0;
12543 	u8 mcs_nss_len;
12544 	int i;
12545 
12546 	/* No need to consider legacy here. Basic rates are always present
12547 	 * in bitrate mask
12548 	 */
12549 
12550 	for (i = 0; i < ARRAY_SIZE(mask->control[band].ht_mcs); i++) {
12551 		if (mask->control[band].ht_mcs[i] == 0)
12552 			continue;
12553 		else if (mask->control[band].ht_mcs[i] ==
12554 			 sband->ht_cap.mcs.rx_mask[i])
12555 			ht_nss_mask |= BIT(i);
12556 		else
12557 			return false;
12558 	}
12559 
12560 	for (i = 0; i < ARRAY_SIZE(mask->control[band].vht_mcs); i++) {
12561 		if (mask->control[band].vht_mcs[i] == 0)
12562 			continue;
12563 		else if (mask->control[band].vht_mcs[i] ==
12564 			 ath12k_mac_get_max_vht_mcs_map(vht_mcs_map, i))
12565 			vht_nss_mask |= BIT(i);
12566 		else
12567 			return false;
12568 	}
12569 
12570 	he_cap = ieee80211_get_he_iftype_cap_vif(sband, vif);
12571 	if (!he_cap)
12572 		return false;
12573 
12574 	he_mcs_map = le16_to_cpu(ath12k_mac_get_tx_mcs_map(he_cap));
12575 
12576 	for (i = 0; i < ARRAY_SIZE(mask->control[band].he_mcs); i++) {
12577 		if (mask->control[band].he_mcs[i] == 0)
12578 			continue;
12579 
12580 		if (mask->control[band].he_mcs[i] ==
12581 		    ath12k_mac_get_max_he_mcs_map(he_mcs_map, i))
12582 			he_nss_mask |= BIT(i);
12583 		else
12584 			return false;
12585 	}
12586 
12587 	data = ieee80211_get_sband_iftype_data(sband, vif->type);
12588 
12589 	mcs_nss_len = ieee80211_eht_mcs_nss_size(&data->he_cap.he_cap_elem,
12590 						 &data->eht_cap.eht_cap_elem,
12591 						 false);
12592 	if (mcs_nss_len == 4) {
12593 		/* 20 MHz only STA case */
12594 		const struct ieee80211_eht_mcs_nss_supp_20mhz_only *eht_mcs_nss =
12595 			&data->eht_cap.eht_mcs_nss_supp.only_20mhz;
12596 		if (eht_mcs_nss->rx_tx_mcs13_max_nss)
12597 			eht_mcs_map = 0x1fff;
12598 		else if (eht_mcs_nss->rx_tx_mcs11_max_nss)
12599 			eht_mcs_map = 0x07ff;
12600 		else if (eht_mcs_nss->rx_tx_mcs9_max_nss)
12601 			eht_mcs_map = 0x01ff;
12602 		else
12603 			eht_mcs_map = 0x007f;
12604 	} else {
12605 		const struct ieee80211_eht_mcs_nss_supp_bw *eht_mcs_nss;
12606 
12607 		switch (mcs_nss_len) {
12608 		case 9:
12609 			eht_mcs_nss = &data->eht_cap.eht_mcs_nss_supp.bw._320;
12610 			break;
12611 		case 6:
12612 			eht_mcs_nss = &data->eht_cap.eht_mcs_nss_supp.bw._160;
12613 			break;
12614 		case 3:
12615 			eht_mcs_nss = &data->eht_cap.eht_mcs_nss_supp.bw._80;
12616 			break;
12617 		default:
12618 			return false;
12619 		}
12620 
12621 		if (eht_mcs_nss->rx_tx_mcs13_max_nss)
12622 			eht_mcs_map = 0x1fff;
12623 		else if (eht_mcs_nss->rx_tx_mcs11_max_nss)
12624 			eht_mcs_map = 0x7ff;
12625 		else
12626 			eht_mcs_map = 0x1ff;
12627 	}
12628 
12629 	for (i = 0; i < ARRAY_SIZE(mask->control[band].eht_mcs); i++) {
12630 		if (mask->control[band].eht_mcs[i] == 0)
12631 			continue;
12632 
12633 		if (mask->control[band].eht_mcs[i] < eht_mcs_map)
12634 			eht_nss_mask |= BIT(i);
12635 		else
12636 			return false;
12637 	}
12638 
12639 	if (ht_nss_mask != vht_nss_mask || ht_nss_mask != he_nss_mask ||
12640 	    ht_nss_mask != eht_nss_mask)
12641 		return false;
12642 
12643 	if (ht_nss_mask == 0)
12644 		return false;
12645 
12646 	if (BIT(fls(ht_nss_mask)) - 1 != ht_nss_mask)
12647 		return false;
12648 
12649 	*nss = fls(ht_nss_mask);
12650 
12651 	return true;
12652 }
12653 
12654 static int
12655 ath12k_mac_get_single_legacy_rate(struct ath12k *ar,
12656 				  enum nl80211_band band,
12657 				  const struct cfg80211_bitrate_mask *mask,
12658 				  u32 *rate, u8 *nss)
12659 {
12660 	int rate_idx;
12661 	u16 bitrate;
12662 	u8 preamble;
12663 	u8 hw_rate;
12664 
12665 	if (hweight32(mask->control[band].legacy) != 1)
12666 		return -EINVAL;
12667 
12668 	rate_idx = ffs(mask->control[band].legacy) - 1;
12669 
12670 	if (band == NL80211_BAND_5GHZ || band == NL80211_BAND_6GHZ)
12671 		rate_idx += ATH12K_MAC_FIRST_OFDM_RATE_IDX;
12672 
12673 	hw_rate = ath12k_legacy_rates[rate_idx].hw_value;
12674 	bitrate = ath12k_legacy_rates[rate_idx].bitrate;
12675 
12676 	if (ath12k_mac_bitrate_is_cck(bitrate))
12677 		preamble = WMI_RATE_PREAMBLE_CCK;
12678 	else
12679 		preamble = WMI_RATE_PREAMBLE_OFDM;
12680 
12681 	*nss = 1;
12682 	*rate = ATH12K_HW_RATE_CODE(hw_rate, 0, preamble);
12683 
12684 	return 0;
12685 }
12686 
12687 static int
12688 ath12k_mac_set_fixed_rate_gi_ltf(struct ath12k_link_vif *arvif, u8 gi, u8 ltf,
12689 				 u32 param)
12690 {
12691 	struct ath12k *ar = arvif->ar;
12692 	int ret;
12693 
12694 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
12695 
12696 	/* 0.8 = 0, 1.6 = 2 and 3.2 = 3. */
12697 	if (gi && gi != 0xFF)
12698 		gi += 1;
12699 
12700 	ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
12701 					    WMI_VDEV_PARAM_SGI, gi);
12702 	if (ret) {
12703 		ath12k_warn(ar->ab, "failed to set GI:%d, error:%d\n",
12704 			    gi, ret);
12705 		return ret;
12706 	}
12707 
12708 	if (param == WMI_VDEV_PARAM_HE_LTF) {
12709 		/* HE values start from 1 */
12710 		if (ltf != 0xFF)
12711 			ltf += 1;
12712 	} else {
12713 		/* EHT values start from 5 */
12714 		if (ltf != 0xFF)
12715 			ltf += 4;
12716 	}
12717 
12718 	ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
12719 					    param, ltf);
12720 	if (ret) {
12721 		ath12k_warn(ar->ab, "failed to set LTF:%d, error:%d\n",
12722 			    ltf, ret);
12723 		return ret;
12724 	}
12725 	return 0;
12726 }
12727 
12728 static int
12729 ath12k_mac_set_auto_rate_gi_ltf(struct ath12k_link_vif *arvif, u16 gi, u8 ltf)
12730 {
12731 	struct ath12k *ar = arvif->ar;
12732 	int ret;
12733 	u32 ar_gi_ltf;
12734 
12735 	if (gi != 0xFF) {
12736 		switch (gi) {
12737 		case ATH12K_RATE_INFO_GI_0_8:
12738 			gi = WMI_AUTORATE_800NS_GI;
12739 			break;
12740 		case ATH12K_RATE_INFO_GI_1_6:
12741 			gi = WMI_AUTORATE_1600NS_GI;
12742 			break;
12743 		case ATH12K_RATE_INFO_GI_3_2:
12744 			gi = WMI_AUTORATE_3200NS_GI;
12745 			break;
12746 		default:
12747 			ath12k_warn(ar->ab, "Invalid GI\n");
12748 			return -EINVAL;
12749 		}
12750 	}
12751 
12752 	if (ltf != 0xFF) {
12753 		switch (ltf) {
12754 		case ATH12K_RATE_INFO_1XLTF:
12755 			ltf = WMI_AUTORATE_LTF_1X;
12756 			break;
12757 		case ATH12K_RATE_INFO_2XLTF:
12758 			ltf = WMI_AUTORATE_LTF_2X;
12759 			break;
12760 		case ATH12K_RATE_INFO_4XLTF:
12761 			ltf = WMI_AUTORATE_LTF_4X;
12762 			break;
12763 		default:
12764 			ath12k_warn(ar->ab, "Invalid LTF\n");
12765 			return -EINVAL;
12766 		}
12767 	}
12768 
12769 	ar_gi_ltf = gi | ltf;
12770 
12771 	ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
12772 					    WMI_VDEV_PARAM_AUTORATE_MISC_CFG,
12773 					    ar_gi_ltf);
12774 	if (ret) {
12775 		ath12k_warn(ar->ab,
12776 			    "failed to set autorate GI:%u, LTF:%u params, error:%d\n",
12777 			    gi, ltf, ret);
12778 		return ret;
12779 	}
12780 
12781 	return 0;
12782 }
12783 
12784 static u32 ath12k_mac_nlgi_to_wmigi(enum nl80211_txrate_gi gi)
12785 {
12786 	switch (gi) {
12787 	case NL80211_TXRATE_DEFAULT_GI:
12788 		return WMI_GI_400_NS;
12789 	case NL80211_TXRATE_FORCE_LGI:
12790 		return WMI_GI_800_NS;
12791 	default:
12792 		return WMI_GI_400_NS;
12793 	}
12794 }
12795 
12796 static int ath12k_mac_set_rate_params(struct ath12k_link_vif *arvif,
12797 				      u32 rate, u8 nss, u8 sgi, u8 ldpc,
12798 				      u8 he_gi, u8 he_ltf, bool he_fixed_rate,
12799 				      u8 eht_gi, u8 eht_ltf,
12800 				      bool eht_fixed_rate)
12801 {
12802 	struct ieee80211_bss_conf *link_conf;
12803 	struct ath12k *ar = arvif->ar;
12804 	bool he_support, eht_support, gi_ltf_set = false;
12805 	u32 vdev_param;
12806 	u32 param_value;
12807 	int ret;
12808 
12809 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
12810 
12811 	link_conf = ath12k_mac_get_link_bss_conf(arvif);
12812 	if (!link_conf)
12813 		return -EINVAL;
12814 
12815 	he_support = link_conf->he_support;
12816 	eht_support = link_conf->eht_support;
12817 
12818 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
12819 		   "mac set rate params vdev %i rate 0x%02x nss 0x%02x sgi 0x%02x ldpc 0x%02x\n",
12820 		   arvif->vdev_id, rate, nss, sgi, ldpc);
12821 
12822 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
12823 		   "he_gi 0x%02x he_ltf 0x%02x he_fixed_rate %d\n", he_gi,
12824 		   he_ltf, he_fixed_rate);
12825 
12826 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
12827 		   "eht_gi 0x%02x eht_ltf 0x%02x eht_fixed_rate %d\n",
12828 		   eht_gi, eht_ltf, eht_fixed_rate);
12829 
12830 	if (!he_support && !eht_support) {
12831 		vdev_param = WMI_VDEV_PARAM_FIXED_RATE;
12832 		ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
12833 						    vdev_param, rate);
12834 		if (ret) {
12835 			ath12k_warn(ar->ab, "failed to set fixed rate param 0x%02x: %d\n",
12836 				    rate, ret);
12837 			return ret;
12838 		}
12839 	}
12840 
12841 	vdev_param = WMI_VDEV_PARAM_NSS;
12842 
12843 	ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
12844 					    vdev_param, nss);
12845 	if (ret) {
12846 		ath12k_warn(ar->ab, "failed to set nss param %d: %d\n",
12847 			    nss, ret);
12848 		return ret;
12849 	}
12850 
12851 	ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
12852 					    WMI_VDEV_PARAM_LDPC, ldpc);
12853 	if (ret) {
12854 		ath12k_warn(ar->ab, "failed to set ldpc param %d: %d\n",
12855 			    ldpc, ret);
12856 		return ret;
12857 	}
12858 
12859 	if (eht_support) {
12860 		if (eht_fixed_rate)
12861 			ret = ath12k_mac_set_fixed_rate_gi_ltf(arvif, eht_gi, eht_ltf,
12862 							       WMI_VDEV_PARAM_EHT_LTF);
12863 		else
12864 			ret = ath12k_mac_set_auto_rate_gi_ltf(arvif, eht_gi, eht_ltf);
12865 
12866 		if (ret) {
12867 			ath12k_warn(ar->ab,
12868 				    "failed to set EHT LTF/GI params %d/%d: %d\n",
12869 				    eht_gi, eht_ltf, ret);
12870 			return ret;
12871 		}
12872 		gi_ltf_set = true;
12873 	}
12874 
12875 	if (he_support) {
12876 		if (he_fixed_rate)
12877 			ret = ath12k_mac_set_fixed_rate_gi_ltf(arvif, he_gi, he_ltf,
12878 							       WMI_VDEV_PARAM_HE_LTF);
12879 		else
12880 			ret = ath12k_mac_set_auto_rate_gi_ltf(arvif, he_gi, he_ltf);
12881 		if (ret)
12882 			return ret;
12883 		gi_ltf_set = true;
12884 	}
12885 
12886 	if (!gi_ltf_set) {
12887 		vdev_param = WMI_VDEV_PARAM_SGI;
12888 		param_value = ath12k_mac_nlgi_to_wmigi(sgi);
12889 		ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
12890 						    vdev_param, param_value);
12891 		if (ret) {
12892 			ath12k_warn(ar->ab, "failed to set sgi param %d: %d\n",
12893 				    sgi, ret);
12894 			return ret;
12895 		}
12896 	}
12897 
12898 	return 0;
12899 }
12900 
12901 static bool
12902 ath12k_mac_vht_mcs_range_present(struct ath12k *ar,
12903 				 enum nl80211_band band,
12904 				 const struct cfg80211_bitrate_mask *mask)
12905 {
12906 	int i;
12907 	u16 vht_mcs;
12908 
12909 	for (i = 0; i < NL80211_VHT_NSS_MAX; i++) {
12910 		vht_mcs = mask->control[band].vht_mcs[i];
12911 
12912 		switch (vht_mcs) {
12913 		case 0:
12914 		case BIT(8) - 1:
12915 		case BIT(9) - 1:
12916 		case BIT(10) - 1:
12917 			break;
12918 		default:
12919 			return false;
12920 		}
12921 	}
12922 
12923 	return true;
12924 }
12925 
12926 static bool
12927 ath12k_mac_he_mcs_range_present(struct ath12k *ar,
12928 				enum nl80211_band band,
12929 				const struct cfg80211_bitrate_mask *mask)
12930 {
12931 	int i;
12932 	u16 he_mcs;
12933 
12934 	for (i = 0; i < NL80211_HE_NSS_MAX; i++) {
12935 		he_mcs = mask->control[band].he_mcs[i];
12936 
12937 		switch (he_mcs) {
12938 		case 0:
12939 		case BIT(8) - 1:
12940 		case BIT(10) - 1:
12941 		case BIT(12) - 1:
12942 			break;
12943 		default:
12944 			return false;
12945 		}
12946 	}
12947 
12948 	return true;
12949 }
12950 
12951 static bool
12952 ath12k_mac_eht_mcs_range_present(struct ath12k *ar,
12953 				 enum nl80211_band band,
12954 				 const struct cfg80211_bitrate_mask *mask)
12955 {
12956 	u16 eht_mcs;
12957 	int i;
12958 
12959 	for (i = 0; i < NL80211_EHT_NSS_MAX; i++) {
12960 		eht_mcs = mask->control[band].eht_mcs[i];
12961 
12962 		switch (eht_mcs) {
12963 		case 0:
12964 		case BIT(8) - 1:
12965 		case BIT(10) - 1:
12966 		case BIT(12) - 1:
12967 		case BIT(14) - 1:
12968 			break;
12969 		case BIT(15) - 1:
12970 		case BIT(16) - 1:
12971 		case BIT(16) - BIT(14) - 1:
12972 			if (i != 0)
12973 				return false;
12974 			break;
12975 		default:
12976 			return false;
12977 		}
12978 	}
12979 
12980 	return true;
12981 }
12982 
12983 static void ath12k_mac_set_bitrate_mask_iter(void *data,
12984 					     struct ieee80211_sta *sta)
12985 {
12986 	struct ath12k_link_vif *arvif = data;
12987 	struct ath12k_sta *ahsta = ath12k_sta_to_ahsta(sta);
12988 	struct ath12k_link_sta *arsta;
12989 	struct ath12k *ar = arvif->ar;
12990 
12991 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
12992 
12993 	arsta = wiphy_dereference(ath12k_ar_to_hw(ar)->wiphy,
12994 				  ahsta->link[arvif->link_id]);
12995 	if (!arsta || arsta->arvif != arvif)
12996 		return;
12997 
12998 	spin_lock_bh(&ar->data_lock);
12999 	arsta->changed |= IEEE80211_RC_SUPP_RATES_CHANGED;
13000 	spin_unlock_bh(&ar->data_lock);
13001 
13002 	wiphy_work_queue(ath12k_ar_to_hw(ar)->wiphy, &arsta->update_wk);
13003 }
13004 
13005 static void ath12k_mac_disable_peer_fixed_rate(void *data,
13006 					       struct ieee80211_sta *sta)
13007 {
13008 	struct ath12k_link_vif *arvif = data;
13009 	struct ath12k_sta *ahsta = ath12k_sta_to_ahsta(sta);
13010 	struct ath12k_link_sta *arsta;
13011 	struct ath12k *ar = arvif->ar;
13012 	int ret;
13013 
13014 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
13015 
13016 	arsta = wiphy_dereference(ath12k_ar_to_hw(ar)->wiphy,
13017 				  ahsta->link[arvif->link_id]);
13018 
13019 	if (!arsta || arsta->arvif != arvif)
13020 		return;
13021 
13022 	ret = ath12k_wmi_set_peer_param(ar, arsta->addr,
13023 					arvif->vdev_id,
13024 					WMI_PEER_PARAM_FIXED_RATE,
13025 					WMI_FIXED_RATE_NONE);
13026 	if (ret)
13027 		ath12k_warn(ar->ab,
13028 			    "failed to disable peer fixed rate for STA %pM ret %d\n",
13029 			    arsta->addr, ret);
13030 }
13031 
13032 static bool
13033 ath12k_mac_validate_fixed_rate_settings(struct ath12k *ar, enum nl80211_band band,
13034 					const struct cfg80211_bitrate_mask *mask,
13035 					unsigned int link_id)
13036 {
13037 	bool eht_fixed_rate = false, he_fixed_rate = false, vht_fixed_rate = false;
13038 	const u16 *vht_mcs_mask, *he_mcs_mask, *eht_mcs_mask;
13039 	struct ieee80211_link_sta *link_sta;
13040 	struct ath12k_dp_link_peer *peer, *tmp;
13041 	u8 vht_nss, he_nss, eht_nss;
13042 	int ret = true;
13043 	struct ath12k_base *ab = ar->ab;
13044 	struct ath12k_dp *dp = ath12k_ab_to_dp(ab);
13045 
13046 	vht_mcs_mask = mask->control[band].vht_mcs;
13047 	he_mcs_mask = mask->control[band].he_mcs;
13048 	eht_mcs_mask = mask->control[band].eht_mcs;
13049 
13050 	if (ath12k_mac_bitrate_mask_num_vht_rates(ar, band, mask) == 1)
13051 		vht_fixed_rate = true;
13052 
13053 	if (ath12k_mac_bitrate_mask_num_he_rates(ar, band, mask) == 1)
13054 		he_fixed_rate = true;
13055 
13056 	if (ath12k_mac_bitrate_mask_num_eht_rates(ar, band, mask) == 1)
13057 		eht_fixed_rate = true;
13058 
13059 	if (!vht_fixed_rate && !he_fixed_rate && !eht_fixed_rate)
13060 		return true;
13061 
13062 	vht_nss = ath12k_mac_max_vht_nss(vht_mcs_mask);
13063 	he_nss =  ath12k_mac_max_he_nss(he_mcs_mask);
13064 	eht_nss = ath12k_mac_max_eht_nss(eht_mcs_mask);
13065 
13066 	rcu_read_lock();
13067 	spin_lock_bh(&dp->dp_lock);
13068 	list_for_each_entry_safe(peer, tmp, &dp->peers, list) {
13069 		if (peer->sta) {
13070 			link_sta = rcu_dereference(peer->sta->link[link_id]);
13071 			if (!link_sta) {
13072 				ret = false;
13073 				goto exit;
13074 			}
13075 
13076 			if (vht_fixed_rate && (!link_sta->vht_cap.vht_supported ||
13077 					       link_sta->rx_nss < vht_nss)) {
13078 				ret = false;
13079 				goto exit;
13080 			}
13081 			if (he_fixed_rate && (!link_sta->he_cap.has_he ||
13082 					      link_sta->rx_nss < he_nss)) {
13083 				ret = false;
13084 				goto exit;
13085 			}
13086 			if (eht_fixed_rate && (!link_sta->eht_cap.has_eht ||
13087 					       link_sta->rx_nss < eht_nss)) {
13088 				ret = false;
13089 				goto exit;
13090 			}
13091 		}
13092 	}
13093 exit:
13094 	spin_unlock_bh(&dp->dp_lock);
13095 	rcu_read_unlock();
13096 	return ret;
13097 }
13098 
13099 int
13100 ath12k_mac_op_set_bitrate_mask(struct ieee80211_hw *hw,
13101 			       struct ieee80211_vif *vif,
13102 			       const struct cfg80211_bitrate_mask *mask)
13103 {
13104 	struct ath12k_vif *ahvif = ath12k_vif_to_ahvif(vif);
13105 	struct ath12k_link_vif *arvif;
13106 	struct cfg80211_chan_def def;
13107 	struct ath12k *ar;
13108 	enum nl80211_band band;
13109 	const u8 *ht_mcs_mask;
13110 	const u16 *vht_mcs_mask;
13111 	const u16 *he_mcs_mask;
13112 	const u16 *eht_mcs_mask;
13113 	u8 he_ltf = 0;
13114 	u8 he_gi = 0;
13115 	u8 eht_ltf = 0, eht_gi = 0;
13116 	u32 rate;
13117 	u8 nss, mac_nss;
13118 	u8 sgi;
13119 	u8 ldpc;
13120 	int single_nss;
13121 	int ret;
13122 	int num_rates;
13123 	bool he_fixed_rate = false;
13124 	bool eht_fixed_rate = false;
13125 
13126 	lockdep_assert_wiphy(hw->wiphy);
13127 
13128 	arvif = &ahvif->deflink;
13129 
13130 	ar = arvif->ar;
13131 	if (ath12k_mac_vif_link_chan(vif, arvif->link_id, &def)) {
13132 		ret = -EPERM;
13133 		goto out;
13134 	}
13135 
13136 	band = def.chan->band;
13137 	ht_mcs_mask = mask->control[band].ht_mcs;
13138 	vht_mcs_mask = mask->control[band].vht_mcs;
13139 	he_mcs_mask = mask->control[band].he_mcs;
13140 	eht_mcs_mask = mask->control[band].eht_mcs;
13141 	ldpc = !!(ar->ht_cap_info & WMI_HT_CAP_LDPC);
13142 
13143 	sgi = mask->control[band].gi;
13144 	if (sgi == NL80211_TXRATE_FORCE_SGI) {
13145 		ret = -EINVAL;
13146 		goto out;
13147 	}
13148 
13149 	he_gi = mask->control[band].he_gi;
13150 	he_ltf = mask->control[band].he_ltf;
13151 
13152 	eht_gi = mask->control[band].eht_gi;
13153 	eht_ltf = mask->control[band].eht_ltf;
13154 
13155 	/* mac80211 doesn't support sending a fixed HT/VHT MCS alone, rather it
13156 	 * requires passing at least one of used basic rates along with them.
13157 	 * Fixed rate setting across different preambles(legacy, HT, VHT) is
13158 	 * not supported by the FW. Hence use of FIXED_RATE vdev param is not
13159 	 * suitable for setting single HT/VHT rates.
13160 	 * But, there could be a single basic rate passed from userspace which
13161 	 * can be done through the FIXED_RATE param.
13162 	 */
13163 	if (ath12k_mac_has_single_legacy_rate(ar, band, mask)) {
13164 		ret = ath12k_mac_get_single_legacy_rate(ar, band, mask, &rate,
13165 							&nss);
13166 		if (ret) {
13167 			ath12k_warn(ar->ab, "failed to get single legacy rate for vdev %i: %d\n",
13168 				    arvif->vdev_id, ret);
13169 			goto out;
13170 		}
13171 
13172 		ieee80211_iterate_stations_mtx(hw,
13173 					       ath12k_mac_disable_peer_fixed_rate,
13174 					       arvif);
13175 	} else if (ath12k_mac_bitrate_mask_get_single_nss(ar, vif, band, mask,
13176 							  &single_nss)) {
13177 		rate = WMI_FIXED_RATE_NONE;
13178 		nss = single_nss;
13179 		arvif->bitrate_mask = *mask;
13180 
13181 		ieee80211_iterate_stations_atomic(hw,
13182 						  ath12k_mac_set_bitrate_mask_iter,
13183 						  arvif);
13184 	} else {
13185 		rate = WMI_FIXED_RATE_NONE;
13186 
13187 		if (!ath12k_mac_validate_fixed_rate_settings(ar, band,
13188 							     mask, arvif->link_id))
13189 			ath12k_warn(ar->ab,
13190 				    "failed to update fixed rate settings due to mcs/nss incompatibility\n");
13191 
13192 		mac_nss = max(max3(ath12k_mac_max_ht_nss(ht_mcs_mask),
13193 				   ath12k_mac_max_vht_nss(vht_mcs_mask),
13194 				   ath12k_mac_max_he_nss(he_mcs_mask)),
13195 			       ath12k_mac_max_eht_nss(eht_mcs_mask));
13196 		nss = min_t(u32, ar->num_tx_chains, mac_nss);
13197 
13198 		/* If multiple rates across different preambles are given
13199 		 * we can reconfigure this info with all peers using PEER_ASSOC
13200 		 * command with the below exception cases.
13201 		 * - Single VHT Rate : peer_assoc command accommodates only MCS
13202 		 * range values i.e 0-7, 0-8, 0-9 for VHT. Though mac80211
13203 		 * mandates passing basic rates along with HT/VHT rates, FW
13204 		 * doesn't allow switching from VHT to Legacy. Hence instead of
13205 		 * setting legacy and VHT rates using RATEMASK_CMD vdev cmd,
13206 		 * we could set this VHT rate as peer fixed rate param, which
13207 		 * will override FIXED rate and FW rate control algorithm.
13208 		 * If single VHT rate is passed along with HT rates, we select
13209 		 * the VHT rate as fixed rate for vht peers.
13210 		 * - Multiple VHT Rates : When Multiple VHT rates are given,this
13211 		 * can be set using RATEMASK CMD which uses FW rate-ctl alg.
13212 		 * TODO: Setting multiple VHT MCS and replacing peer_assoc with
13213 		 * RATEMASK_CMDID can cover all use cases of setting rates
13214 		 * across multiple preambles and rates within same type.
13215 		 * But requires more validation of the command at this point.
13216 		 */
13217 
13218 		num_rates = ath12k_mac_bitrate_mask_num_vht_rates(ar, band,
13219 								  mask);
13220 
13221 		if (!ath12k_mac_vht_mcs_range_present(ar, band, mask) &&
13222 		    num_rates > 1) {
13223 			/* TODO: Handle multiple VHT MCS values setting using
13224 			 * RATEMASK CMD
13225 			 */
13226 			ath12k_warn(ar->ab,
13227 				    "Setting more than one MCS Value in bitrate mask not supported\n");
13228 			ret = -EINVAL;
13229 			goto out;
13230 		}
13231 
13232 		num_rates = ath12k_mac_bitrate_mask_num_he_rates(ar, band, mask);
13233 		if (num_rates == 1)
13234 			he_fixed_rate = true;
13235 
13236 		if (!ath12k_mac_he_mcs_range_present(ar, band, mask) &&
13237 		    num_rates > 1) {
13238 			ath12k_warn(ar->ab,
13239 				    "Setting more than one HE MCS Value in bitrate mask not supported\n");
13240 			ret = -EINVAL;
13241 			goto out;
13242 		}
13243 
13244 		num_rates = ath12k_mac_bitrate_mask_num_eht_rates(ar, band,
13245 								  mask);
13246 		if (num_rates == 1)
13247 			eht_fixed_rate = true;
13248 
13249 		if (!ath12k_mac_eht_mcs_range_present(ar, band, mask) &&
13250 		    num_rates > 1) {
13251 			ath12k_warn(ar->ab,
13252 				    "Setting more than one EHT MCS Value in bitrate mask not supported\n");
13253 			ret = -EINVAL;
13254 			goto out;
13255 		}
13256 
13257 		ieee80211_iterate_stations_mtx(hw,
13258 					       ath12k_mac_disable_peer_fixed_rate,
13259 					       arvif);
13260 
13261 		arvif->bitrate_mask = *mask;
13262 		ieee80211_iterate_stations_mtx(hw,
13263 					       ath12k_mac_set_bitrate_mask_iter,
13264 					       arvif);
13265 	}
13266 
13267 	ret = ath12k_mac_set_rate_params(arvif, rate, nss, sgi, ldpc, he_gi,
13268 					 he_ltf, he_fixed_rate, eht_gi, eht_ltf,
13269 					 eht_fixed_rate);
13270 	if (ret) {
13271 		ath12k_warn(ar->ab, "failed to set rate params on vdev %i: %d\n",
13272 			    arvif->vdev_id, ret);
13273 	}
13274 
13275 out:
13276 	return ret;
13277 }
13278 EXPORT_SYMBOL(ath12k_mac_op_set_bitrate_mask);
13279 
13280 void
13281 ath12k_mac_op_reconfig_complete(struct ieee80211_hw *hw,
13282 				enum ieee80211_reconfig_type reconfig_type)
13283 {
13284 	struct ath12k_hw *ah = ath12k_hw_to_ah(hw);
13285 	struct ath12k *ar;
13286 	struct ath12k_base *ab;
13287 	struct ath12k_vif *ahvif;
13288 	struct ath12k_link_vif *arvif;
13289 	int recovery_count, i;
13290 
13291 	lockdep_assert_wiphy(hw->wiphy);
13292 
13293 	if (reconfig_type != IEEE80211_RECONFIG_TYPE_RESTART)
13294 		return;
13295 
13296 	guard(mutex)(&ah->hw_mutex);
13297 
13298 	if (ah->state != ATH12K_HW_STATE_RESTARTED)
13299 		return;
13300 
13301 	ah->state = ATH12K_HW_STATE_ON;
13302 	ieee80211_wake_queues(hw);
13303 
13304 	for_each_ar(ah, ar, i) {
13305 		ab = ar->ab;
13306 
13307 		ath12k_warn(ar->ab, "pdev %d successfully recovered\n",
13308 			    ar->pdev->pdev_id);
13309 
13310 		if (ar->ab->hw_params->current_cc_support &&
13311 		    ar->alpha2[0] != 0 && ar->alpha2[1] != 0) {
13312 			struct wmi_set_current_country_arg arg = {};
13313 
13314 			memcpy(&arg.alpha2, ar->alpha2, 2);
13315 			reinit_completion(&ar->regd_update_completed);
13316 			ath12k_wmi_send_set_current_country_cmd(ar, &arg);
13317 		}
13318 
13319 		if (ab->is_reset) {
13320 			recovery_count = atomic_inc_return(&ab->recovery_count);
13321 
13322 			ath12k_dbg(ab, ATH12K_DBG_BOOT, "recovery count %d\n",
13323 				   recovery_count);
13324 
13325 			/* When there are multiple radios in an SOC,
13326 			 * the recovery has to be done for each radio
13327 			 */
13328 			if (recovery_count == ab->num_radios) {
13329 				atomic_dec(&ab->reset_count);
13330 				complete(&ab->reset_complete);
13331 				ab->is_reset = false;
13332 				atomic_set(&ab->fail_cont_count, 0);
13333 				ath12k_dbg(ab, ATH12K_DBG_BOOT, "reset success\n");
13334 			}
13335 		}
13336 
13337 		list_for_each_entry(arvif, &ar->arvifs, list) {
13338 			ahvif = arvif->ahvif;
13339 			ath12k_dbg(ab, ATH12K_DBG_BOOT,
13340 				   "reconfig cipher %d up %d vdev type %d\n",
13341 				   ahvif->dp_vif.key_cipher,
13342 				   arvif->is_up,
13343 				   ahvif->vdev_type);
13344 
13345 			/* After trigger disconnect, then upper layer will
13346 			 * trigger connect again, then the PN number of
13347 			 * upper layer will be reset to keep up with AP
13348 			 * side, hence PN number mismatch will not happen.
13349 			 */
13350 			if (arvif->is_up &&
13351 			    ahvif->vdev_type == WMI_VDEV_TYPE_STA &&
13352 			    ahvif->vdev_subtype == WMI_VDEV_SUBTYPE_NONE) {
13353 				ieee80211_hw_restart_disconnect(ahvif->vif);
13354 
13355 				ath12k_dbg(ab, ATH12K_DBG_BOOT,
13356 					   "restart disconnect\n");
13357 			}
13358 		}
13359 	}
13360 }
13361 EXPORT_SYMBOL(ath12k_mac_op_reconfig_complete);
13362 
13363 static void
13364 ath12k_mac_update_bss_chan_survey(struct ath12k *ar,
13365 				  struct ieee80211_channel *channel)
13366 {
13367 	int ret;
13368 	enum wmi_bss_chan_info_req_type type = WMI_BSS_SURVEY_REQ_TYPE_READ;
13369 
13370 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
13371 
13372 	if (!test_bit(WMI_TLV_SERVICE_BSS_CHANNEL_INFO_64, ar->ab->wmi_ab.svc_map) ||
13373 	    ar->rx_channel != channel)
13374 		return;
13375 
13376 	if (ar->scan.state != ATH12K_SCAN_IDLE) {
13377 		ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
13378 			   "ignoring bss chan info req while scanning..\n");
13379 		return;
13380 	}
13381 
13382 	reinit_completion(&ar->bss_survey_done);
13383 
13384 	ret = ath12k_wmi_pdev_bss_chan_info_request(ar, type);
13385 	if (ret) {
13386 		ath12k_warn(ar->ab, "failed to send pdev bss chan info request\n");
13387 		return;
13388 	}
13389 
13390 	ret = wait_for_completion_timeout(&ar->bss_survey_done, 3 * HZ);
13391 	if (ret == 0)
13392 		ath12k_warn(ar->ab, "bss channel survey timed out\n");
13393 }
13394 
13395 int ath12k_mac_op_get_survey(struct ieee80211_hw *hw, int idx,
13396 			     struct survey_info *survey)
13397 {
13398 	struct ath12k *ar;
13399 	struct ieee80211_supported_band *sband;
13400 	struct survey_info *ar_survey;
13401 
13402 	lockdep_assert_wiphy(hw->wiphy);
13403 
13404 	if (idx >= ATH12K_NUM_CHANS)
13405 		return -ENOENT;
13406 
13407 	sband = hw->wiphy->bands[NL80211_BAND_2GHZ];
13408 	if (sband && idx >= sband->n_channels) {
13409 		idx -= sband->n_channels;
13410 		sband = NULL;
13411 	}
13412 
13413 	if (!sband)
13414 		sband = hw->wiphy->bands[NL80211_BAND_5GHZ];
13415 	if (sband && idx >= sband->n_channels) {
13416 		idx -= sband->n_channels;
13417 		sband = NULL;
13418 	}
13419 
13420 	if (!sband)
13421 		sband = hw->wiphy->bands[NL80211_BAND_6GHZ];
13422 
13423 	if (!sband || idx >= sband->n_channels)
13424 		return -ENOENT;
13425 
13426 	ar = ath12k_mac_get_ar_by_chan(hw, &sband->channels[idx]);
13427 	if (!ar) {
13428 		if (sband->channels[idx].flags & IEEE80211_CHAN_DISABLED) {
13429 			memset(survey, 0, sizeof(*survey));
13430 			return 0;
13431 		}
13432 		return -ENOENT;
13433 	}
13434 
13435 	ar_survey = &ar->survey[idx];
13436 
13437 	ath12k_mac_update_bss_chan_survey(ar, &sband->channels[idx]);
13438 
13439 	spin_lock_bh(&ar->data_lock);
13440 	memcpy(survey, ar_survey, sizeof(*survey));
13441 	spin_unlock_bh(&ar->data_lock);
13442 
13443 	survey->channel = &sband->channels[idx];
13444 
13445 	if (ar->rx_channel == survey->channel)
13446 		survey->filled |= SURVEY_INFO_IN_USE;
13447 
13448 	return 0;
13449 }
13450 EXPORT_SYMBOL(ath12k_mac_op_get_survey);
13451 
13452 static void ath12k_mac_put_chain_rssi(struct station_info *sinfo,
13453 				      struct ath12k_link_sta *arsta)
13454 {
13455 	s8 rssi;
13456 	int i;
13457 
13458 	for (i = 0; i < ARRAY_SIZE(sinfo->chain_signal); i++) {
13459 		sinfo->chains &= ~BIT(i);
13460 		rssi = arsta->chain_signal[i];
13461 
13462 		if (rssi != ATH12K_DEFAULT_NOISE_FLOOR &&
13463 		    rssi != ATH12K_INVALID_RSSI_FULL &&
13464 		    rssi != ATH12K_INVALID_RSSI_EMPTY &&
13465 		    rssi != 0) {
13466 			sinfo->chain_signal[i] = rssi;
13467 			sinfo->chains |= BIT(i);
13468 			sinfo->filled |= BIT_ULL(NL80211_STA_INFO_CHAIN_SIGNAL);
13469 		}
13470 	}
13471 }
13472 
13473 void ath12k_mac_op_sta_statistics(struct ieee80211_hw *hw,
13474 				  struct ieee80211_vif *vif,
13475 				  struct ieee80211_sta *sta,
13476 				  struct station_info *sinfo)
13477 {
13478 	struct ath12k_sta *ahsta = ath12k_sta_to_ahsta(sta);
13479 	struct ath12k_dp_link_peer_rate_info rate_info = {};
13480 	struct ath12k_fw_stats_req_params params = {};
13481 	struct ath12k_dp_link_peer *peer;
13482 	struct ath12k_link_sta *arsta;
13483 	s8 signal, noise_floor;
13484 	struct ath12k_dp *dp;
13485 	struct ath12k *ar;
13486 	bool db2dbm;
13487 
13488 	lockdep_assert_wiphy(hw->wiphy);
13489 
13490 	arsta = &ahsta->deflink;
13491 	ar = ath12k_get_ar_by_vif(hw, vif, arsta->link_id);
13492 	if (!ar)
13493 		return;
13494 
13495 	dp = ath12k_ab_to_dp(ar->ab);
13496 	ath12k_dp_link_peer_get_sta_rate_info_stats(dp, arsta->addr, &rate_info);
13497 
13498 	db2dbm = test_bit(WMI_TLV_SERVICE_HW_DB2DBM_CONVERSION_SUPPORT,
13499 			  ar->ab->wmi_ab.svc_map);
13500 
13501 	sinfo->rx_duration = rate_info.rx_duration;
13502 	sinfo->filled |= BIT_ULL(NL80211_STA_INFO_RX_DURATION);
13503 
13504 	sinfo->tx_duration = rate_info.tx_duration;
13505 	sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_DURATION);
13506 
13507 	if (rate_info.txrate.legacy || rate_info.txrate.nss) {
13508 		if (rate_info.txrate.legacy) {
13509 			sinfo->txrate.legacy = rate_info.txrate.legacy;
13510 		} else {
13511 			sinfo->txrate.mcs = rate_info.txrate.mcs;
13512 			sinfo->txrate.nss = rate_info.txrate.nss;
13513 			sinfo->txrate.bw = rate_info.txrate.bw;
13514 			sinfo->txrate.he_gi = rate_info.txrate.he_gi;
13515 			sinfo->txrate.he_dcm = rate_info.txrate.he_dcm;
13516 			sinfo->txrate.he_ru_alloc = rate_info.txrate.he_ru_alloc;
13517 			sinfo->txrate.eht_gi = rate_info.txrate.eht_gi;
13518 			sinfo->txrate.eht_ru_alloc = rate_info.txrate.eht_ru_alloc;
13519 		}
13520 		sinfo->txrate.flags = rate_info.txrate.flags;
13521 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_BITRATE);
13522 	}
13523 
13524 	/* TODO: Use real NF instead of default one. */
13525 	signal = rate_info.rssi_comb;
13526 
13527 	params.pdev_id = ath12k_mac_get_target_pdev_id(ar);
13528 	params.vdev_id = 0;
13529 	params.stats_id = WMI_REQUEST_VDEV_STAT;
13530 
13531 	if (!signal &&
13532 	    ahsta->ahvif->vdev_type == WMI_VDEV_TYPE_STA &&
13533 	    !(ath12k_mac_get_fw_stats(ar, &params))) {
13534 		signal = arsta->rssi_beacon;
13535 		ath12k_fw_stats_reset(ar);
13536 	}
13537 
13538 	params.stats_id = WMI_REQUEST_RSSI_PER_CHAIN_STAT;
13539 	if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_CHAIN_SIGNAL)) &&
13540 	    ahsta->ahvif->vdev_type == WMI_VDEV_TYPE_STA &&
13541 	    !(ath12k_mac_get_fw_stats(ar, &params))) {
13542 		ath12k_mac_put_chain_rssi(sinfo, arsta);
13543 		ath12k_fw_stats_reset(ar);
13544 	}
13545 
13546 	spin_lock_bh(&ar->data_lock);
13547 	noise_floor = ath12k_pdev_get_noise_floor(ar);
13548 	spin_unlock_bh(&ar->data_lock);
13549 
13550 	if (signal) {
13551 		sinfo->signal = db2dbm ? signal : signal + noise_floor;
13552 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_SIGNAL);
13553 	}
13554 
13555 	sinfo->signal_avg = rate_info.signal_avg;
13556 
13557 	if (!db2dbm)
13558 		sinfo->signal_avg += noise_floor;
13559 
13560 	sinfo->filled |= BIT_ULL(NL80211_STA_INFO_SIGNAL_AVG);
13561 
13562 	spin_lock_bh(&dp->dp_lock);
13563 	peer = ath12k_dp_link_peer_find_by_addr(dp, arsta->addr);
13564 	if (!peer) {
13565 		spin_unlock_bh(&dp->dp_lock);
13566 		return;
13567 	}
13568 
13569 	sinfo->tx_retries = peer->tx_retry_count;
13570 	sinfo->tx_failed = peer->tx_retry_failed;
13571 	sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_RETRIES);
13572 	sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_FAILED);
13573 
13574 	spin_unlock_bh(&dp->dp_lock);
13575 }
13576 EXPORT_SYMBOL(ath12k_mac_op_sta_statistics);
13577 
13578 void ath12k_mac_op_link_sta_statistics(struct ieee80211_hw *hw,
13579 				       struct ieee80211_vif *vif,
13580 				       struct ieee80211_link_sta *link_sta,
13581 				       struct link_station_info *link_sinfo)
13582 {
13583 	struct ath12k_sta *ahsta = ath12k_sta_to_ahsta(link_sta->sta);
13584 	struct ath12k_fw_stats_req_params params = {};
13585 	struct ath12k_dp_link_peer *peer;
13586 	struct ath12k_link_sta *arsta;
13587 	struct ath12k *ar;
13588 	s8 signal;
13589 	bool db2dbm;
13590 
13591 	lockdep_assert_wiphy(hw->wiphy);
13592 
13593 	arsta = wiphy_dereference(hw->wiphy, ahsta->link[link_sta->link_id]);
13594 
13595 	if (!arsta)
13596 		return;
13597 
13598 	ar = ath12k_get_ar_by_vif(hw, vif, arsta->link_id);
13599 	if (!ar)
13600 		return;
13601 
13602 	db2dbm = test_bit(WMI_TLV_SERVICE_HW_DB2DBM_CONVERSION_SUPPORT,
13603 			  ar->ab->wmi_ab.svc_map);
13604 
13605 	spin_lock_bh(&ar->ab->dp->dp_lock);
13606 	peer = ath12k_dp_link_peer_find_by_addr(ar->ab->dp, arsta->addr);
13607 	if (!peer) {
13608 		spin_unlock_bh(&ar->ab->dp->dp_lock);
13609 		return;
13610 	}
13611 
13612 	link_sinfo->rx_duration = peer->rx_duration;
13613 	link_sinfo->filled |= BIT_ULL(NL80211_STA_INFO_RX_DURATION);
13614 
13615 	link_sinfo->tx_duration = peer->tx_duration;
13616 	link_sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_DURATION);
13617 
13618 	if (peer->txrate.legacy || peer->txrate.nss) {
13619 		if (peer->txrate.legacy) {
13620 			link_sinfo->txrate.legacy = peer->txrate.legacy;
13621 		} else {
13622 			link_sinfo->txrate.mcs = peer->txrate.mcs;
13623 			link_sinfo->txrate.nss = peer->txrate.nss;
13624 			link_sinfo->txrate.bw = peer->txrate.bw;
13625 			link_sinfo->txrate.he_gi = peer->txrate.he_gi;
13626 			link_sinfo->txrate.he_dcm = peer->txrate.he_dcm;
13627 			link_sinfo->txrate.he_ru_alloc =
13628 				peer->txrate.he_ru_alloc;
13629 			link_sinfo->txrate.eht_gi = peer->txrate.eht_gi;
13630 			link_sinfo->txrate.eht_ru_alloc =
13631 				peer->txrate.eht_ru_alloc;
13632 		}
13633 		link_sinfo->txrate.flags = peer->txrate.flags;
13634 		link_sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_BITRATE);
13635 	}
13636 
13637 	link_sinfo->signal_avg = ewma_avg_rssi_read(&peer->avg_rssi);
13638 
13639 	if (!db2dbm)
13640 		link_sinfo->signal_avg += ATH12K_DEFAULT_NOISE_FLOOR;
13641 
13642 	link_sinfo->filled |= BIT_ULL(NL80211_STA_INFO_SIGNAL_AVG);
13643 
13644 	link_sinfo->tx_retries = peer->tx_retry_count;
13645 	link_sinfo->tx_failed = peer->tx_retry_failed;
13646 	link_sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_RETRIES);
13647 	link_sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_FAILED);
13648 
13649 	/* TODO: Use real NF instead of default one. */
13650 	signal = peer->rssi_comb;
13651 
13652 	spin_unlock_bh(&ar->ab->dp->dp_lock);
13653 
13654 	if (!signal && ahsta->ahvif->vdev_type == WMI_VDEV_TYPE_STA) {
13655 		params.pdev_id = ath12k_mac_get_target_pdev_id(ar);
13656 		params.vdev_id = 0;
13657 		params.stats_id = WMI_REQUEST_VDEV_STAT;
13658 
13659 		if (!ath12k_mac_get_fw_stats(ar, &params)) {
13660 			signal = arsta->rssi_beacon;
13661 			ath12k_fw_stats_reset(ar);
13662 		}
13663 	}
13664 
13665 	if (signal) {
13666 		link_sinfo->signal =
13667 			db2dbm ? signal : signal + ATH12K_DEFAULT_NOISE_FLOOR;
13668 		link_sinfo->filled |= BIT_ULL(NL80211_STA_INFO_SIGNAL);
13669 	}
13670 }
13671 EXPORT_SYMBOL(ath12k_mac_op_link_sta_statistics);
13672 
13673 int ath12k_mac_op_cancel_remain_on_channel(struct ieee80211_hw *hw,
13674 					   struct ieee80211_vif *vif)
13675 {
13676 	struct ath12k_hw *ah = ath12k_hw_to_ah(hw);
13677 	struct ath12k *ar;
13678 
13679 	ar = ath12k_ah_to_ar(ah, 0);
13680 
13681 	lockdep_assert_wiphy(hw->wiphy);
13682 
13683 	spin_lock_bh(&ar->data_lock);
13684 	ar->scan.roc_notify = false;
13685 	spin_unlock_bh(&ar->data_lock);
13686 
13687 	ath12k_scan_abort(ar);
13688 
13689 	cancel_delayed_work_sync(&ar->scan.timeout);
13690 	wiphy_work_flush(hw->wiphy, &ar->scan.vdev_clean_wk);
13691 
13692 	return 0;
13693 }
13694 EXPORT_SYMBOL(ath12k_mac_op_cancel_remain_on_channel);
13695 
13696 int ath12k_mac_op_remain_on_channel(struct ieee80211_hw *hw,
13697 				    struct ieee80211_vif *vif,
13698 				    struct ieee80211_channel *chan,
13699 				    int duration,
13700 				    enum ieee80211_roc_type type)
13701 {
13702 	struct ath12k_vif *ahvif = ath12k_vif_to_ahvif(vif);
13703 	struct ath12k_hw *ah = ath12k_hw_to_ah(hw);
13704 	struct ath12k_link_vif *arvif;
13705 	struct ath12k *ar;
13706 	u32 scan_time_msec;
13707 	bool create = true;
13708 	u8 link_id;
13709 	int ret;
13710 
13711 	lockdep_assert_wiphy(hw->wiphy);
13712 
13713 	ar = ath12k_mac_select_scan_device(hw, vif, chan->center_freq);
13714 	if (!ar)
13715 		return -EINVAL;
13716 
13717 	/* check if any of the links of ML VIF is already started on
13718 	 * radio(ar) corresponding to given scan frequency and use it,
13719 	 * if not use deflink(link 0) for scan purpose.
13720 	 */
13721 
13722 	link_id = ath12k_mac_find_link_id_by_ar(ahvif, ar);
13723 	arvif = ath12k_mac_assign_link_vif(ah, vif, link_id);
13724 	/* If the vif is already assigned to a specific vdev of an ar,
13725 	 * check whether its already started, vdev which is started
13726 	 * are not allowed to switch to a new radio.
13727 	 * If the vdev is not started, but was earlier created on a
13728 	 * different ar, delete that vdev and create a new one. We don't
13729 	 * delete at the scan stop as an optimization to avoid redundant
13730 	 * delete-create vdev's for the same ar, in case the request is
13731 	 * always on the same band for the vif
13732 	 */
13733 	if (arvif->is_created) {
13734 		if (WARN_ON(!arvif->ar))
13735 			return -EINVAL;
13736 
13737 		if (ar != arvif->ar && arvif->is_started)
13738 			return -EBUSY;
13739 
13740 		if (ar != arvif->ar) {
13741 			ath12k_mac_remove_link_interface(hw, arvif);
13742 			ath12k_mac_unassign_link_vif(arvif);
13743 		} else {
13744 			create = false;
13745 		}
13746 	}
13747 
13748 	if (create) {
13749 		arvif = ath12k_mac_assign_link_vif(ah, vif, link_id);
13750 
13751 		ret = ath12k_mac_vdev_create(ar, arvif);
13752 		if (ret) {
13753 			ath12k_warn(ar->ab, "unable to create scan vdev for roc: %d\n",
13754 				    ret);
13755 			ath12k_mac_unassign_link_vif(arvif);
13756 			return ret;
13757 		}
13758 	}
13759 
13760 	spin_lock_bh(&ar->data_lock);
13761 
13762 	switch (ar->scan.state) {
13763 	case ATH12K_SCAN_IDLE:
13764 		reinit_completion(&ar->scan.started);
13765 		reinit_completion(&ar->scan.completed);
13766 		reinit_completion(&ar->scan.on_channel);
13767 		ar->scan.state = ATH12K_SCAN_STARTING;
13768 		ar->scan.is_roc = true;
13769 		ar->scan.arvif = arvif;
13770 		ar->scan.roc_freq = chan->center_freq;
13771 		ar->scan.roc_notify = true;
13772 		ret = 0;
13773 		break;
13774 	case ATH12K_SCAN_STARTING:
13775 	case ATH12K_SCAN_RUNNING:
13776 	case ATH12K_SCAN_ABORTING:
13777 		ret = -EBUSY;
13778 		break;
13779 	}
13780 
13781 	spin_unlock_bh(&ar->data_lock);
13782 
13783 	if (ret)
13784 		return ret;
13785 
13786 	scan_time_msec = hw->wiphy->max_remain_on_channel_duration * 2;
13787 
13788 	struct ath12k_wmi_scan_req_arg *arg __free(kfree) =
13789 					kzalloc_flex(*arg, chan_list, 1);
13790 	if (!arg)
13791 		return -ENOMEM;
13792 
13793 	arg->num_chan = 1;
13794 	ath12k_wmi_start_scan_init(ar, arg);
13795 
13796 	arg->vdev_id = arvif->vdev_id;
13797 	arg->scan_id = ATH12K_SCAN_ID;
13798 	arg->chan_list[0] = chan->center_freq;
13799 	arg->dwell_time_active = scan_time_msec;
13800 	arg->dwell_time_passive = scan_time_msec;
13801 	arg->max_scan_time = scan_time_msec;
13802 	arg->scan_f_passive = 1;
13803 	arg->burst_duration = duration;
13804 
13805 	ret = ath12k_start_scan(ar, arg);
13806 	if (ret) {
13807 		ath12k_warn(ar->ab, "failed to start roc scan: %d\n", ret);
13808 
13809 		spin_lock_bh(&ar->data_lock);
13810 		ar->scan.state = ATH12K_SCAN_IDLE;
13811 		spin_unlock_bh(&ar->data_lock);
13812 		return ret;
13813 	}
13814 
13815 	ret = wait_for_completion_timeout(&ar->scan.on_channel, 3 * HZ);
13816 	if (ret == 0) {
13817 		ath12k_warn(ar->ab, "failed to switch to channel for roc scan\n");
13818 		ret = ath12k_scan_stop(ar);
13819 		if (ret)
13820 			ath12k_warn(ar->ab, "failed to stop scan: %d\n", ret);
13821 		return -ETIMEDOUT;
13822 	}
13823 
13824 	ieee80211_queue_delayed_work(hw, &ar->scan.timeout,
13825 				     msecs_to_jiffies(duration));
13826 
13827 	return 0;
13828 }
13829 EXPORT_SYMBOL(ath12k_mac_op_remain_on_channel);
13830 
13831 void ath12k_mac_op_set_rekey_data(struct ieee80211_hw *hw,
13832 				  struct ieee80211_vif *vif,
13833 				  struct cfg80211_gtk_rekey_data *data)
13834 {
13835 	struct ath12k_vif *ahvif = ath12k_vif_to_ahvif(vif);
13836 	struct ath12k_rekey_data *rekey_data;
13837 	struct ath12k_hw *ah = ath12k_hw_to_ah(hw);
13838 	struct ath12k *ar = ath12k_ah_to_ar(ah, 0);
13839 	struct ath12k_link_vif *arvif;
13840 
13841 	lockdep_assert_wiphy(hw->wiphy);
13842 
13843 	arvif = &ahvif->deflink;
13844 	rekey_data = &arvif->rekey_data;
13845 
13846 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac set rekey data vdev %d\n",
13847 		   arvif->vdev_id);
13848 
13849 	memcpy(rekey_data->kck, data->kck, NL80211_KCK_LEN);
13850 	memcpy(rekey_data->kek, data->kek, NL80211_KEK_LEN);
13851 
13852 	/* The supplicant works on big-endian, the firmware expects it on
13853 	 * little endian.
13854 	 */
13855 	rekey_data->replay_ctr = get_unaligned_be64(data->replay_ctr);
13856 
13857 	arvif->rekey_data.enable_offload = true;
13858 
13859 	ath12k_dbg_dump(ar->ab, ATH12K_DBG_MAC, "kck", NULL,
13860 			rekey_data->kck, NL80211_KCK_LEN);
13861 	ath12k_dbg_dump(ar->ab, ATH12K_DBG_MAC, "kek", NULL,
13862 			rekey_data->kck, NL80211_KEK_LEN);
13863 	ath12k_dbg_dump(ar->ab, ATH12K_DBG_MAC, "replay ctr", NULL,
13864 			&rekey_data->replay_ctr, sizeof(rekey_data->replay_ctr));
13865 }
13866 EXPORT_SYMBOL(ath12k_mac_op_set_rekey_data);
13867 
13868 void ath12k_mac_update_freq_range(struct ath12k *ar,
13869 				  u32 freq_low, u32 freq_high)
13870 {
13871 	if (!(freq_low && freq_high))
13872 		return;
13873 
13874 	if (ar->freq_range.start_freq || ar->freq_range.end_freq) {
13875 		ar->freq_range.start_freq = min(ar->freq_range.start_freq,
13876 						MHZ_TO_KHZ(freq_low));
13877 		ar->freq_range.end_freq = max(ar->freq_range.end_freq,
13878 					      MHZ_TO_KHZ(freq_high));
13879 	} else {
13880 		ar->freq_range.start_freq = MHZ_TO_KHZ(freq_low);
13881 		ar->freq_range.end_freq = MHZ_TO_KHZ(freq_high);
13882 	}
13883 
13884 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
13885 		   "mac pdev %u freq limit updated. New range %u->%u MHz\n",
13886 		   ar->pdev->pdev_id, KHZ_TO_MHZ(ar->freq_range.start_freq),
13887 		   KHZ_TO_MHZ(ar->freq_range.end_freq));
13888 }
13889 
13890 static void ath12k_mac_update_ch_list(struct ath12k *ar,
13891 				      struct ieee80211_supported_band *band,
13892 				      u32 freq_low, u32 freq_high)
13893 {
13894 	int i;
13895 
13896 	if (!(freq_low && freq_high))
13897 		return;
13898 
13899 	for (i = 0; i < band->n_channels; i++) {
13900 		if (band->channels[i].center_freq < freq_low ||
13901 		    band->channels[i].center_freq > freq_high)
13902 			band->channels[i].flags |= IEEE80211_CHAN_DISABLED;
13903 	}
13904 }
13905 
13906 static u32 ath12k_get_phy_id(struct ath12k *ar, u32 band)
13907 {
13908 	struct ath12k_pdev *pdev = ar->pdev;
13909 	struct ath12k_pdev_cap *pdev_cap = &pdev->cap;
13910 
13911 	if (band == WMI_HOST_WLAN_2GHZ_CAP)
13912 		return pdev_cap->band[NL80211_BAND_2GHZ].phy_id;
13913 
13914 	if (band == WMI_HOST_WLAN_5GHZ_CAP)
13915 		return pdev_cap->band[NL80211_BAND_5GHZ].phy_id;
13916 
13917 	ath12k_warn(ar->ab, "unsupported phy cap:%d\n", band);
13918 
13919 	return 0;
13920 }
13921 
13922 static int ath12k_mac_update_band(struct ath12k *ar,
13923 				  struct ieee80211_supported_band *orig_band,
13924 				  struct ieee80211_supported_band *new_band)
13925 {
13926 	int i;
13927 
13928 	if (!orig_band || !new_band)
13929 		return -EINVAL;
13930 
13931 	if (orig_band->band != new_band->band)
13932 		return -EINVAL;
13933 
13934 	for (i = 0; i < new_band->n_channels; i++) {
13935 		if (new_band->channels[i].flags & IEEE80211_CHAN_DISABLED)
13936 			continue;
13937 		/* An enabled channel in new_band should not be already enabled
13938 		 * in the orig_band
13939 		 */
13940 		if (WARN_ON(!(orig_band->channels[i].flags &
13941 			      IEEE80211_CHAN_DISABLED)))
13942 			return -EINVAL;
13943 		orig_band->channels[i].flags &= ~IEEE80211_CHAN_DISABLED;
13944 	}
13945 	return 0;
13946 }
13947 
13948 static void ath12k_mac_update_5_9_ghz_ch_list(struct ath12k *ar,
13949 					      struct ieee80211_supported_band *band)
13950 {
13951 	int i;
13952 
13953 	if (test_bit(WMI_TLV_SERVICE_5_9GHZ_SUPPORT,
13954 		     ar->ab->wmi_ab.svc_map))
13955 		return;
13956 
13957 	guard(spinlock_bh)(&ar->ab->base_lock);
13958 	if (ar->ab->dfs_region != ATH12K_DFS_REG_FCC)
13959 		return;
13960 
13961 	for (i = 0; i < band->n_channels; i++) {
13962 		if (band->channels[i].center_freq >= ATH12K_5_9_GHZ_MIN_FREQ &&
13963 		    band->channels[i].center_freq <= ATH12K_5_9_GHZ_MAX_FREQ)
13964 			band->channels[i].flags |= IEEE80211_CHAN_DISABLED;
13965 	}
13966 }
13967 
13968 static int ath12k_mac_setup_channels_rates(struct ath12k *ar,
13969 					   u32 supported_bands,
13970 					   struct ieee80211_supported_band *bands[])
13971 {
13972 	struct ieee80211_supported_band *band;
13973 	struct ath12k_wmi_hal_reg_capabilities_ext_arg *reg_cap;
13974 	struct ath12k_base *ab = ar->ab;
13975 	u32 phy_id, freq_low, freq_high;
13976 	struct ath12k_hw *ah = ar->ah;
13977 	void *channels;
13978 	int ret;
13979 
13980 	BUILD_BUG_ON((ARRAY_SIZE(ath12k_2ghz_channels) +
13981 		      ARRAY_SIZE(ath12k_5ghz_channels) +
13982 		      ARRAY_SIZE(ath12k_6ghz_channels)) !=
13983 		     ATH12K_NUM_CHANS);
13984 
13985 	reg_cap = &ab->hal_reg_cap[ar->pdev_idx];
13986 
13987 	if (supported_bands & WMI_HOST_WLAN_2GHZ_CAP) {
13988 		channels = kmemdup(ath12k_2ghz_channels,
13989 				   sizeof(ath12k_2ghz_channels),
13990 				   GFP_KERNEL);
13991 		if (!channels)
13992 			return -ENOMEM;
13993 
13994 		band = &ar->mac.sbands[NL80211_BAND_2GHZ];
13995 		band->band = NL80211_BAND_2GHZ;
13996 		band->n_channels = ARRAY_SIZE(ath12k_2ghz_channels);
13997 		band->channels = channels;
13998 		band->n_bitrates = ath12k_g_rates_size;
13999 		band->bitrates = ath12k_g_rates;
14000 
14001 		if (ab->hw_params->single_pdev_only) {
14002 			phy_id = ath12k_get_phy_id(ar, WMI_HOST_WLAN_2GHZ_CAP);
14003 			reg_cap = &ab->hal_reg_cap[phy_id];
14004 		}
14005 
14006 		freq_low = max(reg_cap->low_2ghz_chan,
14007 			       ab->reg_freq_2ghz.start_freq);
14008 		freq_high = min(reg_cap->high_2ghz_chan,
14009 				ab->reg_freq_2ghz.end_freq);
14010 
14011 		ath12k_mac_update_ch_list(ar, band,
14012 					  reg_cap->low_2ghz_chan,
14013 					  reg_cap->high_2ghz_chan);
14014 
14015 		ath12k_mac_update_freq_range(ar, freq_low, freq_high);
14016 
14017 		if (!bands[NL80211_BAND_2GHZ]) {
14018 			bands[NL80211_BAND_2GHZ] = band;
14019 		} else {
14020 			/* Split mac in same band under same wiphy */
14021 			ret = ath12k_mac_update_band(ar, bands[NL80211_BAND_2GHZ], band);
14022 			if (ret) {
14023 				kfree(channels);
14024 				band->channels = NULL;
14025 				return ret;
14026 			}
14027 			ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac pdev %u identified as 2 GHz split mac with start freq %d end freq %d",
14028 				   ar->pdev->pdev_id,
14029 				   KHZ_TO_MHZ(ar->freq_range.start_freq),
14030 				   KHZ_TO_MHZ(ar->freq_range.end_freq));
14031 		}
14032 	}
14033 
14034 	if (supported_bands & WMI_HOST_WLAN_5GHZ_CAP) {
14035 		if (reg_cap->high_5ghz_chan >= ATH12K_MIN_6GHZ_FREQ) {
14036 			channels = kmemdup(ath12k_6ghz_channels,
14037 					   sizeof(ath12k_6ghz_channels), GFP_KERNEL);
14038 			if (!channels) {
14039 				kfree(ar->mac.sbands[NL80211_BAND_2GHZ].channels);
14040 				return -ENOMEM;
14041 			}
14042 
14043 			ar->supports_6ghz = true;
14044 			band = &ar->mac.sbands[NL80211_BAND_6GHZ];
14045 			band->band = NL80211_BAND_6GHZ;
14046 			band->n_channels = ARRAY_SIZE(ath12k_6ghz_channels);
14047 			band->channels = channels;
14048 			band->n_bitrates = ath12k_a_rates_size;
14049 			band->bitrates = ath12k_a_rates;
14050 
14051 			freq_low = max(reg_cap->low_5ghz_chan,
14052 				       ab->reg_freq_6ghz.start_freq);
14053 			freq_high = min(reg_cap->high_5ghz_chan,
14054 					ab->reg_freq_6ghz.end_freq);
14055 
14056 			ath12k_mac_update_ch_list(ar, band,
14057 						  reg_cap->low_5ghz_chan,
14058 						  reg_cap->high_5ghz_chan);
14059 
14060 			ath12k_mac_update_freq_range(ar, freq_low, freq_high);
14061 			ah->use_6ghz_regd = true;
14062 
14063 			if (!bands[NL80211_BAND_6GHZ]) {
14064 				bands[NL80211_BAND_6GHZ] = band;
14065 			} else {
14066 				/* Split mac in same band under same wiphy */
14067 				ret = ath12k_mac_update_band(ar,
14068 							     bands[NL80211_BAND_6GHZ],
14069 							     band);
14070 				if (ret) {
14071 					kfree(ar->mac.sbands[NL80211_BAND_2GHZ].channels);
14072 					ar->mac.sbands[NL80211_BAND_2GHZ].channels = NULL;
14073 					kfree(channels);
14074 					band->channels = NULL;
14075 					return ret;
14076 				}
14077 				ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac pdev %u identified as 6 GHz split mac with start freq %d end freq %d",
14078 					   ar->pdev->pdev_id,
14079 					   KHZ_TO_MHZ(ar->freq_range.start_freq),
14080 					   KHZ_TO_MHZ(ar->freq_range.end_freq));
14081 			}
14082 		}
14083 
14084 		if (reg_cap->low_5ghz_chan < ATH12K_MIN_6GHZ_FREQ) {
14085 			channels = kmemdup(ath12k_5ghz_channels,
14086 					   sizeof(ath12k_5ghz_channels),
14087 					   GFP_KERNEL);
14088 			if (!channels) {
14089 				kfree(ar->mac.sbands[NL80211_BAND_2GHZ].channels);
14090 				kfree(ar->mac.sbands[NL80211_BAND_6GHZ].channels);
14091 				return -ENOMEM;
14092 			}
14093 
14094 			band = &ar->mac.sbands[NL80211_BAND_5GHZ];
14095 			band->band = NL80211_BAND_5GHZ;
14096 			band->n_channels = ARRAY_SIZE(ath12k_5ghz_channels);
14097 			band->channels = channels;
14098 			band->n_bitrates = ath12k_a_rates_size;
14099 			band->bitrates = ath12k_a_rates;
14100 
14101 			ath12k_mac_update_5_9_ghz_ch_list(ar, band);
14102 
14103 			if (ab->hw_params->single_pdev_only) {
14104 				phy_id = ath12k_get_phy_id(ar, WMI_HOST_WLAN_5GHZ_CAP);
14105 				reg_cap = &ab->hal_reg_cap[phy_id];
14106 			}
14107 
14108 			freq_low = max(reg_cap->low_5ghz_chan,
14109 				       ab->reg_freq_5ghz.start_freq);
14110 			freq_high = min(reg_cap->high_5ghz_chan,
14111 					ab->reg_freq_5ghz.end_freq);
14112 
14113 			ath12k_mac_update_ch_list(ar, band,
14114 						  reg_cap->low_5ghz_chan,
14115 						  reg_cap->high_5ghz_chan);
14116 
14117 			ath12k_mac_update_freq_range(ar, freq_low, freq_high);
14118 
14119 			if (!bands[NL80211_BAND_5GHZ]) {
14120 				bands[NL80211_BAND_5GHZ] = band;
14121 			} else {
14122 				/* Split mac in same band under same wiphy */
14123 				ret = ath12k_mac_update_band(ar,
14124 							     bands[NL80211_BAND_5GHZ],
14125 							     band);
14126 				if (ret) {
14127 					kfree(ar->mac.sbands[NL80211_BAND_2GHZ].channels);
14128 					ar->mac.sbands[NL80211_BAND_2GHZ].channels = NULL;
14129 					kfree(ar->mac.sbands[NL80211_BAND_6GHZ].channels);
14130 					ar->mac.sbands[NL80211_BAND_2GHZ].channels = NULL;
14131 					kfree(channels);
14132 					band->channels = NULL;
14133 					return ret;
14134 				}
14135 				ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac pdev %u identified as 5 GHz split mac with start freq %d end freq %d",
14136 					   ar->pdev->pdev_id,
14137 					   KHZ_TO_MHZ(ar->freq_range.start_freq),
14138 					   KHZ_TO_MHZ(ar->freq_range.end_freq));
14139 			}
14140 		}
14141 	}
14142 
14143 	return 0;
14144 }
14145 
14146 static u16 ath12k_mac_get_ifmodes(struct ath12k_hw *ah)
14147 {
14148 	struct ath12k *ar;
14149 	int i;
14150 	u16 interface_modes = U16_MAX;
14151 
14152 	for_each_ar(ah, ar, i)
14153 		interface_modes &= ar->ab->hw_params->interface_modes;
14154 
14155 	return interface_modes == U16_MAX ? 0 : interface_modes;
14156 }
14157 
14158 static bool ath12k_mac_is_iface_mode_enable(struct ath12k_hw *ah,
14159 					    enum nl80211_iftype type)
14160 {
14161 	struct ath12k *ar;
14162 	int i;
14163 	u16 interface_modes, mode = 0;
14164 	bool is_enable = false;
14165 
14166 	if (type == NL80211_IFTYPE_MESH_POINT) {
14167 		if (IS_ENABLED(CONFIG_MAC80211_MESH))
14168 			mode = BIT(type);
14169 	} else {
14170 		mode = BIT(type);
14171 	}
14172 
14173 	for_each_ar(ah, ar, i) {
14174 		interface_modes = ar->ab->hw_params->interface_modes;
14175 		if (interface_modes & mode) {
14176 			is_enable = true;
14177 			break;
14178 		}
14179 	}
14180 
14181 	return is_enable;
14182 }
14183 
14184 static int
14185 ath12k_mac_setup_radio_iface_comb(struct ath12k *ar,
14186 				  struct ieee80211_iface_combination *comb)
14187 {
14188 	u16 interface_modes = ar->ab->hw_params->interface_modes;
14189 	struct ieee80211_iface_limit *limits;
14190 	int n_limits, max_interfaces;
14191 	bool ap, mesh, p2p;
14192 
14193 	ap = interface_modes & BIT(NL80211_IFTYPE_AP);
14194 	p2p = interface_modes & BIT(NL80211_IFTYPE_P2P_DEVICE);
14195 
14196 	mesh = IS_ENABLED(CONFIG_MAC80211_MESH) &&
14197 	       (interface_modes & BIT(NL80211_IFTYPE_MESH_POINT));
14198 
14199 	if ((ap || mesh) && !p2p) {
14200 		n_limits = 2;
14201 		max_interfaces = 16;
14202 	} else if (p2p) {
14203 		n_limits = 3;
14204 		if (ap || mesh)
14205 			max_interfaces = 16;
14206 		else
14207 			max_interfaces = 3;
14208 	} else {
14209 		n_limits = 1;
14210 		max_interfaces = 1;
14211 	}
14212 
14213 	limits = kzalloc_objs(*limits, n_limits);
14214 	if (!limits)
14215 		return -ENOMEM;
14216 
14217 	limits[0].max = 1;
14218 	limits[0].types |= BIT(NL80211_IFTYPE_STATION);
14219 
14220 	if (ap || mesh || p2p)
14221 		limits[1].max = max_interfaces;
14222 
14223 	if (ap)
14224 		limits[1].types |= BIT(NL80211_IFTYPE_AP);
14225 
14226 	if (mesh)
14227 		limits[1].types |= BIT(NL80211_IFTYPE_MESH_POINT);
14228 
14229 	if (p2p) {
14230 		limits[1].types |= BIT(NL80211_IFTYPE_P2P_CLIENT) |
14231 					BIT(NL80211_IFTYPE_P2P_GO);
14232 		limits[2].max = 1;
14233 		limits[2].types |= BIT(NL80211_IFTYPE_P2P_DEVICE);
14234 	}
14235 
14236 	comb[0].limits = limits;
14237 	comb[0].n_limits = n_limits;
14238 	comb[0].max_interfaces = max_interfaces;
14239 	comb[0].beacon_int_infra_match = true;
14240 	comb[0].beacon_int_min_gcd = 100;
14241 
14242 	comb[0].num_different_channels = 1;
14243 	comb[0].radar_detect_widths = BIT(NL80211_CHAN_WIDTH_20_NOHT) |
14244 				      BIT(NL80211_CHAN_WIDTH_20) |
14245 				      BIT(NL80211_CHAN_WIDTH_40) |
14246 				      BIT(NL80211_CHAN_WIDTH_80) |
14247 				      BIT(NL80211_CHAN_WIDTH_160);
14248 
14249 	return 0;
14250 }
14251 
14252 static int
14253 ath12k_mac_setup_global_iface_comb(struct ath12k_hw *ah,
14254 				   struct wiphy_radio *radio,
14255 				   u8 n_radio,
14256 				   struct ieee80211_iface_combination *comb)
14257 {
14258 	const struct ieee80211_iface_combination *iter_comb;
14259 	struct ieee80211_iface_limit *limits;
14260 	int i, j, n_limits;
14261 	bool ap, mesh, p2p;
14262 
14263 	if (!n_radio)
14264 		return 0;
14265 
14266 	ap = ath12k_mac_is_iface_mode_enable(ah, NL80211_IFTYPE_AP);
14267 	p2p = ath12k_mac_is_iface_mode_enable(ah, NL80211_IFTYPE_P2P_DEVICE);
14268 	mesh = ath12k_mac_is_iface_mode_enable(ah, NL80211_IFTYPE_MESH_POINT);
14269 
14270 	if ((ap || mesh) && !p2p)
14271 		n_limits = 2;
14272 	else if (p2p)
14273 		n_limits = 3;
14274 	else
14275 		n_limits = 1;
14276 
14277 	limits = kzalloc_objs(*limits, n_limits);
14278 	if (!limits)
14279 		return -ENOMEM;
14280 
14281 	for (i = 0; i < n_radio; i++) {
14282 		iter_comb = radio[i].iface_combinations;
14283 		for (j = 0; j < iter_comb->n_limits && j < n_limits; j++) {
14284 			limits[j].types |= iter_comb->limits[j].types;
14285 			limits[j].max += iter_comb->limits[j].max;
14286 		}
14287 
14288 		comb->max_interfaces += iter_comb->max_interfaces;
14289 		comb->num_different_channels += iter_comb->num_different_channels;
14290 		comb->radar_detect_widths |= iter_comb->radar_detect_widths;
14291 	}
14292 
14293 	comb->limits = limits;
14294 	comb->n_limits = n_limits;
14295 	comb->beacon_int_infra_match = true;
14296 	comb->beacon_int_min_gcd = 100;
14297 
14298 	return 0;
14299 }
14300 
14301 static
14302 void ath12k_mac_cleanup_iface_comb(const struct ieee80211_iface_combination *iface_comb)
14303 {
14304 	kfree(iface_comb[0].limits);
14305 	kfree(iface_comb);
14306 }
14307 
14308 static void ath12k_mac_cleanup_iface_combinations(struct ath12k_hw *ah)
14309 {
14310 	struct wiphy *wiphy = ah->hw->wiphy;
14311 	const struct wiphy_radio *radio;
14312 	int i;
14313 
14314 	if (wiphy->n_radio > 0) {
14315 		radio = wiphy->radio;
14316 		for (i = 0; i < wiphy->n_radio; i++)
14317 			ath12k_mac_cleanup_iface_comb(radio[i].iface_combinations);
14318 
14319 		kfree(wiphy->radio);
14320 	}
14321 
14322 	ath12k_mac_cleanup_iface_comb(wiphy->iface_combinations);
14323 }
14324 
14325 static int ath12k_mac_setup_iface_combinations(struct ath12k_hw *ah)
14326 {
14327 	struct ieee80211_iface_combination *combinations, *comb;
14328 	struct wiphy *wiphy = ah->hw->wiphy;
14329 	struct wiphy_radio *radio;
14330 	int n_combinations = 1;
14331 	struct ath12k *ar;
14332 	int i, ret;
14333 
14334 	if (ah->num_radio == 1) {
14335 		ar = &ah->radio[0];
14336 
14337 		if (ar->ab->hw_params->single_pdev_only)
14338 			n_combinations = 2;
14339 
14340 		combinations = kzalloc_objs(*combinations, n_combinations);
14341 		if (!combinations)
14342 			return -ENOMEM;
14343 
14344 		ret = ath12k_mac_setup_radio_iface_comb(ar, combinations);
14345 		if (ret) {
14346 			ath12k_hw_warn(ah, "failed to setup radio interface combinations for one radio: %d",
14347 				       ret);
14348 			goto err_free_combinations;
14349 		}
14350 
14351 		if (ar->ab->hw_params->single_pdev_only) {
14352 			comb = combinations + 1;
14353 			memcpy(comb, combinations, sizeof(*comb));
14354 			comb->num_different_channels = 2;
14355 			comb->radar_detect_widths = 0;
14356 		}
14357 
14358 		goto out;
14359 	}
14360 
14361 	combinations = kzalloc_objs(*combinations, n_combinations);
14362 	if (!combinations)
14363 		return -ENOMEM;
14364 
14365 	/* there are multiple radios */
14366 
14367 	radio = kzalloc_objs(*radio, ah->num_radio);
14368 	if (!radio) {
14369 		ret = -ENOMEM;
14370 		goto err_free_combinations;
14371 	}
14372 
14373 	for_each_ar(ah, ar, i) {
14374 		comb = kzalloc_obj(*comb);
14375 		if (!comb) {
14376 			ret = -ENOMEM;
14377 			goto err_free_radios;
14378 		}
14379 
14380 		ret = ath12k_mac_setup_radio_iface_comb(ar, comb);
14381 		if (ret) {
14382 			ath12k_hw_warn(ah, "failed to setup radio interface combinations for radio %d: %d",
14383 				       i, ret);
14384 			kfree(comb);
14385 			goto err_free_radios;
14386 		}
14387 
14388 		radio[i].freq_range = &ar->freq_range;
14389 		radio[i].n_freq_range = 1;
14390 
14391 		radio[i].iface_combinations = comb;
14392 		radio[i].n_iface_combinations = 1;
14393 	}
14394 
14395 	ret = ath12k_mac_setup_global_iface_comb(ah, radio, ah->num_radio, combinations);
14396 	if (ret) {
14397 		ath12k_hw_warn(ah, "failed to setup global interface combinations: %d",
14398 			       ret);
14399 		goto err_free_all_radios;
14400 	}
14401 
14402 	wiphy->radio = radio;
14403 	wiphy->n_radio = ah->num_radio;
14404 
14405 out:
14406 	wiphy->iface_combinations = combinations;
14407 	wiphy->n_iface_combinations = n_combinations;
14408 
14409 	return 0;
14410 
14411 err_free_all_radios:
14412 	i = ah->num_radio;
14413 
14414 err_free_radios:
14415 	while (i--)
14416 		ath12k_mac_cleanup_iface_comb(radio[i].iface_combinations);
14417 
14418 	kfree(radio);
14419 
14420 err_free_combinations:
14421 	kfree(combinations);
14422 
14423 	return ret;
14424 }
14425 
14426 static const u8 ath12k_if_types_ext_capa[] = {
14427 	[0] = WLAN_EXT_CAPA1_EXT_CHANNEL_SWITCHING,
14428 	[2] = WLAN_EXT_CAPA3_MULTI_BSSID_SUPPORT,
14429 	[7] = WLAN_EXT_CAPA8_OPMODE_NOTIF,
14430 };
14431 
14432 static const u8 ath12k_if_types_ext_capa_sta[] = {
14433 	[0] = WLAN_EXT_CAPA1_EXT_CHANNEL_SWITCHING,
14434 	[2] = WLAN_EXT_CAPA3_MULTI_BSSID_SUPPORT,
14435 	[7] = WLAN_EXT_CAPA8_OPMODE_NOTIF,
14436 	[9] = WLAN_EXT_CAPA10_TWT_REQUESTER_SUPPORT,
14437 };
14438 
14439 static const u8 ath12k_if_types_ext_capa_ap[] = {
14440 	[0] = WLAN_EXT_CAPA1_EXT_CHANNEL_SWITCHING,
14441 	[2] = WLAN_EXT_CAPA3_MULTI_BSSID_SUPPORT,
14442 	[7] = WLAN_EXT_CAPA8_OPMODE_NOTIF,
14443 	[9] = WLAN_EXT_CAPA10_TWT_RESPONDER_SUPPORT,
14444 	[10] = WLAN_EXT_CAPA11_EMA_SUPPORT,
14445 };
14446 
14447 static struct wiphy_iftype_ext_capab ath12k_iftypes_ext_capa[] = {
14448 	{
14449 		.extended_capabilities = ath12k_if_types_ext_capa,
14450 		.extended_capabilities_mask = ath12k_if_types_ext_capa,
14451 		.extended_capabilities_len = sizeof(ath12k_if_types_ext_capa),
14452 	}, {
14453 		.iftype = NL80211_IFTYPE_STATION,
14454 		.extended_capabilities = ath12k_if_types_ext_capa_sta,
14455 		.extended_capabilities_mask = ath12k_if_types_ext_capa_sta,
14456 		.extended_capabilities_len =
14457 				sizeof(ath12k_if_types_ext_capa_sta),
14458 	}, {
14459 		.iftype = NL80211_IFTYPE_AP,
14460 		.extended_capabilities = ath12k_if_types_ext_capa_ap,
14461 		.extended_capabilities_mask = ath12k_if_types_ext_capa_ap,
14462 		.extended_capabilities_len =
14463 				sizeof(ath12k_if_types_ext_capa_ap),
14464 		.eml_capabilities = 0,
14465 		.mld_capa_and_ops = 0,
14466 	},
14467 };
14468 
14469 static void ath12k_mac_cleanup_unregister(struct ath12k *ar)
14470 {
14471 	idr_for_each(&ar->txmgmt_idr, ath12k_mac_tx_mgmt_pending_free, ar);
14472 	idr_destroy(&ar->txmgmt_idr);
14473 
14474 	kfree(ar->mac.sbands[NL80211_BAND_2GHZ].channels);
14475 	kfree(ar->mac.sbands[NL80211_BAND_5GHZ].channels);
14476 	kfree(ar->mac.sbands[NL80211_BAND_6GHZ].channels);
14477 }
14478 
14479 static void ath12k_mac_hw_unregister(struct ath12k_hw *ah)
14480 {
14481 	struct ieee80211_hw *hw = ah->hw;
14482 	struct ath12k *ar;
14483 	int i;
14484 
14485 	for_each_ar(ah, ar, i) {
14486 		cancel_work_sync(&ar->regd_channel_update_work);
14487 		cancel_work_sync(&ar->regd_update_work);
14488 		ath12k_debugfs_unregister(ar);
14489 		ath12k_fw_stats_reset(ar);
14490 	}
14491 
14492 	ieee80211_unregister_hw(hw);
14493 
14494 	for_each_ar(ah, ar, i)
14495 		ath12k_mac_cleanup_unregister(ar);
14496 
14497 	ath12k_mac_cleanup_iface_combinations(ah);
14498 
14499 	SET_IEEE80211_DEV(hw, NULL);
14500 }
14501 
14502 static int ath12k_mac_setup_register(struct ath12k *ar,
14503 				     u32 *ht_cap,
14504 				     struct ieee80211_supported_band *bands[])
14505 {
14506 	struct ath12k_pdev_cap *cap = &ar->pdev->cap;
14507 	int ret;
14508 
14509 	init_waitqueue_head(&ar->txmgmt_empty_waitq);
14510 	idr_init(&ar->txmgmt_idr);
14511 	spin_lock_init(&ar->txmgmt_idr_lock);
14512 
14513 	ath12k_pdev_caps_update(ar);
14514 
14515 	ret = ath12k_mac_setup_channels_rates(ar,
14516 					      cap->supported_bands,
14517 					      bands);
14518 	if (ret)
14519 		return ret;
14520 
14521 	ath12k_mac_setup_ht_vht_cap(ar, cap, ht_cap);
14522 	ath12k_mac_setup_sband_iftype_data(ar, cap);
14523 
14524 	ar->max_num_stations = ath12k_core_get_max_station_per_radio(ar->ab);
14525 	ar->max_num_peers = ath12k_core_get_max_peers_per_radio(ar->ab);
14526 
14527 	ar->rssi_info.min_nf_dbm = ATH12K_DEFAULT_NOISE_FLOOR;
14528 	ar->rssi_info.temp_offset = 0;
14529 	ar->rssi_info.noise_floor = ar->rssi_info.min_nf_dbm + ar->rssi_info.temp_offset;
14530 
14531 	ath12k_thermal_init_configs(ar);
14532 
14533 	return 0;
14534 }
14535 
14536 static int ath12k_mac_hw_register(struct ath12k_hw *ah)
14537 {
14538 	struct ieee80211_hw *hw = ah->hw;
14539 	struct wiphy *wiphy = hw->wiphy;
14540 	struct ath12k *ar = ath12k_ah_to_ar(ah, 0);
14541 	struct ath12k_base *ab = ar->ab;
14542 	struct ath12k_pdev *pdev;
14543 	struct ath12k_pdev_cap *cap;
14544 	static const u32 cipher_suites[] = {
14545 		WLAN_CIPHER_SUITE_TKIP,
14546 		WLAN_CIPHER_SUITE_CCMP,
14547 		WLAN_CIPHER_SUITE_AES_CMAC,
14548 		WLAN_CIPHER_SUITE_BIP_CMAC_256,
14549 		WLAN_CIPHER_SUITE_BIP_GMAC_128,
14550 		WLAN_CIPHER_SUITE_BIP_GMAC_256,
14551 		WLAN_CIPHER_SUITE_GCMP,
14552 		WLAN_CIPHER_SUITE_GCMP_256,
14553 		WLAN_CIPHER_SUITE_CCMP_256,
14554 	};
14555 	int ret, i, j;
14556 	u32 ht_cap = U32_MAX, antennas_rx = 0, antennas_tx = 0;
14557 	bool is_6ghz = false, is_raw_mode = false, is_monitor_disable = false;
14558 	u8 *mac_addr = NULL;
14559 	u8 mbssid_max_interfaces = 0;
14560 
14561 	wiphy->max_ap_assoc_sta = 0;
14562 
14563 	for_each_ar(ah, ar, i) {
14564 		u32 ht_cap_info = 0;
14565 
14566 		pdev = ar->pdev;
14567 		if (ar->ab->pdevs_macaddr_valid) {
14568 			ether_addr_copy(ar->mac_addr, pdev->mac_addr);
14569 		} else {
14570 			ether_addr_copy(ar->mac_addr, ar->ab->mac_addr);
14571 			ar->mac_addr[4] += ar->pdev_idx;
14572 		}
14573 
14574 		ret = ath12k_mac_setup_register(ar, &ht_cap_info, hw->wiphy->bands);
14575 		if (ret)
14576 			goto err_cleanup_unregister;
14577 
14578 		/* 6 GHz does not support HT Cap, hence do not consider it */
14579 		if (!ar->supports_6ghz)
14580 			ht_cap &= ht_cap_info;
14581 
14582 		wiphy->max_ap_assoc_sta += ar->max_num_stations;
14583 
14584 		/* Advertise the max antenna support of all radios, driver can handle
14585 		 * per pdev specific antenna setting based on pdev cap when antenna
14586 		 * changes are made
14587 		 */
14588 		cap = &pdev->cap;
14589 
14590 		antennas_rx = max_t(u32, antennas_rx, cap->rx_chain_mask);
14591 		antennas_tx = max_t(u32, antennas_tx, cap->tx_chain_mask);
14592 
14593 		if (ar->supports_6ghz)
14594 			is_6ghz = true;
14595 
14596 		if (test_bit(ATH12K_FLAG_RAW_MODE, &ar->ab->dev_flags))
14597 			is_raw_mode = true;
14598 
14599 		if (!ar->ab->hw_params->supports_monitor)
14600 			is_monitor_disable = true;
14601 
14602 		if (i == 0)
14603 			mac_addr = ar->mac_addr;
14604 		else
14605 			mac_addr = ab->mac_addr;
14606 
14607 		mbssid_max_interfaces += TARGET_NUM_VDEVS(ar->ab);
14608 	}
14609 
14610 	wiphy->available_antennas_rx = antennas_rx;
14611 	wiphy->available_antennas_tx = antennas_tx;
14612 
14613 	SET_IEEE80211_PERM_ADDR(hw, mac_addr);
14614 	SET_IEEE80211_DEV(hw, ab->dev);
14615 
14616 	ret = ath12k_mac_setup_iface_combinations(ah);
14617 	if (ret) {
14618 		ath12k_err(ab, "failed to setup interface combinations: %d\n", ret);
14619 		goto err_complete_cleanup_unregister;
14620 	}
14621 
14622 	wiphy->interface_modes = ath12k_mac_get_ifmodes(ah);
14623 
14624 	if (ah->num_radio == 1 &&
14625 	    wiphy->bands[NL80211_BAND_2GHZ] &&
14626 	    wiphy->bands[NL80211_BAND_5GHZ] &&
14627 	    wiphy->bands[NL80211_BAND_6GHZ])
14628 		ieee80211_hw_set(hw, SINGLE_SCAN_ON_ALL_BANDS);
14629 
14630 	ieee80211_hw_set(hw, SIGNAL_DBM);
14631 	ieee80211_hw_set(hw, SUPPORTS_PS);
14632 	ieee80211_hw_set(hw, SUPPORTS_DYNAMIC_PS);
14633 	ieee80211_hw_set(hw, MFP_CAPABLE);
14634 	ieee80211_hw_set(hw, REPORTS_TX_ACK_STATUS);
14635 	ieee80211_hw_set(hw, HAS_RATE_CONTROL);
14636 	ieee80211_hw_set(hw, AP_LINK_PS);
14637 	ieee80211_hw_set(hw, SPECTRUM_MGMT);
14638 	ieee80211_hw_set(hw, CONNECTION_MONITOR);
14639 	ieee80211_hw_set(hw, SUPPORTS_PER_STA_GTK);
14640 	ieee80211_hw_set(hw, CHANCTX_STA_CSA);
14641 	ieee80211_hw_set(hw, QUEUE_CONTROL);
14642 	ieee80211_hw_set(hw, SUPPORTS_TX_FRAG);
14643 	ieee80211_hw_set(hw, REPORTS_LOW_ACK);
14644 	ieee80211_hw_set(hw, NO_VIRTUAL_MONITOR);
14645 
14646 	if (test_bit(WMI_TLV_SERVICE_ETH_OFFLOAD, ar->wmi->wmi_ab->svc_map)) {
14647 		ieee80211_hw_set(hw, SUPPORTS_TX_ENCAP_OFFLOAD);
14648 		ieee80211_hw_set(hw, SUPPORTS_RX_DECAP_OFFLOAD);
14649 	}
14650 
14651 	if (cap->nss_ratio_enabled)
14652 		ieee80211_hw_set(hw, SUPPORTS_VHT_EXT_NSS_BW);
14653 
14654 	if ((ht_cap & WMI_HT_CAP_ENABLED) || is_6ghz) {
14655 		ieee80211_hw_set(hw, AMPDU_AGGREGATION);
14656 		ieee80211_hw_set(hw, TX_AMPDU_SETUP_IN_HW);
14657 		ieee80211_hw_set(hw, SUPPORTS_REORDERING_BUFFER);
14658 		ieee80211_hw_set(hw, SUPPORTS_AMSDU_IN_AMPDU);
14659 		ieee80211_hw_set(hw, USES_RSS);
14660 	}
14661 
14662 	wiphy->features |= NL80211_FEATURE_STATIC_SMPS;
14663 	wiphy->flags |= WIPHY_FLAG_IBSS_RSN;
14664 
14665 	/* TODO: Check if HT capability advertised from firmware is different
14666 	 * for each band for a dual band capable radio. It will be tricky to
14667 	 * handle it when the ht capability different for each band.
14668 	 */
14669 	if (ht_cap & WMI_HT_CAP_DYNAMIC_SMPS ||
14670 	    (is_6ghz && ab->hw_params->supports_dynamic_smps_6ghz))
14671 		wiphy->features |= NL80211_FEATURE_DYNAMIC_SMPS;
14672 
14673 	wiphy->max_scan_ssids = WLAN_SCAN_PARAMS_MAX_SSID;
14674 	wiphy->max_scan_ie_len = WLAN_SCAN_PARAMS_MAX_IE_LEN;
14675 
14676 	hw->max_listen_interval = ATH12K_MAX_HW_LISTEN_INTERVAL;
14677 
14678 	wiphy->flags |= WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL;
14679 	wiphy->flags |= WIPHY_FLAG_HAS_CHANNEL_SWITCH;
14680 	wiphy->max_remain_on_channel_duration = 5000;
14681 
14682 	wiphy->flags |= WIPHY_FLAG_AP_UAPSD;
14683 	wiphy->features |= NL80211_FEATURE_AP_MODE_CHAN_WIDTH_CHANGE |
14684 				   NL80211_FEATURE_AP_SCAN;
14685 
14686 	wiphy->features |= NL80211_FEATURE_TX_POWER_INSERTION;
14687 
14688 	/* MLO is not yet supported so disable Wireless Extensions for now
14689 	 * to make sure ath12k users don't use it. This flag can be removed
14690 	 * once WIPHY_FLAG_SUPPORTS_MLO is enabled.
14691 	 */
14692 	wiphy->flags |= WIPHY_FLAG_DISABLE_WEXT;
14693 
14694 	/* Copy over MLO related capabilities received from
14695 	 * WMI_SERVICE_READY_EXT2_EVENT if single_chip_mlo_supp is set.
14696 	 */
14697 	if (ab->ag->mlo_capable) {
14698 		ath12k_iftypes_ext_capa[2].eml_capabilities = cap->eml_cap;
14699 		ath12k_iftypes_ext_capa[2].mld_capa_and_ops = cap->mld_cap;
14700 		wiphy->flags |= WIPHY_FLAG_SUPPORTS_MLO;
14701 
14702 		ieee80211_hw_set(hw, MLO_MCAST_MULTI_LINK_TX);
14703 	}
14704 
14705 	hw->queues = ATH12K_HW_MAX_QUEUES;
14706 	wiphy->tx_queue_len = ATH12K_QUEUE_LEN;
14707 	hw->offchannel_tx_hw_queue = ATH12K_HW_MAX_QUEUES - 1;
14708 	hw->max_rx_aggregation_subframes = IEEE80211_MAX_AMPDU_BUF_EHT;
14709 
14710 	hw->vif_data_size = sizeof(struct ath12k_vif);
14711 	hw->sta_data_size = sizeof(struct ath12k_sta);
14712 	hw->extra_tx_headroom = ab->hw_params->iova_mask;
14713 
14714 	wiphy_ext_feature_set(wiphy, NL80211_EXT_FEATURE_CQM_RSSI_LIST);
14715 	wiphy_ext_feature_set(wiphy, NL80211_EXT_FEATURE_STA_TX_PWR);
14716 	wiphy_ext_feature_set(wiphy, NL80211_EXT_FEATURE_ACK_SIGNAL_SUPPORT);
14717 	if (test_bit(WMI_TLV_SERVICE_BSS_COLOR_OFFLOAD,
14718 		     ab->wmi_ab.svc_map)) {
14719 		wiphy_ext_feature_set(wiphy, NL80211_EXT_FEATURE_BSS_COLOR);
14720 		ieee80211_hw_set(hw, DETECTS_COLOR_COLLISION);
14721 	}
14722 
14723 	wiphy->cipher_suites = cipher_suites;
14724 	wiphy->n_cipher_suites = ARRAY_SIZE(cipher_suites);
14725 
14726 	wiphy->iftype_ext_capab = ath12k_iftypes_ext_capa;
14727 	wiphy->num_iftype_ext_capab = ARRAY_SIZE(ath12k_iftypes_ext_capa);
14728 
14729 	wiphy->mbssid_max_interfaces = mbssid_max_interfaces;
14730 	wiphy->ema_max_profile_periodicity = TARGET_EMA_MAX_PROFILE_PERIOD;
14731 	ieee80211_hw_set(hw, SUPPORTS_MULTI_BSSID);
14732 
14733 	if (is_6ghz) {
14734 		wiphy_ext_feature_set(wiphy,
14735 				      NL80211_EXT_FEATURE_FILS_DISCOVERY);
14736 		wiphy_ext_feature_set(wiphy,
14737 				      NL80211_EXT_FEATURE_UNSOL_BCAST_PROBE_RESP);
14738 	}
14739 
14740 	wiphy_ext_feature_set(wiphy, NL80211_EXT_FEATURE_PUNCT);
14741 	if (test_bit(WMI_TLV_SERVICE_BEACON_PROTECTION_SUPPORT, ab->wmi_ab.svc_map))
14742 		wiphy_ext_feature_set(hw->wiphy, NL80211_EXT_FEATURE_BEACON_PROTECTION);
14743 
14744 	ath12k_reg_init(hw);
14745 
14746 	if (!is_raw_mode) {
14747 		hw->netdev_features = NETIF_F_HW_CSUM;
14748 		ieee80211_hw_set(hw, SW_CRYPTO_CONTROL);
14749 		ieee80211_hw_set(hw, SUPPORT_FAST_XMIT);
14750 	}
14751 
14752 	if (test_bit(WMI_TLV_SERVICE_NLO, ar->wmi->wmi_ab->svc_map)) {
14753 		wiphy->max_sched_scan_ssids = WMI_PNO_MAX_SUPP_NETWORKS;
14754 		wiphy->max_match_sets = WMI_PNO_MAX_SUPP_NETWORKS;
14755 		wiphy->max_sched_scan_ie_len = WMI_PNO_MAX_IE_LENGTH;
14756 		wiphy->max_sched_scan_plans = WMI_PNO_MAX_SCHED_SCAN_PLANS;
14757 		wiphy->max_sched_scan_plan_interval =
14758 					WMI_PNO_MAX_SCHED_SCAN_PLAN_INT;
14759 		wiphy->max_sched_scan_plan_iterations =
14760 					WMI_PNO_MAX_SCHED_SCAN_PLAN_ITRNS;
14761 		wiphy->features |= NL80211_FEATURE_ND_RANDOM_MAC_ADDR;
14762 	}
14763 
14764 	ret = ath12k_wow_init(ar);
14765 	if (ret) {
14766 		ath12k_warn(ar->ab, "failed to init wow: %d\n", ret);
14767 		goto err_cleanup_if_combs;
14768 	}
14769 
14770 	/* Boot-time regulatory updates have already been processed.
14771 	 * Mark them as complete now, because after registration,
14772 	 * cfg80211 will notify us again if there are any pending hints.
14773 	 * We need to wait for those hints to be processed, so it's
14774 	 * important to mark the boot-time updates as complete before
14775 	 * proceeding with registration.
14776 	 */
14777 	for_each_ar(ah, ar, i)
14778 		complete_all(&ar->regd_update_completed);
14779 
14780 	ret = ieee80211_register_hw(hw);
14781 	if (ret) {
14782 		ath12k_err(ab, "ieee80211 registration failed: %d\n", ret);
14783 		goto err_cleanup_if_combs;
14784 	}
14785 
14786 	if (is_monitor_disable)
14787 		/* There's a race between calling ieee80211_register_hw()
14788 		 * and here where the monitor mode is enabled for a little
14789 		 * while. But that time is so short and in practice it doesn't make
14790 		 * a difference in real life.
14791 		 */
14792 		wiphy->interface_modes &= ~BIT(NL80211_IFTYPE_MONITOR);
14793 
14794 	for_each_ar(ah, ar, i) {
14795 		/* Apply the regd received during initialization */
14796 		ret = ath12k_regd_update(ar, true);
14797 		if (ret) {
14798 			ath12k_err(ar->ab, "ath12k regd update failed: %d\n", ret);
14799 			goto err_unregister_hw;
14800 		}
14801 
14802 		if (ar->ab->hw_params->current_cc_support && ab->new_alpha2[0]) {
14803 			struct wmi_set_current_country_arg current_cc = {};
14804 
14805 			memcpy(&current_cc.alpha2, ab->new_alpha2, 2);
14806 			memcpy(&ar->alpha2, ab->new_alpha2, 2);
14807 
14808 			reinit_completion(&ar->regd_update_completed);
14809 
14810 			ret = ath12k_wmi_send_set_current_country_cmd(ar, &current_cc);
14811 			if (ret)
14812 				ath12k_warn(ar->ab,
14813 					    "failed set cc code for mac register: %d\n",
14814 					    ret);
14815 		}
14816 
14817 		ath12k_fw_stats_init(ar);
14818 		ath12k_debugfs_register(ar);
14819 	}
14820 
14821 	return 0;
14822 
14823 err_unregister_hw:
14824 	for_each_ar(ah, ar, i)
14825 		ath12k_debugfs_unregister(ar);
14826 
14827 	ieee80211_unregister_hw(hw);
14828 
14829 err_cleanup_if_combs:
14830 	ath12k_mac_cleanup_iface_combinations(ah);
14831 
14832 err_complete_cleanup_unregister:
14833 	i = ah->num_radio;
14834 
14835 err_cleanup_unregister:
14836 	for (j = 0; j < i; j++) {
14837 		ar = ath12k_ah_to_ar(ah, j);
14838 		ath12k_mac_cleanup_unregister(ar);
14839 	}
14840 
14841 	SET_IEEE80211_DEV(hw, NULL);
14842 
14843 	return ret;
14844 }
14845 
14846 static void ath12k_mac_setup(struct ath12k *ar)
14847 {
14848 	struct ath12k_base *ab = ar->ab;
14849 	struct ath12k_pdev *pdev = ar->pdev;
14850 	u8 pdev_idx = ar->pdev_idx;
14851 
14852 	ar->lmac_id = ath12k_hw_get_mac_from_pdev_id(ab->hw_params, pdev_idx);
14853 
14854 	ar->wmi = &ab->wmi_ab.wmi[pdev_idx];
14855 	/* FIXME: wmi[0] is already initialized during attach,
14856 	 * Should we do this again?
14857 	 */
14858 	ath12k_wmi_pdev_attach(ab, pdev_idx);
14859 
14860 	ar->cfg_tx_chainmask = pdev->cap.tx_chain_mask;
14861 	ar->cfg_rx_chainmask = pdev->cap.rx_chain_mask;
14862 	ar->num_tx_chains = hweight32(pdev->cap.tx_chain_mask);
14863 	ar->num_rx_chains = hweight32(pdev->cap.rx_chain_mask);
14864 	ar->scan.arvif = NULL;
14865 	ar->vdev_id_11d_scan = ATH12K_11D_INVALID_VDEV_ID;
14866 
14867 	spin_lock_init(&ar->data_lock);
14868 	spin_lock_init(&ar->dp.ppdu_list_lock);
14869 	INIT_LIST_HEAD(&ar->arvifs);
14870 	INIT_LIST_HEAD(&ar->dp.ppdu_stats_info);
14871 
14872 	init_completion(&ar->vdev_setup_done);
14873 	init_completion(&ar->vdev_delete_done);
14874 	init_completion(&ar->peer_assoc_done);
14875 	init_completion(&ar->peer_delete_done);
14876 	init_completion(&ar->install_key_done);
14877 	init_completion(&ar->bss_survey_done);
14878 	init_completion(&ar->scan.started);
14879 	init_completion(&ar->scan.completed);
14880 	init_completion(&ar->scan.on_channel);
14881 	init_completion(&ar->mlo_setup_done);
14882 	init_completion(&ar->completed_11d_scan);
14883 	init_completion(&ar->regd_update_completed);
14884 	init_completion(&ar->thermal.wmi_sync);
14885 	mutex_init(&ar->thermal.lock);
14886 
14887 	ar->thermal.temperature = 0;
14888 	ar->thermal.hwmon_dev = NULL;
14889 
14890 	INIT_DELAYED_WORK(&ar->scan.timeout, ath12k_scan_timeout_work);
14891 	wiphy_work_init(&ar->scan.vdev_clean_wk, ath12k_scan_vdev_clean_work);
14892 	INIT_WORK(&ar->regd_channel_update_work, ath12k_regd_update_chan_list_work);
14893 	INIT_LIST_HEAD(&ar->regd_channel_update_queue);
14894 	INIT_WORK(&ar->regd_update_work, ath12k_regd_update_work);
14895 
14896 	wiphy_work_init(&ar->wmi_mgmt_tx_work, ath12k_mgmt_over_wmi_tx_work);
14897 	skb_queue_head_init(&ar->wmi_mgmt_tx_queue);
14898 
14899 	ar->monitor_vdev_id = -1;
14900 	ar->monitor_vdev_created = false;
14901 	ar->monitor_started = false;
14902 }
14903 
14904 static int __ath12k_mac_mlo_setup(struct ath12k *ar)
14905 {
14906 	u8 num_link = 0, partner_link_id[ATH12K_GROUP_MAX_RADIO] = {};
14907 	struct ath12k_base *partner_ab, *ab = ar->ab;
14908 	struct ath12k_hw_group *ag = ab->ag;
14909 	struct wmi_mlo_setup_arg mlo = {};
14910 	struct ath12k_pdev *pdev;
14911 	unsigned long time_left;
14912 	int i, j, ret;
14913 
14914 	lockdep_assert_held(&ag->mutex);
14915 
14916 	reinit_completion(&ar->mlo_setup_done);
14917 
14918 	for (i = 0; i < ag->num_devices; i++) {
14919 		partner_ab = ag->ab[i];
14920 
14921 		for (j = 0; j < partner_ab->num_radios; j++) {
14922 			pdev = &partner_ab->pdevs[j];
14923 
14924 			/* Avoid the self link */
14925 			if (ar == pdev->ar)
14926 				continue;
14927 
14928 			partner_link_id[num_link] = pdev->hw_link_id;
14929 			num_link++;
14930 
14931 			ath12k_dbg(ab, ATH12K_DBG_MAC, "device %d pdev %d hw_link_id %d num_link %d\n",
14932 				   i, j, pdev->hw_link_id, num_link);
14933 		}
14934 	}
14935 
14936 	if (num_link == 0)
14937 		return 0;
14938 
14939 	mlo.group_id = cpu_to_le32(ag->id);
14940 	mlo.partner_link_id = partner_link_id;
14941 	mlo.num_partner_links = num_link;
14942 	ar->mlo_setup_status = 0;
14943 
14944 	ath12k_dbg(ab, ATH12K_DBG_MAC, "group id %d num_link %d\n", ag->id, num_link);
14945 
14946 	ret = ath12k_wmi_mlo_setup(ar, &mlo);
14947 	if (ret) {
14948 		ath12k_err(ab, "failed to send  setup MLO WMI command for pdev %d: %d\n",
14949 			   ar->pdev_idx, ret);
14950 		return ret;
14951 	}
14952 
14953 	time_left = wait_for_completion_timeout(&ar->mlo_setup_done,
14954 						WMI_MLO_CMD_TIMEOUT_HZ);
14955 
14956 	if (!time_left || ar->mlo_setup_status)
14957 		return ar->mlo_setup_status ? : -ETIMEDOUT;
14958 
14959 	ath12k_dbg(ab, ATH12K_DBG_MAC, "mlo setup done for pdev %d\n", ar->pdev_idx);
14960 
14961 	return 0;
14962 }
14963 
14964 static int __ath12k_mac_mlo_teardown(struct ath12k *ar)
14965 {
14966 	struct ath12k_base *ab = ar->ab;
14967 	int ret;
14968 	u8 num_link;
14969 
14970 	if (test_bit(ATH12K_FLAG_RECOVERY, &ab->dev_flags))
14971 		return 0;
14972 
14973 	num_link = ath12k_get_num_partner_link(ar);
14974 
14975 	if (num_link == 0)
14976 		return 0;
14977 
14978 	ret = ath12k_wmi_mlo_teardown(ar);
14979 	if (ret) {
14980 		ath12k_warn(ab, "failed to send MLO teardown WMI command for pdev %d: %d\n",
14981 			    ar->pdev_idx, ret);
14982 		return ret;
14983 	}
14984 
14985 	ath12k_dbg(ab, ATH12K_DBG_MAC, "mlo teardown for pdev %d\n", ar->pdev_idx);
14986 
14987 	return 0;
14988 }
14989 
14990 int ath12k_mac_mlo_setup(struct ath12k_hw_group *ag)
14991 {
14992 	struct ath12k_hw *ah;
14993 	struct ath12k *ar;
14994 	int ret;
14995 	int i, j;
14996 
14997 	for (i = 0; i < ag->num_hw; i++) {
14998 		ah = ag->ah[i];
14999 		if (!ah)
15000 			continue;
15001 
15002 		for_each_ar(ah, ar, j) {
15003 			ar = &ah->radio[j];
15004 			ret = __ath12k_mac_mlo_setup(ar);
15005 			if (ret) {
15006 				ath12k_err(ar->ab, "failed to setup MLO: %d\n", ret);
15007 				goto err_setup;
15008 			}
15009 		}
15010 	}
15011 
15012 	return 0;
15013 
15014 err_setup:
15015 	for (i = i - 1; i >= 0; i--) {
15016 		ah = ag->ah[i];
15017 		if (!ah)
15018 			continue;
15019 
15020 		for (j = j - 1; j >= 0; j--) {
15021 			ar = &ah->radio[j];
15022 			if (!ar)
15023 				continue;
15024 
15025 			__ath12k_mac_mlo_teardown(ar);
15026 		}
15027 	}
15028 
15029 	return ret;
15030 }
15031 
15032 void ath12k_mac_mlo_teardown(struct ath12k_hw_group *ag)
15033 {
15034 	struct ath12k_hw *ah;
15035 	struct ath12k *ar;
15036 	int ret, i, j;
15037 
15038 	for (i = 0; i < ag->num_hw; i++) {
15039 		ah = ag->ah[i];
15040 		if (!ah)
15041 			continue;
15042 
15043 		for_each_ar(ah, ar, j) {
15044 			ar = &ah->radio[j];
15045 			ret = __ath12k_mac_mlo_teardown(ar);
15046 			if (ret) {
15047 				ath12k_err(ar->ab, "failed to teardown MLO: %d\n", ret);
15048 				break;
15049 			}
15050 		}
15051 	}
15052 }
15053 
15054 int ath12k_mac_register(struct ath12k_hw_group *ag)
15055 {
15056 	struct ath12k_hw *ah;
15057 	int i;
15058 	int ret;
15059 
15060 	for (i = 0; i < ag->num_hw; i++) {
15061 		ah = ath12k_ag_to_ah(ag, i);
15062 
15063 		ret = ath12k_mac_hw_register(ah);
15064 		if (ret)
15065 			goto err;
15066 	}
15067 
15068 	return 0;
15069 
15070 err:
15071 	for (i = i - 1; i >= 0; i--) {
15072 		ah = ath12k_ag_to_ah(ag, i);
15073 		if (!ah)
15074 			continue;
15075 
15076 		ath12k_mac_hw_unregister(ah);
15077 	}
15078 
15079 	return ret;
15080 }
15081 
15082 void ath12k_mac_unregister(struct ath12k_hw_group *ag)
15083 {
15084 	struct ath12k_hw *ah;
15085 	int i;
15086 
15087 	for (i = ag->num_hw - 1; i >= 0; i--) {
15088 		ah = ath12k_ag_to_ah(ag, i);
15089 		if (!ah)
15090 			continue;
15091 
15092 		ath12k_mac_hw_unregister(ah);
15093 	}
15094 }
15095 
15096 static void ath12k_mac_hw_destroy(struct ath12k_hw *ah)
15097 {
15098 	ieee80211_free_hw(ah->hw);
15099 }
15100 
15101 static struct ath12k_hw *ath12k_mac_hw_allocate(struct ath12k_hw_group *ag,
15102 						struct ath12k_pdev_map *pdev_map,
15103 						u8 num_pdev_map)
15104 {
15105 	struct ieee80211_hw *hw;
15106 	struct ath12k *ar;
15107 	struct ath12k_base *ab;
15108 	struct ath12k_pdev *pdev;
15109 	struct ath12k_hw *ah;
15110 	int i;
15111 	u8 pdev_idx;
15112 
15113 	hw = ieee80211_alloc_hw(struct_size(ah, radio, num_pdev_map),
15114 				pdev_map->ab->ath12k_ops);
15115 	if (!hw)
15116 		return NULL;
15117 
15118 	ah = ath12k_hw_to_ah(hw);
15119 	ah->hw = hw;
15120 	ah->num_radio = num_pdev_map;
15121 
15122 	mutex_init(&ah->hw_mutex);
15123 
15124 	spin_lock_init(&ah->dp_hw.peer_lock);
15125 	INIT_LIST_HEAD(&ah->dp_hw.dp_peers_list);
15126 
15127 	for (i = 0; i < num_pdev_map; i++) {
15128 		ab = pdev_map[i].ab;
15129 		pdev_idx = pdev_map[i].pdev_idx;
15130 		pdev = &ab->pdevs[pdev_idx];
15131 
15132 		ar = ath12k_ah_to_ar(ah, i);
15133 		ar->ah = ah;
15134 		ar->ab = ab;
15135 		ar->hw_link_id = pdev->hw_link_id;
15136 		ar->pdev = pdev;
15137 		ar->pdev_idx = pdev_idx;
15138 		ar->radio_idx = i;
15139 		pdev->ar = ar;
15140 
15141 		ag->hw_links[ar->hw_link_id].device_id = ab->device_id;
15142 		ag->hw_links[ar->hw_link_id].pdev_idx = pdev_idx;
15143 
15144 		ath12k_mac_setup(ar);
15145 		ath12k_dp_pdev_pre_alloc(ar);
15146 	}
15147 
15148 	return ah;
15149 }
15150 
15151 void ath12k_mac_destroy(struct ath12k_hw_group *ag)
15152 {
15153 	struct ath12k_pdev *pdev;
15154 	struct ath12k_base *ab = ag->ab[0];
15155 	int i, j;
15156 	struct ath12k_hw *ah;
15157 
15158 	for (i = 0; i < ag->num_devices; i++) {
15159 		ab = ag->ab[i];
15160 		if (!ab)
15161 			continue;
15162 
15163 		for (j = 0; j < ab->num_radios; j++) {
15164 			pdev = &ab->pdevs[j];
15165 			if (!pdev->ar)
15166 				continue;
15167 			pdev->ar = NULL;
15168 		}
15169 	}
15170 
15171 	for (i = 0; i < ag->num_hw; i++) {
15172 		ah = ath12k_ag_to_ah(ag, i);
15173 		if (!ah)
15174 			continue;
15175 
15176 		ath12k_mac_hw_destroy(ah);
15177 		ath12k_ag_set_ah(ag, i, NULL);
15178 	}
15179 }
15180 
15181 static void ath12k_mac_set_device_defaults(struct ath12k_base *ab)
15182 {
15183 	int total_vdev;
15184 
15185 	/* Initialize channel counters frequency value in hertz */
15186 	ab->cc_freq_hz = 320000;
15187 	total_vdev = ab->num_radios * TARGET_NUM_VDEVS(ab);
15188 	ab->free_vdev_map = (1LL << total_vdev) - 1;
15189 }
15190 
15191 int ath12k_mac_allocate(struct ath12k_hw_group *ag)
15192 {
15193 	struct ath12k_pdev_map pdev_map[ATH12K_GROUP_MAX_RADIO];
15194 	int mac_id, device_id, total_radio, num_hw;
15195 	struct ath12k_base *ab;
15196 	struct ath12k_hw *ah;
15197 	int ret, i, j;
15198 	u8 radio_per_hw;
15199 
15200 	total_radio = 0;
15201 	for (i = 0; i < ag->num_devices; i++) {
15202 		ab = ag->ab[i];
15203 		if (!ab)
15204 			continue;
15205 
15206 		ath12k_mac_set_device_defaults(ab);
15207 		total_radio += ab->num_radios;
15208 	}
15209 
15210 	if (!total_radio)
15211 		return -EINVAL;
15212 
15213 	if (WARN_ON(total_radio > ATH12K_GROUP_MAX_RADIO))
15214 		return -ENOSPC;
15215 
15216 	/* All pdev get combined and register as single wiphy based on
15217 	 * hardware group which participate in multi-link operation else
15218 	 * each pdev get register separately.
15219 	 */
15220 	if (ag->mlo_capable)
15221 		radio_per_hw = total_radio;
15222 	else
15223 		radio_per_hw = 1;
15224 
15225 	num_hw = total_radio / radio_per_hw;
15226 
15227 	ag->num_hw = 0;
15228 	device_id = 0;
15229 	mac_id = 0;
15230 	for (i = 0; i < num_hw; i++) {
15231 		for (j = 0; j < radio_per_hw; j++) {
15232 			if (device_id >= ag->num_devices || !ag->ab[device_id]) {
15233 				ret = -ENOSPC;
15234 				goto err;
15235 			}
15236 
15237 			ab = ag->ab[device_id];
15238 			pdev_map[j].ab = ab;
15239 			pdev_map[j].pdev_idx = mac_id;
15240 			mac_id++;
15241 
15242 			/* If mac_id falls beyond the current device MACs then
15243 			 * move to next device
15244 			 */
15245 			if (mac_id >= ab->num_radios) {
15246 				mac_id = 0;
15247 				device_id++;
15248 			}
15249 		}
15250 
15251 		ab = pdev_map->ab;
15252 
15253 		ah = ath12k_mac_hw_allocate(ag, pdev_map, radio_per_hw);
15254 		if (!ah) {
15255 			ath12k_warn(ab, "failed to allocate mac80211 hw device for hw_idx %d\n",
15256 				    i);
15257 			ret = -ENOMEM;
15258 			goto err;
15259 		}
15260 
15261 		ah->dev = ab->dev;
15262 
15263 		ag->ah[i] = ah;
15264 		ag->num_hw++;
15265 	}
15266 
15267 	return 0;
15268 
15269 err:
15270 	for (i = i - 1; i >= 0; i--) {
15271 		ah = ath12k_ag_to_ah(ag, i);
15272 		if (!ah)
15273 			continue;
15274 
15275 		ath12k_mac_hw_destroy(ah);
15276 		ath12k_ag_set_ah(ag, i, NULL);
15277 	}
15278 
15279 	return ret;
15280 }
15281 
15282 int ath12k_mac_vif_set_keepalive(struct ath12k_link_vif *arvif,
15283 				 enum wmi_sta_keepalive_method method,
15284 				 u32 interval)
15285 {
15286 	struct wmi_sta_keepalive_arg arg = {};
15287 	struct ath12k *ar = arvif->ar;
15288 	int ret;
15289 
15290 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
15291 
15292 	if (arvif->ahvif->vdev_type != WMI_VDEV_TYPE_STA)
15293 		return 0;
15294 
15295 	if (!test_bit(WMI_TLV_SERVICE_STA_KEEP_ALIVE, ar->ab->wmi_ab.svc_map))
15296 		return 0;
15297 
15298 	arg.vdev_id = arvif->vdev_id;
15299 	arg.enabled = 1;
15300 	arg.method = method;
15301 	arg.interval = interval;
15302 
15303 	ret = ath12k_wmi_sta_keepalive(ar, &arg);
15304 	if (ret) {
15305 		ath12k_warn(ar->ab, "failed to set keepalive on vdev %i: %d\n",
15306 			    arvif->vdev_id, ret);
15307 		return ret;
15308 	}
15309 
15310 	return 0;
15311 }
15312