xref: /linux/drivers/net/wireless/ath/ath12k/mac.c (revision 68f715a820b02f965e2afc584a6cb542843cbc98)
1 // SPDX-License-Identifier: BSD-3-Clause-Clear
2 /*
3  * Copyright (c) 2018-2021 The Linux Foundation. All rights reserved.
4  * Copyright (c) 2021-2023 Qualcomm Innovation Center, Inc. All rights reserved.
5  */
6 
7 #include <net/mac80211.h>
8 #include <linux/etherdevice.h>
9 #include "mac.h"
10 #include "core.h"
11 #include "debug.h"
12 #include "wmi.h"
13 #include "hw.h"
14 #include "dp_tx.h"
15 #include "dp_rx.h"
16 #include "peer.h"
17 
18 #define CHAN2G(_channel, _freq, _flags) { \
19 	.band                   = NL80211_BAND_2GHZ, \
20 	.hw_value               = (_channel), \
21 	.center_freq            = (_freq), \
22 	.flags                  = (_flags), \
23 	.max_antenna_gain       = 0, \
24 	.max_power              = 30, \
25 }
26 
27 #define CHAN5G(_channel, _freq, _flags) { \
28 	.band                   = NL80211_BAND_5GHZ, \
29 	.hw_value               = (_channel), \
30 	.center_freq            = (_freq), \
31 	.flags                  = (_flags), \
32 	.max_antenna_gain       = 0, \
33 	.max_power              = 30, \
34 }
35 
36 #define CHAN6G(_channel, _freq, _flags) { \
37 	.band                   = NL80211_BAND_6GHZ, \
38 	.hw_value               = (_channel), \
39 	.center_freq            = (_freq), \
40 	.flags                  = (_flags), \
41 	.max_antenna_gain       = 0, \
42 	.max_power              = 30, \
43 }
44 
45 static const struct ieee80211_channel ath12k_2ghz_channels[] = {
46 	CHAN2G(1, 2412, 0),
47 	CHAN2G(2, 2417, 0),
48 	CHAN2G(3, 2422, 0),
49 	CHAN2G(4, 2427, 0),
50 	CHAN2G(5, 2432, 0),
51 	CHAN2G(6, 2437, 0),
52 	CHAN2G(7, 2442, 0),
53 	CHAN2G(8, 2447, 0),
54 	CHAN2G(9, 2452, 0),
55 	CHAN2G(10, 2457, 0),
56 	CHAN2G(11, 2462, 0),
57 	CHAN2G(12, 2467, 0),
58 	CHAN2G(13, 2472, 0),
59 	CHAN2G(14, 2484, 0),
60 };
61 
62 static const struct ieee80211_channel ath12k_5ghz_channels[] = {
63 	CHAN5G(36, 5180, 0),
64 	CHAN5G(40, 5200, 0),
65 	CHAN5G(44, 5220, 0),
66 	CHAN5G(48, 5240, 0),
67 	CHAN5G(52, 5260, 0),
68 	CHAN5G(56, 5280, 0),
69 	CHAN5G(60, 5300, 0),
70 	CHAN5G(64, 5320, 0),
71 	CHAN5G(100, 5500, 0),
72 	CHAN5G(104, 5520, 0),
73 	CHAN5G(108, 5540, 0),
74 	CHAN5G(112, 5560, 0),
75 	CHAN5G(116, 5580, 0),
76 	CHAN5G(120, 5600, 0),
77 	CHAN5G(124, 5620, 0),
78 	CHAN5G(128, 5640, 0),
79 	CHAN5G(132, 5660, 0),
80 	CHAN5G(136, 5680, 0),
81 	CHAN5G(140, 5700, 0),
82 	CHAN5G(144, 5720, 0),
83 	CHAN5G(149, 5745, 0),
84 	CHAN5G(153, 5765, 0),
85 	CHAN5G(157, 5785, 0),
86 	CHAN5G(161, 5805, 0),
87 	CHAN5G(165, 5825, 0),
88 	CHAN5G(169, 5845, 0),
89 	CHAN5G(173, 5865, 0),
90 };
91 
92 static const struct ieee80211_channel ath12k_6ghz_channels[] = {
93 	CHAN6G(1, 5955, 0),
94 	CHAN6G(5, 5975, 0),
95 	CHAN6G(9, 5995, 0),
96 	CHAN6G(13, 6015, 0),
97 	CHAN6G(17, 6035, 0),
98 	CHAN6G(21, 6055, 0),
99 	CHAN6G(25, 6075, 0),
100 	CHAN6G(29, 6095, 0),
101 	CHAN6G(33, 6115, 0),
102 	CHAN6G(37, 6135, 0),
103 	CHAN6G(41, 6155, 0),
104 	CHAN6G(45, 6175, 0),
105 	CHAN6G(49, 6195, 0),
106 	CHAN6G(53, 6215, 0),
107 	CHAN6G(57, 6235, 0),
108 	CHAN6G(61, 6255, 0),
109 	CHAN6G(65, 6275, 0),
110 	CHAN6G(69, 6295, 0),
111 	CHAN6G(73, 6315, 0),
112 	CHAN6G(77, 6335, 0),
113 	CHAN6G(81, 6355, 0),
114 	CHAN6G(85, 6375, 0),
115 	CHAN6G(89, 6395, 0),
116 	CHAN6G(93, 6415, 0),
117 	CHAN6G(97, 6435, 0),
118 	CHAN6G(101, 6455, 0),
119 	CHAN6G(105, 6475, 0),
120 	CHAN6G(109, 6495, 0),
121 	CHAN6G(113, 6515, 0),
122 	CHAN6G(117, 6535, 0),
123 	CHAN6G(121, 6555, 0),
124 	CHAN6G(125, 6575, 0),
125 	CHAN6G(129, 6595, 0),
126 	CHAN6G(133, 6615, 0),
127 	CHAN6G(137, 6635, 0),
128 	CHAN6G(141, 6655, 0),
129 	CHAN6G(145, 6675, 0),
130 	CHAN6G(149, 6695, 0),
131 	CHAN6G(153, 6715, 0),
132 	CHAN6G(157, 6735, 0),
133 	CHAN6G(161, 6755, 0),
134 	CHAN6G(165, 6775, 0),
135 	CHAN6G(169, 6795, 0),
136 	CHAN6G(173, 6815, 0),
137 	CHAN6G(177, 6835, 0),
138 	CHAN6G(181, 6855, 0),
139 	CHAN6G(185, 6875, 0),
140 	CHAN6G(189, 6895, 0),
141 	CHAN6G(193, 6915, 0),
142 	CHAN6G(197, 6935, 0),
143 	CHAN6G(201, 6955, 0),
144 	CHAN6G(205, 6975, 0),
145 	CHAN6G(209, 6995, 0),
146 	CHAN6G(213, 7015, 0),
147 	CHAN6G(217, 7035, 0),
148 	CHAN6G(221, 7055, 0),
149 	CHAN6G(225, 7075, 0),
150 	CHAN6G(229, 7095, 0),
151 	CHAN6G(233, 7115, 0),
152 };
153 
154 static struct ieee80211_rate ath12k_legacy_rates[] = {
155 	{ .bitrate = 10,
156 	  .hw_value = ATH12K_HW_RATE_CCK_LP_1M },
157 	{ .bitrate = 20,
158 	  .hw_value = ATH12K_HW_RATE_CCK_LP_2M,
159 	  .hw_value_short = ATH12K_HW_RATE_CCK_SP_2M,
160 	  .flags = IEEE80211_RATE_SHORT_PREAMBLE },
161 	{ .bitrate = 55,
162 	  .hw_value = ATH12K_HW_RATE_CCK_LP_5_5M,
163 	  .hw_value_short = ATH12K_HW_RATE_CCK_SP_5_5M,
164 	  .flags = IEEE80211_RATE_SHORT_PREAMBLE },
165 	{ .bitrate = 110,
166 	  .hw_value = ATH12K_HW_RATE_CCK_LP_11M,
167 	  .hw_value_short = ATH12K_HW_RATE_CCK_SP_11M,
168 	  .flags = IEEE80211_RATE_SHORT_PREAMBLE },
169 
170 	{ .bitrate = 60, .hw_value = ATH12K_HW_RATE_OFDM_6M },
171 	{ .bitrate = 90, .hw_value = ATH12K_HW_RATE_OFDM_9M },
172 	{ .bitrate = 120, .hw_value = ATH12K_HW_RATE_OFDM_12M },
173 	{ .bitrate = 180, .hw_value = ATH12K_HW_RATE_OFDM_18M },
174 	{ .bitrate = 240, .hw_value = ATH12K_HW_RATE_OFDM_24M },
175 	{ .bitrate = 360, .hw_value = ATH12K_HW_RATE_OFDM_36M },
176 	{ .bitrate = 480, .hw_value = ATH12K_HW_RATE_OFDM_48M },
177 	{ .bitrate = 540, .hw_value = ATH12K_HW_RATE_OFDM_54M },
178 };
179 
180 static const int
181 ath12k_phymodes[NUM_NL80211_BANDS][ATH12K_CHAN_WIDTH_NUM] = {
182 	[NL80211_BAND_2GHZ] = {
183 			[NL80211_CHAN_WIDTH_5] = MODE_UNKNOWN,
184 			[NL80211_CHAN_WIDTH_10] = MODE_UNKNOWN,
185 			[NL80211_CHAN_WIDTH_20_NOHT] = MODE_11BE_EHT20_2G,
186 			[NL80211_CHAN_WIDTH_20] = MODE_11BE_EHT20_2G,
187 			[NL80211_CHAN_WIDTH_40] = MODE_11BE_EHT40_2G,
188 			[NL80211_CHAN_WIDTH_80] = MODE_UNKNOWN,
189 			[NL80211_CHAN_WIDTH_80P80] = MODE_UNKNOWN,
190 			[NL80211_CHAN_WIDTH_160] = MODE_UNKNOWN,
191 			[NL80211_CHAN_WIDTH_320] = MODE_UNKNOWN,
192 	},
193 	[NL80211_BAND_5GHZ] = {
194 			[NL80211_CHAN_WIDTH_5] = MODE_UNKNOWN,
195 			[NL80211_CHAN_WIDTH_10] = MODE_UNKNOWN,
196 			[NL80211_CHAN_WIDTH_20_NOHT] = MODE_11BE_EHT20,
197 			[NL80211_CHAN_WIDTH_20] = MODE_11BE_EHT20,
198 			[NL80211_CHAN_WIDTH_40] = MODE_11BE_EHT40,
199 			[NL80211_CHAN_WIDTH_80] = MODE_11BE_EHT80,
200 			[NL80211_CHAN_WIDTH_160] = MODE_11BE_EHT160,
201 			[NL80211_CHAN_WIDTH_80P80] = MODE_11BE_EHT80_80,
202 			[NL80211_CHAN_WIDTH_320] = MODE_11BE_EHT320,
203 	},
204 	[NL80211_BAND_6GHZ] = {
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_11BE_EHT80_80,
213 			[NL80211_CHAN_WIDTH_320] = MODE_11BE_EHT320,
214 	},
215 
216 };
217 
218 const struct htt_rx_ring_tlv_filter ath12k_mac_mon_status_filter_default = {
219 	.rx_filter = HTT_RX_FILTER_TLV_FLAGS_MPDU_START |
220 		     HTT_RX_FILTER_TLV_FLAGS_PPDU_END |
221 		     HTT_RX_FILTER_TLV_FLAGS_PPDU_END_STATUS_DONE,
222 	.pkt_filter_flags0 = HTT_RX_FP_MGMT_FILTER_FLAGS0,
223 	.pkt_filter_flags1 = HTT_RX_FP_MGMT_FILTER_FLAGS1,
224 	.pkt_filter_flags2 = HTT_RX_FP_CTRL_FILTER_FLASG2,
225 	.pkt_filter_flags3 = HTT_RX_FP_DATA_FILTER_FLASG3 |
226 			     HTT_RX_FP_CTRL_FILTER_FLASG3
227 };
228 
229 #define ATH12K_MAC_FIRST_OFDM_RATE_IDX 4
230 #define ath12k_g_rates ath12k_legacy_rates
231 #define ath12k_g_rates_size (ARRAY_SIZE(ath12k_legacy_rates))
232 #define ath12k_a_rates (ath12k_legacy_rates + 4)
233 #define ath12k_a_rates_size (ARRAY_SIZE(ath12k_legacy_rates) - 4)
234 
235 #define ATH12K_MAC_SCAN_TIMEOUT_MSECS 200 /* in msecs */
236 
237 static const u32 ath12k_smps_map[] = {
238 	[WLAN_HT_CAP_SM_PS_STATIC] = WMI_PEER_SMPS_STATIC,
239 	[WLAN_HT_CAP_SM_PS_DYNAMIC] = WMI_PEER_SMPS_DYNAMIC,
240 	[WLAN_HT_CAP_SM_PS_INVALID] = WMI_PEER_SMPS_PS_NONE,
241 	[WLAN_HT_CAP_SM_PS_DISABLED] = WMI_PEER_SMPS_PS_NONE,
242 };
243 
244 static int ath12k_start_vdev_delay(struct ieee80211_hw *hw,
245 				   struct ieee80211_vif *vif);
246 
247 static const char *ath12k_mac_phymode_str(enum wmi_phy_mode mode)
248 {
249 	switch (mode) {
250 	case MODE_11A:
251 		return "11a";
252 	case MODE_11G:
253 		return "11g";
254 	case MODE_11B:
255 		return "11b";
256 	case MODE_11GONLY:
257 		return "11gonly";
258 	case MODE_11NA_HT20:
259 		return "11na-ht20";
260 	case MODE_11NG_HT20:
261 		return "11ng-ht20";
262 	case MODE_11NA_HT40:
263 		return "11na-ht40";
264 	case MODE_11NG_HT40:
265 		return "11ng-ht40";
266 	case MODE_11AC_VHT20:
267 		return "11ac-vht20";
268 	case MODE_11AC_VHT40:
269 		return "11ac-vht40";
270 	case MODE_11AC_VHT80:
271 		return "11ac-vht80";
272 	case MODE_11AC_VHT160:
273 		return "11ac-vht160";
274 	case MODE_11AC_VHT80_80:
275 		return "11ac-vht80+80";
276 	case MODE_11AC_VHT20_2G:
277 		return "11ac-vht20-2g";
278 	case MODE_11AC_VHT40_2G:
279 		return "11ac-vht40-2g";
280 	case MODE_11AC_VHT80_2G:
281 		return "11ac-vht80-2g";
282 	case MODE_11AX_HE20:
283 		return "11ax-he20";
284 	case MODE_11AX_HE40:
285 		return "11ax-he40";
286 	case MODE_11AX_HE80:
287 		return "11ax-he80";
288 	case MODE_11AX_HE80_80:
289 		return "11ax-he80+80";
290 	case MODE_11AX_HE160:
291 		return "11ax-he160";
292 	case MODE_11AX_HE20_2G:
293 		return "11ax-he20-2g";
294 	case MODE_11AX_HE40_2G:
295 		return "11ax-he40-2g";
296 	case MODE_11AX_HE80_2G:
297 		return "11ax-he80-2g";
298 	case MODE_11BE_EHT20:
299 		return "11be-eht20";
300 	case MODE_11BE_EHT40:
301 		return "11be-eht40";
302 	case MODE_11BE_EHT80:
303 		return "11be-eht80";
304 	case MODE_11BE_EHT80_80:
305 		return "11be-eht80+80";
306 	case MODE_11BE_EHT160:
307 		return "11be-eht160";
308 	case MODE_11BE_EHT160_160:
309 		return "11be-eht160+160";
310 	case MODE_11BE_EHT320:
311 		return "11be-eht320";
312 	case MODE_11BE_EHT20_2G:
313 		return "11be-eht20-2g";
314 	case MODE_11BE_EHT40_2G:
315 		return "11be-eht40-2g";
316 	case MODE_UNKNOWN:
317 		/* skip */
318 		break;
319 
320 		/* no default handler to allow compiler to check that the
321 		 * enum is fully handled
322 		 */
323 	}
324 
325 	return "<unknown>";
326 }
327 
328 enum rate_info_bw
329 ath12k_mac_bw_to_mac80211_bw(enum ath12k_supported_bw bw)
330 {
331 	u8 ret = RATE_INFO_BW_20;
332 
333 	switch (bw) {
334 	case ATH12K_BW_20:
335 		ret = RATE_INFO_BW_20;
336 		break;
337 	case ATH12K_BW_40:
338 		ret = RATE_INFO_BW_40;
339 		break;
340 	case ATH12K_BW_80:
341 		ret = RATE_INFO_BW_80;
342 		break;
343 	case ATH12K_BW_160:
344 		ret = RATE_INFO_BW_160;
345 		break;
346 	case ATH12K_BW_320:
347 		ret = RATE_INFO_BW_320;
348 		break;
349 	}
350 
351 	return ret;
352 }
353 
354 enum ath12k_supported_bw ath12k_mac_mac80211_bw_to_ath12k_bw(enum rate_info_bw bw)
355 {
356 	switch (bw) {
357 	case RATE_INFO_BW_20:
358 		return ATH12K_BW_20;
359 	case RATE_INFO_BW_40:
360 		return ATH12K_BW_40;
361 	case RATE_INFO_BW_80:
362 		return ATH12K_BW_80;
363 	case RATE_INFO_BW_160:
364 		return ATH12K_BW_160;
365 	case RATE_INFO_BW_320:
366 		return ATH12K_BW_320;
367 	default:
368 		return ATH12K_BW_20;
369 	}
370 }
371 
372 int ath12k_mac_hw_ratecode_to_legacy_rate(u8 hw_rc, u8 preamble, u8 *rateidx,
373 					  u16 *rate)
374 {
375 	/* As default, it is OFDM rates */
376 	int i = ATH12K_MAC_FIRST_OFDM_RATE_IDX;
377 	int max_rates_idx = ath12k_g_rates_size;
378 
379 	if (preamble == WMI_RATE_PREAMBLE_CCK) {
380 		hw_rc &= ~ATH12K_HW_RATECODE_CCK_SHORT_PREAM_MASK;
381 		i = 0;
382 		max_rates_idx = ATH12K_MAC_FIRST_OFDM_RATE_IDX;
383 	}
384 
385 	while (i < max_rates_idx) {
386 		if (hw_rc == ath12k_legacy_rates[i].hw_value) {
387 			*rateidx = i;
388 			*rate = ath12k_legacy_rates[i].bitrate;
389 			return 0;
390 		}
391 		i++;
392 	}
393 
394 	return -EINVAL;
395 }
396 
397 u8 ath12k_mac_bitrate_to_idx(const struct ieee80211_supported_band *sband,
398 			     u32 bitrate)
399 {
400 	int i;
401 
402 	for (i = 0; i < sband->n_bitrates; i++)
403 		if (sband->bitrates[i].bitrate == bitrate)
404 			return i;
405 
406 	return 0;
407 }
408 
409 static u32
410 ath12k_mac_max_ht_nss(const u8 *ht_mcs_mask)
411 {
412 	int nss;
413 
414 	for (nss = IEEE80211_HT_MCS_MASK_LEN - 1; nss >= 0; nss--)
415 		if (ht_mcs_mask[nss])
416 			return nss + 1;
417 
418 	return 1;
419 }
420 
421 static u32
422 ath12k_mac_max_vht_nss(const u16 *vht_mcs_mask)
423 {
424 	int nss;
425 
426 	for (nss = NL80211_VHT_NSS_MAX - 1; nss >= 0; nss--)
427 		if (vht_mcs_mask[nss])
428 			return nss + 1;
429 
430 	return 1;
431 }
432 
433 static u8 ath12k_parse_mpdudensity(u8 mpdudensity)
434 {
435 /*  From IEEE Std 802.11-2020 defined values for "Minimum MPDU Start Spacing":
436  *   0 for no restriction
437  *   1 for 1/4 us
438  *   2 for 1/2 us
439  *   3 for 1 us
440  *   4 for 2 us
441  *   5 for 4 us
442  *   6 for 8 us
443  *   7 for 16 us
444  */
445 	switch (mpdudensity) {
446 	case 0:
447 		return 0;
448 	case 1:
449 	case 2:
450 	case 3:
451 	/* Our lower layer calculations limit our precision to
452 	 * 1 microsecond
453 	 */
454 		return 1;
455 	case 4:
456 		return 2;
457 	case 5:
458 		return 4;
459 	case 6:
460 		return 8;
461 	case 7:
462 		return 16;
463 	default:
464 		return 0;
465 	}
466 }
467 
468 static int ath12k_mac_vif_chan(struct ieee80211_vif *vif,
469 			       struct cfg80211_chan_def *def)
470 {
471 	struct ieee80211_chanctx_conf *conf;
472 
473 	rcu_read_lock();
474 	conf = rcu_dereference(vif->bss_conf.chanctx_conf);
475 	if (!conf) {
476 		rcu_read_unlock();
477 		return -ENOENT;
478 	}
479 
480 	*def = conf->def;
481 	rcu_read_unlock();
482 
483 	return 0;
484 }
485 
486 static bool ath12k_mac_bitrate_is_cck(int bitrate)
487 {
488 	switch (bitrate) {
489 	case 10:
490 	case 20:
491 	case 55:
492 	case 110:
493 		return true;
494 	}
495 
496 	return false;
497 }
498 
499 u8 ath12k_mac_hw_rate_to_idx(const struct ieee80211_supported_band *sband,
500 			     u8 hw_rate, bool cck)
501 {
502 	const struct ieee80211_rate *rate;
503 	int i;
504 
505 	for (i = 0; i < sband->n_bitrates; i++) {
506 		rate = &sband->bitrates[i];
507 
508 		if (ath12k_mac_bitrate_is_cck(rate->bitrate) != cck)
509 			continue;
510 
511 		if (rate->hw_value == hw_rate)
512 			return i;
513 		else if (rate->flags & IEEE80211_RATE_SHORT_PREAMBLE &&
514 			 rate->hw_value_short == hw_rate)
515 			return i;
516 	}
517 
518 	return 0;
519 }
520 
521 static u8 ath12k_mac_bitrate_to_rate(int bitrate)
522 {
523 	return DIV_ROUND_UP(bitrate, 5) |
524 	       (ath12k_mac_bitrate_is_cck(bitrate) ? BIT(7) : 0);
525 }
526 
527 static void ath12k_get_arvif_iter(void *data, u8 *mac,
528 				  struct ieee80211_vif *vif)
529 {
530 	struct ath12k_vif_iter *arvif_iter = data;
531 	struct ath12k_vif *arvif = ath12k_vif_to_arvif(vif);
532 
533 	if (arvif->vdev_id == arvif_iter->vdev_id)
534 		arvif_iter->arvif = arvif;
535 }
536 
537 struct ath12k_vif *ath12k_mac_get_arvif(struct ath12k *ar, u32 vdev_id)
538 {
539 	struct ath12k_vif_iter arvif_iter = {};
540 	u32 flags;
541 
542 	arvif_iter.vdev_id = vdev_id;
543 
544 	flags = IEEE80211_IFACE_ITER_RESUME_ALL;
545 	ieee80211_iterate_active_interfaces_atomic(ar->hw,
546 						   flags,
547 						   ath12k_get_arvif_iter,
548 						   &arvif_iter);
549 	if (!arvif_iter.arvif) {
550 		ath12k_warn(ar->ab, "No VIF found for vdev %d\n", vdev_id);
551 		return NULL;
552 	}
553 
554 	return arvif_iter.arvif;
555 }
556 
557 struct ath12k_vif *ath12k_mac_get_arvif_by_vdev_id(struct ath12k_base *ab,
558 						   u32 vdev_id)
559 {
560 	int i;
561 	struct ath12k_pdev *pdev;
562 	struct ath12k_vif *arvif;
563 
564 	for (i = 0; i < ab->num_radios; i++) {
565 		pdev = rcu_dereference(ab->pdevs_active[i]);
566 		if (pdev && pdev->ar) {
567 			arvif = ath12k_mac_get_arvif(pdev->ar, vdev_id);
568 			if (arvif)
569 				return arvif;
570 		}
571 	}
572 
573 	return NULL;
574 }
575 
576 struct ath12k *ath12k_mac_get_ar_by_vdev_id(struct ath12k_base *ab, u32 vdev_id)
577 {
578 	int i;
579 	struct ath12k_pdev *pdev;
580 
581 	for (i = 0; i < ab->num_radios; i++) {
582 		pdev = rcu_dereference(ab->pdevs_active[i]);
583 		if (pdev && pdev->ar) {
584 			if (pdev->ar->allocated_vdev_map & (1LL << vdev_id))
585 				return pdev->ar;
586 		}
587 	}
588 
589 	return NULL;
590 }
591 
592 struct ath12k *ath12k_mac_get_ar_by_pdev_id(struct ath12k_base *ab, u32 pdev_id)
593 {
594 	int i;
595 	struct ath12k_pdev *pdev;
596 
597 	if (ab->hw_params->single_pdev_only) {
598 		pdev = rcu_dereference(ab->pdevs_active[0]);
599 		return pdev ? pdev->ar : NULL;
600 	}
601 
602 	if (WARN_ON(pdev_id > ab->num_radios))
603 		return NULL;
604 
605 	for (i = 0; i < ab->num_radios; i++) {
606 		pdev = rcu_dereference(ab->pdevs_active[i]);
607 
608 		if (pdev && pdev->pdev_id == pdev_id)
609 			return (pdev->ar ? pdev->ar : NULL);
610 	}
611 
612 	return NULL;
613 }
614 
615 static void ath12k_pdev_caps_update(struct ath12k *ar)
616 {
617 	struct ath12k_base *ab = ar->ab;
618 
619 	ar->max_tx_power = ab->target_caps.hw_max_tx_power;
620 
621 	/* FIXME: Set min_tx_power to ab->target_caps.hw_min_tx_power.
622 	 * But since the received value in svcrdy is same as hw_max_tx_power,
623 	 * we can set ar->min_tx_power to 0 currently until
624 	 * this is fixed in firmware
625 	 */
626 	ar->min_tx_power = 0;
627 
628 	ar->txpower_limit_2g = ar->max_tx_power;
629 	ar->txpower_limit_5g = ar->max_tx_power;
630 	ar->txpower_scale = WMI_HOST_TP_SCALE_MAX;
631 }
632 
633 static int ath12k_mac_txpower_recalc(struct ath12k *ar)
634 {
635 	struct ath12k_pdev *pdev = ar->pdev;
636 	struct ath12k_vif *arvif;
637 	int ret, txpower = -1;
638 	u32 param;
639 
640 	lockdep_assert_held(&ar->conf_mutex);
641 
642 	list_for_each_entry(arvif, &ar->arvifs, list) {
643 		if (arvif->txpower <= 0)
644 			continue;
645 
646 		if (txpower == -1)
647 			txpower = arvif->txpower;
648 		else
649 			txpower = min(txpower, arvif->txpower);
650 	}
651 
652 	if (txpower == -1)
653 		return 0;
654 
655 	/* txpwr is set as 2 units per dBm in FW*/
656 	txpower = min_t(u32, max_t(u32, ar->min_tx_power, txpower),
657 			ar->max_tx_power) * 2;
658 
659 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "txpower to set in hw %d\n",
660 		   txpower / 2);
661 
662 	if ((pdev->cap.supported_bands & WMI_HOST_WLAN_2G_CAP) &&
663 	    ar->txpower_limit_2g != txpower) {
664 		param = WMI_PDEV_PARAM_TXPOWER_LIMIT2G;
665 		ret = ath12k_wmi_pdev_set_param(ar, param,
666 						txpower, ar->pdev->pdev_id);
667 		if (ret)
668 			goto fail;
669 		ar->txpower_limit_2g = txpower;
670 	}
671 
672 	if ((pdev->cap.supported_bands & WMI_HOST_WLAN_5G_CAP) &&
673 	    ar->txpower_limit_5g != txpower) {
674 		param = WMI_PDEV_PARAM_TXPOWER_LIMIT5G;
675 		ret = ath12k_wmi_pdev_set_param(ar, param,
676 						txpower, ar->pdev->pdev_id);
677 		if (ret)
678 			goto fail;
679 		ar->txpower_limit_5g = txpower;
680 	}
681 
682 	return 0;
683 
684 fail:
685 	ath12k_warn(ar->ab, "failed to recalc txpower limit %d using pdev param %d: %d\n",
686 		    txpower / 2, param, ret);
687 	return ret;
688 }
689 
690 static int ath12k_recalc_rtscts_prot(struct ath12k_vif *arvif)
691 {
692 	struct ath12k *ar = arvif->ar;
693 	u32 vdev_param, rts_cts;
694 	int ret;
695 
696 	lockdep_assert_held(&ar->conf_mutex);
697 
698 	vdev_param = WMI_VDEV_PARAM_ENABLE_RTSCTS;
699 
700 	/* Enable RTS/CTS protection for sw retries (when legacy stations
701 	 * are in BSS) or by default only for second rate series.
702 	 * TODO: Check if we need to enable CTS 2 Self in any case
703 	 */
704 	rts_cts = WMI_USE_RTS_CTS;
705 
706 	if (arvif->num_legacy_stations > 0)
707 		rts_cts |= WMI_RTSCTS_ACROSS_SW_RETRIES << 4;
708 	else
709 		rts_cts |= WMI_RTSCTS_FOR_SECOND_RATESERIES << 4;
710 
711 	/* Need not send duplicate param value to firmware */
712 	if (arvif->rtscts_prot_mode == rts_cts)
713 		return 0;
714 
715 	arvif->rtscts_prot_mode = rts_cts;
716 
717 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac vdev %d recalc rts/cts prot %d\n",
718 		   arvif->vdev_id, rts_cts);
719 
720 	ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
721 					    vdev_param, rts_cts);
722 	if (ret)
723 		ath12k_warn(ar->ab, "failed to recalculate rts/cts prot for vdev %d: %d\n",
724 			    arvif->vdev_id, ret);
725 
726 	return ret;
727 }
728 
729 static int ath12k_mac_set_kickout(struct ath12k_vif *arvif)
730 {
731 	struct ath12k *ar = arvif->ar;
732 	u32 param;
733 	int ret;
734 
735 	ret = ath12k_wmi_pdev_set_param(ar, WMI_PDEV_PARAM_STA_KICKOUT_TH,
736 					ATH12K_KICKOUT_THRESHOLD,
737 					ar->pdev->pdev_id);
738 	if (ret) {
739 		ath12k_warn(ar->ab, "failed to set kickout threshold on vdev %i: %d\n",
740 			    arvif->vdev_id, ret);
741 		return ret;
742 	}
743 
744 	param = WMI_VDEV_PARAM_AP_KEEPALIVE_MIN_IDLE_INACTIVE_TIME_SECS;
745 	ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id, param,
746 					    ATH12K_KEEPALIVE_MIN_IDLE);
747 	if (ret) {
748 		ath12k_warn(ar->ab, "failed to set keepalive minimum idle time on vdev %i: %d\n",
749 			    arvif->vdev_id, ret);
750 		return ret;
751 	}
752 
753 	param = WMI_VDEV_PARAM_AP_KEEPALIVE_MAX_IDLE_INACTIVE_TIME_SECS;
754 	ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id, param,
755 					    ATH12K_KEEPALIVE_MAX_IDLE);
756 	if (ret) {
757 		ath12k_warn(ar->ab, "failed to set keepalive maximum idle time on vdev %i: %d\n",
758 			    arvif->vdev_id, ret);
759 		return ret;
760 	}
761 
762 	param = WMI_VDEV_PARAM_AP_KEEPALIVE_MAX_UNRESPONSIVE_TIME_SECS;
763 	ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id, param,
764 					    ATH12K_KEEPALIVE_MAX_UNRESPONSIVE);
765 	if (ret) {
766 		ath12k_warn(ar->ab, "failed to set keepalive maximum unresponsive time on vdev %i: %d\n",
767 			    arvif->vdev_id, ret);
768 		return ret;
769 	}
770 
771 	return 0;
772 }
773 
774 void ath12k_mac_peer_cleanup_all(struct ath12k *ar)
775 {
776 	struct ath12k_peer *peer, *tmp;
777 	struct ath12k_base *ab = ar->ab;
778 
779 	lockdep_assert_held(&ar->conf_mutex);
780 
781 	spin_lock_bh(&ab->base_lock);
782 	list_for_each_entry_safe(peer, tmp, &ab->peers, list) {
783 		ath12k_dp_rx_peer_tid_cleanup(ar, peer);
784 		list_del(&peer->list);
785 		kfree(peer);
786 	}
787 	spin_unlock_bh(&ab->base_lock);
788 
789 	ar->num_peers = 0;
790 	ar->num_stations = 0;
791 }
792 
793 static int ath12k_mac_vdev_setup_sync(struct ath12k *ar)
794 {
795 	lockdep_assert_held(&ar->conf_mutex);
796 
797 	if (test_bit(ATH12K_FLAG_CRASH_FLUSH, &ar->ab->dev_flags))
798 		return -ESHUTDOWN;
799 
800 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "vdev setup timeout %d\n",
801 		   ATH12K_VDEV_SETUP_TIMEOUT_HZ);
802 
803 	if (!wait_for_completion_timeout(&ar->vdev_setup_done,
804 					 ATH12K_VDEV_SETUP_TIMEOUT_HZ))
805 		return -ETIMEDOUT;
806 
807 	return ar->last_wmi_vdev_start_status ? -EINVAL : 0;
808 }
809 
810 static int ath12k_monitor_vdev_up(struct ath12k *ar, int vdev_id)
811 {
812 	int ret;
813 
814 	ret = ath12k_wmi_vdev_up(ar, vdev_id, 0, ar->mac_addr);
815 	if (ret) {
816 		ath12k_warn(ar->ab, "failed to put up monitor vdev %i: %d\n",
817 			    vdev_id, ret);
818 		return ret;
819 	}
820 
821 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac monitor vdev %i started\n",
822 		   vdev_id);
823 	return 0;
824 }
825 
826 static int ath12k_mac_monitor_vdev_start(struct ath12k *ar, int vdev_id,
827 					 struct cfg80211_chan_def *chandef)
828 {
829 	struct ieee80211_channel *channel;
830 	struct wmi_vdev_start_req_arg arg = {};
831 	int ret;
832 
833 	lockdep_assert_held(&ar->conf_mutex);
834 
835 	channel = chandef->chan;
836 	arg.vdev_id = vdev_id;
837 	arg.freq = channel->center_freq;
838 	arg.band_center_freq1 = chandef->center_freq1;
839 	arg.band_center_freq2 = chandef->center_freq2;
840 	arg.mode = ath12k_phymodes[chandef->chan->band][chandef->width];
841 	arg.chan_radar = !!(channel->flags & IEEE80211_CHAN_RADAR);
842 
843 	arg.min_power = 0;
844 	arg.max_power = channel->max_power;
845 	arg.max_reg_power = channel->max_reg_power;
846 	arg.max_antenna_gain = channel->max_antenna_gain;
847 
848 	arg.pref_tx_streams = ar->num_tx_chains;
849 	arg.pref_rx_streams = ar->num_rx_chains;
850 	arg.punct_bitmap = 0xFFFFFFFF;
851 
852 	arg.passive |= !!(chandef->chan->flags & IEEE80211_CHAN_NO_IR);
853 
854 	reinit_completion(&ar->vdev_setup_done);
855 	reinit_completion(&ar->vdev_delete_done);
856 
857 	ret = ath12k_wmi_vdev_start(ar, &arg, false);
858 	if (ret) {
859 		ath12k_warn(ar->ab, "failed to request monitor vdev %i start: %d\n",
860 			    vdev_id, ret);
861 		return ret;
862 	}
863 
864 	ret = ath12k_mac_vdev_setup_sync(ar);
865 	if (ret) {
866 		ath12k_warn(ar->ab, "failed to synchronize setup for monitor vdev %i start: %d\n",
867 			    vdev_id, ret);
868 		return ret;
869 	}
870 
871 	ret = ath12k_wmi_vdev_up(ar, vdev_id, 0, ar->mac_addr);
872 	if (ret) {
873 		ath12k_warn(ar->ab, "failed to put up monitor vdev %i: %d\n",
874 			    vdev_id, ret);
875 		goto vdev_stop;
876 	}
877 
878 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac monitor vdev %i started\n",
879 		   vdev_id);
880 	return 0;
881 
882 vdev_stop:
883 	ret = ath12k_wmi_vdev_stop(ar, vdev_id);
884 	if (ret)
885 		ath12k_warn(ar->ab, "failed to stop monitor vdev %i after start failure: %d\n",
886 			    vdev_id, ret);
887 	return ret;
888 }
889 
890 static int ath12k_mac_monitor_vdev_stop(struct ath12k *ar)
891 {
892 	int ret;
893 
894 	lockdep_assert_held(&ar->conf_mutex);
895 
896 	reinit_completion(&ar->vdev_setup_done);
897 
898 	ret = ath12k_wmi_vdev_stop(ar, ar->monitor_vdev_id);
899 	if (ret)
900 		ath12k_warn(ar->ab, "failed to request monitor vdev %i stop: %d\n",
901 			    ar->monitor_vdev_id, ret);
902 
903 	ret = ath12k_mac_vdev_setup_sync(ar);
904 	if (ret)
905 		ath12k_warn(ar->ab, "failed to synchronize monitor vdev %i stop: %d\n",
906 			    ar->monitor_vdev_id, ret);
907 
908 	ret = ath12k_wmi_vdev_down(ar, ar->monitor_vdev_id);
909 	if (ret)
910 		ath12k_warn(ar->ab, "failed to put down monitor vdev %i: %d\n",
911 			    ar->monitor_vdev_id, ret);
912 
913 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac monitor vdev %i stopped\n",
914 		   ar->monitor_vdev_id);
915 	return ret;
916 }
917 
918 static int ath12k_mac_monitor_vdev_create(struct ath12k *ar)
919 {
920 	struct ath12k_pdev *pdev = ar->pdev;
921 	struct ath12k_wmi_vdev_create_arg arg = {};
922 	int bit, ret;
923 	u8 tmp_addr[6];
924 	u16 nss;
925 
926 	lockdep_assert_held(&ar->conf_mutex);
927 
928 	if (ar->monitor_vdev_created)
929 		return 0;
930 
931 	if (ar->ab->free_vdev_map == 0) {
932 		ath12k_warn(ar->ab, "failed to find free vdev id for monitor vdev\n");
933 		return -ENOMEM;
934 	}
935 
936 	bit = __ffs64(ar->ab->free_vdev_map);
937 
938 	ar->monitor_vdev_id = bit;
939 
940 	arg.if_id = ar->monitor_vdev_id;
941 	arg.type = WMI_VDEV_TYPE_MONITOR;
942 	arg.subtype = WMI_VDEV_SUBTYPE_NONE;
943 	arg.pdev_id = pdev->pdev_id;
944 	arg.if_stats_id = ATH12K_INVAL_VDEV_STATS_ID;
945 
946 	if (pdev->cap.supported_bands & WMI_HOST_WLAN_2G_CAP) {
947 		arg.chains[NL80211_BAND_2GHZ].tx = ar->num_tx_chains;
948 		arg.chains[NL80211_BAND_2GHZ].rx = ar->num_rx_chains;
949 	}
950 
951 	if (pdev->cap.supported_bands & WMI_HOST_WLAN_5G_CAP) {
952 		arg.chains[NL80211_BAND_5GHZ].tx = ar->num_tx_chains;
953 		arg.chains[NL80211_BAND_5GHZ].rx = ar->num_rx_chains;
954 	}
955 
956 	ret = ath12k_wmi_vdev_create(ar, tmp_addr, &arg);
957 	if (ret) {
958 		ath12k_warn(ar->ab, "failed to request monitor vdev %i creation: %d\n",
959 			    ar->monitor_vdev_id, ret);
960 		ar->monitor_vdev_id = -1;
961 		return ret;
962 	}
963 
964 	nss = hweight32(ar->cfg_tx_chainmask) ? : 1;
965 	ret = ath12k_wmi_vdev_set_param_cmd(ar, ar->monitor_vdev_id,
966 					    WMI_VDEV_PARAM_NSS, nss);
967 	if (ret) {
968 		ath12k_warn(ar->ab, "failed to set vdev %d chainmask 0x%x, nss %d :%d\n",
969 			    ar->monitor_vdev_id, ar->cfg_tx_chainmask, nss, ret);
970 		return ret;
971 	}
972 
973 	ret = ath12k_mac_txpower_recalc(ar);
974 	if (ret)
975 		return ret;
976 
977 	ar->allocated_vdev_map |= 1LL << ar->monitor_vdev_id;
978 	ar->ab->free_vdev_map &= ~(1LL << ar->monitor_vdev_id);
979 	ar->num_created_vdevs++;
980 	ar->monitor_vdev_created = true;
981 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac monitor vdev %d created\n",
982 		   ar->monitor_vdev_id);
983 
984 	return 0;
985 }
986 
987 static int ath12k_mac_monitor_vdev_delete(struct ath12k *ar)
988 {
989 	int ret;
990 	unsigned long time_left;
991 
992 	lockdep_assert_held(&ar->conf_mutex);
993 
994 	if (!ar->monitor_vdev_created)
995 		return 0;
996 
997 	reinit_completion(&ar->vdev_delete_done);
998 
999 	ret = ath12k_wmi_vdev_delete(ar, ar->monitor_vdev_id);
1000 	if (ret) {
1001 		ath12k_warn(ar->ab, "failed to request wmi monitor vdev %i removal: %d\n",
1002 			    ar->monitor_vdev_id, ret);
1003 		return ret;
1004 	}
1005 
1006 	time_left = wait_for_completion_timeout(&ar->vdev_delete_done,
1007 						ATH12K_VDEV_DELETE_TIMEOUT_HZ);
1008 	if (time_left == 0) {
1009 		ath12k_warn(ar->ab, "Timeout in receiving vdev delete response\n");
1010 	} else {
1011 		ar->allocated_vdev_map &= ~(1LL << ar->monitor_vdev_id);
1012 		ar->ab->free_vdev_map |= 1LL << (ar->monitor_vdev_id);
1013 		ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac monitor vdev %d deleted\n",
1014 			   ar->monitor_vdev_id);
1015 		ar->num_created_vdevs--;
1016 		ar->monitor_vdev_id = -1;
1017 		ar->monitor_vdev_created = false;
1018 	}
1019 
1020 	return ret;
1021 }
1022 
1023 static void
1024 ath12k_mac_get_any_chandef_iter(struct ieee80211_hw *hw,
1025 				struct ieee80211_chanctx_conf *conf,
1026 				void *data)
1027 {
1028 	struct cfg80211_chan_def **def = data;
1029 
1030 	*def = &conf->def;
1031 }
1032 
1033 static int ath12k_mac_monitor_start(struct ath12k *ar)
1034 {
1035 	struct cfg80211_chan_def *chandef = NULL;
1036 	int ret;
1037 
1038 	lockdep_assert_held(&ar->conf_mutex);
1039 
1040 	if (ar->monitor_started)
1041 		return 0;
1042 
1043 	ieee80211_iter_chan_contexts_atomic(ar->hw,
1044 					    ath12k_mac_get_any_chandef_iter,
1045 					    &chandef);
1046 	if (!chandef)
1047 		return 0;
1048 
1049 	ret = ath12k_mac_monitor_vdev_start(ar, ar->monitor_vdev_id, chandef);
1050 	if (ret) {
1051 		ath12k_warn(ar->ab, "failed to start monitor vdev: %d\n", ret);
1052 		ath12k_mac_monitor_vdev_delete(ar);
1053 		return ret;
1054 	}
1055 
1056 	ar->monitor_started = true;
1057 	ar->num_started_vdevs++;
1058 	ret = ath12k_dp_tx_htt_monitor_mode_ring_config(ar, false);
1059 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac monitor started ret %d\n", ret);
1060 
1061 	return ret;
1062 }
1063 
1064 static int ath12k_mac_monitor_stop(struct ath12k *ar)
1065 {
1066 	int ret;
1067 
1068 	lockdep_assert_held(&ar->conf_mutex);
1069 
1070 	if (!ar->monitor_started)
1071 		return 0;
1072 
1073 	ret = ath12k_mac_monitor_vdev_stop(ar);
1074 	if (ret) {
1075 		ath12k_warn(ar->ab, "failed to stop monitor vdev: %d\n", ret);
1076 		return ret;
1077 	}
1078 
1079 	ar->monitor_started = false;
1080 	ar->num_started_vdevs--;
1081 	ret = ath12k_dp_tx_htt_monitor_mode_ring_config(ar, true);
1082 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac monitor stopped ret %d\n", ret);
1083 	return ret;
1084 }
1085 
1086 static int ath12k_mac_op_config(struct ieee80211_hw *hw, u32 changed)
1087 {
1088 	struct ath12k *ar = hw->priv;
1089 	struct ieee80211_conf *conf = &hw->conf;
1090 	int ret = 0;
1091 
1092 	mutex_lock(&ar->conf_mutex);
1093 
1094 	if (changed & IEEE80211_CONF_CHANGE_MONITOR) {
1095 		ar->monitor_conf_enabled = conf->flags & IEEE80211_CONF_MONITOR;
1096 		if (ar->monitor_conf_enabled) {
1097 			if (ar->monitor_vdev_created)
1098 				goto exit;
1099 			ret = ath12k_mac_monitor_vdev_create(ar);
1100 			if (ret)
1101 				goto exit;
1102 			ret = ath12k_mac_monitor_start(ar);
1103 			if (ret)
1104 				goto err_mon_del;
1105 		} else {
1106 			if (!ar->monitor_vdev_created)
1107 				goto exit;
1108 			ret = ath12k_mac_monitor_stop(ar);
1109 			if (ret)
1110 				goto exit;
1111 			ath12k_mac_monitor_vdev_delete(ar);
1112 		}
1113 	}
1114 
1115 exit:
1116 	mutex_unlock(&ar->conf_mutex);
1117 	return ret;
1118 
1119 err_mon_del:
1120 	ath12k_mac_monitor_vdev_delete(ar);
1121 	mutex_unlock(&ar->conf_mutex);
1122 	return ret;
1123 }
1124 
1125 static int ath12k_mac_setup_bcn_tmpl(struct ath12k_vif *arvif)
1126 {
1127 	struct ath12k *ar = arvif->ar;
1128 	struct ath12k_base *ab = ar->ab;
1129 	struct ieee80211_hw *hw = ar->hw;
1130 	struct ieee80211_vif *vif = arvif->vif;
1131 	struct ieee80211_mutable_offsets offs = {};
1132 	struct sk_buff *bcn;
1133 	struct ieee80211_mgmt *mgmt;
1134 	u8 *ies;
1135 	int ret;
1136 
1137 	if (arvif->vdev_type != WMI_VDEV_TYPE_AP)
1138 		return 0;
1139 
1140 	bcn = ieee80211_beacon_get_template(hw, vif, &offs, 0);
1141 	if (!bcn) {
1142 		ath12k_warn(ab, "failed to get beacon template from mac80211\n");
1143 		return -EPERM;
1144 	}
1145 
1146 	ies = bcn->data + ieee80211_get_hdrlen_from_skb(bcn);
1147 	ies += sizeof(mgmt->u.beacon);
1148 
1149 	if (cfg80211_find_ie(WLAN_EID_RSN, ies, (skb_tail_pointer(bcn) - ies)))
1150 		arvif->rsnie_present = true;
1151 
1152 	if (cfg80211_find_vendor_ie(WLAN_OUI_MICROSOFT,
1153 				    WLAN_OUI_TYPE_MICROSOFT_WPA,
1154 				    ies, (skb_tail_pointer(bcn) - ies)))
1155 		arvif->wpaie_present = true;
1156 
1157 	ret = ath12k_wmi_bcn_tmpl(ar, arvif->vdev_id, &offs, bcn);
1158 
1159 	kfree_skb(bcn);
1160 
1161 	if (ret)
1162 		ath12k_warn(ab, "failed to submit beacon template command: %d\n",
1163 			    ret);
1164 
1165 	return ret;
1166 }
1167 
1168 static void ath12k_control_beaconing(struct ath12k_vif *arvif,
1169 				     struct ieee80211_bss_conf *info)
1170 {
1171 	struct ath12k *ar = arvif->ar;
1172 	int ret;
1173 
1174 	lockdep_assert_held(&arvif->ar->conf_mutex);
1175 
1176 	if (!info->enable_beacon) {
1177 		ret = ath12k_wmi_vdev_down(ar, arvif->vdev_id);
1178 		if (ret)
1179 			ath12k_warn(ar->ab, "failed to down vdev_id %i: %d\n",
1180 				    arvif->vdev_id, ret);
1181 
1182 		arvif->is_up = false;
1183 		return;
1184 	}
1185 
1186 	/* Install the beacon template to the FW */
1187 	ret = ath12k_mac_setup_bcn_tmpl(arvif);
1188 	if (ret) {
1189 		ath12k_warn(ar->ab, "failed to update bcn tmpl during vdev up: %d\n",
1190 			    ret);
1191 		return;
1192 	}
1193 
1194 	arvif->aid = 0;
1195 
1196 	ether_addr_copy(arvif->bssid, info->bssid);
1197 
1198 	ret = ath12k_wmi_vdev_up(arvif->ar, arvif->vdev_id, arvif->aid,
1199 				 arvif->bssid);
1200 	if (ret) {
1201 		ath12k_warn(ar->ab, "failed to bring up vdev %d: %i\n",
1202 			    arvif->vdev_id, ret);
1203 		return;
1204 	}
1205 
1206 	arvif->is_up = true;
1207 
1208 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac vdev %d up\n", arvif->vdev_id);
1209 }
1210 
1211 static void ath12k_peer_assoc_h_basic(struct ath12k *ar,
1212 				      struct ieee80211_vif *vif,
1213 				      struct ieee80211_sta *sta,
1214 				      struct ath12k_wmi_peer_assoc_arg *arg)
1215 {
1216 	struct ath12k_vif *arvif = ath12k_vif_to_arvif(vif);
1217 	u32 aid;
1218 
1219 	lockdep_assert_held(&ar->conf_mutex);
1220 
1221 	if (vif->type == NL80211_IFTYPE_STATION)
1222 		aid = vif->cfg.aid;
1223 	else
1224 		aid = sta->aid;
1225 
1226 	ether_addr_copy(arg->peer_mac, sta->addr);
1227 	arg->vdev_id = arvif->vdev_id;
1228 	arg->peer_associd = aid;
1229 	arg->auth_flag = true;
1230 	/* TODO: STA WAR in ath10k for listen interval required? */
1231 	arg->peer_listen_intval = ar->hw->conf.listen_interval;
1232 	arg->peer_nss = 1;
1233 	arg->peer_caps = vif->bss_conf.assoc_capability;
1234 }
1235 
1236 static void ath12k_peer_assoc_h_crypto(struct ath12k *ar,
1237 				       struct ieee80211_vif *vif,
1238 				       struct ieee80211_sta *sta,
1239 				       struct ath12k_wmi_peer_assoc_arg *arg)
1240 {
1241 	struct ieee80211_bss_conf *info = &vif->bss_conf;
1242 	struct cfg80211_chan_def def;
1243 	struct cfg80211_bss *bss;
1244 	struct ath12k_vif *arvif = ath12k_vif_to_arvif(vif);
1245 	const u8 *rsnie = NULL;
1246 	const u8 *wpaie = NULL;
1247 
1248 	lockdep_assert_held(&ar->conf_mutex);
1249 
1250 	if (WARN_ON(ath12k_mac_vif_chan(vif, &def)))
1251 		return;
1252 
1253 	bss = cfg80211_get_bss(ar->hw->wiphy, def.chan, info->bssid, NULL, 0,
1254 			       IEEE80211_BSS_TYPE_ANY, IEEE80211_PRIVACY_ANY);
1255 
1256 	if (arvif->rsnie_present || arvif->wpaie_present) {
1257 		arg->need_ptk_4_way = true;
1258 		if (arvif->wpaie_present)
1259 			arg->need_gtk_2_way = true;
1260 	} else if (bss) {
1261 		const struct cfg80211_bss_ies *ies;
1262 
1263 		rcu_read_lock();
1264 		rsnie = ieee80211_bss_get_ie(bss, WLAN_EID_RSN);
1265 
1266 		ies = rcu_dereference(bss->ies);
1267 
1268 		wpaie = cfg80211_find_vendor_ie(WLAN_OUI_MICROSOFT,
1269 						WLAN_OUI_TYPE_MICROSOFT_WPA,
1270 						ies->data,
1271 						ies->len);
1272 		rcu_read_unlock();
1273 		cfg80211_put_bss(ar->hw->wiphy, bss);
1274 	}
1275 
1276 	/* FIXME: base on RSN IE/WPA IE is a correct idea? */
1277 	if (rsnie || wpaie) {
1278 		ath12k_dbg(ar->ab, ATH12K_DBG_WMI,
1279 			   "%s: rsn ie found\n", __func__);
1280 		arg->need_ptk_4_way = true;
1281 	}
1282 
1283 	if (wpaie) {
1284 		ath12k_dbg(ar->ab, ATH12K_DBG_WMI,
1285 			   "%s: wpa ie found\n", __func__);
1286 		arg->need_gtk_2_way = true;
1287 	}
1288 
1289 	if (sta->mfp) {
1290 		/* TODO: Need to check if FW supports PMF? */
1291 		arg->is_pmf_enabled = true;
1292 	}
1293 
1294 	/* TODO: safe_mode_enabled (bypass 4-way handshake) flag req? */
1295 }
1296 
1297 static void ath12k_peer_assoc_h_rates(struct ath12k *ar,
1298 				      struct ieee80211_vif *vif,
1299 				      struct ieee80211_sta *sta,
1300 				      struct ath12k_wmi_peer_assoc_arg *arg)
1301 {
1302 	struct ath12k_vif *arvif = ath12k_vif_to_arvif(vif);
1303 	struct wmi_rate_set_arg *rateset = &arg->peer_legacy_rates;
1304 	struct cfg80211_chan_def def;
1305 	const struct ieee80211_supported_band *sband;
1306 	const struct ieee80211_rate *rates;
1307 	enum nl80211_band band;
1308 	u32 ratemask;
1309 	u8 rate;
1310 	int i;
1311 
1312 	lockdep_assert_held(&ar->conf_mutex);
1313 
1314 	if (WARN_ON(ath12k_mac_vif_chan(vif, &def)))
1315 		return;
1316 
1317 	band = def.chan->band;
1318 	sband = ar->hw->wiphy->bands[band];
1319 	ratemask = sta->deflink.supp_rates[band];
1320 	ratemask &= arvif->bitrate_mask.control[band].legacy;
1321 	rates = sband->bitrates;
1322 
1323 	rateset->num_rates = 0;
1324 
1325 	for (i = 0; i < 32; i++, ratemask >>= 1, rates++) {
1326 		if (!(ratemask & 1))
1327 			continue;
1328 
1329 		rate = ath12k_mac_bitrate_to_rate(rates->bitrate);
1330 		rateset->rates[rateset->num_rates] = rate;
1331 		rateset->num_rates++;
1332 	}
1333 }
1334 
1335 static bool
1336 ath12k_peer_assoc_h_ht_masked(const u8 *ht_mcs_mask)
1337 {
1338 	int nss;
1339 
1340 	for (nss = 0; nss < IEEE80211_HT_MCS_MASK_LEN; nss++)
1341 		if (ht_mcs_mask[nss])
1342 			return false;
1343 
1344 	return true;
1345 }
1346 
1347 static bool
1348 ath12k_peer_assoc_h_vht_masked(const u16 *vht_mcs_mask)
1349 {
1350 	int nss;
1351 
1352 	for (nss = 0; nss < NL80211_VHT_NSS_MAX; nss++)
1353 		if (vht_mcs_mask[nss])
1354 			return false;
1355 
1356 	return true;
1357 }
1358 
1359 static void ath12k_peer_assoc_h_ht(struct ath12k *ar,
1360 				   struct ieee80211_vif *vif,
1361 				   struct ieee80211_sta *sta,
1362 				   struct ath12k_wmi_peer_assoc_arg *arg)
1363 {
1364 	const struct ieee80211_sta_ht_cap *ht_cap = &sta->deflink.ht_cap;
1365 	struct ath12k_vif *arvif = ath12k_vif_to_arvif(vif);
1366 	struct cfg80211_chan_def def;
1367 	enum nl80211_band band;
1368 	const u8 *ht_mcs_mask;
1369 	int i, n;
1370 	u8 max_nss;
1371 	u32 stbc;
1372 
1373 	lockdep_assert_held(&ar->conf_mutex);
1374 
1375 	if (WARN_ON(ath12k_mac_vif_chan(vif, &def)))
1376 		return;
1377 
1378 	if (!ht_cap->ht_supported)
1379 		return;
1380 
1381 	band = def.chan->band;
1382 	ht_mcs_mask = arvif->bitrate_mask.control[band].ht_mcs;
1383 
1384 	if (ath12k_peer_assoc_h_ht_masked(ht_mcs_mask))
1385 		return;
1386 
1387 	arg->ht_flag = true;
1388 
1389 	arg->peer_max_mpdu = (1 << (IEEE80211_HT_MAX_AMPDU_FACTOR +
1390 				    ht_cap->ampdu_factor)) - 1;
1391 
1392 	arg->peer_mpdu_density =
1393 		ath12k_parse_mpdudensity(ht_cap->ampdu_density);
1394 
1395 	arg->peer_ht_caps = ht_cap->cap;
1396 	arg->peer_rate_caps |= WMI_HOST_RC_HT_FLAG;
1397 
1398 	if (ht_cap->cap & IEEE80211_HT_CAP_LDPC_CODING)
1399 		arg->ldpc_flag = true;
1400 
1401 	if (sta->deflink.bandwidth >= IEEE80211_STA_RX_BW_40) {
1402 		arg->bw_40 = true;
1403 		arg->peer_rate_caps |= WMI_HOST_RC_CW40_FLAG;
1404 	}
1405 
1406 	if (arvif->bitrate_mask.control[band].gi != NL80211_TXRATE_FORCE_LGI) {
1407 		if (ht_cap->cap & (IEEE80211_HT_CAP_SGI_20 |
1408 		    IEEE80211_HT_CAP_SGI_40))
1409 			arg->peer_rate_caps |= WMI_HOST_RC_SGI_FLAG;
1410 	}
1411 
1412 	if (ht_cap->cap & IEEE80211_HT_CAP_TX_STBC) {
1413 		arg->peer_rate_caps |= WMI_HOST_RC_TX_STBC_FLAG;
1414 		arg->stbc_flag = true;
1415 	}
1416 
1417 	if (ht_cap->cap & IEEE80211_HT_CAP_RX_STBC) {
1418 		stbc = ht_cap->cap & IEEE80211_HT_CAP_RX_STBC;
1419 		stbc = stbc >> IEEE80211_HT_CAP_RX_STBC_SHIFT;
1420 		stbc = stbc << WMI_HOST_RC_RX_STBC_FLAG_S;
1421 		arg->peer_rate_caps |= stbc;
1422 		arg->stbc_flag = true;
1423 	}
1424 
1425 	if (ht_cap->mcs.rx_mask[1] && ht_cap->mcs.rx_mask[2])
1426 		arg->peer_rate_caps |= WMI_HOST_RC_TS_FLAG;
1427 	else if (ht_cap->mcs.rx_mask[1])
1428 		arg->peer_rate_caps |= WMI_HOST_RC_DS_FLAG;
1429 
1430 	for (i = 0, n = 0, max_nss = 0; i < IEEE80211_HT_MCS_MASK_LEN * 8; i++)
1431 		if ((ht_cap->mcs.rx_mask[i / 8] & BIT(i % 8)) &&
1432 		    (ht_mcs_mask[i / 8] & BIT(i % 8))) {
1433 			max_nss = (i / 8) + 1;
1434 			arg->peer_ht_rates.rates[n++] = i;
1435 		}
1436 
1437 	/* This is a workaround for HT-enabled STAs which break the spec
1438 	 * and have no HT capabilities RX mask (no HT RX MCS map).
1439 	 *
1440 	 * As per spec, in section 20.3.5 Modulation and coding scheme (MCS),
1441 	 * MCS 0 through 7 are mandatory in 20MHz with 800 ns GI at all STAs.
1442 	 *
1443 	 * Firmware asserts if such situation occurs.
1444 	 */
1445 	if (n == 0) {
1446 		arg->peer_ht_rates.num_rates = 8;
1447 		for (i = 0; i < arg->peer_ht_rates.num_rates; i++)
1448 			arg->peer_ht_rates.rates[i] = i;
1449 	} else {
1450 		arg->peer_ht_rates.num_rates = n;
1451 		arg->peer_nss = min(sta->deflink.rx_nss, max_nss);
1452 	}
1453 
1454 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac ht peer %pM mcs cnt %d nss %d\n",
1455 		   arg->peer_mac,
1456 		   arg->peer_ht_rates.num_rates,
1457 		   arg->peer_nss);
1458 }
1459 
1460 static int ath12k_mac_get_max_vht_mcs_map(u16 mcs_map, int nss)
1461 {
1462 	switch ((mcs_map >> (2 * nss)) & 0x3) {
1463 	case IEEE80211_VHT_MCS_SUPPORT_0_7: return BIT(8) - 1;
1464 	case IEEE80211_VHT_MCS_SUPPORT_0_8: return BIT(9) - 1;
1465 	case IEEE80211_VHT_MCS_SUPPORT_0_9: return BIT(10) - 1;
1466 	}
1467 	return 0;
1468 }
1469 
1470 static u16
1471 ath12k_peer_assoc_h_vht_limit(u16 tx_mcs_set,
1472 			      const u16 vht_mcs_limit[NL80211_VHT_NSS_MAX])
1473 {
1474 	int idx_limit;
1475 	int nss;
1476 	u16 mcs_map;
1477 	u16 mcs;
1478 
1479 	for (nss = 0; nss < NL80211_VHT_NSS_MAX; nss++) {
1480 		mcs_map = ath12k_mac_get_max_vht_mcs_map(tx_mcs_set, nss) &
1481 			  vht_mcs_limit[nss];
1482 
1483 		if (mcs_map)
1484 			idx_limit = fls(mcs_map) - 1;
1485 		else
1486 			idx_limit = -1;
1487 
1488 		switch (idx_limit) {
1489 		case 0:
1490 		case 1:
1491 		case 2:
1492 		case 3:
1493 		case 4:
1494 		case 5:
1495 		case 6:
1496 		case 7:
1497 			mcs = IEEE80211_VHT_MCS_SUPPORT_0_7;
1498 			break;
1499 		case 8:
1500 			mcs = IEEE80211_VHT_MCS_SUPPORT_0_8;
1501 			break;
1502 		case 9:
1503 			mcs = IEEE80211_VHT_MCS_SUPPORT_0_9;
1504 			break;
1505 		default:
1506 			WARN_ON(1);
1507 			fallthrough;
1508 		case -1:
1509 			mcs = IEEE80211_VHT_MCS_NOT_SUPPORTED;
1510 			break;
1511 		}
1512 
1513 		tx_mcs_set &= ~(0x3 << (nss * 2));
1514 		tx_mcs_set |= mcs << (nss * 2);
1515 	}
1516 
1517 	return tx_mcs_set;
1518 }
1519 
1520 static void ath12k_peer_assoc_h_vht(struct ath12k *ar,
1521 				    struct ieee80211_vif *vif,
1522 				    struct ieee80211_sta *sta,
1523 				    struct ath12k_wmi_peer_assoc_arg *arg)
1524 {
1525 	const struct ieee80211_sta_vht_cap *vht_cap = &sta->deflink.vht_cap;
1526 	struct ath12k_vif *arvif = ath12k_vif_to_arvif(vif);
1527 	struct cfg80211_chan_def def;
1528 	enum nl80211_band band;
1529 	const u16 *vht_mcs_mask;
1530 	u16 tx_mcs_map;
1531 	u8 ampdu_factor;
1532 	u8 max_nss, vht_mcs;
1533 	int i;
1534 
1535 	if (WARN_ON(ath12k_mac_vif_chan(vif, &def)))
1536 		return;
1537 
1538 	if (!vht_cap->vht_supported)
1539 		return;
1540 
1541 	band = def.chan->band;
1542 	vht_mcs_mask = arvif->bitrate_mask.control[band].vht_mcs;
1543 
1544 	if (ath12k_peer_assoc_h_vht_masked(vht_mcs_mask))
1545 		return;
1546 
1547 	arg->vht_flag = true;
1548 
1549 	/* TODO: similar flags required? */
1550 	arg->vht_capable = true;
1551 
1552 	if (def.chan->band == NL80211_BAND_2GHZ)
1553 		arg->vht_ng_flag = true;
1554 
1555 	arg->peer_vht_caps = vht_cap->cap;
1556 
1557 	ampdu_factor = (vht_cap->cap &
1558 			IEEE80211_VHT_CAP_MAX_A_MPDU_LENGTH_EXPONENT_MASK) >>
1559 		       IEEE80211_VHT_CAP_MAX_A_MPDU_LENGTH_EXPONENT_SHIFT;
1560 
1561 	/* Workaround: Some Netgear/Linksys 11ac APs set Rx A-MPDU factor to
1562 	 * zero in VHT IE. Using it would result in degraded throughput.
1563 	 * arg->peer_max_mpdu at this point contains HT max_mpdu so keep
1564 	 * it if VHT max_mpdu is smaller.
1565 	 */
1566 	arg->peer_max_mpdu = max(arg->peer_max_mpdu,
1567 				 (1U << (IEEE80211_HT_MAX_AMPDU_FACTOR +
1568 					ampdu_factor)) - 1);
1569 
1570 	if (sta->deflink.bandwidth == IEEE80211_STA_RX_BW_80)
1571 		arg->bw_80 = true;
1572 
1573 	if (sta->deflink.bandwidth == IEEE80211_STA_RX_BW_160)
1574 		arg->bw_160 = true;
1575 
1576 	/* Calculate peer NSS capability from VHT capabilities if STA
1577 	 * supports VHT.
1578 	 */
1579 	for (i = 0, max_nss = 0, vht_mcs = 0; i < NL80211_VHT_NSS_MAX; i++) {
1580 		vht_mcs = __le16_to_cpu(vht_cap->vht_mcs.rx_mcs_map) >>
1581 			  (2 * i) & 3;
1582 
1583 		if (vht_mcs != IEEE80211_VHT_MCS_NOT_SUPPORTED &&
1584 		    vht_mcs_mask[i])
1585 			max_nss = i + 1;
1586 	}
1587 	arg->peer_nss = min(sta->deflink.rx_nss, max_nss);
1588 	arg->rx_max_rate = __le16_to_cpu(vht_cap->vht_mcs.rx_highest);
1589 	arg->rx_mcs_set = __le16_to_cpu(vht_cap->vht_mcs.rx_mcs_map);
1590 	arg->tx_max_rate = __le16_to_cpu(vht_cap->vht_mcs.tx_highest);
1591 
1592 	tx_mcs_map = __le16_to_cpu(vht_cap->vht_mcs.tx_mcs_map);
1593 	arg->tx_mcs_set = ath12k_peer_assoc_h_vht_limit(tx_mcs_map, vht_mcs_mask);
1594 
1595 	/* In QCN9274 platform, VHT MCS rate 10 and 11 is enabled by default.
1596 	 * VHT MCS rate 10 and 11 is not supported in 11ac standard.
1597 	 * so explicitly disable the VHT MCS rate 10 and 11 in 11ac mode.
1598 	 */
1599 	arg->tx_mcs_set &= ~IEEE80211_VHT_MCS_SUPPORT_0_11_MASK;
1600 	arg->tx_mcs_set |= IEEE80211_DISABLE_VHT_MCS_SUPPORT_0_11;
1601 
1602 	if ((arg->tx_mcs_set & IEEE80211_VHT_MCS_NOT_SUPPORTED) ==
1603 			IEEE80211_VHT_MCS_NOT_SUPPORTED)
1604 		arg->peer_vht_caps &= ~IEEE80211_VHT_CAP_MU_BEAMFORMEE_CAPABLE;
1605 
1606 	/* TODO:  Check */
1607 	arg->tx_max_mcs_nss = 0xFF;
1608 
1609 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac vht peer %pM max_mpdu %d flags 0x%x\n",
1610 		   sta->addr, arg->peer_max_mpdu, arg->peer_flags);
1611 
1612 	/* TODO: rxnss_override */
1613 }
1614 
1615 static void ath12k_peer_assoc_h_he(struct ath12k *ar,
1616 				   struct ieee80211_vif *vif,
1617 				   struct ieee80211_sta *sta,
1618 				   struct ath12k_wmi_peer_assoc_arg *arg)
1619 {
1620 	const struct ieee80211_sta_he_cap *he_cap = &sta->deflink.he_cap;
1621 	int i;
1622 	u8 ampdu_factor, rx_mcs_80, rx_mcs_160, max_nss;
1623 	u16 mcs_160_map, mcs_80_map;
1624 	bool support_160;
1625 	u16 v;
1626 
1627 	if (!he_cap->has_he)
1628 		return;
1629 
1630 	arg->he_flag = true;
1631 
1632 	support_160 = !!(he_cap->he_cap_elem.phy_cap_info[0] &
1633 		  IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_160MHZ_IN_5G);
1634 
1635 	/* Supported HE-MCS and NSS Set of peer he_cap is intersection with self he_cp */
1636 	mcs_160_map = le16_to_cpu(he_cap->he_mcs_nss_supp.rx_mcs_160);
1637 	mcs_80_map = le16_to_cpu(he_cap->he_mcs_nss_supp.rx_mcs_80);
1638 
1639 	if (support_160) {
1640 		for (i = 7; i >= 0; i--) {
1641 			u8 mcs_160 = (mcs_160_map >> (2 * i)) & 3;
1642 
1643 			if (mcs_160 != IEEE80211_HE_MCS_NOT_SUPPORTED) {
1644 				rx_mcs_160 = i + 1;
1645 				break;
1646 			}
1647 		}
1648 	}
1649 
1650 	for (i = 7; i >= 0; i--) {
1651 		u8 mcs_80 = (mcs_80_map >> (2 * i)) & 3;
1652 
1653 		if (mcs_80 != IEEE80211_HE_MCS_NOT_SUPPORTED) {
1654 			rx_mcs_80 = i + 1;
1655 			break;
1656 		}
1657 	}
1658 
1659 	if (support_160)
1660 		max_nss = min(rx_mcs_80, rx_mcs_160);
1661 	else
1662 		max_nss = rx_mcs_80;
1663 
1664 	arg->peer_nss = min(sta->deflink.rx_nss, max_nss);
1665 
1666 	memcpy(&arg->peer_he_cap_macinfo, he_cap->he_cap_elem.mac_cap_info,
1667 	       sizeof(he_cap->he_cap_elem.mac_cap_info));
1668 	memcpy(&arg->peer_he_cap_phyinfo, he_cap->he_cap_elem.phy_cap_info,
1669 	       sizeof(he_cap->he_cap_elem.phy_cap_info));
1670 	arg->peer_he_ops = vif->bss_conf.he_oper.params;
1671 
1672 	/* the top most byte is used to indicate BSS color info */
1673 	arg->peer_he_ops &= 0xffffff;
1674 
1675 	/* As per section 26.6.1 IEEE Std 802.11ax‐2022, if the Max AMPDU
1676 	 * Exponent Extension in HE cap is zero, use the arg->peer_max_mpdu
1677 	 * as calculated while parsing VHT caps(if VHT caps is present)
1678 	 * or HT caps (if VHT caps is not present).
1679 	 *
1680 	 * For non-zero value of Max AMPDU Exponent Extension in HE MAC caps,
1681 	 * if a HE STA sends VHT cap and HE cap IE in assoc request then, use
1682 	 * MAX_AMPDU_LEN_FACTOR as 20 to calculate max_ampdu length.
1683 	 * If a HE STA that does not send VHT cap, but HE and HT cap in assoc
1684 	 * request, then use MAX_AMPDU_LEN_FACTOR as 16 to calculate max_ampdu
1685 	 * length.
1686 	 */
1687 	ampdu_factor = (he_cap->he_cap_elem.mac_cap_info[3] &
1688 			IEEE80211_HE_MAC_CAP3_MAX_AMPDU_LEN_EXP_MASK) >>
1689 			IEEE80211_HE_MAC_CAP3_MAX_AMPDU_LEN_EXP_MASK;
1690 
1691 	if (ampdu_factor) {
1692 		if (sta->deflink.vht_cap.vht_supported)
1693 			arg->peer_max_mpdu = (1 << (IEEE80211_HE_VHT_MAX_AMPDU_FACTOR +
1694 						    ampdu_factor)) - 1;
1695 		else if (sta->deflink.ht_cap.ht_supported)
1696 			arg->peer_max_mpdu = (1 << (IEEE80211_HE_HT_MAX_AMPDU_FACTOR +
1697 						    ampdu_factor)) - 1;
1698 	}
1699 
1700 	if (he_cap->he_cap_elem.phy_cap_info[6] &
1701 	    IEEE80211_HE_PHY_CAP6_PPE_THRESHOLD_PRESENT) {
1702 		int bit = 7;
1703 		int nss, ru;
1704 
1705 		arg->peer_ppet.numss_m1 = he_cap->ppe_thres[0] &
1706 					  IEEE80211_PPE_THRES_NSS_MASK;
1707 		arg->peer_ppet.ru_bit_mask =
1708 			(he_cap->ppe_thres[0] &
1709 			 IEEE80211_PPE_THRES_RU_INDEX_BITMASK_MASK) >>
1710 			IEEE80211_PPE_THRES_RU_INDEX_BITMASK_POS;
1711 
1712 		for (nss = 0; nss <= arg->peer_ppet.numss_m1; nss++) {
1713 			for (ru = 0; ru < 4; ru++) {
1714 				u32 val = 0;
1715 				int i;
1716 
1717 				if ((arg->peer_ppet.ru_bit_mask & BIT(ru)) == 0)
1718 					continue;
1719 				for (i = 0; i < 6; i++) {
1720 					val >>= 1;
1721 					val |= ((he_cap->ppe_thres[bit / 8] >>
1722 						 (bit % 8)) & 0x1) << 5;
1723 					bit++;
1724 				}
1725 				arg->peer_ppet.ppet16_ppet8_ru3_ru0[nss] |=
1726 								val << (ru * 6);
1727 			}
1728 		}
1729 	}
1730 
1731 	if (he_cap->he_cap_elem.mac_cap_info[0] & IEEE80211_HE_MAC_CAP0_TWT_RES)
1732 		arg->twt_responder = true;
1733 	if (he_cap->he_cap_elem.mac_cap_info[0] & IEEE80211_HE_MAC_CAP0_TWT_REQ)
1734 		arg->twt_requester = true;
1735 
1736 	switch (sta->deflink.bandwidth) {
1737 	case IEEE80211_STA_RX_BW_160:
1738 		if (he_cap->he_cap_elem.phy_cap_info[0] &
1739 		    IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_80PLUS80_MHZ_IN_5G) {
1740 			v = le16_to_cpu(he_cap->he_mcs_nss_supp.rx_mcs_80p80);
1741 			arg->peer_he_rx_mcs_set[WMI_HECAP_TXRX_MCS_NSS_IDX_80_80] = v;
1742 
1743 			v = le16_to_cpu(he_cap->he_mcs_nss_supp.tx_mcs_80p80);
1744 			arg->peer_he_tx_mcs_set[WMI_HECAP_TXRX_MCS_NSS_IDX_80_80] = v;
1745 
1746 			arg->peer_he_mcs_count++;
1747 		}
1748 		v = le16_to_cpu(he_cap->he_mcs_nss_supp.rx_mcs_160);
1749 		arg->peer_he_rx_mcs_set[WMI_HECAP_TXRX_MCS_NSS_IDX_160] = v;
1750 
1751 		v = le16_to_cpu(he_cap->he_mcs_nss_supp.tx_mcs_160);
1752 		arg->peer_he_tx_mcs_set[WMI_HECAP_TXRX_MCS_NSS_IDX_160] = v;
1753 
1754 		arg->peer_he_mcs_count++;
1755 		fallthrough;
1756 
1757 	default:
1758 		v = le16_to_cpu(he_cap->he_mcs_nss_supp.rx_mcs_80);
1759 		arg->peer_he_rx_mcs_set[WMI_HECAP_TXRX_MCS_NSS_IDX_80] = v;
1760 
1761 		v = le16_to_cpu(he_cap->he_mcs_nss_supp.tx_mcs_80);
1762 		arg->peer_he_tx_mcs_set[WMI_HECAP_TXRX_MCS_NSS_IDX_80] = v;
1763 
1764 		arg->peer_he_mcs_count++;
1765 		break;
1766 	}
1767 }
1768 
1769 static void ath12k_peer_assoc_h_smps(struct ieee80211_sta *sta,
1770 				     struct ath12k_wmi_peer_assoc_arg *arg)
1771 {
1772 	const struct ieee80211_sta_ht_cap *ht_cap = &sta->deflink.ht_cap;
1773 	int smps;
1774 
1775 	if (!ht_cap->ht_supported)
1776 		return;
1777 
1778 	smps = ht_cap->cap & IEEE80211_HT_CAP_SM_PS;
1779 	smps >>= IEEE80211_HT_CAP_SM_PS_SHIFT;
1780 
1781 	switch (smps) {
1782 	case WLAN_HT_CAP_SM_PS_STATIC:
1783 		arg->static_mimops_flag = true;
1784 		break;
1785 	case WLAN_HT_CAP_SM_PS_DYNAMIC:
1786 		arg->dynamic_mimops_flag = true;
1787 		break;
1788 	case WLAN_HT_CAP_SM_PS_DISABLED:
1789 		arg->spatial_mux_flag = true;
1790 		break;
1791 	default:
1792 		break;
1793 	}
1794 }
1795 
1796 static void ath12k_peer_assoc_h_qos(struct ath12k *ar,
1797 				    struct ieee80211_vif *vif,
1798 				    struct ieee80211_sta *sta,
1799 				    struct ath12k_wmi_peer_assoc_arg *arg)
1800 {
1801 	struct ath12k_vif *arvif = ath12k_vif_to_arvif(vif);
1802 
1803 	switch (arvif->vdev_type) {
1804 	case WMI_VDEV_TYPE_AP:
1805 		if (sta->wme) {
1806 			/* TODO: Check WME vs QoS */
1807 			arg->is_wme_set = true;
1808 			arg->qos_flag = true;
1809 		}
1810 
1811 		if (sta->wme && sta->uapsd_queues) {
1812 			/* TODO: Check WME vs QoS */
1813 			arg->is_wme_set = true;
1814 			arg->apsd_flag = true;
1815 			arg->peer_rate_caps |= WMI_HOST_RC_UAPSD_FLAG;
1816 		}
1817 		break;
1818 	case WMI_VDEV_TYPE_STA:
1819 		if (sta->wme) {
1820 			arg->is_wme_set = true;
1821 			arg->qos_flag = true;
1822 		}
1823 		break;
1824 	default:
1825 		break;
1826 	}
1827 
1828 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac peer %pM qos %d\n",
1829 		   sta->addr, arg->qos_flag);
1830 }
1831 
1832 static int ath12k_peer_assoc_qos_ap(struct ath12k *ar,
1833 				    struct ath12k_vif *arvif,
1834 				    struct ieee80211_sta *sta)
1835 {
1836 	struct ath12k_wmi_ap_ps_arg arg;
1837 	u32 max_sp;
1838 	u32 uapsd;
1839 	int ret;
1840 
1841 	lockdep_assert_held(&ar->conf_mutex);
1842 
1843 	arg.vdev_id = arvif->vdev_id;
1844 
1845 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac uapsd_queues 0x%x max_sp %d\n",
1846 		   sta->uapsd_queues, sta->max_sp);
1847 
1848 	uapsd = 0;
1849 	if (sta->uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_VO)
1850 		uapsd |= WMI_AP_PS_UAPSD_AC3_DELIVERY_EN |
1851 			 WMI_AP_PS_UAPSD_AC3_TRIGGER_EN;
1852 	if (sta->uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_VI)
1853 		uapsd |= WMI_AP_PS_UAPSD_AC2_DELIVERY_EN |
1854 			 WMI_AP_PS_UAPSD_AC2_TRIGGER_EN;
1855 	if (sta->uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_BK)
1856 		uapsd |= WMI_AP_PS_UAPSD_AC1_DELIVERY_EN |
1857 			 WMI_AP_PS_UAPSD_AC1_TRIGGER_EN;
1858 	if (sta->uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_BE)
1859 		uapsd |= WMI_AP_PS_UAPSD_AC0_DELIVERY_EN |
1860 			 WMI_AP_PS_UAPSD_AC0_TRIGGER_EN;
1861 
1862 	max_sp = 0;
1863 	if (sta->max_sp < MAX_WMI_AP_PS_PEER_PARAM_MAX_SP)
1864 		max_sp = sta->max_sp;
1865 
1866 	arg.param = WMI_AP_PS_PEER_PARAM_UAPSD;
1867 	arg.value = uapsd;
1868 	ret = ath12k_wmi_send_set_ap_ps_param_cmd(ar, sta->addr, &arg);
1869 	if (ret)
1870 		goto err;
1871 
1872 	arg.param = WMI_AP_PS_PEER_PARAM_MAX_SP;
1873 	arg.value = max_sp;
1874 	ret = ath12k_wmi_send_set_ap_ps_param_cmd(ar, sta->addr, &arg);
1875 	if (ret)
1876 		goto err;
1877 
1878 	/* TODO: revisit during testing */
1879 	arg.param = WMI_AP_PS_PEER_PARAM_SIFS_RESP_FRMTYPE;
1880 	arg.value = DISABLE_SIFS_RESPONSE_TRIGGER;
1881 	ret = ath12k_wmi_send_set_ap_ps_param_cmd(ar, sta->addr, &arg);
1882 	if (ret)
1883 		goto err;
1884 
1885 	arg.param = WMI_AP_PS_PEER_PARAM_SIFS_RESP_UAPSD;
1886 	arg.value = DISABLE_SIFS_RESPONSE_TRIGGER;
1887 	ret = ath12k_wmi_send_set_ap_ps_param_cmd(ar, sta->addr, &arg);
1888 	if (ret)
1889 		goto err;
1890 
1891 	return 0;
1892 
1893 err:
1894 	ath12k_warn(ar->ab, "failed to set ap ps peer param %d for vdev %i: %d\n",
1895 		    arg.param, arvif->vdev_id, ret);
1896 	return ret;
1897 }
1898 
1899 static bool ath12k_mac_sta_has_ofdm_only(struct ieee80211_sta *sta)
1900 {
1901 	return sta->deflink.supp_rates[NL80211_BAND_2GHZ] >>
1902 	       ATH12K_MAC_FIRST_OFDM_RATE_IDX;
1903 }
1904 
1905 static enum wmi_phy_mode ath12k_mac_get_phymode_vht(struct ath12k *ar,
1906 						    struct ieee80211_sta *sta)
1907 {
1908 	if (sta->deflink.bandwidth == IEEE80211_STA_RX_BW_160) {
1909 		switch (sta->deflink.vht_cap.cap &
1910 			IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_MASK) {
1911 		case IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_160MHZ:
1912 			return MODE_11AC_VHT160;
1913 		case IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_160_80PLUS80MHZ:
1914 			return MODE_11AC_VHT80_80;
1915 		default:
1916 			/* not sure if this is a valid case? */
1917 			return MODE_11AC_VHT160;
1918 		}
1919 	}
1920 
1921 	if (sta->deflink.bandwidth == IEEE80211_STA_RX_BW_80)
1922 		return MODE_11AC_VHT80;
1923 
1924 	if (sta->deflink.bandwidth == IEEE80211_STA_RX_BW_40)
1925 		return MODE_11AC_VHT40;
1926 
1927 	if (sta->deflink.bandwidth == IEEE80211_STA_RX_BW_20)
1928 		return MODE_11AC_VHT20;
1929 
1930 	return MODE_UNKNOWN;
1931 }
1932 
1933 static enum wmi_phy_mode ath12k_mac_get_phymode_he(struct ath12k *ar,
1934 						   struct ieee80211_sta *sta)
1935 {
1936 	if (sta->deflink.bandwidth == IEEE80211_STA_RX_BW_160) {
1937 		if (sta->deflink.he_cap.he_cap_elem.phy_cap_info[0] &
1938 		     IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_160MHZ_IN_5G)
1939 			return MODE_11AX_HE160;
1940 		else if (sta->deflink.he_cap.he_cap_elem.phy_cap_info[0] &
1941 		     IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_80PLUS80_MHZ_IN_5G)
1942 			return MODE_11AX_HE80_80;
1943 		/* not sure if this is a valid case? */
1944 		return MODE_11AX_HE160;
1945 	}
1946 
1947 	if (sta->deflink.bandwidth == IEEE80211_STA_RX_BW_80)
1948 		return MODE_11AX_HE80;
1949 
1950 	if (sta->deflink.bandwidth == IEEE80211_STA_RX_BW_40)
1951 		return MODE_11AX_HE40;
1952 
1953 	if (sta->deflink.bandwidth == IEEE80211_STA_RX_BW_20)
1954 		return MODE_11AX_HE20;
1955 
1956 	return MODE_UNKNOWN;
1957 }
1958 
1959 static enum wmi_phy_mode ath12k_mac_get_phymode_eht(struct ath12k *ar,
1960 						    struct ieee80211_sta *sta)
1961 {
1962 	if (sta->deflink.bandwidth == IEEE80211_STA_RX_BW_320)
1963 		if (sta->deflink.eht_cap.eht_cap_elem.phy_cap_info[0] &
1964 		    IEEE80211_EHT_PHY_CAP0_320MHZ_IN_6GHZ)
1965 			return MODE_11BE_EHT320;
1966 
1967 	if (sta->deflink.bandwidth == IEEE80211_STA_RX_BW_160) {
1968 		if (sta->deflink.he_cap.he_cap_elem.phy_cap_info[0] &
1969 		    IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_160MHZ_IN_5G)
1970 			return MODE_11BE_EHT160;
1971 
1972 		if (sta->deflink.he_cap.he_cap_elem.phy_cap_info[0] &
1973 			 IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_80PLUS80_MHZ_IN_5G)
1974 			return MODE_11BE_EHT80_80;
1975 
1976 		ath12k_warn(ar->ab, "invalid EHT PHY capability info for 160 Mhz: %d\n",
1977 			    sta->deflink.he_cap.he_cap_elem.phy_cap_info[0]);
1978 
1979 		return MODE_11BE_EHT160;
1980 	}
1981 
1982 	if (sta->deflink.bandwidth == IEEE80211_STA_RX_BW_80)
1983 		return MODE_11BE_EHT80;
1984 
1985 	if (sta->deflink.bandwidth == IEEE80211_STA_RX_BW_40)
1986 		return MODE_11BE_EHT40;
1987 
1988 	if (sta->deflink.bandwidth == IEEE80211_STA_RX_BW_20)
1989 		return MODE_11BE_EHT20;
1990 
1991 	return MODE_UNKNOWN;
1992 }
1993 
1994 static void ath12k_peer_assoc_h_phymode(struct ath12k *ar,
1995 					struct ieee80211_vif *vif,
1996 					struct ieee80211_sta *sta,
1997 					struct ath12k_wmi_peer_assoc_arg *arg)
1998 {
1999 	struct ath12k_vif *arvif = ath12k_vif_to_arvif(vif);
2000 	struct cfg80211_chan_def def;
2001 	enum nl80211_band band;
2002 	const u8 *ht_mcs_mask;
2003 	const u16 *vht_mcs_mask;
2004 	enum wmi_phy_mode phymode = MODE_UNKNOWN;
2005 
2006 	if (WARN_ON(ath12k_mac_vif_chan(vif, &def)))
2007 		return;
2008 
2009 	band = def.chan->band;
2010 	ht_mcs_mask = arvif->bitrate_mask.control[band].ht_mcs;
2011 	vht_mcs_mask = arvif->bitrate_mask.control[band].vht_mcs;
2012 
2013 	switch (band) {
2014 	case NL80211_BAND_2GHZ:
2015 		if (sta->deflink.eht_cap.has_eht) {
2016 			if (sta->deflink.bandwidth == IEEE80211_STA_RX_BW_40)
2017 				phymode = MODE_11BE_EHT40_2G;
2018 			else
2019 				phymode = MODE_11BE_EHT20_2G;
2020 		} else if (sta->deflink.he_cap.has_he) {
2021 			if (sta->deflink.bandwidth == IEEE80211_STA_RX_BW_80)
2022 				phymode = MODE_11AX_HE80_2G;
2023 			else if (sta->deflink.bandwidth == IEEE80211_STA_RX_BW_40)
2024 				phymode = MODE_11AX_HE40_2G;
2025 			else
2026 				phymode = MODE_11AX_HE20_2G;
2027 		} else if (sta->deflink.vht_cap.vht_supported &&
2028 		    !ath12k_peer_assoc_h_vht_masked(vht_mcs_mask)) {
2029 			if (sta->deflink.bandwidth == IEEE80211_STA_RX_BW_40)
2030 				phymode = MODE_11AC_VHT40;
2031 			else
2032 				phymode = MODE_11AC_VHT20;
2033 		} else if (sta->deflink.ht_cap.ht_supported &&
2034 			   !ath12k_peer_assoc_h_ht_masked(ht_mcs_mask)) {
2035 			if (sta->deflink.bandwidth == IEEE80211_STA_RX_BW_40)
2036 				phymode = MODE_11NG_HT40;
2037 			else
2038 				phymode = MODE_11NG_HT20;
2039 		} else if (ath12k_mac_sta_has_ofdm_only(sta)) {
2040 			phymode = MODE_11G;
2041 		} else {
2042 			phymode = MODE_11B;
2043 		}
2044 		break;
2045 	case NL80211_BAND_5GHZ:
2046 	case NL80211_BAND_6GHZ:
2047 		/* Check EHT first */
2048 		if (sta->deflink.eht_cap.has_eht) {
2049 			phymode = ath12k_mac_get_phymode_eht(ar, sta);
2050 		} else if (sta->deflink.he_cap.has_he) {
2051 			phymode = ath12k_mac_get_phymode_he(ar, sta);
2052 		} else if (sta->deflink.vht_cap.vht_supported &&
2053 		    !ath12k_peer_assoc_h_vht_masked(vht_mcs_mask)) {
2054 			phymode = ath12k_mac_get_phymode_vht(ar, sta);
2055 		} else if (sta->deflink.ht_cap.ht_supported &&
2056 			   !ath12k_peer_assoc_h_ht_masked(ht_mcs_mask)) {
2057 			if (sta->deflink.bandwidth >= IEEE80211_STA_RX_BW_40)
2058 				phymode = MODE_11NA_HT40;
2059 			else
2060 				phymode = MODE_11NA_HT20;
2061 		} else {
2062 			phymode = MODE_11A;
2063 		}
2064 		break;
2065 	default:
2066 		break;
2067 	}
2068 
2069 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac peer %pM phymode %s\n",
2070 		   sta->addr, ath12k_mac_phymode_str(phymode));
2071 
2072 	arg->peer_phymode = phymode;
2073 	WARN_ON(phymode == MODE_UNKNOWN);
2074 }
2075 
2076 static void ath12k_mac_set_eht_mcs(u8 rx_tx_mcs7, u8 rx_tx_mcs9,
2077 				   u8 rx_tx_mcs11, u8 rx_tx_mcs13,
2078 				   u32 *rx_mcs, u32 *tx_mcs)
2079 {
2080 	*rx_mcs = 0;
2081 	u32p_replace_bits(rx_mcs,
2082 			  u8_get_bits(rx_tx_mcs7, IEEE80211_EHT_MCS_NSS_RX),
2083 			  WMI_EHT_MCS_NSS_0_7);
2084 	u32p_replace_bits(rx_mcs,
2085 			  u8_get_bits(rx_tx_mcs9, IEEE80211_EHT_MCS_NSS_RX),
2086 			  WMI_EHT_MCS_NSS_8_9);
2087 	u32p_replace_bits(rx_mcs,
2088 			  u8_get_bits(rx_tx_mcs11, IEEE80211_EHT_MCS_NSS_RX),
2089 			  WMI_EHT_MCS_NSS_10_11);
2090 	u32p_replace_bits(rx_mcs,
2091 			  u8_get_bits(rx_tx_mcs13, IEEE80211_EHT_MCS_NSS_RX),
2092 			  WMI_EHT_MCS_NSS_12_13);
2093 
2094 	*tx_mcs = 0;
2095 	u32p_replace_bits(tx_mcs,
2096 			  u8_get_bits(rx_tx_mcs7, IEEE80211_EHT_MCS_NSS_TX),
2097 			  WMI_EHT_MCS_NSS_0_7);
2098 	u32p_replace_bits(tx_mcs,
2099 			  u8_get_bits(rx_tx_mcs9, IEEE80211_EHT_MCS_NSS_TX),
2100 			  WMI_EHT_MCS_NSS_8_9);
2101 	u32p_replace_bits(tx_mcs,
2102 			  u8_get_bits(rx_tx_mcs11, IEEE80211_EHT_MCS_NSS_TX),
2103 			  WMI_EHT_MCS_NSS_10_11);
2104 	u32p_replace_bits(tx_mcs,
2105 			  u8_get_bits(rx_tx_mcs13, IEEE80211_EHT_MCS_NSS_TX),
2106 			  WMI_EHT_MCS_NSS_12_13);
2107 }
2108 
2109 static void ath12k_mac_set_eht_ppe_threshold(const u8 *ppe_thres,
2110 					     struct ath12k_wmi_ppe_threshold_arg *ppet)
2111 {
2112 	u32 bit_pos = IEEE80211_EHT_PPE_THRES_INFO_HEADER_SIZE, val;
2113 	u8 nss, ru, i;
2114 	u8 ppet_bit_len_per_ru = IEEE80211_EHT_PPE_THRES_INFO_PPET_SIZE * 2;
2115 
2116 	ppet->numss_m1 = u8_get_bits(ppe_thres[0], IEEE80211_EHT_PPE_THRES_NSS_MASK);
2117 	ppet->ru_bit_mask = u16_get_bits(get_unaligned_le16(ppe_thres),
2118 					 IEEE80211_EHT_PPE_THRES_RU_INDEX_BITMASK_MASK);
2119 
2120 	for (nss = 0; nss <= ppet->numss_m1; nss++) {
2121 		for (ru = 0;
2122 		     ru < hweight16(IEEE80211_EHT_PPE_THRES_RU_INDEX_BITMASK_MASK);
2123 		     ru++) {
2124 			if ((ppet->ru_bit_mask & BIT(ru)) == 0)
2125 				continue;
2126 
2127 			val = 0;
2128 			for (i = 0; i < ppet_bit_len_per_ru; i++) {
2129 				val |= (((ppe_thres[bit_pos / 8] >>
2130 					  (bit_pos % 8)) & 0x1) << i);
2131 				bit_pos++;
2132 			}
2133 			ppet->ppet16_ppet8_ru3_ru0[nss] |=
2134 					(val << (ru * ppet_bit_len_per_ru));
2135 		}
2136 	}
2137 }
2138 
2139 static void ath12k_peer_assoc_h_eht(struct ath12k *ar,
2140 				    struct ieee80211_vif *vif,
2141 				    struct ieee80211_sta *sta,
2142 				    struct ath12k_wmi_peer_assoc_arg *arg)
2143 {
2144 	const struct ieee80211_sta_eht_cap *eht_cap = &sta->deflink.eht_cap;
2145 	const struct ieee80211_sta_he_cap *he_cap = &sta->deflink.he_cap;
2146 	const struct ieee80211_eht_mcs_nss_supp_20mhz_only *bw_20;
2147 	const struct ieee80211_eht_mcs_nss_supp_bw *bw;
2148 	struct ath12k_vif *arvif = ath12k_vif_to_arvif(vif);
2149 	u32 *rx_mcs, *tx_mcs;
2150 
2151 	if (!sta->deflink.he_cap.has_he || !eht_cap->has_eht)
2152 		return;
2153 
2154 	arg->eht_flag = true;
2155 
2156 	if ((eht_cap->eht_cap_elem.phy_cap_info[5] &
2157 	     IEEE80211_EHT_PHY_CAP5_PPE_THRESHOLD_PRESENT) &&
2158 	    eht_cap->eht_ppe_thres[0] != 0)
2159 		ath12k_mac_set_eht_ppe_threshold(eht_cap->eht_ppe_thres,
2160 						 &arg->peer_eht_ppet);
2161 
2162 	memcpy(arg->peer_eht_cap_mac, eht_cap->eht_cap_elem.mac_cap_info,
2163 	       sizeof(eht_cap->eht_cap_elem.mac_cap_info));
2164 	memcpy(arg->peer_eht_cap_phy, eht_cap->eht_cap_elem.phy_cap_info,
2165 	       sizeof(eht_cap->eht_cap_elem.phy_cap_info));
2166 
2167 	rx_mcs = arg->peer_eht_rx_mcs_set;
2168 	tx_mcs = arg->peer_eht_tx_mcs_set;
2169 
2170 	switch (sta->deflink.bandwidth) {
2171 	case IEEE80211_STA_RX_BW_320:
2172 		bw = &eht_cap->eht_mcs_nss_supp.bw._320;
2173 		ath12k_mac_set_eht_mcs(bw->rx_tx_mcs9_max_nss,
2174 				       bw->rx_tx_mcs9_max_nss,
2175 				       bw->rx_tx_mcs11_max_nss,
2176 				       bw->rx_tx_mcs13_max_nss,
2177 				       &rx_mcs[WMI_EHTCAP_TXRX_MCS_NSS_IDX_320],
2178 				       &tx_mcs[WMI_EHTCAP_TXRX_MCS_NSS_IDX_320]);
2179 		arg->peer_eht_mcs_count++;
2180 		fallthrough;
2181 	case IEEE80211_STA_RX_BW_160:
2182 		bw = &eht_cap->eht_mcs_nss_supp.bw._160;
2183 		ath12k_mac_set_eht_mcs(bw->rx_tx_mcs9_max_nss,
2184 				       bw->rx_tx_mcs9_max_nss,
2185 				       bw->rx_tx_mcs11_max_nss,
2186 				       bw->rx_tx_mcs13_max_nss,
2187 				       &rx_mcs[WMI_EHTCAP_TXRX_MCS_NSS_IDX_160],
2188 				       &tx_mcs[WMI_EHTCAP_TXRX_MCS_NSS_IDX_160]);
2189 		arg->peer_eht_mcs_count++;
2190 		fallthrough;
2191 	default:
2192 		if ((he_cap->he_cap_elem.phy_cap_info[0] &
2193 		     (IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_40MHZ_IN_2G |
2194 		      IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_40MHZ_80MHZ_IN_5G |
2195 		      IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_160MHZ_IN_5G |
2196 		      IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_80PLUS80_MHZ_IN_5G)) == 0) {
2197 			bw_20 = &eht_cap->eht_mcs_nss_supp.only_20mhz;
2198 
2199 			ath12k_mac_set_eht_mcs(bw_20->rx_tx_mcs7_max_nss,
2200 					       bw_20->rx_tx_mcs9_max_nss,
2201 					       bw_20->rx_tx_mcs11_max_nss,
2202 					       bw_20->rx_tx_mcs13_max_nss,
2203 					       &rx_mcs[WMI_EHTCAP_TXRX_MCS_NSS_IDX_80],
2204 					       &tx_mcs[WMI_EHTCAP_TXRX_MCS_NSS_IDX_80]);
2205 		} else {
2206 			bw = &eht_cap->eht_mcs_nss_supp.bw._80;
2207 			ath12k_mac_set_eht_mcs(bw->rx_tx_mcs9_max_nss,
2208 					       bw->rx_tx_mcs9_max_nss,
2209 					       bw->rx_tx_mcs11_max_nss,
2210 					       bw->rx_tx_mcs13_max_nss,
2211 					       &rx_mcs[WMI_EHTCAP_TXRX_MCS_NSS_IDX_80],
2212 					       &tx_mcs[WMI_EHTCAP_TXRX_MCS_NSS_IDX_80]);
2213 		}
2214 
2215 		arg->peer_eht_mcs_count++;
2216 		break;
2217 	}
2218 
2219 	arg->punct_bitmap = ~arvif->punct_bitmap;
2220 }
2221 
2222 static void ath12k_peer_assoc_prepare(struct ath12k *ar,
2223 				      struct ieee80211_vif *vif,
2224 				      struct ieee80211_sta *sta,
2225 				      struct ath12k_wmi_peer_assoc_arg *arg,
2226 				      bool reassoc)
2227 {
2228 	lockdep_assert_held(&ar->conf_mutex);
2229 
2230 	memset(arg, 0, sizeof(*arg));
2231 
2232 	reinit_completion(&ar->peer_assoc_done);
2233 
2234 	arg->peer_new_assoc = !reassoc;
2235 	ath12k_peer_assoc_h_basic(ar, vif, sta, arg);
2236 	ath12k_peer_assoc_h_crypto(ar, vif, sta, arg);
2237 	ath12k_peer_assoc_h_rates(ar, vif, sta, arg);
2238 	ath12k_peer_assoc_h_ht(ar, vif, sta, arg);
2239 	ath12k_peer_assoc_h_vht(ar, vif, sta, arg);
2240 	ath12k_peer_assoc_h_he(ar, vif, sta, arg);
2241 	ath12k_peer_assoc_h_eht(ar, vif, sta, arg);
2242 	ath12k_peer_assoc_h_qos(ar, vif, sta, arg);
2243 	ath12k_peer_assoc_h_phymode(ar, vif, sta, arg);
2244 	ath12k_peer_assoc_h_smps(sta, arg);
2245 
2246 	/* TODO: amsdu_disable req? */
2247 }
2248 
2249 static int ath12k_setup_peer_smps(struct ath12k *ar, struct ath12k_vif *arvif,
2250 				  const u8 *addr,
2251 				  const struct ieee80211_sta_ht_cap *ht_cap)
2252 {
2253 	int smps;
2254 
2255 	if (!ht_cap->ht_supported)
2256 		return 0;
2257 
2258 	smps = ht_cap->cap & IEEE80211_HT_CAP_SM_PS;
2259 	smps >>= IEEE80211_HT_CAP_SM_PS_SHIFT;
2260 
2261 	if (smps >= ARRAY_SIZE(ath12k_smps_map))
2262 		return -EINVAL;
2263 
2264 	return ath12k_wmi_set_peer_param(ar, addr, arvif->vdev_id,
2265 					 WMI_PEER_MIMO_PS_STATE,
2266 					 ath12k_smps_map[smps]);
2267 }
2268 
2269 static void ath12k_bss_assoc(struct ieee80211_hw *hw,
2270 			     struct ieee80211_vif *vif,
2271 			     struct ieee80211_bss_conf *bss_conf)
2272 {
2273 	struct ath12k *ar = hw->priv;
2274 	struct ath12k_vif *arvif = ath12k_vif_to_arvif(vif);
2275 	struct ath12k_wmi_peer_assoc_arg peer_arg;
2276 	struct ieee80211_sta *ap_sta;
2277 	struct ath12k_peer *peer;
2278 	bool is_auth = false;
2279 	int ret;
2280 
2281 	lockdep_assert_held(&ar->conf_mutex);
2282 
2283 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac vdev %i assoc bssid %pM aid %d\n",
2284 		   arvif->vdev_id, arvif->bssid, arvif->aid);
2285 
2286 	rcu_read_lock();
2287 
2288 	ap_sta = ieee80211_find_sta(vif, bss_conf->bssid);
2289 	if (!ap_sta) {
2290 		ath12k_warn(ar->ab, "failed to find station entry for bss %pM vdev %i\n",
2291 			    bss_conf->bssid, arvif->vdev_id);
2292 		rcu_read_unlock();
2293 		return;
2294 	}
2295 
2296 	ath12k_peer_assoc_prepare(ar, vif, ap_sta, &peer_arg, false);
2297 
2298 	rcu_read_unlock();
2299 
2300 	ret = ath12k_wmi_send_peer_assoc_cmd(ar, &peer_arg);
2301 	if (ret) {
2302 		ath12k_warn(ar->ab, "failed to run peer assoc for %pM vdev %i: %d\n",
2303 			    bss_conf->bssid, arvif->vdev_id, ret);
2304 		return;
2305 	}
2306 
2307 	if (!wait_for_completion_timeout(&ar->peer_assoc_done, 1 * HZ)) {
2308 		ath12k_warn(ar->ab, "failed to get peer assoc conf event for %pM vdev %i\n",
2309 			    bss_conf->bssid, arvif->vdev_id);
2310 		return;
2311 	}
2312 
2313 	ret = ath12k_setup_peer_smps(ar, arvif, bss_conf->bssid,
2314 				     &ap_sta->deflink.ht_cap);
2315 	if (ret) {
2316 		ath12k_warn(ar->ab, "failed to setup peer SMPS for vdev %d: %d\n",
2317 			    arvif->vdev_id, ret);
2318 		return;
2319 	}
2320 
2321 	WARN_ON(arvif->is_up);
2322 
2323 	arvif->aid = vif->cfg.aid;
2324 	ether_addr_copy(arvif->bssid, bss_conf->bssid);
2325 
2326 	ret = ath12k_wmi_vdev_up(ar, arvif->vdev_id, arvif->aid, arvif->bssid);
2327 	if (ret) {
2328 		ath12k_warn(ar->ab, "failed to set vdev %d up: %d\n",
2329 			    arvif->vdev_id, ret);
2330 		return;
2331 	}
2332 
2333 	arvif->is_up = true;
2334 
2335 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
2336 		   "mac vdev %d up (associated) bssid %pM aid %d\n",
2337 		   arvif->vdev_id, bss_conf->bssid, vif->cfg.aid);
2338 
2339 	spin_lock_bh(&ar->ab->base_lock);
2340 
2341 	peer = ath12k_peer_find(ar->ab, arvif->vdev_id, arvif->bssid);
2342 	if (peer && peer->is_authorized)
2343 		is_auth = true;
2344 
2345 	spin_unlock_bh(&ar->ab->base_lock);
2346 
2347 	/* Authorize BSS Peer */
2348 	if (is_auth) {
2349 		ret = ath12k_wmi_set_peer_param(ar, arvif->bssid,
2350 						arvif->vdev_id,
2351 						WMI_PEER_AUTHORIZE,
2352 						1);
2353 		if (ret)
2354 			ath12k_warn(ar->ab, "Unable to authorize BSS peer: %d\n", ret);
2355 	}
2356 
2357 	ret = ath12k_wmi_send_obss_spr_cmd(ar, arvif->vdev_id,
2358 					   &bss_conf->he_obss_pd);
2359 	if (ret)
2360 		ath12k_warn(ar->ab, "failed to set vdev %i OBSS PD parameters: %d\n",
2361 			    arvif->vdev_id, ret);
2362 }
2363 
2364 static void ath12k_bss_disassoc(struct ieee80211_hw *hw,
2365 				struct ieee80211_vif *vif)
2366 {
2367 	struct ath12k *ar = hw->priv;
2368 	struct ath12k_vif *arvif = ath12k_vif_to_arvif(vif);
2369 	int ret;
2370 
2371 	lockdep_assert_held(&ar->conf_mutex);
2372 
2373 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac vdev %i disassoc bssid %pM\n",
2374 		   arvif->vdev_id, arvif->bssid);
2375 
2376 	ret = ath12k_wmi_vdev_down(ar, arvif->vdev_id);
2377 	if (ret)
2378 		ath12k_warn(ar->ab, "failed to down vdev %i: %d\n",
2379 			    arvif->vdev_id, ret);
2380 
2381 	arvif->is_up = false;
2382 
2383 	/* TODO: cancel connection_loss_work */
2384 }
2385 
2386 static u32 ath12k_mac_get_rate_hw_value(int bitrate)
2387 {
2388 	u32 preamble;
2389 	u16 hw_value;
2390 	int rate;
2391 	size_t i;
2392 
2393 	if (ath12k_mac_bitrate_is_cck(bitrate))
2394 		preamble = WMI_RATE_PREAMBLE_CCK;
2395 	else
2396 		preamble = WMI_RATE_PREAMBLE_OFDM;
2397 
2398 	for (i = 0; i < ARRAY_SIZE(ath12k_legacy_rates); i++) {
2399 		if (ath12k_legacy_rates[i].bitrate != bitrate)
2400 			continue;
2401 
2402 		hw_value = ath12k_legacy_rates[i].hw_value;
2403 		rate = ATH12K_HW_RATE_CODE(hw_value, 0, preamble);
2404 
2405 		return rate;
2406 	}
2407 
2408 	return -EINVAL;
2409 }
2410 
2411 static void ath12k_recalculate_mgmt_rate(struct ath12k *ar,
2412 					 struct ieee80211_vif *vif,
2413 					 struct cfg80211_chan_def *def)
2414 {
2415 	struct ath12k_vif *arvif = ath12k_vif_to_arvif(vif);
2416 	const struct ieee80211_supported_band *sband;
2417 	u8 basic_rate_idx;
2418 	int hw_rate_code;
2419 	u32 vdev_param;
2420 	u16 bitrate;
2421 	int ret;
2422 
2423 	lockdep_assert_held(&ar->conf_mutex);
2424 
2425 	sband = ar->hw->wiphy->bands[def->chan->band];
2426 	basic_rate_idx = ffs(vif->bss_conf.basic_rates) - 1;
2427 	bitrate = sband->bitrates[basic_rate_idx].bitrate;
2428 
2429 	hw_rate_code = ath12k_mac_get_rate_hw_value(bitrate);
2430 	if (hw_rate_code < 0) {
2431 		ath12k_warn(ar->ab, "bitrate not supported %d\n", bitrate);
2432 		return;
2433 	}
2434 
2435 	vdev_param = WMI_VDEV_PARAM_MGMT_RATE;
2436 	ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id, vdev_param,
2437 					    hw_rate_code);
2438 	if (ret)
2439 		ath12k_warn(ar->ab, "failed to set mgmt tx rate %d\n", ret);
2440 
2441 	vdev_param = WMI_VDEV_PARAM_BEACON_RATE;
2442 	ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id, vdev_param,
2443 					    hw_rate_code);
2444 	if (ret)
2445 		ath12k_warn(ar->ab, "failed to set beacon tx rate %d\n", ret);
2446 }
2447 
2448 static int ath12k_mac_fils_discovery(struct ath12k_vif *arvif,
2449 				     struct ieee80211_bss_conf *info)
2450 {
2451 	struct ath12k *ar = arvif->ar;
2452 	struct sk_buff *tmpl;
2453 	int ret;
2454 	u32 interval;
2455 	bool unsol_bcast_probe_resp_enabled = false;
2456 
2457 	if (info->fils_discovery.max_interval) {
2458 		interval = info->fils_discovery.max_interval;
2459 
2460 		tmpl = ieee80211_get_fils_discovery_tmpl(ar->hw, arvif->vif);
2461 		if (tmpl)
2462 			ret = ath12k_wmi_fils_discovery_tmpl(ar, arvif->vdev_id,
2463 							     tmpl);
2464 	} else if (info->unsol_bcast_probe_resp_interval) {
2465 		unsol_bcast_probe_resp_enabled = 1;
2466 		interval = info->unsol_bcast_probe_resp_interval;
2467 
2468 		tmpl = ieee80211_get_unsol_bcast_probe_resp_tmpl(ar->hw,
2469 								 arvif->vif);
2470 		if (tmpl)
2471 			ret = ath12k_wmi_probe_resp_tmpl(ar, arvif->vdev_id,
2472 							 tmpl);
2473 	} else { /* Disable */
2474 		return ath12k_wmi_fils_discovery(ar, arvif->vdev_id, 0, false);
2475 	}
2476 
2477 	if (!tmpl) {
2478 		ath12k_warn(ar->ab,
2479 			    "mac vdev %i failed to retrieve %s template\n",
2480 			    arvif->vdev_id, (unsol_bcast_probe_resp_enabled ?
2481 			    "unsolicited broadcast probe response" :
2482 			    "FILS discovery"));
2483 		return -EPERM;
2484 	}
2485 	kfree_skb(tmpl);
2486 
2487 	if (!ret)
2488 		ret = ath12k_wmi_fils_discovery(ar, arvif->vdev_id, interval,
2489 						unsol_bcast_probe_resp_enabled);
2490 
2491 	return ret;
2492 }
2493 
2494 static void ath12k_mac_op_bss_info_changed(struct ieee80211_hw *hw,
2495 					   struct ieee80211_vif *vif,
2496 					   struct ieee80211_bss_conf *info,
2497 					   u64 changed)
2498 {
2499 	struct ath12k *ar = hw->priv;
2500 	struct ath12k_vif *arvif = ath12k_vif_to_arvif(vif);
2501 	struct cfg80211_chan_def def;
2502 	u32 param_id, param_value;
2503 	enum nl80211_band band;
2504 	u32 vdev_param;
2505 	int mcast_rate;
2506 	u32 preamble;
2507 	u16 hw_value;
2508 	u16 bitrate;
2509 	int ret;
2510 	u8 rateidx;
2511 	u32 rate;
2512 
2513 	mutex_lock(&ar->conf_mutex);
2514 
2515 	if (changed & BSS_CHANGED_BEACON_INT) {
2516 		arvif->beacon_interval = info->beacon_int;
2517 
2518 		param_id = WMI_VDEV_PARAM_BEACON_INTERVAL;
2519 		ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
2520 						    param_id,
2521 						    arvif->beacon_interval);
2522 		if (ret)
2523 			ath12k_warn(ar->ab, "Failed to set beacon interval for VDEV: %d\n",
2524 				    arvif->vdev_id);
2525 		else
2526 			ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
2527 				   "Beacon interval: %d set for VDEV: %d\n",
2528 				   arvif->beacon_interval, arvif->vdev_id);
2529 	}
2530 
2531 	if (changed & BSS_CHANGED_BEACON) {
2532 		param_id = WMI_PDEV_PARAM_BEACON_TX_MODE;
2533 		param_value = WMI_BEACON_BURST_MODE;
2534 		ret = ath12k_wmi_pdev_set_param(ar, param_id,
2535 						param_value, ar->pdev->pdev_id);
2536 		if (ret)
2537 			ath12k_warn(ar->ab, "Failed to set beacon mode for VDEV: %d\n",
2538 				    arvif->vdev_id);
2539 		else
2540 			ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
2541 				   "Set burst beacon mode for VDEV: %d\n",
2542 				   arvif->vdev_id);
2543 
2544 		ret = ath12k_mac_setup_bcn_tmpl(arvif);
2545 		if (ret)
2546 			ath12k_warn(ar->ab, "failed to update bcn template: %d\n",
2547 				    ret);
2548 	}
2549 
2550 	if (changed & (BSS_CHANGED_BEACON_INFO | BSS_CHANGED_BEACON)) {
2551 		arvif->dtim_period = info->dtim_period;
2552 
2553 		param_id = WMI_VDEV_PARAM_DTIM_PERIOD;
2554 		ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
2555 						    param_id,
2556 						    arvif->dtim_period);
2557 
2558 		if (ret)
2559 			ath12k_warn(ar->ab, "Failed to set dtim period for VDEV %d: %i\n",
2560 				    arvif->vdev_id, ret);
2561 		else
2562 			ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
2563 				   "DTIM period: %d set for VDEV: %d\n",
2564 				   arvif->dtim_period, arvif->vdev_id);
2565 	}
2566 
2567 	if (changed & BSS_CHANGED_SSID &&
2568 	    vif->type == NL80211_IFTYPE_AP) {
2569 		arvif->u.ap.ssid_len = vif->cfg.ssid_len;
2570 		if (vif->cfg.ssid_len)
2571 			memcpy(arvif->u.ap.ssid, vif->cfg.ssid, vif->cfg.ssid_len);
2572 		arvif->u.ap.hidden_ssid = info->hidden_ssid;
2573 	}
2574 
2575 	if (changed & BSS_CHANGED_BSSID && !is_zero_ether_addr(info->bssid))
2576 		ether_addr_copy(arvif->bssid, info->bssid);
2577 
2578 	if (changed & BSS_CHANGED_BEACON_ENABLED) {
2579 		ath12k_control_beaconing(arvif, info);
2580 
2581 		if (arvif->is_up && vif->bss_conf.he_support &&
2582 		    vif->bss_conf.he_oper.params) {
2583 			/* TODO: Extend to support 1024 BA Bitmap size */
2584 			ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
2585 							    WMI_VDEV_PARAM_BA_MODE,
2586 							    WMI_BA_MODE_BUFFER_SIZE_256);
2587 			if (ret)
2588 				ath12k_warn(ar->ab,
2589 					    "failed to set BA BUFFER SIZE 256 for vdev: %d\n",
2590 					    arvif->vdev_id);
2591 
2592 			param_id = WMI_VDEV_PARAM_HEOPS_0_31;
2593 			param_value = vif->bss_conf.he_oper.params;
2594 			ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
2595 							    param_id, param_value);
2596 			ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
2597 				   "he oper param: %x set for VDEV: %d\n",
2598 				   param_value, arvif->vdev_id);
2599 
2600 			if (ret)
2601 				ath12k_warn(ar->ab, "Failed to set he oper params %x for VDEV %d: %i\n",
2602 					    param_value, arvif->vdev_id, ret);
2603 		}
2604 	}
2605 
2606 	if (changed & BSS_CHANGED_ERP_CTS_PROT) {
2607 		u32 cts_prot;
2608 
2609 		cts_prot = !!(info->use_cts_prot);
2610 		param_id = WMI_VDEV_PARAM_PROTECTION_MODE;
2611 
2612 		if (arvif->is_started) {
2613 			ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
2614 							    param_id, cts_prot);
2615 			if (ret)
2616 				ath12k_warn(ar->ab, "Failed to set CTS prot for VDEV: %d\n",
2617 					    arvif->vdev_id);
2618 			else
2619 				ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "Set CTS prot: %d for VDEV: %d\n",
2620 					   cts_prot, arvif->vdev_id);
2621 		} else {
2622 			ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "defer protection mode setup, vdev is not ready yet\n");
2623 		}
2624 	}
2625 
2626 	if (changed & BSS_CHANGED_ERP_SLOT) {
2627 		u32 slottime;
2628 
2629 		if (info->use_short_slot)
2630 			slottime = WMI_VDEV_SLOT_TIME_SHORT; /* 9us */
2631 
2632 		else
2633 			slottime = WMI_VDEV_SLOT_TIME_LONG; /* 20us */
2634 
2635 		param_id = WMI_VDEV_PARAM_SLOT_TIME;
2636 		ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
2637 						    param_id, slottime);
2638 		if (ret)
2639 			ath12k_warn(ar->ab, "Failed to set erp slot for VDEV: %d\n",
2640 				    arvif->vdev_id);
2641 		else
2642 			ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
2643 				   "Set slottime: %d for VDEV: %d\n",
2644 				   slottime, arvif->vdev_id);
2645 	}
2646 
2647 	if (changed & BSS_CHANGED_ERP_PREAMBLE) {
2648 		u32 preamble;
2649 
2650 		if (info->use_short_preamble)
2651 			preamble = WMI_VDEV_PREAMBLE_SHORT;
2652 		else
2653 			preamble = WMI_VDEV_PREAMBLE_LONG;
2654 
2655 		param_id = WMI_VDEV_PARAM_PREAMBLE;
2656 		ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
2657 						    param_id, preamble);
2658 		if (ret)
2659 			ath12k_warn(ar->ab, "Failed to set preamble for VDEV: %d\n",
2660 				    arvif->vdev_id);
2661 		else
2662 			ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
2663 				   "Set preamble: %d for VDEV: %d\n",
2664 				   preamble, arvif->vdev_id);
2665 	}
2666 
2667 	if (changed & BSS_CHANGED_ASSOC) {
2668 		if (vif->cfg.assoc)
2669 			ath12k_bss_assoc(hw, vif, info);
2670 		else
2671 			ath12k_bss_disassoc(hw, vif);
2672 	}
2673 
2674 	if (changed & BSS_CHANGED_TXPOWER) {
2675 		ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac vdev_id %i txpower %d\n",
2676 			   arvif->vdev_id, info->txpower);
2677 
2678 		arvif->txpower = info->txpower;
2679 		ath12k_mac_txpower_recalc(ar);
2680 	}
2681 
2682 	if (changed & BSS_CHANGED_MCAST_RATE &&
2683 	    !ath12k_mac_vif_chan(arvif->vif, &def)) {
2684 		band = def.chan->band;
2685 		mcast_rate = vif->bss_conf.mcast_rate[band];
2686 
2687 		if (mcast_rate > 0)
2688 			rateidx = mcast_rate - 1;
2689 		else
2690 			rateidx = ffs(vif->bss_conf.basic_rates) - 1;
2691 
2692 		if (ar->pdev->cap.supported_bands & WMI_HOST_WLAN_5G_CAP)
2693 			rateidx += ATH12K_MAC_FIRST_OFDM_RATE_IDX;
2694 
2695 		bitrate = ath12k_legacy_rates[rateidx].bitrate;
2696 		hw_value = ath12k_legacy_rates[rateidx].hw_value;
2697 
2698 		if (ath12k_mac_bitrate_is_cck(bitrate))
2699 			preamble = WMI_RATE_PREAMBLE_CCK;
2700 		else
2701 			preamble = WMI_RATE_PREAMBLE_OFDM;
2702 
2703 		rate = ATH12K_HW_RATE_CODE(hw_value, 0, preamble);
2704 
2705 		ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
2706 			   "mac vdev %d mcast_rate %x\n",
2707 			   arvif->vdev_id, rate);
2708 
2709 		vdev_param = WMI_VDEV_PARAM_MCAST_DATA_RATE;
2710 		ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
2711 						    vdev_param, rate);
2712 		if (ret)
2713 			ath12k_warn(ar->ab,
2714 				    "failed to set mcast rate on vdev %i: %d\n",
2715 				    arvif->vdev_id,  ret);
2716 
2717 		vdev_param = WMI_VDEV_PARAM_BCAST_DATA_RATE;
2718 		ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
2719 						    vdev_param, rate);
2720 		if (ret)
2721 			ath12k_warn(ar->ab,
2722 				    "failed to set bcast rate on vdev %i: %d\n",
2723 				    arvif->vdev_id,  ret);
2724 	}
2725 
2726 	if (changed & BSS_CHANGED_BASIC_RATES &&
2727 	    !ath12k_mac_vif_chan(arvif->vif, &def))
2728 		ath12k_recalculate_mgmt_rate(ar, vif, &def);
2729 
2730 	if (changed & BSS_CHANGED_TWT) {
2731 		if (info->twt_requester || info->twt_responder)
2732 			ath12k_wmi_send_twt_enable_cmd(ar, ar->pdev->pdev_id);
2733 		else
2734 			ath12k_wmi_send_twt_disable_cmd(ar, ar->pdev->pdev_id);
2735 	}
2736 
2737 	if (changed & BSS_CHANGED_HE_OBSS_PD)
2738 		ath12k_wmi_send_obss_spr_cmd(ar, arvif->vdev_id,
2739 					     &info->he_obss_pd);
2740 
2741 	if (changed & BSS_CHANGED_HE_BSS_COLOR) {
2742 		if (vif->type == NL80211_IFTYPE_AP) {
2743 			ret = ath12k_wmi_obss_color_cfg_cmd(ar,
2744 							    arvif->vdev_id,
2745 							    info->he_bss_color.color,
2746 							    ATH12K_BSS_COLOR_AP_PERIODS,
2747 							    info->he_bss_color.enabled);
2748 			if (ret)
2749 				ath12k_warn(ar->ab, "failed to set bss color collision on vdev %i: %d\n",
2750 					    arvif->vdev_id,  ret);
2751 		} else if (vif->type == NL80211_IFTYPE_STATION) {
2752 			ret = ath12k_wmi_send_bss_color_change_enable_cmd(ar,
2753 									  arvif->vdev_id,
2754 									  1);
2755 			if (ret)
2756 				ath12k_warn(ar->ab, "failed to enable bss color change on vdev %i: %d\n",
2757 					    arvif->vdev_id,  ret);
2758 			ret = ath12k_wmi_obss_color_cfg_cmd(ar,
2759 							    arvif->vdev_id,
2760 							    0,
2761 							    ATH12K_BSS_COLOR_STA_PERIODS,
2762 							    1);
2763 			if (ret)
2764 				ath12k_warn(ar->ab, "failed to set bss color collision on vdev %i: %d\n",
2765 					    arvif->vdev_id,  ret);
2766 		}
2767 	}
2768 
2769 	ath12k_mac_fils_discovery(arvif, info);
2770 
2771 	if (changed & BSS_CHANGED_EHT_PUNCTURING)
2772 		arvif->punct_bitmap = info->eht_puncturing;
2773 
2774 	mutex_unlock(&ar->conf_mutex);
2775 }
2776 
2777 void __ath12k_mac_scan_finish(struct ath12k *ar)
2778 {
2779 	lockdep_assert_held(&ar->data_lock);
2780 
2781 	switch (ar->scan.state) {
2782 	case ATH12K_SCAN_IDLE:
2783 		break;
2784 	case ATH12K_SCAN_RUNNING:
2785 	case ATH12K_SCAN_ABORTING:
2786 		if (ar->scan.is_roc && ar->scan.roc_notify)
2787 			ieee80211_remain_on_channel_expired(ar->hw);
2788 		fallthrough;
2789 	case ATH12K_SCAN_STARTING:
2790 		if (!ar->scan.is_roc) {
2791 			struct cfg80211_scan_info info = {
2792 				.aborted = ((ar->scan.state ==
2793 					    ATH12K_SCAN_ABORTING) ||
2794 					    (ar->scan.state ==
2795 					    ATH12K_SCAN_STARTING)),
2796 			};
2797 
2798 			ieee80211_scan_completed(ar->hw, &info);
2799 		}
2800 
2801 		ar->scan.state = ATH12K_SCAN_IDLE;
2802 		ar->scan_channel = NULL;
2803 		ar->scan.roc_freq = 0;
2804 		cancel_delayed_work(&ar->scan.timeout);
2805 		complete(&ar->scan.completed);
2806 		break;
2807 	}
2808 }
2809 
2810 void ath12k_mac_scan_finish(struct ath12k *ar)
2811 {
2812 	spin_lock_bh(&ar->data_lock);
2813 	__ath12k_mac_scan_finish(ar);
2814 	spin_unlock_bh(&ar->data_lock);
2815 }
2816 
2817 static int ath12k_scan_stop(struct ath12k *ar)
2818 {
2819 	struct ath12k_wmi_scan_cancel_arg arg = {
2820 		.req_type = WLAN_SCAN_CANCEL_SINGLE,
2821 		.scan_id = ATH12K_SCAN_ID,
2822 	};
2823 	int ret;
2824 
2825 	lockdep_assert_held(&ar->conf_mutex);
2826 
2827 	/* TODO: Fill other STOP Params */
2828 	arg.pdev_id = ar->pdev->pdev_id;
2829 
2830 	ret = ath12k_wmi_send_scan_stop_cmd(ar, &arg);
2831 	if (ret) {
2832 		ath12k_warn(ar->ab, "failed to stop wmi scan: %d\n", ret);
2833 		goto out;
2834 	}
2835 
2836 	ret = wait_for_completion_timeout(&ar->scan.completed, 3 * HZ);
2837 	if (ret == 0) {
2838 		ath12k_warn(ar->ab,
2839 			    "failed to receive scan abort comple: timed out\n");
2840 		ret = -ETIMEDOUT;
2841 	} else if (ret > 0) {
2842 		ret = 0;
2843 	}
2844 
2845 out:
2846 	/* Scan state should be updated upon scan completion but in case
2847 	 * firmware fails to deliver the event (for whatever reason) it is
2848 	 * desired to clean up scan state anyway. Firmware may have just
2849 	 * dropped the scan completion event delivery due to transport pipe
2850 	 * being overflown with data and/or it can recover on its own before
2851 	 * next scan request is submitted.
2852 	 */
2853 	spin_lock_bh(&ar->data_lock);
2854 	if (ar->scan.state != ATH12K_SCAN_IDLE)
2855 		__ath12k_mac_scan_finish(ar);
2856 	spin_unlock_bh(&ar->data_lock);
2857 
2858 	return ret;
2859 }
2860 
2861 static void ath12k_scan_abort(struct ath12k *ar)
2862 {
2863 	int ret;
2864 
2865 	lockdep_assert_held(&ar->conf_mutex);
2866 
2867 	spin_lock_bh(&ar->data_lock);
2868 
2869 	switch (ar->scan.state) {
2870 	case ATH12K_SCAN_IDLE:
2871 		/* This can happen if timeout worker kicked in and called
2872 		 * abortion while scan completion was being processed.
2873 		 */
2874 		break;
2875 	case ATH12K_SCAN_STARTING:
2876 	case ATH12K_SCAN_ABORTING:
2877 		ath12k_warn(ar->ab, "refusing scan abortion due to invalid scan state: %d\n",
2878 			    ar->scan.state);
2879 		break;
2880 	case ATH12K_SCAN_RUNNING:
2881 		ar->scan.state = ATH12K_SCAN_ABORTING;
2882 		spin_unlock_bh(&ar->data_lock);
2883 
2884 		ret = ath12k_scan_stop(ar);
2885 		if (ret)
2886 			ath12k_warn(ar->ab, "failed to abort scan: %d\n", ret);
2887 
2888 		spin_lock_bh(&ar->data_lock);
2889 		break;
2890 	}
2891 
2892 	spin_unlock_bh(&ar->data_lock);
2893 }
2894 
2895 static void ath12k_scan_timeout_work(struct work_struct *work)
2896 {
2897 	struct ath12k *ar = container_of(work, struct ath12k,
2898 					 scan.timeout.work);
2899 
2900 	mutex_lock(&ar->conf_mutex);
2901 	ath12k_scan_abort(ar);
2902 	mutex_unlock(&ar->conf_mutex);
2903 }
2904 
2905 static int ath12k_start_scan(struct ath12k *ar,
2906 			     struct ath12k_wmi_scan_req_arg *arg)
2907 {
2908 	int ret;
2909 
2910 	lockdep_assert_held(&ar->conf_mutex);
2911 
2912 	ret = ath12k_wmi_send_scan_start_cmd(ar, arg);
2913 	if (ret)
2914 		return ret;
2915 
2916 	ret = wait_for_completion_timeout(&ar->scan.started, 1 * HZ);
2917 	if (ret == 0) {
2918 		ret = ath12k_scan_stop(ar);
2919 		if (ret)
2920 			ath12k_warn(ar->ab, "failed to stop scan: %d\n", ret);
2921 
2922 		return -ETIMEDOUT;
2923 	}
2924 
2925 	/* If we failed to start the scan, return error code at
2926 	 * this point.  This is probably due to some issue in the
2927 	 * firmware, but no need to wedge the driver due to that...
2928 	 */
2929 	spin_lock_bh(&ar->data_lock);
2930 	if (ar->scan.state == ATH12K_SCAN_IDLE) {
2931 		spin_unlock_bh(&ar->data_lock);
2932 		return -EINVAL;
2933 	}
2934 	spin_unlock_bh(&ar->data_lock);
2935 
2936 	return 0;
2937 }
2938 
2939 static int ath12k_mac_op_hw_scan(struct ieee80211_hw *hw,
2940 				 struct ieee80211_vif *vif,
2941 				 struct ieee80211_scan_request *hw_req)
2942 {
2943 	struct ath12k *ar = hw->priv;
2944 	struct ath12k_vif *arvif = ath12k_vif_to_arvif(vif);
2945 	struct cfg80211_scan_request *req = &hw_req->req;
2946 	struct ath12k_wmi_scan_req_arg arg = {};
2947 	int ret;
2948 	int i;
2949 
2950 	mutex_lock(&ar->conf_mutex);
2951 
2952 	spin_lock_bh(&ar->data_lock);
2953 	switch (ar->scan.state) {
2954 	case ATH12K_SCAN_IDLE:
2955 		reinit_completion(&ar->scan.started);
2956 		reinit_completion(&ar->scan.completed);
2957 		ar->scan.state = ATH12K_SCAN_STARTING;
2958 		ar->scan.is_roc = false;
2959 		ar->scan.vdev_id = arvif->vdev_id;
2960 		ret = 0;
2961 		break;
2962 	case ATH12K_SCAN_STARTING:
2963 	case ATH12K_SCAN_RUNNING:
2964 	case ATH12K_SCAN_ABORTING:
2965 		ret = -EBUSY;
2966 		break;
2967 	}
2968 	spin_unlock_bh(&ar->data_lock);
2969 
2970 	if (ret)
2971 		goto exit;
2972 
2973 	ath12k_wmi_start_scan_init(ar, &arg);
2974 	arg.vdev_id = arvif->vdev_id;
2975 	arg.scan_id = ATH12K_SCAN_ID;
2976 
2977 	if (req->ie_len) {
2978 		arg.extraie.ptr = kmemdup(req->ie, req->ie_len, GFP_KERNEL);
2979 		if (!arg.extraie.ptr) {
2980 			ret = -ENOMEM;
2981 			goto exit;
2982 		}
2983 		arg.extraie.len = req->ie_len;
2984 	}
2985 
2986 	if (req->n_ssids) {
2987 		arg.num_ssids = req->n_ssids;
2988 		for (i = 0; i < arg.num_ssids; i++)
2989 			arg.ssid[i] = req->ssids[i];
2990 	} else {
2991 		arg.scan_flags |= WMI_SCAN_FLAG_PASSIVE;
2992 	}
2993 
2994 	if (req->n_channels) {
2995 		arg.num_chan = req->n_channels;
2996 		arg.chan_list = kcalloc(arg.num_chan, sizeof(*arg.chan_list),
2997 					GFP_KERNEL);
2998 
2999 		if (!arg.chan_list) {
3000 			ret = -ENOMEM;
3001 			goto exit;
3002 		}
3003 
3004 		for (i = 0; i < arg.num_chan; i++)
3005 			arg.chan_list[i] = req->channels[i]->center_freq;
3006 	}
3007 
3008 	ret = ath12k_start_scan(ar, &arg);
3009 	if (ret) {
3010 		ath12k_warn(ar->ab, "failed to start hw scan: %d\n", ret);
3011 		spin_lock_bh(&ar->data_lock);
3012 		ar->scan.state = ATH12K_SCAN_IDLE;
3013 		spin_unlock_bh(&ar->data_lock);
3014 	}
3015 
3016 	/* Add a margin to account for event/command processing */
3017 	ieee80211_queue_delayed_work(ar->hw, &ar->scan.timeout,
3018 				     msecs_to_jiffies(arg.max_scan_time +
3019 						      ATH12K_MAC_SCAN_TIMEOUT_MSECS));
3020 
3021 exit:
3022 	kfree(arg.chan_list);
3023 
3024 	if (req->ie_len)
3025 		kfree(arg.extraie.ptr);
3026 
3027 	mutex_unlock(&ar->conf_mutex);
3028 	return ret;
3029 }
3030 
3031 static void ath12k_mac_op_cancel_hw_scan(struct ieee80211_hw *hw,
3032 					 struct ieee80211_vif *vif)
3033 {
3034 	struct ath12k *ar = hw->priv;
3035 
3036 	mutex_lock(&ar->conf_mutex);
3037 	ath12k_scan_abort(ar);
3038 	mutex_unlock(&ar->conf_mutex);
3039 
3040 	cancel_delayed_work_sync(&ar->scan.timeout);
3041 }
3042 
3043 static int ath12k_install_key(struct ath12k_vif *arvif,
3044 			      struct ieee80211_key_conf *key,
3045 			      enum set_key_cmd cmd,
3046 			      const u8 *macaddr, u32 flags)
3047 {
3048 	int ret;
3049 	struct ath12k *ar = arvif->ar;
3050 	struct wmi_vdev_install_key_arg arg = {
3051 		.vdev_id = arvif->vdev_id,
3052 		.key_idx = key->keyidx,
3053 		.key_len = key->keylen,
3054 		.key_data = key->key,
3055 		.key_flags = flags,
3056 		.macaddr = macaddr,
3057 	};
3058 
3059 	lockdep_assert_held(&arvif->ar->conf_mutex);
3060 
3061 	reinit_completion(&ar->install_key_done);
3062 
3063 	if (test_bit(ATH12K_FLAG_HW_CRYPTO_DISABLED, &ar->ab->dev_flags))
3064 		return 0;
3065 
3066 	if (cmd == DISABLE_KEY) {
3067 		/* TODO: Check if FW expects  value other than NONE for del */
3068 		/* arg.key_cipher = WMI_CIPHER_NONE; */
3069 		arg.key_len = 0;
3070 		arg.key_data = NULL;
3071 		goto install;
3072 	}
3073 
3074 	switch (key->cipher) {
3075 	case WLAN_CIPHER_SUITE_CCMP:
3076 		arg.key_cipher = WMI_CIPHER_AES_CCM;
3077 		/* TODO: Re-check if flag is valid */
3078 		key->flags |= IEEE80211_KEY_FLAG_GENERATE_IV_MGMT;
3079 		break;
3080 	case WLAN_CIPHER_SUITE_TKIP:
3081 		arg.key_cipher = WMI_CIPHER_TKIP;
3082 		arg.key_txmic_len = 8;
3083 		arg.key_rxmic_len = 8;
3084 		break;
3085 	case WLAN_CIPHER_SUITE_CCMP_256:
3086 		arg.key_cipher = WMI_CIPHER_AES_CCM;
3087 		break;
3088 	case WLAN_CIPHER_SUITE_GCMP:
3089 	case WLAN_CIPHER_SUITE_GCMP_256:
3090 		arg.key_cipher = WMI_CIPHER_AES_GCM;
3091 		break;
3092 	default:
3093 		ath12k_warn(ar->ab, "cipher %d is not supported\n", key->cipher);
3094 		return -EOPNOTSUPP;
3095 	}
3096 
3097 	if (test_bit(ATH12K_FLAG_RAW_MODE, &ar->ab->dev_flags))
3098 		key->flags |= IEEE80211_KEY_FLAG_GENERATE_IV |
3099 			      IEEE80211_KEY_FLAG_RESERVE_TAILROOM;
3100 
3101 install:
3102 	ret = ath12k_wmi_vdev_install_key(arvif->ar, &arg);
3103 
3104 	if (ret)
3105 		return ret;
3106 
3107 	if (!wait_for_completion_timeout(&ar->install_key_done, 1 * HZ))
3108 		return -ETIMEDOUT;
3109 
3110 	if (ether_addr_equal(macaddr, arvif->vif->addr))
3111 		arvif->key_cipher = key->cipher;
3112 
3113 	return ar->install_key_status ? -EINVAL : 0;
3114 }
3115 
3116 static int ath12k_clear_peer_keys(struct ath12k_vif *arvif,
3117 				  const u8 *addr)
3118 {
3119 	struct ath12k *ar = arvif->ar;
3120 	struct ath12k_base *ab = ar->ab;
3121 	struct ath12k_peer *peer;
3122 	int first_errno = 0;
3123 	int ret;
3124 	int i;
3125 	u32 flags = 0;
3126 
3127 	lockdep_assert_held(&ar->conf_mutex);
3128 
3129 	spin_lock_bh(&ab->base_lock);
3130 	peer = ath12k_peer_find(ab, arvif->vdev_id, addr);
3131 	spin_unlock_bh(&ab->base_lock);
3132 
3133 	if (!peer)
3134 		return -ENOENT;
3135 
3136 	for (i = 0; i < ARRAY_SIZE(peer->keys); i++) {
3137 		if (!peer->keys[i])
3138 			continue;
3139 
3140 		/* key flags are not required to delete the key */
3141 		ret = ath12k_install_key(arvif, peer->keys[i],
3142 					 DISABLE_KEY, addr, flags);
3143 		if (ret < 0 && first_errno == 0)
3144 			first_errno = ret;
3145 
3146 		if (ret < 0)
3147 			ath12k_warn(ab, "failed to remove peer key %d: %d\n",
3148 				    i, ret);
3149 
3150 		spin_lock_bh(&ab->base_lock);
3151 		peer->keys[i] = NULL;
3152 		spin_unlock_bh(&ab->base_lock);
3153 	}
3154 
3155 	return first_errno;
3156 }
3157 
3158 static int ath12k_mac_op_set_key(struct ieee80211_hw *hw, enum set_key_cmd cmd,
3159 				 struct ieee80211_vif *vif, struct ieee80211_sta *sta,
3160 				 struct ieee80211_key_conf *key)
3161 {
3162 	struct ath12k *ar = hw->priv;
3163 	struct ath12k_base *ab = ar->ab;
3164 	struct ath12k_vif *arvif = ath12k_vif_to_arvif(vif);
3165 	struct ath12k_peer *peer;
3166 	struct ath12k_sta *arsta;
3167 	const u8 *peer_addr;
3168 	int ret = 0;
3169 	u32 flags = 0;
3170 
3171 	/* BIP needs to be done in software */
3172 	if (key->cipher == WLAN_CIPHER_SUITE_AES_CMAC ||
3173 	    key->cipher == WLAN_CIPHER_SUITE_BIP_GMAC_128 ||
3174 	    key->cipher == WLAN_CIPHER_SUITE_BIP_GMAC_256 ||
3175 	    key->cipher == WLAN_CIPHER_SUITE_BIP_CMAC_256)
3176 		return 1;
3177 
3178 	if (test_bit(ATH12K_FLAG_HW_CRYPTO_DISABLED, &ar->ab->dev_flags))
3179 		return 1;
3180 
3181 	if (key->keyidx > WMI_MAX_KEY_INDEX)
3182 		return -ENOSPC;
3183 
3184 	mutex_lock(&ar->conf_mutex);
3185 
3186 	if (sta)
3187 		peer_addr = sta->addr;
3188 	else if (arvif->vdev_type == WMI_VDEV_TYPE_STA)
3189 		peer_addr = vif->bss_conf.bssid;
3190 	else
3191 		peer_addr = vif->addr;
3192 
3193 	key->hw_key_idx = key->keyidx;
3194 
3195 	/* the peer should not disappear in mid-way (unless FW goes awry) since
3196 	 * we already hold conf_mutex. we just make sure its there now.
3197 	 */
3198 	spin_lock_bh(&ab->base_lock);
3199 	peer = ath12k_peer_find(ab, arvif->vdev_id, peer_addr);
3200 	spin_unlock_bh(&ab->base_lock);
3201 
3202 	if (!peer) {
3203 		if (cmd == SET_KEY) {
3204 			ath12k_warn(ab, "cannot install key for non-existent peer %pM\n",
3205 				    peer_addr);
3206 			ret = -EOPNOTSUPP;
3207 			goto exit;
3208 		} else {
3209 			/* if the peer doesn't exist there is no key to disable
3210 			 * anymore
3211 			 */
3212 			goto exit;
3213 		}
3214 	}
3215 
3216 	if (key->flags & IEEE80211_KEY_FLAG_PAIRWISE)
3217 		flags |= WMI_KEY_PAIRWISE;
3218 	else
3219 		flags |= WMI_KEY_GROUP;
3220 
3221 	ret = ath12k_install_key(arvif, key, cmd, peer_addr, flags);
3222 	if (ret) {
3223 		ath12k_warn(ab, "ath12k_install_key failed (%d)\n", ret);
3224 		goto exit;
3225 	}
3226 
3227 	ret = ath12k_dp_rx_peer_pn_replay_config(arvif, peer_addr, cmd, key);
3228 	if (ret) {
3229 		ath12k_warn(ab, "failed to offload PN replay detection %d\n", ret);
3230 		goto exit;
3231 	}
3232 
3233 	spin_lock_bh(&ab->base_lock);
3234 	peer = ath12k_peer_find(ab, arvif->vdev_id, peer_addr);
3235 	if (peer && cmd == SET_KEY) {
3236 		peer->keys[key->keyidx] = key;
3237 		if (key->flags & IEEE80211_KEY_FLAG_PAIRWISE) {
3238 			peer->ucast_keyidx = key->keyidx;
3239 			peer->sec_type = ath12k_dp_tx_get_encrypt_type(key->cipher);
3240 		} else {
3241 			peer->mcast_keyidx = key->keyidx;
3242 			peer->sec_type_grp = ath12k_dp_tx_get_encrypt_type(key->cipher);
3243 		}
3244 	} else if (peer && cmd == DISABLE_KEY) {
3245 		peer->keys[key->keyidx] = NULL;
3246 		if (key->flags & IEEE80211_KEY_FLAG_PAIRWISE)
3247 			peer->ucast_keyidx = 0;
3248 		else
3249 			peer->mcast_keyidx = 0;
3250 	} else if (!peer)
3251 		/* impossible unless FW goes crazy */
3252 		ath12k_warn(ab, "peer %pM disappeared!\n", peer_addr);
3253 
3254 	if (sta) {
3255 		arsta = ath12k_sta_to_arsta(sta);
3256 
3257 		switch (key->cipher) {
3258 		case WLAN_CIPHER_SUITE_TKIP:
3259 		case WLAN_CIPHER_SUITE_CCMP:
3260 		case WLAN_CIPHER_SUITE_CCMP_256:
3261 		case WLAN_CIPHER_SUITE_GCMP:
3262 		case WLAN_CIPHER_SUITE_GCMP_256:
3263 			if (cmd == SET_KEY)
3264 				arsta->pn_type = HAL_PN_TYPE_WPA;
3265 			else
3266 				arsta->pn_type = HAL_PN_TYPE_NONE;
3267 			break;
3268 		default:
3269 			arsta->pn_type = HAL_PN_TYPE_NONE;
3270 			break;
3271 		}
3272 	}
3273 
3274 	spin_unlock_bh(&ab->base_lock);
3275 
3276 exit:
3277 	mutex_unlock(&ar->conf_mutex);
3278 	return ret;
3279 }
3280 
3281 static int
3282 ath12k_mac_bitrate_mask_num_vht_rates(struct ath12k *ar,
3283 				      enum nl80211_band band,
3284 				      const struct cfg80211_bitrate_mask *mask)
3285 {
3286 	int num_rates = 0;
3287 	int i;
3288 
3289 	for (i = 0; i < ARRAY_SIZE(mask->control[band].vht_mcs); i++)
3290 		num_rates += hweight16(mask->control[band].vht_mcs[i]);
3291 
3292 	return num_rates;
3293 }
3294 
3295 static int
3296 ath12k_mac_set_peer_vht_fixed_rate(struct ath12k_vif *arvif,
3297 				   struct ieee80211_sta *sta,
3298 				   const struct cfg80211_bitrate_mask *mask,
3299 				   enum nl80211_band band)
3300 {
3301 	struct ath12k *ar = arvif->ar;
3302 	u8 vht_rate, nss;
3303 	u32 rate_code;
3304 	int ret, i;
3305 
3306 	lockdep_assert_held(&ar->conf_mutex);
3307 
3308 	nss = 0;
3309 
3310 	for (i = 0; i < ARRAY_SIZE(mask->control[band].vht_mcs); i++) {
3311 		if (hweight16(mask->control[band].vht_mcs[i]) == 1) {
3312 			nss = i + 1;
3313 			vht_rate = ffs(mask->control[band].vht_mcs[i]) - 1;
3314 		}
3315 	}
3316 
3317 	if (!nss) {
3318 		ath12k_warn(ar->ab, "No single VHT Fixed rate found to set for %pM",
3319 			    sta->addr);
3320 		return -EINVAL;
3321 	}
3322 
3323 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
3324 		   "Setting Fixed VHT Rate for peer %pM. Device will not switch to any other selected rates",
3325 		   sta->addr);
3326 
3327 	rate_code = ATH12K_HW_RATE_CODE(vht_rate, nss - 1,
3328 					WMI_RATE_PREAMBLE_VHT);
3329 	ret = ath12k_wmi_set_peer_param(ar, sta->addr,
3330 					arvif->vdev_id,
3331 					WMI_PEER_PARAM_FIXED_RATE,
3332 					rate_code);
3333 	if (ret)
3334 		ath12k_warn(ar->ab,
3335 			    "failed to update STA %pM Fixed Rate %d: %d\n",
3336 			     sta->addr, rate_code, ret);
3337 
3338 	return ret;
3339 }
3340 
3341 static int ath12k_station_assoc(struct ath12k *ar,
3342 				struct ieee80211_vif *vif,
3343 				struct ieee80211_sta *sta,
3344 				bool reassoc)
3345 {
3346 	struct ath12k_vif *arvif = ath12k_vif_to_arvif(vif);
3347 	struct ath12k_wmi_peer_assoc_arg peer_arg;
3348 	int ret;
3349 	struct cfg80211_chan_def def;
3350 	enum nl80211_band band;
3351 	struct cfg80211_bitrate_mask *mask;
3352 	u8 num_vht_rates;
3353 
3354 	lockdep_assert_held(&ar->conf_mutex);
3355 
3356 	if (WARN_ON(ath12k_mac_vif_chan(vif, &def)))
3357 		return -EPERM;
3358 
3359 	band = def.chan->band;
3360 	mask = &arvif->bitrate_mask;
3361 
3362 	ath12k_peer_assoc_prepare(ar, vif, sta, &peer_arg, reassoc);
3363 
3364 	ret = ath12k_wmi_send_peer_assoc_cmd(ar, &peer_arg);
3365 	if (ret) {
3366 		ath12k_warn(ar->ab, "failed to run peer assoc for STA %pM vdev %i: %d\n",
3367 			    sta->addr, arvif->vdev_id, ret);
3368 		return ret;
3369 	}
3370 
3371 	if (!wait_for_completion_timeout(&ar->peer_assoc_done, 1 * HZ)) {
3372 		ath12k_warn(ar->ab, "failed to get peer assoc conf event for %pM vdev %i\n",
3373 			    sta->addr, arvif->vdev_id);
3374 		return -ETIMEDOUT;
3375 	}
3376 
3377 	num_vht_rates = ath12k_mac_bitrate_mask_num_vht_rates(ar, band, mask);
3378 
3379 	/* If single VHT rate is configured (by set_bitrate_mask()),
3380 	 * peer_assoc will disable VHT. This is now enabled by a peer specific
3381 	 * fixed param.
3382 	 * Note that all other rates and NSS will be disabled for this peer.
3383 	 */
3384 	if (sta->deflink.vht_cap.vht_supported && num_vht_rates == 1) {
3385 		ret = ath12k_mac_set_peer_vht_fixed_rate(arvif, sta, mask,
3386 							 band);
3387 		if (ret)
3388 			return ret;
3389 	}
3390 
3391 	/* Re-assoc is run only to update supported rates for given station. It
3392 	 * doesn't make much sense to reconfigure the peer completely.
3393 	 */
3394 	if (reassoc)
3395 		return 0;
3396 
3397 	ret = ath12k_setup_peer_smps(ar, arvif, sta->addr,
3398 				     &sta->deflink.ht_cap);
3399 	if (ret) {
3400 		ath12k_warn(ar->ab, "failed to setup peer SMPS for vdev %d: %d\n",
3401 			    arvif->vdev_id, ret);
3402 		return ret;
3403 	}
3404 
3405 	if (!sta->wme) {
3406 		arvif->num_legacy_stations++;
3407 		ret = ath12k_recalc_rtscts_prot(arvif);
3408 		if (ret)
3409 			return ret;
3410 	}
3411 
3412 	if (sta->wme && sta->uapsd_queues) {
3413 		ret = ath12k_peer_assoc_qos_ap(ar, arvif, sta);
3414 		if (ret) {
3415 			ath12k_warn(ar->ab, "failed to set qos params for STA %pM for vdev %i: %d\n",
3416 				    sta->addr, arvif->vdev_id, ret);
3417 			return ret;
3418 		}
3419 	}
3420 
3421 	return 0;
3422 }
3423 
3424 static int ath12k_station_disassoc(struct ath12k *ar,
3425 				   struct ieee80211_vif *vif,
3426 				   struct ieee80211_sta *sta)
3427 {
3428 	struct ath12k_vif *arvif = ath12k_vif_to_arvif(vif);
3429 	int ret;
3430 
3431 	lockdep_assert_held(&ar->conf_mutex);
3432 
3433 	if (!sta->wme) {
3434 		arvif->num_legacy_stations--;
3435 		ret = ath12k_recalc_rtscts_prot(arvif);
3436 		if (ret)
3437 			return ret;
3438 	}
3439 
3440 	ret = ath12k_clear_peer_keys(arvif, sta->addr);
3441 	if (ret) {
3442 		ath12k_warn(ar->ab, "failed to clear all peer keys for vdev %i: %d\n",
3443 			    arvif->vdev_id, ret);
3444 		return ret;
3445 	}
3446 	return 0;
3447 }
3448 
3449 static void ath12k_sta_rc_update_wk(struct work_struct *wk)
3450 {
3451 	struct ath12k *ar;
3452 	struct ath12k_vif *arvif;
3453 	struct ath12k_sta *arsta;
3454 	struct ieee80211_sta *sta;
3455 	struct cfg80211_chan_def def;
3456 	enum nl80211_band band;
3457 	const u8 *ht_mcs_mask;
3458 	const u16 *vht_mcs_mask;
3459 	u32 changed, bw, nss, smps, bw_prev;
3460 	int err, num_vht_rates;
3461 	const struct cfg80211_bitrate_mask *mask;
3462 	struct ath12k_wmi_peer_assoc_arg peer_arg;
3463 	enum wmi_phy_mode peer_phymode;
3464 
3465 	arsta = container_of(wk, struct ath12k_sta, update_wk);
3466 	sta = container_of((void *)arsta, struct ieee80211_sta, drv_priv);
3467 	arvif = arsta->arvif;
3468 	ar = arvif->ar;
3469 
3470 	if (WARN_ON(ath12k_mac_vif_chan(arvif->vif, &def)))
3471 		return;
3472 
3473 	band = def.chan->band;
3474 	ht_mcs_mask = arvif->bitrate_mask.control[band].ht_mcs;
3475 	vht_mcs_mask = arvif->bitrate_mask.control[band].vht_mcs;
3476 
3477 	spin_lock_bh(&ar->data_lock);
3478 
3479 	changed = arsta->changed;
3480 	arsta->changed = 0;
3481 
3482 	bw = arsta->bw;
3483 	bw_prev = arsta->bw_prev;
3484 	nss = arsta->nss;
3485 	smps = arsta->smps;
3486 
3487 	spin_unlock_bh(&ar->data_lock);
3488 
3489 	mutex_lock(&ar->conf_mutex);
3490 
3491 	nss = max_t(u32, 1, nss);
3492 	nss = min(nss, max(ath12k_mac_max_ht_nss(ht_mcs_mask),
3493 			   ath12k_mac_max_vht_nss(vht_mcs_mask)));
3494 
3495 	if (changed & IEEE80211_RC_BW_CHANGED) {
3496 		ath12k_peer_assoc_h_phymode(ar, arvif->vif, sta, &peer_arg);
3497 		peer_phymode = peer_arg.peer_phymode;
3498 
3499 		if (bw > bw_prev) {
3500 			/* Phymode shows maximum supported channel width, if we
3501 			 * upgrade bandwidth then due to sanity check of firmware,
3502 			 * we have to send WMI_PEER_PHYMODE followed by
3503 			 * WMI_PEER_CHWIDTH
3504 			 */
3505 			ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac bandwidth upgrade for sta %pM new %d old %d\n",
3506 				   sta->addr, bw, bw_prev);
3507 			err = ath12k_wmi_set_peer_param(ar, sta->addr,
3508 							arvif->vdev_id, WMI_PEER_PHYMODE,
3509 							peer_phymode);
3510 			if (err) {
3511 				ath12k_warn(ar->ab, "failed to update STA %pM to peer phymode %d: %d\n",
3512 					    sta->addr, peer_phymode, err);
3513 				goto err_rc_bw_changed;
3514 			}
3515 			err = ath12k_wmi_set_peer_param(ar, sta->addr,
3516 							arvif->vdev_id, WMI_PEER_CHWIDTH,
3517 							bw);
3518 			if (err)
3519 				ath12k_warn(ar->ab, "failed to update STA %pM to peer bandwidth %d: %d\n",
3520 					    sta->addr, bw, err);
3521 		} else {
3522 			/* When we downgrade bandwidth this will conflict with phymode
3523 			 * and cause to trigger firmware crash. In this case we send
3524 			 * WMI_PEER_CHWIDTH followed by WMI_PEER_PHYMODE
3525 			 */
3526 			ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac bandwidth downgrade for sta %pM new %d old %d\n",
3527 				   sta->addr, bw, bw_prev);
3528 			err = ath12k_wmi_set_peer_param(ar, sta->addr,
3529 							arvif->vdev_id, WMI_PEER_CHWIDTH,
3530 							bw);
3531 			if (err) {
3532 				ath12k_warn(ar->ab, "failed to update STA %pM peer to bandwidth %d: %d\n",
3533 					    sta->addr, bw, err);
3534 				goto err_rc_bw_changed;
3535 			}
3536 			err = ath12k_wmi_set_peer_param(ar, sta->addr,
3537 							arvif->vdev_id, WMI_PEER_PHYMODE,
3538 							peer_phymode);
3539 			if (err)
3540 				ath12k_warn(ar->ab, "failed to update STA %pM to peer phymode %d: %d\n",
3541 					    sta->addr, peer_phymode, err);
3542 		}
3543 	}
3544 
3545 	if (changed & IEEE80211_RC_NSS_CHANGED) {
3546 		ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac update sta %pM nss %d\n",
3547 			   sta->addr, nss);
3548 
3549 		err = ath12k_wmi_set_peer_param(ar, sta->addr, arvif->vdev_id,
3550 						WMI_PEER_NSS, nss);
3551 		if (err)
3552 			ath12k_warn(ar->ab, "failed to update STA %pM nss %d: %d\n",
3553 				    sta->addr, nss, err);
3554 	}
3555 
3556 	if (changed & IEEE80211_RC_SMPS_CHANGED) {
3557 		ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac update sta %pM smps %d\n",
3558 			   sta->addr, smps);
3559 
3560 		err = ath12k_wmi_set_peer_param(ar, sta->addr, arvif->vdev_id,
3561 						WMI_PEER_MIMO_PS_STATE, smps);
3562 		if (err)
3563 			ath12k_warn(ar->ab, "failed to update STA %pM smps %d: %d\n",
3564 				    sta->addr, smps, err);
3565 	}
3566 
3567 	if (changed & IEEE80211_RC_SUPP_RATES_CHANGED) {
3568 		mask = &arvif->bitrate_mask;
3569 		num_vht_rates = ath12k_mac_bitrate_mask_num_vht_rates(ar, band,
3570 								      mask);
3571 
3572 		/* Peer_assoc_prepare will reject vht rates in
3573 		 * bitrate_mask if its not available in range format and
3574 		 * sets vht tx_rateset as unsupported. So multiple VHT MCS
3575 		 * setting(eg. MCS 4,5,6) per peer is not supported here.
3576 		 * But, Single rate in VHT mask can be set as per-peer
3577 		 * fixed rate. But even if any HT rates are configured in
3578 		 * the bitrate mask, device will not switch to those rates
3579 		 * when per-peer Fixed rate is set.
3580 		 * TODO: Check RATEMASK_CMDID to support auto rates selection
3581 		 * across HT/VHT and for multiple VHT MCS support.
3582 		 */
3583 		if (sta->deflink.vht_cap.vht_supported && num_vht_rates == 1) {
3584 			ath12k_mac_set_peer_vht_fixed_rate(arvif, sta, mask,
3585 							   band);
3586 		} else {
3587 			/* If the peer is non-VHT or no fixed VHT rate
3588 			 * is provided in the new bitrate mask we set the
3589 			 * other rates using peer_assoc command.
3590 			 */
3591 			ath12k_peer_assoc_prepare(ar, arvif->vif, sta,
3592 						  &peer_arg, true);
3593 
3594 			err = ath12k_wmi_send_peer_assoc_cmd(ar, &peer_arg);
3595 			if (err)
3596 				ath12k_warn(ar->ab, "failed to run peer assoc for STA %pM vdev %i: %d\n",
3597 					    sta->addr, arvif->vdev_id, err);
3598 
3599 			if (!wait_for_completion_timeout(&ar->peer_assoc_done, 1 * HZ))
3600 				ath12k_warn(ar->ab, "failed to get peer assoc conf event for %pM vdev %i\n",
3601 					    sta->addr, arvif->vdev_id);
3602 		}
3603 	}
3604 err_rc_bw_changed:
3605 	mutex_unlock(&ar->conf_mutex);
3606 }
3607 
3608 static int ath12k_mac_inc_num_stations(struct ath12k_vif *arvif,
3609 				       struct ieee80211_sta *sta)
3610 {
3611 	struct ath12k *ar = arvif->ar;
3612 
3613 	lockdep_assert_held(&ar->conf_mutex);
3614 
3615 	if (arvif->vdev_type == WMI_VDEV_TYPE_STA && !sta->tdls)
3616 		return 0;
3617 
3618 	if (ar->num_stations >= ar->max_num_stations)
3619 		return -ENOBUFS;
3620 
3621 	ar->num_stations++;
3622 
3623 	return 0;
3624 }
3625 
3626 static void ath12k_mac_dec_num_stations(struct ath12k_vif *arvif,
3627 					struct ieee80211_sta *sta)
3628 {
3629 	struct ath12k *ar = arvif->ar;
3630 
3631 	lockdep_assert_held(&ar->conf_mutex);
3632 
3633 	if (arvif->vdev_type == WMI_VDEV_TYPE_STA && !sta->tdls)
3634 		return;
3635 
3636 	ar->num_stations--;
3637 }
3638 
3639 static int ath12k_mac_station_add(struct ath12k *ar,
3640 				  struct ieee80211_vif *vif,
3641 				  struct ieee80211_sta *sta)
3642 {
3643 	struct ath12k_base *ab = ar->ab;
3644 	struct ath12k_vif *arvif = ath12k_vif_to_arvif(vif);
3645 	struct ath12k_sta *arsta = ath12k_sta_to_arsta(sta);
3646 	struct ath12k_wmi_peer_create_arg peer_param;
3647 	int ret;
3648 
3649 	lockdep_assert_held(&ar->conf_mutex);
3650 
3651 	ret = ath12k_mac_inc_num_stations(arvif, sta);
3652 	if (ret) {
3653 		ath12k_warn(ab, "refusing to associate station: too many connected already (%d)\n",
3654 			    ar->max_num_stations);
3655 		goto exit;
3656 	}
3657 
3658 	arsta->rx_stats = kzalloc(sizeof(*arsta->rx_stats), GFP_KERNEL);
3659 	if (!arsta->rx_stats) {
3660 		ret = -ENOMEM;
3661 		goto dec_num_station;
3662 	}
3663 
3664 	peer_param.vdev_id = arvif->vdev_id;
3665 	peer_param.peer_addr = sta->addr;
3666 	peer_param.peer_type = WMI_PEER_TYPE_DEFAULT;
3667 
3668 	ret = ath12k_peer_create(ar, arvif, sta, &peer_param);
3669 	if (ret) {
3670 		ath12k_warn(ab, "Failed to add peer: %pM for VDEV: %d\n",
3671 			    sta->addr, arvif->vdev_id);
3672 		goto free_peer;
3673 	}
3674 
3675 	ath12k_dbg(ab, ATH12K_DBG_MAC, "Added peer: %pM for VDEV: %d\n",
3676 		   sta->addr, arvif->vdev_id);
3677 
3678 	if (ieee80211_vif_is_mesh(vif)) {
3679 		ret = ath12k_wmi_set_peer_param(ar, sta->addr,
3680 						arvif->vdev_id,
3681 						WMI_PEER_USE_4ADDR, 1);
3682 		if (ret) {
3683 			ath12k_warn(ab, "failed to STA %pM 4addr capability: %d\n",
3684 				    sta->addr, ret);
3685 			goto free_peer;
3686 		}
3687 	}
3688 
3689 	ret = ath12k_dp_peer_setup(ar, arvif->vdev_id, sta->addr);
3690 	if (ret) {
3691 		ath12k_warn(ab, "failed to setup dp for peer %pM on vdev %i (%d)\n",
3692 			    sta->addr, arvif->vdev_id, ret);
3693 		goto free_peer;
3694 	}
3695 
3696 	if (ab->hw_params->vdev_start_delay &&
3697 	    !arvif->is_started &&
3698 	    arvif->vdev_type != WMI_VDEV_TYPE_AP) {
3699 		ret = ath12k_start_vdev_delay(ar->hw, vif);
3700 		if (ret) {
3701 			ath12k_warn(ab, "failed to delay vdev start: %d\n", ret);
3702 			goto free_peer;
3703 		}
3704 	}
3705 
3706 	return 0;
3707 
3708 free_peer:
3709 	ath12k_peer_delete(ar, arvif->vdev_id, sta->addr);
3710 dec_num_station:
3711 	ath12k_mac_dec_num_stations(arvif, sta);
3712 exit:
3713 	return ret;
3714 }
3715 
3716 static u32 ath12k_mac_ieee80211_sta_bw_to_wmi(struct ath12k *ar,
3717 					      struct ieee80211_sta *sta)
3718 {
3719 	u32 bw = WMI_PEER_CHWIDTH_20MHZ;
3720 
3721 	switch (sta->deflink.bandwidth) {
3722 	case IEEE80211_STA_RX_BW_20:
3723 		bw = WMI_PEER_CHWIDTH_20MHZ;
3724 		break;
3725 	case IEEE80211_STA_RX_BW_40:
3726 		bw = WMI_PEER_CHWIDTH_40MHZ;
3727 		break;
3728 	case IEEE80211_STA_RX_BW_80:
3729 		bw = WMI_PEER_CHWIDTH_80MHZ;
3730 		break;
3731 	case IEEE80211_STA_RX_BW_160:
3732 		bw = WMI_PEER_CHWIDTH_160MHZ;
3733 		break;
3734 	case IEEE80211_STA_RX_BW_320:
3735 		bw = WMI_PEER_CHWIDTH_320MHZ;
3736 		break;
3737 	default:
3738 		ath12k_warn(ar->ab, "Invalid bandwidth %d in rc update for %pM\n",
3739 			    sta->deflink.bandwidth, sta->addr);
3740 		bw = WMI_PEER_CHWIDTH_20MHZ;
3741 		break;
3742 	}
3743 
3744 	return bw;
3745 }
3746 
3747 static int ath12k_mac_op_sta_state(struct ieee80211_hw *hw,
3748 				   struct ieee80211_vif *vif,
3749 				   struct ieee80211_sta *sta,
3750 				   enum ieee80211_sta_state old_state,
3751 				   enum ieee80211_sta_state new_state)
3752 {
3753 	struct ath12k *ar = hw->priv;
3754 	struct ath12k_vif *arvif = ath12k_vif_to_arvif(vif);
3755 	struct ath12k_sta *arsta = ath12k_sta_to_arsta(sta);
3756 	struct ath12k_peer *peer;
3757 	int ret = 0;
3758 
3759 	/* cancel must be done outside the mutex to avoid deadlock */
3760 	if ((old_state == IEEE80211_STA_NONE &&
3761 	     new_state == IEEE80211_STA_NOTEXIST))
3762 		cancel_work_sync(&arsta->update_wk);
3763 
3764 	mutex_lock(&ar->conf_mutex);
3765 
3766 	if (old_state == IEEE80211_STA_NOTEXIST &&
3767 	    new_state == IEEE80211_STA_NONE) {
3768 		memset(arsta, 0, sizeof(*arsta));
3769 		arsta->arvif = arvif;
3770 		INIT_WORK(&arsta->update_wk, ath12k_sta_rc_update_wk);
3771 
3772 		ret = ath12k_mac_station_add(ar, vif, sta);
3773 		if (ret)
3774 			ath12k_warn(ar->ab, "Failed to add station: %pM for VDEV: %d\n",
3775 				    sta->addr, arvif->vdev_id);
3776 	} else if ((old_state == IEEE80211_STA_NONE &&
3777 		    new_state == IEEE80211_STA_NOTEXIST)) {
3778 		ath12k_dp_peer_cleanup(ar, arvif->vdev_id, sta->addr);
3779 
3780 		ret = ath12k_peer_delete(ar, arvif->vdev_id, sta->addr);
3781 		if (ret)
3782 			ath12k_warn(ar->ab, "Failed to delete peer: %pM for VDEV: %d\n",
3783 				    sta->addr, arvif->vdev_id);
3784 		else
3785 			ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "Removed peer: %pM for VDEV: %d\n",
3786 				   sta->addr, arvif->vdev_id);
3787 
3788 		ath12k_mac_dec_num_stations(arvif, sta);
3789 		spin_lock_bh(&ar->ab->base_lock);
3790 		peer = ath12k_peer_find(ar->ab, arvif->vdev_id, sta->addr);
3791 		if (peer && peer->sta == sta) {
3792 			ath12k_warn(ar->ab, "Found peer entry %pM n vdev %i after it was supposedly removed\n",
3793 				    vif->addr, arvif->vdev_id);
3794 			peer->sta = NULL;
3795 			list_del(&peer->list);
3796 			kfree(peer);
3797 			ar->num_peers--;
3798 		}
3799 		spin_unlock_bh(&ar->ab->base_lock);
3800 
3801 		kfree(arsta->rx_stats);
3802 		arsta->rx_stats = NULL;
3803 	} else if (old_state == IEEE80211_STA_AUTH &&
3804 		   new_state == IEEE80211_STA_ASSOC &&
3805 		   (vif->type == NL80211_IFTYPE_AP ||
3806 		    vif->type == NL80211_IFTYPE_MESH_POINT ||
3807 		    vif->type == NL80211_IFTYPE_ADHOC)) {
3808 		ret = ath12k_station_assoc(ar, vif, sta, false);
3809 		if (ret)
3810 			ath12k_warn(ar->ab, "Failed to associate station: %pM\n",
3811 				    sta->addr);
3812 
3813 		spin_lock_bh(&ar->data_lock);
3814 
3815 		arsta->bw = ath12k_mac_ieee80211_sta_bw_to_wmi(ar, sta);
3816 		arsta->bw_prev = sta->deflink.bandwidth;
3817 
3818 		spin_unlock_bh(&ar->data_lock);
3819 	} else if (old_state == IEEE80211_STA_ASSOC &&
3820 		   new_state == IEEE80211_STA_AUTHORIZED) {
3821 		spin_lock_bh(&ar->ab->base_lock);
3822 
3823 		peer = ath12k_peer_find(ar->ab, arvif->vdev_id, sta->addr);
3824 		if (peer)
3825 			peer->is_authorized = true;
3826 
3827 		spin_unlock_bh(&ar->ab->base_lock);
3828 
3829 		if (vif->type == NL80211_IFTYPE_STATION && arvif->is_up) {
3830 			ret = ath12k_wmi_set_peer_param(ar, sta->addr,
3831 							arvif->vdev_id,
3832 							WMI_PEER_AUTHORIZE,
3833 							1);
3834 			if (ret)
3835 				ath12k_warn(ar->ab, "Unable to authorize peer %pM vdev %d: %d\n",
3836 					    sta->addr, arvif->vdev_id, ret);
3837 		}
3838 	} else if (old_state == IEEE80211_STA_AUTHORIZED &&
3839 		   new_state == IEEE80211_STA_ASSOC) {
3840 		spin_lock_bh(&ar->ab->base_lock);
3841 
3842 		peer = ath12k_peer_find(ar->ab, arvif->vdev_id, sta->addr);
3843 		if (peer)
3844 			peer->is_authorized = false;
3845 
3846 		spin_unlock_bh(&ar->ab->base_lock);
3847 	} else if (old_state == IEEE80211_STA_ASSOC &&
3848 		   new_state == IEEE80211_STA_AUTH &&
3849 		   (vif->type == NL80211_IFTYPE_AP ||
3850 		    vif->type == NL80211_IFTYPE_MESH_POINT ||
3851 		    vif->type == NL80211_IFTYPE_ADHOC)) {
3852 		ret = ath12k_station_disassoc(ar, vif, sta);
3853 		if (ret)
3854 			ath12k_warn(ar->ab, "Failed to disassociate station: %pM\n",
3855 				    sta->addr);
3856 	}
3857 
3858 	mutex_unlock(&ar->conf_mutex);
3859 	return ret;
3860 }
3861 
3862 static int ath12k_mac_op_sta_set_txpwr(struct ieee80211_hw *hw,
3863 				       struct ieee80211_vif *vif,
3864 				       struct ieee80211_sta *sta)
3865 {
3866 	struct ath12k *ar = hw->priv;
3867 	struct ath12k_vif *arvif = ath12k_vif_to_arvif(vif);
3868 	int ret;
3869 	s16 txpwr;
3870 
3871 	if (sta->deflink.txpwr.type == NL80211_TX_POWER_AUTOMATIC) {
3872 		txpwr = 0;
3873 	} else {
3874 		txpwr = sta->deflink.txpwr.power;
3875 		if (!txpwr)
3876 			return -EINVAL;
3877 	}
3878 
3879 	if (txpwr > ATH12K_TX_POWER_MAX_VAL || txpwr < ATH12K_TX_POWER_MIN_VAL)
3880 		return -EINVAL;
3881 
3882 	mutex_lock(&ar->conf_mutex);
3883 
3884 	ret = ath12k_wmi_set_peer_param(ar, sta->addr, arvif->vdev_id,
3885 					WMI_PEER_USE_FIXED_PWR, txpwr);
3886 	if (ret) {
3887 		ath12k_warn(ar->ab, "failed to set tx power for station ret: %d\n",
3888 			    ret);
3889 		goto out;
3890 	}
3891 
3892 out:
3893 	mutex_unlock(&ar->conf_mutex);
3894 	return ret;
3895 }
3896 
3897 static void ath12k_mac_op_sta_rc_update(struct ieee80211_hw *hw,
3898 					struct ieee80211_vif *vif,
3899 					struct ieee80211_sta *sta,
3900 					u32 changed)
3901 {
3902 	struct ath12k *ar = hw->priv;
3903 	struct ath12k_sta *arsta = ath12k_sta_to_arsta(sta);
3904 	struct ath12k_vif *arvif = ath12k_vif_to_arvif(vif);
3905 	struct ath12k_peer *peer;
3906 	u32 bw, smps;
3907 
3908 	spin_lock_bh(&ar->ab->base_lock);
3909 
3910 	peer = ath12k_peer_find(ar->ab, arvif->vdev_id, sta->addr);
3911 	if (!peer) {
3912 		spin_unlock_bh(&ar->ab->base_lock);
3913 		ath12k_warn(ar->ab, "mac sta rc update failed to find peer %pM on vdev %i\n",
3914 			    sta->addr, arvif->vdev_id);
3915 		return;
3916 	}
3917 
3918 	spin_unlock_bh(&ar->ab->base_lock);
3919 
3920 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
3921 		   "mac sta rc update for %pM changed %08x bw %d nss %d smps %d\n",
3922 		   sta->addr, changed, sta->deflink.bandwidth, sta->deflink.rx_nss,
3923 		   sta->deflink.smps_mode);
3924 
3925 	spin_lock_bh(&ar->data_lock);
3926 
3927 	if (changed & IEEE80211_RC_BW_CHANGED) {
3928 		bw = ath12k_mac_ieee80211_sta_bw_to_wmi(ar, sta);
3929 		arsta->bw_prev = arsta->bw;
3930 		arsta->bw = bw;
3931 	}
3932 
3933 	if (changed & IEEE80211_RC_NSS_CHANGED)
3934 		arsta->nss = sta->deflink.rx_nss;
3935 
3936 	if (changed & IEEE80211_RC_SMPS_CHANGED) {
3937 		smps = WMI_PEER_SMPS_PS_NONE;
3938 
3939 		switch (sta->deflink.smps_mode) {
3940 		case IEEE80211_SMPS_AUTOMATIC:
3941 		case IEEE80211_SMPS_OFF:
3942 			smps = WMI_PEER_SMPS_PS_NONE;
3943 			break;
3944 		case IEEE80211_SMPS_STATIC:
3945 			smps = WMI_PEER_SMPS_STATIC;
3946 			break;
3947 		case IEEE80211_SMPS_DYNAMIC:
3948 			smps = WMI_PEER_SMPS_DYNAMIC;
3949 			break;
3950 		default:
3951 			ath12k_warn(ar->ab, "Invalid smps %d in sta rc update for %pM\n",
3952 				    sta->deflink.smps_mode, sta->addr);
3953 			smps = WMI_PEER_SMPS_PS_NONE;
3954 			break;
3955 		}
3956 
3957 		arsta->smps = smps;
3958 	}
3959 
3960 	arsta->changed |= changed;
3961 
3962 	spin_unlock_bh(&ar->data_lock);
3963 
3964 	ieee80211_queue_work(hw, &arsta->update_wk);
3965 }
3966 
3967 static int ath12k_conf_tx_uapsd(struct ath12k *ar, struct ieee80211_vif *vif,
3968 				u16 ac, bool enable)
3969 {
3970 	struct ath12k_vif *arvif = ath12k_vif_to_arvif(vif);
3971 	u32 value;
3972 	int ret;
3973 
3974 	if (arvif->vdev_type != WMI_VDEV_TYPE_STA)
3975 		return 0;
3976 
3977 	switch (ac) {
3978 	case IEEE80211_AC_VO:
3979 		value = WMI_STA_PS_UAPSD_AC3_DELIVERY_EN |
3980 			WMI_STA_PS_UAPSD_AC3_TRIGGER_EN;
3981 		break;
3982 	case IEEE80211_AC_VI:
3983 		value = WMI_STA_PS_UAPSD_AC2_DELIVERY_EN |
3984 			WMI_STA_PS_UAPSD_AC2_TRIGGER_EN;
3985 		break;
3986 	case IEEE80211_AC_BE:
3987 		value = WMI_STA_PS_UAPSD_AC1_DELIVERY_EN |
3988 			WMI_STA_PS_UAPSD_AC1_TRIGGER_EN;
3989 		break;
3990 	case IEEE80211_AC_BK:
3991 		value = WMI_STA_PS_UAPSD_AC0_DELIVERY_EN |
3992 			WMI_STA_PS_UAPSD_AC0_TRIGGER_EN;
3993 		break;
3994 	}
3995 
3996 	if (enable)
3997 		arvif->u.sta.uapsd |= value;
3998 	else
3999 		arvif->u.sta.uapsd &= ~value;
4000 
4001 	ret = ath12k_wmi_set_sta_ps_param(ar, arvif->vdev_id,
4002 					  WMI_STA_PS_PARAM_UAPSD,
4003 					  arvif->u.sta.uapsd);
4004 	if (ret) {
4005 		ath12k_warn(ar->ab, "could not set uapsd params %d\n", ret);
4006 		goto exit;
4007 	}
4008 
4009 	if (arvif->u.sta.uapsd)
4010 		value = WMI_STA_PS_RX_WAKE_POLICY_POLL_UAPSD;
4011 	else
4012 		value = WMI_STA_PS_RX_WAKE_POLICY_WAKE;
4013 
4014 	ret = ath12k_wmi_set_sta_ps_param(ar, arvif->vdev_id,
4015 					  WMI_STA_PS_PARAM_RX_WAKE_POLICY,
4016 					  value);
4017 	if (ret)
4018 		ath12k_warn(ar->ab, "could not set rx wake param %d\n", ret);
4019 
4020 exit:
4021 	return ret;
4022 }
4023 
4024 static int ath12k_mac_op_conf_tx(struct ieee80211_hw *hw,
4025 				 struct ieee80211_vif *vif,
4026 				 unsigned int link_id, u16 ac,
4027 				 const struct ieee80211_tx_queue_params *params)
4028 {
4029 	struct ath12k *ar = hw->priv;
4030 	struct ath12k_vif *arvif = ath12k_vif_to_arvif(vif);
4031 	struct wmi_wmm_params_arg *p = NULL;
4032 	int ret;
4033 
4034 	mutex_lock(&ar->conf_mutex);
4035 
4036 	switch (ac) {
4037 	case IEEE80211_AC_VO:
4038 		p = &arvif->wmm_params.ac_vo;
4039 		break;
4040 	case IEEE80211_AC_VI:
4041 		p = &arvif->wmm_params.ac_vi;
4042 		break;
4043 	case IEEE80211_AC_BE:
4044 		p = &arvif->wmm_params.ac_be;
4045 		break;
4046 	case IEEE80211_AC_BK:
4047 		p = &arvif->wmm_params.ac_bk;
4048 		break;
4049 	}
4050 
4051 	if (WARN_ON(!p)) {
4052 		ret = -EINVAL;
4053 		goto exit;
4054 	}
4055 
4056 	p->cwmin = params->cw_min;
4057 	p->cwmax = params->cw_max;
4058 	p->aifs = params->aifs;
4059 	p->txop = params->txop;
4060 
4061 	ret = ath12k_wmi_send_wmm_update_cmd(ar, arvif->vdev_id,
4062 					     &arvif->wmm_params);
4063 	if (ret) {
4064 		ath12k_warn(ar->ab, "failed to set wmm params: %d\n", ret);
4065 		goto exit;
4066 	}
4067 
4068 	ret = ath12k_conf_tx_uapsd(ar, vif, ac, params->uapsd);
4069 
4070 	if (ret)
4071 		ath12k_warn(ar->ab, "failed to set sta uapsd: %d\n", ret);
4072 
4073 exit:
4074 	mutex_unlock(&ar->conf_mutex);
4075 	return ret;
4076 }
4077 
4078 static struct ieee80211_sta_ht_cap
4079 ath12k_create_ht_cap(struct ath12k *ar, u32 ar_ht_cap, u32 rate_cap_rx_chainmask)
4080 {
4081 	int i;
4082 	struct ieee80211_sta_ht_cap ht_cap = {0};
4083 	u32 ar_vht_cap = ar->pdev->cap.vht_cap;
4084 
4085 	if (!(ar_ht_cap & WMI_HT_CAP_ENABLED))
4086 		return ht_cap;
4087 
4088 	ht_cap.ht_supported = 1;
4089 	ht_cap.ampdu_factor = IEEE80211_HT_MAX_AMPDU_64K;
4090 	ht_cap.ampdu_density = IEEE80211_HT_MPDU_DENSITY_NONE;
4091 	ht_cap.cap |= IEEE80211_HT_CAP_SUP_WIDTH_20_40;
4092 	ht_cap.cap |= IEEE80211_HT_CAP_DSSSCCK40;
4093 	ht_cap.cap |= WLAN_HT_CAP_SM_PS_STATIC << IEEE80211_HT_CAP_SM_PS_SHIFT;
4094 
4095 	if (ar_ht_cap & WMI_HT_CAP_HT20_SGI)
4096 		ht_cap.cap |= IEEE80211_HT_CAP_SGI_20;
4097 
4098 	if (ar_ht_cap & WMI_HT_CAP_HT40_SGI)
4099 		ht_cap.cap |= IEEE80211_HT_CAP_SGI_40;
4100 
4101 	if (ar_ht_cap & WMI_HT_CAP_DYNAMIC_SMPS) {
4102 		u32 smps;
4103 
4104 		smps   = WLAN_HT_CAP_SM_PS_DYNAMIC;
4105 		smps <<= IEEE80211_HT_CAP_SM_PS_SHIFT;
4106 
4107 		ht_cap.cap |= smps;
4108 	}
4109 
4110 	if (ar_ht_cap & WMI_HT_CAP_TX_STBC)
4111 		ht_cap.cap |= IEEE80211_HT_CAP_TX_STBC;
4112 
4113 	if (ar_ht_cap & WMI_HT_CAP_RX_STBC) {
4114 		u32 stbc;
4115 
4116 		stbc   = ar_ht_cap;
4117 		stbc  &= WMI_HT_CAP_RX_STBC;
4118 		stbc >>= WMI_HT_CAP_RX_STBC_MASK_SHIFT;
4119 		stbc <<= IEEE80211_HT_CAP_RX_STBC_SHIFT;
4120 		stbc  &= IEEE80211_HT_CAP_RX_STBC;
4121 
4122 		ht_cap.cap |= stbc;
4123 	}
4124 
4125 	if (ar_ht_cap & WMI_HT_CAP_RX_LDPC)
4126 		ht_cap.cap |= IEEE80211_HT_CAP_LDPC_CODING;
4127 
4128 	if (ar_ht_cap & WMI_HT_CAP_L_SIG_TXOP_PROT)
4129 		ht_cap.cap |= IEEE80211_HT_CAP_LSIG_TXOP_PROT;
4130 
4131 	if (ar_vht_cap & WMI_VHT_CAP_MAX_MPDU_LEN_MASK)
4132 		ht_cap.cap |= IEEE80211_HT_CAP_MAX_AMSDU;
4133 
4134 	for (i = 0; i < ar->num_rx_chains; i++) {
4135 		if (rate_cap_rx_chainmask & BIT(i))
4136 			ht_cap.mcs.rx_mask[i] = 0xFF;
4137 	}
4138 
4139 	ht_cap.mcs.tx_params |= IEEE80211_HT_MCS_TX_DEFINED;
4140 
4141 	return ht_cap;
4142 }
4143 
4144 static int ath12k_mac_set_txbf_conf(struct ath12k_vif *arvif)
4145 {
4146 	u32 value = 0;
4147 	struct ath12k *ar = arvif->ar;
4148 	int nsts;
4149 	int sound_dim;
4150 	u32 vht_cap = ar->pdev->cap.vht_cap;
4151 	u32 vdev_param = WMI_VDEV_PARAM_TXBF;
4152 
4153 	if (vht_cap & (IEEE80211_VHT_CAP_SU_BEAMFORMEE_CAPABLE)) {
4154 		nsts = vht_cap & IEEE80211_VHT_CAP_BEAMFORMEE_STS_MASK;
4155 		nsts >>= IEEE80211_VHT_CAP_BEAMFORMEE_STS_SHIFT;
4156 		value |= SM(nsts, WMI_TXBF_STS_CAP_OFFSET);
4157 	}
4158 
4159 	if (vht_cap & (IEEE80211_VHT_CAP_SU_BEAMFORMER_CAPABLE)) {
4160 		sound_dim = vht_cap &
4161 			    IEEE80211_VHT_CAP_SOUNDING_DIMENSIONS_MASK;
4162 		sound_dim >>= IEEE80211_VHT_CAP_SOUNDING_DIMENSIONS_SHIFT;
4163 		if (sound_dim > (ar->num_tx_chains - 1))
4164 			sound_dim = ar->num_tx_chains - 1;
4165 		value |= SM(sound_dim, WMI_BF_SOUND_DIM_OFFSET);
4166 	}
4167 
4168 	if (!value)
4169 		return 0;
4170 
4171 	if (vht_cap & IEEE80211_VHT_CAP_SU_BEAMFORMER_CAPABLE) {
4172 		value |= WMI_VDEV_PARAM_TXBF_SU_TX_BFER;
4173 
4174 		if ((vht_cap & IEEE80211_VHT_CAP_MU_BEAMFORMER_CAPABLE) &&
4175 		    arvif->vdev_type == WMI_VDEV_TYPE_AP)
4176 			value |= WMI_VDEV_PARAM_TXBF_MU_TX_BFER;
4177 	}
4178 
4179 	if (vht_cap & IEEE80211_VHT_CAP_SU_BEAMFORMEE_CAPABLE) {
4180 		value |= WMI_VDEV_PARAM_TXBF_SU_TX_BFEE;
4181 
4182 		if ((vht_cap & IEEE80211_VHT_CAP_MU_BEAMFORMEE_CAPABLE) &&
4183 		    arvif->vdev_type == WMI_VDEV_TYPE_STA)
4184 			value |= WMI_VDEV_PARAM_TXBF_MU_TX_BFEE;
4185 	}
4186 
4187 	return ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
4188 					     vdev_param, value);
4189 }
4190 
4191 static void ath12k_set_vht_txbf_cap(struct ath12k *ar, u32 *vht_cap)
4192 {
4193 	bool subfer, subfee;
4194 	int sound_dim = 0;
4195 
4196 	subfer = !!(*vht_cap & (IEEE80211_VHT_CAP_SU_BEAMFORMER_CAPABLE));
4197 	subfee = !!(*vht_cap & (IEEE80211_VHT_CAP_SU_BEAMFORMEE_CAPABLE));
4198 
4199 	if (ar->num_tx_chains < 2) {
4200 		*vht_cap &= ~(IEEE80211_VHT_CAP_SU_BEAMFORMER_CAPABLE);
4201 		subfer = false;
4202 	}
4203 
4204 	/* If SU Beaformer is not set, then disable MU Beamformer Capability */
4205 	if (!subfer)
4206 		*vht_cap &= ~(IEEE80211_VHT_CAP_MU_BEAMFORMER_CAPABLE);
4207 
4208 	/* If SU Beaformee is not set, then disable MU Beamformee Capability */
4209 	if (!subfee)
4210 		*vht_cap &= ~(IEEE80211_VHT_CAP_MU_BEAMFORMEE_CAPABLE);
4211 
4212 	sound_dim = u32_get_bits(*vht_cap,
4213 				 IEEE80211_VHT_CAP_SOUNDING_DIMENSIONS_MASK);
4214 	*vht_cap = u32_replace_bits(*vht_cap, 0,
4215 				    IEEE80211_VHT_CAP_SOUNDING_DIMENSIONS_MASK);
4216 
4217 	/* TODO: Need to check invalid STS and Sound_dim values set by FW? */
4218 
4219 	/* Enable Sounding Dimension Field only if SU BF is enabled */
4220 	if (subfer) {
4221 		if (sound_dim > (ar->num_tx_chains - 1))
4222 			sound_dim = ar->num_tx_chains - 1;
4223 
4224 		*vht_cap = u32_replace_bits(*vht_cap, sound_dim,
4225 					    IEEE80211_VHT_CAP_SOUNDING_DIMENSIONS_MASK);
4226 	}
4227 
4228 	/* Use the STS advertised by FW unless SU Beamformee is not supported*/
4229 	if (!subfee)
4230 		*vht_cap &= ~(IEEE80211_VHT_CAP_BEAMFORMEE_STS_MASK);
4231 }
4232 
4233 static struct ieee80211_sta_vht_cap
4234 ath12k_create_vht_cap(struct ath12k *ar, u32 rate_cap_tx_chainmask,
4235 		      u32 rate_cap_rx_chainmask)
4236 {
4237 	struct ieee80211_sta_vht_cap vht_cap = {0};
4238 	u16 txmcs_map, rxmcs_map;
4239 	int i;
4240 
4241 	vht_cap.vht_supported = 1;
4242 	vht_cap.cap = ar->pdev->cap.vht_cap;
4243 
4244 	ath12k_set_vht_txbf_cap(ar, &vht_cap.cap);
4245 
4246 	/* TODO: Enable back VHT160 mode once association issues are fixed */
4247 	/* Disabling VHT160 and VHT80+80 modes */
4248 	vht_cap.cap &= ~IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_MASK;
4249 	vht_cap.cap &= ~IEEE80211_VHT_CAP_SHORT_GI_160;
4250 
4251 	rxmcs_map = 0;
4252 	txmcs_map = 0;
4253 	for (i = 0; i < 8; i++) {
4254 		if (i < ar->num_tx_chains && rate_cap_tx_chainmask & BIT(i))
4255 			txmcs_map |= IEEE80211_VHT_MCS_SUPPORT_0_9 << (i * 2);
4256 		else
4257 			txmcs_map |= IEEE80211_VHT_MCS_NOT_SUPPORTED << (i * 2);
4258 
4259 		if (i < ar->num_rx_chains && rate_cap_rx_chainmask & BIT(i))
4260 			rxmcs_map |= IEEE80211_VHT_MCS_SUPPORT_0_9 << (i * 2);
4261 		else
4262 			rxmcs_map |= IEEE80211_VHT_MCS_NOT_SUPPORTED << (i * 2);
4263 	}
4264 
4265 	if (rate_cap_tx_chainmask <= 1)
4266 		vht_cap.cap &= ~IEEE80211_VHT_CAP_TXSTBC;
4267 
4268 	vht_cap.vht_mcs.rx_mcs_map = cpu_to_le16(rxmcs_map);
4269 	vht_cap.vht_mcs.tx_mcs_map = cpu_to_le16(txmcs_map);
4270 
4271 	return vht_cap;
4272 }
4273 
4274 static void ath12k_mac_setup_ht_vht_cap(struct ath12k *ar,
4275 					struct ath12k_pdev_cap *cap,
4276 					u32 *ht_cap_info)
4277 {
4278 	struct ieee80211_supported_band *band;
4279 	u32 rate_cap_tx_chainmask;
4280 	u32 rate_cap_rx_chainmask;
4281 	u32 ht_cap;
4282 
4283 	rate_cap_tx_chainmask = ar->cfg_tx_chainmask >> cap->tx_chain_mask_shift;
4284 	rate_cap_rx_chainmask = ar->cfg_rx_chainmask >> cap->rx_chain_mask_shift;
4285 
4286 	if (cap->supported_bands & WMI_HOST_WLAN_2G_CAP) {
4287 		band = &ar->mac.sbands[NL80211_BAND_2GHZ];
4288 		ht_cap = cap->band[NL80211_BAND_2GHZ].ht_cap_info;
4289 		if (ht_cap_info)
4290 			*ht_cap_info = ht_cap;
4291 		band->ht_cap = ath12k_create_ht_cap(ar, ht_cap,
4292 						    rate_cap_rx_chainmask);
4293 	}
4294 
4295 	if (cap->supported_bands & WMI_HOST_WLAN_5G_CAP &&
4296 	    (ar->ab->hw_params->single_pdev_only ||
4297 	     !ar->supports_6ghz)) {
4298 		band = &ar->mac.sbands[NL80211_BAND_5GHZ];
4299 		ht_cap = cap->band[NL80211_BAND_5GHZ].ht_cap_info;
4300 		if (ht_cap_info)
4301 			*ht_cap_info = ht_cap;
4302 		band->ht_cap = ath12k_create_ht_cap(ar, ht_cap,
4303 						    rate_cap_rx_chainmask);
4304 		band->vht_cap = ath12k_create_vht_cap(ar, rate_cap_tx_chainmask,
4305 						      rate_cap_rx_chainmask);
4306 	}
4307 }
4308 
4309 static int ath12k_check_chain_mask(struct ath12k *ar, u32 ant, bool is_tx_ant)
4310 {
4311 	/* TODO: Check the request chainmask against the supported
4312 	 * chainmask table which is advertised in extented_service_ready event
4313 	 */
4314 
4315 	return 0;
4316 }
4317 
4318 static void ath12k_gen_ppe_thresh(struct ath12k_wmi_ppe_threshold_arg *fw_ppet,
4319 				  u8 *he_ppet)
4320 {
4321 	int nss, ru;
4322 	u8 bit = 7;
4323 
4324 	he_ppet[0] = fw_ppet->numss_m1 & IEEE80211_PPE_THRES_NSS_MASK;
4325 	he_ppet[0] |= (fw_ppet->ru_bit_mask <<
4326 		       IEEE80211_PPE_THRES_RU_INDEX_BITMASK_POS) &
4327 		      IEEE80211_PPE_THRES_RU_INDEX_BITMASK_MASK;
4328 	for (nss = 0; nss <= fw_ppet->numss_m1; nss++) {
4329 		for (ru = 0; ru < 4; ru++) {
4330 			u8 val;
4331 			int i;
4332 
4333 			if ((fw_ppet->ru_bit_mask & BIT(ru)) == 0)
4334 				continue;
4335 			val = (fw_ppet->ppet16_ppet8_ru3_ru0[nss] >> (ru * 6)) &
4336 			       0x3f;
4337 			val = ((val >> 3) & 0x7) | ((val & 0x7) << 3);
4338 			for (i = 5; i >= 0; i--) {
4339 				he_ppet[bit / 8] |=
4340 					((val >> i) & 0x1) << ((bit % 8));
4341 				bit++;
4342 			}
4343 		}
4344 	}
4345 }
4346 
4347 static void
4348 ath12k_mac_filter_he_cap_mesh(struct ieee80211_he_cap_elem *he_cap_elem)
4349 {
4350 	u8 m;
4351 
4352 	m = IEEE80211_HE_MAC_CAP0_TWT_RES |
4353 	    IEEE80211_HE_MAC_CAP0_TWT_REQ;
4354 	he_cap_elem->mac_cap_info[0] &= ~m;
4355 
4356 	m = IEEE80211_HE_MAC_CAP2_TRS |
4357 	    IEEE80211_HE_MAC_CAP2_BCAST_TWT |
4358 	    IEEE80211_HE_MAC_CAP2_MU_CASCADING;
4359 	he_cap_elem->mac_cap_info[2] &= ~m;
4360 
4361 	m = IEEE80211_HE_MAC_CAP3_FLEX_TWT_SCHED |
4362 	    IEEE80211_HE_MAC_CAP2_BCAST_TWT |
4363 	    IEEE80211_HE_MAC_CAP2_MU_CASCADING;
4364 	he_cap_elem->mac_cap_info[3] &= ~m;
4365 
4366 	m = IEEE80211_HE_MAC_CAP4_BSRP_BQRP_A_MPDU_AGG |
4367 	    IEEE80211_HE_MAC_CAP4_BQR;
4368 	he_cap_elem->mac_cap_info[4] &= ~m;
4369 
4370 	m = IEEE80211_HE_MAC_CAP5_SUBCHAN_SELECTIVE_TRANSMISSION |
4371 	    IEEE80211_HE_MAC_CAP5_UL_2x996_TONE_RU |
4372 	    IEEE80211_HE_MAC_CAP5_PUNCTURED_SOUNDING |
4373 	    IEEE80211_HE_MAC_CAP5_HT_VHT_TRIG_FRAME_RX;
4374 	he_cap_elem->mac_cap_info[5] &= ~m;
4375 
4376 	m = IEEE80211_HE_PHY_CAP2_UL_MU_FULL_MU_MIMO |
4377 	    IEEE80211_HE_PHY_CAP2_UL_MU_PARTIAL_MU_MIMO;
4378 	he_cap_elem->phy_cap_info[2] &= ~m;
4379 
4380 	m = IEEE80211_HE_PHY_CAP3_RX_PARTIAL_BW_SU_IN_20MHZ_MU |
4381 	    IEEE80211_HE_PHY_CAP3_DCM_MAX_CONST_TX_MASK |
4382 	    IEEE80211_HE_PHY_CAP3_DCM_MAX_CONST_RX_MASK;
4383 	he_cap_elem->phy_cap_info[3] &= ~m;
4384 
4385 	m = IEEE80211_HE_PHY_CAP4_MU_BEAMFORMER;
4386 	he_cap_elem->phy_cap_info[4] &= ~m;
4387 
4388 	m = IEEE80211_HE_PHY_CAP5_NG16_MU_FEEDBACK;
4389 	he_cap_elem->phy_cap_info[5] &= ~m;
4390 
4391 	m = IEEE80211_HE_PHY_CAP6_CODEBOOK_SIZE_75_MU |
4392 	    IEEE80211_HE_PHY_CAP6_TRIG_MU_BEAMFORMING_PARTIAL_BW_FB |
4393 	    IEEE80211_HE_PHY_CAP6_TRIG_CQI_FB |
4394 	    IEEE80211_HE_PHY_CAP6_PARTIAL_BANDWIDTH_DL_MUMIMO;
4395 	he_cap_elem->phy_cap_info[6] &= ~m;
4396 
4397 	m = IEEE80211_HE_PHY_CAP7_PSR_BASED_SR |
4398 	    IEEE80211_HE_PHY_CAP7_POWER_BOOST_FACTOR_SUPP |
4399 	    IEEE80211_HE_PHY_CAP7_STBC_TX_ABOVE_80MHZ |
4400 	    IEEE80211_HE_PHY_CAP7_STBC_RX_ABOVE_80MHZ;
4401 	he_cap_elem->phy_cap_info[7] &= ~m;
4402 
4403 	m = IEEE80211_HE_PHY_CAP8_HE_ER_SU_PPDU_4XLTF_AND_08_US_GI |
4404 	    IEEE80211_HE_PHY_CAP8_20MHZ_IN_40MHZ_HE_PPDU_IN_2G |
4405 	    IEEE80211_HE_PHY_CAP8_20MHZ_IN_160MHZ_HE_PPDU |
4406 	    IEEE80211_HE_PHY_CAP8_80MHZ_IN_160MHZ_HE_PPDU;
4407 	he_cap_elem->phy_cap_info[8] &= ~m;
4408 
4409 	m = IEEE80211_HE_PHY_CAP9_LONGER_THAN_16_SIGB_OFDM_SYM |
4410 	    IEEE80211_HE_PHY_CAP9_NON_TRIGGERED_CQI_FEEDBACK |
4411 	    IEEE80211_HE_PHY_CAP9_RX_1024_QAM_LESS_THAN_242_TONE_RU |
4412 	    IEEE80211_HE_PHY_CAP9_TX_1024_QAM_LESS_THAN_242_TONE_RU |
4413 	    IEEE80211_HE_PHY_CAP9_RX_FULL_BW_SU_USING_MU_WITH_COMP_SIGB |
4414 	    IEEE80211_HE_PHY_CAP9_RX_FULL_BW_SU_USING_MU_WITH_NON_COMP_SIGB;
4415 	he_cap_elem->phy_cap_info[9] &= ~m;
4416 }
4417 
4418 static __le16 ath12k_mac_setup_he_6ghz_cap(struct ath12k_pdev_cap *pcap,
4419 					   struct ath12k_band_cap *bcap)
4420 {
4421 	u8 val;
4422 
4423 	bcap->he_6ghz_capa = IEEE80211_HT_MPDU_DENSITY_NONE;
4424 	if (bcap->ht_cap_info & WMI_HT_CAP_DYNAMIC_SMPS)
4425 		bcap->he_6ghz_capa |=
4426 			u32_encode_bits(WLAN_HT_CAP_SM_PS_DYNAMIC,
4427 					IEEE80211_HE_6GHZ_CAP_SM_PS);
4428 	else
4429 		bcap->he_6ghz_capa |=
4430 			u32_encode_bits(WLAN_HT_CAP_SM_PS_DISABLED,
4431 					IEEE80211_HE_6GHZ_CAP_SM_PS);
4432 	val = u32_get_bits(pcap->vht_cap,
4433 			   IEEE80211_VHT_CAP_MAX_A_MPDU_LENGTH_EXPONENT_MASK);
4434 	bcap->he_6ghz_capa |=
4435 		u32_encode_bits(val, IEEE80211_HE_6GHZ_CAP_MAX_AMPDU_LEN_EXP);
4436 	val = u32_get_bits(pcap->vht_cap,
4437 			   IEEE80211_VHT_CAP_MAX_MPDU_MASK);
4438 	bcap->he_6ghz_capa |=
4439 		u32_encode_bits(val, IEEE80211_HE_6GHZ_CAP_MAX_MPDU_LEN);
4440 	if (pcap->vht_cap & IEEE80211_VHT_CAP_RX_ANTENNA_PATTERN)
4441 		bcap->he_6ghz_capa |= IEEE80211_HE_6GHZ_CAP_RX_ANTPAT_CONS;
4442 	if (pcap->vht_cap & IEEE80211_VHT_CAP_TX_ANTENNA_PATTERN)
4443 		bcap->he_6ghz_capa |= IEEE80211_HE_6GHZ_CAP_TX_ANTPAT_CONS;
4444 
4445 	return cpu_to_le16(bcap->he_6ghz_capa);
4446 }
4447 
4448 static void ath12k_mac_copy_he_cap(struct ath12k_band_cap *band_cap,
4449 				   int iftype, u8 num_tx_chains,
4450 				   struct ieee80211_sta_he_cap *he_cap)
4451 {
4452 	struct ieee80211_he_cap_elem *he_cap_elem = &he_cap->he_cap_elem;
4453 	struct ieee80211_he_mcs_nss_supp *mcs_nss = &he_cap->he_mcs_nss_supp;
4454 
4455 	he_cap->has_he = true;
4456 	memcpy(he_cap_elem->mac_cap_info, band_cap->he_cap_info,
4457 	       sizeof(he_cap_elem->mac_cap_info));
4458 	memcpy(he_cap_elem->phy_cap_info, band_cap->he_cap_phy_info,
4459 	       sizeof(he_cap_elem->phy_cap_info));
4460 
4461 	he_cap_elem->mac_cap_info[1] &=
4462 		IEEE80211_HE_MAC_CAP1_TF_MAC_PAD_DUR_MASK;
4463 
4464 	he_cap_elem->phy_cap_info[5] &=
4465 		~IEEE80211_HE_PHY_CAP5_BEAMFORMEE_NUM_SND_DIM_UNDER_80MHZ_MASK;
4466 	he_cap_elem->phy_cap_info[5] &=
4467 		~IEEE80211_HE_PHY_CAP5_BEAMFORMEE_NUM_SND_DIM_ABOVE_80MHZ_MASK;
4468 	he_cap_elem->phy_cap_info[5] |= num_tx_chains - 1;
4469 
4470 	switch (iftype) {
4471 	case NL80211_IFTYPE_AP:
4472 		he_cap_elem->phy_cap_info[3] &=
4473 			~IEEE80211_HE_PHY_CAP3_DCM_MAX_CONST_TX_MASK;
4474 		he_cap_elem->phy_cap_info[9] |=
4475 			IEEE80211_HE_PHY_CAP9_RX_1024_QAM_LESS_THAN_242_TONE_RU;
4476 		break;
4477 	case NL80211_IFTYPE_STATION:
4478 		he_cap_elem->mac_cap_info[0] &= ~IEEE80211_HE_MAC_CAP0_TWT_RES;
4479 		he_cap_elem->mac_cap_info[0] |= IEEE80211_HE_MAC_CAP0_TWT_REQ;
4480 		he_cap_elem->phy_cap_info[9] |=
4481 			IEEE80211_HE_PHY_CAP9_TX_1024_QAM_LESS_THAN_242_TONE_RU;
4482 		break;
4483 	case NL80211_IFTYPE_MESH_POINT:
4484 		ath12k_mac_filter_he_cap_mesh(he_cap_elem);
4485 		break;
4486 	}
4487 
4488 	mcs_nss->rx_mcs_80 = cpu_to_le16(band_cap->he_mcs & 0xffff);
4489 	mcs_nss->tx_mcs_80 = cpu_to_le16(band_cap->he_mcs & 0xffff);
4490 	mcs_nss->rx_mcs_160 = cpu_to_le16((band_cap->he_mcs >> 16) & 0xffff);
4491 	mcs_nss->tx_mcs_160 = cpu_to_le16((band_cap->he_mcs >> 16) & 0xffff);
4492 	mcs_nss->rx_mcs_80p80 = cpu_to_le16((band_cap->he_mcs >> 16) & 0xffff);
4493 	mcs_nss->tx_mcs_80p80 = cpu_to_le16((band_cap->he_mcs >> 16) & 0xffff);
4494 
4495 	memset(he_cap->ppe_thres, 0, sizeof(he_cap->ppe_thres));
4496 	if (he_cap_elem->phy_cap_info[6] &
4497 	    IEEE80211_HE_PHY_CAP6_PPE_THRESHOLD_PRESENT)
4498 		ath12k_gen_ppe_thresh(&band_cap->he_ppet, he_cap->ppe_thres);
4499 }
4500 
4501 static void
4502 ath12k_mac_copy_eht_mcs_nss(struct ath12k_band_cap *band_cap,
4503 			    struct ieee80211_eht_mcs_nss_supp *mcs_nss,
4504 			    const struct ieee80211_he_cap_elem *he_cap,
4505 			    const struct ieee80211_eht_cap_elem_fixed *eht_cap)
4506 {
4507 	if ((he_cap->phy_cap_info[0] &
4508 	     (IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_40MHZ_IN_2G |
4509 	      IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_40MHZ_80MHZ_IN_5G |
4510 	      IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_160MHZ_IN_5G |
4511 	      IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_80PLUS80_MHZ_IN_5G)) == 0)
4512 		memcpy(&mcs_nss->only_20mhz, &band_cap->eht_mcs_20_only,
4513 		       sizeof(struct ieee80211_eht_mcs_nss_supp_20mhz_only));
4514 
4515 	if (he_cap->phy_cap_info[0] &
4516 	    (IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_40MHZ_IN_2G |
4517 	     IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_40MHZ_80MHZ_IN_5G))
4518 		memcpy(&mcs_nss->bw._80, &band_cap->eht_mcs_80,
4519 		       sizeof(struct ieee80211_eht_mcs_nss_supp_bw));
4520 
4521 	if (he_cap->phy_cap_info[0] &
4522 	    IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_160MHZ_IN_5G)
4523 		memcpy(&mcs_nss->bw._160, &band_cap->eht_mcs_160,
4524 		       sizeof(struct ieee80211_eht_mcs_nss_supp_bw));
4525 
4526 	if (eht_cap->phy_cap_info[0] & IEEE80211_EHT_PHY_CAP0_320MHZ_IN_6GHZ)
4527 		memcpy(&mcs_nss->bw._320, &band_cap->eht_mcs_320,
4528 		       sizeof(struct ieee80211_eht_mcs_nss_supp_bw));
4529 }
4530 
4531 static void ath12k_mac_copy_eht_ppe_thresh(struct ath12k_wmi_ppe_threshold_arg *fw_ppet,
4532 					   struct ieee80211_sta_eht_cap *cap)
4533 {
4534 	u16 bit = IEEE80211_EHT_PPE_THRES_INFO_HEADER_SIZE;
4535 	u8 i, nss, ru, ppet_bit_len_per_ru = IEEE80211_EHT_PPE_THRES_INFO_PPET_SIZE * 2;
4536 
4537 	u8p_replace_bits(&cap->eht_ppe_thres[0], fw_ppet->numss_m1,
4538 			 IEEE80211_EHT_PPE_THRES_NSS_MASK);
4539 
4540 	u16p_replace_bits((u16 *)&cap->eht_ppe_thres[0], fw_ppet->ru_bit_mask,
4541 			  IEEE80211_EHT_PPE_THRES_RU_INDEX_BITMASK_MASK);
4542 
4543 	for (nss = 0; nss <= fw_ppet->numss_m1; nss++) {
4544 		for (ru = 0;
4545 		     ru < hweight16(IEEE80211_EHT_PPE_THRES_RU_INDEX_BITMASK_MASK);
4546 		     ru++) {
4547 			u32 val = 0;
4548 
4549 			if ((fw_ppet->ru_bit_mask & BIT(ru)) == 0)
4550 				continue;
4551 
4552 			u32p_replace_bits(&val, fw_ppet->ppet16_ppet8_ru3_ru0[nss] >>
4553 						(ru * ppet_bit_len_per_ru),
4554 					  GENMASK(ppet_bit_len_per_ru - 1, 0));
4555 
4556 			for (i = 0; i < ppet_bit_len_per_ru; i++) {
4557 				cap->eht_ppe_thres[bit / 8] |=
4558 					(((val >> i) & 0x1) << ((bit % 8)));
4559 				bit++;
4560 			}
4561 		}
4562 	}
4563 }
4564 
4565 static void
4566 ath12k_mac_filter_eht_cap_mesh(struct ieee80211_eht_cap_elem_fixed
4567 			       *eht_cap_elem)
4568 {
4569 	u8 m;
4570 
4571 	m = IEEE80211_EHT_MAC_CAP0_EPCS_PRIO_ACCESS;
4572 	eht_cap_elem->mac_cap_info[0] &= ~m;
4573 
4574 	m = IEEE80211_EHT_PHY_CAP0_PARTIAL_BW_UL_MU_MIMO;
4575 	eht_cap_elem->phy_cap_info[0] &= ~m;
4576 
4577 	m = IEEE80211_EHT_PHY_CAP3_NG_16_MU_FEEDBACK |
4578 	    IEEE80211_EHT_PHY_CAP3_CODEBOOK_7_5_MU_FDBK |
4579 	    IEEE80211_EHT_PHY_CAP3_TRIG_MU_BF_PART_BW_FDBK |
4580 	    IEEE80211_EHT_PHY_CAP3_TRIG_CQI_FDBK;
4581 	eht_cap_elem->phy_cap_info[3] &= ~m;
4582 
4583 	m = IEEE80211_EHT_PHY_CAP4_PART_BW_DL_MU_MIMO |
4584 	    IEEE80211_EHT_PHY_CAP4_PSR_SR_SUPP |
4585 	    IEEE80211_EHT_PHY_CAP4_POWER_BOOST_FACT_SUPP |
4586 	    IEEE80211_EHT_PHY_CAP4_EHT_MU_PPDU_4_EHT_LTF_08_GI;
4587 	eht_cap_elem->phy_cap_info[4] &= ~m;
4588 
4589 	m = IEEE80211_EHT_PHY_CAP5_NON_TRIG_CQI_FEEDBACK |
4590 	    IEEE80211_EHT_PHY_CAP5_TX_LESS_242_TONE_RU_SUPP |
4591 	    IEEE80211_EHT_PHY_CAP5_RX_LESS_242_TONE_RU_SUPP |
4592 	    IEEE80211_EHT_PHY_CAP5_MAX_NUM_SUPP_EHT_LTF_MASK;
4593 	eht_cap_elem->phy_cap_info[5] &= ~m;
4594 
4595 	m = IEEE80211_EHT_PHY_CAP6_MAX_NUM_SUPP_EHT_LTF_MASK;
4596 	eht_cap_elem->phy_cap_info[6] &= ~m;
4597 
4598 	m = IEEE80211_EHT_PHY_CAP7_NON_OFDMA_UL_MU_MIMO_80MHZ |
4599 	    IEEE80211_EHT_PHY_CAP7_NON_OFDMA_UL_MU_MIMO_160MHZ |
4600 	    IEEE80211_EHT_PHY_CAP7_NON_OFDMA_UL_MU_MIMO_320MHZ |
4601 	    IEEE80211_EHT_PHY_CAP7_MU_BEAMFORMER_80MHZ |
4602 	    IEEE80211_EHT_PHY_CAP7_MU_BEAMFORMER_160MHZ |
4603 	    IEEE80211_EHT_PHY_CAP7_MU_BEAMFORMER_320MHZ;
4604 	eht_cap_elem->phy_cap_info[7] &= ~m;
4605 }
4606 
4607 static void ath12k_mac_copy_eht_cap(struct ath12k *ar,
4608 				    struct ath12k_band_cap *band_cap,
4609 				    struct ieee80211_he_cap_elem *he_cap_elem,
4610 				    int iftype,
4611 				    struct ieee80211_sta_eht_cap *eht_cap)
4612 {
4613 	struct ieee80211_eht_cap_elem_fixed *eht_cap_elem = &eht_cap->eht_cap_elem;
4614 
4615 	memset(eht_cap, 0, sizeof(struct ieee80211_sta_eht_cap));
4616 
4617 	if (!(test_bit(WMI_TLV_SERVICE_11BE, ar->ab->wmi_ab.svc_map)))
4618 		return;
4619 
4620 	eht_cap->has_eht = true;
4621 	memcpy(eht_cap_elem->mac_cap_info, band_cap->eht_cap_mac_info,
4622 	       sizeof(eht_cap_elem->mac_cap_info));
4623 	memcpy(eht_cap_elem->phy_cap_info, band_cap->eht_cap_phy_info,
4624 	       sizeof(eht_cap_elem->phy_cap_info));
4625 
4626 	switch (iftype) {
4627 	case NL80211_IFTYPE_AP:
4628 		eht_cap_elem->phy_cap_info[0] &=
4629 			~IEEE80211_EHT_PHY_CAP0_242_TONE_RU_GT20MHZ;
4630 		eht_cap_elem->phy_cap_info[4] &=
4631 			~IEEE80211_EHT_PHY_CAP4_PART_BW_DL_MU_MIMO;
4632 		eht_cap_elem->phy_cap_info[5] &=
4633 			~IEEE80211_EHT_PHY_CAP5_TX_LESS_242_TONE_RU_SUPP;
4634 		break;
4635 	case NL80211_IFTYPE_STATION:
4636 		eht_cap_elem->phy_cap_info[7] &=
4637 			~(IEEE80211_EHT_PHY_CAP7_NON_OFDMA_UL_MU_MIMO_80MHZ |
4638 			  IEEE80211_EHT_PHY_CAP7_NON_OFDMA_UL_MU_MIMO_160MHZ |
4639 			  IEEE80211_EHT_PHY_CAP7_NON_OFDMA_UL_MU_MIMO_320MHZ);
4640 		eht_cap_elem->phy_cap_info[7] &=
4641 			~(IEEE80211_EHT_PHY_CAP7_MU_BEAMFORMER_80MHZ |
4642 			  IEEE80211_EHT_PHY_CAP7_MU_BEAMFORMER_160MHZ |
4643 			  IEEE80211_EHT_PHY_CAP7_MU_BEAMFORMER_320MHZ);
4644 		break;
4645 	case NL80211_IFTYPE_MESH_POINT:
4646 		ath12k_mac_filter_eht_cap_mesh(eht_cap_elem);
4647 		break;
4648 	default:
4649 		break;
4650 	}
4651 
4652 	ath12k_mac_copy_eht_mcs_nss(band_cap, &eht_cap->eht_mcs_nss_supp,
4653 				    he_cap_elem, eht_cap_elem);
4654 
4655 	if (eht_cap_elem->phy_cap_info[5] &
4656 	    IEEE80211_EHT_PHY_CAP5_PPE_THRESHOLD_PRESENT)
4657 		ath12k_mac_copy_eht_ppe_thresh(&band_cap->eht_ppet, eht_cap);
4658 }
4659 
4660 static int ath12k_mac_copy_sband_iftype_data(struct ath12k *ar,
4661 					     struct ath12k_pdev_cap *cap,
4662 					     struct ieee80211_sband_iftype_data *data,
4663 					     int band)
4664 {
4665 	struct ath12k_band_cap *band_cap = &cap->band[band];
4666 	int i, idx = 0;
4667 
4668 	for (i = 0; i < NUM_NL80211_IFTYPES; i++) {
4669 		struct ieee80211_sta_he_cap *he_cap = &data[idx].he_cap;
4670 
4671 		switch (i) {
4672 		case NL80211_IFTYPE_STATION:
4673 		case NL80211_IFTYPE_AP:
4674 		case NL80211_IFTYPE_MESH_POINT:
4675 			break;
4676 
4677 		default:
4678 			continue;
4679 		}
4680 
4681 		data[idx].types_mask = BIT(i);
4682 
4683 		ath12k_mac_copy_he_cap(band_cap, i, ar->num_tx_chains, he_cap);
4684 		if (band == NL80211_BAND_6GHZ) {
4685 			data[idx].he_6ghz_capa.capa =
4686 				ath12k_mac_setup_he_6ghz_cap(cap, band_cap);
4687 		}
4688 		ath12k_mac_copy_eht_cap(ar, band_cap, &he_cap->he_cap_elem, i,
4689 					&data[idx].eht_cap);
4690 		idx++;
4691 	}
4692 
4693 	return idx;
4694 }
4695 
4696 static void ath12k_mac_setup_sband_iftype_data(struct ath12k *ar,
4697 					       struct ath12k_pdev_cap *cap)
4698 {
4699 	struct ieee80211_supported_band *sband;
4700 	enum nl80211_band band;
4701 	int count;
4702 
4703 	if (cap->supported_bands & WMI_HOST_WLAN_2G_CAP) {
4704 		band = NL80211_BAND_2GHZ;
4705 		count = ath12k_mac_copy_sband_iftype_data(ar, cap,
4706 							  ar->mac.iftype[band],
4707 							  band);
4708 		sband = &ar->mac.sbands[band];
4709 		_ieee80211_set_sband_iftype_data(sband, ar->mac.iftype[band],
4710 						 count);
4711 	}
4712 
4713 	if (cap->supported_bands & WMI_HOST_WLAN_5G_CAP) {
4714 		band = NL80211_BAND_5GHZ;
4715 		count = ath12k_mac_copy_sband_iftype_data(ar, cap,
4716 							  ar->mac.iftype[band],
4717 							  band);
4718 		sband = &ar->mac.sbands[band];
4719 		_ieee80211_set_sband_iftype_data(sband, ar->mac.iftype[band],
4720 						 count);
4721 	}
4722 
4723 	if (cap->supported_bands & WMI_HOST_WLAN_5G_CAP &&
4724 	    ar->supports_6ghz) {
4725 		band = NL80211_BAND_6GHZ;
4726 		count = ath12k_mac_copy_sband_iftype_data(ar, cap,
4727 							  ar->mac.iftype[band],
4728 							  band);
4729 		sband = &ar->mac.sbands[band];
4730 		_ieee80211_set_sband_iftype_data(sband, ar->mac.iftype[band],
4731 						 count);
4732 	}
4733 }
4734 
4735 static int __ath12k_set_antenna(struct ath12k *ar, u32 tx_ant, u32 rx_ant)
4736 {
4737 	int ret;
4738 
4739 	lockdep_assert_held(&ar->conf_mutex);
4740 
4741 	if (ath12k_check_chain_mask(ar, tx_ant, true))
4742 		return -EINVAL;
4743 
4744 	if (ath12k_check_chain_mask(ar, rx_ant, false))
4745 		return -EINVAL;
4746 
4747 	ar->cfg_tx_chainmask = tx_ant;
4748 	ar->cfg_rx_chainmask = rx_ant;
4749 
4750 	if (ar->state != ATH12K_STATE_ON &&
4751 	    ar->state != ATH12K_STATE_RESTARTED)
4752 		return 0;
4753 
4754 	ret = ath12k_wmi_pdev_set_param(ar, WMI_PDEV_PARAM_TX_CHAIN_MASK,
4755 					tx_ant, ar->pdev->pdev_id);
4756 	if (ret) {
4757 		ath12k_warn(ar->ab, "failed to set tx-chainmask: %d, req 0x%x\n",
4758 			    ret, tx_ant);
4759 		return ret;
4760 	}
4761 
4762 	ar->num_tx_chains = hweight32(tx_ant);
4763 
4764 	ret = ath12k_wmi_pdev_set_param(ar, WMI_PDEV_PARAM_RX_CHAIN_MASK,
4765 					rx_ant, ar->pdev->pdev_id);
4766 	if (ret) {
4767 		ath12k_warn(ar->ab, "failed to set rx-chainmask: %d, req 0x%x\n",
4768 			    ret, rx_ant);
4769 		return ret;
4770 	}
4771 
4772 	ar->num_rx_chains = hweight32(rx_ant);
4773 
4774 	/* Reload HT/VHT/HE capability */
4775 	ath12k_mac_setup_ht_vht_cap(ar, &ar->pdev->cap, NULL);
4776 	ath12k_mac_setup_sband_iftype_data(ar, &ar->pdev->cap);
4777 
4778 	return 0;
4779 }
4780 
4781 static void ath12k_mgmt_over_wmi_tx_drop(struct ath12k *ar, struct sk_buff *skb)
4782 {
4783 	int num_mgmt;
4784 
4785 	ieee80211_free_txskb(ar->hw, skb);
4786 
4787 	num_mgmt = atomic_dec_if_positive(&ar->num_pending_mgmt_tx);
4788 
4789 	if (num_mgmt < 0)
4790 		WARN_ON_ONCE(1);
4791 
4792 	if (!num_mgmt)
4793 		wake_up(&ar->txmgmt_empty_waitq);
4794 }
4795 
4796 int ath12k_mac_tx_mgmt_pending_free(int buf_id, void *skb, void *ctx)
4797 {
4798 	struct sk_buff *msdu = skb;
4799 	struct ieee80211_tx_info *info;
4800 	struct ath12k *ar = ctx;
4801 	struct ath12k_base *ab = ar->ab;
4802 
4803 	spin_lock_bh(&ar->txmgmt_idr_lock);
4804 	idr_remove(&ar->txmgmt_idr, buf_id);
4805 	spin_unlock_bh(&ar->txmgmt_idr_lock);
4806 	dma_unmap_single(ab->dev, ATH12K_SKB_CB(msdu)->paddr, msdu->len,
4807 			 DMA_TO_DEVICE);
4808 
4809 	info = IEEE80211_SKB_CB(msdu);
4810 	memset(&info->status, 0, sizeof(info->status));
4811 
4812 	ath12k_mgmt_over_wmi_tx_drop(ar, skb);
4813 
4814 	return 0;
4815 }
4816 
4817 static int ath12k_mac_vif_txmgmt_idr_remove(int buf_id, void *skb, void *ctx)
4818 {
4819 	struct ieee80211_vif *vif = ctx;
4820 	struct ath12k_skb_cb *skb_cb = ATH12K_SKB_CB(skb);
4821 	struct sk_buff *msdu = skb;
4822 	struct ath12k *ar = skb_cb->ar;
4823 	struct ath12k_base *ab = ar->ab;
4824 
4825 	if (skb_cb->vif == vif) {
4826 		spin_lock_bh(&ar->txmgmt_idr_lock);
4827 		idr_remove(&ar->txmgmt_idr, buf_id);
4828 		spin_unlock_bh(&ar->txmgmt_idr_lock);
4829 		dma_unmap_single(ab->dev, skb_cb->paddr, msdu->len,
4830 				 DMA_TO_DEVICE);
4831 	}
4832 
4833 	return 0;
4834 }
4835 
4836 static int ath12k_mac_mgmt_tx_wmi(struct ath12k *ar, struct ath12k_vif *arvif,
4837 				  struct sk_buff *skb)
4838 {
4839 	struct ath12k_base *ab = ar->ab;
4840 	struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
4841 	struct ieee80211_tx_info *info;
4842 	dma_addr_t paddr;
4843 	int buf_id;
4844 	int ret;
4845 
4846 	ATH12K_SKB_CB(skb)->ar = ar;
4847 	spin_lock_bh(&ar->txmgmt_idr_lock);
4848 	buf_id = idr_alloc(&ar->txmgmt_idr, skb, 0,
4849 			   ATH12K_TX_MGMT_NUM_PENDING_MAX, GFP_ATOMIC);
4850 	spin_unlock_bh(&ar->txmgmt_idr_lock);
4851 	if (buf_id < 0)
4852 		return -ENOSPC;
4853 
4854 	info = IEEE80211_SKB_CB(skb);
4855 	if (!(info->flags & IEEE80211_TX_CTL_HW_80211_ENCAP)) {
4856 		if ((ieee80211_is_action(hdr->frame_control) ||
4857 		     ieee80211_is_deauth(hdr->frame_control) ||
4858 		     ieee80211_is_disassoc(hdr->frame_control)) &&
4859 		     ieee80211_has_protected(hdr->frame_control)) {
4860 			skb_put(skb, IEEE80211_CCMP_MIC_LEN);
4861 		}
4862 	}
4863 
4864 	paddr = dma_map_single(ab->dev, skb->data, skb->len, DMA_TO_DEVICE);
4865 	if (dma_mapping_error(ab->dev, paddr)) {
4866 		ath12k_warn(ab, "failed to DMA map mgmt Tx buffer\n");
4867 		ret = -EIO;
4868 		goto err_free_idr;
4869 	}
4870 
4871 	ATH12K_SKB_CB(skb)->paddr = paddr;
4872 
4873 	ret = ath12k_wmi_mgmt_send(ar, arvif->vdev_id, buf_id, skb);
4874 	if (ret) {
4875 		ath12k_warn(ar->ab, "failed to send mgmt frame: %d\n", ret);
4876 		goto err_unmap_buf;
4877 	}
4878 
4879 	return 0;
4880 
4881 err_unmap_buf:
4882 	dma_unmap_single(ab->dev, ATH12K_SKB_CB(skb)->paddr,
4883 			 skb->len, DMA_TO_DEVICE);
4884 err_free_idr:
4885 	spin_lock_bh(&ar->txmgmt_idr_lock);
4886 	idr_remove(&ar->txmgmt_idr, buf_id);
4887 	spin_unlock_bh(&ar->txmgmt_idr_lock);
4888 
4889 	return ret;
4890 }
4891 
4892 static void ath12k_mgmt_over_wmi_tx_purge(struct ath12k *ar)
4893 {
4894 	struct sk_buff *skb;
4895 
4896 	while ((skb = skb_dequeue(&ar->wmi_mgmt_tx_queue)) != NULL)
4897 		ath12k_mgmt_over_wmi_tx_drop(ar, skb);
4898 }
4899 
4900 static void ath12k_mgmt_over_wmi_tx_work(struct work_struct *work)
4901 {
4902 	struct ath12k *ar = container_of(work, struct ath12k, wmi_mgmt_tx_work);
4903 	struct ath12k_skb_cb *skb_cb;
4904 	struct ath12k_vif *arvif;
4905 	struct sk_buff *skb;
4906 	int ret;
4907 
4908 	while ((skb = skb_dequeue(&ar->wmi_mgmt_tx_queue)) != NULL) {
4909 		skb_cb = ATH12K_SKB_CB(skb);
4910 		if (!skb_cb->vif) {
4911 			ath12k_warn(ar->ab, "no vif found for mgmt frame\n");
4912 			ath12k_mgmt_over_wmi_tx_drop(ar, skb);
4913 			continue;
4914 		}
4915 
4916 		arvif = ath12k_vif_to_arvif(skb_cb->vif);
4917 		if (ar->allocated_vdev_map & (1LL << arvif->vdev_id) &&
4918 		    arvif->is_started) {
4919 			ret = ath12k_mac_mgmt_tx_wmi(ar, arvif, skb);
4920 			if (ret) {
4921 				ath12k_warn(ar->ab, "failed to tx mgmt frame, vdev_id %d :%d\n",
4922 					    arvif->vdev_id, ret);
4923 				ath12k_mgmt_over_wmi_tx_drop(ar, skb);
4924 			}
4925 		} else {
4926 			ath12k_warn(ar->ab,
4927 				    "dropping mgmt frame for vdev %d, is_started %d\n",
4928 				    arvif->vdev_id,
4929 				    arvif->is_started);
4930 			ath12k_mgmt_over_wmi_tx_drop(ar, skb);
4931 		}
4932 	}
4933 }
4934 
4935 static int ath12k_mac_mgmt_tx(struct ath12k *ar, struct sk_buff *skb,
4936 			      bool is_prb_rsp)
4937 {
4938 	struct sk_buff_head *q = &ar->wmi_mgmt_tx_queue;
4939 
4940 	if (test_bit(ATH12K_FLAG_CRASH_FLUSH, &ar->ab->dev_flags))
4941 		return -ESHUTDOWN;
4942 
4943 	/* Drop probe response packets when the pending management tx
4944 	 * count has reached a certain threshold, so as to prioritize
4945 	 * other mgmt packets like auth and assoc to be sent on time
4946 	 * for establishing successful connections.
4947 	 */
4948 	if (is_prb_rsp &&
4949 	    atomic_read(&ar->num_pending_mgmt_tx) > ATH12K_PRB_RSP_DROP_THRESHOLD) {
4950 		ath12k_warn(ar->ab,
4951 			    "dropping probe response as pending queue is almost full\n");
4952 		return -ENOSPC;
4953 	}
4954 
4955 	if (skb_queue_len_lockless(q) >= ATH12K_TX_MGMT_NUM_PENDING_MAX) {
4956 		ath12k_warn(ar->ab, "mgmt tx queue is full\n");
4957 		return -ENOSPC;
4958 	}
4959 
4960 	skb_queue_tail(q, skb);
4961 	atomic_inc(&ar->num_pending_mgmt_tx);
4962 	ieee80211_queue_work(ar->hw, &ar->wmi_mgmt_tx_work);
4963 
4964 	return 0;
4965 }
4966 
4967 static void ath12k_mac_op_tx(struct ieee80211_hw *hw,
4968 			     struct ieee80211_tx_control *control,
4969 			     struct sk_buff *skb)
4970 {
4971 	struct ath12k_skb_cb *skb_cb = ATH12K_SKB_CB(skb);
4972 	struct ath12k *ar = hw->priv;
4973 	struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
4974 	struct ieee80211_vif *vif = info->control.vif;
4975 	struct ath12k_vif *arvif = ath12k_vif_to_arvif(vif);
4976 	struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
4977 	struct ieee80211_key_conf *key = info->control.hw_key;
4978 	u32 info_flags = info->flags;
4979 	bool is_prb_rsp;
4980 	int ret;
4981 
4982 	memset(skb_cb, 0, sizeof(*skb_cb));
4983 	skb_cb->vif = vif;
4984 
4985 	if (key) {
4986 		skb_cb->cipher = key->cipher;
4987 		skb_cb->flags |= ATH12K_SKB_CIPHER_SET;
4988 	}
4989 
4990 	if (info_flags & IEEE80211_TX_CTL_HW_80211_ENCAP) {
4991 		skb_cb->flags |= ATH12K_SKB_HW_80211_ENCAP;
4992 	} else if (ieee80211_is_mgmt(hdr->frame_control)) {
4993 		is_prb_rsp = ieee80211_is_probe_resp(hdr->frame_control);
4994 		ret = ath12k_mac_mgmt_tx(ar, skb, is_prb_rsp);
4995 		if (ret) {
4996 			ath12k_warn(ar->ab, "failed to queue management frame %d\n",
4997 				    ret);
4998 			ieee80211_free_txskb(hw, skb);
4999 		}
5000 		return;
5001 	}
5002 
5003 	ret = ath12k_dp_tx(ar, arvif, skb);
5004 	if (ret) {
5005 		ath12k_warn(ar->ab, "failed to transmit frame %d\n", ret);
5006 		ieee80211_free_txskb(hw, skb);
5007 	}
5008 }
5009 
5010 void ath12k_mac_drain_tx(struct ath12k *ar)
5011 {
5012 	/* make sure rcu-protected mac80211 tx path itself is drained */
5013 	synchronize_net();
5014 
5015 	cancel_work_sync(&ar->wmi_mgmt_tx_work);
5016 	ath12k_mgmt_over_wmi_tx_purge(ar);
5017 }
5018 
5019 static int ath12k_mac_config_mon_status_default(struct ath12k *ar, bool enable)
5020 {
5021 	return -ENOTSUPP;
5022 	/* TODO: Need to support new monitor mode */
5023 }
5024 
5025 static void ath12k_mac_wait_reconfigure(struct ath12k_base *ab)
5026 {
5027 	int recovery_start_count;
5028 
5029 	if (!ab->is_reset)
5030 		return;
5031 
5032 	recovery_start_count = atomic_inc_return(&ab->recovery_start_count);
5033 
5034 	ath12k_dbg(ab, ATH12K_DBG_MAC, "recovery start count %d\n", recovery_start_count);
5035 
5036 	if (recovery_start_count == ab->num_radios) {
5037 		complete(&ab->recovery_start);
5038 		ath12k_dbg(ab, ATH12K_DBG_MAC, "recovery started success\n");
5039 	}
5040 
5041 	ath12k_dbg(ab, ATH12K_DBG_MAC, "waiting reconfigure...\n");
5042 
5043 	wait_for_completion_timeout(&ab->reconfigure_complete,
5044 				    ATH12K_RECONFIGURE_TIMEOUT_HZ);
5045 }
5046 
5047 static int ath12k_mac_op_start(struct ieee80211_hw *hw)
5048 {
5049 	struct ath12k *ar = hw->priv;
5050 	struct ath12k_base *ab = ar->ab;
5051 	struct ath12k_pdev *pdev = ar->pdev;
5052 	int ret;
5053 
5054 	ath12k_mac_drain_tx(ar);
5055 	mutex_lock(&ar->conf_mutex);
5056 
5057 	switch (ar->state) {
5058 	case ATH12K_STATE_OFF:
5059 		ar->state = ATH12K_STATE_ON;
5060 		break;
5061 	case ATH12K_STATE_RESTARTING:
5062 		ar->state = ATH12K_STATE_RESTARTED;
5063 		ath12k_mac_wait_reconfigure(ab);
5064 		break;
5065 	case ATH12K_STATE_RESTARTED:
5066 	case ATH12K_STATE_WEDGED:
5067 	case ATH12K_STATE_ON:
5068 		WARN_ON(1);
5069 		ret = -EINVAL;
5070 		goto err;
5071 	}
5072 
5073 	ret = ath12k_wmi_pdev_set_param(ar, WMI_PDEV_PARAM_PMF_QOS,
5074 					1, pdev->pdev_id);
5075 
5076 	if (ret) {
5077 		ath12k_err(ar->ab, "failed to enable PMF QOS: (%d\n", ret);
5078 		goto err;
5079 	}
5080 
5081 	ret = ath12k_wmi_pdev_set_param(ar, WMI_PDEV_PARAM_DYNAMIC_BW, 1,
5082 					pdev->pdev_id);
5083 	if (ret) {
5084 		ath12k_err(ar->ab, "failed to enable dynamic bw: %d\n", ret);
5085 		goto err;
5086 	}
5087 
5088 	ret = ath12k_wmi_pdev_set_param(ar, WMI_PDEV_PARAM_ARP_AC_OVERRIDE,
5089 					0, pdev->pdev_id);
5090 	if (ret) {
5091 		ath12k_err(ab, "failed to set ac override for ARP: %d\n",
5092 			   ret);
5093 		goto err;
5094 	}
5095 
5096 	ret = ath12k_wmi_send_dfs_phyerr_offload_enable_cmd(ar, pdev->pdev_id);
5097 	if (ret) {
5098 		ath12k_err(ab, "failed to offload radar detection: %d\n",
5099 			   ret);
5100 		goto err;
5101 	}
5102 
5103 	ret = ath12k_dp_tx_htt_h2t_ppdu_stats_req(ar,
5104 						  HTT_PPDU_STATS_TAG_DEFAULT);
5105 	if (ret) {
5106 		ath12k_err(ab, "failed to req ppdu stats: %d\n", ret);
5107 		goto err;
5108 	}
5109 
5110 	ret = ath12k_wmi_pdev_set_param(ar, WMI_PDEV_PARAM_MESH_MCAST_ENABLE,
5111 					1, pdev->pdev_id);
5112 
5113 	if (ret) {
5114 		ath12k_err(ar->ab, "failed to enable MESH MCAST ENABLE: (%d\n", ret);
5115 		goto err;
5116 	}
5117 
5118 	__ath12k_set_antenna(ar, ar->cfg_tx_chainmask, ar->cfg_rx_chainmask);
5119 
5120 	/* TODO: Do we need to enable ANI? */
5121 
5122 	ath12k_reg_update_chan_list(ar);
5123 
5124 	ar->num_started_vdevs = 0;
5125 	ar->num_created_vdevs = 0;
5126 	ar->num_peers = 0;
5127 	ar->allocated_vdev_map = 0;
5128 
5129 	/* Configure monitor status ring with default rx_filter to get rx status
5130 	 * such as rssi, rx_duration.
5131 	 */
5132 	ret = ath12k_mac_config_mon_status_default(ar, true);
5133 	if (ret && (ret != -ENOTSUPP)) {
5134 		ath12k_err(ab, "failed to configure monitor status ring with default rx_filter: (%d)\n",
5135 			   ret);
5136 		goto err;
5137 	}
5138 
5139 	if (ret == -ENOTSUPP)
5140 		ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
5141 			   "monitor status config is not yet supported");
5142 
5143 	/* Configure the hash seed for hash based reo dest ring selection */
5144 	ath12k_wmi_pdev_lro_cfg(ar, ar->pdev->pdev_id);
5145 
5146 	/* allow device to enter IMPS */
5147 	if (ab->hw_params->idle_ps) {
5148 		ret = ath12k_wmi_pdev_set_param(ar, WMI_PDEV_PARAM_IDLE_PS_CONFIG,
5149 						1, pdev->pdev_id);
5150 		if (ret) {
5151 			ath12k_err(ab, "failed to enable idle ps: %d\n", ret);
5152 			goto err;
5153 		}
5154 	}
5155 
5156 	mutex_unlock(&ar->conf_mutex);
5157 
5158 	rcu_assign_pointer(ab->pdevs_active[ar->pdev_idx],
5159 			   &ab->pdevs[ar->pdev_idx]);
5160 
5161 	return 0;
5162 
5163 err:
5164 	ar->state = ATH12K_STATE_OFF;
5165 	mutex_unlock(&ar->conf_mutex);
5166 
5167 	return ret;
5168 }
5169 
5170 int ath12k_mac_rfkill_config(struct ath12k *ar)
5171 {
5172 	struct ath12k_base *ab = ar->ab;
5173 	u32 param;
5174 	int ret;
5175 
5176 	if (ab->hw_params->rfkill_pin == 0)
5177 		return -EOPNOTSUPP;
5178 
5179 	ath12k_dbg(ab, ATH12K_DBG_MAC,
5180 		   "mac rfkill_pin %d rfkill_cfg %d rfkill_on_level %d",
5181 		   ab->hw_params->rfkill_pin, ab->hw_params->rfkill_cfg,
5182 		   ab->hw_params->rfkill_on_level);
5183 
5184 	param = u32_encode_bits(ab->hw_params->rfkill_on_level,
5185 				WMI_RFKILL_CFG_RADIO_LEVEL) |
5186 		u32_encode_bits(ab->hw_params->rfkill_pin,
5187 				WMI_RFKILL_CFG_GPIO_PIN_NUM) |
5188 		u32_encode_bits(ab->hw_params->rfkill_cfg,
5189 				WMI_RFKILL_CFG_PIN_AS_GPIO);
5190 
5191 	ret = ath12k_wmi_pdev_set_param(ar, WMI_PDEV_PARAM_HW_RFKILL_CONFIG,
5192 					param, ar->pdev->pdev_id);
5193 	if (ret) {
5194 		ath12k_warn(ab,
5195 			    "failed to set rfkill config 0x%x: %d\n",
5196 			    param, ret);
5197 		return ret;
5198 	}
5199 
5200 	return 0;
5201 }
5202 
5203 int ath12k_mac_rfkill_enable_radio(struct ath12k *ar, bool enable)
5204 {
5205 	enum wmi_rfkill_enable_radio param;
5206 	int ret;
5207 
5208 	if (enable)
5209 		param = WMI_RFKILL_ENABLE_RADIO_ON;
5210 	else
5211 		param = WMI_RFKILL_ENABLE_RADIO_OFF;
5212 
5213 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac %d rfkill enable %d",
5214 		   ar->pdev_idx, param);
5215 
5216 	ret = ath12k_wmi_pdev_set_param(ar, WMI_PDEV_PARAM_RFKILL_ENABLE,
5217 					param, ar->pdev->pdev_id);
5218 	if (ret) {
5219 		ath12k_warn(ar->ab, "failed to set rfkill enable param %d: %d\n",
5220 			    param, ret);
5221 		return ret;
5222 	}
5223 
5224 	return 0;
5225 }
5226 
5227 static void ath12k_mac_op_stop(struct ieee80211_hw *hw)
5228 {
5229 	struct ath12k *ar = hw->priv;
5230 	struct htt_ppdu_stats_info *ppdu_stats, *tmp;
5231 	int ret;
5232 
5233 	ath12k_mac_drain_tx(ar);
5234 
5235 	mutex_lock(&ar->conf_mutex);
5236 	ret = ath12k_mac_config_mon_status_default(ar, false);
5237 	if (ret && (ret != -ENOTSUPP))
5238 		ath12k_err(ar->ab, "failed to clear rx_filter for monitor status ring: (%d)\n",
5239 			   ret);
5240 
5241 	clear_bit(ATH12K_CAC_RUNNING, &ar->dev_flags);
5242 	ar->state = ATH12K_STATE_OFF;
5243 	mutex_unlock(&ar->conf_mutex);
5244 
5245 	cancel_delayed_work_sync(&ar->scan.timeout);
5246 	cancel_work_sync(&ar->regd_update_work);
5247 	cancel_work_sync(&ar->ab->rfkill_work);
5248 
5249 	spin_lock_bh(&ar->data_lock);
5250 	list_for_each_entry_safe(ppdu_stats, tmp, &ar->ppdu_stats_info, list) {
5251 		list_del(&ppdu_stats->list);
5252 		kfree(ppdu_stats);
5253 	}
5254 	spin_unlock_bh(&ar->data_lock);
5255 
5256 	rcu_assign_pointer(ar->ab->pdevs_active[ar->pdev_idx], NULL);
5257 
5258 	synchronize_rcu();
5259 
5260 	atomic_set(&ar->num_pending_mgmt_tx, 0);
5261 }
5262 
5263 static u8
5264 ath12k_mac_get_vdev_stats_id(struct ath12k_vif *arvif)
5265 {
5266 	struct ath12k_base *ab = arvif->ar->ab;
5267 	u8 vdev_stats_id = 0;
5268 
5269 	do {
5270 		if (ab->free_vdev_stats_id_map & (1LL << vdev_stats_id)) {
5271 			vdev_stats_id++;
5272 			if (vdev_stats_id <= ATH12K_INVAL_VDEV_STATS_ID) {
5273 				vdev_stats_id = ATH12K_INVAL_VDEV_STATS_ID;
5274 				break;
5275 			}
5276 		} else {
5277 			ab->free_vdev_stats_id_map |= (1LL << vdev_stats_id);
5278 			break;
5279 		}
5280 	} while (vdev_stats_id);
5281 
5282 	arvif->vdev_stats_id = vdev_stats_id;
5283 	return vdev_stats_id;
5284 }
5285 
5286 static void ath12k_mac_setup_vdev_create_arg(struct ath12k_vif *arvif,
5287 					     struct ath12k_wmi_vdev_create_arg *arg)
5288 {
5289 	struct ath12k *ar = arvif->ar;
5290 	struct ath12k_pdev *pdev = ar->pdev;
5291 
5292 	arg->if_id = arvif->vdev_id;
5293 	arg->type = arvif->vdev_type;
5294 	arg->subtype = arvif->vdev_subtype;
5295 	arg->pdev_id = pdev->pdev_id;
5296 
5297 	if (pdev->cap.supported_bands & WMI_HOST_WLAN_2G_CAP) {
5298 		arg->chains[NL80211_BAND_2GHZ].tx = ar->num_tx_chains;
5299 		arg->chains[NL80211_BAND_2GHZ].rx = ar->num_rx_chains;
5300 	}
5301 	if (pdev->cap.supported_bands & WMI_HOST_WLAN_5G_CAP) {
5302 		arg->chains[NL80211_BAND_5GHZ].tx = ar->num_tx_chains;
5303 		arg->chains[NL80211_BAND_5GHZ].rx = ar->num_rx_chains;
5304 	}
5305 	if (pdev->cap.supported_bands & WMI_HOST_WLAN_5G_CAP &&
5306 	    ar->supports_6ghz) {
5307 		arg->chains[NL80211_BAND_6GHZ].tx = ar->num_tx_chains;
5308 		arg->chains[NL80211_BAND_6GHZ].rx = ar->num_rx_chains;
5309 	}
5310 
5311 	arg->if_stats_id = ath12k_mac_get_vdev_stats_id(arvif);
5312 }
5313 
5314 static u32
5315 ath12k_mac_prepare_he_mode(struct ath12k_pdev *pdev, u32 viftype)
5316 {
5317 	struct ath12k_pdev_cap *pdev_cap = &pdev->cap;
5318 	struct ath12k_band_cap *cap_band = NULL;
5319 	u32 *hecap_phy_ptr = NULL;
5320 	u32 hemode;
5321 
5322 	if (pdev->cap.supported_bands & WMI_HOST_WLAN_2G_CAP)
5323 		cap_band = &pdev_cap->band[NL80211_BAND_2GHZ];
5324 	else
5325 		cap_band = &pdev_cap->band[NL80211_BAND_5GHZ];
5326 
5327 	hecap_phy_ptr = &cap_band->he_cap_phy_info[0];
5328 
5329 	hemode = u32_encode_bits(HE_SU_BFEE_ENABLE, HE_MODE_SU_TX_BFEE) |
5330 		 u32_encode_bits(HECAP_PHY_SUBFMR_GET(hecap_phy_ptr),
5331 				 HE_MODE_SU_TX_BFER) |
5332 		 u32_encode_bits(HECAP_PHY_ULMUMIMO_GET(hecap_phy_ptr),
5333 				 HE_MODE_UL_MUMIMO);
5334 
5335 	/* TODO: WDS and other modes */
5336 	if (viftype == NL80211_IFTYPE_AP) {
5337 		hemode |= u32_encode_bits(HECAP_PHY_MUBFMR_GET(hecap_phy_ptr),
5338 					  HE_MODE_MU_TX_BFER) |
5339 			  u32_encode_bits(HE_DL_MUOFDMA_ENABLE, HE_MODE_DL_OFDMA) |
5340 			  u32_encode_bits(HE_UL_MUOFDMA_ENABLE, HE_MODE_UL_OFDMA);
5341 	} else {
5342 		hemode |= u32_encode_bits(HE_MU_BFEE_ENABLE, HE_MODE_MU_TX_BFEE);
5343 	}
5344 
5345 	return hemode;
5346 }
5347 
5348 static int ath12k_set_he_mu_sounding_mode(struct ath12k *ar,
5349 					  struct ath12k_vif *arvif)
5350 {
5351 	u32 param_id, param_value;
5352 	struct ath12k_base *ab = ar->ab;
5353 	int ret;
5354 
5355 	param_id = WMI_VDEV_PARAM_SET_HEMU_MODE;
5356 	param_value = ath12k_mac_prepare_he_mode(ar->pdev, arvif->vif->type);
5357 	ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
5358 					    param_id, param_value);
5359 	if (ret) {
5360 		ath12k_warn(ab, "failed to set vdev %d HE MU mode: %d param_value %x\n",
5361 			    arvif->vdev_id, ret, param_value);
5362 		return ret;
5363 	}
5364 	param_id = WMI_VDEV_PARAM_SET_HE_SOUNDING_MODE;
5365 	param_value =
5366 		u32_encode_bits(HE_VHT_SOUNDING_MODE_ENABLE, HE_VHT_SOUNDING_MODE) |
5367 		u32_encode_bits(HE_TRIG_NONTRIG_SOUNDING_MODE_ENABLE,
5368 				HE_TRIG_NONTRIG_SOUNDING_MODE);
5369 	ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
5370 					    param_id, param_value);
5371 	if (ret) {
5372 		ath12k_warn(ab, "failed to set vdev %d HE MU mode: %d\n",
5373 			    arvif->vdev_id, ret);
5374 		return ret;
5375 	}
5376 	return ret;
5377 }
5378 
5379 static void ath12k_mac_op_update_vif_offload(struct ieee80211_hw *hw,
5380 					     struct ieee80211_vif *vif)
5381 {
5382 	struct ath12k *ar = hw->priv;
5383 	struct ath12k_base *ab = ar->ab;
5384 	struct ath12k_vif *arvif = ath12k_vif_to_arvif(vif);
5385 	u32 param_id, param_value;
5386 	int ret;
5387 
5388 	param_id = WMI_VDEV_PARAM_TX_ENCAP_TYPE;
5389 	if (vif->type != NL80211_IFTYPE_STATION &&
5390 	    vif->type != NL80211_IFTYPE_AP)
5391 		vif->offload_flags &= ~(IEEE80211_OFFLOAD_ENCAP_ENABLED |
5392 					IEEE80211_OFFLOAD_DECAP_ENABLED);
5393 
5394 	if (vif->offload_flags & IEEE80211_OFFLOAD_ENCAP_ENABLED)
5395 		arvif->tx_encap_type = ATH12K_HW_TXRX_ETHERNET;
5396 	else if (test_bit(ATH12K_FLAG_RAW_MODE, &ab->dev_flags))
5397 		arvif->tx_encap_type = ATH12K_HW_TXRX_RAW;
5398 	else
5399 		arvif->tx_encap_type = ATH12K_HW_TXRX_NATIVE_WIFI;
5400 
5401 	ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
5402 					    param_id, arvif->tx_encap_type);
5403 	if (ret) {
5404 		ath12k_warn(ab, "failed to set vdev %d tx encap mode: %d\n",
5405 			    arvif->vdev_id, ret);
5406 		vif->offload_flags &= ~IEEE80211_OFFLOAD_ENCAP_ENABLED;
5407 	}
5408 
5409 	param_id = WMI_VDEV_PARAM_RX_DECAP_TYPE;
5410 	if (vif->offload_flags & IEEE80211_OFFLOAD_DECAP_ENABLED)
5411 		param_value = ATH12K_HW_TXRX_ETHERNET;
5412 	else if (test_bit(ATH12K_FLAG_RAW_MODE, &ab->dev_flags))
5413 		param_value = ATH12K_HW_TXRX_RAW;
5414 	else
5415 		param_value = ATH12K_HW_TXRX_NATIVE_WIFI;
5416 
5417 	ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
5418 					    param_id, param_value);
5419 	if (ret) {
5420 		ath12k_warn(ab, "failed to set vdev %d rx decap mode: %d\n",
5421 			    arvif->vdev_id, ret);
5422 		vif->offload_flags &= ~IEEE80211_OFFLOAD_DECAP_ENABLED;
5423 	}
5424 }
5425 
5426 static int ath12k_mac_op_add_interface(struct ieee80211_hw *hw,
5427 				       struct ieee80211_vif *vif)
5428 {
5429 	struct ath12k *ar = hw->priv;
5430 	struct ath12k_base *ab = ar->ab;
5431 	struct ath12k_vif *arvif = ath12k_vif_to_arvif(vif);
5432 	struct ath12k_wmi_vdev_create_arg vdev_arg = {0};
5433 	struct ath12k_wmi_peer_create_arg peer_param;
5434 	u32 param_id, param_value;
5435 	u16 nss;
5436 	int i;
5437 	int ret;
5438 	int bit;
5439 
5440 	vif->driver_flags |= IEEE80211_VIF_SUPPORTS_UAPSD;
5441 
5442 	mutex_lock(&ar->conf_mutex);
5443 
5444 	if (vif->type == NL80211_IFTYPE_AP &&
5445 	    ar->num_peers > (ar->max_num_peers - 1)) {
5446 		ath12k_warn(ab, "failed to create vdev due to insufficient peer entry resource in firmware\n");
5447 		ret = -ENOBUFS;
5448 		goto err;
5449 	}
5450 
5451 	if (ar->num_created_vdevs > (TARGET_NUM_VDEVS - 1)) {
5452 		ath12k_warn(ab, "failed to create vdev, reached max vdev limit %d\n",
5453 			    TARGET_NUM_VDEVS);
5454 		ret = -EBUSY;
5455 		goto err;
5456 	}
5457 
5458 	memset(arvif, 0, sizeof(*arvif));
5459 
5460 	arvif->ar = ar;
5461 	arvif->vif = vif;
5462 
5463 	INIT_LIST_HEAD(&arvif->list);
5464 
5465 	/* Should we initialize any worker to handle connection loss indication
5466 	 * from firmware in sta mode?
5467 	 */
5468 
5469 	for (i = 0; i < ARRAY_SIZE(arvif->bitrate_mask.control); i++) {
5470 		arvif->bitrate_mask.control[i].legacy = 0xffffffff;
5471 		memset(arvif->bitrate_mask.control[i].ht_mcs, 0xff,
5472 		       sizeof(arvif->bitrate_mask.control[i].ht_mcs));
5473 		memset(arvif->bitrate_mask.control[i].vht_mcs, 0xff,
5474 		       sizeof(arvif->bitrate_mask.control[i].vht_mcs));
5475 	}
5476 
5477 	bit = __ffs64(ab->free_vdev_map);
5478 
5479 	arvif->vdev_id = bit;
5480 	arvif->vdev_subtype = WMI_VDEV_SUBTYPE_NONE;
5481 
5482 	switch (vif->type) {
5483 	case NL80211_IFTYPE_UNSPECIFIED:
5484 	case NL80211_IFTYPE_STATION:
5485 		arvif->vdev_type = WMI_VDEV_TYPE_STA;
5486 		break;
5487 	case NL80211_IFTYPE_MESH_POINT:
5488 		arvif->vdev_subtype = WMI_VDEV_SUBTYPE_MESH_11S;
5489 		fallthrough;
5490 	case NL80211_IFTYPE_AP:
5491 		arvif->vdev_type = WMI_VDEV_TYPE_AP;
5492 		break;
5493 	case NL80211_IFTYPE_MONITOR:
5494 		arvif->vdev_type = WMI_VDEV_TYPE_MONITOR;
5495 		ar->monitor_vdev_id = bit;
5496 		break;
5497 	default:
5498 		WARN_ON(1);
5499 		break;
5500 	}
5501 
5502 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac add interface id %d type %d subtype %d map %llx\n",
5503 		   arvif->vdev_id, arvif->vdev_type, arvif->vdev_subtype,
5504 		   ab->free_vdev_map);
5505 
5506 	vif->cab_queue = arvif->vdev_id % (ATH12K_HW_MAX_QUEUES - 1);
5507 	for (i = 0; i < ARRAY_SIZE(vif->hw_queue); i++)
5508 		vif->hw_queue[i] = i % (ATH12K_HW_MAX_QUEUES - 1);
5509 
5510 	ath12k_mac_setup_vdev_create_arg(arvif, &vdev_arg);
5511 
5512 	ret = ath12k_wmi_vdev_create(ar, vif->addr, &vdev_arg);
5513 	if (ret) {
5514 		ath12k_warn(ab, "failed to create WMI vdev %d: %d\n",
5515 			    arvif->vdev_id, ret);
5516 		goto err;
5517 	}
5518 
5519 	ar->num_created_vdevs++;
5520 	ath12k_dbg(ab, ATH12K_DBG_MAC, "vdev %pM created, vdev_id %d\n",
5521 		   vif->addr, arvif->vdev_id);
5522 	ar->allocated_vdev_map |= 1LL << arvif->vdev_id;
5523 	ab->free_vdev_map &= ~(1LL << arvif->vdev_id);
5524 
5525 	spin_lock_bh(&ar->data_lock);
5526 	list_add(&arvif->list, &ar->arvifs);
5527 	spin_unlock_bh(&ar->data_lock);
5528 
5529 	ath12k_mac_op_update_vif_offload(hw, vif);
5530 
5531 	nss = hweight32(ar->cfg_tx_chainmask) ? : 1;
5532 	ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
5533 					    WMI_VDEV_PARAM_NSS, nss);
5534 	if (ret) {
5535 		ath12k_warn(ab, "failed to set vdev %d chainmask 0x%x, nss %d :%d\n",
5536 			    arvif->vdev_id, ar->cfg_tx_chainmask, nss, ret);
5537 		goto err_vdev_del;
5538 	}
5539 
5540 	switch (arvif->vdev_type) {
5541 	case WMI_VDEV_TYPE_AP:
5542 		peer_param.vdev_id = arvif->vdev_id;
5543 		peer_param.peer_addr = vif->addr;
5544 		peer_param.peer_type = WMI_PEER_TYPE_DEFAULT;
5545 		ret = ath12k_peer_create(ar, arvif, NULL, &peer_param);
5546 		if (ret) {
5547 			ath12k_warn(ab, "failed to vdev %d create peer for AP: %d\n",
5548 				    arvif->vdev_id, ret);
5549 			goto err_vdev_del;
5550 		}
5551 
5552 		ret = ath12k_mac_set_kickout(arvif);
5553 		if (ret) {
5554 			ath12k_warn(ar->ab, "failed to set vdev %i kickout parameters: %d\n",
5555 				    arvif->vdev_id, ret);
5556 			goto err_peer_del;
5557 		}
5558 		break;
5559 	case WMI_VDEV_TYPE_STA:
5560 		param_id = WMI_STA_PS_PARAM_RX_WAKE_POLICY;
5561 		param_value = WMI_STA_PS_RX_WAKE_POLICY_WAKE;
5562 		ret = ath12k_wmi_set_sta_ps_param(ar, arvif->vdev_id,
5563 						  param_id, param_value);
5564 		if (ret) {
5565 			ath12k_warn(ar->ab, "failed to set vdev %d RX wake policy: %d\n",
5566 				    arvif->vdev_id, ret);
5567 			goto err_peer_del;
5568 		}
5569 
5570 		param_id = WMI_STA_PS_PARAM_TX_WAKE_THRESHOLD;
5571 		param_value = WMI_STA_PS_TX_WAKE_THRESHOLD_ALWAYS;
5572 		ret = ath12k_wmi_set_sta_ps_param(ar, arvif->vdev_id,
5573 						  param_id, param_value);
5574 		if (ret) {
5575 			ath12k_warn(ar->ab, "failed to set vdev %d TX wake threshold: %d\n",
5576 				    arvif->vdev_id, ret);
5577 			goto err_peer_del;
5578 		}
5579 
5580 		param_id = WMI_STA_PS_PARAM_PSPOLL_COUNT;
5581 		param_value = WMI_STA_PS_PSPOLL_COUNT_NO_MAX;
5582 		ret = ath12k_wmi_set_sta_ps_param(ar, arvif->vdev_id,
5583 						  param_id, param_value);
5584 		if (ret) {
5585 			ath12k_warn(ar->ab, "failed to set vdev %d pspoll count: %d\n",
5586 				    arvif->vdev_id, ret);
5587 			goto err_peer_del;
5588 		}
5589 
5590 		ret = ath12k_wmi_pdev_set_ps_mode(ar, arvif->vdev_id, false);
5591 		if (ret) {
5592 			ath12k_warn(ar->ab, "failed to disable vdev %d ps mode: %d\n",
5593 				    arvif->vdev_id, ret);
5594 			goto err_peer_del;
5595 		}
5596 		break;
5597 	default:
5598 		break;
5599 	}
5600 
5601 	arvif->txpower = vif->bss_conf.txpower;
5602 	ret = ath12k_mac_txpower_recalc(ar);
5603 	if (ret)
5604 		goto err_peer_del;
5605 
5606 	param_id = WMI_VDEV_PARAM_RTS_THRESHOLD;
5607 	param_value = hw->wiphy->rts_threshold;
5608 	ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
5609 					    param_id, param_value);
5610 	if (ret) {
5611 		ath12k_warn(ar->ab, "failed to set rts threshold for vdev %d: %d\n",
5612 			    arvif->vdev_id, ret);
5613 	}
5614 
5615 	ath12k_dp_vdev_tx_attach(ar, arvif);
5616 
5617 	if (vif->type != NL80211_IFTYPE_MONITOR && ar->monitor_conf_enabled)
5618 		ath12k_mac_monitor_vdev_create(ar);
5619 
5620 	mutex_unlock(&ar->conf_mutex);
5621 
5622 	return ret;
5623 
5624 err_peer_del:
5625 	if (arvif->vdev_type == WMI_VDEV_TYPE_AP) {
5626 		reinit_completion(&ar->peer_delete_done);
5627 
5628 		ret = ath12k_wmi_send_peer_delete_cmd(ar, vif->addr,
5629 						      arvif->vdev_id);
5630 		if (ret) {
5631 			ath12k_warn(ar->ab, "failed to delete peer vdev_id %d addr %pM\n",
5632 				    arvif->vdev_id, vif->addr);
5633 			goto err;
5634 		}
5635 
5636 		ret = ath12k_wait_for_peer_delete_done(ar, arvif->vdev_id,
5637 						       vif->addr);
5638 		if (ret)
5639 			goto err;
5640 
5641 		ar->num_peers--;
5642 	}
5643 
5644 err_vdev_del:
5645 	ath12k_wmi_vdev_delete(ar, arvif->vdev_id);
5646 	ar->num_created_vdevs--;
5647 	ar->allocated_vdev_map &= ~(1LL << arvif->vdev_id);
5648 	ab->free_vdev_map |= 1LL << arvif->vdev_id;
5649 	ab->free_vdev_stats_id_map &= ~(1LL << arvif->vdev_stats_id);
5650 	spin_lock_bh(&ar->data_lock);
5651 	list_del(&arvif->list);
5652 	spin_unlock_bh(&ar->data_lock);
5653 
5654 err:
5655 	mutex_unlock(&ar->conf_mutex);
5656 
5657 	return ret;
5658 }
5659 
5660 static void ath12k_mac_vif_unref(struct ath12k_dp *dp, struct ieee80211_vif *vif)
5661 {
5662 	struct ath12k_tx_desc_info *tx_desc_info;
5663 	struct ath12k_skb_cb *skb_cb;
5664 	struct sk_buff *skb;
5665 	int i;
5666 
5667 	for (i = 0; i < ATH12K_HW_MAX_QUEUES; i++) {
5668 		spin_lock_bh(&dp->tx_desc_lock[i]);
5669 
5670 		list_for_each_entry(tx_desc_info, &dp->tx_desc_used_list[i],
5671 				    list) {
5672 			skb = tx_desc_info->skb;
5673 			if (!skb)
5674 				continue;
5675 
5676 			skb_cb = ATH12K_SKB_CB(skb);
5677 			if (skb_cb->vif == vif)
5678 				skb_cb->vif = NULL;
5679 		}
5680 
5681 		spin_unlock_bh(&dp->tx_desc_lock[i]);
5682 	}
5683 }
5684 
5685 static void ath12k_mac_op_remove_interface(struct ieee80211_hw *hw,
5686 					   struct ieee80211_vif *vif)
5687 {
5688 	struct ath12k *ar = hw->priv;
5689 	struct ath12k_vif *arvif = ath12k_vif_to_arvif(vif);
5690 	struct ath12k_base *ab = ar->ab;
5691 	unsigned long time_left;
5692 	int ret;
5693 
5694 	mutex_lock(&ar->conf_mutex);
5695 
5696 	ath12k_dbg(ab, ATH12K_DBG_MAC, "mac remove interface (vdev %d)\n",
5697 		   arvif->vdev_id);
5698 
5699 	if (arvif->vdev_type == WMI_VDEV_TYPE_AP) {
5700 		ret = ath12k_peer_delete(ar, arvif->vdev_id, vif->addr);
5701 		if (ret)
5702 			ath12k_warn(ab, "failed to submit AP self-peer removal on vdev %d: %d\n",
5703 				    arvif->vdev_id, ret);
5704 	}
5705 
5706 	reinit_completion(&ar->vdev_delete_done);
5707 
5708 	ret = ath12k_wmi_vdev_delete(ar, arvif->vdev_id);
5709 	if (ret) {
5710 		ath12k_warn(ab, "failed to delete WMI vdev %d: %d\n",
5711 			    arvif->vdev_id, ret);
5712 		goto err_vdev_del;
5713 	}
5714 
5715 	time_left = wait_for_completion_timeout(&ar->vdev_delete_done,
5716 						ATH12K_VDEV_DELETE_TIMEOUT_HZ);
5717 	if (time_left == 0) {
5718 		ath12k_warn(ab, "Timeout in receiving vdev delete response\n");
5719 		goto err_vdev_del;
5720 	}
5721 
5722 	if (arvif->vdev_type == WMI_VDEV_TYPE_MONITOR) {
5723 		ar->monitor_vdev_id = -1;
5724 		ar->monitor_vdev_created = false;
5725 	} else if (ar->monitor_vdev_created && !ar->monitor_started) {
5726 		ret = ath12k_mac_monitor_vdev_delete(ar);
5727 	}
5728 
5729 	ab->free_vdev_map |= 1LL << (arvif->vdev_id);
5730 	ar->allocated_vdev_map &= ~(1LL << arvif->vdev_id);
5731 	ab->free_vdev_stats_id_map &= ~(1LL << arvif->vdev_stats_id);
5732 	ar->num_created_vdevs--;
5733 
5734 	ath12k_dbg(ab, ATH12K_DBG_MAC, "vdev %pM deleted, vdev_id %d\n",
5735 		   vif->addr, arvif->vdev_id);
5736 
5737 err_vdev_del:
5738 	spin_lock_bh(&ar->data_lock);
5739 	list_del(&arvif->list);
5740 	spin_unlock_bh(&ar->data_lock);
5741 
5742 	ath12k_peer_cleanup(ar, arvif->vdev_id);
5743 
5744 	idr_for_each(&ar->txmgmt_idr,
5745 		     ath12k_mac_vif_txmgmt_idr_remove, vif);
5746 
5747 	ath12k_mac_vif_unref(&ab->dp, vif);
5748 	ath12k_dp_tx_put_bank_profile(&ab->dp, arvif->bank_id);
5749 
5750 	/* Recalc txpower for remaining vdev */
5751 	ath12k_mac_txpower_recalc(ar);
5752 	clear_bit(ATH12K_FLAG_MONITOR_ENABLED, &ar->monitor_flags);
5753 
5754 	/* TODO: recal traffic pause state based on the available vdevs */
5755 
5756 	mutex_unlock(&ar->conf_mutex);
5757 }
5758 
5759 /* FIXME: Has to be verified. */
5760 #define SUPPORTED_FILTERS			\
5761 	(FIF_ALLMULTI |				\
5762 	FIF_CONTROL |				\
5763 	FIF_PSPOLL |				\
5764 	FIF_OTHER_BSS |				\
5765 	FIF_BCN_PRBRESP_PROMISC |		\
5766 	FIF_PROBE_REQ |				\
5767 	FIF_FCSFAIL)
5768 
5769 static void ath12k_mac_op_configure_filter(struct ieee80211_hw *hw,
5770 					   unsigned int changed_flags,
5771 					   unsigned int *total_flags,
5772 					   u64 multicast)
5773 {
5774 	struct ath12k *ar = hw->priv;
5775 	bool reset_flag;
5776 	int ret;
5777 
5778 	mutex_lock(&ar->conf_mutex);
5779 
5780 	*total_flags &= SUPPORTED_FILTERS;
5781 	ar->filter_flags = *total_flags;
5782 
5783 	/* For monitor mode */
5784 	reset_flag = !(ar->filter_flags & FIF_BCN_PRBRESP_PROMISC);
5785 
5786 	ret = ath12k_dp_tx_htt_monitor_mode_ring_config(ar, reset_flag);
5787 	if (!ret) {
5788 		if (!reset_flag)
5789 			set_bit(ATH12K_FLAG_MONITOR_ENABLED, &ar->monitor_flags);
5790 		else
5791 			clear_bit(ATH12K_FLAG_MONITOR_ENABLED, &ar->monitor_flags);
5792 	} else {
5793 		ath12k_warn(ar->ab,
5794 			    "fail to set monitor filter: %d\n", ret);
5795 	}
5796 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
5797 		   "total_flags:0x%x, reset_flag:%d\n",
5798 		   *total_flags, reset_flag);
5799 
5800 	mutex_unlock(&ar->conf_mutex);
5801 }
5802 
5803 static int ath12k_mac_op_get_antenna(struct ieee80211_hw *hw, u32 *tx_ant, u32 *rx_ant)
5804 {
5805 	struct ath12k *ar = hw->priv;
5806 
5807 	mutex_lock(&ar->conf_mutex);
5808 
5809 	*tx_ant = ar->cfg_tx_chainmask;
5810 	*rx_ant = ar->cfg_rx_chainmask;
5811 
5812 	mutex_unlock(&ar->conf_mutex);
5813 
5814 	return 0;
5815 }
5816 
5817 static int ath12k_mac_op_set_antenna(struct ieee80211_hw *hw, u32 tx_ant, u32 rx_ant)
5818 {
5819 	struct ath12k *ar = hw->priv;
5820 	int ret;
5821 
5822 	mutex_lock(&ar->conf_mutex);
5823 	ret = __ath12k_set_antenna(ar, tx_ant, rx_ant);
5824 	mutex_unlock(&ar->conf_mutex);
5825 
5826 	return ret;
5827 }
5828 
5829 static int ath12k_mac_op_ampdu_action(struct ieee80211_hw *hw,
5830 				      struct ieee80211_vif *vif,
5831 				      struct ieee80211_ampdu_params *params)
5832 {
5833 	struct ath12k *ar = hw->priv;
5834 	int ret = -EINVAL;
5835 
5836 	mutex_lock(&ar->conf_mutex);
5837 
5838 	switch (params->action) {
5839 	case IEEE80211_AMPDU_RX_START:
5840 		ret = ath12k_dp_rx_ampdu_start(ar, params);
5841 		break;
5842 	case IEEE80211_AMPDU_RX_STOP:
5843 		ret = ath12k_dp_rx_ampdu_stop(ar, params);
5844 		break;
5845 	case IEEE80211_AMPDU_TX_START:
5846 	case IEEE80211_AMPDU_TX_STOP_CONT:
5847 	case IEEE80211_AMPDU_TX_STOP_FLUSH:
5848 	case IEEE80211_AMPDU_TX_STOP_FLUSH_CONT:
5849 	case IEEE80211_AMPDU_TX_OPERATIONAL:
5850 		/* Tx A-MPDU aggregation offloaded to hw/fw so deny mac80211
5851 		 * Tx aggregation requests.
5852 		 */
5853 		ret = -EOPNOTSUPP;
5854 		break;
5855 	}
5856 
5857 	mutex_unlock(&ar->conf_mutex);
5858 
5859 	return ret;
5860 }
5861 
5862 static int ath12k_mac_op_add_chanctx(struct ieee80211_hw *hw,
5863 				     struct ieee80211_chanctx_conf *ctx)
5864 {
5865 	struct ath12k *ar = hw->priv;
5866 	struct ath12k_base *ab = ar->ab;
5867 
5868 	ath12k_dbg(ab, ATH12K_DBG_MAC,
5869 		   "mac chanctx add freq %u width %d ptr %pK\n",
5870 		   ctx->def.chan->center_freq, ctx->def.width, ctx);
5871 
5872 	mutex_lock(&ar->conf_mutex);
5873 
5874 	spin_lock_bh(&ar->data_lock);
5875 	/* TODO: In case of multiple channel context, populate rx_channel from
5876 	 * Rx PPDU desc information.
5877 	 */
5878 	ar->rx_channel = ctx->def.chan;
5879 	spin_unlock_bh(&ar->data_lock);
5880 
5881 	mutex_unlock(&ar->conf_mutex);
5882 
5883 	return 0;
5884 }
5885 
5886 static void ath12k_mac_op_remove_chanctx(struct ieee80211_hw *hw,
5887 					 struct ieee80211_chanctx_conf *ctx)
5888 {
5889 	struct ath12k *ar = hw->priv;
5890 	struct ath12k_base *ab = ar->ab;
5891 
5892 	ath12k_dbg(ab, ATH12K_DBG_MAC,
5893 		   "mac chanctx remove freq %u width %d ptr %pK\n",
5894 		   ctx->def.chan->center_freq, ctx->def.width, ctx);
5895 
5896 	mutex_lock(&ar->conf_mutex);
5897 
5898 	spin_lock_bh(&ar->data_lock);
5899 	/* TODO: In case of there is one more channel context left, populate
5900 	 * rx_channel with the channel of that remaining channel context.
5901 	 */
5902 	ar->rx_channel = NULL;
5903 	spin_unlock_bh(&ar->data_lock);
5904 
5905 	mutex_unlock(&ar->conf_mutex);
5906 }
5907 
5908 static enum wmi_phy_mode
5909 ath12k_mac_check_down_grade_phy_mode(struct ath12k *ar,
5910 				     enum wmi_phy_mode mode,
5911 				     enum nl80211_band band,
5912 				     enum nl80211_iftype type)
5913 {
5914 	struct ieee80211_sta_eht_cap *eht_cap;
5915 	enum wmi_phy_mode down_mode;
5916 
5917 	if (mode < MODE_11BE_EHT20)
5918 		return mode;
5919 
5920 	eht_cap = &ar->mac.iftype[band][type].eht_cap;
5921 	if (eht_cap->has_eht)
5922 		return mode;
5923 
5924 	switch (mode) {
5925 	case MODE_11BE_EHT20:
5926 		down_mode = MODE_11AX_HE20;
5927 		break;
5928 	case MODE_11BE_EHT40:
5929 		down_mode = MODE_11AX_HE40;
5930 		break;
5931 	case MODE_11BE_EHT80:
5932 		down_mode = MODE_11AX_HE80;
5933 		break;
5934 	case MODE_11BE_EHT80_80:
5935 		down_mode = MODE_11AX_HE80_80;
5936 		break;
5937 	case MODE_11BE_EHT160:
5938 	case MODE_11BE_EHT160_160:
5939 	case MODE_11BE_EHT320:
5940 		down_mode = MODE_11AX_HE160;
5941 		break;
5942 	case MODE_11BE_EHT20_2G:
5943 		down_mode = MODE_11AX_HE20_2G;
5944 		break;
5945 	case MODE_11BE_EHT40_2G:
5946 		down_mode = MODE_11AX_HE40_2G;
5947 		break;
5948 	default:
5949 		down_mode = mode;
5950 		break;
5951 	}
5952 
5953 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
5954 		   "mac vdev start phymode %s downgrade to %s\n",
5955 		   ath12k_mac_phymode_str(mode),
5956 		   ath12k_mac_phymode_str(down_mode));
5957 
5958 	return down_mode;
5959 }
5960 
5961 static int
5962 ath12k_mac_vdev_start_restart(struct ath12k_vif *arvif,
5963 			      struct ieee80211_chanctx_conf *ctx,
5964 			      bool restart)
5965 {
5966 	struct ath12k *ar = arvif->ar;
5967 	struct ath12k_base *ab = ar->ab;
5968 	struct wmi_vdev_start_req_arg arg = {};
5969 	const struct cfg80211_chan_def *chandef = &ctx->def;
5970 	int he_support = arvif->vif->bss_conf.he_support;
5971 	int ret;
5972 
5973 	lockdep_assert_held(&ar->conf_mutex);
5974 
5975 	reinit_completion(&ar->vdev_setup_done);
5976 
5977 	arg.vdev_id = arvif->vdev_id;
5978 	arg.dtim_period = arvif->dtim_period;
5979 	arg.bcn_intval = arvif->beacon_interval;
5980 	arg.punct_bitmap = ~arvif->punct_bitmap;
5981 
5982 	arg.freq = chandef->chan->center_freq;
5983 	arg.band_center_freq1 = chandef->center_freq1;
5984 	arg.band_center_freq2 = chandef->center_freq2;
5985 	arg.mode = ath12k_phymodes[chandef->chan->band][chandef->width];
5986 
5987 	arg.mode = ath12k_mac_check_down_grade_phy_mode(ar, arg.mode,
5988 							chandef->chan->band,
5989 							arvif->vif->type);
5990 	arg.min_power = 0;
5991 	arg.max_power = chandef->chan->max_power * 2;
5992 	arg.max_reg_power = chandef->chan->max_reg_power * 2;
5993 	arg.max_antenna_gain = chandef->chan->max_antenna_gain * 2;
5994 
5995 	arg.pref_tx_streams = ar->num_tx_chains;
5996 	arg.pref_rx_streams = ar->num_rx_chains;
5997 
5998 	if (arvif->vdev_type == WMI_VDEV_TYPE_AP) {
5999 		arg.ssid = arvif->u.ap.ssid;
6000 		arg.ssid_len = arvif->u.ap.ssid_len;
6001 		arg.hidden_ssid = arvif->u.ap.hidden_ssid;
6002 
6003 		/* For now allow DFS for AP mode */
6004 		arg.chan_radar = !!(chandef->chan->flags & IEEE80211_CHAN_RADAR);
6005 
6006 		arg.freq2_radar = ctx->radar_enabled;
6007 
6008 		arg.passive = arg.chan_radar;
6009 
6010 		spin_lock_bh(&ab->base_lock);
6011 		arg.regdomain = ar->ab->dfs_region;
6012 		spin_unlock_bh(&ab->base_lock);
6013 
6014 		/* TODO: Notify if secondary 80Mhz also needs radar detection */
6015 		if (he_support) {
6016 			ret = ath12k_set_he_mu_sounding_mode(ar, arvif);
6017 			if (ret) {
6018 				ath12k_warn(ar->ab, "failed to set he mode vdev %i\n",
6019 					    arg.vdev_id);
6020 				return ret;
6021 			}
6022 		}
6023 	}
6024 
6025 	arg.passive |= !!(chandef->chan->flags & IEEE80211_CHAN_NO_IR);
6026 
6027 	ath12k_dbg(ab, ATH12K_DBG_MAC,
6028 		   "mac vdev %d start center_freq %d phymode %s punct_bitmap 0x%x\n",
6029 		   arg.vdev_id, arg.freq,
6030 		   ath12k_mac_phymode_str(arg.mode), arg.punct_bitmap);
6031 
6032 	ret = ath12k_wmi_vdev_start(ar, &arg, restart);
6033 	if (ret) {
6034 		ath12k_warn(ar->ab, "failed to %s WMI vdev %i\n",
6035 			    restart ? "restart" : "start", arg.vdev_id);
6036 		return ret;
6037 	}
6038 
6039 	ret = ath12k_mac_vdev_setup_sync(ar);
6040 	if (ret) {
6041 		ath12k_warn(ab, "failed to synchronize setup for vdev %i %s: %d\n",
6042 			    arg.vdev_id, restart ? "restart" : "start", ret);
6043 		return ret;
6044 	}
6045 
6046 	ar->num_started_vdevs++;
6047 	ath12k_dbg(ab, ATH12K_DBG_MAC,  "vdev %pM started, vdev_id %d\n",
6048 		   arvif->vif->addr, arvif->vdev_id);
6049 
6050 	/* Enable CAC Flag in the driver by checking the channel DFS cac time,
6051 	 * i.e dfs_cac_ms value which will be valid only for radar channels
6052 	 * and state as NL80211_DFS_USABLE which indicates CAC needs to be
6053 	 * done before channel usage. This flags is used to drop rx packets.
6054 	 * during CAC.
6055 	 */
6056 	/* TODO: Set the flag for other interface types as required */
6057 	if (arvif->vdev_type == WMI_VDEV_TYPE_AP &&
6058 	    chandef->chan->dfs_cac_ms &&
6059 	    chandef->chan->dfs_state == NL80211_DFS_USABLE) {
6060 		set_bit(ATH12K_CAC_RUNNING, &ar->dev_flags);
6061 		ath12k_dbg(ab, ATH12K_DBG_MAC,
6062 			   "CAC Started in chan_freq %d for vdev %d\n",
6063 			   arg.freq, arg.vdev_id);
6064 	}
6065 
6066 	ret = ath12k_mac_set_txbf_conf(arvif);
6067 	if (ret)
6068 		ath12k_warn(ab, "failed to set txbf conf for vdev %d: %d\n",
6069 			    arvif->vdev_id, ret);
6070 
6071 	return 0;
6072 }
6073 
6074 static int ath12k_mac_vdev_stop(struct ath12k_vif *arvif)
6075 {
6076 	struct ath12k *ar = arvif->ar;
6077 	int ret;
6078 
6079 	lockdep_assert_held(&ar->conf_mutex);
6080 
6081 	reinit_completion(&ar->vdev_setup_done);
6082 
6083 	ret = ath12k_wmi_vdev_stop(ar, arvif->vdev_id);
6084 	if (ret) {
6085 		ath12k_warn(ar->ab, "failed to stop WMI vdev %i: %d\n",
6086 			    arvif->vdev_id, ret);
6087 		goto err;
6088 	}
6089 
6090 	ret = ath12k_mac_vdev_setup_sync(ar);
6091 	if (ret) {
6092 		ath12k_warn(ar->ab, "failed to synchronize setup for vdev %i: %d\n",
6093 			    arvif->vdev_id, ret);
6094 		goto err;
6095 	}
6096 
6097 	WARN_ON(ar->num_started_vdevs == 0);
6098 
6099 	ar->num_started_vdevs--;
6100 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "vdev %pM stopped, vdev_id %d\n",
6101 		   arvif->vif->addr, arvif->vdev_id);
6102 
6103 	if (test_bit(ATH12K_CAC_RUNNING, &ar->dev_flags)) {
6104 		clear_bit(ATH12K_CAC_RUNNING, &ar->dev_flags);
6105 		ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "CAC Stopped for vdev %d\n",
6106 			   arvif->vdev_id);
6107 	}
6108 
6109 	return 0;
6110 err:
6111 	return ret;
6112 }
6113 
6114 static int ath12k_mac_vdev_start(struct ath12k_vif *arvif,
6115 				 struct ieee80211_chanctx_conf *ctx)
6116 {
6117 	return ath12k_mac_vdev_start_restart(arvif, ctx, false);
6118 }
6119 
6120 static int ath12k_mac_vdev_restart(struct ath12k_vif *arvif,
6121 				   struct ieee80211_chanctx_conf *ctx)
6122 {
6123 	return ath12k_mac_vdev_start_restart(arvif, ctx, true);
6124 }
6125 
6126 struct ath12k_mac_change_chanctx_arg {
6127 	struct ieee80211_chanctx_conf *ctx;
6128 	struct ieee80211_vif_chanctx_switch *vifs;
6129 	int n_vifs;
6130 	int next_vif;
6131 };
6132 
6133 static void
6134 ath12k_mac_change_chanctx_cnt_iter(void *data, u8 *mac,
6135 				   struct ieee80211_vif *vif)
6136 {
6137 	struct ath12k_mac_change_chanctx_arg *arg = data;
6138 
6139 	if (rcu_access_pointer(vif->bss_conf.chanctx_conf) != arg->ctx)
6140 		return;
6141 
6142 	arg->n_vifs++;
6143 }
6144 
6145 static void
6146 ath12k_mac_change_chanctx_fill_iter(void *data, u8 *mac,
6147 				    struct ieee80211_vif *vif)
6148 {
6149 	struct ath12k_mac_change_chanctx_arg *arg = data;
6150 	struct ieee80211_chanctx_conf *ctx;
6151 
6152 	ctx = rcu_access_pointer(vif->bss_conf.chanctx_conf);
6153 	if (ctx != arg->ctx)
6154 		return;
6155 
6156 	if (WARN_ON(arg->next_vif == arg->n_vifs))
6157 		return;
6158 
6159 	arg->vifs[arg->next_vif].vif = vif;
6160 	arg->vifs[arg->next_vif].old_ctx = ctx;
6161 	arg->vifs[arg->next_vif].new_ctx = ctx;
6162 	arg->next_vif++;
6163 }
6164 
6165 static void
6166 ath12k_mac_update_vif_chan(struct ath12k *ar,
6167 			   struct ieee80211_vif_chanctx_switch *vifs,
6168 			   int n_vifs)
6169 {
6170 	struct ath12k_base *ab = ar->ab;
6171 	struct ath12k_vif *arvif;
6172 	int ret;
6173 	int i;
6174 	bool monitor_vif = false;
6175 
6176 	lockdep_assert_held(&ar->conf_mutex);
6177 
6178 	for (i = 0; i < n_vifs; i++) {
6179 		arvif = ath12k_vif_to_arvif(vifs[i].vif);
6180 
6181 		if (vifs[i].vif->type == NL80211_IFTYPE_MONITOR)
6182 			monitor_vif = true;
6183 
6184 		ath12k_dbg(ab, ATH12K_DBG_MAC,
6185 			   "mac chanctx switch vdev_id %i freq %u->%u width %d->%d\n",
6186 			   arvif->vdev_id,
6187 			   vifs[i].old_ctx->def.chan->center_freq,
6188 			   vifs[i].new_ctx->def.chan->center_freq,
6189 			   vifs[i].old_ctx->def.width,
6190 			   vifs[i].new_ctx->def.width);
6191 
6192 		if (WARN_ON(!arvif->is_started))
6193 			continue;
6194 
6195 		if (WARN_ON(!arvif->is_up))
6196 			continue;
6197 
6198 		ret = ath12k_wmi_vdev_down(ar, arvif->vdev_id);
6199 		if (ret) {
6200 			ath12k_warn(ab, "failed to down vdev %d: %d\n",
6201 				    arvif->vdev_id, ret);
6202 			continue;
6203 		}
6204 	}
6205 
6206 	/* All relevant vdevs are downed and associated channel resources
6207 	 * should be available for the channel switch now.
6208 	 */
6209 
6210 	/* TODO: Update ar->rx_channel */
6211 
6212 	for (i = 0; i < n_vifs; i++) {
6213 		arvif = ath12k_vif_to_arvif(vifs[i].vif);
6214 
6215 		if (WARN_ON(!arvif->is_started))
6216 			continue;
6217 
6218 		/* Firmware expect vdev_restart only if vdev is up.
6219 		 * If vdev is down then it expect vdev_stop->vdev_start.
6220 		 */
6221 		if (arvif->is_up) {
6222 			ret = ath12k_mac_vdev_restart(arvif, vifs[i].new_ctx);
6223 			if (ret) {
6224 				ath12k_warn(ab, "failed to restart vdev %d: %d\n",
6225 					    arvif->vdev_id, ret);
6226 				continue;
6227 			}
6228 		} else {
6229 			ret = ath12k_mac_vdev_stop(arvif);
6230 			if (ret) {
6231 				ath12k_warn(ab, "failed to stop vdev %d: %d\n",
6232 					    arvif->vdev_id, ret);
6233 				continue;
6234 			}
6235 
6236 			ret = ath12k_mac_vdev_start(arvif, vifs[i].new_ctx);
6237 			if (ret)
6238 				ath12k_warn(ab, "failed to start vdev %d: %d\n",
6239 					    arvif->vdev_id, ret);
6240 			continue;
6241 		}
6242 
6243 		ret = ath12k_mac_setup_bcn_tmpl(arvif);
6244 		if (ret)
6245 			ath12k_warn(ab, "failed to update bcn tmpl during csa: %d\n",
6246 				    ret);
6247 
6248 		ret = ath12k_wmi_vdev_up(arvif->ar, arvif->vdev_id, arvif->aid,
6249 					 arvif->bssid);
6250 		if (ret) {
6251 			ath12k_warn(ab, "failed to bring vdev up %d: %d\n",
6252 				    arvif->vdev_id, ret);
6253 			continue;
6254 		}
6255 	}
6256 
6257 	/* Restart the internal monitor vdev on new channel */
6258 	if (!monitor_vif && ar->monitor_vdev_created) {
6259 		if (!ath12k_mac_monitor_stop(ar))
6260 			ath12k_mac_monitor_start(ar);
6261 	}
6262 }
6263 
6264 static void
6265 ath12k_mac_update_active_vif_chan(struct ath12k *ar,
6266 				  struct ieee80211_chanctx_conf *ctx)
6267 {
6268 	struct ath12k_mac_change_chanctx_arg arg = { .ctx = ctx };
6269 	struct ieee80211_hw *hw = ar->hw;
6270 
6271 	lockdep_assert_held(&ar->conf_mutex);
6272 
6273 	ieee80211_iterate_active_interfaces_atomic(hw,
6274 						   IEEE80211_IFACE_ITER_NORMAL,
6275 						   ath12k_mac_change_chanctx_cnt_iter,
6276 						   &arg);
6277 	if (arg.n_vifs == 0)
6278 		return;
6279 
6280 	arg.vifs = kcalloc(arg.n_vifs, sizeof(arg.vifs[0]), GFP_KERNEL);
6281 	if (!arg.vifs)
6282 		return;
6283 
6284 	ieee80211_iterate_active_interfaces_atomic(hw,
6285 						   IEEE80211_IFACE_ITER_NORMAL,
6286 						   ath12k_mac_change_chanctx_fill_iter,
6287 						   &arg);
6288 
6289 	ath12k_mac_update_vif_chan(ar, arg.vifs, arg.n_vifs);
6290 
6291 	kfree(arg.vifs);
6292 }
6293 
6294 static void ath12k_mac_op_change_chanctx(struct ieee80211_hw *hw,
6295 					 struct ieee80211_chanctx_conf *ctx,
6296 					 u32 changed)
6297 {
6298 	struct ath12k *ar = hw->priv;
6299 	struct ath12k_base *ab = ar->ab;
6300 
6301 	mutex_lock(&ar->conf_mutex);
6302 
6303 	ath12k_dbg(ab, ATH12K_DBG_MAC,
6304 		   "mac chanctx change freq %u width %d ptr %pK changed %x\n",
6305 		   ctx->def.chan->center_freq, ctx->def.width, ctx, changed);
6306 
6307 	/* This shouldn't really happen because channel switching should use
6308 	 * switch_vif_chanctx().
6309 	 */
6310 	if (WARN_ON(changed & IEEE80211_CHANCTX_CHANGE_CHANNEL))
6311 		goto unlock;
6312 
6313 	if (changed & IEEE80211_CHANCTX_CHANGE_WIDTH ||
6314 	    changed & IEEE80211_CHANCTX_CHANGE_RADAR)
6315 		ath12k_mac_update_active_vif_chan(ar, ctx);
6316 
6317 	/* TODO: Recalc radar detection */
6318 
6319 unlock:
6320 	mutex_unlock(&ar->conf_mutex);
6321 }
6322 
6323 static int ath12k_start_vdev_delay(struct ieee80211_hw *hw,
6324 				   struct ieee80211_vif *vif)
6325 {
6326 	struct ath12k *ar = hw->priv;
6327 	struct ath12k_base *ab = ar->ab;
6328 	struct ath12k_vif *arvif = ath12k_vif_to_arvif(vif);
6329 	int ret;
6330 
6331 	if (WARN_ON(arvif->is_started))
6332 		return -EBUSY;
6333 
6334 	ret = ath12k_mac_vdev_start(arvif, &arvif->chanctx);
6335 	if (ret) {
6336 		ath12k_warn(ab, "failed to start vdev %i addr %pM on freq %d: %d\n",
6337 			    arvif->vdev_id, vif->addr,
6338 			    arvif->chanctx.def.chan->center_freq, ret);
6339 		return ret;
6340 	}
6341 
6342 	if (arvif->vdev_type == WMI_VDEV_TYPE_MONITOR) {
6343 		ret = ath12k_monitor_vdev_up(ar, arvif->vdev_id);
6344 		if (ret) {
6345 			ath12k_warn(ab, "failed put monitor up: %d\n", ret);
6346 			return ret;
6347 		}
6348 	}
6349 
6350 	arvif->is_started = true;
6351 
6352 	/* TODO: Setup ps and cts/rts protection */
6353 	return 0;
6354 }
6355 
6356 static int
6357 ath12k_mac_op_assign_vif_chanctx(struct ieee80211_hw *hw,
6358 				 struct ieee80211_vif *vif,
6359 				 struct ieee80211_bss_conf *link_conf,
6360 				 struct ieee80211_chanctx_conf *ctx)
6361 {
6362 	struct ath12k *ar = hw->priv;
6363 	struct ath12k_base *ab = ar->ab;
6364 	struct ath12k_vif *arvif = ath12k_vif_to_arvif(vif);
6365 	int ret;
6366 	struct ath12k_wmi_peer_create_arg param;
6367 
6368 	mutex_lock(&ar->conf_mutex);
6369 
6370 	ath12k_dbg(ab, ATH12K_DBG_MAC,
6371 		   "mac chanctx assign ptr %pK vdev_id %i\n",
6372 		   ctx, arvif->vdev_id);
6373 
6374 	arvif->punct_bitmap = link_conf->eht_puncturing;
6375 
6376 	/* for some targets bss peer must be created before vdev_start */
6377 	if (ab->hw_params->vdev_start_delay &&
6378 	    arvif->vdev_type != WMI_VDEV_TYPE_AP &&
6379 	    arvif->vdev_type != WMI_VDEV_TYPE_MONITOR &&
6380 	    !ath12k_peer_exist_by_vdev_id(ab, arvif->vdev_id)) {
6381 		memcpy(&arvif->chanctx, ctx, sizeof(*ctx));
6382 		ret = 0;
6383 		goto out;
6384 	}
6385 
6386 	if (WARN_ON(arvif->is_started)) {
6387 		ret = -EBUSY;
6388 		goto out;
6389 	}
6390 
6391 	if (ab->hw_params->vdev_start_delay &&
6392 	    arvif->vdev_type != WMI_VDEV_TYPE_AP &&
6393 	    arvif->vdev_type != WMI_VDEV_TYPE_MONITOR) {
6394 		param.vdev_id = arvif->vdev_id;
6395 		param.peer_type = WMI_PEER_TYPE_DEFAULT;
6396 		param.peer_addr = ar->mac_addr;
6397 
6398 		ret = ath12k_peer_create(ar, arvif, NULL, &param);
6399 		if (ret) {
6400 			ath12k_warn(ab, "failed to create peer after vdev start delay: %d",
6401 				    ret);
6402 			goto out;
6403 		}
6404 	}
6405 
6406 	if (arvif->vdev_type == WMI_VDEV_TYPE_MONITOR) {
6407 		ret = ath12k_mac_monitor_start(ar);
6408 		if (ret)
6409 			goto out;
6410 		arvif->is_started = true;
6411 		goto out;
6412 	}
6413 
6414 	ret = ath12k_mac_vdev_start(arvif, ctx);
6415 	if (ret) {
6416 		ath12k_warn(ab, "failed to start vdev %i addr %pM on freq %d: %d\n",
6417 			    arvif->vdev_id, vif->addr,
6418 			    ctx->def.chan->center_freq, ret);
6419 		goto out;
6420 	}
6421 
6422 	if (arvif->vdev_type != WMI_VDEV_TYPE_MONITOR && ar->monitor_vdev_created)
6423 		ath12k_mac_monitor_start(ar);
6424 
6425 	arvif->is_started = true;
6426 
6427 	/* TODO: Setup ps and cts/rts protection */
6428 
6429 out:
6430 	mutex_unlock(&ar->conf_mutex);
6431 
6432 	return ret;
6433 }
6434 
6435 static void
6436 ath12k_mac_op_unassign_vif_chanctx(struct ieee80211_hw *hw,
6437 				   struct ieee80211_vif *vif,
6438 				   struct ieee80211_bss_conf *link_conf,
6439 				   struct ieee80211_chanctx_conf *ctx)
6440 {
6441 	struct ath12k *ar = hw->priv;
6442 	struct ath12k_base *ab = ar->ab;
6443 	struct ath12k_vif *arvif = ath12k_vif_to_arvif(vif);
6444 	int ret;
6445 
6446 	mutex_lock(&ar->conf_mutex);
6447 
6448 	ath12k_dbg(ab, ATH12K_DBG_MAC,
6449 		   "mac chanctx unassign ptr %pK vdev_id %i\n",
6450 		   ctx, arvif->vdev_id);
6451 
6452 	WARN_ON(!arvif->is_started);
6453 
6454 	if (ab->hw_params->vdev_start_delay &&
6455 	    arvif->vdev_type == WMI_VDEV_TYPE_MONITOR &&
6456 	    ath12k_peer_find_by_addr(ab, ar->mac_addr))
6457 		ath12k_peer_delete(ar, arvif->vdev_id, ar->mac_addr);
6458 
6459 	if (arvif->vdev_type == WMI_VDEV_TYPE_MONITOR) {
6460 		ret = ath12k_mac_monitor_stop(ar);
6461 		if (ret) {
6462 			mutex_unlock(&ar->conf_mutex);
6463 			return;
6464 		}
6465 
6466 		arvif->is_started = false;
6467 	}
6468 
6469 	ret = ath12k_mac_vdev_stop(arvif);
6470 	if (ret)
6471 		ath12k_warn(ab, "failed to stop vdev %i: %d\n",
6472 			    arvif->vdev_id, ret);
6473 
6474 	arvif->is_started = false;
6475 
6476 	if (ab->hw_params->vdev_start_delay &&
6477 	    arvif->vdev_type == WMI_VDEV_TYPE_MONITOR)
6478 		ath12k_wmi_vdev_down(ar, arvif->vdev_id);
6479 
6480 	if (arvif->vdev_type != WMI_VDEV_TYPE_MONITOR &&
6481 	    ar->num_started_vdevs == 1 && ar->monitor_vdev_created)
6482 		ath12k_mac_monitor_stop(ar);
6483 
6484 	mutex_unlock(&ar->conf_mutex);
6485 }
6486 
6487 static int
6488 ath12k_mac_op_switch_vif_chanctx(struct ieee80211_hw *hw,
6489 				 struct ieee80211_vif_chanctx_switch *vifs,
6490 				 int n_vifs,
6491 				 enum ieee80211_chanctx_switch_mode mode)
6492 {
6493 	struct ath12k *ar = hw->priv;
6494 
6495 	mutex_lock(&ar->conf_mutex);
6496 
6497 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
6498 		   "mac chanctx switch n_vifs %d mode %d\n",
6499 		   n_vifs, mode);
6500 	ath12k_mac_update_vif_chan(ar, vifs, n_vifs);
6501 
6502 	mutex_unlock(&ar->conf_mutex);
6503 
6504 	return 0;
6505 }
6506 
6507 static int
6508 ath12k_set_vdev_param_to_all_vifs(struct ath12k *ar, int param, u32 value)
6509 {
6510 	struct ath12k_vif *arvif;
6511 	int ret = 0;
6512 
6513 	mutex_lock(&ar->conf_mutex);
6514 	list_for_each_entry(arvif, &ar->arvifs, list) {
6515 		ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "setting mac vdev %d param %d value %d\n",
6516 			   param, arvif->vdev_id, value);
6517 
6518 		ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
6519 						    param, value);
6520 		if (ret) {
6521 			ath12k_warn(ar->ab, "failed to set param %d for vdev %d: %d\n",
6522 				    param, arvif->vdev_id, ret);
6523 			break;
6524 		}
6525 	}
6526 	mutex_unlock(&ar->conf_mutex);
6527 	return ret;
6528 }
6529 
6530 /* mac80211 stores device specific RTS/Fragmentation threshold value,
6531  * this is set interface specific to firmware from ath12k driver
6532  */
6533 static int ath12k_mac_op_set_rts_threshold(struct ieee80211_hw *hw, u32 value)
6534 {
6535 	struct ath12k *ar = hw->priv;
6536 	int param_id = WMI_VDEV_PARAM_RTS_THRESHOLD;
6537 
6538 	return ath12k_set_vdev_param_to_all_vifs(ar, param_id, value);
6539 }
6540 
6541 static int ath12k_mac_op_set_frag_threshold(struct ieee80211_hw *hw, u32 value)
6542 {
6543 	/* Even though there's a WMI vdev param for fragmentation threshold no
6544 	 * known firmware actually implements it. Moreover it is not possible to
6545 	 * rely frame fragmentation to mac80211 because firmware clears the
6546 	 * "more fragments" bit in frame control making it impossible for remote
6547 	 * devices to reassemble frames.
6548 	 *
6549 	 * Hence implement a dummy callback just to say fragmentation isn't
6550 	 * supported. This effectively prevents mac80211 from doing frame
6551 	 * fragmentation in software.
6552 	 */
6553 	return -EOPNOTSUPP;
6554 }
6555 
6556 static void ath12k_mac_op_flush(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
6557 				u32 queues, bool drop)
6558 {
6559 	struct ath12k *ar = hw->priv;
6560 	long time_left;
6561 
6562 	if (drop)
6563 		return;
6564 
6565 	time_left = wait_event_timeout(ar->dp.tx_empty_waitq,
6566 				       (atomic_read(&ar->dp.num_tx_pending) == 0),
6567 				       ATH12K_FLUSH_TIMEOUT);
6568 	if (time_left == 0)
6569 		ath12k_warn(ar->ab, "failed to flush transmit queue %ld\n", time_left);
6570 
6571 	time_left = wait_event_timeout(ar->txmgmt_empty_waitq,
6572 				       (atomic_read(&ar->num_pending_mgmt_tx) == 0),
6573 				       ATH12K_FLUSH_TIMEOUT);
6574 	if (time_left == 0)
6575 		ath12k_warn(ar->ab, "failed to flush mgmt transmit queue %ld\n",
6576 			    time_left);
6577 }
6578 
6579 static int
6580 ath12k_mac_bitrate_mask_num_ht_rates(struct ath12k *ar,
6581 				     enum nl80211_band band,
6582 				     const struct cfg80211_bitrate_mask *mask)
6583 {
6584 	int num_rates = 0;
6585 	int i;
6586 
6587 	for (i = 0; i < ARRAY_SIZE(mask->control[band].ht_mcs); i++)
6588 		num_rates += hweight16(mask->control[band].ht_mcs[i]);
6589 
6590 	return num_rates;
6591 }
6592 
6593 static bool
6594 ath12k_mac_has_single_legacy_rate(struct ath12k *ar,
6595 				  enum nl80211_band band,
6596 				  const struct cfg80211_bitrate_mask *mask)
6597 {
6598 	int num_rates = 0;
6599 
6600 	num_rates = hweight32(mask->control[band].legacy);
6601 
6602 	if (ath12k_mac_bitrate_mask_num_ht_rates(ar, band, mask))
6603 		return false;
6604 
6605 	if (ath12k_mac_bitrate_mask_num_vht_rates(ar, band, mask))
6606 		return false;
6607 
6608 	return num_rates == 1;
6609 }
6610 
6611 static bool
6612 ath12k_mac_bitrate_mask_get_single_nss(struct ath12k *ar,
6613 				       enum nl80211_band band,
6614 				       const struct cfg80211_bitrate_mask *mask,
6615 				       int *nss)
6616 {
6617 	struct ieee80211_supported_band *sband = &ar->mac.sbands[band];
6618 	u16 vht_mcs_map = le16_to_cpu(sband->vht_cap.vht_mcs.tx_mcs_map);
6619 	u8 ht_nss_mask = 0;
6620 	u8 vht_nss_mask = 0;
6621 	int i;
6622 
6623 	/* No need to consider legacy here. Basic rates are always present
6624 	 * in bitrate mask
6625 	 */
6626 
6627 	for (i = 0; i < ARRAY_SIZE(mask->control[band].ht_mcs); i++) {
6628 		if (mask->control[band].ht_mcs[i] == 0)
6629 			continue;
6630 		else if (mask->control[band].ht_mcs[i] ==
6631 			 sband->ht_cap.mcs.rx_mask[i])
6632 			ht_nss_mask |= BIT(i);
6633 		else
6634 			return false;
6635 	}
6636 
6637 	for (i = 0; i < ARRAY_SIZE(mask->control[band].vht_mcs); i++) {
6638 		if (mask->control[band].vht_mcs[i] == 0)
6639 			continue;
6640 		else if (mask->control[band].vht_mcs[i] ==
6641 			 ath12k_mac_get_max_vht_mcs_map(vht_mcs_map, i))
6642 			vht_nss_mask |= BIT(i);
6643 		else
6644 			return false;
6645 	}
6646 
6647 	if (ht_nss_mask != vht_nss_mask)
6648 		return false;
6649 
6650 	if (ht_nss_mask == 0)
6651 		return false;
6652 
6653 	if (BIT(fls(ht_nss_mask)) - 1 != ht_nss_mask)
6654 		return false;
6655 
6656 	*nss = fls(ht_nss_mask);
6657 
6658 	return true;
6659 }
6660 
6661 static int
6662 ath12k_mac_get_single_legacy_rate(struct ath12k *ar,
6663 				  enum nl80211_band band,
6664 				  const struct cfg80211_bitrate_mask *mask,
6665 				  u32 *rate, u8 *nss)
6666 {
6667 	int rate_idx;
6668 	u16 bitrate;
6669 	u8 preamble;
6670 	u8 hw_rate;
6671 
6672 	if (hweight32(mask->control[band].legacy) != 1)
6673 		return -EINVAL;
6674 
6675 	rate_idx = ffs(mask->control[band].legacy) - 1;
6676 
6677 	if (band == NL80211_BAND_5GHZ || band == NL80211_BAND_6GHZ)
6678 		rate_idx += ATH12K_MAC_FIRST_OFDM_RATE_IDX;
6679 
6680 	hw_rate = ath12k_legacy_rates[rate_idx].hw_value;
6681 	bitrate = ath12k_legacy_rates[rate_idx].bitrate;
6682 
6683 	if (ath12k_mac_bitrate_is_cck(bitrate))
6684 		preamble = WMI_RATE_PREAMBLE_CCK;
6685 	else
6686 		preamble = WMI_RATE_PREAMBLE_OFDM;
6687 
6688 	*nss = 1;
6689 	*rate = ATH12K_HW_RATE_CODE(hw_rate, 0, preamble);
6690 
6691 	return 0;
6692 }
6693 
6694 static int ath12k_mac_set_fixed_rate_params(struct ath12k_vif *arvif,
6695 					    u32 rate, u8 nss, u8 sgi, u8 ldpc)
6696 {
6697 	struct ath12k *ar = arvif->ar;
6698 	u32 vdev_param;
6699 	int ret;
6700 
6701 	lockdep_assert_held(&ar->conf_mutex);
6702 
6703 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac set fixed rate params vdev %i rate 0x%02x nss %u sgi %u\n",
6704 		   arvif->vdev_id, rate, nss, sgi);
6705 
6706 	vdev_param = WMI_VDEV_PARAM_FIXED_RATE;
6707 	ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
6708 					    vdev_param, rate);
6709 	if (ret) {
6710 		ath12k_warn(ar->ab, "failed to set fixed rate param 0x%02x: %d\n",
6711 			    rate, ret);
6712 		return ret;
6713 	}
6714 
6715 	vdev_param = WMI_VDEV_PARAM_NSS;
6716 	ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
6717 					    vdev_param, nss);
6718 	if (ret) {
6719 		ath12k_warn(ar->ab, "failed to set nss param %d: %d\n",
6720 			    nss, ret);
6721 		return ret;
6722 	}
6723 
6724 	vdev_param = WMI_VDEV_PARAM_SGI;
6725 	ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
6726 					    vdev_param, sgi);
6727 	if (ret) {
6728 		ath12k_warn(ar->ab, "failed to set sgi param %d: %d\n",
6729 			    sgi, ret);
6730 		return ret;
6731 	}
6732 
6733 	vdev_param = WMI_VDEV_PARAM_LDPC;
6734 	ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
6735 					    vdev_param, ldpc);
6736 	if (ret) {
6737 		ath12k_warn(ar->ab, "failed to set ldpc param %d: %d\n",
6738 			    ldpc, ret);
6739 		return ret;
6740 	}
6741 
6742 	return 0;
6743 }
6744 
6745 static bool
6746 ath12k_mac_vht_mcs_range_present(struct ath12k *ar,
6747 				 enum nl80211_band band,
6748 				 const struct cfg80211_bitrate_mask *mask)
6749 {
6750 	int i;
6751 	u16 vht_mcs;
6752 
6753 	for (i = 0; i < NL80211_VHT_NSS_MAX; i++) {
6754 		vht_mcs = mask->control[band].vht_mcs[i];
6755 
6756 		switch (vht_mcs) {
6757 		case 0:
6758 		case BIT(8) - 1:
6759 		case BIT(9) - 1:
6760 		case BIT(10) - 1:
6761 			break;
6762 		default:
6763 			return false;
6764 		}
6765 	}
6766 
6767 	return true;
6768 }
6769 
6770 static void ath12k_mac_set_bitrate_mask_iter(void *data,
6771 					     struct ieee80211_sta *sta)
6772 {
6773 	struct ath12k_vif *arvif = data;
6774 	struct ath12k_sta *arsta = ath12k_sta_to_arsta(sta);
6775 	struct ath12k *ar = arvif->ar;
6776 
6777 	spin_lock_bh(&ar->data_lock);
6778 	arsta->changed |= IEEE80211_RC_SUPP_RATES_CHANGED;
6779 	spin_unlock_bh(&ar->data_lock);
6780 
6781 	ieee80211_queue_work(ar->hw, &arsta->update_wk);
6782 }
6783 
6784 static void ath12k_mac_disable_peer_fixed_rate(void *data,
6785 					       struct ieee80211_sta *sta)
6786 {
6787 	struct ath12k_vif *arvif = data;
6788 	struct ath12k *ar = arvif->ar;
6789 	int ret;
6790 
6791 	ret = ath12k_wmi_set_peer_param(ar, sta->addr,
6792 					arvif->vdev_id,
6793 					WMI_PEER_PARAM_FIXED_RATE,
6794 					WMI_FIXED_RATE_NONE);
6795 	if (ret)
6796 		ath12k_warn(ar->ab,
6797 			    "failed to disable peer fixed rate for STA %pM ret %d\n",
6798 			    sta->addr, ret);
6799 }
6800 
6801 static int
6802 ath12k_mac_op_set_bitrate_mask(struct ieee80211_hw *hw,
6803 			       struct ieee80211_vif *vif,
6804 			       const struct cfg80211_bitrate_mask *mask)
6805 {
6806 	struct ath12k_vif *arvif = ath12k_vif_to_arvif(vif);
6807 	struct cfg80211_chan_def def;
6808 	struct ath12k *ar = arvif->ar;
6809 	enum nl80211_band band;
6810 	const u8 *ht_mcs_mask;
6811 	const u16 *vht_mcs_mask;
6812 	u32 rate;
6813 	u8 nss;
6814 	u8 sgi;
6815 	u8 ldpc;
6816 	int single_nss;
6817 	int ret;
6818 	int num_rates;
6819 
6820 	if (ath12k_mac_vif_chan(vif, &def))
6821 		return -EPERM;
6822 
6823 	band = def.chan->band;
6824 	ht_mcs_mask = mask->control[band].ht_mcs;
6825 	vht_mcs_mask = mask->control[band].vht_mcs;
6826 	ldpc = !!(ar->ht_cap_info & WMI_HT_CAP_LDPC);
6827 
6828 	sgi = mask->control[band].gi;
6829 	if (sgi == NL80211_TXRATE_FORCE_LGI)
6830 		return -EINVAL;
6831 
6832 	/* mac80211 doesn't support sending a fixed HT/VHT MCS alone, rather it
6833 	 * requires passing at least one of used basic rates along with them.
6834 	 * Fixed rate setting across different preambles(legacy, HT, VHT) is
6835 	 * not supported by the FW. Hence use of FIXED_RATE vdev param is not
6836 	 * suitable for setting single HT/VHT rates.
6837 	 * But, there could be a single basic rate passed from userspace which
6838 	 * can be done through the FIXED_RATE param.
6839 	 */
6840 	if (ath12k_mac_has_single_legacy_rate(ar, band, mask)) {
6841 		ret = ath12k_mac_get_single_legacy_rate(ar, band, mask, &rate,
6842 							&nss);
6843 		if (ret) {
6844 			ath12k_warn(ar->ab, "failed to get single legacy rate for vdev %i: %d\n",
6845 				    arvif->vdev_id, ret);
6846 			return ret;
6847 		}
6848 		ieee80211_iterate_stations_atomic(hw,
6849 						  ath12k_mac_disable_peer_fixed_rate,
6850 						  arvif);
6851 	} else if (ath12k_mac_bitrate_mask_get_single_nss(ar, band, mask,
6852 							  &single_nss)) {
6853 		rate = WMI_FIXED_RATE_NONE;
6854 		nss = single_nss;
6855 	} else {
6856 		rate = WMI_FIXED_RATE_NONE;
6857 		nss = min_t(u32, ar->num_tx_chains,
6858 			    max(ath12k_mac_max_ht_nss(ht_mcs_mask),
6859 				ath12k_mac_max_vht_nss(vht_mcs_mask)));
6860 
6861 		/* If multiple rates across different preambles are given
6862 		 * we can reconfigure this info with all peers using PEER_ASSOC
6863 		 * command with the below exception cases.
6864 		 * - Single VHT Rate : peer_assoc command accommodates only MCS
6865 		 * range values i.e 0-7, 0-8, 0-9 for VHT. Though mac80211
6866 		 * mandates passing basic rates along with HT/VHT rates, FW
6867 		 * doesn't allow switching from VHT to Legacy. Hence instead of
6868 		 * setting legacy and VHT rates using RATEMASK_CMD vdev cmd,
6869 		 * we could set this VHT rate as peer fixed rate param, which
6870 		 * will override FIXED rate and FW rate control algorithm.
6871 		 * If single VHT rate is passed along with HT rates, we select
6872 		 * the VHT rate as fixed rate for vht peers.
6873 		 * - Multiple VHT Rates : When Multiple VHT rates are given,this
6874 		 * can be set using RATEMASK CMD which uses FW rate-ctl alg.
6875 		 * TODO: Setting multiple VHT MCS and replacing peer_assoc with
6876 		 * RATEMASK_CMDID can cover all use cases of setting rates
6877 		 * across multiple preambles and rates within same type.
6878 		 * But requires more validation of the command at this point.
6879 		 */
6880 
6881 		num_rates = ath12k_mac_bitrate_mask_num_vht_rates(ar, band,
6882 								  mask);
6883 
6884 		if (!ath12k_mac_vht_mcs_range_present(ar, band, mask) &&
6885 		    num_rates > 1) {
6886 			/* TODO: Handle multiple VHT MCS values setting using
6887 			 * RATEMASK CMD
6888 			 */
6889 			ath12k_warn(ar->ab,
6890 				    "Setting more than one MCS Value in bitrate mask not supported\n");
6891 			return -EINVAL;
6892 		}
6893 
6894 		ieee80211_iterate_stations_atomic(hw,
6895 						  ath12k_mac_disable_peer_fixed_rate,
6896 						  arvif);
6897 
6898 		mutex_lock(&ar->conf_mutex);
6899 
6900 		arvif->bitrate_mask = *mask;
6901 		ieee80211_iterate_stations_atomic(hw,
6902 						  ath12k_mac_set_bitrate_mask_iter,
6903 						  arvif);
6904 
6905 		mutex_unlock(&ar->conf_mutex);
6906 	}
6907 
6908 	mutex_lock(&ar->conf_mutex);
6909 
6910 	ret = ath12k_mac_set_fixed_rate_params(arvif, rate, nss, sgi, ldpc);
6911 	if (ret) {
6912 		ath12k_warn(ar->ab, "failed to set fixed rate params on vdev %i: %d\n",
6913 			    arvif->vdev_id, ret);
6914 	}
6915 
6916 	mutex_unlock(&ar->conf_mutex);
6917 
6918 	return ret;
6919 }
6920 
6921 static void
6922 ath12k_mac_op_reconfig_complete(struct ieee80211_hw *hw,
6923 				enum ieee80211_reconfig_type reconfig_type)
6924 {
6925 	struct ath12k *ar = hw->priv;
6926 	struct ath12k_base *ab = ar->ab;
6927 	struct ath12k_vif *arvif;
6928 	int recovery_count;
6929 
6930 	if (reconfig_type != IEEE80211_RECONFIG_TYPE_RESTART)
6931 		return;
6932 
6933 	mutex_lock(&ar->conf_mutex);
6934 
6935 	if (ar->state == ATH12K_STATE_RESTARTED) {
6936 		ath12k_warn(ar->ab, "pdev %d successfully recovered\n",
6937 			    ar->pdev->pdev_id);
6938 		ar->state = ATH12K_STATE_ON;
6939 		ieee80211_wake_queues(hw);
6940 
6941 		if (ab->is_reset) {
6942 			recovery_count = atomic_inc_return(&ab->recovery_count);
6943 			ath12k_dbg(ab, ATH12K_DBG_BOOT, "recovery count %d\n",
6944 				   recovery_count);
6945 			/* When there are multiple radios in an SOC,
6946 			 * the recovery has to be done for each radio
6947 			 */
6948 			if (recovery_count == ab->num_radios) {
6949 				atomic_dec(&ab->reset_count);
6950 				complete(&ab->reset_complete);
6951 				ab->is_reset = false;
6952 				atomic_set(&ab->fail_cont_count, 0);
6953 				ath12k_dbg(ab, ATH12K_DBG_BOOT, "reset success\n");
6954 			}
6955 		}
6956 
6957 		list_for_each_entry(arvif, &ar->arvifs, list) {
6958 			ath12k_dbg(ab, ATH12K_DBG_BOOT,
6959 				   "reconfig cipher %d up %d vdev type %d\n",
6960 				   arvif->key_cipher,
6961 				   arvif->is_up,
6962 				   arvif->vdev_type);
6963 			/* After trigger disconnect, then upper layer will
6964 			 * trigger connect again, then the PN number of
6965 			 * upper layer will be reset to keep up with AP
6966 			 * side, hence PN number mismatch will not happen.
6967 			 */
6968 			if (arvif->is_up &&
6969 			    arvif->vdev_type == WMI_VDEV_TYPE_STA &&
6970 			    arvif->vdev_subtype == WMI_VDEV_SUBTYPE_NONE) {
6971 				ieee80211_hw_restart_disconnect(arvif->vif);
6972 				ath12k_dbg(ab, ATH12K_DBG_BOOT,
6973 					   "restart disconnect\n");
6974 			}
6975 		}
6976 	}
6977 
6978 	mutex_unlock(&ar->conf_mutex);
6979 }
6980 
6981 static void
6982 ath12k_mac_update_bss_chan_survey(struct ath12k *ar,
6983 				  struct ieee80211_channel *channel)
6984 {
6985 	int ret;
6986 	enum wmi_bss_chan_info_req_type type = WMI_BSS_SURVEY_REQ_TYPE_READ;
6987 
6988 	lockdep_assert_held(&ar->conf_mutex);
6989 
6990 	if (!test_bit(WMI_TLV_SERVICE_BSS_CHANNEL_INFO_64, ar->ab->wmi_ab.svc_map) ||
6991 	    ar->rx_channel != channel)
6992 		return;
6993 
6994 	if (ar->scan.state != ATH12K_SCAN_IDLE) {
6995 		ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
6996 			   "ignoring bss chan info req while scanning..\n");
6997 		return;
6998 	}
6999 
7000 	reinit_completion(&ar->bss_survey_done);
7001 
7002 	ret = ath12k_wmi_pdev_bss_chan_info_request(ar, type);
7003 	if (ret) {
7004 		ath12k_warn(ar->ab, "failed to send pdev bss chan info request\n");
7005 		return;
7006 	}
7007 
7008 	ret = wait_for_completion_timeout(&ar->bss_survey_done, 3 * HZ);
7009 	if (ret == 0)
7010 		ath12k_warn(ar->ab, "bss channel survey timed out\n");
7011 }
7012 
7013 static int ath12k_mac_op_get_survey(struct ieee80211_hw *hw, int idx,
7014 				    struct survey_info *survey)
7015 {
7016 	struct ath12k *ar = hw->priv;
7017 	struct ieee80211_supported_band *sband;
7018 	struct survey_info *ar_survey;
7019 	int ret = 0;
7020 
7021 	if (idx >= ATH12K_NUM_CHANS)
7022 		return -ENOENT;
7023 
7024 	ar_survey = &ar->survey[idx];
7025 
7026 	mutex_lock(&ar->conf_mutex);
7027 
7028 	sband = hw->wiphy->bands[NL80211_BAND_2GHZ];
7029 	if (sband && idx >= sband->n_channels) {
7030 		idx -= sband->n_channels;
7031 		sband = NULL;
7032 	}
7033 
7034 	if (!sband)
7035 		sband = hw->wiphy->bands[NL80211_BAND_5GHZ];
7036 
7037 	if (!sband || idx >= sband->n_channels) {
7038 		ret = -ENOENT;
7039 		goto exit;
7040 	}
7041 
7042 	ath12k_mac_update_bss_chan_survey(ar, &sband->channels[idx]);
7043 
7044 	spin_lock_bh(&ar->data_lock);
7045 	memcpy(survey, ar_survey, sizeof(*survey));
7046 	spin_unlock_bh(&ar->data_lock);
7047 
7048 	survey->channel = &sband->channels[idx];
7049 
7050 	if (ar->rx_channel == survey->channel)
7051 		survey->filled |= SURVEY_INFO_IN_USE;
7052 
7053 exit:
7054 	mutex_unlock(&ar->conf_mutex);
7055 	return ret;
7056 }
7057 
7058 static void ath12k_mac_op_sta_statistics(struct ieee80211_hw *hw,
7059 					 struct ieee80211_vif *vif,
7060 					 struct ieee80211_sta *sta,
7061 					 struct station_info *sinfo)
7062 {
7063 	struct ath12k_sta *arsta = ath12k_sta_to_arsta(sta);
7064 
7065 	sinfo->rx_duration = arsta->rx_duration;
7066 	sinfo->filled |= BIT_ULL(NL80211_STA_INFO_RX_DURATION);
7067 
7068 	sinfo->tx_duration = arsta->tx_duration;
7069 	sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_DURATION);
7070 
7071 	if (!arsta->txrate.legacy && !arsta->txrate.nss)
7072 		return;
7073 
7074 	if (arsta->txrate.legacy) {
7075 		sinfo->txrate.legacy = arsta->txrate.legacy;
7076 	} else {
7077 		sinfo->txrate.mcs = arsta->txrate.mcs;
7078 		sinfo->txrate.nss = arsta->txrate.nss;
7079 		sinfo->txrate.bw = arsta->txrate.bw;
7080 		sinfo->txrate.he_gi = arsta->txrate.he_gi;
7081 		sinfo->txrate.he_dcm = arsta->txrate.he_dcm;
7082 		sinfo->txrate.he_ru_alloc = arsta->txrate.he_ru_alloc;
7083 	}
7084 	sinfo->txrate.flags = arsta->txrate.flags;
7085 	sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_BITRATE);
7086 
7087 	/* TODO: Use real NF instead of default one. */
7088 	sinfo->signal = arsta->rssi_comb + ATH12K_DEFAULT_NOISE_FLOOR;
7089 	sinfo->filled |= BIT_ULL(NL80211_STA_INFO_SIGNAL);
7090 }
7091 
7092 static const struct ieee80211_ops ath12k_ops = {
7093 	.tx				= ath12k_mac_op_tx,
7094 	.wake_tx_queue			= ieee80211_handle_wake_tx_queue,
7095 	.start                          = ath12k_mac_op_start,
7096 	.stop                           = ath12k_mac_op_stop,
7097 	.reconfig_complete              = ath12k_mac_op_reconfig_complete,
7098 	.add_interface                  = ath12k_mac_op_add_interface,
7099 	.remove_interface		= ath12k_mac_op_remove_interface,
7100 	.update_vif_offload		= ath12k_mac_op_update_vif_offload,
7101 	.config                         = ath12k_mac_op_config,
7102 	.bss_info_changed               = ath12k_mac_op_bss_info_changed,
7103 	.configure_filter		= ath12k_mac_op_configure_filter,
7104 	.hw_scan                        = ath12k_mac_op_hw_scan,
7105 	.cancel_hw_scan                 = ath12k_mac_op_cancel_hw_scan,
7106 	.set_key                        = ath12k_mac_op_set_key,
7107 	.sta_state                      = ath12k_mac_op_sta_state,
7108 	.sta_set_txpwr			= ath12k_mac_op_sta_set_txpwr,
7109 	.sta_rc_update			= ath12k_mac_op_sta_rc_update,
7110 	.conf_tx                        = ath12k_mac_op_conf_tx,
7111 	.set_antenna			= ath12k_mac_op_set_antenna,
7112 	.get_antenna			= ath12k_mac_op_get_antenna,
7113 	.ampdu_action			= ath12k_mac_op_ampdu_action,
7114 	.add_chanctx			= ath12k_mac_op_add_chanctx,
7115 	.remove_chanctx			= ath12k_mac_op_remove_chanctx,
7116 	.change_chanctx			= ath12k_mac_op_change_chanctx,
7117 	.assign_vif_chanctx		= ath12k_mac_op_assign_vif_chanctx,
7118 	.unassign_vif_chanctx		= ath12k_mac_op_unassign_vif_chanctx,
7119 	.switch_vif_chanctx		= ath12k_mac_op_switch_vif_chanctx,
7120 	.set_rts_threshold		= ath12k_mac_op_set_rts_threshold,
7121 	.set_frag_threshold		= ath12k_mac_op_set_frag_threshold,
7122 	.set_bitrate_mask		= ath12k_mac_op_set_bitrate_mask,
7123 	.get_survey			= ath12k_mac_op_get_survey,
7124 	.flush				= ath12k_mac_op_flush,
7125 	.sta_statistics			= ath12k_mac_op_sta_statistics,
7126 };
7127 
7128 static void ath12k_mac_update_ch_list(struct ath12k *ar,
7129 				      struct ieee80211_supported_band *band,
7130 				      u32 freq_low, u32 freq_high)
7131 {
7132 	int i;
7133 
7134 	if (!(freq_low && freq_high))
7135 		return;
7136 
7137 	for (i = 0; i < band->n_channels; i++) {
7138 		if (band->channels[i].center_freq < freq_low ||
7139 		    band->channels[i].center_freq > freq_high)
7140 			band->channels[i].flags |= IEEE80211_CHAN_DISABLED;
7141 	}
7142 }
7143 
7144 static u32 ath12k_get_phy_id(struct ath12k *ar, u32 band)
7145 {
7146 	struct ath12k_pdev *pdev = ar->pdev;
7147 	struct ath12k_pdev_cap *pdev_cap = &pdev->cap;
7148 
7149 	if (band == WMI_HOST_WLAN_2G_CAP)
7150 		return pdev_cap->band[NL80211_BAND_2GHZ].phy_id;
7151 
7152 	if (band == WMI_HOST_WLAN_5G_CAP)
7153 		return pdev_cap->band[NL80211_BAND_5GHZ].phy_id;
7154 
7155 	ath12k_warn(ar->ab, "unsupported phy cap:%d\n", band);
7156 
7157 	return 0;
7158 }
7159 
7160 static int ath12k_mac_setup_channels_rates(struct ath12k *ar,
7161 					   u32 supported_bands)
7162 {
7163 	struct ieee80211_hw *hw = ar->hw;
7164 	struct ieee80211_supported_band *band;
7165 	struct ath12k_wmi_hal_reg_capabilities_ext_arg *reg_cap;
7166 	void *channels;
7167 	u32 phy_id;
7168 
7169 	BUILD_BUG_ON((ARRAY_SIZE(ath12k_2ghz_channels) +
7170 		      ARRAY_SIZE(ath12k_5ghz_channels) +
7171 		      ARRAY_SIZE(ath12k_6ghz_channels)) !=
7172 		     ATH12K_NUM_CHANS);
7173 
7174 	reg_cap = &ar->ab->hal_reg_cap[ar->pdev_idx];
7175 
7176 	if (supported_bands & WMI_HOST_WLAN_2G_CAP) {
7177 		channels = kmemdup(ath12k_2ghz_channels,
7178 				   sizeof(ath12k_2ghz_channels),
7179 				   GFP_KERNEL);
7180 		if (!channels)
7181 			return -ENOMEM;
7182 
7183 		band = &ar->mac.sbands[NL80211_BAND_2GHZ];
7184 		band->band = NL80211_BAND_2GHZ;
7185 		band->n_channels = ARRAY_SIZE(ath12k_2ghz_channels);
7186 		band->channels = channels;
7187 		band->n_bitrates = ath12k_g_rates_size;
7188 		band->bitrates = ath12k_g_rates;
7189 		hw->wiphy->bands[NL80211_BAND_2GHZ] = band;
7190 
7191 		if (ar->ab->hw_params->single_pdev_only) {
7192 			phy_id = ath12k_get_phy_id(ar, WMI_HOST_WLAN_2G_CAP);
7193 			reg_cap = &ar->ab->hal_reg_cap[phy_id];
7194 		}
7195 		ath12k_mac_update_ch_list(ar, band,
7196 					  reg_cap->low_2ghz_chan,
7197 					  reg_cap->high_2ghz_chan);
7198 	}
7199 
7200 	if (supported_bands & WMI_HOST_WLAN_5G_CAP) {
7201 		if (reg_cap->high_5ghz_chan >= ATH12K_MAX_6G_FREQ) {
7202 			channels = kmemdup(ath12k_6ghz_channels,
7203 					   sizeof(ath12k_6ghz_channels), GFP_KERNEL);
7204 			if (!channels) {
7205 				kfree(ar->mac.sbands[NL80211_BAND_2GHZ].channels);
7206 				return -ENOMEM;
7207 			}
7208 
7209 			ar->supports_6ghz = true;
7210 			band = &ar->mac.sbands[NL80211_BAND_6GHZ];
7211 			band->band = NL80211_BAND_6GHZ;
7212 			band->n_channels = ARRAY_SIZE(ath12k_6ghz_channels);
7213 			band->channels = channels;
7214 			band->n_bitrates = ath12k_a_rates_size;
7215 			band->bitrates = ath12k_a_rates;
7216 			hw->wiphy->bands[NL80211_BAND_6GHZ] = band;
7217 			ath12k_mac_update_ch_list(ar, band,
7218 						  reg_cap->low_5ghz_chan,
7219 						  reg_cap->high_5ghz_chan);
7220 		}
7221 
7222 		if (reg_cap->low_5ghz_chan < ATH12K_MIN_6G_FREQ) {
7223 			channels = kmemdup(ath12k_5ghz_channels,
7224 					   sizeof(ath12k_5ghz_channels),
7225 					   GFP_KERNEL);
7226 			if (!channels) {
7227 				kfree(ar->mac.sbands[NL80211_BAND_2GHZ].channels);
7228 				kfree(ar->mac.sbands[NL80211_BAND_6GHZ].channels);
7229 				return -ENOMEM;
7230 			}
7231 
7232 			band = &ar->mac.sbands[NL80211_BAND_5GHZ];
7233 			band->band = NL80211_BAND_5GHZ;
7234 			band->n_channels = ARRAY_SIZE(ath12k_5ghz_channels);
7235 			band->channels = channels;
7236 			band->n_bitrates = ath12k_a_rates_size;
7237 			band->bitrates = ath12k_a_rates;
7238 			hw->wiphy->bands[NL80211_BAND_5GHZ] = band;
7239 
7240 			if (ar->ab->hw_params->single_pdev_only) {
7241 				phy_id = ath12k_get_phy_id(ar, WMI_HOST_WLAN_5G_CAP);
7242 				reg_cap = &ar->ab->hal_reg_cap[phy_id];
7243 			}
7244 
7245 			ath12k_mac_update_ch_list(ar, band,
7246 						  reg_cap->low_5ghz_chan,
7247 						  reg_cap->high_5ghz_chan);
7248 		}
7249 	}
7250 
7251 	return 0;
7252 }
7253 
7254 static int ath12k_mac_setup_iface_combinations(struct ath12k *ar)
7255 {
7256 	struct ath12k_base *ab = ar->ab;
7257 	struct ieee80211_hw *hw = ar->hw;
7258 	struct wiphy *wiphy = hw->wiphy;
7259 	struct ieee80211_iface_combination *combinations;
7260 	struct ieee80211_iface_limit *limits;
7261 	int n_limits, max_interfaces;
7262 	bool ap, mesh;
7263 
7264 	ap = ab->hw_params->interface_modes & BIT(NL80211_IFTYPE_AP);
7265 
7266 	mesh = IS_ENABLED(CONFIG_MAC80211_MESH) &&
7267 		ab->hw_params->interface_modes & BIT(NL80211_IFTYPE_MESH_POINT);
7268 
7269 	combinations = kzalloc(sizeof(*combinations), GFP_KERNEL);
7270 	if (!combinations)
7271 		return -ENOMEM;
7272 
7273 	if (ap || mesh) {
7274 		n_limits = 2;
7275 		max_interfaces = 16;
7276 	} else {
7277 		n_limits = 1;
7278 		max_interfaces = 1;
7279 	}
7280 
7281 	limits = kcalloc(n_limits, sizeof(*limits), GFP_KERNEL);
7282 	if (!limits) {
7283 		kfree(combinations);
7284 		return -ENOMEM;
7285 	}
7286 
7287 	limits[0].max = 1;
7288 	limits[0].types |= BIT(NL80211_IFTYPE_STATION);
7289 
7290 	if (ap) {
7291 		limits[1].max = max_interfaces;
7292 		limits[1].types |= BIT(NL80211_IFTYPE_AP);
7293 	}
7294 
7295 	if (mesh)
7296 		limits[1].types |= BIT(NL80211_IFTYPE_MESH_POINT);
7297 
7298 	combinations[0].limits = limits;
7299 	combinations[0].n_limits = n_limits;
7300 	combinations[0].max_interfaces = max_interfaces;
7301 	combinations[0].num_different_channels = 1;
7302 	combinations[0].beacon_int_infra_match = true;
7303 	combinations[0].beacon_int_min_gcd = 100;
7304 	combinations[0].radar_detect_widths = BIT(NL80211_CHAN_WIDTH_20_NOHT) |
7305 						BIT(NL80211_CHAN_WIDTH_20) |
7306 						BIT(NL80211_CHAN_WIDTH_40) |
7307 						BIT(NL80211_CHAN_WIDTH_80);
7308 
7309 	wiphy->iface_combinations = combinations;
7310 	wiphy->n_iface_combinations = 1;
7311 
7312 	return 0;
7313 }
7314 
7315 static const u8 ath12k_if_types_ext_capa[] = {
7316 	[0] = WLAN_EXT_CAPA1_EXT_CHANNEL_SWITCHING,
7317 	[7] = WLAN_EXT_CAPA8_OPMODE_NOTIF,
7318 };
7319 
7320 static const u8 ath12k_if_types_ext_capa_sta[] = {
7321 	[0] = WLAN_EXT_CAPA1_EXT_CHANNEL_SWITCHING,
7322 	[7] = WLAN_EXT_CAPA8_OPMODE_NOTIF,
7323 	[9] = WLAN_EXT_CAPA10_TWT_REQUESTER_SUPPORT,
7324 };
7325 
7326 static const u8 ath12k_if_types_ext_capa_ap[] = {
7327 	[0] = WLAN_EXT_CAPA1_EXT_CHANNEL_SWITCHING,
7328 	[7] = WLAN_EXT_CAPA8_OPMODE_NOTIF,
7329 	[9] = WLAN_EXT_CAPA10_TWT_RESPONDER_SUPPORT,
7330 };
7331 
7332 static const struct wiphy_iftype_ext_capab ath12k_iftypes_ext_capa[] = {
7333 	{
7334 		.extended_capabilities = ath12k_if_types_ext_capa,
7335 		.extended_capabilities_mask = ath12k_if_types_ext_capa,
7336 		.extended_capabilities_len = sizeof(ath12k_if_types_ext_capa),
7337 	}, {
7338 		.iftype = NL80211_IFTYPE_STATION,
7339 		.extended_capabilities = ath12k_if_types_ext_capa_sta,
7340 		.extended_capabilities_mask = ath12k_if_types_ext_capa_sta,
7341 		.extended_capabilities_len =
7342 				sizeof(ath12k_if_types_ext_capa_sta),
7343 	}, {
7344 		.iftype = NL80211_IFTYPE_AP,
7345 		.extended_capabilities = ath12k_if_types_ext_capa_ap,
7346 		.extended_capabilities_mask = ath12k_if_types_ext_capa_ap,
7347 		.extended_capabilities_len =
7348 				sizeof(ath12k_if_types_ext_capa_ap),
7349 	},
7350 };
7351 
7352 static void __ath12k_mac_unregister(struct ath12k *ar)
7353 {
7354 	struct ieee80211_hw *hw = ar->hw;
7355 	struct wiphy *wiphy = hw->wiphy;
7356 
7357 	cancel_work_sync(&ar->regd_update_work);
7358 
7359 	ieee80211_unregister_hw(hw);
7360 
7361 	idr_for_each(&ar->txmgmt_idr, ath12k_mac_tx_mgmt_pending_free, ar);
7362 	idr_destroy(&ar->txmgmt_idr);
7363 
7364 	kfree(ar->mac.sbands[NL80211_BAND_2GHZ].channels);
7365 	kfree(ar->mac.sbands[NL80211_BAND_5GHZ].channels);
7366 	kfree(ar->mac.sbands[NL80211_BAND_6GHZ].channels);
7367 
7368 	kfree(wiphy->iface_combinations[0].limits);
7369 	kfree(wiphy->iface_combinations);
7370 
7371 	SET_IEEE80211_DEV(hw, NULL);
7372 }
7373 
7374 void ath12k_mac_unregister(struct ath12k_base *ab)
7375 {
7376 	struct ath12k *ar;
7377 	struct ath12k_pdev *pdev;
7378 	int i;
7379 
7380 	for (i = 0; i < ab->num_radios; i++) {
7381 		pdev = &ab->pdevs[i];
7382 		ar = pdev->ar;
7383 		if (!ar)
7384 			continue;
7385 
7386 		__ath12k_mac_unregister(ar);
7387 	}
7388 }
7389 
7390 static int __ath12k_mac_register(struct ath12k *ar)
7391 {
7392 	struct ath12k_base *ab = ar->ab;
7393 	struct ieee80211_hw *hw = ar->hw;
7394 	struct wiphy *wiphy = hw->wiphy;
7395 	struct ath12k_pdev_cap *cap = &ar->pdev->cap;
7396 	static const u32 cipher_suites[] = {
7397 		WLAN_CIPHER_SUITE_TKIP,
7398 		WLAN_CIPHER_SUITE_CCMP,
7399 		WLAN_CIPHER_SUITE_AES_CMAC,
7400 		WLAN_CIPHER_SUITE_BIP_CMAC_256,
7401 		WLAN_CIPHER_SUITE_BIP_GMAC_128,
7402 		WLAN_CIPHER_SUITE_BIP_GMAC_256,
7403 		WLAN_CIPHER_SUITE_GCMP,
7404 		WLAN_CIPHER_SUITE_GCMP_256,
7405 		WLAN_CIPHER_SUITE_CCMP_256,
7406 	};
7407 	int ret;
7408 	u32 ht_cap = 0;
7409 
7410 	ath12k_pdev_caps_update(ar);
7411 
7412 	SET_IEEE80211_PERM_ADDR(hw, ar->mac_addr);
7413 
7414 	SET_IEEE80211_DEV(hw, ab->dev);
7415 
7416 	ret = ath12k_mac_setup_channels_rates(ar,
7417 					      cap->supported_bands);
7418 	if (ret)
7419 		goto err;
7420 
7421 	ath12k_mac_setup_ht_vht_cap(ar, cap, &ht_cap);
7422 	ath12k_mac_setup_sband_iftype_data(ar, cap);
7423 
7424 	ret = ath12k_mac_setup_iface_combinations(ar);
7425 	if (ret) {
7426 		ath12k_err(ar->ab, "failed to setup interface combinations: %d\n", ret);
7427 		goto err_free_channels;
7428 	}
7429 
7430 	wiphy->available_antennas_rx = cap->rx_chain_mask;
7431 	wiphy->available_antennas_tx = cap->tx_chain_mask;
7432 
7433 	wiphy->interface_modes = ab->hw_params->interface_modes;
7434 
7435 	if (wiphy->bands[NL80211_BAND_2GHZ] &&
7436 	    wiphy->bands[NL80211_BAND_5GHZ] &&
7437 	    wiphy->bands[NL80211_BAND_6GHZ])
7438 		ieee80211_hw_set(hw, SINGLE_SCAN_ON_ALL_BANDS);
7439 
7440 	ieee80211_hw_set(hw, SIGNAL_DBM);
7441 	ieee80211_hw_set(hw, SUPPORTS_PS);
7442 	ieee80211_hw_set(hw, SUPPORTS_DYNAMIC_PS);
7443 	ieee80211_hw_set(hw, MFP_CAPABLE);
7444 	ieee80211_hw_set(hw, REPORTS_TX_ACK_STATUS);
7445 	ieee80211_hw_set(hw, HAS_RATE_CONTROL);
7446 	ieee80211_hw_set(hw, AP_LINK_PS);
7447 	ieee80211_hw_set(hw, SPECTRUM_MGMT);
7448 	ieee80211_hw_set(hw, CONNECTION_MONITOR);
7449 	ieee80211_hw_set(hw, SUPPORTS_PER_STA_GTK);
7450 	ieee80211_hw_set(hw, CHANCTX_STA_CSA);
7451 	ieee80211_hw_set(hw, QUEUE_CONTROL);
7452 	ieee80211_hw_set(hw, SUPPORTS_TX_FRAG);
7453 	ieee80211_hw_set(hw, REPORTS_LOW_ACK);
7454 
7455 	if (ht_cap & WMI_HT_CAP_ENABLED) {
7456 		ieee80211_hw_set(hw, AMPDU_AGGREGATION);
7457 		ieee80211_hw_set(hw, TX_AMPDU_SETUP_IN_HW);
7458 		ieee80211_hw_set(hw, SUPPORTS_REORDERING_BUFFER);
7459 		ieee80211_hw_set(hw, SUPPORTS_AMSDU_IN_AMPDU);
7460 		ieee80211_hw_set(hw, USES_RSS);
7461 	}
7462 
7463 	wiphy->features |= NL80211_FEATURE_STATIC_SMPS;
7464 	wiphy->flags |= WIPHY_FLAG_IBSS_RSN;
7465 
7466 	/* TODO: Check if HT capability advertised from firmware is different
7467 	 * for each band for a dual band capable radio. It will be tricky to
7468 	 * handle it when the ht capability different for each band.
7469 	 */
7470 	if (ht_cap & WMI_HT_CAP_DYNAMIC_SMPS)
7471 		wiphy->features |= NL80211_FEATURE_DYNAMIC_SMPS;
7472 
7473 	wiphy->max_scan_ssids = WLAN_SCAN_PARAMS_MAX_SSID;
7474 	wiphy->max_scan_ie_len = WLAN_SCAN_PARAMS_MAX_IE_LEN;
7475 
7476 	hw->max_listen_interval = ATH12K_MAX_HW_LISTEN_INTERVAL;
7477 
7478 	wiphy->flags |= WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL;
7479 	wiphy->flags |= WIPHY_FLAG_HAS_CHANNEL_SWITCH;
7480 	wiphy->max_remain_on_channel_duration = 5000;
7481 
7482 	wiphy->flags |= WIPHY_FLAG_AP_UAPSD;
7483 	wiphy->features |= NL80211_FEATURE_AP_MODE_CHAN_WIDTH_CHANGE |
7484 				   NL80211_FEATURE_AP_SCAN;
7485 
7486 	ar->max_num_stations = TARGET_NUM_STATIONS;
7487 	ar->max_num_peers = TARGET_NUM_PEERS_PDEV;
7488 
7489 	wiphy->max_ap_assoc_sta = ar->max_num_stations;
7490 
7491 	hw->queues = ATH12K_HW_MAX_QUEUES;
7492 	wiphy->tx_queue_len = ATH12K_QUEUE_LEN;
7493 	hw->offchannel_tx_hw_queue = ATH12K_HW_MAX_QUEUES - 1;
7494 	hw->max_rx_aggregation_subframes = IEEE80211_MAX_AMPDU_BUF_HE;
7495 
7496 	hw->vif_data_size = sizeof(struct ath12k_vif);
7497 	hw->sta_data_size = sizeof(struct ath12k_sta);
7498 
7499 	wiphy_ext_feature_set(wiphy, NL80211_EXT_FEATURE_CQM_RSSI_LIST);
7500 	wiphy_ext_feature_set(wiphy, NL80211_EXT_FEATURE_STA_TX_PWR);
7501 
7502 	wiphy->cipher_suites = cipher_suites;
7503 	wiphy->n_cipher_suites = ARRAY_SIZE(cipher_suites);
7504 
7505 	wiphy->iftype_ext_capab = ath12k_iftypes_ext_capa;
7506 	wiphy->num_iftype_ext_capab = ARRAY_SIZE(ath12k_iftypes_ext_capa);
7507 
7508 	if (ar->supports_6ghz) {
7509 		wiphy_ext_feature_set(wiphy,
7510 				      NL80211_EXT_FEATURE_FILS_DISCOVERY);
7511 		wiphy_ext_feature_set(wiphy,
7512 				      NL80211_EXT_FEATURE_UNSOL_BCAST_PROBE_RESP);
7513 	}
7514 
7515 	wiphy_ext_feature_set(wiphy, NL80211_EXT_FEATURE_PUNCT);
7516 
7517 	ath12k_reg_init(hw);
7518 
7519 	if (!test_bit(ATH12K_FLAG_RAW_MODE, &ab->dev_flags)) {
7520 		hw->netdev_features = NETIF_F_HW_CSUM;
7521 		ieee80211_hw_set(hw, SW_CRYPTO_CONTROL);
7522 		ieee80211_hw_set(hw, SUPPORT_FAST_XMIT);
7523 	}
7524 
7525 	ret = ieee80211_register_hw(hw);
7526 	if (ret) {
7527 		ath12k_err(ar->ab, "ieee80211 registration failed: %d\n", ret);
7528 		goto err_free_if_combs;
7529 	}
7530 
7531 	if (!ab->hw_params->supports_monitor)
7532 		/* There's a race between calling ieee80211_register_hw()
7533 		 * and here where the monitor mode is enabled for a little
7534 		 * while. But that time is so short and in practise it make
7535 		 * a difference in real life.
7536 		 */
7537 		wiphy->interface_modes &= ~BIT(NL80211_IFTYPE_MONITOR);
7538 
7539 	/* Apply the regd received during initialization */
7540 	ret = ath12k_regd_update(ar, true);
7541 	if (ret) {
7542 		ath12k_err(ar->ab, "ath12k regd update failed: %d\n", ret);
7543 		goto err_unregister_hw;
7544 	}
7545 
7546 	return 0;
7547 
7548 err_unregister_hw:
7549 	ieee80211_unregister_hw(hw);
7550 
7551 err_free_if_combs:
7552 	kfree(wiphy->iface_combinations[0].limits);
7553 	kfree(wiphy->iface_combinations);
7554 
7555 err_free_channels:
7556 	kfree(ar->mac.sbands[NL80211_BAND_2GHZ].channels);
7557 	kfree(ar->mac.sbands[NL80211_BAND_5GHZ].channels);
7558 	kfree(ar->mac.sbands[NL80211_BAND_6GHZ].channels);
7559 
7560 err:
7561 	SET_IEEE80211_DEV(hw, NULL);
7562 	return ret;
7563 }
7564 
7565 int ath12k_mac_register(struct ath12k_base *ab)
7566 {
7567 	struct ath12k *ar;
7568 	struct ath12k_pdev *pdev;
7569 	int i;
7570 	int ret;
7571 
7572 	if (test_bit(ATH12K_FLAG_REGISTERED, &ab->dev_flags))
7573 		return 0;
7574 
7575 	for (i = 0; i < ab->num_radios; i++) {
7576 		pdev = &ab->pdevs[i];
7577 		ar = pdev->ar;
7578 		if (ab->pdevs_macaddr_valid) {
7579 			ether_addr_copy(ar->mac_addr, pdev->mac_addr);
7580 		} else {
7581 			ether_addr_copy(ar->mac_addr, ab->mac_addr);
7582 			ar->mac_addr[4] += i;
7583 		}
7584 
7585 		ret = __ath12k_mac_register(ar);
7586 		if (ret)
7587 			goto err_cleanup;
7588 
7589 		init_waitqueue_head(&ar->txmgmt_empty_waitq);
7590 		idr_init(&ar->txmgmt_idr);
7591 		spin_lock_init(&ar->txmgmt_idr_lock);
7592 	}
7593 
7594 	/* Initialize channel counters frequency value in hertz */
7595 	ab->cc_freq_hz = 320000;
7596 	ab->free_vdev_map = (1LL << (ab->num_radios * TARGET_NUM_VDEVS)) - 1;
7597 
7598 	return 0;
7599 
7600 err_cleanup:
7601 	for (i = i - 1; i >= 0; i--) {
7602 		pdev = &ab->pdevs[i];
7603 		ar = pdev->ar;
7604 		__ath12k_mac_unregister(ar);
7605 	}
7606 
7607 	return ret;
7608 }
7609 
7610 int ath12k_mac_allocate(struct ath12k_base *ab)
7611 {
7612 	struct ieee80211_hw *hw;
7613 	struct ath12k *ar;
7614 	struct ath12k_pdev *pdev;
7615 	int ret;
7616 	int i;
7617 
7618 	if (test_bit(ATH12K_FLAG_REGISTERED, &ab->dev_flags))
7619 		return 0;
7620 
7621 	for (i = 0; i < ab->num_radios; i++) {
7622 		pdev = &ab->pdevs[i];
7623 		hw = ieee80211_alloc_hw(sizeof(struct ath12k), &ath12k_ops);
7624 		if (!hw) {
7625 			ath12k_warn(ab, "failed to allocate mac80211 hw device\n");
7626 			ret = -ENOMEM;
7627 			goto err_free_mac;
7628 		}
7629 
7630 		ar = hw->priv;
7631 		ar->hw = hw;
7632 		ar->ab = ab;
7633 		ar->pdev = pdev;
7634 		ar->pdev_idx = i;
7635 		ar->lmac_id = ath12k_hw_get_mac_from_pdev_id(ab->hw_params, i);
7636 
7637 		ar->wmi = &ab->wmi_ab.wmi[i];
7638 		/* FIXME: wmi[0] is already initialized during attach,
7639 		 * Should we do this again?
7640 		 */
7641 		ath12k_wmi_pdev_attach(ab, i);
7642 
7643 		ar->cfg_tx_chainmask = pdev->cap.tx_chain_mask;
7644 		ar->cfg_rx_chainmask = pdev->cap.rx_chain_mask;
7645 		ar->num_tx_chains = hweight32(pdev->cap.tx_chain_mask);
7646 		ar->num_rx_chains = hweight32(pdev->cap.rx_chain_mask);
7647 
7648 		pdev->ar = ar;
7649 		spin_lock_init(&ar->data_lock);
7650 		INIT_LIST_HEAD(&ar->arvifs);
7651 		INIT_LIST_HEAD(&ar->ppdu_stats_info);
7652 		mutex_init(&ar->conf_mutex);
7653 		init_completion(&ar->vdev_setup_done);
7654 		init_completion(&ar->vdev_delete_done);
7655 		init_completion(&ar->peer_assoc_done);
7656 		init_completion(&ar->peer_delete_done);
7657 		init_completion(&ar->install_key_done);
7658 		init_completion(&ar->bss_survey_done);
7659 		init_completion(&ar->scan.started);
7660 		init_completion(&ar->scan.completed);
7661 
7662 		INIT_DELAYED_WORK(&ar->scan.timeout, ath12k_scan_timeout_work);
7663 		INIT_WORK(&ar->regd_update_work, ath12k_regd_update_work);
7664 
7665 		INIT_WORK(&ar->wmi_mgmt_tx_work, ath12k_mgmt_over_wmi_tx_work);
7666 		skb_queue_head_init(&ar->wmi_mgmt_tx_queue);
7667 		clear_bit(ATH12K_FLAG_MONITOR_ENABLED, &ar->monitor_flags);
7668 	}
7669 
7670 	return 0;
7671 
7672 err_free_mac:
7673 	ath12k_mac_destroy(ab);
7674 
7675 	return ret;
7676 }
7677 
7678 void ath12k_mac_destroy(struct ath12k_base *ab)
7679 {
7680 	struct ath12k *ar;
7681 	struct ath12k_pdev *pdev;
7682 	int i;
7683 
7684 	for (i = 0; i < ab->num_radios; i++) {
7685 		pdev = &ab->pdevs[i];
7686 		ar = pdev->ar;
7687 		if (!ar)
7688 			continue;
7689 
7690 		ieee80211_free_hw(ar->hw);
7691 		pdev->ar = NULL;
7692 	}
7693 }
7694