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