xref: /linux/drivers/net/wireless/ath/ath12k/mac.c (revision 6d64002310f86def404bd83d8824dee465bed482)
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_QMI_FIRMWARE_MODE_FTM)
863 			pdev = &ab->pdevs[i];
864 		else
865 			pdev = rcu_dereference(ab->pdevs_active[i]);
866 
867 		if (pdev && pdev->pdev_id == pdev_id)
868 			return (pdev->ar ? pdev->ar : NULL);
869 	}
870 
871 	return NULL;
872 }
873 
874 static bool ath12k_mac_is_ml_arvif(struct ath12k_link_vif *arvif)
875 {
876 	struct ath12k_vif *ahvif = arvif->ahvif;
877 
878 	lockdep_assert_wiphy(ahvif->ah->hw->wiphy);
879 
880 	if (ahvif->vif->valid_links & BIT(arvif->link_id))
881 		return true;
882 
883 	return false;
884 }
885 
886 static struct ath12k *ath12k_mac_get_ar_by_chan(struct ieee80211_hw *hw,
887 						struct ieee80211_channel *channel)
888 {
889 	struct ath12k_hw *ah = hw->priv;
890 	struct ath12k *ar;
891 	int i;
892 
893 	ar = ah->radio;
894 
895 	if (ah->num_radio == 1)
896 		return ar;
897 
898 	for_each_ar(ah, ar, i) {
899 		if (channel->center_freq >= KHZ_TO_MHZ(ar->freq_range.start_freq) &&
900 		    channel->center_freq <= KHZ_TO_MHZ(ar->freq_range.end_freq))
901 			return ar;
902 	}
903 	return NULL;
904 }
905 
906 static struct ath12k *ath12k_get_ar_by_ctx(struct ieee80211_hw *hw,
907 					   struct ieee80211_chanctx_conf *ctx)
908 {
909 	if (!ctx)
910 		return NULL;
911 
912 	return ath12k_mac_get_ar_by_chan(hw, ctx->def.chan);
913 }
914 
915 struct ath12k *ath12k_get_ar_by_vif(struct ieee80211_hw *hw,
916 				    struct ieee80211_vif *vif,
917 				    u8 link_id)
918 {
919 	struct ath12k_vif *ahvif = ath12k_vif_to_ahvif(vif);
920 	struct ath12k_hw *ah = ath12k_hw_to_ah(hw);
921 	struct ath12k_link_vif *arvif;
922 
923 	lockdep_assert_wiphy(hw->wiphy);
924 
925 	/* If there is one pdev within ah, then we return
926 	 * ar directly.
927 	 */
928 	if (ah->num_radio == 1)
929 		return ah->radio;
930 
931 	if (!(ahvif->links_map & BIT(link_id)))
932 		return NULL;
933 
934 	arvif = wiphy_dereference(hw->wiphy, ahvif->link[link_id]);
935 	if (arvif && arvif->is_created)
936 		return arvif->ar;
937 
938 	return NULL;
939 }
940 
941 void ath12k_mac_get_any_chanctx_conf_iter(struct ieee80211_hw *hw,
942 					  struct ieee80211_chanctx_conf *conf,
943 					  void *data)
944 {
945 	struct ath12k_mac_get_any_chanctx_conf_arg *arg = data;
946 	struct ath12k *ctx_ar = ath12k_get_ar_by_ctx(hw, conf);
947 
948 	if (ctx_ar == arg->ar)
949 		arg->chanctx_conf = conf;
950 }
951 
952 static struct ath12k_link_vif *ath12k_mac_get_vif_up(struct ath12k *ar)
953 {
954 	struct ath12k_link_vif *arvif;
955 
956 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
957 
958 	list_for_each_entry(arvif, &ar->arvifs, list) {
959 		if (arvif->is_up)
960 			return arvif;
961 	}
962 
963 	return NULL;
964 }
965 
966 static bool ath12k_mac_band_match(enum nl80211_band band1, enum WMI_HOST_WLAN_BAND band2)
967 {
968 	switch (band1) {
969 	case NL80211_BAND_2GHZ:
970 		if (band2 & WMI_HOST_WLAN_2GHZ_CAP)
971 			return true;
972 		break;
973 	case NL80211_BAND_5GHZ:
974 	case NL80211_BAND_6GHZ:
975 		if (band2 & WMI_HOST_WLAN_5GHZ_CAP)
976 			return true;
977 		break;
978 	default:
979 		return false;
980 	}
981 
982 	return false;
983 }
984 
985 static u8 ath12k_mac_get_target_pdev_id_from_vif(struct ath12k_link_vif *arvif)
986 {
987 	struct ath12k *ar = arvif->ar;
988 	struct ath12k_base *ab = ar->ab;
989 	struct ieee80211_vif *vif = arvif->ahvif->vif;
990 	struct cfg80211_chan_def def;
991 	enum nl80211_band band;
992 	u8 pdev_id = ab->fw_pdev[0].pdev_id;
993 	int i;
994 
995 	if (WARN_ON(ath12k_mac_vif_link_chan(vif, arvif->link_id, &def)))
996 		return pdev_id;
997 
998 	band = def.chan->band;
999 
1000 	for (i = 0; i < ab->fw_pdev_count; i++) {
1001 		if (ath12k_mac_band_match(band, ab->fw_pdev[i].supported_bands))
1002 			return ab->fw_pdev[i].pdev_id;
1003 	}
1004 
1005 	return pdev_id;
1006 }
1007 
1008 u8 ath12k_mac_get_target_pdev_id(struct ath12k *ar)
1009 {
1010 	struct ath12k_link_vif *arvif;
1011 	struct ath12k_base *ab = ar->ab;
1012 
1013 	if (!ab->hw_params->single_pdev_only)
1014 		return ar->pdev->pdev_id;
1015 
1016 	arvif = ath12k_mac_get_vif_up(ar);
1017 
1018 	/* fw_pdev array has pdev ids derived from phy capability
1019 	 * service ready event (pdev_and_hw_link_ids).
1020 	 * If no vif is active, return default first index.
1021 	 */
1022 	if (!arvif)
1023 		return ar->ab->fw_pdev[0].pdev_id;
1024 
1025 	/* If active vif is found, return the pdev id matching chandef band */
1026 	return ath12k_mac_get_target_pdev_id_from_vif(arvif);
1027 }
1028 
1029 static void ath12k_pdev_caps_update(struct ath12k *ar)
1030 {
1031 	struct ath12k_base *ab = ar->ab;
1032 
1033 	ar->max_tx_power = ab->target_caps.hw_max_tx_power;
1034 
1035 	/* FIXME: Set min_tx_power to ab->target_caps.hw_min_tx_power.
1036 	 * But since the received value in svcrdy is same as hw_max_tx_power,
1037 	 * we can set ar->min_tx_power to 0 currently until
1038 	 * this is fixed in firmware
1039 	 */
1040 	ar->min_tx_power = 0;
1041 
1042 	ar->txpower_limit_2g = ar->max_tx_power;
1043 	ar->txpower_limit_5g = ar->max_tx_power;
1044 	ar->txpower_scale = WMI_HOST_TP_SCALE_MAX;
1045 }
1046 
1047 static int ath12k_mac_txpower_recalc(struct ath12k *ar)
1048 {
1049 	struct ath12k_pdev *pdev = ar->pdev;
1050 	struct ath12k_link_vif *arvif;
1051 	int ret, txpower = -1;
1052 	u32 param;
1053 
1054 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
1055 
1056 	list_for_each_entry(arvif, &ar->arvifs, list) {
1057 		if (arvif->txpower <= 0)
1058 			continue;
1059 
1060 		if (txpower == -1)
1061 			txpower = arvif->txpower;
1062 		else
1063 			txpower = min(txpower, arvif->txpower);
1064 	}
1065 
1066 	if (txpower == -1)
1067 		return 0;
1068 
1069 	/* txpwr is set as 2 units per dBm in FW*/
1070 	txpower = min_t(u32, max_t(u32, ar->min_tx_power, txpower),
1071 			ar->max_tx_power) * 2;
1072 
1073 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "txpower to set in hw %d\n",
1074 		   txpower / 2);
1075 
1076 	if ((pdev->cap.supported_bands & WMI_HOST_WLAN_2GHZ_CAP) &&
1077 	    ar->txpower_limit_2g != txpower) {
1078 		param = WMI_PDEV_PARAM_TXPOWER_LIMIT2G;
1079 		ret = ath12k_wmi_pdev_set_param(ar, param,
1080 						txpower, ar->pdev->pdev_id);
1081 		if (ret)
1082 			goto fail;
1083 		ar->txpower_limit_2g = txpower;
1084 	}
1085 
1086 	if ((pdev->cap.supported_bands & WMI_HOST_WLAN_5GHZ_CAP) &&
1087 	    ar->txpower_limit_5g != txpower) {
1088 		param = WMI_PDEV_PARAM_TXPOWER_LIMIT5G;
1089 		ret = ath12k_wmi_pdev_set_param(ar, param,
1090 						txpower, ar->pdev->pdev_id);
1091 		if (ret)
1092 			goto fail;
1093 		ar->txpower_limit_5g = txpower;
1094 	}
1095 
1096 	return 0;
1097 
1098 fail:
1099 	ath12k_warn(ar->ab, "failed to recalc txpower limit %d using pdev param %d: %d\n",
1100 		    txpower / 2, param, ret);
1101 	return ret;
1102 }
1103 
1104 static int ath12k_recalc_rtscts_prot(struct ath12k_link_vif *arvif)
1105 {
1106 	struct ath12k *ar = arvif->ar;
1107 	u32 vdev_param, rts_cts;
1108 	int ret;
1109 
1110 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
1111 
1112 	vdev_param = WMI_VDEV_PARAM_ENABLE_RTSCTS;
1113 
1114 	/* Enable RTS/CTS protection for sw retries (when legacy stations
1115 	 * are in BSS) or by default only for second rate series.
1116 	 * TODO: Check if we need to enable CTS 2 Self in any case
1117 	 */
1118 	rts_cts = WMI_USE_RTS_CTS;
1119 
1120 	if (arvif->num_legacy_stations > 0)
1121 		rts_cts |= WMI_RTSCTS_ACROSS_SW_RETRIES << 4;
1122 	else
1123 		rts_cts |= WMI_RTSCTS_FOR_SECOND_RATESERIES << 4;
1124 
1125 	/* Need not send duplicate param value to firmware */
1126 	if (arvif->rtscts_prot_mode == rts_cts)
1127 		return 0;
1128 
1129 	arvif->rtscts_prot_mode = rts_cts;
1130 
1131 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac vdev %d recalc rts/cts prot %d\n",
1132 		   arvif->vdev_id, rts_cts);
1133 
1134 	ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
1135 					    vdev_param, rts_cts);
1136 	if (ret)
1137 		ath12k_warn(ar->ab, "failed to recalculate rts/cts prot for vdev %d: %d\n",
1138 			    arvif->vdev_id, ret);
1139 
1140 	return ret;
1141 }
1142 
1143 static int ath12k_mac_set_kickout(struct ath12k_link_vif *arvif)
1144 {
1145 	struct ath12k *ar = arvif->ar;
1146 	u32 param;
1147 	int ret;
1148 
1149 	ret = ath12k_wmi_pdev_set_param(ar, WMI_PDEV_PARAM_STA_KICKOUT_TH,
1150 					ATH12K_KICKOUT_THRESHOLD,
1151 					ar->pdev->pdev_id);
1152 	if (ret) {
1153 		ath12k_warn(ar->ab, "failed to set kickout threshold on vdev %i: %d\n",
1154 			    arvif->vdev_id, ret);
1155 		return ret;
1156 	}
1157 
1158 	param = WMI_VDEV_PARAM_AP_KEEPALIVE_MIN_IDLE_INACTIVE_TIME_SECS;
1159 	ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id, param,
1160 					    ATH12K_KEEPALIVE_MIN_IDLE);
1161 	if (ret) {
1162 		ath12k_warn(ar->ab, "failed to set keepalive minimum idle time on vdev %i: %d\n",
1163 			    arvif->vdev_id, ret);
1164 		return ret;
1165 	}
1166 
1167 	param = WMI_VDEV_PARAM_AP_KEEPALIVE_MAX_IDLE_INACTIVE_TIME_SECS;
1168 	ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id, param,
1169 					    ATH12K_KEEPALIVE_MAX_IDLE);
1170 	if (ret) {
1171 		ath12k_warn(ar->ab, "failed to set keepalive maximum idle time on vdev %i: %d\n",
1172 			    arvif->vdev_id, ret);
1173 		return ret;
1174 	}
1175 
1176 	param = WMI_VDEV_PARAM_AP_KEEPALIVE_MAX_UNRESPONSIVE_TIME_SECS;
1177 	ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id, param,
1178 					    ATH12K_KEEPALIVE_MAX_UNRESPONSIVE);
1179 	if (ret) {
1180 		ath12k_warn(ar->ab, "failed to set keepalive maximum unresponsive time on vdev %i: %d\n",
1181 			    arvif->vdev_id, ret);
1182 		return ret;
1183 	}
1184 
1185 	return 0;
1186 }
1187 
1188 static void ath12k_mac_link_sta_rhash_cleanup(void *data, struct ieee80211_sta *sta)
1189 {
1190 	u8 link_id;
1191 	unsigned long links_map;
1192 	struct ath12k_sta *ahsta;
1193 	struct ath12k *ar = data;
1194 	struct ath12k_link_sta *arsta;
1195 	struct ath12k_link_vif *arvif;
1196 	struct ath12k_base *ab = ar->ab;
1197 
1198 	ahsta = ath12k_sta_to_ahsta(sta);
1199 	links_map = ahsta->links_map;
1200 
1201 	rcu_read_lock();
1202 	for_each_set_bit(link_id, &links_map, IEEE80211_MLD_MAX_NUM_LINKS) {
1203 		arsta = rcu_dereference(ahsta->link[link_id]);
1204 		if (!arsta)
1205 			continue;
1206 		arvif = arsta->arvif;
1207 		if (!(arvif->ar == ar))
1208 			continue;
1209 
1210 		spin_lock_bh(&ab->base_lock);
1211 		ath12k_link_sta_rhash_delete(ab, arsta);
1212 		spin_unlock_bh(&ab->base_lock);
1213 	}
1214 	rcu_read_unlock();
1215 }
1216 
1217 void ath12k_mac_peer_cleanup_all(struct ath12k *ar)
1218 {
1219 	struct ath12k_dp_link_peer *peer, *tmp;
1220 	struct ath12k_base *ab = ar->ab;
1221 	struct ath12k_dp *dp = ath12k_ab_to_dp(ab);
1222 	struct ath12k_link_vif *arvif, *tmp_vif;
1223 	struct ath12k_dp_hw *dp_hw = &ar->ah->dp_hw;
1224 	struct ath12k_dp_peer *dp_peer = NULL;
1225 	u16 peerid_index;
1226 	struct list_head peers;
1227 
1228 	INIT_LIST_HEAD(&peers);
1229 
1230 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
1231 
1232 	spin_lock_bh(&dp->dp_lock);
1233 	list_for_each_entry_safe(peer, tmp, &dp->peers, list) {
1234 		/* Skip Rx TID cleanup for self peer */
1235 		if (peer->sta && peer->dp_peer)
1236 			ath12k_dp_rx_peer_tid_cleanup(ar, peer);
1237 
1238 		/* cleanup dp peer */
1239 		spin_lock_bh(&dp_hw->peer_lock);
1240 		dp_peer = peer->dp_peer;
1241 		if (dp_peer) {
1242 			peerid_index = ath12k_dp_peer_get_peerid_index(dp, peer->peer_id);
1243 			rcu_assign_pointer(dp_peer->link_peers[peer->link_id], NULL);
1244 			WRITE_ONCE(dp_peer->link_peers_map,
1245 				   READ_ONCE(dp_peer->link_peers_map) & ~BIT(peer->link_id));
1246 			rcu_assign_pointer(dp_hw->dp_peers[peerid_index], NULL);
1247 		}
1248 		spin_unlock_bh(&dp_hw->peer_lock);
1249 
1250 		ath12k_dp_link_peer_rhash_delete(dp, peer);
1251 
1252 		list_move(&peer->list, &peers);
1253 	}
1254 	spin_unlock_bh(&dp->dp_lock);
1255 
1256 	synchronize_rcu();
1257 
1258 	list_for_each_entry_safe(peer, tmp, &peers, list) {
1259 		ath12k_dp_link_peer_free(peer);
1260 	}
1261 
1262 	ar->num_peers = 0;
1263 	ar->num_stations = 0;
1264 
1265 	/* Cleanup rhash table maintained for arsta by iterating over sta */
1266 	ieee80211_iterate_stations_mtx(ar->ah->hw, ath12k_mac_link_sta_rhash_cleanup,
1267 				       ar);
1268 
1269 	/* Delete all the self dp_peers on asserted radio */
1270 	list_for_each_entry_safe_reverse(arvif, tmp_vif, &ar->arvifs, list) {
1271 		if ((arvif->ahvif->vdev_type == WMI_VDEV_TYPE_AP) &&
1272 		    (arvif->link_id < IEEE80211_MLD_MAX_NUM_LINKS)) {
1273 			ath12k_dp_peer_delete(dp_hw, arvif->bssid, NULL);
1274 			arvif->num_stations = 0;
1275 		}
1276 	}
1277 }
1278 
1279 void ath12k_mac_dp_peer_cleanup(struct ath12k_hw *ah)
1280 {
1281 	struct list_head peers;
1282 	struct ath12k_dp_peer *dp_peer, *tmp;
1283 	struct ath12k_dp_hw *dp_hw = &ah->dp_hw;
1284 
1285 	lockdep_assert_wiphy(ah->hw->wiphy);
1286 
1287 	INIT_LIST_HEAD(&peers);
1288 
1289 	spin_lock_bh(&dp_hw->peer_lock);
1290 	list_for_each_entry_safe(dp_peer, tmp, &dp_hw->dp_peers_list, list) {
1291 		if (dp_peer->is_mlo) {
1292 			if (dp_peer->peer_id != ATH12K_MLO_PEER_ID_PENDING)
1293 				rcu_assign_pointer(dp_hw->dp_peers[dp_peer->peer_id],
1294 						   NULL);
1295 			ath12k_peer_ml_free(ah, ath12k_sta_to_ahsta(dp_peer->sta));
1296 		}
1297 
1298 		list_move(&dp_peer->list, &peers);
1299 	}
1300 
1301 	spin_unlock_bh(&dp_hw->peer_lock);
1302 
1303 	synchronize_rcu();
1304 
1305 	list_for_each_entry_safe(dp_peer, tmp, &peers, list) {
1306 		list_del(&dp_peer->list);
1307 		kfree(dp_peer);
1308 	}
1309 }
1310 
1311 static int ath12k_mac_vdev_setup_sync(struct ath12k *ar)
1312 {
1313 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
1314 
1315 	if (test_bit(ATH12K_FLAG_CRASH_FLUSH, &ar->ab->dev_flags))
1316 		return -ESHUTDOWN;
1317 
1318 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "vdev setup timeout %d\n",
1319 		   ATH12K_VDEV_SETUP_TIMEOUT_HZ);
1320 
1321 	if (!wait_for_completion_timeout(&ar->vdev_setup_done,
1322 					 ATH12K_VDEV_SETUP_TIMEOUT_HZ))
1323 		return -ETIMEDOUT;
1324 
1325 	return ar->last_wmi_vdev_start_status ? -EINVAL : 0;
1326 }
1327 
1328 static int ath12k_monitor_vdev_up(struct ath12k *ar, int vdev_id)
1329 {
1330 	struct ath12k_wmi_vdev_up_params params = {};
1331 	int ret;
1332 
1333 	params.vdev_id = vdev_id;
1334 	params.bssid = ar->mac_addr;
1335 	ret = ath12k_wmi_vdev_up(ar, &params);
1336 	if (ret) {
1337 		ath12k_warn(ar->ab, "failed to put up monitor vdev %i: %d\n",
1338 			    vdev_id, ret);
1339 		return ret;
1340 	}
1341 
1342 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac monitor vdev %i started\n",
1343 		   vdev_id);
1344 	return 0;
1345 }
1346 
1347 static int ath12k_mac_monitor_vdev_start(struct ath12k *ar, int vdev_id,
1348 					 struct cfg80211_chan_def *chandef)
1349 {
1350 	struct ieee80211_channel *channel;
1351 	struct wmi_vdev_start_req_arg arg = {};
1352 	struct ath12k_wmi_vdev_up_params params = {};
1353 	int ret;
1354 
1355 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
1356 
1357 	channel = chandef->chan;
1358 	arg.vdev_id = vdev_id;
1359 	arg.freq = channel->center_freq;
1360 	arg.band_center_freq1 = chandef->center_freq1;
1361 	arg.band_center_freq2 = chandef->center_freq2;
1362 	arg.mode = ath12k_phymodes[chandef->chan->band][chandef->width];
1363 	arg.chan_radar = !!(channel->flags & IEEE80211_CHAN_RADAR);
1364 
1365 	arg.min_power = 0;
1366 	arg.max_power = channel->max_power;
1367 	arg.max_reg_power = channel->max_reg_power;
1368 	arg.max_antenna_gain = channel->max_antenna_gain;
1369 
1370 	arg.pref_tx_streams = ar->num_tx_chains;
1371 	arg.pref_rx_streams = ar->num_rx_chains;
1372 	arg.punct_bitmap = 0xFFFFFFFF;
1373 
1374 	arg.passive |= !!(chandef->chan->flags & IEEE80211_CHAN_NO_IR);
1375 
1376 	reinit_completion(&ar->vdev_setup_done);
1377 	reinit_completion(&ar->vdev_delete_done);
1378 
1379 	ret = ath12k_wmi_vdev_start(ar, &arg, false);
1380 	if (ret) {
1381 		ath12k_warn(ar->ab, "failed to request monitor vdev %i start: %d\n",
1382 			    vdev_id, ret);
1383 		return ret;
1384 	}
1385 
1386 	ret = ath12k_mac_vdev_setup_sync(ar);
1387 	if (ret) {
1388 		ath12k_warn(ar->ab, "failed to synchronize setup for monitor vdev %i start: %d\n",
1389 			    vdev_id, ret);
1390 		return ret;
1391 	}
1392 
1393 	params.vdev_id = vdev_id;
1394 	params.bssid = ar->mac_addr;
1395 	ret = ath12k_wmi_vdev_up(ar, &params);
1396 	if (ret) {
1397 		ath12k_warn(ar->ab, "failed to put up monitor vdev %i: %d\n",
1398 			    vdev_id, ret);
1399 		goto vdev_stop;
1400 	}
1401 
1402 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac monitor vdev %i started\n",
1403 		   vdev_id);
1404 	return 0;
1405 
1406 vdev_stop:
1407 	ret = ath12k_wmi_vdev_stop(ar, vdev_id);
1408 	if (ret)
1409 		ath12k_warn(ar->ab, "failed to stop monitor vdev %i after start failure: %d\n",
1410 			    vdev_id, ret);
1411 	return ret;
1412 }
1413 
1414 static int ath12k_mac_monitor_vdev_stop(struct ath12k *ar)
1415 {
1416 	int ret;
1417 
1418 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
1419 
1420 	reinit_completion(&ar->vdev_setup_done);
1421 
1422 	ret = ath12k_wmi_vdev_stop(ar, ar->monitor_vdev_id);
1423 	if (ret)
1424 		ath12k_warn(ar->ab, "failed to request monitor vdev %i stop: %d\n",
1425 			    ar->monitor_vdev_id, ret);
1426 
1427 	ret = ath12k_mac_vdev_setup_sync(ar);
1428 	if (ret)
1429 		ath12k_warn(ar->ab, "failed to synchronize monitor vdev %i stop: %d\n",
1430 			    ar->monitor_vdev_id, ret);
1431 
1432 	ret = ath12k_wmi_vdev_down(ar, ar->monitor_vdev_id);
1433 	if (ret)
1434 		ath12k_warn(ar->ab, "failed to put down monitor vdev %i: %d\n",
1435 			    ar->monitor_vdev_id, ret);
1436 
1437 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac monitor vdev %i stopped\n",
1438 		   ar->monitor_vdev_id);
1439 	return ret;
1440 }
1441 
1442 static int ath12k_mac_monitor_vdev_delete(struct ath12k *ar)
1443 {
1444 	int ret;
1445 	unsigned long time_left;
1446 
1447 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
1448 
1449 	if (!ar->monitor_vdev_created)
1450 		return 0;
1451 
1452 	reinit_completion(&ar->vdev_delete_done);
1453 
1454 	ret = ath12k_wmi_vdev_delete(ar, ar->monitor_vdev_id);
1455 	if (ret) {
1456 		ath12k_warn(ar->ab, "failed to request wmi monitor vdev %i removal: %d\n",
1457 			    ar->monitor_vdev_id, ret);
1458 		return ret;
1459 	}
1460 
1461 	time_left = wait_for_completion_timeout(&ar->vdev_delete_done,
1462 						ATH12K_VDEV_DELETE_TIMEOUT_HZ);
1463 	if (time_left == 0) {
1464 		ath12k_warn(ar->ab, "Timeout in receiving vdev delete response\n");
1465 	} else {
1466 		ar->allocated_vdev_map &= ~(1LL << ar->monitor_vdev_id);
1467 		ar->ab->free_vdev_map |= 1LL << (ar->monitor_vdev_id);
1468 		ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac monitor vdev %d deleted\n",
1469 			   ar->monitor_vdev_id);
1470 		ar->num_created_vdevs--;
1471 		ar->monitor_vdev_id = -1;
1472 		ar->monitor_vdev_created = false;
1473 	}
1474 
1475 	return ret;
1476 }
1477 
1478 static int ath12k_mac_monitor_start(struct ath12k *ar)
1479 {
1480 	struct ath12k_mac_get_any_chanctx_conf_arg arg;
1481 	int ret;
1482 
1483 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
1484 
1485 	if (ar->monitor_started)
1486 		return 0;
1487 
1488 	arg.ar = ar;
1489 	arg.chanctx_conf = NULL;
1490 	ieee80211_iter_chan_contexts_atomic(ath12k_ar_to_hw(ar),
1491 					    ath12k_mac_get_any_chanctx_conf_iter,
1492 					    &arg);
1493 	if (!arg.chanctx_conf)
1494 		return 0;
1495 
1496 	ret = ath12k_mac_monitor_vdev_start(ar, ar->monitor_vdev_id,
1497 					    &arg.chanctx_conf->def);
1498 	if (ret) {
1499 		ath12k_warn(ar->ab, "failed to start monitor vdev: %d\n", ret);
1500 		return ret;
1501 	}
1502 
1503 	ret = ath12k_dp_tx_htt_monitor_mode_ring_config(ar, false);
1504 	if (ret) {
1505 		ath12k_warn(ar->ab, "fail to set monitor filter: %d\n", ret);
1506 		return ret;
1507 	}
1508 
1509 	/*
1510 	 * A previous monitor session may have stopped mid-PPDU, leaving
1511 	 * ppdu_continuation set. Clear it so the first status buffer of
1512 	 * this session is not merged with stale state from before.
1513 	 */
1514 	ar->dp.mon_data.mon_ppdu_info.ppdu_continuation = false;
1515 
1516 	ar->monitor_started = true;
1517 	ar->num_started_vdevs++;
1518 
1519 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac monitor started\n");
1520 
1521 	return 0;
1522 }
1523 
1524 static int ath12k_mac_monitor_stop(struct ath12k *ar)
1525 {
1526 	int ret;
1527 
1528 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
1529 
1530 	if (!ar->monitor_started)
1531 		return 0;
1532 
1533 	ret = ath12k_mac_monitor_vdev_stop(ar);
1534 	if (ret) {
1535 		ath12k_warn(ar->ab, "failed to stop monitor vdev: %d\n", ret);
1536 		return ret;
1537 	}
1538 
1539 	ar->monitor_started = false;
1540 	ar->num_started_vdevs--;
1541 	ret = ath12k_dp_tx_htt_monitor_mode_ring_config(ar, true);
1542 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac monitor stopped ret %d\n", ret);
1543 	return ret;
1544 }
1545 
1546 int ath12k_mac_vdev_stop(struct ath12k_link_vif *arvif)
1547 {
1548 	struct ath12k_vif *ahvif = arvif->ahvif;
1549 	struct ath12k *ar = arvif->ar;
1550 	int ret;
1551 
1552 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
1553 
1554 	reinit_completion(&ar->vdev_setup_done);
1555 
1556 	ret = ath12k_wmi_vdev_stop(ar, arvif->vdev_id);
1557 	if (ret) {
1558 		ath12k_warn(ar->ab, "failed to stop WMI vdev %i: %d\n",
1559 			    arvif->vdev_id, ret);
1560 		goto err;
1561 	}
1562 
1563 	ret = ath12k_mac_vdev_setup_sync(ar);
1564 	if (ret) {
1565 		ath12k_warn(ar->ab, "failed to synchronize setup for vdev %i: %d\n",
1566 			    arvif->vdev_id, ret);
1567 		goto err;
1568 	}
1569 
1570 	WARN_ON(ar->num_started_vdevs == 0);
1571 
1572 	ar->num_started_vdevs--;
1573 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "vdev %pM stopped, vdev_id %d\n",
1574 		   ahvif->vif->addr, arvif->vdev_id);
1575 
1576 	if (test_bit(ATH12K_FLAG_CAC_RUNNING, &ar->dev_flags)) {
1577 		clear_bit(ATH12K_FLAG_CAC_RUNNING, &ar->dev_flags);
1578 		ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "CAC Stopped for vdev %d\n",
1579 			   arvif->vdev_id);
1580 	}
1581 
1582 	return 0;
1583 err:
1584 	return ret;
1585 }
1586 
1587 int ath12k_mac_op_config(struct ieee80211_hw *hw, int radio_idx, u32 changed)
1588 {
1589 	return 0;
1590 }
1591 EXPORT_SYMBOL(ath12k_mac_op_config);
1592 
1593 static int ath12k_mac_setup_bcn_p2p_ie(struct ath12k_link_vif *arvif,
1594 				       struct sk_buff *bcn)
1595 {
1596 	struct ath12k *ar = arvif->ar;
1597 	struct ieee80211_mgmt *mgmt;
1598 	const u8 *p2p_ie;
1599 	int ret;
1600 
1601 	mgmt = (void *)bcn->data;
1602 	p2p_ie = cfg80211_find_vendor_ie(WLAN_OUI_WFA, WLAN_OUI_TYPE_WFA_P2P,
1603 					 mgmt->u.beacon.variable,
1604 					 bcn->len - (mgmt->u.beacon.variable -
1605 						     bcn->data));
1606 	if (!p2p_ie) {
1607 		ath12k_warn(ar->ab, "no P2P ie found in beacon\n");
1608 		return -ENOENT;
1609 	}
1610 
1611 	ret = ath12k_wmi_p2p_go_bcn_ie(ar, arvif->vdev_id, p2p_ie);
1612 	if (ret) {
1613 		ath12k_warn(ar->ab, "failed to submit P2P GO bcn ie for vdev %i: %d\n",
1614 			    arvif->vdev_id, ret);
1615 		return ret;
1616 	}
1617 
1618 	return 0;
1619 }
1620 
1621 static int ath12k_mac_remove_vendor_ie(struct sk_buff *skb, unsigned int oui,
1622 				       u8 oui_type, size_t ie_offset)
1623 {
1624 	const u8 *next, *end;
1625 	size_t len;
1626 	u8 *ie;
1627 
1628 	if (WARN_ON(skb->len < ie_offset))
1629 		return -EINVAL;
1630 
1631 	ie = (u8 *)cfg80211_find_vendor_ie(oui, oui_type,
1632 					   skb->data + ie_offset,
1633 					   skb->len - ie_offset);
1634 	if (!ie)
1635 		return -ENOENT;
1636 
1637 	len = ie[1] + 2;
1638 	end = skb->data + skb->len;
1639 	next = ie + len;
1640 
1641 	if (WARN_ON(next > end))
1642 		return -EINVAL;
1643 
1644 	memmove(ie, next, end - next);
1645 	skb_trim(skb, skb->len - len);
1646 
1647 	return 0;
1648 }
1649 
1650 static void ath12k_mac_set_arvif_ies(struct ath12k_link_vif *arvif,
1651 				     struct ath12k_link_vif *tx_arvif,
1652 				     struct sk_buff *bcn,
1653 				     u8 bssid_index, bool *nontx_profile_found)
1654 {
1655 	struct ieee80211_mgmt *mgmt = (struct ieee80211_mgmt *)bcn->data;
1656 	const struct element *elem, *nontx, *index, *nie, *ext_cap_ie;
1657 	const u8 *start, *tail;
1658 	u16 rem_len;
1659 	u8 i;
1660 
1661 	start = bcn->data + ieee80211_get_hdrlen_from_skb(bcn) + sizeof(mgmt->u.beacon);
1662 	tail = skb_tail_pointer(bcn);
1663 	rem_len = tail - start;
1664 
1665 	arvif->rsnie_present = false;
1666 	arvif->wpaie_present = false;
1667 
1668 	if (cfg80211_find_ie(WLAN_EID_RSN, start, rem_len))
1669 		arvif->rsnie_present = true;
1670 	if (cfg80211_find_vendor_ie(WLAN_OUI_MICROSOFT, WLAN_OUI_TYPE_MICROSOFT_WPA,
1671 				    start, rem_len))
1672 		arvif->wpaie_present = true;
1673 
1674 	ext_cap_ie = cfg80211_find_elem(WLAN_EID_EXT_CAPABILITY, start, rem_len);
1675 	if (ext_cap_ie && ext_cap_ie->datalen >= 11 &&
1676 	    (ext_cap_ie->data[10] & WLAN_EXT_CAPA11_BCN_PROTECT))
1677 		tx_arvif->beacon_prot = true;
1678 
1679 	/* Return from here for the transmitted profile */
1680 	if (!bssid_index)
1681 		return;
1682 
1683 	/* Initial rsnie_present for the nontransmitted profile is set to be same as that
1684 	 * of the transmitted profile. It will be changed if security configurations are
1685 	 * different.
1686 	 */
1687 	*nontx_profile_found = false;
1688 	for_each_element_id(elem, WLAN_EID_MULTIPLE_BSSID, start, rem_len) {
1689 		/* Fixed minimum MBSSID element length with at least one
1690 		 * nontransmitted BSSID profile is 12 bytes as given below;
1691 		 * 1 (max BSSID indicator) +
1692 		 * 2 (Nontransmitted BSSID profile: Subelement ID + length) +
1693 		 * 4 (Nontransmitted BSSID Capabilities: tag + length + info)
1694 		 * 2 (Nontransmitted BSSID SSID: tag + length)
1695 		 * 3 (Nontransmitted BSSID Index: tag + length + BSSID index
1696 		 */
1697 		if (elem->datalen < 12 || elem->data[0] < 1)
1698 			continue; /* Max BSSID indicator must be >=1 */
1699 
1700 		for_each_element(nontx, elem->data + 1, elem->datalen - 1) {
1701 			start = nontx->data;
1702 
1703 			if (nontx->id != 0 || nontx->datalen < 4)
1704 				continue; /* Invalid nontransmitted profile */
1705 
1706 			if (nontx->data[0] != WLAN_EID_NON_TX_BSSID_CAP ||
1707 			    nontx->data[1] != 2) {
1708 				continue; /* Missing nontransmitted BSS capabilities */
1709 			}
1710 
1711 			if (nontx->data[4] != WLAN_EID_SSID)
1712 				continue; /* Missing SSID for nontransmitted BSS */
1713 
1714 			index = cfg80211_find_elem(WLAN_EID_MULTI_BSSID_IDX,
1715 						   start, nontx->datalen);
1716 			if (!index || index->datalen < 1 || index->data[0] == 0)
1717 				continue; /* Invalid MBSSID Index element */
1718 
1719 			if (index->data[0] == bssid_index) {
1720 				*nontx_profile_found = true;
1721 
1722 				/* Check if nontx BSS has beacon protection enabled */
1723 				if (!tx_arvif->beacon_prot) {
1724 					ext_cap_ie =
1725 					    cfg80211_find_elem(WLAN_EID_EXT_CAPABILITY,
1726 							       nontx->data,
1727 							       nontx->datalen);
1728 					if (ext_cap_ie && ext_cap_ie->datalen >= 11 &&
1729 					    (ext_cap_ie->data[10] &
1730 					     WLAN_EXT_CAPA11_BCN_PROTECT))
1731 						tx_arvif->beacon_prot = true;
1732 				}
1733 
1734 				if (cfg80211_find_ie(WLAN_EID_RSN,
1735 						     nontx->data,
1736 						     nontx->datalen)) {
1737 					arvif->rsnie_present = true;
1738 					return;
1739 				} else if (!arvif->rsnie_present) {
1740 					return; /* Both tx and nontx BSS are open */
1741 				}
1742 
1743 				nie = cfg80211_find_ext_elem(WLAN_EID_EXT_NON_INHERITANCE,
1744 							     nontx->data,
1745 							     nontx->datalen);
1746 				if (!nie || nie->datalen < 2)
1747 					return; /* Invalid non-inheritance element */
1748 
1749 				for (i = 1; i < nie->datalen - 1; i++) {
1750 					if (nie->data[i] == WLAN_EID_RSN) {
1751 						arvif->rsnie_present = false;
1752 						break;
1753 					}
1754 				}
1755 
1756 				return;
1757 			}
1758 		}
1759 	}
1760 }
1761 
1762 static int ath12k_mac_setup_bcn_tmpl_ema(struct ath12k_link_vif *arvif,
1763 					 struct ath12k_link_vif *tx_arvif,
1764 					 u8 bssid_index)
1765 {
1766 	struct ath12k_wmi_bcn_tmpl_ema_arg ema_args;
1767 	struct ieee80211_ema_beacons *beacons;
1768 	bool nontx_profile_found = false;
1769 	int ret = 0;
1770 	u8 i;
1771 
1772 	beacons = ieee80211_beacon_get_template_ema_list(ath12k_ar_to_hw(tx_arvif->ar),
1773 							 tx_arvif->ahvif->vif,
1774 							 tx_arvif->link_id);
1775 	if (!beacons || !beacons->cnt) {
1776 		ath12k_warn(arvif->ar->ab,
1777 			    "failed to get ema beacon templates from mac80211\n");
1778 		return -EPERM;
1779 	}
1780 
1781 	if (tx_arvif == arvif)
1782 		ath12k_mac_set_arvif_ies(arvif, tx_arvif, beacons->bcn[0].skb, 0, NULL);
1783 
1784 	for (i = 0; i < beacons->cnt; i++) {
1785 		if (tx_arvif != arvif && !nontx_profile_found)
1786 			ath12k_mac_set_arvif_ies(arvif, tx_arvif, beacons->bcn[i].skb,
1787 						 bssid_index,
1788 						 &nontx_profile_found);
1789 
1790 		ema_args.bcn_cnt = beacons->cnt;
1791 		ema_args.bcn_index = i;
1792 		ret = ath12k_wmi_bcn_tmpl(tx_arvif, &beacons->bcn[i].offs,
1793 					  beacons->bcn[i].skb, &ema_args);
1794 		if (ret) {
1795 			ath12k_warn(tx_arvif->ar->ab,
1796 				    "failed to set ema beacon template id %i error %d\n",
1797 				    i, ret);
1798 			break;
1799 		}
1800 	}
1801 
1802 	if (tx_arvif != arvif && !nontx_profile_found)
1803 		ath12k_warn(arvif->ar->ab,
1804 			    "nontransmitted bssid index %u not found in beacon template\n",
1805 			    bssid_index);
1806 
1807 	ieee80211_beacon_free_ema_list(beacons);
1808 	return ret;
1809 }
1810 
1811 static int ath12k_mac_setup_bcn_tmpl(struct ath12k_link_vif *arvif)
1812 {
1813 	struct ath12k_vif *ahvif = arvif->ahvif;
1814 	struct ieee80211_vif *vif = ath12k_ahvif_to_vif(ahvif);
1815 	struct ieee80211_bss_conf *link_conf;
1816 	struct ath12k_link_vif *tx_arvif;
1817 	struct ath12k *ar = arvif->ar;
1818 	struct ath12k_base *ab = ar->ab;
1819 	struct ieee80211_mutable_offsets offs = {};
1820 	bool nontx_profile_found = false;
1821 	struct sk_buff *bcn;
1822 	int ret;
1823 
1824 	if (ahvif->vdev_type != WMI_VDEV_TYPE_AP)
1825 		return 0;
1826 
1827 	link_conf = ath12k_mac_get_link_bss_conf(arvif);
1828 	if (!link_conf) {
1829 		ath12k_warn(ar->ab, "unable to access bss link conf to set bcn tmpl for vif %pM link %u\n",
1830 			    vif->addr, arvif->link_id);
1831 		return -ENOLINK;
1832 	}
1833 
1834 	tx_arvif = ath12k_mac_get_tx_arvif(arvif, link_conf);
1835 	if (tx_arvif) {
1836 		if (tx_arvif != arvif && arvif->is_up)
1837 			return 0;
1838 
1839 		if (link_conf->ema_ap)
1840 			return ath12k_mac_setup_bcn_tmpl_ema(arvif, tx_arvif,
1841 							     link_conf->bssid_index);
1842 	} else {
1843 		tx_arvif = arvif;
1844 	}
1845 
1846 	bcn = ieee80211_beacon_get_template(ath12k_ar_to_hw(tx_arvif->ar),
1847 					    tx_arvif->ahvif->vif,
1848 					    &offs, tx_arvif->link_id);
1849 	if (!bcn) {
1850 		ath12k_warn(ab, "failed to get beacon template from mac80211\n");
1851 		return -EPERM;
1852 	}
1853 
1854 	if (tx_arvif == arvif) {
1855 		ath12k_mac_set_arvif_ies(arvif, tx_arvif, bcn, 0, NULL);
1856 	} else {
1857 		ath12k_mac_set_arvif_ies(arvif, tx_arvif, bcn,
1858 					 link_conf->bssid_index,
1859 					 &nontx_profile_found);
1860 		if (!nontx_profile_found)
1861 			ath12k_warn(ab,
1862 				    "nontransmitted profile not found in beacon template\n");
1863 	}
1864 
1865 	if (ahvif->vif->type == NL80211_IFTYPE_AP && ahvif->vif->p2p) {
1866 		ret = ath12k_mac_setup_bcn_p2p_ie(arvif, bcn);
1867 		if (ret) {
1868 			ath12k_warn(ab, "failed to setup P2P GO bcn ie: %d\n",
1869 				    ret);
1870 			goto free_bcn_skb;
1871 		}
1872 
1873 		/* P2P IE is inserted by firmware automatically (as
1874 		 * configured above) so remove it from the base beacon
1875 		 * template to avoid duplicate P2P IEs in beacon frames.
1876 		 */
1877 		ret = ath12k_mac_remove_vendor_ie(bcn, WLAN_OUI_WFA,
1878 						  WLAN_OUI_TYPE_WFA_P2P,
1879 						  offsetof(struct ieee80211_mgmt,
1880 							   u.beacon.variable));
1881 		if (ret) {
1882 			ath12k_warn(ab, "failed to remove P2P vendor ie: %d\n",
1883 				    ret);
1884 			goto free_bcn_skb;
1885 		}
1886 	}
1887 
1888 	ret = ath12k_wmi_bcn_tmpl(arvif, &offs, bcn, NULL);
1889 
1890 	if (ret)
1891 		ath12k_warn(ab, "failed to submit beacon template command: %d\n",
1892 			    ret);
1893 
1894 free_bcn_skb:
1895 	kfree_skb(bcn);
1896 	return ret;
1897 }
1898 
1899 static void ath12k_control_beaconing(struct ath12k_link_vif *arvif,
1900 				     struct ieee80211_bss_conf *info)
1901 {
1902 	struct ath12k_wmi_vdev_up_params params = {};
1903 	struct ath12k_vif *ahvif = arvif->ahvif;
1904 	struct ath12k *ar = arvif->ar;
1905 	int ret;
1906 
1907 	lockdep_assert_wiphy(ath12k_ar_to_hw(arvif->ar)->wiphy);
1908 
1909 	if (!info->enable_beacon) {
1910 		ret = ath12k_wmi_vdev_down(ar, arvif->vdev_id);
1911 		if (ret)
1912 			ath12k_warn(ar->ab, "failed to down vdev_id %i: %d\n",
1913 				    arvif->vdev_id, ret);
1914 
1915 		arvif->is_up = false;
1916 		return;
1917 	}
1918 
1919 	/* Install the beacon template to the FW */
1920 	ret = ath12k_mac_setup_bcn_tmpl(arvif);
1921 	if (ret) {
1922 		ath12k_warn(ar->ab, "failed to update bcn tmpl during vdev up: %d\n",
1923 			    ret);
1924 		return;
1925 	}
1926 
1927 	ahvif->aid = 0;
1928 
1929 	ether_addr_copy(arvif->bssid, info->addr);
1930 
1931 	params.vdev_id = arvif->vdev_id;
1932 	params.aid = ahvif->aid;
1933 	params.bssid = arvif->bssid;
1934 	params.tx_bssid = ath12k_mac_get_tx_bssid(arvif);
1935 	if (params.tx_bssid) {
1936 		params.nontx_profile_idx = info->bssid_index;
1937 		params.nontx_profile_cnt = 1 << info->bssid_indicator;
1938 	}
1939 	ret = ath12k_wmi_vdev_up(arvif->ar, &params);
1940 	if (ret) {
1941 		ath12k_warn(ar->ab, "failed to bring up vdev %d: %i\n",
1942 			    arvif->vdev_id, ret);
1943 		return;
1944 	}
1945 
1946 	arvif->is_up = true;
1947 
1948 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac vdev %d up\n", arvif->vdev_id);
1949 }
1950 
1951 static void ath12k_mac_handle_beacon_iter(void *data, u8 *mac,
1952 					  struct ieee80211_vif *vif)
1953 {
1954 	struct sk_buff *skb = data;
1955 	struct ieee80211_mgmt *mgmt = (void *)skb->data;
1956 	struct ath12k_vif *ahvif = ath12k_vif_to_ahvif(vif);
1957 	struct ath12k_link_vif *arvif = &ahvif->deflink;
1958 
1959 	if (vif->type != NL80211_IFTYPE_STATION || !arvif->is_created)
1960 		return;
1961 
1962 	if (!ether_addr_equal(mgmt->bssid, vif->bss_conf.bssid))
1963 		return;
1964 
1965 	cancel_delayed_work(&arvif->connection_loss_work);
1966 }
1967 
1968 void ath12k_mac_handle_beacon(struct ath12k *ar, struct sk_buff *skb)
1969 {
1970 	ieee80211_iterate_active_interfaces_atomic(ath12k_ar_to_hw(ar),
1971 						   IEEE80211_IFACE_ITER_NORMAL,
1972 						   ath12k_mac_handle_beacon_iter,
1973 						   skb);
1974 }
1975 
1976 void ath12k_mac_handle_beacon_miss(struct ath12k *ar,
1977 				   struct ath12k_link_vif *arvif)
1978 {
1979 	struct ieee80211_hw *hw = ath12k_ar_to_hw(ar);
1980 	struct ieee80211_vif *vif = ath12k_ahvif_to_vif(arvif->ahvif);
1981 
1982 	if (!(arvif->is_created && arvif->is_up))
1983 		return;
1984 
1985 	ieee80211_beacon_loss(vif);
1986 
1987 	/* Firmware doesn't report beacon loss events repeatedly. If AP probe
1988 	 * (done by mac80211) succeeds but beacons do not resume then it
1989 	 * doesn't make sense to continue operation. Queue connection loss work
1990 	 * which can be cancelled when beacon is received.
1991 	 */
1992 	ieee80211_queue_delayed_work(hw, &arvif->connection_loss_work,
1993 				     ATH12K_CONNECTION_LOSS_HZ);
1994 }
1995 
1996 static void ath12k_mac_vif_sta_connection_loss_work(struct work_struct *work)
1997 {
1998 	struct ath12k_link_vif *arvif = container_of(work, struct ath12k_link_vif,
1999 						     connection_loss_work.work);
2000 	struct ieee80211_vif *vif = arvif->ahvif->vif;
2001 
2002 	if (!arvif->is_up)
2003 		return;
2004 
2005 	ieee80211_connection_loss(vif);
2006 }
2007 
2008 static void ath12k_peer_assoc_h_basic(struct ath12k *ar,
2009 				      struct ath12k_link_vif *arvif,
2010 				      struct ath12k_link_sta *arsta,
2011 				      struct ath12k_wmi_peer_assoc_arg *arg)
2012 {
2013 	struct ieee80211_vif *vif = ath12k_ahvif_to_vif(arvif->ahvif);
2014 	struct ieee80211_sta *sta = ath12k_ahsta_to_sta(arsta->ahsta);
2015 	struct ieee80211_hw *hw = ath12k_ar_to_hw(ar);
2016 	struct ieee80211_bss_conf *bss_conf;
2017 	u32 aid;
2018 
2019 	lockdep_assert_wiphy(hw->wiphy);
2020 
2021 	if (vif->type == NL80211_IFTYPE_STATION)
2022 		aid = vif->cfg.aid;
2023 	else
2024 		aid = sta->aid;
2025 
2026 	ether_addr_copy(arg->peer_mac, arsta->addr);
2027 	arg->vdev_id = arvif->vdev_id;
2028 	arg->peer_associd = aid;
2029 	arg->auth_flag = true;
2030 	/* TODO: STA WAR in ath10k for listen interval required? */
2031 	arg->peer_listen_intval = hw->conf.listen_interval;
2032 	arg->peer_nss = 1;
2033 
2034 	bss_conf = ath12k_mac_get_link_bss_conf(arvif);
2035 	if (!bss_conf) {
2036 		ath12k_warn(ar->ab, "unable to access bss link conf in peer assoc for vif %pM link %u\n",
2037 			    vif->addr, arvif->link_id);
2038 		return;
2039 	}
2040 
2041 	arg->peer_caps = bss_conf->assoc_capability;
2042 }
2043 
2044 static void ath12k_peer_assoc_h_crypto(struct ath12k *ar,
2045 				       struct ath12k_link_vif *arvif,
2046 				       struct ath12k_link_sta *arsta,
2047 				       struct ath12k_wmi_peer_assoc_arg *arg)
2048 {
2049 	struct ieee80211_vif *vif = ath12k_ahvif_to_vif(arvif->ahvif);
2050 	struct ieee80211_sta *sta = ath12k_ahsta_to_sta(arsta->ahsta);
2051 	struct ieee80211_bss_conf *info;
2052 	struct cfg80211_chan_def def;
2053 	struct cfg80211_bss *bss;
2054 	struct ieee80211_hw *hw = ath12k_ar_to_hw(ar);
2055 	const u8 *rsnie = NULL;
2056 	const u8 *wpaie = NULL;
2057 
2058 	lockdep_assert_wiphy(hw->wiphy);
2059 
2060 	info = ath12k_mac_get_link_bss_conf(arvif);
2061 	if (!info) {
2062 		ath12k_warn(ar->ab, "unable to access bss link conf for peer assoc crypto for vif %pM link %u\n",
2063 			    vif->addr, arvif->link_id);
2064 		return;
2065 	}
2066 
2067 	if (WARN_ON(ath12k_mac_vif_link_chan(vif, arvif->link_id, &def)))
2068 		return;
2069 
2070 	bss = cfg80211_get_bss(hw->wiphy, def.chan, info->bssid, NULL, 0,
2071 			       IEEE80211_BSS_TYPE_ANY, IEEE80211_PRIVACY_ANY);
2072 
2073 	if (arvif->rsnie_present || arvif->wpaie_present) {
2074 		arg->need_ptk_4_way = true;
2075 		if (arvif->wpaie_present)
2076 			arg->need_gtk_2_way = true;
2077 	} else if (bss) {
2078 		const struct cfg80211_bss_ies *ies;
2079 
2080 		rcu_read_lock();
2081 		rsnie = ieee80211_bss_get_ie(bss, WLAN_EID_RSN);
2082 
2083 		ies = rcu_dereference(bss->ies);
2084 
2085 		wpaie = cfg80211_find_vendor_ie(WLAN_OUI_MICROSOFT,
2086 						WLAN_OUI_TYPE_MICROSOFT_WPA,
2087 						ies->data,
2088 						ies->len);
2089 		rcu_read_unlock();
2090 		cfg80211_put_bss(hw->wiphy, bss);
2091 	}
2092 
2093 	/* FIXME: base on RSN IE/WPA IE is a correct idea? */
2094 	if (rsnie || wpaie) {
2095 		ath12k_dbg(ar->ab, ATH12K_DBG_WMI,
2096 			   "%s: rsn ie found\n", __func__);
2097 		arg->need_ptk_4_way = true;
2098 	}
2099 
2100 	if (wpaie) {
2101 		ath12k_dbg(ar->ab, ATH12K_DBG_WMI,
2102 			   "%s: wpa ie found\n", __func__);
2103 		arg->need_gtk_2_way = true;
2104 	}
2105 
2106 	if (sta->mfp) {
2107 		/* TODO: Need to check if FW supports PMF? */
2108 		arg->is_pmf_enabled = true;
2109 	}
2110 
2111 	/* TODO: safe_mode_enabled (bypass 4-way handshake) flag req? */
2112 }
2113 
2114 static void ath12k_peer_assoc_h_rates(struct ath12k *ar,
2115 				      struct ath12k_link_vif *arvif,
2116 				      struct ath12k_link_sta *arsta,
2117 				      struct ath12k_wmi_peer_assoc_arg *arg)
2118 {
2119 	struct ieee80211_vif *vif = ath12k_ahvif_to_vif(arvif->ahvif);
2120 	struct ieee80211_sta *sta = ath12k_ahsta_to_sta(arsta->ahsta);
2121 	struct wmi_rate_set_arg *rateset = &arg->peer_legacy_rates;
2122 	struct ieee80211_link_sta *link_sta;
2123 	struct cfg80211_chan_def def;
2124 	const struct ieee80211_supported_band *sband;
2125 	const struct ieee80211_rate *rates;
2126 	struct ieee80211_hw *hw = ath12k_ar_to_hw(ar);
2127 	enum nl80211_band band;
2128 	u32 ratemask;
2129 	u8 rate;
2130 	int i;
2131 
2132 	lockdep_assert_wiphy(hw->wiphy);
2133 
2134 	if (WARN_ON(ath12k_mac_vif_link_chan(vif, arvif->link_id, &def)))
2135 		return;
2136 
2137 	link_sta = ath12k_mac_get_link_sta(arsta);
2138 	if (!link_sta) {
2139 		ath12k_warn(ar->ab, "unable to access link sta in peer assoc rates for sta %pM link %u\n",
2140 			    sta->addr, arsta->link_id);
2141 		return;
2142 	}
2143 
2144 	band = def.chan->band;
2145 	sband = hw->wiphy->bands[band];
2146 	ratemask = link_sta->supp_rates[band];
2147 	ratemask &= arvif->bitrate_mask.control[band].legacy;
2148 	rates = sband->bitrates;
2149 
2150 	rateset->num_rates = 0;
2151 
2152 	for (i = 0; i < 32; i++, ratemask >>= 1, rates++) {
2153 		if (!(ratemask & 1))
2154 			continue;
2155 
2156 		rate = ath12k_mac_bitrate_to_rate(rates->bitrate);
2157 		rateset->rates[rateset->num_rates] = rate;
2158 		rateset->num_rates++;
2159 	}
2160 }
2161 
2162 static bool
2163 ath12k_peer_assoc_h_ht_masked(const u8 *ht_mcs_mask)
2164 {
2165 	int nss;
2166 
2167 	for (nss = 0; nss < IEEE80211_HT_MCS_MASK_LEN; nss++)
2168 		if (ht_mcs_mask[nss])
2169 			return false;
2170 
2171 	return true;
2172 }
2173 
2174 static bool
2175 ath12k_peer_assoc_h_vht_masked(const u16 *vht_mcs_mask)
2176 {
2177 	int nss;
2178 
2179 	for (nss = 0; nss < NL80211_VHT_NSS_MAX; nss++)
2180 		if (vht_mcs_mask[nss])
2181 			return false;
2182 
2183 	return true;
2184 }
2185 
2186 static void ath12k_peer_assoc_h_ht(struct ath12k *ar,
2187 				   struct ath12k_link_vif *arvif,
2188 				   struct ath12k_link_sta *arsta,
2189 				   struct ath12k_wmi_peer_assoc_arg *arg)
2190 {
2191 	struct ieee80211_vif *vif = ath12k_ahvif_to_vif(arvif->ahvif);
2192 	struct ieee80211_sta *sta = ath12k_ahsta_to_sta(arsta->ahsta);
2193 	const struct ieee80211_sta_ht_cap *ht_cap;
2194 	struct ieee80211_link_sta *link_sta;
2195 	struct cfg80211_chan_def def;
2196 	enum nl80211_band band;
2197 	const u8 *ht_mcs_mask;
2198 	int i, n;
2199 	u8 max_nss;
2200 	u32 stbc;
2201 
2202 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
2203 
2204 	if (WARN_ON(ath12k_mac_vif_link_chan(vif, arvif->link_id, &def)))
2205 		return;
2206 
2207 	link_sta = ath12k_mac_get_link_sta(arsta);
2208 	if (!link_sta) {
2209 		ath12k_warn(ar->ab, "unable to access link sta in peer assoc ht for sta %pM link %u\n",
2210 			    sta->addr, arsta->link_id);
2211 		return;
2212 	}
2213 
2214 	ht_cap = &link_sta->ht_cap;
2215 	if (!ht_cap->ht_supported)
2216 		return;
2217 
2218 	band = def.chan->band;
2219 	ht_mcs_mask = arvif->bitrate_mask.control[band].ht_mcs;
2220 
2221 	if (ath12k_peer_assoc_h_ht_masked(ht_mcs_mask))
2222 		return;
2223 
2224 	arg->ht_flag = true;
2225 
2226 	arg->peer_max_mpdu = (1 << (IEEE80211_HT_MAX_AMPDU_FACTOR +
2227 				    ht_cap->ampdu_factor)) - 1;
2228 
2229 	arg->peer_mpdu_density =
2230 		ath12k_parse_mpdudensity(ht_cap->ampdu_density);
2231 
2232 	arg->peer_ht_caps = ht_cap->cap;
2233 	arg->peer_rate_caps |= WMI_HOST_RC_HT_FLAG;
2234 
2235 	if (ht_cap->cap & IEEE80211_HT_CAP_LDPC_CODING)
2236 		arg->ldpc_flag = true;
2237 
2238 	if (link_sta->bandwidth >= IEEE80211_STA_RX_BW_40) {
2239 		arg->bw_40 = true;
2240 		arg->peer_rate_caps |= WMI_HOST_RC_CW40_FLAG;
2241 	}
2242 
2243 	/* As firmware handles these two flags (IEEE80211_HT_CAP_SGI_20
2244 	 * and IEEE80211_HT_CAP_SGI_40) for enabling SGI, reset both
2245 	 * flags if guard interval is to force Long GI
2246 	 */
2247 	if (arvif->bitrate_mask.control[band].gi == NL80211_TXRATE_FORCE_LGI) {
2248 		arg->peer_ht_caps &= ~(IEEE80211_HT_CAP_SGI_20 | IEEE80211_HT_CAP_SGI_40);
2249 	} else {
2250 		/* Enable SGI flag if either SGI_20 or SGI_40 is supported */
2251 		if (ht_cap->cap & (IEEE80211_HT_CAP_SGI_20 | IEEE80211_HT_CAP_SGI_40))
2252 			arg->peer_rate_caps |= WMI_HOST_RC_SGI_FLAG;
2253 	}
2254 
2255 	if (ht_cap->cap & IEEE80211_HT_CAP_TX_STBC) {
2256 		arg->peer_rate_caps |= WMI_HOST_RC_TX_STBC_FLAG;
2257 		arg->stbc_flag = true;
2258 	}
2259 
2260 	if (ht_cap->cap & IEEE80211_HT_CAP_RX_STBC) {
2261 		stbc = ht_cap->cap & IEEE80211_HT_CAP_RX_STBC;
2262 		stbc = stbc >> IEEE80211_HT_CAP_RX_STBC_SHIFT;
2263 		stbc = stbc << WMI_HOST_RC_RX_STBC_FLAG_S;
2264 		arg->peer_rate_caps |= stbc;
2265 		arg->stbc_flag = true;
2266 	}
2267 
2268 	if (ht_cap->mcs.rx_mask[1] && ht_cap->mcs.rx_mask[2])
2269 		arg->peer_rate_caps |= WMI_HOST_RC_TS_FLAG;
2270 	else if (ht_cap->mcs.rx_mask[1])
2271 		arg->peer_rate_caps |= WMI_HOST_RC_DS_FLAG;
2272 
2273 	for (i = 0, n = 0, max_nss = 0; i < IEEE80211_HT_MCS_MASK_LEN * 8; i++)
2274 		if ((ht_cap->mcs.rx_mask[i / 8] & BIT(i % 8)) &&
2275 		    (ht_mcs_mask[i / 8] & BIT(i % 8))) {
2276 			max_nss = (i / 8) + 1;
2277 			arg->peer_ht_rates.rates[n++] = i;
2278 		}
2279 
2280 	/* This is a workaround for HT-enabled STAs which break the spec
2281 	 * and have no HT capabilities RX mask (no HT RX MCS map).
2282 	 *
2283 	 * As per spec, in section 20.3.5 Modulation and coding scheme (MCS),
2284 	 * MCS 0 through 7 are mandatory in 20MHz with 800 ns GI at all STAs.
2285 	 *
2286 	 * Firmware asserts if such situation occurs.
2287 	 */
2288 	if (n == 0) {
2289 		arg->peer_ht_rates.num_rates = 8;
2290 		for (i = 0; i < arg->peer_ht_rates.num_rates; i++)
2291 			arg->peer_ht_rates.rates[i] = i;
2292 	} else {
2293 		arg->peer_ht_rates.num_rates = n;
2294 		arg->peer_nss = min(link_sta->rx_nss, max_nss);
2295 	}
2296 
2297 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac ht peer %pM mcs cnt %d nss %d\n",
2298 		   arg->peer_mac,
2299 		   arg->peer_ht_rates.num_rates,
2300 		   arg->peer_nss);
2301 }
2302 
2303 static int ath12k_mac_get_max_vht_mcs_map(u16 mcs_map, int nss)
2304 {
2305 	switch ((mcs_map >> (2 * nss)) & 0x3) {
2306 	case IEEE80211_VHT_MCS_SUPPORT_0_7: return BIT(8) - 1;
2307 	case IEEE80211_VHT_MCS_SUPPORT_0_8: return BIT(9) - 1;
2308 	case IEEE80211_VHT_MCS_SUPPORT_0_9: return BIT(10) - 1;
2309 	}
2310 	return 0;
2311 }
2312 
2313 static u16
2314 ath12k_peer_assoc_h_vht_limit(u16 tx_mcs_set,
2315 			      const u16 vht_mcs_limit[NL80211_VHT_NSS_MAX])
2316 {
2317 	int idx_limit;
2318 	int nss;
2319 	u16 mcs_map;
2320 	u16 mcs;
2321 
2322 	for (nss = 0; nss < NL80211_VHT_NSS_MAX; nss++) {
2323 		mcs_map = ath12k_mac_get_max_vht_mcs_map(tx_mcs_set, nss) &
2324 			  vht_mcs_limit[nss];
2325 
2326 		if (mcs_map)
2327 			idx_limit = fls(mcs_map) - 1;
2328 		else
2329 			idx_limit = -1;
2330 
2331 		switch (idx_limit) {
2332 		case 0:
2333 		case 1:
2334 		case 2:
2335 		case 3:
2336 		case 4:
2337 		case 5:
2338 		case 6:
2339 		case 7:
2340 			mcs = IEEE80211_VHT_MCS_SUPPORT_0_7;
2341 			break;
2342 		case 8:
2343 			mcs = IEEE80211_VHT_MCS_SUPPORT_0_8;
2344 			break;
2345 		case 9:
2346 			mcs = IEEE80211_VHT_MCS_SUPPORT_0_9;
2347 			break;
2348 		default:
2349 			WARN_ON(1);
2350 			fallthrough;
2351 		case -1:
2352 			mcs = IEEE80211_VHT_MCS_NOT_SUPPORTED;
2353 			break;
2354 		}
2355 
2356 		tx_mcs_set &= ~(0x3 << (nss * 2));
2357 		tx_mcs_set |= mcs << (nss * 2);
2358 	}
2359 
2360 	return tx_mcs_set;
2361 }
2362 
2363 static u8 ath12k_get_nss_160mhz(struct ath12k *ar,
2364 				u8 max_nss)
2365 {
2366 	u8 nss_ratio_info = ar->pdev->cap.nss_ratio_info;
2367 	u8 max_sup_nss = 0;
2368 
2369 	switch (nss_ratio_info) {
2370 	case WMI_NSS_RATIO_1BY2_NSS:
2371 		max_sup_nss = max_nss >> 1;
2372 		break;
2373 	case WMI_NSS_RATIO_3BY4_NSS:
2374 		ath12k_warn(ar->ab, "WMI_NSS_RATIO_3BY4_NSS not supported\n");
2375 		break;
2376 	case WMI_NSS_RATIO_1_NSS:
2377 		max_sup_nss = max_nss;
2378 		break;
2379 	case WMI_NSS_RATIO_2_NSS:
2380 		ath12k_warn(ar->ab, "WMI_NSS_RATIO_2_NSS not supported\n");
2381 		break;
2382 	default:
2383 		ath12k_warn(ar->ab, "invalid nss ratio received from fw: %d\n",
2384 			    nss_ratio_info);
2385 		break;
2386 	}
2387 
2388 	return max_sup_nss;
2389 }
2390 
2391 static void ath12k_peer_assoc_h_vht(struct ath12k *ar,
2392 				    struct ath12k_link_vif *arvif,
2393 				    struct ath12k_link_sta *arsta,
2394 				    struct ath12k_wmi_peer_assoc_arg *arg)
2395 {
2396 	struct ieee80211_vif *vif = ath12k_ahvif_to_vif(arvif->ahvif);
2397 	struct ieee80211_sta *sta = ath12k_ahsta_to_sta(arsta->ahsta);
2398 	const struct ieee80211_sta_vht_cap *vht_cap;
2399 	struct ieee80211_link_sta *link_sta;
2400 	struct cfg80211_chan_def def;
2401 	enum nl80211_band band;
2402 	u16 *vht_mcs_mask;
2403 	u8 ampdu_factor;
2404 	u8 max_nss, vht_mcs;
2405 	int i, vht_nss, nss_idx;
2406 	bool user_rate_valid = true;
2407 	u32 rx_nss, tx_nss, nss_160;
2408 
2409 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
2410 
2411 	if (WARN_ON(ath12k_mac_vif_link_chan(vif, arvif->link_id, &def)))
2412 		return;
2413 
2414 	link_sta = ath12k_mac_get_link_sta(arsta);
2415 	if (!link_sta) {
2416 		ath12k_warn(ar->ab, "unable to access link sta in peer assoc vht for sta %pM link %u\n",
2417 			    sta->addr, arsta->link_id);
2418 		return;
2419 	}
2420 
2421 	vht_cap = &link_sta->vht_cap;
2422 	if (!vht_cap->vht_supported)
2423 		return;
2424 
2425 	band = def.chan->band;
2426 	vht_mcs_mask = arvif->bitrate_mask.control[band].vht_mcs;
2427 
2428 	if (ath12k_peer_assoc_h_vht_masked(vht_mcs_mask))
2429 		return;
2430 
2431 	arg->vht_flag = true;
2432 
2433 	/* TODO: similar flags required? */
2434 	arg->vht_capable = true;
2435 
2436 	if (def.chan->band == NL80211_BAND_2GHZ)
2437 		arg->vht_ng_flag = true;
2438 
2439 	arg->peer_vht_caps = vht_cap->cap;
2440 
2441 	ampdu_factor = (vht_cap->cap &
2442 			IEEE80211_VHT_CAP_MAX_A_MPDU_LENGTH_EXPONENT_MASK) >>
2443 		       IEEE80211_VHT_CAP_MAX_A_MPDU_LENGTH_EXPONENT_SHIFT;
2444 
2445 	/* Workaround: Some Netgear/Linksys 11ac APs set Rx A-MPDU factor to
2446 	 * zero in VHT IE. Using it would result in degraded throughput.
2447 	 * arg->peer_max_mpdu at this point contains HT max_mpdu so keep
2448 	 * it if VHT max_mpdu is smaller.
2449 	 */
2450 	arg->peer_max_mpdu = max(arg->peer_max_mpdu,
2451 				 (1U << (IEEE80211_HT_MAX_AMPDU_FACTOR +
2452 					ampdu_factor)) - 1);
2453 
2454 	if (link_sta->bandwidth == IEEE80211_STA_RX_BW_80)
2455 		arg->bw_80 = true;
2456 
2457 	if (link_sta->bandwidth == IEEE80211_STA_RX_BW_160)
2458 		arg->bw_160 = true;
2459 
2460 	vht_nss =  ath12k_mac_max_vht_nss(vht_mcs_mask);
2461 
2462 	if (vht_nss > link_sta->rx_nss) {
2463 		user_rate_valid = false;
2464 		for (nss_idx = link_sta->rx_nss - 1; nss_idx >= 0; nss_idx--) {
2465 			if (vht_mcs_mask[nss_idx]) {
2466 				user_rate_valid = true;
2467 				break;
2468 			}
2469 		}
2470 	}
2471 
2472 	if (!user_rate_valid) {
2473 		ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
2474 			   "Setting vht range MCS value to peer supported nss:%d for peer %pM\n",
2475 			   link_sta->rx_nss, arsta->addr);
2476 		vht_mcs_mask[link_sta->rx_nss - 1] = vht_mcs_mask[vht_nss - 1];
2477 	}
2478 
2479 	/* Calculate peer NSS capability from VHT capabilities if STA
2480 	 * supports VHT.
2481 	 */
2482 	for (i = 0, max_nss = 0, vht_mcs = 0; i < NL80211_VHT_NSS_MAX; i++) {
2483 		vht_mcs = __le16_to_cpu(vht_cap->vht_mcs.rx_mcs_map) >>
2484 			  (2 * i) & 3;
2485 
2486 		if (vht_mcs != IEEE80211_VHT_MCS_NOT_SUPPORTED &&
2487 		    vht_mcs_mask[i])
2488 			max_nss = i + 1;
2489 	}
2490 	arg->peer_nss = min(link_sta->rx_nss, max_nss);
2491 	arg->rx_max_rate = __le16_to_cpu(vht_cap->vht_mcs.rx_highest);
2492 	arg->rx_mcs_set = __le16_to_cpu(vht_cap->vht_mcs.rx_mcs_map);
2493 	arg->rx_mcs_set = ath12k_peer_assoc_h_vht_limit(arg->rx_mcs_set, vht_mcs_mask);
2494 
2495 	arg->tx_max_rate = __le16_to_cpu(vht_cap->vht_mcs.tx_highest);
2496 	arg->tx_mcs_set = __le16_to_cpu(vht_cap->vht_mcs.tx_mcs_map);
2497 
2498 	/* In QCN9274 platform, VHT MCS rate 10 and 11 is enabled by default.
2499 	 * VHT MCS rate 10 and 11 is not supported in 11ac standard.
2500 	 * so explicitly disable the VHT MCS rate 10 and 11 in 11ac mode.
2501 	 */
2502 	arg->tx_mcs_set &= ~IEEE80211_VHT_MCS_SUPPORT_0_11_MASK;
2503 	arg->tx_mcs_set |= IEEE80211_DISABLE_VHT_MCS_SUPPORT_0_11;
2504 
2505 	if ((arg->tx_mcs_set & IEEE80211_VHT_MCS_NOT_SUPPORTED) ==
2506 			IEEE80211_VHT_MCS_NOT_SUPPORTED)
2507 		arg->peer_vht_caps &= ~IEEE80211_VHT_CAP_MU_BEAMFORMEE_CAPABLE;
2508 
2509 	/* TODO:  Check */
2510 	arg->tx_max_mcs_nss = 0xFF;
2511 
2512 	if (arg->peer_phymode == MODE_11AC_VHT160) {
2513 		tx_nss = ath12k_get_nss_160mhz(ar, max_nss);
2514 		rx_nss = min(arg->peer_nss, tx_nss);
2515 		arg->peer_bw_rxnss_override = ATH12K_BW_NSS_MAP_ENABLE;
2516 
2517 		if (!rx_nss) {
2518 			ath12k_warn(ar->ab, "invalid max_nss\n");
2519 			return;
2520 		}
2521 
2522 		nss_160 = u32_encode_bits(rx_nss - 1, ATH12K_PEER_RX_NSS_160MHZ);
2523 		arg->peer_bw_rxnss_override |= nss_160;
2524 	}
2525 
2526 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
2527 		   "mac vht peer %pM max_mpdu %d flags 0x%x nss_override 0x%x\n",
2528 		   arsta->addr, arg->peer_max_mpdu, arg->peer_flags,
2529 		   arg->peer_bw_rxnss_override);
2530 }
2531 
2532 static int ath12k_mac_get_max_he_mcs_map(u16 mcs_map, int nss)
2533 {
2534 	switch ((mcs_map >> (2 * nss)) & 0x3) {
2535 	case IEEE80211_HE_MCS_SUPPORT_0_7: return BIT(8) - 1;
2536 	case IEEE80211_HE_MCS_SUPPORT_0_9: return BIT(10) - 1;
2537 	case IEEE80211_HE_MCS_SUPPORT_0_11: return BIT(12) - 1;
2538 	}
2539 	return 0;
2540 }
2541 
2542 static u16 ath12k_peer_assoc_h_he_limit(u16 tx_mcs_set,
2543 					const u16 *he_mcs_limit)
2544 {
2545 	int idx_limit;
2546 	int nss;
2547 	u16 mcs_map;
2548 	u16 mcs;
2549 
2550 	for (nss = 0; nss < NL80211_HE_NSS_MAX; nss++) {
2551 		mcs_map = ath12k_mac_get_max_he_mcs_map(tx_mcs_set, nss) &
2552 			he_mcs_limit[nss];
2553 
2554 		if (mcs_map)
2555 			idx_limit = fls(mcs_map) - 1;
2556 		else
2557 			idx_limit = -1;
2558 
2559 		switch (idx_limit) {
2560 		case 0 ... 7:
2561 			mcs = IEEE80211_HE_MCS_SUPPORT_0_7;
2562 			break;
2563 		case 8:
2564 		case 9:
2565 			mcs = IEEE80211_HE_MCS_SUPPORT_0_9;
2566 			break;
2567 		case 10:
2568 		case 11:
2569 			mcs = IEEE80211_HE_MCS_SUPPORT_0_11;
2570 			break;
2571 		default:
2572 			WARN_ON(1);
2573 			fallthrough;
2574 		case -1:
2575 			mcs = IEEE80211_HE_MCS_NOT_SUPPORTED;
2576 			break;
2577 		}
2578 
2579 		tx_mcs_set &= ~(0x3 << (nss * 2));
2580 		tx_mcs_set |= mcs << (nss * 2);
2581 	}
2582 
2583 	return tx_mcs_set;
2584 }
2585 
2586 static bool
2587 ath12k_peer_assoc_h_he_masked(const u16 he_mcs_mask[NL80211_HE_NSS_MAX])
2588 {
2589 	int nss;
2590 
2591 	for (nss = 0; nss < NL80211_HE_NSS_MAX; nss++)
2592 		if (he_mcs_mask[nss])
2593 			return false;
2594 
2595 	return true;
2596 }
2597 
2598 static void ath12k_peer_assoc_h_he(struct ath12k *ar,
2599 				   struct ath12k_link_vif *arvif,
2600 				   struct ath12k_link_sta *arsta,
2601 				   struct ath12k_wmi_peer_assoc_arg *arg)
2602 {
2603 	struct ieee80211_vif *vif = ath12k_ahvif_to_vif(arvif->ahvif);
2604 	struct ieee80211_sta *sta = ath12k_ahsta_to_sta(arsta->ahsta);
2605 	const struct ieee80211_sta_he_cap *he_cap;
2606 	struct ieee80211_bss_conf *link_conf;
2607 	struct ieee80211_link_sta *link_sta;
2608 	struct cfg80211_chan_def def;
2609 	int i;
2610 	u8 ampdu_factor, max_nss;
2611 	u8 rx_mcs_80 = IEEE80211_HE_MCS_NOT_SUPPORTED;
2612 	u8 rx_mcs_160 = IEEE80211_HE_MCS_NOT_SUPPORTED;
2613 	u16 mcs_160_map, mcs_80_map;
2614 	u8 link_id = arvif->link_id;
2615 	bool support_160;
2616 	enum nl80211_band band;
2617 	u16 *he_mcs_mask;
2618 	u8 he_mcs;
2619 	u16 he_tx_mcs = 0, v = 0;
2620 	int he_nss, nss_idx;
2621 	bool user_rate_valid = true;
2622 	u32 rx_nss, tx_nss, nss_160;
2623 
2624 	if (WARN_ON(ath12k_mac_vif_link_chan(vif, link_id, &def)))
2625 		return;
2626 
2627 	link_conf = ath12k_mac_get_link_bss_conf(arvif);
2628 	if (!link_conf) {
2629 		ath12k_warn(ar->ab, "unable to access bss link conf in peer assoc he for vif %pM link %u",
2630 			    vif->addr, link_id);
2631 		return;
2632 	}
2633 
2634 	link_sta = ath12k_mac_get_link_sta(arsta);
2635 	if (!link_sta) {
2636 		ath12k_warn(ar->ab, "unable to access link sta in peer assoc he for sta %pM link %u\n",
2637 			    sta->addr, arsta->link_id);
2638 		return;
2639 	}
2640 
2641 	he_cap = &link_sta->he_cap;
2642 	if (!he_cap->has_he)
2643 		return;
2644 
2645 	band = def.chan->band;
2646 	he_mcs_mask = arvif->bitrate_mask.control[band].he_mcs;
2647 
2648 	if (ath12k_peer_assoc_h_he_masked(he_mcs_mask))
2649 		return;
2650 
2651 	arg->he_flag = true;
2652 
2653 	support_160 = !!(he_cap->he_cap_elem.phy_cap_info[0] &
2654 		  IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_160MHZ_IN_5G);
2655 
2656 	/* Supported HE-MCS and NSS Set of peer he_cap is intersection with self he_cp */
2657 	mcs_160_map = le16_to_cpu(he_cap->he_mcs_nss_supp.rx_mcs_160);
2658 	mcs_80_map = le16_to_cpu(he_cap->he_mcs_nss_supp.rx_mcs_80);
2659 
2660 	if (support_160) {
2661 		for (i = 7; i >= 0; i--) {
2662 			u8 mcs_160 = (mcs_160_map >> (2 * i)) & 3;
2663 
2664 			if (mcs_160 != IEEE80211_HE_MCS_NOT_SUPPORTED) {
2665 				rx_mcs_160 = i + 1;
2666 				break;
2667 			}
2668 		}
2669 	}
2670 
2671 	for (i = 7; i >= 0; i--) {
2672 		u8 mcs_80 = (mcs_80_map >> (2 * i)) & 3;
2673 
2674 		if (mcs_80 != IEEE80211_HE_MCS_NOT_SUPPORTED) {
2675 			rx_mcs_80 = i + 1;
2676 			break;
2677 		}
2678 	}
2679 
2680 	if (support_160)
2681 		max_nss = min(rx_mcs_80, rx_mcs_160);
2682 	else
2683 		max_nss = rx_mcs_80;
2684 
2685 	arg->peer_nss = min(link_sta->rx_nss, max_nss);
2686 
2687 	memcpy(&arg->peer_he_cap_macinfo, he_cap->he_cap_elem.mac_cap_info,
2688 	       sizeof(he_cap->he_cap_elem.mac_cap_info));
2689 	memcpy(&arg->peer_he_cap_phyinfo, he_cap->he_cap_elem.phy_cap_info,
2690 	       sizeof(he_cap->he_cap_elem.phy_cap_info));
2691 	arg->peer_he_ops = link_conf->he_oper.params;
2692 
2693 	/* the top most byte is used to indicate BSS color info */
2694 	arg->peer_he_ops &= 0xffffff;
2695 
2696 	/* As per section 26.6.1 IEEE Std 802.11ax‐2022, if the Max AMPDU
2697 	 * Exponent Extension in HE cap is zero, use the arg->peer_max_mpdu
2698 	 * as calculated while parsing VHT caps(if VHT caps is present)
2699 	 * or HT caps (if VHT caps is not present).
2700 	 *
2701 	 * For non-zero value of Max AMPDU Exponent Extension in HE MAC caps,
2702 	 * if a HE STA sends VHT cap and HE cap IE in assoc request then, use
2703 	 * MAX_AMPDU_LEN_FACTOR as 20 to calculate max_ampdu length.
2704 	 * If a HE STA that does not send VHT cap, but HE and HT cap in assoc
2705 	 * request, then use MAX_AMPDU_LEN_FACTOR as 16 to calculate max_ampdu
2706 	 * length.
2707 	 */
2708 	ampdu_factor = u8_get_bits(he_cap->he_cap_elem.mac_cap_info[3],
2709 				   IEEE80211_HE_MAC_CAP3_MAX_AMPDU_LEN_EXP_MASK);
2710 
2711 	if (ampdu_factor) {
2712 		if (link_sta->vht_cap.vht_supported)
2713 			arg->peer_max_mpdu = (1 << (IEEE80211_HE_VHT_MAX_AMPDU_FACTOR +
2714 						    ampdu_factor)) - 1;
2715 		else if (link_sta->ht_cap.ht_supported)
2716 			arg->peer_max_mpdu = (1 << (IEEE80211_HE_HT_MAX_AMPDU_FACTOR +
2717 						    ampdu_factor)) - 1;
2718 	}
2719 
2720 	if (he_cap->he_cap_elem.phy_cap_info[6] &
2721 	    IEEE80211_HE_PHY_CAP6_PPE_THRESHOLD_PRESENT) {
2722 		int bit = 7;
2723 		int nss, ru;
2724 
2725 		arg->peer_ppet.numss_m1 = he_cap->ppe_thres[0] &
2726 					  IEEE80211_PPE_THRES_NSS_MASK;
2727 		arg->peer_ppet.ru_bit_mask =
2728 			(he_cap->ppe_thres[0] &
2729 			 IEEE80211_PPE_THRES_RU_INDEX_BITMASK_MASK) >>
2730 			IEEE80211_PPE_THRES_RU_INDEX_BITMASK_POS;
2731 
2732 		for (nss = 0; nss <= arg->peer_ppet.numss_m1; nss++) {
2733 			for (ru = 0; ru < 4; ru++) {
2734 				u32 val = 0;
2735 				int i;
2736 
2737 				if ((arg->peer_ppet.ru_bit_mask & BIT(ru)) == 0)
2738 					continue;
2739 				for (i = 0; i < 6; i++) {
2740 					val >>= 1;
2741 					val |= ((he_cap->ppe_thres[bit / 8] >>
2742 						 (bit % 8)) & 0x1) << 5;
2743 					bit++;
2744 				}
2745 				arg->peer_ppet.ppet16_ppet8_ru3_ru0[nss] |=
2746 								val << (ru * 6);
2747 			}
2748 		}
2749 	}
2750 
2751 	if (he_cap->he_cap_elem.mac_cap_info[0] & IEEE80211_HE_MAC_CAP0_TWT_RES)
2752 		arg->twt_responder = true;
2753 	if (he_cap->he_cap_elem.mac_cap_info[0] & IEEE80211_HE_MAC_CAP0_TWT_REQ)
2754 		arg->twt_requester = true;
2755 
2756 	he_nss = ath12k_mac_max_he_nss(he_mcs_mask);
2757 
2758 	if (he_nss > link_sta->rx_nss) {
2759 		user_rate_valid = false;
2760 		for (nss_idx = link_sta->rx_nss - 1; nss_idx >= 0; nss_idx--) {
2761 			if (he_mcs_mask[nss_idx]) {
2762 				user_rate_valid = true;
2763 				break;
2764 			}
2765 		}
2766 	}
2767 
2768 	if (!user_rate_valid) {
2769 		ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
2770 			   "Setting he range MCS value to peer supported nss:%d for peer %pM\n",
2771 			   link_sta->rx_nss, arsta->addr);
2772 		he_mcs_mask[link_sta->rx_nss - 1] = he_mcs_mask[he_nss - 1];
2773 	}
2774 
2775 	switch (link_sta->bandwidth) {
2776 	case IEEE80211_STA_RX_BW_160:
2777 		v = le16_to_cpu(he_cap->he_mcs_nss_supp.rx_mcs_160);
2778 		v = ath12k_peer_assoc_h_he_limit(v, he_mcs_mask);
2779 		arg->peer_he_rx_mcs_set[WMI_HECAP_TXRX_MCS_NSS_IDX_160] = v;
2780 
2781 		v = le16_to_cpu(he_cap->he_mcs_nss_supp.tx_mcs_160);
2782 		arg->peer_he_tx_mcs_set[WMI_HECAP_TXRX_MCS_NSS_IDX_160] = v;
2783 
2784 		arg->peer_he_mcs_count++;
2785 		if (!he_tx_mcs)
2786 			he_tx_mcs = v;
2787 		fallthrough;
2788 
2789 	default:
2790 		v = le16_to_cpu(he_cap->he_mcs_nss_supp.rx_mcs_80);
2791 		v = ath12k_peer_assoc_h_he_limit(v, he_mcs_mask);
2792 		arg->peer_he_rx_mcs_set[WMI_HECAP_TXRX_MCS_NSS_IDX_80] = v;
2793 
2794 		v = le16_to_cpu(he_cap->he_mcs_nss_supp.tx_mcs_80);
2795 		arg->peer_he_tx_mcs_set[WMI_HECAP_TXRX_MCS_NSS_IDX_80] = v;
2796 
2797 		arg->peer_he_mcs_count++;
2798 		if (!he_tx_mcs)
2799 			he_tx_mcs = v;
2800 		break;
2801 	}
2802 
2803 	/* Calculate peer NSS capability from HE capabilities if STA
2804 	 * supports HE.
2805 	 */
2806 	for (i = 0, max_nss = 0, he_mcs = 0; i < NL80211_HE_NSS_MAX; i++) {
2807 		he_mcs = he_tx_mcs >> (2 * i) & 3;
2808 
2809 		/* In case of fixed rates, MCS Range in he_tx_mcs might have
2810 		 * unsupported range, with he_mcs_mask set, so check either of them
2811 		 * to find nss.
2812 		 */
2813 		if (he_mcs != IEEE80211_HE_MCS_NOT_SUPPORTED ||
2814 		    he_mcs_mask[i])
2815 			max_nss = i + 1;
2816 	}
2817 
2818 	max_nss = min(max_nss, ar->num_tx_chains);
2819 	arg->peer_nss = min(link_sta->rx_nss, max_nss);
2820 
2821 	if (arg->peer_phymode == MODE_11AX_HE160) {
2822 		tx_nss = ath12k_get_nss_160mhz(ar, ar->num_tx_chains);
2823 		rx_nss = min(arg->peer_nss, tx_nss);
2824 
2825 		arg->peer_nss = min(link_sta->rx_nss, ar->num_rx_chains);
2826 		arg->peer_bw_rxnss_override = ATH12K_BW_NSS_MAP_ENABLE;
2827 
2828 		if (!rx_nss) {
2829 			ath12k_warn(ar->ab, "invalid max_nss\n");
2830 			return;
2831 		}
2832 
2833 		nss_160 = u32_encode_bits(rx_nss - 1, ATH12K_PEER_RX_NSS_160MHZ);
2834 		arg->peer_bw_rxnss_override |= nss_160;
2835 	}
2836 
2837 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
2838 		   "mac he peer %pM nss %d mcs cnt %d nss_override 0x%x\n",
2839 		   arsta->addr, arg->peer_nss,
2840 		   arg->peer_he_mcs_count,
2841 		   arg->peer_bw_rxnss_override);
2842 }
2843 
2844 static void ath12k_peer_assoc_h_he_6ghz(struct ath12k *ar,
2845 					struct ath12k_link_vif *arvif,
2846 					struct ath12k_link_sta *arsta,
2847 					struct ath12k_wmi_peer_assoc_arg *arg)
2848 {
2849 	struct ieee80211_vif *vif = ath12k_ahvif_to_vif(arvif->ahvif);
2850 	struct ieee80211_sta *sta = ath12k_ahsta_to_sta(arsta->ahsta);
2851 	const struct ieee80211_sta_he_cap *he_cap;
2852 	struct ieee80211_link_sta *link_sta;
2853 	struct cfg80211_chan_def def;
2854 	enum nl80211_band band;
2855 	u8 ampdu_factor, mpdu_density;
2856 
2857 	if (WARN_ON(ath12k_mac_vif_link_chan(vif, arvif->link_id, &def)))
2858 		return;
2859 
2860 	band = def.chan->band;
2861 
2862 	link_sta = ath12k_mac_get_link_sta(arsta);
2863 	if (!link_sta) {
2864 		ath12k_warn(ar->ab, "unable to access link sta in peer assoc he 6ghz for sta %pM link %u\n",
2865 			    sta->addr, arsta->link_id);
2866 		return;
2867 	}
2868 
2869 	he_cap = &link_sta->he_cap;
2870 
2871 	if (!arg->he_flag || band != NL80211_BAND_6GHZ || !link_sta->he_6ghz_capa.capa)
2872 		return;
2873 
2874 	if (link_sta->bandwidth == IEEE80211_STA_RX_BW_40)
2875 		arg->bw_40 = true;
2876 
2877 	if (link_sta->bandwidth == IEEE80211_STA_RX_BW_80)
2878 		arg->bw_80 = true;
2879 
2880 	if (link_sta->bandwidth == IEEE80211_STA_RX_BW_160)
2881 		arg->bw_160 = true;
2882 
2883 	if (link_sta->bandwidth == IEEE80211_STA_RX_BW_320)
2884 		arg->bw_320 = true;
2885 
2886 	arg->peer_he_caps_6ghz = le16_to_cpu(link_sta->he_6ghz_capa.capa);
2887 
2888 	mpdu_density = u32_get_bits(arg->peer_he_caps_6ghz,
2889 				    IEEE80211_HE_6GHZ_CAP_MIN_MPDU_START);
2890 	arg->peer_mpdu_density = ath12k_parse_mpdudensity(mpdu_density);
2891 
2892 	/* From IEEE Std 802.11ax-2021 - Section 10.12.2: An HE STA shall be capable of
2893 	 * receiving A-MPDU where the A-MPDU pre-EOF padding length is up to the value
2894 	 * indicated by the Maximum A-MPDU Length Exponent Extension field in the HE
2895 	 * Capabilities element and the Maximum A-MPDU Length Exponent field in HE 6 GHz
2896 	 * Band Capabilities element in the 6 GHz band.
2897 	 *
2898 	 * Here, we are extracting the Max A-MPDU Exponent Extension from HE caps and
2899 	 * factor is the Maximum A-MPDU Length Exponent from HE 6 GHZ Band capability.
2900 	 */
2901 	ampdu_factor = u8_get_bits(he_cap->he_cap_elem.mac_cap_info[3],
2902 				   IEEE80211_HE_MAC_CAP3_MAX_AMPDU_LEN_EXP_MASK) +
2903 			u32_get_bits(arg->peer_he_caps_6ghz,
2904 				     IEEE80211_HE_6GHZ_CAP_MAX_AMPDU_LEN_EXP);
2905 
2906 	arg->peer_max_mpdu = (1u << (IEEE80211_HE_6GHZ_MAX_AMPDU_FACTOR +
2907 				     ampdu_factor)) - 1;
2908 }
2909 
2910 static int ath12k_get_smps_from_capa(const struct ieee80211_sta_ht_cap *ht_cap,
2911 				     const struct ieee80211_he_6ghz_capa *he_6ghz_capa,
2912 				     int *smps)
2913 {
2914 	if (ht_cap->ht_supported)
2915 		*smps = u16_get_bits(ht_cap->cap, IEEE80211_HT_CAP_SM_PS);
2916 	else
2917 		*smps = le16_get_bits(he_6ghz_capa->capa,
2918 				      IEEE80211_HE_6GHZ_CAP_SM_PS);
2919 
2920 	if (*smps >= ARRAY_SIZE(ath12k_smps_map))
2921 		return -EINVAL;
2922 
2923 	return 0;
2924 }
2925 
2926 static void ath12k_peer_assoc_h_smps(struct ath12k_link_sta *arsta,
2927 				     struct ath12k_wmi_peer_assoc_arg *arg)
2928 {
2929 	struct ieee80211_sta *sta = ath12k_ahsta_to_sta(arsta->ahsta);
2930 	const struct ieee80211_he_6ghz_capa *he_6ghz_capa;
2931 	struct ath12k_link_vif *arvif = arsta->arvif;
2932 	const struct ieee80211_sta_ht_cap *ht_cap;
2933 	struct ieee80211_link_sta *link_sta;
2934 	struct ath12k *ar = arvif->ar;
2935 	int smps;
2936 
2937 	link_sta = ath12k_mac_get_link_sta(arsta);
2938 	if (!link_sta) {
2939 		ath12k_warn(ar->ab, "unable to access link sta in peer assoc he for sta %pM link %u\n",
2940 			    sta->addr, arsta->link_id);
2941 		return;
2942 	}
2943 
2944 	he_6ghz_capa = &link_sta->he_6ghz_capa;
2945 	ht_cap = &link_sta->ht_cap;
2946 
2947 	if (!ht_cap->ht_supported && !he_6ghz_capa->capa)
2948 		return;
2949 
2950 	if (ath12k_get_smps_from_capa(ht_cap, he_6ghz_capa, &smps))
2951 		return;
2952 
2953 	switch (smps) {
2954 	case WLAN_HT_CAP_SM_PS_STATIC:
2955 		arg->static_mimops_flag = true;
2956 		break;
2957 	case WLAN_HT_CAP_SM_PS_DYNAMIC:
2958 		arg->dynamic_mimops_flag = true;
2959 		break;
2960 	case WLAN_HT_CAP_SM_PS_DISABLED:
2961 		arg->spatial_mux_flag = true;
2962 		break;
2963 	default:
2964 		break;
2965 	}
2966 }
2967 
2968 static void ath12k_peer_assoc_h_qos(struct ath12k *ar,
2969 				    struct ath12k_link_vif *arvif,
2970 				    struct ath12k_link_sta *arsta,
2971 				    struct ath12k_wmi_peer_assoc_arg *arg)
2972 {
2973 	struct ieee80211_sta *sta = ath12k_ahsta_to_sta(arsta->ahsta);
2974 
2975 	switch (arvif->ahvif->vdev_type) {
2976 	case WMI_VDEV_TYPE_AP:
2977 		if (sta->wme) {
2978 			/* TODO: Check WME vs QoS */
2979 			arg->is_wme_set = true;
2980 			arg->qos_flag = true;
2981 		}
2982 
2983 		if (sta->wme && sta->uapsd_queues) {
2984 			/* TODO: Check WME vs QoS */
2985 			arg->is_wme_set = true;
2986 			arg->apsd_flag = true;
2987 			arg->peer_rate_caps |= WMI_HOST_RC_UAPSD_FLAG;
2988 		}
2989 		break;
2990 	case WMI_VDEV_TYPE_STA:
2991 		if (sta->wme) {
2992 			arg->is_wme_set = true;
2993 			arg->qos_flag = true;
2994 		}
2995 		break;
2996 	default:
2997 		break;
2998 	}
2999 
3000 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac peer %pM qos %d\n",
3001 		   arsta->addr, arg->qos_flag);
3002 }
3003 
3004 static int ath12k_peer_assoc_qos_ap(struct ath12k *ar,
3005 				    struct ath12k_link_vif *arvif,
3006 				    struct ath12k_link_sta *arsta)
3007 {
3008 	struct ieee80211_sta *sta = ath12k_ahsta_to_sta(arsta->ahsta);
3009 	struct ath12k_wmi_ap_ps_arg arg;
3010 	u32 max_sp;
3011 	u32 uapsd;
3012 	int ret;
3013 
3014 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
3015 
3016 	arg.vdev_id = arvif->vdev_id;
3017 
3018 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac uapsd_queues 0x%x max_sp %d\n",
3019 		   sta->uapsd_queues, sta->max_sp);
3020 
3021 	uapsd = 0;
3022 	if (sta->uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_VO)
3023 		uapsd |= WMI_AP_PS_UAPSD_AC3_DELIVERY_EN |
3024 			 WMI_AP_PS_UAPSD_AC3_TRIGGER_EN;
3025 	if (sta->uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_VI)
3026 		uapsd |= WMI_AP_PS_UAPSD_AC2_DELIVERY_EN |
3027 			 WMI_AP_PS_UAPSD_AC2_TRIGGER_EN;
3028 	if (sta->uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_BK)
3029 		uapsd |= WMI_AP_PS_UAPSD_AC1_DELIVERY_EN |
3030 			 WMI_AP_PS_UAPSD_AC1_TRIGGER_EN;
3031 	if (sta->uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_BE)
3032 		uapsd |= WMI_AP_PS_UAPSD_AC0_DELIVERY_EN |
3033 			 WMI_AP_PS_UAPSD_AC0_TRIGGER_EN;
3034 
3035 	max_sp = 0;
3036 	if (sta->max_sp < MAX_WMI_AP_PS_PEER_PARAM_MAX_SP)
3037 		max_sp = sta->max_sp;
3038 
3039 	arg.param = WMI_AP_PS_PEER_PARAM_UAPSD;
3040 	arg.value = uapsd;
3041 	ret = ath12k_wmi_send_set_ap_ps_param_cmd(ar, arsta->addr, &arg);
3042 	if (ret)
3043 		goto err;
3044 
3045 	arg.param = WMI_AP_PS_PEER_PARAM_MAX_SP;
3046 	arg.value = max_sp;
3047 	ret = ath12k_wmi_send_set_ap_ps_param_cmd(ar, arsta->addr, &arg);
3048 	if (ret)
3049 		goto err;
3050 
3051 	/* TODO: revisit during testing */
3052 	arg.param = WMI_AP_PS_PEER_PARAM_SIFS_RESP_FRMTYPE;
3053 	arg.value = DISABLE_SIFS_RESPONSE_TRIGGER;
3054 	ret = ath12k_wmi_send_set_ap_ps_param_cmd(ar, arsta->addr, &arg);
3055 	if (ret)
3056 		goto err;
3057 
3058 	arg.param = WMI_AP_PS_PEER_PARAM_SIFS_RESP_UAPSD;
3059 	arg.value = DISABLE_SIFS_RESPONSE_TRIGGER;
3060 	ret = ath12k_wmi_send_set_ap_ps_param_cmd(ar, arsta->addr, &arg);
3061 	if (ret)
3062 		goto err;
3063 
3064 	return 0;
3065 
3066 err:
3067 	ath12k_warn(ar->ab, "failed to set ap ps peer param %d for vdev %i: %d\n",
3068 		    arg.param, arvif->vdev_id, ret);
3069 	return ret;
3070 }
3071 
3072 static bool ath12k_mac_sta_has_ofdm_only(struct ieee80211_link_sta *sta)
3073 {
3074 	return sta->supp_rates[NL80211_BAND_2GHZ] >>
3075 	       ATH12K_MAC_FIRST_OFDM_RATE_IDX;
3076 }
3077 
3078 static enum wmi_phy_mode ath12k_mac_get_phymode_vht(struct ath12k *ar,
3079 						    struct ieee80211_link_sta *link_sta)
3080 {
3081 	if (link_sta->bandwidth == IEEE80211_STA_RX_BW_160) {
3082 		if (link_sta->vht_cap.cap & (IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_160MHZ |
3083 		    IEEE80211_VHT_CAP_EXT_NSS_BW_MASK))
3084 			return MODE_11AC_VHT160;
3085 
3086 		/* Allow STA to connect even if it does not explicitly advertise 160 MHz
3087 		 * support
3088 		 */
3089 		return MODE_11AC_VHT160;
3090 	}
3091 
3092 	if (link_sta->bandwidth == IEEE80211_STA_RX_BW_80)
3093 		return MODE_11AC_VHT80;
3094 
3095 	if (link_sta->bandwidth == IEEE80211_STA_RX_BW_40)
3096 		return MODE_11AC_VHT40;
3097 
3098 	if (link_sta->bandwidth == IEEE80211_STA_RX_BW_20)
3099 		return MODE_11AC_VHT20;
3100 
3101 	return MODE_UNKNOWN;
3102 }
3103 
3104 static enum wmi_phy_mode ath12k_mac_get_phymode_he(struct ath12k *ar,
3105 						   struct ieee80211_link_sta *link_sta)
3106 {
3107 	if (link_sta->bandwidth == IEEE80211_STA_RX_BW_160) {
3108 		if (link_sta->he_cap.he_cap_elem.phy_cap_info[0] &
3109 		     IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_160MHZ_IN_5G)
3110 			return MODE_11AX_HE160;
3111 
3112 		return MODE_UNKNOWN;
3113 	}
3114 
3115 	if (link_sta->bandwidth == IEEE80211_STA_RX_BW_80)
3116 		return MODE_11AX_HE80;
3117 
3118 	if (link_sta->bandwidth == IEEE80211_STA_RX_BW_40)
3119 		return MODE_11AX_HE40;
3120 
3121 	if (link_sta->bandwidth == IEEE80211_STA_RX_BW_20)
3122 		return MODE_11AX_HE20;
3123 
3124 	return MODE_UNKNOWN;
3125 }
3126 
3127 static enum wmi_phy_mode ath12k_mac_get_phymode_eht(struct ath12k *ar,
3128 						    struct ieee80211_link_sta *link_sta)
3129 {
3130 	if (link_sta->bandwidth == IEEE80211_STA_RX_BW_320)
3131 		if (link_sta->eht_cap.eht_cap_elem.phy_cap_info[0] &
3132 		    IEEE80211_EHT_PHY_CAP0_320MHZ_IN_6GHZ)
3133 			return MODE_11BE_EHT320;
3134 
3135 	if (link_sta->bandwidth == IEEE80211_STA_RX_BW_160) {
3136 		if (link_sta->he_cap.he_cap_elem.phy_cap_info[0] &
3137 		    IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_160MHZ_IN_5G)
3138 			return MODE_11BE_EHT160;
3139 
3140 		ath12k_warn(ar->ab, "invalid EHT PHY capability info for 160 Mhz: %d\n",
3141 			    link_sta->he_cap.he_cap_elem.phy_cap_info[0]);
3142 
3143 		return MODE_UNKNOWN;
3144 	}
3145 
3146 	if (link_sta->bandwidth == IEEE80211_STA_RX_BW_80)
3147 		return MODE_11BE_EHT80;
3148 
3149 	if (link_sta->bandwidth == IEEE80211_STA_RX_BW_40)
3150 		return MODE_11BE_EHT40;
3151 
3152 	if (link_sta->bandwidth == IEEE80211_STA_RX_BW_20)
3153 		return MODE_11BE_EHT20;
3154 
3155 	return MODE_UNKNOWN;
3156 }
3157 
3158 static bool
3159 ath12k_peer_assoc_h_eht_masked(const u16 eht_mcs_mask[NL80211_EHT_NSS_MAX])
3160 {
3161 	int nss;
3162 
3163 	for (nss = 0; nss < NL80211_EHT_NSS_MAX; nss++)
3164 		if (eht_mcs_mask[nss])
3165 			return false;
3166 
3167 	return true;
3168 }
3169 
3170 static void ath12k_peer_assoc_h_phymode(struct ath12k *ar,
3171 					struct ath12k_link_vif *arvif,
3172 					struct ath12k_link_sta *arsta,
3173 					struct ath12k_wmi_peer_assoc_arg *arg)
3174 {
3175 	struct ieee80211_link_sta *link_sta;
3176 	struct cfg80211_chan_def def;
3177 	enum nl80211_band band;
3178 	const u8 *ht_mcs_mask;
3179 	const u16 *vht_mcs_mask;
3180 	const u16 *he_mcs_mask;
3181 	const u16 *eht_mcs_mask;
3182 	enum wmi_phy_mode phymode = MODE_UNKNOWN;
3183 
3184 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
3185 
3186 	struct ieee80211_vif *vif = ath12k_ahvif_to_vif(arvif->ahvif);
3187 	struct ieee80211_sta *sta = ath12k_ahsta_to_sta(arsta->ahsta);
3188 
3189 	if (WARN_ON(ath12k_mac_vif_link_chan(vif, arvif->link_id, &def)))
3190 		return;
3191 
3192 	band = def.chan->band;
3193 	ht_mcs_mask = arvif->bitrate_mask.control[band].ht_mcs;
3194 	vht_mcs_mask = arvif->bitrate_mask.control[band].vht_mcs;
3195 	he_mcs_mask = arvif->bitrate_mask.control[band].he_mcs;
3196 	eht_mcs_mask = arvif->bitrate_mask.control[band].eht_mcs;
3197 
3198 	link_sta = ath12k_mac_get_link_sta(arsta);
3199 	if (!link_sta) {
3200 		ath12k_warn(ar->ab, "unable to access link sta in peer assoc he for sta %pM link %u\n",
3201 			    sta->addr, arsta->link_id);
3202 		return;
3203 	}
3204 
3205 	switch (band) {
3206 	case NL80211_BAND_2GHZ:
3207 		if (link_sta->eht_cap.has_eht &&
3208 		    !ath12k_peer_assoc_h_eht_masked(eht_mcs_mask)) {
3209 			if (link_sta->bandwidth == IEEE80211_STA_RX_BW_40)
3210 				phymode = MODE_11BE_EHT40_2G;
3211 			else
3212 				phymode = MODE_11BE_EHT20_2G;
3213 		} else if (link_sta->he_cap.has_he &&
3214 			   !ath12k_peer_assoc_h_he_masked(he_mcs_mask)) {
3215 			if (link_sta->bandwidth == IEEE80211_STA_RX_BW_80)
3216 				phymode = MODE_11AX_HE80_2G;
3217 			else if (link_sta->bandwidth == IEEE80211_STA_RX_BW_40)
3218 				phymode = MODE_11AX_HE40_2G;
3219 			else
3220 				phymode = MODE_11AX_HE20_2G;
3221 		} else if (link_sta->vht_cap.vht_supported &&
3222 		    !ath12k_peer_assoc_h_vht_masked(vht_mcs_mask)) {
3223 			if (link_sta->bandwidth == IEEE80211_STA_RX_BW_40)
3224 				phymode = MODE_11AC_VHT40;
3225 			else
3226 				phymode = MODE_11AC_VHT20;
3227 		} else if (link_sta->ht_cap.ht_supported &&
3228 			   !ath12k_peer_assoc_h_ht_masked(ht_mcs_mask)) {
3229 			if (link_sta->bandwidth == IEEE80211_STA_RX_BW_40)
3230 				phymode = MODE_11NG_HT40;
3231 			else
3232 				phymode = MODE_11NG_HT20;
3233 		} else if (ath12k_mac_sta_has_ofdm_only(link_sta)) {
3234 			phymode = MODE_11G;
3235 		} else {
3236 			phymode = MODE_11B;
3237 		}
3238 		break;
3239 	case NL80211_BAND_5GHZ:
3240 	case NL80211_BAND_6GHZ:
3241 		/* Check EHT first */
3242 		if (link_sta->eht_cap.has_eht) {
3243 			phymode = ath12k_mac_get_phymode_eht(ar, link_sta);
3244 		} else if (link_sta->he_cap.has_he &&
3245 			   !ath12k_peer_assoc_h_he_masked(he_mcs_mask)) {
3246 			phymode = ath12k_mac_get_phymode_he(ar, link_sta);
3247 		} else if (link_sta->vht_cap.vht_supported &&
3248 		    !ath12k_peer_assoc_h_vht_masked(vht_mcs_mask)) {
3249 			phymode = ath12k_mac_get_phymode_vht(ar, link_sta);
3250 		} else if (link_sta->ht_cap.ht_supported &&
3251 			   !ath12k_peer_assoc_h_ht_masked(ht_mcs_mask)) {
3252 			if (link_sta->bandwidth >= IEEE80211_STA_RX_BW_40)
3253 				phymode = MODE_11NA_HT40;
3254 			else
3255 				phymode = MODE_11NA_HT20;
3256 		} else {
3257 			phymode = MODE_11A;
3258 		}
3259 		break;
3260 	default:
3261 		break;
3262 	}
3263 
3264 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac peer %pM phymode %s\n",
3265 		   arsta->addr, ath12k_mac_phymode_str(phymode));
3266 
3267 	arg->peer_phymode = phymode;
3268 	WARN_ON(phymode == MODE_UNKNOWN);
3269 }
3270 
3271 #define ATH12K_EHT_MCS_7_ENABLED	0x00FF
3272 #define ATH12K_EHT_MCS_9_ENABLED	0x0300
3273 #define ATH12K_EHT_MCS_11_ENABLED	0x0C00
3274 #define ATH12K_EHT_MCS_13_ENABLED	0x3000
3275 
3276 static void ath12k_mac_set_eht_mcs(u8 rx_tx_mcs7, u8 rx_tx_mcs9,
3277 				   u8 rx_tx_mcs11, u8 rx_tx_mcs13,
3278 				   u32 *rx_mcs, u32 *tx_mcs,
3279 				   const u16 eht_mcs_limit[NL80211_EHT_NSS_MAX])
3280 {
3281 	int nss;
3282 	u8 mcs_7 = 0, mcs_9 = 0, mcs_11 = 0, mcs_13 = 0;
3283 	u8 peer_mcs_7, peer_mcs_9, peer_mcs_11, peer_mcs_13;
3284 
3285 	for (nss = 0; nss < NL80211_EHT_NSS_MAX; nss++) {
3286 		if (eht_mcs_limit[nss] & ATH12K_EHT_MCS_7_ENABLED)
3287 			mcs_7++;
3288 		if (eht_mcs_limit[nss] & ATH12K_EHT_MCS_9_ENABLED)
3289 			mcs_9++;
3290 		if (eht_mcs_limit[nss] & ATH12K_EHT_MCS_11_ENABLED)
3291 			mcs_11++;
3292 		if (eht_mcs_limit[nss] & ATH12K_EHT_MCS_13_ENABLED)
3293 			mcs_13++;
3294 	}
3295 
3296 	peer_mcs_7 = u8_get_bits(rx_tx_mcs7, IEEE80211_EHT_MCS_NSS_RX);
3297 	peer_mcs_9 = u8_get_bits(rx_tx_mcs9, IEEE80211_EHT_MCS_NSS_RX);
3298 	peer_mcs_11 = u8_get_bits(rx_tx_mcs11, IEEE80211_EHT_MCS_NSS_RX);
3299 	peer_mcs_13 = u8_get_bits(rx_tx_mcs13, IEEE80211_EHT_MCS_NSS_RX);
3300 
3301 	*rx_mcs = u32_encode_bits(min(peer_mcs_7, mcs_7), WMI_EHT_MCS_NSS_0_7) |
3302 		  u32_encode_bits(min(peer_mcs_9, mcs_9), WMI_EHT_MCS_NSS_8_9) |
3303 		  u32_encode_bits(min(peer_mcs_11, mcs_11), WMI_EHT_MCS_NSS_10_11) |
3304 		  u32_encode_bits(min(peer_mcs_13, mcs_13), WMI_EHT_MCS_NSS_12_13);
3305 
3306 	peer_mcs_7 = u8_get_bits(rx_tx_mcs7, IEEE80211_EHT_MCS_NSS_TX);
3307 	peer_mcs_9 = u8_get_bits(rx_tx_mcs9, IEEE80211_EHT_MCS_NSS_TX);
3308 	peer_mcs_11 = u8_get_bits(rx_tx_mcs11, IEEE80211_EHT_MCS_NSS_TX);
3309 	peer_mcs_13 = u8_get_bits(rx_tx_mcs13, IEEE80211_EHT_MCS_NSS_TX);
3310 
3311 	*tx_mcs = u32_encode_bits(min(peer_mcs_7, mcs_7), WMI_EHT_MCS_NSS_0_7) |
3312 		  u32_encode_bits(min(peer_mcs_9, mcs_9), WMI_EHT_MCS_NSS_8_9) |
3313 		  u32_encode_bits(min(peer_mcs_11, mcs_11), WMI_EHT_MCS_NSS_10_11) |
3314 		  u32_encode_bits(min(peer_mcs_13, mcs_13), WMI_EHT_MCS_NSS_12_13);
3315 }
3316 
3317 static void ath12k_mac_set_eht_ppe_threshold(const u8 *ppe_thres,
3318 					     struct ath12k_wmi_ppe_threshold_arg *ppet)
3319 {
3320 	u32 bit_pos = IEEE80211_EHT_PPE_THRES_INFO_HEADER_SIZE, val;
3321 	u8 nss, ru, i;
3322 	u8 ppet_bit_len_per_ru = IEEE80211_EHT_PPE_THRES_INFO_PPET_SIZE * 2;
3323 
3324 	ppet->numss_m1 = u8_get_bits(ppe_thres[0], IEEE80211_EHT_PPE_THRES_NSS_MASK);
3325 	ppet->ru_bit_mask = u16_get_bits(get_unaligned_le16(ppe_thres),
3326 					 IEEE80211_EHT_PPE_THRES_RU_INDEX_BITMASK_MASK);
3327 
3328 	for (nss = 0; nss <= ppet->numss_m1; nss++) {
3329 		for (ru = 0;
3330 		     ru < hweight16(IEEE80211_EHT_PPE_THRES_RU_INDEX_BITMASK_MASK);
3331 		     ru++) {
3332 			if ((ppet->ru_bit_mask & BIT(ru)) == 0)
3333 				continue;
3334 
3335 			val = 0;
3336 			for (i = 0; i < ppet_bit_len_per_ru; i++) {
3337 				val |= (((ppe_thres[bit_pos / 8] >>
3338 					  (bit_pos % 8)) & 0x1) << i);
3339 				bit_pos++;
3340 			}
3341 			ppet->ppet16_ppet8_ru3_ru0[nss] |=
3342 					(val << (ru * ppet_bit_len_per_ru));
3343 		}
3344 	}
3345 }
3346 
3347 static void ath12k_peer_assoc_h_eht(struct ath12k *ar,
3348 				    struct ath12k_link_vif *arvif,
3349 				    struct ath12k_link_sta *arsta,
3350 				    struct ath12k_wmi_peer_assoc_arg *arg)
3351 {
3352 	struct ieee80211_sta *sta = ath12k_ahsta_to_sta(arsta->ahsta);
3353 	struct ieee80211_vif *vif = ath12k_ahvif_to_vif(arvif->ahvif);
3354 	const struct ieee80211_eht_mcs_nss_supp *own_eht_mcs_nss_supp;
3355 	const struct ieee80211_eht_mcs_nss_supp_20mhz_only *bw_20;
3356 	const struct ieee80211_sta_eht_cap *eht_cap, *own_eht_cap;
3357 	const struct ieee80211_sband_iftype_data *iftd;
3358 	const struct ieee80211_eht_mcs_nss_supp_bw *bw;
3359 	const struct ieee80211_sta_he_cap *he_cap;
3360 	struct ieee80211_link_sta *link_sta;
3361 	struct ieee80211_bss_conf *link_conf;
3362 	struct cfg80211_chan_def def;
3363 	bool user_rate_valid = true;
3364 	enum nl80211_band band;
3365 	int eht_nss, nss_idx;
3366 	u32 *rx_mcs, *tx_mcs;
3367 	u16 *eht_mcs_mask;
3368 	u8 max_nss = 0;
3369 	u8 mcs15;
3370 
3371 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
3372 
3373 	link_sta = ath12k_mac_get_link_sta(arsta);
3374 	if (!link_sta) {
3375 		ath12k_warn(ar->ab, "unable to access link sta in peer assoc eht for sta %pM link %u\n",
3376 			    sta->addr, arsta->link_id);
3377 		return;
3378 	}
3379 
3380 	link_conf = ath12k_mac_get_link_bss_conf(arvif);
3381 	if (!link_conf) {
3382 		ath12k_warn(ar->ab, "unable to access link_conf in peer assoc eht set\n");
3383 		return;
3384 	}
3385 
3386 	eht_cap = &link_sta->eht_cap;
3387 	he_cap = &link_sta->he_cap;
3388 	if (!he_cap->has_he || !eht_cap->has_eht)
3389 		return;
3390 
3391 	if (WARN_ON(ath12k_mac_vif_link_chan(vif, arvif->link_id, &def)))
3392 		return;
3393 
3394 	band = def.chan->band;
3395 	eht_mcs_mask = arvif->bitrate_mask.control[band].eht_mcs;
3396 
3397 	iftd = ieee80211_get_sband_iftype_data(&ar->mac.sbands[band], vif->type);
3398 	if (!iftd) {
3399 		ath12k_warn(ar->ab,
3400 			    "unable to access iftype_data in struct ieee80211_supported_band\n");
3401 		return;
3402 	}
3403 
3404 	own_eht_cap = &iftd->eht_cap;
3405 	own_eht_mcs_nss_supp = &own_eht_cap->eht_mcs_nss_supp;
3406 
3407 	arg->eht_flag = true;
3408 
3409 	if ((eht_cap->eht_cap_elem.phy_cap_info[5] &
3410 	     IEEE80211_EHT_PHY_CAP5_PPE_THRESHOLD_PRESENT) &&
3411 	    eht_cap->eht_ppe_thres[0] != 0)
3412 		ath12k_mac_set_eht_ppe_threshold(eht_cap->eht_ppe_thres,
3413 						 &arg->peer_eht_ppet);
3414 
3415 	memcpy(arg->peer_eht_cap_mac, eht_cap->eht_cap_elem.mac_cap_info,
3416 	       sizeof(eht_cap->eht_cap_elem.mac_cap_info));
3417 	memcpy(arg->peer_eht_cap_phy, eht_cap->eht_cap_elem.phy_cap_info,
3418 	       sizeof(eht_cap->eht_cap_elem.phy_cap_info));
3419 
3420 	/*
3421 	 * Firmware rate control enables EHT-MCS15 based only on the peer's
3422 	 * MCS15 support bits, so keep only the ones we support ourselves.
3423 	 */
3424 	mcs15 = u8_get_bits(eht_cap->eht_cap_elem.phy_cap_info[6] &
3425 			    own_eht_cap->eht_cap_elem.phy_cap_info[6],
3426 			    IEEE80211_EHT_PHY_CAP6_MCS15_SUPP_MASK);
3427 	u8p_replace_bits(&((u8 *)arg->peer_eht_cap_phy)[6], mcs15,
3428 			 IEEE80211_EHT_PHY_CAP6_MCS15_SUPP_MASK);
3429 
3430 	rx_mcs = arg->peer_eht_rx_mcs_set;
3431 	tx_mcs = arg->peer_eht_tx_mcs_set;
3432 
3433 	eht_nss = ath12k_mac_max_eht_mcs_nss((void *)own_eht_mcs_nss_supp,
3434 					     sizeof(*own_eht_mcs_nss_supp));
3435 	if (eht_nss > link_sta->rx_nss) {
3436 		user_rate_valid = false;
3437 		for (nss_idx = (link_sta->rx_nss - 1); nss_idx >= 0; nss_idx--) {
3438 			if (eht_mcs_mask[nss_idx]) {
3439 				user_rate_valid = true;
3440 				break;
3441 			}
3442 		}
3443 	}
3444 
3445 	if (!user_rate_valid) {
3446 		ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
3447 			   "Setting eht range MCS value to peer supported nss %d for peer %pM\n",
3448 			   link_sta->rx_nss, arsta->addr);
3449 		eht_mcs_mask[link_sta->rx_nss - 1] = eht_mcs_mask[eht_nss - 1];
3450 	}
3451 
3452 	bw_20 = &eht_cap->eht_mcs_nss_supp.only_20mhz;
3453 	bw = &eht_cap->eht_mcs_nss_supp.bw._80;
3454 
3455 	switch (link_sta->bandwidth) {
3456 	case IEEE80211_STA_RX_BW_320:
3457 		bw = &eht_cap->eht_mcs_nss_supp.bw._320;
3458 		ath12k_mac_set_eht_mcs(bw->rx_tx_mcs9_max_nss,
3459 				       bw->rx_tx_mcs9_max_nss,
3460 				       bw->rx_tx_mcs11_max_nss,
3461 				       bw->rx_tx_mcs13_max_nss,
3462 				       &rx_mcs[WMI_EHTCAP_TXRX_MCS_NSS_IDX_320],
3463 				       &tx_mcs[WMI_EHTCAP_TXRX_MCS_NSS_IDX_320],
3464 				       eht_mcs_mask);
3465 		arg->peer_eht_mcs_count++;
3466 		fallthrough;
3467 	case IEEE80211_STA_RX_BW_160:
3468 		bw = &eht_cap->eht_mcs_nss_supp.bw._160;
3469 		ath12k_mac_set_eht_mcs(bw->rx_tx_mcs9_max_nss,
3470 				       bw->rx_tx_mcs9_max_nss,
3471 				       bw->rx_tx_mcs11_max_nss,
3472 				       bw->rx_tx_mcs13_max_nss,
3473 				       &rx_mcs[WMI_EHTCAP_TXRX_MCS_NSS_IDX_160],
3474 				       &tx_mcs[WMI_EHTCAP_TXRX_MCS_NSS_IDX_160],
3475 				       eht_mcs_mask);
3476 		arg->peer_eht_mcs_count++;
3477 		fallthrough;
3478 	default:
3479 		if ((vif->type == NL80211_IFTYPE_AP ||
3480 		     vif->type == NL80211_IFTYPE_MESH_POINT) &&
3481 		    !(link_sta->he_cap.he_cap_elem.phy_cap_info[0] &
3482 		      IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_MASK_ALL)) {
3483 			bw_20 = &eht_cap->eht_mcs_nss_supp.only_20mhz;
3484 
3485 			ath12k_mac_set_eht_mcs(bw_20->rx_tx_mcs7_max_nss,
3486 					       bw_20->rx_tx_mcs9_max_nss,
3487 					       bw_20->rx_tx_mcs11_max_nss,
3488 					       bw_20->rx_tx_mcs13_max_nss,
3489 					       &rx_mcs[WMI_EHTCAP_TXRX_MCS_NSS_IDX_80],
3490 					       &tx_mcs[WMI_EHTCAP_TXRX_MCS_NSS_IDX_80],
3491 					       eht_mcs_mask);
3492 		} else {
3493 			bw = &eht_cap->eht_mcs_nss_supp.bw._80;
3494 			ath12k_mac_set_eht_mcs(bw->rx_tx_mcs9_max_nss,
3495 					       bw->rx_tx_mcs9_max_nss,
3496 					       bw->rx_tx_mcs11_max_nss,
3497 					       bw->rx_tx_mcs13_max_nss,
3498 					       &rx_mcs[WMI_EHTCAP_TXRX_MCS_NSS_IDX_80],
3499 					       &tx_mcs[WMI_EHTCAP_TXRX_MCS_NSS_IDX_80],
3500 					       eht_mcs_mask);
3501 		}
3502 
3503 		arg->peer_eht_mcs_count++;
3504 		break;
3505 	}
3506 
3507 	arg->punct_bitmap = ~arvif->punct_bitmap;
3508 	arg->eht_disable_mcs15 = link_conf->eht_disable_mcs15;
3509 
3510 	if ((vif->type == NL80211_IFTYPE_AP ||
3511 	     vif->type == NL80211_IFTYPE_MESH_POINT) &&
3512 	    !(link_sta->he_cap.he_cap_elem.phy_cap_info[0] &
3513 	      IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_MASK_ALL)) {
3514 		if (bw_20->rx_tx_mcs13_max_nss)
3515 			max_nss = max(max_nss, u8_get_bits(bw_20->rx_tx_mcs13_max_nss,
3516 							   IEEE80211_EHT_MCS_NSS_RX));
3517 		if (bw_20->rx_tx_mcs11_max_nss)
3518 			max_nss = max(max_nss, u8_get_bits(bw_20->rx_tx_mcs11_max_nss,
3519 							   IEEE80211_EHT_MCS_NSS_RX));
3520 		if (bw_20->rx_tx_mcs9_max_nss)
3521 			max_nss = max(max_nss, u8_get_bits(bw_20->rx_tx_mcs9_max_nss,
3522 							   IEEE80211_EHT_MCS_NSS_RX));
3523 		if (bw_20->rx_tx_mcs7_max_nss)
3524 			max_nss = max(max_nss, u8_get_bits(bw_20->rx_tx_mcs7_max_nss,
3525 							   IEEE80211_EHT_MCS_NSS_RX));
3526 	} else {
3527 		if (bw->rx_tx_mcs13_max_nss)
3528 			max_nss = max(max_nss, u8_get_bits(bw->rx_tx_mcs13_max_nss,
3529 							   IEEE80211_EHT_MCS_NSS_RX));
3530 		if (bw->rx_tx_mcs11_max_nss)
3531 			max_nss = max(max_nss, u8_get_bits(bw->rx_tx_mcs11_max_nss,
3532 							   IEEE80211_EHT_MCS_NSS_RX));
3533 		if (bw->rx_tx_mcs9_max_nss)
3534 			max_nss = max(max_nss, u8_get_bits(bw->rx_tx_mcs9_max_nss,
3535 							   IEEE80211_EHT_MCS_NSS_RX));
3536 	}
3537 
3538 	max_nss = min(max_nss, (u8)eht_nss);
3539 
3540 	arg->peer_nss = min(link_sta->rx_nss, max_nss);
3541 
3542 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
3543 		   "mac eht peer %pM nss %d mcs cnt %d ru_punct_bitmap 0x%x\n",
3544 		   arsta->addr, arg->peer_nss, arg->peer_eht_mcs_count,
3545 		   arg->punct_bitmap);
3546 }
3547 
3548 static void ath12k_peer_assoc_h_mlo(struct ath12k_link_sta *arsta,
3549 				    struct ath12k_wmi_peer_assoc_arg *arg)
3550 {
3551 	struct ieee80211_sta *sta = ath12k_ahsta_to_sta(arsta->ahsta);
3552 	struct peer_assoc_mlo_params *ml = &arg->ml;
3553 	struct ath12k_sta *ahsta = arsta->ahsta;
3554 	struct ath12k_link_sta *arsta_p;
3555 	struct ath12k_link_vif *arvif;
3556 	struct ath12k_hw *ah = arsta->arvif->ar->ah;
3557 	unsigned long links;
3558 	u8 link_id;
3559 	int i;
3560 
3561 	if (!sta->mlo)
3562 		return;
3563 
3564 	if (ah->host_alloc_ml_id &&
3565 	    ahsta->ml_peer_id == ATH12K_MLO_PEER_ID_INVALID)
3566 		return;
3567 
3568 	ml->enabled = true;
3569 	ml->assoc_link = arsta->is_assoc_link;
3570 
3571 	/* For now considering the primary umac based on assoc link */
3572 	ml->primary_umac = arsta->is_assoc_link;
3573 	/*
3574 	 * Only chips that allocate the MLD peer ID on the host send a valid
3575 	 * ml_peer_id in WMI_PEER_ASSOC_CMDID. For chips where the firmware
3576 	 * picks the ID, leave peer_id_valid false to avoid unexpected issues.
3577 	 */
3578 	ml->peer_id_valid = ah->host_alloc_ml_id;
3579 	ml->logical_link_idx_valid = true;
3580 
3581 	ether_addr_copy(ml->mld_addr, sta->addr);
3582 	ml->logical_link_idx = arsta->link_idx;
3583 	/*
3584 	 * WMI_MLO_PEER_ASSOC_PARAMS expects the raw ML peer ID without
3585 	 * the host-side ATH12K_PEER_ML_ID_VALID bookkeeping bit. For chips
3586 	 * where the firmware allocates the ID, the field is unused (the
3587 	 * firmware always allocates regardless of the value here); send 0
3588 	 * to make that intent explicit.
3589 	 */
3590 	ml->ml_peer_id = ah->host_alloc_ml_id ?
3591 			 (ahsta->ml_peer_id & ~ATH12K_PEER_ML_ID_VALID) : 0;
3592 	ml->ieee_link_id = arsta->link_id;
3593 	ml->num_partner_links = 0;
3594 	ml->eml_cap = sta->eml_cap;
3595 	links = ahsta->links_map;
3596 
3597 	rcu_read_lock();
3598 
3599 	i = 0;
3600 
3601 	for_each_set_bit(link_id, &links, IEEE80211_MLD_MAX_NUM_LINKS) {
3602 		if (i >= ATH12K_WMI_MLO_MAX_LINKS)
3603 			break;
3604 
3605 		arsta_p = rcu_dereference(ahsta->link[link_id]);
3606 		arvif = rcu_dereference(ahsta->ahvif->link[link_id]);
3607 
3608 		if (arsta_p == arsta)
3609 			continue;
3610 
3611 		if (!arvif->is_started)
3612 			continue;
3613 
3614 		ml->partner_info[i].vdev_id = arvif->vdev_id;
3615 		ml->partner_info[i].hw_link_id = arvif->ar->pdev->hw_link_id;
3616 		ml->partner_info[i].assoc_link = arsta_p->is_assoc_link;
3617 		ml->partner_info[i].primary_umac = arsta_p->is_assoc_link;
3618 		ml->partner_info[i].logical_link_idx_valid = true;
3619 		ml->partner_info[i].logical_link_idx = arsta_p->link_idx;
3620 		ml->num_partner_links++;
3621 
3622 		i++;
3623 	}
3624 
3625 	rcu_read_unlock();
3626 }
3627 
3628 static void ath12k_peer_assoc_prepare(struct ath12k *ar,
3629 				      struct ath12k_link_vif *arvif,
3630 				      struct ath12k_link_sta *arsta,
3631 				      struct ath12k_wmi_peer_assoc_arg *arg,
3632 				      bool reassoc)
3633 {
3634 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
3635 
3636 	memset(arg, 0, sizeof(*arg));
3637 
3638 	arg->peer_new_assoc = !reassoc;
3639 	ath12k_peer_assoc_h_basic(ar, arvif, arsta, arg);
3640 	ath12k_peer_assoc_h_crypto(ar, arvif, arsta, arg);
3641 	ath12k_peer_assoc_h_rates(ar, arvif, arsta, arg);
3642 	ath12k_peer_assoc_h_ht(ar, arvif, arsta, arg);
3643 	ath12k_peer_assoc_h_vht(ar, arvif, arsta, arg);
3644 	ath12k_peer_assoc_h_he(ar, arvif, arsta, arg);
3645 	ath12k_peer_assoc_h_he_6ghz(ar, arvif, arsta, arg);
3646 	ath12k_peer_assoc_h_eht(ar, arvif, arsta, arg);
3647 	ath12k_peer_assoc_h_qos(ar, arvif, arsta, arg);
3648 	ath12k_peer_assoc_h_phymode(ar, arvif, arsta, arg);
3649 	ath12k_peer_assoc_h_smps(arsta, arg);
3650 	ath12k_peer_assoc_h_mlo(arsta, arg);
3651 
3652 	arsta->peer_nss = arg->peer_nss;
3653 	/* TODO: amsdu_disable req? */
3654 }
3655 
3656 static int ath12k_setup_peer_smps(struct ath12k *ar, struct ath12k_link_vif *arvif,
3657 				  const u8 *addr,
3658 				  const struct ieee80211_sta_ht_cap *ht_cap,
3659 				  const struct ieee80211_he_6ghz_capa *he_6ghz_capa)
3660 {
3661 	int smps, ret = 0;
3662 
3663 	if (!ht_cap->ht_supported && !he_6ghz_capa)
3664 		return 0;
3665 
3666 	ret = ath12k_get_smps_from_capa(ht_cap, he_6ghz_capa, &smps);
3667 	if (ret < 0)
3668 		return ret;
3669 
3670 	return ath12k_wmi_set_peer_param(ar, addr, arvif->vdev_id,
3671 					 WMI_PEER_MIMO_PS_STATE,
3672 					 ath12k_smps_map[smps]);
3673 }
3674 
3675 static int ath12k_mac_set_he_txbf_conf(struct ath12k_link_vif *arvif)
3676 {
3677 	struct ath12k_vif *ahvif = arvif->ahvif;
3678 	struct ath12k *ar = arvif->ar;
3679 	u32 param = WMI_VDEV_PARAM_SET_HEMU_MODE;
3680 	u32 value = 0;
3681 	int ret;
3682 	struct ieee80211_bss_conf *link_conf;
3683 
3684 	link_conf = ath12k_mac_get_link_bss_conf(arvif);
3685 	if (!link_conf) {
3686 		ath12k_warn(ar->ab, "unable to access bss link conf in txbf conf\n");
3687 		return -EINVAL;
3688 	}
3689 
3690 	if (!link_conf->he_support)
3691 		return 0;
3692 
3693 	if (link_conf->he_su_beamformer) {
3694 		value |= u32_encode_bits(HE_SU_BFER_ENABLE, HE_MODE_SU_TX_BFER);
3695 		if (link_conf->he_mu_beamformer &&
3696 		    ahvif->vdev_type == WMI_VDEV_TYPE_AP)
3697 			value |= u32_encode_bits(HE_MU_BFER_ENABLE, HE_MODE_MU_TX_BFER);
3698 	}
3699 
3700 	if (ahvif->vif->type != NL80211_IFTYPE_MESH_POINT) {
3701 		value |= u32_encode_bits(HE_DL_MUOFDMA_ENABLE, HE_MODE_DL_OFDMA) |
3702 			 u32_encode_bits(HE_UL_MUOFDMA_ENABLE, HE_MODE_UL_OFDMA);
3703 
3704 		if (link_conf->he_full_ul_mumimo)
3705 			value |= u32_encode_bits(HE_UL_MUMIMO_ENABLE, HE_MODE_UL_MUMIMO);
3706 
3707 		if (link_conf->he_su_beamformee)
3708 			value |= u32_encode_bits(HE_SU_BFEE_ENABLE, HE_MODE_SU_TX_BFEE);
3709 	}
3710 
3711 	ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id, param, value);
3712 	if (ret) {
3713 		ath12k_warn(ar->ab, "failed to set vdev %d HE MU mode: %d\n",
3714 			    arvif->vdev_id, ret);
3715 		return ret;
3716 	}
3717 
3718 	param = WMI_VDEV_PARAM_SET_HE_SOUNDING_MODE;
3719 	value =	u32_encode_bits(HE_VHT_SOUNDING_MODE_ENABLE, HE_VHT_SOUNDING_MODE) |
3720 		u32_encode_bits(HE_TRIG_NONTRIG_SOUNDING_MODE_ENABLE,
3721 				HE_TRIG_NONTRIG_SOUNDING_MODE);
3722 	ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
3723 					    param, value);
3724 	if (ret) {
3725 		ath12k_warn(ar->ab, "failed to set vdev %d sounding mode: %d\n",
3726 			    arvif->vdev_id, ret);
3727 		return ret;
3728 	}
3729 
3730 	return 0;
3731 }
3732 
3733 static int ath12k_mac_vif_recalc_sta_he_txbf(struct ath12k *ar,
3734 					     struct ath12k_link_vif *arvif,
3735 					     struct ieee80211_sta_he_cap *he_cap,
3736 					     int *hemode)
3737 {
3738 	struct ieee80211_vif *vif = arvif->ahvif->vif;
3739 	struct ieee80211_he_cap_elem he_cap_elem = {};
3740 	struct ieee80211_sta_he_cap *cap_band;
3741 	struct cfg80211_chan_def def;
3742 	u8 link_id = arvif->link_id;
3743 	struct ieee80211_bss_conf *link_conf;
3744 
3745 	link_conf = ath12k_mac_get_link_bss_conf(arvif);
3746 	if (!link_conf) {
3747 		ath12k_warn(ar->ab, "unable to access bss link conf in recalc txbf conf\n");
3748 		return -EINVAL;
3749 	}
3750 
3751 	if (!link_conf->he_support)
3752 		return 0;
3753 
3754 	if (vif->type != NL80211_IFTYPE_STATION)
3755 		return -EINVAL;
3756 
3757 	if (WARN_ON(ath12k_mac_vif_link_chan(vif, link_id, &def)))
3758 		return -EINVAL;
3759 
3760 	if (def.chan->band == NL80211_BAND_2GHZ)
3761 		cap_band = &ar->mac.iftype[NL80211_BAND_2GHZ][vif->type].he_cap;
3762 	else
3763 		cap_band = &ar->mac.iftype[NL80211_BAND_5GHZ][vif->type].he_cap;
3764 
3765 	memcpy(&he_cap_elem, &cap_band->he_cap_elem, sizeof(he_cap_elem));
3766 
3767 	*hemode = 0;
3768 	if (HECAP_PHY_SUBFME_GET(he_cap_elem.phy_cap_info)) {
3769 		if (HECAP_PHY_SUBFMR_GET(he_cap->he_cap_elem.phy_cap_info))
3770 			*hemode |= u32_encode_bits(HE_SU_BFEE_ENABLE, HE_MODE_SU_TX_BFEE);
3771 		if (HECAP_PHY_MUBFMR_GET(he_cap->he_cap_elem.phy_cap_info))
3772 			*hemode |= u32_encode_bits(HE_MU_BFEE_ENABLE, HE_MODE_MU_TX_BFEE);
3773 	}
3774 
3775 	if (vif->type != NL80211_IFTYPE_MESH_POINT) {
3776 		*hemode |= u32_encode_bits(HE_DL_MUOFDMA_ENABLE, HE_MODE_DL_OFDMA) |
3777 			  u32_encode_bits(HE_UL_MUOFDMA_ENABLE, HE_MODE_UL_OFDMA);
3778 
3779 		if (HECAP_PHY_ULMUMIMO_GET(he_cap_elem.phy_cap_info))
3780 			if (HECAP_PHY_ULMUMIMO_GET(he_cap->he_cap_elem.phy_cap_info))
3781 				*hemode |= u32_encode_bits(HE_UL_MUMIMO_ENABLE,
3782 							  HE_MODE_UL_MUMIMO);
3783 
3784 		if (u32_get_bits(*hemode, HE_MODE_MU_TX_BFEE))
3785 			*hemode |= u32_encode_bits(HE_SU_BFEE_ENABLE, HE_MODE_SU_TX_BFEE);
3786 
3787 		if (u32_get_bits(*hemode, HE_MODE_MU_TX_BFER))
3788 			*hemode |= u32_encode_bits(HE_SU_BFER_ENABLE, HE_MODE_SU_TX_BFER);
3789 	}
3790 
3791 	return 0;
3792 }
3793 
3794 static int ath12k_mac_set_eht_txbf_conf(struct ath12k_link_vif *arvif)
3795 {
3796 	struct ath12k_vif *ahvif = arvif->ahvif;
3797 	struct ath12k *ar = arvif->ar;
3798 	u32 param = WMI_VDEV_PARAM_SET_EHT_MU_MODE;
3799 	u32 value = 0;
3800 	int ret;
3801 	struct ieee80211_bss_conf *link_conf;
3802 
3803 	link_conf = ath12k_mac_get_link_bss_conf(arvif);
3804 	if (!link_conf) {
3805 		ath12k_warn(ar->ab, "unable to access bss link conf in eht txbf conf\n");
3806 		return -ENOENT;
3807 	}
3808 
3809 	if (!link_conf->eht_support)
3810 		return 0;
3811 
3812 	if (link_conf->eht_su_beamformer) {
3813 		value |= u32_encode_bits(EHT_SU_BFER_ENABLE, EHT_MODE_SU_TX_BFER);
3814 		if (link_conf->eht_mu_beamformer &&
3815 		    ahvif->vdev_type == WMI_VDEV_TYPE_AP)
3816 			value |= u32_encode_bits(EHT_MU_BFER_ENABLE,
3817 						 EHT_MODE_MU_TX_BFER) |
3818 				 u32_encode_bits(EHT_DL_MUOFDMA_ENABLE,
3819 						 EHT_MODE_DL_OFDMA_MUMIMO) |
3820 				 u32_encode_bits(EHT_UL_MUOFDMA_ENABLE,
3821 						 EHT_MODE_UL_OFDMA_MUMIMO);
3822 	}
3823 
3824 	if (ahvif->vif->type != NL80211_IFTYPE_MESH_POINT) {
3825 		value |= u32_encode_bits(EHT_DL_MUOFDMA_ENABLE, EHT_MODE_DL_OFDMA) |
3826 			 u32_encode_bits(EHT_UL_MUOFDMA_ENABLE, EHT_MODE_UL_OFDMA);
3827 
3828 		if (link_conf->eht_80mhz_full_bw_ul_mumimo)
3829 			value |= u32_encode_bits(EHT_UL_MUMIMO_ENABLE, EHT_MODE_MUMIMO);
3830 
3831 		if (link_conf->eht_su_beamformee)
3832 			value |= u32_encode_bits(EHT_SU_BFEE_ENABLE,
3833 						 EHT_MODE_SU_TX_BFEE);
3834 	}
3835 
3836 	ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id, param, value);
3837 	if (ret) {
3838 		ath12k_warn(ar->ab, "failed to set vdev %d EHT MU mode: %d\n",
3839 			    arvif->vdev_id, ret);
3840 		return ret;
3841 	}
3842 
3843 	return 0;
3844 }
3845 
3846 static u32 ath12k_mac_ieee80211_sta_bw_to_wmi(struct ath12k *ar,
3847 					      struct ieee80211_link_sta *link_sta)
3848 {
3849 	u32 bw;
3850 
3851 	switch (link_sta->bandwidth) {
3852 	case IEEE80211_STA_RX_BW_20:
3853 		bw = WMI_PEER_CHWIDTH_20MHZ;
3854 		break;
3855 	case IEEE80211_STA_RX_BW_40:
3856 		bw = WMI_PEER_CHWIDTH_40MHZ;
3857 		break;
3858 	case IEEE80211_STA_RX_BW_80:
3859 		bw = WMI_PEER_CHWIDTH_80MHZ;
3860 		break;
3861 	case IEEE80211_STA_RX_BW_160:
3862 		bw = WMI_PEER_CHWIDTH_160MHZ;
3863 		break;
3864 	case IEEE80211_STA_RX_BW_320:
3865 		bw = WMI_PEER_CHWIDTH_320MHZ;
3866 		break;
3867 	default:
3868 		ath12k_warn(ar->ab, "Invalid bandwidth %d for link station %pM\n",
3869 			    link_sta->bandwidth, link_sta->addr);
3870 		bw = WMI_PEER_CHWIDTH_20MHZ;
3871 		break;
3872 	}
3873 
3874 	return bw;
3875 }
3876 
3877 static int ath12k_mac_peer_assoc(struct ath12k *ar,
3878 				 struct ath12k_wmi_peer_assoc_arg *peer_arg)
3879 {
3880 	struct ath12k_hw *ah = ath12k_ar_to_ah(ar);
3881 	int ret;
3882 
3883 	reinit_completion(&ar->peer_assoc_done);
3884 	reinit_completion(&ah->peer_ml_id_done);
3885 
3886 	ret = ath12k_wmi_send_peer_assoc_cmd(ar, peer_arg);
3887 	if (ret) {
3888 		ath12k_warn(ar->ab, "failed to run peer assoc for %pM vdev %i: %d\n",
3889 			    peer_arg->peer_mac, peer_arg->vdev_id, ret);
3890 		return ret;
3891 	}
3892 
3893 	if (!wait_for_completion_timeout(&ar->peer_assoc_done, 1 * HZ)) {
3894 		ath12k_warn(ar->ab, "failed to get peer assoc conf event for %pM vdev %i\n",
3895 			    peer_arg->peer_mac, peer_arg->vdev_id);
3896 		return -ETIMEDOUT;
3897 	}
3898 
3899 	/*
3900 	 * For devices where the firmware allocates the MLD peer ID, the host
3901 	 * learns the real ID only from the MLO_RX_PEER_MAP HTT event, which is
3902 	 * handled in a softirq (BH workqueue) context that cannot take the
3903 	 * wiphy lock. Block here, while still holding the wiphy lock, until
3904 	 * that event has fixed up the ID. This serialises the fixup against
3905 	 * all other wiphy-locked ml_peer_id accesses.
3906 	 *
3907 	 * The firmware sends the event only once, in response to the assoc-link
3908 	 * peer assoc, so block only for that link.
3909 	 */
3910 	if (!ah->host_alloc_ml_id &&
3911 	    peer_arg->is_assoc &&
3912 	    peer_arg->ml.enabled &&
3913 	    peer_arg->ml.assoc_link &&
3914 	    !wait_for_completion_timeout(&ah->peer_ml_id_done, 1 * HZ)) {
3915 		ath12k_warn(ar->ab, "failed to get MLO peer map event for %pM vdev %i\n",
3916 			    peer_arg->peer_mac, peer_arg->vdev_id);
3917 		return -ETIMEDOUT;
3918 	}
3919 
3920 	return 0;
3921 }
3922 
3923 static void ath12k_bss_assoc(struct ath12k *ar,
3924 			     struct ath12k_link_vif *arvif,
3925 			     struct ieee80211_bss_conf *bss_conf)
3926 {
3927 	struct ath12k_vif *ahvif = arvif->ahvif;
3928 	struct ieee80211_vif *vif = ath12k_ahvif_to_vif(ahvif);
3929 	struct ath12k_wmi_vdev_up_params params = {};
3930 	struct ieee80211_link_sta *link_sta;
3931 	u8 link_id = bss_conf->link_id;
3932 	struct ath12k_link_sta *arsta;
3933 	struct ieee80211_sta *ap_sta;
3934 	struct ath12k_sta *ahsta;
3935 	struct ath12k_dp_link_peer *peer;
3936 	bool is_auth = false;
3937 	u32 hemode = 0;
3938 	int ret;
3939 	struct ath12k_dp *dp = ath12k_ab_to_dp(ar->ab);
3940 
3941 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
3942 
3943 	struct ath12k_wmi_peer_assoc_arg *peer_arg __free(kfree) =
3944 					kzalloc_obj(*peer_arg);
3945 	if (!peer_arg)
3946 		return;
3947 
3948 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
3949 		   "mac vdev %i link id %u assoc bssid %pM aid %d\n",
3950 		   arvif->vdev_id, link_id, arvif->bssid, ahvif->aid);
3951 
3952 	rcu_read_lock();
3953 
3954 	/* During ML connection, cfg.ap_addr has the MLD address. For
3955 	 * non-ML connection, it has the BSSID.
3956 	 */
3957 	ap_sta = ieee80211_find_sta(vif, vif->cfg.ap_addr);
3958 	if (!ap_sta) {
3959 		ath12k_warn(ar->ab, "failed to find station entry for bss %pM vdev %i\n",
3960 			    vif->cfg.ap_addr, arvif->vdev_id);
3961 		rcu_read_unlock();
3962 		return;
3963 	}
3964 
3965 	ahsta = ath12k_sta_to_ahsta(ap_sta);
3966 
3967 	arsta = wiphy_dereference(ath12k_ar_to_hw(ar)->wiphy,
3968 				  ahsta->link[link_id]);
3969 	if (WARN_ON(!arsta)) {
3970 		rcu_read_unlock();
3971 		return;
3972 	}
3973 
3974 	link_sta = ath12k_mac_get_link_sta(arsta);
3975 	if (WARN_ON(!link_sta)) {
3976 		rcu_read_unlock();
3977 		return;
3978 	}
3979 
3980 	ath12k_peer_assoc_prepare(ar, arvif, arsta, peer_arg, false);
3981 
3982 	/* link_sta->he_cap must be protected by rcu_read_lock */
3983 	ret = ath12k_mac_vif_recalc_sta_he_txbf(ar, arvif, &link_sta->he_cap, &hemode);
3984 	if (ret) {
3985 		ath12k_warn(ar->ab, "failed to recalc he txbf for vdev %i on bss %pM: %d\n",
3986 			    arvif->vdev_id, bss_conf->bssid, ret);
3987 		rcu_read_unlock();
3988 		return;
3989 	}
3990 
3991 	rcu_read_unlock();
3992 
3993 	/* keep this before ath12k_wmi_send_peer_assoc_cmd() */
3994 	ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
3995 					    WMI_VDEV_PARAM_SET_HEMU_MODE, hemode);
3996 	if (ret) {
3997 		ath12k_warn(ar->ab, "failed to submit vdev param txbf 0x%x: %d\n",
3998 			    hemode, ret);
3999 		return;
4000 	}
4001 
4002 	peer_arg->is_assoc = true;
4003 
4004 	ret = ath12k_mac_peer_assoc(ar, peer_arg);
4005 	if (ret)
4006 		return;
4007 
4008 	ret = ath12k_setup_peer_smps(ar, arvif, bss_conf->bssid,
4009 				     &link_sta->ht_cap, &link_sta->he_6ghz_capa);
4010 	if (ret) {
4011 		ath12k_warn(ar->ab, "failed to setup peer SMPS for vdev %d: %d\n",
4012 			    arvif->vdev_id, ret);
4013 		return;
4014 	}
4015 
4016 	WARN_ON(arvif->is_up);
4017 
4018 	ahvif->aid = vif->cfg.aid;
4019 	ether_addr_copy(arvif->bssid, bss_conf->bssid);
4020 
4021 	params.vdev_id = arvif->vdev_id;
4022 	params.aid = ahvif->aid;
4023 	params.bssid = arvif->bssid;
4024 	params.tx_bssid = ath12k_mac_get_tx_bssid(arvif);
4025 	if (params.tx_bssid) {
4026 		params.nontx_profile_idx = bss_conf->bssid_index;
4027 		params.nontx_profile_cnt = 1 << bss_conf->bssid_indicator;
4028 	}
4029 	ret = ath12k_wmi_vdev_up(ar, &params);
4030 	if (ret) {
4031 		ath12k_warn(ar->ab, "failed to set vdev %d up: %d\n",
4032 			    arvif->vdev_id, ret);
4033 		return;
4034 	}
4035 
4036 	arvif->is_up = true;
4037 	arvif->rekey_data.enable_offload = false;
4038 
4039 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
4040 		   "mac vdev %d up (associated) bssid %pM aid %d\n",
4041 		   arvif->vdev_id, bss_conf->bssid, vif->cfg.aid);
4042 
4043 	spin_lock_bh(&dp->dp_lock);
4044 
4045 	peer = ath12k_dp_link_peer_find_by_vdev_and_addr(dp, arvif->vdev_id,
4046 							 arvif->bssid);
4047 	if (peer && peer->is_authorized)
4048 		is_auth = true;
4049 
4050 	spin_unlock_bh(&dp->dp_lock);
4051 
4052 	/* Authorize BSS Peer */
4053 	if (is_auth) {
4054 		ret = ath12k_wmi_set_peer_param(ar, arvif->bssid,
4055 						arvif->vdev_id,
4056 						WMI_PEER_AUTHORIZE,
4057 						1);
4058 		if (ret)
4059 			ath12k_warn(ar->ab, "Unable to authorize BSS peer: %d\n", ret);
4060 	}
4061 
4062 	ret = ath12k_wmi_send_obss_spr_cmd(ar, arvif->vdev_id,
4063 					   &bss_conf->he_obss_pd);
4064 	if (ret)
4065 		ath12k_warn(ar->ab, "failed to set vdev %i OBSS PD parameters: %d\n",
4066 			    arvif->vdev_id, ret);
4067 
4068 	if (ar->ab->hw_params->supports_sta_ps &&
4069 	    ahvif->vdev_type == WMI_VDEV_TYPE_STA &&
4070 	    ahvif->vdev_subtype == WMI_VDEV_SUBTYPE_NONE) {
4071 		ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
4072 						    WMI_VDEV_PARAM_DTIM_POLICY,
4073 						    WMI_DTIM_POLICY_STICK);
4074 		if (ret)
4075 			ath12k_warn(ar->ab, "failed to set vdev %d stick DTIM policy: %d\n",
4076 				    arvif->vdev_id, ret);
4077 	}
4078 
4079 	if (test_bit(WMI_TLV_SERVICE_11D_OFFLOAD, ar->ab->wmi_ab.svc_map) &&
4080 	    ahvif->vdev_type == WMI_VDEV_TYPE_STA &&
4081 	    ahvif->vdev_subtype == WMI_VDEV_SUBTYPE_NONE)
4082 		ath12k_mac_11d_scan_stop_all(ar->ab);
4083 }
4084 
4085 static void ath12k_bss_disassoc(struct ath12k *ar,
4086 				struct ath12k_link_vif *arvif)
4087 {
4088 	int ret;
4089 
4090 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
4091 
4092 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac vdev %i disassoc bssid %pM\n",
4093 		   arvif->vdev_id, arvif->bssid);
4094 
4095 	ret = ath12k_wmi_vdev_down(ar, arvif->vdev_id);
4096 	if (ret)
4097 		ath12k_warn(ar->ab, "failed to down vdev %i: %d\n",
4098 			    arvif->vdev_id, ret);
4099 
4100 	arvif->is_up = false;
4101 
4102 	memset(&arvif->rekey_data, 0, sizeof(arvif->rekey_data));
4103 
4104 	cancel_delayed_work(&arvif->connection_loss_work);
4105 }
4106 
4107 static u32 ath12k_mac_get_rate_hw_value(int bitrate)
4108 {
4109 	u32 preamble;
4110 	u16 hw_value;
4111 	int rate;
4112 	size_t i;
4113 
4114 	if (ath12k_mac_bitrate_is_cck(bitrate))
4115 		preamble = WMI_RATE_PREAMBLE_CCK;
4116 	else
4117 		preamble = WMI_RATE_PREAMBLE_OFDM;
4118 
4119 	for (i = 0; i < ARRAY_SIZE(ath12k_legacy_rates); i++) {
4120 		if (ath12k_legacy_rates[i].bitrate != bitrate)
4121 			continue;
4122 
4123 		hw_value = ath12k_legacy_rates[i].hw_value;
4124 		rate = ATH12K_HW_RATE_CODE(hw_value, 0, preamble);
4125 
4126 		return rate;
4127 	}
4128 
4129 	return -EINVAL;
4130 }
4131 
4132 static void ath12k_recalculate_mgmt_rate(struct ath12k *ar,
4133 					 struct ath12k_link_vif *arvif,
4134 					 struct cfg80211_chan_def *def)
4135 {
4136 	struct ieee80211_vif *vif = ath12k_ahvif_to_vif(arvif->ahvif);
4137 	struct ieee80211_hw *hw = ath12k_ar_to_hw(ar);
4138 	const struct ieee80211_supported_band *sband;
4139 	struct ieee80211_bss_conf *bss_conf;
4140 	u8 basic_rate_idx;
4141 	int hw_rate_code;
4142 	u32 vdev_param;
4143 	u16 bitrate;
4144 	int ret;
4145 
4146 	lockdep_assert_wiphy(hw->wiphy);
4147 
4148 	bss_conf = ath12k_mac_get_link_bss_conf(arvif);
4149 	if (!bss_conf) {
4150 		ath12k_warn(ar->ab, "unable to access bss link conf in mgmt rate calc for vif %pM link %u\n",
4151 			    vif->addr, arvif->link_id);
4152 		return;
4153 	}
4154 
4155 	sband = hw->wiphy->bands[def->chan->band];
4156 	if (bss_conf->basic_rates)
4157 		basic_rate_idx = __ffs(bss_conf->basic_rates);
4158 	else
4159 		basic_rate_idx = 0;
4160 	bitrate = sband->bitrates[basic_rate_idx].bitrate;
4161 
4162 	hw_rate_code = ath12k_mac_get_rate_hw_value(bitrate);
4163 	if (hw_rate_code < 0) {
4164 		ath12k_warn(ar->ab, "bitrate not supported %d\n", bitrate);
4165 		return;
4166 	}
4167 
4168 	vdev_param = WMI_VDEV_PARAM_MGMT_RATE;
4169 	ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id, vdev_param,
4170 					    hw_rate_code);
4171 	if (ret)
4172 		ath12k_warn(ar->ab, "failed to set mgmt tx rate %d\n", ret);
4173 
4174 	vdev_param = WMI_VDEV_PARAM_BEACON_RATE;
4175 	ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id, vdev_param,
4176 					    hw_rate_code);
4177 	if (ret)
4178 		ath12k_warn(ar->ab, "failed to set beacon tx rate %d\n", ret);
4179 }
4180 
4181 static void ath12k_mac_bcn_tx_event(struct ath12k_link_vif *arvif)
4182 {
4183 	struct ieee80211_vif *vif = ath12k_ahvif_to_vif(arvif->ahvif);
4184 	struct ieee80211_bss_conf *link_conf;
4185 
4186 	link_conf = ath12k_mac_get_link_bss_conf(arvif);
4187 	if (!link_conf) {
4188 		ath12k_warn(arvif->ar->ab, "failed to get link conf for vdev %u\n",
4189 			    arvif->vdev_id);
4190 		return;
4191 	}
4192 
4193 	if (link_conf->color_change_active) {
4194 		if (ieee80211_beacon_cntdwn_is_complete(vif, arvif->link_id)) {
4195 			ieee80211_color_change_finish(vif, arvif->link_id);
4196 			return;
4197 		}
4198 
4199 		ieee80211_beacon_update_cntdwn(vif, arvif->link_id);
4200 		ath12k_mac_setup_bcn_tmpl(arvif);
4201 	}
4202 }
4203 
4204 static void ath12k_mac_bcn_tx_work(struct wiphy *wiphy, struct wiphy_work *work)
4205 {
4206 	struct ath12k_link_vif *arvif = container_of(work, struct ath12k_link_vif,
4207 						     bcn_tx_work);
4208 
4209 	lockdep_assert_wiphy(wiphy);
4210 	ath12k_mac_bcn_tx_event(arvif);
4211 }
4212 
4213 static void ath12k_mac_init_arvif(struct ath12k_vif *ahvif,
4214 				  struct ath12k_link_vif *arvif, int link_id)
4215 {
4216 	struct ath12k_hw *ah = ahvif->ah;
4217 	u8 _link_id;
4218 	int i;
4219 
4220 	lockdep_assert_wiphy(ah->hw->wiphy);
4221 
4222 	if (WARN_ON(!arvif))
4223 		return;
4224 
4225 	if (WARN_ON(link_id >= ATH12K_NUM_MAX_LINKS))
4226 		return;
4227 
4228 	if (link_id < 0)
4229 		_link_id = 0;
4230 	else
4231 		_link_id = link_id;
4232 
4233 	arvif->ahvif = ahvif;
4234 	arvif->link_id = _link_id;
4235 
4236 	/* Protects the datapath stats update on a per link basis */
4237 	spin_lock_init(&arvif->link_stats_lock);
4238 
4239 	INIT_LIST_HEAD(&arvif->list);
4240 	INIT_DELAYED_WORK(&arvif->connection_loss_work,
4241 			  ath12k_mac_vif_sta_connection_loss_work);
4242 	wiphy_work_init(&arvif->bcn_tx_work, ath12k_mac_bcn_tx_work);
4243 
4244 	arvif->num_stations = 0;
4245 
4246 	for (i = 0; i < ARRAY_SIZE(arvif->bitrate_mask.control); i++) {
4247 		arvif->bitrate_mask.control[i].legacy = 0xffffffff;
4248 		arvif->bitrate_mask.control[i].gi = NL80211_TXRATE_DEFAULT_GI;
4249 		memset(arvif->bitrate_mask.control[i].ht_mcs, 0xff,
4250 		       sizeof(arvif->bitrate_mask.control[i].ht_mcs));
4251 		memset(arvif->bitrate_mask.control[i].vht_mcs, 0xff,
4252 		       sizeof(arvif->bitrate_mask.control[i].vht_mcs));
4253 		memset(arvif->bitrate_mask.control[i].he_mcs, 0xff,
4254 		       sizeof(arvif->bitrate_mask.control[i].he_mcs));
4255 		memset(arvif->bitrate_mask.control[i].eht_mcs, 0xff,
4256 		       sizeof(arvif->bitrate_mask.control[i].eht_mcs));
4257 	}
4258 
4259 	/* Handle MLO related assignments */
4260 	if (link_id >= 0) {
4261 		rcu_assign_pointer(ahvif->link[arvif->link_id], arvif);
4262 		ahvif->links_map |= BIT(_link_id);
4263 	}
4264 
4265 	ath12k_generic_dbg(ATH12K_DBG_MAC,
4266 			   "mac init link arvif (link_id %d%s) for vif %pM. links_map 0x%x",
4267 			   _link_id, (link_id < 0) ? " deflink" : "", ahvif->vif->addr,
4268 			   ahvif->links_map);
4269 }
4270 
4271 static void ath12k_mac_remove_link_interface(struct ieee80211_hw *hw,
4272 					     struct ath12k_link_vif *arvif)
4273 {
4274 	struct ath12k_vif *ahvif = arvif->ahvif;
4275 	struct ath12k_hw *ah = hw->priv;
4276 	struct ath12k *ar = arvif->ar;
4277 	int ret;
4278 
4279 	lockdep_assert_wiphy(ah->hw->wiphy);
4280 
4281 	cancel_delayed_work_sync(&arvif->connection_loss_work);
4282 	wiphy_work_cancel(ath12k_ar_to_hw(ar)->wiphy, &arvif->bcn_tx_work);
4283 
4284 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac remove link interface (vdev %d link id %d)",
4285 		   arvif->vdev_id, arvif->link_id);
4286 
4287 	if (test_bit(WMI_TLV_SERVICE_11D_OFFLOAD, ar->ab->wmi_ab.svc_map) &&
4288 	    ahvif->vdev_type == WMI_VDEV_TYPE_STA &&
4289 	    ahvif->vdev_subtype == WMI_VDEV_SUBTYPE_NONE)
4290 		ath12k_mac_11d_scan_stop(ar);
4291 
4292 	if (ahvif->vdev_type == WMI_VDEV_TYPE_AP) {
4293 		ret = ath12k_peer_delete(ar, arvif->vdev_id, arvif->bssid);
4294 		if (ret)
4295 			ath12k_warn(ar->ab, "failed to submit AP self-peer removal on vdev %d link id %d: %d",
4296 				    arvif->vdev_id, arvif->link_id, ret);
4297 
4298 		if (arvif->link_id < IEEE80211_MLD_MAX_NUM_LINKS)
4299 			ath12k_dp_peer_delete(&ah->dp_hw, arvif->bssid, NULL);
4300 	}
4301 	ath12k_mac_vdev_delete(ar, arvif);
4302 }
4303 
4304 static struct ath12k_link_vif *ath12k_mac_assign_link_vif(struct ath12k_hw *ah,
4305 							  struct ieee80211_vif *vif,
4306 							  u8 link_id)
4307 {
4308 	struct ath12k_vif *ahvif = ath12k_vif_to_ahvif(vif);
4309 	struct ath12k_link_vif *arvif;
4310 
4311 	lockdep_assert_wiphy(ah->hw->wiphy);
4312 
4313 	arvif = wiphy_dereference(ah->hw->wiphy, ahvif->link[link_id]);
4314 	if (arvif)
4315 		return arvif;
4316 
4317 	/* If this is the first link arvif being created for an ML VIF
4318 	 * use the preallocated deflink memory except for scan arvifs
4319 	 */
4320 	if (!ahvif->links_map && link_id < ATH12K_FIRST_SCAN_LINK) {
4321 		arvif = &ahvif->deflink;
4322 
4323 		if (vif->type == NL80211_IFTYPE_STATION)
4324 			arvif->is_sta_assoc_link = true;
4325 	} else {
4326 		arvif = kzalloc_obj(*arvif);
4327 		if (!arvif)
4328 			return NULL;
4329 	}
4330 
4331 	ath12k_mac_init_arvif(ahvif, arvif, link_id);
4332 
4333 	return arvif;
4334 }
4335 
4336 static void ath12k_mac_unassign_link_vif(struct ath12k_link_vif *arvif)
4337 {
4338 	struct ath12k_vif *ahvif = arvif->ahvif;
4339 	struct ath12k_hw *ah = ahvif->ah;
4340 
4341 	lockdep_assert_wiphy(ah->hw->wiphy);
4342 
4343 	rcu_assign_pointer(ahvif->link[arvif->link_id], NULL);
4344 	synchronize_rcu();
4345 	ahvif->links_map &= ~BIT(arvif->link_id);
4346 
4347 	if (arvif != &ahvif->deflink)
4348 		kfree(arvif);
4349 	else
4350 		memset(arvif, 0, sizeof(*arvif));
4351 }
4352 
4353 int
4354 ath12k_mac_op_change_vif_links(struct ieee80211_hw *hw,
4355 			       struct ieee80211_vif *vif,
4356 			       u16 old_links, u16 new_links,
4357 			       struct ieee80211_bss_conf *ol[IEEE80211_MLD_MAX_NUM_LINKS])
4358 {
4359 	struct ath12k_vif *ahvif = ath12k_vif_to_ahvif(vif);
4360 	unsigned long to_remove = old_links & ~new_links;
4361 	unsigned long to_add = ~old_links & new_links;
4362 	struct ath12k_hw *ah = ath12k_hw_to_ah(hw);
4363 	struct ath12k_link_vif *arvif;
4364 	u8 link_id;
4365 
4366 	lockdep_assert_wiphy(hw->wiphy);
4367 
4368 	ath12k_generic_dbg(ATH12K_DBG_MAC,
4369 			   "mac vif link changed for MLD %pM old_links 0x%x new_links 0x%x\n",
4370 			   vif->addr, old_links, new_links);
4371 
4372 	for_each_set_bit(link_id, &to_add, IEEE80211_MLD_MAX_NUM_LINKS) {
4373 		arvif = wiphy_dereference(hw->wiphy, ahvif->link[link_id]);
4374 		/* mac80211 wants to add link but driver already has the
4375 		 * link. This should not happen ideally.
4376 		 */
4377 		if (WARN_ON(arvif))
4378 			return -EINVAL;
4379 
4380 		arvif = ath12k_mac_assign_link_vif(ah, vif, link_id);
4381 		if (WARN_ON(!arvif))
4382 			return -EINVAL;
4383 	}
4384 
4385 	for_each_set_bit(link_id, &to_remove, IEEE80211_MLD_MAX_NUM_LINKS) {
4386 		arvif = wiphy_dereference(hw->wiphy, ahvif->link[link_id]);
4387 		if (WARN_ON(!arvif))
4388 			return -EINVAL;
4389 
4390 		if (!arvif->is_created) {
4391 			ath12k_mac_unassign_link_vif(arvif);
4392 			continue;
4393 		}
4394 
4395 		if (WARN_ON(!arvif->ar))
4396 			return -EINVAL;
4397 
4398 		ath12k_mac_remove_link_interface(hw, arvif);
4399 		ath12k_mac_unassign_link_vif(arvif);
4400 	}
4401 
4402 	return 0;
4403 }
4404 EXPORT_SYMBOL(ath12k_mac_op_change_vif_links);
4405 
4406 static int ath12k_mac_fils_discovery(struct ath12k_link_vif *arvif,
4407 				     struct ieee80211_bss_conf *info)
4408 {
4409 	struct ieee80211_vif *vif = ath12k_ahvif_to_vif(arvif->ahvif);
4410 	struct ath12k *ar = arvif->ar;
4411 	struct ieee80211_hw *hw = ath12k_ar_to_hw(ar);
4412 	struct sk_buff *tmpl;
4413 	int ret;
4414 	u32 interval;
4415 	bool unsol_bcast_probe_resp_enabled = false;
4416 
4417 	if (info->fils_discovery.max_interval) {
4418 		interval = info->fils_discovery.max_interval;
4419 
4420 		tmpl = ieee80211_get_fils_discovery_tmpl(hw, vif,
4421 							 info->link_id);
4422 		if (tmpl)
4423 			ret = ath12k_wmi_fils_discovery_tmpl(ar, arvif->vdev_id,
4424 							     tmpl);
4425 	} else if (info->unsol_bcast_probe_resp_interval) {
4426 		unsol_bcast_probe_resp_enabled = 1;
4427 		interval = info->unsol_bcast_probe_resp_interval;
4428 
4429 		tmpl = ieee80211_get_unsol_bcast_probe_resp_tmpl(hw, vif,
4430 								 info->link_id);
4431 		if (tmpl)
4432 			ret = ath12k_wmi_probe_resp_tmpl(ar, arvif->vdev_id,
4433 							 tmpl);
4434 	} else { /* Disable */
4435 		return ath12k_wmi_fils_discovery(ar, arvif->vdev_id, 0, false);
4436 	}
4437 
4438 	if (!tmpl) {
4439 		ath12k_warn(ar->ab,
4440 			    "mac vdev %i failed to retrieve %s template\n",
4441 			    arvif->vdev_id, (unsol_bcast_probe_resp_enabled ?
4442 			    "unsolicited broadcast probe response" :
4443 			    "FILS discovery"));
4444 		return -EPERM;
4445 	}
4446 	kfree_skb(tmpl);
4447 
4448 	if (!ret)
4449 		ret = ath12k_wmi_fils_discovery(ar, arvif->vdev_id, interval,
4450 						unsol_bcast_probe_resp_enabled);
4451 
4452 	return ret;
4453 }
4454 
4455 void ath12k_mac_op_vif_cfg_changed(struct ieee80211_hw *hw,
4456 				   struct ieee80211_vif *vif,
4457 				   u64 changed)
4458 {
4459 	struct ath12k_vif *ahvif = ath12k_vif_to_ahvif(vif);
4460 	unsigned long links = ahvif->links_map;
4461 	struct ieee80211_bss_conf *info;
4462 	struct ath12k_link_vif *arvif;
4463 	struct ieee80211_sta *sta;
4464 	struct ath12k_sta *ahsta;
4465 	struct ath12k *ar;
4466 	u8 link_id;
4467 
4468 	lockdep_assert_wiphy(hw->wiphy);
4469 
4470 	if (changed & BSS_CHANGED_SSID && vif->type == NL80211_IFTYPE_AP) {
4471 		ahvif->u.ap.ssid_len = vif->cfg.ssid_len;
4472 		if (vif->cfg.ssid_len)
4473 			memcpy(ahvif->u.ap.ssid, vif->cfg.ssid, vif->cfg.ssid_len);
4474 	}
4475 
4476 	if (changed & BSS_CHANGED_ASSOC) {
4477 		if (vif->cfg.assoc) {
4478 			/* only in station mode we can get here, so it's safe
4479 			 * to use ap_addr
4480 			 */
4481 			rcu_read_lock();
4482 			sta = ieee80211_find_sta(vif, vif->cfg.ap_addr);
4483 			if (!sta) {
4484 				rcu_read_unlock();
4485 				WARN_ONCE(1, "failed to find sta with addr %pM\n",
4486 					  vif->cfg.ap_addr);
4487 				return;
4488 			}
4489 
4490 			ahsta = ath12k_sta_to_ahsta(sta);
4491 			arvif = wiphy_dereference(hw->wiphy,
4492 						  ahvif->link[ahsta->assoc_link_id]);
4493 			rcu_read_unlock();
4494 
4495 			ar = arvif->ar;
4496 			/* there is no reason for which an assoc link's
4497 			 * bss info does not exist
4498 			 */
4499 			info = ath12k_mac_get_link_bss_conf(arvif);
4500 			ath12k_bss_assoc(ar, arvif, info);
4501 
4502 			/* exclude assoc link as it is done above */
4503 			links &= ~BIT(ahsta->assoc_link_id);
4504 		}
4505 
4506 		for_each_set_bit(link_id, &links, IEEE80211_MLD_MAX_NUM_LINKS) {
4507 			arvif = wiphy_dereference(hw->wiphy, ahvif->link[link_id]);
4508 			if (!arvif || !arvif->ar)
4509 				continue;
4510 
4511 			ar = arvif->ar;
4512 
4513 			if (vif->cfg.assoc) {
4514 				info = ath12k_mac_get_link_bss_conf(arvif);
4515 				if (!info)
4516 					continue;
4517 
4518 				ath12k_bss_assoc(ar, arvif, info);
4519 			} else {
4520 				ath12k_bss_disassoc(ar, arvif);
4521 			}
4522 		}
4523 	}
4524 }
4525 EXPORT_SYMBOL(ath12k_mac_op_vif_cfg_changed);
4526 
4527 static void ath12k_mac_vif_setup_ps(struct ath12k_link_vif *arvif)
4528 {
4529 	struct ath12k *ar = arvif->ar;
4530 	struct ieee80211_vif *vif = arvif->ahvif->vif;
4531 	struct ieee80211_conf *conf = &ath12k_ar_to_hw(ar)->conf;
4532 	enum wmi_sta_powersave_param param;
4533 	struct ieee80211_bss_conf *info;
4534 	enum wmi_sta_ps_mode psmode;
4535 	int ret;
4536 	int timeout;
4537 	bool enable_ps;
4538 
4539 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
4540 
4541 	if (vif->type != NL80211_IFTYPE_STATION)
4542 		return;
4543 
4544 	enable_ps = arvif->ahvif->ps;
4545 	if (enable_ps) {
4546 		psmode = WMI_STA_PS_MODE_ENABLED;
4547 		param = WMI_STA_PS_PARAM_INACTIVITY_TIME;
4548 
4549 		timeout = conf->dynamic_ps_timeout;
4550 		if (timeout == 0) {
4551 			info = ath12k_mac_get_link_bss_conf(arvif);
4552 			if (!info) {
4553 				ath12k_warn(ar->ab, "unable to access bss link conf in setup ps for vif %pM link %u\n",
4554 					    vif->addr, arvif->link_id);
4555 				return;
4556 			}
4557 
4558 			/* firmware doesn't like 0 */
4559 			timeout = ieee80211_tu_to_usec(info->beacon_int) / 1000;
4560 		}
4561 
4562 		ret = ath12k_wmi_set_sta_ps_param(ar, arvif->vdev_id, param,
4563 						  timeout);
4564 		if (ret) {
4565 			ath12k_warn(ar->ab, "failed to set inactivity time for vdev %d: %i\n",
4566 				    arvif->vdev_id, ret);
4567 			return;
4568 		}
4569 	} else {
4570 		psmode = WMI_STA_PS_MODE_DISABLED;
4571 	}
4572 
4573 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac vdev %d psmode %s\n",
4574 		   arvif->vdev_id, psmode ? "enable" : "disable");
4575 
4576 	ret = ath12k_wmi_pdev_set_ps_mode(ar, arvif->vdev_id, psmode);
4577 	if (ret)
4578 		ath12k_warn(ar->ab, "failed to set sta power save mode %d for vdev %d: %d\n",
4579 			    psmode, arvif->vdev_id, ret);
4580 }
4581 
4582 static bool ath12k_mac_supports_tpc(struct ath12k *ar, struct ath12k_vif *ahvif,
4583 				    const struct cfg80211_chan_def *chandef)
4584 {
4585 	return ath12k_wmi_supports_6ghz_cc_ext(ar) &&
4586 		test_bit(WMI_TLV_SERVICE_EXT_TPC_REG_SUPPORT, ar->ab->wmi_ab.svc_map) &&
4587 		(ahvif->vdev_type == WMI_VDEV_TYPE_STA  ||
4588 		 ahvif->vdev_type == WMI_VDEV_TYPE_AP) &&
4589 		ahvif->vdev_subtype == WMI_VDEV_SUBTYPE_NONE &&
4590 		chandef->chan &&
4591 		chandef->chan->band == NL80211_BAND_6GHZ;
4592 }
4593 
4594 static void ath12k_wmi_vdev_params_up(struct ath12k *ar,
4595 				      struct ath12k_link_vif *arvif,
4596 				      struct ath12k_link_vif *tx_arvif,
4597 				      struct ieee80211_bss_conf *info, u16 aid)
4598 {
4599 	struct ath12k_wmi_vdev_up_params params = {
4600 		.vdev_id = arvif->vdev_id,
4601 		.aid = aid,
4602 		.bssid = arvif->bssid
4603 	};
4604 	int ret;
4605 
4606 	if (tx_arvif) {
4607 		params.tx_bssid = tx_arvif->bssid;
4608 		params.nontx_profile_idx = info->bssid_index;
4609 		params.nontx_profile_cnt = 1 << info->bssid_indicator;
4610 	}
4611 
4612 	ret = ath12k_wmi_vdev_up(arvif->ar, &params);
4613 	if (ret)
4614 		ath12k_warn(ar->ab, "failed to bring vdev up %d: %d\n",
4615 			    arvif->vdev_id, ret);
4616 }
4617 
4618 static int ath12k_mac_config_obss_pd(struct ath12k_link_vif *arvif,
4619 				     const struct ieee80211_he_obss_pd *he_obss_pd)
4620 {
4621 	struct ath12k_wmi_obss_pd_arg obss_pd_arg = {};
4622 	u32 srg_bitmap[2], non_srg_bitmap[2];
4623 	struct ath12k *ar = arvif->ar;
4624 	u32 param_id, pdev_id;
4625 	u32 param_val;
4626 	int ret;
4627 
4628 	if (ar->ab->hw_params->single_pdev_only)
4629 		pdev_id = ath12k_mac_get_target_pdev_id_from_vif(arvif);
4630 	else
4631 		pdev_id = ar->pdev->pdev_id;
4632 
4633 	/* Set and enable SRG/non-SRG OBSS PD threshold */
4634 	param_id = WMI_PDEV_PARAM_SET_CMD_OBSS_PD_THRESHOLD;
4635 	if (ar->monitor_started || !he_obss_pd->enable) {
4636 		ret = ath12k_wmi_pdev_set_param(ar, param_id, 0, pdev_id);
4637 		if (ret)
4638 			ath12k_warn(ar->ab,
4639 				    "failed to set OBSS PD threshold for pdev %u: %d\n",
4640 				    pdev_id, ret);
4641 		return ret;
4642 	}
4643 
4644 	/*
4645 	 * This service flag indicates firmware support for SRG/SRP-based
4646 	 * spatial reuse. It also specifies whether OBSS PD threshold values
4647 	 * should be interpreted as dB (offset) or dBm (absolute) units.
4648 	 */
4649 	obss_pd_arg.srp_support = test_bit(WMI_TLV_SERVICE_SRG_SRP_SPATIAL_REUSE_SUPPORT,
4650 					   ar->ab->wmi_ab.svc_map);
4651 
4652 	if (!(he_obss_pd->sr_ctrl &
4653 	      IEEE80211_HE_SPR_NON_SRG_OBSS_PD_SR_DISALLOWED)) {
4654 		if (he_obss_pd->sr_ctrl & IEEE80211_HE_SPR_NON_SRG_OFFSET_PRESENT)
4655 			obss_pd_arg.non_srg_th = ATH12K_OBSS_PD_MAX_THRESHOLD +
4656 						 he_obss_pd->non_srg_max_offset;
4657 		else
4658 			obss_pd_arg.non_srg_th = ATH12K_OBSS_PD_NON_SRG_MAX_THRESHOLD;
4659 
4660 		if (!obss_pd_arg.srp_support)
4661 			obss_pd_arg.non_srg_th -= ATH12K_DEFAULT_NOISE_FLOOR;
4662 
4663 		obss_pd_arg.non_srg_enabled = true;
4664 	}
4665 
4666 	if (he_obss_pd->sr_ctrl & IEEE80211_HE_SPR_SRG_INFORMATION_PRESENT) {
4667 		obss_pd_arg.srg_th = ATH12K_OBSS_PD_MAX_THRESHOLD +
4668 				     he_obss_pd->max_offset;
4669 		obss_pd_arg.srg_enabled = true;
4670 	}
4671 
4672 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
4673 		   "pdev %u OBSS PD sr_ctrl 0x%x srg_th %d dBm non_srg_th %d dBm\n",
4674 		   pdev_id, he_obss_pd->sr_ctrl,
4675 		   obss_pd_arg.srg_th, obss_pd_arg.non_srg_th);
4676 
4677 	param_val = ath12k_wmi_build_obss_pd(&obss_pd_arg);
4678 	ret = ath12k_wmi_pdev_set_param(ar, param_id, param_val, pdev_id);
4679 	if (ret) {
4680 		ath12k_warn(ar->ab,
4681 			    "failed to set OBSS PD threshold for pdev %u: %d\n",
4682 			    pdev_id, ret);
4683 		return ret;
4684 	}
4685 
4686 	/* Enable OBSS PD for all access category */
4687 	param_id  = WMI_PDEV_PARAM_SET_CMD_OBSS_PD_PER_AC;
4688 	param_val = 0xf;
4689 	ret = ath12k_wmi_pdev_set_param(ar, param_id, param_val, pdev_id);
4690 	if (ret) {
4691 		ath12k_warn(ar->ab,
4692 			    "failed to set OBSS PD per ac for pdev %u: %d\n",
4693 			    pdev_id, ret);
4694 		return ret;
4695 	}
4696 
4697 	/* Set SR prohibit */
4698 	param_id  = WMI_PDEV_PARAM_ENABLE_SR_PROHIBIT;
4699 	param_val = !!(he_obss_pd->sr_ctrl &
4700 		       IEEE80211_HE_SPR_HESIGA_SR_VAL15_ALLOWED);
4701 	ret = ath12k_wmi_pdev_set_param(ar, param_id, param_val, pdev_id);
4702 	if (ret) {
4703 		ath12k_warn(ar->ab, "failed to set SR prohibit for pdev %u: %d\n",
4704 			    pdev_id, ret);
4705 		return ret;
4706 	}
4707 
4708 	if (!obss_pd_arg.srp_support)
4709 		return 0;
4710 
4711 	memcpy(srg_bitmap, he_obss_pd->bss_color_bitmap, sizeof(srg_bitmap));
4712 	/* Set SRG BSS color bitmap */
4713 	ret = ath12k_wmi_pdev_set_srg_bss_color_bitmap(ar, pdev_id, srg_bitmap);
4714 	if (ret) {
4715 		ath12k_warn(ar->ab,
4716 			    "failed to set SRG bss color bitmap for pdev %u: %d\n",
4717 			    pdev_id, ret);
4718 		return ret;
4719 	}
4720 
4721 	/* Enable BSS colors for SRG */
4722 	ret = ath12k_wmi_pdev_srg_obss_color_enable_bitmap(ar, pdev_id, srg_bitmap);
4723 	if (ret) {
4724 		ath12k_warn(ar->ab,
4725 			    "failed to enable SRG bss color bitmap pdev %u: %d\n",
4726 			    pdev_id, ret);
4727 		return ret;
4728 	}
4729 
4730 	memcpy(srg_bitmap, he_obss_pd->partial_bssid_bitmap, sizeof(srg_bitmap));
4731 	/* Set SRG partial bssid bitmap */
4732 	ret = ath12k_wmi_pdev_set_srg_partial_bssid_bitmap(ar, pdev_id, srg_bitmap);
4733 	if (ret) {
4734 		ath12k_warn(ar->ab,
4735 			    "failed to set SRG partial bssid bitmap for pdev %u: %d\n",
4736 			    pdev_id, ret);
4737 		return ret;
4738 	}
4739 
4740 	/* Enable partial bssid mask for SRG */
4741 	ret = ath12k_wmi_pdev_srg_obss_bssid_enable_bitmap(ar, pdev_id, srg_bitmap);
4742 	if (ret) {
4743 		ath12k_warn(ar->ab,
4744 			    "failed to enable SRG bssid bitmap pdev %u: %d\n",
4745 			    pdev_id, ret);
4746 		return ret;
4747 	}
4748 
4749 	/*
4750 	 * No explicit non-SRG bitmap from mac80211; enable all colors/bssids
4751 	 * as non-SRG candidates. Actual SRG members are filtered by SRG bitmaps.
4752 	 */
4753 	memset(non_srg_bitmap, 0xff, sizeof(non_srg_bitmap));
4754 
4755 	/* Enable BSS colors for non-SRG */
4756 	ret = ath12k_wmi_pdev_non_srg_obss_color_enable_bitmap(ar, pdev_id,
4757 							       non_srg_bitmap);
4758 	if (ret) {
4759 		ath12k_warn(ar->ab,
4760 			    "failed to enable non SRG color bitmap pdev %u: %d\n",
4761 			    pdev_id, ret);
4762 		return ret;
4763 	}
4764 
4765 	/* Enable partial bssid mask for non-SRG */
4766 	ret = ath12k_wmi_pdev_non_srg_obss_bssid_enable_bitmap(ar, pdev_id,
4767 							       non_srg_bitmap);
4768 	if (ret) {
4769 		ath12k_warn(ar->ab,
4770 			    "failed to enable non SRG bssid bitmap pdev %u: %d\n",
4771 			    pdev_id, ret);
4772 		return ret;
4773 	}
4774 
4775 	return 0;
4776 }
4777 
4778 static void ath12k_mac_bss_info_changed(struct ath12k *ar,
4779 					struct ath12k_link_vif *arvif,
4780 					struct ieee80211_bss_conf *info,
4781 					u64 changed)
4782 {
4783 	struct ath12k_vif *ahvif = arvif->ahvif;
4784 	struct ieee80211_vif *vif = ath12k_ahvif_to_vif(ahvif);
4785 	struct ieee80211_vif_cfg *vif_cfg = &vif->cfg;
4786 	struct ath12k_link_vif *tx_arvif;
4787 	struct cfg80211_chan_def def;
4788 	u32 param_id, param_value;
4789 	enum nl80211_band band;
4790 	u32 vdev_param;
4791 	int mcast_rate;
4792 	u32 preamble;
4793 	u16 hw_value;
4794 	u16 bitrate;
4795 	u8 rateidx;
4796 	u32 rate;
4797 	int ret;
4798 
4799 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
4800 
4801 	if (changed & BSS_CHANGED_BEACON_INT) {
4802 		arvif->beacon_interval = info->beacon_int;
4803 
4804 		param_id = WMI_VDEV_PARAM_BEACON_INTERVAL;
4805 		ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
4806 						    param_id,
4807 						    arvif->beacon_interval);
4808 		if (ret)
4809 			ath12k_warn(ar->ab, "Failed to set beacon interval for VDEV: %d\n",
4810 				    arvif->vdev_id);
4811 		else
4812 			ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
4813 				   "Beacon interval: %d set for VDEV: %d\n",
4814 				   arvif->beacon_interval, arvif->vdev_id);
4815 	}
4816 
4817 	if (changed & BSS_CHANGED_BEACON) {
4818 		param_id = WMI_PDEV_PARAM_BEACON_TX_MODE;
4819 		param_value = WMI_BEACON_BURST_MODE;
4820 		ret = ath12k_wmi_pdev_set_param(ar, param_id,
4821 						param_value, ar->pdev->pdev_id);
4822 		if (ret)
4823 			ath12k_warn(ar->ab, "Failed to set beacon mode for VDEV: %d\n",
4824 				    arvif->vdev_id);
4825 		else
4826 			ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
4827 				   "Set burst beacon mode for VDEV: %d\n",
4828 				   arvif->vdev_id);
4829 
4830 		/* In MBSSID case, need to install transmitting VIF's template first */
4831 
4832 		ret = ath12k_mac_setup_bcn_tmpl(arvif);
4833 		if (ret)
4834 			ath12k_warn(ar->ab, "failed to update bcn template: %d\n",
4835 				    ret);
4836 
4837 		if (!arvif->is_csa_in_progress)
4838 			goto skip_vdev_up;
4839 
4840 		tx_arvif = ath12k_mac_get_tx_arvif(arvif, info);
4841 		if (tx_arvif && arvif != tx_arvif && tx_arvif->is_csa_in_progress)
4842 			/* skip non tx vif's */
4843 			goto skip_vdev_up;
4844 
4845 		ath12k_wmi_vdev_params_up(ar, arvif, tx_arvif, info, ahvif->aid);
4846 
4847 		arvif->is_csa_in_progress = false;
4848 
4849 		if (tx_arvif && arvif == tx_arvif) {
4850 			struct ath12k_link_vif *arvif_itr;
4851 
4852 			list_for_each_entry(arvif_itr, &ar->arvifs, list) {
4853 				if (!arvif_itr->is_csa_in_progress)
4854 					continue;
4855 
4856 				ath12k_wmi_vdev_params_up(ar, arvif, tx_arvif,
4857 							  info, ahvif->aid);
4858 				arvif_itr->is_csa_in_progress = false;
4859 			}
4860 		}
4861 	}
4862 
4863 skip_vdev_up:
4864 
4865 	if (changed & (BSS_CHANGED_BEACON_INFO | BSS_CHANGED_BEACON)) {
4866 		arvif->dtim_period = info->dtim_period;
4867 
4868 		param_id = WMI_VDEV_PARAM_DTIM_PERIOD;
4869 		ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
4870 						    param_id,
4871 						    arvif->dtim_period);
4872 
4873 		if (ret)
4874 			ath12k_warn(ar->ab, "Failed to set dtim period for VDEV %d: %i\n",
4875 				    arvif->vdev_id, ret);
4876 		else
4877 			ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
4878 				   "DTIM period: %d set for VDEV: %d\n",
4879 				   arvif->dtim_period, arvif->vdev_id);
4880 	}
4881 
4882 	if (changed & BSS_CHANGED_SSID &&
4883 	    vif->type == NL80211_IFTYPE_AP) {
4884 		ahvif->u.ap.ssid_len = vif->cfg.ssid_len;
4885 		if (vif->cfg.ssid_len)
4886 			memcpy(ahvif->u.ap.ssid, vif->cfg.ssid, vif->cfg.ssid_len);
4887 		ahvif->u.ap.hidden_ssid = info->hidden_ssid;
4888 	}
4889 
4890 	if (changed & BSS_CHANGED_BSSID && !is_zero_ether_addr(info->bssid))
4891 		ether_addr_copy(arvif->bssid, info->bssid);
4892 
4893 	if (changed & BSS_CHANGED_BEACON_ENABLED) {
4894 		if (info->enable_beacon) {
4895 			ret = ath12k_mac_set_he_txbf_conf(arvif);
4896 			if (ret)
4897 				ath12k_warn(ar->ab,
4898 					    "failed to set HE TXBF config for vdev: %d\n",
4899 					    arvif->vdev_id);
4900 
4901 			ret = ath12k_mac_set_eht_txbf_conf(arvif);
4902 			if (ret)
4903 				ath12k_warn(ar->ab,
4904 					    "failed to set EHT TXBF config for vdev: %d\n",
4905 					    arvif->vdev_id);
4906 		}
4907 		ath12k_control_beaconing(arvif, info);
4908 
4909 		if (arvif->is_up && info->he_support &&
4910 		    info->he_oper.params) {
4911 			/* TODO: Extend to support 1024 BA Bitmap size */
4912 			ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
4913 							    WMI_VDEV_PARAM_BA_MODE,
4914 							    WMI_BA_MODE_BUFFER_SIZE_256);
4915 			if (ret)
4916 				ath12k_warn(ar->ab,
4917 					    "failed to set BA BUFFER SIZE 256 for vdev: %d\n",
4918 					    arvif->vdev_id);
4919 
4920 			param_id = WMI_VDEV_PARAM_HEOPS_0_31;
4921 			param_value = info->he_oper.params;
4922 			ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
4923 							    param_id, param_value);
4924 			ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
4925 				   "he oper param: %x set for VDEV: %d\n",
4926 				   param_value, arvif->vdev_id);
4927 
4928 			if (ret)
4929 				ath12k_warn(ar->ab, "Failed to set he oper params %x for VDEV %d: %i\n",
4930 					    param_value, arvif->vdev_id, ret);
4931 		}
4932 	}
4933 
4934 	if (changed & BSS_CHANGED_ERP_CTS_PROT) {
4935 		u32 cts_prot;
4936 
4937 		cts_prot = !!(info->use_cts_prot);
4938 		param_id = WMI_VDEV_PARAM_PROTECTION_MODE;
4939 
4940 		if (arvif->is_started) {
4941 			ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
4942 							    param_id, cts_prot);
4943 			if (ret)
4944 				ath12k_warn(ar->ab, "Failed to set CTS prot for VDEV: %d\n",
4945 					    arvif->vdev_id);
4946 			else
4947 				ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "Set CTS prot: %d for VDEV: %d\n",
4948 					   cts_prot, arvif->vdev_id);
4949 		} else {
4950 			ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "defer protection mode setup, vdev is not ready yet\n");
4951 		}
4952 	}
4953 
4954 	if (changed & BSS_CHANGED_ERP_SLOT) {
4955 		u32 slottime;
4956 
4957 		if (info->use_short_slot)
4958 			slottime = WMI_VDEV_SLOT_TIME_SHORT; /* 9us */
4959 
4960 		else
4961 			slottime = WMI_VDEV_SLOT_TIME_LONG; /* 20us */
4962 
4963 		param_id = WMI_VDEV_PARAM_SLOT_TIME;
4964 		ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
4965 						    param_id, slottime);
4966 		if (ret)
4967 			ath12k_warn(ar->ab, "Failed to set erp slot for VDEV: %d\n",
4968 				    arvif->vdev_id);
4969 		else
4970 			ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
4971 				   "Set slottime: %d for VDEV: %d\n",
4972 				   slottime, arvif->vdev_id);
4973 	}
4974 
4975 	if (changed & BSS_CHANGED_ERP_PREAMBLE) {
4976 		u32 preamble;
4977 
4978 		if (info->use_short_preamble)
4979 			preamble = WMI_VDEV_PREAMBLE_SHORT;
4980 		else
4981 			preamble = WMI_VDEV_PREAMBLE_LONG;
4982 
4983 		param_id = WMI_VDEV_PARAM_PREAMBLE;
4984 		ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
4985 						    param_id, preamble);
4986 		if (ret)
4987 			ath12k_warn(ar->ab, "Failed to set preamble for VDEV: %d\n",
4988 				    arvif->vdev_id);
4989 		else
4990 			ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
4991 				   "Set preamble: %d for VDEV: %d\n",
4992 				   preamble, arvif->vdev_id);
4993 	}
4994 
4995 	if (changed & BSS_CHANGED_ASSOC) {
4996 		if (vif->cfg.assoc)
4997 			ath12k_bss_assoc(ar, arvif, info);
4998 		else
4999 			ath12k_bss_disassoc(ar, arvif);
5000 	}
5001 
5002 	if (changed & BSS_CHANGED_TXPOWER) {
5003 		ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac vdev_id %i txpower %d\n",
5004 			   arvif->vdev_id, info->txpower);
5005 
5006 		arvif->txpower = info->txpower;
5007 		ath12k_mac_txpower_recalc(ar);
5008 	}
5009 
5010 	if (changed & BSS_CHANGED_MCAST_RATE &&
5011 	    !ath12k_mac_vif_link_chan(vif, arvif->link_id, &def)) {
5012 		band = def.chan->band;
5013 		mcast_rate = info->mcast_rate[band];
5014 
5015 		if (mcast_rate > 0) {
5016 			rateidx = mcast_rate - 1;
5017 		} else {
5018 			if (info->basic_rates)
5019 				rateidx = __ffs(info->basic_rates);
5020 			else
5021 				rateidx = 0;
5022 		}
5023 
5024 		if (ar->pdev->cap.supported_bands & WMI_HOST_WLAN_5GHZ_CAP)
5025 			rateidx += ATH12K_MAC_FIRST_OFDM_RATE_IDX;
5026 
5027 		bitrate = ath12k_legacy_rates[rateidx].bitrate;
5028 		hw_value = ath12k_legacy_rates[rateidx].hw_value;
5029 
5030 		if (ath12k_mac_bitrate_is_cck(bitrate))
5031 			preamble = WMI_RATE_PREAMBLE_CCK;
5032 		else
5033 			preamble = WMI_RATE_PREAMBLE_OFDM;
5034 
5035 		rate = ATH12K_HW_RATE_CODE(hw_value, 0, preamble);
5036 
5037 		ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
5038 			   "mac vdev %d mcast_rate %x\n",
5039 			   arvif->vdev_id, rate);
5040 
5041 		vdev_param = WMI_VDEV_PARAM_MCAST_DATA_RATE;
5042 		ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
5043 						    vdev_param, rate);
5044 		if (ret)
5045 			ath12k_warn(ar->ab,
5046 				    "failed to set mcast rate on vdev %i: %d\n",
5047 				    arvif->vdev_id,  ret);
5048 
5049 		vdev_param = WMI_VDEV_PARAM_BCAST_DATA_RATE;
5050 		ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
5051 						    vdev_param, rate);
5052 		if (ret)
5053 			ath12k_warn(ar->ab,
5054 				    "failed to set bcast rate on vdev %i: %d\n",
5055 				    arvif->vdev_id,  ret);
5056 	}
5057 
5058 	if (changed & BSS_CHANGED_BASIC_RATES &&
5059 	    !ath12k_mac_vif_link_chan(vif, arvif->link_id, &def))
5060 		ath12k_recalculate_mgmt_rate(ar, arvif, &def);
5061 
5062 	if (changed & BSS_CHANGED_TWT) {
5063 		if (info->twt_requester || info->twt_responder)
5064 			ath12k_wmi_send_twt_enable_cmd(ar, ar->pdev->pdev_id);
5065 		else
5066 			ath12k_wmi_send_twt_disable_cmd(ar, ar->pdev->pdev_id);
5067 	}
5068 
5069 	if (changed & BSS_CHANGED_HE_OBSS_PD) {
5070 		if (vif->type == NL80211_IFTYPE_AP)
5071 			ath12k_mac_config_obss_pd(arvif, &info->he_obss_pd);
5072 		else
5073 			ath12k_wmi_send_obss_spr_cmd(ar, arvif->vdev_id,
5074 						     &info->he_obss_pd);
5075 	}
5076 
5077 	if (changed & BSS_CHANGED_HE_BSS_COLOR) {
5078 		if (vif->type == NL80211_IFTYPE_AP) {
5079 			ret = ath12k_wmi_obss_color_cfg_cmd(ar,
5080 							    arvif->vdev_id,
5081 							    info->he_bss_color.color,
5082 							    ATH12K_BSS_COLOR_AP_PERIODS,
5083 							    info->he_bss_color.enabled);
5084 			if (ret)
5085 				ath12k_warn(ar->ab, "failed to set bss color collision on vdev %u: %d\n",
5086 					    arvif->vdev_id,  ret);
5087 
5088 			param_id = WMI_VDEV_PARAM_BSS_COLOR;
5089 			if (info->he_bss_color.enabled)
5090 				param_value = info->he_bss_color.color <<
5091 					      IEEE80211_HE_OPERATION_BSS_COLOR_OFFSET;
5092 			else
5093 				param_value = IEEE80211_HE_OPERATION_BSS_COLOR_DISABLED;
5094 
5095 			ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
5096 							    param_id,
5097 							    param_value);
5098 			if (ret)
5099 				ath12k_warn(ar->ab, "failed to set bss color param on vdev %u: %d\n",
5100 					    arvif->vdev_id,  ret);
5101 			else
5102 				ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "bss color param 0x%x set on vdev %u\n",
5103 					   param_value, arvif->vdev_id);
5104 		} else if (vif->type == NL80211_IFTYPE_STATION) {
5105 			ret = ath12k_wmi_send_bss_color_change_enable_cmd(ar,
5106 									  arvif->vdev_id,
5107 									  1);
5108 			if (ret)
5109 				ath12k_warn(ar->ab, "failed to enable bss color change on vdev %i: %d\n",
5110 					    arvif->vdev_id,  ret);
5111 			ret = ath12k_wmi_obss_color_cfg_cmd(ar,
5112 							    arvif->vdev_id,
5113 							    0,
5114 							    ATH12K_BSS_COLOR_STA_PERIODS,
5115 							    1);
5116 			if (ret)
5117 				ath12k_warn(ar->ab, "failed to set bss color collision on vdev %i: %d\n",
5118 					    arvif->vdev_id,  ret);
5119 		}
5120 	}
5121 
5122 	ath12k_mac_fils_discovery(arvif, info);
5123 
5124 	if (changed & BSS_CHANGED_PS &&
5125 	    ar->ab->hw_params->supports_sta_ps) {
5126 		ahvif->ps = vif_cfg->ps;
5127 		ath12k_mac_vif_setup_ps(arvif);
5128 	}
5129 }
5130 
5131 static struct ath12k_vif_cache *ath12k_ahvif_get_link_cache(struct ath12k_vif *ahvif,
5132 							    u8 link_id)
5133 {
5134 	if (!ahvif->cache[link_id]) {
5135 		ahvif->cache[link_id] = kzalloc_obj(*ahvif->cache[0]);
5136 		if (ahvif->cache[link_id])
5137 			INIT_LIST_HEAD(&ahvif->cache[link_id]->key_conf.list);
5138 	}
5139 
5140 	return ahvif->cache[link_id];
5141 }
5142 
5143 static void ath12k_ahvif_put_link_key_cache(struct ath12k_vif_cache *cache)
5144 {
5145 	struct ath12k_key_conf *key_conf, *tmp;
5146 
5147 	if (!cache || list_empty(&cache->key_conf.list))
5148 		return;
5149 	list_for_each_entry_safe(key_conf, tmp, &cache->key_conf.list, list) {
5150 		list_del(&key_conf->list);
5151 		kfree(key_conf);
5152 	}
5153 }
5154 
5155 static void ath12k_ahvif_put_link_cache(struct ath12k_vif *ahvif, u8 link_id)
5156 {
5157 	if (link_id >= IEEE80211_MLD_MAX_NUM_LINKS)
5158 		return;
5159 
5160 	ath12k_ahvif_put_link_key_cache(ahvif->cache[link_id]);
5161 	kfree(ahvif->cache[link_id]);
5162 	ahvif->cache[link_id] = NULL;
5163 }
5164 
5165 void ath12k_mac_op_link_info_changed(struct ieee80211_hw *hw,
5166 				     struct ieee80211_vif *vif,
5167 				     struct ieee80211_bss_conf *info,
5168 				     u64 changed)
5169 {
5170 	struct ath12k *ar;
5171 	struct ath12k_vif *ahvif = ath12k_vif_to_ahvif(vif);
5172 	struct ath12k_vif_cache *cache;
5173 	struct ath12k_link_vif *arvif;
5174 	u8 link_id = info->link_id;
5175 
5176 	lockdep_assert_wiphy(hw->wiphy);
5177 
5178 	arvif = wiphy_dereference(hw->wiphy, ahvif->link[link_id]);
5179 
5180 	/* if the vdev is not created on a certain radio,
5181 	 * cache the info to be updated later on vdev creation
5182 	 */
5183 
5184 	if (!arvif || !arvif->is_created) {
5185 		cache = ath12k_ahvif_get_link_cache(ahvif, link_id);
5186 		if (!cache)
5187 			return;
5188 
5189 		cache->bss_conf_changed |= changed;
5190 
5191 		return;
5192 	}
5193 
5194 	ar = arvif->ar;
5195 
5196 	ath12k_mac_bss_info_changed(ar, arvif, info, changed);
5197 }
5198 EXPORT_SYMBOL(ath12k_mac_op_link_info_changed);
5199 
5200 static struct ath12k*
5201 ath12k_mac_select_scan_device(struct ieee80211_hw *hw,
5202 			      struct ieee80211_vif *vif,
5203 			      u32 center_freq)
5204 {
5205 	struct ath12k_hw *ah = hw->priv;
5206 	enum nl80211_band band;
5207 	struct ath12k *ar;
5208 	int i;
5209 
5210 	if (ah->num_radio == 1)
5211 		return ah->radio;
5212 
5213 	/* Currently mac80211 supports splitting scan requests into
5214 	 * multiple scan requests per band.
5215 	 * Loop through first channel and determine the scan radio
5216 	 * TODO: There could be 5 GHz low/high channels in that case
5217 	 * split the hw request and perform multiple scans
5218 	 */
5219 
5220 	if (center_freq < ATH12K_MIN_5GHZ_FREQ)
5221 		band = NL80211_BAND_2GHZ;
5222 	else if (center_freq < ATH12K_MIN_6GHZ_FREQ)
5223 		band = NL80211_BAND_5GHZ;
5224 	else
5225 		band = NL80211_BAND_6GHZ;
5226 
5227 	for_each_ar(ah, ar, i) {
5228 		if (ar->mac.sbands[band].channels &&
5229 		    center_freq >= KHZ_TO_MHZ(ar->freq_range.start_freq) &&
5230 		    center_freq <= KHZ_TO_MHZ(ar->freq_range.end_freq))
5231 			return ar;
5232 	}
5233 
5234 	return NULL;
5235 }
5236 
5237 void __ath12k_mac_scan_finish(struct ath12k *ar)
5238 {
5239 	struct ieee80211_hw *hw = ath12k_ar_to_hw(ar);
5240 
5241 	lockdep_assert_held(&ar->data_lock);
5242 
5243 	switch (ar->scan.state) {
5244 	case ATH12K_SCAN_IDLE:
5245 		break;
5246 	case ATH12K_SCAN_RUNNING:
5247 	case ATH12K_SCAN_ABORTING:
5248 		if (ar->scan.is_roc && ar->scan.roc_notify)
5249 			ieee80211_remain_on_channel_expired(hw);
5250 		fallthrough;
5251 	case ATH12K_SCAN_STARTING:
5252 		cancel_delayed_work(&ar->scan.timeout);
5253 		complete_all(&ar->scan.completed);
5254 		wiphy_work_queue(ar->ah->hw->wiphy, &ar->scan.vdev_clean_wk);
5255 		break;
5256 	}
5257 }
5258 
5259 void ath12k_mac_scan_finish(struct ath12k *ar)
5260 {
5261 	spin_lock_bh(&ar->data_lock);
5262 	__ath12k_mac_scan_finish(ar);
5263 	spin_unlock_bh(&ar->data_lock);
5264 }
5265 
5266 static int ath12k_scan_stop(struct ath12k *ar)
5267 {
5268 	struct ath12k_wmi_scan_cancel_arg arg = {
5269 		.req_type = WLAN_SCAN_CANCEL_SINGLE,
5270 		.scan_id = ATH12K_SCAN_ID,
5271 	};
5272 	int ret;
5273 
5274 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
5275 
5276 	/* TODO: Fill other STOP Params */
5277 	arg.pdev_id = ar->pdev->pdev_id;
5278 
5279 	ret = ath12k_wmi_send_scan_stop_cmd(ar, &arg);
5280 	if (ret) {
5281 		ath12k_warn(ar->ab, "failed to stop wmi scan: %d\n", ret);
5282 		goto out;
5283 	}
5284 
5285 	ret = wait_for_completion_timeout(&ar->scan.completed, 3 * HZ);
5286 	if (ret == 0) {
5287 		ath12k_warn(ar->ab,
5288 			    "failed to receive scan abort comple: timed out\n");
5289 		ret = -ETIMEDOUT;
5290 	} else if (ret > 0) {
5291 		ret = 0;
5292 	}
5293 
5294 out:
5295 	/* Scan state should be updated in scan completion worker but in
5296 	 * case firmware fails to deliver the event (for whatever reason)
5297 	 * it is desired to clean up scan state anyway. Firmware may have
5298 	 * just dropped the scan completion event delivery due to transport
5299 	 * pipe being overflown with data and/or it can recover on its own
5300 	 * before next scan request is submitted.
5301 	 */
5302 	spin_lock_bh(&ar->data_lock);
5303 	if (ret)
5304 		__ath12k_mac_scan_finish(ar);
5305 	spin_unlock_bh(&ar->data_lock);
5306 
5307 	return ret;
5308 }
5309 
5310 static void ath12k_scan_abort(struct ath12k *ar)
5311 {
5312 	int ret;
5313 
5314 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
5315 
5316 	spin_lock_bh(&ar->data_lock);
5317 
5318 	switch (ar->scan.state) {
5319 	case ATH12K_SCAN_IDLE:
5320 		/* This can happen if timeout worker kicked in and called
5321 		 * abortion while scan completion was being processed.
5322 		 */
5323 		break;
5324 	case ATH12K_SCAN_STARTING:
5325 	case ATH12K_SCAN_ABORTING:
5326 		ath12k_warn(ar->ab, "refusing scan abortion due to invalid scan state: %d\n",
5327 			    ar->scan.state);
5328 		break;
5329 	case ATH12K_SCAN_RUNNING:
5330 		ar->scan.state = ATH12K_SCAN_ABORTING;
5331 		spin_unlock_bh(&ar->data_lock);
5332 
5333 		ret = ath12k_scan_stop(ar);
5334 		if (ret)
5335 			ath12k_warn(ar->ab, "failed to abort scan: %d\n", ret);
5336 
5337 		spin_lock_bh(&ar->data_lock);
5338 		break;
5339 	}
5340 
5341 	spin_unlock_bh(&ar->data_lock);
5342 }
5343 
5344 static void ath12k_scan_timeout_work(struct work_struct *work)
5345 {
5346 	struct ath12k *ar = container_of(work, struct ath12k,
5347 					 scan.timeout.work);
5348 
5349 	wiphy_lock(ath12k_ar_to_hw(ar)->wiphy);
5350 	ath12k_scan_abort(ar);
5351 	wiphy_unlock(ath12k_ar_to_hw(ar)->wiphy);
5352 }
5353 
5354 static void ath12k_mac_scan_send_complete(struct ath12k *ar,
5355 					  struct cfg80211_scan_info *info)
5356 {
5357 	struct ath12k_hw *ah = ar->ah;
5358 	struct ath12k *partner_ar;
5359 	int i;
5360 
5361 	lockdep_assert_wiphy(ah->hw->wiphy);
5362 
5363 	for_each_ar(ah, partner_ar, i)
5364 		if (partner_ar != ar &&
5365 		    partner_ar->scan.state == ATH12K_SCAN_RUNNING)
5366 			return;
5367 
5368 	ieee80211_scan_completed(ah->hw, info);
5369 }
5370 
5371 static void ath12k_scan_vdev_clean_work(struct wiphy *wiphy, struct wiphy_work *work)
5372 {
5373 	struct ath12k *ar = container_of(work, struct ath12k,
5374 					 scan.vdev_clean_wk);
5375 	struct ath12k_hw *ah = ar->ah;
5376 	struct ath12k_link_vif *arvif;
5377 
5378 	lockdep_assert_wiphy(wiphy);
5379 
5380 	arvif = ar->scan.arvif;
5381 
5382 	/* The scan vdev has already been deleted. This can occur when a
5383 	 * new scan request is made on the same vif with a different
5384 	 * frequency, causing the scan arvif to move from one radio to
5385 	 * another. Or, scan was abrupted and via remove interface, the
5386 	 * arvif is already deleted. Alternatively, if the scan vdev is not
5387 	 * being used as an actual vdev, then do not delete it.
5388 	 */
5389 	if (!arvif || arvif->is_started)
5390 		goto work_complete;
5391 
5392 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac clean scan vdev (link id %u)",
5393 		   arvif->link_id);
5394 
5395 	ath12k_mac_remove_link_interface(ah->hw, arvif);
5396 	ath12k_mac_unassign_link_vif(arvif);
5397 
5398 work_complete:
5399 	spin_lock_bh(&ar->data_lock);
5400 	ar->scan.arvif = NULL;
5401 	if (!ar->scan.is_roc) {
5402 		struct cfg80211_scan_info info = {
5403 			.aborted = ((ar->scan.state ==
5404 				    ATH12K_SCAN_ABORTING) ||
5405 				    (ar->scan.state ==
5406 				    ATH12K_SCAN_STARTING)),
5407 		};
5408 
5409 		ath12k_mac_scan_send_complete(ar, &info);
5410 	}
5411 
5412 	ar->scan.state = ATH12K_SCAN_IDLE;
5413 	ar->scan_channel = NULL;
5414 	ar->scan.roc_freq = 0;
5415 	spin_unlock_bh(&ar->data_lock);
5416 }
5417 
5418 static int ath12k_start_scan(struct ath12k *ar,
5419 			     struct ath12k_wmi_scan_req_arg *arg)
5420 {
5421 	int ret;
5422 
5423 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
5424 
5425 	ret = ath12k_wmi_send_scan_start_cmd(ar, arg);
5426 	if (ret)
5427 		return ret;
5428 
5429 	ret = wait_for_completion_timeout(&ar->scan.started, 1 * HZ);
5430 	if (ret == 0) {
5431 		ret = ath12k_scan_stop(ar);
5432 		if (ret)
5433 			ath12k_warn(ar->ab, "failed to stop scan: %d\n", ret);
5434 
5435 		return -ETIMEDOUT;
5436 	}
5437 
5438 	/* If we failed to start the scan, return error code at
5439 	 * this point.  This is probably due to some issue in the
5440 	 * firmware, but no need to wedge the driver due to that...
5441 	 */
5442 	spin_lock_bh(&ar->data_lock);
5443 	if (ar->scan.state == ATH12K_SCAN_IDLE) {
5444 		spin_unlock_bh(&ar->data_lock);
5445 		return -EINVAL;
5446 	}
5447 	spin_unlock_bh(&ar->data_lock);
5448 
5449 	return 0;
5450 }
5451 
5452 int ath12k_mac_get_fw_stats(struct ath12k *ar,
5453 			    struct ath12k_fw_stats_req_params *param)
5454 {
5455 	struct ath12k_base *ab = ar->ab;
5456 	struct ath12k_hw *ah = ath12k_ar_to_ah(ar);
5457 	unsigned long time_left;
5458 	int ret;
5459 
5460 	guard(mutex)(&ah->hw_mutex);
5461 
5462 	if (ah->state != ATH12K_HW_STATE_ON)
5463 		return -ENETDOWN;
5464 
5465 	reinit_completion(&ar->fw_stats_complete);
5466 	reinit_completion(&ar->fw_stats_done);
5467 
5468 	ret = ath12k_wmi_send_stats_request_cmd(ar, param->stats_id,
5469 						param->vdev_id, param->pdev_id);
5470 	if (ret) {
5471 		ath12k_warn(ab, "failed to request fw stats: %d\n", ret);
5472 		return ret;
5473 	}
5474 
5475 	ath12k_dbg(ab, ATH12K_DBG_WMI,
5476 		   "get fw stat pdev id %d vdev id %d stats id 0x%x\n",
5477 		   param->pdev_id, param->vdev_id, param->stats_id);
5478 
5479 	time_left = wait_for_completion_timeout(&ar->fw_stats_complete, 1 * HZ);
5480 	if (!time_left) {
5481 		ath12k_warn(ab, "time out while waiting for get fw stats\n");
5482 		return -ETIMEDOUT;
5483 	}
5484 
5485 	/* Firmware sends WMI_UPDATE_STATS_EVENTID back-to-back
5486 	 * when stats data buffer limit is reached. fw_stats_complete
5487 	 * is completed once host receives first event from firmware, but
5488 	 * still there could be more events following. Below is to wait
5489 	 * until firmware completes sending all the events.
5490 	 */
5491 	time_left = wait_for_completion_timeout(&ar->fw_stats_done, 3 * HZ);
5492 	if (!time_left) {
5493 		ath12k_warn(ab, "time out while waiting for fw stats done\n");
5494 		return -ETIMEDOUT;
5495 	}
5496 
5497 	return 0;
5498 }
5499 
5500 int ath12k_mac_op_get_txpower(struct ieee80211_hw *hw,
5501 			      struct ieee80211_vif *vif,
5502 			      unsigned int link_id,
5503 			      int *dbm)
5504 {
5505 	struct ath12k_vif *ahvif = ath12k_vif_to_ahvif(vif);
5506 	struct ath12k_fw_stats_req_params params = {};
5507 	struct ath12k_fw_stats_pdev *pdev;
5508 	struct ath12k_hw *ah = hw->priv;
5509 	struct ath12k_link_vif *arvif;
5510 	struct ath12k_base *ab;
5511 	struct ath12k *ar;
5512 	int ret;
5513 
5514 	/* Final Tx power is minimum of Target Power, CTL power, Regulatory
5515 	 * Power, PSD EIRP Power. We just know the Regulatory power from the
5516 	 * regulatory rules obtained. FW knows all these power and sets the min
5517 	 * of these. Hence, we request the FW pdev stats in which FW reports
5518 	 * the minimum of all vdev's channel Tx power.
5519 	 */
5520 	lockdep_assert_wiphy(hw->wiphy);
5521 
5522 	arvif = wiphy_dereference(ah->hw->wiphy, ahvif->link[link_id]);
5523 	if (!arvif || !arvif->ar)
5524 		return -EINVAL;
5525 
5526 	ar = arvif->ar;
5527 	ab = ar->ab;
5528 	if (ah->state != ATH12K_HW_STATE_ON)
5529 		goto err_fallback;
5530 
5531 	if (test_bit(ATH12K_FLAG_CAC_RUNNING, &ar->dev_flags))
5532 		return -EAGAIN;
5533 
5534 	/* Limit the requests to Firmware for fetching the tx power */
5535 	if (ar->chan_tx_pwr != ATH12K_PDEV_TX_POWER_INVALID &&
5536 	    time_before(jiffies,
5537 			msecs_to_jiffies(ATH12K_PDEV_TX_POWER_REFRESH_TIME_MSECS) +
5538 					 ar->last_tx_power_update))
5539 		goto send_tx_power;
5540 
5541 	params.pdev_id = ath12k_mac_get_target_pdev_id(ar);
5542 	params.vdev_id = arvif->vdev_id;
5543 	params.stats_id = WMI_REQUEST_PDEV_STAT;
5544 	ret = ath12k_mac_get_fw_stats(ar, &params);
5545 	if (ret) {
5546 		ath12k_warn(ab, "failed to request fw pdev stats: %d\n", ret);
5547 		goto err_fallback;
5548 	}
5549 
5550 	spin_lock_bh(&ar->data_lock);
5551 	pdev = list_first_entry_or_null(&ar->fw_stats.pdevs,
5552 					struct ath12k_fw_stats_pdev, list);
5553 	if (!pdev) {
5554 		spin_unlock_bh(&ar->data_lock);
5555 		goto err_fallback;
5556 	}
5557 
5558 	/* tx power reported by firmware is in units of 0.5 dBm */
5559 	ar->chan_tx_pwr = pdev->chan_tx_power / 2;
5560 	spin_unlock_bh(&ar->data_lock);
5561 	ar->last_tx_power_update = jiffies;
5562 	ath12k_fw_stats_reset(ar);
5563 
5564 send_tx_power:
5565 	*dbm = ar->chan_tx_pwr;
5566 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "txpower fetched from firmware %d dBm\n",
5567 		   *dbm);
5568 	return 0;
5569 
5570 err_fallback:
5571 	/* We didn't get txpower from FW. Hence, relying on vif->bss_conf.txpower */
5572 	*dbm = vif->bss_conf.txpower;
5573 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "txpower from firmware NaN, reported %d dBm\n",
5574 		   *dbm);
5575 	return 0;
5576 }
5577 EXPORT_SYMBOL(ath12k_mac_op_get_txpower);
5578 
5579 static u8
5580 ath12k_mac_find_link_id_by_ar(struct ath12k_vif *ahvif, struct ath12k *ar)
5581 {
5582 	struct ath12k_link_vif *arvif;
5583 	struct ath12k_hw *ah = ahvif->ah;
5584 	unsigned long links = ahvif->links_map;
5585 	unsigned long scan_links_map;
5586 	u8 link_id;
5587 
5588 	lockdep_assert_wiphy(ah->hw->wiphy);
5589 
5590 	for_each_set_bit(link_id, &links, ATH12K_NUM_MAX_LINKS) {
5591 		arvif = wiphy_dereference(ah->hw->wiphy, ahvif->link[link_id]);
5592 
5593 		if (!arvif || !arvif->is_created)
5594 			continue;
5595 
5596 		if (ar == arvif->ar)
5597 			return link_id;
5598 	}
5599 
5600 	/* input ar is not assigned to any of the links of ML VIF, use next
5601 	 * available scan link for scan vdev creation. There are cases where
5602 	 * single scan req needs to be split in driver and initiate separate
5603 	 * scan requests to firmware based on device.
5604 	 */
5605 
5606 	 /* Unset all non-scan links (0-14) of scan_links_map so that ffs() will
5607 	  * choose an available link among scan links (i.e link id >= 15)
5608 	  */
5609 	scan_links_map = ~ahvif->links_map & ATH12K_SCAN_LINKS_MASK;
5610 	if (scan_links_map)
5611 		return __ffs(scan_links_map);
5612 
5613 	return ATH12K_FIRST_SCAN_LINK;
5614 }
5615 
5616 static int ath12k_mac_initiate_hw_scan(struct ieee80211_hw *hw,
5617 				       struct ieee80211_vif *vif,
5618 				       struct ieee80211_scan_request *hw_req,
5619 				       int n_channels,
5620 				       struct ieee80211_channel **chan_list,
5621 				       struct ath12k *ar)
5622 {
5623 	struct ath12k_hw *ah = ath12k_hw_to_ah(hw);
5624 	struct ath12k_vif *ahvif = ath12k_vif_to_ahvif(vif);
5625 	struct ath12k_link_vif *arvif;
5626 	struct cfg80211_scan_request *req = &hw_req->req;
5627 	struct ath12k_wmi_scan_req_arg *arg = NULL;
5628 	u8 link_id;
5629 	int ret;
5630 	int i;
5631 	bool create = true;
5632 
5633 	lockdep_assert_wiphy(hw->wiphy);
5634 
5635 	arvif = &ahvif->deflink;
5636 
5637 	/* check if any of the links of ML VIF is already started on
5638 	 * radio(ar) corresponding to given scan frequency and use it,
5639 	 * if not use scan link (link id >= 15) for scan purpose.
5640 	 */
5641 	link_id = ath12k_mac_find_link_id_by_ar(ahvif, ar);
5642 	/* All scan links are occupied. ideally this shouldn't happen as
5643 	 * mac80211 won't schedule scan for same band until ongoing scan is
5644 	 * completed, don't try to exceed max links just in case if it happens.
5645 	 */
5646 	if (link_id >= ATH12K_NUM_MAX_LINKS)
5647 		return -EBUSY;
5648 
5649 	arvif = ath12k_mac_assign_link_vif(ah, vif, link_id);
5650 
5651 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac link ID %d selected for scan",
5652 		   arvif->link_id);
5653 
5654 	/* If the vif is already assigned to a specific vdev of an ar,
5655 	 * check whether its already started, vdev which is started
5656 	 * are not allowed to switch to a new radio.
5657 	 * If the vdev is not started, but was earlier created on a
5658 	 * different ar, delete that vdev and create a new one. We don't
5659 	 * delete at the scan stop as an optimization to avoid redundant
5660 	 * delete-create vdev's for the same ar, in case the request is
5661 	 * always on the same band for the vif
5662 	 */
5663 	if (arvif->is_created) {
5664 		if (WARN_ON(!arvif->ar))
5665 			return -EINVAL;
5666 
5667 		if (ar != arvif->ar && arvif->is_started)
5668 			return -EINVAL;
5669 
5670 		if (ar != arvif->ar) {
5671 			ath12k_mac_remove_link_interface(hw, arvif);
5672 			ath12k_mac_unassign_link_vif(arvif);
5673 		} else {
5674 			create = false;
5675 		}
5676 	}
5677 
5678 	if (create) {
5679 		/* Previous arvif would've been cleared in radio switch block
5680 		 * above, assign arvif again for create.
5681 		 */
5682 		arvif = ath12k_mac_assign_link_vif(ah, vif, link_id);
5683 
5684 		ret = ath12k_mac_vdev_create(ar, arvif);
5685 		if (ret) {
5686 			ath12k_warn(ar->ab, "unable to create scan vdev %d\n", ret);
5687 			ath12k_mac_unassign_link_vif(arvif);
5688 			return ret;
5689 		}
5690 	}
5691 
5692 	spin_lock_bh(&ar->data_lock);
5693 	switch (ar->scan.state) {
5694 	case ATH12K_SCAN_IDLE:
5695 		reinit_completion(&ar->scan.started);
5696 		reinit_completion(&ar->scan.completed);
5697 		ar->scan.state = ATH12K_SCAN_STARTING;
5698 		ar->scan.is_roc = false;
5699 		ar->scan.arvif = arvif;
5700 		ret = 0;
5701 		break;
5702 	case ATH12K_SCAN_STARTING:
5703 	case ATH12K_SCAN_RUNNING:
5704 	case ATH12K_SCAN_ABORTING:
5705 		ret = -EBUSY;
5706 		break;
5707 	}
5708 	spin_unlock_bh(&ar->data_lock);
5709 
5710 	if (ret)
5711 		goto exit;
5712 
5713 	arg = kzalloc_flex(*arg, chan_list, n_channels);
5714 	if (!arg) {
5715 		ret = -ENOMEM;
5716 		goto exit;
5717 	}
5718 
5719 	arg->num_chan = n_channels;
5720 
5721 	ath12k_wmi_start_scan_init(ar, arg);
5722 	arg->vdev_id = arvif->vdev_id;
5723 	arg->scan_id = ATH12K_SCAN_ID;
5724 
5725 	if (req->ie_len) {
5726 		arg->extraie.ptr = kmemdup(req->ie, req->ie_len, GFP_KERNEL);
5727 		if (!arg->extraie.ptr) {
5728 			ret = -ENOMEM;
5729 			goto exit;
5730 		}
5731 		arg->extraie.len = req->ie_len;
5732 	}
5733 
5734 	if (req->n_ssids) {
5735 		arg->num_ssids = req->n_ssids;
5736 		for (i = 0; i < arg->num_ssids; i++)
5737 			arg->ssid[i] = req->ssids[i];
5738 	} else {
5739 		arg->scan_f_passive = 1;
5740 	}
5741 
5742 	for (i = 0; i < arg->num_chan; i++)
5743 		arg->chan_list[i] = chan_list[i]->center_freq;
5744 
5745 	ret = ath12k_start_scan(ar, arg);
5746 	if (ret) {
5747 		if (ret == -EBUSY)
5748 			ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
5749 				   "scan engine is busy 11d state %d\n", ar->state_11d);
5750 		else
5751 			ath12k_warn(ar->ab, "failed to start hw scan: %d\n", ret);
5752 
5753 		spin_lock_bh(&ar->data_lock);
5754 		ar->scan.state = ATH12K_SCAN_IDLE;
5755 		spin_unlock_bh(&ar->data_lock);
5756 		goto exit;
5757 	}
5758 
5759 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac scan started");
5760 
5761 	/* Add a margin to account for event/command processing */
5762 	ieee80211_queue_delayed_work(ath12k_ar_to_hw(ar), &ar->scan.timeout,
5763 				     msecs_to_jiffies(arg->max_scan_time +
5764 						      ATH12K_MAC_SCAN_TIMEOUT_MSECS));
5765 
5766 exit:
5767 	if (arg) {
5768 		kfree(arg->extraie.ptr);
5769 		kfree(arg);
5770 	}
5771 
5772 	if (ar->state_11d == ATH12K_11D_PREPARING &&
5773 	    ahvif->vdev_type == WMI_VDEV_TYPE_STA &&
5774 	    ahvif->vdev_subtype == WMI_VDEV_SUBTYPE_NONE)
5775 		ath12k_mac_11d_scan_start(ar, arvif->vdev_id);
5776 
5777 	return ret;
5778 }
5779 
5780 int ath12k_mac_op_hw_scan(struct ieee80211_hw *hw,
5781 			  struct ieee80211_vif *vif,
5782 			  struct ieee80211_scan_request *hw_req)
5783 {
5784 	struct ath12k_vif *ahvif = ath12k_vif_to_ahvif(vif);
5785 	struct ieee80211_channel **chan_list, *chan;
5786 	struct ath12k_hw *ah = ath12k_hw_to_ah(hw);
5787 	unsigned long links_map, link_id;
5788 	struct ath12k_link_vif *arvif;
5789 	struct ath12k *ar, *scan_ar;
5790 	int i, j, ret = 0;
5791 
5792 	lockdep_assert_wiphy(hw->wiphy);
5793 
5794 	chan_list = kzalloc_objs(*chan_list, hw_req->req.n_channels);
5795 	if (!chan_list)
5796 		return -ENOMEM;
5797 
5798 	/* There could be channels that belong to multiple underlying radio
5799 	 * in same scan request as mac80211 sees it as single band. In that
5800 	 * case split the hw_req based on frequency range and schedule scans to
5801 	 * corresponding radio.
5802 	 */
5803 	for_each_ar(ah, ar, i) {
5804 		int n_chans = 0;
5805 
5806 		for (j = 0; j < hw_req->req.n_channels; j++) {
5807 			chan = hw_req->req.channels[j];
5808 			scan_ar = ath12k_mac_select_scan_device(hw, vif,
5809 								chan->center_freq);
5810 			if (!scan_ar) {
5811 				ath12k_hw_warn(ah, "unable to select scan device for freq %d\n",
5812 					       chan->center_freq);
5813 				ret = -EINVAL;
5814 				goto abort;
5815 			}
5816 			if (ar != scan_ar)
5817 				continue;
5818 
5819 			chan_list[n_chans++] = chan;
5820 		}
5821 		if (n_chans) {
5822 			ret = ath12k_mac_initiate_hw_scan(hw, vif, hw_req, n_chans,
5823 							  chan_list, ar);
5824 			if (ret)
5825 				goto abort;
5826 		}
5827 	}
5828 abort:
5829 	/* If any of the parallel scans initiated fails, abort all and
5830 	 * remove the scan interfaces created. Return complete scan
5831 	 * failure as mac80211 assumes this as single scan request.
5832 	 */
5833 	if (ret) {
5834 		ath12k_hw_warn(ah, "Scan failed %d , cleanup all scan vdevs\n", ret);
5835 		links_map = ahvif->links_map;
5836 		for_each_set_bit(link_id, &links_map, ATH12K_NUM_MAX_LINKS) {
5837 			arvif = wiphy_dereference(hw->wiphy, ahvif->link[link_id]);
5838 			if (!arvif)
5839 				continue;
5840 
5841 			ar = arvif->ar;
5842 			if (ar->scan.arvif == arvif) {
5843 				wiphy_work_cancel(hw->wiphy, &ar->scan.vdev_clean_wk);
5844 				spin_lock_bh(&ar->data_lock);
5845 				ar->scan.arvif = NULL;
5846 				ar->scan.state = ATH12K_SCAN_IDLE;
5847 				ar->scan_channel = NULL;
5848 				ar->scan.roc_freq = 0;
5849 				spin_unlock_bh(&ar->data_lock);
5850 			}
5851 			if (link_id >= ATH12K_FIRST_SCAN_LINK) {
5852 				ath12k_mac_remove_link_interface(hw, arvif);
5853 				ath12k_mac_unassign_link_vif(arvif);
5854 			}
5855 		}
5856 	}
5857 	kfree(chan_list);
5858 	return ret;
5859 }
5860 EXPORT_SYMBOL(ath12k_mac_op_hw_scan);
5861 
5862 void ath12k_mac_op_cancel_hw_scan(struct ieee80211_hw *hw,
5863 				  struct ieee80211_vif *vif)
5864 {
5865 	struct ath12k_vif *ahvif = ath12k_vif_to_ahvif(vif);
5866 	unsigned long link_id, links_map = ahvif->links_map;
5867 	struct ath12k_link_vif *arvif;
5868 	struct ath12k *ar;
5869 
5870 	lockdep_assert_wiphy(hw->wiphy);
5871 
5872 	for_each_set_bit(link_id, &links_map, ATH12K_NUM_MAX_LINKS) {
5873 		arvif = wiphy_dereference(hw->wiphy, ahvif->link[link_id]);
5874 		if (!arvif || !arvif->is_created ||
5875 		    arvif->ar->scan.arvif != arvif)
5876 			continue;
5877 
5878 		ar = arvif->ar;
5879 
5880 		ath12k_scan_abort(ar);
5881 
5882 		cancel_delayed_work_sync(&ar->scan.timeout);
5883 	}
5884 }
5885 EXPORT_SYMBOL(ath12k_mac_op_cancel_hw_scan);
5886 
5887 static int ath12k_install_key(struct ath12k_link_vif *arvif,
5888 			      struct ieee80211_key_conf *key,
5889 			      enum set_key_cmd cmd,
5890 			      const u8 *macaddr, u32 flags)
5891 {
5892 	int ret;
5893 	struct ath12k *ar = arvif->ar;
5894 	struct wmi_vdev_install_key_arg arg = {
5895 		.vdev_id = arvif->vdev_id,
5896 		.key_idx = key->keyidx,
5897 		.key_len = key->keylen,
5898 		.key_data = key->key,
5899 		.key_flags = flags,
5900 		.ieee80211_key_cipher = key->cipher,
5901 		.macaddr = macaddr,
5902 	};
5903 	struct ath12k_vif *ahvif = arvif->ahvif;
5904 
5905 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
5906 
5907 	if (test_bit(ATH12K_FLAG_HW_CRYPTO_DISABLED, &ar->ab->dev_flags))
5908 		return 0;
5909 
5910 	if (cmd == DISABLE_KEY) {
5911 		/* TODO: Check if FW expects  value other than NONE for del */
5912 		/* arg.key_cipher = WMI_CIPHER_NONE; */
5913 		arg.key_len = 0;
5914 		arg.key_data = NULL;
5915 		goto check_order;
5916 	}
5917 
5918 	switch (key->cipher) {
5919 	case WLAN_CIPHER_SUITE_CCMP:
5920 	case WLAN_CIPHER_SUITE_CCMP_256:
5921 		arg.key_cipher = WMI_CIPHER_AES_CCM;
5922 		key->flags |= IEEE80211_KEY_FLAG_GENERATE_IV_MGMT;
5923 		break;
5924 	case WLAN_CIPHER_SUITE_TKIP:
5925 		arg.key_cipher = WMI_CIPHER_TKIP;
5926 		arg.key_txmic_len = 8;
5927 		arg.key_rxmic_len = 8;
5928 		break;
5929 	case WLAN_CIPHER_SUITE_GCMP:
5930 	case WLAN_CIPHER_SUITE_GCMP_256:
5931 		arg.key_cipher = WMI_CIPHER_AES_GCM;
5932 		key->flags |= IEEE80211_KEY_FLAG_GENERATE_IV_MGMT;
5933 		break;
5934 	case WLAN_CIPHER_SUITE_AES_CMAC:
5935 		arg.key_cipher = WMI_CIPHER_AES_CMAC;
5936 		break;
5937 	case WLAN_CIPHER_SUITE_BIP_GMAC_128:
5938 	case WLAN_CIPHER_SUITE_BIP_GMAC_256:
5939 		arg.key_cipher = WMI_CIPHER_AES_GMAC;
5940 		break;
5941 	case WLAN_CIPHER_SUITE_BIP_CMAC_256:
5942 		arg.key_cipher = WMI_CIPHER_AES_CMAC;
5943 		break;
5944 	default:
5945 		ath12k_warn(ar->ab, "cipher %d is not supported\n", key->cipher);
5946 		return -EOPNOTSUPP;
5947 	}
5948 
5949 	if (test_bit(ATH12K_FLAG_RAW_MODE, &ar->ab->dev_flags))
5950 		key->flags |= IEEE80211_KEY_FLAG_GENERATE_IV |
5951 			      IEEE80211_KEY_FLAG_RESERVE_TAILROOM;
5952 
5953 check_order:
5954 	if (ahvif->vdev_type == WMI_VDEV_TYPE_STA &&
5955 	    arg.key_flags == WMI_KEY_GROUP) {
5956 		if (cmd == SET_KEY) {
5957 			if (arvif->pairwise_key_done) {
5958 				ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
5959 					   "vdev %u pairwise key done, go install group key\n",
5960 					   arg.vdev_id);
5961 				goto install;
5962 			} else {
5963 				/* WCN7850 firmware requires pairwise key to be installed
5964 				 * before group key. In case group key comes first, cache
5965 				 * it and return. Will revisit it once pairwise key gets
5966 				 * installed.
5967 				 */
5968 				arvif->group_key = arg;
5969 				arvif->group_key_valid = true;
5970 				ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
5971 					   "vdev %u group key before pairwise key, cache and skip\n",
5972 					   arg.vdev_id);
5973 
5974 				ret = 0;
5975 				goto out;
5976 			}
5977 		} else {
5978 			arvif->group_key_valid = false;
5979 		}
5980 	}
5981 
5982 install:
5983 	reinit_completion(&ar->install_key_done);
5984 
5985 	ret = ath12k_wmi_vdev_install_key(arvif->ar, &arg);
5986 	if (ret)
5987 		return ret;
5988 
5989 	if (!wait_for_completion_timeout(&ar->install_key_done, 1 * HZ))
5990 		return -ETIMEDOUT;
5991 
5992 	if (ether_addr_equal(arg.macaddr, arvif->bssid))
5993 		ahvif->dp_vif.key_cipher = arg.ieee80211_key_cipher;
5994 
5995 	if (ar->install_key_status) {
5996 		ret = -EINVAL;
5997 		goto out;
5998 	}
5999 
6000 	if (ahvif->vdev_type == WMI_VDEV_TYPE_STA &&
6001 	    arg.key_flags == WMI_KEY_PAIRWISE) {
6002 		if (cmd == SET_KEY) {
6003 			arvif->pairwise_key_done = true;
6004 			if (arvif->group_key_valid) {
6005 				/* Install cached GTK */
6006 				arvif->group_key_valid = false;
6007 				arg = arvif->group_key;
6008 				ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
6009 					   "vdev %u pairwise key done, group key ready, go install\n",
6010 					   arg.vdev_id);
6011 				goto install;
6012 			}
6013 		} else {
6014 			arvif->pairwise_key_done = false;
6015 		}
6016 	}
6017 
6018 out:
6019 	if (ret) {
6020 		/* In case of failure userspace may not do DISABLE_KEY
6021 		 * but triggers re-connection directly, so manually reset
6022 		 * status here.
6023 		 */
6024 		arvif->group_key_valid = false;
6025 		arvif->pairwise_key_done = false;
6026 	}
6027 
6028 	return ret;
6029 }
6030 
6031 static int ath12k_clear_peer_keys(struct ath12k_link_vif *arvif,
6032 				  const u8 *addr)
6033 {
6034 	struct ath12k *ar = arvif->ar;
6035 	struct ath12k_base *ab = ar->ab;
6036 	struct ath12k_dp_link_peer *peer;
6037 	int first_errno = 0;
6038 	int ret;
6039 	int i, len;
6040 	u32 flags = 0;
6041 	struct ath12k_dp *dp = ath12k_ab_to_dp(ab);
6042 	struct ieee80211_key_conf *keys[WMI_MAX_KEY_INDEX + 1] = {};
6043 
6044 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
6045 
6046 	spin_lock_bh(&dp->dp_lock);
6047 	peer = ath12k_dp_link_peer_find_by_vdev_and_addr(dp, arvif->vdev_id, addr);
6048 	if (!peer || !peer->dp_peer) {
6049 		spin_unlock_bh(&dp->dp_lock);
6050 		return -ENOENT;
6051 	}
6052 
6053 	len = ARRAY_SIZE(peer->dp_peer->keys);
6054 	for (i = 0; i < len; i++) {
6055 		if (!peer->dp_peer->keys[i])
6056 			continue;
6057 
6058 		keys[i] = peer->dp_peer->keys[i];
6059 		peer->dp_peer->keys[i] = NULL;
6060 	}
6061 
6062 	spin_unlock_bh(&dp->dp_lock);
6063 
6064 	for (i = 0; i < len; i++) {
6065 		if (!keys[i])
6066 			continue;
6067 
6068 		/* key flags are not required to delete the key */
6069 		ret = ath12k_install_key(arvif, keys[i],
6070 					 DISABLE_KEY, addr, flags);
6071 		if (ret < 0 && first_errno == 0)
6072 			first_errno = ret;
6073 
6074 		if (ret < 0)
6075 			ath12k_warn(ab, "failed to remove peer key %d: %d\n",
6076 				    i, ret);
6077 	}
6078 
6079 	return first_errno;
6080 }
6081 
6082 static int ath12k_mac_set_key(struct ath12k *ar, enum set_key_cmd cmd,
6083 			      struct ath12k_link_vif *arvif,
6084 			      struct ath12k_link_sta *arsta,
6085 			      struct ieee80211_key_conf *key)
6086 {
6087 	struct ieee80211_sta *sta = NULL;
6088 	struct ath12k_base *ab = ar->ab;
6089 	struct ath12k_dp_link_peer *peer;
6090 	struct ath12k_sta *ahsta;
6091 	const u8 *peer_addr;
6092 	int ret;
6093 	u32 flags = 0;
6094 	struct ath12k_dp *dp = ath12k_ab_to_dp(ab);
6095 
6096 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
6097 
6098 	if (arsta)
6099 		sta = ath12k_ahsta_to_sta(arsta->ahsta);
6100 
6101 	if (test_bit(ATH12K_FLAG_HW_CRYPTO_DISABLED, &ab->dev_flags))
6102 		return 1;
6103 
6104 	if (sta)
6105 		peer_addr = arsta->addr;
6106 	else
6107 		peer_addr = arvif->bssid;
6108 
6109 	key->hw_key_idx = key->keyidx;
6110 
6111 	/* the peer should not disappear in mid-way (unless FW goes awry) since
6112 	 * we already hold wiphy lock. we just make sure its there now.
6113 	 */
6114 	spin_lock_bh(&dp->dp_lock);
6115 	peer = ath12k_dp_link_peer_find_by_vdev_and_addr(dp, arvif->vdev_id,
6116 							 peer_addr);
6117 	if (!peer || !peer->dp_peer) {
6118 		spin_unlock_bh(&dp->dp_lock);
6119 
6120 		if (cmd == SET_KEY) {
6121 			ath12k_warn(ab, "cannot install key for non-existent peer %pM\n",
6122 				    peer_addr);
6123 			return -EOPNOTSUPP;
6124 		}
6125 
6126 		/* if the peer doesn't exist there is no key to disable
6127 		 * anymore
6128 		 */
6129 		return 0;
6130 	}
6131 
6132 	spin_unlock_bh(&dp->dp_lock);
6133 
6134 	if (key->flags & IEEE80211_KEY_FLAG_PAIRWISE)
6135 		flags = WMI_KEY_PAIRWISE;
6136 	else
6137 		flags = WMI_KEY_GROUP;
6138 
6139 	ret = ath12k_install_key(arvif, key, cmd, peer_addr, flags);
6140 	if (ret) {
6141 		ath12k_warn(ab, "ath12k_install_key failed (%d)\n", ret);
6142 		return ret;
6143 	}
6144 
6145 	ret = ath12k_dp_rx_peer_pn_replay_config(arvif, peer_addr, cmd, key);
6146 	if (ret) {
6147 		ath12k_warn(ab, "failed to offload PN replay detection %d\n", ret);
6148 		return ret;
6149 	}
6150 
6151 	spin_lock_bh(&dp->dp_lock);
6152 	peer = ath12k_dp_link_peer_find_by_vdev_and_addr(dp, arvif->vdev_id,
6153 							 peer_addr);
6154 	if (peer && peer->dp_peer && cmd == SET_KEY) {
6155 		peer->dp_peer->keys[key->keyidx] = key;
6156 		if (key->flags & IEEE80211_KEY_FLAG_PAIRWISE) {
6157 			peer->dp_peer->ucast_keyidx = key->keyidx;
6158 			peer->dp_peer->sec_type =
6159 					ath12k_dp_tx_get_encrypt_type(key->cipher);
6160 		} else {
6161 			peer->dp_peer->mcast_keyidx = key->keyidx;
6162 			peer->dp_peer->sec_type_grp =
6163 					ath12k_dp_tx_get_encrypt_type(key->cipher);
6164 		}
6165 	} else if (peer && peer->dp_peer && cmd == DISABLE_KEY) {
6166 		peer->dp_peer->keys[key->keyidx] = NULL;
6167 		if (key->flags & IEEE80211_KEY_FLAG_PAIRWISE)
6168 			peer->dp_peer->ucast_keyidx = 0;
6169 		else
6170 			peer->dp_peer->mcast_keyidx = 0;
6171 	} else if (!peer)
6172 		/* impossible unless FW goes crazy */
6173 		ath12k_warn(ab, "peer %pM disappeared!\n", peer_addr);
6174 
6175 	if (sta) {
6176 		ahsta = ath12k_sta_to_ahsta(sta);
6177 
6178 		switch (key->cipher) {
6179 		case WLAN_CIPHER_SUITE_TKIP:
6180 		case WLAN_CIPHER_SUITE_CCMP:
6181 		case WLAN_CIPHER_SUITE_CCMP_256:
6182 		case WLAN_CIPHER_SUITE_GCMP:
6183 		case WLAN_CIPHER_SUITE_GCMP_256:
6184 			if (cmd == SET_KEY)
6185 				ahsta->pn_type = HAL_PN_TYPE_WPA;
6186 			else
6187 				ahsta->pn_type = HAL_PN_TYPE_NONE;
6188 			break;
6189 		default:
6190 			ahsta->pn_type = HAL_PN_TYPE_NONE;
6191 			break;
6192 		}
6193 	}
6194 
6195 	spin_unlock_bh(&dp->dp_lock);
6196 
6197 	return 0;
6198 }
6199 
6200 static int ath12k_mac_update_key_cache(struct ath12k_vif_cache *cache,
6201 				       enum set_key_cmd cmd,
6202 				       struct ieee80211_sta *sta,
6203 				       struct ieee80211_key_conf *key)
6204 {
6205 	struct ath12k_key_conf *key_conf, *tmp;
6206 
6207 	list_for_each_entry_safe(key_conf, tmp, &cache->key_conf.list, list) {
6208 		if (key_conf->key != key)
6209 			continue;
6210 
6211 		/* If SET key entry is already present in cache, nothing to do,
6212 		 * just return
6213 		 */
6214 		if (cmd == SET_KEY)
6215 			return 0;
6216 
6217 		/* DEL key for an old SET key which driver hasn't flushed yet.
6218 		 */
6219 		list_del(&key_conf->list);
6220 		kfree(key_conf);
6221 	}
6222 
6223 	if (cmd == SET_KEY) {
6224 		key_conf = kzalloc_obj(*key_conf);
6225 
6226 		if (!key_conf)
6227 			return -ENOMEM;
6228 
6229 		key_conf->cmd = cmd;
6230 		key_conf->sta = sta;
6231 		key_conf->key = key;
6232 		list_add_tail(&key_conf->list,
6233 			      &cache->key_conf.list);
6234 	}
6235 
6236 	return 0;
6237 }
6238 
6239 int ath12k_mac_op_set_key(struct ieee80211_hw *hw, enum set_key_cmd cmd,
6240 			  struct ieee80211_vif *vif, struct ieee80211_sta *sta,
6241 			  struct ieee80211_key_conf *key)
6242 {
6243 	struct ath12k_vif *ahvif = ath12k_vif_to_ahvif(vif);
6244 	struct ath12k_link_vif *arvif;
6245 	struct ath12k_link_sta *arsta = NULL;
6246 	struct ath12k_vif_cache *cache;
6247 	struct ath12k_sta *ahsta;
6248 	unsigned long links;
6249 	u8 link_id;
6250 	int ret;
6251 
6252 	lockdep_assert_wiphy(hw->wiphy);
6253 
6254 	/* IGTK needs to be done in host software */
6255 	if (key->keyidx == 4 || key->keyidx == 5)
6256 		return 1;
6257 
6258 	if (key->keyidx > WMI_MAX_KEY_INDEX)
6259 		return -ENOSPC;
6260 
6261 	if (sta) {
6262 		ahsta = ath12k_sta_to_ahsta(sta);
6263 
6264 		/* For an ML STA Pairwise key is same for all associated link Stations,
6265 		 * hence do set key for all link STAs which are active.
6266 		 */
6267 		if (sta->mlo) {
6268 			links = ahsta->links_map;
6269 			for_each_set_bit(link_id, &links, IEEE80211_MLD_MAX_NUM_LINKS) {
6270 				arvif = wiphy_dereference(hw->wiphy,
6271 							  ahvif->link[link_id]);
6272 				arsta = wiphy_dereference(hw->wiphy,
6273 							  ahsta->link[link_id]);
6274 
6275 				if (WARN_ON(!arvif || !arsta))
6276 					/* arvif and arsta are expected to be valid when
6277 					 * STA is present.
6278 					 */
6279 					continue;
6280 
6281 				ret = ath12k_mac_set_key(arvif->ar, cmd, arvif,
6282 							 arsta, key);
6283 				if (ret)
6284 					break;
6285 			}
6286 
6287 			return 0;
6288 		}
6289 
6290 		arsta = &ahsta->deflink;
6291 		arvif = arsta->arvif;
6292 		if (WARN_ON(!arvif))
6293 			return -EINVAL;
6294 
6295 		ret = ath12k_mac_set_key(arvif->ar, cmd, arvif, arsta, key);
6296 		if (ret)
6297 			return ret;
6298 
6299 		return 0;
6300 	}
6301 
6302 	if (key->link_id >= 0 && key->link_id < IEEE80211_MLD_MAX_NUM_LINKS) {
6303 		link_id = key->link_id;
6304 		arvif = wiphy_dereference(hw->wiphy, ahvif->link[link_id]);
6305 	} else {
6306 		link_id = 0;
6307 		arvif = &ahvif->deflink;
6308 	}
6309 
6310 	if (!arvif || !arvif->is_created) {
6311 		cache = ath12k_ahvif_get_link_cache(ahvif, link_id);
6312 		if (!cache)
6313 			return -ENOSPC;
6314 
6315 		ret = ath12k_mac_update_key_cache(cache, cmd, sta, key);
6316 		if (ret)
6317 			return ret;
6318 
6319 		return 0;
6320 	}
6321 
6322 	ret = ath12k_mac_set_key(arvif->ar, cmd, arvif, NULL, key);
6323 	if (ret)
6324 		return ret;
6325 
6326 	return 0;
6327 }
6328 EXPORT_SYMBOL(ath12k_mac_op_set_key);
6329 
6330 static int
6331 ath12k_mac_bitrate_mask_num_vht_rates(struct ath12k *ar,
6332 				      enum nl80211_band band,
6333 				      const struct cfg80211_bitrate_mask *mask)
6334 {
6335 	int num_rates = 0;
6336 	int i;
6337 
6338 	for (i = 0; i < ARRAY_SIZE(mask->control[band].vht_mcs); i++)
6339 		num_rates += hweight16(mask->control[band].vht_mcs[i]);
6340 
6341 	return num_rates;
6342 }
6343 
6344 static int
6345 ath12k_mac_bitrate_mask_num_he_rates(struct ath12k *ar,
6346 				     enum nl80211_band band,
6347 				     const struct cfg80211_bitrate_mask *mask)
6348 {
6349 	int num_rates = 0;
6350 	int i;
6351 
6352 	for (i = 0; i < ARRAY_SIZE(mask->control[band].he_mcs); i++)
6353 		num_rates += hweight16(mask->control[band].he_mcs[i]);
6354 
6355 	return num_rates;
6356 }
6357 
6358 static int
6359 ath12k_mac_bitrate_mask_num_eht_rates(struct ath12k *ar,
6360 				      enum nl80211_band band,
6361 				      const struct cfg80211_bitrate_mask *mask)
6362 {
6363 	int num_rates = 0;
6364 	int i;
6365 
6366 	for (i = 0; i < ARRAY_SIZE(mask->control[band].eht_mcs); i++)
6367 		num_rates += hweight16(mask->control[band].eht_mcs[i]);
6368 
6369 	return num_rates;
6370 }
6371 
6372 static int
6373 ath12k_mac_set_peer_vht_fixed_rate(struct ath12k_link_vif *arvif,
6374 				   struct ath12k_link_sta *arsta,
6375 				   const struct cfg80211_bitrate_mask *mask,
6376 				   enum nl80211_band band)
6377 {
6378 	struct ath12k *ar = arvif->ar;
6379 	u8 vht_rate, nss;
6380 	u32 rate_code;
6381 	int ret, i;
6382 
6383 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
6384 
6385 	nss = 0;
6386 
6387 	for (i = 0; i < ARRAY_SIZE(mask->control[band].vht_mcs); i++) {
6388 		if (hweight16(mask->control[band].vht_mcs[i]) == 1) {
6389 			nss = i + 1;
6390 			vht_rate = ffs(mask->control[band].vht_mcs[i]) - 1;
6391 		}
6392 	}
6393 
6394 	if (!nss) {
6395 		ath12k_warn(ar->ab, "No single VHT Fixed rate found to set for %pM",
6396 			    arsta->addr);
6397 		return -EINVAL;
6398 	}
6399 
6400 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
6401 		   "Setting Fixed VHT Rate for peer %pM. Device will not switch to any other selected rates",
6402 		   arsta->addr);
6403 
6404 	rate_code = ATH12K_HW_RATE_CODE(vht_rate, nss - 1,
6405 					WMI_RATE_PREAMBLE_VHT);
6406 	ret = ath12k_wmi_set_peer_param(ar, arsta->addr,
6407 					arvif->vdev_id,
6408 					WMI_PEER_PARAM_FIXED_RATE,
6409 					rate_code);
6410 	if (ret)
6411 		ath12k_warn(ar->ab,
6412 			    "failed to update STA %pM Fixed Rate %d: %d\n",
6413 			     arsta->addr, rate_code, ret);
6414 
6415 	return ret;
6416 }
6417 
6418 static int
6419 ath12k_mac_set_peer_he_fixed_rate(struct ath12k_link_vif *arvif,
6420 				  struct ath12k_link_sta *arsta,
6421 				  const struct cfg80211_bitrate_mask *mask,
6422 				  enum nl80211_band band)
6423 {
6424 	struct ath12k *ar = arvif->ar;
6425 	u8 he_rate, nss;
6426 	u32 rate_code;
6427 	int ret, i;
6428 	struct ath12k_sta *ahsta = arsta->ahsta;
6429 	struct ieee80211_sta *sta;
6430 
6431 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
6432 
6433 	sta = ath12k_ahsta_to_sta(ahsta);
6434 	nss = 0;
6435 
6436 	for (i = 0; i < ARRAY_SIZE(mask->control[band].he_mcs); i++) {
6437 		if (hweight16(mask->control[band].he_mcs[i]) == 1) {
6438 			nss = i + 1;
6439 			he_rate = ffs(mask->control[band].he_mcs[i]) - 1;
6440 		}
6441 	}
6442 
6443 	if (!nss) {
6444 		ath12k_warn(ar->ab, "No single HE Fixed rate found to set for %pM",
6445 			    arsta->addr);
6446 		return -EINVAL;
6447 	}
6448 
6449 	/* Avoid updating invalid nss as fixed rate*/
6450 	if (nss > sta->deflink.rx_nss)
6451 		return -EINVAL;
6452 
6453 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
6454 		   "Setting Fixed HE Rate for peer %pM. Device will not switch to any other selected rates",
6455 		   arsta->addr);
6456 
6457 	rate_code = ATH12K_HW_RATE_CODE(he_rate, nss - 1,
6458 					WMI_RATE_PREAMBLE_HE);
6459 
6460 	ret = ath12k_wmi_set_peer_param(ar, arsta->addr,
6461 					arvif->vdev_id,
6462 					WMI_PEER_PARAM_FIXED_RATE,
6463 					rate_code);
6464 	if (ret)
6465 		ath12k_warn(ar->ab,
6466 			    "failed to update STA %pM Fixed Rate %d: %d\n",
6467 			    arsta->addr, rate_code, ret);
6468 
6469 	return ret;
6470 }
6471 
6472 static int
6473 ath12k_mac_set_peer_eht_fixed_rate(struct ath12k_link_vif *arvif,
6474 				   struct ath12k_link_sta *arsta,
6475 				   const struct cfg80211_bitrate_mask *mask,
6476 				   enum nl80211_band band)
6477 {
6478 	struct ath12k_sta *ahsta = arsta->ahsta;
6479 	struct ath12k *ar = arvif->ar;
6480 	struct ieee80211_sta *sta;
6481 	struct ieee80211_link_sta *link_sta;
6482 	u8 eht_rate, nss = 0;
6483 	u32 rate_code;
6484 	int ret, i;
6485 
6486 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
6487 
6488 	sta = ath12k_ahsta_to_sta(ahsta);
6489 
6490 	for (i = 0; i < ARRAY_SIZE(mask->control[band].eht_mcs); i++) {
6491 		if (hweight16(mask->control[band].eht_mcs[i]) == 1) {
6492 			nss = i + 1;
6493 			eht_rate = ffs(mask->control[band].eht_mcs[i]) - 1;
6494 		}
6495 	}
6496 
6497 	if (!nss) {
6498 		ath12k_warn(ar->ab, "No single EHT Fixed rate found to set for %pM\n",
6499 			    arsta->addr);
6500 		return -EINVAL;
6501 	}
6502 
6503 	/* Avoid updating invalid nss as fixed rate*/
6504 	link_sta = ath12k_mac_get_link_sta(arsta);
6505 	if (!link_sta || nss > link_sta->rx_nss) {
6506 		ath12k_warn(ar->ab,
6507 			    "unable to access link sta for sta %pM link %u or fixed nss of %u is not supported by sta\n",
6508 			    sta->addr, arsta->link_id, nss);
6509 		return -EINVAL;
6510 	}
6511 
6512 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
6513 		   "Setting Fixed EHT Rate for peer %pM. Device will not switch to any other selected rates\n",
6514 		   arsta->addr);
6515 
6516 	rate_code = ATH12K_HW_RATE_CODE(eht_rate, nss - 1,
6517 					WMI_RATE_PREAMBLE_EHT);
6518 
6519 	ret = ath12k_wmi_set_peer_param(ar, arsta->addr,
6520 					arvif->vdev_id,
6521 					WMI_PEER_PARAM_FIXED_RATE,
6522 					rate_code);
6523 	if (ret)
6524 		ath12k_warn(ar->ab,
6525 			    "failed to update STA %pM Fixed Rate %d: %d\n",
6526 			    arsta->addr, rate_code, ret);
6527 
6528 	return ret;
6529 }
6530 
6531 static int ath12k_mac_station_assoc(struct ath12k *ar,
6532 				    struct ath12k_link_vif *arvif,
6533 				    struct ath12k_link_sta *arsta,
6534 				    bool reassoc)
6535 {
6536 	struct ieee80211_vif *vif = ath12k_ahvif_to_vif(arvif->ahvif);
6537 	struct ieee80211_sta *sta = ath12k_ahsta_to_sta(arsta->ahsta);
6538 	struct ieee80211_link_sta *link_sta;
6539 	int ret;
6540 	struct cfg80211_chan_def def;
6541 	enum nl80211_band band;
6542 	struct cfg80211_bitrate_mask *mask;
6543 	u8 num_vht_rates, num_he_rates, num_eht_rates;
6544 	u8 link_id = arvif->link_id;
6545 
6546 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
6547 
6548 	if (WARN_ON(ath12k_mac_vif_link_chan(vif, arvif->link_id, &def)))
6549 		return -EPERM;
6550 
6551 	if (WARN_ON(!rcu_access_pointer(sta->link[link_id])))
6552 		return -EINVAL;
6553 
6554 	band = def.chan->band;
6555 	mask = &arvif->bitrate_mask;
6556 
6557 	struct ath12k_wmi_peer_assoc_arg *peer_arg __free(kfree) =
6558 		kzalloc_obj(*peer_arg);
6559 	if (!peer_arg)
6560 		return -ENOMEM;
6561 
6562 	ath12k_peer_assoc_prepare(ar, arvif, arsta, peer_arg, reassoc);
6563 
6564 	if (peer_arg->peer_nss < 1) {
6565 		ath12k_warn(ar->ab,
6566 			    "invalid peer NSS %d\n", peer_arg->peer_nss);
6567 		return -EINVAL;
6568 	}
6569 
6570 	peer_arg->is_assoc = true;
6571 
6572 	ret = ath12k_mac_peer_assoc(ar, peer_arg);
6573 	if (ret)
6574 		return ret;
6575 
6576 	num_vht_rates = ath12k_mac_bitrate_mask_num_vht_rates(ar, band, mask);
6577 	num_he_rates = ath12k_mac_bitrate_mask_num_he_rates(ar, band, mask);
6578 	num_eht_rates = ath12k_mac_bitrate_mask_num_eht_rates(ar, band, mask);
6579 
6580 	/* If single VHT/HE/EHT rate is configured (by set_bitrate_mask()),
6581 	 * peer_assoc will disable VHT/HE/EHT. This is now enabled by a peer
6582 	 * specific fixed param.
6583 	 * Note that all other rates and NSS will be disabled for this peer.
6584 	 */
6585 	link_sta = ath12k_mac_get_link_sta(arsta);
6586 	if (!link_sta) {
6587 		ath12k_warn(ar->ab, "unable to access link sta in station assoc\n");
6588 		return -EINVAL;
6589 	}
6590 
6591 	spin_lock_bh(&ar->data_lock);
6592 	arsta->bw = ath12k_mac_ieee80211_sta_bw_to_wmi(ar, link_sta);
6593 	arsta->bw_prev = link_sta->bandwidth;
6594 	spin_unlock_bh(&ar->data_lock);
6595 
6596 	if (link_sta->vht_cap.vht_supported && num_vht_rates == 1) {
6597 		ret = ath12k_mac_set_peer_vht_fixed_rate(arvif, arsta, mask, band);
6598 	} else if (link_sta->he_cap.has_he && num_he_rates == 1) {
6599 		ret = ath12k_mac_set_peer_he_fixed_rate(arvif, arsta, mask, band);
6600 		if (ret)
6601 			return ret;
6602 	} else if (link_sta->eht_cap.has_eht && num_eht_rates == 1) {
6603 		ret = ath12k_mac_set_peer_eht_fixed_rate(arvif, arsta, mask, band);
6604 		if (ret)
6605 			return ret;
6606 	}
6607 
6608 	/* Re-assoc is run only to update supported rates for given station. It
6609 	 * doesn't make much sense to reconfigure the peer completely.
6610 	 */
6611 	if (reassoc)
6612 		return 0;
6613 
6614 	ret = ath12k_setup_peer_smps(ar, arvif, arsta->addr,
6615 				     &link_sta->ht_cap, &link_sta->he_6ghz_capa);
6616 	if (ret) {
6617 		ath12k_warn(ar->ab, "failed to setup peer SMPS for vdev %d: %d\n",
6618 			    arvif->vdev_id, ret);
6619 		return ret;
6620 	}
6621 
6622 	if (!sta->wme) {
6623 		arvif->num_legacy_stations++;
6624 		ret = ath12k_recalc_rtscts_prot(arvif);
6625 		if (ret)
6626 			return ret;
6627 	}
6628 
6629 	if (sta->wme && sta->uapsd_queues) {
6630 		ret = ath12k_peer_assoc_qos_ap(ar, arvif, arsta);
6631 		if (ret) {
6632 			ath12k_warn(ar->ab, "failed to set qos params for STA %pM for vdev %i: %d\n",
6633 				    arsta->addr, arvif->vdev_id, ret);
6634 			return ret;
6635 		}
6636 	}
6637 
6638 	return 0;
6639 }
6640 
6641 static int ath12k_mac_station_disassoc(struct ath12k *ar,
6642 				       struct ath12k_link_vif *arvif,
6643 				       struct ath12k_link_sta *arsta)
6644 {
6645 	struct ieee80211_sta *sta = ath12k_ahsta_to_sta(arsta->ahsta);
6646 
6647 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
6648 
6649 	if (!sta->wme) {
6650 		arvif->num_legacy_stations--;
6651 		return ath12k_recalc_rtscts_prot(arvif);
6652 	}
6653 
6654 	return 0;
6655 }
6656 
6657 static int ath12k_mac_sta_set_4addr(struct wiphy *wiphy, struct ath12k_sta *ahsta)
6658 {
6659 	struct ath12k_dp_link_peer *peer;
6660 	struct ath12k_link_vif *arvif;
6661 	struct ath12k_link_sta *arsta;
6662 	struct ath12k_vif *ahvif;
6663 	struct ath12k_dp *dp;
6664 	unsigned long links;
6665 	struct ath12k *ar;
6666 	u8 link_id;
6667 	int ret;
6668 
6669 	links = ahsta->links_map;
6670 	for_each_set_bit(link_id, &links, IEEE80211_MLD_MAX_NUM_LINKS) {
6671 		arsta = wiphy_dereference(wiphy, ahsta->link[link_id]);
6672 		if (!arsta)
6673 			continue;
6674 
6675 		arvif = arsta->arvif;
6676 		ahvif = arvif->ahvif;
6677 		ar = arvif->ar;
6678 
6679 		if (arvif->set_wds_vdev_param)
6680 			goto skip_nawds;
6681 
6682 		ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
6683 			   "setting USE_4ADDR for peer %pM\n", arsta->addr);
6684 
6685 		ret = ath12k_wmi_set_peer_param(ar, arsta->addr,
6686 						arvif->vdev_id,
6687 						WMI_PEER_USE_4ADDR,
6688 						WMI_PEER_4ADDR_ALLOW_EAPOL_DATA_FRAME);
6689 		if (ret) {
6690 			ath12k_warn(ar->ab, "failed to set peer %pM 4addr capability: %d\n",
6691 				    arsta->addr, ret);
6692 			return ret;
6693 		}
6694 
6695 		if (ahvif->dp_vif.tx_encap_type != ATH12K_HW_TXRX_ETHERNET)
6696 			goto skip_nawds;
6697 
6698 		ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
6699 						    WMI_VDEV_PARAM_AP_ENABLE_NAWDS,
6700 						    WDS_EXT_ENABLE);
6701 		if (ret) {
6702 			ath12k_warn(ar->ab, "failed to set vdev %d nawds parameter: %d\n",
6703 				    arvif->vdev_id, ret);
6704 			return ret;
6705 		}
6706 
6707 		arvif->nawds_enabled = true;
6708 
6709 skip_nawds:
6710 		dp = ath12k_ab_to_dp(ar->ab);
6711 		spin_lock_bh(&dp->dp_lock);
6712 		peer = ath12k_dp_link_peer_find_by_vdev_and_addr(dp, arvif->vdev_id,
6713 								 arsta->addr);
6714 		if (peer && peer->dp_peer) {
6715 			peer->dp_peer->ucast_ra_only = true;
6716 			peer->dp_peer->use_4addr = true;
6717 		} else {
6718 			spin_unlock_bh(&dp->dp_lock);
6719 			ath12k_warn(ar->ab, "failed to find DP peer for %pM\n",
6720 				    arsta->addr);
6721 			return -ENOENT;
6722 		}
6723 
6724 		spin_unlock_bh(&dp->dp_lock);
6725 	}
6726 
6727 	return 0;
6728 }
6729 
6730 static void ath12k_sta_rc_update_wk(struct wiphy *wiphy, struct wiphy_work *wk)
6731 {
6732 	struct ieee80211_link_sta *link_sta;
6733 	struct ath12k *ar;
6734 	struct ath12k_link_vif *arvif;
6735 	struct ieee80211_sta *sta;
6736 	struct cfg80211_chan_def def;
6737 	enum nl80211_band band;
6738 	const u8 *ht_mcs_mask;
6739 	const u16 *vht_mcs_mask;
6740 	const u16 *he_mcs_mask;
6741 	const u16 *eht_mcs_mask;
6742 	u32 changed, bw, nss, mac_nss, smps, bw_prev;
6743 	int err, num_vht_rates, num_he_rates, num_eht_rates;
6744 	const struct cfg80211_bitrate_mask *mask;
6745 	enum wmi_phy_mode peer_phymode;
6746 	struct ath12k_link_sta *arsta;
6747 	struct ieee80211_vif *vif;
6748 
6749 	lockdep_assert_wiphy(wiphy);
6750 
6751 	arsta = container_of(wk, struct ath12k_link_sta, update_wk);
6752 	sta = ath12k_ahsta_to_sta(arsta->ahsta);
6753 	arvif = arsta->arvif;
6754 	vif = ath12k_ahvif_to_vif(arvif->ahvif);
6755 	ar = arvif->ar;
6756 
6757 	if (WARN_ON(ath12k_mac_vif_link_chan(vif, arvif->link_id, &def)))
6758 		return;
6759 
6760 	band = def.chan->band;
6761 	ht_mcs_mask = arvif->bitrate_mask.control[band].ht_mcs;
6762 	vht_mcs_mask = arvif->bitrate_mask.control[band].vht_mcs;
6763 	he_mcs_mask = arvif->bitrate_mask.control[band].he_mcs;
6764 	eht_mcs_mask = arvif->bitrate_mask.control[band].eht_mcs;
6765 
6766 	spin_lock_bh(&ar->data_lock);
6767 
6768 	changed = arsta->changed;
6769 	arsta->changed = 0;
6770 
6771 	bw = arsta->bw;
6772 	bw_prev = arsta->bw_prev;
6773 	nss = arsta->nss;
6774 	smps = arsta->smps;
6775 
6776 	spin_unlock_bh(&ar->data_lock);
6777 
6778 	nss = max_t(u32, 1, nss);
6779 	mac_nss = max3(ath12k_mac_max_ht_nss(ht_mcs_mask),
6780 		       ath12k_mac_max_vht_nss(vht_mcs_mask),
6781 		       ath12k_mac_max_he_nss(he_mcs_mask));
6782 	mac_nss = max(mac_nss, ath12k_mac_max_eht_nss(eht_mcs_mask));
6783 	nss = min(nss, mac_nss);
6784 
6785 	struct ath12k_wmi_peer_assoc_arg *peer_arg __free(kfree) =
6786 					kzalloc_obj(*peer_arg);
6787 	if (!peer_arg)
6788 		return;
6789 
6790 	if (changed & IEEE80211_RC_BW_CHANGED) {
6791 		ath12k_peer_assoc_h_phymode(ar, arvif, arsta, peer_arg);
6792 		peer_phymode = peer_arg->peer_phymode;
6793 
6794 		if (bw > bw_prev) {
6795 			/* Phymode shows maximum supported channel width, if we
6796 			 * upgrade bandwidth then due to sanity check of firmware,
6797 			 * we have to send WMI_PEER_PHYMODE followed by
6798 			 * WMI_PEER_CHWIDTH
6799 			 */
6800 			ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac bandwidth upgrade for sta %pM new %d old %d\n",
6801 				   arsta->addr, bw, bw_prev);
6802 			err = ath12k_wmi_set_peer_param(ar, arsta->addr,
6803 							arvif->vdev_id, WMI_PEER_PHYMODE,
6804 							peer_phymode);
6805 			if (err) {
6806 				ath12k_warn(ar->ab, "failed to update STA %pM to peer phymode %d: %d\n",
6807 					    arsta->addr, peer_phymode, err);
6808 				return;
6809 			}
6810 			err = ath12k_wmi_set_peer_param(ar, arsta->addr,
6811 							arvif->vdev_id, WMI_PEER_CHWIDTH,
6812 							bw);
6813 			if (err)
6814 				ath12k_warn(ar->ab, "failed to update STA %pM to peer bandwidth %d: %d\n",
6815 					    arsta->addr, bw, err);
6816 		} else {
6817 			/* When we downgrade bandwidth this will conflict with phymode
6818 			 * and cause to trigger firmware crash. In this case we send
6819 			 * WMI_PEER_CHWIDTH followed by WMI_PEER_PHYMODE
6820 			 */
6821 			ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac bandwidth downgrade for sta %pM new %d old %d\n",
6822 				   arsta->addr, bw, bw_prev);
6823 			err = ath12k_wmi_set_peer_param(ar, arsta->addr,
6824 							arvif->vdev_id, WMI_PEER_CHWIDTH,
6825 							bw);
6826 			if (err) {
6827 				ath12k_warn(ar->ab, "failed to update STA %pM peer to bandwidth %d: %d\n",
6828 					    arsta->addr, bw, err);
6829 				return;
6830 			}
6831 			err = ath12k_wmi_set_peer_param(ar, arsta->addr,
6832 							arvif->vdev_id, WMI_PEER_PHYMODE,
6833 							peer_phymode);
6834 			if (err)
6835 				ath12k_warn(ar->ab, "failed to update STA %pM to peer phymode %d: %d\n",
6836 					    arsta->addr, peer_phymode, err);
6837 		}
6838 	}
6839 
6840 	if (changed & IEEE80211_RC_NSS_CHANGED) {
6841 		ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac update sta %pM nss %d\n",
6842 			   arsta->addr, nss);
6843 
6844 		err = ath12k_wmi_set_peer_param(ar, arsta->addr, arvif->vdev_id,
6845 						WMI_PEER_NSS, nss);
6846 		if (err)
6847 			ath12k_warn(ar->ab, "failed to update STA %pM nss %d: %d\n",
6848 				    arsta->addr, nss, err);
6849 	}
6850 
6851 	if (changed & IEEE80211_RC_SMPS_CHANGED) {
6852 		ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac update sta %pM smps %d\n",
6853 			   arsta->addr, smps);
6854 
6855 		err = ath12k_wmi_set_peer_param(ar, arsta->addr, arvif->vdev_id,
6856 						WMI_PEER_MIMO_PS_STATE, smps);
6857 		if (err)
6858 			ath12k_warn(ar->ab, "failed to update STA %pM smps %d: %d\n",
6859 				    arsta->addr, smps, err);
6860 	}
6861 
6862 	if (changed & IEEE80211_RC_SUPP_RATES_CHANGED) {
6863 		mask = &arvif->bitrate_mask;
6864 		num_vht_rates = ath12k_mac_bitrate_mask_num_vht_rates(ar, band,
6865 								      mask);
6866 		num_he_rates = ath12k_mac_bitrate_mask_num_he_rates(ar, band,
6867 								    mask);
6868 		num_eht_rates = ath12k_mac_bitrate_mask_num_eht_rates(ar, band,
6869 								      mask);
6870 
6871 		/* Peer_assoc_prepare will reject vht rates in
6872 		 * bitrate_mask if its not available in range format and
6873 		 * sets vht tx_rateset as unsupported. So multiple VHT MCS
6874 		 * setting(eg. MCS 4,5,6) per peer is not supported here.
6875 		 * But, Single rate in VHT mask can be set as per-peer
6876 		 * fixed rate. But even if any HT rates are configured in
6877 		 * the bitrate mask, device will not switch to those rates
6878 		 * when per-peer Fixed rate is set.
6879 		 * TODO: Check RATEMASK_CMDID to support auto rates selection
6880 		 * across HT/VHT and for multiple VHT MCS support.
6881 		 */
6882 		link_sta = ath12k_mac_get_link_sta(arsta);
6883 		if (!link_sta) {
6884 			ath12k_warn(ar->ab, "unable to access link sta in peer assoc he for sta %pM link %u\n",
6885 				    sta->addr, arsta->link_id);
6886 			return;
6887 		}
6888 
6889 		if (link_sta->vht_cap.vht_supported && num_vht_rates == 1) {
6890 			ath12k_mac_set_peer_vht_fixed_rate(arvif, arsta, mask,
6891 							   band);
6892 		} else if (link_sta->he_cap.has_he && num_he_rates == 1) {
6893 			ath12k_mac_set_peer_he_fixed_rate(arvif, arsta, mask, band);
6894 		} else if (link_sta->eht_cap.has_eht && num_eht_rates == 1) {
6895 			err = ath12k_mac_set_peer_eht_fixed_rate(arvif, arsta,
6896 								 mask, band);
6897 			if (err) {
6898 				ath12k_warn(ar->ab,
6899 					    "failed to set peer EHT fixed rate for STA %pM ret %d\n",
6900 					    arsta->addr, err);
6901 				return;
6902 			}
6903 		} else {
6904 			/* If the peer is non-VHT/HE/EHT or no fixed VHT/HE/EHT
6905 			 * rate is provided in the new bitrate mask we set the
6906 			 * other rates using peer_assoc command. Also clear
6907 			 * the peer fixed rate settings as it has higher proprity
6908 			 * than peer assoc
6909 			 */
6910 			err = ath12k_wmi_set_peer_param(ar, arsta->addr,
6911 							arvif->vdev_id,
6912 							WMI_PEER_PARAM_FIXED_RATE,
6913 							WMI_FIXED_RATE_NONE);
6914 			if (err)
6915 				ath12k_warn(ar->ab,
6916 					    "failed to disable peer fixed rate for STA %pM ret %d\n",
6917 					    arsta->addr, err);
6918 
6919 			ath12k_peer_assoc_prepare(ar, arvif, arsta,
6920 						  peer_arg, true);
6921 
6922 			peer_arg->is_assoc = false;
6923 
6924 			ath12k_mac_peer_assoc(ar, peer_arg);
6925 		}
6926 	}
6927 }
6928 
6929 static void ath12k_mac_free_unassign_link_sta(struct ath12k_hw *ah,
6930 					      struct ath12k_sta *ahsta,
6931 					      u8 link_id)
6932 {
6933 	struct ath12k_link_sta *arsta;
6934 
6935 	lockdep_assert_wiphy(ah->hw->wiphy);
6936 
6937 	if (WARN_ON(link_id >= IEEE80211_MLD_MAX_NUM_LINKS))
6938 		return;
6939 
6940 	arsta = wiphy_dereference(ah->hw->wiphy, ahsta->link[link_id]);
6941 	if (WARN_ON(!arsta))
6942 		return;
6943 
6944 	ahsta->links_map &= ~BIT(link_id);
6945 	ahsta->free_logical_link_idx_map |= BIT(arsta->link_idx);
6946 
6947 	rcu_assign_pointer(ahsta->link[link_id], NULL);
6948 	synchronize_rcu();
6949 
6950 	if (arsta == &ahsta->deflink) {
6951 		arsta->link_id = ATH12K_INVALID_LINK_ID;
6952 		arsta->ahsta = NULL;
6953 		arsta->arvif = NULL;
6954 		return;
6955 	}
6956 
6957 	kfree(arsta);
6958 }
6959 
6960 static int ath12k_mac_inc_num_stations(struct ath12k_link_vif *arvif,
6961 				       struct ath12k_link_sta *arsta)
6962 {
6963 	struct ieee80211_sta *sta = ath12k_ahsta_to_sta(arsta->ahsta);
6964 	struct ath12k *ar = arvif->ar;
6965 
6966 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
6967 
6968 	if (arvif->ahvif->vdev_type == WMI_VDEV_TYPE_STA && !sta->tdls)
6969 		return 0;
6970 
6971 	if (ar->num_stations >= ar->max_num_stations)
6972 		return -ENOBUFS;
6973 
6974 	ar->num_stations++;
6975 	arvif->num_stations++;
6976 
6977 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
6978 		   "mac station %pM connected to vdev %u num_stations %u\n",
6979 		   arsta->addr, arvif->vdev_id, arvif->num_stations);
6980 
6981 	return 0;
6982 }
6983 
6984 static void ath12k_mac_dec_num_stations(struct ath12k_link_vif *arvif,
6985 					struct ath12k_link_sta *arsta)
6986 {
6987 	struct ieee80211_sta *sta = ath12k_ahsta_to_sta(arsta->ahsta);
6988 	struct ath12k *ar = arvif->ar;
6989 
6990 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
6991 
6992 	if (arvif->ahvif->vdev_type == WMI_VDEV_TYPE_STA && !sta->tdls)
6993 		return;
6994 
6995 	ar->num_stations--;
6996 
6997 	if (arvif->num_stations) {
6998 		arvif->num_stations--;
6999 		ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
7000 			   "mac station %pM disconnected from vdev %u num_stations %u\n",
7001 			   arsta->addr, arvif->vdev_id, arvif->num_stations);
7002 	} else {
7003 		ath12k_warn(ar->ab,
7004 			    "mac station %pM disconnect for vdev %u without any connected station\n",
7005 			    arsta->addr, arvif->vdev_id);
7006 	}
7007 }
7008 
7009 static void ath12k_mac_station_post_remove(struct ath12k *ar,
7010 					   struct ath12k_link_vif *arvif,
7011 					   struct ath12k_link_sta *arsta)
7012 {
7013 	struct ieee80211_vif *vif = ath12k_ahvif_to_vif(arvif->ahvif);
7014 	struct ieee80211_sta *sta = ath12k_ahsta_to_sta(arsta->ahsta);
7015 	struct ath12k_dp_link_peer *peer;
7016 	struct ath12k_dp *dp = ath12k_ab_to_dp(ar->ab);
7017 
7018 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
7019 
7020 	ath12k_mac_dec_num_stations(arvif, arsta);
7021 
7022 	spin_lock_bh(&dp->dp_lock);
7023 
7024 	peer = ath12k_dp_link_peer_find_by_vdev_and_addr(dp, arvif->vdev_id,
7025 							 arsta->addr);
7026 	if (peer && peer->sta == sta) {
7027 		ath12k_warn(ar->ab, "Found peer entry %pM n vdev %i after it was supposedly removed\n",
7028 			    vif->addr, arvif->vdev_id);
7029 		peer->sta = NULL;
7030 
7031 		ath12k_dp_link_peer_free(peer);
7032 		ar->num_peers--;
7033 	}
7034 
7035 	spin_unlock_bh(&dp->dp_lock);
7036 }
7037 
7038 static int ath12k_mac_station_unauthorize(struct ath12k *ar,
7039 					  struct ath12k_link_vif *arvif,
7040 					  struct ath12k_link_sta *arsta)
7041 {
7042 	struct ath12k_dp_link_peer *peer;
7043 	int ret;
7044 	struct ath12k_dp *dp = ath12k_ab_to_dp(ar->ab);
7045 
7046 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
7047 
7048 	spin_lock_bh(&dp->dp_lock);
7049 
7050 	peer = ath12k_dp_link_peer_find_by_vdev_and_addr(dp, arvif->vdev_id,
7051 							 arsta->addr);
7052 	if (peer)
7053 		peer->is_authorized = false;
7054 
7055 	spin_unlock_bh(&dp->dp_lock);
7056 
7057 	/* Driver must clear the keys during the state change from
7058 	 * IEEE80211_STA_AUTHORIZED to IEEE80211_STA_ASSOC, since after
7059 	 * returning from here, mac80211 is going to delete the keys
7060 	 * in __sta_info_destroy_part2(). This will ensure that the driver does
7061 	 * not retain stale key references after mac80211 deletes the keys.
7062 	 */
7063 	ret = ath12k_clear_peer_keys(arvif, arsta->addr);
7064 	if (ret) {
7065 		ath12k_warn(ar->ab, "failed to clear all peer keys for vdev %i: %d\n",
7066 			    arvif->vdev_id, ret);
7067 		return ret;
7068 	}
7069 
7070 	return 0;
7071 }
7072 
7073 static int ath12k_mac_station_authorize(struct ath12k *ar,
7074 					struct ath12k_link_vif *arvif,
7075 					struct ath12k_link_sta *arsta)
7076 {
7077 	struct ath12k_dp_link_peer *peer;
7078 	struct ieee80211_vif *vif = ath12k_ahvif_to_vif(arvif->ahvif);
7079 	int ret;
7080 	struct ath12k_dp *dp = ath12k_ab_to_dp(ar->ab);
7081 
7082 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
7083 
7084 	spin_lock_bh(&dp->dp_lock);
7085 
7086 	peer = ath12k_dp_link_peer_find_by_vdev_and_addr(dp, arvif->vdev_id,
7087 							 arsta->addr);
7088 	if (peer)
7089 		peer->is_authorized = true;
7090 
7091 	spin_unlock_bh(&dp->dp_lock);
7092 
7093 	if (vif->type == NL80211_IFTYPE_STATION && arvif->is_up) {
7094 		ret = ath12k_wmi_set_peer_param(ar, arsta->addr,
7095 						arvif->vdev_id,
7096 						WMI_PEER_AUTHORIZE,
7097 						1);
7098 		if (ret) {
7099 			ath12k_warn(ar->ab, "Unable to authorize peer %pM vdev %d: %d\n",
7100 				    arsta->addr, arvif->vdev_id, ret);
7101 			return ret;
7102 		}
7103 	}
7104 
7105 	return 0;
7106 }
7107 
7108 static int ath12k_mac_station_remove(struct ath12k *ar,
7109 				     struct ath12k_link_vif *arvif,
7110 				     struct ath12k_link_sta *arsta)
7111 {
7112 	struct ieee80211_sta *sta = ath12k_ahsta_to_sta(arsta->ahsta);
7113 	struct ath12k_vif *ahvif = arvif->ahvif;
7114 	int ret = 0;
7115 	struct ath12k_link_sta *temp_arsta;
7116 
7117 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
7118 
7119 	wiphy_work_cancel(ar->ah->hw->wiphy, &arsta->update_wk);
7120 
7121 	if (ahvif->vdev_type == WMI_VDEV_TYPE_STA) {
7122 		ath12k_bss_disassoc(ar, arvif);
7123 		ret = ath12k_mac_vdev_stop(arvif);
7124 		if (ret)
7125 			ath12k_warn(ar->ab, "failed to stop vdev %i: %d\n",
7126 				    arvif->vdev_id, ret);
7127 	}
7128 
7129 	if (sta->mlo)
7130 		return ret;
7131 
7132 	ath12k_dp_peer_cleanup(ar, arvif->vdev_id, arsta->addr);
7133 
7134 	ret = ath12k_peer_delete(ar, arvif->vdev_id, arsta->addr);
7135 	if (ret)
7136 		ath12k_warn(ar->ab, "Failed to delete peer: %pM for VDEV: %d\n",
7137 			    arsta->addr, arvif->vdev_id);
7138 	else
7139 		ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "Removed peer: %pM for VDEV: %d\n",
7140 			   arsta->addr, arvif->vdev_id);
7141 
7142 	ath12k_mac_station_post_remove(ar, arvif, arsta);
7143 
7144 	spin_lock_bh(&ar->ab->base_lock);
7145 
7146 	/* To handle roaming and split phy scenario */
7147 	temp_arsta = ath12k_link_sta_find_by_addr(ar->ab, arsta->addr);
7148 	if (temp_arsta && temp_arsta->arvif->ar == ar)
7149 		ath12k_link_sta_rhash_delete(ar->ab, arsta);
7150 
7151 	spin_unlock_bh(&ar->ab->base_lock);
7152 
7153 	if (sta->valid_links)
7154 		ath12k_mac_free_unassign_link_sta(ahvif->ah,
7155 						  arsta->ahsta, arsta->link_id);
7156 
7157 	return ret;
7158 }
7159 
7160 static int ath12k_mac_station_add(struct ath12k *ar,
7161 				  struct ath12k_link_vif *arvif,
7162 				  struct ath12k_link_sta *arsta)
7163 {
7164 	struct ath12k_base *ab = ar->ab;
7165 	struct ieee80211_vif *vif = ath12k_ahvif_to_vif(arvif->ahvif);
7166 	struct ieee80211_sta *sta = ath12k_ahsta_to_sta(arsta->ahsta);
7167 	struct ath12k_wmi_peer_create_arg peer_param = {};
7168 	int ret;
7169 	struct ath12k_link_sta *temp_arsta;
7170 
7171 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
7172 
7173 	ret = ath12k_mac_inc_num_stations(arvif, arsta);
7174 	if (ret) {
7175 		ath12k_warn(ab, "refusing to associate station: too many connected already (%d)\n",
7176 			    ar->max_num_stations);
7177 		goto exit;
7178 	}
7179 
7180 	spin_lock_bh(&ab->base_lock);
7181 
7182 	/*
7183 	 * In case of Split PHY and roaming scenario, pdev idx
7184 	 * might differ but both the pdev will share same rhash
7185 	 * table. In that case update the rhash table if link_sta is
7186 	 * already present
7187 	 */
7188 	temp_arsta = ath12k_link_sta_find_by_addr(ab, arsta->addr);
7189 	if (temp_arsta && temp_arsta->arvif->ar != ar)
7190 		ath12k_link_sta_rhash_delete(ab, temp_arsta);
7191 
7192 	ret = ath12k_link_sta_rhash_add(ab, arsta);
7193 	spin_unlock_bh(&ab->base_lock);
7194 	if (ret) {
7195 		ath12k_warn(ab, "Failed to add arsta: %pM to hash table, ret: %d",
7196 			    arsta->addr, ret);
7197 		goto dec_num_station;
7198 	}
7199 
7200 	peer_param.vdev_id = arvif->vdev_id;
7201 	peer_param.peer_addr = arsta->addr;
7202 	peer_param.peer_type = WMI_PEER_TYPE_DEFAULT;
7203 	peer_param.ml_enabled = sta->mlo;
7204 
7205 	ret = ath12k_peer_create(ar, arvif, sta, &peer_param);
7206 	if (ret) {
7207 		ath12k_warn(ab, "Failed to add peer: %pM for VDEV: %d\n",
7208 			    arsta->addr, arvif->vdev_id);
7209 		goto free_peer;
7210 	}
7211 
7212 	ath12k_dbg(ab, ATH12K_DBG_MAC, "Added peer: %pM for VDEV: %d\n",
7213 		   arsta->addr, arvif->vdev_id);
7214 
7215 	if (ieee80211_vif_is_mesh(vif)) {
7216 		ret = ath12k_wmi_set_peer_param(ar, arsta->addr,
7217 						arvif->vdev_id,
7218 						WMI_PEER_USE_4ADDR, 1);
7219 		if (ret) {
7220 			ath12k_warn(ab, "failed to STA %pM 4addr capability: %d\n",
7221 				    arsta->addr, ret);
7222 			goto free_peer;
7223 		}
7224 	}
7225 
7226 	ret = ath12k_dp_peer_setup(ar, arvif->vdev_id, arsta->addr);
7227 	if (ret) {
7228 		ath12k_warn(ab, "failed to setup dp for peer %pM on vdev %i (%d)\n",
7229 			    arsta->addr, arvif->vdev_id, ret);
7230 		goto free_peer;
7231 	}
7232 
7233 	if (ab->hw_params->vdev_start_delay &&
7234 	    !arvif->is_started &&
7235 	    arvif->ahvif->vdev_type != WMI_VDEV_TYPE_AP) {
7236 		ret = ath12k_start_vdev_delay(ar, arvif);
7237 		if (ret) {
7238 			ath12k_warn(ab, "failed to delay vdev start: %d\n", ret);
7239 			goto free_peer;
7240 		}
7241 	}
7242 
7243 	return 0;
7244 
7245 free_peer:
7246 	ath12k_peer_delete(ar, arvif->vdev_id, arsta->addr);
7247 	spin_lock_bh(&ab->base_lock);
7248 	ath12k_link_sta_rhash_delete(ab, arsta);
7249 	spin_unlock_bh(&ab->base_lock);
7250 dec_num_station:
7251 	ath12k_mac_dec_num_stations(arvif, arsta);
7252 exit:
7253 	return ret;
7254 }
7255 
7256 static int ath12k_mac_assign_link_sta(struct ath12k_hw *ah,
7257 				      struct ath12k_sta *ahsta,
7258 				      struct ath12k_link_sta *arsta,
7259 				      struct ath12k_vif *ahvif,
7260 				      u8 link_id)
7261 {
7262 	struct ieee80211_sta *sta = ath12k_ahsta_to_sta(ahsta);
7263 	struct ieee80211_link_sta *link_sta;
7264 	struct ath12k_link_vif *arvif;
7265 	int link_idx;
7266 
7267 	lockdep_assert_wiphy(ah->hw->wiphy);
7268 
7269 	if (!arsta || link_id >= IEEE80211_MLD_MAX_NUM_LINKS)
7270 		return -EINVAL;
7271 
7272 	arvif = wiphy_dereference(ah->hw->wiphy, ahvif->link[link_id]);
7273 	if (!arvif)
7274 		return -EINVAL;
7275 
7276 	memset(arsta, 0, sizeof(*arsta));
7277 
7278 	link_sta = wiphy_dereference(ah->hw->wiphy, sta->link[link_id]);
7279 	if (!link_sta)
7280 		return -EINVAL;
7281 
7282 	ether_addr_copy(arsta->addr, link_sta->addr);
7283 
7284 	if (!ahsta->free_logical_link_idx_map)
7285 		return -ENOSPC;
7286 
7287 	/*
7288 	 * Allocate a logical link index by selecting the first available bit
7289 	 * from the free logical index map
7290 	 */
7291 	link_idx = __ffs(ahsta->free_logical_link_idx_map);
7292 	ahsta->free_logical_link_idx_map &= ~BIT(link_idx);
7293 	arsta->link_idx = link_idx;
7294 
7295 	arsta->link_id = link_id;
7296 	ahsta->links_map |= BIT(arsta->link_id);
7297 	arsta->arvif = arvif;
7298 	arsta->ahsta = ahsta;
7299 	ahsta->ahvif = ahvif;
7300 
7301 	wiphy_work_init(&arsta->update_wk, ath12k_sta_rc_update_wk);
7302 
7303 	rcu_assign_pointer(ahsta->link[link_id], arsta);
7304 
7305 	return 0;
7306 }
7307 
7308 static void ath12k_mac_ml_station_remove(struct ath12k_vif *ahvif,
7309 					 struct ath12k_sta *ahsta)
7310 {
7311 	struct ieee80211_sta *sta = ath12k_ahsta_to_sta(ahsta);
7312 	struct ath12k_hw *ah = ahvif->ah;
7313 	struct ath12k_link_vif *arvif;
7314 	struct ath12k_link_sta *arsta;
7315 	unsigned long links;
7316 	struct ath12k *ar;
7317 	u8 link_id;
7318 
7319 	lockdep_assert_wiphy(ah->hw->wiphy);
7320 
7321 	ath12k_peer_mlo_link_peers_delete(ahvif, ahsta);
7322 
7323 	/* validate link station removal and clear arsta links */
7324 	links = ahsta->links_map;
7325 	for_each_set_bit(link_id, &links, IEEE80211_MLD_MAX_NUM_LINKS) {
7326 		arvif = wiphy_dereference(ah->hw->wiphy, ahvif->link[link_id]);
7327 		arsta = wiphy_dereference(ah->hw->wiphy, ahsta->link[link_id]);
7328 		if (!arvif || !arsta)
7329 			continue;
7330 
7331 		ar = arvif->ar;
7332 
7333 		ath12k_mac_station_post_remove(ar, arvif, arsta);
7334 
7335 		spin_lock_bh(&ar->ab->base_lock);
7336 		ath12k_link_sta_rhash_delete(ar->ab, arsta);
7337 		spin_unlock_bh(&ar->ab->base_lock);
7338 
7339 		ath12k_mac_free_unassign_link_sta(ah, ahsta, link_id);
7340 	}
7341 
7342 	if (sta->mlo)
7343 		ath12k_peer_ml_free(ah, ahsta);
7344 }
7345 
7346 static int ath12k_mac_handle_link_sta_state(struct ieee80211_hw *hw,
7347 					    struct ath12k_link_vif *arvif,
7348 					    struct ath12k_link_sta *arsta,
7349 					    enum ieee80211_sta_state old_state,
7350 					    enum ieee80211_sta_state new_state)
7351 {
7352 	struct ieee80211_vif *vif = ath12k_ahvif_to_vif(arvif->ahvif);
7353 	struct ieee80211_bss_conf *link_conf;
7354 	struct ath12k *ar = arvif->ar;
7355 	struct ath12k_reg_info *reg_info;
7356 	struct ath12k_base *ab = ar->ab;
7357 	int ret = 0;
7358 
7359 	lockdep_assert_wiphy(hw->wiphy);
7360 
7361 	ath12k_dbg(ab, ATH12K_DBG_MAC, "mac handle link %u sta %pM state %d -> %d\n",
7362 		   arsta->link_id, arsta->addr, old_state, new_state);
7363 
7364 	/* IEEE80211_STA_NONE -> IEEE80211_STA_NOTEXIST: Remove the station
7365 	 * from driver
7366 	 */
7367 	if ((old_state == IEEE80211_STA_NONE &&
7368 	     new_state == IEEE80211_STA_NOTEXIST)) {
7369 		ret = ath12k_mac_station_remove(ar, arvif, arsta);
7370 		if (ret) {
7371 			ath12k_warn(ab, "Failed to remove station: %pM for VDEV: %d\n",
7372 				    arsta->addr, arvif->vdev_id);
7373 			goto exit;
7374 		}
7375 	}
7376 
7377 	/* IEEE80211_STA_NOTEXIST -> IEEE80211_STA_NONE: Add new station to driver */
7378 	if (old_state == IEEE80211_STA_NOTEXIST &&
7379 	    new_state == IEEE80211_STA_NONE) {
7380 		ret = ath12k_mac_station_add(ar, arvif, arsta);
7381 		if (ret)
7382 			ath12k_warn(ab, "Failed to add station: %pM for VDEV: %d\n",
7383 				    arsta->addr, arvif->vdev_id);
7384 
7385 	/* IEEE80211_STA_AUTH -> IEEE80211_STA_ASSOC: Send station assoc command for
7386 	 * peer associated to AP/Mesh/ADHOC vif type.
7387 	 */
7388 	} else if (old_state == IEEE80211_STA_AUTH &&
7389 		   new_state == IEEE80211_STA_ASSOC &&
7390 		   (vif->type == NL80211_IFTYPE_AP ||
7391 		    vif->type == NL80211_IFTYPE_MESH_POINT ||
7392 		    vif->type == NL80211_IFTYPE_ADHOC)) {
7393 		ret = ath12k_mac_station_assoc(ar, arvif, arsta, false);
7394 		if (ret)
7395 			ath12k_warn(ab, "Failed to associate station: %pM\n",
7396 				    arsta->addr);
7397 
7398 	/* IEEE80211_STA_ASSOC -> IEEE80211_STA_AUTHORIZED: set peer status as
7399 	 * authorized
7400 	 */
7401 	} else if (old_state == IEEE80211_STA_ASSOC &&
7402 		   new_state == IEEE80211_STA_AUTHORIZED) {
7403 		ret = ath12k_mac_station_authorize(ar, arvif, arsta);
7404 		if (ret) {
7405 			ath12k_warn(ab, "Failed to authorize station: %pM\n",
7406 				    arsta->addr);
7407 			goto exit;
7408 		}
7409 
7410 		if (ath12k_wmi_supports_6ghz_cc_ext(ar) &&
7411 		    arvif->ahvif->vdev_type == WMI_VDEV_TYPE_STA) {
7412 			link_conf = ath12k_mac_get_link_bss_conf(arvif);
7413 			reg_info = ab->reg_info[ar->pdev_idx];
7414 			ath12k_dbg(ab, ATH12K_DBG_MAC, "connection done, update reg rules\n");
7415 			ath12k_hw_to_ah(hw)->regd_updated = false;
7416 			ath12k_reg_handle_chan_list(ab, reg_info, arvif->ahvif->vdev_type,
7417 						    link_conf->power_type);
7418 		}
7419 
7420 	/* IEEE80211_STA_AUTHORIZED -> IEEE80211_STA_ASSOC: station may be in removal,
7421 	 * deauthorize it.
7422 	 */
7423 	} else if (old_state == IEEE80211_STA_AUTHORIZED &&
7424 		   new_state == IEEE80211_STA_ASSOC) {
7425 		ath12k_mac_station_unauthorize(ar, arvif, arsta);
7426 
7427 	/* IEEE80211_STA_ASSOC -> IEEE80211_STA_AUTH: disassoc peer connected to
7428 	 * AP/mesh/ADHOC vif type.
7429 	 */
7430 	} else if (old_state == IEEE80211_STA_ASSOC &&
7431 		   new_state == IEEE80211_STA_AUTH &&
7432 		   (vif->type == NL80211_IFTYPE_AP ||
7433 		    vif->type == NL80211_IFTYPE_MESH_POINT ||
7434 		    vif->type == NL80211_IFTYPE_ADHOC)) {
7435 		ret = ath12k_mac_station_disassoc(ar, arvif, arsta);
7436 		if (ret)
7437 			ath12k_warn(ab, "Failed to disassociate station: %pM\n",
7438 				    arsta->addr);
7439 	}
7440 
7441 exit:
7442 	return ret;
7443 }
7444 
7445 static bool ath12k_mac_is_freq_on_mac(struct ath12k_hw_mode_freq_range_arg *freq_range,
7446 				      u32 freq, u8 mac_id)
7447 {
7448 	return (freq >= freq_range[mac_id].low_2ghz_freq &&
7449 		freq <= freq_range[mac_id].high_2ghz_freq) ||
7450 	       (freq >= freq_range[mac_id].low_5ghz_freq &&
7451 		freq <= freq_range[mac_id].high_5ghz_freq);
7452 }
7453 
7454 static bool
7455 ath12k_mac_2_freq_same_mac_in_freq_range(struct ath12k_base *ab,
7456 					 struct ath12k_hw_mode_freq_range_arg *freq_range,
7457 					 u32 freq_link1, u32 freq_link2)
7458 {
7459 	u8 i;
7460 
7461 	for (i = 0; i < MAX_RADIOS; i++) {
7462 		if (ath12k_mac_is_freq_on_mac(freq_range, freq_link1, i) &&
7463 		    ath12k_mac_is_freq_on_mac(freq_range, freq_link2, i))
7464 			return true;
7465 	}
7466 
7467 	return false;
7468 }
7469 
7470 static bool ath12k_mac_is_hw_dbs_capable(struct ath12k_base *ab)
7471 {
7472 	return test_bit(WMI_TLV_SERVICE_DUAL_BAND_SIMULTANEOUS_SUPPORT,
7473 			ab->wmi_ab.svc_map) &&
7474 	       ab->wmi_ab.hw_mode_info.support_dbs;
7475 }
7476 
7477 static bool ath12k_mac_2_freq_same_mac_in_dbs(struct ath12k_base *ab,
7478 					      u32 freq_link1, u32 freq_link2)
7479 {
7480 	struct ath12k_hw_mode_freq_range_arg *freq_range;
7481 
7482 	if (!ath12k_mac_is_hw_dbs_capable(ab))
7483 		return true;
7484 
7485 	freq_range = ab->wmi_ab.hw_mode_info.freq_range_caps[ATH12K_HW_MODE_DBS];
7486 	return ath12k_mac_2_freq_same_mac_in_freq_range(ab, freq_range,
7487 							freq_link1, freq_link2);
7488 }
7489 
7490 static bool ath12k_mac_is_hw_sbs_capable(struct ath12k_base *ab)
7491 {
7492 	return test_bit(WMI_TLV_SERVICE_DUAL_BAND_SIMULTANEOUS_SUPPORT,
7493 			ab->wmi_ab.svc_map) &&
7494 	       ab->wmi_ab.hw_mode_info.support_sbs;
7495 }
7496 
7497 static bool ath12k_mac_2_freq_same_mac_in_sbs(struct ath12k_base *ab,
7498 					      u32 freq_link1, u32 freq_link2)
7499 {
7500 	struct ath12k_hw_mode_info *info = &ab->wmi_ab.hw_mode_info;
7501 	struct ath12k_hw_mode_freq_range_arg *sbs_uppr_share;
7502 	struct ath12k_hw_mode_freq_range_arg *sbs_low_share;
7503 	struct ath12k_hw_mode_freq_range_arg *sbs_range;
7504 
7505 	if (!ath12k_mac_is_hw_sbs_capable(ab))
7506 		return true;
7507 
7508 	if (ab->wmi_ab.sbs_lower_band_end_freq) {
7509 		sbs_uppr_share = info->freq_range_caps[ATH12K_HW_MODE_SBS_UPPER_SHARE];
7510 		sbs_low_share = info->freq_range_caps[ATH12K_HW_MODE_SBS_LOWER_SHARE];
7511 
7512 		return ath12k_mac_2_freq_same_mac_in_freq_range(ab, sbs_low_share,
7513 								freq_link1, freq_link2) ||
7514 		       ath12k_mac_2_freq_same_mac_in_freq_range(ab, sbs_uppr_share,
7515 								freq_link1, freq_link2);
7516 	}
7517 
7518 	sbs_range = info->freq_range_caps[ATH12K_HW_MODE_SBS];
7519 	return ath12k_mac_2_freq_same_mac_in_freq_range(ab, sbs_range,
7520 							freq_link1, freq_link2);
7521 }
7522 
7523 static bool ath12k_mac_freqs_on_same_mac(struct ath12k_base *ab,
7524 					 u32 freq_link1, u32 freq_link2)
7525 {
7526 	return ath12k_mac_2_freq_same_mac_in_dbs(ab, freq_link1, freq_link2) &&
7527 	       ath12k_mac_2_freq_same_mac_in_sbs(ab, freq_link1, freq_link2);
7528 }
7529 
7530 static int ath12k_mac_mlo_sta_set_link_active(struct ath12k_base *ab,
7531 					      enum wmi_mlo_link_force_reason reason,
7532 					      enum wmi_mlo_link_force_mode mode,
7533 					      u8 *mlo_vdev_id_lst,
7534 					      u8 num_mlo_vdev,
7535 					      u8 *mlo_inactive_vdev_lst,
7536 					      u8 num_mlo_inactive_vdev)
7537 {
7538 	struct wmi_mlo_link_set_active_arg param = {};
7539 	u32 entry_idx, entry_offset, vdev_idx;
7540 	u8 vdev_id;
7541 
7542 	param.reason = reason;
7543 	param.force_mode = mode;
7544 
7545 	for (vdev_idx = 0; vdev_idx < num_mlo_vdev; vdev_idx++) {
7546 		vdev_id = mlo_vdev_id_lst[vdev_idx];
7547 		entry_idx = vdev_id / 32;
7548 		entry_offset = vdev_id % 32;
7549 		if (entry_idx >= WMI_MLO_LINK_NUM_SZ) {
7550 			ath12k_warn(ab, "Invalid entry_idx %d num_mlo_vdev %d vdev %d",
7551 				    entry_idx, num_mlo_vdev, vdev_id);
7552 			return -EINVAL;
7553 		}
7554 		param.vdev_bitmap[entry_idx] |= 1 << entry_offset;
7555 		/* update entry number if entry index changed */
7556 		if (param.num_vdev_bitmap < entry_idx + 1)
7557 			param.num_vdev_bitmap = entry_idx + 1;
7558 	}
7559 
7560 	ath12k_dbg(ab, ATH12K_DBG_MAC,
7561 		   "num_vdev_bitmap %d vdev_bitmap[0] = 0x%x, vdev_bitmap[1] = 0x%x",
7562 		   param.num_vdev_bitmap, param.vdev_bitmap[0], param.vdev_bitmap[1]);
7563 
7564 	if (mode == WMI_MLO_LINK_FORCE_MODE_ACTIVE_INACTIVE) {
7565 		for (vdev_idx = 0; vdev_idx < num_mlo_inactive_vdev; vdev_idx++) {
7566 			vdev_id = mlo_inactive_vdev_lst[vdev_idx];
7567 			entry_idx = vdev_id / 32;
7568 			entry_offset = vdev_id % 32;
7569 			if (entry_idx >= WMI_MLO_LINK_NUM_SZ) {
7570 				ath12k_warn(ab, "Invalid entry_idx %d num_mlo_vdev %d vdev %d",
7571 					    entry_idx, num_mlo_inactive_vdev, vdev_id);
7572 				return -EINVAL;
7573 			}
7574 			param.inactive_vdev_bitmap[entry_idx] |= 1 << entry_offset;
7575 			/* update entry number if entry index changed */
7576 			if (param.num_inactive_vdev_bitmap < entry_idx + 1)
7577 				param.num_inactive_vdev_bitmap = entry_idx + 1;
7578 		}
7579 
7580 		ath12k_dbg(ab, ATH12K_DBG_MAC,
7581 			   "num_vdev_bitmap %d inactive_vdev_bitmap[0] = 0x%x, inactive_vdev_bitmap[1] = 0x%x",
7582 			   param.num_inactive_vdev_bitmap,
7583 			   param.inactive_vdev_bitmap[0],
7584 			   param.inactive_vdev_bitmap[1]);
7585 	}
7586 
7587 	if (mode == WMI_MLO_LINK_FORCE_MODE_ACTIVE_LINK_NUM ||
7588 	    mode == WMI_MLO_LINK_FORCE_MODE_INACTIVE_LINK_NUM) {
7589 		param.num_link_entry = 1;
7590 		param.link_num[0].num_of_link = num_mlo_vdev - 1;
7591 	}
7592 
7593 	return ath12k_wmi_send_mlo_link_set_active_cmd(ab, &param);
7594 }
7595 
7596 static int ath12k_mac_mlo_sta_update_link_active(struct ath12k_base *ab,
7597 						 struct ieee80211_hw *hw,
7598 						 struct ath12k_vif *ahvif)
7599 {
7600 	u8 mlo_vdev_id_lst[IEEE80211_MLD_MAX_NUM_LINKS] = {};
7601 	u32 mlo_freq_list[IEEE80211_MLD_MAX_NUM_LINKS] = {};
7602 	unsigned long links = ahvif->links_map;
7603 	enum wmi_mlo_link_force_reason reason;
7604 	struct ieee80211_chanctx_conf *conf;
7605 	enum wmi_mlo_link_force_mode mode;
7606 	struct ieee80211_bss_conf *info;
7607 	struct ath12k_link_vif *arvif;
7608 	u8 num_mlo_vdev = 0;
7609 	u8 link_id;
7610 
7611 	for_each_set_bit(link_id, &links, IEEE80211_MLD_MAX_NUM_LINKS) {
7612 		arvif = wiphy_dereference(hw->wiphy, ahvif->link[link_id]);
7613 		/* make sure vdev is created on this device */
7614 		if (!arvif || !arvif->is_created || arvif->ar->ab != ab)
7615 			continue;
7616 
7617 		info = ath12k_mac_get_link_bss_conf(arvif);
7618 		conf = wiphy_dereference(hw->wiphy, info->chanctx_conf);
7619 		mlo_freq_list[num_mlo_vdev] = conf->def.chan->center_freq;
7620 
7621 		mlo_vdev_id_lst[num_mlo_vdev] = arvif->vdev_id;
7622 		num_mlo_vdev++;
7623 	}
7624 
7625 	/* It is not allowed to activate more links than a single device
7626 	 * supported. Something goes wrong if we reach here.
7627 	 */
7628 	if (num_mlo_vdev > ATH12K_NUM_MAX_ACTIVE_LINKS_PER_DEVICE) {
7629 		WARN_ON_ONCE(1);
7630 		return -EINVAL;
7631 	}
7632 
7633 	/* if 2 links are established and both link channels fall on the
7634 	 * same hardware MAC, send command to firmware to deactivate one
7635 	 * of them.
7636 	 */
7637 	if (num_mlo_vdev == 2 &&
7638 	    ath12k_mac_freqs_on_same_mac(ab, mlo_freq_list[0],
7639 					 mlo_freq_list[1])) {
7640 		mode = WMI_MLO_LINK_FORCE_MODE_INACTIVE_LINK_NUM;
7641 		reason = WMI_MLO_LINK_FORCE_REASON_NEW_CONNECT;
7642 		return ath12k_mac_mlo_sta_set_link_active(ab, reason, mode,
7643 							  mlo_vdev_id_lst, num_mlo_vdev,
7644 							  NULL, 0);
7645 	}
7646 
7647 	return 0;
7648 }
7649 
7650 static bool ath12k_mac_are_sbs_chan(struct ath12k_base *ab, u32 freq_1, u32 freq_2)
7651 {
7652 	if (!ath12k_mac_is_hw_sbs_capable(ab))
7653 		return false;
7654 
7655 	if (ath12k_is_2ghz_channel_freq(freq_1) ||
7656 	    ath12k_is_2ghz_channel_freq(freq_2))
7657 		return false;
7658 
7659 	return !ath12k_mac_2_freq_same_mac_in_sbs(ab, freq_1, freq_2);
7660 }
7661 
7662 static bool ath12k_mac_are_dbs_chan(struct ath12k_base *ab, u32 freq_1, u32 freq_2)
7663 {
7664 	if (!ath12k_mac_is_hw_dbs_capable(ab))
7665 		return false;
7666 
7667 	return !ath12k_mac_2_freq_same_mac_in_dbs(ab, freq_1, freq_2);
7668 }
7669 
7670 static int ath12k_mac_select_links(struct ath12k_base *ab,
7671 				   struct ieee80211_vif *vif,
7672 				   struct ieee80211_hw *hw,
7673 				   u16 *selected_links)
7674 {
7675 	unsigned long useful_links = ieee80211_vif_usable_links(vif);
7676 	struct ath12k_vif *ahvif = ath12k_vif_to_ahvif(vif);
7677 	u8 num_useful_links = hweight_long(useful_links);
7678 	struct ieee80211_chanctx_conf *chanctx;
7679 	struct ath12k_link_vif *assoc_arvif;
7680 	u32 assoc_link_freq, partner_freq;
7681 	u16 sbs_links = 0, dbs_links = 0;
7682 	struct ieee80211_bss_conf *info;
7683 	struct ieee80211_channel *chan;
7684 	struct ieee80211_sta *sta;
7685 	struct ath12k_sta *ahsta;
7686 	u8 link_id;
7687 
7688 	/* activate all useful links if less than max supported */
7689 	if (num_useful_links <= ATH12K_NUM_MAX_ACTIVE_LINKS_PER_DEVICE) {
7690 		*selected_links = useful_links;
7691 		return 0;
7692 	}
7693 
7694 	/* only in station mode we can get here, so it's safe
7695 	 * to use ap_addr
7696 	 */
7697 	rcu_read_lock();
7698 	sta = ieee80211_find_sta(vif, vif->cfg.ap_addr);
7699 	if (!sta) {
7700 		rcu_read_unlock();
7701 		ath12k_warn(ab, "failed to find sta with addr %pM\n", vif->cfg.ap_addr);
7702 		return -EINVAL;
7703 	}
7704 
7705 	ahsta = ath12k_sta_to_ahsta(sta);
7706 	assoc_arvif = wiphy_dereference(hw->wiphy, ahvif->link[ahsta->assoc_link_id]);
7707 	info = ath12k_mac_get_link_bss_conf(assoc_arvif);
7708 	chanctx = rcu_dereference(info->chanctx_conf);
7709 	assoc_link_freq = chanctx->def.chan->center_freq;
7710 	rcu_read_unlock();
7711 	ath12k_dbg(ab, ATH12K_DBG_MAC, "assoc link %u freq %u\n",
7712 		   assoc_arvif->link_id, assoc_link_freq);
7713 
7714 	/* assoc link is already activated and has to be kept active,
7715 	 * only need to select a partner link from others.
7716 	 */
7717 	useful_links &= ~BIT(assoc_arvif->link_id);
7718 	for_each_set_bit(link_id, &useful_links, IEEE80211_MLD_MAX_NUM_LINKS) {
7719 		info = wiphy_dereference(hw->wiphy, vif->link_conf[link_id]);
7720 		if (!info) {
7721 			ath12k_warn(ab, "failed to get link info for link: %u\n",
7722 				    link_id);
7723 			return -ENOLINK;
7724 		}
7725 
7726 		chan = info->chanreq.oper.chan;
7727 		if (!chan) {
7728 			ath12k_warn(ab, "failed to get chan for link: %u\n", link_id);
7729 			return -EINVAL;
7730 		}
7731 
7732 		partner_freq = chan->center_freq;
7733 		if (ath12k_mac_are_sbs_chan(ab, assoc_link_freq, partner_freq)) {
7734 			sbs_links |= BIT(link_id);
7735 			ath12k_dbg(ab, ATH12K_DBG_MAC, "new SBS link %u freq %u\n",
7736 				   link_id, partner_freq);
7737 			continue;
7738 		}
7739 
7740 		if (ath12k_mac_are_dbs_chan(ab, assoc_link_freq, partner_freq)) {
7741 			dbs_links |= BIT(link_id);
7742 			ath12k_dbg(ab, ATH12K_DBG_MAC, "new DBS link %u freq %u\n",
7743 				   link_id, partner_freq);
7744 			continue;
7745 		}
7746 
7747 		ath12k_dbg(ab, ATH12K_DBG_MAC, "non DBS/SBS link %u freq %u\n",
7748 			   link_id, partner_freq);
7749 	}
7750 
7751 	/* choose the first candidate no matter how many is in the list */
7752 	if (sbs_links)
7753 		link_id = __ffs(sbs_links);
7754 	else if (dbs_links)
7755 		link_id = __ffs(dbs_links);
7756 	else
7757 		link_id = ffs(useful_links) - 1;
7758 
7759 	ath12k_dbg(ab, ATH12K_DBG_MAC, "select partner link %u\n", link_id);
7760 
7761 	*selected_links = BIT(assoc_arvif->link_id) | BIT(link_id);
7762 
7763 	return 0;
7764 }
7765 
7766 int ath12k_mac_op_sta_state(struct ieee80211_hw *hw,
7767 			    struct ieee80211_vif *vif,
7768 			    struct ieee80211_sta *sta,
7769 			    enum ieee80211_sta_state old_state,
7770 			    enum ieee80211_sta_state new_state)
7771 {
7772 	struct ath12k_vif *ahvif = ath12k_vif_to_ahvif(vif);
7773 	struct ath12k_sta *ahsta = ath12k_sta_to_ahsta(sta);
7774 	struct ath12k_hw *ah = ath12k_hw_to_ah(hw);
7775 	struct ath12k_base *prev_ab = NULL, *ab;
7776 	struct ath12k_link_vif *arvif;
7777 	struct ath12k_link_sta *arsta;
7778 	unsigned long valid_links;
7779 	u16 selected_links = 0;
7780 	u8 link_id = 0, i;
7781 	struct ath12k *ar;
7782 	int ret = -EINVAL;
7783 	struct ath12k_dp_peer_create_params dp_params = {};
7784 
7785 	lockdep_assert_wiphy(hw->wiphy);
7786 
7787 	if (ieee80211_vif_is_mld(vif) && sta->valid_links) {
7788 		WARN_ON(!sta->mlo && hweight16(sta->valid_links) != 1);
7789 		link_id = ffs(sta->valid_links) - 1;
7790 	}
7791 
7792 	/* IEEE80211_STA_NOTEXIST -> IEEE80211_STA_NONE:
7793 	 * New station add received. If this is a ML station then
7794 	 * ahsta->links_map will be zero and sta->valid_links will be 1.
7795 	 * Assign default link to the first link sta.
7796 	 */
7797 	if (old_state == IEEE80211_STA_NOTEXIST &&
7798 	    new_state == IEEE80211_STA_NONE) {
7799 		memset(ahsta, 0, sizeof(*ahsta));
7800 		ahsta->free_logical_link_idx_map = U16_MAX;
7801 
7802 		arsta = &ahsta->deflink;
7803 
7804 		/* ML sta */
7805 		if (sta->mlo && !ahsta->links_map &&
7806 		    (hweight16(sta->valid_links) == 1)) {
7807 			if (ah->host_alloc_ml_id) {
7808 				ahsta->ml_peer_id = ath12k_peer_ml_alloc(ah);
7809 				if (ahsta->ml_peer_id == ATH12K_MLO_PEER_ID_INVALID) {
7810 					ath12k_hw_warn(ah, "unable to allocate ML peer id for sta %pM",
7811 						       sta->addr);
7812 					goto exit;
7813 				}
7814 			} else {
7815 				/*
7816 				 * firmware allocates the ML peer ID and notifies
7817 				 * the host via HTT_T2H_MSG_TYPE_MLO_RX_PEER_MAP
7818 				 */
7819 				ahsta->ml_peer_id = ATH12K_MLO_PEER_ID_PENDING;
7820 			}
7821 
7822 			dp_params.is_mlo = true;
7823 			dp_params.peer_id = ahsta->ml_peer_id;
7824 		}
7825 
7826 		dp_params.sta = sta;
7827 
7828 		if (vif->type == NL80211_IFTYPE_AP)
7829 			dp_params.ucast_ra_only = true;
7830 
7831 		ret = ath12k_dp_peer_create(&ah->dp_hw, sta->addr, &dp_params);
7832 		if (ret) {
7833 			ath12k_hw_warn(ah, "unable to create ath12k_dp_peer for sta %pM, ret: %d",
7834 				       sta->addr, ret);
7835 
7836 			goto ml_peer_id_clear;
7837 		}
7838 
7839 		ret = ath12k_mac_assign_link_sta(ah, ahsta, arsta, ahvif,
7840 						 link_id);
7841 		if (ret) {
7842 			ath12k_hw_warn(ah, "unable assign link %d for sta %pM",
7843 				       link_id, sta->addr);
7844 			goto peer_delete;
7845 		}
7846 
7847 		/* above arsta will get memset, hence do this after assign
7848 		 * link sta
7849 		 */
7850 		if (sta->mlo) {
7851 			/* For station mode, arvif->is_sta_assoc_link has been set when
7852 			 * vdev starts. Make sure the arvif/arsta pair have same setting
7853 			 */
7854 			if (vif->type == NL80211_IFTYPE_STATION &&
7855 			    !arsta->arvif->is_sta_assoc_link) {
7856 				ath12k_hw_warn(ah, "failed to verify assoc link setting with link id %u\n",
7857 					       link_id);
7858 				ret = -EINVAL;
7859 				goto free_unassign_link;
7860 			}
7861 
7862 			arsta->is_assoc_link = true;
7863 			ahsta->assoc_link_id = link_id;
7864 		}
7865 	}
7866 
7867 	/* In the ML station scenario, activate all partner links once the
7868 	 * client is transitioning to the associated state.
7869 	 *
7870 	 * FIXME: Ideally, this activation should occur when the client
7871 	 * transitions to the authorized state. However, there are some
7872 	 * issues with handling this in the firmware. Until the firmware
7873 	 * can manage it properly, activate the links when the client is
7874 	 * about to move to the associated state.
7875 	 */
7876 	if (ieee80211_vif_is_mld(vif) && vif->type == NL80211_IFTYPE_STATION &&
7877 	    old_state == IEEE80211_STA_AUTH && new_state == IEEE80211_STA_ASSOC) {
7878 		/* TODO: for now only do link selection for single device
7879 		 * MLO case. Other cases would be handled in the future.
7880 		 */
7881 		ab = ah->radio[0].ab;
7882 		if (ab->ag->num_devices == 1) {
7883 			ret = ath12k_mac_select_links(ab, vif, hw, &selected_links);
7884 			if (ret) {
7885 				ath12k_warn(ab,
7886 					    "failed to get selected links: %d\n", ret);
7887 				goto exit;
7888 			}
7889 		} else {
7890 			selected_links = ieee80211_vif_usable_links(vif);
7891 		}
7892 
7893 		ieee80211_set_active_links(vif, selected_links);
7894 	}
7895 
7896 	/* Handle all the other state transitions in generic way */
7897 	valid_links = ahsta->links_map;
7898 	for_each_set_bit(link_id, &valid_links, IEEE80211_MLD_MAX_NUM_LINKS) {
7899 		arvif = wiphy_dereference(hw->wiphy, ahvif->link[link_id]);
7900 		arsta = wiphy_dereference(hw->wiphy, ahsta->link[link_id]);
7901 		/* some assumptions went wrong! */
7902 		if (WARN_ON(!arvif || !arsta))
7903 			continue;
7904 
7905 		/* vdev might be in deleted */
7906 		if (WARN_ON(!arvif->ar))
7907 			continue;
7908 
7909 		ret = ath12k_mac_handle_link_sta_state(hw, arvif, arsta,
7910 						       old_state, new_state);
7911 		if (ret) {
7912 			ath12k_hw_warn(ah, "unable to move link sta %d of sta %pM from state %d to %d",
7913 				       link_id, arsta->addr, old_state, new_state);
7914 
7915 			if (old_state == IEEE80211_STA_NOTEXIST &&
7916 			    new_state == IEEE80211_STA_NONE)
7917 				goto free_unassign_link;
7918 			else
7919 				goto exit;
7920 		}
7921 	}
7922 
7923 	if (ieee80211_vif_is_mld(vif) && vif->type == NL80211_IFTYPE_STATION &&
7924 	    old_state == IEEE80211_STA_ASSOC && new_state == IEEE80211_STA_AUTHORIZED) {
7925 		for_each_ar(ah, ar, i) {
7926 			ab = ar->ab;
7927 			if (prev_ab == ab)
7928 				continue;
7929 
7930 			ret = ath12k_mac_mlo_sta_update_link_active(ab, hw, ahvif);
7931 			if (ret) {
7932 				ath12k_warn(ab,
7933 					    "failed to update link active state on connect %d\n",
7934 					    ret);
7935 				goto exit;
7936 			}
7937 
7938 			prev_ab = ab;
7939 		}
7940 	}
7941 	/* IEEE80211_STA_NONE -> IEEE80211_STA_NOTEXIST:
7942 	 * Remove the station from driver (handle ML sta here since that
7943 	 * needs special handling. Normal sta will be handled in generic
7944 	 * handler below
7945 	 */
7946 	if (old_state == IEEE80211_STA_NONE &&
7947 	    new_state == IEEE80211_STA_NOTEXIST) {
7948 		if (sta->mlo)
7949 			ath12k_mac_ml_station_remove(ahvif, ahsta);
7950 
7951 		ath12k_dp_peer_delete(&ah->dp_hw, sta->addr, sta);
7952 	}
7953 
7954 	ret = 0;
7955 	goto exit;
7956 
7957 free_unassign_link:
7958 	ath12k_mac_free_unassign_link_sta(ah, ahsta, link_id);
7959 peer_delete:
7960 	ath12k_dp_peer_delete(&ah->dp_hw, sta->addr, sta);
7961 ml_peer_id_clear:
7962 	if (sta->mlo)
7963 		ath12k_peer_ml_free(ah, ahsta);
7964 exit:
7965 	/* update the state if everything went well */
7966 	if (!ret)
7967 		ahsta->state = new_state;
7968 
7969 	return ret;
7970 }
7971 EXPORT_SYMBOL(ath12k_mac_op_sta_state);
7972 
7973 int ath12k_mac_op_sta_set_txpwr(struct ieee80211_hw *hw,
7974 				struct ieee80211_vif *vif,
7975 				struct ieee80211_sta *sta)
7976 {
7977 	struct ath12k_sta *ahsta = ath12k_sta_to_ahsta(sta);
7978 	struct ath12k *ar;
7979 	struct ath12k_vif *ahvif = ath12k_vif_to_ahvif(vif);
7980 	struct ath12k_link_vif *arvif;
7981 	struct ath12k_link_sta *arsta;
7982 	u8 link_id;
7983 	int ret;
7984 	s16 txpwr;
7985 
7986 	lockdep_assert_wiphy(hw->wiphy);
7987 
7988 	/* TODO: use link id from mac80211 once that's implemented */
7989 	link_id = 0;
7990 
7991 	arvif = wiphy_dereference(hw->wiphy, ahvif->link[link_id]);
7992 	arsta = wiphy_dereference(hw->wiphy, ahsta->link[link_id]);
7993 
7994 	if (sta->deflink.txpwr.type == NL80211_TX_POWER_AUTOMATIC) {
7995 		txpwr = 0;
7996 	} else {
7997 		txpwr = sta->deflink.txpwr.power;
7998 		if (!txpwr) {
7999 			ret = -EINVAL;
8000 			goto out;
8001 		}
8002 	}
8003 
8004 	if (txpwr > ATH12K_TX_POWER_MAX_VAL || txpwr < ATH12K_TX_POWER_MIN_VAL) {
8005 		ret = -EINVAL;
8006 		goto out;
8007 	}
8008 
8009 	ar = arvif->ar;
8010 
8011 	ret = ath12k_wmi_set_peer_param(ar, arsta->addr, arvif->vdev_id,
8012 					WMI_PEER_USE_FIXED_PWR, txpwr);
8013 	if (ret) {
8014 		ath12k_warn(ar->ab, "failed to set tx power for station ret: %d\n",
8015 			    ret);
8016 		goto out;
8017 	}
8018 
8019 out:
8020 	return ret;
8021 }
8022 EXPORT_SYMBOL(ath12k_mac_op_sta_set_txpwr);
8023 
8024 void ath12k_mac_op_sta_set_4addr(struct ieee80211_hw *hw,
8025 				 struct ieee80211_vif *vif,
8026 				 struct ieee80211_sta *sta, bool enabled)
8027 {
8028 	struct ath12k_sta *ahsta = ath12k_sta_to_ahsta(sta);
8029 
8030 	lockdep_assert_wiphy(hw->wiphy);
8031 
8032 	/*
8033 	 * 4-address mode disabled option is available only for station
8034 	 * interface from mac80211, and we have wds_vdev_param for station
8035 	 * interface and target will not allow to disable the wds_vdev_param
8036 	 * during run time. So, add support only for enable case, for
8037 	 * disable case station interface needs to be reconnect.
8038 	 */
8039 	if (enabled && !ahsta->enable_4addr) {
8040 		if (!ath12k_mac_sta_set_4addr(hw->wiphy, ahsta))
8041 			ahsta->enable_4addr = true;
8042 	}
8043 }
8044 EXPORT_SYMBOL(ath12k_mac_op_sta_set_4addr);
8045 
8046 void ath12k_mac_op_link_sta_rc_update(struct ieee80211_hw *hw,
8047 				      struct ieee80211_vif *vif,
8048 				      struct ieee80211_link_sta *link_sta,
8049 				      u32 changed)
8050 {
8051 	struct ieee80211_sta *sta = link_sta->sta;
8052 	struct ath12k *ar;
8053 	struct ath12k_sta *ahsta = ath12k_sta_to_ahsta(sta);
8054 	struct ath12k_vif *ahvif = ath12k_vif_to_ahvif(vif);
8055 	struct ath12k_hw *ah = ath12k_hw_to_ah(hw);
8056 	struct ath12k_link_sta *arsta;
8057 	struct ath12k_link_vif *arvif;
8058 	struct ath12k_dp_link_peer *peer;
8059 	u32 bw, smps;
8060 	struct ath12k_dp *dp;
8061 
8062 	rcu_read_lock();
8063 	arvif = rcu_dereference(ahvif->link[link_sta->link_id]);
8064 	if (!arvif) {
8065 		ath12k_hw_warn(ah, "mac sta rc update failed to fetch link vif on link id %u for peer %pM\n",
8066 			       link_sta->link_id, sta->addr);
8067 		rcu_read_unlock();
8068 		return;
8069 	}
8070 
8071 	ar = arvif->ar;
8072 	dp = ath12k_ab_to_dp(ar->ab);
8073 
8074 	arsta = rcu_dereference(ahsta->link[link_sta->link_id]);
8075 	if (!arsta) {
8076 		rcu_read_unlock();
8077 		ath12k_warn(ar->ab, "mac sta rc update failed to fetch link sta on link id %u for peer %pM\n",
8078 			    link_sta->link_id, sta->addr);
8079 		return;
8080 	}
8081 	spin_lock_bh(&dp->dp_lock);
8082 
8083 	peer = ath12k_dp_link_peer_find_by_vdev_and_addr(dp, arvif->vdev_id,
8084 							 arsta->addr);
8085 	if (!peer) {
8086 		spin_unlock_bh(&dp->dp_lock);
8087 		rcu_read_unlock();
8088 		ath12k_warn(ar->ab, "mac sta rc update failed to find peer %pM on vdev %i\n",
8089 			    arsta->addr, arvif->vdev_id);
8090 		return;
8091 	}
8092 
8093 	spin_unlock_bh(&dp->dp_lock);
8094 
8095 	if (arsta->link_id >= IEEE80211_MLD_MAX_NUM_LINKS) {
8096 		rcu_read_unlock();
8097 		return;
8098 	}
8099 
8100 	link_sta = rcu_dereference(sta->link[arsta->link_id]);
8101 	if (!link_sta) {
8102 		rcu_read_unlock();
8103 		ath12k_warn(ar->ab, "unable to access link sta in rc update for sta %pM link %u\n",
8104 			    sta->addr, arsta->link_id);
8105 		return;
8106 	}
8107 
8108 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
8109 		   "mac sta rc update for %pM changed %08x bw %d nss %d smps %d\n",
8110 		   arsta->addr, changed, link_sta->bandwidth, link_sta->rx_nss,
8111 		   link_sta->smps_mode);
8112 
8113 	spin_lock_bh(&ar->data_lock);
8114 
8115 	if (changed & IEEE80211_RC_BW_CHANGED) {
8116 		bw = ath12k_mac_ieee80211_sta_bw_to_wmi(ar, link_sta);
8117 		arsta->bw_prev = arsta->bw;
8118 		arsta->bw = bw;
8119 	}
8120 
8121 	if (changed & IEEE80211_RC_NSS_CHANGED)
8122 		arsta->nss = link_sta->rx_nss;
8123 
8124 	if (changed & IEEE80211_RC_SMPS_CHANGED) {
8125 		smps = WMI_PEER_SMPS_PS_NONE;
8126 
8127 		switch (link_sta->smps_mode) {
8128 		case IEEE80211_SMPS_AUTOMATIC:
8129 		case IEEE80211_SMPS_OFF:
8130 			smps = WMI_PEER_SMPS_PS_NONE;
8131 			break;
8132 		case IEEE80211_SMPS_STATIC:
8133 			smps = WMI_PEER_SMPS_STATIC;
8134 			break;
8135 		case IEEE80211_SMPS_DYNAMIC:
8136 			smps = WMI_PEER_SMPS_DYNAMIC;
8137 			break;
8138 		default:
8139 			ath12k_warn(ar->ab, "Invalid smps %d in sta rc update for %pM link %u\n",
8140 				    link_sta->smps_mode, arsta->addr, link_sta->link_id);
8141 			smps = WMI_PEER_SMPS_PS_NONE;
8142 			break;
8143 		}
8144 
8145 		arsta->smps = smps;
8146 	}
8147 
8148 	arsta->changed |= changed;
8149 
8150 	spin_unlock_bh(&ar->data_lock);
8151 
8152 	wiphy_work_queue(hw->wiphy, &arsta->update_wk);
8153 
8154 	rcu_read_unlock();
8155 }
8156 EXPORT_SYMBOL(ath12k_mac_op_link_sta_rc_update);
8157 
8158 static struct ath12k_link_sta *ath12k_mac_alloc_assign_link_sta(struct ath12k_hw *ah,
8159 								struct ath12k_sta *ahsta,
8160 								struct ath12k_vif *ahvif,
8161 								u8 link_id)
8162 {
8163 	struct ath12k_link_sta *arsta;
8164 	int ret;
8165 
8166 	lockdep_assert_wiphy(ah->hw->wiphy);
8167 
8168 	if (link_id >= IEEE80211_MLD_MAX_NUM_LINKS)
8169 		return NULL;
8170 
8171 	arsta = wiphy_dereference(ah->hw->wiphy, ahsta->link[link_id]);
8172 	if (arsta)
8173 		return NULL;
8174 
8175 	arsta = kmalloc_obj(*arsta);
8176 	if (!arsta)
8177 		return NULL;
8178 
8179 	ret = ath12k_mac_assign_link_sta(ah, ahsta, arsta, ahvif, link_id);
8180 	if (ret) {
8181 		kfree(arsta);
8182 		return NULL;
8183 	}
8184 
8185 	return arsta;
8186 }
8187 
8188 int ath12k_mac_op_change_sta_links(struct ieee80211_hw *hw,
8189 				   struct ieee80211_vif *vif,
8190 				   struct ieee80211_sta *sta,
8191 				   u16 old_links, u16 new_links)
8192 {
8193 	struct ath12k_vif *ahvif = ath12k_vif_to_ahvif(vif);
8194 	struct ath12k_sta *ahsta = ath12k_sta_to_ahsta(sta);
8195 	struct ath12k_hw *ah = hw->priv;
8196 	struct ath12k_link_vif *arvif;
8197 	struct ath12k_link_sta *arsta;
8198 	unsigned long valid_links;
8199 	struct ath12k *ar;
8200 	u8 link_id;
8201 	int ret;
8202 
8203 	lockdep_assert_wiphy(hw->wiphy);
8204 
8205 	if (!sta->valid_links)
8206 		return -EINVAL;
8207 
8208 	/* Firmware does not support removal of one of link stas. All sta
8209 	 * would be removed during ML STA delete in sta_state(), hence link
8210 	 * sta removal is not handled here.
8211 	 */
8212 	if (new_links < old_links)
8213 		return 0;
8214 
8215 	if (ahsta->ml_peer_id == ATH12K_MLO_PEER_ID_INVALID) {
8216 		ath12k_hw_warn(ah, "unable to add link for ml sta %pM", sta->addr);
8217 		return -EINVAL;
8218 	}
8219 
8220 	/* this op is expected only after initial sta insertion with default link */
8221 	if (WARN_ON(ahsta->links_map == 0))
8222 		return -EINVAL;
8223 
8224 	valid_links = new_links;
8225 	for_each_set_bit(link_id, &valid_links, IEEE80211_MLD_MAX_NUM_LINKS) {
8226 		if (ahsta->links_map & BIT(link_id))
8227 			continue;
8228 
8229 		arvif = wiphy_dereference(hw->wiphy, ahvif->link[link_id]);
8230 		if (!arvif || !arvif->is_created)
8231 			continue;
8232 
8233 		arsta = ath12k_mac_alloc_assign_link_sta(ah, ahsta, ahvif, link_id);
8234 		if (!arsta) {
8235 			ath12k_hw_warn(ah, "Failed to alloc/assign link sta");
8236 			continue;
8237 		}
8238 
8239 		ar = arvif->ar;
8240 
8241 		ret = ath12k_mac_station_add(ar, arvif, arsta);
8242 		if (ret) {
8243 			ath12k_warn(ar->ab, "Failed to add station: %pM for VDEV: %d\n",
8244 				    arsta->addr, arvif->vdev_id);
8245 			ath12k_mac_free_unassign_link_sta(ah, ahsta, link_id);
8246 			return ret;
8247 		}
8248 	}
8249 
8250 	return 0;
8251 }
8252 EXPORT_SYMBOL(ath12k_mac_op_change_sta_links);
8253 
8254 bool ath12k_mac_op_can_activate_links(struct ieee80211_hw *hw,
8255 				      struct ieee80211_vif *vif,
8256 				      u16 active_links)
8257 {
8258 	/* TODO: Handle recovery case */
8259 
8260 	return true;
8261 }
8262 EXPORT_SYMBOL(ath12k_mac_op_can_activate_links);
8263 
8264 static int ath12k_conf_tx_uapsd(struct ath12k_link_vif *arvif,
8265 				u16 ac, bool enable)
8266 {
8267 	struct ath12k *ar = arvif->ar;
8268 	struct ath12k_vif *ahvif = arvif->ahvif;
8269 	u32 value;
8270 	int ret;
8271 
8272 	if (ahvif->vdev_type != WMI_VDEV_TYPE_STA)
8273 		return 0;
8274 
8275 	switch (ac) {
8276 	case IEEE80211_AC_VO:
8277 		value = WMI_STA_PS_UAPSD_AC3_DELIVERY_EN |
8278 			WMI_STA_PS_UAPSD_AC3_TRIGGER_EN;
8279 		break;
8280 	case IEEE80211_AC_VI:
8281 		value = WMI_STA_PS_UAPSD_AC2_DELIVERY_EN |
8282 			WMI_STA_PS_UAPSD_AC2_TRIGGER_EN;
8283 		break;
8284 	case IEEE80211_AC_BE:
8285 		value = WMI_STA_PS_UAPSD_AC1_DELIVERY_EN |
8286 			WMI_STA_PS_UAPSD_AC1_TRIGGER_EN;
8287 		break;
8288 	case IEEE80211_AC_BK:
8289 		value = WMI_STA_PS_UAPSD_AC0_DELIVERY_EN |
8290 			WMI_STA_PS_UAPSD_AC0_TRIGGER_EN;
8291 		break;
8292 	}
8293 
8294 	if (enable)
8295 		ahvif->u.sta.uapsd |= value;
8296 	else
8297 		ahvif->u.sta.uapsd &= ~value;
8298 
8299 	ret = ath12k_wmi_set_sta_ps_param(ar, arvif->vdev_id,
8300 					  WMI_STA_PS_PARAM_UAPSD,
8301 					  ahvif->u.sta.uapsd);
8302 	if (ret) {
8303 		ath12k_warn(ar->ab, "could not set uapsd params %d\n", ret);
8304 		goto exit;
8305 	}
8306 
8307 	if (ahvif->u.sta.uapsd)
8308 		value = WMI_STA_PS_RX_WAKE_POLICY_POLL_UAPSD;
8309 	else
8310 		value = WMI_STA_PS_RX_WAKE_POLICY_WAKE;
8311 
8312 	ret = ath12k_wmi_set_sta_ps_param(ar, arvif->vdev_id,
8313 					  WMI_STA_PS_PARAM_RX_WAKE_POLICY,
8314 					  value);
8315 	if (ret)
8316 		ath12k_warn(ar->ab, "could not set rx wake param %d\n", ret);
8317 
8318 exit:
8319 	return ret;
8320 }
8321 
8322 static int ath12k_mac_conf_tx(struct ath12k_link_vif *arvif, u16 ac,
8323 			      const struct ieee80211_tx_queue_params *params)
8324 {
8325 	struct wmi_wmm_params_arg *p = NULL;
8326 	struct ath12k *ar = arvif->ar;
8327 	struct ath12k_base *ab = ar->ab;
8328 	int ret;
8329 
8330 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
8331 
8332 	switch (ac) {
8333 	case IEEE80211_AC_VO:
8334 		p = &arvif->wmm_params.ac_vo;
8335 		break;
8336 	case IEEE80211_AC_VI:
8337 		p = &arvif->wmm_params.ac_vi;
8338 		break;
8339 	case IEEE80211_AC_BE:
8340 		p = &arvif->wmm_params.ac_be;
8341 		break;
8342 	case IEEE80211_AC_BK:
8343 		p = &arvif->wmm_params.ac_bk;
8344 		break;
8345 	}
8346 
8347 	if (WARN_ON(!p)) {
8348 		ret = -EINVAL;
8349 		goto exit;
8350 	}
8351 
8352 	p->cwmin = params->cw_min;
8353 	p->cwmax = params->cw_max;
8354 	p->aifs = params->aifs;
8355 	p->txop = params->txop;
8356 
8357 	ret = ath12k_wmi_send_wmm_update_cmd(ar, arvif->vdev_id,
8358 					     &arvif->wmm_params);
8359 	if (ret) {
8360 		ath12k_warn(ab, "pdev idx %d failed to set wmm params: %d\n",
8361 			    ar->pdev_idx, ret);
8362 		goto exit;
8363 	}
8364 
8365 	ret = ath12k_conf_tx_uapsd(arvif, ac, params->uapsd);
8366 	if (ret)
8367 		ath12k_warn(ab, "pdev idx %d failed to set sta uapsd: %d\n",
8368 			    ar->pdev_idx, ret);
8369 
8370 exit:
8371 	return ret;
8372 }
8373 
8374 int ath12k_mac_op_conf_tx(struct ieee80211_hw *hw,
8375 			  struct ieee80211_vif *vif,
8376 			  unsigned int link_id, u16 ac,
8377 			  const struct ieee80211_tx_queue_params *params)
8378 {
8379 	struct ath12k_vif *ahvif = ath12k_vif_to_ahvif(vif);
8380 	struct ath12k_link_vif *arvif;
8381 	struct ath12k_vif_cache *cache;
8382 	int ret;
8383 
8384 	lockdep_assert_wiphy(hw->wiphy);
8385 
8386 	if (link_id >= IEEE80211_MLD_MAX_NUM_LINKS)
8387 		return -EINVAL;
8388 
8389 	arvif = wiphy_dereference(hw->wiphy, ahvif->link[link_id]);
8390 	if (!arvif || !arvif->is_created) {
8391 		cache = ath12k_ahvif_get_link_cache(ahvif, link_id);
8392 		if (!cache)
8393 			return -ENOSPC;
8394 
8395 		cache->tx_conf.changed = true;
8396 		cache->tx_conf.ac = ac;
8397 		cache->tx_conf.tx_queue_params = *params;
8398 
8399 		return 0;
8400 	}
8401 
8402 	ret = ath12k_mac_conf_tx(arvif, ac, params);
8403 
8404 	return ret;
8405 }
8406 EXPORT_SYMBOL(ath12k_mac_op_conf_tx);
8407 
8408 static struct ieee80211_sta_ht_cap
8409 ath12k_create_ht_cap(struct ath12k *ar, u32 ar_ht_cap, u32 rate_cap_rx_chainmask)
8410 {
8411 	int i;
8412 	struct ieee80211_sta_ht_cap ht_cap = {};
8413 	u32 ar_vht_cap = ar->pdev->cap.vht_cap;
8414 
8415 	if (!(ar_ht_cap & WMI_HT_CAP_ENABLED))
8416 		return ht_cap;
8417 
8418 	ht_cap.ht_supported = 1;
8419 	ht_cap.ampdu_factor = IEEE80211_HT_MAX_AMPDU_64K;
8420 	ht_cap.ampdu_density = IEEE80211_HT_MPDU_DENSITY_NONE;
8421 	ht_cap.cap |= IEEE80211_HT_CAP_SUP_WIDTH_20_40;
8422 	ht_cap.cap |= IEEE80211_HT_CAP_DSSSCCK40;
8423 	ht_cap.cap |= WLAN_HT_CAP_SM_PS_STATIC << IEEE80211_HT_CAP_SM_PS_SHIFT;
8424 
8425 	if (ar_ht_cap & WMI_HT_CAP_HT20_SGI)
8426 		ht_cap.cap |= IEEE80211_HT_CAP_SGI_20;
8427 
8428 	if (ar_ht_cap & WMI_HT_CAP_HT40_SGI)
8429 		ht_cap.cap |= IEEE80211_HT_CAP_SGI_40;
8430 
8431 	if (ar_ht_cap & WMI_HT_CAP_DYNAMIC_SMPS) {
8432 		u32 smps;
8433 
8434 		smps   = WLAN_HT_CAP_SM_PS_DYNAMIC;
8435 		smps <<= IEEE80211_HT_CAP_SM_PS_SHIFT;
8436 
8437 		ht_cap.cap |= smps;
8438 	}
8439 
8440 	if (ar_ht_cap & WMI_HT_CAP_TX_STBC)
8441 		ht_cap.cap |= IEEE80211_HT_CAP_TX_STBC;
8442 
8443 	if (ar_ht_cap & WMI_HT_CAP_RX_STBC) {
8444 		u32 stbc;
8445 
8446 		stbc   = ar_ht_cap;
8447 		stbc  &= WMI_HT_CAP_RX_STBC;
8448 		stbc >>= WMI_HT_CAP_RX_STBC_MASK_SHIFT;
8449 		stbc <<= IEEE80211_HT_CAP_RX_STBC_SHIFT;
8450 		stbc  &= IEEE80211_HT_CAP_RX_STBC;
8451 
8452 		ht_cap.cap |= stbc;
8453 	}
8454 
8455 	if (ar_ht_cap & WMI_HT_CAP_RX_LDPC)
8456 		ht_cap.cap |= IEEE80211_HT_CAP_LDPC_CODING;
8457 
8458 	if (ar_ht_cap & WMI_HT_CAP_L_SIG_TXOP_PROT)
8459 		ht_cap.cap |= IEEE80211_HT_CAP_LSIG_TXOP_PROT;
8460 
8461 	if (ar_vht_cap & WMI_VHT_CAP_MAX_MPDU_LEN_MASK)
8462 		ht_cap.cap |= IEEE80211_HT_CAP_MAX_AMSDU;
8463 
8464 	for (i = 0; i < ar->num_rx_chains; i++) {
8465 		if (rate_cap_rx_chainmask & BIT(i))
8466 			ht_cap.mcs.rx_mask[i] = 0xFF;
8467 	}
8468 
8469 	ht_cap.mcs.tx_params |= IEEE80211_HT_MCS_TX_DEFINED;
8470 
8471 	return ht_cap;
8472 }
8473 
8474 static int ath12k_mac_set_txbf_conf(struct ath12k_link_vif *arvif)
8475 {
8476 	u32 value = 0;
8477 	struct ath12k *ar = arvif->ar;
8478 	struct ath12k_vif *ahvif = arvif->ahvif;
8479 	int nsts;
8480 	int sound_dim;
8481 	u32 vht_cap = ar->pdev->cap.vht_cap;
8482 	u32 vdev_param = WMI_VDEV_PARAM_TXBF;
8483 
8484 	if (vht_cap & (IEEE80211_VHT_CAP_SU_BEAMFORMEE_CAPABLE)) {
8485 		nsts = vht_cap & IEEE80211_VHT_CAP_BEAMFORMEE_STS_MASK;
8486 		nsts >>= IEEE80211_VHT_CAP_BEAMFORMEE_STS_SHIFT;
8487 		value |= SM(nsts, WMI_TXBF_STS_CAP_OFFSET);
8488 	}
8489 
8490 	if (vht_cap & (IEEE80211_VHT_CAP_SU_BEAMFORMER_CAPABLE)) {
8491 		sound_dim = vht_cap &
8492 			    IEEE80211_VHT_CAP_SOUNDING_DIMENSIONS_MASK;
8493 		sound_dim >>= IEEE80211_VHT_CAP_SOUNDING_DIMENSIONS_SHIFT;
8494 		if (sound_dim > (ar->num_tx_chains - 1))
8495 			sound_dim = ar->num_tx_chains - 1;
8496 		value |= SM(sound_dim, WMI_BF_SOUND_DIM_OFFSET);
8497 	}
8498 
8499 	if (!value)
8500 		return 0;
8501 
8502 	if (vht_cap & IEEE80211_VHT_CAP_SU_BEAMFORMER_CAPABLE) {
8503 		value |= WMI_VDEV_PARAM_TXBF_SU_TX_BFER;
8504 
8505 		if ((vht_cap & IEEE80211_VHT_CAP_MU_BEAMFORMER_CAPABLE) &&
8506 		    ahvif->vdev_type == WMI_VDEV_TYPE_AP)
8507 			value |= WMI_VDEV_PARAM_TXBF_MU_TX_BFER;
8508 	}
8509 
8510 	if (vht_cap & IEEE80211_VHT_CAP_SU_BEAMFORMEE_CAPABLE) {
8511 		value |= WMI_VDEV_PARAM_TXBF_SU_TX_BFEE;
8512 
8513 		if ((vht_cap & IEEE80211_VHT_CAP_MU_BEAMFORMEE_CAPABLE) &&
8514 		    ahvif->vdev_type == WMI_VDEV_TYPE_STA)
8515 			value |= WMI_VDEV_PARAM_TXBF_MU_TX_BFEE;
8516 	}
8517 
8518 	return ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
8519 					     vdev_param, value);
8520 }
8521 
8522 static void ath12k_set_vht_txbf_cap(struct ath12k *ar, u32 *vht_cap)
8523 {
8524 	bool subfer, subfee;
8525 	int sound_dim = 0;
8526 
8527 	subfer = !!(*vht_cap & (IEEE80211_VHT_CAP_SU_BEAMFORMER_CAPABLE));
8528 	subfee = !!(*vht_cap & (IEEE80211_VHT_CAP_SU_BEAMFORMEE_CAPABLE));
8529 
8530 	if (ar->num_tx_chains < 2) {
8531 		*vht_cap &= ~(IEEE80211_VHT_CAP_SU_BEAMFORMER_CAPABLE);
8532 		subfer = false;
8533 	}
8534 
8535 	/* If SU Beaformer is not set, then disable MU Beamformer Capability */
8536 	if (!subfer)
8537 		*vht_cap &= ~(IEEE80211_VHT_CAP_MU_BEAMFORMER_CAPABLE);
8538 
8539 	/* If SU Beaformee is not set, then disable MU Beamformee Capability */
8540 	if (!subfee)
8541 		*vht_cap &= ~(IEEE80211_VHT_CAP_MU_BEAMFORMEE_CAPABLE);
8542 
8543 	sound_dim = u32_get_bits(*vht_cap,
8544 				 IEEE80211_VHT_CAP_SOUNDING_DIMENSIONS_MASK);
8545 	*vht_cap = u32_replace_bits(*vht_cap, 0,
8546 				    IEEE80211_VHT_CAP_SOUNDING_DIMENSIONS_MASK);
8547 
8548 	/* TODO: Need to check invalid STS and Sound_dim values set by FW? */
8549 
8550 	/* Enable Sounding Dimension Field only if SU BF is enabled */
8551 	if (subfer) {
8552 		if (sound_dim > (ar->num_tx_chains - 1))
8553 			sound_dim = ar->num_tx_chains - 1;
8554 
8555 		*vht_cap = u32_replace_bits(*vht_cap, sound_dim,
8556 					    IEEE80211_VHT_CAP_SOUNDING_DIMENSIONS_MASK);
8557 	}
8558 
8559 	/* Use the STS advertised by FW unless SU Beamformee is not supported*/
8560 	if (!subfee)
8561 		*vht_cap &= ~(IEEE80211_VHT_CAP_BEAMFORMEE_STS_MASK);
8562 }
8563 
8564 static struct ieee80211_sta_vht_cap
8565 ath12k_create_vht_cap(struct ath12k *ar, u32 rate_cap_tx_chainmask,
8566 		      u32 rate_cap_rx_chainmask)
8567 {
8568 	struct ieee80211_sta_vht_cap vht_cap = {};
8569 	u16 txmcs_map, rxmcs_map;
8570 	int i;
8571 
8572 	vht_cap.vht_supported = 1;
8573 	vht_cap.cap = ar->pdev->cap.vht_cap;
8574 
8575 	if (ar->pdev->cap.nss_ratio_enabled)
8576 		vht_cap.vht_mcs.tx_highest |=
8577 			cpu_to_le16(IEEE80211_VHT_EXT_NSS_BW_CAPABLE);
8578 
8579 	ath12k_set_vht_txbf_cap(ar, &vht_cap.cap);
8580 
8581 	/* 80P80 is not supported */
8582 	vht_cap.cap &= ~IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_160_80PLUS80MHZ;
8583 
8584 	rxmcs_map = 0;
8585 	txmcs_map = 0;
8586 	for (i = 0; i < 8; i++) {
8587 		if (i < ar->num_tx_chains && rate_cap_tx_chainmask & BIT(i))
8588 			txmcs_map |= IEEE80211_VHT_MCS_SUPPORT_0_9 << (i * 2);
8589 		else
8590 			txmcs_map |= IEEE80211_VHT_MCS_NOT_SUPPORTED << (i * 2);
8591 
8592 		if (i < ar->num_rx_chains && rate_cap_rx_chainmask & BIT(i))
8593 			rxmcs_map |= IEEE80211_VHT_MCS_SUPPORT_0_9 << (i * 2);
8594 		else
8595 			rxmcs_map |= IEEE80211_VHT_MCS_NOT_SUPPORTED << (i * 2);
8596 	}
8597 
8598 	if (rate_cap_tx_chainmask <= 1)
8599 		vht_cap.cap &= ~IEEE80211_VHT_CAP_TXSTBC;
8600 
8601 	vht_cap.vht_mcs.rx_mcs_map = cpu_to_le16(rxmcs_map);
8602 	vht_cap.vht_mcs.tx_mcs_map = cpu_to_le16(txmcs_map);
8603 
8604 	/* Check if the HW supports 1:1 NSS ratio and reset
8605 	 * EXT NSS BW Support field to 0 to indicate 1:1 ratio
8606 	 */
8607 	if (ar->pdev->cap.nss_ratio_info == WMI_NSS_RATIO_1_NSS)
8608 		vht_cap.cap &= ~IEEE80211_VHT_CAP_EXT_NSS_BW_MASK;
8609 
8610 	return vht_cap;
8611 }
8612 
8613 static void ath12k_mac_setup_ht_vht_cap(struct ath12k *ar,
8614 					struct ath12k_pdev_cap *cap,
8615 					u32 *ht_cap_info)
8616 {
8617 	struct ieee80211_supported_band *band;
8618 	u32 rate_cap_tx_chainmask;
8619 	u32 rate_cap_rx_chainmask;
8620 	u32 ht_cap;
8621 
8622 	rate_cap_tx_chainmask = ar->cfg_tx_chainmask >> cap->tx_chain_mask_shift;
8623 	rate_cap_rx_chainmask = ar->cfg_rx_chainmask >> cap->rx_chain_mask_shift;
8624 
8625 	if (cap->supported_bands & WMI_HOST_WLAN_2GHZ_CAP) {
8626 		band = &ar->mac.sbands[NL80211_BAND_2GHZ];
8627 		ht_cap = cap->band[NL80211_BAND_2GHZ].ht_cap_info;
8628 		if (ht_cap_info)
8629 			*ht_cap_info = ht_cap;
8630 		band->ht_cap = ath12k_create_ht_cap(ar, ht_cap,
8631 						    rate_cap_rx_chainmask);
8632 	}
8633 
8634 	if (cap->supported_bands & WMI_HOST_WLAN_5GHZ_CAP &&
8635 	    (ar->ab->hw_params->single_pdev_only ||
8636 	     !ar->supports_6ghz)) {
8637 		band = &ar->mac.sbands[NL80211_BAND_5GHZ];
8638 		ht_cap = cap->band[NL80211_BAND_5GHZ].ht_cap_info;
8639 		if (ht_cap_info)
8640 			*ht_cap_info = ht_cap;
8641 		band->ht_cap = ath12k_create_ht_cap(ar, ht_cap,
8642 						    rate_cap_rx_chainmask);
8643 		band->vht_cap = ath12k_create_vht_cap(ar, rate_cap_tx_chainmask,
8644 						      rate_cap_rx_chainmask);
8645 	}
8646 }
8647 
8648 static int ath12k_check_chain_mask(struct ath12k *ar, u32 ant, bool is_tx_ant)
8649 {
8650 	/* TODO: Check the request chainmask against the supported
8651 	 * chainmask table which is advertised in extented_service_ready event
8652 	 */
8653 
8654 	return 0;
8655 }
8656 
8657 static void ath12k_gen_ppe_thresh(struct ath12k_wmi_ppe_threshold_arg *fw_ppet,
8658 				  u8 *he_ppet)
8659 {
8660 	int nss, ru;
8661 	u8 bit = 7;
8662 
8663 	he_ppet[0] = fw_ppet->numss_m1 & IEEE80211_PPE_THRES_NSS_MASK;
8664 	he_ppet[0] |= (fw_ppet->ru_bit_mask <<
8665 		       IEEE80211_PPE_THRES_RU_INDEX_BITMASK_POS) &
8666 		      IEEE80211_PPE_THRES_RU_INDEX_BITMASK_MASK;
8667 	for (nss = 0; nss <= fw_ppet->numss_m1; nss++) {
8668 		for (ru = 0; ru < 4; ru++) {
8669 			u8 val;
8670 			int i;
8671 
8672 			if ((fw_ppet->ru_bit_mask & BIT(ru)) == 0)
8673 				continue;
8674 			val = (fw_ppet->ppet16_ppet8_ru3_ru0[nss] >> (ru * 6)) &
8675 			       0x3f;
8676 			val = ((val >> 3) & 0x7) | ((val & 0x7) << 3);
8677 			for (i = 5; i >= 0; i--) {
8678 				he_ppet[bit / 8] |=
8679 					((val >> i) & 0x1) << ((bit % 8));
8680 				bit++;
8681 			}
8682 		}
8683 	}
8684 }
8685 
8686 static void
8687 ath12k_mac_filter_he_cap_mesh(struct ieee80211_he_cap_elem *he_cap_elem)
8688 {
8689 	u8 m;
8690 
8691 	m = IEEE80211_HE_MAC_CAP0_TWT_RES |
8692 	    IEEE80211_HE_MAC_CAP0_TWT_REQ;
8693 	he_cap_elem->mac_cap_info[0] &= ~m;
8694 
8695 	m = IEEE80211_HE_MAC_CAP2_TRS |
8696 	    IEEE80211_HE_MAC_CAP2_BCAST_TWT |
8697 	    IEEE80211_HE_MAC_CAP2_MU_CASCADING;
8698 	he_cap_elem->mac_cap_info[2] &= ~m;
8699 
8700 	m = IEEE80211_HE_MAC_CAP3_FLEX_TWT_SCHED |
8701 	    IEEE80211_HE_MAC_CAP2_BCAST_TWT |
8702 	    IEEE80211_HE_MAC_CAP2_MU_CASCADING;
8703 	he_cap_elem->mac_cap_info[3] &= ~m;
8704 
8705 	m = IEEE80211_HE_MAC_CAP4_BSRP_BQRP_A_MPDU_AGG |
8706 	    IEEE80211_HE_MAC_CAP4_BQR;
8707 	he_cap_elem->mac_cap_info[4] &= ~m;
8708 
8709 	m = IEEE80211_HE_MAC_CAP5_SUBCHAN_SELECTIVE_TRANSMISSION |
8710 	    IEEE80211_HE_MAC_CAP5_UL_2x996_TONE_RU |
8711 	    IEEE80211_HE_MAC_CAP5_PUNCTURED_SOUNDING |
8712 	    IEEE80211_HE_MAC_CAP5_HT_VHT_TRIG_FRAME_RX;
8713 	he_cap_elem->mac_cap_info[5] &= ~m;
8714 
8715 	m = IEEE80211_HE_PHY_CAP2_UL_MU_FULL_MU_MIMO |
8716 	    IEEE80211_HE_PHY_CAP2_UL_MU_PARTIAL_MU_MIMO;
8717 	he_cap_elem->phy_cap_info[2] &= ~m;
8718 
8719 	m = IEEE80211_HE_PHY_CAP3_RX_PARTIAL_BW_SU_IN_20MHZ_MU |
8720 	    IEEE80211_HE_PHY_CAP3_DCM_MAX_CONST_TX_MASK |
8721 	    IEEE80211_HE_PHY_CAP3_DCM_MAX_CONST_RX_MASK;
8722 	he_cap_elem->phy_cap_info[3] &= ~m;
8723 
8724 	m = IEEE80211_HE_PHY_CAP4_MU_BEAMFORMER;
8725 	he_cap_elem->phy_cap_info[4] &= ~m;
8726 
8727 	m = IEEE80211_HE_PHY_CAP5_NG16_MU_FEEDBACK;
8728 	he_cap_elem->phy_cap_info[5] &= ~m;
8729 
8730 	m = IEEE80211_HE_PHY_CAP6_CODEBOOK_SIZE_75_MU |
8731 	    IEEE80211_HE_PHY_CAP6_TRIG_MU_BEAMFORMING_PARTIAL_BW_FB |
8732 	    IEEE80211_HE_PHY_CAP6_TRIG_CQI_FB |
8733 	    IEEE80211_HE_PHY_CAP6_PARTIAL_BANDWIDTH_DL_MUMIMO;
8734 	he_cap_elem->phy_cap_info[6] &= ~m;
8735 
8736 	m = IEEE80211_HE_PHY_CAP7_PSR_BASED_SR |
8737 	    IEEE80211_HE_PHY_CAP7_POWER_BOOST_FACTOR_SUPP |
8738 	    IEEE80211_HE_PHY_CAP7_STBC_TX_ABOVE_80MHZ |
8739 	    IEEE80211_HE_PHY_CAP7_STBC_RX_ABOVE_80MHZ;
8740 	he_cap_elem->phy_cap_info[7] &= ~m;
8741 
8742 	m = IEEE80211_HE_PHY_CAP8_HE_ER_SU_PPDU_4XLTF_AND_08_US_GI |
8743 	    IEEE80211_HE_PHY_CAP8_20MHZ_IN_40MHZ_HE_PPDU_IN_2G |
8744 	    IEEE80211_HE_PHY_CAP8_20MHZ_IN_160MHZ_HE_PPDU |
8745 	    IEEE80211_HE_PHY_CAP8_80MHZ_IN_160MHZ_HE_PPDU;
8746 	he_cap_elem->phy_cap_info[8] &= ~m;
8747 
8748 	m = IEEE80211_HE_PHY_CAP9_LONGER_THAN_16_SIGB_OFDM_SYM |
8749 	    IEEE80211_HE_PHY_CAP9_NON_TRIGGERED_CQI_FEEDBACK |
8750 	    IEEE80211_HE_PHY_CAP9_RX_1024_QAM_LESS_THAN_242_TONE_RU |
8751 	    IEEE80211_HE_PHY_CAP9_TX_1024_QAM_LESS_THAN_242_TONE_RU |
8752 	    IEEE80211_HE_PHY_CAP9_RX_FULL_BW_SU_USING_MU_WITH_COMP_SIGB |
8753 	    IEEE80211_HE_PHY_CAP9_RX_FULL_BW_SU_USING_MU_WITH_NON_COMP_SIGB;
8754 	he_cap_elem->phy_cap_info[9] &= ~m;
8755 }
8756 
8757 static __le16 ath12k_mac_setup_he_6ghz_cap(struct ath12k_pdev_cap *pcap,
8758 					   struct ath12k_band_cap *bcap)
8759 {
8760 	u8 val;
8761 
8762 	bcap->he_6ghz_capa = IEEE80211_HT_MPDU_DENSITY_NONE;
8763 	if (bcap->ht_cap_info & WMI_HT_CAP_DYNAMIC_SMPS)
8764 		bcap->he_6ghz_capa |=
8765 			u32_encode_bits(WLAN_HT_CAP_SM_PS_DYNAMIC,
8766 					IEEE80211_HE_6GHZ_CAP_SM_PS);
8767 	else
8768 		bcap->he_6ghz_capa |=
8769 			u32_encode_bits(WLAN_HT_CAP_SM_PS_DISABLED,
8770 					IEEE80211_HE_6GHZ_CAP_SM_PS);
8771 	val = u32_get_bits(pcap->vht_cap,
8772 			   IEEE80211_VHT_CAP_MAX_A_MPDU_LENGTH_EXPONENT_MASK);
8773 	bcap->he_6ghz_capa |=
8774 		u32_encode_bits(val, IEEE80211_HE_6GHZ_CAP_MAX_AMPDU_LEN_EXP);
8775 	val = u32_get_bits(pcap->vht_cap,
8776 			   IEEE80211_VHT_CAP_MAX_MPDU_MASK);
8777 	bcap->he_6ghz_capa |=
8778 		u32_encode_bits(val, IEEE80211_HE_6GHZ_CAP_MAX_MPDU_LEN);
8779 	if (pcap->vht_cap & IEEE80211_VHT_CAP_RX_ANTENNA_PATTERN)
8780 		bcap->he_6ghz_capa |= IEEE80211_HE_6GHZ_CAP_RX_ANTPAT_CONS;
8781 	if (pcap->vht_cap & IEEE80211_VHT_CAP_TX_ANTENNA_PATTERN)
8782 		bcap->he_6ghz_capa |= IEEE80211_HE_6GHZ_CAP_TX_ANTPAT_CONS;
8783 
8784 	return cpu_to_le16(bcap->he_6ghz_capa);
8785 }
8786 
8787 static void ath12k_mac_set_hemcsmap(struct ath12k *ar,
8788 				    struct ath12k_pdev_cap *cap,
8789 				    struct ieee80211_sta_he_cap *he_cap)
8790 {
8791 	struct ieee80211_he_mcs_nss_supp *mcs_nss = &he_cap->he_mcs_nss_supp;
8792 	u8 maxtxnss_160 = ath12k_get_nss_160mhz(ar, ar->num_tx_chains);
8793 	u8 maxrxnss_160 = ath12k_get_nss_160mhz(ar, ar->num_rx_chains);
8794 	u16 txmcs_map_160 = 0, rxmcs_map_160 = 0;
8795 	u16 txmcs_map = 0, rxmcs_map = 0;
8796 	u32 i;
8797 
8798 	for (i = 0; i < 8; i++) {
8799 		if (i < ar->num_tx_chains &&
8800 		    (ar->cfg_tx_chainmask >> cap->tx_chain_mask_shift) & BIT(i))
8801 			txmcs_map |= IEEE80211_HE_MCS_SUPPORT_0_11 << (i * 2);
8802 		else
8803 			txmcs_map |= IEEE80211_HE_MCS_NOT_SUPPORTED << (i * 2);
8804 
8805 		if (i < ar->num_rx_chains &&
8806 		    (ar->cfg_rx_chainmask >> cap->tx_chain_mask_shift) & BIT(i))
8807 			rxmcs_map |= IEEE80211_HE_MCS_SUPPORT_0_11 << (i * 2);
8808 		else
8809 			rxmcs_map |= IEEE80211_HE_MCS_NOT_SUPPORTED << (i * 2);
8810 
8811 		if (i < maxtxnss_160 &&
8812 		    (ar->cfg_tx_chainmask >> cap->tx_chain_mask_shift) & BIT(i))
8813 			txmcs_map_160 |= IEEE80211_HE_MCS_SUPPORT_0_11 << (i * 2);
8814 		else
8815 			txmcs_map_160 |= IEEE80211_HE_MCS_NOT_SUPPORTED << (i * 2);
8816 
8817 		if (i < maxrxnss_160 &&
8818 		    (ar->cfg_tx_chainmask >> cap->tx_chain_mask_shift) & BIT(i))
8819 			rxmcs_map_160 |= IEEE80211_HE_MCS_SUPPORT_0_11 << (i * 2);
8820 		else
8821 			rxmcs_map_160 |= IEEE80211_HE_MCS_NOT_SUPPORTED << (i * 2);
8822 	}
8823 
8824 	mcs_nss->rx_mcs_80 = cpu_to_le16(rxmcs_map & 0xffff);
8825 	mcs_nss->tx_mcs_80 = cpu_to_le16(txmcs_map & 0xffff);
8826 	mcs_nss->rx_mcs_160 = cpu_to_le16(rxmcs_map_160 & 0xffff);
8827 	mcs_nss->tx_mcs_160 = cpu_to_le16(txmcs_map_160 & 0xffff);
8828 }
8829 
8830 static void ath12k_mac_copy_he_cap(struct ath12k *ar,
8831 				   struct ath12k_band_cap *band_cap,
8832 				   int iftype, u8 num_tx_chains,
8833 				   struct ieee80211_sta_he_cap *he_cap)
8834 {
8835 	struct ieee80211_he_cap_elem *he_cap_elem = &he_cap->he_cap_elem;
8836 
8837 	he_cap->has_he = true;
8838 	memcpy(he_cap_elem->mac_cap_info, band_cap->he_cap_info,
8839 	       sizeof(he_cap_elem->mac_cap_info));
8840 	memcpy(he_cap_elem->phy_cap_info, band_cap->he_cap_phy_info,
8841 	       sizeof(he_cap_elem->phy_cap_info));
8842 
8843 	he_cap_elem->mac_cap_info[1] &=
8844 		IEEE80211_HE_MAC_CAP1_TF_MAC_PAD_DUR_MASK;
8845 	he_cap_elem->phy_cap_info[0] &=
8846 		IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_40MHZ_IN_2G |
8847 		IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_40MHZ_80MHZ_IN_5G |
8848 		IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_160MHZ_IN_5G;
8849 	/* 80PLUS80 is not supported */
8850 	he_cap_elem->phy_cap_info[0] &=
8851 		~IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_80PLUS80_MHZ_IN_5G;
8852 	he_cap_elem->phy_cap_info[5] &=
8853 		~IEEE80211_HE_PHY_CAP5_BEAMFORMEE_NUM_SND_DIM_UNDER_80MHZ_MASK;
8854 	he_cap_elem->phy_cap_info[5] |= num_tx_chains - 1;
8855 
8856 	switch (iftype) {
8857 	case NL80211_IFTYPE_AP:
8858 		he_cap_elem->mac_cap_info[2] &=
8859 			~IEEE80211_HE_MAC_CAP2_BCAST_TWT;
8860 		he_cap_elem->phy_cap_info[3] &=
8861 			~IEEE80211_HE_PHY_CAP3_DCM_MAX_CONST_TX_MASK;
8862 		he_cap_elem->phy_cap_info[9] |=
8863 			IEEE80211_HE_PHY_CAP9_RX_1024_QAM_LESS_THAN_242_TONE_RU;
8864 		break;
8865 	case NL80211_IFTYPE_STATION:
8866 		he_cap_elem->mac_cap_info[0] &= ~IEEE80211_HE_MAC_CAP0_TWT_RES;
8867 		he_cap_elem->mac_cap_info[0] |= IEEE80211_HE_MAC_CAP0_TWT_REQ;
8868 		he_cap_elem->phy_cap_info[9] |=
8869 			IEEE80211_HE_PHY_CAP9_TX_1024_QAM_LESS_THAN_242_TONE_RU;
8870 		break;
8871 	case NL80211_IFTYPE_MESH_POINT:
8872 		ath12k_mac_filter_he_cap_mesh(he_cap_elem);
8873 		break;
8874 	}
8875 
8876 	ath12k_mac_set_hemcsmap(ar, &ar->pdev->cap, he_cap);
8877 	memset(he_cap->ppe_thres, 0, sizeof(he_cap->ppe_thres));
8878 	if (he_cap_elem->phy_cap_info[6] &
8879 	    IEEE80211_HE_PHY_CAP6_PPE_THRESHOLD_PRESENT)
8880 		ath12k_gen_ppe_thresh(&band_cap->he_ppet, he_cap->ppe_thres);
8881 }
8882 
8883 static void
8884 ath12k_mac_copy_eht_mcs_nss(struct ath12k_band_cap *band_cap,
8885 			    struct ieee80211_eht_mcs_nss_supp *mcs_nss,
8886 			    const struct ieee80211_he_cap_elem *he_cap,
8887 			    const struct ieee80211_eht_cap_elem_fixed *eht_cap)
8888 {
8889 	if ((he_cap->phy_cap_info[0] &
8890 	     (IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_40MHZ_IN_2G |
8891 	      IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_40MHZ_80MHZ_IN_5G |
8892 	      IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_160MHZ_IN_5G |
8893 	      IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_80PLUS80_MHZ_IN_5G)) == 0)
8894 		memcpy(&mcs_nss->only_20mhz, &band_cap->eht_mcs_20_only,
8895 		       sizeof(struct ieee80211_eht_mcs_nss_supp_20mhz_only));
8896 
8897 	if (he_cap->phy_cap_info[0] &
8898 	    (IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_40MHZ_IN_2G |
8899 	     IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_40MHZ_80MHZ_IN_5G))
8900 		memcpy(&mcs_nss->bw._80, &band_cap->eht_mcs_80,
8901 		       sizeof(struct ieee80211_eht_mcs_nss_supp_bw));
8902 
8903 	if (he_cap->phy_cap_info[0] &
8904 	    IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_160MHZ_IN_5G)
8905 		memcpy(&mcs_nss->bw._160, &band_cap->eht_mcs_160,
8906 		       sizeof(struct ieee80211_eht_mcs_nss_supp_bw));
8907 
8908 	if (eht_cap->phy_cap_info[0] & IEEE80211_EHT_PHY_CAP0_320MHZ_IN_6GHZ)
8909 		memcpy(&mcs_nss->bw._320, &band_cap->eht_mcs_320,
8910 		       sizeof(struct ieee80211_eht_mcs_nss_supp_bw));
8911 }
8912 
8913 static void ath12k_mac_copy_eht_ppe_thresh(struct ath12k_wmi_ppe_threshold_arg *fw_ppet,
8914 					   struct ieee80211_sta_eht_cap *cap)
8915 {
8916 	u16 bit = IEEE80211_EHT_PPE_THRES_INFO_HEADER_SIZE;
8917 	u8 i, nss, ru, ppet_bit_len_per_ru = IEEE80211_EHT_PPE_THRES_INFO_PPET_SIZE * 2;
8918 
8919 	u8p_replace_bits(&cap->eht_ppe_thres[0], fw_ppet->numss_m1,
8920 			 IEEE80211_EHT_PPE_THRES_NSS_MASK);
8921 
8922 	u16p_replace_bits((u16 *)&cap->eht_ppe_thres[0], fw_ppet->ru_bit_mask,
8923 			  IEEE80211_EHT_PPE_THRES_RU_INDEX_BITMASK_MASK);
8924 
8925 	for (nss = 0; nss <= fw_ppet->numss_m1; nss++) {
8926 		for (ru = 0;
8927 		     ru < hweight16(IEEE80211_EHT_PPE_THRES_RU_INDEX_BITMASK_MASK);
8928 		     ru++) {
8929 			u32 val = 0;
8930 
8931 			if ((fw_ppet->ru_bit_mask & BIT(ru)) == 0)
8932 				continue;
8933 
8934 			u32p_replace_bits(&val, fw_ppet->ppet16_ppet8_ru3_ru0[nss] >>
8935 						(ru * ppet_bit_len_per_ru),
8936 					  GENMASK(ppet_bit_len_per_ru - 1, 0));
8937 
8938 			for (i = 0; i < ppet_bit_len_per_ru; i++) {
8939 				cap->eht_ppe_thres[bit / 8] |=
8940 					(((val >> i) & 0x1) << ((bit % 8)));
8941 				bit++;
8942 			}
8943 		}
8944 	}
8945 }
8946 
8947 static void
8948 ath12k_mac_filter_eht_cap_mesh(struct ieee80211_eht_cap_elem_fixed
8949 			       *eht_cap_elem)
8950 {
8951 	u8 m;
8952 
8953 	m = IEEE80211_EHT_MAC_CAP0_EPCS_PRIO_ACCESS;
8954 	eht_cap_elem->mac_cap_info[0] &= ~m;
8955 
8956 	m = IEEE80211_EHT_PHY_CAP0_PARTIAL_BW_UL_MU_MIMO;
8957 	eht_cap_elem->phy_cap_info[0] &= ~m;
8958 
8959 	m = IEEE80211_EHT_PHY_CAP3_NG_16_MU_FEEDBACK |
8960 	    IEEE80211_EHT_PHY_CAP3_CODEBOOK_7_5_MU_FDBK |
8961 	    IEEE80211_EHT_PHY_CAP3_TRIG_MU_BF_PART_BW_FDBK |
8962 	    IEEE80211_EHT_PHY_CAP3_TRIG_CQI_FDBK;
8963 	eht_cap_elem->phy_cap_info[3] &= ~m;
8964 
8965 	m = IEEE80211_EHT_PHY_CAP4_PART_BW_DL_MU_MIMO |
8966 	    IEEE80211_EHT_PHY_CAP4_PSR_SR_SUPP |
8967 	    IEEE80211_EHT_PHY_CAP4_POWER_BOOST_FACT_SUPP |
8968 	    IEEE80211_EHT_PHY_CAP4_EHT_MU_PPDU_4_EHT_LTF_08_GI;
8969 	eht_cap_elem->phy_cap_info[4] &= ~m;
8970 
8971 	m = IEEE80211_EHT_PHY_CAP5_NON_TRIG_CQI_FEEDBACK |
8972 	    IEEE80211_EHT_PHY_CAP5_TX_LESS_242_TONE_RU_SUPP |
8973 	    IEEE80211_EHT_PHY_CAP5_RX_LESS_242_TONE_RU_SUPP |
8974 	    IEEE80211_EHT_PHY_CAP5_MAX_NUM_SUPP_EHT_LTF_MASK;
8975 	eht_cap_elem->phy_cap_info[5] &= ~m;
8976 
8977 	m = IEEE80211_EHT_PHY_CAP6_MAX_NUM_SUPP_EHT_LTF_MASK;
8978 	eht_cap_elem->phy_cap_info[6] &= ~m;
8979 
8980 	m = IEEE80211_EHT_PHY_CAP7_NON_OFDMA_UL_MU_MIMO_80MHZ |
8981 	    IEEE80211_EHT_PHY_CAP7_NON_OFDMA_UL_MU_MIMO_160MHZ |
8982 	    IEEE80211_EHT_PHY_CAP7_NON_OFDMA_UL_MU_MIMO_320MHZ |
8983 	    IEEE80211_EHT_PHY_CAP7_MU_BEAMFORMER_80MHZ |
8984 	    IEEE80211_EHT_PHY_CAP7_MU_BEAMFORMER_160MHZ |
8985 	    IEEE80211_EHT_PHY_CAP7_MU_BEAMFORMER_320MHZ;
8986 	eht_cap_elem->phy_cap_info[7] &= ~m;
8987 }
8988 
8989 static void ath12k_mac_copy_eht_cap(struct ath12k *ar,
8990 				    struct ath12k_band_cap *band_cap,
8991 				    struct ieee80211_he_cap_elem *he_cap_elem,
8992 				    int iftype,
8993 				    struct ieee80211_sta_eht_cap *eht_cap)
8994 {
8995 	struct ieee80211_eht_cap_elem_fixed *eht_cap_elem = &eht_cap->eht_cap_elem;
8996 
8997 	memset(eht_cap, 0, sizeof(struct ieee80211_sta_eht_cap));
8998 
8999 	if (!(test_bit(WMI_TLV_SERVICE_11BE, ar->ab->wmi_ab.svc_map)) ||
9000 	    ath12k_acpi_get_disable_11be(ar->ab))
9001 		return;
9002 
9003 	eht_cap->has_eht = true;
9004 	memcpy(eht_cap_elem->mac_cap_info, band_cap->eht_cap_mac_info,
9005 	       sizeof(eht_cap_elem->mac_cap_info));
9006 	memcpy(eht_cap_elem->phy_cap_info, band_cap->eht_cap_phy_info,
9007 	       sizeof(eht_cap_elem->phy_cap_info));
9008 
9009 	switch (iftype) {
9010 	case NL80211_IFTYPE_AP:
9011 		eht_cap_elem->phy_cap_info[0] &=
9012 			~IEEE80211_EHT_PHY_CAP0_242_TONE_RU_GT20MHZ;
9013 		eht_cap_elem->phy_cap_info[4] &=
9014 			~IEEE80211_EHT_PHY_CAP4_PART_BW_DL_MU_MIMO;
9015 		eht_cap_elem->phy_cap_info[5] &=
9016 			~IEEE80211_EHT_PHY_CAP5_TX_LESS_242_TONE_RU_SUPP;
9017 		break;
9018 	case NL80211_IFTYPE_STATION:
9019 		eht_cap_elem->phy_cap_info[7] &=
9020 			~(IEEE80211_EHT_PHY_CAP7_NON_OFDMA_UL_MU_MIMO_80MHZ |
9021 			  IEEE80211_EHT_PHY_CAP7_NON_OFDMA_UL_MU_MIMO_160MHZ |
9022 			  IEEE80211_EHT_PHY_CAP7_NON_OFDMA_UL_MU_MIMO_320MHZ);
9023 		eht_cap_elem->phy_cap_info[7] &=
9024 			~(IEEE80211_EHT_PHY_CAP7_MU_BEAMFORMER_80MHZ |
9025 			  IEEE80211_EHT_PHY_CAP7_MU_BEAMFORMER_160MHZ |
9026 			  IEEE80211_EHT_PHY_CAP7_MU_BEAMFORMER_320MHZ);
9027 		break;
9028 	case NL80211_IFTYPE_MESH_POINT:
9029 		ath12k_mac_filter_eht_cap_mesh(eht_cap_elem);
9030 		break;
9031 	default:
9032 		break;
9033 	}
9034 
9035 	ath12k_mac_copy_eht_mcs_nss(band_cap, &eht_cap->eht_mcs_nss_supp,
9036 				    he_cap_elem, eht_cap_elem);
9037 
9038 	if (eht_cap_elem->phy_cap_info[5] &
9039 	    IEEE80211_EHT_PHY_CAP5_PPE_THRESHOLD_PRESENT)
9040 		ath12k_mac_copy_eht_ppe_thresh(&band_cap->eht_ppet, eht_cap);
9041 }
9042 
9043 static int ath12k_mac_copy_sband_iftype_data(struct ath12k *ar,
9044 					     struct ath12k_pdev_cap *cap,
9045 					     struct ieee80211_sband_iftype_data *data,
9046 					     int band)
9047 {
9048 	struct ath12k_band_cap *band_cap = &cap->band[band];
9049 	int i, idx = 0;
9050 
9051 	for (i = 0; i < NUM_NL80211_IFTYPES; i++) {
9052 		struct ieee80211_sta_he_cap *he_cap = &data[idx].he_cap;
9053 
9054 		switch (i) {
9055 		case NL80211_IFTYPE_STATION:
9056 		case NL80211_IFTYPE_AP:
9057 		case NL80211_IFTYPE_MESH_POINT:
9058 			break;
9059 
9060 		default:
9061 			continue;
9062 		}
9063 
9064 		data[idx].types_mask = BIT(i);
9065 
9066 		ath12k_mac_copy_he_cap(ar, band_cap, i, ar->num_tx_chains, he_cap);
9067 		if (band == NL80211_BAND_6GHZ) {
9068 			data[idx].he_6ghz_capa.capa =
9069 				ath12k_mac_setup_he_6ghz_cap(cap, band_cap);
9070 		}
9071 		ath12k_mac_copy_eht_cap(ar, band_cap, &he_cap->he_cap_elem, i,
9072 					&data[idx].eht_cap);
9073 		idx++;
9074 	}
9075 
9076 	return idx;
9077 }
9078 
9079 static void ath12k_mac_setup_sband_iftype_data(struct ath12k *ar,
9080 					       struct ath12k_pdev_cap *cap)
9081 {
9082 	struct ieee80211_supported_band *sband;
9083 	enum nl80211_band band;
9084 	int count;
9085 
9086 	if (cap->supported_bands & WMI_HOST_WLAN_2GHZ_CAP) {
9087 		band = NL80211_BAND_2GHZ;
9088 		count = ath12k_mac_copy_sband_iftype_data(ar, cap,
9089 							  ar->mac.iftype[band],
9090 							  band);
9091 		sband = &ar->mac.sbands[band];
9092 		_ieee80211_set_sband_iftype_data(sband, ar->mac.iftype[band],
9093 						 count);
9094 	}
9095 
9096 	if (cap->supported_bands & WMI_HOST_WLAN_5GHZ_CAP) {
9097 		band = NL80211_BAND_5GHZ;
9098 		count = ath12k_mac_copy_sband_iftype_data(ar, cap,
9099 							  ar->mac.iftype[band],
9100 							  band);
9101 		sband = &ar->mac.sbands[band];
9102 		_ieee80211_set_sband_iftype_data(sband, ar->mac.iftype[band],
9103 						 count);
9104 	}
9105 
9106 	if (cap->supported_bands & WMI_HOST_WLAN_5GHZ_CAP &&
9107 	    ar->supports_6ghz) {
9108 		band = NL80211_BAND_6GHZ;
9109 		count = ath12k_mac_copy_sband_iftype_data(ar, cap,
9110 							  ar->mac.iftype[band],
9111 							  band);
9112 		sband = &ar->mac.sbands[band];
9113 		_ieee80211_set_sband_iftype_data(sband, ar->mac.iftype[band],
9114 						 count);
9115 	}
9116 }
9117 
9118 static int __ath12k_set_antenna(struct ath12k *ar, u32 tx_ant, u32 rx_ant)
9119 {
9120 	struct ath12k_hw *ah = ath12k_ar_to_ah(ar);
9121 	int ret;
9122 
9123 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
9124 
9125 	if (ath12k_check_chain_mask(ar, tx_ant, true))
9126 		return -EINVAL;
9127 
9128 	if (ath12k_check_chain_mask(ar, rx_ant, false))
9129 		return -EINVAL;
9130 
9131 	/* Since we advertised the max cap of all radios combined during wiphy
9132 	 * registration, ensure we don't set the antenna config higher than the
9133 	 * limits
9134 	 */
9135 	tx_ant = min_t(u32, tx_ant, ar->pdev->cap.tx_chain_mask);
9136 	rx_ant = min_t(u32, rx_ant, ar->pdev->cap.rx_chain_mask);
9137 
9138 	ar->cfg_tx_chainmask = tx_ant;
9139 	ar->cfg_rx_chainmask = rx_ant;
9140 
9141 	if (ah->state != ATH12K_HW_STATE_ON &&
9142 	    ah->state != ATH12K_HW_STATE_RESTARTED)
9143 		return 0;
9144 
9145 	ret = ath12k_wmi_pdev_set_param(ar, WMI_PDEV_PARAM_TX_CHAIN_MASK,
9146 					tx_ant, ar->pdev->pdev_id);
9147 	if (ret) {
9148 		ath12k_warn(ar->ab, "failed to set tx-chainmask: %d, req 0x%x\n",
9149 			    ret, tx_ant);
9150 		return ret;
9151 	}
9152 
9153 	ar->num_tx_chains = hweight32(tx_ant);
9154 
9155 	ret = ath12k_wmi_pdev_set_param(ar, WMI_PDEV_PARAM_RX_CHAIN_MASK,
9156 					rx_ant, ar->pdev->pdev_id);
9157 	if (ret) {
9158 		ath12k_warn(ar->ab, "failed to set rx-chainmask: %d, req 0x%x\n",
9159 			    ret, rx_ant);
9160 		return ret;
9161 	}
9162 
9163 	ar->num_rx_chains = hweight32(rx_ant);
9164 
9165 	/* Reload HT/VHT/HE capability */
9166 	ath12k_mac_setup_ht_vht_cap(ar, &ar->pdev->cap, NULL);
9167 	ath12k_mac_setup_sband_iftype_data(ar, &ar->pdev->cap);
9168 
9169 	return 0;
9170 }
9171 
9172 static void ath12k_mgmt_over_wmi_tx_drop(struct ath12k *ar, struct sk_buff *skb)
9173 {
9174 	int num_mgmt;
9175 
9176 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
9177 
9178 	ieee80211_free_txskb(ath12k_ar_to_hw(ar), skb);
9179 
9180 	num_mgmt = atomic_dec_if_positive(&ar->num_pending_mgmt_tx);
9181 
9182 	if (num_mgmt < 0)
9183 		WARN_ON_ONCE(1);
9184 
9185 	if (!num_mgmt)
9186 		wake_up(&ar->txmgmt_empty_waitq);
9187 }
9188 
9189 static void ath12k_mac_tx_mgmt_free(struct ath12k *ar, int buf_id)
9190 {
9191 	struct sk_buff *msdu;
9192 	struct ieee80211_tx_info *info;
9193 
9194 	spin_lock_bh(&ar->txmgmt_idr_lock);
9195 	msdu = idr_remove(&ar->txmgmt_idr, buf_id);
9196 	spin_unlock_bh(&ar->txmgmt_idr_lock);
9197 
9198 	if (!msdu)
9199 		return;
9200 
9201 	dma_unmap_single(ar->ab->dev, ATH12K_SKB_CB(msdu)->paddr, msdu->len,
9202 			 DMA_TO_DEVICE);
9203 
9204 	info = IEEE80211_SKB_CB(msdu);
9205 	memset(&info->status, 0, sizeof(info->status));
9206 
9207 	ath12k_mgmt_over_wmi_tx_drop(ar, msdu);
9208 }
9209 
9210 void ath12k_mac_tx_mgmt_pending_free(struct ath12k *ar)
9211 {
9212 	struct sk_buff *msdu;
9213 	int buf_id;
9214 
9215 	idr_for_each_entry(&ar->txmgmt_idr, msdu, buf_id)
9216 		ath12k_mac_tx_mgmt_free(ar, buf_id);
9217 }
9218 
9219 static void ath12k_mac_vif_txmgmt_cleanup(struct ath12k *ar,
9220 					  struct ieee80211_vif *vif)
9221 {
9222 	struct ath12k_skb_cb *skb_cb;
9223 	struct sk_buff *skb;
9224 	int buf_id;
9225 
9226 	idr_for_each_entry(&ar->txmgmt_idr, skb, buf_id) {
9227 		skb_cb = ATH12K_SKB_CB(skb);
9228 		if (skb_cb->vif == vif)
9229 			ath12k_mac_tx_mgmt_free(ar, buf_id);
9230 	}
9231 }
9232 
9233 static int ath12k_mac_mgmt_tx_wmi(struct ath12k *ar, struct ath12k_link_vif *arvif,
9234 				  struct sk_buff *skb)
9235 {
9236 	struct ath12k_base *ab = ar->ab;
9237 	struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
9238 	struct ath12k_skb_cb *skb_cb = ATH12K_SKB_CB(skb);
9239 	struct ieee80211_tx_info *info;
9240 	enum hal_encrypt_type enctype;
9241 	unsigned int mic_len;
9242 	dma_addr_t paddr;
9243 	int buf_id;
9244 	int ret;
9245 
9246 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
9247 
9248 	skb_cb->ar = ar;
9249 	spin_lock_bh(&ar->txmgmt_idr_lock);
9250 	buf_id = idr_alloc(&ar->txmgmt_idr, skb, 0,
9251 			   ATH12K_TX_MGMT_NUM_PENDING_MAX, GFP_ATOMIC);
9252 	spin_unlock_bh(&ar->txmgmt_idr_lock);
9253 	if (buf_id < 0)
9254 		return -ENOSPC;
9255 
9256 	info = IEEE80211_SKB_CB(skb);
9257 	if ((skb_cb->flags & ATH12K_SKB_CIPHER_SET) &&
9258 	    !(info->flags & IEEE80211_TX_CTL_HW_80211_ENCAP)) {
9259 		if ((ieee80211_is_action(hdr->frame_control) ||
9260 		     ieee80211_is_deauth(hdr->frame_control) ||
9261 		     ieee80211_is_disassoc(hdr->frame_control)) &&
9262 		     ieee80211_has_protected(hdr->frame_control)) {
9263 			enctype = ath12k_dp_tx_get_encrypt_type(skb_cb->cipher);
9264 			mic_len = ath12k_dp_rx_crypto_mic_len(ab->dp, enctype);
9265 			skb_put(skb, mic_len);
9266 		}
9267 	}
9268 
9269 	paddr = dma_map_single(ab->dev, skb->data, skb->len, DMA_TO_DEVICE);
9270 	if (dma_mapping_error(ab->dev, paddr)) {
9271 		ath12k_warn(ab, "failed to DMA map mgmt Tx buffer\n");
9272 		ret = -EIO;
9273 		goto err_free_idr;
9274 	}
9275 
9276 	skb_cb->paddr = paddr;
9277 
9278 	ret = ath12k_wmi_mgmt_send(arvif, buf_id, skb);
9279 	if (ret) {
9280 		ath12k_warn(ar->ab, "failed to send mgmt frame: %d\n", ret);
9281 		goto err_unmap_buf;
9282 	}
9283 
9284 	return 0;
9285 
9286 err_unmap_buf:
9287 	dma_unmap_single(ab->dev, skb_cb->paddr,
9288 			 skb->len, DMA_TO_DEVICE);
9289 err_free_idr:
9290 	spin_lock_bh(&ar->txmgmt_idr_lock);
9291 	idr_remove(&ar->txmgmt_idr, buf_id);
9292 	spin_unlock_bh(&ar->txmgmt_idr_lock);
9293 
9294 	return ret;
9295 }
9296 
9297 static void ath12k_mgmt_over_wmi_tx_purge(struct ath12k *ar)
9298 {
9299 	struct sk_buff *skb;
9300 
9301 	while ((skb = skb_dequeue(&ar->wmi_mgmt_tx_queue)) != NULL)
9302 		ath12k_mgmt_over_wmi_tx_drop(ar, skb);
9303 }
9304 
9305 static int ath12k_mac_mgmt_action_frame_fill_elem_data(struct ath12k_link_vif *arvif,
9306 						       struct sk_buff *skb)
9307 {
9308 	struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
9309 	u8 category, *buf, iv_len, action_code, dialog_token;
9310 	struct ieee80211_bss_conf *link_conf;
9311 	struct ieee80211_chanctx_conf *conf;
9312 	int cur_tx_power, max_tx_power;
9313 	struct ath12k *ar = arvif->ar;
9314 	struct ieee80211_hw *hw = ath12k_ar_to_hw(ar);
9315 	struct wiphy *wiphy = hw->wiphy;
9316 	struct ath12k_skb_cb *skb_cb;
9317 	struct ieee80211_mgmt *mgmt;
9318 	unsigned int remaining_len;
9319 	bool has_protected;
9320 
9321 	lockdep_assert_wiphy(wiphy);
9322 
9323 	/* make sure category field is present */
9324 	if (skb->len < IEEE80211_MIN_ACTION_SIZE(category))
9325 		return -EINVAL;
9326 
9327 	remaining_len = skb->len - IEEE80211_MIN_ACTION_SIZE(category);
9328 	has_protected = ieee80211_has_protected(hdr->frame_control);
9329 
9330 	/* In case of SW crypto and hdr protected (PMF), packet will already be encrypted,
9331 	 * we can't put in data in this case
9332 	 */
9333 	if (test_bit(ATH12K_FLAG_HW_CRYPTO_DISABLED, &ar->ab->dev_flags) &&
9334 	    has_protected)
9335 		return 0;
9336 
9337 	mgmt = (struct ieee80211_mgmt *)hdr;
9338 	buf = (u8 *)&mgmt->u.action;
9339 
9340 	/* FCTL_PROTECTED frame might have extra space added for HDR_LEN. Offset that
9341 	 * many bytes if it is there
9342 	 */
9343 	if (has_protected) {
9344 		skb_cb = ATH12K_SKB_CB(skb);
9345 
9346 		switch (skb_cb->cipher) {
9347 		/* Cipher suite having flag %IEEE80211_KEY_FLAG_GENERATE_IV_MGMT set in
9348 		 * key needs to be processed. See ath12k_install_key()
9349 		 */
9350 		case WLAN_CIPHER_SUITE_CCMP:
9351 		case WLAN_CIPHER_SUITE_CCMP_256:
9352 		case WLAN_CIPHER_SUITE_GCMP:
9353 		case WLAN_CIPHER_SUITE_GCMP_256:
9354 			iv_len = IEEE80211_CCMP_HDR_LEN;
9355 			break;
9356 		case WLAN_CIPHER_SUITE_TKIP:
9357 			iv_len = 0;
9358 			break;
9359 		default:
9360 			return -EINVAL;
9361 		}
9362 
9363 		if (remaining_len < iv_len)
9364 			return -EINVAL;
9365 
9366 		buf += iv_len;
9367 		remaining_len -= iv_len;
9368 	}
9369 
9370 	category = *buf++;
9371 	/* category code is already taken care in %IEEE80211_MIN_ACTION_SIZE hence
9372 	 * no need to adjust remaining_len
9373 	 */
9374 
9375 	switch (category) {
9376 	case WLAN_CATEGORY_RADIO_MEASUREMENT:
9377 		/* need action code and dialog token */
9378 		if (remaining_len < 2)
9379 			return -EINVAL;
9380 
9381 		/* Packet Format:
9382 		 *	Action Code | Dialog Token | Variable Len (based on Action Code)
9383 		 */
9384 		action_code = *buf++;
9385 		dialog_token = *buf++;
9386 		remaining_len -= 2;
9387 
9388 		link_conf = ath12k_mac_get_link_bss_conf(arvif);
9389 		if (!link_conf) {
9390 			ath12k_warn(ar->ab,
9391 				    "failed to get bss link conf for vdev %d in RM handling\n",
9392 				    arvif->vdev_id);
9393 			return -EINVAL;
9394 		}
9395 
9396 		conf = wiphy_dereference(wiphy, link_conf->chanctx_conf);
9397 		if (!conf)
9398 			return -ENOENT;
9399 
9400 		cur_tx_power = link_conf->txpower;
9401 		max_tx_power = min(conf->def.chan->max_reg_power,
9402 				   (int)ar->max_tx_power / 2);
9403 
9404 		ath12k_mac_op_get_txpower(hw, arvif->ahvif->vif, arvif->link_id,
9405 					  &cur_tx_power);
9406 
9407 		switch (action_code) {
9408 		case WLAN_RM_ACTION_LINK_MEASUREMENT_REQUEST:
9409 			/* need variable fields to be present in len */
9410 			if (remaining_len < 2)
9411 				return -EINVAL;
9412 
9413 			/* Variable length format as defined in IEEE 802.11-2024,
9414 			 * Figure 9-1187-Link Measurement Request frame Action field
9415 			 * format.
9416 			 *	Transmit Power | Max Tx Power
9417 			 * We fill both of these.
9418 			 */
9419 			*buf++ = cur_tx_power;
9420 			*buf = max_tx_power;
9421 
9422 			ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
9423 				   "RRM: Link Measurement Req dialog_token %u cur_tx_power %d max_tx_power %d\n",
9424 				   dialog_token, cur_tx_power, max_tx_power);
9425 			break;
9426 		case WLAN_RM_ACTION_LINK_MEASUREMENT_REPORT:
9427 			/* need variable fields to be present in len */
9428 			if (remaining_len < 3)
9429 				return -EINVAL;
9430 
9431 			/* Variable length format as defined in IEEE 802.11-2024,
9432 			 * Figure 9-1188-Link Measurement Report frame Action field format
9433 			 *	TPC Report | Variable Fields
9434 			 *
9435 			 * TPC Report Format:
9436 			 *	Element ID | Len | Tx Power | Link Margin
9437 			 *
9438 			 * We fill Tx power in the TPC Report (2nd index)
9439 			 */
9440 			buf[2] = cur_tx_power;
9441 
9442 			/* TODO: At present, Link margin data is not present so can't
9443 			 * really fill it now. Once it is available, it can be added
9444 			 * here
9445 			 */
9446 			ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
9447 				   "RRM: Link Measurement Report dialog_token %u cur_tx_power %d\n",
9448 				   dialog_token, cur_tx_power);
9449 			break;
9450 		default:
9451 			return -EINVAL;
9452 		}
9453 		break;
9454 	default:
9455 		/* nothing to fill */
9456 		return 0;
9457 	}
9458 
9459 	return 0;
9460 }
9461 
9462 static int ath12k_mac_mgmt_frame_fill_elem_data(struct ath12k_link_vif *arvif,
9463 						struct sk_buff *skb)
9464 {
9465 	struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
9466 
9467 	if (!ieee80211_is_action(hdr->frame_control))
9468 		return 0;
9469 
9470 	return ath12k_mac_mgmt_action_frame_fill_elem_data(arvif, skb);
9471 }
9472 
9473 static void ath12k_mgmt_over_wmi_tx_work(struct wiphy *wiphy, struct wiphy_work *work)
9474 {
9475 	struct ath12k *ar = container_of(work, struct ath12k, wmi_mgmt_tx_work);
9476 	struct ath12k_hw *ah = ar->ah;
9477 	struct ath12k_skb_cb *skb_cb;
9478 	struct ath12k_vif *ahvif;
9479 	struct ath12k_link_vif *arvif;
9480 	struct sk_buff *skb;
9481 	int ret;
9482 
9483 	lockdep_assert_wiphy(wiphy);
9484 
9485 	while ((skb = skb_dequeue(&ar->wmi_mgmt_tx_queue)) != NULL) {
9486 		skb_cb = ATH12K_SKB_CB(skb);
9487 		if (!skb_cb->vif) {
9488 			ath12k_warn(ar->ab, "no vif found for mgmt frame\n");
9489 			ath12k_mgmt_over_wmi_tx_drop(ar, skb);
9490 			continue;
9491 		}
9492 
9493 		ahvif = ath12k_vif_to_ahvif(skb_cb->vif);
9494 		if (!(ahvif->links_map & BIT(skb_cb->link_id))) {
9495 			ath12k_warn(ar->ab,
9496 				    "invalid linkid %u in mgmt over wmi tx with linkmap 0x%x\n",
9497 				    skb_cb->link_id, ahvif->links_map);
9498 			ath12k_mgmt_over_wmi_tx_drop(ar, skb);
9499 			continue;
9500 		}
9501 
9502 		arvif = wiphy_dereference(ah->hw->wiphy, ahvif->link[skb_cb->link_id]);
9503 		if (ar->allocated_vdev_map & (1LL << arvif->vdev_id)) {
9504 			/* Fill in the data which is required to be filled by the driver
9505 			 * For example: Max Tx power in Link Measurement Request/Report
9506 			 */
9507 			ret = ath12k_mac_mgmt_frame_fill_elem_data(arvif, skb);
9508 			if (ret) {
9509 				/* If we couldn't fill the data due to any reason,
9510 				 * let's not discard transmitting the packet.
9511 				 * For example: Software crypto and PMF case
9512 				 */
9513 				ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
9514 					   "Failed to fill the required data for the mgmt packet err %d\n",
9515 					   ret);
9516 			}
9517 
9518 			ret = ath12k_mac_mgmt_tx_wmi(ar, arvif, skb);
9519 			if (ret) {
9520 				ath12k_warn(ar->ab, "failed to tx mgmt frame, vdev_id %d :%d\n",
9521 					    arvif->vdev_id, ret);
9522 				ath12k_mgmt_over_wmi_tx_drop(ar, skb);
9523 			}
9524 		} else {
9525 			ath12k_warn(ar->ab,
9526 				    "dropping mgmt frame for vdev %d link %u is_started %d\n",
9527 				    arvif->vdev_id,
9528 				    skb_cb->link_id,
9529 				    arvif->is_started);
9530 			ath12k_mgmt_over_wmi_tx_drop(ar, skb);
9531 		}
9532 	}
9533 }
9534 
9535 int ath12k_mac_mgmt_tx(struct ath12k *ar, struct sk_buff *skb,
9536 		       bool is_prb_rsp)
9537 {
9538 	struct sk_buff_head *q = &ar->wmi_mgmt_tx_queue;
9539 
9540 	if (test_bit(ATH12K_FLAG_CRASH_FLUSH, &ar->ab->dev_flags))
9541 		return -ESHUTDOWN;
9542 
9543 	/* Drop probe response packets when the pending management tx
9544 	 * count has reached a certain threshold, so as to prioritize
9545 	 * other mgmt packets like auth and assoc to be sent on time
9546 	 * for establishing successful connections.
9547 	 */
9548 	if (is_prb_rsp &&
9549 	    atomic_read(&ar->num_pending_mgmt_tx) > ATH12K_PRB_RSP_DROP_THRESHOLD) {
9550 		ath12k_warn(ar->ab,
9551 			    "dropping probe response as pending queue is almost full\n");
9552 		return -ENOSPC;
9553 	}
9554 
9555 	if (skb_queue_len_lockless(q) >= ATH12K_TX_MGMT_NUM_PENDING_MAX) {
9556 		ath12k_warn(ar->ab, "mgmt tx queue is full\n");
9557 		return -ENOSPC;
9558 	}
9559 
9560 	skb_queue_tail(q, skb);
9561 	atomic_inc(&ar->num_pending_mgmt_tx);
9562 	wiphy_work_queue(ath12k_ar_to_hw(ar)->wiphy, &ar->wmi_mgmt_tx_work);
9563 
9564 	return 0;
9565 }
9566 EXPORT_SYMBOL(ath12k_mac_mgmt_tx);
9567 
9568 void ath12k_mac_add_p2p_noa_ie(struct ath12k *ar,
9569 			       struct ieee80211_vif *vif,
9570 			       struct sk_buff *skb,
9571 			       bool is_prb_rsp)
9572 {
9573 	struct ath12k_vif *ahvif = ath12k_vif_to_ahvif(vif);
9574 
9575 	if (likely(!is_prb_rsp))
9576 		return;
9577 
9578 	spin_lock_bh(&ar->data_lock);
9579 
9580 	if (ahvif->u.ap.noa_data &&
9581 	    !pskb_expand_head(skb, 0, ahvif->u.ap.noa_len,
9582 			      GFP_ATOMIC))
9583 		skb_put_data(skb, ahvif->u.ap.noa_data,
9584 			     ahvif->u.ap.noa_len);
9585 
9586 	spin_unlock_bh(&ar->data_lock);
9587 }
9588 EXPORT_SYMBOL(ath12k_mac_add_p2p_noa_ie);
9589 
9590 /* Note: called under rcu_read_lock() */
9591 void ath12k_mlo_mcast_update_tx_link_address(struct ieee80211_vif *vif,
9592 					     u8 link_id, struct sk_buff *skb,
9593 					     u32 info_flags)
9594 {
9595 	struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
9596 	struct ieee80211_bss_conf *bss_conf;
9597 
9598 	if (info_flags & IEEE80211_TX_CTL_HW_80211_ENCAP)
9599 		return;
9600 
9601 	bss_conf = rcu_dereference(vif->link_conf[link_id]);
9602 	if (bss_conf)
9603 		ether_addr_copy(hdr->addr2, bss_conf->addr);
9604 }
9605 EXPORT_SYMBOL(ath12k_mlo_mcast_update_tx_link_address);
9606 
9607 /* Note: called under rcu_read_lock() */
9608 u8 ath12k_mac_get_tx_link(struct ieee80211_sta *sta, struct ieee80211_vif *vif,
9609 			  u8 link, struct sk_buff *skb, u32 info_flags)
9610 {
9611 	struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
9612 	struct ath12k_vif *ahvif = ath12k_vif_to_ahvif(vif);
9613 	struct ieee80211_link_sta *link_sta;
9614 	struct ieee80211_bss_conf *bss_conf;
9615 	struct ath12k_sta *ahsta;
9616 
9617 	/* Use the link id passed or the default vif link */
9618 	if (!sta) {
9619 		if (link != IEEE80211_LINK_UNSPECIFIED)
9620 			return link;
9621 
9622 		return ahvif->deflink.link_id;
9623 	}
9624 
9625 	ahsta = ath12k_sta_to_ahsta(sta);
9626 
9627 	/* Below translation ensures we pass proper A2 & A3 for non ML clients.
9628 	 * Also it assumes for now support only for MLO AP in this path
9629 	 */
9630 	if (!sta->mlo) {
9631 		link = ahsta->deflink.link_id;
9632 
9633 		if (info_flags & IEEE80211_TX_CTL_HW_80211_ENCAP)
9634 			return link;
9635 
9636 		bss_conf = rcu_dereference(vif->link_conf[link]);
9637 		if (bss_conf) {
9638 			ether_addr_copy(hdr->addr2, bss_conf->addr);
9639 			if (!ieee80211_has_tods(hdr->frame_control) &&
9640 			    !ieee80211_has_fromds(hdr->frame_control))
9641 				ether_addr_copy(hdr->addr3, bss_conf->addr);
9642 		}
9643 
9644 		return link;
9645 	}
9646 
9647 	/* enqueue eth enacap & data frames on primary link, FW does link
9648 	 * selection and address translation.
9649 	 */
9650 	if (info_flags & IEEE80211_TX_CTL_HW_80211_ENCAP ||
9651 	    ieee80211_is_data(hdr->frame_control))
9652 		return ahsta->assoc_link_id;
9653 
9654 	/* 802.11 frame cases */
9655 	if (link == IEEE80211_LINK_UNSPECIFIED)
9656 		link = ahsta->deflink.link_id;
9657 
9658 	if (!ieee80211_is_mgmt(hdr->frame_control))
9659 		return link;
9660 
9661 	/* Perform address conversion for ML STA Tx */
9662 	bss_conf = rcu_dereference(vif->link_conf[link]);
9663 	link_sta = rcu_dereference(sta->link[link]);
9664 
9665 	if (bss_conf && link_sta) {
9666 		ether_addr_copy(hdr->addr1, link_sta->addr);
9667 		ether_addr_copy(hdr->addr2, bss_conf->addr);
9668 
9669 		if (vif->type == NL80211_IFTYPE_STATION && bss_conf->bssid)
9670 			ether_addr_copy(hdr->addr3, bss_conf->bssid);
9671 		else if (vif->type == NL80211_IFTYPE_AP)
9672 			ether_addr_copy(hdr->addr3, bss_conf->addr);
9673 
9674 		return link;
9675 	}
9676 
9677 	if (bss_conf) {
9678 		/* In certain cases where a ML sta associated and added subset of
9679 		 * links on which the ML AP is active, but now sends some frame
9680 		 * (ex. Probe request) on a different link which is active in our
9681 		 * MLD but was not added during previous association, we can
9682 		 * still honor the Tx to that ML STA via the requested link.
9683 		 * The control would reach here in such case only when that link
9684 		 * address is same as the MLD address or in worst case clients
9685 		 * used MLD address at TA wrongly which would have helped
9686 		 * identify the ML sta object and pass it here.
9687 		 * If the link address of that STA is different from MLD address,
9688 		 * then the sta object would be NULL and control won't reach
9689 		 * here but return at the start of the function itself with !sta
9690 		 * check. Also this would not need any translation at hdr->addr1
9691 		 * from MLD to link address since the RA is the MLD address
9692 		 * (same as that link address ideally) already.
9693 		 */
9694 		ether_addr_copy(hdr->addr2, bss_conf->addr);
9695 
9696 		if (vif->type == NL80211_IFTYPE_STATION && bss_conf->bssid)
9697 			ether_addr_copy(hdr->addr3, bss_conf->bssid);
9698 		else if (vif->type == NL80211_IFTYPE_AP)
9699 			ether_addr_copy(hdr->addr3, bss_conf->addr);
9700 	}
9701 
9702 	return link;
9703 }
9704 EXPORT_SYMBOL(ath12k_mac_get_tx_link);
9705 
9706 void ath12k_mac_drain_tx(struct ath12k *ar)
9707 {
9708 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
9709 
9710 	/* make sure rcu-protected mac80211 tx path itself is drained */
9711 	synchronize_net();
9712 
9713 	wiphy_work_cancel(ath12k_ar_to_hw(ar)->wiphy, &ar->wmi_mgmt_tx_work);
9714 	ath12k_mgmt_over_wmi_tx_purge(ar);
9715 }
9716 
9717 static int ath12k_mac_config_mon_status_default(struct ath12k *ar, bool enable)
9718 {
9719 	struct htt_rx_ring_tlv_filter tlv_filter = {};
9720 	struct ath12k_base *ab = ar->ab;
9721 	u32 ring_id, i;
9722 	int ret = 0;
9723 
9724 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
9725 
9726 	if (!ab->hw_params->rxdma1_enable)
9727 		return ret;
9728 
9729 	if (enable) {
9730 		tlv_filter = ath12k_mac_mon_status_filter_default;
9731 
9732 		if (ath12k_debugfs_rx_filter(ar))
9733 			tlv_filter.rx_filter = ath12k_debugfs_rx_filter(ar);
9734 	} else {
9735 		tlv_filter.rxmon_disable = true;
9736 	}
9737 
9738 	for (i = 0; i < ab->hw_params->num_rxdma_per_pdev; i++) {
9739 		ring_id = ar->dp.rxdma_mon_dst_ring[i].ring_id;
9740 		ret = ath12k_dp_tx_htt_rx_filter_setup(ab, ring_id,
9741 						       ar->dp.mac_id + i,
9742 						       HAL_RXDMA_MONITOR_DST,
9743 						       DP_RXDMA_REFILL_RING_SIZE,
9744 						       &tlv_filter);
9745 		if (ret) {
9746 			ath12k_err(ab,
9747 				   "failed to setup filter for monitor buf %d\n",
9748 				   ret);
9749 		}
9750 	}
9751 
9752 	return ret;
9753 }
9754 
9755 static int ath12k_mac_start(struct ath12k *ar)
9756 {
9757 	const struct ath12k_dp_profile_params *dp_params;
9758 	struct ath12k_hw *ah = ar->ah;
9759 	struct ath12k_base *ab = ar->ab;
9760 	struct ath12k_pdev *pdev = ar->pdev;
9761 	int ret;
9762 
9763 	lockdep_assert_held(&ah->hw_mutex);
9764 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
9765 
9766 	dp_params = &ab->profile_param->dp_params;
9767 
9768 	ret = ath12k_wmi_pdev_set_param(ar, WMI_PDEV_PARAM_PMF_QOS,
9769 					1, pdev->pdev_id);
9770 
9771 	if (ret) {
9772 		ath12k_err(ab, "failed to enable PMF QOS: %d\n", ret);
9773 		goto err;
9774 	}
9775 
9776 	ret = ath12k_wmi_pdev_set_param(ar, WMI_PDEV_PARAM_DYNAMIC_BW, 1,
9777 					pdev->pdev_id);
9778 	if (ret) {
9779 		ath12k_err(ab, "failed to enable dynamic bw: %d\n", ret);
9780 		goto err;
9781 	}
9782 
9783 	ret = ath12k_wmi_pdev_set_param(ar, WMI_PDEV_PARAM_ARP_AC_OVERRIDE,
9784 					0, pdev->pdev_id);
9785 	if (ret) {
9786 		ath12k_err(ab, "failed to set ac override for ARP: %d\n",
9787 			   ret);
9788 		goto err;
9789 	}
9790 
9791 	ret = ath12k_wmi_send_dfs_phyerr_offload_enable_cmd(ar, pdev->pdev_id);
9792 	if (ret) {
9793 		ath12k_err(ab, "failed to offload radar detection: %d\n",
9794 			   ret);
9795 		goto err;
9796 	}
9797 
9798 	ret = ath12k_dp_tx_htt_h2t_ppdu_stats_req(ar,
9799 						  HTT_PPDU_STATS_TAG_DEFAULT);
9800 	if (ret) {
9801 		ath12k_err(ab, "failed to req ppdu stats: %d\n", ret);
9802 		goto err;
9803 	}
9804 
9805 	ret = ath12k_wmi_pdev_set_param(ar, WMI_PDEV_PARAM_MESH_MCAST_ENABLE,
9806 					1, pdev->pdev_id);
9807 
9808 	if (ret) {
9809 		ath12k_err(ab, "failed to enable MESH MCAST ENABLE: (%d\n", ret);
9810 		goto err;
9811 	}
9812 
9813 	if (ab->hw_params->supports_cong_ctrl_max_msdus) {
9814 		ret = ath12k_wmi_pdev_set_param(ar,
9815 						WMI_PDEV_PARAM_SET_CONG_CTRL_MAX_MSDUS,
9816 						ath12k_dp_num_pool_tx_desc(dp_params),
9817 						pdev->pdev_id);
9818 		if (ret) {
9819 			ath12k_err(ab,
9820 				   "failed to set congestion control MAX MSDUS: %d\n",
9821 				   ret);
9822 			goto err;
9823 		}
9824 	}
9825 
9826 	__ath12k_set_antenna(ar, ar->cfg_tx_chainmask, ar->cfg_rx_chainmask);
9827 
9828 	/* TODO: Do we need to enable ANI? */
9829 
9830 	ret = ath12k_reg_update_chan_list(ar, false);
9831 
9832 	/* The ar state alone can be turned off for non supported country
9833 	 * without returning the error value. As we need to update the channel
9834 	 * for the next ar.
9835 	 */
9836 	if (ret) {
9837 		if (ret == -EINVAL)
9838 			ret = 0;
9839 		goto err;
9840 	}
9841 
9842 	ar->num_started_vdevs = 0;
9843 	ar->num_created_vdevs = 0;
9844 	ar->num_peers = 0;
9845 	ar->allocated_vdev_map = 0;
9846 	ar->chan_tx_pwr = ATH12K_PDEV_TX_POWER_INVALID;
9847 
9848 	spin_lock_bh(&ar->data_lock);
9849 	ar->incumbent_signal_interference.handling_in_progress = false;
9850 	spin_unlock_bh(&ar->data_lock);
9851 
9852 	/* Configure monitor status ring with default rx_filter to get rx status
9853 	 * such as rssi, rx_duration.
9854 	 */
9855 	ret = ath12k_mac_config_mon_status_default(ar, true);
9856 	if (ret && (ret != -EOPNOTSUPP)) {
9857 		ath12k_err(ab, "failed to configure monitor status ring with default rx_filter: (%d)\n",
9858 			   ret);
9859 		goto err;
9860 	}
9861 
9862 	if (ret == -EOPNOTSUPP)
9863 		ath12k_dbg(ab, ATH12K_DBG_MAC,
9864 			   "monitor status config is not yet supported");
9865 
9866 	/* Configure the hash seed for hash based reo dest ring selection */
9867 	ath12k_wmi_pdev_lro_cfg(ar, ar->pdev->pdev_id);
9868 
9869 	/* allow device to enter IMPS */
9870 	if (ab->hw_params->idle_ps) {
9871 		ret = ath12k_wmi_pdev_set_param(ar, WMI_PDEV_PARAM_IDLE_PS_CONFIG,
9872 						1, pdev->pdev_id);
9873 		if (ret) {
9874 			ath12k_err(ab, "failed to enable idle ps: %d\n", ret);
9875 			goto err;
9876 		}
9877 	}
9878 
9879 	ret = ath12k_thermal_throttling_config_default(ar);
9880 	if (ret) {
9881 		ath12k_err(ab, "failed to set thermal throttle: %d\n", ret);
9882 		goto err;
9883 	}
9884 
9885 	rcu_assign_pointer(ab->pdevs_active[ar->pdev_idx],
9886 			   &ab->pdevs[ar->pdev_idx]);
9887 
9888 	return 0;
9889 err:
9890 
9891 	return ret;
9892 }
9893 
9894 static void ath12k_drain_tx(struct ath12k_hw *ah)
9895 {
9896 	struct ath12k *ar;
9897 	int i;
9898 
9899 	lockdep_assert_wiphy(ah->hw->wiphy);
9900 
9901 	for_each_ar(ah, ar, i)
9902 		ath12k_mac_drain_tx(ar);
9903 }
9904 
9905 int ath12k_mac_op_start(struct ieee80211_hw *hw)
9906 {
9907 	struct ath12k_hw *ah = ath12k_hw_to_ah(hw);
9908 	struct ath12k *ar;
9909 	int ret, i;
9910 
9911 	if (ath12k_ftm_mode)
9912 		return -EPERM;
9913 
9914 	lockdep_assert_wiphy(hw->wiphy);
9915 
9916 	ath12k_drain_tx(ah);
9917 
9918 	guard(mutex)(&ah->hw_mutex);
9919 
9920 	switch (ah->state) {
9921 	case ATH12K_HW_STATE_OFF:
9922 		ah->state = ATH12K_HW_STATE_ON;
9923 		break;
9924 	case ATH12K_HW_STATE_RESTARTING:
9925 		ah->state = ATH12K_HW_STATE_RESTARTED;
9926 		break;
9927 	case ATH12K_HW_STATE_RESTARTED:
9928 	case ATH12K_HW_STATE_WEDGED:
9929 	case ATH12K_HW_STATE_ON:
9930 	case ATH12K_HW_STATE_TM:
9931 		ah->state = ATH12K_HW_STATE_OFF;
9932 
9933 		WARN_ON(1);
9934 		return -EINVAL;
9935 	}
9936 
9937 	for_each_ar(ah, ar, i) {
9938 		ret = ath12k_mac_start(ar);
9939 		if (ret) {
9940 			ah->state = ATH12K_HW_STATE_OFF;
9941 
9942 			ath12k_err(ar->ab, "fail to start mac operations in pdev idx %d ret %d\n",
9943 				   ar->pdev_idx, ret);
9944 			goto fail_start;
9945 		}
9946 	}
9947 
9948 	return 0;
9949 
9950 fail_start:
9951 	for (; i > 0; i--) {
9952 		ar = ath12k_ah_to_ar(ah, i - 1);
9953 		ath12k_mac_stop(ar);
9954 	}
9955 
9956 	return ret;
9957 }
9958 EXPORT_SYMBOL(ath12k_mac_op_start);
9959 
9960 int ath12k_mac_rfkill_config(struct ath12k *ar)
9961 {
9962 	struct ath12k_base *ab = ar->ab;
9963 	u32 param;
9964 	int ret;
9965 
9966 	if (ab->hw_params->rfkill_pin == 0)
9967 		return -EOPNOTSUPP;
9968 
9969 	ath12k_dbg(ab, ATH12K_DBG_MAC,
9970 		   "mac rfkill_pin %d rfkill_cfg %d rfkill_on_level %d",
9971 		   ab->hw_params->rfkill_pin, ab->hw_params->rfkill_cfg,
9972 		   ab->hw_params->rfkill_on_level);
9973 
9974 	param = u32_encode_bits(ab->hw_params->rfkill_on_level,
9975 				WMI_RFKILL_CFG_RADIO_LEVEL) |
9976 		u32_encode_bits(ab->hw_params->rfkill_pin,
9977 				WMI_RFKILL_CFG_GPIO_PIN_NUM) |
9978 		u32_encode_bits(ab->hw_params->rfkill_cfg,
9979 				WMI_RFKILL_CFG_PIN_AS_GPIO);
9980 
9981 	ret = ath12k_wmi_pdev_set_param(ar, WMI_PDEV_PARAM_HW_RFKILL_CONFIG,
9982 					param, ar->pdev->pdev_id);
9983 	if (ret) {
9984 		ath12k_warn(ab,
9985 			    "failed to set rfkill config 0x%x: %d\n",
9986 			    param, ret);
9987 		return ret;
9988 	}
9989 
9990 	return 0;
9991 }
9992 
9993 int ath12k_mac_rfkill_enable_radio(struct ath12k *ar, bool enable)
9994 {
9995 	enum wmi_rfkill_enable_radio param;
9996 	int ret;
9997 
9998 	if (enable)
9999 		param = WMI_RFKILL_ENABLE_RADIO_ON;
10000 	else
10001 		param = WMI_RFKILL_ENABLE_RADIO_OFF;
10002 
10003 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac %d rfkill enable %d",
10004 		   ar->pdev_idx, param);
10005 
10006 	ret = ath12k_wmi_pdev_set_param(ar, WMI_PDEV_PARAM_RFKILL_ENABLE,
10007 					param, ar->pdev->pdev_id);
10008 	if (ret) {
10009 		ath12k_warn(ar->ab, "failed to set rfkill enable param %d: %d\n",
10010 			    param, ret);
10011 		return ret;
10012 	}
10013 
10014 	return 0;
10015 }
10016 
10017 static void ath12k_mac_stop(struct ath12k *ar)
10018 {
10019 	struct ath12k_pdev_dp *dp_pdev = &ar->dp;
10020 	struct ath12k_hw *ah = ar->ah;
10021 	struct htt_ppdu_stats_info *ppdu_stats, *tmp;
10022 	struct ath12k_wmi_scan_chan_list_arg *arg;
10023 	int ret;
10024 
10025 	lockdep_assert_held(&ah->hw_mutex);
10026 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
10027 
10028 	ret = ath12k_mac_config_mon_status_default(ar, false);
10029 	if (ret && (ret != -EOPNOTSUPP))
10030 		ath12k_err(ar->ab, "failed to clear rx_filter for monitor status ring: (%d)\n",
10031 			   ret);
10032 
10033 	clear_bit(ATH12K_FLAG_CAC_RUNNING, &ar->dev_flags);
10034 
10035 	cancel_delayed_work_sync(&ar->scan.timeout);
10036 	wiphy_work_cancel(ath12k_ar_to_hw(ar)->wiphy, &ar->scan.vdev_clean_wk);
10037 	cancel_work_sync(&ar->regd_channel_update_work);
10038 	cancel_work_sync(&ar->regd_update_work);
10039 	cancel_work_sync(&ar->ab->rfkill_work);
10040 	cancel_work_sync(&ar->ab->update_11d_work);
10041 	ar->state_11d = ATH12K_11D_IDLE;
10042 	complete(&ar->completed_11d_scan);
10043 
10044 	spin_lock_bh(&dp_pdev->ppdu_list_lock);
10045 	list_for_each_entry_safe(ppdu_stats, tmp, &dp_pdev->ppdu_stats_info, list) {
10046 		list_del(&ppdu_stats->list);
10047 		kfree(ppdu_stats);
10048 	}
10049 	spin_unlock_bh(&dp_pdev->ppdu_list_lock);
10050 
10051 	spin_lock_bh(&ar->data_lock);
10052 	while ((arg = list_first_entry_or_null(&ar->regd_channel_update_queue,
10053 					       struct ath12k_wmi_scan_chan_list_arg,
10054 					       list))) {
10055 		list_del(&arg->list);
10056 		kfree(arg);
10057 	}
10058 	spin_unlock_bh(&ar->data_lock);
10059 
10060 	rcu_assign_pointer(ar->ab->pdevs_active[ar->pdev_idx], NULL);
10061 
10062 	synchronize_rcu();
10063 
10064 	atomic_set(&ar->num_pending_mgmt_tx, 0);
10065 
10066 	spin_lock_bh(&ar->data_lock);
10067 	ar->incumbent_signal_interference.handling_in_progress = false;
10068 	spin_unlock_bh(&ar->data_lock);
10069 }
10070 
10071 void ath12k_mac_op_stop(struct ieee80211_hw *hw, bool suspend)
10072 {
10073 	struct ath12k_hw *ah = ath12k_hw_to_ah(hw);
10074 	struct ath12k *ar;
10075 	int i;
10076 
10077 	lockdep_assert_wiphy(hw->wiphy);
10078 
10079 	ath12k_drain_tx(ah);
10080 
10081 	mutex_lock(&ah->hw_mutex);
10082 
10083 	ah->state = ATH12K_HW_STATE_OFF;
10084 
10085 	for_each_ar(ah, ar, i)
10086 		ath12k_mac_stop(ar);
10087 
10088 	mutex_unlock(&ah->hw_mutex);
10089 }
10090 EXPORT_SYMBOL(ath12k_mac_op_stop);
10091 
10092 static u8
10093 ath12k_mac_get_vdev_stats_id(struct ath12k_link_vif *arvif)
10094 {
10095 	struct ath12k_base *ab = arvif->ar->ab;
10096 	u8 vdev_stats_id = 0;
10097 
10098 	do {
10099 		if (ab->free_vdev_stats_id_map & (1LL << vdev_stats_id)) {
10100 			vdev_stats_id++;
10101 			if (vdev_stats_id >= ATH12K_MAX_VDEV_STATS_ID) {
10102 				vdev_stats_id = ATH12K_INVAL_VDEV_STATS_ID;
10103 				break;
10104 			}
10105 		} else {
10106 			ab->free_vdev_stats_id_map |= (1LL << vdev_stats_id);
10107 			break;
10108 		}
10109 	} while (vdev_stats_id);
10110 
10111 	arvif->vdev_stats_id = vdev_stats_id;
10112 	return vdev_stats_id;
10113 }
10114 
10115 static int ath12k_mac_setup_vdev_params_mbssid(struct ath12k_link_vif *arvif,
10116 					       u32 *flags, u32 *tx_vdev_id)
10117 {
10118 	struct ath12k_vif *ahvif = arvif->ahvif;
10119 	struct ieee80211_bss_conf *link_conf;
10120 	struct ath12k *ar = arvif->ar;
10121 	struct ath12k_link_vif *tx_arvif;
10122 
10123 	link_conf = ath12k_mac_get_link_bss_conf(arvif);
10124 	if (!link_conf) {
10125 		ath12k_warn(ar->ab, "unable to access bss link conf in set mbssid params for vif %pM link %u\n",
10126 			    ahvif->vif->addr, arvif->link_id);
10127 		return -ENOLINK;
10128 	}
10129 
10130 	tx_arvif = ath12k_mac_get_tx_arvif(arvif, link_conf);
10131 	if (!tx_arvif)
10132 		return 0;
10133 
10134 	if (link_conf->nontransmitted) {
10135 		if (ath12k_ar_to_hw(ar)->wiphy !=
10136 		    ath12k_ar_to_hw(tx_arvif->ar)->wiphy)
10137 			return -EINVAL;
10138 
10139 		*flags = WMI_VDEV_MBSSID_FLAGS_NON_TRANSMIT_AP;
10140 		*tx_vdev_id = tx_arvif->vdev_id;
10141 	} else if (tx_arvif == arvif) {
10142 		*flags = WMI_VDEV_MBSSID_FLAGS_TRANSMIT_AP;
10143 	} else {
10144 		return -EINVAL;
10145 	}
10146 
10147 	if (link_conf->ema_ap)
10148 		*flags |= WMI_VDEV_MBSSID_FLAGS_EMA_MODE;
10149 
10150 	return 0;
10151 }
10152 
10153 static int ath12k_mac_setup_vdev_create_arg(struct ath12k_link_vif *arvif,
10154 					    struct ath12k_wmi_vdev_create_arg *arg)
10155 {
10156 	struct ath12k *ar = arvif->ar;
10157 	struct ath12k_pdev *pdev = ar->pdev;
10158 	struct ath12k_vif *ahvif = arvif->ahvif;
10159 	int ret;
10160 
10161 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
10162 
10163 	arg->if_id = arvif->vdev_id;
10164 	arg->type = ahvif->vdev_type;
10165 	arg->subtype = ahvif->vdev_subtype;
10166 	arg->pdev_id = pdev->pdev_id;
10167 
10168 	arg->mbssid_flags = WMI_VDEV_MBSSID_FLAGS_NON_MBSSID_AP;
10169 	arg->mbssid_tx_vdev_id = 0;
10170 	if (!test_bit(WMI_TLV_SERVICE_MBSS_PARAM_IN_VDEV_START_SUPPORT,
10171 		      ar->ab->wmi_ab.svc_map)) {
10172 		ret = ath12k_mac_setup_vdev_params_mbssid(arvif,
10173 							  &arg->mbssid_flags,
10174 							  &arg->mbssid_tx_vdev_id);
10175 		if (ret)
10176 			return ret;
10177 	}
10178 
10179 	if (pdev->cap.supported_bands & WMI_HOST_WLAN_2GHZ_CAP) {
10180 		arg->chains[NL80211_BAND_2GHZ].tx = ar->num_tx_chains;
10181 		arg->chains[NL80211_BAND_2GHZ].rx = ar->num_rx_chains;
10182 	}
10183 	if (pdev->cap.supported_bands & WMI_HOST_WLAN_5GHZ_CAP) {
10184 		arg->chains[NL80211_BAND_5GHZ].tx = ar->num_tx_chains;
10185 		arg->chains[NL80211_BAND_5GHZ].rx = ar->num_rx_chains;
10186 	}
10187 	if (pdev->cap.supported_bands & WMI_HOST_WLAN_5GHZ_CAP &&
10188 	    ar->supports_6ghz) {
10189 		arg->chains[NL80211_BAND_6GHZ].tx = ar->num_tx_chains;
10190 		arg->chains[NL80211_BAND_6GHZ].rx = ar->num_rx_chains;
10191 	}
10192 
10193 	arg->if_stats_id = ath12k_mac_get_vdev_stats_id(arvif);
10194 
10195 	if (ath12k_mac_is_ml_arvif(arvif)) {
10196 		if (hweight16(ahvif->vif->valid_links) > ATH12K_WMI_MLO_MAX_LINKS) {
10197 			ath12k_warn(ar->ab, "too many MLO links during setting up vdev: %d",
10198 				    ahvif->vif->valid_links);
10199 			return -EINVAL;
10200 		}
10201 
10202 		ether_addr_copy(arg->mld_addr, ahvif->vif->addr);
10203 	}
10204 
10205 	return 0;
10206 }
10207 
10208 static void ath12k_mac_update_vif_offload(struct ath12k_link_vif *arvif)
10209 {
10210 	struct ath12k_vif *ahvif = arvif->ahvif;
10211 	struct ieee80211_vif *vif = ath12k_ahvif_to_vif(ahvif);
10212 	struct ath12k *ar = arvif->ar;
10213 	struct ath12k_base *ab = ar->ab;
10214 	u32 param_id, param_value;
10215 	int ret;
10216 
10217 	param_id = WMI_VDEV_PARAM_TX_ENCAP_TYPE;
10218 	if (vif->type != NL80211_IFTYPE_STATION &&
10219 	    vif->type != NL80211_IFTYPE_AP)
10220 		vif->offload_flags &= ~(IEEE80211_OFFLOAD_ENCAP_ENABLED |
10221 					IEEE80211_OFFLOAD_DECAP_ENABLED |
10222 					IEEE80211_OFFLOAD_ENCAP_MCAST |
10223 					IEEE80211_OFFLOAD_ENCAP_4ADDR);
10224 
10225 	if (vif->offload_flags & IEEE80211_OFFLOAD_ENCAP_ENABLED)
10226 		ahvif->dp_vif.tx_encap_type = ATH12K_HW_TXRX_ETHERNET;
10227 	else if (test_bit(ATH12K_FLAG_RAW_MODE, &ab->dev_flags))
10228 		ahvif->dp_vif.tx_encap_type = ATH12K_HW_TXRX_RAW;
10229 	else
10230 		ahvif->dp_vif.tx_encap_type = ATH12K_HW_TXRX_NATIVE_WIFI;
10231 
10232 	ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
10233 					    param_id, ahvif->dp_vif.tx_encap_type);
10234 	if (ret) {
10235 		ath12k_warn(ab, "failed to set vdev %d tx encap mode: %d\n",
10236 			    arvif->vdev_id, ret);
10237 		vif->offload_flags &= ~IEEE80211_OFFLOAD_ENCAP_ENABLED;
10238 	}
10239 
10240 	if (vif->offload_flags & IEEE80211_OFFLOAD_ENCAP_ENABLED)
10241 		vif->offload_flags |= (IEEE80211_OFFLOAD_ENCAP_MCAST |
10242 				       IEEE80211_OFFLOAD_ENCAP_4ADDR);
10243 
10244 	param_id = WMI_VDEV_PARAM_RX_DECAP_TYPE;
10245 	if (vif->offload_flags & IEEE80211_OFFLOAD_DECAP_ENABLED)
10246 		param_value = ATH12K_HW_TXRX_ETHERNET;
10247 	else if (test_bit(ATH12K_FLAG_RAW_MODE, &ab->dev_flags))
10248 		param_value = ATH12K_HW_TXRX_RAW;
10249 	else
10250 		param_value = ATH12K_HW_TXRX_NATIVE_WIFI;
10251 
10252 	ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
10253 					    param_id, param_value);
10254 	if (ret) {
10255 		ath12k_warn(ab, "failed to set vdev %d rx decap mode: %d\n",
10256 			    arvif->vdev_id, ret);
10257 		vif->offload_flags &= ~IEEE80211_OFFLOAD_DECAP_ENABLED;
10258 	}
10259 }
10260 
10261 void ath12k_mac_op_update_vif_offload(struct ieee80211_hw *hw,
10262 				      struct ieee80211_vif *vif)
10263 {
10264 	struct ath12k_vif *ahvif = ath12k_vif_to_ahvif(vif);
10265 	struct ath12k_link_vif *arvif;
10266 	unsigned long links;
10267 	int link_id;
10268 
10269 	lockdep_assert_wiphy(hw->wiphy);
10270 
10271 	if (vif->valid_links) {
10272 		links = vif->valid_links;
10273 		for_each_set_bit(link_id, &links, IEEE80211_MLD_MAX_NUM_LINKS) {
10274 			arvif = wiphy_dereference(hw->wiphy, ahvif->link[link_id]);
10275 			if (!(arvif && arvif->ar))
10276 				continue;
10277 
10278 			ath12k_mac_update_vif_offload(arvif);
10279 		}
10280 
10281 		return;
10282 	}
10283 
10284 	ath12k_mac_update_vif_offload(&ahvif->deflink);
10285 }
10286 EXPORT_SYMBOL(ath12k_mac_op_update_vif_offload);
10287 
10288 static bool ath12k_mac_vif_ap_active_any(struct ath12k_base *ab)
10289 {
10290 	struct ath12k *ar;
10291 	struct ath12k_pdev *pdev;
10292 	struct ath12k_link_vif *arvif;
10293 	int i;
10294 
10295 	for (i = 0; i < ab->num_radios; i++) {
10296 		pdev = &ab->pdevs[i];
10297 		ar = pdev->ar;
10298 		list_for_each_entry(arvif, &ar->arvifs, list) {
10299 			if (arvif->is_up &&
10300 			    arvif->ahvif->vdev_type == WMI_VDEV_TYPE_AP)
10301 				return true;
10302 		}
10303 	}
10304 	return false;
10305 }
10306 
10307 void ath12k_mac_11d_scan_start(struct ath12k *ar, u32 vdev_id)
10308 {
10309 	struct wmi_11d_scan_start_arg arg;
10310 	int ret;
10311 
10312 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
10313 
10314 	if (ar->regdom_set_by_user)
10315 		goto fin;
10316 
10317 	if (ar->vdev_id_11d_scan != ATH12K_11D_INVALID_VDEV_ID)
10318 		goto fin;
10319 
10320 	if (!test_bit(WMI_TLV_SERVICE_11D_OFFLOAD, ar->ab->wmi_ab.svc_map))
10321 		goto fin;
10322 
10323 	if (ath12k_mac_vif_ap_active_any(ar->ab))
10324 		goto fin;
10325 
10326 	arg.vdev_id = vdev_id;
10327 	arg.start_interval_msec = 0;
10328 	arg.scan_period_msec = ATH12K_SCAN_11D_INTERVAL;
10329 
10330 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
10331 		   "mac start 11d scan for vdev %d\n", vdev_id);
10332 
10333 	ret = ath12k_wmi_send_11d_scan_start_cmd(ar, &arg);
10334 	if (ret) {
10335 		ath12k_warn(ar->ab, "failed to start 11d scan vdev %d ret: %d\n",
10336 			    vdev_id, ret);
10337 	} else {
10338 		ar->vdev_id_11d_scan = vdev_id;
10339 		if (ar->state_11d == ATH12K_11D_PREPARING)
10340 			ar->state_11d = ATH12K_11D_RUNNING;
10341 	}
10342 
10343 fin:
10344 	if (ar->state_11d == ATH12K_11D_PREPARING) {
10345 		ar->state_11d = ATH12K_11D_IDLE;
10346 		complete(&ar->completed_11d_scan);
10347 	}
10348 }
10349 
10350 void ath12k_mac_11d_scan_stop(struct ath12k *ar)
10351 {
10352 	int ret;
10353 	u32 vdev_id;
10354 
10355 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
10356 
10357 	if (!test_bit(WMI_TLV_SERVICE_11D_OFFLOAD, ar->ab->wmi_ab.svc_map))
10358 		return;
10359 
10360 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac stop 11d for vdev %d\n",
10361 		   ar->vdev_id_11d_scan);
10362 
10363 	if (ar->state_11d == ATH12K_11D_PREPARING) {
10364 		ar->state_11d = ATH12K_11D_IDLE;
10365 		complete(&ar->completed_11d_scan);
10366 	}
10367 
10368 	if (ar->vdev_id_11d_scan != ATH12K_11D_INVALID_VDEV_ID) {
10369 		vdev_id = ar->vdev_id_11d_scan;
10370 
10371 		ret = ath12k_wmi_send_11d_scan_stop_cmd(ar, vdev_id);
10372 		if (ret) {
10373 			ath12k_warn(ar->ab,
10374 				    "failed to stopt 11d scan vdev %d ret: %d\n",
10375 				    vdev_id, ret);
10376 		} else {
10377 			ar->vdev_id_11d_scan = ATH12K_11D_INVALID_VDEV_ID;
10378 			ar->state_11d = ATH12K_11D_IDLE;
10379 			complete(&ar->completed_11d_scan);
10380 		}
10381 	}
10382 }
10383 
10384 void ath12k_mac_11d_scan_stop_all(struct ath12k_base *ab)
10385 {
10386 	struct ath12k *ar;
10387 	struct ath12k_pdev *pdev;
10388 	int i;
10389 
10390 	ath12k_dbg(ab, ATH12K_DBG_MAC, "mac stop soc 11d scan\n");
10391 
10392 	for (i = 0; i < ab->num_radios; i++) {
10393 		pdev = &ab->pdevs[i];
10394 		ar = pdev->ar;
10395 
10396 		ath12k_mac_11d_scan_stop(ar);
10397 	}
10398 }
10399 
10400 static void ath12k_mac_determine_vdev_type(struct ieee80211_vif *vif,
10401 					   struct ath12k_vif *ahvif)
10402 {
10403 	ahvif->vdev_subtype = WMI_VDEV_SUBTYPE_NONE;
10404 
10405 	switch (vif->type) {
10406 	case NL80211_IFTYPE_UNSPECIFIED:
10407 	case NL80211_IFTYPE_STATION:
10408 		ahvif->vdev_type = WMI_VDEV_TYPE_STA;
10409 
10410 		if (vif->p2p)
10411 			ahvif->vdev_subtype = WMI_VDEV_SUBTYPE_P2P_CLIENT;
10412 
10413 		break;
10414 	case NL80211_IFTYPE_MESH_POINT:
10415 		ahvif->vdev_subtype = WMI_VDEV_SUBTYPE_MESH_11S;
10416 		fallthrough;
10417 	case NL80211_IFTYPE_AP:
10418 		ahvif->vdev_type = WMI_VDEV_TYPE_AP;
10419 
10420 		if (vif->p2p)
10421 			ahvif->vdev_subtype = WMI_VDEV_SUBTYPE_P2P_GO;
10422 
10423 		break;
10424 	case NL80211_IFTYPE_MONITOR:
10425 		ahvif->vdev_type = WMI_VDEV_TYPE_MONITOR;
10426 		break;
10427 	case NL80211_IFTYPE_P2P_DEVICE:
10428 		ahvif->vdev_type = WMI_VDEV_TYPE_STA;
10429 		ahvif->vdev_subtype = WMI_VDEV_SUBTYPE_P2P_DEVICE;
10430 		break;
10431 	default:
10432 		WARN_ON(1);
10433 		break;
10434 	}
10435 }
10436 
10437 int ath12k_mac_vdev_create(struct ath12k *ar, struct ath12k_link_vif *arvif)
10438 {
10439 	struct ath12k_hw *ah = ar->ah;
10440 	struct ath12k_base *ab = ar->ab;
10441 	struct ieee80211_hw *hw = ah->hw;
10442 	struct ath12k_vif *ahvif = arvif->ahvif;
10443 	struct ieee80211_vif *vif = ath12k_ahvif_to_vif(ahvif);
10444 	struct wireless_dev *wdev = ieee80211_vif_to_wdev(vif);
10445 	struct ath12k_wmi_vdev_create_arg vdev_arg = {};
10446 	struct ath12k_wmi_peer_create_arg peer_param = {};
10447 	struct ieee80211_bss_conf *link_conf = NULL;
10448 	u32 param_id, param_value;
10449 	u16 nss;
10450 	int i;
10451 	int ret, vdev_id;
10452 	u8 link_id;
10453 	struct ath12k_dp_link_vif *dp_link_vif = NULL;
10454 	struct ath12k_dp_peer_create_params params = {};
10455 	bool dp_peer_created = false;
10456 
10457 	lockdep_assert_wiphy(hw->wiphy);
10458 
10459 	/* In NO_VIRTUAL_MONITOR, its necessary to restrict only one monitor
10460 	 * interface in each radio
10461 	 */
10462 	if (vif->type == NL80211_IFTYPE_MONITOR && ar->monitor_vdev_created)
10463 		return -EINVAL;
10464 
10465 	if (ar->num_created_vdevs >= TARGET_NUM_VDEVS(ab)) {
10466 		ath12k_warn(ab, "failed to create vdev, reached max vdev limit %d\n",
10467 			    TARGET_NUM_VDEVS(ab));
10468 		return -ENOSPC;
10469 	}
10470 
10471 	link_id = arvif->link_id;
10472 
10473 	if (link_id < IEEE80211_MLD_MAX_NUM_LINKS) {
10474 		link_conf = wiphy_dereference(hw->wiphy, vif->link_conf[link_id]);
10475 		if (!link_conf) {
10476 			ath12k_warn(ar->ab, "unable to access bss link conf in vdev create for vif %pM link %u\n",
10477 				    vif->addr, arvif->link_id);
10478 			return -ENOLINK;
10479 		}
10480 	}
10481 
10482 	if (link_conf)
10483 		memcpy(arvif->bssid, link_conf->addr, ETH_ALEN);
10484 	else
10485 		memcpy(arvif->bssid, vif->addr, ETH_ALEN);
10486 
10487 	arvif->ar = ar;
10488 	vdev_id = __ffs64(ab->free_vdev_map);
10489 	arvif->vdev_id = vdev_id;
10490 	if (vif->type == NL80211_IFTYPE_MONITOR)
10491 		ar->monitor_vdev_id = vdev_id;
10492 
10493 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac vdev create id %d type %d subtype %d map %llx\n",
10494 		   arvif->vdev_id, ahvif->vdev_type, ahvif->vdev_subtype,
10495 		   ab->free_vdev_map);
10496 
10497 	vif->cab_queue = arvif->vdev_id % (ATH12K_HW_MAX_QUEUES - 1);
10498 	for (i = 0; i < ARRAY_SIZE(vif->hw_queue); i++)
10499 		vif->hw_queue[i] = i % (ATH12K_HW_MAX_QUEUES - 1);
10500 
10501 	ret = ath12k_mac_setup_vdev_create_arg(arvif, &vdev_arg);
10502 	if (ret) {
10503 		ath12k_warn(ab, "failed to create vdev parameters %d: %d\n",
10504 			    arvif->vdev_id, ret);
10505 		goto err;
10506 	}
10507 
10508 	ret = ath12k_wmi_vdev_create(ar, arvif->bssid, &vdev_arg);
10509 	if (ret) {
10510 		ath12k_warn(ab, "failed to create WMI vdev %d: %d\n",
10511 			    arvif->vdev_id, ret);
10512 		goto err;
10513 	}
10514 
10515 	ar->num_created_vdevs++;
10516 	arvif->is_created = true;
10517 	ath12k_dbg(ab, ATH12K_DBG_MAC, "vdev %pM created, vdev_id %d\n",
10518 		   vif->addr, arvif->vdev_id);
10519 	ar->allocated_vdev_map |= 1LL << arvif->vdev_id;
10520 	ab->free_vdev_map &= ~(1LL << arvif->vdev_id);
10521 
10522 	spin_lock_bh(&ar->data_lock);
10523 	list_add(&arvif->list, &ar->arvifs);
10524 	spin_unlock_bh(&ar->data_lock);
10525 
10526 	ath12k_mac_update_vif_offload(arvif);
10527 
10528 	nss = hweight32(ar->cfg_tx_chainmask) ? : 1;
10529 	ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
10530 					    WMI_VDEV_PARAM_NSS, nss);
10531 	if (ret) {
10532 		ath12k_warn(ab, "failed to set vdev %d chainmask 0x%x, nss %d :%d\n",
10533 			    arvif->vdev_id, ar->cfg_tx_chainmask, nss, ret);
10534 		goto err_vdev_del;
10535 	}
10536 
10537 	dp_link_vif = ath12k_dp_vif_to_dp_link_vif(&ahvif->dp_vif, arvif->link_id);
10538 
10539 	dp_link_vif->vdev_id = arvif->vdev_id;
10540 	dp_link_vif->lmac_id = ar->lmac_id;
10541 	dp_link_vif->pdev_idx = ar->pdev_idx;
10542 
10543 	switch (ahvif->vdev_type) {
10544 	case WMI_VDEV_TYPE_AP:
10545 		params.ucast_ra_only = true;
10546 
10547 		if (arvif->link_id < IEEE80211_MLD_MAX_NUM_LINKS) {
10548 			ret = ath12k_dp_peer_create(&ah->dp_hw, arvif->bssid, &params);
10549 			if (ret) {
10550 				ath12k_warn(ab, "failed to vdev %d create dp_peer for AP: %d\n",
10551 					    arvif->vdev_id, ret);
10552 				goto err_vdev_del;
10553 			}
10554 			dp_peer_created = true;
10555 		}
10556 
10557 		peer_param.vdev_id = arvif->vdev_id;
10558 		peer_param.peer_addr = arvif->bssid;
10559 		peer_param.peer_type = WMI_PEER_TYPE_DEFAULT;
10560 		ret = ath12k_peer_create(ar, arvif, NULL, &peer_param);
10561 		if (ret) {
10562 			ath12k_warn(ab, "failed to vdev %d create peer for AP: %d\n",
10563 				    arvif->vdev_id, ret);
10564 			goto err_dp_peer_del;
10565 		}
10566 
10567 		ret = ath12k_mac_set_kickout(arvif);
10568 		if (ret) {
10569 			ath12k_warn(ar->ab, "failed to set vdev %i kickout parameters: %d\n",
10570 				    arvif->vdev_id, ret);
10571 			goto err_peer_del;
10572 		}
10573 		ath12k_mac_11d_scan_stop_all(ar->ab);
10574 		break;
10575 	case WMI_VDEV_TYPE_STA:
10576 		param_id = WMI_STA_PS_PARAM_RX_WAKE_POLICY;
10577 		param_value = WMI_STA_PS_RX_WAKE_POLICY_WAKE;
10578 		ret = ath12k_wmi_set_sta_ps_param(ar, arvif->vdev_id,
10579 						  param_id, param_value);
10580 		if (ret) {
10581 			ath12k_warn(ar->ab, "failed to set vdev %d RX wake policy: %d\n",
10582 				    arvif->vdev_id, ret);
10583 			goto err_peer_del;
10584 		}
10585 
10586 		param_id = WMI_STA_PS_PARAM_TX_WAKE_THRESHOLD;
10587 		param_value = WMI_STA_PS_TX_WAKE_THRESHOLD_ALWAYS;
10588 		ret = ath12k_wmi_set_sta_ps_param(ar, arvif->vdev_id,
10589 						  param_id, param_value);
10590 		if (ret) {
10591 			ath12k_warn(ar->ab, "failed to set vdev %d TX wake threshold: %d\n",
10592 				    arvif->vdev_id, ret);
10593 			goto err_peer_del;
10594 		}
10595 
10596 		param_id = WMI_STA_PS_PARAM_PSPOLL_COUNT;
10597 		param_value = WMI_STA_PS_PSPOLL_COUNT_NO_MAX;
10598 		ret = ath12k_wmi_set_sta_ps_param(ar, arvif->vdev_id,
10599 						  param_id, param_value);
10600 		if (ret) {
10601 			ath12k_warn(ar->ab, "failed to set vdev %d pspoll count: %d\n",
10602 				    arvif->vdev_id, ret);
10603 			goto err_peer_del;
10604 		}
10605 
10606 		ret = ath12k_wmi_pdev_set_ps_mode(ar, arvif->vdev_id, false);
10607 		if (ret) {
10608 			ath12k_warn(ar->ab, "failed to disable vdev %d ps mode: %d\n",
10609 				    arvif->vdev_id, ret);
10610 			goto err_peer_del;
10611 		}
10612 
10613 		/*
10614 		 * There could be race condition in firmware for the station
10615 		 * interface between enabling 4-address peer WMI param and
10616 		 * sending 4-address frame (NULL or EAPOL via TCL).
10617 		 * Make the station as WDS while bringup itself
10618 		 * to avoid the race condition
10619 		 */
10620 		if (vif->type == NL80211_IFTYPE_STATION &&
10621 		    (wdev && wdev->use_4addr)) {
10622 			ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
10623 							    WMI_VDEV_PARAM_WDS,
10624 							    1);
10625 			if (ret) {
10626 				ath12k_warn(ar->ab, "failed to set WDS vdev param: %d\n",
10627 					    ret);
10628 				goto err_peer_del;
10629 			}
10630 			arvif->set_wds_vdev_param = true;
10631 		}
10632 
10633 		if (test_bit(WMI_TLV_SERVICE_11D_OFFLOAD, ab->wmi_ab.svc_map) &&
10634 		    ahvif->vdev_type == WMI_VDEV_TYPE_STA &&
10635 		    ahvif->vdev_subtype == WMI_VDEV_SUBTYPE_NONE) {
10636 			reinit_completion(&ar->completed_11d_scan);
10637 			ar->state_11d = ATH12K_11D_PREPARING;
10638 		}
10639 		break;
10640 	case WMI_VDEV_TYPE_MONITOR:
10641 		ar->monitor_vdev_created = true;
10642 		break;
10643 	default:
10644 		break;
10645 	}
10646 
10647 	if (link_conf)
10648 		arvif->txpower = link_conf->txpower;
10649 	else
10650 		arvif->txpower = NL80211_TX_POWER_AUTOMATIC;
10651 
10652 	ret = ath12k_mac_txpower_recalc(ar);
10653 	if (ret)
10654 		goto err_peer_del;
10655 
10656 	param_id = WMI_VDEV_PARAM_RTS_THRESHOLD;
10657 	param_value = hw->wiphy->rts_threshold;
10658 	ar->rts_threshold = param_value;
10659 	ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
10660 					    param_id, param_value);
10661 	if (ret) {
10662 		ath12k_warn(ar->ab, "failed to set rts threshold for vdev %d: %d\n",
10663 			    arvif->vdev_id, ret);
10664 	}
10665 
10666 	ath12k_dp_vdev_tx_attach(ar, arvif);
10667 
10668 	return ret;
10669 
10670 err_peer_del:
10671 	if (ahvif->vdev_type == WMI_VDEV_TYPE_AP) {
10672 		/* ignore return value: propagate the original error */
10673 		ath12k_peer_delete(ar, arvif->vdev_id, arvif->bssid);
10674 	}
10675 
10676 err_dp_peer_del:
10677 	if (dp_peer_created)
10678 		ath12k_dp_peer_delete(&ah->dp_hw, arvif->bssid, NULL);
10679 
10680 err_vdev_del:
10681 	if (ahvif->vdev_type == WMI_VDEV_TYPE_MONITOR) {
10682 		ar->monitor_vdev_id = -1;
10683 		ar->monitor_vdev_created = false;
10684 	}
10685 
10686 	ath12k_wmi_vdev_delete(ar, arvif->vdev_id);
10687 	ar->num_created_vdevs--;
10688 	ar->allocated_vdev_map &= ~(1LL << arvif->vdev_id);
10689 	ab->free_vdev_map |= 1LL << arvif->vdev_id;
10690 	ab->free_vdev_stats_id_map &= ~(1LL << arvif->vdev_stats_id);
10691 	spin_lock_bh(&ar->data_lock);
10692 	list_del(&arvif->list);
10693 	spin_unlock_bh(&ar->data_lock);
10694 
10695 err:
10696 	arvif->is_created = false;
10697 	arvif->ar = NULL;
10698 	return ret;
10699 }
10700 
10701 static void ath12k_mac_vif_flush_key_cache(struct ath12k_link_vif *arvif)
10702 {
10703 	struct ath12k_key_conf *key_conf, *tmp;
10704 	struct ath12k_vif *ahvif = arvif->ahvif;
10705 	struct ath12k_hw *ah = ahvif->ah;
10706 	struct ath12k_sta *ahsta;
10707 	struct ath12k_link_sta *arsta;
10708 	struct ath12k_vif_cache *cache = ahvif->cache[arvif->link_id];
10709 	int ret;
10710 
10711 	lockdep_assert_wiphy(ah->hw->wiphy);
10712 
10713 	list_for_each_entry_safe(key_conf, tmp, &cache->key_conf.list, list) {
10714 		arsta = NULL;
10715 		if (key_conf->sta) {
10716 			ahsta = ath12k_sta_to_ahsta(key_conf->sta);
10717 			arsta = wiphy_dereference(ah->hw->wiphy,
10718 						  ahsta->link[arvif->link_id]);
10719 			if (!arsta)
10720 				goto free_cache;
10721 		}
10722 
10723 		ret = ath12k_mac_set_key(arvif->ar, key_conf->cmd,
10724 					 arvif, arsta,
10725 					 key_conf->key);
10726 		if (ret)
10727 			ath12k_warn(arvif->ar->ab, "unable to apply set key param to vdev %d ret %d\n",
10728 				    arvif->vdev_id, ret);
10729 free_cache:
10730 		list_del(&key_conf->list);
10731 		kfree(key_conf);
10732 	}
10733 }
10734 
10735 static void ath12k_mac_vif_cache_flush(struct ath12k *ar, struct ath12k_link_vif *arvif)
10736 {
10737 	struct ath12k_vif *ahvif = arvif->ahvif;
10738 	struct ieee80211_vif *vif = ath12k_ahvif_to_vif(ahvif);
10739 	struct ath12k_vif_cache *cache = ahvif->cache[arvif->link_id];
10740 	struct ath12k_base *ab = ar->ab;
10741 	struct ieee80211_bss_conf *link_conf;
10742 
10743 	int ret;
10744 
10745 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
10746 
10747 	if (!cache)
10748 		return;
10749 
10750 	if (cache->tx_conf.changed) {
10751 		ret = ath12k_mac_conf_tx(arvif, cache->tx_conf.ac,
10752 					 &cache->tx_conf.tx_queue_params);
10753 		if (ret)
10754 			ath12k_warn(ab,
10755 				    "unable to apply tx config parameters to vdev %d\n",
10756 				    ret);
10757 	}
10758 
10759 	if (cache->bss_conf_changed) {
10760 		link_conf = ath12k_mac_get_link_bss_conf(arvif);
10761 		if (!link_conf) {
10762 			ath12k_warn(ar->ab, "unable to access bss link conf in cache flush for vif %pM link %u\n",
10763 				    vif->addr, arvif->link_id);
10764 			return;
10765 		}
10766 		ath12k_mac_bss_info_changed(ar, arvif, link_conf,
10767 					    cache->bss_conf_changed);
10768 	}
10769 
10770 	if (!list_empty(&cache->key_conf.list))
10771 		ath12k_mac_vif_flush_key_cache(arvif);
10772 
10773 	ath12k_ahvif_put_link_cache(ahvif, arvif->link_id);
10774 }
10775 
10776 static struct ath12k *ath12k_mac_assign_vif_to_vdev(struct ieee80211_hw *hw,
10777 						    struct ath12k_link_vif *arvif,
10778 						    struct ieee80211_chanctx_conf *ctx)
10779 {
10780 	struct ath12k_vif *ahvif = arvif->ahvif;
10781 	struct ieee80211_vif *vif = ath12k_ahvif_to_vif(ahvif);
10782 	struct ath12k_link_vif *scan_arvif;
10783 	struct ath12k_hw *ah = hw->priv;
10784 	struct ath12k *ar;
10785 	struct ath12k_base *ab;
10786 	u8 link_id = arvif->link_id, scan_link_id;
10787 	unsigned long scan_link_map;
10788 	int ret;
10789 
10790 	lockdep_assert_wiphy(hw->wiphy);
10791 
10792 	if (ah->num_radio == 1)
10793 		ar = ah->radio;
10794 	else if (ctx)
10795 		ar = ath12k_get_ar_by_ctx(hw, ctx);
10796 	else
10797 		return NULL;
10798 
10799 	if (!ar)
10800 		return NULL;
10801 
10802 	/* cleanup the scan vdev if we are done scan on that ar
10803 	 * and now we want to create for actual usage.
10804 	 */
10805 	if (ieee80211_vif_is_mld(vif)) {
10806 		scan_link_map = ahvif->links_map & ATH12K_SCAN_LINKS_MASK;
10807 		for_each_set_bit(scan_link_id, &scan_link_map, ATH12K_NUM_MAX_LINKS) {
10808 			scan_arvif = wiphy_dereference(hw->wiphy,
10809 						       ahvif->link[scan_link_id]);
10810 			if (scan_arvif && scan_arvif->ar == ar) {
10811 				ar->scan.arvif = NULL;
10812 				ath12k_mac_remove_link_interface(hw, scan_arvif);
10813 				ath12k_mac_unassign_link_vif(scan_arvif);
10814 				break;
10815 			}
10816 		}
10817 	}
10818 
10819 	if (arvif->ar) {
10820 		/* This is not expected really */
10821 		if (WARN_ON(!arvif->is_created)) {
10822 			arvif->ar = NULL;
10823 			return NULL;
10824 		}
10825 
10826 		if (ah->num_radio == 1)
10827 			return arvif->ar;
10828 
10829 		/* This can happen as scan vdev gets created during multiple scans
10830 		 * across different radios before a vdev is brought up in
10831 		 * a certain radio.
10832 		 */
10833 		if (ar != arvif->ar) {
10834 			if (WARN_ON(arvif->is_started))
10835 				return NULL;
10836 
10837 			ath12k_mac_remove_link_interface(hw, arvif);
10838 			ath12k_mac_unassign_link_vif(arvif);
10839 		}
10840 	}
10841 
10842 	ab = ar->ab;
10843 
10844 	/* Assign arvif again here since previous radio switch block
10845 	 * would've unassigned and cleared it.
10846 	 */
10847 	arvif = ath12k_mac_assign_link_vif(ah, vif, link_id);
10848 	if (vif->type == NL80211_IFTYPE_AP &&
10849 	    ar->num_peers > (ar->max_num_peers - 1)) {
10850 		ath12k_warn(ab, "failed to create vdev due to insufficient peer entry resource in firmware\n");
10851 		goto unlock;
10852 	}
10853 
10854 	if (arvif->is_created)
10855 		goto flush;
10856 
10857 	ret = ath12k_mac_vdev_create(ar, arvif);
10858 	if (ret) {
10859 		ath12k_warn(ab, "failed to create vdev %pM ret %d", vif->addr, ret);
10860 		goto unlock;
10861 	}
10862 
10863 flush:
10864 	/* If the vdev is created during channel assign and not during
10865 	 * add_interface(), Apply any parameters for the vdev which were received
10866 	 * after add_interface, corresponding to this vif.
10867 	 */
10868 	ath12k_mac_vif_cache_flush(ar, arvif);
10869 unlock:
10870 	return arvif->ar;
10871 }
10872 
10873 int ath12k_mac_op_add_interface(struct ieee80211_hw *hw,
10874 				struct ieee80211_vif *vif)
10875 {
10876 	struct ath12k_hw *ah = ath12k_hw_to_ah(hw);
10877 	struct ath12k_vif *ahvif = ath12k_vif_to_ahvif(vif);
10878 	struct ath12k_reg_info *reg_info;
10879 	struct ath12k_link_vif *arvif;
10880 	struct ath12k_base *ab;
10881 	struct ath12k *ar;
10882 	int i;
10883 
10884 	lockdep_assert_wiphy(hw->wiphy);
10885 
10886 	memset(ahvif, 0, sizeof(*ahvif));
10887 
10888 	ahvif->ah = ah;
10889 	ahvif->vif = vif;
10890 	arvif = &ahvif->deflink;
10891 
10892 	ath12k_mac_init_arvif(ahvif, arvif, -1);
10893 
10894 	/* Allocate Default Queue now and reassign during actual vdev create */
10895 	vif->cab_queue = ATH12K_HW_DEFAULT_QUEUE;
10896 	for (i = 0; i < ARRAY_SIZE(vif->hw_queue); i++)
10897 		vif->hw_queue[i] = ATH12K_HW_DEFAULT_QUEUE;
10898 
10899 	vif->driver_flags |= IEEE80211_VIF_SUPPORTS_UAPSD;
10900 
10901 	ath12k_mac_determine_vdev_type(vif, ahvif);
10902 
10903 	for_each_ar(ah, ar, i) {
10904 		if (!ath12k_wmi_supports_6ghz_cc_ext(ar))
10905 			continue;
10906 
10907 		ab = ar->ab;
10908 		reg_info = ab->reg_info[ar->pdev_idx];
10909 		ath12k_dbg(ab, ATH12K_DBG_MAC, "interface added to change reg rules\n");
10910 		ah->regd_updated = false;
10911 		ath12k_reg_handle_chan_list(ab, reg_info, ahvif->vdev_type,
10912 					    IEEE80211_REG_UNSET_AP);
10913 		break;
10914 	}
10915 
10916 	/* Defer vdev creation until assign_chanctx or hw_scan is initiated as driver
10917 	 * will not know if this interface is an ML vif at this point.
10918 	 */
10919 	return 0;
10920 }
10921 EXPORT_SYMBOL(ath12k_mac_op_add_interface);
10922 
10923 static void ath12k_mac_vif_unref(struct ath12k_dp *dp, struct ieee80211_vif *vif)
10924 {
10925 	struct ath12k_tx_desc_info *tx_desc_info;
10926 	struct ath12k_skb_cb *skb_cb;
10927 	struct sk_buff *skb;
10928 	int i;
10929 
10930 	for (i = 0; i < ATH12K_HW_MAX_QUEUES; i++) {
10931 		spin_lock_bh(&dp->tx_desc_lock[i]);
10932 
10933 		list_for_each_entry(tx_desc_info, &dp->tx_desc_used_list[i],
10934 				    list) {
10935 			skb = tx_desc_info->skb;
10936 			if (!skb)
10937 				continue;
10938 
10939 			skb_cb = ATH12K_SKB_CB(skb);
10940 			if (skb_cb->vif == vif)
10941 				skb_cb->vif = NULL;
10942 		}
10943 
10944 		spin_unlock_bh(&dp->tx_desc_lock[i]);
10945 	}
10946 }
10947 
10948 static int ath12k_mac_vdev_delete(struct ath12k *ar, struct ath12k_link_vif *arvif)
10949 {
10950 	struct ath12k_vif *ahvif = arvif->ahvif;
10951 	struct ieee80211_vif *vif = ath12k_ahvif_to_vif(ahvif);
10952 	struct ath12k_dp_link_vif *dp_link_vif;
10953 	struct ath12k_base *ab = ar->ab;
10954 	unsigned long time_left;
10955 	int ret;
10956 
10957 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
10958 
10959 	reinit_completion(&ar->vdev_delete_done);
10960 
10961 	ret = ath12k_wmi_vdev_delete(ar, arvif->vdev_id);
10962 	if (ret) {
10963 		ath12k_warn(ab, "failed to delete WMI vdev %d: %d\n",
10964 			    arvif->vdev_id, ret);
10965 		goto err_vdev_del;
10966 	}
10967 
10968 	time_left = wait_for_completion_timeout(&ar->vdev_delete_done,
10969 						ATH12K_VDEV_DELETE_TIMEOUT_HZ);
10970 	if (time_left == 0) {
10971 		ath12k_warn(ab, "Timeout in receiving vdev delete response\n");
10972 		goto err_vdev_del;
10973 	}
10974 
10975 	ab->free_vdev_map |= 1LL << arvif->vdev_id;
10976 	ar->allocated_vdev_map &= ~(1LL << arvif->vdev_id);
10977 	ar->num_created_vdevs--;
10978 
10979 	if (ahvif->vdev_type == WMI_VDEV_TYPE_MONITOR) {
10980 		ar->monitor_vdev_id = -1;
10981 		ar->monitor_vdev_created = false;
10982 	}
10983 
10984 	ath12k_dbg(ab, ATH12K_DBG_MAC, "vdev %pM deleted, vdev_id %d\n",
10985 		   vif->addr, arvif->vdev_id);
10986 
10987 err_vdev_del:
10988 	spin_lock_bh(&ar->data_lock);
10989 	list_del(&arvif->list);
10990 	spin_unlock_bh(&ar->data_lock);
10991 
10992 	ath12k_peer_cleanup(ar, arvif->vdev_id);
10993 	ath12k_ahvif_put_link_cache(ahvif, arvif->link_id);
10994 
10995 	ath12k_mac_vif_txmgmt_cleanup(ar, vif);
10996 
10997 	ath12k_mac_vif_unref(ath12k_ab_to_dp(ab), vif);
10998 
10999 	dp_link_vif = ath12k_dp_vif_to_dp_link_vif(&ahvif->dp_vif, arvif->link_id);
11000 	ath12k_dp_tx_put_bank_profile(ath12k_ab_to_dp(ab), dp_link_vif->bank_id);
11001 
11002 	/* Recalc txpower for remaining vdev */
11003 	ath12k_mac_txpower_recalc(ar);
11004 
11005 	/* TODO: recal traffic pause state based on the available vdevs */
11006 	arvif->is_created = false;
11007 	arvif->ar = NULL;
11008 
11009 	return ret;
11010 }
11011 
11012 void ath12k_mac_op_remove_interface(struct ieee80211_hw *hw,
11013 				    struct ieee80211_vif *vif)
11014 {
11015 	struct ath12k_vif *ahvif = ath12k_vif_to_ahvif(vif);
11016 	struct ath12k_link_vif *arvif;
11017 	struct ath12k *ar;
11018 	u8 link_id;
11019 
11020 	lockdep_assert_wiphy(hw->wiphy);
11021 
11022 	for (link_id = 0; link_id < ATH12K_NUM_MAX_LINKS; link_id++) {
11023 		/* if we cached some config but never received assign chanctx,
11024 		 * free the allocated cache.
11025 		 */
11026 		ath12k_ahvif_put_link_cache(ahvif, link_id);
11027 		arvif = wiphy_dereference(hw->wiphy, ahvif->link[link_id]);
11028 		if (!arvif || !arvif->is_created)
11029 			continue;
11030 
11031 		ar = arvif->ar;
11032 
11033 		/* Scan abortion is in progress since before this, cancel_hw_scan()
11034 		 * is expected to be executed. Since link is anyways going to be removed
11035 		 * now, just cancel the worker and send the scan aborted to user space
11036 		 */
11037 		if (ar->scan.arvif == arvif) {
11038 			wiphy_work_cancel(hw->wiphy, &ar->scan.vdev_clean_wk);
11039 
11040 			spin_lock_bh(&ar->data_lock);
11041 			ar->scan.arvif = NULL;
11042 			if (!ar->scan.is_roc) {
11043 				struct cfg80211_scan_info info = {
11044 					.aborted = true,
11045 				};
11046 
11047 				ath12k_mac_scan_send_complete(ar, &info);
11048 			}
11049 
11050 			ar->scan.state = ATH12K_SCAN_IDLE;
11051 			ar->scan_channel = NULL;
11052 			ar->scan.roc_freq = 0;
11053 			spin_unlock_bh(&ar->data_lock);
11054 		}
11055 
11056 		ath12k_mac_remove_link_interface(hw, arvif);
11057 		ath12k_mac_unassign_link_vif(arvif);
11058 	}
11059 }
11060 EXPORT_SYMBOL(ath12k_mac_op_remove_interface);
11061 
11062 /* FIXME: Has to be verified. */
11063 #define SUPPORTED_FILTERS			\
11064 	(FIF_ALLMULTI |				\
11065 	FIF_CONTROL |				\
11066 	FIF_PSPOLL |				\
11067 	FIF_OTHER_BSS |				\
11068 	FIF_BCN_PRBRESP_PROMISC |		\
11069 	FIF_PROBE_REQ |				\
11070 	FIF_FCSFAIL)
11071 
11072 void ath12k_mac_op_configure_filter(struct ieee80211_hw *hw,
11073 				    unsigned int changed_flags,
11074 				    unsigned int *total_flags,
11075 				    u64 multicast)
11076 {
11077 	struct ath12k_hw *ah = ath12k_hw_to_ah(hw);
11078 	struct ath12k *ar;
11079 
11080 	lockdep_assert_wiphy(hw->wiphy);
11081 
11082 	ar = ath12k_ah_to_ar(ah, 0);
11083 
11084 	*total_flags &= SUPPORTED_FILTERS;
11085 	ar->filter_flags = *total_flags;
11086 }
11087 EXPORT_SYMBOL(ath12k_mac_op_configure_filter);
11088 
11089 int ath12k_mac_op_get_antenna(struct ieee80211_hw *hw, int radio_idx,
11090 			      u32 *tx_ant, u32 *rx_ant)
11091 {
11092 	struct ath12k_hw *ah = ath12k_hw_to_ah(hw);
11093 	int antennas_rx = 0, antennas_tx = 0;
11094 	struct ath12k *ar;
11095 	int i;
11096 
11097 	lockdep_assert_wiphy(hw->wiphy);
11098 
11099 	for_each_ar(ah, ar, i) {
11100 		antennas_rx = max_t(u32, antennas_rx, ar->cfg_rx_chainmask);
11101 		antennas_tx = max_t(u32, antennas_tx, ar->cfg_tx_chainmask);
11102 	}
11103 
11104 	*tx_ant = antennas_tx;
11105 	*rx_ant = antennas_rx;
11106 
11107 	return 0;
11108 }
11109 EXPORT_SYMBOL(ath12k_mac_op_get_antenna);
11110 
11111 int ath12k_mac_op_set_antenna(struct ieee80211_hw *hw, int radio_idx,
11112 			      u32 tx_ant, u32 rx_ant)
11113 {
11114 	struct ath12k_hw *ah = ath12k_hw_to_ah(hw);
11115 	struct ath12k *ar;
11116 	int ret = 0;
11117 	int i;
11118 
11119 	lockdep_assert_wiphy(hw->wiphy);
11120 
11121 	for_each_ar(ah, ar, i) {
11122 		ret = __ath12k_set_antenna(ar, tx_ant, rx_ant);
11123 		if (ret)
11124 			break;
11125 	}
11126 
11127 	return ret;
11128 }
11129 EXPORT_SYMBOL(ath12k_mac_op_set_antenna);
11130 
11131 static int ath12k_mac_ampdu_action(struct ieee80211_hw *hw,
11132 				   struct ieee80211_vif *vif,
11133 				   struct ieee80211_ampdu_params *params,
11134 				   u8 link_id)
11135 {
11136 	struct ath12k *ar;
11137 	int ret = -EINVAL;
11138 
11139 	lockdep_assert_wiphy(hw->wiphy);
11140 
11141 	ar = ath12k_get_ar_by_vif(hw, vif, link_id);
11142 	if (!ar)
11143 		return -EINVAL;
11144 
11145 	switch (params->action) {
11146 	case IEEE80211_AMPDU_RX_START:
11147 		ret = ath12k_dp_rx_ampdu_start(ar, params, link_id);
11148 		break;
11149 	case IEEE80211_AMPDU_RX_STOP:
11150 		ret = ath12k_dp_rx_ampdu_stop(ar, params, link_id);
11151 		break;
11152 	case IEEE80211_AMPDU_TX_START:
11153 	case IEEE80211_AMPDU_TX_STOP_CONT:
11154 	case IEEE80211_AMPDU_TX_STOP_FLUSH:
11155 	case IEEE80211_AMPDU_TX_STOP_FLUSH_CONT:
11156 	case IEEE80211_AMPDU_TX_OPERATIONAL:
11157 		/* Tx A-MPDU aggregation offloaded to hw/fw so deny mac80211
11158 		 * Tx aggregation requests.
11159 		 */
11160 		ret = -EOPNOTSUPP;
11161 		break;
11162 	}
11163 
11164 	if (ret)
11165 		ath12k_warn(ar->ab, "unable to perform ampdu action %d for vif %pM link %u ret %d\n",
11166 			    params->action, vif->addr, link_id, ret);
11167 
11168 	return ret;
11169 }
11170 
11171 int ath12k_mac_op_ampdu_action(struct ieee80211_hw *hw,
11172 			       struct ieee80211_vif *vif,
11173 			       struct ieee80211_ampdu_params *params)
11174 {
11175 	struct ieee80211_sta *sta = params->sta;
11176 	struct ath12k_sta *ahsta = ath12k_sta_to_ahsta(sta);
11177 	unsigned long links_map = ahsta->links_map;
11178 	int ret = -EINVAL;
11179 	u8 link_id;
11180 
11181 	lockdep_assert_wiphy(hw->wiphy);
11182 
11183 	if (WARN_ON(!links_map))
11184 		return ret;
11185 
11186 	for_each_set_bit(link_id, &links_map, IEEE80211_MLD_MAX_NUM_LINKS) {
11187 		ret = ath12k_mac_ampdu_action(hw, vif, params, link_id);
11188 		if (ret)
11189 			return ret;
11190 	}
11191 
11192 	return 0;
11193 }
11194 EXPORT_SYMBOL(ath12k_mac_op_ampdu_action);
11195 
11196 int ath12k_mac_op_add_chanctx(struct ieee80211_hw *hw,
11197 			      struct ieee80211_chanctx_conf *ctx)
11198 {
11199 	struct ath12k *ar;
11200 	struct ath12k_base *ab;
11201 
11202 	lockdep_assert_wiphy(hw->wiphy);
11203 
11204 	ar = ath12k_get_ar_by_ctx(hw, ctx);
11205 	if (!ar)
11206 		return -EINVAL;
11207 
11208 	ab = ar->ab;
11209 
11210 	ath12k_dbg(ab, ATH12K_DBG_MAC,
11211 		   "mac chanctx add freq %u width %d ptr %p\n",
11212 		   ctx->def.chan->center_freq, ctx->def.width, ctx);
11213 
11214 	spin_lock_bh(&ar->data_lock);
11215 	/* TODO: In case of multiple channel context, populate rx_channel from
11216 	 * Rx PPDU desc information.
11217 	 */
11218 	ar->rx_channel = ctx->def.chan;
11219 	spin_unlock_bh(&ar->data_lock);
11220 	ar->chan_tx_pwr = ATH12K_PDEV_TX_POWER_INVALID;
11221 
11222 	return 0;
11223 }
11224 EXPORT_SYMBOL(ath12k_mac_op_add_chanctx);
11225 
11226 void ath12k_mac_op_remove_chanctx(struct ieee80211_hw *hw,
11227 				  struct ieee80211_chanctx_conf *ctx)
11228 {
11229 	struct ath12k *ar;
11230 	struct ath12k_base *ab;
11231 
11232 	lockdep_assert_wiphy(hw->wiphy);
11233 
11234 	ar = ath12k_get_ar_by_ctx(hw, ctx);
11235 	if (!ar)
11236 		return;
11237 
11238 	ab = ar->ab;
11239 
11240 	ath12k_dbg(ab, ATH12K_DBG_MAC,
11241 		   "mac chanctx remove freq %u width %d ptr %p\n",
11242 		   ctx->def.chan->center_freq, ctx->def.width, ctx);
11243 
11244 	spin_lock_bh(&ar->data_lock);
11245 	/* TODO: In case of there is one more channel context left, populate
11246 	 * rx_channel with the channel of that remaining channel context.
11247 	 */
11248 	ar->rx_channel = NULL;
11249 	spin_unlock_bh(&ar->data_lock);
11250 	ar->chan_tx_pwr = ATH12K_PDEV_TX_POWER_INVALID;
11251 }
11252 EXPORT_SYMBOL(ath12k_mac_op_remove_chanctx);
11253 
11254 static enum wmi_phy_mode
11255 ath12k_mac_check_down_grade_phy_mode(struct ath12k *ar,
11256 				     enum wmi_phy_mode mode,
11257 				     enum nl80211_band band,
11258 				     enum nl80211_iftype type)
11259 {
11260 	struct ieee80211_sta_eht_cap *eht_cap = NULL;
11261 	enum wmi_phy_mode down_mode;
11262 	int n = ar->mac.sbands[band].n_iftype_data;
11263 	int i;
11264 	struct ieee80211_sband_iftype_data *data;
11265 
11266 	if (mode < MODE_11BE_EHT20)
11267 		return mode;
11268 
11269 	data = ar->mac.iftype[band];
11270 	for (i = 0; i < n; i++) {
11271 		if (data[i].types_mask & BIT(type)) {
11272 			eht_cap = &data[i].eht_cap;
11273 			break;
11274 		}
11275 	}
11276 
11277 	if (eht_cap && eht_cap->has_eht)
11278 		return mode;
11279 
11280 	switch (mode) {
11281 	case MODE_11BE_EHT20:
11282 		down_mode = MODE_11AX_HE20;
11283 		break;
11284 	case MODE_11BE_EHT40:
11285 		down_mode = MODE_11AX_HE40;
11286 		break;
11287 	case MODE_11BE_EHT80:
11288 		down_mode = MODE_11AX_HE80;
11289 		break;
11290 	case MODE_11BE_EHT80_80:
11291 		down_mode = MODE_11AX_HE80_80;
11292 		break;
11293 	case MODE_11BE_EHT160:
11294 	case MODE_11BE_EHT160_160:
11295 	case MODE_11BE_EHT320:
11296 		down_mode = MODE_11AX_HE160;
11297 		break;
11298 	case MODE_11BE_EHT20_2G:
11299 		down_mode = MODE_11AX_HE20_2G;
11300 		break;
11301 	case MODE_11BE_EHT40_2G:
11302 		down_mode = MODE_11AX_HE40_2G;
11303 		break;
11304 	default:
11305 		down_mode = mode;
11306 		break;
11307 	}
11308 
11309 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
11310 		   "mac vdev start phymode %s downgrade to %s\n",
11311 		   ath12k_mac_phymode_str(mode),
11312 		   ath12k_mac_phymode_str(down_mode));
11313 
11314 	return down_mode;
11315 }
11316 
11317 static void
11318 ath12k_mac_mlo_get_vdev_args(struct ath12k_link_vif *arvif,
11319 			     struct wmi_ml_arg *ml_arg)
11320 {
11321 	struct ath12k_vif *ahvif = arvif->ahvif;
11322 	struct wmi_ml_partner_info *partner_info;
11323 	struct ieee80211_bss_conf *link_conf;
11324 	struct ath12k_link_vif *arvif_p;
11325 	unsigned long links;
11326 	u8 link_id;
11327 
11328 	lockdep_assert_wiphy(ahvif->ah->hw->wiphy);
11329 
11330 	if (!ath12k_mac_is_ml_arvif(arvif))
11331 		return;
11332 
11333 	if (hweight16(ahvif->vif->valid_links) > ATH12K_WMI_MLO_MAX_LINKS)
11334 		return;
11335 
11336 	ml_arg->enabled = true;
11337 
11338 	/* Driver always add a new link via VDEV START, FW takes
11339 	 * care of internally adding this link to existing
11340 	 * link vdevs which are advertised as partners below
11341 	 */
11342 	ml_arg->link_add = true;
11343 
11344 	ml_arg->assoc_link = arvif->is_sta_assoc_link;
11345 
11346 	ml_arg->ieee_link_id = arvif->link_id;
11347 
11348 	partner_info = ml_arg->partner_info;
11349 
11350 	links = ahvif->links_map;
11351 	for_each_set_bit(link_id, &links, IEEE80211_MLD_MAX_NUM_LINKS) {
11352 		arvif_p = wiphy_dereference(ahvif->ah->hw->wiphy, ahvif->link[link_id]);
11353 
11354 		if (WARN_ON(!arvif_p))
11355 			continue;
11356 
11357 		if (arvif == arvif_p)
11358 			continue;
11359 
11360 		if (!arvif_p->is_started)
11361 			continue;
11362 
11363 		link_conf = wiphy_dereference(ahvif->ah->hw->wiphy,
11364 					      ahvif->vif->link_conf[arvif_p->link_id]);
11365 
11366 		if (!link_conf)
11367 			continue;
11368 
11369 		partner_info->vdev_id = arvif_p->vdev_id;
11370 		partner_info->hw_link_id = arvif_p->ar->pdev->hw_link_id;
11371 		partner_info->ieee_link_id = arvif_p->link_id;
11372 		ether_addr_copy(partner_info->addr, link_conf->addr);
11373 		ml_arg->num_partner_links++;
11374 		partner_info++;
11375 	}
11376 }
11377 
11378 static int
11379 ath12k_mac_vdev_start_restart(struct ath12k_link_vif *arvif,
11380 			      struct ieee80211_chanctx_conf *ctx,
11381 			      bool restart)
11382 {
11383 	struct ath12k *ar = arvif->ar;
11384 	struct ath12k_base *ab = ar->ab;
11385 	struct wmi_vdev_start_req_arg arg = {};
11386 	const struct cfg80211_chan_def *chandef = &ctx->def;
11387 	struct ieee80211_hw *hw = ath12k_ar_to_hw(ar);
11388 	struct ath12k_vif *ahvif = arvif->ahvif;
11389 	struct ieee80211_bss_conf *link_conf;
11390 	unsigned int dfs_cac_time;
11391 	int ret;
11392 
11393 	lockdep_assert_wiphy(hw->wiphy);
11394 
11395 	link_conf = ath12k_mac_get_link_bss_conf(arvif);
11396 	if (!link_conf) {
11397 		ath12k_warn(ar->ab, "unable to access bss link conf in vdev start for vif %pM link %u\n",
11398 			    ahvif->vif->addr, arvif->link_id);
11399 		return -ENOLINK;
11400 	}
11401 
11402 	reinit_completion(&ar->vdev_setup_done);
11403 
11404 	arg.vdev_id = arvif->vdev_id;
11405 	arg.dtim_period = arvif->dtim_period;
11406 	arg.bcn_intval = arvif->beacon_interval;
11407 	arg.punct_bitmap = ~arvif->punct_bitmap;
11408 
11409 	arg.freq = chandef->chan->center_freq;
11410 	arg.band_center_freq1 = chandef->center_freq1;
11411 	arg.band_center_freq2 = chandef->center_freq2;
11412 	arg.mode = ath12k_phymodes[chandef->chan->band][chandef->width];
11413 
11414 	arg.mode = ath12k_mac_check_down_grade_phy_mode(ar, arg.mode,
11415 							chandef->chan->band,
11416 							ahvif->vif->type);
11417 	arg.min_power = 0;
11418 	arg.max_power = chandef->chan->max_power;
11419 	arg.max_reg_power = chandef->chan->max_reg_power;
11420 	arg.max_antenna_gain = chandef->chan->max_antenna_gain;
11421 
11422 	arg.pref_tx_streams = ar->num_tx_chains;
11423 	arg.pref_rx_streams = ar->num_rx_chains;
11424 
11425 	arg.mbssid_flags = WMI_VDEV_MBSSID_FLAGS_NON_MBSSID_AP;
11426 	arg.mbssid_tx_vdev_id = 0;
11427 	if (test_bit(WMI_TLV_SERVICE_MBSS_PARAM_IN_VDEV_START_SUPPORT,
11428 		     ar->ab->wmi_ab.svc_map)) {
11429 		ret = ath12k_mac_setup_vdev_params_mbssid(arvif,
11430 							  &arg.mbssid_flags,
11431 							  &arg.mbssid_tx_vdev_id);
11432 		if (ret)
11433 			return ret;
11434 	}
11435 
11436 	if (ahvif->vdev_type == WMI_VDEV_TYPE_AP) {
11437 		arg.ssid = ahvif->u.ap.ssid;
11438 		arg.ssid_len = ahvif->u.ap.ssid_len;
11439 		arg.hidden_ssid = ahvif->u.ap.hidden_ssid;
11440 
11441 		/* For now allow DFS for AP mode */
11442 		arg.chan_radar = !!(chandef->chan->flags & IEEE80211_CHAN_RADAR);
11443 
11444 		arg.freq2_radar = ctx->radar_enabled;
11445 
11446 		arg.passive = arg.chan_radar;
11447 
11448 		spin_lock_bh(&ab->base_lock);
11449 		arg.regdomain = ar->ab->dfs_region;
11450 		spin_unlock_bh(&ab->base_lock);
11451 
11452 		/* TODO: Notify if secondary 80Mhz also needs radar detection */
11453 	}
11454 
11455 	arg.passive |= !!(chandef->chan->flags & IEEE80211_CHAN_NO_IR);
11456 
11457 	if (!restart)
11458 		ath12k_mac_mlo_get_vdev_args(arvif, &arg.ml);
11459 
11460 	ath12k_dbg(ab, ATH12K_DBG_MAC,
11461 		   "mac vdev %d start center_freq %d phymode %s punct_bitmap 0x%x\n",
11462 		   arg.vdev_id, arg.freq,
11463 		   ath12k_mac_phymode_str(arg.mode), arg.punct_bitmap);
11464 
11465 	ret = ath12k_wmi_vdev_start(ar, &arg, restart);
11466 	if (ret) {
11467 		ath12k_warn(ar->ab, "failed to %s WMI vdev %i\n",
11468 			    restart ? "restart" : "start", arg.vdev_id);
11469 		return ret;
11470 	}
11471 
11472 	ret = ath12k_mac_vdev_setup_sync(ar);
11473 	if (ret) {
11474 		ath12k_warn(ab, "failed to synchronize setup for vdev %i %s: %d\n",
11475 			    arg.vdev_id, restart ? "restart" : "start", ret);
11476 		return ret;
11477 	}
11478 
11479 	/* TODO: For now we only set TPC power here. However when
11480 	 * channel changes, say CSA, it should be updated again.
11481 	 */
11482 	if (ath12k_mac_supports_tpc(ar, ahvif, chandef)) {
11483 		ath12k_mac_fill_reg_tpc_info(ar, arvif, ctx);
11484 		ath12k_wmi_send_vdev_set_tpc_power(ar, arvif->vdev_id,
11485 						   &arvif->reg_tpc_info);
11486 	}
11487 
11488 	ar->num_started_vdevs++;
11489 	ath12k_dbg(ab, ATH12K_DBG_MAC,  "vdev %pM started, vdev_id %d\n",
11490 		   ahvif->vif->addr, arvif->vdev_id);
11491 
11492 	/* Enable CAC Running Flag in the driver by checking all sub-channel's DFS
11493 	 * state as NL80211_DFS_USABLE which indicates CAC needs to be
11494 	 * done before channel usage. This flag is used to drop rx packets.
11495 	 * during CAC.
11496 	 */
11497 	/* TODO: Set the flag for other interface types as required */
11498 	if (arvif->ahvif->vdev_type == WMI_VDEV_TYPE_AP && ctx->radar_enabled &&
11499 	    cfg80211_chandef_dfs_usable(hw->wiphy, chandef)) {
11500 		set_bit(ATH12K_FLAG_CAC_RUNNING, &ar->dev_flags);
11501 		dfs_cac_time = cfg80211_chandef_dfs_cac_time(hw->wiphy, chandef);
11502 
11503 		ath12k_dbg(ab, ATH12K_DBG_MAC,
11504 			   "CAC started dfs_cac_time %u center_freq %d center_freq1 %d for vdev %d\n",
11505 			   dfs_cac_time, arg.freq, arg.band_center_freq1, arg.vdev_id);
11506 	}
11507 
11508 	ret = ath12k_mac_set_txbf_conf(arvif);
11509 	if (ret)
11510 		ath12k_warn(ab, "failed to set txbf conf for vdev %d: %d\n",
11511 			    arvif->vdev_id, ret);
11512 
11513 	return 0;
11514 }
11515 
11516 static int ath12k_mac_vdev_start(struct ath12k_link_vif *arvif,
11517 				 struct ieee80211_chanctx_conf *ctx)
11518 {
11519 	return ath12k_mac_vdev_start_restart(arvif, ctx, false);
11520 }
11521 
11522 static int ath12k_mac_vdev_restart(struct ath12k_link_vif *arvif,
11523 				   struct ieee80211_chanctx_conf *ctx)
11524 {
11525 	return ath12k_mac_vdev_start_restart(arvif, ctx, true);
11526 }
11527 
11528 struct ath12k_mac_change_chanctx_arg {
11529 	struct ieee80211_chanctx_conf *ctx;
11530 	struct ieee80211_vif_chanctx_switch *vifs;
11531 	int n_vifs;
11532 	int next_vif;
11533 	struct ath12k *ar;
11534 };
11535 
11536 static void
11537 ath12k_mac_change_chanctx_cnt_iter(void *data, u8 *mac,
11538 				   struct ieee80211_vif *vif)
11539 {
11540 	struct ath12k_vif *ahvif = ath12k_vif_to_ahvif(vif);
11541 	struct ath12k_mac_change_chanctx_arg *arg = data;
11542 	struct ieee80211_bss_conf *link_conf;
11543 	struct ath12k_link_vif *arvif;
11544 	unsigned long links_map;
11545 	u8 link_id;
11546 
11547 	lockdep_assert_wiphy(ahvif->ah->hw->wiphy);
11548 
11549 	links_map = ahvif->links_map;
11550 	for_each_set_bit(link_id, &links_map, IEEE80211_MLD_MAX_NUM_LINKS) {
11551 		arvif = wiphy_dereference(ahvif->ah->hw->wiphy, ahvif->link[link_id]);
11552 		if (WARN_ON(!arvif))
11553 			continue;
11554 
11555 		if (!arvif->is_created || arvif->ar != arg->ar)
11556 			continue;
11557 
11558 		link_conf = wiphy_dereference(ahvif->ah->hw->wiphy,
11559 					      vif->link_conf[link_id]);
11560 		if (WARN_ON(!link_conf))
11561 			continue;
11562 
11563 		if (rcu_access_pointer(link_conf->chanctx_conf) != arg->ctx)
11564 			continue;
11565 
11566 		arg->n_vifs++;
11567 	}
11568 }
11569 
11570 static void
11571 ath12k_mac_change_chanctx_fill_iter(void *data, u8 *mac,
11572 				    struct ieee80211_vif *vif)
11573 {
11574 	struct ath12k_vif *ahvif = ath12k_vif_to_ahvif(vif);
11575 	struct ath12k_mac_change_chanctx_arg *arg = data;
11576 	struct ieee80211_bss_conf *link_conf;
11577 	struct ieee80211_chanctx_conf *ctx;
11578 	struct ath12k_link_vif *arvif;
11579 	unsigned long links_map;
11580 	u8 link_id;
11581 
11582 	lockdep_assert_wiphy(ahvif->ah->hw->wiphy);
11583 
11584 	links_map = ahvif->links_map;
11585 	for_each_set_bit(link_id, &links_map, IEEE80211_MLD_MAX_NUM_LINKS) {
11586 		arvif = wiphy_dereference(ahvif->ah->hw->wiphy, ahvif->link[link_id]);
11587 		if (WARN_ON(!arvif))
11588 			continue;
11589 
11590 		if (!arvif->is_created || arvif->ar != arg->ar)
11591 			continue;
11592 
11593 		link_conf = wiphy_dereference(ahvif->ah->hw->wiphy,
11594 					      vif->link_conf[arvif->link_id]);
11595 		if (WARN_ON(!link_conf))
11596 			continue;
11597 
11598 		ctx = rcu_access_pointer(link_conf->chanctx_conf);
11599 		if (ctx != arg->ctx)
11600 			continue;
11601 
11602 		if (WARN_ON(arg->next_vif == arg->n_vifs))
11603 			return;
11604 
11605 		arg->vifs[arg->next_vif].vif = vif;
11606 		arg->vifs[arg->next_vif].old_ctx = ctx;
11607 		arg->vifs[arg->next_vif].new_ctx = ctx;
11608 		arg->vifs[arg->next_vif].link_conf = link_conf;
11609 		arg->next_vif++;
11610 	}
11611 }
11612 
11613 static u32 ath12k_mac_nlwidth_to_wmiwidth(enum nl80211_chan_width width)
11614 {
11615 	switch (width) {
11616 	case NL80211_CHAN_WIDTH_20:
11617 		return WMI_CHAN_WIDTH_20;
11618 	case NL80211_CHAN_WIDTH_40:
11619 		return WMI_CHAN_WIDTH_40;
11620 	case NL80211_CHAN_WIDTH_80:
11621 		return WMI_CHAN_WIDTH_80;
11622 	case NL80211_CHAN_WIDTH_160:
11623 		return WMI_CHAN_WIDTH_160;
11624 	case NL80211_CHAN_WIDTH_80P80:
11625 		return WMI_CHAN_WIDTH_80P80;
11626 	case NL80211_CHAN_WIDTH_5:
11627 		return WMI_CHAN_WIDTH_5;
11628 	case NL80211_CHAN_WIDTH_10:
11629 		return WMI_CHAN_WIDTH_10;
11630 	case NL80211_CHAN_WIDTH_320:
11631 		return WMI_CHAN_WIDTH_320;
11632 	default:
11633 		WARN_ON(1);
11634 		return WMI_CHAN_WIDTH_20;
11635 	}
11636 }
11637 
11638 static int ath12k_mac_update_peer_puncturing_width(struct ath12k *ar,
11639 						   struct ath12k_link_vif *arvif,
11640 						   struct cfg80211_chan_def def)
11641 {
11642 	u32 param_id, param_value;
11643 	int ret;
11644 
11645 	if (arvif->ahvif->vdev_type != WMI_VDEV_TYPE_STA)
11646 		return 0;
11647 
11648 	param_id = WMI_PEER_CHWIDTH_PUNCTURE_20MHZ_BITMAP;
11649 	param_value = ath12k_mac_nlwidth_to_wmiwidth(def.width) |
11650 		u32_encode_bits((~def.punctured),
11651 				WMI_PEER_PUNCTURE_BITMAP);
11652 
11653 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
11654 		   "punctured bitmap %02x width %d vdev %d\n",
11655 		   def.punctured, def.width, arvif->vdev_id);
11656 
11657 	ret = ath12k_wmi_set_peer_param(ar, arvif->bssid,
11658 					arvif->vdev_id, param_id,
11659 					param_value);
11660 
11661 	return ret;
11662 }
11663 
11664 static void
11665 ath12k_mac_update_vif_chan(struct ath12k *ar,
11666 			   struct ieee80211_vif_chanctx_switch *vifs,
11667 			   int n_vifs)
11668 {
11669 	struct ath12k_incumbent_signal_interference *incumbent;
11670 	struct ath12k_wmi_vdev_up_params params = {};
11671 	struct ieee80211_bss_conf *link_conf;
11672 	struct cfg80211_chan_def *chandef;
11673 	struct ath12k_base *ab = ar->ab;
11674 	struct ath12k_link_vif *arvif;
11675 	struct ieee80211_vif *vif;
11676 	struct ath12k_vif *ahvif;
11677 	u8 link_id;
11678 	int ret;
11679 	int i;
11680 	bool monitor_vif = false;
11681 
11682 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
11683 
11684 	for (i = 0; i < n_vifs; i++) {
11685 		vif = vifs[i].vif;
11686 		ahvif = ath12k_vif_to_ahvif(vif);
11687 		link_conf = vifs[i].link_conf;
11688 		link_id = link_conf->link_id;
11689 		arvif = wiphy_dereference(ath12k_ar_to_hw(ar)->wiphy,
11690 					  ahvif->link[link_id]);
11691 
11692 		if (vif->type == NL80211_IFTYPE_MONITOR) {
11693 			monitor_vif = true;
11694 			continue;
11695 		}
11696 
11697 		if (WARN_ON(!arvif))
11698 			continue;
11699 
11700 		ath12k_dbg(ab, ATH12K_DBG_MAC,
11701 			   "mac chanctx switch vdev_id %i freq %u->%u width %d->%d\n",
11702 			   arvif->vdev_id,
11703 			   vifs[i].old_ctx->def.chan->center_freq,
11704 			   vifs[i].new_ctx->def.chan->center_freq,
11705 			   vifs[i].old_ctx->def.width,
11706 			   vifs[i].new_ctx->def.width);
11707 
11708 		if (WARN_ON(!arvif->is_started))
11709 			continue;
11710 
11711 		arvif->punct_bitmap = vifs[i].new_ctx->def.punctured;
11712 
11713 		/* Firmware expect vdev_restart only if vdev is up.
11714 		 * If vdev is down then it expect vdev_stop->vdev_start.
11715 		 */
11716 		if (arvif->is_up) {
11717 			ret = ath12k_mac_vdev_restart(arvif, vifs[i].new_ctx);
11718 			if (ret) {
11719 				ath12k_warn(ab, "failed to restart vdev %d: %d\n",
11720 					    arvif->vdev_id, ret);
11721 				continue;
11722 			}
11723 		} else {
11724 			ret = ath12k_mac_vdev_stop(arvif);
11725 			if (ret) {
11726 				ath12k_warn(ab, "failed to stop vdev %d: %d\n",
11727 					    arvif->vdev_id, ret);
11728 				continue;
11729 			}
11730 
11731 			ret = ath12k_mac_vdev_start(arvif, vifs[i].new_ctx);
11732 			if (ret)
11733 				ath12k_warn(ab, "failed to start vdev %d: %d\n",
11734 					    arvif->vdev_id, ret);
11735 			continue;
11736 		}
11737 
11738 		ret = ath12k_mac_update_peer_puncturing_width(arvif->ar, arvif,
11739 							      vifs[i].new_ctx->def);
11740 		if (ret) {
11741 			ath12k_warn(ar->ab,
11742 				    "failed to update puncturing bitmap %02x and width %d: %d\n",
11743 				    vifs[i].new_ctx->def.punctured,
11744 				    vifs[i].new_ctx->def.width, ret);
11745 			continue;
11746 		}
11747 
11748 		/* Defer VDEV bring-up during CSA to avoid installing stale
11749 		 * beacon templates. The beacon content is updated only
11750 		 * after CSA finalize, so we mark CSA in progress and skip
11751 		 * VDEV_UP for now. It will be handled later in
11752 		 * bss_info_changed().
11753 		 */
11754 		if (link_conf->csa_active &&
11755 		    arvif->ahvif->vdev_type == WMI_VDEV_TYPE_AP) {
11756 			arvif->is_csa_in_progress = true;
11757 			continue;
11758 		}
11759 
11760 		ret = ath12k_mac_setup_bcn_tmpl(arvif);
11761 		if (ret)
11762 			ath12k_warn(ab, "failed to update bcn tmpl during csa: %d\n",
11763 				    ret);
11764 
11765 		memset(&params, 0, sizeof(params));
11766 		params.vdev_id = arvif->vdev_id;
11767 		params.aid = ahvif->aid;
11768 		params.bssid = arvif->bssid;
11769 		params.tx_bssid = ath12k_mac_get_tx_bssid(arvif);
11770 		if (params.tx_bssid) {
11771 			params.nontx_profile_idx = link_conf->bssid_index;
11772 			params.nontx_profile_cnt = 1 << link_conf->bssid_indicator;
11773 		}
11774 		ret = ath12k_wmi_vdev_up(arvif->ar, &params);
11775 		if (ret) {
11776 			ath12k_warn(ab, "failed to bring vdev up %d: %d\n",
11777 				    arvif->vdev_id, ret);
11778 			continue;
11779 		}
11780 	}
11781 
11782 	/* Restart the internal monitor vdev on new channel */
11783 	if (!monitor_vif && ar->monitor_vdev_created) {
11784 		if (!ath12k_mac_monitor_stop(ar))
11785 			ath12k_mac_monitor_start(ar);
11786 	}
11787 
11788 	incumbent = &ar->incumbent_signal_interference;
11789 	spin_lock_bh(&ar->data_lock);
11790 	if (incumbent->handling_in_progress) {
11791 		chandef = &vifs[0].new_ctx->def;
11792 		if (incumbent->chan_bw_interference_bitmap &
11793 		    ATH12K_WMI_DCS_SEG_PRI20) {
11794 			if (incumbent->center_freq !=
11795 			    chandef->chan->center_freq) {
11796 				incumbent->chan_bw_interference_bitmap = 0;
11797 				incumbent->handling_in_progress = false;
11798 				ath12k_dbg(ab, ATH12K_DBG_MAC,
11799 					   "incumbent signal interference chan switch completed\n");
11800 			} else {
11801 				ath12k_warn(ab,
11802 					    "incumbent signal interference chan switch not done, freq %u\n",
11803 					    incumbent->center_freq);
11804 			}
11805 		} else {
11806 			if (incumbent->center_freq !=
11807 			    chandef->chan->center_freq ||
11808 			    incumbent->width != chandef->width) {
11809 				incumbent->chan_bw_interference_bitmap = 0;
11810 				incumbent->handling_in_progress = false;
11811 				ath12k_dbg(ab, ATH12K_DBG_MAC,
11812 					   "Bandwidth/channel change due to incumbent signal interference completed\n");
11813 			} else {
11814 				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",
11815 					    incumbent->center_freq,
11816 					    chandef->chan->center_freq,
11817 					    incumbent->width,
11818 					    chandef->width);
11819 			}
11820 		}
11821 	}
11822 	spin_unlock_bh(&ar->data_lock);
11823 }
11824 
11825 static void
11826 ath12k_mac_update_active_vif_chan(struct ath12k *ar,
11827 				  struct ieee80211_chanctx_conf *ctx)
11828 {
11829 	struct ath12k_mac_change_chanctx_arg arg = { .ctx = ctx, .ar = ar };
11830 	struct ieee80211_hw *hw = ath12k_ar_to_hw(ar);
11831 
11832 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
11833 
11834 	ieee80211_iterate_active_interfaces_atomic(hw,
11835 						   IEEE80211_IFACE_ITER_NORMAL,
11836 						   ath12k_mac_change_chanctx_cnt_iter,
11837 						   &arg);
11838 	if (arg.n_vifs == 0)
11839 		return;
11840 
11841 	arg.vifs = kzalloc_objs(arg.vifs[0], arg.n_vifs);
11842 	if (!arg.vifs)
11843 		return;
11844 
11845 	ieee80211_iterate_active_interfaces_atomic(hw,
11846 						   IEEE80211_IFACE_ITER_NORMAL,
11847 						   ath12k_mac_change_chanctx_fill_iter,
11848 						   &arg);
11849 
11850 	ath12k_mac_update_vif_chan(ar, arg.vifs, arg.n_vifs);
11851 
11852 	kfree(arg.vifs);
11853 }
11854 
11855 void ath12k_mac_op_change_chanctx(struct ieee80211_hw *hw,
11856 				  struct ieee80211_chanctx_conf *ctx,
11857 				  u32 changed)
11858 {
11859 	struct ath12k *ar;
11860 	struct ath12k_base *ab;
11861 
11862 	lockdep_assert_wiphy(hw->wiphy);
11863 
11864 	ar = ath12k_get_ar_by_ctx(hw, ctx);
11865 	if (!ar)
11866 		return;
11867 
11868 	ab = ar->ab;
11869 
11870 	ath12k_dbg(ab, ATH12K_DBG_MAC,
11871 		   "mac chanctx change freq %u width %d ptr %p changed %x\n",
11872 		   ctx->def.chan->center_freq, ctx->def.width, ctx, changed);
11873 
11874 	/* This shouldn't really happen because channel switching should use
11875 	 * switch_vif_chanctx().
11876 	 */
11877 	if (WARN_ON(changed & IEEE80211_CHANCTX_CHANGE_CHANNEL))
11878 		return;
11879 
11880 	if (changed & IEEE80211_CHANCTX_CHANGE_WIDTH ||
11881 	    changed & IEEE80211_CHANCTX_CHANGE_RADAR ||
11882 	    changed & IEEE80211_CHANCTX_CHANGE_PUNCTURING)
11883 		ath12k_mac_update_active_vif_chan(ar, ctx);
11884 
11885 	/* TODO: Recalc radar detection */
11886 }
11887 EXPORT_SYMBOL(ath12k_mac_op_change_chanctx);
11888 
11889 static int ath12k_start_vdev_delay(struct ath12k *ar,
11890 				   struct ath12k_link_vif *arvif)
11891 {
11892 	struct ath12k_base *ab = ar->ab;
11893 	struct ath12k_vif *ahvif = arvif->ahvif;
11894 	struct ieee80211_vif *vif = ath12k_ahvif_to_vif(arvif->ahvif);
11895 	struct ieee80211_chanctx_conf *chanctx;
11896 	struct ieee80211_bss_conf *link_conf;
11897 	int ret;
11898 
11899 	if (WARN_ON(arvif->is_started))
11900 		return -EBUSY;
11901 
11902 	link_conf = ath12k_mac_get_link_bss_conf(arvif);
11903 	if (!link_conf) {
11904 		ath12k_warn(ab, "failed to get link conf for vdev %u\n", arvif->vdev_id);
11905 		return -EINVAL;
11906 	}
11907 
11908 	chanctx	= wiphy_dereference(ath12k_ar_to_hw(arvif->ar)->wiphy,
11909 				    link_conf->chanctx_conf);
11910 	ret = ath12k_mac_vdev_start(arvif, chanctx);
11911 	if (ret) {
11912 		ath12k_warn(ab, "failed to start vdev %i addr %pM on freq %d: %d\n",
11913 			    arvif->vdev_id, vif->addr,
11914 			    chanctx->def.chan->center_freq, ret);
11915 		return ret;
11916 	}
11917 
11918 	if (ahvif->vdev_type == WMI_VDEV_TYPE_MONITOR) {
11919 		ret = ath12k_monitor_vdev_up(ar, arvif->vdev_id);
11920 		if (ret) {
11921 			ath12k_warn(ab, "failed put monitor up: %d\n", ret);
11922 			return ret;
11923 		}
11924 	}
11925 
11926 	arvif->is_started = true;
11927 
11928 	/* TODO: Setup ps and cts/rts protection */
11929 	return 0;
11930 }
11931 
11932 static u8 ath12k_mac_get_num_pwr_levels(struct cfg80211_chan_def *chan_def)
11933 {
11934 	if (chan_def->chan->flags & IEEE80211_CHAN_PSD) {
11935 		switch (chan_def->width) {
11936 		case NL80211_CHAN_WIDTH_20:
11937 			return 1;
11938 		case NL80211_CHAN_WIDTH_40:
11939 			return 2;
11940 		case NL80211_CHAN_WIDTH_80:
11941 			return 4;
11942 		case NL80211_CHAN_WIDTH_160:
11943 			return 8;
11944 		case NL80211_CHAN_WIDTH_320:
11945 			return 16;
11946 		default:
11947 			return 1;
11948 		}
11949 	} else {
11950 		switch (chan_def->width) {
11951 		case NL80211_CHAN_WIDTH_20:
11952 			return 1;
11953 		case NL80211_CHAN_WIDTH_40:
11954 			return 2;
11955 		case NL80211_CHAN_WIDTH_80:
11956 			return 3;
11957 		case NL80211_CHAN_WIDTH_160:
11958 			return 4;
11959 		case NL80211_CHAN_WIDTH_320:
11960 			return 5;
11961 		default:
11962 			return 1;
11963 		}
11964 	}
11965 }
11966 
11967 static u16 ath12k_mac_get_6ghz_start_frequency(struct cfg80211_chan_def *chan_def)
11968 {
11969 	u16 diff_seq;
11970 
11971 	/* It is to get the lowest channel number's center frequency of the chan.
11972 	 * For example,
11973 	 * bandwidth=40 MHz, center frequency is 5965, lowest channel is 1
11974 	 * with center frequency 5955, its diff is 5965 - 5955 = 10.
11975 	 * bandwidth=80 MHz, center frequency is 5985, lowest channel is 1
11976 	 * with center frequency 5955, its diff is 5985 - 5955 = 30.
11977 	 * bandwidth=160 MHz, center frequency is 6025, lowest channel is 1
11978 	 * with center frequency 5955, its diff is 6025 - 5955 = 70.
11979 	 * bandwidth=320 MHz, center frequency is 6105, lowest channel is 1
11980 	 * with center frequency 5955, its diff is 6105 - 5955 = 70.
11981 	 */
11982 	switch (chan_def->width) {
11983 	case NL80211_CHAN_WIDTH_320:
11984 		diff_seq = 150;
11985 		break;
11986 	case NL80211_CHAN_WIDTH_160:
11987 		diff_seq = 70;
11988 		break;
11989 	case NL80211_CHAN_WIDTH_80:
11990 		diff_seq = 30;
11991 		break;
11992 	case NL80211_CHAN_WIDTH_40:
11993 		diff_seq = 10;
11994 		break;
11995 	default:
11996 		diff_seq = 0;
11997 	}
11998 
11999 	return chan_def->center_freq1 - diff_seq;
12000 }
12001 
12002 static u16 ath12k_mac_get_seg_freq(struct cfg80211_chan_def *chan_def,
12003 				   u16 start_seq, u8 seq)
12004 {
12005 	u16 seg_seq;
12006 
12007 	/* It is to get the center frequency of the specific bandwidth.
12008 	 * start_seq means the lowest channel number's center frequency.
12009 	 * seq 0/1/2/3 means 20 MHz/40 MHz/80 MHz/160 MHz.
12010 	 * For example,
12011 	 * lowest channel is 1, its center frequency 5955,
12012 	 * center frequency is 5955 when bandwidth=20 MHz, its diff is 5955 - 5955 = 0.
12013 	 * lowest channel is 1, its center frequency 5955,
12014 	 * center frequency is 5965 when bandwidth=40 MHz, its diff is 5965 - 5955 = 10.
12015 	 * lowest channel is 1, its center frequency 5955,
12016 	 * center frequency is 5985 when bandwidth=80 MHz, its diff is 5985 - 5955 = 30.
12017 	 * lowest channel is 1, its center frequency 5955,
12018 	 * center frequency is 6025 when bandwidth=160 MHz, its diff is 6025 - 5955 = 70.
12019 	 */
12020 	seg_seq = 10 * (BIT(seq) - 1);
12021 	return seg_seq + start_seq;
12022 }
12023 
12024 static void ath12k_mac_get_psd_channel(struct ath12k *ar,
12025 				       u16 step_freq,
12026 				       u16 *start_freq,
12027 				       u16 *center_freq,
12028 				       u8 i,
12029 				       struct ieee80211_channel **temp_chan,
12030 				       s8 *tx_power)
12031 {
12032 	/* It is to get the center frequency for each 20 MHz.
12033 	 * For example, if the chan is 160 MHz and center frequency is 6025,
12034 	 * then it include 8 channels, they are 1/5/9/13/17/21/25/29,
12035 	 * channel number 1's center frequency is 5955, it is parameter start_freq.
12036 	 * parameter i is the step of the 8 channels. i is 0~7 for the 8 channels.
12037 	 * the channel 1/5/9/13/17/21/25/29 maps i=0/1/2/3/4/5/6/7,
12038 	 * and maps its center frequency is 5955/5975/5995/6015/6035/6055/6075/6095,
12039 	 * the gap is 20 for each channel, parameter step_freq means the gap.
12040 	 * after get the center frequency of each channel, it is easy to find the
12041 	 * struct ieee80211_channel of it and get the max_reg_power.
12042 	 */
12043 	*center_freq = *start_freq + i * step_freq;
12044 	*temp_chan = ieee80211_get_channel(ar->ah->hw->wiphy, *center_freq);
12045 	*tx_power = (*temp_chan)->max_reg_power;
12046 }
12047 
12048 static void ath12k_mac_get_eirp_power(struct ath12k *ar,
12049 				      u16 *start_freq,
12050 				      u16 *center_freq,
12051 				      u8 i,
12052 				      struct ieee80211_channel **temp_chan,
12053 				      struct cfg80211_chan_def *def,
12054 				      s8 *tx_power)
12055 {
12056 	/* It is to get the center frequency for 20 MHz/40 MHz/80 MHz/
12057 	 * 160 MHz bandwidth, and then plus 10 to the center frequency,
12058 	 * it is the center frequency of a channel number.
12059 	 * For example, when configured channel number is 1.
12060 	 * center frequency is 5965 when bandwidth=40 MHz, after plus 10, it is 5975,
12061 	 * then it is channel number 5.
12062 	 * center frequency is 5985 when bandwidth=80 MHz, after plus 10, it is 5995,
12063 	 * then it is channel number 9.
12064 	 * center frequency is 6025 when bandwidth=160 MHz, after plus 10, it is 6035,
12065 	 * then it is channel number 17.
12066 	 * after get the center frequency of each channel, it is easy to find the
12067 	 * struct ieee80211_channel of it and get the max_reg_power.
12068 	 */
12069 	*center_freq = ath12k_mac_get_seg_freq(def, *start_freq, i);
12070 
12071 	/* For the 20 MHz, its center frequency is same with same channel */
12072 	if (i != 0)
12073 		*center_freq += 10;
12074 
12075 	*temp_chan = ieee80211_get_channel(ar->ah->hw->wiphy, *center_freq);
12076 	*tx_power = (*temp_chan)->max_reg_power;
12077 }
12078 
12079 void ath12k_mac_fill_reg_tpc_info(struct ath12k *ar,
12080 				  struct ath12k_link_vif *arvif,
12081 				  struct ieee80211_chanctx_conf *ctx)
12082 {
12083 	struct ath12k_base *ab = ar->ab;
12084 	struct ath12k_reg_tpc_power_info *reg_tpc_info = &arvif->reg_tpc_info;
12085 	struct ieee80211_bss_conf *bss_conf = ath12k_mac_get_link_bss_conf(arvif);
12086 	struct ieee80211_channel *chan, *temp_chan;
12087 	u8 pwr_lvl_idx, num_pwr_levels, pwr_reduction;
12088 	bool is_psd_power = false, is_tpe_present = false;
12089 	s8 max_tx_power[ATH12K_NUM_PWR_LEVELS], psd_power, tx_power;
12090 	s8 eirp_power = 0;
12091 	struct ath12k_vif *ahvif = arvif->ahvif;
12092 	u16 start_freq, center_freq;
12093 	u8 reg_6ghz_power_mode;
12094 
12095 	chan = ctx->def.chan;
12096 	start_freq = ath12k_mac_get_6ghz_start_frequency(&ctx->def);
12097 	pwr_reduction = bss_conf->pwr_reduction;
12098 
12099 	if (arvif->reg_tpc_info.num_pwr_levels) {
12100 		is_tpe_present = true;
12101 		num_pwr_levels = arvif->reg_tpc_info.num_pwr_levels;
12102 	} else {
12103 		num_pwr_levels = ath12k_mac_get_num_pwr_levels(&ctx->def);
12104 	}
12105 
12106 	for (pwr_lvl_idx = 0; pwr_lvl_idx < num_pwr_levels; pwr_lvl_idx++) {
12107 		/* STA received TPE IE*/
12108 		if (is_tpe_present) {
12109 			/* local power is PSD power*/
12110 			if (chan->flags & IEEE80211_CHAN_PSD) {
12111 				/* Connecting AP is psd power */
12112 				if (reg_tpc_info->is_psd_power) {
12113 					is_psd_power = true;
12114 					ath12k_mac_get_psd_channel(ar, 20,
12115 								   &start_freq,
12116 								   &center_freq,
12117 								   pwr_lvl_idx,
12118 								   &temp_chan,
12119 								   &tx_power);
12120 					psd_power = temp_chan->psd;
12121 					eirp_power = tx_power;
12122 					max_tx_power[pwr_lvl_idx] =
12123 						min_t(s8,
12124 						      psd_power,
12125 						      reg_tpc_info->tpe[pwr_lvl_idx]);
12126 				/* Connecting AP is not psd power */
12127 				} else {
12128 					ath12k_mac_get_eirp_power(ar,
12129 								  &start_freq,
12130 								  &center_freq,
12131 								  pwr_lvl_idx,
12132 								  &temp_chan,
12133 								  &ctx->def,
12134 								  &tx_power);
12135 					psd_power = temp_chan->psd;
12136 					/* convert psd power to EIRP power based
12137 					 * on channel width
12138 					 */
12139 					tx_power =
12140 						min_t(s8, tx_power,
12141 						      psd_power + 13 + pwr_lvl_idx * 3);
12142 					max_tx_power[pwr_lvl_idx] =
12143 						min_t(s8,
12144 						      tx_power,
12145 						      reg_tpc_info->tpe[pwr_lvl_idx]);
12146 				}
12147 			/* local power is not PSD power */
12148 			} else {
12149 				/* Connecting AP is psd power */
12150 				if (reg_tpc_info->is_psd_power) {
12151 					is_psd_power = true;
12152 					ath12k_mac_get_psd_channel(ar, 20,
12153 								   &start_freq,
12154 								   &center_freq,
12155 								   pwr_lvl_idx,
12156 								   &temp_chan,
12157 								   &tx_power);
12158 					eirp_power = tx_power;
12159 					max_tx_power[pwr_lvl_idx] =
12160 						reg_tpc_info->tpe[pwr_lvl_idx];
12161 				/* Connecting AP is not psd power */
12162 				} else {
12163 					ath12k_mac_get_eirp_power(ar,
12164 								  &start_freq,
12165 								  &center_freq,
12166 								  pwr_lvl_idx,
12167 								  &temp_chan,
12168 								  &ctx->def,
12169 								  &tx_power);
12170 					max_tx_power[pwr_lvl_idx] =
12171 						min_t(s8,
12172 						      tx_power,
12173 						      reg_tpc_info->tpe[pwr_lvl_idx]);
12174 				}
12175 			}
12176 		/* STA not received TPE IE */
12177 		} else {
12178 			/* local power is PSD power*/
12179 			if (chan->flags & IEEE80211_CHAN_PSD) {
12180 				is_psd_power = true;
12181 				ath12k_mac_get_psd_channel(ar, 20,
12182 							   &start_freq,
12183 							   &center_freq,
12184 							   pwr_lvl_idx,
12185 							   &temp_chan,
12186 							   &tx_power);
12187 				psd_power = temp_chan->psd;
12188 				eirp_power = tx_power;
12189 				max_tx_power[pwr_lvl_idx] = psd_power;
12190 			} else {
12191 				ath12k_mac_get_eirp_power(ar,
12192 							  &start_freq,
12193 							  &center_freq,
12194 							  pwr_lvl_idx,
12195 							  &temp_chan,
12196 							  &ctx->def,
12197 							  &tx_power);
12198 				max_tx_power[pwr_lvl_idx] = tx_power;
12199 			}
12200 		}
12201 
12202 		if (is_psd_power) {
12203 			/* If AP local power constraint is present */
12204 			if (pwr_reduction)
12205 				eirp_power = eirp_power - pwr_reduction;
12206 
12207 			/* If firmware updated max tx power is non zero, then take
12208 			 * the min of firmware updated ap tx power
12209 			 * and max power derived from above mentioned parameters.
12210 			 */
12211 			ath12k_dbg(ab, ATH12K_DBG_MAC,
12212 				   "eirp power : %d firmware report power : %d\n",
12213 				   eirp_power, ar->max_allowed_tx_power);
12214 			/* Firmware reports lower max_allowed_tx_power during vdev
12215 			 * start response. In case of 6 GHz, firmware is not aware
12216 			 * of EIRP power unless driver sets EIRP power through WMI
12217 			 * TPC command. So radio which does not support idle power
12218 			 * save can set maximum calculated EIRP power directly to
12219 			 * firmware through TPC command without min comparison with
12220 			 * vdev start response's max_allowed_tx_power.
12221 			 */
12222 			if (ar->max_allowed_tx_power && ab->hw_params->idle_ps)
12223 				eirp_power = min_t(s8,
12224 						   eirp_power,
12225 						   ar->max_allowed_tx_power);
12226 		} else {
12227 			/* If AP local power constraint is present */
12228 			if (pwr_reduction)
12229 				max_tx_power[pwr_lvl_idx] =
12230 					max_tx_power[pwr_lvl_idx] - pwr_reduction;
12231 			/* If firmware updated max tx power is non zero, then take
12232 			 * the min of firmware updated ap tx power
12233 			 * and max power derived from above mentioned parameters.
12234 			 */
12235 			if (ar->max_allowed_tx_power && ab->hw_params->idle_ps)
12236 				max_tx_power[pwr_lvl_idx] =
12237 					min_t(s8,
12238 					      max_tx_power[pwr_lvl_idx],
12239 					      ar->max_allowed_tx_power);
12240 		}
12241 		reg_tpc_info->chan_power_info[pwr_lvl_idx].chan_cfreq = center_freq;
12242 		reg_tpc_info->chan_power_info[pwr_lvl_idx].tx_power =
12243 			max_tx_power[pwr_lvl_idx];
12244 	}
12245 
12246 	reg_tpc_info->num_pwr_levels = num_pwr_levels;
12247 	reg_tpc_info->is_psd_power = is_psd_power;
12248 	reg_tpc_info->eirp_power = eirp_power;
12249 	if (ahvif->vdev_type == WMI_VDEV_TYPE_STA)
12250 		reg_6ghz_power_mode = bss_conf->power_type;
12251 	else
12252 		/* For now, LPI is the only supported AP power mode */
12253 		reg_6ghz_power_mode = IEEE80211_REG_LPI_AP;
12254 
12255 	reg_tpc_info->ap_power_type =
12256 		ath12k_reg_ap_pwr_convert(reg_6ghz_power_mode);
12257 }
12258 
12259 static void ath12k_mac_parse_tx_pwr_env(struct ath12k *ar,
12260 					struct ath12k_link_vif *arvif)
12261 {
12262 	struct ieee80211_bss_conf *bss_conf = ath12k_mac_get_link_bss_conf(arvif);
12263 	struct ath12k_reg_tpc_power_info *tpc_info = &arvif->reg_tpc_info;
12264 	struct ieee80211_parsed_tpe_eirp *local_non_psd, *reg_non_psd;
12265 	struct ieee80211_parsed_tpe_psd *local_psd, *reg_psd;
12266 	struct ieee80211_parsed_tpe *tpe = &bss_conf->tpe;
12267 	enum wmi_reg_6g_client_type client_type;
12268 	struct ath12k_reg_info *reg_info;
12269 	struct ath12k_base *ab = ar->ab;
12270 	bool psd_valid, non_psd_valid;
12271 	int i;
12272 
12273 	reg_info = ab->reg_info[ar->pdev_idx];
12274 	client_type = reg_info->client_type;
12275 
12276 	local_psd = &tpe->psd_local[client_type];
12277 	reg_psd = &tpe->psd_reg_client[client_type];
12278 	local_non_psd = &tpe->max_local[client_type];
12279 	reg_non_psd = &tpe->max_reg_client[client_type];
12280 
12281 	psd_valid = local_psd->valid | reg_psd->valid;
12282 	non_psd_valid = local_non_psd->valid | reg_non_psd->valid;
12283 
12284 	if (!psd_valid && !non_psd_valid) {
12285 		ath12k_warn(ab,
12286 			    "no transmit power envelope match client power type %d\n",
12287 			    client_type);
12288 		return;
12289 	}
12290 
12291 	if (psd_valid) {
12292 		tpc_info->is_psd_power = true;
12293 
12294 		tpc_info->num_pwr_levels = max(local_psd->count,
12295 					       reg_psd->count);
12296 		tpc_info->num_pwr_levels =
12297 				min3(tpc_info->num_pwr_levels,
12298 				     IEEE80211_TPE_PSD_ENTRIES_320MHZ,
12299 				     ATH12K_NUM_PWR_LEVELS);
12300 
12301 		for (i = 0; i < tpc_info->num_pwr_levels; i++) {
12302 			tpc_info->tpe[i] = min(local_psd->power[i],
12303 					       reg_psd->power[i]) / 2;
12304 			ath12k_dbg(ab, ATH12K_DBG_MAC,
12305 				   "TPE PSD power[%d] : %d\n",
12306 				   i, tpc_info->tpe[i]);
12307 		}
12308 	} else {
12309 		tpc_info->is_psd_power = false;
12310 		tpc_info->eirp_power = 0;
12311 
12312 		tpc_info->num_pwr_levels = max(local_non_psd->count,
12313 					       reg_non_psd->count);
12314 		tpc_info->num_pwr_levels =
12315 				min3(tpc_info->num_pwr_levels,
12316 				     IEEE80211_TPE_EIRP_ENTRIES_320MHZ,
12317 				     ATH12K_NUM_PWR_LEVELS);
12318 
12319 		for (i = 0; i < tpc_info->num_pwr_levels; i++) {
12320 			tpc_info->tpe[i] = min(local_non_psd->power[i],
12321 					       reg_non_psd->power[i]) / 2;
12322 			ath12k_dbg(ab, ATH12K_DBG_MAC,
12323 				   "non PSD power[%d] : %d\n",
12324 				   i, tpc_info->tpe[i]);
12325 		}
12326 	}
12327 }
12328 
12329 int
12330 ath12k_mac_op_assign_vif_chanctx(struct ieee80211_hw *hw,
12331 				 struct ieee80211_vif *vif,
12332 				 struct ieee80211_bss_conf *link_conf,
12333 				 struct ieee80211_chanctx_conf *ctx)
12334 {
12335 	struct ath12k_hw *ah = ath12k_hw_to_ah(hw);
12336 	struct ath12k *ar;
12337 	struct ath12k_base *ab;
12338 	struct ath12k_vif *ahvif = ath12k_vif_to_ahvif(vif);
12339 	u8 link_id = link_conf->link_id;
12340 	struct ath12k_link_vif *arvif;
12341 	int ret;
12342 
12343 	lockdep_assert_wiphy(hw->wiphy);
12344 
12345 	/* For multi radio wiphy, the vdev was not created during add_interface
12346 	 * create now since we have a channel ctx now to assign to a specific ar/fw
12347 	 */
12348 	arvif = ath12k_mac_assign_link_vif(ah, vif, link_id);
12349 	if (!arvif) {
12350 		WARN_ON(1);
12351 		return -ENOMEM;
12352 	}
12353 
12354 	ar = ath12k_mac_assign_vif_to_vdev(hw, arvif, ctx);
12355 	if (!ar) {
12356 		ath12k_hw_warn(ah, "failed to assign chanctx for vif %pM link id %u link vif is already started",
12357 			       vif->addr, link_id);
12358 		return -EINVAL;
12359 	}
12360 
12361 	ab = ar->ab;
12362 
12363 	ath12k_dbg(ab, ATH12K_DBG_MAC,
12364 		   "mac chanctx assign ptr %p vdev_id %i\n",
12365 		   ctx, arvif->vdev_id);
12366 
12367 	if (ath12k_wmi_supports_6ghz_cc_ext(ar) &&
12368 	    ctx->def.chan->band == NL80211_BAND_6GHZ &&
12369 	    ahvif->vdev_type == WMI_VDEV_TYPE_STA)
12370 		ath12k_mac_parse_tx_pwr_env(ar, arvif);
12371 
12372 	arvif->punct_bitmap = ctx->def.punctured;
12373 
12374 	/* for some targets bss peer must be created before vdev_start */
12375 	if (ab->hw_params->vdev_start_delay &&
12376 	    ahvif->vdev_type != WMI_VDEV_TYPE_AP &&
12377 	    ahvif->vdev_type != WMI_VDEV_TYPE_MONITOR &&
12378 	    !ath12k_dp_link_peer_exist_by_vdev_id(ath12k_ab_to_dp(ab), arvif->vdev_id)) {
12379 		ret = 0;
12380 		goto out;
12381 	}
12382 
12383 	if (WARN_ON(arvif->is_started)) {
12384 		ret = -EBUSY;
12385 		goto out;
12386 	}
12387 
12388 	if (ahvif->vdev_type == WMI_VDEV_TYPE_MONITOR) {
12389 		ret = ath12k_mac_monitor_start(ar);
12390 		if (ret) {
12391 			ath12k_mac_monitor_vdev_delete(ar);
12392 			goto out;
12393 		}
12394 
12395 		arvif->is_started = true;
12396 		goto out;
12397 	}
12398 
12399 	ret = ath12k_mac_vdev_start(arvif, ctx);
12400 	if (ret) {
12401 		ath12k_warn(ab, "failed to start vdev %i addr %pM on freq %d: %d\n",
12402 			    arvif->vdev_id, vif->addr,
12403 			    ctx->def.chan->center_freq, ret);
12404 		goto out;
12405 	}
12406 
12407 	arvif->is_started = true;
12408 
12409 	/* TODO: Setup ps and cts/rts protection */
12410 
12411 out:
12412 	return ret;
12413 }
12414 EXPORT_SYMBOL(ath12k_mac_op_assign_vif_chanctx);
12415 
12416 void
12417 ath12k_mac_op_unassign_vif_chanctx(struct ieee80211_hw *hw,
12418 				   struct ieee80211_vif *vif,
12419 				   struct ieee80211_bss_conf *link_conf,
12420 				   struct ieee80211_chanctx_conf *ctx)
12421 {
12422 	struct ath12k *ar;
12423 	struct ath12k_base *ab;
12424 	struct ath12k_vif *ahvif = ath12k_vif_to_ahvif(vif);
12425 	struct ath12k_link_vif *arvif;
12426 	u8 link_id = link_conf->link_id;
12427 	int ret;
12428 
12429 	lockdep_assert_wiphy(hw->wiphy);
12430 
12431 	arvif = wiphy_dereference(hw->wiphy, ahvif->link[link_id]);
12432 
12433 	/* The vif is expected to be attached to an ar's VDEV.
12434 	 * We leave the vif/vdev in this function as is
12435 	 * and not delete the vdev symmetric to assign_vif_chanctx()
12436 	 * the VDEV will be deleted and unassigned either during
12437 	 * remove_interface() or when there is a change in channel
12438 	 * that moves the vif to a new ar
12439 	 */
12440 	if (!arvif || !arvif->is_created)
12441 		return;
12442 
12443 	ar = arvif->ar;
12444 	ab = ar->ab;
12445 
12446 	ath12k_dbg(ab, ATH12K_DBG_MAC,
12447 		   "mac chanctx unassign ptr %p vdev_id %i\n",
12448 		   ctx, arvif->vdev_id);
12449 
12450 	WARN_ON(!arvif->is_started);
12451 
12452 	if (ahvif->vdev_type == WMI_VDEV_TYPE_MONITOR) {
12453 		ret = ath12k_mac_monitor_stop(ar);
12454 		if (ret)
12455 			return;
12456 
12457 		arvif->is_started = false;
12458 	}
12459 
12460 	if (ahvif->vdev_type != WMI_VDEV_TYPE_STA &&
12461 	    ahvif->vdev_type != WMI_VDEV_TYPE_MONITOR) {
12462 		ath12k_bss_disassoc(ar, arvif);
12463 		ret = ath12k_mac_vdev_stop(arvif);
12464 		if (ret)
12465 			ath12k_warn(ab, "failed to stop vdev %i: %d\n",
12466 				    arvif->vdev_id, ret);
12467 	}
12468 	arvif->is_started = false;
12469 
12470 	if (test_bit(WMI_TLV_SERVICE_11D_OFFLOAD, ab->wmi_ab.svc_map) &&
12471 	    ahvif->vdev_type == WMI_VDEV_TYPE_STA &&
12472 	    ahvif->vdev_subtype == WMI_VDEV_SUBTYPE_NONE &&
12473 	    ar->state_11d != ATH12K_11D_PREPARING) {
12474 		reinit_completion(&ar->completed_11d_scan);
12475 		ar->state_11d = ATH12K_11D_PREPARING;
12476 	}
12477 
12478 	if (ar->scan.arvif == arvif && ar->scan.state == ATH12K_SCAN_RUNNING) {
12479 		ath12k_scan_abort(ar);
12480 		ar->scan.arvif = NULL;
12481 	}
12482 }
12483 EXPORT_SYMBOL(ath12k_mac_op_unassign_vif_chanctx);
12484 
12485 int
12486 ath12k_mac_op_switch_vif_chanctx(struct ieee80211_hw *hw,
12487 				 struct ieee80211_vif_chanctx_switch *vifs,
12488 				 int n_vifs,
12489 				 enum ieee80211_chanctx_switch_mode mode)
12490 {
12491 	struct ath12k *curr_ar, *new_ar, *group_ar;
12492 	struct ieee80211_vif_chanctx_switch *v;
12493 	int i, j, count = 0;
12494 
12495 	lockdep_assert_wiphy(hw->wiphy);
12496 
12497 	if (n_vifs == 0)
12498 		return 0;
12499 
12500 	struct ath12k **ar_map __free(kfree) = kzalloc_objs(*ar_map, n_vifs);
12501 
12502 	if (!ar_map)
12503 		return -ENOMEM;
12504 
12505 	for (i = 0; i < n_vifs; i++) {
12506 		v = &vifs[i];
12507 
12508 		if (v->old_ctx->def.chan->band != v->new_ctx->def.chan->band) {
12509 			ath12k_generic_dbg(ATH12K_DBG_MAC,
12510 					   "mac chanctx switch band change not supported\n");
12511 			return -EOPNOTSUPP;
12512 		}
12513 
12514 		curr_ar = ath12k_get_ar_by_ctx(hw, v->old_ctx);
12515 		new_ar = ath12k_get_ar_by_ctx(hw, v->new_ctx);
12516 
12517 		if (!curr_ar || !new_ar) {
12518 			ath12k_generic_dbg(ATH12K_DBG_MAC,
12519 					   "unable to determine device for the passed channel ctx\n");
12520 			ath12k_generic_dbg(ATH12K_DBG_MAC,
12521 					   "Old freq %d MHz (device %s) to new freq %d MHz (device %s)\n",
12522 					   v->old_ctx->def.chan->center_freq,
12523 					   curr_ar ? "valid" : "invalid",
12524 					   v->new_ctx->def.chan->center_freq,
12525 					   new_ar ? "valid" : "invalid");
12526 			return -EINVAL;
12527 		}
12528 
12529 		/* Switching a vif between two radios is not allowed */
12530 		if (curr_ar != new_ar) {
12531 			ath12k_dbg(curr_ar->ab, ATH12K_DBG_MAC,
12532 				   "mac chanctx switch to another radio not supported\n");
12533 			return -EOPNOTSUPP;
12534 		}
12535 
12536 		ar_map[i] = curr_ar;
12537 	}
12538 
12539 	/* Group vifs by radio (ar) and process each group independently. */
12540 	bool *processed __free(kfree) = kzalloc_objs(*processed, n_vifs);
12541 
12542 	if (!processed)
12543 		return -ENOMEM;
12544 
12545 	struct ieee80211_vif_chanctx_switch *group_vifs __free(kfree) =
12546 						kzalloc_objs(*group_vifs, n_vifs);
12547 
12548 	if (!group_vifs)
12549 		return -ENOMEM;
12550 
12551 	for (i = 0; i < n_vifs; i++) {
12552 		if (processed[i])
12553 			continue;
12554 
12555 		group_ar = ar_map[i];
12556 
12557 		count = 0;
12558 		for (j = 0; j < n_vifs; j++) {
12559 			if (!processed[j] && ar_map[j] == group_ar) {
12560 				group_vifs[count++] = vifs[j];
12561 				processed[j] = true;
12562 			}
12563 		}
12564 
12565 		ath12k_dbg(group_ar->ab, ATH12K_DBG_MAC,
12566 			   "mac chanctx switch n_vifs %d mode %d\n",
12567 			   count, mode);
12568 		ath12k_mac_update_vif_chan(group_ar, group_vifs, count);
12569 	}
12570 	return 0;
12571 }
12572 EXPORT_SYMBOL(ath12k_mac_op_switch_vif_chanctx);
12573 
12574 static int
12575 ath12k_set_vdev_param_to_all_vifs(struct ath12k *ar, int param, u32 value)
12576 {
12577 	struct ath12k_link_vif *arvif;
12578 	int ret = 0;
12579 
12580 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
12581 
12582 	list_for_each_entry(arvif, &ar->arvifs, list) {
12583 		ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "setting mac vdev %d param %d value %d\n",
12584 			   param, arvif->vdev_id, value);
12585 
12586 		ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
12587 						    param, value);
12588 		if (ret) {
12589 			ath12k_warn(ar->ab, "failed to set param %d for vdev %d: %d\n",
12590 				    param, arvif->vdev_id, ret);
12591 			break;
12592 		}
12593 	}
12594 
12595 	return ret;
12596 }
12597 
12598 /* mac80211 stores device specific RTS/Fragmentation threshold value,
12599  * this is set interface specific to firmware from ath12k driver
12600  */
12601 int ath12k_mac_op_set_rts_threshold(struct ieee80211_hw *hw,
12602 				    int radio_idx, u32 value)
12603 {
12604 	struct ath12k_hw *ah = ath12k_hw_to_ah(hw);
12605 	struct wiphy *wiphy = hw->wiphy;
12606 	struct ath12k *ar;
12607 	int param_id = WMI_VDEV_PARAM_RTS_THRESHOLD;
12608 	int ret = 0, ret_err, i;
12609 
12610 	lockdep_assert_wiphy(hw->wiphy);
12611 
12612 	if (radio_idx >= wiphy->n_radio || radio_idx < -1)
12613 		return -EINVAL;
12614 
12615 	if (radio_idx != -1) {
12616 		/* Update RTS threshold in specified radio */
12617 		ar = ath12k_ah_to_ar(ah, radio_idx);
12618 		ret = ath12k_set_vdev_param_to_all_vifs(ar, param_id, value);
12619 		if (ret) {
12620 			ath12k_warn(ar->ab,
12621 				    "failed to set RTS config for all vdevs of pdev %d",
12622 				    ar->pdev->pdev_id);
12623 			return ret;
12624 		}
12625 
12626 		ar->rts_threshold = value;
12627 		return 0;
12628 	}
12629 
12630 	/* Radio_index passed is -1, so set RTS threshold for all radios. */
12631 	for_each_ar(ah, ar, i) {
12632 		ret = ath12k_set_vdev_param_to_all_vifs(ar, param_id, value);
12633 		if (ret) {
12634 			ath12k_warn(ar->ab, "failed to set RTS config for all vdevs of pdev %d",
12635 				    ar->pdev->pdev_id);
12636 			break;
12637 		}
12638 	}
12639 	if (!ret) {
12640 		/* Setting new RTS threshold for vdevs of all radios passed, so update
12641 		 * the RTS threshold value for all radios
12642 		 */
12643 		for_each_ar(ah, ar, i)
12644 			ar->rts_threshold = value;
12645 		return 0;
12646 	}
12647 
12648 	/* RTS threshold config failed, revert to the previous RTS threshold */
12649 	for (i = i - 1; i >= 0; i--) {
12650 		ar = ath12k_ah_to_ar(ah, i);
12651 		ret_err = ath12k_set_vdev_param_to_all_vifs(ar, param_id,
12652 							    ar->rts_threshold);
12653 		if (ret_err)
12654 			ath12k_warn(ar->ab,
12655 				    "failed to restore RTS threshold for all vdevs of pdev %d",
12656 				    ar->pdev->pdev_id);
12657 	}
12658 
12659 	return ret;
12660 }
12661 EXPORT_SYMBOL(ath12k_mac_op_set_rts_threshold);
12662 
12663 int ath12k_mac_op_set_frag_threshold(struct ieee80211_hw *hw,
12664 				     int radio_idx, u32 value)
12665 {
12666 	/* Even though there's a WMI vdev param for fragmentation threshold no
12667 	 * known firmware actually implements it. Moreover it is not possible to
12668 	 * rely frame fragmentation to mac80211 because firmware clears the
12669 	 * "more fragments" bit in frame control making it impossible for remote
12670 	 * devices to reassemble frames.
12671 	 *
12672 	 * Hence implement a dummy callback just to say fragmentation isn't
12673 	 * supported. This effectively prevents mac80211 from doing frame
12674 	 * fragmentation in software.
12675 	 */
12676 
12677 	lockdep_assert_wiphy(hw->wiphy);
12678 
12679 	return -EOPNOTSUPP;
12680 }
12681 EXPORT_SYMBOL(ath12k_mac_op_set_frag_threshold);
12682 
12683 static int ath12k_mac_flush(struct ath12k *ar)
12684 {
12685 	long time_left;
12686 	int ret = 0;
12687 
12688 	time_left = wait_event_timeout(ar->dp.tx_empty_waitq,
12689 				       (atomic_read(&ar->dp.num_tx_pending) == 0),
12690 				       ATH12K_FLUSH_TIMEOUT);
12691 	if (time_left == 0) {
12692 		ath12k_warn(ar->ab,
12693 			    "failed to flush transmit queue, data pkts pending %d\n",
12694 			    atomic_read(&ar->dp.num_tx_pending));
12695 		ret = -ETIMEDOUT;
12696 	}
12697 
12698 	time_left = wait_event_timeout(ar->txmgmt_empty_waitq,
12699 				       (atomic_read(&ar->num_pending_mgmt_tx) == 0),
12700 				       ATH12K_FLUSH_TIMEOUT);
12701 	if (time_left == 0) {
12702 		ath12k_warn(ar->ab,
12703 			    "failed to flush mgmt transmit queue, mgmt pkts pending %d\n",
12704 			    atomic_read(&ar->num_pending_mgmt_tx));
12705 		ret = -ETIMEDOUT;
12706 	}
12707 
12708 	return ret;
12709 }
12710 
12711 int ath12k_mac_wait_tx_complete(struct ath12k *ar)
12712 {
12713 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
12714 
12715 	ath12k_mac_drain_tx(ar);
12716 	return ath12k_mac_flush(ar);
12717 }
12718 
12719 void ath12k_mac_op_flush(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
12720 			 u32 queues, bool drop)
12721 {
12722 	struct ath12k_hw *ah = ath12k_hw_to_ah(hw);
12723 	struct ath12k_link_vif *arvif;
12724 	struct ath12k_vif *ahvif;
12725 	unsigned long links;
12726 	struct ath12k *ar;
12727 	u8 link_id;
12728 	int i;
12729 
12730 	lockdep_assert_wiphy(hw->wiphy);
12731 
12732 	if (drop)
12733 		return;
12734 
12735 	for_each_ar(ah, ar, i)
12736 		wiphy_work_flush(hw->wiphy, &ar->wmi_mgmt_tx_work);
12737 
12738 	/* vif can be NULL when flush() is considered for hw */
12739 	if (!vif) {
12740 		for_each_ar(ah, ar, i)
12741 			ath12k_mac_flush(ar);
12742 		return;
12743 	}
12744 
12745 	ahvif = ath12k_vif_to_ahvif(vif);
12746 	links = ahvif->links_map;
12747 	for_each_set_bit(link_id, &links, IEEE80211_MLD_MAX_NUM_LINKS) {
12748 		arvif = wiphy_dereference(hw->wiphy, ahvif->link[link_id]);
12749 		if (!(arvif && arvif->ar))
12750 			continue;
12751 
12752 		ath12k_mac_flush(arvif->ar);
12753 	}
12754 }
12755 EXPORT_SYMBOL(ath12k_mac_op_flush);
12756 
12757 static int
12758 ath12k_mac_bitrate_mask_num_ht_rates(struct ath12k *ar,
12759 				     enum nl80211_band band,
12760 				     const struct cfg80211_bitrate_mask *mask)
12761 {
12762 	int num_rates = 0;
12763 	int i;
12764 
12765 	for (i = 0; i < ARRAY_SIZE(mask->control[band].ht_mcs); i++)
12766 		num_rates += hweight16(mask->control[band].ht_mcs[i]);
12767 
12768 	return num_rates;
12769 }
12770 
12771 static bool
12772 ath12k_mac_has_single_legacy_rate(struct ath12k *ar,
12773 				  enum nl80211_band band,
12774 				  const struct cfg80211_bitrate_mask *mask)
12775 {
12776 	int num_rates = 0;
12777 
12778 	num_rates = hweight32(mask->control[band].legacy);
12779 
12780 	if (ath12k_mac_bitrate_mask_num_ht_rates(ar, band, mask))
12781 		return false;
12782 
12783 	if (ath12k_mac_bitrate_mask_num_vht_rates(ar, band, mask))
12784 		return false;
12785 
12786 	if (ath12k_mac_bitrate_mask_num_he_rates(ar, band, mask))
12787 		return false;
12788 
12789 	if (ath12k_mac_bitrate_mask_num_eht_rates(ar, band, mask))
12790 		return false;
12791 
12792 	return num_rates == 1;
12793 }
12794 
12795 static __le16
12796 ath12k_mac_get_tx_mcs_map(const struct ieee80211_sta_he_cap *he_cap)
12797 {
12798 	if (he_cap->he_cap_elem.phy_cap_info[0] &
12799 	    IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_160MHZ_IN_5G)
12800 		return he_cap->he_mcs_nss_supp.tx_mcs_160;
12801 
12802 	return he_cap->he_mcs_nss_supp.tx_mcs_80;
12803 }
12804 
12805 static bool
12806 ath12k_mac_bitrate_mask_get_single_nss(struct ath12k *ar,
12807 				       struct ieee80211_vif *vif,
12808 				       enum nl80211_band band,
12809 				       const struct cfg80211_bitrate_mask *mask,
12810 				       int *nss)
12811 {
12812 	struct ieee80211_supported_band *sband = &ar->mac.sbands[band];
12813 	u16 vht_mcs_map = le16_to_cpu(sband->vht_cap.vht_mcs.tx_mcs_map);
12814 	const struct ieee80211_sband_iftype_data *data;
12815 	const struct ieee80211_sta_he_cap *he_cap;
12816 	u16 he_mcs_map = 0;
12817 	u16 eht_mcs_map = 0;
12818 	u8 ht_nss_mask = 0;
12819 	u8 vht_nss_mask = 0;
12820 	u8 he_nss_mask = 0;
12821 	u8 eht_nss_mask = 0;
12822 	u8 mcs_nss_len;
12823 	int i;
12824 
12825 	/* No need to consider legacy here. Basic rates are always present
12826 	 * in bitrate mask
12827 	 */
12828 
12829 	for (i = 0; i < ARRAY_SIZE(mask->control[band].ht_mcs); i++) {
12830 		if (mask->control[band].ht_mcs[i] == 0)
12831 			continue;
12832 		else if (mask->control[band].ht_mcs[i] ==
12833 			 sband->ht_cap.mcs.rx_mask[i])
12834 			ht_nss_mask |= BIT(i);
12835 		else
12836 			return false;
12837 	}
12838 
12839 	for (i = 0; i < ARRAY_SIZE(mask->control[band].vht_mcs); i++) {
12840 		if (mask->control[band].vht_mcs[i] == 0)
12841 			continue;
12842 		else if (mask->control[band].vht_mcs[i] ==
12843 			 ath12k_mac_get_max_vht_mcs_map(vht_mcs_map, i))
12844 			vht_nss_mask |= BIT(i);
12845 		else
12846 			return false;
12847 	}
12848 
12849 	he_cap = ieee80211_get_he_iftype_cap_vif(sband, vif);
12850 	if (!he_cap)
12851 		return false;
12852 
12853 	he_mcs_map = le16_to_cpu(ath12k_mac_get_tx_mcs_map(he_cap));
12854 
12855 	for (i = 0; i < ARRAY_SIZE(mask->control[band].he_mcs); i++) {
12856 		if (mask->control[band].he_mcs[i] == 0)
12857 			continue;
12858 
12859 		if (mask->control[band].he_mcs[i] ==
12860 		    ath12k_mac_get_max_he_mcs_map(he_mcs_map, i))
12861 			he_nss_mask |= BIT(i);
12862 		else
12863 			return false;
12864 	}
12865 
12866 	data = ieee80211_get_sband_iftype_data(sband, vif->type);
12867 
12868 	mcs_nss_len = ieee80211_eht_mcs_nss_size(&data->he_cap.he_cap_elem,
12869 						 &data->eht_cap.eht_cap_elem,
12870 						 false);
12871 	if (mcs_nss_len == 4) {
12872 		/* 20 MHz only STA case */
12873 		const struct ieee80211_eht_mcs_nss_supp_20mhz_only *eht_mcs_nss =
12874 			&data->eht_cap.eht_mcs_nss_supp.only_20mhz;
12875 		if (eht_mcs_nss->rx_tx_mcs13_max_nss)
12876 			eht_mcs_map = 0x1fff;
12877 		else if (eht_mcs_nss->rx_tx_mcs11_max_nss)
12878 			eht_mcs_map = 0x07ff;
12879 		else if (eht_mcs_nss->rx_tx_mcs9_max_nss)
12880 			eht_mcs_map = 0x01ff;
12881 		else
12882 			eht_mcs_map = 0x007f;
12883 	} else {
12884 		const struct ieee80211_eht_mcs_nss_supp_bw *eht_mcs_nss;
12885 
12886 		switch (mcs_nss_len) {
12887 		case 9:
12888 			eht_mcs_nss = &data->eht_cap.eht_mcs_nss_supp.bw._320;
12889 			break;
12890 		case 6:
12891 			eht_mcs_nss = &data->eht_cap.eht_mcs_nss_supp.bw._160;
12892 			break;
12893 		case 3:
12894 			eht_mcs_nss = &data->eht_cap.eht_mcs_nss_supp.bw._80;
12895 			break;
12896 		default:
12897 			return false;
12898 		}
12899 
12900 		if (eht_mcs_nss->rx_tx_mcs13_max_nss)
12901 			eht_mcs_map = 0x1fff;
12902 		else if (eht_mcs_nss->rx_tx_mcs11_max_nss)
12903 			eht_mcs_map = 0x7ff;
12904 		else
12905 			eht_mcs_map = 0x1ff;
12906 	}
12907 
12908 	for (i = 0; i < ARRAY_SIZE(mask->control[band].eht_mcs); i++) {
12909 		if (mask->control[band].eht_mcs[i] == 0)
12910 			continue;
12911 
12912 		if (mask->control[band].eht_mcs[i] < eht_mcs_map)
12913 			eht_nss_mask |= BIT(i);
12914 		else
12915 			return false;
12916 	}
12917 
12918 	if (ht_nss_mask != vht_nss_mask || ht_nss_mask != he_nss_mask ||
12919 	    ht_nss_mask != eht_nss_mask)
12920 		return false;
12921 
12922 	if (ht_nss_mask == 0)
12923 		return false;
12924 
12925 	if (BIT(fls(ht_nss_mask)) - 1 != ht_nss_mask)
12926 		return false;
12927 
12928 	*nss = fls(ht_nss_mask);
12929 
12930 	return true;
12931 }
12932 
12933 static int
12934 ath12k_mac_get_single_legacy_rate(struct ath12k *ar,
12935 				  enum nl80211_band band,
12936 				  const struct cfg80211_bitrate_mask *mask,
12937 				  u32 *rate, u8 *nss)
12938 {
12939 	int rate_idx;
12940 	u16 bitrate;
12941 	u8 preamble;
12942 	u8 hw_rate;
12943 
12944 	if (hweight32(mask->control[band].legacy) != 1)
12945 		return -EINVAL;
12946 
12947 	rate_idx = ffs(mask->control[band].legacy) - 1;
12948 
12949 	if (band == NL80211_BAND_5GHZ || band == NL80211_BAND_6GHZ)
12950 		rate_idx += ATH12K_MAC_FIRST_OFDM_RATE_IDX;
12951 
12952 	hw_rate = ath12k_legacy_rates[rate_idx].hw_value;
12953 	bitrate = ath12k_legacy_rates[rate_idx].bitrate;
12954 
12955 	if (ath12k_mac_bitrate_is_cck(bitrate))
12956 		preamble = WMI_RATE_PREAMBLE_CCK;
12957 	else
12958 		preamble = WMI_RATE_PREAMBLE_OFDM;
12959 
12960 	*nss = 1;
12961 	*rate = ATH12K_HW_RATE_CODE(hw_rate, 0, preamble);
12962 
12963 	return 0;
12964 }
12965 
12966 static int
12967 ath12k_mac_set_fixed_rate_gi_ltf(struct ath12k_link_vif *arvif, u8 gi, u8 ltf,
12968 				 u32 param)
12969 {
12970 	struct ath12k *ar = arvif->ar;
12971 	int ret;
12972 
12973 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
12974 
12975 	/* 0.8 = 0, 1.6 = 2 and 3.2 = 3. */
12976 	if (gi && gi != 0xFF)
12977 		gi += 1;
12978 
12979 	ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
12980 					    WMI_VDEV_PARAM_SGI, gi);
12981 	if (ret) {
12982 		ath12k_warn(ar->ab, "failed to set GI:%d, error:%d\n",
12983 			    gi, ret);
12984 		return ret;
12985 	}
12986 
12987 	if (param == WMI_VDEV_PARAM_HE_LTF) {
12988 		/* HE values start from 1 */
12989 		if (ltf != 0xFF)
12990 			ltf += 1;
12991 	} else {
12992 		/* EHT values start from 5 */
12993 		if (ltf != 0xFF)
12994 			ltf += 4;
12995 	}
12996 
12997 	ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
12998 					    param, ltf);
12999 	if (ret) {
13000 		ath12k_warn(ar->ab, "failed to set LTF:%d, error:%d\n",
13001 			    ltf, ret);
13002 		return ret;
13003 	}
13004 	return 0;
13005 }
13006 
13007 static int
13008 ath12k_mac_set_auto_rate_gi_ltf(struct ath12k_link_vif *arvif, u16 gi, u8 ltf)
13009 {
13010 	struct ath12k *ar = arvif->ar;
13011 	int ret;
13012 	u32 ar_gi_ltf;
13013 
13014 	if (gi != 0xFF) {
13015 		switch (gi) {
13016 		case ATH12K_RATE_INFO_GI_0_8:
13017 			gi = WMI_AUTORATE_800NS_GI;
13018 			break;
13019 		case ATH12K_RATE_INFO_GI_1_6:
13020 			gi = WMI_AUTORATE_1600NS_GI;
13021 			break;
13022 		case ATH12K_RATE_INFO_GI_3_2:
13023 			gi = WMI_AUTORATE_3200NS_GI;
13024 			break;
13025 		default:
13026 			ath12k_warn(ar->ab, "Invalid GI\n");
13027 			return -EINVAL;
13028 		}
13029 	}
13030 
13031 	if (ltf != 0xFF) {
13032 		switch (ltf) {
13033 		case ATH12K_RATE_INFO_1XLTF:
13034 			ltf = WMI_AUTORATE_LTF_1X;
13035 			break;
13036 		case ATH12K_RATE_INFO_2XLTF:
13037 			ltf = WMI_AUTORATE_LTF_2X;
13038 			break;
13039 		case ATH12K_RATE_INFO_4XLTF:
13040 			ltf = WMI_AUTORATE_LTF_4X;
13041 			break;
13042 		default:
13043 			ath12k_warn(ar->ab, "Invalid LTF\n");
13044 			return -EINVAL;
13045 		}
13046 	}
13047 
13048 	ar_gi_ltf = gi | ltf;
13049 
13050 	ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
13051 					    WMI_VDEV_PARAM_AUTORATE_MISC_CFG,
13052 					    ar_gi_ltf);
13053 	if (ret) {
13054 		ath12k_warn(ar->ab,
13055 			    "failed to set autorate GI:%u, LTF:%u params, error:%d\n",
13056 			    gi, ltf, ret);
13057 		return ret;
13058 	}
13059 
13060 	return 0;
13061 }
13062 
13063 static u32 ath12k_mac_nlgi_to_wmigi(enum nl80211_txrate_gi gi)
13064 {
13065 	switch (gi) {
13066 	case NL80211_TXRATE_DEFAULT_GI:
13067 		return WMI_GI_400_NS;
13068 	case NL80211_TXRATE_FORCE_LGI:
13069 		return WMI_GI_800_NS;
13070 	default:
13071 		return WMI_GI_400_NS;
13072 	}
13073 }
13074 
13075 static int ath12k_mac_set_rate_params(struct ath12k_link_vif *arvif,
13076 				      u32 rate, u8 nss, u8 sgi, u8 ldpc,
13077 				      u8 he_gi, u8 he_ltf, bool he_fixed_rate,
13078 				      u8 eht_gi, u8 eht_ltf,
13079 				      bool eht_fixed_rate)
13080 {
13081 	struct ieee80211_bss_conf *link_conf;
13082 	struct ath12k *ar = arvif->ar;
13083 	bool he_support, eht_support, gi_ltf_set = false;
13084 	u32 vdev_param;
13085 	u32 param_value;
13086 	int ret;
13087 
13088 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
13089 
13090 	link_conf = ath12k_mac_get_link_bss_conf(arvif);
13091 	if (!link_conf)
13092 		return -EINVAL;
13093 
13094 	he_support = link_conf->he_support;
13095 	eht_support = link_conf->eht_support;
13096 
13097 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
13098 		   "mac set rate params vdev %i rate 0x%02x nss 0x%02x sgi 0x%02x ldpc 0x%02x\n",
13099 		   arvif->vdev_id, rate, nss, sgi, ldpc);
13100 
13101 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
13102 		   "he_gi 0x%02x he_ltf 0x%02x he_fixed_rate %d\n", he_gi,
13103 		   he_ltf, he_fixed_rate);
13104 
13105 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
13106 		   "eht_gi 0x%02x eht_ltf 0x%02x eht_fixed_rate %d\n",
13107 		   eht_gi, eht_ltf, eht_fixed_rate);
13108 
13109 	if (!he_support && !eht_support) {
13110 		vdev_param = WMI_VDEV_PARAM_FIXED_RATE;
13111 		ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
13112 						    vdev_param, rate);
13113 		if (ret) {
13114 			ath12k_warn(ar->ab, "failed to set fixed rate param 0x%02x: %d\n",
13115 				    rate, ret);
13116 			return ret;
13117 		}
13118 	}
13119 
13120 	vdev_param = WMI_VDEV_PARAM_NSS;
13121 
13122 	ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
13123 					    vdev_param, nss);
13124 	if (ret) {
13125 		ath12k_warn(ar->ab, "failed to set nss param %d: %d\n",
13126 			    nss, ret);
13127 		return ret;
13128 	}
13129 
13130 	ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
13131 					    WMI_VDEV_PARAM_LDPC, ldpc);
13132 	if (ret) {
13133 		ath12k_warn(ar->ab, "failed to set ldpc param %d: %d\n",
13134 			    ldpc, ret);
13135 		return ret;
13136 	}
13137 
13138 	if (eht_support) {
13139 		if (eht_fixed_rate)
13140 			ret = ath12k_mac_set_fixed_rate_gi_ltf(arvif, eht_gi, eht_ltf,
13141 							       WMI_VDEV_PARAM_EHT_LTF);
13142 		else
13143 			ret = ath12k_mac_set_auto_rate_gi_ltf(arvif, eht_gi, eht_ltf);
13144 
13145 		if (ret) {
13146 			ath12k_warn(ar->ab,
13147 				    "failed to set EHT LTF/GI params %d/%d: %d\n",
13148 				    eht_gi, eht_ltf, ret);
13149 			return ret;
13150 		}
13151 		gi_ltf_set = true;
13152 	}
13153 
13154 	if (he_support) {
13155 		if (he_fixed_rate)
13156 			ret = ath12k_mac_set_fixed_rate_gi_ltf(arvif, he_gi, he_ltf,
13157 							       WMI_VDEV_PARAM_HE_LTF);
13158 		else
13159 			ret = ath12k_mac_set_auto_rate_gi_ltf(arvif, he_gi, he_ltf);
13160 		if (ret)
13161 			return ret;
13162 		gi_ltf_set = true;
13163 	}
13164 
13165 	if (!gi_ltf_set) {
13166 		vdev_param = WMI_VDEV_PARAM_SGI;
13167 		param_value = ath12k_mac_nlgi_to_wmigi(sgi);
13168 		ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
13169 						    vdev_param, param_value);
13170 		if (ret) {
13171 			ath12k_warn(ar->ab, "failed to set sgi param %d: %d\n",
13172 				    sgi, ret);
13173 			return ret;
13174 		}
13175 	}
13176 
13177 	return 0;
13178 }
13179 
13180 static bool
13181 ath12k_mac_vht_mcs_range_present(struct ath12k *ar,
13182 				 enum nl80211_band band,
13183 				 const struct cfg80211_bitrate_mask *mask)
13184 {
13185 	int i;
13186 	u16 vht_mcs;
13187 
13188 	for (i = 0; i < NL80211_VHT_NSS_MAX; i++) {
13189 		vht_mcs = mask->control[band].vht_mcs[i];
13190 
13191 		switch (vht_mcs) {
13192 		case 0:
13193 		case BIT(8) - 1:
13194 		case BIT(9) - 1:
13195 		case BIT(10) - 1:
13196 			break;
13197 		default:
13198 			return false;
13199 		}
13200 	}
13201 
13202 	return true;
13203 }
13204 
13205 static bool
13206 ath12k_mac_he_mcs_range_present(struct ath12k *ar,
13207 				enum nl80211_band band,
13208 				const struct cfg80211_bitrate_mask *mask)
13209 {
13210 	int i;
13211 	u16 he_mcs;
13212 
13213 	for (i = 0; i < NL80211_HE_NSS_MAX; i++) {
13214 		he_mcs = mask->control[band].he_mcs[i];
13215 
13216 		switch (he_mcs) {
13217 		case 0:
13218 		case BIT(8) - 1:
13219 		case BIT(10) - 1:
13220 		case BIT(12) - 1:
13221 			break;
13222 		default:
13223 			return false;
13224 		}
13225 	}
13226 
13227 	return true;
13228 }
13229 
13230 static bool
13231 ath12k_mac_eht_mcs_range_present(struct ath12k *ar,
13232 				 enum nl80211_band band,
13233 				 const struct cfg80211_bitrate_mask *mask)
13234 {
13235 	u16 eht_mcs;
13236 	int i;
13237 
13238 	for (i = 0; i < NL80211_EHT_NSS_MAX; i++) {
13239 		eht_mcs = mask->control[band].eht_mcs[i];
13240 
13241 		switch (eht_mcs) {
13242 		case 0:
13243 		case BIT(8) - 1:
13244 		case BIT(10) - 1:
13245 		case BIT(12) - 1:
13246 		case BIT(14) - 1:
13247 			break;
13248 		case BIT(15) - 1:
13249 		case BIT(16) - 1:
13250 		case BIT(16) - BIT(14) - 1:
13251 			if (i != 0)
13252 				return false;
13253 			break;
13254 		default:
13255 			return false;
13256 		}
13257 	}
13258 
13259 	return true;
13260 }
13261 
13262 static void ath12k_mac_set_bitrate_mask_iter(void *data,
13263 					     struct ieee80211_sta *sta)
13264 {
13265 	struct ath12k_link_vif *arvif = data;
13266 	struct ath12k_sta *ahsta = ath12k_sta_to_ahsta(sta);
13267 	struct ath12k_link_sta *arsta;
13268 	struct ath12k *ar = arvif->ar;
13269 
13270 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
13271 
13272 	arsta = wiphy_dereference(ath12k_ar_to_hw(ar)->wiphy,
13273 				  ahsta->link[arvif->link_id]);
13274 	if (!arsta || arsta->arvif != arvif)
13275 		return;
13276 
13277 	spin_lock_bh(&ar->data_lock);
13278 	arsta->changed |= IEEE80211_RC_SUPP_RATES_CHANGED;
13279 	spin_unlock_bh(&ar->data_lock);
13280 
13281 	wiphy_work_queue(ath12k_ar_to_hw(ar)->wiphy, &arsta->update_wk);
13282 }
13283 
13284 static void ath12k_mac_disable_peer_fixed_rate(void *data,
13285 					       struct ieee80211_sta *sta)
13286 {
13287 	struct ath12k_link_vif *arvif = data;
13288 	struct ath12k_sta *ahsta = ath12k_sta_to_ahsta(sta);
13289 	struct ath12k_link_sta *arsta;
13290 	struct ath12k *ar = arvif->ar;
13291 	int ret;
13292 
13293 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
13294 
13295 	arsta = wiphy_dereference(ath12k_ar_to_hw(ar)->wiphy,
13296 				  ahsta->link[arvif->link_id]);
13297 
13298 	if (!arsta || arsta->arvif != arvif)
13299 		return;
13300 
13301 	ret = ath12k_wmi_set_peer_param(ar, arsta->addr,
13302 					arvif->vdev_id,
13303 					WMI_PEER_PARAM_FIXED_RATE,
13304 					WMI_FIXED_RATE_NONE);
13305 	if (ret)
13306 		ath12k_warn(ar->ab,
13307 			    "failed to disable peer fixed rate for STA %pM ret %d\n",
13308 			    arsta->addr, ret);
13309 }
13310 
13311 static bool
13312 ath12k_mac_validate_fixed_rate_settings(struct ath12k *ar, enum nl80211_band band,
13313 					const struct cfg80211_bitrate_mask *mask,
13314 					unsigned int link_id)
13315 {
13316 	bool eht_fixed_rate = false, he_fixed_rate = false, vht_fixed_rate = false;
13317 	const u16 *vht_mcs_mask, *he_mcs_mask, *eht_mcs_mask;
13318 	struct ieee80211_link_sta *link_sta;
13319 	struct ath12k_dp_link_peer *peer, *tmp;
13320 	u8 vht_nss, he_nss, eht_nss;
13321 	int ret = true;
13322 	struct ath12k_base *ab = ar->ab;
13323 	struct ath12k_dp *dp = ath12k_ab_to_dp(ab);
13324 
13325 	vht_mcs_mask = mask->control[band].vht_mcs;
13326 	he_mcs_mask = mask->control[band].he_mcs;
13327 	eht_mcs_mask = mask->control[band].eht_mcs;
13328 
13329 	if (ath12k_mac_bitrate_mask_num_vht_rates(ar, band, mask) == 1)
13330 		vht_fixed_rate = true;
13331 
13332 	if (ath12k_mac_bitrate_mask_num_he_rates(ar, band, mask) == 1)
13333 		he_fixed_rate = true;
13334 
13335 	if (ath12k_mac_bitrate_mask_num_eht_rates(ar, band, mask) == 1)
13336 		eht_fixed_rate = true;
13337 
13338 	if (!vht_fixed_rate && !he_fixed_rate && !eht_fixed_rate)
13339 		return true;
13340 
13341 	vht_nss = ath12k_mac_max_vht_nss(vht_mcs_mask);
13342 	he_nss =  ath12k_mac_max_he_nss(he_mcs_mask);
13343 	eht_nss = ath12k_mac_max_eht_nss(eht_mcs_mask);
13344 
13345 	rcu_read_lock();
13346 	spin_lock_bh(&dp->dp_lock);
13347 	list_for_each_entry_safe(peer, tmp, &dp->peers, list) {
13348 		if (peer->sta) {
13349 			link_sta = rcu_dereference(peer->sta->link[link_id]);
13350 			if (!link_sta) {
13351 				ret = false;
13352 				goto exit;
13353 			}
13354 
13355 			if (vht_fixed_rate && (!link_sta->vht_cap.vht_supported ||
13356 					       link_sta->rx_nss < vht_nss)) {
13357 				ret = false;
13358 				goto exit;
13359 			}
13360 			if (he_fixed_rate && (!link_sta->he_cap.has_he ||
13361 					      link_sta->rx_nss < he_nss)) {
13362 				ret = false;
13363 				goto exit;
13364 			}
13365 			if (eht_fixed_rate && (!link_sta->eht_cap.has_eht ||
13366 					       link_sta->rx_nss < eht_nss)) {
13367 				ret = false;
13368 				goto exit;
13369 			}
13370 		}
13371 	}
13372 exit:
13373 	spin_unlock_bh(&dp->dp_lock);
13374 	rcu_read_unlock();
13375 	return ret;
13376 }
13377 
13378 int
13379 ath12k_mac_op_set_bitrate_mask(struct ieee80211_hw *hw,
13380 			       struct ieee80211_vif *vif,
13381 			       const struct cfg80211_bitrate_mask *mask)
13382 {
13383 	struct ath12k_vif *ahvif = ath12k_vif_to_ahvif(vif);
13384 	struct ath12k_link_vif *arvif;
13385 	struct cfg80211_chan_def def;
13386 	struct ath12k *ar;
13387 	enum nl80211_band band;
13388 	const u8 *ht_mcs_mask;
13389 	const u16 *vht_mcs_mask;
13390 	const u16 *he_mcs_mask;
13391 	const u16 *eht_mcs_mask;
13392 	u8 he_ltf = 0;
13393 	u8 he_gi = 0;
13394 	u8 eht_ltf = 0, eht_gi = 0;
13395 	u32 rate;
13396 	u8 nss, mac_nss;
13397 	u8 sgi;
13398 	u8 ldpc;
13399 	int single_nss;
13400 	int ret;
13401 	int num_rates;
13402 	bool he_fixed_rate = false;
13403 	bool eht_fixed_rate = false;
13404 
13405 	lockdep_assert_wiphy(hw->wiphy);
13406 
13407 	arvif = &ahvif->deflink;
13408 
13409 	ar = arvif->ar;
13410 	if (ath12k_mac_vif_link_chan(vif, arvif->link_id, &def)) {
13411 		ret = -EPERM;
13412 		goto out;
13413 	}
13414 
13415 	band = def.chan->band;
13416 	ht_mcs_mask = mask->control[band].ht_mcs;
13417 	vht_mcs_mask = mask->control[band].vht_mcs;
13418 	he_mcs_mask = mask->control[band].he_mcs;
13419 	eht_mcs_mask = mask->control[band].eht_mcs;
13420 	ldpc = !!(ar->ht_cap_info & WMI_HT_CAP_LDPC);
13421 
13422 	sgi = mask->control[band].gi;
13423 	if (sgi == NL80211_TXRATE_FORCE_SGI) {
13424 		ret = -EINVAL;
13425 		goto out;
13426 	}
13427 
13428 	he_gi = mask->control[band].he_gi;
13429 	he_ltf = mask->control[band].he_ltf;
13430 
13431 	eht_gi = mask->control[band].eht_gi;
13432 	eht_ltf = mask->control[band].eht_ltf;
13433 
13434 	/* mac80211 doesn't support sending a fixed HT/VHT MCS alone, rather it
13435 	 * requires passing at least one of used basic rates along with them.
13436 	 * Fixed rate setting across different preambles(legacy, HT, VHT) is
13437 	 * not supported by the FW. Hence use of FIXED_RATE vdev param is not
13438 	 * suitable for setting single HT/VHT rates.
13439 	 * But, there could be a single basic rate passed from userspace which
13440 	 * can be done through the FIXED_RATE param.
13441 	 */
13442 	if (ath12k_mac_has_single_legacy_rate(ar, band, mask)) {
13443 		ret = ath12k_mac_get_single_legacy_rate(ar, band, mask, &rate,
13444 							&nss);
13445 		if (ret) {
13446 			ath12k_warn(ar->ab, "failed to get single legacy rate for vdev %i: %d\n",
13447 				    arvif->vdev_id, ret);
13448 			goto out;
13449 		}
13450 
13451 		ieee80211_iterate_stations_mtx(hw,
13452 					       ath12k_mac_disable_peer_fixed_rate,
13453 					       arvif);
13454 	} else if (ath12k_mac_bitrate_mask_get_single_nss(ar, vif, band, mask,
13455 							  &single_nss)) {
13456 		rate = WMI_FIXED_RATE_NONE;
13457 		nss = single_nss;
13458 		arvif->bitrate_mask = *mask;
13459 
13460 		ieee80211_iterate_stations_atomic(hw,
13461 						  ath12k_mac_set_bitrate_mask_iter,
13462 						  arvif);
13463 	} else {
13464 		rate = WMI_FIXED_RATE_NONE;
13465 
13466 		if (!ath12k_mac_validate_fixed_rate_settings(ar, band,
13467 							     mask, arvif->link_id))
13468 			ath12k_warn(ar->ab,
13469 				    "failed to update fixed rate settings due to mcs/nss incompatibility\n");
13470 
13471 		mac_nss = max(max3(ath12k_mac_max_ht_nss(ht_mcs_mask),
13472 				   ath12k_mac_max_vht_nss(vht_mcs_mask),
13473 				   ath12k_mac_max_he_nss(he_mcs_mask)),
13474 			       ath12k_mac_max_eht_nss(eht_mcs_mask));
13475 		nss = min_t(u32, ar->num_tx_chains, mac_nss);
13476 
13477 		/* If multiple rates across different preambles are given
13478 		 * we can reconfigure this info with all peers using PEER_ASSOC
13479 		 * command with the below exception cases.
13480 		 * - Single VHT Rate : peer_assoc command accommodates only MCS
13481 		 * range values i.e 0-7, 0-8, 0-9 for VHT. Though mac80211
13482 		 * mandates passing basic rates along with HT/VHT rates, FW
13483 		 * doesn't allow switching from VHT to Legacy. Hence instead of
13484 		 * setting legacy and VHT rates using RATEMASK_CMD vdev cmd,
13485 		 * we could set this VHT rate as peer fixed rate param, which
13486 		 * will override FIXED rate and FW rate control algorithm.
13487 		 * If single VHT rate is passed along with HT rates, we select
13488 		 * the VHT rate as fixed rate for vht peers.
13489 		 * - Multiple VHT Rates : When Multiple VHT rates are given,this
13490 		 * can be set using RATEMASK CMD which uses FW rate-ctl alg.
13491 		 * TODO: Setting multiple VHT MCS and replacing peer_assoc with
13492 		 * RATEMASK_CMDID can cover all use cases of setting rates
13493 		 * across multiple preambles and rates within same type.
13494 		 * But requires more validation of the command at this point.
13495 		 */
13496 
13497 		num_rates = ath12k_mac_bitrate_mask_num_vht_rates(ar, band,
13498 								  mask);
13499 
13500 		if (!ath12k_mac_vht_mcs_range_present(ar, band, mask) &&
13501 		    num_rates > 1) {
13502 			/* TODO: Handle multiple VHT MCS values setting using
13503 			 * RATEMASK CMD
13504 			 */
13505 			ath12k_warn(ar->ab,
13506 				    "Setting more than one MCS Value in bitrate mask not supported\n");
13507 			ret = -EINVAL;
13508 			goto out;
13509 		}
13510 
13511 		num_rates = ath12k_mac_bitrate_mask_num_he_rates(ar, band, mask);
13512 		if (num_rates == 1)
13513 			he_fixed_rate = true;
13514 
13515 		if (!ath12k_mac_he_mcs_range_present(ar, band, mask) &&
13516 		    num_rates > 1) {
13517 			ath12k_warn(ar->ab,
13518 				    "Setting more than one HE MCS Value in bitrate mask not supported\n");
13519 			ret = -EINVAL;
13520 			goto out;
13521 		}
13522 
13523 		num_rates = ath12k_mac_bitrate_mask_num_eht_rates(ar, band,
13524 								  mask);
13525 		if (num_rates == 1)
13526 			eht_fixed_rate = true;
13527 
13528 		if (!ath12k_mac_eht_mcs_range_present(ar, band, mask) &&
13529 		    num_rates > 1) {
13530 			ath12k_warn(ar->ab,
13531 				    "Setting more than one EHT MCS Value in bitrate mask not supported\n");
13532 			ret = -EINVAL;
13533 			goto out;
13534 		}
13535 
13536 		ieee80211_iterate_stations_mtx(hw,
13537 					       ath12k_mac_disable_peer_fixed_rate,
13538 					       arvif);
13539 
13540 		arvif->bitrate_mask = *mask;
13541 		ieee80211_iterate_stations_mtx(hw,
13542 					       ath12k_mac_set_bitrate_mask_iter,
13543 					       arvif);
13544 	}
13545 
13546 	ret = ath12k_mac_set_rate_params(arvif, rate, nss, sgi, ldpc, he_gi,
13547 					 he_ltf, he_fixed_rate, eht_gi, eht_ltf,
13548 					 eht_fixed_rate);
13549 	if (ret) {
13550 		ath12k_warn(ar->ab, "failed to set rate params on vdev %i: %d\n",
13551 			    arvif->vdev_id, ret);
13552 	}
13553 
13554 out:
13555 	return ret;
13556 }
13557 EXPORT_SYMBOL(ath12k_mac_op_set_bitrate_mask);
13558 
13559 void
13560 ath12k_mac_op_reconfig_complete(struct ieee80211_hw *hw,
13561 				enum ieee80211_reconfig_type reconfig_type)
13562 {
13563 	struct ath12k_hw *ah = ath12k_hw_to_ah(hw);
13564 	struct ath12k *ar;
13565 	struct ath12k_base *ab;
13566 	struct ath12k_vif *ahvif;
13567 	struct ath12k_link_vif *arvif;
13568 	int recovery_count, i;
13569 
13570 	lockdep_assert_wiphy(hw->wiphy);
13571 
13572 	if (reconfig_type != IEEE80211_RECONFIG_TYPE_RESTART)
13573 		return;
13574 
13575 	guard(mutex)(&ah->hw_mutex);
13576 
13577 	if (ah->state != ATH12K_HW_STATE_RESTARTED)
13578 		return;
13579 
13580 	ah->state = ATH12K_HW_STATE_ON;
13581 	ieee80211_wake_queues(hw);
13582 
13583 	for_each_ar(ah, ar, i) {
13584 		ab = ar->ab;
13585 
13586 		ath12k_warn(ar->ab, "pdev %d successfully recovered\n",
13587 			    ar->pdev->pdev_id);
13588 
13589 		if (ar->ab->hw_params->current_cc_support &&
13590 		    ar->alpha2[0] != 0 && ar->alpha2[1] != 0) {
13591 			struct wmi_set_current_country_arg arg = {};
13592 
13593 			memcpy(&arg.alpha2, ar->alpha2, 2);
13594 			reinit_completion(&ar->regd_update_completed);
13595 			ath12k_wmi_send_set_current_country_cmd(ar, &arg);
13596 		}
13597 
13598 		if (ab->is_reset) {
13599 			recovery_count = atomic_inc_return(&ab->recovery_count);
13600 
13601 			ath12k_dbg(ab, ATH12K_DBG_BOOT, "recovery count %d\n",
13602 				   recovery_count);
13603 
13604 			/* When there are multiple radios in an SOC,
13605 			 * the recovery has to be done for each radio
13606 			 */
13607 			if (recovery_count == ab->num_radios) {
13608 				atomic_dec(&ab->reset_count);
13609 				complete(&ab->reset_complete);
13610 				ab->is_reset = false;
13611 				atomic_set(&ab->fail_cont_count, 0);
13612 				ath12k_dbg(ab, ATH12K_DBG_BOOT, "reset success\n");
13613 			}
13614 		}
13615 
13616 		list_for_each_entry(arvif, &ar->arvifs, list) {
13617 			ahvif = arvif->ahvif;
13618 			ath12k_dbg(ab, ATH12K_DBG_BOOT,
13619 				   "reconfig cipher %d up %d vdev type %d\n",
13620 				   ahvif->dp_vif.key_cipher,
13621 				   arvif->is_up,
13622 				   ahvif->vdev_type);
13623 
13624 			/* After trigger disconnect, then upper layer will
13625 			 * trigger connect again, then the PN number of
13626 			 * upper layer will be reset to keep up with AP
13627 			 * side, hence PN number mismatch will not happen.
13628 			 */
13629 			if (arvif->is_up &&
13630 			    ahvif->vdev_type == WMI_VDEV_TYPE_STA &&
13631 			    ahvif->vdev_subtype == WMI_VDEV_SUBTYPE_NONE) {
13632 				ieee80211_hw_restart_disconnect(ahvif->vif);
13633 
13634 				ath12k_dbg(ab, ATH12K_DBG_BOOT,
13635 					   "restart disconnect\n");
13636 			}
13637 		}
13638 	}
13639 }
13640 EXPORT_SYMBOL(ath12k_mac_op_reconfig_complete);
13641 
13642 static void
13643 ath12k_mac_update_bss_chan_survey(struct ath12k *ar,
13644 				  struct ieee80211_channel *channel)
13645 {
13646 	int ret;
13647 	enum wmi_bss_chan_info_req_type type = WMI_BSS_SURVEY_REQ_TYPE_READ;
13648 
13649 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
13650 
13651 	if (!test_bit(WMI_TLV_SERVICE_BSS_CHANNEL_INFO_64, ar->ab->wmi_ab.svc_map) ||
13652 	    ar->rx_channel != channel)
13653 		return;
13654 
13655 	if (ar->scan.state != ATH12K_SCAN_IDLE) {
13656 		ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
13657 			   "ignoring bss chan info req while scanning..\n");
13658 		return;
13659 	}
13660 
13661 	reinit_completion(&ar->bss_survey_done);
13662 
13663 	ret = ath12k_wmi_pdev_bss_chan_info_request(ar, type);
13664 	if (ret) {
13665 		ath12k_warn(ar->ab, "failed to send pdev bss chan info request\n");
13666 		return;
13667 	}
13668 
13669 	ret = wait_for_completion_timeout(&ar->bss_survey_done, 3 * HZ);
13670 	if (ret == 0)
13671 		ath12k_warn(ar->ab, "bss channel survey timed out\n");
13672 }
13673 
13674 int ath12k_mac_op_get_survey(struct ieee80211_hw *hw, int idx,
13675 			     struct survey_info *survey)
13676 {
13677 	struct ath12k_hw *ah = hw->priv;
13678 	struct ath12k *ar;
13679 	struct ieee80211_supported_band *sband;
13680 	struct survey_info *ah_survey;
13681 	int sband_idx = idx;
13682 
13683 	lockdep_assert_wiphy(hw->wiphy);
13684 
13685 	if (sband_idx >= ATH12K_NUM_CHANS)
13686 		return -ENOENT;
13687 
13688 	sband = hw->wiphy->bands[NL80211_BAND_2GHZ];
13689 	if (sband && sband_idx >= sband->n_channels) {
13690 		sband_idx -= sband->n_channels;
13691 		sband = NULL;
13692 	}
13693 
13694 	if (!sband)
13695 		sband = hw->wiphy->bands[NL80211_BAND_5GHZ];
13696 	if (sband && sband_idx >= sband->n_channels) {
13697 		sband_idx -= sband->n_channels;
13698 		sband = NULL;
13699 	}
13700 
13701 	if (!sband)
13702 		sband = hw->wiphy->bands[NL80211_BAND_6GHZ];
13703 
13704 	if (!sband || sband_idx >= sband->n_channels)
13705 		return -ENOENT;
13706 
13707 	ar = ath12k_mac_get_ar_by_chan(hw, &sband->channels[sband_idx]);
13708 	if (!ar) {
13709 		if (sband->channels[sband_idx].flags & IEEE80211_CHAN_DISABLED) {
13710 			memset(survey, 0, sizeof(*survey));
13711 			return 0;
13712 		}
13713 		return -ENOENT;
13714 	}
13715 
13716 	ah_survey = &ah->survey[idx];
13717 
13718 	ath12k_mac_update_bss_chan_survey(ar, &sband->channels[sband_idx]);
13719 
13720 	scoped_guard(spinlock_bh, &ah->survey_lock) {
13721 		memcpy(survey, ah_survey, sizeof(*survey));
13722 	}
13723 
13724 	survey->channel = &sband->channels[sband_idx];
13725 
13726 	if (ar->rx_channel == survey->channel)
13727 		survey->filled |= SURVEY_INFO_IN_USE;
13728 
13729 	return 0;
13730 }
13731 EXPORT_SYMBOL(ath12k_mac_op_get_survey);
13732 
13733 static void ath12k_mac_put_chain_rssi(struct station_info *sinfo,
13734 				      struct ath12k_link_sta *arsta)
13735 {
13736 	s8 rssi;
13737 	int i;
13738 
13739 	for (i = 0; i < ARRAY_SIZE(sinfo->chain_signal); i++) {
13740 		sinfo->chains &= ~BIT(i);
13741 		rssi = arsta->chain_signal[i];
13742 
13743 		if (rssi != ATH12K_DEFAULT_NOISE_FLOOR &&
13744 		    rssi != ATH12K_INVALID_RSSI_FULL &&
13745 		    rssi != ATH12K_INVALID_RSSI_EMPTY &&
13746 		    rssi != 0) {
13747 			sinfo->chain_signal[i] = rssi;
13748 			sinfo->chains |= BIT(i);
13749 			sinfo->filled |= BIT_ULL(NL80211_STA_INFO_CHAIN_SIGNAL);
13750 		}
13751 	}
13752 }
13753 
13754 void ath12k_mac_op_sta_statistics(struct ieee80211_hw *hw,
13755 				  struct ieee80211_vif *vif,
13756 				  struct ieee80211_sta *sta,
13757 				  struct station_info *sinfo)
13758 {
13759 	struct ath12k_sta *ahsta = ath12k_sta_to_ahsta(sta);
13760 	struct ath12k_dp_link_peer_rate_info rate_info = {};
13761 	struct ath12k_fw_stats_req_params params = {};
13762 	struct ath12k_dp_link_peer *peer;
13763 	struct ath12k_link_sta *arsta;
13764 	s8 signal, noise_floor;
13765 	struct ath12k_dp *dp;
13766 	struct ath12k *ar;
13767 	bool db2dbm;
13768 
13769 	lockdep_assert_wiphy(hw->wiphy);
13770 
13771 	arsta = &ahsta->deflink;
13772 	ar = ath12k_get_ar_by_vif(hw, vif, arsta->link_id);
13773 	if (!ar)
13774 		return;
13775 
13776 	dp = ath12k_ab_to_dp(ar->ab);
13777 	ath12k_dp_link_peer_get_sta_rate_info_stats(dp, arsta->addr, &rate_info);
13778 
13779 	db2dbm = test_bit(WMI_TLV_SERVICE_HW_DB2DBM_CONVERSION_SUPPORT,
13780 			  ar->ab->wmi_ab.svc_map);
13781 
13782 	sinfo->rx_duration = rate_info.rx_duration;
13783 	sinfo->filled |= BIT_ULL(NL80211_STA_INFO_RX_DURATION);
13784 
13785 	sinfo->tx_duration = rate_info.tx_duration;
13786 	sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_DURATION);
13787 
13788 	if (rate_info.txrate.legacy || rate_info.txrate.nss) {
13789 		if (rate_info.txrate.legacy) {
13790 			sinfo->txrate.legacy = rate_info.txrate.legacy;
13791 		} else {
13792 			sinfo->txrate.mcs = rate_info.txrate.mcs;
13793 			sinfo->txrate.nss = rate_info.txrate.nss;
13794 			sinfo->txrate.bw = rate_info.txrate.bw;
13795 			sinfo->txrate.he_gi = rate_info.txrate.he_gi;
13796 			sinfo->txrate.he_dcm = rate_info.txrate.he_dcm;
13797 			sinfo->txrate.he_ru_alloc = rate_info.txrate.he_ru_alloc;
13798 			sinfo->txrate.eht_gi = rate_info.txrate.eht_gi;
13799 			sinfo->txrate.eht_ru_alloc = rate_info.txrate.eht_ru_alloc;
13800 		}
13801 		sinfo->txrate.flags = rate_info.txrate.flags;
13802 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_BITRATE);
13803 	}
13804 
13805 	/* TODO: Use real NF instead of default one. */
13806 	signal = rate_info.rssi_comb;
13807 
13808 	params.pdev_id = ath12k_mac_get_target_pdev_id(ar);
13809 	params.vdev_id = 0;
13810 	params.stats_id = WMI_REQUEST_VDEV_STAT;
13811 
13812 	if (!signal &&
13813 	    ahsta->ahvif->vdev_type == WMI_VDEV_TYPE_STA &&
13814 	    !(ath12k_mac_get_fw_stats(ar, &params))) {
13815 		signal = arsta->rssi_beacon;
13816 		ath12k_fw_stats_reset(ar);
13817 	}
13818 
13819 	params.stats_id = WMI_REQUEST_RSSI_PER_CHAIN_STAT;
13820 	if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_CHAIN_SIGNAL)) &&
13821 	    ahsta->ahvif->vdev_type == WMI_VDEV_TYPE_STA &&
13822 	    !(ath12k_mac_get_fw_stats(ar, &params))) {
13823 		ath12k_mac_put_chain_rssi(sinfo, arsta);
13824 		ath12k_fw_stats_reset(ar);
13825 	}
13826 
13827 	spin_lock_bh(&ar->data_lock);
13828 	noise_floor = ath12k_pdev_get_noise_floor(ar);
13829 	spin_unlock_bh(&ar->data_lock);
13830 
13831 	if (signal) {
13832 		sinfo->signal = db2dbm ? signal : signal + noise_floor;
13833 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_SIGNAL);
13834 	}
13835 
13836 	sinfo->signal_avg = rate_info.signal_avg;
13837 
13838 	if (!db2dbm)
13839 		sinfo->signal_avg += noise_floor;
13840 
13841 	sinfo->filled |= BIT_ULL(NL80211_STA_INFO_SIGNAL_AVG);
13842 
13843 	spin_lock_bh(&dp->dp_lock);
13844 	peer = ath12k_dp_link_peer_find_by_addr(dp, arsta->addr);
13845 	if (!peer) {
13846 		spin_unlock_bh(&dp->dp_lock);
13847 		return;
13848 	}
13849 
13850 	sinfo->tx_retries = peer->tx_retry_count;
13851 	sinfo->tx_failed = peer->tx_retry_failed;
13852 	sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_RETRIES);
13853 	sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_FAILED);
13854 
13855 	spin_unlock_bh(&dp->dp_lock);
13856 }
13857 EXPORT_SYMBOL(ath12k_mac_op_sta_statistics);
13858 
13859 void ath12k_mac_op_link_sta_statistics(struct ieee80211_hw *hw,
13860 				       struct ieee80211_vif *vif,
13861 				       struct ieee80211_link_sta *link_sta,
13862 				       struct link_station_info *link_sinfo)
13863 {
13864 	struct ath12k_sta *ahsta = ath12k_sta_to_ahsta(link_sta->sta);
13865 	struct ath12k_fw_stats_req_params params = {};
13866 	struct ath12k_dp_link_peer *peer;
13867 	struct ath12k_link_sta *arsta;
13868 	struct ath12k *ar;
13869 	s8 signal;
13870 	bool db2dbm;
13871 
13872 	lockdep_assert_wiphy(hw->wiphy);
13873 
13874 	arsta = wiphy_dereference(hw->wiphy, ahsta->link[link_sta->link_id]);
13875 
13876 	if (!arsta)
13877 		return;
13878 
13879 	ar = ath12k_get_ar_by_vif(hw, vif, arsta->link_id);
13880 	if (!ar)
13881 		return;
13882 
13883 	db2dbm = test_bit(WMI_TLV_SERVICE_HW_DB2DBM_CONVERSION_SUPPORT,
13884 			  ar->ab->wmi_ab.svc_map);
13885 
13886 	spin_lock_bh(&ar->ab->dp->dp_lock);
13887 	peer = ath12k_dp_link_peer_find_by_addr(ar->ab->dp, arsta->addr);
13888 	if (!peer) {
13889 		spin_unlock_bh(&ar->ab->dp->dp_lock);
13890 		return;
13891 	}
13892 
13893 	link_sinfo->rx_duration = peer->rx_duration;
13894 	link_sinfo->filled |= BIT_ULL(NL80211_STA_INFO_RX_DURATION);
13895 
13896 	link_sinfo->tx_duration = peer->tx_duration;
13897 	link_sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_DURATION);
13898 
13899 	if (peer->txrate.legacy || peer->txrate.nss) {
13900 		if (peer->txrate.legacy) {
13901 			link_sinfo->txrate.legacy = peer->txrate.legacy;
13902 		} else {
13903 			link_sinfo->txrate.mcs = peer->txrate.mcs;
13904 			link_sinfo->txrate.nss = peer->txrate.nss;
13905 			link_sinfo->txrate.bw = peer->txrate.bw;
13906 			link_sinfo->txrate.he_gi = peer->txrate.he_gi;
13907 			link_sinfo->txrate.he_dcm = peer->txrate.he_dcm;
13908 			link_sinfo->txrate.he_ru_alloc =
13909 				peer->txrate.he_ru_alloc;
13910 			link_sinfo->txrate.eht_gi = peer->txrate.eht_gi;
13911 			link_sinfo->txrate.eht_ru_alloc =
13912 				peer->txrate.eht_ru_alloc;
13913 		}
13914 		link_sinfo->txrate.flags = peer->txrate.flags;
13915 		link_sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_BITRATE);
13916 	}
13917 
13918 	link_sinfo->signal_avg = ewma_avg_rssi_read(&peer->avg_rssi);
13919 
13920 	if (!db2dbm)
13921 		link_sinfo->signal_avg += ATH12K_DEFAULT_NOISE_FLOOR;
13922 
13923 	link_sinfo->filled |= BIT_ULL(NL80211_STA_INFO_SIGNAL_AVG);
13924 
13925 	link_sinfo->tx_retries = peer->tx_retry_count;
13926 	link_sinfo->tx_failed = peer->tx_retry_failed;
13927 	link_sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_RETRIES);
13928 	link_sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_FAILED);
13929 
13930 	/* TODO: Use real NF instead of default one. */
13931 	signal = peer->rssi_comb;
13932 
13933 	spin_unlock_bh(&ar->ab->dp->dp_lock);
13934 
13935 	if (!signal && ahsta->ahvif->vdev_type == WMI_VDEV_TYPE_STA) {
13936 		params.pdev_id = ath12k_mac_get_target_pdev_id(ar);
13937 		params.vdev_id = 0;
13938 		params.stats_id = WMI_REQUEST_VDEV_STAT;
13939 
13940 		if (!ath12k_mac_get_fw_stats(ar, &params)) {
13941 			signal = arsta->rssi_beacon;
13942 			ath12k_fw_stats_reset(ar);
13943 		}
13944 	}
13945 
13946 	if (signal) {
13947 		link_sinfo->signal =
13948 			db2dbm ? signal : signal + ATH12K_DEFAULT_NOISE_FLOOR;
13949 		link_sinfo->filled |= BIT_ULL(NL80211_STA_INFO_SIGNAL);
13950 	}
13951 }
13952 EXPORT_SYMBOL(ath12k_mac_op_link_sta_statistics);
13953 
13954 int ath12k_mac_op_cancel_remain_on_channel(struct ieee80211_hw *hw,
13955 					   struct ieee80211_vif *vif)
13956 {
13957 	struct ath12k_hw *ah = ath12k_hw_to_ah(hw);
13958 	struct ath12k *ar;
13959 
13960 	ar = ath12k_ah_to_ar(ah, 0);
13961 
13962 	lockdep_assert_wiphy(hw->wiphy);
13963 
13964 	spin_lock_bh(&ar->data_lock);
13965 	ar->scan.roc_notify = false;
13966 	spin_unlock_bh(&ar->data_lock);
13967 
13968 	ath12k_scan_abort(ar);
13969 
13970 	cancel_delayed_work_sync(&ar->scan.timeout);
13971 	wiphy_work_flush(hw->wiphy, &ar->scan.vdev_clean_wk);
13972 
13973 	return 0;
13974 }
13975 EXPORT_SYMBOL(ath12k_mac_op_cancel_remain_on_channel);
13976 
13977 int ath12k_mac_op_remain_on_channel(struct ieee80211_hw *hw,
13978 				    struct ieee80211_vif *vif,
13979 				    struct ieee80211_channel *chan,
13980 				    int duration,
13981 				    enum ieee80211_roc_type type)
13982 {
13983 	struct ath12k_vif *ahvif = ath12k_vif_to_ahvif(vif);
13984 	struct ath12k_hw *ah = ath12k_hw_to_ah(hw);
13985 	struct ath12k_link_vif *arvif;
13986 	struct ath12k *ar;
13987 	u32 scan_time_msec;
13988 	bool create = true;
13989 	u8 link_id;
13990 	int ret;
13991 
13992 	lockdep_assert_wiphy(hw->wiphy);
13993 
13994 	ar = ath12k_mac_select_scan_device(hw, vif, chan->center_freq);
13995 	if (!ar)
13996 		return -EINVAL;
13997 
13998 	/* check if any of the links of ML VIF is already started on
13999 	 * radio(ar) corresponding to given scan frequency and use it,
14000 	 * if not use deflink(link 0) for scan purpose.
14001 	 */
14002 
14003 	link_id = ath12k_mac_find_link_id_by_ar(ahvif, ar);
14004 	arvif = ath12k_mac_assign_link_vif(ah, vif, link_id);
14005 	/* If the vif is already assigned to a specific vdev of an ar,
14006 	 * check whether its already started, vdev which is started
14007 	 * are not allowed to switch to a new radio.
14008 	 * If the vdev is not started, but was earlier created on a
14009 	 * different ar, delete that vdev and create a new one. We don't
14010 	 * delete at the scan stop as an optimization to avoid redundant
14011 	 * delete-create vdev's for the same ar, in case the request is
14012 	 * always on the same band for the vif
14013 	 */
14014 	if (arvif->is_created) {
14015 		if (WARN_ON(!arvif->ar))
14016 			return -EINVAL;
14017 
14018 		if (ar != arvif->ar && arvif->is_started)
14019 			return -EBUSY;
14020 
14021 		if (ar != arvif->ar) {
14022 			ath12k_mac_remove_link_interface(hw, arvif);
14023 			ath12k_mac_unassign_link_vif(arvif);
14024 		} else {
14025 			create = false;
14026 		}
14027 	}
14028 
14029 	if (create) {
14030 		arvif = ath12k_mac_assign_link_vif(ah, vif, link_id);
14031 
14032 		ret = ath12k_mac_vdev_create(ar, arvif);
14033 		if (ret) {
14034 			ath12k_warn(ar->ab, "unable to create scan vdev for roc: %d\n",
14035 				    ret);
14036 			ath12k_mac_unassign_link_vif(arvif);
14037 			return ret;
14038 		}
14039 	}
14040 
14041 	spin_lock_bh(&ar->data_lock);
14042 
14043 	switch (ar->scan.state) {
14044 	case ATH12K_SCAN_IDLE:
14045 		reinit_completion(&ar->scan.started);
14046 		reinit_completion(&ar->scan.completed);
14047 		reinit_completion(&ar->scan.on_channel);
14048 		ar->scan.state = ATH12K_SCAN_STARTING;
14049 		ar->scan.is_roc = true;
14050 		ar->scan.arvif = arvif;
14051 		ar->scan.roc_freq = chan->center_freq;
14052 		ar->scan.roc_notify = true;
14053 		ret = 0;
14054 		break;
14055 	case ATH12K_SCAN_STARTING:
14056 	case ATH12K_SCAN_RUNNING:
14057 	case ATH12K_SCAN_ABORTING:
14058 		ret = -EBUSY;
14059 		break;
14060 	}
14061 
14062 	spin_unlock_bh(&ar->data_lock);
14063 
14064 	if (ret)
14065 		return ret;
14066 
14067 	scan_time_msec = hw->wiphy->max_remain_on_channel_duration * 2;
14068 
14069 	struct ath12k_wmi_scan_req_arg *arg __free(kfree) =
14070 					kzalloc_flex(*arg, chan_list, 1);
14071 	if (!arg)
14072 		return -ENOMEM;
14073 
14074 	arg->num_chan = 1;
14075 	ath12k_wmi_start_scan_init(ar, arg);
14076 
14077 	arg->vdev_id = arvif->vdev_id;
14078 	arg->scan_id = ATH12K_SCAN_ID;
14079 	arg->chan_list[0] = chan->center_freq;
14080 	arg->dwell_time_active = scan_time_msec;
14081 	arg->dwell_time_passive = scan_time_msec;
14082 	arg->max_scan_time = scan_time_msec;
14083 	arg->scan_f_passive = 1;
14084 	arg->burst_duration = duration;
14085 
14086 	ret = ath12k_start_scan(ar, arg);
14087 	if (ret) {
14088 		ath12k_warn(ar->ab, "failed to start roc scan: %d\n", ret);
14089 
14090 		spin_lock_bh(&ar->data_lock);
14091 		ar->scan.state = ATH12K_SCAN_IDLE;
14092 		spin_unlock_bh(&ar->data_lock);
14093 		return ret;
14094 	}
14095 
14096 	ret = wait_for_completion_timeout(&ar->scan.on_channel, 3 * HZ);
14097 	if (ret == 0) {
14098 		ath12k_warn(ar->ab, "failed to switch to channel for roc scan\n");
14099 		ret = ath12k_scan_stop(ar);
14100 		if (ret)
14101 			ath12k_warn(ar->ab, "failed to stop scan: %d\n", ret);
14102 		return -ETIMEDOUT;
14103 	}
14104 
14105 	ieee80211_queue_delayed_work(hw, &ar->scan.timeout,
14106 				     msecs_to_jiffies(duration));
14107 
14108 	return 0;
14109 }
14110 EXPORT_SYMBOL(ath12k_mac_op_remain_on_channel);
14111 
14112 void ath12k_mac_op_set_rekey_data(struct ieee80211_hw *hw,
14113 				  struct ieee80211_vif *vif,
14114 				  struct cfg80211_gtk_rekey_data *data)
14115 {
14116 	struct ath12k_vif *ahvif = ath12k_vif_to_ahvif(vif);
14117 	struct ath12k_rekey_data *rekey_data;
14118 	struct ath12k_hw *ah = ath12k_hw_to_ah(hw);
14119 	struct ath12k *ar = ath12k_ah_to_ar(ah, 0);
14120 	struct ath12k_link_vif *arvif;
14121 
14122 	lockdep_assert_wiphy(hw->wiphy);
14123 
14124 	arvif = &ahvif->deflink;
14125 	rekey_data = &arvif->rekey_data;
14126 
14127 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac set rekey data vdev %d\n",
14128 		   arvif->vdev_id);
14129 
14130 	memcpy(rekey_data->kck, data->kck, NL80211_KCK_LEN);
14131 	memcpy(rekey_data->kek, data->kek, NL80211_KEK_LEN);
14132 
14133 	/* The supplicant works on big-endian, the firmware expects it on
14134 	 * little endian.
14135 	 */
14136 	rekey_data->replay_ctr = get_unaligned_be64(data->replay_ctr);
14137 
14138 	arvif->rekey_data.enable_offload = true;
14139 
14140 	ath12k_dbg_dump(ar->ab, ATH12K_DBG_MAC, "kck", NULL,
14141 			rekey_data->kck, NL80211_KCK_LEN);
14142 	ath12k_dbg_dump(ar->ab, ATH12K_DBG_MAC, "kek", NULL,
14143 			rekey_data->kck, NL80211_KEK_LEN);
14144 	ath12k_dbg_dump(ar->ab, ATH12K_DBG_MAC, "replay ctr", NULL,
14145 			&rekey_data->replay_ctr, sizeof(rekey_data->replay_ctr));
14146 }
14147 EXPORT_SYMBOL(ath12k_mac_op_set_rekey_data);
14148 
14149 void ath12k_mac_update_freq_range(struct ath12k *ar,
14150 				  u32 freq_low, u32 freq_high)
14151 {
14152 	if (!(freq_low && freq_high))
14153 		return;
14154 
14155 	if (ar->freq_range.start_freq || ar->freq_range.end_freq) {
14156 		ar->freq_range.start_freq = min(ar->freq_range.start_freq,
14157 						MHZ_TO_KHZ(freq_low));
14158 		ar->freq_range.end_freq = max(ar->freq_range.end_freq,
14159 					      MHZ_TO_KHZ(freq_high));
14160 	} else {
14161 		ar->freq_range.start_freq = MHZ_TO_KHZ(freq_low);
14162 		ar->freq_range.end_freq = MHZ_TO_KHZ(freq_high);
14163 	}
14164 
14165 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
14166 		   "mac pdev %u freq limit updated. New range %u->%u MHz\n",
14167 		   ar->pdev->pdev_id, KHZ_TO_MHZ(ar->freq_range.start_freq),
14168 		   KHZ_TO_MHZ(ar->freq_range.end_freq));
14169 }
14170 
14171 static void ath12k_mac_update_ch_list(struct ath12k *ar,
14172 				      struct ieee80211_supported_band *band,
14173 				      u32 freq_low, u32 freq_high)
14174 {
14175 	int i;
14176 
14177 	if (!(freq_low && freq_high))
14178 		return;
14179 
14180 	for (i = 0; i < band->n_channels; i++) {
14181 		if (band->channels[i].center_freq < freq_low ||
14182 		    band->channels[i].center_freq > freq_high)
14183 			band->channels[i].flags |= IEEE80211_CHAN_DISABLED;
14184 	}
14185 }
14186 
14187 static u32 ath12k_get_phy_id(struct ath12k *ar, u32 band)
14188 {
14189 	struct ath12k_pdev *pdev = ar->pdev;
14190 	struct ath12k_pdev_cap *pdev_cap = &pdev->cap;
14191 
14192 	if (band == WMI_HOST_WLAN_2GHZ_CAP)
14193 		return pdev_cap->band[NL80211_BAND_2GHZ].phy_id;
14194 
14195 	if (band == WMI_HOST_WLAN_5GHZ_CAP)
14196 		return pdev_cap->band[NL80211_BAND_5GHZ].phy_id;
14197 
14198 	ath12k_warn(ar->ab, "unsupported phy cap:%d\n", band);
14199 
14200 	return 0;
14201 }
14202 
14203 static int ath12k_mac_update_band(struct ath12k *ar,
14204 				  struct ieee80211_supported_band *orig_band,
14205 				  struct ieee80211_supported_band *new_band)
14206 {
14207 	int i;
14208 
14209 	if (!orig_band || !new_band)
14210 		return -EINVAL;
14211 
14212 	if (orig_band->band != new_band->band)
14213 		return -EINVAL;
14214 
14215 	for (i = 0; i < new_band->n_channels; i++) {
14216 		if (new_band->channels[i].flags & IEEE80211_CHAN_DISABLED)
14217 			continue;
14218 		/* An enabled channel in new_band should not be already enabled
14219 		 * in the orig_band
14220 		 */
14221 		if (WARN_ON(!(orig_band->channels[i].flags &
14222 			      IEEE80211_CHAN_DISABLED)))
14223 			return -EINVAL;
14224 		orig_band->channels[i].flags &= ~IEEE80211_CHAN_DISABLED;
14225 	}
14226 	return 0;
14227 }
14228 
14229 static void ath12k_mac_update_5_9_ghz_ch_list(struct ath12k *ar,
14230 					      struct ieee80211_supported_band *band)
14231 {
14232 	int i;
14233 
14234 	if (test_bit(WMI_TLV_SERVICE_5_9GHZ_SUPPORT,
14235 		     ar->ab->wmi_ab.svc_map))
14236 		return;
14237 
14238 	guard(spinlock_bh)(&ar->ab->base_lock);
14239 	if (ar->ab->dfs_region != ATH12K_DFS_REG_FCC)
14240 		return;
14241 
14242 	for (i = 0; i < band->n_channels; i++) {
14243 		if (band->channels[i].center_freq >= ATH12K_5_9_GHZ_MIN_FREQ &&
14244 		    band->channels[i].center_freq <= ATH12K_5_9_GHZ_MAX_FREQ)
14245 			band->channels[i].flags |= IEEE80211_CHAN_DISABLED;
14246 	}
14247 }
14248 
14249 static int ath12k_mac_setup_channels_rates(struct ath12k *ar,
14250 					   u32 supported_bands,
14251 					   struct ieee80211_supported_band *bands[])
14252 {
14253 	struct ieee80211_supported_band *band;
14254 	struct ath12k_wmi_hal_reg_capabilities_ext_arg *reg_cap;
14255 	struct ath12k_base *ab = ar->ab;
14256 	u32 phy_id, freq_low, freq_high;
14257 	struct ath12k_hw *ah = ar->ah;
14258 	void *channels;
14259 	int ret;
14260 
14261 	BUILD_BUG_ON((ARRAY_SIZE(ath12k_2ghz_channels) +
14262 		      ARRAY_SIZE(ath12k_5ghz_channels) +
14263 		      ARRAY_SIZE(ath12k_6ghz_channels)) !=
14264 		     ATH12K_NUM_CHANS);
14265 
14266 	reg_cap = &ab->hal_reg_cap[ar->pdev_idx];
14267 
14268 	if (supported_bands & WMI_HOST_WLAN_2GHZ_CAP) {
14269 		channels = kmemdup(ath12k_2ghz_channels,
14270 				   sizeof(ath12k_2ghz_channels),
14271 				   GFP_KERNEL);
14272 		if (!channels)
14273 			return -ENOMEM;
14274 
14275 		band = &ar->mac.sbands[NL80211_BAND_2GHZ];
14276 		band->band = NL80211_BAND_2GHZ;
14277 		band->n_channels = ARRAY_SIZE(ath12k_2ghz_channels);
14278 		band->channels = channels;
14279 		band->n_bitrates = ath12k_g_rates_size;
14280 		band->bitrates = ath12k_g_rates;
14281 
14282 		if (ab->hw_params->single_pdev_only) {
14283 			phy_id = ath12k_get_phy_id(ar, WMI_HOST_WLAN_2GHZ_CAP);
14284 			reg_cap = &ab->hal_reg_cap[phy_id];
14285 		}
14286 
14287 		freq_low = max(reg_cap->low_2ghz_chan,
14288 			       ab->reg_freq_2ghz.start_freq);
14289 		freq_high = min(reg_cap->high_2ghz_chan,
14290 				ab->reg_freq_2ghz.end_freq);
14291 
14292 		ath12k_mac_update_ch_list(ar, band,
14293 					  reg_cap->low_2ghz_chan,
14294 					  reg_cap->high_2ghz_chan);
14295 
14296 		ath12k_mac_update_freq_range(ar, freq_low, freq_high);
14297 
14298 		if (!bands[NL80211_BAND_2GHZ]) {
14299 			bands[NL80211_BAND_2GHZ] = band;
14300 		} else {
14301 			/* Split mac in same band under same wiphy */
14302 			ret = ath12k_mac_update_band(ar, bands[NL80211_BAND_2GHZ], band);
14303 			if (ret) {
14304 				kfree(channels);
14305 				band->channels = NULL;
14306 				return ret;
14307 			}
14308 			ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac pdev %u identified as 2 GHz split mac with start freq %d end freq %d",
14309 				   ar->pdev->pdev_id,
14310 				   KHZ_TO_MHZ(ar->freq_range.start_freq),
14311 				   KHZ_TO_MHZ(ar->freq_range.end_freq));
14312 		}
14313 	}
14314 
14315 	if (supported_bands & WMI_HOST_WLAN_5GHZ_CAP) {
14316 		if (reg_cap->high_5ghz_chan >= ATH12K_MIN_6GHZ_FREQ) {
14317 			channels = kmemdup(ath12k_6ghz_channels,
14318 					   sizeof(ath12k_6ghz_channels), GFP_KERNEL);
14319 			if (!channels) {
14320 				kfree(ar->mac.sbands[NL80211_BAND_2GHZ].channels);
14321 				ar->mac.sbands[NL80211_BAND_2GHZ].channels = NULL;
14322 				return -ENOMEM;
14323 			}
14324 
14325 			ar->supports_6ghz = true;
14326 			band = &ar->mac.sbands[NL80211_BAND_6GHZ];
14327 			band->band = NL80211_BAND_6GHZ;
14328 			band->n_channels = ARRAY_SIZE(ath12k_6ghz_channels);
14329 			band->channels = channels;
14330 			band->n_bitrates = ath12k_a_rates_size;
14331 			band->bitrates = ath12k_a_rates;
14332 
14333 			freq_low = max(reg_cap->low_5ghz_chan,
14334 				       ab->reg_freq_6ghz.start_freq);
14335 			freq_high = min(reg_cap->high_5ghz_chan,
14336 					ab->reg_freq_6ghz.end_freq);
14337 
14338 			ath12k_mac_update_ch_list(ar, band,
14339 						  reg_cap->low_5ghz_chan,
14340 						  reg_cap->high_5ghz_chan);
14341 
14342 			ath12k_mac_update_freq_range(ar, freq_low, freq_high);
14343 			ah->use_6ghz_regd = true;
14344 
14345 			if (!bands[NL80211_BAND_6GHZ]) {
14346 				bands[NL80211_BAND_6GHZ] = band;
14347 			} else {
14348 				/* Split mac in same band under same wiphy */
14349 				ret = ath12k_mac_update_band(ar,
14350 							     bands[NL80211_BAND_6GHZ],
14351 							     band);
14352 				if (ret) {
14353 					kfree(ar->mac.sbands[NL80211_BAND_2GHZ].channels);
14354 					ar->mac.sbands[NL80211_BAND_2GHZ].channels = NULL;
14355 					kfree(channels);
14356 					band->channels = NULL;
14357 					return ret;
14358 				}
14359 				ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac pdev %u identified as 6 GHz split mac with start freq %d end freq %d",
14360 					   ar->pdev->pdev_id,
14361 					   KHZ_TO_MHZ(ar->freq_range.start_freq),
14362 					   KHZ_TO_MHZ(ar->freq_range.end_freq));
14363 			}
14364 		}
14365 
14366 		if (reg_cap->low_5ghz_chan < ATH12K_MIN_6GHZ_FREQ) {
14367 			channels = kmemdup(ath12k_5ghz_channels,
14368 					   sizeof(ath12k_5ghz_channels),
14369 					   GFP_KERNEL);
14370 			if (!channels) {
14371 				kfree(ar->mac.sbands[NL80211_BAND_2GHZ].channels);
14372 				ar->mac.sbands[NL80211_BAND_2GHZ].channels = NULL;
14373 				kfree(ar->mac.sbands[NL80211_BAND_6GHZ].channels);
14374 				ar->mac.sbands[NL80211_BAND_6GHZ].channels = NULL;
14375 				return -ENOMEM;
14376 			}
14377 
14378 			band = &ar->mac.sbands[NL80211_BAND_5GHZ];
14379 			band->band = NL80211_BAND_5GHZ;
14380 			band->n_channels = ARRAY_SIZE(ath12k_5ghz_channels);
14381 			band->channels = channels;
14382 			band->n_bitrates = ath12k_a_rates_size;
14383 			band->bitrates = ath12k_a_rates;
14384 
14385 			ath12k_mac_update_5_9_ghz_ch_list(ar, band);
14386 
14387 			if (ab->hw_params->single_pdev_only) {
14388 				phy_id = ath12k_get_phy_id(ar, WMI_HOST_WLAN_5GHZ_CAP);
14389 				reg_cap = &ab->hal_reg_cap[phy_id];
14390 			}
14391 
14392 			freq_low = max(reg_cap->low_5ghz_chan,
14393 				       ab->reg_freq_5ghz.start_freq);
14394 			freq_high = min(reg_cap->high_5ghz_chan,
14395 					ab->reg_freq_5ghz.end_freq);
14396 
14397 			ath12k_mac_update_ch_list(ar, band,
14398 						  reg_cap->low_5ghz_chan,
14399 						  reg_cap->high_5ghz_chan);
14400 
14401 			ath12k_mac_update_freq_range(ar, freq_low, freq_high);
14402 
14403 			if (!bands[NL80211_BAND_5GHZ]) {
14404 				bands[NL80211_BAND_5GHZ] = band;
14405 			} else {
14406 				/* Split mac in same band under same wiphy */
14407 				ret = ath12k_mac_update_band(ar,
14408 							     bands[NL80211_BAND_5GHZ],
14409 							     band);
14410 				if (ret) {
14411 					kfree(ar->mac.sbands[NL80211_BAND_2GHZ].channels);
14412 					ar->mac.sbands[NL80211_BAND_2GHZ].channels = NULL;
14413 					kfree(ar->mac.sbands[NL80211_BAND_6GHZ].channels);
14414 					ar->mac.sbands[NL80211_BAND_6GHZ].channels = NULL;
14415 					kfree(channels);
14416 					band->channels = NULL;
14417 					return ret;
14418 				}
14419 				ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac pdev %u identified as 5 GHz split mac with start freq %d end freq %d",
14420 					   ar->pdev->pdev_id,
14421 					   KHZ_TO_MHZ(ar->freq_range.start_freq),
14422 					   KHZ_TO_MHZ(ar->freq_range.end_freq));
14423 			}
14424 		}
14425 	}
14426 
14427 	return 0;
14428 }
14429 
14430 static u16 ath12k_mac_get_ifmodes(struct ath12k_hw *ah)
14431 {
14432 	struct ath12k *ar;
14433 	int i;
14434 	u16 interface_modes = U16_MAX;
14435 
14436 	for_each_ar(ah, ar, i)
14437 		interface_modes &= ar->ab->hw_params->interface_modes;
14438 
14439 	return interface_modes == U16_MAX ? 0 : interface_modes;
14440 }
14441 
14442 static bool ath12k_mac_is_iface_mode_enable(struct ath12k_hw *ah,
14443 					    enum nl80211_iftype type)
14444 {
14445 	struct ath12k *ar;
14446 	int i;
14447 	u16 interface_modes, mode = 0;
14448 	bool is_enable = false;
14449 
14450 	if (type == NL80211_IFTYPE_MESH_POINT) {
14451 		if (IS_ENABLED(CONFIG_MAC80211_MESH))
14452 			mode = BIT(type);
14453 	} else {
14454 		mode = BIT(type);
14455 	}
14456 
14457 	for_each_ar(ah, ar, i) {
14458 		interface_modes = ar->ab->hw_params->interface_modes;
14459 		if (interface_modes & mode) {
14460 			is_enable = true;
14461 			break;
14462 		}
14463 	}
14464 
14465 	return is_enable;
14466 }
14467 
14468 static int
14469 ath12k_mac_setup_radio_iface_comb(struct ath12k *ar,
14470 				  struct ieee80211_iface_combination *comb)
14471 {
14472 	u16 interface_modes = ar->ab->hw_params->interface_modes;
14473 	struct ieee80211_iface_limit *limits;
14474 	int n_limits, max_interfaces;
14475 	bool ap, mesh, p2p;
14476 
14477 	ap = interface_modes & BIT(NL80211_IFTYPE_AP);
14478 	p2p = interface_modes & BIT(NL80211_IFTYPE_P2P_DEVICE);
14479 
14480 	mesh = IS_ENABLED(CONFIG_MAC80211_MESH) &&
14481 	       (interface_modes & BIT(NL80211_IFTYPE_MESH_POINT));
14482 
14483 	if ((ap || mesh) && !p2p) {
14484 		n_limits = 2;
14485 		max_interfaces = 16;
14486 	} else if (p2p) {
14487 		n_limits = 3;
14488 		if (ap || mesh)
14489 			max_interfaces = 16;
14490 		else
14491 			max_interfaces = 3;
14492 	} else {
14493 		n_limits = 1;
14494 		max_interfaces = 1;
14495 	}
14496 
14497 	limits = kzalloc_objs(*limits, n_limits);
14498 	if (!limits)
14499 		return -ENOMEM;
14500 
14501 	limits[0].max = 1;
14502 	limits[0].types |= BIT(NL80211_IFTYPE_STATION);
14503 
14504 	if (ap || mesh || p2p)
14505 		limits[1].max = max_interfaces;
14506 
14507 	if (ap)
14508 		limits[1].types |= BIT(NL80211_IFTYPE_AP);
14509 
14510 	if (mesh)
14511 		limits[1].types |= BIT(NL80211_IFTYPE_MESH_POINT);
14512 
14513 	if (p2p) {
14514 		limits[1].types |= BIT(NL80211_IFTYPE_P2P_CLIENT) |
14515 					BIT(NL80211_IFTYPE_P2P_GO);
14516 		limits[2].max = 1;
14517 		limits[2].types |= BIT(NL80211_IFTYPE_P2P_DEVICE);
14518 	}
14519 
14520 	comb[0].limits = limits;
14521 	comb[0].n_limits = n_limits;
14522 	comb[0].max_interfaces = max_interfaces;
14523 	comb[0].beacon_int_infra_match = true;
14524 	comb[0].beacon_int_min_gcd = 100;
14525 
14526 	comb[0].num_different_channels = 1;
14527 	comb[0].radar_detect_widths = BIT(NL80211_CHAN_WIDTH_20_NOHT) |
14528 				      BIT(NL80211_CHAN_WIDTH_20) |
14529 				      BIT(NL80211_CHAN_WIDTH_40) |
14530 				      BIT(NL80211_CHAN_WIDTH_80) |
14531 				      BIT(NL80211_CHAN_WIDTH_160);
14532 
14533 	return 0;
14534 }
14535 
14536 static int
14537 ath12k_mac_setup_global_iface_comb(struct ath12k_hw *ah,
14538 				   struct wiphy_radio *radio,
14539 				   u8 n_radio,
14540 				   struct ieee80211_iface_combination *comb)
14541 {
14542 	const struct ieee80211_iface_combination *iter_comb;
14543 	struct ieee80211_iface_limit *limits;
14544 	int i, j, n_limits;
14545 	bool ap, mesh, p2p;
14546 
14547 	if (!n_radio)
14548 		return 0;
14549 
14550 	ap = ath12k_mac_is_iface_mode_enable(ah, NL80211_IFTYPE_AP);
14551 	p2p = ath12k_mac_is_iface_mode_enable(ah, NL80211_IFTYPE_P2P_DEVICE);
14552 	mesh = ath12k_mac_is_iface_mode_enable(ah, NL80211_IFTYPE_MESH_POINT);
14553 
14554 	if ((ap || mesh) && !p2p)
14555 		n_limits = 2;
14556 	else if (p2p)
14557 		n_limits = 3;
14558 	else
14559 		n_limits = 1;
14560 
14561 	limits = kzalloc_objs(*limits, n_limits);
14562 	if (!limits)
14563 		return -ENOMEM;
14564 
14565 	for (i = 0; i < n_radio; i++) {
14566 		iter_comb = radio[i].iface_combinations;
14567 		for (j = 0; j < iter_comb->n_limits && j < n_limits; j++) {
14568 			limits[j].types |= iter_comb->limits[j].types;
14569 			limits[j].max += iter_comb->limits[j].max;
14570 		}
14571 
14572 		comb->max_interfaces += iter_comb->max_interfaces;
14573 		comb->num_different_channels += iter_comb->num_different_channels;
14574 		comb->radar_detect_widths |= iter_comb->radar_detect_widths;
14575 	}
14576 
14577 	comb->limits = limits;
14578 	comb->n_limits = n_limits;
14579 	comb->beacon_int_infra_match = true;
14580 	comb->beacon_int_min_gcd = 100;
14581 
14582 	return 0;
14583 }
14584 
14585 static
14586 void ath12k_mac_cleanup_iface_comb(const struct ieee80211_iface_combination *iface_comb)
14587 {
14588 	kfree(iface_comb[0].limits);
14589 	kfree(iface_comb);
14590 }
14591 
14592 static void ath12k_mac_cleanup_iface_combinations(struct ath12k_hw *ah)
14593 {
14594 	struct wiphy *wiphy = ah->hw->wiphy;
14595 	const struct wiphy_radio *radio;
14596 	int i;
14597 
14598 	if (wiphy->n_radio > 0) {
14599 		radio = wiphy->radio;
14600 		for (i = 0; i < wiphy->n_radio; i++)
14601 			ath12k_mac_cleanup_iface_comb(radio[i].iface_combinations);
14602 
14603 		kfree(wiphy->radio);
14604 	}
14605 
14606 	ath12k_mac_cleanup_iface_comb(wiphy->iface_combinations);
14607 }
14608 
14609 static int ath12k_mac_setup_iface_combinations(struct ath12k_hw *ah)
14610 {
14611 	struct ieee80211_iface_combination *combinations, *comb;
14612 	struct wiphy *wiphy = ah->hw->wiphy;
14613 	struct wiphy_radio *radio;
14614 	int n_combinations = 1;
14615 	struct ath12k *ar;
14616 	int i, ret;
14617 
14618 	if (ah->num_radio == 1) {
14619 		ar = &ah->radio[0];
14620 
14621 		if (ar->ab->hw_params->single_pdev_only)
14622 			n_combinations = 2;
14623 
14624 		combinations = kzalloc_objs(*combinations, n_combinations);
14625 		if (!combinations)
14626 			return -ENOMEM;
14627 
14628 		ret = ath12k_mac_setup_radio_iface_comb(ar, combinations);
14629 		if (ret) {
14630 			ath12k_hw_warn(ah, "failed to setup radio interface combinations for one radio: %d",
14631 				       ret);
14632 			goto err_free_combinations;
14633 		}
14634 
14635 		if (ar->ab->hw_params->single_pdev_only) {
14636 			comb = combinations + 1;
14637 			memcpy(comb, combinations, sizeof(*comb));
14638 			comb->num_different_channels = 2;
14639 			comb->radar_detect_widths = 0;
14640 		}
14641 
14642 		goto out;
14643 	}
14644 
14645 	combinations = kzalloc_objs(*combinations, n_combinations);
14646 	if (!combinations)
14647 		return -ENOMEM;
14648 
14649 	/* there are multiple radios */
14650 
14651 	radio = kzalloc_objs(*radio, ah->num_radio);
14652 	if (!radio) {
14653 		ret = -ENOMEM;
14654 		goto err_free_combinations;
14655 	}
14656 
14657 	for_each_ar(ah, ar, i) {
14658 		comb = kzalloc_obj(*comb);
14659 		if (!comb) {
14660 			ret = -ENOMEM;
14661 			goto err_free_radios;
14662 		}
14663 
14664 		ret = ath12k_mac_setup_radio_iface_comb(ar, comb);
14665 		if (ret) {
14666 			ath12k_hw_warn(ah, "failed to setup radio interface combinations for radio %d: %d",
14667 				       i, ret);
14668 			kfree(comb);
14669 			goto err_free_radios;
14670 		}
14671 
14672 		radio[i].freq_range = &ar->freq_range;
14673 		radio[i].n_freq_range = 1;
14674 
14675 		radio[i].iface_combinations = comb;
14676 		radio[i].n_iface_combinations = 1;
14677 	}
14678 
14679 	ret = ath12k_mac_setup_global_iface_comb(ah, radio, ah->num_radio, combinations);
14680 	if (ret) {
14681 		ath12k_hw_warn(ah, "failed to setup global interface combinations: %d",
14682 			       ret);
14683 		goto err_free_all_radios;
14684 	}
14685 
14686 	wiphy->radio = radio;
14687 	wiphy->n_radio = ah->num_radio;
14688 
14689 out:
14690 	wiphy->iface_combinations = combinations;
14691 	wiphy->n_iface_combinations = n_combinations;
14692 
14693 	return 0;
14694 
14695 err_free_all_radios:
14696 	i = ah->num_radio;
14697 
14698 err_free_radios:
14699 	while (i--)
14700 		ath12k_mac_cleanup_iface_comb(radio[i].iface_combinations);
14701 
14702 	kfree(radio);
14703 
14704 err_free_combinations:
14705 	kfree(combinations);
14706 
14707 	return ret;
14708 }
14709 
14710 static const u8 ath12k_if_types_ext_capa[] = {
14711 	[0] = WLAN_EXT_CAPA1_EXT_CHANNEL_SWITCHING,
14712 	[2] = WLAN_EXT_CAPA3_MULTI_BSSID_SUPPORT,
14713 	[7] = WLAN_EXT_CAPA8_OPMODE_NOTIF,
14714 };
14715 
14716 static const u8 ath12k_if_types_ext_capa_sta[] = {
14717 	[0] = WLAN_EXT_CAPA1_EXT_CHANNEL_SWITCHING,
14718 	[2] = WLAN_EXT_CAPA3_MULTI_BSSID_SUPPORT,
14719 	[7] = WLAN_EXT_CAPA8_OPMODE_NOTIF,
14720 	[9] = WLAN_EXT_CAPA10_TWT_REQUESTER_SUPPORT,
14721 };
14722 
14723 static const u8 ath12k_if_types_ext_capa_ap[] = {
14724 	[0] = WLAN_EXT_CAPA1_EXT_CHANNEL_SWITCHING,
14725 	[2] = WLAN_EXT_CAPA3_MULTI_BSSID_SUPPORT,
14726 	[7] = WLAN_EXT_CAPA8_OPMODE_NOTIF,
14727 	[9] = WLAN_EXT_CAPA10_TWT_RESPONDER_SUPPORT,
14728 	[10] = WLAN_EXT_CAPA11_EMA_SUPPORT,
14729 };
14730 
14731 static struct wiphy_iftype_ext_capab ath12k_iftypes_ext_capa[] = {
14732 	{
14733 		.extended_capabilities = ath12k_if_types_ext_capa,
14734 		.extended_capabilities_mask = ath12k_if_types_ext_capa,
14735 		.extended_capabilities_len = sizeof(ath12k_if_types_ext_capa),
14736 	}, {
14737 		.iftype = NL80211_IFTYPE_STATION,
14738 		.extended_capabilities = ath12k_if_types_ext_capa_sta,
14739 		.extended_capabilities_mask = ath12k_if_types_ext_capa_sta,
14740 		.extended_capabilities_len =
14741 				sizeof(ath12k_if_types_ext_capa_sta),
14742 	}, {
14743 		.iftype = NL80211_IFTYPE_AP,
14744 		.extended_capabilities = ath12k_if_types_ext_capa_ap,
14745 		.extended_capabilities_mask = ath12k_if_types_ext_capa_ap,
14746 		.extended_capabilities_len =
14747 				sizeof(ath12k_if_types_ext_capa_ap),
14748 		.eml_capabilities = 0,
14749 		.mld_capa_and_ops = 0,
14750 	},
14751 };
14752 
14753 static void ath12k_mac_cleanup_unregister(struct ath12k *ar)
14754 {
14755 	ath12k_mac_tx_mgmt_pending_free(ar);
14756 	idr_destroy(&ar->txmgmt_idr);
14757 
14758 	kfree(ar->mac.sbands[NL80211_BAND_2GHZ].channels);
14759 	kfree(ar->mac.sbands[NL80211_BAND_5GHZ].channels);
14760 	kfree(ar->mac.sbands[NL80211_BAND_6GHZ].channels);
14761 }
14762 
14763 static void ath12k_mac_cleanup_mac_address_list(struct ath12k_hw *ah)
14764 {
14765 	kfree(ah->hw->wiphy->addresses);
14766 	ah->hw->wiphy->addresses = NULL;
14767 	ah->hw->wiphy->n_addresses = 0;
14768 }
14769 
14770 static void ath12k_mac_setup_mac_address_list(struct ath12k_hw *ah,
14771 					      const u8 *mac_addr)
14772 {
14773 	struct mac_address *addresses;
14774 	struct ath12k *ar;
14775 	u16 n_addresses;
14776 	int i;
14777 
14778 	/*
14779 	 * wiphy->addresses is per wiphy, so the generated interface MAC
14780 	 * pool is advertised for the whole ath12k_hw. The hw_params flag is
14781 	 * expected to be consistent across all radios in an ath12k_hw, so
14782 	 * use the first radio as the source.
14783 	 */
14784 	ar = ath12k_ah_to_ar(ah, 0);
14785 	if (!ar->ab->hw_params->advertise_iface_mac_pool)
14786 		return;
14787 
14788 	/*
14789 	 * Only the upper nibble of the first octet is varied, so at most 16
14790 	 * addresses can be derived from one base.
14791 	 */
14792 	n_addresses = min_t(u16, TARGET_NUM_VDEVS(ar->ab), 16);
14793 	if (n_addresses <= 1)
14794 		return;
14795 
14796 	addresses = kzalloc_objs(*addresses, n_addresses);
14797 	if (!addresses)
14798 		return;
14799 
14800 	ether_addr_copy(addresses[0].addr, mac_addr);
14801 	for (i = 1; i < n_addresses; i++) {
14802 		ether_addr_copy(addresses[i].addr, mac_addr);
14803 		/* set Local Administered Address bit */
14804 		addresses[i].addr[0] |= 0x2;
14805 
14806 		addresses[i].addr[0] += i << 4;
14807 	}
14808 
14809 	ah->hw->wiphy->addresses = addresses;
14810 	ah->hw->wiphy->n_addresses = n_addresses;
14811 }
14812 
14813 static void ath12k_mac_hw_unregister(struct ath12k_hw *ah)
14814 {
14815 	struct ieee80211_hw *hw = ah->hw;
14816 	struct ath12k *ar;
14817 	int i;
14818 
14819 	for_each_ar(ah, ar, i) {
14820 		cancel_work_sync(&ar->regd_channel_update_work);
14821 		cancel_work_sync(&ar->regd_update_work);
14822 		ath12k_debugfs_unregister(ar);
14823 		ath12k_fw_stats_reset(ar);
14824 	}
14825 
14826 	ieee80211_unregister_hw(hw);
14827 
14828 	for_each_ar(ah, ar, i)
14829 		ath12k_mac_cleanup_unregister(ar);
14830 
14831 	ath12k_mac_cleanup_iface_combinations(ah);
14832 	ath12k_mac_cleanup_mac_address_list(ah);
14833 
14834 	SET_IEEE80211_DEV(hw, NULL);
14835 }
14836 
14837 static int ath12k_mac_setup_register(struct ath12k *ar,
14838 				     u32 *ht_cap,
14839 				     struct ieee80211_supported_band *bands[])
14840 {
14841 	struct ath12k_pdev_cap *cap = &ar->pdev->cap;
14842 	int ret;
14843 
14844 	init_waitqueue_head(&ar->txmgmt_empty_waitq);
14845 	idr_init(&ar->txmgmt_idr);
14846 	spin_lock_init(&ar->txmgmt_idr_lock);
14847 
14848 	ath12k_pdev_caps_update(ar);
14849 
14850 	ret = ath12k_mac_setup_channels_rates(ar,
14851 					      cap->supported_bands,
14852 					      bands);
14853 	if (ret)
14854 		return ret;
14855 
14856 	ath12k_mac_setup_ht_vht_cap(ar, cap, ht_cap);
14857 	ath12k_mac_setup_sband_iftype_data(ar, cap);
14858 
14859 	ar->max_num_stations = ath12k_core_get_max_station_per_radio(ar->ab);
14860 	ar->max_num_peers = ath12k_core_get_max_peers_per_radio(ar->ab);
14861 
14862 	ar->rssi_info.min_nf_dbm = ATH12K_DEFAULT_NOISE_FLOOR;
14863 	ar->rssi_info.temp_offset = 0;
14864 	ar->rssi_info.noise_floor = ar->rssi_info.min_nf_dbm + ar->rssi_info.temp_offset;
14865 
14866 	ath12k_thermal_init_configs(ar);
14867 
14868 	return 0;
14869 }
14870 
14871 static int ath12k_mac_hw_register(struct ath12k_hw *ah)
14872 {
14873 	struct ieee80211_hw *hw = ah->hw;
14874 	struct wiphy *wiphy = hw->wiphy;
14875 	struct ath12k *ar = ath12k_ah_to_ar(ah, 0);
14876 	struct ath12k_base *ab = ar->ab;
14877 	struct ath12k_pdev *pdev;
14878 	struct ath12k_pdev_cap *cap;
14879 	static const u32 cipher_suites[] = {
14880 		WLAN_CIPHER_SUITE_TKIP,
14881 		WLAN_CIPHER_SUITE_CCMP,
14882 		WLAN_CIPHER_SUITE_AES_CMAC,
14883 		WLAN_CIPHER_SUITE_BIP_CMAC_256,
14884 		WLAN_CIPHER_SUITE_BIP_GMAC_128,
14885 		WLAN_CIPHER_SUITE_BIP_GMAC_256,
14886 		WLAN_CIPHER_SUITE_GCMP,
14887 		WLAN_CIPHER_SUITE_GCMP_256,
14888 		WLAN_CIPHER_SUITE_CCMP_256,
14889 	};
14890 	int ret, i, j;
14891 	u32 ht_cap = U32_MAX, antennas_rx = 0, antennas_tx = 0;
14892 	bool is_6ghz = false, is_raw_mode = false, is_monitor_disable = false;
14893 	u8 *mac_addr = NULL;
14894 	u8 mbssid_max_interfaces = 0;
14895 
14896 	wiphy->max_ap_assoc_sta = 0;
14897 
14898 	for_each_ar(ah, ar, i) {
14899 		u32 ht_cap_info = 0;
14900 
14901 		pdev = ar->pdev;
14902 		if (ar->ab->pdevs_macaddr_valid) {
14903 			ether_addr_copy(ar->mac_addr, pdev->mac_addr);
14904 		} else {
14905 			ether_addr_copy(ar->mac_addr, ar->ab->mac_addr);
14906 			ar->mac_addr[4] += ar->pdev_idx;
14907 		}
14908 
14909 		ret = ath12k_mac_setup_register(ar, &ht_cap_info, hw->wiphy->bands);
14910 		if (ret)
14911 			goto err_cleanup_unregister;
14912 
14913 		/* 6 GHz does not support HT Cap, hence do not consider it */
14914 		if (!ar->supports_6ghz)
14915 			ht_cap &= ht_cap_info;
14916 
14917 		wiphy->max_ap_assoc_sta += ar->max_num_stations;
14918 
14919 		/* Advertise the max antenna support of all radios, driver can handle
14920 		 * per pdev specific antenna setting based on pdev cap when antenna
14921 		 * changes are made
14922 		 */
14923 		cap = &pdev->cap;
14924 
14925 		antennas_rx = max_t(u32, antennas_rx, cap->rx_chain_mask);
14926 		antennas_tx = max_t(u32, antennas_tx, cap->tx_chain_mask);
14927 
14928 		if (ar->supports_6ghz)
14929 			is_6ghz = true;
14930 
14931 		if (test_bit(ATH12K_FLAG_RAW_MODE, &ar->ab->dev_flags))
14932 			is_raw_mode = true;
14933 
14934 		if (!ar->ab->hw_params->supports_monitor)
14935 			is_monitor_disable = true;
14936 
14937 		if (i == 0)
14938 			mac_addr = ar->mac_addr;
14939 		else
14940 			mac_addr = ab->mac_addr;
14941 
14942 		mbssid_max_interfaces += TARGET_NUM_VDEVS(ar->ab);
14943 	}
14944 
14945 	wiphy->available_antennas_rx = antennas_rx;
14946 	wiphy->available_antennas_tx = antennas_tx;
14947 
14948 	SET_IEEE80211_PERM_ADDR(hw, mac_addr);
14949 	ath12k_mac_setup_mac_address_list(ah, mac_addr);
14950 	SET_IEEE80211_DEV(hw, ab->dev);
14951 
14952 	ret = ath12k_mac_setup_iface_combinations(ah);
14953 	if (ret) {
14954 		ath12k_err(ab, "failed to setup interface combinations: %d\n", ret);
14955 		goto err_complete_cleanup_unregister;
14956 	}
14957 
14958 	wiphy->interface_modes = ath12k_mac_get_ifmodes(ah);
14959 
14960 	if (ah->num_radio == 1 &&
14961 	    wiphy->bands[NL80211_BAND_2GHZ] &&
14962 	    wiphy->bands[NL80211_BAND_5GHZ] &&
14963 	    wiphy->bands[NL80211_BAND_6GHZ])
14964 		ieee80211_hw_set(hw, SINGLE_SCAN_ON_ALL_BANDS);
14965 
14966 	ieee80211_hw_set(hw, SIGNAL_DBM);
14967 	ieee80211_hw_set(hw, SUPPORTS_PS);
14968 	ieee80211_hw_set(hw, SUPPORTS_DYNAMIC_PS);
14969 	ieee80211_hw_set(hw, MFP_CAPABLE);
14970 	ieee80211_hw_set(hw, REPORTS_TX_ACK_STATUS);
14971 	ieee80211_hw_set(hw, HAS_RATE_CONTROL);
14972 	ieee80211_hw_set(hw, AP_LINK_PS);
14973 	ieee80211_hw_set(hw, SPECTRUM_MGMT);
14974 	ieee80211_hw_set(hw, CONNECTION_MONITOR);
14975 	ieee80211_hw_set(hw, SUPPORTS_PER_STA_GTK);
14976 	ieee80211_hw_set(hw, CHANCTX_STA_CSA);
14977 	ieee80211_hw_set(hw, QUEUE_CONTROL);
14978 	ieee80211_hw_set(hw, SUPPORTS_TX_FRAG);
14979 	ieee80211_hw_set(hw, REPORTS_LOW_ACK);
14980 	ieee80211_hw_set(hw, NO_VIRTUAL_MONITOR);
14981 
14982 	if (test_bit(WMI_TLV_SERVICE_ETH_OFFLOAD, ar->wmi->wmi_ab->svc_map)) {
14983 		ieee80211_hw_set(hw, SUPPORTS_TX_ENCAP_OFFLOAD);
14984 		ieee80211_hw_set(hw, SUPPORTS_RX_DECAP_OFFLOAD);
14985 	}
14986 
14987 	if (cap->nss_ratio_enabled)
14988 		ieee80211_hw_set(hw, SUPPORTS_VHT_EXT_NSS_BW);
14989 
14990 	if ((ht_cap & WMI_HT_CAP_ENABLED) || is_6ghz) {
14991 		ieee80211_hw_set(hw, AMPDU_AGGREGATION);
14992 		ieee80211_hw_set(hw, TX_AMPDU_SETUP_IN_HW);
14993 		ieee80211_hw_set(hw, SUPPORTS_REORDERING_BUFFER);
14994 		ieee80211_hw_set(hw, SUPPORTS_AMSDU_IN_AMPDU);
14995 		ieee80211_hw_set(hw, USES_RSS);
14996 	}
14997 
14998 	wiphy->features |= NL80211_FEATURE_STATIC_SMPS;
14999 	wiphy->flags |= WIPHY_FLAG_IBSS_RSN;
15000 
15001 	/* TODO: Check if HT capability advertised from firmware is different
15002 	 * for each band for a dual band capable radio. It will be tricky to
15003 	 * handle it when the ht capability different for each band.
15004 	 */
15005 	if (ht_cap & WMI_HT_CAP_DYNAMIC_SMPS ||
15006 	    (is_6ghz && ab->hw_params->supports_dynamic_smps_6ghz))
15007 		wiphy->features |= NL80211_FEATURE_DYNAMIC_SMPS;
15008 
15009 	wiphy->max_scan_ssids = WLAN_SCAN_PARAMS_MAX_SSID;
15010 	wiphy->max_scan_ie_len = WLAN_SCAN_PARAMS_MAX_IE_LEN;
15011 
15012 	hw->max_listen_interval = ATH12K_MAX_HW_LISTEN_INTERVAL;
15013 
15014 	wiphy->flags |= WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL;
15015 	wiphy->flags |= WIPHY_FLAG_HAS_CHANNEL_SWITCH;
15016 	wiphy->max_remain_on_channel_duration = 5000;
15017 
15018 	wiphy->flags |= WIPHY_FLAG_AP_UAPSD;
15019 	wiphy->features |= NL80211_FEATURE_AP_MODE_CHAN_WIDTH_CHANGE |
15020 				   NL80211_FEATURE_AP_SCAN;
15021 
15022 	wiphy->features |= NL80211_FEATURE_TX_POWER_INSERTION;
15023 
15024 	/* Copy over MLO related capabilities received from
15025 	 * WMI_SERVICE_READY_EXT2_EVENT if single_chip_mlo_supp is set.
15026 	 */
15027 	if (ab->ag->mlo_capable) {
15028 		ath12k_iftypes_ext_capa[2].eml_capabilities = cap->eml_cap;
15029 		ath12k_iftypes_ext_capa[2].mld_capa_and_ops = cap->mld_cap;
15030 		wiphy->flags |= WIPHY_FLAG_SUPPORTS_MLO;
15031 
15032 		ieee80211_hw_set(hw, MLO_MCAST_MULTI_LINK_TX);
15033 	}
15034 
15035 	hw->queues = ATH12K_HW_MAX_QUEUES;
15036 	wiphy->tx_queue_len = ATH12K_QUEUE_LEN;
15037 	hw->offchannel_tx_hw_queue = ATH12K_HW_MAX_QUEUES - 1;
15038 	hw->max_rx_aggregation_subframes = IEEE80211_MAX_AMPDU_BUF_EHT;
15039 
15040 	hw->vif_data_size = sizeof(struct ath12k_vif);
15041 	hw->sta_data_size = sizeof(struct ath12k_sta);
15042 	hw->extra_tx_headroom = ab->hw_params->iova_mask;
15043 
15044 	wiphy_ext_feature_set(wiphy, NL80211_EXT_FEATURE_CQM_RSSI_LIST);
15045 	wiphy_ext_feature_set(wiphy, NL80211_EXT_FEATURE_STA_TX_PWR);
15046 	wiphy_ext_feature_set(wiphy, NL80211_EXT_FEATURE_ACK_SIGNAL_SUPPORT);
15047 	if (test_bit(WMI_TLV_SERVICE_BSS_COLOR_OFFLOAD,
15048 		     ab->wmi_ab.svc_map)) {
15049 		wiphy_ext_feature_set(wiphy, NL80211_EXT_FEATURE_BSS_COLOR);
15050 		ieee80211_hw_set(hw, DETECTS_COLOR_COLLISION);
15051 	}
15052 
15053 	wiphy->cipher_suites = cipher_suites;
15054 	wiphy->n_cipher_suites = ARRAY_SIZE(cipher_suites);
15055 
15056 	wiphy->iftype_ext_capab = ath12k_iftypes_ext_capa;
15057 	wiphy->num_iftype_ext_capab = ARRAY_SIZE(ath12k_iftypes_ext_capa);
15058 
15059 	wiphy->mbssid_max_interfaces = mbssid_max_interfaces;
15060 	wiphy->ema_max_profile_periodicity = TARGET_EMA_MAX_PROFILE_PERIOD;
15061 	ieee80211_hw_set(hw, SUPPORTS_MULTI_BSSID);
15062 
15063 	if (is_6ghz) {
15064 		wiphy_ext_feature_set(wiphy,
15065 				      NL80211_EXT_FEATURE_FILS_DISCOVERY);
15066 		wiphy_ext_feature_set(wiphy,
15067 				      NL80211_EXT_FEATURE_UNSOL_BCAST_PROBE_RESP);
15068 	}
15069 
15070 	wiphy_ext_feature_set(wiphy, NL80211_EXT_FEATURE_PUNCT);
15071 	if (test_bit(WMI_TLV_SERVICE_BEACON_PROTECTION_SUPPORT, ab->wmi_ab.svc_map))
15072 		wiphy_ext_feature_set(hw->wiphy, NL80211_EXT_FEATURE_BEACON_PROTECTION);
15073 
15074 	ath12k_reg_init(hw);
15075 
15076 	if (!is_raw_mode) {
15077 		hw->netdev_features = NETIF_F_HW_CSUM;
15078 		ieee80211_hw_set(hw, SW_CRYPTO_CONTROL);
15079 		ieee80211_hw_set(hw, SUPPORT_FAST_XMIT);
15080 	}
15081 
15082 	if (test_bit(WMI_TLV_SERVICE_NLO, ar->wmi->wmi_ab->svc_map)) {
15083 		wiphy->max_sched_scan_ssids = WMI_PNO_MAX_SUPP_NETWORKS;
15084 		wiphy->max_match_sets = WMI_PNO_MAX_SUPP_NETWORKS;
15085 		wiphy->max_sched_scan_ie_len = WMI_PNO_MAX_IE_LENGTH;
15086 		wiphy->max_sched_scan_plans = WMI_PNO_MAX_SCHED_SCAN_PLANS;
15087 		wiphy->max_sched_scan_plan_interval =
15088 					WMI_PNO_MAX_SCHED_SCAN_PLAN_INT;
15089 		wiphy->max_sched_scan_plan_iterations =
15090 					WMI_PNO_MAX_SCHED_SCAN_PLAN_ITRNS;
15091 		wiphy->features |= NL80211_FEATURE_ND_RANDOM_MAC_ADDR;
15092 	}
15093 
15094 	ret = ath12k_wow_init(ar);
15095 	if (ret) {
15096 		ath12k_warn(ar->ab, "failed to init wow: %d\n", ret);
15097 		goto err_cleanup_if_combs;
15098 	}
15099 
15100 	/* Boot-time regulatory updates have already been processed.
15101 	 * Mark them as complete now, because after registration,
15102 	 * cfg80211 will notify us again if there are any pending hints.
15103 	 * We need to wait for those hints to be processed, so it's
15104 	 * important to mark the boot-time updates as complete before
15105 	 * proceeding with registration.
15106 	 */
15107 	for_each_ar(ah, ar, i)
15108 		complete_all(&ar->regd_update_completed);
15109 
15110 	ret = ieee80211_register_hw(hw);
15111 	if (ret) {
15112 		ath12k_err(ab, "ieee80211 registration failed: %d\n", ret);
15113 		goto err_cleanup_if_combs;
15114 	}
15115 
15116 	if (is_monitor_disable)
15117 		/* There's a race between calling ieee80211_register_hw()
15118 		 * and here where the monitor mode is enabled for a little
15119 		 * while. But that time is so short and in practice it doesn't make
15120 		 * a difference in real life.
15121 		 */
15122 		wiphy->interface_modes &= ~BIT(NL80211_IFTYPE_MONITOR);
15123 
15124 	for_each_ar(ah, ar, i) {
15125 		struct ath12k_base *this_ab = ar->ab;
15126 
15127 		/* Apply the regd received during initialization */
15128 		ret = ath12k_regd_update(ar, true);
15129 		if (ret) {
15130 			ath12k_err(this_ab, "ath12k regd update failed: %d\n", ret);
15131 			goto err_unregister_hw;
15132 		}
15133 
15134 		if (this_ab->hw_params->current_cc_support) {
15135 			struct wmi_set_current_country_arg current_cc = {};
15136 			struct ieee80211_regdomain *default_regd;
15137 			bool same_cc = false;
15138 
15139 			spin_lock_bh(&this_ab->base_lock);
15140 			memcpy(&current_cc.alpha2, this_ab->new_alpha2, 2);
15141 			spin_unlock_bh(&this_ab->base_lock);
15142 
15143 			if (!current_cc.alpha2[0])
15144 				goto fw_stats_init;
15145 
15146 			memcpy(&ar->alpha2, current_cc.alpha2, 2);
15147 
15148 			spin_lock_bh(&this_ab->base_lock);
15149 			default_regd = this_ab->default_regd[ar->pdev_idx];
15150 			if (default_regd)
15151 				same_cc = !memcmp(default_regd->alpha2,
15152 						  current_cc.alpha2, 2);
15153 			spin_unlock_bh(&this_ab->base_lock);
15154 
15155 			if (same_cc)
15156 				goto fw_stats_init;
15157 
15158 			reinit_completion(&ar->regd_update_completed);
15159 
15160 			ret = ath12k_wmi_send_set_current_country_cmd(ar, &current_cc);
15161 			if (ret)
15162 				ath12k_warn(this_ab,
15163 					    "failed set cc code for mac register: %d\n",
15164 					    ret);
15165 		}
15166 
15167 fw_stats_init:
15168 		ath12k_fw_stats_init(ar);
15169 		ath12k_debugfs_register(ar);
15170 	}
15171 
15172 	return 0;
15173 
15174 err_unregister_hw:
15175 	for_each_ar(ah, ar, i)
15176 		ath12k_debugfs_unregister(ar);
15177 
15178 	ieee80211_unregister_hw(hw);
15179 
15180 err_cleanup_if_combs:
15181 	ath12k_mac_cleanup_iface_combinations(ah);
15182 
15183 err_complete_cleanup_unregister:
15184 	i = ah->num_radio;
15185 
15186 err_cleanup_unregister:
15187 	for (j = 0; j < i; j++) {
15188 		ar = ath12k_ah_to_ar(ah, j);
15189 		ath12k_mac_cleanup_unregister(ar);
15190 	}
15191 
15192 	ath12k_mac_cleanup_mac_address_list(ah);
15193 
15194 	SET_IEEE80211_DEV(hw, NULL);
15195 
15196 	return ret;
15197 }
15198 
15199 static void ath12k_mac_setup(struct ath12k *ar)
15200 {
15201 	struct ath12k_base *ab = ar->ab;
15202 	struct ath12k_pdev *pdev = ar->pdev;
15203 	u8 pdev_idx = ar->pdev_idx;
15204 
15205 	ar->lmac_id = ath12k_hw_get_mac_from_pdev_id(ab->hw_params, pdev_idx);
15206 
15207 	ar->wmi = &ab->wmi_ab.wmi[pdev_idx];
15208 	/* FIXME: wmi[0] is already initialized during attach,
15209 	 * Should we do this again?
15210 	 */
15211 	ath12k_wmi_pdev_attach(ab, pdev_idx);
15212 
15213 	ar->cfg_tx_chainmask = pdev->cap.tx_chain_mask;
15214 	ar->cfg_rx_chainmask = pdev->cap.rx_chain_mask;
15215 	ar->num_tx_chains = hweight32(pdev->cap.tx_chain_mask);
15216 	ar->num_rx_chains = hweight32(pdev->cap.rx_chain_mask);
15217 	ar->scan.arvif = NULL;
15218 	ar->vdev_id_11d_scan = ATH12K_11D_INVALID_VDEV_ID;
15219 
15220 	spin_lock_init(&ar->data_lock);
15221 	spin_lock_init(&ar->dp.ppdu_list_lock);
15222 	INIT_LIST_HEAD(&ar->arvifs);
15223 	INIT_LIST_HEAD(&ar->dp.ppdu_stats_info);
15224 	INIT_LIST_HEAD(&ar->peer_delete_waits);
15225 
15226 	init_completion(&ar->vdev_setup_done);
15227 	init_completion(&ar->vdev_delete_done);
15228 	init_completion(&ar->peer_assoc_done);
15229 	init_completion(&ar->install_key_done);
15230 	init_completion(&ar->bss_survey_done);
15231 	init_completion(&ar->scan.started);
15232 	init_completion(&ar->scan.completed);
15233 	init_completion(&ar->scan.on_channel);
15234 	init_completion(&ar->mlo_setup_done);
15235 	init_completion(&ar->completed_11d_scan);
15236 	init_completion(&ar->regd_update_completed);
15237 	init_completion(&ar->thermal.wmi_sync);
15238 	mutex_init(&ar->thermal.lock);
15239 
15240 	ar->thermal.temperature = 0;
15241 	ar->thermal.hwmon_dev = NULL;
15242 
15243 	INIT_DELAYED_WORK(&ar->scan.timeout, ath12k_scan_timeout_work);
15244 	wiphy_work_init(&ar->scan.vdev_clean_wk, ath12k_scan_vdev_clean_work);
15245 	INIT_WORK(&ar->regd_channel_update_work, ath12k_regd_update_chan_list_work);
15246 	INIT_LIST_HEAD(&ar->regd_channel_update_queue);
15247 	INIT_WORK(&ar->regd_update_work, ath12k_regd_update_work);
15248 
15249 	wiphy_work_init(&ar->wmi_mgmt_tx_work, ath12k_mgmt_over_wmi_tx_work);
15250 	skb_queue_head_init(&ar->wmi_mgmt_tx_queue);
15251 
15252 	ar->monitor_vdev_id = -1;
15253 	ar->monitor_vdev_created = false;
15254 	ar->monitor_started = false;
15255 }
15256 
15257 static int __ath12k_mac_mlo_setup(struct ath12k *ar)
15258 {
15259 	u8 num_link = 0, partner_link_id[ATH12K_GROUP_MAX_RADIO] = {};
15260 	struct ath12k_base *partner_ab, *ab = ar->ab;
15261 	struct ath12k_hw_group *ag = ab->ag;
15262 	struct wmi_mlo_setup_arg mlo = {};
15263 	struct ath12k_pdev *pdev;
15264 	unsigned long time_left;
15265 	int i, j, ret;
15266 
15267 	lockdep_assert_held(&ag->mutex);
15268 
15269 	reinit_completion(&ar->mlo_setup_done);
15270 
15271 	for (i = 0; i < ag->num_devices; i++) {
15272 		partner_ab = ag->ab[i];
15273 
15274 		for (j = 0; j < partner_ab->num_radios; j++) {
15275 			pdev = &partner_ab->pdevs[j];
15276 
15277 			/* Avoid the self link */
15278 			if (ar == pdev->ar)
15279 				continue;
15280 
15281 			partner_link_id[num_link] = pdev->hw_link_id;
15282 			num_link++;
15283 
15284 			ath12k_dbg(ab, ATH12K_DBG_MAC, "device %d pdev %d hw_link_id %d num_link %d\n",
15285 				   i, j, pdev->hw_link_id, num_link);
15286 		}
15287 	}
15288 
15289 	if (num_link == 0)
15290 		return 0;
15291 
15292 	mlo.group_id = cpu_to_le32(ag->id);
15293 	mlo.partner_link_id = partner_link_id;
15294 	mlo.num_partner_links = num_link;
15295 	ar->mlo_setup_status = 0;
15296 
15297 	ath12k_dbg(ab, ATH12K_DBG_MAC, "group id %d num_link %d\n", ag->id, num_link);
15298 
15299 	ret = ath12k_wmi_mlo_setup(ar, &mlo);
15300 	if (ret) {
15301 		ath12k_err(ab, "failed to send  setup MLO WMI command for pdev %d: %d\n",
15302 			   ar->pdev_idx, ret);
15303 		return ret;
15304 	}
15305 
15306 	time_left = wait_for_completion_timeout(&ar->mlo_setup_done,
15307 						WMI_MLO_CMD_TIMEOUT_HZ);
15308 
15309 	if (!time_left || ar->mlo_setup_status)
15310 		return ar->mlo_setup_status ? : -ETIMEDOUT;
15311 
15312 	ath12k_dbg(ab, ATH12K_DBG_MAC, "mlo setup done for pdev %d\n", ar->pdev_idx);
15313 
15314 	return 0;
15315 }
15316 
15317 static int __ath12k_mac_mlo_teardown(struct ath12k *ar)
15318 {
15319 	struct ath12k_base *ab = ar->ab;
15320 	int ret;
15321 	u8 num_link;
15322 
15323 	if (test_bit(ATH12K_FLAG_RECOVERY, &ab->dev_flags))
15324 		return 0;
15325 
15326 	num_link = ath12k_get_num_partner_link(ar);
15327 
15328 	if (num_link == 0)
15329 		return 0;
15330 
15331 	ret = ath12k_wmi_mlo_teardown(ar);
15332 	if (ret) {
15333 		ath12k_warn(ab, "failed to send MLO teardown WMI command for pdev %d: %d\n",
15334 			    ar->pdev_idx, ret);
15335 		return ret;
15336 	}
15337 
15338 	ath12k_dbg(ab, ATH12K_DBG_MAC, "mlo teardown for pdev %d\n", ar->pdev_idx);
15339 
15340 	return 0;
15341 }
15342 
15343 int ath12k_mac_mlo_setup(struct ath12k_hw_group *ag)
15344 {
15345 	struct ath12k_hw *ah;
15346 	struct ath12k *ar;
15347 	int ret;
15348 	int i, j;
15349 
15350 	for (i = 0; i < ag->num_hw; i++) {
15351 		ah = ag->ah[i];
15352 		if (!ah)
15353 			continue;
15354 
15355 		for_each_ar(ah, ar, j) {
15356 			ar = &ah->radio[j];
15357 			ret = __ath12k_mac_mlo_setup(ar);
15358 			if (ret) {
15359 				ath12k_err(ar->ab, "failed to setup MLO: %d\n", ret);
15360 				goto err_setup;
15361 			}
15362 		}
15363 	}
15364 
15365 	return 0;
15366 
15367 err_setup:
15368 	for (i = i - 1; i >= 0; i--) {
15369 		ah = ag->ah[i];
15370 		if (!ah)
15371 			continue;
15372 
15373 		for (j = j - 1; j >= 0; j--) {
15374 			ar = &ah->radio[j];
15375 			if (!ar)
15376 				continue;
15377 
15378 			__ath12k_mac_mlo_teardown(ar);
15379 		}
15380 	}
15381 
15382 	return ret;
15383 }
15384 
15385 void ath12k_mac_mlo_teardown(struct ath12k_hw_group *ag)
15386 {
15387 	struct ath12k_hw *ah;
15388 	struct ath12k *ar;
15389 	int ret, i, j;
15390 
15391 	for (i = 0; i < ag->num_hw; i++) {
15392 		ah = ag->ah[i];
15393 		if (!ah)
15394 			continue;
15395 
15396 		for_each_ar(ah, ar, j) {
15397 			ar = &ah->radio[j];
15398 			ret = __ath12k_mac_mlo_teardown(ar);
15399 			if (ret) {
15400 				ath12k_err(ar->ab, "failed to teardown MLO: %d\n", ret);
15401 				break;
15402 			}
15403 		}
15404 	}
15405 }
15406 
15407 int ath12k_mac_register(struct ath12k_hw_group *ag)
15408 {
15409 	struct ath12k_hw *ah;
15410 	int i;
15411 	int ret;
15412 
15413 	for (i = 0; i < ag->num_hw; i++) {
15414 		ah = ath12k_ag_to_ah(ag, i);
15415 
15416 		ret = ath12k_mac_hw_register(ah);
15417 		if (ret)
15418 			goto err;
15419 	}
15420 
15421 	return 0;
15422 
15423 err:
15424 	for (i = i - 1; i >= 0; i--) {
15425 		ah = ath12k_ag_to_ah(ag, i);
15426 		if (!ah)
15427 			continue;
15428 
15429 		ath12k_mac_hw_unregister(ah);
15430 	}
15431 
15432 	return ret;
15433 }
15434 
15435 void ath12k_mac_unregister(struct ath12k_hw_group *ag)
15436 {
15437 	struct ath12k_hw *ah;
15438 	int i;
15439 
15440 	for (i = ag->num_hw - 1; i >= 0; i--) {
15441 		ah = ath12k_ag_to_ah(ag, i);
15442 		if (!ah)
15443 			continue;
15444 
15445 		ath12k_mac_hw_unregister(ah);
15446 	}
15447 }
15448 
15449 static void ath12k_mac_hw_destroy(struct ath12k_hw *ah)
15450 {
15451 	ieee80211_free_hw(ah->hw);
15452 }
15453 
15454 static struct ath12k_hw *ath12k_mac_hw_allocate(struct ath12k_hw_group *ag,
15455 						struct ath12k_pdev_map *pdev_map,
15456 						u8 num_pdev_map)
15457 {
15458 	struct ieee80211_hw *hw;
15459 	struct ath12k *ar;
15460 	struct ath12k_base *ab;
15461 	struct ath12k_pdev *pdev;
15462 	struct ath12k_hw *ah;
15463 	int i;
15464 	u8 pdev_idx;
15465 
15466 	hw = ieee80211_alloc_hw(struct_size(ah, radio, num_pdev_map),
15467 				pdev_map->ab->ath12k_ops);
15468 	if (!hw)
15469 		return NULL;
15470 
15471 	ah = ath12k_hw_to_ah(hw);
15472 	ah->hw = hw;
15473 	ah->num_radio = num_pdev_map;
15474 
15475 	mutex_init(&ah->hw_mutex);
15476 	init_completion(&ah->peer_ml_id_done);
15477 
15478 	spin_lock_init(&ah->survey_lock);
15479 	spin_lock_init(&ah->dp_hw.peer_lock);
15480 	INIT_LIST_HEAD(&ah->dp_hw.dp_peers_list);
15481 
15482 	for (i = 0; i < num_pdev_map; i++) {
15483 		ab = pdev_map[i].ab;
15484 		pdev_idx = pdev_map[i].pdev_idx;
15485 		pdev = &ab->pdevs[pdev_idx];
15486 
15487 		ar = ath12k_ah_to_ar(ah, i);
15488 		ar->ah = ah;
15489 		ar->ab = ab;
15490 		ar->hw_link_id = pdev->hw_link_id;
15491 		ar->pdev = pdev;
15492 		ar->pdev_idx = pdev_idx;
15493 		ar->radio_idx = i;
15494 		pdev->ar = ar;
15495 
15496 		ag->hw_links[ar->hw_link_id].device_id = ab->device_id;
15497 		ag->hw_links[ar->hw_link_id].pdev_idx = pdev_idx;
15498 
15499 		ath12k_mac_setup(ar);
15500 		ath12k_dp_pdev_pre_alloc(ar);
15501 	}
15502 
15503 	return ah;
15504 }
15505 
15506 void ath12k_mac_destroy(struct ath12k_hw_group *ag)
15507 {
15508 	struct ath12k_pdev *pdev;
15509 	struct ath12k_base *ab = ag->ab[0];
15510 	int i, j;
15511 	struct ath12k_hw *ah;
15512 
15513 	for (i = 0; i < ag->num_devices; i++) {
15514 		ab = ag->ab[i];
15515 		if (!ab)
15516 			continue;
15517 
15518 		for (j = 0; j < ab->num_radios; j++) {
15519 			pdev = &ab->pdevs[j];
15520 			if (!pdev->ar)
15521 				continue;
15522 			pdev->ar = NULL;
15523 		}
15524 	}
15525 
15526 	for (i = 0; i < ag->num_hw; i++) {
15527 		ah = ath12k_ag_to_ah(ag, i);
15528 		if (!ah)
15529 			continue;
15530 
15531 		ath12k_mac_hw_destroy(ah);
15532 		ath12k_ag_set_ah(ag, i, NULL);
15533 	}
15534 }
15535 
15536 static void ath12k_mac_set_device_defaults(struct ath12k_base *ab)
15537 {
15538 	int total_vdev;
15539 
15540 	/* Initialize channel counters frequency value in hertz */
15541 	ab->cc_freq_hz = 320000;
15542 	total_vdev = ab->num_radios * TARGET_NUM_VDEVS(ab);
15543 	ab->free_vdev_map = (1LL << total_vdev) - 1;
15544 }
15545 
15546 int ath12k_mac_allocate(struct ath12k_hw_group *ag)
15547 {
15548 	struct ath12k_pdev_map pdev_map[ATH12K_GROUP_MAX_RADIO];
15549 	int mac_id, device_id, total_radio, num_hw;
15550 	struct ath12k_base *ab;
15551 	struct ath12k_hw *ah;
15552 	bool conf = false;
15553 	u8 radio_per_hw;
15554 	int ret, i, j;
15555 
15556 	total_radio = 0;
15557 	for (i = 0; i < ag->num_devices; i++) {
15558 		ab = ag->ab[i];
15559 		if (!ab)
15560 			continue;
15561 
15562 		ath12k_mac_set_device_defaults(ab);
15563 		total_radio += ab->num_radios;
15564 	}
15565 
15566 	if (!total_radio)
15567 		return -EINVAL;
15568 
15569 	if (WARN_ON(total_radio > ATH12K_GROUP_MAX_RADIO))
15570 		return -ENOSPC;
15571 
15572 	/* All pdev get combined and register as single wiphy based on
15573 	 * hardware group which participate in multi-link operation else
15574 	 * each pdev get register separately.
15575 	 */
15576 	if (ag->mlo_capable)
15577 		radio_per_hw = total_radio;
15578 	else
15579 		radio_per_hw = 1;
15580 
15581 	num_hw = total_radio / radio_per_hw;
15582 
15583 	ag->num_hw = 0;
15584 	device_id = 0;
15585 	mac_id = 0;
15586 	for (i = 0; i < num_hw; i++) {
15587 		for (j = 0; j < radio_per_hw; j++) {
15588 			if (device_id >= ag->num_devices || !ag->ab[device_id]) {
15589 				ret = -ENOSPC;
15590 				goto err;
15591 			}
15592 
15593 			ab = ag->ab[device_id];
15594 
15595 			/*
15596 			 * the assumption is all devices within an ah
15597 			 * share the same host_alloc_ml_id configuration
15598 			 */
15599 			if (j == 0) {
15600 				conf = ab->hw_params->host_alloc_ml_id;
15601 			} else if (conf != ab->hw_params->host_alloc_ml_id) {
15602 				ath12k_warn(ab, "inconsistent ML ID config within ah, device 0 uses %s allocated ID, while device %u doesn't\n",
15603 					    conf ? "host" : "firmware", device_id);
15604 				ret = -EINVAL;
15605 				goto err;
15606 			}
15607 
15608 			pdev_map[j].ab = ab;
15609 			pdev_map[j].pdev_idx = mac_id;
15610 			mac_id++;
15611 
15612 			/* If mac_id falls beyond the current device MACs then
15613 			 * move to next device
15614 			 */
15615 			if (mac_id >= ab->num_radios) {
15616 				mac_id = 0;
15617 				device_id++;
15618 			}
15619 		}
15620 
15621 		ab = pdev_map->ab;
15622 
15623 		ah = ath12k_mac_hw_allocate(ag, pdev_map, radio_per_hw);
15624 		if (!ah) {
15625 			ath12k_warn(ab, "failed to allocate mac80211 hw device for hw_idx %d\n",
15626 				    i);
15627 			ret = -ENOMEM;
15628 			goto err;
15629 		}
15630 
15631 		ah->dev = ab->dev;
15632 		ah->host_alloc_ml_id = conf;
15633 
15634 		ag->ah[i] = ah;
15635 		ag->num_hw++;
15636 	}
15637 
15638 	return 0;
15639 
15640 err:
15641 	for (i = i - 1; i >= 0; i--) {
15642 		ah = ath12k_ag_to_ah(ag, i);
15643 		if (!ah)
15644 			continue;
15645 
15646 		ath12k_mac_hw_destroy(ah);
15647 		ath12k_ag_set_ah(ag, i, NULL);
15648 	}
15649 
15650 	return ret;
15651 }
15652 
15653 int ath12k_mac_vif_set_keepalive(struct ath12k_link_vif *arvif,
15654 				 enum wmi_sta_keepalive_method method,
15655 				 u32 interval)
15656 {
15657 	struct wmi_sta_keepalive_arg arg = {};
15658 	struct ath12k *ar = arvif->ar;
15659 	int ret;
15660 
15661 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
15662 
15663 	if (arvif->ahvif->vdev_type != WMI_VDEV_TYPE_STA)
15664 		return 0;
15665 
15666 	if (!test_bit(WMI_TLV_SERVICE_STA_KEEP_ALIVE, ar->ab->wmi_ab.svc_map))
15667 		return 0;
15668 
15669 	arg.vdev_id = arvif->vdev_id;
15670 	arg.enabled = 1;
15671 	arg.method = method;
15672 	arg.interval = interval;
15673 
15674 	ret = ath12k_wmi_sta_keepalive(ar, &arg);
15675 	if (ret) {
15676 		ath12k_warn(ar->ab, "failed to set keepalive on vdev %i: %d\n",
15677 			    arvif->vdev_id, ret);
15678 		return ret;
15679 	}
15680 
15681 	return 0;
15682 }
15683