xref: /linux/drivers/net/wireless/ath/ath12k/mac.c (revision 8f7aa3d3c7323f4ca2768a9e74ebbe359c4f8f88)
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_started)
5499 			continue;
5500 
5501 		ar = arvif->ar;
5502 
5503 		ath12k_scan_abort(ar);
5504 
5505 		cancel_delayed_work_sync(&ar->scan.timeout);
5506 	}
5507 }
5508 
5509 static int ath12k_install_key(struct ath12k_link_vif *arvif,
5510 			      struct ieee80211_key_conf *key,
5511 			      enum set_key_cmd cmd,
5512 			      const u8 *macaddr, u32 flags)
5513 {
5514 	int ret;
5515 	struct ath12k *ar = arvif->ar;
5516 	struct wmi_vdev_install_key_arg arg = {
5517 		.vdev_id = arvif->vdev_id,
5518 		.key_idx = key->keyidx,
5519 		.key_len = key->keylen,
5520 		.key_data = key->key,
5521 		.key_flags = flags,
5522 		.ieee80211_key_cipher = key->cipher,
5523 		.macaddr = macaddr,
5524 	};
5525 	struct ath12k_vif *ahvif = arvif->ahvif;
5526 
5527 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
5528 
5529 	if (test_bit(ATH12K_FLAG_HW_CRYPTO_DISABLED, &ar->ab->dev_flags))
5530 		return 0;
5531 
5532 	if (cmd == DISABLE_KEY) {
5533 		/* TODO: Check if FW expects  value other than NONE for del */
5534 		/* arg.key_cipher = WMI_CIPHER_NONE; */
5535 		arg.key_len = 0;
5536 		arg.key_data = NULL;
5537 		goto check_order;
5538 	}
5539 
5540 	switch (key->cipher) {
5541 	case WLAN_CIPHER_SUITE_CCMP:
5542 	case WLAN_CIPHER_SUITE_CCMP_256:
5543 		arg.key_cipher = WMI_CIPHER_AES_CCM;
5544 		key->flags |= IEEE80211_KEY_FLAG_GENERATE_IV_MGMT;
5545 		break;
5546 	case WLAN_CIPHER_SUITE_TKIP:
5547 		arg.key_cipher = WMI_CIPHER_TKIP;
5548 		arg.key_txmic_len = 8;
5549 		arg.key_rxmic_len = 8;
5550 		break;
5551 	case WLAN_CIPHER_SUITE_GCMP:
5552 	case WLAN_CIPHER_SUITE_GCMP_256:
5553 		arg.key_cipher = WMI_CIPHER_AES_GCM;
5554 		key->flags |= IEEE80211_KEY_FLAG_GENERATE_IV_MGMT;
5555 		break;
5556 	case WLAN_CIPHER_SUITE_AES_CMAC:
5557 		arg.key_cipher = WMI_CIPHER_AES_CMAC;
5558 		break;
5559 	case WLAN_CIPHER_SUITE_BIP_GMAC_128:
5560 	case WLAN_CIPHER_SUITE_BIP_GMAC_256:
5561 		arg.key_cipher = WMI_CIPHER_AES_GMAC;
5562 		break;
5563 	case WLAN_CIPHER_SUITE_BIP_CMAC_256:
5564 		arg.key_cipher = WMI_CIPHER_AES_CMAC;
5565 		break;
5566 	default:
5567 		ath12k_warn(ar->ab, "cipher %d is not supported\n", key->cipher);
5568 		return -EOPNOTSUPP;
5569 	}
5570 
5571 	if (test_bit(ATH12K_FLAG_RAW_MODE, &ar->ab->dev_flags))
5572 		key->flags |= IEEE80211_KEY_FLAG_GENERATE_IV |
5573 			      IEEE80211_KEY_FLAG_RESERVE_TAILROOM;
5574 
5575 check_order:
5576 	if (ahvif->vdev_type == WMI_VDEV_TYPE_STA &&
5577 	    arg.key_flags == WMI_KEY_GROUP) {
5578 		if (cmd == SET_KEY) {
5579 			if (arvif->pairwise_key_done) {
5580 				ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
5581 					   "vdev %u pairwise key done, go install group key\n",
5582 					   arg.vdev_id);
5583 				goto install;
5584 			} else {
5585 				/* WCN7850 firmware requires pairwise key to be installed
5586 				 * before group key. In case group key comes first, cache
5587 				 * it and return. Will revisit it once pairwise key gets
5588 				 * installed.
5589 				 */
5590 				arvif->group_key = arg;
5591 				arvif->group_key_valid = true;
5592 				ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
5593 					   "vdev %u group key before pairwise key, cache and skip\n",
5594 					   arg.vdev_id);
5595 
5596 				ret = 0;
5597 				goto out;
5598 			}
5599 		} else {
5600 			arvif->group_key_valid = false;
5601 		}
5602 	}
5603 
5604 install:
5605 	reinit_completion(&ar->install_key_done);
5606 
5607 	ret = ath12k_wmi_vdev_install_key(arvif->ar, &arg);
5608 	if (ret)
5609 		return ret;
5610 
5611 	if (!wait_for_completion_timeout(&ar->install_key_done, 1 * HZ))
5612 		return -ETIMEDOUT;
5613 
5614 	if (ether_addr_equal(arg.macaddr, arvif->bssid))
5615 		ahvif->key_cipher = arg.ieee80211_key_cipher;
5616 
5617 	if (ar->install_key_status) {
5618 		ret = -EINVAL;
5619 		goto out;
5620 	}
5621 
5622 	if (ahvif->vdev_type == WMI_VDEV_TYPE_STA &&
5623 	    arg.key_flags == WMI_KEY_PAIRWISE) {
5624 		if (cmd == SET_KEY) {
5625 			arvif->pairwise_key_done = true;
5626 			if (arvif->group_key_valid) {
5627 				/* Install cached GTK */
5628 				arvif->group_key_valid = false;
5629 				arg = arvif->group_key;
5630 				ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
5631 					   "vdev %u pairwise key done, group key ready, go install\n",
5632 					   arg.vdev_id);
5633 				goto install;
5634 			}
5635 		} else {
5636 			arvif->pairwise_key_done = false;
5637 		}
5638 	}
5639 
5640 out:
5641 	if (ret) {
5642 		/* In case of failure userspace may not do DISABLE_KEY
5643 		 * but triggers re-connection directly, so manually reset
5644 		 * status here.
5645 		 */
5646 		arvif->group_key_valid = false;
5647 		arvif->pairwise_key_done = false;
5648 	}
5649 
5650 	return ret;
5651 }
5652 
5653 static int ath12k_clear_peer_keys(struct ath12k_link_vif *arvif,
5654 				  const u8 *addr)
5655 {
5656 	struct ath12k *ar = arvif->ar;
5657 	struct ath12k_base *ab = ar->ab;
5658 	struct ath12k_peer *peer;
5659 	int first_errno = 0;
5660 	int ret;
5661 	int i;
5662 	u32 flags = 0;
5663 
5664 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
5665 
5666 	spin_lock_bh(&ab->base_lock);
5667 	peer = ath12k_peer_find(ab, arvif->vdev_id, addr);
5668 	spin_unlock_bh(&ab->base_lock);
5669 
5670 	if (!peer)
5671 		return -ENOENT;
5672 
5673 	for (i = 0; i < ARRAY_SIZE(peer->keys); i++) {
5674 		if (!peer->keys[i])
5675 			continue;
5676 
5677 		/* key flags are not required to delete the key */
5678 		ret = ath12k_install_key(arvif, peer->keys[i],
5679 					 DISABLE_KEY, addr, flags);
5680 		if (ret < 0 && first_errno == 0)
5681 			first_errno = ret;
5682 
5683 		if (ret < 0)
5684 			ath12k_warn(ab, "failed to remove peer key %d: %d\n",
5685 				    i, ret);
5686 
5687 		spin_lock_bh(&ab->base_lock);
5688 		peer->keys[i] = NULL;
5689 		spin_unlock_bh(&ab->base_lock);
5690 	}
5691 
5692 	return first_errno;
5693 }
5694 
5695 static int ath12k_mac_set_key(struct ath12k *ar, enum set_key_cmd cmd,
5696 			      struct ath12k_link_vif *arvif,
5697 			      struct ath12k_link_sta *arsta,
5698 			      struct ieee80211_key_conf *key)
5699 {
5700 	struct ieee80211_sta *sta = NULL;
5701 	struct ath12k_base *ab = ar->ab;
5702 	struct ath12k_peer *peer;
5703 	struct ath12k_sta *ahsta;
5704 	const u8 *peer_addr;
5705 	int ret;
5706 	u32 flags = 0;
5707 
5708 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
5709 
5710 	if (arsta)
5711 		sta = ath12k_ahsta_to_sta(arsta->ahsta);
5712 
5713 	if (test_bit(ATH12K_FLAG_HW_CRYPTO_DISABLED, &ab->dev_flags))
5714 		return 1;
5715 
5716 	if (sta)
5717 		peer_addr = arsta->addr;
5718 	else
5719 		peer_addr = arvif->bssid;
5720 
5721 	key->hw_key_idx = key->keyidx;
5722 
5723 	/* the peer should not disappear in mid-way (unless FW goes awry) since
5724 	 * we already hold wiphy lock. we just make sure its there now.
5725 	 */
5726 	spin_lock_bh(&ab->base_lock);
5727 	peer = ath12k_peer_find(ab, arvif->vdev_id, peer_addr);
5728 	spin_unlock_bh(&ab->base_lock);
5729 
5730 	if (!peer) {
5731 		if (cmd == SET_KEY) {
5732 			ath12k_warn(ab, "cannot install key for non-existent peer %pM\n",
5733 				    peer_addr);
5734 			return -EOPNOTSUPP;
5735 		}
5736 
5737 		/* if the peer doesn't exist there is no key to disable
5738 		 * anymore
5739 		 */
5740 		return 0;
5741 	}
5742 
5743 	if (key->flags & IEEE80211_KEY_FLAG_PAIRWISE)
5744 		flags = WMI_KEY_PAIRWISE;
5745 	else
5746 		flags = WMI_KEY_GROUP;
5747 
5748 	ret = ath12k_install_key(arvif, key, cmd, peer_addr, flags);
5749 	if (ret) {
5750 		ath12k_warn(ab, "ath12k_install_key failed (%d)\n", ret);
5751 		return ret;
5752 	}
5753 
5754 	ret = ath12k_dp_rx_peer_pn_replay_config(arvif, peer_addr, cmd, key);
5755 	if (ret) {
5756 		ath12k_warn(ab, "failed to offload PN replay detection %d\n", ret);
5757 		return ret;
5758 	}
5759 
5760 	spin_lock_bh(&ab->base_lock);
5761 	peer = ath12k_peer_find(ab, arvif->vdev_id, peer_addr);
5762 	if (peer && cmd == SET_KEY) {
5763 		peer->keys[key->keyidx] = key;
5764 		if (key->flags & IEEE80211_KEY_FLAG_PAIRWISE) {
5765 			peer->ucast_keyidx = key->keyidx;
5766 			peer->sec_type = ath12k_dp_tx_get_encrypt_type(key->cipher);
5767 		} else {
5768 			peer->mcast_keyidx = key->keyidx;
5769 			peer->sec_type_grp = ath12k_dp_tx_get_encrypt_type(key->cipher);
5770 		}
5771 	} else if (peer && cmd == DISABLE_KEY) {
5772 		peer->keys[key->keyidx] = NULL;
5773 		if (key->flags & IEEE80211_KEY_FLAG_PAIRWISE)
5774 			peer->ucast_keyidx = 0;
5775 		else
5776 			peer->mcast_keyidx = 0;
5777 	} else if (!peer)
5778 		/* impossible unless FW goes crazy */
5779 		ath12k_warn(ab, "peer %pM disappeared!\n", peer_addr);
5780 
5781 	if (sta) {
5782 		ahsta = ath12k_sta_to_ahsta(sta);
5783 
5784 		switch (key->cipher) {
5785 		case WLAN_CIPHER_SUITE_TKIP:
5786 		case WLAN_CIPHER_SUITE_CCMP:
5787 		case WLAN_CIPHER_SUITE_CCMP_256:
5788 		case WLAN_CIPHER_SUITE_GCMP:
5789 		case WLAN_CIPHER_SUITE_GCMP_256:
5790 			if (cmd == SET_KEY)
5791 				ahsta->pn_type = HAL_PN_TYPE_WPA;
5792 			else
5793 				ahsta->pn_type = HAL_PN_TYPE_NONE;
5794 			break;
5795 		default:
5796 			ahsta->pn_type = HAL_PN_TYPE_NONE;
5797 			break;
5798 		}
5799 	}
5800 
5801 	spin_unlock_bh(&ab->base_lock);
5802 
5803 	return 0;
5804 }
5805 
5806 static int ath12k_mac_update_key_cache(struct ath12k_vif_cache *cache,
5807 				       enum set_key_cmd cmd,
5808 				       struct ieee80211_sta *sta,
5809 				       struct ieee80211_key_conf *key)
5810 {
5811 	struct ath12k_key_conf *key_conf, *tmp;
5812 
5813 	list_for_each_entry_safe(key_conf, tmp, &cache->key_conf.list, list) {
5814 		if (key_conf->key != key)
5815 			continue;
5816 
5817 		/* If SET key entry is already present in cache, nothing to do,
5818 		 * just return
5819 		 */
5820 		if (cmd == SET_KEY)
5821 			return 0;
5822 
5823 		/* DEL key for an old SET key which driver hasn't flushed yet.
5824 		 */
5825 		list_del(&key_conf->list);
5826 		kfree(key_conf);
5827 	}
5828 
5829 	if (cmd == SET_KEY) {
5830 		key_conf = kzalloc(sizeof(*key_conf), GFP_KERNEL);
5831 
5832 		if (!key_conf)
5833 			return -ENOMEM;
5834 
5835 		key_conf->cmd = cmd;
5836 		key_conf->sta = sta;
5837 		key_conf->key = key;
5838 		list_add_tail(&key_conf->list,
5839 			      &cache->key_conf.list);
5840 	}
5841 
5842 	return 0;
5843 }
5844 
5845 static int ath12k_mac_op_set_key(struct ieee80211_hw *hw, enum set_key_cmd cmd,
5846 				 struct ieee80211_vif *vif, struct ieee80211_sta *sta,
5847 				 struct ieee80211_key_conf *key)
5848 {
5849 	struct ath12k_vif *ahvif = ath12k_vif_to_ahvif(vif);
5850 	struct ath12k_link_vif *arvif;
5851 	struct ath12k_link_sta *arsta = NULL;
5852 	struct ath12k_vif_cache *cache;
5853 	struct ath12k_sta *ahsta;
5854 	unsigned long links;
5855 	u8 link_id;
5856 	int ret;
5857 
5858 	lockdep_assert_wiphy(hw->wiphy);
5859 
5860 	/* IGTK needs to be done in host software */
5861 	if (key->keyidx == 4 || key->keyidx == 5)
5862 		return 1;
5863 
5864 	if (key->keyidx > WMI_MAX_KEY_INDEX)
5865 		return -ENOSPC;
5866 
5867 	if (sta) {
5868 		ahsta = ath12k_sta_to_ahsta(sta);
5869 
5870 		/* For an ML STA Pairwise key is same for all associated link Stations,
5871 		 * hence do set key for all link STAs which are active.
5872 		 */
5873 		if (sta->mlo) {
5874 			links = ahsta->links_map;
5875 			for_each_set_bit(link_id, &links, IEEE80211_MLD_MAX_NUM_LINKS) {
5876 				arvif = wiphy_dereference(hw->wiphy,
5877 							  ahvif->link[link_id]);
5878 				arsta = wiphy_dereference(hw->wiphy,
5879 							  ahsta->link[link_id]);
5880 
5881 				if (WARN_ON(!arvif || !arsta))
5882 					/* arvif and arsta are expected to be valid when
5883 					 * STA is present.
5884 					 */
5885 					continue;
5886 
5887 				ret = ath12k_mac_set_key(arvif->ar, cmd, arvif,
5888 							 arsta, key);
5889 				if (ret)
5890 					break;
5891 			}
5892 
5893 			return 0;
5894 		}
5895 
5896 		arsta = &ahsta->deflink;
5897 		arvif = arsta->arvif;
5898 		if (WARN_ON(!arvif))
5899 			return -EINVAL;
5900 
5901 		ret = ath12k_mac_set_key(arvif->ar, cmd, arvif, arsta, key);
5902 		if (ret)
5903 			return ret;
5904 
5905 		return 0;
5906 	}
5907 
5908 	if (key->link_id >= 0 && key->link_id < IEEE80211_MLD_MAX_NUM_LINKS) {
5909 		link_id = key->link_id;
5910 		arvif = wiphy_dereference(hw->wiphy, ahvif->link[link_id]);
5911 	} else {
5912 		link_id = 0;
5913 		arvif = &ahvif->deflink;
5914 	}
5915 
5916 	if (!arvif || !arvif->is_created) {
5917 		cache = ath12k_ahvif_get_link_cache(ahvif, link_id);
5918 		if (!cache)
5919 			return -ENOSPC;
5920 
5921 		ret = ath12k_mac_update_key_cache(cache, cmd, sta, key);
5922 		if (ret)
5923 			return ret;
5924 
5925 		return 0;
5926 	}
5927 
5928 	ret = ath12k_mac_set_key(arvif->ar, cmd, arvif, NULL, key);
5929 	if (ret)
5930 		return ret;
5931 
5932 	return 0;
5933 }
5934 
5935 static int
5936 ath12k_mac_bitrate_mask_num_vht_rates(struct ath12k *ar,
5937 				      enum nl80211_band band,
5938 				      const struct cfg80211_bitrate_mask *mask)
5939 {
5940 	int num_rates = 0;
5941 	int i;
5942 
5943 	for (i = 0; i < ARRAY_SIZE(mask->control[band].vht_mcs); i++)
5944 		num_rates += hweight16(mask->control[band].vht_mcs[i]);
5945 
5946 	return num_rates;
5947 }
5948 
5949 static int
5950 ath12k_mac_bitrate_mask_num_he_rates(struct ath12k *ar,
5951 				     enum nl80211_band band,
5952 				     const struct cfg80211_bitrate_mask *mask)
5953 {
5954 	int num_rates = 0;
5955 	int i;
5956 
5957 	for (i = 0; i < ARRAY_SIZE(mask->control[band].he_mcs); i++)
5958 		num_rates += hweight16(mask->control[band].he_mcs[i]);
5959 
5960 	return num_rates;
5961 }
5962 
5963 static int
5964 ath12k_mac_bitrate_mask_num_eht_rates(struct ath12k *ar,
5965 				      enum nl80211_band band,
5966 				      const struct cfg80211_bitrate_mask *mask)
5967 {
5968 	int num_rates = 0;
5969 	int i;
5970 
5971 	for (i = 0; i < ARRAY_SIZE(mask->control[band].eht_mcs); i++)
5972 		num_rates += hweight16(mask->control[band].eht_mcs[i]);
5973 
5974 	return num_rates;
5975 }
5976 
5977 static int
5978 ath12k_mac_set_peer_vht_fixed_rate(struct ath12k_link_vif *arvif,
5979 				   struct ath12k_link_sta *arsta,
5980 				   const struct cfg80211_bitrate_mask *mask,
5981 				   enum nl80211_band band)
5982 {
5983 	struct ath12k *ar = arvif->ar;
5984 	u8 vht_rate, nss;
5985 	u32 rate_code;
5986 	int ret, i;
5987 
5988 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
5989 
5990 	nss = 0;
5991 
5992 	for (i = 0; i < ARRAY_SIZE(mask->control[band].vht_mcs); i++) {
5993 		if (hweight16(mask->control[band].vht_mcs[i]) == 1) {
5994 			nss = i + 1;
5995 			vht_rate = ffs(mask->control[band].vht_mcs[i]) - 1;
5996 		}
5997 	}
5998 
5999 	if (!nss) {
6000 		ath12k_warn(ar->ab, "No single VHT Fixed rate found to set for %pM",
6001 			    arsta->addr);
6002 		return -EINVAL;
6003 	}
6004 
6005 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
6006 		   "Setting Fixed VHT Rate for peer %pM. Device will not switch to any other selected rates",
6007 		   arsta->addr);
6008 
6009 	rate_code = ATH12K_HW_RATE_CODE(vht_rate, nss - 1,
6010 					WMI_RATE_PREAMBLE_VHT);
6011 	ret = ath12k_wmi_set_peer_param(ar, arsta->addr,
6012 					arvif->vdev_id,
6013 					WMI_PEER_PARAM_FIXED_RATE,
6014 					rate_code);
6015 	if (ret)
6016 		ath12k_warn(ar->ab,
6017 			    "failed to update STA %pM Fixed Rate %d: %d\n",
6018 			     arsta->addr, rate_code, ret);
6019 
6020 	return ret;
6021 }
6022 
6023 static int
6024 ath12k_mac_set_peer_he_fixed_rate(struct ath12k_link_vif *arvif,
6025 				  struct ath12k_link_sta *arsta,
6026 				  const struct cfg80211_bitrate_mask *mask,
6027 				  enum nl80211_band band)
6028 {
6029 	struct ath12k *ar = arvif->ar;
6030 	u8 he_rate, nss;
6031 	u32 rate_code;
6032 	int ret, i;
6033 	struct ath12k_sta *ahsta = arsta->ahsta;
6034 	struct ieee80211_sta *sta;
6035 
6036 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
6037 
6038 	sta = ath12k_ahsta_to_sta(ahsta);
6039 	nss = 0;
6040 
6041 	for (i = 0; i < ARRAY_SIZE(mask->control[band].he_mcs); i++) {
6042 		if (hweight16(mask->control[band].he_mcs[i]) == 1) {
6043 			nss = i + 1;
6044 			he_rate = ffs(mask->control[band].he_mcs[i]) - 1;
6045 		}
6046 	}
6047 
6048 	if (!nss) {
6049 		ath12k_warn(ar->ab, "No single HE Fixed rate found to set for %pM",
6050 			    arsta->addr);
6051 		return -EINVAL;
6052 	}
6053 
6054 	/* Avoid updating invalid nss as fixed rate*/
6055 	if (nss > sta->deflink.rx_nss)
6056 		return -EINVAL;
6057 
6058 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
6059 		   "Setting Fixed HE Rate for peer %pM. Device will not switch to any other selected rates",
6060 		   arsta->addr);
6061 
6062 	rate_code = ATH12K_HW_RATE_CODE(he_rate, nss - 1,
6063 					WMI_RATE_PREAMBLE_HE);
6064 
6065 	ret = ath12k_wmi_set_peer_param(ar, arsta->addr,
6066 					arvif->vdev_id,
6067 					WMI_PEER_PARAM_FIXED_RATE,
6068 					rate_code);
6069 	if (ret)
6070 		ath12k_warn(ar->ab,
6071 			    "failed to update STA %pM Fixed Rate %d: %d\n",
6072 			    arsta->addr, rate_code, ret);
6073 
6074 	return ret;
6075 }
6076 
6077 static int
6078 ath12k_mac_set_peer_eht_fixed_rate(struct ath12k_link_vif *arvif,
6079 				   struct ath12k_link_sta *arsta,
6080 				   const struct cfg80211_bitrate_mask *mask,
6081 				   enum nl80211_band band)
6082 {
6083 	struct ath12k_sta *ahsta = arsta->ahsta;
6084 	struct ath12k *ar = arvif->ar;
6085 	struct ieee80211_sta *sta;
6086 	struct ieee80211_link_sta *link_sta;
6087 	u8 eht_rate, nss = 0;
6088 	u32 rate_code;
6089 	int ret, i;
6090 
6091 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
6092 
6093 	sta = ath12k_ahsta_to_sta(ahsta);
6094 
6095 	for (i = 0; i < ARRAY_SIZE(mask->control[band].eht_mcs); i++) {
6096 		if (hweight16(mask->control[band].eht_mcs[i]) == 1) {
6097 			nss = i + 1;
6098 			eht_rate = ffs(mask->control[band].eht_mcs[i]) - 1;
6099 		}
6100 	}
6101 
6102 	if (!nss) {
6103 		ath12k_warn(ar->ab, "No single EHT Fixed rate found to set for %pM\n",
6104 			    arsta->addr);
6105 		return -EINVAL;
6106 	}
6107 
6108 	/* Avoid updating invalid nss as fixed rate*/
6109 	link_sta = ath12k_mac_get_link_sta(arsta);
6110 	if (!link_sta || nss > link_sta->rx_nss) {
6111 		ath12k_warn(ar->ab,
6112 			    "unable to access link sta for sta %pM link %u or fixed nss of %u is not supported by sta\n",
6113 			    sta->addr, arsta->link_id, nss);
6114 		return -EINVAL;
6115 	}
6116 
6117 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
6118 		   "Setting Fixed EHT Rate for peer %pM. Device will not switch to any other selected rates\n",
6119 		   arsta->addr);
6120 
6121 	rate_code = ATH12K_HW_RATE_CODE(eht_rate, nss - 1,
6122 					WMI_RATE_PREAMBLE_EHT);
6123 
6124 	ret = ath12k_wmi_set_peer_param(ar, arsta->addr,
6125 					arvif->vdev_id,
6126 					WMI_PEER_PARAM_FIXED_RATE,
6127 					rate_code);
6128 	if (ret)
6129 		ath12k_warn(ar->ab,
6130 			    "failed to update STA %pM Fixed Rate %d: %d\n",
6131 			    arsta->addr, rate_code, ret);
6132 
6133 	return ret;
6134 }
6135 
6136 static int ath12k_mac_station_assoc(struct ath12k *ar,
6137 				    struct ath12k_link_vif *arvif,
6138 				    struct ath12k_link_sta *arsta,
6139 				    bool reassoc)
6140 {
6141 	struct ieee80211_vif *vif = ath12k_ahvif_to_vif(arvif->ahvif);
6142 	struct ieee80211_sta *sta = ath12k_ahsta_to_sta(arsta->ahsta);
6143 	struct ieee80211_link_sta *link_sta;
6144 	int ret;
6145 	struct cfg80211_chan_def def;
6146 	enum nl80211_band band;
6147 	struct cfg80211_bitrate_mask *mask;
6148 	u8 num_vht_rates, num_he_rates, num_eht_rates;
6149 	u8 link_id = arvif->link_id;
6150 
6151 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
6152 
6153 	if (WARN_ON(ath12k_mac_vif_link_chan(vif, arvif->link_id, &def)))
6154 		return -EPERM;
6155 
6156 	if (WARN_ON(!rcu_access_pointer(sta->link[link_id])))
6157 		return -EINVAL;
6158 
6159 	band = def.chan->band;
6160 	mask = &arvif->bitrate_mask;
6161 
6162 	struct ath12k_wmi_peer_assoc_arg *peer_arg __free(kfree) =
6163 		kzalloc(sizeof(*peer_arg), GFP_KERNEL);
6164 	if (!peer_arg)
6165 		return -ENOMEM;
6166 
6167 	ath12k_peer_assoc_prepare(ar, arvif, arsta, peer_arg, reassoc);
6168 
6169 	if (peer_arg->peer_nss < 1) {
6170 		ath12k_warn(ar->ab,
6171 			    "invalid peer NSS %d\n", peer_arg->peer_nss);
6172 		return -EINVAL;
6173 	}
6174 
6175 	peer_arg->is_assoc = true;
6176 	ret = ath12k_wmi_send_peer_assoc_cmd(ar, peer_arg);
6177 	if (ret) {
6178 		ath12k_warn(ar->ab, "failed to run peer assoc for STA %pM vdev %i: %d\n",
6179 			    arsta->addr, arvif->vdev_id, ret);
6180 		return ret;
6181 	}
6182 
6183 	if (!wait_for_completion_timeout(&ar->peer_assoc_done, 1 * HZ)) {
6184 		ath12k_warn(ar->ab, "failed to get peer assoc conf event for %pM vdev %i\n",
6185 			    arsta->addr, arvif->vdev_id);
6186 		return -ETIMEDOUT;
6187 	}
6188 
6189 	num_vht_rates = ath12k_mac_bitrate_mask_num_vht_rates(ar, band, mask);
6190 	num_he_rates = ath12k_mac_bitrate_mask_num_he_rates(ar, band, mask);
6191 	num_eht_rates = ath12k_mac_bitrate_mask_num_eht_rates(ar, band, mask);
6192 
6193 	/* If single VHT/HE/EHT rate is configured (by set_bitrate_mask()),
6194 	 * peer_assoc will disable VHT/HE/EHT. This is now enabled by a peer
6195 	 * specific fixed param.
6196 	 * Note that all other rates and NSS will be disabled for this peer.
6197 	 */
6198 	link_sta = ath12k_mac_get_link_sta(arsta);
6199 	if (!link_sta) {
6200 		ath12k_warn(ar->ab, "unable to access link sta in station assoc\n");
6201 		return -EINVAL;
6202 	}
6203 
6204 	spin_lock_bh(&ar->data_lock);
6205 	arsta->bw = ath12k_mac_ieee80211_sta_bw_to_wmi(ar, link_sta);
6206 	arsta->bw_prev = link_sta->bandwidth;
6207 	spin_unlock_bh(&ar->data_lock);
6208 
6209 	if (link_sta->vht_cap.vht_supported && num_vht_rates == 1) {
6210 		ret = ath12k_mac_set_peer_vht_fixed_rate(arvif, arsta, mask, band);
6211 	} else if (link_sta->he_cap.has_he && num_he_rates == 1) {
6212 		ret = ath12k_mac_set_peer_he_fixed_rate(arvif, arsta, mask, band);
6213 		if (ret)
6214 			return ret;
6215 	} else if (link_sta->eht_cap.has_eht && num_eht_rates == 1) {
6216 		ret = ath12k_mac_set_peer_eht_fixed_rate(arvif, arsta, mask, band);
6217 		if (ret)
6218 			return ret;
6219 	}
6220 
6221 	/* Re-assoc is run only to update supported rates for given station. It
6222 	 * doesn't make much sense to reconfigure the peer completely.
6223 	 */
6224 	if (reassoc)
6225 		return 0;
6226 
6227 	ret = ath12k_setup_peer_smps(ar, arvif, arsta->addr,
6228 				     &link_sta->ht_cap, &link_sta->he_6ghz_capa);
6229 	if (ret) {
6230 		ath12k_warn(ar->ab, "failed to setup peer SMPS for vdev %d: %d\n",
6231 			    arvif->vdev_id, ret);
6232 		return ret;
6233 	}
6234 
6235 	if (!sta->wme) {
6236 		arvif->num_legacy_stations++;
6237 		ret = ath12k_recalc_rtscts_prot(arvif);
6238 		if (ret)
6239 			return ret;
6240 	}
6241 
6242 	if (sta->wme && sta->uapsd_queues) {
6243 		ret = ath12k_peer_assoc_qos_ap(ar, arvif, arsta);
6244 		if (ret) {
6245 			ath12k_warn(ar->ab, "failed to set qos params for STA %pM for vdev %i: %d\n",
6246 				    arsta->addr, arvif->vdev_id, ret);
6247 			return ret;
6248 		}
6249 	}
6250 
6251 	return 0;
6252 }
6253 
6254 static int ath12k_mac_station_disassoc(struct ath12k *ar,
6255 				       struct ath12k_link_vif *arvif,
6256 				       struct ath12k_link_sta *arsta)
6257 {
6258 	struct ieee80211_sta *sta = ath12k_ahsta_to_sta(arsta->ahsta);
6259 
6260 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
6261 
6262 	if (!sta->wme) {
6263 		arvif->num_legacy_stations--;
6264 		return ath12k_recalc_rtscts_prot(arvif);
6265 	}
6266 
6267 	return 0;
6268 }
6269 
6270 static void ath12k_sta_rc_update_wk(struct wiphy *wiphy, struct wiphy_work *wk)
6271 {
6272 	struct ieee80211_link_sta *link_sta;
6273 	struct ath12k *ar;
6274 	struct ath12k_link_vif *arvif;
6275 	struct ieee80211_sta *sta;
6276 	struct cfg80211_chan_def def;
6277 	enum nl80211_band band;
6278 	const u8 *ht_mcs_mask;
6279 	const u16 *vht_mcs_mask;
6280 	const u16 *he_mcs_mask;
6281 	const u16 *eht_mcs_mask;
6282 	u32 changed, bw, nss, mac_nss, smps, bw_prev;
6283 	int err, num_vht_rates, num_he_rates, num_eht_rates;
6284 	const struct cfg80211_bitrate_mask *mask;
6285 	enum wmi_phy_mode peer_phymode;
6286 	struct ath12k_link_sta *arsta;
6287 	struct ieee80211_vif *vif;
6288 
6289 	lockdep_assert_wiphy(wiphy);
6290 
6291 	arsta = container_of(wk, struct ath12k_link_sta, update_wk);
6292 	sta = ath12k_ahsta_to_sta(arsta->ahsta);
6293 	arvif = arsta->arvif;
6294 	vif = ath12k_ahvif_to_vif(arvif->ahvif);
6295 	ar = arvif->ar;
6296 
6297 	if (WARN_ON(ath12k_mac_vif_link_chan(vif, arvif->link_id, &def)))
6298 		return;
6299 
6300 	band = def.chan->band;
6301 	ht_mcs_mask = arvif->bitrate_mask.control[band].ht_mcs;
6302 	vht_mcs_mask = arvif->bitrate_mask.control[band].vht_mcs;
6303 	he_mcs_mask = arvif->bitrate_mask.control[band].he_mcs;
6304 	eht_mcs_mask = arvif->bitrate_mask.control[band].eht_mcs;
6305 
6306 	spin_lock_bh(&ar->data_lock);
6307 
6308 	changed = arsta->changed;
6309 	arsta->changed = 0;
6310 
6311 	bw = arsta->bw;
6312 	bw_prev = arsta->bw_prev;
6313 	nss = arsta->nss;
6314 	smps = arsta->smps;
6315 
6316 	spin_unlock_bh(&ar->data_lock);
6317 
6318 	nss = max_t(u32, 1, nss);
6319 	mac_nss = max3(ath12k_mac_max_ht_nss(ht_mcs_mask),
6320 		       ath12k_mac_max_vht_nss(vht_mcs_mask),
6321 		       ath12k_mac_max_he_nss(he_mcs_mask));
6322 	mac_nss = max(mac_nss, ath12k_mac_max_eht_nss(eht_mcs_mask));
6323 	nss = min(nss, mac_nss);
6324 
6325 	struct ath12k_wmi_peer_assoc_arg *peer_arg __free(kfree) =
6326 					kzalloc(sizeof(*peer_arg), GFP_KERNEL);
6327 	if (!peer_arg)
6328 		return;
6329 
6330 	if (changed & IEEE80211_RC_BW_CHANGED) {
6331 		ath12k_peer_assoc_h_phymode(ar, arvif, arsta, peer_arg);
6332 		peer_phymode = peer_arg->peer_phymode;
6333 
6334 		if (bw > bw_prev) {
6335 			/* Phymode shows maximum supported channel width, if we
6336 			 * upgrade bandwidth then due to sanity check of firmware,
6337 			 * we have to send WMI_PEER_PHYMODE followed by
6338 			 * WMI_PEER_CHWIDTH
6339 			 */
6340 			ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac bandwidth upgrade for sta %pM new %d old %d\n",
6341 				   arsta->addr, bw, bw_prev);
6342 			err = ath12k_wmi_set_peer_param(ar, arsta->addr,
6343 							arvif->vdev_id, WMI_PEER_PHYMODE,
6344 							peer_phymode);
6345 			if (err) {
6346 				ath12k_warn(ar->ab, "failed to update STA %pM to peer phymode %d: %d\n",
6347 					    arsta->addr, peer_phymode, err);
6348 				return;
6349 			}
6350 			err = ath12k_wmi_set_peer_param(ar, arsta->addr,
6351 							arvif->vdev_id, WMI_PEER_CHWIDTH,
6352 							bw);
6353 			if (err)
6354 				ath12k_warn(ar->ab, "failed to update STA %pM to peer bandwidth %d: %d\n",
6355 					    arsta->addr, bw, err);
6356 		} else {
6357 			/* When we downgrade bandwidth this will conflict with phymode
6358 			 * and cause to trigger firmware crash. In this case we send
6359 			 * WMI_PEER_CHWIDTH followed by WMI_PEER_PHYMODE
6360 			 */
6361 			ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac bandwidth downgrade for sta %pM new %d old %d\n",
6362 				   arsta->addr, bw, bw_prev);
6363 			err = ath12k_wmi_set_peer_param(ar, arsta->addr,
6364 							arvif->vdev_id, WMI_PEER_CHWIDTH,
6365 							bw);
6366 			if (err) {
6367 				ath12k_warn(ar->ab, "failed to update STA %pM peer to bandwidth %d: %d\n",
6368 					    arsta->addr, bw, err);
6369 				return;
6370 			}
6371 			err = ath12k_wmi_set_peer_param(ar, arsta->addr,
6372 							arvif->vdev_id, WMI_PEER_PHYMODE,
6373 							peer_phymode);
6374 			if (err)
6375 				ath12k_warn(ar->ab, "failed to update STA %pM to peer phymode %d: %d\n",
6376 					    arsta->addr, peer_phymode, err);
6377 		}
6378 	}
6379 
6380 	if (changed & IEEE80211_RC_NSS_CHANGED) {
6381 		ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac update sta %pM nss %d\n",
6382 			   arsta->addr, nss);
6383 
6384 		err = ath12k_wmi_set_peer_param(ar, arsta->addr, arvif->vdev_id,
6385 						WMI_PEER_NSS, nss);
6386 		if (err)
6387 			ath12k_warn(ar->ab, "failed to update STA %pM nss %d: %d\n",
6388 				    arsta->addr, nss, err);
6389 	}
6390 
6391 	if (changed & IEEE80211_RC_SMPS_CHANGED) {
6392 		ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac update sta %pM smps %d\n",
6393 			   arsta->addr, smps);
6394 
6395 		err = ath12k_wmi_set_peer_param(ar, arsta->addr, arvif->vdev_id,
6396 						WMI_PEER_MIMO_PS_STATE, smps);
6397 		if (err)
6398 			ath12k_warn(ar->ab, "failed to update STA %pM smps %d: %d\n",
6399 				    arsta->addr, smps, err);
6400 	}
6401 
6402 	if (changed & IEEE80211_RC_SUPP_RATES_CHANGED) {
6403 		mask = &arvif->bitrate_mask;
6404 		num_vht_rates = ath12k_mac_bitrate_mask_num_vht_rates(ar, band,
6405 								      mask);
6406 		num_he_rates = ath12k_mac_bitrate_mask_num_he_rates(ar, band,
6407 								    mask);
6408 		num_eht_rates = ath12k_mac_bitrate_mask_num_eht_rates(ar, band,
6409 								      mask);
6410 
6411 		/* Peer_assoc_prepare will reject vht rates in
6412 		 * bitrate_mask if its not available in range format and
6413 		 * sets vht tx_rateset as unsupported. So multiple VHT MCS
6414 		 * setting(eg. MCS 4,5,6) per peer is not supported here.
6415 		 * But, Single rate in VHT mask can be set as per-peer
6416 		 * fixed rate. But even if any HT rates are configured in
6417 		 * the bitrate mask, device will not switch to those rates
6418 		 * when per-peer Fixed rate is set.
6419 		 * TODO: Check RATEMASK_CMDID to support auto rates selection
6420 		 * across HT/VHT and for multiple VHT MCS support.
6421 		 */
6422 		link_sta = ath12k_mac_get_link_sta(arsta);
6423 		if (!link_sta) {
6424 			ath12k_warn(ar->ab, "unable to access link sta in peer assoc he for sta %pM link %u\n",
6425 				    sta->addr, arsta->link_id);
6426 			return;
6427 		}
6428 
6429 		if (link_sta->vht_cap.vht_supported && num_vht_rates == 1) {
6430 			ath12k_mac_set_peer_vht_fixed_rate(arvif, arsta, mask,
6431 							   band);
6432 		} else if (link_sta->he_cap.has_he && num_he_rates == 1) {
6433 			ath12k_mac_set_peer_he_fixed_rate(arvif, arsta, mask, band);
6434 		} else if (link_sta->eht_cap.has_eht && num_eht_rates == 1) {
6435 			err = ath12k_mac_set_peer_eht_fixed_rate(arvif, arsta,
6436 								 mask, band);
6437 			if (err) {
6438 				ath12k_warn(ar->ab,
6439 					    "failed to set peer EHT fixed rate for STA %pM ret %d\n",
6440 					    arsta->addr, err);
6441 				return;
6442 			}
6443 		} else {
6444 			/* If the peer is non-VHT/HE/EHT or no fixed VHT/HE/EHT
6445 			 * rate is provided in the new bitrate mask we set the
6446 			 * other rates using peer_assoc command. Also clear
6447 			 * the peer fixed rate settings as it has higher proprity
6448 			 * than peer assoc
6449 			 */
6450 			err = ath12k_wmi_set_peer_param(ar, arsta->addr,
6451 							arvif->vdev_id,
6452 							WMI_PEER_PARAM_FIXED_RATE,
6453 							WMI_FIXED_RATE_NONE);
6454 			if (err)
6455 				ath12k_warn(ar->ab,
6456 					    "failed to disable peer fixed rate for STA %pM ret %d\n",
6457 					    arsta->addr, err);
6458 
6459 			ath12k_peer_assoc_prepare(ar, arvif, arsta,
6460 						  peer_arg, true);
6461 
6462 			peer_arg->is_assoc = false;
6463 			err = ath12k_wmi_send_peer_assoc_cmd(ar, peer_arg);
6464 			if (err)
6465 				ath12k_warn(ar->ab, "failed to run peer assoc for STA %pM vdev %i: %d\n",
6466 					    arsta->addr, arvif->vdev_id, err);
6467 
6468 			if (!wait_for_completion_timeout(&ar->peer_assoc_done, 1 * HZ))
6469 				ath12k_warn(ar->ab, "failed to get peer assoc conf event for %pM vdev %i\n",
6470 					    arsta->addr, arvif->vdev_id);
6471 		}
6472 	}
6473 }
6474 
6475 static void ath12k_mac_free_unassign_link_sta(struct ath12k_hw *ah,
6476 					      struct ath12k_sta *ahsta,
6477 					      u8 link_id)
6478 {
6479 	struct ath12k_link_sta *arsta;
6480 
6481 	lockdep_assert_wiphy(ah->hw->wiphy);
6482 
6483 	if (WARN_ON(link_id >= IEEE80211_MLD_MAX_NUM_LINKS))
6484 		return;
6485 
6486 	arsta = wiphy_dereference(ah->hw->wiphy, ahsta->link[link_id]);
6487 	if (WARN_ON(!arsta))
6488 		return;
6489 
6490 	ahsta->links_map &= ~BIT(link_id);
6491 	rcu_assign_pointer(ahsta->link[link_id], NULL);
6492 	synchronize_rcu();
6493 
6494 	if (arsta == &ahsta->deflink) {
6495 		arsta->link_id = ATH12K_INVALID_LINK_ID;
6496 		arsta->ahsta = NULL;
6497 		arsta->arvif = NULL;
6498 		return;
6499 	}
6500 
6501 	kfree(arsta);
6502 }
6503 
6504 static int ath12k_mac_inc_num_stations(struct ath12k_link_vif *arvif,
6505 				       struct ath12k_link_sta *arsta)
6506 {
6507 	struct ieee80211_sta *sta = ath12k_ahsta_to_sta(arsta->ahsta);
6508 	struct ath12k *ar = arvif->ar;
6509 
6510 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
6511 
6512 	if (arvif->ahvif->vdev_type == WMI_VDEV_TYPE_STA && !sta->tdls)
6513 		return 0;
6514 
6515 	if (ar->num_stations >= ar->max_num_stations)
6516 		return -ENOBUFS;
6517 
6518 	ar->num_stations++;
6519 	arvif->num_stations++;
6520 
6521 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
6522 		   "mac station %pM connected to vdev %u num_stations %u\n",
6523 		   arsta->addr, arvif->vdev_id, arvif->num_stations);
6524 
6525 	return 0;
6526 }
6527 
6528 static void ath12k_mac_dec_num_stations(struct ath12k_link_vif *arvif,
6529 					struct ath12k_link_sta *arsta)
6530 {
6531 	struct ieee80211_sta *sta = ath12k_ahsta_to_sta(arsta->ahsta);
6532 	struct ath12k *ar = arvif->ar;
6533 
6534 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
6535 
6536 	if (arvif->ahvif->vdev_type == WMI_VDEV_TYPE_STA && !sta->tdls)
6537 		return;
6538 
6539 	ar->num_stations--;
6540 
6541 	if (arvif->num_stations) {
6542 		arvif->num_stations--;
6543 		ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
6544 			   "mac station %pM disconnected from vdev %u num_stations %u\n",
6545 			   arsta->addr, arvif->vdev_id, arvif->num_stations);
6546 	} else {
6547 		ath12k_warn(ar->ab,
6548 			    "mac station %pM disconnect for vdev %u without any connected station\n",
6549 			    arsta->addr, arvif->vdev_id);
6550 	}
6551 }
6552 
6553 static void ath12k_mac_station_post_remove(struct ath12k *ar,
6554 					   struct ath12k_link_vif *arvif,
6555 					   struct ath12k_link_sta *arsta)
6556 {
6557 	struct ieee80211_vif *vif = ath12k_ahvif_to_vif(arvif->ahvif);
6558 	struct ieee80211_sta *sta = ath12k_ahsta_to_sta(arsta->ahsta);
6559 	struct ath12k_peer *peer;
6560 
6561 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
6562 
6563 	ath12k_mac_dec_num_stations(arvif, arsta);
6564 
6565 	spin_lock_bh(&ar->ab->base_lock);
6566 
6567 	peer = ath12k_peer_find(ar->ab, arvif->vdev_id, arsta->addr);
6568 	if (peer && peer->sta == sta) {
6569 		ath12k_warn(ar->ab, "Found peer entry %pM n vdev %i after it was supposedly removed\n",
6570 			    vif->addr, arvif->vdev_id);
6571 		peer->sta = NULL;
6572 		list_del(&peer->list);
6573 		kfree(peer);
6574 		ar->num_peers--;
6575 	}
6576 
6577 	spin_unlock_bh(&ar->ab->base_lock);
6578 
6579 	kfree(arsta->rx_stats);
6580 	arsta->rx_stats = NULL;
6581 }
6582 
6583 static int ath12k_mac_station_unauthorize(struct ath12k *ar,
6584 					  struct ath12k_link_vif *arvif,
6585 					  struct ath12k_link_sta *arsta)
6586 {
6587 	struct ath12k_peer *peer;
6588 	int ret;
6589 
6590 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
6591 
6592 	spin_lock_bh(&ar->ab->base_lock);
6593 
6594 	peer = ath12k_peer_find(ar->ab, arvif->vdev_id, arsta->addr);
6595 	if (peer)
6596 		peer->is_authorized = false;
6597 
6598 	spin_unlock_bh(&ar->ab->base_lock);
6599 
6600 	/* Driver must clear the keys during the state change from
6601 	 * IEEE80211_STA_AUTHORIZED to IEEE80211_STA_ASSOC, since after
6602 	 * returning from here, mac80211 is going to delete the keys
6603 	 * in __sta_info_destroy_part2(). This will ensure that the driver does
6604 	 * not retain stale key references after mac80211 deletes the keys.
6605 	 */
6606 	ret = ath12k_clear_peer_keys(arvif, arsta->addr);
6607 	if (ret) {
6608 		ath12k_warn(ar->ab, "failed to clear all peer keys for vdev %i: %d\n",
6609 			    arvif->vdev_id, ret);
6610 		return ret;
6611 	}
6612 
6613 	return 0;
6614 }
6615 
6616 static int ath12k_mac_station_authorize(struct ath12k *ar,
6617 					struct ath12k_link_vif *arvif,
6618 					struct ath12k_link_sta *arsta)
6619 {
6620 	struct ath12k_peer *peer;
6621 	struct ieee80211_vif *vif = ath12k_ahvif_to_vif(arvif->ahvif);
6622 	int ret;
6623 
6624 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
6625 
6626 	spin_lock_bh(&ar->ab->base_lock);
6627 
6628 	peer = ath12k_peer_find(ar->ab, arvif->vdev_id, arsta->addr);
6629 	if (peer)
6630 		peer->is_authorized = true;
6631 
6632 	spin_unlock_bh(&ar->ab->base_lock);
6633 
6634 	if (vif->type == NL80211_IFTYPE_STATION && arvif->is_up) {
6635 		ret = ath12k_wmi_set_peer_param(ar, arsta->addr,
6636 						arvif->vdev_id,
6637 						WMI_PEER_AUTHORIZE,
6638 						1);
6639 		if (ret) {
6640 			ath12k_warn(ar->ab, "Unable to authorize peer %pM vdev %d: %d\n",
6641 				    arsta->addr, arvif->vdev_id, ret);
6642 			return ret;
6643 		}
6644 	}
6645 
6646 	return 0;
6647 }
6648 
6649 static int ath12k_mac_station_remove(struct ath12k *ar,
6650 				     struct ath12k_link_vif *arvif,
6651 				     struct ath12k_link_sta *arsta)
6652 {
6653 	struct ieee80211_sta *sta = ath12k_ahsta_to_sta(arsta->ahsta);
6654 	struct ath12k_vif *ahvif = arvif->ahvif;
6655 	int ret = 0;
6656 
6657 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
6658 
6659 	wiphy_work_cancel(ar->ah->hw->wiphy, &arsta->update_wk);
6660 
6661 	if (ahvif->vdev_type == WMI_VDEV_TYPE_STA) {
6662 		ath12k_bss_disassoc(ar, arvif);
6663 		ret = ath12k_mac_vdev_stop(arvif);
6664 		if (ret)
6665 			ath12k_warn(ar->ab, "failed to stop vdev %i: %d\n",
6666 				    arvif->vdev_id, ret);
6667 	}
6668 
6669 	if (sta->mlo)
6670 		return ret;
6671 
6672 	ath12k_dp_peer_cleanup(ar, arvif->vdev_id, arsta->addr);
6673 
6674 	ret = ath12k_peer_delete(ar, arvif->vdev_id, arsta->addr);
6675 	if (ret)
6676 		ath12k_warn(ar->ab, "Failed to delete peer: %pM for VDEV: %d\n",
6677 			    arsta->addr, arvif->vdev_id);
6678 	else
6679 		ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "Removed peer: %pM for VDEV: %d\n",
6680 			   arsta->addr, arvif->vdev_id);
6681 
6682 	ath12k_mac_station_post_remove(ar, arvif, arsta);
6683 
6684 	if (sta->valid_links)
6685 		ath12k_mac_free_unassign_link_sta(ahvif->ah,
6686 						  arsta->ahsta, arsta->link_id);
6687 
6688 	return ret;
6689 }
6690 
6691 static int ath12k_mac_station_add(struct ath12k *ar,
6692 				  struct ath12k_link_vif *arvif,
6693 				  struct ath12k_link_sta *arsta)
6694 {
6695 	struct ath12k_base *ab = ar->ab;
6696 	struct ieee80211_vif *vif = ath12k_ahvif_to_vif(arvif->ahvif);
6697 	struct ieee80211_sta *sta = ath12k_ahsta_to_sta(arsta->ahsta);
6698 	struct ath12k_wmi_peer_create_arg peer_param = {};
6699 	int ret;
6700 
6701 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
6702 
6703 	ret = ath12k_mac_inc_num_stations(arvif, arsta);
6704 	if (ret) {
6705 		ath12k_warn(ab, "refusing to associate station: too many connected already (%d)\n",
6706 			    ar->max_num_stations);
6707 		goto exit;
6708 	}
6709 
6710 	if (ath12k_debugfs_is_extd_rx_stats_enabled(ar) && !arsta->rx_stats) {
6711 		arsta->rx_stats = kzalloc(sizeof(*arsta->rx_stats), GFP_KERNEL);
6712 		if (!arsta->rx_stats) {
6713 			ret = -ENOMEM;
6714 			goto dec_num_station;
6715 		}
6716 	}
6717 
6718 	peer_param.vdev_id = arvif->vdev_id;
6719 	peer_param.peer_addr = arsta->addr;
6720 	peer_param.peer_type = WMI_PEER_TYPE_DEFAULT;
6721 	peer_param.ml_enabled = sta->mlo;
6722 
6723 	ret = ath12k_peer_create(ar, arvif, sta, &peer_param);
6724 	if (ret) {
6725 		ath12k_warn(ab, "Failed to add peer: %pM for VDEV: %d\n",
6726 			    arsta->addr, arvif->vdev_id);
6727 		goto free_peer;
6728 	}
6729 
6730 	ath12k_dbg(ab, ATH12K_DBG_MAC, "Added peer: %pM for VDEV: %d\n",
6731 		   arsta->addr, arvif->vdev_id);
6732 
6733 	if (ieee80211_vif_is_mesh(vif)) {
6734 		ret = ath12k_wmi_set_peer_param(ar, arsta->addr,
6735 						arvif->vdev_id,
6736 						WMI_PEER_USE_4ADDR, 1);
6737 		if (ret) {
6738 			ath12k_warn(ab, "failed to STA %pM 4addr capability: %d\n",
6739 				    arsta->addr, ret);
6740 			goto free_peer;
6741 		}
6742 	}
6743 
6744 	ret = ath12k_dp_peer_setup(ar, arvif->vdev_id, arsta->addr);
6745 	if (ret) {
6746 		ath12k_warn(ab, "failed to setup dp for peer %pM on vdev %i (%d)\n",
6747 			    arsta->addr, arvif->vdev_id, ret);
6748 		goto free_peer;
6749 	}
6750 
6751 	if (ab->hw_params->vdev_start_delay &&
6752 	    !arvif->is_started &&
6753 	    arvif->ahvif->vdev_type != WMI_VDEV_TYPE_AP) {
6754 		ret = ath12k_start_vdev_delay(ar, arvif);
6755 		if (ret) {
6756 			ath12k_warn(ab, "failed to delay vdev start: %d\n", ret);
6757 			goto free_peer;
6758 		}
6759 	}
6760 
6761 	ewma_avg_rssi_init(&arsta->avg_rssi);
6762 	return 0;
6763 
6764 free_peer:
6765 	ath12k_peer_delete(ar, arvif->vdev_id, arsta->addr);
6766 	kfree(arsta->rx_stats);
6767 	arsta->rx_stats = NULL;
6768 dec_num_station:
6769 	ath12k_mac_dec_num_stations(arvif, arsta);
6770 exit:
6771 	return ret;
6772 }
6773 
6774 static int ath12k_mac_assign_link_sta(struct ath12k_hw *ah,
6775 				      struct ath12k_sta *ahsta,
6776 				      struct ath12k_link_sta *arsta,
6777 				      struct ath12k_vif *ahvif,
6778 				      u8 link_id)
6779 {
6780 	struct ieee80211_sta *sta = ath12k_ahsta_to_sta(ahsta);
6781 	struct ieee80211_link_sta *link_sta;
6782 	struct ath12k_link_vif *arvif;
6783 
6784 	lockdep_assert_wiphy(ah->hw->wiphy);
6785 
6786 	if (!arsta || link_id >= IEEE80211_MLD_MAX_NUM_LINKS)
6787 		return -EINVAL;
6788 
6789 	arvif = wiphy_dereference(ah->hw->wiphy, ahvif->link[link_id]);
6790 	if (!arvif)
6791 		return -EINVAL;
6792 
6793 	memset(arsta, 0, sizeof(*arsta));
6794 
6795 	link_sta = wiphy_dereference(ah->hw->wiphy, sta->link[link_id]);
6796 	if (!link_sta)
6797 		return -EINVAL;
6798 
6799 	ether_addr_copy(arsta->addr, link_sta->addr);
6800 
6801 	/* logical index of the link sta in order of creation */
6802 	arsta->link_idx = ahsta->num_peer++;
6803 
6804 	arsta->link_id = link_id;
6805 	ahsta->links_map |= BIT(arsta->link_id);
6806 	arsta->arvif = arvif;
6807 	arsta->ahsta = ahsta;
6808 	ahsta->ahvif = ahvif;
6809 
6810 	wiphy_work_init(&arsta->update_wk, ath12k_sta_rc_update_wk);
6811 
6812 	rcu_assign_pointer(ahsta->link[link_id], arsta);
6813 
6814 	return 0;
6815 }
6816 
6817 static void ath12k_mac_ml_station_remove(struct ath12k_vif *ahvif,
6818 					 struct ath12k_sta *ahsta)
6819 {
6820 	struct ieee80211_sta *sta = ath12k_ahsta_to_sta(ahsta);
6821 	struct ath12k_hw *ah = ahvif->ah;
6822 	struct ath12k_link_vif *arvif;
6823 	struct ath12k_link_sta *arsta;
6824 	unsigned long links;
6825 	struct ath12k *ar;
6826 	u8 link_id;
6827 
6828 	lockdep_assert_wiphy(ah->hw->wiphy);
6829 
6830 	ath12k_peer_mlo_link_peers_delete(ahvif, ahsta);
6831 
6832 	/* validate link station removal and clear arsta links */
6833 	links = ahsta->links_map;
6834 	for_each_set_bit(link_id, &links, IEEE80211_MLD_MAX_NUM_LINKS) {
6835 		arvif = wiphy_dereference(ah->hw->wiphy, ahvif->link[link_id]);
6836 		arsta = wiphy_dereference(ah->hw->wiphy, ahsta->link[link_id]);
6837 		if (!arvif || !arsta)
6838 			continue;
6839 
6840 		ar = arvif->ar;
6841 
6842 		ath12k_mac_station_post_remove(ar, arvif, arsta);
6843 
6844 		ath12k_mac_free_unassign_link_sta(ah, ahsta, link_id);
6845 	}
6846 
6847 	ath12k_peer_ml_delete(ah, sta);
6848 }
6849 
6850 static int ath12k_mac_handle_link_sta_state(struct ieee80211_hw *hw,
6851 					    struct ath12k_link_vif *arvif,
6852 					    struct ath12k_link_sta *arsta,
6853 					    enum ieee80211_sta_state old_state,
6854 					    enum ieee80211_sta_state new_state)
6855 {
6856 	struct ieee80211_vif *vif = ath12k_ahvif_to_vif(arvif->ahvif);
6857 	struct ieee80211_bss_conf *link_conf;
6858 	struct ath12k *ar = arvif->ar;
6859 	struct ath12k_reg_info *reg_info;
6860 	struct ath12k_base *ab = ar->ab;
6861 	int ret = 0;
6862 
6863 	lockdep_assert_wiphy(hw->wiphy);
6864 
6865 	ath12k_dbg(ab, ATH12K_DBG_MAC, "mac handle link %u sta %pM state %d -> %d\n",
6866 		   arsta->link_id, arsta->addr, old_state, new_state);
6867 
6868 	/* IEEE80211_STA_NONE -> IEEE80211_STA_NOTEXIST: Remove the station
6869 	 * from driver
6870 	 */
6871 	if ((old_state == IEEE80211_STA_NONE &&
6872 	     new_state == IEEE80211_STA_NOTEXIST)) {
6873 		ret = ath12k_mac_station_remove(ar, arvif, arsta);
6874 		if (ret) {
6875 			ath12k_warn(ab, "Failed to remove station: %pM for VDEV: %d\n",
6876 				    arsta->addr, arvif->vdev_id);
6877 			goto exit;
6878 		}
6879 	}
6880 
6881 	/* IEEE80211_STA_NOTEXIST -> IEEE80211_STA_NONE: Add new station to driver */
6882 	if (old_state == IEEE80211_STA_NOTEXIST &&
6883 	    new_state == IEEE80211_STA_NONE) {
6884 		ret = ath12k_mac_station_add(ar, arvif, arsta);
6885 		if (ret)
6886 			ath12k_warn(ab, "Failed to add station: %pM for VDEV: %d\n",
6887 				    arsta->addr, arvif->vdev_id);
6888 
6889 	/* IEEE80211_STA_AUTH -> IEEE80211_STA_ASSOC: Send station assoc command for
6890 	 * peer associated to AP/Mesh/ADHOC vif type.
6891 	 */
6892 	} else if (old_state == IEEE80211_STA_AUTH &&
6893 		   new_state == IEEE80211_STA_ASSOC &&
6894 		   (vif->type == NL80211_IFTYPE_AP ||
6895 		    vif->type == NL80211_IFTYPE_MESH_POINT ||
6896 		    vif->type == NL80211_IFTYPE_ADHOC)) {
6897 		ret = ath12k_mac_station_assoc(ar, arvif, arsta, false);
6898 		if (ret)
6899 			ath12k_warn(ab, "Failed to associate station: %pM\n",
6900 				    arsta->addr);
6901 
6902 	/* IEEE80211_STA_ASSOC -> IEEE80211_STA_AUTHORIZED: set peer status as
6903 	 * authorized
6904 	 */
6905 	} else if (old_state == IEEE80211_STA_ASSOC &&
6906 		   new_state == IEEE80211_STA_AUTHORIZED) {
6907 		ret = ath12k_mac_station_authorize(ar, arvif, arsta);
6908 		if (ret) {
6909 			ath12k_warn(ab, "Failed to authorize station: %pM\n",
6910 				    arsta->addr);
6911 			goto exit;
6912 		}
6913 
6914 		if (ath12k_wmi_supports_6ghz_cc_ext(ar) &&
6915 		    arvif->ahvif->vdev_type == WMI_VDEV_TYPE_STA) {
6916 			link_conf = ath12k_mac_get_link_bss_conf(arvif);
6917 			reg_info = ab->reg_info[ar->pdev_idx];
6918 			ath12k_dbg(ab, ATH12K_DBG_MAC, "connection done, update reg rules\n");
6919 			ath12k_hw_to_ah(hw)->regd_updated = false;
6920 			ath12k_reg_handle_chan_list(ab, reg_info, arvif->ahvif->vdev_type,
6921 						    link_conf->power_type);
6922 		}
6923 
6924 	/* IEEE80211_STA_AUTHORIZED -> IEEE80211_STA_ASSOC: station may be in removal,
6925 	 * deauthorize it.
6926 	 */
6927 	} else if (old_state == IEEE80211_STA_AUTHORIZED &&
6928 		   new_state == IEEE80211_STA_ASSOC) {
6929 		ath12k_mac_station_unauthorize(ar, arvif, arsta);
6930 
6931 	/* IEEE80211_STA_ASSOC -> IEEE80211_STA_AUTH: disassoc peer connected to
6932 	 * AP/mesh/ADHOC vif type.
6933 	 */
6934 	} else if (old_state == IEEE80211_STA_ASSOC &&
6935 		   new_state == IEEE80211_STA_AUTH &&
6936 		   (vif->type == NL80211_IFTYPE_AP ||
6937 		    vif->type == NL80211_IFTYPE_MESH_POINT ||
6938 		    vif->type == NL80211_IFTYPE_ADHOC)) {
6939 		ret = ath12k_mac_station_disassoc(ar, arvif, arsta);
6940 		if (ret)
6941 			ath12k_warn(ab, "Failed to disassociate station: %pM\n",
6942 				    arsta->addr);
6943 	}
6944 
6945 exit:
6946 	return ret;
6947 }
6948 
6949 static bool ath12k_mac_is_freq_on_mac(struct ath12k_hw_mode_freq_range_arg *freq_range,
6950 				      u32 freq, u8 mac_id)
6951 {
6952 	return (freq >= freq_range[mac_id].low_2ghz_freq &&
6953 		freq <= freq_range[mac_id].high_2ghz_freq) ||
6954 	       (freq >= freq_range[mac_id].low_5ghz_freq &&
6955 		freq <= freq_range[mac_id].high_5ghz_freq);
6956 }
6957 
6958 static bool
6959 ath12k_mac_2_freq_same_mac_in_freq_range(struct ath12k_base *ab,
6960 					 struct ath12k_hw_mode_freq_range_arg *freq_range,
6961 					 u32 freq_link1, u32 freq_link2)
6962 {
6963 	u8 i;
6964 
6965 	for (i = 0; i < MAX_RADIOS; i++) {
6966 		if (ath12k_mac_is_freq_on_mac(freq_range, freq_link1, i) &&
6967 		    ath12k_mac_is_freq_on_mac(freq_range, freq_link2, i))
6968 			return true;
6969 	}
6970 
6971 	return false;
6972 }
6973 
6974 static bool ath12k_mac_is_hw_dbs_capable(struct ath12k_base *ab)
6975 {
6976 	return test_bit(WMI_TLV_SERVICE_DUAL_BAND_SIMULTANEOUS_SUPPORT,
6977 			ab->wmi_ab.svc_map) &&
6978 	       ab->wmi_ab.hw_mode_info.support_dbs;
6979 }
6980 
6981 static bool ath12k_mac_2_freq_same_mac_in_dbs(struct ath12k_base *ab,
6982 					      u32 freq_link1, u32 freq_link2)
6983 {
6984 	struct ath12k_hw_mode_freq_range_arg *freq_range;
6985 
6986 	if (!ath12k_mac_is_hw_dbs_capable(ab))
6987 		return true;
6988 
6989 	freq_range = ab->wmi_ab.hw_mode_info.freq_range_caps[ATH12K_HW_MODE_DBS];
6990 	return ath12k_mac_2_freq_same_mac_in_freq_range(ab, freq_range,
6991 							freq_link1, freq_link2);
6992 }
6993 
6994 static bool ath12k_mac_is_hw_sbs_capable(struct ath12k_base *ab)
6995 {
6996 	return test_bit(WMI_TLV_SERVICE_DUAL_BAND_SIMULTANEOUS_SUPPORT,
6997 			ab->wmi_ab.svc_map) &&
6998 	       ab->wmi_ab.hw_mode_info.support_sbs;
6999 }
7000 
7001 static bool ath12k_mac_2_freq_same_mac_in_sbs(struct ath12k_base *ab,
7002 					      u32 freq_link1, u32 freq_link2)
7003 {
7004 	struct ath12k_hw_mode_info *info = &ab->wmi_ab.hw_mode_info;
7005 	struct ath12k_hw_mode_freq_range_arg *sbs_uppr_share;
7006 	struct ath12k_hw_mode_freq_range_arg *sbs_low_share;
7007 	struct ath12k_hw_mode_freq_range_arg *sbs_range;
7008 
7009 	if (!ath12k_mac_is_hw_sbs_capable(ab))
7010 		return true;
7011 
7012 	if (ab->wmi_ab.sbs_lower_band_end_freq) {
7013 		sbs_uppr_share = info->freq_range_caps[ATH12K_HW_MODE_SBS_UPPER_SHARE];
7014 		sbs_low_share = info->freq_range_caps[ATH12K_HW_MODE_SBS_LOWER_SHARE];
7015 
7016 		return ath12k_mac_2_freq_same_mac_in_freq_range(ab, sbs_low_share,
7017 								freq_link1, freq_link2) ||
7018 		       ath12k_mac_2_freq_same_mac_in_freq_range(ab, sbs_uppr_share,
7019 								freq_link1, freq_link2);
7020 	}
7021 
7022 	sbs_range = info->freq_range_caps[ATH12K_HW_MODE_SBS];
7023 	return ath12k_mac_2_freq_same_mac_in_freq_range(ab, sbs_range,
7024 							freq_link1, freq_link2);
7025 }
7026 
7027 static bool ath12k_mac_freqs_on_same_mac(struct ath12k_base *ab,
7028 					 u32 freq_link1, u32 freq_link2)
7029 {
7030 	return ath12k_mac_2_freq_same_mac_in_dbs(ab, freq_link1, freq_link2) &&
7031 	       ath12k_mac_2_freq_same_mac_in_sbs(ab, freq_link1, freq_link2);
7032 }
7033 
7034 static int ath12k_mac_mlo_sta_set_link_active(struct ath12k_base *ab,
7035 					      enum wmi_mlo_link_force_reason reason,
7036 					      enum wmi_mlo_link_force_mode mode,
7037 					      u8 *mlo_vdev_id_lst,
7038 					      u8 num_mlo_vdev,
7039 					      u8 *mlo_inactive_vdev_lst,
7040 					      u8 num_mlo_inactive_vdev)
7041 {
7042 	struct wmi_mlo_link_set_active_arg param = {};
7043 	u32 entry_idx, entry_offset, vdev_idx;
7044 	u8 vdev_id;
7045 
7046 	param.reason = reason;
7047 	param.force_mode = mode;
7048 
7049 	for (vdev_idx = 0; vdev_idx < num_mlo_vdev; vdev_idx++) {
7050 		vdev_id = mlo_vdev_id_lst[vdev_idx];
7051 		entry_idx = vdev_id / 32;
7052 		entry_offset = vdev_id % 32;
7053 		if (entry_idx >= WMI_MLO_LINK_NUM_SZ) {
7054 			ath12k_warn(ab, "Invalid entry_idx %d num_mlo_vdev %d vdev %d",
7055 				    entry_idx, num_mlo_vdev, vdev_id);
7056 			return -EINVAL;
7057 		}
7058 		param.vdev_bitmap[entry_idx] |= 1 << entry_offset;
7059 		/* update entry number if entry index changed */
7060 		if (param.num_vdev_bitmap < entry_idx + 1)
7061 			param.num_vdev_bitmap = entry_idx + 1;
7062 	}
7063 
7064 	ath12k_dbg(ab, ATH12K_DBG_MAC,
7065 		   "num_vdev_bitmap %d vdev_bitmap[0] = 0x%x, vdev_bitmap[1] = 0x%x",
7066 		   param.num_vdev_bitmap, param.vdev_bitmap[0], param.vdev_bitmap[1]);
7067 
7068 	if (mode == WMI_MLO_LINK_FORCE_MODE_ACTIVE_INACTIVE) {
7069 		for (vdev_idx = 0; vdev_idx < num_mlo_inactive_vdev; vdev_idx++) {
7070 			vdev_id = mlo_inactive_vdev_lst[vdev_idx];
7071 			entry_idx = vdev_id / 32;
7072 			entry_offset = vdev_id % 32;
7073 			if (entry_idx >= WMI_MLO_LINK_NUM_SZ) {
7074 				ath12k_warn(ab, "Invalid entry_idx %d num_mlo_vdev %d vdev %d",
7075 					    entry_idx, num_mlo_inactive_vdev, vdev_id);
7076 				return -EINVAL;
7077 			}
7078 			param.inactive_vdev_bitmap[entry_idx] |= 1 << entry_offset;
7079 			/* update entry number if entry index changed */
7080 			if (param.num_inactive_vdev_bitmap < entry_idx + 1)
7081 				param.num_inactive_vdev_bitmap = entry_idx + 1;
7082 		}
7083 
7084 		ath12k_dbg(ab, ATH12K_DBG_MAC,
7085 			   "num_vdev_bitmap %d inactive_vdev_bitmap[0] = 0x%x, inactive_vdev_bitmap[1] = 0x%x",
7086 			   param.num_inactive_vdev_bitmap,
7087 			   param.inactive_vdev_bitmap[0],
7088 			   param.inactive_vdev_bitmap[1]);
7089 	}
7090 
7091 	if (mode == WMI_MLO_LINK_FORCE_MODE_ACTIVE_LINK_NUM ||
7092 	    mode == WMI_MLO_LINK_FORCE_MODE_INACTIVE_LINK_NUM) {
7093 		param.num_link_entry = 1;
7094 		param.link_num[0].num_of_link = num_mlo_vdev - 1;
7095 	}
7096 
7097 	return ath12k_wmi_send_mlo_link_set_active_cmd(ab, &param);
7098 }
7099 
7100 static int ath12k_mac_mlo_sta_update_link_active(struct ath12k_base *ab,
7101 						 struct ieee80211_hw *hw,
7102 						 struct ath12k_vif *ahvif)
7103 {
7104 	u8 mlo_vdev_id_lst[IEEE80211_MLD_MAX_NUM_LINKS] = {};
7105 	u32 mlo_freq_list[IEEE80211_MLD_MAX_NUM_LINKS] = {};
7106 	unsigned long links = ahvif->links_map;
7107 	enum wmi_mlo_link_force_reason reason;
7108 	struct ieee80211_chanctx_conf *conf;
7109 	enum wmi_mlo_link_force_mode mode;
7110 	struct ieee80211_bss_conf *info;
7111 	struct ath12k_link_vif *arvif;
7112 	u8 num_mlo_vdev = 0;
7113 	u8 link_id;
7114 
7115 	for_each_set_bit(link_id, &links, IEEE80211_MLD_MAX_NUM_LINKS) {
7116 		arvif = wiphy_dereference(hw->wiphy, ahvif->link[link_id]);
7117 		/* make sure vdev is created on this device */
7118 		if (!arvif || !arvif->is_created || arvif->ar->ab != ab)
7119 			continue;
7120 
7121 		info = ath12k_mac_get_link_bss_conf(arvif);
7122 		conf = wiphy_dereference(hw->wiphy, info->chanctx_conf);
7123 		mlo_freq_list[num_mlo_vdev] = conf->def.chan->center_freq;
7124 
7125 		mlo_vdev_id_lst[num_mlo_vdev] = arvif->vdev_id;
7126 		num_mlo_vdev++;
7127 	}
7128 
7129 	/* It is not allowed to activate more links than a single device
7130 	 * supported. Something goes wrong if we reach here.
7131 	 */
7132 	if (num_mlo_vdev > ATH12K_NUM_MAX_ACTIVE_LINKS_PER_DEVICE) {
7133 		WARN_ON_ONCE(1);
7134 		return -EINVAL;
7135 	}
7136 
7137 	/* if 2 links are established and both link channels fall on the
7138 	 * same hardware MAC, send command to firmware to deactivate one
7139 	 * of them.
7140 	 */
7141 	if (num_mlo_vdev == 2 &&
7142 	    ath12k_mac_freqs_on_same_mac(ab, mlo_freq_list[0],
7143 					 mlo_freq_list[1])) {
7144 		mode = WMI_MLO_LINK_FORCE_MODE_INACTIVE_LINK_NUM;
7145 		reason = WMI_MLO_LINK_FORCE_REASON_NEW_CONNECT;
7146 		return ath12k_mac_mlo_sta_set_link_active(ab, reason, mode,
7147 							  mlo_vdev_id_lst, num_mlo_vdev,
7148 							  NULL, 0);
7149 	}
7150 
7151 	return 0;
7152 }
7153 
7154 static bool ath12k_mac_are_sbs_chan(struct ath12k_base *ab, u32 freq_1, u32 freq_2)
7155 {
7156 	if (!ath12k_mac_is_hw_sbs_capable(ab))
7157 		return false;
7158 
7159 	if (ath12k_is_2ghz_channel_freq(freq_1) ||
7160 	    ath12k_is_2ghz_channel_freq(freq_2))
7161 		return false;
7162 
7163 	return !ath12k_mac_2_freq_same_mac_in_sbs(ab, freq_1, freq_2);
7164 }
7165 
7166 static bool ath12k_mac_are_dbs_chan(struct ath12k_base *ab, u32 freq_1, u32 freq_2)
7167 {
7168 	if (!ath12k_mac_is_hw_dbs_capable(ab))
7169 		return false;
7170 
7171 	return !ath12k_mac_2_freq_same_mac_in_dbs(ab, freq_1, freq_2);
7172 }
7173 
7174 static int ath12k_mac_select_links(struct ath12k_base *ab,
7175 				   struct ieee80211_vif *vif,
7176 				   struct ieee80211_hw *hw,
7177 				   u16 *selected_links)
7178 {
7179 	unsigned long useful_links = ieee80211_vif_usable_links(vif);
7180 	struct ath12k_vif *ahvif = ath12k_vif_to_ahvif(vif);
7181 	u8 num_useful_links = hweight_long(useful_links);
7182 	struct ieee80211_chanctx_conf *chanctx;
7183 	struct ath12k_link_vif *assoc_arvif;
7184 	u32 assoc_link_freq, partner_freq;
7185 	u16 sbs_links = 0, dbs_links = 0;
7186 	struct ieee80211_bss_conf *info;
7187 	struct ieee80211_channel *chan;
7188 	struct ieee80211_sta *sta;
7189 	struct ath12k_sta *ahsta;
7190 	u8 link_id;
7191 
7192 	/* activate all useful links if less than max supported */
7193 	if (num_useful_links <= ATH12K_NUM_MAX_ACTIVE_LINKS_PER_DEVICE) {
7194 		*selected_links = useful_links;
7195 		return 0;
7196 	}
7197 
7198 	/* only in station mode we can get here, so it's safe
7199 	 * to use ap_addr
7200 	 */
7201 	rcu_read_lock();
7202 	sta = ieee80211_find_sta(vif, vif->cfg.ap_addr);
7203 	if (!sta) {
7204 		rcu_read_unlock();
7205 		ath12k_warn(ab, "failed to find sta with addr %pM\n", vif->cfg.ap_addr);
7206 		return -EINVAL;
7207 	}
7208 
7209 	ahsta = ath12k_sta_to_ahsta(sta);
7210 	assoc_arvif = wiphy_dereference(hw->wiphy, ahvif->link[ahsta->assoc_link_id]);
7211 	info = ath12k_mac_get_link_bss_conf(assoc_arvif);
7212 	chanctx = rcu_dereference(info->chanctx_conf);
7213 	assoc_link_freq = chanctx->def.chan->center_freq;
7214 	rcu_read_unlock();
7215 	ath12k_dbg(ab, ATH12K_DBG_MAC, "assoc link %u freq %u\n",
7216 		   assoc_arvif->link_id, assoc_link_freq);
7217 
7218 	/* assoc link is already activated and has to be kept active,
7219 	 * only need to select a partner link from others.
7220 	 */
7221 	useful_links &= ~BIT(assoc_arvif->link_id);
7222 	for_each_set_bit(link_id, &useful_links, IEEE80211_MLD_MAX_NUM_LINKS) {
7223 		info = wiphy_dereference(hw->wiphy, vif->link_conf[link_id]);
7224 		if (!info) {
7225 			ath12k_warn(ab, "failed to get link info for link: %u\n",
7226 				    link_id);
7227 			return -ENOLINK;
7228 		}
7229 
7230 		chan = info->chanreq.oper.chan;
7231 		if (!chan) {
7232 			ath12k_warn(ab, "failed to get chan for link: %u\n", link_id);
7233 			return -EINVAL;
7234 		}
7235 
7236 		partner_freq = chan->center_freq;
7237 		if (ath12k_mac_are_sbs_chan(ab, assoc_link_freq, partner_freq)) {
7238 			sbs_links |= BIT(link_id);
7239 			ath12k_dbg(ab, ATH12K_DBG_MAC, "new SBS link %u freq %u\n",
7240 				   link_id, partner_freq);
7241 			continue;
7242 		}
7243 
7244 		if (ath12k_mac_are_dbs_chan(ab, assoc_link_freq, partner_freq)) {
7245 			dbs_links |= BIT(link_id);
7246 			ath12k_dbg(ab, ATH12K_DBG_MAC, "new DBS link %u freq %u\n",
7247 				   link_id, partner_freq);
7248 			continue;
7249 		}
7250 
7251 		ath12k_dbg(ab, ATH12K_DBG_MAC, "non DBS/SBS link %u freq %u\n",
7252 			   link_id, partner_freq);
7253 	}
7254 
7255 	/* choose the first candidate no matter how many is in the list */
7256 	if (sbs_links)
7257 		link_id = __ffs(sbs_links);
7258 	else if (dbs_links)
7259 		link_id = __ffs(dbs_links);
7260 	else
7261 		link_id = ffs(useful_links) - 1;
7262 
7263 	ath12k_dbg(ab, ATH12K_DBG_MAC, "select partner link %u\n", link_id);
7264 
7265 	*selected_links = BIT(assoc_arvif->link_id) | BIT(link_id);
7266 
7267 	return 0;
7268 }
7269 
7270 static int ath12k_mac_op_sta_state(struct ieee80211_hw *hw,
7271 				   struct ieee80211_vif *vif,
7272 				   struct ieee80211_sta *sta,
7273 				   enum ieee80211_sta_state old_state,
7274 				   enum ieee80211_sta_state new_state)
7275 {
7276 	struct ath12k_vif *ahvif = ath12k_vif_to_ahvif(vif);
7277 	struct ath12k_sta *ahsta = ath12k_sta_to_ahsta(sta);
7278 	struct ath12k_hw *ah = ath12k_hw_to_ah(hw);
7279 	struct ath12k_base *prev_ab = NULL, *ab;
7280 	struct ath12k_link_vif *arvif;
7281 	struct ath12k_link_sta *arsta;
7282 	unsigned long valid_links;
7283 	u16 selected_links = 0;
7284 	u8 link_id = 0, i;
7285 	struct ath12k *ar;
7286 	int ret;
7287 
7288 	lockdep_assert_wiphy(hw->wiphy);
7289 
7290 	if (ieee80211_vif_is_mld(vif) && sta->valid_links) {
7291 		WARN_ON(!sta->mlo && hweight16(sta->valid_links) != 1);
7292 		link_id = ffs(sta->valid_links) - 1;
7293 	}
7294 
7295 	/* IEEE80211_STA_NOTEXIST -> IEEE80211_STA_NONE:
7296 	 * New station add received. If this is a ML station then
7297 	 * ahsta->links_map will be zero and sta->valid_links will be 1.
7298 	 * Assign default link to the first link sta.
7299 	 */
7300 	if (old_state == IEEE80211_STA_NOTEXIST &&
7301 	    new_state == IEEE80211_STA_NONE) {
7302 		memset(ahsta, 0, sizeof(*ahsta));
7303 
7304 		arsta = &ahsta->deflink;
7305 
7306 		/* ML sta */
7307 		if (sta->mlo && !ahsta->links_map &&
7308 		    (hweight16(sta->valid_links) == 1)) {
7309 			ret = ath12k_peer_ml_create(ah, sta);
7310 			if (ret) {
7311 				ath12k_hw_warn(ah, "unable to create ML peer for sta %pM",
7312 					       sta->addr);
7313 				goto exit;
7314 			}
7315 		}
7316 
7317 		ret = ath12k_mac_assign_link_sta(ah, ahsta, arsta, ahvif,
7318 						 link_id);
7319 		if (ret) {
7320 			ath12k_hw_warn(ah, "unable assign link %d for sta %pM",
7321 				       link_id, sta->addr);
7322 			goto exit;
7323 		}
7324 
7325 		/* above arsta will get memset, hence do this after assign
7326 		 * link sta
7327 		 */
7328 		if (sta->mlo) {
7329 			/* For station mode, arvif->is_sta_assoc_link has been set when
7330 			 * vdev starts. Make sure the arvif/arsta pair have same setting
7331 			 */
7332 			if (vif->type == NL80211_IFTYPE_STATION &&
7333 			    !arsta->arvif->is_sta_assoc_link) {
7334 				ath12k_hw_warn(ah, "failed to verify assoc link setting with link id %u\n",
7335 					       link_id);
7336 				ret = -EINVAL;
7337 				goto exit;
7338 			}
7339 
7340 			arsta->is_assoc_link = true;
7341 			ahsta->assoc_link_id = link_id;
7342 		}
7343 	}
7344 
7345 	/* In the ML station scenario, activate all partner links once the
7346 	 * client is transitioning to the associated state.
7347 	 *
7348 	 * FIXME: Ideally, this activation should occur when the client
7349 	 * transitions to the authorized state. However, there are some
7350 	 * issues with handling this in the firmware. Until the firmware
7351 	 * can manage it properly, activate the links when the client is
7352 	 * about to move to the associated state.
7353 	 */
7354 	if (ieee80211_vif_is_mld(vif) && vif->type == NL80211_IFTYPE_STATION &&
7355 	    old_state == IEEE80211_STA_AUTH && new_state == IEEE80211_STA_ASSOC) {
7356 		/* TODO: for now only do link selection for single device
7357 		 * MLO case. Other cases would be handled in the future.
7358 		 */
7359 		ab = ah->radio[0].ab;
7360 		if (ab->ag->num_devices == 1) {
7361 			ret = ath12k_mac_select_links(ab, vif, hw, &selected_links);
7362 			if (ret) {
7363 				ath12k_warn(ab,
7364 					    "failed to get selected links: %d\n", ret);
7365 				goto exit;
7366 			}
7367 		} else {
7368 			selected_links = ieee80211_vif_usable_links(vif);
7369 		}
7370 
7371 		ieee80211_set_active_links(vif, selected_links);
7372 	}
7373 
7374 	/* Handle all the other state transitions in generic way */
7375 	valid_links = ahsta->links_map;
7376 	for_each_set_bit(link_id, &valid_links, IEEE80211_MLD_MAX_NUM_LINKS) {
7377 		arvif = wiphy_dereference(hw->wiphy, ahvif->link[link_id]);
7378 		arsta = wiphy_dereference(hw->wiphy, ahsta->link[link_id]);
7379 		/* some assumptions went wrong! */
7380 		if (WARN_ON(!arvif || !arsta))
7381 			continue;
7382 
7383 		/* vdev might be in deleted */
7384 		if (WARN_ON(!arvif->ar))
7385 			continue;
7386 
7387 		ret = ath12k_mac_handle_link_sta_state(hw, arvif, arsta,
7388 						       old_state, new_state);
7389 		if (ret) {
7390 			ath12k_hw_warn(ah, "unable to move link sta %d of sta %pM from state %d to %d",
7391 				       link_id, arsta->addr, old_state, new_state);
7392 			goto exit;
7393 		}
7394 	}
7395 
7396 	if (ieee80211_vif_is_mld(vif) && vif->type == NL80211_IFTYPE_STATION &&
7397 	    old_state == IEEE80211_STA_ASSOC && new_state == IEEE80211_STA_AUTHORIZED) {
7398 		for_each_ar(ah, ar, i) {
7399 			ab = ar->ab;
7400 			if (prev_ab == ab)
7401 				continue;
7402 
7403 			ret = ath12k_mac_mlo_sta_update_link_active(ab, hw, ahvif);
7404 			if (ret) {
7405 				ath12k_warn(ab,
7406 					    "failed to update link active state on connect %d\n",
7407 					    ret);
7408 				goto exit;
7409 			}
7410 
7411 			prev_ab = ab;
7412 		}
7413 	}
7414 	/* IEEE80211_STA_NONE -> IEEE80211_STA_NOTEXIST:
7415 	 * Remove the station from driver (handle ML sta here since that
7416 	 * needs special handling. Normal sta will be handled in generic
7417 	 * handler below
7418 	 */
7419 	if (old_state == IEEE80211_STA_NONE &&
7420 	    new_state == IEEE80211_STA_NOTEXIST && sta->mlo)
7421 		ath12k_mac_ml_station_remove(ahvif, ahsta);
7422 
7423 	ret = 0;
7424 
7425 exit:
7426 	/* update the state if everything went well */
7427 	if (!ret)
7428 		ahsta->state = new_state;
7429 
7430 	return ret;
7431 }
7432 
7433 static int ath12k_mac_op_sta_set_txpwr(struct ieee80211_hw *hw,
7434 				       struct ieee80211_vif *vif,
7435 				       struct ieee80211_sta *sta)
7436 {
7437 	struct ath12k_sta *ahsta = ath12k_sta_to_ahsta(sta);
7438 	struct ath12k *ar;
7439 	struct ath12k_vif *ahvif = ath12k_vif_to_ahvif(vif);
7440 	struct ath12k_link_vif *arvif;
7441 	struct ath12k_link_sta *arsta;
7442 	u8 link_id;
7443 	int ret;
7444 	s16 txpwr;
7445 
7446 	lockdep_assert_wiphy(hw->wiphy);
7447 
7448 	/* TODO: use link id from mac80211 once that's implemented */
7449 	link_id = 0;
7450 
7451 	arvif = wiphy_dereference(hw->wiphy, ahvif->link[link_id]);
7452 	arsta = wiphy_dereference(hw->wiphy, ahsta->link[link_id]);
7453 
7454 	if (sta->deflink.txpwr.type == NL80211_TX_POWER_AUTOMATIC) {
7455 		txpwr = 0;
7456 	} else {
7457 		txpwr = sta->deflink.txpwr.power;
7458 		if (!txpwr) {
7459 			ret = -EINVAL;
7460 			goto out;
7461 		}
7462 	}
7463 
7464 	if (txpwr > ATH12K_TX_POWER_MAX_VAL || txpwr < ATH12K_TX_POWER_MIN_VAL) {
7465 		ret = -EINVAL;
7466 		goto out;
7467 	}
7468 
7469 	ar = arvif->ar;
7470 
7471 	ret = ath12k_wmi_set_peer_param(ar, arsta->addr, arvif->vdev_id,
7472 					WMI_PEER_USE_FIXED_PWR, txpwr);
7473 	if (ret) {
7474 		ath12k_warn(ar->ab, "failed to set tx power for station ret: %d\n",
7475 			    ret);
7476 		goto out;
7477 	}
7478 
7479 out:
7480 	return ret;
7481 }
7482 
7483 static void ath12k_mac_op_link_sta_rc_update(struct ieee80211_hw *hw,
7484 					     struct ieee80211_vif *vif,
7485 					     struct ieee80211_link_sta *link_sta,
7486 					     u32 changed)
7487 {
7488 	struct ieee80211_sta *sta = link_sta->sta;
7489 	struct ath12k *ar;
7490 	struct ath12k_sta *ahsta = ath12k_sta_to_ahsta(sta);
7491 	struct ath12k_vif *ahvif = ath12k_vif_to_ahvif(vif);
7492 	struct ath12k_hw *ah = ath12k_hw_to_ah(hw);
7493 	struct ath12k_link_sta *arsta;
7494 	struct ath12k_link_vif *arvif;
7495 	struct ath12k_peer *peer;
7496 	u32 bw, smps;
7497 
7498 	rcu_read_lock();
7499 	arvif = rcu_dereference(ahvif->link[link_sta->link_id]);
7500 	if (!arvif) {
7501 		ath12k_hw_warn(ah, "mac sta rc update failed to fetch link vif on link id %u for peer %pM\n",
7502 			       link_sta->link_id, sta->addr);
7503 		rcu_read_unlock();
7504 		return;
7505 	}
7506 
7507 	ar = arvif->ar;
7508 
7509 	arsta = rcu_dereference(ahsta->link[link_sta->link_id]);
7510 	if (!arsta) {
7511 		rcu_read_unlock();
7512 		ath12k_warn(ar->ab, "mac sta rc update failed to fetch link sta on link id %u for peer %pM\n",
7513 			    link_sta->link_id, sta->addr);
7514 		return;
7515 	}
7516 	spin_lock_bh(&ar->ab->base_lock);
7517 
7518 	peer = ath12k_peer_find(ar->ab, arvif->vdev_id, arsta->addr);
7519 	if (!peer) {
7520 		spin_unlock_bh(&ar->ab->base_lock);
7521 		rcu_read_unlock();
7522 		ath12k_warn(ar->ab, "mac sta rc update failed to find peer %pM on vdev %i\n",
7523 			    arsta->addr, arvif->vdev_id);
7524 		return;
7525 	}
7526 
7527 	spin_unlock_bh(&ar->ab->base_lock);
7528 
7529 	if (arsta->link_id >= IEEE80211_MLD_MAX_NUM_LINKS) {
7530 		rcu_read_unlock();
7531 		return;
7532 	}
7533 
7534 	link_sta = rcu_dereference(sta->link[arsta->link_id]);
7535 	if (!link_sta) {
7536 		rcu_read_unlock();
7537 		ath12k_warn(ar->ab, "unable to access link sta in rc update for sta %pM link %u\n",
7538 			    sta->addr, arsta->link_id);
7539 		return;
7540 	}
7541 
7542 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
7543 		   "mac sta rc update for %pM changed %08x bw %d nss %d smps %d\n",
7544 		   arsta->addr, changed, link_sta->bandwidth, link_sta->rx_nss,
7545 		   link_sta->smps_mode);
7546 
7547 	spin_lock_bh(&ar->data_lock);
7548 
7549 	if (changed & IEEE80211_RC_BW_CHANGED) {
7550 		bw = ath12k_mac_ieee80211_sta_bw_to_wmi(ar, link_sta);
7551 		arsta->bw_prev = arsta->bw;
7552 		arsta->bw = bw;
7553 	}
7554 
7555 	if (changed & IEEE80211_RC_NSS_CHANGED)
7556 		arsta->nss = link_sta->rx_nss;
7557 
7558 	if (changed & IEEE80211_RC_SMPS_CHANGED) {
7559 		smps = WMI_PEER_SMPS_PS_NONE;
7560 
7561 		switch (link_sta->smps_mode) {
7562 		case IEEE80211_SMPS_AUTOMATIC:
7563 		case IEEE80211_SMPS_OFF:
7564 			smps = WMI_PEER_SMPS_PS_NONE;
7565 			break;
7566 		case IEEE80211_SMPS_STATIC:
7567 			smps = WMI_PEER_SMPS_STATIC;
7568 			break;
7569 		case IEEE80211_SMPS_DYNAMIC:
7570 			smps = WMI_PEER_SMPS_DYNAMIC;
7571 			break;
7572 		default:
7573 			ath12k_warn(ar->ab, "Invalid smps %d in sta rc update for %pM link %u\n",
7574 				    link_sta->smps_mode, arsta->addr, link_sta->link_id);
7575 			smps = WMI_PEER_SMPS_PS_NONE;
7576 			break;
7577 		}
7578 
7579 		arsta->smps = smps;
7580 	}
7581 
7582 	arsta->changed |= changed;
7583 
7584 	spin_unlock_bh(&ar->data_lock);
7585 
7586 	wiphy_work_queue(hw->wiphy, &arsta->update_wk);
7587 
7588 	rcu_read_unlock();
7589 }
7590 
7591 static struct ath12k_link_sta *ath12k_mac_alloc_assign_link_sta(struct ath12k_hw *ah,
7592 								struct ath12k_sta *ahsta,
7593 								struct ath12k_vif *ahvif,
7594 								u8 link_id)
7595 {
7596 	struct ath12k_link_sta *arsta;
7597 	int ret;
7598 
7599 	lockdep_assert_wiphy(ah->hw->wiphy);
7600 
7601 	if (link_id >= IEEE80211_MLD_MAX_NUM_LINKS)
7602 		return NULL;
7603 
7604 	arsta = wiphy_dereference(ah->hw->wiphy, ahsta->link[link_id]);
7605 	if (arsta)
7606 		return NULL;
7607 
7608 	arsta = kmalloc(sizeof(*arsta), GFP_KERNEL);
7609 	if (!arsta)
7610 		return NULL;
7611 
7612 	ret = ath12k_mac_assign_link_sta(ah, ahsta, arsta, ahvif, link_id);
7613 	if (ret) {
7614 		kfree(arsta);
7615 		return NULL;
7616 	}
7617 
7618 	return arsta;
7619 }
7620 
7621 static int ath12k_mac_op_change_sta_links(struct ieee80211_hw *hw,
7622 					  struct ieee80211_vif *vif,
7623 					  struct ieee80211_sta *sta,
7624 					  u16 old_links, u16 new_links)
7625 {
7626 	struct ath12k_vif *ahvif = ath12k_vif_to_ahvif(vif);
7627 	struct ath12k_sta *ahsta = ath12k_sta_to_ahsta(sta);
7628 	struct ath12k_hw *ah = hw->priv;
7629 	struct ath12k_link_vif *arvif;
7630 	struct ath12k_link_sta *arsta;
7631 	unsigned long valid_links;
7632 	struct ath12k *ar;
7633 	u8 link_id;
7634 	int ret;
7635 
7636 	lockdep_assert_wiphy(hw->wiphy);
7637 
7638 	if (!sta->valid_links)
7639 		return -EINVAL;
7640 
7641 	/* Firmware does not support removal of one of link stas. All sta
7642 	 * would be removed during ML STA delete in sta_state(), hence link
7643 	 * sta removal is not handled here.
7644 	 */
7645 	if (new_links < old_links)
7646 		return 0;
7647 
7648 	if (ahsta->ml_peer_id == ATH12K_MLO_PEER_ID_INVALID) {
7649 		ath12k_hw_warn(ah, "unable to add link for ml sta %pM", sta->addr);
7650 		return -EINVAL;
7651 	}
7652 
7653 	/* this op is expected only after initial sta insertion with default link */
7654 	if (WARN_ON(ahsta->links_map == 0))
7655 		return -EINVAL;
7656 
7657 	valid_links = new_links;
7658 	for_each_set_bit(link_id, &valid_links, IEEE80211_MLD_MAX_NUM_LINKS) {
7659 		if (ahsta->links_map & BIT(link_id))
7660 			continue;
7661 
7662 		arvif = wiphy_dereference(hw->wiphy, ahvif->link[link_id]);
7663 		arsta = ath12k_mac_alloc_assign_link_sta(ah, ahsta, ahvif, link_id);
7664 
7665 		if (!arvif || !arsta) {
7666 			ath12k_hw_warn(ah, "Failed to alloc/assign link sta");
7667 			continue;
7668 		}
7669 
7670 		ar = arvif->ar;
7671 		if (!ar)
7672 			continue;
7673 
7674 		ret = ath12k_mac_station_add(ar, arvif, arsta);
7675 		if (ret) {
7676 			ath12k_warn(ar->ab, "Failed to add station: %pM for VDEV: %d\n",
7677 				    arsta->addr, arvif->vdev_id);
7678 			ath12k_mac_free_unassign_link_sta(ah, ahsta, link_id);
7679 			return ret;
7680 		}
7681 	}
7682 
7683 	return 0;
7684 }
7685 
7686 static bool ath12k_mac_op_can_activate_links(struct ieee80211_hw *hw,
7687 					     struct ieee80211_vif *vif,
7688 					     u16 active_links)
7689 {
7690 	/* TODO: Handle recovery case */
7691 
7692 	return true;
7693 }
7694 
7695 static int ath12k_conf_tx_uapsd(struct ath12k_link_vif *arvif,
7696 				u16 ac, bool enable)
7697 {
7698 	struct ath12k *ar = arvif->ar;
7699 	struct ath12k_vif *ahvif = arvif->ahvif;
7700 	u32 value;
7701 	int ret;
7702 
7703 	if (ahvif->vdev_type != WMI_VDEV_TYPE_STA)
7704 		return 0;
7705 
7706 	switch (ac) {
7707 	case IEEE80211_AC_VO:
7708 		value = WMI_STA_PS_UAPSD_AC3_DELIVERY_EN |
7709 			WMI_STA_PS_UAPSD_AC3_TRIGGER_EN;
7710 		break;
7711 	case IEEE80211_AC_VI:
7712 		value = WMI_STA_PS_UAPSD_AC2_DELIVERY_EN |
7713 			WMI_STA_PS_UAPSD_AC2_TRIGGER_EN;
7714 		break;
7715 	case IEEE80211_AC_BE:
7716 		value = WMI_STA_PS_UAPSD_AC1_DELIVERY_EN |
7717 			WMI_STA_PS_UAPSD_AC1_TRIGGER_EN;
7718 		break;
7719 	case IEEE80211_AC_BK:
7720 		value = WMI_STA_PS_UAPSD_AC0_DELIVERY_EN |
7721 			WMI_STA_PS_UAPSD_AC0_TRIGGER_EN;
7722 		break;
7723 	}
7724 
7725 	if (enable)
7726 		ahvif->u.sta.uapsd |= value;
7727 	else
7728 		ahvif->u.sta.uapsd &= ~value;
7729 
7730 	ret = ath12k_wmi_set_sta_ps_param(ar, arvif->vdev_id,
7731 					  WMI_STA_PS_PARAM_UAPSD,
7732 					  ahvif->u.sta.uapsd);
7733 	if (ret) {
7734 		ath12k_warn(ar->ab, "could not set uapsd params %d\n", ret);
7735 		goto exit;
7736 	}
7737 
7738 	if (ahvif->u.sta.uapsd)
7739 		value = WMI_STA_PS_RX_WAKE_POLICY_POLL_UAPSD;
7740 	else
7741 		value = WMI_STA_PS_RX_WAKE_POLICY_WAKE;
7742 
7743 	ret = ath12k_wmi_set_sta_ps_param(ar, arvif->vdev_id,
7744 					  WMI_STA_PS_PARAM_RX_WAKE_POLICY,
7745 					  value);
7746 	if (ret)
7747 		ath12k_warn(ar->ab, "could not set rx wake param %d\n", ret);
7748 
7749 exit:
7750 	return ret;
7751 }
7752 
7753 static int ath12k_mac_conf_tx(struct ath12k_link_vif *arvif, u16 ac,
7754 			      const struct ieee80211_tx_queue_params *params)
7755 {
7756 	struct wmi_wmm_params_arg *p = NULL;
7757 	struct ath12k *ar = arvif->ar;
7758 	struct ath12k_base *ab = ar->ab;
7759 	int ret;
7760 
7761 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
7762 
7763 	switch (ac) {
7764 	case IEEE80211_AC_VO:
7765 		p = &arvif->wmm_params.ac_vo;
7766 		break;
7767 	case IEEE80211_AC_VI:
7768 		p = &arvif->wmm_params.ac_vi;
7769 		break;
7770 	case IEEE80211_AC_BE:
7771 		p = &arvif->wmm_params.ac_be;
7772 		break;
7773 	case IEEE80211_AC_BK:
7774 		p = &arvif->wmm_params.ac_bk;
7775 		break;
7776 	}
7777 
7778 	if (WARN_ON(!p)) {
7779 		ret = -EINVAL;
7780 		goto exit;
7781 	}
7782 
7783 	p->cwmin = params->cw_min;
7784 	p->cwmax = params->cw_max;
7785 	p->aifs = params->aifs;
7786 	p->txop = params->txop;
7787 
7788 	ret = ath12k_wmi_send_wmm_update_cmd(ar, arvif->vdev_id,
7789 					     &arvif->wmm_params);
7790 	if (ret) {
7791 		ath12k_warn(ab, "pdev idx %d failed to set wmm params: %d\n",
7792 			    ar->pdev_idx, ret);
7793 		goto exit;
7794 	}
7795 
7796 	ret = ath12k_conf_tx_uapsd(arvif, ac, params->uapsd);
7797 	if (ret)
7798 		ath12k_warn(ab, "pdev idx %d failed to set sta uapsd: %d\n",
7799 			    ar->pdev_idx, ret);
7800 
7801 exit:
7802 	return ret;
7803 }
7804 
7805 static int ath12k_mac_op_conf_tx(struct ieee80211_hw *hw,
7806 				 struct ieee80211_vif *vif,
7807 				 unsigned int link_id, u16 ac,
7808 				 const struct ieee80211_tx_queue_params *params)
7809 {
7810 	struct ath12k_vif *ahvif = ath12k_vif_to_ahvif(vif);
7811 	struct ath12k_link_vif *arvif;
7812 	struct ath12k_vif_cache *cache;
7813 	int ret;
7814 
7815 	lockdep_assert_wiphy(hw->wiphy);
7816 
7817 	if (link_id >= IEEE80211_MLD_MAX_NUM_LINKS)
7818 		return -EINVAL;
7819 
7820 	arvif = wiphy_dereference(hw->wiphy, ahvif->link[link_id]);
7821 	if (!arvif || !arvif->is_created) {
7822 		cache = ath12k_ahvif_get_link_cache(ahvif, link_id);
7823 		if (!cache)
7824 			return -ENOSPC;
7825 
7826 		cache->tx_conf.changed = true;
7827 		cache->tx_conf.ac = ac;
7828 		cache->tx_conf.tx_queue_params = *params;
7829 
7830 		return 0;
7831 	}
7832 
7833 	ret = ath12k_mac_conf_tx(arvif, ac, params);
7834 
7835 	return ret;
7836 }
7837 
7838 static struct ieee80211_sta_ht_cap
7839 ath12k_create_ht_cap(struct ath12k *ar, u32 ar_ht_cap, u32 rate_cap_rx_chainmask)
7840 {
7841 	int i;
7842 	struct ieee80211_sta_ht_cap ht_cap = {};
7843 	u32 ar_vht_cap = ar->pdev->cap.vht_cap;
7844 
7845 	if (!(ar_ht_cap & WMI_HT_CAP_ENABLED))
7846 		return ht_cap;
7847 
7848 	ht_cap.ht_supported = 1;
7849 	ht_cap.ampdu_factor = IEEE80211_HT_MAX_AMPDU_64K;
7850 	ht_cap.ampdu_density = IEEE80211_HT_MPDU_DENSITY_NONE;
7851 	ht_cap.cap |= IEEE80211_HT_CAP_SUP_WIDTH_20_40;
7852 	ht_cap.cap |= IEEE80211_HT_CAP_DSSSCCK40;
7853 	ht_cap.cap |= WLAN_HT_CAP_SM_PS_STATIC << IEEE80211_HT_CAP_SM_PS_SHIFT;
7854 
7855 	if (ar_ht_cap & WMI_HT_CAP_HT20_SGI)
7856 		ht_cap.cap |= IEEE80211_HT_CAP_SGI_20;
7857 
7858 	if (ar_ht_cap & WMI_HT_CAP_HT40_SGI)
7859 		ht_cap.cap |= IEEE80211_HT_CAP_SGI_40;
7860 
7861 	if (ar_ht_cap & WMI_HT_CAP_DYNAMIC_SMPS) {
7862 		u32 smps;
7863 
7864 		smps   = WLAN_HT_CAP_SM_PS_DYNAMIC;
7865 		smps <<= IEEE80211_HT_CAP_SM_PS_SHIFT;
7866 
7867 		ht_cap.cap |= smps;
7868 	}
7869 
7870 	if (ar_ht_cap & WMI_HT_CAP_TX_STBC)
7871 		ht_cap.cap |= IEEE80211_HT_CAP_TX_STBC;
7872 
7873 	if (ar_ht_cap & WMI_HT_CAP_RX_STBC) {
7874 		u32 stbc;
7875 
7876 		stbc   = ar_ht_cap;
7877 		stbc  &= WMI_HT_CAP_RX_STBC;
7878 		stbc >>= WMI_HT_CAP_RX_STBC_MASK_SHIFT;
7879 		stbc <<= IEEE80211_HT_CAP_RX_STBC_SHIFT;
7880 		stbc  &= IEEE80211_HT_CAP_RX_STBC;
7881 
7882 		ht_cap.cap |= stbc;
7883 	}
7884 
7885 	if (ar_ht_cap & WMI_HT_CAP_RX_LDPC)
7886 		ht_cap.cap |= IEEE80211_HT_CAP_LDPC_CODING;
7887 
7888 	if (ar_ht_cap & WMI_HT_CAP_L_SIG_TXOP_PROT)
7889 		ht_cap.cap |= IEEE80211_HT_CAP_LSIG_TXOP_PROT;
7890 
7891 	if (ar_vht_cap & WMI_VHT_CAP_MAX_MPDU_LEN_MASK)
7892 		ht_cap.cap |= IEEE80211_HT_CAP_MAX_AMSDU;
7893 
7894 	for (i = 0; i < ar->num_rx_chains; i++) {
7895 		if (rate_cap_rx_chainmask & BIT(i))
7896 			ht_cap.mcs.rx_mask[i] = 0xFF;
7897 	}
7898 
7899 	ht_cap.mcs.tx_params |= IEEE80211_HT_MCS_TX_DEFINED;
7900 
7901 	return ht_cap;
7902 }
7903 
7904 static int ath12k_mac_set_txbf_conf(struct ath12k_link_vif *arvif)
7905 {
7906 	u32 value = 0;
7907 	struct ath12k *ar = arvif->ar;
7908 	struct ath12k_vif *ahvif = arvif->ahvif;
7909 	int nsts;
7910 	int sound_dim;
7911 	u32 vht_cap = ar->pdev->cap.vht_cap;
7912 	u32 vdev_param = WMI_VDEV_PARAM_TXBF;
7913 
7914 	if (vht_cap & (IEEE80211_VHT_CAP_SU_BEAMFORMEE_CAPABLE)) {
7915 		nsts = vht_cap & IEEE80211_VHT_CAP_BEAMFORMEE_STS_MASK;
7916 		nsts >>= IEEE80211_VHT_CAP_BEAMFORMEE_STS_SHIFT;
7917 		value |= SM(nsts, WMI_TXBF_STS_CAP_OFFSET);
7918 	}
7919 
7920 	if (vht_cap & (IEEE80211_VHT_CAP_SU_BEAMFORMER_CAPABLE)) {
7921 		sound_dim = vht_cap &
7922 			    IEEE80211_VHT_CAP_SOUNDING_DIMENSIONS_MASK;
7923 		sound_dim >>= IEEE80211_VHT_CAP_SOUNDING_DIMENSIONS_SHIFT;
7924 		if (sound_dim > (ar->num_tx_chains - 1))
7925 			sound_dim = ar->num_tx_chains - 1;
7926 		value |= SM(sound_dim, WMI_BF_SOUND_DIM_OFFSET);
7927 	}
7928 
7929 	if (!value)
7930 		return 0;
7931 
7932 	if (vht_cap & IEEE80211_VHT_CAP_SU_BEAMFORMER_CAPABLE) {
7933 		value |= WMI_VDEV_PARAM_TXBF_SU_TX_BFER;
7934 
7935 		if ((vht_cap & IEEE80211_VHT_CAP_MU_BEAMFORMER_CAPABLE) &&
7936 		    ahvif->vdev_type == WMI_VDEV_TYPE_AP)
7937 			value |= WMI_VDEV_PARAM_TXBF_MU_TX_BFER;
7938 	}
7939 
7940 	if (vht_cap & IEEE80211_VHT_CAP_SU_BEAMFORMEE_CAPABLE) {
7941 		value |= WMI_VDEV_PARAM_TXBF_SU_TX_BFEE;
7942 
7943 		if ((vht_cap & IEEE80211_VHT_CAP_MU_BEAMFORMEE_CAPABLE) &&
7944 		    ahvif->vdev_type == WMI_VDEV_TYPE_STA)
7945 			value |= WMI_VDEV_PARAM_TXBF_MU_TX_BFEE;
7946 	}
7947 
7948 	return ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
7949 					     vdev_param, value);
7950 }
7951 
7952 static void ath12k_set_vht_txbf_cap(struct ath12k *ar, u32 *vht_cap)
7953 {
7954 	bool subfer, subfee;
7955 	int sound_dim = 0;
7956 
7957 	subfer = !!(*vht_cap & (IEEE80211_VHT_CAP_SU_BEAMFORMER_CAPABLE));
7958 	subfee = !!(*vht_cap & (IEEE80211_VHT_CAP_SU_BEAMFORMEE_CAPABLE));
7959 
7960 	if (ar->num_tx_chains < 2) {
7961 		*vht_cap &= ~(IEEE80211_VHT_CAP_SU_BEAMFORMER_CAPABLE);
7962 		subfer = false;
7963 	}
7964 
7965 	/* If SU Beaformer is not set, then disable MU Beamformer Capability */
7966 	if (!subfer)
7967 		*vht_cap &= ~(IEEE80211_VHT_CAP_MU_BEAMFORMER_CAPABLE);
7968 
7969 	/* If SU Beaformee is not set, then disable MU Beamformee Capability */
7970 	if (!subfee)
7971 		*vht_cap &= ~(IEEE80211_VHT_CAP_MU_BEAMFORMEE_CAPABLE);
7972 
7973 	sound_dim = u32_get_bits(*vht_cap,
7974 				 IEEE80211_VHT_CAP_SOUNDING_DIMENSIONS_MASK);
7975 	*vht_cap = u32_replace_bits(*vht_cap, 0,
7976 				    IEEE80211_VHT_CAP_SOUNDING_DIMENSIONS_MASK);
7977 
7978 	/* TODO: Need to check invalid STS and Sound_dim values set by FW? */
7979 
7980 	/* Enable Sounding Dimension Field only if SU BF is enabled */
7981 	if (subfer) {
7982 		if (sound_dim > (ar->num_tx_chains - 1))
7983 			sound_dim = ar->num_tx_chains - 1;
7984 
7985 		*vht_cap = u32_replace_bits(*vht_cap, sound_dim,
7986 					    IEEE80211_VHT_CAP_SOUNDING_DIMENSIONS_MASK);
7987 	}
7988 
7989 	/* Use the STS advertised by FW unless SU Beamformee is not supported*/
7990 	if (!subfee)
7991 		*vht_cap &= ~(IEEE80211_VHT_CAP_BEAMFORMEE_STS_MASK);
7992 }
7993 
7994 static struct ieee80211_sta_vht_cap
7995 ath12k_create_vht_cap(struct ath12k *ar, u32 rate_cap_tx_chainmask,
7996 		      u32 rate_cap_rx_chainmask)
7997 {
7998 	struct ieee80211_sta_vht_cap vht_cap = {};
7999 	u16 txmcs_map, rxmcs_map;
8000 	int i;
8001 
8002 	vht_cap.vht_supported = 1;
8003 	vht_cap.cap = ar->pdev->cap.vht_cap;
8004 
8005 	ath12k_set_vht_txbf_cap(ar, &vht_cap.cap);
8006 
8007 	/* 80P80 is not supported */
8008 	vht_cap.cap &= ~IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_160_80PLUS80MHZ;
8009 
8010 	rxmcs_map = 0;
8011 	txmcs_map = 0;
8012 	for (i = 0; i < 8; i++) {
8013 		if (i < ar->num_tx_chains && rate_cap_tx_chainmask & BIT(i))
8014 			txmcs_map |= IEEE80211_VHT_MCS_SUPPORT_0_9 << (i * 2);
8015 		else
8016 			txmcs_map |= IEEE80211_VHT_MCS_NOT_SUPPORTED << (i * 2);
8017 
8018 		if (i < ar->num_rx_chains && rate_cap_rx_chainmask & BIT(i))
8019 			rxmcs_map |= IEEE80211_VHT_MCS_SUPPORT_0_9 << (i * 2);
8020 		else
8021 			rxmcs_map |= IEEE80211_VHT_MCS_NOT_SUPPORTED << (i * 2);
8022 	}
8023 
8024 	if (rate_cap_tx_chainmask <= 1)
8025 		vht_cap.cap &= ~IEEE80211_VHT_CAP_TXSTBC;
8026 
8027 	vht_cap.vht_mcs.rx_mcs_map = cpu_to_le16(rxmcs_map);
8028 	vht_cap.vht_mcs.tx_mcs_map = cpu_to_le16(txmcs_map);
8029 
8030 	/* Check if the HW supports 1:1 NSS ratio and reset
8031 	 * EXT NSS BW Support field to 0 to indicate 1:1 ratio
8032 	 */
8033 	if (ar->pdev->cap.nss_ratio_info == WMI_NSS_RATIO_1_NSS)
8034 		vht_cap.cap &= ~IEEE80211_VHT_CAP_EXT_NSS_BW_MASK;
8035 
8036 	return vht_cap;
8037 }
8038 
8039 static void ath12k_mac_setup_ht_vht_cap(struct ath12k *ar,
8040 					struct ath12k_pdev_cap *cap,
8041 					u32 *ht_cap_info)
8042 {
8043 	struct ieee80211_supported_band *band;
8044 	u32 rate_cap_tx_chainmask;
8045 	u32 rate_cap_rx_chainmask;
8046 	u32 ht_cap;
8047 
8048 	rate_cap_tx_chainmask = ar->cfg_tx_chainmask >> cap->tx_chain_mask_shift;
8049 	rate_cap_rx_chainmask = ar->cfg_rx_chainmask >> cap->rx_chain_mask_shift;
8050 
8051 	if (cap->supported_bands & WMI_HOST_WLAN_2GHZ_CAP) {
8052 		band = &ar->mac.sbands[NL80211_BAND_2GHZ];
8053 		ht_cap = cap->band[NL80211_BAND_2GHZ].ht_cap_info;
8054 		if (ht_cap_info)
8055 			*ht_cap_info = ht_cap;
8056 		band->ht_cap = ath12k_create_ht_cap(ar, ht_cap,
8057 						    rate_cap_rx_chainmask);
8058 	}
8059 
8060 	if (cap->supported_bands & WMI_HOST_WLAN_5GHZ_CAP &&
8061 	    (ar->ab->hw_params->single_pdev_only ||
8062 	     !ar->supports_6ghz)) {
8063 		band = &ar->mac.sbands[NL80211_BAND_5GHZ];
8064 		ht_cap = cap->band[NL80211_BAND_5GHZ].ht_cap_info;
8065 		if (ht_cap_info)
8066 			*ht_cap_info = ht_cap;
8067 		band->ht_cap = ath12k_create_ht_cap(ar, ht_cap,
8068 						    rate_cap_rx_chainmask);
8069 		band->vht_cap = ath12k_create_vht_cap(ar, rate_cap_tx_chainmask,
8070 						      rate_cap_rx_chainmask);
8071 	}
8072 }
8073 
8074 static int ath12k_check_chain_mask(struct ath12k *ar, u32 ant, bool is_tx_ant)
8075 {
8076 	/* TODO: Check the request chainmask against the supported
8077 	 * chainmask table which is advertised in extented_service_ready event
8078 	 */
8079 
8080 	return 0;
8081 }
8082 
8083 static void ath12k_gen_ppe_thresh(struct ath12k_wmi_ppe_threshold_arg *fw_ppet,
8084 				  u8 *he_ppet)
8085 {
8086 	int nss, ru;
8087 	u8 bit = 7;
8088 
8089 	he_ppet[0] = fw_ppet->numss_m1 & IEEE80211_PPE_THRES_NSS_MASK;
8090 	he_ppet[0] |= (fw_ppet->ru_bit_mask <<
8091 		       IEEE80211_PPE_THRES_RU_INDEX_BITMASK_POS) &
8092 		      IEEE80211_PPE_THRES_RU_INDEX_BITMASK_MASK;
8093 	for (nss = 0; nss <= fw_ppet->numss_m1; nss++) {
8094 		for (ru = 0; ru < 4; ru++) {
8095 			u8 val;
8096 			int i;
8097 
8098 			if ((fw_ppet->ru_bit_mask & BIT(ru)) == 0)
8099 				continue;
8100 			val = (fw_ppet->ppet16_ppet8_ru3_ru0[nss] >> (ru * 6)) &
8101 			       0x3f;
8102 			val = ((val >> 3) & 0x7) | ((val & 0x7) << 3);
8103 			for (i = 5; i >= 0; i--) {
8104 				he_ppet[bit / 8] |=
8105 					((val >> i) & 0x1) << ((bit % 8));
8106 				bit++;
8107 			}
8108 		}
8109 	}
8110 }
8111 
8112 static void
8113 ath12k_mac_filter_he_cap_mesh(struct ieee80211_he_cap_elem *he_cap_elem)
8114 {
8115 	u8 m;
8116 
8117 	m = IEEE80211_HE_MAC_CAP0_TWT_RES |
8118 	    IEEE80211_HE_MAC_CAP0_TWT_REQ;
8119 	he_cap_elem->mac_cap_info[0] &= ~m;
8120 
8121 	m = IEEE80211_HE_MAC_CAP2_TRS |
8122 	    IEEE80211_HE_MAC_CAP2_BCAST_TWT |
8123 	    IEEE80211_HE_MAC_CAP2_MU_CASCADING;
8124 	he_cap_elem->mac_cap_info[2] &= ~m;
8125 
8126 	m = IEEE80211_HE_MAC_CAP3_FLEX_TWT_SCHED |
8127 	    IEEE80211_HE_MAC_CAP2_BCAST_TWT |
8128 	    IEEE80211_HE_MAC_CAP2_MU_CASCADING;
8129 	he_cap_elem->mac_cap_info[3] &= ~m;
8130 
8131 	m = IEEE80211_HE_MAC_CAP4_BSRP_BQRP_A_MPDU_AGG |
8132 	    IEEE80211_HE_MAC_CAP4_BQR;
8133 	he_cap_elem->mac_cap_info[4] &= ~m;
8134 
8135 	m = IEEE80211_HE_MAC_CAP5_SUBCHAN_SELECTIVE_TRANSMISSION |
8136 	    IEEE80211_HE_MAC_CAP5_UL_2x996_TONE_RU |
8137 	    IEEE80211_HE_MAC_CAP5_PUNCTURED_SOUNDING |
8138 	    IEEE80211_HE_MAC_CAP5_HT_VHT_TRIG_FRAME_RX;
8139 	he_cap_elem->mac_cap_info[5] &= ~m;
8140 
8141 	m = IEEE80211_HE_PHY_CAP2_UL_MU_FULL_MU_MIMO |
8142 	    IEEE80211_HE_PHY_CAP2_UL_MU_PARTIAL_MU_MIMO;
8143 	he_cap_elem->phy_cap_info[2] &= ~m;
8144 
8145 	m = IEEE80211_HE_PHY_CAP3_RX_PARTIAL_BW_SU_IN_20MHZ_MU |
8146 	    IEEE80211_HE_PHY_CAP3_DCM_MAX_CONST_TX_MASK |
8147 	    IEEE80211_HE_PHY_CAP3_DCM_MAX_CONST_RX_MASK;
8148 	he_cap_elem->phy_cap_info[3] &= ~m;
8149 
8150 	m = IEEE80211_HE_PHY_CAP4_MU_BEAMFORMER;
8151 	he_cap_elem->phy_cap_info[4] &= ~m;
8152 
8153 	m = IEEE80211_HE_PHY_CAP5_NG16_MU_FEEDBACK;
8154 	he_cap_elem->phy_cap_info[5] &= ~m;
8155 
8156 	m = IEEE80211_HE_PHY_CAP6_CODEBOOK_SIZE_75_MU |
8157 	    IEEE80211_HE_PHY_CAP6_TRIG_MU_BEAMFORMING_PARTIAL_BW_FB |
8158 	    IEEE80211_HE_PHY_CAP6_TRIG_CQI_FB |
8159 	    IEEE80211_HE_PHY_CAP6_PARTIAL_BANDWIDTH_DL_MUMIMO;
8160 	he_cap_elem->phy_cap_info[6] &= ~m;
8161 
8162 	m = IEEE80211_HE_PHY_CAP7_PSR_BASED_SR |
8163 	    IEEE80211_HE_PHY_CAP7_POWER_BOOST_FACTOR_SUPP |
8164 	    IEEE80211_HE_PHY_CAP7_STBC_TX_ABOVE_80MHZ |
8165 	    IEEE80211_HE_PHY_CAP7_STBC_RX_ABOVE_80MHZ;
8166 	he_cap_elem->phy_cap_info[7] &= ~m;
8167 
8168 	m = IEEE80211_HE_PHY_CAP8_HE_ER_SU_PPDU_4XLTF_AND_08_US_GI |
8169 	    IEEE80211_HE_PHY_CAP8_20MHZ_IN_40MHZ_HE_PPDU_IN_2G |
8170 	    IEEE80211_HE_PHY_CAP8_20MHZ_IN_160MHZ_HE_PPDU |
8171 	    IEEE80211_HE_PHY_CAP8_80MHZ_IN_160MHZ_HE_PPDU;
8172 	he_cap_elem->phy_cap_info[8] &= ~m;
8173 
8174 	m = IEEE80211_HE_PHY_CAP9_LONGER_THAN_16_SIGB_OFDM_SYM |
8175 	    IEEE80211_HE_PHY_CAP9_NON_TRIGGERED_CQI_FEEDBACK |
8176 	    IEEE80211_HE_PHY_CAP9_RX_1024_QAM_LESS_THAN_242_TONE_RU |
8177 	    IEEE80211_HE_PHY_CAP9_TX_1024_QAM_LESS_THAN_242_TONE_RU |
8178 	    IEEE80211_HE_PHY_CAP9_RX_FULL_BW_SU_USING_MU_WITH_COMP_SIGB |
8179 	    IEEE80211_HE_PHY_CAP9_RX_FULL_BW_SU_USING_MU_WITH_NON_COMP_SIGB;
8180 	he_cap_elem->phy_cap_info[9] &= ~m;
8181 }
8182 
8183 static __le16 ath12k_mac_setup_he_6ghz_cap(struct ath12k_pdev_cap *pcap,
8184 					   struct ath12k_band_cap *bcap)
8185 {
8186 	u8 val;
8187 
8188 	bcap->he_6ghz_capa = IEEE80211_HT_MPDU_DENSITY_NONE;
8189 	if (bcap->ht_cap_info & WMI_HT_CAP_DYNAMIC_SMPS)
8190 		bcap->he_6ghz_capa |=
8191 			u32_encode_bits(WLAN_HT_CAP_SM_PS_DYNAMIC,
8192 					IEEE80211_HE_6GHZ_CAP_SM_PS);
8193 	else
8194 		bcap->he_6ghz_capa |=
8195 			u32_encode_bits(WLAN_HT_CAP_SM_PS_DISABLED,
8196 					IEEE80211_HE_6GHZ_CAP_SM_PS);
8197 	val = u32_get_bits(pcap->vht_cap,
8198 			   IEEE80211_VHT_CAP_MAX_A_MPDU_LENGTH_EXPONENT_MASK);
8199 	bcap->he_6ghz_capa |=
8200 		u32_encode_bits(val, IEEE80211_HE_6GHZ_CAP_MAX_AMPDU_LEN_EXP);
8201 	val = u32_get_bits(pcap->vht_cap,
8202 			   IEEE80211_VHT_CAP_MAX_MPDU_MASK);
8203 	bcap->he_6ghz_capa |=
8204 		u32_encode_bits(val, IEEE80211_HE_6GHZ_CAP_MAX_MPDU_LEN);
8205 	if (pcap->vht_cap & IEEE80211_VHT_CAP_RX_ANTENNA_PATTERN)
8206 		bcap->he_6ghz_capa |= IEEE80211_HE_6GHZ_CAP_RX_ANTPAT_CONS;
8207 	if (pcap->vht_cap & IEEE80211_VHT_CAP_TX_ANTENNA_PATTERN)
8208 		bcap->he_6ghz_capa |= IEEE80211_HE_6GHZ_CAP_TX_ANTPAT_CONS;
8209 
8210 	return cpu_to_le16(bcap->he_6ghz_capa);
8211 }
8212 
8213 static void ath12k_mac_set_hemcsmap(struct ath12k *ar,
8214 				    struct ath12k_pdev_cap *cap,
8215 				    struct ieee80211_sta_he_cap *he_cap)
8216 {
8217 	struct ieee80211_he_mcs_nss_supp *mcs_nss = &he_cap->he_mcs_nss_supp;
8218 	u8 maxtxnss_160 = ath12k_get_nss_160mhz(ar, ar->num_tx_chains);
8219 	u8 maxrxnss_160 = ath12k_get_nss_160mhz(ar, ar->num_rx_chains);
8220 	u16 txmcs_map_160 = 0, rxmcs_map_160 = 0;
8221 	u16 txmcs_map = 0, rxmcs_map = 0;
8222 	u32 i;
8223 
8224 	for (i = 0; i < 8; i++) {
8225 		if (i < ar->num_tx_chains &&
8226 		    (ar->cfg_tx_chainmask >> cap->tx_chain_mask_shift) & BIT(i))
8227 			txmcs_map |= IEEE80211_HE_MCS_SUPPORT_0_11 << (i * 2);
8228 		else
8229 			txmcs_map |= IEEE80211_HE_MCS_NOT_SUPPORTED << (i * 2);
8230 
8231 		if (i < ar->num_rx_chains &&
8232 		    (ar->cfg_rx_chainmask >> cap->tx_chain_mask_shift) & BIT(i))
8233 			rxmcs_map |= IEEE80211_HE_MCS_SUPPORT_0_11 << (i * 2);
8234 		else
8235 			rxmcs_map |= IEEE80211_HE_MCS_NOT_SUPPORTED << (i * 2);
8236 
8237 		if (i < maxtxnss_160 &&
8238 		    (ar->cfg_tx_chainmask >> cap->tx_chain_mask_shift) & BIT(i))
8239 			txmcs_map_160 |= IEEE80211_HE_MCS_SUPPORT_0_11 << (i * 2);
8240 		else
8241 			txmcs_map_160 |= IEEE80211_HE_MCS_NOT_SUPPORTED << (i * 2);
8242 
8243 		if (i < maxrxnss_160 &&
8244 		    (ar->cfg_tx_chainmask >> cap->tx_chain_mask_shift) & BIT(i))
8245 			rxmcs_map_160 |= IEEE80211_HE_MCS_SUPPORT_0_11 << (i * 2);
8246 		else
8247 			rxmcs_map_160 |= IEEE80211_HE_MCS_NOT_SUPPORTED << (i * 2);
8248 	}
8249 
8250 	mcs_nss->rx_mcs_80 = cpu_to_le16(rxmcs_map & 0xffff);
8251 	mcs_nss->tx_mcs_80 = cpu_to_le16(txmcs_map & 0xffff);
8252 	mcs_nss->rx_mcs_160 = cpu_to_le16(rxmcs_map_160 & 0xffff);
8253 	mcs_nss->tx_mcs_160 = cpu_to_le16(txmcs_map_160 & 0xffff);
8254 }
8255 
8256 static void ath12k_mac_copy_he_cap(struct ath12k *ar,
8257 				   struct ath12k_band_cap *band_cap,
8258 				   int iftype, u8 num_tx_chains,
8259 				   struct ieee80211_sta_he_cap *he_cap)
8260 {
8261 	struct ieee80211_he_cap_elem *he_cap_elem = &he_cap->he_cap_elem;
8262 
8263 	he_cap->has_he = true;
8264 	memcpy(he_cap_elem->mac_cap_info, band_cap->he_cap_info,
8265 	       sizeof(he_cap_elem->mac_cap_info));
8266 	memcpy(he_cap_elem->phy_cap_info, band_cap->he_cap_phy_info,
8267 	       sizeof(he_cap_elem->phy_cap_info));
8268 
8269 	he_cap_elem->mac_cap_info[1] &=
8270 		IEEE80211_HE_MAC_CAP1_TF_MAC_PAD_DUR_MASK;
8271 	he_cap_elem->phy_cap_info[0] &=
8272 		IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_40MHZ_IN_2G |
8273 		IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_40MHZ_80MHZ_IN_5G |
8274 		IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_160MHZ_IN_5G;
8275 	/* 80PLUS80 is not supported */
8276 	he_cap_elem->phy_cap_info[0] &=
8277 		~IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_80PLUS80_MHZ_IN_5G;
8278 	he_cap_elem->phy_cap_info[5] &=
8279 		~IEEE80211_HE_PHY_CAP5_BEAMFORMEE_NUM_SND_DIM_UNDER_80MHZ_MASK;
8280 	he_cap_elem->phy_cap_info[5] |= num_tx_chains - 1;
8281 
8282 	switch (iftype) {
8283 	case NL80211_IFTYPE_AP:
8284 		he_cap_elem->mac_cap_info[2] &=
8285 			~IEEE80211_HE_MAC_CAP2_BCAST_TWT;
8286 		he_cap_elem->phy_cap_info[3] &=
8287 			~IEEE80211_HE_PHY_CAP3_DCM_MAX_CONST_TX_MASK;
8288 		he_cap_elem->phy_cap_info[9] |=
8289 			IEEE80211_HE_PHY_CAP9_RX_1024_QAM_LESS_THAN_242_TONE_RU;
8290 		break;
8291 	case NL80211_IFTYPE_STATION:
8292 		he_cap_elem->mac_cap_info[0] &= ~IEEE80211_HE_MAC_CAP0_TWT_RES;
8293 		he_cap_elem->mac_cap_info[0] |= IEEE80211_HE_MAC_CAP0_TWT_REQ;
8294 		he_cap_elem->phy_cap_info[9] |=
8295 			IEEE80211_HE_PHY_CAP9_TX_1024_QAM_LESS_THAN_242_TONE_RU;
8296 		break;
8297 	case NL80211_IFTYPE_MESH_POINT:
8298 		ath12k_mac_filter_he_cap_mesh(he_cap_elem);
8299 		break;
8300 	}
8301 
8302 	ath12k_mac_set_hemcsmap(ar, &ar->pdev->cap, he_cap);
8303 	memset(he_cap->ppe_thres, 0, sizeof(he_cap->ppe_thres));
8304 	if (he_cap_elem->phy_cap_info[6] &
8305 	    IEEE80211_HE_PHY_CAP6_PPE_THRESHOLD_PRESENT)
8306 		ath12k_gen_ppe_thresh(&band_cap->he_ppet, he_cap->ppe_thres);
8307 }
8308 
8309 static void
8310 ath12k_mac_copy_eht_mcs_nss(struct ath12k_band_cap *band_cap,
8311 			    struct ieee80211_eht_mcs_nss_supp *mcs_nss,
8312 			    const struct ieee80211_he_cap_elem *he_cap,
8313 			    const struct ieee80211_eht_cap_elem_fixed *eht_cap)
8314 {
8315 	if ((he_cap->phy_cap_info[0] &
8316 	     (IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_40MHZ_IN_2G |
8317 	      IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_40MHZ_80MHZ_IN_5G |
8318 	      IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_160MHZ_IN_5G |
8319 	      IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_80PLUS80_MHZ_IN_5G)) == 0)
8320 		memcpy(&mcs_nss->only_20mhz, &band_cap->eht_mcs_20_only,
8321 		       sizeof(struct ieee80211_eht_mcs_nss_supp_20mhz_only));
8322 
8323 	if (he_cap->phy_cap_info[0] &
8324 	    (IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_40MHZ_IN_2G |
8325 	     IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_40MHZ_80MHZ_IN_5G))
8326 		memcpy(&mcs_nss->bw._80, &band_cap->eht_mcs_80,
8327 		       sizeof(struct ieee80211_eht_mcs_nss_supp_bw));
8328 
8329 	if (he_cap->phy_cap_info[0] &
8330 	    IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_160MHZ_IN_5G)
8331 		memcpy(&mcs_nss->bw._160, &band_cap->eht_mcs_160,
8332 		       sizeof(struct ieee80211_eht_mcs_nss_supp_bw));
8333 
8334 	if (eht_cap->phy_cap_info[0] & IEEE80211_EHT_PHY_CAP0_320MHZ_IN_6GHZ)
8335 		memcpy(&mcs_nss->bw._320, &band_cap->eht_mcs_320,
8336 		       sizeof(struct ieee80211_eht_mcs_nss_supp_bw));
8337 }
8338 
8339 static void ath12k_mac_copy_eht_ppe_thresh(struct ath12k_wmi_ppe_threshold_arg *fw_ppet,
8340 					   struct ieee80211_sta_eht_cap *cap)
8341 {
8342 	u16 bit = IEEE80211_EHT_PPE_THRES_INFO_HEADER_SIZE;
8343 	u8 i, nss, ru, ppet_bit_len_per_ru = IEEE80211_EHT_PPE_THRES_INFO_PPET_SIZE * 2;
8344 
8345 	u8p_replace_bits(&cap->eht_ppe_thres[0], fw_ppet->numss_m1,
8346 			 IEEE80211_EHT_PPE_THRES_NSS_MASK);
8347 
8348 	u16p_replace_bits((u16 *)&cap->eht_ppe_thres[0], fw_ppet->ru_bit_mask,
8349 			  IEEE80211_EHT_PPE_THRES_RU_INDEX_BITMASK_MASK);
8350 
8351 	for (nss = 0; nss <= fw_ppet->numss_m1; nss++) {
8352 		for (ru = 0;
8353 		     ru < hweight16(IEEE80211_EHT_PPE_THRES_RU_INDEX_BITMASK_MASK);
8354 		     ru++) {
8355 			u32 val = 0;
8356 
8357 			if ((fw_ppet->ru_bit_mask & BIT(ru)) == 0)
8358 				continue;
8359 
8360 			u32p_replace_bits(&val, fw_ppet->ppet16_ppet8_ru3_ru0[nss] >>
8361 						(ru * ppet_bit_len_per_ru),
8362 					  GENMASK(ppet_bit_len_per_ru - 1, 0));
8363 
8364 			for (i = 0; i < ppet_bit_len_per_ru; i++) {
8365 				cap->eht_ppe_thres[bit / 8] |=
8366 					(((val >> i) & 0x1) << ((bit % 8)));
8367 				bit++;
8368 			}
8369 		}
8370 	}
8371 }
8372 
8373 static void
8374 ath12k_mac_filter_eht_cap_mesh(struct ieee80211_eht_cap_elem_fixed
8375 			       *eht_cap_elem)
8376 {
8377 	u8 m;
8378 
8379 	m = IEEE80211_EHT_MAC_CAP0_EPCS_PRIO_ACCESS;
8380 	eht_cap_elem->mac_cap_info[0] &= ~m;
8381 
8382 	m = IEEE80211_EHT_PHY_CAP0_PARTIAL_BW_UL_MU_MIMO;
8383 	eht_cap_elem->phy_cap_info[0] &= ~m;
8384 
8385 	m = IEEE80211_EHT_PHY_CAP3_NG_16_MU_FEEDBACK |
8386 	    IEEE80211_EHT_PHY_CAP3_CODEBOOK_7_5_MU_FDBK |
8387 	    IEEE80211_EHT_PHY_CAP3_TRIG_MU_BF_PART_BW_FDBK |
8388 	    IEEE80211_EHT_PHY_CAP3_TRIG_CQI_FDBK;
8389 	eht_cap_elem->phy_cap_info[3] &= ~m;
8390 
8391 	m = IEEE80211_EHT_PHY_CAP4_PART_BW_DL_MU_MIMO |
8392 	    IEEE80211_EHT_PHY_CAP4_PSR_SR_SUPP |
8393 	    IEEE80211_EHT_PHY_CAP4_POWER_BOOST_FACT_SUPP |
8394 	    IEEE80211_EHT_PHY_CAP4_EHT_MU_PPDU_4_EHT_LTF_08_GI;
8395 	eht_cap_elem->phy_cap_info[4] &= ~m;
8396 
8397 	m = IEEE80211_EHT_PHY_CAP5_NON_TRIG_CQI_FEEDBACK |
8398 	    IEEE80211_EHT_PHY_CAP5_TX_LESS_242_TONE_RU_SUPP |
8399 	    IEEE80211_EHT_PHY_CAP5_RX_LESS_242_TONE_RU_SUPP |
8400 	    IEEE80211_EHT_PHY_CAP5_MAX_NUM_SUPP_EHT_LTF_MASK;
8401 	eht_cap_elem->phy_cap_info[5] &= ~m;
8402 
8403 	m = IEEE80211_EHT_PHY_CAP6_MAX_NUM_SUPP_EHT_LTF_MASK;
8404 	eht_cap_elem->phy_cap_info[6] &= ~m;
8405 
8406 	m = IEEE80211_EHT_PHY_CAP7_NON_OFDMA_UL_MU_MIMO_80MHZ |
8407 	    IEEE80211_EHT_PHY_CAP7_NON_OFDMA_UL_MU_MIMO_160MHZ |
8408 	    IEEE80211_EHT_PHY_CAP7_NON_OFDMA_UL_MU_MIMO_320MHZ |
8409 	    IEEE80211_EHT_PHY_CAP7_MU_BEAMFORMER_80MHZ |
8410 	    IEEE80211_EHT_PHY_CAP7_MU_BEAMFORMER_160MHZ |
8411 	    IEEE80211_EHT_PHY_CAP7_MU_BEAMFORMER_320MHZ;
8412 	eht_cap_elem->phy_cap_info[7] &= ~m;
8413 }
8414 
8415 static void ath12k_mac_copy_eht_cap(struct ath12k *ar,
8416 				    struct ath12k_band_cap *band_cap,
8417 				    struct ieee80211_he_cap_elem *he_cap_elem,
8418 				    int iftype,
8419 				    struct ieee80211_sta_eht_cap *eht_cap)
8420 {
8421 	struct ieee80211_eht_cap_elem_fixed *eht_cap_elem = &eht_cap->eht_cap_elem;
8422 
8423 	memset(eht_cap, 0, sizeof(struct ieee80211_sta_eht_cap));
8424 
8425 	if (!(test_bit(WMI_TLV_SERVICE_11BE, ar->ab->wmi_ab.svc_map)) ||
8426 	    ath12k_acpi_get_disable_11be(ar->ab))
8427 		return;
8428 
8429 	eht_cap->has_eht = true;
8430 	memcpy(eht_cap_elem->mac_cap_info, band_cap->eht_cap_mac_info,
8431 	       sizeof(eht_cap_elem->mac_cap_info));
8432 	memcpy(eht_cap_elem->phy_cap_info, band_cap->eht_cap_phy_info,
8433 	       sizeof(eht_cap_elem->phy_cap_info));
8434 
8435 	switch (iftype) {
8436 	case NL80211_IFTYPE_AP:
8437 		eht_cap_elem->phy_cap_info[0] &=
8438 			~IEEE80211_EHT_PHY_CAP0_242_TONE_RU_GT20MHZ;
8439 		eht_cap_elem->phy_cap_info[4] &=
8440 			~IEEE80211_EHT_PHY_CAP4_PART_BW_DL_MU_MIMO;
8441 		eht_cap_elem->phy_cap_info[5] &=
8442 			~IEEE80211_EHT_PHY_CAP5_TX_LESS_242_TONE_RU_SUPP;
8443 		break;
8444 	case NL80211_IFTYPE_STATION:
8445 		eht_cap_elem->phy_cap_info[7] &=
8446 			~(IEEE80211_EHT_PHY_CAP7_NON_OFDMA_UL_MU_MIMO_80MHZ |
8447 			  IEEE80211_EHT_PHY_CAP7_NON_OFDMA_UL_MU_MIMO_160MHZ |
8448 			  IEEE80211_EHT_PHY_CAP7_NON_OFDMA_UL_MU_MIMO_320MHZ);
8449 		eht_cap_elem->phy_cap_info[7] &=
8450 			~(IEEE80211_EHT_PHY_CAP7_MU_BEAMFORMER_80MHZ |
8451 			  IEEE80211_EHT_PHY_CAP7_MU_BEAMFORMER_160MHZ |
8452 			  IEEE80211_EHT_PHY_CAP7_MU_BEAMFORMER_320MHZ);
8453 		break;
8454 	case NL80211_IFTYPE_MESH_POINT:
8455 		ath12k_mac_filter_eht_cap_mesh(eht_cap_elem);
8456 		break;
8457 	default:
8458 		break;
8459 	}
8460 
8461 	ath12k_mac_copy_eht_mcs_nss(band_cap, &eht_cap->eht_mcs_nss_supp,
8462 				    he_cap_elem, eht_cap_elem);
8463 
8464 	if (eht_cap_elem->phy_cap_info[5] &
8465 	    IEEE80211_EHT_PHY_CAP5_PPE_THRESHOLD_PRESENT)
8466 		ath12k_mac_copy_eht_ppe_thresh(&band_cap->eht_ppet, eht_cap);
8467 }
8468 
8469 static int ath12k_mac_copy_sband_iftype_data(struct ath12k *ar,
8470 					     struct ath12k_pdev_cap *cap,
8471 					     struct ieee80211_sband_iftype_data *data,
8472 					     int band)
8473 {
8474 	struct ath12k_band_cap *band_cap = &cap->band[band];
8475 	int i, idx = 0;
8476 
8477 	for (i = 0; i < NUM_NL80211_IFTYPES; i++) {
8478 		struct ieee80211_sta_he_cap *he_cap = &data[idx].he_cap;
8479 
8480 		switch (i) {
8481 		case NL80211_IFTYPE_STATION:
8482 		case NL80211_IFTYPE_AP:
8483 		case NL80211_IFTYPE_MESH_POINT:
8484 			break;
8485 
8486 		default:
8487 			continue;
8488 		}
8489 
8490 		data[idx].types_mask = BIT(i);
8491 
8492 		ath12k_mac_copy_he_cap(ar, band_cap, i, ar->num_tx_chains, he_cap);
8493 		if (band == NL80211_BAND_6GHZ) {
8494 			data[idx].he_6ghz_capa.capa =
8495 				ath12k_mac_setup_he_6ghz_cap(cap, band_cap);
8496 		}
8497 		ath12k_mac_copy_eht_cap(ar, band_cap, &he_cap->he_cap_elem, i,
8498 					&data[idx].eht_cap);
8499 		idx++;
8500 	}
8501 
8502 	return idx;
8503 }
8504 
8505 static void ath12k_mac_setup_sband_iftype_data(struct ath12k *ar,
8506 					       struct ath12k_pdev_cap *cap)
8507 {
8508 	struct ieee80211_supported_band *sband;
8509 	enum nl80211_band band;
8510 	int count;
8511 
8512 	if (cap->supported_bands & WMI_HOST_WLAN_2GHZ_CAP) {
8513 		band = NL80211_BAND_2GHZ;
8514 		count = ath12k_mac_copy_sband_iftype_data(ar, cap,
8515 							  ar->mac.iftype[band],
8516 							  band);
8517 		sband = &ar->mac.sbands[band];
8518 		_ieee80211_set_sband_iftype_data(sband, ar->mac.iftype[band],
8519 						 count);
8520 	}
8521 
8522 	if (cap->supported_bands & WMI_HOST_WLAN_5GHZ_CAP) {
8523 		band = NL80211_BAND_5GHZ;
8524 		count = ath12k_mac_copy_sband_iftype_data(ar, cap,
8525 							  ar->mac.iftype[band],
8526 							  band);
8527 		sband = &ar->mac.sbands[band];
8528 		_ieee80211_set_sband_iftype_data(sband, ar->mac.iftype[band],
8529 						 count);
8530 	}
8531 
8532 	if (cap->supported_bands & WMI_HOST_WLAN_5GHZ_CAP &&
8533 	    ar->supports_6ghz) {
8534 		band = NL80211_BAND_6GHZ;
8535 		count = ath12k_mac_copy_sband_iftype_data(ar, cap,
8536 							  ar->mac.iftype[band],
8537 							  band);
8538 		sband = &ar->mac.sbands[band];
8539 		_ieee80211_set_sband_iftype_data(sband, ar->mac.iftype[band],
8540 						 count);
8541 	}
8542 }
8543 
8544 static int __ath12k_set_antenna(struct ath12k *ar, u32 tx_ant, u32 rx_ant)
8545 {
8546 	struct ath12k_hw *ah = ath12k_ar_to_ah(ar);
8547 	int ret;
8548 
8549 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
8550 
8551 	if (ath12k_check_chain_mask(ar, tx_ant, true))
8552 		return -EINVAL;
8553 
8554 	if (ath12k_check_chain_mask(ar, rx_ant, false))
8555 		return -EINVAL;
8556 
8557 	/* Since we advertised the max cap of all radios combined during wiphy
8558 	 * registration, ensure we don't set the antenna config higher than the
8559 	 * limits
8560 	 */
8561 	tx_ant = min_t(u32, tx_ant, ar->pdev->cap.tx_chain_mask);
8562 	rx_ant = min_t(u32, rx_ant, ar->pdev->cap.rx_chain_mask);
8563 
8564 	ar->cfg_tx_chainmask = tx_ant;
8565 	ar->cfg_rx_chainmask = rx_ant;
8566 
8567 	if (ah->state != ATH12K_HW_STATE_ON &&
8568 	    ah->state != ATH12K_HW_STATE_RESTARTED)
8569 		return 0;
8570 
8571 	ret = ath12k_wmi_pdev_set_param(ar, WMI_PDEV_PARAM_TX_CHAIN_MASK,
8572 					tx_ant, ar->pdev->pdev_id);
8573 	if (ret) {
8574 		ath12k_warn(ar->ab, "failed to set tx-chainmask: %d, req 0x%x\n",
8575 			    ret, tx_ant);
8576 		return ret;
8577 	}
8578 
8579 	ar->num_tx_chains = hweight32(tx_ant);
8580 
8581 	ret = ath12k_wmi_pdev_set_param(ar, WMI_PDEV_PARAM_RX_CHAIN_MASK,
8582 					rx_ant, ar->pdev->pdev_id);
8583 	if (ret) {
8584 		ath12k_warn(ar->ab, "failed to set rx-chainmask: %d, req 0x%x\n",
8585 			    ret, rx_ant);
8586 		return ret;
8587 	}
8588 
8589 	ar->num_rx_chains = hweight32(rx_ant);
8590 
8591 	/* Reload HT/VHT/HE capability */
8592 	ath12k_mac_setup_ht_vht_cap(ar, &ar->pdev->cap, NULL);
8593 	ath12k_mac_setup_sband_iftype_data(ar, &ar->pdev->cap);
8594 
8595 	return 0;
8596 }
8597 
8598 static void ath12k_mgmt_over_wmi_tx_drop(struct ath12k *ar, struct sk_buff *skb)
8599 {
8600 	int num_mgmt;
8601 
8602 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
8603 
8604 	ieee80211_free_txskb(ath12k_ar_to_hw(ar), skb);
8605 
8606 	num_mgmt = atomic_dec_if_positive(&ar->num_pending_mgmt_tx);
8607 
8608 	if (num_mgmt < 0)
8609 		WARN_ON_ONCE(1);
8610 
8611 	if (!num_mgmt)
8612 		wake_up(&ar->txmgmt_empty_waitq);
8613 }
8614 
8615 static void ath12k_mac_tx_mgmt_free(struct ath12k *ar, int buf_id)
8616 {
8617 	struct sk_buff *msdu;
8618 	struct ieee80211_tx_info *info;
8619 
8620 	spin_lock_bh(&ar->txmgmt_idr_lock);
8621 	msdu = idr_remove(&ar->txmgmt_idr, buf_id);
8622 	spin_unlock_bh(&ar->txmgmt_idr_lock);
8623 
8624 	if (!msdu)
8625 		return;
8626 
8627 	dma_unmap_single(ar->ab->dev, ATH12K_SKB_CB(msdu)->paddr, msdu->len,
8628 			 DMA_TO_DEVICE);
8629 
8630 	info = IEEE80211_SKB_CB(msdu);
8631 	memset(&info->status, 0, sizeof(info->status));
8632 
8633 	ath12k_mgmt_over_wmi_tx_drop(ar, msdu);
8634 }
8635 
8636 int ath12k_mac_tx_mgmt_pending_free(int buf_id, void *skb, void *ctx)
8637 {
8638 	struct ath12k *ar = ctx;
8639 
8640 	ath12k_mac_tx_mgmt_free(ar, buf_id);
8641 
8642 	return 0;
8643 }
8644 
8645 static int ath12k_mac_vif_txmgmt_idr_remove(int buf_id, void *skb, void *ctx)
8646 {
8647 	struct ieee80211_vif *vif = ctx;
8648 	struct ath12k_skb_cb *skb_cb = ATH12K_SKB_CB(skb);
8649 	struct ath12k *ar = skb_cb->ar;
8650 
8651 	if (skb_cb->vif == vif)
8652 		ath12k_mac_tx_mgmt_free(ar, buf_id);
8653 
8654 	return 0;
8655 }
8656 
8657 static int ath12k_mac_mgmt_tx_wmi(struct ath12k *ar, struct ath12k_link_vif *arvif,
8658 				  struct sk_buff *skb)
8659 {
8660 	struct ath12k_base *ab = ar->ab;
8661 	struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
8662 	struct ath12k_skb_cb *skb_cb = ATH12K_SKB_CB(skb);
8663 	struct ieee80211_tx_info *info;
8664 	enum hal_encrypt_type enctype;
8665 	unsigned int mic_len;
8666 	dma_addr_t paddr;
8667 	int buf_id;
8668 	int ret;
8669 
8670 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
8671 
8672 	skb_cb->ar = ar;
8673 	spin_lock_bh(&ar->txmgmt_idr_lock);
8674 	buf_id = idr_alloc(&ar->txmgmt_idr, skb, 0,
8675 			   ATH12K_TX_MGMT_NUM_PENDING_MAX, GFP_ATOMIC);
8676 	spin_unlock_bh(&ar->txmgmt_idr_lock);
8677 	if (buf_id < 0)
8678 		return -ENOSPC;
8679 
8680 	info = IEEE80211_SKB_CB(skb);
8681 	if ((skb_cb->flags & ATH12K_SKB_CIPHER_SET) &&
8682 	    !(info->flags & IEEE80211_TX_CTL_HW_80211_ENCAP)) {
8683 		if ((ieee80211_is_action(hdr->frame_control) ||
8684 		     ieee80211_is_deauth(hdr->frame_control) ||
8685 		     ieee80211_is_disassoc(hdr->frame_control)) &&
8686 		     ieee80211_has_protected(hdr->frame_control)) {
8687 			enctype = ath12k_dp_tx_get_encrypt_type(skb_cb->cipher);
8688 			mic_len = ath12k_dp_rx_crypto_mic_len(ar, enctype);
8689 			skb_put(skb, mic_len);
8690 		}
8691 	}
8692 
8693 	paddr = dma_map_single(ab->dev, skb->data, skb->len, DMA_TO_DEVICE);
8694 	if (dma_mapping_error(ab->dev, paddr)) {
8695 		ath12k_warn(ab, "failed to DMA map mgmt Tx buffer\n");
8696 		ret = -EIO;
8697 		goto err_free_idr;
8698 	}
8699 
8700 	skb_cb->paddr = paddr;
8701 
8702 	ret = ath12k_wmi_mgmt_send(arvif, buf_id, skb);
8703 	if (ret) {
8704 		ath12k_warn(ar->ab, "failed to send mgmt frame: %d\n", ret);
8705 		goto err_unmap_buf;
8706 	}
8707 
8708 	return 0;
8709 
8710 err_unmap_buf:
8711 	dma_unmap_single(ab->dev, skb_cb->paddr,
8712 			 skb->len, DMA_TO_DEVICE);
8713 err_free_idr:
8714 	spin_lock_bh(&ar->txmgmt_idr_lock);
8715 	idr_remove(&ar->txmgmt_idr, buf_id);
8716 	spin_unlock_bh(&ar->txmgmt_idr_lock);
8717 
8718 	return ret;
8719 }
8720 
8721 static void ath12k_mgmt_over_wmi_tx_purge(struct ath12k *ar)
8722 {
8723 	struct sk_buff *skb;
8724 
8725 	while ((skb = skb_dequeue(&ar->wmi_mgmt_tx_queue)) != NULL)
8726 		ath12k_mgmt_over_wmi_tx_drop(ar, skb);
8727 }
8728 
8729 static int ath12k_mac_mgmt_action_frame_fill_elem_data(struct ath12k_link_vif *arvif,
8730 						       struct sk_buff *skb)
8731 {
8732 	struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
8733 	u8 category, *buf, iv_len, action_code, dialog_token;
8734 	struct ieee80211_bss_conf *link_conf;
8735 	struct ieee80211_chanctx_conf *conf;
8736 	int cur_tx_power, max_tx_power;
8737 	struct ath12k *ar = arvif->ar;
8738 	struct ieee80211_hw *hw = ath12k_ar_to_hw(ar);
8739 	struct wiphy *wiphy = hw->wiphy;
8740 	struct ath12k_skb_cb *skb_cb;
8741 	struct ieee80211_mgmt *mgmt;
8742 	unsigned int remaining_len;
8743 	bool has_protected;
8744 
8745 	lockdep_assert_wiphy(wiphy);
8746 
8747 	/* make sure category field is present */
8748 	if (skb->len < IEEE80211_MIN_ACTION_SIZE)
8749 		return -EINVAL;
8750 
8751 	remaining_len = skb->len - IEEE80211_MIN_ACTION_SIZE;
8752 	has_protected = ieee80211_has_protected(hdr->frame_control);
8753 
8754 	/* In case of SW crypto and hdr protected (PMF), packet will already be encrypted,
8755 	 * we can't put in data in this case
8756 	 */
8757 	if (test_bit(ATH12K_FLAG_HW_CRYPTO_DISABLED, &ar->ab->dev_flags) &&
8758 	    has_protected)
8759 		return 0;
8760 
8761 	mgmt = (struct ieee80211_mgmt *)hdr;
8762 	buf = (u8 *)&mgmt->u.action;
8763 
8764 	/* FCTL_PROTECTED frame might have extra space added for HDR_LEN. Offset that
8765 	 * many bytes if it is there
8766 	 */
8767 	if (has_protected) {
8768 		skb_cb = ATH12K_SKB_CB(skb);
8769 
8770 		switch (skb_cb->cipher) {
8771 		/* Cipher suite having flag %IEEE80211_KEY_FLAG_GENERATE_IV_MGMT set in
8772 		 * key needs to be processed. See ath12k_install_key()
8773 		 */
8774 		case WLAN_CIPHER_SUITE_CCMP:
8775 		case WLAN_CIPHER_SUITE_CCMP_256:
8776 		case WLAN_CIPHER_SUITE_GCMP:
8777 		case WLAN_CIPHER_SUITE_GCMP_256:
8778 			iv_len = IEEE80211_CCMP_HDR_LEN;
8779 			break;
8780 		case WLAN_CIPHER_SUITE_TKIP:
8781 			iv_len = 0;
8782 			break;
8783 		default:
8784 			return -EINVAL;
8785 		}
8786 
8787 		if (remaining_len < iv_len)
8788 			return -EINVAL;
8789 
8790 		buf += iv_len;
8791 		remaining_len -= iv_len;
8792 	}
8793 
8794 	category = *buf++;
8795 	/* category code is already taken care in %IEEE80211_MIN_ACTION_SIZE hence
8796 	 * no need to adjust remaining_len
8797 	 */
8798 
8799 	switch (category) {
8800 	case WLAN_CATEGORY_RADIO_MEASUREMENT:
8801 		/* need action code and dialog token */
8802 		if (remaining_len < 2)
8803 			return -EINVAL;
8804 
8805 		/* Packet Format:
8806 		 *	Action Code | Dialog Token | Variable Len (based on Action Code)
8807 		 */
8808 		action_code = *buf++;
8809 		dialog_token = *buf++;
8810 		remaining_len -= 2;
8811 
8812 		link_conf = ath12k_mac_get_link_bss_conf(arvif);
8813 		if (!link_conf) {
8814 			ath12k_warn(ar->ab,
8815 				    "failed to get bss link conf for vdev %d in RM handling\n",
8816 				    arvif->vdev_id);
8817 			return -EINVAL;
8818 		}
8819 
8820 		conf = wiphy_dereference(wiphy, link_conf->chanctx_conf);
8821 		if (!conf)
8822 			return -ENOENT;
8823 
8824 		cur_tx_power = link_conf->txpower;
8825 		max_tx_power = min(conf->def.chan->max_reg_power,
8826 				   (int)ar->max_tx_power / 2);
8827 
8828 		ath12k_mac_op_get_txpower(hw, arvif->ahvif->vif, arvif->link_id,
8829 					  &cur_tx_power);
8830 
8831 		switch (action_code) {
8832 		case WLAN_RM_ACTION_LINK_MEASUREMENT_REQUEST:
8833 			/* need variable fields to be present in len */
8834 			if (remaining_len < 2)
8835 				return -EINVAL;
8836 
8837 			/* Variable length format as defined in IEEE 802.11-2024,
8838 			 * Figure 9-1187-Link Measurement Request frame Action field
8839 			 * format.
8840 			 *	Transmit Power | Max Tx Power
8841 			 * We fill both of these.
8842 			 */
8843 			*buf++ = cur_tx_power;
8844 			*buf = max_tx_power;
8845 
8846 			ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
8847 				   "RRM: Link Measurement Req dialog_token %u cur_tx_power %d max_tx_power %d\n",
8848 				   dialog_token, cur_tx_power, max_tx_power);
8849 			break;
8850 		case WLAN_RM_ACTION_LINK_MEASUREMENT_REPORT:
8851 			/* need variable fields to be present in len */
8852 			if (remaining_len < 3)
8853 				return -EINVAL;
8854 
8855 			/* Variable length format as defined in IEEE 802.11-2024,
8856 			 * Figure 9-1188-Link Measurement Report frame Action field format
8857 			 *	TPC Report | Variable Fields
8858 			 *
8859 			 * TPC Report Format:
8860 			 *	Element ID | Len | Tx Power | Link Margin
8861 			 *
8862 			 * We fill Tx power in the TPC Report (2nd index)
8863 			 */
8864 			buf[2] = cur_tx_power;
8865 
8866 			/* TODO: At present, Link margin data is not present so can't
8867 			 * really fill it now. Once it is available, it can be added
8868 			 * here
8869 			 */
8870 			ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
8871 				   "RRM: Link Measurement Report dialog_token %u cur_tx_power %d\n",
8872 				   dialog_token, cur_tx_power);
8873 			break;
8874 		default:
8875 			return -EINVAL;
8876 		}
8877 		break;
8878 	default:
8879 		/* nothing to fill */
8880 		return 0;
8881 	}
8882 
8883 	return 0;
8884 }
8885 
8886 static int ath12k_mac_mgmt_frame_fill_elem_data(struct ath12k_link_vif *arvif,
8887 						struct sk_buff *skb)
8888 {
8889 	struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
8890 
8891 	if (!ieee80211_is_action(hdr->frame_control))
8892 		return 0;
8893 
8894 	return ath12k_mac_mgmt_action_frame_fill_elem_data(arvif, skb);
8895 }
8896 
8897 static void ath12k_mgmt_over_wmi_tx_work(struct wiphy *wiphy, struct wiphy_work *work)
8898 {
8899 	struct ath12k *ar = container_of(work, struct ath12k, wmi_mgmt_tx_work);
8900 	struct ath12k_hw *ah = ar->ah;
8901 	struct ath12k_skb_cb *skb_cb;
8902 	struct ath12k_vif *ahvif;
8903 	struct ath12k_link_vif *arvif;
8904 	struct sk_buff *skb;
8905 	int ret;
8906 
8907 	lockdep_assert_wiphy(wiphy);
8908 
8909 	while ((skb = skb_dequeue(&ar->wmi_mgmt_tx_queue)) != NULL) {
8910 		skb_cb = ATH12K_SKB_CB(skb);
8911 		if (!skb_cb->vif) {
8912 			ath12k_warn(ar->ab, "no vif found for mgmt frame\n");
8913 			ath12k_mgmt_over_wmi_tx_drop(ar, skb);
8914 			continue;
8915 		}
8916 
8917 		ahvif = ath12k_vif_to_ahvif(skb_cb->vif);
8918 		if (!(ahvif->links_map & BIT(skb_cb->link_id))) {
8919 			ath12k_warn(ar->ab,
8920 				    "invalid linkid %u in mgmt over wmi tx with linkmap 0x%x\n",
8921 				    skb_cb->link_id, ahvif->links_map);
8922 			ath12k_mgmt_over_wmi_tx_drop(ar, skb);
8923 			continue;
8924 		}
8925 
8926 		arvif = wiphy_dereference(ah->hw->wiphy, ahvif->link[skb_cb->link_id]);
8927 		if (ar->allocated_vdev_map & (1LL << arvif->vdev_id)) {
8928 			/* Fill in the data which is required to be filled by the driver
8929 			 * For example: Max Tx power in Link Measurement Request/Report
8930 			 */
8931 			ret = ath12k_mac_mgmt_frame_fill_elem_data(arvif, skb);
8932 			if (ret) {
8933 				/* If we couldn't fill the data due to any reason,
8934 				 * let's not discard transmitting the packet.
8935 				 * For example: Software crypto and PMF case
8936 				 */
8937 				ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
8938 					   "Failed to fill the required data for the mgmt packet err %d\n",
8939 					   ret);
8940 			}
8941 
8942 			ret = ath12k_mac_mgmt_tx_wmi(ar, arvif, skb);
8943 			if (ret) {
8944 				ath12k_warn(ar->ab, "failed to tx mgmt frame, vdev_id %d :%d\n",
8945 					    arvif->vdev_id, ret);
8946 				ath12k_mgmt_over_wmi_tx_drop(ar, skb);
8947 			}
8948 		} else {
8949 			ath12k_warn(ar->ab,
8950 				    "dropping mgmt frame for vdev %d link %u is_started %d\n",
8951 				    arvif->vdev_id,
8952 				    skb_cb->link_id,
8953 				    arvif->is_started);
8954 			ath12k_mgmt_over_wmi_tx_drop(ar, skb);
8955 		}
8956 	}
8957 }
8958 
8959 static int ath12k_mac_mgmt_tx(struct ath12k *ar, struct sk_buff *skb,
8960 			      bool is_prb_rsp)
8961 {
8962 	struct sk_buff_head *q = &ar->wmi_mgmt_tx_queue;
8963 
8964 	if (test_bit(ATH12K_FLAG_CRASH_FLUSH, &ar->ab->dev_flags))
8965 		return -ESHUTDOWN;
8966 
8967 	/* Drop probe response packets when the pending management tx
8968 	 * count has reached a certain threshold, so as to prioritize
8969 	 * other mgmt packets like auth and assoc to be sent on time
8970 	 * for establishing successful connections.
8971 	 */
8972 	if (is_prb_rsp &&
8973 	    atomic_read(&ar->num_pending_mgmt_tx) > ATH12K_PRB_RSP_DROP_THRESHOLD) {
8974 		ath12k_warn(ar->ab,
8975 			    "dropping probe response as pending queue is almost full\n");
8976 		return -ENOSPC;
8977 	}
8978 
8979 	if (skb_queue_len_lockless(q) >= ATH12K_TX_MGMT_NUM_PENDING_MAX) {
8980 		ath12k_warn(ar->ab, "mgmt tx queue is full\n");
8981 		return -ENOSPC;
8982 	}
8983 
8984 	skb_queue_tail(q, skb);
8985 	atomic_inc(&ar->num_pending_mgmt_tx);
8986 	wiphy_work_queue(ath12k_ar_to_hw(ar)->wiphy, &ar->wmi_mgmt_tx_work);
8987 
8988 	return 0;
8989 }
8990 
8991 static void ath12k_mac_add_p2p_noa_ie(struct ath12k *ar,
8992 				      struct ieee80211_vif *vif,
8993 				      struct sk_buff *skb,
8994 				      bool is_prb_rsp)
8995 {
8996 	struct ath12k_vif *ahvif = ath12k_vif_to_ahvif(vif);
8997 
8998 	if (likely(!is_prb_rsp))
8999 		return;
9000 
9001 	spin_lock_bh(&ar->data_lock);
9002 
9003 	if (ahvif->u.ap.noa_data &&
9004 	    !pskb_expand_head(skb, 0, ahvif->u.ap.noa_len,
9005 			      GFP_ATOMIC))
9006 		skb_put_data(skb, ahvif->u.ap.noa_data,
9007 			     ahvif->u.ap.noa_len);
9008 
9009 	spin_unlock_bh(&ar->data_lock);
9010 }
9011 
9012 /* Note: called under rcu_read_lock() */
9013 static void ath12k_mlo_mcast_update_tx_link_address(struct ieee80211_vif *vif,
9014 						    u8 link_id, struct sk_buff *skb,
9015 						    u32 info_flags)
9016 {
9017 	struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
9018 	struct ieee80211_bss_conf *bss_conf;
9019 
9020 	if (info_flags & IEEE80211_TX_CTL_HW_80211_ENCAP)
9021 		return;
9022 
9023 	bss_conf = rcu_dereference(vif->link_conf[link_id]);
9024 	if (bss_conf)
9025 		ether_addr_copy(hdr->addr2, bss_conf->addr);
9026 }
9027 
9028 /* Note: called under rcu_read_lock() */
9029 static u8 ath12k_mac_get_tx_link(struct ieee80211_sta *sta, struct ieee80211_vif *vif,
9030 				 u8 link, struct sk_buff *skb, u32 info_flags)
9031 {
9032 	struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
9033 	struct ath12k_vif *ahvif = ath12k_vif_to_ahvif(vif);
9034 	struct ieee80211_link_sta *link_sta;
9035 	struct ieee80211_bss_conf *bss_conf;
9036 	struct ath12k_sta *ahsta;
9037 
9038 	/* Use the link id passed or the default vif link */
9039 	if (!sta) {
9040 		if (link != IEEE80211_LINK_UNSPECIFIED)
9041 			return link;
9042 
9043 		return ahvif->deflink.link_id;
9044 	}
9045 
9046 	ahsta = ath12k_sta_to_ahsta(sta);
9047 
9048 	/* Below translation ensures we pass proper A2 & A3 for non ML clients.
9049 	 * Also it assumes for now support only for MLO AP in this path
9050 	 */
9051 	if (!sta->mlo) {
9052 		link = ahsta->deflink.link_id;
9053 
9054 		if (info_flags & IEEE80211_TX_CTL_HW_80211_ENCAP)
9055 			return link;
9056 
9057 		bss_conf = rcu_dereference(vif->link_conf[link]);
9058 		if (bss_conf) {
9059 			ether_addr_copy(hdr->addr2, bss_conf->addr);
9060 			if (!ieee80211_has_tods(hdr->frame_control) &&
9061 			    !ieee80211_has_fromds(hdr->frame_control))
9062 				ether_addr_copy(hdr->addr3, bss_conf->addr);
9063 		}
9064 
9065 		return link;
9066 	}
9067 
9068 	/* enqueue eth enacap & data frames on primary link, FW does link
9069 	 * selection and address translation.
9070 	 */
9071 	if (info_flags & IEEE80211_TX_CTL_HW_80211_ENCAP ||
9072 	    ieee80211_is_data(hdr->frame_control))
9073 		return ahsta->assoc_link_id;
9074 
9075 	/* 802.11 frame cases */
9076 	if (link == IEEE80211_LINK_UNSPECIFIED)
9077 		link = ahsta->deflink.link_id;
9078 
9079 	if (!ieee80211_is_mgmt(hdr->frame_control))
9080 		return link;
9081 
9082 	/* Perform address conversion for ML STA Tx */
9083 	bss_conf = rcu_dereference(vif->link_conf[link]);
9084 	link_sta = rcu_dereference(sta->link[link]);
9085 
9086 	if (bss_conf && link_sta) {
9087 		ether_addr_copy(hdr->addr1, link_sta->addr);
9088 		ether_addr_copy(hdr->addr2, bss_conf->addr);
9089 
9090 		if (vif->type == NL80211_IFTYPE_STATION && bss_conf->bssid)
9091 			ether_addr_copy(hdr->addr3, bss_conf->bssid);
9092 		else if (vif->type == NL80211_IFTYPE_AP)
9093 			ether_addr_copy(hdr->addr3, bss_conf->addr);
9094 
9095 		return link;
9096 	}
9097 
9098 	if (bss_conf) {
9099 		/* In certain cases where a ML sta associated and added subset of
9100 		 * links on which the ML AP is active, but now sends some frame
9101 		 * (ex. Probe request) on a different link which is active in our
9102 		 * MLD but was not added during previous association, we can
9103 		 * still honor the Tx to that ML STA via the requested link.
9104 		 * The control would reach here in such case only when that link
9105 		 * address is same as the MLD address or in worst case clients
9106 		 * used MLD address at TA wrongly which would have helped
9107 		 * identify the ML sta object and pass it here.
9108 		 * If the link address of that STA is different from MLD address,
9109 		 * then the sta object would be NULL and control won't reach
9110 		 * here but return at the start of the function itself with !sta
9111 		 * check. Also this would not need any translation at hdr->addr1
9112 		 * from MLD to link address since the RA is the MLD address
9113 		 * (same as that link address ideally) already.
9114 		 */
9115 		ether_addr_copy(hdr->addr2, bss_conf->addr);
9116 
9117 		if (vif->type == NL80211_IFTYPE_STATION && bss_conf->bssid)
9118 			ether_addr_copy(hdr->addr3, bss_conf->bssid);
9119 		else if (vif->type == NL80211_IFTYPE_AP)
9120 			ether_addr_copy(hdr->addr3, bss_conf->addr);
9121 	}
9122 
9123 	return link;
9124 }
9125 
9126 /* Note: called under rcu_read_lock() */
9127 static void ath12k_mac_op_tx(struct ieee80211_hw *hw,
9128 			     struct ieee80211_tx_control *control,
9129 			     struct sk_buff *skb)
9130 {
9131 	struct ath12k_skb_cb *skb_cb = ATH12K_SKB_CB(skb);
9132 	struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
9133 	struct ieee80211_vif *vif = info->control.vif;
9134 	struct ath12k_vif *ahvif = ath12k_vif_to_ahvif(vif);
9135 	struct ath12k_link_vif *arvif = &ahvif->deflink;
9136 	struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
9137 	struct ieee80211_key_conf *key = info->control.hw_key;
9138 	struct ieee80211_sta *sta = control->sta;
9139 	struct ath12k_link_vif *tmp_arvif;
9140 	u32 info_flags = info->flags;
9141 	struct sk_buff *msdu_copied;
9142 	struct ath12k *ar, *tmp_ar;
9143 	struct ath12k_peer *peer;
9144 	unsigned long links_map;
9145 	bool is_mcast = false;
9146 	bool is_dvlan = false;
9147 	struct ethhdr *eth;
9148 	bool is_prb_rsp;
9149 	u16 mcbc_gsn;
9150 	u8 link_id;
9151 	int ret;
9152 
9153 	if (ahvif->vdev_type == WMI_VDEV_TYPE_MONITOR) {
9154 		ieee80211_free_txskb(hw, skb);
9155 		return;
9156 	}
9157 
9158 	link_id = u32_get_bits(info->control.flags, IEEE80211_TX_CTRL_MLO_LINK);
9159 	memset(skb_cb, 0, sizeof(*skb_cb));
9160 	skb_cb->vif = vif;
9161 
9162 	if (key) {
9163 		skb_cb->cipher = key->cipher;
9164 		skb_cb->flags |= ATH12K_SKB_CIPHER_SET;
9165 	}
9166 
9167 	/* handle only for MLO case, use deflink for non MLO case */
9168 	if (ieee80211_vif_is_mld(vif)) {
9169 		link_id = ath12k_mac_get_tx_link(sta, vif, link_id, skb, info_flags);
9170 		if (link_id >= IEEE80211_MLD_MAX_NUM_LINKS) {
9171 			ieee80211_free_txskb(hw, skb);
9172 			return;
9173 		}
9174 	} else {
9175 		link_id = 0;
9176 	}
9177 
9178 	arvif = rcu_dereference(ahvif->link[link_id]);
9179 	if (!arvif || !arvif->ar) {
9180 		ath12k_warn(ahvif->ah, "failed to find arvif link id %u for frame transmission",
9181 			    link_id);
9182 		ieee80211_free_txskb(hw, skb);
9183 		return;
9184 	}
9185 
9186 	ar = arvif->ar;
9187 	skb_cb->link_id = link_id;
9188 	is_prb_rsp = ieee80211_is_probe_resp(hdr->frame_control);
9189 
9190 	if (info_flags & IEEE80211_TX_CTL_HW_80211_ENCAP) {
9191 		eth = (struct ethhdr *)skb->data;
9192 		is_mcast = is_multicast_ether_addr(eth->h_dest);
9193 
9194 		skb_cb->flags |= ATH12K_SKB_HW_80211_ENCAP;
9195 	} else if (ieee80211_is_mgmt(hdr->frame_control)) {
9196 		if (sta && sta->mlo)
9197 			skb_cb->flags |= ATH12K_SKB_MLO_STA;
9198 
9199 		ret = ath12k_mac_mgmt_tx(ar, skb, is_prb_rsp);
9200 		if (ret) {
9201 			ath12k_warn(ar->ab, "failed to queue management frame %d\n",
9202 				    ret);
9203 			ieee80211_free_txskb(hw, skb);
9204 		}
9205 		return;
9206 	}
9207 
9208 	if (!(info_flags & IEEE80211_TX_CTL_HW_80211_ENCAP))
9209 		is_mcast = is_multicast_ether_addr(hdr->addr1);
9210 
9211 	/* This is case only for P2P_GO */
9212 	if (vif->type == NL80211_IFTYPE_AP && vif->p2p)
9213 		ath12k_mac_add_p2p_noa_ie(ar, vif, skb, is_prb_rsp);
9214 
9215 	/* Checking if it is a DVLAN frame */
9216 	if (!test_bit(ATH12K_FLAG_HW_CRYPTO_DISABLED, &ar->ab->dev_flags) &&
9217 	    !(skb_cb->flags & ATH12K_SKB_HW_80211_ENCAP) &&
9218 	    !(skb_cb->flags & ATH12K_SKB_CIPHER_SET) &&
9219 	    ieee80211_has_protected(hdr->frame_control))
9220 		is_dvlan = true;
9221 
9222 	if (!vif->valid_links || !is_mcast || is_dvlan ||
9223 	    (skb_cb->flags & ATH12K_SKB_HW_80211_ENCAP) ||
9224 	    test_bit(ATH12K_FLAG_RAW_MODE, &ar->ab->dev_flags)) {
9225 		ret = ath12k_dp_tx(ar, arvif, skb, false, 0, is_mcast);
9226 		if (unlikely(ret)) {
9227 			ath12k_warn(ar->ab, "failed to transmit frame %d\n", ret);
9228 			ieee80211_free_txskb(ar->ah->hw, skb);
9229 			return;
9230 		}
9231 	} else {
9232 		mcbc_gsn = atomic_inc_return(&ahvif->mcbc_gsn) & 0xfff;
9233 
9234 		links_map = ahvif->links_map;
9235 		for_each_set_bit(link_id, &links_map,
9236 				 IEEE80211_MLD_MAX_NUM_LINKS) {
9237 			tmp_arvif = rcu_dereference(ahvif->link[link_id]);
9238 			if (!tmp_arvif || !tmp_arvif->is_up)
9239 				continue;
9240 
9241 			tmp_ar = tmp_arvif->ar;
9242 			msdu_copied = skb_copy(skb, GFP_ATOMIC);
9243 			if (!msdu_copied) {
9244 				ath12k_err(ar->ab,
9245 					   "skb copy failure link_id 0x%X vdevid 0x%X\n",
9246 					   link_id, tmp_arvif->vdev_id);
9247 				continue;
9248 			}
9249 
9250 			ath12k_mlo_mcast_update_tx_link_address(vif, link_id,
9251 								msdu_copied,
9252 								info_flags);
9253 
9254 			skb_cb = ATH12K_SKB_CB(msdu_copied);
9255 			skb_cb->link_id = link_id;
9256 
9257 			/* For open mode, skip peer find logic */
9258 			if (unlikely(!ahvif->key_cipher))
9259 				goto skip_peer_find;
9260 
9261 			spin_lock_bh(&tmp_ar->ab->base_lock);
9262 			peer = ath12k_peer_find_by_addr(tmp_ar->ab, tmp_arvif->bssid);
9263 			if (!peer) {
9264 				spin_unlock_bh(&tmp_ar->ab->base_lock);
9265 				ath12k_warn(tmp_ar->ab,
9266 					    "failed to find peer for vdev_id 0x%X addr %pM link_map 0x%X\n",
9267 					    tmp_arvif->vdev_id, tmp_arvif->bssid,
9268 					    ahvif->links_map);
9269 				dev_kfree_skb_any(msdu_copied);
9270 				continue;
9271 			}
9272 
9273 			key = peer->keys[peer->mcast_keyidx];
9274 			if (key) {
9275 				skb_cb->cipher = key->cipher;
9276 				skb_cb->flags |= ATH12K_SKB_CIPHER_SET;
9277 
9278 				hdr = (struct ieee80211_hdr *)msdu_copied->data;
9279 				if (!ieee80211_has_protected(hdr->frame_control))
9280 					hdr->frame_control |=
9281 						cpu_to_le16(IEEE80211_FCTL_PROTECTED);
9282 			}
9283 			spin_unlock_bh(&tmp_ar->ab->base_lock);
9284 
9285 skip_peer_find:
9286 			ret = ath12k_dp_tx(tmp_ar, tmp_arvif,
9287 					   msdu_copied, true, mcbc_gsn, is_mcast);
9288 			if (unlikely(ret)) {
9289 				if (ret == -ENOMEM) {
9290 					/* Drops are expected during heavy multicast
9291 					 * frame flood. Print with debug log
9292 					 * level to avoid lot of console prints
9293 					 */
9294 					ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
9295 						   "failed to transmit frame %d\n",
9296 						   ret);
9297 				} else {
9298 					ath12k_warn(ar->ab,
9299 						    "failed to transmit frame %d\n",
9300 						    ret);
9301 				}
9302 
9303 				dev_kfree_skb_any(msdu_copied);
9304 			}
9305 		}
9306 		ieee80211_free_txskb(ar->ah->hw, skb);
9307 	}
9308 }
9309 
9310 void ath12k_mac_drain_tx(struct ath12k *ar)
9311 {
9312 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
9313 
9314 	/* make sure rcu-protected mac80211 tx path itself is drained */
9315 	synchronize_net();
9316 
9317 	wiphy_work_cancel(ath12k_ar_to_hw(ar)->wiphy, &ar->wmi_mgmt_tx_work);
9318 	ath12k_mgmt_over_wmi_tx_purge(ar);
9319 }
9320 
9321 static int ath12k_mac_config_mon_status_default(struct ath12k *ar, bool enable)
9322 {
9323 	struct htt_rx_ring_tlv_filter tlv_filter = {};
9324 	struct ath12k_base *ab = ar->ab;
9325 	u32 ring_id, i;
9326 	int ret = 0;
9327 
9328 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
9329 
9330 	if (!ab->hw_params->rxdma1_enable)
9331 		return ret;
9332 
9333 	if (enable) {
9334 		tlv_filter = ath12k_mac_mon_status_filter_default;
9335 
9336 		if (ath12k_debugfs_rx_filter(ar))
9337 			tlv_filter.rx_filter = ath12k_debugfs_rx_filter(ar);
9338 	} else {
9339 		tlv_filter.rxmon_disable = true;
9340 	}
9341 
9342 	for (i = 0; i < ab->hw_params->num_rxdma_per_pdev; i++) {
9343 		ring_id = ar->dp.rxdma_mon_dst_ring[i].ring_id;
9344 		ret = ath12k_dp_tx_htt_rx_filter_setup(ab, ring_id,
9345 						       ar->dp.mac_id + i,
9346 						       HAL_RXDMA_MONITOR_DST,
9347 						       DP_RXDMA_REFILL_RING_SIZE,
9348 						       &tlv_filter);
9349 		if (ret) {
9350 			ath12k_err(ab,
9351 				   "failed to setup filter for monitor buf %d\n",
9352 				   ret);
9353 		}
9354 	}
9355 
9356 	return ret;
9357 }
9358 
9359 static int ath12k_mac_start(struct ath12k *ar)
9360 {
9361 	struct ath12k_hw *ah = ar->ah;
9362 	struct ath12k_base *ab = ar->ab;
9363 	struct ath12k_pdev *pdev = ar->pdev;
9364 	int ret;
9365 
9366 	lockdep_assert_held(&ah->hw_mutex);
9367 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
9368 
9369 	ret = ath12k_wmi_pdev_set_param(ar, WMI_PDEV_PARAM_PMF_QOS,
9370 					1, pdev->pdev_id);
9371 
9372 	if (ret) {
9373 		ath12k_err(ab, "failed to enable PMF QOS: %d\n", ret);
9374 		goto err;
9375 	}
9376 
9377 	ret = ath12k_wmi_pdev_set_param(ar, WMI_PDEV_PARAM_DYNAMIC_BW, 1,
9378 					pdev->pdev_id);
9379 	if (ret) {
9380 		ath12k_err(ab, "failed to enable dynamic bw: %d\n", ret);
9381 		goto err;
9382 	}
9383 
9384 	ret = ath12k_wmi_pdev_set_param(ar, WMI_PDEV_PARAM_ARP_AC_OVERRIDE,
9385 					0, pdev->pdev_id);
9386 	if (ret) {
9387 		ath12k_err(ab, "failed to set ac override for ARP: %d\n",
9388 			   ret);
9389 		goto err;
9390 	}
9391 
9392 	ret = ath12k_wmi_send_dfs_phyerr_offload_enable_cmd(ar, pdev->pdev_id);
9393 	if (ret) {
9394 		ath12k_err(ab, "failed to offload radar detection: %d\n",
9395 			   ret);
9396 		goto err;
9397 	}
9398 
9399 	ret = ath12k_dp_tx_htt_h2t_ppdu_stats_req(ar,
9400 						  HTT_PPDU_STATS_TAG_DEFAULT);
9401 	if (ret) {
9402 		ath12k_err(ab, "failed to req ppdu stats: %d\n", ret);
9403 		goto err;
9404 	}
9405 
9406 	ret = ath12k_wmi_pdev_set_param(ar, WMI_PDEV_PARAM_MESH_MCAST_ENABLE,
9407 					1, pdev->pdev_id);
9408 
9409 	if (ret) {
9410 		ath12k_err(ab, "failed to enable MESH MCAST ENABLE: (%d\n", ret);
9411 		goto err;
9412 	}
9413 
9414 	__ath12k_set_antenna(ar, ar->cfg_tx_chainmask, ar->cfg_rx_chainmask);
9415 
9416 	/* TODO: Do we need to enable ANI? */
9417 
9418 	ret = ath12k_reg_update_chan_list(ar, false);
9419 
9420 	/* The ar state alone can be turned off for non supported country
9421 	 * without returning the error value. As we need to update the channel
9422 	 * for the next ar.
9423 	 */
9424 	if (ret) {
9425 		if (ret == -EINVAL)
9426 			ret = 0;
9427 		goto err;
9428 	}
9429 
9430 	ar->num_started_vdevs = 0;
9431 	ar->num_created_vdevs = 0;
9432 	ar->num_peers = 0;
9433 	ar->allocated_vdev_map = 0;
9434 	ar->chan_tx_pwr = ATH12K_PDEV_TX_POWER_INVALID;
9435 
9436 	/* Configure monitor status ring with default rx_filter to get rx status
9437 	 * such as rssi, rx_duration.
9438 	 */
9439 	ret = ath12k_mac_config_mon_status_default(ar, true);
9440 	if (ret && (ret != -EOPNOTSUPP)) {
9441 		ath12k_err(ab, "failed to configure monitor status ring with default rx_filter: (%d)\n",
9442 			   ret);
9443 		goto err;
9444 	}
9445 
9446 	if (ret == -EOPNOTSUPP)
9447 		ath12k_dbg(ab, ATH12K_DBG_MAC,
9448 			   "monitor status config is not yet supported");
9449 
9450 	/* Configure the hash seed for hash based reo dest ring selection */
9451 	ath12k_wmi_pdev_lro_cfg(ar, ar->pdev->pdev_id);
9452 
9453 	/* allow device to enter IMPS */
9454 	if (ab->hw_params->idle_ps) {
9455 		ret = ath12k_wmi_pdev_set_param(ar, WMI_PDEV_PARAM_IDLE_PS_CONFIG,
9456 						1, pdev->pdev_id);
9457 		if (ret) {
9458 			ath12k_err(ab, "failed to enable idle ps: %d\n", ret);
9459 			goto err;
9460 		}
9461 	}
9462 
9463 	rcu_assign_pointer(ab->pdevs_active[ar->pdev_idx],
9464 			   &ab->pdevs[ar->pdev_idx]);
9465 
9466 	return 0;
9467 err:
9468 
9469 	return ret;
9470 }
9471 
9472 static void ath12k_drain_tx(struct ath12k_hw *ah)
9473 {
9474 	struct ath12k *ar;
9475 	int i;
9476 
9477 	lockdep_assert_wiphy(ah->hw->wiphy);
9478 
9479 	for_each_ar(ah, ar, i)
9480 		ath12k_mac_drain_tx(ar);
9481 }
9482 
9483 static int ath12k_mac_op_start(struct ieee80211_hw *hw)
9484 {
9485 	struct ath12k_hw *ah = ath12k_hw_to_ah(hw);
9486 	struct ath12k *ar;
9487 	int ret, i;
9488 
9489 	if (ath12k_ftm_mode)
9490 		return -EPERM;
9491 
9492 	lockdep_assert_wiphy(hw->wiphy);
9493 
9494 	ath12k_drain_tx(ah);
9495 
9496 	guard(mutex)(&ah->hw_mutex);
9497 
9498 	switch (ah->state) {
9499 	case ATH12K_HW_STATE_OFF:
9500 		ah->state = ATH12K_HW_STATE_ON;
9501 		break;
9502 	case ATH12K_HW_STATE_RESTARTING:
9503 		ah->state = ATH12K_HW_STATE_RESTARTED;
9504 		break;
9505 	case ATH12K_HW_STATE_RESTARTED:
9506 	case ATH12K_HW_STATE_WEDGED:
9507 	case ATH12K_HW_STATE_ON:
9508 	case ATH12K_HW_STATE_TM:
9509 		ah->state = ATH12K_HW_STATE_OFF;
9510 
9511 		WARN_ON(1);
9512 		return -EINVAL;
9513 	}
9514 
9515 	for_each_ar(ah, ar, i) {
9516 		ret = ath12k_mac_start(ar);
9517 		if (ret) {
9518 			ah->state = ATH12K_HW_STATE_OFF;
9519 
9520 			ath12k_err(ar->ab, "fail to start mac operations in pdev idx %d ret %d\n",
9521 				   ar->pdev_idx, ret);
9522 			goto fail_start;
9523 		}
9524 	}
9525 
9526 	return 0;
9527 
9528 fail_start:
9529 	for (; i > 0; i--) {
9530 		ar = ath12k_ah_to_ar(ah, i - 1);
9531 		ath12k_mac_stop(ar);
9532 	}
9533 
9534 	return ret;
9535 }
9536 
9537 int ath12k_mac_rfkill_config(struct ath12k *ar)
9538 {
9539 	struct ath12k_base *ab = ar->ab;
9540 	u32 param;
9541 	int ret;
9542 
9543 	if (ab->hw_params->rfkill_pin == 0)
9544 		return -EOPNOTSUPP;
9545 
9546 	ath12k_dbg(ab, ATH12K_DBG_MAC,
9547 		   "mac rfkill_pin %d rfkill_cfg %d rfkill_on_level %d",
9548 		   ab->hw_params->rfkill_pin, ab->hw_params->rfkill_cfg,
9549 		   ab->hw_params->rfkill_on_level);
9550 
9551 	param = u32_encode_bits(ab->hw_params->rfkill_on_level,
9552 				WMI_RFKILL_CFG_RADIO_LEVEL) |
9553 		u32_encode_bits(ab->hw_params->rfkill_pin,
9554 				WMI_RFKILL_CFG_GPIO_PIN_NUM) |
9555 		u32_encode_bits(ab->hw_params->rfkill_cfg,
9556 				WMI_RFKILL_CFG_PIN_AS_GPIO);
9557 
9558 	ret = ath12k_wmi_pdev_set_param(ar, WMI_PDEV_PARAM_HW_RFKILL_CONFIG,
9559 					param, ar->pdev->pdev_id);
9560 	if (ret) {
9561 		ath12k_warn(ab,
9562 			    "failed to set rfkill config 0x%x: %d\n",
9563 			    param, ret);
9564 		return ret;
9565 	}
9566 
9567 	return 0;
9568 }
9569 
9570 int ath12k_mac_rfkill_enable_radio(struct ath12k *ar, bool enable)
9571 {
9572 	enum wmi_rfkill_enable_radio param;
9573 	int ret;
9574 
9575 	if (enable)
9576 		param = WMI_RFKILL_ENABLE_RADIO_ON;
9577 	else
9578 		param = WMI_RFKILL_ENABLE_RADIO_OFF;
9579 
9580 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac %d rfkill enable %d",
9581 		   ar->pdev_idx, param);
9582 
9583 	ret = ath12k_wmi_pdev_set_param(ar, WMI_PDEV_PARAM_RFKILL_ENABLE,
9584 					param, ar->pdev->pdev_id);
9585 	if (ret) {
9586 		ath12k_warn(ar->ab, "failed to set rfkill enable param %d: %d\n",
9587 			    param, ret);
9588 		return ret;
9589 	}
9590 
9591 	return 0;
9592 }
9593 
9594 static void ath12k_mac_stop(struct ath12k *ar)
9595 {
9596 	struct ath12k_hw *ah = ar->ah;
9597 	struct htt_ppdu_stats_info *ppdu_stats, *tmp;
9598 	struct ath12k_wmi_scan_chan_list_arg *arg;
9599 	int ret;
9600 
9601 	lockdep_assert_held(&ah->hw_mutex);
9602 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
9603 
9604 	ret = ath12k_mac_config_mon_status_default(ar, false);
9605 	if (ret && (ret != -EOPNOTSUPP))
9606 		ath12k_err(ar->ab, "failed to clear rx_filter for monitor status ring: (%d)\n",
9607 			   ret);
9608 
9609 	clear_bit(ATH12K_FLAG_CAC_RUNNING, &ar->dev_flags);
9610 
9611 	cancel_delayed_work_sync(&ar->scan.timeout);
9612 	wiphy_work_cancel(ath12k_ar_to_hw(ar)->wiphy, &ar->scan.vdev_clean_wk);
9613 	cancel_work_sync(&ar->regd_channel_update_work);
9614 	cancel_work_sync(&ar->regd_update_work);
9615 	cancel_work_sync(&ar->ab->rfkill_work);
9616 	cancel_work_sync(&ar->ab->update_11d_work);
9617 	ar->state_11d = ATH12K_11D_IDLE;
9618 	complete(&ar->completed_11d_scan);
9619 
9620 	spin_lock_bh(&ar->data_lock);
9621 
9622 	list_for_each_entry_safe(ppdu_stats, tmp, &ar->ppdu_stats_info, list) {
9623 		list_del(&ppdu_stats->list);
9624 		kfree(ppdu_stats);
9625 	}
9626 
9627 	while ((arg = list_first_entry_or_null(&ar->regd_channel_update_queue,
9628 					       struct ath12k_wmi_scan_chan_list_arg,
9629 					       list))) {
9630 		list_del(&arg->list);
9631 		kfree(arg);
9632 	}
9633 	spin_unlock_bh(&ar->data_lock);
9634 
9635 	rcu_assign_pointer(ar->ab->pdevs_active[ar->pdev_idx], NULL);
9636 
9637 	synchronize_rcu();
9638 
9639 	atomic_set(&ar->num_pending_mgmt_tx, 0);
9640 }
9641 
9642 static void ath12k_mac_op_stop(struct ieee80211_hw *hw, bool suspend)
9643 {
9644 	struct ath12k_hw *ah = ath12k_hw_to_ah(hw);
9645 	struct ath12k *ar;
9646 	int i;
9647 
9648 	lockdep_assert_wiphy(hw->wiphy);
9649 
9650 	ath12k_drain_tx(ah);
9651 
9652 	mutex_lock(&ah->hw_mutex);
9653 
9654 	ah->state = ATH12K_HW_STATE_OFF;
9655 
9656 	for_each_ar(ah, ar, i)
9657 		ath12k_mac_stop(ar);
9658 
9659 	mutex_unlock(&ah->hw_mutex);
9660 }
9661 
9662 static u8
9663 ath12k_mac_get_vdev_stats_id(struct ath12k_link_vif *arvif)
9664 {
9665 	struct ath12k_base *ab = arvif->ar->ab;
9666 	u8 vdev_stats_id = 0;
9667 
9668 	do {
9669 		if (ab->free_vdev_stats_id_map & (1LL << vdev_stats_id)) {
9670 			vdev_stats_id++;
9671 			if (vdev_stats_id >= ATH12K_MAX_VDEV_STATS_ID) {
9672 				vdev_stats_id = ATH12K_INVAL_VDEV_STATS_ID;
9673 				break;
9674 			}
9675 		} else {
9676 			ab->free_vdev_stats_id_map |= (1LL << vdev_stats_id);
9677 			break;
9678 		}
9679 	} while (vdev_stats_id);
9680 
9681 	arvif->vdev_stats_id = vdev_stats_id;
9682 	return vdev_stats_id;
9683 }
9684 
9685 static int ath12k_mac_setup_vdev_params_mbssid(struct ath12k_link_vif *arvif,
9686 					       u32 *flags, u32 *tx_vdev_id)
9687 {
9688 	struct ath12k_vif *ahvif = arvif->ahvif;
9689 	struct ieee80211_bss_conf *link_conf;
9690 	struct ath12k *ar = arvif->ar;
9691 	struct ath12k_link_vif *tx_arvif;
9692 
9693 	link_conf = ath12k_mac_get_link_bss_conf(arvif);
9694 	if (!link_conf) {
9695 		ath12k_warn(ar->ab, "unable to access bss link conf in set mbssid params for vif %pM link %u\n",
9696 			    ahvif->vif->addr, arvif->link_id);
9697 		return -ENOLINK;
9698 	}
9699 
9700 	tx_arvif = ath12k_mac_get_tx_arvif(arvif, link_conf);
9701 	if (!tx_arvif)
9702 		return 0;
9703 
9704 	if (link_conf->nontransmitted) {
9705 		if (ath12k_ar_to_hw(ar)->wiphy !=
9706 		    ath12k_ar_to_hw(tx_arvif->ar)->wiphy)
9707 			return -EINVAL;
9708 
9709 		*flags = WMI_VDEV_MBSSID_FLAGS_NON_TRANSMIT_AP;
9710 		*tx_vdev_id = tx_arvif->vdev_id;
9711 	} else if (tx_arvif == arvif) {
9712 		*flags = WMI_VDEV_MBSSID_FLAGS_TRANSMIT_AP;
9713 	} else {
9714 		return -EINVAL;
9715 	}
9716 
9717 	if (link_conf->ema_ap)
9718 		*flags |= WMI_VDEV_MBSSID_FLAGS_EMA_MODE;
9719 
9720 	return 0;
9721 }
9722 
9723 static int ath12k_mac_setup_vdev_create_arg(struct ath12k_link_vif *arvif,
9724 					    struct ath12k_wmi_vdev_create_arg *arg)
9725 {
9726 	struct ath12k *ar = arvif->ar;
9727 	struct ath12k_pdev *pdev = ar->pdev;
9728 	struct ath12k_vif *ahvif = arvif->ahvif;
9729 	int ret;
9730 
9731 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
9732 
9733 	arg->if_id = arvif->vdev_id;
9734 	arg->type = ahvif->vdev_type;
9735 	arg->subtype = ahvif->vdev_subtype;
9736 	arg->pdev_id = pdev->pdev_id;
9737 
9738 	arg->mbssid_flags = WMI_VDEV_MBSSID_FLAGS_NON_MBSSID_AP;
9739 	arg->mbssid_tx_vdev_id = 0;
9740 	if (!test_bit(WMI_TLV_SERVICE_MBSS_PARAM_IN_VDEV_START_SUPPORT,
9741 		      ar->ab->wmi_ab.svc_map)) {
9742 		ret = ath12k_mac_setup_vdev_params_mbssid(arvif,
9743 							  &arg->mbssid_flags,
9744 							  &arg->mbssid_tx_vdev_id);
9745 		if (ret)
9746 			return ret;
9747 	}
9748 
9749 	if (pdev->cap.supported_bands & WMI_HOST_WLAN_2GHZ_CAP) {
9750 		arg->chains[NL80211_BAND_2GHZ].tx = ar->num_tx_chains;
9751 		arg->chains[NL80211_BAND_2GHZ].rx = ar->num_rx_chains;
9752 	}
9753 	if (pdev->cap.supported_bands & WMI_HOST_WLAN_5GHZ_CAP) {
9754 		arg->chains[NL80211_BAND_5GHZ].tx = ar->num_tx_chains;
9755 		arg->chains[NL80211_BAND_5GHZ].rx = ar->num_rx_chains;
9756 	}
9757 	if (pdev->cap.supported_bands & WMI_HOST_WLAN_5GHZ_CAP &&
9758 	    ar->supports_6ghz) {
9759 		arg->chains[NL80211_BAND_6GHZ].tx = ar->num_tx_chains;
9760 		arg->chains[NL80211_BAND_6GHZ].rx = ar->num_rx_chains;
9761 	}
9762 
9763 	arg->if_stats_id = ath12k_mac_get_vdev_stats_id(arvif);
9764 
9765 	if (ath12k_mac_is_ml_arvif(arvif)) {
9766 		if (hweight16(ahvif->vif->valid_links) > ATH12K_WMI_MLO_MAX_LINKS) {
9767 			ath12k_warn(ar->ab, "too many MLO links during setting up vdev: %d",
9768 				    ahvif->vif->valid_links);
9769 			return -EINVAL;
9770 		}
9771 
9772 		ether_addr_copy(arg->mld_addr, ahvif->vif->addr);
9773 	}
9774 
9775 	return 0;
9776 }
9777 
9778 static void ath12k_mac_update_vif_offload(struct ath12k_link_vif *arvif)
9779 {
9780 	struct ath12k_vif *ahvif = arvif->ahvif;
9781 	struct ieee80211_vif *vif = ath12k_ahvif_to_vif(ahvif);
9782 	struct ath12k *ar = arvif->ar;
9783 	struct ath12k_base *ab = ar->ab;
9784 	u32 param_id, param_value;
9785 	int ret;
9786 
9787 	param_id = WMI_VDEV_PARAM_TX_ENCAP_TYPE;
9788 	if (vif->type != NL80211_IFTYPE_STATION &&
9789 	    vif->type != NL80211_IFTYPE_AP)
9790 		vif->offload_flags &= ~(IEEE80211_OFFLOAD_ENCAP_ENABLED |
9791 					IEEE80211_OFFLOAD_DECAP_ENABLED);
9792 
9793 	if (vif->offload_flags & IEEE80211_OFFLOAD_ENCAP_ENABLED)
9794 		ahvif->tx_encap_type = ATH12K_HW_TXRX_ETHERNET;
9795 	else if (test_bit(ATH12K_FLAG_RAW_MODE, &ab->dev_flags))
9796 		ahvif->tx_encap_type = ATH12K_HW_TXRX_RAW;
9797 	else
9798 		ahvif->tx_encap_type = ATH12K_HW_TXRX_NATIVE_WIFI;
9799 
9800 	ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
9801 					    param_id, ahvif->tx_encap_type);
9802 	if (ret) {
9803 		ath12k_warn(ab, "failed to set vdev %d tx encap mode: %d\n",
9804 			    arvif->vdev_id, ret);
9805 		vif->offload_flags &= ~IEEE80211_OFFLOAD_ENCAP_ENABLED;
9806 	}
9807 
9808 	param_id = WMI_VDEV_PARAM_RX_DECAP_TYPE;
9809 	if (vif->offload_flags & IEEE80211_OFFLOAD_DECAP_ENABLED)
9810 		param_value = ATH12K_HW_TXRX_ETHERNET;
9811 	else if (test_bit(ATH12K_FLAG_RAW_MODE, &ab->dev_flags))
9812 		param_value = ATH12K_HW_TXRX_RAW;
9813 	else
9814 		param_value = ATH12K_HW_TXRX_NATIVE_WIFI;
9815 
9816 	ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
9817 					    param_id, param_value);
9818 	if (ret) {
9819 		ath12k_warn(ab, "failed to set vdev %d rx decap mode: %d\n",
9820 			    arvif->vdev_id, ret);
9821 		vif->offload_flags &= ~IEEE80211_OFFLOAD_DECAP_ENABLED;
9822 	}
9823 }
9824 
9825 static void ath12k_mac_op_update_vif_offload(struct ieee80211_hw *hw,
9826 					     struct ieee80211_vif *vif)
9827 {
9828 	struct ath12k_vif *ahvif = ath12k_vif_to_ahvif(vif);
9829 	struct ath12k_link_vif *arvif;
9830 	unsigned long links;
9831 	int link_id;
9832 
9833 	lockdep_assert_wiphy(hw->wiphy);
9834 
9835 	if (vif->valid_links) {
9836 		links = vif->valid_links;
9837 		for_each_set_bit(link_id, &links, IEEE80211_MLD_MAX_NUM_LINKS) {
9838 			arvif = wiphy_dereference(hw->wiphy, ahvif->link[link_id]);
9839 			if (!(arvif && arvif->ar))
9840 				continue;
9841 
9842 			ath12k_mac_update_vif_offload(arvif);
9843 		}
9844 
9845 		return;
9846 	}
9847 
9848 	ath12k_mac_update_vif_offload(&ahvif->deflink);
9849 }
9850 
9851 static bool ath12k_mac_vif_ap_active_any(struct ath12k_base *ab)
9852 {
9853 	struct ath12k *ar;
9854 	struct ath12k_pdev *pdev;
9855 	struct ath12k_link_vif *arvif;
9856 	int i;
9857 
9858 	for (i = 0; i < ab->num_radios; i++) {
9859 		pdev = &ab->pdevs[i];
9860 		ar = pdev->ar;
9861 		list_for_each_entry(arvif, &ar->arvifs, list) {
9862 			if (arvif->is_up &&
9863 			    arvif->ahvif->vdev_type == WMI_VDEV_TYPE_AP)
9864 				return true;
9865 		}
9866 	}
9867 	return false;
9868 }
9869 
9870 void ath12k_mac_11d_scan_start(struct ath12k *ar, u32 vdev_id)
9871 {
9872 	struct wmi_11d_scan_start_arg arg;
9873 	int ret;
9874 
9875 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
9876 
9877 	if (ar->regdom_set_by_user)
9878 		goto fin;
9879 
9880 	if (ar->vdev_id_11d_scan != ATH12K_11D_INVALID_VDEV_ID)
9881 		goto fin;
9882 
9883 	if (!test_bit(WMI_TLV_SERVICE_11D_OFFLOAD, ar->ab->wmi_ab.svc_map))
9884 		goto fin;
9885 
9886 	if (ath12k_mac_vif_ap_active_any(ar->ab))
9887 		goto fin;
9888 
9889 	arg.vdev_id = vdev_id;
9890 	arg.start_interval_msec = 0;
9891 	arg.scan_period_msec = ATH12K_SCAN_11D_INTERVAL;
9892 
9893 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
9894 		   "mac start 11d scan for vdev %d\n", vdev_id);
9895 
9896 	ret = ath12k_wmi_send_11d_scan_start_cmd(ar, &arg);
9897 	if (ret) {
9898 		ath12k_warn(ar->ab, "failed to start 11d scan vdev %d ret: %d\n",
9899 			    vdev_id, ret);
9900 	} else {
9901 		ar->vdev_id_11d_scan = vdev_id;
9902 		if (ar->state_11d == ATH12K_11D_PREPARING)
9903 			ar->state_11d = ATH12K_11D_RUNNING;
9904 	}
9905 
9906 fin:
9907 	if (ar->state_11d == ATH12K_11D_PREPARING) {
9908 		ar->state_11d = ATH12K_11D_IDLE;
9909 		complete(&ar->completed_11d_scan);
9910 	}
9911 }
9912 
9913 void ath12k_mac_11d_scan_stop(struct ath12k *ar)
9914 {
9915 	int ret;
9916 	u32 vdev_id;
9917 
9918 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
9919 
9920 	if (!test_bit(WMI_TLV_SERVICE_11D_OFFLOAD, ar->ab->wmi_ab.svc_map))
9921 		return;
9922 
9923 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac stop 11d for vdev %d\n",
9924 		   ar->vdev_id_11d_scan);
9925 
9926 	if (ar->state_11d == ATH12K_11D_PREPARING) {
9927 		ar->state_11d = ATH12K_11D_IDLE;
9928 		complete(&ar->completed_11d_scan);
9929 	}
9930 
9931 	if (ar->vdev_id_11d_scan != ATH12K_11D_INVALID_VDEV_ID) {
9932 		vdev_id = ar->vdev_id_11d_scan;
9933 
9934 		ret = ath12k_wmi_send_11d_scan_stop_cmd(ar, vdev_id);
9935 		if (ret) {
9936 			ath12k_warn(ar->ab,
9937 				    "failed to stopt 11d scan vdev %d ret: %d\n",
9938 				    vdev_id, ret);
9939 		} else {
9940 			ar->vdev_id_11d_scan = ATH12K_11D_INVALID_VDEV_ID;
9941 			ar->state_11d = ATH12K_11D_IDLE;
9942 			complete(&ar->completed_11d_scan);
9943 		}
9944 	}
9945 }
9946 
9947 void ath12k_mac_11d_scan_stop_all(struct ath12k_base *ab)
9948 {
9949 	struct ath12k *ar;
9950 	struct ath12k_pdev *pdev;
9951 	int i;
9952 
9953 	ath12k_dbg(ab, ATH12K_DBG_MAC, "mac stop soc 11d scan\n");
9954 
9955 	for (i = 0; i < ab->num_radios; i++) {
9956 		pdev = &ab->pdevs[i];
9957 		ar = pdev->ar;
9958 
9959 		ath12k_mac_11d_scan_stop(ar);
9960 	}
9961 }
9962 
9963 static void ath12k_mac_determine_vdev_type(struct ieee80211_vif *vif,
9964 					   struct ath12k_vif *ahvif)
9965 {
9966 	ahvif->vdev_subtype = WMI_VDEV_SUBTYPE_NONE;
9967 
9968 	switch (vif->type) {
9969 	case NL80211_IFTYPE_UNSPECIFIED:
9970 	case NL80211_IFTYPE_STATION:
9971 		ahvif->vdev_type = WMI_VDEV_TYPE_STA;
9972 
9973 		if (vif->p2p)
9974 			ahvif->vdev_subtype = WMI_VDEV_SUBTYPE_P2P_CLIENT;
9975 
9976 		break;
9977 	case NL80211_IFTYPE_MESH_POINT:
9978 		ahvif->vdev_subtype = WMI_VDEV_SUBTYPE_MESH_11S;
9979 		fallthrough;
9980 	case NL80211_IFTYPE_AP:
9981 		ahvif->vdev_type = WMI_VDEV_TYPE_AP;
9982 
9983 		if (vif->p2p)
9984 			ahvif->vdev_subtype = WMI_VDEV_SUBTYPE_P2P_GO;
9985 
9986 		break;
9987 	case NL80211_IFTYPE_MONITOR:
9988 		ahvif->vdev_type = WMI_VDEV_TYPE_MONITOR;
9989 		break;
9990 	case NL80211_IFTYPE_P2P_DEVICE:
9991 		ahvif->vdev_type = WMI_VDEV_TYPE_STA;
9992 		ahvif->vdev_subtype = WMI_VDEV_SUBTYPE_P2P_DEVICE;
9993 		break;
9994 	default:
9995 		WARN_ON(1);
9996 		break;
9997 	}
9998 }
9999 
10000 int ath12k_mac_vdev_create(struct ath12k *ar, struct ath12k_link_vif *arvif)
10001 {
10002 	struct ath12k_hw *ah = ar->ah;
10003 	struct ath12k_base *ab = ar->ab;
10004 	struct ieee80211_hw *hw = ah->hw;
10005 	struct ath12k_vif *ahvif = arvif->ahvif;
10006 	struct ieee80211_vif *vif = ath12k_ahvif_to_vif(ahvif);
10007 	struct ath12k_wmi_vdev_create_arg vdev_arg = {};
10008 	struct ath12k_wmi_peer_create_arg peer_param = {};
10009 	struct ieee80211_bss_conf *link_conf = NULL;
10010 	u32 param_id, param_value;
10011 	u16 nss;
10012 	int i;
10013 	int ret, vdev_id;
10014 	u8 link_id;
10015 
10016 	lockdep_assert_wiphy(hw->wiphy);
10017 
10018 	/* In NO_VIRTUAL_MONITOR, its necessary to restrict only one monitor
10019 	 * interface in each radio
10020 	 */
10021 	if (vif->type == NL80211_IFTYPE_MONITOR && ar->monitor_vdev_created)
10022 		return -EINVAL;
10023 
10024 	if (ar->num_created_vdevs >= TARGET_NUM_VDEVS(ab)) {
10025 		ath12k_warn(ab, "failed to create vdev, reached max vdev limit %d\n",
10026 			    TARGET_NUM_VDEVS(ab));
10027 		return -ENOSPC;
10028 	}
10029 
10030 	link_id = arvif->link_id;
10031 
10032 	if (link_id < IEEE80211_MLD_MAX_NUM_LINKS) {
10033 		link_conf = wiphy_dereference(hw->wiphy, vif->link_conf[link_id]);
10034 		if (!link_conf) {
10035 			ath12k_warn(ar->ab, "unable to access bss link conf in vdev create for vif %pM link %u\n",
10036 				    vif->addr, arvif->link_id);
10037 			return -ENOLINK;
10038 		}
10039 	}
10040 
10041 	if (link_conf)
10042 		memcpy(arvif->bssid, link_conf->addr, ETH_ALEN);
10043 	else
10044 		memcpy(arvif->bssid, vif->addr, ETH_ALEN);
10045 
10046 	arvif->ar = ar;
10047 	vdev_id = __ffs64(ab->free_vdev_map);
10048 	arvif->vdev_id = vdev_id;
10049 	if (vif->type == NL80211_IFTYPE_MONITOR)
10050 		ar->monitor_vdev_id = vdev_id;
10051 
10052 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac vdev create id %d type %d subtype %d map %llx\n",
10053 		   arvif->vdev_id, ahvif->vdev_type, ahvif->vdev_subtype,
10054 		   ab->free_vdev_map);
10055 
10056 	vif->cab_queue = arvif->vdev_id % (ATH12K_HW_MAX_QUEUES - 1);
10057 	for (i = 0; i < ARRAY_SIZE(vif->hw_queue); i++)
10058 		vif->hw_queue[i] = i % (ATH12K_HW_MAX_QUEUES - 1);
10059 
10060 	ret = ath12k_mac_setup_vdev_create_arg(arvif, &vdev_arg);
10061 	if (ret) {
10062 		ath12k_warn(ab, "failed to create vdev parameters %d: %d\n",
10063 			    arvif->vdev_id, ret);
10064 		goto err;
10065 	}
10066 
10067 	ret = ath12k_wmi_vdev_create(ar, arvif->bssid, &vdev_arg);
10068 	if (ret) {
10069 		ath12k_warn(ab, "failed to create WMI vdev %d: %d\n",
10070 			    arvif->vdev_id, ret);
10071 		return ret;
10072 	}
10073 
10074 	ar->num_created_vdevs++;
10075 	arvif->is_created = true;
10076 	ath12k_dbg(ab, ATH12K_DBG_MAC, "vdev %pM created, vdev_id %d\n",
10077 		   vif->addr, arvif->vdev_id);
10078 	ar->allocated_vdev_map |= 1LL << arvif->vdev_id;
10079 	ab->free_vdev_map &= ~(1LL << arvif->vdev_id);
10080 
10081 	spin_lock_bh(&ar->data_lock);
10082 	list_add(&arvif->list, &ar->arvifs);
10083 	spin_unlock_bh(&ar->data_lock);
10084 
10085 	ath12k_mac_update_vif_offload(arvif);
10086 
10087 	nss = hweight32(ar->cfg_tx_chainmask) ? : 1;
10088 	ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
10089 					    WMI_VDEV_PARAM_NSS, nss);
10090 	if (ret) {
10091 		ath12k_warn(ab, "failed to set vdev %d chainmask 0x%x, nss %d :%d\n",
10092 			    arvif->vdev_id, ar->cfg_tx_chainmask, nss, ret);
10093 		goto err_vdev_del;
10094 	}
10095 
10096 	switch (ahvif->vdev_type) {
10097 	case WMI_VDEV_TYPE_AP:
10098 		peer_param.vdev_id = arvif->vdev_id;
10099 		peer_param.peer_addr = arvif->bssid;
10100 		peer_param.peer_type = WMI_PEER_TYPE_DEFAULT;
10101 		ret = ath12k_peer_create(ar, arvif, NULL, &peer_param);
10102 		if (ret) {
10103 			ath12k_warn(ab, "failed to vdev %d create peer for AP: %d\n",
10104 				    arvif->vdev_id, ret);
10105 			goto err_vdev_del;
10106 		}
10107 
10108 		ret = ath12k_mac_set_kickout(arvif);
10109 		if (ret) {
10110 			ath12k_warn(ar->ab, "failed to set vdev %i kickout parameters: %d\n",
10111 				    arvif->vdev_id, ret);
10112 			goto err_peer_del;
10113 		}
10114 		ath12k_mac_11d_scan_stop_all(ar->ab);
10115 		break;
10116 	case WMI_VDEV_TYPE_STA:
10117 		param_id = WMI_STA_PS_PARAM_RX_WAKE_POLICY;
10118 		param_value = WMI_STA_PS_RX_WAKE_POLICY_WAKE;
10119 		ret = ath12k_wmi_set_sta_ps_param(ar, arvif->vdev_id,
10120 						  param_id, param_value);
10121 		if (ret) {
10122 			ath12k_warn(ar->ab, "failed to set vdev %d RX wake policy: %d\n",
10123 				    arvif->vdev_id, ret);
10124 			goto err_peer_del;
10125 		}
10126 
10127 		param_id = WMI_STA_PS_PARAM_TX_WAKE_THRESHOLD;
10128 		param_value = WMI_STA_PS_TX_WAKE_THRESHOLD_ALWAYS;
10129 		ret = ath12k_wmi_set_sta_ps_param(ar, arvif->vdev_id,
10130 						  param_id, param_value);
10131 		if (ret) {
10132 			ath12k_warn(ar->ab, "failed to set vdev %d TX wake threshold: %d\n",
10133 				    arvif->vdev_id, ret);
10134 			goto err_peer_del;
10135 		}
10136 
10137 		param_id = WMI_STA_PS_PARAM_PSPOLL_COUNT;
10138 		param_value = WMI_STA_PS_PSPOLL_COUNT_NO_MAX;
10139 		ret = ath12k_wmi_set_sta_ps_param(ar, arvif->vdev_id,
10140 						  param_id, param_value);
10141 		if (ret) {
10142 			ath12k_warn(ar->ab, "failed to set vdev %d pspoll count: %d\n",
10143 				    arvif->vdev_id, ret);
10144 			goto err_peer_del;
10145 		}
10146 
10147 		ret = ath12k_wmi_pdev_set_ps_mode(ar, arvif->vdev_id, false);
10148 		if (ret) {
10149 			ath12k_warn(ar->ab, "failed to disable vdev %d ps mode: %d\n",
10150 				    arvif->vdev_id, ret);
10151 			goto err_peer_del;
10152 		}
10153 
10154 		if (test_bit(WMI_TLV_SERVICE_11D_OFFLOAD, ab->wmi_ab.svc_map) &&
10155 		    ahvif->vdev_type == WMI_VDEV_TYPE_STA &&
10156 		    ahvif->vdev_subtype == WMI_VDEV_SUBTYPE_NONE) {
10157 			reinit_completion(&ar->completed_11d_scan);
10158 			ar->state_11d = ATH12K_11D_PREPARING;
10159 		}
10160 		break;
10161 	case WMI_VDEV_TYPE_MONITOR:
10162 		ar->monitor_vdev_created = true;
10163 		break;
10164 	default:
10165 		break;
10166 	}
10167 
10168 	if (link_conf)
10169 		arvif->txpower = link_conf->txpower;
10170 	else
10171 		arvif->txpower = NL80211_TX_POWER_AUTOMATIC;
10172 
10173 	ret = ath12k_mac_txpower_recalc(ar);
10174 	if (ret)
10175 		goto err_peer_del;
10176 
10177 	param_id = WMI_VDEV_PARAM_RTS_THRESHOLD;
10178 	param_value = hw->wiphy->rts_threshold;
10179 	ar->rts_threshold = param_value;
10180 	ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
10181 					    param_id, param_value);
10182 	if (ret) {
10183 		ath12k_warn(ar->ab, "failed to set rts threshold for vdev %d: %d\n",
10184 			    arvif->vdev_id, ret);
10185 	}
10186 
10187 	ath12k_dp_vdev_tx_attach(ar, arvif);
10188 
10189 	return ret;
10190 
10191 err_peer_del:
10192 	if (ahvif->vdev_type == WMI_VDEV_TYPE_AP) {
10193 		reinit_completion(&ar->peer_delete_done);
10194 
10195 		ret = ath12k_wmi_send_peer_delete_cmd(ar, arvif->bssid,
10196 						      arvif->vdev_id);
10197 		if (ret) {
10198 			ath12k_warn(ar->ab, "failed to delete peer vdev_id %d addr %pM\n",
10199 				    arvif->vdev_id, arvif->bssid);
10200 			goto err;
10201 		}
10202 
10203 		ret = ath12k_wait_for_peer_delete_done(ar, arvif->vdev_id,
10204 						       arvif->bssid);
10205 		if (ret)
10206 			goto err_vdev_del;
10207 
10208 		ar->num_peers--;
10209 	}
10210 
10211 err_vdev_del:
10212 	if (ahvif->vdev_type == WMI_VDEV_TYPE_MONITOR) {
10213 		ar->monitor_vdev_id = -1;
10214 		ar->monitor_vdev_created = false;
10215 	}
10216 
10217 	ath12k_wmi_vdev_delete(ar, arvif->vdev_id);
10218 	ar->num_created_vdevs--;
10219 	arvif->is_created = false;
10220 	arvif->ar = NULL;
10221 	ar->allocated_vdev_map &= ~(1LL << arvif->vdev_id);
10222 	ab->free_vdev_map |= 1LL << arvif->vdev_id;
10223 	ab->free_vdev_stats_id_map &= ~(1LL << arvif->vdev_stats_id);
10224 	spin_lock_bh(&ar->data_lock);
10225 	list_del(&arvif->list);
10226 	spin_unlock_bh(&ar->data_lock);
10227 
10228 err:
10229 	arvif->ar = NULL;
10230 	return ret;
10231 }
10232 
10233 static void ath12k_mac_vif_flush_key_cache(struct ath12k_link_vif *arvif)
10234 {
10235 	struct ath12k_key_conf *key_conf, *tmp;
10236 	struct ath12k_vif *ahvif = arvif->ahvif;
10237 	struct ath12k_hw *ah = ahvif->ah;
10238 	struct ath12k_sta *ahsta;
10239 	struct ath12k_link_sta *arsta;
10240 	struct ath12k_vif_cache *cache = ahvif->cache[arvif->link_id];
10241 	int ret;
10242 
10243 	lockdep_assert_wiphy(ah->hw->wiphy);
10244 
10245 	list_for_each_entry_safe(key_conf, tmp, &cache->key_conf.list, list) {
10246 		arsta = NULL;
10247 		if (key_conf->sta) {
10248 			ahsta = ath12k_sta_to_ahsta(key_conf->sta);
10249 			arsta = wiphy_dereference(ah->hw->wiphy,
10250 						  ahsta->link[arvif->link_id]);
10251 			if (!arsta)
10252 				goto free_cache;
10253 		}
10254 
10255 		ret = ath12k_mac_set_key(arvif->ar, key_conf->cmd,
10256 					 arvif, arsta,
10257 					 key_conf->key);
10258 		if (ret)
10259 			ath12k_warn(arvif->ar->ab, "unable to apply set key param to vdev %d ret %d\n",
10260 				    arvif->vdev_id, ret);
10261 free_cache:
10262 		list_del(&key_conf->list);
10263 		kfree(key_conf);
10264 	}
10265 }
10266 
10267 static void ath12k_mac_vif_cache_flush(struct ath12k *ar, struct ath12k_link_vif *arvif)
10268 {
10269 	struct ath12k_vif *ahvif = arvif->ahvif;
10270 	struct ieee80211_vif *vif = ath12k_ahvif_to_vif(ahvif);
10271 	struct ath12k_vif_cache *cache = ahvif->cache[arvif->link_id];
10272 	struct ath12k_base *ab = ar->ab;
10273 	struct ieee80211_bss_conf *link_conf;
10274 
10275 	int ret;
10276 
10277 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
10278 
10279 	if (!cache)
10280 		return;
10281 
10282 	if (cache->tx_conf.changed) {
10283 		ret = ath12k_mac_conf_tx(arvif, cache->tx_conf.ac,
10284 					 &cache->tx_conf.tx_queue_params);
10285 		if (ret)
10286 			ath12k_warn(ab,
10287 				    "unable to apply tx config parameters to vdev %d\n",
10288 				    ret);
10289 	}
10290 
10291 	if (cache->bss_conf_changed) {
10292 		link_conf = ath12k_mac_get_link_bss_conf(arvif);
10293 		if (!link_conf) {
10294 			ath12k_warn(ar->ab, "unable to access bss link conf in cache flush for vif %pM link %u\n",
10295 				    vif->addr, arvif->link_id);
10296 			return;
10297 		}
10298 		ath12k_mac_bss_info_changed(ar, arvif, link_conf,
10299 					    cache->bss_conf_changed);
10300 	}
10301 
10302 	if (!list_empty(&cache->key_conf.list))
10303 		ath12k_mac_vif_flush_key_cache(arvif);
10304 
10305 	ath12k_ahvif_put_link_cache(ahvif, arvif->link_id);
10306 }
10307 
10308 static struct ath12k *ath12k_mac_assign_vif_to_vdev(struct ieee80211_hw *hw,
10309 						    struct ath12k_link_vif *arvif,
10310 						    struct ieee80211_chanctx_conf *ctx)
10311 {
10312 	struct ath12k_vif *ahvif = arvif->ahvif;
10313 	struct ieee80211_vif *vif = ath12k_ahvif_to_vif(ahvif);
10314 	struct ath12k_link_vif *scan_arvif;
10315 	struct ath12k_hw *ah = hw->priv;
10316 	struct ath12k *ar;
10317 	struct ath12k_base *ab;
10318 	u8 link_id = arvif->link_id, scan_link_id;
10319 	unsigned long scan_link_map;
10320 	int ret;
10321 
10322 	lockdep_assert_wiphy(hw->wiphy);
10323 
10324 	if (ah->num_radio == 1)
10325 		ar = ah->radio;
10326 	else if (ctx)
10327 		ar = ath12k_get_ar_by_ctx(hw, ctx);
10328 	else
10329 		return NULL;
10330 
10331 	if (!ar)
10332 		return NULL;
10333 
10334 	/* cleanup the scan vdev if we are done scan on that ar
10335 	 * and now we want to create for actual usage.
10336 	 */
10337 	if (ieee80211_vif_is_mld(vif)) {
10338 		scan_link_map = ahvif->links_map & ATH12K_SCAN_LINKS_MASK;
10339 		for_each_set_bit(scan_link_id, &scan_link_map, ATH12K_NUM_MAX_LINKS) {
10340 			scan_arvif = wiphy_dereference(hw->wiphy,
10341 						       ahvif->link[scan_link_id]);
10342 			if (scan_arvif && scan_arvif->ar == ar) {
10343 				ar->scan.arvif = NULL;
10344 				ath12k_mac_remove_link_interface(hw, scan_arvif);
10345 				ath12k_mac_unassign_link_vif(scan_arvif);
10346 				break;
10347 			}
10348 		}
10349 	}
10350 
10351 	if (arvif->ar) {
10352 		/* This is not expected really */
10353 		if (WARN_ON(!arvif->is_created)) {
10354 			arvif->ar = NULL;
10355 			return NULL;
10356 		}
10357 
10358 		if (ah->num_radio == 1)
10359 			return arvif->ar;
10360 
10361 		/* This can happen as scan vdev gets created during multiple scans
10362 		 * across different radios before a vdev is brought up in
10363 		 * a certain radio.
10364 		 */
10365 		if (ar != arvif->ar) {
10366 			if (WARN_ON(arvif->is_started))
10367 				return NULL;
10368 
10369 			ath12k_mac_remove_link_interface(hw, arvif);
10370 			ath12k_mac_unassign_link_vif(arvif);
10371 		}
10372 	}
10373 
10374 	ab = ar->ab;
10375 
10376 	/* Assign arvif again here since previous radio switch block
10377 	 * would've unassigned and cleared it.
10378 	 */
10379 	arvif = ath12k_mac_assign_link_vif(ah, vif, link_id);
10380 	if (vif->type == NL80211_IFTYPE_AP &&
10381 	    ar->num_peers > (ar->max_num_peers - 1)) {
10382 		ath12k_warn(ab, "failed to create vdev due to insufficient peer entry resource in firmware\n");
10383 		goto unlock;
10384 	}
10385 
10386 	if (arvif->is_created)
10387 		goto flush;
10388 
10389 	ret = ath12k_mac_vdev_create(ar, arvif);
10390 	if (ret) {
10391 		ath12k_warn(ab, "failed to create vdev %pM ret %d", vif->addr, ret);
10392 		goto unlock;
10393 	}
10394 
10395 flush:
10396 	/* If the vdev is created during channel assign and not during
10397 	 * add_interface(), Apply any parameters for the vdev which were received
10398 	 * after add_interface, corresponding to this vif.
10399 	 */
10400 	ath12k_mac_vif_cache_flush(ar, arvif);
10401 unlock:
10402 	return arvif->ar;
10403 }
10404 
10405 static int ath12k_mac_op_add_interface(struct ieee80211_hw *hw,
10406 				       struct ieee80211_vif *vif)
10407 {
10408 	struct ath12k_hw *ah = ath12k_hw_to_ah(hw);
10409 	struct ath12k_vif *ahvif = ath12k_vif_to_ahvif(vif);
10410 	struct ath12k_reg_info *reg_info;
10411 	struct ath12k_link_vif *arvif;
10412 	struct ath12k_base *ab;
10413 	struct ath12k *ar;
10414 	int i;
10415 
10416 	lockdep_assert_wiphy(hw->wiphy);
10417 
10418 	memset(ahvif, 0, sizeof(*ahvif));
10419 
10420 	ahvif->ah = ah;
10421 	ahvif->vif = vif;
10422 	arvif = &ahvif->deflink;
10423 
10424 	ath12k_mac_init_arvif(ahvif, arvif, -1);
10425 
10426 	/* Allocate Default Queue now and reassign during actual vdev create */
10427 	vif->cab_queue = ATH12K_HW_DEFAULT_QUEUE;
10428 	for (i = 0; i < ARRAY_SIZE(vif->hw_queue); i++)
10429 		vif->hw_queue[i] = ATH12K_HW_DEFAULT_QUEUE;
10430 
10431 	vif->driver_flags |= IEEE80211_VIF_SUPPORTS_UAPSD;
10432 
10433 	ath12k_mac_determine_vdev_type(vif, ahvif);
10434 
10435 	for_each_ar(ah, ar, i) {
10436 		if (!ath12k_wmi_supports_6ghz_cc_ext(ar))
10437 			continue;
10438 
10439 		ab = ar->ab;
10440 		reg_info = ab->reg_info[ar->pdev_idx];
10441 		ath12k_dbg(ab, ATH12K_DBG_MAC, "interface added to change reg rules\n");
10442 		ah->regd_updated = false;
10443 		ath12k_reg_handle_chan_list(ab, reg_info, ahvif->vdev_type,
10444 					    IEEE80211_REG_UNSET_AP);
10445 		break;
10446 	}
10447 
10448 	/* Defer vdev creation until assign_chanctx or hw_scan is initiated as driver
10449 	 * will not know if this interface is an ML vif at this point.
10450 	 */
10451 	return 0;
10452 }
10453 
10454 static void ath12k_mac_vif_unref(struct ath12k_dp *dp, struct ieee80211_vif *vif)
10455 {
10456 	struct ath12k_tx_desc_info *tx_desc_info;
10457 	struct ath12k_skb_cb *skb_cb;
10458 	struct sk_buff *skb;
10459 	int i;
10460 
10461 	for (i = 0; i < ATH12K_HW_MAX_QUEUES; i++) {
10462 		spin_lock_bh(&dp->tx_desc_lock[i]);
10463 
10464 		list_for_each_entry(tx_desc_info, &dp->tx_desc_used_list[i],
10465 				    list) {
10466 			skb = tx_desc_info->skb;
10467 			if (!skb)
10468 				continue;
10469 
10470 			skb_cb = ATH12K_SKB_CB(skb);
10471 			if (skb_cb->vif == vif)
10472 				skb_cb->vif = NULL;
10473 		}
10474 
10475 		spin_unlock_bh(&dp->tx_desc_lock[i]);
10476 	}
10477 }
10478 
10479 static int ath12k_mac_vdev_delete(struct ath12k *ar, struct ath12k_link_vif *arvif)
10480 {
10481 	struct ath12k_vif *ahvif = arvif->ahvif;
10482 	struct ieee80211_vif *vif = ath12k_ahvif_to_vif(ahvif);
10483 	struct ath12k_base *ab = ar->ab;
10484 	unsigned long time_left;
10485 	int ret;
10486 
10487 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
10488 
10489 	reinit_completion(&ar->vdev_delete_done);
10490 
10491 	ret = ath12k_wmi_vdev_delete(ar, arvif->vdev_id);
10492 	if (ret) {
10493 		ath12k_warn(ab, "failed to delete WMI vdev %d: %d\n",
10494 			    arvif->vdev_id, ret);
10495 		goto err_vdev_del;
10496 	}
10497 
10498 	time_left = wait_for_completion_timeout(&ar->vdev_delete_done,
10499 						ATH12K_VDEV_DELETE_TIMEOUT_HZ);
10500 	if (time_left == 0) {
10501 		ath12k_warn(ab, "Timeout in receiving vdev delete response\n");
10502 		goto err_vdev_del;
10503 	}
10504 
10505 	ab->free_vdev_map |= 1LL << arvif->vdev_id;
10506 	ar->allocated_vdev_map &= ~(1LL << arvif->vdev_id);
10507 	ar->num_created_vdevs--;
10508 
10509 	if (ahvif->vdev_type == WMI_VDEV_TYPE_MONITOR) {
10510 		ar->monitor_vdev_id = -1;
10511 		ar->monitor_vdev_created = false;
10512 	}
10513 
10514 	ath12k_dbg(ab, ATH12K_DBG_MAC, "vdev %pM deleted, vdev_id %d\n",
10515 		   vif->addr, arvif->vdev_id);
10516 
10517 err_vdev_del:
10518 	spin_lock_bh(&ar->data_lock);
10519 	list_del(&arvif->list);
10520 	spin_unlock_bh(&ar->data_lock);
10521 
10522 	ath12k_peer_cleanup(ar, arvif->vdev_id);
10523 	ath12k_ahvif_put_link_cache(ahvif, arvif->link_id);
10524 
10525 	idr_for_each(&ar->txmgmt_idr,
10526 		     ath12k_mac_vif_txmgmt_idr_remove, vif);
10527 
10528 	ath12k_mac_vif_unref(&ab->dp, vif);
10529 	ath12k_dp_tx_put_bank_profile(&ab->dp, arvif->bank_id);
10530 
10531 	/* Recalc txpower for remaining vdev */
10532 	ath12k_mac_txpower_recalc(ar);
10533 
10534 	/* TODO: recal traffic pause state based on the available vdevs */
10535 	arvif->is_created = false;
10536 	arvif->ar = NULL;
10537 
10538 	return ret;
10539 }
10540 
10541 static void ath12k_mac_op_remove_interface(struct ieee80211_hw *hw,
10542 					   struct ieee80211_vif *vif)
10543 {
10544 	struct ath12k_vif *ahvif = ath12k_vif_to_ahvif(vif);
10545 	struct ath12k_link_vif *arvif;
10546 	struct ath12k *ar;
10547 	u8 link_id;
10548 
10549 	lockdep_assert_wiphy(hw->wiphy);
10550 
10551 	for (link_id = 0; link_id < ATH12K_NUM_MAX_LINKS; link_id++) {
10552 		/* if we cached some config but never received assign chanctx,
10553 		 * free the allocated cache.
10554 		 */
10555 		ath12k_ahvif_put_link_cache(ahvif, link_id);
10556 		arvif = wiphy_dereference(hw->wiphy, ahvif->link[link_id]);
10557 		if (!arvif || !arvif->is_created)
10558 			continue;
10559 
10560 		ar = arvif->ar;
10561 
10562 		/* Scan abortion is in progress since before this, cancel_hw_scan()
10563 		 * is expected to be executed. Since link is anyways going to be removed
10564 		 * now, just cancel the worker and send the scan aborted to user space
10565 		 */
10566 		if (ar->scan.arvif == arvif) {
10567 			wiphy_work_cancel(hw->wiphy, &ar->scan.vdev_clean_wk);
10568 
10569 			spin_lock_bh(&ar->data_lock);
10570 			ar->scan.arvif = NULL;
10571 			if (!ar->scan.is_roc) {
10572 				struct cfg80211_scan_info info = {
10573 					.aborted = true,
10574 				};
10575 
10576 				ath12k_mac_scan_send_complete(ar, &info);
10577 			}
10578 
10579 			ar->scan.state = ATH12K_SCAN_IDLE;
10580 			ar->scan_channel = NULL;
10581 			ar->scan.roc_freq = 0;
10582 			spin_unlock_bh(&ar->data_lock);
10583 		}
10584 
10585 		ath12k_mac_remove_link_interface(hw, arvif);
10586 		ath12k_mac_unassign_link_vif(arvif);
10587 	}
10588 }
10589 
10590 /* FIXME: Has to be verified. */
10591 #define SUPPORTED_FILTERS			\
10592 	(FIF_ALLMULTI |				\
10593 	FIF_CONTROL |				\
10594 	FIF_PSPOLL |				\
10595 	FIF_OTHER_BSS |				\
10596 	FIF_BCN_PRBRESP_PROMISC |		\
10597 	FIF_PROBE_REQ |				\
10598 	FIF_FCSFAIL)
10599 
10600 static void ath12k_mac_op_configure_filter(struct ieee80211_hw *hw,
10601 					   unsigned int changed_flags,
10602 					   unsigned int *total_flags,
10603 					   u64 multicast)
10604 {
10605 	struct ath12k_hw *ah = ath12k_hw_to_ah(hw);
10606 	struct ath12k *ar;
10607 
10608 	lockdep_assert_wiphy(hw->wiphy);
10609 
10610 	ar = ath12k_ah_to_ar(ah, 0);
10611 
10612 	*total_flags &= SUPPORTED_FILTERS;
10613 	ar->filter_flags = *total_flags;
10614 }
10615 
10616 static int ath12k_mac_op_get_antenna(struct ieee80211_hw *hw, int radio_idx,
10617 				     u32 *tx_ant, u32 *rx_ant)
10618 {
10619 	struct ath12k_hw *ah = ath12k_hw_to_ah(hw);
10620 	int antennas_rx = 0, antennas_tx = 0;
10621 	struct ath12k *ar;
10622 	int i;
10623 
10624 	lockdep_assert_wiphy(hw->wiphy);
10625 
10626 	for_each_ar(ah, ar, i) {
10627 		antennas_rx = max_t(u32, antennas_rx, ar->cfg_rx_chainmask);
10628 		antennas_tx = max_t(u32, antennas_tx, ar->cfg_tx_chainmask);
10629 	}
10630 
10631 	*tx_ant = antennas_tx;
10632 	*rx_ant = antennas_rx;
10633 
10634 	return 0;
10635 }
10636 
10637 static int ath12k_mac_op_set_antenna(struct ieee80211_hw *hw, int radio_idx,
10638 				     u32 tx_ant, u32 rx_ant)
10639 {
10640 	struct ath12k_hw *ah = ath12k_hw_to_ah(hw);
10641 	struct ath12k *ar;
10642 	int ret = 0;
10643 	int i;
10644 
10645 	lockdep_assert_wiphy(hw->wiphy);
10646 
10647 	for_each_ar(ah, ar, i) {
10648 		ret = __ath12k_set_antenna(ar, tx_ant, rx_ant);
10649 		if (ret)
10650 			break;
10651 	}
10652 
10653 	return ret;
10654 }
10655 
10656 static int ath12k_mac_ampdu_action(struct ieee80211_hw *hw,
10657 				   struct ieee80211_vif *vif,
10658 				   struct ieee80211_ampdu_params *params,
10659 				   u8 link_id)
10660 {
10661 	struct ath12k *ar;
10662 	int ret = -EINVAL;
10663 
10664 	lockdep_assert_wiphy(hw->wiphy);
10665 
10666 	ar = ath12k_get_ar_by_vif(hw, vif, link_id);
10667 	if (!ar)
10668 		return -EINVAL;
10669 
10670 	switch (params->action) {
10671 	case IEEE80211_AMPDU_RX_START:
10672 		ret = ath12k_dp_rx_ampdu_start(ar, params, link_id);
10673 		break;
10674 	case IEEE80211_AMPDU_RX_STOP:
10675 		ret = ath12k_dp_rx_ampdu_stop(ar, params, link_id);
10676 		break;
10677 	case IEEE80211_AMPDU_TX_START:
10678 	case IEEE80211_AMPDU_TX_STOP_CONT:
10679 	case IEEE80211_AMPDU_TX_STOP_FLUSH:
10680 	case IEEE80211_AMPDU_TX_STOP_FLUSH_CONT:
10681 	case IEEE80211_AMPDU_TX_OPERATIONAL:
10682 		/* Tx A-MPDU aggregation offloaded to hw/fw so deny mac80211
10683 		 * Tx aggregation requests.
10684 		 */
10685 		ret = -EOPNOTSUPP;
10686 		break;
10687 	}
10688 
10689 	if (ret)
10690 		ath12k_warn(ar->ab, "unable to perform ampdu action %d for vif %pM link %u ret %d\n",
10691 			    params->action, vif->addr, link_id, ret);
10692 
10693 	return ret;
10694 }
10695 
10696 static int ath12k_mac_op_ampdu_action(struct ieee80211_hw *hw,
10697 				      struct ieee80211_vif *vif,
10698 				      struct ieee80211_ampdu_params *params)
10699 {
10700 	struct ieee80211_sta *sta = params->sta;
10701 	struct ath12k_sta *ahsta = ath12k_sta_to_ahsta(sta);
10702 	unsigned long links_map = ahsta->links_map;
10703 	int ret = -EINVAL;
10704 	u8 link_id;
10705 
10706 	lockdep_assert_wiphy(hw->wiphy);
10707 
10708 	if (WARN_ON(!links_map))
10709 		return ret;
10710 
10711 	for_each_set_bit(link_id, &links_map, IEEE80211_MLD_MAX_NUM_LINKS) {
10712 		ret = ath12k_mac_ampdu_action(hw, vif, params, link_id);
10713 		if (ret)
10714 			return ret;
10715 	}
10716 
10717 	return 0;
10718 }
10719 
10720 static int ath12k_mac_op_add_chanctx(struct ieee80211_hw *hw,
10721 				     struct ieee80211_chanctx_conf *ctx)
10722 {
10723 	struct ath12k *ar;
10724 	struct ath12k_base *ab;
10725 
10726 	lockdep_assert_wiphy(hw->wiphy);
10727 
10728 	ar = ath12k_get_ar_by_ctx(hw, ctx);
10729 	if (!ar)
10730 		return -EINVAL;
10731 
10732 	ab = ar->ab;
10733 
10734 	ath12k_dbg(ab, ATH12K_DBG_MAC,
10735 		   "mac chanctx add freq %u width %d ptr %p\n",
10736 		   ctx->def.chan->center_freq, ctx->def.width, ctx);
10737 
10738 	spin_lock_bh(&ar->data_lock);
10739 	/* TODO: In case of multiple channel context, populate rx_channel from
10740 	 * Rx PPDU desc information.
10741 	 */
10742 	ar->rx_channel = ctx->def.chan;
10743 	spin_unlock_bh(&ar->data_lock);
10744 	ar->chan_tx_pwr = ATH12K_PDEV_TX_POWER_INVALID;
10745 
10746 	return 0;
10747 }
10748 
10749 static void ath12k_mac_op_remove_chanctx(struct ieee80211_hw *hw,
10750 					 struct ieee80211_chanctx_conf *ctx)
10751 {
10752 	struct ath12k *ar;
10753 	struct ath12k_base *ab;
10754 
10755 	lockdep_assert_wiphy(hw->wiphy);
10756 
10757 	ar = ath12k_get_ar_by_ctx(hw, ctx);
10758 	if (!ar)
10759 		return;
10760 
10761 	ab = ar->ab;
10762 
10763 	ath12k_dbg(ab, ATH12K_DBG_MAC,
10764 		   "mac chanctx remove freq %u width %d ptr %p\n",
10765 		   ctx->def.chan->center_freq, ctx->def.width, ctx);
10766 
10767 	spin_lock_bh(&ar->data_lock);
10768 	/* TODO: In case of there is one more channel context left, populate
10769 	 * rx_channel with the channel of that remaining channel context.
10770 	 */
10771 	ar->rx_channel = NULL;
10772 	spin_unlock_bh(&ar->data_lock);
10773 	ar->chan_tx_pwr = ATH12K_PDEV_TX_POWER_INVALID;
10774 }
10775 
10776 static enum wmi_phy_mode
10777 ath12k_mac_check_down_grade_phy_mode(struct ath12k *ar,
10778 				     enum wmi_phy_mode mode,
10779 				     enum nl80211_band band,
10780 				     enum nl80211_iftype type)
10781 {
10782 	struct ieee80211_sta_eht_cap *eht_cap = NULL;
10783 	enum wmi_phy_mode down_mode;
10784 	int n = ar->mac.sbands[band].n_iftype_data;
10785 	int i;
10786 	struct ieee80211_sband_iftype_data *data;
10787 
10788 	if (mode < MODE_11BE_EHT20)
10789 		return mode;
10790 
10791 	data = ar->mac.iftype[band];
10792 	for (i = 0; i < n; i++) {
10793 		if (data[i].types_mask & BIT(type)) {
10794 			eht_cap = &data[i].eht_cap;
10795 			break;
10796 		}
10797 	}
10798 
10799 	if (eht_cap && eht_cap->has_eht)
10800 		return mode;
10801 
10802 	switch (mode) {
10803 	case MODE_11BE_EHT20:
10804 		down_mode = MODE_11AX_HE20;
10805 		break;
10806 	case MODE_11BE_EHT40:
10807 		down_mode = MODE_11AX_HE40;
10808 		break;
10809 	case MODE_11BE_EHT80:
10810 		down_mode = MODE_11AX_HE80;
10811 		break;
10812 	case MODE_11BE_EHT80_80:
10813 		down_mode = MODE_11AX_HE80_80;
10814 		break;
10815 	case MODE_11BE_EHT160:
10816 	case MODE_11BE_EHT160_160:
10817 	case MODE_11BE_EHT320:
10818 		down_mode = MODE_11AX_HE160;
10819 		break;
10820 	case MODE_11BE_EHT20_2G:
10821 		down_mode = MODE_11AX_HE20_2G;
10822 		break;
10823 	case MODE_11BE_EHT40_2G:
10824 		down_mode = MODE_11AX_HE40_2G;
10825 		break;
10826 	default:
10827 		down_mode = mode;
10828 		break;
10829 	}
10830 
10831 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
10832 		   "mac vdev start phymode %s downgrade to %s\n",
10833 		   ath12k_mac_phymode_str(mode),
10834 		   ath12k_mac_phymode_str(down_mode));
10835 
10836 	return down_mode;
10837 }
10838 
10839 static void
10840 ath12k_mac_mlo_get_vdev_args(struct ath12k_link_vif *arvif,
10841 			     struct wmi_ml_arg *ml_arg)
10842 {
10843 	struct ath12k_vif *ahvif = arvif->ahvif;
10844 	struct wmi_ml_partner_info *partner_info;
10845 	struct ieee80211_bss_conf *link_conf;
10846 	struct ath12k_link_vif *arvif_p;
10847 	unsigned long links;
10848 	u8 link_id;
10849 
10850 	lockdep_assert_wiphy(ahvif->ah->hw->wiphy);
10851 
10852 	if (!ath12k_mac_is_ml_arvif(arvif))
10853 		return;
10854 
10855 	if (hweight16(ahvif->vif->valid_links) > ATH12K_WMI_MLO_MAX_LINKS)
10856 		return;
10857 
10858 	ml_arg->enabled = true;
10859 
10860 	/* Driver always add a new link via VDEV START, FW takes
10861 	 * care of internally adding this link to existing
10862 	 * link vdevs which are advertised as partners below
10863 	 */
10864 	ml_arg->link_add = true;
10865 
10866 	ml_arg->assoc_link = arvif->is_sta_assoc_link;
10867 
10868 	partner_info = ml_arg->partner_info;
10869 
10870 	links = ahvif->links_map;
10871 	for_each_set_bit(link_id, &links, IEEE80211_MLD_MAX_NUM_LINKS) {
10872 		arvif_p = wiphy_dereference(ahvif->ah->hw->wiphy, ahvif->link[link_id]);
10873 
10874 		if (WARN_ON(!arvif_p))
10875 			continue;
10876 
10877 		if (arvif == arvif_p)
10878 			continue;
10879 
10880 		if (!arvif_p->is_created)
10881 			continue;
10882 
10883 		link_conf = wiphy_dereference(ahvif->ah->hw->wiphy,
10884 					      ahvif->vif->link_conf[arvif_p->link_id]);
10885 
10886 		if (!link_conf)
10887 			continue;
10888 
10889 		partner_info->vdev_id = arvif_p->vdev_id;
10890 		partner_info->hw_link_id = arvif_p->ar->pdev->hw_link_id;
10891 		ether_addr_copy(partner_info->addr, link_conf->addr);
10892 		ml_arg->num_partner_links++;
10893 		partner_info++;
10894 	}
10895 }
10896 
10897 static int
10898 ath12k_mac_vdev_start_restart(struct ath12k_link_vif *arvif,
10899 			      struct ieee80211_chanctx_conf *ctx,
10900 			      bool restart)
10901 {
10902 	struct ath12k *ar = arvif->ar;
10903 	struct ath12k_base *ab = ar->ab;
10904 	struct wmi_vdev_start_req_arg arg = {};
10905 	const struct cfg80211_chan_def *chandef = &ctx->def;
10906 	struct ieee80211_hw *hw = ath12k_ar_to_hw(ar);
10907 	struct ath12k_vif *ahvif = arvif->ahvif;
10908 	struct ieee80211_bss_conf *link_conf;
10909 	unsigned int dfs_cac_time;
10910 	int ret;
10911 
10912 	lockdep_assert_wiphy(hw->wiphy);
10913 
10914 	link_conf = ath12k_mac_get_link_bss_conf(arvif);
10915 	if (!link_conf) {
10916 		ath12k_warn(ar->ab, "unable to access bss link conf in vdev start for vif %pM link %u\n",
10917 			    ahvif->vif->addr, arvif->link_id);
10918 		return -ENOLINK;
10919 	}
10920 
10921 	reinit_completion(&ar->vdev_setup_done);
10922 
10923 	arg.vdev_id = arvif->vdev_id;
10924 	arg.dtim_period = arvif->dtim_period;
10925 	arg.bcn_intval = arvif->beacon_interval;
10926 	arg.punct_bitmap = ~arvif->punct_bitmap;
10927 
10928 	arg.freq = chandef->chan->center_freq;
10929 	arg.band_center_freq1 = chandef->center_freq1;
10930 	arg.band_center_freq2 = chandef->center_freq2;
10931 	arg.mode = ath12k_phymodes[chandef->chan->band][chandef->width];
10932 
10933 	arg.mode = ath12k_mac_check_down_grade_phy_mode(ar, arg.mode,
10934 							chandef->chan->band,
10935 							ahvif->vif->type);
10936 	arg.min_power = 0;
10937 	arg.max_power = chandef->chan->max_power;
10938 	arg.max_reg_power = chandef->chan->max_reg_power;
10939 	arg.max_antenna_gain = chandef->chan->max_antenna_gain;
10940 
10941 	arg.pref_tx_streams = ar->num_tx_chains;
10942 	arg.pref_rx_streams = ar->num_rx_chains;
10943 
10944 	arg.mbssid_flags = WMI_VDEV_MBSSID_FLAGS_NON_MBSSID_AP;
10945 	arg.mbssid_tx_vdev_id = 0;
10946 	if (test_bit(WMI_TLV_SERVICE_MBSS_PARAM_IN_VDEV_START_SUPPORT,
10947 		     ar->ab->wmi_ab.svc_map)) {
10948 		ret = ath12k_mac_setup_vdev_params_mbssid(arvif,
10949 							  &arg.mbssid_flags,
10950 							  &arg.mbssid_tx_vdev_id);
10951 		if (ret)
10952 			return ret;
10953 	}
10954 
10955 	if (ahvif->vdev_type == WMI_VDEV_TYPE_AP) {
10956 		arg.ssid = ahvif->u.ap.ssid;
10957 		arg.ssid_len = ahvif->u.ap.ssid_len;
10958 		arg.hidden_ssid = ahvif->u.ap.hidden_ssid;
10959 
10960 		/* For now allow DFS for AP mode */
10961 		arg.chan_radar = !!(chandef->chan->flags & IEEE80211_CHAN_RADAR);
10962 
10963 		arg.freq2_radar = ctx->radar_enabled;
10964 
10965 		arg.passive = arg.chan_radar;
10966 
10967 		spin_lock_bh(&ab->base_lock);
10968 		arg.regdomain = ar->ab->dfs_region;
10969 		spin_unlock_bh(&ab->base_lock);
10970 
10971 		/* TODO: Notify if secondary 80Mhz also needs radar detection */
10972 	}
10973 
10974 	arg.passive |= !!(chandef->chan->flags & IEEE80211_CHAN_NO_IR);
10975 
10976 	if (!restart)
10977 		ath12k_mac_mlo_get_vdev_args(arvif, &arg.ml);
10978 
10979 	ath12k_dbg(ab, ATH12K_DBG_MAC,
10980 		   "mac vdev %d start center_freq %d phymode %s punct_bitmap 0x%x\n",
10981 		   arg.vdev_id, arg.freq,
10982 		   ath12k_mac_phymode_str(arg.mode), arg.punct_bitmap);
10983 
10984 	ret = ath12k_wmi_vdev_start(ar, &arg, restart);
10985 	if (ret) {
10986 		ath12k_warn(ar->ab, "failed to %s WMI vdev %i\n",
10987 			    restart ? "restart" : "start", arg.vdev_id);
10988 		return ret;
10989 	}
10990 
10991 	ret = ath12k_mac_vdev_setup_sync(ar);
10992 	if (ret) {
10993 		ath12k_warn(ab, "failed to synchronize setup for vdev %i %s: %d\n",
10994 			    arg.vdev_id, restart ? "restart" : "start", ret);
10995 		return ret;
10996 	}
10997 
10998 	/* TODO: For now we only set TPC power here. However when
10999 	 * channel changes, say CSA, it should be updated again.
11000 	 */
11001 	if (ath12k_mac_supports_tpc(ar, ahvif, chandef)) {
11002 		ath12k_mac_fill_reg_tpc_info(ar, arvif, ctx);
11003 		ath12k_wmi_send_vdev_set_tpc_power(ar, arvif->vdev_id,
11004 						   &arvif->reg_tpc_info);
11005 	}
11006 
11007 	ar->num_started_vdevs++;
11008 	ath12k_dbg(ab, ATH12K_DBG_MAC,  "vdev %pM started, vdev_id %d\n",
11009 		   ahvif->vif->addr, arvif->vdev_id);
11010 
11011 	/* Enable CAC Running Flag in the driver by checking all sub-channel's DFS
11012 	 * state as NL80211_DFS_USABLE which indicates CAC needs to be
11013 	 * done before channel usage. This flag is used to drop rx packets.
11014 	 * during CAC.
11015 	 */
11016 	/* TODO: Set the flag for other interface types as required */
11017 	if (arvif->ahvif->vdev_type == WMI_VDEV_TYPE_AP && ctx->radar_enabled &&
11018 	    cfg80211_chandef_dfs_usable(hw->wiphy, chandef)) {
11019 		set_bit(ATH12K_FLAG_CAC_RUNNING, &ar->dev_flags);
11020 		dfs_cac_time = cfg80211_chandef_dfs_cac_time(hw->wiphy, chandef);
11021 
11022 		ath12k_dbg(ab, ATH12K_DBG_MAC,
11023 			   "CAC started dfs_cac_time %u center_freq %d center_freq1 %d for vdev %d\n",
11024 			   dfs_cac_time, arg.freq, arg.band_center_freq1, arg.vdev_id);
11025 	}
11026 
11027 	ret = ath12k_mac_set_txbf_conf(arvif);
11028 	if (ret)
11029 		ath12k_warn(ab, "failed to set txbf conf for vdev %d: %d\n",
11030 			    arvif->vdev_id, ret);
11031 
11032 	return 0;
11033 }
11034 
11035 static int ath12k_mac_vdev_start(struct ath12k_link_vif *arvif,
11036 				 struct ieee80211_chanctx_conf *ctx)
11037 {
11038 	return ath12k_mac_vdev_start_restart(arvif, ctx, false);
11039 }
11040 
11041 static int ath12k_mac_vdev_restart(struct ath12k_link_vif *arvif,
11042 				   struct ieee80211_chanctx_conf *ctx)
11043 {
11044 	return ath12k_mac_vdev_start_restart(arvif, ctx, true);
11045 }
11046 
11047 struct ath12k_mac_change_chanctx_arg {
11048 	struct ieee80211_chanctx_conf *ctx;
11049 	struct ieee80211_vif_chanctx_switch *vifs;
11050 	int n_vifs;
11051 	int next_vif;
11052 	struct ath12k *ar;
11053 };
11054 
11055 static void
11056 ath12k_mac_change_chanctx_cnt_iter(void *data, u8 *mac,
11057 				   struct ieee80211_vif *vif)
11058 {
11059 	struct ath12k_vif *ahvif = ath12k_vif_to_ahvif(vif);
11060 	struct ath12k_mac_change_chanctx_arg *arg = data;
11061 	struct ieee80211_bss_conf *link_conf;
11062 	struct ath12k_link_vif *arvif;
11063 	unsigned long links_map;
11064 	u8 link_id;
11065 
11066 	lockdep_assert_wiphy(ahvif->ah->hw->wiphy);
11067 
11068 	links_map = ahvif->links_map;
11069 	for_each_set_bit(link_id, &links_map, IEEE80211_MLD_MAX_NUM_LINKS) {
11070 		arvif = wiphy_dereference(ahvif->ah->hw->wiphy, ahvif->link[link_id]);
11071 		if (WARN_ON(!arvif))
11072 			continue;
11073 
11074 		if (!arvif->is_created || arvif->ar != arg->ar)
11075 			continue;
11076 
11077 		link_conf = wiphy_dereference(ahvif->ah->hw->wiphy,
11078 					      vif->link_conf[link_id]);
11079 		if (WARN_ON(!link_conf))
11080 			continue;
11081 
11082 		if (rcu_access_pointer(link_conf->chanctx_conf) != arg->ctx)
11083 			continue;
11084 
11085 		arg->n_vifs++;
11086 	}
11087 }
11088 
11089 static void
11090 ath12k_mac_change_chanctx_fill_iter(void *data, u8 *mac,
11091 				    struct ieee80211_vif *vif)
11092 {
11093 	struct ath12k_vif *ahvif = ath12k_vif_to_ahvif(vif);
11094 	struct ath12k_mac_change_chanctx_arg *arg = data;
11095 	struct ieee80211_bss_conf *link_conf;
11096 	struct ieee80211_chanctx_conf *ctx;
11097 	struct ath12k_link_vif *arvif;
11098 	unsigned long links_map;
11099 	u8 link_id;
11100 
11101 	lockdep_assert_wiphy(ahvif->ah->hw->wiphy);
11102 
11103 	links_map = ahvif->links_map;
11104 	for_each_set_bit(link_id, &links_map, IEEE80211_MLD_MAX_NUM_LINKS) {
11105 		arvif = wiphy_dereference(ahvif->ah->hw->wiphy, ahvif->link[link_id]);
11106 		if (WARN_ON(!arvif))
11107 			continue;
11108 
11109 		if (!arvif->is_created || arvif->ar != arg->ar)
11110 			continue;
11111 
11112 		link_conf = wiphy_dereference(ahvif->ah->hw->wiphy,
11113 					      vif->link_conf[arvif->link_id]);
11114 		if (WARN_ON(!link_conf))
11115 			continue;
11116 
11117 		ctx = rcu_access_pointer(link_conf->chanctx_conf);
11118 		if (ctx != arg->ctx)
11119 			continue;
11120 
11121 		if (WARN_ON(arg->next_vif == arg->n_vifs))
11122 			return;
11123 
11124 		arg->vifs[arg->next_vif].vif = vif;
11125 		arg->vifs[arg->next_vif].old_ctx = ctx;
11126 		arg->vifs[arg->next_vif].new_ctx = ctx;
11127 		arg->vifs[arg->next_vif].link_conf = link_conf;
11128 		arg->next_vif++;
11129 	}
11130 }
11131 
11132 static u32 ath12k_mac_nlwidth_to_wmiwidth(enum nl80211_chan_width width)
11133 {
11134 	switch (width) {
11135 	case NL80211_CHAN_WIDTH_20:
11136 		return WMI_CHAN_WIDTH_20;
11137 	case NL80211_CHAN_WIDTH_40:
11138 		return WMI_CHAN_WIDTH_40;
11139 	case NL80211_CHAN_WIDTH_80:
11140 		return WMI_CHAN_WIDTH_80;
11141 	case NL80211_CHAN_WIDTH_160:
11142 		return WMI_CHAN_WIDTH_160;
11143 	case NL80211_CHAN_WIDTH_80P80:
11144 		return WMI_CHAN_WIDTH_80P80;
11145 	case NL80211_CHAN_WIDTH_5:
11146 		return WMI_CHAN_WIDTH_5;
11147 	case NL80211_CHAN_WIDTH_10:
11148 		return WMI_CHAN_WIDTH_10;
11149 	case NL80211_CHAN_WIDTH_320:
11150 		return WMI_CHAN_WIDTH_320;
11151 	default:
11152 		WARN_ON(1);
11153 		return WMI_CHAN_WIDTH_20;
11154 	}
11155 }
11156 
11157 static int ath12k_mac_update_peer_puncturing_width(struct ath12k *ar,
11158 						   struct ath12k_link_vif *arvif,
11159 						   struct cfg80211_chan_def def)
11160 {
11161 	u32 param_id, param_value;
11162 	int ret;
11163 
11164 	if (arvif->ahvif->vdev_type != WMI_VDEV_TYPE_STA)
11165 		return 0;
11166 
11167 	param_id = WMI_PEER_CHWIDTH_PUNCTURE_20MHZ_BITMAP;
11168 	param_value = ath12k_mac_nlwidth_to_wmiwidth(def.width) |
11169 		u32_encode_bits((~def.punctured),
11170 				WMI_PEER_PUNCTURE_BITMAP);
11171 
11172 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
11173 		   "punctured bitmap %02x width %d vdev %d\n",
11174 		   def.punctured, def.width, arvif->vdev_id);
11175 
11176 	ret = ath12k_wmi_set_peer_param(ar, arvif->bssid,
11177 					arvif->vdev_id, param_id,
11178 					param_value);
11179 
11180 	return ret;
11181 }
11182 
11183 static void
11184 ath12k_mac_update_vif_chan(struct ath12k *ar,
11185 			   struct ieee80211_vif_chanctx_switch *vifs,
11186 			   int n_vifs)
11187 {
11188 	struct ath12k_wmi_vdev_up_params params = {};
11189 	struct ieee80211_bss_conf *link_conf;
11190 	struct ath12k_base *ab = ar->ab;
11191 	struct ath12k_link_vif *arvif;
11192 	struct ieee80211_vif *vif;
11193 	struct ath12k_vif *ahvif;
11194 	u8 link_id;
11195 	int ret;
11196 	int i;
11197 	bool monitor_vif = false;
11198 
11199 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
11200 
11201 	for (i = 0; i < n_vifs; i++) {
11202 		vif = vifs[i].vif;
11203 		ahvif = ath12k_vif_to_ahvif(vif);
11204 		link_conf = vifs[i].link_conf;
11205 		link_id = link_conf->link_id;
11206 		arvif = wiphy_dereference(ath12k_ar_to_hw(ar)->wiphy,
11207 					  ahvif->link[link_id]);
11208 
11209 		if (vif->type == NL80211_IFTYPE_MONITOR) {
11210 			monitor_vif = true;
11211 			continue;
11212 		}
11213 
11214 		ath12k_dbg(ab, ATH12K_DBG_MAC,
11215 			   "mac chanctx switch vdev_id %i freq %u->%u width %d->%d\n",
11216 			   arvif->vdev_id,
11217 			   vifs[i].old_ctx->def.chan->center_freq,
11218 			   vifs[i].new_ctx->def.chan->center_freq,
11219 			   vifs[i].old_ctx->def.width,
11220 			   vifs[i].new_ctx->def.width);
11221 
11222 		if (WARN_ON(!arvif->is_started))
11223 			continue;
11224 
11225 		arvif->punct_bitmap = vifs[i].new_ctx->def.punctured;
11226 
11227 		/* Firmware expect vdev_restart only if vdev is up.
11228 		 * If vdev is down then it expect vdev_stop->vdev_start.
11229 		 */
11230 		if (arvif->is_up) {
11231 			ret = ath12k_mac_vdev_restart(arvif, vifs[i].new_ctx);
11232 			if (ret) {
11233 				ath12k_warn(ab, "failed to restart vdev %d: %d\n",
11234 					    arvif->vdev_id, ret);
11235 				continue;
11236 			}
11237 		} else {
11238 			ret = ath12k_mac_vdev_stop(arvif);
11239 			if (ret) {
11240 				ath12k_warn(ab, "failed to stop vdev %d: %d\n",
11241 					    arvif->vdev_id, ret);
11242 				continue;
11243 			}
11244 
11245 			ret = ath12k_mac_vdev_start(arvif, vifs[i].new_ctx);
11246 			if (ret)
11247 				ath12k_warn(ab, "failed to start vdev %d: %d\n",
11248 					    arvif->vdev_id, ret);
11249 			continue;
11250 		}
11251 
11252 		ret = ath12k_mac_update_peer_puncturing_width(arvif->ar, arvif,
11253 							      vifs[i].new_ctx->def);
11254 		if (ret) {
11255 			ath12k_warn(ar->ab,
11256 				    "failed to update puncturing bitmap %02x and width %d: %d\n",
11257 				    vifs[i].new_ctx->def.punctured,
11258 				    vifs[i].new_ctx->def.width, ret);
11259 			continue;
11260 		}
11261 
11262 		/* Defer VDEV bring-up during CSA to avoid installing stale
11263 		 * beacon templates. The beacon content is updated only
11264 		 * after CSA finalize, so we mark CSA in progress and skip
11265 		 * VDEV_UP for now. It will be handled later in
11266 		 * bss_info_changed().
11267 		 */
11268 		if (link_conf->csa_active &&
11269 		    arvif->ahvif->vdev_type == WMI_VDEV_TYPE_AP) {
11270 			arvif->is_csa_in_progress = true;
11271 			continue;
11272 		}
11273 
11274 		ret = ath12k_mac_setup_bcn_tmpl(arvif);
11275 		if (ret)
11276 			ath12k_warn(ab, "failed to update bcn tmpl during csa: %d\n",
11277 				    ret);
11278 
11279 		memset(&params, 0, sizeof(params));
11280 		params.vdev_id = arvif->vdev_id;
11281 		params.aid = ahvif->aid;
11282 		params.bssid = arvif->bssid;
11283 		params.tx_bssid = ath12k_mac_get_tx_bssid(arvif);
11284 		if (params.tx_bssid) {
11285 			params.nontx_profile_idx = link_conf->bssid_index;
11286 			params.nontx_profile_cnt = 1 << link_conf->bssid_indicator;
11287 		}
11288 		ret = ath12k_wmi_vdev_up(arvif->ar, &params);
11289 		if (ret) {
11290 			ath12k_warn(ab, "failed to bring vdev up %d: %d\n",
11291 				    arvif->vdev_id, ret);
11292 			continue;
11293 		}
11294 	}
11295 
11296 	/* Restart the internal monitor vdev on new channel */
11297 	if (!monitor_vif && ar->monitor_vdev_created) {
11298 		if (!ath12k_mac_monitor_stop(ar))
11299 			ath12k_mac_monitor_start(ar);
11300 	}
11301 }
11302 
11303 static void
11304 ath12k_mac_update_active_vif_chan(struct ath12k *ar,
11305 				  struct ieee80211_chanctx_conf *ctx)
11306 {
11307 	struct ath12k_mac_change_chanctx_arg arg = { .ctx = ctx, .ar = ar };
11308 	struct ieee80211_hw *hw = ath12k_ar_to_hw(ar);
11309 
11310 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
11311 
11312 	ieee80211_iterate_active_interfaces_atomic(hw,
11313 						   IEEE80211_IFACE_ITER_NORMAL,
11314 						   ath12k_mac_change_chanctx_cnt_iter,
11315 						   &arg);
11316 	if (arg.n_vifs == 0)
11317 		return;
11318 
11319 	arg.vifs = kcalloc(arg.n_vifs, sizeof(arg.vifs[0]), GFP_KERNEL);
11320 	if (!arg.vifs)
11321 		return;
11322 
11323 	ieee80211_iterate_active_interfaces_atomic(hw,
11324 						   IEEE80211_IFACE_ITER_NORMAL,
11325 						   ath12k_mac_change_chanctx_fill_iter,
11326 						   &arg);
11327 
11328 	ath12k_mac_update_vif_chan(ar, arg.vifs, arg.n_vifs);
11329 
11330 	kfree(arg.vifs);
11331 }
11332 
11333 static void ath12k_mac_op_change_chanctx(struct ieee80211_hw *hw,
11334 					 struct ieee80211_chanctx_conf *ctx,
11335 					 u32 changed)
11336 {
11337 	struct ath12k *ar;
11338 	struct ath12k_base *ab;
11339 
11340 	lockdep_assert_wiphy(hw->wiphy);
11341 
11342 	ar = ath12k_get_ar_by_ctx(hw, ctx);
11343 	if (!ar)
11344 		return;
11345 
11346 	ab = ar->ab;
11347 
11348 	ath12k_dbg(ab, ATH12K_DBG_MAC,
11349 		   "mac chanctx change freq %u width %d ptr %p changed %x\n",
11350 		   ctx->def.chan->center_freq, ctx->def.width, ctx, changed);
11351 
11352 	/* This shouldn't really happen because channel switching should use
11353 	 * switch_vif_chanctx().
11354 	 */
11355 	if (WARN_ON(changed & IEEE80211_CHANCTX_CHANGE_CHANNEL))
11356 		return;
11357 
11358 	if (changed & IEEE80211_CHANCTX_CHANGE_WIDTH ||
11359 	    changed & IEEE80211_CHANCTX_CHANGE_RADAR ||
11360 	    changed & IEEE80211_CHANCTX_CHANGE_PUNCTURING)
11361 		ath12k_mac_update_active_vif_chan(ar, ctx);
11362 
11363 	/* TODO: Recalc radar detection */
11364 }
11365 
11366 static int ath12k_start_vdev_delay(struct ath12k *ar,
11367 				   struct ath12k_link_vif *arvif)
11368 {
11369 	struct ath12k_base *ab = ar->ab;
11370 	struct ath12k_vif *ahvif = arvif->ahvif;
11371 	struct ieee80211_vif *vif = ath12k_ahvif_to_vif(arvif->ahvif);
11372 	struct ieee80211_chanctx_conf *chanctx;
11373 	struct ieee80211_bss_conf *link_conf;
11374 	int ret;
11375 
11376 	if (WARN_ON(arvif->is_started))
11377 		return -EBUSY;
11378 
11379 	link_conf = ath12k_mac_get_link_bss_conf(arvif);
11380 	if (!link_conf) {
11381 		ath12k_warn(ab, "failed to get link conf for vdev %u\n", arvif->vdev_id);
11382 		return -EINVAL;
11383 	}
11384 
11385 	chanctx	= wiphy_dereference(ath12k_ar_to_hw(arvif->ar)->wiphy,
11386 				    link_conf->chanctx_conf);
11387 	ret = ath12k_mac_vdev_start(arvif, chanctx);
11388 	if (ret) {
11389 		ath12k_warn(ab, "failed to start vdev %i addr %pM on freq %d: %d\n",
11390 			    arvif->vdev_id, vif->addr,
11391 			    chanctx->def.chan->center_freq, ret);
11392 		return ret;
11393 	}
11394 
11395 	if (ahvif->vdev_type == WMI_VDEV_TYPE_MONITOR) {
11396 		ret = ath12k_monitor_vdev_up(ar, arvif->vdev_id);
11397 		if (ret) {
11398 			ath12k_warn(ab, "failed put monitor up: %d\n", ret);
11399 			return ret;
11400 		}
11401 	}
11402 
11403 	arvif->is_started = true;
11404 
11405 	/* TODO: Setup ps and cts/rts protection */
11406 	return 0;
11407 }
11408 
11409 static u8 ath12k_mac_get_num_pwr_levels(struct cfg80211_chan_def *chan_def)
11410 {
11411 	if (chan_def->chan->flags & IEEE80211_CHAN_PSD) {
11412 		switch (chan_def->width) {
11413 		case NL80211_CHAN_WIDTH_20:
11414 			return 1;
11415 		case NL80211_CHAN_WIDTH_40:
11416 			return 2;
11417 		case NL80211_CHAN_WIDTH_80:
11418 			return 4;
11419 		case NL80211_CHAN_WIDTH_160:
11420 			return 8;
11421 		case NL80211_CHAN_WIDTH_320:
11422 			return 16;
11423 		default:
11424 			return 1;
11425 		}
11426 	} else {
11427 		switch (chan_def->width) {
11428 		case NL80211_CHAN_WIDTH_20:
11429 			return 1;
11430 		case NL80211_CHAN_WIDTH_40:
11431 			return 2;
11432 		case NL80211_CHAN_WIDTH_80:
11433 			return 3;
11434 		case NL80211_CHAN_WIDTH_160:
11435 			return 4;
11436 		case NL80211_CHAN_WIDTH_320:
11437 			return 5;
11438 		default:
11439 			return 1;
11440 		}
11441 	}
11442 }
11443 
11444 static u16 ath12k_mac_get_6ghz_start_frequency(struct cfg80211_chan_def *chan_def)
11445 {
11446 	u16 diff_seq;
11447 
11448 	/* It is to get the lowest channel number's center frequency of the chan.
11449 	 * For example,
11450 	 * bandwidth=40 MHz, center frequency is 5965, lowest channel is 1
11451 	 * with center frequency 5955, its diff is 5965 - 5955 = 10.
11452 	 * bandwidth=80 MHz, center frequency is 5985, lowest channel is 1
11453 	 * with center frequency 5955, its diff is 5985 - 5955 = 30.
11454 	 * bandwidth=160 MHz, center frequency is 6025, lowest channel is 1
11455 	 * with center frequency 5955, its diff is 6025 - 5955 = 70.
11456 	 * bandwidth=320 MHz, center frequency is 6105, lowest channel is 1
11457 	 * with center frequency 5955, its diff is 6105 - 5955 = 70.
11458 	 */
11459 	switch (chan_def->width) {
11460 	case NL80211_CHAN_WIDTH_320:
11461 		diff_seq = 150;
11462 		break;
11463 	case NL80211_CHAN_WIDTH_160:
11464 		diff_seq = 70;
11465 		break;
11466 	case NL80211_CHAN_WIDTH_80:
11467 		diff_seq = 30;
11468 		break;
11469 	case NL80211_CHAN_WIDTH_40:
11470 		diff_seq = 10;
11471 		break;
11472 	default:
11473 		diff_seq = 0;
11474 	}
11475 
11476 	return chan_def->center_freq1 - diff_seq;
11477 }
11478 
11479 static u16 ath12k_mac_get_seg_freq(struct cfg80211_chan_def *chan_def,
11480 				   u16 start_seq, u8 seq)
11481 {
11482 	u16 seg_seq;
11483 
11484 	/* It is to get the center frequency of the specific bandwidth.
11485 	 * start_seq means the lowest channel number's center frequency.
11486 	 * seq 0/1/2/3 means 20 MHz/40 MHz/80 MHz/160 MHz.
11487 	 * For example,
11488 	 * lowest channel is 1, its center frequency 5955,
11489 	 * center frequency is 5955 when bandwidth=20 MHz, its diff is 5955 - 5955 = 0.
11490 	 * lowest channel is 1, its center frequency 5955,
11491 	 * center frequency is 5965 when bandwidth=40 MHz, its diff is 5965 - 5955 = 10.
11492 	 * lowest channel is 1, its center frequency 5955,
11493 	 * center frequency is 5985 when bandwidth=80 MHz, its diff is 5985 - 5955 = 30.
11494 	 * lowest channel is 1, its center frequency 5955,
11495 	 * center frequency is 6025 when bandwidth=160 MHz, its diff is 6025 - 5955 = 70.
11496 	 */
11497 	seg_seq = 10 * (BIT(seq) - 1);
11498 	return seg_seq + start_seq;
11499 }
11500 
11501 static void ath12k_mac_get_psd_channel(struct ath12k *ar,
11502 				       u16 step_freq,
11503 				       u16 *start_freq,
11504 				       u16 *center_freq,
11505 				       u8 i,
11506 				       struct ieee80211_channel **temp_chan,
11507 				       s8 *tx_power)
11508 {
11509 	/* It is to get the center frequency for each 20 MHz.
11510 	 * For example, if the chan is 160 MHz and center frequency is 6025,
11511 	 * then it include 8 channels, they are 1/5/9/13/17/21/25/29,
11512 	 * channel number 1's center frequency is 5955, it is parameter start_freq.
11513 	 * parameter i is the step of the 8 channels. i is 0~7 for the 8 channels.
11514 	 * the channel 1/5/9/13/17/21/25/29 maps i=0/1/2/3/4/5/6/7,
11515 	 * and maps its center frequency is 5955/5975/5995/6015/6035/6055/6075/6095,
11516 	 * the gap is 20 for each channel, parameter step_freq means the gap.
11517 	 * after get the center frequency of each channel, it is easy to find the
11518 	 * struct ieee80211_channel of it and get the max_reg_power.
11519 	 */
11520 	*center_freq = *start_freq + i * step_freq;
11521 	*temp_chan = ieee80211_get_channel(ar->ah->hw->wiphy, *center_freq);
11522 	*tx_power = (*temp_chan)->max_reg_power;
11523 }
11524 
11525 static void ath12k_mac_get_eirp_power(struct ath12k *ar,
11526 				      u16 *start_freq,
11527 				      u16 *center_freq,
11528 				      u8 i,
11529 				      struct ieee80211_channel **temp_chan,
11530 				      struct cfg80211_chan_def *def,
11531 				      s8 *tx_power)
11532 {
11533 	/* It is to get the center frequency for 20 MHz/40 MHz/80 MHz/
11534 	 * 160 MHz bandwidth, and then plus 10 to the center frequency,
11535 	 * it is the center frequency of a channel number.
11536 	 * For example, when configured channel number is 1.
11537 	 * center frequency is 5965 when bandwidth=40 MHz, after plus 10, it is 5975,
11538 	 * then it is channel number 5.
11539 	 * center frequency is 5985 when bandwidth=80 MHz, after plus 10, it is 5995,
11540 	 * then it is channel number 9.
11541 	 * center frequency is 6025 when bandwidth=160 MHz, after plus 10, it is 6035,
11542 	 * then it is channel number 17.
11543 	 * after get the center frequency of each channel, it is easy to find the
11544 	 * struct ieee80211_channel of it and get the max_reg_power.
11545 	 */
11546 	*center_freq = ath12k_mac_get_seg_freq(def, *start_freq, i);
11547 
11548 	/* For the 20 MHz, its center frequency is same with same channel */
11549 	if (i != 0)
11550 		*center_freq += 10;
11551 
11552 	*temp_chan = ieee80211_get_channel(ar->ah->hw->wiphy, *center_freq);
11553 	*tx_power = (*temp_chan)->max_reg_power;
11554 }
11555 
11556 void ath12k_mac_fill_reg_tpc_info(struct ath12k *ar,
11557 				  struct ath12k_link_vif *arvif,
11558 				  struct ieee80211_chanctx_conf *ctx)
11559 {
11560 	struct ath12k_base *ab = ar->ab;
11561 	struct ath12k_reg_tpc_power_info *reg_tpc_info = &arvif->reg_tpc_info;
11562 	struct ieee80211_bss_conf *bss_conf = ath12k_mac_get_link_bss_conf(arvif);
11563 	struct ieee80211_channel *chan, *temp_chan;
11564 	u8 pwr_lvl_idx, num_pwr_levels, pwr_reduction;
11565 	bool is_psd_power = false, is_tpe_present = false;
11566 	s8 max_tx_power[ATH12K_NUM_PWR_LEVELS], psd_power, tx_power;
11567 	s8 eirp_power = 0;
11568 	struct ath12k_vif *ahvif = arvif->ahvif;
11569 	u16 start_freq, center_freq;
11570 	u8 reg_6ghz_power_mode;
11571 
11572 	chan = ctx->def.chan;
11573 	start_freq = ath12k_mac_get_6ghz_start_frequency(&ctx->def);
11574 	pwr_reduction = bss_conf->pwr_reduction;
11575 
11576 	if (arvif->reg_tpc_info.num_pwr_levels) {
11577 		is_tpe_present = true;
11578 		num_pwr_levels = arvif->reg_tpc_info.num_pwr_levels;
11579 	} else {
11580 		num_pwr_levels = ath12k_mac_get_num_pwr_levels(&ctx->def);
11581 	}
11582 
11583 	for (pwr_lvl_idx = 0; pwr_lvl_idx < num_pwr_levels; pwr_lvl_idx++) {
11584 		/* STA received TPE IE*/
11585 		if (is_tpe_present) {
11586 			/* local power is PSD power*/
11587 			if (chan->flags & IEEE80211_CHAN_PSD) {
11588 				/* Connecting AP is psd power */
11589 				if (reg_tpc_info->is_psd_power) {
11590 					is_psd_power = true;
11591 					ath12k_mac_get_psd_channel(ar, 20,
11592 								   &start_freq,
11593 								   &center_freq,
11594 								   pwr_lvl_idx,
11595 								   &temp_chan,
11596 								   &tx_power);
11597 					psd_power = temp_chan->psd;
11598 					eirp_power = tx_power;
11599 					max_tx_power[pwr_lvl_idx] =
11600 						min_t(s8,
11601 						      psd_power,
11602 						      reg_tpc_info->tpe[pwr_lvl_idx]);
11603 				/* Connecting AP is not psd power */
11604 				} else {
11605 					ath12k_mac_get_eirp_power(ar,
11606 								  &start_freq,
11607 								  &center_freq,
11608 								  pwr_lvl_idx,
11609 								  &temp_chan,
11610 								  &ctx->def,
11611 								  &tx_power);
11612 					psd_power = temp_chan->psd;
11613 					/* convert psd power to EIRP power based
11614 					 * on channel width
11615 					 */
11616 					tx_power =
11617 						min_t(s8, tx_power,
11618 						      psd_power + 13 + pwr_lvl_idx * 3);
11619 					max_tx_power[pwr_lvl_idx] =
11620 						min_t(s8,
11621 						      tx_power,
11622 						      reg_tpc_info->tpe[pwr_lvl_idx]);
11623 				}
11624 			/* local power is not PSD power */
11625 			} else {
11626 				/* Connecting AP is psd power */
11627 				if (reg_tpc_info->is_psd_power) {
11628 					is_psd_power = true;
11629 					ath12k_mac_get_psd_channel(ar, 20,
11630 								   &start_freq,
11631 								   &center_freq,
11632 								   pwr_lvl_idx,
11633 								   &temp_chan,
11634 								   &tx_power);
11635 					eirp_power = tx_power;
11636 					max_tx_power[pwr_lvl_idx] =
11637 						reg_tpc_info->tpe[pwr_lvl_idx];
11638 				/* Connecting AP is not psd power */
11639 				} else {
11640 					ath12k_mac_get_eirp_power(ar,
11641 								  &start_freq,
11642 								  &center_freq,
11643 								  pwr_lvl_idx,
11644 								  &temp_chan,
11645 								  &ctx->def,
11646 								  &tx_power);
11647 					max_tx_power[pwr_lvl_idx] =
11648 						min_t(s8,
11649 						      tx_power,
11650 						      reg_tpc_info->tpe[pwr_lvl_idx]);
11651 				}
11652 			}
11653 		/* STA not received TPE IE */
11654 		} else {
11655 			/* local power is PSD power*/
11656 			if (chan->flags & IEEE80211_CHAN_PSD) {
11657 				is_psd_power = true;
11658 				ath12k_mac_get_psd_channel(ar, 20,
11659 							   &start_freq,
11660 							   &center_freq,
11661 							   pwr_lvl_idx,
11662 							   &temp_chan,
11663 							   &tx_power);
11664 				psd_power = temp_chan->psd;
11665 				eirp_power = tx_power;
11666 				max_tx_power[pwr_lvl_idx] = psd_power;
11667 			} else {
11668 				ath12k_mac_get_eirp_power(ar,
11669 							  &start_freq,
11670 							  &center_freq,
11671 							  pwr_lvl_idx,
11672 							  &temp_chan,
11673 							  &ctx->def,
11674 							  &tx_power);
11675 				max_tx_power[pwr_lvl_idx] = tx_power;
11676 			}
11677 		}
11678 
11679 		if (is_psd_power) {
11680 			/* If AP local power constraint is present */
11681 			if (pwr_reduction)
11682 				eirp_power = eirp_power - pwr_reduction;
11683 
11684 			/* If firmware updated max tx power is non zero, then take
11685 			 * the min of firmware updated ap tx power
11686 			 * and max power derived from above mentioned parameters.
11687 			 */
11688 			ath12k_dbg(ab, ATH12K_DBG_MAC,
11689 				   "eirp power : %d firmware report power : %d\n",
11690 				   eirp_power, ar->max_allowed_tx_power);
11691 			/* Firmware reports lower max_allowed_tx_power during vdev
11692 			 * start response. In case of 6 GHz, firmware is not aware
11693 			 * of EIRP power unless driver sets EIRP power through WMI
11694 			 * TPC command. So radio which does not support idle power
11695 			 * save can set maximum calculated EIRP power directly to
11696 			 * firmware through TPC command without min comparison with
11697 			 * vdev start response's max_allowed_tx_power.
11698 			 */
11699 			if (ar->max_allowed_tx_power && ab->hw_params->idle_ps)
11700 				eirp_power = min_t(s8,
11701 						   eirp_power,
11702 						   ar->max_allowed_tx_power);
11703 		} else {
11704 			/* If AP local power constraint is present */
11705 			if (pwr_reduction)
11706 				max_tx_power[pwr_lvl_idx] =
11707 					max_tx_power[pwr_lvl_idx] - pwr_reduction;
11708 			/* If firmware updated max tx power is non zero, then take
11709 			 * the min of firmware updated ap tx power
11710 			 * and max power derived from above mentioned parameters.
11711 			 */
11712 			if (ar->max_allowed_tx_power && ab->hw_params->idle_ps)
11713 				max_tx_power[pwr_lvl_idx] =
11714 					min_t(s8,
11715 					      max_tx_power[pwr_lvl_idx],
11716 					      ar->max_allowed_tx_power);
11717 		}
11718 		reg_tpc_info->chan_power_info[pwr_lvl_idx].chan_cfreq = center_freq;
11719 		reg_tpc_info->chan_power_info[pwr_lvl_idx].tx_power =
11720 			max_tx_power[pwr_lvl_idx];
11721 	}
11722 
11723 	reg_tpc_info->num_pwr_levels = num_pwr_levels;
11724 	reg_tpc_info->is_psd_power = is_psd_power;
11725 	reg_tpc_info->eirp_power = eirp_power;
11726 	if (ahvif->vdev_type == WMI_VDEV_TYPE_STA)
11727 		reg_6ghz_power_mode = bss_conf->power_type;
11728 	else
11729 		/* For now, LPI is the only supported AP power mode */
11730 		reg_6ghz_power_mode = IEEE80211_REG_LPI_AP;
11731 
11732 	reg_tpc_info->ap_power_type =
11733 		ath12k_reg_ap_pwr_convert(reg_6ghz_power_mode);
11734 }
11735 
11736 static void ath12k_mac_parse_tx_pwr_env(struct ath12k *ar,
11737 					struct ath12k_link_vif *arvif)
11738 {
11739 	struct ieee80211_bss_conf *bss_conf = ath12k_mac_get_link_bss_conf(arvif);
11740 	struct ath12k_reg_tpc_power_info *tpc_info = &arvif->reg_tpc_info;
11741 	struct ieee80211_parsed_tpe_eirp *local_non_psd, *reg_non_psd;
11742 	struct ieee80211_parsed_tpe_psd *local_psd, *reg_psd;
11743 	struct ieee80211_parsed_tpe *tpe = &bss_conf->tpe;
11744 	enum wmi_reg_6g_client_type client_type;
11745 	struct ath12k_reg_info *reg_info;
11746 	struct ath12k_base *ab = ar->ab;
11747 	bool psd_valid, non_psd_valid;
11748 	int i;
11749 
11750 	reg_info = ab->reg_info[ar->pdev_idx];
11751 	client_type = reg_info->client_type;
11752 
11753 	local_psd = &tpe->psd_local[client_type];
11754 	reg_psd = &tpe->psd_reg_client[client_type];
11755 	local_non_psd = &tpe->max_local[client_type];
11756 	reg_non_psd = &tpe->max_reg_client[client_type];
11757 
11758 	psd_valid = local_psd->valid | reg_psd->valid;
11759 	non_psd_valid = local_non_psd->valid | reg_non_psd->valid;
11760 
11761 	if (!psd_valid && !non_psd_valid) {
11762 		ath12k_warn(ab,
11763 			    "no transmit power envelope match client power type %d\n",
11764 			    client_type);
11765 		return;
11766 	}
11767 
11768 	if (psd_valid) {
11769 		tpc_info->is_psd_power = true;
11770 
11771 		tpc_info->num_pwr_levels = max(local_psd->count,
11772 					       reg_psd->count);
11773 		tpc_info->num_pwr_levels =
11774 				min3(tpc_info->num_pwr_levels,
11775 				     IEEE80211_TPE_PSD_ENTRIES_320MHZ,
11776 				     ATH12K_NUM_PWR_LEVELS);
11777 
11778 		for (i = 0; i < tpc_info->num_pwr_levels; i++) {
11779 			tpc_info->tpe[i] = min(local_psd->power[i],
11780 					       reg_psd->power[i]) / 2;
11781 			ath12k_dbg(ab, ATH12K_DBG_MAC,
11782 				   "TPE PSD power[%d] : %d\n",
11783 				   i, tpc_info->tpe[i]);
11784 		}
11785 	} else {
11786 		tpc_info->is_psd_power = false;
11787 		tpc_info->eirp_power = 0;
11788 
11789 		tpc_info->num_pwr_levels = max(local_non_psd->count,
11790 					       reg_non_psd->count);
11791 		tpc_info->num_pwr_levels =
11792 				min3(tpc_info->num_pwr_levels,
11793 				     IEEE80211_TPE_EIRP_ENTRIES_320MHZ,
11794 				     ATH12K_NUM_PWR_LEVELS);
11795 
11796 		for (i = 0; i < tpc_info->num_pwr_levels; i++) {
11797 			tpc_info->tpe[i] = min(local_non_psd->power[i],
11798 					       reg_non_psd->power[i]) / 2;
11799 			ath12k_dbg(ab, ATH12K_DBG_MAC,
11800 				   "non PSD power[%d] : %d\n",
11801 				   i, tpc_info->tpe[i]);
11802 		}
11803 	}
11804 }
11805 
11806 static int
11807 ath12k_mac_op_assign_vif_chanctx(struct ieee80211_hw *hw,
11808 				 struct ieee80211_vif *vif,
11809 				 struct ieee80211_bss_conf *link_conf,
11810 				 struct ieee80211_chanctx_conf *ctx)
11811 {
11812 	struct ath12k_hw *ah = ath12k_hw_to_ah(hw);
11813 	struct ath12k *ar;
11814 	struct ath12k_base *ab;
11815 	struct ath12k_vif *ahvif = ath12k_vif_to_ahvif(vif);
11816 	u8 link_id = link_conf->link_id;
11817 	struct ath12k_link_vif *arvif;
11818 	int ret;
11819 
11820 	lockdep_assert_wiphy(hw->wiphy);
11821 
11822 	/* For multi radio wiphy, the vdev was not created during add_interface
11823 	 * create now since we have a channel ctx now to assign to a specific ar/fw
11824 	 */
11825 	arvif = ath12k_mac_assign_link_vif(ah, vif, link_id);
11826 	if (!arvif) {
11827 		WARN_ON(1);
11828 		return -ENOMEM;
11829 	}
11830 
11831 	ar = ath12k_mac_assign_vif_to_vdev(hw, arvif, ctx);
11832 	if (!ar) {
11833 		ath12k_hw_warn(ah, "failed to assign chanctx for vif %pM link id %u link vif is already started",
11834 			       vif->addr, link_id);
11835 		return -EINVAL;
11836 	}
11837 
11838 	ab = ar->ab;
11839 
11840 	ath12k_dbg(ab, ATH12K_DBG_MAC,
11841 		   "mac chanctx assign ptr %p vdev_id %i\n",
11842 		   ctx, arvif->vdev_id);
11843 
11844 	if (ath12k_wmi_supports_6ghz_cc_ext(ar) &&
11845 	    ctx->def.chan->band == NL80211_BAND_6GHZ &&
11846 	    ahvif->vdev_type == WMI_VDEV_TYPE_STA)
11847 		ath12k_mac_parse_tx_pwr_env(ar, arvif);
11848 
11849 	arvif->punct_bitmap = ctx->def.punctured;
11850 
11851 	/* for some targets bss peer must be created before vdev_start */
11852 	if (ab->hw_params->vdev_start_delay &&
11853 	    ahvif->vdev_type != WMI_VDEV_TYPE_AP &&
11854 	    ahvif->vdev_type != WMI_VDEV_TYPE_MONITOR &&
11855 	    !ath12k_peer_exist_by_vdev_id(ab, arvif->vdev_id)) {
11856 		ret = 0;
11857 		goto out;
11858 	}
11859 
11860 	if (WARN_ON(arvif->is_started)) {
11861 		ret = -EBUSY;
11862 		goto out;
11863 	}
11864 
11865 	if (ahvif->vdev_type == WMI_VDEV_TYPE_MONITOR) {
11866 		ret = ath12k_mac_monitor_start(ar);
11867 		if (ret) {
11868 			ath12k_mac_monitor_vdev_delete(ar);
11869 			goto out;
11870 		}
11871 
11872 		arvif->is_started = true;
11873 		goto out;
11874 	}
11875 
11876 	ret = ath12k_mac_vdev_start(arvif, ctx);
11877 	if (ret) {
11878 		ath12k_warn(ab, "failed to start vdev %i addr %pM on freq %d: %d\n",
11879 			    arvif->vdev_id, vif->addr,
11880 			    ctx->def.chan->center_freq, ret);
11881 		goto out;
11882 	}
11883 
11884 	arvif->is_started = true;
11885 
11886 	/* TODO: Setup ps and cts/rts protection */
11887 
11888 out:
11889 	return ret;
11890 }
11891 
11892 static void
11893 ath12k_mac_op_unassign_vif_chanctx(struct ieee80211_hw *hw,
11894 				   struct ieee80211_vif *vif,
11895 				   struct ieee80211_bss_conf *link_conf,
11896 				   struct ieee80211_chanctx_conf *ctx)
11897 {
11898 	struct ath12k *ar;
11899 	struct ath12k_base *ab;
11900 	struct ath12k_vif *ahvif = ath12k_vif_to_ahvif(vif);
11901 	struct ath12k_link_vif *arvif;
11902 	u8 link_id = link_conf->link_id;
11903 	int ret;
11904 
11905 	lockdep_assert_wiphy(hw->wiphy);
11906 
11907 	arvif = wiphy_dereference(hw->wiphy, ahvif->link[link_id]);
11908 
11909 	/* The vif is expected to be attached to an ar's VDEV.
11910 	 * We leave the vif/vdev in this function as is
11911 	 * and not delete the vdev symmetric to assign_vif_chanctx()
11912 	 * the VDEV will be deleted and unassigned either during
11913 	 * remove_interface() or when there is a change in channel
11914 	 * that moves the vif to a new ar
11915 	 */
11916 	if (!arvif || !arvif->is_created)
11917 		return;
11918 
11919 	ar = arvif->ar;
11920 	ab = ar->ab;
11921 
11922 	ath12k_dbg(ab, ATH12K_DBG_MAC,
11923 		   "mac chanctx unassign ptr %p vdev_id %i\n",
11924 		   ctx, arvif->vdev_id);
11925 
11926 	WARN_ON(!arvif->is_started);
11927 
11928 	if (ahvif->vdev_type == WMI_VDEV_TYPE_MONITOR) {
11929 		ret = ath12k_mac_monitor_stop(ar);
11930 		if (ret)
11931 			return;
11932 
11933 		arvif->is_started = false;
11934 	}
11935 
11936 	if (ahvif->vdev_type != WMI_VDEV_TYPE_STA &&
11937 	    ahvif->vdev_type != WMI_VDEV_TYPE_MONITOR) {
11938 		ath12k_bss_disassoc(ar, arvif);
11939 		ret = ath12k_mac_vdev_stop(arvif);
11940 		if (ret)
11941 			ath12k_warn(ab, "failed to stop vdev %i: %d\n",
11942 				    arvif->vdev_id, ret);
11943 	}
11944 	arvif->is_started = false;
11945 
11946 	if (test_bit(WMI_TLV_SERVICE_11D_OFFLOAD, ab->wmi_ab.svc_map) &&
11947 	    ahvif->vdev_type == WMI_VDEV_TYPE_STA &&
11948 	    ahvif->vdev_subtype == WMI_VDEV_SUBTYPE_NONE &&
11949 	    ar->state_11d != ATH12K_11D_PREPARING) {
11950 		reinit_completion(&ar->completed_11d_scan);
11951 		ar->state_11d = ATH12K_11D_PREPARING;
11952 	}
11953 
11954 	if (ar->scan.arvif == arvif && ar->scan.state == ATH12K_SCAN_RUNNING) {
11955 		ath12k_scan_abort(ar);
11956 		ar->scan.arvif = NULL;
11957 	}
11958 }
11959 
11960 static int
11961 ath12k_mac_op_switch_vif_chanctx(struct ieee80211_hw *hw,
11962 				 struct ieee80211_vif_chanctx_switch *vifs,
11963 				 int n_vifs,
11964 				 enum ieee80211_chanctx_switch_mode mode)
11965 {
11966 	struct ath12k *ar;
11967 
11968 	lockdep_assert_wiphy(hw->wiphy);
11969 
11970 	ar = ath12k_get_ar_by_ctx(hw, vifs->old_ctx);
11971 	if (!ar)
11972 		return -EINVAL;
11973 
11974 	/* Switching channels across radio is not allowed */
11975 	if (ar != ath12k_get_ar_by_ctx(hw, vifs->new_ctx))
11976 		return -EINVAL;
11977 
11978 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
11979 		   "mac chanctx switch n_vifs %d mode %d\n",
11980 		   n_vifs, mode);
11981 	ath12k_mac_update_vif_chan(ar, vifs, n_vifs);
11982 
11983 	return 0;
11984 }
11985 
11986 static int
11987 ath12k_set_vdev_param_to_all_vifs(struct ath12k *ar, int param, u32 value)
11988 {
11989 	struct ath12k_link_vif *arvif;
11990 	int ret = 0;
11991 
11992 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
11993 
11994 	list_for_each_entry(arvif, &ar->arvifs, list) {
11995 		ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "setting mac vdev %d param %d value %d\n",
11996 			   param, arvif->vdev_id, value);
11997 
11998 		ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
11999 						    param, value);
12000 		if (ret) {
12001 			ath12k_warn(ar->ab, "failed to set param %d for vdev %d: %d\n",
12002 				    param, arvif->vdev_id, ret);
12003 			break;
12004 		}
12005 	}
12006 
12007 	return ret;
12008 }
12009 
12010 /* mac80211 stores device specific RTS/Fragmentation threshold value,
12011  * this is set interface specific to firmware from ath12k driver
12012  */
12013 static int ath12k_mac_op_set_rts_threshold(struct ieee80211_hw *hw,
12014 					   int radio_idx, u32 value)
12015 {
12016 	struct ath12k_hw *ah = ath12k_hw_to_ah(hw);
12017 	struct wiphy *wiphy = hw->wiphy;
12018 	struct ath12k *ar;
12019 	int param_id = WMI_VDEV_PARAM_RTS_THRESHOLD;
12020 	int ret = 0, ret_err, i;
12021 
12022 	lockdep_assert_wiphy(hw->wiphy);
12023 
12024 	if (radio_idx >= wiphy->n_radio || radio_idx < -1)
12025 		return -EINVAL;
12026 
12027 	if (radio_idx != -1) {
12028 		/* Update RTS threshold in specified radio */
12029 		ar = ath12k_ah_to_ar(ah, radio_idx);
12030 		ret = ath12k_set_vdev_param_to_all_vifs(ar, param_id, value);
12031 		if (ret) {
12032 			ath12k_warn(ar->ab,
12033 				    "failed to set RTS config for all vdevs of pdev %d",
12034 				    ar->pdev->pdev_id);
12035 			return ret;
12036 		}
12037 
12038 		ar->rts_threshold = value;
12039 		return 0;
12040 	}
12041 
12042 	/* Radio_index passed is -1, so set RTS threshold for all radios. */
12043 	for_each_ar(ah, ar, i) {
12044 		ret = ath12k_set_vdev_param_to_all_vifs(ar, param_id, value);
12045 		if (ret) {
12046 			ath12k_warn(ar->ab, "failed to set RTS config for all vdevs of pdev %d",
12047 				    ar->pdev->pdev_id);
12048 			break;
12049 		}
12050 	}
12051 	if (!ret) {
12052 		/* Setting new RTS threshold for vdevs of all radios passed, so update
12053 		 * the RTS threshold value for all radios
12054 		 */
12055 		for_each_ar(ah, ar, i)
12056 			ar->rts_threshold = value;
12057 		return 0;
12058 	}
12059 
12060 	/* RTS threshold config failed, revert to the previous RTS threshold */
12061 	for (i = i - 1; i >= 0; i--) {
12062 		ar = ath12k_ah_to_ar(ah, i);
12063 		ret_err = ath12k_set_vdev_param_to_all_vifs(ar, param_id,
12064 							    ar->rts_threshold);
12065 		if (ret_err)
12066 			ath12k_warn(ar->ab,
12067 				    "failed to restore RTS threshold for all vdevs of pdev %d",
12068 				    ar->pdev->pdev_id);
12069 	}
12070 
12071 	return ret;
12072 }
12073 
12074 static int ath12k_mac_op_set_frag_threshold(struct ieee80211_hw *hw,
12075 					    int radio_idx, u32 value)
12076 {
12077 	/* Even though there's a WMI vdev param for fragmentation threshold no
12078 	 * known firmware actually implements it. Moreover it is not possible to
12079 	 * rely frame fragmentation to mac80211 because firmware clears the
12080 	 * "more fragments" bit in frame control making it impossible for remote
12081 	 * devices to reassemble frames.
12082 	 *
12083 	 * Hence implement a dummy callback just to say fragmentation isn't
12084 	 * supported. This effectively prevents mac80211 from doing frame
12085 	 * fragmentation in software.
12086 	 */
12087 
12088 	lockdep_assert_wiphy(hw->wiphy);
12089 
12090 	return -EOPNOTSUPP;
12091 }
12092 
12093 static int ath12k_mac_flush(struct ath12k *ar)
12094 {
12095 	long time_left;
12096 	int ret = 0;
12097 
12098 	time_left = wait_event_timeout(ar->dp.tx_empty_waitq,
12099 				       (atomic_read(&ar->dp.num_tx_pending) == 0),
12100 				       ATH12K_FLUSH_TIMEOUT);
12101 	if (time_left == 0) {
12102 		ath12k_warn(ar->ab,
12103 			    "failed to flush transmit queue, data pkts pending %d\n",
12104 			    atomic_read(&ar->dp.num_tx_pending));
12105 		ret = -ETIMEDOUT;
12106 	}
12107 
12108 	time_left = wait_event_timeout(ar->txmgmt_empty_waitq,
12109 				       (atomic_read(&ar->num_pending_mgmt_tx) == 0),
12110 				       ATH12K_FLUSH_TIMEOUT);
12111 	if (time_left == 0) {
12112 		ath12k_warn(ar->ab,
12113 			    "failed to flush mgmt transmit queue, mgmt pkts pending %d\n",
12114 			    atomic_read(&ar->num_pending_mgmt_tx));
12115 		ret = -ETIMEDOUT;
12116 	}
12117 
12118 	return ret;
12119 }
12120 
12121 int ath12k_mac_wait_tx_complete(struct ath12k *ar)
12122 {
12123 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
12124 
12125 	ath12k_mac_drain_tx(ar);
12126 	return ath12k_mac_flush(ar);
12127 }
12128 
12129 static void ath12k_mac_op_flush(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
12130 				u32 queues, bool drop)
12131 {
12132 	struct ath12k_hw *ah = ath12k_hw_to_ah(hw);
12133 	struct ath12k_link_vif *arvif;
12134 	struct ath12k_vif *ahvif;
12135 	unsigned long links;
12136 	struct ath12k *ar;
12137 	u8 link_id;
12138 	int i;
12139 
12140 	lockdep_assert_wiphy(hw->wiphy);
12141 
12142 	if (drop)
12143 		return;
12144 
12145 	/* vif can be NULL when flush() is considered for hw */
12146 	if (!vif) {
12147 		for_each_ar(ah, ar, i)
12148 			ath12k_mac_flush(ar);
12149 		return;
12150 	}
12151 
12152 	for_each_ar(ah, ar, i)
12153 		wiphy_work_flush(hw->wiphy, &ar->wmi_mgmt_tx_work);
12154 
12155 	ahvif = ath12k_vif_to_ahvif(vif);
12156 	links = ahvif->links_map;
12157 	for_each_set_bit(link_id, &links, IEEE80211_MLD_MAX_NUM_LINKS) {
12158 		arvif = wiphy_dereference(hw->wiphy, ahvif->link[link_id]);
12159 		if (!(arvif && arvif->ar))
12160 			continue;
12161 
12162 		ath12k_mac_flush(arvif->ar);
12163 	}
12164 }
12165 
12166 static int
12167 ath12k_mac_bitrate_mask_num_ht_rates(struct ath12k *ar,
12168 				     enum nl80211_band band,
12169 				     const struct cfg80211_bitrate_mask *mask)
12170 {
12171 	int num_rates = 0;
12172 	int i;
12173 
12174 	for (i = 0; i < ARRAY_SIZE(mask->control[band].ht_mcs); i++)
12175 		num_rates += hweight16(mask->control[band].ht_mcs[i]);
12176 
12177 	return num_rates;
12178 }
12179 
12180 static bool
12181 ath12k_mac_has_single_legacy_rate(struct ath12k *ar,
12182 				  enum nl80211_band band,
12183 				  const struct cfg80211_bitrate_mask *mask)
12184 {
12185 	int num_rates = 0;
12186 
12187 	num_rates = hweight32(mask->control[band].legacy);
12188 
12189 	if (ath12k_mac_bitrate_mask_num_ht_rates(ar, band, mask))
12190 		return false;
12191 
12192 	if (ath12k_mac_bitrate_mask_num_vht_rates(ar, band, mask))
12193 		return false;
12194 
12195 	if (ath12k_mac_bitrate_mask_num_he_rates(ar, band, mask))
12196 		return false;
12197 
12198 	if (ath12k_mac_bitrate_mask_num_eht_rates(ar, band, mask))
12199 		return false;
12200 
12201 	return num_rates == 1;
12202 }
12203 
12204 static __le16
12205 ath12k_mac_get_tx_mcs_map(const struct ieee80211_sta_he_cap *he_cap)
12206 {
12207 	if (he_cap->he_cap_elem.phy_cap_info[0] &
12208 	    IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_160MHZ_IN_5G)
12209 		return he_cap->he_mcs_nss_supp.tx_mcs_160;
12210 
12211 	return he_cap->he_mcs_nss_supp.tx_mcs_80;
12212 }
12213 
12214 static bool
12215 ath12k_mac_bitrate_mask_get_single_nss(struct ath12k *ar,
12216 				       struct ieee80211_vif *vif,
12217 				       enum nl80211_band band,
12218 				       const struct cfg80211_bitrate_mask *mask,
12219 				       int *nss)
12220 {
12221 	struct ieee80211_supported_band *sband = &ar->mac.sbands[band];
12222 	u16 vht_mcs_map = le16_to_cpu(sband->vht_cap.vht_mcs.tx_mcs_map);
12223 	const struct ieee80211_sband_iftype_data *data;
12224 	const struct ieee80211_sta_he_cap *he_cap;
12225 	u16 he_mcs_map = 0;
12226 	u16 eht_mcs_map = 0;
12227 	u8 ht_nss_mask = 0;
12228 	u8 vht_nss_mask = 0;
12229 	u8 he_nss_mask = 0;
12230 	u8 eht_nss_mask = 0;
12231 	u8 mcs_nss_len;
12232 	int i;
12233 
12234 	/* No need to consider legacy here. Basic rates are always present
12235 	 * in bitrate mask
12236 	 */
12237 
12238 	for (i = 0; i < ARRAY_SIZE(mask->control[band].ht_mcs); i++) {
12239 		if (mask->control[band].ht_mcs[i] == 0)
12240 			continue;
12241 		else if (mask->control[band].ht_mcs[i] ==
12242 			 sband->ht_cap.mcs.rx_mask[i])
12243 			ht_nss_mask |= BIT(i);
12244 		else
12245 			return false;
12246 	}
12247 
12248 	for (i = 0; i < ARRAY_SIZE(mask->control[band].vht_mcs); i++) {
12249 		if (mask->control[band].vht_mcs[i] == 0)
12250 			continue;
12251 		else if (mask->control[band].vht_mcs[i] ==
12252 			 ath12k_mac_get_max_vht_mcs_map(vht_mcs_map, i))
12253 			vht_nss_mask |= BIT(i);
12254 		else
12255 			return false;
12256 	}
12257 
12258 	he_cap = ieee80211_get_he_iftype_cap_vif(sband, vif);
12259 	if (!he_cap)
12260 		return false;
12261 
12262 	he_mcs_map = le16_to_cpu(ath12k_mac_get_tx_mcs_map(he_cap));
12263 
12264 	for (i = 0; i < ARRAY_SIZE(mask->control[band].he_mcs); i++) {
12265 		if (mask->control[band].he_mcs[i] == 0)
12266 			continue;
12267 
12268 		if (mask->control[band].he_mcs[i] ==
12269 		    ath12k_mac_get_max_he_mcs_map(he_mcs_map, i))
12270 			he_nss_mask |= BIT(i);
12271 		else
12272 			return false;
12273 	}
12274 
12275 	data = ieee80211_get_sband_iftype_data(sband, vif->type);
12276 
12277 	mcs_nss_len = ieee80211_eht_mcs_nss_size(&data->he_cap.he_cap_elem,
12278 						 &data->eht_cap.eht_cap_elem,
12279 						 false);
12280 	if (mcs_nss_len == 4) {
12281 		/* 20 MHz only STA case */
12282 		const struct ieee80211_eht_mcs_nss_supp_20mhz_only *eht_mcs_nss =
12283 			&data->eht_cap.eht_mcs_nss_supp.only_20mhz;
12284 		if (eht_mcs_nss->rx_tx_mcs13_max_nss)
12285 			eht_mcs_map = 0x1fff;
12286 		else if (eht_mcs_nss->rx_tx_mcs11_max_nss)
12287 			eht_mcs_map = 0x07ff;
12288 		else if (eht_mcs_nss->rx_tx_mcs9_max_nss)
12289 			eht_mcs_map = 0x01ff;
12290 		else
12291 			eht_mcs_map = 0x007f;
12292 	} else {
12293 		const struct ieee80211_eht_mcs_nss_supp_bw *eht_mcs_nss;
12294 
12295 		switch (mcs_nss_len) {
12296 		case 9:
12297 			eht_mcs_nss = &data->eht_cap.eht_mcs_nss_supp.bw._320;
12298 			break;
12299 		case 6:
12300 			eht_mcs_nss = &data->eht_cap.eht_mcs_nss_supp.bw._160;
12301 			break;
12302 		case 3:
12303 			eht_mcs_nss = &data->eht_cap.eht_mcs_nss_supp.bw._80;
12304 			break;
12305 		default:
12306 			return false;
12307 		}
12308 
12309 		if (eht_mcs_nss->rx_tx_mcs13_max_nss)
12310 			eht_mcs_map = 0x1fff;
12311 		else if (eht_mcs_nss->rx_tx_mcs11_max_nss)
12312 			eht_mcs_map = 0x7ff;
12313 		else
12314 			eht_mcs_map = 0x1ff;
12315 	}
12316 
12317 	for (i = 0; i < ARRAY_SIZE(mask->control[band].eht_mcs); i++) {
12318 		if (mask->control[band].eht_mcs[i] == 0)
12319 			continue;
12320 
12321 		if (mask->control[band].eht_mcs[i] < eht_mcs_map)
12322 			eht_nss_mask |= BIT(i);
12323 		else
12324 			return false;
12325 	}
12326 
12327 	if (ht_nss_mask != vht_nss_mask || ht_nss_mask != he_nss_mask ||
12328 	    ht_nss_mask != eht_nss_mask)
12329 		return false;
12330 
12331 	if (ht_nss_mask == 0)
12332 		return false;
12333 
12334 	if (BIT(fls(ht_nss_mask)) - 1 != ht_nss_mask)
12335 		return false;
12336 
12337 	*nss = fls(ht_nss_mask);
12338 
12339 	return true;
12340 }
12341 
12342 static int
12343 ath12k_mac_get_single_legacy_rate(struct ath12k *ar,
12344 				  enum nl80211_band band,
12345 				  const struct cfg80211_bitrate_mask *mask,
12346 				  u32 *rate, u8 *nss)
12347 {
12348 	int rate_idx;
12349 	u16 bitrate;
12350 	u8 preamble;
12351 	u8 hw_rate;
12352 
12353 	if (hweight32(mask->control[band].legacy) != 1)
12354 		return -EINVAL;
12355 
12356 	rate_idx = ffs(mask->control[band].legacy) - 1;
12357 
12358 	if (band == NL80211_BAND_5GHZ || band == NL80211_BAND_6GHZ)
12359 		rate_idx += ATH12K_MAC_FIRST_OFDM_RATE_IDX;
12360 
12361 	hw_rate = ath12k_legacy_rates[rate_idx].hw_value;
12362 	bitrate = ath12k_legacy_rates[rate_idx].bitrate;
12363 
12364 	if (ath12k_mac_bitrate_is_cck(bitrate))
12365 		preamble = WMI_RATE_PREAMBLE_CCK;
12366 	else
12367 		preamble = WMI_RATE_PREAMBLE_OFDM;
12368 
12369 	*nss = 1;
12370 	*rate = ATH12K_HW_RATE_CODE(hw_rate, 0, preamble);
12371 
12372 	return 0;
12373 }
12374 
12375 static int
12376 ath12k_mac_set_fixed_rate_gi_ltf(struct ath12k_link_vif *arvif, u8 gi, u8 ltf,
12377 				 u32 param)
12378 {
12379 	struct ath12k *ar = arvif->ar;
12380 	int ret;
12381 
12382 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
12383 
12384 	/* 0.8 = 0, 1.6 = 2 and 3.2 = 3. */
12385 	if (gi && gi != 0xFF)
12386 		gi += 1;
12387 
12388 	ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
12389 					    WMI_VDEV_PARAM_SGI, gi);
12390 	if (ret) {
12391 		ath12k_warn(ar->ab, "failed to set GI:%d, error:%d\n",
12392 			    gi, ret);
12393 		return ret;
12394 	}
12395 
12396 	if (param == WMI_VDEV_PARAM_HE_LTF) {
12397 		/* HE values start from 1 */
12398 		if (ltf != 0xFF)
12399 			ltf += 1;
12400 	} else {
12401 		/* EHT values start from 5 */
12402 		if (ltf != 0xFF)
12403 			ltf += 4;
12404 	}
12405 
12406 	ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
12407 					    param, ltf);
12408 	if (ret) {
12409 		ath12k_warn(ar->ab, "failed to set LTF:%d, error:%d\n",
12410 			    ltf, ret);
12411 		return ret;
12412 	}
12413 	return 0;
12414 }
12415 
12416 static int
12417 ath12k_mac_set_auto_rate_gi_ltf(struct ath12k_link_vif *arvif, u16 gi, u8 ltf)
12418 {
12419 	struct ath12k *ar = arvif->ar;
12420 	int ret;
12421 	u32 ar_gi_ltf;
12422 
12423 	if (gi != 0xFF) {
12424 		switch (gi) {
12425 		case ATH12K_RATE_INFO_GI_0_8:
12426 			gi = WMI_AUTORATE_800NS_GI;
12427 			break;
12428 		case ATH12K_RATE_INFO_GI_1_6:
12429 			gi = WMI_AUTORATE_1600NS_GI;
12430 			break;
12431 		case ATH12K_RATE_INFO_GI_3_2:
12432 			gi = WMI_AUTORATE_3200NS_GI;
12433 			break;
12434 		default:
12435 			ath12k_warn(ar->ab, "Invalid GI\n");
12436 			return -EINVAL;
12437 		}
12438 	}
12439 
12440 	if (ltf != 0xFF) {
12441 		switch (ltf) {
12442 		case ATH12K_RATE_INFO_1XLTF:
12443 			ltf = WMI_AUTORATE_LTF_1X;
12444 			break;
12445 		case ATH12K_RATE_INFO_2XLTF:
12446 			ltf = WMI_AUTORATE_LTF_2X;
12447 			break;
12448 		case ATH12K_RATE_INFO_4XLTF:
12449 			ltf = WMI_AUTORATE_LTF_4X;
12450 			break;
12451 		default:
12452 			ath12k_warn(ar->ab, "Invalid LTF\n");
12453 			return -EINVAL;
12454 		}
12455 	}
12456 
12457 	ar_gi_ltf = gi | ltf;
12458 
12459 	ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
12460 					    WMI_VDEV_PARAM_AUTORATE_MISC_CFG,
12461 					    ar_gi_ltf);
12462 	if (ret) {
12463 		ath12k_warn(ar->ab,
12464 			    "failed to set autorate GI:%u, LTF:%u params, error:%d\n",
12465 			    gi, ltf, ret);
12466 		return ret;
12467 	}
12468 
12469 	return 0;
12470 }
12471 
12472 static u32 ath12k_mac_nlgi_to_wmigi(enum nl80211_txrate_gi gi)
12473 {
12474 	switch (gi) {
12475 	case NL80211_TXRATE_DEFAULT_GI:
12476 		return WMI_GI_400_NS;
12477 	case NL80211_TXRATE_FORCE_LGI:
12478 		return WMI_GI_800_NS;
12479 	default:
12480 		return WMI_GI_400_NS;
12481 	}
12482 }
12483 
12484 static int ath12k_mac_set_rate_params(struct ath12k_link_vif *arvif,
12485 				      u32 rate, u8 nss, u8 sgi, u8 ldpc,
12486 				      u8 he_gi, u8 he_ltf, bool he_fixed_rate,
12487 				      u8 eht_gi, u8 eht_ltf,
12488 				      bool eht_fixed_rate)
12489 {
12490 	struct ieee80211_bss_conf *link_conf;
12491 	struct ath12k *ar = arvif->ar;
12492 	bool he_support, eht_support, gi_ltf_set = false;
12493 	u32 vdev_param;
12494 	u32 param_value;
12495 	int ret;
12496 
12497 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
12498 
12499 	link_conf = ath12k_mac_get_link_bss_conf(arvif);
12500 	if (!link_conf)
12501 		return -EINVAL;
12502 
12503 	he_support = link_conf->he_support;
12504 	eht_support = link_conf->eht_support;
12505 
12506 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
12507 		   "mac set rate params vdev %i rate 0x%02x nss 0x%02x sgi 0x%02x ldpc 0x%02x\n",
12508 		   arvif->vdev_id, rate, nss, sgi, ldpc);
12509 
12510 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
12511 		   "he_gi 0x%02x he_ltf 0x%02x he_fixed_rate %d\n", he_gi,
12512 		   he_ltf, he_fixed_rate);
12513 
12514 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
12515 		   "eht_gi 0x%02x eht_ltf 0x%02x eht_fixed_rate %d\n",
12516 		   eht_gi, eht_ltf, eht_fixed_rate);
12517 
12518 	if (!he_support && !eht_support) {
12519 		vdev_param = WMI_VDEV_PARAM_FIXED_RATE;
12520 		ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
12521 						    vdev_param, rate);
12522 		if (ret) {
12523 			ath12k_warn(ar->ab, "failed to set fixed rate param 0x%02x: %d\n",
12524 				    rate, ret);
12525 			return ret;
12526 		}
12527 	}
12528 
12529 	vdev_param = WMI_VDEV_PARAM_NSS;
12530 
12531 	ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
12532 					    vdev_param, nss);
12533 	if (ret) {
12534 		ath12k_warn(ar->ab, "failed to set nss param %d: %d\n",
12535 			    nss, ret);
12536 		return ret;
12537 	}
12538 
12539 	ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
12540 					    WMI_VDEV_PARAM_LDPC, ldpc);
12541 	if (ret) {
12542 		ath12k_warn(ar->ab, "failed to set ldpc param %d: %d\n",
12543 			    ldpc, ret);
12544 		return ret;
12545 	}
12546 
12547 	if (eht_support) {
12548 		if (eht_fixed_rate)
12549 			ret = ath12k_mac_set_fixed_rate_gi_ltf(arvif, eht_gi, eht_ltf,
12550 							       WMI_VDEV_PARAM_EHT_LTF);
12551 		else
12552 			ret = ath12k_mac_set_auto_rate_gi_ltf(arvif, eht_gi, eht_ltf);
12553 
12554 		if (ret) {
12555 			ath12k_warn(ar->ab,
12556 				    "failed to set EHT LTF/GI params %d/%d: %d\n",
12557 				    eht_gi, eht_ltf, ret);
12558 			return ret;
12559 		}
12560 		gi_ltf_set = true;
12561 	}
12562 
12563 	if (he_support) {
12564 		if (he_fixed_rate)
12565 			ret = ath12k_mac_set_fixed_rate_gi_ltf(arvif, he_gi, he_ltf,
12566 							       WMI_VDEV_PARAM_HE_LTF);
12567 		else
12568 			ret = ath12k_mac_set_auto_rate_gi_ltf(arvif, he_gi, he_ltf);
12569 		if (ret)
12570 			return ret;
12571 		gi_ltf_set = true;
12572 	}
12573 
12574 	if (!gi_ltf_set) {
12575 		vdev_param = WMI_VDEV_PARAM_SGI;
12576 		param_value = ath12k_mac_nlgi_to_wmigi(sgi);
12577 		ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
12578 						    vdev_param, param_value);
12579 		if (ret) {
12580 			ath12k_warn(ar->ab, "failed to set sgi param %d: %d\n",
12581 				    sgi, ret);
12582 			return ret;
12583 		}
12584 	}
12585 
12586 	return 0;
12587 }
12588 
12589 static bool
12590 ath12k_mac_vht_mcs_range_present(struct ath12k *ar,
12591 				 enum nl80211_band band,
12592 				 const struct cfg80211_bitrate_mask *mask)
12593 {
12594 	int i;
12595 	u16 vht_mcs;
12596 
12597 	for (i = 0; i < NL80211_VHT_NSS_MAX; i++) {
12598 		vht_mcs = mask->control[band].vht_mcs[i];
12599 
12600 		switch (vht_mcs) {
12601 		case 0:
12602 		case BIT(8) - 1:
12603 		case BIT(9) - 1:
12604 		case BIT(10) - 1:
12605 			break;
12606 		default:
12607 			return false;
12608 		}
12609 	}
12610 
12611 	return true;
12612 }
12613 
12614 static bool
12615 ath12k_mac_he_mcs_range_present(struct ath12k *ar,
12616 				enum nl80211_band band,
12617 				const struct cfg80211_bitrate_mask *mask)
12618 {
12619 	int i;
12620 	u16 he_mcs;
12621 
12622 	for (i = 0; i < NL80211_HE_NSS_MAX; i++) {
12623 		he_mcs = mask->control[band].he_mcs[i];
12624 
12625 		switch (he_mcs) {
12626 		case 0:
12627 		case BIT(8) - 1:
12628 		case BIT(10) - 1:
12629 		case BIT(12) - 1:
12630 			break;
12631 		default:
12632 			return false;
12633 		}
12634 	}
12635 
12636 	return true;
12637 }
12638 
12639 static bool
12640 ath12k_mac_eht_mcs_range_present(struct ath12k *ar,
12641 				 enum nl80211_band band,
12642 				 const struct cfg80211_bitrate_mask *mask)
12643 {
12644 	u16 eht_mcs;
12645 	int i;
12646 
12647 	for (i = 0; i < NL80211_EHT_NSS_MAX; i++) {
12648 		eht_mcs = mask->control[band].eht_mcs[i];
12649 
12650 		switch (eht_mcs) {
12651 		case 0:
12652 		case BIT(8) - 1:
12653 		case BIT(10) - 1:
12654 		case BIT(12) - 1:
12655 		case BIT(14) - 1:
12656 			break;
12657 		case BIT(15) - 1:
12658 		case BIT(16) - 1:
12659 		case BIT(16) - BIT(14) - 1:
12660 			if (i != 0)
12661 				return false;
12662 			break;
12663 		default:
12664 			return false;
12665 		}
12666 	}
12667 
12668 	return true;
12669 }
12670 
12671 static void ath12k_mac_set_bitrate_mask_iter(void *data,
12672 					     struct ieee80211_sta *sta)
12673 {
12674 	struct ath12k_link_vif *arvif = data;
12675 	struct ath12k_sta *ahsta = ath12k_sta_to_ahsta(sta);
12676 	struct ath12k_link_sta *arsta;
12677 	struct ath12k *ar = arvif->ar;
12678 
12679 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
12680 
12681 	arsta = wiphy_dereference(ath12k_ar_to_hw(ar)->wiphy,
12682 				  ahsta->link[arvif->link_id]);
12683 	if (!arsta || arsta->arvif != arvif)
12684 		return;
12685 
12686 	spin_lock_bh(&ar->data_lock);
12687 	arsta->changed |= IEEE80211_RC_SUPP_RATES_CHANGED;
12688 	spin_unlock_bh(&ar->data_lock);
12689 
12690 	wiphy_work_queue(ath12k_ar_to_hw(ar)->wiphy, &arsta->update_wk);
12691 }
12692 
12693 static void ath12k_mac_disable_peer_fixed_rate(void *data,
12694 					       struct ieee80211_sta *sta)
12695 {
12696 	struct ath12k_link_vif *arvif = data;
12697 	struct ath12k_sta *ahsta = ath12k_sta_to_ahsta(sta);
12698 	struct ath12k_link_sta *arsta;
12699 	struct ath12k *ar = arvif->ar;
12700 	int ret;
12701 
12702 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
12703 
12704 	arsta = wiphy_dereference(ath12k_ar_to_hw(ar)->wiphy,
12705 				  ahsta->link[arvif->link_id]);
12706 
12707 	if (!arsta || arsta->arvif != arvif)
12708 		return;
12709 
12710 	ret = ath12k_wmi_set_peer_param(ar, arsta->addr,
12711 					arvif->vdev_id,
12712 					WMI_PEER_PARAM_FIXED_RATE,
12713 					WMI_FIXED_RATE_NONE);
12714 	if (ret)
12715 		ath12k_warn(ar->ab,
12716 			    "failed to disable peer fixed rate for STA %pM ret %d\n",
12717 			    arsta->addr, ret);
12718 }
12719 
12720 static bool
12721 ath12k_mac_validate_fixed_rate_settings(struct ath12k *ar, enum nl80211_band band,
12722 					const struct cfg80211_bitrate_mask *mask,
12723 					unsigned int link_id)
12724 {
12725 	bool eht_fixed_rate = false, he_fixed_rate = false, vht_fixed_rate = false;
12726 	const u16 *vht_mcs_mask, *he_mcs_mask, *eht_mcs_mask;
12727 	struct ieee80211_link_sta *link_sta;
12728 	struct ath12k_peer *peer, *tmp;
12729 	u8 vht_nss, he_nss, eht_nss;
12730 	int ret = true;
12731 
12732 	vht_mcs_mask = mask->control[band].vht_mcs;
12733 	he_mcs_mask = mask->control[band].he_mcs;
12734 	eht_mcs_mask = mask->control[band].eht_mcs;
12735 
12736 	if (ath12k_mac_bitrate_mask_num_vht_rates(ar, band, mask) == 1)
12737 		vht_fixed_rate = true;
12738 
12739 	if (ath12k_mac_bitrate_mask_num_he_rates(ar, band, mask) == 1)
12740 		he_fixed_rate = true;
12741 
12742 	if (ath12k_mac_bitrate_mask_num_eht_rates(ar, band, mask) == 1)
12743 		eht_fixed_rate = true;
12744 
12745 	if (!vht_fixed_rate && !he_fixed_rate && !eht_fixed_rate)
12746 		return true;
12747 
12748 	vht_nss = ath12k_mac_max_vht_nss(vht_mcs_mask);
12749 	he_nss =  ath12k_mac_max_he_nss(he_mcs_mask);
12750 	eht_nss = ath12k_mac_max_eht_nss(eht_mcs_mask);
12751 
12752 	rcu_read_lock();
12753 	spin_lock_bh(&ar->ab->base_lock);
12754 	list_for_each_entry_safe(peer, tmp, &ar->ab->peers, list) {
12755 		if (peer->sta) {
12756 			link_sta = rcu_dereference(peer->sta->link[link_id]);
12757 			if (!link_sta) {
12758 				ret = false;
12759 				goto exit;
12760 			}
12761 
12762 			if (vht_fixed_rate && (!link_sta->vht_cap.vht_supported ||
12763 					       link_sta->rx_nss < vht_nss)) {
12764 				ret = false;
12765 				goto exit;
12766 			}
12767 			if (he_fixed_rate && (!link_sta->he_cap.has_he ||
12768 					      link_sta->rx_nss < he_nss)) {
12769 				ret = false;
12770 				goto exit;
12771 			}
12772 			if (eht_fixed_rate && (!link_sta->eht_cap.has_eht ||
12773 					       link_sta->rx_nss < eht_nss)) {
12774 				ret = false;
12775 				goto exit;
12776 			}
12777 		}
12778 	}
12779 exit:
12780 	spin_unlock_bh(&ar->ab->base_lock);
12781 	rcu_read_unlock();
12782 	return ret;
12783 }
12784 
12785 static int
12786 ath12k_mac_op_set_bitrate_mask(struct ieee80211_hw *hw,
12787 			       struct ieee80211_vif *vif,
12788 			       const struct cfg80211_bitrate_mask *mask)
12789 {
12790 	struct ath12k_vif *ahvif = ath12k_vif_to_ahvif(vif);
12791 	struct ath12k_link_vif *arvif;
12792 	struct cfg80211_chan_def def;
12793 	struct ath12k *ar;
12794 	enum nl80211_band band;
12795 	const u8 *ht_mcs_mask;
12796 	const u16 *vht_mcs_mask;
12797 	const u16 *he_mcs_mask;
12798 	const u16 *eht_mcs_mask;
12799 	u8 he_ltf = 0;
12800 	u8 he_gi = 0;
12801 	u8 eht_ltf = 0, eht_gi = 0;
12802 	u32 rate;
12803 	u8 nss, mac_nss;
12804 	u8 sgi;
12805 	u8 ldpc;
12806 	int single_nss;
12807 	int ret;
12808 	int num_rates;
12809 	bool he_fixed_rate = false;
12810 	bool eht_fixed_rate = false;
12811 
12812 	lockdep_assert_wiphy(hw->wiphy);
12813 
12814 	arvif = &ahvif->deflink;
12815 
12816 	ar = arvif->ar;
12817 	if (ath12k_mac_vif_link_chan(vif, arvif->link_id, &def)) {
12818 		ret = -EPERM;
12819 		goto out;
12820 	}
12821 
12822 	band = def.chan->band;
12823 	ht_mcs_mask = mask->control[band].ht_mcs;
12824 	vht_mcs_mask = mask->control[band].vht_mcs;
12825 	he_mcs_mask = mask->control[band].he_mcs;
12826 	eht_mcs_mask = mask->control[band].eht_mcs;
12827 	ldpc = !!(ar->ht_cap_info & WMI_HT_CAP_LDPC);
12828 
12829 	sgi = mask->control[band].gi;
12830 	if (sgi == NL80211_TXRATE_FORCE_SGI) {
12831 		ret = -EINVAL;
12832 		goto out;
12833 	}
12834 
12835 	he_gi = mask->control[band].he_gi;
12836 	he_ltf = mask->control[band].he_ltf;
12837 
12838 	eht_gi = mask->control[band].eht_gi;
12839 	eht_ltf = mask->control[band].eht_ltf;
12840 
12841 	/* mac80211 doesn't support sending a fixed HT/VHT MCS alone, rather it
12842 	 * requires passing at least one of used basic rates along with them.
12843 	 * Fixed rate setting across different preambles(legacy, HT, VHT) is
12844 	 * not supported by the FW. Hence use of FIXED_RATE vdev param is not
12845 	 * suitable for setting single HT/VHT rates.
12846 	 * But, there could be a single basic rate passed from userspace which
12847 	 * can be done through the FIXED_RATE param.
12848 	 */
12849 	if (ath12k_mac_has_single_legacy_rate(ar, band, mask)) {
12850 		ret = ath12k_mac_get_single_legacy_rate(ar, band, mask, &rate,
12851 							&nss);
12852 		if (ret) {
12853 			ath12k_warn(ar->ab, "failed to get single legacy rate for vdev %i: %d\n",
12854 				    arvif->vdev_id, ret);
12855 			goto out;
12856 		}
12857 
12858 		ieee80211_iterate_stations_mtx(hw,
12859 					       ath12k_mac_disable_peer_fixed_rate,
12860 					       arvif);
12861 	} else if (ath12k_mac_bitrate_mask_get_single_nss(ar, vif, band, mask,
12862 							  &single_nss)) {
12863 		rate = WMI_FIXED_RATE_NONE;
12864 		nss = single_nss;
12865 		arvif->bitrate_mask = *mask;
12866 
12867 		ieee80211_iterate_stations_atomic(hw,
12868 						  ath12k_mac_set_bitrate_mask_iter,
12869 						  arvif);
12870 	} else {
12871 		rate = WMI_FIXED_RATE_NONE;
12872 
12873 		if (!ath12k_mac_validate_fixed_rate_settings(ar, band,
12874 							     mask, arvif->link_id))
12875 			ath12k_warn(ar->ab,
12876 				    "failed to update fixed rate settings due to mcs/nss incompatibility\n");
12877 
12878 		mac_nss = max(max3(ath12k_mac_max_ht_nss(ht_mcs_mask),
12879 				   ath12k_mac_max_vht_nss(vht_mcs_mask),
12880 				   ath12k_mac_max_he_nss(he_mcs_mask)),
12881 			       ath12k_mac_max_eht_nss(eht_mcs_mask));
12882 		nss = min_t(u32, ar->num_tx_chains, mac_nss);
12883 
12884 		/* If multiple rates across different preambles are given
12885 		 * we can reconfigure this info with all peers using PEER_ASSOC
12886 		 * command with the below exception cases.
12887 		 * - Single VHT Rate : peer_assoc command accommodates only MCS
12888 		 * range values i.e 0-7, 0-8, 0-9 for VHT. Though mac80211
12889 		 * mandates passing basic rates along with HT/VHT rates, FW
12890 		 * doesn't allow switching from VHT to Legacy. Hence instead of
12891 		 * setting legacy and VHT rates using RATEMASK_CMD vdev cmd,
12892 		 * we could set this VHT rate as peer fixed rate param, which
12893 		 * will override FIXED rate and FW rate control algorithm.
12894 		 * If single VHT rate is passed along with HT rates, we select
12895 		 * the VHT rate as fixed rate for vht peers.
12896 		 * - Multiple VHT Rates : When Multiple VHT rates are given,this
12897 		 * can be set using RATEMASK CMD which uses FW rate-ctl alg.
12898 		 * TODO: Setting multiple VHT MCS and replacing peer_assoc with
12899 		 * RATEMASK_CMDID can cover all use cases of setting rates
12900 		 * across multiple preambles and rates within same type.
12901 		 * But requires more validation of the command at this point.
12902 		 */
12903 
12904 		num_rates = ath12k_mac_bitrate_mask_num_vht_rates(ar, band,
12905 								  mask);
12906 
12907 		if (!ath12k_mac_vht_mcs_range_present(ar, band, mask) &&
12908 		    num_rates > 1) {
12909 			/* TODO: Handle multiple VHT MCS values setting using
12910 			 * RATEMASK CMD
12911 			 */
12912 			ath12k_warn(ar->ab,
12913 				    "Setting more than one MCS Value in bitrate mask not supported\n");
12914 			ret = -EINVAL;
12915 			goto out;
12916 		}
12917 
12918 		num_rates = ath12k_mac_bitrate_mask_num_he_rates(ar, band, mask);
12919 		if (num_rates == 1)
12920 			he_fixed_rate = true;
12921 
12922 		if (!ath12k_mac_he_mcs_range_present(ar, band, mask) &&
12923 		    num_rates > 1) {
12924 			ath12k_warn(ar->ab,
12925 				    "Setting more than one HE MCS Value in bitrate mask not supported\n");
12926 			ret = -EINVAL;
12927 			goto out;
12928 		}
12929 
12930 		num_rates = ath12k_mac_bitrate_mask_num_eht_rates(ar, band,
12931 								  mask);
12932 		if (num_rates == 1)
12933 			eht_fixed_rate = true;
12934 
12935 		if (!ath12k_mac_eht_mcs_range_present(ar, band, mask) &&
12936 		    num_rates > 1) {
12937 			ath12k_warn(ar->ab,
12938 				    "Setting more than one EHT MCS Value in bitrate mask not supported\n");
12939 			ret = -EINVAL;
12940 			goto out;
12941 		}
12942 
12943 		ieee80211_iterate_stations_mtx(hw,
12944 					       ath12k_mac_disable_peer_fixed_rate,
12945 					       arvif);
12946 
12947 		arvif->bitrate_mask = *mask;
12948 		ieee80211_iterate_stations_mtx(hw,
12949 					       ath12k_mac_set_bitrate_mask_iter,
12950 					       arvif);
12951 	}
12952 
12953 	ret = ath12k_mac_set_rate_params(arvif, rate, nss, sgi, ldpc, he_gi,
12954 					 he_ltf, he_fixed_rate, eht_gi, eht_ltf,
12955 					 eht_fixed_rate);
12956 	if (ret) {
12957 		ath12k_warn(ar->ab, "failed to set rate params on vdev %i: %d\n",
12958 			    arvif->vdev_id, ret);
12959 	}
12960 
12961 out:
12962 	return ret;
12963 }
12964 
12965 static void
12966 ath12k_mac_op_reconfig_complete(struct ieee80211_hw *hw,
12967 				enum ieee80211_reconfig_type reconfig_type)
12968 {
12969 	struct ath12k_hw *ah = ath12k_hw_to_ah(hw);
12970 	struct ath12k *ar;
12971 	struct ath12k_base *ab;
12972 	struct ath12k_vif *ahvif;
12973 	struct ath12k_link_vif *arvif;
12974 	int recovery_count, i;
12975 
12976 	lockdep_assert_wiphy(hw->wiphy);
12977 
12978 	if (reconfig_type != IEEE80211_RECONFIG_TYPE_RESTART)
12979 		return;
12980 
12981 	guard(mutex)(&ah->hw_mutex);
12982 
12983 	if (ah->state != ATH12K_HW_STATE_RESTARTED)
12984 		return;
12985 
12986 	ah->state = ATH12K_HW_STATE_ON;
12987 	ieee80211_wake_queues(hw);
12988 
12989 	for_each_ar(ah, ar, i) {
12990 		ab = ar->ab;
12991 
12992 		ath12k_warn(ar->ab, "pdev %d successfully recovered\n",
12993 			    ar->pdev->pdev_id);
12994 
12995 		if (ar->ab->hw_params->current_cc_support &&
12996 		    ar->alpha2[0] != 0 && ar->alpha2[1] != 0) {
12997 			struct wmi_set_current_country_arg arg = {};
12998 
12999 			memcpy(&arg.alpha2, ar->alpha2, 2);
13000 			reinit_completion(&ar->regd_update_completed);
13001 			ath12k_wmi_send_set_current_country_cmd(ar, &arg);
13002 		}
13003 
13004 		if (ab->is_reset) {
13005 			recovery_count = atomic_inc_return(&ab->recovery_count);
13006 
13007 			ath12k_dbg(ab, ATH12K_DBG_BOOT, "recovery count %d\n",
13008 				   recovery_count);
13009 
13010 			/* When there are multiple radios in an SOC,
13011 			 * the recovery has to be done for each radio
13012 			 */
13013 			if (recovery_count == ab->num_radios) {
13014 				atomic_dec(&ab->reset_count);
13015 				complete(&ab->reset_complete);
13016 				ab->is_reset = false;
13017 				atomic_set(&ab->fail_cont_count, 0);
13018 				ath12k_dbg(ab, ATH12K_DBG_BOOT, "reset success\n");
13019 			}
13020 		}
13021 
13022 		list_for_each_entry(arvif, &ar->arvifs, list) {
13023 			ahvif = arvif->ahvif;
13024 			ath12k_dbg(ab, ATH12K_DBG_BOOT,
13025 				   "reconfig cipher %d up %d vdev type %d\n",
13026 				   ahvif->key_cipher,
13027 				   arvif->is_up,
13028 				   ahvif->vdev_type);
13029 
13030 			/* After trigger disconnect, then upper layer will
13031 			 * trigger connect again, then the PN number of
13032 			 * upper layer will be reset to keep up with AP
13033 			 * side, hence PN number mismatch will not happen.
13034 			 */
13035 			if (arvif->is_up &&
13036 			    ahvif->vdev_type == WMI_VDEV_TYPE_STA &&
13037 			    ahvif->vdev_subtype == WMI_VDEV_SUBTYPE_NONE) {
13038 				ieee80211_hw_restart_disconnect(ahvif->vif);
13039 
13040 				ath12k_dbg(ab, ATH12K_DBG_BOOT,
13041 					   "restart disconnect\n");
13042 			}
13043 		}
13044 	}
13045 }
13046 
13047 static void
13048 ath12k_mac_update_bss_chan_survey(struct ath12k *ar,
13049 				  struct ieee80211_channel *channel)
13050 {
13051 	int ret;
13052 	enum wmi_bss_chan_info_req_type type = WMI_BSS_SURVEY_REQ_TYPE_READ;
13053 
13054 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
13055 
13056 	if (!test_bit(WMI_TLV_SERVICE_BSS_CHANNEL_INFO_64, ar->ab->wmi_ab.svc_map) ||
13057 	    ar->rx_channel != channel)
13058 		return;
13059 
13060 	if (ar->scan.state != ATH12K_SCAN_IDLE) {
13061 		ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
13062 			   "ignoring bss chan info req while scanning..\n");
13063 		return;
13064 	}
13065 
13066 	reinit_completion(&ar->bss_survey_done);
13067 
13068 	ret = ath12k_wmi_pdev_bss_chan_info_request(ar, type);
13069 	if (ret) {
13070 		ath12k_warn(ar->ab, "failed to send pdev bss chan info request\n");
13071 		return;
13072 	}
13073 
13074 	ret = wait_for_completion_timeout(&ar->bss_survey_done, 3 * HZ);
13075 	if (ret == 0)
13076 		ath12k_warn(ar->ab, "bss channel survey timed out\n");
13077 }
13078 
13079 static int ath12k_mac_op_get_survey(struct ieee80211_hw *hw, int idx,
13080 				    struct survey_info *survey)
13081 {
13082 	struct ath12k *ar;
13083 	struct ieee80211_supported_band *sband;
13084 	struct survey_info *ar_survey;
13085 
13086 	lockdep_assert_wiphy(hw->wiphy);
13087 
13088 	if (idx >= ATH12K_NUM_CHANS)
13089 		return -ENOENT;
13090 
13091 	sband = hw->wiphy->bands[NL80211_BAND_2GHZ];
13092 	if (sband && idx >= sband->n_channels) {
13093 		idx -= sband->n_channels;
13094 		sband = NULL;
13095 	}
13096 
13097 	if (!sband)
13098 		sband = hw->wiphy->bands[NL80211_BAND_5GHZ];
13099 	if (sband && idx >= sband->n_channels) {
13100 		idx -= sband->n_channels;
13101 		sband = NULL;
13102 	}
13103 
13104 	if (!sband)
13105 		sband = hw->wiphy->bands[NL80211_BAND_6GHZ];
13106 
13107 	if (!sband || idx >= sband->n_channels)
13108 		return -ENOENT;
13109 
13110 	ar = ath12k_mac_get_ar_by_chan(hw, &sband->channels[idx]);
13111 	if (!ar) {
13112 		if (sband->channels[idx].flags & IEEE80211_CHAN_DISABLED) {
13113 			memset(survey, 0, sizeof(*survey));
13114 			return 0;
13115 		}
13116 		return -ENOENT;
13117 	}
13118 
13119 	ar_survey = &ar->survey[idx];
13120 
13121 	ath12k_mac_update_bss_chan_survey(ar, &sband->channels[idx]);
13122 
13123 	spin_lock_bh(&ar->data_lock);
13124 	memcpy(survey, ar_survey, sizeof(*survey));
13125 	spin_unlock_bh(&ar->data_lock);
13126 
13127 	survey->channel = &sband->channels[idx];
13128 
13129 	if (ar->rx_channel == survey->channel)
13130 		survey->filled |= SURVEY_INFO_IN_USE;
13131 
13132 	return 0;
13133 }
13134 
13135 static void ath12k_mac_put_chain_rssi(struct station_info *sinfo,
13136 				      struct ath12k_link_sta *arsta)
13137 {
13138 	s8 rssi;
13139 	int i;
13140 
13141 	for (i = 0; i < ARRAY_SIZE(sinfo->chain_signal); i++) {
13142 		sinfo->chains &= ~BIT(i);
13143 		rssi = arsta->chain_signal[i];
13144 
13145 		if (rssi != ATH12K_DEFAULT_NOISE_FLOOR &&
13146 		    rssi != ATH12K_INVALID_RSSI_FULL &&
13147 		    rssi != ATH12K_INVALID_RSSI_EMPTY &&
13148 		    rssi != 0) {
13149 			sinfo->chain_signal[i] = rssi;
13150 			sinfo->chains |= BIT(i);
13151 			sinfo->filled |= BIT_ULL(NL80211_STA_INFO_CHAIN_SIGNAL);
13152 		}
13153 	}
13154 }
13155 
13156 static void ath12k_mac_op_sta_statistics(struct ieee80211_hw *hw,
13157 					 struct ieee80211_vif *vif,
13158 					 struct ieee80211_sta *sta,
13159 					 struct station_info *sinfo)
13160 {
13161 	struct ath12k_sta *ahsta = ath12k_sta_to_ahsta(sta);
13162 	struct ath12k_fw_stats_req_params params = {};
13163 	struct ath12k_link_sta *arsta;
13164 	s8 signal, noise_floor;
13165 	struct ath12k *ar;
13166 	bool db2dbm;
13167 
13168 	lockdep_assert_wiphy(hw->wiphy);
13169 
13170 	arsta = &ahsta->deflink;
13171 	ar = ath12k_get_ar_by_vif(hw, vif, arsta->link_id);
13172 	if (!ar)
13173 		return;
13174 
13175 	db2dbm = test_bit(WMI_TLV_SERVICE_HW_DB2DBM_CONVERSION_SUPPORT,
13176 			  ar->ab->wmi_ab.svc_map);
13177 
13178 	sinfo->rx_duration = arsta->rx_duration;
13179 	sinfo->filled |= BIT_ULL(NL80211_STA_INFO_RX_DURATION);
13180 
13181 	sinfo->tx_duration = arsta->tx_duration;
13182 	sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_DURATION);
13183 
13184 	if (arsta->txrate.legacy || arsta->txrate.nss) {
13185 		if (arsta->txrate.legacy) {
13186 			sinfo->txrate.legacy = arsta->txrate.legacy;
13187 		} else {
13188 			sinfo->txrate.mcs = arsta->txrate.mcs;
13189 			sinfo->txrate.nss = arsta->txrate.nss;
13190 			sinfo->txrate.bw = arsta->txrate.bw;
13191 			sinfo->txrate.he_gi = arsta->txrate.he_gi;
13192 			sinfo->txrate.he_dcm = arsta->txrate.he_dcm;
13193 			sinfo->txrate.he_ru_alloc = arsta->txrate.he_ru_alloc;
13194 			sinfo->txrate.eht_gi = arsta->txrate.eht_gi;
13195 			sinfo->txrate.eht_ru_alloc = arsta->txrate.eht_ru_alloc;
13196 		}
13197 		sinfo->txrate.flags = arsta->txrate.flags;
13198 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_BITRATE);
13199 	}
13200 
13201 	/* TODO: Use real NF instead of default one. */
13202 	signal = arsta->rssi_comb;
13203 
13204 	params.pdev_id = ar->pdev->pdev_id;
13205 	params.vdev_id = 0;
13206 	params.stats_id = WMI_REQUEST_VDEV_STAT;
13207 
13208 	if (!signal &&
13209 	    ahsta->ahvif->vdev_type == WMI_VDEV_TYPE_STA &&
13210 	    !(ath12k_mac_get_fw_stats(ar, &params))) {
13211 		signal = arsta->rssi_beacon;
13212 		ath12k_fw_stats_reset(ar);
13213 	}
13214 
13215 	params.stats_id = WMI_REQUEST_RSSI_PER_CHAIN_STAT;
13216 	if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_CHAIN_SIGNAL)) &&
13217 	    ahsta->ahvif->vdev_type == WMI_VDEV_TYPE_STA &&
13218 	    !(ath12k_mac_get_fw_stats(ar, &params))) {
13219 		ath12k_mac_put_chain_rssi(sinfo, arsta);
13220 		ath12k_fw_stats_reset(ar);
13221 	}
13222 
13223 	spin_lock_bh(&ar->data_lock);
13224 	noise_floor = ath12k_pdev_get_noise_floor(ar);
13225 	spin_unlock_bh(&ar->data_lock);
13226 
13227 	if (signal) {
13228 		sinfo->signal = db2dbm ? signal : signal + noise_floor;
13229 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_SIGNAL);
13230 	}
13231 
13232 	sinfo->signal_avg = ewma_avg_rssi_read(&arsta->avg_rssi);
13233 
13234 	if (!db2dbm)
13235 		sinfo->signal_avg += noise_floor;
13236 
13237 	sinfo->filled |= BIT_ULL(NL80211_STA_INFO_SIGNAL_AVG);
13238 
13239 	sinfo->tx_retries = arsta->tx_retry_count;
13240 	sinfo->tx_failed = arsta->tx_retry_failed;
13241 	sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_RETRIES);
13242 	sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_FAILED);
13243 }
13244 
13245 static void ath12k_mac_op_link_sta_statistics(struct ieee80211_hw *hw,
13246 					      struct ieee80211_vif *vif,
13247 					      struct ieee80211_link_sta *link_sta,
13248 					      struct link_station_info *link_sinfo)
13249 {
13250 	struct ath12k_sta *ahsta = ath12k_sta_to_ahsta(link_sta->sta);
13251 	struct ath12k_fw_stats_req_params params = {};
13252 	struct ath12k_link_sta *arsta;
13253 	struct ath12k *ar;
13254 	s8 signal;
13255 	bool db2dbm;
13256 
13257 	lockdep_assert_wiphy(hw->wiphy);
13258 
13259 	arsta = wiphy_dereference(hw->wiphy, ahsta->link[link_sta->link_id]);
13260 
13261 	if (!arsta)
13262 		return;
13263 
13264 	ar = ath12k_get_ar_by_vif(hw, vif, arsta->link_id);
13265 	if (!ar)
13266 		return;
13267 
13268 	db2dbm = test_bit(WMI_TLV_SERVICE_HW_DB2DBM_CONVERSION_SUPPORT,
13269 			  ar->ab->wmi_ab.svc_map);
13270 
13271 	link_sinfo->rx_duration = arsta->rx_duration;
13272 	link_sinfo->filled |= BIT_ULL(NL80211_STA_INFO_RX_DURATION);
13273 
13274 	link_sinfo->tx_duration = arsta->tx_duration;
13275 	link_sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_DURATION);
13276 
13277 	if (arsta->txrate.legacy || arsta->txrate.nss) {
13278 		if (arsta->txrate.legacy) {
13279 			link_sinfo->txrate.legacy = arsta->txrate.legacy;
13280 		} else {
13281 			link_sinfo->txrate.mcs = arsta->txrate.mcs;
13282 			link_sinfo->txrate.nss = arsta->txrate.nss;
13283 			link_sinfo->txrate.bw = arsta->txrate.bw;
13284 			link_sinfo->txrate.he_gi = arsta->txrate.he_gi;
13285 			link_sinfo->txrate.he_dcm = arsta->txrate.he_dcm;
13286 			link_sinfo->txrate.he_ru_alloc =
13287 				arsta->txrate.he_ru_alloc;
13288 			link_sinfo->txrate.eht_gi = arsta->txrate.eht_gi;
13289 			link_sinfo->txrate.eht_ru_alloc =
13290 				arsta->txrate.eht_ru_alloc;
13291 		}
13292 		link_sinfo->txrate.flags = arsta->txrate.flags;
13293 		link_sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_BITRATE);
13294 	}
13295 
13296 	/* TODO: Use real NF instead of default one. */
13297 	signal = arsta->rssi_comb;
13298 
13299 	params.pdev_id = ar->pdev->pdev_id;
13300 	params.vdev_id = 0;
13301 	params.stats_id = WMI_REQUEST_VDEV_STAT;
13302 
13303 	if (!signal &&
13304 	    ahsta->ahvif->vdev_type == WMI_VDEV_TYPE_STA &&
13305 	    !(ath12k_mac_get_fw_stats(ar, &params))) {
13306 		signal = arsta->rssi_beacon;
13307 		ath12k_fw_stats_reset(ar);
13308 	}
13309 
13310 	if (signal) {
13311 		link_sinfo->signal =
13312 			db2dbm ? signal : signal + ATH12K_DEFAULT_NOISE_FLOOR;
13313 		link_sinfo->filled |= BIT_ULL(NL80211_STA_INFO_SIGNAL);
13314 	}
13315 
13316 	link_sinfo->signal_avg = ewma_avg_rssi_read(&arsta->avg_rssi);
13317 
13318 	if (!db2dbm)
13319 		link_sinfo->signal_avg += ATH12K_DEFAULT_NOISE_FLOOR;
13320 
13321 	link_sinfo->filled |= BIT_ULL(NL80211_STA_INFO_SIGNAL_AVG);
13322 
13323 	link_sinfo->tx_retries = arsta->tx_retry_count;
13324 	link_sinfo->tx_failed = arsta->tx_retry_failed;
13325 	link_sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_RETRIES);
13326 	link_sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_FAILED);
13327 }
13328 
13329 static int ath12k_mac_op_cancel_remain_on_channel(struct ieee80211_hw *hw,
13330 						  struct ieee80211_vif *vif)
13331 {
13332 	struct ath12k_hw *ah = ath12k_hw_to_ah(hw);
13333 	struct ath12k *ar;
13334 
13335 	ar = ath12k_ah_to_ar(ah, 0);
13336 
13337 	lockdep_assert_wiphy(hw->wiphy);
13338 
13339 	spin_lock_bh(&ar->data_lock);
13340 	ar->scan.roc_notify = false;
13341 	spin_unlock_bh(&ar->data_lock);
13342 
13343 	ath12k_scan_abort(ar);
13344 
13345 	cancel_delayed_work_sync(&ar->scan.timeout);
13346 	wiphy_work_cancel(hw->wiphy, &ar->scan.vdev_clean_wk);
13347 
13348 	return 0;
13349 }
13350 
13351 static int ath12k_mac_op_remain_on_channel(struct ieee80211_hw *hw,
13352 					   struct ieee80211_vif *vif,
13353 					   struct ieee80211_channel *chan,
13354 					   int duration,
13355 					   enum ieee80211_roc_type type)
13356 {
13357 	struct ath12k_vif *ahvif = ath12k_vif_to_ahvif(vif);
13358 	struct ath12k_hw *ah = ath12k_hw_to_ah(hw);
13359 	struct ath12k_link_vif *arvif;
13360 	struct ath12k *ar;
13361 	u32 scan_time_msec;
13362 	bool create = true;
13363 	u8 link_id;
13364 	int ret;
13365 
13366 	lockdep_assert_wiphy(hw->wiphy);
13367 
13368 	ar = ath12k_mac_select_scan_device(hw, vif, chan->center_freq);
13369 	if (!ar)
13370 		return -EINVAL;
13371 
13372 	/* check if any of the links of ML VIF is already started on
13373 	 * radio(ar) corresponding to given scan frequency and use it,
13374 	 * if not use deflink(link 0) for scan purpose.
13375 	 */
13376 
13377 	link_id = ath12k_mac_find_link_id_by_ar(ahvif, ar);
13378 	arvif = ath12k_mac_assign_link_vif(ah, vif, link_id);
13379 	/* If the vif is already assigned to a specific vdev of an ar,
13380 	 * check whether its already started, vdev which is started
13381 	 * are not allowed to switch to a new radio.
13382 	 * If the vdev is not started, but was earlier created on a
13383 	 * different ar, delete that vdev and create a new one. We don't
13384 	 * delete at the scan stop as an optimization to avoid redundant
13385 	 * delete-create vdev's for the same ar, in case the request is
13386 	 * always on the same band for the vif
13387 	 */
13388 	if (arvif->is_created) {
13389 		if (WARN_ON(!arvif->ar))
13390 			return -EINVAL;
13391 
13392 		if (ar != arvif->ar && arvif->is_started)
13393 			return -EBUSY;
13394 
13395 		if (ar != arvif->ar) {
13396 			ath12k_mac_remove_link_interface(hw, arvif);
13397 			ath12k_mac_unassign_link_vif(arvif);
13398 		} else {
13399 			create = false;
13400 		}
13401 	}
13402 
13403 	if (create) {
13404 		arvif = ath12k_mac_assign_link_vif(ah, vif, link_id);
13405 
13406 		ret = ath12k_mac_vdev_create(ar, arvif);
13407 		if (ret) {
13408 			ath12k_warn(ar->ab, "unable to create scan vdev for roc: %d\n",
13409 				    ret);
13410 			ath12k_mac_unassign_link_vif(arvif);
13411 			return ret;
13412 		}
13413 	}
13414 
13415 	spin_lock_bh(&ar->data_lock);
13416 
13417 	switch (ar->scan.state) {
13418 	case ATH12K_SCAN_IDLE:
13419 		reinit_completion(&ar->scan.started);
13420 		reinit_completion(&ar->scan.completed);
13421 		reinit_completion(&ar->scan.on_channel);
13422 		ar->scan.state = ATH12K_SCAN_STARTING;
13423 		ar->scan.is_roc = true;
13424 		ar->scan.arvif = arvif;
13425 		ar->scan.roc_freq = chan->center_freq;
13426 		ar->scan.roc_notify = true;
13427 		ret = 0;
13428 		break;
13429 	case ATH12K_SCAN_STARTING:
13430 	case ATH12K_SCAN_RUNNING:
13431 	case ATH12K_SCAN_ABORTING:
13432 		ret = -EBUSY;
13433 		break;
13434 	}
13435 
13436 	spin_unlock_bh(&ar->data_lock);
13437 
13438 	if (ret)
13439 		return ret;
13440 
13441 	scan_time_msec = hw->wiphy->max_remain_on_channel_duration * 2;
13442 
13443 	struct ath12k_wmi_scan_req_arg *arg __free(kfree) =
13444 					kzalloc(sizeof(*arg), GFP_KERNEL);
13445 	if (!arg)
13446 		return -ENOMEM;
13447 
13448 	ath12k_wmi_start_scan_init(ar, arg);
13449 	arg->num_chan = 1;
13450 
13451 	u32 *chan_list __free(kfree) = kcalloc(arg->num_chan, sizeof(*chan_list),
13452 					       GFP_KERNEL);
13453 	if (!chan_list)
13454 		return -ENOMEM;
13455 
13456 	arg->chan_list = chan_list;
13457 	arg->vdev_id = arvif->vdev_id;
13458 	arg->scan_id = ATH12K_SCAN_ID;
13459 	arg->chan_list[0] = chan->center_freq;
13460 	arg->dwell_time_active = scan_time_msec;
13461 	arg->dwell_time_passive = scan_time_msec;
13462 	arg->max_scan_time = scan_time_msec;
13463 	arg->scan_f_passive = 1;
13464 	arg->burst_duration = duration;
13465 
13466 	ret = ath12k_start_scan(ar, arg);
13467 	if (ret) {
13468 		ath12k_warn(ar->ab, "failed to start roc scan: %d\n", ret);
13469 
13470 		spin_lock_bh(&ar->data_lock);
13471 		ar->scan.state = ATH12K_SCAN_IDLE;
13472 		spin_unlock_bh(&ar->data_lock);
13473 		return ret;
13474 	}
13475 
13476 	ret = wait_for_completion_timeout(&ar->scan.on_channel, 3 * HZ);
13477 	if (ret == 0) {
13478 		ath12k_warn(ar->ab, "failed to switch to channel for roc scan\n");
13479 		ret = ath12k_scan_stop(ar);
13480 		if (ret)
13481 			ath12k_warn(ar->ab, "failed to stop scan: %d\n", ret);
13482 		return -ETIMEDOUT;
13483 	}
13484 
13485 	ieee80211_queue_delayed_work(hw, &ar->scan.timeout,
13486 				     msecs_to_jiffies(duration));
13487 
13488 	return 0;
13489 }
13490 
13491 static void ath12k_mac_op_set_rekey_data(struct ieee80211_hw *hw,
13492 					 struct ieee80211_vif *vif,
13493 					 struct cfg80211_gtk_rekey_data *data)
13494 {
13495 	struct ath12k_vif *ahvif = ath12k_vif_to_ahvif(vif);
13496 	struct ath12k_rekey_data *rekey_data;
13497 	struct ath12k_hw *ah = ath12k_hw_to_ah(hw);
13498 	struct ath12k *ar = ath12k_ah_to_ar(ah, 0);
13499 	struct ath12k_link_vif *arvif;
13500 
13501 	lockdep_assert_wiphy(hw->wiphy);
13502 
13503 	arvif = &ahvif->deflink;
13504 	rekey_data = &arvif->rekey_data;
13505 
13506 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac set rekey data vdev %d\n",
13507 		   arvif->vdev_id);
13508 
13509 	memcpy(rekey_data->kck, data->kck, NL80211_KCK_LEN);
13510 	memcpy(rekey_data->kek, data->kek, NL80211_KEK_LEN);
13511 
13512 	/* The supplicant works on big-endian, the firmware expects it on
13513 	 * little endian.
13514 	 */
13515 	rekey_data->replay_ctr = get_unaligned_be64(data->replay_ctr);
13516 
13517 	arvif->rekey_data.enable_offload = true;
13518 
13519 	ath12k_dbg_dump(ar->ab, ATH12K_DBG_MAC, "kck", NULL,
13520 			rekey_data->kck, NL80211_KCK_LEN);
13521 	ath12k_dbg_dump(ar->ab, ATH12K_DBG_MAC, "kek", NULL,
13522 			rekey_data->kck, NL80211_KEK_LEN);
13523 	ath12k_dbg_dump(ar->ab, ATH12K_DBG_MAC, "replay ctr", NULL,
13524 			&rekey_data->replay_ctr, sizeof(rekey_data->replay_ctr));
13525 }
13526 
13527 static const struct ieee80211_ops ath12k_ops = {
13528 	.tx				= ath12k_mac_op_tx,
13529 	.wake_tx_queue			= ieee80211_handle_wake_tx_queue,
13530 	.start                          = ath12k_mac_op_start,
13531 	.stop                           = ath12k_mac_op_stop,
13532 	.reconfig_complete              = ath12k_mac_op_reconfig_complete,
13533 	.add_interface                  = ath12k_mac_op_add_interface,
13534 	.remove_interface		= ath12k_mac_op_remove_interface,
13535 	.update_vif_offload		= ath12k_mac_op_update_vif_offload,
13536 	.config                         = ath12k_mac_op_config,
13537 	.link_info_changed              = ath12k_mac_op_link_info_changed,
13538 	.vif_cfg_changed		= ath12k_mac_op_vif_cfg_changed,
13539 	.change_vif_links               = ath12k_mac_op_change_vif_links,
13540 	.configure_filter		= ath12k_mac_op_configure_filter,
13541 	.hw_scan                        = ath12k_mac_op_hw_scan,
13542 	.cancel_hw_scan                 = ath12k_mac_op_cancel_hw_scan,
13543 	.set_key                        = ath12k_mac_op_set_key,
13544 	.set_rekey_data	                = ath12k_mac_op_set_rekey_data,
13545 	.sta_state                      = ath12k_mac_op_sta_state,
13546 	.sta_set_txpwr			= ath12k_mac_op_sta_set_txpwr,
13547 	.link_sta_rc_update		= ath12k_mac_op_link_sta_rc_update,
13548 	.conf_tx                        = ath12k_mac_op_conf_tx,
13549 	.set_antenna			= ath12k_mac_op_set_antenna,
13550 	.get_antenna			= ath12k_mac_op_get_antenna,
13551 	.ampdu_action			= ath12k_mac_op_ampdu_action,
13552 	.add_chanctx			= ath12k_mac_op_add_chanctx,
13553 	.remove_chanctx			= ath12k_mac_op_remove_chanctx,
13554 	.change_chanctx			= ath12k_mac_op_change_chanctx,
13555 	.assign_vif_chanctx		= ath12k_mac_op_assign_vif_chanctx,
13556 	.unassign_vif_chanctx		= ath12k_mac_op_unassign_vif_chanctx,
13557 	.switch_vif_chanctx		= ath12k_mac_op_switch_vif_chanctx,
13558 	.get_txpower			= ath12k_mac_op_get_txpower,
13559 	.set_rts_threshold		= ath12k_mac_op_set_rts_threshold,
13560 	.set_frag_threshold		= ath12k_mac_op_set_frag_threshold,
13561 	.set_bitrate_mask		= ath12k_mac_op_set_bitrate_mask,
13562 	.get_survey			= ath12k_mac_op_get_survey,
13563 	.flush				= ath12k_mac_op_flush,
13564 	.sta_statistics			= ath12k_mac_op_sta_statistics,
13565 	.link_sta_statistics		= ath12k_mac_op_link_sta_statistics,
13566 	.remain_on_channel              = ath12k_mac_op_remain_on_channel,
13567 	.cancel_remain_on_channel       = ath12k_mac_op_cancel_remain_on_channel,
13568 	.change_sta_links               = ath12k_mac_op_change_sta_links,
13569 	.can_activate_links             = ath12k_mac_op_can_activate_links,
13570 #ifdef CONFIG_PM
13571 	.suspend			= ath12k_wow_op_suspend,
13572 	.resume				= ath12k_wow_op_resume,
13573 	.set_wakeup			= ath12k_wow_op_set_wakeup,
13574 #endif
13575 #ifdef CONFIG_ATH12K_DEBUGFS
13576 	.vif_add_debugfs                = ath12k_debugfs_op_vif_add,
13577 #endif
13578 	CFG80211_TESTMODE_CMD(ath12k_tm_cmd)
13579 #ifdef CONFIG_ATH12K_DEBUGFS
13580 	.link_sta_add_debugfs           = ath12k_debugfs_link_sta_op_add,
13581 #endif
13582 };
13583 
13584 void ath12k_mac_update_freq_range(struct ath12k *ar,
13585 				  u32 freq_low, u32 freq_high)
13586 {
13587 	if (!(freq_low && freq_high))
13588 		return;
13589 
13590 	if (ar->freq_range.start_freq || ar->freq_range.end_freq) {
13591 		ar->freq_range.start_freq = min(ar->freq_range.start_freq,
13592 						MHZ_TO_KHZ(freq_low));
13593 		ar->freq_range.end_freq = max(ar->freq_range.end_freq,
13594 					      MHZ_TO_KHZ(freq_high));
13595 	} else {
13596 		ar->freq_range.start_freq = MHZ_TO_KHZ(freq_low);
13597 		ar->freq_range.end_freq = MHZ_TO_KHZ(freq_high);
13598 	}
13599 
13600 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
13601 		   "mac pdev %u freq limit updated. New range %u->%u MHz\n",
13602 		   ar->pdev->pdev_id, KHZ_TO_MHZ(ar->freq_range.start_freq),
13603 		   KHZ_TO_MHZ(ar->freq_range.end_freq));
13604 }
13605 
13606 static void ath12k_mac_update_ch_list(struct ath12k *ar,
13607 				      struct ieee80211_supported_band *band,
13608 				      u32 freq_low, u32 freq_high)
13609 {
13610 	int i;
13611 
13612 	if (!(freq_low && freq_high))
13613 		return;
13614 
13615 	for (i = 0; i < band->n_channels; i++) {
13616 		if (band->channels[i].center_freq < freq_low ||
13617 		    band->channels[i].center_freq > freq_high)
13618 			band->channels[i].flags |= IEEE80211_CHAN_DISABLED;
13619 	}
13620 }
13621 
13622 static u32 ath12k_get_phy_id(struct ath12k *ar, u32 band)
13623 {
13624 	struct ath12k_pdev *pdev = ar->pdev;
13625 	struct ath12k_pdev_cap *pdev_cap = &pdev->cap;
13626 
13627 	if (band == WMI_HOST_WLAN_2GHZ_CAP)
13628 		return pdev_cap->band[NL80211_BAND_2GHZ].phy_id;
13629 
13630 	if (band == WMI_HOST_WLAN_5GHZ_CAP)
13631 		return pdev_cap->band[NL80211_BAND_5GHZ].phy_id;
13632 
13633 	ath12k_warn(ar->ab, "unsupported phy cap:%d\n", band);
13634 
13635 	return 0;
13636 }
13637 
13638 static int ath12k_mac_update_band(struct ath12k *ar,
13639 				  struct ieee80211_supported_band *orig_band,
13640 				  struct ieee80211_supported_band *new_band)
13641 {
13642 	int i;
13643 
13644 	if (!orig_band || !new_band)
13645 		return -EINVAL;
13646 
13647 	if (orig_band->band != new_band->band)
13648 		return -EINVAL;
13649 
13650 	for (i = 0; i < new_band->n_channels; i++) {
13651 		if (new_band->channels[i].flags & IEEE80211_CHAN_DISABLED)
13652 			continue;
13653 		/* An enabled channel in new_band should not be already enabled
13654 		 * in the orig_band
13655 		 */
13656 		if (WARN_ON(!(orig_band->channels[i].flags &
13657 			      IEEE80211_CHAN_DISABLED)))
13658 			return -EINVAL;
13659 		orig_band->channels[i].flags &= ~IEEE80211_CHAN_DISABLED;
13660 	}
13661 	return 0;
13662 }
13663 
13664 static int ath12k_mac_setup_channels_rates(struct ath12k *ar,
13665 					   u32 supported_bands,
13666 					   struct ieee80211_supported_band *bands[])
13667 {
13668 	struct ieee80211_supported_band *band;
13669 	struct ath12k_wmi_hal_reg_capabilities_ext_arg *reg_cap;
13670 	struct ath12k_base *ab = ar->ab;
13671 	u32 phy_id, freq_low, freq_high;
13672 	struct ath12k_hw *ah = ar->ah;
13673 	void *channels;
13674 	int ret;
13675 
13676 	BUILD_BUG_ON((ARRAY_SIZE(ath12k_2ghz_channels) +
13677 		      ARRAY_SIZE(ath12k_5ghz_channels) +
13678 		      ARRAY_SIZE(ath12k_6ghz_channels)) !=
13679 		     ATH12K_NUM_CHANS);
13680 
13681 	reg_cap = &ab->hal_reg_cap[ar->pdev_idx];
13682 
13683 	if (supported_bands & WMI_HOST_WLAN_2GHZ_CAP) {
13684 		channels = kmemdup(ath12k_2ghz_channels,
13685 				   sizeof(ath12k_2ghz_channels),
13686 				   GFP_KERNEL);
13687 		if (!channels)
13688 			return -ENOMEM;
13689 
13690 		band = &ar->mac.sbands[NL80211_BAND_2GHZ];
13691 		band->band = NL80211_BAND_2GHZ;
13692 		band->n_channels = ARRAY_SIZE(ath12k_2ghz_channels);
13693 		band->channels = channels;
13694 		band->n_bitrates = ath12k_g_rates_size;
13695 		band->bitrates = ath12k_g_rates;
13696 
13697 		if (ab->hw_params->single_pdev_only) {
13698 			phy_id = ath12k_get_phy_id(ar, WMI_HOST_WLAN_2GHZ_CAP);
13699 			reg_cap = &ab->hal_reg_cap[phy_id];
13700 		}
13701 
13702 		freq_low = max(reg_cap->low_2ghz_chan,
13703 			       ab->reg_freq_2ghz.start_freq);
13704 		freq_high = min(reg_cap->high_2ghz_chan,
13705 				ab->reg_freq_2ghz.end_freq);
13706 
13707 		ath12k_mac_update_ch_list(ar, band,
13708 					  reg_cap->low_2ghz_chan,
13709 					  reg_cap->high_2ghz_chan);
13710 
13711 		ath12k_mac_update_freq_range(ar, freq_low, freq_high);
13712 
13713 		if (!bands[NL80211_BAND_2GHZ]) {
13714 			bands[NL80211_BAND_2GHZ] = band;
13715 		} else {
13716 			/* Split mac in same band under same wiphy */
13717 			ret = ath12k_mac_update_band(ar, bands[NL80211_BAND_2GHZ], band);
13718 			if (ret) {
13719 				kfree(channels);
13720 				band->channels = NULL;
13721 				return ret;
13722 			}
13723 			ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac pdev %u identified as 2 GHz split mac with start freq %d end freq %d",
13724 				   ar->pdev->pdev_id,
13725 				   KHZ_TO_MHZ(ar->freq_range.start_freq),
13726 				   KHZ_TO_MHZ(ar->freq_range.end_freq));
13727 		}
13728 	}
13729 
13730 	if (supported_bands & WMI_HOST_WLAN_5GHZ_CAP) {
13731 		if (reg_cap->high_5ghz_chan >= ATH12K_MIN_6GHZ_FREQ) {
13732 			channels = kmemdup(ath12k_6ghz_channels,
13733 					   sizeof(ath12k_6ghz_channels), GFP_KERNEL);
13734 			if (!channels) {
13735 				kfree(ar->mac.sbands[NL80211_BAND_2GHZ].channels);
13736 				return -ENOMEM;
13737 			}
13738 
13739 			ar->supports_6ghz = true;
13740 			band = &ar->mac.sbands[NL80211_BAND_6GHZ];
13741 			band->band = NL80211_BAND_6GHZ;
13742 			band->n_channels = ARRAY_SIZE(ath12k_6ghz_channels);
13743 			band->channels = channels;
13744 			band->n_bitrates = ath12k_a_rates_size;
13745 			band->bitrates = ath12k_a_rates;
13746 
13747 			freq_low = max(reg_cap->low_5ghz_chan,
13748 				       ab->reg_freq_6ghz.start_freq);
13749 			freq_high = min(reg_cap->high_5ghz_chan,
13750 					ab->reg_freq_6ghz.end_freq);
13751 
13752 			ath12k_mac_update_ch_list(ar, band,
13753 						  reg_cap->low_5ghz_chan,
13754 						  reg_cap->high_5ghz_chan);
13755 
13756 			ath12k_mac_update_freq_range(ar, freq_low, freq_high);
13757 			ah->use_6ghz_regd = true;
13758 
13759 			if (!bands[NL80211_BAND_6GHZ]) {
13760 				bands[NL80211_BAND_6GHZ] = band;
13761 			} else {
13762 				/* Split mac in same band under same wiphy */
13763 				ret = ath12k_mac_update_band(ar,
13764 							     bands[NL80211_BAND_6GHZ],
13765 							     band);
13766 				if (ret) {
13767 					kfree(ar->mac.sbands[NL80211_BAND_2GHZ].channels);
13768 					ar->mac.sbands[NL80211_BAND_2GHZ].channels = NULL;
13769 					kfree(channels);
13770 					band->channels = NULL;
13771 					return ret;
13772 				}
13773 				ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac pdev %u identified as 6 GHz split mac with start freq %d end freq %d",
13774 					   ar->pdev->pdev_id,
13775 					   KHZ_TO_MHZ(ar->freq_range.start_freq),
13776 					   KHZ_TO_MHZ(ar->freq_range.end_freq));
13777 			}
13778 		}
13779 
13780 		if (reg_cap->low_5ghz_chan < ATH12K_MIN_6GHZ_FREQ) {
13781 			channels = kmemdup(ath12k_5ghz_channels,
13782 					   sizeof(ath12k_5ghz_channels),
13783 					   GFP_KERNEL);
13784 			if (!channels) {
13785 				kfree(ar->mac.sbands[NL80211_BAND_2GHZ].channels);
13786 				kfree(ar->mac.sbands[NL80211_BAND_6GHZ].channels);
13787 				return -ENOMEM;
13788 			}
13789 
13790 			band = &ar->mac.sbands[NL80211_BAND_5GHZ];
13791 			band->band = NL80211_BAND_5GHZ;
13792 			band->n_channels = ARRAY_SIZE(ath12k_5ghz_channels);
13793 			band->channels = channels;
13794 			band->n_bitrates = ath12k_a_rates_size;
13795 			band->bitrates = ath12k_a_rates;
13796 
13797 			if (ab->hw_params->single_pdev_only) {
13798 				phy_id = ath12k_get_phy_id(ar, WMI_HOST_WLAN_5GHZ_CAP);
13799 				reg_cap = &ab->hal_reg_cap[phy_id];
13800 			}
13801 
13802 			freq_low = max(reg_cap->low_5ghz_chan,
13803 				       ab->reg_freq_5ghz.start_freq);
13804 			freq_high = min(reg_cap->high_5ghz_chan,
13805 					ab->reg_freq_5ghz.end_freq);
13806 
13807 			ath12k_mac_update_ch_list(ar, band,
13808 						  reg_cap->low_5ghz_chan,
13809 						  reg_cap->high_5ghz_chan);
13810 
13811 			ath12k_mac_update_freq_range(ar, freq_low, freq_high);
13812 
13813 			if (!bands[NL80211_BAND_5GHZ]) {
13814 				bands[NL80211_BAND_5GHZ] = band;
13815 			} else {
13816 				/* Split mac in same band under same wiphy */
13817 				ret = ath12k_mac_update_band(ar,
13818 							     bands[NL80211_BAND_5GHZ],
13819 							     band);
13820 				if (ret) {
13821 					kfree(ar->mac.sbands[NL80211_BAND_2GHZ].channels);
13822 					ar->mac.sbands[NL80211_BAND_2GHZ].channels = NULL;
13823 					kfree(ar->mac.sbands[NL80211_BAND_6GHZ].channels);
13824 					ar->mac.sbands[NL80211_BAND_2GHZ].channels = NULL;
13825 					kfree(channels);
13826 					band->channels = NULL;
13827 					return ret;
13828 				}
13829 				ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac pdev %u identified as 5 GHz split mac with start freq %d end freq %d",
13830 					   ar->pdev->pdev_id,
13831 					   KHZ_TO_MHZ(ar->freq_range.start_freq),
13832 					   KHZ_TO_MHZ(ar->freq_range.end_freq));
13833 			}
13834 		}
13835 	}
13836 
13837 	return 0;
13838 }
13839 
13840 static u16 ath12k_mac_get_ifmodes(struct ath12k_hw *ah)
13841 {
13842 	struct ath12k *ar;
13843 	int i;
13844 	u16 interface_modes = U16_MAX;
13845 
13846 	for_each_ar(ah, ar, i)
13847 		interface_modes &= ar->ab->hw_params->interface_modes;
13848 
13849 	return interface_modes == U16_MAX ? 0 : interface_modes;
13850 }
13851 
13852 static bool ath12k_mac_is_iface_mode_enable(struct ath12k_hw *ah,
13853 					    enum nl80211_iftype type)
13854 {
13855 	struct ath12k *ar;
13856 	int i;
13857 	u16 interface_modes, mode = 0;
13858 	bool is_enable = false;
13859 
13860 	if (type == NL80211_IFTYPE_MESH_POINT) {
13861 		if (IS_ENABLED(CONFIG_MAC80211_MESH))
13862 			mode = BIT(type);
13863 	} else {
13864 		mode = BIT(type);
13865 	}
13866 
13867 	for_each_ar(ah, ar, i) {
13868 		interface_modes = ar->ab->hw_params->interface_modes;
13869 		if (interface_modes & mode) {
13870 			is_enable = true;
13871 			break;
13872 		}
13873 	}
13874 
13875 	return is_enable;
13876 }
13877 
13878 static int
13879 ath12k_mac_setup_radio_iface_comb(struct ath12k *ar,
13880 				  struct ieee80211_iface_combination *comb)
13881 {
13882 	u16 interface_modes = ar->ab->hw_params->interface_modes;
13883 	struct ieee80211_iface_limit *limits;
13884 	int n_limits, max_interfaces;
13885 	bool ap, mesh, p2p;
13886 
13887 	ap = interface_modes & BIT(NL80211_IFTYPE_AP);
13888 	p2p = interface_modes & BIT(NL80211_IFTYPE_P2P_DEVICE);
13889 
13890 	mesh = IS_ENABLED(CONFIG_MAC80211_MESH) &&
13891 	       (interface_modes & BIT(NL80211_IFTYPE_MESH_POINT));
13892 
13893 	if ((ap || mesh) && !p2p) {
13894 		n_limits = 2;
13895 		max_interfaces = 16;
13896 	} else if (p2p) {
13897 		n_limits = 3;
13898 		if (ap || mesh)
13899 			max_interfaces = 16;
13900 		else
13901 			max_interfaces = 3;
13902 	} else {
13903 		n_limits = 1;
13904 		max_interfaces = 1;
13905 	}
13906 
13907 	limits = kcalloc(n_limits, sizeof(*limits), GFP_KERNEL);
13908 	if (!limits)
13909 		return -ENOMEM;
13910 
13911 	limits[0].max = 1;
13912 	limits[0].types |= BIT(NL80211_IFTYPE_STATION);
13913 
13914 	if (ap || mesh || p2p)
13915 		limits[1].max = max_interfaces;
13916 
13917 	if (ap)
13918 		limits[1].types |= BIT(NL80211_IFTYPE_AP);
13919 
13920 	if (mesh)
13921 		limits[1].types |= BIT(NL80211_IFTYPE_MESH_POINT);
13922 
13923 	if (p2p) {
13924 		limits[1].types |= BIT(NL80211_IFTYPE_P2P_CLIENT) |
13925 					BIT(NL80211_IFTYPE_P2P_GO);
13926 		limits[2].max = 1;
13927 		limits[2].types |= BIT(NL80211_IFTYPE_P2P_DEVICE);
13928 	}
13929 
13930 	comb[0].limits = limits;
13931 	comb[0].n_limits = n_limits;
13932 	comb[0].max_interfaces = max_interfaces;
13933 	comb[0].beacon_int_infra_match = true;
13934 	comb[0].beacon_int_min_gcd = 100;
13935 
13936 	comb[0].num_different_channels = 1;
13937 	comb[0].radar_detect_widths = BIT(NL80211_CHAN_WIDTH_20_NOHT) |
13938 				      BIT(NL80211_CHAN_WIDTH_20) |
13939 				      BIT(NL80211_CHAN_WIDTH_40) |
13940 				      BIT(NL80211_CHAN_WIDTH_80) |
13941 				      BIT(NL80211_CHAN_WIDTH_160);
13942 
13943 	return 0;
13944 }
13945 
13946 static int
13947 ath12k_mac_setup_global_iface_comb(struct ath12k_hw *ah,
13948 				   struct wiphy_radio *radio,
13949 				   u8 n_radio,
13950 				   struct ieee80211_iface_combination *comb)
13951 {
13952 	const struct ieee80211_iface_combination *iter_comb;
13953 	struct ieee80211_iface_limit *limits;
13954 	int i, j, n_limits;
13955 	bool ap, mesh, p2p;
13956 
13957 	if (!n_radio)
13958 		return 0;
13959 
13960 	ap = ath12k_mac_is_iface_mode_enable(ah, NL80211_IFTYPE_AP);
13961 	p2p = ath12k_mac_is_iface_mode_enable(ah, NL80211_IFTYPE_P2P_DEVICE);
13962 	mesh = ath12k_mac_is_iface_mode_enable(ah, NL80211_IFTYPE_MESH_POINT);
13963 
13964 	if ((ap || mesh) && !p2p)
13965 		n_limits = 2;
13966 	else if (p2p)
13967 		n_limits = 3;
13968 	else
13969 		n_limits = 1;
13970 
13971 	limits = kcalloc(n_limits, sizeof(*limits), GFP_KERNEL);
13972 	if (!limits)
13973 		return -ENOMEM;
13974 
13975 	for (i = 0; i < n_radio; i++) {
13976 		iter_comb = radio[i].iface_combinations;
13977 		for (j = 0; j < iter_comb->n_limits && j < n_limits; j++) {
13978 			limits[j].types |= iter_comb->limits[j].types;
13979 			limits[j].max += iter_comb->limits[j].max;
13980 		}
13981 
13982 		comb->max_interfaces += iter_comb->max_interfaces;
13983 		comb->num_different_channels += iter_comb->num_different_channels;
13984 		comb->radar_detect_widths |= iter_comb->radar_detect_widths;
13985 	}
13986 
13987 	comb->limits = limits;
13988 	comb->n_limits = n_limits;
13989 	comb->beacon_int_infra_match = true;
13990 	comb->beacon_int_min_gcd = 100;
13991 
13992 	return 0;
13993 }
13994 
13995 static
13996 void ath12k_mac_cleanup_iface_comb(const struct ieee80211_iface_combination *iface_comb)
13997 {
13998 	kfree(iface_comb[0].limits);
13999 	kfree(iface_comb);
14000 }
14001 
14002 static void ath12k_mac_cleanup_iface_combinations(struct ath12k_hw *ah)
14003 {
14004 	struct wiphy *wiphy = ah->hw->wiphy;
14005 	const struct wiphy_radio *radio;
14006 	int i;
14007 
14008 	if (wiphy->n_radio > 0) {
14009 		radio = wiphy->radio;
14010 		for (i = 0; i < wiphy->n_radio; i++)
14011 			ath12k_mac_cleanup_iface_comb(radio[i].iface_combinations);
14012 
14013 		kfree(wiphy->radio);
14014 	}
14015 
14016 	ath12k_mac_cleanup_iface_comb(wiphy->iface_combinations);
14017 }
14018 
14019 static int ath12k_mac_setup_iface_combinations(struct ath12k_hw *ah)
14020 {
14021 	struct ieee80211_iface_combination *combinations, *comb;
14022 	struct wiphy *wiphy = ah->hw->wiphy;
14023 	struct wiphy_radio *radio;
14024 	int n_combinations = 1;
14025 	struct ath12k *ar;
14026 	int i, ret;
14027 
14028 	if (ah->num_radio == 1) {
14029 		ar = &ah->radio[0];
14030 
14031 		if (ar->ab->hw_params->single_pdev_only)
14032 			n_combinations = 2;
14033 
14034 		combinations = kcalloc(n_combinations, sizeof(*combinations),
14035 				       GFP_KERNEL);
14036 		if (!combinations)
14037 			return -ENOMEM;
14038 
14039 		ret = ath12k_mac_setup_radio_iface_comb(ar, combinations);
14040 		if (ret) {
14041 			ath12k_hw_warn(ah, "failed to setup radio interface combinations for one radio: %d",
14042 				       ret);
14043 			goto err_free_combinations;
14044 		}
14045 
14046 		if (ar->ab->hw_params->single_pdev_only) {
14047 			comb = combinations + 1;
14048 			memcpy(comb, combinations, sizeof(*comb));
14049 			comb->num_different_channels = 2;
14050 			comb->radar_detect_widths = 0;
14051 		}
14052 
14053 		goto out;
14054 	}
14055 
14056 	combinations = kcalloc(n_combinations, sizeof(*combinations), GFP_KERNEL);
14057 	if (!combinations)
14058 		return -ENOMEM;
14059 
14060 	/* there are multiple radios */
14061 
14062 	radio = kcalloc(ah->num_radio, sizeof(*radio), GFP_KERNEL);
14063 	if (!radio) {
14064 		ret = -ENOMEM;
14065 		goto err_free_combinations;
14066 	}
14067 
14068 	for_each_ar(ah, ar, i) {
14069 		comb = kzalloc(sizeof(*comb), GFP_KERNEL);
14070 		if (!comb) {
14071 			ret = -ENOMEM;
14072 			goto err_free_radios;
14073 		}
14074 
14075 		ret = ath12k_mac_setup_radio_iface_comb(ar, comb);
14076 		if (ret) {
14077 			ath12k_hw_warn(ah, "failed to setup radio interface combinations for radio %d: %d",
14078 				       i, ret);
14079 			kfree(comb);
14080 			goto err_free_radios;
14081 		}
14082 
14083 		radio[i].freq_range = &ar->freq_range;
14084 		radio[i].n_freq_range = 1;
14085 
14086 		radio[i].iface_combinations = comb;
14087 		radio[i].n_iface_combinations = 1;
14088 	}
14089 
14090 	ret = ath12k_mac_setup_global_iface_comb(ah, radio, ah->num_radio, combinations);
14091 	if (ret) {
14092 		ath12k_hw_warn(ah, "failed to setup global interface combinations: %d",
14093 			       ret);
14094 		goto err_free_all_radios;
14095 	}
14096 
14097 	wiphy->radio = radio;
14098 	wiphy->n_radio = ah->num_radio;
14099 
14100 out:
14101 	wiphy->iface_combinations = combinations;
14102 	wiphy->n_iface_combinations = n_combinations;
14103 
14104 	return 0;
14105 
14106 err_free_all_radios:
14107 	i = ah->num_radio;
14108 
14109 err_free_radios:
14110 	while (i--)
14111 		ath12k_mac_cleanup_iface_comb(radio[i].iface_combinations);
14112 
14113 	kfree(radio);
14114 
14115 err_free_combinations:
14116 	kfree(combinations);
14117 
14118 	return ret;
14119 }
14120 
14121 static const u8 ath12k_if_types_ext_capa[] = {
14122 	[0] = WLAN_EXT_CAPA1_EXT_CHANNEL_SWITCHING,
14123 	[2] = WLAN_EXT_CAPA3_MULTI_BSSID_SUPPORT,
14124 	[7] = WLAN_EXT_CAPA8_OPMODE_NOTIF,
14125 };
14126 
14127 static const u8 ath12k_if_types_ext_capa_sta[] = {
14128 	[0] = WLAN_EXT_CAPA1_EXT_CHANNEL_SWITCHING,
14129 	[2] = WLAN_EXT_CAPA3_MULTI_BSSID_SUPPORT,
14130 	[7] = WLAN_EXT_CAPA8_OPMODE_NOTIF,
14131 	[9] = WLAN_EXT_CAPA10_TWT_REQUESTER_SUPPORT,
14132 };
14133 
14134 static const u8 ath12k_if_types_ext_capa_ap[] = {
14135 	[0] = WLAN_EXT_CAPA1_EXT_CHANNEL_SWITCHING,
14136 	[2] = WLAN_EXT_CAPA3_MULTI_BSSID_SUPPORT,
14137 	[7] = WLAN_EXT_CAPA8_OPMODE_NOTIF,
14138 	[9] = WLAN_EXT_CAPA10_TWT_RESPONDER_SUPPORT,
14139 	[10] = WLAN_EXT_CAPA11_EMA_SUPPORT,
14140 };
14141 
14142 static struct wiphy_iftype_ext_capab ath12k_iftypes_ext_capa[] = {
14143 	{
14144 		.extended_capabilities = ath12k_if_types_ext_capa,
14145 		.extended_capabilities_mask = ath12k_if_types_ext_capa,
14146 		.extended_capabilities_len = sizeof(ath12k_if_types_ext_capa),
14147 	}, {
14148 		.iftype = NL80211_IFTYPE_STATION,
14149 		.extended_capabilities = ath12k_if_types_ext_capa_sta,
14150 		.extended_capabilities_mask = ath12k_if_types_ext_capa_sta,
14151 		.extended_capabilities_len =
14152 				sizeof(ath12k_if_types_ext_capa_sta),
14153 	}, {
14154 		.iftype = NL80211_IFTYPE_AP,
14155 		.extended_capabilities = ath12k_if_types_ext_capa_ap,
14156 		.extended_capabilities_mask = ath12k_if_types_ext_capa_ap,
14157 		.extended_capabilities_len =
14158 				sizeof(ath12k_if_types_ext_capa_ap),
14159 		.eml_capabilities = 0,
14160 		.mld_capa_and_ops = 0,
14161 	},
14162 };
14163 
14164 static void ath12k_mac_cleanup_unregister(struct ath12k *ar)
14165 {
14166 	idr_for_each(&ar->txmgmt_idr, ath12k_mac_tx_mgmt_pending_free, ar);
14167 	idr_destroy(&ar->txmgmt_idr);
14168 
14169 	kfree(ar->mac.sbands[NL80211_BAND_2GHZ].channels);
14170 	kfree(ar->mac.sbands[NL80211_BAND_5GHZ].channels);
14171 	kfree(ar->mac.sbands[NL80211_BAND_6GHZ].channels);
14172 }
14173 
14174 static void ath12k_mac_hw_unregister(struct ath12k_hw *ah)
14175 {
14176 	struct ieee80211_hw *hw = ah->hw;
14177 	struct ath12k *ar;
14178 	int i;
14179 
14180 	for_each_ar(ah, ar, i) {
14181 		cancel_work_sync(&ar->regd_channel_update_work);
14182 		cancel_work_sync(&ar->regd_update_work);
14183 		ath12k_debugfs_unregister(ar);
14184 		ath12k_fw_stats_reset(ar);
14185 	}
14186 
14187 	ieee80211_unregister_hw(hw);
14188 
14189 	for_each_ar(ah, ar, i)
14190 		ath12k_mac_cleanup_unregister(ar);
14191 
14192 	ath12k_mac_cleanup_iface_combinations(ah);
14193 
14194 	SET_IEEE80211_DEV(hw, NULL);
14195 }
14196 
14197 static int ath12k_mac_setup_register(struct ath12k *ar,
14198 				     u32 *ht_cap,
14199 				     struct ieee80211_supported_band *bands[])
14200 {
14201 	struct ath12k_pdev_cap *cap = &ar->pdev->cap;
14202 	int ret;
14203 
14204 	init_waitqueue_head(&ar->txmgmt_empty_waitq);
14205 	idr_init(&ar->txmgmt_idr);
14206 	spin_lock_init(&ar->txmgmt_idr_lock);
14207 
14208 	ath12k_pdev_caps_update(ar);
14209 
14210 	ret = ath12k_mac_setup_channels_rates(ar,
14211 					      cap->supported_bands,
14212 					      bands);
14213 	if (ret)
14214 		return ret;
14215 
14216 	ath12k_mac_setup_ht_vht_cap(ar, cap, ht_cap);
14217 	ath12k_mac_setup_sband_iftype_data(ar, cap);
14218 
14219 	ar->max_num_stations = ath12k_core_get_max_station_per_radio(ar->ab);
14220 	ar->max_num_peers = ath12k_core_get_max_peers_per_radio(ar->ab);
14221 
14222 	ar->rssi_info.min_nf_dbm = ATH12K_DEFAULT_NOISE_FLOOR;
14223 	ar->rssi_info.temp_offset = 0;
14224 	ar->rssi_info.noise_floor = ar->rssi_info.min_nf_dbm + ar->rssi_info.temp_offset;
14225 
14226 	return 0;
14227 }
14228 
14229 static int ath12k_mac_hw_register(struct ath12k_hw *ah)
14230 {
14231 	struct ieee80211_hw *hw = ah->hw;
14232 	struct wiphy *wiphy = hw->wiphy;
14233 	struct ath12k *ar = ath12k_ah_to_ar(ah, 0);
14234 	struct ath12k_base *ab = ar->ab;
14235 	struct ath12k_pdev *pdev;
14236 	struct ath12k_pdev_cap *cap;
14237 	static const u32 cipher_suites[] = {
14238 		WLAN_CIPHER_SUITE_TKIP,
14239 		WLAN_CIPHER_SUITE_CCMP,
14240 		WLAN_CIPHER_SUITE_AES_CMAC,
14241 		WLAN_CIPHER_SUITE_BIP_CMAC_256,
14242 		WLAN_CIPHER_SUITE_BIP_GMAC_128,
14243 		WLAN_CIPHER_SUITE_BIP_GMAC_256,
14244 		WLAN_CIPHER_SUITE_GCMP,
14245 		WLAN_CIPHER_SUITE_GCMP_256,
14246 		WLAN_CIPHER_SUITE_CCMP_256,
14247 	};
14248 	int ret, i, j;
14249 	u32 ht_cap = U32_MAX, antennas_rx = 0, antennas_tx = 0;
14250 	bool is_6ghz = false, is_raw_mode = false, is_monitor_disable = false;
14251 	u8 *mac_addr = NULL;
14252 	u8 mbssid_max_interfaces = 0;
14253 
14254 	wiphy->max_ap_assoc_sta = 0;
14255 
14256 	for_each_ar(ah, ar, i) {
14257 		u32 ht_cap_info = 0;
14258 
14259 		pdev = ar->pdev;
14260 		if (ar->ab->pdevs_macaddr_valid) {
14261 			ether_addr_copy(ar->mac_addr, pdev->mac_addr);
14262 		} else {
14263 			ether_addr_copy(ar->mac_addr, ar->ab->mac_addr);
14264 			ar->mac_addr[4] += ar->pdev_idx;
14265 		}
14266 
14267 		ret = ath12k_mac_setup_register(ar, &ht_cap_info, hw->wiphy->bands);
14268 		if (ret)
14269 			goto err_cleanup_unregister;
14270 
14271 		/* 6 GHz does not support HT Cap, hence do not consider it */
14272 		if (!ar->supports_6ghz)
14273 			ht_cap &= ht_cap_info;
14274 
14275 		wiphy->max_ap_assoc_sta += ar->max_num_stations;
14276 
14277 		/* Advertise the max antenna support of all radios, driver can handle
14278 		 * per pdev specific antenna setting based on pdev cap when antenna
14279 		 * changes are made
14280 		 */
14281 		cap = &pdev->cap;
14282 
14283 		antennas_rx = max_t(u32, antennas_rx, cap->rx_chain_mask);
14284 		antennas_tx = max_t(u32, antennas_tx, cap->tx_chain_mask);
14285 
14286 		if (ar->supports_6ghz)
14287 			is_6ghz = true;
14288 
14289 		if (test_bit(ATH12K_FLAG_RAW_MODE, &ar->ab->dev_flags))
14290 			is_raw_mode = true;
14291 
14292 		if (!ar->ab->hw_params->supports_monitor)
14293 			is_monitor_disable = true;
14294 
14295 		if (i == 0)
14296 			mac_addr = ar->mac_addr;
14297 		else
14298 			mac_addr = ab->mac_addr;
14299 
14300 		mbssid_max_interfaces += TARGET_NUM_VDEVS(ar->ab);
14301 	}
14302 
14303 	wiphy->available_antennas_rx = antennas_rx;
14304 	wiphy->available_antennas_tx = antennas_tx;
14305 
14306 	SET_IEEE80211_PERM_ADDR(hw, mac_addr);
14307 	SET_IEEE80211_DEV(hw, ab->dev);
14308 
14309 	ret = ath12k_mac_setup_iface_combinations(ah);
14310 	if (ret) {
14311 		ath12k_err(ab, "failed to setup interface combinations: %d\n", ret);
14312 		goto err_complete_cleanup_unregister;
14313 	}
14314 
14315 	wiphy->interface_modes = ath12k_mac_get_ifmodes(ah);
14316 
14317 	if (ah->num_radio == 1 &&
14318 	    wiphy->bands[NL80211_BAND_2GHZ] &&
14319 	    wiphy->bands[NL80211_BAND_5GHZ] &&
14320 	    wiphy->bands[NL80211_BAND_6GHZ])
14321 		ieee80211_hw_set(hw, SINGLE_SCAN_ON_ALL_BANDS);
14322 
14323 	ieee80211_hw_set(hw, SIGNAL_DBM);
14324 	ieee80211_hw_set(hw, SUPPORTS_PS);
14325 	ieee80211_hw_set(hw, SUPPORTS_DYNAMIC_PS);
14326 	ieee80211_hw_set(hw, MFP_CAPABLE);
14327 	ieee80211_hw_set(hw, REPORTS_TX_ACK_STATUS);
14328 	ieee80211_hw_set(hw, HAS_RATE_CONTROL);
14329 	ieee80211_hw_set(hw, AP_LINK_PS);
14330 	ieee80211_hw_set(hw, SPECTRUM_MGMT);
14331 	ieee80211_hw_set(hw, CONNECTION_MONITOR);
14332 	ieee80211_hw_set(hw, SUPPORTS_PER_STA_GTK);
14333 	ieee80211_hw_set(hw, CHANCTX_STA_CSA);
14334 	ieee80211_hw_set(hw, QUEUE_CONTROL);
14335 	ieee80211_hw_set(hw, SUPPORTS_TX_FRAG);
14336 	ieee80211_hw_set(hw, REPORTS_LOW_ACK);
14337 	ieee80211_hw_set(hw, NO_VIRTUAL_MONITOR);
14338 
14339 	if (test_bit(WMI_TLV_SERVICE_ETH_OFFLOAD, ar->wmi->wmi_ab->svc_map)) {
14340 		ieee80211_hw_set(hw, SUPPORTS_TX_ENCAP_OFFLOAD);
14341 		ieee80211_hw_set(hw, SUPPORTS_RX_DECAP_OFFLOAD);
14342 	}
14343 
14344 	if (cap->nss_ratio_enabled)
14345 		ieee80211_hw_set(hw, SUPPORTS_VHT_EXT_NSS_BW);
14346 
14347 	if ((ht_cap & WMI_HT_CAP_ENABLED) || is_6ghz) {
14348 		ieee80211_hw_set(hw, AMPDU_AGGREGATION);
14349 		ieee80211_hw_set(hw, TX_AMPDU_SETUP_IN_HW);
14350 		ieee80211_hw_set(hw, SUPPORTS_REORDERING_BUFFER);
14351 		ieee80211_hw_set(hw, SUPPORTS_AMSDU_IN_AMPDU);
14352 		ieee80211_hw_set(hw, USES_RSS);
14353 	}
14354 
14355 	wiphy->features |= NL80211_FEATURE_STATIC_SMPS;
14356 	wiphy->flags |= WIPHY_FLAG_IBSS_RSN;
14357 
14358 	/* TODO: Check if HT capability advertised from firmware is different
14359 	 * for each band for a dual band capable radio. It will be tricky to
14360 	 * handle it when the ht capability different for each band.
14361 	 */
14362 	if (ht_cap & WMI_HT_CAP_DYNAMIC_SMPS ||
14363 	    (is_6ghz && ab->hw_params->supports_dynamic_smps_6ghz))
14364 		wiphy->features |= NL80211_FEATURE_DYNAMIC_SMPS;
14365 
14366 	wiphy->max_scan_ssids = WLAN_SCAN_PARAMS_MAX_SSID;
14367 	wiphy->max_scan_ie_len = WLAN_SCAN_PARAMS_MAX_IE_LEN;
14368 
14369 	hw->max_listen_interval = ATH12K_MAX_HW_LISTEN_INTERVAL;
14370 
14371 	wiphy->flags |= WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL;
14372 	wiphy->flags |= WIPHY_FLAG_HAS_CHANNEL_SWITCH;
14373 	wiphy->max_remain_on_channel_duration = 5000;
14374 
14375 	wiphy->flags |= WIPHY_FLAG_AP_UAPSD;
14376 	wiphy->features |= NL80211_FEATURE_AP_MODE_CHAN_WIDTH_CHANGE |
14377 				   NL80211_FEATURE_AP_SCAN;
14378 
14379 	wiphy->features |= NL80211_FEATURE_TX_POWER_INSERTION;
14380 
14381 	/* MLO is not yet supported so disable Wireless Extensions for now
14382 	 * to make sure ath12k users don't use it. This flag can be removed
14383 	 * once WIPHY_FLAG_SUPPORTS_MLO is enabled.
14384 	 */
14385 	wiphy->flags |= WIPHY_FLAG_DISABLE_WEXT;
14386 
14387 	/* Copy over MLO related capabilities received from
14388 	 * WMI_SERVICE_READY_EXT2_EVENT if single_chip_mlo_supp is set.
14389 	 */
14390 	if (ab->ag->mlo_capable) {
14391 		ath12k_iftypes_ext_capa[2].eml_capabilities = cap->eml_cap;
14392 		ath12k_iftypes_ext_capa[2].mld_capa_and_ops = cap->mld_cap;
14393 		wiphy->flags |= WIPHY_FLAG_SUPPORTS_MLO;
14394 
14395 		ieee80211_hw_set(hw, MLO_MCAST_MULTI_LINK_TX);
14396 	}
14397 
14398 	hw->queues = ATH12K_HW_MAX_QUEUES;
14399 	wiphy->tx_queue_len = ATH12K_QUEUE_LEN;
14400 	hw->offchannel_tx_hw_queue = ATH12K_HW_MAX_QUEUES - 1;
14401 	hw->max_rx_aggregation_subframes = IEEE80211_MAX_AMPDU_BUF_EHT;
14402 
14403 	hw->vif_data_size = sizeof(struct ath12k_vif);
14404 	hw->sta_data_size = sizeof(struct ath12k_sta);
14405 	hw->extra_tx_headroom = ab->hw_params->iova_mask;
14406 
14407 	wiphy_ext_feature_set(wiphy, NL80211_EXT_FEATURE_CQM_RSSI_LIST);
14408 	wiphy_ext_feature_set(wiphy, NL80211_EXT_FEATURE_STA_TX_PWR);
14409 	wiphy_ext_feature_set(wiphy, NL80211_EXT_FEATURE_ACK_SIGNAL_SUPPORT);
14410 	if (test_bit(WMI_TLV_SERVICE_BSS_COLOR_OFFLOAD,
14411 		     ab->wmi_ab.svc_map)) {
14412 		wiphy_ext_feature_set(wiphy, NL80211_EXT_FEATURE_BSS_COLOR);
14413 		ieee80211_hw_set(hw, DETECTS_COLOR_COLLISION);
14414 	}
14415 
14416 	wiphy->cipher_suites = cipher_suites;
14417 	wiphy->n_cipher_suites = ARRAY_SIZE(cipher_suites);
14418 
14419 	wiphy->iftype_ext_capab = ath12k_iftypes_ext_capa;
14420 	wiphy->num_iftype_ext_capab = ARRAY_SIZE(ath12k_iftypes_ext_capa);
14421 
14422 	wiphy->mbssid_max_interfaces = mbssid_max_interfaces;
14423 	wiphy->ema_max_profile_periodicity = TARGET_EMA_MAX_PROFILE_PERIOD;
14424 	ieee80211_hw_set(hw, SUPPORTS_MULTI_BSSID);
14425 
14426 	if (is_6ghz) {
14427 		wiphy_ext_feature_set(wiphy,
14428 				      NL80211_EXT_FEATURE_FILS_DISCOVERY);
14429 		wiphy_ext_feature_set(wiphy,
14430 				      NL80211_EXT_FEATURE_UNSOL_BCAST_PROBE_RESP);
14431 	}
14432 
14433 	wiphy_ext_feature_set(wiphy, NL80211_EXT_FEATURE_PUNCT);
14434 	if (test_bit(WMI_TLV_SERVICE_BEACON_PROTECTION_SUPPORT, ab->wmi_ab.svc_map))
14435 		wiphy_ext_feature_set(hw->wiphy, NL80211_EXT_FEATURE_BEACON_PROTECTION);
14436 
14437 	ath12k_reg_init(hw);
14438 
14439 	if (!is_raw_mode) {
14440 		hw->netdev_features = NETIF_F_HW_CSUM;
14441 		ieee80211_hw_set(hw, SW_CRYPTO_CONTROL);
14442 		ieee80211_hw_set(hw, SUPPORT_FAST_XMIT);
14443 	}
14444 
14445 	if (test_bit(WMI_TLV_SERVICE_NLO, ar->wmi->wmi_ab->svc_map)) {
14446 		wiphy->max_sched_scan_ssids = WMI_PNO_MAX_SUPP_NETWORKS;
14447 		wiphy->max_match_sets = WMI_PNO_MAX_SUPP_NETWORKS;
14448 		wiphy->max_sched_scan_ie_len = WMI_PNO_MAX_IE_LENGTH;
14449 		wiphy->max_sched_scan_plans = WMI_PNO_MAX_SCHED_SCAN_PLANS;
14450 		wiphy->max_sched_scan_plan_interval =
14451 					WMI_PNO_MAX_SCHED_SCAN_PLAN_INT;
14452 		wiphy->max_sched_scan_plan_iterations =
14453 					WMI_PNO_MAX_SCHED_SCAN_PLAN_ITRNS;
14454 		wiphy->features |= NL80211_FEATURE_ND_RANDOM_MAC_ADDR;
14455 	}
14456 
14457 	ret = ath12k_wow_init(ar);
14458 	if (ret) {
14459 		ath12k_warn(ar->ab, "failed to init wow: %d\n", ret);
14460 		goto err_cleanup_if_combs;
14461 	}
14462 
14463 	/* Boot-time regulatory updates have already been processed.
14464 	 * Mark them as complete now, because after registration,
14465 	 * cfg80211 will notify us again if there are any pending hints.
14466 	 * We need to wait for those hints to be processed, so it's
14467 	 * important to mark the boot-time updates as complete before
14468 	 * proceeding with registration.
14469 	 */
14470 	for_each_ar(ah, ar, i)
14471 		complete_all(&ar->regd_update_completed);
14472 
14473 	ret = ieee80211_register_hw(hw);
14474 	if (ret) {
14475 		ath12k_err(ab, "ieee80211 registration failed: %d\n", ret);
14476 		goto err_cleanup_if_combs;
14477 	}
14478 
14479 	if (is_monitor_disable)
14480 		/* There's a race between calling ieee80211_register_hw()
14481 		 * and here where the monitor mode is enabled for a little
14482 		 * while. But that time is so short and in practise it make
14483 		 * a difference in real life.
14484 		 */
14485 		wiphy->interface_modes &= ~BIT(NL80211_IFTYPE_MONITOR);
14486 
14487 	for_each_ar(ah, ar, i) {
14488 		/* Apply the regd received during initialization */
14489 		ret = ath12k_regd_update(ar, true);
14490 		if (ret) {
14491 			ath12k_err(ar->ab, "ath12k regd update failed: %d\n", ret);
14492 			goto err_unregister_hw;
14493 		}
14494 
14495 		if (ar->ab->hw_params->current_cc_support && ab->new_alpha2[0]) {
14496 			struct wmi_set_current_country_arg current_cc = {};
14497 
14498 			memcpy(&current_cc.alpha2, ab->new_alpha2, 2);
14499 			memcpy(&ar->alpha2, ab->new_alpha2, 2);
14500 
14501 			reinit_completion(&ar->regd_update_completed);
14502 
14503 			ret = ath12k_wmi_send_set_current_country_cmd(ar, &current_cc);
14504 			if (ret)
14505 				ath12k_warn(ar->ab,
14506 					    "failed set cc code for mac register: %d\n",
14507 					    ret);
14508 		}
14509 
14510 		ath12k_fw_stats_init(ar);
14511 		ath12k_debugfs_register(ar);
14512 	}
14513 
14514 	return 0;
14515 
14516 err_unregister_hw:
14517 	for_each_ar(ah, ar, i)
14518 		ath12k_debugfs_unregister(ar);
14519 
14520 	ieee80211_unregister_hw(hw);
14521 
14522 err_cleanup_if_combs:
14523 	ath12k_mac_cleanup_iface_combinations(ah);
14524 
14525 err_complete_cleanup_unregister:
14526 	i = ah->num_radio;
14527 
14528 err_cleanup_unregister:
14529 	for (j = 0; j < i; j++) {
14530 		ar = ath12k_ah_to_ar(ah, j);
14531 		ath12k_mac_cleanup_unregister(ar);
14532 	}
14533 
14534 	SET_IEEE80211_DEV(hw, NULL);
14535 
14536 	return ret;
14537 }
14538 
14539 static void ath12k_mac_setup(struct ath12k *ar)
14540 {
14541 	struct ath12k_base *ab = ar->ab;
14542 	struct ath12k_pdev *pdev = ar->pdev;
14543 	u8 pdev_idx = ar->pdev_idx;
14544 
14545 	ar->lmac_id = ath12k_hw_get_mac_from_pdev_id(ab->hw_params, pdev_idx);
14546 
14547 	ar->wmi = &ab->wmi_ab.wmi[pdev_idx];
14548 	/* FIXME: wmi[0] is already initialized during attach,
14549 	 * Should we do this again?
14550 	 */
14551 	ath12k_wmi_pdev_attach(ab, pdev_idx);
14552 
14553 	ar->cfg_tx_chainmask = pdev->cap.tx_chain_mask;
14554 	ar->cfg_rx_chainmask = pdev->cap.rx_chain_mask;
14555 	ar->num_tx_chains = hweight32(pdev->cap.tx_chain_mask);
14556 	ar->num_rx_chains = hweight32(pdev->cap.rx_chain_mask);
14557 	ar->scan.arvif = NULL;
14558 	ar->vdev_id_11d_scan = ATH12K_11D_INVALID_VDEV_ID;
14559 
14560 	spin_lock_init(&ar->data_lock);
14561 	INIT_LIST_HEAD(&ar->arvifs);
14562 	INIT_LIST_HEAD(&ar->ppdu_stats_info);
14563 
14564 	init_completion(&ar->vdev_setup_done);
14565 	init_completion(&ar->vdev_delete_done);
14566 	init_completion(&ar->peer_assoc_done);
14567 	init_completion(&ar->peer_delete_done);
14568 	init_completion(&ar->install_key_done);
14569 	init_completion(&ar->bss_survey_done);
14570 	init_completion(&ar->scan.started);
14571 	init_completion(&ar->scan.completed);
14572 	init_completion(&ar->scan.on_channel);
14573 	init_completion(&ar->mlo_setup_done);
14574 	init_completion(&ar->completed_11d_scan);
14575 	init_completion(&ar->regd_update_completed);
14576 
14577 	INIT_DELAYED_WORK(&ar->scan.timeout, ath12k_scan_timeout_work);
14578 	wiphy_work_init(&ar->scan.vdev_clean_wk, ath12k_scan_vdev_clean_work);
14579 	INIT_WORK(&ar->regd_channel_update_work, ath12k_regd_update_chan_list_work);
14580 	INIT_LIST_HEAD(&ar->regd_channel_update_queue);
14581 	INIT_WORK(&ar->regd_update_work, ath12k_regd_update_work);
14582 
14583 	wiphy_work_init(&ar->wmi_mgmt_tx_work, ath12k_mgmt_over_wmi_tx_work);
14584 	skb_queue_head_init(&ar->wmi_mgmt_tx_queue);
14585 
14586 	ar->monitor_vdev_id = -1;
14587 	ar->monitor_vdev_created = false;
14588 	ar->monitor_started = false;
14589 }
14590 
14591 static int __ath12k_mac_mlo_setup(struct ath12k *ar)
14592 {
14593 	u8 num_link = 0, partner_link_id[ATH12K_GROUP_MAX_RADIO] = {};
14594 	struct ath12k_base *partner_ab, *ab = ar->ab;
14595 	struct ath12k_hw_group *ag = ab->ag;
14596 	struct wmi_mlo_setup_arg mlo = {};
14597 	struct ath12k_pdev *pdev;
14598 	unsigned long time_left;
14599 	int i, j, ret;
14600 
14601 	lockdep_assert_held(&ag->mutex);
14602 
14603 	reinit_completion(&ar->mlo_setup_done);
14604 
14605 	for (i = 0; i < ag->num_devices; i++) {
14606 		partner_ab = ag->ab[i];
14607 
14608 		for (j = 0; j < partner_ab->num_radios; j++) {
14609 			pdev = &partner_ab->pdevs[j];
14610 
14611 			/* Avoid the self link */
14612 			if (ar == pdev->ar)
14613 				continue;
14614 
14615 			partner_link_id[num_link] = pdev->hw_link_id;
14616 			num_link++;
14617 
14618 			ath12k_dbg(ab, ATH12K_DBG_MAC, "device %d pdev %d hw_link_id %d num_link %d\n",
14619 				   i, j, pdev->hw_link_id, num_link);
14620 		}
14621 	}
14622 
14623 	if (num_link == 0)
14624 		return 0;
14625 
14626 	mlo.group_id = cpu_to_le32(ag->id);
14627 	mlo.partner_link_id = partner_link_id;
14628 	mlo.num_partner_links = num_link;
14629 	ar->mlo_setup_status = 0;
14630 
14631 	ath12k_dbg(ab, ATH12K_DBG_MAC, "group id %d num_link %d\n", ag->id, num_link);
14632 
14633 	ret = ath12k_wmi_mlo_setup(ar, &mlo);
14634 	if (ret) {
14635 		ath12k_err(ab, "failed to send  setup MLO WMI command for pdev %d: %d\n",
14636 			   ar->pdev_idx, ret);
14637 		return ret;
14638 	}
14639 
14640 	time_left = wait_for_completion_timeout(&ar->mlo_setup_done,
14641 						WMI_MLO_CMD_TIMEOUT_HZ);
14642 
14643 	if (!time_left || ar->mlo_setup_status)
14644 		return ar->mlo_setup_status ? : -ETIMEDOUT;
14645 
14646 	ath12k_dbg(ab, ATH12K_DBG_MAC, "mlo setup done for pdev %d\n", ar->pdev_idx);
14647 
14648 	return 0;
14649 }
14650 
14651 static int __ath12k_mac_mlo_teardown(struct ath12k *ar)
14652 {
14653 	struct ath12k_base *ab = ar->ab;
14654 	int ret;
14655 	u8 num_link;
14656 
14657 	if (test_bit(ATH12K_FLAG_RECOVERY, &ab->dev_flags))
14658 		return 0;
14659 
14660 	num_link = ath12k_get_num_partner_link(ar);
14661 
14662 	if (num_link == 0)
14663 		return 0;
14664 
14665 	ret = ath12k_wmi_mlo_teardown(ar);
14666 	if (ret) {
14667 		ath12k_warn(ab, "failed to send MLO teardown WMI command for pdev %d: %d\n",
14668 			    ar->pdev_idx, ret);
14669 		return ret;
14670 	}
14671 
14672 	ath12k_dbg(ab, ATH12K_DBG_MAC, "mlo teardown for pdev %d\n", ar->pdev_idx);
14673 
14674 	return 0;
14675 }
14676 
14677 int ath12k_mac_mlo_setup(struct ath12k_hw_group *ag)
14678 {
14679 	struct ath12k_hw *ah;
14680 	struct ath12k *ar;
14681 	int ret;
14682 	int i, j;
14683 
14684 	for (i = 0; i < ag->num_hw; i++) {
14685 		ah = ag->ah[i];
14686 		if (!ah)
14687 			continue;
14688 
14689 		for_each_ar(ah, ar, j) {
14690 			ar = &ah->radio[j];
14691 			ret = __ath12k_mac_mlo_setup(ar);
14692 			if (ret) {
14693 				ath12k_err(ar->ab, "failed to setup MLO: %d\n", ret);
14694 				goto err_setup;
14695 			}
14696 		}
14697 	}
14698 
14699 	return 0;
14700 
14701 err_setup:
14702 	for (i = i - 1; i >= 0; i--) {
14703 		ah = ag->ah[i];
14704 		if (!ah)
14705 			continue;
14706 
14707 		for (j = j - 1; j >= 0; j--) {
14708 			ar = &ah->radio[j];
14709 			if (!ar)
14710 				continue;
14711 
14712 			__ath12k_mac_mlo_teardown(ar);
14713 		}
14714 	}
14715 
14716 	return ret;
14717 }
14718 
14719 void ath12k_mac_mlo_teardown(struct ath12k_hw_group *ag)
14720 {
14721 	struct ath12k_hw *ah;
14722 	struct ath12k *ar;
14723 	int ret, i, j;
14724 
14725 	for (i = 0; i < ag->num_hw; i++) {
14726 		ah = ag->ah[i];
14727 		if (!ah)
14728 			continue;
14729 
14730 		for_each_ar(ah, ar, j) {
14731 			ar = &ah->radio[j];
14732 			ret = __ath12k_mac_mlo_teardown(ar);
14733 			if (ret) {
14734 				ath12k_err(ar->ab, "failed to teardown MLO: %d\n", ret);
14735 				break;
14736 			}
14737 		}
14738 	}
14739 }
14740 
14741 int ath12k_mac_register(struct ath12k_hw_group *ag)
14742 {
14743 	struct ath12k_hw *ah;
14744 	int i;
14745 	int ret;
14746 
14747 	for (i = 0; i < ag->num_hw; i++) {
14748 		ah = ath12k_ag_to_ah(ag, i);
14749 
14750 		ret = ath12k_mac_hw_register(ah);
14751 		if (ret)
14752 			goto err;
14753 	}
14754 
14755 	return 0;
14756 
14757 err:
14758 	for (i = i - 1; i >= 0; i--) {
14759 		ah = ath12k_ag_to_ah(ag, i);
14760 		if (!ah)
14761 			continue;
14762 
14763 		ath12k_mac_hw_unregister(ah);
14764 	}
14765 
14766 	return ret;
14767 }
14768 
14769 void ath12k_mac_unregister(struct ath12k_hw_group *ag)
14770 {
14771 	struct ath12k_hw *ah;
14772 	int i;
14773 
14774 	for (i = ag->num_hw - 1; i >= 0; i--) {
14775 		ah = ath12k_ag_to_ah(ag, i);
14776 		if (!ah)
14777 			continue;
14778 
14779 		ath12k_mac_hw_unregister(ah);
14780 	}
14781 }
14782 
14783 static void ath12k_mac_hw_destroy(struct ath12k_hw *ah)
14784 {
14785 	ieee80211_free_hw(ah->hw);
14786 }
14787 
14788 static struct ath12k_hw *ath12k_mac_hw_allocate(struct ath12k_hw_group *ag,
14789 						struct ath12k_pdev_map *pdev_map,
14790 						u8 num_pdev_map)
14791 {
14792 	struct ieee80211_hw *hw;
14793 	struct ath12k *ar;
14794 	struct ath12k_base *ab;
14795 	struct ath12k_pdev *pdev;
14796 	struct ath12k_hw *ah;
14797 	int i;
14798 	u8 pdev_idx;
14799 
14800 	hw = ieee80211_alloc_hw(struct_size(ah, radio, num_pdev_map),
14801 				&ath12k_ops);
14802 	if (!hw)
14803 		return NULL;
14804 
14805 	ah = ath12k_hw_to_ah(hw);
14806 	ah->hw = hw;
14807 	ah->num_radio = num_pdev_map;
14808 
14809 	mutex_init(&ah->hw_mutex);
14810 	INIT_LIST_HEAD(&ah->ml_peers);
14811 
14812 	for (i = 0; i < num_pdev_map; i++) {
14813 		ab = pdev_map[i].ab;
14814 		pdev_idx = pdev_map[i].pdev_idx;
14815 		pdev = &ab->pdevs[pdev_idx];
14816 
14817 		ar = ath12k_ah_to_ar(ah, i);
14818 		ar->ah = ah;
14819 		ar->ab = ab;
14820 		ar->hw_link_id = pdev->hw_link_id;
14821 		ar->pdev = pdev;
14822 		ar->pdev_idx = pdev_idx;
14823 		pdev->ar = ar;
14824 
14825 		ag->hw_links[ar->hw_link_id].device_id = ab->device_id;
14826 		ag->hw_links[ar->hw_link_id].pdev_idx = pdev_idx;
14827 
14828 		ath12k_mac_setup(ar);
14829 		ath12k_dp_pdev_pre_alloc(ar);
14830 	}
14831 
14832 	return ah;
14833 }
14834 
14835 void ath12k_mac_destroy(struct ath12k_hw_group *ag)
14836 {
14837 	struct ath12k_pdev *pdev;
14838 	struct ath12k_base *ab = ag->ab[0];
14839 	int i, j;
14840 	struct ath12k_hw *ah;
14841 
14842 	for (i = 0; i < ag->num_devices; i++) {
14843 		ab = ag->ab[i];
14844 		if (!ab)
14845 			continue;
14846 
14847 		for (j = 0; j < ab->num_radios; j++) {
14848 			pdev = &ab->pdevs[j];
14849 			if (!pdev->ar)
14850 				continue;
14851 			pdev->ar = NULL;
14852 		}
14853 	}
14854 
14855 	for (i = 0; i < ag->num_hw; i++) {
14856 		ah = ath12k_ag_to_ah(ag, i);
14857 		if (!ah)
14858 			continue;
14859 
14860 		ath12k_mac_hw_destroy(ah);
14861 		ath12k_ag_set_ah(ag, i, NULL);
14862 	}
14863 }
14864 
14865 static void ath12k_mac_set_device_defaults(struct ath12k_base *ab)
14866 {
14867 	int total_vdev;
14868 
14869 	/* Initialize channel counters frequency value in hertz */
14870 	ab->cc_freq_hz = 320000;
14871 	total_vdev = ab->num_radios * TARGET_NUM_VDEVS(ab);
14872 	ab->free_vdev_map = (1LL << total_vdev) - 1;
14873 }
14874 
14875 int ath12k_mac_allocate(struct ath12k_hw_group *ag)
14876 {
14877 	struct ath12k_pdev_map pdev_map[ATH12K_GROUP_MAX_RADIO];
14878 	int mac_id, device_id, total_radio, num_hw;
14879 	struct ath12k_base *ab;
14880 	struct ath12k_hw *ah;
14881 	int ret, i, j;
14882 	u8 radio_per_hw;
14883 
14884 	total_radio = 0;
14885 	for (i = 0; i < ag->num_devices; i++) {
14886 		ab = ag->ab[i];
14887 		if (!ab)
14888 			continue;
14889 
14890 		ath12k_debugfs_pdev_create(ab);
14891 		ath12k_mac_set_device_defaults(ab);
14892 		total_radio += ab->num_radios;
14893 	}
14894 
14895 	if (!total_radio)
14896 		return -EINVAL;
14897 
14898 	if (WARN_ON(total_radio > ATH12K_GROUP_MAX_RADIO))
14899 		return -ENOSPC;
14900 
14901 	/* All pdev get combined and register as single wiphy based on
14902 	 * hardware group which participate in multi-link operation else
14903 	 * each pdev get register separately.
14904 	 */
14905 	if (ag->mlo_capable)
14906 		radio_per_hw = total_radio;
14907 	else
14908 		radio_per_hw = 1;
14909 
14910 	num_hw = total_radio / radio_per_hw;
14911 
14912 	ag->num_hw = 0;
14913 	device_id = 0;
14914 	mac_id = 0;
14915 	for (i = 0; i < num_hw; i++) {
14916 		for (j = 0; j < radio_per_hw; j++) {
14917 			if (device_id >= ag->num_devices || !ag->ab[device_id]) {
14918 				ret = -ENOSPC;
14919 				goto err;
14920 			}
14921 
14922 			ab = ag->ab[device_id];
14923 			pdev_map[j].ab = ab;
14924 			pdev_map[j].pdev_idx = mac_id;
14925 			mac_id++;
14926 
14927 			/* If mac_id falls beyond the current device MACs then
14928 			 * move to next device
14929 			 */
14930 			if (mac_id >= ab->num_radios) {
14931 				mac_id = 0;
14932 				device_id++;
14933 			}
14934 		}
14935 
14936 		ab = pdev_map->ab;
14937 
14938 		ah = ath12k_mac_hw_allocate(ag, pdev_map, radio_per_hw);
14939 		if (!ah) {
14940 			ath12k_warn(ab, "failed to allocate mac80211 hw device for hw_idx %d\n",
14941 				    i);
14942 			ret = -ENOMEM;
14943 			goto err;
14944 		}
14945 
14946 		ah->dev = ab->dev;
14947 
14948 		ag->ah[i] = ah;
14949 		ag->num_hw++;
14950 	}
14951 
14952 	return 0;
14953 
14954 err:
14955 	for (i = i - 1; i >= 0; i--) {
14956 		ah = ath12k_ag_to_ah(ag, i);
14957 		if (!ah)
14958 			continue;
14959 
14960 		ath12k_mac_hw_destroy(ah);
14961 		ath12k_ag_set_ah(ag, i, NULL);
14962 	}
14963 
14964 	return ret;
14965 }
14966 
14967 int ath12k_mac_vif_set_keepalive(struct ath12k_link_vif *arvif,
14968 				 enum wmi_sta_keepalive_method method,
14969 				 u32 interval)
14970 {
14971 	struct wmi_sta_keepalive_arg arg = {};
14972 	struct ath12k *ar = arvif->ar;
14973 	int ret;
14974 
14975 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
14976 
14977 	if (arvif->ahvif->vdev_type != WMI_VDEV_TYPE_STA)
14978 		return 0;
14979 
14980 	if (!test_bit(WMI_TLV_SERVICE_STA_KEEP_ALIVE, ar->ab->wmi_ab.svc_map))
14981 		return 0;
14982 
14983 	arg.vdev_id = arvif->vdev_id;
14984 	arg.enabled = 1;
14985 	arg.method = method;
14986 	arg.interval = interval;
14987 
14988 	ret = ath12k_wmi_sta_keepalive(ar, &arg);
14989 	if (ret) {
14990 		ath12k_warn(ar->ab, "failed to set keepalive on vdev %i: %d\n",
14991 			    arvif->vdev_id, ret);
14992 		return ret;
14993 	}
14994 
14995 	return 0;
14996 }
14997