xref: /linux/drivers/net/wireless/ath/ath12k/mac.c (revision 31707572108da55a005e7fed32cc3869c16b7c16)
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 
25 #define CHAN2G(_channel, _freq, _flags) { \
26 	.band                   = NL80211_BAND_2GHZ, \
27 	.hw_value               = (_channel), \
28 	.center_freq            = (_freq), \
29 	.flags                  = (_flags), \
30 	.max_antenna_gain       = 0, \
31 	.max_power              = 30, \
32 }
33 
34 #define CHAN5G(_channel, _freq, _flags) { \
35 	.band                   = NL80211_BAND_5GHZ, \
36 	.hw_value               = (_channel), \
37 	.center_freq            = (_freq), \
38 	.flags                  = (_flags), \
39 	.max_antenna_gain       = 0, \
40 	.max_power              = 30, \
41 }
42 
43 #define CHAN6G(_channel, _freq, _flags) { \
44 	.band                   = NL80211_BAND_6GHZ, \
45 	.hw_value               = (_channel), \
46 	.center_freq            = (_freq), \
47 	.flags                  = (_flags), \
48 	.max_antenna_gain       = 0, \
49 	.max_power              = 30, \
50 }
51 
52 static const struct ieee80211_channel ath12k_2ghz_channels[] = {
53 	CHAN2G(1, 2412, 0),
54 	CHAN2G(2, 2417, 0),
55 	CHAN2G(3, 2422, 0),
56 	CHAN2G(4, 2427, 0),
57 	CHAN2G(5, 2432, 0),
58 	CHAN2G(6, 2437, 0),
59 	CHAN2G(7, 2442, 0),
60 	CHAN2G(8, 2447, 0),
61 	CHAN2G(9, 2452, 0),
62 	CHAN2G(10, 2457, 0),
63 	CHAN2G(11, 2462, 0),
64 	CHAN2G(12, 2467, 0),
65 	CHAN2G(13, 2472, 0),
66 	CHAN2G(14, 2484, 0),
67 };
68 
69 static const struct ieee80211_channel ath12k_5ghz_channels[] = {
70 	CHAN5G(36, 5180, 0),
71 	CHAN5G(40, 5200, 0),
72 	CHAN5G(44, 5220, 0),
73 	CHAN5G(48, 5240, 0),
74 	CHAN5G(52, 5260, 0),
75 	CHAN5G(56, 5280, 0),
76 	CHAN5G(60, 5300, 0),
77 	CHAN5G(64, 5320, 0),
78 	CHAN5G(100, 5500, 0),
79 	CHAN5G(104, 5520, 0),
80 	CHAN5G(108, 5540, 0),
81 	CHAN5G(112, 5560, 0),
82 	CHAN5G(116, 5580, 0),
83 	CHAN5G(120, 5600, 0),
84 	CHAN5G(124, 5620, 0),
85 	CHAN5G(128, 5640, 0),
86 	CHAN5G(132, 5660, 0),
87 	CHAN5G(136, 5680, 0),
88 	CHAN5G(140, 5700, 0),
89 	CHAN5G(144, 5720, 0),
90 	CHAN5G(149, 5745, 0),
91 	CHAN5G(153, 5765, 0),
92 	CHAN5G(157, 5785, 0),
93 	CHAN5G(161, 5805, 0),
94 	CHAN5G(165, 5825, 0),
95 	CHAN5G(169, 5845, 0),
96 	CHAN5G(173, 5865, 0),
97 };
98 
99 static const struct ieee80211_channel ath12k_6ghz_channels[] = {
100 	/* Operating Class 136 */
101 	CHAN6G(2, 5935, 0),
102 
103 	/* Operating Classes 131-135 */
104 	CHAN6G(1, 5955, 0),
105 	CHAN6G(5, 5975, 0),
106 	CHAN6G(9, 5995, 0),
107 	CHAN6G(13, 6015, 0),
108 	CHAN6G(17, 6035, 0),
109 	CHAN6G(21, 6055, 0),
110 	CHAN6G(25, 6075, 0),
111 	CHAN6G(29, 6095, 0),
112 	CHAN6G(33, 6115, 0),
113 	CHAN6G(37, 6135, 0),
114 	CHAN6G(41, 6155, 0),
115 	CHAN6G(45, 6175, 0),
116 	CHAN6G(49, 6195, 0),
117 	CHAN6G(53, 6215, 0),
118 	CHAN6G(57, 6235, 0),
119 	CHAN6G(61, 6255, 0),
120 	CHAN6G(65, 6275, 0),
121 	CHAN6G(69, 6295, 0),
122 	CHAN6G(73, 6315, 0),
123 	CHAN6G(77, 6335, 0),
124 	CHAN6G(81, 6355, 0),
125 	CHAN6G(85, 6375, 0),
126 	CHAN6G(89, 6395, 0),
127 	CHAN6G(93, 6415, 0),
128 	CHAN6G(97, 6435, 0),
129 	CHAN6G(101, 6455, 0),
130 	CHAN6G(105, 6475, 0),
131 	CHAN6G(109, 6495, 0),
132 	CHAN6G(113, 6515, 0),
133 	CHAN6G(117, 6535, 0),
134 	CHAN6G(121, 6555, 0),
135 	CHAN6G(125, 6575, 0),
136 	CHAN6G(129, 6595, 0),
137 	CHAN6G(133, 6615, 0),
138 	CHAN6G(137, 6635, 0),
139 	CHAN6G(141, 6655, 0),
140 	CHAN6G(145, 6675, 0),
141 	CHAN6G(149, 6695, 0),
142 	CHAN6G(153, 6715, 0),
143 	CHAN6G(157, 6735, 0),
144 	CHAN6G(161, 6755, 0),
145 	CHAN6G(165, 6775, 0),
146 	CHAN6G(169, 6795, 0),
147 	CHAN6G(173, 6815, 0),
148 	CHAN6G(177, 6835, 0),
149 	CHAN6G(181, 6855, 0),
150 	CHAN6G(185, 6875, 0),
151 	CHAN6G(189, 6895, 0),
152 	CHAN6G(193, 6915, 0),
153 	CHAN6G(197, 6935, 0),
154 	CHAN6G(201, 6955, 0),
155 	CHAN6G(205, 6975, 0),
156 	CHAN6G(209, 6995, 0),
157 	CHAN6G(213, 7015, 0),
158 	CHAN6G(217, 7035, 0),
159 	CHAN6G(221, 7055, 0),
160 	CHAN6G(225, 7075, 0),
161 	CHAN6G(229, 7095, 0),
162 	CHAN6G(233, 7115, 0),
163 };
164 
165 static struct ieee80211_rate ath12k_legacy_rates[] = {
166 	{ .bitrate = 10,
167 	  .hw_value = ATH12K_HW_RATE_CCK_LP_1M },
168 	{ .bitrate = 20,
169 	  .hw_value = ATH12K_HW_RATE_CCK_LP_2M,
170 	  .hw_value_short = ATH12K_HW_RATE_CCK_SP_2M,
171 	  .flags = IEEE80211_RATE_SHORT_PREAMBLE },
172 	{ .bitrate = 55,
173 	  .hw_value = ATH12K_HW_RATE_CCK_LP_5_5M,
174 	  .hw_value_short = ATH12K_HW_RATE_CCK_SP_5_5M,
175 	  .flags = IEEE80211_RATE_SHORT_PREAMBLE },
176 	{ .bitrate = 110,
177 	  .hw_value = ATH12K_HW_RATE_CCK_LP_11M,
178 	  .hw_value_short = ATH12K_HW_RATE_CCK_SP_11M,
179 	  .flags = IEEE80211_RATE_SHORT_PREAMBLE },
180 
181 	{ .bitrate = 60, .hw_value = ATH12K_HW_RATE_OFDM_6M },
182 	{ .bitrate = 90, .hw_value = ATH12K_HW_RATE_OFDM_9M },
183 	{ .bitrate = 120, .hw_value = ATH12K_HW_RATE_OFDM_12M },
184 	{ .bitrate = 180, .hw_value = ATH12K_HW_RATE_OFDM_18M },
185 	{ .bitrate = 240, .hw_value = ATH12K_HW_RATE_OFDM_24M },
186 	{ .bitrate = 360, .hw_value = ATH12K_HW_RATE_OFDM_36M },
187 	{ .bitrate = 480, .hw_value = ATH12K_HW_RATE_OFDM_48M },
188 	{ .bitrate = 540, .hw_value = ATH12K_HW_RATE_OFDM_54M },
189 };
190 
191 static const int
192 ath12k_phymodes[NUM_NL80211_BANDS][ATH12K_CHAN_WIDTH_NUM] = {
193 	[NL80211_BAND_2GHZ] = {
194 			[NL80211_CHAN_WIDTH_5] = MODE_UNKNOWN,
195 			[NL80211_CHAN_WIDTH_10] = MODE_UNKNOWN,
196 			[NL80211_CHAN_WIDTH_20_NOHT] = MODE_11BE_EHT20_2G,
197 			[NL80211_CHAN_WIDTH_20] = MODE_11BE_EHT20_2G,
198 			[NL80211_CHAN_WIDTH_40] = MODE_11BE_EHT40_2G,
199 			[NL80211_CHAN_WIDTH_80] = MODE_UNKNOWN,
200 			[NL80211_CHAN_WIDTH_80P80] = MODE_UNKNOWN,
201 			[NL80211_CHAN_WIDTH_160] = MODE_UNKNOWN,
202 			[NL80211_CHAN_WIDTH_320] = MODE_UNKNOWN,
203 	},
204 	[NL80211_BAND_5GHZ] = {
205 			[NL80211_CHAN_WIDTH_5] = MODE_UNKNOWN,
206 			[NL80211_CHAN_WIDTH_10] = MODE_UNKNOWN,
207 			[NL80211_CHAN_WIDTH_20_NOHT] = MODE_11BE_EHT20,
208 			[NL80211_CHAN_WIDTH_20] = MODE_11BE_EHT20,
209 			[NL80211_CHAN_WIDTH_40] = MODE_11BE_EHT40,
210 			[NL80211_CHAN_WIDTH_80] = MODE_11BE_EHT80,
211 			[NL80211_CHAN_WIDTH_160] = MODE_11BE_EHT160,
212 			[NL80211_CHAN_WIDTH_80P80] = MODE_UNKNOWN,
213 			[NL80211_CHAN_WIDTH_320] = MODE_11BE_EHT320,
214 	},
215 	[NL80211_BAND_6GHZ] = {
216 			[NL80211_CHAN_WIDTH_5] = MODE_UNKNOWN,
217 			[NL80211_CHAN_WIDTH_10] = MODE_UNKNOWN,
218 			[NL80211_CHAN_WIDTH_20_NOHT] = MODE_11BE_EHT20,
219 			[NL80211_CHAN_WIDTH_20] = MODE_11BE_EHT20,
220 			[NL80211_CHAN_WIDTH_40] = MODE_11BE_EHT40,
221 			[NL80211_CHAN_WIDTH_80] = MODE_11BE_EHT80,
222 			[NL80211_CHAN_WIDTH_160] = MODE_11BE_EHT160,
223 			[NL80211_CHAN_WIDTH_80P80] = MODE_UNKNOWN,
224 			[NL80211_CHAN_WIDTH_320] = MODE_11BE_EHT320,
225 	},
226 
227 };
228 
229 const struct htt_rx_ring_tlv_filter ath12k_mac_mon_status_filter_default = {
230 	.rx_filter = HTT_RX_FILTER_TLV_FLAGS_MPDU_START |
231 		     HTT_RX_FILTER_TLV_FLAGS_PPDU_END |
232 		     HTT_RX_FILTER_TLV_FLAGS_PPDU_END_STATUS_DONE |
233 		     HTT_RX_FILTER_TLV_FLAGS_PPDU_START_USER_INFO,
234 	.pkt_filter_flags0 = HTT_RX_FP_MGMT_FILTER_FLAGS0,
235 	.pkt_filter_flags1 = HTT_RX_FP_MGMT_FILTER_FLAGS1,
236 	.pkt_filter_flags2 = HTT_RX_FP_CTRL_FILTER_FLASG2,
237 	.pkt_filter_flags3 = HTT_RX_FP_DATA_FILTER_FLASG3 |
238 			     HTT_RX_FP_CTRL_FILTER_FLASG3
239 };
240 
241 #define ATH12K_MAC_FIRST_OFDM_RATE_IDX 4
242 #define ath12k_g_rates ath12k_legacy_rates
243 #define ath12k_g_rates_size (ARRAY_SIZE(ath12k_legacy_rates))
244 #define ath12k_a_rates (ath12k_legacy_rates + 4)
245 #define ath12k_a_rates_size (ARRAY_SIZE(ath12k_legacy_rates) - 4)
246 
247 #define ATH12K_MAC_SCAN_TIMEOUT_MSECS 200 /* in msecs */
248 
249 static const u32 ath12k_smps_map[] = {
250 	[WLAN_HT_CAP_SM_PS_STATIC] = WMI_PEER_SMPS_STATIC,
251 	[WLAN_HT_CAP_SM_PS_DYNAMIC] = WMI_PEER_SMPS_DYNAMIC,
252 	[WLAN_HT_CAP_SM_PS_INVALID] = WMI_PEER_SMPS_PS_NONE,
253 	[WLAN_HT_CAP_SM_PS_DISABLED] = WMI_PEER_SMPS_PS_NONE,
254 };
255 
256 static int ath12k_start_vdev_delay(struct ath12k *ar,
257 				   struct ath12k_link_vif *arvif);
258 static void ath12k_mac_stop(struct ath12k *ar);
259 static int ath12k_mac_vdev_create(struct ath12k *ar, struct ath12k_link_vif *arvif);
260 static int ath12k_mac_vdev_delete(struct ath12k *ar, struct ath12k_link_vif *arvif);
261 
262 static const char *ath12k_mac_phymode_str(enum wmi_phy_mode mode)
263 {
264 	switch (mode) {
265 	case MODE_11A:
266 		return "11a";
267 	case MODE_11G:
268 		return "11g";
269 	case MODE_11B:
270 		return "11b";
271 	case MODE_11GONLY:
272 		return "11gonly";
273 	case MODE_11NA_HT20:
274 		return "11na-ht20";
275 	case MODE_11NG_HT20:
276 		return "11ng-ht20";
277 	case MODE_11NA_HT40:
278 		return "11na-ht40";
279 	case MODE_11NG_HT40:
280 		return "11ng-ht40";
281 	case MODE_11AC_VHT20:
282 		return "11ac-vht20";
283 	case MODE_11AC_VHT40:
284 		return "11ac-vht40";
285 	case MODE_11AC_VHT80:
286 		return "11ac-vht80";
287 	case MODE_11AC_VHT160:
288 		return "11ac-vht160";
289 	case MODE_11AC_VHT80_80:
290 		return "11ac-vht80+80";
291 	case MODE_11AC_VHT20_2G:
292 		return "11ac-vht20-2g";
293 	case MODE_11AC_VHT40_2G:
294 		return "11ac-vht40-2g";
295 	case MODE_11AC_VHT80_2G:
296 		return "11ac-vht80-2g";
297 	case MODE_11AX_HE20:
298 		return "11ax-he20";
299 	case MODE_11AX_HE40:
300 		return "11ax-he40";
301 	case MODE_11AX_HE80:
302 		return "11ax-he80";
303 	case MODE_11AX_HE80_80:
304 		return "11ax-he80+80";
305 	case MODE_11AX_HE160:
306 		return "11ax-he160";
307 	case MODE_11AX_HE20_2G:
308 		return "11ax-he20-2g";
309 	case MODE_11AX_HE40_2G:
310 		return "11ax-he40-2g";
311 	case MODE_11AX_HE80_2G:
312 		return "11ax-he80-2g";
313 	case MODE_11BE_EHT20:
314 		return "11be-eht20";
315 	case MODE_11BE_EHT40:
316 		return "11be-eht40";
317 	case MODE_11BE_EHT80:
318 		return "11be-eht80";
319 	case MODE_11BE_EHT80_80:
320 		return "11be-eht80+80";
321 	case MODE_11BE_EHT160:
322 		return "11be-eht160";
323 	case MODE_11BE_EHT160_160:
324 		return "11be-eht160+160";
325 	case MODE_11BE_EHT320:
326 		return "11be-eht320";
327 	case MODE_11BE_EHT20_2G:
328 		return "11be-eht20-2g";
329 	case MODE_11BE_EHT40_2G:
330 		return "11be-eht40-2g";
331 	case MODE_UNKNOWN:
332 		/* skip */
333 		break;
334 
335 		/* no default handler to allow compiler to check that the
336 		 * enum is fully handled
337 		 */
338 	}
339 
340 	return "<unknown>";
341 }
342 
343 u16 ath12k_mac_he_convert_tones_to_ru_tones(u16 tones)
344 {
345 	switch (tones) {
346 	case 26:
347 		return RU_26;
348 	case 52:
349 		return RU_52;
350 	case 106:
351 		return RU_106;
352 	case 242:
353 		return RU_242;
354 	case 484:
355 		return RU_484;
356 	case 996:
357 		return RU_996;
358 	case (996 * 2):
359 		return RU_2X996;
360 	default:
361 		return RU_26;
362 	}
363 }
364 
365 enum nl80211_eht_gi ath12k_mac_eht_gi_to_nl80211_eht_gi(u8 sgi)
366 {
367 	switch (sgi) {
368 	case RX_MSDU_START_SGI_0_8_US:
369 		return NL80211_RATE_INFO_EHT_GI_0_8;
370 	case RX_MSDU_START_SGI_1_6_US:
371 		return NL80211_RATE_INFO_EHT_GI_1_6;
372 	case RX_MSDU_START_SGI_3_2_US:
373 		return NL80211_RATE_INFO_EHT_GI_3_2;
374 	default:
375 		return NL80211_RATE_INFO_EHT_GI_0_8;
376 	}
377 }
378 
379 enum nl80211_eht_ru_alloc ath12k_mac_eht_ru_tones_to_nl80211_eht_ru_alloc(u16 ru_tones)
380 {
381 	switch (ru_tones) {
382 	case 26:
383 		return NL80211_RATE_INFO_EHT_RU_ALLOC_26;
384 	case 52:
385 		return NL80211_RATE_INFO_EHT_RU_ALLOC_52;
386 	case (52 + 26):
387 		return NL80211_RATE_INFO_EHT_RU_ALLOC_52P26;
388 	case 106:
389 		return NL80211_RATE_INFO_EHT_RU_ALLOC_106;
390 	case (106 + 26):
391 		return NL80211_RATE_INFO_EHT_RU_ALLOC_106P26;
392 	case 242:
393 		return NL80211_RATE_INFO_EHT_RU_ALLOC_242;
394 	case 484:
395 		return NL80211_RATE_INFO_EHT_RU_ALLOC_484;
396 	case (484 + 242):
397 		return NL80211_RATE_INFO_EHT_RU_ALLOC_484P242;
398 	case 996:
399 		return NL80211_RATE_INFO_EHT_RU_ALLOC_996;
400 	case (996 + 484):
401 		return NL80211_RATE_INFO_EHT_RU_ALLOC_996P484;
402 	case (996 + 484 + 242):
403 		return NL80211_RATE_INFO_EHT_RU_ALLOC_996P484P242;
404 	case (2 * 996):
405 		return NL80211_RATE_INFO_EHT_RU_ALLOC_2x996;
406 	case (2 * 996 + 484):
407 		return NL80211_RATE_INFO_EHT_RU_ALLOC_2x996P484;
408 	case (3 * 996):
409 		return NL80211_RATE_INFO_EHT_RU_ALLOC_3x996;
410 	case (3 * 996 + 484):
411 		return NL80211_RATE_INFO_EHT_RU_ALLOC_3x996P484;
412 	case (4 * 996):
413 		return NL80211_RATE_INFO_EHT_RU_ALLOC_4x996;
414 	default:
415 		return NL80211_RATE_INFO_EHT_RU_ALLOC_26;
416 	}
417 }
418 
419 enum rate_info_bw
420 ath12k_mac_bw_to_mac80211_bw(enum ath12k_supported_bw bw)
421 {
422 	u8 ret = RATE_INFO_BW_20;
423 
424 	switch (bw) {
425 	case ATH12K_BW_20:
426 		ret = RATE_INFO_BW_20;
427 		break;
428 	case ATH12K_BW_40:
429 		ret = RATE_INFO_BW_40;
430 		break;
431 	case ATH12K_BW_80:
432 		ret = RATE_INFO_BW_80;
433 		break;
434 	case ATH12K_BW_160:
435 		ret = RATE_INFO_BW_160;
436 		break;
437 	case ATH12K_BW_320:
438 		ret = RATE_INFO_BW_320;
439 		break;
440 	}
441 
442 	return ret;
443 }
444 
445 enum ath12k_supported_bw ath12k_mac_mac80211_bw_to_ath12k_bw(enum rate_info_bw bw)
446 {
447 	switch (bw) {
448 	case RATE_INFO_BW_20:
449 		return ATH12K_BW_20;
450 	case RATE_INFO_BW_40:
451 		return ATH12K_BW_40;
452 	case RATE_INFO_BW_80:
453 		return ATH12K_BW_80;
454 	case RATE_INFO_BW_160:
455 		return ATH12K_BW_160;
456 	case RATE_INFO_BW_320:
457 		return ATH12K_BW_320;
458 	default:
459 		return ATH12K_BW_20;
460 	}
461 }
462 
463 int ath12k_mac_hw_ratecode_to_legacy_rate(u8 hw_rc, u8 preamble, u8 *rateidx,
464 					  u16 *rate)
465 {
466 	/* As default, it is OFDM rates */
467 	int i = ATH12K_MAC_FIRST_OFDM_RATE_IDX;
468 	int max_rates_idx = ath12k_g_rates_size;
469 
470 	if (preamble == WMI_RATE_PREAMBLE_CCK) {
471 		hw_rc &= ~ATH12K_HW_RATECODE_CCK_SHORT_PREAM_MASK;
472 		i = 0;
473 		max_rates_idx = ATH12K_MAC_FIRST_OFDM_RATE_IDX;
474 	}
475 
476 	while (i < max_rates_idx) {
477 		if (hw_rc == ath12k_legacy_rates[i].hw_value) {
478 			*rateidx = i;
479 			*rate = ath12k_legacy_rates[i].bitrate;
480 			return 0;
481 		}
482 		i++;
483 	}
484 
485 	return -EINVAL;
486 }
487 
488 u8 ath12k_mac_bitrate_to_idx(const struct ieee80211_supported_band *sband,
489 			     u32 bitrate)
490 {
491 	int i;
492 
493 	for (i = 0; i < sband->n_bitrates; i++)
494 		if (sband->bitrates[i].bitrate == bitrate)
495 			return i;
496 
497 	return 0;
498 }
499 
500 static u32
501 ath12k_mac_max_ht_nss(const u8 *ht_mcs_mask)
502 {
503 	int nss;
504 
505 	for (nss = IEEE80211_HT_MCS_MASK_LEN - 1; nss >= 0; nss--)
506 		if (ht_mcs_mask[nss])
507 			return nss + 1;
508 
509 	return 1;
510 }
511 
512 static u32
513 ath12k_mac_max_vht_nss(const u16 *vht_mcs_mask)
514 {
515 	int nss;
516 
517 	for (nss = NL80211_VHT_NSS_MAX - 1; nss >= 0; nss--)
518 		if (vht_mcs_mask[nss])
519 			return nss + 1;
520 
521 	return 1;
522 }
523 
524 static u32
525 ath12k_mac_max_he_nss(const u16 he_mcs_mask[NL80211_HE_NSS_MAX])
526 {
527 	int nss;
528 
529 	for (nss = NL80211_HE_NSS_MAX - 1; nss >= 0; nss--)
530 		if (he_mcs_mask[nss])
531 			return nss + 1;
532 
533 	return 1;
534 }
535 
536 static u32
537 ath12k_mac_max_eht_nss(const u16 eht_mcs_mask[NL80211_EHT_NSS_MAX])
538 {
539 	int nss;
540 
541 	for (nss = NL80211_EHT_NSS_MAX - 1; nss >= 0; nss--)
542 		if (eht_mcs_mask[nss])
543 			return nss + 1;
544 
545 	return 1;
546 }
547 
548 static u32
549 ath12k_mac_max_eht_mcs_nss(const u8 *eht_mcs, int eht_mcs_set_size)
550 {
551 	int i;
552 	u8 nss = 0;
553 
554 	for (i = 0; i < eht_mcs_set_size; i++)
555 		nss = max(nss, u8_get_bits(eht_mcs[i], IEEE80211_EHT_MCS_NSS_RX));
556 
557 	return nss;
558 }
559 
560 static u8 ath12k_parse_mpdudensity(u8 mpdudensity)
561 {
562 /*  From IEEE Std 802.11-2020 defined values for "Minimum MPDU Start Spacing":
563  *   0 for no restriction
564  *   1 for 1/4 us
565  *   2 for 1/2 us
566  *   3 for 1 us
567  *   4 for 2 us
568  *   5 for 4 us
569  *   6 for 8 us
570  *   7 for 16 us
571  */
572 	switch (mpdudensity) {
573 	case 0:
574 		return 0;
575 	case 1:
576 	case 2:
577 	case 3:
578 	/* Our lower layer calculations limit our precision to
579 	 * 1 microsecond
580 	 */
581 		return 1;
582 	case 4:
583 		return 2;
584 	case 5:
585 		return 4;
586 	case 6:
587 		return 8;
588 	case 7:
589 		return 16;
590 	default:
591 		return 0;
592 	}
593 }
594 
595 static int ath12k_mac_vif_link_chan(struct ieee80211_vif *vif, u8 link_id,
596 				    struct cfg80211_chan_def *def)
597 {
598 	struct ieee80211_bss_conf *link_conf;
599 	struct ieee80211_chanctx_conf *conf;
600 
601 	rcu_read_lock();
602 	link_conf = rcu_dereference(vif->link_conf[link_id]);
603 
604 	if (!link_conf) {
605 		rcu_read_unlock();
606 		return -ENOLINK;
607 	}
608 
609 	conf = rcu_dereference(link_conf->chanctx_conf);
610 	if (!conf) {
611 		rcu_read_unlock();
612 		return -ENOENT;
613 	}
614 	*def = conf->def;
615 	rcu_read_unlock();
616 
617 	return 0;
618 }
619 
620 static struct ath12k_link_vif *
621 ath12k_mac_get_tx_arvif(struct ath12k_link_vif *arvif,
622 			struct ieee80211_bss_conf *link_conf)
623 {
624 	struct ieee80211_bss_conf *tx_bss_conf;
625 	struct ath12k *ar = arvif->ar;
626 	struct ath12k_vif *tx_ahvif;
627 
628 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
629 
630 	tx_bss_conf = wiphy_dereference(ath12k_ar_to_hw(ar)->wiphy,
631 					link_conf->tx_bss_conf);
632 	if (tx_bss_conf) {
633 		tx_ahvif = ath12k_vif_to_ahvif(tx_bss_conf->vif);
634 		return wiphy_dereference(tx_ahvif->ah->hw->wiphy,
635 					 tx_ahvif->link[tx_bss_conf->link_id]);
636 	}
637 
638 	return NULL;
639 }
640 
641 static const u8 *ath12k_mac_get_tx_bssid(struct ath12k_link_vif *arvif)
642 {
643 	struct ieee80211_bss_conf *link_conf;
644 	struct ath12k_link_vif *tx_arvif;
645 	struct ath12k *ar = arvif->ar;
646 
647 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
648 
649 	link_conf = ath12k_mac_get_link_bss_conf(arvif);
650 	if (!link_conf) {
651 		ath12k_warn(ar->ab,
652 			    "unable to access bss link conf for link %u required to retrieve transmitting link conf\n",
653 			    arvif->link_id);
654 		return NULL;
655 	}
656 	if (link_conf->vif->type == NL80211_IFTYPE_STATION) {
657 		if (link_conf->nontransmitted)
658 			return link_conf->transmitter_bssid;
659 	} else {
660 		tx_arvif = ath12k_mac_get_tx_arvif(arvif, link_conf);
661 		if (tx_arvif)
662 			return tx_arvif->bssid;
663 	}
664 
665 	return NULL;
666 }
667 
668 struct ieee80211_bss_conf *
669 ath12k_mac_get_link_bss_conf(struct ath12k_link_vif *arvif)
670 {
671 	struct ieee80211_vif *vif = arvif->ahvif->vif;
672 	struct ieee80211_bss_conf *link_conf;
673 	struct ath12k *ar = arvif->ar;
674 
675 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
676 
677 	if (arvif->link_id >= IEEE80211_MLD_MAX_NUM_LINKS)
678 		return NULL;
679 
680 	link_conf = wiphy_dereference(ath12k_ar_to_hw(ar)->wiphy,
681 				      vif->link_conf[arvif->link_id]);
682 
683 	return link_conf;
684 }
685 
686 static struct ieee80211_link_sta *ath12k_mac_get_link_sta(struct ath12k_link_sta *arsta)
687 {
688 	struct ath12k_sta *ahsta = arsta->ahsta;
689 	struct ieee80211_sta *sta = ath12k_ahsta_to_sta(ahsta);
690 	struct ieee80211_link_sta *link_sta;
691 
692 	lockdep_assert_wiphy(ahsta->ahvif->ah->hw->wiphy);
693 
694 	if (arsta->link_id >= IEEE80211_MLD_MAX_NUM_LINKS)
695 		return NULL;
696 
697 	link_sta = wiphy_dereference(ahsta->ahvif->ah->hw->wiphy,
698 				     sta->link[arsta->link_id]);
699 
700 	return link_sta;
701 }
702 
703 static bool ath12k_mac_bitrate_is_cck(int bitrate)
704 {
705 	switch (bitrate) {
706 	case 10:
707 	case 20:
708 	case 55:
709 	case 110:
710 		return true;
711 	}
712 
713 	return false;
714 }
715 
716 u8 ath12k_mac_hw_rate_to_idx(const struct ieee80211_supported_band *sband,
717 			     u8 hw_rate, bool cck)
718 {
719 	const struct ieee80211_rate *rate;
720 	int i;
721 
722 	for (i = 0; i < sband->n_bitrates; i++) {
723 		rate = &sband->bitrates[i];
724 
725 		if (ath12k_mac_bitrate_is_cck(rate->bitrate) != cck)
726 			continue;
727 
728 		if (rate->hw_value == hw_rate)
729 			return i;
730 		else if (rate->flags & IEEE80211_RATE_SHORT_PREAMBLE &&
731 			 rate->hw_value_short == hw_rate)
732 			return i;
733 	}
734 
735 	return 0;
736 }
737 
738 static u8 ath12k_mac_bitrate_to_rate(int bitrate)
739 {
740 	return DIV_ROUND_UP(bitrate, 5) |
741 	       (ath12k_mac_bitrate_is_cck(bitrate) ? BIT(7) : 0);
742 }
743 
744 static void ath12k_get_arvif_iter(void *data, u8 *mac,
745 				  struct ieee80211_vif *vif)
746 {
747 	struct ath12k_vif_iter *arvif_iter = data;
748 	struct ath12k_vif *ahvif = ath12k_vif_to_ahvif(vif);
749 	unsigned long links_map = ahvif->links_map;
750 	struct ath12k_link_vif *arvif;
751 	u8 link_id;
752 
753 	for_each_set_bit(link_id, &links_map, IEEE80211_MLD_MAX_NUM_LINKS) {
754 		arvif = rcu_dereference(ahvif->link[link_id]);
755 
756 		if (WARN_ON(!arvif))
757 			continue;
758 
759 		if (!arvif->is_created)
760 			continue;
761 
762 		if (arvif->vdev_id == arvif_iter->vdev_id &&
763 		    arvif->ar == arvif_iter->ar) {
764 			arvif_iter->arvif = arvif;
765 			break;
766 		}
767 	}
768 }
769 
770 struct ath12k_link_vif *ath12k_mac_get_arvif(struct ath12k *ar, u32 vdev_id)
771 {
772 	struct ath12k_vif_iter arvif_iter = {};
773 	u32 flags;
774 
775 	/* To use the arvif returned, caller must have held rcu read lock.
776 	 */
777 	WARN_ON(!rcu_read_lock_any_held());
778 	arvif_iter.vdev_id = vdev_id;
779 	arvif_iter.ar = ar;
780 
781 	flags = IEEE80211_IFACE_ITER_RESUME_ALL;
782 	ieee80211_iterate_active_interfaces_atomic(ath12k_ar_to_hw(ar),
783 						   flags,
784 						   ath12k_get_arvif_iter,
785 						   &arvif_iter);
786 	if (!arvif_iter.arvif) {
787 		ath12k_warn(ar->ab, "No VIF found for vdev %d\n", vdev_id);
788 		return NULL;
789 	}
790 
791 	return arvif_iter.arvif;
792 }
793 
794 struct ath12k_link_vif *ath12k_mac_get_arvif_by_vdev_id(struct ath12k_base *ab,
795 							u32 vdev_id)
796 {
797 	int i;
798 	struct ath12k_pdev *pdev;
799 	struct ath12k_link_vif *arvif;
800 
801 	for (i = 0; i < ab->num_radios; i++) {
802 		pdev = rcu_dereference(ab->pdevs_active[i]);
803 		if (pdev && pdev->ar &&
804 		    (pdev->ar->allocated_vdev_map & (1LL << vdev_id))) {
805 			arvif = ath12k_mac_get_arvif(pdev->ar, vdev_id);
806 			if (arvif)
807 				return arvif;
808 		}
809 	}
810 
811 	return NULL;
812 }
813 
814 struct ath12k *ath12k_mac_get_ar_by_vdev_id(struct ath12k_base *ab, u32 vdev_id)
815 {
816 	int i;
817 	struct ath12k_pdev *pdev;
818 
819 	for (i = 0; i < ab->num_radios; i++) {
820 		pdev = rcu_dereference(ab->pdevs_active[i]);
821 		if (pdev && pdev->ar) {
822 			if (pdev->ar->allocated_vdev_map & (1LL << vdev_id))
823 				return pdev->ar;
824 		}
825 	}
826 
827 	return NULL;
828 }
829 
830 struct ath12k *ath12k_mac_get_ar_by_pdev_id(struct ath12k_base *ab, u32 pdev_id)
831 {
832 	int i;
833 	struct ath12k_pdev *pdev;
834 
835 	if (ab->hw_params->single_pdev_only) {
836 		pdev = rcu_dereference(ab->pdevs_active[0]);
837 		return pdev ? pdev->ar : NULL;
838 	}
839 
840 	if (WARN_ON(pdev_id > ab->num_radios))
841 		return NULL;
842 
843 	for (i = 0; i < ab->num_radios; i++) {
844 		if (ab->fw_mode == ATH12K_FIRMWARE_MODE_FTM)
845 			pdev = &ab->pdevs[i];
846 		else
847 			pdev = rcu_dereference(ab->pdevs_active[i]);
848 
849 		if (pdev && pdev->pdev_id == pdev_id)
850 			return (pdev->ar ? pdev->ar : NULL);
851 	}
852 
853 	return NULL;
854 }
855 
856 static bool ath12k_mac_is_ml_arvif(struct ath12k_link_vif *arvif)
857 {
858 	struct ath12k_vif *ahvif = arvif->ahvif;
859 
860 	lockdep_assert_wiphy(ahvif->ah->hw->wiphy);
861 
862 	if (ahvif->vif->valid_links & BIT(arvif->link_id))
863 		return true;
864 
865 	return false;
866 }
867 
868 static struct ath12k *ath12k_mac_get_ar_by_chan(struct ieee80211_hw *hw,
869 						struct ieee80211_channel *channel)
870 {
871 	struct ath12k_hw *ah = hw->priv;
872 	struct ath12k *ar;
873 	int i;
874 
875 	ar = ah->radio;
876 
877 	if (ah->num_radio == 1)
878 		return ar;
879 
880 	for_each_ar(ah, ar, i) {
881 		if (channel->center_freq >= KHZ_TO_MHZ(ar->freq_range.start_freq) &&
882 		    channel->center_freq <= KHZ_TO_MHZ(ar->freq_range.end_freq))
883 			return ar;
884 	}
885 	return NULL;
886 }
887 
888 static struct ath12k *ath12k_get_ar_by_ctx(struct ieee80211_hw *hw,
889 					   struct ieee80211_chanctx_conf *ctx)
890 {
891 	if (!ctx)
892 		return NULL;
893 
894 	return ath12k_mac_get_ar_by_chan(hw, ctx->def.chan);
895 }
896 
897 struct ath12k *ath12k_get_ar_by_vif(struct ieee80211_hw *hw,
898 				    struct ieee80211_vif *vif,
899 				    u8 link_id)
900 {
901 	struct ath12k_vif *ahvif = ath12k_vif_to_ahvif(vif);
902 	struct ath12k_hw *ah = ath12k_hw_to_ah(hw);
903 	struct ath12k_link_vif *arvif;
904 
905 	lockdep_assert_wiphy(hw->wiphy);
906 
907 	/* If there is one pdev within ah, then we return
908 	 * ar directly.
909 	 */
910 	if (ah->num_radio == 1)
911 		return ah->radio;
912 
913 	if (!(ahvif->links_map & BIT(link_id)))
914 		return NULL;
915 
916 	arvif = wiphy_dereference(hw->wiphy, ahvif->link[link_id]);
917 	if (arvif && arvif->is_created)
918 		return arvif->ar;
919 
920 	return NULL;
921 }
922 
923 void ath12k_mac_get_any_chanctx_conf_iter(struct ieee80211_hw *hw,
924 					  struct ieee80211_chanctx_conf *conf,
925 					  void *data)
926 {
927 	struct ath12k_mac_get_any_chanctx_conf_arg *arg = data;
928 	struct ath12k *ctx_ar = ath12k_get_ar_by_ctx(hw, conf);
929 
930 	if (ctx_ar == arg->ar)
931 		arg->chanctx_conf = conf;
932 }
933 
934 static struct ath12k_link_vif *ath12k_mac_get_vif_up(struct ath12k *ar)
935 {
936 	struct ath12k_link_vif *arvif;
937 
938 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
939 
940 	list_for_each_entry(arvif, &ar->arvifs, list) {
941 		if (arvif->is_up)
942 			return arvif;
943 	}
944 
945 	return NULL;
946 }
947 
948 static bool ath12k_mac_band_match(enum nl80211_band band1, enum WMI_HOST_WLAN_BAND band2)
949 {
950 	switch (band1) {
951 	case NL80211_BAND_2GHZ:
952 		if (band2 & WMI_HOST_WLAN_2GHZ_CAP)
953 			return true;
954 		break;
955 	case NL80211_BAND_5GHZ:
956 	case NL80211_BAND_6GHZ:
957 		if (band2 & WMI_HOST_WLAN_5GHZ_CAP)
958 			return true;
959 		break;
960 	default:
961 		return false;
962 	}
963 
964 	return false;
965 }
966 
967 static u8 ath12k_mac_get_target_pdev_id_from_vif(struct ath12k_link_vif *arvif)
968 {
969 	struct ath12k *ar = arvif->ar;
970 	struct ath12k_base *ab = ar->ab;
971 	struct ieee80211_vif *vif = arvif->ahvif->vif;
972 	struct cfg80211_chan_def def;
973 	enum nl80211_band band;
974 	u8 pdev_id = ab->fw_pdev[0].pdev_id;
975 	int i;
976 
977 	if (WARN_ON(ath12k_mac_vif_link_chan(vif, arvif->link_id, &def)))
978 		return pdev_id;
979 
980 	band = def.chan->band;
981 
982 	for (i = 0; i < ab->fw_pdev_count; i++) {
983 		if (ath12k_mac_band_match(band, ab->fw_pdev[i].supported_bands))
984 			return ab->fw_pdev[i].pdev_id;
985 	}
986 
987 	return pdev_id;
988 }
989 
990 u8 ath12k_mac_get_target_pdev_id(struct ath12k *ar)
991 {
992 	struct ath12k_link_vif *arvif;
993 	struct ath12k_base *ab = ar->ab;
994 
995 	if (!ab->hw_params->single_pdev_only)
996 		return ar->pdev->pdev_id;
997 
998 	arvif = ath12k_mac_get_vif_up(ar);
999 
1000 	/* fw_pdev array has pdev ids derived from phy capability
1001 	 * service ready event (pdev_and_hw_link_ids).
1002 	 * If no vif is active, return default first index.
1003 	 */
1004 	if (!arvif)
1005 		return ar->ab->fw_pdev[0].pdev_id;
1006 
1007 	/* If active vif is found, return the pdev id matching chandef band */
1008 	return ath12k_mac_get_target_pdev_id_from_vif(arvif);
1009 }
1010 
1011 static void ath12k_pdev_caps_update(struct ath12k *ar)
1012 {
1013 	struct ath12k_base *ab = ar->ab;
1014 
1015 	ar->max_tx_power = ab->target_caps.hw_max_tx_power;
1016 
1017 	/* FIXME: Set min_tx_power to ab->target_caps.hw_min_tx_power.
1018 	 * But since the received value in svcrdy is same as hw_max_tx_power,
1019 	 * we can set ar->min_tx_power to 0 currently until
1020 	 * this is fixed in firmware
1021 	 */
1022 	ar->min_tx_power = 0;
1023 
1024 	ar->txpower_limit_2g = ar->max_tx_power;
1025 	ar->txpower_limit_5g = ar->max_tx_power;
1026 	ar->txpower_scale = WMI_HOST_TP_SCALE_MAX;
1027 }
1028 
1029 static int ath12k_mac_txpower_recalc(struct ath12k *ar)
1030 {
1031 	struct ath12k_pdev *pdev = ar->pdev;
1032 	struct ath12k_link_vif *arvif;
1033 	int ret, txpower = -1;
1034 	u32 param;
1035 
1036 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
1037 
1038 	list_for_each_entry(arvif, &ar->arvifs, list) {
1039 		if (arvif->txpower <= 0)
1040 			continue;
1041 
1042 		if (txpower == -1)
1043 			txpower = arvif->txpower;
1044 		else
1045 			txpower = min(txpower, arvif->txpower);
1046 	}
1047 
1048 	if (txpower == -1)
1049 		return 0;
1050 
1051 	/* txpwr is set as 2 units per dBm in FW*/
1052 	txpower = min_t(u32, max_t(u32, ar->min_tx_power, txpower),
1053 			ar->max_tx_power) * 2;
1054 
1055 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "txpower to set in hw %d\n",
1056 		   txpower / 2);
1057 
1058 	if ((pdev->cap.supported_bands & WMI_HOST_WLAN_2GHZ_CAP) &&
1059 	    ar->txpower_limit_2g != txpower) {
1060 		param = WMI_PDEV_PARAM_TXPOWER_LIMIT2G;
1061 		ret = ath12k_wmi_pdev_set_param(ar, param,
1062 						txpower, ar->pdev->pdev_id);
1063 		if (ret)
1064 			goto fail;
1065 		ar->txpower_limit_2g = txpower;
1066 	}
1067 
1068 	if ((pdev->cap.supported_bands & WMI_HOST_WLAN_5GHZ_CAP) &&
1069 	    ar->txpower_limit_5g != txpower) {
1070 		param = WMI_PDEV_PARAM_TXPOWER_LIMIT5G;
1071 		ret = ath12k_wmi_pdev_set_param(ar, param,
1072 						txpower, ar->pdev->pdev_id);
1073 		if (ret)
1074 			goto fail;
1075 		ar->txpower_limit_5g = txpower;
1076 	}
1077 
1078 	return 0;
1079 
1080 fail:
1081 	ath12k_warn(ar->ab, "failed to recalc txpower limit %d using pdev param %d: %d\n",
1082 		    txpower / 2, param, ret);
1083 	return ret;
1084 }
1085 
1086 static int ath12k_recalc_rtscts_prot(struct ath12k_link_vif *arvif)
1087 {
1088 	struct ath12k *ar = arvif->ar;
1089 	u32 vdev_param, rts_cts;
1090 	int ret;
1091 
1092 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
1093 
1094 	vdev_param = WMI_VDEV_PARAM_ENABLE_RTSCTS;
1095 
1096 	/* Enable RTS/CTS protection for sw retries (when legacy stations
1097 	 * are in BSS) or by default only for second rate series.
1098 	 * TODO: Check if we need to enable CTS 2 Self in any case
1099 	 */
1100 	rts_cts = WMI_USE_RTS_CTS;
1101 
1102 	if (arvif->num_legacy_stations > 0)
1103 		rts_cts |= WMI_RTSCTS_ACROSS_SW_RETRIES << 4;
1104 	else
1105 		rts_cts |= WMI_RTSCTS_FOR_SECOND_RATESERIES << 4;
1106 
1107 	/* Need not send duplicate param value to firmware */
1108 	if (arvif->rtscts_prot_mode == rts_cts)
1109 		return 0;
1110 
1111 	arvif->rtscts_prot_mode = rts_cts;
1112 
1113 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac vdev %d recalc rts/cts prot %d\n",
1114 		   arvif->vdev_id, rts_cts);
1115 
1116 	ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
1117 					    vdev_param, rts_cts);
1118 	if (ret)
1119 		ath12k_warn(ar->ab, "failed to recalculate rts/cts prot for vdev %d: %d\n",
1120 			    arvif->vdev_id, ret);
1121 
1122 	return ret;
1123 }
1124 
1125 static int ath12k_mac_set_kickout(struct ath12k_link_vif *arvif)
1126 {
1127 	struct ath12k *ar = arvif->ar;
1128 	u32 param;
1129 	int ret;
1130 
1131 	ret = ath12k_wmi_pdev_set_param(ar, WMI_PDEV_PARAM_STA_KICKOUT_TH,
1132 					ATH12K_KICKOUT_THRESHOLD,
1133 					ar->pdev->pdev_id);
1134 	if (ret) {
1135 		ath12k_warn(ar->ab, "failed to set kickout threshold on vdev %i: %d\n",
1136 			    arvif->vdev_id, ret);
1137 		return ret;
1138 	}
1139 
1140 	param = WMI_VDEV_PARAM_AP_KEEPALIVE_MIN_IDLE_INACTIVE_TIME_SECS;
1141 	ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id, param,
1142 					    ATH12K_KEEPALIVE_MIN_IDLE);
1143 	if (ret) {
1144 		ath12k_warn(ar->ab, "failed to set keepalive minimum idle time on vdev %i: %d\n",
1145 			    arvif->vdev_id, ret);
1146 		return ret;
1147 	}
1148 
1149 	param = WMI_VDEV_PARAM_AP_KEEPALIVE_MAX_IDLE_INACTIVE_TIME_SECS;
1150 	ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id, param,
1151 					    ATH12K_KEEPALIVE_MAX_IDLE);
1152 	if (ret) {
1153 		ath12k_warn(ar->ab, "failed to set keepalive maximum idle time on vdev %i: %d\n",
1154 			    arvif->vdev_id, ret);
1155 		return ret;
1156 	}
1157 
1158 	param = WMI_VDEV_PARAM_AP_KEEPALIVE_MAX_UNRESPONSIVE_TIME_SECS;
1159 	ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id, param,
1160 					    ATH12K_KEEPALIVE_MAX_UNRESPONSIVE);
1161 	if (ret) {
1162 		ath12k_warn(ar->ab, "failed to set keepalive maximum unresponsive time on vdev %i: %d\n",
1163 			    arvif->vdev_id, ret);
1164 		return ret;
1165 	}
1166 
1167 	return 0;
1168 }
1169 
1170 void ath12k_mac_peer_cleanup_all(struct ath12k *ar)
1171 {
1172 	struct ath12k_peer *peer, *tmp;
1173 	struct ath12k_base *ab = ar->ab;
1174 
1175 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
1176 
1177 	spin_lock_bh(&ab->base_lock);
1178 	list_for_each_entry_safe(peer, tmp, &ab->peers, list) {
1179 		/* Skip Rx TID cleanup for self peer */
1180 		if (peer->sta)
1181 			ath12k_dp_rx_peer_tid_cleanup(ar, peer);
1182 
1183 		list_del(&peer->list);
1184 		kfree(peer);
1185 	}
1186 	spin_unlock_bh(&ab->base_lock);
1187 
1188 	ar->num_peers = 0;
1189 	ar->num_stations = 0;
1190 }
1191 
1192 static int ath12k_mac_vdev_setup_sync(struct ath12k *ar)
1193 {
1194 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
1195 
1196 	if (test_bit(ATH12K_FLAG_CRASH_FLUSH, &ar->ab->dev_flags))
1197 		return -ESHUTDOWN;
1198 
1199 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "vdev setup timeout %d\n",
1200 		   ATH12K_VDEV_SETUP_TIMEOUT_HZ);
1201 
1202 	if (!wait_for_completion_timeout(&ar->vdev_setup_done,
1203 					 ATH12K_VDEV_SETUP_TIMEOUT_HZ))
1204 		return -ETIMEDOUT;
1205 
1206 	return ar->last_wmi_vdev_start_status ? -EINVAL : 0;
1207 }
1208 
1209 static int ath12k_monitor_vdev_up(struct ath12k *ar, int vdev_id)
1210 {
1211 	struct ath12k_wmi_vdev_up_params params = {};
1212 	int ret;
1213 
1214 	params.vdev_id = vdev_id;
1215 	params.bssid = ar->mac_addr;
1216 	ret = ath12k_wmi_vdev_up(ar, &params);
1217 	if (ret) {
1218 		ath12k_warn(ar->ab, "failed to put up monitor vdev %i: %d\n",
1219 			    vdev_id, ret);
1220 		return ret;
1221 	}
1222 
1223 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac monitor vdev %i started\n",
1224 		   vdev_id);
1225 	return 0;
1226 }
1227 
1228 static int ath12k_mac_monitor_vdev_start(struct ath12k *ar, int vdev_id,
1229 					 struct cfg80211_chan_def *chandef)
1230 {
1231 	struct ieee80211_channel *channel;
1232 	struct wmi_vdev_start_req_arg arg = {};
1233 	struct ath12k_wmi_vdev_up_params params = {};
1234 	int ret;
1235 
1236 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
1237 
1238 	channel = chandef->chan;
1239 	arg.vdev_id = vdev_id;
1240 	arg.freq = channel->center_freq;
1241 	arg.band_center_freq1 = chandef->center_freq1;
1242 	arg.band_center_freq2 = chandef->center_freq2;
1243 	arg.mode = ath12k_phymodes[chandef->chan->band][chandef->width];
1244 	arg.chan_radar = !!(channel->flags & IEEE80211_CHAN_RADAR);
1245 
1246 	arg.min_power = 0;
1247 	arg.max_power = channel->max_power;
1248 	arg.max_reg_power = channel->max_reg_power;
1249 	arg.max_antenna_gain = channel->max_antenna_gain;
1250 
1251 	arg.pref_tx_streams = ar->num_tx_chains;
1252 	arg.pref_rx_streams = ar->num_rx_chains;
1253 	arg.punct_bitmap = 0xFFFFFFFF;
1254 
1255 	arg.passive |= !!(chandef->chan->flags & IEEE80211_CHAN_NO_IR);
1256 
1257 	reinit_completion(&ar->vdev_setup_done);
1258 	reinit_completion(&ar->vdev_delete_done);
1259 
1260 	ret = ath12k_wmi_vdev_start(ar, &arg, false);
1261 	if (ret) {
1262 		ath12k_warn(ar->ab, "failed to request monitor vdev %i start: %d\n",
1263 			    vdev_id, ret);
1264 		return ret;
1265 	}
1266 
1267 	ret = ath12k_mac_vdev_setup_sync(ar);
1268 	if (ret) {
1269 		ath12k_warn(ar->ab, "failed to synchronize setup for monitor vdev %i start: %d\n",
1270 			    vdev_id, ret);
1271 		return ret;
1272 	}
1273 
1274 	params.vdev_id = vdev_id;
1275 	params.bssid = ar->mac_addr;
1276 	ret = ath12k_wmi_vdev_up(ar, &params);
1277 	if (ret) {
1278 		ath12k_warn(ar->ab, "failed to put up monitor vdev %i: %d\n",
1279 			    vdev_id, ret);
1280 		goto vdev_stop;
1281 	}
1282 
1283 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac monitor vdev %i started\n",
1284 		   vdev_id);
1285 	return 0;
1286 
1287 vdev_stop:
1288 	ret = ath12k_wmi_vdev_stop(ar, vdev_id);
1289 	if (ret)
1290 		ath12k_warn(ar->ab, "failed to stop monitor vdev %i after start failure: %d\n",
1291 			    vdev_id, ret);
1292 	return ret;
1293 }
1294 
1295 static int ath12k_mac_monitor_vdev_stop(struct ath12k *ar)
1296 {
1297 	int ret;
1298 
1299 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
1300 
1301 	reinit_completion(&ar->vdev_setup_done);
1302 
1303 	ret = ath12k_wmi_vdev_stop(ar, ar->monitor_vdev_id);
1304 	if (ret)
1305 		ath12k_warn(ar->ab, "failed to request monitor vdev %i stop: %d\n",
1306 			    ar->monitor_vdev_id, ret);
1307 
1308 	ret = ath12k_mac_vdev_setup_sync(ar);
1309 	if (ret)
1310 		ath12k_warn(ar->ab, "failed to synchronize monitor vdev %i stop: %d\n",
1311 			    ar->monitor_vdev_id, ret);
1312 
1313 	ret = ath12k_wmi_vdev_down(ar, ar->monitor_vdev_id);
1314 	if (ret)
1315 		ath12k_warn(ar->ab, "failed to put down monitor vdev %i: %d\n",
1316 			    ar->monitor_vdev_id, ret);
1317 
1318 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac monitor vdev %i stopped\n",
1319 		   ar->monitor_vdev_id);
1320 	return ret;
1321 }
1322 
1323 static int ath12k_mac_monitor_vdev_delete(struct ath12k *ar)
1324 {
1325 	int ret;
1326 	unsigned long time_left;
1327 
1328 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
1329 
1330 	if (!ar->monitor_vdev_created)
1331 		return 0;
1332 
1333 	reinit_completion(&ar->vdev_delete_done);
1334 
1335 	ret = ath12k_wmi_vdev_delete(ar, ar->monitor_vdev_id);
1336 	if (ret) {
1337 		ath12k_warn(ar->ab, "failed to request wmi monitor vdev %i removal: %d\n",
1338 			    ar->monitor_vdev_id, ret);
1339 		return ret;
1340 	}
1341 
1342 	time_left = wait_for_completion_timeout(&ar->vdev_delete_done,
1343 						ATH12K_VDEV_DELETE_TIMEOUT_HZ);
1344 	if (time_left == 0) {
1345 		ath12k_warn(ar->ab, "Timeout in receiving vdev delete response\n");
1346 	} else {
1347 		ar->allocated_vdev_map &= ~(1LL << ar->monitor_vdev_id);
1348 		ar->ab->free_vdev_map |= 1LL << (ar->monitor_vdev_id);
1349 		ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac monitor vdev %d deleted\n",
1350 			   ar->monitor_vdev_id);
1351 		ar->num_created_vdevs--;
1352 		ar->monitor_vdev_id = -1;
1353 		ar->monitor_vdev_created = false;
1354 	}
1355 
1356 	return ret;
1357 }
1358 
1359 static int ath12k_mac_monitor_start(struct ath12k *ar)
1360 {
1361 	struct ath12k_mac_get_any_chanctx_conf_arg arg;
1362 	int ret;
1363 
1364 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
1365 
1366 	if (ar->monitor_started)
1367 		return 0;
1368 
1369 	arg.ar = ar;
1370 	arg.chanctx_conf = NULL;
1371 	ieee80211_iter_chan_contexts_atomic(ath12k_ar_to_hw(ar),
1372 					    ath12k_mac_get_any_chanctx_conf_iter,
1373 					    &arg);
1374 	if (!arg.chanctx_conf)
1375 		return 0;
1376 
1377 	ret = ath12k_mac_monitor_vdev_start(ar, ar->monitor_vdev_id,
1378 					    &arg.chanctx_conf->def);
1379 	if (ret) {
1380 		ath12k_warn(ar->ab, "failed to start monitor vdev: %d\n", ret);
1381 		return ret;
1382 	}
1383 
1384 	ret = ath12k_dp_tx_htt_monitor_mode_ring_config(ar, false);
1385 	if (ret) {
1386 		ath12k_warn(ar->ab, "fail to set monitor filter: %d\n", ret);
1387 		return ret;
1388 	}
1389 
1390 	ar->monitor_started = true;
1391 	ar->num_started_vdevs++;
1392 
1393 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac monitor started\n");
1394 
1395 	return 0;
1396 }
1397 
1398 static int ath12k_mac_monitor_stop(struct ath12k *ar)
1399 {
1400 	int ret;
1401 
1402 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
1403 
1404 	if (!ar->monitor_started)
1405 		return 0;
1406 
1407 	ret = ath12k_mac_monitor_vdev_stop(ar);
1408 	if (ret) {
1409 		ath12k_warn(ar->ab, "failed to stop monitor vdev: %d\n", ret);
1410 		return ret;
1411 	}
1412 
1413 	ar->monitor_started = false;
1414 	ar->num_started_vdevs--;
1415 	ret = ath12k_dp_tx_htt_monitor_mode_ring_config(ar, true);
1416 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac monitor stopped ret %d\n", ret);
1417 	return ret;
1418 }
1419 
1420 int ath12k_mac_vdev_stop(struct ath12k_link_vif *arvif)
1421 {
1422 	struct ath12k_vif *ahvif = arvif->ahvif;
1423 	struct ath12k *ar = arvif->ar;
1424 	int ret;
1425 
1426 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
1427 
1428 	reinit_completion(&ar->vdev_setup_done);
1429 
1430 	ret = ath12k_wmi_vdev_stop(ar, arvif->vdev_id);
1431 	if (ret) {
1432 		ath12k_warn(ar->ab, "failed to stop WMI vdev %i: %d\n",
1433 			    arvif->vdev_id, ret);
1434 		goto err;
1435 	}
1436 
1437 	ret = ath12k_mac_vdev_setup_sync(ar);
1438 	if (ret) {
1439 		ath12k_warn(ar->ab, "failed to synchronize setup for vdev %i: %d\n",
1440 			    arvif->vdev_id, ret);
1441 		goto err;
1442 	}
1443 
1444 	WARN_ON(ar->num_started_vdevs == 0);
1445 
1446 	ar->num_started_vdevs--;
1447 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "vdev %pM stopped, vdev_id %d\n",
1448 		   ahvif->vif->addr, arvif->vdev_id);
1449 
1450 	if (test_bit(ATH12K_FLAG_CAC_RUNNING, &ar->dev_flags)) {
1451 		clear_bit(ATH12K_FLAG_CAC_RUNNING, &ar->dev_flags);
1452 		ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "CAC Stopped for vdev %d\n",
1453 			   arvif->vdev_id);
1454 	}
1455 
1456 	return 0;
1457 err:
1458 	return ret;
1459 }
1460 
1461 static int ath12k_mac_op_config(struct ieee80211_hw *hw, int radio_idx, u32 changed)
1462 {
1463 	return 0;
1464 }
1465 
1466 static int ath12k_mac_setup_bcn_p2p_ie(struct ath12k_link_vif *arvif,
1467 				       struct sk_buff *bcn)
1468 {
1469 	struct ath12k *ar = arvif->ar;
1470 	struct ieee80211_mgmt *mgmt;
1471 	const u8 *p2p_ie;
1472 	int ret;
1473 
1474 	mgmt = (void *)bcn->data;
1475 	p2p_ie = cfg80211_find_vendor_ie(WLAN_OUI_WFA, WLAN_OUI_TYPE_WFA_P2P,
1476 					 mgmt->u.beacon.variable,
1477 					 bcn->len - (mgmt->u.beacon.variable -
1478 						     bcn->data));
1479 	if (!p2p_ie) {
1480 		ath12k_warn(ar->ab, "no P2P ie found in beacon\n");
1481 		return -ENOENT;
1482 	}
1483 
1484 	ret = ath12k_wmi_p2p_go_bcn_ie(ar, arvif->vdev_id, p2p_ie);
1485 	if (ret) {
1486 		ath12k_warn(ar->ab, "failed to submit P2P GO bcn ie for vdev %i: %d\n",
1487 			    arvif->vdev_id, ret);
1488 		return ret;
1489 	}
1490 
1491 	return 0;
1492 }
1493 
1494 static int ath12k_mac_remove_vendor_ie(struct sk_buff *skb, unsigned int oui,
1495 				       u8 oui_type, size_t ie_offset)
1496 {
1497 	const u8 *next, *end;
1498 	size_t len;
1499 	u8 *ie;
1500 
1501 	if (WARN_ON(skb->len < ie_offset))
1502 		return -EINVAL;
1503 
1504 	ie = (u8 *)cfg80211_find_vendor_ie(oui, oui_type,
1505 					   skb->data + ie_offset,
1506 					   skb->len - ie_offset);
1507 	if (!ie)
1508 		return -ENOENT;
1509 
1510 	len = ie[1] + 2;
1511 	end = skb->data + skb->len;
1512 	next = ie + len;
1513 
1514 	if (WARN_ON(next > end))
1515 		return -EINVAL;
1516 
1517 	memmove(ie, next, end - next);
1518 	skb_trim(skb, skb->len - len);
1519 
1520 	return 0;
1521 }
1522 
1523 static void ath12k_mac_set_arvif_ies(struct ath12k_link_vif *arvif,
1524 				     struct ath12k_link_vif *tx_arvif,
1525 				     struct sk_buff *bcn,
1526 				     u8 bssid_index, bool *nontx_profile_found)
1527 {
1528 	struct ieee80211_mgmt *mgmt = (struct ieee80211_mgmt *)bcn->data;
1529 	const struct element *elem, *nontx, *index, *nie, *ext_cap_ie;
1530 	const u8 *start, *tail;
1531 	u16 rem_len;
1532 	u8 i;
1533 
1534 	start = bcn->data + ieee80211_get_hdrlen_from_skb(bcn) + sizeof(mgmt->u.beacon);
1535 	tail = skb_tail_pointer(bcn);
1536 	rem_len = tail - start;
1537 
1538 	arvif->rsnie_present = false;
1539 	arvif->wpaie_present = false;
1540 
1541 	if (cfg80211_find_ie(WLAN_EID_RSN, start, rem_len))
1542 		arvif->rsnie_present = true;
1543 	if (cfg80211_find_vendor_ie(WLAN_OUI_MICROSOFT, WLAN_OUI_TYPE_MICROSOFT_WPA,
1544 				    start, rem_len))
1545 		arvif->wpaie_present = true;
1546 
1547 	ext_cap_ie = cfg80211_find_elem(WLAN_EID_EXT_CAPABILITY, start, rem_len);
1548 	if (ext_cap_ie && ext_cap_ie->datalen >= 11 &&
1549 	    (ext_cap_ie->data[10] & WLAN_EXT_CAPA11_BCN_PROTECT))
1550 		tx_arvif->beacon_prot = true;
1551 
1552 	/* Return from here for the transmitted profile */
1553 	if (!bssid_index)
1554 		return;
1555 
1556 	/* Initial rsnie_present for the nontransmitted profile is set to be same as that
1557 	 * of the transmitted profile. It will be changed if security configurations are
1558 	 * different.
1559 	 */
1560 	*nontx_profile_found = false;
1561 	for_each_element_id(elem, WLAN_EID_MULTIPLE_BSSID, start, rem_len) {
1562 		/* Fixed minimum MBSSID element length with at least one
1563 		 * nontransmitted BSSID profile is 12 bytes as given below;
1564 		 * 1 (max BSSID indicator) +
1565 		 * 2 (Nontransmitted BSSID profile: Subelement ID + length) +
1566 		 * 4 (Nontransmitted BSSID Capabilities: tag + length + info)
1567 		 * 2 (Nontransmitted BSSID SSID: tag + length)
1568 		 * 3 (Nontransmitted BSSID Index: tag + length + BSSID index
1569 		 */
1570 		if (elem->datalen < 12 || elem->data[0] < 1)
1571 			continue; /* Max BSSID indicator must be >=1 */
1572 
1573 		for_each_element(nontx, elem->data + 1, elem->datalen - 1) {
1574 			start = nontx->data;
1575 
1576 			if (nontx->id != 0 || nontx->datalen < 4)
1577 				continue; /* Invalid nontransmitted profile */
1578 
1579 			if (nontx->data[0] != WLAN_EID_NON_TX_BSSID_CAP ||
1580 			    nontx->data[1] != 2) {
1581 				continue; /* Missing nontransmitted BSS capabilities */
1582 			}
1583 
1584 			if (nontx->data[4] != WLAN_EID_SSID)
1585 				continue; /* Missing SSID for nontransmitted BSS */
1586 
1587 			index = cfg80211_find_elem(WLAN_EID_MULTI_BSSID_IDX,
1588 						   start, nontx->datalen);
1589 			if (!index || index->datalen < 1 || index->data[0] == 0)
1590 				continue; /* Invalid MBSSID Index element */
1591 
1592 			if (index->data[0] == bssid_index) {
1593 				*nontx_profile_found = true;
1594 
1595 				/* Check if nontx BSS has beacon protection enabled */
1596 				if (!tx_arvif->beacon_prot) {
1597 					ext_cap_ie =
1598 					    cfg80211_find_elem(WLAN_EID_EXT_CAPABILITY,
1599 							       nontx->data,
1600 							       nontx->datalen);
1601 					if (ext_cap_ie && ext_cap_ie->datalen >= 11 &&
1602 					    (ext_cap_ie->data[10] &
1603 					     WLAN_EXT_CAPA11_BCN_PROTECT))
1604 						tx_arvif->beacon_prot = true;
1605 				}
1606 
1607 				if (cfg80211_find_ie(WLAN_EID_RSN,
1608 						     nontx->data,
1609 						     nontx->datalen)) {
1610 					arvif->rsnie_present = true;
1611 					return;
1612 				} else if (!arvif->rsnie_present) {
1613 					return; /* Both tx and nontx BSS are open */
1614 				}
1615 
1616 				nie = cfg80211_find_ext_elem(WLAN_EID_EXT_NON_INHERITANCE,
1617 							     nontx->data,
1618 							     nontx->datalen);
1619 				if (!nie || nie->datalen < 2)
1620 					return; /* Invalid non-inheritance element */
1621 
1622 				for (i = 1; i < nie->datalen - 1; i++) {
1623 					if (nie->data[i] == WLAN_EID_RSN) {
1624 						arvif->rsnie_present = false;
1625 						break;
1626 					}
1627 				}
1628 
1629 				return;
1630 			}
1631 		}
1632 	}
1633 }
1634 
1635 static int ath12k_mac_setup_bcn_tmpl_ema(struct ath12k_link_vif *arvif,
1636 					 struct ath12k_link_vif *tx_arvif,
1637 					 u8 bssid_index)
1638 {
1639 	struct ath12k_wmi_bcn_tmpl_ema_arg ema_args;
1640 	struct ieee80211_ema_beacons *beacons;
1641 	bool nontx_profile_found = false;
1642 	int ret = 0;
1643 	u8 i;
1644 
1645 	beacons = ieee80211_beacon_get_template_ema_list(ath12k_ar_to_hw(tx_arvif->ar),
1646 							 tx_arvif->ahvif->vif,
1647 							 tx_arvif->link_id);
1648 	if (!beacons || !beacons->cnt) {
1649 		ath12k_warn(arvif->ar->ab,
1650 			    "failed to get ema beacon templates from mac80211\n");
1651 		return -EPERM;
1652 	}
1653 
1654 	if (tx_arvif == arvif)
1655 		ath12k_mac_set_arvif_ies(arvif, tx_arvif, beacons->bcn[0].skb, 0, NULL);
1656 
1657 	for (i = 0; i < beacons->cnt; i++) {
1658 		if (tx_arvif != arvif && !nontx_profile_found)
1659 			ath12k_mac_set_arvif_ies(arvif, tx_arvif, beacons->bcn[i].skb,
1660 						 bssid_index,
1661 						 &nontx_profile_found);
1662 
1663 		ema_args.bcn_cnt = beacons->cnt;
1664 		ema_args.bcn_index = i;
1665 		ret = ath12k_wmi_bcn_tmpl(tx_arvif, &beacons->bcn[i].offs,
1666 					  beacons->bcn[i].skb, &ema_args);
1667 		if (ret) {
1668 			ath12k_warn(tx_arvif->ar->ab,
1669 				    "failed to set ema beacon template id %i error %d\n",
1670 				    i, ret);
1671 			break;
1672 		}
1673 	}
1674 
1675 	if (tx_arvif != arvif && !nontx_profile_found)
1676 		ath12k_warn(arvif->ar->ab,
1677 			    "nontransmitted bssid index %u not found in beacon template\n",
1678 			    bssid_index);
1679 
1680 	ieee80211_beacon_free_ema_list(beacons);
1681 	return ret;
1682 }
1683 
1684 static int ath12k_mac_setup_bcn_tmpl(struct ath12k_link_vif *arvif)
1685 {
1686 	struct ath12k_vif *ahvif = arvif->ahvif;
1687 	struct ieee80211_vif *vif = ath12k_ahvif_to_vif(ahvif);
1688 	struct ieee80211_bss_conf *link_conf;
1689 	struct ath12k_link_vif *tx_arvif;
1690 	struct ath12k *ar = arvif->ar;
1691 	struct ath12k_base *ab = ar->ab;
1692 	struct ieee80211_mutable_offsets offs = {};
1693 	bool nontx_profile_found = false;
1694 	struct sk_buff *bcn;
1695 	int ret;
1696 
1697 	if (ahvif->vdev_type != WMI_VDEV_TYPE_AP)
1698 		return 0;
1699 
1700 	link_conf = ath12k_mac_get_link_bss_conf(arvif);
1701 	if (!link_conf) {
1702 		ath12k_warn(ar->ab, "unable to access bss link conf to set bcn tmpl for vif %pM link %u\n",
1703 			    vif->addr, arvif->link_id);
1704 		return -ENOLINK;
1705 	}
1706 
1707 	tx_arvif = ath12k_mac_get_tx_arvif(arvif, link_conf);
1708 	if (tx_arvif) {
1709 		if (tx_arvif != arvif && arvif->is_up)
1710 			return 0;
1711 
1712 		if (link_conf->ema_ap)
1713 			return ath12k_mac_setup_bcn_tmpl_ema(arvif, tx_arvif,
1714 							     link_conf->bssid_index);
1715 	} else {
1716 		tx_arvif = arvif;
1717 	}
1718 
1719 	bcn = ieee80211_beacon_get_template(ath12k_ar_to_hw(tx_arvif->ar),
1720 					    tx_arvif->ahvif->vif,
1721 					    &offs, tx_arvif->link_id);
1722 	if (!bcn) {
1723 		ath12k_warn(ab, "failed to get beacon template from mac80211\n");
1724 		return -EPERM;
1725 	}
1726 
1727 	if (tx_arvif == arvif) {
1728 		ath12k_mac_set_arvif_ies(arvif, tx_arvif, bcn, 0, NULL);
1729 	} else {
1730 		ath12k_mac_set_arvif_ies(arvif, tx_arvif, bcn,
1731 					 link_conf->bssid_index,
1732 					 &nontx_profile_found);
1733 		if (!nontx_profile_found)
1734 			ath12k_warn(ab,
1735 				    "nontransmitted profile not found in beacon template\n");
1736 	}
1737 
1738 	if (ahvif->vif->type == NL80211_IFTYPE_AP && ahvif->vif->p2p) {
1739 		ret = ath12k_mac_setup_bcn_p2p_ie(arvif, bcn);
1740 		if (ret) {
1741 			ath12k_warn(ab, "failed to setup P2P GO bcn ie: %d\n",
1742 				    ret);
1743 			goto free_bcn_skb;
1744 		}
1745 
1746 		/* P2P IE is inserted by firmware automatically (as
1747 		 * configured above) so remove it from the base beacon
1748 		 * template to avoid duplicate P2P IEs in beacon frames.
1749 		 */
1750 		ret = ath12k_mac_remove_vendor_ie(bcn, WLAN_OUI_WFA,
1751 						  WLAN_OUI_TYPE_WFA_P2P,
1752 						  offsetof(struct ieee80211_mgmt,
1753 							   u.beacon.variable));
1754 		if (ret) {
1755 			ath12k_warn(ab, "failed to remove P2P vendor ie: %d\n",
1756 				    ret);
1757 			goto free_bcn_skb;
1758 		}
1759 	}
1760 
1761 	ret = ath12k_wmi_bcn_tmpl(arvif, &offs, bcn, NULL);
1762 
1763 	if (ret)
1764 		ath12k_warn(ab, "failed to submit beacon template command: %d\n",
1765 			    ret);
1766 
1767 free_bcn_skb:
1768 	kfree_skb(bcn);
1769 	return ret;
1770 }
1771 
1772 static void ath12k_control_beaconing(struct ath12k_link_vif *arvif,
1773 				     struct ieee80211_bss_conf *info)
1774 {
1775 	struct ath12k_wmi_vdev_up_params params = {};
1776 	struct ath12k_vif *ahvif = arvif->ahvif;
1777 	struct ath12k *ar = arvif->ar;
1778 	int ret;
1779 
1780 	lockdep_assert_wiphy(ath12k_ar_to_hw(arvif->ar)->wiphy);
1781 
1782 	if (!info->enable_beacon) {
1783 		ret = ath12k_wmi_vdev_down(ar, arvif->vdev_id);
1784 		if (ret)
1785 			ath12k_warn(ar->ab, "failed to down vdev_id %i: %d\n",
1786 				    arvif->vdev_id, ret);
1787 
1788 		arvif->is_up = false;
1789 		return;
1790 	}
1791 
1792 	/* Install the beacon template to the FW */
1793 	ret = ath12k_mac_setup_bcn_tmpl(arvif);
1794 	if (ret) {
1795 		ath12k_warn(ar->ab, "failed to update bcn tmpl during vdev up: %d\n",
1796 			    ret);
1797 		return;
1798 	}
1799 
1800 	ahvif->aid = 0;
1801 
1802 	ether_addr_copy(arvif->bssid, info->addr);
1803 
1804 	params.vdev_id = arvif->vdev_id;
1805 	params.aid = ahvif->aid;
1806 	params.bssid = arvif->bssid;
1807 	params.tx_bssid = ath12k_mac_get_tx_bssid(arvif);
1808 	if (params.tx_bssid) {
1809 		params.nontx_profile_idx = info->bssid_index;
1810 		params.nontx_profile_cnt = 1 << info->bssid_indicator;
1811 	}
1812 	ret = ath12k_wmi_vdev_up(arvif->ar, &params);
1813 	if (ret) {
1814 		ath12k_warn(ar->ab, "failed to bring up vdev %d: %i\n",
1815 			    arvif->vdev_id, ret);
1816 		return;
1817 	}
1818 
1819 	arvif->is_up = true;
1820 
1821 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac vdev %d up\n", arvif->vdev_id);
1822 }
1823 
1824 static void ath12k_mac_handle_beacon_iter(void *data, u8 *mac,
1825 					  struct ieee80211_vif *vif)
1826 {
1827 	struct sk_buff *skb = data;
1828 	struct ieee80211_mgmt *mgmt = (void *)skb->data;
1829 	struct ath12k_vif *ahvif = ath12k_vif_to_ahvif(vif);
1830 	struct ath12k_link_vif *arvif = &ahvif->deflink;
1831 
1832 	if (vif->type != NL80211_IFTYPE_STATION || !arvif->is_created)
1833 		return;
1834 
1835 	if (!ether_addr_equal(mgmt->bssid, vif->bss_conf.bssid))
1836 		return;
1837 
1838 	cancel_delayed_work(&arvif->connection_loss_work);
1839 }
1840 
1841 void ath12k_mac_handle_beacon(struct ath12k *ar, struct sk_buff *skb)
1842 {
1843 	ieee80211_iterate_active_interfaces_atomic(ath12k_ar_to_hw(ar),
1844 						   IEEE80211_IFACE_ITER_NORMAL,
1845 						   ath12k_mac_handle_beacon_iter,
1846 						   skb);
1847 }
1848 
1849 void ath12k_mac_handle_beacon_miss(struct ath12k *ar,
1850 				   struct ath12k_link_vif *arvif)
1851 {
1852 	struct ieee80211_hw *hw = ath12k_ar_to_hw(ar);
1853 	struct ieee80211_vif *vif = ath12k_ahvif_to_vif(arvif->ahvif);
1854 
1855 	if (!(arvif->is_created && arvif->is_up))
1856 		return;
1857 
1858 	ieee80211_beacon_loss(vif);
1859 
1860 	/* Firmware doesn't report beacon loss events repeatedly. If AP probe
1861 	 * (done by mac80211) succeeds but beacons do not resume then it
1862 	 * doesn't make sense to continue operation. Queue connection loss work
1863 	 * which can be cancelled when beacon is received.
1864 	 */
1865 	ieee80211_queue_delayed_work(hw, &arvif->connection_loss_work,
1866 				     ATH12K_CONNECTION_LOSS_HZ);
1867 }
1868 
1869 static void ath12k_mac_vif_sta_connection_loss_work(struct work_struct *work)
1870 {
1871 	struct ath12k_link_vif *arvif = container_of(work, struct ath12k_link_vif,
1872 						     connection_loss_work.work);
1873 	struct ieee80211_vif *vif = arvif->ahvif->vif;
1874 
1875 	if (!arvif->is_up)
1876 		return;
1877 
1878 	ieee80211_connection_loss(vif);
1879 }
1880 
1881 static void ath12k_peer_assoc_h_basic(struct ath12k *ar,
1882 				      struct ath12k_link_vif *arvif,
1883 				      struct ath12k_link_sta *arsta,
1884 				      struct ath12k_wmi_peer_assoc_arg *arg)
1885 {
1886 	struct ieee80211_vif *vif = ath12k_ahvif_to_vif(arvif->ahvif);
1887 	struct ieee80211_sta *sta = ath12k_ahsta_to_sta(arsta->ahsta);
1888 	struct ieee80211_hw *hw = ath12k_ar_to_hw(ar);
1889 	struct ieee80211_bss_conf *bss_conf;
1890 	u32 aid;
1891 
1892 	lockdep_assert_wiphy(hw->wiphy);
1893 
1894 	if (vif->type == NL80211_IFTYPE_STATION)
1895 		aid = vif->cfg.aid;
1896 	else
1897 		aid = sta->aid;
1898 
1899 	ether_addr_copy(arg->peer_mac, arsta->addr);
1900 	arg->vdev_id = arvif->vdev_id;
1901 	arg->peer_associd = aid;
1902 	arg->auth_flag = true;
1903 	/* TODO: STA WAR in ath10k for listen interval required? */
1904 	arg->peer_listen_intval = hw->conf.listen_interval;
1905 	arg->peer_nss = 1;
1906 
1907 	bss_conf = ath12k_mac_get_link_bss_conf(arvif);
1908 	if (!bss_conf) {
1909 		ath12k_warn(ar->ab, "unable to access bss link conf in peer assoc for vif %pM link %u\n",
1910 			    vif->addr, arvif->link_id);
1911 		return;
1912 	}
1913 
1914 	arg->peer_caps = bss_conf->assoc_capability;
1915 }
1916 
1917 static void ath12k_peer_assoc_h_crypto(struct ath12k *ar,
1918 				       struct ath12k_link_vif *arvif,
1919 				       struct ath12k_link_sta *arsta,
1920 				       struct ath12k_wmi_peer_assoc_arg *arg)
1921 {
1922 	struct ieee80211_vif *vif = ath12k_ahvif_to_vif(arvif->ahvif);
1923 	struct ieee80211_sta *sta = ath12k_ahsta_to_sta(arsta->ahsta);
1924 	struct ieee80211_bss_conf *info;
1925 	struct cfg80211_chan_def def;
1926 	struct cfg80211_bss *bss;
1927 	struct ieee80211_hw *hw = ath12k_ar_to_hw(ar);
1928 	const u8 *rsnie = NULL;
1929 	const u8 *wpaie = NULL;
1930 
1931 	lockdep_assert_wiphy(hw->wiphy);
1932 
1933 	info = ath12k_mac_get_link_bss_conf(arvif);
1934 	if (!info) {
1935 		ath12k_warn(ar->ab, "unable to access bss link conf for peer assoc crypto for vif %pM link %u\n",
1936 			    vif->addr, arvif->link_id);
1937 		return;
1938 	}
1939 
1940 	if (WARN_ON(ath12k_mac_vif_link_chan(vif, arvif->link_id, &def)))
1941 		return;
1942 
1943 	bss = cfg80211_get_bss(hw->wiphy, def.chan, info->bssid, NULL, 0,
1944 			       IEEE80211_BSS_TYPE_ANY, IEEE80211_PRIVACY_ANY);
1945 
1946 	if (arvif->rsnie_present || arvif->wpaie_present) {
1947 		arg->need_ptk_4_way = true;
1948 		if (arvif->wpaie_present)
1949 			arg->need_gtk_2_way = true;
1950 	} else if (bss) {
1951 		const struct cfg80211_bss_ies *ies;
1952 
1953 		rcu_read_lock();
1954 		rsnie = ieee80211_bss_get_ie(bss, WLAN_EID_RSN);
1955 
1956 		ies = rcu_dereference(bss->ies);
1957 
1958 		wpaie = cfg80211_find_vendor_ie(WLAN_OUI_MICROSOFT,
1959 						WLAN_OUI_TYPE_MICROSOFT_WPA,
1960 						ies->data,
1961 						ies->len);
1962 		rcu_read_unlock();
1963 		cfg80211_put_bss(hw->wiphy, bss);
1964 	}
1965 
1966 	/* FIXME: base on RSN IE/WPA IE is a correct idea? */
1967 	if (rsnie || wpaie) {
1968 		ath12k_dbg(ar->ab, ATH12K_DBG_WMI,
1969 			   "%s: rsn ie found\n", __func__);
1970 		arg->need_ptk_4_way = true;
1971 	}
1972 
1973 	if (wpaie) {
1974 		ath12k_dbg(ar->ab, ATH12K_DBG_WMI,
1975 			   "%s: wpa ie found\n", __func__);
1976 		arg->need_gtk_2_way = true;
1977 	}
1978 
1979 	if (sta->mfp) {
1980 		/* TODO: Need to check if FW supports PMF? */
1981 		arg->is_pmf_enabled = true;
1982 	}
1983 
1984 	/* TODO: safe_mode_enabled (bypass 4-way handshake) flag req? */
1985 }
1986 
1987 static void ath12k_peer_assoc_h_rates(struct ath12k *ar,
1988 				      struct ath12k_link_vif *arvif,
1989 				      struct ath12k_link_sta *arsta,
1990 				      struct ath12k_wmi_peer_assoc_arg *arg)
1991 {
1992 	struct ieee80211_vif *vif = ath12k_ahvif_to_vif(arvif->ahvif);
1993 	struct ieee80211_sta *sta = ath12k_ahsta_to_sta(arsta->ahsta);
1994 	struct wmi_rate_set_arg *rateset = &arg->peer_legacy_rates;
1995 	struct ieee80211_link_sta *link_sta;
1996 	struct cfg80211_chan_def def;
1997 	const struct ieee80211_supported_band *sband;
1998 	const struct ieee80211_rate *rates;
1999 	struct ieee80211_hw *hw = ath12k_ar_to_hw(ar);
2000 	enum nl80211_band band;
2001 	u32 ratemask;
2002 	u8 rate;
2003 	int i;
2004 
2005 	lockdep_assert_wiphy(hw->wiphy);
2006 
2007 	if (WARN_ON(ath12k_mac_vif_link_chan(vif, arvif->link_id, &def)))
2008 		return;
2009 
2010 	link_sta = ath12k_mac_get_link_sta(arsta);
2011 	if (!link_sta) {
2012 		ath12k_warn(ar->ab, "unable to access link sta in peer assoc rates for sta %pM link %u\n",
2013 			    sta->addr, arsta->link_id);
2014 		return;
2015 	}
2016 
2017 	band = def.chan->band;
2018 	sband = hw->wiphy->bands[band];
2019 	ratemask = link_sta->supp_rates[band];
2020 	ratemask &= arvif->bitrate_mask.control[band].legacy;
2021 	rates = sband->bitrates;
2022 
2023 	rateset->num_rates = 0;
2024 
2025 	for (i = 0; i < 32; i++, ratemask >>= 1, rates++) {
2026 		if (!(ratemask & 1))
2027 			continue;
2028 
2029 		rate = ath12k_mac_bitrate_to_rate(rates->bitrate);
2030 		rateset->rates[rateset->num_rates] = rate;
2031 		rateset->num_rates++;
2032 	}
2033 }
2034 
2035 static bool
2036 ath12k_peer_assoc_h_ht_masked(const u8 *ht_mcs_mask)
2037 {
2038 	int nss;
2039 
2040 	for (nss = 0; nss < IEEE80211_HT_MCS_MASK_LEN; nss++)
2041 		if (ht_mcs_mask[nss])
2042 			return false;
2043 
2044 	return true;
2045 }
2046 
2047 static bool
2048 ath12k_peer_assoc_h_vht_masked(const u16 *vht_mcs_mask)
2049 {
2050 	int nss;
2051 
2052 	for (nss = 0; nss < NL80211_VHT_NSS_MAX; nss++)
2053 		if (vht_mcs_mask[nss])
2054 			return false;
2055 
2056 	return true;
2057 }
2058 
2059 static void ath12k_peer_assoc_h_ht(struct ath12k *ar,
2060 				   struct ath12k_link_vif *arvif,
2061 				   struct ath12k_link_sta *arsta,
2062 				   struct ath12k_wmi_peer_assoc_arg *arg)
2063 {
2064 	struct ieee80211_vif *vif = ath12k_ahvif_to_vif(arvif->ahvif);
2065 	struct ieee80211_sta *sta = ath12k_ahsta_to_sta(arsta->ahsta);
2066 	const struct ieee80211_sta_ht_cap *ht_cap;
2067 	struct ieee80211_link_sta *link_sta;
2068 	struct cfg80211_chan_def def;
2069 	enum nl80211_band band;
2070 	const u8 *ht_mcs_mask;
2071 	int i, n;
2072 	u8 max_nss;
2073 	u32 stbc;
2074 
2075 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
2076 
2077 	if (WARN_ON(ath12k_mac_vif_link_chan(vif, arvif->link_id, &def)))
2078 		return;
2079 
2080 	link_sta = ath12k_mac_get_link_sta(arsta);
2081 	if (!link_sta) {
2082 		ath12k_warn(ar->ab, "unable to access link sta in peer assoc ht for sta %pM link %u\n",
2083 			    sta->addr, arsta->link_id);
2084 		return;
2085 	}
2086 
2087 	ht_cap = &link_sta->ht_cap;
2088 	if (!ht_cap->ht_supported)
2089 		return;
2090 
2091 	band = def.chan->band;
2092 	ht_mcs_mask = arvif->bitrate_mask.control[band].ht_mcs;
2093 
2094 	if (ath12k_peer_assoc_h_ht_masked(ht_mcs_mask))
2095 		return;
2096 
2097 	arg->ht_flag = true;
2098 
2099 	arg->peer_max_mpdu = (1 << (IEEE80211_HT_MAX_AMPDU_FACTOR +
2100 				    ht_cap->ampdu_factor)) - 1;
2101 
2102 	arg->peer_mpdu_density =
2103 		ath12k_parse_mpdudensity(ht_cap->ampdu_density);
2104 
2105 	arg->peer_ht_caps = ht_cap->cap;
2106 	arg->peer_rate_caps |= WMI_HOST_RC_HT_FLAG;
2107 
2108 	if (ht_cap->cap & IEEE80211_HT_CAP_LDPC_CODING)
2109 		arg->ldpc_flag = true;
2110 
2111 	if (link_sta->bandwidth >= IEEE80211_STA_RX_BW_40) {
2112 		arg->bw_40 = true;
2113 		arg->peer_rate_caps |= WMI_HOST_RC_CW40_FLAG;
2114 	}
2115 
2116 	/* As firmware handles these two flags (IEEE80211_HT_CAP_SGI_20
2117 	 * and IEEE80211_HT_CAP_SGI_40) for enabling SGI, reset both
2118 	 * flags if guard interval is to force Long GI
2119 	 */
2120 	if (arvif->bitrate_mask.control[band].gi == NL80211_TXRATE_FORCE_LGI) {
2121 		arg->peer_ht_caps &= ~(IEEE80211_HT_CAP_SGI_20 | IEEE80211_HT_CAP_SGI_40);
2122 	} else {
2123 		/* Enable SGI flag if either SGI_20 or SGI_40 is supported */
2124 		if (ht_cap->cap & (IEEE80211_HT_CAP_SGI_20 | IEEE80211_HT_CAP_SGI_40))
2125 			arg->peer_rate_caps |= WMI_HOST_RC_SGI_FLAG;
2126 	}
2127 
2128 	if (ht_cap->cap & IEEE80211_HT_CAP_TX_STBC) {
2129 		arg->peer_rate_caps |= WMI_HOST_RC_TX_STBC_FLAG;
2130 		arg->stbc_flag = true;
2131 	}
2132 
2133 	if (ht_cap->cap & IEEE80211_HT_CAP_RX_STBC) {
2134 		stbc = ht_cap->cap & IEEE80211_HT_CAP_RX_STBC;
2135 		stbc = stbc >> IEEE80211_HT_CAP_RX_STBC_SHIFT;
2136 		stbc = stbc << WMI_HOST_RC_RX_STBC_FLAG_S;
2137 		arg->peer_rate_caps |= stbc;
2138 		arg->stbc_flag = true;
2139 	}
2140 
2141 	if (ht_cap->mcs.rx_mask[1] && ht_cap->mcs.rx_mask[2])
2142 		arg->peer_rate_caps |= WMI_HOST_RC_TS_FLAG;
2143 	else if (ht_cap->mcs.rx_mask[1])
2144 		arg->peer_rate_caps |= WMI_HOST_RC_DS_FLAG;
2145 
2146 	for (i = 0, n = 0, max_nss = 0; i < IEEE80211_HT_MCS_MASK_LEN * 8; i++)
2147 		if ((ht_cap->mcs.rx_mask[i / 8] & BIT(i % 8)) &&
2148 		    (ht_mcs_mask[i / 8] & BIT(i % 8))) {
2149 			max_nss = (i / 8) + 1;
2150 			arg->peer_ht_rates.rates[n++] = i;
2151 		}
2152 
2153 	/* This is a workaround for HT-enabled STAs which break the spec
2154 	 * and have no HT capabilities RX mask (no HT RX MCS map).
2155 	 *
2156 	 * As per spec, in section 20.3.5 Modulation and coding scheme (MCS),
2157 	 * MCS 0 through 7 are mandatory in 20MHz with 800 ns GI at all STAs.
2158 	 *
2159 	 * Firmware asserts if such situation occurs.
2160 	 */
2161 	if (n == 0) {
2162 		arg->peer_ht_rates.num_rates = 8;
2163 		for (i = 0; i < arg->peer_ht_rates.num_rates; i++)
2164 			arg->peer_ht_rates.rates[i] = i;
2165 	} else {
2166 		arg->peer_ht_rates.num_rates = n;
2167 		arg->peer_nss = min(link_sta->rx_nss, max_nss);
2168 	}
2169 
2170 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac ht peer %pM mcs cnt %d nss %d\n",
2171 		   arg->peer_mac,
2172 		   arg->peer_ht_rates.num_rates,
2173 		   arg->peer_nss);
2174 }
2175 
2176 static int ath12k_mac_get_max_vht_mcs_map(u16 mcs_map, int nss)
2177 {
2178 	switch ((mcs_map >> (2 * nss)) & 0x3) {
2179 	case IEEE80211_VHT_MCS_SUPPORT_0_7: return BIT(8) - 1;
2180 	case IEEE80211_VHT_MCS_SUPPORT_0_8: return BIT(9) - 1;
2181 	case IEEE80211_VHT_MCS_SUPPORT_0_9: return BIT(10) - 1;
2182 	}
2183 	return 0;
2184 }
2185 
2186 static u16
2187 ath12k_peer_assoc_h_vht_limit(u16 tx_mcs_set,
2188 			      const u16 vht_mcs_limit[NL80211_VHT_NSS_MAX])
2189 {
2190 	int idx_limit;
2191 	int nss;
2192 	u16 mcs_map;
2193 	u16 mcs;
2194 
2195 	for (nss = 0; nss < NL80211_VHT_NSS_MAX; nss++) {
2196 		mcs_map = ath12k_mac_get_max_vht_mcs_map(tx_mcs_set, nss) &
2197 			  vht_mcs_limit[nss];
2198 
2199 		if (mcs_map)
2200 			idx_limit = fls(mcs_map) - 1;
2201 		else
2202 			idx_limit = -1;
2203 
2204 		switch (idx_limit) {
2205 		case 0:
2206 		case 1:
2207 		case 2:
2208 		case 3:
2209 		case 4:
2210 		case 5:
2211 		case 6:
2212 		case 7:
2213 			mcs = IEEE80211_VHT_MCS_SUPPORT_0_7;
2214 			break;
2215 		case 8:
2216 			mcs = IEEE80211_VHT_MCS_SUPPORT_0_8;
2217 			break;
2218 		case 9:
2219 			mcs = IEEE80211_VHT_MCS_SUPPORT_0_9;
2220 			break;
2221 		default:
2222 			WARN_ON(1);
2223 			fallthrough;
2224 		case -1:
2225 			mcs = IEEE80211_VHT_MCS_NOT_SUPPORTED;
2226 			break;
2227 		}
2228 
2229 		tx_mcs_set &= ~(0x3 << (nss * 2));
2230 		tx_mcs_set |= mcs << (nss * 2);
2231 	}
2232 
2233 	return tx_mcs_set;
2234 }
2235 
2236 static u8 ath12k_get_nss_160mhz(struct ath12k *ar,
2237 				u8 max_nss)
2238 {
2239 	u8 nss_ratio_info = ar->pdev->cap.nss_ratio_info;
2240 	u8 max_sup_nss = 0;
2241 
2242 	switch (nss_ratio_info) {
2243 	case WMI_NSS_RATIO_1BY2_NSS:
2244 		max_sup_nss = max_nss >> 1;
2245 		break;
2246 	case WMI_NSS_RATIO_3BY4_NSS:
2247 		ath12k_warn(ar->ab, "WMI_NSS_RATIO_3BY4_NSS not supported\n");
2248 		break;
2249 	case WMI_NSS_RATIO_1_NSS:
2250 		max_sup_nss = max_nss;
2251 		break;
2252 	case WMI_NSS_RATIO_2_NSS:
2253 		ath12k_warn(ar->ab, "WMI_NSS_RATIO_2_NSS not supported\n");
2254 		break;
2255 	default:
2256 		ath12k_warn(ar->ab, "invalid nss ratio received from fw: %d\n",
2257 			    nss_ratio_info);
2258 		break;
2259 	}
2260 
2261 	return max_sup_nss;
2262 }
2263 
2264 static void ath12k_peer_assoc_h_vht(struct ath12k *ar,
2265 				    struct ath12k_link_vif *arvif,
2266 				    struct ath12k_link_sta *arsta,
2267 				    struct ath12k_wmi_peer_assoc_arg *arg)
2268 {
2269 	struct ieee80211_vif *vif = ath12k_ahvif_to_vif(arvif->ahvif);
2270 	struct ieee80211_sta *sta = ath12k_ahsta_to_sta(arsta->ahsta);
2271 	const struct ieee80211_sta_vht_cap *vht_cap;
2272 	struct ieee80211_link_sta *link_sta;
2273 	struct cfg80211_chan_def def;
2274 	enum nl80211_band band;
2275 	u16 *vht_mcs_mask;
2276 	u8 ampdu_factor;
2277 	u8 max_nss, vht_mcs;
2278 	int i, vht_nss, nss_idx;
2279 	bool user_rate_valid = true;
2280 	u32 rx_nss, tx_nss, nss_160;
2281 
2282 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
2283 
2284 	if (WARN_ON(ath12k_mac_vif_link_chan(vif, arvif->link_id, &def)))
2285 		return;
2286 
2287 	link_sta = ath12k_mac_get_link_sta(arsta);
2288 	if (!link_sta) {
2289 		ath12k_warn(ar->ab, "unable to access link sta in peer assoc vht for sta %pM link %u\n",
2290 			    sta->addr, arsta->link_id);
2291 		return;
2292 	}
2293 
2294 	vht_cap = &link_sta->vht_cap;
2295 	if (!vht_cap->vht_supported)
2296 		return;
2297 
2298 	band = def.chan->band;
2299 	vht_mcs_mask = arvif->bitrate_mask.control[band].vht_mcs;
2300 
2301 	if (ath12k_peer_assoc_h_vht_masked(vht_mcs_mask))
2302 		return;
2303 
2304 	arg->vht_flag = true;
2305 
2306 	/* TODO: similar flags required? */
2307 	arg->vht_capable = true;
2308 
2309 	if (def.chan->band == NL80211_BAND_2GHZ)
2310 		arg->vht_ng_flag = true;
2311 
2312 	arg->peer_vht_caps = vht_cap->cap;
2313 
2314 	ampdu_factor = (vht_cap->cap &
2315 			IEEE80211_VHT_CAP_MAX_A_MPDU_LENGTH_EXPONENT_MASK) >>
2316 		       IEEE80211_VHT_CAP_MAX_A_MPDU_LENGTH_EXPONENT_SHIFT;
2317 
2318 	/* Workaround: Some Netgear/Linksys 11ac APs set Rx A-MPDU factor to
2319 	 * zero in VHT IE. Using it would result in degraded throughput.
2320 	 * arg->peer_max_mpdu at this point contains HT max_mpdu so keep
2321 	 * it if VHT max_mpdu is smaller.
2322 	 */
2323 	arg->peer_max_mpdu = max(arg->peer_max_mpdu,
2324 				 (1U << (IEEE80211_HT_MAX_AMPDU_FACTOR +
2325 					ampdu_factor)) - 1);
2326 
2327 	if (link_sta->bandwidth == IEEE80211_STA_RX_BW_80)
2328 		arg->bw_80 = true;
2329 
2330 	if (link_sta->bandwidth == IEEE80211_STA_RX_BW_160)
2331 		arg->bw_160 = true;
2332 
2333 	vht_nss =  ath12k_mac_max_vht_nss(vht_mcs_mask);
2334 
2335 	if (vht_nss > link_sta->rx_nss) {
2336 		user_rate_valid = false;
2337 		for (nss_idx = link_sta->rx_nss - 1; nss_idx >= 0; nss_idx--) {
2338 			if (vht_mcs_mask[nss_idx]) {
2339 				user_rate_valid = true;
2340 				break;
2341 			}
2342 		}
2343 	}
2344 
2345 	if (!user_rate_valid) {
2346 		ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
2347 			   "Setting vht range MCS value to peer supported nss:%d for peer %pM\n",
2348 			   link_sta->rx_nss, arsta->addr);
2349 		vht_mcs_mask[link_sta->rx_nss - 1] = vht_mcs_mask[vht_nss - 1];
2350 	}
2351 
2352 	/* Calculate peer NSS capability from VHT capabilities if STA
2353 	 * supports VHT.
2354 	 */
2355 	for (i = 0, max_nss = 0, vht_mcs = 0; i < NL80211_VHT_NSS_MAX; i++) {
2356 		vht_mcs = __le16_to_cpu(vht_cap->vht_mcs.rx_mcs_map) >>
2357 			  (2 * i) & 3;
2358 
2359 		if (vht_mcs != IEEE80211_VHT_MCS_NOT_SUPPORTED &&
2360 		    vht_mcs_mask[i])
2361 			max_nss = i + 1;
2362 	}
2363 	arg->peer_nss = min(link_sta->rx_nss, max_nss);
2364 	arg->rx_max_rate = __le16_to_cpu(vht_cap->vht_mcs.rx_highest);
2365 	arg->rx_mcs_set = __le16_to_cpu(vht_cap->vht_mcs.rx_mcs_map);
2366 	arg->rx_mcs_set = ath12k_peer_assoc_h_vht_limit(arg->rx_mcs_set, vht_mcs_mask);
2367 
2368 	arg->tx_max_rate = __le16_to_cpu(vht_cap->vht_mcs.tx_highest);
2369 	arg->tx_mcs_set = __le16_to_cpu(vht_cap->vht_mcs.tx_mcs_map);
2370 
2371 	/* In QCN9274 platform, VHT MCS rate 10 and 11 is enabled by default.
2372 	 * VHT MCS rate 10 and 11 is not supported in 11ac standard.
2373 	 * so explicitly disable the VHT MCS rate 10 and 11 in 11ac mode.
2374 	 */
2375 	arg->tx_mcs_set &= ~IEEE80211_VHT_MCS_SUPPORT_0_11_MASK;
2376 	arg->tx_mcs_set |= IEEE80211_DISABLE_VHT_MCS_SUPPORT_0_11;
2377 
2378 	if ((arg->tx_mcs_set & IEEE80211_VHT_MCS_NOT_SUPPORTED) ==
2379 			IEEE80211_VHT_MCS_NOT_SUPPORTED)
2380 		arg->peer_vht_caps &= ~IEEE80211_VHT_CAP_MU_BEAMFORMEE_CAPABLE;
2381 
2382 	/* TODO:  Check */
2383 	arg->tx_max_mcs_nss = 0xFF;
2384 
2385 	if (arg->peer_phymode == MODE_11AC_VHT160) {
2386 		tx_nss = ath12k_get_nss_160mhz(ar, max_nss);
2387 		rx_nss = min(arg->peer_nss, tx_nss);
2388 		arg->peer_bw_rxnss_override = ATH12K_BW_NSS_MAP_ENABLE;
2389 
2390 		if (!rx_nss) {
2391 			ath12k_warn(ar->ab, "invalid max_nss\n");
2392 			return;
2393 		}
2394 
2395 		nss_160 = u32_encode_bits(rx_nss - 1, ATH12K_PEER_RX_NSS_160MHZ);
2396 		arg->peer_bw_rxnss_override |= nss_160;
2397 	}
2398 
2399 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
2400 		   "mac vht peer %pM max_mpdu %d flags 0x%x nss_override 0x%x\n",
2401 		   arsta->addr, arg->peer_max_mpdu, arg->peer_flags,
2402 		   arg->peer_bw_rxnss_override);
2403 }
2404 
2405 static int ath12k_mac_get_max_he_mcs_map(u16 mcs_map, int nss)
2406 {
2407 	switch ((mcs_map >> (2 * nss)) & 0x3) {
2408 	case IEEE80211_HE_MCS_SUPPORT_0_7: return BIT(8) - 1;
2409 	case IEEE80211_HE_MCS_SUPPORT_0_9: return BIT(10) - 1;
2410 	case IEEE80211_HE_MCS_SUPPORT_0_11: return BIT(12) - 1;
2411 	}
2412 	return 0;
2413 }
2414 
2415 static u16 ath12k_peer_assoc_h_he_limit(u16 tx_mcs_set,
2416 					const u16 *he_mcs_limit)
2417 {
2418 	int idx_limit;
2419 	int nss;
2420 	u16 mcs_map;
2421 	u16 mcs;
2422 
2423 	for (nss = 0; nss < NL80211_HE_NSS_MAX; nss++) {
2424 		mcs_map = ath12k_mac_get_max_he_mcs_map(tx_mcs_set, nss) &
2425 			he_mcs_limit[nss];
2426 
2427 		if (mcs_map)
2428 			idx_limit = fls(mcs_map) - 1;
2429 		else
2430 			idx_limit = -1;
2431 
2432 		switch (idx_limit) {
2433 		case 0 ... 7:
2434 			mcs = IEEE80211_HE_MCS_SUPPORT_0_7;
2435 			break;
2436 		case 8:
2437 		case 9:
2438 			mcs = IEEE80211_HE_MCS_SUPPORT_0_9;
2439 			break;
2440 		case 10:
2441 		case 11:
2442 			mcs = IEEE80211_HE_MCS_SUPPORT_0_11;
2443 			break;
2444 		default:
2445 			WARN_ON(1);
2446 			fallthrough;
2447 		case -1:
2448 			mcs = IEEE80211_HE_MCS_NOT_SUPPORTED;
2449 			break;
2450 		}
2451 
2452 		tx_mcs_set &= ~(0x3 << (nss * 2));
2453 		tx_mcs_set |= mcs << (nss * 2);
2454 	}
2455 
2456 	return tx_mcs_set;
2457 }
2458 
2459 static bool
2460 ath12k_peer_assoc_h_he_masked(const u16 he_mcs_mask[NL80211_HE_NSS_MAX])
2461 {
2462 	int nss;
2463 
2464 	for (nss = 0; nss < NL80211_HE_NSS_MAX; nss++)
2465 		if (he_mcs_mask[nss])
2466 			return false;
2467 
2468 	return true;
2469 }
2470 
2471 static void ath12k_peer_assoc_h_he(struct ath12k *ar,
2472 				   struct ath12k_link_vif *arvif,
2473 				   struct ath12k_link_sta *arsta,
2474 				   struct ath12k_wmi_peer_assoc_arg *arg)
2475 {
2476 	struct ieee80211_vif *vif = ath12k_ahvif_to_vif(arvif->ahvif);
2477 	struct ieee80211_sta *sta = ath12k_ahsta_to_sta(arsta->ahsta);
2478 	const struct ieee80211_sta_he_cap *he_cap;
2479 	struct ieee80211_bss_conf *link_conf;
2480 	struct ieee80211_link_sta *link_sta;
2481 	struct cfg80211_chan_def def;
2482 	int i;
2483 	u8 ampdu_factor, max_nss;
2484 	u8 rx_mcs_80 = IEEE80211_HE_MCS_NOT_SUPPORTED;
2485 	u8 rx_mcs_160 = IEEE80211_HE_MCS_NOT_SUPPORTED;
2486 	u16 mcs_160_map, mcs_80_map;
2487 	u8 link_id = arvif->link_id;
2488 	bool support_160;
2489 	enum nl80211_band band;
2490 	u16 *he_mcs_mask;
2491 	u8 he_mcs;
2492 	u16 he_tx_mcs = 0, v = 0;
2493 	int he_nss, nss_idx;
2494 	bool user_rate_valid = true;
2495 	u32 rx_nss, tx_nss, nss_160;
2496 
2497 	if (WARN_ON(ath12k_mac_vif_link_chan(vif, link_id, &def)))
2498 		return;
2499 
2500 	link_conf = ath12k_mac_get_link_bss_conf(arvif);
2501 	if (!link_conf) {
2502 		ath12k_warn(ar->ab, "unable to access bss link conf in peer assoc he for vif %pM link %u",
2503 			    vif->addr, link_id);
2504 		return;
2505 	}
2506 
2507 	link_sta = ath12k_mac_get_link_sta(arsta);
2508 	if (!link_sta) {
2509 		ath12k_warn(ar->ab, "unable to access link sta in peer assoc he for sta %pM link %u\n",
2510 			    sta->addr, arsta->link_id);
2511 		return;
2512 	}
2513 
2514 	he_cap = &link_sta->he_cap;
2515 	if (!he_cap->has_he)
2516 		return;
2517 
2518 	band = def.chan->band;
2519 	he_mcs_mask = arvif->bitrate_mask.control[band].he_mcs;
2520 
2521 	if (ath12k_peer_assoc_h_he_masked(he_mcs_mask))
2522 		return;
2523 
2524 	arg->he_flag = true;
2525 
2526 	support_160 = !!(he_cap->he_cap_elem.phy_cap_info[0] &
2527 		  IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_160MHZ_IN_5G);
2528 
2529 	/* Supported HE-MCS and NSS Set of peer he_cap is intersection with self he_cp */
2530 	mcs_160_map = le16_to_cpu(he_cap->he_mcs_nss_supp.rx_mcs_160);
2531 	mcs_80_map = le16_to_cpu(he_cap->he_mcs_nss_supp.rx_mcs_80);
2532 
2533 	if (support_160) {
2534 		for (i = 7; i >= 0; i--) {
2535 			u8 mcs_160 = (mcs_160_map >> (2 * i)) & 3;
2536 
2537 			if (mcs_160 != IEEE80211_HE_MCS_NOT_SUPPORTED) {
2538 				rx_mcs_160 = i + 1;
2539 				break;
2540 			}
2541 		}
2542 	}
2543 
2544 	for (i = 7; i >= 0; i--) {
2545 		u8 mcs_80 = (mcs_80_map >> (2 * i)) & 3;
2546 
2547 		if (mcs_80 != IEEE80211_HE_MCS_NOT_SUPPORTED) {
2548 			rx_mcs_80 = i + 1;
2549 			break;
2550 		}
2551 	}
2552 
2553 	if (support_160)
2554 		max_nss = min(rx_mcs_80, rx_mcs_160);
2555 	else
2556 		max_nss = rx_mcs_80;
2557 
2558 	arg->peer_nss = min(link_sta->rx_nss, max_nss);
2559 
2560 	memcpy(&arg->peer_he_cap_macinfo, he_cap->he_cap_elem.mac_cap_info,
2561 	       sizeof(he_cap->he_cap_elem.mac_cap_info));
2562 	memcpy(&arg->peer_he_cap_phyinfo, he_cap->he_cap_elem.phy_cap_info,
2563 	       sizeof(he_cap->he_cap_elem.phy_cap_info));
2564 	arg->peer_he_ops = link_conf->he_oper.params;
2565 
2566 	/* the top most byte is used to indicate BSS color info */
2567 	arg->peer_he_ops &= 0xffffff;
2568 
2569 	/* As per section 26.6.1 IEEE Std 802.11ax‐2022, if the Max AMPDU
2570 	 * Exponent Extension in HE cap is zero, use the arg->peer_max_mpdu
2571 	 * as calculated while parsing VHT caps(if VHT caps is present)
2572 	 * or HT caps (if VHT caps is not present).
2573 	 *
2574 	 * For non-zero value of Max AMPDU Exponent Extension in HE MAC caps,
2575 	 * if a HE STA sends VHT cap and HE cap IE in assoc request then, use
2576 	 * MAX_AMPDU_LEN_FACTOR as 20 to calculate max_ampdu length.
2577 	 * If a HE STA that does not send VHT cap, but HE and HT cap in assoc
2578 	 * request, then use MAX_AMPDU_LEN_FACTOR as 16 to calculate max_ampdu
2579 	 * length.
2580 	 */
2581 	ampdu_factor = u8_get_bits(he_cap->he_cap_elem.mac_cap_info[3],
2582 				   IEEE80211_HE_MAC_CAP3_MAX_AMPDU_LEN_EXP_MASK);
2583 
2584 	if (ampdu_factor) {
2585 		if (link_sta->vht_cap.vht_supported)
2586 			arg->peer_max_mpdu = (1 << (IEEE80211_HE_VHT_MAX_AMPDU_FACTOR +
2587 						    ampdu_factor)) - 1;
2588 		else if (link_sta->ht_cap.ht_supported)
2589 			arg->peer_max_mpdu = (1 << (IEEE80211_HE_HT_MAX_AMPDU_FACTOR +
2590 						    ampdu_factor)) - 1;
2591 	}
2592 
2593 	if (he_cap->he_cap_elem.phy_cap_info[6] &
2594 	    IEEE80211_HE_PHY_CAP6_PPE_THRESHOLD_PRESENT) {
2595 		int bit = 7;
2596 		int nss, ru;
2597 
2598 		arg->peer_ppet.numss_m1 = he_cap->ppe_thres[0] &
2599 					  IEEE80211_PPE_THRES_NSS_MASK;
2600 		arg->peer_ppet.ru_bit_mask =
2601 			(he_cap->ppe_thres[0] &
2602 			 IEEE80211_PPE_THRES_RU_INDEX_BITMASK_MASK) >>
2603 			IEEE80211_PPE_THRES_RU_INDEX_BITMASK_POS;
2604 
2605 		for (nss = 0; nss <= arg->peer_ppet.numss_m1; nss++) {
2606 			for (ru = 0; ru < 4; ru++) {
2607 				u32 val = 0;
2608 				int i;
2609 
2610 				if ((arg->peer_ppet.ru_bit_mask & BIT(ru)) == 0)
2611 					continue;
2612 				for (i = 0; i < 6; i++) {
2613 					val >>= 1;
2614 					val |= ((he_cap->ppe_thres[bit / 8] >>
2615 						 (bit % 8)) & 0x1) << 5;
2616 					bit++;
2617 				}
2618 				arg->peer_ppet.ppet16_ppet8_ru3_ru0[nss] |=
2619 								val << (ru * 6);
2620 			}
2621 		}
2622 	}
2623 
2624 	if (he_cap->he_cap_elem.mac_cap_info[0] & IEEE80211_HE_MAC_CAP0_TWT_RES)
2625 		arg->twt_responder = true;
2626 	if (he_cap->he_cap_elem.mac_cap_info[0] & IEEE80211_HE_MAC_CAP0_TWT_REQ)
2627 		arg->twt_requester = true;
2628 
2629 	he_nss = ath12k_mac_max_he_nss(he_mcs_mask);
2630 
2631 	if (he_nss > link_sta->rx_nss) {
2632 		user_rate_valid = false;
2633 		for (nss_idx = link_sta->rx_nss - 1; nss_idx >= 0; nss_idx--) {
2634 			if (he_mcs_mask[nss_idx]) {
2635 				user_rate_valid = true;
2636 				break;
2637 			}
2638 		}
2639 	}
2640 
2641 	if (!user_rate_valid) {
2642 		ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
2643 			   "Setting he range MCS value to peer supported nss:%d for peer %pM\n",
2644 			   link_sta->rx_nss, arsta->addr);
2645 		he_mcs_mask[link_sta->rx_nss - 1] = he_mcs_mask[he_nss - 1];
2646 	}
2647 
2648 	switch (link_sta->bandwidth) {
2649 	case IEEE80211_STA_RX_BW_160:
2650 		v = le16_to_cpu(he_cap->he_mcs_nss_supp.rx_mcs_160);
2651 		v = ath12k_peer_assoc_h_he_limit(v, he_mcs_mask);
2652 		arg->peer_he_rx_mcs_set[WMI_HECAP_TXRX_MCS_NSS_IDX_160] = v;
2653 
2654 		v = le16_to_cpu(he_cap->he_mcs_nss_supp.tx_mcs_160);
2655 		arg->peer_he_tx_mcs_set[WMI_HECAP_TXRX_MCS_NSS_IDX_160] = v;
2656 
2657 		arg->peer_he_mcs_count++;
2658 		if (!he_tx_mcs)
2659 			he_tx_mcs = v;
2660 		fallthrough;
2661 
2662 	default:
2663 		v = le16_to_cpu(he_cap->he_mcs_nss_supp.rx_mcs_80);
2664 		v = ath12k_peer_assoc_h_he_limit(v, he_mcs_mask);
2665 		arg->peer_he_rx_mcs_set[WMI_HECAP_TXRX_MCS_NSS_IDX_80] = v;
2666 
2667 		v = le16_to_cpu(he_cap->he_mcs_nss_supp.tx_mcs_80);
2668 		arg->peer_he_tx_mcs_set[WMI_HECAP_TXRX_MCS_NSS_IDX_80] = v;
2669 
2670 		arg->peer_he_mcs_count++;
2671 		if (!he_tx_mcs)
2672 			he_tx_mcs = v;
2673 		break;
2674 	}
2675 
2676 	/* Calculate peer NSS capability from HE capabilities if STA
2677 	 * supports HE.
2678 	 */
2679 	for (i = 0, max_nss = 0, he_mcs = 0; i < NL80211_HE_NSS_MAX; i++) {
2680 		he_mcs = he_tx_mcs >> (2 * i) & 3;
2681 
2682 		/* In case of fixed rates, MCS Range in he_tx_mcs might have
2683 		 * unsupported range, with he_mcs_mask set, so check either of them
2684 		 * to find nss.
2685 		 */
2686 		if (he_mcs != IEEE80211_HE_MCS_NOT_SUPPORTED ||
2687 		    he_mcs_mask[i])
2688 			max_nss = i + 1;
2689 	}
2690 
2691 	max_nss = min(max_nss, ar->num_tx_chains);
2692 	arg->peer_nss = min(link_sta->rx_nss, max_nss);
2693 
2694 	if (arg->peer_phymode == MODE_11AX_HE160) {
2695 		tx_nss = ath12k_get_nss_160mhz(ar, ar->num_tx_chains);
2696 		rx_nss = min(arg->peer_nss, tx_nss);
2697 
2698 		arg->peer_nss = min(link_sta->rx_nss, ar->num_rx_chains);
2699 		arg->peer_bw_rxnss_override = ATH12K_BW_NSS_MAP_ENABLE;
2700 
2701 		if (!rx_nss) {
2702 			ath12k_warn(ar->ab, "invalid max_nss\n");
2703 			return;
2704 		}
2705 
2706 		nss_160 = u32_encode_bits(rx_nss - 1, ATH12K_PEER_RX_NSS_160MHZ);
2707 		arg->peer_bw_rxnss_override |= nss_160;
2708 	}
2709 
2710 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
2711 		   "mac he peer %pM nss %d mcs cnt %d nss_override 0x%x\n",
2712 		   arsta->addr, arg->peer_nss,
2713 		   arg->peer_he_mcs_count,
2714 		   arg->peer_bw_rxnss_override);
2715 }
2716 
2717 static void ath12k_peer_assoc_h_he_6ghz(struct ath12k *ar,
2718 					struct ath12k_link_vif *arvif,
2719 					struct ath12k_link_sta *arsta,
2720 					struct ath12k_wmi_peer_assoc_arg *arg)
2721 {
2722 	struct ieee80211_vif *vif = ath12k_ahvif_to_vif(arvif->ahvif);
2723 	struct ieee80211_sta *sta = ath12k_ahsta_to_sta(arsta->ahsta);
2724 	const struct ieee80211_sta_he_cap *he_cap;
2725 	struct ieee80211_link_sta *link_sta;
2726 	struct cfg80211_chan_def def;
2727 	enum nl80211_band band;
2728 	u8 ampdu_factor, mpdu_density;
2729 
2730 	if (WARN_ON(ath12k_mac_vif_link_chan(vif, arvif->link_id, &def)))
2731 		return;
2732 
2733 	band = def.chan->band;
2734 
2735 	link_sta = ath12k_mac_get_link_sta(arsta);
2736 	if (!link_sta) {
2737 		ath12k_warn(ar->ab, "unable to access link sta in peer assoc he 6ghz for sta %pM link %u\n",
2738 			    sta->addr, arsta->link_id);
2739 		return;
2740 	}
2741 
2742 	he_cap = &link_sta->he_cap;
2743 
2744 	if (!arg->he_flag || band != NL80211_BAND_6GHZ || !link_sta->he_6ghz_capa.capa)
2745 		return;
2746 
2747 	if (link_sta->bandwidth == IEEE80211_STA_RX_BW_40)
2748 		arg->bw_40 = true;
2749 
2750 	if (link_sta->bandwidth == IEEE80211_STA_RX_BW_80)
2751 		arg->bw_80 = true;
2752 
2753 	if (link_sta->bandwidth == IEEE80211_STA_RX_BW_160)
2754 		arg->bw_160 = true;
2755 
2756 	if (link_sta->bandwidth == IEEE80211_STA_RX_BW_320)
2757 		arg->bw_320 = true;
2758 
2759 	arg->peer_he_caps_6ghz = le16_to_cpu(link_sta->he_6ghz_capa.capa);
2760 
2761 	mpdu_density = u32_get_bits(arg->peer_he_caps_6ghz,
2762 				    IEEE80211_HE_6GHZ_CAP_MIN_MPDU_START);
2763 	arg->peer_mpdu_density = ath12k_parse_mpdudensity(mpdu_density);
2764 
2765 	/* From IEEE Std 802.11ax-2021 - Section 10.12.2: An HE STA shall be capable of
2766 	 * receiving A-MPDU where the A-MPDU pre-EOF padding length is up to the value
2767 	 * indicated by the Maximum A-MPDU Length Exponent Extension field in the HE
2768 	 * Capabilities element and the Maximum A-MPDU Length Exponent field in HE 6 GHz
2769 	 * Band Capabilities element in the 6 GHz band.
2770 	 *
2771 	 * Here, we are extracting the Max A-MPDU Exponent Extension from HE caps and
2772 	 * factor is the Maximum A-MPDU Length Exponent from HE 6 GHZ Band capability.
2773 	 */
2774 	ampdu_factor = u8_get_bits(he_cap->he_cap_elem.mac_cap_info[3],
2775 				   IEEE80211_HE_MAC_CAP3_MAX_AMPDU_LEN_EXP_MASK) +
2776 			u32_get_bits(arg->peer_he_caps_6ghz,
2777 				     IEEE80211_HE_6GHZ_CAP_MAX_AMPDU_LEN_EXP);
2778 
2779 	arg->peer_max_mpdu = (1u << (IEEE80211_HE_6GHZ_MAX_AMPDU_FACTOR +
2780 				     ampdu_factor)) - 1;
2781 }
2782 
2783 static int ath12k_get_smps_from_capa(const struct ieee80211_sta_ht_cap *ht_cap,
2784 				     const struct ieee80211_he_6ghz_capa *he_6ghz_capa,
2785 				     int *smps)
2786 {
2787 	if (ht_cap->ht_supported)
2788 		*smps = u16_get_bits(ht_cap->cap, IEEE80211_HT_CAP_SM_PS);
2789 	else
2790 		*smps = le16_get_bits(he_6ghz_capa->capa,
2791 				      IEEE80211_HE_6GHZ_CAP_SM_PS);
2792 
2793 	if (*smps >= ARRAY_SIZE(ath12k_smps_map))
2794 		return -EINVAL;
2795 
2796 	return 0;
2797 }
2798 
2799 static void ath12k_peer_assoc_h_smps(struct ath12k_link_sta *arsta,
2800 				     struct ath12k_wmi_peer_assoc_arg *arg)
2801 {
2802 	struct ieee80211_sta *sta = ath12k_ahsta_to_sta(arsta->ahsta);
2803 	const struct ieee80211_he_6ghz_capa *he_6ghz_capa;
2804 	struct ath12k_link_vif *arvif = arsta->arvif;
2805 	const struct ieee80211_sta_ht_cap *ht_cap;
2806 	struct ieee80211_link_sta *link_sta;
2807 	struct ath12k *ar = arvif->ar;
2808 	int smps;
2809 
2810 	link_sta = ath12k_mac_get_link_sta(arsta);
2811 	if (!link_sta) {
2812 		ath12k_warn(ar->ab, "unable to access link sta in peer assoc he for sta %pM link %u\n",
2813 			    sta->addr, arsta->link_id);
2814 		return;
2815 	}
2816 
2817 	he_6ghz_capa = &link_sta->he_6ghz_capa;
2818 	ht_cap = &link_sta->ht_cap;
2819 
2820 	if (!ht_cap->ht_supported && !he_6ghz_capa->capa)
2821 		return;
2822 
2823 	if (ath12k_get_smps_from_capa(ht_cap, he_6ghz_capa, &smps))
2824 		return;
2825 
2826 	switch (smps) {
2827 	case WLAN_HT_CAP_SM_PS_STATIC:
2828 		arg->static_mimops_flag = true;
2829 		break;
2830 	case WLAN_HT_CAP_SM_PS_DYNAMIC:
2831 		arg->dynamic_mimops_flag = true;
2832 		break;
2833 	case WLAN_HT_CAP_SM_PS_DISABLED:
2834 		arg->spatial_mux_flag = true;
2835 		break;
2836 	default:
2837 		break;
2838 	}
2839 }
2840 
2841 static void ath12k_peer_assoc_h_qos(struct ath12k *ar,
2842 				    struct ath12k_link_vif *arvif,
2843 				    struct ath12k_link_sta *arsta,
2844 				    struct ath12k_wmi_peer_assoc_arg *arg)
2845 {
2846 	struct ieee80211_sta *sta = ath12k_ahsta_to_sta(arsta->ahsta);
2847 
2848 	switch (arvif->ahvif->vdev_type) {
2849 	case WMI_VDEV_TYPE_AP:
2850 		if (sta->wme) {
2851 			/* TODO: Check WME vs QoS */
2852 			arg->is_wme_set = true;
2853 			arg->qos_flag = true;
2854 		}
2855 
2856 		if (sta->wme && sta->uapsd_queues) {
2857 			/* TODO: Check WME vs QoS */
2858 			arg->is_wme_set = true;
2859 			arg->apsd_flag = true;
2860 			arg->peer_rate_caps |= WMI_HOST_RC_UAPSD_FLAG;
2861 		}
2862 		break;
2863 	case WMI_VDEV_TYPE_STA:
2864 		if (sta->wme) {
2865 			arg->is_wme_set = true;
2866 			arg->qos_flag = true;
2867 		}
2868 		break;
2869 	default:
2870 		break;
2871 	}
2872 
2873 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac peer %pM qos %d\n",
2874 		   arsta->addr, arg->qos_flag);
2875 }
2876 
2877 static int ath12k_peer_assoc_qos_ap(struct ath12k *ar,
2878 				    struct ath12k_link_vif *arvif,
2879 				    struct ath12k_link_sta *arsta)
2880 {
2881 	struct ieee80211_sta *sta = ath12k_ahsta_to_sta(arsta->ahsta);
2882 	struct ath12k_wmi_ap_ps_arg arg;
2883 	u32 max_sp;
2884 	u32 uapsd;
2885 	int ret;
2886 
2887 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
2888 
2889 	arg.vdev_id = arvif->vdev_id;
2890 
2891 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac uapsd_queues 0x%x max_sp %d\n",
2892 		   sta->uapsd_queues, sta->max_sp);
2893 
2894 	uapsd = 0;
2895 	if (sta->uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_VO)
2896 		uapsd |= WMI_AP_PS_UAPSD_AC3_DELIVERY_EN |
2897 			 WMI_AP_PS_UAPSD_AC3_TRIGGER_EN;
2898 	if (sta->uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_VI)
2899 		uapsd |= WMI_AP_PS_UAPSD_AC2_DELIVERY_EN |
2900 			 WMI_AP_PS_UAPSD_AC2_TRIGGER_EN;
2901 	if (sta->uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_BK)
2902 		uapsd |= WMI_AP_PS_UAPSD_AC1_DELIVERY_EN |
2903 			 WMI_AP_PS_UAPSD_AC1_TRIGGER_EN;
2904 	if (sta->uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_BE)
2905 		uapsd |= WMI_AP_PS_UAPSD_AC0_DELIVERY_EN |
2906 			 WMI_AP_PS_UAPSD_AC0_TRIGGER_EN;
2907 
2908 	max_sp = 0;
2909 	if (sta->max_sp < MAX_WMI_AP_PS_PEER_PARAM_MAX_SP)
2910 		max_sp = sta->max_sp;
2911 
2912 	arg.param = WMI_AP_PS_PEER_PARAM_UAPSD;
2913 	arg.value = uapsd;
2914 	ret = ath12k_wmi_send_set_ap_ps_param_cmd(ar, arsta->addr, &arg);
2915 	if (ret)
2916 		goto err;
2917 
2918 	arg.param = WMI_AP_PS_PEER_PARAM_MAX_SP;
2919 	arg.value = max_sp;
2920 	ret = ath12k_wmi_send_set_ap_ps_param_cmd(ar, arsta->addr, &arg);
2921 	if (ret)
2922 		goto err;
2923 
2924 	/* TODO: revisit during testing */
2925 	arg.param = WMI_AP_PS_PEER_PARAM_SIFS_RESP_FRMTYPE;
2926 	arg.value = DISABLE_SIFS_RESPONSE_TRIGGER;
2927 	ret = ath12k_wmi_send_set_ap_ps_param_cmd(ar, arsta->addr, &arg);
2928 	if (ret)
2929 		goto err;
2930 
2931 	arg.param = WMI_AP_PS_PEER_PARAM_SIFS_RESP_UAPSD;
2932 	arg.value = DISABLE_SIFS_RESPONSE_TRIGGER;
2933 	ret = ath12k_wmi_send_set_ap_ps_param_cmd(ar, arsta->addr, &arg);
2934 	if (ret)
2935 		goto err;
2936 
2937 	return 0;
2938 
2939 err:
2940 	ath12k_warn(ar->ab, "failed to set ap ps peer param %d for vdev %i: %d\n",
2941 		    arg.param, arvif->vdev_id, ret);
2942 	return ret;
2943 }
2944 
2945 static bool ath12k_mac_sta_has_ofdm_only(struct ieee80211_link_sta *sta)
2946 {
2947 	return sta->supp_rates[NL80211_BAND_2GHZ] >>
2948 	       ATH12K_MAC_FIRST_OFDM_RATE_IDX;
2949 }
2950 
2951 static enum wmi_phy_mode ath12k_mac_get_phymode_vht(struct ath12k *ar,
2952 						    struct ieee80211_link_sta *link_sta)
2953 {
2954 	if (link_sta->bandwidth == IEEE80211_STA_RX_BW_160) {
2955 		if (link_sta->vht_cap.cap & (IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_160MHZ |
2956 		    IEEE80211_VHT_CAP_EXT_NSS_BW_MASK))
2957 			return MODE_11AC_VHT160;
2958 
2959 		/* Allow STA to connect even if it does not explicitly advertise 160 MHz
2960 		 * support
2961 		 */
2962 		return MODE_11AC_VHT160;
2963 	}
2964 
2965 	if (link_sta->bandwidth == IEEE80211_STA_RX_BW_80)
2966 		return MODE_11AC_VHT80;
2967 
2968 	if (link_sta->bandwidth == IEEE80211_STA_RX_BW_40)
2969 		return MODE_11AC_VHT40;
2970 
2971 	if (link_sta->bandwidth == IEEE80211_STA_RX_BW_20)
2972 		return MODE_11AC_VHT20;
2973 
2974 	return MODE_UNKNOWN;
2975 }
2976 
2977 static enum wmi_phy_mode ath12k_mac_get_phymode_he(struct ath12k *ar,
2978 						   struct ieee80211_link_sta *link_sta)
2979 {
2980 	if (link_sta->bandwidth == IEEE80211_STA_RX_BW_160) {
2981 		if (link_sta->he_cap.he_cap_elem.phy_cap_info[0] &
2982 		     IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_160MHZ_IN_5G)
2983 			return MODE_11AX_HE160;
2984 
2985 		return MODE_UNKNOWN;
2986 	}
2987 
2988 	if (link_sta->bandwidth == IEEE80211_STA_RX_BW_80)
2989 		return MODE_11AX_HE80;
2990 
2991 	if (link_sta->bandwidth == IEEE80211_STA_RX_BW_40)
2992 		return MODE_11AX_HE40;
2993 
2994 	if (link_sta->bandwidth == IEEE80211_STA_RX_BW_20)
2995 		return MODE_11AX_HE20;
2996 
2997 	return MODE_UNKNOWN;
2998 }
2999 
3000 static enum wmi_phy_mode ath12k_mac_get_phymode_eht(struct ath12k *ar,
3001 						    struct ieee80211_link_sta *link_sta)
3002 {
3003 	if (link_sta->bandwidth == IEEE80211_STA_RX_BW_320)
3004 		if (link_sta->eht_cap.eht_cap_elem.phy_cap_info[0] &
3005 		    IEEE80211_EHT_PHY_CAP0_320MHZ_IN_6GHZ)
3006 			return MODE_11BE_EHT320;
3007 
3008 	if (link_sta->bandwidth == IEEE80211_STA_RX_BW_160) {
3009 		if (link_sta->he_cap.he_cap_elem.phy_cap_info[0] &
3010 		    IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_160MHZ_IN_5G)
3011 			return MODE_11BE_EHT160;
3012 
3013 		ath12k_warn(ar->ab, "invalid EHT PHY capability info for 160 Mhz: %d\n",
3014 			    link_sta->he_cap.he_cap_elem.phy_cap_info[0]);
3015 
3016 		return MODE_UNKNOWN;
3017 	}
3018 
3019 	if (link_sta->bandwidth == IEEE80211_STA_RX_BW_80)
3020 		return MODE_11BE_EHT80;
3021 
3022 	if (link_sta->bandwidth == IEEE80211_STA_RX_BW_40)
3023 		return MODE_11BE_EHT40;
3024 
3025 	if (link_sta->bandwidth == IEEE80211_STA_RX_BW_20)
3026 		return MODE_11BE_EHT20;
3027 
3028 	return MODE_UNKNOWN;
3029 }
3030 
3031 static bool
3032 ath12k_peer_assoc_h_eht_masked(const u16 eht_mcs_mask[NL80211_EHT_NSS_MAX])
3033 {
3034 	int nss;
3035 
3036 	for (nss = 0; nss < NL80211_EHT_NSS_MAX; nss++)
3037 		if (eht_mcs_mask[nss])
3038 			return false;
3039 
3040 	return true;
3041 }
3042 
3043 static void ath12k_peer_assoc_h_phymode(struct ath12k *ar,
3044 					struct ath12k_link_vif *arvif,
3045 					struct ath12k_link_sta *arsta,
3046 					struct ath12k_wmi_peer_assoc_arg *arg)
3047 {
3048 	struct ieee80211_link_sta *link_sta;
3049 	struct cfg80211_chan_def def;
3050 	enum nl80211_band band;
3051 	const u8 *ht_mcs_mask;
3052 	const u16 *vht_mcs_mask;
3053 	const u16 *he_mcs_mask;
3054 	const u16 *eht_mcs_mask;
3055 	enum wmi_phy_mode phymode = MODE_UNKNOWN;
3056 
3057 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
3058 
3059 	struct ieee80211_vif *vif = ath12k_ahvif_to_vif(arvif->ahvif);
3060 	struct ieee80211_sta *sta = ath12k_ahsta_to_sta(arsta->ahsta);
3061 
3062 	if (WARN_ON(ath12k_mac_vif_link_chan(vif, arvif->link_id, &def)))
3063 		return;
3064 
3065 	band = def.chan->band;
3066 	ht_mcs_mask = arvif->bitrate_mask.control[band].ht_mcs;
3067 	vht_mcs_mask = arvif->bitrate_mask.control[band].vht_mcs;
3068 	he_mcs_mask = arvif->bitrate_mask.control[band].he_mcs;
3069 	eht_mcs_mask = arvif->bitrate_mask.control[band].eht_mcs;
3070 
3071 	link_sta = ath12k_mac_get_link_sta(arsta);
3072 	if (!link_sta) {
3073 		ath12k_warn(ar->ab, "unable to access link sta in peer assoc he for sta %pM link %u\n",
3074 			    sta->addr, arsta->link_id);
3075 		return;
3076 	}
3077 
3078 	switch (band) {
3079 	case NL80211_BAND_2GHZ:
3080 		if (link_sta->eht_cap.has_eht &&
3081 		    !ath12k_peer_assoc_h_eht_masked(eht_mcs_mask)) {
3082 			if (link_sta->bandwidth == IEEE80211_STA_RX_BW_40)
3083 				phymode = MODE_11BE_EHT40_2G;
3084 			else
3085 				phymode = MODE_11BE_EHT20_2G;
3086 		} else if (link_sta->he_cap.has_he &&
3087 			   !ath12k_peer_assoc_h_he_masked(he_mcs_mask)) {
3088 			if (link_sta->bandwidth == IEEE80211_STA_RX_BW_80)
3089 				phymode = MODE_11AX_HE80_2G;
3090 			else if (link_sta->bandwidth == IEEE80211_STA_RX_BW_40)
3091 				phymode = MODE_11AX_HE40_2G;
3092 			else
3093 				phymode = MODE_11AX_HE20_2G;
3094 		} else if (link_sta->vht_cap.vht_supported &&
3095 		    !ath12k_peer_assoc_h_vht_masked(vht_mcs_mask)) {
3096 			if (link_sta->bandwidth == IEEE80211_STA_RX_BW_40)
3097 				phymode = MODE_11AC_VHT40;
3098 			else
3099 				phymode = MODE_11AC_VHT20;
3100 		} else if (link_sta->ht_cap.ht_supported &&
3101 			   !ath12k_peer_assoc_h_ht_masked(ht_mcs_mask)) {
3102 			if (link_sta->bandwidth == IEEE80211_STA_RX_BW_40)
3103 				phymode = MODE_11NG_HT40;
3104 			else
3105 				phymode = MODE_11NG_HT20;
3106 		} else if (ath12k_mac_sta_has_ofdm_only(link_sta)) {
3107 			phymode = MODE_11G;
3108 		} else {
3109 			phymode = MODE_11B;
3110 		}
3111 		break;
3112 	case NL80211_BAND_5GHZ:
3113 	case NL80211_BAND_6GHZ:
3114 		/* Check EHT first */
3115 		if (link_sta->eht_cap.has_eht) {
3116 			phymode = ath12k_mac_get_phymode_eht(ar, link_sta);
3117 		} else if (link_sta->he_cap.has_he &&
3118 			   !ath12k_peer_assoc_h_he_masked(he_mcs_mask)) {
3119 			phymode = ath12k_mac_get_phymode_he(ar, link_sta);
3120 		} else if (link_sta->vht_cap.vht_supported &&
3121 		    !ath12k_peer_assoc_h_vht_masked(vht_mcs_mask)) {
3122 			phymode = ath12k_mac_get_phymode_vht(ar, link_sta);
3123 		} else if (link_sta->ht_cap.ht_supported &&
3124 			   !ath12k_peer_assoc_h_ht_masked(ht_mcs_mask)) {
3125 			if (link_sta->bandwidth >= IEEE80211_STA_RX_BW_40)
3126 				phymode = MODE_11NA_HT40;
3127 			else
3128 				phymode = MODE_11NA_HT20;
3129 		} else {
3130 			phymode = MODE_11A;
3131 		}
3132 		break;
3133 	default:
3134 		break;
3135 	}
3136 
3137 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac peer %pM phymode %s\n",
3138 		   arsta->addr, ath12k_mac_phymode_str(phymode));
3139 
3140 	arg->peer_phymode = phymode;
3141 	WARN_ON(phymode == MODE_UNKNOWN);
3142 }
3143 
3144 #define ATH12K_EHT_MCS_7_ENABLED	0x00FF
3145 #define ATH12K_EHT_MCS_9_ENABLED	0x0300
3146 #define ATH12K_EHT_MCS_11_ENABLED	0x0C00
3147 #define ATH12K_EHT_MCS_13_ENABLED	0x3000
3148 
3149 static void ath12k_mac_set_eht_mcs(u8 rx_tx_mcs7, u8 rx_tx_mcs9,
3150 				   u8 rx_tx_mcs11, u8 rx_tx_mcs13,
3151 				   u32 *rx_mcs, u32 *tx_mcs,
3152 				   const u16 eht_mcs_limit[NL80211_EHT_NSS_MAX])
3153 {
3154 	int nss;
3155 	u8 mcs_7 = 0, mcs_9 = 0, mcs_11 = 0, mcs_13 = 0;
3156 	u8 peer_mcs_7, peer_mcs_9, peer_mcs_11, peer_mcs_13;
3157 
3158 	for (nss = 0; nss < NL80211_EHT_NSS_MAX; nss++) {
3159 		if (eht_mcs_limit[nss] & ATH12K_EHT_MCS_7_ENABLED)
3160 			mcs_7++;
3161 		if (eht_mcs_limit[nss] & ATH12K_EHT_MCS_9_ENABLED)
3162 			mcs_9++;
3163 		if (eht_mcs_limit[nss] & ATH12K_EHT_MCS_11_ENABLED)
3164 			mcs_11++;
3165 		if (eht_mcs_limit[nss] & ATH12K_EHT_MCS_13_ENABLED)
3166 			mcs_13++;
3167 	}
3168 
3169 	peer_mcs_7 = u8_get_bits(rx_tx_mcs7, IEEE80211_EHT_MCS_NSS_RX);
3170 	peer_mcs_9 = u8_get_bits(rx_tx_mcs9, IEEE80211_EHT_MCS_NSS_RX);
3171 	peer_mcs_11 = u8_get_bits(rx_tx_mcs11, IEEE80211_EHT_MCS_NSS_RX);
3172 	peer_mcs_13 = u8_get_bits(rx_tx_mcs13, IEEE80211_EHT_MCS_NSS_RX);
3173 
3174 	*rx_mcs = u32_encode_bits(min(peer_mcs_7, mcs_7), WMI_EHT_MCS_NSS_0_7) |
3175 		  u32_encode_bits(min(peer_mcs_9, mcs_9), WMI_EHT_MCS_NSS_8_9) |
3176 		  u32_encode_bits(min(peer_mcs_11, mcs_11), WMI_EHT_MCS_NSS_10_11) |
3177 		  u32_encode_bits(min(peer_mcs_13, mcs_13), WMI_EHT_MCS_NSS_12_13);
3178 
3179 	peer_mcs_7 = u8_get_bits(rx_tx_mcs7, IEEE80211_EHT_MCS_NSS_TX);
3180 	peer_mcs_9 = u8_get_bits(rx_tx_mcs9, IEEE80211_EHT_MCS_NSS_TX);
3181 	peer_mcs_11 = u8_get_bits(rx_tx_mcs11, IEEE80211_EHT_MCS_NSS_TX);
3182 	peer_mcs_13 = u8_get_bits(rx_tx_mcs13, IEEE80211_EHT_MCS_NSS_TX);
3183 
3184 	*tx_mcs = u32_encode_bits(min(peer_mcs_7, mcs_7), WMI_EHT_MCS_NSS_0_7) |
3185 		  u32_encode_bits(min(peer_mcs_9, mcs_9), WMI_EHT_MCS_NSS_8_9) |
3186 		  u32_encode_bits(min(peer_mcs_11, mcs_11), WMI_EHT_MCS_NSS_10_11) |
3187 		  u32_encode_bits(min(peer_mcs_13, mcs_13), WMI_EHT_MCS_NSS_12_13);
3188 }
3189 
3190 static void ath12k_mac_set_eht_ppe_threshold(const u8 *ppe_thres,
3191 					     struct ath12k_wmi_ppe_threshold_arg *ppet)
3192 {
3193 	u32 bit_pos = IEEE80211_EHT_PPE_THRES_INFO_HEADER_SIZE, val;
3194 	u8 nss, ru, i;
3195 	u8 ppet_bit_len_per_ru = IEEE80211_EHT_PPE_THRES_INFO_PPET_SIZE * 2;
3196 
3197 	ppet->numss_m1 = u8_get_bits(ppe_thres[0], IEEE80211_EHT_PPE_THRES_NSS_MASK);
3198 	ppet->ru_bit_mask = u16_get_bits(get_unaligned_le16(ppe_thres),
3199 					 IEEE80211_EHT_PPE_THRES_RU_INDEX_BITMASK_MASK);
3200 
3201 	for (nss = 0; nss <= ppet->numss_m1; nss++) {
3202 		for (ru = 0;
3203 		     ru < hweight16(IEEE80211_EHT_PPE_THRES_RU_INDEX_BITMASK_MASK);
3204 		     ru++) {
3205 			if ((ppet->ru_bit_mask & BIT(ru)) == 0)
3206 				continue;
3207 
3208 			val = 0;
3209 			for (i = 0; i < ppet_bit_len_per_ru; i++) {
3210 				val |= (((ppe_thres[bit_pos / 8] >>
3211 					  (bit_pos % 8)) & 0x1) << i);
3212 				bit_pos++;
3213 			}
3214 			ppet->ppet16_ppet8_ru3_ru0[nss] |=
3215 					(val << (ru * ppet_bit_len_per_ru));
3216 		}
3217 	}
3218 }
3219 
3220 static void ath12k_peer_assoc_h_eht(struct ath12k *ar,
3221 				    struct ath12k_link_vif *arvif,
3222 				    struct ath12k_link_sta *arsta,
3223 				    struct ath12k_wmi_peer_assoc_arg *arg)
3224 {
3225 	struct ieee80211_sta *sta = ath12k_ahsta_to_sta(arsta->ahsta);
3226 	struct ieee80211_vif *vif = ath12k_ahvif_to_vif(arvif->ahvif);
3227 	const struct ieee80211_eht_mcs_nss_supp *own_eht_mcs_nss_supp;
3228 	const struct ieee80211_eht_mcs_nss_supp_20mhz_only *bw_20;
3229 	const struct ieee80211_sta_eht_cap *eht_cap, *own_eht_cap;
3230 	const struct ieee80211_sband_iftype_data *iftd;
3231 	const struct ieee80211_eht_mcs_nss_supp_bw *bw;
3232 	const struct ieee80211_sta_he_cap *he_cap;
3233 	struct ieee80211_link_sta *link_sta;
3234 	struct ieee80211_bss_conf *link_conf;
3235 	struct cfg80211_chan_def def;
3236 	bool user_rate_valid = true;
3237 	enum nl80211_band band;
3238 	int eht_nss, nss_idx;
3239 	u32 *rx_mcs, *tx_mcs;
3240 	u16 *eht_mcs_mask;
3241 	u8 max_nss = 0;
3242 
3243 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
3244 
3245 	link_sta = ath12k_mac_get_link_sta(arsta);
3246 	if (!link_sta) {
3247 		ath12k_warn(ar->ab, "unable to access link sta in peer assoc eht for sta %pM link %u\n",
3248 			    sta->addr, arsta->link_id);
3249 		return;
3250 	}
3251 
3252 	link_conf = ath12k_mac_get_link_bss_conf(arvif);
3253 	if (!link_conf) {
3254 		ath12k_warn(ar->ab, "unable to access link_conf in peer assoc eht set\n");
3255 		return;
3256 	}
3257 
3258 	eht_cap = &link_sta->eht_cap;
3259 	he_cap = &link_sta->he_cap;
3260 	if (!he_cap->has_he || !eht_cap->has_eht)
3261 		return;
3262 
3263 	if (WARN_ON(ath12k_mac_vif_link_chan(vif, arvif->link_id, &def)))
3264 		return;
3265 
3266 	band = def.chan->band;
3267 	eht_mcs_mask = arvif->bitrate_mask.control[band].eht_mcs;
3268 
3269 	iftd = ieee80211_get_sband_iftype_data(&ar->mac.sbands[band], vif->type);
3270 	if (!iftd) {
3271 		ath12k_warn(ar->ab,
3272 			    "unable to access iftype_data in struct ieee80211_supported_band\n");
3273 		return;
3274 	}
3275 
3276 	own_eht_cap = &iftd->eht_cap;
3277 	own_eht_mcs_nss_supp = &own_eht_cap->eht_mcs_nss_supp;
3278 
3279 	arg->eht_flag = true;
3280 
3281 	if ((eht_cap->eht_cap_elem.phy_cap_info[5] &
3282 	     IEEE80211_EHT_PHY_CAP5_PPE_THRESHOLD_PRESENT) &&
3283 	    eht_cap->eht_ppe_thres[0] != 0)
3284 		ath12k_mac_set_eht_ppe_threshold(eht_cap->eht_ppe_thres,
3285 						 &arg->peer_eht_ppet);
3286 
3287 	memcpy(arg->peer_eht_cap_mac, eht_cap->eht_cap_elem.mac_cap_info,
3288 	       sizeof(eht_cap->eht_cap_elem.mac_cap_info));
3289 	memcpy(arg->peer_eht_cap_phy, eht_cap->eht_cap_elem.phy_cap_info,
3290 	       sizeof(eht_cap->eht_cap_elem.phy_cap_info));
3291 
3292 	rx_mcs = arg->peer_eht_rx_mcs_set;
3293 	tx_mcs = arg->peer_eht_tx_mcs_set;
3294 
3295 	eht_nss = ath12k_mac_max_eht_mcs_nss((void *)own_eht_mcs_nss_supp,
3296 					     sizeof(*own_eht_mcs_nss_supp));
3297 	if (eht_nss > link_sta->rx_nss) {
3298 		user_rate_valid = false;
3299 		for (nss_idx = (link_sta->rx_nss - 1); nss_idx >= 0; nss_idx--) {
3300 			if (eht_mcs_mask[nss_idx]) {
3301 				user_rate_valid = true;
3302 				break;
3303 			}
3304 		}
3305 	}
3306 
3307 	if (!user_rate_valid) {
3308 		ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
3309 			   "Setting eht range MCS value to peer supported nss %d for peer %pM\n",
3310 			   link_sta->rx_nss, arsta->addr);
3311 		eht_mcs_mask[link_sta->rx_nss - 1] = eht_mcs_mask[eht_nss - 1];
3312 	}
3313 
3314 	bw_20 = &eht_cap->eht_mcs_nss_supp.only_20mhz;
3315 	bw = &eht_cap->eht_mcs_nss_supp.bw._80;
3316 
3317 	switch (link_sta->bandwidth) {
3318 	case IEEE80211_STA_RX_BW_320:
3319 		bw = &eht_cap->eht_mcs_nss_supp.bw._320;
3320 		ath12k_mac_set_eht_mcs(bw->rx_tx_mcs9_max_nss,
3321 				       bw->rx_tx_mcs9_max_nss,
3322 				       bw->rx_tx_mcs11_max_nss,
3323 				       bw->rx_tx_mcs13_max_nss,
3324 				       &rx_mcs[WMI_EHTCAP_TXRX_MCS_NSS_IDX_320],
3325 				       &tx_mcs[WMI_EHTCAP_TXRX_MCS_NSS_IDX_320],
3326 				       eht_mcs_mask);
3327 		arg->peer_eht_mcs_count++;
3328 		fallthrough;
3329 	case IEEE80211_STA_RX_BW_160:
3330 		bw = &eht_cap->eht_mcs_nss_supp.bw._160;
3331 		ath12k_mac_set_eht_mcs(bw->rx_tx_mcs9_max_nss,
3332 				       bw->rx_tx_mcs9_max_nss,
3333 				       bw->rx_tx_mcs11_max_nss,
3334 				       bw->rx_tx_mcs13_max_nss,
3335 				       &rx_mcs[WMI_EHTCAP_TXRX_MCS_NSS_IDX_160],
3336 				       &tx_mcs[WMI_EHTCAP_TXRX_MCS_NSS_IDX_160],
3337 				       eht_mcs_mask);
3338 		arg->peer_eht_mcs_count++;
3339 		fallthrough;
3340 	default:
3341 		if (!(link_sta->he_cap.he_cap_elem.phy_cap_info[0] &
3342 		      IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_MASK_ALL)) {
3343 			bw_20 = &eht_cap->eht_mcs_nss_supp.only_20mhz;
3344 
3345 			ath12k_mac_set_eht_mcs(bw_20->rx_tx_mcs7_max_nss,
3346 					       bw_20->rx_tx_mcs9_max_nss,
3347 					       bw_20->rx_tx_mcs11_max_nss,
3348 					       bw_20->rx_tx_mcs13_max_nss,
3349 					       &rx_mcs[WMI_EHTCAP_TXRX_MCS_NSS_IDX_80],
3350 					       &tx_mcs[WMI_EHTCAP_TXRX_MCS_NSS_IDX_80],
3351 					       eht_mcs_mask);
3352 		} else {
3353 			bw = &eht_cap->eht_mcs_nss_supp.bw._80;
3354 			ath12k_mac_set_eht_mcs(bw->rx_tx_mcs9_max_nss,
3355 					       bw->rx_tx_mcs9_max_nss,
3356 					       bw->rx_tx_mcs11_max_nss,
3357 					       bw->rx_tx_mcs13_max_nss,
3358 					       &rx_mcs[WMI_EHTCAP_TXRX_MCS_NSS_IDX_80],
3359 					       &tx_mcs[WMI_EHTCAP_TXRX_MCS_NSS_IDX_80],
3360 					       eht_mcs_mask);
3361 		}
3362 
3363 		arg->peer_eht_mcs_count++;
3364 		break;
3365 	}
3366 
3367 	arg->punct_bitmap = ~arvif->punct_bitmap;
3368 	arg->eht_disable_mcs15 = link_conf->eht_disable_mcs15;
3369 
3370 	if (!(link_sta->he_cap.he_cap_elem.phy_cap_info[0] &
3371 	      IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_MASK_ALL)) {
3372 		if (bw_20->rx_tx_mcs13_max_nss)
3373 			max_nss = max(max_nss, u8_get_bits(bw_20->rx_tx_mcs13_max_nss,
3374 							   IEEE80211_EHT_MCS_NSS_RX));
3375 		if (bw_20->rx_tx_mcs11_max_nss)
3376 			max_nss = max(max_nss, u8_get_bits(bw_20->rx_tx_mcs11_max_nss,
3377 							   IEEE80211_EHT_MCS_NSS_RX));
3378 		if (bw_20->rx_tx_mcs9_max_nss)
3379 			max_nss = max(max_nss, u8_get_bits(bw_20->rx_tx_mcs9_max_nss,
3380 							   IEEE80211_EHT_MCS_NSS_RX));
3381 		if (bw_20->rx_tx_mcs7_max_nss)
3382 			max_nss = max(max_nss, u8_get_bits(bw_20->rx_tx_mcs7_max_nss,
3383 							   IEEE80211_EHT_MCS_NSS_RX));
3384 	} else {
3385 		if (bw->rx_tx_mcs13_max_nss)
3386 			max_nss = max(max_nss, u8_get_bits(bw->rx_tx_mcs13_max_nss,
3387 							   IEEE80211_EHT_MCS_NSS_RX));
3388 		if (bw->rx_tx_mcs11_max_nss)
3389 			max_nss = max(max_nss, u8_get_bits(bw->rx_tx_mcs11_max_nss,
3390 							   IEEE80211_EHT_MCS_NSS_RX));
3391 		if (bw->rx_tx_mcs9_max_nss)
3392 			max_nss = max(max_nss, u8_get_bits(bw->rx_tx_mcs9_max_nss,
3393 							   IEEE80211_EHT_MCS_NSS_RX));
3394 	}
3395 
3396 	max_nss = min(max_nss, (uint8_t)eht_nss);
3397 
3398 	arg->peer_nss = min(link_sta->rx_nss, max_nss);
3399 
3400 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
3401 		   "mac eht peer %pM nss %d mcs cnt %d ru_punct_bitmap 0x%x\n",
3402 		   arsta->addr, arg->peer_nss, arg->peer_eht_mcs_count,
3403 		   arg->punct_bitmap);
3404 }
3405 
3406 static void ath12k_peer_assoc_h_mlo(struct ath12k_link_sta *arsta,
3407 				    struct ath12k_wmi_peer_assoc_arg *arg)
3408 {
3409 	struct ieee80211_sta *sta = ath12k_ahsta_to_sta(arsta->ahsta);
3410 	struct peer_assoc_mlo_params *ml = &arg->ml;
3411 	struct ath12k_sta *ahsta = arsta->ahsta;
3412 	struct ath12k_link_sta *arsta_p;
3413 	struct ath12k_link_vif *arvif;
3414 	unsigned long links;
3415 	u8 link_id;
3416 	int i;
3417 
3418 	if (!sta->mlo || ahsta->ml_peer_id == ATH12K_MLO_PEER_ID_INVALID)
3419 		return;
3420 
3421 	ml->enabled = true;
3422 	ml->assoc_link = arsta->is_assoc_link;
3423 
3424 	/* For now considering the primary umac based on assoc link */
3425 	ml->primary_umac = arsta->is_assoc_link;
3426 	ml->peer_id_valid = true;
3427 	ml->logical_link_idx_valid = true;
3428 
3429 	ether_addr_copy(ml->mld_addr, sta->addr);
3430 	ml->logical_link_idx = arsta->link_idx;
3431 	ml->ml_peer_id = ahsta->ml_peer_id;
3432 	ml->ieee_link_id = arsta->link_id;
3433 	ml->num_partner_links = 0;
3434 	ml->eml_cap = sta->eml_cap;
3435 	links = ahsta->links_map;
3436 
3437 	rcu_read_lock();
3438 
3439 	i = 0;
3440 
3441 	for_each_set_bit(link_id, &links, IEEE80211_MLD_MAX_NUM_LINKS) {
3442 		if (i >= ATH12K_WMI_MLO_MAX_LINKS)
3443 			break;
3444 
3445 		arsta_p = rcu_dereference(ahsta->link[link_id]);
3446 		arvif = rcu_dereference(ahsta->ahvif->link[link_id]);
3447 
3448 		if (arsta_p == arsta)
3449 			continue;
3450 
3451 		if (!arvif->is_started)
3452 			continue;
3453 
3454 		ml->partner_info[i].vdev_id = arvif->vdev_id;
3455 		ml->partner_info[i].hw_link_id = arvif->ar->pdev->hw_link_id;
3456 		ml->partner_info[i].assoc_link = arsta_p->is_assoc_link;
3457 		ml->partner_info[i].primary_umac = arsta_p->is_assoc_link;
3458 		ml->partner_info[i].logical_link_idx_valid = true;
3459 		ml->partner_info[i].logical_link_idx = arsta_p->link_idx;
3460 		ml->num_partner_links++;
3461 
3462 		i++;
3463 	}
3464 
3465 	rcu_read_unlock();
3466 }
3467 
3468 static void ath12k_peer_assoc_prepare(struct ath12k *ar,
3469 				      struct ath12k_link_vif *arvif,
3470 				      struct ath12k_link_sta *arsta,
3471 				      struct ath12k_wmi_peer_assoc_arg *arg,
3472 				      bool reassoc)
3473 {
3474 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
3475 
3476 	memset(arg, 0, sizeof(*arg));
3477 
3478 	reinit_completion(&ar->peer_assoc_done);
3479 
3480 	arg->peer_new_assoc = !reassoc;
3481 	ath12k_peer_assoc_h_basic(ar, arvif, arsta, arg);
3482 	ath12k_peer_assoc_h_crypto(ar, arvif, arsta, arg);
3483 	ath12k_peer_assoc_h_rates(ar, arvif, arsta, arg);
3484 	ath12k_peer_assoc_h_ht(ar, arvif, arsta, arg);
3485 	ath12k_peer_assoc_h_vht(ar, arvif, arsta, arg);
3486 	ath12k_peer_assoc_h_he(ar, arvif, arsta, arg);
3487 	ath12k_peer_assoc_h_he_6ghz(ar, arvif, arsta, arg);
3488 	ath12k_peer_assoc_h_eht(ar, arvif, arsta, arg);
3489 	ath12k_peer_assoc_h_qos(ar, arvif, arsta, arg);
3490 	ath12k_peer_assoc_h_phymode(ar, arvif, arsta, arg);
3491 	ath12k_peer_assoc_h_smps(arsta, arg);
3492 	ath12k_peer_assoc_h_mlo(arsta, arg);
3493 
3494 	arsta->peer_nss = arg->peer_nss;
3495 	/* TODO: amsdu_disable req? */
3496 }
3497 
3498 static int ath12k_setup_peer_smps(struct ath12k *ar, struct ath12k_link_vif *arvif,
3499 				  const u8 *addr,
3500 				  const struct ieee80211_sta_ht_cap *ht_cap,
3501 				  const struct ieee80211_he_6ghz_capa *he_6ghz_capa)
3502 {
3503 	int smps, ret = 0;
3504 
3505 	if (!ht_cap->ht_supported && !he_6ghz_capa)
3506 		return 0;
3507 
3508 	ret = ath12k_get_smps_from_capa(ht_cap, he_6ghz_capa, &smps);
3509 	if (ret < 0)
3510 		return ret;
3511 
3512 	return ath12k_wmi_set_peer_param(ar, addr, arvif->vdev_id,
3513 					 WMI_PEER_MIMO_PS_STATE,
3514 					 ath12k_smps_map[smps]);
3515 }
3516 
3517 static int ath12k_mac_set_he_txbf_conf(struct ath12k_link_vif *arvif)
3518 {
3519 	struct ath12k_vif *ahvif = arvif->ahvif;
3520 	struct ath12k *ar = arvif->ar;
3521 	u32 param = WMI_VDEV_PARAM_SET_HEMU_MODE;
3522 	u32 value = 0;
3523 	int ret;
3524 	struct ieee80211_bss_conf *link_conf;
3525 
3526 	link_conf = ath12k_mac_get_link_bss_conf(arvif);
3527 	if (!link_conf) {
3528 		ath12k_warn(ar->ab, "unable to access bss link conf in txbf conf\n");
3529 		return -EINVAL;
3530 	}
3531 
3532 	if (!link_conf->he_support)
3533 		return 0;
3534 
3535 	if (link_conf->he_su_beamformer) {
3536 		value |= u32_encode_bits(HE_SU_BFER_ENABLE, HE_MODE_SU_TX_BFER);
3537 		if (link_conf->he_mu_beamformer &&
3538 		    ahvif->vdev_type == WMI_VDEV_TYPE_AP)
3539 			value |= u32_encode_bits(HE_MU_BFER_ENABLE, HE_MODE_MU_TX_BFER);
3540 	}
3541 
3542 	if (ahvif->vif->type != NL80211_IFTYPE_MESH_POINT) {
3543 		value |= u32_encode_bits(HE_DL_MUOFDMA_ENABLE, HE_MODE_DL_OFDMA) |
3544 			 u32_encode_bits(HE_UL_MUOFDMA_ENABLE, HE_MODE_UL_OFDMA);
3545 
3546 		if (link_conf->he_full_ul_mumimo)
3547 			value |= u32_encode_bits(HE_UL_MUMIMO_ENABLE, HE_MODE_UL_MUMIMO);
3548 
3549 		if (link_conf->he_su_beamformee)
3550 			value |= u32_encode_bits(HE_SU_BFEE_ENABLE, HE_MODE_SU_TX_BFEE);
3551 	}
3552 
3553 	ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id, param, value);
3554 	if (ret) {
3555 		ath12k_warn(ar->ab, "failed to set vdev %d HE MU mode: %d\n",
3556 			    arvif->vdev_id, ret);
3557 		return ret;
3558 	}
3559 
3560 	param = WMI_VDEV_PARAM_SET_HE_SOUNDING_MODE;
3561 	value =	u32_encode_bits(HE_VHT_SOUNDING_MODE_ENABLE, HE_VHT_SOUNDING_MODE) |
3562 		u32_encode_bits(HE_TRIG_NONTRIG_SOUNDING_MODE_ENABLE,
3563 				HE_TRIG_NONTRIG_SOUNDING_MODE);
3564 	ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
3565 					    param, value);
3566 	if (ret) {
3567 		ath12k_warn(ar->ab, "failed to set vdev %d sounding mode: %d\n",
3568 			    arvif->vdev_id, ret);
3569 		return ret;
3570 	}
3571 
3572 	return 0;
3573 }
3574 
3575 static int ath12k_mac_vif_recalc_sta_he_txbf(struct ath12k *ar,
3576 					     struct ath12k_link_vif *arvif,
3577 					     struct ieee80211_sta_he_cap *he_cap,
3578 					     int *hemode)
3579 {
3580 	struct ieee80211_vif *vif = arvif->ahvif->vif;
3581 	struct ieee80211_he_cap_elem he_cap_elem = {};
3582 	struct ieee80211_sta_he_cap *cap_band;
3583 	struct cfg80211_chan_def def;
3584 	u8 link_id = arvif->link_id;
3585 	struct ieee80211_bss_conf *link_conf;
3586 
3587 	link_conf = ath12k_mac_get_link_bss_conf(arvif);
3588 	if (!link_conf) {
3589 		ath12k_warn(ar->ab, "unable to access bss link conf in recalc txbf conf\n");
3590 		return -EINVAL;
3591 	}
3592 
3593 	if (!link_conf->he_support)
3594 		return 0;
3595 
3596 	if (vif->type != NL80211_IFTYPE_STATION)
3597 		return -EINVAL;
3598 
3599 	if (WARN_ON(ath12k_mac_vif_link_chan(vif, link_id, &def)))
3600 		return -EINVAL;
3601 
3602 	if (def.chan->band == NL80211_BAND_2GHZ)
3603 		cap_band = &ar->mac.iftype[NL80211_BAND_2GHZ][vif->type].he_cap;
3604 	else
3605 		cap_band = &ar->mac.iftype[NL80211_BAND_5GHZ][vif->type].he_cap;
3606 
3607 	memcpy(&he_cap_elem, &cap_band->he_cap_elem, sizeof(he_cap_elem));
3608 
3609 	*hemode = 0;
3610 	if (HECAP_PHY_SUBFME_GET(he_cap_elem.phy_cap_info)) {
3611 		if (HECAP_PHY_SUBFMR_GET(he_cap->he_cap_elem.phy_cap_info))
3612 			*hemode |= u32_encode_bits(HE_SU_BFEE_ENABLE, HE_MODE_SU_TX_BFEE);
3613 		if (HECAP_PHY_MUBFMR_GET(he_cap->he_cap_elem.phy_cap_info))
3614 			*hemode |= u32_encode_bits(HE_MU_BFEE_ENABLE, HE_MODE_MU_TX_BFEE);
3615 	}
3616 
3617 	if (vif->type != NL80211_IFTYPE_MESH_POINT) {
3618 		*hemode |= u32_encode_bits(HE_DL_MUOFDMA_ENABLE, HE_MODE_DL_OFDMA) |
3619 			  u32_encode_bits(HE_UL_MUOFDMA_ENABLE, HE_MODE_UL_OFDMA);
3620 
3621 		if (HECAP_PHY_ULMUMIMO_GET(he_cap_elem.phy_cap_info))
3622 			if (HECAP_PHY_ULMUMIMO_GET(he_cap->he_cap_elem.phy_cap_info))
3623 				*hemode |= u32_encode_bits(HE_UL_MUMIMO_ENABLE,
3624 							  HE_MODE_UL_MUMIMO);
3625 
3626 		if (u32_get_bits(*hemode, HE_MODE_MU_TX_BFEE))
3627 			*hemode |= u32_encode_bits(HE_SU_BFEE_ENABLE, HE_MODE_SU_TX_BFEE);
3628 
3629 		if (u32_get_bits(*hemode, HE_MODE_MU_TX_BFER))
3630 			*hemode |= u32_encode_bits(HE_SU_BFER_ENABLE, HE_MODE_SU_TX_BFER);
3631 	}
3632 
3633 	return 0;
3634 }
3635 
3636 static int ath12k_mac_set_eht_txbf_conf(struct ath12k_link_vif *arvif)
3637 {
3638 	struct ath12k_vif *ahvif = arvif->ahvif;
3639 	struct ath12k *ar = arvif->ar;
3640 	u32 param = WMI_VDEV_PARAM_SET_EHT_MU_MODE;
3641 	u32 value = 0;
3642 	int ret;
3643 	struct ieee80211_bss_conf *link_conf;
3644 
3645 	link_conf = ath12k_mac_get_link_bss_conf(arvif);
3646 	if (!link_conf) {
3647 		ath12k_warn(ar->ab, "unable to access bss link conf in eht txbf conf\n");
3648 		return -ENOENT;
3649 	}
3650 
3651 	if (!link_conf->eht_support)
3652 		return 0;
3653 
3654 	if (link_conf->eht_su_beamformer) {
3655 		value |= u32_encode_bits(EHT_SU_BFER_ENABLE, EHT_MODE_SU_TX_BFER);
3656 		if (link_conf->eht_mu_beamformer &&
3657 		    ahvif->vdev_type == WMI_VDEV_TYPE_AP)
3658 			value |= u32_encode_bits(EHT_MU_BFER_ENABLE,
3659 						 EHT_MODE_MU_TX_BFER) |
3660 				 u32_encode_bits(EHT_DL_MUOFDMA_ENABLE,
3661 						 EHT_MODE_DL_OFDMA_MUMIMO) |
3662 				 u32_encode_bits(EHT_UL_MUOFDMA_ENABLE,
3663 						 EHT_MODE_UL_OFDMA_MUMIMO);
3664 	}
3665 
3666 	if (ahvif->vif->type != NL80211_IFTYPE_MESH_POINT) {
3667 		value |= u32_encode_bits(EHT_DL_MUOFDMA_ENABLE, EHT_MODE_DL_OFDMA) |
3668 			 u32_encode_bits(EHT_UL_MUOFDMA_ENABLE, EHT_MODE_UL_OFDMA);
3669 
3670 		if (link_conf->eht_80mhz_full_bw_ul_mumimo)
3671 			value |= u32_encode_bits(EHT_UL_MUMIMO_ENABLE, EHT_MODE_MUMIMO);
3672 
3673 		if (link_conf->eht_su_beamformee)
3674 			value |= u32_encode_bits(EHT_SU_BFEE_ENABLE,
3675 						 EHT_MODE_SU_TX_BFEE);
3676 	}
3677 
3678 	ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id, param, value);
3679 	if (ret) {
3680 		ath12k_warn(ar->ab, "failed to set vdev %d EHT MU mode: %d\n",
3681 			    arvif->vdev_id, ret);
3682 		return ret;
3683 	}
3684 
3685 	return 0;
3686 }
3687 
3688 static u32 ath12k_mac_ieee80211_sta_bw_to_wmi(struct ath12k *ar,
3689 					      struct ieee80211_link_sta *link_sta)
3690 {
3691 	u32 bw;
3692 
3693 	switch (link_sta->bandwidth) {
3694 	case IEEE80211_STA_RX_BW_20:
3695 		bw = WMI_PEER_CHWIDTH_20MHZ;
3696 		break;
3697 	case IEEE80211_STA_RX_BW_40:
3698 		bw = WMI_PEER_CHWIDTH_40MHZ;
3699 		break;
3700 	case IEEE80211_STA_RX_BW_80:
3701 		bw = WMI_PEER_CHWIDTH_80MHZ;
3702 		break;
3703 	case IEEE80211_STA_RX_BW_160:
3704 		bw = WMI_PEER_CHWIDTH_160MHZ;
3705 		break;
3706 	case IEEE80211_STA_RX_BW_320:
3707 		bw = WMI_PEER_CHWIDTH_320MHZ;
3708 		break;
3709 	default:
3710 		ath12k_warn(ar->ab, "Invalid bandwidth %d for link station %pM\n",
3711 			    link_sta->bandwidth, link_sta->addr);
3712 		bw = WMI_PEER_CHWIDTH_20MHZ;
3713 		break;
3714 	}
3715 
3716 	return bw;
3717 }
3718 
3719 static void ath12k_bss_assoc(struct ath12k *ar,
3720 			     struct ath12k_link_vif *arvif,
3721 			     struct ieee80211_bss_conf *bss_conf)
3722 {
3723 	struct ath12k_vif *ahvif = arvif->ahvif;
3724 	struct ieee80211_vif *vif = ath12k_ahvif_to_vif(ahvif);
3725 	struct ath12k_wmi_vdev_up_params params = {};
3726 	struct ieee80211_link_sta *link_sta;
3727 	u8 link_id = bss_conf->link_id;
3728 	struct ath12k_link_sta *arsta;
3729 	struct ieee80211_sta *ap_sta;
3730 	struct ath12k_sta *ahsta;
3731 	struct ath12k_peer *peer;
3732 	bool is_auth = false;
3733 	u32 hemode = 0;
3734 	int ret;
3735 
3736 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
3737 
3738 	struct ath12k_wmi_peer_assoc_arg *peer_arg __free(kfree) =
3739 					kzalloc(sizeof(*peer_arg), GFP_KERNEL);
3740 	if (!peer_arg)
3741 		return;
3742 
3743 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
3744 		   "mac vdev %i link id %u assoc bssid %pM aid %d\n",
3745 		   arvif->vdev_id, link_id, arvif->bssid, ahvif->aid);
3746 
3747 	rcu_read_lock();
3748 
3749 	/* During ML connection, cfg.ap_addr has the MLD address. For
3750 	 * non-ML connection, it has the BSSID.
3751 	 */
3752 	ap_sta = ieee80211_find_sta(vif, vif->cfg.ap_addr);
3753 	if (!ap_sta) {
3754 		ath12k_warn(ar->ab, "failed to find station entry for bss %pM vdev %i\n",
3755 			    vif->cfg.ap_addr, arvif->vdev_id);
3756 		rcu_read_unlock();
3757 		return;
3758 	}
3759 
3760 	ahsta = ath12k_sta_to_ahsta(ap_sta);
3761 
3762 	arsta = wiphy_dereference(ath12k_ar_to_hw(ar)->wiphy,
3763 				  ahsta->link[link_id]);
3764 	if (WARN_ON(!arsta)) {
3765 		rcu_read_unlock();
3766 		return;
3767 	}
3768 
3769 	link_sta = ath12k_mac_get_link_sta(arsta);
3770 	if (WARN_ON(!link_sta)) {
3771 		rcu_read_unlock();
3772 		return;
3773 	}
3774 
3775 	ath12k_peer_assoc_prepare(ar, arvif, arsta, peer_arg, false);
3776 
3777 	/* link_sta->he_cap must be protected by rcu_read_lock */
3778 	ret = ath12k_mac_vif_recalc_sta_he_txbf(ar, arvif, &link_sta->he_cap, &hemode);
3779 	if (ret) {
3780 		ath12k_warn(ar->ab, "failed to recalc he txbf for vdev %i on bss %pM: %d\n",
3781 			    arvif->vdev_id, bss_conf->bssid, ret);
3782 		rcu_read_unlock();
3783 		return;
3784 	}
3785 
3786 	rcu_read_unlock();
3787 
3788 	/* keep this before ath12k_wmi_send_peer_assoc_cmd() */
3789 	ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
3790 					    WMI_VDEV_PARAM_SET_HEMU_MODE, hemode);
3791 	if (ret) {
3792 		ath12k_warn(ar->ab, "failed to submit vdev param txbf 0x%x: %d\n",
3793 			    hemode, ret);
3794 		return;
3795 	}
3796 
3797 	peer_arg->is_assoc = true;
3798 	ret = ath12k_wmi_send_peer_assoc_cmd(ar, peer_arg);
3799 	if (ret) {
3800 		ath12k_warn(ar->ab, "failed to run peer assoc for %pM vdev %i: %d\n",
3801 			    bss_conf->bssid, arvif->vdev_id, ret);
3802 		return;
3803 	}
3804 
3805 	if (!wait_for_completion_timeout(&ar->peer_assoc_done, 1 * HZ)) {
3806 		ath12k_warn(ar->ab, "failed to get peer assoc conf event for %pM vdev %i\n",
3807 			    bss_conf->bssid, arvif->vdev_id);
3808 		return;
3809 	}
3810 
3811 	ret = ath12k_setup_peer_smps(ar, arvif, bss_conf->bssid,
3812 				     &link_sta->ht_cap, &link_sta->he_6ghz_capa);
3813 	if (ret) {
3814 		ath12k_warn(ar->ab, "failed to setup peer SMPS for vdev %d: %d\n",
3815 			    arvif->vdev_id, ret);
3816 		return;
3817 	}
3818 
3819 	WARN_ON(arvif->is_up);
3820 
3821 	ahvif->aid = vif->cfg.aid;
3822 	ether_addr_copy(arvif->bssid, bss_conf->bssid);
3823 
3824 	params.vdev_id = arvif->vdev_id;
3825 	params.aid = ahvif->aid;
3826 	params.bssid = arvif->bssid;
3827 	params.tx_bssid = ath12k_mac_get_tx_bssid(arvif);
3828 	if (params.tx_bssid) {
3829 		params.nontx_profile_idx = bss_conf->bssid_index;
3830 		params.nontx_profile_cnt = 1 << bss_conf->bssid_indicator;
3831 	}
3832 	ret = ath12k_wmi_vdev_up(ar, &params);
3833 	if (ret) {
3834 		ath12k_warn(ar->ab, "failed to set vdev %d up: %d\n",
3835 			    arvif->vdev_id, ret);
3836 		return;
3837 	}
3838 
3839 	arvif->is_up = true;
3840 	arvif->rekey_data.enable_offload = false;
3841 
3842 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
3843 		   "mac vdev %d up (associated) bssid %pM aid %d\n",
3844 		   arvif->vdev_id, bss_conf->bssid, vif->cfg.aid);
3845 
3846 	spin_lock_bh(&ar->ab->base_lock);
3847 
3848 	peer = ath12k_peer_find(ar->ab, arvif->vdev_id, arvif->bssid);
3849 	if (peer && peer->is_authorized)
3850 		is_auth = true;
3851 
3852 	spin_unlock_bh(&ar->ab->base_lock);
3853 
3854 	/* Authorize BSS Peer */
3855 	if (is_auth) {
3856 		ret = ath12k_wmi_set_peer_param(ar, arvif->bssid,
3857 						arvif->vdev_id,
3858 						WMI_PEER_AUTHORIZE,
3859 						1);
3860 		if (ret)
3861 			ath12k_warn(ar->ab, "Unable to authorize BSS peer: %d\n", ret);
3862 	}
3863 
3864 	ret = ath12k_wmi_send_obss_spr_cmd(ar, arvif->vdev_id,
3865 					   &bss_conf->he_obss_pd);
3866 	if (ret)
3867 		ath12k_warn(ar->ab, "failed to set vdev %i OBSS PD parameters: %d\n",
3868 			    arvif->vdev_id, ret);
3869 
3870 	if (test_bit(WMI_TLV_SERVICE_11D_OFFLOAD, ar->ab->wmi_ab.svc_map) &&
3871 	    ahvif->vdev_type == WMI_VDEV_TYPE_STA &&
3872 	    ahvif->vdev_subtype == WMI_VDEV_SUBTYPE_NONE)
3873 		ath12k_mac_11d_scan_stop_all(ar->ab);
3874 }
3875 
3876 static void ath12k_bss_disassoc(struct ath12k *ar,
3877 				struct ath12k_link_vif *arvif)
3878 {
3879 	int ret;
3880 
3881 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
3882 
3883 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac vdev %i disassoc bssid %pM\n",
3884 		   arvif->vdev_id, arvif->bssid);
3885 
3886 	ret = ath12k_wmi_vdev_down(ar, arvif->vdev_id);
3887 	if (ret)
3888 		ath12k_warn(ar->ab, "failed to down vdev %i: %d\n",
3889 			    arvif->vdev_id, ret);
3890 
3891 	arvif->is_up = false;
3892 
3893 	memset(&arvif->rekey_data, 0, sizeof(arvif->rekey_data));
3894 
3895 	cancel_delayed_work(&arvif->connection_loss_work);
3896 }
3897 
3898 static u32 ath12k_mac_get_rate_hw_value(int bitrate)
3899 {
3900 	u32 preamble;
3901 	u16 hw_value;
3902 	int rate;
3903 	size_t i;
3904 
3905 	if (ath12k_mac_bitrate_is_cck(bitrate))
3906 		preamble = WMI_RATE_PREAMBLE_CCK;
3907 	else
3908 		preamble = WMI_RATE_PREAMBLE_OFDM;
3909 
3910 	for (i = 0; i < ARRAY_SIZE(ath12k_legacy_rates); i++) {
3911 		if (ath12k_legacy_rates[i].bitrate != bitrate)
3912 			continue;
3913 
3914 		hw_value = ath12k_legacy_rates[i].hw_value;
3915 		rate = ATH12K_HW_RATE_CODE(hw_value, 0, preamble);
3916 
3917 		return rate;
3918 	}
3919 
3920 	return -EINVAL;
3921 }
3922 
3923 static void ath12k_recalculate_mgmt_rate(struct ath12k *ar,
3924 					 struct ath12k_link_vif *arvif,
3925 					 struct cfg80211_chan_def *def)
3926 {
3927 	struct ieee80211_vif *vif = ath12k_ahvif_to_vif(arvif->ahvif);
3928 	struct ieee80211_hw *hw = ath12k_ar_to_hw(ar);
3929 	const struct ieee80211_supported_band *sband;
3930 	struct ieee80211_bss_conf *bss_conf;
3931 	u8 basic_rate_idx;
3932 	int hw_rate_code;
3933 	u32 vdev_param;
3934 	u16 bitrate;
3935 	int ret;
3936 
3937 	lockdep_assert_wiphy(hw->wiphy);
3938 
3939 	bss_conf = ath12k_mac_get_link_bss_conf(arvif);
3940 	if (!bss_conf) {
3941 		ath12k_warn(ar->ab, "unable to access bss link conf in mgmt rate calc for vif %pM link %u\n",
3942 			    vif->addr, arvif->link_id);
3943 		return;
3944 	}
3945 
3946 	sband = hw->wiphy->bands[def->chan->band];
3947 	if (bss_conf->basic_rates)
3948 		basic_rate_idx = __ffs(bss_conf->basic_rates);
3949 	else
3950 		basic_rate_idx = 0;
3951 	bitrate = sband->bitrates[basic_rate_idx].bitrate;
3952 
3953 	hw_rate_code = ath12k_mac_get_rate_hw_value(bitrate);
3954 	if (hw_rate_code < 0) {
3955 		ath12k_warn(ar->ab, "bitrate not supported %d\n", bitrate);
3956 		return;
3957 	}
3958 
3959 	vdev_param = WMI_VDEV_PARAM_MGMT_RATE;
3960 	ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id, vdev_param,
3961 					    hw_rate_code);
3962 	if (ret)
3963 		ath12k_warn(ar->ab, "failed to set mgmt tx rate %d\n", ret);
3964 
3965 	vdev_param = WMI_VDEV_PARAM_BEACON_RATE;
3966 	ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id, vdev_param,
3967 					    hw_rate_code);
3968 	if (ret)
3969 		ath12k_warn(ar->ab, "failed to set beacon tx rate %d\n", ret);
3970 }
3971 
3972 static void ath12k_mac_bcn_tx_event(struct ath12k_link_vif *arvif)
3973 {
3974 	struct ieee80211_vif *vif = ath12k_ahvif_to_vif(arvif->ahvif);
3975 	struct ieee80211_bss_conf *link_conf;
3976 
3977 	link_conf = ath12k_mac_get_link_bss_conf(arvif);
3978 	if (!link_conf) {
3979 		ath12k_warn(arvif->ar->ab, "failed to get link conf for vdev %u\n",
3980 			    arvif->vdev_id);
3981 		return;
3982 	}
3983 
3984 	if (link_conf->color_change_active) {
3985 		if (ieee80211_beacon_cntdwn_is_complete(vif, arvif->link_id)) {
3986 			ieee80211_color_change_finish(vif, arvif->link_id);
3987 			return;
3988 		}
3989 
3990 		ieee80211_beacon_update_cntdwn(vif, arvif->link_id);
3991 		ath12k_mac_setup_bcn_tmpl(arvif);
3992 	}
3993 }
3994 
3995 static void ath12k_mac_bcn_tx_work(struct wiphy *wiphy, struct wiphy_work *work)
3996 {
3997 	struct ath12k_link_vif *arvif = container_of(work, struct ath12k_link_vif,
3998 						     bcn_tx_work);
3999 
4000 	lockdep_assert_wiphy(wiphy);
4001 	ath12k_mac_bcn_tx_event(arvif);
4002 }
4003 
4004 static void ath12k_mac_init_arvif(struct ath12k_vif *ahvif,
4005 				  struct ath12k_link_vif *arvif, int link_id)
4006 {
4007 	struct ath12k_hw *ah = ahvif->ah;
4008 	u8 _link_id;
4009 	int i;
4010 
4011 	lockdep_assert_wiphy(ah->hw->wiphy);
4012 
4013 	if (WARN_ON(!arvif))
4014 		return;
4015 
4016 	if (WARN_ON(link_id >= ATH12K_NUM_MAX_LINKS))
4017 		return;
4018 
4019 	if (link_id < 0)
4020 		_link_id = 0;
4021 	else
4022 		_link_id = link_id;
4023 
4024 	arvif->ahvif = ahvif;
4025 	arvif->link_id = _link_id;
4026 
4027 	/* Protects the datapath stats update on a per link basis */
4028 	spin_lock_init(&arvif->link_stats_lock);
4029 
4030 	INIT_LIST_HEAD(&arvif->list);
4031 	INIT_DELAYED_WORK(&arvif->connection_loss_work,
4032 			  ath12k_mac_vif_sta_connection_loss_work);
4033 	wiphy_work_init(&arvif->bcn_tx_work, ath12k_mac_bcn_tx_work);
4034 
4035 	arvif->num_stations = 0;
4036 
4037 	for (i = 0; i < ARRAY_SIZE(arvif->bitrate_mask.control); i++) {
4038 		arvif->bitrate_mask.control[i].legacy = 0xffffffff;
4039 		arvif->bitrate_mask.control[i].gi = NL80211_TXRATE_DEFAULT_GI;
4040 		memset(arvif->bitrate_mask.control[i].ht_mcs, 0xff,
4041 		       sizeof(arvif->bitrate_mask.control[i].ht_mcs));
4042 		memset(arvif->bitrate_mask.control[i].vht_mcs, 0xff,
4043 		       sizeof(arvif->bitrate_mask.control[i].vht_mcs));
4044 		memset(arvif->bitrate_mask.control[i].he_mcs, 0xff,
4045 		       sizeof(arvif->bitrate_mask.control[i].he_mcs));
4046 		memset(arvif->bitrate_mask.control[i].eht_mcs, 0xff,
4047 		       sizeof(arvif->bitrate_mask.control[i].eht_mcs));
4048 	}
4049 
4050 	/* Handle MLO related assignments */
4051 	if (link_id >= 0) {
4052 		rcu_assign_pointer(ahvif->link[arvif->link_id], arvif);
4053 		ahvif->links_map |= BIT(_link_id);
4054 	}
4055 
4056 	ath12k_generic_dbg(ATH12K_DBG_MAC,
4057 			   "mac init link arvif (link_id %d%s) for vif %pM. links_map 0x%x",
4058 			   _link_id, (link_id < 0) ? " deflink" : "", ahvif->vif->addr,
4059 			   ahvif->links_map);
4060 }
4061 
4062 static void ath12k_mac_remove_link_interface(struct ieee80211_hw *hw,
4063 					     struct ath12k_link_vif *arvif)
4064 {
4065 	struct ath12k_vif *ahvif = arvif->ahvif;
4066 	struct ath12k_hw *ah = hw->priv;
4067 	struct ath12k *ar = arvif->ar;
4068 	int ret;
4069 
4070 	lockdep_assert_wiphy(ah->hw->wiphy);
4071 
4072 	cancel_delayed_work_sync(&arvif->connection_loss_work);
4073 	wiphy_work_cancel(ath12k_ar_to_hw(ar)->wiphy, &arvif->bcn_tx_work);
4074 
4075 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac remove link interface (vdev %d link id %d)",
4076 		   arvif->vdev_id, arvif->link_id);
4077 
4078 	if (test_bit(WMI_TLV_SERVICE_11D_OFFLOAD, ar->ab->wmi_ab.svc_map) &&
4079 	    ahvif->vdev_type == WMI_VDEV_TYPE_STA &&
4080 	    ahvif->vdev_subtype == WMI_VDEV_SUBTYPE_NONE)
4081 		ath12k_mac_11d_scan_stop(ar);
4082 
4083 	if (ahvif->vdev_type == WMI_VDEV_TYPE_AP) {
4084 		ret = ath12k_peer_delete(ar, arvif->vdev_id, arvif->bssid);
4085 		if (ret)
4086 			ath12k_warn(ar->ab, "failed to submit AP self-peer removal on vdev %d link id %d: %d",
4087 				    arvif->vdev_id, arvif->link_id, ret);
4088 	}
4089 	ath12k_mac_vdev_delete(ar, arvif);
4090 }
4091 
4092 static struct ath12k_link_vif *ath12k_mac_assign_link_vif(struct ath12k_hw *ah,
4093 							  struct ieee80211_vif *vif,
4094 							  u8 link_id)
4095 {
4096 	struct ath12k_vif *ahvif = ath12k_vif_to_ahvif(vif);
4097 	struct ath12k_link_vif *arvif;
4098 
4099 	lockdep_assert_wiphy(ah->hw->wiphy);
4100 
4101 	arvif = wiphy_dereference(ah->hw->wiphy, ahvif->link[link_id]);
4102 	if (arvif)
4103 		return arvif;
4104 
4105 	/* If this is the first link arvif being created for an ML VIF
4106 	 * use the preallocated deflink memory except for scan arvifs
4107 	 */
4108 	if (!ahvif->links_map && link_id < ATH12K_FIRST_SCAN_LINK) {
4109 		arvif = &ahvif->deflink;
4110 
4111 		if (vif->type == NL80211_IFTYPE_STATION)
4112 			arvif->is_sta_assoc_link = true;
4113 	} else {
4114 		arvif = kzalloc(sizeof(*arvif), GFP_KERNEL);
4115 		if (!arvif)
4116 			return NULL;
4117 	}
4118 
4119 	ath12k_mac_init_arvif(ahvif, arvif, link_id);
4120 
4121 	return arvif;
4122 }
4123 
4124 static void ath12k_mac_unassign_link_vif(struct ath12k_link_vif *arvif)
4125 {
4126 	struct ath12k_vif *ahvif = arvif->ahvif;
4127 	struct ath12k_hw *ah = ahvif->ah;
4128 
4129 	lockdep_assert_wiphy(ah->hw->wiphy);
4130 
4131 	rcu_assign_pointer(ahvif->link[arvif->link_id], NULL);
4132 	synchronize_rcu();
4133 	ahvif->links_map &= ~BIT(arvif->link_id);
4134 
4135 	if (arvif != &ahvif->deflink)
4136 		kfree(arvif);
4137 	else
4138 		memset(arvif, 0, sizeof(*arvif));
4139 }
4140 
4141 static int
4142 ath12k_mac_op_change_vif_links(struct ieee80211_hw *hw,
4143 			       struct ieee80211_vif *vif,
4144 			       u16 old_links, u16 new_links,
4145 			       struct ieee80211_bss_conf *ol[IEEE80211_MLD_MAX_NUM_LINKS])
4146 {
4147 	struct ath12k_vif *ahvif = ath12k_vif_to_ahvif(vif);
4148 	unsigned long to_remove = old_links & ~new_links;
4149 	unsigned long to_add = ~old_links & new_links;
4150 	struct ath12k_hw *ah = ath12k_hw_to_ah(hw);
4151 	struct ath12k_link_vif *arvif;
4152 	u8 link_id;
4153 
4154 	lockdep_assert_wiphy(hw->wiphy);
4155 
4156 	ath12k_generic_dbg(ATH12K_DBG_MAC,
4157 			   "mac vif link changed for MLD %pM old_links 0x%x new_links 0x%x\n",
4158 			   vif->addr, old_links, new_links);
4159 
4160 	for_each_set_bit(link_id, &to_add, IEEE80211_MLD_MAX_NUM_LINKS) {
4161 		arvif = wiphy_dereference(hw->wiphy, ahvif->link[link_id]);
4162 		/* mac80211 wants to add link but driver already has the
4163 		 * link. This should not happen ideally.
4164 		 */
4165 		if (WARN_ON(arvif))
4166 			return -EINVAL;
4167 
4168 		arvif = ath12k_mac_assign_link_vif(ah, vif, link_id);
4169 		if (WARN_ON(!arvif))
4170 			return -EINVAL;
4171 	}
4172 
4173 	for_each_set_bit(link_id, &to_remove, IEEE80211_MLD_MAX_NUM_LINKS) {
4174 		arvif = wiphy_dereference(hw->wiphy, ahvif->link[link_id]);
4175 		if (WARN_ON(!arvif))
4176 			return -EINVAL;
4177 
4178 		if (!arvif->is_created)
4179 			continue;
4180 
4181 		if (WARN_ON(!arvif->ar))
4182 			return -EINVAL;
4183 
4184 		ath12k_mac_remove_link_interface(hw, arvif);
4185 		ath12k_mac_unassign_link_vif(arvif);
4186 	}
4187 
4188 	return 0;
4189 }
4190 
4191 static int ath12k_mac_fils_discovery(struct ath12k_link_vif *arvif,
4192 				     struct ieee80211_bss_conf *info)
4193 {
4194 	struct ieee80211_vif *vif = ath12k_ahvif_to_vif(arvif->ahvif);
4195 	struct ath12k *ar = arvif->ar;
4196 	struct ieee80211_hw *hw = ath12k_ar_to_hw(ar);
4197 	struct sk_buff *tmpl;
4198 	int ret;
4199 	u32 interval;
4200 	bool unsol_bcast_probe_resp_enabled = false;
4201 
4202 	if (info->fils_discovery.max_interval) {
4203 		interval = info->fils_discovery.max_interval;
4204 
4205 		tmpl = ieee80211_get_fils_discovery_tmpl(hw, vif);
4206 		if (tmpl)
4207 			ret = ath12k_wmi_fils_discovery_tmpl(ar, arvif->vdev_id,
4208 							     tmpl);
4209 	} else if (info->unsol_bcast_probe_resp_interval) {
4210 		unsol_bcast_probe_resp_enabled = 1;
4211 		interval = info->unsol_bcast_probe_resp_interval;
4212 
4213 		tmpl = ieee80211_get_unsol_bcast_probe_resp_tmpl(hw, vif);
4214 		if (tmpl)
4215 			ret = ath12k_wmi_probe_resp_tmpl(ar, arvif->vdev_id,
4216 							 tmpl);
4217 	} else { /* Disable */
4218 		return ath12k_wmi_fils_discovery(ar, arvif->vdev_id, 0, false);
4219 	}
4220 
4221 	if (!tmpl) {
4222 		ath12k_warn(ar->ab,
4223 			    "mac vdev %i failed to retrieve %s template\n",
4224 			    arvif->vdev_id, (unsol_bcast_probe_resp_enabled ?
4225 			    "unsolicited broadcast probe response" :
4226 			    "FILS discovery"));
4227 		return -EPERM;
4228 	}
4229 	kfree_skb(tmpl);
4230 
4231 	if (!ret)
4232 		ret = ath12k_wmi_fils_discovery(ar, arvif->vdev_id, interval,
4233 						unsol_bcast_probe_resp_enabled);
4234 
4235 	return ret;
4236 }
4237 
4238 static void ath12k_mac_op_vif_cfg_changed(struct ieee80211_hw *hw,
4239 					  struct ieee80211_vif *vif,
4240 					  u64 changed)
4241 {
4242 	struct ath12k_vif *ahvif = ath12k_vif_to_ahvif(vif);
4243 	unsigned long links = ahvif->links_map;
4244 	struct ieee80211_bss_conf *info;
4245 	struct ath12k_link_vif *arvif;
4246 	struct ieee80211_sta *sta;
4247 	struct ath12k_sta *ahsta;
4248 	struct ath12k *ar;
4249 	u8 link_id;
4250 
4251 	lockdep_assert_wiphy(hw->wiphy);
4252 
4253 	if (changed & BSS_CHANGED_SSID && vif->type == NL80211_IFTYPE_AP) {
4254 		ahvif->u.ap.ssid_len = vif->cfg.ssid_len;
4255 		if (vif->cfg.ssid_len)
4256 			memcpy(ahvif->u.ap.ssid, vif->cfg.ssid, vif->cfg.ssid_len);
4257 	}
4258 
4259 	if (changed & BSS_CHANGED_ASSOC) {
4260 		if (vif->cfg.assoc) {
4261 			/* only in station mode we can get here, so it's safe
4262 			 * to use ap_addr
4263 			 */
4264 			rcu_read_lock();
4265 			sta = ieee80211_find_sta(vif, vif->cfg.ap_addr);
4266 			if (!sta) {
4267 				rcu_read_unlock();
4268 				WARN_ONCE(1, "failed to find sta with addr %pM\n",
4269 					  vif->cfg.ap_addr);
4270 				return;
4271 			}
4272 
4273 			ahsta = ath12k_sta_to_ahsta(sta);
4274 			arvif = wiphy_dereference(hw->wiphy,
4275 						  ahvif->link[ahsta->assoc_link_id]);
4276 			rcu_read_unlock();
4277 
4278 			ar = arvif->ar;
4279 			/* there is no reason for which an assoc link's
4280 			 * bss info does not exist
4281 			 */
4282 			info = ath12k_mac_get_link_bss_conf(arvif);
4283 			ath12k_bss_assoc(ar, arvif, info);
4284 
4285 			/* exclude assoc link as it is done above */
4286 			links &= ~BIT(ahsta->assoc_link_id);
4287 		}
4288 
4289 		for_each_set_bit(link_id, &links, IEEE80211_MLD_MAX_NUM_LINKS) {
4290 			arvif = wiphy_dereference(hw->wiphy, ahvif->link[link_id]);
4291 			if (!arvif || !arvif->ar)
4292 				continue;
4293 
4294 			ar = arvif->ar;
4295 
4296 			if (vif->cfg.assoc) {
4297 				info = ath12k_mac_get_link_bss_conf(arvif);
4298 				if (!info)
4299 					continue;
4300 
4301 				ath12k_bss_assoc(ar, arvif, info);
4302 			} else {
4303 				ath12k_bss_disassoc(ar, arvif);
4304 			}
4305 		}
4306 	}
4307 }
4308 
4309 static void ath12k_mac_vif_setup_ps(struct ath12k_link_vif *arvif)
4310 {
4311 	struct ath12k *ar = arvif->ar;
4312 	struct ieee80211_vif *vif = arvif->ahvif->vif;
4313 	struct ieee80211_conf *conf = &ath12k_ar_to_hw(ar)->conf;
4314 	enum wmi_sta_powersave_param param;
4315 	struct ieee80211_bss_conf *info;
4316 	enum wmi_sta_ps_mode psmode;
4317 	int ret;
4318 	int timeout;
4319 	bool enable_ps;
4320 
4321 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
4322 
4323 	if (vif->type != NL80211_IFTYPE_STATION)
4324 		return;
4325 
4326 	enable_ps = arvif->ahvif->ps;
4327 	if (enable_ps) {
4328 		psmode = WMI_STA_PS_MODE_ENABLED;
4329 		param = WMI_STA_PS_PARAM_INACTIVITY_TIME;
4330 
4331 		timeout = conf->dynamic_ps_timeout;
4332 		if (timeout == 0) {
4333 			info = ath12k_mac_get_link_bss_conf(arvif);
4334 			if (!info) {
4335 				ath12k_warn(ar->ab, "unable to access bss link conf in setup ps for vif %pM link %u\n",
4336 					    vif->addr, arvif->link_id);
4337 				return;
4338 			}
4339 
4340 			/* firmware doesn't like 0 */
4341 			timeout = ieee80211_tu_to_usec(info->beacon_int) / 1000;
4342 		}
4343 
4344 		ret = ath12k_wmi_set_sta_ps_param(ar, arvif->vdev_id, param,
4345 						  timeout);
4346 		if (ret) {
4347 			ath12k_warn(ar->ab, "failed to set inactivity time for vdev %d: %i\n",
4348 				    arvif->vdev_id, ret);
4349 			return;
4350 		}
4351 	} else {
4352 		psmode = WMI_STA_PS_MODE_DISABLED;
4353 	}
4354 
4355 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac vdev %d psmode %s\n",
4356 		   arvif->vdev_id, psmode ? "enable" : "disable");
4357 
4358 	ret = ath12k_wmi_pdev_set_ps_mode(ar, arvif->vdev_id, psmode);
4359 	if (ret)
4360 		ath12k_warn(ar->ab, "failed to set sta power save mode %d for vdev %d: %d\n",
4361 			    psmode, arvif->vdev_id, ret);
4362 }
4363 
4364 static bool ath12k_mac_supports_tpc(struct ath12k *ar, struct ath12k_vif *ahvif,
4365 				    const struct cfg80211_chan_def *chandef)
4366 {
4367 	return ath12k_wmi_supports_6ghz_cc_ext(ar) &&
4368 		test_bit(WMI_TLV_SERVICE_EXT_TPC_REG_SUPPORT, ar->ab->wmi_ab.svc_map) &&
4369 		(ahvif->vdev_type == WMI_VDEV_TYPE_STA  ||
4370 		 ahvif->vdev_type == WMI_VDEV_TYPE_AP) &&
4371 		ahvif->vdev_subtype == WMI_VDEV_SUBTYPE_NONE &&
4372 		chandef->chan &&
4373 		chandef->chan->band == NL80211_BAND_6GHZ;
4374 }
4375 
4376 static void ath12k_wmi_vdev_params_up(struct ath12k *ar,
4377 				      struct ath12k_link_vif *arvif,
4378 				      struct ath12k_link_vif *tx_arvif,
4379 				      struct ieee80211_bss_conf *info, u16 aid)
4380 {
4381 	struct ath12k_wmi_vdev_up_params params = {
4382 		.vdev_id = arvif->vdev_id,
4383 		.aid = aid,
4384 		.bssid = arvif->bssid
4385 	};
4386 	int ret;
4387 
4388 	if (tx_arvif) {
4389 		params.tx_bssid = tx_arvif->bssid;
4390 		params.nontx_profile_idx = info->bssid_index;
4391 		params.nontx_profile_cnt = 1 << info->bssid_indicator;
4392 	}
4393 
4394 	ret = ath12k_wmi_vdev_up(arvif->ar, &params);
4395 	if (ret)
4396 		ath12k_warn(ar->ab, "failed to bring vdev up %d: %d\n",
4397 			    arvif->vdev_id, ret);
4398 }
4399 
4400 static void ath12k_mac_bss_info_changed(struct ath12k *ar,
4401 					struct ath12k_link_vif *arvif,
4402 					struct ieee80211_bss_conf *info,
4403 					u64 changed)
4404 {
4405 	struct ath12k_vif *ahvif = arvif->ahvif;
4406 	struct ieee80211_vif *vif = ath12k_ahvif_to_vif(ahvif);
4407 	struct ieee80211_vif_cfg *vif_cfg = &vif->cfg;
4408 	struct ath12k_link_vif *tx_arvif;
4409 	struct cfg80211_chan_def def;
4410 	u32 param_id, param_value;
4411 	enum nl80211_band band;
4412 	u32 vdev_param;
4413 	int mcast_rate;
4414 	u32 preamble;
4415 	u16 hw_value;
4416 	u16 bitrate;
4417 	u8 rateidx;
4418 	u32 rate;
4419 	int ret;
4420 
4421 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
4422 
4423 	if (changed & BSS_CHANGED_BEACON_INT) {
4424 		arvif->beacon_interval = info->beacon_int;
4425 
4426 		param_id = WMI_VDEV_PARAM_BEACON_INTERVAL;
4427 		ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
4428 						    param_id,
4429 						    arvif->beacon_interval);
4430 		if (ret)
4431 			ath12k_warn(ar->ab, "Failed to set beacon interval for VDEV: %d\n",
4432 				    arvif->vdev_id);
4433 		else
4434 			ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
4435 				   "Beacon interval: %d set for VDEV: %d\n",
4436 				   arvif->beacon_interval, arvif->vdev_id);
4437 	}
4438 
4439 	if (changed & BSS_CHANGED_BEACON) {
4440 		param_id = WMI_PDEV_PARAM_BEACON_TX_MODE;
4441 		param_value = WMI_BEACON_BURST_MODE;
4442 		ret = ath12k_wmi_pdev_set_param(ar, param_id,
4443 						param_value, ar->pdev->pdev_id);
4444 		if (ret)
4445 			ath12k_warn(ar->ab, "Failed to set beacon mode for VDEV: %d\n",
4446 				    arvif->vdev_id);
4447 		else
4448 			ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
4449 				   "Set burst beacon mode for VDEV: %d\n",
4450 				   arvif->vdev_id);
4451 
4452 		/* In MBSSID case, need to install transmitting VIF's template first */
4453 
4454 		ret = ath12k_mac_setup_bcn_tmpl(arvif);
4455 		if (ret)
4456 			ath12k_warn(ar->ab, "failed to update bcn template: %d\n",
4457 				    ret);
4458 
4459 		if (!arvif->is_csa_in_progress)
4460 			goto skip_vdev_up;
4461 
4462 		tx_arvif = ath12k_mac_get_tx_arvif(arvif, info);
4463 		if (tx_arvif && arvif != tx_arvif && tx_arvif->is_csa_in_progress)
4464 			/* skip non tx vif's */
4465 			goto skip_vdev_up;
4466 
4467 		ath12k_wmi_vdev_params_up(ar, arvif, tx_arvif, info, ahvif->aid);
4468 
4469 		arvif->is_csa_in_progress = false;
4470 
4471 		if (tx_arvif && arvif == tx_arvif) {
4472 			struct ath12k_link_vif *arvif_itr;
4473 
4474 			list_for_each_entry(arvif_itr, &ar->arvifs, list) {
4475 				if (!arvif_itr->is_csa_in_progress)
4476 					continue;
4477 
4478 				ath12k_wmi_vdev_params_up(ar, arvif, tx_arvif,
4479 							  info, ahvif->aid);
4480 				arvif_itr->is_csa_in_progress = false;
4481 			}
4482 		}
4483 	}
4484 
4485 skip_vdev_up:
4486 
4487 	if (changed & (BSS_CHANGED_BEACON_INFO | BSS_CHANGED_BEACON)) {
4488 		arvif->dtim_period = info->dtim_period;
4489 
4490 		param_id = WMI_VDEV_PARAM_DTIM_PERIOD;
4491 		ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
4492 						    param_id,
4493 						    arvif->dtim_period);
4494 
4495 		if (ret)
4496 			ath12k_warn(ar->ab, "Failed to set dtim period for VDEV %d: %i\n",
4497 				    arvif->vdev_id, ret);
4498 		else
4499 			ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
4500 				   "DTIM period: %d set for VDEV: %d\n",
4501 				   arvif->dtim_period, arvif->vdev_id);
4502 	}
4503 
4504 	if (changed & BSS_CHANGED_SSID &&
4505 	    vif->type == NL80211_IFTYPE_AP) {
4506 		ahvif->u.ap.ssid_len = vif->cfg.ssid_len;
4507 		if (vif->cfg.ssid_len)
4508 			memcpy(ahvif->u.ap.ssid, vif->cfg.ssid, vif->cfg.ssid_len);
4509 		ahvif->u.ap.hidden_ssid = info->hidden_ssid;
4510 	}
4511 
4512 	if (changed & BSS_CHANGED_BSSID && !is_zero_ether_addr(info->bssid))
4513 		ether_addr_copy(arvif->bssid, info->bssid);
4514 
4515 	if (changed & BSS_CHANGED_BEACON_ENABLED) {
4516 		if (info->enable_beacon) {
4517 			ret = ath12k_mac_set_he_txbf_conf(arvif);
4518 			if (ret)
4519 				ath12k_warn(ar->ab,
4520 					    "failed to set HE TXBF config for vdev: %d\n",
4521 					    arvif->vdev_id);
4522 
4523 			ret = ath12k_mac_set_eht_txbf_conf(arvif);
4524 			if (ret)
4525 				ath12k_warn(ar->ab,
4526 					    "failed to set EHT TXBF config for vdev: %d\n",
4527 					    arvif->vdev_id);
4528 		}
4529 		ath12k_control_beaconing(arvif, info);
4530 
4531 		if (arvif->is_up && info->he_support &&
4532 		    info->he_oper.params) {
4533 			/* TODO: Extend to support 1024 BA Bitmap size */
4534 			ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
4535 							    WMI_VDEV_PARAM_BA_MODE,
4536 							    WMI_BA_MODE_BUFFER_SIZE_256);
4537 			if (ret)
4538 				ath12k_warn(ar->ab,
4539 					    "failed to set BA BUFFER SIZE 256 for vdev: %d\n",
4540 					    arvif->vdev_id);
4541 
4542 			param_id = WMI_VDEV_PARAM_HEOPS_0_31;
4543 			param_value = info->he_oper.params;
4544 			ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
4545 							    param_id, param_value);
4546 			ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
4547 				   "he oper param: %x set for VDEV: %d\n",
4548 				   param_value, arvif->vdev_id);
4549 
4550 			if (ret)
4551 				ath12k_warn(ar->ab, "Failed to set he oper params %x for VDEV %d: %i\n",
4552 					    param_value, arvif->vdev_id, ret);
4553 		}
4554 	}
4555 
4556 	if (changed & BSS_CHANGED_ERP_CTS_PROT) {
4557 		u32 cts_prot;
4558 
4559 		cts_prot = !!(info->use_cts_prot);
4560 		param_id = WMI_VDEV_PARAM_PROTECTION_MODE;
4561 
4562 		if (arvif->is_started) {
4563 			ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
4564 							    param_id, cts_prot);
4565 			if (ret)
4566 				ath12k_warn(ar->ab, "Failed to set CTS prot for VDEV: %d\n",
4567 					    arvif->vdev_id);
4568 			else
4569 				ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "Set CTS prot: %d for VDEV: %d\n",
4570 					   cts_prot, arvif->vdev_id);
4571 		} else {
4572 			ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "defer protection mode setup, vdev is not ready yet\n");
4573 		}
4574 	}
4575 
4576 	if (changed & BSS_CHANGED_ERP_SLOT) {
4577 		u32 slottime;
4578 
4579 		if (info->use_short_slot)
4580 			slottime = WMI_VDEV_SLOT_TIME_SHORT; /* 9us */
4581 
4582 		else
4583 			slottime = WMI_VDEV_SLOT_TIME_LONG; /* 20us */
4584 
4585 		param_id = WMI_VDEV_PARAM_SLOT_TIME;
4586 		ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
4587 						    param_id, slottime);
4588 		if (ret)
4589 			ath12k_warn(ar->ab, "Failed to set erp slot for VDEV: %d\n",
4590 				    arvif->vdev_id);
4591 		else
4592 			ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
4593 				   "Set slottime: %d for VDEV: %d\n",
4594 				   slottime, arvif->vdev_id);
4595 	}
4596 
4597 	if (changed & BSS_CHANGED_ERP_PREAMBLE) {
4598 		u32 preamble;
4599 
4600 		if (info->use_short_preamble)
4601 			preamble = WMI_VDEV_PREAMBLE_SHORT;
4602 		else
4603 			preamble = WMI_VDEV_PREAMBLE_LONG;
4604 
4605 		param_id = WMI_VDEV_PARAM_PREAMBLE;
4606 		ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
4607 						    param_id, preamble);
4608 		if (ret)
4609 			ath12k_warn(ar->ab, "Failed to set preamble for VDEV: %d\n",
4610 				    arvif->vdev_id);
4611 		else
4612 			ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
4613 				   "Set preamble: %d for VDEV: %d\n",
4614 				   preamble, arvif->vdev_id);
4615 	}
4616 
4617 	if (changed & BSS_CHANGED_ASSOC) {
4618 		if (vif->cfg.assoc)
4619 			ath12k_bss_assoc(ar, arvif, info);
4620 		else
4621 			ath12k_bss_disassoc(ar, arvif);
4622 	}
4623 
4624 	if (changed & BSS_CHANGED_TXPOWER) {
4625 		ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac vdev_id %i txpower %d\n",
4626 			   arvif->vdev_id, info->txpower);
4627 
4628 		arvif->txpower = info->txpower;
4629 		ath12k_mac_txpower_recalc(ar);
4630 	}
4631 
4632 	if (changed & BSS_CHANGED_MCAST_RATE &&
4633 	    !ath12k_mac_vif_link_chan(vif, arvif->link_id, &def)) {
4634 		band = def.chan->band;
4635 		mcast_rate = info->mcast_rate[band];
4636 
4637 		if (mcast_rate > 0) {
4638 			rateidx = mcast_rate - 1;
4639 		} else {
4640 			if (info->basic_rates)
4641 				rateidx = __ffs(info->basic_rates);
4642 			else
4643 				rateidx = 0;
4644 		}
4645 
4646 		if (ar->pdev->cap.supported_bands & WMI_HOST_WLAN_5GHZ_CAP)
4647 			rateidx += ATH12K_MAC_FIRST_OFDM_RATE_IDX;
4648 
4649 		bitrate = ath12k_legacy_rates[rateidx].bitrate;
4650 		hw_value = ath12k_legacy_rates[rateidx].hw_value;
4651 
4652 		if (ath12k_mac_bitrate_is_cck(bitrate))
4653 			preamble = WMI_RATE_PREAMBLE_CCK;
4654 		else
4655 			preamble = WMI_RATE_PREAMBLE_OFDM;
4656 
4657 		rate = ATH12K_HW_RATE_CODE(hw_value, 0, preamble);
4658 
4659 		ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
4660 			   "mac vdev %d mcast_rate %x\n",
4661 			   arvif->vdev_id, rate);
4662 
4663 		vdev_param = WMI_VDEV_PARAM_MCAST_DATA_RATE;
4664 		ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
4665 						    vdev_param, rate);
4666 		if (ret)
4667 			ath12k_warn(ar->ab,
4668 				    "failed to set mcast rate on vdev %i: %d\n",
4669 				    arvif->vdev_id,  ret);
4670 
4671 		vdev_param = WMI_VDEV_PARAM_BCAST_DATA_RATE;
4672 		ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
4673 						    vdev_param, rate);
4674 		if (ret)
4675 			ath12k_warn(ar->ab,
4676 				    "failed to set bcast rate on vdev %i: %d\n",
4677 				    arvif->vdev_id,  ret);
4678 	}
4679 
4680 	if (changed & BSS_CHANGED_BASIC_RATES &&
4681 	    !ath12k_mac_vif_link_chan(vif, arvif->link_id, &def))
4682 		ath12k_recalculate_mgmt_rate(ar, arvif, &def);
4683 
4684 	if (changed & BSS_CHANGED_TWT) {
4685 		if (info->twt_requester || info->twt_responder)
4686 			ath12k_wmi_send_twt_enable_cmd(ar, ar->pdev->pdev_id);
4687 		else
4688 			ath12k_wmi_send_twt_disable_cmd(ar, ar->pdev->pdev_id);
4689 	}
4690 
4691 	if (changed & BSS_CHANGED_HE_OBSS_PD)
4692 		ath12k_wmi_send_obss_spr_cmd(ar, arvif->vdev_id,
4693 					     &info->he_obss_pd);
4694 
4695 	if (changed & BSS_CHANGED_HE_BSS_COLOR) {
4696 		if (vif->type == NL80211_IFTYPE_AP) {
4697 			ret = ath12k_wmi_obss_color_cfg_cmd(ar,
4698 							    arvif->vdev_id,
4699 							    info->he_bss_color.color,
4700 							    ATH12K_BSS_COLOR_AP_PERIODS,
4701 							    info->he_bss_color.enabled);
4702 			if (ret)
4703 				ath12k_warn(ar->ab, "failed to set bss color collision on vdev %u: %d\n",
4704 					    arvif->vdev_id,  ret);
4705 
4706 			param_id = WMI_VDEV_PARAM_BSS_COLOR;
4707 			if (info->he_bss_color.enabled)
4708 				param_value = info->he_bss_color.color <<
4709 					      IEEE80211_HE_OPERATION_BSS_COLOR_OFFSET;
4710 			else
4711 				param_value = IEEE80211_HE_OPERATION_BSS_COLOR_DISABLED;
4712 
4713 			ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
4714 							    param_id,
4715 							    param_value);
4716 			if (ret)
4717 				ath12k_warn(ar->ab, "failed to set bss color param on vdev %u: %d\n",
4718 					    arvif->vdev_id,  ret);
4719 			else
4720 				ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "bss color param 0x%x set on vdev %u\n",
4721 					   param_value, arvif->vdev_id);
4722 		} else if (vif->type == NL80211_IFTYPE_STATION) {
4723 			ret = ath12k_wmi_send_bss_color_change_enable_cmd(ar,
4724 									  arvif->vdev_id,
4725 									  1);
4726 			if (ret)
4727 				ath12k_warn(ar->ab, "failed to enable bss color change on vdev %i: %d\n",
4728 					    arvif->vdev_id,  ret);
4729 			ret = ath12k_wmi_obss_color_cfg_cmd(ar,
4730 							    arvif->vdev_id,
4731 							    0,
4732 							    ATH12K_BSS_COLOR_STA_PERIODS,
4733 							    1);
4734 			if (ret)
4735 				ath12k_warn(ar->ab, "failed to set bss color collision on vdev %i: %d\n",
4736 					    arvif->vdev_id,  ret);
4737 		}
4738 	}
4739 
4740 	ath12k_mac_fils_discovery(arvif, info);
4741 
4742 	if (changed & BSS_CHANGED_PS &&
4743 	    ar->ab->hw_params->supports_sta_ps) {
4744 		ahvif->ps = vif_cfg->ps;
4745 		ath12k_mac_vif_setup_ps(arvif);
4746 	}
4747 }
4748 
4749 static struct ath12k_vif_cache *ath12k_ahvif_get_link_cache(struct ath12k_vif *ahvif,
4750 							    u8 link_id)
4751 {
4752 	if (!ahvif->cache[link_id]) {
4753 		ahvif->cache[link_id] = kzalloc(sizeof(*ahvif->cache[0]), GFP_KERNEL);
4754 		if (ahvif->cache[link_id])
4755 			INIT_LIST_HEAD(&ahvif->cache[link_id]->key_conf.list);
4756 	}
4757 
4758 	return ahvif->cache[link_id];
4759 }
4760 
4761 static void ath12k_ahvif_put_link_key_cache(struct ath12k_vif_cache *cache)
4762 {
4763 	struct ath12k_key_conf *key_conf, *tmp;
4764 
4765 	if (!cache || list_empty(&cache->key_conf.list))
4766 		return;
4767 	list_for_each_entry_safe(key_conf, tmp, &cache->key_conf.list, list) {
4768 		list_del(&key_conf->list);
4769 		kfree(key_conf);
4770 	}
4771 }
4772 
4773 static void ath12k_ahvif_put_link_cache(struct ath12k_vif *ahvif, u8 link_id)
4774 {
4775 	if (link_id >= IEEE80211_MLD_MAX_NUM_LINKS)
4776 		return;
4777 
4778 	ath12k_ahvif_put_link_key_cache(ahvif->cache[link_id]);
4779 	kfree(ahvif->cache[link_id]);
4780 	ahvif->cache[link_id] = NULL;
4781 }
4782 
4783 static void ath12k_mac_op_link_info_changed(struct ieee80211_hw *hw,
4784 					    struct ieee80211_vif *vif,
4785 					    struct ieee80211_bss_conf *info,
4786 					    u64 changed)
4787 {
4788 	struct ath12k *ar;
4789 	struct ath12k_vif *ahvif = ath12k_vif_to_ahvif(vif);
4790 	struct ath12k_vif_cache *cache;
4791 	struct ath12k_link_vif *arvif;
4792 	u8 link_id = info->link_id;
4793 
4794 	lockdep_assert_wiphy(hw->wiphy);
4795 
4796 	arvif = wiphy_dereference(hw->wiphy, ahvif->link[link_id]);
4797 
4798 	/* if the vdev is not created on a certain radio,
4799 	 * cache the info to be updated later on vdev creation
4800 	 */
4801 
4802 	if (!arvif || !arvif->is_created) {
4803 		cache = ath12k_ahvif_get_link_cache(ahvif, link_id);
4804 		if (!cache)
4805 			return;
4806 
4807 		cache->bss_conf_changed |= changed;
4808 
4809 		return;
4810 	}
4811 
4812 	ar = arvif->ar;
4813 
4814 	ath12k_mac_bss_info_changed(ar, arvif, info, changed);
4815 }
4816 
4817 static struct ath12k*
4818 ath12k_mac_select_scan_device(struct ieee80211_hw *hw,
4819 			      struct ieee80211_vif *vif,
4820 			      u32 center_freq)
4821 {
4822 	struct ath12k_hw *ah = hw->priv;
4823 	enum nl80211_band band;
4824 	struct ath12k *ar;
4825 	int i;
4826 
4827 	if (ah->num_radio == 1)
4828 		return ah->radio;
4829 
4830 	/* Currently mac80211 supports splitting scan requests into
4831 	 * multiple scan requests per band.
4832 	 * Loop through first channel and determine the scan radio
4833 	 * TODO: There could be 5 GHz low/high channels in that case
4834 	 * split the hw request and perform multiple scans
4835 	 */
4836 
4837 	if (center_freq < ATH12K_MIN_5GHZ_FREQ)
4838 		band = NL80211_BAND_2GHZ;
4839 	else if (center_freq < ATH12K_MIN_6GHZ_FREQ)
4840 		band = NL80211_BAND_5GHZ;
4841 	else
4842 		band = NL80211_BAND_6GHZ;
4843 
4844 	for_each_ar(ah, ar, i) {
4845 		if (ar->mac.sbands[band].channels &&
4846 		    center_freq >= KHZ_TO_MHZ(ar->freq_range.start_freq) &&
4847 		    center_freq <= KHZ_TO_MHZ(ar->freq_range.end_freq))
4848 			return ar;
4849 	}
4850 
4851 	return NULL;
4852 }
4853 
4854 void __ath12k_mac_scan_finish(struct ath12k *ar)
4855 {
4856 	struct ieee80211_hw *hw = ath12k_ar_to_hw(ar);
4857 
4858 	lockdep_assert_held(&ar->data_lock);
4859 
4860 	switch (ar->scan.state) {
4861 	case ATH12K_SCAN_IDLE:
4862 		break;
4863 	case ATH12K_SCAN_RUNNING:
4864 	case ATH12K_SCAN_ABORTING:
4865 		if (ar->scan.is_roc && ar->scan.roc_notify)
4866 			ieee80211_remain_on_channel_expired(hw);
4867 		fallthrough;
4868 	case ATH12K_SCAN_STARTING:
4869 		cancel_delayed_work(&ar->scan.timeout);
4870 		complete_all(&ar->scan.completed);
4871 		wiphy_work_queue(ar->ah->hw->wiphy, &ar->scan.vdev_clean_wk);
4872 		break;
4873 	}
4874 }
4875 
4876 void ath12k_mac_scan_finish(struct ath12k *ar)
4877 {
4878 	spin_lock_bh(&ar->data_lock);
4879 	__ath12k_mac_scan_finish(ar);
4880 	spin_unlock_bh(&ar->data_lock);
4881 }
4882 
4883 static int ath12k_scan_stop(struct ath12k *ar)
4884 {
4885 	struct ath12k_wmi_scan_cancel_arg arg = {
4886 		.req_type = WLAN_SCAN_CANCEL_SINGLE,
4887 		.scan_id = ATH12K_SCAN_ID,
4888 	};
4889 	int ret;
4890 
4891 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
4892 
4893 	/* TODO: Fill other STOP Params */
4894 	arg.pdev_id = ar->pdev->pdev_id;
4895 
4896 	ret = ath12k_wmi_send_scan_stop_cmd(ar, &arg);
4897 	if (ret) {
4898 		ath12k_warn(ar->ab, "failed to stop wmi scan: %d\n", ret);
4899 		goto out;
4900 	}
4901 
4902 	ret = wait_for_completion_timeout(&ar->scan.completed, 3 * HZ);
4903 	if (ret == 0) {
4904 		ath12k_warn(ar->ab,
4905 			    "failed to receive scan abort comple: timed out\n");
4906 		ret = -ETIMEDOUT;
4907 	} else if (ret > 0) {
4908 		ret = 0;
4909 	}
4910 
4911 out:
4912 	/* Scan state should be updated in scan completion worker but in
4913 	 * case firmware fails to deliver the event (for whatever reason)
4914 	 * it is desired to clean up scan state anyway. Firmware may have
4915 	 * just dropped the scan completion event delivery due to transport
4916 	 * pipe being overflown with data and/or it can recover on its own
4917 	 * before next scan request is submitted.
4918 	 */
4919 	spin_lock_bh(&ar->data_lock);
4920 	if (ret)
4921 		__ath12k_mac_scan_finish(ar);
4922 	spin_unlock_bh(&ar->data_lock);
4923 
4924 	return ret;
4925 }
4926 
4927 static void ath12k_scan_abort(struct ath12k *ar)
4928 {
4929 	int ret;
4930 
4931 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
4932 
4933 	spin_lock_bh(&ar->data_lock);
4934 
4935 	switch (ar->scan.state) {
4936 	case ATH12K_SCAN_IDLE:
4937 		/* This can happen if timeout worker kicked in and called
4938 		 * abortion while scan completion was being processed.
4939 		 */
4940 		break;
4941 	case ATH12K_SCAN_STARTING:
4942 	case ATH12K_SCAN_ABORTING:
4943 		ath12k_warn(ar->ab, "refusing scan abortion due to invalid scan state: %d\n",
4944 			    ar->scan.state);
4945 		break;
4946 	case ATH12K_SCAN_RUNNING:
4947 		ar->scan.state = ATH12K_SCAN_ABORTING;
4948 		spin_unlock_bh(&ar->data_lock);
4949 
4950 		ret = ath12k_scan_stop(ar);
4951 		if (ret)
4952 			ath12k_warn(ar->ab, "failed to abort scan: %d\n", ret);
4953 
4954 		spin_lock_bh(&ar->data_lock);
4955 		break;
4956 	}
4957 
4958 	spin_unlock_bh(&ar->data_lock);
4959 }
4960 
4961 static void ath12k_scan_timeout_work(struct work_struct *work)
4962 {
4963 	struct ath12k *ar = container_of(work, struct ath12k,
4964 					 scan.timeout.work);
4965 
4966 	wiphy_lock(ath12k_ar_to_hw(ar)->wiphy);
4967 	ath12k_scan_abort(ar);
4968 	wiphy_unlock(ath12k_ar_to_hw(ar)->wiphy);
4969 }
4970 
4971 static void ath12k_mac_scan_send_complete(struct ath12k *ar,
4972 					  struct cfg80211_scan_info *info)
4973 {
4974 	struct ath12k_hw *ah = ar->ah;
4975 	struct ath12k *partner_ar;
4976 	int i;
4977 
4978 	lockdep_assert_wiphy(ah->hw->wiphy);
4979 
4980 	for_each_ar(ah, partner_ar, i)
4981 		if (partner_ar != ar &&
4982 		    partner_ar->scan.state == ATH12K_SCAN_RUNNING)
4983 			return;
4984 
4985 	ieee80211_scan_completed(ah->hw, info);
4986 }
4987 
4988 static void ath12k_scan_vdev_clean_work(struct wiphy *wiphy, struct wiphy_work *work)
4989 {
4990 	struct ath12k *ar = container_of(work, struct ath12k,
4991 					 scan.vdev_clean_wk);
4992 	struct ath12k_hw *ah = ar->ah;
4993 	struct ath12k_link_vif *arvif;
4994 
4995 	lockdep_assert_wiphy(wiphy);
4996 
4997 	arvif = ar->scan.arvif;
4998 
4999 	/* The scan vdev has already been deleted. This can occur when a
5000 	 * new scan request is made on the same vif with a different
5001 	 * frequency, causing the scan arvif to move from one radio to
5002 	 * another. Or, scan was abrupted and via remove interface, the
5003 	 * arvif is already deleted. Alternatively, if the scan vdev is not
5004 	 * being used as an actual vdev, then do not delete it.
5005 	 */
5006 	if (!arvif || arvif->is_started)
5007 		goto work_complete;
5008 
5009 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac clean scan vdev (link id %u)",
5010 		   arvif->link_id);
5011 
5012 	ath12k_mac_remove_link_interface(ah->hw, arvif);
5013 	ath12k_mac_unassign_link_vif(arvif);
5014 
5015 work_complete:
5016 	spin_lock_bh(&ar->data_lock);
5017 	ar->scan.arvif = NULL;
5018 	if (!ar->scan.is_roc) {
5019 		struct cfg80211_scan_info info = {
5020 			.aborted = ((ar->scan.state ==
5021 				    ATH12K_SCAN_ABORTING) ||
5022 				    (ar->scan.state ==
5023 				    ATH12K_SCAN_STARTING)),
5024 		};
5025 
5026 		ath12k_mac_scan_send_complete(ar, &info);
5027 	}
5028 
5029 	ar->scan.state = ATH12K_SCAN_IDLE;
5030 	ar->scan_channel = NULL;
5031 	ar->scan.roc_freq = 0;
5032 	spin_unlock_bh(&ar->data_lock);
5033 }
5034 
5035 static int ath12k_start_scan(struct ath12k *ar,
5036 			     struct ath12k_wmi_scan_req_arg *arg)
5037 {
5038 	int ret;
5039 
5040 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
5041 
5042 	ret = ath12k_wmi_send_scan_start_cmd(ar, arg);
5043 	if (ret)
5044 		return ret;
5045 
5046 	ret = wait_for_completion_timeout(&ar->scan.started, 1 * HZ);
5047 	if (ret == 0) {
5048 		ret = ath12k_scan_stop(ar);
5049 		if (ret)
5050 			ath12k_warn(ar->ab, "failed to stop scan: %d\n", ret);
5051 
5052 		return -ETIMEDOUT;
5053 	}
5054 
5055 	/* If we failed to start the scan, return error code at
5056 	 * this point.  This is probably due to some issue in the
5057 	 * firmware, but no need to wedge the driver due to that...
5058 	 */
5059 	spin_lock_bh(&ar->data_lock);
5060 	if (ar->scan.state == ATH12K_SCAN_IDLE) {
5061 		spin_unlock_bh(&ar->data_lock);
5062 		return -EINVAL;
5063 	}
5064 	spin_unlock_bh(&ar->data_lock);
5065 
5066 	return 0;
5067 }
5068 
5069 int ath12k_mac_get_fw_stats(struct ath12k *ar,
5070 			    struct ath12k_fw_stats_req_params *param)
5071 {
5072 	struct ath12k_base *ab = ar->ab;
5073 	struct ath12k_hw *ah = ath12k_ar_to_ah(ar);
5074 	unsigned long time_left;
5075 	int ret;
5076 
5077 	guard(mutex)(&ah->hw_mutex);
5078 
5079 	if (ah->state != ATH12K_HW_STATE_ON)
5080 		return -ENETDOWN;
5081 
5082 	reinit_completion(&ar->fw_stats_complete);
5083 	reinit_completion(&ar->fw_stats_done);
5084 
5085 	ret = ath12k_wmi_send_stats_request_cmd(ar, param->stats_id,
5086 						param->vdev_id, param->pdev_id);
5087 	if (ret) {
5088 		ath12k_warn(ab, "failed to request fw stats: %d\n", ret);
5089 		return ret;
5090 	}
5091 
5092 	ath12k_dbg(ab, ATH12K_DBG_WMI,
5093 		   "get fw stat pdev id %d vdev id %d stats id 0x%x\n",
5094 		   param->pdev_id, param->vdev_id, param->stats_id);
5095 
5096 	time_left = wait_for_completion_timeout(&ar->fw_stats_complete, 1 * HZ);
5097 	if (!time_left) {
5098 		ath12k_warn(ab, "time out while waiting for get fw stats\n");
5099 		return -ETIMEDOUT;
5100 	}
5101 
5102 	/* Firmware sends WMI_UPDATE_STATS_EVENTID back-to-back
5103 	 * when stats data buffer limit is reached. fw_stats_complete
5104 	 * is completed once host receives first event from firmware, but
5105 	 * still there could be more events following. Below is to wait
5106 	 * until firmware completes sending all the events.
5107 	 */
5108 	time_left = wait_for_completion_timeout(&ar->fw_stats_done, 3 * HZ);
5109 	if (!time_left) {
5110 		ath12k_warn(ab, "time out while waiting for fw stats done\n");
5111 		return -ETIMEDOUT;
5112 	}
5113 
5114 	return 0;
5115 }
5116 
5117 static int ath12k_mac_op_get_txpower(struct ieee80211_hw *hw,
5118 				     struct ieee80211_vif *vif,
5119 				     unsigned int link_id,
5120 				     int *dbm)
5121 {
5122 	struct ath12k_vif *ahvif = ath12k_vif_to_ahvif(vif);
5123 	struct ath12k_fw_stats_req_params params = {};
5124 	struct ath12k_fw_stats_pdev *pdev;
5125 	struct ath12k_hw *ah = hw->priv;
5126 	struct ath12k_link_vif *arvif;
5127 	struct ath12k_base *ab;
5128 	struct ath12k *ar;
5129 	int ret;
5130 
5131 	/* Final Tx power is minimum of Target Power, CTL power, Regulatory
5132 	 * Power, PSD EIRP Power. We just know the Regulatory power from the
5133 	 * regulatory rules obtained. FW knows all these power and sets the min
5134 	 * of these. Hence, we request the FW pdev stats in which FW reports
5135 	 * the minimum of all vdev's channel Tx power.
5136 	 */
5137 	lockdep_assert_wiphy(hw->wiphy);
5138 
5139 	arvif = wiphy_dereference(ah->hw->wiphy, ahvif->link[link_id]);
5140 	if (!arvif || !arvif->ar)
5141 		return -EINVAL;
5142 
5143 	ar = arvif->ar;
5144 	ab = ar->ab;
5145 	if (ah->state != ATH12K_HW_STATE_ON)
5146 		goto err_fallback;
5147 
5148 	if (test_bit(ATH12K_FLAG_CAC_RUNNING, &ar->dev_flags))
5149 		return -EAGAIN;
5150 
5151 	/* Limit the requests to Firmware for fetching the tx power */
5152 	if (ar->chan_tx_pwr != ATH12K_PDEV_TX_POWER_INVALID &&
5153 	    time_before(jiffies,
5154 			msecs_to_jiffies(ATH12K_PDEV_TX_POWER_REFRESH_TIME_MSECS) +
5155 					 ar->last_tx_power_update))
5156 		goto send_tx_power;
5157 
5158 	params.pdev_id = ar->pdev->pdev_id;
5159 	params.vdev_id = arvif->vdev_id;
5160 	params.stats_id = WMI_REQUEST_PDEV_STAT;
5161 	ret = ath12k_mac_get_fw_stats(ar, &params);
5162 	if (ret) {
5163 		ath12k_warn(ab, "failed to request fw pdev stats: %d\n", ret);
5164 		goto err_fallback;
5165 	}
5166 
5167 	spin_lock_bh(&ar->data_lock);
5168 	pdev = list_first_entry_or_null(&ar->fw_stats.pdevs,
5169 					struct ath12k_fw_stats_pdev, list);
5170 	if (!pdev) {
5171 		spin_unlock_bh(&ar->data_lock);
5172 		goto err_fallback;
5173 	}
5174 
5175 	/* tx power reported by firmware is in units of 0.5 dBm */
5176 	ar->chan_tx_pwr = pdev->chan_tx_power / 2;
5177 	spin_unlock_bh(&ar->data_lock);
5178 	ar->last_tx_power_update = jiffies;
5179 	ath12k_fw_stats_reset(ar);
5180 
5181 send_tx_power:
5182 	*dbm = ar->chan_tx_pwr;
5183 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "txpower fetched from firmware %d dBm\n",
5184 		   *dbm);
5185 	return 0;
5186 
5187 err_fallback:
5188 	/* We didn't get txpower from FW. Hence, relying on vif->bss_conf.txpower */
5189 	*dbm = vif->bss_conf.txpower;
5190 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "txpower from firmware NaN, reported %d dBm\n",
5191 		   *dbm);
5192 	return 0;
5193 }
5194 
5195 static u8
5196 ath12k_mac_find_link_id_by_ar(struct ath12k_vif *ahvif, struct ath12k *ar)
5197 {
5198 	struct ath12k_link_vif *arvif;
5199 	struct ath12k_hw *ah = ahvif->ah;
5200 	unsigned long links = ahvif->links_map;
5201 	unsigned long scan_links_map;
5202 	u8 link_id;
5203 
5204 	lockdep_assert_wiphy(ah->hw->wiphy);
5205 
5206 	for_each_set_bit(link_id, &links, ATH12K_NUM_MAX_LINKS) {
5207 		arvif = wiphy_dereference(ah->hw->wiphy, ahvif->link[link_id]);
5208 
5209 		if (!arvif || !arvif->is_created)
5210 			continue;
5211 
5212 		if (ar == arvif->ar)
5213 			return link_id;
5214 	}
5215 
5216 	/* input ar is not assigned to any of the links of ML VIF, use next
5217 	 * available scan link for scan vdev creation. There are cases where
5218 	 * single scan req needs to be split in driver and initiate separate
5219 	 * scan requests to firmware based on device.
5220 	 */
5221 
5222 	 /* Unset all non-scan links (0-14) of scan_links_map so that ffs() will
5223 	  * choose an available link among scan links (i.e link id >= 15)
5224 	  */
5225 	scan_links_map = ~ahvif->links_map & ATH12K_SCAN_LINKS_MASK;
5226 	if (scan_links_map)
5227 		return __ffs(scan_links_map);
5228 
5229 	return ATH12K_FIRST_SCAN_LINK;
5230 }
5231 
5232 static int ath12k_mac_initiate_hw_scan(struct ieee80211_hw *hw,
5233 				       struct ieee80211_vif *vif,
5234 				       struct ieee80211_scan_request *hw_req,
5235 				       int n_channels,
5236 				       struct ieee80211_channel **chan_list,
5237 				       struct ath12k *ar)
5238 {
5239 	struct ath12k_hw *ah = ath12k_hw_to_ah(hw);
5240 	struct ath12k_vif *ahvif = ath12k_vif_to_ahvif(vif);
5241 	struct ath12k_link_vif *arvif;
5242 	struct cfg80211_scan_request *req = &hw_req->req;
5243 	struct ath12k_wmi_scan_req_arg *arg = NULL;
5244 	u8 link_id;
5245 	int ret;
5246 	int i;
5247 	bool create = true;
5248 
5249 	lockdep_assert_wiphy(hw->wiphy);
5250 
5251 	arvif = &ahvif->deflink;
5252 
5253 	/* check if any of the links of ML VIF is already started on
5254 	 * radio(ar) corresponding to given scan frequency and use it,
5255 	 * if not use scan link (link id >= 15) for scan purpose.
5256 	 */
5257 	link_id = ath12k_mac_find_link_id_by_ar(ahvif, ar);
5258 	/* All scan links are occupied. ideally this shouldn't happen as
5259 	 * mac80211 won't schedule scan for same band until ongoing scan is
5260 	 * completed, don't try to exceed max links just in case if it happens.
5261 	 */
5262 	if (link_id >= ATH12K_NUM_MAX_LINKS)
5263 		return -EBUSY;
5264 
5265 	arvif = ath12k_mac_assign_link_vif(ah, vif, link_id);
5266 
5267 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac link ID %d selected for scan",
5268 		   arvif->link_id);
5269 
5270 	/* If the vif is already assigned to a specific vdev of an ar,
5271 	 * check whether its already started, vdev which is started
5272 	 * are not allowed to switch to a new radio.
5273 	 * If the vdev is not started, but was earlier created on a
5274 	 * different ar, delete that vdev and create a new one. We don't
5275 	 * delete at the scan stop as an optimization to avoid redundant
5276 	 * delete-create vdev's for the same ar, in case the request is
5277 	 * always on the same band for the vif
5278 	 */
5279 	if (arvif->is_created) {
5280 		if (WARN_ON(!arvif->ar))
5281 			return -EINVAL;
5282 
5283 		if (ar != arvif->ar && arvif->is_started)
5284 			return -EINVAL;
5285 
5286 		if (ar != arvif->ar) {
5287 			ath12k_mac_remove_link_interface(hw, arvif);
5288 			ath12k_mac_unassign_link_vif(arvif);
5289 		} else {
5290 			create = false;
5291 		}
5292 	}
5293 
5294 	if (create) {
5295 		/* Previous arvif would've been cleared in radio switch block
5296 		 * above, assign arvif again for create.
5297 		 */
5298 		arvif = ath12k_mac_assign_link_vif(ah, vif, link_id);
5299 
5300 		ret = ath12k_mac_vdev_create(ar, arvif);
5301 		if (ret) {
5302 			ath12k_warn(ar->ab, "unable to create scan vdev %d\n", ret);
5303 			ath12k_mac_unassign_link_vif(arvif);
5304 			return ret;
5305 		}
5306 	}
5307 
5308 	spin_lock_bh(&ar->data_lock);
5309 	switch (ar->scan.state) {
5310 	case ATH12K_SCAN_IDLE:
5311 		reinit_completion(&ar->scan.started);
5312 		reinit_completion(&ar->scan.completed);
5313 		ar->scan.state = ATH12K_SCAN_STARTING;
5314 		ar->scan.is_roc = false;
5315 		ar->scan.arvif = arvif;
5316 		ret = 0;
5317 		break;
5318 	case ATH12K_SCAN_STARTING:
5319 	case ATH12K_SCAN_RUNNING:
5320 	case ATH12K_SCAN_ABORTING:
5321 		ret = -EBUSY;
5322 		break;
5323 	}
5324 	spin_unlock_bh(&ar->data_lock);
5325 
5326 	if (ret)
5327 		goto exit;
5328 
5329 	arg = kzalloc(sizeof(*arg), GFP_KERNEL);
5330 	if (!arg) {
5331 		ret = -ENOMEM;
5332 		goto exit;
5333 	}
5334 
5335 	ath12k_wmi_start_scan_init(ar, arg);
5336 	arg->vdev_id = arvif->vdev_id;
5337 	arg->scan_id = ATH12K_SCAN_ID;
5338 
5339 	if (req->ie_len) {
5340 		arg->extraie.ptr = kmemdup(req->ie, req->ie_len, GFP_KERNEL);
5341 		if (!arg->extraie.ptr) {
5342 			ret = -ENOMEM;
5343 			goto exit;
5344 		}
5345 		arg->extraie.len = req->ie_len;
5346 	}
5347 
5348 	if (req->n_ssids) {
5349 		arg->num_ssids = req->n_ssids;
5350 		for (i = 0; i < arg->num_ssids; i++)
5351 			arg->ssid[i] = req->ssids[i];
5352 	} else {
5353 		arg->scan_f_passive = 1;
5354 	}
5355 
5356 	if (n_channels) {
5357 		arg->num_chan = n_channels;
5358 		arg->chan_list = kcalloc(arg->num_chan, sizeof(*arg->chan_list),
5359 					 GFP_KERNEL);
5360 		if (!arg->chan_list) {
5361 			ret = -ENOMEM;
5362 			goto exit;
5363 		}
5364 
5365 		for (i = 0; i < arg->num_chan; i++)
5366 			arg->chan_list[i] = chan_list[i]->center_freq;
5367 	}
5368 
5369 	ret = ath12k_start_scan(ar, arg);
5370 	if (ret) {
5371 		if (ret == -EBUSY)
5372 			ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
5373 				   "scan engine is busy 11d state %d\n", ar->state_11d);
5374 		else
5375 			ath12k_warn(ar->ab, "failed to start hw scan: %d\n", ret);
5376 
5377 		spin_lock_bh(&ar->data_lock);
5378 		ar->scan.state = ATH12K_SCAN_IDLE;
5379 		spin_unlock_bh(&ar->data_lock);
5380 		goto exit;
5381 	}
5382 
5383 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac scan started");
5384 
5385 	/* Add a margin to account for event/command processing */
5386 	ieee80211_queue_delayed_work(ath12k_ar_to_hw(ar), &ar->scan.timeout,
5387 				     msecs_to_jiffies(arg->max_scan_time +
5388 						      ATH12K_MAC_SCAN_TIMEOUT_MSECS));
5389 
5390 exit:
5391 	if (arg) {
5392 		kfree(arg->chan_list);
5393 		kfree(arg->extraie.ptr);
5394 		kfree(arg);
5395 	}
5396 
5397 	if (ar->state_11d == ATH12K_11D_PREPARING &&
5398 	    ahvif->vdev_type == WMI_VDEV_TYPE_STA &&
5399 	    ahvif->vdev_subtype == WMI_VDEV_SUBTYPE_NONE)
5400 		ath12k_mac_11d_scan_start(ar, arvif->vdev_id);
5401 
5402 	return ret;
5403 }
5404 
5405 static int ath12k_mac_op_hw_scan(struct ieee80211_hw *hw,
5406 				 struct ieee80211_vif *vif,
5407 				 struct ieee80211_scan_request *hw_req)
5408 {
5409 	struct ath12k_vif *ahvif = ath12k_vif_to_ahvif(vif);
5410 	struct ieee80211_channel **chan_list, *chan;
5411 	struct ath12k_hw *ah = ath12k_hw_to_ah(hw);
5412 	unsigned long links_map, link_id;
5413 	struct ath12k_link_vif *arvif;
5414 	struct ath12k *ar, *scan_ar;
5415 	int i, j, ret = 0;
5416 
5417 	lockdep_assert_wiphy(hw->wiphy);
5418 
5419 	chan_list = kcalloc(hw_req->req.n_channels, sizeof(*chan_list), GFP_KERNEL);
5420 	if (!chan_list)
5421 		return -ENOMEM;
5422 
5423 	/* There could be channels that belong to multiple underlying radio
5424 	 * in same scan request as mac80211 sees it as single band. In that
5425 	 * case split the hw_req based on frequency range and schedule scans to
5426 	 * corresponding radio.
5427 	 */
5428 	for_each_ar(ah, ar, i) {
5429 		int n_chans = 0;
5430 
5431 		for (j = 0; j < hw_req->req.n_channels; j++) {
5432 			chan = hw_req->req.channels[j];
5433 			scan_ar = ath12k_mac_select_scan_device(hw, vif,
5434 								chan->center_freq);
5435 			if (!scan_ar) {
5436 				ath12k_hw_warn(ah, "unable to select scan device for freq %d\n",
5437 					       chan->center_freq);
5438 				ret = -EINVAL;
5439 				goto abort;
5440 			}
5441 			if (ar != scan_ar)
5442 				continue;
5443 
5444 			chan_list[n_chans++] = chan;
5445 		}
5446 		if (n_chans) {
5447 			ret = ath12k_mac_initiate_hw_scan(hw, vif, hw_req, n_chans,
5448 							  chan_list, ar);
5449 			if (ret)
5450 				goto abort;
5451 		}
5452 	}
5453 abort:
5454 	/* If any of the parallel scans initiated fails, abort all and
5455 	 * remove the scan interfaces created. Return complete scan
5456 	 * failure as mac80211 assumes this as single scan request.
5457 	 */
5458 	if (ret) {
5459 		ath12k_hw_warn(ah, "Scan failed %d , cleanup all scan vdevs\n", ret);
5460 		links_map = ahvif->links_map;
5461 		for_each_set_bit(link_id, &links_map, ATH12K_NUM_MAX_LINKS) {
5462 			arvif = wiphy_dereference(hw->wiphy, ahvif->link[link_id]);
5463 			if (!arvif)
5464 				continue;
5465 
5466 			ar = arvif->ar;
5467 			if (ar->scan.arvif == arvif) {
5468 				wiphy_work_cancel(hw->wiphy, &ar->scan.vdev_clean_wk);
5469 				spin_lock_bh(&ar->data_lock);
5470 				ar->scan.arvif = NULL;
5471 				ar->scan.state = ATH12K_SCAN_IDLE;
5472 				ar->scan_channel = NULL;
5473 				ar->scan.roc_freq = 0;
5474 				spin_unlock_bh(&ar->data_lock);
5475 			}
5476 			if (link_id >= ATH12K_FIRST_SCAN_LINK) {
5477 				ath12k_mac_remove_link_interface(hw, arvif);
5478 				ath12k_mac_unassign_link_vif(arvif);
5479 			}
5480 		}
5481 	}
5482 	kfree(chan_list);
5483 	return ret;
5484 }
5485 
5486 static void ath12k_mac_op_cancel_hw_scan(struct ieee80211_hw *hw,
5487 					 struct ieee80211_vif *vif)
5488 {
5489 	struct ath12k_vif *ahvif = ath12k_vif_to_ahvif(vif);
5490 	unsigned long link_id, links_map = ahvif->links_map;
5491 	struct ath12k_link_vif *arvif;
5492 	struct ath12k *ar;
5493 
5494 	lockdep_assert_wiphy(hw->wiphy);
5495 
5496 	for_each_set_bit(link_id, &links_map, ATH12K_NUM_MAX_LINKS) {
5497 		arvif = wiphy_dereference(hw->wiphy, ahvif->link[link_id]);
5498 		if (!arvif || !arvif->is_created ||
5499 		    arvif->ar->scan.arvif != arvif)
5500 			continue;
5501 
5502 		ar = arvif->ar;
5503 
5504 		ath12k_scan_abort(ar);
5505 
5506 		cancel_delayed_work_sync(&ar->scan.timeout);
5507 	}
5508 }
5509 
5510 static int ath12k_install_key(struct ath12k_link_vif *arvif,
5511 			      struct ieee80211_key_conf *key,
5512 			      enum set_key_cmd cmd,
5513 			      const u8 *macaddr, u32 flags)
5514 {
5515 	int ret;
5516 	struct ath12k *ar = arvif->ar;
5517 	struct wmi_vdev_install_key_arg arg = {
5518 		.vdev_id = arvif->vdev_id,
5519 		.key_idx = key->keyidx,
5520 		.key_len = key->keylen,
5521 		.key_data = key->key,
5522 		.key_flags = flags,
5523 		.ieee80211_key_cipher = key->cipher,
5524 		.macaddr = macaddr,
5525 	};
5526 	struct ath12k_vif *ahvif = arvif->ahvif;
5527 
5528 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
5529 
5530 	if (test_bit(ATH12K_FLAG_HW_CRYPTO_DISABLED, &ar->ab->dev_flags))
5531 		return 0;
5532 
5533 	if (cmd == DISABLE_KEY) {
5534 		/* TODO: Check if FW expects  value other than NONE for del */
5535 		/* arg.key_cipher = WMI_CIPHER_NONE; */
5536 		arg.key_len = 0;
5537 		arg.key_data = NULL;
5538 		goto check_order;
5539 	}
5540 
5541 	switch (key->cipher) {
5542 	case WLAN_CIPHER_SUITE_CCMP:
5543 	case WLAN_CIPHER_SUITE_CCMP_256:
5544 		arg.key_cipher = WMI_CIPHER_AES_CCM;
5545 		key->flags |= IEEE80211_KEY_FLAG_GENERATE_IV_MGMT;
5546 		break;
5547 	case WLAN_CIPHER_SUITE_TKIP:
5548 		arg.key_cipher = WMI_CIPHER_TKIP;
5549 		arg.key_txmic_len = 8;
5550 		arg.key_rxmic_len = 8;
5551 		break;
5552 	case WLAN_CIPHER_SUITE_GCMP:
5553 	case WLAN_CIPHER_SUITE_GCMP_256:
5554 		arg.key_cipher = WMI_CIPHER_AES_GCM;
5555 		key->flags |= IEEE80211_KEY_FLAG_GENERATE_IV_MGMT;
5556 		break;
5557 	case WLAN_CIPHER_SUITE_AES_CMAC:
5558 		arg.key_cipher = WMI_CIPHER_AES_CMAC;
5559 		break;
5560 	case WLAN_CIPHER_SUITE_BIP_GMAC_128:
5561 	case WLAN_CIPHER_SUITE_BIP_GMAC_256:
5562 		arg.key_cipher = WMI_CIPHER_AES_GMAC;
5563 		break;
5564 	case WLAN_CIPHER_SUITE_BIP_CMAC_256:
5565 		arg.key_cipher = WMI_CIPHER_AES_CMAC;
5566 		break;
5567 	default:
5568 		ath12k_warn(ar->ab, "cipher %d is not supported\n", key->cipher);
5569 		return -EOPNOTSUPP;
5570 	}
5571 
5572 	if (test_bit(ATH12K_FLAG_RAW_MODE, &ar->ab->dev_flags))
5573 		key->flags |= IEEE80211_KEY_FLAG_GENERATE_IV |
5574 			      IEEE80211_KEY_FLAG_RESERVE_TAILROOM;
5575 
5576 check_order:
5577 	if (ahvif->vdev_type == WMI_VDEV_TYPE_STA &&
5578 	    arg.key_flags == WMI_KEY_GROUP) {
5579 		if (cmd == SET_KEY) {
5580 			if (arvif->pairwise_key_done) {
5581 				ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
5582 					   "vdev %u pairwise key done, go install group key\n",
5583 					   arg.vdev_id);
5584 				goto install;
5585 			} else {
5586 				/* WCN7850 firmware requires pairwise key to be installed
5587 				 * before group key. In case group key comes first, cache
5588 				 * it and return. Will revisit it once pairwise key gets
5589 				 * installed.
5590 				 */
5591 				arvif->group_key = arg;
5592 				arvif->group_key_valid = true;
5593 				ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
5594 					   "vdev %u group key before pairwise key, cache and skip\n",
5595 					   arg.vdev_id);
5596 
5597 				ret = 0;
5598 				goto out;
5599 			}
5600 		} else {
5601 			arvif->group_key_valid = false;
5602 		}
5603 	}
5604 
5605 install:
5606 	reinit_completion(&ar->install_key_done);
5607 
5608 	ret = ath12k_wmi_vdev_install_key(arvif->ar, &arg);
5609 	if (ret)
5610 		return ret;
5611 
5612 	if (!wait_for_completion_timeout(&ar->install_key_done, 1 * HZ))
5613 		return -ETIMEDOUT;
5614 
5615 	if (ether_addr_equal(arg.macaddr, arvif->bssid))
5616 		ahvif->key_cipher = arg.ieee80211_key_cipher;
5617 
5618 	if (ar->install_key_status) {
5619 		ret = -EINVAL;
5620 		goto out;
5621 	}
5622 
5623 	if (ahvif->vdev_type == WMI_VDEV_TYPE_STA &&
5624 	    arg.key_flags == WMI_KEY_PAIRWISE) {
5625 		if (cmd == SET_KEY) {
5626 			arvif->pairwise_key_done = true;
5627 			if (arvif->group_key_valid) {
5628 				/* Install cached GTK */
5629 				arvif->group_key_valid = false;
5630 				arg = arvif->group_key;
5631 				ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
5632 					   "vdev %u pairwise key done, group key ready, go install\n",
5633 					   arg.vdev_id);
5634 				goto install;
5635 			}
5636 		} else {
5637 			arvif->pairwise_key_done = false;
5638 		}
5639 	}
5640 
5641 out:
5642 	if (ret) {
5643 		/* In case of failure userspace may not do DISABLE_KEY
5644 		 * but triggers re-connection directly, so manually reset
5645 		 * status here.
5646 		 */
5647 		arvif->group_key_valid = false;
5648 		arvif->pairwise_key_done = false;
5649 	}
5650 
5651 	return ret;
5652 }
5653 
5654 static int ath12k_clear_peer_keys(struct ath12k_link_vif *arvif,
5655 				  const u8 *addr)
5656 {
5657 	struct ath12k *ar = arvif->ar;
5658 	struct ath12k_base *ab = ar->ab;
5659 	struct ath12k_peer *peer;
5660 	int first_errno = 0;
5661 	int ret;
5662 	int i;
5663 	u32 flags = 0;
5664 
5665 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
5666 
5667 	spin_lock_bh(&ab->base_lock);
5668 	peer = ath12k_peer_find(ab, arvif->vdev_id, addr);
5669 	spin_unlock_bh(&ab->base_lock);
5670 
5671 	if (!peer)
5672 		return -ENOENT;
5673 
5674 	for (i = 0; i < ARRAY_SIZE(peer->keys); i++) {
5675 		if (!peer->keys[i])
5676 			continue;
5677 
5678 		/* key flags are not required to delete the key */
5679 		ret = ath12k_install_key(arvif, peer->keys[i],
5680 					 DISABLE_KEY, addr, flags);
5681 		if (ret < 0 && first_errno == 0)
5682 			first_errno = ret;
5683 
5684 		if (ret < 0)
5685 			ath12k_warn(ab, "failed to remove peer key %d: %d\n",
5686 				    i, ret);
5687 
5688 		spin_lock_bh(&ab->base_lock);
5689 		peer->keys[i] = NULL;
5690 		spin_unlock_bh(&ab->base_lock);
5691 	}
5692 
5693 	return first_errno;
5694 }
5695 
5696 static int ath12k_mac_set_key(struct ath12k *ar, enum set_key_cmd cmd,
5697 			      struct ath12k_link_vif *arvif,
5698 			      struct ath12k_link_sta *arsta,
5699 			      struct ieee80211_key_conf *key)
5700 {
5701 	struct ieee80211_sta *sta = NULL;
5702 	struct ath12k_base *ab = ar->ab;
5703 	struct ath12k_peer *peer;
5704 	struct ath12k_sta *ahsta;
5705 	const u8 *peer_addr;
5706 	int ret;
5707 	u32 flags = 0;
5708 
5709 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
5710 
5711 	if (arsta)
5712 		sta = ath12k_ahsta_to_sta(arsta->ahsta);
5713 
5714 	if (test_bit(ATH12K_FLAG_HW_CRYPTO_DISABLED, &ab->dev_flags))
5715 		return 1;
5716 
5717 	if (sta)
5718 		peer_addr = arsta->addr;
5719 	else
5720 		peer_addr = arvif->bssid;
5721 
5722 	key->hw_key_idx = key->keyidx;
5723 
5724 	/* the peer should not disappear in mid-way (unless FW goes awry) since
5725 	 * we already hold wiphy lock. we just make sure its there now.
5726 	 */
5727 	spin_lock_bh(&ab->base_lock);
5728 	peer = ath12k_peer_find(ab, arvif->vdev_id, peer_addr);
5729 	spin_unlock_bh(&ab->base_lock);
5730 
5731 	if (!peer) {
5732 		if (cmd == SET_KEY) {
5733 			ath12k_warn(ab, "cannot install key for non-existent peer %pM\n",
5734 				    peer_addr);
5735 			return -EOPNOTSUPP;
5736 		}
5737 
5738 		/* if the peer doesn't exist there is no key to disable
5739 		 * anymore
5740 		 */
5741 		return 0;
5742 	}
5743 
5744 	if (key->flags & IEEE80211_KEY_FLAG_PAIRWISE)
5745 		flags = WMI_KEY_PAIRWISE;
5746 	else
5747 		flags = WMI_KEY_GROUP;
5748 
5749 	ret = ath12k_install_key(arvif, key, cmd, peer_addr, flags);
5750 	if (ret) {
5751 		ath12k_warn(ab, "ath12k_install_key failed (%d)\n", ret);
5752 		return ret;
5753 	}
5754 
5755 	ret = ath12k_dp_rx_peer_pn_replay_config(arvif, peer_addr, cmd, key);
5756 	if (ret) {
5757 		ath12k_warn(ab, "failed to offload PN replay detection %d\n", ret);
5758 		return ret;
5759 	}
5760 
5761 	spin_lock_bh(&ab->base_lock);
5762 	peer = ath12k_peer_find(ab, arvif->vdev_id, peer_addr);
5763 	if (peer && cmd == SET_KEY) {
5764 		peer->keys[key->keyidx] = key;
5765 		if (key->flags & IEEE80211_KEY_FLAG_PAIRWISE) {
5766 			peer->ucast_keyidx = key->keyidx;
5767 			peer->sec_type = ath12k_dp_tx_get_encrypt_type(key->cipher);
5768 		} else {
5769 			peer->mcast_keyidx = key->keyidx;
5770 			peer->sec_type_grp = ath12k_dp_tx_get_encrypt_type(key->cipher);
5771 		}
5772 	} else if (peer && cmd == DISABLE_KEY) {
5773 		peer->keys[key->keyidx] = NULL;
5774 		if (key->flags & IEEE80211_KEY_FLAG_PAIRWISE)
5775 			peer->ucast_keyidx = 0;
5776 		else
5777 			peer->mcast_keyidx = 0;
5778 	} else if (!peer)
5779 		/* impossible unless FW goes crazy */
5780 		ath12k_warn(ab, "peer %pM disappeared!\n", peer_addr);
5781 
5782 	if (sta) {
5783 		ahsta = ath12k_sta_to_ahsta(sta);
5784 
5785 		switch (key->cipher) {
5786 		case WLAN_CIPHER_SUITE_TKIP:
5787 		case WLAN_CIPHER_SUITE_CCMP:
5788 		case WLAN_CIPHER_SUITE_CCMP_256:
5789 		case WLAN_CIPHER_SUITE_GCMP:
5790 		case WLAN_CIPHER_SUITE_GCMP_256:
5791 			if (cmd == SET_KEY)
5792 				ahsta->pn_type = HAL_PN_TYPE_WPA;
5793 			else
5794 				ahsta->pn_type = HAL_PN_TYPE_NONE;
5795 			break;
5796 		default:
5797 			ahsta->pn_type = HAL_PN_TYPE_NONE;
5798 			break;
5799 		}
5800 	}
5801 
5802 	spin_unlock_bh(&ab->base_lock);
5803 
5804 	return 0;
5805 }
5806 
5807 static int ath12k_mac_update_key_cache(struct ath12k_vif_cache *cache,
5808 				       enum set_key_cmd cmd,
5809 				       struct ieee80211_sta *sta,
5810 				       struct ieee80211_key_conf *key)
5811 {
5812 	struct ath12k_key_conf *key_conf, *tmp;
5813 
5814 	list_for_each_entry_safe(key_conf, tmp, &cache->key_conf.list, list) {
5815 		if (key_conf->key != key)
5816 			continue;
5817 
5818 		/* If SET key entry is already present in cache, nothing to do,
5819 		 * just return
5820 		 */
5821 		if (cmd == SET_KEY)
5822 			return 0;
5823 
5824 		/* DEL key for an old SET key which driver hasn't flushed yet.
5825 		 */
5826 		list_del(&key_conf->list);
5827 		kfree(key_conf);
5828 	}
5829 
5830 	if (cmd == SET_KEY) {
5831 		key_conf = kzalloc(sizeof(*key_conf), GFP_KERNEL);
5832 
5833 		if (!key_conf)
5834 			return -ENOMEM;
5835 
5836 		key_conf->cmd = cmd;
5837 		key_conf->sta = sta;
5838 		key_conf->key = key;
5839 		list_add_tail(&key_conf->list,
5840 			      &cache->key_conf.list);
5841 	}
5842 
5843 	return 0;
5844 }
5845 
5846 static int ath12k_mac_op_set_key(struct ieee80211_hw *hw, enum set_key_cmd cmd,
5847 				 struct ieee80211_vif *vif, struct ieee80211_sta *sta,
5848 				 struct ieee80211_key_conf *key)
5849 {
5850 	struct ath12k_vif *ahvif = ath12k_vif_to_ahvif(vif);
5851 	struct ath12k_link_vif *arvif;
5852 	struct ath12k_link_sta *arsta = NULL;
5853 	struct ath12k_vif_cache *cache;
5854 	struct ath12k_sta *ahsta;
5855 	unsigned long links;
5856 	u8 link_id;
5857 	int ret;
5858 
5859 	lockdep_assert_wiphy(hw->wiphy);
5860 
5861 	/* IGTK needs to be done in host software */
5862 	if (key->keyidx == 4 || key->keyidx == 5)
5863 		return 1;
5864 
5865 	if (key->keyidx > WMI_MAX_KEY_INDEX)
5866 		return -ENOSPC;
5867 
5868 	if (sta) {
5869 		ahsta = ath12k_sta_to_ahsta(sta);
5870 
5871 		/* For an ML STA Pairwise key is same for all associated link Stations,
5872 		 * hence do set key for all link STAs which are active.
5873 		 */
5874 		if (sta->mlo) {
5875 			links = ahsta->links_map;
5876 			for_each_set_bit(link_id, &links, IEEE80211_MLD_MAX_NUM_LINKS) {
5877 				arvif = wiphy_dereference(hw->wiphy,
5878 							  ahvif->link[link_id]);
5879 				arsta = wiphy_dereference(hw->wiphy,
5880 							  ahsta->link[link_id]);
5881 
5882 				if (WARN_ON(!arvif || !arsta))
5883 					/* arvif and arsta are expected to be valid when
5884 					 * STA is present.
5885 					 */
5886 					continue;
5887 
5888 				ret = ath12k_mac_set_key(arvif->ar, cmd, arvif,
5889 							 arsta, key);
5890 				if (ret)
5891 					break;
5892 			}
5893 
5894 			return 0;
5895 		}
5896 
5897 		arsta = &ahsta->deflink;
5898 		arvif = arsta->arvif;
5899 		if (WARN_ON(!arvif))
5900 			return -EINVAL;
5901 
5902 		ret = ath12k_mac_set_key(arvif->ar, cmd, arvif, arsta, key);
5903 		if (ret)
5904 			return ret;
5905 
5906 		return 0;
5907 	}
5908 
5909 	if (key->link_id >= 0 && key->link_id < IEEE80211_MLD_MAX_NUM_LINKS) {
5910 		link_id = key->link_id;
5911 		arvif = wiphy_dereference(hw->wiphy, ahvif->link[link_id]);
5912 	} else {
5913 		link_id = 0;
5914 		arvif = &ahvif->deflink;
5915 	}
5916 
5917 	if (!arvif || !arvif->is_created) {
5918 		cache = ath12k_ahvif_get_link_cache(ahvif, link_id);
5919 		if (!cache)
5920 			return -ENOSPC;
5921 
5922 		ret = ath12k_mac_update_key_cache(cache, cmd, sta, key);
5923 		if (ret)
5924 			return ret;
5925 
5926 		return 0;
5927 	}
5928 
5929 	ret = ath12k_mac_set_key(arvif->ar, cmd, arvif, NULL, key);
5930 	if (ret)
5931 		return ret;
5932 
5933 	return 0;
5934 }
5935 
5936 static int
5937 ath12k_mac_bitrate_mask_num_vht_rates(struct ath12k *ar,
5938 				      enum nl80211_band band,
5939 				      const struct cfg80211_bitrate_mask *mask)
5940 {
5941 	int num_rates = 0;
5942 	int i;
5943 
5944 	for (i = 0; i < ARRAY_SIZE(mask->control[band].vht_mcs); i++)
5945 		num_rates += hweight16(mask->control[band].vht_mcs[i]);
5946 
5947 	return num_rates;
5948 }
5949 
5950 static int
5951 ath12k_mac_bitrate_mask_num_he_rates(struct ath12k *ar,
5952 				     enum nl80211_band band,
5953 				     const struct cfg80211_bitrate_mask *mask)
5954 {
5955 	int num_rates = 0;
5956 	int i;
5957 
5958 	for (i = 0; i < ARRAY_SIZE(mask->control[band].he_mcs); i++)
5959 		num_rates += hweight16(mask->control[band].he_mcs[i]);
5960 
5961 	return num_rates;
5962 }
5963 
5964 static int
5965 ath12k_mac_bitrate_mask_num_eht_rates(struct ath12k *ar,
5966 				      enum nl80211_band band,
5967 				      const struct cfg80211_bitrate_mask *mask)
5968 {
5969 	int num_rates = 0;
5970 	int i;
5971 
5972 	for (i = 0; i < ARRAY_SIZE(mask->control[band].eht_mcs); i++)
5973 		num_rates += hweight16(mask->control[band].eht_mcs[i]);
5974 
5975 	return num_rates;
5976 }
5977 
5978 static int
5979 ath12k_mac_set_peer_vht_fixed_rate(struct ath12k_link_vif *arvif,
5980 				   struct ath12k_link_sta *arsta,
5981 				   const struct cfg80211_bitrate_mask *mask,
5982 				   enum nl80211_band band)
5983 {
5984 	struct ath12k *ar = arvif->ar;
5985 	u8 vht_rate, nss;
5986 	u32 rate_code;
5987 	int ret, i;
5988 
5989 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
5990 
5991 	nss = 0;
5992 
5993 	for (i = 0; i < ARRAY_SIZE(mask->control[band].vht_mcs); i++) {
5994 		if (hweight16(mask->control[band].vht_mcs[i]) == 1) {
5995 			nss = i + 1;
5996 			vht_rate = ffs(mask->control[band].vht_mcs[i]) - 1;
5997 		}
5998 	}
5999 
6000 	if (!nss) {
6001 		ath12k_warn(ar->ab, "No single VHT Fixed rate found to set for %pM",
6002 			    arsta->addr);
6003 		return -EINVAL;
6004 	}
6005 
6006 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
6007 		   "Setting Fixed VHT Rate for peer %pM. Device will not switch to any other selected rates",
6008 		   arsta->addr);
6009 
6010 	rate_code = ATH12K_HW_RATE_CODE(vht_rate, nss - 1,
6011 					WMI_RATE_PREAMBLE_VHT);
6012 	ret = ath12k_wmi_set_peer_param(ar, arsta->addr,
6013 					arvif->vdev_id,
6014 					WMI_PEER_PARAM_FIXED_RATE,
6015 					rate_code);
6016 	if (ret)
6017 		ath12k_warn(ar->ab,
6018 			    "failed to update STA %pM Fixed Rate %d: %d\n",
6019 			     arsta->addr, rate_code, ret);
6020 
6021 	return ret;
6022 }
6023 
6024 static int
6025 ath12k_mac_set_peer_he_fixed_rate(struct ath12k_link_vif *arvif,
6026 				  struct ath12k_link_sta *arsta,
6027 				  const struct cfg80211_bitrate_mask *mask,
6028 				  enum nl80211_band band)
6029 {
6030 	struct ath12k *ar = arvif->ar;
6031 	u8 he_rate, nss;
6032 	u32 rate_code;
6033 	int ret, i;
6034 	struct ath12k_sta *ahsta = arsta->ahsta;
6035 	struct ieee80211_sta *sta;
6036 
6037 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
6038 
6039 	sta = ath12k_ahsta_to_sta(ahsta);
6040 	nss = 0;
6041 
6042 	for (i = 0; i < ARRAY_SIZE(mask->control[band].he_mcs); i++) {
6043 		if (hweight16(mask->control[band].he_mcs[i]) == 1) {
6044 			nss = i + 1;
6045 			he_rate = ffs(mask->control[band].he_mcs[i]) - 1;
6046 		}
6047 	}
6048 
6049 	if (!nss) {
6050 		ath12k_warn(ar->ab, "No single HE Fixed rate found to set for %pM",
6051 			    arsta->addr);
6052 		return -EINVAL;
6053 	}
6054 
6055 	/* Avoid updating invalid nss as fixed rate*/
6056 	if (nss > sta->deflink.rx_nss)
6057 		return -EINVAL;
6058 
6059 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
6060 		   "Setting Fixed HE Rate for peer %pM. Device will not switch to any other selected rates",
6061 		   arsta->addr);
6062 
6063 	rate_code = ATH12K_HW_RATE_CODE(he_rate, nss - 1,
6064 					WMI_RATE_PREAMBLE_HE);
6065 
6066 	ret = ath12k_wmi_set_peer_param(ar, arsta->addr,
6067 					arvif->vdev_id,
6068 					WMI_PEER_PARAM_FIXED_RATE,
6069 					rate_code);
6070 	if (ret)
6071 		ath12k_warn(ar->ab,
6072 			    "failed to update STA %pM Fixed Rate %d: %d\n",
6073 			    arsta->addr, rate_code, ret);
6074 
6075 	return ret;
6076 }
6077 
6078 static int
6079 ath12k_mac_set_peer_eht_fixed_rate(struct ath12k_link_vif *arvif,
6080 				   struct ath12k_link_sta *arsta,
6081 				   const struct cfg80211_bitrate_mask *mask,
6082 				   enum nl80211_band band)
6083 {
6084 	struct ath12k_sta *ahsta = arsta->ahsta;
6085 	struct ath12k *ar = arvif->ar;
6086 	struct ieee80211_sta *sta;
6087 	struct ieee80211_link_sta *link_sta;
6088 	u8 eht_rate, nss = 0;
6089 	u32 rate_code;
6090 	int ret, i;
6091 
6092 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
6093 
6094 	sta = ath12k_ahsta_to_sta(ahsta);
6095 
6096 	for (i = 0; i < ARRAY_SIZE(mask->control[band].eht_mcs); i++) {
6097 		if (hweight16(mask->control[band].eht_mcs[i]) == 1) {
6098 			nss = i + 1;
6099 			eht_rate = ffs(mask->control[band].eht_mcs[i]) - 1;
6100 		}
6101 	}
6102 
6103 	if (!nss) {
6104 		ath12k_warn(ar->ab, "No single EHT Fixed rate found to set for %pM\n",
6105 			    arsta->addr);
6106 		return -EINVAL;
6107 	}
6108 
6109 	/* Avoid updating invalid nss as fixed rate*/
6110 	link_sta = ath12k_mac_get_link_sta(arsta);
6111 	if (!link_sta || nss > link_sta->rx_nss) {
6112 		ath12k_warn(ar->ab,
6113 			    "unable to access link sta for sta %pM link %u or fixed nss of %u is not supported by sta\n",
6114 			    sta->addr, arsta->link_id, nss);
6115 		return -EINVAL;
6116 	}
6117 
6118 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
6119 		   "Setting Fixed EHT Rate for peer %pM. Device will not switch to any other selected rates\n",
6120 		   arsta->addr);
6121 
6122 	rate_code = ATH12K_HW_RATE_CODE(eht_rate, nss - 1,
6123 					WMI_RATE_PREAMBLE_EHT);
6124 
6125 	ret = ath12k_wmi_set_peer_param(ar, arsta->addr,
6126 					arvif->vdev_id,
6127 					WMI_PEER_PARAM_FIXED_RATE,
6128 					rate_code);
6129 	if (ret)
6130 		ath12k_warn(ar->ab,
6131 			    "failed to update STA %pM Fixed Rate %d: %d\n",
6132 			    arsta->addr, rate_code, ret);
6133 
6134 	return ret;
6135 }
6136 
6137 static int ath12k_mac_station_assoc(struct ath12k *ar,
6138 				    struct ath12k_link_vif *arvif,
6139 				    struct ath12k_link_sta *arsta,
6140 				    bool reassoc)
6141 {
6142 	struct ieee80211_vif *vif = ath12k_ahvif_to_vif(arvif->ahvif);
6143 	struct ieee80211_sta *sta = ath12k_ahsta_to_sta(arsta->ahsta);
6144 	struct ieee80211_link_sta *link_sta;
6145 	int ret;
6146 	struct cfg80211_chan_def def;
6147 	enum nl80211_band band;
6148 	struct cfg80211_bitrate_mask *mask;
6149 	u8 num_vht_rates, num_he_rates, num_eht_rates;
6150 	u8 link_id = arvif->link_id;
6151 
6152 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
6153 
6154 	if (WARN_ON(ath12k_mac_vif_link_chan(vif, arvif->link_id, &def)))
6155 		return -EPERM;
6156 
6157 	if (WARN_ON(!rcu_access_pointer(sta->link[link_id])))
6158 		return -EINVAL;
6159 
6160 	band = def.chan->band;
6161 	mask = &arvif->bitrate_mask;
6162 
6163 	struct ath12k_wmi_peer_assoc_arg *peer_arg __free(kfree) =
6164 		kzalloc(sizeof(*peer_arg), GFP_KERNEL);
6165 	if (!peer_arg)
6166 		return -ENOMEM;
6167 
6168 	ath12k_peer_assoc_prepare(ar, arvif, arsta, peer_arg, reassoc);
6169 
6170 	if (peer_arg->peer_nss < 1) {
6171 		ath12k_warn(ar->ab,
6172 			    "invalid peer NSS %d\n", peer_arg->peer_nss);
6173 		return -EINVAL;
6174 	}
6175 
6176 	peer_arg->is_assoc = true;
6177 	ret = ath12k_wmi_send_peer_assoc_cmd(ar, peer_arg);
6178 	if (ret) {
6179 		ath12k_warn(ar->ab, "failed to run peer assoc for STA %pM vdev %i: %d\n",
6180 			    arsta->addr, arvif->vdev_id, ret);
6181 		return ret;
6182 	}
6183 
6184 	if (!wait_for_completion_timeout(&ar->peer_assoc_done, 1 * HZ)) {
6185 		ath12k_warn(ar->ab, "failed to get peer assoc conf event for %pM vdev %i\n",
6186 			    arsta->addr, arvif->vdev_id);
6187 		return -ETIMEDOUT;
6188 	}
6189 
6190 	num_vht_rates = ath12k_mac_bitrate_mask_num_vht_rates(ar, band, mask);
6191 	num_he_rates = ath12k_mac_bitrate_mask_num_he_rates(ar, band, mask);
6192 	num_eht_rates = ath12k_mac_bitrate_mask_num_eht_rates(ar, band, mask);
6193 
6194 	/* If single VHT/HE/EHT rate is configured (by set_bitrate_mask()),
6195 	 * peer_assoc will disable VHT/HE/EHT. This is now enabled by a peer
6196 	 * specific fixed param.
6197 	 * Note that all other rates and NSS will be disabled for this peer.
6198 	 */
6199 	link_sta = ath12k_mac_get_link_sta(arsta);
6200 	if (!link_sta) {
6201 		ath12k_warn(ar->ab, "unable to access link sta in station assoc\n");
6202 		return -EINVAL;
6203 	}
6204 
6205 	spin_lock_bh(&ar->data_lock);
6206 	arsta->bw = ath12k_mac_ieee80211_sta_bw_to_wmi(ar, link_sta);
6207 	arsta->bw_prev = link_sta->bandwidth;
6208 	spin_unlock_bh(&ar->data_lock);
6209 
6210 	if (link_sta->vht_cap.vht_supported && num_vht_rates == 1) {
6211 		ret = ath12k_mac_set_peer_vht_fixed_rate(arvif, arsta, mask, band);
6212 	} else if (link_sta->he_cap.has_he && num_he_rates == 1) {
6213 		ret = ath12k_mac_set_peer_he_fixed_rate(arvif, arsta, mask, band);
6214 		if (ret)
6215 			return ret;
6216 	} else if (link_sta->eht_cap.has_eht && num_eht_rates == 1) {
6217 		ret = ath12k_mac_set_peer_eht_fixed_rate(arvif, arsta, mask, band);
6218 		if (ret)
6219 			return ret;
6220 	}
6221 
6222 	/* Re-assoc is run only to update supported rates for given station. It
6223 	 * doesn't make much sense to reconfigure the peer completely.
6224 	 */
6225 	if (reassoc)
6226 		return 0;
6227 
6228 	ret = ath12k_setup_peer_smps(ar, arvif, arsta->addr,
6229 				     &link_sta->ht_cap, &link_sta->he_6ghz_capa);
6230 	if (ret) {
6231 		ath12k_warn(ar->ab, "failed to setup peer SMPS for vdev %d: %d\n",
6232 			    arvif->vdev_id, ret);
6233 		return ret;
6234 	}
6235 
6236 	if (!sta->wme) {
6237 		arvif->num_legacy_stations++;
6238 		ret = ath12k_recalc_rtscts_prot(arvif);
6239 		if (ret)
6240 			return ret;
6241 	}
6242 
6243 	if (sta->wme && sta->uapsd_queues) {
6244 		ret = ath12k_peer_assoc_qos_ap(ar, arvif, arsta);
6245 		if (ret) {
6246 			ath12k_warn(ar->ab, "failed to set qos params for STA %pM for vdev %i: %d\n",
6247 				    arsta->addr, arvif->vdev_id, ret);
6248 			return ret;
6249 		}
6250 	}
6251 
6252 	return 0;
6253 }
6254 
6255 static int ath12k_mac_station_disassoc(struct ath12k *ar,
6256 				       struct ath12k_link_vif *arvif,
6257 				       struct ath12k_link_sta *arsta)
6258 {
6259 	struct ieee80211_sta *sta = ath12k_ahsta_to_sta(arsta->ahsta);
6260 
6261 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
6262 
6263 	if (!sta->wme) {
6264 		arvif->num_legacy_stations--;
6265 		return ath12k_recalc_rtscts_prot(arvif);
6266 	}
6267 
6268 	return 0;
6269 }
6270 
6271 static void ath12k_sta_rc_update_wk(struct wiphy *wiphy, struct wiphy_work *wk)
6272 {
6273 	struct ieee80211_link_sta *link_sta;
6274 	struct ath12k *ar;
6275 	struct ath12k_link_vif *arvif;
6276 	struct ieee80211_sta *sta;
6277 	struct cfg80211_chan_def def;
6278 	enum nl80211_band band;
6279 	const u8 *ht_mcs_mask;
6280 	const u16 *vht_mcs_mask;
6281 	const u16 *he_mcs_mask;
6282 	const u16 *eht_mcs_mask;
6283 	u32 changed, bw, nss, mac_nss, smps, bw_prev;
6284 	int err, num_vht_rates, num_he_rates, num_eht_rates;
6285 	const struct cfg80211_bitrate_mask *mask;
6286 	enum wmi_phy_mode peer_phymode;
6287 	struct ath12k_link_sta *arsta;
6288 	struct ieee80211_vif *vif;
6289 
6290 	lockdep_assert_wiphy(wiphy);
6291 
6292 	arsta = container_of(wk, struct ath12k_link_sta, update_wk);
6293 	sta = ath12k_ahsta_to_sta(arsta->ahsta);
6294 	arvif = arsta->arvif;
6295 	vif = ath12k_ahvif_to_vif(arvif->ahvif);
6296 	ar = arvif->ar;
6297 
6298 	if (WARN_ON(ath12k_mac_vif_link_chan(vif, arvif->link_id, &def)))
6299 		return;
6300 
6301 	band = def.chan->band;
6302 	ht_mcs_mask = arvif->bitrate_mask.control[band].ht_mcs;
6303 	vht_mcs_mask = arvif->bitrate_mask.control[band].vht_mcs;
6304 	he_mcs_mask = arvif->bitrate_mask.control[band].he_mcs;
6305 	eht_mcs_mask = arvif->bitrate_mask.control[band].eht_mcs;
6306 
6307 	spin_lock_bh(&ar->data_lock);
6308 
6309 	changed = arsta->changed;
6310 	arsta->changed = 0;
6311 
6312 	bw = arsta->bw;
6313 	bw_prev = arsta->bw_prev;
6314 	nss = arsta->nss;
6315 	smps = arsta->smps;
6316 
6317 	spin_unlock_bh(&ar->data_lock);
6318 
6319 	nss = max_t(u32, 1, nss);
6320 	mac_nss = max3(ath12k_mac_max_ht_nss(ht_mcs_mask),
6321 		       ath12k_mac_max_vht_nss(vht_mcs_mask),
6322 		       ath12k_mac_max_he_nss(he_mcs_mask));
6323 	mac_nss = max(mac_nss, ath12k_mac_max_eht_nss(eht_mcs_mask));
6324 	nss = min(nss, mac_nss);
6325 
6326 	struct ath12k_wmi_peer_assoc_arg *peer_arg __free(kfree) =
6327 					kzalloc(sizeof(*peer_arg), GFP_KERNEL);
6328 	if (!peer_arg)
6329 		return;
6330 
6331 	if (changed & IEEE80211_RC_BW_CHANGED) {
6332 		ath12k_peer_assoc_h_phymode(ar, arvif, arsta, peer_arg);
6333 		peer_phymode = peer_arg->peer_phymode;
6334 
6335 		if (bw > bw_prev) {
6336 			/* Phymode shows maximum supported channel width, if we
6337 			 * upgrade bandwidth then due to sanity check of firmware,
6338 			 * we have to send WMI_PEER_PHYMODE followed by
6339 			 * WMI_PEER_CHWIDTH
6340 			 */
6341 			ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac bandwidth upgrade for sta %pM new %d old %d\n",
6342 				   arsta->addr, bw, bw_prev);
6343 			err = ath12k_wmi_set_peer_param(ar, arsta->addr,
6344 							arvif->vdev_id, WMI_PEER_PHYMODE,
6345 							peer_phymode);
6346 			if (err) {
6347 				ath12k_warn(ar->ab, "failed to update STA %pM to peer phymode %d: %d\n",
6348 					    arsta->addr, peer_phymode, err);
6349 				return;
6350 			}
6351 			err = ath12k_wmi_set_peer_param(ar, arsta->addr,
6352 							arvif->vdev_id, WMI_PEER_CHWIDTH,
6353 							bw);
6354 			if (err)
6355 				ath12k_warn(ar->ab, "failed to update STA %pM to peer bandwidth %d: %d\n",
6356 					    arsta->addr, bw, err);
6357 		} else {
6358 			/* When we downgrade bandwidth this will conflict with phymode
6359 			 * and cause to trigger firmware crash. In this case we send
6360 			 * WMI_PEER_CHWIDTH followed by WMI_PEER_PHYMODE
6361 			 */
6362 			ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac bandwidth downgrade for sta %pM new %d old %d\n",
6363 				   arsta->addr, bw, bw_prev);
6364 			err = ath12k_wmi_set_peer_param(ar, arsta->addr,
6365 							arvif->vdev_id, WMI_PEER_CHWIDTH,
6366 							bw);
6367 			if (err) {
6368 				ath12k_warn(ar->ab, "failed to update STA %pM peer to bandwidth %d: %d\n",
6369 					    arsta->addr, bw, err);
6370 				return;
6371 			}
6372 			err = ath12k_wmi_set_peer_param(ar, arsta->addr,
6373 							arvif->vdev_id, WMI_PEER_PHYMODE,
6374 							peer_phymode);
6375 			if (err)
6376 				ath12k_warn(ar->ab, "failed to update STA %pM to peer phymode %d: %d\n",
6377 					    arsta->addr, peer_phymode, err);
6378 		}
6379 	}
6380 
6381 	if (changed & IEEE80211_RC_NSS_CHANGED) {
6382 		ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac update sta %pM nss %d\n",
6383 			   arsta->addr, nss);
6384 
6385 		err = ath12k_wmi_set_peer_param(ar, arsta->addr, arvif->vdev_id,
6386 						WMI_PEER_NSS, nss);
6387 		if (err)
6388 			ath12k_warn(ar->ab, "failed to update STA %pM nss %d: %d\n",
6389 				    arsta->addr, nss, err);
6390 	}
6391 
6392 	if (changed & IEEE80211_RC_SMPS_CHANGED) {
6393 		ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac update sta %pM smps %d\n",
6394 			   arsta->addr, smps);
6395 
6396 		err = ath12k_wmi_set_peer_param(ar, arsta->addr, arvif->vdev_id,
6397 						WMI_PEER_MIMO_PS_STATE, smps);
6398 		if (err)
6399 			ath12k_warn(ar->ab, "failed to update STA %pM smps %d: %d\n",
6400 				    arsta->addr, smps, err);
6401 	}
6402 
6403 	if (changed & IEEE80211_RC_SUPP_RATES_CHANGED) {
6404 		mask = &arvif->bitrate_mask;
6405 		num_vht_rates = ath12k_mac_bitrate_mask_num_vht_rates(ar, band,
6406 								      mask);
6407 		num_he_rates = ath12k_mac_bitrate_mask_num_he_rates(ar, band,
6408 								    mask);
6409 		num_eht_rates = ath12k_mac_bitrate_mask_num_eht_rates(ar, band,
6410 								      mask);
6411 
6412 		/* Peer_assoc_prepare will reject vht rates in
6413 		 * bitrate_mask if its not available in range format and
6414 		 * sets vht tx_rateset as unsupported. So multiple VHT MCS
6415 		 * setting(eg. MCS 4,5,6) per peer is not supported here.
6416 		 * But, Single rate in VHT mask can be set as per-peer
6417 		 * fixed rate. But even if any HT rates are configured in
6418 		 * the bitrate mask, device will not switch to those rates
6419 		 * when per-peer Fixed rate is set.
6420 		 * TODO: Check RATEMASK_CMDID to support auto rates selection
6421 		 * across HT/VHT and for multiple VHT MCS support.
6422 		 */
6423 		link_sta = ath12k_mac_get_link_sta(arsta);
6424 		if (!link_sta) {
6425 			ath12k_warn(ar->ab, "unable to access link sta in peer assoc he for sta %pM link %u\n",
6426 				    sta->addr, arsta->link_id);
6427 			return;
6428 		}
6429 
6430 		if (link_sta->vht_cap.vht_supported && num_vht_rates == 1) {
6431 			ath12k_mac_set_peer_vht_fixed_rate(arvif, arsta, mask,
6432 							   band);
6433 		} else if (link_sta->he_cap.has_he && num_he_rates == 1) {
6434 			ath12k_mac_set_peer_he_fixed_rate(arvif, arsta, mask, band);
6435 		} else if (link_sta->eht_cap.has_eht && num_eht_rates == 1) {
6436 			err = ath12k_mac_set_peer_eht_fixed_rate(arvif, arsta,
6437 								 mask, band);
6438 			if (err) {
6439 				ath12k_warn(ar->ab,
6440 					    "failed to set peer EHT fixed rate for STA %pM ret %d\n",
6441 					    arsta->addr, err);
6442 				return;
6443 			}
6444 		} else {
6445 			/* If the peer is non-VHT/HE/EHT or no fixed VHT/HE/EHT
6446 			 * rate is provided in the new bitrate mask we set the
6447 			 * other rates using peer_assoc command. Also clear
6448 			 * the peer fixed rate settings as it has higher proprity
6449 			 * than peer assoc
6450 			 */
6451 			err = ath12k_wmi_set_peer_param(ar, arsta->addr,
6452 							arvif->vdev_id,
6453 							WMI_PEER_PARAM_FIXED_RATE,
6454 							WMI_FIXED_RATE_NONE);
6455 			if (err)
6456 				ath12k_warn(ar->ab,
6457 					    "failed to disable peer fixed rate for STA %pM ret %d\n",
6458 					    arsta->addr, err);
6459 
6460 			ath12k_peer_assoc_prepare(ar, arvif, arsta,
6461 						  peer_arg, true);
6462 
6463 			peer_arg->is_assoc = false;
6464 			err = ath12k_wmi_send_peer_assoc_cmd(ar, peer_arg);
6465 			if (err)
6466 				ath12k_warn(ar->ab, "failed to run peer assoc for STA %pM vdev %i: %d\n",
6467 					    arsta->addr, arvif->vdev_id, err);
6468 
6469 			if (!wait_for_completion_timeout(&ar->peer_assoc_done, 1 * HZ))
6470 				ath12k_warn(ar->ab, "failed to get peer assoc conf event for %pM vdev %i\n",
6471 					    arsta->addr, arvif->vdev_id);
6472 		}
6473 	}
6474 }
6475 
6476 static void ath12k_mac_free_unassign_link_sta(struct ath12k_hw *ah,
6477 					      struct ath12k_sta *ahsta,
6478 					      u8 link_id)
6479 {
6480 	struct ath12k_link_sta *arsta;
6481 
6482 	lockdep_assert_wiphy(ah->hw->wiphy);
6483 
6484 	if (WARN_ON(link_id >= IEEE80211_MLD_MAX_NUM_LINKS))
6485 		return;
6486 
6487 	arsta = wiphy_dereference(ah->hw->wiphy, ahsta->link[link_id]);
6488 	if (WARN_ON(!arsta))
6489 		return;
6490 
6491 	ahsta->links_map &= ~BIT(link_id);
6492 	rcu_assign_pointer(ahsta->link[link_id], NULL);
6493 	synchronize_rcu();
6494 
6495 	if (arsta == &ahsta->deflink) {
6496 		arsta->link_id = ATH12K_INVALID_LINK_ID;
6497 		arsta->ahsta = NULL;
6498 		arsta->arvif = NULL;
6499 		return;
6500 	}
6501 
6502 	kfree(arsta);
6503 }
6504 
6505 static int ath12k_mac_inc_num_stations(struct ath12k_link_vif *arvif,
6506 				       struct ath12k_link_sta *arsta)
6507 {
6508 	struct ieee80211_sta *sta = ath12k_ahsta_to_sta(arsta->ahsta);
6509 	struct ath12k *ar = arvif->ar;
6510 
6511 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
6512 
6513 	if (arvif->ahvif->vdev_type == WMI_VDEV_TYPE_STA && !sta->tdls)
6514 		return 0;
6515 
6516 	if (ar->num_stations >= ar->max_num_stations)
6517 		return -ENOBUFS;
6518 
6519 	ar->num_stations++;
6520 	arvif->num_stations++;
6521 
6522 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
6523 		   "mac station %pM connected to vdev %u num_stations %u\n",
6524 		   arsta->addr, arvif->vdev_id, arvif->num_stations);
6525 
6526 	return 0;
6527 }
6528 
6529 static void ath12k_mac_dec_num_stations(struct ath12k_link_vif *arvif,
6530 					struct ath12k_link_sta *arsta)
6531 {
6532 	struct ieee80211_sta *sta = ath12k_ahsta_to_sta(arsta->ahsta);
6533 	struct ath12k *ar = arvif->ar;
6534 
6535 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
6536 
6537 	if (arvif->ahvif->vdev_type == WMI_VDEV_TYPE_STA && !sta->tdls)
6538 		return;
6539 
6540 	ar->num_stations--;
6541 
6542 	if (arvif->num_stations) {
6543 		arvif->num_stations--;
6544 		ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
6545 			   "mac station %pM disconnected from vdev %u num_stations %u\n",
6546 			   arsta->addr, arvif->vdev_id, arvif->num_stations);
6547 	} else {
6548 		ath12k_warn(ar->ab,
6549 			    "mac station %pM disconnect for vdev %u without any connected station\n",
6550 			    arsta->addr, arvif->vdev_id);
6551 	}
6552 }
6553 
6554 static void ath12k_mac_station_post_remove(struct ath12k *ar,
6555 					   struct ath12k_link_vif *arvif,
6556 					   struct ath12k_link_sta *arsta)
6557 {
6558 	struct ieee80211_vif *vif = ath12k_ahvif_to_vif(arvif->ahvif);
6559 	struct ieee80211_sta *sta = ath12k_ahsta_to_sta(arsta->ahsta);
6560 	struct ath12k_peer *peer;
6561 
6562 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
6563 
6564 	ath12k_mac_dec_num_stations(arvif, arsta);
6565 
6566 	spin_lock_bh(&ar->ab->base_lock);
6567 
6568 	peer = ath12k_peer_find(ar->ab, arvif->vdev_id, arsta->addr);
6569 	if (peer && peer->sta == sta) {
6570 		ath12k_warn(ar->ab, "Found peer entry %pM n vdev %i after it was supposedly removed\n",
6571 			    vif->addr, arvif->vdev_id);
6572 		peer->sta = NULL;
6573 		list_del(&peer->list);
6574 		kfree(peer);
6575 		ar->num_peers--;
6576 	}
6577 
6578 	spin_unlock_bh(&ar->ab->base_lock);
6579 
6580 	kfree(arsta->rx_stats);
6581 	arsta->rx_stats = NULL;
6582 }
6583 
6584 static int ath12k_mac_station_unauthorize(struct ath12k *ar,
6585 					  struct ath12k_link_vif *arvif,
6586 					  struct ath12k_link_sta *arsta)
6587 {
6588 	struct ath12k_peer *peer;
6589 	int ret;
6590 
6591 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
6592 
6593 	spin_lock_bh(&ar->ab->base_lock);
6594 
6595 	peer = ath12k_peer_find(ar->ab, arvif->vdev_id, arsta->addr);
6596 	if (peer)
6597 		peer->is_authorized = false;
6598 
6599 	spin_unlock_bh(&ar->ab->base_lock);
6600 
6601 	/* Driver must clear the keys during the state change from
6602 	 * IEEE80211_STA_AUTHORIZED to IEEE80211_STA_ASSOC, since after
6603 	 * returning from here, mac80211 is going to delete the keys
6604 	 * in __sta_info_destroy_part2(). This will ensure that the driver does
6605 	 * not retain stale key references after mac80211 deletes the keys.
6606 	 */
6607 	ret = ath12k_clear_peer_keys(arvif, arsta->addr);
6608 	if (ret) {
6609 		ath12k_warn(ar->ab, "failed to clear all peer keys for vdev %i: %d\n",
6610 			    arvif->vdev_id, ret);
6611 		return ret;
6612 	}
6613 
6614 	return 0;
6615 }
6616 
6617 static int ath12k_mac_station_authorize(struct ath12k *ar,
6618 					struct ath12k_link_vif *arvif,
6619 					struct ath12k_link_sta *arsta)
6620 {
6621 	struct ath12k_peer *peer;
6622 	struct ieee80211_vif *vif = ath12k_ahvif_to_vif(arvif->ahvif);
6623 	int ret;
6624 
6625 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
6626 
6627 	spin_lock_bh(&ar->ab->base_lock);
6628 
6629 	peer = ath12k_peer_find(ar->ab, arvif->vdev_id, arsta->addr);
6630 	if (peer)
6631 		peer->is_authorized = true;
6632 
6633 	spin_unlock_bh(&ar->ab->base_lock);
6634 
6635 	if (vif->type == NL80211_IFTYPE_STATION && arvif->is_up) {
6636 		ret = ath12k_wmi_set_peer_param(ar, arsta->addr,
6637 						arvif->vdev_id,
6638 						WMI_PEER_AUTHORIZE,
6639 						1);
6640 		if (ret) {
6641 			ath12k_warn(ar->ab, "Unable to authorize peer %pM vdev %d: %d\n",
6642 				    arsta->addr, arvif->vdev_id, ret);
6643 			return ret;
6644 		}
6645 	}
6646 
6647 	return 0;
6648 }
6649 
6650 static int ath12k_mac_station_remove(struct ath12k *ar,
6651 				     struct ath12k_link_vif *arvif,
6652 				     struct ath12k_link_sta *arsta)
6653 {
6654 	struct ieee80211_sta *sta = ath12k_ahsta_to_sta(arsta->ahsta);
6655 	struct ath12k_vif *ahvif = arvif->ahvif;
6656 	int ret = 0;
6657 
6658 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
6659 
6660 	wiphy_work_cancel(ar->ah->hw->wiphy, &arsta->update_wk);
6661 
6662 	if (ahvif->vdev_type == WMI_VDEV_TYPE_STA) {
6663 		ath12k_bss_disassoc(ar, arvif);
6664 		ret = ath12k_mac_vdev_stop(arvif);
6665 		if (ret)
6666 			ath12k_warn(ar->ab, "failed to stop vdev %i: %d\n",
6667 				    arvif->vdev_id, ret);
6668 	}
6669 
6670 	if (sta->mlo)
6671 		return ret;
6672 
6673 	ath12k_dp_peer_cleanup(ar, arvif->vdev_id, arsta->addr);
6674 
6675 	ret = ath12k_peer_delete(ar, arvif->vdev_id, arsta->addr);
6676 	if (ret)
6677 		ath12k_warn(ar->ab, "Failed to delete peer: %pM for VDEV: %d\n",
6678 			    arsta->addr, arvif->vdev_id);
6679 	else
6680 		ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "Removed peer: %pM for VDEV: %d\n",
6681 			   arsta->addr, arvif->vdev_id);
6682 
6683 	ath12k_mac_station_post_remove(ar, arvif, arsta);
6684 
6685 	if (sta->valid_links)
6686 		ath12k_mac_free_unassign_link_sta(ahvif->ah,
6687 						  arsta->ahsta, arsta->link_id);
6688 
6689 	return ret;
6690 }
6691 
6692 static int ath12k_mac_station_add(struct ath12k *ar,
6693 				  struct ath12k_link_vif *arvif,
6694 				  struct ath12k_link_sta *arsta)
6695 {
6696 	struct ath12k_base *ab = ar->ab;
6697 	struct ieee80211_vif *vif = ath12k_ahvif_to_vif(arvif->ahvif);
6698 	struct ieee80211_sta *sta = ath12k_ahsta_to_sta(arsta->ahsta);
6699 	struct ath12k_wmi_peer_create_arg peer_param = {};
6700 	int ret;
6701 
6702 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
6703 
6704 	ret = ath12k_mac_inc_num_stations(arvif, arsta);
6705 	if (ret) {
6706 		ath12k_warn(ab, "refusing to associate station: too many connected already (%d)\n",
6707 			    ar->max_num_stations);
6708 		goto exit;
6709 	}
6710 
6711 	if (ath12k_debugfs_is_extd_rx_stats_enabled(ar) && !arsta->rx_stats) {
6712 		arsta->rx_stats = kzalloc(sizeof(*arsta->rx_stats), GFP_KERNEL);
6713 		if (!arsta->rx_stats) {
6714 			ret = -ENOMEM;
6715 			goto dec_num_station;
6716 		}
6717 	}
6718 
6719 	peer_param.vdev_id = arvif->vdev_id;
6720 	peer_param.peer_addr = arsta->addr;
6721 	peer_param.peer_type = WMI_PEER_TYPE_DEFAULT;
6722 	peer_param.ml_enabled = sta->mlo;
6723 
6724 	ret = ath12k_peer_create(ar, arvif, sta, &peer_param);
6725 	if (ret) {
6726 		ath12k_warn(ab, "Failed to add peer: %pM for VDEV: %d\n",
6727 			    arsta->addr, arvif->vdev_id);
6728 		goto free_peer;
6729 	}
6730 
6731 	ath12k_dbg(ab, ATH12K_DBG_MAC, "Added peer: %pM for VDEV: %d\n",
6732 		   arsta->addr, arvif->vdev_id);
6733 
6734 	if (ieee80211_vif_is_mesh(vif)) {
6735 		ret = ath12k_wmi_set_peer_param(ar, arsta->addr,
6736 						arvif->vdev_id,
6737 						WMI_PEER_USE_4ADDR, 1);
6738 		if (ret) {
6739 			ath12k_warn(ab, "failed to STA %pM 4addr capability: %d\n",
6740 				    arsta->addr, ret);
6741 			goto free_peer;
6742 		}
6743 	}
6744 
6745 	ret = ath12k_dp_peer_setup(ar, arvif->vdev_id, arsta->addr);
6746 	if (ret) {
6747 		ath12k_warn(ab, "failed to setup dp for peer %pM on vdev %i (%d)\n",
6748 			    arsta->addr, arvif->vdev_id, ret);
6749 		goto free_peer;
6750 	}
6751 
6752 	if (ab->hw_params->vdev_start_delay &&
6753 	    !arvif->is_started &&
6754 	    arvif->ahvif->vdev_type != WMI_VDEV_TYPE_AP) {
6755 		ret = ath12k_start_vdev_delay(ar, arvif);
6756 		if (ret) {
6757 			ath12k_warn(ab, "failed to delay vdev start: %d\n", ret);
6758 			goto free_peer;
6759 		}
6760 	}
6761 
6762 	ewma_avg_rssi_init(&arsta->avg_rssi);
6763 	return 0;
6764 
6765 free_peer:
6766 	ath12k_peer_delete(ar, arvif->vdev_id, arsta->addr);
6767 	kfree(arsta->rx_stats);
6768 	arsta->rx_stats = NULL;
6769 dec_num_station:
6770 	ath12k_mac_dec_num_stations(arvif, arsta);
6771 exit:
6772 	return ret;
6773 }
6774 
6775 static int ath12k_mac_assign_link_sta(struct ath12k_hw *ah,
6776 				      struct ath12k_sta *ahsta,
6777 				      struct ath12k_link_sta *arsta,
6778 				      struct ath12k_vif *ahvif,
6779 				      u8 link_id)
6780 {
6781 	struct ieee80211_sta *sta = ath12k_ahsta_to_sta(ahsta);
6782 	struct ieee80211_link_sta *link_sta;
6783 	struct ath12k_link_vif *arvif;
6784 
6785 	lockdep_assert_wiphy(ah->hw->wiphy);
6786 
6787 	if (!arsta || link_id >= IEEE80211_MLD_MAX_NUM_LINKS)
6788 		return -EINVAL;
6789 
6790 	arvif = wiphy_dereference(ah->hw->wiphy, ahvif->link[link_id]);
6791 	if (!arvif)
6792 		return -EINVAL;
6793 
6794 	memset(arsta, 0, sizeof(*arsta));
6795 
6796 	link_sta = wiphy_dereference(ah->hw->wiphy, sta->link[link_id]);
6797 	if (!link_sta)
6798 		return -EINVAL;
6799 
6800 	ether_addr_copy(arsta->addr, link_sta->addr);
6801 
6802 	/* logical index of the link sta in order of creation */
6803 	arsta->link_idx = ahsta->num_peer++;
6804 
6805 	arsta->link_id = link_id;
6806 	ahsta->links_map |= BIT(arsta->link_id);
6807 	arsta->arvif = arvif;
6808 	arsta->ahsta = ahsta;
6809 	ahsta->ahvif = ahvif;
6810 
6811 	wiphy_work_init(&arsta->update_wk, ath12k_sta_rc_update_wk);
6812 
6813 	rcu_assign_pointer(ahsta->link[link_id], arsta);
6814 
6815 	return 0;
6816 }
6817 
6818 static void ath12k_mac_ml_station_remove(struct ath12k_vif *ahvif,
6819 					 struct ath12k_sta *ahsta)
6820 {
6821 	struct ieee80211_sta *sta = ath12k_ahsta_to_sta(ahsta);
6822 	struct ath12k_hw *ah = ahvif->ah;
6823 	struct ath12k_link_vif *arvif;
6824 	struct ath12k_link_sta *arsta;
6825 	unsigned long links;
6826 	struct ath12k *ar;
6827 	u8 link_id;
6828 
6829 	lockdep_assert_wiphy(ah->hw->wiphy);
6830 
6831 	ath12k_peer_mlo_link_peers_delete(ahvif, ahsta);
6832 
6833 	/* validate link station removal and clear arsta links */
6834 	links = ahsta->links_map;
6835 	for_each_set_bit(link_id, &links, IEEE80211_MLD_MAX_NUM_LINKS) {
6836 		arvif = wiphy_dereference(ah->hw->wiphy, ahvif->link[link_id]);
6837 		arsta = wiphy_dereference(ah->hw->wiphy, ahsta->link[link_id]);
6838 		if (!arvif || !arsta)
6839 			continue;
6840 
6841 		ar = arvif->ar;
6842 
6843 		ath12k_mac_station_post_remove(ar, arvif, arsta);
6844 
6845 		ath12k_mac_free_unassign_link_sta(ah, ahsta, link_id);
6846 	}
6847 
6848 	ath12k_peer_ml_delete(ah, sta);
6849 }
6850 
6851 static int ath12k_mac_handle_link_sta_state(struct ieee80211_hw *hw,
6852 					    struct ath12k_link_vif *arvif,
6853 					    struct ath12k_link_sta *arsta,
6854 					    enum ieee80211_sta_state old_state,
6855 					    enum ieee80211_sta_state new_state)
6856 {
6857 	struct ieee80211_vif *vif = ath12k_ahvif_to_vif(arvif->ahvif);
6858 	struct ieee80211_bss_conf *link_conf;
6859 	struct ath12k *ar = arvif->ar;
6860 	struct ath12k_reg_info *reg_info;
6861 	struct ath12k_base *ab = ar->ab;
6862 	int ret = 0;
6863 
6864 	lockdep_assert_wiphy(hw->wiphy);
6865 
6866 	ath12k_dbg(ab, ATH12K_DBG_MAC, "mac handle link %u sta %pM state %d -> %d\n",
6867 		   arsta->link_id, arsta->addr, old_state, new_state);
6868 
6869 	/* IEEE80211_STA_NONE -> IEEE80211_STA_NOTEXIST: Remove the station
6870 	 * from driver
6871 	 */
6872 	if ((old_state == IEEE80211_STA_NONE &&
6873 	     new_state == IEEE80211_STA_NOTEXIST)) {
6874 		ret = ath12k_mac_station_remove(ar, arvif, arsta);
6875 		if (ret) {
6876 			ath12k_warn(ab, "Failed to remove station: %pM for VDEV: %d\n",
6877 				    arsta->addr, arvif->vdev_id);
6878 			goto exit;
6879 		}
6880 	}
6881 
6882 	/* IEEE80211_STA_NOTEXIST -> IEEE80211_STA_NONE: Add new station to driver */
6883 	if (old_state == IEEE80211_STA_NOTEXIST &&
6884 	    new_state == IEEE80211_STA_NONE) {
6885 		ret = ath12k_mac_station_add(ar, arvif, arsta);
6886 		if (ret)
6887 			ath12k_warn(ab, "Failed to add station: %pM for VDEV: %d\n",
6888 				    arsta->addr, arvif->vdev_id);
6889 
6890 	/* IEEE80211_STA_AUTH -> IEEE80211_STA_ASSOC: Send station assoc command for
6891 	 * peer associated to AP/Mesh/ADHOC vif type.
6892 	 */
6893 	} else if (old_state == IEEE80211_STA_AUTH &&
6894 		   new_state == IEEE80211_STA_ASSOC &&
6895 		   (vif->type == NL80211_IFTYPE_AP ||
6896 		    vif->type == NL80211_IFTYPE_MESH_POINT ||
6897 		    vif->type == NL80211_IFTYPE_ADHOC)) {
6898 		ret = ath12k_mac_station_assoc(ar, arvif, arsta, false);
6899 		if (ret)
6900 			ath12k_warn(ab, "Failed to associate station: %pM\n",
6901 				    arsta->addr);
6902 
6903 	/* IEEE80211_STA_ASSOC -> IEEE80211_STA_AUTHORIZED: set peer status as
6904 	 * authorized
6905 	 */
6906 	} else if (old_state == IEEE80211_STA_ASSOC &&
6907 		   new_state == IEEE80211_STA_AUTHORIZED) {
6908 		ret = ath12k_mac_station_authorize(ar, arvif, arsta);
6909 		if (ret) {
6910 			ath12k_warn(ab, "Failed to authorize station: %pM\n",
6911 				    arsta->addr);
6912 			goto exit;
6913 		}
6914 
6915 		if (ath12k_wmi_supports_6ghz_cc_ext(ar) &&
6916 		    arvif->ahvif->vdev_type == WMI_VDEV_TYPE_STA) {
6917 			link_conf = ath12k_mac_get_link_bss_conf(arvif);
6918 			reg_info = ab->reg_info[ar->pdev_idx];
6919 			ath12k_dbg(ab, ATH12K_DBG_MAC, "connection done, update reg rules\n");
6920 			ath12k_hw_to_ah(hw)->regd_updated = false;
6921 			ath12k_reg_handle_chan_list(ab, reg_info, arvif->ahvif->vdev_type,
6922 						    link_conf->power_type);
6923 		}
6924 
6925 	/* IEEE80211_STA_AUTHORIZED -> IEEE80211_STA_ASSOC: station may be in removal,
6926 	 * deauthorize it.
6927 	 */
6928 	} else if (old_state == IEEE80211_STA_AUTHORIZED &&
6929 		   new_state == IEEE80211_STA_ASSOC) {
6930 		ath12k_mac_station_unauthorize(ar, arvif, arsta);
6931 
6932 	/* IEEE80211_STA_ASSOC -> IEEE80211_STA_AUTH: disassoc peer connected to
6933 	 * AP/mesh/ADHOC vif type.
6934 	 */
6935 	} else if (old_state == IEEE80211_STA_ASSOC &&
6936 		   new_state == IEEE80211_STA_AUTH &&
6937 		   (vif->type == NL80211_IFTYPE_AP ||
6938 		    vif->type == NL80211_IFTYPE_MESH_POINT ||
6939 		    vif->type == NL80211_IFTYPE_ADHOC)) {
6940 		ret = ath12k_mac_station_disassoc(ar, arvif, arsta);
6941 		if (ret)
6942 			ath12k_warn(ab, "Failed to disassociate station: %pM\n",
6943 				    arsta->addr);
6944 	}
6945 
6946 exit:
6947 	return ret;
6948 }
6949 
6950 static bool ath12k_mac_is_freq_on_mac(struct ath12k_hw_mode_freq_range_arg *freq_range,
6951 				      u32 freq, u8 mac_id)
6952 {
6953 	return (freq >= freq_range[mac_id].low_2ghz_freq &&
6954 		freq <= freq_range[mac_id].high_2ghz_freq) ||
6955 	       (freq >= freq_range[mac_id].low_5ghz_freq &&
6956 		freq <= freq_range[mac_id].high_5ghz_freq);
6957 }
6958 
6959 static bool
6960 ath12k_mac_2_freq_same_mac_in_freq_range(struct ath12k_base *ab,
6961 					 struct ath12k_hw_mode_freq_range_arg *freq_range,
6962 					 u32 freq_link1, u32 freq_link2)
6963 {
6964 	u8 i;
6965 
6966 	for (i = 0; i < MAX_RADIOS; i++) {
6967 		if (ath12k_mac_is_freq_on_mac(freq_range, freq_link1, i) &&
6968 		    ath12k_mac_is_freq_on_mac(freq_range, freq_link2, i))
6969 			return true;
6970 	}
6971 
6972 	return false;
6973 }
6974 
6975 static bool ath12k_mac_is_hw_dbs_capable(struct ath12k_base *ab)
6976 {
6977 	return test_bit(WMI_TLV_SERVICE_DUAL_BAND_SIMULTANEOUS_SUPPORT,
6978 			ab->wmi_ab.svc_map) &&
6979 	       ab->wmi_ab.hw_mode_info.support_dbs;
6980 }
6981 
6982 static bool ath12k_mac_2_freq_same_mac_in_dbs(struct ath12k_base *ab,
6983 					      u32 freq_link1, u32 freq_link2)
6984 {
6985 	struct ath12k_hw_mode_freq_range_arg *freq_range;
6986 
6987 	if (!ath12k_mac_is_hw_dbs_capable(ab))
6988 		return true;
6989 
6990 	freq_range = ab->wmi_ab.hw_mode_info.freq_range_caps[ATH12K_HW_MODE_DBS];
6991 	return ath12k_mac_2_freq_same_mac_in_freq_range(ab, freq_range,
6992 							freq_link1, freq_link2);
6993 }
6994 
6995 static bool ath12k_mac_is_hw_sbs_capable(struct ath12k_base *ab)
6996 {
6997 	return test_bit(WMI_TLV_SERVICE_DUAL_BAND_SIMULTANEOUS_SUPPORT,
6998 			ab->wmi_ab.svc_map) &&
6999 	       ab->wmi_ab.hw_mode_info.support_sbs;
7000 }
7001 
7002 static bool ath12k_mac_2_freq_same_mac_in_sbs(struct ath12k_base *ab,
7003 					      u32 freq_link1, u32 freq_link2)
7004 {
7005 	struct ath12k_hw_mode_info *info = &ab->wmi_ab.hw_mode_info;
7006 	struct ath12k_hw_mode_freq_range_arg *sbs_uppr_share;
7007 	struct ath12k_hw_mode_freq_range_arg *sbs_low_share;
7008 	struct ath12k_hw_mode_freq_range_arg *sbs_range;
7009 
7010 	if (!ath12k_mac_is_hw_sbs_capable(ab))
7011 		return true;
7012 
7013 	if (ab->wmi_ab.sbs_lower_band_end_freq) {
7014 		sbs_uppr_share = info->freq_range_caps[ATH12K_HW_MODE_SBS_UPPER_SHARE];
7015 		sbs_low_share = info->freq_range_caps[ATH12K_HW_MODE_SBS_LOWER_SHARE];
7016 
7017 		return ath12k_mac_2_freq_same_mac_in_freq_range(ab, sbs_low_share,
7018 								freq_link1, freq_link2) ||
7019 		       ath12k_mac_2_freq_same_mac_in_freq_range(ab, sbs_uppr_share,
7020 								freq_link1, freq_link2);
7021 	}
7022 
7023 	sbs_range = info->freq_range_caps[ATH12K_HW_MODE_SBS];
7024 	return ath12k_mac_2_freq_same_mac_in_freq_range(ab, sbs_range,
7025 							freq_link1, freq_link2);
7026 }
7027 
7028 static bool ath12k_mac_freqs_on_same_mac(struct ath12k_base *ab,
7029 					 u32 freq_link1, u32 freq_link2)
7030 {
7031 	return ath12k_mac_2_freq_same_mac_in_dbs(ab, freq_link1, freq_link2) &&
7032 	       ath12k_mac_2_freq_same_mac_in_sbs(ab, freq_link1, freq_link2);
7033 }
7034 
7035 static int ath12k_mac_mlo_sta_set_link_active(struct ath12k_base *ab,
7036 					      enum wmi_mlo_link_force_reason reason,
7037 					      enum wmi_mlo_link_force_mode mode,
7038 					      u8 *mlo_vdev_id_lst,
7039 					      u8 num_mlo_vdev,
7040 					      u8 *mlo_inactive_vdev_lst,
7041 					      u8 num_mlo_inactive_vdev)
7042 {
7043 	struct wmi_mlo_link_set_active_arg param = {};
7044 	u32 entry_idx, entry_offset, vdev_idx;
7045 	u8 vdev_id;
7046 
7047 	param.reason = reason;
7048 	param.force_mode = mode;
7049 
7050 	for (vdev_idx = 0; vdev_idx < num_mlo_vdev; vdev_idx++) {
7051 		vdev_id = mlo_vdev_id_lst[vdev_idx];
7052 		entry_idx = vdev_id / 32;
7053 		entry_offset = vdev_id % 32;
7054 		if (entry_idx >= WMI_MLO_LINK_NUM_SZ) {
7055 			ath12k_warn(ab, "Invalid entry_idx %d num_mlo_vdev %d vdev %d",
7056 				    entry_idx, num_mlo_vdev, vdev_id);
7057 			return -EINVAL;
7058 		}
7059 		param.vdev_bitmap[entry_idx] |= 1 << entry_offset;
7060 		/* update entry number if entry index changed */
7061 		if (param.num_vdev_bitmap < entry_idx + 1)
7062 			param.num_vdev_bitmap = entry_idx + 1;
7063 	}
7064 
7065 	ath12k_dbg(ab, ATH12K_DBG_MAC,
7066 		   "num_vdev_bitmap %d vdev_bitmap[0] = 0x%x, vdev_bitmap[1] = 0x%x",
7067 		   param.num_vdev_bitmap, param.vdev_bitmap[0], param.vdev_bitmap[1]);
7068 
7069 	if (mode == WMI_MLO_LINK_FORCE_MODE_ACTIVE_INACTIVE) {
7070 		for (vdev_idx = 0; vdev_idx < num_mlo_inactive_vdev; vdev_idx++) {
7071 			vdev_id = mlo_inactive_vdev_lst[vdev_idx];
7072 			entry_idx = vdev_id / 32;
7073 			entry_offset = vdev_id % 32;
7074 			if (entry_idx >= WMI_MLO_LINK_NUM_SZ) {
7075 				ath12k_warn(ab, "Invalid entry_idx %d num_mlo_vdev %d vdev %d",
7076 					    entry_idx, num_mlo_inactive_vdev, vdev_id);
7077 				return -EINVAL;
7078 			}
7079 			param.inactive_vdev_bitmap[entry_idx] |= 1 << entry_offset;
7080 			/* update entry number if entry index changed */
7081 			if (param.num_inactive_vdev_bitmap < entry_idx + 1)
7082 				param.num_inactive_vdev_bitmap = entry_idx + 1;
7083 		}
7084 
7085 		ath12k_dbg(ab, ATH12K_DBG_MAC,
7086 			   "num_vdev_bitmap %d inactive_vdev_bitmap[0] = 0x%x, inactive_vdev_bitmap[1] = 0x%x",
7087 			   param.num_inactive_vdev_bitmap,
7088 			   param.inactive_vdev_bitmap[0],
7089 			   param.inactive_vdev_bitmap[1]);
7090 	}
7091 
7092 	if (mode == WMI_MLO_LINK_FORCE_MODE_ACTIVE_LINK_NUM ||
7093 	    mode == WMI_MLO_LINK_FORCE_MODE_INACTIVE_LINK_NUM) {
7094 		param.num_link_entry = 1;
7095 		param.link_num[0].num_of_link = num_mlo_vdev - 1;
7096 	}
7097 
7098 	return ath12k_wmi_send_mlo_link_set_active_cmd(ab, &param);
7099 }
7100 
7101 static int ath12k_mac_mlo_sta_update_link_active(struct ath12k_base *ab,
7102 						 struct ieee80211_hw *hw,
7103 						 struct ath12k_vif *ahvif)
7104 {
7105 	u8 mlo_vdev_id_lst[IEEE80211_MLD_MAX_NUM_LINKS] = {};
7106 	u32 mlo_freq_list[IEEE80211_MLD_MAX_NUM_LINKS] = {};
7107 	unsigned long links = ahvif->links_map;
7108 	enum wmi_mlo_link_force_reason reason;
7109 	struct ieee80211_chanctx_conf *conf;
7110 	enum wmi_mlo_link_force_mode mode;
7111 	struct ieee80211_bss_conf *info;
7112 	struct ath12k_link_vif *arvif;
7113 	u8 num_mlo_vdev = 0;
7114 	u8 link_id;
7115 
7116 	for_each_set_bit(link_id, &links, IEEE80211_MLD_MAX_NUM_LINKS) {
7117 		arvif = wiphy_dereference(hw->wiphy, ahvif->link[link_id]);
7118 		/* make sure vdev is created on this device */
7119 		if (!arvif || !arvif->is_created || arvif->ar->ab != ab)
7120 			continue;
7121 
7122 		info = ath12k_mac_get_link_bss_conf(arvif);
7123 		conf = wiphy_dereference(hw->wiphy, info->chanctx_conf);
7124 		mlo_freq_list[num_mlo_vdev] = conf->def.chan->center_freq;
7125 
7126 		mlo_vdev_id_lst[num_mlo_vdev] = arvif->vdev_id;
7127 		num_mlo_vdev++;
7128 	}
7129 
7130 	/* It is not allowed to activate more links than a single device
7131 	 * supported. Something goes wrong if we reach here.
7132 	 */
7133 	if (num_mlo_vdev > ATH12K_NUM_MAX_ACTIVE_LINKS_PER_DEVICE) {
7134 		WARN_ON_ONCE(1);
7135 		return -EINVAL;
7136 	}
7137 
7138 	/* if 2 links are established and both link channels fall on the
7139 	 * same hardware MAC, send command to firmware to deactivate one
7140 	 * of them.
7141 	 */
7142 	if (num_mlo_vdev == 2 &&
7143 	    ath12k_mac_freqs_on_same_mac(ab, mlo_freq_list[0],
7144 					 mlo_freq_list[1])) {
7145 		mode = WMI_MLO_LINK_FORCE_MODE_INACTIVE_LINK_NUM;
7146 		reason = WMI_MLO_LINK_FORCE_REASON_NEW_CONNECT;
7147 		return ath12k_mac_mlo_sta_set_link_active(ab, reason, mode,
7148 							  mlo_vdev_id_lst, num_mlo_vdev,
7149 							  NULL, 0);
7150 	}
7151 
7152 	return 0;
7153 }
7154 
7155 static bool ath12k_mac_are_sbs_chan(struct ath12k_base *ab, u32 freq_1, u32 freq_2)
7156 {
7157 	if (!ath12k_mac_is_hw_sbs_capable(ab))
7158 		return false;
7159 
7160 	if (ath12k_is_2ghz_channel_freq(freq_1) ||
7161 	    ath12k_is_2ghz_channel_freq(freq_2))
7162 		return false;
7163 
7164 	return !ath12k_mac_2_freq_same_mac_in_sbs(ab, freq_1, freq_2);
7165 }
7166 
7167 static bool ath12k_mac_are_dbs_chan(struct ath12k_base *ab, u32 freq_1, u32 freq_2)
7168 {
7169 	if (!ath12k_mac_is_hw_dbs_capable(ab))
7170 		return false;
7171 
7172 	return !ath12k_mac_2_freq_same_mac_in_dbs(ab, freq_1, freq_2);
7173 }
7174 
7175 static int ath12k_mac_select_links(struct ath12k_base *ab,
7176 				   struct ieee80211_vif *vif,
7177 				   struct ieee80211_hw *hw,
7178 				   u16 *selected_links)
7179 {
7180 	unsigned long useful_links = ieee80211_vif_usable_links(vif);
7181 	struct ath12k_vif *ahvif = ath12k_vif_to_ahvif(vif);
7182 	u8 num_useful_links = hweight_long(useful_links);
7183 	struct ieee80211_chanctx_conf *chanctx;
7184 	struct ath12k_link_vif *assoc_arvif;
7185 	u32 assoc_link_freq, partner_freq;
7186 	u16 sbs_links = 0, dbs_links = 0;
7187 	struct ieee80211_bss_conf *info;
7188 	struct ieee80211_channel *chan;
7189 	struct ieee80211_sta *sta;
7190 	struct ath12k_sta *ahsta;
7191 	u8 link_id;
7192 
7193 	/* activate all useful links if less than max supported */
7194 	if (num_useful_links <= ATH12K_NUM_MAX_ACTIVE_LINKS_PER_DEVICE) {
7195 		*selected_links = useful_links;
7196 		return 0;
7197 	}
7198 
7199 	/* only in station mode we can get here, so it's safe
7200 	 * to use ap_addr
7201 	 */
7202 	rcu_read_lock();
7203 	sta = ieee80211_find_sta(vif, vif->cfg.ap_addr);
7204 	if (!sta) {
7205 		rcu_read_unlock();
7206 		ath12k_warn(ab, "failed to find sta with addr %pM\n", vif->cfg.ap_addr);
7207 		return -EINVAL;
7208 	}
7209 
7210 	ahsta = ath12k_sta_to_ahsta(sta);
7211 	assoc_arvif = wiphy_dereference(hw->wiphy, ahvif->link[ahsta->assoc_link_id]);
7212 	info = ath12k_mac_get_link_bss_conf(assoc_arvif);
7213 	chanctx = rcu_dereference(info->chanctx_conf);
7214 	assoc_link_freq = chanctx->def.chan->center_freq;
7215 	rcu_read_unlock();
7216 	ath12k_dbg(ab, ATH12K_DBG_MAC, "assoc link %u freq %u\n",
7217 		   assoc_arvif->link_id, assoc_link_freq);
7218 
7219 	/* assoc link is already activated and has to be kept active,
7220 	 * only need to select a partner link from others.
7221 	 */
7222 	useful_links &= ~BIT(assoc_arvif->link_id);
7223 	for_each_set_bit(link_id, &useful_links, IEEE80211_MLD_MAX_NUM_LINKS) {
7224 		info = wiphy_dereference(hw->wiphy, vif->link_conf[link_id]);
7225 		if (!info) {
7226 			ath12k_warn(ab, "failed to get link info for link: %u\n",
7227 				    link_id);
7228 			return -ENOLINK;
7229 		}
7230 
7231 		chan = info->chanreq.oper.chan;
7232 		if (!chan) {
7233 			ath12k_warn(ab, "failed to get chan for link: %u\n", link_id);
7234 			return -EINVAL;
7235 		}
7236 
7237 		partner_freq = chan->center_freq;
7238 		if (ath12k_mac_are_sbs_chan(ab, assoc_link_freq, partner_freq)) {
7239 			sbs_links |= BIT(link_id);
7240 			ath12k_dbg(ab, ATH12K_DBG_MAC, "new SBS link %u freq %u\n",
7241 				   link_id, partner_freq);
7242 			continue;
7243 		}
7244 
7245 		if (ath12k_mac_are_dbs_chan(ab, assoc_link_freq, partner_freq)) {
7246 			dbs_links |= BIT(link_id);
7247 			ath12k_dbg(ab, ATH12K_DBG_MAC, "new DBS link %u freq %u\n",
7248 				   link_id, partner_freq);
7249 			continue;
7250 		}
7251 
7252 		ath12k_dbg(ab, ATH12K_DBG_MAC, "non DBS/SBS link %u freq %u\n",
7253 			   link_id, partner_freq);
7254 	}
7255 
7256 	/* choose the first candidate no matter how many is in the list */
7257 	if (sbs_links)
7258 		link_id = __ffs(sbs_links);
7259 	else if (dbs_links)
7260 		link_id = __ffs(dbs_links);
7261 	else
7262 		link_id = ffs(useful_links) - 1;
7263 
7264 	ath12k_dbg(ab, ATH12K_DBG_MAC, "select partner link %u\n", link_id);
7265 
7266 	*selected_links = BIT(assoc_arvif->link_id) | BIT(link_id);
7267 
7268 	return 0;
7269 }
7270 
7271 static int ath12k_mac_op_sta_state(struct ieee80211_hw *hw,
7272 				   struct ieee80211_vif *vif,
7273 				   struct ieee80211_sta *sta,
7274 				   enum ieee80211_sta_state old_state,
7275 				   enum ieee80211_sta_state new_state)
7276 {
7277 	struct ath12k_vif *ahvif = ath12k_vif_to_ahvif(vif);
7278 	struct ath12k_sta *ahsta = ath12k_sta_to_ahsta(sta);
7279 	struct ath12k_hw *ah = ath12k_hw_to_ah(hw);
7280 	struct ath12k_base *prev_ab = NULL, *ab;
7281 	struct ath12k_link_vif *arvif;
7282 	struct ath12k_link_sta *arsta;
7283 	unsigned long valid_links;
7284 	u16 selected_links = 0;
7285 	u8 link_id = 0, i;
7286 	struct ath12k *ar;
7287 	int ret;
7288 
7289 	lockdep_assert_wiphy(hw->wiphy);
7290 
7291 	if (ieee80211_vif_is_mld(vif) && sta->valid_links) {
7292 		WARN_ON(!sta->mlo && hweight16(sta->valid_links) != 1);
7293 		link_id = ffs(sta->valid_links) - 1;
7294 	}
7295 
7296 	/* IEEE80211_STA_NOTEXIST -> IEEE80211_STA_NONE:
7297 	 * New station add received. If this is a ML station then
7298 	 * ahsta->links_map will be zero and sta->valid_links will be 1.
7299 	 * Assign default link to the first link sta.
7300 	 */
7301 	if (old_state == IEEE80211_STA_NOTEXIST &&
7302 	    new_state == IEEE80211_STA_NONE) {
7303 		memset(ahsta, 0, sizeof(*ahsta));
7304 
7305 		arsta = &ahsta->deflink;
7306 
7307 		/* ML sta */
7308 		if (sta->mlo && !ahsta->links_map &&
7309 		    (hweight16(sta->valid_links) == 1)) {
7310 			ret = ath12k_peer_ml_create(ah, sta);
7311 			if (ret) {
7312 				ath12k_hw_warn(ah, "unable to create ML peer for sta %pM",
7313 					       sta->addr);
7314 				goto exit;
7315 			}
7316 		}
7317 
7318 		ret = ath12k_mac_assign_link_sta(ah, ahsta, arsta, ahvif,
7319 						 link_id);
7320 		if (ret) {
7321 			ath12k_hw_warn(ah, "unable assign link %d for sta %pM",
7322 				       link_id, sta->addr);
7323 			goto exit;
7324 		}
7325 
7326 		/* above arsta will get memset, hence do this after assign
7327 		 * link sta
7328 		 */
7329 		if (sta->mlo) {
7330 			/* For station mode, arvif->is_sta_assoc_link has been set when
7331 			 * vdev starts. Make sure the arvif/arsta pair have same setting
7332 			 */
7333 			if (vif->type == NL80211_IFTYPE_STATION &&
7334 			    !arsta->arvif->is_sta_assoc_link) {
7335 				ath12k_hw_warn(ah, "failed to verify assoc link setting with link id %u\n",
7336 					       link_id);
7337 				ret = -EINVAL;
7338 				goto exit;
7339 			}
7340 
7341 			arsta->is_assoc_link = true;
7342 			ahsta->assoc_link_id = link_id;
7343 		}
7344 	}
7345 
7346 	/* In the ML station scenario, activate all partner links once the
7347 	 * client is transitioning to the associated state.
7348 	 *
7349 	 * FIXME: Ideally, this activation should occur when the client
7350 	 * transitions to the authorized state. However, there are some
7351 	 * issues with handling this in the firmware. Until the firmware
7352 	 * can manage it properly, activate the links when the client is
7353 	 * about to move to the associated state.
7354 	 */
7355 	if (ieee80211_vif_is_mld(vif) && vif->type == NL80211_IFTYPE_STATION &&
7356 	    old_state == IEEE80211_STA_AUTH && new_state == IEEE80211_STA_ASSOC) {
7357 		/* TODO: for now only do link selection for single device
7358 		 * MLO case. Other cases would be handled in the future.
7359 		 */
7360 		ab = ah->radio[0].ab;
7361 		if (ab->ag->num_devices == 1) {
7362 			ret = ath12k_mac_select_links(ab, vif, hw, &selected_links);
7363 			if (ret) {
7364 				ath12k_warn(ab,
7365 					    "failed to get selected links: %d\n", ret);
7366 				goto exit;
7367 			}
7368 		} else {
7369 			selected_links = ieee80211_vif_usable_links(vif);
7370 		}
7371 
7372 		ieee80211_set_active_links(vif, selected_links);
7373 	}
7374 
7375 	/* Handle all the other state transitions in generic way */
7376 	valid_links = ahsta->links_map;
7377 	for_each_set_bit(link_id, &valid_links, IEEE80211_MLD_MAX_NUM_LINKS) {
7378 		arvif = wiphy_dereference(hw->wiphy, ahvif->link[link_id]);
7379 		arsta = wiphy_dereference(hw->wiphy, ahsta->link[link_id]);
7380 		/* some assumptions went wrong! */
7381 		if (WARN_ON(!arvif || !arsta))
7382 			continue;
7383 
7384 		/* vdev might be in deleted */
7385 		if (WARN_ON(!arvif->ar))
7386 			continue;
7387 
7388 		ret = ath12k_mac_handle_link_sta_state(hw, arvif, arsta,
7389 						       old_state, new_state);
7390 		if (ret) {
7391 			ath12k_hw_warn(ah, "unable to move link sta %d of sta %pM from state %d to %d",
7392 				       link_id, arsta->addr, old_state, new_state);
7393 			goto exit;
7394 		}
7395 	}
7396 
7397 	if (ieee80211_vif_is_mld(vif) && vif->type == NL80211_IFTYPE_STATION &&
7398 	    old_state == IEEE80211_STA_ASSOC && new_state == IEEE80211_STA_AUTHORIZED) {
7399 		for_each_ar(ah, ar, i) {
7400 			ab = ar->ab;
7401 			if (prev_ab == ab)
7402 				continue;
7403 
7404 			ret = ath12k_mac_mlo_sta_update_link_active(ab, hw, ahvif);
7405 			if (ret) {
7406 				ath12k_warn(ab,
7407 					    "failed to update link active state on connect %d\n",
7408 					    ret);
7409 				goto exit;
7410 			}
7411 
7412 			prev_ab = ab;
7413 		}
7414 	}
7415 	/* IEEE80211_STA_NONE -> IEEE80211_STA_NOTEXIST:
7416 	 * Remove the station from driver (handle ML sta here since that
7417 	 * needs special handling. Normal sta will be handled in generic
7418 	 * handler below
7419 	 */
7420 	if (old_state == IEEE80211_STA_NONE &&
7421 	    new_state == IEEE80211_STA_NOTEXIST && sta->mlo)
7422 		ath12k_mac_ml_station_remove(ahvif, ahsta);
7423 
7424 	ret = 0;
7425 
7426 exit:
7427 	/* update the state if everything went well */
7428 	if (!ret)
7429 		ahsta->state = new_state;
7430 
7431 	return ret;
7432 }
7433 
7434 static int ath12k_mac_op_sta_set_txpwr(struct ieee80211_hw *hw,
7435 				       struct ieee80211_vif *vif,
7436 				       struct ieee80211_sta *sta)
7437 {
7438 	struct ath12k_sta *ahsta = ath12k_sta_to_ahsta(sta);
7439 	struct ath12k *ar;
7440 	struct ath12k_vif *ahvif = ath12k_vif_to_ahvif(vif);
7441 	struct ath12k_link_vif *arvif;
7442 	struct ath12k_link_sta *arsta;
7443 	u8 link_id;
7444 	int ret;
7445 	s16 txpwr;
7446 
7447 	lockdep_assert_wiphy(hw->wiphy);
7448 
7449 	/* TODO: use link id from mac80211 once that's implemented */
7450 	link_id = 0;
7451 
7452 	arvif = wiphy_dereference(hw->wiphy, ahvif->link[link_id]);
7453 	arsta = wiphy_dereference(hw->wiphy, ahsta->link[link_id]);
7454 
7455 	if (sta->deflink.txpwr.type == NL80211_TX_POWER_AUTOMATIC) {
7456 		txpwr = 0;
7457 	} else {
7458 		txpwr = sta->deflink.txpwr.power;
7459 		if (!txpwr) {
7460 			ret = -EINVAL;
7461 			goto out;
7462 		}
7463 	}
7464 
7465 	if (txpwr > ATH12K_TX_POWER_MAX_VAL || txpwr < ATH12K_TX_POWER_MIN_VAL) {
7466 		ret = -EINVAL;
7467 		goto out;
7468 	}
7469 
7470 	ar = arvif->ar;
7471 
7472 	ret = ath12k_wmi_set_peer_param(ar, arsta->addr, arvif->vdev_id,
7473 					WMI_PEER_USE_FIXED_PWR, txpwr);
7474 	if (ret) {
7475 		ath12k_warn(ar->ab, "failed to set tx power for station ret: %d\n",
7476 			    ret);
7477 		goto out;
7478 	}
7479 
7480 out:
7481 	return ret;
7482 }
7483 
7484 static void ath12k_mac_op_link_sta_rc_update(struct ieee80211_hw *hw,
7485 					     struct ieee80211_vif *vif,
7486 					     struct ieee80211_link_sta *link_sta,
7487 					     u32 changed)
7488 {
7489 	struct ieee80211_sta *sta = link_sta->sta;
7490 	struct ath12k *ar;
7491 	struct ath12k_sta *ahsta = ath12k_sta_to_ahsta(sta);
7492 	struct ath12k_vif *ahvif = ath12k_vif_to_ahvif(vif);
7493 	struct ath12k_hw *ah = ath12k_hw_to_ah(hw);
7494 	struct ath12k_link_sta *arsta;
7495 	struct ath12k_link_vif *arvif;
7496 	struct ath12k_peer *peer;
7497 	u32 bw, smps;
7498 
7499 	rcu_read_lock();
7500 	arvif = rcu_dereference(ahvif->link[link_sta->link_id]);
7501 	if (!arvif) {
7502 		ath12k_hw_warn(ah, "mac sta rc update failed to fetch link vif on link id %u for peer %pM\n",
7503 			       link_sta->link_id, sta->addr);
7504 		rcu_read_unlock();
7505 		return;
7506 	}
7507 
7508 	ar = arvif->ar;
7509 
7510 	arsta = rcu_dereference(ahsta->link[link_sta->link_id]);
7511 	if (!arsta) {
7512 		rcu_read_unlock();
7513 		ath12k_warn(ar->ab, "mac sta rc update failed to fetch link sta on link id %u for peer %pM\n",
7514 			    link_sta->link_id, sta->addr);
7515 		return;
7516 	}
7517 	spin_lock_bh(&ar->ab->base_lock);
7518 
7519 	peer = ath12k_peer_find(ar->ab, arvif->vdev_id, arsta->addr);
7520 	if (!peer) {
7521 		spin_unlock_bh(&ar->ab->base_lock);
7522 		rcu_read_unlock();
7523 		ath12k_warn(ar->ab, "mac sta rc update failed to find peer %pM on vdev %i\n",
7524 			    arsta->addr, arvif->vdev_id);
7525 		return;
7526 	}
7527 
7528 	spin_unlock_bh(&ar->ab->base_lock);
7529 
7530 	if (arsta->link_id >= IEEE80211_MLD_MAX_NUM_LINKS) {
7531 		rcu_read_unlock();
7532 		return;
7533 	}
7534 
7535 	link_sta = rcu_dereference(sta->link[arsta->link_id]);
7536 	if (!link_sta) {
7537 		rcu_read_unlock();
7538 		ath12k_warn(ar->ab, "unable to access link sta in rc update for sta %pM link %u\n",
7539 			    sta->addr, arsta->link_id);
7540 		return;
7541 	}
7542 
7543 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
7544 		   "mac sta rc update for %pM changed %08x bw %d nss %d smps %d\n",
7545 		   arsta->addr, changed, link_sta->bandwidth, link_sta->rx_nss,
7546 		   link_sta->smps_mode);
7547 
7548 	spin_lock_bh(&ar->data_lock);
7549 
7550 	if (changed & IEEE80211_RC_BW_CHANGED) {
7551 		bw = ath12k_mac_ieee80211_sta_bw_to_wmi(ar, link_sta);
7552 		arsta->bw_prev = arsta->bw;
7553 		arsta->bw = bw;
7554 	}
7555 
7556 	if (changed & IEEE80211_RC_NSS_CHANGED)
7557 		arsta->nss = link_sta->rx_nss;
7558 
7559 	if (changed & IEEE80211_RC_SMPS_CHANGED) {
7560 		smps = WMI_PEER_SMPS_PS_NONE;
7561 
7562 		switch (link_sta->smps_mode) {
7563 		case IEEE80211_SMPS_AUTOMATIC:
7564 		case IEEE80211_SMPS_OFF:
7565 			smps = WMI_PEER_SMPS_PS_NONE;
7566 			break;
7567 		case IEEE80211_SMPS_STATIC:
7568 			smps = WMI_PEER_SMPS_STATIC;
7569 			break;
7570 		case IEEE80211_SMPS_DYNAMIC:
7571 			smps = WMI_PEER_SMPS_DYNAMIC;
7572 			break;
7573 		default:
7574 			ath12k_warn(ar->ab, "Invalid smps %d in sta rc update for %pM link %u\n",
7575 				    link_sta->smps_mode, arsta->addr, link_sta->link_id);
7576 			smps = WMI_PEER_SMPS_PS_NONE;
7577 			break;
7578 		}
7579 
7580 		arsta->smps = smps;
7581 	}
7582 
7583 	arsta->changed |= changed;
7584 
7585 	spin_unlock_bh(&ar->data_lock);
7586 
7587 	wiphy_work_queue(hw->wiphy, &arsta->update_wk);
7588 
7589 	rcu_read_unlock();
7590 }
7591 
7592 static struct ath12k_link_sta *ath12k_mac_alloc_assign_link_sta(struct ath12k_hw *ah,
7593 								struct ath12k_sta *ahsta,
7594 								struct ath12k_vif *ahvif,
7595 								u8 link_id)
7596 {
7597 	struct ath12k_link_sta *arsta;
7598 	int ret;
7599 
7600 	lockdep_assert_wiphy(ah->hw->wiphy);
7601 
7602 	if (link_id >= IEEE80211_MLD_MAX_NUM_LINKS)
7603 		return NULL;
7604 
7605 	arsta = wiphy_dereference(ah->hw->wiphy, ahsta->link[link_id]);
7606 	if (arsta)
7607 		return NULL;
7608 
7609 	arsta = kmalloc(sizeof(*arsta), GFP_KERNEL);
7610 	if (!arsta)
7611 		return NULL;
7612 
7613 	ret = ath12k_mac_assign_link_sta(ah, ahsta, arsta, ahvif, link_id);
7614 	if (ret) {
7615 		kfree(arsta);
7616 		return NULL;
7617 	}
7618 
7619 	return arsta;
7620 }
7621 
7622 static int ath12k_mac_op_change_sta_links(struct ieee80211_hw *hw,
7623 					  struct ieee80211_vif *vif,
7624 					  struct ieee80211_sta *sta,
7625 					  u16 old_links, u16 new_links)
7626 {
7627 	struct ath12k_vif *ahvif = ath12k_vif_to_ahvif(vif);
7628 	struct ath12k_sta *ahsta = ath12k_sta_to_ahsta(sta);
7629 	struct ath12k_hw *ah = hw->priv;
7630 	struct ath12k_link_vif *arvif;
7631 	struct ath12k_link_sta *arsta;
7632 	unsigned long valid_links;
7633 	struct ath12k *ar;
7634 	u8 link_id;
7635 	int ret;
7636 
7637 	lockdep_assert_wiphy(hw->wiphy);
7638 
7639 	if (!sta->valid_links)
7640 		return -EINVAL;
7641 
7642 	/* Firmware does not support removal of one of link stas. All sta
7643 	 * would be removed during ML STA delete in sta_state(), hence link
7644 	 * sta removal is not handled here.
7645 	 */
7646 	if (new_links < old_links)
7647 		return 0;
7648 
7649 	if (ahsta->ml_peer_id == ATH12K_MLO_PEER_ID_INVALID) {
7650 		ath12k_hw_warn(ah, "unable to add link for ml sta %pM", sta->addr);
7651 		return -EINVAL;
7652 	}
7653 
7654 	/* this op is expected only after initial sta insertion with default link */
7655 	if (WARN_ON(ahsta->links_map == 0))
7656 		return -EINVAL;
7657 
7658 	valid_links = new_links;
7659 	for_each_set_bit(link_id, &valid_links, IEEE80211_MLD_MAX_NUM_LINKS) {
7660 		if (ahsta->links_map & BIT(link_id))
7661 			continue;
7662 
7663 		arvif = wiphy_dereference(hw->wiphy, ahvif->link[link_id]);
7664 		arsta = ath12k_mac_alloc_assign_link_sta(ah, ahsta, ahvif, link_id);
7665 
7666 		if (!arvif || !arsta) {
7667 			ath12k_hw_warn(ah, "Failed to alloc/assign link sta");
7668 			continue;
7669 		}
7670 
7671 		ar = arvif->ar;
7672 		if (!ar)
7673 			continue;
7674 
7675 		ret = ath12k_mac_station_add(ar, arvif, arsta);
7676 		if (ret) {
7677 			ath12k_warn(ar->ab, "Failed to add station: %pM for VDEV: %d\n",
7678 				    arsta->addr, arvif->vdev_id);
7679 			ath12k_mac_free_unassign_link_sta(ah, ahsta, link_id);
7680 			return ret;
7681 		}
7682 	}
7683 
7684 	return 0;
7685 }
7686 
7687 static bool ath12k_mac_op_can_activate_links(struct ieee80211_hw *hw,
7688 					     struct ieee80211_vif *vif,
7689 					     u16 active_links)
7690 {
7691 	/* TODO: Handle recovery case */
7692 
7693 	return true;
7694 }
7695 
7696 static int ath12k_conf_tx_uapsd(struct ath12k_link_vif *arvif,
7697 				u16 ac, bool enable)
7698 {
7699 	struct ath12k *ar = arvif->ar;
7700 	struct ath12k_vif *ahvif = arvif->ahvif;
7701 	u32 value;
7702 	int ret;
7703 
7704 	if (ahvif->vdev_type != WMI_VDEV_TYPE_STA)
7705 		return 0;
7706 
7707 	switch (ac) {
7708 	case IEEE80211_AC_VO:
7709 		value = WMI_STA_PS_UAPSD_AC3_DELIVERY_EN |
7710 			WMI_STA_PS_UAPSD_AC3_TRIGGER_EN;
7711 		break;
7712 	case IEEE80211_AC_VI:
7713 		value = WMI_STA_PS_UAPSD_AC2_DELIVERY_EN |
7714 			WMI_STA_PS_UAPSD_AC2_TRIGGER_EN;
7715 		break;
7716 	case IEEE80211_AC_BE:
7717 		value = WMI_STA_PS_UAPSD_AC1_DELIVERY_EN |
7718 			WMI_STA_PS_UAPSD_AC1_TRIGGER_EN;
7719 		break;
7720 	case IEEE80211_AC_BK:
7721 		value = WMI_STA_PS_UAPSD_AC0_DELIVERY_EN |
7722 			WMI_STA_PS_UAPSD_AC0_TRIGGER_EN;
7723 		break;
7724 	}
7725 
7726 	if (enable)
7727 		ahvif->u.sta.uapsd |= value;
7728 	else
7729 		ahvif->u.sta.uapsd &= ~value;
7730 
7731 	ret = ath12k_wmi_set_sta_ps_param(ar, arvif->vdev_id,
7732 					  WMI_STA_PS_PARAM_UAPSD,
7733 					  ahvif->u.sta.uapsd);
7734 	if (ret) {
7735 		ath12k_warn(ar->ab, "could not set uapsd params %d\n", ret);
7736 		goto exit;
7737 	}
7738 
7739 	if (ahvif->u.sta.uapsd)
7740 		value = WMI_STA_PS_RX_WAKE_POLICY_POLL_UAPSD;
7741 	else
7742 		value = WMI_STA_PS_RX_WAKE_POLICY_WAKE;
7743 
7744 	ret = ath12k_wmi_set_sta_ps_param(ar, arvif->vdev_id,
7745 					  WMI_STA_PS_PARAM_RX_WAKE_POLICY,
7746 					  value);
7747 	if (ret)
7748 		ath12k_warn(ar->ab, "could not set rx wake param %d\n", ret);
7749 
7750 exit:
7751 	return ret;
7752 }
7753 
7754 static int ath12k_mac_conf_tx(struct ath12k_link_vif *arvif, u16 ac,
7755 			      const struct ieee80211_tx_queue_params *params)
7756 {
7757 	struct wmi_wmm_params_arg *p = NULL;
7758 	struct ath12k *ar = arvif->ar;
7759 	struct ath12k_base *ab = ar->ab;
7760 	int ret;
7761 
7762 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
7763 
7764 	switch (ac) {
7765 	case IEEE80211_AC_VO:
7766 		p = &arvif->wmm_params.ac_vo;
7767 		break;
7768 	case IEEE80211_AC_VI:
7769 		p = &arvif->wmm_params.ac_vi;
7770 		break;
7771 	case IEEE80211_AC_BE:
7772 		p = &arvif->wmm_params.ac_be;
7773 		break;
7774 	case IEEE80211_AC_BK:
7775 		p = &arvif->wmm_params.ac_bk;
7776 		break;
7777 	}
7778 
7779 	if (WARN_ON(!p)) {
7780 		ret = -EINVAL;
7781 		goto exit;
7782 	}
7783 
7784 	p->cwmin = params->cw_min;
7785 	p->cwmax = params->cw_max;
7786 	p->aifs = params->aifs;
7787 	p->txop = params->txop;
7788 
7789 	ret = ath12k_wmi_send_wmm_update_cmd(ar, arvif->vdev_id,
7790 					     &arvif->wmm_params);
7791 	if (ret) {
7792 		ath12k_warn(ab, "pdev idx %d failed to set wmm params: %d\n",
7793 			    ar->pdev_idx, ret);
7794 		goto exit;
7795 	}
7796 
7797 	ret = ath12k_conf_tx_uapsd(arvif, ac, params->uapsd);
7798 	if (ret)
7799 		ath12k_warn(ab, "pdev idx %d failed to set sta uapsd: %d\n",
7800 			    ar->pdev_idx, ret);
7801 
7802 exit:
7803 	return ret;
7804 }
7805 
7806 static int ath12k_mac_op_conf_tx(struct ieee80211_hw *hw,
7807 				 struct ieee80211_vif *vif,
7808 				 unsigned int link_id, u16 ac,
7809 				 const struct ieee80211_tx_queue_params *params)
7810 {
7811 	struct ath12k_vif *ahvif = ath12k_vif_to_ahvif(vif);
7812 	struct ath12k_link_vif *arvif;
7813 	struct ath12k_vif_cache *cache;
7814 	int ret;
7815 
7816 	lockdep_assert_wiphy(hw->wiphy);
7817 
7818 	if (link_id >= IEEE80211_MLD_MAX_NUM_LINKS)
7819 		return -EINVAL;
7820 
7821 	arvif = wiphy_dereference(hw->wiphy, ahvif->link[link_id]);
7822 	if (!arvif || !arvif->is_created) {
7823 		cache = ath12k_ahvif_get_link_cache(ahvif, link_id);
7824 		if (!cache)
7825 			return -ENOSPC;
7826 
7827 		cache->tx_conf.changed = true;
7828 		cache->tx_conf.ac = ac;
7829 		cache->tx_conf.tx_queue_params = *params;
7830 
7831 		return 0;
7832 	}
7833 
7834 	ret = ath12k_mac_conf_tx(arvif, ac, params);
7835 
7836 	return ret;
7837 }
7838 
7839 static struct ieee80211_sta_ht_cap
7840 ath12k_create_ht_cap(struct ath12k *ar, u32 ar_ht_cap, u32 rate_cap_rx_chainmask)
7841 {
7842 	int i;
7843 	struct ieee80211_sta_ht_cap ht_cap = {};
7844 	u32 ar_vht_cap = ar->pdev->cap.vht_cap;
7845 
7846 	if (!(ar_ht_cap & WMI_HT_CAP_ENABLED))
7847 		return ht_cap;
7848 
7849 	ht_cap.ht_supported = 1;
7850 	ht_cap.ampdu_factor = IEEE80211_HT_MAX_AMPDU_64K;
7851 	ht_cap.ampdu_density = IEEE80211_HT_MPDU_DENSITY_NONE;
7852 	ht_cap.cap |= IEEE80211_HT_CAP_SUP_WIDTH_20_40;
7853 	ht_cap.cap |= IEEE80211_HT_CAP_DSSSCCK40;
7854 	ht_cap.cap |= WLAN_HT_CAP_SM_PS_STATIC << IEEE80211_HT_CAP_SM_PS_SHIFT;
7855 
7856 	if (ar_ht_cap & WMI_HT_CAP_HT20_SGI)
7857 		ht_cap.cap |= IEEE80211_HT_CAP_SGI_20;
7858 
7859 	if (ar_ht_cap & WMI_HT_CAP_HT40_SGI)
7860 		ht_cap.cap |= IEEE80211_HT_CAP_SGI_40;
7861 
7862 	if (ar_ht_cap & WMI_HT_CAP_DYNAMIC_SMPS) {
7863 		u32 smps;
7864 
7865 		smps   = WLAN_HT_CAP_SM_PS_DYNAMIC;
7866 		smps <<= IEEE80211_HT_CAP_SM_PS_SHIFT;
7867 
7868 		ht_cap.cap |= smps;
7869 	}
7870 
7871 	if (ar_ht_cap & WMI_HT_CAP_TX_STBC)
7872 		ht_cap.cap |= IEEE80211_HT_CAP_TX_STBC;
7873 
7874 	if (ar_ht_cap & WMI_HT_CAP_RX_STBC) {
7875 		u32 stbc;
7876 
7877 		stbc   = ar_ht_cap;
7878 		stbc  &= WMI_HT_CAP_RX_STBC;
7879 		stbc >>= WMI_HT_CAP_RX_STBC_MASK_SHIFT;
7880 		stbc <<= IEEE80211_HT_CAP_RX_STBC_SHIFT;
7881 		stbc  &= IEEE80211_HT_CAP_RX_STBC;
7882 
7883 		ht_cap.cap |= stbc;
7884 	}
7885 
7886 	if (ar_ht_cap & WMI_HT_CAP_RX_LDPC)
7887 		ht_cap.cap |= IEEE80211_HT_CAP_LDPC_CODING;
7888 
7889 	if (ar_ht_cap & WMI_HT_CAP_L_SIG_TXOP_PROT)
7890 		ht_cap.cap |= IEEE80211_HT_CAP_LSIG_TXOP_PROT;
7891 
7892 	if (ar_vht_cap & WMI_VHT_CAP_MAX_MPDU_LEN_MASK)
7893 		ht_cap.cap |= IEEE80211_HT_CAP_MAX_AMSDU;
7894 
7895 	for (i = 0; i < ar->num_rx_chains; i++) {
7896 		if (rate_cap_rx_chainmask & BIT(i))
7897 			ht_cap.mcs.rx_mask[i] = 0xFF;
7898 	}
7899 
7900 	ht_cap.mcs.tx_params |= IEEE80211_HT_MCS_TX_DEFINED;
7901 
7902 	return ht_cap;
7903 }
7904 
7905 static int ath12k_mac_set_txbf_conf(struct ath12k_link_vif *arvif)
7906 {
7907 	u32 value = 0;
7908 	struct ath12k *ar = arvif->ar;
7909 	struct ath12k_vif *ahvif = arvif->ahvif;
7910 	int nsts;
7911 	int sound_dim;
7912 	u32 vht_cap = ar->pdev->cap.vht_cap;
7913 	u32 vdev_param = WMI_VDEV_PARAM_TXBF;
7914 
7915 	if (vht_cap & (IEEE80211_VHT_CAP_SU_BEAMFORMEE_CAPABLE)) {
7916 		nsts = vht_cap & IEEE80211_VHT_CAP_BEAMFORMEE_STS_MASK;
7917 		nsts >>= IEEE80211_VHT_CAP_BEAMFORMEE_STS_SHIFT;
7918 		value |= SM(nsts, WMI_TXBF_STS_CAP_OFFSET);
7919 	}
7920 
7921 	if (vht_cap & (IEEE80211_VHT_CAP_SU_BEAMFORMER_CAPABLE)) {
7922 		sound_dim = vht_cap &
7923 			    IEEE80211_VHT_CAP_SOUNDING_DIMENSIONS_MASK;
7924 		sound_dim >>= IEEE80211_VHT_CAP_SOUNDING_DIMENSIONS_SHIFT;
7925 		if (sound_dim > (ar->num_tx_chains - 1))
7926 			sound_dim = ar->num_tx_chains - 1;
7927 		value |= SM(sound_dim, WMI_BF_SOUND_DIM_OFFSET);
7928 	}
7929 
7930 	if (!value)
7931 		return 0;
7932 
7933 	if (vht_cap & IEEE80211_VHT_CAP_SU_BEAMFORMER_CAPABLE) {
7934 		value |= WMI_VDEV_PARAM_TXBF_SU_TX_BFER;
7935 
7936 		if ((vht_cap & IEEE80211_VHT_CAP_MU_BEAMFORMER_CAPABLE) &&
7937 		    ahvif->vdev_type == WMI_VDEV_TYPE_AP)
7938 			value |= WMI_VDEV_PARAM_TXBF_MU_TX_BFER;
7939 	}
7940 
7941 	if (vht_cap & IEEE80211_VHT_CAP_SU_BEAMFORMEE_CAPABLE) {
7942 		value |= WMI_VDEV_PARAM_TXBF_SU_TX_BFEE;
7943 
7944 		if ((vht_cap & IEEE80211_VHT_CAP_MU_BEAMFORMEE_CAPABLE) &&
7945 		    ahvif->vdev_type == WMI_VDEV_TYPE_STA)
7946 			value |= WMI_VDEV_PARAM_TXBF_MU_TX_BFEE;
7947 	}
7948 
7949 	return ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
7950 					     vdev_param, value);
7951 }
7952 
7953 static void ath12k_set_vht_txbf_cap(struct ath12k *ar, u32 *vht_cap)
7954 {
7955 	bool subfer, subfee;
7956 	int sound_dim = 0;
7957 
7958 	subfer = !!(*vht_cap & (IEEE80211_VHT_CAP_SU_BEAMFORMER_CAPABLE));
7959 	subfee = !!(*vht_cap & (IEEE80211_VHT_CAP_SU_BEAMFORMEE_CAPABLE));
7960 
7961 	if (ar->num_tx_chains < 2) {
7962 		*vht_cap &= ~(IEEE80211_VHT_CAP_SU_BEAMFORMER_CAPABLE);
7963 		subfer = false;
7964 	}
7965 
7966 	/* If SU Beaformer is not set, then disable MU Beamformer Capability */
7967 	if (!subfer)
7968 		*vht_cap &= ~(IEEE80211_VHT_CAP_MU_BEAMFORMER_CAPABLE);
7969 
7970 	/* If SU Beaformee is not set, then disable MU Beamformee Capability */
7971 	if (!subfee)
7972 		*vht_cap &= ~(IEEE80211_VHT_CAP_MU_BEAMFORMEE_CAPABLE);
7973 
7974 	sound_dim = u32_get_bits(*vht_cap,
7975 				 IEEE80211_VHT_CAP_SOUNDING_DIMENSIONS_MASK);
7976 	*vht_cap = u32_replace_bits(*vht_cap, 0,
7977 				    IEEE80211_VHT_CAP_SOUNDING_DIMENSIONS_MASK);
7978 
7979 	/* TODO: Need to check invalid STS and Sound_dim values set by FW? */
7980 
7981 	/* Enable Sounding Dimension Field only if SU BF is enabled */
7982 	if (subfer) {
7983 		if (sound_dim > (ar->num_tx_chains - 1))
7984 			sound_dim = ar->num_tx_chains - 1;
7985 
7986 		*vht_cap = u32_replace_bits(*vht_cap, sound_dim,
7987 					    IEEE80211_VHT_CAP_SOUNDING_DIMENSIONS_MASK);
7988 	}
7989 
7990 	/* Use the STS advertised by FW unless SU Beamformee is not supported*/
7991 	if (!subfee)
7992 		*vht_cap &= ~(IEEE80211_VHT_CAP_BEAMFORMEE_STS_MASK);
7993 }
7994 
7995 static struct ieee80211_sta_vht_cap
7996 ath12k_create_vht_cap(struct ath12k *ar, u32 rate_cap_tx_chainmask,
7997 		      u32 rate_cap_rx_chainmask)
7998 {
7999 	struct ieee80211_sta_vht_cap vht_cap = {};
8000 	u16 txmcs_map, rxmcs_map;
8001 	int i;
8002 
8003 	vht_cap.vht_supported = 1;
8004 	vht_cap.cap = ar->pdev->cap.vht_cap;
8005 
8006 	ath12k_set_vht_txbf_cap(ar, &vht_cap.cap);
8007 
8008 	/* 80P80 is not supported */
8009 	vht_cap.cap &= ~IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_160_80PLUS80MHZ;
8010 
8011 	rxmcs_map = 0;
8012 	txmcs_map = 0;
8013 	for (i = 0; i < 8; i++) {
8014 		if (i < ar->num_tx_chains && rate_cap_tx_chainmask & BIT(i))
8015 			txmcs_map |= IEEE80211_VHT_MCS_SUPPORT_0_9 << (i * 2);
8016 		else
8017 			txmcs_map |= IEEE80211_VHT_MCS_NOT_SUPPORTED << (i * 2);
8018 
8019 		if (i < ar->num_rx_chains && rate_cap_rx_chainmask & BIT(i))
8020 			rxmcs_map |= IEEE80211_VHT_MCS_SUPPORT_0_9 << (i * 2);
8021 		else
8022 			rxmcs_map |= IEEE80211_VHT_MCS_NOT_SUPPORTED << (i * 2);
8023 	}
8024 
8025 	if (rate_cap_tx_chainmask <= 1)
8026 		vht_cap.cap &= ~IEEE80211_VHT_CAP_TXSTBC;
8027 
8028 	vht_cap.vht_mcs.rx_mcs_map = cpu_to_le16(rxmcs_map);
8029 	vht_cap.vht_mcs.tx_mcs_map = cpu_to_le16(txmcs_map);
8030 
8031 	/* Check if the HW supports 1:1 NSS ratio and reset
8032 	 * EXT NSS BW Support field to 0 to indicate 1:1 ratio
8033 	 */
8034 	if (ar->pdev->cap.nss_ratio_info == WMI_NSS_RATIO_1_NSS)
8035 		vht_cap.cap &= ~IEEE80211_VHT_CAP_EXT_NSS_BW_MASK;
8036 
8037 	return vht_cap;
8038 }
8039 
8040 static void ath12k_mac_setup_ht_vht_cap(struct ath12k *ar,
8041 					struct ath12k_pdev_cap *cap,
8042 					u32 *ht_cap_info)
8043 {
8044 	struct ieee80211_supported_band *band;
8045 	u32 rate_cap_tx_chainmask;
8046 	u32 rate_cap_rx_chainmask;
8047 	u32 ht_cap;
8048 
8049 	rate_cap_tx_chainmask = ar->cfg_tx_chainmask >> cap->tx_chain_mask_shift;
8050 	rate_cap_rx_chainmask = ar->cfg_rx_chainmask >> cap->rx_chain_mask_shift;
8051 
8052 	if (cap->supported_bands & WMI_HOST_WLAN_2GHZ_CAP) {
8053 		band = &ar->mac.sbands[NL80211_BAND_2GHZ];
8054 		ht_cap = cap->band[NL80211_BAND_2GHZ].ht_cap_info;
8055 		if (ht_cap_info)
8056 			*ht_cap_info = ht_cap;
8057 		band->ht_cap = ath12k_create_ht_cap(ar, ht_cap,
8058 						    rate_cap_rx_chainmask);
8059 	}
8060 
8061 	if (cap->supported_bands & WMI_HOST_WLAN_5GHZ_CAP &&
8062 	    (ar->ab->hw_params->single_pdev_only ||
8063 	     !ar->supports_6ghz)) {
8064 		band = &ar->mac.sbands[NL80211_BAND_5GHZ];
8065 		ht_cap = cap->band[NL80211_BAND_5GHZ].ht_cap_info;
8066 		if (ht_cap_info)
8067 			*ht_cap_info = ht_cap;
8068 		band->ht_cap = ath12k_create_ht_cap(ar, ht_cap,
8069 						    rate_cap_rx_chainmask);
8070 		band->vht_cap = ath12k_create_vht_cap(ar, rate_cap_tx_chainmask,
8071 						      rate_cap_rx_chainmask);
8072 	}
8073 }
8074 
8075 static int ath12k_check_chain_mask(struct ath12k *ar, u32 ant, bool is_tx_ant)
8076 {
8077 	/* TODO: Check the request chainmask against the supported
8078 	 * chainmask table which is advertised in extented_service_ready event
8079 	 */
8080 
8081 	return 0;
8082 }
8083 
8084 static void ath12k_gen_ppe_thresh(struct ath12k_wmi_ppe_threshold_arg *fw_ppet,
8085 				  u8 *he_ppet)
8086 {
8087 	int nss, ru;
8088 	u8 bit = 7;
8089 
8090 	he_ppet[0] = fw_ppet->numss_m1 & IEEE80211_PPE_THRES_NSS_MASK;
8091 	he_ppet[0] |= (fw_ppet->ru_bit_mask <<
8092 		       IEEE80211_PPE_THRES_RU_INDEX_BITMASK_POS) &
8093 		      IEEE80211_PPE_THRES_RU_INDEX_BITMASK_MASK;
8094 	for (nss = 0; nss <= fw_ppet->numss_m1; nss++) {
8095 		for (ru = 0; ru < 4; ru++) {
8096 			u8 val;
8097 			int i;
8098 
8099 			if ((fw_ppet->ru_bit_mask & BIT(ru)) == 0)
8100 				continue;
8101 			val = (fw_ppet->ppet16_ppet8_ru3_ru0[nss] >> (ru * 6)) &
8102 			       0x3f;
8103 			val = ((val >> 3) & 0x7) | ((val & 0x7) << 3);
8104 			for (i = 5; i >= 0; i--) {
8105 				he_ppet[bit / 8] |=
8106 					((val >> i) & 0x1) << ((bit % 8));
8107 				bit++;
8108 			}
8109 		}
8110 	}
8111 }
8112 
8113 static void
8114 ath12k_mac_filter_he_cap_mesh(struct ieee80211_he_cap_elem *he_cap_elem)
8115 {
8116 	u8 m;
8117 
8118 	m = IEEE80211_HE_MAC_CAP0_TWT_RES |
8119 	    IEEE80211_HE_MAC_CAP0_TWT_REQ;
8120 	he_cap_elem->mac_cap_info[0] &= ~m;
8121 
8122 	m = IEEE80211_HE_MAC_CAP2_TRS |
8123 	    IEEE80211_HE_MAC_CAP2_BCAST_TWT |
8124 	    IEEE80211_HE_MAC_CAP2_MU_CASCADING;
8125 	he_cap_elem->mac_cap_info[2] &= ~m;
8126 
8127 	m = IEEE80211_HE_MAC_CAP3_FLEX_TWT_SCHED |
8128 	    IEEE80211_HE_MAC_CAP2_BCAST_TWT |
8129 	    IEEE80211_HE_MAC_CAP2_MU_CASCADING;
8130 	he_cap_elem->mac_cap_info[3] &= ~m;
8131 
8132 	m = IEEE80211_HE_MAC_CAP4_BSRP_BQRP_A_MPDU_AGG |
8133 	    IEEE80211_HE_MAC_CAP4_BQR;
8134 	he_cap_elem->mac_cap_info[4] &= ~m;
8135 
8136 	m = IEEE80211_HE_MAC_CAP5_SUBCHAN_SELECTIVE_TRANSMISSION |
8137 	    IEEE80211_HE_MAC_CAP5_UL_2x996_TONE_RU |
8138 	    IEEE80211_HE_MAC_CAP5_PUNCTURED_SOUNDING |
8139 	    IEEE80211_HE_MAC_CAP5_HT_VHT_TRIG_FRAME_RX;
8140 	he_cap_elem->mac_cap_info[5] &= ~m;
8141 
8142 	m = IEEE80211_HE_PHY_CAP2_UL_MU_FULL_MU_MIMO |
8143 	    IEEE80211_HE_PHY_CAP2_UL_MU_PARTIAL_MU_MIMO;
8144 	he_cap_elem->phy_cap_info[2] &= ~m;
8145 
8146 	m = IEEE80211_HE_PHY_CAP3_RX_PARTIAL_BW_SU_IN_20MHZ_MU |
8147 	    IEEE80211_HE_PHY_CAP3_DCM_MAX_CONST_TX_MASK |
8148 	    IEEE80211_HE_PHY_CAP3_DCM_MAX_CONST_RX_MASK;
8149 	he_cap_elem->phy_cap_info[3] &= ~m;
8150 
8151 	m = IEEE80211_HE_PHY_CAP4_MU_BEAMFORMER;
8152 	he_cap_elem->phy_cap_info[4] &= ~m;
8153 
8154 	m = IEEE80211_HE_PHY_CAP5_NG16_MU_FEEDBACK;
8155 	he_cap_elem->phy_cap_info[5] &= ~m;
8156 
8157 	m = IEEE80211_HE_PHY_CAP6_CODEBOOK_SIZE_75_MU |
8158 	    IEEE80211_HE_PHY_CAP6_TRIG_MU_BEAMFORMING_PARTIAL_BW_FB |
8159 	    IEEE80211_HE_PHY_CAP6_TRIG_CQI_FB |
8160 	    IEEE80211_HE_PHY_CAP6_PARTIAL_BANDWIDTH_DL_MUMIMO;
8161 	he_cap_elem->phy_cap_info[6] &= ~m;
8162 
8163 	m = IEEE80211_HE_PHY_CAP7_PSR_BASED_SR |
8164 	    IEEE80211_HE_PHY_CAP7_POWER_BOOST_FACTOR_SUPP |
8165 	    IEEE80211_HE_PHY_CAP7_STBC_TX_ABOVE_80MHZ |
8166 	    IEEE80211_HE_PHY_CAP7_STBC_RX_ABOVE_80MHZ;
8167 	he_cap_elem->phy_cap_info[7] &= ~m;
8168 
8169 	m = IEEE80211_HE_PHY_CAP8_HE_ER_SU_PPDU_4XLTF_AND_08_US_GI |
8170 	    IEEE80211_HE_PHY_CAP8_20MHZ_IN_40MHZ_HE_PPDU_IN_2G |
8171 	    IEEE80211_HE_PHY_CAP8_20MHZ_IN_160MHZ_HE_PPDU |
8172 	    IEEE80211_HE_PHY_CAP8_80MHZ_IN_160MHZ_HE_PPDU;
8173 	he_cap_elem->phy_cap_info[8] &= ~m;
8174 
8175 	m = IEEE80211_HE_PHY_CAP9_LONGER_THAN_16_SIGB_OFDM_SYM |
8176 	    IEEE80211_HE_PHY_CAP9_NON_TRIGGERED_CQI_FEEDBACK |
8177 	    IEEE80211_HE_PHY_CAP9_RX_1024_QAM_LESS_THAN_242_TONE_RU |
8178 	    IEEE80211_HE_PHY_CAP9_TX_1024_QAM_LESS_THAN_242_TONE_RU |
8179 	    IEEE80211_HE_PHY_CAP9_RX_FULL_BW_SU_USING_MU_WITH_COMP_SIGB |
8180 	    IEEE80211_HE_PHY_CAP9_RX_FULL_BW_SU_USING_MU_WITH_NON_COMP_SIGB;
8181 	he_cap_elem->phy_cap_info[9] &= ~m;
8182 }
8183 
8184 static __le16 ath12k_mac_setup_he_6ghz_cap(struct ath12k_pdev_cap *pcap,
8185 					   struct ath12k_band_cap *bcap)
8186 {
8187 	u8 val;
8188 
8189 	bcap->he_6ghz_capa = IEEE80211_HT_MPDU_DENSITY_NONE;
8190 	if (bcap->ht_cap_info & WMI_HT_CAP_DYNAMIC_SMPS)
8191 		bcap->he_6ghz_capa |=
8192 			u32_encode_bits(WLAN_HT_CAP_SM_PS_DYNAMIC,
8193 					IEEE80211_HE_6GHZ_CAP_SM_PS);
8194 	else
8195 		bcap->he_6ghz_capa |=
8196 			u32_encode_bits(WLAN_HT_CAP_SM_PS_DISABLED,
8197 					IEEE80211_HE_6GHZ_CAP_SM_PS);
8198 	val = u32_get_bits(pcap->vht_cap,
8199 			   IEEE80211_VHT_CAP_MAX_A_MPDU_LENGTH_EXPONENT_MASK);
8200 	bcap->he_6ghz_capa |=
8201 		u32_encode_bits(val, IEEE80211_HE_6GHZ_CAP_MAX_AMPDU_LEN_EXP);
8202 	val = u32_get_bits(pcap->vht_cap,
8203 			   IEEE80211_VHT_CAP_MAX_MPDU_MASK);
8204 	bcap->he_6ghz_capa |=
8205 		u32_encode_bits(val, IEEE80211_HE_6GHZ_CAP_MAX_MPDU_LEN);
8206 	if (pcap->vht_cap & IEEE80211_VHT_CAP_RX_ANTENNA_PATTERN)
8207 		bcap->he_6ghz_capa |= IEEE80211_HE_6GHZ_CAP_RX_ANTPAT_CONS;
8208 	if (pcap->vht_cap & IEEE80211_VHT_CAP_TX_ANTENNA_PATTERN)
8209 		bcap->he_6ghz_capa |= IEEE80211_HE_6GHZ_CAP_TX_ANTPAT_CONS;
8210 
8211 	return cpu_to_le16(bcap->he_6ghz_capa);
8212 }
8213 
8214 static void ath12k_mac_set_hemcsmap(struct ath12k *ar,
8215 				    struct ath12k_pdev_cap *cap,
8216 				    struct ieee80211_sta_he_cap *he_cap)
8217 {
8218 	struct ieee80211_he_mcs_nss_supp *mcs_nss = &he_cap->he_mcs_nss_supp;
8219 	u8 maxtxnss_160 = ath12k_get_nss_160mhz(ar, ar->num_tx_chains);
8220 	u8 maxrxnss_160 = ath12k_get_nss_160mhz(ar, ar->num_rx_chains);
8221 	u16 txmcs_map_160 = 0, rxmcs_map_160 = 0;
8222 	u16 txmcs_map = 0, rxmcs_map = 0;
8223 	u32 i;
8224 
8225 	for (i = 0; i < 8; i++) {
8226 		if (i < ar->num_tx_chains &&
8227 		    (ar->cfg_tx_chainmask >> cap->tx_chain_mask_shift) & BIT(i))
8228 			txmcs_map |= IEEE80211_HE_MCS_SUPPORT_0_11 << (i * 2);
8229 		else
8230 			txmcs_map |= IEEE80211_HE_MCS_NOT_SUPPORTED << (i * 2);
8231 
8232 		if (i < ar->num_rx_chains &&
8233 		    (ar->cfg_rx_chainmask >> cap->tx_chain_mask_shift) & BIT(i))
8234 			rxmcs_map |= IEEE80211_HE_MCS_SUPPORT_0_11 << (i * 2);
8235 		else
8236 			rxmcs_map |= IEEE80211_HE_MCS_NOT_SUPPORTED << (i * 2);
8237 
8238 		if (i < maxtxnss_160 &&
8239 		    (ar->cfg_tx_chainmask >> cap->tx_chain_mask_shift) & BIT(i))
8240 			txmcs_map_160 |= IEEE80211_HE_MCS_SUPPORT_0_11 << (i * 2);
8241 		else
8242 			txmcs_map_160 |= IEEE80211_HE_MCS_NOT_SUPPORTED << (i * 2);
8243 
8244 		if (i < maxrxnss_160 &&
8245 		    (ar->cfg_tx_chainmask >> cap->tx_chain_mask_shift) & BIT(i))
8246 			rxmcs_map_160 |= IEEE80211_HE_MCS_SUPPORT_0_11 << (i * 2);
8247 		else
8248 			rxmcs_map_160 |= IEEE80211_HE_MCS_NOT_SUPPORTED << (i * 2);
8249 	}
8250 
8251 	mcs_nss->rx_mcs_80 = cpu_to_le16(rxmcs_map & 0xffff);
8252 	mcs_nss->tx_mcs_80 = cpu_to_le16(txmcs_map & 0xffff);
8253 	mcs_nss->rx_mcs_160 = cpu_to_le16(rxmcs_map_160 & 0xffff);
8254 	mcs_nss->tx_mcs_160 = cpu_to_le16(txmcs_map_160 & 0xffff);
8255 }
8256 
8257 static void ath12k_mac_copy_he_cap(struct ath12k *ar,
8258 				   struct ath12k_band_cap *band_cap,
8259 				   int iftype, u8 num_tx_chains,
8260 				   struct ieee80211_sta_he_cap *he_cap)
8261 {
8262 	struct ieee80211_he_cap_elem *he_cap_elem = &he_cap->he_cap_elem;
8263 
8264 	he_cap->has_he = true;
8265 	memcpy(he_cap_elem->mac_cap_info, band_cap->he_cap_info,
8266 	       sizeof(he_cap_elem->mac_cap_info));
8267 	memcpy(he_cap_elem->phy_cap_info, band_cap->he_cap_phy_info,
8268 	       sizeof(he_cap_elem->phy_cap_info));
8269 
8270 	he_cap_elem->mac_cap_info[1] &=
8271 		IEEE80211_HE_MAC_CAP1_TF_MAC_PAD_DUR_MASK;
8272 	he_cap_elem->phy_cap_info[0] &=
8273 		IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_40MHZ_IN_2G |
8274 		IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_40MHZ_80MHZ_IN_5G |
8275 		IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_160MHZ_IN_5G;
8276 	/* 80PLUS80 is not supported */
8277 	he_cap_elem->phy_cap_info[0] &=
8278 		~IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_80PLUS80_MHZ_IN_5G;
8279 	he_cap_elem->phy_cap_info[5] &=
8280 		~IEEE80211_HE_PHY_CAP5_BEAMFORMEE_NUM_SND_DIM_UNDER_80MHZ_MASK;
8281 	he_cap_elem->phy_cap_info[5] |= num_tx_chains - 1;
8282 
8283 	switch (iftype) {
8284 	case NL80211_IFTYPE_AP:
8285 		he_cap_elem->mac_cap_info[2] &=
8286 			~IEEE80211_HE_MAC_CAP2_BCAST_TWT;
8287 		he_cap_elem->phy_cap_info[3] &=
8288 			~IEEE80211_HE_PHY_CAP3_DCM_MAX_CONST_TX_MASK;
8289 		he_cap_elem->phy_cap_info[9] |=
8290 			IEEE80211_HE_PHY_CAP9_RX_1024_QAM_LESS_THAN_242_TONE_RU;
8291 		break;
8292 	case NL80211_IFTYPE_STATION:
8293 		he_cap_elem->mac_cap_info[0] &= ~IEEE80211_HE_MAC_CAP0_TWT_RES;
8294 		he_cap_elem->mac_cap_info[0] |= IEEE80211_HE_MAC_CAP0_TWT_REQ;
8295 		he_cap_elem->phy_cap_info[9] |=
8296 			IEEE80211_HE_PHY_CAP9_TX_1024_QAM_LESS_THAN_242_TONE_RU;
8297 		break;
8298 	case NL80211_IFTYPE_MESH_POINT:
8299 		ath12k_mac_filter_he_cap_mesh(he_cap_elem);
8300 		break;
8301 	}
8302 
8303 	ath12k_mac_set_hemcsmap(ar, &ar->pdev->cap, he_cap);
8304 	memset(he_cap->ppe_thres, 0, sizeof(he_cap->ppe_thres));
8305 	if (he_cap_elem->phy_cap_info[6] &
8306 	    IEEE80211_HE_PHY_CAP6_PPE_THRESHOLD_PRESENT)
8307 		ath12k_gen_ppe_thresh(&band_cap->he_ppet, he_cap->ppe_thres);
8308 }
8309 
8310 static void
8311 ath12k_mac_copy_eht_mcs_nss(struct ath12k_band_cap *band_cap,
8312 			    struct ieee80211_eht_mcs_nss_supp *mcs_nss,
8313 			    const struct ieee80211_he_cap_elem *he_cap,
8314 			    const struct ieee80211_eht_cap_elem_fixed *eht_cap)
8315 {
8316 	if ((he_cap->phy_cap_info[0] &
8317 	     (IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_40MHZ_IN_2G |
8318 	      IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_40MHZ_80MHZ_IN_5G |
8319 	      IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_160MHZ_IN_5G |
8320 	      IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_80PLUS80_MHZ_IN_5G)) == 0)
8321 		memcpy(&mcs_nss->only_20mhz, &band_cap->eht_mcs_20_only,
8322 		       sizeof(struct ieee80211_eht_mcs_nss_supp_20mhz_only));
8323 
8324 	if (he_cap->phy_cap_info[0] &
8325 	    (IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_40MHZ_IN_2G |
8326 	     IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_40MHZ_80MHZ_IN_5G))
8327 		memcpy(&mcs_nss->bw._80, &band_cap->eht_mcs_80,
8328 		       sizeof(struct ieee80211_eht_mcs_nss_supp_bw));
8329 
8330 	if (he_cap->phy_cap_info[0] &
8331 	    IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_160MHZ_IN_5G)
8332 		memcpy(&mcs_nss->bw._160, &band_cap->eht_mcs_160,
8333 		       sizeof(struct ieee80211_eht_mcs_nss_supp_bw));
8334 
8335 	if (eht_cap->phy_cap_info[0] & IEEE80211_EHT_PHY_CAP0_320MHZ_IN_6GHZ)
8336 		memcpy(&mcs_nss->bw._320, &band_cap->eht_mcs_320,
8337 		       sizeof(struct ieee80211_eht_mcs_nss_supp_bw));
8338 }
8339 
8340 static void ath12k_mac_copy_eht_ppe_thresh(struct ath12k_wmi_ppe_threshold_arg *fw_ppet,
8341 					   struct ieee80211_sta_eht_cap *cap)
8342 {
8343 	u16 bit = IEEE80211_EHT_PPE_THRES_INFO_HEADER_SIZE;
8344 	u8 i, nss, ru, ppet_bit_len_per_ru = IEEE80211_EHT_PPE_THRES_INFO_PPET_SIZE * 2;
8345 
8346 	u8p_replace_bits(&cap->eht_ppe_thres[0], fw_ppet->numss_m1,
8347 			 IEEE80211_EHT_PPE_THRES_NSS_MASK);
8348 
8349 	u16p_replace_bits((u16 *)&cap->eht_ppe_thres[0], fw_ppet->ru_bit_mask,
8350 			  IEEE80211_EHT_PPE_THRES_RU_INDEX_BITMASK_MASK);
8351 
8352 	for (nss = 0; nss <= fw_ppet->numss_m1; nss++) {
8353 		for (ru = 0;
8354 		     ru < hweight16(IEEE80211_EHT_PPE_THRES_RU_INDEX_BITMASK_MASK);
8355 		     ru++) {
8356 			u32 val = 0;
8357 
8358 			if ((fw_ppet->ru_bit_mask & BIT(ru)) == 0)
8359 				continue;
8360 
8361 			u32p_replace_bits(&val, fw_ppet->ppet16_ppet8_ru3_ru0[nss] >>
8362 						(ru * ppet_bit_len_per_ru),
8363 					  GENMASK(ppet_bit_len_per_ru - 1, 0));
8364 
8365 			for (i = 0; i < ppet_bit_len_per_ru; i++) {
8366 				cap->eht_ppe_thres[bit / 8] |=
8367 					(((val >> i) & 0x1) << ((bit % 8)));
8368 				bit++;
8369 			}
8370 		}
8371 	}
8372 }
8373 
8374 static void
8375 ath12k_mac_filter_eht_cap_mesh(struct ieee80211_eht_cap_elem_fixed
8376 			       *eht_cap_elem)
8377 {
8378 	u8 m;
8379 
8380 	m = IEEE80211_EHT_MAC_CAP0_EPCS_PRIO_ACCESS;
8381 	eht_cap_elem->mac_cap_info[0] &= ~m;
8382 
8383 	m = IEEE80211_EHT_PHY_CAP0_PARTIAL_BW_UL_MU_MIMO;
8384 	eht_cap_elem->phy_cap_info[0] &= ~m;
8385 
8386 	m = IEEE80211_EHT_PHY_CAP3_NG_16_MU_FEEDBACK |
8387 	    IEEE80211_EHT_PHY_CAP3_CODEBOOK_7_5_MU_FDBK |
8388 	    IEEE80211_EHT_PHY_CAP3_TRIG_MU_BF_PART_BW_FDBK |
8389 	    IEEE80211_EHT_PHY_CAP3_TRIG_CQI_FDBK;
8390 	eht_cap_elem->phy_cap_info[3] &= ~m;
8391 
8392 	m = IEEE80211_EHT_PHY_CAP4_PART_BW_DL_MU_MIMO |
8393 	    IEEE80211_EHT_PHY_CAP4_PSR_SR_SUPP |
8394 	    IEEE80211_EHT_PHY_CAP4_POWER_BOOST_FACT_SUPP |
8395 	    IEEE80211_EHT_PHY_CAP4_EHT_MU_PPDU_4_EHT_LTF_08_GI;
8396 	eht_cap_elem->phy_cap_info[4] &= ~m;
8397 
8398 	m = IEEE80211_EHT_PHY_CAP5_NON_TRIG_CQI_FEEDBACK |
8399 	    IEEE80211_EHT_PHY_CAP5_TX_LESS_242_TONE_RU_SUPP |
8400 	    IEEE80211_EHT_PHY_CAP5_RX_LESS_242_TONE_RU_SUPP |
8401 	    IEEE80211_EHT_PHY_CAP5_MAX_NUM_SUPP_EHT_LTF_MASK;
8402 	eht_cap_elem->phy_cap_info[5] &= ~m;
8403 
8404 	m = IEEE80211_EHT_PHY_CAP6_MAX_NUM_SUPP_EHT_LTF_MASK;
8405 	eht_cap_elem->phy_cap_info[6] &= ~m;
8406 
8407 	m = IEEE80211_EHT_PHY_CAP7_NON_OFDMA_UL_MU_MIMO_80MHZ |
8408 	    IEEE80211_EHT_PHY_CAP7_NON_OFDMA_UL_MU_MIMO_160MHZ |
8409 	    IEEE80211_EHT_PHY_CAP7_NON_OFDMA_UL_MU_MIMO_320MHZ |
8410 	    IEEE80211_EHT_PHY_CAP7_MU_BEAMFORMER_80MHZ |
8411 	    IEEE80211_EHT_PHY_CAP7_MU_BEAMFORMER_160MHZ |
8412 	    IEEE80211_EHT_PHY_CAP7_MU_BEAMFORMER_320MHZ;
8413 	eht_cap_elem->phy_cap_info[7] &= ~m;
8414 }
8415 
8416 static void ath12k_mac_copy_eht_cap(struct ath12k *ar,
8417 				    struct ath12k_band_cap *band_cap,
8418 				    struct ieee80211_he_cap_elem *he_cap_elem,
8419 				    int iftype,
8420 				    struct ieee80211_sta_eht_cap *eht_cap)
8421 {
8422 	struct ieee80211_eht_cap_elem_fixed *eht_cap_elem = &eht_cap->eht_cap_elem;
8423 
8424 	memset(eht_cap, 0, sizeof(struct ieee80211_sta_eht_cap));
8425 
8426 	if (!(test_bit(WMI_TLV_SERVICE_11BE, ar->ab->wmi_ab.svc_map)) ||
8427 	    ath12k_acpi_get_disable_11be(ar->ab))
8428 		return;
8429 
8430 	eht_cap->has_eht = true;
8431 	memcpy(eht_cap_elem->mac_cap_info, band_cap->eht_cap_mac_info,
8432 	       sizeof(eht_cap_elem->mac_cap_info));
8433 	memcpy(eht_cap_elem->phy_cap_info, band_cap->eht_cap_phy_info,
8434 	       sizeof(eht_cap_elem->phy_cap_info));
8435 
8436 	switch (iftype) {
8437 	case NL80211_IFTYPE_AP:
8438 		eht_cap_elem->phy_cap_info[0] &=
8439 			~IEEE80211_EHT_PHY_CAP0_242_TONE_RU_GT20MHZ;
8440 		eht_cap_elem->phy_cap_info[4] &=
8441 			~IEEE80211_EHT_PHY_CAP4_PART_BW_DL_MU_MIMO;
8442 		eht_cap_elem->phy_cap_info[5] &=
8443 			~IEEE80211_EHT_PHY_CAP5_TX_LESS_242_TONE_RU_SUPP;
8444 		break;
8445 	case NL80211_IFTYPE_STATION:
8446 		eht_cap_elem->phy_cap_info[7] &=
8447 			~(IEEE80211_EHT_PHY_CAP7_NON_OFDMA_UL_MU_MIMO_80MHZ |
8448 			  IEEE80211_EHT_PHY_CAP7_NON_OFDMA_UL_MU_MIMO_160MHZ |
8449 			  IEEE80211_EHT_PHY_CAP7_NON_OFDMA_UL_MU_MIMO_320MHZ);
8450 		eht_cap_elem->phy_cap_info[7] &=
8451 			~(IEEE80211_EHT_PHY_CAP7_MU_BEAMFORMER_80MHZ |
8452 			  IEEE80211_EHT_PHY_CAP7_MU_BEAMFORMER_160MHZ |
8453 			  IEEE80211_EHT_PHY_CAP7_MU_BEAMFORMER_320MHZ);
8454 		break;
8455 	case NL80211_IFTYPE_MESH_POINT:
8456 		ath12k_mac_filter_eht_cap_mesh(eht_cap_elem);
8457 		break;
8458 	default:
8459 		break;
8460 	}
8461 
8462 	ath12k_mac_copy_eht_mcs_nss(band_cap, &eht_cap->eht_mcs_nss_supp,
8463 				    he_cap_elem, eht_cap_elem);
8464 
8465 	if (eht_cap_elem->phy_cap_info[5] &
8466 	    IEEE80211_EHT_PHY_CAP5_PPE_THRESHOLD_PRESENT)
8467 		ath12k_mac_copy_eht_ppe_thresh(&band_cap->eht_ppet, eht_cap);
8468 }
8469 
8470 static int ath12k_mac_copy_sband_iftype_data(struct ath12k *ar,
8471 					     struct ath12k_pdev_cap *cap,
8472 					     struct ieee80211_sband_iftype_data *data,
8473 					     int band)
8474 {
8475 	struct ath12k_band_cap *band_cap = &cap->band[band];
8476 	int i, idx = 0;
8477 
8478 	for (i = 0; i < NUM_NL80211_IFTYPES; i++) {
8479 		struct ieee80211_sta_he_cap *he_cap = &data[idx].he_cap;
8480 
8481 		switch (i) {
8482 		case NL80211_IFTYPE_STATION:
8483 		case NL80211_IFTYPE_AP:
8484 		case NL80211_IFTYPE_MESH_POINT:
8485 			break;
8486 
8487 		default:
8488 			continue;
8489 		}
8490 
8491 		data[idx].types_mask = BIT(i);
8492 
8493 		ath12k_mac_copy_he_cap(ar, band_cap, i, ar->num_tx_chains, he_cap);
8494 		if (band == NL80211_BAND_6GHZ) {
8495 			data[idx].he_6ghz_capa.capa =
8496 				ath12k_mac_setup_he_6ghz_cap(cap, band_cap);
8497 		}
8498 		ath12k_mac_copy_eht_cap(ar, band_cap, &he_cap->he_cap_elem, i,
8499 					&data[idx].eht_cap);
8500 		idx++;
8501 	}
8502 
8503 	return idx;
8504 }
8505 
8506 static void ath12k_mac_setup_sband_iftype_data(struct ath12k *ar,
8507 					       struct ath12k_pdev_cap *cap)
8508 {
8509 	struct ieee80211_supported_band *sband;
8510 	enum nl80211_band band;
8511 	int count;
8512 
8513 	if (cap->supported_bands & WMI_HOST_WLAN_2GHZ_CAP) {
8514 		band = NL80211_BAND_2GHZ;
8515 		count = ath12k_mac_copy_sband_iftype_data(ar, cap,
8516 							  ar->mac.iftype[band],
8517 							  band);
8518 		sband = &ar->mac.sbands[band];
8519 		_ieee80211_set_sband_iftype_data(sband, ar->mac.iftype[band],
8520 						 count);
8521 	}
8522 
8523 	if (cap->supported_bands & WMI_HOST_WLAN_5GHZ_CAP) {
8524 		band = NL80211_BAND_5GHZ;
8525 		count = ath12k_mac_copy_sband_iftype_data(ar, cap,
8526 							  ar->mac.iftype[band],
8527 							  band);
8528 		sband = &ar->mac.sbands[band];
8529 		_ieee80211_set_sband_iftype_data(sband, ar->mac.iftype[band],
8530 						 count);
8531 	}
8532 
8533 	if (cap->supported_bands & WMI_HOST_WLAN_5GHZ_CAP &&
8534 	    ar->supports_6ghz) {
8535 		band = NL80211_BAND_6GHZ;
8536 		count = ath12k_mac_copy_sband_iftype_data(ar, cap,
8537 							  ar->mac.iftype[band],
8538 							  band);
8539 		sband = &ar->mac.sbands[band];
8540 		_ieee80211_set_sband_iftype_data(sband, ar->mac.iftype[band],
8541 						 count);
8542 	}
8543 }
8544 
8545 static int __ath12k_set_antenna(struct ath12k *ar, u32 tx_ant, u32 rx_ant)
8546 {
8547 	struct ath12k_hw *ah = ath12k_ar_to_ah(ar);
8548 	int ret;
8549 
8550 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
8551 
8552 	if (ath12k_check_chain_mask(ar, tx_ant, true))
8553 		return -EINVAL;
8554 
8555 	if (ath12k_check_chain_mask(ar, rx_ant, false))
8556 		return -EINVAL;
8557 
8558 	/* Since we advertised the max cap of all radios combined during wiphy
8559 	 * registration, ensure we don't set the antenna config higher than the
8560 	 * limits
8561 	 */
8562 	tx_ant = min_t(u32, tx_ant, ar->pdev->cap.tx_chain_mask);
8563 	rx_ant = min_t(u32, rx_ant, ar->pdev->cap.rx_chain_mask);
8564 
8565 	ar->cfg_tx_chainmask = tx_ant;
8566 	ar->cfg_rx_chainmask = rx_ant;
8567 
8568 	if (ah->state != ATH12K_HW_STATE_ON &&
8569 	    ah->state != ATH12K_HW_STATE_RESTARTED)
8570 		return 0;
8571 
8572 	ret = ath12k_wmi_pdev_set_param(ar, WMI_PDEV_PARAM_TX_CHAIN_MASK,
8573 					tx_ant, ar->pdev->pdev_id);
8574 	if (ret) {
8575 		ath12k_warn(ar->ab, "failed to set tx-chainmask: %d, req 0x%x\n",
8576 			    ret, tx_ant);
8577 		return ret;
8578 	}
8579 
8580 	ar->num_tx_chains = hweight32(tx_ant);
8581 
8582 	ret = ath12k_wmi_pdev_set_param(ar, WMI_PDEV_PARAM_RX_CHAIN_MASK,
8583 					rx_ant, ar->pdev->pdev_id);
8584 	if (ret) {
8585 		ath12k_warn(ar->ab, "failed to set rx-chainmask: %d, req 0x%x\n",
8586 			    ret, rx_ant);
8587 		return ret;
8588 	}
8589 
8590 	ar->num_rx_chains = hweight32(rx_ant);
8591 
8592 	/* Reload HT/VHT/HE capability */
8593 	ath12k_mac_setup_ht_vht_cap(ar, &ar->pdev->cap, NULL);
8594 	ath12k_mac_setup_sband_iftype_data(ar, &ar->pdev->cap);
8595 
8596 	return 0;
8597 }
8598 
8599 static void ath12k_mgmt_over_wmi_tx_drop(struct ath12k *ar, struct sk_buff *skb)
8600 {
8601 	int num_mgmt;
8602 
8603 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
8604 
8605 	ieee80211_free_txskb(ath12k_ar_to_hw(ar), skb);
8606 
8607 	num_mgmt = atomic_dec_if_positive(&ar->num_pending_mgmt_tx);
8608 
8609 	if (num_mgmt < 0)
8610 		WARN_ON_ONCE(1);
8611 
8612 	if (!num_mgmt)
8613 		wake_up(&ar->txmgmt_empty_waitq);
8614 }
8615 
8616 static void ath12k_mac_tx_mgmt_free(struct ath12k *ar, int buf_id)
8617 {
8618 	struct sk_buff *msdu;
8619 	struct ieee80211_tx_info *info;
8620 
8621 	spin_lock_bh(&ar->txmgmt_idr_lock);
8622 	msdu = idr_remove(&ar->txmgmt_idr, buf_id);
8623 	spin_unlock_bh(&ar->txmgmt_idr_lock);
8624 
8625 	if (!msdu)
8626 		return;
8627 
8628 	dma_unmap_single(ar->ab->dev, ATH12K_SKB_CB(msdu)->paddr, msdu->len,
8629 			 DMA_TO_DEVICE);
8630 
8631 	info = IEEE80211_SKB_CB(msdu);
8632 	memset(&info->status, 0, sizeof(info->status));
8633 
8634 	ath12k_mgmt_over_wmi_tx_drop(ar, msdu);
8635 }
8636 
8637 int ath12k_mac_tx_mgmt_pending_free(int buf_id, void *skb, void *ctx)
8638 {
8639 	struct ath12k *ar = ctx;
8640 
8641 	ath12k_mac_tx_mgmt_free(ar, buf_id);
8642 
8643 	return 0;
8644 }
8645 
8646 static int ath12k_mac_vif_txmgmt_idr_remove(int buf_id, void *skb, void *ctx)
8647 {
8648 	struct ieee80211_vif *vif = ctx;
8649 	struct ath12k_skb_cb *skb_cb = ATH12K_SKB_CB(skb);
8650 	struct ath12k *ar = skb_cb->ar;
8651 
8652 	if (skb_cb->vif == vif)
8653 		ath12k_mac_tx_mgmt_free(ar, buf_id);
8654 
8655 	return 0;
8656 }
8657 
8658 static int ath12k_mac_mgmt_tx_wmi(struct ath12k *ar, struct ath12k_link_vif *arvif,
8659 				  struct sk_buff *skb)
8660 {
8661 	struct ath12k_base *ab = ar->ab;
8662 	struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
8663 	struct ath12k_skb_cb *skb_cb = ATH12K_SKB_CB(skb);
8664 	struct ieee80211_tx_info *info;
8665 	enum hal_encrypt_type enctype;
8666 	unsigned int mic_len;
8667 	dma_addr_t paddr;
8668 	int buf_id;
8669 	int ret;
8670 
8671 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
8672 
8673 	skb_cb->ar = ar;
8674 	spin_lock_bh(&ar->txmgmt_idr_lock);
8675 	buf_id = idr_alloc(&ar->txmgmt_idr, skb, 0,
8676 			   ATH12K_TX_MGMT_NUM_PENDING_MAX, GFP_ATOMIC);
8677 	spin_unlock_bh(&ar->txmgmt_idr_lock);
8678 	if (buf_id < 0)
8679 		return -ENOSPC;
8680 
8681 	info = IEEE80211_SKB_CB(skb);
8682 	if ((skb_cb->flags & ATH12K_SKB_CIPHER_SET) &&
8683 	    !(info->flags & IEEE80211_TX_CTL_HW_80211_ENCAP)) {
8684 		if ((ieee80211_is_action(hdr->frame_control) ||
8685 		     ieee80211_is_deauth(hdr->frame_control) ||
8686 		     ieee80211_is_disassoc(hdr->frame_control)) &&
8687 		     ieee80211_has_protected(hdr->frame_control)) {
8688 			enctype = ath12k_dp_tx_get_encrypt_type(skb_cb->cipher);
8689 			mic_len = ath12k_dp_rx_crypto_mic_len(ar, enctype);
8690 			skb_put(skb, mic_len);
8691 		}
8692 	}
8693 
8694 	paddr = dma_map_single(ab->dev, skb->data, skb->len, DMA_TO_DEVICE);
8695 	if (dma_mapping_error(ab->dev, paddr)) {
8696 		ath12k_warn(ab, "failed to DMA map mgmt Tx buffer\n");
8697 		ret = -EIO;
8698 		goto err_free_idr;
8699 	}
8700 
8701 	skb_cb->paddr = paddr;
8702 
8703 	ret = ath12k_wmi_mgmt_send(arvif, buf_id, skb);
8704 	if (ret) {
8705 		ath12k_warn(ar->ab, "failed to send mgmt frame: %d\n", ret);
8706 		goto err_unmap_buf;
8707 	}
8708 
8709 	return 0;
8710 
8711 err_unmap_buf:
8712 	dma_unmap_single(ab->dev, skb_cb->paddr,
8713 			 skb->len, DMA_TO_DEVICE);
8714 err_free_idr:
8715 	spin_lock_bh(&ar->txmgmt_idr_lock);
8716 	idr_remove(&ar->txmgmt_idr, buf_id);
8717 	spin_unlock_bh(&ar->txmgmt_idr_lock);
8718 
8719 	return ret;
8720 }
8721 
8722 static void ath12k_mgmt_over_wmi_tx_purge(struct ath12k *ar)
8723 {
8724 	struct sk_buff *skb;
8725 
8726 	while ((skb = skb_dequeue(&ar->wmi_mgmt_tx_queue)) != NULL)
8727 		ath12k_mgmt_over_wmi_tx_drop(ar, skb);
8728 }
8729 
8730 static int ath12k_mac_mgmt_action_frame_fill_elem_data(struct ath12k_link_vif *arvif,
8731 						       struct sk_buff *skb)
8732 {
8733 	struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
8734 	u8 category, *buf, iv_len, action_code, dialog_token;
8735 	struct ieee80211_bss_conf *link_conf;
8736 	struct ieee80211_chanctx_conf *conf;
8737 	int cur_tx_power, max_tx_power;
8738 	struct ath12k *ar = arvif->ar;
8739 	struct ieee80211_hw *hw = ath12k_ar_to_hw(ar);
8740 	struct wiphy *wiphy = hw->wiphy;
8741 	struct ath12k_skb_cb *skb_cb;
8742 	struct ieee80211_mgmt *mgmt;
8743 	unsigned int remaining_len;
8744 	bool has_protected;
8745 
8746 	lockdep_assert_wiphy(wiphy);
8747 
8748 	/* make sure category field is present */
8749 	if (skb->len < IEEE80211_MIN_ACTION_SIZE)
8750 		return -EINVAL;
8751 
8752 	remaining_len = skb->len - IEEE80211_MIN_ACTION_SIZE;
8753 	has_protected = ieee80211_has_protected(hdr->frame_control);
8754 
8755 	/* In case of SW crypto and hdr protected (PMF), packet will already be encrypted,
8756 	 * we can't put in data in this case
8757 	 */
8758 	if (test_bit(ATH12K_FLAG_HW_CRYPTO_DISABLED, &ar->ab->dev_flags) &&
8759 	    has_protected)
8760 		return 0;
8761 
8762 	mgmt = (struct ieee80211_mgmt *)hdr;
8763 	buf = (u8 *)&mgmt->u.action;
8764 
8765 	/* FCTL_PROTECTED frame might have extra space added for HDR_LEN. Offset that
8766 	 * many bytes if it is there
8767 	 */
8768 	if (has_protected) {
8769 		skb_cb = ATH12K_SKB_CB(skb);
8770 
8771 		switch (skb_cb->cipher) {
8772 		/* Cipher suite having flag %IEEE80211_KEY_FLAG_GENERATE_IV_MGMT set in
8773 		 * key needs to be processed. See ath12k_install_key()
8774 		 */
8775 		case WLAN_CIPHER_SUITE_CCMP:
8776 		case WLAN_CIPHER_SUITE_CCMP_256:
8777 		case WLAN_CIPHER_SUITE_GCMP:
8778 		case WLAN_CIPHER_SUITE_GCMP_256:
8779 			iv_len = IEEE80211_CCMP_HDR_LEN;
8780 			break;
8781 		case WLAN_CIPHER_SUITE_TKIP:
8782 			iv_len = 0;
8783 			break;
8784 		default:
8785 			return -EINVAL;
8786 		}
8787 
8788 		if (remaining_len < iv_len)
8789 			return -EINVAL;
8790 
8791 		buf += iv_len;
8792 		remaining_len -= iv_len;
8793 	}
8794 
8795 	category = *buf++;
8796 	/* category code is already taken care in %IEEE80211_MIN_ACTION_SIZE hence
8797 	 * no need to adjust remaining_len
8798 	 */
8799 
8800 	switch (category) {
8801 	case WLAN_CATEGORY_RADIO_MEASUREMENT:
8802 		/* need action code and dialog token */
8803 		if (remaining_len < 2)
8804 			return -EINVAL;
8805 
8806 		/* Packet Format:
8807 		 *	Action Code | Dialog Token | Variable Len (based on Action Code)
8808 		 */
8809 		action_code = *buf++;
8810 		dialog_token = *buf++;
8811 		remaining_len -= 2;
8812 
8813 		link_conf = ath12k_mac_get_link_bss_conf(arvif);
8814 		if (!link_conf) {
8815 			ath12k_warn(ar->ab,
8816 				    "failed to get bss link conf for vdev %d in RM handling\n",
8817 				    arvif->vdev_id);
8818 			return -EINVAL;
8819 		}
8820 
8821 		conf = wiphy_dereference(wiphy, link_conf->chanctx_conf);
8822 		if (!conf)
8823 			return -ENOENT;
8824 
8825 		cur_tx_power = link_conf->txpower;
8826 		max_tx_power = min(conf->def.chan->max_reg_power,
8827 				   (int)ar->max_tx_power / 2);
8828 
8829 		ath12k_mac_op_get_txpower(hw, arvif->ahvif->vif, arvif->link_id,
8830 					  &cur_tx_power);
8831 
8832 		switch (action_code) {
8833 		case WLAN_RM_ACTION_LINK_MEASUREMENT_REQUEST:
8834 			/* need variable fields to be present in len */
8835 			if (remaining_len < 2)
8836 				return -EINVAL;
8837 
8838 			/* Variable length format as defined in IEEE 802.11-2024,
8839 			 * Figure 9-1187-Link Measurement Request frame Action field
8840 			 * format.
8841 			 *	Transmit Power | Max Tx Power
8842 			 * We fill both of these.
8843 			 */
8844 			*buf++ = cur_tx_power;
8845 			*buf = max_tx_power;
8846 
8847 			ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
8848 				   "RRM: Link Measurement Req dialog_token %u cur_tx_power %d max_tx_power %d\n",
8849 				   dialog_token, cur_tx_power, max_tx_power);
8850 			break;
8851 		case WLAN_RM_ACTION_LINK_MEASUREMENT_REPORT:
8852 			/* need variable fields to be present in len */
8853 			if (remaining_len < 3)
8854 				return -EINVAL;
8855 
8856 			/* Variable length format as defined in IEEE 802.11-2024,
8857 			 * Figure 9-1188-Link Measurement Report frame Action field format
8858 			 *	TPC Report | Variable Fields
8859 			 *
8860 			 * TPC Report Format:
8861 			 *	Element ID | Len | Tx Power | Link Margin
8862 			 *
8863 			 * We fill Tx power in the TPC Report (2nd index)
8864 			 */
8865 			buf[2] = cur_tx_power;
8866 
8867 			/* TODO: At present, Link margin data is not present so can't
8868 			 * really fill it now. Once it is available, it can be added
8869 			 * here
8870 			 */
8871 			ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
8872 				   "RRM: Link Measurement Report dialog_token %u cur_tx_power %d\n",
8873 				   dialog_token, cur_tx_power);
8874 			break;
8875 		default:
8876 			return -EINVAL;
8877 		}
8878 		break;
8879 	default:
8880 		/* nothing to fill */
8881 		return 0;
8882 	}
8883 
8884 	return 0;
8885 }
8886 
8887 static int ath12k_mac_mgmt_frame_fill_elem_data(struct ath12k_link_vif *arvif,
8888 						struct sk_buff *skb)
8889 {
8890 	struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
8891 
8892 	if (!ieee80211_is_action(hdr->frame_control))
8893 		return 0;
8894 
8895 	return ath12k_mac_mgmt_action_frame_fill_elem_data(arvif, skb);
8896 }
8897 
8898 static void ath12k_mgmt_over_wmi_tx_work(struct wiphy *wiphy, struct wiphy_work *work)
8899 {
8900 	struct ath12k *ar = container_of(work, struct ath12k, wmi_mgmt_tx_work);
8901 	struct ath12k_hw *ah = ar->ah;
8902 	struct ath12k_skb_cb *skb_cb;
8903 	struct ath12k_vif *ahvif;
8904 	struct ath12k_link_vif *arvif;
8905 	struct sk_buff *skb;
8906 	int ret;
8907 
8908 	lockdep_assert_wiphy(wiphy);
8909 
8910 	while ((skb = skb_dequeue(&ar->wmi_mgmt_tx_queue)) != NULL) {
8911 		skb_cb = ATH12K_SKB_CB(skb);
8912 		if (!skb_cb->vif) {
8913 			ath12k_warn(ar->ab, "no vif found for mgmt frame\n");
8914 			ath12k_mgmt_over_wmi_tx_drop(ar, skb);
8915 			continue;
8916 		}
8917 
8918 		ahvif = ath12k_vif_to_ahvif(skb_cb->vif);
8919 		if (!(ahvif->links_map & BIT(skb_cb->link_id))) {
8920 			ath12k_warn(ar->ab,
8921 				    "invalid linkid %u in mgmt over wmi tx with linkmap 0x%x\n",
8922 				    skb_cb->link_id, ahvif->links_map);
8923 			ath12k_mgmt_over_wmi_tx_drop(ar, skb);
8924 			continue;
8925 		}
8926 
8927 		arvif = wiphy_dereference(ah->hw->wiphy, ahvif->link[skb_cb->link_id]);
8928 		if (ar->allocated_vdev_map & (1LL << arvif->vdev_id)) {
8929 			/* Fill in the data which is required to be filled by the driver
8930 			 * For example: Max Tx power in Link Measurement Request/Report
8931 			 */
8932 			ret = ath12k_mac_mgmt_frame_fill_elem_data(arvif, skb);
8933 			if (ret) {
8934 				/* If we couldn't fill the data due to any reason,
8935 				 * let's not discard transmitting the packet.
8936 				 * For example: Software crypto and PMF case
8937 				 */
8938 				ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
8939 					   "Failed to fill the required data for the mgmt packet err %d\n",
8940 					   ret);
8941 			}
8942 
8943 			ret = ath12k_mac_mgmt_tx_wmi(ar, arvif, skb);
8944 			if (ret) {
8945 				ath12k_warn(ar->ab, "failed to tx mgmt frame, vdev_id %d :%d\n",
8946 					    arvif->vdev_id, ret);
8947 				ath12k_mgmt_over_wmi_tx_drop(ar, skb);
8948 			}
8949 		} else {
8950 			ath12k_warn(ar->ab,
8951 				    "dropping mgmt frame for vdev %d link %u is_started %d\n",
8952 				    arvif->vdev_id,
8953 				    skb_cb->link_id,
8954 				    arvif->is_started);
8955 			ath12k_mgmt_over_wmi_tx_drop(ar, skb);
8956 		}
8957 	}
8958 }
8959 
8960 static int ath12k_mac_mgmt_tx(struct ath12k *ar, struct sk_buff *skb,
8961 			      bool is_prb_rsp)
8962 {
8963 	struct sk_buff_head *q = &ar->wmi_mgmt_tx_queue;
8964 
8965 	if (test_bit(ATH12K_FLAG_CRASH_FLUSH, &ar->ab->dev_flags))
8966 		return -ESHUTDOWN;
8967 
8968 	/* Drop probe response packets when the pending management tx
8969 	 * count has reached a certain threshold, so as to prioritize
8970 	 * other mgmt packets like auth and assoc to be sent on time
8971 	 * for establishing successful connections.
8972 	 */
8973 	if (is_prb_rsp &&
8974 	    atomic_read(&ar->num_pending_mgmt_tx) > ATH12K_PRB_RSP_DROP_THRESHOLD) {
8975 		ath12k_warn(ar->ab,
8976 			    "dropping probe response as pending queue is almost full\n");
8977 		return -ENOSPC;
8978 	}
8979 
8980 	if (skb_queue_len_lockless(q) >= ATH12K_TX_MGMT_NUM_PENDING_MAX) {
8981 		ath12k_warn(ar->ab, "mgmt tx queue is full\n");
8982 		return -ENOSPC;
8983 	}
8984 
8985 	skb_queue_tail(q, skb);
8986 	atomic_inc(&ar->num_pending_mgmt_tx);
8987 	wiphy_work_queue(ath12k_ar_to_hw(ar)->wiphy, &ar->wmi_mgmt_tx_work);
8988 
8989 	return 0;
8990 }
8991 
8992 static void ath12k_mac_add_p2p_noa_ie(struct ath12k *ar,
8993 				      struct ieee80211_vif *vif,
8994 				      struct sk_buff *skb,
8995 				      bool is_prb_rsp)
8996 {
8997 	struct ath12k_vif *ahvif = ath12k_vif_to_ahvif(vif);
8998 
8999 	if (likely(!is_prb_rsp))
9000 		return;
9001 
9002 	spin_lock_bh(&ar->data_lock);
9003 
9004 	if (ahvif->u.ap.noa_data &&
9005 	    !pskb_expand_head(skb, 0, ahvif->u.ap.noa_len,
9006 			      GFP_ATOMIC))
9007 		skb_put_data(skb, ahvif->u.ap.noa_data,
9008 			     ahvif->u.ap.noa_len);
9009 
9010 	spin_unlock_bh(&ar->data_lock);
9011 }
9012 
9013 /* Note: called under rcu_read_lock() */
9014 static void ath12k_mlo_mcast_update_tx_link_address(struct ieee80211_vif *vif,
9015 						    u8 link_id, struct sk_buff *skb,
9016 						    u32 info_flags)
9017 {
9018 	struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
9019 	struct ieee80211_bss_conf *bss_conf;
9020 
9021 	if (info_flags & IEEE80211_TX_CTL_HW_80211_ENCAP)
9022 		return;
9023 
9024 	bss_conf = rcu_dereference(vif->link_conf[link_id]);
9025 	if (bss_conf)
9026 		ether_addr_copy(hdr->addr2, bss_conf->addr);
9027 }
9028 
9029 /* Note: called under rcu_read_lock() */
9030 static u8 ath12k_mac_get_tx_link(struct ieee80211_sta *sta, struct ieee80211_vif *vif,
9031 				 u8 link, struct sk_buff *skb, u32 info_flags)
9032 {
9033 	struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
9034 	struct ath12k_vif *ahvif = ath12k_vif_to_ahvif(vif);
9035 	struct ieee80211_link_sta *link_sta;
9036 	struct ieee80211_bss_conf *bss_conf;
9037 	struct ath12k_sta *ahsta;
9038 
9039 	/* Use the link id passed or the default vif link */
9040 	if (!sta) {
9041 		if (link != IEEE80211_LINK_UNSPECIFIED)
9042 			return link;
9043 
9044 		return ahvif->deflink.link_id;
9045 	}
9046 
9047 	ahsta = ath12k_sta_to_ahsta(sta);
9048 
9049 	/* Below translation ensures we pass proper A2 & A3 for non ML clients.
9050 	 * Also it assumes for now support only for MLO AP in this path
9051 	 */
9052 	if (!sta->mlo) {
9053 		link = ahsta->deflink.link_id;
9054 
9055 		if (info_flags & IEEE80211_TX_CTL_HW_80211_ENCAP)
9056 			return link;
9057 
9058 		bss_conf = rcu_dereference(vif->link_conf[link]);
9059 		if (bss_conf) {
9060 			ether_addr_copy(hdr->addr2, bss_conf->addr);
9061 			if (!ieee80211_has_tods(hdr->frame_control) &&
9062 			    !ieee80211_has_fromds(hdr->frame_control))
9063 				ether_addr_copy(hdr->addr3, bss_conf->addr);
9064 		}
9065 
9066 		return link;
9067 	}
9068 
9069 	/* enqueue eth enacap & data frames on primary link, FW does link
9070 	 * selection and address translation.
9071 	 */
9072 	if (info_flags & IEEE80211_TX_CTL_HW_80211_ENCAP ||
9073 	    ieee80211_is_data(hdr->frame_control))
9074 		return ahsta->assoc_link_id;
9075 
9076 	/* 802.11 frame cases */
9077 	if (link == IEEE80211_LINK_UNSPECIFIED)
9078 		link = ahsta->deflink.link_id;
9079 
9080 	if (!ieee80211_is_mgmt(hdr->frame_control))
9081 		return link;
9082 
9083 	/* Perform address conversion for ML STA Tx */
9084 	bss_conf = rcu_dereference(vif->link_conf[link]);
9085 	link_sta = rcu_dereference(sta->link[link]);
9086 
9087 	if (bss_conf && link_sta) {
9088 		ether_addr_copy(hdr->addr1, link_sta->addr);
9089 		ether_addr_copy(hdr->addr2, bss_conf->addr);
9090 
9091 		if (vif->type == NL80211_IFTYPE_STATION && bss_conf->bssid)
9092 			ether_addr_copy(hdr->addr3, bss_conf->bssid);
9093 		else if (vif->type == NL80211_IFTYPE_AP)
9094 			ether_addr_copy(hdr->addr3, bss_conf->addr);
9095 
9096 		return link;
9097 	}
9098 
9099 	if (bss_conf) {
9100 		/* In certain cases where a ML sta associated and added subset of
9101 		 * links on which the ML AP is active, but now sends some frame
9102 		 * (ex. Probe request) on a different link which is active in our
9103 		 * MLD but was not added during previous association, we can
9104 		 * still honor the Tx to that ML STA via the requested link.
9105 		 * The control would reach here in such case only when that link
9106 		 * address is same as the MLD address or in worst case clients
9107 		 * used MLD address at TA wrongly which would have helped
9108 		 * identify the ML sta object and pass it here.
9109 		 * If the link address of that STA is different from MLD address,
9110 		 * then the sta object would be NULL and control won't reach
9111 		 * here but return at the start of the function itself with !sta
9112 		 * check. Also this would not need any translation at hdr->addr1
9113 		 * from MLD to link address since the RA is the MLD address
9114 		 * (same as that link address ideally) already.
9115 		 */
9116 		ether_addr_copy(hdr->addr2, bss_conf->addr);
9117 
9118 		if (vif->type == NL80211_IFTYPE_STATION && bss_conf->bssid)
9119 			ether_addr_copy(hdr->addr3, bss_conf->bssid);
9120 		else if (vif->type == NL80211_IFTYPE_AP)
9121 			ether_addr_copy(hdr->addr3, bss_conf->addr);
9122 	}
9123 
9124 	return link;
9125 }
9126 
9127 /* Note: called under rcu_read_lock() */
9128 static void ath12k_mac_op_tx(struct ieee80211_hw *hw,
9129 			     struct ieee80211_tx_control *control,
9130 			     struct sk_buff *skb)
9131 {
9132 	struct ath12k_skb_cb *skb_cb = ATH12K_SKB_CB(skb);
9133 	struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
9134 	struct ieee80211_vif *vif = info->control.vif;
9135 	struct ath12k_vif *ahvif = ath12k_vif_to_ahvif(vif);
9136 	struct ath12k_link_vif *arvif = &ahvif->deflink;
9137 	struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
9138 	struct ieee80211_key_conf *key = info->control.hw_key;
9139 	struct ieee80211_sta *sta = control->sta;
9140 	struct ath12k_link_vif *tmp_arvif;
9141 	u32 info_flags = info->flags;
9142 	struct sk_buff *msdu_copied;
9143 	struct ath12k *ar, *tmp_ar;
9144 	struct ath12k_peer *peer;
9145 	unsigned long links_map;
9146 	bool is_mcast = false;
9147 	bool is_dvlan = false;
9148 	struct ethhdr *eth;
9149 	bool is_prb_rsp;
9150 	u16 mcbc_gsn;
9151 	u8 link_id;
9152 	int ret;
9153 
9154 	if (ahvif->vdev_type == WMI_VDEV_TYPE_MONITOR) {
9155 		ieee80211_free_txskb(hw, skb);
9156 		return;
9157 	}
9158 
9159 	link_id = u32_get_bits(info->control.flags, IEEE80211_TX_CTRL_MLO_LINK);
9160 	memset(skb_cb, 0, sizeof(*skb_cb));
9161 	skb_cb->vif = vif;
9162 
9163 	if (key) {
9164 		skb_cb->cipher = key->cipher;
9165 		skb_cb->flags |= ATH12K_SKB_CIPHER_SET;
9166 	}
9167 
9168 	/* handle only for MLO case, use deflink for non MLO case */
9169 	if (ieee80211_vif_is_mld(vif)) {
9170 		link_id = ath12k_mac_get_tx_link(sta, vif, link_id, skb, info_flags);
9171 		if (link_id >= IEEE80211_MLD_MAX_NUM_LINKS) {
9172 			ieee80211_free_txskb(hw, skb);
9173 			return;
9174 		}
9175 	} else {
9176 		if (vif->type == NL80211_IFTYPE_P2P_DEVICE)
9177 			link_id = ATH12K_FIRST_SCAN_LINK;
9178 		else
9179 			link_id = 0;
9180 	}
9181 
9182 	arvif = rcu_dereference(ahvif->link[link_id]);
9183 	if (!arvif || !arvif->ar) {
9184 		ath12k_warn(ahvif->ah, "failed to find arvif link id %u for frame transmission",
9185 			    link_id);
9186 		ieee80211_free_txskb(hw, skb);
9187 		return;
9188 	}
9189 
9190 	ar = arvif->ar;
9191 	skb_cb->link_id = link_id;
9192 	is_prb_rsp = ieee80211_is_probe_resp(hdr->frame_control);
9193 
9194 	if (info_flags & IEEE80211_TX_CTL_HW_80211_ENCAP) {
9195 		eth = (struct ethhdr *)skb->data;
9196 		is_mcast = is_multicast_ether_addr(eth->h_dest);
9197 
9198 		skb_cb->flags |= ATH12K_SKB_HW_80211_ENCAP;
9199 	} else if (ieee80211_is_mgmt(hdr->frame_control)) {
9200 		if (sta && sta->mlo)
9201 			skb_cb->flags |= ATH12K_SKB_MLO_STA;
9202 
9203 		ret = ath12k_mac_mgmt_tx(ar, skb, is_prb_rsp);
9204 		if (ret) {
9205 			ath12k_warn(ar->ab, "failed to queue management frame %d\n",
9206 				    ret);
9207 			ieee80211_free_txskb(hw, skb);
9208 		}
9209 		return;
9210 	}
9211 
9212 	if (!(info_flags & IEEE80211_TX_CTL_HW_80211_ENCAP))
9213 		is_mcast = is_multicast_ether_addr(hdr->addr1);
9214 
9215 	/* This is case only for P2P_GO */
9216 	if (vif->type == NL80211_IFTYPE_AP && vif->p2p)
9217 		ath12k_mac_add_p2p_noa_ie(ar, vif, skb, is_prb_rsp);
9218 
9219 	/* Checking if it is a DVLAN frame */
9220 	if (!test_bit(ATH12K_FLAG_HW_CRYPTO_DISABLED, &ar->ab->dev_flags) &&
9221 	    !(skb_cb->flags & ATH12K_SKB_HW_80211_ENCAP) &&
9222 	    !(skb_cb->flags & ATH12K_SKB_CIPHER_SET) &&
9223 	    ieee80211_has_protected(hdr->frame_control))
9224 		is_dvlan = true;
9225 
9226 	if (!vif->valid_links || !is_mcast || is_dvlan ||
9227 	    (skb_cb->flags & ATH12K_SKB_HW_80211_ENCAP) ||
9228 	    test_bit(ATH12K_FLAG_RAW_MODE, &ar->ab->dev_flags)) {
9229 		ret = ath12k_dp_tx(ar, arvif, skb, false, 0, is_mcast);
9230 		if (unlikely(ret)) {
9231 			ath12k_warn(ar->ab, "failed to transmit frame %d\n", ret);
9232 			ieee80211_free_txskb(ar->ah->hw, skb);
9233 			return;
9234 		}
9235 	} else {
9236 		mcbc_gsn = atomic_inc_return(&ahvif->mcbc_gsn) & 0xfff;
9237 
9238 		links_map = ahvif->links_map;
9239 		for_each_set_bit(link_id, &links_map,
9240 				 IEEE80211_MLD_MAX_NUM_LINKS) {
9241 			tmp_arvif = rcu_dereference(ahvif->link[link_id]);
9242 			if (!tmp_arvif || !tmp_arvif->is_up)
9243 				continue;
9244 
9245 			tmp_ar = tmp_arvif->ar;
9246 			msdu_copied = skb_copy(skb, GFP_ATOMIC);
9247 			if (!msdu_copied) {
9248 				ath12k_err(ar->ab,
9249 					   "skb copy failure link_id 0x%X vdevid 0x%X\n",
9250 					   link_id, tmp_arvif->vdev_id);
9251 				continue;
9252 			}
9253 
9254 			ath12k_mlo_mcast_update_tx_link_address(vif, link_id,
9255 								msdu_copied,
9256 								info_flags);
9257 
9258 			skb_cb = ATH12K_SKB_CB(msdu_copied);
9259 			skb_cb->link_id = link_id;
9260 
9261 			/* For open mode, skip peer find logic */
9262 			if (unlikely(!ahvif->key_cipher))
9263 				goto skip_peer_find;
9264 
9265 			spin_lock_bh(&tmp_ar->ab->base_lock);
9266 			peer = ath12k_peer_find_by_addr(tmp_ar->ab, tmp_arvif->bssid);
9267 			if (!peer) {
9268 				spin_unlock_bh(&tmp_ar->ab->base_lock);
9269 				ath12k_warn(tmp_ar->ab,
9270 					    "failed to find peer for vdev_id 0x%X addr %pM link_map 0x%X\n",
9271 					    tmp_arvif->vdev_id, tmp_arvif->bssid,
9272 					    ahvif->links_map);
9273 				dev_kfree_skb_any(msdu_copied);
9274 				continue;
9275 			}
9276 
9277 			key = peer->keys[peer->mcast_keyidx];
9278 			if (key) {
9279 				skb_cb->cipher = key->cipher;
9280 				skb_cb->flags |= ATH12K_SKB_CIPHER_SET;
9281 
9282 				hdr = (struct ieee80211_hdr *)msdu_copied->data;
9283 				if (!ieee80211_has_protected(hdr->frame_control))
9284 					hdr->frame_control |=
9285 						cpu_to_le16(IEEE80211_FCTL_PROTECTED);
9286 			}
9287 			spin_unlock_bh(&tmp_ar->ab->base_lock);
9288 
9289 skip_peer_find:
9290 			ret = ath12k_dp_tx(tmp_ar, tmp_arvif,
9291 					   msdu_copied, true, mcbc_gsn, is_mcast);
9292 			if (unlikely(ret)) {
9293 				if (ret == -ENOMEM) {
9294 					/* Drops are expected during heavy multicast
9295 					 * frame flood. Print with debug log
9296 					 * level to avoid lot of console prints
9297 					 */
9298 					ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
9299 						   "failed to transmit frame %d\n",
9300 						   ret);
9301 				} else {
9302 					ath12k_warn(ar->ab,
9303 						    "failed to transmit frame %d\n",
9304 						    ret);
9305 				}
9306 
9307 				dev_kfree_skb_any(msdu_copied);
9308 			}
9309 		}
9310 		ieee80211_free_txskb(ar->ah->hw, skb);
9311 	}
9312 }
9313 
9314 void ath12k_mac_drain_tx(struct ath12k *ar)
9315 {
9316 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
9317 
9318 	/* make sure rcu-protected mac80211 tx path itself is drained */
9319 	synchronize_net();
9320 
9321 	wiphy_work_cancel(ath12k_ar_to_hw(ar)->wiphy, &ar->wmi_mgmt_tx_work);
9322 	ath12k_mgmt_over_wmi_tx_purge(ar);
9323 }
9324 
9325 static int ath12k_mac_config_mon_status_default(struct ath12k *ar, bool enable)
9326 {
9327 	struct htt_rx_ring_tlv_filter tlv_filter = {};
9328 	struct ath12k_base *ab = ar->ab;
9329 	u32 ring_id, i;
9330 	int ret = 0;
9331 
9332 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
9333 
9334 	if (!ab->hw_params->rxdma1_enable)
9335 		return ret;
9336 
9337 	if (enable) {
9338 		tlv_filter = ath12k_mac_mon_status_filter_default;
9339 
9340 		if (ath12k_debugfs_rx_filter(ar))
9341 			tlv_filter.rx_filter = ath12k_debugfs_rx_filter(ar);
9342 	} else {
9343 		tlv_filter.rxmon_disable = true;
9344 	}
9345 
9346 	for (i = 0; i < ab->hw_params->num_rxdma_per_pdev; i++) {
9347 		ring_id = ar->dp.rxdma_mon_dst_ring[i].ring_id;
9348 		ret = ath12k_dp_tx_htt_rx_filter_setup(ab, ring_id,
9349 						       ar->dp.mac_id + i,
9350 						       HAL_RXDMA_MONITOR_DST,
9351 						       DP_RXDMA_REFILL_RING_SIZE,
9352 						       &tlv_filter);
9353 		if (ret) {
9354 			ath12k_err(ab,
9355 				   "failed to setup filter for monitor buf %d\n",
9356 				   ret);
9357 		}
9358 	}
9359 
9360 	return ret;
9361 }
9362 
9363 static int ath12k_mac_start(struct ath12k *ar)
9364 {
9365 	struct ath12k_hw *ah = ar->ah;
9366 	struct ath12k_base *ab = ar->ab;
9367 	struct ath12k_pdev *pdev = ar->pdev;
9368 	int ret;
9369 
9370 	lockdep_assert_held(&ah->hw_mutex);
9371 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
9372 
9373 	ret = ath12k_wmi_pdev_set_param(ar, WMI_PDEV_PARAM_PMF_QOS,
9374 					1, pdev->pdev_id);
9375 
9376 	if (ret) {
9377 		ath12k_err(ab, "failed to enable PMF QOS: %d\n", ret);
9378 		goto err;
9379 	}
9380 
9381 	ret = ath12k_wmi_pdev_set_param(ar, WMI_PDEV_PARAM_DYNAMIC_BW, 1,
9382 					pdev->pdev_id);
9383 	if (ret) {
9384 		ath12k_err(ab, "failed to enable dynamic bw: %d\n", ret);
9385 		goto err;
9386 	}
9387 
9388 	ret = ath12k_wmi_pdev_set_param(ar, WMI_PDEV_PARAM_ARP_AC_OVERRIDE,
9389 					0, pdev->pdev_id);
9390 	if (ret) {
9391 		ath12k_err(ab, "failed to set ac override for ARP: %d\n",
9392 			   ret);
9393 		goto err;
9394 	}
9395 
9396 	ret = ath12k_wmi_send_dfs_phyerr_offload_enable_cmd(ar, pdev->pdev_id);
9397 	if (ret) {
9398 		ath12k_err(ab, "failed to offload radar detection: %d\n",
9399 			   ret);
9400 		goto err;
9401 	}
9402 
9403 	ret = ath12k_dp_tx_htt_h2t_ppdu_stats_req(ar,
9404 						  HTT_PPDU_STATS_TAG_DEFAULT);
9405 	if (ret) {
9406 		ath12k_err(ab, "failed to req ppdu stats: %d\n", ret);
9407 		goto err;
9408 	}
9409 
9410 	ret = ath12k_wmi_pdev_set_param(ar, WMI_PDEV_PARAM_MESH_MCAST_ENABLE,
9411 					1, pdev->pdev_id);
9412 
9413 	if (ret) {
9414 		ath12k_err(ab, "failed to enable MESH MCAST ENABLE: (%d\n", ret);
9415 		goto err;
9416 	}
9417 
9418 	__ath12k_set_antenna(ar, ar->cfg_tx_chainmask, ar->cfg_rx_chainmask);
9419 
9420 	/* TODO: Do we need to enable ANI? */
9421 
9422 	ret = ath12k_reg_update_chan_list(ar, false);
9423 
9424 	/* The ar state alone can be turned off for non supported country
9425 	 * without returning the error value. As we need to update the channel
9426 	 * for the next ar.
9427 	 */
9428 	if (ret) {
9429 		if (ret == -EINVAL)
9430 			ret = 0;
9431 		goto err;
9432 	}
9433 
9434 	ar->num_started_vdevs = 0;
9435 	ar->num_created_vdevs = 0;
9436 	ar->num_peers = 0;
9437 	ar->allocated_vdev_map = 0;
9438 	ar->chan_tx_pwr = ATH12K_PDEV_TX_POWER_INVALID;
9439 
9440 	/* Configure monitor status ring with default rx_filter to get rx status
9441 	 * such as rssi, rx_duration.
9442 	 */
9443 	ret = ath12k_mac_config_mon_status_default(ar, true);
9444 	if (ret && (ret != -EOPNOTSUPP)) {
9445 		ath12k_err(ab, "failed to configure monitor status ring with default rx_filter: (%d)\n",
9446 			   ret);
9447 		goto err;
9448 	}
9449 
9450 	if (ret == -EOPNOTSUPP)
9451 		ath12k_dbg(ab, ATH12K_DBG_MAC,
9452 			   "monitor status config is not yet supported");
9453 
9454 	/* Configure the hash seed for hash based reo dest ring selection */
9455 	ath12k_wmi_pdev_lro_cfg(ar, ar->pdev->pdev_id);
9456 
9457 	/* allow device to enter IMPS */
9458 	if (ab->hw_params->idle_ps) {
9459 		ret = ath12k_wmi_pdev_set_param(ar, WMI_PDEV_PARAM_IDLE_PS_CONFIG,
9460 						1, pdev->pdev_id);
9461 		if (ret) {
9462 			ath12k_err(ab, "failed to enable idle ps: %d\n", ret);
9463 			goto err;
9464 		}
9465 	}
9466 
9467 	rcu_assign_pointer(ab->pdevs_active[ar->pdev_idx],
9468 			   &ab->pdevs[ar->pdev_idx]);
9469 
9470 	return 0;
9471 err:
9472 
9473 	return ret;
9474 }
9475 
9476 static void ath12k_drain_tx(struct ath12k_hw *ah)
9477 {
9478 	struct ath12k *ar;
9479 	int i;
9480 
9481 	lockdep_assert_wiphy(ah->hw->wiphy);
9482 
9483 	for_each_ar(ah, ar, i)
9484 		ath12k_mac_drain_tx(ar);
9485 }
9486 
9487 static int ath12k_mac_op_start(struct ieee80211_hw *hw)
9488 {
9489 	struct ath12k_hw *ah = ath12k_hw_to_ah(hw);
9490 	struct ath12k *ar;
9491 	int ret, i;
9492 
9493 	if (ath12k_ftm_mode)
9494 		return -EPERM;
9495 
9496 	lockdep_assert_wiphy(hw->wiphy);
9497 
9498 	ath12k_drain_tx(ah);
9499 
9500 	guard(mutex)(&ah->hw_mutex);
9501 
9502 	switch (ah->state) {
9503 	case ATH12K_HW_STATE_OFF:
9504 		ah->state = ATH12K_HW_STATE_ON;
9505 		break;
9506 	case ATH12K_HW_STATE_RESTARTING:
9507 		ah->state = ATH12K_HW_STATE_RESTARTED;
9508 		break;
9509 	case ATH12K_HW_STATE_RESTARTED:
9510 	case ATH12K_HW_STATE_WEDGED:
9511 	case ATH12K_HW_STATE_ON:
9512 	case ATH12K_HW_STATE_TM:
9513 		ah->state = ATH12K_HW_STATE_OFF;
9514 
9515 		WARN_ON(1);
9516 		return -EINVAL;
9517 	}
9518 
9519 	for_each_ar(ah, ar, i) {
9520 		ret = ath12k_mac_start(ar);
9521 		if (ret) {
9522 			ah->state = ATH12K_HW_STATE_OFF;
9523 
9524 			ath12k_err(ar->ab, "fail to start mac operations in pdev idx %d ret %d\n",
9525 				   ar->pdev_idx, ret);
9526 			goto fail_start;
9527 		}
9528 	}
9529 
9530 	return 0;
9531 
9532 fail_start:
9533 	for (; i > 0; i--) {
9534 		ar = ath12k_ah_to_ar(ah, i - 1);
9535 		ath12k_mac_stop(ar);
9536 	}
9537 
9538 	return ret;
9539 }
9540 
9541 int ath12k_mac_rfkill_config(struct ath12k *ar)
9542 {
9543 	struct ath12k_base *ab = ar->ab;
9544 	u32 param;
9545 	int ret;
9546 
9547 	if (ab->hw_params->rfkill_pin == 0)
9548 		return -EOPNOTSUPP;
9549 
9550 	ath12k_dbg(ab, ATH12K_DBG_MAC,
9551 		   "mac rfkill_pin %d rfkill_cfg %d rfkill_on_level %d",
9552 		   ab->hw_params->rfkill_pin, ab->hw_params->rfkill_cfg,
9553 		   ab->hw_params->rfkill_on_level);
9554 
9555 	param = u32_encode_bits(ab->hw_params->rfkill_on_level,
9556 				WMI_RFKILL_CFG_RADIO_LEVEL) |
9557 		u32_encode_bits(ab->hw_params->rfkill_pin,
9558 				WMI_RFKILL_CFG_GPIO_PIN_NUM) |
9559 		u32_encode_bits(ab->hw_params->rfkill_cfg,
9560 				WMI_RFKILL_CFG_PIN_AS_GPIO);
9561 
9562 	ret = ath12k_wmi_pdev_set_param(ar, WMI_PDEV_PARAM_HW_RFKILL_CONFIG,
9563 					param, ar->pdev->pdev_id);
9564 	if (ret) {
9565 		ath12k_warn(ab,
9566 			    "failed to set rfkill config 0x%x: %d\n",
9567 			    param, ret);
9568 		return ret;
9569 	}
9570 
9571 	return 0;
9572 }
9573 
9574 int ath12k_mac_rfkill_enable_radio(struct ath12k *ar, bool enable)
9575 {
9576 	enum wmi_rfkill_enable_radio param;
9577 	int ret;
9578 
9579 	if (enable)
9580 		param = WMI_RFKILL_ENABLE_RADIO_ON;
9581 	else
9582 		param = WMI_RFKILL_ENABLE_RADIO_OFF;
9583 
9584 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac %d rfkill enable %d",
9585 		   ar->pdev_idx, param);
9586 
9587 	ret = ath12k_wmi_pdev_set_param(ar, WMI_PDEV_PARAM_RFKILL_ENABLE,
9588 					param, ar->pdev->pdev_id);
9589 	if (ret) {
9590 		ath12k_warn(ar->ab, "failed to set rfkill enable param %d: %d\n",
9591 			    param, ret);
9592 		return ret;
9593 	}
9594 
9595 	return 0;
9596 }
9597 
9598 static void ath12k_mac_stop(struct ath12k *ar)
9599 {
9600 	struct ath12k_hw *ah = ar->ah;
9601 	struct htt_ppdu_stats_info *ppdu_stats, *tmp;
9602 	struct ath12k_wmi_scan_chan_list_arg *arg;
9603 	int ret;
9604 
9605 	lockdep_assert_held(&ah->hw_mutex);
9606 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
9607 
9608 	ret = ath12k_mac_config_mon_status_default(ar, false);
9609 	if (ret && (ret != -EOPNOTSUPP))
9610 		ath12k_err(ar->ab, "failed to clear rx_filter for monitor status ring: (%d)\n",
9611 			   ret);
9612 
9613 	clear_bit(ATH12K_FLAG_CAC_RUNNING, &ar->dev_flags);
9614 
9615 	cancel_delayed_work_sync(&ar->scan.timeout);
9616 	wiphy_work_cancel(ath12k_ar_to_hw(ar)->wiphy, &ar->scan.vdev_clean_wk);
9617 	cancel_work_sync(&ar->regd_channel_update_work);
9618 	cancel_work_sync(&ar->regd_update_work);
9619 	cancel_work_sync(&ar->ab->rfkill_work);
9620 	cancel_work_sync(&ar->ab->update_11d_work);
9621 	ar->state_11d = ATH12K_11D_IDLE;
9622 	complete(&ar->completed_11d_scan);
9623 
9624 	spin_lock_bh(&ar->data_lock);
9625 
9626 	list_for_each_entry_safe(ppdu_stats, tmp, &ar->ppdu_stats_info, list) {
9627 		list_del(&ppdu_stats->list);
9628 		kfree(ppdu_stats);
9629 	}
9630 
9631 	while ((arg = list_first_entry_or_null(&ar->regd_channel_update_queue,
9632 					       struct ath12k_wmi_scan_chan_list_arg,
9633 					       list))) {
9634 		list_del(&arg->list);
9635 		kfree(arg);
9636 	}
9637 	spin_unlock_bh(&ar->data_lock);
9638 
9639 	rcu_assign_pointer(ar->ab->pdevs_active[ar->pdev_idx], NULL);
9640 
9641 	synchronize_rcu();
9642 
9643 	atomic_set(&ar->num_pending_mgmt_tx, 0);
9644 }
9645 
9646 static void ath12k_mac_op_stop(struct ieee80211_hw *hw, bool suspend)
9647 {
9648 	struct ath12k_hw *ah = ath12k_hw_to_ah(hw);
9649 	struct ath12k *ar;
9650 	int i;
9651 
9652 	lockdep_assert_wiphy(hw->wiphy);
9653 
9654 	ath12k_drain_tx(ah);
9655 
9656 	mutex_lock(&ah->hw_mutex);
9657 
9658 	ah->state = ATH12K_HW_STATE_OFF;
9659 
9660 	for_each_ar(ah, ar, i)
9661 		ath12k_mac_stop(ar);
9662 
9663 	mutex_unlock(&ah->hw_mutex);
9664 }
9665 
9666 static u8
9667 ath12k_mac_get_vdev_stats_id(struct ath12k_link_vif *arvif)
9668 {
9669 	struct ath12k_base *ab = arvif->ar->ab;
9670 	u8 vdev_stats_id = 0;
9671 
9672 	do {
9673 		if (ab->free_vdev_stats_id_map & (1LL << vdev_stats_id)) {
9674 			vdev_stats_id++;
9675 			if (vdev_stats_id >= ATH12K_MAX_VDEV_STATS_ID) {
9676 				vdev_stats_id = ATH12K_INVAL_VDEV_STATS_ID;
9677 				break;
9678 			}
9679 		} else {
9680 			ab->free_vdev_stats_id_map |= (1LL << vdev_stats_id);
9681 			break;
9682 		}
9683 	} while (vdev_stats_id);
9684 
9685 	arvif->vdev_stats_id = vdev_stats_id;
9686 	return vdev_stats_id;
9687 }
9688 
9689 static int ath12k_mac_setup_vdev_params_mbssid(struct ath12k_link_vif *arvif,
9690 					       u32 *flags, u32 *tx_vdev_id)
9691 {
9692 	struct ath12k_vif *ahvif = arvif->ahvif;
9693 	struct ieee80211_bss_conf *link_conf;
9694 	struct ath12k *ar = arvif->ar;
9695 	struct ath12k_link_vif *tx_arvif;
9696 
9697 	link_conf = ath12k_mac_get_link_bss_conf(arvif);
9698 	if (!link_conf) {
9699 		ath12k_warn(ar->ab, "unable to access bss link conf in set mbssid params for vif %pM link %u\n",
9700 			    ahvif->vif->addr, arvif->link_id);
9701 		return -ENOLINK;
9702 	}
9703 
9704 	tx_arvif = ath12k_mac_get_tx_arvif(arvif, link_conf);
9705 	if (!tx_arvif)
9706 		return 0;
9707 
9708 	if (link_conf->nontransmitted) {
9709 		if (ath12k_ar_to_hw(ar)->wiphy !=
9710 		    ath12k_ar_to_hw(tx_arvif->ar)->wiphy)
9711 			return -EINVAL;
9712 
9713 		*flags = WMI_VDEV_MBSSID_FLAGS_NON_TRANSMIT_AP;
9714 		*tx_vdev_id = tx_arvif->vdev_id;
9715 	} else if (tx_arvif == arvif) {
9716 		*flags = WMI_VDEV_MBSSID_FLAGS_TRANSMIT_AP;
9717 	} else {
9718 		return -EINVAL;
9719 	}
9720 
9721 	if (link_conf->ema_ap)
9722 		*flags |= WMI_VDEV_MBSSID_FLAGS_EMA_MODE;
9723 
9724 	return 0;
9725 }
9726 
9727 static int ath12k_mac_setup_vdev_create_arg(struct ath12k_link_vif *arvif,
9728 					    struct ath12k_wmi_vdev_create_arg *arg)
9729 {
9730 	struct ath12k *ar = arvif->ar;
9731 	struct ath12k_pdev *pdev = ar->pdev;
9732 	struct ath12k_vif *ahvif = arvif->ahvif;
9733 	int ret;
9734 
9735 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
9736 
9737 	arg->if_id = arvif->vdev_id;
9738 	arg->type = ahvif->vdev_type;
9739 	arg->subtype = ahvif->vdev_subtype;
9740 	arg->pdev_id = pdev->pdev_id;
9741 
9742 	arg->mbssid_flags = WMI_VDEV_MBSSID_FLAGS_NON_MBSSID_AP;
9743 	arg->mbssid_tx_vdev_id = 0;
9744 	if (!test_bit(WMI_TLV_SERVICE_MBSS_PARAM_IN_VDEV_START_SUPPORT,
9745 		      ar->ab->wmi_ab.svc_map)) {
9746 		ret = ath12k_mac_setup_vdev_params_mbssid(arvif,
9747 							  &arg->mbssid_flags,
9748 							  &arg->mbssid_tx_vdev_id);
9749 		if (ret)
9750 			return ret;
9751 	}
9752 
9753 	if (pdev->cap.supported_bands & WMI_HOST_WLAN_2GHZ_CAP) {
9754 		arg->chains[NL80211_BAND_2GHZ].tx = ar->num_tx_chains;
9755 		arg->chains[NL80211_BAND_2GHZ].rx = ar->num_rx_chains;
9756 	}
9757 	if (pdev->cap.supported_bands & WMI_HOST_WLAN_5GHZ_CAP) {
9758 		arg->chains[NL80211_BAND_5GHZ].tx = ar->num_tx_chains;
9759 		arg->chains[NL80211_BAND_5GHZ].rx = ar->num_rx_chains;
9760 	}
9761 	if (pdev->cap.supported_bands & WMI_HOST_WLAN_5GHZ_CAP &&
9762 	    ar->supports_6ghz) {
9763 		arg->chains[NL80211_BAND_6GHZ].tx = ar->num_tx_chains;
9764 		arg->chains[NL80211_BAND_6GHZ].rx = ar->num_rx_chains;
9765 	}
9766 
9767 	arg->if_stats_id = ath12k_mac_get_vdev_stats_id(arvif);
9768 
9769 	if (ath12k_mac_is_ml_arvif(arvif)) {
9770 		if (hweight16(ahvif->vif->valid_links) > ATH12K_WMI_MLO_MAX_LINKS) {
9771 			ath12k_warn(ar->ab, "too many MLO links during setting up vdev: %d",
9772 				    ahvif->vif->valid_links);
9773 			return -EINVAL;
9774 		}
9775 
9776 		ether_addr_copy(arg->mld_addr, ahvif->vif->addr);
9777 	}
9778 
9779 	return 0;
9780 }
9781 
9782 static void ath12k_mac_update_vif_offload(struct ath12k_link_vif *arvif)
9783 {
9784 	struct ath12k_vif *ahvif = arvif->ahvif;
9785 	struct ieee80211_vif *vif = ath12k_ahvif_to_vif(ahvif);
9786 	struct ath12k *ar = arvif->ar;
9787 	struct ath12k_base *ab = ar->ab;
9788 	u32 param_id, param_value;
9789 	int ret;
9790 
9791 	param_id = WMI_VDEV_PARAM_TX_ENCAP_TYPE;
9792 	if (vif->type != NL80211_IFTYPE_STATION &&
9793 	    vif->type != NL80211_IFTYPE_AP)
9794 		vif->offload_flags &= ~(IEEE80211_OFFLOAD_ENCAP_ENABLED |
9795 					IEEE80211_OFFLOAD_DECAP_ENABLED);
9796 
9797 	if (vif->offload_flags & IEEE80211_OFFLOAD_ENCAP_ENABLED)
9798 		ahvif->tx_encap_type = ATH12K_HW_TXRX_ETHERNET;
9799 	else if (test_bit(ATH12K_FLAG_RAW_MODE, &ab->dev_flags))
9800 		ahvif->tx_encap_type = ATH12K_HW_TXRX_RAW;
9801 	else
9802 		ahvif->tx_encap_type = ATH12K_HW_TXRX_NATIVE_WIFI;
9803 
9804 	ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
9805 					    param_id, ahvif->tx_encap_type);
9806 	if (ret) {
9807 		ath12k_warn(ab, "failed to set vdev %d tx encap mode: %d\n",
9808 			    arvif->vdev_id, ret);
9809 		vif->offload_flags &= ~IEEE80211_OFFLOAD_ENCAP_ENABLED;
9810 	}
9811 
9812 	param_id = WMI_VDEV_PARAM_RX_DECAP_TYPE;
9813 	if (vif->offload_flags & IEEE80211_OFFLOAD_DECAP_ENABLED)
9814 		param_value = ATH12K_HW_TXRX_ETHERNET;
9815 	else if (test_bit(ATH12K_FLAG_RAW_MODE, &ab->dev_flags))
9816 		param_value = ATH12K_HW_TXRX_RAW;
9817 	else
9818 		param_value = ATH12K_HW_TXRX_NATIVE_WIFI;
9819 
9820 	ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
9821 					    param_id, param_value);
9822 	if (ret) {
9823 		ath12k_warn(ab, "failed to set vdev %d rx decap mode: %d\n",
9824 			    arvif->vdev_id, ret);
9825 		vif->offload_flags &= ~IEEE80211_OFFLOAD_DECAP_ENABLED;
9826 	}
9827 }
9828 
9829 static void ath12k_mac_op_update_vif_offload(struct ieee80211_hw *hw,
9830 					     struct ieee80211_vif *vif)
9831 {
9832 	struct ath12k_vif *ahvif = ath12k_vif_to_ahvif(vif);
9833 	struct ath12k_link_vif *arvif;
9834 	unsigned long links;
9835 	int link_id;
9836 
9837 	lockdep_assert_wiphy(hw->wiphy);
9838 
9839 	if (vif->valid_links) {
9840 		links = vif->valid_links;
9841 		for_each_set_bit(link_id, &links, IEEE80211_MLD_MAX_NUM_LINKS) {
9842 			arvif = wiphy_dereference(hw->wiphy, ahvif->link[link_id]);
9843 			if (!(arvif && arvif->ar))
9844 				continue;
9845 
9846 			ath12k_mac_update_vif_offload(arvif);
9847 		}
9848 
9849 		return;
9850 	}
9851 
9852 	ath12k_mac_update_vif_offload(&ahvif->deflink);
9853 }
9854 
9855 static bool ath12k_mac_vif_ap_active_any(struct ath12k_base *ab)
9856 {
9857 	struct ath12k *ar;
9858 	struct ath12k_pdev *pdev;
9859 	struct ath12k_link_vif *arvif;
9860 	int i;
9861 
9862 	for (i = 0; i < ab->num_radios; i++) {
9863 		pdev = &ab->pdevs[i];
9864 		ar = pdev->ar;
9865 		list_for_each_entry(arvif, &ar->arvifs, list) {
9866 			if (arvif->is_up &&
9867 			    arvif->ahvif->vdev_type == WMI_VDEV_TYPE_AP)
9868 				return true;
9869 		}
9870 	}
9871 	return false;
9872 }
9873 
9874 void ath12k_mac_11d_scan_start(struct ath12k *ar, u32 vdev_id)
9875 {
9876 	struct wmi_11d_scan_start_arg arg;
9877 	int ret;
9878 
9879 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
9880 
9881 	if (ar->regdom_set_by_user)
9882 		goto fin;
9883 
9884 	if (ar->vdev_id_11d_scan != ATH12K_11D_INVALID_VDEV_ID)
9885 		goto fin;
9886 
9887 	if (!test_bit(WMI_TLV_SERVICE_11D_OFFLOAD, ar->ab->wmi_ab.svc_map))
9888 		goto fin;
9889 
9890 	if (ath12k_mac_vif_ap_active_any(ar->ab))
9891 		goto fin;
9892 
9893 	arg.vdev_id = vdev_id;
9894 	arg.start_interval_msec = 0;
9895 	arg.scan_period_msec = ATH12K_SCAN_11D_INTERVAL;
9896 
9897 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
9898 		   "mac start 11d scan for vdev %d\n", vdev_id);
9899 
9900 	ret = ath12k_wmi_send_11d_scan_start_cmd(ar, &arg);
9901 	if (ret) {
9902 		ath12k_warn(ar->ab, "failed to start 11d scan vdev %d ret: %d\n",
9903 			    vdev_id, ret);
9904 	} else {
9905 		ar->vdev_id_11d_scan = vdev_id;
9906 		if (ar->state_11d == ATH12K_11D_PREPARING)
9907 			ar->state_11d = ATH12K_11D_RUNNING;
9908 	}
9909 
9910 fin:
9911 	if (ar->state_11d == ATH12K_11D_PREPARING) {
9912 		ar->state_11d = ATH12K_11D_IDLE;
9913 		complete(&ar->completed_11d_scan);
9914 	}
9915 }
9916 
9917 void ath12k_mac_11d_scan_stop(struct ath12k *ar)
9918 {
9919 	int ret;
9920 	u32 vdev_id;
9921 
9922 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
9923 
9924 	if (!test_bit(WMI_TLV_SERVICE_11D_OFFLOAD, ar->ab->wmi_ab.svc_map))
9925 		return;
9926 
9927 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac stop 11d for vdev %d\n",
9928 		   ar->vdev_id_11d_scan);
9929 
9930 	if (ar->state_11d == ATH12K_11D_PREPARING) {
9931 		ar->state_11d = ATH12K_11D_IDLE;
9932 		complete(&ar->completed_11d_scan);
9933 	}
9934 
9935 	if (ar->vdev_id_11d_scan != ATH12K_11D_INVALID_VDEV_ID) {
9936 		vdev_id = ar->vdev_id_11d_scan;
9937 
9938 		ret = ath12k_wmi_send_11d_scan_stop_cmd(ar, vdev_id);
9939 		if (ret) {
9940 			ath12k_warn(ar->ab,
9941 				    "failed to stopt 11d scan vdev %d ret: %d\n",
9942 				    vdev_id, ret);
9943 		} else {
9944 			ar->vdev_id_11d_scan = ATH12K_11D_INVALID_VDEV_ID;
9945 			ar->state_11d = ATH12K_11D_IDLE;
9946 			complete(&ar->completed_11d_scan);
9947 		}
9948 	}
9949 }
9950 
9951 void ath12k_mac_11d_scan_stop_all(struct ath12k_base *ab)
9952 {
9953 	struct ath12k *ar;
9954 	struct ath12k_pdev *pdev;
9955 	int i;
9956 
9957 	ath12k_dbg(ab, ATH12K_DBG_MAC, "mac stop soc 11d scan\n");
9958 
9959 	for (i = 0; i < ab->num_radios; i++) {
9960 		pdev = &ab->pdevs[i];
9961 		ar = pdev->ar;
9962 
9963 		ath12k_mac_11d_scan_stop(ar);
9964 	}
9965 }
9966 
9967 static void ath12k_mac_determine_vdev_type(struct ieee80211_vif *vif,
9968 					   struct ath12k_vif *ahvif)
9969 {
9970 	ahvif->vdev_subtype = WMI_VDEV_SUBTYPE_NONE;
9971 
9972 	switch (vif->type) {
9973 	case NL80211_IFTYPE_UNSPECIFIED:
9974 	case NL80211_IFTYPE_STATION:
9975 		ahvif->vdev_type = WMI_VDEV_TYPE_STA;
9976 
9977 		if (vif->p2p)
9978 			ahvif->vdev_subtype = WMI_VDEV_SUBTYPE_P2P_CLIENT;
9979 
9980 		break;
9981 	case NL80211_IFTYPE_MESH_POINT:
9982 		ahvif->vdev_subtype = WMI_VDEV_SUBTYPE_MESH_11S;
9983 		fallthrough;
9984 	case NL80211_IFTYPE_AP:
9985 		ahvif->vdev_type = WMI_VDEV_TYPE_AP;
9986 
9987 		if (vif->p2p)
9988 			ahvif->vdev_subtype = WMI_VDEV_SUBTYPE_P2P_GO;
9989 
9990 		break;
9991 	case NL80211_IFTYPE_MONITOR:
9992 		ahvif->vdev_type = WMI_VDEV_TYPE_MONITOR;
9993 		break;
9994 	case NL80211_IFTYPE_P2P_DEVICE:
9995 		ahvif->vdev_type = WMI_VDEV_TYPE_STA;
9996 		ahvif->vdev_subtype = WMI_VDEV_SUBTYPE_P2P_DEVICE;
9997 		break;
9998 	default:
9999 		WARN_ON(1);
10000 		break;
10001 	}
10002 }
10003 
10004 int ath12k_mac_vdev_create(struct ath12k *ar, struct ath12k_link_vif *arvif)
10005 {
10006 	struct ath12k_hw *ah = ar->ah;
10007 	struct ath12k_base *ab = ar->ab;
10008 	struct ieee80211_hw *hw = ah->hw;
10009 	struct ath12k_vif *ahvif = arvif->ahvif;
10010 	struct ieee80211_vif *vif = ath12k_ahvif_to_vif(ahvif);
10011 	struct ath12k_wmi_vdev_create_arg vdev_arg = {};
10012 	struct ath12k_wmi_peer_create_arg peer_param = {};
10013 	struct ieee80211_bss_conf *link_conf = NULL;
10014 	u32 param_id, param_value;
10015 	u16 nss;
10016 	int i;
10017 	int ret, vdev_id;
10018 	u8 link_id;
10019 
10020 	lockdep_assert_wiphy(hw->wiphy);
10021 
10022 	/* In NO_VIRTUAL_MONITOR, its necessary to restrict only one monitor
10023 	 * interface in each radio
10024 	 */
10025 	if (vif->type == NL80211_IFTYPE_MONITOR && ar->monitor_vdev_created)
10026 		return -EINVAL;
10027 
10028 	if (ar->num_created_vdevs >= TARGET_NUM_VDEVS(ab)) {
10029 		ath12k_warn(ab, "failed to create vdev, reached max vdev limit %d\n",
10030 			    TARGET_NUM_VDEVS(ab));
10031 		return -ENOSPC;
10032 	}
10033 
10034 	link_id = arvif->link_id;
10035 
10036 	if (link_id < IEEE80211_MLD_MAX_NUM_LINKS) {
10037 		link_conf = wiphy_dereference(hw->wiphy, vif->link_conf[link_id]);
10038 		if (!link_conf) {
10039 			ath12k_warn(ar->ab, "unable to access bss link conf in vdev create for vif %pM link %u\n",
10040 				    vif->addr, arvif->link_id);
10041 			return -ENOLINK;
10042 		}
10043 	}
10044 
10045 	if (link_conf)
10046 		memcpy(arvif->bssid, link_conf->addr, ETH_ALEN);
10047 	else
10048 		memcpy(arvif->bssid, vif->addr, ETH_ALEN);
10049 
10050 	arvif->ar = ar;
10051 	vdev_id = __ffs64(ab->free_vdev_map);
10052 	arvif->vdev_id = vdev_id;
10053 	if (vif->type == NL80211_IFTYPE_MONITOR)
10054 		ar->monitor_vdev_id = vdev_id;
10055 
10056 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac vdev create id %d type %d subtype %d map %llx\n",
10057 		   arvif->vdev_id, ahvif->vdev_type, ahvif->vdev_subtype,
10058 		   ab->free_vdev_map);
10059 
10060 	vif->cab_queue = arvif->vdev_id % (ATH12K_HW_MAX_QUEUES - 1);
10061 	for (i = 0; i < ARRAY_SIZE(vif->hw_queue); i++)
10062 		vif->hw_queue[i] = i % (ATH12K_HW_MAX_QUEUES - 1);
10063 
10064 	ret = ath12k_mac_setup_vdev_create_arg(arvif, &vdev_arg);
10065 	if (ret) {
10066 		ath12k_warn(ab, "failed to create vdev parameters %d: %d\n",
10067 			    arvif->vdev_id, ret);
10068 		goto err;
10069 	}
10070 
10071 	ret = ath12k_wmi_vdev_create(ar, arvif->bssid, &vdev_arg);
10072 	if (ret) {
10073 		ath12k_warn(ab, "failed to create WMI vdev %d: %d\n",
10074 			    arvif->vdev_id, ret);
10075 		return ret;
10076 	}
10077 
10078 	ar->num_created_vdevs++;
10079 	arvif->is_created = true;
10080 	ath12k_dbg(ab, ATH12K_DBG_MAC, "vdev %pM created, vdev_id %d\n",
10081 		   vif->addr, arvif->vdev_id);
10082 	ar->allocated_vdev_map |= 1LL << arvif->vdev_id;
10083 	ab->free_vdev_map &= ~(1LL << arvif->vdev_id);
10084 
10085 	spin_lock_bh(&ar->data_lock);
10086 	list_add(&arvif->list, &ar->arvifs);
10087 	spin_unlock_bh(&ar->data_lock);
10088 
10089 	ath12k_mac_update_vif_offload(arvif);
10090 
10091 	nss = hweight32(ar->cfg_tx_chainmask) ? : 1;
10092 	ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
10093 					    WMI_VDEV_PARAM_NSS, nss);
10094 	if (ret) {
10095 		ath12k_warn(ab, "failed to set vdev %d chainmask 0x%x, nss %d :%d\n",
10096 			    arvif->vdev_id, ar->cfg_tx_chainmask, nss, ret);
10097 		goto err_vdev_del;
10098 	}
10099 
10100 	switch (ahvif->vdev_type) {
10101 	case WMI_VDEV_TYPE_AP:
10102 		peer_param.vdev_id = arvif->vdev_id;
10103 		peer_param.peer_addr = arvif->bssid;
10104 		peer_param.peer_type = WMI_PEER_TYPE_DEFAULT;
10105 		ret = ath12k_peer_create(ar, arvif, NULL, &peer_param);
10106 		if (ret) {
10107 			ath12k_warn(ab, "failed to vdev %d create peer for AP: %d\n",
10108 				    arvif->vdev_id, ret);
10109 			goto err_vdev_del;
10110 		}
10111 
10112 		ret = ath12k_mac_set_kickout(arvif);
10113 		if (ret) {
10114 			ath12k_warn(ar->ab, "failed to set vdev %i kickout parameters: %d\n",
10115 				    arvif->vdev_id, ret);
10116 			goto err_peer_del;
10117 		}
10118 		ath12k_mac_11d_scan_stop_all(ar->ab);
10119 		break;
10120 	case WMI_VDEV_TYPE_STA:
10121 		param_id = WMI_STA_PS_PARAM_RX_WAKE_POLICY;
10122 		param_value = WMI_STA_PS_RX_WAKE_POLICY_WAKE;
10123 		ret = ath12k_wmi_set_sta_ps_param(ar, arvif->vdev_id,
10124 						  param_id, param_value);
10125 		if (ret) {
10126 			ath12k_warn(ar->ab, "failed to set vdev %d RX wake policy: %d\n",
10127 				    arvif->vdev_id, ret);
10128 			goto err_peer_del;
10129 		}
10130 
10131 		param_id = WMI_STA_PS_PARAM_TX_WAKE_THRESHOLD;
10132 		param_value = WMI_STA_PS_TX_WAKE_THRESHOLD_ALWAYS;
10133 		ret = ath12k_wmi_set_sta_ps_param(ar, arvif->vdev_id,
10134 						  param_id, param_value);
10135 		if (ret) {
10136 			ath12k_warn(ar->ab, "failed to set vdev %d TX wake threshold: %d\n",
10137 				    arvif->vdev_id, ret);
10138 			goto err_peer_del;
10139 		}
10140 
10141 		param_id = WMI_STA_PS_PARAM_PSPOLL_COUNT;
10142 		param_value = WMI_STA_PS_PSPOLL_COUNT_NO_MAX;
10143 		ret = ath12k_wmi_set_sta_ps_param(ar, arvif->vdev_id,
10144 						  param_id, param_value);
10145 		if (ret) {
10146 			ath12k_warn(ar->ab, "failed to set vdev %d pspoll count: %d\n",
10147 				    arvif->vdev_id, ret);
10148 			goto err_peer_del;
10149 		}
10150 
10151 		ret = ath12k_wmi_pdev_set_ps_mode(ar, arvif->vdev_id, false);
10152 		if (ret) {
10153 			ath12k_warn(ar->ab, "failed to disable vdev %d ps mode: %d\n",
10154 				    arvif->vdev_id, ret);
10155 			goto err_peer_del;
10156 		}
10157 
10158 		if (test_bit(WMI_TLV_SERVICE_11D_OFFLOAD, ab->wmi_ab.svc_map) &&
10159 		    ahvif->vdev_type == WMI_VDEV_TYPE_STA &&
10160 		    ahvif->vdev_subtype == WMI_VDEV_SUBTYPE_NONE) {
10161 			reinit_completion(&ar->completed_11d_scan);
10162 			ar->state_11d = ATH12K_11D_PREPARING;
10163 		}
10164 		break;
10165 	case WMI_VDEV_TYPE_MONITOR:
10166 		ar->monitor_vdev_created = true;
10167 		break;
10168 	default:
10169 		break;
10170 	}
10171 
10172 	if (link_conf)
10173 		arvif->txpower = link_conf->txpower;
10174 	else
10175 		arvif->txpower = NL80211_TX_POWER_AUTOMATIC;
10176 
10177 	ret = ath12k_mac_txpower_recalc(ar);
10178 	if (ret)
10179 		goto err_peer_del;
10180 
10181 	param_id = WMI_VDEV_PARAM_RTS_THRESHOLD;
10182 	param_value = hw->wiphy->rts_threshold;
10183 	ar->rts_threshold = param_value;
10184 	ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
10185 					    param_id, param_value);
10186 	if (ret) {
10187 		ath12k_warn(ar->ab, "failed to set rts threshold for vdev %d: %d\n",
10188 			    arvif->vdev_id, ret);
10189 	}
10190 
10191 	ath12k_dp_vdev_tx_attach(ar, arvif);
10192 
10193 	return ret;
10194 
10195 err_peer_del:
10196 	if (ahvif->vdev_type == WMI_VDEV_TYPE_AP) {
10197 		reinit_completion(&ar->peer_delete_done);
10198 
10199 		ret = ath12k_wmi_send_peer_delete_cmd(ar, arvif->bssid,
10200 						      arvif->vdev_id);
10201 		if (ret) {
10202 			ath12k_warn(ar->ab, "failed to delete peer vdev_id %d addr %pM\n",
10203 				    arvif->vdev_id, arvif->bssid);
10204 			goto err;
10205 		}
10206 
10207 		ret = ath12k_wait_for_peer_delete_done(ar, arvif->vdev_id,
10208 						       arvif->bssid);
10209 		if (ret)
10210 			goto err_vdev_del;
10211 
10212 		ar->num_peers--;
10213 	}
10214 
10215 err_vdev_del:
10216 	if (ahvif->vdev_type == WMI_VDEV_TYPE_MONITOR) {
10217 		ar->monitor_vdev_id = -1;
10218 		ar->monitor_vdev_created = false;
10219 	}
10220 
10221 	ath12k_wmi_vdev_delete(ar, arvif->vdev_id);
10222 	ar->num_created_vdevs--;
10223 	arvif->is_created = false;
10224 	arvif->ar = NULL;
10225 	ar->allocated_vdev_map &= ~(1LL << arvif->vdev_id);
10226 	ab->free_vdev_map |= 1LL << arvif->vdev_id;
10227 	ab->free_vdev_stats_id_map &= ~(1LL << arvif->vdev_stats_id);
10228 	spin_lock_bh(&ar->data_lock);
10229 	list_del(&arvif->list);
10230 	spin_unlock_bh(&ar->data_lock);
10231 
10232 err:
10233 	arvif->ar = NULL;
10234 	return ret;
10235 }
10236 
10237 static void ath12k_mac_vif_flush_key_cache(struct ath12k_link_vif *arvif)
10238 {
10239 	struct ath12k_key_conf *key_conf, *tmp;
10240 	struct ath12k_vif *ahvif = arvif->ahvif;
10241 	struct ath12k_hw *ah = ahvif->ah;
10242 	struct ath12k_sta *ahsta;
10243 	struct ath12k_link_sta *arsta;
10244 	struct ath12k_vif_cache *cache = ahvif->cache[arvif->link_id];
10245 	int ret;
10246 
10247 	lockdep_assert_wiphy(ah->hw->wiphy);
10248 
10249 	list_for_each_entry_safe(key_conf, tmp, &cache->key_conf.list, list) {
10250 		arsta = NULL;
10251 		if (key_conf->sta) {
10252 			ahsta = ath12k_sta_to_ahsta(key_conf->sta);
10253 			arsta = wiphy_dereference(ah->hw->wiphy,
10254 						  ahsta->link[arvif->link_id]);
10255 			if (!arsta)
10256 				goto free_cache;
10257 		}
10258 
10259 		ret = ath12k_mac_set_key(arvif->ar, key_conf->cmd,
10260 					 arvif, arsta,
10261 					 key_conf->key);
10262 		if (ret)
10263 			ath12k_warn(arvif->ar->ab, "unable to apply set key param to vdev %d ret %d\n",
10264 				    arvif->vdev_id, ret);
10265 free_cache:
10266 		list_del(&key_conf->list);
10267 		kfree(key_conf);
10268 	}
10269 }
10270 
10271 static void ath12k_mac_vif_cache_flush(struct ath12k *ar, struct ath12k_link_vif *arvif)
10272 {
10273 	struct ath12k_vif *ahvif = arvif->ahvif;
10274 	struct ieee80211_vif *vif = ath12k_ahvif_to_vif(ahvif);
10275 	struct ath12k_vif_cache *cache = ahvif->cache[arvif->link_id];
10276 	struct ath12k_base *ab = ar->ab;
10277 	struct ieee80211_bss_conf *link_conf;
10278 
10279 	int ret;
10280 
10281 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
10282 
10283 	if (!cache)
10284 		return;
10285 
10286 	if (cache->tx_conf.changed) {
10287 		ret = ath12k_mac_conf_tx(arvif, cache->tx_conf.ac,
10288 					 &cache->tx_conf.tx_queue_params);
10289 		if (ret)
10290 			ath12k_warn(ab,
10291 				    "unable to apply tx config parameters to vdev %d\n",
10292 				    ret);
10293 	}
10294 
10295 	if (cache->bss_conf_changed) {
10296 		link_conf = ath12k_mac_get_link_bss_conf(arvif);
10297 		if (!link_conf) {
10298 			ath12k_warn(ar->ab, "unable to access bss link conf in cache flush for vif %pM link %u\n",
10299 				    vif->addr, arvif->link_id);
10300 			return;
10301 		}
10302 		ath12k_mac_bss_info_changed(ar, arvif, link_conf,
10303 					    cache->bss_conf_changed);
10304 	}
10305 
10306 	if (!list_empty(&cache->key_conf.list))
10307 		ath12k_mac_vif_flush_key_cache(arvif);
10308 
10309 	ath12k_ahvif_put_link_cache(ahvif, arvif->link_id);
10310 }
10311 
10312 static struct ath12k *ath12k_mac_assign_vif_to_vdev(struct ieee80211_hw *hw,
10313 						    struct ath12k_link_vif *arvif,
10314 						    struct ieee80211_chanctx_conf *ctx)
10315 {
10316 	struct ath12k_vif *ahvif = arvif->ahvif;
10317 	struct ieee80211_vif *vif = ath12k_ahvif_to_vif(ahvif);
10318 	struct ath12k_link_vif *scan_arvif;
10319 	struct ath12k_hw *ah = hw->priv;
10320 	struct ath12k *ar;
10321 	struct ath12k_base *ab;
10322 	u8 link_id = arvif->link_id, scan_link_id;
10323 	unsigned long scan_link_map;
10324 	int ret;
10325 
10326 	lockdep_assert_wiphy(hw->wiphy);
10327 
10328 	if (ah->num_radio == 1)
10329 		ar = ah->radio;
10330 	else if (ctx)
10331 		ar = ath12k_get_ar_by_ctx(hw, ctx);
10332 	else
10333 		return NULL;
10334 
10335 	if (!ar)
10336 		return NULL;
10337 
10338 	/* cleanup the scan vdev if we are done scan on that ar
10339 	 * and now we want to create for actual usage.
10340 	 */
10341 	if (ieee80211_vif_is_mld(vif)) {
10342 		scan_link_map = ahvif->links_map & ATH12K_SCAN_LINKS_MASK;
10343 		for_each_set_bit(scan_link_id, &scan_link_map, ATH12K_NUM_MAX_LINKS) {
10344 			scan_arvif = wiphy_dereference(hw->wiphy,
10345 						       ahvif->link[scan_link_id]);
10346 			if (scan_arvif && scan_arvif->ar == ar) {
10347 				ar->scan.arvif = NULL;
10348 				ath12k_mac_remove_link_interface(hw, scan_arvif);
10349 				ath12k_mac_unassign_link_vif(scan_arvif);
10350 				break;
10351 			}
10352 		}
10353 	}
10354 
10355 	if (arvif->ar) {
10356 		/* This is not expected really */
10357 		if (WARN_ON(!arvif->is_created)) {
10358 			arvif->ar = NULL;
10359 			return NULL;
10360 		}
10361 
10362 		if (ah->num_radio == 1)
10363 			return arvif->ar;
10364 
10365 		/* This can happen as scan vdev gets created during multiple scans
10366 		 * across different radios before a vdev is brought up in
10367 		 * a certain radio.
10368 		 */
10369 		if (ar != arvif->ar) {
10370 			if (WARN_ON(arvif->is_started))
10371 				return NULL;
10372 
10373 			ath12k_mac_remove_link_interface(hw, arvif);
10374 			ath12k_mac_unassign_link_vif(arvif);
10375 		}
10376 	}
10377 
10378 	ab = ar->ab;
10379 
10380 	/* Assign arvif again here since previous radio switch block
10381 	 * would've unassigned and cleared it.
10382 	 */
10383 	arvif = ath12k_mac_assign_link_vif(ah, vif, link_id);
10384 	if (vif->type == NL80211_IFTYPE_AP &&
10385 	    ar->num_peers > (ar->max_num_peers - 1)) {
10386 		ath12k_warn(ab, "failed to create vdev due to insufficient peer entry resource in firmware\n");
10387 		goto unlock;
10388 	}
10389 
10390 	if (arvif->is_created)
10391 		goto flush;
10392 
10393 	ret = ath12k_mac_vdev_create(ar, arvif);
10394 	if (ret) {
10395 		ath12k_warn(ab, "failed to create vdev %pM ret %d", vif->addr, ret);
10396 		goto unlock;
10397 	}
10398 
10399 flush:
10400 	/* If the vdev is created during channel assign and not during
10401 	 * add_interface(), Apply any parameters for the vdev which were received
10402 	 * after add_interface, corresponding to this vif.
10403 	 */
10404 	ath12k_mac_vif_cache_flush(ar, arvif);
10405 unlock:
10406 	return arvif->ar;
10407 }
10408 
10409 static int ath12k_mac_op_add_interface(struct ieee80211_hw *hw,
10410 				       struct ieee80211_vif *vif)
10411 {
10412 	struct ath12k_hw *ah = ath12k_hw_to_ah(hw);
10413 	struct ath12k_vif *ahvif = ath12k_vif_to_ahvif(vif);
10414 	struct ath12k_reg_info *reg_info;
10415 	struct ath12k_link_vif *arvif;
10416 	struct ath12k_base *ab;
10417 	struct ath12k *ar;
10418 	int i;
10419 
10420 	lockdep_assert_wiphy(hw->wiphy);
10421 
10422 	memset(ahvif, 0, sizeof(*ahvif));
10423 
10424 	ahvif->ah = ah;
10425 	ahvif->vif = vif;
10426 	arvif = &ahvif->deflink;
10427 
10428 	ath12k_mac_init_arvif(ahvif, arvif, -1);
10429 
10430 	/* Allocate Default Queue now and reassign during actual vdev create */
10431 	vif->cab_queue = ATH12K_HW_DEFAULT_QUEUE;
10432 	for (i = 0; i < ARRAY_SIZE(vif->hw_queue); i++)
10433 		vif->hw_queue[i] = ATH12K_HW_DEFAULT_QUEUE;
10434 
10435 	vif->driver_flags |= IEEE80211_VIF_SUPPORTS_UAPSD;
10436 
10437 	ath12k_mac_determine_vdev_type(vif, ahvif);
10438 
10439 	for_each_ar(ah, ar, i) {
10440 		if (!ath12k_wmi_supports_6ghz_cc_ext(ar))
10441 			continue;
10442 
10443 		ab = ar->ab;
10444 		reg_info = ab->reg_info[ar->pdev_idx];
10445 		ath12k_dbg(ab, ATH12K_DBG_MAC, "interface added to change reg rules\n");
10446 		ah->regd_updated = false;
10447 		ath12k_reg_handle_chan_list(ab, reg_info, ahvif->vdev_type,
10448 					    IEEE80211_REG_UNSET_AP);
10449 		break;
10450 	}
10451 
10452 	/* Defer vdev creation until assign_chanctx or hw_scan is initiated as driver
10453 	 * will not know if this interface is an ML vif at this point.
10454 	 */
10455 	return 0;
10456 }
10457 
10458 static void ath12k_mac_vif_unref(struct ath12k_dp *dp, struct ieee80211_vif *vif)
10459 {
10460 	struct ath12k_tx_desc_info *tx_desc_info;
10461 	struct ath12k_skb_cb *skb_cb;
10462 	struct sk_buff *skb;
10463 	int i;
10464 
10465 	for (i = 0; i < ATH12K_HW_MAX_QUEUES; i++) {
10466 		spin_lock_bh(&dp->tx_desc_lock[i]);
10467 
10468 		list_for_each_entry(tx_desc_info, &dp->tx_desc_used_list[i],
10469 				    list) {
10470 			skb = tx_desc_info->skb;
10471 			if (!skb)
10472 				continue;
10473 
10474 			skb_cb = ATH12K_SKB_CB(skb);
10475 			if (skb_cb->vif == vif)
10476 				skb_cb->vif = NULL;
10477 		}
10478 
10479 		spin_unlock_bh(&dp->tx_desc_lock[i]);
10480 	}
10481 }
10482 
10483 static int ath12k_mac_vdev_delete(struct ath12k *ar, struct ath12k_link_vif *arvif)
10484 {
10485 	struct ath12k_vif *ahvif = arvif->ahvif;
10486 	struct ieee80211_vif *vif = ath12k_ahvif_to_vif(ahvif);
10487 	struct ath12k_base *ab = ar->ab;
10488 	unsigned long time_left;
10489 	int ret;
10490 
10491 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
10492 
10493 	reinit_completion(&ar->vdev_delete_done);
10494 
10495 	ret = ath12k_wmi_vdev_delete(ar, arvif->vdev_id);
10496 	if (ret) {
10497 		ath12k_warn(ab, "failed to delete WMI vdev %d: %d\n",
10498 			    arvif->vdev_id, ret);
10499 		goto err_vdev_del;
10500 	}
10501 
10502 	time_left = wait_for_completion_timeout(&ar->vdev_delete_done,
10503 						ATH12K_VDEV_DELETE_TIMEOUT_HZ);
10504 	if (time_left == 0) {
10505 		ath12k_warn(ab, "Timeout in receiving vdev delete response\n");
10506 		goto err_vdev_del;
10507 	}
10508 
10509 	ab->free_vdev_map |= 1LL << arvif->vdev_id;
10510 	ar->allocated_vdev_map &= ~(1LL << arvif->vdev_id);
10511 	ar->num_created_vdevs--;
10512 
10513 	if (ahvif->vdev_type == WMI_VDEV_TYPE_MONITOR) {
10514 		ar->monitor_vdev_id = -1;
10515 		ar->monitor_vdev_created = false;
10516 	}
10517 
10518 	ath12k_dbg(ab, ATH12K_DBG_MAC, "vdev %pM deleted, vdev_id %d\n",
10519 		   vif->addr, arvif->vdev_id);
10520 
10521 err_vdev_del:
10522 	spin_lock_bh(&ar->data_lock);
10523 	list_del(&arvif->list);
10524 	spin_unlock_bh(&ar->data_lock);
10525 
10526 	ath12k_peer_cleanup(ar, arvif->vdev_id);
10527 	ath12k_ahvif_put_link_cache(ahvif, arvif->link_id);
10528 
10529 	idr_for_each(&ar->txmgmt_idr,
10530 		     ath12k_mac_vif_txmgmt_idr_remove, vif);
10531 
10532 	ath12k_mac_vif_unref(&ab->dp, vif);
10533 	ath12k_dp_tx_put_bank_profile(&ab->dp, arvif->bank_id);
10534 
10535 	/* Recalc txpower for remaining vdev */
10536 	ath12k_mac_txpower_recalc(ar);
10537 
10538 	/* TODO: recal traffic pause state based on the available vdevs */
10539 	arvif->is_created = false;
10540 	arvif->ar = NULL;
10541 
10542 	return ret;
10543 }
10544 
10545 static void ath12k_mac_op_remove_interface(struct ieee80211_hw *hw,
10546 					   struct ieee80211_vif *vif)
10547 {
10548 	struct ath12k_vif *ahvif = ath12k_vif_to_ahvif(vif);
10549 	struct ath12k_link_vif *arvif;
10550 	struct ath12k *ar;
10551 	u8 link_id;
10552 
10553 	lockdep_assert_wiphy(hw->wiphy);
10554 
10555 	for (link_id = 0; link_id < ATH12K_NUM_MAX_LINKS; link_id++) {
10556 		/* if we cached some config but never received assign chanctx,
10557 		 * free the allocated cache.
10558 		 */
10559 		ath12k_ahvif_put_link_cache(ahvif, link_id);
10560 		arvif = wiphy_dereference(hw->wiphy, ahvif->link[link_id]);
10561 		if (!arvif || !arvif->is_created)
10562 			continue;
10563 
10564 		ar = arvif->ar;
10565 
10566 		/* Scan abortion is in progress since before this, cancel_hw_scan()
10567 		 * is expected to be executed. Since link is anyways going to be removed
10568 		 * now, just cancel the worker and send the scan aborted to user space
10569 		 */
10570 		if (ar->scan.arvif == arvif) {
10571 			wiphy_work_cancel(hw->wiphy, &ar->scan.vdev_clean_wk);
10572 
10573 			spin_lock_bh(&ar->data_lock);
10574 			ar->scan.arvif = NULL;
10575 			if (!ar->scan.is_roc) {
10576 				struct cfg80211_scan_info info = {
10577 					.aborted = true,
10578 				};
10579 
10580 				ath12k_mac_scan_send_complete(ar, &info);
10581 			}
10582 
10583 			ar->scan.state = ATH12K_SCAN_IDLE;
10584 			ar->scan_channel = NULL;
10585 			ar->scan.roc_freq = 0;
10586 			spin_unlock_bh(&ar->data_lock);
10587 		}
10588 
10589 		ath12k_mac_remove_link_interface(hw, arvif);
10590 		ath12k_mac_unassign_link_vif(arvif);
10591 	}
10592 }
10593 
10594 /* FIXME: Has to be verified. */
10595 #define SUPPORTED_FILTERS			\
10596 	(FIF_ALLMULTI |				\
10597 	FIF_CONTROL |				\
10598 	FIF_PSPOLL |				\
10599 	FIF_OTHER_BSS |				\
10600 	FIF_BCN_PRBRESP_PROMISC |		\
10601 	FIF_PROBE_REQ |				\
10602 	FIF_FCSFAIL)
10603 
10604 static void ath12k_mac_op_configure_filter(struct ieee80211_hw *hw,
10605 					   unsigned int changed_flags,
10606 					   unsigned int *total_flags,
10607 					   u64 multicast)
10608 {
10609 	struct ath12k_hw *ah = ath12k_hw_to_ah(hw);
10610 	struct ath12k *ar;
10611 
10612 	lockdep_assert_wiphy(hw->wiphy);
10613 
10614 	ar = ath12k_ah_to_ar(ah, 0);
10615 
10616 	*total_flags &= SUPPORTED_FILTERS;
10617 	ar->filter_flags = *total_flags;
10618 }
10619 
10620 static int ath12k_mac_op_get_antenna(struct ieee80211_hw *hw, int radio_idx,
10621 				     u32 *tx_ant, u32 *rx_ant)
10622 {
10623 	struct ath12k_hw *ah = ath12k_hw_to_ah(hw);
10624 	int antennas_rx = 0, antennas_tx = 0;
10625 	struct ath12k *ar;
10626 	int i;
10627 
10628 	lockdep_assert_wiphy(hw->wiphy);
10629 
10630 	for_each_ar(ah, ar, i) {
10631 		antennas_rx = max_t(u32, antennas_rx, ar->cfg_rx_chainmask);
10632 		antennas_tx = max_t(u32, antennas_tx, ar->cfg_tx_chainmask);
10633 	}
10634 
10635 	*tx_ant = antennas_tx;
10636 	*rx_ant = antennas_rx;
10637 
10638 	return 0;
10639 }
10640 
10641 static int ath12k_mac_op_set_antenna(struct ieee80211_hw *hw, int radio_idx,
10642 				     u32 tx_ant, u32 rx_ant)
10643 {
10644 	struct ath12k_hw *ah = ath12k_hw_to_ah(hw);
10645 	struct ath12k *ar;
10646 	int ret = 0;
10647 	int i;
10648 
10649 	lockdep_assert_wiphy(hw->wiphy);
10650 
10651 	for_each_ar(ah, ar, i) {
10652 		ret = __ath12k_set_antenna(ar, tx_ant, rx_ant);
10653 		if (ret)
10654 			break;
10655 	}
10656 
10657 	return ret;
10658 }
10659 
10660 static int ath12k_mac_ampdu_action(struct ieee80211_hw *hw,
10661 				   struct ieee80211_vif *vif,
10662 				   struct ieee80211_ampdu_params *params,
10663 				   u8 link_id)
10664 {
10665 	struct ath12k *ar;
10666 	int ret = -EINVAL;
10667 
10668 	lockdep_assert_wiphy(hw->wiphy);
10669 
10670 	ar = ath12k_get_ar_by_vif(hw, vif, link_id);
10671 	if (!ar)
10672 		return -EINVAL;
10673 
10674 	switch (params->action) {
10675 	case IEEE80211_AMPDU_RX_START:
10676 		ret = ath12k_dp_rx_ampdu_start(ar, params, link_id);
10677 		break;
10678 	case IEEE80211_AMPDU_RX_STOP:
10679 		ret = ath12k_dp_rx_ampdu_stop(ar, params, link_id);
10680 		break;
10681 	case IEEE80211_AMPDU_TX_START:
10682 	case IEEE80211_AMPDU_TX_STOP_CONT:
10683 	case IEEE80211_AMPDU_TX_STOP_FLUSH:
10684 	case IEEE80211_AMPDU_TX_STOP_FLUSH_CONT:
10685 	case IEEE80211_AMPDU_TX_OPERATIONAL:
10686 		/* Tx A-MPDU aggregation offloaded to hw/fw so deny mac80211
10687 		 * Tx aggregation requests.
10688 		 */
10689 		ret = -EOPNOTSUPP;
10690 		break;
10691 	}
10692 
10693 	if (ret)
10694 		ath12k_warn(ar->ab, "unable to perform ampdu action %d for vif %pM link %u ret %d\n",
10695 			    params->action, vif->addr, link_id, ret);
10696 
10697 	return ret;
10698 }
10699 
10700 static int ath12k_mac_op_ampdu_action(struct ieee80211_hw *hw,
10701 				      struct ieee80211_vif *vif,
10702 				      struct ieee80211_ampdu_params *params)
10703 {
10704 	struct ieee80211_sta *sta = params->sta;
10705 	struct ath12k_sta *ahsta = ath12k_sta_to_ahsta(sta);
10706 	unsigned long links_map = ahsta->links_map;
10707 	int ret = -EINVAL;
10708 	u8 link_id;
10709 
10710 	lockdep_assert_wiphy(hw->wiphy);
10711 
10712 	if (WARN_ON(!links_map))
10713 		return ret;
10714 
10715 	for_each_set_bit(link_id, &links_map, IEEE80211_MLD_MAX_NUM_LINKS) {
10716 		ret = ath12k_mac_ampdu_action(hw, vif, params, link_id);
10717 		if (ret)
10718 			return ret;
10719 	}
10720 
10721 	return 0;
10722 }
10723 
10724 static int ath12k_mac_op_add_chanctx(struct ieee80211_hw *hw,
10725 				     struct ieee80211_chanctx_conf *ctx)
10726 {
10727 	struct ath12k *ar;
10728 	struct ath12k_base *ab;
10729 
10730 	lockdep_assert_wiphy(hw->wiphy);
10731 
10732 	ar = ath12k_get_ar_by_ctx(hw, ctx);
10733 	if (!ar)
10734 		return -EINVAL;
10735 
10736 	ab = ar->ab;
10737 
10738 	ath12k_dbg(ab, ATH12K_DBG_MAC,
10739 		   "mac chanctx add freq %u width %d ptr %p\n",
10740 		   ctx->def.chan->center_freq, ctx->def.width, ctx);
10741 
10742 	spin_lock_bh(&ar->data_lock);
10743 	/* TODO: In case of multiple channel context, populate rx_channel from
10744 	 * Rx PPDU desc information.
10745 	 */
10746 	ar->rx_channel = ctx->def.chan;
10747 	spin_unlock_bh(&ar->data_lock);
10748 	ar->chan_tx_pwr = ATH12K_PDEV_TX_POWER_INVALID;
10749 
10750 	return 0;
10751 }
10752 
10753 static void ath12k_mac_op_remove_chanctx(struct ieee80211_hw *hw,
10754 					 struct ieee80211_chanctx_conf *ctx)
10755 {
10756 	struct ath12k *ar;
10757 	struct ath12k_base *ab;
10758 
10759 	lockdep_assert_wiphy(hw->wiphy);
10760 
10761 	ar = ath12k_get_ar_by_ctx(hw, ctx);
10762 	if (!ar)
10763 		return;
10764 
10765 	ab = ar->ab;
10766 
10767 	ath12k_dbg(ab, ATH12K_DBG_MAC,
10768 		   "mac chanctx remove freq %u width %d ptr %p\n",
10769 		   ctx->def.chan->center_freq, ctx->def.width, ctx);
10770 
10771 	spin_lock_bh(&ar->data_lock);
10772 	/* TODO: In case of there is one more channel context left, populate
10773 	 * rx_channel with the channel of that remaining channel context.
10774 	 */
10775 	ar->rx_channel = NULL;
10776 	spin_unlock_bh(&ar->data_lock);
10777 	ar->chan_tx_pwr = ATH12K_PDEV_TX_POWER_INVALID;
10778 }
10779 
10780 static enum wmi_phy_mode
10781 ath12k_mac_check_down_grade_phy_mode(struct ath12k *ar,
10782 				     enum wmi_phy_mode mode,
10783 				     enum nl80211_band band,
10784 				     enum nl80211_iftype type)
10785 {
10786 	struct ieee80211_sta_eht_cap *eht_cap = NULL;
10787 	enum wmi_phy_mode down_mode;
10788 	int n = ar->mac.sbands[band].n_iftype_data;
10789 	int i;
10790 	struct ieee80211_sband_iftype_data *data;
10791 
10792 	if (mode < MODE_11BE_EHT20)
10793 		return mode;
10794 
10795 	data = ar->mac.iftype[band];
10796 	for (i = 0; i < n; i++) {
10797 		if (data[i].types_mask & BIT(type)) {
10798 			eht_cap = &data[i].eht_cap;
10799 			break;
10800 		}
10801 	}
10802 
10803 	if (eht_cap && eht_cap->has_eht)
10804 		return mode;
10805 
10806 	switch (mode) {
10807 	case MODE_11BE_EHT20:
10808 		down_mode = MODE_11AX_HE20;
10809 		break;
10810 	case MODE_11BE_EHT40:
10811 		down_mode = MODE_11AX_HE40;
10812 		break;
10813 	case MODE_11BE_EHT80:
10814 		down_mode = MODE_11AX_HE80;
10815 		break;
10816 	case MODE_11BE_EHT80_80:
10817 		down_mode = MODE_11AX_HE80_80;
10818 		break;
10819 	case MODE_11BE_EHT160:
10820 	case MODE_11BE_EHT160_160:
10821 	case MODE_11BE_EHT320:
10822 		down_mode = MODE_11AX_HE160;
10823 		break;
10824 	case MODE_11BE_EHT20_2G:
10825 		down_mode = MODE_11AX_HE20_2G;
10826 		break;
10827 	case MODE_11BE_EHT40_2G:
10828 		down_mode = MODE_11AX_HE40_2G;
10829 		break;
10830 	default:
10831 		down_mode = mode;
10832 		break;
10833 	}
10834 
10835 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
10836 		   "mac vdev start phymode %s downgrade to %s\n",
10837 		   ath12k_mac_phymode_str(mode),
10838 		   ath12k_mac_phymode_str(down_mode));
10839 
10840 	return down_mode;
10841 }
10842 
10843 static void
10844 ath12k_mac_mlo_get_vdev_args(struct ath12k_link_vif *arvif,
10845 			     struct wmi_ml_arg *ml_arg)
10846 {
10847 	struct ath12k_vif *ahvif = arvif->ahvif;
10848 	struct wmi_ml_partner_info *partner_info;
10849 	struct ieee80211_bss_conf *link_conf;
10850 	struct ath12k_link_vif *arvif_p;
10851 	unsigned long links;
10852 	u8 link_id;
10853 
10854 	lockdep_assert_wiphy(ahvif->ah->hw->wiphy);
10855 
10856 	if (!ath12k_mac_is_ml_arvif(arvif))
10857 		return;
10858 
10859 	if (hweight16(ahvif->vif->valid_links) > ATH12K_WMI_MLO_MAX_LINKS)
10860 		return;
10861 
10862 	ml_arg->enabled = true;
10863 
10864 	/* Driver always add a new link via VDEV START, FW takes
10865 	 * care of internally adding this link to existing
10866 	 * link vdevs which are advertised as partners below
10867 	 */
10868 	ml_arg->link_add = true;
10869 
10870 	ml_arg->assoc_link = arvif->is_sta_assoc_link;
10871 
10872 	partner_info = ml_arg->partner_info;
10873 
10874 	links = ahvif->links_map;
10875 	for_each_set_bit(link_id, &links, IEEE80211_MLD_MAX_NUM_LINKS) {
10876 		arvif_p = wiphy_dereference(ahvif->ah->hw->wiphy, ahvif->link[link_id]);
10877 
10878 		if (WARN_ON(!arvif_p))
10879 			continue;
10880 
10881 		if (arvif == arvif_p)
10882 			continue;
10883 
10884 		if (!arvif_p->is_created)
10885 			continue;
10886 
10887 		link_conf = wiphy_dereference(ahvif->ah->hw->wiphy,
10888 					      ahvif->vif->link_conf[arvif_p->link_id]);
10889 
10890 		if (!link_conf)
10891 			continue;
10892 
10893 		partner_info->vdev_id = arvif_p->vdev_id;
10894 		partner_info->hw_link_id = arvif_p->ar->pdev->hw_link_id;
10895 		ether_addr_copy(partner_info->addr, link_conf->addr);
10896 		ml_arg->num_partner_links++;
10897 		partner_info++;
10898 	}
10899 }
10900 
10901 static int
10902 ath12k_mac_vdev_start_restart(struct ath12k_link_vif *arvif,
10903 			      struct ieee80211_chanctx_conf *ctx,
10904 			      bool restart)
10905 {
10906 	struct ath12k *ar = arvif->ar;
10907 	struct ath12k_base *ab = ar->ab;
10908 	struct wmi_vdev_start_req_arg arg = {};
10909 	const struct cfg80211_chan_def *chandef = &ctx->def;
10910 	struct ieee80211_hw *hw = ath12k_ar_to_hw(ar);
10911 	struct ath12k_vif *ahvif = arvif->ahvif;
10912 	struct ieee80211_bss_conf *link_conf;
10913 	unsigned int dfs_cac_time;
10914 	int ret;
10915 
10916 	lockdep_assert_wiphy(hw->wiphy);
10917 
10918 	link_conf = ath12k_mac_get_link_bss_conf(arvif);
10919 	if (!link_conf) {
10920 		ath12k_warn(ar->ab, "unable to access bss link conf in vdev start for vif %pM link %u\n",
10921 			    ahvif->vif->addr, arvif->link_id);
10922 		return -ENOLINK;
10923 	}
10924 
10925 	reinit_completion(&ar->vdev_setup_done);
10926 
10927 	arg.vdev_id = arvif->vdev_id;
10928 	arg.dtim_period = arvif->dtim_period;
10929 	arg.bcn_intval = arvif->beacon_interval;
10930 	arg.punct_bitmap = ~arvif->punct_bitmap;
10931 
10932 	arg.freq = chandef->chan->center_freq;
10933 	arg.band_center_freq1 = chandef->center_freq1;
10934 	arg.band_center_freq2 = chandef->center_freq2;
10935 	arg.mode = ath12k_phymodes[chandef->chan->band][chandef->width];
10936 
10937 	arg.mode = ath12k_mac_check_down_grade_phy_mode(ar, arg.mode,
10938 							chandef->chan->band,
10939 							ahvif->vif->type);
10940 	arg.min_power = 0;
10941 	arg.max_power = chandef->chan->max_power;
10942 	arg.max_reg_power = chandef->chan->max_reg_power;
10943 	arg.max_antenna_gain = chandef->chan->max_antenna_gain;
10944 
10945 	arg.pref_tx_streams = ar->num_tx_chains;
10946 	arg.pref_rx_streams = ar->num_rx_chains;
10947 
10948 	arg.mbssid_flags = WMI_VDEV_MBSSID_FLAGS_NON_MBSSID_AP;
10949 	arg.mbssid_tx_vdev_id = 0;
10950 	if (test_bit(WMI_TLV_SERVICE_MBSS_PARAM_IN_VDEV_START_SUPPORT,
10951 		     ar->ab->wmi_ab.svc_map)) {
10952 		ret = ath12k_mac_setup_vdev_params_mbssid(arvif,
10953 							  &arg.mbssid_flags,
10954 							  &arg.mbssid_tx_vdev_id);
10955 		if (ret)
10956 			return ret;
10957 	}
10958 
10959 	if (ahvif->vdev_type == WMI_VDEV_TYPE_AP) {
10960 		arg.ssid = ahvif->u.ap.ssid;
10961 		arg.ssid_len = ahvif->u.ap.ssid_len;
10962 		arg.hidden_ssid = ahvif->u.ap.hidden_ssid;
10963 
10964 		/* For now allow DFS for AP mode */
10965 		arg.chan_radar = !!(chandef->chan->flags & IEEE80211_CHAN_RADAR);
10966 
10967 		arg.freq2_radar = ctx->radar_enabled;
10968 
10969 		arg.passive = arg.chan_radar;
10970 
10971 		spin_lock_bh(&ab->base_lock);
10972 		arg.regdomain = ar->ab->dfs_region;
10973 		spin_unlock_bh(&ab->base_lock);
10974 
10975 		/* TODO: Notify if secondary 80Mhz also needs radar detection */
10976 	}
10977 
10978 	arg.passive |= !!(chandef->chan->flags & IEEE80211_CHAN_NO_IR);
10979 
10980 	if (!restart)
10981 		ath12k_mac_mlo_get_vdev_args(arvif, &arg.ml);
10982 
10983 	ath12k_dbg(ab, ATH12K_DBG_MAC,
10984 		   "mac vdev %d start center_freq %d phymode %s punct_bitmap 0x%x\n",
10985 		   arg.vdev_id, arg.freq,
10986 		   ath12k_mac_phymode_str(arg.mode), arg.punct_bitmap);
10987 
10988 	ret = ath12k_wmi_vdev_start(ar, &arg, restart);
10989 	if (ret) {
10990 		ath12k_warn(ar->ab, "failed to %s WMI vdev %i\n",
10991 			    restart ? "restart" : "start", arg.vdev_id);
10992 		return ret;
10993 	}
10994 
10995 	ret = ath12k_mac_vdev_setup_sync(ar);
10996 	if (ret) {
10997 		ath12k_warn(ab, "failed to synchronize setup for vdev %i %s: %d\n",
10998 			    arg.vdev_id, restart ? "restart" : "start", ret);
10999 		return ret;
11000 	}
11001 
11002 	/* TODO: For now we only set TPC power here. However when
11003 	 * channel changes, say CSA, it should be updated again.
11004 	 */
11005 	if (ath12k_mac_supports_tpc(ar, ahvif, chandef)) {
11006 		ath12k_mac_fill_reg_tpc_info(ar, arvif, ctx);
11007 		ath12k_wmi_send_vdev_set_tpc_power(ar, arvif->vdev_id,
11008 						   &arvif->reg_tpc_info);
11009 	}
11010 
11011 	ar->num_started_vdevs++;
11012 	ath12k_dbg(ab, ATH12K_DBG_MAC,  "vdev %pM started, vdev_id %d\n",
11013 		   ahvif->vif->addr, arvif->vdev_id);
11014 
11015 	/* Enable CAC Running Flag in the driver by checking all sub-channel's DFS
11016 	 * state as NL80211_DFS_USABLE which indicates CAC needs to be
11017 	 * done before channel usage. This flag is used to drop rx packets.
11018 	 * during CAC.
11019 	 */
11020 	/* TODO: Set the flag for other interface types as required */
11021 	if (arvif->ahvif->vdev_type == WMI_VDEV_TYPE_AP && ctx->radar_enabled &&
11022 	    cfg80211_chandef_dfs_usable(hw->wiphy, chandef)) {
11023 		set_bit(ATH12K_FLAG_CAC_RUNNING, &ar->dev_flags);
11024 		dfs_cac_time = cfg80211_chandef_dfs_cac_time(hw->wiphy, chandef);
11025 
11026 		ath12k_dbg(ab, ATH12K_DBG_MAC,
11027 			   "CAC started dfs_cac_time %u center_freq %d center_freq1 %d for vdev %d\n",
11028 			   dfs_cac_time, arg.freq, arg.band_center_freq1, arg.vdev_id);
11029 	}
11030 
11031 	ret = ath12k_mac_set_txbf_conf(arvif);
11032 	if (ret)
11033 		ath12k_warn(ab, "failed to set txbf conf for vdev %d: %d\n",
11034 			    arvif->vdev_id, ret);
11035 
11036 	return 0;
11037 }
11038 
11039 static int ath12k_mac_vdev_start(struct ath12k_link_vif *arvif,
11040 				 struct ieee80211_chanctx_conf *ctx)
11041 {
11042 	return ath12k_mac_vdev_start_restart(arvif, ctx, false);
11043 }
11044 
11045 static int ath12k_mac_vdev_restart(struct ath12k_link_vif *arvif,
11046 				   struct ieee80211_chanctx_conf *ctx)
11047 {
11048 	return ath12k_mac_vdev_start_restart(arvif, ctx, true);
11049 }
11050 
11051 struct ath12k_mac_change_chanctx_arg {
11052 	struct ieee80211_chanctx_conf *ctx;
11053 	struct ieee80211_vif_chanctx_switch *vifs;
11054 	int n_vifs;
11055 	int next_vif;
11056 	struct ath12k *ar;
11057 };
11058 
11059 static void
11060 ath12k_mac_change_chanctx_cnt_iter(void *data, u8 *mac,
11061 				   struct ieee80211_vif *vif)
11062 {
11063 	struct ath12k_vif *ahvif = ath12k_vif_to_ahvif(vif);
11064 	struct ath12k_mac_change_chanctx_arg *arg = data;
11065 	struct ieee80211_bss_conf *link_conf;
11066 	struct ath12k_link_vif *arvif;
11067 	unsigned long links_map;
11068 	u8 link_id;
11069 
11070 	lockdep_assert_wiphy(ahvif->ah->hw->wiphy);
11071 
11072 	links_map = ahvif->links_map;
11073 	for_each_set_bit(link_id, &links_map, IEEE80211_MLD_MAX_NUM_LINKS) {
11074 		arvif = wiphy_dereference(ahvif->ah->hw->wiphy, ahvif->link[link_id]);
11075 		if (WARN_ON(!arvif))
11076 			continue;
11077 
11078 		if (!arvif->is_created || arvif->ar != arg->ar)
11079 			continue;
11080 
11081 		link_conf = wiphy_dereference(ahvif->ah->hw->wiphy,
11082 					      vif->link_conf[link_id]);
11083 		if (WARN_ON(!link_conf))
11084 			continue;
11085 
11086 		if (rcu_access_pointer(link_conf->chanctx_conf) != arg->ctx)
11087 			continue;
11088 
11089 		arg->n_vifs++;
11090 	}
11091 }
11092 
11093 static void
11094 ath12k_mac_change_chanctx_fill_iter(void *data, u8 *mac,
11095 				    struct ieee80211_vif *vif)
11096 {
11097 	struct ath12k_vif *ahvif = ath12k_vif_to_ahvif(vif);
11098 	struct ath12k_mac_change_chanctx_arg *arg = data;
11099 	struct ieee80211_bss_conf *link_conf;
11100 	struct ieee80211_chanctx_conf *ctx;
11101 	struct ath12k_link_vif *arvif;
11102 	unsigned long links_map;
11103 	u8 link_id;
11104 
11105 	lockdep_assert_wiphy(ahvif->ah->hw->wiphy);
11106 
11107 	links_map = ahvif->links_map;
11108 	for_each_set_bit(link_id, &links_map, IEEE80211_MLD_MAX_NUM_LINKS) {
11109 		arvif = wiphy_dereference(ahvif->ah->hw->wiphy, ahvif->link[link_id]);
11110 		if (WARN_ON(!arvif))
11111 			continue;
11112 
11113 		if (!arvif->is_created || arvif->ar != arg->ar)
11114 			continue;
11115 
11116 		link_conf = wiphy_dereference(ahvif->ah->hw->wiphy,
11117 					      vif->link_conf[arvif->link_id]);
11118 		if (WARN_ON(!link_conf))
11119 			continue;
11120 
11121 		ctx = rcu_access_pointer(link_conf->chanctx_conf);
11122 		if (ctx != arg->ctx)
11123 			continue;
11124 
11125 		if (WARN_ON(arg->next_vif == arg->n_vifs))
11126 			return;
11127 
11128 		arg->vifs[arg->next_vif].vif = vif;
11129 		arg->vifs[arg->next_vif].old_ctx = ctx;
11130 		arg->vifs[arg->next_vif].new_ctx = ctx;
11131 		arg->vifs[arg->next_vif].link_conf = link_conf;
11132 		arg->next_vif++;
11133 	}
11134 }
11135 
11136 static u32 ath12k_mac_nlwidth_to_wmiwidth(enum nl80211_chan_width width)
11137 {
11138 	switch (width) {
11139 	case NL80211_CHAN_WIDTH_20:
11140 		return WMI_CHAN_WIDTH_20;
11141 	case NL80211_CHAN_WIDTH_40:
11142 		return WMI_CHAN_WIDTH_40;
11143 	case NL80211_CHAN_WIDTH_80:
11144 		return WMI_CHAN_WIDTH_80;
11145 	case NL80211_CHAN_WIDTH_160:
11146 		return WMI_CHAN_WIDTH_160;
11147 	case NL80211_CHAN_WIDTH_80P80:
11148 		return WMI_CHAN_WIDTH_80P80;
11149 	case NL80211_CHAN_WIDTH_5:
11150 		return WMI_CHAN_WIDTH_5;
11151 	case NL80211_CHAN_WIDTH_10:
11152 		return WMI_CHAN_WIDTH_10;
11153 	case NL80211_CHAN_WIDTH_320:
11154 		return WMI_CHAN_WIDTH_320;
11155 	default:
11156 		WARN_ON(1);
11157 		return WMI_CHAN_WIDTH_20;
11158 	}
11159 }
11160 
11161 static int ath12k_mac_update_peer_puncturing_width(struct ath12k *ar,
11162 						   struct ath12k_link_vif *arvif,
11163 						   struct cfg80211_chan_def def)
11164 {
11165 	u32 param_id, param_value;
11166 	int ret;
11167 
11168 	if (arvif->ahvif->vdev_type != WMI_VDEV_TYPE_STA)
11169 		return 0;
11170 
11171 	param_id = WMI_PEER_CHWIDTH_PUNCTURE_20MHZ_BITMAP;
11172 	param_value = ath12k_mac_nlwidth_to_wmiwidth(def.width) |
11173 		u32_encode_bits((~def.punctured),
11174 				WMI_PEER_PUNCTURE_BITMAP);
11175 
11176 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
11177 		   "punctured bitmap %02x width %d vdev %d\n",
11178 		   def.punctured, def.width, arvif->vdev_id);
11179 
11180 	ret = ath12k_wmi_set_peer_param(ar, arvif->bssid,
11181 					arvif->vdev_id, param_id,
11182 					param_value);
11183 
11184 	return ret;
11185 }
11186 
11187 static void
11188 ath12k_mac_update_vif_chan(struct ath12k *ar,
11189 			   struct ieee80211_vif_chanctx_switch *vifs,
11190 			   int n_vifs)
11191 {
11192 	struct ath12k_wmi_vdev_up_params params = {};
11193 	struct ieee80211_bss_conf *link_conf;
11194 	struct ath12k_base *ab = ar->ab;
11195 	struct ath12k_link_vif *arvif;
11196 	struct ieee80211_vif *vif;
11197 	struct ath12k_vif *ahvif;
11198 	u8 link_id;
11199 	int ret;
11200 	int i;
11201 	bool monitor_vif = false;
11202 
11203 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
11204 
11205 	for (i = 0; i < n_vifs; i++) {
11206 		vif = vifs[i].vif;
11207 		ahvif = ath12k_vif_to_ahvif(vif);
11208 		link_conf = vifs[i].link_conf;
11209 		link_id = link_conf->link_id;
11210 		arvif = wiphy_dereference(ath12k_ar_to_hw(ar)->wiphy,
11211 					  ahvif->link[link_id]);
11212 
11213 		if (vif->type == NL80211_IFTYPE_MONITOR) {
11214 			monitor_vif = true;
11215 			continue;
11216 		}
11217 
11218 		ath12k_dbg(ab, ATH12K_DBG_MAC,
11219 			   "mac chanctx switch vdev_id %i freq %u->%u width %d->%d\n",
11220 			   arvif->vdev_id,
11221 			   vifs[i].old_ctx->def.chan->center_freq,
11222 			   vifs[i].new_ctx->def.chan->center_freq,
11223 			   vifs[i].old_ctx->def.width,
11224 			   vifs[i].new_ctx->def.width);
11225 
11226 		if (WARN_ON(!arvif->is_started))
11227 			continue;
11228 
11229 		arvif->punct_bitmap = vifs[i].new_ctx->def.punctured;
11230 
11231 		/* Firmware expect vdev_restart only if vdev is up.
11232 		 * If vdev is down then it expect vdev_stop->vdev_start.
11233 		 */
11234 		if (arvif->is_up) {
11235 			ret = ath12k_mac_vdev_restart(arvif, vifs[i].new_ctx);
11236 			if (ret) {
11237 				ath12k_warn(ab, "failed to restart vdev %d: %d\n",
11238 					    arvif->vdev_id, ret);
11239 				continue;
11240 			}
11241 		} else {
11242 			ret = ath12k_mac_vdev_stop(arvif);
11243 			if (ret) {
11244 				ath12k_warn(ab, "failed to stop vdev %d: %d\n",
11245 					    arvif->vdev_id, ret);
11246 				continue;
11247 			}
11248 
11249 			ret = ath12k_mac_vdev_start(arvif, vifs[i].new_ctx);
11250 			if (ret)
11251 				ath12k_warn(ab, "failed to start vdev %d: %d\n",
11252 					    arvif->vdev_id, ret);
11253 			continue;
11254 		}
11255 
11256 		ret = ath12k_mac_update_peer_puncturing_width(arvif->ar, arvif,
11257 							      vifs[i].new_ctx->def);
11258 		if (ret) {
11259 			ath12k_warn(ar->ab,
11260 				    "failed to update puncturing bitmap %02x and width %d: %d\n",
11261 				    vifs[i].new_ctx->def.punctured,
11262 				    vifs[i].new_ctx->def.width, ret);
11263 			continue;
11264 		}
11265 
11266 		/* Defer VDEV bring-up during CSA to avoid installing stale
11267 		 * beacon templates. The beacon content is updated only
11268 		 * after CSA finalize, so we mark CSA in progress and skip
11269 		 * VDEV_UP for now. It will be handled later in
11270 		 * bss_info_changed().
11271 		 */
11272 		if (link_conf->csa_active &&
11273 		    arvif->ahvif->vdev_type == WMI_VDEV_TYPE_AP) {
11274 			arvif->is_csa_in_progress = true;
11275 			continue;
11276 		}
11277 
11278 		ret = ath12k_mac_setup_bcn_tmpl(arvif);
11279 		if (ret)
11280 			ath12k_warn(ab, "failed to update bcn tmpl during csa: %d\n",
11281 				    ret);
11282 
11283 		memset(&params, 0, sizeof(params));
11284 		params.vdev_id = arvif->vdev_id;
11285 		params.aid = ahvif->aid;
11286 		params.bssid = arvif->bssid;
11287 		params.tx_bssid = ath12k_mac_get_tx_bssid(arvif);
11288 		if (params.tx_bssid) {
11289 			params.nontx_profile_idx = link_conf->bssid_index;
11290 			params.nontx_profile_cnt = 1 << link_conf->bssid_indicator;
11291 		}
11292 		ret = ath12k_wmi_vdev_up(arvif->ar, &params);
11293 		if (ret) {
11294 			ath12k_warn(ab, "failed to bring vdev up %d: %d\n",
11295 				    arvif->vdev_id, ret);
11296 			continue;
11297 		}
11298 	}
11299 
11300 	/* Restart the internal monitor vdev on new channel */
11301 	if (!monitor_vif && ar->monitor_vdev_created) {
11302 		if (!ath12k_mac_monitor_stop(ar))
11303 			ath12k_mac_monitor_start(ar);
11304 	}
11305 }
11306 
11307 static void
11308 ath12k_mac_update_active_vif_chan(struct ath12k *ar,
11309 				  struct ieee80211_chanctx_conf *ctx)
11310 {
11311 	struct ath12k_mac_change_chanctx_arg arg = { .ctx = ctx, .ar = ar };
11312 	struct ieee80211_hw *hw = ath12k_ar_to_hw(ar);
11313 
11314 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
11315 
11316 	ieee80211_iterate_active_interfaces_atomic(hw,
11317 						   IEEE80211_IFACE_ITER_NORMAL,
11318 						   ath12k_mac_change_chanctx_cnt_iter,
11319 						   &arg);
11320 	if (arg.n_vifs == 0)
11321 		return;
11322 
11323 	arg.vifs = kcalloc(arg.n_vifs, sizeof(arg.vifs[0]), GFP_KERNEL);
11324 	if (!arg.vifs)
11325 		return;
11326 
11327 	ieee80211_iterate_active_interfaces_atomic(hw,
11328 						   IEEE80211_IFACE_ITER_NORMAL,
11329 						   ath12k_mac_change_chanctx_fill_iter,
11330 						   &arg);
11331 
11332 	ath12k_mac_update_vif_chan(ar, arg.vifs, arg.n_vifs);
11333 
11334 	kfree(arg.vifs);
11335 }
11336 
11337 static void ath12k_mac_op_change_chanctx(struct ieee80211_hw *hw,
11338 					 struct ieee80211_chanctx_conf *ctx,
11339 					 u32 changed)
11340 {
11341 	struct ath12k *ar;
11342 	struct ath12k_base *ab;
11343 
11344 	lockdep_assert_wiphy(hw->wiphy);
11345 
11346 	ar = ath12k_get_ar_by_ctx(hw, ctx);
11347 	if (!ar)
11348 		return;
11349 
11350 	ab = ar->ab;
11351 
11352 	ath12k_dbg(ab, ATH12K_DBG_MAC,
11353 		   "mac chanctx change freq %u width %d ptr %p changed %x\n",
11354 		   ctx->def.chan->center_freq, ctx->def.width, ctx, changed);
11355 
11356 	/* This shouldn't really happen because channel switching should use
11357 	 * switch_vif_chanctx().
11358 	 */
11359 	if (WARN_ON(changed & IEEE80211_CHANCTX_CHANGE_CHANNEL))
11360 		return;
11361 
11362 	if (changed & IEEE80211_CHANCTX_CHANGE_WIDTH ||
11363 	    changed & IEEE80211_CHANCTX_CHANGE_RADAR ||
11364 	    changed & IEEE80211_CHANCTX_CHANGE_PUNCTURING)
11365 		ath12k_mac_update_active_vif_chan(ar, ctx);
11366 
11367 	/* TODO: Recalc radar detection */
11368 }
11369 
11370 static int ath12k_start_vdev_delay(struct ath12k *ar,
11371 				   struct ath12k_link_vif *arvif)
11372 {
11373 	struct ath12k_base *ab = ar->ab;
11374 	struct ath12k_vif *ahvif = arvif->ahvif;
11375 	struct ieee80211_vif *vif = ath12k_ahvif_to_vif(arvif->ahvif);
11376 	struct ieee80211_chanctx_conf *chanctx;
11377 	struct ieee80211_bss_conf *link_conf;
11378 	int ret;
11379 
11380 	if (WARN_ON(arvif->is_started))
11381 		return -EBUSY;
11382 
11383 	link_conf = ath12k_mac_get_link_bss_conf(arvif);
11384 	if (!link_conf) {
11385 		ath12k_warn(ab, "failed to get link conf for vdev %u\n", arvif->vdev_id);
11386 		return -EINVAL;
11387 	}
11388 
11389 	chanctx	= wiphy_dereference(ath12k_ar_to_hw(arvif->ar)->wiphy,
11390 				    link_conf->chanctx_conf);
11391 	ret = ath12k_mac_vdev_start(arvif, chanctx);
11392 	if (ret) {
11393 		ath12k_warn(ab, "failed to start vdev %i addr %pM on freq %d: %d\n",
11394 			    arvif->vdev_id, vif->addr,
11395 			    chanctx->def.chan->center_freq, ret);
11396 		return ret;
11397 	}
11398 
11399 	if (ahvif->vdev_type == WMI_VDEV_TYPE_MONITOR) {
11400 		ret = ath12k_monitor_vdev_up(ar, arvif->vdev_id);
11401 		if (ret) {
11402 			ath12k_warn(ab, "failed put monitor up: %d\n", ret);
11403 			return ret;
11404 		}
11405 	}
11406 
11407 	arvif->is_started = true;
11408 
11409 	/* TODO: Setup ps and cts/rts protection */
11410 	return 0;
11411 }
11412 
11413 static u8 ath12k_mac_get_num_pwr_levels(struct cfg80211_chan_def *chan_def)
11414 {
11415 	if (chan_def->chan->flags & IEEE80211_CHAN_PSD) {
11416 		switch (chan_def->width) {
11417 		case NL80211_CHAN_WIDTH_20:
11418 			return 1;
11419 		case NL80211_CHAN_WIDTH_40:
11420 			return 2;
11421 		case NL80211_CHAN_WIDTH_80:
11422 			return 4;
11423 		case NL80211_CHAN_WIDTH_160:
11424 			return 8;
11425 		case NL80211_CHAN_WIDTH_320:
11426 			return 16;
11427 		default:
11428 			return 1;
11429 		}
11430 	} else {
11431 		switch (chan_def->width) {
11432 		case NL80211_CHAN_WIDTH_20:
11433 			return 1;
11434 		case NL80211_CHAN_WIDTH_40:
11435 			return 2;
11436 		case NL80211_CHAN_WIDTH_80:
11437 			return 3;
11438 		case NL80211_CHAN_WIDTH_160:
11439 			return 4;
11440 		case NL80211_CHAN_WIDTH_320:
11441 			return 5;
11442 		default:
11443 			return 1;
11444 		}
11445 	}
11446 }
11447 
11448 static u16 ath12k_mac_get_6ghz_start_frequency(struct cfg80211_chan_def *chan_def)
11449 {
11450 	u16 diff_seq;
11451 
11452 	/* It is to get the lowest channel number's center frequency of the chan.
11453 	 * For example,
11454 	 * bandwidth=40 MHz, center frequency is 5965, lowest channel is 1
11455 	 * with center frequency 5955, its diff is 5965 - 5955 = 10.
11456 	 * bandwidth=80 MHz, center frequency is 5985, lowest channel is 1
11457 	 * with center frequency 5955, its diff is 5985 - 5955 = 30.
11458 	 * bandwidth=160 MHz, center frequency is 6025, lowest channel is 1
11459 	 * with center frequency 5955, its diff is 6025 - 5955 = 70.
11460 	 * bandwidth=320 MHz, center frequency is 6105, lowest channel is 1
11461 	 * with center frequency 5955, its diff is 6105 - 5955 = 70.
11462 	 */
11463 	switch (chan_def->width) {
11464 	case NL80211_CHAN_WIDTH_320:
11465 		diff_seq = 150;
11466 		break;
11467 	case NL80211_CHAN_WIDTH_160:
11468 		diff_seq = 70;
11469 		break;
11470 	case NL80211_CHAN_WIDTH_80:
11471 		diff_seq = 30;
11472 		break;
11473 	case NL80211_CHAN_WIDTH_40:
11474 		diff_seq = 10;
11475 		break;
11476 	default:
11477 		diff_seq = 0;
11478 	}
11479 
11480 	return chan_def->center_freq1 - diff_seq;
11481 }
11482 
11483 static u16 ath12k_mac_get_seg_freq(struct cfg80211_chan_def *chan_def,
11484 				   u16 start_seq, u8 seq)
11485 {
11486 	u16 seg_seq;
11487 
11488 	/* It is to get the center frequency of the specific bandwidth.
11489 	 * start_seq means the lowest channel number's center frequency.
11490 	 * seq 0/1/2/3 means 20 MHz/40 MHz/80 MHz/160 MHz.
11491 	 * For example,
11492 	 * lowest channel is 1, its center frequency 5955,
11493 	 * center frequency is 5955 when bandwidth=20 MHz, its diff is 5955 - 5955 = 0.
11494 	 * lowest channel is 1, its center frequency 5955,
11495 	 * center frequency is 5965 when bandwidth=40 MHz, its diff is 5965 - 5955 = 10.
11496 	 * lowest channel is 1, its center frequency 5955,
11497 	 * center frequency is 5985 when bandwidth=80 MHz, its diff is 5985 - 5955 = 30.
11498 	 * lowest channel is 1, its center frequency 5955,
11499 	 * center frequency is 6025 when bandwidth=160 MHz, its diff is 6025 - 5955 = 70.
11500 	 */
11501 	seg_seq = 10 * (BIT(seq) - 1);
11502 	return seg_seq + start_seq;
11503 }
11504 
11505 static void ath12k_mac_get_psd_channel(struct ath12k *ar,
11506 				       u16 step_freq,
11507 				       u16 *start_freq,
11508 				       u16 *center_freq,
11509 				       u8 i,
11510 				       struct ieee80211_channel **temp_chan,
11511 				       s8 *tx_power)
11512 {
11513 	/* It is to get the center frequency for each 20 MHz.
11514 	 * For example, if the chan is 160 MHz and center frequency is 6025,
11515 	 * then it include 8 channels, they are 1/5/9/13/17/21/25/29,
11516 	 * channel number 1's center frequency is 5955, it is parameter start_freq.
11517 	 * parameter i is the step of the 8 channels. i is 0~7 for the 8 channels.
11518 	 * the channel 1/5/9/13/17/21/25/29 maps i=0/1/2/3/4/5/6/7,
11519 	 * and maps its center frequency is 5955/5975/5995/6015/6035/6055/6075/6095,
11520 	 * the gap is 20 for each channel, parameter step_freq means the gap.
11521 	 * after get the center frequency of each channel, it is easy to find the
11522 	 * struct ieee80211_channel of it and get the max_reg_power.
11523 	 */
11524 	*center_freq = *start_freq + i * step_freq;
11525 	*temp_chan = ieee80211_get_channel(ar->ah->hw->wiphy, *center_freq);
11526 	*tx_power = (*temp_chan)->max_reg_power;
11527 }
11528 
11529 static void ath12k_mac_get_eirp_power(struct ath12k *ar,
11530 				      u16 *start_freq,
11531 				      u16 *center_freq,
11532 				      u8 i,
11533 				      struct ieee80211_channel **temp_chan,
11534 				      struct cfg80211_chan_def *def,
11535 				      s8 *tx_power)
11536 {
11537 	/* It is to get the center frequency for 20 MHz/40 MHz/80 MHz/
11538 	 * 160 MHz bandwidth, and then plus 10 to the center frequency,
11539 	 * it is the center frequency of a channel number.
11540 	 * For example, when configured channel number is 1.
11541 	 * center frequency is 5965 when bandwidth=40 MHz, after plus 10, it is 5975,
11542 	 * then it is channel number 5.
11543 	 * center frequency is 5985 when bandwidth=80 MHz, after plus 10, it is 5995,
11544 	 * then it is channel number 9.
11545 	 * center frequency is 6025 when bandwidth=160 MHz, after plus 10, it is 6035,
11546 	 * then it is channel number 17.
11547 	 * after get the center frequency of each channel, it is easy to find the
11548 	 * struct ieee80211_channel of it and get the max_reg_power.
11549 	 */
11550 	*center_freq = ath12k_mac_get_seg_freq(def, *start_freq, i);
11551 
11552 	/* For the 20 MHz, its center frequency is same with same channel */
11553 	if (i != 0)
11554 		*center_freq += 10;
11555 
11556 	*temp_chan = ieee80211_get_channel(ar->ah->hw->wiphy, *center_freq);
11557 	*tx_power = (*temp_chan)->max_reg_power;
11558 }
11559 
11560 void ath12k_mac_fill_reg_tpc_info(struct ath12k *ar,
11561 				  struct ath12k_link_vif *arvif,
11562 				  struct ieee80211_chanctx_conf *ctx)
11563 {
11564 	struct ath12k_base *ab = ar->ab;
11565 	struct ath12k_reg_tpc_power_info *reg_tpc_info = &arvif->reg_tpc_info;
11566 	struct ieee80211_bss_conf *bss_conf = ath12k_mac_get_link_bss_conf(arvif);
11567 	struct ieee80211_channel *chan, *temp_chan;
11568 	u8 pwr_lvl_idx, num_pwr_levels, pwr_reduction;
11569 	bool is_psd_power = false, is_tpe_present = false;
11570 	s8 max_tx_power[ATH12K_NUM_PWR_LEVELS], psd_power, tx_power;
11571 	s8 eirp_power = 0;
11572 	struct ath12k_vif *ahvif = arvif->ahvif;
11573 	u16 start_freq, center_freq;
11574 	u8 reg_6ghz_power_mode;
11575 
11576 	chan = ctx->def.chan;
11577 	start_freq = ath12k_mac_get_6ghz_start_frequency(&ctx->def);
11578 	pwr_reduction = bss_conf->pwr_reduction;
11579 
11580 	if (arvif->reg_tpc_info.num_pwr_levels) {
11581 		is_tpe_present = true;
11582 		num_pwr_levels = arvif->reg_tpc_info.num_pwr_levels;
11583 	} else {
11584 		num_pwr_levels = ath12k_mac_get_num_pwr_levels(&ctx->def);
11585 	}
11586 
11587 	for (pwr_lvl_idx = 0; pwr_lvl_idx < num_pwr_levels; pwr_lvl_idx++) {
11588 		/* STA received TPE IE*/
11589 		if (is_tpe_present) {
11590 			/* local power is PSD power*/
11591 			if (chan->flags & IEEE80211_CHAN_PSD) {
11592 				/* Connecting AP is psd power */
11593 				if (reg_tpc_info->is_psd_power) {
11594 					is_psd_power = true;
11595 					ath12k_mac_get_psd_channel(ar, 20,
11596 								   &start_freq,
11597 								   &center_freq,
11598 								   pwr_lvl_idx,
11599 								   &temp_chan,
11600 								   &tx_power);
11601 					psd_power = temp_chan->psd;
11602 					eirp_power = tx_power;
11603 					max_tx_power[pwr_lvl_idx] =
11604 						min_t(s8,
11605 						      psd_power,
11606 						      reg_tpc_info->tpe[pwr_lvl_idx]);
11607 				/* Connecting AP is not psd power */
11608 				} else {
11609 					ath12k_mac_get_eirp_power(ar,
11610 								  &start_freq,
11611 								  &center_freq,
11612 								  pwr_lvl_idx,
11613 								  &temp_chan,
11614 								  &ctx->def,
11615 								  &tx_power);
11616 					psd_power = temp_chan->psd;
11617 					/* convert psd power to EIRP power based
11618 					 * on channel width
11619 					 */
11620 					tx_power =
11621 						min_t(s8, tx_power,
11622 						      psd_power + 13 + pwr_lvl_idx * 3);
11623 					max_tx_power[pwr_lvl_idx] =
11624 						min_t(s8,
11625 						      tx_power,
11626 						      reg_tpc_info->tpe[pwr_lvl_idx]);
11627 				}
11628 			/* local power is not PSD power */
11629 			} else {
11630 				/* Connecting AP is psd power */
11631 				if (reg_tpc_info->is_psd_power) {
11632 					is_psd_power = true;
11633 					ath12k_mac_get_psd_channel(ar, 20,
11634 								   &start_freq,
11635 								   &center_freq,
11636 								   pwr_lvl_idx,
11637 								   &temp_chan,
11638 								   &tx_power);
11639 					eirp_power = tx_power;
11640 					max_tx_power[pwr_lvl_idx] =
11641 						reg_tpc_info->tpe[pwr_lvl_idx];
11642 				/* Connecting AP is not psd power */
11643 				} else {
11644 					ath12k_mac_get_eirp_power(ar,
11645 								  &start_freq,
11646 								  &center_freq,
11647 								  pwr_lvl_idx,
11648 								  &temp_chan,
11649 								  &ctx->def,
11650 								  &tx_power);
11651 					max_tx_power[pwr_lvl_idx] =
11652 						min_t(s8,
11653 						      tx_power,
11654 						      reg_tpc_info->tpe[pwr_lvl_idx]);
11655 				}
11656 			}
11657 		/* STA not received TPE IE */
11658 		} else {
11659 			/* local power is PSD power*/
11660 			if (chan->flags & IEEE80211_CHAN_PSD) {
11661 				is_psd_power = true;
11662 				ath12k_mac_get_psd_channel(ar, 20,
11663 							   &start_freq,
11664 							   &center_freq,
11665 							   pwr_lvl_idx,
11666 							   &temp_chan,
11667 							   &tx_power);
11668 				psd_power = temp_chan->psd;
11669 				eirp_power = tx_power;
11670 				max_tx_power[pwr_lvl_idx] = psd_power;
11671 			} else {
11672 				ath12k_mac_get_eirp_power(ar,
11673 							  &start_freq,
11674 							  &center_freq,
11675 							  pwr_lvl_idx,
11676 							  &temp_chan,
11677 							  &ctx->def,
11678 							  &tx_power);
11679 				max_tx_power[pwr_lvl_idx] = tx_power;
11680 			}
11681 		}
11682 
11683 		if (is_psd_power) {
11684 			/* If AP local power constraint is present */
11685 			if (pwr_reduction)
11686 				eirp_power = eirp_power - pwr_reduction;
11687 
11688 			/* If firmware updated max tx power is non zero, then take
11689 			 * the min of firmware updated ap tx power
11690 			 * and max power derived from above mentioned parameters.
11691 			 */
11692 			ath12k_dbg(ab, ATH12K_DBG_MAC,
11693 				   "eirp power : %d firmware report power : %d\n",
11694 				   eirp_power, ar->max_allowed_tx_power);
11695 			/* Firmware reports lower max_allowed_tx_power during vdev
11696 			 * start response. In case of 6 GHz, firmware is not aware
11697 			 * of EIRP power unless driver sets EIRP power through WMI
11698 			 * TPC command. So radio which does not support idle power
11699 			 * save can set maximum calculated EIRP power directly to
11700 			 * firmware through TPC command without min comparison with
11701 			 * vdev start response's max_allowed_tx_power.
11702 			 */
11703 			if (ar->max_allowed_tx_power && ab->hw_params->idle_ps)
11704 				eirp_power = min_t(s8,
11705 						   eirp_power,
11706 						   ar->max_allowed_tx_power);
11707 		} else {
11708 			/* If AP local power constraint is present */
11709 			if (pwr_reduction)
11710 				max_tx_power[pwr_lvl_idx] =
11711 					max_tx_power[pwr_lvl_idx] - pwr_reduction;
11712 			/* If firmware updated max tx power is non zero, then take
11713 			 * the min of firmware updated ap tx power
11714 			 * and max power derived from above mentioned parameters.
11715 			 */
11716 			if (ar->max_allowed_tx_power && ab->hw_params->idle_ps)
11717 				max_tx_power[pwr_lvl_idx] =
11718 					min_t(s8,
11719 					      max_tx_power[pwr_lvl_idx],
11720 					      ar->max_allowed_tx_power);
11721 		}
11722 		reg_tpc_info->chan_power_info[pwr_lvl_idx].chan_cfreq = center_freq;
11723 		reg_tpc_info->chan_power_info[pwr_lvl_idx].tx_power =
11724 			max_tx_power[pwr_lvl_idx];
11725 	}
11726 
11727 	reg_tpc_info->num_pwr_levels = num_pwr_levels;
11728 	reg_tpc_info->is_psd_power = is_psd_power;
11729 	reg_tpc_info->eirp_power = eirp_power;
11730 	if (ahvif->vdev_type == WMI_VDEV_TYPE_STA)
11731 		reg_6ghz_power_mode = bss_conf->power_type;
11732 	else
11733 		/* For now, LPI is the only supported AP power mode */
11734 		reg_6ghz_power_mode = IEEE80211_REG_LPI_AP;
11735 
11736 	reg_tpc_info->ap_power_type =
11737 		ath12k_reg_ap_pwr_convert(reg_6ghz_power_mode);
11738 }
11739 
11740 static void ath12k_mac_parse_tx_pwr_env(struct ath12k *ar,
11741 					struct ath12k_link_vif *arvif)
11742 {
11743 	struct ieee80211_bss_conf *bss_conf = ath12k_mac_get_link_bss_conf(arvif);
11744 	struct ath12k_reg_tpc_power_info *tpc_info = &arvif->reg_tpc_info;
11745 	struct ieee80211_parsed_tpe_eirp *local_non_psd, *reg_non_psd;
11746 	struct ieee80211_parsed_tpe_psd *local_psd, *reg_psd;
11747 	struct ieee80211_parsed_tpe *tpe = &bss_conf->tpe;
11748 	enum wmi_reg_6g_client_type client_type;
11749 	struct ath12k_reg_info *reg_info;
11750 	struct ath12k_base *ab = ar->ab;
11751 	bool psd_valid, non_psd_valid;
11752 	int i;
11753 
11754 	reg_info = ab->reg_info[ar->pdev_idx];
11755 	client_type = reg_info->client_type;
11756 
11757 	local_psd = &tpe->psd_local[client_type];
11758 	reg_psd = &tpe->psd_reg_client[client_type];
11759 	local_non_psd = &tpe->max_local[client_type];
11760 	reg_non_psd = &tpe->max_reg_client[client_type];
11761 
11762 	psd_valid = local_psd->valid | reg_psd->valid;
11763 	non_psd_valid = local_non_psd->valid | reg_non_psd->valid;
11764 
11765 	if (!psd_valid && !non_psd_valid) {
11766 		ath12k_warn(ab,
11767 			    "no transmit power envelope match client power type %d\n",
11768 			    client_type);
11769 		return;
11770 	}
11771 
11772 	if (psd_valid) {
11773 		tpc_info->is_psd_power = true;
11774 
11775 		tpc_info->num_pwr_levels = max(local_psd->count,
11776 					       reg_psd->count);
11777 		tpc_info->num_pwr_levels =
11778 				min3(tpc_info->num_pwr_levels,
11779 				     IEEE80211_TPE_PSD_ENTRIES_320MHZ,
11780 				     ATH12K_NUM_PWR_LEVELS);
11781 
11782 		for (i = 0; i < tpc_info->num_pwr_levels; i++) {
11783 			tpc_info->tpe[i] = min(local_psd->power[i],
11784 					       reg_psd->power[i]) / 2;
11785 			ath12k_dbg(ab, ATH12K_DBG_MAC,
11786 				   "TPE PSD power[%d] : %d\n",
11787 				   i, tpc_info->tpe[i]);
11788 		}
11789 	} else {
11790 		tpc_info->is_psd_power = false;
11791 		tpc_info->eirp_power = 0;
11792 
11793 		tpc_info->num_pwr_levels = max(local_non_psd->count,
11794 					       reg_non_psd->count);
11795 		tpc_info->num_pwr_levels =
11796 				min3(tpc_info->num_pwr_levels,
11797 				     IEEE80211_TPE_EIRP_ENTRIES_320MHZ,
11798 				     ATH12K_NUM_PWR_LEVELS);
11799 
11800 		for (i = 0; i < tpc_info->num_pwr_levels; i++) {
11801 			tpc_info->tpe[i] = min(local_non_psd->power[i],
11802 					       reg_non_psd->power[i]) / 2;
11803 			ath12k_dbg(ab, ATH12K_DBG_MAC,
11804 				   "non PSD power[%d] : %d\n",
11805 				   i, tpc_info->tpe[i]);
11806 		}
11807 	}
11808 }
11809 
11810 static int
11811 ath12k_mac_op_assign_vif_chanctx(struct ieee80211_hw *hw,
11812 				 struct ieee80211_vif *vif,
11813 				 struct ieee80211_bss_conf *link_conf,
11814 				 struct ieee80211_chanctx_conf *ctx)
11815 {
11816 	struct ath12k_hw *ah = ath12k_hw_to_ah(hw);
11817 	struct ath12k *ar;
11818 	struct ath12k_base *ab;
11819 	struct ath12k_vif *ahvif = ath12k_vif_to_ahvif(vif);
11820 	u8 link_id = link_conf->link_id;
11821 	struct ath12k_link_vif *arvif;
11822 	int ret;
11823 
11824 	lockdep_assert_wiphy(hw->wiphy);
11825 
11826 	/* For multi radio wiphy, the vdev was not created during add_interface
11827 	 * create now since we have a channel ctx now to assign to a specific ar/fw
11828 	 */
11829 	arvif = ath12k_mac_assign_link_vif(ah, vif, link_id);
11830 	if (!arvif) {
11831 		WARN_ON(1);
11832 		return -ENOMEM;
11833 	}
11834 
11835 	ar = ath12k_mac_assign_vif_to_vdev(hw, arvif, ctx);
11836 	if (!ar) {
11837 		ath12k_hw_warn(ah, "failed to assign chanctx for vif %pM link id %u link vif is already started",
11838 			       vif->addr, link_id);
11839 		return -EINVAL;
11840 	}
11841 
11842 	ab = ar->ab;
11843 
11844 	ath12k_dbg(ab, ATH12K_DBG_MAC,
11845 		   "mac chanctx assign ptr %p vdev_id %i\n",
11846 		   ctx, arvif->vdev_id);
11847 
11848 	if (ath12k_wmi_supports_6ghz_cc_ext(ar) &&
11849 	    ctx->def.chan->band == NL80211_BAND_6GHZ &&
11850 	    ahvif->vdev_type == WMI_VDEV_TYPE_STA)
11851 		ath12k_mac_parse_tx_pwr_env(ar, arvif);
11852 
11853 	arvif->punct_bitmap = ctx->def.punctured;
11854 
11855 	/* for some targets bss peer must be created before vdev_start */
11856 	if (ab->hw_params->vdev_start_delay &&
11857 	    ahvif->vdev_type != WMI_VDEV_TYPE_AP &&
11858 	    ahvif->vdev_type != WMI_VDEV_TYPE_MONITOR &&
11859 	    !ath12k_peer_exist_by_vdev_id(ab, arvif->vdev_id)) {
11860 		ret = 0;
11861 		goto out;
11862 	}
11863 
11864 	if (WARN_ON(arvif->is_started)) {
11865 		ret = -EBUSY;
11866 		goto out;
11867 	}
11868 
11869 	if (ahvif->vdev_type == WMI_VDEV_TYPE_MONITOR) {
11870 		ret = ath12k_mac_monitor_start(ar);
11871 		if (ret) {
11872 			ath12k_mac_monitor_vdev_delete(ar);
11873 			goto out;
11874 		}
11875 
11876 		arvif->is_started = true;
11877 		goto out;
11878 	}
11879 
11880 	ret = ath12k_mac_vdev_start(arvif, ctx);
11881 	if (ret) {
11882 		ath12k_warn(ab, "failed to start vdev %i addr %pM on freq %d: %d\n",
11883 			    arvif->vdev_id, vif->addr,
11884 			    ctx->def.chan->center_freq, ret);
11885 		goto out;
11886 	}
11887 
11888 	arvif->is_started = true;
11889 
11890 	/* TODO: Setup ps and cts/rts protection */
11891 
11892 out:
11893 	return ret;
11894 }
11895 
11896 static void
11897 ath12k_mac_op_unassign_vif_chanctx(struct ieee80211_hw *hw,
11898 				   struct ieee80211_vif *vif,
11899 				   struct ieee80211_bss_conf *link_conf,
11900 				   struct ieee80211_chanctx_conf *ctx)
11901 {
11902 	struct ath12k *ar;
11903 	struct ath12k_base *ab;
11904 	struct ath12k_vif *ahvif = ath12k_vif_to_ahvif(vif);
11905 	struct ath12k_link_vif *arvif;
11906 	u8 link_id = link_conf->link_id;
11907 	int ret;
11908 
11909 	lockdep_assert_wiphy(hw->wiphy);
11910 
11911 	arvif = wiphy_dereference(hw->wiphy, ahvif->link[link_id]);
11912 
11913 	/* The vif is expected to be attached to an ar's VDEV.
11914 	 * We leave the vif/vdev in this function as is
11915 	 * and not delete the vdev symmetric to assign_vif_chanctx()
11916 	 * the VDEV will be deleted and unassigned either during
11917 	 * remove_interface() or when there is a change in channel
11918 	 * that moves the vif to a new ar
11919 	 */
11920 	if (!arvif || !arvif->is_created)
11921 		return;
11922 
11923 	ar = arvif->ar;
11924 	ab = ar->ab;
11925 
11926 	ath12k_dbg(ab, ATH12K_DBG_MAC,
11927 		   "mac chanctx unassign ptr %p vdev_id %i\n",
11928 		   ctx, arvif->vdev_id);
11929 
11930 	WARN_ON(!arvif->is_started);
11931 
11932 	if (ahvif->vdev_type == WMI_VDEV_TYPE_MONITOR) {
11933 		ret = ath12k_mac_monitor_stop(ar);
11934 		if (ret)
11935 			return;
11936 
11937 		arvif->is_started = false;
11938 	}
11939 
11940 	if (ahvif->vdev_type != WMI_VDEV_TYPE_STA &&
11941 	    ahvif->vdev_type != WMI_VDEV_TYPE_MONITOR) {
11942 		ath12k_bss_disassoc(ar, arvif);
11943 		ret = ath12k_mac_vdev_stop(arvif);
11944 		if (ret)
11945 			ath12k_warn(ab, "failed to stop vdev %i: %d\n",
11946 				    arvif->vdev_id, ret);
11947 	}
11948 	arvif->is_started = false;
11949 
11950 	if (test_bit(WMI_TLV_SERVICE_11D_OFFLOAD, ab->wmi_ab.svc_map) &&
11951 	    ahvif->vdev_type == WMI_VDEV_TYPE_STA &&
11952 	    ahvif->vdev_subtype == WMI_VDEV_SUBTYPE_NONE &&
11953 	    ar->state_11d != ATH12K_11D_PREPARING) {
11954 		reinit_completion(&ar->completed_11d_scan);
11955 		ar->state_11d = ATH12K_11D_PREPARING;
11956 	}
11957 
11958 	if (ar->scan.arvif == arvif && ar->scan.state == ATH12K_SCAN_RUNNING) {
11959 		ath12k_scan_abort(ar);
11960 		ar->scan.arvif = NULL;
11961 	}
11962 }
11963 
11964 static int
11965 ath12k_mac_op_switch_vif_chanctx(struct ieee80211_hw *hw,
11966 				 struct ieee80211_vif_chanctx_switch *vifs,
11967 				 int n_vifs,
11968 				 enum ieee80211_chanctx_switch_mode mode)
11969 {
11970 	struct ath12k *ar;
11971 
11972 	lockdep_assert_wiphy(hw->wiphy);
11973 
11974 	ar = ath12k_get_ar_by_ctx(hw, vifs->old_ctx);
11975 	if (!ar)
11976 		return -EINVAL;
11977 
11978 	/* Switching channels across radio is not allowed */
11979 	if (ar != ath12k_get_ar_by_ctx(hw, vifs->new_ctx))
11980 		return -EINVAL;
11981 
11982 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
11983 		   "mac chanctx switch n_vifs %d mode %d\n",
11984 		   n_vifs, mode);
11985 	ath12k_mac_update_vif_chan(ar, vifs, n_vifs);
11986 
11987 	return 0;
11988 }
11989 
11990 static int
11991 ath12k_set_vdev_param_to_all_vifs(struct ath12k *ar, int param, u32 value)
11992 {
11993 	struct ath12k_link_vif *arvif;
11994 	int ret = 0;
11995 
11996 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
11997 
11998 	list_for_each_entry(arvif, &ar->arvifs, list) {
11999 		ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "setting mac vdev %d param %d value %d\n",
12000 			   param, arvif->vdev_id, value);
12001 
12002 		ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
12003 						    param, value);
12004 		if (ret) {
12005 			ath12k_warn(ar->ab, "failed to set param %d for vdev %d: %d\n",
12006 				    param, arvif->vdev_id, ret);
12007 			break;
12008 		}
12009 	}
12010 
12011 	return ret;
12012 }
12013 
12014 /* mac80211 stores device specific RTS/Fragmentation threshold value,
12015  * this is set interface specific to firmware from ath12k driver
12016  */
12017 static int ath12k_mac_op_set_rts_threshold(struct ieee80211_hw *hw,
12018 					   int radio_idx, u32 value)
12019 {
12020 	struct ath12k_hw *ah = ath12k_hw_to_ah(hw);
12021 	struct wiphy *wiphy = hw->wiphy;
12022 	struct ath12k *ar;
12023 	int param_id = WMI_VDEV_PARAM_RTS_THRESHOLD;
12024 	int ret = 0, ret_err, i;
12025 
12026 	lockdep_assert_wiphy(hw->wiphy);
12027 
12028 	if (radio_idx >= wiphy->n_radio || radio_idx < -1)
12029 		return -EINVAL;
12030 
12031 	if (radio_idx != -1) {
12032 		/* Update RTS threshold in specified radio */
12033 		ar = ath12k_ah_to_ar(ah, radio_idx);
12034 		ret = ath12k_set_vdev_param_to_all_vifs(ar, param_id, value);
12035 		if (ret) {
12036 			ath12k_warn(ar->ab,
12037 				    "failed to set RTS config for all vdevs of pdev %d",
12038 				    ar->pdev->pdev_id);
12039 			return ret;
12040 		}
12041 
12042 		ar->rts_threshold = value;
12043 		return 0;
12044 	}
12045 
12046 	/* Radio_index passed is -1, so set RTS threshold for all radios. */
12047 	for_each_ar(ah, ar, i) {
12048 		ret = ath12k_set_vdev_param_to_all_vifs(ar, param_id, value);
12049 		if (ret) {
12050 			ath12k_warn(ar->ab, "failed to set RTS config for all vdevs of pdev %d",
12051 				    ar->pdev->pdev_id);
12052 			break;
12053 		}
12054 	}
12055 	if (!ret) {
12056 		/* Setting new RTS threshold for vdevs of all radios passed, so update
12057 		 * the RTS threshold value for all radios
12058 		 */
12059 		for_each_ar(ah, ar, i)
12060 			ar->rts_threshold = value;
12061 		return 0;
12062 	}
12063 
12064 	/* RTS threshold config failed, revert to the previous RTS threshold */
12065 	for (i = i - 1; i >= 0; i--) {
12066 		ar = ath12k_ah_to_ar(ah, i);
12067 		ret_err = ath12k_set_vdev_param_to_all_vifs(ar, param_id,
12068 							    ar->rts_threshold);
12069 		if (ret_err)
12070 			ath12k_warn(ar->ab,
12071 				    "failed to restore RTS threshold for all vdevs of pdev %d",
12072 				    ar->pdev->pdev_id);
12073 	}
12074 
12075 	return ret;
12076 }
12077 
12078 static int ath12k_mac_op_set_frag_threshold(struct ieee80211_hw *hw,
12079 					    int radio_idx, u32 value)
12080 {
12081 	/* Even though there's a WMI vdev param for fragmentation threshold no
12082 	 * known firmware actually implements it. Moreover it is not possible to
12083 	 * rely frame fragmentation to mac80211 because firmware clears the
12084 	 * "more fragments" bit in frame control making it impossible for remote
12085 	 * devices to reassemble frames.
12086 	 *
12087 	 * Hence implement a dummy callback just to say fragmentation isn't
12088 	 * supported. This effectively prevents mac80211 from doing frame
12089 	 * fragmentation in software.
12090 	 */
12091 
12092 	lockdep_assert_wiphy(hw->wiphy);
12093 
12094 	return -EOPNOTSUPP;
12095 }
12096 
12097 static int ath12k_mac_flush(struct ath12k *ar)
12098 {
12099 	long time_left;
12100 	int ret = 0;
12101 
12102 	time_left = wait_event_timeout(ar->dp.tx_empty_waitq,
12103 				       (atomic_read(&ar->dp.num_tx_pending) == 0),
12104 				       ATH12K_FLUSH_TIMEOUT);
12105 	if (time_left == 0) {
12106 		ath12k_warn(ar->ab,
12107 			    "failed to flush transmit queue, data pkts pending %d\n",
12108 			    atomic_read(&ar->dp.num_tx_pending));
12109 		ret = -ETIMEDOUT;
12110 	}
12111 
12112 	time_left = wait_event_timeout(ar->txmgmt_empty_waitq,
12113 				       (atomic_read(&ar->num_pending_mgmt_tx) == 0),
12114 				       ATH12K_FLUSH_TIMEOUT);
12115 	if (time_left == 0) {
12116 		ath12k_warn(ar->ab,
12117 			    "failed to flush mgmt transmit queue, mgmt pkts pending %d\n",
12118 			    atomic_read(&ar->num_pending_mgmt_tx));
12119 		ret = -ETIMEDOUT;
12120 	}
12121 
12122 	return ret;
12123 }
12124 
12125 int ath12k_mac_wait_tx_complete(struct ath12k *ar)
12126 {
12127 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
12128 
12129 	ath12k_mac_drain_tx(ar);
12130 	return ath12k_mac_flush(ar);
12131 }
12132 
12133 static void ath12k_mac_op_flush(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
12134 				u32 queues, bool drop)
12135 {
12136 	struct ath12k_hw *ah = ath12k_hw_to_ah(hw);
12137 	struct ath12k_link_vif *arvif;
12138 	struct ath12k_vif *ahvif;
12139 	unsigned long links;
12140 	struct ath12k *ar;
12141 	u8 link_id;
12142 	int i;
12143 
12144 	lockdep_assert_wiphy(hw->wiphy);
12145 
12146 	if (drop)
12147 		return;
12148 
12149 	for_each_ar(ah, ar, i)
12150 		wiphy_work_flush(hw->wiphy, &ar->wmi_mgmt_tx_work);
12151 
12152 	/* vif can be NULL when flush() is considered for hw */
12153 	if (!vif) {
12154 		for_each_ar(ah, ar, i)
12155 			ath12k_mac_flush(ar);
12156 		return;
12157 	}
12158 
12159 	ahvif = ath12k_vif_to_ahvif(vif);
12160 	links = ahvif->links_map;
12161 	for_each_set_bit(link_id, &links, IEEE80211_MLD_MAX_NUM_LINKS) {
12162 		arvif = wiphy_dereference(hw->wiphy, ahvif->link[link_id]);
12163 		if (!(arvif && arvif->ar))
12164 			continue;
12165 
12166 		ath12k_mac_flush(arvif->ar);
12167 	}
12168 }
12169 
12170 static int
12171 ath12k_mac_bitrate_mask_num_ht_rates(struct ath12k *ar,
12172 				     enum nl80211_band band,
12173 				     const struct cfg80211_bitrate_mask *mask)
12174 {
12175 	int num_rates = 0;
12176 	int i;
12177 
12178 	for (i = 0; i < ARRAY_SIZE(mask->control[band].ht_mcs); i++)
12179 		num_rates += hweight16(mask->control[band].ht_mcs[i]);
12180 
12181 	return num_rates;
12182 }
12183 
12184 static bool
12185 ath12k_mac_has_single_legacy_rate(struct ath12k *ar,
12186 				  enum nl80211_band band,
12187 				  const struct cfg80211_bitrate_mask *mask)
12188 {
12189 	int num_rates = 0;
12190 
12191 	num_rates = hweight32(mask->control[band].legacy);
12192 
12193 	if (ath12k_mac_bitrate_mask_num_ht_rates(ar, band, mask))
12194 		return false;
12195 
12196 	if (ath12k_mac_bitrate_mask_num_vht_rates(ar, band, mask))
12197 		return false;
12198 
12199 	if (ath12k_mac_bitrate_mask_num_he_rates(ar, band, mask))
12200 		return false;
12201 
12202 	if (ath12k_mac_bitrate_mask_num_eht_rates(ar, band, mask))
12203 		return false;
12204 
12205 	return num_rates == 1;
12206 }
12207 
12208 static __le16
12209 ath12k_mac_get_tx_mcs_map(const struct ieee80211_sta_he_cap *he_cap)
12210 {
12211 	if (he_cap->he_cap_elem.phy_cap_info[0] &
12212 	    IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_160MHZ_IN_5G)
12213 		return he_cap->he_mcs_nss_supp.tx_mcs_160;
12214 
12215 	return he_cap->he_mcs_nss_supp.tx_mcs_80;
12216 }
12217 
12218 static bool
12219 ath12k_mac_bitrate_mask_get_single_nss(struct ath12k *ar,
12220 				       struct ieee80211_vif *vif,
12221 				       enum nl80211_band band,
12222 				       const struct cfg80211_bitrate_mask *mask,
12223 				       int *nss)
12224 {
12225 	struct ieee80211_supported_band *sband = &ar->mac.sbands[band];
12226 	u16 vht_mcs_map = le16_to_cpu(sband->vht_cap.vht_mcs.tx_mcs_map);
12227 	const struct ieee80211_sband_iftype_data *data;
12228 	const struct ieee80211_sta_he_cap *he_cap;
12229 	u16 he_mcs_map = 0;
12230 	u16 eht_mcs_map = 0;
12231 	u8 ht_nss_mask = 0;
12232 	u8 vht_nss_mask = 0;
12233 	u8 he_nss_mask = 0;
12234 	u8 eht_nss_mask = 0;
12235 	u8 mcs_nss_len;
12236 	int i;
12237 
12238 	/* No need to consider legacy here. Basic rates are always present
12239 	 * in bitrate mask
12240 	 */
12241 
12242 	for (i = 0; i < ARRAY_SIZE(mask->control[band].ht_mcs); i++) {
12243 		if (mask->control[band].ht_mcs[i] == 0)
12244 			continue;
12245 		else if (mask->control[band].ht_mcs[i] ==
12246 			 sband->ht_cap.mcs.rx_mask[i])
12247 			ht_nss_mask |= BIT(i);
12248 		else
12249 			return false;
12250 	}
12251 
12252 	for (i = 0; i < ARRAY_SIZE(mask->control[band].vht_mcs); i++) {
12253 		if (mask->control[band].vht_mcs[i] == 0)
12254 			continue;
12255 		else if (mask->control[band].vht_mcs[i] ==
12256 			 ath12k_mac_get_max_vht_mcs_map(vht_mcs_map, i))
12257 			vht_nss_mask |= BIT(i);
12258 		else
12259 			return false;
12260 	}
12261 
12262 	he_cap = ieee80211_get_he_iftype_cap_vif(sband, vif);
12263 	if (!he_cap)
12264 		return false;
12265 
12266 	he_mcs_map = le16_to_cpu(ath12k_mac_get_tx_mcs_map(he_cap));
12267 
12268 	for (i = 0; i < ARRAY_SIZE(mask->control[band].he_mcs); i++) {
12269 		if (mask->control[band].he_mcs[i] == 0)
12270 			continue;
12271 
12272 		if (mask->control[band].he_mcs[i] ==
12273 		    ath12k_mac_get_max_he_mcs_map(he_mcs_map, i))
12274 			he_nss_mask |= BIT(i);
12275 		else
12276 			return false;
12277 	}
12278 
12279 	data = ieee80211_get_sband_iftype_data(sband, vif->type);
12280 
12281 	mcs_nss_len = ieee80211_eht_mcs_nss_size(&data->he_cap.he_cap_elem,
12282 						 &data->eht_cap.eht_cap_elem,
12283 						 false);
12284 	if (mcs_nss_len == 4) {
12285 		/* 20 MHz only STA case */
12286 		const struct ieee80211_eht_mcs_nss_supp_20mhz_only *eht_mcs_nss =
12287 			&data->eht_cap.eht_mcs_nss_supp.only_20mhz;
12288 		if (eht_mcs_nss->rx_tx_mcs13_max_nss)
12289 			eht_mcs_map = 0x1fff;
12290 		else if (eht_mcs_nss->rx_tx_mcs11_max_nss)
12291 			eht_mcs_map = 0x07ff;
12292 		else if (eht_mcs_nss->rx_tx_mcs9_max_nss)
12293 			eht_mcs_map = 0x01ff;
12294 		else
12295 			eht_mcs_map = 0x007f;
12296 	} else {
12297 		const struct ieee80211_eht_mcs_nss_supp_bw *eht_mcs_nss;
12298 
12299 		switch (mcs_nss_len) {
12300 		case 9:
12301 			eht_mcs_nss = &data->eht_cap.eht_mcs_nss_supp.bw._320;
12302 			break;
12303 		case 6:
12304 			eht_mcs_nss = &data->eht_cap.eht_mcs_nss_supp.bw._160;
12305 			break;
12306 		case 3:
12307 			eht_mcs_nss = &data->eht_cap.eht_mcs_nss_supp.bw._80;
12308 			break;
12309 		default:
12310 			return false;
12311 		}
12312 
12313 		if (eht_mcs_nss->rx_tx_mcs13_max_nss)
12314 			eht_mcs_map = 0x1fff;
12315 		else if (eht_mcs_nss->rx_tx_mcs11_max_nss)
12316 			eht_mcs_map = 0x7ff;
12317 		else
12318 			eht_mcs_map = 0x1ff;
12319 	}
12320 
12321 	for (i = 0; i < ARRAY_SIZE(mask->control[band].eht_mcs); i++) {
12322 		if (mask->control[band].eht_mcs[i] == 0)
12323 			continue;
12324 
12325 		if (mask->control[band].eht_mcs[i] < eht_mcs_map)
12326 			eht_nss_mask |= BIT(i);
12327 		else
12328 			return false;
12329 	}
12330 
12331 	if (ht_nss_mask != vht_nss_mask || ht_nss_mask != he_nss_mask ||
12332 	    ht_nss_mask != eht_nss_mask)
12333 		return false;
12334 
12335 	if (ht_nss_mask == 0)
12336 		return false;
12337 
12338 	if (BIT(fls(ht_nss_mask)) - 1 != ht_nss_mask)
12339 		return false;
12340 
12341 	*nss = fls(ht_nss_mask);
12342 
12343 	return true;
12344 }
12345 
12346 static int
12347 ath12k_mac_get_single_legacy_rate(struct ath12k *ar,
12348 				  enum nl80211_band band,
12349 				  const struct cfg80211_bitrate_mask *mask,
12350 				  u32 *rate, u8 *nss)
12351 {
12352 	int rate_idx;
12353 	u16 bitrate;
12354 	u8 preamble;
12355 	u8 hw_rate;
12356 
12357 	if (hweight32(mask->control[band].legacy) != 1)
12358 		return -EINVAL;
12359 
12360 	rate_idx = ffs(mask->control[band].legacy) - 1;
12361 
12362 	if (band == NL80211_BAND_5GHZ || band == NL80211_BAND_6GHZ)
12363 		rate_idx += ATH12K_MAC_FIRST_OFDM_RATE_IDX;
12364 
12365 	hw_rate = ath12k_legacy_rates[rate_idx].hw_value;
12366 	bitrate = ath12k_legacy_rates[rate_idx].bitrate;
12367 
12368 	if (ath12k_mac_bitrate_is_cck(bitrate))
12369 		preamble = WMI_RATE_PREAMBLE_CCK;
12370 	else
12371 		preamble = WMI_RATE_PREAMBLE_OFDM;
12372 
12373 	*nss = 1;
12374 	*rate = ATH12K_HW_RATE_CODE(hw_rate, 0, preamble);
12375 
12376 	return 0;
12377 }
12378 
12379 static int
12380 ath12k_mac_set_fixed_rate_gi_ltf(struct ath12k_link_vif *arvif, u8 gi, u8 ltf,
12381 				 u32 param)
12382 {
12383 	struct ath12k *ar = arvif->ar;
12384 	int ret;
12385 
12386 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
12387 
12388 	/* 0.8 = 0, 1.6 = 2 and 3.2 = 3. */
12389 	if (gi && gi != 0xFF)
12390 		gi += 1;
12391 
12392 	ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
12393 					    WMI_VDEV_PARAM_SGI, gi);
12394 	if (ret) {
12395 		ath12k_warn(ar->ab, "failed to set GI:%d, error:%d\n",
12396 			    gi, ret);
12397 		return ret;
12398 	}
12399 
12400 	if (param == WMI_VDEV_PARAM_HE_LTF) {
12401 		/* HE values start from 1 */
12402 		if (ltf != 0xFF)
12403 			ltf += 1;
12404 	} else {
12405 		/* EHT values start from 5 */
12406 		if (ltf != 0xFF)
12407 			ltf += 4;
12408 	}
12409 
12410 	ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
12411 					    param, ltf);
12412 	if (ret) {
12413 		ath12k_warn(ar->ab, "failed to set LTF:%d, error:%d\n",
12414 			    ltf, ret);
12415 		return ret;
12416 	}
12417 	return 0;
12418 }
12419 
12420 static int
12421 ath12k_mac_set_auto_rate_gi_ltf(struct ath12k_link_vif *arvif, u16 gi, u8 ltf)
12422 {
12423 	struct ath12k *ar = arvif->ar;
12424 	int ret;
12425 	u32 ar_gi_ltf;
12426 
12427 	if (gi != 0xFF) {
12428 		switch (gi) {
12429 		case ATH12K_RATE_INFO_GI_0_8:
12430 			gi = WMI_AUTORATE_800NS_GI;
12431 			break;
12432 		case ATH12K_RATE_INFO_GI_1_6:
12433 			gi = WMI_AUTORATE_1600NS_GI;
12434 			break;
12435 		case ATH12K_RATE_INFO_GI_3_2:
12436 			gi = WMI_AUTORATE_3200NS_GI;
12437 			break;
12438 		default:
12439 			ath12k_warn(ar->ab, "Invalid GI\n");
12440 			return -EINVAL;
12441 		}
12442 	}
12443 
12444 	if (ltf != 0xFF) {
12445 		switch (ltf) {
12446 		case ATH12K_RATE_INFO_1XLTF:
12447 			ltf = WMI_AUTORATE_LTF_1X;
12448 			break;
12449 		case ATH12K_RATE_INFO_2XLTF:
12450 			ltf = WMI_AUTORATE_LTF_2X;
12451 			break;
12452 		case ATH12K_RATE_INFO_4XLTF:
12453 			ltf = WMI_AUTORATE_LTF_4X;
12454 			break;
12455 		default:
12456 			ath12k_warn(ar->ab, "Invalid LTF\n");
12457 			return -EINVAL;
12458 		}
12459 	}
12460 
12461 	ar_gi_ltf = gi | ltf;
12462 
12463 	ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
12464 					    WMI_VDEV_PARAM_AUTORATE_MISC_CFG,
12465 					    ar_gi_ltf);
12466 	if (ret) {
12467 		ath12k_warn(ar->ab,
12468 			    "failed to set autorate GI:%u, LTF:%u params, error:%d\n",
12469 			    gi, ltf, ret);
12470 		return ret;
12471 	}
12472 
12473 	return 0;
12474 }
12475 
12476 static u32 ath12k_mac_nlgi_to_wmigi(enum nl80211_txrate_gi gi)
12477 {
12478 	switch (gi) {
12479 	case NL80211_TXRATE_DEFAULT_GI:
12480 		return WMI_GI_400_NS;
12481 	case NL80211_TXRATE_FORCE_LGI:
12482 		return WMI_GI_800_NS;
12483 	default:
12484 		return WMI_GI_400_NS;
12485 	}
12486 }
12487 
12488 static int ath12k_mac_set_rate_params(struct ath12k_link_vif *arvif,
12489 				      u32 rate, u8 nss, u8 sgi, u8 ldpc,
12490 				      u8 he_gi, u8 he_ltf, bool he_fixed_rate,
12491 				      u8 eht_gi, u8 eht_ltf,
12492 				      bool eht_fixed_rate)
12493 {
12494 	struct ieee80211_bss_conf *link_conf;
12495 	struct ath12k *ar = arvif->ar;
12496 	bool he_support, eht_support, gi_ltf_set = false;
12497 	u32 vdev_param;
12498 	u32 param_value;
12499 	int ret;
12500 
12501 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
12502 
12503 	link_conf = ath12k_mac_get_link_bss_conf(arvif);
12504 	if (!link_conf)
12505 		return -EINVAL;
12506 
12507 	he_support = link_conf->he_support;
12508 	eht_support = link_conf->eht_support;
12509 
12510 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
12511 		   "mac set rate params vdev %i rate 0x%02x nss 0x%02x sgi 0x%02x ldpc 0x%02x\n",
12512 		   arvif->vdev_id, rate, nss, sgi, ldpc);
12513 
12514 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
12515 		   "he_gi 0x%02x he_ltf 0x%02x he_fixed_rate %d\n", he_gi,
12516 		   he_ltf, he_fixed_rate);
12517 
12518 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
12519 		   "eht_gi 0x%02x eht_ltf 0x%02x eht_fixed_rate %d\n",
12520 		   eht_gi, eht_ltf, eht_fixed_rate);
12521 
12522 	if (!he_support && !eht_support) {
12523 		vdev_param = WMI_VDEV_PARAM_FIXED_RATE;
12524 		ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
12525 						    vdev_param, rate);
12526 		if (ret) {
12527 			ath12k_warn(ar->ab, "failed to set fixed rate param 0x%02x: %d\n",
12528 				    rate, ret);
12529 			return ret;
12530 		}
12531 	}
12532 
12533 	vdev_param = WMI_VDEV_PARAM_NSS;
12534 
12535 	ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
12536 					    vdev_param, nss);
12537 	if (ret) {
12538 		ath12k_warn(ar->ab, "failed to set nss param %d: %d\n",
12539 			    nss, ret);
12540 		return ret;
12541 	}
12542 
12543 	ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
12544 					    WMI_VDEV_PARAM_LDPC, ldpc);
12545 	if (ret) {
12546 		ath12k_warn(ar->ab, "failed to set ldpc param %d: %d\n",
12547 			    ldpc, ret);
12548 		return ret;
12549 	}
12550 
12551 	if (eht_support) {
12552 		if (eht_fixed_rate)
12553 			ret = ath12k_mac_set_fixed_rate_gi_ltf(arvif, eht_gi, eht_ltf,
12554 							       WMI_VDEV_PARAM_EHT_LTF);
12555 		else
12556 			ret = ath12k_mac_set_auto_rate_gi_ltf(arvif, eht_gi, eht_ltf);
12557 
12558 		if (ret) {
12559 			ath12k_warn(ar->ab,
12560 				    "failed to set EHT LTF/GI params %d/%d: %d\n",
12561 				    eht_gi, eht_ltf, ret);
12562 			return ret;
12563 		}
12564 		gi_ltf_set = true;
12565 	}
12566 
12567 	if (he_support) {
12568 		if (he_fixed_rate)
12569 			ret = ath12k_mac_set_fixed_rate_gi_ltf(arvif, he_gi, he_ltf,
12570 							       WMI_VDEV_PARAM_HE_LTF);
12571 		else
12572 			ret = ath12k_mac_set_auto_rate_gi_ltf(arvif, he_gi, he_ltf);
12573 		if (ret)
12574 			return ret;
12575 		gi_ltf_set = true;
12576 	}
12577 
12578 	if (!gi_ltf_set) {
12579 		vdev_param = WMI_VDEV_PARAM_SGI;
12580 		param_value = ath12k_mac_nlgi_to_wmigi(sgi);
12581 		ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
12582 						    vdev_param, param_value);
12583 		if (ret) {
12584 			ath12k_warn(ar->ab, "failed to set sgi param %d: %d\n",
12585 				    sgi, ret);
12586 			return ret;
12587 		}
12588 	}
12589 
12590 	return 0;
12591 }
12592 
12593 static bool
12594 ath12k_mac_vht_mcs_range_present(struct ath12k *ar,
12595 				 enum nl80211_band band,
12596 				 const struct cfg80211_bitrate_mask *mask)
12597 {
12598 	int i;
12599 	u16 vht_mcs;
12600 
12601 	for (i = 0; i < NL80211_VHT_NSS_MAX; i++) {
12602 		vht_mcs = mask->control[band].vht_mcs[i];
12603 
12604 		switch (vht_mcs) {
12605 		case 0:
12606 		case BIT(8) - 1:
12607 		case BIT(9) - 1:
12608 		case BIT(10) - 1:
12609 			break;
12610 		default:
12611 			return false;
12612 		}
12613 	}
12614 
12615 	return true;
12616 }
12617 
12618 static bool
12619 ath12k_mac_he_mcs_range_present(struct ath12k *ar,
12620 				enum nl80211_band band,
12621 				const struct cfg80211_bitrate_mask *mask)
12622 {
12623 	int i;
12624 	u16 he_mcs;
12625 
12626 	for (i = 0; i < NL80211_HE_NSS_MAX; i++) {
12627 		he_mcs = mask->control[band].he_mcs[i];
12628 
12629 		switch (he_mcs) {
12630 		case 0:
12631 		case BIT(8) - 1:
12632 		case BIT(10) - 1:
12633 		case BIT(12) - 1:
12634 			break;
12635 		default:
12636 			return false;
12637 		}
12638 	}
12639 
12640 	return true;
12641 }
12642 
12643 static bool
12644 ath12k_mac_eht_mcs_range_present(struct ath12k *ar,
12645 				 enum nl80211_band band,
12646 				 const struct cfg80211_bitrate_mask *mask)
12647 {
12648 	u16 eht_mcs;
12649 	int i;
12650 
12651 	for (i = 0; i < NL80211_EHT_NSS_MAX; i++) {
12652 		eht_mcs = mask->control[band].eht_mcs[i];
12653 
12654 		switch (eht_mcs) {
12655 		case 0:
12656 		case BIT(8) - 1:
12657 		case BIT(10) - 1:
12658 		case BIT(12) - 1:
12659 		case BIT(14) - 1:
12660 			break;
12661 		case BIT(15) - 1:
12662 		case BIT(16) - 1:
12663 		case BIT(16) - BIT(14) - 1:
12664 			if (i != 0)
12665 				return false;
12666 			break;
12667 		default:
12668 			return false;
12669 		}
12670 	}
12671 
12672 	return true;
12673 }
12674 
12675 static void ath12k_mac_set_bitrate_mask_iter(void *data,
12676 					     struct ieee80211_sta *sta)
12677 {
12678 	struct ath12k_link_vif *arvif = data;
12679 	struct ath12k_sta *ahsta = ath12k_sta_to_ahsta(sta);
12680 	struct ath12k_link_sta *arsta;
12681 	struct ath12k *ar = arvif->ar;
12682 
12683 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
12684 
12685 	arsta = wiphy_dereference(ath12k_ar_to_hw(ar)->wiphy,
12686 				  ahsta->link[arvif->link_id]);
12687 	if (!arsta || arsta->arvif != arvif)
12688 		return;
12689 
12690 	spin_lock_bh(&ar->data_lock);
12691 	arsta->changed |= IEEE80211_RC_SUPP_RATES_CHANGED;
12692 	spin_unlock_bh(&ar->data_lock);
12693 
12694 	wiphy_work_queue(ath12k_ar_to_hw(ar)->wiphy, &arsta->update_wk);
12695 }
12696 
12697 static void ath12k_mac_disable_peer_fixed_rate(void *data,
12698 					       struct ieee80211_sta *sta)
12699 {
12700 	struct ath12k_link_vif *arvif = data;
12701 	struct ath12k_sta *ahsta = ath12k_sta_to_ahsta(sta);
12702 	struct ath12k_link_sta *arsta;
12703 	struct ath12k *ar = arvif->ar;
12704 	int ret;
12705 
12706 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
12707 
12708 	arsta = wiphy_dereference(ath12k_ar_to_hw(ar)->wiphy,
12709 				  ahsta->link[arvif->link_id]);
12710 
12711 	if (!arsta || arsta->arvif != arvif)
12712 		return;
12713 
12714 	ret = ath12k_wmi_set_peer_param(ar, arsta->addr,
12715 					arvif->vdev_id,
12716 					WMI_PEER_PARAM_FIXED_RATE,
12717 					WMI_FIXED_RATE_NONE);
12718 	if (ret)
12719 		ath12k_warn(ar->ab,
12720 			    "failed to disable peer fixed rate for STA %pM ret %d\n",
12721 			    arsta->addr, ret);
12722 }
12723 
12724 static bool
12725 ath12k_mac_validate_fixed_rate_settings(struct ath12k *ar, enum nl80211_band band,
12726 					const struct cfg80211_bitrate_mask *mask,
12727 					unsigned int link_id)
12728 {
12729 	bool eht_fixed_rate = false, he_fixed_rate = false, vht_fixed_rate = false;
12730 	const u16 *vht_mcs_mask, *he_mcs_mask, *eht_mcs_mask;
12731 	struct ieee80211_link_sta *link_sta;
12732 	struct ath12k_peer *peer, *tmp;
12733 	u8 vht_nss, he_nss, eht_nss;
12734 	int ret = true;
12735 
12736 	vht_mcs_mask = mask->control[band].vht_mcs;
12737 	he_mcs_mask = mask->control[band].he_mcs;
12738 	eht_mcs_mask = mask->control[band].eht_mcs;
12739 
12740 	if (ath12k_mac_bitrate_mask_num_vht_rates(ar, band, mask) == 1)
12741 		vht_fixed_rate = true;
12742 
12743 	if (ath12k_mac_bitrate_mask_num_he_rates(ar, band, mask) == 1)
12744 		he_fixed_rate = true;
12745 
12746 	if (ath12k_mac_bitrate_mask_num_eht_rates(ar, band, mask) == 1)
12747 		eht_fixed_rate = true;
12748 
12749 	if (!vht_fixed_rate && !he_fixed_rate && !eht_fixed_rate)
12750 		return true;
12751 
12752 	vht_nss = ath12k_mac_max_vht_nss(vht_mcs_mask);
12753 	he_nss =  ath12k_mac_max_he_nss(he_mcs_mask);
12754 	eht_nss = ath12k_mac_max_eht_nss(eht_mcs_mask);
12755 
12756 	rcu_read_lock();
12757 	spin_lock_bh(&ar->ab->base_lock);
12758 	list_for_each_entry_safe(peer, tmp, &ar->ab->peers, list) {
12759 		if (peer->sta) {
12760 			link_sta = rcu_dereference(peer->sta->link[link_id]);
12761 			if (!link_sta) {
12762 				ret = false;
12763 				goto exit;
12764 			}
12765 
12766 			if (vht_fixed_rate && (!link_sta->vht_cap.vht_supported ||
12767 					       link_sta->rx_nss < vht_nss)) {
12768 				ret = false;
12769 				goto exit;
12770 			}
12771 			if (he_fixed_rate && (!link_sta->he_cap.has_he ||
12772 					      link_sta->rx_nss < he_nss)) {
12773 				ret = false;
12774 				goto exit;
12775 			}
12776 			if (eht_fixed_rate && (!link_sta->eht_cap.has_eht ||
12777 					       link_sta->rx_nss < eht_nss)) {
12778 				ret = false;
12779 				goto exit;
12780 			}
12781 		}
12782 	}
12783 exit:
12784 	spin_unlock_bh(&ar->ab->base_lock);
12785 	rcu_read_unlock();
12786 	return ret;
12787 }
12788 
12789 static int
12790 ath12k_mac_op_set_bitrate_mask(struct ieee80211_hw *hw,
12791 			       struct ieee80211_vif *vif,
12792 			       const struct cfg80211_bitrate_mask *mask)
12793 {
12794 	struct ath12k_vif *ahvif = ath12k_vif_to_ahvif(vif);
12795 	struct ath12k_link_vif *arvif;
12796 	struct cfg80211_chan_def def;
12797 	struct ath12k *ar;
12798 	enum nl80211_band band;
12799 	const u8 *ht_mcs_mask;
12800 	const u16 *vht_mcs_mask;
12801 	const u16 *he_mcs_mask;
12802 	const u16 *eht_mcs_mask;
12803 	u8 he_ltf = 0;
12804 	u8 he_gi = 0;
12805 	u8 eht_ltf = 0, eht_gi = 0;
12806 	u32 rate;
12807 	u8 nss, mac_nss;
12808 	u8 sgi;
12809 	u8 ldpc;
12810 	int single_nss;
12811 	int ret;
12812 	int num_rates;
12813 	bool he_fixed_rate = false;
12814 	bool eht_fixed_rate = false;
12815 
12816 	lockdep_assert_wiphy(hw->wiphy);
12817 
12818 	arvif = &ahvif->deflink;
12819 
12820 	ar = arvif->ar;
12821 	if (ath12k_mac_vif_link_chan(vif, arvif->link_id, &def)) {
12822 		ret = -EPERM;
12823 		goto out;
12824 	}
12825 
12826 	band = def.chan->band;
12827 	ht_mcs_mask = mask->control[band].ht_mcs;
12828 	vht_mcs_mask = mask->control[band].vht_mcs;
12829 	he_mcs_mask = mask->control[band].he_mcs;
12830 	eht_mcs_mask = mask->control[band].eht_mcs;
12831 	ldpc = !!(ar->ht_cap_info & WMI_HT_CAP_LDPC);
12832 
12833 	sgi = mask->control[band].gi;
12834 	if (sgi == NL80211_TXRATE_FORCE_SGI) {
12835 		ret = -EINVAL;
12836 		goto out;
12837 	}
12838 
12839 	he_gi = mask->control[band].he_gi;
12840 	he_ltf = mask->control[band].he_ltf;
12841 
12842 	eht_gi = mask->control[band].eht_gi;
12843 	eht_ltf = mask->control[band].eht_ltf;
12844 
12845 	/* mac80211 doesn't support sending a fixed HT/VHT MCS alone, rather it
12846 	 * requires passing at least one of used basic rates along with them.
12847 	 * Fixed rate setting across different preambles(legacy, HT, VHT) is
12848 	 * not supported by the FW. Hence use of FIXED_RATE vdev param is not
12849 	 * suitable for setting single HT/VHT rates.
12850 	 * But, there could be a single basic rate passed from userspace which
12851 	 * can be done through the FIXED_RATE param.
12852 	 */
12853 	if (ath12k_mac_has_single_legacy_rate(ar, band, mask)) {
12854 		ret = ath12k_mac_get_single_legacy_rate(ar, band, mask, &rate,
12855 							&nss);
12856 		if (ret) {
12857 			ath12k_warn(ar->ab, "failed to get single legacy rate for vdev %i: %d\n",
12858 				    arvif->vdev_id, ret);
12859 			goto out;
12860 		}
12861 
12862 		ieee80211_iterate_stations_mtx(hw,
12863 					       ath12k_mac_disable_peer_fixed_rate,
12864 					       arvif);
12865 	} else if (ath12k_mac_bitrate_mask_get_single_nss(ar, vif, band, mask,
12866 							  &single_nss)) {
12867 		rate = WMI_FIXED_RATE_NONE;
12868 		nss = single_nss;
12869 		arvif->bitrate_mask = *mask;
12870 
12871 		ieee80211_iterate_stations_atomic(hw,
12872 						  ath12k_mac_set_bitrate_mask_iter,
12873 						  arvif);
12874 	} else {
12875 		rate = WMI_FIXED_RATE_NONE;
12876 
12877 		if (!ath12k_mac_validate_fixed_rate_settings(ar, band,
12878 							     mask, arvif->link_id))
12879 			ath12k_warn(ar->ab,
12880 				    "failed to update fixed rate settings due to mcs/nss incompatibility\n");
12881 
12882 		mac_nss = max(max3(ath12k_mac_max_ht_nss(ht_mcs_mask),
12883 				   ath12k_mac_max_vht_nss(vht_mcs_mask),
12884 				   ath12k_mac_max_he_nss(he_mcs_mask)),
12885 			       ath12k_mac_max_eht_nss(eht_mcs_mask));
12886 		nss = min_t(u32, ar->num_tx_chains, mac_nss);
12887 
12888 		/* If multiple rates across different preambles are given
12889 		 * we can reconfigure this info with all peers using PEER_ASSOC
12890 		 * command with the below exception cases.
12891 		 * - Single VHT Rate : peer_assoc command accommodates only MCS
12892 		 * range values i.e 0-7, 0-8, 0-9 for VHT. Though mac80211
12893 		 * mandates passing basic rates along with HT/VHT rates, FW
12894 		 * doesn't allow switching from VHT to Legacy. Hence instead of
12895 		 * setting legacy and VHT rates using RATEMASK_CMD vdev cmd,
12896 		 * we could set this VHT rate as peer fixed rate param, which
12897 		 * will override FIXED rate and FW rate control algorithm.
12898 		 * If single VHT rate is passed along with HT rates, we select
12899 		 * the VHT rate as fixed rate for vht peers.
12900 		 * - Multiple VHT Rates : When Multiple VHT rates are given,this
12901 		 * can be set using RATEMASK CMD which uses FW rate-ctl alg.
12902 		 * TODO: Setting multiple VHT MCS and replacing peer_assoc with
12903 		 * RATEMASK_CMDID can cover all use cases of setting rates
12904 		 * across multiple preambles and rates within same type.
12905 		 * But requires more validation of the command at this point.
12906 		 */
12907 
12908 		num_rates = ath12k_mac_bitrate_mask_num_vht_rates(ar, band,
12909 								  mask);
12910 
12911 		if (!ath12k_mac_vht_mcs_range_present(ar, band, mask) &&
12912 		    num_rates > 1) {
12913 			/* TODO: Handle multiple VHT MCS values setting using
12914 			 * RATEMASK CMD
12915 			 */
12916 			ath12k_warn(ar->ab,
12917 				    "Setting more than one MCS Value in bitrate mask not supported\n");
12918 			ret = -EINVAL;
12919 			goto out;
12920 		}
12921 
12922 		num_rates = ath12k_mac_bitrate_mask_num_he_rates(ar, band, mask);
12923 		if (num_rates == 1)
12924 			he_fixed_rate = true;
12925 
12926 		if (!ath12k_mac_he_mcs_range_present(ar, band, mask) &&
12927 		    num_rates > 1) {
12928 			ath12k_warn(ar->ab,
12929 				    "Setting more than one HE MCS Value in bitrate mask not supported\n");
12930 			ret = -EINVAL;
12931 			goto out;
12932 		}
12933 
12934 		num_rates = ath12k_mac_bitrate_mask_num_eht_rates(ar, band,
12935 								  mask);
12936 		if (num_rates == 1)
12937 			eht_fixed_rate = true;
12938 
12939 		if (!ath12k_mac_eht_mcs_range_present(ar, band, mask) &&
12940 		    num_rates > 1) {
12941 			ath12k_warn(ar->ab,
12942 				    "Setting more than one EHT MCS Value in bitrate mask not supported\n");
12943 			ret = -EINVAL;
12944 			goto out;
12945 		}
12946 
12947 		ieee80211_iterate_stations_mtx(hw,
12948 					       ath12k_mac_disable_peer_fixed_rate,
12949 					       arvif);
12950 
12951 		arvif->bitrate_mask = *mask;
12952 		ieee80211_iterate_stations_mtx(hw,
12953 					       ath12k_mac_set_bitrate_mask_iter,
12954 					       arvif);
12955 	}
12956 
12957 	ret = ath12k_mac_set_rate_params(arvif, rate, nss, sgi, ldpc, he_gi,
12958 					 he_ltf, he_fixed_rate, eht_gi, eht_ltf,
12959 					 eht_fixed_rate);
12960 	if (ret) {
12961 		ath12k_warn(ar->ab, "failed to set rate params on vdev %i: %d\n",
12962 			    arvif->vdev_id, ret);
12963 	}
12964 
12965 out:
12966 	return ret;
12967 }
12968 
12969 static void
12970 ath12k_mac_op_reconfig_complete(struct ieee80211_hw *hw,
12971 				enum ieee80211_reconfig_type reconfig_type)
12972 {
12973 	struct ath12k_hw *ah = ath12k_hw_to_ah(hw);
12974 	struct ath12k *ar;
12975 	struct ath12k_base *ab;
12976 	struct ath12k_vif *ahvif;
12977 	struct ath12k_link_vif *arvif;
12978 	int recovery_count, i;
12979 
12980 	lockdep_assert_wiphy(hw->wiphy);
12981 
12982 	if (reconfig_type != IEEE80211_RECONFIG_TYPE_RESTART)
12983 		return;
12984 
12985 	guard(mutex)(&ah->hw_mutex);
12986 
12987 	if (ah->state != ATH12K_HW_STATE_RESTARTED)
12988 		return;
12989 
12990 	ah->state = ATH12K_HW_STATE_ON;
12991 	ieee80211_wake_queues(hw);
12992 
12993 	for_each_ar(ah, ar, i) {
12994 		ab = ar->ab;
12995 
12996 		ath12k_warn(ar->ab, "pdev %d successfully recovered\n",
12997 			    ar->pdev->pdev_id);
12998 
12999 		if (ar->ab->hw_params->current_cc_support &&
13000 		    ar->alpha2[0] != 0 && ar->alpha2[1] != 0) {
13001 			struct wmi_set_current_country_arg arg = {};
13002 
13003 			memcpy(&arg.alpha2, ar->alpha2, 2);
13004 			reinit_completion(&ar->regd_update_completed);
13005 			ath12k_wmi_send_set_current_country_cmd(ar, &arg);
13006 		}
13007 
13008 		if (ab->is_reset) {
13009 			recovery_count = atomic_inc_return(&ab->recovery_count);
13010 
13011 			ath12k_dbg(ab, ATH12K_DBG_BOOT, "recovery count %d\n",
13012 				   recovery_count);
13013 
13014 			/* When there are multiple radios in an SOC,
13015 			 * the recovery has to be done for each radio
13016 			 */
13017 			if (recovery_count == ab->num_radios) {
13018 				atomic_dec(&ab->reset_count);
13019 				complete(&ab->reset_complete);
13020 				ab->is_reset = false;
13021 				atomic_set(&ab->fail_cont_count, 0);
13022 				ath12k_dbg(ab, ATH12K_DBG_BOOT, "reset success\n");
13023 			}
13024 		}
13025 
13026 		list_for_each_entry(arvif, &ar->arvifs, list) {
13027 			ahvif = arvif->ahvif;
13028 			ath12k_dbg(ab, ATH12K_DBG_BOOT,
13029 				   "reconfig cipher %d up %d vdev type %d\n",
13030 				   ahvif->key_cipher,
13031 				   arvif->is_up,
13032 				   ahvif->vdev_type);
13033 
13034 			/* After trigger disconnect, then upper layer will
13035 			 * trigger connect again, then the PN number of
13036 			 * upper layer will be reset to keep up with AP
13037 			 * side, hence PN number mismatch will not happen.
13038 			 */
13039 			if (arvif->is_up &&
13040 			    ahvif->vdev_type == WMI_VDEV_TYPE_STA &&
13041 			    ahvif->vdev_subtype == WMI_VDEV_SUBTYPE_NONE) {
13042 				ieee80211_hw_restart_disconnect(ahvif->vif);
13043 
13044 				ath12k_dbg(ab, ATH12K_DBG_BOOT,
13045 					   "restart disconnect\n");
13046 			}
13047 		}
13048 	}
13049 }
13050 
13051 static void
13052 ath12k_mac_update_bss_chan_survey(struct ath12k *ar,
13053 				  struct ieee80211_channel *channel)
13054 {
13055 	int ret;
13056 	enum wmi_bss_chan_info_req_type type = WMI_BSS_SURVEY_REQ_TYPE_READ;
13057 
13058 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
13059 
13060 	if (!test_bit(WMI_TLV_SERVICE_BSS_CHANNEL_INFO_64, ar->ab->wmi_ab.svc_map) ||
13061 	    ar->rx_channel != channel)
13062 		return;
13063 
13064 	if (ar->scan.state != ATH12K_SCAN_IDLE) {
13065 		ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
13066 			   "ignoring bss chan info req while scanning..\n");
13067 		return;
13068 	}
13069 
13070 	reinit_completion(&ar->bss_survey_done);
13071 
13072 	ret = ath12k_wmi_pdev_bss_chan_info_request(ar, type);
13073 	if (ret) {
13074 		ath12k_warn(ar->ab, "failed to send pdev bss chan info request\n");
13075 		return;
13076 	}
13077 
13078 	ret = wait_for_completion_timeout(&ar->bss_survey_done, 3 * HZ);
13079 	if (ret == 0)
13080 		ath12k_warn(ar->ab, "bss channel survey timed out\n");
13081 }
13082 
13083 static int ath12k_mac_op_get_survey(struct ieee80211_hw *hw, int idx,
13084 				    struct survey_info *survey)
13085 {
13086 	struct ath12k *ar;
13087 	struct ieee80211_supported_band *sband;
13088 	struct survey_info *ar_survey;
13089 
13090 	lockdep_assert_wiphy(hw->wiphy);
13091 
13092 	if (idx >= ATH12K_NUM_CHANS)
13093 		return -ENOENT;
13094 
13095 	sband = hw->wiphy->bands[NL80211_BAND_2GHZ];
13096 	if (sband && idx >= sband->n_channels) {
13097 		idx -= sband->n_channels;
13098 		sband = NULL;
13099 	}
13100 
13101 	if (!sband)
13102 		sband = hw->wiphy->bands[NL80211_BAND_5GHZ];
13103 	if (sband && idx >= sband->n_channels) {
13104 		idx -= sband->n_channels;
13105 		sband = NULL;
13106 	}
13107 
13108 	if (!sband)
13109 		sband = hw->wiphy->bands[NL80211_BAND_6GHZ];
13110 
13111 	if (!sband || idx >= sband->n_channels)
13112 		return -ENOENT;
13113 
13114 	ar = ath12k_mac_get_ar_by_chan(hw, &sband->channels[idx]);
13115 	if (!ar) {
13116 		if (sband->channels[idx].flags & IEEE80211_CHAN_DISABLED) {
13117 			memset(survey, 0, sizeof(*survey));
13118 			return 0;
13119 		}
13120 		return -ENOENT;
13121 	}
13122 
13123 	ar_survey = &ar->survey[idx];
13124 
13125 	ath12k_mac_update_bss_chan_survey(ar, &sband->channels[idx]);
13126 
13127 	spin_lock_bh(&ar->data_lock);
13128 	memcpy(survey, ar_survey, sizeof(*survey));
13129 	spin_unlock_bh(&ar->data_lock);
13130 
13131 	survey->channel = &sband->channels[idx];
13132 
13133 	if (ar->rx_channel == survey->channel)
13134 		survey->filled |= SURVEY_INFO_IN_USE;
13135 
13136 	return 0;
13137 }
13138 
13139 static void ath12k_mac_put_chain_rssi(struct station_info *sinfo,
13140 				      struct ath12k_link_sta *arsta)
13141 {
13142 	s8 rssi;
13143 	int i;
13144 
13145 	for (i = 0; i < ARRAY_SIZE(sinfo->chain_signal); i++) {
13146 		sinfo->chains &= ~BIT(i);
13147 		rssi = arsta->chain_signal[i];
13148 
13149 		if (rssi != ATH12K_DEFAULT_NOISE_FLOOR &&
13150 		    rssi != ATH12K_INVALID_RSSI_FULL &&
13151 		    rssi != ATH12K_INVALID_RSSI_EMPTY &&
13152 		    rssi != 0) {
13153 			sinfo->chain_signal[i] = rssi;
13154 			sinfo->chains |= BIT(i);
13155 			sinfo->filled |= BIT_ULL(NL80211_STA_INFO_CHAIN_SIGNAL);
13156 		}
13157 	}
13158 }
13159 
13160 static void ath12k_mac_op_sta_statistics(struct ieee80211_hw *hw,
13161 					 struct ieee80211_vif *vif,
13162 					 struct ieee80211_sta *sta,
13163 					 struct station_info *sinfo)
13164 {
13165 	struct ath12k_sta *ahsta = ath12k_sta_to_ahsta(sta);
13166 	struct ath12k_fw_stats_req_params params = {};
13167 	struct ath12k_link_sta *arsta;
13168 	s8 signal, noise_floor;
13169 	struct ath12k *ar;
13170 	bool db2dbm;
13171 
13172 	lockdep_assert_wiphy(hw->wiphy);
13173 
13174 	arsta = &ahsta->deflink;
13175 	ar = ath12k_get_ar_by_vif(hw, vif, arsta->link_id);
13176 	if (!ar)
13177 		return;
13178 
13179 	db2dbm = test_bit(WMI_TLV_SERVICE_HW_DB2DBM_CONVERSION_SUPPORT,
13180 			  ar->ab->wmi_ab.svc_map);
13181 
13182 	sinfo->rx_duration = arsta->rx_duration;
13183 	sinfo->filled |= BIT_ULL(NL80211_STA_INFO_RX_DURATION);
13184 
13185 	sinfo->tx_duration = arsta->tx_duration;
13186 	sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_DURATION);
13187 
13188 	if (arsta->txrate.legacy || arsta->txrate.nss) {
13189 		if (arsta->txrate.legacy) {
13190 			sinfo->txrate.legacy = arsta->txrate.legacy;
13191 		} else {
13192 			sinfo->txrate.mcs = arsta->txrate.mcs;
13193 			sinfo->txrate.nss = arsta->txrate.nss;
13194 			sinfo->txrate.bw = arsta->txrate.bw;
13195 			sinfo->txrate.he_gi = arsta->txrate.he_gi;
13196 			sinfo->txrate.he_dcm = arsta->txrate.he_dcm;
13197 			sinfo->txrate.he_ru_alloc = arsta->txrate.he_ru_alloc;
13198 			sinfo->txrate.eht_gi = arsta->txrate.eht_gi;
13199 			sinfo->txrate.eht_ru_alloc = arsta->txrate.eht_ru_alloc;
13200 		}
13201 		sinfo->txrate.flags = arsta->txrate.flags;
13202 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_BITRATE);
13203 	}
13204 
13205 	/* TODO: Use real NF instead of default one. */
13206 	signal = arsta->rssi_comb;
13207 
13208 	params.pdev_id = ar->pdev->pdev_id;
13209 	params.vdev_id = 0;
13210 	params.stats_id = WMI_REQUEST_VDEV_STAT;
13211 
13212 	if (!signal &&
13213 	    ahsta->ahvif->vdev_type == WMI_VDEV_TYPE_STA &&
13214 	    !(ath12k_mac_get_fw_stats(ar, &params))) {
13215 		signal = arsta->rssi_beacon;
13216 		ath12k_fw_stats_reset(ar);
13217 	}
13218 
13219 	params.stats_id = WMI_REQUEST_RSSI_PER_CHAIN_STAT;
13220 	if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_CHAIN_SIGNAL)) &&
13221 	    ahsta->ahvif->vdev_type == WMI_VDEV_TYPE_STA &&
13222 	    !(ath12k_mac_get_fw_stats(ar, &params))) {
13223 		ath12k_mac_put_chain_rssi(sinfo, arsta);
13224 		ath12k_fw_stats_reset(ar);
13225 	}
13226 
13227 	spin_lock_bh(&ar->data_lock);
13228 	noise_floor = ath12k_pdev_get_noise_floor(ar);
13229 	spin_unlock_bh(&ar->data_lock);
13230 
13231 	if (signal) {
13232 		sinfo->signal = db2dbm ? signal : signal + noise_floor;
13233 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_SIGNAL);
13234 	}
13235 
13236 	sinfo->signal_avg = ewma_avg_rssi_read(&arsta->avg_rssi);
13237 
13238 	if (!db2dbm)
13239 		sinfo->signal_avg += noise_floor;
13240 
13241 	sinfo->filled |= BIT_ULL(NL80211_STA_INFO_SIGNAL_AVG);
13242 
13243 	sinfo->tx_retries = arsta->tx_retry_count;
13244 	sinfo->tx_failed = arsta->tx_retry_failed;
13245 	sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_RETRIES);
13246 	sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_FAILED);
13247 }
13248 
13249 static void ath12k_mac_op_link_sta_statistics(struct ieee80211_hw *hw,
13250 					      struct ieee80211_vif *vif,
13251 					      struct ieee80211_link_sta *link_sta,
13252 					      struct link_station_info *link_sinfo)
13253 {
13254 	struct ath12k_sta *ahsta = ath12k_sta_to_ahsta(link_sta->sta);
13255 	struct ath12k_fw_stats_req_params params = {};
13256 	struct ath12k_link_sta *arsta;
13257 	struct ath12k *ar;
13258 	s8 signal;
13259 	bool db2dbm;
13260 
13261 	lockdep_assert_wiphy(hw->wiphy);
13262 
13263 	arsta = wiphy_dereference(hw->wiphy, ahsta->link[link_sta->link_id]);
13264 
13265 	if (!arsta)
13266 		return;
13267 
13268 	ar = ath12k_get_ar_by_vif(hw, vif, arsta->link_id);
13269 	if (!ar)
13270 		return;
13271 
13272 	db2dbm = test_bit(WMI_TLV_SERVICE_HW_DB2DBM_CONVERSION_SUPPORT,
13273 			  ar->ab->wmi_ab.svc_map);
13274 
13275 	link_sinfo->rx_duration = arsta->rx_duration;
13276 	link_sinfo->filled |= BIT_ULL(NL80211_STA_INFO_RX_DURATION);
13277 
13278 	link_sinfo->tx_duration = arsta->tx_duration;
13279 	link_sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_DURATION);
13280 
13281 	if (arsta->txrate.legacy || arsta->txrate.nss) {
13282 		if (arsta->txrate.legacy) {
13283 			link_sinfo->txrate.legacy = arsta->txrate.legacy;
13284 		} else {
13285 			link_sinfo->txrate.mcs = arsta->txrate.mcs;
13286 			link_sinfo->txrate.nss = arsta->txrate.nss;
13287 			link_sinfo->txrate.bw = arsta->txrate.bw;
13288 			link_sinfo->txrate.he_gi = arsta->txrate.he_gi;
13289 			link_sinfo->txrate.he_dcm = arsta->txrate.he_dcm;
13290 			link_sinfo->txrate.he_ru_alloc =
13291 				arsta->txrate.he_ru_alloc;
13292 			link_sinfo->txrate.eht_gi = arsta->txrate.eht_gi;
13293 			link_sinfo->txrate.eht_ru_alloc =
13294 				arsta->txrate.eht_ru_alloc;
13295 		}
13296 		link_sinfo->txrate.flags = arsta->txrate.flags;
13297 		link_sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_BITRATE);
13298 	}
13299 
13300 	/* TODO: Use real NF instead of default one. */
13301 	signal = arsta->rssi_comb;
13302 
13303 	params.pdev_id = ar->pdev->pdev_id;
13304 	params.vdev_id = 0;
13305 	params.stats_id = WMI_REQUEST_VDEV_STAT;
13306 
13307 	if (!signal &&
13308 	    ahsta->ahvif->vdev_type == WMI_VDEV_TYPE_STA &&
13309 	    !(ath12k_mac_get_fw_stats(ar, &params))) {
13310 		signal = arsta->rssi_beacon;
13311 		ath12k_fw_stats_reset(ar);
13312 	}
13313 
13314 	if (signal) {
13315 		link_sinfo->signal =
13316 			db2dbm ? signal : signal + ATH12K_DEFAULT_NOISE_FLOOR;
13317 		link_sinfo->filled |= BIT_ULL(NL80211_STA_INFO_SIGNAL);
13318 	}
13319 
13320 	link_sinfo->signal_avg = ewma_avg_rssi_read(&arsta->avg_rssi);
13321 
13322 	if (!db2dbm)
13323 		link_sinfo->signal_avg += ATH12K_DEFAULT_NOISE_FLOOR;
13324 
13325 	link_sinfo->filled |= BIT_ULL(NL80211_STA_INFO_SIGNAL_AVG);
13326 
13327 	link_sinfo->tx_retries = arsta->tx_retry_count;
13328 	link_sinfo->tx_failed = arsta->tx_retry_failed;
13329 	link_sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_RETRIES);
13330 	link_sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_FAILED);
13331 }
13332 
13333 static int ath12k_mac_op_cancel_remain_on_channel(struct ieee80211_hw *hw,
13334 						  struct ieee80211_vif *vif)
13335 {
13336 	struct ath12k_hw *ah = ath12k_hw_to_ah(hw);
13337 	struct ath12k *ar;
13338 
13339 	ar = ath12k_ah_to_ar(ah, 0);
13340 
13341 	lockdep_assert_wiphy(hw->wiphy);
13342 
13343 	spin_lock_bh(&ar->data_lock);
13344 	ar->scan.roc_notify = false;
13345 	spin_unlock_bh(&ar->data_lock);
13346 
13347 	ath12k_scan_abort(ar);
13348 
13349 	cancel_delayed_work_sync(&ar->scan.timeout);
13350 	wiphy_work_flush(hw->wiphy, &ar->scan.vdev_clean_wk);
13351 
13352 	return 0;
13353 }
13354 
13355 static int ath12k_mac_op_remain_on_channel(struct ieee80211_hw *hw,
13356 					   struct ieee80211_vif *vif,
13357 					   struct ieee80211_channel *chan,
13358 					   int duration,
13359 					   enum ieee80211_roc_type type)
13360 {
13361 	struct ath12k_vif *ahvif = ath12k_vif_to_ahvif(vif);
13362 	struct ath12k_hw *ah = ath12k_hw_to_ah(hw);
13363 	struct ath12k_link_vif *arvif;
13364 	struct ath12k *ar;
13365 	u32 scan_time_msec;
13366 	bool create = true;
13367 	u8 link_id;
13368 	int ret;
13369 
13370 	lockdep_assert_wiphy(hw->wiphy);
13371 
13372 	ar = ath12k_mac_select_scan_device(hw, vif, chan->center_freq);
13373 	if (!ar)
13374 		return -EINVAL;
13375 
13376 	/* check if any of the links of ML VIF is already started on
13377 	 * radio(ar) corresponding to given scan frequency and use it,
13378 	 * if not use deflink(link 0) for scan purpose.
13379 	 */
13380 
13381 	link_id = ath12k_mac_find_link_id_by_ar(ahvif, ar);
13382 	arvif = ath12k_mac_assign_link_vif(ah, vif, link_id);
13383 	/* If the vif is already assigned to a specific vdev of an ar,
13384 	 * check whether its already started, vdev which is started
13385 	 * are not allowed to switch to a new radio.
13386 	 * If the vdev is not started, but was earlier created on a
13387 	 * different ar, delete that vdev and create a new one. We don't
13388 	 * delete at the scan stop as an optimization to avoid redundant
13389 	 * delete-create vdev's for the same ar, in case the request is
13390 	 * always on the same band for the vif
13391 	 */
13392 	if (arvif->is_created) {
13393 		if (WARN_ON(!arvif->ar))
13394 			return -EINVAL;
13395 
13396 		if (ar != arvif->ar && arvif->is_started)
13397 			return -EBUSY;
13398 
13399 		if (ar != arvif->ar) {
13400 			ath12k_mac_remove_link_interface(hw, arvif);
13401 			ath12k_mac_unassign_link_vif(arvif);
13402 		} else {
13403 			create = false;
13404 		}
13405 	}
13406 
13407 	if (create) {
13408 		arvif = ath12k_mac_assign_link_vif(ah, vif, link_id);
13409 
13410 		ret = ath12k_mac_vdev_create(ar, arvif);
13411 		if (ret) {
13412 			ath12k_warn(ar->ab, "unable to create scan vdev for roc: %d\n",
13413 				    ret);
13414 			ath12k_mac_unassign_link_vif(arvif);
13415 			return ret;
13416 		}
13417 	}
13418 
13419 	spin_lock_bh(&ar->data_lock);
13420 
13421 	switch (ar->scan.state) {
13422 	case ATH12K_SCAN_IDLE:
13423 		reinit_completion(&ar->scan.started);
13424 		reinit_completion(&ar->scan.completed);
13425 		reinit_completion(&ar->scan.on_channel);
13426 		ar->scan.state = ATH12K_SCAN_STARTING;
13427 		ar->scan.is_roc = true;
13428 		ar->scan.arvif = arvif;
13429 		ar->scan.roc_freq = chan->center_freq;
13430 		ar->scan.roc_notify = true;
13431 		ret = 0;
13432 		break;
13433 	case ATH12K_SCAN_STARTING:
13434 	case ATH12K_SCAN_RUNNING:
13435 	case ATH12K_SCAN_ABORTING:
13436 		ret = -EBUSY;
13437 		break;
13438 	}
13439 
13440 	spin_unlock_bh(&ar->data_lock);
13441 
13442 	if (ret)
13443 		return ret;
13444 
13445 	scan_time_msec = hw->wiphy->max_remain_on_channel_duration * 2;
13446 
13447 	struct ath12k_wmi_scan_req_arg *arg __free(kfree) =
13448 					kzalloc(sizeof(*arg), GFP_KERNEL);
13449 	if (!arg)
13450 		return -ENOMEM;
13451 
13452 	ath12k_wmi_start_scan_init(ar, arg);
13453 	arg->num_chan = 1;
13454 
13455 	u32 *chan_list __free(kfree) = kcalloc(arg->num_chan, sizeof(*chan_list),
13456 					       GFP_KERNEL);
13457 	if (!chan_list)
13458 		return -ENOMEM;
13459 
13460 	arg->chan_list = chan_list;
13461 	arg->vdev_id = arvif->vdev_id;
13462 	arg->scan_id = ATH12K_SCAN_ID;
13463 	arg->chan_list[0] = chan->center_freq;
13464 	arg->dwell_time_active = scan_time_msec;
13465 	arg->dwell_time_passive = scan_time_msec;
13466 	arg->max_scan_time = scan_time_msec;
13467 	arg->scan_f_passive = 1;
13468 	arg->burst_duration = duration;
13469 
13470 	ret = ath12k_start_scan(ar, arg);
13471 	if (ret) {
13472 		ath12k_warn(ar->ab, "failed to start roc scan: %d\n", ret);
13473 
13474 		spin_lock_bh(&ar->data_lock);
13475 		ar->scan.state = ATH12K_SCAN_IDLE;
13476 		spin_unlock_bh(&ar->data_lock);
13477 		return ret;
13478 	}
13479 
13480 	ret = wait_for_completion_timeout(&ar->scan.on_channel, 3 * HZ);
13481 	if (ret == 0) {
13482 		ath12k_warn(ar->ab, "failed to switch to channel for roc scan\n");
13483 		ret = ath12k_scan_stop(ar);
13484 		if (ret)
13485 			ath12k_warn(ar->ab, "failed to stop scan: %d\n", ret);
13486 		return -ETIMEDOUT;
13487 	}
13488 
13489 	ieee80211_queue_delayed_work(hw, &ar->scan.timeout,
13490 				     msecs_to_jiffies(duration));
13491 
13492 	return 0;
13493 }
13494 
13495 static void ath12k_mac_op_set_rekey_data(struct ieee80211_hw *hw,
13496 					 struct ieee80211_vif *vif,
13497 					 struct cfg80211_gtk_rekey_data *data)
13498 {
13499 	struct ath12k_vif *ahvif = ath12k_vif_to_ahvif(vif);
13500 	struct ath12k_rekey_data *rekey_data;
13501 	struct ath12k_hw *ah = ath12k_hw_to_ah(hw);
13502 	struct ath12k *ar = ath12k_ah_to_ar(ah, 0);
13503 	struct ath12k_link_vif *arvif;
13504 
13505 	lockdep_assert_wiphy(hw->wiphy);
13506 
13507 	arvif = &ahvif->deflink;
13508 	rekey_data = &arvif->rekey_data;
13509 
13510 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac set rekey data vdev %d\n",
13511 		   arvif->vdev_id);
13512 
13513 	memcpy(rekey_data->kck, data->kck, NL80211_KCK_LEN);
13514 	memcpy(rekey_data->kek, data->kek, NL80211_KEK_LEN);
13515 
13516 	/* The supplicant works on big-endian, the firmware expects it on
13517 	 * little endian.
13518 	 */
13519 	rekey_data->replay_ctr = get_unaligned_be64(data->replay_ctr);
13520 
13521 	arvif->rekey_data.enable_offload = true;
13522 
13523 	ath12k_dbg_dump(ar->ab, ATH12K_DBG_MAC, "kck", NULL,
13524 			rekey_data->kck, NL80211_KCK_LEN);
13525 	ath12k_dbg_dump(ar->ab, ATH12K_DBG_MAC, "kek", NULL,
13526 			rekey_data->kck, NL80211_KEK_LEN);
13527 	ath12k_dbg_dump(ar->ab, ATH12K_DBG_MAC, "replay ctr", NULL,
13528 			&rekey_data->replay_ctr, sizeof(rekey_data->replay_ctr));
13529 }
13530 
13531 static const struct ieee80211_ops ath12k_ops = {
13532 	.tx				= ath12k_mac_op_tx,
13533 	.wake_tx_queue			= ieee80211_handle_wake_tx_queue,
13534 	.start                          = ath12k_mac_op_start,
13535 	.stop                           = ath12k_mac_op_stop,
13536 	.reconfig_complete              = ath12k_mac_op_reconfig_complete,
13537 	.add_interface                  = ath12k_mac_op_add_interface,
13538 	.remove_interface		= ath12k_mac_op_remove_interface,
13539 	.update_vif_offload		= ath12k_mac_op_update_vif_offload,
13540 	.config                         = ath12k_mac_op_config,
13541 	.link_info_changed              = ath12k_mac_op_link_info_changed,
13542 	.vif_cfg_changed		= ath12k_mac_op_vif_cfg_changed,
13543 	.change_vif_links               = ath12k_mac_op_change_vif_links,
13544 	.configure_filter		= ath12k_mac_op_configure_filter,
13545 	.hw_scan                        = ath12k_mac_op_hw_scan,
13546 	.cancel_hw_scan                 = ath12k_mac_op_cancel_hw_scan,
13547 	.set_key                        = ath12k_mac_op_set_key,
13548 	.set_rekey_data	                = ath12k_mac_op_set_rekey_data,
13549 	.sta_state                      = ath12k_mac_op_sta_state,
13550 	.sta_set_txpwr			= ath12k_mac_op_sta_set_txpwr,
13551 	.link_sta_rc_update		= ath12k_mac_op_link_sta_rc_update,
13552 	.conf_tx                        = ath12k_mac_op_conf_tx,
13553 	.set_antenna			= ath12k_mac_op_set_antenna,
13554 	.get_antenna			= ath12k_mac_op_get_antenna,
13555 	.ampdu_action			= ath12k_mac_op_ampdu_action,
13556 	.add_chanctx			= ath12k_mac_op_add_chanctx,
13557 	.remove_chanctx			= ath12k_mac_op_remove_chanctx,
13558 	.change_chanctx			= ath12k_mac_op_change_chanctx,
13559 	.assign_vif_chanctx		= ath12k_mac_op_assign_vif_chanctx,
13560 	.unassign_vif_chanctx		= ath12k_mac_op_unassign_vif_chanctx,
13561 	.switch_vif_chanctx		= ath12k_mac_op_switch_vif_chanctx,
13562 	.get_txpower			= ath12k_mac_op_get_txpower,
13563 	.set_rts_threshold		= ath12k_mac_op_set_rts_threshold,
13564 	.set_frag_threshold		= ath12k_mac_op_set_frag_threshold,
13565 	.set_bitrate_mask		= ath12k_mac_op_set_bitrate_mask,
13566 	.get_survey			= ath12k_mac_op_get_survey,
13567 	.flush				= ath12k_mac_op_flush,
13568 	.sta_statistics			= ath12k_mac_op_sta_statistics,
13569 	.link_sta_statistics		= ath12k_mac_op_link_sta_statistics,
13570 	.remain_on_channel              = ath12k_mac_op_remain_on_channel,
13571 	.cancel_remain_on_channel       = ath12k_mac_op_cancel_remain_on_channel,
13572 	.change_sta_links               = ath12k_mac_op_change_sta_links,
13573 	.can_activate_links             = ath12k_mac_op_can_activate_links,
13574 #ifdef CONFIG_PM
13575 	.suspend			= ath12k_wow_op_suspend,
13576 	.resume				= ath12k_wow_op_resume,
13577 	.set_wakeup			= ath12k_wow_op_set_wakeup,
13578 #endif
13579 #ifdef CONFIG_ATH12K_DEBUGFS
13580 	.vif_add_debugfs                = ath12k_debugfs_op_vif_add,
13581 #endif
13582 	CFG80211_TESTMODE_CMD(ath12k_tm_cmd)
13583 #ifdef CONFIG_ATH12K_DEBUGFS
13584 	.link_sta_add_debugfs           = ath12k_debugfs_link_sta_op_add,
13585 #endif
13586 };
13587 
13588 void ath12k_mac_update_freq_range(struct ath12k *ar,
13589 				  u32 freq_low, u32 freq_high)
13590 {
13591 	if (!(freq_low && freq_high))
13592 		return;
13593 
13594 	if (ar->freq_range.start_freq || ar->freq_range.end_freq) {
13595 		ar->freq_range.start_freq = min(ar->freq_range.start_freq,
13596 						MHZ_TO_KHZ(freq_low));
13597 		ar->freq_range.end_freq = max(ar->freq_range.end_freq,
13598 					      MHZ_TO_KHZ(freq_high));
13599 	} else {
13600 		ar->freq_range.start_freq = MHZ_TO_KHZ(freq_low);
13601 		ar->freq_range.end_freq = MHZ_TO_KHZ(freq_high);
13602 	}
13603 
13604 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
13605 		   "mac pdev %u freq limit updated. New range %u->%u MHz\n",
13606 		   ar->pdev->pdev_id, KHZ_TO_MHZ(ar->freq_range.start_freq),
13607 		   KHZ_TO_MHZ(ar->freq_range.end_freq));
13608 }
13609 
13610 static void ath12k_mac_update_ch_list(struct ath12k *ar,
13611 				      struct ieee80211_supported_band *band,
13612 				      u32 freq_low, u32 freq_high)
13613 {
13614 	int i;
13615 
13616 	if (!(freq_low && freq_high))
13617 		return;
13618 
13619 	for (i = 0; i < band->n_channels; i++) {
13620 		if (band->channels[i].center_freq < freq_low ||
13621 		    band->channels[i].center_freq > freq_high)
13622 			band->channels[i].flags |= IEEE80211_CHAN_DISABLED;
13623 	}
13624 }
13625 
13626 static u32 ath12k_get_phy_id(struct ath12k *ar, u32 band)
13627 {
13628 	struct ath12k_pdev *pdev = ar->pdev;
13629 	struct ath12k_pdev_cap *pdev_cap = &pdev->cap;
13630 
13631 	if (band == WMI_HOST_WLAN_2GHZ_CAP)
13632 		return pdev_cap->band[NL80211_BAND_2GHZ].phy_id;
13633 
13634 	if (band == WMI_HOST_WLAN_5GHZ_CAP)
13635 		return pdev_cap->band[NL80211_BAND_5GHZ].phy_id;
13636 
13637 	ath12k_warn(ar->ab, "unsupported phy cap:%d\n", band);
13638 
13639 	return 0;
13640 }
13641 
13642 static int ath12k_mac_update_band(struct ath12k *ar,
13643 				  struct ieee80211_supported_band *orig_band,
13644 				  struct ieee80211_supported_band *new_band)
13645 {
13646 	int i;
13647 
13648 	if (!orig_band || !new_band)
13649 		return -EINVAL;
13650 
13651 	if (orig_band->band != new_band->band)
13652 		return -EINVAL;
13653 
13654 	for (i = 0; i < new_band->n_channels; i++) {
13655 		if (new_band->channels[i].flags & IEEE80211_CHAN_DISABLED)
13656 			continue;
13657 		/* An enabled channel in new_band should not be already enabled
13658 		 * in the orig_band
13659 		 */
13660 		if (WARN_ON(!(orig_band->channels[i].flags &
13661 			      IEEE80211_CHAN_DISABLED)))
13662 			return -EINVAL;
13663 		orig_band->channels[i].flags &= ~IEEE80211_CHAN_DISABLED;
13664 	}
13665 	return 0;
13666 }
13667 
13668 static int ath12k_mac_setup_channels_rates(struct ath12k *ar,
13669 					   u32 supported_bands,
13670 					   struct ieee80211_supported_band *bands[])
13671 {
13672 	struct ieee80211_supported_band *band;
13673 	struct ath12k_wmi_hal_reg_capabilities_ext_arg *reg_cap;
13674 	struct ath12k_base *ab = ar->ab;
13675 	u32 phy_id, freq_low, freq_high;
13676 	struct ath12k_hw *ah = ar->ah;
13677 	void *channels;
13678 	int ret;
13679 
13680 	BUILD_BUG_ON((ARRAY_SIZE(ath12k_2ghz_channels) +
13681 		      ARRAY_SIZE(ath12k_5ghz_channels) +
13682 		      ARRAY_SIZE(ath12k_6ghz_channels)) !=
13683 		     ATH12K_NUM_CHANS);
13684 
13685 	reg_cap = &ab->hal_reg_cap[ar->pdev_idx];
13686 
13687 	if (supported_bands & WMI_HOST_WLAN_2GHZ_CAP) {
13688 		channels = kmemdup(ath12k_2ghz_channels,
13689 				   sizeof(ath12k_2ghz_channels),
13690 				   GFP_KERNEL);
13691 		if (!channels)
13692 			return -ENOMEM;
13693 
13694 		band = &ar->mac.sbands[NL80211_BAND_2GHZ];
13695 		band->band = NL80211_BAND_2GHZ;
13696 		band->n_channels = ARRAY_SIZE(ath12k_2ghz_channels);
13697 		band->channels = channels;
13698 		band->n_bitrates = ath12k_g_rates_size;
13699 		band->bitrates = ath12k_g_rates;
13700 
13701 		if (ab->hw_params->single_pdev_only) {
13702 			phy_id = ath12k_get_phy_id(ar, WMI_HOST_WLAN_2GHZ_CAP);
13703 			reg_cap = &ab->hal_reg_cap[phy_id];
13704 		}
13705 
13706 		freq_low = max(reg_cap->low_2ghz_chan,
13707 			       ab->reg_freq_2ghz.start_freq);
13708 		freq_high = min(reg_cap->high_2ghz_chan,
13709 				ab->reg_freq_2ghz.end_freq);
13710 
13711 		ath12k_mac_update_ch_list(ar, band,
13712 					  reg_cap->low_2ghz_chan,
13713 					  reg_cap->high_2ghz_chan);
13714 
13715 		ath12k_mac_update_freq_range(ar, freq_low, freq_high);
13716 
13717 		if (!bands[NL80211_BAND_2GHZ]) {
13718 			bands[NL80211_BAND_2GHZ] = band;
13719 		} else {
13720 			/* Split mac in same band under same wiphy */
13721 			ret = ath12k_mac_update_band(ar, bands[NL80211_BAND_2GHZ], band);
13722 			if (ret) {
13723 				kfree(channels);
13724 				band->channels = NULL;
13725 				return ret;
13726 			}
13727 			ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac pdev %u identified as 2 GHz split mac with start freq %d end freq %d",
13728 				   ar->pdev->pdev_id,
13729 				   KHZ_TO_MHZ(ar->freq_range.start_freq),
13730 				   KHZ_TO_MHZ(ar->freq_range.end_freq));
13731 		}
13732 	}
13733 
13734 	if (supported_bands & WMI_HOST_WLAN_5GHZ_CAP) {
13735 		if (reg_cap->high_5ghz_chan >= ATH12K_MIN_6GHZ_FREQ) {
13736 			channels = kmemdup(ath12k_6ghz_channels,
13737 					   sizeof(ath12k_6ghz_channels), GFP_KERNEL);
13738 			if (!channels) {
13739 				kfree(ar->mac.sbands[NL80211_BAND_2GHZ].channels);
13740 				return -ENOMEM;
13741 			}
13742 
13743 			ar->supports_6ghz = true;
13744 			band = &ar->mac.sbands[NL80211_BAND_6GHZ];
13745 			band->band = NL80211_BAND_6GHZ;
13746 			band->n_channels = ARRAY_SIZE(ath12k_6ghz_channels);
13747 			band->channels = channels;
13748 			band->n_bitrates = ath12k_a_rates_size;
13749 			band->bitrates = ath12k_a_rates;
13750 
13751 			freq_low = max(reg_cap->low_5ghz_chan,
13752 				       ab->reg_freq_6ghz.start_freq);
13753 			freq_high = min(reg_cap->high_5ghz_chan,
13754 					ab->reg_freq_6ghz.end_freq);
13755 
13756 			ath12k_mac_update_ch_list(ar, band,
13757 						  reg_cap->low_5ghz_chan,
13758 						  reg_cap->high_5ghz_chan);
13759 
13760 			ath12k_mac_update_freq_range(ar, freq_low, freq_high);
13761 			ah->use_6ghz_regd = true;
13762 
13763 			if (!bands[NL80211_BAND_6GHZ]) {
13764 				bands[NL80211_BAND_6GHZ] = band;
13765 			} else {
13766 				/* Split mac in same band under same wiphy */
13767 				ret = ath12k_mac_update_band(ar,
13768 							     bands[NL80211_BAND_6GHZ],
13769 							     band);
13770 				if (ret) {
13771 					kfree(ar->mac.sbands[NL80211_BAND_2GHZ].channels);
13772 					ar->mac.sbands[NL80211_BAND_2GHZ].channels = NULL;
13773 					kfree(channels);
13774 					band->channels = NULL;
13775 					return ret;
13776 				}
13777 				ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac pdev %u identified as 6 GHz split mac with start freq %d end freq %d",
13778 					   ar->pdev->pdev_id,
13779 					   KHZ_TO_MHZ(ar->freq_range.start_freq),
13780 					   KHZ_TO_MHZ(ar->freq_range.end_freq));
13781 			}
13782 		}
13783 
13784 		if (reg_cap->low_5ghz_chan < ATH12K_MIN_6GHZ_FREQ) {
13785 			channels = kmemdup(ath12k_5ghz_channels,
13786 					   sizeof(ath12k_5ghz_channels),
13787 					   GFP_KERNEL);
13788 			if (!channels) {
13789 				kfree(ar->mac.sbands[NL80211_BAND_2GHZ].channels);
13790 				kfree(ar->mac.sbands[NL80211_BAND_6GHZ].channels);
13791 				return -ENOMEM;
13792 			}
13793 
13794 			band = &ar->mac.sbands[NL80211_BAND_5GHZ];
13795 			band->band = NL80211_BAND_5GHZ;
13796 			band->n_channels = ARRAY_SIZE(ath12k_5ghz_channels);
13797 			band->channels = channels;
13798 			band->n_bitrates = ath12k_a_rates_size;
13799 			band->bitrates = ath12k_a_rates;
13800 
13801 			if (ab->hw_params->single_pdev_only) {
13802 				phy_id = ath12k_get_phy_id(ar, WMI_HOST_WLAN_5GHZ_CAP);
13803 				reg_cap = &ab->hal_reg_cap[phy_id];
13804 			}
13805 
13806 			freq_low = max(reg_cap->low_5ghz_chan,
13807 				       ab->reg_freq_5ghz.start_freq);
13808 			freq_high = min(reg_cap->high_5ghz_chan,
13809 					ab->reg_freq_5ghz.end_freq);
13810 
13811 			ath12k_mac_update_ch_list(ar, band,
13812 						  reg_cap->low_5ghz_chan,
13813 						  reg_cap->high_5ghz_chan);
13814 
13815 			ath12k_mac_update_freq_range(ar, freq_low, freq_high);
13816 
13817 			if (!bands[NL80211_BAND_5GHZ]) {
13818 				bands[NL80211_BAND_5GHZ] = band;
13819 			} else {
13820 				/* Split mac in same band under same wiphy */
13821 				ret = ath12k_mac_update_band(ar,
13822 							     bands[NL80211_BAND_5GHZ],
13823 							     band);
13824 				if (ret) {
13825 					kfree(ar->mac.sbands[NL80211_BAND_2GHZ].channels);
13826 					ar->mac.sbands[NL80211_BAND_2GHZ].channels = NULL;
13827 					kfree(ar->mac.sbands[NL80211_BAND_6GHZ].channels);
13828 					ar->mac.sbands[NL80211_BAND_2GHZ].channels = NULL;
13829 					kfree(channels);
13830 					band->channels = NULL;
13831 					return ret;
13832 				}
13833 				ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac pdev %u identified as 5 GHz split mac with start freq %d end freq %d",
13834 					   ar->pdev->pdev_id,
13835 					   KHZ_TO_MHZ(ar->freq_range.start_freq),
13836 					   KHZ_TO_MHZ(ar->freq_range.end_freq));
13837 			}
13838 		}
13839 	}
13840 
13841 	return 0;
13842 }
13843 
13844 static u16 ath12k_mac_get_ifmodes(struct ath12k_hw *ah)
13845 {
13846 	struct ath12k *ar;
13847 	int i;
13848 	u16 interface_modes = U16_MAX;
13849 
13850 	for_each_ar(ah, ar, i)
13851 		interface_modes &= ar->ab->hw_params->interface_modes;
13852 
13853 	return interface_modes == U16_MAX ? 0 : interface_modes;
13854 }
13855 
13856 static bool ath12k_mac_is_iface_mode_enable(struct ath12k_hw *ah,
13857 					    enum nl80211_iftype type)
13858 {
13859 	struct ath12k *ar;
13860 	int i;
13861 	u16 interface_modes, mode = 0;
13862 	bool is_enable = false;
13863 
13864 	if (type == NL80211_IFTYPE_MESH_POINT) {
13865 		if (IS_ENABLED(CONFIG_MAC80211_MESH))
13866 			mode = BIT(type);
13867 	} else {
13868 		mode = BIT(type);
13869 	}
13870 
13871 	for_each_ar(ah, ar, i) {
13872 		interface_modes = ar->ab->hw_params->interface_modes;
13873 		if (interface_modes & mode) {
13874 			is_enable = true;
13875 			break;
13876 		}
13877 	}
13878 
13879 	return is_enable;
13880 }
13881 
13882 static int
13883 ath12k_mac_setup_radio_iface_comb(struct ath12k *ar,
13884 				  struct ieee80211_iface_combination *comb)
13885 {
13886 	u16 interface_modes = ar->ab->hw_params->interface_modes;
13887 	struct ieee80211_iface_limit *limits;
13888 	int n_limits, max_interfaces;
13889 	bool ap, mesh, p2p;
13890 
13891 	ap = interface_modes & BIT(NL80211_IFTYPE_AP);
13892 	p2p = interface_modes & BIT(NL80211_IFTYPE_P2P_DEVICE);
13893 
13894 	mesh = IS_ENABLED(CONFIG_MAC80211_MESH) &&
13895 	       (interface_modes & BIT(NL80211_IFTYPE_MESH_POINT));
13896 
13897 	if ((ap || mesh) && !p2p) {
13898 		n_limits = 2;
13899 		max_interfaces = 16;
13900 	} else if (p2p) {
13901 		n_limits = 3;
13902 		if (ap || mesh)
13903 			max_interfaces = 16;
13904 		else
13905 			max_interfaces = 3;
13906 	} else {
13907 		n_limits = 1;
13908 		max_interfaces = 1;
13909 	}
13910 
13911 	limits = kcalloc(n_limits, sizeof(*limits), GFP_KERNEL);
13912 	if (!limits)
13913 		return -ENOMEM;
13914 
13915 	limits[0].max = 1;
13916 	limits[0].types |= BIT(NL80211_IFTYPE_STATION);
13917 
13918 	if (ap || mesh || p2p)
13919 		limits[1].max = max_interfaces;
13920 
13921 	if (ap)
13922 		limits[1].types |= BIT(NL80211_IFTYPE_AP);
13923 
13924 	if (mesh)
13925 		limits[1].types |= BIT(NL80211_IFTYPE_MESH_POINT);
13926 
13927 	if (p2p) {
13928 		limits[1].types |= BIT(NL80211_IFTYPE_P2P_CLIENT) |
13929 					BIT(NL80211_IFTYPE_P2P_GO);
13930 		limits[2].max = 1;
13931 		limits[2].types |= BIT(NL80211_IFTYPE_P2P_DEVICE);
13932 	}
13933 
13934 	comb[0].limits = limits;
13935 	comb[0].n_limits = n_limits;
13936 	comb[0].max_interfaces = max_interfaces;
13937 	comb[0].beacon_int_infra_match = true;
13938 	comb[0].beacon_int_min_gcd = 100;
13939 
13940 	comb[0].num_different_channels = 1;
13941 	comb[0].radar_detect_widths = BIT(NL80211_CHAN_WIDTH_20_NOHT) |
13942 				      BIT(NL80211_CHAN_WIDTH_20) |
13943 				      BIT(NL80211_CHAN_WIDTH_40) |
13944 				      BIT(NL80211_CHAN_WIDTH_80) |
13945 				      BIT(NL80211_CHAN_WIDTH_160);
13946 
13947 	return 0;
13948 }
13949 
13950 static int
13951 ath12k_mac_setup_global_iface_comb(struct ath12k_hw *ah,
13952 				   struct wiphy_radio *radio,
13953 				   u8 n_radio,
13954 				   struct ieee80211_iface_combination *comb)
13955 {
13956 	const struct ieee80211_iface_combination *iter_comb;
13957 	struct ieee80211_iface_limit *limits;
13958 	int i, j, n_limits;
13959 	bool ap, mesh, p2p;
13960 
13961 	if (!n_radio)
13962 		return 0;
13963 
13964 	ap = ath12k_mac_is_iface_mode_enable(ah, NL80211_IFTYPE_AP);
13965 	p2p = ath12k_mac_is_iface_mode_enable(ah, NL80211_IFTYPE_P2P_DEVICE);
13966 	mesh = ath12k_mac_is_iface_mode_enable(ah, NL80211_IFTYPE_MESH_POINT);
13967 
13968 	if ((ap || mesh) && !p2p)
13969 		n_limits = 2;
13970 	else if (p2p)
13971 		n_limits = 3;
13972 	else
13973 		n_limits = 1;
13974 
13975 	limits = kcalloc(n_limits, sizeof(*limits), GFP_KERNEL);
13976 	if (!limits)
13977 		return -ENOMEM;
13978 
13979 	for (i = 0; i < n_radio; i++) {
13980 		iter_comb = radio[i].iface_combinations;
13981 		for (j = 0; j < iter_comb->n_limits && j < n_limits; j++) {
13982 			limits[j].types |= iter_comb->limits[j].types;
13983 			limits[j].max += iter_comb->limits[j].max;
13984 		}
13985 
13986 		comb->max_interfaces += iter_comb->max_interfaces;
13987 		comb->num_different_channels += iter_comb->num_different_channels;
13988 		comb->radar_detect_widths |= iter_comb->radar_detect_widths;
13989 	}
13990 
13991 	comb->limits = limits;
13992 	comb->n_limits = n_limits;
13993 	comb->beacon_int_infra_match = true;
13994 	comb->beacon_int_min_gcd = 100;
13995 
13996 	return 0;
13997 }
13998 
13999 static
14000 void ath12k_mac_cleanup_iface_comb(const struct ieee80211_iface_combination *iface_comb)
14001 {
14002 	kfree(iface_comb[0].limits);
14003 	kfree(iface_comb);
14004 }
14005 
14006 static void ath12k_mac_cleanup_iface_combinations(struct ath12k_hw *ah)
14007 {
14008 	struct wiphy *wiphy = ah->hw->wiphy;
14009 	const struct wiphy_radio *radio;
14010 	int i;
14011 
14012 	if (wiphy->n_radio > 0) {
14013 		radio = wiphy->radio;
14014 		for (i = 0; i < wiphy->n_radio; i++)
14015 			ath12k_mac_cleanup_iface_comb(radio[i].iface_combinations);
14016 
14017 		kfree(wiphy->radio);
14018 	}
14019 
14020 	ath12k_mac_cleanup_iface_comb(wiphy->iface_combinations);
14021 }
14022 
14023 static int ath12k_mac_setup_iface_combinations(struct ath12k_hw *ah)
14024 {
14025 	struct ieee80211_iface_combination *combinations, *comb;
14026 	struct wiphy *wiphy = ah->hw->wiphy;
14027 	struct wiphy_radio *radio;
14028 	int n_combinations = 1;
14029 	struct ath12k *ar;
14030 	int i, ret;
14031 
14032 	if (ah->num_radio == 1) {
14033 		ar = &ah->radio[0];
14034 
14035 		if (ar->ab->hw_params->single_pdev_only)
14036 			n_combinations = 2;
14037 
14038 		combinations = kcalloc(n_combinations, sizeof(*combinations),
14039 				       GFP_KERNEL);
14040 		if (!combinations)
14041 			return -ENOMEM;
14042 
14043 		ret = ath12k_mac_setup_radio_iface_comb(ar, combinations);
14044 		if (ret) {
14045 			ath12k_hw_warn(ah, "failed to setup radio interface combinations for one radio: %d",
14046 				       ret);
14047 			goto err_free_combinations;
14048 		}
14049 
14050 		if (ar->ab->hw_params->single_pdev_only) {
14051 			comb = combinations + 1;
14052 			memcpy(comb, combinations, sizeof(*comb));
14053 			comb->num_different_channels = 2;
14054 			comb->radar_detect_widths = 0;
14055 		}
14056 
14057 		goto out;
14058 	}
14059 
14060 	combinations = kcalloc(n_combinations, sizeof(*combinations), GFP_KERNEL);
14061 	if (!combinations)
14062 		return -ENOMEM;
14063 
14064 	/* there are multiple radios */
14065 
14066 	radio = kcalloc(ah->num_radio, sizeof(*radio), GFP_KERNEL);
14067 	if (!radio) {
14068 		ret = -ENOMEM;
14069 		goto err_free_combinations;
14070 	}
14071 
14072 	for_each_ar(ah, ar, i) {
14073 		comb = kzalloc(sizeof(*comb), GFP_KERNEL);
14074 		if (!comb) {
14075 			ret = -ENOMEM;
14076 			goto err_free_radios;
14077 		}
14078 
14079 		ret = ath12k_mac_setup_radio_iface_comb(ar, comb);
14080 		if (ret) {
14081 			ath12k_hw_warn(ah, "failed to setup radio interface combinations for radio %d: %d",
14082 				       i, ret);
14083 			kfree(comb);
14084 			goto err_free_radios;
14085 		}
14086 
14087 		radio[i].freq_range = &ar->freq_range;
14088 		radio[i].n_freq_range = 1;
14089 
14090 		radio[i].iface_combinations = comb;
14091 		radio[i].n_iface_combinations = 1;
14092 	}
14093 
14094 	ret = ath12k_mac_setup_global_iface_comb(ah, radio, ah->num_radio, combinations);
14095 	if (ret) {
14096 		ath12k_hw_warn(ah, "failed to setup global interface combinations: %d",
14097 			       ret);
14098 		goto err_free_all_radios;
14099 	}
14100 
14101 	wiphy->radio = radio;
14102 	wiphy->n_radio = ah->num_radio;
14103 
14104 out:
14105 	wiphy->iface_combinations = combinations;
14106 	wiphy->n_iface_combinations = n_combinations;
14107 
14108 	return 0;
14109 
14110 err_free_all_radios:
14111 	i = ah->num_radio;
14112 
14113 err_free_radios:
14114 	while (i--)
14115 		ath12k_mac_cleanup_iface_comb(radio[i].iface_combinations);
14116 
14117 	kfree(radio);
14118 
14119 err_free_combinations:
14120 	kfree(combinations);
14121 
14122 	return ret;
14123 }
14124 
14125 static const u8 ath12k_if_types_ext_capa[] = {
14126 	[0] = WLAN_EXT_CAPA1_EXT_CHANNEL_SWITCHING,
14127 	[2] = WLAN_EXT_CAPA3_MULTI_BSSID_SUPPORT,
14128 	[7] = WLAN_EXT_CAPA8_OPMODE_NOTIF,
14129 };
14130 
14131 static const u8 ath12k_if_types_ext_capa_sta[] = {
14132 	[0] = WLAN_EXT_CAPA1_EXT_CHANNEL_SWITCHING,
14133 	[2] = WLAN_EXT_CAPA3_MULTI_BSSID_SUPPORT,
14134 	[7] = WLAN_EXT_CAPA8_OPMODE_NOTIF,
14135 	[9] = WLAN_EXT_CAPA10_TWT_REQUESTER_SUPPORT,
14136 };
14137 
14138 static const u8 ath12k_if_types_ext_capa_ap[] = {
14139 	[0] = WLAN_EXT_CAPA1_EXT_CHANNEL_SWITCHING,
14140 	[2] = WLAN_EXT_CAPA3_MULTI_BSSID_SUPPORT,
14141 	[7] = WLAN_EXT_CAPA8_OPMODE_NOTIF,
14142 	[9] = WLAN_EXT_CAPA10_TWT_RESPONDER_SUPPORT,
14143 	[10] = WLAN_EXT_CAPA11_EMA_SUPPORT,
14144 };
14145 
14146 static struct wiphy_iftype_ext_capab ath12k_iftypes_ext_capa[] = {
14147 	{
14148 		.extended_capabilities = ath12k_if_types_ext_capa,
14149 		.extended_capabilities_mask = ath12k_if_types_ext_capa,
14150 		.extended_capabilities_len = sizeof(ath12k_if_types_ext_capa),
14151 	}, {
14152 		.iftype = NL80211_IFTYPE_STATION,
14153 		.extended_capabilities = ath12k_if_types_ext_capa_sta,
14154 		.extended_capabilities_mask = ath12k_if_types_ext_capa_sta,
14155 		.extended_capabilities_len =
14156 				sizeof(ath12k_if_types_ext_capa_sta),
14157 	}, {
14158 		.iftype = NL80211_IFTYPE_AP,
14159 		.extended_capabilities = ath12k_if_types_ext_capa_ap,
14160 		.extended_capabilities_mask = ath12k_if_types_ext_capa_ap,
14161 		.extended_capabilities_len =
14162 				sizeof(ath12k_if_types_ext_capa_ap),
14163 		.eml_capabilities = 0,
14164 		.mld_capa_and_ops = 0,
14165 	},
14166 };
14167 
14168 static void ath12k_mac_cleanup_unregister(struct ath12k *ar)
14169 {
14170 	idr_for_each(&ar->txmgmt_idr, ath12k_mac_tx_mgmt_pending_free, ar);
14171 	idr_destroy(&ar->txmgmt_idr);
14172 
14173 	kfree(ar->mac.sbands[NL80211_BAND_2GHZ].channels);
14174 	kfree(ar->mac.sbands[NL80211_BAND_5GHZ].channels);
14175 	kfree(ar->mac.sbands[NL80211_BAND_6GHZ].channels);
14176 }
14177 
14178 static void ath12k_mac_hw_unregister(struct ath12k_hw *ah)
14179 {
14180 	struct ieee80211_hw *hw = ah->hw;
14181 	struct ath12k *ar;
14182 	int i;
14183 
14184 	for_each_ar(ah, ar, i) {
14185 		cancel_work_sync(&ar->regd_channel_update_work);
14186 		cancel_work_sync(&ar->regd_update_work);
14187 		ath12k_debugfs_unregister(ar);
14188 		ath12k_fw_stats_reset(ar);
14189 	}
14190 
14191 	ieee80211_unregister_hw(hw);
14192 
14193 	for_each_ar(ah, ar, i)
14194 		ath12k_mac_cleanup_unregister(ar);
14195 
14196 	ath12k_mac_cleanup_iface_combinations(ah);
14197 
14198 	SET_IEEE80211_DEV(hw, NULL);
14199 }
14200 
14201 static int ath12k_mac_setup_register(struct ath12k *ar,
14202 				     u32 *ht_cap,
14203 				     struct ieee80211_supported_band *bands[])
14204 {
14205 	struct ath12k_pdev_cap *cap = &ar->pdev->cap;
14206 	int ret;
14207 
14208 	init_waitqueue_head(&ar->txmgmt_empty_waitq);
14209 	idr_init(&ar->txmgmt_idr);
14210 	spin_lock_init(&ar->txmgmt_idr_lock);
14211 
14212 	ath12k_pdev_caps_update(ar);
14213 
14214 	ret = ath12k_mac_setup_channels_rates(ar,
14215 					      cap->supported_bands,
14216 					      bands);
14217 	if (ret)
14218 		return ret;
14219 
14220 	ath12k_mac_setup_ht_vht_cap(ar, cap, ht_cap);
14221 	ath12k_mac_setup_sband_iftype_data(ar, cap);
14222 
14223 	ar->max_num_stations = ath12k_core_get_max_station_per_radio(ar->ab);
14224 	ar->max_num_peers = ath12k_core_get_max_peers_per_radio(ar->ab);
14225 
14226 	ar->rssi_info.min_nf_dbm = ATH12K_DEFAULT_NOISE_FLOOR;
14227 	ar->rssi_info.temp_offset = 0;
14228 	ar->rssi_info.noise_floor = ar->rssi_info.min_nf_dbm + ar->rssi_info.temp_offset;
14229 
14230 	return 0;
14231 }
14232 
14233 static int ath12k_mac_hw_register(struct ath12k_hw *ah)
14234 {
14235 	struct ieee80211_hw *hw = ah->hw;
14236 	struct wiphy *wiphy = hw->wiphy;
14237 	struct ath12k *ar = ath12k_ah_to_ar(ah, 0);
14238 	struct ath12k_base *ab = ar->ab;
14239 	struct ath12k_pdev *pdev;
14240 	struct ath12k_pdev_cap *cap;
14241 	static const u32 cipher_suites[] = {
14242 		WLAN_CIPHER_SUITE_TKIP,
14243 		WLAN_CIPHER_SUITE_CCMP,
14244 		WLAN_CIPHER_SUITE_AES_CMAC,
14245 		WLAN_CIPHER_SUITE_BIP_CMAC_256,
14246 		WLAN_CIPHER_SUITE_BIP_GMAC_128,
14247 		WLAN_CIPHER_SUITE_BIP_GMAC_256,
14248 		WLAN_CIPHER_SUITE_GCMP,
14249 		WLAN_CIPHER_SUITE_GCMP_256,
14250 		WLAN_CIPHER_SUITE_CCMP_256,
14251 	};
14252 	int ret, i, j;
14253 	u32 ht_cap = U32_MAX, antennas_rx = 0, antennas_tx = 0;
14254 	bool is_6ghz = false, is_raw_mode = false, is_monitor_disable = false;
14255 	u8 *mac_addr = NULL;
14256 	u8 mbssid_max_interfaces = 0;
14257 
14258 	wiphy->max_ap_assoc_sta = 0;
14259 
14260 	for_each_ar(ah, ar, i) {
14261 		u32 ht_cap_info = 0;
14262 
14263 		pdev = ar->pdev;
14264 		if (ar->ab->pdevs_macaddr_valid) {
14265 			ether_addr_copy(ar->mac_addr, pdev->mac_addr);
14266 		} else {
14267 			ether_addr_copy(ar->mac_addr, ar->ab->mac_addr);
14268 			ar->mac_addr[4] += ar->pdev_idx;
14269 		}
14270 
14271 		ret = ath12k_mac_setup_register(ar, &ht_cap_info, hw->wiphy->bands);
14272 		if (ret)
14273 			goto err_cleanup_unregister;
14274 
14275 		/* 6 GHz does not support HT Cap, hence do not consider it */
14276 		if (!ar->supports_6ghz)
14277 			ht_cap &= ht_cap_info;
14278 
14279 		wiphy->max_ap_assoc_sta += ar->max_num_stations;
14280 
14281 		/* Advertise the max antenna support of all radios, driver can handle
14282 		 * per pdev specific antenna setting based on pdev cap when antenna
14283 		 * changes are made
14284 		 */
14285 		cap = &pdev->cap;
14286 
14287 		antennas_rx = max_t(u32, antennas_rx, cap->rx_chain_mask);
14288 		antennas_tx = max_t(u32, antennas_tx, cap->tx_chain_mask);
14289 
14290 		if (ar->supports_6ghz)
14291 			is_6ghz = true;
14292 
14293 		if (test_bit(ATH12K_FLAG_RAW_MODE, &ar->ab->dev_flags))
14294 			is_raw_mode = true;
14295 
14296 		if (!ar->ab->hw_params->supports_monitor)
14297 			is_monitor_disable = true;
14298 
14299 		if (i == 0)
14300 			mac_addr = ar->mac_addr;
14301 		else
14302 			mac_addr = ab->mac_addr;
14303 
14304 		mbssid_max_interfaces += TARGET_NUM_VDEVS(ar->ab);
14305 	}
14306 
14307 	wiphy->available_antennas_rx = antennas_rx;
14308 	wiphy->available_antennas_tx = antennas_tx;
14309 
14310 	SET_IEEE80211_PERM_ADDR(hw, mac_addr);
14311 	SET_IEEE80211_DEV(hw, ab->dev);
14312 
14313 	ret = ath12k_mac_setup_iface_combinations(ah);
14314 	if (ret) {
14315 		ath12k_err(ab, "failed to setup interface combinations: %d\n", ret);
14316 		goto err_complete_cleanup_unregister;
14317 	}
14318 
14319 	wiphy->interface_modes = ath12k_mac_get_ifmodes(ah);
14320 
14321 	if (ah->num_radio == 1 &&
14322 	    wiphy->bands[NL80211_BAND_2GHZ] &&
14323 	    wiphy->bands[NL80211_BAND_5GHZ] &&
14324 	    wiphy->bands[NL80211_BAND_6GHZ])
14325 		ieee80211_hw_set(hw, SINGLE_SCAN_ON_ALL_BANDS);
14326 
14327 	ieee80211_hw_set(hw, SIGNAL_DBM);
14328 	ieee80211_hw_set(hw, SUPPORTS_PS);
14329 	ieee80211_hw_set(hw, SUPPORTS_DYNAMIC_PS);
14330 	ieee80211_hw_set(hw, MFP_CAPABLE);
14331 	ieee80211_hw_set(hw, REPORTS_TX_ACK_STATUS);
14332 	ieee80211_hw_set(hw, HAS_RATE_CONTROL);
14333 	ieee80211_hw_set(hw, AP_LINK_PS);
14334 	ieee80211_hw_set(hw, SPECTRUM_MGMT);
14335 	ieee80211_hw_set(hw, CONNECTION_MONITOR);
14336 	ieee80211_hw_set(hw, SUPPORTS_PER_STA_GTK);
14337 	ieee80211_hw_set(hw, CHANCTX_STA_CSA);
14338 	ieee80211_hw_set(hw, QUEUE_CONTROL);
14339 	ieee80211_hw_set(hw, SUPPORTS_TX_FRAG);
14340 	ieee80211_hw_set(hw, REPORTS_LOW_ACK);
14341 	ieee80211_hw_set(hw, NO_VIRTUAL_MONITOR);
14342 
14343 	if (test_bit(WMI_TLV_SERVICE_ETH_OFFLOAD, ar->wmi->wmi_ab->svc_map)) {
14344 		ieee80211_hw_set(hw, SUPPORTS_TX_ENCAP_OFFLOAD);
14345 		ieee80211_hw_set(hw, SUPPORTS_RX_DECAP_OFFLOAD);
14346 	}
14347 
14348 	if (cap->nss_ratio_enabled)
14349 		ieee80211_hw_set(hw, SUPPORTS_VHT_EXT_NSS_BW);
14350 
14351 	if ((ht_cap & WMI_HT_CAP_ENABLED) || is_6ghz) {
14352 		ieee80211_hw_set(hw, AMPDU_AGGREGATION);
14353 		ieee80211_hw_set(hw, TX_AMPDU_SETUP_IN_HW);
14354 		ieee80211_hw_set(hw, SUPPORTS_REORDERING_BUFFER);
14355 		ieee80211_hw_set(hw, SUPPORTS_AMSDU_IN_AMPDU);
14356 		ieee80211_hw_set(hw, USES_RSS);
14357 	}
14358 
14359 	wiphy->features |= NL80211_FEATURE_STATIC_SMPS;
14360 	wiphy->flags |= WIPHY_FLAG_IBSS_RSN;
14361 
14362 	/* TODO: Check if HT capability advertised from firmware is different
14363 	 * for each band for a dual band capable radio. It will be tricky to
14364 	 * handle it when the ht capability different for each band.
14365 	 */
14366 	if (ht_cap & WMI_HT_CAP_DYNAMIC_SMPS ||
14367 	    (is_6ghz && ab->hw_params->supports_dynamic_smps_6ghz))
14368 		wiphy->features |= NL80211_FEATURE_DYNAMIC_SMPS;
14369 
14370 	wiphy->max_scan_ssids = WLAN_SCAN_PARAMS_MAX_SSID;
14371 	wiphy->max_scan_ie_len = WLAN_SCAN_PARAMS_MAX_IE_LEN;
14372 
14373 	hw->max_listen_interval = ATH12K_MAX_HW_LISTEN_INTERVAL;
14374 
14375 	wiphy->flags |= WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL;
14376 	wiphy->flags |= WIPHY_FLAG_HAS_CHANNEL_SWITCH;
14377 	wiphy->max_remain_on_channel_duration = 5000;
14378 
14379 	wiphy->flags |= WIPHY_FLAG_AP_UAPSD;
14380 	wiphy->features |= NL80211_FEATURE_AP_MODE_CHAN_WIDTH_CHANGE |
14381 				   NL80211_FEATURE_AP_SCAN;
14382 
14383 	wiphy->features |= NL80211_FEATURE_TX_POWER_INSERTION;
14384 
14385 	/* MLO is not yet supported so disable Wireless Extensions for now
14386 	 * to make sure ath12k users don't use it. This flag can be removed
14387 	 * once WIPHY_FLAG_SUPPORTS_MLO is enabled.
14388 	 */
14389 	wiphy->flags |= WIPHY_FLAG_DISABLE_WEXT;
14390 
14391 	/* Copy over MLO related capabilities received from
14392 	 * WMI_SERVICE_READY_EXT2_EVENT if single_chip_mlo_supp is set.
14393 	 */
14394 	if (ab->ag->mlo_capable) {
14395 		ath12k_iftypes_ext_capa[2].eml_capabilities = cap->eml_cap;
14396 		ath12k_iftypes_ext_capa[2].mld_capa_and_ops = cap->mld_cap;
14397 		wiphy->flags |= WIPHY_FLAG_SUPPORTS_MLO;
14398 
14399 		ieee80211_hw_set(hw, MLO_MCAST_MULTI_LINK_TX);
14400 	}
14401 
14402 	hw->queues = ATH12K_HW_MAX_QUEUES;
14403 	wiphy->tx_queue_len = ATH12K_QUEUE_LEN;
14404 	hw->offchannel_tx_hw_queue = ATH12K_HW_MAX_QUEUES - 1;
14405 	hw->max_rx_aggregation_subframes = IEEE80211_MAX_AMPDU_BUF_EHT;
14406 
14407 	hw->vif_data_size = sizeof(struct ath12k_vif);
14408 	hw->sta_data_size = sizeof(struct ath12k_sta);
14409 	hw->extra_tx_headroom = ab->hw_params->iova_mask;
14410 
14411 	wiphy_ext_feature_set(wiphy, NL80211_EXT_FEATURE_CQM_RSSI_LIST);
14412 	wiphy_ext_feature_set(wiphy, NL80211_EXT_FEATURE_STA_TX_PWR);
14413 	wiphy_ext_feature_set(wiphy, NL80211_EXT_FEATURE_ACK_SIGNAL_SUPPORT);
14414 	if (test_bit(WMI_TLV_SERVICE_BSS_COLOR_OFFLOAD,
14415 		     ab->wmi_ab.svc_map)) {
14416 		wiphy_ext_feature_set(wiphy, NL80211_EXT_FEATURE_BSS_COLOR);
14417 		ieee80211_hw_set(hw, DETECTS_COLOR_COLLISION);
14418 	}
14419 
14420 	wiphy->cipher_suites = cipher_suites;
14421 	wiphy->n_cipher_suites = ARRAY_SIZE(cipher_suites);
14422 
14423 	wiphy->iftype_ext_capab = ath12k_iftypes_ext_capa;
14424 	wiphy->num_iftype_ext_capab = ARRAY_SIZE(ath12k_iftypes_ext_capa);
14425 
14426 	wiphy->mbssid_max_interfaces = mbssid_max_interfaces;
14427 	wiphy->ema_max_profile_periodicity = TARGET_EMA_MAX_PROFILE_PERIOD;
14428 	ieee80211_hw_set(hw, SUPPORTS_MULTI_BSSID);
14429 
14430 	if (is_6ghz) {
14431 		wiphy_ext_feature_set(wiphy,
14432 				      NL80211_EXT_FEATURE_FILS_DISCOVERY);
14433 		wiphy_ext_feature_set(wiphy,
14434 				      NL80211_EXT_FEATURE_UNSOL_BCAST_PROBE_RESP);
14435 	}
14436 
14437 	wiphy_ext_feature_set(wiphy, NL80211_EXT_FEATURE_PUNCT);
14438 	if (test_bit(WMI_TLV_SERVICE_BEACON_PROTECTION_SUPPORT, ab->wmi_ab.svc_map))
14439 		wiphy_ext_feature_set(hw->wiphy, NL80211_EXT_FEATURE_BEACON_PROTECTION);
14440 
14441 	ath12k_reg_init(hw);
14442 
14443 	if (!is_raw_mode) {
14444 		hw->netdev_features = NETIF_F_HW_CSUM;
14445 		ieee80211_hw_set(hw, SW_CRYPTO_CONTROL);
14446 		ieee80211_hw_set(hw, SUPPORT_FAST_XMIT);
14447 	}
14448 
14449 	if (test_bit(WMI_TLV_SERVICE_NLO, ar->wmi->wmi_ab->svc_map)) {
14450 		wiphy->max_sched_scan_ssids = WMI_PNO_MAX_SUPP_NETWORKS;
14451 		wiphy->max_match_sets = WMI_PNO_MAX_SUPP_NETWORKS;
14452 		wiphy->max_sched_scan_ie_len = WMI_PNO_MAX_IE_LENGTH;
14453 		wiphy->max_sched_scan_plans = WMI_PNO_MAX_SCHED_SCAN_PLANS;
14454 		wiphy->max_sched_scan_plan_interval =
14455 					WMI_PNO_MAX_SCHED_SCAN_PLAN_INT;
14456 		wiphy->max_sched_scan_plan_iterations =
14457 					WMI_PNO_MAX_SCHED_SCAN_PLAN_ITRNS;
14458 		wiphy->features |= NL80211_FEATURE_ND_RANDOM_MAC_ADDR;
14459 	}
14460 
14461 	ret = ath12k_wow_init(ar);
14462 	if (ret) {
14463 		ath12k_warn(ar->ab, "failed to init wow: %d\n", ret);
14464 		goto err_cleanup_if_combs;
14465 	}
14466 
14467 	/* Boot-time regulatory updates have already been processed.
14468 	 * Mark them as complete now, because after registration,
14469 	 * cfg80211 will notify us again if there are any pending hints.
14470 	 * We need to wait for those hints to be processed, so it's
14471 	 * important to mark the boot-time updates as complete before
14472 	 * proceeding with registration.
14473 	 */
14474 	for_each_ar(ah, ar, i)
14475 		complete_all(&ar->regd_update_completed);
14476 
14477 	ret = ieee80211_register_hw(hw);
14478 	if (ret) {
14479 		ath12k_err(ab, "ieee80211 registration failed: %d\n", ret);
14480 		goto err_cleanup_if_combs;
14481 	}
14482 
14483 	if (is_monitor_disable)
14484 		/* There's a race between calling ieee80211_register_hw()
14485 		 * and here where the monitor mode is enabled for a little
14486 		 * while. But that time is so short and in practise it make
14487 		 * a difference in real life.
14488 		 */
14489 		wiphy->interface_modes &= ~BIT(NL80211_IFTYPE_MONITOR);
14490 
14491 	for_each_ar(ah, ar, i) {
14492 		/* Apply the regd received during initialization */
14493 		ret = ath12k_regd_update(ar, true);
14494 		if (ret) {
14495 			ath12k_err(ar->ab, "ath12k regd update failed: %d\n", ret);
14496 			goto err_unregister_hw;
14497 		}
14498 
14499 		if (ar->ab->hw_params->current_cc_support && ab->new_alpha2[0]) {
14500 			struct wmi_set_current_country_arg current_cc = {};
14501 
14502 			memcpy(&current_cc.alpha2, ab->new_alpha2, 2);
14503 			memcpy(&ar->alpha2, ab->new_alpha2, 2);
14504 
14505 			reinit_completion(&ar->regd_update_completed);
14506 
14507 			ret = ath12k_wmi_send_set_current_country_cmd(ar, &current_cc);
14508 			if (ret)
14509 				ath12k_warn(ar->ab,
14510 					    "failed set cc code for mac register: %d\n",
14511 					    ret);
14512 		}
14513 
14514 		ath12k_fw_stats_init(ar);
14515 		ath12k_debugfs_register(ar);
14516 	}
14517 
14518 	return 0;
14519 
14520 err_unregister_hw:
14521 	for_each_ar(ah, ar, i)
14522 		ath12k_debugfs_unregister(ar);
14523 
14524 	ieee80211_unregister_hw(hw);
14525 
14526 err_cleanup_if_combs:
14527 	ath12k_mac_cleanup_iface_combinations(ah);
14528 
14529 err_complete_cleanup_unregister:
14530 	i = ah->num_radio;
14531 
14532 err_cleanup_unregister:
14533 	for (j = 0; j < i; j++) {
14534 		ar = ath12k_ah_to_ar(ah, j);
14535 		ath12k_mac_cleanup_unregister(ar);
14536 	}
14537 
14538 	SET_IEEE80211_DEV(hw, NULL);
14539 
14540 	return ret;
14541 }
14542 
14543 static void ath12k_mac_setup(struct ath12k *ar)
14544 {
14545 	struct ath12k_base *ab = ar->ab;
14546 	struct ath12k_pdev *pdev = ar->pdev;
14547 	u8 pdev_idx = ar->pdev_idx;
14548 
14549 	ar->lmac_id = ath12k_hw_get_mac_from_pdev_id(ab->hw_params, pdev_idx);
14550 
14551 	ar->wmi = &ab->wmi_ab.wmi[pdev_idx];
14552 	/* FIXME: wmi[0] is already initialized during attach,
14553 	 * Should we do this again?
14554 	 */
14555 	ath12k_wmi_pdev_attach(ab, pdev_idx);
14556 
14557 	ar->cfg_tx_chainmask = pdev->cap.tx_chain_mask;
14558 	ar->cfg_rx_chainmask = pdev->cap.rx_chain_mask;
14559 	ar->num_tx_chains = hweight32(pdev->cap.tx_chain_mask);
14560 	ar->num_rx_chains = hweight32(pdev->cap.rx_chain_mask);
14561 	ar->scan.arvif = NULL;
14562 	ar->vdev_id_11d_scan = ATH12K_11D_INVALID_VDEV_ID;
14563 
14564 	spin_lock_init(&ar->data_lock);
14565 	INIT_LIST_HEAD(&ar->arvifs);
14566 	INIT_LIST_HEAD(&ar->ppdu_stats_info);
14567 
14568 	init_completion(&ar->vdev_setup_done);
14569 	init_completion(&ar->vdev_delete_done);
14570 	init_completion(&ar->peer_assoc_done);
14571 	init_completion(&ar->peer_delete_done);
14572 	init_completion(&ar->install_key_done);
14573 	init_completion(&ar->bss_survey_done);
14574 	init_completion(&ar->scan.started);
14575 	init_completion(&ar->scan.completed);
14576 	init_completion(&ar->scan.on_channel);
14577 	init_completion(&ar->mlo_setup_done);
14578 	init_completion(&ar->completed_11d_scan);
14579 	init_completion(&ar->regd_update_completed);
14580 
14581 	INIT_DELAYED_WORK(&ar->scan.timeout, ath12k_scan_timeout_work);
14582 	wiphy_work_init(&ar->scan.vdev_clean_wk, ath12k_scan_vdev_clean_work);
14583 	INIT_WORK(&ar->regd_channel_update_work, ath12k_regd_update_chan_list_work);
14584 	INIT_LIST_HEAD(&ar->regd_channel_update_queue);
14585 	INIT_WORK(&ar->regd_update_work, ath12k_regd_update_work);
14586 
14587 	wiphy_work_init(&ar->wmi_mgmt_tx_work, ath12k_mgmt_over_wmi_tx_work);
14588 	skb_queue_head_init(&ar->wmi_mgmt_tx_queue);
14589 
14590 	ar->monitor_vdev_id = -1;
14591 	ar->monitor_vdev_created = false;
14592 	ar->monitor_started = false;
14593 }
14594 
14595 static int __ath12k_mac_mlo_setup(struct ath12k *ar)
14596 {
14597 	u8 num_link = 0, partner_link_id[ATH12K_GROUP_MAX_RADIO] = {};
14598 	struct ath12k_base *partner_ab, *ab = ar->ab;
14599 	struct ath12k_hw_group *ag = ab->ag;
14600 	struct wmi_mlo_setup_arg mlo = {};
14601 	struct ath12k_pdev *pdev;
14602 	unsigned long time_left;
14603 	int i, j, ret;
14604 
14605 	lockdep_assert_held(&ag->mutex);
14606 
14607 	reinit_completion(&ar->mlo_setup_done);
14608 
14609 	for (i = 0; i < ag->num_devices; i++) {
14610 		partner_ab = ag->ab[i];
14611 
14612 		for (j = 0; j < partner_ab->num_radios; j++) {
14613 			pdev = &partner_ab->pdevs[j];
14614 
14615 			/* Avoid the self link */
14616 			if (ar == pdev->ar)
14617 				continue;
14618 
14619 			partner_link_id[num_link] = pdev->hw_link_id;
14620 			num_link++;
14621 
14622 			ath12k_dbg(ab, ATH12K_DBG_MAC, "device %d pdev %d hw_link_id %d num_link %d\n",
14623 				   i, j, pdev->hw_link_id, num_link);
14624 		}
14625 	}
14626 
14627 	if (num_link == 0)
14628 		return 0;
14629 
14630 	mlo.group_id = cpu_to_le32(ag->id);
14631 	mlo.partner_link_id = partner_link_id;
14632 	mlo.num_partner_links = num_link;
14633 	ar->mlo_setup_status = 0;
14634 
14635 	ath12k_dbg(ab, ATH12K_DBG_MAC, "group id %d num_link %d\n", ag->id, num_link);
14636 
14637 	ret = ath12k_wmi_mlo_setup(ar, &mlo);
14638 	if (ret) {
14639 		ath12k_err(ab, "failed to send  setup MLO WMI command for pdev %d: %d\n",
14640 			   ar->pdev_idx, ret);
14641 		return ret;
14642 	}
14643 
14644 	time_left = wait_for_completion_timeout(&ar->mlo_setup_done,
14645 						WMI_MLO_CMD_TIMEOUT_HZ);
14646 
14647 	if (!time_left || ar->mlo_setup_status)
14648 		return ar->mlo_setup_status ? : -ETIMEDOUT;
14649 
14650 	ath12k_dbg(ab, ATH12K_DBG_MAC, "mlo setup done for pdev %d\n", ar->pdev_idx);
14651 
14652 	return 0;
14653 }
14654 
14655 static int __ath12k_mac_mlo_teardown(struct ath12k *ar)
14656 {
14657 	struct ath12k_base *ab = ar->ab;
14658 	int ret;
14659 	u8 num_link;
14660 
14661 	if (test_bit(ATH12K_FLAG_RECOVERY, &ab->dev_flags))
14662 		return 0;
14663 
14664 	num_link = ath12k_get_num_partner_link(ar);
14665 
14666 	if (num_link == 0)
14667 		return 0;
14668 
14669 	ret = ath12k_wmi_mlo_teardown(ar);
14670 	if (ret) {
14671 		ath12k_warn(ab, "failed to send MLO teardown WMI command for pdev %d: %d\n",
14672 			    ar->pdev_idx, ret);
14673 		return ret;
14674 	}
14675 
14676 	ath12k_dbg(ab, ATH12K_DBG_MAC, "mlo teardown for pdev %d\n", ar->pdev_idx);
14677 
14678 	return 0;
14679 }
14680 
14681 int ath12k_mac_mlo_setup(struct ath12k_hw_group *ag)
14682 {
14683 	struct ath12k_hw *ah;
14684 	struct ath12k *ar;
14685 	int ret;
14686 	int i, j;
14687 
14688 	for (i = 0; i < ag->num_hw; i++) {
14689 		ah = ag->ah[i];
14690 		if (!ah)
14691 			continue;
14692 
14693 		for_each_ar(ah, ar, j) {
14694 			ar = &ah->radio[j];
14695 			ret = __ath12k_mac_mlo_setup(ar);
14696 			if (ret) {
14697 				ath12k_err(ar->ab, "failed to setup MLO: %d\n", ret);
14698 				goto err_setup;
14699 			}
14700 		}
14701 	}
14702 
14703 	return 0;
14704 
14705 err_setup:
14706 	for (i = i - 1; i >= 0; i--) {
14707 		ah = ag->ah[i];
14708 		if (!ah)
14709 			continue;
14710 
14711 		for (j = j - 1; j >= 0; j--) {
14712 			ar = &ah->radio[j];
14713 			if (!ar)
14714 				continue;
14715 
14716 			__ath12k_mac_mlo_teardown(ar);
14717 		}
14718 	}
14719 
14720 	return ret;
14721 }
14722 
14723 void ath12k_mac_mlo_teardown(struct ath12k_hw_group *ag)
14724 {
14725 	struct ath12k_hw *ah;
14726 	struct ath12k *ar;
14727 	int ret, i, j;
14728 
14729 	for (i = 0; i < ag->num_hw; i++) {
14730 		ah = ag->ah[i];
14731 		if (!ah)
14732 			continue;
14733 
14734 		for_each_ar(ah, ar, j) {
14735 			ar = &ah->radio[j];
14736 			ret = __ath12k_mac_mlo_teardown(ar);
14737 			if (ret) {
14738 				ath12k_err(ar->ab, "failed to teardown MLO: %d\n", ret);
14739 				break;
14740 			}
14741 		}
14742 	}
14743 }
14744 
14745 int ath12k_mac_register(struct ath12k_hw_group *ag)
14746 {
14747 	struct ath12k_hw *ah;
14748 	int i;
14749 	int ret;
14750 
14751 	for (i = 0; i < ag->num_hw; i++) {
14752 		ah = ath12k_ag_to_ah(ag, i);
14753 
14754 		ret = ath12k_mac_hw_register(ah);
14755 		if (ret)
14756 			goto err;
14757 	}
14758 
14759 	return 0;
14760 
14761 err:
14762 	for (i = i - 1; i >= 0; i--) {
14763 		ah = ath12k_ag_to_ah(ag, i);
14764 		if (!ah)
14765 			continue;
14766 
14767 		ath12k_mac_hw_unregister(ah);
14768 	}
14769 
14770 	return ret;
14771 }
14772 
14773 void ath12k_mac_unregister(struct ath12k_hw_group *ag)
14774 {
14775 	struct ath12k_hw *ah;
14776 	int i;
14777 
14778 	for (i = ag->num_hw - 1; i >= 0; i--) {
14779 		ah = ath12k_ag_to_ah(ag, i);
14780 		if (!ah)
14781 			continue;
14782 
14783 		ath12k_mac_hw_unregister(ah);
14784 	}
14785 }
14786 
14787 static void ath12k_mac_hw_destroy(struct ath12k_hw *ah)
14788 {
14789 	ieee80211_free_hw(ah->hw);
14790 }
14791 
14792 static struct ath12k_hw *ath12k_mac_hw_allocate(struct ath12k_hw_group *ag,
14793 						struct ath12k_pdev_map *pdev_map,
14794 						u8 num_pdev_map)
14795 {
14796 	struct ieee80211_hw *hw;
14797 	struct ath12k *ar;
14798 	struct ath12k_base *ab;
14799 	struct ath12k_pdev *pdev;
14800 	struct ath12k_hw *ah;
14801 	int i;
14802 	u8 pdev_idx;
14803 
14804 	hw = ieee80211_alloc_hw(struct_size(ah, radio, num_pdev_map),
14805 				&ath12k_ops);
14806 	if (!hw)
14807 		return NULL;
14808 
14809 	ah = ath12k_hw_to_ah(hw);
14810 	ah->hw = hw;
14811 	ah->num_radio = num_pdev_map;
14812 
14813 	mutex_init(&ah->hw_mutex);
14814 	INIT_LIST_HEAD(&ah->ml_peers);
14815 
14816 	for (i = 0; i < num_pdev_map; i++) {
14817 		ab = pdev_map[i].ab;
14818 		pdev_idx = pdev_map[i].pdev_idx;
14819 		pdev = &ab->pdevs[pdev_idx];
14820 
14821 		ar = ath12k_ah_to_ar(ah, i);
14822 		ar->ah = ah;
14823 		ar->ab = ab;
14824 		ar->hw_link_id = pdev->hw_link_id;
14825 		ar->pdev = pdev;
14826 		ar->pdev_idx = pdev_idx;
14827 		pdev->ar = ar;
14828 
14829 		ag->hw_links[ar->hw_link_id].device_id = ab->device_id;
14830 		ag->hw_links[ar->hw_link_id].pdev_idx = pdev_idx;
14831 
14832 		ath12k_mac_setup(ar);
14833 		ath12k_dp_pdev_pre_alloc(ar);
14834 	}
14835 
14836 	return ah;
14837 }
14838 
14839 void ath12k_mac_destroy(struct ath12k_hw_group *ag)
14840 {
14841 	struct ath12k_pdev *pdev;
14842 	struct ath12k_base *ab = ag->ab[0];
14843 	int i, j;
14844 	struct ath12k_hw *ah;
14845 
14846 	for (i = 0; i < ag->num_devices; i++) {
14847 		ab = ag->ab[i];
14848 		if (!ab)
14849 			continue;
14850 
14851 		for (j = 0; j < ab->num_radios; j++) {
14852 			pdev = &ab->pdevs[j];
14853 			if (!pdev->ar)
14854 				continue;
14855 			pdev->ar = NULL;
14856 		}
14857 	}
14858 
14859 	for (i = 0; i < ag->num_hw; i++) {
14860 		ah = ath12k_ag_to_ah(ag, i);
14861 		if (!ah)
14862 			continue;
14863 
14864 		ath12k_mac_hw_destroy(ah);
14865 		ath12k_ag_set_ah(ag, i, NULL);
14866 	}
14867 }
14868 
14869 static void ath12k_mac_set_device_defaults(struct ath12k_base *ab)
14870 {
14871 	int total_vdev;
14872 
14873 	/* Initialize channel counters frequency value in hertz */
14874 	ab->cc_freq_hz = 320000;
14875 	total_vdev = ab->num_radios * TARGET_NUM_VDEVS(ab);
14876 	ab->free_vdev_map = (1LL << total_vdev) - 1;
14877 }
14878 
14879 int ath12k_mac_allocate(struct ath12k_hw_group *ag)
14880 {
14881 	struct ath12k_pdev_map pdev_map[ATH12K_GROUP_MAX_RADIO];
14882 	int mac_id, device_id, total_radio, num_hw;
14883 	struct ath12k_base *ab;
14884 	struct ath12k_hw *ah;
14885 	int ret, i, j;
14886 	u8 radio_per_hw;
14887 
14888 	total_radio = 0;
14889 	for (i = 0; i < ag->num_devices; i++) {
14890 		ab = ag->ab[i];
14891 		if (!ab)
14892 			continue;
14893 
14894 		ath12k_debugfs_pdev_create(ab);
14895 		ath12k_mac_set_device_defaults(ab);
14896 		total_radio += ab->num_radios;
14897 	}
14898 
14899 	if (!total_radio)
14900 		return -EINVAL;
14901 
14902 	if (WARN_ON(total_radio > ATH12K_GROUP_MAX_RADIO))
14903 		return -ENOSPC;
14904 
14905 	/* All pdev get combined and register as single wiphy based on
14906 	 * hardware group which participate in multi-link operation else
14907 	 * each pdev get register separately.
14908 	 */
14909 	if (ag->mlo_capable)
14910 		radio_per_hw = total_radio;
14911 	else
14912 		radio_per_hw = 1;
14913 
14914 	num_hw = total_radio / radio_per_hw;
14915 
14916 	ag->num_hw = 0;
14917 	device_id = 0;
14918 	mac_id = 0;
14919 	for (i = 0; i < num_hw; i++) {
14920 		for (j = 0; j < radio_per_hw; j++) {
14921 			if (device_id >= ag->num_devices || !ag->ab[device_id]) {
14922 				ret = -ENOSPC;
14923 				goto err;
14924 			}
14925 
14926 			ab = ag->ab[device_id];
14927 			pdev_map[j].ab = ab;
14928 			pdev_map[j].pdev_idx = mac_id;
14929 			mac_id++;
14930 
14931 			/* If mac_id falls beyond the current device MACs then
14932 			 * move to next device
14933 			 */
14934 			if (mac_id >= ab->num_radios) {
14935 				mac_id = 0;
14936 				device_id++;
14937 			}
14938 		}
14939 
14940 		ab = pdev_map->ab;
14941 
14942 		ah = ath12k_mac_hw_allocate(ag, pdev_map, radio_per_hw);
14943 		if (!ah) {
14944 			ath12k_warn(ab, "failed to allocate mac80211 hw device for hw_idx %d\n",
14945 				    i);
14946 			ret = -ENOMEM;
14947 			goto err;
14948 		}
14949 
14950 		ah->dev = ab->dev;
14951 
14952 		ag->ah[i] = ah;
14953 		ag->num_hw++;
14954 	}
14955 
14956 	return 0;
14957 
14958 err:
14959 	for (i = i - 1; i >= 0; i--) {
14960 		ah = ath12k_ag_to_ah(ag, i);
14961 		if (!ah)
14962 			continue;
14963 
14964 		ath12k_mac_hw_destroy(ah);
14965 		ath12k_ag_set_ah(ag, i, NULL);
14966 	}
14967 
14968 	return ret;
14969 }
14970 
14971 int ath12k_mac_vif_set_keepalive(struct ath12k_link_vif *arvif,
14972 				 enum wmi_sta_keepalive_method method,
14973 				 u32 interval)
14974 {
14975 	struct wmi_sta_keepalive_arg arg = {};
14976 	struct ath12k *ar = arvif->ar;
14977 	int ret;
14978 
14979 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
14980 
14981 	if (arvif->ahvif->vdev_type != WMI_VDEV_TYPE_STA)
14982 		return 0;
14983 
14984 	if (!test_bit(WMI_TLV_SERVICE_STA_KEEP_ALIVE, ar->ab->wmi_ab.svc_map))
14985 		return 0;
14986 
14987 	arg.vdev_id = arvif->vdev_id;
14988 	arg.enabled = 1;
14989 	arg.method = method;
14990 	arg.interval = interval;
14991 
14992 	ret = ath12k_wmi_sta_keepalive(ar, &arg);
14993 	if (ret) {
14994 		ath12k_warn(ar->ab, "failed to set keepalive on vdev %i: %d\n",
14995 			    arvif->vdev_id, ret);
14996 		return ret;
14997 	}
14998 
14999 	return 0;
15000 }
15001