xref: /linux/drivers/net/wireless/ath/ath12k/mac.c (revision 2ed4b46b4fc77749cb0f8dd31a01441b82c8dbaa)
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 static const struct ieee80211_channel ath12k_2ghz_channels[] = {
55 	CHAN2G(1, 2412, 0),
56 	CHAN2G(2, 2417, 0),
57 	CHAN2G(3, 2422, 0),
58 	CHAN2G(4, 2427, 0),
59 	CHAN2G(5, 2432, 0),
60 	CHAN2G(6, 2437, 0),
61 	CHAN2G(7, 2442, 0),
62 	CHAN2G(8, 2447, 0),
63 	CHAN2G(9, 2452, 0),
64 	CHAN2G(10, 2457, 0),
65 	CHAN2G(11, 2462, 0),
66 	CHAN2G(12, 2467, 0),
67 	CHAN2G(13, 2472, 0),
68 	CHAN2G(14, 2484, 0),
69 };
70 
71 static const struct ieee80211_channel ath12k_5ghz_channels[] = {
72 	CHAN5G(36, 5180, 0),
73 	CHAN5G(40, 5200, 0),
74 	CHAN5G(44, 5220, 0),
75 	CHAN5G(48, 5240, 0),
76 	CHAN5G(52, 5260, 0),
77 	CHAN5G(56, 5280, 0),
78 	CHAN5G(60, 5300, 0),
79 	CHAN5G(64, 5320, 0),
80 	CHAN5G(100, 5500, 0),
81 	CHAN5G(104, 5520, 0),
82 	CHAN5G(108, 5540, 0),
83 	CHAN5G(112, 5560, 0),
84 	CHAN5G(116, 5580, 0),
85 	CHAN5G(120, 5600, 0),
86 	CHAN5G(124, 5620, 0),
87 	CHAN5G(128, 5640, 0),
88 	CHAN5G(132, 5660, 0),
89 	CHAN5G(136, 5680, 0),
90 	CHAN5G(140, 5700, 0),
91 	CHAN5G(144, 5720, 0),
92 	CHAN5G(149, 5745, 0),
93 	CHAN5G(153, 5765, 0),
94 	CHAN5G(157, 5785, 0),
95 	CHAN5G(161, 5805, 0),
96 	CHAN5G(165, 5825, 0),
97 	CHAN5G(169, 5845, 0),
98 	CHAN5G(173, 5865, 0),
99 };
100 
101 static const struct ieee80211_channel ath12k_6ghz_channels[] = {
102 	/* Operating Class 136 */
103 	CHAN6G(2, 5935, 0),
104 
105 	/* Operating Classes 131-135 */
106 	CHAN6G(1, 5955, 0),
107 	CHAN6G(5, 5975, 0),
108 	CHAN6G(9, 5995, 0),
109 	CHAN6G(13, 6015, 0),
110 	CHAN6G(17, 6035, 0),
111 	CHAN6G(21, 6055, 0),
112 	CHAN6G(25, 6075, 0),
113 	CHAN6G(29, 6095, 0),
114 	CHAN6G(33, 6115, 0),
115 	CHAN6G(37, 6135, 0),
116 	CHAN6G(41, 6155, 0),
117 	CHAN6G(45, 6175, 0),
118 	CHAN6G(49, 6195, 0),
119 	CHAN6G(53, 6215, 0),
120 	CHAN6G(57, 6235, 0),
121 	CHAN6G(61, 6255, 0),
122 	CHAN6G(65, 6275, 0),
123 	CHAN6G(69, 6295, 0),
124 	CHAN6G(73, 6315, 0),
125 	CHAN6G(77, 6335, 0),
126 	CHAN6G(81, 6355, 0),
127 	CHAN6G(85, 6375, 0),
128 	CHAN6G(89, 6395, 0),
129 	CHAN6G(93, 6415, 0),
130 	CHAN6G(97, 6435, 0),
131 	CHAN6G(101, 6455, 0),
132 	CHAN6G(105, 6475, 0),
133 	CHAN6G(109, 6495, 0),
134 	CHAN6G(113, 6515, 0),
135 	CHAN6G(117, 6535, 0),
136 	CHAN6G(121, 6555, 0),
137 	CHAN6G(125, 6575, 0),
138 	CHAN6G(129, 6595, 0),
139 	CHAN6G(133, 6615, 0),
140 	CHAN6G(137, 6635, 0),
141 	CHAN6G(141, 6655, 0),
142 	CHAN6G(145, 6675, 0),
143 	CHAN6G(149, 6695, 0),
144 	CHAN6G(153, 6715, 0),
145 	CHAN6G(157, 6735, 0),
146 	CHAN6G(161, 6755, 0),
147 	CHAN6G(165, 6775, 0),
148 	CHAN6G(169, 6795, 0),
149 	CHAN6G(173, 6815, 0),
150 	CHAN6G(177, 6835, 0),
151 	CHAN6G(181, 6855, 0),
152 	CHAN6G(185, 6875, 0),
153 	CHAN6G(189, 6895, 0),
154 	CHAN6G(193, 6915, 0),
155 	CHAN6G(197, 6935, 0),
156 	CHAN6G(201, 6955, 0),
157 	CHAN6G(205, 6975, 0),
158 	CHAN6G(209, 6995, 0),
159 	CHAN6G(213, 7015, 0),
160 	CHAN6G(217, 7035, 0),
161 	CHAN6G(221, 7055, 0),
162 	CHAN6G(225, 7075, 0),
163 	CHAN6G(229, 7095, 0),
164 	CHAN6G(233, 7115, 0),
165 };
166 
167 static struct ieee80211_rate ath12k_legacy_rates[] = {
168 	{ .bitrate = 10,
169 	  .hw_value = ATH12K_HW_RATE_CCK_LP_1M },
170 	{ .bitrate = 20,
171 	  .hw_value = ATH12K_HW_RATE_CCK_LP_2M,
172 	  .hw_value_short = ATH12K_HW_RATE_CCK_SP_2M,
173 	  .flags = IEEE80211_RATE_SHORT_PREAMBLE },
174 	{ .bitrate = 55,
175 	  .hw_value = ATH12K_HW_RATE_CCK_LP_5_5M,
176 	  .hw_value_short = ATH12K_HW_RATE_CCK_SP_5_5M,
177 	  .flags = IEEE80211_RATE_SHORT_PREAMBLE },
178 	{ .bitrate = 110,
179 	  .hw_value = ATH12K_HW_RATE_CCK_LP_11M,
180 	  .hw_value_short = ATH12K_HW_RATE_CCK_SP_11M,
181 	  .flags = IEEE80211_RATE_SHORT_PREAMBLE },
182 
183 	{ .bitrate = 60, .hw_value = ATH12K_HW_RATE_OFDM_6M },
184 	{ .bitrate = 90, .hw_value = ATH12K_HW_RATE_OFDM_9M },
185 	{ .bitrate = 120, .hw_value = ATH12K_HW_RATE_OFDM_12M },
186 	{ .bitrate = 180, .hw_value = ATH12K_HW_RATE_OFDM_18M },
187 	{ .bitrate = 240, .hw_value = ATH12K_HW_RATE_OFDM_24M },
188 	{ .bitrate = 360, .hw_value = ATH12K_HW_RATE_OFDM_36M },
189 	{ .bitrate = 480, .hw_value = ATH12K_HW_RATE_OFDM_48M },
190 	{ .bitrate = 540, .hw_value = ATH12K_HW_RATE_OFDM_54M },
191 };
192 
193 static const int
194 ath12k_phymodes[NUM_NL80211_BANDS][ATH12K_CHAN_WIDTH_NUM] = {
195 	[NL80211_BAND_2GHZ] = {
196 			[NL80211_CHAN_WIDTH_5] = MODE_UNKNOWN,
197 			[NL80211_CHAN_WIDTH_10] = MODE_UNKNOWN,
198 			[NL80211_CHAN_WIDTH_20_NOHT] = MODE_11BE_EHT20_2G,
199 			[NL80211_CHAN_WIDTH_20] = MODE_11BE_EHT20_2G,
200 			[NL80211_CHAN_WIDTH_40] = MODE_11BE_EHT40_2G,
201 			[NL80211_CHAN_WIDTH_80] = MODE_UNKNOWN,
202 			[NL80211_CHAN_WIDTH_80P80] = MODE_UNKNOWN,
203 			[NL80211_CHAN_WIDTH_160] = MODE_UNKNOWN,
204 			[NL80211_CHAN_WIDTH_320] = MODE_UNKNOWN,
205 	},
206 	[NL80211_BAND_5GHZ] = {
207 			[NL80211_CHAN_WIDTH_5] = MODE_UNKNOWN,
208 			[NL80211_CHAN_WIDTH_10] = MODE_UNKNOWN,
209 			[NL80211_CHAN_WIDTH_20_NOHT] = MODE_11BE_EHT20,
210 			[NL80211_CHAN_WIDTH_20] = MODE_11BE_EHT20,
211 			[NL80211_CHAN_WIDTH_40] = MODE_11BE_EHT40,
212 			[NL80211_CHAN_WIDTH_80] = MODE_11BE_EHT80,
213 			[NL80211_CHAN_WIDTH_160] = MODE_11BE_EHT160,
214 			[NL80211_CHAN_WIDTH_80P80] = MODE_UNKNOWN,
215 			[NL80211_CHAN_WIDTH_320] = MODE_11BE_EHT320,
216 	},
217 	[NL80211_BAND_6GHZ] = {
218 			[NL80211_CHAN_WIDTH_5] = MODE_UNKNOWN,
219 			[NL80211_CHAN_WIDTH_10] = MODE_UNKNOWN,
220 			[NL80211_CHAN_WIDTH_20_NOHT] = MODE_11BE_EHT20,
221 			[NL80211_CHAN_WIDTH_20] = MODE_11BE_EHT20,
222 			[NL80211_CHAN_WIDTH_40] = MODE_11BE_EHT40,
223 			[NL80211_CHAN_WIDTH_80] = MODE_11BE_EHT80,
224 			[NL80211_CHAN_WIDTH_160] = MODE_11BE_EHT160,
225 			[NL80211_CHAN_WIDTH_80P80] = MODE_UNKNOWN,
226 			[NL80211_CHAN_WIDTH_320] = MODE_11BE_EHT320,
227 	},
228 
229 };
230 
231 const struct htt_rx_ring_tlv_filter ath12k_mac_mon_status_filter_default = {
232 	.rx_filter = HTT_RX_FILTER_TLV_FLAGS_MPDU_START |
233 		     HTT_RX_FILTER_TLV_FLAGS_PPDU_END |
234 		     HTT_RX_FILTER_TLV_FLAGS_PPDU_END_STATUS_DONE |
235 		     HTT_RX_FILTER_TLV_FLAGS_PPDU_START_USER_INFO,
236 	.pkt_filter_flags0 = HTT_RX_FP_MGMT_FILTER_FLAGS0,
237 	.pkt_filter_flags1 = HTT_RX_FP_MGMT_FILTER_FLAGS1,
238 	.pkt_filter_flags2 = HTT_RX_FP_CTRL_FILTER_FLASG2,
239 	.pkt_filter_flags3 = HTT_RX_FP_DATA_FILTER_FLASG3 |
240 			     HTT_RX_FP_CTRL_FILTER_FLASG3
241 };
242 
243 #define ATH12K_MAC_FIRST_OFDM_RATE_IDX 4
244 #define ath12k_g_rates ath12k_legacy_rates
245 #define ath12k_g_rates_size (ARRAY_SIZE(ath12k_legacy_rates))
246 #define ath12k_a_rates (ath12k_legacy_rates + 4)
247 #define ath12k_a_rates_size (ARRAY_SIZE(ath12k_legacy_rates) - 4)
248 
249 #define ATH12K_MAC_SCAN_TIMEOUT_MSECS 200 /* in msecs */
250 
251 static const u32 ath12k_smps_map[] = {
252 	[WLAN_HT_CAP_SM_PS_STATIC] = WMI_PEER_SMPS_STATIC,
253 	[WLAN_HT_CAP_SM_PS_DYNAMIC] = WMI_PEER_SMPS_DYNAMIC,
254 	[WLAN_HT_CAP_SM_PS_INVALID] = WMI_PEER_SMPS_PS_NONE,
255 	[WLAN_HT_CAP_SM_PS_DISABLED] = WMI_PEER_SMPS_PS_NONE,
256 };
257 
258 static int ath12k_start_vdev_delay(struct ath12k *ar,
259 				   struct ath12k_link_vif *arvif);
260 static void ath12k_mac_stop(struct ath12k *ar);
261 static int ath12k_mac_vdev_create(struct ath12k *ar, struct ath12k_link_vif *arvif);
262 static int ath12k_mac_vdev_delete(struct ath12k *ar, struct ath12k_link_vif *arvif);
263 
264 static const char *ath12k_mac_phymode_str(enum wmi_phy_mode mode)
265 {
266 	switch (mode) {
267 	case MODE_11A:
268 		return "11a";
269 	case MODE_11G:
270 		return "11g";
271 	case MODE_11B:
272 		return "11b";
273 	case MODE_11GONLY:
274 		return "11gonly";
275 	case MODE_11NA_HT20:
276 		return "11na-ht20";
277 	case MODE_11NG_HT20:
278 		return "11ng-ht20";
279 	case MODE_11NA_HT40:
280 		return "11na-ht40";
281 	case MODE_11NG_HT40:
282 		return "11ng-ht40";
283 	case MODE_11AC_VHT20:
284 		return "11ac-vht20";
285 	case MODE_11AC_VHT40:
286 		return "11ac-vht40";
287 	case MODE_11AC_VHT80:
288 		return "11ac-vht80";
289 	case MODE_11AC_VHT160:
290 		return "11ac-vht160";
291 	case MODE_11AC_VHT80_80:
292 		return "11ac-vht80+80";
293 	case MODE_11AC_VHT20_2G:
294 		return "11ac-vht20-2g";
295 	case MODE_11AC_VHT40_2G:
296 		return "11ac-vht40-2g";
297 	case MODE_11AC_VHT80_2G:
298 		return "11ac-vht80-2g";
299 	case MODE_11AX_HE20:
300 		return "11ax-he20";
301 	case MODE_11AX_HE40:
302 		return "11ax-he40";
303 	case MODE_11AX_HE80:
304 		return "11ax-he80";
305 	case MODE_11AX_HE80_80:
306 		return "11ax-he80+80";
307 	case MODE_11AX_HE160:
308 		return "11ax-he160";
309 	case MODE_11AX_HE20_2G:
310 		return "11ax-he20-2g";
311 	case MODE_11AX_HE40_2G:
312 		return "11ax-he40-2g";
313 	case MODE_11AX_HE80_2G:
314 		return "11ax-he80-2g";
315 	case MODE_11BE_EHT20:
316 		return "11be-eht20";
317 	case MODE_11BE_EHT40:
318 		return "11be-eht40";
319 	case MODE_11BE_EHT80:
320 		return "11be-eht80";
321 	case MODE_11BE_EHT80_80:
322 		return "11be-eht80+80";
323 	case MODE_11BE_EHT160:
324 		return "11be-eht160";
325 	case MODE_11BE_EHT160_160:
326 		return "11be-eht160+160";
327 	case MODE_11BE_EHT320:
328 		return "11be-eht320";
329 	case MODE_11BE_EHT20_2G:
330 		return "11be-eht20-2g";
331 	case MODE_11BE_EHT40_2G:
332 		return "11be-eht40-2g";
333 	case MODE_UNKNOWN:
334 		/* skip */
335 		break;
336 
337 		/* no default handler to allow compiler to check that the
338 		 * enum is fully handled
339 		 */
340 	}
341 
342 	return "<unknown>";
343 }
344 
345 u16 ath12k_mac_he_convert_tones_to_ru_tones(u16 tones)
346 {
347 	switch (tones) {
348 	case 26:
349 		return RU_26;
350 	case 52:
351 		return RU_52;
352 	case 106:
353 		return RU_106;
354 	case 242:
355 		return RU_242;
356 	case 484:
357 		return RU_484;
358 	case 996:
359 		return RU_996;
360 	case (996 * 2):
361 		return RU_2X996;
362 	default:
363 		return RU_26;
364 	}
365 }
366 EXPORT_SYMBOL(ath12k_mac_he_convert_tones_to_ru_tones);
367 
368 enum nl80211_eht_gi ath12k_mac_eht_gi_to_nl80211_eht_gi(u8 sgi)
369 {
370 	switch (sgi) {
371 	case RX_MSDU_START_SGI_0_8_US:
372 		return NL80211_RATE_INFO_EHT_GI_0_8;
373 	case RX_MSDU_START_SGI_1_6_US:
374 		return NL80211_RATE_INFO_EHT_GI_1_6;
375 	case RX_MSDU_START_SGI_3_2_US:
376 		return NL80211_RATE_INFO_EHT_GI_3_2;
377 	default:
378 		return NL80211_RATE_INFO_EHT_GI_0_8;
379 	}
380 }
381 EXPORT_SYMBOL(ath12k_mac_eht_gi_to_nl80211_eht_gi);
382 
383 enum nl80211_eht_ru_alloc ath12k_mac_eht_ru_tones_to_nl80211_eht_ru_alloc(u16 ru_tones)
384 {
385 	switch (ru_tones) {
386 	case 26:
387 		return NL80211_RATE_INFO_EHT_RU_ALLOC_26;
388 	case 52:
389 		return NL80211_RATE_INFO_EHT_RU_ALLOC_52;
390 	case (52 + 26):
391 		return NL80211_RATE_INFO_EHT_RU_ALLOC_52P26;
392 	case 106:
393 		return NL80211_RATE_INFO_EHT_RU_ALLOC_106;
394 	case (106 + 26):
395 		return NL80211_RATE_INFO_EHT_RU_ALLOC_106P26;
396 	case 242:
397 		return NL80211_RATE_INFO_EHT_RU_ALLOC_242;
398 	case 484:
399 		return NL80211_RATE_INFO_EHT_RU_ALLOC_484;
400 	case (484 + 242):
401 		return NL80211_RATE_INFO_EHT_RU_ALLOC_484P242;
402 	case 996:
403 		return NL80211_RATE_INFO_EHT_RU_ALLOC_996;
404 	case (996 + 484):
405 		return NL80211_RATE_INFO_EHT_RU_ALLOC_996P484;
406 	case (996 + 484 + 242):
407 		return NL80211_RATE_INFO_EHT_RU_ALLOC_996P484P242;
408 	case (2 * 996):
409 		return NL80211_RATE_INFO_EHT_RU_ALLOC_2x996;
410 	case (2 * 996 + 484):
411 		return NL80211_RATE_INFO_EHT_RU_ALLOC_2x996P484;
412 	case (3 * 996):
413 		return NL80211_RATE_INFO_EHT_RU_ALLOC_3x996;
414 	case (3 * 996 + 484):
415 		return NL80211_RATE_INFO_EHT_RU_ALLOC_3x996P484;
416 	case (4 * 996):
417 		return NL80211_RATE_INFO_EHT_RU_ALLOC_4x996;
418 	default:
419 		return NL80211_RATE_INFO_EHT_RU_ALLOC_26;
420 	}
421 }
422 EXPORT_SYMBOL(ath12k_mac_eht_ru_tones_to_nl80211_eht_ru_alloc);
423 
424 enum rate_info_bw
425 ath12k_mac_bw_to_mac80211_bw(enum ath12k_supported_bw bw)
426 {
427 	u8 ret = RATE_INFO_BW_20;
428 
429 	switch (bw) {
430 	case ATH12K_BW_20:
431 		ret = RATE_INFO_BW_20;
432 		break;
433 	case ATH12K_BW_40:
434 		ret = RATE_INFO_BW_40;
435 		break;
436 	case ATH12K_BW_80:
437 		ret = RATE_INFO_BW_80;
438 		break;
439 	case ATH12K_BW_160:
440 		ret = RATE_INFO_BW_160;
441 		break;
442 	case ATH12K_BW_320:
443 		ret = RATE_INFO_BW_320;
444 		break;
445 	}
446 
447 	return ret;
448 }
449 EXPORT_SYMBOL(ath12k_mac_bw_to_mac80211_bw);
450 
451 enum ath12k_supported_bw ath12k_mac_mac80211_bw_to_ath12k_bw(enum rate_info_bw bw)
452 {
453 	switch (bw) {
454 	case RATE_INFO_BW_20:
455 		return ATH12K_BW_20;
456 	case RATE_INFO_BW_40:
457 		return ATH12K_BW_40;
458 	case RATE_INFO_BW_80:
459 		return ATH12K_BW_80;
460 	case RATE_INFO_BW_160:
461 		return ATH12K_BW_160;
462 	case RATE_INFO_BW_320:
463 		return ATH12K_BW_320;
464 	default:
465 		return ATH12K_BW_20;
466 	}
467 }
468 
469 int ath12k_mac_hw_ratecode_to_legacy_rate(u8 hw_rc, u8 preamble, u8 *rateidx,
470 					  u16 *rate)
471 {
472 	/* As default, it is OFDM rates */
473 	int i = ATH12K_MAC_FIRST_OFDM_RATE_IDX;
474 	int max_rates_idx = ath12k_g_rates_size;
475 
476 	if (preamble == WMI_RATE_PREAMBLE_CCK) {
477 		hw_rc &= ~ATH12K_HW_RATECODE_CCK_SHORT_PREAM_MASK;
478 		i = 0;
479 		max_rates_idx = ATH12K_MAC_FIRST_OFDM_RATE_IDX;
480 	}
481 
482 	while (i < max_rates_idx) {
483 		if (hw_rc == ath12k_legacy_rates[i].hw_value) {
484 			*rateidx = i;
485 			*rate = ath12k_legacy_rates[i].bitrate;
486 			return 0;
487 		}
488 		i++;
489 	}
490 
491 	return -EINVAL;
492 }
493 EXPORT_SYMBOL(ath12k_mac_hw_ratecode_to_legacy_rate);
494 
495 u8 ath12k_mac_bitrate_to_idx(const struct ieee80211_supported_band *sband,
496 			     u32 bitrate)
497 {
498 	int i;
499 
500 	for (i = 0; i < sband->n_bitrates; i++)
501 		if (sband->bitrates[i].bitrate == bitrate)
502 			return i;
503 
504 	return 0;
505 }
506 
507 static u32
508 ath12k_mac_max_ht_nss(const u8 *ht_mcs_mask)
509 {
510 	int nss;
511 
512 	for (nss = IEEE80211_HT_MCS_MASK_LEN - 1; nss >= 0; nss--)
513 		if (ht_mcs_mask[nss])
514 			return nss + 1;
515 
516 	return 1;
517 }
518 
519 static u32
520 ath12k_mac_max_vht_nss(const u16 *vht_mcs_mask)
521 {
522 	int nss;
523 
524 	for (nss = NL80211_VHT_NSS_MAX - 1; nss >= 0; nss--)
525 		if (vht_mcs_mask[nss])
526 			return nss + 1;
527 
528 	return 1;
529 }
530 
531 static u32
532 ath12k_mac_max_he_nss(const u16 he_mcs_mask[NL80211_HE_NSS_MAX])
533 {
534 	int nss;
535 
536 	for (nss = NL80211_HE_NSS_MAX - 1; nss >= 0; nss--)
537 		if (he_mcs_mask[nss])
538 			return nss + 1;
539 
540 	return 1;
541 }
542 
543 static u32
544 ath12k_mac_max_eht_nss(const u16 eht_mcs_mask[NL80211_EHT_NSS_MAX])
545 {
546 	int nss;
547 
548 	for (nss = NL80211_EHT_NSS_MAX - 1; nss >= 0; nss--)
549 		if (eht_mcs_mask[nss])
550 			return nss + 1;
551 
552 	return 1;
553 }
554 
555 static u32
556 ath12k_mac_max_eht_mcs_nss(const u8 *eht_mcs, int eht_mcs_set_size)
557 {
558 	int i;
559 	u8 nss = 0;
560 
561 	for (i = 0; i < eht_mcs_set_size; i++)
562 		nss = max(nss, u8_get_bits(eht_mcs[i], IEEE80211_EHT_MCS_NSS_RX));
563 
564 	return nss;
565 }
566 
567 static u8 ath12k_parse_mpdudensity(u8 mpdudensity)
568 {
569 /*  From IEEE Std 802.11-2020 defined values for "Minimum MPDU Start Spacing":
570  *   0 for no restriction
571  *   1 for 1/4 us
572  *   2 for 1/2 us
573  *   3 for 1 us
574  *   4 for 2 us
575  *   5 for 4 us
576  *   6 for 8 us
577  *   7 for 16 us
578  */
579 	switch (mpdudensity) {
580 	case 0:
581 		return 0;
582 	case 1:
583 	case 2:
584 	case 3:
585 	/* Our lower layer calculations limit our precision to
586 	 * 1 microsecond
587 	 */
588 		return 1;
589 	case 4:
590 		return 2;
591 	case 5:
592 		return 4;
593 	case 6:
594 		return 8;
595 	case 7:
596 		return 16;
597 	default:
598 		return 0;
599 	}
600 }
601 
602 static int ath12k_mac_vif_link_chan(struct ieee80211_vif *vif, u8 link_id,
603 				    struct cfg80211_chan_def *def)
604 {
605 	struct ieee80211_bss_conf *link_conf;
606 	struct ieee80211_chanctx_conf *conf;
607 
608 	rcu_read_lock();
609 	link_conf = rcu_dereference(vif->link_conf[link_id]);
610 
611 	if (!link_conf) {
612 		rcu_read_unlock();
613 		return -ENOLINK;
614 	}
615 
616 	conf = rcu_dereference(link_conf->chanctx_conf);
617 	if (!conf) {
618 		rcu_read_unlock();
619 		return -ENOENT;
620 	}
621 	*def = conf->def;
622 	rcu_read_unlock();
623 
624 	return 0;
625 }
626 
627 static struct ath12k_link_vif *
628 ath12k_mac_get_tx_arvif(struct ath12k_link_vif *arvif,
629 			struct ieee80211_bss_conf *link_conf)
630 {
631 	struct ieee80211_bss_conf *tx_bss_conf;
632 	struct ath12k *ar = arvif->ar;
633 	struct ath12k_vif *tx_ahvif;
634 
635 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
636 
637 	tx_bss_conf = wiphy_dereference(ath12k_ar_to_hw(ar)->wiphy,
638 					link_conf->tx_bss_conf);
639 	if (tx_bss_conf) {
640 		tx_ahvif = ath12k_vif_to_ahvif(tx_bss_conf->vif);
641 		return wiphy_dereference(tx_ahvif->ah->hw->wiphy,
642 					 tx_ahvif->link[tx_bss_conf->link_id]);
643 	}
644 
645 	return NULL;
646 }
647 
648 static const u8 *ath12k_mac_get_tx_bssid(struct ath12k_link_vif *arvif)
649 {
650 	struct ieee80211_bss_conf *link_conf;
651 	struct ath12k_link_vif *tx_arvif;
652 	struct ath12k *ar = arvif->ar;
653 
654 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
655 
656 	link_conf = ath12k_mac_get_link_bss_conf(arvif);
657 	if (!link_conf) {
658 		ath12k_warn(ar->ab,
659 			    "unable to access bss link conf for link %u required to retrieve transmitting link conf\n",
660 			    arvif->link_id);
661 		return NULL;
662 	}
663 	if (link_conf->vif->type == NL80211_IFTYPE_STATION) {
664 		if (link_conf->nontransmitted)
665 			return link_conf->transmitter_bssid;
666 	} else {
667 		tx_arvif = ath12k_mac_get_tx_arvif(arvif, link_conf);
668 		if (tx_arvif)
669 			return tx_arvif->bssid;
670 	}
671 
672 	return NULL;
673 }
674 
675 struct ieee80211_bss_conf *
676 ath12k_mac_get_link_bss_conf(struct ath12k_link_vif *arvif)
677 {
678 	struct ieee80211_vif *vif = arvif->ahvif->vif;
679 	struct ieee80211_bss_conf *link_conf;
680 	struct ath12k *ar = arvif->ar;
681 
682 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
683 
684 	if (arvif->link_id >= IEEE80211_MLD_MAX_NUM_LINKS)
685 		return NULL;
686 
687 	link_conf = wiphy_dereference(ath12k_ar_to_hw(ar)->wiphy,
688 				      vif->link_conf[arvif->link_id]);
689 
690 	return link_conf;
691 }
692 
693 static struct ieee80211_link_sta *ath12k_mac_get_link_sta(struct ath12k_link_sta *arsta)
694 {
695 	struct ath12k_sta *ahsta = arsta->ahsta;
696 	struct ieee80211_sta *sta = ath12k_ahsta_to_sta(ahsta);
697 	struct ieee80211_link_sta *link_sta;
698 
699 	lockdep_assert_wiphy(ahsta->ahvif->ah->hw->wiphy);
700 
701 	if (arsta->link_id >= IEEE80211_MLD_MAX_NUM_LINKS)
702 		return NULL;
703 
704 	link_sta = wiphy_dereference(ahsta->ahvif->ah->hw->wiphy,
705 				     sta->link[arsta->link_id]);
706 
707 	return link_sta;
708 }
709 
710 static bool ath12k_mac_bitrate_is_cck(int bitrate)
711 {
712 	switch (bitrate) {
713 	case 10:
714 	case 20:
715 	case 55:
716 	case 110:
717 		return true;
718 	}
719 
720 	return false;
721 }
722 
723 u8 ath12k_mac_hw_rate_to_idx(const struct ieee80211_supported_band *sband,
724 			     u8 hw_rate, bool cck)
725 {
726 	const struct ieee80211_rate *rate;
727 	int i;
728 
729 	for (i = 0; i < sband->n_bitrates; i++) {
730 		rate = &sband->bitrates[i];
731 
732 		if (ath12k_mac_bitrate_is_cck(rate->bitrate) != cck)
733 			continue;
734 
735 		if (rate->hw_value == hw_rate)
736 			return i;
737 		else if (rate->flags & IEEE80211_RATE_SHORT_PREAMBLE &&
738 			 rate->hw_value_short == hw_rate)
739 			return i;
740 	}
741 
742 	return 0;
743 }
744 
745 static u8 ath12k_mac_bitrate_to_rate(int bitrate)
746 {
747 	return DIV_ROUND_UP(bitrate, 5) |
748 	       (ath12k_mac_bitrate_is_cck(bitrate) ? BIT(7) : 0);
749 }
750 
751 static void ath12k_get_arvif_iter(void *data, u8 *mac,
752 				  struct ieee80211_vif *vif)
753 {
754 	struct ath12k_vif_iter *arvif_iter = data;
755 	struct ath12k_vif *ahvif = ath12k_vif_to_ahvif(vif);
756 	unsigned long links_map = ahvif->links_map;
757 	struct ath12k_link_vif *arvif;
758 	u8 link_id;
759 
760 	for_each_set_bit(link_id, &links_map, IEEE80211_MLD_MAX_NUM_LINKS) {
761 		arvif = rcu_dereference(ahvif->link[link_id]);
762 
763 		if (WARN_ON(!arvif))
764 			continue;
765 
766 		if (!arvif->is_created)
767 			continue;
768 
769 		if (arvif->vdev_id == arvif_iter->vdev_id &&
770 		    arvif->ar == arvif_iter->ar) {
771 			arvif_iter->arvif = arvif;
772 			break;
773 		}
774 	}
775 }
776 
777 struct ath12k_link_vif *ath12k_mac_get_arvif(struct ath12k *ar, u32 vdev_id)
778 {
779 	struct ath12k_vif_iter arvif_iter = {};
780 	u32 flags;
781 
782 	/* To use the arvif returned, caller must have held rcu read lock.
783 	 */
784 	WARN_ON(!rcu_read_lock_any_held());
785 	arvif_iter.vdev_id = vdev_id;
786 	arvif_iter.ar = ar;
787 
788 	flags = IEEE80211_IFACE_ITER_RESUME_ALL;
789 	ieee80211_iterate_active_interfaces_atomic(ath12k_ar_to_hw(ar),
790 						   flags,
791 						   ath12k_get_arvif_iter,
792 						   &arvif_iter);
793 	if (!arvif_iter.arvif) {
794 		ath12k_warn(ar->ab, "No VIF found for vdev %d\n", vdev_id);
795 		return NULL;
796 	}
797 
798 	return arvif_iter.arvif;
799 }
800 
801 struct ath12k_link_vif *ath12k_mac_get_arvif_by_vdev_id(struct ath12k_base *ab,
802 							u32 vdev_id)
803 {
804 	int i;
805 	struct ath12k_pdev *pdev;
806 	struct ath12k_link_vif *arvif;
807 
808 	for (i = 0; i < ab->num_radios; i++) {
809 		pdev = rcu_dereference(ab->pdevs_active[i]);
810 		if (pdev && pdev->ar &&
811 		    (pdev->ar->allocated_vdev_map & (1LL << vdev_id))) {
812 			arvif = ath12k_mac_get_arvif(pdev->ar, vdev_id);
813 			if (arvif)
814 				return arvif;
815 		}
816 	}
817 
818 	return NULL;
819 }
820 
821 struct ath12k *ath12k_mac_get_ar_by_vdev_id(struct ath12k_base *ab, u32 vdev_id)
822 {
823 	int i;
824 	struct ath12k_pdev *pdev;
825 
826 	for (i = 0; i < ab->num_radios; i++) {
827 		pdev = rcu_dereference(ab->pdevs_active[i]);
828 		if (pdev && pdev->ar) {
829 			if (pdev->ar->allocated_vdev_map & (1LL << vdev_id))
830 				return pdev->ar;
831 		}
832 	}
833 
834 	return NULL;
835 }
836 
837 struct ath12k *ath12k_mac_get_ar_by_pdev_id(struct ath12k_base *ab, u32 pdev_id)
838 {
839 	int i;
840 	struct ath12k_pdev *pdev;
841 
842 	if (ab->hw_params->single_pdev_only) {
843 		pdev = rcu_dereference(ab->pdevs_active[0]);
844 		return pdev ? pdev->ar : NULL;
845 	}
846 
847 	if (WARN_ON(pdev_id > ab->num_radios))
848 		return NULL;
849 
850 	for (i = 0; i < ab->num_radios; i++) {
851 		if (ab->fw_mode == ATH12K_FIRMWARE_MODE_FTM)
852 			pdev = &ab->pdevs[i];
853 		else
854 			pdev = rcu_dereference(ab->pdevs_active[i]);
855 
856 		if (pdev && pdev->pdev_id == pdev_id)
857 			return (pdev->ar ? pdev->ar : NULL);
858 	}
859 
860 	return NULL;
861 }
862 
863 static bool ath12k_mac_is_ml_arvif(struct ath12k_link_vif *arvif)
864 {
865 	struct ath12k_vif *ahvif = arvif->ahvif;
866 
867 	lockdep_assert_wiphy(ahvif->ah->hw->wiphy);
868 
869 	if (ahvif->vif->valid_links & BIT(arvif->link_id))
870 		return true;
871 
872 	return false;
873 }
874 
875 static struct ath12k *ath12k_mac_get_ar_by_chan(struct ieee80211_hw *hw,
876 						struct ieee80211_channel *channel)
877 {
878 	struct ath12k_hw *ah = hw->priv;
879 	struct ath12k *ar;
880 	int i;
881 
882 	ar = ah->radio;
883 
884 	if (ah->num_radio == 1)
885 		return ar;
886 
887 	for_each_ar(ah, ar, i) {
888 		if (channel->center_freq >= KHZ_TO_MHZ(ar->freq_range.start_freq) &&
889 		    channel->center_freq <= KHZ_TO_MHZ(ar->freq_range.end_freq))
890 			return ar;
891 	}
892 	return NULL;
893 }
894 
895 static struct ath12k *ath12k_get_ar_by_ctx(struct ieee80211_hw *hw,
896 					   struct ieee80211_chanctx_conf *ctx)
897 {
898 	if (!ctx)
899 		return NULL;
900 
901 	return ath12k_mac_get_ar_by_chan(hw, ctx->def.chan);
902 }
903 
904 struct ath12k *ath12k_get_ar_by_vif(struct ieee80211_hw *hw,
905 				    struct ieee80211_vif *vif,
906 				    u8 link_id)
907 {
908 	struct ath12k_vif *ahvif = ath12k_vif_to_ahvif(vif);
909 	struct ath12k_hw *ah = ath12k_hw_to_ah(hw);
910 	struct ath12k_link_vif *arvif;
911 
912 	lockdep_assert_wiphy(hw->wiphy);
913 
914 	/* If there is one pdev within ah, then we return
915 	 * ar directly.
916 	 */
917 	if (ah->num_radio == 1)
918 		return ah->radio;
919 
920 	if (!(ahvif->links_map & BIT(link_id)))
921 		return NULL;
922 
923 	arvif = wiphy_dereference(hw->wiphy, ahvif->link[link_id]);
924 	if (arvif && arvif->is_created)
925 		return arvif->ar;
926 
927 	return NULL;
928 }
929 
930 void ath12k_mac_get_any_chanctx_conf_iter(struct ieee80211_hw *hw,
931 					  struct ieee80211_chanctx_conf *conf,
932 					  void *data)
933 {
934 	struct ath12k_mac_get_any_chanctx_conf_arg *arg = data;
935 	struct ath12k *ctx_ar = ath12k_get_ar_by_ctx(hw, conf);
936 
937 	if (ctx_ar == arg->ar)
938 		arg->chanctx_conf = conf;
939 }
940 
941 static struct ath12k_link_vif *ath12k_mac_get_vif_up(struct ath12k *ar)
942 {
943 	struct ath12k_link_vif *arvif;
944 
945 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
946 
947 	list_for_each_entry(arvif, &ar->arvifs, list) {
948 		if (arvif->is_up)
949 			return arvif;
950 	}
951 
952 	return NULL;
953 }
954 
955 static bool ath12k_mac_band_match(enum nl80211_band band1, enum WMI_HOST_WLAN_BAND band2)
956 {
957 	switch (band1) {
958 	case NL80211_BAND_2GHZ:
959 		if (band2 & WMI_HOST_WLAN_2GHZ_CAP)
960 			return true;
961 		break;
962 	case NL80211_BAND_5GHZ:
963 	case NL80211_BAND_6GHZ:
964 		if (band2 & WMI_HOST_WLAN_5GHZ_CAP)
965 			return true;
966 		break;
967 	default:
968 		return false;
969 	}
970 
971 	return false;
972 }
973 
974 static u8 ath12k_mac_get_target_pdev_id_from_vif(struct ath12k_link_vif *arvif)
975 {
976 	struct ath12k *ar = arvif->ar;
977 	struct ath12k_base *ab = ar->ab;
978 	struct ieee80211_vif *vif = arvif->ahvif->vif;
979 	struct cfg80211_chan_def def;
980 	enum nl80211_band band;
981 	u8 pdev_id = ab->fw_pdev[0].pdev_id;
982 	int i;
983 
984 	if (WARN_ON(ath12k_mac_vif_link_chan(vif, arvif->link_id, &def)))
985 		return pdev_id;
986 
987 	band = def.chan->band;
988 
989 	for (i = 0; i < ab->fw_pdev_count; i++) {
990 		if (ath12k_mac_band_match(band, ab->fw_pdev[i].supported_bands))
991 			return ab->fw_pdev[i].pdev_id;
992 	}
993 
994 	return pdev_id;
995 }
996 
997 u8 ath12k_mac_get_target_pdev_id(struct ath12k *ar)
998 {
999 	struct ath12k_link_vif *arvif;
1000 	struct ath12k_base *ab = ar->ab;
1001 
1002 	if (!ab->hw_params->single_pdev_only)
1003 		return ar->pdev->pdev_id;
1004 
1005 	arvif = ath12k_mac_get_vif_up(ar);
1006 
1007 	/* fw_pdev array has pdev ids derived from phy capability
1008 	 * service ready event (pdev_and_hw_link_ids).
1009 	 * If no vif is active, return default first index.
1010 	 */
1011 	if (!arvif)
1012 		return ar->ab->fw_pdev[0].pdev_id;
1013 
1014 	/* If active vif is found, return the pdev id matching chandef band */
1015 	return ath12k_mac_get_target_pdev_id_from_vif(arvif);
1016 }
1017 
1018 static void ath12k_pdev_caps_update(struct ath12k *ar)
1019 {
1020 	struct ath12k_base *ab = ar->ab;
1021 
1022 	ar->max_tx_power = ab->target_caps.hw_max_tx_power;
1023 
1024 	/* FIXME: Set min_tx_power to ab->target_caps.hw_min_tx_power.
1025 	 * But since the received value in svcrdy is same as hw_max_tx_power,
1026 	 * we can set ar->min_tx_power to 0 currently until
1027 	 * this is fixed in firmware
1028 	 */
1029 	ar->min_tx_power = 0;
1030 
1031 	ar->txpower_limit_2g = ar->max_tx_power;
1032 	ar->txpower_limit_5g = ar->max_tx_power;
1033 	ar->txpower_scale = WMI_HOST_TP_SCALE_MAX;
1034 }
1035 
1036 static int ath12k_mac_txpower_recalc(struct ath12k *ar)
1037 {
1038 	struct ath12k_pdev *pdev = ar->pdev;
1039 	struct ath12k_link_vif *arvif;
1040 	int ret, txpower = -1;
1041 	u32 param;
1042 
1043 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
1044 
1045 	list_for_each_entry(arvif, &ar->arvifs, list) {
1046 		if (arvif->txpower <= 0)
1047 			continue;
1048 
1049 		if (txpower == -1)
1050 			txpower = arvif->txpower;
1051 		else
1052 			txpower = min(txpower, arvif->txpower);
1053 	}
1054 
1055 	if (txpower == -1)
1056 		return 0;
1057 
1058 	/* txpwr is set as 2 units per dBm in FW*/
1059 	txpower = min_t(u32, max_t(u32, ar->min_tx_power, txpower),
1060 			ar->max_tx_power) * 2;
1061 
1062 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "txpower to set in hw %d\n",
1063 		   txpower / 2);
1064 
1065 	if ((pdev->cap.supported_bands & WMI_HOST_WLAN_2GHZ_CAP) &&
1066 	    ar->txpower_limit_2g != txpower) {
1067 		param = WMI_PDEV_PARAM_TXPOWER_LIMIT2G;
1068 		ret = ath12k_wmi_pdev_set_param(ar, param,
1069 						txpower, ar->pdev->pdev_id);
1070 		if (ret)
1071 			goto fail;
1072 		ar->txpower_limit_2g = txpower;
1073 	}
1074 
1075 	if ((pdev->cap.supported_bands & WMI_HOST_WLAN_5GHZ_CAP) &&
1076 	    ar->txpower_limit_5g != txpower) {
1077 		param = WMI_PDEV_PARAM_TXPOWER_LIMIT5G;
1078 		ret = ath12k_wmi_pdev_set_param(ar, param,
1079 						txpower, ar->pdev->pdev_id);
1080 		if (ret)
1081 			goto fail;
1082 		ar->txpower_limit_5g = txpower;
1083 	}
1084 
1085 	return 0;
1086 
1087 fail:
1088 	ath12k_warn(ar->ab, "failed to recalc txpower limit %d using pdev param %d: %d\n",
1089 		    txpower / 2, param, ret);
1090 	return ret;
1091 }
1092 
1093 static int ath12k_recalc_rtscts_prot(struct ath12k_link_vif *arvif)
1094 {
1095 	struct ath12k *ar = arvif->ar;
1096 	u32 vdev_param, rts_cts;
1097 	int ret;
1098 
1099 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
1100 
1101 	vdev_param = WMI_VDEV_PARAM_ENABLE_RTSCTS;
1102 
1103 	/* Enable RTS/CTS protection for sw retries (when legacy stations
1104 	 * are in BSS) or by default only for second rate series.
1105 	 * TODO: Check if we need to enable CTS 2 Self in any case
1106 	 */
1107 	rts_cts = WMI_USE_RTS_CTS;
1108 
1109 	if (arvif->num_legacy_stations > 0)
1110 		rts_cts |= WMI_RTSCTS_ACROSS_SW_RETRIES << 4;
1111 	else
1112 		rts_cts |= WMI_RTSCTS_FOR_SECOND_RATESERIES << 4;
1113 
1114 	/* Need not send duplicate param value to firmware */
1115 	if (arvif->rtscts_prot_mode == rts_cts)
1116 		return 0;
1117 
1118 	arvif->rtscts_prot_mode = rts_cts;
1119 
1120 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac vdev %d recalc rts/cts prot %d\n",
1121 		   arvif->vdev_id, rts_cts);
1122 
1123 	ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
1124 					    vdev_param, rts_cts);
1125 	if (ret)
1126 		ath12k_warn(ar->ab, "failed to recalculate rts/cts prot for vdev %d: %d\n",
1127 			    arvif->vdev_id, ret);
1128 
1129 	return ret;
1130 }
1131 
1132 static int ath12k_mac_set_kickout(struct ath12k_link_vif *arvif)
1133 {
1134 	struct ath12k *ar = arvif->ar;
1135 	u32 param;
1136 	int ret;
1137 
1138 	ret = ath12k_wmi_pdev_set_param(ar, WMI_PDEV_PARAM_STA_KICKOUT_TH,
1139 					ATH12K_KICKOUT_THRESHOLD,
1140 					ar->pdev->pdev_id);
1141 	if (ret) {
1142 		ath12k_warn(ar->ab, "failed to set kickout threshold on vdev %i: %d\n",
1143 			    arvif->vdev_id, ret);
1144 		return ret;
1145 	}
1146 
1147 	param = WMI_VDEV_PARAM_AP_KEEPALIVE_MIN_IDLE_INACTIVE_TIME_SECS;
1148 	ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id, param,
1149 					    ATH12K_KEEPALIVE_MIN_IDLE);
1150 	if (ret) {
1151 		ath12k_warn(ar->ab, "failed to set keepalive minimum idle time on vdev %i: %d\n",
1152 			    arvif->vdev_id, ret);
1153 		return ret;
1154 	}
1155 
1156 	param = WMI_VDEV_PARAM_AP_KEEPALIVE_MAX_IDLE_INACTIVE_TIME_SECS;
1157 	ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id, param,
1158 					    ATH12K_KEEPALIVE_MAX_IDLE);
1159 	if (ret) {
1160 		ath12k_warn(ar->ab, "failed to set keepalive maximum idle time on vdev %i: %d\n",
1161 			    arvif->vdev_id, ret);
1162 		return ret;
1163 	}
1164 
1165 	param = WMI_VDEV_PARAM_AP_KEEPALIVE_MAX_UNRESPONSIVE_TIME_SECS;
1166 	ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id, param,
1167 					    ATH12K_KEEPALIVE_MAX_UNRESPONSIVE);
1168 	if (ret) {
1169 		ath12k_warn(ar->ab, "failed to set keepalive maximum unresponsive time on vdev %i: %d\n",
1170 			    arvif->vdev_id, ret);
1171 		return ret;
1172 	}
1173 
1174 	return 0;
1175 }
1176 
1177 static void ath12k_mac_link_sta_rhash_cleanup(void *data, struct ieee80211_sta *sta)
1178 {
1179 	u8 link_id;
1180 	unsigned long links_map;
1181 	struct ath12k_sta *ahsta;
1182 	struct ath12k *ar = data;
1183 	struct ath12k_link_sta *arsta;
1184 	struct ath12k_link_vif *arvif;
1185 	struct ath12k_base *ab = ar->ab;
1186 
1187 	ahsta = ath12k_sta_to_ahsta(sta);
1188 	links_map = ahsta->links_map;
1189 
1190 	rcu_read_lock();
1191 	for_each_set_bit(link_id, &links_map, IEEE80211_MLD_MAX_NUM_LINKS) {
1192 		arsta = rcu_dereference(ahsta->link[link_id]);
1193 		if (!arsta)
1194 			continue;
1195 		arvif = arsta->arvif;
1196 		if (!(arvif->ar == ar))
1197 			continue;
1198 
1199 		spin_lock_bh(&ab->base_lock);
1200 		ath12k_link_sta_rhash_delete(ab, arsta);
1201 		spin_unlock_bh(&ab->base_lock);
1202 	}
1203 	rcu_read_unlock();
1204 }
1205 
1206 void ath12k_mac_peer_cleanup_all(struct ath12k *ar)
1207 {
1208 	struct ath12k_dp_link_peer *peer, *tmp;
1209 	struct ath12k_base *ab = ar->ab;
1210 	struct ath12k_dp *dp = ath12k_ab_to_dp(ab);
1211 	struct ath12k_link_vif *arvif, *tmp_vif;
1212 	struct ath12k_dp_hw *dp_hw = &ar->ah->dp_hw;
1213 	struct ath12k_dp_peer *dp_peer = NULL;
1214 	u16 peerid_index;
1215 	struct list_head peers;
1216 
1217 	INIT_LIST_HEAD(&peers);
1218 
1219 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
1220 
1221 	spin_lock_bh(&dp->dp_lock);
1222 	list_for_each_entry_safe(peer, tmp, &dp->peers, list) {
1223 		/* Skip Rx TID cleanup for self peer */
1224 		if (peer->sta && peer->dp_peer)
1225 			ath12k_dp_rx_peer_tid_cleanup(ar, peer);
1226 
1227 		/* cleanup dp peer */
1228 		spin_lock_bh(&dp_hw->peer_lock);
1229 		dp_peer = peer->dp_peer;
1230 		peerid_index = ath12k_dp_peer_get_peerid_index(dp, peer->peer_id);
1231 		rcu_assign_pointer(dp_peer->link_peers[peer->link_id], NULL);
1232 		rcu_assign_pointer(dp_hw->dp_peers[peerid_index], NULL);
1233 		spin_unlock_bh(&dp_hw->peer_lock);
1234 
1235 		ath12k_dp_link_peer_rhash_delete(dp, peer);
1236 
1237 		list_move(&peer->list, &peers);
1238 	}
1239 	spin_unlock_bh(&dp->dp_lock);
1240 
1241 	synchronize_rcu();
1242 
1243 	list_for_each_entry_safe(peer, tmp, &peers, list) {
1244 		ath12k_dp_link_peer_free(peer);
1245 	}
1246 
1247 	ar->num_peers = 0;
1248 	ar->num_stations = 0;
1249 
1250 	/* Cleanup rhash table maintained for arsta by iterating over sta */
1251 	ieee80211_iterate_stations_mtx(ar->ah->hw, ath12k_mac_link_sta_rhash_cleanup,
1252 				       ar);
1253 
1254 	/* Delete all the self dp_peers on asserted radio */
1255 	list_for_each_entry_safe_reverse(arvif, tmp_vif, &ar->arvifs, list) {
1256 		if ((arvif->ahvif->vdev_type == WMI_VDEV_TYPE_AP) &&
1257 		    (arvif->link_id < IEEE80211_MLD_MAX_NUM_LINKS)) {
1258 			ath12k_dp_peer_delete(dp_hw, arvif->bssid, NULL);
1259 			arvif->num_stations = 0;
1260 		}
1261 	}
1262 }
1263 
1264 void ath12k_mac_dp_peer_cleanup(struct ath12k_hw *ah)
1265 {
1266 	struct list_head peers;
1267 	struct ath12k_dp_peer *dp_peer, *tmp;
1268 	struct ath12k_dp_hw *dp_hw = &ah->dp_hw;
1269 
1270 	INIT_LIST_HEAD(&peers);
1271 
1272 	spin_lock_bh(&dp_hw->peer_lock);
1273 	list_for_each_entry_safe(dp_peer, tmp, &dp_hw->dp_peers_list, list) {
1274 		if (dp_peer->is_mlo) {
1275 			rcu_assign_pointer(dp_hw->dp_peers[dp_peer->peer_id], NULL);
1276 			clear_bit(dp_peer->peer_id, ah->free_ml_peer_id_map);
1277 		}
1278 
1279 		list_move(&dp_peer->list, &peers);
1280 	}
1281 
1282 	spin_unlock_bh(&dp_hw->peer_lock);
1283 
1284 	synchronize_rcu();
1285 
1286 	list_for_each_entry_safe(dp_peer, tmp, &peers, list) {
1287 		list_del(&dp_peer->list);
1288 		kfree(dp_peer);
1289 	}
1290 }
1291 
1292 static int ath12k_mac_vdev_setup_sync(struct ath12k *ar)
1293 {
1294 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
1295 
1296 	if (test_bit(ATH12K_FLAG_CRASH_FLUSH, &ar->ab->dev_flags))
1297 		return -ESHUTDOWN;
1298 
1299 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "vdev setup timeout %d\n",
1300 		   ATH12K_VDEV_SETUP_TIMEOUT_HZ);
1301 
1302 	if (!wait_for_completion_timeout(&ar->vdev_setup_done,
1303 					 ATH12K_VDEV_SETUP_TIMEOUT_HZ))
1304 		return -ETIMEDOUT;
1305 
1306 	return ar->last_wmi_vdev_start_status ? -EINVAL : 0;
1307 }
1308 
1309 static int ath12k_monitor_vdev_up(struct ath12k *ar, int vdev_id)
1310 {
1311 	struct ath12k_wmi_vdev_up_params params = {};
1312 	int ret;
1313 
1314 	params.vdev_id = vdev_id;
1315 	params.bssid = ar->mac_addr;
1316 	ret = ath12k_wmi_vdev_up(ar, &params);
1317 	if (ret) {
1318 		ath12k_warn(ar->ab, "failed to put up monitor vdev %i: %d\n",
1319 			    vdev_id, ret);
1320 		return ret;
1321 	}
1322 
1323 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac monitor vdev %i started\n",
1324 		   vdev_id);
1325 	return 0;
1326 }
1327 
1328 static int ath12k_mac_monitor_vdev_start(struct ath12k *ar, int vdev_id,
1329 					 struct cfg80211_chan_def *chandef)
1330 {
1331 	struct ieee80211_channel *channel;
1332 	struct wmi_vdev_start_req_arg arg = {};
1333 	struct ath12k_wmi_vdev_up_params params = {};
1334 	int ret;
1335 
1336 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
1337 
1338 	channel = chandef->chan;
1339 	arg.vdev_id = vdev_id;
1340 	arg.freq = channel->center_freq;
1341 	arg.band_center_freq1 = chandef->center_freq1;
1342 	arg.band_center_freq2 = chandef->center_freq2;
1343 	arg.mode = ath12k_phymodes[chandef->chan->band][chandef->width];
1344 	arg.chan_radar = !!(channel->flags & IEEE80211_CHAN_RADAR);
1345 
1346 	arg.min_power = 0;
1347 	arg.max_power = channel->max_power;
1348 	arg.max_reg_power = channel->max_reg_power;
1349 	arg.max_antenna_gain = channel->max_antenna_gain;
1350 
1351 	arg.pref_tx_streams = ar->num_tx_chains;
1352 	arg.pref_rx_streams = ar->num_rx_chains;
1353 	arg.punct_bitmap = 0xFFFFFFFF;
1354 
1355 	arg.passive |= !!(chandef->chan->flags & IEEE80211_CHAN_NO_IR);
1356 
1357 	reinit_completion(&ar->vdev_setup_done);
1358 	reinit_completion(&ar->vdev_delete_done);
1359 
1360 	ret = ath12k_wmi_vdev_start(ar, &arg, false);
1361 	if (ret) {
1362 		ath12k_warn(ar->ab, "failed to request monitor vdev %i start: %d\n",
1363 			    vdev_id, ret);
1364 		return ret;
1365 	}
1366 
1367 	ret = ath12k_mac_vdev_setup_sync(ar);
1368 	if (ret) {
1369 		ath12k_warn(ar->ab, "failed to synchronize setup for monitor vdev %i start: %d\n",
1370 			    vdev_id, ret);
1371 		return ret;
1372 	}
1373 
1374 	params.vdev_id = vdev_id;
1375 	params.bssid = ar->mac_addr;
1376 	ret = ath12k_wmi_vdev_up(ar, &params);
1377 	if (ret) {
1378 		ath12k_warn(ar->ab, "failed to put up monitor vdev %i: %d\n",
1379 			    vdev_id, ret);
1380 		goto vdev_stop;
1381 	}
1382 
1383 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac monitor vdev %i started\n",
1384 		   vdev_id);
1385 	return 0;
1386 
1387 vdev_stop:
1388 	ret = ath12k_wmi_vdev_stop(ar, vdev_id);
1389 	if (ret)
1390 		ath12k_warn(ar->ab, "failed to stop monitor vdev %i after start failure: %d\n",
1391 			    vdev_id, ret);
1392 	return ret;
1393 }
1394 
1395 static int ath12k_mac_monitor_vdev_stop(struct ath12k *ar)
1396 {
1397 	int ret;
1398 
1399 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
1400 
1401 	reinit_completion(&ar->vdev_setup_done);
1402 
1403 	ret = ath12k_wmi_vdev_stop(ar, ar->monitor_vdev_id);
1404 	if (ret)
1405 		ath12k_warn(ar->ab, "failed to request monitor vdev %i stop: %d\n",
1406 			    ar->monitor_vdev_id, ret);
1407 
1408 	ret = ath12k_mac_vdev_setup_sync(ar);
1409 	if (ret)
1410 		ath12k_warn(ar->ab, "failed to synchronize monitor vdev %i stop: %d\n",
1411 			    ar->monitor_vdev_id, ret);
1412 
1413 	ret = ath12k_wmi_vdev_down(ar, ar->monitor_vdev_id);
1414 	if (ret)
1415 		ath12k_warn(ar->ab, "failed to put down monitor vdev %i: %d\n",
1416 			    ar->monitor_vdev_id, ret);
1417 
1418 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac monitor vdev %i stopped\n",
1419 		   ar->monitor_vdev_id);
1420 	return ret;
1421 }
1422 
1423 static int ath12k_mac_monitor_vdev_delete(struct ath12k *ar)
1424 {
1425 	int ret;
1426 	unsigned long time_left;
1427 
1428 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
1429 
1430 	if (!ar->monitor_vdev_created)
1431 		return 0;
1432 
1433 	reinit_completion(&ar->vdev_delete_done);
1434 
1435 	ret = ath12k_wmi_vdev_delete(ar, ar->monitor_vdev_id);
1436 	if (ret) {
1437 		ath12k_warn(ar->ab, "failed to request wmi monitor vdev %i removal: %d\n",
1438 			    ar->monitor_vdev_id, ret);
1439 		return ret;
1440 	}
1441 
1442 	time_left = wait_for_completion_timeout(&ar->vdev_delete_done,
1443 						ATH12K_VDEV_DELETE_TIMEOUT_HZ);
1444 	if (time_left == 0) {
1445 		ath12k_warn(ar->ab, "Timeout in receiving vdev delete response\n");
1446 	} else {
1447 		ar->allocated_vdev_map &= ~(1LL << ar->monitor_vdev_id);
1448 		ar->ab->free_vdev_map |= 1LL << (ar->monitor_vdev_id);
1449 		ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac monitor vdev %d deleted\n",
1450 			   ar->monitor_vdev_id);
1451 		ar->num_created_vdevs--;
1452 		ar->monitor_vdev_id = -1;
1453 		ar->monitor_vdev_created = false;
1454 	}
1455 
1456 	return ret;
1457 }
1458 
1459 static int ath12k_mac_monitor_start(struct ath12k *ar)
1460 {
1461 	struct ath12k_mac_get_any_chanctx_conf_arg arg;
1462 	int ret;
1463 
1464 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
1465 
1466 	if (ar->monitor_started)
1467 		return 0;
1468 
1469 	arg.ar = ar;
1470 	arg.chanctx_conf = NULL;
1471 	ieee80211_iter_chan_contexts_atomic(ath12k_ar_to_hw(ar),
1472 					    ath12k_mac_get_any_chanctx_conf_iter,
1473 					    &arg);
1474 	if (!arg.chanctx_conf)
1475 		return 0;
1476 
1477 	ret = ath12k_mac_monitor_vdev_start(ar, ar->monitor_vdev_id,
1478 					    &arg.chanctx_conf->def);
1479 	if (ret) {
1480 		ath12k_warn(ar->ab, "failed to start monitor vdev: %d\n", ret);
1481 		return ret;
1482 	}
1483 
1484 	ret = ath12k_dp_tx_htt_monitor_mode_ring_config(ar, false);
1485 	if (ret) {
1486 		ath12k_warn(ar->ab, "fail to set monitor filter: %d\n", ret);
1487 		return ret;
1488 	}
1489 
1490 	ar->monitor_started = true;
1491 	ar->num_started_vdevs++;
1492 
1493 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac monitor started\n");
1494 
1495 	return 0;
1496 }
1497 
1498 static int ath12k_mac_monitor_stop(struct ath12k *ar)
1499 {
1500 	int ret;
1501 
1502 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
1503 
1504 	if (!ar->monitor_started)
1505 		return 0;
1506 
1507 	ret = ath12k_mac_monitor_vdev_stop(ar);
1508 	if (ret) {
1509 		ath12k_warn(ar->ab, "failed to stop monitor vdev: %d\n", ret);
1510 		return ret;
1511 	}
1512 
1513 	ar->monitor_started = false;
1514 	ar->num_started_vdevs--;
1515 	ret = ath12k_dp_tx_htt_monitor_mode_ring_config(ar, true);
1516 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac monitor stopped ret %d\n", ret);
1517 	return ret;
1518 }
1519 
1520 int ath12k_mac_vdev_stop(struct ath12k_link_vif *arvif)
1521 {
1522 	struct ath12k_vif *ahvif = arvif->ahvif;
1523 	struct ath12k *ar = arvif->ar;
1524 	int ret;
1525 
1526 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
1527 
1528 	reinit_completion(&ar->vdev_setup_done);
1529 
1530 	ret = ath12k_wmi_vdev_stop(ar, arvif->vdev_id);
1531 	if (ret) {
1532 		ath12k_warn(ar->ab, "failed to stop WMI vdev %i: %d\n",
1533 			    arvif->vdev_id, ret);
1534 		goto err;
1535 	}
1536 
1537 	ret = ath12k_mac_vdev_setup_sync(ar);
1538 	if (ret) {
1539 		ath12k_warn(ar->ab, "failed to synchronize setup for vdev %i: %d\n",
1540 			    arvif->vdev_id, ret);
1541 		goto err;
1542 	}
1543 
1544 	WARN_ON(ar->num_started_vdevs == 0);
1545 
1546 	ar->num_started_vdevs--;
1547 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "vdev %pM stopped, vdev_id %d\n",
1548 		   ahvif->vif->addr, arvif->vdev_id);
1549 
1550 	if (test_bit(ATH12K_FLAG_CAC_RUNNING, &ar->dev_flags)) {
1551 		clear_bit(ATH12K_FLAG_CAC_RUNNING, &ar->dev_flags);
1552 		ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "CAC Stopped for vdev %d\n",
1553 			   arvif->vdev_id);
1554 	}
1555 
1556 	return 0;
1557 err:
1558 	return ret;
1559 }
1560 
1561 int ath12k_mac_op_config(struct ieee80211_hw *hw, int radio_idx, u32 changed)
1562 {
1563 	return 0;
1564 }
1565 EXPORT_SYMBOL(ath12k_mac_op_config);
1566 
1567 static int ath12k_mac_setup_bcn_p2p_ie(struct ath12k_link_vif *arvif,
1568 				       struct sk_buff *bcn)
1569 {
1570 	struct ath12k *ar = arvif->ar;
1571 	struct ieee80211_mgmt *mgmt;
1572 	const u8 *p2p_ie;
1573 	int ret;
1574 
1575 	mgmt = (void *)bcn->data;
1576 	p2p_ie = cfg80211_find_vendor_ie(WLAN_OUI_WFA, WLAN_OUI_TYPE_WFA_P2P,
1577 					 mgmt->u.beacon.variable,
1578 					 bcn->len - (mgmt->u.beacon.variable -
1579 						     bcn->data));
1580 	if (!p2p_ie) {
1581 		ath12k_warn(ar->ab, "no P2P ie found in beacon\n");
1582 		return -ENOENT;
1583 	}
1584 
1585 	ret = ath12k_wmi_p2p_go_bcn_ie(ar, arvif->vdev_id, p2p_ie);
1586 	if (ret) {
1587 		ath12k_warn(ar->ab, "failed to submit P2P GO bcn ie for vdev %i: %d\n",
1588 			    arvif->vdev_id, ret);
1589 		return ret;
1590 	}
1591 
1592 	return 0;
1593 }
1594 
1595 static int ath12k_mac_remove_vendor_ie(struct sk_buff *skb, unsigned int oui,
1596 				       u8 oui_type, size_t ie_offset)
1597 {
1598 	const u8 *next, *end;
1599 	size_t len;
1600 	u8 *ie;
1601 
1602 	if (WARN_ON(skb->len < ie_offset))
1603 		return -EINVAL;
1604 
1605 	ie = (u8 *)cfg80211_find_vendor_ie(oui, oui_type,
1606 					   skb->data + ie_offset,
1607 					   skb->len - ie_offset);
1608 	if (!ie)
1609 		return -ENOENT;
1610 
1611 	len = ie[1] + 2;
1612 	end = skb->data + skb->len;
1613 	next = ie + len;
1614 
1615 	if (WARN_ON(next > end))
1616 		return -EINVAL;
1617 
1618 	memmove(ie, next, end - next);
1619 	skb_trim(skb, skb->len - len);
1620 
1621 	return 0;
1622 }
1623 
1624 static void ath12k_mac_set_arvif_ies(struct ath12k_link_vif *arvif,
1625 				     struct ath12k_link_vif *tx_arvif,
1626 				     struct sk_buff *bcn,
1627 				     u8 bssid_index, bool *nontx_profile_found)
1628 {
1629 	struct ieee80211_mgmt *mgmt = (struct ieee80211_mgmt *)bcn->data;
1630 	const struct element *elem, *nontx, *index, *nie, *ext_cap_ie;
1631 	const u8 *start, *tail;
1632 	u16 rem_len;
1633 	u8 i;
1634 
1635 	start = bcn->data + ieee80211_get_hdrlen_from_skb(bcn) + sizeof(mgmt->u.beacon);
1636 	tail = skb_tail_pointer(bcn);
1637 	rem_len = tail - start;
1638 
1639 	arvif->rsnie_present = false;
1640 	arvif->wpaie_present = false;
1641 
1642 	if (cfg80211_find_ie(WLAN_EID_RSN, start, rem_len))
1643 		arvif->rsnie_present = true;
1644 	if (cfg80211_find_vendor_ie(WLAN_OUI_MICROSOFT, WLAN_OUI_TYPE_MICROSOFT_WPA,
1645 				    start, rem_len))
1646 		arvif->wpaie_present = true;
1647 
1648 	ext_cap_ie = cfg80211_find_elem(WLAN_EID_EXT_CAPABILITY, start, rem_len);
1649 	if (ext_cap_ie && ext_cap_ie->datalen >= 11 &&
1650 	    (ext_cap_ie->data[10] & WLAN_EXT_CAPA11_BCN_PROTECT))
1651 		tx_arvif->beacon_prot = true;
1652 
1653 	/* Return from here for the transmitted profile */
1654 	if (!bssid_index)
1655 		return;
1656 
1657 	/* Initial rsnie_present for the nontransmitted profile is set to be same as that
1658 	 * of the transmitted profile. It will be changed if security configurations are
1659 	 * different.
1660 	 */
1661 	*nontx_profile_found = false;
1662 	for_each_element_id(elem, WLAN_EID_MULTIPLE_BSSID, start, rem_len) {
1663 		/* Fixed minimum MBSSID element length with at least one
1664 		 * nontransmitted BSSID profile is 12 bytes as given below;
1665 		 * 1 (max BSSID indicator) +
1666 		 * 2 (Nontransmitted BSSID profile: Subelement ID + length) +
1667 		 * 4 (Nontransmitted BSSID Capabilities: tag + length + info)
1668 		 * 2 (Nontransmitted BSSID SSID: tag + length)
1669 		 * 3 (Nontransmitted BSSID Index: tag + length + BSSID index
1670 		 */
1671 		if (elem->datalen < 12 || elem->data[0] < 1)
1672 			continue; /* Max BSSID indicator must be >=1 */
1673 
1674 		for_each_element(nontx, elem->data + 1, elem->datalen - 1) {
1675 			start = nontx->data;
1676 
1677 			if (nontx->id != 0 || nontx->datalen < 4)
1678 				continue; /* Invalid nontransmitted profile */
1679 
1680 			if (nontx->data[0] != WLAN_EID_NON_TX_BSSID_CAP ||
1681 			    nontx->data[1] != 2) {
1682 				continue; /* Missing nontransmitted BSS capabilities */
1683 			}
1684 
1685 			if (nontx->data[4] != WLAN_EID_SSID)
1686 				continue; /* Missing SSID for nontransmitted BSS */
1687 
1688 			index = cfg80211_find_elem(WLAN_EID_MULTI_BSSID_IDX,
1689 						   start, nontx->datalen);
1690 			if (!index || index->datalen < 1 || index->data[0] == 0)
1691 				continue; /* Invalid MBSSID Index element */
1692 
1693 			if (index->data[0] == bssid_index) {
1694 				*nontx_profile_found = true;
1695 
1696 				/* Check if nontx BSS has beacon protection enabled */
1697 				if (!tx_arvif->beacon_prot) {
1698 					ext_cap_ie =
1699 					    cfg80211_find_elem(WLAN_EID_EXT_CAPABILITY,
1700 							       nontx->data,
1701 							       nontx->datalen);
1702 					if (ext_cap_ie && ext_cap_ie->datalen >= 11 &&
1703 					    (ext_cap_ie->data[10] &
1704 					     WLAN_EXT_CAPA11_BCN_PROTECT))
1705 						tx_arvif->beacon_prot = true;
1706 				}
1707 
1708 				if (cfg80211_find_ie(WLAN_EID_RSN,
1709 						     nontx->data,
1710 						     nontx->datalen)) {
1711 					arvif->rsnie_present = true;
1712 					return;
1713 				} else if (!arvif->rsnie_present) {
1714 					return; /* Both tx and nontx BSS are open */
1715 				}
1716 
1717 				nie = cfg80211_find_ext_elem(WLAN_EID_EXT_NON_INHERITANCE,
1718 							     nontx->data,
1719 							     nontx->datalen);
1720 				if (!nie || nie->datalen < 2)
1721 					return; /* Invalid non-inheritance element */
1722 
1723 				for (i = 1; i < nie->datalen - 1; i++) {
1724 					if (nie->data[i] == WLAN_EID_RSN) {
1725 						arvif->rsnie_present = false;
1726 						break;
1727 					}
1728 				}
1729 
1730 				return;
1731 			}
1732 		}
1733 	}
1734 }
1735 
1736 static int ath12k_mac_setup_bcn_tmpl_ema(struct ath12k_link_vif *arvif,
1737 					 struct ath12k_link_vif *tx_arvif,
1738 					 u8 bssid_index)
1739 {
1740 	struct ath12k_wmi_bcn_tmpl_ema_arg ema_args;
1741 	struct ieee80211_ema_beacons *beacons;
1742 	bool nontx_profile_found = false;
1743 	int ret = 0;
1744 	u8 i;
1745 
1746 	beacons = ieee80211_beacon_get_template_ema_list(ath12k_ar_to_hw(tx_arvif->ar),
1747 							 tx_arvif->ahvif->vif,
1748 							 tx_arvif->link_id);
1749 	if (!beacons || !beacons->cnt) {
1750 		ath12k_warn(arvif->ar->ab,
1751 			    "failed to get ema beacon templates from mac80211\n");
1752 		return -EPERM;
1753 	}
1754 
1755 	if (tx_arvif == arvif)
1756 		ath12k_mac_set_arvif_ies(arvif, tx_arvif, beacons->bcn[0].skb, 0, NULL);
1757 
1758 	for (i = 0; i < beacons->cnt; i++) {
1759 		if (tx_arvif != arvif && !nontx_profile_found)
1760 			ath12k_mac_set_arvif_ies(arvif, tx_arvif, beacons->bcn[i].skb,
1761 						 bssid_index,
1762 						 &nontx_profile_found);
1763 
1764 		ema_args.bcn_cnt = beacons->cnt;
1765 		ema_args.bcn_index = i;
1766 		ret = ath12k_wmi_bcn_tmpl(tx_arvif, &beacons->bcn[i].offs,
1767 					  beacons->bcn[i].skb, &ema_args);
1768 		if (ret) {
1769 			ath12k_warn(tx_arvif->ar->ab,
1770 				    "failed to set ema beacon template id %i error %d\n",
1771 				    i, ret);
1772 			break;
1773 		}
1774 	}
1775 
1776 	if (tx_arvif != arvif && !nontx_profile_found)
1777 		ath12k_warn(arvif->ar->ab,
1778 			    "nontransmitted bssid index %u not found in beacon template\n",
1779 			    bssid_index);
1780 
1781 	ieee80211_beacon_free_ema_list(beacons);
1782 	return ret;
1783 }
1784 
1785 static int ath12k_mac_setup_bcn_tmpl(struct ath12k_link_vif *arvif)
1786 {
1787 	struct ath12k_vif *ahvif = arvif->ahvif;
1788 	struct ieee80211_vif *vif = ath12k_ahvif_to_vif(ahvif);
1789 	struct ieee80211_bss_conf *link_conf;
1790 	struct ath12k_link_vif *tx_arvif;
1791 	struct ath12k *ar = arvif->ar;
1792 	struct ath12k_base *ab = ar->ab;
1793 	struct ieee80211_mutable_offsets offs = {};
1794 	bool nontx_profile_found = false;
1795 	struct sk_buff *bcn;
1796 	int ret;
1797 
1798 	if (ahvif->vdev_type != WMI_VDEV_TYPE_AP)
1799 		return 0;
1800 
1801 	link_conf = ath12k_mac_get_link_bss_conf(arvif);
1802 	if (!link_conf) {
1803 		ath12k_warn(ar->ab, "unable to access bss link conf to set bcn tmpl for vif %pM link %u\n",
1804 			    vif->addr, arvif->link_id);
1805 		return -ENOLINK;
1806 	}
1807 
1808 	tx_arvif = ath12k_mac_get_tx_arvif(arvif, link_conf);
1809 	if (tx_arvif) {
1810 		if (tx_arvif != arvif && arvif->is_up)
1811 			return 0;
1812 
1813 		if (link_conf->ema_ap)
1814 			return ath12k_mac_setup_bcn_tmpl_ema(arvif, tx_arvif,
1815 							     link_conf->bssid_index);
1816 	} else {
1817 		tx_arvif = arvif;
1818 	}
1819 
1820 	bcn = ieee80211_beacon_get_template(ath12k_ar_to_hw(tx_arvif->ar),
1821 					    tx_arvif->ahvif->vif,
1822 					    &offs, tx_arvif->link_id);
1823 	if (!bcn) {
1824 		ath12k_warn(ab, "failed to get beacon template from mac80211\n");
1825 		return -EPERM;
1826 	}
1827 
1828 	if (tx_arvif == arvif) {
1829 		ath12k_mac_set_arvif_ies(arvif, tx_arvif, bcn, 0, NULL);
1830 	} else {
1831 		ath12k_mac_set_arvif_ies(arvif, tx_arvif, bcn,
1832 					 link_conf->bssid_index,
1833 					 &nontx_profile_found);
1834 		if (!nontx_profile_found)
1835 			ath12k_warn(ab,
1836 				    "nontransmitted profile not found in beacon template\n");
1837 	}
1838 
1839 	if (ahvif->vif->type == NL80211_IFTYPE_AP && ahvif->vif->p2p) {
1840 		ret = ath12k_mac_setup_bcn_p2p_ie(arvif, bcn);
1841 		if (ret) {
1842 			ath12k_warn(ab, "failed to setup P2P GO bcn ie: %d\n",
1843 				    ret);
1844 			goto free_bcn_skb;
1845 		}
1846 
1847 		/* P2P IE is inserted by firmware automatically (as
1848 		 * configured above) so remove it from the base beacon
1849 		 * template to avoid duplicate P2P IEs in beacon frames.
1850 		 */
1851 		ret = ath12k_mac_remove_vendor_ie(bcn, WLAN_OUI_WFA,
1852 						  WLAN_OUI_TYPE_WFA_P2P,
1853 						  offsetof(struct ieee80211_mgmt,
1854 							   u.beacon.variable));
1855 		if (ret) {
1856 			ath12k_warn(ab, "failed to remove P2P vendor ie: %d\n",
1857 				    ret);
1858 			goto free_bcn_skb;
1859 		}
1860 	}
1861 
1862 	ret = ath12k_wmi_bcn_tmpl(arvif, &offs, bcn, NULL);
1863 
1864 	if (ret)
1865 		ath12k_warn(ab, "failed to submit beacon template command: %d\n",
1866 			    ret);
1867 
1868 free_bcn_skb:
1869 	kfree_skb(bcn);
1870 	return ret;
1871 }
1872 
1873 static void ath12k_control_beaconing(struct ath12k_link_vif *arvif,
1874 				     struct ieee80211_bss_conf *info)
1875 {
1876 	struct ath12k_wmi_vdev_up_params params = {};
1877 	struct ath12k_vif *ahvif = arvif->ahvif;
1878 	struct ath12k *ar = arvif->ar;
1879 	int ret;
1880 
1881 	lockdep_assert_wiphy(ath12k_ar_to_hw(arvif->ar)->wiphy);
1882 
1883 	if (!info->enable_beacon) {
1884 		ret = ath12k_wmi_vdev_down(ar, arvif->vdev_id);
1885 		if (ret)
1886 			ath12k_warn(ar->ab, "failed to down vdev_id %i: %d\n",
1887 				    arvif->vdev_id, ret);
1888 
1889 		arvif->is_up = false;
1890 		return;
1891 	}
1892 
1893 	/* Install the beacon template to the FW */
1894 	ret = ath12k_mac_setup_bcn_tmpl(arvif);
1895 	if (ret) {
1896 		ath12k_warn(ar->ab, "failed to update bcn tmpl during vdev up: %d\n",
1897 			    ret);
1898 		return;
1899 	}
1900 
1901 	ahvif->aid = 0;
1902 
1903 	ether_addr_copy(arvif->bssid, info->addr);
1904 
1905 	params.vdev_id = arvif->vdev_id;
1906 	params.aid = ahvif->aid;
1907 	params.bssid = arvif->bssid;
1908 	params.tx_bssid = ath12k_mac_get_tx_bssid(arvif);
1909 	if (params.tx_bssid) {
1910 		params.nontx_profile_idx = info->bssid_index;
1911 		params.nontx_profile_cnt = 1 << info->bssid_indicator;
1912 	}
1913 	ret = ath12k_wmi_vdev_up(arvif->ar, &params);
1914 	if (ret) {
1915 		ath12k_warn(ar->ab, "failed to bring up vdev %d: %i\n",
1916 			    arvif->vdev_id, ret);
1917 		return;
1918 	}
1919 
1920 	arvif->is_up = true;
1921 
1922 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac vdev %d up\n", arvif->vdev_id);
1923 }
1924 
1925 static void ath12k_mac_handle_beacon_iter(void *data, u8 *mac,
1926 					  struct ieee80211_vif *vif)
1927 {
1928 	struct sk_buff *skb = data;
1929 	struct ieee80211_mgmt *mgmt = (void *)skb->data;
1930 	struct ath12k_vif *ahvif = ath12k_vif_to_ahvif(vif);
1931 	struct ath12k_link_vif *arvif = &ahvif->deflink;
1932 
1933 	if (vif->type != NL80211_IFTYPE_STATION || !arvif->is_created)
1934 		return;
1935 
1936 	if (!ether_addr_equal(mgmt->bssid, vif->bss_conf.bssid))
1937 		return;
1938 
1939 	cancel_delayed_work(&arvif->connection_loss_work);
1940 }
1941 
1942 void ath12k_mac_handle_beacon(struct ath12k *ar, struct sk_buff *skb)
1943 {
1944 	ieee80211_iterate_active_interfaces_atomic(ath12k_ar_to_hw(ar),
1945 						   IEEE80211_IFACE_ITER_NORMAL,
1946 						   ath12k_mac_handle_beacon_iter,
1947 						   skb);
1948 }
1949 
1950 void ath12k_mac_handle_beacon_miss(struct ath12k *ar,
1951 				   struct ath12k_link_vif *arvif)
1952 {
1953 	struct ieee80211_hw *hw = ath12k_ar_to_hw(ar);
1954 	struct ieee80211_vif *vif = ath12k_ahvif_to_vif(arvif->ahvif);
1955 
1956 	if (!(arvif->is_created && arvif->is_up))
1957 		return;
1958 
1959 	ieee80211_beacon_loss(vif);
1960 
1961 	/* Firmware doesn't report beacon loss events repeatedly. If AP probe
1962 	 * (done by mac80211) succeeds but beacons do not resume then it
1963 	 * doesn't make sense to continue operation. Queue connection loss work
1964 	 * which can be cancelled when beacon is received.
1965 	 */
1966 	ieee80211_queue_delayed_work(hw, &arvif->connection_loss_work,
1967 				     ATH12K_CONNECTION_LOSS_HZ);
1968 }
1969 
1970 static void ath12k_mac_vif_sta_connection_loss_work(struct work_struct *work)
1971 {
1972 	struct ath12k_link_vif *arvif = container_of(work, struct ath12k_link_vif,
1973 						     connection_loss_work.work);
1974 	struct ieee80211_vif *vif = arvif->ahvif->vif;
1975 
1976 	if (!arvif->is_up)
1977 		return;
1978 
1979 	ieee80211_connection_loss(vif);
1980 }
1981 
1982 static void ath12k_peer_assoc_h_basic(struct ath12k *ar,
1983 				      struct ath12k_link_vif *arvif,
1984 				      struct ath12k_link_sta *arsta,
1985 				      struct ath12k_wmi_peer_assoc_arg *arg)
1986 {
1987 	struct ieee80211_vif *vif = ath12k_ahvif_to_vif(arvif->ahvif);
1988 	struct ieee80211_sta *sta = ath12k_ahsta_to_sta(arsta->ahsta);
1989 	struct ieee80211_hw *hw = ath12k_ar_to_hw(ar);
1990 	struct ieee80211_bss_conf *bss_conf;
1991 	u32 aid;
1992 
1993 	lockdep_assert_wiphy(hw->wiphy);
1994 
1995 	if (vif->type == NL80211_IFTYPE_STATION)
1996 		aid = vif->cfg.aid;
1997 	else
1998 		aid = sta->aid;
1999 
2000 	ether_addr_copy(arg->peer_mac, arsta->addr);
2001 	arg->vdev_id = arvif->vdev_id;
2002 	arg->peer_associd = aid;
2003 	arg->auth_flag = true;
2004 	/* TODO: STA WAR in ath10k for listen interval required? */
2005 	arg->peer_listen_intval = hw->conf.listen_interval;
2006 	arg->peer_nss = 1;
2007 
2008 	bss_conf = ath12k_mac_get_link_bss_conf(arvif);
2009 	if (!bss_conf) {
2010 		ath12k_warn(ar->ab, "unable to access bss link conf in peer assoc for vif %pM link %u\n",
2011 			    vif->addr, arvif->link_id);
2012 		return;
2013 	}
2014 
2015 	arg->peer_caps = bss_conf->assoc_capability;
2016 }
2017 
2018 static void ath12k_peer_assoc_h_crypto(struct ath12k *ar,
2019 				       struct ath12k_link_vif *arvif,
2020 				       struct ath12k_link_sta *arsta,
2021 				       struct ath12k_wmi_peer_assoc_arg *arg)
2022 {
2023 	struct ieee80211_vif *vif = ath12k_ahvif_to_vif(arvif->ahvif);
2024 	struct ieee80211_sta *sta = ath12k_ahsta_to_sta(arsta->ahsta);
2025 	struct ieee80211_bss_conf *info;
2026 	struct cfg80211_chan_def def;
2027 	struct cfg80211_bss *bss;
2028 	struct ieee80211_hw *hw = ath12k_ar_to_hw(ar);
2029 	const u8 *rsnie = NULL;
2030 	const u8 *wpaie = NULL;
2031 
2032 	lockdep_assert_wiphy(hw->wiphy);
2033 
2034 	info = ath12k_mac_get_link_bss_conf(arvif);
2035 	if (!info) {
2036 		ath12k_warn(ar->ab, "unable to access bss link conf for peer assoc crypto for vif %pM link %u\n",
2037 			    vif->addr, arvif->link_id);
2038 		return;
2039 	}
2040 
2041 	if (WARN_ON(ath12k_mac_vif_link_chan(vif, arvif->link_id, &def)))
2042 		return;
2043 
2044 	bss = cfg80211_get_bss(hw->wiphy, def.chan, info->bssid, NULL, 0,
2045 			       IEEE80211_BSS_TYPE_ANY, IEEE80211_PRIVACY_ANY);
2046 
2047 	if (arvif->rsnie_present || arvif->wpaie_present) {
2048 		arg->need_ptk_4_way = true;
2049 		if (arvif->wpaie_present)
2050 			arg->need_gtk_2_way = true;
2051 	} else if (bss) {
2052 		const struct cfg80211_bss_ies *ies;
2053 
2054 		rcu_read_lock();
2055 		rsnie = ieee80211_bss_get_ie(bss, WLAN_EID_RSN);
2056 
2057 		ies = rcu_dereference(bss->ies);
2058 
2059 		wpaie = cfg80211_find_vendor_ie(WLAN_OUI_MICROSOFT,
2060 						WLAN_OUI_TYPE_MICROSOFT_WPA,
2061 						ies->data,
2062 						ies->len);
2063 		rcu_read_unlock();
2064 		cfg80211_put_bss(hw->wiphy, bss);
2065 	}
2066 
2067 	/* FIXME: base on RSN IE/WPA IE is a correct idea? */
2068 	if (rsnie || wpaie) {
2069 		ath12k_dbg(ar->ab, ATH12K_DBG_WMI,
2070 			   "%s: rsn ie found\n", __func__);
2071 		arg->need_ptk_4_way = true;
2072 	}
2073 
2074 	if (wpaie) {
2075 		ath12k_dbg(ar->ab, ATH12K_DBG_WMI,
2076 			   "%s: wpa ie found\n", __func__);
2077 		arg->need_gtk_2_way = true;
2078 	}
2079 
2080 	if (sta->mfp) {
2081 		/* TODO: Need to check if FW supports PMF? */
2082 		arg->is_pmf_enabled = true;
2083 	}
2084 
2085 	/* TODO: safe_mode_enabled (bypass 4-way handshake) flag req? */
2086 }
2087 
2088 static void ath12k_peer_assoc_h_rates(struct ath12k *ar,
2089 				      struct ath12k_link_vif *arvif,
2090 				      struct ath12k_link_sta *arsta,
2091 				      struct ath12k_wmi_peer_assoc_arg *arg)
2092 {
2093 	struct ieee80211_vif *vif = ath12k_ahvif_to_vif(arvif->ahvif);
2094 	struct ieee80211_sta *sta = ath12k_ahsta_to_sta(arsta->ahsta);
2095 	struct wmi_rate_set_arg *rateset = &arg->peer_legacy_rates;
2096 	struct ieee80211_link_sta *link_sta;
2097 	struct cfg80211_chan_def def;
2098 	const struct ieee80211_supported_band *sband;
2099 	const struct ieee80211_rate *rates;
2100 	struct ieee80211_hw *hw = ath12k_ar_to_hw(ar);
2101 	enum nl80211_band band;
2102 	u32 ratemask;
2103 	u8 rate;
2104 	int i;
2105 
2106 	lockdep_assert_wiphy(hw->wiphy);
2107 
2108 	if (WARN_ON(ath12k_mac_vif_link_chan(vif, arvif->link_id, &def)))
2109 		return;
2110 
2111 	link_sta = ath12k_mac_get_link_sta(arsta);
2112 	if (!link_sta) {
2113 		ath12k_warn(ar->ab, "unable to access link sta in peer assoc rates for sta %pM link %u\n",
2114 			    sta->addr, arsta->link_id);
2115 		return;
2116 	}
2117 
2118 	band = def.chan->band;
2119 	sband = hw->wiphy->bands[band];
2120 	ratemask = link_sta->supp_rates[band];
2121 	ratemask &= arvif->bitrate_mask.control[band].legacy;
2122 	rates = sband->bitrates;
2123 
2124 	rateset->num_rates = 0;
2125 
2126 	for (i = 0; i < 32; i++, ratemask >>= 1, rates++) {
2127 		if (!(ratemask & 1))
2128 			continue;
2129 
2130 		rate = ath12k_mac_bitrate_to_rate(rates->bitrate);
2131 		rateset->rates[rateset->num_rates] = rate;
2132 		rateset->num_rates++;
2133 	}
2134 }
2135 
2136 static bool
2137 ath12k_peer_assoc_h_ht_masked(const u8 *ht_mcs_mask)
2138 {
2139 	int nss;
2140 
2141 	for (nss = 0; nss < IEEE80211_HT_MCS_MASK_LEN; nss++)
2142 		if (ht_mcs_mask[nss])
2143 			return false;
2144 
2145 	return true;
2146 }
2147 
2148 static bool
2149 ath12k_peer_assoc_h_vht_masked(const u16 *vht_mcs_mask)
2150 {
2151 	int nss;
2152 
2153 	for (nss = 0; nss < NL80211_VHT_NSS_MAX; nss++)
2154 		if (vht_mcs_mask[nss])
2155 			return false;
2156 
2157 	return true;
2158 }
2159 
2160 static void ath12k_peer_assoc_h_ht(struct ath12k *ar,
2161 				   struct ath12k_link_vif *arvif,
2162 				   struct ath12k_link_sta *arsta,
2163 				   struct ath12k_wmi_peer_assoc_arg *arg)
2164 {
2165 	struct ieee80211_vif *vif = ath12k_ahvif_to_vif(arvif->ahvif);
2166 	struct ieee80211_sta *sta = ath12k_ahsta_to_sta(arsta->ahsta);
2167 	const struct ieee80211_sta_ht_cap *ht_cap;
2168 	struct ieee80211_link_sta *link_sta;
2169 	struct cfg80211_chan_def def;
2170 	enum nl80211_band band;
2171 	const u8 *ht_mcs_mask;
2172 	int i, n;
2173 	u8 max_nss;
2174 	u32 stbc;
2175 
2176 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
2177 
2178 	if (WARN_ON(ath12k_mac_vif_link_chan(vif, arvif->link_id, &def)))
2179 		return;
2180 
2181 	link_sta = ath12k_mac_get_link_sta(arsta);
2182 	if (!link_sta) {
2183 		ath12k_warn(ar->ab, "unable to access link sta in peer assoc ht for sta %pM link %u\n",
2184 			    sta->addr, arsta->link_id);
2185 		return;
2186 	}
2187 
2188 	ht_cap = &link_sta->ht_cap;
2189 	if (!ht_cap->ht_supported)
2190 		return;
2191 
2192 	band = def.chan->band;
2193 	ht_mcs_mask = arvif->bitrate_mask.control[band].ht_mcs;
2194 
2195 	if (ath12k_peer_assoc_h_ht_masked(ht_mcs_mask))
2196 		return;
2197 
2198 	arg->ht_flag = true;
2199 
2200 	arg->peer_max_mpdu = (1 << (IEEE80211_HT_MAX_AMPDU_FACTOR +
2201 				    ht_cap->ampdu_factor)) - 1;
2202 
2203 	arg->peer_mpdu_density =
2204 		ath12k_parse_mpdudensity(ht_cap->ampdu_density);
2205 
2206 	arg->peer_ht_caps = ht_cap->cap;
2207 	arg->peer_rate_caps |= WMI_HOST_RC_HT_FLAG;
2208 
2209 	if (ht_cap->cap & IEEE80211_HT_CAP_LDPC_CODING)
2210 		arg->ldpc_flag = true;
2211 
2212 	if (link_sta->bandwidth >= IEEE80211_STA_RX_BW_40) {
2213 		arg->bw_40 = true;
2214 		arg->peer_rate_caps |= WMI_HOST_RC_CW40_FLAG;
2215 	}
2216 
2217 	/* As firmware handles these two flags (IEEE80211_HT_CAP_SGI_20
2218 	 * and IEEE80211_HT_CAP_SGI_40) for enabling SGI, reset both
2219 	 * flags if guard interval is to force Long GI
2220 	 */
2221 	if (arvif->bitrate_mask.control[band].gi == NL80211_TXRATE_FORCE_LGI) {
2222 		arg->peer_ht_caps &= ~(IEEE80211_HT_CAP_SGI_20 | IEEE80211_HT_CAP_SGI_40);
2223 	} else {
2224 		/* Enable SGI flag if either SGI_20 or SGI_40 is supported */
2225 		if (ht_cap->cap & (IEEE80211_HT_CAP_SGI_20 | IEEE80211_HT_CAP_SGI_40))
2226 			arg->peer_rate_caps |= WMI_HOST_RC_SGI_FLAG;
2227 	}
2228 
2229 	if (ht_cap->cap & IEEE80211_HT_CAP_TX_STBC) {
2230 		arg->peer_rate_caps |= WMI_HOST_RC_TX_STBC_FLAG;
2231 		arg->stbc_flag = true;
2232 	}
2233 
2234 	if (ht_cap->cap & IEEE80211_HT_CAP_RX_STBC) {
2235 		stbc = ht_cap->cap & IEEE80211_HT_CAP_RX_STBC;
2236 		stbc = stbc >> IEEE80211_HT_CAP_RX_STBC_SHIFT;
2237 		stbc = stbc << WMI_HOST_RC_RX_STBC_FLAG_S;
2238 		arg->peer_rate_caps |= stbc;
2239 		arg->stbc_flag = true;
2240 	}
2241 
2242 	if (ht_cap->mcs.rx_mask[1] && ht_cap->mcs.rx_mask[2])
2243 		arg->peer_rate_caps |= WMI_HOST_RC_TS_FLAG;
2244 	else if (ht_cap->mcs.rx_mask[1])
2245 		arg->peer_rate_caps |= WMI_HOST_RC_DS_FLAG;
2246 
2247 	for (i = 0, n = 0, max_nss = 0; i < IEEE80211_HT_MCS_MASK_LEN * 8; i++)
2248 		if ((ht_cap->mcs.rx_mask[i / 8] & BIT(i % 8)) &&
2249 		    (ht_mcs_mask[i / 8] & BIT(i % 8))) {
2250 			max_nss = (i / 8) + 1;
2251 			arg->peer_ht_rates.rates[n++] = i;
2252 		}
2253 
2254 	/* This is a workaround for HT-enabled STAs which break the spec
2255 	 * and have no HT capabilities RX mask (no HT RX MCS map).
2256 	 *
2257 	 * As per spec, in section 20.3.5 Modulation and coding scheme (MCS),
2258 	 * MCS 0 through 7 are mandatory in 20MHz with 800 ns GI at all STAs.
2259 	 *
2260 	 * Firmware asserts if such situation occurs.
2261 	 */
2262 	if (n == 0) {
2263 		arg->peer_ht_rates.num_rates = 8;
2264 		for (i = 0; i < arg->peer_ht_rates.num_rates; i++)
2265 			arg->peer_ht_rates.rates[i] = i;
2266 	} else {
2267 		arg->peer_ht_rates.num_rates = n;
2268 		arg->peer_nss = min(link_sta->rx_nss, max_nss);
2269 	}
2270 
2271 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac ht peer %pM mcs cnt %d nss %d\n",
2272 		   arg->peer_mac,
2273 		   arg->peer_ht_rates.num_rates,
2274 		   arg->peer_nss);
2275 }
2276 
2277 static int ath12k_mac_get_max_vht_mcs_map(u16 mcs_map, int nss)
2278 {
2279 	switch ((mcs_map >> (2 * nss)) & 0x3) {
2280 	case IEEE80211_VHT_MCS_SUPPORT_0_7: return BIT(8) - 1;
2281 	case IEEE80211_VHT_MCS_SUPPORT_0_8: return BIT(9) - 1;
2282 	case IEEE80211_VHT_MCS_SUPPORT_0_9: return BIT(10) - 1;
2283 	}
2284 	return 0;
2285 }
2286 
2287 static u16
2288 ath12k_peer_assoc_h_vht_limit(u16 tx_mcs_set,
2289 			      const u16 vht_mcs_limit[NL80211_VHT_NSS_MAX])
2290 {
2291 	int idx_limit;
2292 	int nss;
2293 	u16 mcs_map;
2294 	u16 mcs;
2295 
2296 	for (nss = 0; nss < NL80211_VHT_NSS_MAX; nss++) {
2297 		mcs_map = ath12k_mac_get_max_vht_mcs_map(tx_mcs_set, nss) &
2298 			  vht_mcs_limit[nss];
2299 
2300 		if (mcs_map)
2301 			idx_limit = fls(mcs_map) - 1;
2302 		else
2303 			idx_limit = -1;
2304 
2305 		switch (idx_limit) {
2306 		case 0:
2307 		case 1:
2308 		case 2:
2309 		case 3:
2310 		case 4:
2311 		case 5:
2312 		case 6:
2313 		case 7:
2314 			mcs = IEEE80211_VHT_MCS_SUPPORT_0_7;
2315 			break;
2316 		case 8:
2317 			mcs = IEEE80211_VHT_MCS_SUPPORT_0_8;
2318 			break;
2319 		case 9:
2320 			mcs = IEEE80211_VHT_MCS_SUPPORT_0_9;
2321 			break;
2322 		default:
2323 			WARN_ON(1);
2324 			fallthrough;
2325 		case -1:
2326 			mcs = IEEE80211_VHT_MCS_NOT_SUPPORTED;
2327 			break;
2328 		}
2329 
2330 		tx_mcs_set &= ~(0x3 << (nss * 2));
2331 		tx_mcs_set |= mcs << (nss * 2);
2332 	}
2333 
2334 	return tx_mcs_set;
2335 }
2336 
2337 static u8 ath12k_get_nss_160mhz(struct ath12k *ar,
2338 				u8 max_nss)
2339 {
2340 	u8 nss_ratio_info = ar->pdev->cap.nss_ratio_info;
2341 	u8 max_sup_nss = 0;
2342 
2343 	switch (nss_ratio_info) {
2344 	case WMI_NSS_RATIO_1BY2_NSS:
2345 		max_sup_nss = max_nss >> 1;
2346 		break;
2347 	case WMI_NSS_RATIO_3BY4_NSS:
2348 		ath12k_warn(ar->ab, "WMI_NSS_RATIO_3BY4_NSS not supported\n");
2349 		break;
2350 	case WMI_NSS_RATIO_1_NSS:
2351 		max_sup_nss = max_nss;
2352 		break;
2353 	case WMI_NSS_RATIO_2_NSS:
2354 		ath12k_warn(ar->ab, "WMI_NSS_RATIO_2_NSS not supported\n");
2355 		break;
2356 	default:
2357 		ath12k_warn(ar->ab, "invalid nss ratio received from fw: %d\n",
2358 			    nss_ratio_info);
2359 		break;
2360 	}
2361 
2362 	return max_sup_nss;
2363 }
2364 
2365 static void ath12k_peer_assoc_h_vht(struct ath12k *ar,
2366 				    struct ath12k_link_vif *arvif,
2367 				    struct ath12k_link_sta *arsta,
2368 				    struct ath12k_wmi_peer_assoc_arg *arg)
2369 {
2370 	struct ieee80211_vif *vif = ath12k_ahvif_to_vif(arvif->ahvif);
2371 	struct ieee80211_sta *sta = ath12k_ahsta_to_sta(arsta->ahsta);
2372 	const struct ieee80211_sta_vht_cap *vht_cap;
2373 	struct ieee80211_link_sta *link_sta;
2374 	struct cfg80211_chan_def def;
2375 	enum nl80211_band band;
2376 	u16 *vht_mcs_mask;
2377 	u8 ampdu_factor;
2378 	u8 max_nss, vht_mcs;
2379 	int i, vht_nss, nss_idx;
2380 	bool user_rate_valid = true;
2381 	u32 rx_nss, tx_nss, nss_160;
2382 
2383 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
2384 
2385 	if (WARN_ON(ath12k_mac_vif_link_chan(vif, arvif->link_id, &def)))
2386 		return;
2387 
2388 	link_sta = ath12k_mac_get_link_sta(arsta);
2389 	if (!link_sta) {
2390 		ath12k_warn(ar->ab, "unable to access link sta in peer assoc vht for sta %pM link %u\n",
2391 			    sta->addr, arsta->link_id);
2392 		return;
2393 	}
2394 
2395 	vht_cap = &link_sta->vht_cap;
2396 	if (!vht_cap->vht_supported)
2397 		return;
2398 
2399 	band = def.chan->band;
2400 	vht_mcs_mask = arvif->bitrate_mask.control[band].vht_mcs;
2401 
2402 	if (ath12k_peer_assoc_h_vht_masked(vht_mcs_mask))
2403 		return;
2404 
2405 	arg->vht_flag = true;
2406 
2407 	/* TODO: similar flags required? */
2408 	arg->vht_capable = true;
2409 
2410 	if (def.chan->band == NL80211_BAND_2GHZ)
2411 		arg->vht_ng_flag = true;
2412 
2413 	arg->peer_vht_caps = vht_cap->cap;
2414 
2415 	ampdu_factor = (vht_cap->cap &
2416 			IEEE80211_VHT_CAP_MAX_A_MPDU_LENGTH_EXPONENT_MASK) >>
2417 		       IEEE80211_VHT_CAP_MAX_A_MPDU_LENGTH_EXPONENT_SHIFT;
2418 
2419 	/* Workaround: Some Netgear/Linksys 11ac APs set Rx A-MPDU factor to
2420 	 * zero in VHT IE. Using it would result in degraded throughput.
2421 	 * arg->peer_max_mpdu at this point contains HT max_mpdu so keep
2422 	 * it if VHT max_mpdu is smaller.
2423 	 */
2424 	arg->peer_max_mpdu = max(arg->peer_max_mpdu,
2425 				 (1U << (IEEE80211_HT_MAX_AMPDU_FACTOR +
2426 					ampdu_factor)) - 1);
2427 
2428 	if (link_sta->bandwidth == IEEE80211_STA_RX_BW_80)
2429 		arg->bw_80 = true;
2430 
2431 	if (link_sta->bandwidth == IEEE80211_STA_RX_BW_160)
2432 		arg->bw_160 = true;
2433 
2434 	vht_nss =  ath12k_mac_max_vht_nss(vht_mcs_mask);
2435 
2436 	if (vht_nss > link_sta->rx_nss) {
2437 		user_rate_valid = false;
2438 		for (nss_idx = link_sta->rx_nss - 1; nss_idx >= 0; nss_idx--) {
2439 			if (vht_mcs_mask[nss_idx]) {
2440 				user_rate_valid = true;
2441 				break;
2442 			}
2443 		}
2444 	}
2445 
2446 	if (!user_rate_valid) {
2447 		ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
2448 			   "Setting vht range MCS value to peer supported nss:%d for peer %pM\n",
2449 			   link_sta->rx_nss, arsta->addr);
2450 		vht_mcs_mask[link_sta->rx_nss - 1] = vht_mcs_mask[vht_nss - 1];
2451 	}
2452 
2453 	/* Calculate peer NSS capability from VHT capabilities if STA
2454 	 * supports VHT.
2455 	 */
2456 	for (i = 0, max_nss = 0, vht_mcs = 0; i < NL80211_VHT_NSS_MAX; i++) {
2457 		vht_mcs = __le16_to_cpu(vht_cap->vht_mcs.rx_mcs_map) >>
2458 			  (2 * i) & 3;
2459 
2460 		if (vht_mcs != IEEE80211_VHT_MCS_NOT_SUPPORTED &&
2461 		    vht_mcs_mask[i])
2462 			max_nss = i + 1;
2463 	}
2464 	arg->peer_nss = min(link_sta->rx_nss, max_nss);
2465 	arg->rx_max_rate = __le16_to_cpu(vht_cap->vht_mcs.rx_highest);
2466 	arg->rx_mcs_set = __le16_to_cpu(vht_cap->vht_mcs.rx_mcs_map);
2467 	arg->rx_mcs_set = ath12k_peer_assoc_h_vht_limit(arg->rx_mcs_set, vht_mcs_mask);
2468 
2469 	arg->tx_max_rate = __le16_to_cpu(vht_cap->vht_mcs.tx_highest);
2470 	arg->tx_mcs_set = __le16_to_cpu(vht_cap->vht_mcs.tx_mcs_map);
2471 
2472 	/* In QCN9274 platform, VHT MCS rate 10 and 11 is enabled by default.
2473 	 * VHT MCS rate 10 and 11 is not supported in 11ac standard.
2474 	 * so explicitly disable the VHT MCS rate 10 and 11 in 11ac mode.
2475 	 */
2476 	arg->tx_mcs_set &= ~IEEE80211_VHT_MCS_SUPPORT_0_11_MASK;
2477 	arg->tx_mcs_set |= IEEE80211_DISABLE_VHT_MCS_SUPPORT_0_11;
2478 
2479 	if ((arg->tx_mcs_set & IEEE80211_VHT_MCS_NOT_SUPPORTED) ==
2480 			IEEE80211_VHT_MCS_NOT_SUPPORTED)
2481 		arg->peer_vht_caps &= ~IEEE80211_VHT_CAP_MU_BEAMFORMEE_CAPABLE;
2482 
2483 	/* TODO:  Check */
2484 	arg->tx_max_mcs_nss = 0xFF;
2485 
2486 	if (arg->peer_phymode == MODE_11AC_VHT160) {
2487 		tx_nss = ath12k_get_nss_160mhz(ar, max_nss);
2488 		rx_nss = min(arg->peer_nss, tx_nss);
2489 		arg->peer_bw_rxnss_override = ATH12K_BW_NSS_MAP_ENABLE;
2490 
2491 		if (!rx_nss) {
2492 			ath12k_warn(ar->ab, "invalid max_nss\n");
2493 			return;
2494 		}
2495 
2496 		nss_160 = u32_encode_bits(rx_nss - 1, ATH12K_PEER_RX_NSS_160MHZ);
2497 		arg->peer_bw_rxnss_override |= nss_160;
2498 	}
2499 
2500 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
2501 		   "mac vht peer %pM max_mpdu %d flags 0x%x nss_override 0x%x\n",
2502 		   arsta->addr, arg->peer_max_mpdu, arg->peer_flags,
2503 		   arg->peer_bw_rxnss_override);
2504 }
2505 
2506 static int ath12k_mac_get_max_he_mcs_map(u16 mcs_map, int nss)
2507 {
2508 	switch ((mcs_map >> (2 * nss)) & 0x3) {
2509 	case IEEE80211_HE_MCS_SUPPORT_0_7: return BIT(8) - 1;
2510 	case IEEE80211_HE_MCS_SUPPORT_0_9: return BIT(10) - 1;
2511 	case IEEE80211_HE_MCS_SUPPORT_0_11: return BIT(12) - 1;
2512 	}
2513 	return 0;
2514 }
2515 
2516 static u16 ath12k_peer_assoc_h_he_limit(u16 tx_mcs_set,
2517 					const u16 *he_mcs_limit)
2518 {
2519 	int idx_limit;
2520 	int nss;
2521 	u16 mcs_map;
2522 	u16 mcs;
2523 
2524 	for (nss = 0; nss < NL80211_HE_NSS_MAX; nss++) {
2525 		mcs_map = ath12k_mac_get_max_he_mcs_map(tx_mcs_set, nss) &
2526 			he_mcs_limit[nss];
2527 
2528 		if (mcs_map)
2529 			idx_limit = fls(mcs_map) - 1;
2530 		else
2531 			idx_limit = -1;
2532 
2533 		switch (idx_limit) {
2534 		case 0 ... 7:
2535 			mcs = IEEE80211_HE_MCS_SUPPORT_0_7;
2536 			break;
2537 		case 8:
2538 		case 9:
2539 			mcs = IEEE80211_HE_MCS_SUPPORT_0_9;
2540 			break;
2541 		case 10:
2542 		case 11:
2543 			mcs = IEEE80211_HE_MCS_SUPPORT_0_11;
2544 			break;
2545 		default:
2546 			WARN_ON(1);
2547 			fallthrough;
2548 		case -1:
2549 			mcs = IEEE80211_HE_MCS_NOT_SUPPORTED;
2550 			break;
2551 		}
2552 
2553 		tx_mcs_set &= ~(0x3 << (nss * 2));
2554 		tx_mcs_set |= mcs << (nss * 2);
2555 	}
2556 
2557 	return tx_mcs_set;
2558 }
2559 
2560 static bool
2561 ath12k_peer_assoc_h_he_masked(const u16 he_mcs_mask[NL80211_HE_NSS_MAX])
2562 {
2563 	int nss;
2564 
2565 	for (nss = 0; nss < NL80211_HE_NSS_MAX; nss++)
2566 		if (he_mcs_mask[nss])
2567 			return false;
2568 
2569 	return true;
2570 }
2571 
2572 static void ath12k_peer_assoc_h_he(struct ath12k *ar,
2573 				   struct ath12k_link_vif *arvif,
2574 				   struct ath12k_link_sta *arsta,
2575 				   struct ath12k_wmi_peer_assoc_arg *arg)
2576 {
2577 	struct ieee80211_vif *vif = ath12k_ahvif_to_vif(arvif->ahvif);
2578 	struct ieee80211_sta *sta = ath12k_ahsta_to_sta(arsta->ahsta);
2579 	const struct ieee80211_sta_he_cap *he_cap;
2580 	struct ieee80211_bss_conf *link_conf;
2581 	struct ieee80211_link_sta *link_sta;
2582 	struct cfg80211_chan_def def;
2583 	int i;
2584 	u8 ampdu_factor, max_nss;
2585 	u8 rx_mcs_80 = IEEE80211_HE_MCS_NOT_SUPPORTED;
2586 	u8 rx_mcs_160 = IEEE80211_HE_MCS_NOT_SUPPORTED;
2587 	u16 mcs_160_map, mcs_80_map;
2588 	u8 link_id = arvif->link_id;
2589 	bool support_160;
2590 	enum nl80211_band band;
2591 	u16 *he_mcs_mask;
2592 	u8 he_mcs;
2593 	u16 he_tx_mcs = 0, v = 0;
2594 	int he_nss, nss_idx;
2595 	bool user_rate_valid = true;
2596 	u32 rx_nss, tx_nss, nss_160;
2597 
2598 	if (WARN_ON(ath12k_mac_vif_link_chan(vif, link_id, &def)))
2599 		return;
2600 
2601 	link_conf = ath12k_mac_get_link_bss_conf(arvif);
2602 	if (!link_conf) {
2603 		ath12k_warn(ar->ab, "unable to access bss link conf in peer assoc he for vif %pM link %u",
2604 			    vif->addr, link_id);
2605 		return;
2606 	}
2607 
2608 	link_sta = ath12k_mac_get_link_sta(arsta);
2609 	if (!link_sta) {
2610 		ath12k_warn(ar->ab, "unable to access link sta in peer assoc he for sta %pM link %u\n",
2611 			    sta->addr, arsta->link_id);
2612 		return;
2613 	}
2614 
2615 	he_cap = &link_sta->he_cap;
2616 	if (!he_cap->has_he)
2617 		return;
2618 
2619 	band = def.chan->band;
2620 	he_mcs_mask = arvif->bitrate_mask.control[band].he_mcs;
2621 
2622 	if (ath12k_peer_assoc_h_he_masked(he_mcs_mask))
2623 		return;
2624 
2625 	arg->he_flag = true;
2626 
2627 	support_160 = !!(he_cap->he_cap_elem.phy_cap_info[0] &
2628 		  IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_160MHZ_IN_5G);
2629 
2630 	/* Supported HE-MCS and NSS Set of peer he_cap is intersection with self he_cp */
2631 	mcs_160_map = le16_to_cpu(he_cap->he_mcs_nss_supp.rx_mcs_160);
2632 	mcs_80_map = le16_to_cpu(he_cap->he_mcs_nss_supp.rx_mcs_80);
2633 
2634 	if (support_160) {
2635 		for (i = 7; i >= 0; i--) {
2636 			u8 mcs_160 = (mcs_160_map >> (2 * i)) & 3;
2637 
2638 			if (mcs_160 != IEEE80211_HE_MCS_NOT_SUPPORTED) {
2639 				rx_mcs_160 = i + 1;
2640 				break;
2641 			}
2642 		}
2643 	}
2644 
2645 	for (i = 7; i >= 0; i--) {
2646 		u8 mcs_80 = (mcs_80_map >> (2 * i)) & 3;
2647 
2648 		if (mcs_80 != IEEE80211_HE_MCS_NOT_SUPPORTED) {
2649 			rx_mcs_80 = i + 1;
2650 			break;
2651 		}
2652 	}
2653 
2654 	if (support_160)
2655 		max_nss = min(rx_mcs_80, rx_mcs_160);
2656 	else
2657 		max_nss = rx_mcs_80;
2658 
2659 	arg->peer_nss = min(link_sta->rx_nss, max_nss);
2660 
2661 	memcpy(&arg->peer_he_cap_macinfo, he_cap->he_cap_elem.mac_cap_info,
2662 	       sizeof(he_cap->he_cap_elem.mac_cap_info));
2663 	memcpy(&arg->peer_he_cap_phyinfo, he_cap->he_cap_elem.phy_cap_info,
2664 	       sizeof(he_cap->he_cap_elem.phy_cap_info));
2665 	arg->peer_he_ops = link_conf->he_oper.params;
2666 
2667 	/* the top most byte is used to indicate BSS color info */
2668 	arg->peer_he_ops &= 0xffffff;
2669 
2670 	/* As per section 26.6.1 IEEE Std 802.11ax‐2022, if the Max AMPDU
2671 	 * Exponent Extension in HE cap is zero, use the arg->peer_max_mpdu
2672 	 * as calculated while parsing VHT caps(if VHT caps is present)
2673 	 * or HT caps (if VHT caps is not present).
2674 	 *
2675 	 * For non-zero value of Max AMPDU Exponent Extension in HE MAC caps,
2676 	 * if a HE STA sends VHT cap and HE cap IE in assoc request then, use
2677 	 * MAX_AMPDU_LEN_FACTOR as 20 to calculate max_ampdu length.
2678 	 * If a HE STA that does not send VHT cap, but HE and HT cap in assoc
2679 	 * request, then use MAX_AMPDU_LEN_FACTOR as 16 to calculate max_ampdu
2680 	 * length.
2681 	 */
2682 	ampdu_factor = u8_get_bits(he_cap->he_cap_elem.mac_cap_info[3],
2683 				   IEEE80211_HE_MAC_CAP3_MAX_AMPDU_LEN_EXP_MASK);
2684 
2685 	if (ampdu_factor) {
2686 		if (link_sta->vht_cap.vht_supported)
2687 			arg->peer_max_mpdu = (1 << (IEEE80211_HE_VHT_MAX_AMPDU_FACTOR +
2688 						    ampdu_factor)) - 1;
2689 		else if (link_sta->ht_cap.ht_supported)
2690 			arg->peer_max_mpdu = (1 << (IEEE80211_HE_HT_MAX_AMPDU_FACTOR +
2691 						    ampdu_factor)) - 1;
2692 	}
2693 
2694 	if (he_cap->he_cap_elem.phy_cap_info[6] &
2695 	    IEEE80211_HE_PHY_CAP6_PPE_THRESHOLD_PRESENT) {
2696 		int bit = 7;
2697 		int nss, ru;
2698 
2699 		arg->peer_ppet.numss_m1 = he_cap->ppe_thres[0] &
2700 					  IEEE80211_PPE_THRES_NSS_MASK;
2701 		arg->peer_ppet.ru_bit_mask =
2702 			(he_cap->ppe_thres[0] &
2703 			 IEEE80211_PPE_THRES_RU_INDEX_BITMASK_MASK) >>
2704 			IEEE80211_PPE_THRES_RU_INDEX_BITMASK_POS;
2705 
2706 		for (nss = 0; nss <= arg->peer_ppet.numss_m1; nss++) {
2707 			for (ru = 0; ru < 4; ru++) {
2708 				u32 val = 0;
2709 				int i;
2710 
2711 				if ((arg->peer_ppet.ru_bit_mask & BIT(ru)) == 0)
2712 					continue;
2713 				for (i = 0; i < 6; i++) {
2714 					val >>= 1;
2715 					val |= ((he_cap->ppe_thres[bit / 8] >>
2716 						 (bit % 8)) & 0x1) << 5;
2717 					bit++;
2718 				}
2719 				arg->peer_ppet.ppet16_ppet8_ru3_ru0[nss] |=
2720 								val << (ru * 6);
2721 			}
2722 		}
2723 	}
2724 
2725 	if (he_cap->he_cap_elem.mac_cap_info[0] & IEEE80211_HE_MAC_CAP0_TWT_RES)
2726 		arg->twt_responder = true;
2727 	if (he_cap->he_cap_elem.mac_cap_info[0] & IEEE80211_HE_MAC_CAP0_TWT_REQ)
2728 		arg->twt_requester = true;
2729 
2730 	he_nss = ath12k_mac_max_he_nss(he_mcs_mask);
2731 
2732 	if (he_nss > link_sta->rx_nss) {
2733 		user_rate_valid = false;
2734 		for (nss_idx = link_sta->rx_nss - 1; nss_idx >= 0; nss_idx--) {
2735 			if (he_mcs_mask[nss_idx]) {
2736 				user_rate_valid = true;
2737 				break;
2738 			}
2739 		}
2740 	}
2741 
2742 	if (!user_rate_valid) {
2743 		ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
2744 			   "Setting he range MCS value to peer supported nss:%d for peer %pM\n",
2745 			   link_sta->rx_nss, arsta->addr);
2746 		he_mcs_mask[link_sta->rx_nss - 1] = he_mcs_mask[he_nss - 1];
2747 	}
2748 
2749 	switch (link_sta->bandwidth) {
2750 	case IEEE80211_STA_RX_BW_160:
2751 		v = le16_to_cpu(he_cap->he_mcs_nss_supp.rx_mcs_160);
2752 		v = ath12k_peer_assoc_h_he_limit(v, he_mcs_mask);
2753 		arg->peer_he_rx_mcs_set[WMI_HECAP_TXRX_MCS_NSS_IDX_160] = v;
2754 
2755 		v = le16_to_cpu(he_cap->he_mcs_nss_supp.tx_mcs_160);
2756 		arg->peer_he_tx_mcs_set[WMI_HECAP_TXRX_MCS_NSS_IDX_160] = v;
2757 
2758 		arg->peer_he_mcs_count++;
2759 		if (!he_tx_mcs)
2760 			he_tx_mcs = v;
2761 		fallthrough;
2762 
2763 	default:
2764 		v = le16_to_cpu(he_cap->he_mcs_nss_supp.rx_mcs_80);
2765 		v = ath12k_peer_assoc_h_he_limit(v, he_mcs_mask);
2766 		arg->peer_he_rx_mcs_set[WMI_HECAP_TXRX_MCS_NSS_IDX_80] = v;
2767 
2768 		v = le16_to_cpu(he_cap->he_mcs_nss_supp.tx_mcs_80);
2769 		arg->peer_he_tx_mcs_set[WMI_HECAP_TXRX_MCS_NSS_IDX_80] = v;
2770 
2771 		arg->peer_he_mcs_count++;
2772 		if (!he_tx_mcs)
2773 			he_tx_mcs = v;
2774 		break;
2775 	}
2776 
2777 	/* Calculate peer NSS capability from HE capabilities if STA
2778 	 * supports HE.
2779 	 */
2780 	for (i = 0, max_nss = 0, he_mcs = 0; i < NL80211_HE_NSS_MAX; i++) {
2781 		he_mcs = he_tx_mcs >> (2 * i) & 3;
2782 
2783 		/* In case of fixed rates, MCS Range in he_tx_mcs might have
2784 		 * unsupported range, with he_mcs_mask set, so check either of them
2785 		 * to find nss.
2786 		 */
2787 		if (he_mcs != IEEE80211_HE_MCS_NOT_SUPPORTED ||
2788 		    he_mcs_mask[i])
2789 			max_nss = i + 1;
2790 	}
2791 
2792 	max_nss = min(max_nss, ar->num_tx_chains);
2793 	arg->peer_nss = min(link_sta->rx_nss, max_nss);
2794 
2795 	if (arg->peer_phymode == MODE_11AX_HE160) {
2796 		tx_nss = ath12k_get_nss_160mhz(ar, ar->num_tx_chains);
2797 		rx_nss = min(arg->peer_nss, tx_nss);
2798 
2799 		arg->peer_nss = min(link_sta->rx_nss, ar->num_rx_chains);
2800 		arg->peer_bw_rxnss_override = ATH12K_BW_NSS_MAP_ENABLE;
2801 
2802 		if (!rx_nss) {
2803 			ath12k_warn(ar->ab, "invalid max_nss\n");
2804 			return;
2805 		}
2806 
2807 		nss_160 = u32_encode_bits(rx_nss - 1, ATH12K_PEER_RX_NSS_160MHZ);
2808 		arg->peer_bw_rxnss_override |= nss_160;
2809 	}
2810 
2811 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
2812 		   "mac he peer %pM nss %d mcs cnt %d nss_override 0x%x\n",
2813 		   arsta->addr, arg->peer_nss,
2814 		   arg->peer_he_mcs_count,
2815 		   arg->peer_bw_rxnss_override);
2816 }
2817 
2818 static void ath12k_peer_assoc_h_he_6ghz(struct ath12k *ar,
2819 					struct ath12k_link_vif *arvif,
2820 					struct ath12k_link_sta *arsta,
2821 					struct ath12k_wmi_peer_assoc_arg *arg)
2822 {
2823 	struct ieee80211_vif *vif = ath12k_ahvif_to_vif(arvif->ahvif);
2824 	struct ieee80211_sta *sta = ath12k_ahsta_to_sta(arsta->ahsta);
2825 	const struct ieee80211_sta_he_cap *he_cap;
2826 	struct ieee80211_link_sta *link_sta;
2827 	struct cfg80211_chan_def def;
2828 	enum nl80211_band band;
2829 	u8 ampdu_factor, mpdu_density;
2830 
2831 	if (WARN_ON(ath12k_mac_vif_link_chan(vif, arvif->link_id, &def)))
2832 		return;
2833 
2834 	band = def.chan->band;
2835 
2836 	link_sta = ath12k_mac_get_link_sta(arsta);
2837 	if (!link_sta) {
2838 		ath12k_warn(ar->ab, "unable to access link sta in peer assoc he 6ghz for sta %pM link %u\n",
2839 			    sta->addr, arsta->link_id);
2840 		return;
2841 	}
2842 
2843 	he_cap = &link_sta->he_cap;
2844 
2845 	if (!arg->he_flag || band != NL80211_BAND_6GHZ || !link_sta->he_6ghz_capa.capa)
2846 		return;
2847 
2848 	if (link_sta->bandwidth == IEEE80211_STA_RX_BW_40)
2849 		arg->bw_40 = true;
2850 
2851 	if (link_sta->bandwidth == IEEE80211_STA_RX_BW_80)
2852 		arg->bw_80 = true;
2853 
2854 	if (link_sta->bandwidth == IEEE80211_STA_RX_BW_160)
2855 		arg->bw_160 = true;
2856 
2857 	if (link_sta->bandwidth == IEEE80211_STA_RX_BW_320)
2858 		arg->bw_320 = true;
2859 
2860 	arg->peer_he_caps_6ghz = le16_to_cpu(link_sta->he_6ghz_capa.capa);
2861 
2862 	mpdu_density = u32_get_bits(arg->peer_he_caps_6ghz,
2863 				    IEEE80211_HE_6GHZ_CAP_MIN_MPDU_START);
2864 	arg->peer_mpdu_density = ath12k_parse_mpdudensity(mpdu_density);
2865 
2866 	/* From IEEE Std 802.11ax-2021 - Section 10.12.2: An HE STA shall be capable of
2867 	 * receiving A-MPDU where the A-MPDU pre-EOF padding length is up to the value
2868 	 * indicated by the Maximum A-MPDU Length Exponent Extension field in the HE
2869 	 * Capabilities element and the Maximum A-MPDU Length Exponent field in HE 6 GHz
2870 	 * Band Capabilities element in the 6 GHz band.
2871 	 *
2872 	 * Here, we are extracting the Max A-MPDU Exponent Extension from HE caps and
2873 	 * factor is the Maximum A-MPDU Length Exponent from HE 6 GHZ Band capability.
2874 	 */
2875 	ampdu_factor = u8_get_bits(he_cap->he_cap_elem.mac_cap_info[3],
2876 				   IEEE80211_HE_MAC_CAP3_MAX_AMPDU_LEN_EXP_MASK) +
2877 			u32_get_bits(arg->peer_he_caps_6ghz,
2878 				     IEEE80211_HE_6GHZ_CAP_MAX_AMPDU_LEN_EXP);
2879 
2880 	arg->peer_max_mpdu = (1u << (IEEE80211_HE_6GHZ_MAX_AMPDU_FACTOR +
2881 				     ampdu_factor)) - 1;
2882 }
2883 
2884 static int ath12k_get_smps_from_capa(const struct ieee80211_sta_ht_cap *ht_cap,
2885 				     const struct ieee80211_he_6ghz_capa *he_6ghz_capa,
2886 				     int *smps)
2887 {
2888 	if (ht_cap->ht_supported)
2889 		*smps = u16_get_bits(ht_cap->cap, IEEE80211_HT_CAP_SM_PS);
2890 	else
2891 		*smps = le16_get_bits(he_6ghz_capa->capa,
2892 				      IEEE80211_HE_6GHZ_CAP_SM_PS);
2893 
2894 	if (*smps >= ARRAY_SIZE(ath12k_smps_map))
2895 		return -EINVAL;
2896 
2897 	return 0;
2898 }
2899 
2900 static void ath12k_peer_assoc_h_smps(struct ath12k_link_sta *arsta,
2901 				     struct ath12k_wmi_peer_assoc_arg *arg)
2902 {
2903 	struct ieee80211_sta *sta = ath12k_ahsta_to_sta(arsta->ahsta);
2904 	const struct ieee80211_he_6ghz_capa *he_6ghz_capa;
2905 	struct ath12k_link_vif *arvif = arsta->arvif;
2906 	const struct ieee80211_sta_ht_cap *ht_cap;
2907 	struct ieee80211_link_sta *link_sta;
2908 	struct ath12k *ar = arvif->ar;
2909 	int smps;
2910 
2911 	link_sta = ath12k_mac_get_link_sta(arsta);
2912 	if (!link_sta) {
2913 		ath12k_warn(ar->ab, "unable to access link sta in peer assoc he for sta %pM link %u\n",
2914 			    sta->addr, arsta->link_id);
2915 		return;
2916 	}
2917 
2918 	he_6ghz_capa = &link_sta->he_6ghz_capa;
2919 	ht_cap = &link_sta->ht_cap;
2920 
2921 	if (!ht_cap->ht_supported && !he_6ghz_capa->capa)
2922 		return;
2923 
2924 	if (ath12k_get_smps_from_capa(ht_cap, he_6ghz_capa, &smps))
2925 		return;
2926 
2927 	switch (smps) {
2928 	case WLAN_HT_CAP_SM_PS_STATIC:
2929 		arg->static_mimops_flag = true;
2930 		break;
2931 	case WLAN_HT_CAP_SM_PS_DYNAMIC:
2932 		arg->dynamic_mimops_flag = true;
2933 		break;
2934 	case WLAN_HT_CAP_SM_PS_DISABLED:
2935 		arg->spatial_mux_flag = true;
2936 		break;
2937 	default:
2938 		break;
2939 	}
2940 }
2941 
2942 static void ath12k_peer_assoc_h_qos(struct ath12k *ar,
2943 				    struct ath12k_link_vif *arvif,
2944 				    struct ath12k_link_sta *arsta,
2945 				    struct ath12k_wmi_peer_assoc_arg *arg)
2946 {
2947 	struct ieee80211_sta *sta = ath12k_ahsta_to_sta(arsta->ahsta);
2948 
2949 	switch (arvif->ahvif->vdev_type) {
2950 	case WMI_VDEV_TYPE_AP:
2951 		if (sta->wme) {
2952 			/* TODO: Check WME vs QoS */
2953 			arg->is_wme_set = true;
2954 			arg->qos_flag = true;
2955 		}
2956 
2957 		if (sta->wme && sta->uapsd_queues) {
2958 			/* TODO: Check WME vs QoS */
2959 			arg->is_wme_set = true;
2960 			arg->apsd_flag = true;
2961 			arg->peer_rate_caps |= WMI_HOST_RC_UAPSD_FLAG;
2962 		}
2963 		break;
2964 	case WMI_VDEV_TYPE_STA:
2965 		if (sta->wme) {
2966 			arg->is_wme_set = true;
2967 			arg->qos_flag = true;
2968 		}
2969 		break;
2970 	default:
2971 		break;
2972 	}
2973 
2974 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac peer %pM qos %d\n",
2975 		   arsta->addr, arg->qos_flag);
2976 }
2977 
2978 static int ath12k_peer_assoc_qos_ap(struct ath12k *ar,
2979 				    struct ath12k_link_vif *arvif,
2980 				    struct ath12k_link_sta *arsta)
2981 {
2982 	struct ieee80211_sta *sta = ath12k_ahsta_to_sta(arsta->ahsta);
2983 	struct ath12k_wmi_ap_ps_arg arg;
2984 	u32 max_sp;
2985 	u32 uapsd;
2986 	int ret;
2987 
2988 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
2989 
2990 	arg.vdev_id = arvif->vdev_id;
2991 
2992 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac uapsd_queues 0x%x max_sp %d\n",
2993 		   sta->uapsd_queues, sta->max_sp);
2994 
2995 	uapsd = 0;
2996 	if (sta->uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_VO)
2997 		uapsd |= WMI_AP_PS_UAPSD_AC3_DELIVERY_EN |
2998 			 WMI_AP_PS_UAPSD_AC3_TRIGGER_EN;
2999 	if (sta->uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_VI)
3000 		uapsd |= WMI_AP_PS_UAPSD_AC2_DELIVERY_EN |
3001 			 WMI_AP_PS_UAPSD_AC2_TRIGGER_EN;
3002 	if (sta->uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_BK)
3003 		uapsd |= WMI_AP_PS_UAPSD_AC1_DELIVERY_EN |
3004 			 WMI_AP_PS_UAPSD_AC1_TRIGGER_EN;
3005 	if (sta->uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_BE)
3006 		uapsd |= WMI_AP_PS_UAPSD_AC0_DELIVERY_EN |
3007 			 WMI_AP_PS_UAPSD_AC0_TRIGGER_EN;
3008 
3009 	max_sp = 0;
3010 	if (sta->max_sp < MAX_WMI_AP_PS_PEER_PARAM_MAX_SP)
3011 		max_sp = sta->max_sp;
3012 
3013 	arg.param = WMI_AP_PS_PEER_PARAM_UAPSD;
3014 	arg.value = uapsd;
3015 	ret = ath12k_wmi_send_set_ap_ps_param_cmd(ar, arsta->addr, &arg);
3016 	if (ret)
3017 		goto err;
3018 
3019 	arg.param = WMI_AP_PS_PEER_PARAM_MAX_SP;
3020 	arg.value = max_sp;
3021 	ret = ath12k_wmi_send_set_ap_ps_param_cmd(ar, arsta->addr, &arg);
3022 	if (ret)
3023 		goto err;
3024 
3025 	/* TODO: revisit during testing */
3026 	arg.param = WMI_AP_PS_PEER_PARAM_SIFS_RESP_FRMTYPE;
3027 	arg.value = DISABLE_SIFS_RESPONSE_TRIGGER;
3028 	ret = ath12k_wmi_send_set_ap_ps_param_cmd(ar, arsta->addr, &arg);
3029 	if (ret)
3030 		goto err;
3031 
3032 	arg.param = WMI_AP_PS_PEER_PARAM_SIFS_RESP_UAPSD;
3033 	arg.value = DISABLE_SIFS_RESPONSE_TRIGGER;
3034 	ret = ath12k_wmi_send_set_ap_ps_param_cmd(ar, arsta->addr, &arg);
3035 	if (ret)
3036 		goto err;
3037 
3038 	return 0;
3039 
3040 err:
3041 	ath12k_warn(ar->ab, "failed to set ap ps peer param %d for vdev %i: %d\n",
3042 		    arg.param, arvif->vdev_id, ret);
3043 	return ret;
3044 }
3045 
3046 static bool ath12k_mac_sta_has_ofdm_only(struct ieee80211_link_sta *sta)
3047 {
3048 	return sta->supp_rates[NL80211_BAND_2GHZ] >>
3049 	       ATH12K_MAC_FIRST_OFDM_RATE_IDX;
3050 }
3051 
3052 static enum wmi_phy_mode ath12k_mac_get_phymode_vht(struct ath12k *ar,
3053 						    struct ieee80211_link_sta *link_sta)
3054 {
3055 	if (link_sta->bandwidth == IEEE80211_STA_RX_BW_160) {
3056 		if (link_sta->vht_cap.cap & (IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_160MHZ |
3057 		    IEEE80211_VHT_CAP_EXT_NSS_BW_MASK))
3058 			return MODE_11AC_VHT160;
3059 
3060 		/* Allow STA to connect even if it does not explicitly advertise 160 MHz
3061 		 * support
3062 		 */
3063 		return MODE_11AC_VHT160;
3064 	}
3065 
3066 	if (link_sta->bandwidth == IEEE80211_STA_RX_BW_80)
3067 		return MODE_11AC_VHT80;
3068 
3069 	if (link_sta->bandwidth == IEEE80211_STA_RX_BW_40)
3070 		return MODE_11AC_VHT40;
3071 
3072 	if (link_sta->bandwidth == IEEE80211_STA_RX_BW_20)
3073 		return MODE_11AC_VHT20;
3074 
3075 	return MODE_UNKNOWN;
3076 }
3077 
3078 static enum wmi_phy_mode ath12k_mac_get_phymode_he(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->he_cap.he_cap_elem.phy_cap_info[0] &
3083 		     IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_160MHZ_IN_5G)
3084 			return MODE_11AX_HE160;
3085 
3086 		return MODE_UNKNOWN;
3087 	}
3088 
3089 	if (link_sta->bandwidth == IEEE80211_STA_RX_BW_80)
3090 		return MODE_11AX_HE80;
3091 
3092 	if (link_sta->bandwidth == IEEE80211_STA_RX_BW_40)
3093 		return MODE_11AX_HE40;
3094 
3095 	if (link_sta->bandwidth == IEEE80211_STA_RX_BW_20)
3096 		return MODE_11AX_HE20;
3097 
3098 	return MODE_UNKNOWN;
3099 }
3100 
3101 static enum wmi_phy_mode ath12k_mac_get_phymode_eht(struct ath12k *ar,
3102 						    struct ieee80211_link_sta *link_sta)
3103 {
3104 	if (link_sta->bandwidth == IEEE80211_STA_RX_BW_320)
3105 		if (link_sta->eht_cap.eht_cap_elem.phy_cap_info[0] &
3106 		    IEEE80211_EHT_PHY_CAP0_320MHZ_IN_6GHZ)
3107 			return MODE_11BE_EHT320;
3108 
3109 	if (link_sta->bandwidth == IEEE80211_STA_RX_BW_160) {
3110 		if (link_sta->he_cap.he_cap_elem.phy_cap_info[0] &
3111 		    IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_160MHZ_IN_5G)
3112 			return MODE_11BE_EHT160;
3113 
3114 		ath12k_warn(ar->ab, "invalid EHT PHY capability info for 160 Mhz: %d\n",
3115 			    link_sta->he_cap.he_cap_elem.phy_cap_info[0]);
3116 
3117 		return MODE_UNKNOWN;
3118 	}
3119 
3120 	if (link_sta->bandwidth == IEEE80211_STA_RX_BW_80)
3121 		return MODE_11BE_EHT80;
3122 
3123 	if (link_sta->bandwidth == IEEE80211_STA_RX_BW_40)
3124 		return MODE_11BE_EHT40;
3125 
3126 	if (link_sta->bandwidth == IEEE80211_STA_RX_BW_20)
3127 		return MODE_11BE_EHT20;
3128 
3129 	return MODE_UNKNOWN;
3130 }
3131 
3132 static bool
3133 ath12k_peer_assoc_h_eht_masked(const u16 eht_mcs_mask[NL80211_EHT_NSS_MAX])
3134 {
3135 	int nss;
3136 
3137 	for (nss = 0; nss < NL80211_EHT_NSS_MAX; nss++)
3138 		if (eht_mcs_mask[nss])
3139 			return false;
3140 
3141 	return true;
3142 }
3143 
3144 static void ath12k_peer_assoc_h_phymode(struct ath12k *ar,
3145 					struct ath12k_link_vif *arvif,
3146 					struct ath12k_link_sta *arsta,
3147 					struct ath12k_wmi_peer_assoc_arg *arg)
3148 {
3149 	struct ieee80211_link_sta *link_sta;
3150 	struct cfg80211_chan_def def;
3151 	enum nl80211_band band;
3152 	const u8 *ht_mcs_mask;
3153 	const u16 *vht_mcs_mask;
3154 	const u16 *he_mcs_mask;
3155 	const u16 *eht_mcs_mask;
3156 	enum wmi_phy_mode phymode = MODE_UNKNOWN;
3157 
3158 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
3159 
3160 	struct ieee80211_vif *vif = ath12k_ahvif_to_vif(arvif->ahvif);
3161 	struct ieee80211_sta *sta = ath12k_ahsta_to_sta(arsta->ahsta);
3162 
3163 	if (WARN_ON(ath12k_mac_vif_link_chan(vif, arvif->link_id, &def)))
3164 		return;
3165 
3166 	band = def.chan->band;
3167 	ht_mcs_mask = arvif->bitrate_mask.control[band].ht_mcs;
3168 	vht_mcs_mask = arvif->bitrate_mask.control[band].vht_mcs;
3169 	he_mcs_mask = arvif->bitrate_mask.control[band].he_mcs;
3170 	eht_mcs_mask = arvif->bitrate_mask.control[band].eht_mcs;
3171 
3172 	link_sta = ath12k_mac_get_link_sta(arsta);
3173 	if (!link_sta) {
3174 		ath12k_warn(ar->ab, "unable to access link sta in peer assoc he for sta %pM link %u\n",
3175 			    sta->addr, arsta->link_id);
3176 		return;
3177 	}
3178 
3179 	switch (band) {
3180 	case NL80211_BAND_2GHZ:
3181 		if (link_sta->eht_cap.has_eht &&
3182 		    !ath12k_peer_assoc_h_eht_masked(eht_mcs_mask)) {
3183 			if (link_sta->bandwidth == IEEE80211_STA_RX_BW_40)
3184 				phymode = MODE_11BE_EHT40_2G;
3185 			else
3186 				phymode = MODE_11BE_EHT20_2G;
3187 		} else if (link_sta->he_cap.has_he &&
3188 			   !ath12k_peer_assoc_h_he_masked(he_mcs_mask)) {
3189 			if (link_sta->bandwidth == IEEE80211_STA_RX_BW_80)
3190 				phymode = MODE_11AX_HE80_2G;
3191 			else if (link_sta->bandwidth == IEEE80211_STA_RX_BW_40)
3192 				phymode = MODE_11AX_HE40_2G;
3193 			else
3194 				phymode = MODE_11AX_HE20_2G;
3195 		} else if (link_sta->vht_cap.vht_supported &&
3196 		    !ath12k_peer_assoc_h_vht_masked(vht_mcs_mask)) {
3197 			if (link_sta->bandwidth == IEEE80211_STA_RX_BW_40)
3198 				phymode = MODE_11AC_VHT40;
3199 			else
3200 				phymode = MODE_11AC_VHT20;
3201 		} else if (link_sta->ht_cap.ht_supported &&
3202 			   !ath12k_peer_assoc_h_ht_masked(ht_mcs_mask)) {
3203 			if (link_sta->bandwidth == IEEE80211_STA_RX_BW_40)
3204 				phymode = MODE_11NG_HT40;
3205 			else
3206 				phymode = MODE_11NG_HT20;
3207 		} else if (ath12k_mac_sta_has_ofdm_only(link_sta)) {
3208 			phymode = MODE_11G;
3209 		} else {
3210 			phymode = MODE_11B;
3211 		}
3212 		break;
3213 	case NL80211_BAND_5GHZ:
3214 	case NL80211_BAND_6GHZ:
3215 		/* Check EHT first */
3216 		if (link_sta->eht_cap.has_eht) {
3217 			phymode = ath12k_mac_get_phymode_eht(ar, link_sta);
3218 		} else if (link_sta->he_cap.has_he &&
3219 			   !ath12k_peer_assoc_h_he_masked(he_mcs_mask)) {
3220 			phymode = ath12k_mac_get_phymode_he(ar, link_sta);
3221 		} else if (link_sta->vht_cap.vht_supported &&
3222 		    !ath12k_peer_assoc_h_vht_masked(vht_mcs_mask)) {
3223 			phymode = ath12k_mac_get_phymode_vht(ar, link_sta);
3224 		} else if (link_sta->ht_cap.ht_supported &&
3225 			   !ath12k_peer_assoc_h_ht_masked(ht_mcs_mask)) {
3226 			if (link_sta->bandwidth >= IEEE80211_STA_RX_BW_40)
3227 				phymode = MODE_11NA_HT40;
3228 			else
3229 				phymode = MODE_11NA_HT20;
3230 		} else {
3231 			phymode = MODE_11A;
3232 		}
3233 		break;
3234 	default:
3235 		break;
3236 	}
3237 
3238 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac peer %pM phymode %s\n",
3239 		   arsta->addr, ath12k_mac_phymode_str(phymode));
3240 
3241 	arg->peer_phymode = phymode;
3242 	WARN_ON(phymode == MODE_UNKNOWN);
3243 }
3244 
3245 #define ATH12K_EHT_MCS_7_ENABLED	0x00FF
3246 #define ATH12K_EHT_MCS_9_ENABLED	0x0300
3247 #define ATH12K_EHT_MCS_11_ENABLED	0x0C00
3248 #define ATH12K_EHT_MCS_13_ENABLED	0x3000
3249 
3250 static void ath12k_mac_set_eht_mcs(u8 rx_tx_mcs7, u8 rx_tx_mcs9,
3251 				   u8 rx_tx_mcs11, u8 rx_tx_mcs13,
3252 				   u32 *rx_mcs, u32 *tx_mcs,
3253 				   const u16 eht_mcs_limit[NL80211_EHT_NSS_MAX])
3254 {
3255 	int nss;
3256 	u8 mcs_7 = 0, mcs_9 = 0, mcs_11 = 0, mcs_13 = 0;
3257 	u8 peer_mcs_7, peer_mcs_9, peer_mcs_11, peer_mcs_13;
3258 
3259 	for (nss = 0; nss < NL80211_EHT_NSS_MAX; nss++) {
3260 		if (eht_mcs_limit[nss] & ATH12K_EHT_MCS_7_ENABLED)
3261 			mcs_7++;
3262 		if (eht_mcs_limit[nss] & ATH12K_EHT_MCS_9_ENABLED)
3263 			mcs_9++;
3264 		if (eht_mcs_limit[nss] & ATH12K_EHT_MCS_11_ENABLED)
3265 			mcs_11++;
3266 		if (eht_mcs_limit[nss] & ATH12K_EHT_MCS_13_ENABLED)
3267 			mcs_13++;
3268 	}
3269 
3270 	peer_mcs_7 = u8_get_bits(rx_tx_mcs7, IEEE80211_EHT_MCS_NSS_RX);
3271 	peer_mcs_9 = u8_get_bits(rx_tx_mcs9, IEEE80211_EHT_MCS_NSS_RX);
3272 	peer_mcs_11 = u8_get_bits(rx_tx_mcs11, IEEE80211_EHT_MCS_NSS_RX);
3273 	peer_mcs_13 = u8_get_bits(rx_tx_mcs13, IEEE80211_EHT_MCS_NSS_RX);
3274 
3275 	*rx_mcs = u32_encode_bits(min(peer_mcs_7, mcs_7), WMI_EHT_MCS_NSS_0_7) |
3276 		  u32_encode_bits(min(peer_mcs_9, mcs_9), WMI_EHT_MCS_NSS_8_9) |
3277 		  u32_encode_bits(min(peer_mcs_11, mcs_11), WMI_EHT_MCS_NSS_10_11) |
3278 		  u32_encode_bits(min(peer_mcs_13, mcs_13), WMI_EHT_MCS_NSS_12_13);
3279 
3280 	peer_mcs_7 = u8_get_bits(rx_tx_mcs7, IEEE80211_EHT_MCS_NSS_TX);
3281 	peer_mcs_9 = u8_get_bits(rx_tx_mcs9, IEEE80211_EHT_MCS_NSS_TX);
3282 	peer_mcs_11 = u8_get_bits(rx_tx_mcs11, IEEE80211_EHT_MCS_NSS_TX);
3283 	peer_mcs_13 = u8_get_bits(rx_tx_mcs13, IEEE80211_EHT_MCS_NSS_TX);
3284 
3285 	*tx_mcs = u32_encode_bits(min(peer_mcs_7, mcs_7), WMI_EHT_MCS_NSS_0_7) |
3286 		  u32_encode_bits(min(peer_mcs_9, mcs_9), WMI_EHT_MCS_NSS_8_9) |
3287 		  u32_encode_bits(min(peer_mcs_11, mcs_11), WMI_EHT_MCS_NSS_10_11) |
3288 		  u32_encode_bits(min(peer_mcs_13, mcs_13), WMI_EHT_MCS_NSS_12_13);
3289 }
3290 
3291 static void ath12k_mac_set_eht_ppe_threshold(const u8 *ppe_thres,
3292 					     struct ath12k_wmi_ppe_threshold_arg *ppet)
3293 {
3294 	u32 bit_pos = IEEE80211_EHT_PPE_THRES_INFO_HEADER_SIZE, val;
3295 	u8 nss, ru, i;
3296 	u8 ppet_bit_len_per_ru = IEEE80211_EHT_PPE_THRES_INFO_PPET_SIZE * 2;
3297 
3298 	ppet->numss_m1 = u8_get_bits(ppe_thres[0], IEEE80211_EHT_PPE_THRES_NSS_MASK);
3299 	ppet->ru_bit_mask = u16_get_bits(get_unaligned_le16(ppe_thres),
3300 					 IEEE80211_EHT_PPE_THRES_RU_INDEX_BITMASK_MASK);
3301 
3302 	for (nss = 0; nss <= ppet->numss_m1; nss++) {
3303 		for (ru = 0;
3304 		     ru < hweight16(IEEE80211_EHT_PPE_THRES_RU_INDEX_BITMASK_MASK);
3305 		     ru++) {
3306 			if ((ppet->ru_bit_mask & BIT(ru)) == 0)
3307 				continue;
3308 
3309 			val = 0;
3310 			for (i = 0; i < ppet_bit_len_per_ru; i++) {
3311 				val |= (((ppe_thres[bit_pos / 8] >>
3312 					  (bit_pos % 8)) & 0x1) << i);
3313 				bit_pos++;
3314 			}
3315 			ppet->ppet16_ppet8_ru3_ru0[nss] |=
3316 					(val << (ru * ppet_bit_len_per_ru));
3317 		}
3318 	}
3319 }
3320 
3321 static void ath12k_peer_assoc_h_eht(struct ath12k *ar,
3322 				    struct ath12k_link_vif *arvif,
3323 				    struct ath12k_link_sta *arsta,
3324 				    struct ath12k_wmi_peer_assoc_arg *arg)
3325 {
3326 	struct ieee80211_sta *sta = ath12k_ahsta_to_sta(arsta->ahsta);
3327 	struct ieee80211_vif *vif = ath12k_ahvif_to_vif(arvif->ahvif);
3328 	const struct ieee80211_eht_mcs_nss_supp *own_eht_mcs_nss_supp;
3329 	const struct ieee80211_eht_mcs_nss_supp_20mhz_only *bw_20;
3330 	const struct ieee80211_sta_eht_cap *eht_cap, *own_eht_cap;
3331 	const struct ieee80211_sband_iftype_data *iftd;
3332 	const struct ieee80211_eht_mcs_nss_supp_bw *bw;
3333 	const struct ieee80211_sta_he_cap *he_cap;
3334 	struct ieee80211_link_sta *link_sta;
3335 	struct ieee80211_bss_conf *link_conf;
3336 	struct cfg80211_chan_def def;
3337 	bool user_rate_valid = true;
3338 	enum nl80211_band band;
3339 	int eht_nss, nss_idx;
3340 	u32 *rx_mcs, *tx_mcs;
3341 	u16 *eht_mcs_mask;
3342 	u8 max_nss = 0;
3343 
3344 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
3345 
3346 	link_sta = ath12k_mac_get_link_sta(arsta);
3347 	if (!link_sta) {
3348 		ath12k_warn(ar->ab, "unable to access link sta in peer assoc eht for sta %pM link %u\n",
3349 			    sta->addr, arsta->link_id);
3350 		return;
3351 	}
3352 
3353 	link_conf = ath12k_mac_get_link_bss_conf(arvif);
3354 	if (!link_conf) {
3355 		ath12k_warn(ar->ab, "unable to access link_conf in peer assoc eht set\n");
3356 		return;
3357 	}
3358 
3359 	eht_cap = &link_sta->eht_cap;
3360 	he_cap = &link_sta->he_cap;
3361 	if (!he_cap->has_he || !eht_cap->has_eht)
3362 		return;
3363 
3364 	if (WARN_ON(ath12k_mac_vif_link_chan(vif, arvif->link_id, &def)))
3365 		return;
3366 
3367 	band = def.chan->band;
3368 	eht_mcs_mask = arvif->bitrate_mask.control[band].eht_mcs;
3369 
3370 	iftd = ieee80211_get_sband_iftype_data(&ar->mac.sbands[band], vif->type);
3371 	if (!iftd) {
3372 		ath12k_warn(ar->ab,
3373 			    "unable to access iftype_data in struct ieee80211_supported_band\n");
3374 		return;
3375 	}
3376 
3377 	own_eht_cap = &iftd->eht_cap;
3378 	own_eht_mcs_nss_supp = &own_eht_cap->eht_mcs_nss_supp;
3379 
3380 	arg->eht_flag = true;
3381 
3382 	if ((eht_cap->eht_cap_elem.phy_cap_info[5] &
3383 	     IEEE80211_EHT_PHY_CAP5_PPE_THRESHOLD_PRESENT) &&
3384 	    eht_cap->eht_ppe_thres[0] != 0)
3385 		ath12k_mac_set_eht_ppe_threshold(eht_cap->eht_ppe_thres,
3386 						 &arg->peer_eht_ppet);
3387 
3388 	memcpy(arg->peer_eht_cap_mac, eht_cap->eht_cap_elem.mac_cap_info,
3389 	       sizeof(eht_cap->eht_cap_elem.mac_cap_info));
3390 	memcpy(arg->peer_eht_cap_phy, eht_cap->eht_cap_elem.phy_cap_info,
3391 	       sizeof(eht_cap->eht_cap_elem.phy_cap_info));
3392 
3393 	rx_mcs = arg->peer_eht_rx_mcs_set;
3394 	tx_mcs = arg->peer_eht_tx_mcs_set;
3395 
3396 	eht_nss = ath12k_mac_max_eht_mcs_nss((void *)own_eht_mcs_nss_supp,
3397 					     sizeof(*own_eht_mcs_nss_supp));
3398 	if (eht_nss > link_sta->rx_nss) {
3399 		user_rate_valid = false;
3400 		for (nss_idx = (link_sta->rx_nss - 1); nss_idx >= 0; nss_idx--) {
3401 			if (eht_mcs_mask[nss_idx]) {
3402 				user_rate_valid = true;
3403 				break;
3404 			}
3405 		}
3406 	}
3407 
3408 	if (!user_rate_valid) {
3409 		ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
3410 			   "Setting eht range MCS value to peer supported nss %d for peer %pM\n",
3411 			   link_sta->rx_nss, arsta->addr);
3412 		eht_mcs_mask[link_sta->rx_nss - 1] = eht_mcs_mask[eht_nss - 1];
3413 	}
3414 
3415 	bw_20 = &eht_cap->eht_mcs_nss_supp.only_20mhz;
3416 	bw = &eht_cap->eht_mcs_nss_supp.bw._80;
3417 
3418 	switch (link_sta->bandwidth) {
3419 	case IEEE80211_STA_RX_BW_320:
3420 		bw = &eht_cap->eht_mcs_nss_supp.bw._320;
3421 		ath12k_mac_set_eht_mcs(bw->rx_tx_mcs9_max_nss,
3422 				       bw->rx_tx_mcs9_max_nss,
3423 				       bw->rx_tx_mcs11_max_nss,
3424 				       bw->rx_tx_mcs13_max_nss,
3425 				       &rx_mcs[WMI_EHTCAP_TXRX_MCS_NSS_IDX_320],
3426 				       &tx_mcs[WMI_EHTCAP_TXRX_MCS_NSS_IDX_320],
3427 				       eht_mcs_mask);
3428 		arg->peer_eht_mcs_count++;
3429 		fallthrough;
3430 	case IEEE80211_STA_RX_BW_160:
3431 		bw = &eht_cap->eht_mcs_nss_supp.bw._160;
3432 		ath12k_mac_set_eht_mcs(bw->rx_tx_mcs9_max_nss,
3433 				       bw->rx_tx_mcs9_max_nss,
3434 				       bw->rx_tx_mcs11_max_nss,
3435 				       bw->rx_tx_mcs13_max_nss,
3436 				       &rx_mcs[WMI_EHTCAP_TXRX_MCS_NSS_IDX_160],
3437 				       &tx_mcs[WMI_EHTCAP_TXRX_MCS_NSS_IDX_160],
3438 				       eht_mcs_mask);
3439 		arg->peer_eht_mcs_count++;
3440 		fallthrough;
3441 	default:
3442 		if (!(link_sta->he_cap.he_cap_elem.phy_cap_info[0] &
3443 		      IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_MASK_ALL)) {
3444 			bw_20 = &eht_cap->eht_mcs_nss_supp.only_20mhz;
3445 
3446 			ath12k_mac_set_eht_mcs(bw_20->rx_tx_mcs7_max_nss,
3447 					       bw_20->rx_tx_mcs9_max_nss,
3448 					       bw_20->rx_tx_mcs11_max_nss,
3449 					       bw_20->rx_tx_mcs13_max_nss,
3450 					       &rx_mcs[WMI_EHTCAP_TXRX_MCS_NSS_IDX_80],
3451 					       &tx_mcs[WMI_EHTCAP_TXRX_MCS_NSS_IDX_80],
3452 					       eht_mcs_mask);
3453 		} else {
3454 			bw = &eht_cap->eht_mcs_nss_supp.bw._80;
3455 			ath12k_mac_set_eht_mcs(bw->rx_tx_mcs9_max_nss,
3456 					       bw->rx_tx_mcs9_max_nss,
3457 					       bw->rx_tx_mcs11_max_nss,
3458 					       bw->rx_tx_mcs13_max_nss,
3459 					       &rx_mcs[WMI_EHTCAP_TXRX_MCS_NSS_IDX_80],
3460 					       &tx_mcs[WMI_EHTCAP_TXRX_MCS_NSS_IDX_80],
3461 					       eht_mcs_mask);
3462 		}
3463 
3464 		arg->peer_eht_mcs_count++;
3465 		break;
3466 	}
3467 
3468 	arg->punct_bitmap = ~arvif->punct_bitmap;
3469 	arg->eht_disable_mcs15 = link_conf->eht_disable_mcs15;
3470 
3471 	if (!(link_sta->he_cap.he_cap_elem.phy_cap_info[0] &
3472 	      IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_MASK_ALL)) {
3473 		if (bw_20->rx_tx_mcs13_max_nss)
3474 			max_nss = max(max_nss, u8_get_bits(bw_20->rx_tx_mcs13_max_nss,
3475 							   IEEE80211_EHT_MCS_NSS_RX));
3476 		if (bw_20->rx_tx_mcs11_max_nss)
3477 			max_nss = max(max_nss, u8_get_bits(bw_20->rx_tx_mcs11_max_nss,
3478 							   IEEE80211_EHT_MCS_NSS_RX));
3479 		if (bw_20->rx_tx_mcs9_max_nss)
3480 			max_nss = max(max_nss, u8_get_bits(bw_20->rx_tx_mcs9_max_nss,
3481 							   IEEE80211_EHT_MCS_NSS_RX));
3482 		if (bw_20->rx_tx_mcs7_max_nss)
3483 			max_nss = max(max_nss, u8_get_bits(bw_20->rx_tx_mcs7_max_nss,
3484 							   IEEE80211_EHT_MCS_NSS_RX));
3485 	} else {
3486 		if (bw->rx_tx_mcs13_max_nss)
3487 			max_nss = max(max_nss, u8_get_bits(bw->rx_tx_mcs13_max_nss,
3488 							   IEEE80211_EHT_MCS_NSS_RX));
3489 		if (bw->rx_tx_mcs11_max_nss)
3490 			max_nss = max(max_nss, u8_get_bits(bw->rx_tx_mcs11_max_nss,
3491 							   IEEE80211_EHT_MCS_NSS_RX));
3492 		if (bw->rx_tx_mcs9_max_nss)
3493 			max_nss = max(max_nss, u8_get_bits(bw->rx_tx_mcs9_max_nss,
3494 							   IEEE80211_EHT_MCS_NSS_RX));
3495 	}
3496 
3497 	max_nss = min(max_nss, (uint8_t)eht_nss);
3498 
3499 	arg->peer_nss = min(link_sta->rx_nss, max_nss);
3500 
3501 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
3502 		   "mac eht peer %pM nss %d mcs cnt %d ru_punct_bitmap 0x%x\n",
3503 		   arsta->addr, arg->peer_nss, arg->peer_eht_mcs_count,
3504 		   arg->punct_bitmap);
3505 }
3506 
3507 static void ath12k_peer_assoc_h_mlo(struct ath12k_link_sta *arsta,
3508 				    struct ath12k_wmi_peer_assoc_arg *arg)
3509 {
3510 	struct ieee80211_sta *sta = ath12k_ahsta_to_sta(arsta->ahsta);
3511 	struct peer_assoc_mlo_params *ml = &arg->ml;
3512 	struct ath12k_sta *ahsta = arsta->ahsta;
3513 	struct ath12k_link_sta *arsta_p;
3514 	struct ath12k_link_vif *arvif;
3515 	unsigned long links;
3516 	u8 link_id;
3517 	int i;
3518 
3519 	if (!sta->mlo || ahsta->ml_peer_id == ATH12K_MLO_PEER_ID_INVALID)
3520 		return;
3521 
3522 	ml->enabled = true;
3523 	ml->assoc_link = arsta->is_assoc_link;
3524 
3525 	/* For now considering the primary umac based on assoc link */
3526 	ml->primary_umac = arsta->is_assoc_link;
3527 	ml->peer_id_valid = true;
3528 	ml->logical_link_idx_valid = true;
3529 
3530 	ether_addr_copy(ml->mld_addr, sta->addr);
3531 	ml->logical_link_idx = arsta->link_idx;
3532 	ml->ml_peer_id = ahsta->ml_peer_id;
3533 	ml->ieee_link_id = arsta->link_id;
3534 	ml->num_partner_links = 0;
3535 	ml->eml_cap = sta->eml_cap;
3536 	links = ahsta->links_map;
3537 
3538 	rcu_read_lock();
3539 
3540 	i = 0;
3541 
3542 	for_each_set_bit(link_id, &links, IEEE80211_MLD_MAX_NUM_LINKS) {
3543 		if (i >= ATH12K_WMI_MLO_MAX_LINKS)
3544 			break;
3545 
3546 		arsta_p = rcu_dereference(ahsta->link[link_id]);
3547 		arvif = rcu_dereference(ahsta->ahvif->link[link_id]);
3548 
3549 		if (arsta_p == arsta)
3550 			continue;
3551 
3552 		if (!arvif->is_started)
3553 			continue;
3554 
3555 		ml->partner_info[i].vdev_id = arvif->vdev_id;
3556 		ml->partner_info[i].hw_link_id = arvif->ar->pdev->hw_link_id;
3557 		ml->partner_info[i].assoc_link = arsta_p->is_assoc_link;
3558 		ml->partner_info[i].primary_umac = arsta_p->is_assoc_link;
3559 		ml->partner_info[i].logical_link_idx_valid = true;
3560 		ml->partner_info[i].logical_link_idx = arsta_p->link_idx;
3561 		ml->num_partner_links++;
3562 
3563 		i++;
3564 	}
3565 
3566 	rcu_read_unlock();
3567 }
3568 
3569 static void ath12k_peer_assoc_prepare(struct ath12k *ar,
3570 				      struct ath12k_link_vif *arvif,
3571 				      struct ath12k_link_sta *arsta,
3572 				      struct ath12k_wmi_peer_assoc_arg *arg,
3573 				      bool reassoc)
3574 {
3575 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
3576 
3577 	memset(arg, 0, sizeof(*arg));
3578 
3579 	reinit_completion(&ar->peer_assoc_done);
3580 
3581 	arg->peer_new_assoc = !reassoc;
3582 	ath12k_peer_assoc_h_basic(ar, arvif, arsta, arg);
3583 	ath12k_peer_assoc_h_crypto(ar, arvif, arsta, arg);
3584 	ath12k_peer_assoc_h_rates(ar, arvif, arsta, arg);
3585 	ath12k_peer_assoc_h_ht(ar, arvif, arsta, arg);
3586 	ath12k_peer_assoc_h_vht(ar, arvif, arsta, arg);
3587 	ath12k_peer_assoc_h_he(ar, arvif, arsta, arg);
3588 	ath12k_peer_assoc_h_he_6ghz(ar, arvif, arsta, arg);
3589 	ath12k_peer_assoc_h_eht(ar, arvif, arsta, arg);
3590 	ath12k_peer_assoc_h_qos(ar, arvif, arsta, arg);
3591 	ath12k_peer_assoc_h_phymode(ar, arvif, arsta, arg);
3592 	ath12k_peer_assoc_h_smps(arsta, arg);
3593 	ath12k_peer_assoc_h_mlo(arsta, arg);
3594 
3595 	arsta->peer_nss = arg->peer_nss;
3596 	/* TODO: amsdu_disable req? */
3597 }
3598 
3599 static int ath12k_setup_peer_smps(struct ath12k *ar, struct ath12k_link_vif *arvif,
3600 				  const u8 *addr,
3601 				  const struct ieee80211_sta_ht_cap *ht_cap,
3602 				  const struct ieee80211_he_6ghz_capa *he_6ghz_capa)
3603 {
3604 	int smps, ret = 0;
3605 
3606 	if (!ht_cap->ht_supported && !he_6ghz_capa)
3607 		return 0;
3608 
3609 	ret = ath12k_get_smps_from_capa(ht_cap, he_6ghz_capa, &smps);
3610 	if (ret < 0)
3611 		return ret;
3612 
3613 	return ath12k_wmi_set_peer_param(ar, addr, arvif->vdev_id,
3614 					 WMI_PEER_MIMO_PS_STATE,
3615 					 ath12k_smps_map[smps]);
3616 }
3617 
3618 static int ath12k_mac_set_he_txbf_conf(struct ath12k_link_vif *arvif)
3619 {
3620 	struct ath12k_vif *ahvif = arvif->ahvif;
3621 	struct ath12k *ar = arvif->ar;
3622 	u32 param = WMI_VDEV_PARAM_SET_HEMU_MODE;
3623 	u32 value = 0;
3624 	int ret;
3625 	struct ieee80211_bss_conf *link_conf;
3626 
3627 	link_conf = ath12k_mac_get_link_bss_conf(arvif);
3628 	if (!link_conf) {
3629 		ath12k_warn(ar->ab, "unable to access bss link conf in txbf conf\n");
3630 		return -EINVAL;
3631 	}
3632 
3633 	if (!link_conf->he_support)
3634 		return 0;
3635 
3636 	if (link_conf->he_su_beamformer) {
3637 		value |= u32_encode_bits(HE_SU_BFER_ENABLE, HE_MODE_SU_TX_BFER);
3638 		if (link_conf->he_mu_beamformer &&
3639 		    ahvif->vdev_type == WMI_VDEV_TYPE_AP)
3640 			value |= u32_encode_bits(HE_MU_BFER_ENABLE, HE_MODE_MU_TX_BFER);
3641 	}
3642 
3643 	if (ahvif->vif->type != NL80211_IFTYPE_MESH_POINT) {
3644 		value |= u32_encode_bits(HE_DL_MUOFDMA_ENABLE, HE_MODE_DL_OFDMA) |
3645 			 u32_encode_bits(HE_UL_MUOFDMA_ENABLE, HE_MODE_UL_OFDMA);
3646 
3647 		if (link_conf->he_full_ul_mumimo)
3648 			value |= u32_encode_bits(HE_UL_MUMIMO_ENABLE, HE_MODE_UL_MUMIMO);
3649 
3650 		if (link_conf->he_su_beamformee)
3651 			value |= u32_encode_bits(HE_SU_BFEE_ENABLE, HE_MODE_SU_TX_BFEE);
3652 	}
3653 
3654 	ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id, param, value);
3655 	if (ret) {
3656 		ath12k_warn(ar->ab, "failed to set vdev %d HE MU mode: %d\n",
3657 			    arvif->vdev_id, ret);
3658 		return ret;
3659 	}
3660 
3661 	param = WMI_VDEV_PARAM_SET_HE_SOUNDING_MODE;
3662 	value =	u32_encode_bits(HE_VHT_SOUNDING_MODE_ENABLE, HE_VHT_SOUNDING_MODE) |
3663 		u32_encode_bits(HE_TRIG_NONTRIG_SOUNDING_MODE_ENABLE,
3664 				HE_TRIG_NONTRIG_SOUNDING_MODE);
3665 	ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
3666 					    param, value);
3667 	if (ret) {
3668 		ath12k_warn(ar->ab, "failed to set vdev %d sounding mode: %d\n",
3669 			    arvif->vdev_id, ret);
3670 		return ret;
3671 	}
3672 
3673 	return 0;
3674 }
3675 
3676 static int ath12k_mac_vif_recalc_sta_he_txbf(struct ath12k *ar,
3677 					     struct ath12k_link_vif *arvif,
3678 					     struct ieee80211_sta_he_cap *he_cap,
3679 					     int *hemode)
3680 {
3681 	struct ieee80211_vif *vif = arvif->ahvif->vif;
3682 	struct ieee80211_he_cap_elem he_cap_elem = {};
3683 	struct ieee80211_sta_he_cap *cap_band;
3684 	struct cfg80211_chan_def def;
3685 	u8 link_id = arvif->link_id;
3686 	struct ieee80211_bss_conf *link_conf;
3687 
3688 	link_conf = ath12k_mac_get_link_bss_conf(arvif);
3689 	if (!link_conf) {
3690 		ath12k_warn(ar->ab, "unable to access bss link conf in recalc txbf conf\n");
3691 		return -EINVAL;
3692 	}
3693 
3694 	if (!link_conf->he_support)
3695 		return 0;
3696 
3697 	if (vif->type != NL80211_IFTYPE_STATION)
3698 		return -EINVAL;
3699 
3700 	if (WARN_ON(ath12k_mac_vif_link_chan(vif, link_id, &def)))
3701 		return -EINVAL;
3702 
3703 	if (def.chan->band == NL80211_BAND_2GHZ)
3704 		cap_band = &ar->mac.iftype[NL80211_BAND_2GHZ][vif->type].he_cap;
3705 	else
3706 		cap_band = &ar->mac.iftype[NL80211_BAND_5GHZ][vif->type].he_cap;
3707 
3708 	memcpy(&he_cap_elem, &cap_band->he_cap_elem, sizeof(he_cap_elem));
3709 
3710 	*hemode = 0;
3711 	if (HECAP_PHY_SUBFME_GET(he_cap_elem.phy_cap_info)) {
3712 		if (HECAP_PHY_SUBFMR_GET(he_cap->he_cap_elem.phy_cap_info))
3713 			*hemode |= u32_encode_bits(HE_SU_BFEE_ENABLE, HE_MODE_SU_TX_BFEE);
3714 		if (HECAP_PHY_MUBFMR_GET(he_cap->he_cap_elem.phy_cap_info))
3715 			*hemode |= u32_encode_bits(HE_MU_BFEE_ENABLE, HE_MODE_MU_TX_BFEE);
3716 	}
3717 
3718 	if (vif->type != NL80211_IFTYPE_MESH_POINT) {
3719 		*hemode |= u32_encode_bits(HE_DL_MUOFDMA_ENABLE, HE_MODE_DL_OFDMA) |
3720 			  u32_encode_bits(HE_UL_MUOFDMA_ENABLE, HE_MODE_UL_OFDMA);
3721 
3722 		if (HECAP_PHY_ULMUMIMO_GET(he_cap_elem.phy_cap_info))
3723 			if (HECAP_PHY_ULMUMIMO_GET(he_cap->he_cap_elem.phy_cap_info))
3724 				*hemode |= u32_encode_bits(HE_UL_MUMIMO_ENABLE,
3725 							  HE_MODE_UL_MUMIMO);
3726 
3727 		if (u32_get_bits(*hemode, HE_MODE_MU_TX_BFEE))
3728 			*hemode |= u32_encode_bits(HE_SU_BFEE_ENABLE, HE_MODE_SU_TX_BFEE);
3729 
3730 		if (u32_get_bits(*hemode, HE_MODE_MU_TX_BFER))
3731 			*hemode |= u32_encode_bits(HE_SU_BFER_ENABLE, HE_MODE_SU_TX_BFER);
3732 	}
3733 
3734 	return 0;
3735 }
3736 
3737 static int ath12k_mac_set_eht_txbf_conf(struct ath12k_link_vif *arvif)
3738 {
3739 	struct ath12k_vif *ahvif = arvif->ahvif;
3740 	struct ath12k *ar = arvif->ar;
3741 	u32 param = WMI_VDEV_PARAM_SET_EHT_MU_MODE;
3742 	u32 value = 0;
3743 	int ret;
3744 	struct ieee80211_bss_conf *link_conf;
3745 
3746 	link_conf = ath12k_mac_get_link_bss_conf(arvif);
3747 	if (!link_conf) {
3748 		ath12k_warn(ar->ab, "unable to access bss link conf in eht txbf conf\n");
3749 		return -ENOENT;
3750 	}
3751 
3752 	if (!link_conf->eht_support)
3753 		return 0;
3754 
3755 	if (link_conf->eht_su_beamformer) {
3756 		value |= u32_encode_bits(EHT_SU_BFER_ENABLE, EHT_MODE_SU_TX_BFER);
3757 		if (link_conf->eht_mu_beamformer &&
3758 		    ahvif->vdev_type == WMI_VDEV_TYPE_AP)
3759 			value |= u32_encode_bits(EHT_MU_BFER_ENABLE,
3760 						 EHT_MODE_MU_TX_BFER) |
3761 				 u32_encode_bits(EHT_DL_MUOFDMA_ENABLE,
3762 						 EHT_MODE_DL_OFDMA_MUMIMO) |
3763 				 u32_encode_bits(EHT_UL_MUOFDMA_ENABLE,
3764 						 EHT_MODE_UL_OFDMA_MUMIMO);
3765 	}
3766 
3767 	if (ahvif->vif->type != NL80211_IFTYPE_MESH_POINT) {
3768 		value |= u32_encode_bits(EHT_DL_MUOFDMA_ENABLE, EHT_MODE_DL_OFDMA) |
3769 			 u32_encode_bits(EHT_UL_MUOFDMA_ENABLE, EHT_MODE_UL_OFDMA);
3770 
3771 		if (link_conf->eht_80mhz_full_bw_ul_mumimo)
3772 			value |= u32_encode_bits(EHT_UL_MUMIMO_ENABLE, EHT_MODE_MUMIMO);
3773 
3774 		if (link_conf->eht_su_beamformee)
3775 			value |= u32_encode_bits(EHT_SU_BFEE_ENABLE,
3776 						 EHT_MODE_SU_TX_BFEE);
3777 	}
3778 
3779 	ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id, param, value);
3780 	if (ret) {
3781 		ath12k_warn(ar->ab, "failed to set vdev %d EHT MU mode: %d\n",
3782 			    arvif->vdev_id, ret);
3783 		return ret;
3784 	}
3785 
3786 	return 0;
3787 }
3788 
3789 static u32 ath12k_mac_ieee80211_sta_bw_to_wmi(struct ath12k *ar,
3790 					      struct ieee80211_link_sta *link_sta)
3791 {
3792 	u32 bw;
3793 
3794 	switch (link_sta->bandwidth) {
3795 	case IEEE80211_STA_RX_BW_20:
3796 		bw = WMI_PEER_CHWIDTH_20MHZ;
3797 		break;
3798 	case IEEE80211_STA_RX_BW_40:
3799 		bw = WMI_PEER_CHWIDTH_40MHZ;
3800 		break;
3801 	case IEEE80211_STA_RX_BW_80:
3802 		bw = WMI_PEER_CHWIDTH_80MHZ;
3803 		break;
3804 	case IEEE80211_STA_RX_BW_160:
3805 		bw = WMI_PEER_CHWIDTH_160MHZ;
3806 		break;
3807 	case IEEE80211_STA_RX_BW_320:
3808 		bw = WMI_PEER_CHWIDTH_320MHZ;
3809 		break;
3810 	default:
3811 		ath12k_warn(ar->ab, "Invalid bandwidth %d for link station %pM\n",
3812 			    link_sta->bandwidth, link_sta->addr);
3813 		bw = WMI_PEER_CHWIDTH_20MHZ;
3814 		break;
3815 	}
3816 
3817 	return bw;
3818 }
3819 
3820 static void ath12k_bss_assoc(struct ath12k *ar,
3821 			     struct ath12k_link_vif *arvif,
3822 			     struct ieee80211_bss_conf *bss_conf)
3823 {
3824 	struct ath12k_vif *ahvif = arvif->ahvif;
3825 	struct ieee80211_vif *vif = ath12k_ahvif_to_vif(ahvif);
3826 	struct ath12k_wmi_vdev_up_params params = {};
3827 	struct ieee80211_link_sta *link_sta;
3828 	u8 link_id = bss_conf->link_id;
3829 	struct ath12k_link_sta *arsta;
3830 	struct ieee80211_sta *ap_sta;
3831 	struct ath12k_sta *ahsta;
3832 	struct ath12k_dp_link_peer *peer;
3833 	bool is_auth = false;
3834 	u32 hemode = 0;
3835 	int ret;
3836 	struct ath12k_dp *dp = ath12k_ab_to_dp(ar->ab);
3837 
3838 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
3839 
3840 	struct ath12k_wmi_peer_assoc_arg *peer_arg __free(kfree) =
3841 					kzalloc_obj(*peer_arg);
3842 	if (!peer_arg)
3843 		return;
3844 
3845 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
3846 		   "mac vdev %i link id %u assoc bssid %pM aid %d\n",
3847 		   arvif->vdev_id, link_id, arvif->bssid, ahvif->aid);
3848 
3849 	rcu_read_lock();
3850 
3851 	/* During ML connection, cfg.ap_addr has the MLD address. For
3852 	 * non-ML connection, it has the BSSID.
3853 	 */
3854 	ap_sta = ieee80211_find_sta(vif, vif->cfg.ap_addr);
3855 	if (!ap_sta) {
3856 		ath12k_warn(ar->ab, "failed to find station entry for bss %pM vdev %i\n",
3857 			    vif->cfg.ap_addr, arvif->vdev_id);
3858 		rcu_read_unlock();
3859 		return;
3860 	}
3861 
3862 	ahsta = ath12k_sta_to_ahsta(ap_sta);
3863 
3864 	arsta = wiphy_dereference(ath12k_ar_to_hw(ar)->wiphy,
3865 				  ahsta->link[link_id]);
3866 	if (WARN_ON(!arsta)) {
3867 		rcu_read_unlock();
3868 		return;
3869 	}
3870 
3871 	link_sta = ath12k_mac_get_link_sta(arsta);
3872 	if (WARN_ON(!link_sta)) {
3873 		rcu_read_unlock();
3874 		return;
3875 	}
3876 
3877 	ath12k_peer_assoc_prepare(ar, arvif, arsta, peer_arg, false);
3878 
3879 	/* link_sta->he_cap must be protected by rcu_read_lock */
3880 	ret = ath12k_mac_vif_recalc_sta_he_txbf(ar, arvif, &link_sta->he_cap, &hemode);
3881 	if (ret) {
3882 		ath12k_warn(ar->ab, "failed to recalc he txbf for vdev %i on bss %pM: %d\n",
3883 			    arvif->vdev_id, bss_conf->bssid, ret);
3884 		rcu_read_unlock();
3885 		return;
3886 	}
3887 
3888 	rcu_read_unlock();
3889 
3890 	/* keep this before ath12k_wmi_send_peer_assoc_cmd() */
3891 	ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
3892 					    WMI_VDEV_PARAM_SET_HEMU_MODE, hemode);
3893 	if (ret) {
3894 		ath12k_warn(ar->ab, "failed to submit vdev param txbf 0x%x: %d\n",
3895 			    hemode, ret);
3896 		return;
3897 	}
3898 
3899 	peer_arg->is_assoc = true;
3900 	ret = ath12k_wmi_send_peer_assoc_cmd(ar, peer_arg);
3901 	if (ret) {
3902 		ath12k_warn(ar->ab, "failed to run peer assoc for %pM vdev %i: %d\n",
3903 			    bss_conf->bssid, arvif->vdev_id, ret);
3904 		return;
3905 	}
3906 
3907 	if (!wait_for_completion_timeout(&ar->peer_assoc_done, 1 * HZ)) {
3908 		ath12k_warn(ar->ab, "failed to get peer assoc conf event for %pM vdev %i\n",
3909 			    bss_conf->bssid, arvif->vdev_id);
3910 		return;
3911 	}
3912 
3913 	ret = ath12k_setup_peer_smps(ar, arvif, bss_conf->bssid,
3914 				     &link_sta->ht_cap, &link_sta->he_6ghz_capa);
3915 	if (ret) {
3916 		ath12k_warn(ar->ab, "failed to setup peer SMPS for vdev %d: %d\n",
3917 			    arvif->vdev_id, ret);
3918 		return;
3919 	}
3920 
3921 	WARN_ON(arvif->is_up);
3922 
3923 	ahvif->aid = vif->cfg.aid;
3924 	ether_addr_copy(arvif->bssid, bss_conf->bssid);
3925 
3926 	params.vdev_id = arvif->vdev_id;
3927 	params.aid = ahvif->aid;
3928 	params.bssid = arvif->bssid;
3929 	params.tx_bssid = ath12k_mac_get_tx_bssid(arvif);
3930 	if (params.tx_bssid) {
3931 		params.nontx_profile_idx = bss_conf->bssid_index;
3932 		params.nontx_profile_cnt = 1 << bss_conf->bssid_indicator;
3933 	}
3934 	ret = ath12k_wmi_vdev_up(ar, &params);
3935 	if (ret) {
3936 		ath12k_warn(ar->ab, "failed to set vdev %d up: %d\n",
3937 			    arvif->vdev_id, ret);
3938 		return;
3939 	}
3940 
3941 	arvif->is_up = true;
3942 	arvif->rekey_data.enable_offload = false;
3943 
3944 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
3945 		   "mac vdev %d up (associated) bssid %pM aid %d\n",
3946 		   arvif->vdev_id, bss_conf->bssid, vif->cfg.aid);
3947 
3948 	spin_lock_bh(&dp->dp_lock);
3949 
3950 	peer = ath12k_dp_link_peer_find_by_vdev_and_addr(dp, arvif->vdev_id,
3951 							 arvif->bssid);
3952 	if (peer && peer->is_authorized)
3953 		is_auth = true;
3954 
3955 	spin_unlock_bh(&dp->dp_lock);
3956 
3957 	/* Authorize BSS Peer */
3958 	if (is_auth) {
3959 		ret = ath12k_wmi_set_peer_param(ar, arvif->bssid,
3960 						arvif->vdev_id,
3961 						WMI_PEER_AUTHORIZE,
3962 						1);
3963 		if (ret)
3964 			ath12k_warn(ar->ab, "Unable to authorize BSS peer: %d\n", ret);
3965 	}
3966 
3967 	ret = ath12k_wmi_send_obss_spr_cmd(ar, arvif->vdev_id,
3968 					   &bss_conf->he_obss_pd);
3969 	if (ret)
3970 		ath12k_warn(ar->ab, "failed to set vdev %i OBSS PD parameters: %d\n",
3971 			    arvif->vdev_id, ret);
3972 
3973 	if (test_bit(WMI_TLV_SERVICE_11D_OFFLOAD, ar->ab->wmi_ab.svc_map) &&
3974 	    ahvif->vdev_type == WMI_VDEV_TYPE_STA &&
3975 	    ahvif->vdev_subtype == WMI_VDEV_SUBTYPE_NONE)
3976 		ath12k_mac_11d_scan_stop_all(ar->ab);
3977 }
3978 
3979 static void ath12k_bss_disassoc(struct ath12k *ar,
3980 				struct ath12k_link_vif *arvif)
3981 {
3982 	int ret;
3983 
3984 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
3985 
3986 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac vdev %i disassoc bssid %pM\n",
3987 		   arvif->vdev_id, arvif->bssid);
3988 
3989 	ret = ath12k_wmi_vdev_down(ar, arvif->vdev_id);
3990 	if (ret)
3991 		ath12k_warn(ar->ab, "failed to down vdev %i: %d\n",
3992 			    arvif->vdev_id, ret);
3993 
3994 	arvif->is_up = false;
3995 
3996 	memset(&arvif->rekey_data, 0, sizeof(arvif->rekey_data));
3997 
3998 	cancel_delayed_work(&arvif->connection_loss_work);
3999 }
4000 
4001 static u32 ath12k_mac_get_rate_hw_value(int bitrate)
4002 {
4003 	u32 preamble;
4004 	u16 hw_value;
4005 	int rate;
4006 	size_t i;
4007 
4008 	if (ath12k_mac_bitrate_is_cck(bitrate))
4009 		preamble = WMI_RATE_PREAMBLE_CCK;
4010 	else
4011 		preamble = WMI_RATE_PREAMBLE_OFDM;
4012 
4013 	for (i = 0; i < ARRAY_SIZE(ath12k_legacy_rates); i++) {
4014 		if (ath12k_legacy_rates[i].bitrate != bitrate)
4015 			continue;
4016 
4017 		hw_value = ath12k_legacy_rates[i].hw_value;
4018 		rate = ATH12K_HW_RATE_CODE(hw_value, 0, preamble);
4019 
4020 		return rate;
4021 	}
4022 
4023 	return -EINVAL;
4024 }
4025 
4026 static void ath12k_recalculate_mgmt_rate(struct ath12k *ar,
4027 					 struct ath12k_link_vif *arvif,
4028 					 struct cfg80211_chan_def *def)
4029 {
4030 	struct ieee80211_vif *vif = ath12k_ahvif_to_vif(arvif->ahvif);
4031 	struct ieee80211_hw *hw = ath12k_ar_to_hw(ar);
4032 	const struct ieee80211_supported_band *sband;
4033 	struct ieee80211_bss_conf *bss_conf;
4034 	u8 basic_rate_idx;
4035 	int hw_rate_code;
4036 	u32 vdev_param;
4037 	u16 bitrate;
4038 	int ret;
4039 
4040 	lockdep_assert_wiphy(hw->wiphy);
4041 
4042 	bss_conf = ath12k_mac_get_link_bss_conf(arvif);
4043 	if (!bss_conf) {
4044 		ath12k_warn(ar->ab, "unable to access bss link conf in mgmt rate calc for vif %pM link %u\n",
4045 			    vif->addr, arvif->link_id);
4046 		return;
4047 	}
4048 
4049 	sband = hw->wiphy->bands[def->chan->band];
4050 	if (bss_conf->basic_rates)
4051 		basic_rate_idx = __ffs(bss_conf->basic_rates);
4052 	else
4053 		basic_rate_idx = 0;
4054 	bitrate = sband->bitrates[basic_rate_idx].bitrate;
4055 
4056 	hw_rate_code = ath12k_mac_get_rate_hw_value(bitrate);
4057 	if (hw_rate_code < 0) {
4058 		ath12k_warn(ar->ab, "bitrate not supported %d\n", bitrate);
4059 		return;
4060 	}
4061 
4062 	vdev_param = WMI_VDEV_PARAM_MGMT_RATE;
4063 	ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id, vdev_param,
4064 					    hw_rate_code);
4065 	if (ret)
4066 		ath12k_warn(ar->ab, "failed to set mgmt tx rate %d\n", ret);
4067 
4068 	vdev_param = WMI_VDEV_PARAM_BEACON_RATE;
4069 	ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id, vdev_param,
4070 					    hw_rate_code);
4071 	if (ret)
4072 		ath12k_warn(ar->ab, "failed to set beacon tx rate %d\n", ret);
4073 }
4074 
4075 static void ath12k_mac_bcn_tx_event(struct ath12k_link_vif *arvif)
4076 {
4077 	struct ieee80211_vif *vif = ath12k_ahvif_to_vif(arvif->ahvif);
4078 	struct ieee80211_bss_conf *link_conf;
4079 
4080 	link_conf = ath12k_mac_get_link_bss_conf(arvif);
4081 	if (!link_conf) {
4082 		ath12k_warn(arvif->ar->ab, "failed to get link conf for vdev %u\n",
4083 			    arvif->vdev_id);
4084 		return;
4085 	}
4086 
4087 	if (link_conf->color_change_active) {
4088 		if (ieee80211_beacon_cntdwn_is_complete(vif, arvif->link_id)) {
4089 			ieee80211_color_change_finish(vif, arvif->link_id);
4090 			return;
4091 		}
4092 
4093 		ieee80211_beacon_update_cntdwn(vif, arvif->link_id);
4094 		ath12k_mac_setup_bcn_tmpl(arvif);
4095 	}
4096 }
4097 
4098 static void ath12k_mac_bcn_tx_work(struct wiphy *wiphy, struct wiphy_work *work)
4099 {
4100 	struct ath12k_link_vif *arvif = container_of(work, struct ath12k_link_vif,
4101 						     bcn_tx_work);
4102 
4103 	lockdep_assert_wiphy(wiphy);
4104 	ath12k_mac_bcn_tx_event(arvif);
4105 }
4106 
4107 static void ath12k_mac_init_arvif(struct ath12k_vif *ahvif,
4108 				  struct ath12k_link_vif *arvif, int link_id)
4109 {
4110 	struct ath12k_hw *ah = ahvif->ah;
4111 	u8 _link_id;
4112 	int i;
4113 
4114 	lockdep_assert_wiphy(ah->hw->wiphy);
4115 
4116 	if (WARN_ON(!arvif))
4117 		return;
4118 
4119 	if (WARN_ON(link_id >= ATH12K_NUM_MAX_LINKS))
4120 		return;
4121 
4122 	if (link_id < 0)
4123 		_link_id = 0;
4124 	else
4125 		_link_id = link_id;
4126 
4127 	arvif->ahvif = ahvif;
4128 	arvif->link_id = _link_id;
4129 
4130 	/* Protects the datapath stats update on a per link basis */
4131 	spin_lock_init(&arvif->link_stats_lock);
4132 
4133 	INIT_LIST_HEAD(&arvif->list);
4134 	INIT_DELAYED_WORK(&arvif->connection_loss_work,
4135 			  ath12k_mac_vif_sta_connection_loss_work);
4136 	wiphy_work_init(&arvif->bcn_tx_work, ath12k_mac_bcn_tx_work);
4137 
4138 	arvif->num_stations = 0;
4139 
4140 	for (i = 0; i < ARRAY_SIZE(arvif->bitrate_mask.control); i++) {
4141 		arvif->bitrate_mask.control[i].legacy = 0xffffffff;
4142 		arvif->bitrate_mask.control[i].gi = NL80211_TXRATE_DEFAULT_GI;
4143 		memset(arvif->bitrate_mask.control[i].ht_mcs, 0xff,
4144 		       sizeof(arvif->bitrate_mask.control[i].ht_mcs));
4145 		memset(arvif->bitrate_mask.control[i].vht_mcs, 0xff,
4146 		       sizeof(arvif->bitrate_mask.control[i].vht_mcs));
4147 		memset(arvif->bitrate_mask.control[i].he_mcs, 0xff,
4148 		       sizeof(arvif->bitrate_mask.control[i].he_mcs));
4149 		memset(arvif->bitrate_mask.control[i].eht_mcs, 0xff,
4150 		       sizeof(arvif->bitrate_mask.control[i].eht_mcs));
4151 	}
4152 
4153 	/* Handle MLO related assignments */
4154 	if (link_id >= 0) {
4155 		rcu_assign_pointer(ahvif->link[arvif->link_id], arvif);
4156 		ahvif->links_map |= BIT(_link_id);
4157 	}
4158 
4159 	ath12k_generic_dbg(ATH12K_DBG_MAC,
4160 			   "mac init link arvif (link_id %d%s) for vif %pM. links_map 0x%x",
4161 			   _link_id, (link_id < 0) ? " deflink" : "", ahvif->vif->addr,
4162 			   ahvif->links_map);
4163 }
4164 
4165 static void ath12k_mac_remove_link_interface(struct ieee80211_hw *hw,
4166 					     struct ath12k_link_vif *arvif)
4167 {
4168 	struct ath12k_vif *ahvif = arvif->ahvif;
4169 	struct ath12k_hw *ah = hw->priv;
4170 	struct ath12k *ar = arvif->ar;
4171 	int ret;
4172 
4173 	lockdep_assert_wiphy(ah->hw->wiphy);
4174 
4175 	cancel_delayed_work_sync(&arvif->connection_loss_work);
4176 	wiphy_work_cancel(ath12k_ar_to_hw(ar)->wiphy, &arvif->bcn_tx_work);
4177 
4178 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac remove link interface (vdev %d link id %d)",
4179 		   arvif->vdev_id, arvif->link_id);
4180 
4181 	if (test_bit(WMI_TLV_SERVICE_11D_OFFLOAD, ar->ab->wmi_ab.svc_map) &&
4182 	    ahvif->vdev_type == WMI_VDEV_TYPE_STA &&
4183 	    ahvif->vdev_subtype == WMI_VDEV_SUBTYPE_NONE)
4184 		ath12k_mac_11d_scan_stop(ar);
4185 
4186 	if (ahvif->vdev_type == WMI_VDEV_TYPE_AP) {
4187 		ret = ath12k_peer_delete(ar, arvif->vdev_id, arvif->bssid);
4188 		if (ret)
4189 			ath12k_warn(ar->ab, "failed to submit AP self-peer removal on vdev %d link id %d: %d",
4190 				    arvif->vdev_id, arvif->link_id, ret);
4191 
4192 		if (arvif->link_id < IEEE80211_MLD_MAX_NUM_LINKS)
4193 			ath12k_dp_peer_delete(&ah->dp_hw, arvif->bssid, NULL);
4194 	}
4195 	ath12k_mac_vdev_delete(ar, arvif);
4196 }
4197 
4198 static struct ath12k_link_vif *ath12k_mac_assign_link_vif(struct ath12k_hw *ah,
4199 							  struct ieee80211_vif *vif,
4200 							  u8 link_id)
4201 {
4202 	struct ath12k_vif *ahvif = ath12k_vif_to_ahvif(vif);
4203 	struct ath12k_link_vif *arvif;
4204 
4205 	lockdep_assert_wiphy(ah->hw->wiphy);
4206 
4207 	arvif = wiphy_dereference(ah->hw->wiphy, ahvif->link[link_id]);
4208 	if (arvif)
4209 		return arvif;
4210 
4211 	/* If this is the first link arvif being created for an ML VIF
4212 	 * use the preallocated deflink memory except for scan arvifs
4213 	 */
4214 	if (!ahvif->links_map && link_id < ATH12K_FIRST_SCAN_LINK) {
4215 		arvif = &ahvif->deflink;
4216 
4217 		if (vif->type == NL80211_IFTYPE_STATION)
4218 			arvif->is_sta_assoc_link = true;
4219 	} else {
4220 		arvif = kzalloc_obj(*arvif);
4221 		if (!arvif)
4222 			return NULL;
4223 	}
4224 
4225 	ath12k_mac_init_arvif(ahvif, arvif, link_id);
4226 
4227 	return arvif;
4228 }
4229 
4230 static void ath12k_mac_unassign_link_vif(struct ath12k_link_vif *arvif)
4231 {
4232 	struct ath12k_vif *ahvif = arvif->ahvif;
4233 	struct ath12k_hw *ah = ahvif->ah;
4234 
4235 	lockdep_assert_wiphy(ah->hw->wiphy);
4236 
4237 	rcu_assign_pointer(ahvif->link[arvif->link_id], NULL);
4238 	synchronize_rcu();
4239 	ahvif->links_map &= ~BIT(arvif->link_id);
4240 
4241 	if (arvif != &ahvif->deflink)
4242 		kfree(arvif);
4243 	else
4244 		memset(arvif, 0, sizeof(*arvif));
4245 }
4246 
4247 int
4248 ath12k_mac_op_change_vif_links(struct ieee80211_hw *hw,
4249 			       struct ieee80211_vif *vif,
4250 			       u16 old_links, u16 new_links,
4251 			       struct ieee80211_bss_conf *ol[IEEE80211_MLD_MAX_NUM_LINKS])
4252 {
4253 	struct ath12k_vif *ahvif = ath12k_vif_to_ahvif(vif);
4254 	unsigned long to_remove = old_links & ~new_links;
4255 	unsigned long to_add = ~old_links & new_links;
4256 	struct ath12k_hw *ah = ath12k_hw_to_ah(hw);
4257 	struct ath12k_link_vif *arvif;
4258 	u8 link_id;
4259 
4260 	lockdep_assert_wiphy(hw->wiphy);
4261 
4262 	ath12k_generic_dbg(ATH12K_DBG_MAC,
4263 			   "mac vif link changed for MLD %pM old_links 0x%x new_links 0x%x\n",
4264 			   vif->addr, old_links, new_links);
4265 
4266 	for_each_set_bit(link_id, &to_add, IEEE80211_MLD_MAX_NUM_LINKS) {
4267 		arvif = wiphy_dereference(hw->wiphy, ahvif->link[link_id]);
4268 		/* mac80211 wants to add link but driver already has the
4269 		 * link. This should not happen ideally.
4270 		 */
4271 		if (WARN_ON(arvif))
4272 			return -EINVAL;
4273 
4274 		arvif = ath12k_mac_assign_link_vif(ah, vif, link_id);
4275 		if (WARN_ON(!arvif))
4276 			return -EINVAL;
4277 	}
4278 
4279 	for_each_set_bit(link_id, &to_remove, IEEE80211_MLD_MAX_NUM_LINKS) {
4280 		arvif = wiphy_dereference(hw->wiphy, ahvif->link[link_id]);
4281 		if (WARN_ON(!arvif))
4282 			return -EINVAL;
4283 
4284 		if (!arvif->is_created) {
4285 			ath12k_mac_unassign_link_vif(arvif);
4286 			continue;
4287 		}
4288 
4289 		if (WARN_ON(!arvif->ar))
4290 			return -EINVAL;
4291 
4292 		ath12k_mac_remove_link_interface(hw, arvif);
4293 		ath12k_mac_unassign_link_vif(arvif);
4294 	}
4295 
4296 	return 0;
4297 }
4298 EXPORT_SYMBOL(ath12k_mac_op_change_vif_links);
4299 
4300 static int ath12k_mac_fils_discovery(struct ath12k_link_vif *arvif,
4301 				     struct ieee80211_bss_conf *info)
4302 {
4303 	struct ieee80211_vif *vif = ath12k_ahvif_to_vif(arvif->ahvif);
4304 	struct ath12k *ar = arvif->ar;
4305 	struct ieee80211_hw *hw = ath12k_ar_to_hw(ar);
4306 	struct sk_buff *tmpl;
4307 	int ret;
4308 	u32 interval;
4309 	bool unsol_bcast_probe_resp_enabled = false;
4310 
4311 	if (info->fils_discovery.max_interval) {
4312 		interval = info->fils_discovery.max_interval;
4313 
4314 		tmpl = ieee80211_get_fils_discovery_tmpl(hw, vif,
4315 							 info->link_id);
4316 		if (tmpl)
4317 			ret = ath12k_wmi_fils_discovery_tmpl(ar, arvif->vdev_id,
4318 							     tmpl);
4319 	} else if (info->unsol_bcast_probe_resp_interval) {
4320 		unsol_bcast_probe_resp_enabled = 1;
4321 		interval = info->unsol_bcast_probe_resp_interval;
4322 
4323 		tmpl = ieee80211_get_unsol_bcast_probe_resp_tmpl(hw, vif,
4324 								 info->link_id);
4325 		if (tmpl)
4326 			ret = ath12k_wmi_probe_resp_tmpl(ar, arvif->vdev_id,
4327 							 tmpl);
4328 	} else { /* Disable */
4329 		return ath12k_wmi_fils_discovery(ar, arvif->vdev_id, 0, false);
4330 	}
4331 
4332 	if (!tmpl) {
4333 		ath12k_warn(ar->ab,
4334 			    "mac vdev %i failed to retrieve %s template\n",
4335 			    arvif->vdev_id, (unsol_bcast_probe_resp_enabled ?
4336 			    "unsolicited broadcast probe response" :
4337 			    "FILS discovery"));
4338 		return -EPERM;
4339 	}
4340 	kfree_skb(tmpl);
4341 
4342 	if (!ret)
4343 		ret = ath12k_wmi_fils_discovery(ar, arvif->vdev_id, interval,
4344 						unsol_bcast_probe_resp_enabled);
4345 
4346 	return ret;
4347 }
4348 
4349 void ath12k_mac_op_vif_cfg_changed(struct ieee80211_hw *hw,
4350 				   struct ieee80211_vif *vif,
4351 				   u64 changed)
4352 {
4353 	struct ath12k_vif *ahvif = ath12k_vif_to_ahvif(vif);
4354 	unsigned long links = ahvif->links_map;
4355 	struct ieee80211_bss_conf *info;
4356 	struct ath12k_link_vif *arvif;
4357 	struct ieee80211_sta *sta;
4358 	struct ath12k_sta *ahsta;
4359 	struct ath12k *ar;
4360 	u8 link_id;
4361 
4362 	lockdep_assert_wiphy(hw->wiphy);
4363 
4364 	if (changed & BSS_CHANGED_SSID && vif->type == NL80211_IFTYPE_AP) {
4365 		ahvif->u.ap.ssid_len = vif->cfg.ssid_len;
4366 		if (vif->cfg.ssid_len)
4367 			memcpy(ahvif->u.ap.ssid, vif->cfg.ssid, vif->cfg.ssid_len);
4368 	}
4369 
4370 	if (changed & BSS_CHANGED_ASSOC) {
4371 		if (vif->cfg.assoc) {
4372 			/* only in station mode we can get here, so it's safe
4373 			 * to use ap_addr
4374 			 */
4375 			rcu_read_lock();
4376 			sta = ieee80211_find_sta(vif, vif->cfg.ap_addr);
4377 			if (!sta) {
4378 				rcu_read_unlock();
4379 				WARN_ONCE(1, "failed to find sta with addr %pM\n",
4380 					  vif->cfg.ap_addr);
4381 				return;
4382 			}
4383 
4384 			ahsta = ath12k_sta_to_ahsta(sta);
4385 			arvif = wiphy_dereference(hw->wiphy,
4386 						  ahvif->link[ahsta->assoc_link_id]);
4387 			rcu_read_unlock();
4388 
4389 			ar = arvif->ar;
4390 			/* there is no reason for which an assoc link's
4391 			 * bss info does not exist
4392 			 */
4393 			info = ath12k_mac_get_link_bss_conf(arvif);
4394 			ath12k_bss_assoc(ar, arvif, info);
4395 
4396 			/* exclude assoc link as it is done above */
4397 			links &= ~BIT(ahsta->assoc_link_id);
4398 		}
4399 
4400 		for_each_set_bit(link_id, &links, IEEE80211_MLD_MAX_NUM_LINKS) {
4401 			arvif = wiphy_dereference(hw->wiphy, ahvif->link[link_id]);
4402 			if (!arvif || !arvif->ar)
4403 				continue;
4404 
4405 			ar = arvif->ar;
4406 
4407 			if (vif->cfg.assoc) {
4408 				info = ath12k_mac_get_link_bss_conf(arvif);
4409 				if (!info)
4410 					continue;
4411 
4412 				ath12k_bss_assoc(ar, arvif, info);
4413 			} else {
4414 				ath12k_bss_disassoc(ar, arvif);
4415 			}
4416 		}
4417 	}
4418 }
4419 EXPORT_SYMBOL(ath12k_mac_op_vif_cfg_changed);
4420 
4421 static void ath12k_mac_vif_setup_ps(struct ath12k_link_vif *arvif)
4422 {
4423 	struct ath12k *ar = arvif->ar;
4424 	struct ieee80211_vif *vif = arvif->ahvif->vif;
4425 	struct ieee80211_conf *conf = &ath12k_ar_to_hw(ar)->conf;
4426 	enum wmi_sta_powersave_param param;
4427 	struct ieee80211_bss_conf *info;
4428 	enum wmi_sta_ps_mode psmode;
4429 	int ret;
4430 	int timeout;
4431 	bool enable_ps;
4432 
4433 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
4434 
4435 	if (vif->type != NL80211_IFTYPE_STATION)
4436 		return;
4437 
4438 	enable_ps = arvif->ahvif->ps;
4439 	if (enable_ps) {
4440 		psmode = WMI_STA_PS_MODE_ENABLED;
4441 		param = WMI_STA_PS_PARAM_INACTIVITY_TIME;
4442 
4443 		timeout = conf->dynamic_ps_timeout;
4444 		if (timeout == 0) {
4445 			info = ath12k_mac_get_link_bss_conf(arvif);
4446 			if (!info) {
4447 				ath12k_warn(ar->ab, "unable to access bss link conf in setup ps for vif %pM link %u\n",
4448 					    vif->addr, arvif->link_id);
4449 				return;
4450 			}
4451 
4452 			/* firmware doesn't like 0 */
4453 			timeout = ieee80211_tu_to_usec(info->beacon_int) / 1000;
4454 		}
4455 
4456 		ret = ath12k_wmi_set_sta_ps_param(ar, arvif->vdev_id, param,
4457 						  timeout);
4458 		if (ret) {
4459 			ath12k_warn(ar->ab, "failed to set inactivity time for vdev %d: %i\n",
4460 				    arvif->vdev_id, ret);
4461 			return;
4462 		}
4463 	} else {
4464 		psmode = WMI_STA_PS_MODE_DISABLED;
4465 	}
4466 
4467 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac vdev %d psmode %s\n",
4468 		   arvif->vdev_id, psmode ? "enable" : "disable");
4469 
4470 	ret = ath12k_wmi_pdev_set_ps_mode(ar, arvif->vdev_id, psmode);
4471 	if (ret)
4472 		ath12k_warn(ar->ab, "failed to set sta power save mode %d for vdev %d: %d\n",
4473 			    psmode, arvif->vdev_id, ret);
4474 }
4475 
4476 static bool ath12k_mac_supports_tpc(struct ath12k *ar, struct ath12k_vif *ahvif,
4477 				    const struct cfg80211_chan_def *chandef)
4478 {
4479 	return ath12k_wmi_supports_6ghz_cc_ext(ar) &&
4480 		test_bit(WMI_TLV_SERVICE_EXT_TPC_REG_SUPPORT, ar->ab->wmi_ab.svc_map) &&
4481 		(ahvif->vdev_type == WMI_VDEV_TYPE_STA  ||
4482 		 ahvif->vdev_type == WMI_VDEV_TYPE_AP) &&
4483 		ahvif->vdev_subtype == WMI_VDEV_SUBTYPE_NONE &&
4484 		chandef->chan &&
4485 		chandef->chan->band == NL80211_BAND_6GHZ;
4486 }
4487 
4488 static void ath12k_wmi_vdev_params_up(struct ath12k *ar,
4489 				      struct ath12k_link_vif *arvif,
4490 				      struct ath12k_link_vif *tx_arvif,
4491 				      struct ieee80211_bss_conf *info, u16 aid)
4492 {
4493 	struct ath12k_wmi_vdev_up_params params = {
4494 		.vdev_id = arvif->vdev_id,
4495 		.aid = aid,
4496 		.bssid = arvif->bssid
4497 	};
4498 	int ret;
4499 
4500 	if (tx_arvif) {
4501 		params.tx_bssid = tx_arvif->bssid;
4502 		params.nontx_profile_idx = info->bssid_index;
4503 		params.nontx_profile_cnt = 1 << info->bssid_indicator;
4504 	}
4505 
4506 	ret = ath12k_wmi_vdev_up(arvif->ar, &params);
4507 	if (ret)
4508 		ath12k_warn(ar->ab, "failed to bring vdev up %d: %d\n",
4509 			    arvif->vdev_id, ret);
4510 }
4511 
4512 static int ath12k_mac_config_obss_pd(struct ath12k_link_vif *arvif,
4513 				     const struct ieee80211_he_obss_pd *he_obss_pd)
4514 {
4515 	struct ath12k_wmi_obss_pd_arg obss_pd_arg = {};
4516 	u32 srg_bitmap[2], non_srg_bitmap[2];
4517 	struct ath12k *ar = arvif->ar;
4518 	u32 param_id, pdev_id;
4519 	u32 param_val;
4520 	int ret;
4521 
4522 	if (ar->ab->hw_params->single_pdev_only)
4523 		pdev_id = ath12k_mac_get_target_pdev_id_from_vif(arvif);
4524 	else
4525 		pdev_id = ar->pdev->pdev_id;
4526 
4527 	/* Set and enable SRG/non-SRG OBSS PD threshold */
4528 	param_id = WMI_PDEV_PARAM_SET_CMD_OBSS_PD_THRESHOLD;
4529 	if (ar->monitor_started || !he_obss_pd->enable) {
4530 		ret = ath12k_wmi_pdev_set_param(ar, param_id, 0, pdev_id);
4531 		if (ret)
4532 			ath12k_warn(ar->ab,
4533 				    "failed to set OBSS PD threshold for pdev %u: %d\n",
4534 				    pdev_id, ret);
4535 		return ret;
4536 	}
4537 
4538 	/*
4539 	 * This service flag indicates firmware support for SRG/SRP-based
4540 	 * spatial reuse. It also specifies whether OBSS PD threshold values
4541 	 * should be interpreted as dB (offset) or dBm (absolute) units.
4542 	 */
4543 	obss_pd_arg.srp_support = test_bit(WMI_TLV_SERVICE_SRG_SRP_SPATIAL_REUSE_SUPPORT,
4544 					   ar->ab->wmi_ab.svc_map);
4545 
4546 	if (!(he_obss_pd->sr_ctrl &
4547 	      IEEE80211_HE_SPR_NON_SRG_OBSS_PD_SR_DISALLOWED)) {
4548 		if (he_obss_pd->sr_ctrl & IEEE80211_HE_SPR_NON_SRG_OFFSET_PRESENT)
4549 			obss_pd_arg.non_srg_th = ATH12K_OBSS_PD_MAX_THRESHOLD +
4550 						 he_obss_pd->non_srg_max_offset;
4551 		else
4552 			obss_pd_arg.non_srg_th = ATH12K_OBSS_PD_NON_SRG_MAX_THRESHOLD;
4553 
4554 		if (!obss_pd_arg.srp_support)
4555 			obss_pd_arg.non_srg_th -= ATH12K_DEFAULT_NOISE_FLOOR;
4556 
4557 		obss_pd_arg.non_srg_enabled = true;
4558 	}
4559 
4560 	if (he_obss_pd->sr_ctrl & IEEE80211_HE_SPR_SRG_INFORMATION_PRESENT) {
4561 		obss_pd_arg.srg_th = ATH12K_OBSS_PD_MAX_THRESHOLD +
4562 				     he_obss_pd->max_offset;
4563 		obss_pd_arg.srg_enabled = true;
4564 	}
4565 
4566 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
4567 		   "pdev %u OBSS PD sr_ctrl 0x%x srg_th %d dBm non_srg_th %d dBm\n",
4568 		   pdev_id, he_obss_pd->sr_ctrl,
4569 		   obss_pd_arg.srg_th, obss_pd_arg.non_srg_th);
4570 
4571 	param_val = ath12k_wmi_build_obss_pd(&obss_pd_arg);
4572 	ret = ath12k_wmi_pdev_set_param(ar, param_id, param_val, pdev_id);
4573 	if (ret) {
4574 		ath12k_warn(ar->ab,
4575 			    "failed to set OBSS PD threshold for pdev %u: %d\n",
4576 			    pdev_id, ret);
4577 		return ret;
4578 	}
4579 
4580 	/* Enable OBSS PD for all access category */
4581 	param_id  = WMI_PDEV_PARAM_SET_CMD_OBSS_PD_PER_AC;
4582 	param_val = 0xf;
4583 	ret = ath12k_wmi_pdev_set_param(ar, param_id, param_val, pdev_id);
4584 	if (ret) {
4585 		ath12k_warn(ar->ab,
4586 			    "failed to set OBSS PD per ac for pdev %u: %d\n",
4587 			    pdev_id, ret);
4588 		return ret;
4589 	}
4590 
4591 	/* Set SR prohibit */
4592 	param_id  = WMI_PDEV_PARAM_ENABLE_SR_PROHIBIT;
4593 	param_val = !!(he_obss_pd->sr_ctrl &
4594 		       IEEE80211_HE_SPR_HESIGA_SR_VAL15_ALLOWED);
4595 	ret = ath12k_wmi_pdev_set_param(ar, param_id, param_val, pdev_id);
4596 	if (ret) {
4597 		ath12k_warn(ar->ab, "failed to set SR prohibit for pdev %u: %d\n",
4598 			    pdev_id, ret);
4599 		return ret;
4600 	}
4601 
4602 	if (!obss_pd_arg.srp_support)
4603 		return 0;
4604 
4605 	memcpy(srg_bitmap, he_obss_pd->bss_color_bitmap, sizeof(srg_bitmap));
4606 	/* Set SRG BSS color bitmap */
4607 	ret = ath12k_wmi_pdev_set_srg_bss_color_bitmap(ar, pdev_id, srg_bitmap);
4608 	if (ret) {
4609 		ath12k_warn(ar->ab,
4610 			    "failed to set SRG bss color bitmap for pdev %u: %d\n",
4611 			    pdev_id, ret);
4612 		return ret;
4613 	}
4614 
4615 	/* Enable BSS colors for SRG */
4616 	ret = ath12k_wmi_pdev_srg_obss_color_enable_bitmap(ar, pdev_id, srg_bitmap);
4617 	if (ret) {
4618 		ath12k_warn(ar->ab,
4619 			    "failed to enable SRG bss color bitmap pdev %u: %d\n",
4620 			    pdev_id, ret);
4621 		return ret;
4622 	}
4623 
4624 	memcpy(srg_bitmap, he_obss_pd->partial_bssid_bitmap, sizeof(srg_bitmap));
4625 	/* Set SRG partial bssid bitmap */
4626 	ret = ath12k_wmi_pdev_set_srg_partial_bssid_bitmap(ar, pdev_id, srg_bitmap);
4627 	if (ret) {
4628 		ath12k_warn(ar->ab,
4629 			    "failed to set SRG partial bssid bitmap for pdev %u: %d\n",
4630 			    pdev_id, ret);
4631 		return ret;
4632 	}
4633 
4634 	/* Enable partial bssid mask for SRG */
4635 	ret = ath12k_wmi_pdev_srg_obss_bssid_enable_bitmap(ar, pdev_id, srg_bitmap);
4636 	if (ret) {
4637 		ath12k_warn(ar->ab,
4638 			    "failed to enable SRG bssid bitmap pdev %u: %d\n",
4639 			    pdev_id, ret);
4640 		return ret;
4641 	}
4642 
4643 	/*
4644 	 * No explicit non-SRG bitmap from mac80211; enable all colors/bssids
4645 	 * as non-SRG candidates. Actual SRG members are filtered by SRG bitmaps.
4646 	 */
4647 	memset(non_srg_bitmap, 0xff, sizeof(non_srg_bitmap));
4648 
4649 	/* Enable BSS colors for non-SRG */
4650 	ret = ath12k_wmi_pdev_non_srg_obss_color_enable_bitmap(ar, pdev_id,
4651 							       non_srg_bitmap);
4652 	if (ret) {
4653 		ath12k_warn(ar->ab,
4654 			    "failed to enable non SRG color bitmap pdev %u: %d\n",
4655 			    pdev_id, ret);
4656 		return ret;
4657 	}
4658 
4659 	/* Enable partial bssid mask for non-SRG */
4660 	ret = ath12k_wmi_pdev_non_srg_obss_bssid_enable_bitmap(ar, pdev_id,
4661 							       non_srg_bitmap);
4662 	if (ret) {
4663 		ath12k_warn(ar->ab,
4664 			    "failed to enable non SRG bssid bitmap pdev %u: %d\n",
4665 			    pdev_id, ret);
4666 		return ret;
4667 	}
4668 
4669 	return 0;
4670 }
4671 
4672 static void ath12k_mac_bss_info_changed(struct ath12k *ar,
4673 					struct ath12k_link_vif *arvif,
4674 					struct ieee80211_bss_conf *info,
4675 					u64 changed)
4676 {
4677 	struct ath12k_vif *ahvif = arvif->ahvif;
4678 	struct ieee80211_vif *vif = ath12k_ahvif_to_vif(ahvif);
4679 	struct ieee80211_vif_cfg *vif_cfg = &vif->cfg;
4680 	struct ath12k_link_vif *tx_arvif;
4681 	struct cfg80211_chan_def def;
4682 	u32 param_id, param_value;
4683 	enum nl80211_band band;
4684 	u32 vdev_param;
4685 	int mcast_rate;
4686 	u32 preamble;
4687 	u16 hw_value;
4688 	u16 bitrate;
4689 	u8 rateidx;
4690 	u32 rate;
4691 	int ret;
4692 
4693 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
4694 
4695 	if (changed & BSS_CHANGED_BEACON_INT) {
4696 		arvif->beacon_interval = info->beacon_int;
4697 
4698 		param_id = WMI_VDEV_PARAM_BEACON_INTERVAL;
4699 		ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
4700 						    param_id,
4701 						    arvif->beacon_interval);
4702 		if (ret)
4703 			ath12k_warn(ar->ab, "Failed to set beacon interval for VDEV: %d\n",
4704 				    arvif->vdev_id);
4705 		else
4706 			ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
4707 				   "Beacon interval: %d set for VDEV: %d\n",
4708 				   arvif->beacon_interval, arvif->vdev_id);
4709 	}
4710 
4711 	if (changed & BSS_CHANGED_BEACON) {
4712 		param_id = WMI_PDEV_PARAM_BEACON_TX_MODE;
4713 		param_value = WMI_BEACON_BURST_MODE;
4714 		ret = ath12k_wmi_pdev_set_param(ar, param_id,
4715 						param_value, ar->pdev->pdev_id);
4716 		if (ret)
4717 			ath12k_warn(ar->ab, "Failed to set beacon mode for VDEV: %d\n",
4718 				    arvif->vdev_id);
4719 		else
4720 			ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
4721 				   "Set burst beacon mode for VDEV: %d\n",
4722 				   arvif->vdev_id);
4723 
4724 		/* In MBSSID case, need to install transmitting VIF's template first */
4725 
4726 		ret = ath12k_mac_setup_bcn_tmpl(arvif);
4727 		if (ret)
4728 			ath12k_warn(ar->ab, "failed to update bcn template: %d\n",
4729 				    ret);
4730 
4731 		if (!arvif->is_csa_in_progress)
4732 			goto skip_vdev_up;
4733 
4734 		tx_arvif = ath12k_mac_get_tx_arvif(arvif, info);
4735 		if (tx_arvif && arvif != tx_arvif && tx_arvif->is_csa_in_progress)
4736 			/* skip non tx vif's */
4737 			goto skip_vdev_up;
4738 
4739 		ath12k_wmi_vdev_params_up(ar, arvif, tx_arvif, info, ahvif->aid);
4740 
4741 		arvif->is_csa_in_progress = false;
4742 
4743 		if (tx_arvif && arvif == tx_arvif) {
4744 			struct ath12k_link_vif *arvif_itr;
4745 
4746 			list_for_each_entry(arvif_itr, &ar->arvifs, list) {
4747 				if (!arvif_itr->is_csa_in_progress)
4748 					continue;
4749 
4750 				ath12k_wmi_vdev_params_up(ar, arvif, tx_arvif,
4751 							  info, ahvif->aid);
4752 				arvif_itr->is_csa_in_progress = false;
4753 			}
4754 		}
4755 	}
4756 
4757 skip_vdev_up:
4758 
4759 	if (changed & (BSS_CHANGED_BEACON_INFO | BSS_CHANGED_BEACON)) {
4760 		arvif->dtim_period = info->dtim_period;
4761 
4762 		param_id = WMI_VDEV_PARAM_DTIM_PERIOD;
4763 		ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
4764 						    param_id,
4765 						    arvif->dtim_period);
4766 
4767 		if (ret)
4768 			ath12k_warn(ar->ab, "Failed to set dtim period for VDEV %d: %i\n",
4769 				    arvif->vdev_id, ret);
4770 		else
4771 			ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
4772 				   "DTIM period: %d set for VDEV: %d\n",
4773 				   arvif->dtim_period, arvif->vdev_id);
4774 	}
4775 
4776 	if (changed & BSS_CHANGED_SSID &&
4777 	    vif->type == NL80211_IFTYPE_AP) {
4778 		ahvif->u.ap.ssid_len = vif->cfg.ssid_len;
4779 		if (vif->cfg.ssid_len)
4780 			memcpy(ahvif->u.ap.ssid, vif->cfg.ssid, vif->cfg.ssid_len);
4781 		ahvif->u.ap.hidden_ssid = info->hidden_ssid;
4782 	}
4783 
4784 	if (changed & BSS_CHANGED_BSSID && !is_zero_ether_addr(info->bssid))
4785 		ether_addr_copy(arvif->bssid, info->bssid);
4786 
4787 	if (changed & BSS_CHANGED_BEACON_ENABLED) {
4788 		if (info->enable_beacon) {
4789 			ret = ath12k_mac_set_he_txbf_conf(arvif);
4790 			if (ret)
4791 				ath12k_warn(ar->ab,
4792 					    "failed to set HE TXBF config for vdev: %d\n",
4793 					    arvif->vdev_id);
4794 
4795 			ret = ath12k_mac_set_eht_txbf_conf(arvif);
4796 			if (ret)
4797 				ath12k_warn(ar->ab,
4798 					    "failed to set EHT TXBF config for vdev: %d\n",
4799 					    arvif->vdev_id);
4800 		}
4801 		ath12k_control_beaconing(arvif, info);
4802 
4803 		if (arvif->is_up && info->he_support &&
4804 		    info->he_oper.params) {
4805 			/* TODO: Extend to support 1024 BA Bitmap size */
4806 			ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
4807 							    WMI_VDEV_PARAM_BA_MODE,
4808 							    WMI_BA_MODE_BUFFER_SIZE_256);
4809 			if (ret)
4810 				ath12k_warn(ar->ab,
4811 					    "failed to set BA BUFFER SIZE 256 for vdev: %d\n",
4812 					    arvif->vdev_id);
4813 
4814 			param_id = WMI_VDEV_PARAM_HEOPS_0_31;
4815 			param_value = info->he_oper.params;
4816 			ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
4817 							    param_id, param_value);
4818 			ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
4819 				   "he oper param: %x set for VDEV: %d\n",
4820 				   param_value, arvif->vdev_id);
4821 
4822 			if (ret)
4823 				ath12k_warn(ar->ab, "Failed to set he oper params %x for VDEV %d: %i\n",
4824 					    param_value, arvif->vdev_id, ret);
4825 		}
4826 	}
4827 
4828 	if (changed & BSS_CHANGED_ERP_CTS_PROT) {
4829 		u32 cts_prot;
4830 
4831 		cts_prot = !!(info->use_cts_prot);
4832 		param_id = WMI_VDEV_PARAM_PROTECTION_MODE;
4833 
4834 		if (arvif->is_started) {
4835 			ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
4836 							    param_id, cts_prot);
4837 			if (ret)
4838 				ath12k_warn(ar->ab, "Failed to set CTS prot for VDEV: %d\n",
4839 					    arvif->vdev_id);
4840 			else
4841 				ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "Set CTS prot: %d for VDEV: %d\n",
4842 					   cts_prot, arvif->vdev_id);
4843 		} else {
4844 			ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "defer protection mode setup, vdev is not ready yet\n");
4845 		}
4846 	}
4847 
4848 	if (changed & BSS_CHANGED_ERP_SLOT) {
4849 		u32 slottime;
4850 
4851 		if (info->use_short_slot)
4852 			slottime = WMI_VDEV_SLOT_TIME_SHORT; /* 9us */
4853 
4854 		else
4855 			slottime = WMI_VDEV_SLOT_TIME_LONG; /* 20us */
4856 
4857 		param_id = WMI_VDEV_PARAM_SLOT_TIME;
4858 		ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
4859 						    param_id, slottime);
4860 		if (ret)
4861 			ath12k_warn(ar->ab, "Failed to set erp slot for VDEV: %d\n",
4862 				    arvif->vdev_id);
4863 		else
4864 			ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
4865 				   "Set slottime: %d for VDEV: %d\n",
4866 				   slottime, arvif->vdev_id);
4867 	}
4868 
4869 	if (changed & BSS_CHANGED_ERP_PREAMBLE) {
4870 		u32 preamble;
4871 
4872 		if (info->use_short_preamble)
4873 			preamble = WMI_VDEV_PREAMBLE_SHORT;
4874 		else
4875 			preamble = WMI_VDEV_PREAMBLE_LONG;
4876 
4877 		param_id = WMI_VDEV_PARAM_PREAMBLE;
4878 		ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
4879 						    param_id, preamble);
4880 		if (ret)
4881 			ath12k_warn(ar->ab, "Failed to set preamble for VDEV: %d\n",
4882 				    arvif->vdev_id);
4883 		else
4884 			ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
4885 				   "Set preamble: %d for VDEV: %d\n",
4886 				   preamble, arvif->vdev_id);
4887 	}
4888 
4889 	if (changed & BSS_CHANGED_ASSOC) {
4890 		if (vif->cfg.assoc)
4891 			ath12k_bss_assoc(ar, arvif, info);
4892 		else
4893 			ath12k_bss_disassoc(ar, arvif);
4894 	}
4895 
4896 	if (changed & BSS_CHANGED_TXPOWER) {
4897 		ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac vdev_id %i txpower %d\n",
4898 			   arvif->vdev_id, info->txpower);
4899 
4900 		arvif->txpower = info->txpower;
4901 		ath12k_mac_txpower_recalc(ar);
4902 	}
4903 
4904 	if (changed & BSS_CHANGED_MCAST_RATE &&
4905 	    !ath12k_mac_vif_link_chan(vif, arvif->link_id, &def)) {
4906 		band = def.chan->band;
4907 		mcast_rate = info->mcast_rate[band];
4908 
4909 		if (mcast_rate > 0) {
4910 			rateidx = mcast_rate - 1;
4911 		} else {
4912 			if (info->basic_rates)
4913 				rateidx = __ffs(info->basic_rates);
4914 			else
4915 				rateidx = 0;
4916 		}
4917 
4918 		if (ar->pdev->cap.supported_bands & WMI_HOST_WLAN_5GHZ_CAP)
4919 			rateidx += ATH12K_MAC_FIRST_OFDM_RATE_IDX;
4920 
4921 		bitrate = ath12k_legacy_rates[rateidx].bitrate;
4922 		hw_value = ath12k_legacy_rates[rateidx].hw_value;
4923 
4924 		if (ath12k_mac_bitrate_is_cck(bitrate))
4925 			preamble = WMI_RATE_PREAMBLE_CCK;
4926 		else
4927 			preamble = WMI_RATE_PREAMBLE_OFDM;
4928 
4929 		rate = ATH12K_HW_RATE_CODE(hw_value, 0, preamble);
4930 
4931 		ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
4932 			   "mac vdev %d mcast_rate %x\n",
4933 			   arvif->vdev_id, rate);
4934 
4935 		vdev_param = WMI_VDEV_PARAM_MCAST_DATA_RATE;
4936 		ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
4937 						    vdev_param, rate);
4938 		if (ret)
4939 			ath12k_warn(ar->ab,
4940 				    "failed to set mcast rate on vdev %i: %d\n",
4941 				    arvif->vdev_id,  ret);
4942 
4943 		vdev_param = WMI_VDEV_PARAM_BCAST_DATA_RATE;
4944 		ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
4945 						    vdev_param, rate);
4946 		if (ret)
4947 			ath12k_warn(ar->ab,
4948 				    "failed to set bcast rate on vdev %i: %d\n",
4949 				    arvif->vdev_id,  ret);
4950 	}
4951 
4952 	if (changed & BSS_CHANGED_BASIC_RATES &&
4953 	    !ath12k_mac_vif_link_chan(vif, arvif->link_id, &def))
4954 		ath12k_recalculate_mgmt_rate(ar, arvif, &def);
4955 
4956 	if (changed & BSS_CHANGED_TWT) {
4957 		if (info->twt_requester || info->twt_responder)
4958 			ath12k_wmi_send_twt_enable_cmd(ar, ar->pdev->pdev_id);
4959 		else
4960 			ath12k_wmi_send_twt_disable_cmd(ar, ar->pdev->pdev_id);
4961 	}
4962 
4963 	if (changed & BSS_CHANGED_HE_OBSS_PD) {
4964 		if (vif->type == NL80211_IFTYPE_AP)
4965 			ath12k_mac_config_obss_pd(arvif, &info->he_obss_pd);
4966 		else
4967 			ath12k_wmi_send_obss_spr_cmd(ar, arvif->vdev_id,
4968 						     &info->he_obss_pd);
4969 	}
4970 
4971 	if (changed & BSS_CHANGED_HE_BSS_COLOR) {
4972 		if (vif->type == NL80211_IFTYPE_AP) {
4973 			ret = ath12k_wmi_obss_color_cfg_cmd(ar,
4974 							    arvif->vdev_id,
4975 							    info->he_bss_color.color,
4976 							    ATH12K_BSS_COLOR_AP_PERIODS,
4977 							    info->he_bss_color.enabled);
4978 			if (ret)
4979 				ath12k_warn(ar->ab, "failed to set bss color collision on vdev %u: %d\n",
4980 					    arvif->vdev_id,  ret);
4981 
4982 			param_id = WMI_VDEV_PARAM_BSS_COLOR;
4983 			if (info->he_bss_color.enabled)
4984 				param_value = info->he_bss_color.color <<
4985 					      IEEE80211_HE_OPERATION_BSS_COLOR_OFFSET;
4986 			else
4987 				param_value = IEEE80211_HE_OPERATION_BSS_COLOR_DISABLED;
4988 
4989 			ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
4990 							    param_id,
4991 							    param_value);
4992 			if (ret)
4993 				ath12k_warn(ar->ab, "failed to set bss color param on vdev %u: %d\n",
4994 					    arvif->vdev_id,  ret);
4995 			else
4996 				ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "bss color param 0x%x set on vdev %u\n",
4997 					   param_value, arvif->vdev_id);
4998 		} else if (vif->type == NL80211_IFTYPE_STATION) {
4999 			ret = ath12k_wmi_send_bss_color_change_enable_cmd(ar,
5000 									  arvif->vdev_id,
5001 									  1);
5002 			if (ret)
5003 				ath12k_warn(ar->ab, "failed to enable bss color change on vdev %i: %d\n",
5004 					    arvif->vdev_id,  ret);
5005 			ret = ath12k_wmi_obss_color_cfg_cmd(ar,
5006 							    arvif->vdev_id,
5007 							    0,
5008 							    ATH12K_BSS_COLOR_STA_PERIODS,
5009 							    1);
5010 			if (ret)
5011 				ath12k_warn(ar->ab, "failed to set bss color collision on vdev %i: %d\n",
5012 					    arvif->vdev_id,  ret);
5013 		}
5014 	}
5015 
5016 	ath12k_mac_fils_discovery(arvif, info);
5017 
5018 	if (changed & BSS_CHANGED_PS &&
5019 	    ar->ab->hw_params->supports_sta_ps) {
5020 		ahvif->ps = vif_cfg->ps;
5021 		ath12k_mac_vif_setup_ps(arvif);
5022 	}
5023 }
5024 
5025 static struct ath12k_vif_cache *ath12k_ahvif_get_link_cache(struct ath12k_vif *ahvif,
5026 							    u8 link_id)
5027 {
5028 	if (!ahvif->cache[link_id]) {
5029 		ahvif->cache[link_id] = kzalloc_obj(*ahvif->cache[0]);
5030 		if (ahvif->cache[link_id])
5031 			INIT_LIST_HEAD(&ahvif->cache[link_id]->key_conf.list);
5032 	}
5033 
5034 	return ahvif->cache[link_id];
5035 }
5036 
5037 static void ath12k_ahvif_put_link_key_cache(struct ath12k_vif_cache *cache)
5038 {
5039 	struct ath12k_key_conf *key_conf, *tmp;
5040 
5041 	if (!cache || list_empty(&cache->key_conf.list))
5042 		return;
5043 	list_for_each_entry_safe(key_conf, tmp, &cache->key_conf.list, list) {
5044 		list_del(&key_conf->list);
5045 		kfree(key_conf);
5046 	}
5047 }
5048 
5049 static void ath12k_ahvif_put_link_cache(struct ath12k_vif *ahvif, u8 link_id)
5050 {
5051 	if (link_id >= IEEE80211_MLD_MAX_NUM_LINKS)
5052 		return;
5053 
5054 	ath12k_ahvif_put_link_key_cache(ahvif->cache[link_id]);
5055 	kfree(ahvif->cache[link_id]);
5056 	ahvif->cache[link_id] = NULL;
5057 }
5058 
5059 void ath12k_mac_op_link_info_changed(struct ieee80211_hw *hw,
5060 				     struct ieee80211_vif *vif,
5061 				     struct ieee80211_bss_conf *info,
5062 				     u64 changed)
5063 {
5064 	struct ath12k *ar;
5065 	struct ath12k_vif *ahvif = ath12k_vif_to_ahvif(vif);
5066 	struct ath12k_vif_cache *cache;
5067 	struct ath12k_link_vif *arvif;
5068 	u8 link_id = info->link_id;
5069 
5070 	lockdep_assert_wiphy(hw->wiphy);
5071 
5072 	arvif = wiphy_dereference(hw->wiphy, ahvif->link[link_id]);
5073 
5074 	/* if the vdev is not created on a certain radio,
5075 	 * cache the info to be updated later on vdev creation
5076 	 */
5077 
5078 	if (!arvif || !arvif->is_created) {
5079 		cache = ath12k_ahvif_get_link_cache(ahvif, link_id);
5080 		if (!cache)
5081 			return;
5082 
5083 		cache->bss_conf_changed |= changed;
5084 
5085 		return;
5086 	}
5087 
5088 	ar = arvif->ar;
5089 
5090 	ath12k_mac_bss_info_changed(ar, arvif, info, changed);
5091 }
5092 EXPORT_SYMBOL(ath12k_mac_op_link_info_changed);
5093 
5094 static struct ath12k*
5095 ath12k_mac_select_scan_device(struct ieee80211_hw *hw,
5096 			      struct ieee80211_vif *vif,
5097 			      u32 center_freq)
5098 {
5099 	struct ath12k_hw *ah = hw->priv;
5100 	enum nl80211_band band;
5101 	struct ath12k *ar;
5102 	int i;
5103 
5104 	if (ah->num_radio == 1)
5105 		return ah->radio;
5106 
5107 	/* Currently mac80211 supports splitting scan requests into
5108 	 * multiple scan requests per band.
5109 	 * Loop through first channel and determine the scan radio
5110 	 * TODO: There could be 5 GHz low/high channels in that case
5111 	 * split the hw request and perform multiple scans
5112 	 */
5113 
5114 	if (center_freq < ATH12K_MIN_5GHZ_FREQ)
5115 		band = NL80211_BAND_2GHZ;
5116 	else if (center_freq < ATH12K_MIN_6GHZ_FREQ)
5117 		band = NL80211_BAND_5GHZ;
5118 	else
5119 		band = NL80211_BAND_6GHZ;
5120 
5121 	for_each_ar(ah, ar, i) {
5122 		if (ar->mac.sbands[band].channels &&
5123 		    center_freq >= KHZ_TO_MHZ(ar->freq_range.start_freq) &&
5124 		    center_freq <= KHZ_TO_MHZ(ar->freq_range.end_freq))
5125 			return ar;
5126 	}
5127 
5128 	return NULL;
5129 }
5130 
5131 void __ath12k_mac_scan_finish(struct ath12k *ar)
5132 {
5133 	struct ieee80211_hw *hw = ath12k_ar_to_hw(ar);
5134 
5135 	lockdep_assert_held(&ar->data_lock);
5136 
5137 	switch (ar->scan.state) {
5138 	case ATH12K_SCAN_IDLE:
5139 		break;
5140 	case ATH12K_SCAN_RUNNING:
5141 	case ATH12K_SCAN_ABORTING:
5142 		if (ar->scan.is_roc && ar->scan.roc_notify)
5143 			ieee80211_remain_on_channel_expired(hw);
5144 		fallthrough;
5145 	case ATH12K_SCAN_STARTING:
5146 		cancel_delayed_work(&ar->scan.timeout);
5147 		complete_all(&ar->scan.completed);
5148 		wiphy_work_queue(ar->ah->hw->wiphy, &ar->scan.vdev_clean_wk);
5149 		break;
5150 	}
5151 }
5152 
5153 void ath12k_mac_scan_finish(struct ath12k *ar)
5154 {
5155 	spin_lock_bh(&ar->data_lock);
5156 	__ath12k_mac_scan_finish(ar);
5157 	spin_unlock_bh(&ar->data_lock);
5158 }
5159 
5160 static int ath12k_scan_stop(struct ath12k *ar)
5161 {
5162 	struct ath12k_wmi_scan_cancel_arg arg = {
5163 		.req_type = WLAN_SCAN_CANCEL_SINGLE,
5164 		.scan_id = ATH12K_SCAN_ID,
5165 	};
5166 	int ret;
5167 
5168 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
5169 
5170 	/* TODO: Fill other STOP Params */
5171 	arg.pdev_id = ar->pdev->pdev_id;
5172 
5173 	ret = ath12k_wmi_send_scan_stop_cmd(ar, &arg);
5174 	if (ret) {
5175 		ath12k_warn(ar->ab, "failed to stop wmi scan: %d\n", ret);
5176 		goto out;
5177 	}
5178 
5179 	ret = wait_for_completion_timeout(&ar->scan.completed, 3 * HZ);
5180 	if (ret == 0) {
5181 		ath12k_warn(ar->ab,
5182 			    "failed to receive scan abort comple: timed out\n");
5183 		ret = -ETIMEDOUT;
5184 	} else if (ret > 0) {
5185 		ret = 0;
5186 	}
5187 
5188 out:
5189 	/* Scan state should be updated in scan completion worker but in
5190 	 * case firmware fails to deliver the event (for whatever reason)
5191 	 * it is desired to clean up scan state anyway. Firmware may have
5192 	 * just dropped the scan completion event delivery due to transport
5193 	 * pipe being overflown with data and/or it can recover on its own
5194 	 * before next scan request is submitted.
5195 	 */
5196 	spin_lock_bh(&ar->data_lock);
5197 	if (ret)
5198 		__ath12k_mac_scan_finish(ar);
5199 	spin_unlock_bh(&ar->data_lock);
5200 
5201 	return ret;
5202 }
5203 
5204 static void ath12k_scan_abort(struct ath12k *ar)
5205 {
5206 	int ret;
5207 
5208 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
5209 
5210 	spin_lock_bh(&ar->data_lock);
5211 
5212 	switch (ar->scan.state) {
5213 	case ATH12K_SCAN_IDLE:
5214 		/* This can happen if timeout worker kicked in and called
5215 		 * abortion while scan completion was being processed.
5216 		 */
5217 		break;
5218 	case ATH12K_SCAN_STARTING:
5219 	case ATH12K_SCAN_ABORTING:
5220 		ath12k_warn(ar->ab, "refusing scan abortion due to invalid scan state: %d\n",
5221 			    ar->scan.state);
5222 		break;
5223 	case ATH12K_SCAN_RUNNING:
5224 		ar->scan.state = ATH12K_SCAN_ABORTING;
5225 		spin_unlock_bh(&ar->data_lock);
5226 
5227 		ret = ath12k_scan_stop(ar);
5228 		if (ret)
5229 			ath12k_warn(ar->ab, "failed to abort scan: %d\n", ret);
5230 
5231 		spin_lock_bh(&ar->data_lock);
5232 		break;
5233 	}
5234 
5235 	spin_unlock_bh(&ar->data_lock);
5236 }
5237 
5238 static void ath12k_scan_timeout_work(struct work_struct *work)
5239 {
5240 	struct ath12k *ar = container_of(work, struct ath12k,
5241 					 scan.timeout.work);
5242 
5243 	wiphy_lock(ath12k_ar_to_hw(ar)->wiphy);
5244 	ath12k_scan_abort(ar);
5245 	wiphy_unlock(ath12k_ar_to_hw(ar)->wiphy);
5246 }
5247 
5248 static void ath12k_mac_scan_send_complete(struct ath12k *ar,
5249 					  struct cfg80211_scan_info *info)
5250 {
5251 	struct ath12k_hw *ah = ar->ah;
5252 	struct ath12k *partner_ar;
5253 	int i;
5254 
5255 	lockdep_assert_wiphy(ah->hw->wiphy);
5256 
5257 	for_each_ar(ah, partner_ar, i)
5258 		if (partner_ar != ar &&
5259 		    partner_ar->scan.state == ATH12K_SCAN_RUNNING)
5260 			return;
5261 
5262 	ieee80211_scan_completed(ah->hw, info);
5263 }
5264 
5265 static void ath12k_scan_vdev_clean_work(struct wiphy *wiphy, struct wiphy_work *work)
5266 {
5267 	struct ath12k *ar = container_of(work, struct ath12k,
5268 					 scan.vdev_clean_wk);
5269 	struct ath12k_hw *ah = ar->ah;
5270 	struct ath12k_link_vif *arvif;
5271 
5272 	lockdep_assert_wiphy(wiphy);
5273 
5274 	arvif = ar->scan.arvif;
5275 
5276 	/* The scan vdev has already been deleted. This can occur when a
5277 	 * new scan request is made on the same vif with a different
5278 	 * frequency, causing the scan arvif to move from one radio to
5279 	 * another. Or, scan was abrupted and via remove interface, the
5280 	 * arvif is already deleted. Alternatively, if the scan vdev is not
5281 	 * being used as an actual vdev, then do not delete it.
5282 	 */
5283 	if (!arvif || arvif->is_started)
5284 		goto work_complete;
5285 
5286 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac clean scan vdev (link id %u)",
5287 		   arvif->link_id);
5288 
5289 	ath12k_mac_remove_link_interface(ah->hw, arvif);
5290 	ath12k_mac_unassign_link_vif(arvif);
5291 
5292 work_complete:
5293 	spin_lock_bh(&ar->data_lock);
5294 	ar->scan.arvif = NULL;
5295 	if (!ar->scan.is_roc) {
5296 		struct cfg80211_scan_info info = {
5297 			.aborted = ((ar->scan.state ==
5298 				    ATH12K_SCAN_ABORTING) ||
5299 				    (ar->scan.state ==
5300 				    ATH12K_SCAN_STARTING)),
5301 		};
5302 
5303 		ath12k_mac_scan_send_complete(ar, &info);
5304 	}
5305 
5306 	ar->scan.state = ATH12K_SCAN_IDLE;
5307 	ar->scan_channel = NULL;
5308 	ar->scan.roc_freq = 0;
5309 	spin_unlock_bh(&ar->data_lock);
5310 }
5311 
5312 static int ath12k_start_scan(struct ath12k *ar,
5313 			     struct ath12k_wmi_scan_req_arg *arg)
5314 {
5315 	int ret;
5316 
5317 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
5318 
5319 	ret = ath12k_wmi_send_scan_start_cmd(ar, arg);
5320 	if (ret)
5321 		return ret;
5322 
5323 	ret = wait_for_completion_timeout(&ar->scan.started, 1 * HZ);
5324 	if (ret == 0) {
5325 		ret = ath12k_scan_stop(ar);
5326 		if (ret)
5327 			ath12k_warn(ar->ab, "failed to stop scan: %d\n", ret);
5328 
5329 		return -ETIMEDOUT;
5330 	}
5331 
5332 	/* If we failed to start the scan, return error code at
5333 	 * this point.  This is probably due to some issue in the
5334 	 * firmware, but no need to wedge the driver due to that...
5335 	 */
5336 	spin_lock_bh(&ar->data_lock);
5337 	if (ar->scan.state == ATH12K_SCAN_IDLE) {
5338 		spin_unlock_bh(&ar->data_lock);
5339 		return -EINVAL;
5340 	}
5341 	spin_unlock_bh(&ar->data_lock);
5342 
5343 	return 0;
5344 }
5345 
5346 int ath12k_mac_get_fw_stats(struct ath12k *ar,
5347 			    struct ath12k_fw_stats_req_params *param)
5348 {
5349 	struct ath12k_base *ab = ar->ab;
5350 	struct ath12k_hw *ah = ath12k_ar_to_ah(ar);
5351 	unsigned long time_left;
5352 	int ret;
5353 
5354 	guard(mutex)(&ah->hw_mutex);
5355 
5356 	if (ah->state != ATH12K_HW_STATE_ON)
5357 		return -ENETDOWN;
5358 
5359 	reinit_completion(&ar->fw_stats_complete);
5360 	reinit_completion(&ar->fw_stats_done);
5361 
5362 	ret = ath12k_wmi_send_stats_request_cmd(ar, param->stats_id,
5363 						param->vdev_id, param->pdev_id);
5364 	if (ret) {
5365 		ath12k_warn(ab, "failed to request fw stats: %d\n", ret);
5366 		return ret;
5367 	}
5368 
5369 	ath12k_dbg(ab, ATH12K_DBG_WMI,
5370 		   "get fw stat pdev id %d vdev id %d stats id 0x%x\n",
5371 		   param->pdev_id, param->vdev_id, param->stats_id);
5372 
5373 	time_left = wait_for_completion_timeout(&ar->fw_stats_complete, 1 * HZ);
5374 	if (!time_left) {
5375 		ath12k_warn(ab, "time out while waiting for get fw stats\n");
5376 		return -ETIMEDOUT;
5377 	}
5378 
5379 	/* Firmware sends WMI_UPDATE_STATS_EVENTID back-to-back
5380 	 * when stats data buffer limit is reached. fw_stats_complete
5381 	 * is completed once host receives first event from firmware, but
5382 	 * still there could be more events following. Below is to wait
5383 	 * until firmware completes sending all the events.
5384 	 */
5385 	time_left = wait_for_completion_timeout(&ar->fw_stats_done, 3 * HZ);
5386 	if (!time_left) {
5387 		ath12k_warn(ab, "time out while waiting for fw stats done\n");
5388 		return -ETIMEDOUT;
5389 	}
5390 
5391 	return 0;
5392 }
5393 
5394 int ath12k_mac_op_get_txpower(struct ieee80211_hw *hw,
5395 			      struct ieee80211_vif *vif,
5396 			      unsigned int link_id,
5397 			      int *dbm)
5398 {
5399 	struct ath12k_vif *ahvif = ath12k_vif_to_ahvif(vif);
5400 	struct ath12k_fw_stats_req_params params = {};
5401 	struct ath12k_fw_stats_pdev *pdev;
5402 	struct ath12k_hw *ah = hw->priv;
5403 	struct ath12k_link_vif *arvif;
5404 	struct ath12k_base *ab;
5405 	struct ath12k *ar;
5406 	int ret;
5407 
5408 	/* Final Tx power is minimum of Target Power, CTL power, Regulatory
5409 	 * Power, PSD EIRP Power. We just know the Regulatory power from the
5410 	 * regulatory rules obtained. FW knows all these power and sets the min
5411 	 * of these. Hence, we request the FW pdev stats in which FW reports
5412 	 * the minimum of all vdev's channel Tx power.
5413 	 */
5414 	lockdep_assert_wiphy(hw->wiphy);
5415 
5416 	arvif = wiphy_dereference(ah->hw->wiphy, ahvif->link[link_id]);
5417 	if (!arvif || !arvif->ar)
5418 		return -EINVAL;
5419 
5420 	ar = arvif->ar;
5421 	ab = ar->ab;
5422 	if (ah->state != ATH12K_HW_STATE_ON)
5423 		goto err_fallback;
5424 
5425 	if (test_bit(ATH12K_FLAG_CAC_RUNNING, &ar->dev_flags))
5426 		return -EAGAIN;
5427 
5428 	/* Limit the requests to Firmware for fetching the tx power */
5429 	if (ar->chan_tx_pwr != ATH12K_PDEV_TX_POWER_INVALID &&
5430 	    time_before(jiffies,
5431 			msecs_to_jiffies(ATH12K_PDEV_TX_POWER_REFRESH_TIME_MSECS) +
5432 					 ar->last_tx_power_update))
5433 		goto send_tx_power;
5434 
5435 	params.pdev_id = ath12k_mac_get_target_pdev_id(ar);
5436 	params.vdev_id = arvif->vdev_id;
5437 	params.stats_id = WMI_REQUEST_PDEV_STAT;
5438 	ret = ath12k_mac_get_fw_stats(ar, &params);
5439 	if (ret) {
5440 		ath12k_warn(ab, "failed to request fw pdev stats: %d\n", ret);
5441 		goto err_fallback;
5442 	}
5443 
5444 	spin_lock_bh(&ar->data_lock);
5445 	pdev = list_first_entry_or_null(&ar->fw_stats.pdevs,
5446 					struct ath12k_fw_stats_pdev, list);
5447 	if (!pdev) {
5448 		spin_unlock_bh(&ar->data_lock);
5449 		goto err_fallback;
5450 	}
5451 
5452 	/* tx power reported by firmware is in units of 0.5 dBm */
5453 	ar->chan_tx_pwr = pdev->chan_tx_power / 2;
5454 	spin_unlock_bh(&ar->data_lock);
5455 	ar->last_tx_power_update = jiffies;
5456 	ath12k_fw_stats_reset(ar);
5457 
5458 send_tx_power:
5459 	*dbm = ar->chan_tx_pwr;
5460 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "txpower fetched from firmware %d dBm\n",
5461 		   *dbm);
5462 	return 0;
5463 
5464 err_fallback:
5465 	/* We didn't get txpower from FW. Hence, relying on vif->bss_conf.txpower */
5466 	*dbm = vif->bss_conf.txpower;
5467 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "txpower from firmware NaN, reported %d dBm\n",
5468 		   *dbm);
5469 	return 0;
5470 }
5471 EXPORT_SYMBOL(ath12k_mac_op_get_txpower);
5472 
5473 static u8
5474 ath12k_mac_find_link_id_by_ar(struct ath12k_vif *ahvif, struct ath12k *ar)
5475 {
5476 	struct ath12k_link_vif *arvif;
5477 	struct ath12k_hw *ah = ahvif->ah;
5478 	unsigned long links = ahvif->links_map;
5479 	unsigned long scan_links_map;
5480 	u8 link_id;
5481 
5482 	lockdep_assert_wiphy(ah->hw->wiphy);
5483 
5484 	for_each_set_bit(link_id, &links, ATH12K_NUM_MAX_LINKS) {
5485 		arvif = wiphy_dereference(ah->hw->wiphy, ahvif->link[link_id]);
5486 
5487 		if (!arvif || !arvif->is_created)
5488 			continue;
5489 
5490 		if (ar == arvif->ar)
5491 			return link_id;
5492 	}
5493 
5494 	/* input ar is not assigned to any of the links of ML VIF, use next
5495 	 * available scan link for scan vdev creation. There are cases where
5496 	 * single scan req needs to be split in driver and initiate separate
5497 	 * scan requests to firmware based on device.
5498 	 */
5499 
5500 	 /* Unset all non-scan links (0-14) of scan_links_map so that ffs() will
5501 	  * choose an available link among scan links (i.e link id >= 15)
5502 	  */
5503 	scan_links_map = ~ahvif->links_map & ATH12K_SCAN_LINKS_MASK;
5504 	if (scan_links_map)
5505 		return __ffs(scan_links_map);
5506 
5507 	return ATH12K_FIRST_SCAN_LINK;
5508 }
5509 
5510 static int ath12k_mac_initiate_hw_scan(struct ieee80211_hw *hw,
5511 				       struct ieee80211_vif *vif,
5512 				       struct ieee80211_scan_request *hw_req,
5513 				       int n_channels,
5514 				       struct ieee80211_channel **chan_list,
5515 				       struct ath12k *ar)
5516 {
5517 	struct ath12k_hw *ah = ath12k_hw_to_ah(hw);
5518 	struct ath12k_vif *ahvif = ath12k_vif_to_ahvif(vif);
5519 	struct ath12k_link_vif *arvif;
5520 	struct cfg80211_scan_request *req = &hw_req->req;
5521 	struct ath12k_wmi_scan_req_arg *arg = NULL;
5522 	u8 link_id;
5523 	int ret;
5524 	int i;
5525 	bool create = true;
5526 
5527 	lockdep_assert_wiphy(hw->wiphy);
5528 
5529 	arvif = &ahvif->deflink;
5530 
5531 	/* check if any of the links of ML VIF is already started on
5532 	 * radio(ar) corresponding to given scan frequency and use it,
5533 	 * if not use scan link (link id >= 15) for scan purpose.
5534 	 */
5535 	link_id = ath12k_mac_find_link_id_by_ar(ahvif, ar);
5536 	/* All scan links are occupied. ideally this shouldn't happen as
5537 	 * mac80211 won't schedule scan for same band until ongoing scan is
5538 	 * completed, don't try to exceed max links just in case if it happens.
5539 	 */
5540 	if (link_id >= ATH12K_NUM_MAX_LINKS)
5541 		return -EBUSY;
5542 
5543 	arvif = ath12k_mac_assign_link_vif(ah, vif, link_id);
5544 
5545 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac link ID %d selected for scan",
5546 		   arvif->link_id);
5547 
5548 	/* If the vif is already assigned to a specific vdev of an ar,
5549 	 * check whether its already started, vdev which is started
5550 	 * are not allowed to switch to a new radio.
5551 	 * If the vdev is not started, but was earlier created on a
5552 	 * different ar, delete that vdev and create a new one. We don't
5553 	 * delete at the scan stop as an optimization to avoid redundant
5554 	 * delete-create vdev's for the same ar, in case the request is
5555 	 * always on the same band for the vif
5556 	 */
5557 	if (arvif->is_created) {
5558 		if (WARN_ON(!arvif->ar))
5559 			return -EINVAL;
5560 
5561 		if (ar != arvif->ar && arvif->is_started)
5562 			return -EINVAL;
5563 
5564 		if (ar != arvif->ar) {
5565 			ath12k_mac_remove_link_interface(hw, arvif);
5566 			ath12k_mac_unassign_link_vif(arvif);
5567 		} else {
5568 			create = false;
5569 		}
5570 	}
5571 
5572 	if (create) {
5573 		/* Previous arvif would've been cleared in radio switch block
5574 		 * above, assign arvif again for create.
5575 		 */
5576 		arvif = ath12k_mac_assign_link_vif(ah, vif, link_id);
5577 
5578 		ret = ath12k_mac_vdev_create(ar, arvif);
5579 		if (ret) {
5580 			ath12k_warn(ar->ab, "unable to create scan vdev %d\n", ret);
5581 			ath12k_mac_unassign_link_vif(arvif);
5582 			return ret;
5583 		}
5584 	}
5585 
5586 	spin_lock_bh(&ar->data_lock);
5587 	switch (ar->scan.state) {
5588 	case ATH12K_SCAN_IDLE:
5589 		reinit_completion(&ar->scan.started);
5590 		reinit_completion(&ar->scan.completed);
5591 		ar->scan.state = ATH12K_SCAN_STARTING;
5592 		ar->scan.is_roc = false;
5593 		ar->scan.arvif = arvif;
5594 		ret = 0;
5595 		break;
5596 	case ATH12K_SCAN_STARTING:
5597 	case ATH12K_SCAN_RUNNING:
5598 	case ATH12K_SCAN_ABORTING:
5599 		ret = -EBUSY;
5600 		break;
5601 	}
5602 	spin_unlock_bh(&ar->data_lock);
5603 
5604 	if (ret)
5605 		goto exit;
5606 
5607 	arg = kzalloc_obj(*arg);
5608 	if (!arg) {
5609 		ret = -ENOMEM;
5610 		goto exit;
5611 	}
5612 
5613 	ath12k_wmi_start_scan_init(ar, arg);
5614 	arg->vdev_id = arvif->vdev_id;
5615 	arg->scan_id = ATH12K_SCAN_ID;
5616 
5617 	if (req->ie_len) {
5618 		arg->extraie.ptr = kmemdup(req->ie, req->ie_len, GFP_KERNEL);
5619 		if (!arg->extraie.ptr) {
5620 			ret = -ENOMEM;
5621 			goto exit;
5622 		}
5623 		arg->extraie.len = req->ie_len;
5624 	}
5625 
5626 	if (req->n_ssids) {
5627 		arg->num_ssids = req->n_ssids;
5628 		for (i = 0; i < arg->num_ssids; i++)
5629 			arg->ssid[i] = req->ssids[i];
5630 	} else {
5631 		arg->scan_f_passive = 1;
5632 	}
5633 
5634 	if (n_channels) {
5635 		arg->num_chan = n_channels;
5636 		arg->chan_list = kcalloc(arg->num_chan, sizeof(*arg->chan_list),
5637 					 GFP_KERNEL);
5638 		if (!arg->chan_list) {
5639 			ret = -ENOMEM;
5640 			goto exit;
5641 		}
5642 
5643 		for (i = 0; i < arg->num_chan; i++)
5644 			arg->chan_list[i] = chan_list[i]->center_freq;
5645 	}
5646 
5647 	ret = ath12k_start_scan(ar, arg);
5648 	if (ret) {
5649 		if (ret == -EBUSY)
5650 			ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
5651 				   "scan engine is busy 11d state %d\n", ar->state_11d);
5652 		else
5653 			ath12k_warn(ar->ab, "failed to start hw scan: %d\n", ret);
5654 
5655 		spin_lock_bh(&ar->data_lock);
5656 		ar->scan.state = ATH12K_SCAN_IDLE;
5657 		spin_unlock_bh(&ar->data_lock);
5658 		goto exit;
5659 	}
5660 
5661 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac scan started");
5662 
5663 	/* Add a margin to account for event/command processing */
5664 	ieee80211_queue_delayed_work(ath12k_ar_to_hw(ar), &ar->scan.timeout,
5665 				     msecs_to_jiffies(arg->max_scan_time +
5666 						      ATH12K_MAC_SCAN_TIMEOUT_MSECS));
5667 
5668 exit:
5669 	if (arg) {
5670 		kfree(arg->chan_list);
5671 		kfree(arg->extraie.ptr);
5672 		kfree(arg);
5673 	}
5674 
5675 	if (ar->state_11d == ATH12K_11D_PREPARING &&
5676 	    ahvif->vdev_type == WMI_VDEV_TYPE_STA &&
5677 	    ahvif->vdev_subtype == WMI_VDEV_SUBTYPE_NONE)
5678 		ath12k_mac_11d_scan_start(ar, arvif->vdev_id);
5679 
5680 	return ret;
5681 }
5682 
5683 int ath12k_mac_op_hw_scan(struct ieee80211_hw *hw,
5684 			  struct ieee80211_vif *vif,
5685 			  struct ieee80211_scan_request *hw_req)
5686 {
5687 	struct ath12k_vif *ahvif = ath12k_vif_to_ahvif(vif);
5688 	struct ieee80211_channel **chan_list, *chan;
5689 	struct ath12k_hw *ah = ath12k_hw_to_ah(hw);
5690 	unsigned long links_map, link_id;
5691 	struct ath12k_link_vif *arvif;
5692 	struct ath12k *ar, *scan_ar;
5693 	int i, j, ret = 0;
5694 
5695 	lockdep_assert_wiphy(hw->wiphy);
5696 
5697 	chan_list = kzalloc_objs(*chan_list, hw_req->req.n_channels);
5698 	if (!chan_list)
5699 		return -ENOMEM;
5700 
5701 	/* There could be channels that belong to multiple underlying radio
5702 	 * in same scan request as mac80211 sees it as single band. In that
5703 	 * case split the hw_req based on frequency range and schedule scans to
5704 	 * corresponding radio.
5705 	 */
5706 	for_each_ar(ah, ar, i) {
5707 		int n_chans = 0;
5708 
5709 		for (j = 0; j < hw_req->req.n_channels; j++) {
5710 			chan = hw_req->req.channels[j];
5711 			scan_ar = ath12k_mac_select_scan_device(hw, vif,
5712 								chan->center_freq);
5713 			if (!scan_ar) {
5714 				ath12k_hw_warn(ah, "unable to select scan device for freq %d\n",
5715 					       chan->center_freq);
5716 				ret = -EINVAL;
5717 				goto abort;
5718 			}
5719 			if (ar != scan_ar)
5720 				continue;
5721 
5722 			chan_list[n_chans++] = chan;
5723 		}
5724 		if (n_chans) {
5725 			ret = ath12k_mac_initiate_hw_scan(hw, vif, hw_req, n_chans,
5726 							  chan_list, ar);
5727 			if (ret)
5728 				goto abort;
5729 		}
5730 	}
5731 abort:
5732 	/* If any of the parallel scans initiated fails, abort all and
5733 	 * remove the scan interfaces created. Return complete scan
5734 	 * failure as mac80211 assumes this as single scan request.
5735 	 */
5736 	if (ret) {
5737 		ath12k_hw_warn(ah, "Scan failed %d , cleanup all scan vdevs\n", ret);
5738 		links_map = ahvif->links_map;
5739 		for_each_set_bit(link_id, &links_map, ATH12K_NUM_MAX_LINKS) {
5740 			arvif = wiphy_dereference(hw->wiphy, ahvif->link[link_id]);
5741 			if (!arvif)
5742 				continue;
5743 
5744 			ar = arvif->ar;
5745 			if (ar->scan.arvif == arvif) {
5746 				wiphy_work_cancel(hw->wiphy, &ar->scan.vdev_clean_wk);
5747 				spin_lock_bh(&ar->data_lock);
5748 				ar->scan.arvif = NULL;
5749 				ar->scan.state = ATH12K_SCAN_IDLE;
5750 				ar->scan_channel = NULL;
5751 				ar->scan.roc_freq = 0;
5752 				spin_unlock_bh(&ar->data_lock);
5753 			}
5754 			if (link_id >= ATH12K_FIRST_SCAN_LINK) {
5755 				ath12k_mac_remove_link_interface(hw, arvif);
5756 				ath12k_mac_unassign_link_vif(arvif);
5757 			}
5758 		}
5759 	}
5760 	kfree(chan_list);
5761 	return ret;
5762 }
5763 EXPORT_SYMBOL(ath12k_mac_op_hw_scan);
5764 
5765 void ath12k_mac_op_cancel_hw_scan(struct ieee80211_hw *hw,
5766 				  struct ieee80211_vif *vif)
5767 {
5768 	struct ath12k_vif *ahvif = ath12k_vif_to_ahvif(vif);
5769 	unsigned long link_id, links_map = ahvif->links_map;
5770 	struct ath12k_link_vif *arvif;
5771 	struct ath12k *ar;
5772 
5773 	lockdep_assert_wiphy(hw->wiphy);
5774 
5775 	for_each_set_bit(link_id, &links_map, ATH12K_NUM_MAX_LINKS) {
5776 		arvif = wiphy_dereference(hw->wiphy, ahvif->link[link_id]);
5777 		if (!arvif || !arvif->is_created ||
5778 		    arvif->ar->scan.arvif != arvif)
5779 			continue;
5780 
5781 		ar = arvif->ar;
5782 
5783 		ath12k_scan_abort(ar);
5784 
5785 		cancel_delayed_work_sync(&ar->scan.timeout);
5786 	}
5787 }
5788 EXPORT_SYMBOL(ath12k_mac_op_cancel_hw_scan);
5789 
5790 static int ath12k_install_key(struct ath12k_link_vif *arvif,
5791 			      struct ieee80211_key_conf *key,
5792 			      enum set_key_cmd cmd,
5793 			      const u8 *macaddr, u32 flags)
5794 {
5795 	int ret;
5796 	struct ath12k *ar = arvif->ar;
5797 	struct wmi_vdev_install_key_arg arg = {
5798 		.vdev_id = arvif->vdev_id,
5799 		.key_idx = key->keyidx,
5800 		.key_len = key->keylen,
5801 		.key_data = key->key,
5802 		.key_flags = flags,
5803 		.ieee80211_key_cipher = key->cipher,
5804 		.macaddr = macaddr,
5805 	};
5806 	struct ath12k_vif *ahvif = arvif->ahvif;
5807 
5808 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
5809 
5810 	if (test_bit(ATH12K_FLAG_HW_CRYPTO_DISABLED, &ar->ab->dev_flags))
5811 		return 0;
5812 
5813 	if (cmd == DISABLE_KEY) {
5814 		/* TODO: Check if FW expects  value other than NONE for del */
5815 		/* arg.key_cipher = WMI_CIPHER_NONE; */
5816 		arg.key_len = 0;
5817 		arg.key_data = NULL;
5818 		goto check_order;
5819 	}
5820 
5821 	switch (key->cipher) {
5822 	case WLAN_CIPHER_SUITE_CCMP:
5823 	case WLAN_CIPHER_SUITE_CCMP_256:
5824 		arg.key_cipher = WMI_CIPHER_AES_CCM;
5825 		key->flags |= IEEE80211_KEY_FLAG_GENERATE_IV_MGMT;
5826 		break;
5827 	case WLAN_CIPHER_SUITE_TKIP:
5828 		arg.key_cipher = WMI_CIPHER_TKIP;
5829 		arg.key_txmic_len = 8;
5830 		arg.key_rxmic_len = 8;
5831 		break;
5832 	case WLAN_CIPHER_SUITE_GCMP:
5833 	case WLAN_CIPHER_SUITE_GCMP_256:
5834 		arg.key_cipher = WMI_CIPHER_AES_GCM;
5835 		key->flags |= IEEE80211_KEY_FLAG_GENERATE_IV_MGMT;
5836 		break;
5837 	case WLAN_CIPHER_SUITE_AES_CMAC:
5838 		arg.key_cipher = WMI_CIPHER_AES_CMAC;
5839 		break;
5840 	case WLAN_CIPHER_SUITE_BIP_GMAC_128:
5841 	case WLAN_CIPHER_SUITE_BIP_GMAC_256:
5842 		arg.key_cipher = WMI_CIPHER_AES_GMAC;
5843 		break;
5844 	case WLAN_CIPHER_SUITE_BIP_CMAC_256:
5845 		arg.key_cipher = WMI_CIPHER_AES_CMAC;
5846 		break;
5847 	default:
5848 		ath12k_warn(ar->ab, "cipher %d is not supported\n", key->cipher);
5849 		return -EOPNOTSUPP;
5850 	}
5851 
5852 	if (test_bit(ATH12K_FLAG_RAW_MODE, &ar->ab->dev_flags))
5853 		key->flags |= IEEE80211_KEY_FLAG_GENERATE_IV |
5854 			      IEEE80211_KEY_FLAG_RESERVE_TAILROOM;
5855 
5856 check_order:
5857 	if (ahvif->vdev_type == WMI_VDEV_TYPE_STA &&
5858 	    arg.key_flags == WMI_KEY_GROUP) {
5859 		if (cmd == SET_KEY) {
5860 			if (arvif->pairwise_key_done) {
5861 				ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
5862 					   "vdev %u pairwise key done, go install group key\n",
5863 					   arg.vdev_id);
5864 				goto install;
5865 			} else {
5866 				/* WCN7850 firmware requires pairwise key to be installed
5867 				 * before group key. In case group key comes first, cache
5868 				 * it and return. Will revisit it once pairwise key gets
5869 				 * installed.
5870 				 */
5871 				arvif->group_key = arg;
5872 				arvif->group_key_valid = true;
5873 				ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
5874 					   "vdev %u group key before pairwise key, cache and skip\n",
5875 					   arg.vdev_id);
5876 
5877 				ret = 0;
5878 				goto out;
5879 			}
5880 		} else {
5881 			arvif->group_key_valid = false;
5882 		}
5883 	}
5884 
5885 install:
5886 	reinit_completion(&ar->install_key_done);
5887 
5888 	ret = ath12k_wmi_vdev_install_key(arvif->ar, &arg);
5889 	if (ret)
5890 		return ret;
5891 
5892 	if (!wait_for_completion_timeout(&ar->install_key_done, 1 * HZ))
5893 		return -ETIMEDOUT;
5894 
5895 	if (ether_addr_equal(arg.macaddr, arvif->bssid))
5896 		ahvif->dp_vif.key_cipher = arg.ieee80211_key_cipher;
5897 
5898 	if (ar->install_key_status) {
5899 		ret = -EINVAL;
5900 		goto out;
5901 	}
5902 
5903 	if (ahvif->vdev_type == WMI_VDEV_TYPE_STA &&
5904 	    arg.key_flags == WMI_KEY_PAIRWISE) {
5905 		if (cmd == SET_KEY) {
5906 			arvif->pairwise_key_done = true;
5907 			if (arvif->group_key_valid) {
5908 				/* Install cached GTK */
5909 				arvif->group_key_valid = false;
5910 				arg = arvif->group_key;
5911 				ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
5912 					   "vdev %u pairwise key done, group key ready, go install\n",
5913 					   arg.vdev_id);
5914 				goto install;
5915 			}
5916 		} else {
5917 			arvif->pairwise_key_done = false;
5918 		}
5919 	}
5920 
5921 out:
5922 	if (ret) {
5923 		/* In case of failure userspace may not do DISABLE_KEY
5924 		 * but triggers re-connection directly, so manually reset
5925 		 * status here.
5926 		 */
5927 		arvif->group_key_valid = false;
5928 		arvif->pairwise_key_done = false;
5929 	}
5930 
5931 	return ret;
5932 }
5933 
5934 static int ath12k_clear_peer_keys(struct ath12k_link_vif *arvif,
5935 				  const u8 *addr)
5936 {
5937 	struct ath12k *ar = arvif->ar;
5938 	struct ath12k_base *ab = ar->ab;
5939 	struct ath12k_dp_link_peer *peer;
5940 	int first_errno = 0;
5941 	int ret;
5942 	int i, len;
5943 	u32 flags = 0;
5944 	struct ath12k_dp *dp = ath12k_ab_to_dp(ab);
5945 	struct ieee80211_key_conf *keys[WMI_MAX_KEY_INDEX + 1] = {};
5946 
5947 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
5948 
5949 	spin_lock_bh(&dp->dp_lock);
5950 	peer = ath12k_dp_link_peer_find_by_vdev_and_addr(dp, arvif->vdev_id, addr);
5951 	if (!peer || !peer->dp_peer) {
5952 		spin_unlock_bh(&dp->dp_lock);
5953 		return -ENOENT;
5954 	}
5955 
5956 	len = ARRAY_SIZE(peer->dp_peer->keys);
5957 	for (i = 0; i < len; i++) {
5958 		if (!peer->dp_peer->keys[i])
5959 			continue;
5960 
5961 		keys[i] = peer->dp_peer->keys[i];
5962 		peer->dp_peer->keys[i] = NULL;
5963 	}
5964 
5965 	spin_unlock_bh(&dp->dp_lock);
5966 
5967 	for (i = 0; i < len; i++) {
5968 		if (!keys[i])
5969 			continue;
5970 
5971 		/* key flags are not required to delete the key */
5972 		ret = ath12k_install_key(arvif, keys[i],
5973 					 DISABLE_KEY, addr, flags);
5974 		if (ret < 0 && first_errno == 0)
5975 			first_errno = ret;
5976 
5977 		if (ret < 0)
5978 			ath12k_warn(ab, "failed to remove peer key %d: %d\n",
5979 				    i, ret);
5980 	}
5981 
5982 	return first_errno;
5983 }
5984 
5985 static int ath12k_mac_set_key(struct ath12k *ar, enum set_key_cmd cmd,
5986 			      struct ath12k_link_vif *arvif,
5987 			      struct ath12k_link_sta *arsta,
5988 			      struct ieee80211_key_conf *key)
5989 {
5990 	struct ieee80211_sta *sta = NULL;
5991 	struct ath12k_base *ab = ar->ab;
5992 	struct ath12k_dp_link_peer *peer;
5993 	struct ath12k_sta *ahsta;
5994 	const u8 *peer_addr;
5995 	int ret;
5996 	u32 flags = 0;
5997 	struct ath12k_dp *dp = ath12k_ab_to_dp(ab);
5998 
5999 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
6000 
6001 	if (arsta)
6002 		sta = ath12k_ahsta_to_sta(arsta->ahsta);
6003 
6004 	if (test_bit(ATH12K_FLAG_HW_CRYPTO_DISABLED, &ab->dev_flags))
6005 		return 1;
6006 
6007 	if (sta)
6008 		peer_addr = arsta->addr;
6009 	else
6010 		peer_addr = arvif->bssid;
6011 
6012 	key->hw_key_idx = key->keyidx;
6013 
6014 	/* the peer should not disappear in mid-way (unless FW goes awry) since
6015 	 * we already hold wiphy lock. we just make sure its there now.
6016 	 */
6017 	spin_lock_bh(&dp->dp_lock);
6018 	peer = ath12k_dp_link_peer_find_by_vdev_and_addr(dp, arvif->vdev_id,
6019 							 peer_addr);
6020 	if (!peer || !peer->dp_peer) {
6021 		spin_unlock_bh(&dp->dp_lock);
6022 
6023 		if (cmd == SET_KEY) {
6024 			ath12k_warn(ab, "cannot install key for non-existent peer %pM\n",
6025 				    peer_addr);
6026 			return -EOPNOTSUPP;
6027 		}
6028 
6029 		/* if the peer doesn't exist there is no key to disable
6030 		 * anymore
6031 		 */
6032 		return 0;
6033 	}
6034 
6035 	spin_unlock_bh(&dp->dp_lock);
6036 
6037 	if (key->flags & IEEE80211_KEY_FLAG_PAIRWISE)
6038 		flags = WMI_KEY_PAIRWISE;
6039 	else
6040 		flags = WMI_KEY_GROUP;
6041 
6042 	ret = ath12k_install_key(arvif, key, cmd, peer_addr, flags);
6043 	if (ret) {
6044 		ath12k_warn(ab, "ath12k_install_key failed (%d)\n", ret);
6045 		return ret;
6046 	}
6047 
6048 	ret = ath12k_dp_rx_peer_pn_replay_config(arvif, peer_addr, cmd, key);
6049 	if (ret) {
6050 		ath12k_warn(ab, "failed to offload PN replay detection %d\n", ret);
6051 		return ret;
6052 	}
6053 
6054 	spin_lock_bh(&dp->dp_lock);
6055 	peer = ath12k_dp_link_peer_find_by_vdev_and_addr(dp, arvif->vdev_id,
6056 							 peer_addr);
6057 	if (peer && peer->dp_peer && cmd == SET_KEY) {
6058 		peer->dp_peer->keys[key->keyidx] = key;
6059 		if (key->flags & IEEE80211_KEY_FLAG_PAIRWISE) {
6060 			peer->dp_peer->ucast_keyidx = key->keyidx;
6061 			peer->dp_peer->sec_type =
6062 					ath12k_dp_tx_get_encrypt_type(key->cipher);
6063 		} else {
6064 			peer->dp_peer->mcast_keyidx = key->keyidx;
6065 			peer->dp_peer->sec_type_grp =
6066 					ath12k_dp_tx_get_encrypt_type(key->cipher);
6067 		}
6068 	} else if (peer && peer->dp_peer && cmd == DISABLE_KEY) {
6069 		peer->dp_peer->keys[key->keyidx] = NULL;
6070 		if (key->flags & IEEE80211_KEY_FLAG_PAIRWISE)
6071 			peer->dp_peer->ucast_keyidx = 0;
6072 		else
6073 			peer->dp_peer->mcast_keyidx = 0;
6074 	} else if (!peer)
6075 		/* impossible unless FW goes crazy */
6076 		ath12k_warn(ab, "peer %pM disappeared!\n", peer_addr);
6077 
6078 	if (sta) {
6079 		ahsta = ath12k_sta_to_ahsta(sta);
6080 
6081 		switch (key->cipher) {
6082 		case WLAN_CIPHER_SUITE_TKIP:
6083 		case WLAN_CIPHER_SUITE_CCMP:
6084 		case WLAN_CIPHER_SUITE_CCMP_256:
6085 		case WLAN_CIPHER_SUITE_GCMP:
6086 		case WLAN_CIPHER_SUITE_GCMP_256:
6087 			if (cmd == SET_KEY)
6088 				ahsta->pn_type = HAL_PN_TYPE_WPA;
6089 			else
6090 				ahsta->pn_type = HAL_PN_TYPE_NONE;
6091 			break;
6092 		default:
6093 			ahsta->pn_type = HAL_PN_TYPE_NONE;
6094 			break;
6095 		}
6096 	}
6097 
6098 	spin_unlock_bh(&dp->dp_lock);
6099 
6100 	return 0;
6101 }
6102 
6103 static int ath12k_mac_update_key_cache(struct ath12k_vif_cache *cache,
6104 				       enum set_key_cmd cmd,
6105 				       struct ieee80211_sta *sta,
6106 				       struct ieee80211_key_conf *key)
6107 {
6108 	struct ath12k_key_conf *key_conf, *tmp;
6109 
6110 	list_for_each_entry_safe(key_conf, tmp, &cache->key_conf.list, list) {
6111 		if (key_conf->key != key)
6112 			continue;
6113 
6114 		/* If SET key entry is already present in cache, nothing to do,
6115 		 * just return
6116 		 */
6117 		if (cmd == SET_KEY)
6118 			return 0;
6119 
6120 		/* DEL key for an old SET key which driver hasn't flushed yet.
6121 		 */
6122 		list_del(&key_conf->list);
6123 		kfree(key_conf);
6124 	}
6125 
6126 	if (cmd == SET_KEY) {
6127 		key_conf = kzalloc_obj(*key_conf);
6128 
6129 		if (!key_conf)
6130 			return -ENOMEM;
6131 
6132 		key_conf->cmd = cmd;
6133 		key_conf->sta = sta;
6134 		key_conf->key = key;
6135 		list_add_tail(&key_conf->list,
6136 			      &cache->key_conf.list);
6137 	}
6138 
6139 	return 0;
6140 }
6141 
6142 int ath12k_mac_op_set_key(struct ieee80211_hw *hw, enum set_key_cmd cmd,
6143 			  struct ieee80211_vif *vif, struct ieee80211_sta *sta,
6144 			  struct ieee80211_key_conf *key)
6145 {
6146 	struct ath12k_vif *ahvif = ath12k_vif_to_ahvif(vif);
6147 	struct ath12k_link_vif *arvif;
6148 	struct ath12k_link_sta *arsta = NULL;
6149 	struct ath12k_vif_cache *cache;
6150 	struct ath12k_sta *ahsta;
6151 	unsigned long links;
6152 	u8 link_id;
6153 	int ret;
6154 
6155 	lockdep_assert_wiphy(hw->wiphy);
6156 
6157 	/* IGTK needs to be done in host software */
6158 	if (key->keyidx == 4 || key->keyidx == 5)
6159 		return 1;
6160 
6161 	if (key->keyidx > WMI_MAX_KEY_INDEX)
6162 		return -ENOSPC;
6163 
6164 	if (sta) {
6165 		ahsta = ath12k_sta_to_ahsta(sta);
6166 
6167 		/* For an ML STA Pairwise key is same for all associated link Stations,
6168 		 * hence do set key for all link STAs which are active.
6169 		 */
6170 		if (sta->mlo) {
6171 			links = ahsta->links_map;
6172 			for_each_set_bit(link_id, &links, IEEE80211_MLD_MAX_NUM_LINKS) {
6173 				arvif = wiphy_dereference(hw->wiphy,
6174 							  ahvif->link[link_id]);
6175 				arsta = wiphy_dereference(hw->wiphy,
6176 							  ahsta->link[link_id]);
6177 
6178 				if (WARN_ON(!arvif || !arsta))
6179 					/* arvif and arsta are expected to be valid when
6180 					 * STA is present.
6181 					 */
6182 					continue;
6183 
6184 				ret = ath12k_mac_set_key(arvif->ar, cmd, arvif,
6185 							 arsta, key);
6186 				if (ret)
6187 					break;
6188 			}
6189 
6190 			return 0;
6191 		}
6192 
6193 		arsta = &ahsta->deflink;
6194 		arvif = arsta->arvif;
6195 		if (WARN_ON(!arvif))
6196 			return -EINVAL;
6197 
6198 		ret = ath12k_mac_set_key(arvif->ar, cmd, arvif, arsta, key);
6199 		if (ret)
6200 			return ret;
6201 
6202 		return 0;
6203 	}
6204 
6205 	if (key->link_id >= 0 && key->link_id < IEEE80211_MLD_MAX_NUM_LINKS) {
6206 		link_id = key->link_id;
6207 		arvif = wiphy_dereference(hw->wiphy, ahvif->link[link_id]);
6208 	} else {
6209 		link_id = 0;
6210 		arvif = &ahvif->deflink;
6211 	}
6212 
6213 	if (!arvif || !arvif->is_created) {
6214 		cache = ath12k_ahvif_get_link_cache(ahvif, link_id);
6215 		if (!cache)
6216 			return -ENOSPC;
6217 
6218 		ret = ath12k_mac_update_key_cache(cache, cmd, sta, key);
6219 		if (ret)
6220 			return ret;
6221 
6222 		return 0;
6223 	}
6224 
6225 	ret = ath12k_mac_set_key(arvif->ar, cmd, arvif, NULL, key);
6226 	if (ret)
6227 		return ret;
6228 
6229 	return 0;
6230 }
6231 EXPORT_SYMBOL(ath12k_mac_op_set_key);
6232 
6233 static int
6234 ath12k_mac_bitrate_mask_num_vht_rates(struct ath12k *ar,
6235 				      enum nl80211_band band,
6236 				      const struct cfg80211_bitrate_mask *mask)
6237 {
6238 	int num_rates = 0;
6239 	int i;
6240 
6241 	for (i = 0; i < ARRAY_SIZE(mask->control[band].vht_mcs); i++)
6242 		num_rates += hweight16(mask->control[band].vht_mcs[i]);
6243 
6244 	return num_rates;
6245 }
6246 
6247 static int
6248 ath12k_mac_bitrate_mask_num_he_rates(struct ath12k *ar,
6249 				     enum nl80211_band band,
6250 				     const struct cfg80211_bitrate_mask *mask)
6251 {
6252 	int num_rates = 0;
6253 	int i;
6254 
6255 	for (i = 0; i < ARRAY_SIZE(mask->control[band].he_mcs); i++)
6256 		num_rates += hweight16(mask->control[band].he_mcs[i]);
6257 
6258 	return num_rates;
6259 }
6260 
6261 static int
6262 ath12k_mac_bitrate_mask_num_eht_rates(struct ath12k *ar,
6263 				      enum nl80211_band band,
6264 				      const struct cfg80211_bitrate_mask *mask)
6265 {
6266 	int num_rates = 0;
6267 	int i;
6268 
6269 	for (i = 0; i < ARRAY_SIZE(mask->control[band].eht_mcs); i++)
6270 		num_rates += hweight16(mask->control[band].eht_mcs[i]);
6271 
6272 	return num_rates;
6273 }
6274 
6275 static int
6276 ath12k_mac_set_peer_vht_fixed_rate(struct ath12k_link_vif *arvif,
6277 				   struct ath12k_link_sta *arsta,
6278 				   const struct cfg80211_bitrate_mask *mask,
6279 				   enum nl80211_band band)
6280 {
6281 	struct ath12k *ar = arvif->ar;
6282 	u8 vht_rate, nss;
6283 	u32 rate_code;
6284 	int ret, i;
6285 
6286 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
6287 
6288 	nss = 0;
6289 
6290 	for (i = 0; i < ARRAY_SIZE(mask->control[band].vht_mcs); i++) {
6291 		if (hweight16(mask->control[band].vht_mcs[i]) == 1) {
6292 			nss = i + 1;
6293 			vht_rate = ffs(mask->control[band].vht_mcs[i]) - 1;
6294 		}
6295 	}
6296 
6297 	if (!nss) {
6298 		ath12k_warn(ar->ab, "No single VHT Fixed rate found to set for %pM",
6299 			    arsta->addr);
6300 		return -EINVAL;
6301 	}
6302 
6303 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
6304 		   "Setting Fixed VHT Rate for peer %pM. Device will not switch to any other selected rates",
6305 		   arsta->addr);
6306 
6307 	rate_code = ATH12K_HW_RATE_CODE(vht_rate, nss - 1,
6308 					WMI_RATE_PREAMBLE_VHT);
6309 	ret = ath12k_wmi_set_peer_param(ar, arsta->addr,
6310 					arvif->vdev_id,
6311 					WMI_PEER_PARAM_FIXED_RATE,
6312 					rate_code);
6313 	if (ret)
6314 		ath12k_warn(ar->ab,
6315 			    "failed to update STA %pM Fixed Rate %d: %d\n",
6316 			     arsta->addr, rate_code, ret);
6317 
6318 	return ret;
6319 }
6320 
6321 static int
6322 ath12k_mac_set_peer_he_fixed_rate(struct ath12k_link_vif *arvif,
6323 				  struct ath12k_link_sta *arsta,
6324 				  const struct cfg80211_bitrate_mask *mask,
6325 				  enum nl80211_band band)
6326 {
6327 	struct ath12k *ar = arvif->ar;
6328 	u8 he_rate, nss;
6329 	u32 rate_code;
6330 	int ret, i;
6331 	struct ath12k_sta *ahsta = arsta->ahsta;
6332 	struct ieee80211_sta *sta;
6333 
6334 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
6335 
6336 	sta = ath12k_ahsta_to_sta(ahsta);
6337 	nss = 0;
6338 
6339 	for (i = 0; i < ARRAY_SIZE(mask->control[band].he_mcs); i++) {
6340 		if (hweight16(mask->control[band].he_mcs[i]) == 1) {
6341 			nss = i + 1;
6342 			he_rate = ffs(mask->control[band].he_mcs[i]) - 1;
6343 		}
6344 	}
6345 
6346 	if (!nss) {
6347 		ath12k_warn(ar->ab, "No single HE Fixed rate found to set for %pM",
6348 			    arsta->addr);
6349 		return -EINVAL;
6350 	}
6351 
6352 	/* Avoid updating invalid nss as fixed rate*/
6353 	if (nss > sta->deflink.rx_nss)
6354 		return -EINVAL;
6355 
6356 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
6357 		   "Setting Fixed HE Rate for peer %pM. Device will not switch to any other selected rates",
6358 		   arsta->addr);
6359 
6360 	rate_code = ATH12K_HW_RATE_CODE(he_rate, nss - 1,
6361 					WMI_RATE_PREAMBLE_HE);
6362 
6363 	ret = ath12k_wmi_set_peer_param(ar, arsta->addr,
6364 					arvif->vdev_id,
6365 					WMI_PEER_PARAM_FIXED_RATE,
6366 					rate_code);
6367 	if (ret)
6368 		ath12k_warn(ar->ab,
6369 			    "failed to update STA %pM Fixed Rate %d: %d\n",
6370 			    arsta->addr, rate_code, ret);
6371 
6372 	return ret;
6373 }
6374 
6375 static int
6376 ath12k_mac_set_peer_eht_fixed_rate(struct ath12k_link_vif *arvif,
6377 				   struct ath12k_link_sta *arsta,
6378 				   const struct cfg80211_bitrate_mask *mask,
6379 				   enum nl80211_band band)
6380 {
6381 	struct ath12k_sta *ahsta = arsta->ahsta;
6382 	struct ath12k *ar = arvif->ar;
6383 	struct ieee80211_sta *sta;
6384 	struct ieee80211_link_sta *link_sta;
6385 	u8 eht_rate, nss = 0;
6386 	u32 rate_code;
6387 	int ret, i;
6388 
6389 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
6390 
6391 	sta = ath12k_ahsta_to_sta(ahsta);
6392 
6393 	for (i = 0; i < ARRAY_SIZE(mask->control[band].eht_mcs); i++) {
6394 		if (hweight16(mask->control[band].eht_mcs[i]) == 1) {
6395 			nss = i + 1;
6396 			eht_rate = ffs(mask->control[band].eht_mcs[i]) - 1;
6397 		}
6398 	}
6399 
6400 	if (!nss) {
6401 		ath12k_warn(ar->ab, "No single EHT Fixed rate found to set for %pM\n",
6402 			    arsta->addr);
6403 		return -EINVAL;
6404 	}
6405 
6406 	/* Avoid updating invalid nss as fixed rate*/
6407 	link_sta = ath12k_mac_get_link_sta(arsta);
6408 	if (!link_sta || nss > link_sta->rx_nss) {
6409 		ath12k_warn(ar->ab,
6410 			    "unable to access link sta for sta %pM link %u or fixed nss of %u is not supported by sta\n",
6411 			    sta->addr, arsta->link_id, nss);
6412 		return -EINVAL;
6413 	}
6414 
6415 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
6416 		   "Setting Fixed EHT Rate for peer %pM. Device will not switch to any other selected rates\n",
6417 		   arsta->addr);
6418 
6419 	rate_code = ATH12K_HW_RATE_CODE(eht_rate, nss - 1,
6420 					WMI_RATE_PREAMBLE_EHT);
6421 
6422 	ret = ath12k_wmi_set_peer_param(ar, arsta->addr,
6423 					arvif->vdev_id,
6424 					WMI_PEER_PARAM_FIXED_RATE,
6425 					rate_code);
6426 	if (ret)
6427 		ath12k_warn(ar->ab,
6428 			    "failed to update STA %pM Fixed Rate %d: %d\n",
6429 			    arsta->addr, rate_code, ret);
6430 
6431 	return ret;
6432 }
6433 
6434 static int ath12k_mac_station_assoc(struct ath12k *ar,
6435 				    struct ath12k_link_vif *arvif,
6436 				    struct ath12k_link_sta *arsta,
6437 				    bool reassoc)
6438 {
6439 	struct ieee80211_vif *vif = ath12k_ahvif_to_vif(arvif->ahvif);
6440 	struct ieee80211_sta *sta = ath12k_ahsta_to_sta(arsta->ahsta);
6441 	struct ieee80211_link_sta *link_sta;
6442 	int ret;
6443 	struct cfg80211_chan_def def;
6444 	enum nl80211_band band;
6445 	struct cfg80211_bitrate_mask *mask;
6446 	u8 num_vht_rates, num_he_rates, num_eht_rates;
6447 	u8 link_id = arvif->link_id;
6448 
6449 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
6450 
6451 	if (WARN_ON(ath12k_mac_vif_link_chan(vif, arvif->link_id, &def)))
6452 		return -EPERM;
6453 
6454 	if (WARN_ON(!rcu_access_pointer(sta->link[link_id])))
6455 		return -EINVAL;
6456 
6457 	band = def.chan->band;
6458 	mask = &arvif->bitrate_mask;
6459 
6460 	struct ath12k_wmi_peer_assoc_arg *peer_arg __free(kfree) =
6461 		kzalloc_obj(*peer_arg);
6462 	if (!peer_arg)
6463 		return -ENOMEM;
6464 
6465 	ath12k_peer_assoc_prepare(ar, arvif, arsta, peer_arg, reassoc);
6466 
6467 	if (peer_arg->peer_nss < 1) {
6468 		ath12k_warn(ar->ab,
6469 			    "invalid peer NSS %d\n", peer_arg->peer_nss);
6470 		return -EINVAL;
6471 	}
6472 
6473 	peer_arg->is_assoc = true;
6474 	ret = ath12k_wmi_send_peer_assoc_cmd(ar, peer_arg);
6475 	if (ret) {
6476 		ath12k_warn(ar->ab, "failed to run peer assoc for STA %pM vdev %i: %d\n",
6477 			    arsta->addr, arvif->vdev_id, ret);
6478 		return ret;
6479 	}
6480 
6481 	if (!wait_for_completion_timeout(&ar->peer_assoc_done, 1 * HZ)) {
6482 		ath12k_warn(ar->ab, "failed to get peer assoc conf event for %pM vdev %i\n",
6483 			    arsta->addr, arvif->vdev_id);
6484 		return -ETIMEDOUT;
6485 	}
6486 
6487 	num_vht_rates = ath12k_mac_bitrate_mask_num_vht_rates(ar, band, mask);
6488 	num_he_rates = ath12k_mac_bitrate_mask_num_he_rates(ar, band, mask);
6489 	num_eht_rates = ath12k_mac_bitrate_mask_num_eht_rates(ar, band, mask);
6490 
6491 	/* If single VHT/HE/EHT rate is configured (by set_bitrate_mask()),
6492 	 * peer_assoc will disable VHT/HE/EHT. This is now enabled by a peer
6493 	 * specific fixed param.
6494 	 * Note that all other rates and NSS will be disabled for this peer.
6495 	 */
6496 	link_sta = ath12k_mac_get_link_sta(arsta);
6497 	if (!link_sta) {
6498 		ath12k_warn(ar->ab, "unable to access link sta in station assoc\n");
6499 		return -EINVAL;
6500 	}
6501 
6502 	spin_lock_bh(&ar->data_lock);
6503 	arsta->bw = ath12k_mac_ieee80211_sta_bw_to_wmi(ar, link_sta);
6504 	arsta->bw_prev = link_sta->bandwidth;
6505 	spin_unlock_bh(&ar->data_lock);
6506 
6507 	if (link_sta->vht_cap.vht_supported && num_vht_rates == 1) {
6508 		ret = ath12k_mac_set_peer_vht_fixed_rate(arvif, arsta, mask, band);
6509 	} else if (link_sta->he_cap.has_he && num_he_rates == 1) {
6510 		ret = ath12k_mac_set_peer_he_fixed_rate(arvif, arsta, mask, band);
6511 		if (ret)
6512 			return ret;
6513 	} else if (link_sta->eht_cap.has_eht && num_eht_rates == 1) {
6514 		ret = ath12k_mac_set_peer_eht_fixed_rate(arvif, arsta, mask, band);
6515 		if (ret)
6516 			return ret;
6517 	}
6518 
6519 	/* Re-assoc is run only to update supported rates for given station. It
6520 	 * doesn't make much sense to reconfigure the peer completely.
6521 	 */
6522 	if (reassoc)
6523 		return 0;
6524 
6525 	ret = ath12k_setup_peer_smps(ar, arvif, arsta->addr,
6526 				     &link_sta->ht_cap, &link_sta->he_6ghz_capa);
6527 	if (ret) {
6528 		ath12k_warn(ar->ab, "failed to setup peer SMPS for vdev %d: %d\n",
6529 			    arvif->vdev_id, ret);
6530 		return ret;
6531 	}
6532 
6533 	if (!sta->wme) {
6534 		arvif->num_legacy_stations++;
6535 		ret = ath12k_recalc_rtscts_prot(arvif);
6536 		if (ret)
6537 			return ret;
6538 	}
6539 
6540 	if (sta->wme && sta->uapsd_queues) {
6541 		ret = ath12k_peer_assoc_qos_ap(ar, arvif, arsta);
6542 		if (ret) {
6543 			ath12k_warn(ar->ab, "failed to set qos params for STA %pM for vdev %i: %d\n",
6544 				    arsta->addr, arvif->vdev_id, ret);
6545 			return ret;
6546 		}
6547 	}
6548 
6549 	return 0;
6550 }
6551 
6552 static int ath12k_mac_station_disassoc(struct ath12k *ar,
6553 				       struct ath12k_link_vif *arvif,
6554 				       struct ath12k_link_sta *arsta)
6555 {
6556 	struct ieee80211_sta *sta = ath12k_ahsta_to_sta(arsta->ahsta);
6557 
6558 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
6559 
6560 	if (!sta->wme) {
6561 		arvif->num_legacy_stations--;
6562 		return ath12k_recalc_rtscts_prot(arvif);
6563 	}
6564 
6565 	return 0;
6566 }
6567 
6568 static void ath12k_sta_rc_update_wk(struct wiphy *wiphy, struct wiphy_work *wk)
6569 {
6570 	struct ieee80211_link_sta *link_sta;
6571 	struct ath12k *ar;
6572 	struct ath12k_link_vif *arvif;
6573 	struct ieee80211_sta *sta;
6574 	struct cfg80211_chan_def def;
6575 	enum nl80211_band band;
6576 	const u8 *ht_mcs_mask;
6577 	const u16 *vht_mcs_mask;
6578 	const u16 *he_mcs_mask;
6579 	const u16 *eht_mcs_mask;
6580 	u32 changed, bw, nss, mac_nss, smps, bw_prev;
6581 	int err, num_vht_rates, num_he_rates, num_eht_rates;
6582 	const struct cfg80211_bitrate_mask *mask;
6583 	enum wmi_phy_mode peer_phymode;
6584 	struct ath12k_link_sta *arsta;
6585 	struct ieee80211_vif *vif;
6586 
6587 	lockdep_assert_wiphy(wiphy);
6588 
6589 	arsta = container_of(wk, struct ath12k_link_sta, update_wk);
6590 	sta = ath12k_ahsta_to_sta(arsta->ahsta);
6591 	arvif = arsta->arvif;
6592 	vif = ath12k_ahvif_to_vif(arvif->ahvif);
6593 	ar = arvif->ar;
6594 
6595 	if (WARN_ON(ath12k_mac_vif_link_chan(vif, arvif->link_id, &def)))
6596 		return;
6597 
6598 	band = def.chan->band;
6599 	ht_mcs_mask = arvif->bitrate_mask.control[band].ht_mcs;
6600 	vht_mcs_mask = arvif->bitrate_mask.control[band].vht_mcs;
6601 	he_mcs_mask = arvif->bitrate_mask.control[band].he_mcs;
6602 	eht_mcs_mask = arvif->bitrate_mask.control[band].eht_mcs;
6603 
6604 	spin_lock_bh(&ar->data_lock);
6605 
6606 	changed = arsta->changed;
6607 	arsta->changed = 0;
6608 
6609 	bw = arsta->bw;
6610 	bw_prev = arsta->bw_prev;
6611 	nss = arsta->nss;
6612 	smps = arsta->smps;
6613 
6614 	spin_unlock_bh(&ar->data_lock);
6615 
6616 	nss = max_t(u32, 1, nss);
6617 	mac_nss = max3(ath12k_mac_max_ht_nss(ht_mcs_mask),
6618 		       ath12k_mac_max_vht_nss(vht_mcs_mask),
6619 		       ath12k_mac_max_he_nss(he_mcs_mask));
6620 	mac_nss = max(mac_nss, ath12k_mac_max_eht_nss(eht_mcs_mask));
6621 	nss = min(nss, mac_nss);
6622 
6623 	struct ath12k_wmi_peer_assoc_arg *peer_arg __free(kfree) =
6624 					kzalloc_obj(*peer_arg);
6625 	if (!peer_arg)
6626 		return;
6627 
6628 	if (changed & IEEE80211_RC_BW_CHANGED) {
6629 		ath12k_peer_assoc_h_phymode(ar, arvif, arsta, peer_arg);
6630 		peer_phymode = peer_arg->peer_phymode;
6631 
6632 		if (bw > bw_prev) {
6633 			/* Phymode shows maximum supported channel width, if we
6634 			 * upgrade bandwidth then due to sanity check of firmware,
6635 			 * we have to send WMI_PEER_PHYMODE followed by
6636 			 * WMI_PEER_CHWIDTH
6637 			 */
6638 			ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac bandwidth upgrade for sta %pM new %d old %d\n",
6639 				   arsta->addr, bw, bw_prev);
6640 			err = ath12k_wmi_set_peer_param(ar, arsta->addr,
6641 							arvif->vdev_id, WMI_PEER_PHYMODE,
6642 							peer_phymode);
6643 			if (err) {
6644 				ath12k_warn(ar->ab, "failed to update STA %pM to peer phymode %d: %d\n",
6645 					    arsta->addr, peer_phymode, err);
6646 				return;
6647 			}
6648 			err = ath12k_wmi_set_peer_param(ar, arsta->addr,
6649 							arvif->vdev_id, WMI_PEER_CHWIDTH,
6650 							bw);
6651 			if (err)
6652 				ath12k_warn(ar->ab, "failed to update STA %pM to peer bandwidth %d: %d\n",
6653 					    arsta->addr, bw, err);
6654 		} else {
6655 			/* When we downgrade bandwidth this will conflict with phymode
6656 			 * and cause to trigger firmware crash. In this case we send
6657 			 * WMI_PEER_CHWIDTH followed by WMI_PEER_PHYMODE
6658 			 */
6659 			ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac bandwidth downgrade for sta %pM new %d old %d\n",
6660 				   arsta->addr, bw, bw_prev);
6661 			err = ath12k_wmi_set_peer_param(ar, arsta->addr,
6662 							arvif->vdev_id, WMI_PEER_CHWIDTH,
6663 							bw);
6664 			if (err) {
6665 				ath12k_warn(ar->ab, "failed to update STA %pM peer to bandwidth %d: %d\n",
6666 					    arsta->addr, bw, err);
6667 				return;
6668 			}
6669 			err = ath12k_wmi_set_peer_param(ar, arsta->addr,
6670 							arvif->vdev_id, WMI_PEER_PHYMODE,
6671 							peer_phymode);
6672 			if (err)
6673 				ath12k_warn(ar->ab, "failed to update STA %pM to peer phymode %d: %d\n",
6674 					    arsta->addr, peer_phymode, err);
6675 		}
6676 	}
6677 
6678 	if (changed & IEEE80211_RC_NSS_CHANGED) {
6679 		ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac update sta %pM nss %d\n",
6680 			   arsta->addr, nss);
6681 
6682 		err = ath12k_wmi_set_peer_param(ar, arsta->addr, arvif->vdev_id,
6683 						WMI_PEER_NSS, nss);
6684 		if (err)
6685 			ath12k_warn(ar->ab, "failed to update STA %pM nss %d: %d\n",
6686 				    arsta->addr, nss, err);
6687 	}
6688 
6689 	if (changed & IEEE80211_RC_SMPS_CHANGED) {
6690 		ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac update sta %pM smps %d\n",
6691 			   arsta->addr, smps);
6692 
6693 		err = ath12k_wmi_set_peer_param(ar, arsta->addr, arvif->vdev_id,
6694 						WMI_PEER_MIMO_PS_STATE, smps);
6695 		if (err)
6696 			ath12k_warn(ar->ab, "failed to update STA %pM smps %d: %d\n",
6697 				    arsta->addr, smps, err);
6698 	}
6699 
6700 	if (changed & IEEE80211_RC_SUPP_RATES_CHANGED) {
6701 		mask = &arvif->bitrate_mask;
6702 		num_vht_rates = ath12k_mac_bitrate_mask_num_vht_rates(ar, band,
6703 								      mask);
6704 		num_he_rates = ath12k_mac_bitrate_mask_num_he_rates(ar, band,
6705 								    mask);
6706 		num_eht_rates = ath12k_mac_bitrate_mask_num_eht_rates(ar, band,
6707 								      mask);
6708 
6709 		/* Peer_assoc_prepare will reject vht rates in
6710 		 * bitrate_mask if its not available in range format and
6711 		 * sets vht tx_rateset as unsupported. So multiple VHT MCS
6712 		 * setting(eg. MCS 4,5,6) per peer is not supported here.
6713 		 * But, Single rate in VHT mask can be set as per-peer
6714 		 * fixed rate. But even if any HT rates are configured in
6715 		 * the bitrate mask, device will not switch to those rates
6716 		 * when per-peer Fixed rate is set.
6717 		 * TODO: Check RATEMASK_CMDID to support auto rates selection
6718 		 * across HT/VHT and for multiple VHT MCS support.
6719 		 */
6720 		link_sta = ath12k_mac_get_link_sta(arsta);
6721 		if (!link_sta) {
6722 			ath12k_warn(ar->ab, "unable to access link sta in peer assoc he for sta %pM link %u\n",
6723 				    sta->addr, arsta->link_id);
6724 			return;
6725 		}
6726 
6727 		if (link_sta->vht_cap.vht_supported && num_vht_rates == 1) {
6728 			ath12k_mac_set_peer_vht_fixed_rate(arvif, arsta, mask,
6729 							   band);
6730 		} else if (link_sta->he_cap.has_he && num_he_rates == 1) {
6731 			ath12k_mac_set_peer_he_fixed_rate(arvif, arsta, mask, band);
6732 		} else if (link_sta->eht_cap.has_eht && num_eht_rates == 1) {
6733 			err = ath12k_mac_set_peer_eht_fixed_rate(arvif, arsta,
6734 								 mask, band);
6735 			if (err) {
6736 				ath12k_warn(ar->ab,
6737 					    "failed to set peer EHT fixed rate for STA %pM ret %d\n",
6738 					    arsta->addr, err);
6739 				return;
6740 			}
6741 		} else {
6742 			/* If the peer is non-VHT/HE/EHT or no fixed VHT/HE/EHT
6743 			 * rate is provided in the new bitrate mask we set the
6744 			 * other rates using peer_assoc command. Also clear
6745 			 * the peer fixed rate settings as it has higher proprity
6746 			 * than peer assoc
6747 			 */
6748 			err = ath12k_wmi_set_peer_param(ar, arsta->addr,
6749 							arvif->vdev_id,
6750 							WMI_PEER_PARAM_FIXED_RATE,
6751 							WMI_FIXED_RATE_NONE);
6752 			if (err)
6753 				ath12k_warn(ar->ab,
6754 					    "failed to disable peer fixed rate for STA %pM ret %d\n",
6755 					    arsta->addr, err);
6756 
6757 			ath12k_peer_assoc_prepare(ar, arvif, arsta,
6758 						  peer_arg, true);
6759 
6760 			peer_arg->is_assoc = false;
6761 			err = ath12k_wmi_send_peer_assoc_cmd(ar, peer_arg);
6762 			if (err)
6763 				ath12k_warn(ar->ab, "failed to run peer assoc for STA %pM vdev %i: %d\n",
6764 					    arsta->addr, arvif->vdev_id, err);
6765 
6766 			if (!wait_for_completion_timeout(&ar->peer_assoc_done, 1 * HZ))
6767 				ath12k_warn(ar->ab, "failed to get peer assoc conf event for %pM vdev %i\n",
6768 					    arsta->addr, arvif->vdev_id);
6769 		}
6770 	}
6771 }
6772 
6773 static void ath12k_mac_free_unassign_link_sta(struct ath12k_hw *ah,
6774 					      struct ath12k_sta *ahsta,
6775 					      u8 link_id)
6776 {
6777 	struct ath12k_link_sta *arsta;
6778 
6779 	lockdep_assert_wiphy(ah->hw->wiphy);
6780 
6781 	if (WARN_ON(link_id >= IEEE80211_MLD_MAX_NUM_LINKS))
6782 		return;
6783 
6784 	arsta = wiphy_dereference(ah->hw->wiphy, ahsta->link[link_id]);
6785 	if (WARN_ON(!arsta))
6786 		return;
6787 
6788 	ahsta->links_map &= ~BIT(link_id);
6789 	rcu_assign_pointer(ahsta->link[link_id], NULL);
6790 	synchronize_rcu();
6791 
6792 	if (arsta == &ahsta->deflink) {
6793 		arsta->link_id = ATH12K_INVALID_LINK_ID;
6794 		arsta->ahsta = NULL;
6795 		arsta->arvif = NULL;
6796 		return;
6797 	}
6798 
6799 	kfree(arsta);
6800 }
6801 
6802 static int ath12k_mac_inc_num_stations(struct ath12k_link_vif *arvif,
6803 				       struct ath12k_link_sta *arsta)
6804 {
6805 	struct ieee80211_sta *sta = ath12k_ahsta_to_sta(arsta->ahsta);
6806 	struct ath12k *ar = arvif->ar;
6807 
6808 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
6809 
6810 	if (arvif->ahvif->vdev_type == WMI_VDEV_TYPE_STA && !sta->tdls)
6811 		return 0;
6812 
6813 	if (ar->num_stations >= ar->max_num_stations)
6814 		return -ENOBUFS;
6815 
6816 	ar->num_stations++;
6817 	arvif->num_stations++;
6818 
6819 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
6820 		   "mac station %pM connected to vdev %u num_stations %u\n",
6821 		   arsta->addr, arvif->vdev_id, arvif->num_stations);
6822 
6823 	return 0;
6824 }
6825 
6826 static void ath12k_mac_dec_num_stations(struct ath12k_link_vif *arvif,
6827 					struct ath12k_link_sta *arsta)
6828 {
6829 	struct ieee80211_sta *sta = ath12k_ahsta_to_sta(arsta->ahsta);
6830 	struct ath12k *ar = arvif->ar;
6831 
6832 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
6833 
6834 	if (arvif->ahvif->vdev_type == WMI_VDEV_TYPE_STA && !sta->tdls)
6835 		return;
6836 
6837 	ar->num_stations--;
6838 
6839 	if (arvif->num_stations) {
6840 		arvif->num_stations--;
6841 		ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
6842 			   "mac station %pM disconnected from vdev %u num_stations %u\n",
6843 			   arsta->addr, arvif->vdev_id, arvif->num_stations);
6844 	} else {
6845 		ath12k_warn(ar->ab,
6846 			    "mac station %pM disconnect for vdev %u without any connected station\n",
6847 			    arsta->addr, arvif->vdev_id);
6848 	}
6849 }
6850 
6851 static void ath12k_mac_station_post_remove(struct ath12k *ar,
6852 					   struct ath12k_link_vif *arvif,
6853 					   struct ath12k_link_sta *arsta)
6854 {
6855 	struct ieee80211_vif *vif = ath12k_ahvif_to_vif(arvif->ahvif);
6856 	struct ieee80211_sta *sta = ath12k_ahsta_to_sta(arsta->ahsta);
6857 	struct ath12k_dp_link_peer *peer;
6858 	struct ath12k_dp *dp = ath12k_ab_to_dp(ar->ab);
6859 
6860 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
6861 
6862 	ath12k_mac_dec_num_stations(arvif, arsta);
6863 
6864 	spin_lock_bh(&dp->dp_lock);
6865 
6866 	peer = ath12k_dp_link_peer_find_by_vdev_and_addr(dp, arvif->vdev_id,
6867 							 arsta->addr);
6868 	if (peer && peer->sta == sta) {
6869 		ath12k_warn(ar->ab, "Found peer entry %pM n vdev %i after it was supposedly removed\n",
6870 			    vif->addr, arvif->vdev_id);
6871 		peer->sta = NULL;
6872 
6873 		ath12k_dp_link_peer_free(peer);
6874 		ar->num_peers--;
6875 	}
6876 
6877 	spin_unlock_bh(&dp->dp_lock);
6878 }
6879 
6880 static int ath12k_mac_station_unauthorize(struct ath12k *ar,
6881 					  struct ath12k_link_vif *arvif,
6882 					  struct ath12k_link_sta *arsta)
6883 {
6884 	struct ath12k_dp_link_peer *peer;
6885 	int ret;
6886 	struct ath12k_dp *dp = ath12k_ab_to_dp(ar->ab);
6887 
6888 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
6889 
6890 	spin_lock_bh(&dp->dp_lock);
6891 
6892 	peer = ath12k_dp_link_peer_find_by_vdev_and_addr(dp, arvif->vdev_id,
6893 							 arsta->addr);
6894 	if (peer)
6895 		peer->is_authorized = false;
6896 
6897 	spin_unlock_bh(&dp->dp_lock);
6898 
6899 	/* Driver must clear the keys during the state change from
6900 	 * IEEE80211_STA_AUTHORIZED to IEEE80211_STA_ASSOC, since after
6901 	 * returning from here, mac80211 is going to delete the keys
6902 	 * in __sta_info_destroy_part2(). This will ensure that the driver does
6903 	 * not retain stale key references after mac80211 deletes the keys.
6904 	 */
6905 	ret = ath12k_clear_peer_keys(arvif, arsta->addr);
6906 	if (ret) {
6907 		ath12k_warn(ar->ab, "failed to clear all peer keys for vdev %i: %d\n",
6908 			    arvif->vdev_id, ret);
6909 		return ret;
6910 	}
6911 
6912 	return 0;
6913 }
6914 
6915 static int ath12k_mac_station_authorize(struct ath12k *ar,
6916 					struct ath12k_link_vif *arvif,
6917 					struct ath12k_link_sta *arsta)
6918 {
6919 	struct ath12k_dp_link_peer *peer;
6920 	struct ieee80211_vif *vif = ath12k_ahvif_to_vif(arvif->ahvif);
6921 	int ret;
6922 	struct ath12k_dp *dp = ath12k_ab_to_dp(ar->ab);
6923 
6924 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
6925 
6926 	spin_lock_bh(&dp->dp_lock);
6927 
6928 	peer = ath12k_dp_link_peer_find_by_vdev_and_addr(dp, arvif->vdev_id,
6929 							 arsta->addr);
6930 	if (peer)
6931 		peer->is_authorized = true;
6932 
6933 	spin_unlock_bh(&dp->dp_lock);
6934 
6935 	if (vif->type == NL80211_IFTYPE_STATION && arvif->is_up) {
6936 		ret = ath12k_wmi_set_peer_param(ar, arsta->addr,
6937 						arvif->vdev_id,
6938 						WMI_PEER_AUTHORIZE,
6939 						1);
6940 		if (ret) {
6941 			ath12k_warn(ar->ab, "Unable to authorize peer %pM vdev %d: %d\n",
6942 				    arsta->addr, arvif->vdev_id, ret);
6943 			return ret;
6944 		}
6945 	}
6946 
6947 	return 0;
6948 }
6949 
6950 static int ath12k_mac_station_remove(struct ath12k *ar,
6951 				     struct ath12k_link_vif *arvif,
6952 				     struct ath12k_link_sta *arsta)
6953 {
6954 	struct ieee80211_sta *sta = ath12k_ahsta_to_sta(arsta->ahsta);
6955 	struct ath12k_vif *ahvif = arvif->ahvif;
6956 	int ret = 0;
6957 	struct ath12k_link_sta *temp_arsta;
6958 
6959 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
6960 
6961 	wiphy_work_cancel(ar->ah->hw->wiphy, &arsta->update_wk);
6962 
6963 	if (ahvif->vdev_type == WMI_VDEV_TYPE_STA) {
6964 		ath12k_bss_disassoc(ar, arvif);
6965 		ret = ath12k_mac_vdev_stop(arvif);
6966 		if (ret)
6967 			ath12k_warn(ar->ab, "failed to stop vdev %i: %d\n",
6968 				    arvif->vdev_id, ret);
6969 	}
6970 
6971 	if (sta->mlo)
6972 		return ret;
6973 
6974 	ath12k_dp_peer_cleanup(ar, arvif->vdev_id, arsta->addr);
6975 
6976 	ret = ath12k_peer_delete(ar, arvif->vdev_id, arsta->addr);
6977 	if (ret)
6978 		ath12k_warn(ar->ab, "Failed to delete peer: %pM for VDEV: %d\n",
6979 			    arsta->addr, arvif->vdev_id);
6980 	else
6981 		ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "Removed peer: %pM for VDEV: %d\n",
6982 			   arsta->addr, arvif->vdev_id);
6983 
6984 	ath12k_mac_station_post_remove(ar, arvif, arsta);
6985 
6986 	spin_lock_bh(&ar->ab->base_lock);
6987 
6988 	/* To handle roaming and split phy scenario */
6989 	temp_arsta = ath12k_link_sta_find_by_addr(ar->ab, arsta->addr);
6990 	if (temp_arsta && temp_arsta->arvif->ar == ar)
6991 		ath12k_link_sta_rhash_delete(ar->ab, arsta);
6992 
6993 	spin_unlock_bh(&ar->ab->base_lock);
6994 
6995 	if (sta->valid_links)
6996 		ath12k_mac_free_unassign_link_sta(ahvif->ah,
6997 						  arsta->ahsta, arsta->link_id);
6998 
6999 	return ret;
7000 }
7001 
7002 static int ath12k_mac_station_add(struct ath12k *ar,
7003 				  struct ath12k_link_vif *arvif,
7004 				  struct ath12k_link_sta *arsta)
7005 {
7006 	struct ath12k_base *ab = ar->ab;
7007 	struct ieee80211_vif *vif = ath12k_ahvif_to_vif(arvif->ahvif);
7008 	struct ieee80211_sta *sta = ath12k_ahsta_to_sta(arsta->ahsta);
7009 	struct ath12k_wmi_peer_create_arg peer_param = {};
7010 	int ret;
7011 	struct ath12k_link_sta *temp_arsta;
7012 
7013 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
7014 
7015 	ret = ath12k_mac_inc_num_stations(arvif, arsta);
7016 	if (ret) {
7017 		ath12k_warn(ab, "refusing to associate station: too many connected already (%d)\n",
7018 			    ar->max_num_stations);
7019 		goto exit;
7020 	}
7021 
7022 	spin_lock_bh(&ab->base_lock);
7023 
7024 	/*
7025 	 * In case of Split PHY and roaming scenario, pdev idx
7026 	 * might differ but both the pdev will share same rhash
7027 	 * table. In that case update the rhash table if link_sta is
7028 	 * already present
7029 	 */
7030 	temp_arsta = ath12k_link_sta_find_by_addr(ab, arsta->addr);
7031 	if (temp_arsta && temp_arsta->arvif->ar != ar)
7032 		ath12k_link_sta_rhash_delete(ab, temp_arsta);
7033 
7034 	ret = ath12k_link_sta_rhash_add(ab, arsta);
7035 	spin_unlock_bh(&ab->base_lock);
7036 	if (ret) {
7037 		ath12k_warn(ab, "Failed to add arsta: %pM to hash table, ret: %d",
7038 			    arsta->addr, ret);
7039 		goto dec_num_station;
7040 	}
7041 
7042 	peer_param.vdev_id = arvif->vdev_id;
7043 	peer_param.peer_addr = arsta->addr;
7044 	peer_param.peer_type = WMI_PEER_TYPE_DEFAULT;
7045 	peer_param.ml_enabled = sta->mlo;
7046 
7047 	ret = ath12k_peer_create(ar, arvif, sta, &peer_param);
7048 	if (ret) {
7049 		ath12k_warn(ab, "Failed to add peer: %pM for VDEV: %d\n",
7050 			    arsta->addr, arvif->vdev_id);
7051 		goto free_peer;
7052 	}
7053 
7054 	ath12k_dbg(ab, ATH12K_DBG_MAC, "Added peer: %pM for VDEV: %d\n",
7055 		   arsta->addr, arvif->vdev_id);
7056 
7057 	if (ieee80211_vif_is_mesh(vif)) {
7058 		ret = ath12k_wmi_set_peer_param(ar, arsta->addr,
7059 						arvif->vdev_id,
7060 						WMI_PEER_USE_4ADDR, 1);
7061 		if (ret) {
7062 			ath12k_warn(ab, "failed to STA %pM 4addr capability: %d\n",
7063 				    arsta->addr, ret);
7064 			goto free_peer;
7065 		}
7066 	}
7067 
7068 	ret = ath12k_dp_peer_setup(ar, arvif->vdev_id, arsta->addr);
7069 	if (ret) {
7070 		ath12k_warn(ab, "failed to setup dp for peer %pM on vdev %i (%d)\n",
7071 			    arsta->addr, arvif->vdev_id, ret);
7072 		goto free_peer;
7073 	}
7074 
7075 	if (ab->hw_params->vdev_start_delay &&
7076 	    !arvif->is_started &&
7077 	    arvif->ahvif->vdev_type != WMI_VDEV_TYPE_AP) {
7078 		ret = ath12k_start_vdev_delay(ar, arvif);
7079 		if (ret) {
7080 			ath12k_warn(ab, "failed to delay vdev start: %d\n", ret);
7081 			goto free_peer;
7082 		}
7083 	}
7084 
7085 	return 0;
7086 
7087 free_peer:
7088 	ath12k_peer_delete(ar, arvif->vdev_id, arsta->addr);
7089 	spin_lock_bh(&ab->base_lock);
7090 	ath12k_link_sta_rhash_delete(ab, arsta);
7091 	spin_unlock_bh(&ab->base_lock);
7092 dec_num_station:
7093 	ath12k_mac_dec_num_stations(arvif, arsta);
7094 exit:
7095 	return ret;
7096 }
7097 
7098 static int ath12k_mac_assign_link_sta(struct ath12k_hw *ah,
7099 				      struct ath12k_sta *ahsta,
7100 				      struct ath12k_link_sta *arsta,
7101 				      struct ath12k_vif *ahvif,
7102 				      u8 link_id)
7103 {
7104 	struct ieee80211_sta *sta = ath12k_ahsta_to_sta(ahsta);
7105 	struct ieee80211_link_sta *link_sta;
7106 	struct ath12k_link_vif *arvif;
7107 
7108 	lockdep_assert_wiphy(ah->hw->wiphy);
7109 
7110 	if (!arsta || link_id >= IEEE80211_MLD_MAX_NUM_LINKS)
7111 		return -EINVAL;
7112 
7113 	arvif = wiphy_dereference(ah->hw->wiphy, ahvif->link[link_id]);
7114 	if (!arvif)
7115 		return -EINVAL;
7116 
7117 	memset(arsta, 0, sizeof(*arsta));
7118 
7119 	link_sta = wiphy_dereference(ah->hw->wiphy, sta->link[link_id]);
7120 	if (!link_sta)
7121 		return -EINVAL;
7122 
7123 	ether_addr_copy(arsta->addr, link_sta->addr);
7124 
7125 	/* logical index of the link sta in order of creation */
7126 	arsta->link_idx = ahsta->num_peer++;
7127 
7128 	arsta->link_id = link_id;
7129 	ahsta->links_map |= BIT(arsta->link_id);
7130 	arsta->arvif = arvif;
7131 	arsta->ahsta = ahsta;
7132 	ahsta->ahvif = ahvif;
7133 
7134 	wiphy_work_init(&arsta->update_wk, ath12k_sta_rc_update_wk);
7135 
7136 	rcu_assign_pointer(ahsta->link[link_id], arsta);
7137 
7138 	return 0;
7139 }
7140 
7141 static void ath12k_mac_ml_station_remove(struct ath12k_vif *ahvif,
7142 					 struct ath12k_sta *ahsta)
7143 {
7144 	struct ieee80211_sta *sta = ath12k_ahsta_to_sta(ahsta);
7145 	struct ath12k_hw *ah = ahvif->ah;
7146 	struct ath12k_link_vif *arvif;
7147 	struct ath12k_link_sta *arsta;
7148 	unsigned long links;
7149 	struct ath12k *ar;
7150 	u8 link_id;
7151 
7152 	lockdep_assert_wiphy(ah->hw->wiphy);
7153 
7154 	ath12k_peer_mlo_link_peers_delete(ahvif, ahsta);
7155 
7156 	/* validate link station removal and clear arsta links */
7157 	links = ahsta->links_map;
7158 	for_each_set_bit(link_id, &links, IEEE80211_MLD_MAX_NUM_LINKS) {
7159 		arvif = wiphy_dereference(ah->hw->wiphy, ahvif->link[link_id]);
7160 		arsta = wiphy_dereference(ah->hw->wiphy, ahsta->link[link_id]);
7161 		if (!arvif || !arsta)
7162 			continue;
7163 
7164 		ar = arvif->ar;
7165 
7166 		ath12k_mac_station_post_remove(ar, arvif, arsta);
7167 
7168 		spin_lock_bh(&ar->ab->base_lock);
7169 		ath12k_link_sta_rhash_delete(ar->ab, arsta);
7170 		spin_unlock_bh(&ar->ab->base_lock);
7171 
7172 		ath12k_mac_free_unassign_link_sta(ah, ahsta, link_id);
7173 	}
7174 
7175 	if (sta->mlo) {
7176 		clear_bit(ahsta->ml_peer_id, ah->free_ml_peer_id_map);
7177 		ahsta->ml_peer_id = ATH12K_MLO_PEER_ID_INVALID;
7178 	}
7179 }
7180 
7181 static int ath12k_mac_handle_link_sta_state(struct ieee80211_hw *hw,
7182 					    struct ath12k_link_vif *arvif,
7183 					    struct ath12k_link_sta *arsta,
7184 					    enum ieee80211_sta_state old_state,
7185 					    enum ieee80211_sta_state new_state)
7186 {
7187 	struct ieee80211_vif *vif = ath12k_ahvif_to_vif(arvif->ahvif);
7188 	struct ieee80211_bss_conf *link_conf;
7189 	struct ath12k *ar = arvif->ar;
7190 	struct ath12k_reg_info *reg_info;
7191 	struct ath12k_base *ab = ar->ab;
7192 	int ret = 0;
7193 
7194 	lockdep_assert_wiphy(hw->wiphy);
7195 
7196 	ath12k_dbg(ab, ATH12K_DBG_MAC, "mac handle link %u sta %pM state %d -> %d\n",
7197 		   arsta->link_id, arsta->addr, old_state, new_state);
7198 
7199 	/* IEEE80211_STA_NONE -> IEEE80211_STA_NOTEXIST: Remove the station
7200 	 * from driver
7201 	 */
7202 	if ((old_state == IEEE80211_STA_NONE &&
7203 	     new_state == IEEE80211_STA_NOTEXIST)) {
7204 		ret = ath12k_mac_station_remove(ar, arvif, arsta);
7205 		if (ret) {
7206 			ath12k_warn(ab, "Failed to remove station: %pM for VDEV: %d\n",
7207 				    arsta->addr, arvif->vdev_id);
7208 			goto exit;
7209 		}
7210 	}
7211 
7212 	/* IEEE80211_STA_NOTEXIST -> IEEE80211_STA_NONE: Add new station to driver */
7213 	if (old_state == IEEE80211_STA_NOTEXIST &&
7214 	    new_state == IEEE80211_STA_NONE) {
7215 		ret = ath12k_mac_station_add(ar, arvif, arsta);
7216 		if (ret)
7217 			ath12k_warn(ab, "Failed to add station: %pM for VDEV: %d\n",
7218 				    arsta->addr, arvif->vdev_id);
7219 
7220 	/* IEEE80211_STA_AUTH -> IEEE80211_STA_ASSOC: Send station assoc command for
7221 	 * peer associated to AP/Mesh/ADHOC vif type.
7222 	 */
7223 	} else if (old_state == IEEE80211_STA_AUTH &&
7224 		   new_state == IEEE80211_STA_ASSOC &&
7225 		   (vif->type == NL80211_IFTYPE_AP ||
7226 		    vif->type == NL80211_IFTYPE_MESH_POINT ||
7227 		    vif->type == NL80211_IFTYPE_ADHOC)) {
7228 		ret = ath12k_mac_station_assoc(ar, arvif, arsta, false);
7229 		if (ret)
7230 			ath12k_warn(ab, "Failed to associate station: %pM\n",
7231 				    arsta->addr);
7232 
7233 	/* IEEE80211_STA_ASSOC -> IEEE80211_STA_AUTHORIZED: set peer status as
7234 	 * authorized
7235 	 */
7236 	} else if (old_state == IEEE80211_STA_ASSOC &&
7237 		   new_state == IEEE80211_STA_AUTHORIZED) {
7238 		ret = ath12k_mac_station_authorize(ar, arvif, arsta);
7239 		if (ret) {
7240 			ath12k_warn(ab, "Failed to authorize station: %pM\n",
7241 				    arsta->addr);
7242 			goto exit;
7243 		}
7244 
7245 		if (ath12k_wmi_supports_6ghz_cc_ext(ar) &&
7246 		    arvif->ahvif->vdev_type == WMI_VDEV_TYPE_STA) {
7247 			link_conf = ath12k_mac_get_link_bss_conf(arvif);
7248 			reg_info = ab->reg_info[ar->pdev_idx];
7249 			ath12k_dbg(ab, ATH12K_DBG_MAC, "connection done, update reg rules\n");
7250 			ath12k_hw_to_ah(hw)->regd_updated = false;
7251 			ath12k_reg_handle_chan_list(ab, reg_info, arvif->ahvif->vdev_type,
7252 						    link_conf->power_type);
7253 		}
7254 
7255 	/* IEEE80211_STA_AUTHORIZED -> IEEE80211_STA_ASSOC: station may be in removal,
7256 	 * deauthorize it.
7257 	 */
7258 	} else if (old_state == IEEE80211_STA_AUTHORIZED &&
7259 		   new_state == IEEE80211_STA_ASSOC) {
7260 		ath12k_mac_station_unauthorize(ar, arvif, arsta);
7261 
7262 	/* IEEE80211_STA_ASSOC -> IEEE80211_STA_AUTH: disassoc peer connected to
7263 	 * AP/mesh/ADHOC vif type.
7264 	 */
7265 	} else if (old_state == IEEE80211_STA_ASSOC &&
7266 		   new_state == IEEE80211_STA_AUTH &&
7267 		   (vif->type == NL80211_IFTYPE_AP ||
7268 		    vif->type == NL80211_IFTYPE_MESH_POINT ||
7269 		    vif->type == NL80211_IFTYPE_ADHOC)) {
7270 		ret = ath12k_mac_station_disassoc(ar, arvif, arsta);
7271 		if (ret)
7272 			ath12k_warn(ab, "Failed to disassociate station: %pM\n",
7273 				    arsta->addr);
7274 	}
7275 
7276 exit:
7277 	return ret;
7278 }
7279 
7280 static bool ath12k_mac_is_freq_on_mac(struct ath12k_hw_mode_freq_range_arg *freq_range,
7281 				      u32 freq, u8 mac_id)
7282 {
7283 	return (freq >= freq_range[mac_id].low_2ghz_freq &&
7284 		freq <= freq_range[mac_id].high_2ghz_freq) ||
7285 	       (freq >= freq_range[mac_id].low_5ghz_freq &&
7286 		freq <= freq_range[mac_id].high_5ghz_freq);
7287 }
7288 
7289 static bool
7290 ath12k_mac_2_freq_same_mac_in_freq_range(struct ath12k_base *ab,
7291 					 struct ath12k_hw_mode_freq_range_arg *freq_range,
7292 					 u32 freq_link1, u32 freq_link2)
7293 {
7294 	u8 i;
7295 
7296 	for (i = 0; i < MAX_RADIOS; i++) {
7297 		if (ath12k_mac_is_freq_on_mac(freq_range, freq_link1, i) &&
7298 		    ath12k_mac_is_freq_on_mac(freq_range, freq_link2, i))
7299 			return true;
7300 	}
7301 
7302 	return false;
7303 }
7304 
7305 static bool ath12k_mac_is_hw_dbs_capable(struct ath12k_base *ab)
7306 {
7307 	return test_bit(WMI_TLV_SERVICE_DUAL_BAND_SIMULTANEOUS_SUPPORT,
7308 			ab->wmi_ab.svc_map) &&
7309 	       ab->wmi_ab.hw_mode_info.support_dbs;
7310 }
7311 
7312 static bool ath12k_mac_2_freq_same_mac_in_dbs(struct ath12k_base *ab,
7313 					      u32 freq_link1, u32 freq_link2)
7314 {
7315 	struct ath12k_hw_mode_freq_range_arg *freq_range;
7316 
7317 	if (!ath12k_mac_is_hw_dbs_capable(ab))
7318 		return true;
7319 
7320 	freq_range = ab->wmi_ab.hw_mode_info.freq_range_caps[ATH12K_HW_MODE_DBS];
7321 	return ath12k_mac_2_freq_same_mac_in_freq_range(ab, freq_range,
7322 							freq_link1, freq_link2);
7323 }
7324 
7325 static bool ath12k_mac_is_hw_sbs_capable(struct ath12k_base *ab)
7326 {
7327 	return test_bit(WMI_TLV_SERVICE_DUAL_BAND_SIMULTANEOUS_SUPPORT,
7328 			ab->wmi_ab.svc_map) &&
7329 	       ab->wmi_ab.hw_mode_info.support_sbs;
7330 }
7331 
7332 static bool ath12k_mac_2_freq_same_mac_in_sbs(struct ath12k_base *ab,
7333 					      u32 freq_link1, u32 freq_link2)
7334 {
7335 	struct ath12k_hw_mode_info *info = &ab->wmi_ab.hw_mode_info;
7336 	struct ath12k_hw_mode_freq_range_arg *sbs_uppr_share;
7337 	struct ath12k_hw_mode_freq_range_arg *sbs_low_share;
7338 	struct ath12k_hw_mode_freq_range_arg *sbs_range;
7339 
7340 	if (!ath12k_mac_is_hw_sbs_capable(ab))
7341 		return true;
7342 
7343 	if (ab->wmi_ab.sbs_lower_band_end_freq) {
7344 		sbs_uppr_share = info->freq_range_caps[ATH12K_HW_MODE_SBS_UPPER_SHARE];
7345 		sbs_low_share = info->freq_range_caps[ATH12K_HW_MODE_SBS_LOWER_SHARE];
7346 
7347 		return ath12k_mac_2_freq_same_mac_in_freq_range(ab, sbs_low_share,
7348 								freq_link1, freq_link2) ||
7349 		       ath12k_mac_2_freq_same_mac_in_freq_range(ab, sbs_uppr_share,
7350 								freq_link1, freq_link2);
7351 	}
7352 
7353 	sbs_range = info->freq_range_caps[ATH12K_HW_MODE_SBS];
7354 	return ath12k_mac_2_freq_same_mac_in_freq_range(ab, sbs_range,
7355 							freq_link1, freq_link2);
7356 }
7357 
7358 static bool ath12k_mac_freqs_on_same_mac(struct ath12k_base *ab,
7359 					 u32 freq_link1, u32 freq_link2)
7360 {
7361 	return ath12k_mac_2_freq_same_mac_in_dbs(ab, freq_link1, freq_link2) &&
7362 	       ath12k_mac_2_freq_same_mac_in_sbs(ab, freq_link1, freq_link2);
7363 }
7364 
7365 static int ath12k_mac_mlo_sta_set_link_active(struct ath12k_base *ab,
7366 					      enum wmi_mlo_link_force_reason reason,
7367 					      enum wmi_mlo_link_force_mode mode,
7368 					      u8 *mlo_vdev_id_lst,
7369 					      u8 num_mlo_vdev,
7370 					      u8 *mlo_inactive_vdev_lst,
7371 					      u8 num_mlo_inactive_vdev)
7372 {
7373 	struct wmi_mlo_link_set_active_arg param = {};
7374 	u32 entry_idx, entry_offset, vdev_idx;
7375 	u8 vdev_id;
7376 
7377 	param.reason = reason;
7378 	param.force_mode = mode;
7379 
7380 	for (vdev_idx = 0; vdev_idx < num_mlo_vdev; vdev_idx++) {
7381 		vdev_id = mlo_vdev_id_lst[vdev_idx];
7382 		entry_idx = vdev_id / 32;
7383 		entry_offset = vdev_id % 32;
7384 		if (entry_idx >= WMI_MLO_LINK_NUM_SZ) {
7385 			ath12k_warn(ab, "Invalid entry_idx %d num_mlo_vdev %d vdev %d",
7386 				    entry_idx, num_mlo_vdev, vdev_id);
7387 			return -EINVAL;
7388 		}
7389 		param.vdev_bitmap[entry_idx] |= 1 << entry_offset;
7390 		/* update entry number if entry index changed */
7391 		if (param.num_vdev_bitmap < entry_idx + 1)
7392 			param.num_vdev_bitmap = entry_idx + 1;
7393 	}
7394 
7395 	ath12k_dbg(ab, ATH12K_DBG_MAC,
7396 		   "num_vdev_bitmap %d vdev_bitmap[0] = 0x%x, vdev_bitmap[1] = 0x%x",
7397 		   param.num_vdev_bitmap, param.vdev_bitmap[0], param.vdev_bitmap[1]);
7398 
7399 	if (mode == WMI_MLO_LINK_FORCE_MODE_ACTIVE_INACTIVE) {
7400 		for (vdev_idx = 0; vdev_idx < num_mlo_inactive_vdev; vdev_idx++) {
7401 			vdev_id = mlo_inactive_vdev_lst[vdev_idx];
7402 			entry_idx = vdev_id / 32;
7403 			entry_offset = vdev_id % 32;
7404 			if (entry_idx >= WMI_MLO_LINK_NUM_SZ) {
7405 				ath12k_warn(ab, "Invalid entry_idx %d num_mlo_vdev %d vdev %d",
7406 					    entry_idx, num_mlo_inactive_vdev, vdev_id);
7407 				return -EINVAL;
7408 			}
7409 			param.inactive_vdev_bitmap[entry_idx] |= 1 << entry_offset;
7410 			/* update entry number if entry index changed */
7411 			if (param.num_inactive_vdev_bitmap < entry_idx + 1)
7412 				param.num_inactive_vdev_bitmap = entry_idx + 1;
7413 		}
7414 
7415 		ath12k_dbg(ab, ATH12K_DBG_MAC,
7416 			   "num_vdev_bitmap %d inactive_vdev_bitmap[0] = 0x%x, inactive_vdev_bitmap[1] = 0x%x",
7417 			   param.num_inactive_vdev_bitmap,
7418 			   param.inactive_vdev_bitmap[0],
7419 			   param.inactive_vdev_bitmap[1]);
7420 	}
7421 
7422 	if (mode == WMI_MLO_LINK_FORCE_MODE_ACTIVE_LINK_NUM ||
7423 	    mode == WMI_MLO_LINK_FORCE_MODE_INACTIVE_LINK_NUM) {
7424 		param.num_link_entry = 1;
7425 		param.link_num[0].num_of_link = num_mlo_vdev - 1;
7426 	}
7427 
7428 	return ath12k_wmi_send_mlo_link_set_active_cmd(ab, &param);
7429 }
7430 
7431 static int ath12k_mac_mlo_sta_update_link_active(struct ath12k_base *ab,
7432 						 struct ieee80211_hw *hw,
7433 						 struct ath12k_vif *ahvif)
7434 {
7435 	u8 mlo_vdev_id_lst[IEEE80211_MLD_MAX_NUM_LINKS] = {};
7436 	u32 mlo_freq_list[IEEE80211_MLD_MAX_NUM_LINKS] = {};
7437 	unsigned long links = ahvif->links_map;
7438 	enum wmi_mlo_link_force_reason reason;
7439 	struct ieee80211_chanctx_conf *conf;
7440 	enum wmi_mlo_link_force_mode mode;
7441 	struct ieee80211_bss_conf *info;
7442 	struct ath12k_link_vif *arvif;
7443 	u8 num_mlo_vdev = 0;
7444 	u8 link_id;
7445 
7446 	for_each_set_bit(link_id, &links, IEEE80211_MLD_MAX_NUM_LINKS) {
7447 		arvif = wiphy_dereference(hw->wiphy, ahvif->link[link_id]);
7448 		/* make sure vdev is created on this device */
7449 		if (!arvif || !arvif->is_created || arvif->ar->ab != ab)
7450 			continue;
7451 
7452 		info = ath12k_mac_get_link_bss_conf(arvif);
7453 		conf = wiphy_dereference(hw->wiphy, info->chanctx_conf);
7454 		mlo_freq_list[num_mlo_vdev] = conf->def.chan->center_freq;
7455 
7456 		mlo_vdev_id_lst[num_mlo_vdev] = arvif->vdev_id;
7457 		num_mlo_vdev++;
7458 	}
7459 
7460 	/* It is not allowed to activate more links than a single device
7461 	 * supported. Something goes wrong if we reach here.
7462 	 */
7463 	if (num_mlo_vdev > ATH12K_NUM_MAX_ACTIVE_LINKS_PER_DEVICE) {
7464 		WARN_ON_ONCE(1);
7465 		return -EINVAL;
7466 	}
7467 
7468 	/* if 2 links are established and both link channels fall on the
7469 	 * same hardware MAC, send command to firmware to deactivate one
7470 	 * of them.
7471 	 */
7472 	if (num_mlo_vdev == 2 &&
7473 	    ath12k_mac_freqs_on_same_mac(ab, mlo_freq_list[0],
7474 					 mlo_freq_list[1])) {
7475 		mode = WMI_MLO_LINK_FORCE_MODE_INACTIVE_LINK_NUM;
7476 		reason = WMI_MLO_LINK_FORCE_REASON_NEW_CONNECT;
7477 		return ath12k_mac_mlo_sta_set_link_active(ab, reason, mode,
7478 							  mlo_vdev_id_lst, num_mlo_vdev,
7479 							  NULL, 0);
7480 	}
7481 
7482 	return 0;
7483 }
7484 
7485 static bool ath12k_mac_are_sbs_chan(struct ath12k_base *ab, u32 freq_1, u32 freq_2)
7486 {
7487 	if (!ath12k_mac_is_hw_sbs_capable(ab))
7488 		return false;
7489 
7490 	if (ath12k_is_2ghz_channel_freq(freq_1) ||
7491 	    ath12k_is_2ghz_channel_freq(freq_2))
7492 		return false;
7493 
7494 	return !ath12k_mac_2_freq_same_mac_in_sbs(ab, freq_1, freq_2);
7495 }
7496 
7497 static bool ath12k_mac_are_dbs_chan(struct ath12k_base *ab, u32 freq_1, u32 freq_2)
7498 {
7499 	if (!ath12k_mac_is_hw_dbs_capable(ab))
7500 		return false;
7501 
7502 	return !ath12k_mac_2_freq_same_mac_in_dbs(ab, freq_1, freq_2);
7503 }
7504 
7505 static int ath12k_mac_select_links(struct ath12k_base *ab,
7506 				   struct ieee80211_vif *vif,
7507 				   struct ieee80211_hw *hw,
7508 				   u16 *selected_links)
7509 {
7510 	unsigned long useful_links = ieee80211_vif_usable_links(vif);
7511 	struct ath12k_vif *ahvif = ath12k_vif_to_ahvif(vif);
7512 	u8 num_useful_links = hweight_long(useful_links);
7513 	struct ieee80211_chanctx_conf *chanctx;
7514 	struct ath12k_link_vif *assoc_arvif;
7515 	u32 assoc_link_freq, partner_freq;
7516 	u16 sbs_links = 0, dbs_links = 0;
7517 	struct ieee80211_bss_conf *info;
7518 	struct ieee80211_channel *chan;
7519 	struct ieee80211_sta *sta;
7520 	struct ath12k_sta *ahsta;
7521 	u8 link_id;
7522 
7523 	/* activate all useful links if less than max supported */
7524 	if (num_useful_links <= ATH12K_NUM_MAX_ACTIVE_LINKS_PER_DEVICE) {
7525 		*selected_links = useful_links;
7526 		return 0;
7527 	}
7528 
7529 	/* only in station mode we can get here, so it's safe
7530 	 * to use ap_addr
7531 	 */
7532 	rcu_read_lock();
7533 	sta = ieee80211_find_sta(vif, vif->cfg.ap_addr);
7534 	if (!sta) {
7535 		rcu_read_unlock();
7536 		ath12k_warn(ab, "failed to find sta with addr %pM\n", vif->cfg.ap_addr);
7537 		return -EINVAL;
7538 	}
7539 
7540 	ahsta = ath12k_sta_to_ahsta(sta);
7541 	assoc_arvif = wiphy_dereference(hw->wiphy, ahvif->link[ahsta->assoc_link_id]);
7542 	info = ath12k_mac_get_link_bss_conf(assoc_arvif);
7543 	chanctx = rcu_dereference(info->chanctx_conf);
7544 	assoc_link_freq = chanctx->def.chan->center_freq;
7545 	rcu_read_unlock();
7546 	ath12k_dbg(ab, ATH12K_DBG_MAC, "assoc link %u freq %u\n",
7547 		   assoc_arvif->link_id, assoc_link_freq);
7548 
7549 	/* assoc link is already activated and has to be kept active,
7550 	 * only need to select a partner link from others.
7551 	 */
7552 	useful_links &= ~BIT(assoc_arvif->link_id);
7553 	for_each_set_bit(link_id, &useful_links, IEEE80211_MLD_MAX_NUM_LINKS) {
7554 		info = wiphy_dereference(hw->wiphy, vif->link_conf[link_id]);
7555 		if (!info) {
7556 			ath12k_warn(ab, "failed to get link info for link: %u\n",
7557 				    link_id);
7558 			return -ENOLINK;
7559 		}
7560 
7561 		chan = info->chanreq.oper.chan;
7562 		if (!chan) {
7563 			ath12k_warn(ab, "failed to get chan for link: %u\n", link_id);
7564 			return -EINVAL;
7565 		}
7566 
7567 		partner_freq = chan->center_freq;
7568 		if (ath12k_mac_are_sbs_chan(ab, assoc_link_freq, partner_freq)) {
7569 			sbs_links |= BIT(link_id);
7570 			ath12k_dbg(ab, ATH12K_DBG_MAC, "new SBS link %u freq %u\n",
7571 				   link_id, partner_freq);
7572 			continue;
7573 		}
7574 
7575 		if (ath12k_mac_are_dbs_chan(ab, assoc_link_freq, partner_freq)) {
7576 			dbs_links |= BIT(link_id);
7577 			ath12k_dbg(ab, ATH12K_DBG_MAC, "new DBS link %u freq %u\n",
7578 				   link_id, partner_freq);
7579 			continue;
7580 		}
7581 
7582 		ath12k_dbg(ab, ATH12K_DBG_MAC, "non DBS/SBS link %u freq %u\n",
7583 			   link_id, partner_freq);
7584 	}
7585 
7586 	/* choose the first candidate no matter how many is in the list */
7587 	if (sbs_links)
7588 		link_id = __ffs(sbs_links);
7589 	else if (dbs_links)
7590 		link_id = __ffs(dbs_links);
7591 	else
7592 		link_id = ffs(useful_links) - 1;
7593 
7594 	ath12k_dbg(ab, ATH12K_DBG_MAC, "select partner link %u\n", link_id);
7595 
7596 	*selected_links = BIT(assoc_arvif->link_id) | BIT(link_id);
7597 
7598 	return 0;
7599 }
7600 
7601 int ath12k_mac_op_sta_state(struct ieee80211_hw *hw,
7602 			    struct ieee80211_vif *vif,
7603 			    struct ieee80211_sta *sta,
7604 			    enum ieee80211_sta_state old_state,
7605 			    enum ieee80211_sta_state new_state)
7606 {
7607 	struct ath12k_vif *ahvif = ath12k_vif_to_ahvif(vif);
7608 	struct ath12k_sta *ahsta = ath12k_sta_to_ahsta(sta);
7609 	struct ath12k_hw *ah = ath12k_hw_to_ah(hw);
7610 	struct ath12k_base *prev_ab = NULL, *ab;
7611 	struct ath12k_link_vif *arvif;
7612 	struct ath12k_link_sta *arsta;
7613 	unsigned long valid_links;
7614 	u16 selected_links = 0;
7615 	u8 link_id = 0, i;
7616 	struct ath12k *ar;
7617 	int ret = -EINVAL;
7618 	struct ath12k_dp_peer_create_params dp_params = {};
7619 
7620 	lockdep_assert_wiphy(hw->wiphy);
7621 
7622 	if (ieee80211_vif_is_mld(vif) && sta->valid_links) {
7623 		WARN_ON(!sta->mlo && hweight16(sta->valid_links) != 1);
7624 		link_id = ffs(sta->valid_links) - 1;
7625 	}
7626 
7627 	/* IEEE80211_STA_NOTEXIST -> IEEE80211_STA_NONE:
7628 	 * New station add received. If this is a ML station then
7629 	 * ahsta->links_map will be zero and sta->valid_links will be 1.
7630 	 * Assign default link to the first link sta.
7631 	 */
7632 	if (old_state == IEEE80211_STA_NOTEXIST &&
7633 	    new_state == IEEE80211_STA_NONE) {
7634 		memset(ahsta, 0, sizeof(*ahsta));
7635 
7636 		arsta = &ahsta->deflink;
7637 
7638 		/* ML sta */
7639 		if (sta->mlo && !ahsta->links_map &&
7640 		    (hweight16(sta->valid_links) == 1)) {
7641 			ahsta->ml_peer_id = ath12k_peer_ml_alloc(ah);
7642 			if (ahsta->ml_peer_id == ATH12K_MLO_PEER_ID_INVALID) {
7643 				ath12k_hw_warn(ah, "unable to allocate ML peer id for sta %pM",
7644 					       sta->addr);
7645 				goto exit;
7646 			}
7647 
7648 			dp_params.is_mlo = true;
7649 			dp_params.peer_id = ahsta->ml_peer_id | ATH12K_PEER_ML_ID_VALID;
7650 		}
7651 
7652 		dp_params.sta = sta;
7653 
7654 		if (vif->type == NL80211_IFTYPE_AP)
7655 			dp_params.ucast_ra_only = true;
7656 
7657 		ret = ath12k_dp_peer_create(&ah->dp_hw, sta->addr, &dp_params);
7658 		if (ret) {
7659 			ath12k_hw_warn(ah, "unable to create ath12k_dp_peer for sta %pM, ret: %d",
7660 				       sta->addr, ret);
7661 
7662 			goto ml_peer_id_clear;
7663 		}
7664 
7665 		ret = ath12k_mac_assign_link_sta(ah, ahsta, arsta, ahvif,
7666 						 link_id);
7667 		if (ret) {
7668 			ath12k_hw_warn(ah, "unable assign link %d for sta %pM",
7669 				       link_id, sta->addr);
7670 			goto peer_delete;
7671 		}
7672 
7673 		/* above arsta will get memset, hence do this after assign
7674 		 * link sta
7675 		 */
7676 		if (sta->mlo) {
7677 			/* For station mode, arvif->is_sta_assoc_link has been set when
7678 			 * vdev starts. Make sure the arvif/arsta pair have same setting
7679 			 */
7680 			if (vif->type == NL80211_IFTYPE_STATION &&
7681 			    !arsta->arvif->is_sta_assoc_link) {
7682 				ath12k_hw_warn(ah, "failed to verify assoc link setting with link id %u\n",
7683 					       link_id);
7684 				ret = -EINVAL;
7685 				goto exit;
7686 			}
7687 
7688 			arsta->is_assoc_link = true;
7689 			ahsta->assoc_link_id = link_id;
7690 		}
7691 	}
7692 
7693 	/* In the ML station scenario, activate all partner links once the
7694 	 * client is transitioning to the associated state.
7695 	 *
7696 	 * FIXME: Ideally, this activation should occur when the client
7697 	 * transitions to the authorized state. However, there are some
7698 	 * issues with handling this in the firmware. Until the firmware
7699 	 * can manage it properly, activate the links when the client is
7700 	 * about to move to the associated state.
7701 	 */
7702 	if (ieee80211_vif_is_mld(vif) && vif->type == NL80211_IFTYPE_STATION &&
7703 	    old_state == IEEE80211_STA_AUTH && new_state == IEEE80211_STA_ASSOC) {
7704 		/* TODO: for now only do link selection for single device
7705 		 * MLO case. Other cases would be handled in the future.
7706 		 */
7707 		ab = ah->radio[0].ab;
7708 		if (ab->ag->num_devices == 1) {
7709 			ret = ath12k_mac_select_links(ab, vif, hw, &selected_links);
7710 			if (ret) {
7711 				ath12k_warn(ab,
7712 					    "failed to get selected links: %d\n", ret);
7713 				goto exit;
7714 			}
7715 		} else {
7716 			selected_links = ieee80211_vif_usable_links(vif);
7717 		}
7718 
7719 		ieee80211_set_active_links(vif, selected_links);
7720 	}
7721 
7722 	/* Handle all the other state transitions in generic way */
7723 	valid_links = ahsta->links_map;
7724 	for_each_set_bit(link_id, &valid_links, IEEE80211_MLD_MAX_NUM_LINKS) {
7725 		arvif = wiphy_dereference(hw->wiphy, ahvif->link[link_id]);
7726 		arsta = wiphy_dereference(hw->wiphy, ahsta->link[link_id]);
7727 		/* some assumptions went wrong! */
7728 		if (WARN_ON(!arvif || !arsta))
7729 			continue;
7730 
7731 		/* vdev might be in deleted */
7732 		if (WARN_ON(!arvif->ar))
7733 			continue;
7734 
7735 		ret = ath12k_mac_handle_link_sta_state(hw, arvif, arsta,
7736 						       old_state, new_state);
7737 		if (ret) {
7738 			ath12k_hw_warn(ah, "unable to move link sta %d of sta %pM from state %d to %d",
7739 				       link_id, arsta->addr, old_state, new_state);
7740 
7741 			if (old_state == IEEE80211_STA_NOTEXIST &&
7742 			    new_state == IEEE80211_STA_NONE)
7743 				goto peer_delete;
7744 			else
7745 				goto exit;
7746 		}
7747 	}
7748 
7749 	if (ieee80211_vif_is_mld(vif) && vif->type == NL80211_IFTYPE_STATION &&
7750 	    old_state == IEEE80211_STA_ASSOC && new_state == IEEE80211_STA_AUTHORIZED) {
7751 		for_each_ar(ah, ar, i) {
7752 			ab = ar->ab;
7753 			if (prev_ab == ab)
7754 				continue;
7755 
7756 			ret = ath12k_mac_mlo_sta_update_link_active(ab, hw, ahvif);
7757 			if (ret) {
7758 				ath12k_warn(ab,
7759 					    "failed to update link active state on connect %d\n",
7760 					    ret);
7761 				goto exit;
7762 			}
7763 
7764 			prev_ab = ab;
7765 		}
7766 	}
7767 	/* IEEE80211_STA_NONE -> IEEE80211_STA_NOTEXIST:
7768 	 * Remove the station from driver (handle ML sta here since that
7769 	 * needs special handling. Normal sta will be handled in generic
7770 	 * handler below
7771 	 */
7772 	if (old_state == IEEE80211_STA_NONE &&
7773 	    new_state == IEEE80211_STA_NOTEXIST) {
7774 		if (sta->mlo)
7775 			ath12k_mac_ml_station_remove(ahvif, ahsta);
7776 
7777 		ath12k_dp_peer_delete(&ah->dp_hw, sta->addr, sta);
7778 	}
7779 
7780 	ret = 0;
7781 	goto exit;
7782 
7783 peer_delete:
7784 	ath12k_dp_peer_delete(&ah->dp_hw, sta->addr, sta);
7785 ml_peer_id_clear:
7786 	if (sta->mlo) {
7787 		clear_bit(ahsta->ml_peer_id, ah->free_ml_peer_id_map);
7788 		ahsta->ml_peer_id = ATH12K_MLO_PEER_ID_INVALID;
7789 	}
7790 exit:
7791 	/* update the state if everything went well */
7792 	if (!ret)
7793 		ahsta->state = new_state;
7794 
7795 	return ret;
7796 }
7797 EXPORT_SYMBOL(ath12k_mac_op_sta_state);
7798 
7799 int ath12k_mac_op_sta_set_txpwr(struct ieee80211_hw *hw,
7800 				struct ieee80211_vif *vif,
7801 				struct ieee80211_sta *sta)
7802 {
7803 	struct ath12k_sta *ahsta = ath12k_sta_to_ahsta(sta);
7804 	struct ath12k *ar;
7805 	struct ath12k_vif *ahvif = ath12k_vif_to_ahvif(vif);
7806 	struct ath12k_link_vif *arvif;
7807 	struct ath12k_link_sta *arsta;
7808 	u8 link_id;
7809 	int ret;
7810 	s16 txpwr;
7811 
7812 	lockdep_assert_wiphy(hw->wiphy);
7813 
7814 	/* TODO: use link id from mac80211 once that's implemented */
7815 	link_id = 0;
7816 
7817 	arvif = wiphy_dereference(hw->wiphy, ahvif->link[link_id]);
7818 	arsta = wiphy_dereference(hw->wiphy, ahsta->link[link_id]);
7819 
7820 	if (sta->deflink.txpwr.type == NL80211_TX_POWER_AUTOMATIC) {
7821 		txpwr = 0;
7822 	} else {
7823 		txpwr = sta->deflink.txpwr.power;
7824 		if (!txpwr) {
7825 			ret = -EINVAL;
7826 			goto out;
7827 		}
7828 	}
7829 
7830 	if (txpwr > ATH12K_TX_POWER_MAX_VAL || txpwr < ATH12K_TX_POWER_MIN_VAL) {
7831 		ret = -EINVAL;
7832 		goto out;
7833 	}
7834 
7835 	ar = arvif->ar;
7836 
7837 	ret = ath12k_wmi_set_peer_param(ar, arsta->addr, arvif->vdev_id,
7838 					WMI_PEER_USE_FIXED_PWR, txpwr);
7839 	if (ret) {
7840 		ath12k_warn(ar->ab, "failed to set tx power for station ret: %d\n",
7841 			    ret);
7842 		goto out;
7843 	}
7844 
7845 out:
7846 	return ret;
7847 }
7848 EXPORT_SYMBOL(ath12k_mac_op_sta_set_txpwr);
7849 
7850 void ath12k_mac_op_link_sta_rc_update(struct ieee80211_hw *hw,
7851 				      struct ieee80211_vif *vif,
7852 				      struct ieee80211_link_sta *link_sta,
7853 				      u32 changed)
7854 {
7855 	struct ieee80211_sta *sta = link_sta->sta;
7856 	struct ath12k *ar;
7857 	struct ath12k_sta *ahsta = ath12k_sta_to_ahsta(sta);
7858 	struct ath12k_vif *ahvif = ath12k_vif_to_ahvif(vif);
7859 	struct ath12k_hw *ah = ath12k_hw_to_ah(hw);
7860 	struct ath12k_link_sta *arsta;
7861 	struct ath12k_link_vif *arvif;
7862 	struct ath12k_dp_link_peer *peer;
7863 	u32 bw, smps;
7864 	struct ath12k_dp *dp;
7865 
7866 	rcu_read_lock();
7867 	arvif = rcu_dereference(ahvif->link[link_sta->link_id]);
7868 	if (!arvif) {
7869 		ath12k_hw_warn(ah, "mac sta rc update failed to fetch link vif on link id %u for peer %pM\n",
7870 			       link_sta->link_id, sta->addr);
7871 		rcu_read_unlock();
7872 		return;
7873 	}
7874 
7875 	ar = arvif->ar;
7876 	dp = ath12k_ab_to_dp(ar->ab);
7877 
7878 	arsta = rcu_dereference(ahsta->link[link_sta->link_id]);
7879 	if (!arsta) {
7880 		rcu_read_unlock();
7881 		ath12k_warn(ar->ab, "mac sta rc update failed to fetch link sta on link id %u for peer %pM\n",
7882 			    link_sta->link_id, sta->addr);
7883 		return;
7884 	}
7885 	spin_lock_bh(&dp->dp_lock);
7886 
7887 	peer = ath12k_dp_link_peer_find_by_vdev_and_addr(dp, arvif->vdev_id,
7888 							 arsta->addr);
7889 	if (!peer) {
7890 		spin_unlock_bh(&dp->dp_lock);
7891 		rcu_read_unlock();
7892 		ath12k_warn(ar->ab, "mac sta rc update failed to find peer %pM on vdev %i\n",
7893 			    arsta->addr, arvif->vdev_id);
7894 		return;
7895 	}
7896 
7897 	spin_unlock_bh(&dp->dp_lock);
7898 
7899 	if (arsta->link_id >= IEEE80211_MLD_MAX_NUM_LINKS) {
7900 		rcu_read_unlock();
7901 		return;
7902 	}
7903 
7904 	link_sta = rcu_dereference(sta->link[arsta->link_id]);
7905 	if (!link_sta) {
7906 		rcu_read_unlock();
7907 		ath12k_warn(ar->ab, "unable to access link sta in rc update for sta %pM link %u\n",
7908 			    sta->addr, arsta->link_id);
7909 		return;
7910 	}
7911 
7912 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
7913 		   "mac sta rc update for %pM changed %08x bw %d nss %d smps %d\n",
7914 		   arsta->addr, changed, link_sta->bandwidth, link_sta->rx_nss,
7915 		   link_sta->smps_mode);
7916 
7917 	spin_lock_bh(&ar->data_lock);
7918 
7919 	if (changed & IEEE80211_RC_BW_CHANGED) {
7920 		bw = ath12k_mac_ieee80211_sta_bw_to_wmi(ar, link_sta);
7921 		arsta->bw_prev = arsta->bw;
7922 		arsta->bw = bw;
7923 	}
7924 
7925 	if (changed & IEEE80211_RC_NSS_CHANGED)
7926 		arsta->nss = link_sta->rx_nss;
7927 
7928 	if (changed & IEEE80211_RC_SMPS_CHANGED) {
7929 		smps = WMI_PEER_SMPS_PS_NONE;
7930 
7931 		switch (link_sta->smps_mode) {
7932 		case IEEE80211_SMPS_AUTOMATIC:
7933 		case IEEE80211_SMPS_OFF:
7934 			smps = WMI_PEER_SMPS_PS_NONE;
7935 			break;
7936 		case IEEE80211_SMPS_STATIC:
7937 			smps = WMI_PEER_SMPS_STATIC;
7938 			break;
7939 		case IEEE80211_SMPS_DYNAMIC:
7940 			smps = WMI_PEER_SMPS_DYNAMIC;
7941 			break;
7942 		default:
7943 			ath12k_warn(ar->ab, "Invalid smps %d in sta rc update for %pM link %u\n",
7944 				    link_sta->smps_mode, arsta->addr, link_sta->link_id);
7945 			smps = WMI_PEER_SMPS_PS_NONE;
7946 			break;
7947 		}
7948 
7949 		arsta->smps = smps;
7950 	}
7951 
7952 	arsta->changed |= changed;
7953 
7954 	spin_unlock_bh(&ar->data_lock);
7955 
7956 	wiphy_work_queue(hw->wiphy, &arsta->update_wk);
7957 
7958 	rcu_read_unlock();
7959 }
7960 EXPORT_SYMBOL(ath12k_mac_op_link_sta_rc_update);
7961 
7962 static struct ath12k_link_sta *ath12k_mac_alloc_assign_link_sta(struct ath12k_hw *ah,
7963 								struct ath12k_sta *ahsta,
7964 								struct ath12k_vif *ahvif,
7965 								u8 link_id)
7966 {
7967 	struct ath12k_link_sta *arsta;
7968 	int ret;
7969 
7970 	lockdep_assert_wiphy(ah->hw->wiphy);
7971 
7972 	if (link_id >= IEEE80211_MLD_MAX_NUM_LINKS)
7973 		return NULL;
7974 
7975 	arsta = wiphy_dereference(ah->hw->wiphy, ahsta->link[link_id]);
7976 	if (arsta)
7977 		return NULL;
7978 
7979 	arsta = kmalloc_obj(*arsta);
7980 	if (!arsta)
7981 		return NULL;
7982 
7983 	ret = ath12k_mac_assign_link_sta(ah, ahsta, arsta, ahvif, link_id);
7984 	if (ret) {
7985 		kfree(arsta);
7986 		return NULL;
7987 	}
7988 
7989 	return arsta;
7990 }
7991 
7992 int ath12k_mac_op_change_sta_links(struct ieee80211_hw *hw,
7993 				   struct ieee80211_vif *vif,
7994 				   struct ieee80211_sta *sta,
7995 				   u16 old_links, u16 new_links)
7996 {
7997 	struct ath12k_vif *ahvif = ath12k_vif_to_ahvif(vif);
7998 	struct ath12k_sta *ahsta = ath12k_sta_to_ahsta(sta);
7999 	struct ath12k_hw *ah = hw->priv;
8000 	struct ath12k_link_vif *arvif;
8001 	struct ath12k_link_sta *arsta;
8002 	unsigned long valid_links;
8003 	struct ath12k *ar;
8004 	u8 link_id;
8005 	int ret;
8006 
8007 	lockdep_assert_wiphy(hw->wiphy);
8008 
8009 	if (!sta->valid_links)
8010 		return -EINVAL;
8011 
8012 	/* Firmware does not support removal of one of link stas. All sta
8013 	 * would be removed during ML STA delete in sta_state(), hence link
8014 	 * sta removal is not handled here.
8015 	 */
8016 	if (new_links < old_links)
8017 		return 0;
8018 
8019 	if (ahsta->ml_peer_id == ATH12K_MLO_PEER_ID_INVALID) {
8020 		ath12k_hw_warn(ah, "unable to add link for ml sta %pM", sta->addr);
8021 		return -EINVAL;
8022 	}
8023 
8024 	/* this op is expected only after initial sta insertion with default link */
8025 	if (WARN_ON(ahsta->links_map == 0))
8026 		return -EINVAL;
8027 
8028 	valid_links = new_links;
8029 	for_each_set_bit(link_id, &valid_links, IEEE80211_MLD_MAX_NUM_LINKS) {
8030 		if (ahsta->links_map & BIT(link_id))
8031 			continue;
8032 
8033 		arvif = wiphy_dereference(hw->wiphy, ahvif->link[link_id]);
8034 		arsta = ath12k_mac_alloc_assign_link_sta(ah, ahsta, ahvif, link_id);
8035 
8036 		if (!arvif || !arsta) {
8037 			ath12k_hw_warn(ah, "Failed to alloc/assign link sta");
8038 			continue;
8039 		}
8040 
8041 		ar = arvif->ar;
8042 		if (!ar)
8043 			continue;
8044 
8045 		ret = ath12k_mac_station_add(ar, arvif, arsta);
8046 		if (ret) {
8047 			ath12k_warn(ar->ab, "Failed to add station: %pM for VDEV: %d\n",
8048 				    arsta->addr, arvif->vdev_id);
8049 			ath12k_mac_free_unassign_link_sta(ah, ahsta, link_id);
8050 			return ret;
8051 		}
8052 	}
8053 
8054 	return 0;
8055 }
8056 EXPORT_SYMBOL(ath12k_mac_op_change_sta_links);
8057 
8058 bool ath12k_mac_op_can_activate_links(struct ieee80211_hw *hw,
8059 				      struct ieee80211_vif *vif,
8060 				      u16 active_links)
8061 {
8062 	/* TODO: Handle recovery case */
8063 
8064 	return true;
8065 }
8066 EXPORT_SYMBOL(ath12k_mac_op_can_activate_links);
8067 
8068 static int ath12k_conf_tx_uapsd(struct ath12k_link_vif *arvif,
8069 				u16 ac, bool enable)
8070 {
8071 	struct ath12k *ar = arvif->ar;
8072 	struct ath12k_vif *ahvif = arvif->ahvif;
8073 	u32 value;
8074 	int ret;
8075 
8076 	if (ahvif->vdev_type != WMI_VDEV_TYPE_STA)
8077 		return 0;
8078 
8079 	switch (ac) {
8080 	case IEEE80211_AC_VO:
8081 		value = WMI_STA_PS_UAPSD_AC3_DELIVERY_EN |
8082 			WMI_STA_PS_UAPSD_AC3_TRIGGER_EN;
8083 		break;
8084 	case IEEE80211_AC_VI:
8085 		value = WMI_STA_PS_UAPSD_AC2_DELIVERY_EN |
8086 			WMI_STA_PS_UAPSD_AC2_TRIGGER_EN;
8087 		break;
8088 	case IEEE80211_AC_BE:
8089 		value = WMI_STA_PS_UAPSD_AC1_DELIVERY_EN |
8090 			WMI_STA_PS_UAPSD_AC1_TRIGGER_EN;
8091 		break;
8092 	case IEEE80211_AC_BK:
8093 		value = WMI_STA_PS_UAPSD_AC0_DELIVERY_EN |
8094 			WMI_STA_PS_UAPSD_AC0_TRIGGER_EN;
8095 		break;
8096 	}
8097 
8098 	if (enable)
8099 		ahvif->u.sta.uapsd |= value;
8100 	else
8101 		ahvif->u.sta.uapsd &= ~value;
8102 
8103 	ret = ath12k_wmi_set_sta_ps_param(ar, arvif->vdev_id,
8104 					  WMI_STA_PS_PARAM_UAPSD,
8105 					  ahvif->u.sta.uapsd);
8106 	if (ret) {
8107 		ath12k_warn(ar->ab, "could not set uapsd params %d\n", ret);
8108 		goto exit;
8109 	}
8110 
8111 	if (ahvif->u.sta.uapsd)
8112 		value = WMI_STA_PS_RX_WAKE_POLICY_POLL_UAPSD;
8113 	else
8114 		value = WMI_STA_PS_RX_WAKE_POLICY_WAKE;
8115 
8116 	ret = ath12k_wmi_set_sta_ps_param(ar, arvif->vdev_id,
8117 					  WMI_STA_PS_PARAM_RX_WAKE_POLICY,
8118 					  value);
8119 	if (ret)
8120 		ath12k_warn(ar->ab, "could not set rx wake param %d\n", ret);
8121 
8122 exit:
8123 	return ret;
8124 }
8125 
8126 static int ath12k_mac_conf_tx(struct ath12k_link_vif *arvif, u16 ac,
8127 			      const struct ieee80211_tx_queue_params *params)
8128 {
8129 	struct wmi_wmm_params_arg *p = NULL;
8130 	struct ath12k *ar = arvif->ar;
8131 	struct ath12k_base *ab = ar->ab;
8132 	int ret;
8133 
8134 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
8135 
8136 	switch (ac) {
8137 	case IEEE80211_AC_VO:
8138 		p = &arvif->wmm_params.ac_vo;
8139 		break;
8140 	case IEEE80211_AC_VI:
8141 		p = &arvif->wmm_params.ac_vi;
8142 		break;
8143 	case IEEE80211_AC_BE:
8144 		p = &arvif->wmm_params.ac_be;
8145 		break;
8146 	case IEEE80211_AC_BK:
8147 		p = &arvif->wmm_params.ac_bk;
8148 		break;
8149 	}
8150 
8151 	if (WARN_ON(!p)) {
8152 		ret = -EINVAL;
8153 		goto exit;
8154 	}
8155 
8156 	p->cwmin = params->cw_min;
8157 	p->cwmax = params->cw_max;
8158 	p->aifs = params->aifs;
8159 	p->txop = params->txop;
8160 
8161 	ret = ath12k_wmi_send_wmm_update_cmd(ar, arvif->vdev_id,
8162 					     &arvif->wmm_params);
8163 	if (ret) {
8164 		ath12k_warn(ab, "pdev idx %d failed to set wmm params: %d\n",
8165 			    ar->pdev_idx, ret);
8166 		goto exit;
8167 	}
8168 
8169 	ret = ath12k_conf_tx_uapsd(arvif, ac, params->uapsd);
8170 	if (ret)
8171 		ath12k_warn(ab, "pdev idx %d failed to set sta uapsd: %d\n",
8172 			    ar->pdev_idx, ret);
8173 
8174 exit:
8175 	return ret;
8176 }
8177 
8178 int ath12k_mac_op_conf_tx(struct ieee80211_hw *hw,
8179 			  struct ieee80211_vif *vif,
8180 			  unsigned int link_id, u16 ac,
8181 			  const struct ieee80211_tx_queue_params *params)
8182 {
8183 	struct ath12k_vif *ahvif = ath12k_vif_to_ahvif(vif);
8184 	struct ath12k_link_vif *arvif;
8185 	struct ath12k_vif_cache *cache;
8186 	int ret;
8187 
8188 	lockdep_assert_wiphy(hw->wiphy);
8189 
8190 	if (link_id >= IEEE80211_MLD_MAX_NUM_LINKS)
8191 		return -EINVAL;
8192 
8193 	arvif = wiphy_dereference(hw->wiphy, ahvif->link[link_id]);
8194 	if (!arvif || !arvif->is_created) {
8195 		cache = ath12k_ahvif_get_link_cache(ahvif, link_id);
8196 		if (!cache)
8197 			return -ENOSPC;
8198 
8199 		cache->tx_conf.changed = true;
8200 		cache->tx_conf.ac = ac;
8201 		cache->tx_conf.tx_queue_params = *params;
8202 
8203 		return 0;
8204 	}
8205 
8206 	ret = ath12k_mac_conf_tx(arvif, ac, params);
8207 
8208 	return ret;
8209 }
8210 EXPORT_SYMBOL(ath12k_mac_op_conf_tx);
8211 
8212 static struct ieee80211_sta_ht_cap
8213 ath12k_create_ht_cap(struct ath12k *ar, u32 ar_ht_cap, u32 rate_cap_rx_chainmask)
8214 {
8215 	int i;
8216 	struct ieee80211_sta_ht_cap ht_cap = {};
8217 	u32 ar_vht_cap = ar->pdev->cap.vht_cap;
8218 
8219 	if (!(ar_ht_cap & WMI_HT_CAP_ENABLED))
8220 		return ht_cap;
8221 
8222 	ht_cap.ht_supported = 1;
8223 	ht_cap.ampdu_factor = IEEE80211_HT_MAX_AMPDU_64K;
8224 	ht_cap.ampdu_density = IEEE80211_HT_MPDU_DENSITY_NONE;
8225 	ht_cap.cap |= IEEE80211_HT_CAP_SUP_WIDTH_20_40;
8226 	ht_cap.cap |= IEEE80211_HT_CAP_DSSSCCK40;
8227 	ht_cap.cap |= WLAN_HT_CAP_SM_PS_STATIC << IEEE80211_HT_CAP_SM_PS_SHIFT;
8228 
8229 	if (ar_ht_cap & WMI_HT_CAP_HT20_SGI)
8230 		ht_cap.cap |= IEEE80211_HT_CAP_SGI_20;
8231 
8232 	if (ar_ht_cap & WMI_HT_CAP_HT40_SGI)
8233 		ht_cap.cap |= IEEE80211_HT_CAP_SGI_40;
8234 
8235 	if (ar_ht_cap & WMI_HT_CAP_DYNAMIC_SMPS) {
8236 		u32 smps;
8237 
8238 		smps   = WLAN_HT_CAP_SM_PS_DYNAMIC;
8239 		smps <<= IEEE80211_HT_CAP_SM_PS_SHIFT;
8240 
8241 		ht_cap.cap |= smps;
8242 	}
8243 
8244 	if (ar_ht_cap & WMI_HT_CAP_TX_STBC)
8245 		ht_cap.cap |= IEEE80211_HT_CAP_TX_STBC;
8246 
8247 	if (ar_ht_cap & WMI_HT_CAP_RX_STBC) {
8248 		u32 stbc;
8249 
8250 		stbc   = ar_ht_cap;
8251 		stbc  &= WMI_HT_CAP_RX_STBC;
8252 		stbc >>= WMI_HT_CAP_RX_STBC_MASK_SHIFT;
8253 		stbc <<= IEEE80211_HT_CAP_RX_STBC_SHIFT;
8254 		stbc  &= IEEE80211_HT_CAP_RX_STBC;
8255 
8256 		ht_cap.cap |= stbc;
8257 	}
8258 
8259 	if (ar_ht_cap & WMI_HT_CAP_RX_LDPC)
8260 		ht_cap.cap |= IEEE80211_HT_CAP_LDPC_CODING;
8261 
8262 	if (ar_ht_cap & WMI_HT_CAP_L_SIG_TXOP_PROT)
8263 		ht_cap.cap |= IEEE80211_HT_CAP_LSIG_TXOP_PROT;
8264 
8265 	if (ar_vht_cap & WMI_VHT_CAP_MAX_MPDU_LEN_MASK)
8266 		ht_cap.cap |= IEEE80211_HT_CAP_MAX_AMSDU;
8267 
8268 	for (i = 0; i < ar->num_rx_chains; i++) {
8269 		if (rate_cap_rx_chainmask & BIT(i))
8270 			ht_cap.mcs.rx_mask[i] = 0xFF;
8271 	}
8272 
8273 	ht_cap.mcs.tx_params |= IEEE80211_HT_MCS_TX_DEFINED;
8274 
8275 	return ht_cap;
8276 }
8277 
8278 static int ath12k_mac_set_txbf_conf(struct ath12k_link_vif *arvif)
8279 {
8280 	u32 value = 0;
8281 	struct ath12k *ar = arvif->ar;
8282 	struct ath12k_vif *ahvif = arvif->ahvif;
8283 	int nsts;
8284 	int sound_dim;
8285 	u32 vht_cap = ar->pdev->cap.vht_cap;
8286 	u32 vdev_param = WMI_VDEV_PARAM_TXBF;
8287 
8288 	if (vht_cap & (IEEE80211_VHT_CAP_SU_BEAMFORMEE_CAPABLE)) {
8289 		nsts = vht_cap & IEEE80211_VHT_CAP_BEAMFORMEE_STS_MASK;
8290 		nsts >>= IEEE80211_VHT_CAP_BEAMFORMEE_STS_SHIFT;
8291 		value |= SM(nsts, WMI_TXBF_STS_CAP_OFFSET);
8292 	}
8293 
8294 	if (vht_cap & (IEEE80211_VHT_CAP_SU_BEAMFORMER_CAPABLE)) {
8295 		sound_dim = vht_cap &
8296 			    IEEE80211_VHT_CAP_SOUNDING_DIMENSIONS_MASK;
8297 		sound_dim >>= IEEE80211_VHT_CAP_SOUNDING_DIMENSIONS_SHIFT;
8298 		if (sound_dim > (ar->num_tx_chains - 1))
8299 			sound_dim = ar->num_tx_chains - 1;
8300 		value |= SM(sound_dim, WMI_BF_SOUND_DIM_OFFSET);
8301 	}
8302 
8303 	if (!value)
8304 		return 0;
8305 
8306 	if (vht_cap & IEEE80211_VHT_CAP_SU_BEAMFORMER_CAPABLE) {
8307 		value |= WMI_VDEV_PARAM_TXBF_SU_TX_BFER;
8308 
8309 		if ((vht_cap & IEEE80211_VHT_CAP_MU_BEAMFORMER_CAPABLE) &&
8310 		    ahvif->vdev_type == WMI_VDEV_TYPE_AP)
8311 			value |= WMI_VDEV_PARAM_TXBF_MU_TX_BFER;
8312 	}
8313 
8314 	if (vht_cap & IEEE80211_VHT_CAP_SU_BEAMFORMEE_CAPABLE) {
8315 		value |= WMI_VDEV_PARAM_TXBF_SU_TX_BFEE;
8316 
8317 		if ((vht_cap & IEEE80211_VHT_CAP_MU_BEAMFORMEE_CAPABLE) &&
8318 		    ahvif->vdev_type == WMI_VDEV_TYPE_STA)
8319 			value |= WMI_VDEV_PARAM_TXBF_MU_TX_BFEE;
8320 	}
8321 
8322 	return ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
8323 					     vdev_param, value);
8324 }
8325 
8326 static void ath12k_set_vht_txbf_cap(struct ath12k *ar, u32 *vht_cap)
8327 {
8328 	bool subfer, subfee;
8329 	int sound_dim = 0;
8330 
8331 	subfer = !!(*vht_cap & (IEEE80211_VHT_CAP_SU_BEAMFORMER_CAPABLE));
8332 	subfee = !!(*vht_cap & (IEEE80211_VHT_CAP_SU_BEAMFORMEE_CAPABLE));
8333 
8334 	if (ar->num_tx_chains < 2) {
8335 		*vht_cap &= ~(IEEE80211_VHT_CAP_SU_BEAMFORMER_CAPABLE);
8336 		subfer = false;
8337 	}
8338 
8339 	/* If SU Beaformer is not set, then disable MU Beamformer Capability */
8340 	if (!subfer)
8341 		*vht_cap &= ~(IEEE80211_VHT_CAP_MU_BEAMFORMER_CAPABLE);
8342 
8343 	/* If SU Beaformee is not set, then disable MU Beamformee Capability */
8344 	if (!subfee)
8345 		*vht_cap &= ~(IEEE80211_VHT_CAP_MU_BEAMFORMEE_CAPABLE);
8346 
8347 	sound_dim = u32_get_bits(*vht_cap,
8348 				 IEEE80211_VHT_CAP_SOUNDING_DIMENSIONS_MASK);
8349 	*vht_cap = u32_replace_bits(*vht_cap, 0,
8350 				    IEEE80211_VHT_CAP_SOUNDING_DIMENSIONS_MASK);
8351 
8352 	/* TODO: Need to check invalid STS and Sound_dim values set by FW? */
8353 
8354 	/* Enable Sounding Dimension Field only if SU BF is enabled */
8355 	if (subfer) {
8356 		if (sound_dim > (ar->num_tx_chains - 1))
8357 			sound_dim = ar->num_tx_chains - 1;
8358 
8359 		*vht_cap = u32_replace_bits(*vht_cap, sound_dim,
8360 					    IEEE80211_VHT_CAP_SOUNDING_DIMENSIONS_MASK);
8361 	}
8362 
8363 	/* Use the STS advertised by FW unless SU Beamformee is not supported*/
8364 	if (!subfee)
8365 		*vht_cap &= ~(IEEE80211_VHT_CAP_BEAMFORMEE_STS_MASK);
8366 }
8367 
8368 static struct ieee80211_sta_vht_cap
8369 ath12k_create_vht_cap(struct ath12k *ar, u32 rate_cap_tx_chainmask,
8370 		      u32 rate_cap_rx_chainmask)
8371 {
8372 	struct ieee80211_sta_vht_cap vht_cap = {};
8373 	u16 txmcs_map, rxmcs_map;
8374 	int i;
8375 
8376 	vht_cap.vht_supported = 1;
8377 	vht_cap.cap = ar->pdev->cap.vht_cap;
8378 
8379 	ath12k_set_vht_txbf_cap(ar, &vht_cap.cap);
8380 
8381 	/* 80P80 is not supported */
8382 	vht_cap.cap &= ~IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_160_80PLUS80MHZ;
8383 
8384 	rxmcs_map = 0;
8385 	txmcs_map = 0;
8386 	for (i = 0; i < 8; i++) {
8387 		if (i < ar->num_tx_chains && rate_cap_tx_chainmask & BIT(i))
8388 			txmcs_map |= IEEE80211_VHT_MCS_SUPPORT_0_9 << (i * 2);
8389 		else
8390 			txmcs_map |= IEEE80211_VHT_MCS_NOT_SUPPORTED << (i * 2);
8391 
8392 		if (i < ar->num_rx_chains && rate_cap_rx_chainmask & BIT(i))
8393 			rxmcs_map |= IEEE80211_VHT_MCS_SUPPORT_0_9 << (i * 2);
8394 		else
8395 			rxmcs_map |= IEEE80211_VHT_MCS_NOT_SUPPORTED << (i * 2);
8396 	}
8397 
8398 	if (rate_cap_tx_chainmask <= 1)
8399 		vht_cap.cap &= ~IEEE80211_VHT_CAP_TXSTBC;
8400 
8401 	vht_cap.vht_mcs.rx_mcs_map = cpu_to_le16(rxmcs_map);
8402 	vht_cap.vht_mcs.tx_mcs_map = cpu_to_le16(txmcs_map);
8403 
8404 	/* Check if the HW supports 1:1 NSS ratio and reset
8405 	 * EXT NSS BW Support field to 0 to indicate 1:1 ratio
8406 	 */
8407 	if (ar->pdev->cap.nss_ratio_info == WMI_NSS_RATIO_1_NSS)
8408 		vht_cap.cap &= ~IEEE80211_VHT_CAP_EXT_NSS_BW_MASK;
8409 
8410 	return vht_cap;
8411 }
8412 
8413 static void ath12k_mac_setup_ht_vht_cap(struct ath12k *ar,
8414 					struct ath12k_pdev_cap *cap,
8415 					u32 *ht_cap_info)
8416 {
8417 	struct ieee80211_supported_band *band;
8418 	u32 rate_cap_tx_chainmask;
8419 	u32 rate_cap_rx_chainmask;
8420 	u32 ht_cap;
8421 
8422 	rate_cap_tx_chainmask = ar->cfg_tx_chainmask >> cap->tx_chain_mask_shift;
8423 	rate_cap_rx_chainmask = ar->cfg_rx_chainmask >> cap->rx_chain_mask_shift;
8424 
8425 	if (cap->supported_bands & WMI_HOST_WLAN_2GHZ_CAP) {
8426 		band = &ar->mac.sbands[NL80211_BAND_2GHZ];
8427 		ht_cap = cap->band[NL80211_BAND_2GHZ].ht_cap_info;
8428 		if (ht_cap_info)
8429 			*ht_cap_info = ht_cap;
8430 		band->ht_cap = ath12k_create_ht_cap(ar, ht_cap,
8431 						    rate_cap_rx_chainmask);
8432 	}
8433 
8434 	if (cap->supported_bands & WMI_HOST_WLAN_5GHZ_CAP &&
8435 	    (ar->ab->hw_params->single_pdev_only ||
8436 	     !ar->supports_6ghz)) {
8437 		band = &ar->mac.sbands[NL80211_BAND_5GHZ];
8438 		ht_cap = cap->band[NL80211_BAND_5GHZ].ht_cap_info;
8439 		if (ht_cap_info)
8440 			*ht_cap_info = ht_cap;
8441 		band->ht_cap = ath12k_create_ht_cap(ar, ht_cap,
8442 						    rate_cap_rx_chainmask);
8443 		band->vht_cap = ath12k_create_vht_cap(ar, rate_cap_tx_chainmask,
8444 						      rate_cap_rx_chainmask);
8445 	}
8446 }
8447 
8448 static int ath12k_check_chain_mask(struct ath12k *ar, u32 ant, bool is_tx_ant)
8449 {
8450 	/* TODO: Check the request chainmask against the supported
8451 	 * chainmask table which is advertised in extented_service_ready event
8452 	 */
8453 
8454 	return 0;
8455 }
8456 
8457 static void ath12k_gen_ppe_thresh(struct ath12k_wmi_ppe_threshold_arg *fw_ppet,
8458 				  u8 *he_ppet)
8459 {
8460 	int nss, ru;
8461 	u8 bit = 7;
8462 
8463 	he_ppet[0] = fw_ppet->numss_m1 & IEEE80211_PPE_THRES_NSS_MASK;
8464 	he_ppet[0] |= (fw_ppet->ru_bit_mask <<
8465 		       IEEE80211_PPE_THRES_RU_INDEX_BITMASK_POS) &
8466 		      IEEE80211_PPE_THRES_RU_INDEX_BITMASK_MASK;
8467 	for (nss = 0; nss <= fw_ppet->numss_m1; nss++) {
8468 		for (ru = 0; ru < 4; ru++) {
8469 			u8 val;
8470 			int i;
8471 
8472 			if ((fw_ppet->ru_bit_mask & BIT(ru)) == 0)
8473 				continue;
8474 			val = (fw_ppet->ppet16_ppet8_ru3_ru0[nss] >> (ru * 6)) &
8475 			       0x3f;
8476 			val = ((val >> 3) & 0x7) | ((val & 0x7) << 3);
8477 			for (i = 5; i >= 0; i--) {
8478 				he_ppet[bit / 8] |=
8479 					((val >> i) & 0x1) << ((bit % 8));
8480 				bit++;
8481 			}
8482 		}
8483 	}
8484 }
8485 
8486 static void
8487 ath12k_mac_filter_he_cap_mesh(struct ieee80211_he_cap_elem *he_cap_elem)
8488 {
8489 	u8 m;
8490 
8491 	m = IEEE80211_HE_MAC_CAP0_TWT_RES |
8492 	    IEEE80211_HE_MAC_CAP0_TWT_REQ;
8493 	he_cap_elem->mac_cap_info[0] &= ~m;
8494 
8495 	m = IEEE80211_HE_MAC_CAP2_TRS |
8496 	    IEEE80211_HE_MAC_CAP2_BCAST_TWT |
8497 	    IEEE80211_HE_MAC_CAP2_MU_CASCADING;
8498 	he_cap_elem->mac_cap_info[2] &= ~m;
8499 
8500 	m = IEEE80211_HE_MAC_CAP3_FLEX_TWT_SCHED |
8501 	    IEEE80211_HE_MAC_CAP2_BCAST_TWT |
8502 	    IEEE80211_HE_MAC_CAP2_MU_CASCADING;
8503 	he_cap_elem->mac_cap_info[3] &= ~m;
8504 
8505 	m = IEEE80211_HE_MAC_CAP4_BSRP_BQRP_A_MPDU_AGG |
8506 	    IEEE80211_HE_MAC_CAP4_BQR;
8507 	he_cap_elem->mac_cap_info[4] &= ~m;
8508 
8509 	m = IEEE80211_HE_MAC_CAP5_SUBCHAN_SELECTIVE_TRANSMISSION |
8510 	    IEEE80211_HE_MAC_CAP5_UL_2x996_TONE_RU |
8511 	    IEEE80211_HE_MAC_CAP5_PUNCTURED_SOUNDING |
8512 	    IEEE80211_HE_MAC_CAP5_HT_VHT_TRIG_FRAME_RX;
8513 	he_cap_elem->mac_cap_info[5] &= ~m;
8514 
8515 	m = IEEE80211_HE_PHY_CAP2_UL_MU_FULL_MU_MIMO |
8516 	    IEEE80211_HE_PHY_CAP2_UL_MU_PARTIAL_MU_MIMO;
8517 	he_cap_elem->phy_cap_info[2] &= ~m;
8518 
8519 	m = IEEE80211_HE_PHY_CAP3_RX_PARTIAL_BW_SU_IN_20MHZ_MU |
8520 	    IEEE80211_HE_PHY_CAP3_DCM_MAX_CONST_TX_MASK |
8521 	    IEEE80211_HE_PHY_CAP3_DCM_MAX_CONST_RX_MASK;
8522 	he_cap_elem->phy_cap_info[3] &= ~m;
8523 
8524 	m = IEEE80211_HE_PHY_CAP4_MU_BEAMFORMER;
8525 	he_cap_elem->phy_cap_info[4] &= ~m;
8526 
8527 	m = IEEE80211_HE_PHY_CAP5_NG16_MU_FEEDBACK;
8528 	he_cap_elem->phy_cap_info[5] &= ~m;
8529 
8530 	m = IEEE80211_HE_PHY_CAP6_CODEBOOK_SIZE_75_MU |
8531 	    IEEE80211_HE_PHY_CAP6_TRIG_MU_BEAMFORMING_PARTIAL_BW_FB |
8532 	    IEEE80211_HE_PHY_CAP6_TRIG_CQI_FB |
8533 	    IEEE80211_HE_PHY_CAP6_PARTIAL_BANDWIDTH_DL_MUMIMO;
8534 	he_cap_elem->phy_cap_info[6] &= ~m;
8535 
8536 	m = IEEE80211_HE_PHY_CAP7_PSR_BASED_SR |
8537 	    IEEE80211_HE_PHY_CAP7_POWER_BOOST_FACTOR_SUPP |
8538 	    IEEE80211_HE_PHY_CAP7_STBC_TX_ABOVE_80MHZ |
8539 	    IEEE80211_HE_PHY_CAP7_STBC_RX_ABOVE_80MHZ;
8540 	he_cap_elem->phy_cap_info[7] &= ~m;
8541 
8542 	m = IEEE80211_HE_PHY_CAP8_HE_ER_SU_PPDU_4XLTF_AND_08_US_GI |
8543 	    IEEE80211_HE_PHY_CAP8_20MHZ_IN_40MHZ_HE_PPDU_IN_2G |
8544 	    IEEE80211_HE_PHY_CAP8_20MHZ_IN_160MHZ_HE_PPDU |
8545 	    IEEE80211_HE_PHY_CAP8_80MHZ_IN_160MHZ_HE_PPDU;
8546 	he_cap_elem->phy_cap_info[8] &= ~m;
8547 
8548 	m = IEEE80211_HE_PHY_CAP9_LONGER_THAN_16_SIGB_OFDM_SYM |
8549 	    IEEE80211_HE_PHY_CAP9_NON_TRIGGERED_CQI_FEEDBACK |
8550 	    IEEE80211_HE_PHY_CAP9_RX_1024_QAM_LESS_THAN_242_TONE_RU |
8551 	    IEEE80211_HE_PHY_CAP9_TX_1024_QAM_LESS_THAN_242_TONE_RU |
8552 	    IEEE80211_HE_PHY_CAP9_RX_FULL_BW_SU_USING_MU_WITH_COMP_SIGB |
8553 	    IEEE80211_HE_PHY_CAP9_RX_FULL_BW_SU_USING_MU_WITH_NON_COMP_SIGB;
8554 	he_cap_elem->phy_cap_info[9] &= ~m;
8555 }
8556 
8557 static __le16 ath12k_mac_setup_he_6ghz_cap(struct ath12k_pdev_cap *pcap,
8558 					   struct ath12k_band_cap *bcap)
8559 {
8560 	u8 val;
8561 
8562 	bcap->he_6ghz_capa = IEEE80211_HT_MPDU_DENSITY_NONE;
8563 	if (bcap->ht_cap_info & WMI_HT_CAP_DYNAMIC_SMPS)
8564 		bcap->he_6ghz_capa |=
8565 			u32_encode_bits(WLAN_HT_CAP_SM_PS_DYNAMIC,
8566 					IEEE80211_HE_6GHZ_CAP_SM_PS);
8567 	else
8568 		bcap->he_6ghz_capa |=
8569 			u32_encode_bits(WLAN_HT_CAP_SM_PS_DISABLED,
8570 					IEEE80211_HE_6GHZ_CAP_SM_PS);
8571 	val = u32_get_bits(pcap->vht_cap,
8572 			   IEEE80211_VHT_CAP_MAX_A_MPDU_LENGTH_EXPONENT_MASK);
8573 	bcap->he_6ghz_capa |=
8574 		u32_encode_bits(val, IEEE80211_HE_6GHZ_CAP_MAX_AMPDU_LEN_EXP);
8575 	val = u32_get_bits(pcap->vht_cap,
8576 			   IEEE80211_VHT_CAP_MAX_MPDU_MASK);
8577 	bcap->he_6ghz_capa |=
8578 		u32_encode_bits(val, IEEE80211_HE_6GHZ_CAP_MAX_MPDU_LEN);
8579 	if (pcap->vht_cap & IEEE80211_VHT_CAP_RX_ANTENNA_PATTERN)
8580 		bcap->he_6ghz_capa |= IEEE80211_HE_6GHZ_CAP_RX_ANTPAT_CONS;
8581 	if (pcap->vht_cap & IEEE80211_VHT_CAP_TX_ANTENNA_PATTERN)
8582 		bcap->he_6ghz_capa |= IEEE80211_HE_6GHZ_CAP_TX_ANTPAT_CONS;
8583 
8584 	return cpu_to_le16(bcap->he_6ghz_capa);
8585 }
8586 
8587 static void ath12k_mac_set_hemcsmap(struct ath12k *ar,
8588 				    struct ath12k_pdev_cap *cap,
8589 				    struct ieee80211_sta_he_cap *he_cap)
8590 {
8591 	struct ieee80211_he_mcs_nss_supp *mcs_nss = &he_cap->he_mcs_nss_supp;
8592 	u8 maxtxnss_160 = ath12k_get_nss_160mhz(ar, ar->num_tx_chains);
8593 	u8 maxrxnss_160 = ath12k_get_nss_160mhz(ar, ar->num_rx_chains);
8594 	u16 txmcs_map_160 = 0, rxmcs_map_160 = 0;
8595 	u16 txmcs_map = 0, rxmcs_map = 0;
8596 	u32 i;
8597 
8598 	for (i = 0; i < 8; i++) {
8599 		if (i < ar->num_tx_chains &&
8600 		    (ar->cfg_tx_chainmask >> cap->tx_chain_mask_shift) & BIT(i))
8601 			txmcs_map |= IEEE80211_HE_MCS_SUPPORT_0_11 << (i * 2);
8602 		else
8603 			txmcs_map |= IEEE80211_HE_MCS_NOT_SUPPORTED << (i * 2);
8604 
8605 		if (i < ar->num_rx_chains &&
8606 		    (ar->cfg_rx_chainmask >> cap->tx_chain_mask_shift) & BIT(i))
8607 			rxmcs_map |= IEEE80211_HE_MCS_SUPPORT_0_11 << (i * 2);
8608 		else
8609 			rxmcs_map |= IEEE80211_HE_MCS_NOT_SUPPORTED << (i * 2);
8610 
8611 		if (i < maxtxnss_160 &&
8612 		    (ar->cfg_tx_chainmask >> cap->tx_chain_mask_shift) & BIT(i))
8613 			txmcs_map_160 |= IEEE80211_HE_MCS_SUPPORT_0_11 << (i * 2);
8614 		else
8615 			txmcs_map_160 |= IEEE80211_HE_MCS_NOT_SUPPORTED << (i * 2);
8616 
8617 		if (i < maxrxnss_160 &&
8618 		    (ar->cfg_tx_chainmask >> cap->tx_chain_mask_shift) & BIT(i))
8619 			rxmcs_map_160 |= IEEE80211_HE_MCS_SUPPORT_0_11 << (i * 2);
8620 		else
8621 			rxmcs_map_160 |= IEEE80211_HE_MCS_NOT_SUPPORTED << (i * 2);
8622 	}
8623 
8624 	mcs_nss->rx_mcs_80 = cpu_to_le16(rxmcs_map & 0xffff);
8625 	mcs_nss->tx_mcs_80 = cpu_to_le16(txmcs_map & 0xffff);
8626 	mcs_nss->rx_mcs_160 = cpu_to_le16(rxmcs_map_160 & 0xffff);
8627 	mcs_nss->tx_mcs_160 = cpu_to_le16(txmcs_map_160 & 0xffff);
8628 }
8629 
8630 static void ath12k_mac_copy_he_cap(struct ath12k *ar,
8631 				   struct ath12k_band_cap *band_cap,
8632 				   int iftype, u8 num_tx_chains,
8633 				   struct ieee80211_sta_he_cap *he_cap)
8634 {
8635 	struct ieee80211_he_cap_elem *he_cap_elem = &he_cap->he_cap_elem;
8636 
8637 	he_cap->has_he = true;
8638 	memcpy(he_cap_elem->mac_cap_info, band_cap->he_cap_info,
8639 	       sizeof(he_cap_elem->mac_cap_info));
8640 	memcpy(he_cap_elem->phy_cap_info, band_cap->he_cap_phy_info,
8641 	       sizeof(he_cap_elem->phy_cap_info));
8642 
8643 	he_cap_elem->mac_cap_info[1] &=
8644 		IEEE80211_HE_MAC_CAP1_TF_MAC_PAD_DUR_MASK;
8645 	he_cap_elem->phy_cap_info[0] &=
8646 		IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_40MHZ_IN_2G |
8647 		IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_40MHZ_80MHZ_IN_5G |
8648 		IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_160MHZ_IN_5G;
8649 	/* 80PLUS80 is not supported */
8650 	he_cap_elem->phy_cap_info[0] &=
8651 		~IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_80PLUS80_MHZ_IN_5G;
8652 	he_cap_elem->phy_cap_info[5] &=
8653 		~IEEE80211_HE_PHY_CAP5_BEAMFORMEE_NUM_SND_DIM_UNDER_80MHZ_MASK;
8654 	he_cap_elem->phy_cap_info[5] |= num_tx_chains - 1;
8655 
8656 	switch (iftype) {
8657 	case NL80211_IFTYPE_AP:
8658 		he_cap_elem->mac_cap_info[2] &=
8659 			~IEEE80211_HE_MAC_CAP2_BCAST_TWT;
8660 		he_cap_elem->phy_cap_info[3] &=
8661 			~IEEE80211_HE_PHY_CAP3_DCM_MAX_CONST_TX_MASK;
8662 		he_cap_elem->phy_cap_info[9] |=
8663 			IEEE80211_HE_PHY_CAP9_RX_1024_QAM_LESS_THAN_242_TONE_RU;
8664 		break;
8665 	case NL80211_IFTYPE_STATION:
8666 		he_cap_elem->mac_cap_info[0] &= ~IEEE80211_HE_MAC_CAP0_TWT_RES;
8667 		he_cap_elem->mac_cap_info[0] |= IEEE80211_HE_MAC_CAP0_TWT_REQ;
8668 		he_cap_elem->phy_cap_info[9] |=
8669 			IEEE80211_HE_PHY_CAP9_TX_1024_QAM_LESS_THAN_242_TONE_RU;
8670 		break;
8671 	case NL80211_IFTYPE_MESH_POINT:
8672 		ath12k_mac_filter_he_cap_mesh(he_cap_elem);
8673 		break;
8674 	}
8675 
8676 	ath12k_mac_set_hemcsmap(ar, &ar->pdev->cap, he_cap);
8677 	memset(he_cap->ppe_thres, 0, sizeof(he_cap->ppe_thres));
8678 	if (he_cap_elem->phy_cap_info[6] &
8679 	    IEEE80211_HE_PHY_CAP6_PPE_THRESHOLD_PRESENT)
8680 		ath12k_gen_ppe_thresh(&band_cap->he_ppet, he_cap->ppe_thres);
8681 }
8682 
8683 static void
8684 ath12k_mac_copy_eht_mcs_nss(struct ath12k_band_cap *band_cap,
8685 			    struct ieee80211_eht_mcs_nss_supp *mcs_nss,
8686 			    const struct ieee80211_he_cap_elem *he_cap,
8687 			    const struct ieee80211_eht_cap_elem_fixed *eht_cap)
8688 {
8689 	if ((he_cap->phy_cap_info[0] &
8690 	     (IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_40MHZ_IN_2G |
8691 	      IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_40MHZ_80MHZ_IN_5G |
8692 	      IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_160MHZ_IN_5G |
8693 	      IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_80PLUS80_MHZ_IN_5G)) == 0)
8694 		memcpy(&mcs_nss->only_20mhz, &band_cap->eht_mcs_20_only,
8695 		       sizeof(struct ieee80211_eht_mcs_nss_supp_20mhz_only));
8696 
8697 	if (he_cap->phy_cap_info[0] &
8698 	    (IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_40MHZ_IN_2G |
8699 	     IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_40MHZ_80MHZ_IN_5G))
8700 		memcpy(&mcs_nss->bw._80, &band_cap->eht_mcs_80,
8701 		       sizeof(struct ieee80211_eht_mcs_nss_supp_bw));
8702 
8703 	if (he_cap->phy_cap_info[0] &
8704 	    IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_160MHZ_IN_5G)
8705 		memcpy(&mcs_nss->bw._160, &band_cap->eht_mcs_160,
8706 		       sizeof(struct ieee80211_eht_mcs_nss_supp_bw));
8707 
8708 	if (eht_cap->phy_cap_info[0] & IEEE80211_EHT_PHY_CAP0_320MHZ_IN_6GHZ)
8709 		memcpy(&mcs_nss->bw._320, &band_cap->eht_mcs_320,
8710 		       sizeof(struct ieee80211_eht_mcs_nss_supp_bw));
8711 }
8712 
8713 static void ath12k_mac_copy_eht_ppe_thresh(struct ath12k_wmi_ppe_threshold_arg *fw_ppet,
8714 					   struct ieee80211_sta_eht_cap *cap)
8715 {
8716 	u16 bit = IEEE80211_EHT_PPE_THRES_INFO_HEADER_SIZE;
8717 	u8 i, nss, ru, ppet_bit_len_per_ru = IEEE80211_EHT_PPE_THRES_INFO_PPET_SIZE * 2;
8718 
8719 	u8p_replace_bits(&cap->eht_ppe_thres[0], fw_ppet->numss_m1,
8720 			 IEEE80211_EHT_PPE_THRES_NSS_MASK);
8721 
8722 	u16p_replace_bits((u16 *)&cap->eht_ppe_thres[0], fw_ppet->ru_bit_mask,
8723 			  IEEE80211_EHT_PPE_THRES_RU_INDEX_BITMASK_MASK);
8724 
8725 	for (nss = 0; nss <= fw_ppet->numss_m1; nss++) {
8726 		for (ru = 0;
8727 		     ru < hweight16(IEEE80211_EHT_PPE_THRES_RU_INDEX_BITMASK_MASK);
8728 		     ru++) {
8729 			u32 val = 0;
8730 
8731 			if ((fw_ppet->ru_bit_mask & BIT(ru)) == 0)
8732 				continue;
8733 
8734 			u32p_replace_bits(&val, fw_ppet->ppet16_ppet8_ru3_ru0[nss] >>
8735 						(ru * ppet_bit_len_per_ru),
8736 					  GENMASK(ppet_bit_len_per_ru - 1, 0));
8737 
8738 			for (i = 0; i < ppet_bit_len_per_ru; i++) {
8739 				cap->eht_ppe_thres[bit / 8] |=
8740 					(((val >> i) & 0x1) << ((bit % 8)));
8741 				bit++;
8742 			}
8743 		}
8744 	}
8745 }
8746 
8747 static void
8748 ath12k_mac_filter_eht_cap_mesh(struct ieee80211_eht_cap_elem_fixed
8749 			       *eht_cap_elem)
8750 {
8751 	u8 m;
8752 
8753 	m = IEEE80211_EHT_MAC_CAP0_EPCS_PRIO_ACCESS;
8754 	eht_cap_elem->mac_cap_info[0] &= ~m;
8755 
8756 	m = IEEE80211_EHT_PHY_CAP0_PARTIAL_BW_UL_MU_MIMO;
8757 	eht_cap_elem->phy_cap_info[0] &= ~m;
8758 
8759 	m = IEEE80211_EHT_PHY_CAP3_NG_16_MU_FEEDBACK |
8760 	    IEEE80211_EHT_PHY_CAP3_CODEBOOK_7_5_MU_FDBK |
8761 	    IEEE80211_EHT_PHY_CAP3_TRIG_MU_BF_PART_BW_FDBK |
8762 	    IEEE80211_EHT_PHY_CAP3_TRIG_CQI_FDBK;
8763 	eht_cap_elem->phy_cap_info[3] &= ~m;
8764 
8765 	m = IEEE80211_EHT_PHY_CAP4_PART_BW_DL_MU_MIMO |
8766 	    IEEE80211_EHT_PHY_CAP4_PSR_SR_SUPP |
8767 	    IEEE80211_EHT_PHY_CAP4_POWER_BOOST_FACT_SUPP |
8768 	    IEEE80211_EHT_PHY_CAP4_EHT_MU_PPDU_4_EHT_LTF_08_GI;
8769 	eht_cap_elem->phy_cap_info[4] &= ~m;
8770 
8771 	m = IEEE80211_EHT_PHY_CAP5_NON_TRIG_CQI_FEEDBACK |
8772 	    IEEE80211_EHT_PHY_CAP5_TX_LESS_242_TONE_RU_SUPP |
8773 	    IEEE80211_EHT_PHY_CAP5_RX_LESS_242_TONE_RU_SUPP |
8774 	    IEEE80211_EHT_PHY_CAP5_MAX_NUM_SUPP_EHT_LTF_MASK;
8775 	eht_cap_elem->phy_cap_info[5] &= ~m;
8776 
8777 	m = IEEE80211_EHT_PHY_CAP6_MAX_NUM_SUPP_EHT_LTF_MASK;
8778 	eht_cap_elem->phy_cap_info[6] &= ~m;
8779 
8780 	m = IEEE80211_EHT_PHY_CAP7_NON_OFDMA_UL_MU_MIMO_80MHZ |
8781 	    IEEE80211_EHT_PHY_CAP7_NON_OFDMA_UL_MU_MIMO_160MHZ |
8782 	    IEEE80211_EHT_PHY_CAP7_NON_OFDMA_UL_MU_MIMO_320MHZ |
8783 	    IEEE80211_EHT_PHY_CAP7_MU_BEAMFORMER_80MHZ |
8784 	    IEEE80211_EHT_PHY_CAP7_MU_BEAMFORMER_160MHZ |
8785 	    IEEE80211_EHT_PHY_CAP7_MU_BEAMFORMER_320MHZ;
8786 	eht_cap_elem->phy_cap_info[7] &= ~m;
8787 }
8788 
8789 static void ath12k_mac_copy_eht_cap(struct ath12k *ar,
8790 				    struct ath12k_band_cap *band_cap,
8791 				    struct ieee80211_he_cap_elem *he_cap_elem,
8792 				    int iftype,
8793 				    struct ieee80211_sta_eht_cap *eht_cap)
8794 {
8795 	struct ieee80211_eht_cap_elem_fixed *eht_cap_elem = &eht_cap->eht_cap_elem;
8796 
8797 	memset(eht_cap, 0, sizeof(struct ieee80211_sta_eht_cap));
8798 
8799 	if (!(test_bit(WMI_TLV_SERVICE_11BE, ar->ab->wmi_ab.svc_map)) ||
8800 	    ath12k_acpi_get_disable_11be(ar->ab))
8801 		return;
8802 
8803 	eht_cap->has_eht = true;
8804 	memcpy(eht_cap_elem->mac_cap_info, band_cap->eht_cap_mac_info,
8805 	       sizeof(eht_cap_elem->mac_cap_info));
8806 	memcpy(eht_cap_elem->phy_cap_info, band_cap->eht_cap_phy_info,
8807 	       sizeof(eht_cap_elem->phy_cap_info));
8808 
8809 	switch (iftype) {
8810 	case NL80211_IFTYPE_AP:
8811 		eht_cap_elem->phy_cap_info[0] &=
8812 			~IEEE80211_EHT_PHY_CAP0_242_TONE_RU_GT20MHZ;
8813 		eht_cap_elem->phy_cap_info[4] &=
8814 			~IEEE80211_EHT_PHY_CAP4_PART_BW_DL_MU_MIMO;
8815 		eht_cap_elem->phy_cap_info[5] &=
8816 			~IEEE80211_EHT_PHY_CAP5_TX_LESS_242_TONE_RU_SUPP;
8817 		break;
8818 	case NL80211_IFTYPE_STATION:
8819 		eht_cap_elem->phy_cap_info[7] &=
8820 			~(IEEE80211_EHT_PHY_CAP7_NON_OFDMA_UL_MU_MIMO_80MHZ |
8821 			  IEEE80211_EHT_PHY_CAP7_NON_OFDMA_UL_MU_MIMO_160MHZ |
8822 			  IEEE80211_EHT_PHY_CAP7_NON_OFDMA_UL_MU_MIMO_320MHZ);
8823 		eht_cap_elem->phy_cap_info[7] &=
8824 			~(IEEE80211_EHT_PHY_CAP7_MU_BEAMFORMER_80MHZ |
8825 			  IEEE80211_EHT_PHY_CAP7_MU_BEAMFORMER_160MHZ |
8826 			  IEEE80211_EHT_PHY_CAP7_MU_BEAMFORMER_320MHZ);
8827 		break;
8828 	case NL80211_IFTYPE_MESH_POINT:
8829 		ath12k_mac_filter_eht_cap_mesh(eht_cap_elem);
8830 		break;
8831 	default:
8832 		break;
8833 	}
8834 
8835 	ath12k_mac_copy_eht_mcs_nss(band_cap, &eht_cap->eht_mcs_nss_supp,
8836 				    he_cap_elem, eht_cap_elem);
8837 
8838 	if (eht_cap_elem->phy_cap_info[5] &
8839 	    IEEE80211_EHT_PHY_CAP5_PPE_THRESHOLD_PRESENT)
8840 		ath12k_mac_copy_eht_ppe_thresh(&band_cap->eht_ppet, eht_cap);
8841 }
8842 
8843 static int ath12k_mac_copy_sband_iftype_data(struct ath12k *ar,
8844 					     struct ath12k_pdev_cap *cap,
8845 					     struct ieee80211_sband_iftype_data *data,
8846 					     int band)
8847 {
8848 	struct ath12k_band_cap *band_cap = &cap->band[band];
8849 	int i, idx = 0;
8850 
8851 	for (i = 0; i < NUM_NL80211_IFTYPES; i++) {
8852 		struct ieee80211_sta_he_cap *he_cap = &data[idx].he_cap;
8853 
8854 		switch (i) {
8855 		case NL80211_IFTYPE_STATION:
8856 		case NL80211_IFTYPE_AP:
8857 		case NL80211_IFTYPE_MESH_POINT:
8858 			break;
8859 
8860 		default:
8861 			continue;
8862 		}
8863 
8864 		data[idx].types_mask = BIT(i);
8865 
8866 		ath12k_mac_copy_he_cap(ar, band_cap, i, ar->num_tx_chains, he_cap);
8867 		if (band == NL80211_BAND_6GHZ) {
8868 			data[idx].he_6ghz_capa.capa =
8869 				ath12k_mac_setup_he_6ghz_cap(cap, band_cap);
8870 		}
8871 		ath12k_mac_copy_eht_cap(ar, band_cap, &he_cap->he_cap_elem, i,
8872 					&data[idx].eht_cap);
8873 		idx++;
8874 	}
8875 
8876 	return idx;
8877 }
8878 
8879 static void ath12k_mac_setup_sband_iftype_data(struct ath12k *ar,
8880 					       struct ath12k_pdev_cap *cap)
8881 {
8882 	struct ieee80211_supported_band *sband;
8883 	enum nl80211_band band;
8884 	int count;
8885 
8886 	if (cap->supported_bands & WMI_HOST_WLAN_2GHZ_CAP) {
8887 		band = NL80211_BAND_2GHZ;
8888 		count = ath12k_mac_copy_sband_iftype_data(ar, cap,
8889 							  ar->mac.iftype[band],
8890 							  band);
8891 		sband = &ar->mac.sbands[band];
8892 		_ieee80211_set_sband_iftype_data(sband, ar->mac.iftype[band],
8893 						 count);
8894 	}
8895 
8896 	if (cap->supported_bands & WMI_HOST_WLAN_5GHZ_CAP) {
8897 		band = NL80211_BAND_5GHZ;
8898 		count = ath12k_mac_copy_sband_iftype_data(ar, cap,
8899 							  ar->mac.iftype[band],
8900 							  band);
8901 		sband = &ar->mac.sbands[band];
8902 		_ieee80211_set_sband_iftype_data(sband, ar->mac.iftype[band],
8903 						 count);
8904 	}
8905 
8906 	if (cap->supported_bands & WMI_HOST_WLAN_5GHZ_CAP &&
8907 	    ar->supports_6ghz) {
8908 		band = NL80211_BAND_6GHZ;
8909 		count = ath12k_mac_copy_sband_iftype_data(ar, cap,
8910 							  ar->mac.iftype[band],
8911 							  band);
8912 		sband = &ar->mac.sbands[band];
8913 		_ieee80211_set_sband_iftype_data(sband, ar->mac.iftype[band],
8914 						 count);
8915 	}
8916 }
8917 
8918 static int __ath12k_set_antenna(struct ath12k *ar, u32 tx_ant, u32 rx_ant)
8919 {
8920 	struct ath12k_hw *ah = ath12k_ar_to_ah(ar);
8921 	int ret;
8922 
8923 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
8924 
8925 	if (ath12k_check_chain_mask(ar, tx_ant, true))
8926 		return -EINVAL;
8927 
8928 	if (ath12k_check_chain_mask(ar, rx_ant, false))
8929 		return -EINVAL;
8930 
8931 	/* Since we advertised the max cap of all radios combined during wiphy
8932 	 * registration, ensure we don't set the antenna config higher than the
8933 	 * limits
8934 	 */
8935 	tx_ant = min_t(u32, tx_ant, ar->pdev->cap.tx_chain_mask);
8936 	rx_ant = min_t(u32, rx_ant, ar->pdev->cap.rx_chain_mask);
8937 
8938 	ar->cfg_tx_chainmask = tx_ant;
8939 	ar->cfg_rx_chainmask = rx_ant;
8940 
8941 	if (ah->state != ATH12K_HW_STATE_ON &&
8942 	    ah->state != ATH12K_HW_STATE_RESTARTED)
8943 		return 0;
8944 
8945 	ret = ath12k_wmi_pdev_set_param(ar, WMI_PDEV_PARAM_TX_CHAIN_MASK,
8946 					tx_ant, ar->pdev->pdev_id);
8947 	if (ret) {
8948 		ath12k_warn(ar->ab, "failed to set tx-chainmask: %d, req 0x%x\n",
8949 			    ret, tx_ant);
8950 		return ret;
8951 	}
8952 
8953 	ar->num_tx_chains = hweight32(tx_ant);
8954 
8955 	ret = ath12k_wmi_pdev_set_param(ar, WMI_PDEV_PARAM_RX_CHAIN_MASK,
8956 					rx_ant, ar->pdev->pdev_id);
8957 	if (ret) {
8958 		ath12k_warn(ar->ab, "failed to set rx-chainmask: %d, req 0x%x\n",
8959 			    ret, rx_ant);
8960 		return ret;
8961 	}
8962 
8963 	ar->num_rx_chains = hweight32(rx_ant);
8964 
8965 	/* Reload HT/VHT/HE capability */
8966 	ath12k_mac_setup_ht_vht_cap(ar, &ar->pdev->cap, NULL);
8967 	ath12k_mac_setup_sband_iftype_data(ar, &ar->pdev->cap);
8968 
8969 	return 0;
8970 }
8971 
8972 static void ath12k_mgmt_over_wmi_tx_drop(struct ath12k *ar, struct sk_buff *skb)
8973 {
8974 	int num_mgmt;
8975 
8976 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
8977 
8978 	ieee80211_free_txskb(ath12k_ar_to_hw(ar), skb);
8979 
8980 	num_mgmt = atomic_dec_if_positive(&ar->num_pending_mgmt_tx);
8981 
8982 	if (num_mgmt < 0)
8983 		WARN_ON_ONCE(1);
8984 
8985 	if (!num_mgmt)
8986 		wake_up(&ar->txmgmt_empty_waitq);
8987 }
8988 
8989 static void ath12k_mac_tx_mgmt_free(struct ath12k *ar, int buf_id)
8990 {
8991 	struct sk_buff *msdu;
8992 	struct ieee80211_tx_info *info;
8993 
8994 	spin_lock_bh(&ar->txmgmt_idr_lock);
8995 	msdu = idr_remove(&ar->txmgmt_idr, buf_id);
8996 	spin_unlock_bh(&ar->txmgmt_idr_lock);
8997 
8998 	if (!msdu)
8999 		return;
9000 
9001 	dma_unmap_single(ar->ab->dev, ATH12K_SKB_CB(msdu)->paddr, msdu->len,
9002 			 DMA_TO_DEVICE);
9003 
9004 	info = IEEE80211_SKB_CB(msdu);
9005 	memset(&info->status, 0, sizeof(info->status));
9006 
9007 	ath12k_mgmt_over_wmi_tx_drop(ar, msdu);
9008 }
9009 
9010 int ath12k_mac_tx_mgmt_pending_free(int buf_id, void *skb, void *ctx)
9011 {
9012 	struct ath12k *ar = ctx;
9013 
9014 	ath12k_mac_tx_mgmt_free(ar, buf_id);
9015 
9016 	return 0;
9017 }
9018 
9019 static int ath12k_mac_vif_txmgmt_idr_remove(int buf_id, void *skb, void *ctx)
9020 {
9021 	struct ieee80211_vif *vif = ctx;
9022 	struct ath12k_skb_cb *skb_cb = ATH12K_SKB_CB(skb);
9023 	struct ath12k *ar = skb_cb->ar;
9024 
9025 	if (skb_cb->vif == vif)
9026 		ath12k_mac_tx_mgmt_free(ar, buf_id);
9027 
9028 	return 0;
9029 }
9030 
9031 static int ath12k_mac_mgmt_tx_wmi(struct ath12k *ar, struct ath12k_link_vif *arvif,
9032 				  struct sk_buff *skb)
9033 {
9034 	struct ath12k_base *ab = ar->ab;
9035 	struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
9036 	struct ath12k_skb_cb *skb_cb = ATH12K_SKB_CB(skb);
9037 	struct ieee80211_tx_info *info;
9038 	enum hal_encrypt_type enctype;
9039 	unsigned int mic_len;
9040 	dma_addr_t paddr;
9041 	int buf_id;
9042 	int ret;
9043 
9044 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
9045 
9046 	skb_cb->ar = ar;
9047 	spin_lock_bh(&ar->txmgmt_idr_lock);
9048 	buf_id = idr_alloc(&ar->txmgmt_idr, skb, 0,
9049 			   ATH12K_TX_MGMT_NUM_PENDING_MAX, GFP_ATOMIC);
9050 	spin_unlock_bh(&ar->txmgmt_idr_lock);
9051 	if (buf_id < 0)
9052 		return -ENOSPC;
9053 
9054 	info = IEEE80211_SKB_CB(skb);
9055 	if ((skb_cb->flags & ATH12K_SKB_CIPHER_SET) &&
9056 	    !(info->flags & IEEE80211_TX_CTL_HW_80211_ENCAP)) {
9057 		if ((ieee80211_is_action(hdr->frame_control) ||
9058 		     ieee80211_is_deauth(hdr->frame_control) ||
9059 		     ieee80211_is_disassoc(hdr->frame_control)) &&
9060 		     ieee80211_has_protected(hdr->frame_control)) {
9061 			enctype = ath12k_dp_tx_get_encrypt_type(skb_cb->cipher);
9062 			mic_len = ath12k_dp_rx_crypto_mic_len(ab->dp, enctype);
9063 			skb_put(skb, mic_len);
9064 		}
9065 	}
9066 
9067 	paddr = dma_map_single(ab->dev, skb->data, skb->len, DMA_TO_DEVICE);
9068 	if (dma_mapping_error(ab->dev, paddr)) {
9069 		ath12k_warn(ab, "failed to DMA map mgmt Tx buffer\n");
9070 		ret = -EIO;
9071 		goto err_free_idr;
9072 	}
9073 
9074 	skb_cb->paddr = paddr;
9075 
9076 	ret = ath12k_wmi_mgmt_send(arvif, buf_id, skb);
9077 	if (ret) {
9078 		ath12k_warn(ar->ab, "failed to send mgmt frame: %d\n", ret);
9079 		goto err_unmap_buf;
9080 	}
9081 
9082 	return 0;
9083 
9084 err_unmap_buf:
9085 	dma_unmap_single(ab->dev, skb_cb->paddr,
9086 			 skb->len, DMA_TO_DEVICE);
9087 err_free_idr:
9088 	spin_lock_bh(&ar->txmgmt_idr_lock);
9089 	idr_remove(&ar->txmgmt_idr, buf_id);
9090 	spin_unlock_bh(&ar->txmgmt_idr_lock);
9091 
9092 	return ret;
9093 }
9094 
9095 static void ath12k_mgmt_over_wmi_tx_purge(struct ath12k *ar)
9096 {
9097 	struct sk_buff *skb;
9098 
9099 	while ((skb = skb_dequeue(&ar->wmi_mgmt_tx_queue)) != NULL)
9100 		ath12k_mgmt_over_wmi_tx_drop(ar, skb);
9101 }
9102 
9103 static int ath12k_mac_mgmt_action_frame_fill_elem_data(struct ath12k_link_vif *arvif,
9104 						       struct sk_buff *skb)
9105 {
9106 	struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
9107 	u8 category, *buf, iv_len, action_code, dialog_token;
9108 	struct ieee80211_bss_conf *link_conf;
9109 	struct ieee80211_chanctx_conf *conf;
9110 	int cur_tx_power, max_tx_power;
9111 	struct ath12k *ar = arvif->ar;
9112 	struct ieee80211_hw *hw = ath12k_ar_to_hw(ar);
9113 	struct wiphy *wiphy = hw->wiphy;
9114 	struct ath12k_skb_cb *skb_cb;
9115 	struct ieee80211_mgmt *mgmt;
9116 	unsigned int remaining_len;
9117 	bool has_protected;
9118 
9119 	lockdep_assert_wiphy(wiphy);
9120 
9121 	/* make sure category field is present */
9122 	if (skb->len < IEEE80211_MIN_ACTION_SIZE)
9123 		return -EINVAL;
9124 
9125 	remaining_len = skb->len - IEEE80211_MIN_ACTION_SIZE;
9126 	has_protected = ieee80211_has_protected(hdr->frame_control);
9127 
9128 	/* In case of SW crypto and hdr protected (PMF), packet will already be encrypted,
9129 	 * we can't put in data in this case
9130 	 */
9131 	if (test_bit(ATH12K_FLAG_HW_CRYPTO_DISABLED, &ar->ab->dev_flags) &&
9132 	    has_protected)
9133 		return 0;
9134 
9135 	mgmt = (struct ieee80211_mgmt *)hdr;
9136 	buf = (u8 *)&mgmt->u.action;
9137 
9138 	/* FCTL_PROTECTED frame might have extra space added for HDR_LEN. Offset that
9139 	 * many bytes if it is there
9140 	 */
9141 	if (has_protected) {
9142 		skb_cb = ATH12K_SKB_CB(skb);
9143 
9144 		switch (skb_cb->cipher) {
9145 		/* Cipher suite having flag %IEEE80211_KEY_FLAG_GENERATE_IV_MGMT set in
9146 		 * key needs to be processed. See ath12k_install_key()
9147 		 */
9148 		case WLAN_CIPHER_SUITE_CCMP:
9149 		case WLAN_CIPHER_SUITE_CCMP_256:
9150 		case WLAN_CIPHER_SUITE_GCMP:
9151 		case WLAN_CIPHER_SUITE_GCMP_256:
9152 			iv_len = IEEE80211_CCMP_HDR_LEN;
9153 			break;
9154 		case WLAN_CIPHER_SUITE_TKIP:
9155 			iv_len = 0;
9156 			break;
9157 		default:
9158 			return -EINVAL;
9159 		}
9160 
9161 		if (remaining_len < iv_len)
9162 			return -EINVAL;
9163 
9164 		buf += iv_len;
9165 		remaining_len -= iv_len;
9166 	}
9167 
9168 	category = *buf++;
9169 	/* category code is already taken care in %IEEE80211_MIN_ACTION_SIZE hence
9170 	 * no need to adjust remaining_len
9171 	 */
9172 
9173 	switch (category) {
9174 	case WLAN_CATEGORY_RADIO_MEASUREMENT:
9175 		/* need action code and dialog token */
9176 		if (remaining_len < 2)
9177 			return -EINVAL;
9178 
9179 		/* Packet Format:
9180 		 *	Action Code | Dialog Token | Variable Len (based on Action Code)
9181 		 */
9182 		action_code = *buf++;
9183 		dialog_token = *buf++;
9184 		remaining_len -= 2;
9185 
9186 		link_conf = ath12k_mac_get_link_bss_conf(arvif);
9187 		if (!link_conf) {
9188 			ath12k_warn(ar->ab,
9189 				    "failed to get bss link conf for vdev %d in RM handling\n",
9190 				    arvif->vdev_id);
9191 			return -EINVAL;
9192 		}
9193 
9194 		conf = wiphy_dereference(wiphy, link_conf->chanctx_conf);
9195 		if (!conf)
9196 			return -ENOENT;
9197 
9198 		cur_tx_power = link_conf->txpower;
9199 		max_tx_power = min(conf->def.chan->max_reg_power,
9200 				   (int)ar->max_tx_power / 2);
9201 
9202 		ath12k_mac_op_get_txpower(hw, arvif->ahvif->vif, arvif->link_id,
9203 					  &cur_tx_power);
9204 
9205 		switch (action_code) {
9206 		case WLAN_RM_ACTION_LINK_MEASUREMENT_REQUEST:
9207 			/* need variable fields to be present in len */
9208 			if (remaining_len < 2)
9209 				return -EINVAL;
9210 
9211 			/* Variable length format as defined in IEEE 802.11-2024,
9212 			 * Figure 9-1187-Link Measurement Request frame Action field
9213 			 * format.
9214 			 *	Transmit Power | Max Tx Power
9215 			 * We fill both of these.
9216 			 */
9217 			*buf++ = cur_tx_power;
9218 			*buf = max_tx_power;
9219 
9220 			ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
9221 				   "RRM: Link Measurement Req dialog_token %u cur_tx_power %d max_tx_power %d\n",
9222 				   dialog_token, cur_tx_power, max_tx_power);
9223 			break;
9224 		case WLAN_RM_ACTION_LINK_MEASUREMENT_REPORT:
9225 			/* need variable fields to be present in len */
9226 			if (remaining_len < 3)
9227 				return -EINVAL;
9228 
9229 			/* Variable length format as defined in IEEE 802.11-2024,
9230 			 * Figure 9-1188-Link Measurement Report frame Action field format
9231 			 *	TPC Report | Variable Fields
9232 			 *
9233 			 * TPC Report Format:
9234 			 *	Element ID | Len | Tx Power | Link Margin
9235 			 *
9236 			 * We fill Tx power in the TPC Report (2nd index)
9237 			 */
9238 			buf[2] = cur_tx_power;
9239 
9240 			/* TODO: At present, Link margin data is not present so can't
9241 			 * really fill it now. Once it is available, it can be added
9242 			 * here
9243 			 */
9244 			ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
9245 				   "RRM: Link Measurement Report dialog_token %u cur_tx_power %d\n",
9246 				   dialog_token, cur_tx_power);
9247 			break;
9248 		default:
9249 			return -EINVAL;
9250 		}
9251 		break;
9252 	default:
9253 		/* nothing to fill */
9254 		return 0;
9255 	}
9256 
9257 	return 0;
9258 }
9259 
9260 static int ath12k_mac_mgmt_frame_fill_elem_data(struct ath12k_link_vif *arvif,
9261 						struct sk_buff *skb)
9262 {
9263 	struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
9264 
9265 	if (!ieee80211_is_action(hdr->frame_control))
9266 		return 0;
9267 
9268 	return ath12k_mac_mgmt_action_frame_fill_elem_data(arvif, skb);
9269 }
9270 
9271 static void ath12k_mgmt_over_wmi_tx_work(struct wiphy *wiphy, struct wiphy_work *work)
9272 {
9273 	struct ath12k *ar = container_of(work, struct ath12k, wmi_mgmt_tx_work);
9274 	struct ath12k_hw *ah = ar->ah;
9275 	struct ath12k_skb_cb *skb_cb;
9276 	struct ath12k_vif *ahvif;
9277 	struct ath12k_link_vif *arvif;
9278 	struct sk_buff *skb;
9279 	int ret;
9280 
9281 	lockdep_assert_wiphy(wiphy);
9282 
9283 	while ((skb = skb_dequeue(&ar->wmi_mgmt_tx_queue)) != NULL) {
9284 		skb_cb = ATH12K_SKB_CB(skb);
9285 		if (!skb_cb->vif) {
9286 			ath12k_warn(ar->ab, "no vif found for mgmt frame\n");
9287 			ath12k_mgmt_over_wmi_tx_drop(ar, skb);
9288 			continue;
9289 		}
9290 
9291 		ahvif = ath12k_vif_to_ahvif(skb_cb->vif);
9292 		if (!(ahvif->links_map & BIT(skb_cb->link_id))) {
9293 			ath12k_warn(ar->ab,
9294 				    "invalid linkid %u in mgmt over wmi tx with linkmap 0x%x\n",
9295 				    skb_cb->link_id, ahvif->links_map);
9296 			ath12k_mgmt_over_wmi_tx_drop(ar, skb);
9297 			continue;
9298 		}
9299 
9300 		arvif = wiphy_dereference(ah->hw->wiphy, ahvif->link[skb_cb->link_id]);
9301 		if (ar->allocated_vdev_map & (1LL << arvif->vdev_id)) {
9302 			/* Fill in the data which is required to be filled by the driver
9303 			 * For example: Max Tx power in Link Measurement Request/Report
9304 			 */
9305 			ret = ath12k_mac_mgmt_frame_fill_elem_data(arvif, skb);
9306 			if (ret) {
9307 				/* If we couldn't fill the data due to any reason,
9308 				 * let's not discard transmitting the packet.
9309 				 * For example: Software crypto and PMF case
9310 				 */
9311 				ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
9312 					   "Failed to fill the required data for the mgmt packet err %d\n",
9313 					   ret);
9314 			}
9315 
9316 			ret = ath12k_mac_mgmt_tx_wmi(ar, arvif, skb);
9317 			if (ret) {
9318 				ath12k_warn(ar->ab, "failed to tx mgmt frame, vdev_id %d :%d\n",
9319 					    arvif->vdev_id, ret);
9320 				ath12k_mgmt_over_wmi_tx_drop(ar, skb);
9321 			}
9322 		} else {
9323 			ath12k_warn(ar->ab,
9324 				    "dropping mgmt frame for vdev %d link %u is_started %d\n",
9325 				    arvif->vdev_id,
9326 				    skb_cb->link_id,
9327 				    arvif->is_started);
9328 			ath12k_mgmt_over_wmi_tx_drop(ar, skb);
9329 		}
9330 	}
9331 }
9332 
9333 int ath12k_mac_mgmt_tx(struct ath12k *ar, struct sk_buff *skb,
9334 		       bool is_prb_rsp)
9335 {
9336 	struct sk_buff_head *q = &ar->wmi_mgmt_tx_queue;
9337 
9338 	if (test_bit(ATH12K_FLAG_CRASH_FLUSH, &ar->ab->dev_flags))
9339 		return -ESHUTDOWN;
9340 
9341 	/* Drop probe response packets when the pending management tx
9342 	 * count has reached a certain threshold, so as to prioritize
9343 	 * other mgmt packets like auth and assoc to be sent on time
9344 	 * for establishing successful connections.
9345 	 */
9346 	if (is_prb_rsp &&
9347 	    atomic_read(&ar->num_pending_mgmt_tx) > ATH12K_PRB_RSP_DROP_THRESHOLD) {
9348 		ath12k_warn(ar->ab,
9349 			    "dropping probe response as pending queue is almost full\n");
9350 		return -ENOSPC;
9351 	}
9352 
9353 	if (skb_queue_len_lockless(q) >= ATH12K_TX_MGMT_NUM_PENDING_MAX) {
9354 		ath12k_warn(ar->ab, "mgmt tx queue is full\n");
9355 		return -ENOSPC;
9356 	}
9357 
9358 	skb_queue_tail(q, skb);
9359 	atomic_inc(&ar->num_pending_mgmt_tx);
9360 	wiphy_work_queue(ath12k_ar_to_hw(ar)->wiphy, &ar->wmi_mgmt_tx_work);
9361 
9362 	return 0;
9363 }
9364 EXPORT_SYMBOL(ath12k_mac_mgmt_tx);
9365 
9366 void ath12k_mac_add_p2p_noa_ie(struct ath12k *ar,
9367 			       struct ieee80211_vif *vif,
9368 			       struct sk_buff *skb,
9369 			       bool is_prb_rsp)
9370 {
9371 	struct ath12k_vif *ahvif = ath12k_vif_to_ahvif(vif);
9372 
9373 	if (likely(!is_prb_rsp))
9374 		return;
9375 
9376 	spin_lock_bh(&ar->data_lock);
9377 
9378 	if (ahvif->u.ap.noa_data &&
9379 	    !pskb_expand_head(skb, 0, ahvif->u.ap.noa_len,
9380 			      GFP_ATOMIC))
9381 		skb_put_data(skb, ahvif->u.ap.noa_data,
9382 			     ahvif->u.ap.noa_len);
9383 
9384 	spin_unlock_bh(&ar->data_lock);
9385 }
9386 EXPORT_SYMBOL(ath12k_mac_add_p2p_noa_ie);
9387 
9388 /* Note: called under rcu_read_lock() */
9389 void ath12k_mlo_mcast_update_tx_link_address(struct ieee80211_vif *vif,
9390 					     u8 link_id, struct sk_buff *skb,
9391 					     u32 info_flags)
9392 {
9393 	struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
9394 	struct ieee80211_bss_conf *bss_conf;
9395 
9396 	if (info_flags & IEEE80211_TX_CTL_HW_80211_ENCAP)
9397 		return;
9398 
9399 	bss_conf = rcu_dereference(vif->link_conf[link_id]);
9400 	if (bss_conf)
9401 		ether_addr_copy(hdr->addr2, bss_conf->addr);
9402 }
9403 EXPORT_SYMBOL(ath12k_mlo_mcast_update_tx_link_address);
9404 
9405 /* Note: called under rcu_read_lock() */
9406 u8 ath12k_mac_get_tx_link(struct ieee80211_sta *sta, struct ieee80211_vif *vif,
9407 			  u8 link, struct sk_buff *skb, u32 info_flags)
9408 {
9409 	struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
9410 	struct ath12k_vif *ahvif = ath12k_vif_to_ahvif(vif);
9411 	struct ieee80211_link_sta *link_sta;
9412 	struct ieee80211_bss_conf *bss_conf;
9413 	struct ath12k_sta *ahsta;
9414 
9415 	/* Use the link id passed or the default vif link */
9416 	if (!sta) {
9417 		if (link != IEEE80211_LINK_UNSPECIFIED)
9418 			return link;
9419 
9420 		return ahvif->deflink.link_id;
9421 	}
9422 
9423 	ahsta = ath12k_sta_to_ahsta(sta);
9424 
9425 	/* Below translation ensures we pass proper A2 & A3 for non ML clients.
9426 	 * Also it assumes for now support only for MLO AP in this path
9427 	 */
9428 	if (!sta->mlo) {
9429 		link = ahsta->deflink.link_id;
9430 
9431 		if (info_flags & IEEE80211_TX_CTL_HW_80211_ENCAP)
9432 			return link;
9433 
9434 		bss_conf = rcu_dereference(vif->link_conf[link]);
9435 		if (bss_conf) {
9436 			ether_addr_copy(hdr->addr2, bss_conf->addr);
9437 			if (!ieee80211_has_tods(hdr->frame_control) &&
9438 			    !ieee80211_has_fromds(hdr->frame_control))
9439 				ether_addr_copy(hdr->addr3, bss_conf->addr);
9440 		}
9441 
9442 		return link;
9443 	}
9444 
9445 	/* enqueue eth enacap & data frames on primary link, FW does link
9446 	 * selection and address translation.
9447 	 */
9448 	if (info_flags & IEEE80211_TX_CTL_HW_80211_ENCAP ||
9449 	    ieee80211_is_data(hdr->frame_control))
9450 		return ahsta->assoc_link_id;
9451 
9452 	/* 802.11 frame cases */
9453 	if (link == IEEE80211_LINK_UNSPECIFIED)
9454 		link = ahsta->deflink.link_id;
9455 
9456 	if (!ieee80211_is_mgmt(hdr->frame_control))
9457 		return link;
9458 
9459 	/* Perform address conversion for ML STA Tx */
9460 	bss_conf = rcu_dereference(vif->link_conf[link]);
9461 	link_sta = rcu_dereference(sta->link[link]);
9462 
9463 	if (bss_conf && link_sta) {
9464 		ether_addr_copy(hdr->addr1, link_sta->addr);
9465 		ether_addr_copy(hdr->addr2, bss_conf->addr);
9466 
9467 		if (vif->type == NL80211_IFTYPE_STATION && bss_conf->bssid)
9468 			ether_addr_copy(hdr->addr3, bss_conf->bssid);
9469 		else if (vif->type == NL80211_IFTYPE_AP)
9470 			ether_addr_copy(hdr->addr3, bss_conf->addr);
9471 
9472 		return link;
9473 	}
9474 
9475 	if (bss_conf) {
9476 		/* In certain cases where a ML sta associated and added subset of
9477 		 * links on which the ML AP is active, but now sends some frame
9478 		 * (ex. Probe request) on a different link which is active in our
9479 		 * MLD but was not added during previous association, we can
9480 		 * still honor the Tx to that ML STA via the requested link.
9481 		 * The control would reach here in such case only when that link
9482 		 * address is same as the MLD address or in worst case clients
9483 		 * used MLD address at TA wrongly which would have helped
9484 		 * identify the ML sta object and pass it here.
9485 		 * If the link address of that STA is different from MLD address,
9486 		 * then the sta object would be NULL and control won't reach
9487 		 * here but return at the start of the function itself with !sta
9488 		 * check. Also this would not need any translation at hdr->addr1
9489 		 * from MLD to link address since the RA is the MLD address
9490 		 * (same as that link address ideally) already.
9491 		 */
9492 		ether_addr_copy(hdr->addr2, bss_conf->addr);
9493 
9494 		if (vif->type == NL80211_IFTYPE_STATION && bss_conf->bssid)
9495 			ether_addr_copy(hdr->addr3, bss_conf->bssid);
9496 		else if (vif->type == NL80211_IFTYPE_AP)
9497 			ether_addr_copy(hdr->addr3, bss_conf->addr);
9498 	}
9499 
9500 	return link;
9501 }
9502 EXPORT_SYMBOL(ath12k_mac_get_tx_link);
9503 
9504 void ath12k_mac_drain_tx(struct ath12k *ar)
9505 {
9506 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
9507 
9508 	/* make sure rcu-protected mac80211 tx path itself is drained */
9509 	synchronize_net();
9510 
9511 	wiphy_work_cancel(ath12k_ar_to_hw(ar)->wiphy, &ar->wmi_mgmt_tx_work);
9512 	ath12k_mgmt_over_wmi_tx_purge(ar);
9513 }
9514 
9515 static int ath12k_mac_config_mon_status_default(struct ath12k *ar, bool enable)
9516 {
9517 	struct htt_rx_ring_tlv_filter tlv_filter = {};
9518 	struct ath12k_base *ab = ar->ab;
9519 	u32 ring_id, i;
9520 	int ret = 0;
9521 
9522 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
9523 
9524 	if (!ab->hw_params->rxdma1_enable)
9525 		return ret;
9526 
9527 	if (enable) {
9528 		tlv_filter = ath12k_mac_mon_status_filter_default;
9529 
9530 		if (ath12k_debugfs_rx_filter(ar))
9531 			tlv_filter.rx_filter = ath12k_debugfs_rx_filter(ar);
9532 	} else {
9533 		tlv_filter.rxmon_disable = true;
9534 	}
9535 
9536 	for (i = 0; i < ab->hw_params->num_rxdma_per_pdev; i++) {
9537 		ring_id = ar->dp.rxdma_mon_dst_ring[i].ring_id;
9538 		ret = ath12k_dp_tx_htt_rx_filter_setup(ab, ring_id,
9539 						       ar->dp.mac_id + i,
9540 						       HAL_RXDMA_MONITOR_DST,
9541 						       DP_RXDMA_REFILL_RING_SIZE,
9542 						       &tlv_filter);
9543 		if (ret) {
9544 			ath12k_err(ab,
9545 				   "failed to setup filter for monitor buf %d\n",
9546 				   ret);
9547 		}
9548 	}
9549 
9550 	return ret;
9551 }
9552 
9553 static int ath12k_mac_start(struct ath12k *ar)
9554 {
9555 	struct ath12k_hw *ah = ar->ah;
9556 	struct ath12k_base *ab = ar->ab;
9557 	struct ath12k_pdev *pdev = ar->pdev;
9558 	int ret;
9559 
9560 	lockdep_assert_held(&ah->hw_mutex);
9561 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
9562 
9563 	ret = ath12k_wmi_pdev_set_param(ar, WMI_PDEV_PARAM_PMF_QOS,
9564 					1, pdev->pdev_id);
9565 
9566 	if (ret) {
9567 		ath12k_err(ab, "failed to enable PMF QOS: %d\n", ret);
9568 		goto err;
9569 	}
9570 
9571 	ret = ath12k_wmi_pdev_set_param(ar, WMI_PDEV_PARAM_DYNAMIC_BW, 1,
9572 					pdev->pdev_id);
9573 	if (ret) {
9574 		ath12k_err(ab, "failed to enable dynamic bw: %d\n", ret);
9575 		goto err;
9576 	}
9577 
9578 	ret = ath12k_wmi_pdev_set_param(ar, WMI_PDEV_PARAM_ARP_AC_OVERRIDE,
9579 					0, pdev->pdev_id);
9580 	if (ret) {
9581 		ath12k_err(ab, "failed to set ac override for ARP: %d\n",
9582 			   ret);
9583 		goto err;
9584 	}
9585 
9586 	ret = ath12k_wmi_send_dfs_phyerr_offload_enable_cmd(ar, pdev->pdev_id);
9587 	if (ret) {
9588 		ath12k_err(ab, "failed to offload radar detection: %d\n",
9589 			   ret);
9590 		goto err;
9591 	}
9592 
9593 	ret = ath12k_dp_tx_htt_h2t_ppdu_stats_req(ar,
9594 						  HTT_PPDU_STATS_TAG_DEFAULT);
9595 	if (ret) {
9596 		ath12k_err(ab, "failed to req ppdu stats: %d\n", ret);
9597 		goto err;
9598 	}
9599 
9600 	ret = ath12k_wmi_pdev_set_param(ar, WMI_PDEV_PARAM_MESH_MCAST_ENABLE,
9601 					1, pdev->pdev_id);
9602 
9603 	if (ret) {
9604 		ath12k_err(ab, "failed to enable MESH MCAST ENABLE: (%d\n", ret);
9605 		goto err;
9606 	}
9607 
9608 	__ath12k_set_antenna(ar, ar->cfg_tx_chainmask, ar->cfg_rx_chainmask);
9609 
9610 	/* TODO: Do we need to enable ANI? */
9611 
9612 	ret = ath12k_reg_update_chan_list(ar, false);
9613 
9614 	/* The ar state alone can be turned off for non supported country
9615 	 * without returning the error value. As we need to update the channel
9616 	 * for the next ar.
9617 	 */
9618 	if (ret) {
9619 		if (ret == -EINVAL)
9620 			ret = 0;
9621 		goto err;
9622 	}
9623 
9624 	ar->num_started_vdevs = 0;
9625 	ar->num_created_vdevs = 0;
9626 	ar->num_peers = 0;
9627 	ar->allocated_vdev_map = 0;
9628 	ar->chan_tx_pwr = ATH12K_PDEV_TX_POWER_INVALID;
9629 
9630 	/* Configure monitor status ring with default rx_filter to get rx status
9631 	 * such as rssi, rx_duration.
9632 	 */
9633 	ret = ath12k_mac_config_mon_status_default(ar, true);
9634 	if (ret && (ret != -EOPNOTSUPP)) {
9635 		ath12k_err(ab, "failed to configure monitor status ring with default rx_filter: (%d)\n",
9636 			   ret);
9637 		goto err;
9638 	}
9639 
9640 	if (ret == -EOPNOTSUPP)
9641 		ath12k_dbg(ab, ATH12K_DBG_MAC,
9642 			   "monitor status config is not yet supported");
9643 
9644 	/* Configure the hash seed for hash based reo dest ring selection */
9645 	ath12k_wmi_pdev_lro_cfg(ar, ar->pdev->pdev_id);
9646 
9647 	/* allow device to enter IMPS */
9648 	if (ab->hw_params->idle_ps) {
9649 		ret = ath12k_wmi_pdev_set_param(ar, WMI_PDEV_PARAM_IDLE_PS_CONFIG,
9650 						1, pdev->pdev_id);
9651 		if (ret) {
9652 			ath12k_err(ab, "failed to enable idle ps: %d\n", ret);
9653 			goto err;
9654 		}
9655 	}
9656 
9657 	rcu_assign_pointer(ab->pdevs_active[ar->pdev_idx],
9658 			   &ab->pdevs[ar->pdev_idx]);
9659 
9660 	return 0;
9661 err:
9662 
9663 	return ret;
9664 }
9665 
9666 static void ath12k_drain_tx(struct ath12k_hw *ah)
9667 {
9668 	struct ath12k *ar;
9669 	int i;
9670 
9671 	lockdep_assert_wiphy(ah->hw->wiphy);
9672 
9673 	for_each_ar(ah, ar, i)
9674 		ath12k_mac_drain_tx(ar);
9675 }
9676 
9677 int ath12k_mac_op_start(struct ieee80211_hw *hw)
9678 {
9679 	struct ath12k_hw *ah = ath12k_hw_to_ah(hw);
9680 	struct ath12k *ar;
9681 	int ret, i;
9682 
9683 	if (ath12k_ftm_mode)
9684 		return -EPERM;
9685 
9686 	lockdep_assert_wiphy(hw->wiphy);
9687 
9688 	ath12k_drain_tx(ah);
9689 
9690 	guard(mutex)(&ah->hw_mutex);
9691 
9692 	switch (ah->state) {
9693 	case ATH12K_HW_STATE_OFF:
9694 		ah->state = ATH12K_HW_STATE_ON;
9695 		break;
9696 	case ATH12K_HW_STATE_RESTARTING:
9697 		ah->state = ATH12K_HW_STATE_RESTARTED;
9698 		break;
9699 	case ATH12K_HW_STATE_RESTARTED:
9700 	case ATH12K_HW_STATE_WEDGED:
9701 	case ATH12K_HW_STATE_ON:
9702 	case ATH12K_HW_STATE_TM:
9703 		ah->state = ATH12K_HW_STATE_OFF;
9704 
9705 		WARN_ON(1);
9706 		return -EINVAL;
9707 	}
9708 
9709 	for_each_ar(ah, ar, i) {
9710 		ret = ath12k_mac_start(ar);
9711 		if (ret) {
9712 			ah->state = ATH12K_HW_STATE_OFF;
9713 
9714 			ath12k_err(ar->ab, "fail to start mac operations in pdev idx %d ret %d\n",
9715 				   ar->pdev_idx, ret);
9716 			goto fail_start;
9717 		}
9718 	}
9719 
9720 	return 0;
9721 
9722 fail_start:
9723 	for (; i > 0; i--) {
9724 		ar = ath12k_ah_to_ar(ah, i - 1);
9725 		ath12k_mac_stop(ar);
9726 	}
9727 
9728 	return ret;
9729 }
9730 EXPORT_SYMBOL(ath12k_mac_op_start);
9731 
9732 int ath12k_mac_rfkill_config(struct ath12k *ar)
9733 {
9734 	struct ath12k_base *ab = ar->ab;
9735 	u32 param;
9736 	int ret;
9737 
9738 	if (ab->hw_params->rfkill_pin == 0)
9739 		return -EOPNOTSUPP;
9740 
9741 	ath12k_dbg(ab, ATH12K_DBG_MAC,
9742 		   "mac rfkill_pin %d rfkill_cfg %d rfkill_on_level %d",
9743 		   ab->hw_params->rfkill_pin, ab->hw_params->rfkill_cfg,
9744 		   ab->hw_params->rfkill_on_level);
9745 
9746 	param = u32_encode_bits(ab->hw_params->rfkill_on_level,
9747 				WMI_RFKILL_CFG_RADIO_LEVEL) |
9748 		u32_encode_bits(ab->hw_params->rfkill_pin,
9749 				WMI_RFKILL_CFG_GPIO_PIN_NUM) |
9750 		u32_encode_bits(ab->hw_params->rfkill_cfg,
9751 				WMI_RFKILL_CFG_PIN_AS_GPIO);
9752 
9753 	ret = ath12k_wmi_pdev_set_param(ar, WMI_PDEV_PARAM_HW_RFKILL_CONFIG,
9754 					param, ar->pdev->pdev_id);
9755 	if (ret) {
9756 		ath12k_warn(ab,
9757 			    "failed to set rfkill config 0x%x: %d\n",
9758 			    param, ret);
9759 		return ret;
9760 	}
9761 
9762 	return 0;
9763 }
9764 
9765 int ath12k_mac_rfkill_enable_radio(struct ath12k *ar, bool enable)
9766 {
9767 	enum wmi_rfkill_enable_radio param;
9768 	int ret;
9769 
9770 	if (enable)
9771 		param = WMI_RFKILL_ENABLE_RADIO_ON;
9772 	else
9773 		param = WMI_RFKILL_ENABLE_RADIO_OFF;
9774 
9775 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac %d rfkill enable %d",
9776 		   ar->pdev_idx, param);
9777 
9778 	ret = ath12k_wmi_pdev_set_param(ar, WMI_PDEV_PARAM_RFKILL_ENABLE,
9779 					param, ar->pdev->pdev_id);
9780 	if (ret) {
9781 		ath12k_warn(ar->ab, "failed to set rfkill enable param %d: %d\n",
9782 			    param, ret);
9783 		return ret;
9784 	}
9785 
9786 	return 0;
9787 }
9788 
9789 static void ath12k_mac_stop(struct ath12k *ar)
9790 {
9791 	struct ath12k_pdev_dp *dp_pdev = &ar->dp;
9792 	struct ath12k_hw *ah = ar->ah;
9793 	struct htt_ppdu_stats_info *ppdu_stats, *tmp;
9794 	struct ath12k_wmi_scan_chan_list_arg *arg;
9795 	int ret;
9796 
9797 	lockdep_assert_held(&ah->hw_mutex);
9798 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
9799 
9800 	ret = ath12k_mac_config_mon_status_default(ar, false);
9801 	if (ret && (ret != -EOPNOTSUPP))
9802 		ath12k_err(ar->ab, "failed to clear rx_filter for monitor status ring: (%d)\n",
9803 			   ret);
9804 
9805 	clear_bit(ATH12K_FLAG_CAC_RUNNING, &ar->dev_flags);
9806 
9807 	cancel_delayed_work_sync(&ar->scan.timeout);
9808 	wiphy_work_cancel(ath12k_ar_to_hw(ar)->wiphy, &ar->scan.vdev_clean_wk);
9809 	cancel_work_sync(&ar->regd_channel_update_work);
9810 	cancel_work_sync(&ar->regd_update_work);
9811 	cancel_work_sync(&ar->ab->rfkill_work);
9812 	cancel_work_sync(&ar->ab->update_11d_work);
9813 	ar->state_11d = ATH12K_11D_IDLE;
9814 	complete(&ar->completed_11d_scan);
9815 
9816 	spin_lock_bh(&dp_pdev->ppdu_list_lock);
9817 	list_for_each_entry_safe(ppdu_stats, tmp, &dp_pdev->ppdu_stats_info, list) {
9818 		list_del(&ppdu_stats->list);
9819 		kfree(ppdu_stats);
9820 	}
9821 	spin_unlock_bh(&dp_pdev->ppdu_list_lock);
9822 
9823 	spin_lock_bh(&ar->data_lock);
9824 	while ((arg = list_first_entry_or_null(&ar->regd_channel_update_queue,
9825 					       struct ath12k_wmi_scan_chan_list_arg,
9826 					       list))) {
9827 		list_del(&arg->list);
9828 		kfree(arg);
9829 	}
9830 	spin_unlock_bh(&ar->data_lock);
9831 
9832 	rcu_assign_pointer(ar->ab->pdevs_active[ar->pdev_idx], NULL);
9833 
9834 	synchronize_rcu();
9835 
9836 	atomic_set(&ar->num_pending_mgmt_tx, 0);
9837 }
9838 
9839 void ath12k_mac_op_stop(struct ieee80211_hw *hw, bool suspend)
9840 {
9841 	struct ath12k_hw *ah = ath12k_hw_to_ah(hw);
9842 	struct ath12k *ar;
9843 	int i;
9844 
9845 	lockdep_assert_wiphy(hw->wiphy);
9846 
9847 	ath12k_drain_tx(ah);
9848 
9849 	mutex_lock(&ah->hw_mutex);
9850 
9851 	ah->state = ATH12K_HW_STATE_OFF;
9852 
9853 	for_each_ar(ah, ar, i)
9854 		ath12k_mac_stop(ar);
9855 
9856 	mutex_unlock(&ah->hw_mutex);
9857 }
9858 EXPORT_SYMBOL(ath12k_mac_op_stop);
9859 
9860 static u8
9861 ath12k_mac_get_vdev_stats_id(struct ath12k_link_vif *arvif)
9862 {
9863 	struct ath12k_base *ab = arvif->ar->ab;
9864 	u8 vdev_stats_id = 0;
9865 
9866 	do {
9867 		if (ab->free_vdev_stats_id_map & (1LL << vdev_stats_id)) {
9868 			vdev_stats_id++;
9869 			if (vdev_stats_id >= ATH12K_MAX_VDEV_STATS_ID) {
9870 				vdev_stats_id = ATH12K_INVAL_VDEV_STATS_ID;
9871 				break;
9872 			}
9873 		} else {
9874 			ab->free_vdev_stats_id_map |= (1LL << vdev_stats_id);
9875 			break;
9876 		}
9877 	} while (vdev_stats_id);
9878 
9879 	arvif->vdev_stats_id = vdev_stats_id;
9880 	return vdev_stats_id;
9881 }
9882 
9883 static int ath12k_mac_setup_vdev_params_mbssid(struct ath12k_link_vif *arvif,
9884 					       u32 *flags, u32 *tx_vdev_id)
9885 {
9886 	struct ath12k_vif *ahvif = arvif->ahvif;
9887 	struct ieee80211_bss_conf *link_conf;
9888 	struct ath12k *ar = arvif->ar;
9889 	struct ath12k_link_vif *tx_arvif;
9890 
9891 	link_conf = ath12k_mac_get_link_bss_conf(arvif);
9892 	if (!link_conf) {
9893 		ath12k_warn(ar->ab, "unable to access bss link conf in set mbssid params for vif %pM link %u\n",
9894 			    ahvif->vif->addr, arvif->link_id);
9895 		return -ENOLINK;
9896 	}
9897 
9898 	tx_arvif = ath12k_mac_get_tx_arvif(arvif, link_conf);
9899 	if (!tx_arvif)
9900 		return 0;
9901 
9902 	if (link_conf->nontransmitted) {
9903 		if (ath12k_ar_to_hw(ar)->wiphy !=
9904 		    ath12k_ar_to_hw(tx_arvif->ar)->wiphy)
9905 			return -EINVAL;
9906 
9907 		*flags = WMI_VDEV_MBSSID_FLAGS_NON_TRANSMIT_AP;
9908 		*tx_vdev_id = tx_arvif->vdev_id;
9909 	} else if (tx_arvif == arvif) {
9910 		*flags = WMI_VDEV_MBSSID_FLAGS_TRANSMIT_AP;
9911 	} else {
9912 		return -EINVAL;
9913 	}
9914 
9915 	if (link_conf->ema_ap)
9916 		*flags |= WMI_VDEV_MBSSID_FLAGS_EMA_MODE;
9917 
9918 	return 0;
9919 }
9920 
9921 static int ath12k_mac_setup_vdev_create_arg(struct ath12k_link_vif *arvif,
9922 					    struct ath12k_wmi_vdev_create_arg *arg)
9923 {
9924 	struct ath12k *ar = arvif->ar;
9925 	struct ath12k_pdev *pdev = ar->pdev;
9926 	struct ath12k_vif *ahvif = arvif->ahvif;
9927 	int ret;
9928 
9929 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
9930 
9931 	arg->if_id = arvif->vdev_id;
9932 	arg->type = ahvif->vdev_type;
9933 	arg->subtype = ahvif->vdev_subtype;
9934 	arg->pdev_id = pdev->pdev_id;
9935 
9936 	arg->mbssid_flags = WMI_VDEV_MBSSID_FLAGS_NON_MBSSID_AP;
9937 	arg->mbssid_tx_vdev_id = 0;
9938 	if (!test_bit(WMI_TLV_SERVICE_MBSS_PARAM_IN_VDEV_START_SUPPORT,
9939 		      ar->ab->wmi_ab.svc_map)) {
9940 		ret = ath12k_mac_setup_vdev_params_mbssid(arvif,
9941 							  &arg->mbssid_flags,
9942 							  &arg->mbssid_tx_vdev_id);
9943 		if (ret)
9944 			return ret;
9945 	}
9946 
9947 	if (pdev->cap.supported_bands & WMI_HOST_WLAN_2GHZ_CAP) {
9948 		arg->chains[NL80211_BAND_2GHZ].tx = ar->num_tx_chains;
9949 		arg->chains[NL80211_BAND_2GHZ].rx = ar->num_rx_chains;
9950 	}
9951 	if (pdev->cap.supported_bands & WMI_HOST_WLAN_5GHZ_CAP) {
9952 		arg->chains[NL80211_BAND_5GHZ].tx = ar->num_tx_chains;
9953 		arg->chains[NL80211_BAND_5GHZ].rx = ar->num_rx_chains;
9954 	}
9955 	if (pdev->cap.supported_bands & WMI_HOST_WLAN_5GHZ_CAP &&
9956 	    ar->supports_6ghz) {
9957 		arg->chains[NL80211_BAND_6GHZ].tx = ar->num_tx_chains;
9958 		arg->chains[NL80211_BAND_6GHZ].rx = ar->num_rx_chains;
9959 	}
9960 
9961 	arg->if_stats_id = ath12k_mac_get_vdev_stats_id(arvif);
9962 
9963 	if (ath12k_mac_is_ml_arvif(arvif)) {
9964 		if (hweight16(ahvif->vif->valid_links) > ATH12K_WMI_MLO_MAX_LINKS) {
9965 			ath12k_warn(ar->ab, "too many MLO links during setting up vdev: %d",
9966 				    ahvif->vif->valid_links);
9967 			return -EINVAL;
9968 		}
9969 
9970 		ether_addr_copy(arg->mld_addr, ahvif->vif->addr);
9971 	}
9972 
9973 	return 0;
9974 }
9975 
9976 static void ath12k_mac_update_vif_offload(struct ath12k_link_vif *arvif)
9977 {
9978 	struct ath12k_vif *ahvif = arvif->ahvif;
9979 	struct ieee80211_vif *vif = ath12k_ahvif_to_vif(ahvif);
9980 	struct ath12k *ar = arvif->ar;
9981 	struct ath12k_base *ab = ar->ab;
9982 	u32 param_id, param_value;
9983 	int ret;
9984 
9985 	param_id = WMI_VDEV_PARAM_TX_ENCAP_TYPE;
9986 	if (vif->type != NL80211_IFTYPE_STATION &&
9987 	    vif->type != NL80211_IFTYPE_AP)
9988 		vif->offload_flags &= ~(IEEE80211_OFFLOAD_ENCAP_ENABLED |
9989 					IEEE80211_OFFLOAD_DECAP_ENABLED);
9990 
9991 	if (vif->offload_flags & IEEE80211_OFFLOAD_ENCAP_ENABLED)
9992 		ahvif->dp_vif.tx_encap_type = ATH12K_HW_TXRX_ETHERNET;
9993 	else if (test_bit(ATH12K_FLAG_RAW_MODE, &ab->dev_flags))
9994 		ahvif->dp_vif.tx_encap_type = ATH12K_HW_TXRX_RAW;
9995 	else
9996 		ahvif->dp_vif.tx_encap_type = ATH12K_HW_TXRX_NATIVE_WIFI;
9997 
9998 	ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
9999 					    param_id, ahvif->dp_vif.tx_encap_type);
10000 	if (ret) {
10001 		ath12k_warn(ab, "failed to set vdev %d tx encap mode: %d\n",
10002 			    arvif->vdev_id, ret);
10003 		vif->offload_flags &= ~IEEE80211_OFFLOAD_ENCAP_ENABLED;
10004 	}
10005 
10006 	param_id = WMI_VDEV_PARAM_RX_DECAP_TYPE;
10007 	if (vif->offload_flags & IEEE80211_OFFLOAD_DECAP_ENABLED)
10008 		param_value = ATH12K_HW_TXRX_ETHERNET;
10009 	else if (test_bit(ATH12K_FLAG_RAW_MODE, &ab->dev_flags))
10010 		param_value = ATH12K_HW_TXRX_RAW;
10011 	else
10012 		param_value = ATH12K_HW_TXRX_NATIVE_WIFI;
10013 
10014 	ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
10015 					    param_id, param_value);
10016 	if (ret) {
10017 		ath12k_warn(ab, "failed to set vdev %d rx decap mode: %d\n",
10018 			    arvif->vdev_id, ret);
10019 		vif->offload_flags &= ~IEEE80211_OFFLOAD_DECAP_ENABLED;
10020 	}
10021 }
10022 
10023 void ath12k_mac_op_update_vif_offload(struct ieee80211_hw *hw,
10024 				      struct ieee80211_vif *vif)
10025 {
10026 	struct ath12k_vif *ahvif = ath12k_vif_to_ahvif(vif);
10027 	struct ath12k_link_vif *arvif;
10028 	unsigned long links;
10029 	int link_id;
10030 
10031 	lockdep_assert_wiphy(hw->wiphy);
10032 
10033 	if (vif->valid_links) {
10034 		links = vif->valid_links;
10035 		for_each_set_bit(link_id, &links, IEEE80211_MLD_MAX_NUM_LINKS) {
10036 			arvif = wiphy_dereference(hw->wiphy, ahvif->link[link_id]);
10037 			if (!(arvif && arvif->ar))
10038 				continue;
10039 
10040 			ath12k_mac_update_vif_offload(arvif);
10041 		}
10042 
10043 		return;
10044 	}
10045 
10046 	ath12k_mac_update_vif_offload(&ahvif->deflink);
10047 }
10048 EXPORT_SYMBOL(ath12k_mac_op_update_vif_offload);
10049 
10050 static bool ath12k_mac_vif_ap_active_any(struct ath12k_base *ab)
10051 {
10052 	struct ath12k *ar;
10053 	struct ath12k_pdev *pdev;
10054 	struct ath12k_link_vif *arvif;
10055 	int i;
10056 
10057 	for (i = 0; i < ab->num_radios; i++) {
10058 		pdev = &ab->pdevs[i];
10059 		ar = pdev->ar;
10060 		list_for_each_entry(arvif, &ar->arvifs, list) {
10061 			if (arvif->is_up &&
10062 			    arvif->ahvif->vdev_type == WMI_VDEV_TYPE_AP)
10063 				return true;
10064 		}
10065 	}
10066 	return false;
10067 }
10068 
10069 void ath12k_mac_11d_scan_start(struct ath12k *ar, u32 vdev_id)
10070 {
10071 	struct wmi_11d_scan_start_arg arg;
10072 	int ret;
10073 
10074 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
10075 
10076 	if (ar->regdom_set_by_user)
10077 		goto fin;
10078 
10079 	if (ar->vdev_id_11d_scan != ATH12K_11D_INVALID_VDEV_ID)
10080 		goto fin;
10081 
10082 	if (!test_bit(WMI_TLV_SERVICE_11D_OFFLOAD, ar->ab->wmi_ab.svc_map))
10083 		goto fin;
10084 
10085 	if (ath12k_mac_vif_ap_active_any(ar->ab))
10086 		goto fin;
10087 
10088 	arg.vdev_id = vdev_id;
10089 	arg.start_interval_msec = 0;
10090 	arg.scan_period_msec = ATH12K_SCAN_11D_INTERVAL;
10091 
10092 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
10093 		   "mac start 11d scan for vdev %d\n", vdev_id);
10094 
10095 	ret = ath12k_wmi_send_11d_scan_start_cmd(ar, &arg);
10096 	if (ret) {
10097 		ath12k_warn(ar->ab, "failed to start 11d scan vdev %d ret: %d\n",
10098 			    vdev_id, ret);
10099 	} else {
10100 		ar->vdev_id_11d_scan = vdev_id;
10101 		if (ar->state_11d == ATH12K_11D_PREPARING)
10102 			ar->state_11d = ATH12K_11D_RUNNING;
10103 	}
10104 
10105 fin:
10106 	if (ar->state_11d == ATH12K_11D_PREPARING) {
10107 		ar->state_11d = ATH12K_11D_IDLE;
10108 		complete(&ar->completed_11d_scan);
10109 	}
10110 }
10111 
10112 void ath12k_mac_11d_scan_stop(struct ath12k *ar)
10113 {
10114 	int ret;
10115 	u32 vdev_id;
10116 
10117 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
10118 
10119 	if (!test_bit(WMI_TLV_SERVICE_11D_OFFLOAD, ar->ab->wmi_ab.svc_map))
10120 		return;
10121 
10122 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac stop 11d for vdev %d\n",
10123 		   ar->vdev_id_11d_scan);
10124 
10125 	if (ar->state_11d == ATH12K_11D_PREPARING) {
10126 		ar->state_11d = ATH12K_11D_IDLE;
10127 		complete(&ar->completed_11d_scan);
10128 	}
10129 
10130 	if (ar->vdev_id_11d_scan != ATH12K_11D_INVALID_VDEV_ID) {
10131 		vdev_id = ar->vdev_id_11d_scan;
10132 
10133 		ret = ath12k_wmi_send_11d_scan_stop_cmd(ar, vdev_id);
10134 		if (ret) {
10135 			ath12k_warn(ar->ab,
10136 				    "failed to stopt 11d scan vdev %d ret: %d\n",
10137 				    vdev_id, ret);
10138 		} else {
10139 			ar->vdev_id_11d_scan = ATH12K_11D_INVALID_VDEV_ID;
10140 			ar->state_11d = ATH12K_11D_IDLE;
10141 			complete(&ar->completed_11d_scan);
10142 		}
10143 	}
10144 }
10145 
10146 void ath12k_mac_11d_scan_stop_all(struct ath12k_base *ab)
10147 {
10148 	struct ath12k *ar;
10149 	struct ath12k_pdev *pdev;
10150 	int i;
10151 
10152 	ath12k_dbg(ab, ATH12K_DBG_MAC, "mac stop soc 11d scan\n");
10153 
10154 	for (i = 0; i < ab->num_radios; i++) {
10155 		pdev = &ab->pdevs[i];
10156 		ar = pdev->ar;
10157 
10158 		ath12k_mac_11d_scan_stop(ar);
10159 	}
10160 }
10161 
10162 static void ath12k_mac_determine_vdev_type(struct ieee80211_vif *vif,
10163 					   struct ath12k_vif *ahvif)
10164 {
10165 	ahvif->vdev_subtype = WMI_VDEV_SUBTYPE_NONE;
10166 
10167 	switch (vif->type) {
10168 	case NL80211_IFTYPE_UNSPECIFIED:
10169 	case NL80211_IFTYPE_STATION:
10170 		ahvif->vdev_type = WMI_VDEV_TYPE_STA;
10171 
10172 		if (vif->p2p)
10173 			ahvif->vdev_subtype = WMI_VDEV_SUBTYPE_P2P_CLIENT;
10174 
10175 		break;
10176 	case NL80211_IFTYPE_MESH_POINT:
10177 		ahvif->vdev_subtype = WMI_VDEV_SUBTYPE_MESH_11S;
10178 		fallthrough;
10179 	case NL80211_IFTYPE_AP:
10180 		ahvif->vdev_type = WMI_VDEV_TYPE_AP;
10181 
10182 		if (vif->p2p)
10183 			ahvif->vdev_subtype = WMI_VDEV_SUBTYPE_P2P_GO;
10184 
10185 		break;
10186 	case NL80211_IFTYPE_MONITOR:
10187 		ahvif->vdev_type = WMI_VDEV_TYPE_MONITOR;
10188 		break;
10189 	case NL80211_IFTYPE_P2P_DEVICE:
10190 		ahvif->vdev_type = WMI_VDEV_TYPE_STA;
10191 		ahvif->vdev_subtype = WMI_VDEV_SUBTYPE_P2P_DEVICE;
10192 		break;
10193 	default:
10194 		WARN_ON(1);
10195 		break;
10196 	}
10197 }
10198 
10199 int ath12k_mac_vdev_create(struct ath12k *ar, struct ath12k_link_vif *arvif)
10200 {
10201 	struct ath12k_hw *ah = ar->ah;
10202 	struct ath12k_base *ab = ar->ab;
10203 	struct ieee80211_hw *hw = ah->hw;
10204 	struct ath12k_vif *ahvif = arvif->ahvif;
10205 	struct ieee80211_vif *vif = ath12k_ahvif_to_vif(ahvif);
10206 	struct ath12k_wmi_vdev_create_arg vdev_arg = {};
10207 	struct ath12k_wmi_peer_create_arg peer_param = {};
10208 	struct ieee80211_bss_conf *link_conf = NULL;
10209 	u32 param_id, param_value;
10210 	u16 nss;
10211 	int i;
10212 	int ret, vdev_id;
10213 	u8 link_id;
10214 	struct ath12k_dp_link_vif *dp_link_vif = NULL;
10215 	struct ath12k_dp_peer_create_params params = {};
10216 	bool dp_peer_created = false;
10217 
10218 	lockdep_assert_wiphy(hw->wiphy);
10219 
10220 	/* In NO_VIRTUAL_MONITOR, its necessary to restrict only one monitor
10221 	 * interface in each radio
10222 	 */
10223 	if (vif->type == NL80211_IFTYPE_MONITOR && ar->monitor_vdev_created)
10224 		return -EINVAL;
10225 
10226 	if (ar->num_created_vdevs >= TARGET_NUM_VDEVS(ab)) {
10227 		ath12k_warn(ab, "failed to create vdev, reached max vdev limit %d\n",
10228 			    TARGET_NUM_VDEVS(ab));
10229 		return -ENOSPC;
10230 	}
10231 
10232 	link_id = arvif->link_id;
10233 
10234 	if (link_id < IEEE80211_MLD_MAX_NUM_LINKS) {
10235 		link_conf = wiphy_dereference(hw->wiphy, vif->link_conf[link_id]);
10236 		if (!link_conf) {
10237 			ath12k_warn(ar->ab, "unable to access bss link conf in vdev create for vif %pM link %u\n",
10238 				    vif->addr, arvif->link_id);
10239 			return -ENOLINK;
10240 		}
10241 	}
10242 
10243 	if (link_conf)
10244 		memcpy(arvif->bssid, link_conf->addr, ETH_ALEN);
10245 	else
10246 		memcpy(arvif->bssid, vif->addr, ETH_ALEN);
10247 
10248 	arvif->ar = ar;
10249 	vdev_id = __ffs64(ab->free_vdev_map);
10250 	arvif->vdev_id = vdev_id;
10251 	if (vif->type == NL80211_IFTYPE_MONITOR)
10252 		ar->monitor_vdev_id = vdev_id;
10253 
10254 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac vdev create id %d type %d subtype %d map %llx\n",
10255 		   arvif->vdev_id, ahvif->vdev_type, ahvif->vdev_subtype,
10256 		   ab->free_vdev_map);
10257 
10258 	vif->cab_queue = arvif->vdev_id % (ATH12K_HW_MAX_QUEUES - 1);
10259 	for (i = 0; i < ARRAY_SIZE(vif->hw_queue); i++)
10260 		vif->hw_queue[i] = i % (ATH12K_HW_MAX_QUEUES - 1);
10261 
10262 	ret = ath12k_mac_setup_vdev_create_arg(arvif, &vdev_arg);
10263 	if (ret) {
10264 		ath12k_warn(ab, "failed to create vdev parameters %d: %d\n",
10265 			    arvif->vdev_id, ret);
10266 		goto err;
10267 	}
10268 
10269 	ret = ath12k_wmi_vdev_create(ar, arvif->bssid, &vdev_arg);
10270 	if (ret) {
10271 		ath12k_warn(ab, "failed to create WMI vdev %d: %d\n",
10272 			    arvif->vdev_id, ret);
10273 		return ret;
10274 	}
10275 
10276 	ar->num_created_vdevs++;
10277 	arvif->is_created = true;
10278 	ath12k_dbg(ab, ATH12K_DBG_MAC, "vdev %pM created, vdev_id %d\n",
10279 		   vif->addr, arvif->vdev_id);
10280 	ar->allocated_vdev_map |= 1LL << arvif->vdev_id;
10281 	ab->free_vdev_map &= ~(1LL << arvif->vdev_id);
10282 
10283 	spin_lock_bh(&ar->data_lock);
10284 	list_add(&arvif->list, &ar->arvifs);
10285 	spin_unlock_bh(&ar->data_lock);
10286 
10287 	ath12k_mac_update_vif_offload(arvif);
10288 
10289 	nss = hweight32(ar->cfg_tx_chainmask) ? : 1;
10290 	ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
10291 					    WMI_VDEV_PARAM_NSS, nss);
10292 	if (ret) {
10293 		ath12k_warn(ab, "failed to set vdev %d chainmask 0x%x, nss %d :%d\n",
10294 			    arvif->vdev_id, ar->cfg_tx_chainmask, nss, ret);
10295 		goto err_vdev_del;
10296 	}
10297 
10298 	dp_link_vif = ath12k_dp_vif_to_dp_link_vif(&ahvif->dp_vif, arvif->link_id);
10299 
10300 	dp_link_vif->vdev_id = arvif->vdev_id;
10301 	dp_link_vif->lmac_id = ar->lmac_id;
10302 	dp_link_vif->pdev_idx = ar->pdev_idx;
10303 
10304 	switch (ahvif->vdev_type) {
10305 	case WMI_VDEV_TYPE_AP:
10306 		params.ucast_ra_only = true;
10307 
10308 		if (arvif->link_id < IEEE80211_MLD_MAX_NUM_LINKS) {
10309 			ret = ath12k_dp_peer_create(&ah->dp_hw, arvif->bssid, &params);
10310 			if (ret) {
10311 				ath12k_warn(ab, "failed to vdev %d create dp_peer for AP: %d\n",
10312 					    arvif->vdev_id, ret);
10313 				goto err_vdev_del;
10314 			}
10315 			dp_peer_created = true;
10316 		}
10317 
10318 		peer_param.vdev_id = arvif->vdev_id;
10319 		peer_param.peer_addr = arvif->bssid;
10320 		peer_param.peer_type = WMI_PEER_TYPE_DEFAULT;
10321 		ret = ath12k_peer_create(ar, arvif, NULL, &peer_param);
10322 		if (ret) {
10323 			ath12k_warn(ab, "failed to vdev %d create peer for AP: %d\n",
10324 				    arvif->vdev_id, ret);
10325 			goto err_dp_peer_del;
10326 		}
10327 
10328 		ret = ath12k_mac_set_kickout(arvif);
10329 		if (ret) {
10330 			ath12k_warn(ar->ab, "failed to set vdev %i kickout parameters: %d\n",
10331 				    arvif->vdev_id, ret);
10332 			goto err_peer_del;
10333 		}
10334 		ath12k_mac_11d_scan_stop_all(ar->ab);
10335 		break;
10336 	case WMI_VDEV_TYPE_STA:
10337 		param_id = WMI_STA_PS_PARAM_RX_WAKE_POLICY;
10338 		param_value = WMI_STA_PS_RX_WAKE_POLICY_WAKE;
10339 		ret = ath12k_wmi_set_sta_ps_param(ar, arvif->vdev_id,
10340 						  param_id, param_value);
10341 		if (ret) {
10342 			ath12k_warn(ar->ab, "failed to set vdev %d RX wake policy: %d\n",
10343 				    arvif->vdev_id, ret);
10344 			goto err_peer_del;
10345 		}
10346 
10347 		param_id = WMI_STA_PS_PARAM_TX_WAKE_THRESHOLD;
10348 		param_value = WMI_STA_PS_TX_WAKE_THRESHOLD_ALWAYS;
10349 		ret = ath12k_wmi_set_sta_ps_param(ar, arvif->vdev_id,
10350 						  param_id, param_value);
10351 		if (ret) {
10352 			ath12k_warn(ar->ab, "failed to set vdev %d TX wake threshold: %d\n",
10353 				    arvif->vdev_id, ret);
10354 			goto err_peer_del;
10355 		}
10356 
10357 		param_id = WMI_STA_PS_PARAM_PSPOLL_COUNT;
10358 		param_value = WMI_STA_PS_PSPOLL_COUNT_NO_MAX;
10359 		ret = ath12k_wmi_set_sta_ps_param(ar, arvif->vdev_id,
10360 						  param_id, param_value);
10361 		if (ret) {
10362 			ath12k_warn(ar->ab, "failed to set vdev %d pspoll count: %d\n",
10363 				    arvif->vdev_id, ret);
10364 			goto err_peer_del;
10365 		}
10366 
10367 		ret = ath12k_wmi_pdev_set_ps_mode(ar, arvif->vdev_id, false);
10368 		if (ret) {
10369 			ath12k_warn(ar->ab, "failed to disable vdev %d ps mode: %d\n",
10370 				    arvif->vdev_id, ret);
10371 			goto err_peer_del;
10372 		}
10373 
10374 		if (test_bit(WMI_TLV_SERVICE_11D_OFFLOAD, ab->wmi_ab.svc_map) &&
10375 		    ahvif->vdev_type == WMI_VDEV_TYPE_STA &&
10376 		    ahvif->vdev_subtype == WMI_VDEV_SUBTYPE_NONE) {
10377 			reinit_completion(&ar->completed_11d_scan);
10378 			ar->state_11d = ATH12K_11D_PREPARING;
10379 		}
10380 		break;
10381 	case WMI_VDEV_TYPE_MONITOR:
10382 		ar->monitor_vdev_created = true;
10383 		break;
10384 	default:
10385 		break;
10386 	}
10387 
10388 	if (link_conf)
10389 		arvif->txpower = link_conf->txpower;
10390 	else
10391 		arvif->txpower = NL80211_TX_POWER_AUTOMATIC;
10392 
10393 	ret = ath12k_mac_txpower_recalc(ar);
10394 	if (ret)
10395 		goto err_peer_del;
10396 
10397 	param_id = WMI_VDEV_PARAM_RTS_THRESHOLD;
10398 	param_value = hw->wiphy->rts_threshold;
10399 	ar->rts_threshold = param_value;
10400 	ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
10401 					    param_id, param_value);
10402 	if (ret) {
10403 		ath12k_warn(ar->ab, "failed to set rts threshold for vdev %d: %d\n",
10404 			    arvif->vdev_id, ret);
10405 	}
10406 
10407 	ath12k_dp_vdev_tx_attach(ar, arvif);
10408 
10409 	return ret;
10410 
10411 err_peer_del:
10412 	if (ahvif->vdev_type == WMI_VDEV_TYPE_AP) {
10413 		reinit_completion(&ar->peer_delete_done);
10414 
10415 		ret = ath12k_wmi_send_peer_delete_cmd(ar, arvif->bssid,
10416 						      arvif->vdev_id);
10417 		if (ret) {
10418 			ath12k_warn(ar->ab, "failed to delete peer vdev_id %d addr %pM\n",
10419 				    arvif->vdev_id, arvif->bssid);
10420 			goto err;
10421 		}
10422 
10423 		ret = ath12k_wait_for_peer_delete_done(ar, arvif->vdev_id,
10424 						       arvif->bssid);
10425 		if (ret)
10426 			goto err_vdev_del;
10427 
10428 		ar->num_peers--;
10429 	}
10430 
10431 err_dp_peer_del:
10432 	if (dp_peer_created)
10433 		ath12k_dp_peer_delete(&ah->dp_hw, arvif->bssid, NULL);
10434 
10435 err_vdev_del:
10436 	if (ahvif->vdev_type == WMI_VDEV_TYPE_MONITOR) {
10437 		ar->monitor_vdev_id = -1;
10438 		ar->monitor_vdev_created = false;
10439 	}
10440 
10441 	ath12k_wmi_vdev_delete(ar, arvif->vdev_id);
10442 	ar->num_created_vdevs--;
10443 	arvif->is_created = false;
10444 	arvif->ar = NULL;
10445 	ar->allocated_vdev_map &= ~(1LL << arvif->vdev_id);
10446 	ab->free_vdev_map |= 1LL << arvif->vdev_id;
10447 	ab->free_vdev_stats_id_map &= ~(1LL << arvif->vdev_stats_id);
10448 	spin_lock_bh(&ar->data_lock);
10449 	list_del(&arvif->list);
10450 	spin_unlock_bh(&ar->data_lock);
10451 
10452 err:
10453 	arvif->ar = NULL;
10454 	return ret;
10455 }
10456 
10457 static void ath12k_mac_vif_flush_key_cache(struct ath12k_link_vif *arvif)
10458 {
10459 	struct ath12k_key_conf *key_conf, *tmp;
10460 	struct ath12k_vif *ahvif = arvif->ahvif;
10461 	struct ath12k_hw *ah = ahvif->ah;
10462 	struct ath12k_sta *ahsta;
10463 	struct ath12k_link_sta *arsta;
10464 	struct ath12k_vif_cache *cache = ahvif->cache[arvif->link_id];
10465 	int ret;
10466 
10467 	lockdep_assert_wiphy(ah->hw->wiphy);
10468 
10469 	list_for_each_entry_safe(key_conf, tmp, &cache->key_conf.list, list) {
10470 		arsta = NULL;
10471 		if (key_conf->sta) {
10472 			ahsta = ath12k_sta_to_ahsta(key_conf->sta);
10473 			arsta = wiphy_dereference(ah->hw->wiphy,
10474 						  ahsta->link[arvif->link_id]);
10475 			if (!arsta)
10476 				goto free_cache;
10477 		}
10478 
10479 		ret = ath12k_mac_set_key(arvif->ar, key_conf->cmd,
10480 					 arvif, arsta,
10481 					 key_conf->key);
10482 		if (ret)
10483 			ath12k_warn(arvif->ar->ab, "unable to apply set key param to vdev %d ret %d\n",
10484 				    arvif->vdev_id, ret);
10485 free_cache:
10486 		list_del(&key_conf->list);
10487 		kfree(key_conf);
10488 	}
10489 }
10490 
10491 static void ath12k_mac_vif_cache_flush(struct ath12k *ar, struct ath12k_link_vif *arvif)
10492 {
10493 	struct ath12k_vif *ahvif = arvif->ahvif;
10494 	struct ieee80211_vif *vif = ath12k_ahvif_to_vif(ahvif);
10495 	struct ath12k_vif_cache *cache = ahvif->cache[arvif->link_id];
10496 	struct ath12k_base *ab = ar->ab;
10497 	struct ieee80211_bss_conf *link_conf;
10498 
10499 	int ret;
10500 
10501 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
10502 
10503 	if (!cache)
10504 		return;
10505 
10506 	if (cache->tx_conf.changed) {
10507 		ret = ath12k_mac_conf_tx(arvif, cache->tx_conf.ac,
10508 					 &cache->tx_conf.tx_queue_params);
10509 		if (ret)
10510 			ath12k_warn(ab,
10511 				    "unable to apply tx config parameters to vdev %d\n",
10512 				    ret);
10513 	}
10514 
10515 	if (cache->bss_conf_changed) {
10516 		link_conf = ath12k_mac_get_link_bss_conf(arvif);
10517 		if (!link_conf) {
10518 			ath12k_warn(ar->ab, "unable to access bss link conf in cache flush for vif %pM link %u\n",
10519 				    vif->addr, arvif->link_id);
10520 			return;
10521 		}
10522 		ath12k_mac_bss_info_changed(ar, arvif, link_conf,
10523 					    cache->bss_conf_changed);
10524 	}
10525 
10526 	if (!list_empty(&cache->key_conf.list))
10527 		ath12k_mac_vif_flush_key_cache(arvif);
10528 
10529 	ath12k_ahvif_put_link_cache(ahvif, arvif->link_id);
10530 }
10531 
10532 static struct ath12k *ath12k_mac_assign_vif_to_vdev(struct ieee80211_hw *hw,
10533 						    struct ath12k_link_vif *arvif,
10534 						    struct ieee80211_chanctx_conf *ctx)
10535 {
10536 	struct ath12k_vif *ahvif = arvif->ahvif;
10537 	struct ieee80211_vif *vif = ath12k_ahvif_to_vif(ahvif);
10538 	struct ath12k_link_vif *scan_arvif;
10539 	struct ath12k_hw *ah = hw->priv;
10540 	struct ath12k *ar;
10541 	struct ath12k_base *ab;
10542 	u8 link_id = arvif->link_id, scan_link_id;
10543 	unsigned long scan_link_map;
10544 	int ret;
10545 
10546 	lockdep_assert_wiphy(hw->wiphy);
10547 
10548 	if (ah->num_radio == 1)
10549 		ar = ah->radio;
10550 	else if (ctx)
10551 		ar = ath12k_get_ar_by_ctx(hw, ctx);
10552 	else
10553 		return NULL;
10554 
10555 	if (!ar)
10556 		return NULL;
10557 
10558 	/* cleanup the scan vdev if we are done scan on that ar
10559 	 * and now we want to create for actual usage.
10560 	 */
10561 	if (ieee80211_vif_is_mld(vif)) {
10562 		scan_link_map = ahvif->links_map & ATH12K_SCAN_LINKS_MASK;
10563 		for_each_set_bit(scan_link_id, &scan_link_map, ATH12K_NUM_MAX_LINKS) {
10564 			scan_arvif = wiphy_dereference(hw->wiphy,
10565 						       ahvif->link[scan_link_id]);
10566 			if (scan_arvif && scan_arvif->ar == ar) {
10567 				ar->scan.arvif = NULL;
10568 				ath12k_mac_remove_link_interface(hw, scan_arvif);
10569 				ath12k_mac_unassign_link_vif(scan_arvif);
10570 				break;
10571 			}
10572 		}
10573 	}
10574 
10575 	if (arvif->ar) {
10576 		/* This is not expected really */
10577 		if (WARN_ON(!arvif->is_created)) {
10578 			arvif->ar = NULL;
10579 			return NULL;
10580 		}
10581 
10582 		if (ah->num_radio == 1)
10583 			return arvif->ar;
10584 
10585 		/* This can happen as scan vdev gets created during multiple scans
10586 		 * across different radios before a vdev is brought up in
10587 		 * a certain radio.
10588 		 */
10589 		if (ar != arvif->ar) {
10590 			if (WARN_ON(arvif->is_started))
10591 				return NULL;
10592 
10593 			ath12k_mac_remove_link_interface(hw, arvif);
10594 			ath12k_mac_unassign_link_vif(arvif);
10595 		}
10596 	}
10597 
10598 	ab = ar->ab;
10599 
10600 	/* Assign arvif again here since previous radio switch block
10601 	 * would've unassigned and cleared it.
10602 	 */
10603 	arvif = ath12k_mac_assign_link_vif(ah, vif, link_id);
10604 	if (vif->type == NL80211_IFTYPE_AP &&
10605 	    ar->num_peers > (ar->max_num_peers - 1)) {
10606 		ath12k_warn(ab, "failed to create vdev due to insufficient peer entry resource in firmware\n");
10607 		goto unlock;
10608 	}
10609 
10610 	if (arvif->is_created)
10611 		goto flush;
10612 
10613 	ret = ath12k_mac_vdev_create(ar, arvif);
10614 	if (ret) {
10615 		ath12k_warn(ab, "failed to create vdev %pM ret %d", vif->addr, ret);
10616 		goto unlock;
10617 	}
10618 
10619 flush:
10620 	/* If the vdev is created during channel assign and not during
10621 	 * add_interface(), Apply any parameters for the vdev which were received
10622 	 * after add_interface, corresponding to this vif.
10623 	 */
10624 	ath12k_mac_vif_cache_flush(ar, arvif);
10625 unlock:
10626 	return arvif->ar;
10627 }
10628 
10629 int ath12k_mac_op_add_interface(struct ieee80211_hw *hw,
10630 				struct ieee80211_vif *vif)
10631 {
10632 	struct ath12k_hw *ah = ath12k_hw_to_ah(hw);
10633 	struct ath12k_vif *ahvif = ath12k_vif_to_ahvif(vif);
10634 	struct ath12k_reg_info *reg_info;
10635 	struct ath12k_link_vif *arvif;
10636 	struct ath12k_base *ab;
10637 	struct ath12k *ar;
10638 	int i;
10639 
10640 	lockdep_assert_wiphy(hw->wiphy);
10641 
10642 	memset(ahvif, 0, sizeof(*ahvif));
10643 
10644 	ahvif->ah = ah;
10645 	ahvif->vif = vif;
10646 	arvif = &ahvif->deflink;
10647 
10648 	ath12k_mac_init_arvif(ahvif, arvif, -1);
10649 
10650 	/* Allocate Default Queue now and reassign during actual vdev create */
10651 	vif->cab_queue = ATH12K_HW_DEFAULT_QUEUE;
10652 	for (i = 0; i < ARRAY_SIZE(vif->hw_queue); i++)
10653 		vif->hw_queue[i] = ATH12K_HW_DEFAULT_QUEUE;
10654 
10655 	vif->driver_flags |= IEEE80211_VIF_SUPPORTS_UAPSD;
10656 
10657 	ath12k_mac_determine_vdev_type(vif, ahvif);
10658 
10659 	for_each_ar(ah, ar, i) {
10660 		if (!ath12k_wmi_supports_6ghz_cc_ext(ar))
10661 			continue;
10662 
10663 		ab = ar->ab;
10664 		reg_info = ab->reg_info[ar->pdev_idx];
10665 		ath12k_dbg(ab, ATH12K_DBG_MAC, "interface added to change reg rules\n");
10666 		ah->regd_updated = false;
10667 		ath12k_reg_handle_chan_list(ab, reg_info, ahvif->vdev_type,
10668 					    IEEE80211_REG_UNSET_AP);
10669 		break;
10670 	}
10671 
10672 	/* Defer vdev creation until assign_chanctx or hw_scan is initiated as driver
10673 	 * will not know if this interface is an ML vif at this point.
10674 	 */
10675 	return 0;
10676 }
10677 EXPORT_SYMBOL(ath12k_mac_op_add_interface);
10678 
10679 static void ath12k_mac_vif_unref(struct ath12k_dp *dp, struct ieee80211_vif *vif)
10680 {
10681 	struct ath12k_tx_desc_info *tx_desc_info;
10682 	struct ath12k_skb_cb *skb_cb;
10683 	struct sk_buff *skb;
10684 	int i;
10685 
10686 	for (i = 0; i < ATH12K_HW_MAX_QUEUES; i++) {
10687 		spin_lock_bh(&dp->tx_desc_lock[i]);
10688 
10689 		list_for_each_entry(tx_desc_info, &dp->tx_desc_used_list[i],
10690 				    list) {
10691 			skb = tx_desc_info->skb;
10692 			if (!skb)
10693 				continue;
10694 
10695 			skb_cb = ATH12K_SKB_CB(skb);
10696 			if (skb_cb->vif == vif)
10697 				skb_cb->vif = NULL;
10698 		}
10699 
10700 		spin_unlock_bh(&dp->tx_desc_lock[i]);
10701 	}
10702 }
10703 
10704 static int ath12k_mac_vdev_delete(struct ath12k *ar, struct ath12k_link_vif *arvif)
10705 {
10706 	struct ath12k_vif *ahvif = arvif->ahvif;
10707 	struct ieee80211_vif *vif = ath12k_ahvif_to_vif(ahvif);
10708 	struct ath12k_dp_link_vif *dp_link_vif;
10709 	struct ath12k_base *ab = ar->ab;
10710 	unsigned long time_left;
10711 	int ret;
10712 
10713 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
10714 
10715 	reinit_completion(&ar->vdev_delete_done);
10716 
10717 	ret = ath12k_wmi_vdev_delete(ar, arvif->vdev_id);
10718 	if (ret) {
10719 		ath12k_warn(ab, "failed to delete WMI vdev %d: %d\n",
10720 			    arvif->vdev_id, ret);
10721 		goto err_vdev_del;
10722 	}
10723 
10724 	time_left = wait_for_completion_timeout(&ar->vdev_delete_done,
10725 						ATH12K_VDEV_DELETE_TIMEOUT_HZ);
10726 	if (time_left == 0) {
10727 		ath12k_warn(ab, "Timeout in receiving vdev delete response\n");
10728 		goto err_vdev_del;
10729 	}
10730 
10731 	ab->free_vdev_map |= 1LL << arvif->vdev_id;
10732 	ar->allocated_vdev_map &= ~(1LL << arvif->vdev_id);
10733 	ar->num_created_vdevs--;
10734 
10735 	if (ahvif->vdev_type == WMI_VDEV_TYPE_MONITOR) {
10736 		ar->monitor_vdev_id = -1;
10737 		ar->monitor_vdev_created = false;
10738 	}
10739 
10740 	ath12k_dbg(ab, ATH12K_DBG_MAC, "vdev %pM deleted, vdev_id %d\n",
10741 		   vif->addr, arvif->vdev_id);
10742 
10743 err_vdev_del:
10744 	spin_lock_bh(&ar->data_lock);
10745 	list_del(&arvif->list);
10746 	spin_unlock_bh(&ar->data_lock);
10747 
10748 	ath12k_peer_cleanup(ar, arvif->vdev_id);
10749 	ath12k_ahvif_put_link_cache(ahvif, arvif->link_id);
10750 
10751 	idr_for_each(&ar->txmgmt_idr,
10752 		     ath12k_mac_vif_txmgmt_idr_remove, vif);
10753 
10754 	ath12k_mac_vif_unref(ath12k_ab_to_dp(ab), vif);
10755 
10756 	dp_link_vif = ath12k_dp_vif_to_dp_link_vif(&ahvif->dp_vif, arvif->link_id);
10757 	ath12k_dp_tx_put_bank_profile(ath12k_ab_to_dp(ab), dp_link_vif->bank_id);
10758 
10759 	/* Recalc txpower for remaining vdev */
10760 	ath12k_mac_txpower_recalc(ar);
10761 
10762 	/* TODO: recal traffic pause state based on the available vdevs */
10763 	arvif->is_created = false;
10764 	arvif->ar = NULL;
10765 
10766 	return ret;
10767 }
10768 
10769 void ath12k_mac_op_remove_interface(struct ieee80211_hw *hw,
10770 				    struct ieee80211_vif *vif)
10771 {
10772 	struct ath12k_vif *ahvif = ath12k_vif_to_ahvif(vif);
10773 	struct ath12k_link_vif *arvif;
10774 	struct ath12k *ar;
10775 	u8 link_id;
10776 
10777 	lockdep_assert_wiphy(hw->wiphy);
10778 
10779 	for (link_id = 0; link_id < ATH12K_NUM_MAX_LINKS; link_id++) {
10780 		/* if we cached some config but never received assign chanctx,
10781 		 * free the allocated cache.
10782 		 */
10783 		ath12k_ahvif_put_link_cache(ahvif, link_id);
10784 		arvif = wiphy_dereference(hw->wiphy, ahvif->link[link_id]);
10785 		if (!arvif || !arvif->is_created)
10786 			continue;
10787 
10788 		ar = arvif->ar;
10789 
10790 		/* Scan abortion is in progress since before this, cancel_hw_scan()
10791 		 * is expected to be executed. Since link is anyways going to be removed
10792 		 * now, just cancel the worker and send the scan aborted to user space
10793 		 */
10794 		if (ar->scan.arvif == arvif) {
10795 			wiphy_work_cancel(hw->wiphy, &ar->scan.vdev_clean_wk);
10796 
10797 			spin_lock_bh(&ar->data_lock);
10798 			ar->scan.arvif = NULL;
10799 			if (!ar->scan.is_roc) {
10800 				struct cfg80211_scan_info info = {
10801 					.aborted = true,
10802 				};
10803 
10804 				ath12k_mac_scan_send_complete(ar, &info);
10805 			}
10806 
10807 			ar->scan.state = ATH12K_SCAN_IDLE;
10808 			ar->scan_channel = NULL;
10809 			ar->scan.roc_freq = 0;
10810 			spin_unlock_bh(&ar->data_lock);
10811 		}
10812 
10813 		ath12k_mac_remove_link_interface(hw, arvif);
10814 		ath12k_mac_unassign_link_vif(arvif);
10815 	}
10816 }
10817 EXPORT_SYMBOL(ath12k_mac_op_remove_interface);
10818 
10819 /* FIXME: Has to be verified. */
10820 #define SUPPORTED_FILTERS			\
10821 	(FIF_ALLMULTI |				\
10822 	FIF_CONTROL |				\
10823 	FIF_PSPOLL |				\
10824 	FIF_OTHER_BSS |				\
10825 	FIF_BCN_PRBRESP_PROMISC |		\
10826 	FIF_PROBE_REQ |				\
10827 	FIF_FCSFAIL)
10828 
10829 void ath12k_mac_op_configure_filter(struct ieee80211_hw *hw,
10830 				    unsigned int changed_flags,
10831 				    unsigned int *total_flags,
10832 				    u64 multicast)
10833 {
10834 	struct ath12k_hw *ah = ath12k_hw_to_ah(hw);
10835 	struct ath12k *ar;
10836 
10837 	lockdep_assert_wiphy(hw->wiphy);
10838 
10839 	ar = ath12k_ah_to_ar(ah, 0);
10840 
10841 	*total_flags &= SUPPORTED_FILTERS;
10842 	ar->filter_flags = *total_flags;
10843 }
10844 EXPORT_SYMBOL(ath12k_mac_op_configure_filter);
10845 
10846 int ath12k_mac_op_get_antenna(struct ieee80211_hw *hw, int radio_idx,
10847 			      u32 *tx_ant, u32 *rx_ant)
10848 {
10849 	struct ath12k_hw *ah = ath12k_hw_to_ah(hw);
10850 	int antennas_rx = 0, antennas_tx = 0;
10851 	struct ath12k *ar;
10852 	int i;
10853 
10854 	lockdep_assert_wiphy(hw->wiphy);
10855 
10856 	for_each_ar(ah, ar, i) {
10857 		antennas_rx = max_t(u32, antennas_rx, ar->cfg_rx_chainmask);
10858 		antennas_tx = max_t(u32, antennas_tx, ar->cfg_tx_chainmask);
10859 	}
10860 
10861 	*tx_ant = antennas_tx;
10862 	*rx_ant = antennas_rx;
10863 
10864 	return 0;
10865 }
10866 EXPORT_SYMBOL(ath12k_mac_op_get_antenna);
10867 
10868 int ath12k_mac_op_set_antenna(struct ieee80211_hw *hw, int radio_idx,
10869 			      u32 tx_ant, u32 rx_ant)
10870 {
10871 	struct ath12k_hw *ah = ath12k_hw_to_ah(hw);
10872 	struct ath12k *ar;
10873 	int ret = 0;
10874 	int i;
10875 
10876 	lockdep_assert_wiphy(hw->wiphy);
10877 
10878 	for_each_ar(ah, ar, i) {
10879 		ret = __ath12k_set_antenna(ar, tx_ant, rx_ant);
10880 		if (ret)
10881 			break;
10882 	}
10883 
10884 	return ret;
10885 }
10886 EXPORT_SYMBOL(ath12k_mac_op_set_antenna);
10887 
10888 static int ath12k_mac_ampdu_action(struct ieee80211_hw *hw,
10889 				   struct ieee80211_vif *vif,
10890 				   struct ieee80211_ampdu_params *params,
10891 				   u8 link_id)
10892 {
10893 	struct ath12k *ar;
10894 	int ret = -EINVAL;
10895 
10896 	lockdep_assert_wiphy(hw->wiphy);
10897 
10898 	ar = ath12k_get_ar_by_vif(hw, vif, link_id);
10899 	if (!ar)
10900 		return -EINVAL;
10901 
10902 	switch (params->action) {
10903 	case IEEE80211_AMPDU_RX_START:
10904 		ret = ath12k_dp_rx_ampdu_start(ar, params, link_id);
10905 		break;
10906 	case IEEE80211_AMPDU_RX_STOP:
10907 		ret = ath12k_dp_rx_ampdu_stop(ar, params, link_id);
10908 		break;
10909 	case IEEE80211_AMPDU_TX_START:
10910 	case IEEE80211_AMPDU_TX_STOP_CONT:
10911 	case IEEE80211_AMPDU_TX_STOP_FLUSH:
10912 	case IEEE80211_AMPDU_TX_STOP_FLUSH_CONT:
10913 	case IEEE80211_AMPDU_TX_OPERATIONAL:
10914 		/* Tx A-MPDU aggregation offloaded to hw/fw so deny mac80211
10915 		 * Tx aggregation requests.
10916 		 */
10917 		ret = -EOPNOTSUPP;
10918 		break;
10919 	}
10920 
10921 	if (ret)
10922 		ath12k_warn(ar->ab, "unable to perform ampdu action %d for vif %pM link %u ret %d\n",
10923 			    params->action, vif->addr, link_id, ret);
10924 
10925 	return ret;
10926 }
10927 
10928 int ath12k_mac_op_ampdu_action(struct ieee80211_hw *hw,
10929 			       struct ieee80211_vif *vif,
10930 			       struct ieee80211_ampdu_params *params)
10931 {
10932 	struct ieee80211_sta *sta = params->sta;
10933 	struct ath12k_sta *ahsta = ath12k_sta_to_ahsta(sta);
10934 	unsigned long links_map = ahsta->links_map;
10935 	int ret = -EINVAL;
10936 	u8 link_id;
10937 
10938 	lockdep_assert_wiphy(hw->wiphy);
10939 
10940 	if (WARN_ON(!links_map))
10941 		return ret;
10942 
10943 	for_each_set_bit(link_id, &links_map, IEEE80211_MLD_MAX_NUM_LINKS) {
10944 		ret = ath12k_mac_ampdu_action(hw, vif, params, link_id);
10945 		if (ret)
10946 			return ret;
10947 	}
10948 
10949 	return 0;
10950 }
10951 EXPORT_SYMBOL(ath12k_mac_op_ampdu_action);
10952 
10953 int ath12k_mac_op_add_chanctx(struct ieee80211_hw *hw,
10954 			      struct ieee80211_chanctx_conf *ctx)
10955 {
10956 	struct ath12k *ar;
10957 	struct ath12k_base *ab;
10958 
10959 	lockdep_assert_wiphy(hw->wiphy);
10960 
10961 	ar = ath12k_get_ar_by_ctx(hw, ctx);
10962 	if (!ar)
10963 		return -EINVAL;
10964 
10965 	ab = ar->ab;
10966 
10967 	ath12k_dbg(ab, ATH12K_DBG_MAC,
10968 		   "mac chanctx add freq %u width %d ptr %p\n",
10969 		   ctx->def.chan->center_freq, ctx->def.width, ctx);
10970 
10971 	spin_lock_bh(&ar->data_lock);
10972 	/* TODO: In case of multiple channel context, populate rx_channel from
10973 	 * Rx PPDU desc information.
10974 	 */
10975 	ar->rx_channel = ctx->def.chan;
10976 	spin_unlock_bh(&ar->data_lock);
10977 	ar->chan_tx_pwr = ATH12K_PDEV_TX_POWER_INVALID;
10978 
10979 	return 0;
10980 }
10981 EXPORT_SYMBOL(ath12k_mac_op_add_chanctx);
10982 
10983 void ath12k_mac_op_remove_chanctx(struct ieee80211_hw *hw,
10984 				  struct ieee80211_chanctx_conf *ctx)
10985 {
10986 	struct ath12k *ar;
10987 	struct ath12k_base *ab;
10988 
10989 	lockdep_assert_wiphy(hw->wiphy);
10990 
10991 	ar = ath12k_get_ar_by_ctx(hw, ctx);
10992 	if (!ar)
10993 		return;
10994 
10995 	ab = ar->ab;
10996 
10997 	ath12k_dbg(ab, ATH12K_DBG_MAC,
10998 		   "mac chanctx remove freq %u width %d ptr %p\n",
10999 		   ctx->def.chan->center_freq, ctx->def.width, ctx);
11000 
11001 	spin_lock_bh(&ar->data_lock);
11002 	/* TODO: In case of there is one more channel context left, populate
11003 	 * rx_channel with the channel of that remaining channel context.
11004 	 */
11005 	ar->rx_channel = NULL;
11006 	spin_unlock_bh(&ar->data_lock);
11007 	ar->chan_tx_pwr = ATH12K_PDEV_TX_POWER_INVALID;
11008 }
11009 EXPORT_SYMBOL(ath12k_mac_op_remove_chanctx);
11010 
11011 static enum wmi_phy_mode
11012 ath12k_mac_check_down_grade_phy_mode(struct ath12k *ar,
11013 				     enum wmi_phy_mode mode,
11014 				     enum nl80211_band band,
11015 				     enum nl80211_iftype type)
11016 {
11017 	struct ieee80211_sta_eht_cap *eht_cap = NULL;
11018 	enum wmi_phy_mode down_mode;
11019 	int n = ar->mac.sbands[band].n_iftype_data;
11020 	int i;
11021 	struct ieee80211_sband_iftype_data *data;
11022 
11023 	if (mode < MODE_11BE_EHT20)
11024 		return mode;
11025 
11026 	data = ar->mac.iftype[band];
11027 	for (i = 0; i < n; i++) {
11028 		if (data[i].types_mask & BIT(type)) {
11029 			eht_cap = &data[i].eht_cap;
11030 			break;
11031 		}
11032 	}
11033 
11034 	if (eht_cap && eht_cap->has_eht)
11035 		return mode;
11036 
11037 	switch (mode) {
11038 	case MODE_11BE_EHT20:
11039 		down_mode = MODE_11AX_HE20;
11040 		break;
11041 	case MODE_11BE_EHT40:
11042 		down_mode = MODE_11AX_HE40;
11043 		break;
11044 	case MODE_11BE_EHT80:
11045 		down_mode = MODE_11AX_HE80;
11046 		break;
11047 	case MODE_11BE_EHT80_80:
11048 		down_mode = MODE_11AX_HE80_80;
11049 		break;
11050 	case MODE_11BE_EHT160:
11051 	case MODE_11BE_EHT160_160:
11052 	case MODE_11BE_EHT320:
11053 		down_mode = MODE_11AX_HE160;
11054 		break;
11055 	case MODE_11BE_EHT20_2G:
11056 		down_mode = MODE_11AX_HE20_2G;
11057 		break;
11058 	case MODE_11BE_EHT40_2G:
11059 		down_mode = MODE_11AX_HE40_2G;
11060 		break;
11061 	default:
11062 		down_mode = mode;
11063 		break;
11064 	}
11065 
11066 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
11067 		   "mac vdev start phymode %s downgrade to %s\n",
11068 		   ath12k_mac_phymode_str(mode),
11069 		   ath12k_mac_phymode_str(down_mode));
11070 
11071 	return down_mode;
11072 }
11073 
11074 static void
11075 ath12k_mac_mlo_get_vdev_args(struct ath12k_link_vif *arvif,
11076 			     struct wmi_ml_arg *ml_arg)
11077 {
11078 	struct ath12k_vif *ahvif = arvif->ahvif;
11079 	struct wmi_ml_partner_info *partner_info;
11080 	struct ieee80211_bss_conf *link_conf;
11081 	struct ath12k_link_vif *arvif_p;
11082 	unsigned long links;
11083 	u8 link_id;
11084 
11085 	lockdep_assert_wiphy(ahvif->ah->hw->wiphy);
11086 
11087 	if (!ath12k_mac_is_ml_arvif(arvif))
11088 		return;
11089 
11090 	if (hweight16(ahvif->vif->valid_links) > ATH12K_WMI_MLO_MAX_LINKS)
11091 		return;
11092 
11093 	ml_arg->enabled = true;
11094 
11095 	/* Driver always add a new link via VDEV START, FW takes
11096 	 * care of internally adding this link to existing
11097 	 * link vdevs which are advertised as partners below
11098 	 */
11099 	ml_arg->link_add = true;
11100 
11101 	ml_arg->assoc_link = arvif->is_sta_assoc_link;
11102 
11103 	partner_info = ml_arg->partner_info;
11104 
11105 	links = ahvif->links_map;
11106 	for_each_set_bit(link_id, &links, IEEE80211_MLD_MAX_NUM_LINKS) {
11107 		arvif_p = wiphy_dereference(ahvif->ah->hw->wiphy, ahvif->link[link_id]);
11108 
11109 		if (WARN_ON(!arvif_p))
11110 			continue;
11111 
11112 		if (arvif == arvif_p)
11113 			continue;
11114 
11115 		if (!arvif_p->is_created)
11116 			continue;
11117 
11118 		link_conf = wiphy_dereference(ahvif->ah->hw->wiphy,
11119 					      ahvif->vif->link_conf[arvif_p->link_id]);
11120 
11121 		if (!link_conf)
11122 			continue;
11123 
11124 		partner_info->vdev_id = arvif_p->vdev_id;
11125 		partner_info->hw_link_id = arvif_p->ar->pdev->hw_link_id;
11126 		ether_addr_copy(partner_info->addr, link_conf->addr);
11127 		ml_arg->num_partner_links++;
11128 		partner_info++;
11129 	}
11130 }
11131 
11132 static int
11133 ath12k_mac_vdev_start_restart(struct ath12k_link_vif *arvif,
11134 			      struct ieee80211_chanctx_conf *ctx,
11135 			      bool restart)
11136 {
11137 	struct ath12k *ar = arvif->ar;
11138 	struct ath12k_base *ab = ar->ab;
11139 	struct wmi_vdev_start_req_arg arg = {};
11140 	const struct cfg80211_chan_def *chandef = &ctx->def;
11141 	struct ieee80211_hw *hw = ath12k_ar_to_hw(ar);
11142 	struct ath12k_vif *ahvif = arvif->ahvif;
11143 	struct ieee80211_bss_conf *link_conf;
11144 	unsigned int dfs_cac_time;
11145 	int ret;
11146 
11147 	lockdep_assert_wiphy(hw->wiphy);
11148 
11149 	link_conf = ath12k_mac_get_link_bss_conf(arvif);
11150 	if (!link_conf) {
11151 		ath12k_warn(ar->ab, "unable to access bss link conf in vdev start for vif %pM link %u\n",
11152 			    ahvif->vif->addr, arvif->link_id);
11153 		return -ENOLINK;
11154 	}
11155 
11156 	reinit_completion(&ar->vdev_setup_done);
11157 
11158 	arg.vdev_id = arvif->vdev_id;
11159 	arg.dtim_period = arvif->dtim_period;
11160 	arg.bcn_intval = arvif->beacon_interval;
11161 	arg.punct_bitmap = ~arvif->punct_bitmap;
11162 
11163 	arg.freq = chandef->chan->center_freq;
11164 	arg.band_center_freq1 = chandef->center_freq1;
11165 	arg.band_center_freq2 = chandef->center_freq2;
11166 	arg.mode = ath12k_phymodes[chandef->chan->band][chandef->width];
11167 
11168 	arg.mode = ath12k_mac_check_down_grade_phy_mode(ar, arg.mode,
11169 							chandef->chan->band,
11170 							ahvif->vif->type);
11171 	arg.min_power = 0;
11172 	arg.max_power = chandef->chan->max_power;
11173 	arg.max_reg_power = chandef->chan->max_reg_power;
11174 	arg.max_antenna_gain = chandef->chan->max_antenna_gain;
11175 
11176 	arg.pref_tx_streams = ar->num_tx_chains;
11177 	arg.pref_rx_streams = ar->num_rx_chains;
11178 
11179 	arg.mbssid_flags = WMI_VDEV_MBSSID_FLAGS_NON_MBSSID_AP;
11180 	arg.mbssid_tx_vdev_id = 0;
11181 	if (test_bit(WMI_TLV_SERVICE_MBSS_PARAM_IN_VDEV_START_SUPPORT,
11182 		     ar->ab->wmi_ab.svc_map)) {
11183 		ret = ath12k_mac_setup_vdev_params_mbssid(arvif,
11184 							  &arg.mbssid_flags,
11185 							  &arg.mbssid_tx_vdev_id);
11186 		if (ret)
11187 			return ret;
11188 	}
11189 
11190 	if (ahvif->vdev_type == WMI_VDEV_TYPE_AP) {
11191 		arg.ssid = ahvif->u.ap.ssid;
11192 		arg.ssid_len = ahvif->u.ap.ssid_len;
11193 		arg.hidden_ssid = ahvif->u.ap.hidden_ssid;
11194 
11195 		/* For now allow DFS for AP mode */
11196 		arg.chan_radar = !!(chandef->chan->flags & IEEE80211_CHAN_RADAR);
11197 
11198 		arg.freq2_radar = ctx->radar_enabled;
11199 
11200 		arg.passive = arg.chan_radar;
11201 
11202 		spin_lock_bh(&ab->base_lock);
11203 		arg.regdomain = ar->ab->dfs_region;
11204 		spin_unlock_bh(&ab->base_lock);
11205 
11206 		/* TODO: Notify if secondary 80Mhz also needs radar detection */
11207 	}
11208 
11209 	arg.passive |= !!(chandef->chan->flags & IEEE80211_CHAN_NO_IR);
11210 
11211 	if (!restart)
11212 		ath12k_mac_mlo_get_vdev_args(arvif, &arg.ml);
11213 
11214 	ath12k_dbg(ab, ATH12K_DBG_MAC,
11215 		   "mac vdev %d start center_freq %d phymode %s punct_bitmap 0x%x\n",
11216 		   arg.vdev_id, arg.freq,
11217 		   ath12k_mac_phymode_str(arg.mode), arg.punct_bitmap);
11218 
11219 	ret = ath12k_wmi_vdev_start(ar, &arg, restart);
11220 	if (ret) {
11221 		ath12k_warn(ar->ab, "failed to %s WMI vdev %i\n",
11222 			    restart ? "restart" : "start", arg.vdev_id);
11223 		return ret;
11224 	}
11225 
11226 	ret = ath12k_mac_vdev_setup_sync(ar);
11227 	if (ret) {
11228 		ath12k_warn(ab, "failed to synchronize setup for vdev %i %s: %d\n",
11229 			    arg.vdev_id, restart ? "restart" : "start", ret);
11230 		return ret;
11231 	}
11232 
11233 	/* TODO: For now we only set TPC power here. However when
11234 	 * channel changes, say CSA, it should be updated again.
11235 	 */
11236 	if (ath12k_mac_supports_tpc(ar, ahvif, chandef)) {
11237 		ath12k_mac_fill_reg_tpc_info(ar, arvif, ctx);
11238 		ath12k_wmi_send_vdev_set_tpc_power(ar, arvif->vdev_id,
11239 						   &arvif->reg_tpc_info);
11240 	}
11241 
11242 	ar->num_started_vdevs++;
11243 	ath12k_dbg(ab, ATH12K_DBG_MAC,  "vdev %pM started, vdev_id %d\n",
11244 		   ahvif->vif->addr, arvif->vdev_id);
11245 
11246 	/* Enable CAC Running Flag in the driver by checking all sub-channel's DFS
11247 	 * state as NL80211_DFS_USABLE which indicates CAC needs to be
11248 	 * done before channel usage. This flag is used to drop rx packets.
11249 	 * during CAC.
11250 	 */
11251 	/* TODO: Set the flag for other interface types as required */
11252 	if (arvif->ahvif->vdev_type == WMI_VDEV_TYPE_AP && ctx->radar_enabled &&
11253 	    cfg80211_chandef_dfs_usable(hw->wiphy, chandef)) {
11254 		set_bit(ATH12K_FLAG_CAC_RUNNING, &ar->dev_flags);
11255 		dfs_cac_time = cfg80211_chandef_dfs_cac_time(hw->wiphy, chandef);
11256 
11257 		ath12k_dbg(ab, ATH12K_DBG_MAC,
11258 			   "CAC started dfs_cac_time %u center_freq %d center_freq1 %d for vdev %d\n",
11259 			   dfs_cac_time, arg.freq, arg.band_center_freq1, arg.vdev_id);
11260 	}
11261 
11262 	ret = ath12k_mac_set_txbf_conf(arvif);
11263 	if (ret)
11264 		ath12k_warn(ab, "failed to set txbf conf for vdev %d: %d\n",
11265 			    arvif->vdev_id, ret);
11266 
11267 	return 0;
11268 }
11269 
11270 static int ath12k_mac_vdev_start(struct ath12k_link_vif *arvif,
11271 				 struct ieee80211_chanctx_conf *ctx)
11272 {
11273 	return ath12k_mac_vdev_start_restart(arvif, ctx, false);
11274 }
11275 
11276 static int ath12k_mac_vdev_restart(struct ath12k_link_vif *arvif,
11277 				   struct ieee80211_chanctx_conf *ctx)
11278 {
11279 	return ath12k_mac_vdev_start_restart(arvif, ctx, true);
11280 }
11281 
11282 struct ath12k_mac_change_chanctx_arg {
11283 	struct ieee80211_chanctx_conf *ctx;
11284 	struct ieee80211_vif_chanctx_switch *vifs;
11285 	int n_vifs;
11286 	int next_vif;
11287 	struct ath12k *ar;
11288 };
11289 
11290 static void
11291 ath12k_mac_change_chanctx_cnt_iter(void *data, u8 *mac,
11292 				   struct ieee80211_vif *vif)
11293 {
11294 	struct ath12k_vif *ahvif = ath12k_vif_to_ahvif(vif);
11295 	struct ath12k_mac_change_chanctx_arg *arg = data;
11296 	struct ieee80211_bss_conf *link_conf;
11297 	struct ath12k_link_vif *arvif;
11298 	unsigned long links_map;
11299 	u8 link_id;
11300 
11301 	lockdep_assert_wiphy(ahvif->ah->hw->wiphy);
11302 
11303 	links_map = ahvif->links_map;
11304 	for_each_set_bit(link_id, &links_map, IEEE80211_MLD_MAX_NUM_LINKS) {
11305 		arvif = wiphy_dereference(ahvif->ah->hw->wiphy, ahvif->link[link_id]);
11306 		if (WARN_ON(!arvif))
11307 			continue;
11308 
11309 		if (!arvif->is_created || arvif->ar != arg->ar)
11310 			continue;
11311 
11312 		link_conf = wiphy_dereference(ahvif->ah->hw->wiphy,
11313 					      vif->link_conf[link_id]);
11314 		if (WARN_ON(!link_conf))
11315 			continue;
11316 
11317 		if (rcu_access_pointer(link_conf->chanctx_conf) != arg->ctx)
11318 			continue;
11319 
11320 		arg->n_vifs++;
11321 	}
11322 }
11323 
11324 static void
11325 ath12k_mac_change_chanctx_fill_iter(void *data, u8 *mac,
11326 				    struct ieee80211_vif *vif)
11327 {
11328 	struct ath12k_vif *ahvif = ath12k_vif_to_ahvif(vif);
11329 	struct ath12k_mac_change_chanctx_arg *arg = data;
11330 	struct ieee80211_bss_conf *link_conf;
11331 	struct ieee80211_chanctx_conf *ctx;
11332 	struct ath12k_link_vif *arvif;
11333 	unsigned long links_map;
11334 	u8 link_id;
11335 
11336 	lockdep_assert_wiphy(ahvif->ah->hw->wiphy);
11337 
11338 	links_map = ahvif->links_map;
11339 	for_each_set_bit(link_id, &links_map, IEEE80211_MLD_MAX_NUM_LINKS) {
11340 		arvif = wiphy_dereference(ahvif->ah->hw->wiphy, ahvif->link[link_id]);
11341 		if (WARN_ON(!arvif))
11342 			continue;
11343 
11344 		if (!arvif->is_created || arvif->ar != arg->ar)
11345 			continue;
11346 
11347 		link_conf = wiphy_dereference(ahvif->ah->hw->wiphy,
11348 					      vif->link_conf[arvif->link_id]);
11349 		if (WARN_ON(!link_conf))
11350 			continue;
11351 
11352 		ctx = rcu_access_pointer(link_conf->chanctx_conf);
11353 		if (ctx != arg->ctx)
11354 			continue;
11355 
11356 		if (WARN_ON(arg->next_vif == arg->n_vifs))
11357 			return;
11358 
11359 		arg->vifs[arg->next_vif].vif = vif;
11360 		arg->vifs[arg->next_vif].old_ctx = ctx;
11361 		arg->vifs[arg->next_vif].new_ctx = ctx;
11362 		arg->vifs[arg->next_vif].link_conf = link_conf;
11363 		arg->next_vif++;
11364 	}
11365 }
11366 
11367 static u32 ath12k_mac_nlwidth_to_wmiwidth(enum nl80211_chan_width width)
11368 {
11369 	switch (width) {
11370 	case NL80211_CHAN_WIDTH_20:
11371 		return WMI_CHAN_WIDTH_20;
11372 	case NL80211_CHAN_WIDTH_40:
11373 		return WMI_CHAN_WIDTH_40;
11374 	case NL80211_CHAN_WIDTH_80:
11375 		return WMI_CHAN_WIDTH_80;
11376 	case NL80211_CHAN_WIDTH_160:
11377 		return WMI_CHAN_WIDTH_160;
11378 	case NL80211_CHAN_WIDTH_80P80:
11379 		return WMI_CHAN_WIDTH_80P80;
11380 	case NL80211_CHAN_WIDTH_5:
11381 		return WMI_CHAN_WIDTH_5;
11382 	case NL80211_CHAN_WIDTH_10:
11383 		return WMI_CHAN_WIDTH_10;
11384 	case NL80211_CHAN_WIDTH_320:
11385 		return WMI_CHAN_WIDTH_320;
11386 	default:
11387 		WARN_ON(1);
11388 		return WMI_CHAN_WIDTH_20;
11389 	}
11390 }
11391 
11392 static int ath12k_mac_update_peer_puncturing_width(struct ath12k *ar,
11393 						   struct ath12k_link_vif *arvif,
11394 						   struct cfg80211_chan_def def)
11395 {
11396 	u32 param_id, param_value;
11397 	int ret;
11398 
11399 	if (arvif->ahvif->vdev_type != WMI_VDEV_TYPE_STA)
11400 		return 0;
11401 
11402 	param_id = WMI_PEER_CHWIDTH_PUNCTURE_20MHZ_BITMAP;
11403 	param_value = ath12k_mac_nlwidth_to_wmiwidth(def.width) |
11404 		u32_encode_bits((~def.punctured),
11405 				WMI_PEER_PUNCTURE_BITMAP);
11406 
11407 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
11408 		   "punctured bitmap %02x width %d vdev %d\n",
11409 		   def.punctured, def.width, arvif->vdev_id);
11410 
11411 	ret = ath12k_wmi_set_peer_param(ar, arvif->bssid,
11412 					arvif->vdev_id, param_id,
11413 					param_value);
11414 
11415 	return ret;
11416 }
11417 
11418 static void
11419 ath12k_mac_update_vif_chan(struct ath12k *ar,
11420 			   struct ieee80211_vif_chanctx_switch *vifs,
11421 			   int n_vifs)
11422 {
11423 	struct ath12k_wmi_vdev_up_params params = {};
11424 	struct ieee80211_bss_conf *link_conf;
11425 	struct ath12k_base *ab = ar->ab;
11426 	struct ath12k_link_vif *arvif;
11427 	struct ieee80211_vif *vif;
11428 	struct ath12k_vif *ahvif;
11429 	u8 link_id;
11430 	int ret;
11431 	int i;
11432 	bool monitor_vif = false;
11433 
11434 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
11435 
11436 	for (i = 0; i < n_vifs; i++) {
11437 		vif = vifs[i].vif;
11438 		ahvif = ath12k_vif_to_ahvif(vif);
11439 		link_conf = vifs[i].link_conf;
11440 		link_id = link_conf->link_id;
11441 		arvif = wiphy_dereference(ath12k_ar_to_hw(ar)->wiphy,
11442 					  ahvif->link[link_id]);
11443 
11444 		if (vif->type == NL80211_IFTYPE_MONITOR) {
11445 			monitor_vif = true;
11446 			continue;
11447 		}
11448 
11449 		ath12k_dbg(ab, ATH12K_DBG_MAC,
11450 			   "mac chanctx switch vdev_id %i freq %u->%u width %d->%d\n",
11451 			   arvif->vdev_id,
11452 			   vifs[i].old_ctx->def.chan->center_freq,
11453 			   vifs[i].new_ctx->def.chan->center_freq,
11454 			   vifs[i].old_ctx->def.width,
11455 			   vifs[i].new_ctx->def.width);
11456 
11457 		if (WARN_ON(!arvif->is_started))
11458 			continue;
11459 
11460 		arvif->punct_bitmap = vifs[i].new_ctx->def.punctured;
11461 
11462 		/* Firmware expect vdev_restart only if vdev is up.
11463 		 * If vdev is down then it expect vdev_stop->vdev_start.
11464 		 */
11465 		if (arvif->is_up) {
11466 			ret = ath12k_mac_vdev_restart(arvif, vifs[i].new_ctx);
11467 			if (ret) {
11468 				ath12k_warn(ab, "failed to restart vdev %d: %d\n",
11469 					    arvif->vdev_id, ret);
11470 				continue;
11471 			}
11472 		} else {
11473 			ret = ath12k_mac_vdev_stop(arvif);
11474 			if (ret) {
11475 				ath12k_warn(ab, "failed to stop vdev %d: %d\n",
11476 					    arvif->vdev_id, ret);
11477 				continue;
11478 			}
11479 
11480 			ret = ath12k_mac_vdev_start(arvif, vifs[i].new_ctx);
11481 			if (ret)
11482 				ath12k_warn(ab, "failed to start vdev %d: %d\n",
11483 					    arvif->vdev_id, ret);
11484 			continue;
11485 		}
11486 
11487 		ret = ath12k_mac_update_peer_puncturing_width(arvif->ar, arvif,
11488 							      vifs[i].new_ctx->def);
11489 		if (ret) {
11490 			ath12k_warn(ar->ab,
11491 				    "failed to update puncturing bitmap %02x and width %d: %d\n",
11492 				    vifs[i].new_ctx->def.punctured,
11493 				    vifs[i].new_ctx->def.width, ret);
11494 			continue;
11495 		}
11496 
11497 		/* Defer VDEV bring-up during CSA to avoid installing stale
11498 		 * beacon templates. The beacon content is updated only
11499 		 * after CSA finalize, so we mark CSA in progress and skip
11500 		 * VDEV_UP for now. It will be handled later in
11501 		 * bss_info_changed().
11502 		 */
11503 		if (link_conf->csa_active &&
11504 		    arvif->ahvif->vdev_type == WMI_VDEV_TYPE_AP) {
11505 			arvif->is_csa_in_progress = true;
11506 			continue;
11507 		}
11508 
11509 		ret = ath12k_mac_setup_bcn_tmpl(arvif);
11510 		if (ret)
11511 			ath12k_warn(ab, "failed to update bcn tmpl during csa: %d\n",
11512 				    ret);
11513 
11514 		memset(&params, 0, sizeof(params));
11515 		params.vdev_id = arvif->vdev_id;
11516 		params.aid = ahvif->aid;
11517 		params.bssid = arvif->bssid;
11518 		params.tx_bssid = ath12k_mac_get_tx_bssid(arvif);
11519 		if (params.tx_bssid) {
11520 			params.nontx_profile_idx = link_conf->bssid_index;
11521 			params.nontx_profile_cnt = 1 << link_conf->bssid_indicator;
11522 		}
11523 		ret = ath12k_wmi_vdev_up(arvif->ar, &params);
11524 		if (ret) {
11525 			ath12k_warn(ab, "failed to bring vdev up %d: %d\n",
11526 				    arvif->vdev_id, ret);
11527 			continue;
11528 		}
11529 	}
11530 
11531 	/* Restart the internal monitor vdev on new channel */
11532 	if (!monitor_vif && ar->monitor_vdev_created) {
11533 		if (!ath12k_mac_monitor_stop(ar))
11534 			ath12k_mac_monitor_start(ar);
11535 	}
11536 }
11537 
11538 static void
11539 ath12k_mac_update_active_vif_chan(struct ath12k *ar,
11540 				  struct ieee80211_chanctx_conf *ctx)
11541 {
11542 	struct ath12k_mac_change_chanctx_arg arg = { .ctx = ctx, .ar = ar };
11543 	struct ieee80211_hw *hw = ath12k_ar_to_hw(ar);
11544 
11545 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
11546 
11547 	ieee80211_iterate_active_interfaces_atomic(hw,
11548 						   IEEE80211_IFACE_ITER_NORMAL,
11549 						   ath12k_mac_change_chanctx_cnt_iter,
11550 						   &arg);
11551 	if (arg.n_vifs == 0)
11552 		return;
11553 
11554 	arg.vifs = kzalloc_objs(arg.vifs[0], arg.n_vifs);
11555 	if (!arg.vifs)
11556 		return;
11557 
11558 	ieee80211_iterate_active_interfaces_atomic(hw,
11559 						   IEEE80211_IFACE_ITER_NORMAL,
11560 						   ath12k_mac_change_chanctx_fill_iter,
11561 						   &arg);
11562 
11563 	ath12k_mac_update_vif_chan(ar, arg.vifs, arg.n_vifs);
11564 
11565 	kfree(arg.vifs);
11566 }
11567 
11568 void ath12k_mac_op_change_chanctx(struct ieee80211_hw *hw,
11569 				  struct ieee80211_chanctx_conf *ctx,
11570 				  u32 changed)
11571 {
11572 	struct ath12k *ar;
11573 	struct ath12k_base *ab;
11574 
11575 	lockdep_assert_wiphy(hw->wiphy);
11576 
11577 	ar = ath12k_get_ar_by_ctx(hw, ctx);
11578 	if (!ar)
11579 		return;
11580 
11581 	ab = ar->ab;
11582 
11583 	ath12k_dbg(ab, ATH12K_DBG_MAC,
11584 		   "mac chanctx change freq %u width %d ptr %p changed %x\n",
11585 		   ctx->def.chan->center_freq, ctx->def.width, ctx, changed);
11586 
11587 	/* This shouldn't really happen because channel switching should use
11588 	 * switch_vif_chanctx().
11589 	 */
11590 	if (WARN_ON(changed & IEEE80211_CHANCTX_CHANGE_CHANNEL))
11591 		return;
11592 
11593 	if (changed & IEEE80211_CHANCTX_CHANGE_WIDTH ||
11594 	    changed & IEEE80211_CHANCTX_CHANGE_RADAR ||
11595 	    changed & IEEE80211_CHANCTX_CHANGE_PUNCTURING)
11596 		ath12k_mac_update_active_vif_chan(ar, ctx);
11597 
11598 	/* TODO: Recalc radar detection */
11599 }
11600 EXPORT_SYMBOL(ath12k_mac_op_change_chanctx);
11601 
11602 static int ath12k_start_vdev_delay(struct ath12k *ar,
11603 				   struct ath12k_link_vif *arvif)
11604 {
11605 	struct ath12k_base *ab = ar->ab;
11606 	struct ath12k_vif *ahvif = arvif->ahvif;
11607 	struct ieee80211_vif *vif = ath12k_ahvif_to_vif(arvif->ahvif);
11608 	struct ieee80211_chanctx_conf *chanctx;
11609 	struct ieee80211_bss_conf *link_conf;
11610 	int ret;
11611 
11612 	if (WARN_ON(arvif->is_started))
11613 		return -EBUSY;
11614 
11615 	link_conf = ath12k_mac_get_link_bss_conf(arvif);
11616 	if (!link_conf) {
11617 		ath12k_warn(ab, "failed to get link conf for vdev %u\n", arvif->vdev_id);
11618 		return -EINVAL;
11619 	}
11620 
11621 	chanctx	= wiphy_dereference(ath12k_ar_to_hw(arvif->ar)->wiphy,
11622 				    link_conf->chanctx_conf);
11623 	ret = ath12k_mac_vdev_start(arvif, chanctx);
11624 	if (ret) {
11625 		ath12k_warn(ab, "failed to start vdev %i addr %pM on freq %d: %d\n",
11626 			    arvif->vdev_id, vif->addr,
11627 			    chanctx->def.chan->center_freq, ret);
11628 		return ret;
11629 	}
11630 
11631 	if (ahvif->vdev_type == WMI_VDEV_TYPE_MONITOR) {
11632 		ret = ath12k_monitor_vdev_up(ar, arvif->vdev_id);
11633 		if (ret) {
11634 			ath12k_warn(ab, "failed put monitor up: %d\n", ret);
11635 			return ret;
11636 		}
11637 	}
11638 
11639 	arvif->is_started = true;
11640 
11641 	/* TODO: Setup ps and cts/rts protection */
11642 	return 0;
11643 }
11644 
11645 static u8 ath12k_mac_get_num_pwr_levels(struct cfg80211_chan_def *chan_def)
11646 {
11647 	if (chan_def->chan->flags & IEEE80211_CHAN_PSD) {
11648 		switch (chan_def->width) {
11649 		case NL80211_CHAN_WIDTH_20:
11650 			return 1;
11651 		case NL80211_CHAN_WIDTH_40:
11652 			return 2;
11653 		case NL80211_CHAN_WIDTH_80:
11654 			return 4;
11655 		case NL80211_CHAN_WIDTH_160:
11656 			return 8;
11657 		case NL80211_CHAN_WIDTH_320:
11658 			return 16;
11659 		default:
11660 			return 1;
11661 		}
11662 	} else {
11663 		switch (chan_def->width) {
11664 		case NL80211_CHAN_WIDTH_20:
11665 			return 1;
11666 		case NL80211_CHAN_WIDTH_40:
11667 			return 2;
11668 		case NL80211_CHAN_WIDTH_80:
11669 			return 3;
11670 		case NL80211_CHAN_WIDTH_160:
11671 			return 4;
11672 		case NL80211_CHAN_WIDTH_320:
11673 			return 5;
11674 		default:
11675 			return 1;
11676 		}
11677 	}
11678 }
11679 
11680 static u16 ath12k_mac_get_6ghz_start_frequency(struct cfg80211_chan_def *chan_def)
11681 {
11682 	u16 diff_seq;
11683 
11684 	/* It is to get the lowest channel number's center frequency of the chan.
11685 	 * For example,
11686 	 * bandwidth=40 MHz, center frequency is 5965, lowest channel is 1
11687 	 * with center frequency 5955, its diff is 5965 - 5955 = 10.
11688 	 * bandwidth=80 MHz, center frequency is 5985, lowest channel is 1
11689 	 * with center frequency 5955, its diff is 5985 - 5955 = 30.
11690 	 * bandwidth=160 MHz, center frequency is 6025, lowest channel is 1
11691 	 * with center frequency 5955, its diff is 6025 - 5955 = 70.
11692 	 * bandwidth=320 MHz, center frequency is 6105, lowest channel is 1
11693 	 * with center frequency 5955, its diff is 6105 - 5955 = 70.
11694 	 */
11695 	switch (chan_def->width) {
11696 	case NL80211_CHAN_WIDTH_320:
11697 		diff_seq = 150;
11698 		break;
11699 	case NL80211_CHAN_WIDTH_160:
11700 		diff_seq = 70;
11701 		break;
11702 	case NL80211_CHAN_WIDTH_80:
11703 		diff_seq = 30;
11704 		break;
11705 	case NL80211_CHAN_WIDTH_40:
11706 		diff_seq = 10;
11707 		break;
11708 	default:
11709 		diff_seq = 0;
11710 	}
11711 
11712 	return chan_def->center_freq1 - diff_seq;
11713 }
11714 
11715 static u16 ath12k_mac_get_seg_freq(struct cfg80211_chan_def *chan_def,
11716 				   u16 start_seq, u8 seq)
11717 {
11718 	u16 seg_seq;
11719 
11720 	/* It is to get the center frequency of the specific bandwidth.
11721 	 * start_seq means the lowest channel number's center frequency.
11722 	 * seq 0/1/2/3 means 20 MHz/40 MHz/80 MHz/160 MHz.
11723 	 * For example,
11724 	 * lowest channel is 1, its center frequency 5955,
11725 	 * center frequency is 5955 when bandwidth=20 MHz, its diff is 5955 - 5955 = 0.
11726 	 * lowest channel is 1, its center frequency 5955,
11727 	 * center frequency is 5965 when bandwidth=40 MHz, its diff is 5965 - 5955 = 10.
11728 	 * lowest channel is 1, its center frequency 5955,
11729 	 * center frequency is 5985 when bandwidth=80 MHz, its diff is 5985 - 5955 = 30.
11730 	 * lowest channel is 1, its center frequency 5955,
11731 	 * center frequency is 6025 when bandwidth=160 MHz, its diff is 6025 - 5955 = 70.
11732 	 */
11733 	seg_seq = 10 * (BIT(seq) - 1);
11734 	return seg_seq + start_seq;
11735 }
11736 
11737 static void ath12k_mac_get_psd_channel(struct ath12k *ar,
11738 				       u16 step_freq,
11739 				       u16 *start_freq,
11740 				       u16 *center_freq,
11741 				       u8 i,
11742 				       struct ieee80211_channel **temp_chan,
11743 				       s8 *tx_power)
11744 {
11745 	/* It is to get the center frequency for each 20 MHz.
11746 	 * For example, if the chan is 160 MHz and center frequency is 6025,
11747 	 * then it include 8 channels, they are 1/5/9/13/17/21/25/29,
11748 	 * channel number 1's center frequency is 5955, it is parameter start_freq.
11749 	 * parameter i is the step of the 8 channels. i is 0~7 for the 8 channels.
11750 	 * the channel 1/5/9/13/17/21/25/29 maps i=0/1/2/3/4/5/6/7,
11751 	 * and maps its center frequency is 5955/5975/5995/6015/6035/6055/6075/6095,
11752 	 * the gap is 20 for each channel, parameter step_freq means the gap.
11753 	 * after get the center frequency of each channel, it is easy to find the
11754 	 * struct ieee80211_channel of it and get the max_reg_power.
11755 	 */
11756 	*center_freq = *start_freq + i * step_freq;
11757 	*temp_chan = ieee80211_get_channel(ar->ah->hw->wiphy, *center_freq);
11758 	*tx_power = (*temp_chan)->max_reg_power;
11759 }
11760 
11761 static void ath12k_mac_get_eirp_power(struct ath12k *ar,
11762 				      u16 *start_freq,
11763 				      u16 *center_freq,
11764 				      u8 i,
11765 				      struct ieee80211_channel **temp_chan,
11766 				      struct cfg80211_chan_def *def,
11767 				      s8 *tx_power)
11768 {
11769 	/* It is to get the center frequency for 20 MHz/40 MHz/80 MHz/
11770 	 * 160 MHz bandwidth, and then plus 10 to the center frequency,
11771 	 * it is the center frequency of a channel number.
11772 	 * For example, when configured channel number is 1.
11773 	 * center frequency is 5965 when bandwidth=40 MHz, after plus 10, it is 5975,
11774 	 * then it is channel number 5.
11775 	 * center frequency is 5985 when bandwidth=80 MHz, after plus 10, it is 5995,
11776 	 * then it is channel number 9.
11777 	 * center frequency is 6025 when bandwidth=160 MHz, after plus 10, it is 6035,
11778 	 * then it is channel number 17.
11779 	 * after get the center frequency of each channel, it is easy to find the
11780 	 * struct ieee80211_channel of it and get the max_reg_power.
11781 	 */
11782 	*center_freq = ath12k_mac_get_seg_freq(def, *start_freq, i);
11783 
11784 	/* For the 20 MHz, its center frequency is same with same channel */
11785 	if (i != 0)
11786 		*center_freq += 10;
11787 
11788 	*temp_chan = ieee80211_get_channel(ar->ah->hw->wiphy, *center_freq);
11789 	*tx_power = (*temp_chan)->max_reg_power;
11790 }
11791 
11792 void ath12k_mac_fill_reg_tpc_info(struct ath12k *ar,
11793 				  struct ath12k_link_vif *arvif,
11794 				  struct ieee80211_chanctx_conf *ctx)
11795 {
11796 	struct ath12k_base *ab = ar->ab;
11797 	struct ath12k_reg_tpc_power_info *reg_tpc_info = &arvif->reg_tpc_info;
11798 	struct ieee80211_bss_conf *bss_conf = ath12k_mac_get_link_bss_conf(arvif);
11799 	struct ieee80211_channel *chan, *temp_chan;
11800 	u8 pwr_lvl_idx, num_pwr_levels, pwr_reduction;
11801 	bool is_psd_power = false, is_tpe_present = false;
11802 	s8 max_tx_power[ATH12K_NUM_PWR_LEVELS], psd_power, tx_power;
11803 	s8 eirp_power = 0;
11804 	struct ath12k_vif *ahvif = arvif->ahvif;
11805 	u16 start_freq, center_freq;
11806 	u8 reg_6ghz_power_mode;
11807 
11808 	chan = ctx->def.chan;
11809 	start_freq = ath12k_mac_get_6ghz_start_frequency(&ctx->def);
11810 	pwr_reduction = bss_conf->pwr_reduction;
11811 
11812 	if (arvif->reg_tpc_info.num_pwr_levels) {
11813 		is_tpe_present = true;
11814 		num_pwr_levels = arvif->reg_tpc_info.num_pwr_levels;
11815 	} else {
11816 		num_pwr_levels = ath12k_mac_get_num_pwr_levels(&ctx->def);
11817 	}
11818 
11819 	for (pwr_lvl_idx = 0; pwr_lvl_idx < num_pwr_levels; pwr_lvl_idx++) {
11820 		/* STA received TPE IE*/
11821 		if (is_tpe_present) {
11822 			/* local power is PSD power*/
11823 			if (chan->flags & IEEE80211_CHAN_PSD) {
11824 				/* Connecting AP is psd power */
11825 				if (reg_tpc_info->is_psd_power) {
11826 					is_psd_power = true;
11827 					ath12k_mac_get_psd_channel(ar, 20,
11828 								   &start_freq,
11829 								   &center_freq,
11830 								   pwr_lvl_idx,
11831 								   &temp_chan,
11832 								   &tx_power);
11833 					psd_power = temp_chan->psd;
11834 					eirp_power = tx_power;
11835 					max_tx_power[pwr_lvl_idx] =
11836 						min_t(s8,
11837 						      psd_power,
11838 						      reg_tpc_info->tpe[pwr_lvl_idx]);
11839 				/* Connecting AP is not psd power */
11840 				} else {
11841 					ath12k_mac_get_eirp_power(ar,
11842 								  &start_freq,
11843 								  &center_freq,
11844 								  pwr_lvl_idx,
11845 								  &temp_chan,
11846 								  &ctx->def,
11847 								  &tx_power);
11848 					psd_power = temp_chan->psd;
11849 					/* convert psd power to EIRP power based
11850 					 * on channel width
11851 					 */
11852 					tx_power =
11853 						min_t(s8, tx_power,
11854 						      psd_power + 13 + pwr_lvl_idx * 3);
11855 					max_tx_power[pwr_lvl_idx] =
11856 						min_t(s8,
11857 						      tx_power,
11858 						      reg_tpc_info->tpe[pwr_lvl_idx]);
11859 				}
11860 			/* local power is not PSD power */
11861 			} else {
11862 				/* Connecting AP is psd power */
11863 				if (reg_tpc_info->is_psd_power) {
11864 					is_psd_power = true;
11865 					ath12k_mac_get_psd_channel(ar, 20,
11866 								   &start_freq,
11867 								   &center_freq,
11868 								   pwr_lvl_idx,
11869 								   &temp_chan,
11870 								   &tx_power);
11871 					eirp_power = tx_power;
11872 					max_tx_power[pwr_lvl_idx] =
11873 						reg_tpc_info->tpe[pwr_lvl_idx];
11874 				/* Connecting AP is not psd power */
11875 				} else {
11876 					ath12k_mac_get_eirp_power(ar,
11877 								  &start_freq,
11878 								  &center_freq,
11879 								  pwr_lvl_idx,
11880 								  &temp_chan,
11881 								  &ctx->def,
11882 								  &tx_power);
11883 					max_tx_power[pwr_lvl_idx] =
11884 						min_t(s8,
11885 						      tx_power,
11886 						      reg_tpc_info->tpe[pwr_lvl_idx]);
11887 				}
11888 			}
11889 		/* STA not received TPE IE */
11890 		} else {
11891 			/* local power is PSD power*/
11892 			if (chan->flags & IEEE80211_CHAN_PSD) {
11893 				is_psd_power = true;
11894 				ath12k_mac_get_psd_channel(ar, 20,
11895 							   &start_freq,
11896 							   &center_freq,
11897 							   pwr_lvl_idx,
11898 							   &temp_chan,
11899 							   &tx_power);
11900 				psd_power = temp_chan->psd;
11901 				eirp_power = tx_power;
11902 				max_tx_power[pwr_lvl_idx] = psd_power;
11903 			} else {
11904 				ath12k_mac_get_eirp_power(ar,
11905 							  &start_freq,
11906 							  &center_freq,
11907 							  pwr_lvl_idx,
11908 							  &temp_chan,
11909 							  &ctx->def,
11910 							  &tx_power);
11911 				max_tx_power[pwr_lvl_idx] = tx_power;
11912 			}
11913 		}
11914 
11915 		if (is_psd_power) {
11916 			/* If AP local power constraint is present */
11917 			if (pwr_reduction)
11918 				eirp_power = eirp_power - pwr_reduction;
11919 
11920 			/* If firmware updated max tx power is non zero, then take
11921 			 * the min of firmware updated ap tx power
11922 			 * and max power derived from above mentioned parameters.
11923 			 */
11924 			ath12k_dbg(ab, ATH12K_DBG_MAC,
11925 				   "eirp power : %d firmware report power : %d\n",
11926 				   eirp_power, ar->max_allowed_tx_power);
11927 			/* Firmware reports lower max_allowed_tx_power during vdev
11928 			 * start response. In case of 6 GHz, firmware is not aware
11929 			 * of EIRP power unless driver sets EIRP power through WMI
11930 			 * TPC command. So radio which does not support idle power
11931 			 * save can set maximum calculated EIRP power directly to
11932 			 * firmware through TPC command without min comparison with
11933 			 * vdev start response's max_allowed_tx_power.
11934 			 */
11935 			if (ar->max_allowed_tx_power && ab->hw_params->idle_ps)
11936 				eirp_power = min_t(s8,
11937 						   eirp_power,
11938 						   ar->max_allowed_tx_power);
11939 		} else {
11940 			/* If AP local power constraint is present */
11941 			if (pwr_reduction)
11942 				max_tx_power[pwr_lvl_idx] =
11943 					max_tx_power[pwr_lvl_idx] - pwr_reduction;
11944 			/* If firmware updated max tx power is non zero, then take
11945 			 * the min of firmware updated ap tx power
11946 			 * and max power derived from above mentioned parameters.
11947 			 */
11948 			if (ar->max_allowed_tx_power && ab->hw_params->idle_ps)
11949 				max_tx_power[pwr_lvl_idx] =
11950 					min_t(s8,
11951 					      max_tx_power[pwr_lvl_idx],
11952 					      ar->max_allowed_tx_power);
11953 		}
11954 		reg_tpc_info->chan_power_info[pwr_lvl_idx].chan_cfreq = center_freq;
11955 		reg_tpc_info->chan_power_info[pwr_lvl_idx].tx_power =
11956 			max_tx_power[pwr_lvl_idx];
11957 	}
11958 
11959 	reg_tpc_info->num_pwr_levels = num_pwr_levels;
11960 	reg_tpc_info->is_psd_power = is_psd_power;
11961 	reg_tpc_info->eirp_power = eirp_power;
11962 	if (ahvif->vdev_type == WMI_VDEV_TYPE_STA)
11963 		reg_6ghz_power_mode = bss_conf->power_type;
11964 	else
11965 		/* For now, LPI is the only supported AP power mode */
11966 		reg_6ghz_power_mode = IEEE80211_REG_LPI_AP;
11967 
11968 	reg_tpc_info->ap_power_type =
11969 		ath12k_reg_ap_pwr_convert(reg_6ghz_power_mode);
11970 }
11971 
11972 static void ath12k_mac_parse_tx_pwr_env(struct ath12k *ar,
11973 					struct ath12k_link_vif *arvif)
11974 {
11975 	struct ieee80211_bss_conf *bss_conf = ath12k_mac_get_link_bss_conf(arvif);
11976 	struct ath12k_reg_tpc_power_info *tpc_info = &arvif->reg_tpc_info;
11977 	struct ieee80211_parsed_tpe_eirp *local_non_psd, *reg_non_psd;
11978 	struct ieee80211_parsed_tpe_psd *local_psd, *reg_psd;
11979 	struct ieee80211_parsed_tpe *tpe = &bss_conf->tpe;
11980 	enum wmi_reg_6g_client_type client_type;
11981 	struct ath12k_reg_info *reg_info;
11982 	struct ath12k_base *ab = ar->ab;
11983 	bool psd_valid, non_psd_valid;
11984 	int i;
11985 
11986 	reg_info = ab->reg_info[ar->pdev_idx];
11987 	client_type = reg_info->client_type;
11988 
11989 	local_psd = &tpe->psd_local[client_type];
11990 	reg_psd = &tpe->psd_reg_client[client_type];
11991 	local_non_psd = &tpe->max_local[client_type];
11992 	reg_non_psd = &tpe->max_reg_client[client_type];
11993 
11994 	psd_valid = local_psd->valid | reg_psd->valid;
11995 	non_psd_valid = local_non_psd->valid | reg_non_psd->valid;
11996 
11997 	if (!psd_valid && !non_psd_valid) {
11998 		ath12k_warn(ab,
11999 			    "no transmit power envelope match client power type %d\n",
12000 			    client_type);
12001 		return;
12002 	}
12003 
12004 	if (psd_valid) {
12005 		tpc_info->is_psd_power = true;
12006 
12007 		tpc_info->num_pwr_levels = max(local_psd->count,
12008 					       reg_psd->count);
12009 		tpc_info->num_pwr_levels =
12010 				min3(tpc_info->num_pwr_levels,
12011 				     IEEE80211_TPE_PSD_ENTRIES_320MHZ,
12012 				     ATH12K_NUM_PWR_LEVELS);
12013 
12014 		for (i = 0; i < tpc_info->num_pwr_levels; i++) {
12015 			tpc_info->tpe[i] = min(local_psd->power[i],
12016 					       reg_psd->power[i]) / 2;
12017 			ath12k_dbg(ab, ATH12K_DBG_MAC,
12018 				   "TPE PSD power[%d] : %d\n",
12019 				   i, tpc_info->tpe[i]);
12020 		}
12021 	} else {
12022 		tpc_info->is_psd_power = false;
12023 		tpc_info->eirp_power = 0;
12024 
12025 		tpc_info->num_pwr_levels = max(local_non_psd->count,
12026 					       reg_non_psd->count);
12027 		tpc_info->num_pwr_levels =
12028 				min3(tpc_info->num_pwr_levels,
12029 				     IEEE80211_TPE_EIRP_ENTRIES_320MHZ,
12030 				     ATH12K_NUM_PWR_LEVELS);
12031 
12032 		for (i = 0; i < tpc_info->num_pwr_levels; i++) {
12033 			tpc_info->tpe[i] = min(local_non_psd->power[i],
12034 					       reg_non_psd->power[i]) / 2;
12035 			ath12k_dbg(ab, ATH12K_DBG_MAC,
12036 				   "non PSD power[%d] : %d\n",
12037 				   i, tpc_info->tpe[i]);
12038 		}
12039 	}
12040 }
12041 
12042 int
12043 ath12k_mac_op_assign_vif_chanctx(struct ieee80211_hw *hw,
12044 				 struct ieee80211_vif *vif,
12045 				 struct ieee80211_bss_conf *link_conf,
12046 				 struct ieee80211_chanctx_conf *ctx)
12047 {
12048 	struct ath12k_hw *ah = ath12k_hw_to_ah(hw);
12049 	struct ath12k *ar;
12050 	struct ath12k_base *ab;
12051 	struct ath12k_vif *ahvif = ath12k_vif_to_ahvif(vif);
12052 	u8 link_id = link_conf->link_id;
12053 	struct ath12k_link_vif *arvif;
12054 	int ret;
12055 
12056 	lockdep_assert_wiphy(hw->wiphy);
12057 
12058 	/* For multi radio wiphy, the vdev was not created during add_interface
12059 	 * create now since we have a channel ctx now to assign to a specific ar/fw
12060 	 */
12061 	arvif = ath12k_mac_assign_link_vif(ah, vif, link_id);
12062 	if (!arvif) {
12063 		WARN_ON(1);
12064 		return -ENOMEM;
12065 	}
12066 
12067 	ar = ath12k_mac_assign_vif_to_vdev(hw, arvif, ctx);
12068 	if (!ar) {
12069 		ath12k_hw_warn(ah, "failed to assign chanctx for vif %pM link id %u link vif is already started",
12070 			       vif->addr, link_id);
12071 		return -EINVAL;
12072 	}
12073 
12074 	ab = ar->ab;
12075 
12076 	ath12k_dbg(ab, ATH12K_DBG_MAC,
12077 		   "mac chanctx assign ptr %p vdev_id %i\n",
12078 		   ctx, arvif->vdev_id);
12079 
12080 	if (ath12k_wmi_supports_6ghz_cc_ext(ar) &&
12081 	    ctx->def.chan->band == NL80211_BAND_6GHZ &&
12082 	    ahvif->vdev_type == WMI_VDEV_TYPE_STA)
12083 		ath12k_mac_parse_tx_pwr_env(ar, arvif);
12084 
12085 	arvif->punct_bitmap = ctx->def.punctured;
12086 
12087 	/* for some targets bss peer must be created before vdev_start */
12088 	if (ab->hw_params->vdev_start_delay &&
12089 	    ahvif->vdev_type != WMI_VDEV_TYPE_AP &&
12090 	    ahvif->vdev_type != WMI_VDEV_TYPE_MONITOR &&
12091 	    !ath12k_dp_link_peer_exist_by_vdev_id(ath12k_ab_to_dp(ab), arvif->vdev_id)) {
12092 		ret = 0;
12093 		goto out;
12094 	}
12095 
12096 	if (WARN_ON(arvif->is_started)) {
12097 		ret = -EBUSY;
12098 		goto out;
12099 	}
12100 
12101 	if (ahvif->vdev_type == WMI_VDEV_TYPE_MONITOR) {
12102 		ret = ath12k_mac_monitor_start(ar);
12103 		if (ret) {
12104 			ath12k_mac_monitor_vdev_delete(ar);
12105 			goto out;
12106 		}
12107 
12108 		arvif->is_started = true;
12109 		goto out;
12110 	}
12111 
12112 	ret = ath12k_mac_vdev_start(arvif, ctx);
12113 	if (ret) {
12114 		ath12k_warn(ab, "failed to start vdev %i addr %pM on freq %d: %d\n",
12115 			    arvif->vdev_id, vif->addr,
12116 			    ctx->def.chan->center_freq, ret);
12117 		goto out;
12118 	}
12119 
12120 	arvif->is_started = true;
12121 
12122 	/* TODO: Setup ps and cts/rts protection */
12123 
12124 out:
12125 	return ret;
12126 }
12127 EXPORT_SYMBOL(ath12k_mac_op_assign_vif_chanctx);
12128 
12129 void
12130 ath12k_mac_op_unassign_vif_chanctx(struct ieee80211_hw *hw,
12131 				   struct ieee80211_vif *vif,
12132 				   struct ieee80211_bss_conf *link_conf,
12133 				   struct ieee80211_chanctx_conf *ctx)
12134 {
12135 	struct ath12k *ar;
12136 	struct ath12k_base *ab;
12137 	struct ath12k_vif *ahvif = ath12k_vif_to_ahvif(vif);
12138 	struct ath12k_link_vif *arvif;
12139 	u8 link_id = link_conf->link_id;
12140 	int ret;
12141 
12142 	lockdep_assert_wiphy(hw->wiphy);
12143 
12144 	arvif = wiphy_dereference(hw->wiphy, ahvif->link[link_id]);
12145 
12146 	/* The vif is expected to be attached to an ar's VDEV.
12147 	 * We leave the vif/vdev in this function as is
12148 	 * and not delete the vdev symmetric to assign_vif_chanctx()
12149 	 * the VDEV will be deleted and unassigned either during
12150 	 * remove_interface() or when there is a change in channel
12151 	 * that moves the vif to a new ar
12152 	 */
12153 	if (!arvif || !arvif->is_created)
12154 		return;
12155 
12156 	ar = arvif->ar;
12157 	ab = ar->ab;
12158 
12159 	ath12k_dbg(ab, ATH12K_DBG_MAC,
12160 		   "mac chanctx unassign ptr %p vdev_id %i\n",
12161 		   ctx, arvif->vdev_id);
12162 
12163 	WARN_ON(!arvif->is_started);
12164 
12165 	if (ahvif->vdev_type == WMI_VDEV_TYPE_MONITOR) {
12166 		ret = ath12k_mac_monitor_stop(ar);
12167 		if (ret)
12168 			return;
12169 
12170 		arvif->is_started = false;
12171 	}
12172 
12173 	if (ahvif->vdev_type != WMI_VDEV_TYPE_STA &&
12174 	    ahvif->vdev_type != WMI_VDEV_TYPE_MONITOR) {
12175 		ath12k_bss_disassoc(ar, arvif);
12176 		ret = ath12k_mac_vdev_stop(arvif);
12177 		if (ret)
12178 			ath12k_warn(ab, "failed to stop vdev %i: %d\n",
12179 				    arvif->vdev_id, ret);
12180 	}
12181 	arvif->is_started = false;
12182 
12183 	if (test_bit(WMI_TLV_SERVICE_11D_OFFLOAD, ab->wmi_ab.svc_map) &&
12184 	    ahvif->vdev_type == WMI_VDEV_TYPE_STA &&
12185 	    ahvif->vdev_subtype == WMI_VDEV_SUBTYPE_NONE &&
12186 	    ar->state_11d != ATH12K_11D_PREPARING) {
12187 		reinit_completion(&ar->completed_11d_scan);
12188 		ar->state_11d = ATH12K_11D_PREPARING;
12189 	}
12190 
12191 	if (ar->scan.arvif == arvif && ar->scan.state == ATH12K_SCAN_RUNNING) {
12192 		ath12k_scan_abort(ar);
12193 		ar->scan.arvif = NULL;
12194 	}
12195 }
12196 EXPORT_SYMBOL(ath12k_mac_op_unassign_vif_chanctx);
12197 
12198 int
12199 ath12k_mac_op_switch_vif_chanctx(struct ieee80211_hw *hw,
12200 				 struct ieee80211_vif_chanctx_switch *vifs,
12201 				 int n_vifs,
12202 				 enum ieee80211_chanctx_switch_mode mode)
12203 {
12204 	struct ath12k *ar;
12205 
12206 	lockdep_assert_wiphy(hw->wiphy);
12207 
12208 	ar = ath12k_get_ar_by_ctx(hw, vifs->old_ctx);
12209 	if (!ar)
12210 		return -EINVAL;
12211 
12212 	/* Switching channels across radio is not allowed */
12213 	if (ar != ath12k_get_ar_by_ctx(hw, vifs->new_ctx))
12214 		return -EINVAL;
12215 
12216 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
12217 		   "mac chanctx switch n_vifs %d mode %d\n",
12218 		   n_vifs, mode);
12219 	ath12k_mac_update_vif_chan(ar, vifs, n_vifs);
12220 
12221 	return 0;
12222 }
12223 EXPORT_SYMBOL(ath12k_mac_op_switch_vif_chanctx);
12224 
12225 static int
12226 ath12k_set_vdev_param_to_all_vifs(struct ath12k *ar, int param, u32 value)
12227 {
12228 	struct ath12k_link_vif *arvif;
12229 	int ret = 0;
12230 
12231 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
12232 
12233 	list_for_each_entry(arvif, &ar->arvifs, list) {
12234 		ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "setting mac vdev %d param %d value %d\n",
12235 			   param, arvif->vdev_id, value);
12236 
12237 		ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
12238 						    param, value);
12239 		if (ret) {
12240 			ath12k_warn(ar->ab, "failed to set param %d for vdev %d: %d\n",
12241 				    param, arvif->vdev_id, ret);
12242 			break;
12243 		}
12244 	}
12245 
12246 	return ret;
12247 }
12248 
12249 /* mac80211 stores device specific RTS/Fragmentation threshold value,
12250  * this is set interface specific to firmware from ath12k driver
12251  */
12252 int ath12k_mac_op_set_rts_threshold(struct ieee80211_hw *hw,
12253 				    int radio_idx, u32 value)
12254 {
12255 	struct ath12k_hw *ah = ath12k_hw_to_ah(hw);
12256 	struct wiphy *wiphy = hw->wiphy;
12257 	struct ath12k *ar;
12258 	int param_id = WMI_VDEV_PARAM_RTS_THRESHOLD;
12259 	int ret = 0, ret_err, i;
12260 
12261 	lockdep_assert_wiphy(hw->wiphy);
12262 
12263 	if (radio_idx >= wiphy->n_radio || radio_idx < -1)
12264 		return -EINVAL;
12265 
12266 	if (radio_idx != -1) {
12267 		/* Update RTS threshold in specified radio */
12268 		ar = ath12k_ah_to_ar(ah, radio_idx);
12269 		ret = ath12k_set_vdev_param_to_all_vifs(ar, param_id, value);
12270 		if (ret) {
12271 			ath12k_warn(ar->ab,
12272 				    "failed to set RTS config for all vdevs of pdev %d",
12273 				    ar->pdev->pdev_id);
12274 			return ret;
12275 		}
12276 
12277 		ar->rts_threshold = value;
12278 		return 0;
12279 	}
12280 
12281 	/* Radio_index passed is -1, so set RTS threshold for all radios. */
12282 	for_each_ar(ah, ar, i) {
12283 		ret = ath12k_set_vdev_param_to_all_vifs(ar, param_id, value);
12284 		if (ret) {
12285 			ath12k_warn(ar->ab, "failed to set RTS config for all vdevs of pdev %d",
12286 				    ar->pdev->pdev_id);
12287 			break;
12288 		}
12289 	}
12290 	if (!ret) {
12291 		/* Setting new RTS threshold for vdevs of all radios passed, so update
12292 		 * the RTS threshold value for all radios
12293 		 */
12294 		for_each_ar(ah, ar, i)
12295 			ar->rts_threshold = value;
12296 		return 0;
12297 	}
12298 
12299 	/* RTS threshold config failed, revert to the previous RTS threshold */
12300 	for (i = i - 1; i >= 0; i--) {
12301 		ar = ath12k_ah_to_ar(ah, i);
12302 		ret_err = ath12k_set_vdev_param_to_all_vifs(ar, param_id,
12303 							    ar->rts_threshold);
12304 		if (ret_err)
12305 			ath12k_warn(ar->ab,
12306 				    "failed to restore RTS threshold for all vdevs of pdev %d",
12307 				    ar->pdev->pdev_id);
12308 	}
12309 
12310 	return ret;
12311 }
12312 EXPORT_SYMBOL(ath12k_mac_op_set_rts_threshold);
12313 
12314 int ath12k_mac_op_set_frag_threshold(struct ieee80211_hw *hw,
12315 				     int radio_idx, u32 value)
12316 {
12317 	/* Even though there's a WMI vdev param for fragmentation threshold no
12318 	 * known firmware actually implements it. Moreover it is not possible to
12319 	 * rely frame fragmentation to mac80211 because firmware clears the
12320 	 * "more fragments" bit in frame control making it impossible for remote
12321 	 * devices to reassemble frames.
12322 	 *
12323 	 * Hence implement a dummy callback just to say fragmentation isn't
12324 	 * supported. This effectively prevents mac80211 from doing frame
12325 	 * fragmentation in software.
12326 	 */
12327 
12328 	lockdep_assert_wiphy(hw->wiphy);
12329 
12330 	return -EOPNOTSUPP;
12331 }
12332 EXPORT_SYMBOL(ath12k_mac_op_set_frag_threshold);
12333 
12334 static int ath12k_mac_flush(struct ath12k *ar)
12335 {
12336 	long time_left;
12337 	int ret = 0;
12338 
12339 	time_left = wait_event_timeout(ar->dp.tx_empty_waitq,
12340 				       (atomic_read(&ar->dp.num_tx_pending) == 0),
12341 				       ATH12K_FLUSH_TIMEOUT);
12342 	if (time_left == 0) {
12343 		ath12k_warn(ar->ab,
12344 			    "failed to flush transmit queue, data pkts pending %d\n",
12345 			    atomic_read(&ar->dp.num_tx_pending));
12346 		ret = -ETIMEDOUT;
12347 	}
12348 
12349 	time_left = wait_event_timeout(ar->txmgmt_empty_waitq,
12350 				       (atomic_read(&ar->num_pending_mgmt_tx) == 0),
12351 				       ATH12K_FLUSH_TIMEOUT);
12352 	if (time_left == 0) {
12353 		ath12k_warn(ar->ab,
12354 			    "failed to flush mgmt transmit queue, mgmt pkts pending %d\n",
12355 			    atomic_read(&ar->num_pending_mgmt_tx));
12356 		ret = -ETIMEDOUT;
12357 	}
12358 
12359 	return ret;
12360 }
12361 
12362 int ath12k_mac_wait_tx_complete(struct ath12k *ar)
12363 {
12364 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
12365 
12366 	ath12k_mac_drain_tx(ar);
12367 	return ath12k_mac_flush(ar);
12368 }
12369 
12370 void ath12k_mac_op_flush(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
12371 			 u32 queues, bool drop)
12372 {
12373 	struct ath12k_hw *ah = ath12k_hw_to_ah(hw);
12374 	struct ath12k_link_vif *arvif;
12375 	struct ath12k_vif *ahvif;
12376 	unsigned long links;
12377 	struct ath12k *ar;
12378 	u8 link_id;
12379 	int i;
12380 
12381 	lockdep_assert_wiphy(hw->wiphy);
12382 
12383 	if (drop)
12384 		return;
12385 
12386 	for_each_ar(ah, ar, i)
12387 		wiphy_work_flush(hw->wiphy, &ar->wmi_mgmt_tx_work);
12388 
12389 	/* vif can be NULL when flush() is considered for hw */
12390 	if (!vif) {
12391 		for_each_ar(ah, ar, i)
12392 			ath12k_mac_flush(ar);
12393 		return;
12394 	}
12395 
12396 	ahvif = ath12k_vif_to_ahvif(vif);
12397 	links = ahvif->links_map;
12398 	for_each_set_bit(link_id, &links, IEEE80211_MLD_MAX_NUM_LINKS) {
12399 		arvif = wiphy_dereference(hw->wiphy, ahvif->link[link_id]);
12400 		if (!(arvif && arvif->ar))
12401 			continue;
12402 
12403 		ath12k_mac_flush(arvif->ar);
12404 	}
12405 }
12406 EXPORT_SYMBOL(ath12k_mac_op_flush);
12407 
12408 static int
12409 ath12k_mac_bitrate_mask_num_ht_rates(struct ath12k *ar,
12410 				     enum nl80211_band band,
12411 				     const struct cfg80211_bitrate_mask *mask)
12412 {
12413 	int num_rates = 0;
12414 	int i;
12415 
12416 	for (i = 0; i < ARRAY_SIZE(mask->control[band].ht_mcs); i++)
12417 		num_rates += hweight16(mask->control[band].ht_mcs[i]);
12418 
12419 	return num_rates;
12420 }
12421 
12422 static bool
12423 ath12k_mac_has_single_legacy_rate(struct ath12k *ar,
12424 				  enum nl80211_band band,
12425 				  const struct cfg80211_bitrate_mask *mask)
12426 {
12427 	int num_rates = 0;
12428 
12429 	num_rates = hweight32(mask->control[band].legacy);
12430 
12431 	if (ath12k_mac_bitrate_mask_num_ht_rates(ar, band, mask))
12432 		return false;
12433 
12434 	if (ath12k_mac_bitrate_mask_num_vht_rates(ar, band, mask))
12435 		return false;
12436 
12437 	if (ath12k_mac_bitrate_mask_num_he_rates(ar, band, mask))
12438 		return false;
12439 
12440 	if (ath12k_mac_bitrate_mask_num_eht_rates(ar, band, mask))
12441 		return false;
12442 
12443 	return num_rates == 1;
12444 }
12445 
12446 static __le16
12447 ath12k_mac_get_tx_mcs_map(const struct ieee80211_sta_he_cap *he_cap)
12448 {
12449 	if (he_cap->he_cap_elem.phy_cap_info[0] &
12450 	    IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_160MHZ_IN_5G)
12451 		return he_cap->he_mcs_nss_supp.tx_mcs_160;
12452 
12453 	return he_cap->he_mcs_nss_supp.tx_mcs_80;
12454 }
12455 
12456 static bool
12457 ath12k_mac_bitrate_mask_get_single_nss(struct ath12k *ar,
12458 				       struct ieee80211_vif *vif,
12459 				       enum nl80211_band band,
12460 				       const struct cfg80211_bitrate_mask *mask,
12461 				       int *nss)
12462 {
12463 	struct ieee80211_supported_band *sband = &ar->mac.sbands[band];
12464 	u16 vht_mcs_map = le16_to_cpu(sband->vht_cap.vht_mcs.tx_mcs_map);
12465 	const struct ieee80211_sband_iftype_data *data;
12466 	const struct ieee80211_sta_he_cap *he_cap;
12467 	u16 he_mcs_map = 0;
12468 	u16 eht_mcs_map = 0;
12469 	u8 ht_nss_mask = 0;
12470 	u8 vht_nss_mask = 0;
12471 	u8 he_nss_mask = 0;
12472 	u8 eht_nss_mask = 0;
12473 	u8 mcs_nss_len;
12474 	int i;
12475 
12476 	/* No need to consider legacy here. Basic rates are always present
12477 	 * in bitrate mask
12478 	 */
12479 
12480 	for (i = 0; i < ARRAY_SIZE(mask->control[band].ht_mcs); i++) {
12481 		if (mask->control[band].ht_mcs[i] == 0)
12482 			continue;
12483 		else if (mask->control[band].ht_mcs[i] ==
12484 			 sband->ht_cap.mcs.rx_mask[i])
12485 			ht_nss_mask |= BIT(i);
12486 		else
12487 			return false;
12488 	}
12489 
12490 	for (i = 0; i < ARRAY_SIZE(mask->control[band].vht_mcs); i++) {
12491 		if (mask->control[band].vht_mcs[i] == 0)
12492 			continue;
12493 		else if (mask->control[band].vht_mcs[i] ==
12494 			 ath12k_mac_get_max_vht_mcs_map(vht_mcs_map, i))
12495 			vht_nss_mask |= BIT(i);
12496 		else
12497 			return false;
12498 	}
12499 
12500 	he_cap = ieee80211_get_he_iftype_cap_vif(sband, vif);
12501 	if (!he_cap)
12502 		return false;
12503 
12504 	he_mcs_map = le16_to_cpu(ath12k_mac_get_tx_mcs_map(he_cap));
12505 
12506 	for (i = 0; i < ARRAY_SIZE(mask->control[band].he_mcs); i++) {
12507 		if (mask->control[band].he_mcs[i] == 0)
12508 			continue;
12509 
12510 		if (mask->control[band].he_mcs[i] ==
12511 		    ath12k_mac_get_max_he_mcs_map(he_mcs_map, i))
12512 			he_nss_mask |= BIT(i);
12513 		else
12514 			return false;
12515 	}
12516 
12517 	data = ieee80211_get_sband_iftype_data(sband, vif->type);
12518 
12519 	mcs_nss_len = ieee80211_eht_mcs_nss_size(&data->he_cap.he_cap_elem,
12520 						 &data->eht_cap.eht_cap_elem,
12521 						 false);
12522 	if (mcs_nss_len == 4) {
12523 		/* 20 MHz only STA case */
12524 		const struct ieee80211_eht_mcs_nss_supp_20mhz_only *eht_mcs_nss =
12525 			&data->eht_cap.eht_mcs_nss_supp.only_20mhz;
12526 		if (eht_mcs_nss->rx_tx_mcs13_max_nss)
12527 			eht_mcs_map = 0x1fff;
12528 		else if (eht_mcs_nss->rx_tx_mcs11_max_nss)
12529 			eht_mcs_map = 0x07ff;
12530 		else if (eht_mcs_nss->rx_tx_mcs9_max_nss)
12531 			eht_mcs_map = 0x01ff;
12532 		else
12533 			eht_mcs_map = 0x007f;
12534 	} else {
12535 		const struct ieee80211_eht_mcs_nss_supp_bw *eht_mcs_nss;
12536 
12537 		switch (mcs_nss_len) {
12538 		case 9:
12539 			eht_mcs_nss = &data->eht_cap.eht_mcs_nss_supp.bw._320;
12540 			break;
12541 		case 6:
12542 			eht_mcs_nss = &data->eht_cap.eht_mcs_nss_supp.bw._160;
12543 			break;
12544 		case 3:
12545 			eht_mcs_nss = &data->eht_cap.eht_mcs_nss_supp.bw._80;
12546 			break;
12547 		default:
12548 			return false;
12549 		}
12550 
12551 		if (eht_mcs_nss->rx_tx_mcs13_max_nss)
12552 			eht_mcs_map = 0x1fff;
12553 		else if (eht_mcs_nss->rx_tx_mcs11_max_nss)
12554 			eht_mcs_map = 0x7ff;
12555 		else
12556 			eht_mcs_map = 0x1ff;
12557 	}
12558 
12559 	for (i = 0; i < ARRAY_SIZE(mask->control[band].eht_mcs); i++) {
12560 		if (mask->control[band].eht_mcs[i] == 0)
12561 			continue;
12562 
12563 		if (mask->control[band].eht_mcs[i] < eht_mcs_map)
12564 			eht_nss_mask |= BIT(i);
12565 		else
12566 			return false;
12567 	}
12568 
12569 	if (ht_nss_mask != vht_nss_mask || ht_nss_mask != he_nss_mask ||
12570 	    ht_nss_mask != eht_nss_mask)
12571 		return false;
12572 
12573 	if (ht_nss_mask == 0)
12574 		return false;
12575 
12576 	if (BIT(fls(ht_nss_mask)) - 1 != ht_nss_mask)
12577 		return false;
12578 
12579 	*nss = fls(ht_nss_mask);
12580 
12581 	return true;
12582 }
12583 
12584 static int
12585 ath12k_mac_get_single_legacy_rate(struct ath12k *ar,
12586 				  enum nl80211_band band,
12587 				  const struct cfg80211_bitrate_mask *mask,
12588 				  u32 *rate, u8 *nss)
12589 {
12590 	int rate_idx;
12591 	u16 bitrate;
12592 	u8 preamble;
12593 	u8 hw_rate;
12594 
12595 	if (hweight32(mask->control[band].legacy) != 1)
12596 		return -EINVAL;
12597 
12598 	rate_idx = ffs(mask->control[band].legacy) - 1;
12599 
12600 	if (band == NL80211_BAND_5GHZ || band == NL80211_BAND_6GHZ)
12601 		rate_idx += ATH12K_MAC_FIRST_OFDM_RATE_IDX;
12602 
12603 	hw_rate = ath12k_legacy_rates[rate_idx].hw_value;
12604 	bitrate = ath12k_legacy_rates[rate_idx].bitrate;
12605 
12606 	if (ath12k_mac_bitrate_is_cck(bitrate))
12607 		preamble = WMI_RATE_PREAMBLE_CCK;
12608 	else
12609 		preamble = WMI_RATE_PREAMBLE_OFDM;
12610 
12611 	*nss = 1;
12612 	*rate = ATH12K_HW_RATE_CODE(hw_rate, 0, preamble);
12613 
12614 	return 0;
12615 }
12616 
12617 static int
12618 ath12k_mac_set_fixed_rate_gi_ltf(struct ath12k_link_vif *arvif, u8 gi, u8 ltf,
12619 				 u32 param)
12620 {
12621 	struct ath12k *ar = arvif->ar;
12622 	int ret;
12623 
12624 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
12625 
12626 	/* 0.8 = 0, 1.6 = 2 and 3.2 = 3. */
12627 	if (gi && gi != 0xFF)
12628 		gi += 1;
12629 
12630 	ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
12631 					    WMI_VDEV_PARAM_SGI, gi);
12632 	if (ret) {
12633 		ath12k_warn(ar->ab, "failed to set GI:%d, error:%d\n",
12634 			    gi, ret);
12635 		return ret;
12636 	}
12637 
12638 	if (param == WMI_VDEV_PARAM_HE_LTF) {
12639 		/* HE values start from 1 */
12640 		if (ltf != 0xFF)
12641 			ltf += 1;
12642 	} else {
12643 		/* EHT values start from 5 */
12644 		if (ltf != 0xFF)
12645 			ltf += 4;
12646 	}
12647 
12648 	ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
12649 					    param, ltf);
12650 	if (ret) {
12651 		ath12k_warn(ar->ab, "failed to set LTF:%d, error:%d\n",
12652 			    ltf, ret);
12653 		return ret;
12654 	}
12655 	return 0;
12656 }
12657 
12658 static int
12659 ath12k_mac_set_auto_rate_gi_ltf(struct ath12k_link_vif *arvif, u16 gi, u8 ltf)
12660 {
12661 	struct ath12k *ar = arvif->ar;
12662 	int ret;
12663 	u32 ar_gi_ltf;
12664 
12665 	if (gi != 0xFF) {
12666 		switch (gi) {
12667 		case ATH12K_RATE_INFO_GI_0_8:
12668 			gi = WMI_AUTORATE_800NS_GI;
12669 			break;
12670 		case ATH12K_RATE_INFO_GI_1_6:
12671 			gi = WMI_AUTORATE_1600NS_GI;
12672 			break;
12673 		case ATH12K_RATE_INFO_GI_3_2:
12674 			gi = WMI_AUTORATE_3200NS_GI;
12675 			break;
12676 		default:
12677 			ath12k_warn(ar->ab, "Invalid GI\n");
12678 			return -EINVAL;
12679 		}
12680 	}
12681 
12682 	if (ltf != 0xFF) {
12683 		switch (ltf) {
12684 		case ATH12K_RATE_INFO_1XLTF:
12685 			ltf = WMI_AUTORATE_LTF_1X;
12686 			break;
12687 		case ATH12K_RATE_INFO_2XLTF:
12688 			ltf = WMI_AUTORATE_LTF_2X;
12689 			break;
12690 		case ATH12K_RATE_INFO_4XLTF:
12691 			ltf = WMI_AUTORATE_LTF_4X;
12692 			break;
12693 		default:
12694 			ath12k_warn(ar->ab, "Invalid LTF\n");
12695 			return -EINVAL;
12696 		}
12697 	}
12698 
12699 	ar_gi_ltf = gi | ltf;
12700 
12701 	ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
12702 					    WMI_VDEV_PARAM_AUTORATE_MISC_CFG,
12703 					    ar_gi_ltf);
12704 	if (ret) {
12705 		ath12k_warn(ar->ab,
12706 			    "failed to set autorate GI:%u, LTF:%u params, error:%d\n",
12707 			    gi, ltf, ret);
12708 		return ret;
12709 	}
12710 
12711 	return 0;
12712 }
12713 
12714 static u32 ath12k_mac_nlgi_to_wmigi(enum nl80211_txrate_gi gi)
12715 {
12716 	switch (gi) {
12717 	case NL80211_TXRATE_DEFAULT_GI:
12718 		return WMI_GI_400_NS;
12719 	case NL80211_TXRATE_FORCE_LGI:
12720 		return WMI_GI_800_NS;
12721 	default:
12722 		return WMI_GI_400_NS;
12723 	}
12724 }
12725 
12726 static int ath12k_mac_set_rate_params(struct ath12k_link_vif *arvif,
12727 				      u32 rate, u8 nss, u8 sgi, u8 ldpc,
12728 				      u8 he_gi, u8 he_ltf, bool he_fixed_rate,
12729 				      u8 eht_gi, u8 eht_ltf,
12730 				      bool eht_fixed_rate)
12731 {
12732 	struct ieee80211_bss_conf *link_conf;
12733 	struct ath12k *ar = arvif->ar;
12734 	bool he_support, eht_support, gi_ltf_set = false;
12735 	u32 vdev_param;
12736 	u32 param_value;
12737 	int ret;
12738 
12739 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
12740 
12741 	link_conf = ath12k_mac_get_link_bss_conf(arvif);
12742 	if (!link_conf)
12743 		return -EINVAL;
12744 
12745 	he_support = link_conf->he_support;
12746 	eht_support = link_conf->eht_support;
12747 
12748 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
12749 		   "mac set rate params vdev %i rate 0x%02x nss 0x%02x sgi 0x%02x ldpc 0x%02x\n",
12750 		   arvif->vdev_id, rate, nss, sgi, ldpc);
12751 
12752 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
12753 		   "he_gi 0x%02x he_ltf 0x%02x he_fixed_rate %d\n", he_gi,
12754 		   he_ltf, he_fixed_rate);
12755 
12756 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
12757 		   "eht_gi 0x%02x eht_ltf 0x%02x eht_fixed_rate %d\n",
12758 		   eht_gi, eht_ltf, eht_fixed_rate);
12759 
12760 	if (!he_support && !eht_support) {
12761 		vdev_param = WMI_VDEV_PARAM_FIXED_RATE;
12762 		ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
12763 						    vdev_param, rate);
12764 		if (ret) {
12765 			ath12k_warn(ar->ab, "failed to set fixed rate param 0x%02x: %d\n",
12766 				    rate, ret);
12767 			return ret;
12768 		}
12769 	}
12770 
12771 	vdev_param = WMI_VDEV_PARAM_NSS;
12772 
12773 	ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
12774 					    vdev_param, nss);
12775 	if (ret) {
12776 		ath12k_warn(ar->ab, "failed to set nss param %d: %d\n",
12777 			    nss, ret);
12778 		return ret;
12779 	}
12780 
12781 	ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
12782 					    WMI_VDEV_PARAM_LDPC, ldpc);
12783 	if (ret) {
12784 		ath12k_warn(ar->ab, "failed to set ldpc param %d: %d\n",
12785 			    ldpc, ret);
12786 		return ret;
12787 	}
12788 
12789 	if (eht_support) {
12790 		if (eht_fixed_rate)
12791 			ret = ath12k_mac_set_fixed_rate_gi_ltf(arvif, eht_gi, eht_ltf,
12792 							       WMI_VDEV_PARAM_EHT_LTF);
12793 		else
12794 			ret = ath12k_mac_set_auto_rate_gi_ltf(arvif, eht_gi, eht_ltf);
12795 
12796 		if (ret) {
12797 			ath12k_warn(ar->ab,
12798 				    "failed to set EHT LTF/GI params %d/%d: %d\n",
12799 				    eht_gi, eht_ltf, ret);
12800 			return ret;
12801 		}
12802 		gi_ltf_set = true;
12803 	}
12804 
12805 	if (he_support) {
12806 		if (he_fixed_rate)
12807 			ret = ath12k_mac_set_fixed_rate_gi_ltf(arvif, he_gi, he_ltf,
12808 							       WMI_VDEV_PARAM_HE_LTF);
12809 		else
12810 			ret = ath12k_mac_set_auto_rate_gi_ltf(arvif, he_gi, he_ltf);
12811 		if (ret)
12812 			return ret;
12813 		gi_ltf_set = true;
12814 	}
12815 
12816 	if (!gi_ltf_set) {
12817 		vdev_param = WMI_VDEV_PARAM_SGI;
12818 		param_value = ath12k_mac_nlgi_to_wmigi(sgi);
12819 		ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
12820 						    vdev_param, param_value);
12821 		if (ret) {
12822 			ath12k_warn(ar->ab, "failed to set sgi param %d: %d\n",
12823 				    sgi, ret);
12824 			return ret;
12825 		}
12826 	}
12827 
12828 	return 0;
12829 }
12830 
12831 static bool
12832 ath12k_mac_vht_mcs_range_present(struct ath12k *ar,
12833 				 enum nl80211_band band,
12834 				 const struct cfg80211_bitrate_mask *mask)
12835 {
12836 	int i;
12837 	u16 vht_mcs;
12838 
12839 	for (i = 0; i < NL80211_VHT_NSS_MAX; i++) {
12840 		vht_mcs = mask->control[band].vht_mcs[i];
12841 
12842 		switch (vht_mcs) {
12843 		case 0:
12844 		case BIT(8) - 1:
12845 		case BIT(9) - 1:
12846 		case BIT(10) - 1:
12847 			break;
12848 		default:
12849 			return false;
12850 		}
12851 	}
12852 
12853 	return true;
12854 }
12855 
12856 static bool
12857 ath12k_mac_he_mcs_range_present(struct ath12k *ar,
12858 				enum nl80211_band band,
12859 				const struct cfg80211_bitrate_mask *mask)
12860 {
12861 	int i;
12862 	u16 he_mcs;
12863 
12864 	for (i = 0; i < NL80211_HE_NSS_MAX; i++) {
12865 		he_mcs = mask->control[band].he_mcs[i];
12866 
12867 		switch (he_mcs) {
12868 		case 0:
12869 		case BIT(8) - 1:
12870 		case BIT(10) - 1:
12871 		case BIT(12) - 1:
12872 			break;
12873 		default:
12874 			return false;
12875 		}
12876 	}
12877 
12878 	return true;
12879 }
12880 
12881 static bool
12882 ath12k_mac_eht_mcs_range_present(struct ath12k *ar,
12883 				 enum nl80211_band band,
12884 				 const struct cfg80211_bitrate_mask *mask)
12885 {
12886 	u16 eht_mcs;
12887 	int i;
12888 
12889 	for (i = 0; i < NL80211_EHT_NSS_MAX; i++) {
12890 		eht_mcs = mask->control[band].eht_mcs[i];
12891 
12892 		switch (eht_mcs) {
12893 		case 0:
12894 		case BIT(8) - 1:
12895 		case BIT(10) - 1:
12896 		case BIT(12) - 1:
12897 		case BIT(14) - 1:
12898 			break;
12899 		case BIT(15) - 1:
12900 		case BIT(16) - 1:
12901 		case BIT(16) - BIT(14) - 1:
12902 			if (i != 0)
12903 				return false;
12904 			break;
12905 		default:
12906 			return false;
12907 		}
12908 	}
12909 
12910 	return true;
12911 }
12912 
12913 static void ath12k_mac_set_bitrate_mask_iter(void *data,
12914 					     struct ieee80211_sta *sta)
12915 {
12916 	struct ath12k_link_vif *arvif = data;
12917 	struct ath12k_sta *ahsta = ath12k_sta_to_ahsta(sta);
12918 	struct ath12k_link_sta *arsta;
12919 	struct ath12k *ar = arvif->ar;
12920 
12921 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
12922 
12923 	arsta = wiphy_dereference(ath12k_ar_to_hw(ar)->wiphy,
12924 				  ahsta->link[arvif->link_id]);
12925 	if (!arsta || arsta->arvif != arvif)
12926 		return;
12927 
12928 	spin_lock_bh(&ar->data_lock);
12929 	arsta->changed |= IEEE80211_RC_SUPP_RATES_CHANGED;
12930 	spin_unlock_bh(&ar->data_lock);
12931 
12932 	wiphy_work_queue(ath12k_ar_to_hw(ar)->wiphy, &arsta->update_wk);
12933 }
12934 
12935 static void ath12k_mac_disable_peer_fixed_rate(void *data,
12936 					       struct ieee80211_sta *sta)
12937 {
12938 	struct ath12k_link_vif *arvif = data;
12939 	struct ath12k_sta *ahsta = ath12k_sta_to_ahsta(sta);
12940 	struct ath12k_link_sta *arsta;
12941 	struct ath12k *ar = arvif->ar;
12942 	int ret;
12943 
12944 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
12945 
12946 	arsta = wiphy_dereference(ath12k_ar_to_hw(ar)->wiphy,
12947 				  ahsta->link[arvif->link_id]);
12948 
12949 	if (!arsta || arsta->arvif != arvif)
12950 		return;
12951 
12952 	ret = ath12k_wmi_set_peer_param(ar, arsta->addr,
12953 					arvif->vdev_id,
12954 					WMI_PEER_PARAM_FIXED_RATE,
12955 					WMI_FIXED_RATE_NONE);
12956 	if (ret)
12957 		ath12k_warn(ar->ab,
12958 			    "failed to disable peer fixed rate for STA %pM ret %d\n",
12959 			    arsta->addr, ret);
12960 }
12961 
12962 static bool
12963 ath12k_mac_validate_fixed_rate_settings(struct ath12k *ar, enum nl80211_band band,
12964 					const struct cfg80211_bitrate_mask *mask,
12965 					unsigned int link_id)
12966 {
12967 	bool eht_fixed_rate = false, he_fixed_rate = false, vht_fixed_rate = false;
12968 	const u16 *vht_mcs_mask, *he_mcs_mask, *eht_mcs_mask;
12969 	struct ieee80211_link_sta *link_sta;
12970 	struct ath12k_dp_link_peer *peer, *tmp;
12971 	u8 vht_nss, he_nss, eht_nss;
12972 	int ret = true;
12973 	struct ath12k_base *ab = ar->ab;
12974 	struct ath12k_dp *dp = ath12k_ab_to_dp(ab);
12975 
12976 	vht_mcs_mask = mask->control[band].vht_mcs;
12977 	he_mcs_mask = mask->control[band].he_mcs;
12978 	eht_mcs_mask = mask->control[band].eht_mcs;
12979 
12980 	if (ath12k_mac_bitrate_mask_num_vht_rates(ar, band, mask) == 1)
12981 		vht_fixed_rate = true;
12982 
12983 	if (ath12k_mac_bitrate_mask_num_he_rates(ar, band, mask) == 1)
12984 		he_fixed_rate = true;
12985 
12986 	if (ath12k_mac_bitrate_mask_num_eht_rates(ar, band, mask) == 1)
12987 		eht_fixed_rate = true;
12988 
12989 	if (!vht_fixed_rate && !he_fixed_rate && !eht_fixed_rate)
12990 		return true;
12991 
12992 	vht_nss = ath12k_mac_max_vht_nss(vht_mcs_mask);
12993 	he_nss =  ath12k_mac_max_he_nss(he_mcs_mask);
12994 	eht_nss = ath12k_mac_max_eht_nss(eht_mcs_mask);
12995 
12996 	rcu_read_lock();
12997 	spin_lock_bh(&dp->dp_lock);
12998 	list_for_each_entry_safe(peer, tmp, &dp->peers, list) {
12999 		if (peer->sta) {
13000 			link_sta = rcu_dereference(peer->sta->link[link_id]);
13001 			if (!link_sta) {
13002 				ret = false;
13003 				goto exit;
13004 			}
13005 
13006 			if (vht_fixed_rate && (!link_sta->vht_cap.vht_supported ||
13007 					       link_sta->rx_nss < vht_nss)) {
13008 				ret = false;
13009 				goto exit;
13010 			}
13011 			if (he_fixed_rate && (!link_sta->he_cap.has_he ||
13012 					      link_sta->rx_nss < he_nss)) {
13013 				ret = false;
13014 				goto exit;
13015 			}
13016 			if (eht_fixed_rate && (!link_sta->eht_cap.has_eht ||
13017 					       link_sta->rx_nss < eht_nss)) {
13018 				ret = false;
13019 				goto exit;
13020 			}
13021 		}
13022 	}
13023 exit:
13024 	spin_unlock_bh(&dp->dp_lock);
13025 	rcu_read_unlock();
13026 	return ret;
13027 }
13028 
13029 int
13030 ath12k_mac_op_set_bitrate_mask(struct ieee80211_hw *hw,
13031 			       struct ieee80211_vif *vif,
13032 			       const struct cfg80211_bitrate_mask *mask)
13033 {
13034 	struct ath12k_vif *ahvif = ath12k_vif_to_ahvif(vif);
13035 	struct ath12k_link_vif *arvif;
13036 	struct cfg80211_chan_def def;
13037 	struct ath12k *ar;
13038 	enum nl80211_band band;
13039 	const u8 *ht_mcs_mask;
13040 	const u16 *vht_mcs_mask;
13041 	const u16 *he_mcs_mask;
13042 	const u16 *eht_mcs_mask;
13043 	u8 he_ltf = 0;
13044 	u8 he_gi = 0;
13045 	u8 eht_ltf = 0, eht_gi = 0;
13046 	u32 rate;
13047 	u8 nss, mac_nss;
13048 	u8 sgi;
13049 	u8 ldpc;
13050 	int single_nss;
13051 	int ret;
13052 	int num_rates;
13053 	bool he_fixed_rate = false;
13054 	bool eht_fixed_rate = false;
13055 
13056 	lockdep_assert_wiphy(hw->wiphy);
13057 
13058 	arvif = &ahvif->deflink;
13059 
13060 	ar = arvif->ar;
13061 	if (ath12k_mac_vif_link_chan(vif, arvif->link_id, &def)) {
13062 		ret = -EPERM;
13063 		goto out;
13064 	}
13065 
13066 	band = def.chan->band;
13067 	ht_mcs_mask = mask->control[band].ht_mcs;
13068 	vht_mcs_mask = mask->control[band].vht_mcs;
13069 	he_mcs_mask = mask->control[band].he_mcs;
13070 	eht_mcs_mask = mask->control[band].eht_mcs;
13071 	ldpc = !!(ar->ht_cap_info & WMI_HT_CAP_LDPC);
13072 
13073 	sgi = mask->control[band].gi;
13074 	if (sgi == NL80211_TXRATE_FORCE_SGI) {
13075 		ret = -EINVAL;
13076 		goto out;
13077 	}
13078 
13079 	he_gi = mask->control[band].he_gi;
13080 	he_ltf = mask->control[band].he_ltf;
13081 
13082 	eht_gi = mask->control[band].eht_gi;
13083 	eht_ltf = mask->control[band].eht_ltf;
13084 
13085 	/* mac80211 doesn't support sending a fixed HT/VHT MCS alone, rather it
13086 	 * requires passing at least one of used basic rates along with them.
13087 	 * Fixed rate setting across different preambles(legacy, HT, VHT) is
13088 	 * not supported by the FW. Hence use of FIXED_RATE vdev param is not
13089 	 * suitable for setting single HT/VHT rates.
13090 	 * But, there could be a single basic rate passed from userspace which
13091 	 * can be done through the FIXED_RATE param.
13092 	 */
13093 	if (ath12k_mac_has_single_legacy_rate(ar, band, mask)) {
13094 		ret = ath12k_mac_get_single_legacy_rate(ar, band, mask, &rate,
13095 							&nss);
13096 		if (ret) {
13097 			ath12k_warn(ar->ab, "failed to get single legacy rate for vdev %i: %d\n",
13098 				    arvif->vdev_id, ret);
13099 			goto out;
13100 		}
13101 
13102 		ieee80211_iterate_stations_mtx(hw,
13103 					       ath12k_mac_disable_peer_fixed_rate,
13104 					       arvif);
13105 	} else if (ath12k_mac_bitrate_mask_get_single_nss(ar, vif, band, mask,
13106 							  &single_nss)) {
13107 		rate = WMI_FIXED_RATE_NONE;
13108 		nss = single_nss;
13109 		arvif->bitrate_mask = *mask;
13110 
13111 		ieee80211_iterate_stations_atomic(hw,
13112 						  ath12k_mac_set_bitrate_mask_iter,
13113 						  arvif);
13114 	} else {
13115 		rate = WMI_FIXED_RATE_NONE;
13116 
13117 		if (!ath12k_mac_validate_fixed_rate_settings(ar, band,
13118 							     mask, arvif->link_id))
13119 			ath12k_warn(ar->ab,
13120 				    "failed to update fixed rate settings due to mcs/nss incompatibility\n");
13121 
13122 		mac_nss = max(max3(ath12k_mac_max_ht_nss(ht_mcs_mask),
13123 				   ath12k_mac_max_vht_nss(vht_mcs_mask),
13124 				   ath12k_mac_max_he_nss(he_mcs_mask)),
13125 			       ath12k_mac_max_eht_nss(eht_mcs_mask));
13126 		nss = min_t(u32, ar->num_tx_chains, mac_nss);
13127 
13128 		/* If multiple rates across different preambles are given
13129 		 * we can reconfigure this info with all peers using PEER_ASSOC
13130 		 * command with the below exception cases.
13131 		 * - Single VHT Rate : peer_assoc command accommodates only MCS
13132 		 * range values i.e 0-7, 0-8, 0-9 for VHT. Though mac80211
13133 		 * mandates passing basic rates along with HT/VHT rates, FW
13134 		 * doesn't allow switching from VHT to Legacy. Hence instead of
13135 		 * setting legacy and VHT rates using RATEMASK_CMD vdev cmd,
13136 		 * we could set this VHT rate as peer fixed rate param, which
13137 		 * will override FIXED rate and FW rate control algorithm.
13138 		 * If single VHT rate is passed along with HT rates, we select
13139 		 * the VHT rate as fixed rate for vht peers.
13140 		 * - Multiple VHT Rates : When Multiple VHT rates are given,this
13141 		 * can be set using RATEMASK CMD which uses FW rate-ctl alg.
13142 		 * TODO: Setting multiple VHT MCS and replacing peer_assoc with
13143 		 * RATEMASK_CMDID can cover all use cases of setting rates
13144 		 * across multiple preambles and rates within same type.
13145 		 * But requires more validation of the command at this point.
13146 		 */
13147 
13148 		num_rates = ath12k_mac_bitrate_mask_num_vht_rates(ar, band,
13149 								  mask);
13150 
13151 		if (!ath12k_mac_vht_mcs_range_present(ar, band, mask) &&
13152 		    num_rates > 1) {
13153 			/* TODO: Handle multiple VHT MCS values setting using
13154 			 * RATEMASK CMD
13155 			 */
13156 			ath12k_warn(ar->ab,
13157 				    "Setting more than one MCS Value in bitrate mask not supported\n");
13158 			ret = -EINVAL;
13159 			goto out;
13160 		}
13161 
13162 		num_rates = ath12k_mac_bitrate_mask_num_he_rates(ar, band, mask);
13163 		if (num_rates == 1)
13164 			he_fixed_rate = true;
13165 
13166 		if (!ath12k_mac_he_mcs_range_present(ar, band, mask) &&
13167 		    num_rates > 1) {
13168 			ath12k_warn(ar->ab,
13169 				    "Setting more than one HE MCS Value in bitrate mask not supported\n");
13170 			ret = -EINVAL;
13171 			goto out;
13172 		}
13173 
13174 		num_rates = ath12k_mac_bitrate_mask_num_eht_rates(ar, band,
13175 								  mask);
13176 		if (num_rates == 1)
13177 			eht_fixed_rate = true;
13178 
13179 		if (!ath12k_mac_eht_mcs_range_present(ar, band, mask) &&
13180 		    num_rates > 1) {
13181 			ath12k_warn(ar->ab,
13182 				    "Setting more than one EHT MCS Value in bitrate mask not supported\n");
13183 			ret = -EINVAL;
13184 			goto out;
13185 		}
13186 
13187 		ieee80211_iterate_stations_mtx(hw,
13188 					       ath12k_mac_disable_peer_fixed_rate,
13189 					       arvif);
13190 
13191 		arvif->bitrate_mask = *mask;
13192 		ieee80211_iterate_stations_mtx(hw,
13193 					       ath12k_mac_set_bitrate_mask_iter,
13194 					       arvif);
13195 	}
13196 
13197 	ret = ath12k_mac_set_rate_params(arvif, rate, nss, sgi, ldpc, he_gi,
13198 					 he_ltf, he_fixed_rate, eht_gi, eht_ltf,
13199 					 eht_fixed_rate);
13200 	if (ret) {
13201 		ath12k_warn(ar->ab, "failed to set rate params on vdev %i: %d\n",
13202 			    arvif->vdev_id, ret);
13203 	}
13204 
13205 out:
13206 	return ret;
13207 }
13208 EXPORT_SYMBOL(ath12k_mac_op_set_bitrate_mask);
13209 
13210 void
13211 ath12k_mac_op_reconfig_complete(struct ieee80211_hw *hw,
13212 				enum ieee80211_reconfig_type reconfig_type)
13213 {
13214 	struct ath12k_hw *ah = ath12k_hw_to_ah(hw);
13215 	struct ath12k *ar;
13216 	struct ath12k_base *ab;
13217 	struct ath12k_vif *ahvif;
13218 	struct ath12k_link_vif *arvif;
13219 	int recovery_count, i;
13220 
13221 	lockdep_assert_wiphy(hw->wiphy);
13222 
13223 	if (reconfig_type != IEEE80211_RECONFIG_TYPE_RESTART)
13224 		return;
13225 
13226 	guard(mutex)(&ah->hw_mutex);
13227 
13228 	if (ah->state != ATH12K_HW_STATE_RESTARTED)
13229 		return;
13230 
13231 	ah->state = ATH12K_HW_STATE_ON;
13232 	ieee80211_wake_queues(hw);
13233 
13234 	for_each_ar(ah, ar, i) {
13235 		ab = ar->ab;
13236 
13237 		ath12k_warn(ar->ab, "pdev %d successfully recovered\n",
13238 			    ar->pdev->pdev_id);
13239 
13240 		if (ar->ab->hw_params->current_cc_support &&
13241 		    ar->alpha2[0] != 0 && ar->alpha2[1] != 0) {
13242 			struct wmi_set_current_country_arg arg = {};
13243 
13244 			memcpy(&arg.alpha2, ar->alpha2, 2);
13245 			reinit_completion(&ar->regd_update_completed);
13246 			ath12k_wmi_send_set_current_country_cmd(ar, &arg);
13247 		}
13248 
13249 		if (ab->is_reset) {
13250 			recovery_count = atomic_inc_return(&ab->recovery_count);
13251 
13252 			ath12k_dbg(ab, ATH12K_DBG_BOOT, "recovery count %d\n",
13253 				   recovery_count);
13254 
13255 			/* When there are multiple radios in an SOC,
13256 			 * the recovery has to be done for each radio
13257 			 */
13258 			if (recovery_count == ab->num_radios) {
13259 				atomic_dec(&ab->reset_count);
13260 				complete(&ab->reset_complete);
13261 				ab->is_reset = false;
13262 				atomic_set(&ab->fail_cont_count, 0);
13263 				ath12k_dbg(ab, ATH12K_DBG_BOOT, "reset success\n");
13264 			}
13265 		}
13266 
13267 		list_for_each_entry(arvif, &ar->arvifs, list) {
13268 			ahvif = arvif->ahvif;
13269 			ath12k_dbg(ab, ATH12K_DBG_BOOT,
13270 				   "reconfig cipher %d up %d vdev type %d\n",
13271 				   ahvif->dp_vif.key_cipher,
13272 				   arvif->is_up,
13273 				   ahvif->vdev_type);
13274 
13275 			/* After trigger disconnect, then upper layer will
13276 			 * trigger connect again, then the PN number of
13277 			 * upper layer will be reset to keep up with AP
13278 			 * side, hence PN number mismatch will not happen.
13279 			 */
13280 			if (arvif->is_up &&
13281 			    ahvif->vdev_type == WMI_VDEV_TYPE_STA &&
13282 			    ahvif->vdev_subtype == WMI_VDEV_SUBTYPE_NONE) {
13283 				ieee80211_hw_restart_disconnect(ahvif->vif);
13284 
13285 				ath12k_dbg(ab, ATH12K_DBG_BOOT,
13286 					   "restart disconnect\n");
13287 			}
13288 		}
13289 	}
13290 }
13291 EXPORT_SYMBOL(ath12k_mac_op_reconfig_complete);
13292 
13293 static void
13294 ath12k_mac_update_bss_chan_survey(struct ath12k *ar,
13295 				  struct ieee80211_channel *channel)
13296 {
13297 	int ret;
13298 	enum wmi_bss_chan_info_req_type type = WMI_BSS_SURVEY_REQ_TYPE_READ;
13299 
13300 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
13301 
13302 	if (!test_bit(WMI_TLV_SERVICE_BSS_CHANNEL_INFO_64, ar->ab->wmi_ab.svc_map) ||
13303 	    ar->rx_channel != channel)
13304 		return;
13305 
13306 	if (ar->scan.state != ATH12K_SCAN_IDLE) {
13307 		ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
13308 			   "ignoring bss chan info req while scanning..\n");
13309 		return;
13310 	}
13311 
13312 	reinit_completion(&ar->bss_survey_done);
13313 
13314 	ret = ath12k_wmi_pdev_bss_chan_info_request(ar, type);
13315 	if (ret) {
13316 		ath12k_warn(ar->ab, "failed to send pdev bss chan info request\n");
13317 		return;
13318 	}
13319 
13320 	ret = wait_for_completion_timeout(&ar->bss_survey_done, 3 * HZ);
13321 	if (ret == 0)
13322 		ath12k_warn(ar->ab, "bss channel survey timed out\n");
13323 }
13324 
13325 int ath12k_mac_op_get_survey(struct ieee80211_hw *hw, int idx,
13326 			     struct survey_info *survey)
13327 {
13328 	struct ath12k *ar;
13329 	struct ieee80211_supported_band *sband;
13330 	struct survey_info *ar_survey;
13331 
13332 	lockdep_assert_wiphy(hw->wiphy);
13333 
13334 	if (idx >= ATH12K_NUM_CHANS)
13335 		return -ENOENT;
13336 
13337 	sband = hw->wiphy->bands[NL80211_BAND_2GHZ];
13338 	if (sband && idx >= sband->n_channels) {
13339 		idx -= sband->n_channels;
13340 		sband = NULL;
13341 	}
13342 
13343 	if (!sband)
13344 		sband = hw->wiphy->bands[NL80211_BAND_5GHZ];
13345 	if (sband && idx >= sband->n_channels) {
13346 		idx -= sband->n_channels;
13347 		sband = NULL;
13348 	}
13349 
13350 	if (!sband)
13351 		sband = hw->wiphy->bands[NL80211_BAND_6GHZ];
13352 
13353 	if (!sband || idx >= sband->n_channels)
13354 		return -ENOENT;
13355 
13356 	ar = ath12k_mac_get_ar_by_chan(hw, &sband->channels[idx]);
13357 	if (!ar) {
13358 		if (sband->channels[idx].flags & IEEE80211_CHAN_DISABLED) {
13359 			memset(survey, 0, sizeof(*survey));
13360 			return 0;
13361 		}
13362 		return -ENOENT;
13363 	}
13364 
13365 	ar_survey = &ar->survey[idx];
13366 
13367 	ath12k_mac_update_bss_chan_survey(ar, &sband->channels[idx]);
13368 
13369 	spin_lock_bh(&ar->data_lock);
13370 	memcpy(survey, ar_survey, sizeof(*survey));
13371 	spin_unlock_bh(&ar->data_lock);
13372 
13373 	survey->channel = &sband->channels[idx];
13374 
13375 	if (ar->rx_channel == survey->channel)
13376 		survey->filled |= SURVEY_INFO_IN_USE;
13377 
13378 	return 0;
13379 }
13380 EXPORT_SYMBOL(ath12k_mac_op_get_survey);
13381 
13382 static void ath12k_mac_put_chain_rssi(struct station_info *sinfo,
13383 				      struct ath12k_link_sta *arsta)
13384 {
13385 	s8 rssi;
13386 	int i;
13387 
13388 	for (i = 0; i < ARRAY_SIZE(sinfo->chain_signal); i++) {
13389 		sinfo->chains &= ~BIT(i);
13390 		rssi = arsta->chain_signal[i];
13391 
13392 		if (rssi != ATH12K_DEFAULT_NOISE_FLOOR &&
13393 		    rssi != ATH12K_INVALID_RSSI_FULL &&
13394 		    rssi != ATH12K_INVALID_RSSI_EMPTY &&
13395 		    rssi != 0) {
13396 			sinfo->chain_signal[i] = rssi;
13397 			sinfo->chains |= BIT(i);
13398 			sinfo->filled |= BIT_ULL(NL80211_STA_INFO_CHAIN_SIGNAL);
13399 		}
13400 	}
13401 }
13402 
13403 void ath12k_mac_op_sta_statistics(struct ieee80211_hw *hw,
13404 				  struct ieee80211_vif *vif,
13405 				  struct ieee80211_sta *sta,
13406 				  struct station_info *sinfo)
13407 {
13408 	struct ath12k_sta *ahsta = ath12k_sta_to_ahsta(sta);
13409 	struct ath12k_dp_link_peer_rate_info rate_info = {};
13410 	struct ath12k_fw_stats_req_params params = {};
13411 	struct ath12k_dp_link_peer *peer;
13412 	struct ath12k_link_sta *arsta;
13413 	s8 signal, noise_floor;
13414 	struct ath12k_dp *dp;
13415 	struct ath12k *ar;
13416 	bool db2dbm;
13417 
13418 	lockdep_assert_wiphy(hw->wiphy);
13419 
13420 	arsta = &ahsta->deflink;
13421 	ar = ath12k_get_ar_by_vif(hw, vif, arsta->link_id);
13422 	if (!ar)
13423 		return;
13424 
13425 	dp = ath12k_ab_to_dp(ar->ab);
13426 	ath12k_dp_link_peer_get_sta_rate_info_stats(dp, arsta->addr, &rate_info);
13427 
13428 	db2dbm = test_bit(WMI_TLV_SERVICE_HW_DB2DBM_CONVERSION_SUPPORT,
13429 			  ar->ab->wmi_ab.svc_map);
13430 
13431 	sinfo->rx_duration = rate_info.rx_duration;
13432 	sinfo->filled |= BIT_ULL(NL80211_STA_INFO_RX_DURATION);
13433 
13434 	sinfo->tx_duration = rate_info.tx_duration;
13435 	sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_DURATION);
13436 
13437 	if (rate_info.txrate.legacy || rate_info.txrate.nss) {
13438 		if (rate_info.txrate.legacy) {
13439 			sinfo->txrate.legacy = rate_info.txrate.legacy;
13440 		} else {
13441 			sinfo->txrate.mcs = rate_info.txrate.mcs;
13442 			sinfo->txrate.nss = rate_info.txrate.nss;
13443 			sinfo->txrate.bw = rate_info.txrate.bw;
13444 			sinfo->txrate.he_gi = rate_info.txrate.he_gi;
13445 			sinfo->txrate.he_dcm = rate_info.txrate.he_dcm;
13446 			sinfo->txrate.he_ru_alloc = rate_info.txrate.he_ru_alloc;
13447 			sinfo->txrate.eht_gi = rate_info.txrate.eht_gi;
13448 			sinfo->txrate.eht_ru_alloc = rate_info.txrate.eht_ru_alloc;
13449 		}
13450 		sinfo->txrate.flags = rate_info.txrate.flags;
13451 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_BITRATE);
13452 	}
13453 
13454 	/* TODO: Use real NF instead of default one. */
13455 	signal = rate_info.rssi_comb;
13456 
13457 	params.pdev_id = ath12k_mac_get_target_pdev_id(ar);
13458 	params.vdev_id = 0;
13459 	params.stats_id = WMI_REQUEST_VDEV_STAT;
13460 
13461 	if (!signal &&
13462 	    ahsta->ahvif->vdev_type == WMI_VDEV_TYPE_STA &&
13463 	    !(ath12k_mac_get_fw_stats(ar, &params))) {
13464 		signal = arsta->rssi_beacon;
13465 		ath12k_fw_stats_reset(ar);
13466 	}
13467 
13468 	params.stats_id = WMI_REQUEST_RSSI_PER_CHAIN_STAT;
13469 	if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_CHAIN_SIGNAL)) &&
13470 	    ahsta->ahvif->vdev_type == WMI_VDEV_TYPE_STA &&
13471 	    !(ath12k_mac_get_fw_stats(ar, &params))) {
13472 		ath12k_mac_put_chain_rssi(sinfo, arsta);
13473 		ath12k_fw_stats_reset(ar);
13474 	}
13475 
13476 	spin_lock_bh(&ar->data_lock);
13477 	noise_floor = ath12k_pdev_get_noise_floor(ar);
13478 	spin_unlock_bh(&ar->data_lock);
13479 
13480 	if (signal) {
13481 		sinfo->signal = db2dbm ? signal : signal + noise_floor;
13482 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_SIGNAL);
13483 	}
13484 
13485 	sinfo->signal_avg = rate_info.signal_avg;
13486 
13487 	if (!db2dbm)
13488 		sinfo->signal_avg += noise_floor;
13489 
13490 	sinfo->filled |= BIT_ULL(NL80211_STA_INFO_SIGNAL_AVG);
13491 
13492 	spin_lock_bh(&dp->dp_lock);
13493 	peer = ath12k_dp_link_peer_find_by_addr(dp, arsta->addr);
13494 	if (!peer) {
13495 		spin_unlock_bh(&dp->dp_lock);
13496 		return;
13497 	}
13498 
13499 	sinfo->tx_retries = peer->tx_retry_count;
13500 	sinfo->tx_failed = peer->tx_retry_failed;
13501 	sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_RETRIES);
13502 	sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_FAILED);
13503 
13504 	spin_unlock_bh(&dp->dp_lock);
13505 }
13506 EXPORT_SYMBOL(ath12k_mac_op_sta_statistics);
13507 
13508 void ath12k_mac_op_link_sta_statistics(struct ieee80211_hw *hw,
13509 				       struct ieee80211_vif *vif,
13510 				       struct ieee80211_link_sta *link_sta,
13511 				       struct link_station_info *link_sinfo)
13512 {
13513 	struct ath12k_sta *ahsta = ath12k_sta_to_ahsta(link_sta->sta);
13514 	struct ath12k_fw_stats_req_params params = {};
13515 	struct ath12k_dp_link_peer *peer;
13516 	struct ath12k_link_sta *arsta;
13517 	struct ath12k *ar;
13518 	s8 signal;
13519 	bool db2dbm;
13520 
13521 	lockdep_assert_wiphy(hw->wiphy);
13522 
13523 	arsta = wiphy_dereference(hw->wiphy, ahsta->link[link_sta->link_id]);
13524 
13525 	if (!arsta)
13526 		return;
13527 
13528 	ar = ath12k_get_ar_by_vif(hw, vif, arsta->link_id);
13529 	if (!ar)
13530 		return;
13531 
13532 	db2dbm = test_bit(WMI_TLV_SERVICE_HW_DB2DBM_CONVERSION_SUPPORT,
13533 			  ar->ab->wmi_ab.svc_map);
13534 
13535 	spin_lock_bh(&ar->ab->dp->dp_lock);
13536 	peer = ath12k_dp_link_peer_find_by_addr(ar->ab->dp, arsta->addr);
13537 	if (!peer) {
13538 		spin_unlock_bh(&ar->ab->dp->dp_lock);
13539 		return;
13540 	}
13541 
13542 	link_sinfo->rx_duration = peer->rx_duration;
13543 	link_sinfo->filled |= BIT_ULL(NL80211_STA_INFO_RX_DURATION);
13544 
13545 	link_sinfo->tx_duration = peer->tx_duration;
13546 	link_sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_DURATION);
13547 
13548 	if (peer->txrate.legacy || peer->txrate.nss) {
13549 		if (peer->txrate.legacy) {
13550 			link_sinfo->txrate.legacy = peer->txrate.legacy;
13551 		} else {
13552 			link_sinfo->txrate.mcs = peer->txrate.mcs;
13553 			link_sinfo->txrate.nss = peer->txrate.nss;
13554 			link_sinfo->txrate.bw = peer->txrate.bw;
13555 			link_sinfo->txrate.he_gi = peer->txrate.he_gi;
13556 			link_sinfo->txrate.he_dcm = peer->txrate.he_dcm;
13557 			link_sinfo->txrate.he_ru_alloc =
13558 				peer->txrate.he_ru_alloc;
13559 			link_sinfo->txrate.eht_gi = peer->txrate.eht_gi;
13560 			link_sinfo->txrate.eht_ru_alloc =
13561 				peer->txrate.eht_ru_alloc;
13562 		}
13563 		link_sinfo->txrate.flags = peer->txrate.flags;
13564 		link_sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_BITRATE);
13565 	}
13566 
13567 	link_sinfo->signal_avg = ewma_avg_rssi_read(&peer->avg_rssi);
13568 
13569 	if (!db2dbm)
13570 		link_sinfo->signal_avg += ATH12K_DEFAULT_NOISE_FLOOR;
13571 
13572 	link_sinfo->filled |= BIT_ULL(NL80211_STA_INFO_SIGNAL_AVG);
13573 
13574 	link_sinfo->tx_retries = peer->tx_retry_count;
13575 	link_sinfo->tx_failed = peer->tx_retry_failed;
13576 	link_sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_RETRIES);
13577 	link_sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_FAILED);
13578 
13579 	/* TODO: Use real NF instead of default one. */
13580 	signal = peer->rssi_comb;
13581 
13582 	spin_unlock_bh(&ar->ab->dp->dp_lock);
13583 
13584 	if (!signal && ahsta->ahvif->vdev_type == WMI_VDEV_TYPE_STA) {
13585 		params.pdev_id = ath12k_mac_get_target_pdev_id(ar);
13586 		params.vdev_id = 0;
13587 		params.stats_id = WMI_REQUEST_VDEV_STAT;
13588 
13589 		if (!ath12k_mac_get_fw_stats(ar, &params)) {
13590 			signal = arsta->rssi_beacon;
13591 			ath12k_fw_stats_reset(ar);
13592 		}
13593 	}
13594 
13595 	if (signal) {
13596 		link_sinfo->signal =
13597 			db2dbm ? signal : signal + ATH12K_DEFAULT_NOISE_FLOOR;
13598 		link_sinfo->filled |= BIT_ULL(NL80211_STA_INFO_SIGNAL);
13599 	}
13600 }
13601 EXPORT_SYMBOL(ath12k_mac_op_link_sta_statistics);
13602 
13603 int ath12k_mac_op_cancel_remain_on_channel(struct ieee80211_hw *hw,
13604 					   struct ieee80211_vif *vif)
13605 {
13606 	struct ath12k_hw *ah = ath12k_hw_to_ah(hw);
13607 	struct ath12k *ar;
13608 
13609 	ar = ath12k_ah_to_ar(ah, 0);
13610 
13611 	lockdep_assert_wiphy(hw->wiphy);
13612 
13613 	spin_lock_bh(&ar->data_lock);
13614 	ar->scan.roc_notify = false;
13615 	spin_unlock_bh(&ar->data_lock);
13616 
13617 	ath12k_scan_abort(ar);
13618 
13619 	cancel_delayed_work_sync(&ar->scan.timeout);
13620 	wiphy_work_flush(hw->wiphy, &ar->scan.vdev_clean_wk);
13621 
13622 	return 0;
13623 }
13624 EXPORT_SYMBOL(ath12k_mac_op_cancel_remain_on_channel);
13625 
13626 int ath12k_mac_op_remain_on_channel(struct ieee80211_hw *hw,
13627 				    struct ieee80211_vif *vif,
13628 				    struct ieee80211_channel *chan,
13629 				    int duration,
13630 				    enum ieee80211_roc_type type)
13631 {
13632 	struct ath12k_vif *ahvif = ath12k_vif_to_ahvif(vif);
13633 	struct ath12k_hw *ah = ath12k_hw_to_ah(hw);
13634 	struct ath12k_link_vif *arvif;
13635 	struct ath12k *ar;
13636 	u32 scan_time_msec;
13637 	bool create = true;
13638 	u8 link_id;
13639 	int ret;
13640 
13641 	lockdep_assert_wiphy(hw->wiphy);
13642 
13643 	ar = ath12k_mac_select_scan_device(hw, vif, chan->center_freq);
13644 	if (!ar)
13645 		return -EINVAL;
13646 
13647 	/* check if any of the links of ML VIF is already started on
13648 	 * radio(ar) corresponding to given scan frequency and use it,
13649 	 * if not use deflink(link 0) for scan purpose.
13650 	 */
13651 
13652 	link_id = ath12k_mac_find_link_id_by_ar(ahvif, ar);
13653 	arvif = ath12k_mac_assign_link_vif(ah, vif, link_id);
13654 	/* If the vif is already assigned to a specific vdev of an ar,
13655 	 * check whether its already started, vdev which is started
13656 	 * are not allowed to switch to a new radio.
13657 	 * If the vdev is not started, but was earlier created on a
13658 	 * different ar, delete that vdev and create a new one. We don't
13659 	 * delete at the scan stop as an optimization to avoid redundant
13660 	 * delete-create vdev's for the same ar, in case the request is
13661 	 * always on the same band for the vif
13662 	 */
13663 	if (arvif->is_created) {
13664 		if (WARN_ON(!arvif->ar))
13665 			return -EINVAL;
13666 
13667 		if (ar != arvif->ar && arvif->is_started)
13668 			return -EBUSY;
13669 
13670 		if (ar != arvif->ar) {
13671 			ath12k_mac_remove_link_interface(hw, arvif);
13672 			ath12k_mac_unassign_link_vif(arvif);
13673 		} else {
13674 			create = false;
13675 		}
13676 	}
13677 
13678 	if (create) {
13679 		arvif = ath12k_mac_assign_link_vif(ah, vif, link_id);
13680 
13681 		ret = ath12k_mac_vdev_create(ar, arvif);
13682 		if (ret) {
13683 			ath12k_warn(ar->ab, "unable to create scan vdev for roc: %d\n",
13684 				    ret);
13685 			ath12k_mac_unassign_link_vif(arvif);
13686 			return ret;
13687 		}
13688 	}
13689 
13690 	spin_lock_bh(&ar->data_lock);
13691 
13692 	switch (ar->scan.state) {
13693 	case ATH12K_SCAN_IDLE:
13694 		reinit_completion(&ar->scan.started);
13695 		reinit_completion(&ar->scan.completed);
13696 		reinit_completion(&ar->scan.on_channel);
13697 		ar->scan.state = ATH12K_SCAN_STARTING;
13698 		ar->scan.is_roc = true;
13699 		ar->scan.arvif = arvif;
13700 		ar->scan.roc_freq = chan->center_freq;
13701 		ar->scan.roc_notify = true;
13702 		ret = 0;
13703 		break;
13704 	case ATH12K_SCAN_STARTING:
13705 	case ATH12K_SCAN_RUNNING:
13706 	case ATH12K_SCAN_ABORTING:
13707 		ret = -EBUSY;
13708 		break;
13709 	}
13710 
13711 	spin_unlock_bh(&ar->data_lock);
13712 
13713 	if (ret)
13714 		return ret;
13715 
13716 	scan_time_msec = hw->wiphy->max_remain_on_channel_duration * 2;
13717 
13718 	struct ath12k_wmi_scan_req_arg *arg __free(kfree) =
13719 					kzalloc_obj(*arg);
13720 	if (!arg)
13721 		return -ENOMEM;
13722 
13723 	ath12k_wmi_start_scan_init(ar, arg);
13724 	arg->num_chan = 1;
13725 
13726 	u32 *chan_list __free(kfree) = kcalloc(arg->num_chan, sizeof(*chan_list),
13727 					       GFP_KERNEL);
13728 	if (!chan_list)
13729 		return -ENOMEM;
13730 
13731 	arg->chan_list = chan_list;
13732 	arg->vdev_id = arvif->vdev_id;
13733 	arg->scan_id = ATH12K_SCAN_ID;
13734 	arg->chan_list[0] = chan->center_freq;
13735 	arg->dwell_time_active = scan_time_msec;
13736 	arg->dwell_time_passive = scan_time_msec;
13737 	arg->max_scan_time = scan_time_msec;
13738 	arg->scan_f_passive = 1;
13739 	arg->burst_duration = duration;
13740 
13741 	ret = ath12k_start_scan(ar, arg);
13742 	if (ret) {
13743 		ath12k_warn(ar->ab, "failed to start roc scan: %d\n", ret);
13744 
13745 		spin_lock_bh(&ar->data_lock);
13746 		ar->scan.state = ATH12K_SCAN_IDLE;
13747 		spin_unlock_bh(&ar->data_lock);
13748 		return ret;
13749 	}
13750 
13751 	ret = wait_for_completion_timeout(&ar->scan.on_channel, 3 * HZ);
13752 	if (ret == 0) {
13753 		ath12k_warn(ar->ab, "failed to switch to channel for roc scan\n");
13754 		ret = ath12k_scan_stop(ar);
13755 		if (ret)
13756 			ath12k_warn(ar->ab, "failed to stop scan: %d\n", ret);
13757 		return -ETIMEDOUT;
13758 	}
13759 
13760 	ieee80211_queue_delayed_work(hw, &ar->scan.timeout,
13761 				     msecs_to_jiffies(duration));
13762 
13763 	return 0;
13764 }
13765 EXPORT_SYMBOL(ath12k_mac_op_remain_on_channel);
13766 
13767 void ath12k_mac_op_set_rekey_data(struct ieee80211_hw *hw,
13768 				  struct ieee80211_vif *vif,
13769 				  struct cfg80211_gtk_rekey_data *data)
13770 {
13771 	struct ath12k_vif *ahvif = ath12k_vif_to_ahvif(vif);
13772 	struct ath12k_rekey_data *rekey_data;
13773 	struct ath12k_hw *ah = ath12k_hw_to_ah(hw);
13774 	struct ath12k *ar = ath12k_ah_to_ar(ah, 0);
13775 	struct ath12k_link_vif *arvif;
13776 
13777 	lockdep_assert_wiphy(hw->wiphy);
13778 
13779 	arvif = &ahvif->deflink;
13780 	rekey_data = &arvif->rekey_data;
13781 
13782 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac set rekey data vdev %d\n",
13783 		   arvif->vdev_id);
13784 
13785 	memcpy(rekey_data->kck, data->kck, NL80211_KCK_LEN);
13786 	memcpy(rekey_data->kek, data->kek, NL80211_KEK_LEN);
13787 
13788 	/* The supplicant works on big-endian, the firmware expects it on
13789 	 * little endian.
13790 	 */
13791 	rekey_data->replay_ctr = get_unaligned_be64(data->replay_ctr);
13792 
13793 	arvif->rekey_data.enable_offload = true;
13794 
13795 	ath12k_dbg_dump(ar->ab, ATH12K_DBG_MAC, "kck", NULL,
13796 			rekey_data->kck, NL80211_KCK_LEN);
13797 	ath12k_dbg_dump(ar->ab, ATH12K_DBG_MAC, "kek", NULL,
13798 			rekey_data->kck, NL80211_KEK_LEN);
13799 	ath12k_dbg_dump(ar->ab, ATH12K_DBG_MAC, "replay ctr", NULL,
13800 			&rekey_data->replay_ctr, sizeof(rekey_data->replay_ctr));
13801 }
13802 EXPORT_SYMBOL(ath12k_mac_op_set_rekey_data);
13803 
13804 void ath12k_mac_update_freq_range(struct ath12k *ar,
13805 				  u32 freq_low, u32 freq_high)
13806 {
13807 	if (!(freq_low && freq_high))
13808 		return;
13809 
13810 	if (ar->freq_range.start_freq || ar->freq_range.end_freq) {
13811 		ar->freq_range.start_freq = min(ar->freq_range.start_freq,
13812 						MHZ_TO_KHZ(freq_low));
13813 		ar->freq_range.end_freq = max(ar->freq_range.end_freq,
13814 					      MHZ_TO_KHZ(freq_high));
13815 	} else {
13816 		ar->freq_range.start_freq = MHZ_TO_KHZ(freq_low);
13817 		ar->freq_range.end_freq = MHZ_TO_KHZ(freq_high);
13818 	}
13819 
13820 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
13821 		   "mac pdev %u freq limit updated. New range %u->%u MHz\n",
13822 		   ar->pdev->pdev_id, KHZ_TO_MHZ(ar->freq_range.start_freq),
13823 		   KHZ_TO_MHZ(ar->freq_range.end_freq));
13824 }
13825 
13826 static void ath12k_mac_update_ch_list(struct ath12k *ar,
13827 				      struct ieee80211_supported_band *band,
13828 				      u32 freq_low, u32 freq_high)
13829 {
13830 	int i;
13831 
13832 	if (!(freq_low && freq_high))
13833 		return;
13834 
13835 	for (i = 0; i < band->n_channels; i++) {
13836 		if (band->channels[i].center_freq < freq_low ||
13837 		    band->channels[i].center_freq > freq_high)
13838 			band->channels[i].flags |= IEEE80211_CHAN_DISABLED;
13839 	}
13840 }
13841 
13842 static u32 ath12k_get_phy_id(struct ath12k *ar, u32 band)
13843 {
13844 	struct ath12k_pdev *pdev = ar->pdev;
13845 	struct ath12k_pdev_cap *pdev_cap = &pdev->cap;
13846 
13847 	if (band == WMI_HOST_WLAN_2GHZ_CAP)
13848 		return pdev_cap->band[NL80211_BAND_2GHZ].phy_id;
13849 
13850 	if (band == WMI_HOST_WLAN_5GHZ_CAP)
13851 		return pdev_cap->band[NL80211_BAND_5GHZ].phy_id;
13852 
13853 	ath12k_warn(ar->ab, "unsupported phy cap:%d\n", band);
13854 
13855 	return 0;
13856 }
13857 
13858 static int ath12k_mac_update_band(struct ath12k *ar,
13859 				  struct ieee80211_supported_band *orig_band,
13860 				  struct ieee80211_supported_band *new_band)
13861 {
13862 	int i;
13863 
13864 	if (!orig_band || !new_band)
13865 		return -EINVAL;
13866 
13867 	if (orig_band->band != new_band->band)
13868 		return -EINVAL;
13869 
13870 	for (i = 0; i < new_band->n_channels; i++) {
13871 		if (new_band->channels[i].flags & IEEE80211_CHAN_DISABLED)
13872 			continue;
13873 		/* An enabled channel in new_band should not be already enabled
13874 		 * in the orig_band
13875 		 */
13876 		if (WARN_ON(!(orig_band->channels[i].flags &
13877 			      IEEE80211_CHAN_DISABLED)))
13878 			return -EINVAL;
13879 		orig_band->channels[i].flags &= ~IEEE80211_CHAN_DISABLED;
13880 	}
13881 	return 0;
13882 }
13883 
13884 static int ath12k_mac_setup_channels_rates(struct ath12k *ar,
13885 					   u32 supported_bands,
13886 					   struct ieee80211_supported_band *bands[])
13887 {
13888 	struct ieee80211_supported_band *band;
13889 	struct ath12k_wmi_hal_reg_capabilities_ext_arg *reg_cap;
13890 	struct ath12k_base *ab = ar->ab;
13891 	u32 phy_id, freq_low, freq_high;
13892 	struct ath12k_hw *ah = ar->ah;
13893 	void *channels;
13894 	int ret;
13895 
13896 	BUILD_BUG_ON((ARRAY_SIZE(ath12k_2ghz_channels) +
13897 		      ARRAY_SIZE(ath12k_5ghz_channels) +
13898 		      ARRAY_SIZE(ath12k_6ghz_channels)) !=
13899 		     ATH12K_NUM_CHANS);
13900 
13901 	reg_cap = &ab->hal_reg_cap[ar->pdev_idx];
13902 
13903 	if (supported_bands & WMI_HOST_WLAN_2GHZ_CAP) {
13904 		channels = kmemdup(ath12k_2ghz_channels,
13905 				   sizeof(ath12k_2ghz_channels),
13906 				   GFP_KERNEL);
13907 		if (!channels)
13908 			return -ENOMEM;
13909 
13910 		band = &ar->mac.sbands[NL80211_BAND_2GHZ];
13911 		band->band = NL80211_BAND_2GHZ;
13912 		band->n_channels = ARRAY_SIZE(ath12k_2ghz_channels);
13913 		band->channels = channels;
13914 		band->n_bitrates = ath12k_g_rates_size;
13915 		band->bitrates = ath12k_g_rates;
13916 
13917 		if (ab->hw_params->single_pdev_only) {
13918 			phy_id = ath12k_get_phy_id(ar, WMI_HOST_WLAN_2GHZ_CAP);
13919 			reg_cap = &ab->hal_reg_cap[phy_id];
13920 		}
13921 
13922 		freq_low = max(reg_cap->low_2ghz_chan,
13923 			       ab->reg_freq_2ghz.start_freq);
13924 		freq_high = min(reg_cap->high_2ghz_chan,
13925 				ab->reg_freq_2ghz.end_freq);
13926 
13927 		ath12k_mac_update_ch_list(ar, band,
13928 					  reg_cap->low_2ghz_chan,
13929 					  reg_cap->high_2ghz_chan);
13930 
13931 		ath12k_mac_update_freq_range(ar, freq_low, freq_high);
13932 
13933 		if (!bands[NL80211_BAND_2GHZ]) {
13934 			bands[NL80211_BAND_2GHZ] = band;
13935 		} else {
13936 			/* Split mac in same band under same wiphy */
13937 			ret = ath12k_mac_update_band(ar, bands[NL80211_BAND_2GHZ], band);
13938 			if (ret) {
13939 				kfree(channels);
13940 				band->channels = NULL;
13941 				return ret;
13942 			}
13943 			ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac pdev %u identified as 2 GHz split mac with start freq %d end freq %d",
13944 				   ar->pdev->pdev_id,
13945 				   KHZ_TO_MHZ(ar->freq_range.start_freq),
13946 				   KHZ_TO_MHZ(ar->freq_range.end_freq));
13947 		}
13948 	}
13949 
13950 	if (supported_bands & WMI_HOST_WLAN_5GHZ_CAP) {
13951 		if (reg_cap->high_5ghz_chan >= ATH12K_MIN_6GHZ_FREQ) {
13952 			channels = kmemdup(ath12k_6ghz_channels,
13953 					   sizeof(ath12k_6ghz_channels), GFP_KERNEL);
13954 			if (!channels) {
13955 				kfree(ar->mac.sbands[NL80211_BAND_2GHZ].channels);
13956 				return -ENOMEM;
13957 			}
13958 
13959 			ar->supports_6ghz = true;
13960 			band = &ar->mac.sbands[NL80211_BAND_6GHZ];
13961 			band->band = NL80211_BAND_6GHZ;
13962 			band->n_channels = ARRAY_SIZE(ath12k_6ghz_channels);
13963 			band->channels = channels;
13964 			band->n_bitrates = ath12k_a_rates_size;
13965 			band->bitrates = ath12k_a_rates;
13966 
13967 			freq_low = max(reg_cap->low_5ghz_chan,
13968 				       ab->reg_freq_6ghz.start_freq);
13969 			freq_high = min(reg_cap->high_5ghz_chan,
13970 					ab->reg_freq_6ghz.end_freq);
13971 
13972 			ath12k_mac_update_ch_list(ar, band,
13973 						  reg_cap->low_5ghz_chan,
13974 						  reg_cap->high_5ghz_chan);
13975 
13976 			ath12k_mac_update_freq_range(ar, freq_low, freq_high);
13977 			ah->use_6ghz_regd = true;
13978 
13979 			if (!bands[NL80211_BAND_6GHZ]) {
13980 				bands[NL80211_BAND_6GHZ] = band;
13981 			} else {
13982 				/* Split mac in same band under same wiphy */
13983 				ret = ath12k_mac_update_band(ar,
13984 							     bands[NL80211_BAND_6GHZ],
13985 							     band);
13986 				if (ret) {
13987 					kfree(ar->mac.sbands[NL80211_BAND_2GHZ].channels);
13988 					ar->mac.sbands[NL80211_BAND_2GHZ].channels = NULL;
13989 					kfree(channels);
13990 					band->channels = NULL;
13991 					return ret;
13992 				}
13993 				ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac pdev %u identified as 6 GHz split mac with start freq %d end freq %d",
13994 					   ar->pdev->pdev_id,
13995 					   KHZ_TO_MHZ(ar->freq_range.start_freq),
13996 					   KHZ_TO_MHZ(ar->freq_range.end_freq));
13997 			}
13998 		}
13999 
14000 		if (reg_cap->low_5ghz_chan < ATH12K_MIN_6GHZ_FREQ) {
14001 			channels = kmemdup(ath12k_5ghz_channels,
14002 					   sizeof(ath12k_5ghz_channels),
14003 					   GFP_KERNEL);
14004 			if (!channels) {
14005 				kfree(ar->mac.sbands[NL80211_BAND_2GHZ].channels);
14006 				kfree(ar->mac.sbands[NL80211_BAND_6GHZ].channels);
14007 				return -ENOMEM;
14008 			}
14009 
14010 			band = &ar->mac.sbands[NL80211_BAND_5GHZ];
14011 			band->band = NL80211_BAND_5GHZ;
14012 			band->n_channels = ARRAY_SIZE(ath12k_5ghz_channels);
14013 			band->channels = channels;
14014 			band->n_bitrates = ath12k_a_rates_size;
14015 			band->bitrates = ath12k_a_rates;
14016 
14017 			if (ab->hw_params->single_pdev_only) {
14018 				phy_id = ath12k_get_phy_id(ar, WMI_HOST_WLAN_5GHZ_CAP);
14019 				reg_cap = &ab->hal_reg_cap[phy_id];
14020 			}
14021 
14022 			freq_low = max(reg_cap->low_5ghz_chan,
14023 				       ab->reg_freq_5ghz.start_freq);
14024 			freq_high = min(reg_cap->high_5ghz_chan,
14025 					ab->reg_freq_5ghz.end_freq);
14026 
14027 			ath12k_mac_update_ch_list(ar, band,
14028 						  reg_cap->low_5ghz_chan,
14029 						  reg_cap->high_5ghz_chan);
14030 
14031 			ath12k_mac_update_freq_range(ar, freq_low, freq_high);
14032 
14033 			if (!bands[NL80211_BAND_5GHZ]) {
14034 				bands[NL80211_BAND_5GHZ] = band;
14035 			} else {
14036 				/* Split mac in same band under same wiphy */
14037 				ret = ath12k_mac_update_band(ar,
14038 							     bands[NL80211_BAND_5GHZ],
14039 							     band);
14040 				if (ret) {
14041 					kfree(ar->mac.sbands[NL80211_BAND_2GHZ].channels);
14042 					ar->mac.sbands[NL80211_BAND_2GHZ].channels = NULL;
14043 					kfree(ar->mac.sbands[NL80211_BAND_6GHZ].channels);
14044 					ar->mac.sbands[NL80211_BAND_2GHZ].channels = NULL;
14045 					kfree(channels);
14046 					band->channels = NULL;
14047 					return ret;
14048 				}
14049 				ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac pdev %u identified as 5 GHz split mac with start freq %d end freq %d",
14050 					   ar->pdev->pdev_id,
14051 					   KHZ_TO_MHZ(ar->freq_range.start_freq),
14052 					   KHZ_TO_MHZ(ar->freq_range.end_freq));
14053 			}
14054 		}
14055 	}
14056 
14057 	return 0;
14058 }
14059 
14060 static u16 ath12k_mac_get_ifmodes(struct ath12k_hw *ah)
14061 {
14062 	struct ath12k *ar;
14063 	int i;
14064 	u16 interface_modes = U16_MAX;
14065 
14066 	for_each_ar(ah, ar, i)
14067 		interface_modes &= ar->ab->hw_params->interface_modes;
14068 
14069 	return interface_modes == U16_MAX ? 0 : interface_modes;
14070 }
14071 
14072 static bool ath12k_mac_is_iface_mode_enable(struct ath12k_hw *ah,
14073 					    enum nl80211_iftype type)
14074 {
14075 	struct ath12k *ar;
14076 	int i;
14077 	u16 interface_modes, mode = 0;
14078 	bool is_enable = false;
14079 
14080 	if (type == NL80211_IFTYPE_MESH_POINT) {
14081 		if (IS_ENABLED(CONFIG_MAC80211_MESH))
14082 			mode = BIT(type);
14083 	} else {
14084 		mode = BIT(type);
14085 	}
14086 
14087 	for_each_ar(ah, ar, i) {
14088 		interface_modes = ar->ab->hw_params->interface_modes;
14089 		if (interface_modes & mode) {
14090 			is_enable = true;
14091 			break;
14092 		}
14093 	}
14094 
14095 	return is_enable;
14096 }
14097 
14098 static int
14099 ath12k_mac_setup_radio_iface_comb(struct ath12k *ar,
14100 				  struct ieee80211_iface_combination *comb)
14101 {
14102 	u16 interface_modes = ar->ab->hw_params->interface_modes;
14103 	struct ieee80211_iface_limit *limits;
14104 	int n_limits, max_interfaces;
14105 	bool ap, mesh, p2p;
14106 
14107 	ap = interface_modes & BIT(NL80211_IFTYPE_AP);
14108 	p2p = interface_modes & BIT(NL80211_IFTYPE_P2P_DEVICE);
14109 
14110 	mesh = IS_ENABLED(CONFIG_MAC80211_MESH) &&
14111 	       (interface_modes & BIT(NL80211_IFTYPE_MESH_POINT));
14112 
14113 	if ((ap || mesh) && !p2p) {
14114 		n_limits = 2;
14115 		max_interfaces = 16;
14116 	} else if (p2p) {
14117 		n_limits = 3;
14118 		if (ap || mesh)
14119 			max_interfaces = 16;
14120 		else
14121 			max_interfaces = 3;
14122 	} else {
14123 		n_limits = 1;
14124 		max_interfaces = 1;
14125 	}
14126 
14127 	limits = kzalloc_objs(*limits, n_limits);
14128 	if (!limits)
14129 		return -ENOMEM;
14130 
14131 	limits[0].max = 1;
14132 	limits[0].types |= BIT(NL80211_IFTYPE_STATION);
14133 
14134 	if (ap || mesh || p2p)
14135 		limits[1].max = max_interfaces;
14136 
14137 	if (ap)
14138 		limits[1].types |= BIT(NL80211_IFTYPE_AP);
14139 
14140 	if (mesh)
14141 		limits[1].types |= BIT(NL80211_IFTYPE_MESH_POINT);
14142 
14143 	if (p2p) {
14144 		limits[1].types |= BIT(NL80211_IFTYPE_P2P_CLIENT) |
14145 					BIT(NL80211_IFTYPE_P2P_GO);
14146 		limits[2].max = 1;
14147 		limits[2].types |= BIT(NL80211_IFTYPE_P2P_DEVICE);
14148 	}
14149 
14150 	comb[0].limits = limits;
14151 	comb[0].n_limits = n_limits;
14152 	comb[0].max_interfaces = max_interfaces;
14153 	comb[0].beacon_int_infra_match = true;
14154 	comb[0].beacon_int_min_gcd = 100;
14155 
14156 	comb[0].num_different_channels = 1;
14157 	comb[0].radar_detect_widths = BIT(NL80211_CHAN_WIDTH_20_NOHT) |
14158 				      BIT(NL80211_CHAN_WIDTH_20) |
14159 				      BIT(NL80211_CHAN_WIDTH_40) |
14160 				      BIT(NL80211_CHAN_WIDTH_80) |
14161 				      BIT(NL80211_CHAN_WIDTH_160);
14162 
14163 	return 0;
14164 }
14165 
14166 static int
14167 ath12k_mac_setup_global_iface_comb(struct ath12k_hw *ah,
14168 				   struct wiphy_radio *radio,
14169 				   u8 n_radio,
14170 				   struct ieee80211_iface_combination *comb)
14171 {
14172 	const struct ieee80211_iface_combination *iter_comb;
14173 	struct ieee80211_iface_limit *limits;
14174 	int i, j, n_limits;
14175 	bool ap, mesh, p2p;
14176 
14177 	if (!n_radio)
14178 		return 0;
14179 
14180 	ap = ath12k_mac_is_iface_mode_enable(ah, NL80211_IFTYPE_AP);
14181 	p2p = ath12k_mac_is_iface_mode_enable(ah, NL80211_IFTYPE_P2P_DEVICE);
14182 	mesh = ath12k_mac_is_iface_mode_enable(ah, NL80211_IFTYPE_MESH_POINT);
14183 
14184 	if ((ap || mesh) && !p2p)
14185 		n_limits = 2;
14186 	else if (p2p)
14187 		n_limits = 3;
14188 	else
14189 		n_limits = 1;
14190 
14191 	limits = kzalloc_objs(*limits, n_limits);
14192 	if (!limits)
14193 		return -ENOMEM;
14194 
14195 	for (i = 0; i < n_radio; i++) {
14196 		iter_comb = radio[i].iface_combinations;
14197 		for (j = 0; j < iter_comb->n_limits && j < n_limits; j++) {
14198 			limits[j].types |= iter_comb->limits[j].types;
14199 			limits[j].max += iter_comb->limits[j].max;
14200 		}
14201 
14202 		comb->max_interfaces += iter_comb->max_interfaces;
14203 		comb->num_different_channels += iter_comb->num_different_channels;
14204 		comb->radar_detect_widths |= iter_comb->radar_detect_widths;
14205 	}
14206 
14207 	comb->limits = limits;
14208 	comb->n_limits = n_limits;
14209 	comb->beacon_int_infra_match = true;
14210 	comb->beacon_int_min_gcd = 100;
14211 
14212 	return 0;
14213 }
14214 
14215 static
14216 void ath12k_mac_cleanup_iface_comb(const struct ieee80211_iface_combination *iface_comb)
14217 {
14218 	kfree(iface_comb[0].limits);
14219 	kfree(iface_comb);
14220 }
14221 
14222 static void ath12k_mac_cleanup_iface_combinations(struct ath12k_hw *ah)
14223 {
14224 	struct wiphy *wiphy = ah->hw->wiphy;
14225 	const struct wiphy_radio *radio;
14226 	int i;
14227 
14228 	if (wiphy->n_radio > 0) {
14229 		radio = wiphy->radio;
14230 		for (i = 0; i < wiphy->n_radio; i++)
14231 			ath12k_mac_cleanup_iface_comb(radio[i].iface_combinations);
14232 
14233 		kfree(wiphy->radio);
14234 	}
14235 
14236 	ath12k_mac_cleanup_iface_comb(wiphy->iface_combinations);
14237 }
14238 
14239 static int ath12k_mac_setup_iface_combinations(struct ath12k_hw *ah)
14240 {
14241 	struct ieee80211_iface_combination *combinations, *comb;
14242 	struct wiphy *wiphy = ah->hw->wiphy;
14243 	struct wiphy_radio *radio;
14244 	int n_combinations = 1;
14245 	struct ath12k *ar;
14246 	int i, ret;
14247 
14248 	if (ah->num_radio == 1) {
14249 		ar = &ah->radio[0];
14250 
14251 		if (ar->ab->hw_params->single_pdev_only)
14252 			n_combinations = 2;
14253 
14254 		combinations = kzalloc_objs(*combinations, n_combinations);
14255 		if (!combinations)
14256 			return -ENOMEM;
14257 
14258 		ret = ath12k_mac_setup_radio_iface_comb(ar, combinations);
14259 		if (ret) {
14260 			ath12k_hw_warn(ah, "failed to setup radio interface combinations for one radio: %d",
14261 				       ret);
14262 			goto err_free_combinations;
14263 		}
14264 
14265 		if (ar->ab->hw_params->single_pdev_only) {
14266 			comb = combinations + 1;
14267 			memcpy(comb, combinations, sizeof(*comb));
14268 			comb->num_different_channels = 2;
14269 			comb->radar_detect_widths = 0;
14270 		}
14271 
14272 		goto out;
14273 	}
14274 
14275 	combinations = kzalloc_objs(*combinations, n_combinations);
14276 	if (!combinations)
14277 		return -ENOMEM;
14278 
14279 	/* there are multiple radios */
14280 
14281 	radio = kzalloc_objs(*radio, ah->num_radio);
14282 	if (!radio) {
14283 		ret = -ENOMEM;
14284 		goto err_free_combinations;
14285 	}
14286 
14287 	for_each_ar(ah, ar, i) {
14288 		comb = kzalloc_obj(*comb);
14289 		if (!comb) {
14290 			ret = -ENOMEM;
14291 			goto err_free_radios;
14292 		}
14293 
14294 		ret = ath12k_mac_setup_radio_iface_comb(ar, comb);
14295 		if (ret) {
14296 			ath12k_hw_warn(ah, "failed to setup radio interface combinations for radio %d: %d",
14297 				       i, ret);
14298 			kfree(comb);
14299 			goto err_free_radios;
14300 		}
14301 
14302 		radio[i].freq_range = &ar->freq_range;
14303 		radio[i].n_freq_range = 1;
14304 
14305 		radio[i].iface_combinations = comb;
14306 		radio[i].n_iface_combinations = 1;
14307 	}
14308 
14309 	ret = ath12k_mac_setup_global_iface_comb(ah, radio, ah->num_radio, combinations);
14310 	if (ret) {
14311 		ath12k_hw_warn(ah, "failed to setup global interface combinations: %d",
14312 			       ret);
14313 		goto err_free_all_radios;
14314 	}
14315 
14316 	wiphy->radio = radio;
14317 	wiphy->n_radio = ah->num_radio;
14318 
14319 out:
14320 	wiphy->iface_combinations = combinations;
14321 	wiphy->n_iface_combinations = n_combinations;
14322 
14323 	return 0;
14324 
14325 err_free_all_radios:
14326 	i = ah->num_radio;
14327 
14328 err_free_radios:
14329 	while (i--)
14330 		ath12k_mac_cleanup_iface_comb(radio[i].iface_combinations);
14331 
14332 	kfree(radio);
14333 
14334 err_free_combinations:
14335 	kfree(combinations);
14336 
14337 	return ret;
14338 }
14339 
14340 static const u8 ath12k_if_types_ext_capa[] = {
14341 	[0] = WLAN_EXT_CAPA1_EXT_CHANNEL_SWITCHING,
14342 	[2] = WLAN_EXT_CAPA3_MULTI_BSSID_SUPPORT,
14343 	[7] = WLAN_EXT_CAPA8_OPMODE_NOTIF,
14344 };
14345 
14346 static const u8 ath12k_if_types_ext_capa_sta[] = {
14347 	[0] = WLAN_EXT_CAPA1_EXT_CHANNEL_SWITCHING,
14348 	[2] = WLAN_EXT_CAPA3_MULTI_BSSID_SUPPORT,
14349 	[7] = WLAN_EXT_CAPA8_OPMODE_NOTIF,
14350 	[9] = WLAN_EXT_CAPA10_TWT_REQUESTER_SUPPORT,
14351 };
14352 
14353 static const u8 ath12k_if_types_ext_capa_ap[] = {
14354 	[0] = WLAN_EXT_CAPA1_EXT_CHANNEL_SWITCHING,
14355 	[2] = WLAN_EXT_CAPA3_MULTI_BSSID_SUPPORT,
14356 	[7] = WLAN_EXT_CAPA8_OPMODE_NOTIF,
14357 	[9] = WLAN_EXT_CAPA10_TWT_RESPONDER_SUPPORT,
14358 	[10] = WLAN_EXT_CAPA11_EMA_SUPPORT,
14359 };
14360 
14361 static struct wiphy_iftype_ext_capab ath12k_iftypes_ext_capa[] = {
14362 	{
14363 		.extended_capabilities = ath12k_if_types_ext_capa,
14364 		.extended_capabilities_mask = ath12k_if_types_ext_capa,
14365 		.extended_capabilities_len = sizeof(ath12k_if_types_ext_capa),
14366 	}, {
14367 		.iftype = NL80211_IFTYPE_STATION,
14368 		.extended_capabilities = ath12k_if_types_ext_capa_sta,
14369 		.extended_capabilities_mask = ath12k_if_types_ext_capa_sta,
14370 		.extended_capabilities_len =
14371 				sizeof(ath12k_if_types_ext_capa_sta),
14372 	}, {
14373 		.iftype = NL80211_IFTYPE_AP,
14374 		.extended_capabilities = ath12k_if_types_ext_capa_ap,
14375 		.extended_capabilities_mask = ath12k_if_types_ext_capa_ap,
14376 		.extended_capabilities_len =
14377 				sizeof(ath12k_if_types_ext_capa_ap),
14378 		.eml_capabilities = 0,
14379 		.mld_capa_and_ops = 0,
14380 	},
14381 };
14382 
14383 static void ath12k_mac_cleanup_unregister(struct ath12k *ar)
14384 {
14385 	idr_for_each(&ar->txmgmt_idr, ath12k_mac_tx_mgmt_pending_free, ar);
14386 	idr_destroy(&ar->txmgmt_idr);
14387 
14388 	kfree(ar->mac.sbands[NL80211_BAND_2GHZ].channels);
14389 	kfree(ar->mac.sbands[NL80211_BAND_5GHZ].channels);
14390 	kfree(ar->mac.sbands[NL80211_BAND_6GHZ].channels);
14391 }
14392 
14393 static void ath12k_mac_hw_unregister(struct ath12k_hw *ah)
14394 {
14395 	struct ieee80211_hw *hw = ah->hw;
14396 	struct ath12k *ar;
14397 	int i;
14398 
14399 	for_each_ar(ah, ar, i) {
14400 		cancel_work_sync(&ar->regd_channel_update_work);
14401 		cancel_work_sync(&ar->regd_update_work);
14402 		ath12k_debugfs_unregister(ar);
14403 		ath12k_fw_stats_reset(ar);
14404 	}
14405 
14406 	ieee80211_unregister_hw(hw);
14407 
14408 	for_each_ar(ah, ar, i)
14409 		ath12k_mac_cleanup_unregister(ar);
14410 
14411 	ath12k_mac_cleanup_iface_combinations(ah);
14412 
14413 	SET_IEEE80211_DEV(hw, NULL);
14414 }
14415 
14416 static int ath12k_mac_setup_register(struct ath12k *ar,
14417 				     u32 *ht_cap,
14418 				     struct ieee80211_supported_band *bands[])
14419 {
14420 	struct ath12k_pdev_cap *cap = &ar->pdev->cap;
14421 	int ret;
14422 
14423 	init_waitqueue_head(&ar->txmgmt_empty_waitq);
14424 	idr_init(&ar->txmgmt_idr);
14425 	spin_lock_init(&ar->txmgmt_idr_lock);
14426 
14427 	ath12k_pdev_caps_update(ar);
14428 
14429 	ret = ath12k_mac_setup_channels_rates(ar,
14430 					      cap->supported_bands,
14431 					      bands);
14432 	if (ret)
14433 		return ret;
14434 
14435 	ath12k_mac_setup_ht_vht_cap(ar, cap, ht_cap);
14436 	ath12k_mac_setup_sband_iftype_data(ar, cap);
14437 
14438 	ar->max_num_stations = ath12k_core_get_max_station_per_radio(ar->ab);
14439 	ar->max_num_peers = ath12k_core_get_max_peers_per_radio(ar->ab);
14440 
14441 	ar->rssi_info.min_nf_dbm = ATH12K_DEFAULT_NOISE_FLOOR;
14442 	ar->rssi_info.temp_offset = 0;
14443 	ar->rssi_info.noise_floor = ar->rssi_info.min_nf_dbm + ar->rssi_info.temp_offset;
14444 
14445 	return 0;
14446 }
14447 
14448 static int ath12k_mac_hw_register(struct ath12k_hw *ah)
14449 {
14450 	struct ieee80211_hw *hw = ah->hw;
14451 	struct wiphy *wiphy = hw->wiphy;
14452 	struct ath12k *ar = ath12k_ah_to_ar(ah, 0);
14453 	struct ath12k_base *ab = ar->ab;
14454 	struct ath12k_pdev *pdev;
14455 	struct ath12k_pdev_cap *cap;
14456 	static const u32 cipher_suites[] = {
14457 		WLAN_CIPHER_SUITE_TKIP,
14458 		WLAN_CIPHER_SUITE_CCMP,
14459 		WLAN_CIPHER_SUITE_AES_CMAC,
14460 		WLAN_CIPHER_SUITE_BIP_CMAC_256,
14461 		WLAN_CIPHER_SUITE_BIP_GMAC_128,
14462 		WLAN_CIPHER_SUITE_BIP_GMAC_256,
14463 		WLAN_CIPHER_SUITE_GCMP,
14464 		WLAN_CIPHER_SUITE_GCMP_256,
14465 		WLAN_CIPHER_SUITE_CCMP_256,
14466 	};
14467 	int ret, i, j;
14468 	u32 ht_cap = U32_MAX, antennas_rx = 0, antennas_tx = 0;
14469 	bool is_6ghz = false, is_raw_mode = false, is_monitor_disable = false;
14470 	u8 *mac_addr = NULL;
14471 	u8 mbssid_max_interfaces = 0;
14472 
14473 	wiphy->max_ap_assoc_sta = 0;
14474 
14475 	for_each_ar(ah, ar, i) {
14476 		u32 ht_cap_info = 0;
14477 
14478 		pdev = ar->pdev;
14479 		if (ar->ab->pdevs_macaddr_valid) {
14480 			ether_addr_copy(ar->mac_addr, pdev->mac_addr);
14481 		} else {
14482 			ether_addr_copy(ar->mac_addr, ar->ab->mac_addr);
14483 			ar->mac_addr[4] += ar->pdev_idx;
14484 		}
14485 
14486 		ret = ath12k_mac_setup_register(ar, &ht_cap_info, hw->wiphy->bands);
14487 		if (ret)
14488 			goto err_cleanup_unregister;
14489 
14490 		/* 6 GHz does not support HT Cap, hence do not consider it */
14491 		if (!ar->supports_6ghz)
14492 			ht_cap &= ht_cap_info;
14493 
14494 		wiphy->max_ap_assoc_sta += ar->max_num_stations;
14495 
14496 		/* Advertise the max antenna support of all radios, driver can handle
14497 		 * per pdev specific antenna setting based on pdev cap when antenna
14498 		 * changes are made
14499 		 */
14500 		cap = &pdev->cap;
14501 
14502 		antennas_rx = max_t(u32, antennas_rx, cap->rx_chain_mask);
14503 		antennas_tx = max_t(u32, antennas_tx, cap->tx_chain_mask);
14504 
14505 		if (ar->supports_6ghz)
14506 			is_6ghz = true;
14507 
14508 		if (test_bit(ATH12K_FLAG_RAW_MODE, &ar->ab->dev_flags))
14509 			is_raw_mode = true;
14510 
14511 		if (!ar->ab->hw_params->supports_monitor)
14512 			is_monitor_disable = true;
14513 
14514 		if (i == 0)
14515 			mac_addr = ar->mac_addr;
14516 		else
14517 			mac_addr = ab->mac_addr;
14518 
14519 		mbssid_max_interfaces += TARGET_NUM_VDEVS(ar->ab);
14520 	}
14521 
14522 	wiphy->available_antennas_rx = antennas_rx;
14523 	wiphy->available_antennas_tx = antennas_tx;
14524 
14525 	SET_IEEE80211_PERM_ADDR(hw, mac_addr);
14526 	SET_IEEE80211_DEV(hw, ab->dev);
14527 
14528 	ret = ath12k_mac_setup_iface_combinations(ah);
14529 	if (ret) {
14530 		ath12k_err(ab, "failed to setup interface combinations: %d\n", ret);
14531 		goto err_complete_cleanup_unregister;
14532 	}
14533 
14534 	wiphy->interface_modes = ath12k_mac_get_ifmodes(ah);
14535 
14536 	if (ah->num_radio == 1 &&
14537 	    wiphy->bands[NL80211_BAND_2GHZ] &&
14538 	    wiphy->bands[NL80211_BAND_5GHZ] &&
14539 	    wiphy->bands[NL80211_BAND_6GHZ])
14540 		ieee80211_hw_set(hw, SINGLE_SCAN_ON_ALL_BANDS);
14541 
14542 	ieee80211_hw_set(hw, SIGNAL_DBM);
14543 	ieee80211_hw_set(hw, SUPPORTS_PS);
14544 	ieee80211_hw_set(hw, SUPPORTS_DYNAMIC_PS);
14545 	ieee80211_hw_set(hw, MFP_CAPABLE);
14546 	ieee80211_hw_set(hw, REPORTS_TX_ACK_STATUS);
14547 	ieee80211_hw_set(hw, HAS_RATE_CONTROL);
14548 	ieee80211_hw_set(hw, AP_LINK_PS);
14549 	ieee80211_hw_set(hw, SPECTRUM_MGMT);
14550 	ieee80211_hw_set(hw, CONNECTION_MONITOR);
14551 	ieee80211_hw_set(hw, SUPPORTS_PER_STA_GTK);
14552 	ieee80211_hw_set(hw, CHANCTX_STA_CSA);
14553 	ieee80211_hw_set(hw, QUEUE_CONTROL);
14554 	ieee80211_hw_set(hw, SUPPORTS_TX_FRAG);
14555 	ieee80211_hw_set(hw, REPORTS_LOW_ACK);
14556 	ieee80211_hw_set(hw, NO_VIRTUAL_MONITOR);
14557 
14558 	if (test_bit(WMI_TLV_SERVICE_ETH_OFFLOAD, ar->wmi->wmi_ab->svc_map)) {
14559 		ieee80211_hw_set(hw, SUPPORTS_TX_ENCAP_OFFLOAD);
14560 		ieee80211_hw_set(hw, SUPPORTS_RX_DECAP_OFFLOAD);
14561 	}
14562 
14563 	if (cap->nss_ratio_enabled)
14564 		ieee80211_hw_set(hw, SUPPORTS_VHT_EXT_NSS_BW);
14565 
14566 	if ((ht_cap & WMI_HT_CAP_ENABLED) || is_6ghz) {
14567 		ieee80211_hw_set(hw, AMPDU_AGGREGATION);
14568 		ieee80211_hw_set(hw, TX_AMPDU_SETUP_IN_HW);
14569 		ieee80211_hw_set(hw, SUPPORTS_REORDERING_BUFFER);
14570 		ieee80211_hw_set(hw, SUPPORTS_AMSDU_IN_AMPDU);
14571 		ieee80211_hw_set(hw, USES_RSS);
14572 	}
14573 
14574 	wiphy->features |= NL80211_FEATURE_STATIC_SMPS;
14575 	wiphy->flags |= WIPHY_FLAG_IBSS_RSN;
14576 
14577 	/* TODO: Check if HT capability advertised from firmware is different
14578 	 * for each band for a dual band capable radio. It will be tricky to
14579 	 * handle it when the ht capability different for each band.
14580 	 */
14581 	if (ht_cap & WMI_HT_CAP_DYNAMIC_SMPS ||
14582 	    (is_6ghz && ab->hw_params->supports_dynamic_smps_6ghz))
14583 		wiphy->features |= NL80211_FEATURE_DYNAMIC_SMPS;
14584 
14585 	wiphy->max_scan_ssids = WLAN_SCAN_PARAMS_MAX_SSID;
14586 	wiphy->max_scan_ie_len = WLAN_SCAN_PARAMS_MAX_IE_LEN;
14587 
14588 	hw->max_listen_interval = ATH12K_MAX_HW_LISTEN_INTERVAL;
14589 
14590 	wiphy->flags |= WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL;
14591 	wiphy->flags |= WIPHY_FLAG_HAS_CHANNEL_SWITCH;
14592 	wiphy->max_remain_on_channel_duration = 5000;
14593 
14594 	wiphy->flags |= WIPHY_FLAG_AP_UAPSD;
14595 	wiphy->features |= NL80211_FEATURE_AP_MODE_CHAN_WIDTH_CHANGE |
14596 				   NL80211_FEATURE_AP_SCAN;
14597 
14598 	wiphy->features |= NL80211_FEATURE_TX_POWER_INSERTION;
14599 
14600 	/* MLO is not yet supported so disable Wireless Extensions for now
14601 	 * to make sure ath12k users don't use it. This flag can be removed
14602 	 * once WIPHY_FLAG_SUPPORTS_MLO is enabled.
14603 	 */
14604 	wiphy->flags |= WIPHY_FLAG_DISABLE_WEXT;
14605 
14606 	/* Copy over MLO related capabilities received from
14607 	 * WMI_SERVICE_READY_EXT2_EVENT if single_chip_mlo_supp is set.
14608 	 */
14609 	if (ab->ag->mlo_capable) {
14610 		ath12k_iftypes_ext_capa[2].eml_capabilities = cap->eml_cap;
14611 		ath12k_iftypes_ext_capa[2].mld_capa_and_ops = cap->mld_cap;
14612 		wiphy->flags |= WIPHY_FLAG_SUPPORTS_MLO;
14613 
14614 		ieee80211_hw_set(hw, MLO_MCAST_MULTI_LINK_TX);
14615 	}
14616 
14617 	hw->queues = ATH12K_HW_MAX_QUEUES;
14618 	wiphy->tx_queue_len = ATH12K_QUEUE_LEN;
14619 	hw->offchannel_tx_hw_queue = ATH12K_HW_MAX_QUEUES - 1;
14620 	hw->max_rx_aggregation_subframes = IEEE80211_MAX_AMPDU_BUF_EHT;
14621 
14622 	hw->vif_data_size = sizeof(struct ath12k_vif);
14623 	hw->sta_data_size = sizeof(struct ath12k_sta);
14624 	hw->extra_tx_headroom = ab->hw_params->iova_mask;
14625 
14626 	wiphy_ext_feature_set(wiphy, NL80211_EXT_FEATURE_CQM_RSSI_LIST);
14627 	wiphy_ext_feature_set(wiphy, NL80211_EXT_FEATURE_STA_TX_PWR);
14628 	wiphy_ext_feature_set(wiphy, NL80211_EXT_FEATURE_ACK_SIGNAL_SUPPORT);
14629 	if (test_bit(WMI_TLV_SERVICE_BSS_COLOR_OFFLOAD,
14630 		     ab->wmi_ab.svc_map)) {
14631 		wiphy_ext_feature_set(wiphy, NL80211_EXT_FEATURE_BSS_COLOR);
14632 		ieee80211_hw_set(hw, DETECTS_COLOR_COLLISION);
14633 	}
14634 
14635 	wiphy->cipher_suites = cipher_suites;
14636 	wiphy->n_cipher_suites = ARRAY_SIZE(cipher_suites);
14637 
14638 	wiphy->iftype_ext_capab = ath12k_iftypes_ext_capa;
14639 	wiphy->num_iftype_ext_capab = ARRAY_SIZE(ath12k_iftypes_ext_capa);
14640 
14641 	wiphy->mbssid_max_interfaces = mbssid_max_interfaces;
14642 	wiphy->ema_max_profile_periodicity = TARGET_EMA_MAX_PROFILE_PERIOD;
14643 	ieee80211_hw_set(hw, SUPPORTS_MULTI_BSSID);
14644 
14645 	if (is_6ghz) {
14646 		wiphy_ext_feature_set(wiphy,
14647 				      NL80211_EXT_FEATURE_FILS_DISCOVERY);
14648 		wiphy_ext_feature_set(wiphy,
14649 				      NL80211_EXT_FEATURE_UNSOL_BCAST_PROBE_RESP);
14650 	}
14651 
14652 	wiphy_ext_feature_set(wiphy, NL80211_EXT_FEATURE_PUNCT);
14653 	if (test_bit(WMI_TLV_SERVICE_BEACON_PROTECTION_SUPPORT, ab->wmi_ab.svc_map))
14654 		wiphy_ext_feature_set(hw->wiphy, NL80211_EXT_FEATURE_BEACON_PROTECTION);
14655 
14656 	ath12k_reg_init(hw);
14657 
14658 	if (!is_raw_mode) {
14659 		hw->netdev_features = NETIF_F_HW_CSUM;
14660 		ieee80211_hw_set(hw, SW_CRYPTO_CONTROL);
14661 		ieee80211_hw_set(hw, SUPPORT_FAST_XMIT);
14662 	}
14663 
14664 	if (test_bit(WMI_TLV_SERVICE_NLO, ar->wmi->wmi_ab->svc_map)) {
14665 		wiphy->max_sched_scan_ssids = WMI_PNO_MAX_SUPP_NETWORKS;
14666 		wiphy->max_match_sets = WMI_PNO_MAX_SUPP_NETWORKS;
14667 		wiphy->max_sched_scan_ie_len = WMI_PNO_MAX_IE_LENGTH;
14668 		wiphy->max_sched_scan_plans = WMI_PNO_MAX_SCHED_SCAN_PLANS;
14669 		wiphy->max_sched_scan_plan_interval =
14670 					WMI_PNO_MAX_SCHED_SCAN_PLAN_INT;
14671 		wiphy->max_sched_scan_plan_iterations =
14672 					WMI_PNO_MAX_SCHED_SCAN_PLAN_ITRNS;
14673 		wiphy->features |= NL80211_FEATURE_ND_RANDOM_MAC_ADDR;
14674 	}
14675 
14676 	ret = ath12k_wow_init(ar);
14677 	if (ret) {
14678 		ath12k_warn(ar->ab, "failed to init wow: %d\n", ret);
14679 		goto err_cleanup_if_combs;
14680 	}
14681 
14682 	/* Boot-time regulatory updates have already been processed.
14683 	 * Mark them as complete now, because after registration,
14684 	 * cfg80211 will notify us again if there are any pending hints.
14685 	 * We need to wait for those hints to be processed, so it's
14686 	 * important to mark the boot-time updates as complete before
14687 	 * proceeding with registration.
14688 	 */
14689 	for_each_ar(ah, ar, i)
14690 		complete_all(&ar->regd_update_completed);
14691 
14692 	ret = ieee80211_register_hw(hw);
14693 	if (ret) {
14694 		ath12k_err(ab, "ieee80211 registration failed: %d\n", ret);
14695 		goto err_cleanup_if_combs;
14696 	}
14697 
14698 	if (is_monitor_disable)
14699 		/* There's a race between calling ieee80211_register_hw()
14700 		 * and here where the monitor mode is enabled for a little
14701 		 * while. But that time is so short and in practice it doesn't make
14702 		 * a difference in real life.
14703 		 */
14704 		wiphy->interface_modes &= ~BIT(NL80211_IFTYPE_MONITOR);
14705 
14706 	for_each_ar(ah, ar, i) {
14707 		/* Apply the regd received during initialization */
14708 		ret = ath12k_regd_update(ar, true);
14709 		if (ret) {
14710 			ath12k_err(ar->ab, "ath12k regd update failed: %d\n", ret);
14711 			goto err_unregister_hw;
14712 		}
14713 
14714 		if (ar->ab->hw_params->current_cc_support && ab->new_alpha2[0]) {
14715 			struct wmi_set_current_country_arg current_cc = {};
14716 
14717 			memcpy(&current_cc.alpha2, ab->new_alpha2, 2);
14718 			memcpy(&ar->alpha2, ab->new_alpha2, 2);
14719 
14720 			reinit_completion(&ar->regd_update_completed);
14721 
14722 			ret = ath12k_wmi_send_set_current_country_cmd(ar, &current_cc);
14723 			if (ret)
14724 				ath12k_warn(ar->ab,
14725 					    "failed set cc code for mac register: %d\n",
14726 					    ret);
14727 		}
14728 
14729 		ath12k_fw_stats_init(ar);
14730 		ath12k_debugfs_register(ar);
14731 	}
14732 
14733 	return 0;
14734 
14735 err_unregister_hw:
14736 	for_each_ar(ah, ar, i)
14737 		ath12k_debugfs_unregister(ar);
14738 
14739 	ieee80211_unregister_hw(hw);
14740 
14741 err_cleanup_if_combs:
14742 	ath12k_mac_cleanup_iface_combinations(ah);
14743 
14744 err_complete_cleanup_unregister:
14745 	i = ah->num_radio;
14746 
14747 err_cleanup_unregister:
14748 	for (j = 0; j < i; j++) {
14749 		ar = ath12k_ah_to_ar(ah, j);
14750 		ath12k_mac_cleanup_unregister(ar);
14751 	}
14752 
14753 	SET_IEEE80211_DEV(hw, NULL);
14754 
14755 	return ret;
14756 }
14757 
14758 static void ath12k_mac_setup(struct ath12k *ar)
14759 {
14760 	struct ath12k_base *ab = ar->ab;
14761 	struct ath12k_pdev *pdev = ar->pdev;
14762 	u8 pdev_idx = ar->pdev_idx;
14763 
14764 	ar->lmac_id = ath12k_hw_get_mac_from_pdev_id(ab->hw_params, pdev_idx);
14765 
14766 	ar->wmi = &ab->wmi_ab.wmi[pdev_idx];
14767 	/* FIXME: wmi[0] is already initialized during attach,
14768 	 * Should we do this again?
14769 	 */
14770 	ath12k_wmi_pdev_attach(ab, pdev_idx);
14771 
14772 	ar->cfg_tx_chainmask = pdev->cap.tx_chain_mask;
14773 	ar->cfg_rx_chainmask = pdev->cap.rx_chain_mask;
14774 	ar->num_tx_chains = hweight32(pdev->cap.tx_chain_mask);
14775 	ar->num_rx_chains = hweight32(pdev->cap.rx_chain_mask);
14776 	ar->scan.arvif = NULL;
14777 	ar->vdev_id_11d_scan = ATH12K_11D_INVALID_VDEV_ID;
14778 
14779 	spin_lock_init(&ar->data_lock);
14780 	spin_lock_init(&ar->dp.ppdu_list_lock);
14781 	INIT_LIST_HEAD(&ar->arvifs);
14782 	INIT_LIST_HEAD(&ar->dp.ppdu_stats_info);
14783 
14784 	init_completion(&ar->vdev_setup_done);
14785 	init_completion(&ar->vdev_delete_done);
14786 	init_completion(&ar->peer_assoc_done);
14787 	init_completion(&ar->peer_delete_done);
14788 	init_completion(&ar->install_key_done);
14789 	init_completion(&ar->bss_survey_done);
14790 	init_completion(&ar->scan.started);
14791 	init_completion(&ar->scan.completed);
14792 	init_completion(&ar->scan.on_channel);
14793 	init_completion(&ar->mlo_setup_done);
14794 	init_completion(&ar->completed_11d_scan);
14795 	init_completion(&ar->regd_update_completed);
14796 	init_completion(&ar->thermal.wmi_sync);
14797 
14798 	ar->thermal.temperature = 0;
14799 	ar->thermal.hwmon_dev = NULL;
14800 
14801 	INIT_DELAYED_WORK(&ar->scan.timeout, ath12k_scan_timeout_work);
14802 	wiphy_work_init(&ar->scan.vdev_clean_wk, ath12k_scan_vdev_clean_work);
14803 	INIT_WORK(&ar->regd_channel_update_work, ath12k_regd_update_chan_list_work);
14804 	INIT_LIST_HEAD(&ar->regd_channel_update_queue);
14805 	INIT_WORK(&ar->regd_update_work, ath12k_regd_update_work);
14806 
14807 	wiphy_work_init(&ar->wmi_mgmt_tx_work, ath12k_mgmt_over_wmi_tx_work);
14808 	skb_queue_head_init(&ar->wmi_mgmt_tx_queue);
14809 
14810 	ar->monitor_vdev_id = -1;
14811 	ar->monitor_vdev_created = false;
14812 	ar->monitor_started = false;
14813 }
14814 
14815 static int __ath12k_mac_mlo_setup(struct ath12k *ar)
14816 {
14817 	u8 num_link = 0, partner_link_id[ATH12K_GROUP_MAX_RADIO] = {};
14818 	struct ath12k_base *partner_ab, *ab = ar->ab;
14819 	struct ath12k_hw_group *ag = ab->ag;
14820 	struct wmi_mlo_setup_arg mlo = {};
14821 	struct ath12k_pdev *pdev;
14822 	unsigned long time_left;
14823 	int i, j, ret;
14824 
14825 	lockdep_assert_held(&ag->mutex);
14826 
14827 	reinit_completion(&ar->mlo_setup_done);
14828 
14829 	for (i = 0; i < ag->num_devices; i++) {
14830 		partner_ab = ag->ab[i];
14831 
14832 		for (j = 0; j < partner_ab->num_radios; j++) {
14833 			pdev = &partner_ab->pdevs[j];
14834 
14835 			/* Avoid the self link */
14836 			if (ar == pdev->ar)
14837 				continue;
14838 
14839 			partner_link_id[num_link] = pdev->hw_link_id;
14840 			num_link++;
14841 
14842 			ath12k_dbg(ab, ATH12K_DBG_MAC, "device %d pdev %d hw_link_id %d num_link %d\n",
14843 				   i, j, pdev->hw_link_id, num_link);
14844 		}
14845 	}
14846 
14847 	if (num_link == 0)
14848 		return 0;
14849 
14850 	mlo.group_id = cpu_to_le32(ag->id);
14851 	mlo.partner_link_id = partner_link_id;
14852 	mlo.num_partner_links = num_link;
14853 	ar->mlo_setup_status = 0;
14854 
14855 	ath12k_dbg(ab, ATH12K_DBG_MAC, "group id %d num_link %d\n", ag->id, num_link);
14856 
14857 	ret = ath12k_wmi_mlo_setup(ar, &mlo);
14858 	if (ret) {
14859 		ath12k_err(ab, "failed to send  setup MLO WMI command for pdev %d: %d\n",
14860 			   ar->pdev_idx, ret);
14861 		return ret;
14862 	}
14863 
14864 	time_left = wait_for_completion_timeout(&ar->mlo_setup_done,
14865 						WMI_MLO_CMD_TIMEOUT_HZ);
14866 
14867 	if (!time_left || ar->mlo_setup_status)
14868 		return ar->mlo_setup_status ? : -ETIMEDOUT;
14869 
14870 	ath12k_dbg(ab, ATH12K_DBG_MAC, "mlo setup done for pdev %d\n", ar->pdev_idx);
14871 
14872 	return 0;
14873 }
14874 
14875 static int __ath12k_mac_mlo_teardown(struct ath12k *ar)
14876 {
14877 	struct ath12k_base *ab = ar->ab;
14878 	int ret;
14879 	u8 num_link;
14880 
14881 	if (test_bit(ATH12K_FLAG_RECOVERY, &ab->dev_flags))
14882 		return 0;
14883 
14884 	num_link = ath12k_get_num_partner_link(ar);
14885 
14886 	if (num_link == 0)
14887 		return 0;
14888 
14889 	ret = ath12k_wmi_mlo_teardown(ar);
14890 	if (ret) {
14891 		ath12k_warn(ab, "failed to send MLO teardown WMI command for pdev %d: %d\n",
14892 			    ar->pdev_idx, ret);
14893 		return ret;
14894 	}
14895 
14896 	ath12k_dbg(ab, ATH12K_DBG_MAC, "mlo teardown for pdev %d\n", ar->pdev_idx);
14897 
14898 	return 0;
14899 }
14900 
14901 int ath12k_mac_mlo_setup(struct ath12k_hw_group *ag)
14902 {
14903 	struct ath12k_hw *ah;
14904 	struct ath12k *ar;
14905 	int ret;
14906 	int i, j;
14907 
14908 	for (i = 0; i < ag->num_hw; i++) {
14909 		ah = ag->ah[i];
14910 		if (!ah)
14911 			continue;
14912 
14913 		for_each_ar(ah, ar, j) {
14914 			ar = &ah->radio[j];
14915 			ret = __ath12k_mac_mlo_setup(ar);
14916 			if (ret) {
14917 				ath12k_err(ar->ab, "failed to setup MLO: %d\n", ret);
14918 				goto err_setup;
14919 			}
14920 		}
14921 	}
14922 
14923 	return 0;
14924 
14925 err_setup:
14926 	for (i = i - 1; i >= 0; i--) {
14927 		ah = ag->ah[i];
14928 		if (!ah)
14929 			continue;
14930 
14931 		for (j = j - 1; j >= 0; j--) {
14932 			ar = &ah->radio[j];
14933 			if (!ar)
14934 				continue;
14935 
14936 			__ath12k_mac_mlo_teardown(ar);
14937 		}
14938 	}
14939 
14940 	return ret;
14941 }
14942 
14943 void ath12k_mac_mlo_teardown(struct ath12k_hw_group *ag)
14944 {
14945 	struct ath12k_hw *ah;
14946 	struct ath12k *ar;
14947 	int ret, i, j;
14948 
14949 	for (i = 0; i < ag->num_hw; i++) {
14950 		ah = ag->ah[i];
14951 		if (!ah)
14952 			continue;
14953 
14954 		for_each_ar(ah, ar, j) {
14955 			ar = &ah->radio[j];
14956 			ret = __ath12k_mac_mlo_teardown(ar);
14957 			if (ret) {
14958 				ath12k_err(ar->ab, "failed to teardown MLO: %d\n", ret);
14959 				break;
14960 			}
14961 		}
14962 	}
14963 }
14964 
14965 int ath12k_mac_register(struct ath12k_hw_group *ag)
14966 {
14967 	struct ath12k_hw *ah;
14968 	int i;
14969 	int ret;
14970 
14971 	for (i = 0; i < ag->num_hw; i++) {
14972 		ah = ath12k_ag_to_ah(ag, i);
14973 
14974 		ret = ath12k_mac_hw_register(ah);
14975 		if (ret)
14976 			goto err;
14977 	}
14978 
14979 	return 0;
14980 
14981 err:
14982 	for (i = i - 1; i >= 0; i--) {
14983 		ah = ath12k_ag_to_ah(ag, i);
14984 		if (!ah)
14985 			continue;
14986 
14987 		ath12k_mac_hw_unregister(ah);
14988 	}
14989 
14990 	return ret;
14991 }
14992 
14993 void ath12k_mac_unregister(struct ath12k_hw_group *ag)
14994 {
14995 	struct ath12k_hw *ah;
14996 	int i;
14997 
14998 	for (i = ag->num_hw - 1; i >= 0; i--) {
14999 		ah = ath12k_ag_to_ah(ag, i);
15000 		if (!ah)
15001 			continue;
15002 
15003 		ath12k_mac_hw_unregister(ah);
15004 	}
15005 }
15006 
15007 static void ath12k_mac_hw_destroy(struct ath12k_hw *ah)
15008 {
15009 	ieee80211_free_hw(ah->hw);
15010 }
15011 
15012 static struct ath12k_hw *ath12k_mac_hw_allocate(struct ath12k_hw_group *ag,
15013 						struct ath12k_pdev_map *pdev_map,
15014 						u8 num_pdev_map)
15015 {
15016 	struct ieee80211_hw *hw;
15017 	struct ath12k *ar;
15018 	struct ath12k_base *ab;
15019 	struct ath12k_pdev *pdev;
15020 	struct ath12k_hw *ah;
15021 	int i;
15022 	u8 pdev_idx;
15023 
15024 	hw = ieee80211_alloc_hw(struct_size(ah, radio, num_pdev_map),
15025 				pdev_map->ab->ath12k_ops);
15026 	if (!hw)
15027 		return NULL;
15028 
15029 	ah = ath12k_hw_to_ah(hw);
15030 	ah->hw = hw;
15031 	ah->num_radio = num_pdev_map;
15032 
15033 	mutex_init(&ah->hw_mutex);
15034 
15035 	spin_lock_init(&ah->dp_hw.peer_lock);
15036 	INIT_LIST_HEAD(&ah->dp_hw.dp_peers_list);
15037 
15038 	for (i = 0; i < num_pdev_map; i++) {
15039 		ab = pdev_map[i].ab;
15040 		pdev_idx = pdev_map[i].pdev_idx;
15041 		pdev = &ab->pdevs[pdev_idx];
15042 
15043 		ar = ath12k_ah_to_ar(ah, i);
15044 		ar->ah = ah;
15045 		ar->ab = ab;
15046 		ar->hw_link_id = pdev->hw_link_id;
15047 		ar->pdev = pdev;
15048 		ar->pdev_idx = pdev_idx;
15049 		pdev->ar = ar;
15050 
15051 		ag->hw_links[ar->hw_link_id].device_id = ab->device_id;
15052 		ag->hw_links[ar->hw_link_id].pdev_idx = pdev_idx;
15053 
15054 		ath12k_mac_setup(ar);
15055 		ath12k_dp_pdev_pre_alloc(ar);
15056 	}
15057 
15058 	return ah;
15059 }
15060 
15061 void ath12k_mac_destroy(struct ath12k_hw_group *ag)
15062 {
15063 	struct ath12k_pdev *pdev;
15064 	struct ath12k_base *ab = ag->ab[0];
15065 	int i, j;
15066 	struct ath12k_hw *ah;
15067 
15068 	for (i = 0; i < ag->num_devices; i++) {
15069 		ab = ag->ab[i];
15070 		if (!ab)
15071 			continue;
15072 
15073 		for (j = 0; j < ab->num_radios; j++) {
15074 			pdev = &ab->pdevs[j];
15075 			if (!pdev->ar)
15076 				continue;
15077 			pdev->ar = NULL;
15078 		}
15079 	}
15080 
15081 	for (i = 0; i < ag->num_hw; i++) {
15082 		ah = ath12k_ag_to_ah(ag, i);
15083 		if (!ah)
15084 			continue;
15085 
15086 		ath12k_mac_hw_destroy(ah);
15087 		ath12k_ag_set_ah(ag, i, NULL);
15088 	}
15089 }
15090 
15091 static void ath12k_mac_set_device_defaults(struct ath12k_base *ab)
15092 {
15093 	int total_vdev;
15094 
15095 	/* Initialize channel counters frequency value in hertz */
15096 	ab->cc_freq_hz = 320000;
15097 	total_vdev = ab->num_radios * TARGET_NUM_VDEVS(ab);
15098 	ab->free_vdev_map = (1LL << total_vdev) - 1;
15099 }
15100 
15101 int ath12k_mac_allocate(struct ath12k_hw_group *ag)
15102 {
15103 	struct ath12k_pdev_map pdev_map[ATH12K_GROUP_MAX_RADIO];
15104 	int mac_id, device_id, total_radio, num_hw;
15105 	struct ath12k_base *ab;
15106 	struct ath12k_hw *ah;
15107 	int ret, i, j;
15108 	u8 radio_per_hw;
15109 
15110 	total_radio = 0;
15111 	for (i = 0; i < ag->num_devices; i++) {
15112 		ab = ag->ab[i];
15113 		if (!ab)
15114 			continue;
15115 
15116 		ath12k_debugfs_pdev_create(ab);
15117 		ath12k_mac_set_device_defaults(ab);
15118 		total_radio += ab->num_radios;
15119 	}
15120 
15121 	if (!total_radio)
15122 		return -EINVAL;
15123 
15124 	if (WARN_ON(total_radio > ATH12K_GROUP_MAX_RADIO))
15125 		return -ENOSPC;
15126 
15127 	/* All pdev get combined and register as single wiphy based on
15128 	 * hardware group which participate in multi-link operation else
15129 	 * each pdev get register separately.
15130 	 */
15131 	if (ag->mlo_capable)
15132 		radio_per_hw = total_radio;
15133 	else
15134 		radio_per_hw = 1;
15135 
15136 	num_hw = total_radio / radio_per_hw;
15137 
15138 	ag->num_hw = 0;
15139 	device_id = 0;
15140 	mac_id = 0;
15141 	for (i = 0; i < num_hw; i++) {
15142 		for (j = 0; j < radio_per_hw; j++) {
15143 			if (device_id >= ag->num_devices || !ag->ab[device_id]) {
15144 				ret = -ENOSPC;
15145 				goto err;
15146 			}
15147 
15148 			ab = ag->ab[device_id];
15149 			pdev_map[j].ab = ab;
15150 			pdev_map[j].pdev_idx = mac_id;
15151 			mac_id++;
15152 
15153 			/* If mac_id falls beyond the current device MACs then
15154 			 * move to next device
15155 			 */
15156 			if (mac_id >= ab->num_radios) {
15157 				mac_id = 0;
15158 				device_id++;
15159 			}
15160 		}
15161 
15162 		ab = pdev_map->ab;
15163 
15164 		ah = ath12k_mac_hw_allocate(ag, pdev_map, radio_per_hw);
15165 		if (!ah) {
15166 			ath12k_warn(ab, "failed to allocate mac80211 hw device for hw_idx %d\n",
15167 				    i);
15168 			ret = -ENOMEM;
15169 			goto err;
15170 		}
15171 
15172 		ah->dev = ab->dev;
15173 
15174 		ag->ah[i] = ah;
15175 		ag->num_hw++;
15176 	}
15177 
15178 	return 0;
15179 
15180 err:
15181 	for (i = i - 1; i >= 0; i--) {
15182 		ah = ath12k_ag_to_ah(ag, i);
15183 		if (!ah)
15184 			continue;
15185 
15186 		ath12k_mac_hw_destroy(ah);
15187 		ath12k_ag_set_ah(ag, i, NULL);
15188 	}
15189 
15190 	return ret;
15191 }
15192 
15193 int ath12k_mac_vif_set_keepalive(struct ath12k_link_vif *arvif,
15194 				 enum wmi_sta_keepalive_method method,
15195 				 u32 interval)
15196 {
15197 	struct wmi_sta_keepalive_arg arg = {};
15198 	struct ath12k *ar = arvif->ar;
15199 	int ret;
15200 
15201 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
15202 
15203 	if (arvif->ahvif->vdev_type != WMI_VDEV_TYPE_STA)
15204 		return 0;
15205 
15206 	if (!test_bit(WMI_TLV_SERVICE_STA_KEEP_ALIVE, ar->ab->wmi_ab.svc_map))
15207 		return 0;
15208 
15209 	arg.vdev_id = arvif->vdev_id;
15210 	arg.enabled = 1;
15211 	arg.method = method;
15212 	arg.interval = interval;
15213 
15214 	ret = ath12k_wmi_sta_keepalive(ar, &arg);
15215 	if (ret) {
15216 		ath12k_warn(ar->ab, "failed to set keepalive on vdev %i: %d\n",
15217 			    arvif->vdev_id, ret);
15218 		return ret;
15219 	}
15220 
15221 	return 0;
15222 }
15223