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