xref: /openbmc/linux/drivers/net/wireless/ath/ath12k/mac.c (revision 00d0f31a)
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, bw_prev;
3224 	int err, num_vht_rates;
3225 	const struct cfg80211_bitrate_mask *mask;
3226 	struct ath12k_wmi_peer_assoc_arg peer_arg;
3227 	enum wmi_phy_mode peer_phymode;
3228 
3229 	arsta = container_of(wk, struct ath12k_sta, update_wk);
3230 	sta = container_of((void *)arsta, struct ieee80211_sta, drv_priv);
3231 	arvif = arsta->arvif;
3232 	ar = arvif->ar;
3233 
3234 	if (WARN_ON(ath12k_mac_vif_chan(arvif->vif, &def)))
3235 		return;
3236 
3237 	band = def.chan->band;
3238 	ht_mcs_mask = arvif->bitrate_mask.control[band].ht_mcs;
3239 	vht_mcs_mask = arvif->bitrate_mask.control[band].vht_mcs;
3240 
3241 	spin_lock_bh(&ar->data_lock);
3242 
3243 	changed = arsta->changed;
3244 	arsta->changed = 0;
3245 
3246 	bw = arsta->bw;
3247 	bw_prev = arsta->bw_prev;
3248 	nss = arsta->nss;
3249 	smps = arsta->smps;
3250 
3251 	spin_unlock_bh(&ar->data_lock);
3252 
3253 	mutex_lock(&ar->conf_mutex);
3254 
3255 	nss = max_t(u32, 1, nss);
3256 	nss = min(nss, max(ath12k_mac_max_ht_nss(ht_mcs_mask),
3257 			   ath12k_mac_max_vht_nss(vht_mcs_mask)));
3258 
3259 	if (changed & IEEE80211_RC_BW_CHANGED) {
3260 		ath12k_peer_assoc_h_phymode(ar, arvif->vif, sta, &peer_arg);
3261 		peer_phymode = peer_arg.peer_phymode;
3262 
3263 		if (bw > bw_prev) {
3264 			/* Phymode shows maximum supported channel width, if we
3265 			 * upgrade bandwidth then due to sanity check of firmware,
3266 			 * we have to send WMI_PEER_PHYMODE followed by
3267 			 * WMI_PEER_CHWIDTH
3268 			 */
3269 			ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac bandwidth upgrade for sta %pM new %d old %d\n",
3270 				   sta->addr, bw, bw_prev);
3271 			err = ath12k_wmi_set_peer_param(ar, sta->addr,
3272 							arvif->vdev_id, WMI_PEER_PHYMODE,
3273 							peer_phymode);
3274 			if (err) {
3275 				ath12k_warn(ar->ab, "failed to update STA %pM to peer phymode %d: %d\n",
3276 					    sta->addr, peer_phymode, err);
3277 				goto err_rc_bw_changed;
3278 			}
3279 			err = ath12k_wmi_set_peer_param(ar, sta->addr,
3280 							arvif->vdev_id, WMI_PEER_CHWIDTH,
3281 							bw);
3282 			if (err)
3283 				ath12k_warn(ar->ab, "failed to update STA %pM to peer bandwidth %d: %d\n",
3284 					    sta->addr, bw, err);
3285 		} else {
3286 			/* When we downgrade bandwidth this will conflict with phymode
3287 			 * and cause to trigger firmware crash. In this case we send
3288 			 * WMI_PEER_CHWIDTH followed by WMI_PEER_PHYMODE
3289 			 */
3290 			ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac bandwidth downgrade for sta %pM new %d old %d\n",
3291 				   sta->addr, bw, bw_prev);
3292 			err = ath12k_wmi_set_peer_param(ar, sta->addr,
3293 							arvif->vdev_id, WMI_PEER_CHWIDTH,
3294 							bw);
3295 			if (err) {
3296 				ath12k_warn(ar->ab, "failed to update STA %pM peer to bandwidth %d: %d\n",
3297 					    sta->addr, bw, err);
3298 				goto err_rc_bw_changed;
3299 			}
3300 			err = ath12k_wmi_set_peer_param(ar, sta->addr,
3301 							arvif->vdev_id, WMI_PEER_PHYMODE,
3302 							peer_phymode);
3303 			if (err)
3304 				ath12k_warn(ar->ab, "failed to update STA %pM to peer phymode %d: %d\n",
3305 					    sta->addr, peer_phymode, err);
3306 		}
3307 	}
3308 
3309 	if (changed & IEEE80211_RC_NSS_CHANGED) {
3310 		ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac update sta %pM nss %d\n",
3311 			   sta->addr, nss);
3312 
3313 		err = ath12k_wmi_set_peer_param(ar, sta->addr, arvif->vdev_id,
3314 						WMI_PEER_NSS, nss);
3315 		if (err)
3316 			ath12k_warn(ar->ab, "failed to update STA %pM nss %d: %d\n",
3317 				    sta->addr, nss, err);
3318 	}
3319 
3320 	if (changed & IEEE80211_RC_SMPS_CHANGED) {
3321 		ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac update sta %pM smps %d\n",
3322 			   sta->addr, smps);
3323 
3324 		err = ath12k_wmi_set_peer_param(ar, sta->addr, arvif->vdev_id,
3325 						WMI_PEER_MIMO_PS_STATE, smps);
3326 		if (err)
3327 			ath12k_warn(ar->ab, "failed to update STA %pM smps %d: %d\n",
3328 				    sta->addr, smps, err);
3329 	}
3330 
3331 	if (changed & IEEE80211_RC_SUPP_RATES_CHANGED) {
3332 		mask = &arvif->bitrate_mask;
3333 		num_vht_rates = ath12k_mac_bitrate_mask_num_vht_rates(ar, band,
3334 								      mask);
3335 
3336 		/* Peer_assoc_prepare will reject vht rates in
3337 		 * bitrate_mask if its not available in range format and
3338 		 * sets vht tx_rateset as unsupported. So multiple VHT MCS
3339 		 * setting(eg. MCS 4,5,6) per peer is not supported here.
3340 		 * But, Single rate in VHT mask can be set as per-peer
3341 		 * fixed rate. But even if any HT rates are configured in
3342 		 * the bitrate mask, device will not switch to those rates
3343 		 * when per-peer Fixed rate is set.
3344 		 * TODO: Check RATEMASK_CMDID to support auto rates selection
3345 		 * across HT/VHT and for multiple VHT MCS support.
3346 		 */
3347 		if (sta->deflink.vht_cap.vht_supported && num_vht_rates == 1) {
3348 			ath12k_mac_set_peer_vht_fixed_rate(arvif, sta, mask,
3349 							   band);
3350 		} else {
3351 			/* If the peer is non-VHT or no fixed VHT rate
3352 			 * is provided in the new bitrate mask we set the
3353 			 * other rates using peer_assoc command.
3354 			 */
3355 			ath12k_peer_assoc_prepare(ar, arvif->vif, sta,
3356 						  &peer_arg, true);
3357 
3358 			err = ath12k_wmi_send_peer_assoc_cmd(ar, &peer_arg);
3359 			if (err)
3360 				ath12k_warn(ar->ab, "failed to run peer assoc for STA %pM vdev %i: %d\n",
3361 					    sta->addr, arvif->vdev_id, err);
3362 
3363 			if (!wait_for_completion_timeout(&ar->peer_assoc_done, 1 * HZ))
3364 				ath12k_warn(ar->ab, "failed to get peer assoc conf event for %pM vdev %i\n",
3365 					    sta->addr, arvif->vdev_id);
3366 		}
3367 	}
3368 err_rc_bw_changed:
3369 	mutex_unlock(&ar->conf_mutex);
3370 }
3371 
3372 static int ath12k_mac_inc_num_stations(struct ath12k_vif *arvif,
3373 				       struct ieee80211_sta *sta)
3374 {
3375 	struct ath12k *ar = arvif->ar;
3376 
3377 	lockdep_assert_held(&ar->conf_mutex);
3378 
3379 	if (arvif->vdev_type == WMI_VDEV_TYPE_STA && !sta->tdls)
3380 		return 0;
3381 
3382 	if (ar->num_stations >= ar->max_num_stations)
3383 		return -ENOBUFS;
3384 
3385 	ar->num_stations++;
3386 
3387 	return 0;
3388 }
3389 
3390 static void ath12k_mac_dec_num_stations(struct ath12k_vif *arvif,
3391 					struct ieee80211_sta *sta)
3392 {
3393 	struct ath12k *ar = arvif->ar;
3394 
3395 	lockdep_assert_held(&ar->conf_mutex);
3396 
3397 	if (arvif->vdev_type == WMI_VDEV_TYPE_STA && !sta->tdls)
3398 		return;
3399 
3400 	ar->num_stations--;
3401 }
3402 
3403 static int ath12k_mac_station_add(struct ath12k *ar,
3404 				  struct ieee80211_vif *vif,
3405 				  struct ieee80211_sta *sta)
3406 {
3407 	struct ath12k_base *ab = ar->ab;
3408 	struct ath12k_vif *arvif = ath12k_vif_to_arvif(vif);
3409 	struct ath12k_sta *arsta = (struct ath12k_sta *)sta->drv_priv;
3410 	struct ath12k_wmi_peer_create_arg peer_param;
3411 	int ret;
3412 
3413 	lockdep_assert_held(&ar->conf_mutex);
3414 
3415 	ret = ath12k_mac_inc_num_stations(arvif, sta);
3416 	if (ret) {
3417 		ath12k_warn(ab, "refusing to associate station: too many connected already (%d)\n",
3418 			    ar->max_num_stations);
3419 		goto exit;
3420 	}
3421 
3422 	arsta->rx_stats = kzalloc(sizeof(*arsta->rx_stats), GFP_KERNEL);
3423 	if (!arsta->rx_stats) {
3424 		ret = -ENOMEM;
3425 		goto dec_num_station;
3426 	}
3427 
3428 	peer_param.vdev_id = arvif->vdev_id;
3429 	peer_param.peer_addr = sta->addr;
3430 	peer_param.peer_type = WMI_PEER_TYPE_DEFAULT;
3431 
3432 	ret = ath12k_peer_create(ar, arvif, sta, &peer_param);
3433 	if (ret) {
3434 		ath12k_warn(ab, "Failed to add peer: %pM for VDEV: %d\n",
3435 			    sta->addr, arvif->vdev_id);
3436 		goto free_peer;
3437 	}
3438 
3439 	ath12k_dbg(ab, ATH12K_DBG_MAC, "Added peer: %pM for VDEV: %d\n",
3440 		   sta->addr, arvif->vdev_id);
3441 
3442 	if (ieee80211_vif_is_mesh(vif)) {
3443 		ret = ath12k_wmi_set_peer_param(ar, sta->addr,
3444 						arvif->vdev_id,
3445 						WMI_PEER_USE_4ADDR, 1);
3446 		if (ret) {
3447 			ath12k_warn(ab, "failed to STA %pM 4addr capability: %d\n",
3448 				    sta->addr, ret);
3449 			goto free_peer;
3450 		}
3451 	}
3452 
3453 	ret = ath12k_dp_peer_setup(ar, arvif->vdev_id, sta->addr);
3454 	if (ret) {
3455 		ath12k_warn(ab, "failed to setup dp for peer %pM on vdev %i (%d)\n",
3456 			    sta->addr, arvif->vdev_id, ret);
3457 		goto free_peer;
3458 	}
3459 
3460 	if (ab->hw_params->vdev_start_delay &&
3461 	    !arvif->is_started &&
3462 	    arvif->vdev_type != WMI_VDEV_TYPE_AP) {
3463 		ret = ath12k_start_vdev_delay(ar->hw, vif);
3464 		if (ret) {
3465 			ath12k_warn(ab, "failed to delay vdev start: %d\n", ret);
3466 			goto free_peer;
3467 		}
3468 	}
3469 
3470 	return 0;
3471 
3472 free_peer:
3473 	ath12k_peer_delete(ar, arvif->vdev_id, sta->addr);
3474 dec_num_station:
3475 	ath12k_mac_dec_num_stations(arvif, sta);
3476 exit:
3477 	return ret;
3478 }
3479 
3480 static u32 ath12k_mac_ieee80211_sta_bw_to_wmi(struct ath12k *ar,
3481 					      struct ieee80211_sta *sta)
3482 {
3483 	u32 bw = WMI_PEER_CHWIDTH_20MHZ;
3484 
3485 	switch (sta->deflink.bandwidth) {
3486 	case IEEE80211_STA_RX_BW_20:
3487 		bw = WMI_PEER_CHWIDTH_20MHZ;
3488 		break;
3489 	case IEEE80211_STA_RX_BW_40:
3490 		bw = WMI_PEER_CHWIDTH_40MHZ;
3491 		break;
3492 	case IEEE80211_STA_RX_BW_80:
3493 		bw = WMI_PEER_CHWIDTH_80MHZ;
3494 		break;
3495 	case IEEE80211_STA_RX_BW_160:
3496 		bw = WMI_PEER_CHWIDTH_160MHZ;
3497 		break;
3498 	default:
3499 		ath12k_warn(ar->ab, "Invalid bandwidth %d in rc update for %pM\n",
3500 			    sta->deflink.bandwidth, sta->addr);
3501 		bw = WMI_PEER_CHWIDTH_20MHZ;
3502 		break;
3503 	}
3504 
3505 	return bw;
3506 }
3507 
3508 static int ath12k_mac_op_sta_state(struct ieee80211_hw *hw,
3509 				   struct ieee80211_vif *vif,
3510 				   struct ieee80211_sta *sta,
3511 				   enum ieee80211_sta_state old_state,
3512 				   enum ieee80211_sta_state new_state)
3513 {
3514 	struct ath12k *ar = hw->priv;
3515 	struct ath12k_vif *arvif = ath12k_vif_to_arvif(vif);
3516 	struct ath12k_sta *arsta = (struct ath12k_sta *)sta->drv_priv;
3517 	struct ath12k_peer *peer;
3518 	int ret = 0;
3519 
3520 	/* cancel must be done outside the mutex to avoid deadlock */
3521 	if ((old_state == IEEE80211_STA_NONE &&
3522 	     new_state == IEEE80211_STA_NOTEXIST))
3523 		cancel_work_sync(&arsta->update_wk);
3524 
3525 	mutex_lock(&ar->conf_mutex);
3526 
3527 	if (old_state == IEEE80211_STA_NOTEXIST &&
3528 	    new_state == IEEE80211_STA_NONE) {
3529 		memset(arsta, 0, sizeof(*arsta));
3530 		arsta->arvif = arvif;
3531 		INIT_WORK(&arsta->update_wk, ath12k_sta_rc_update_wk);
3532 
3533 		ret = ath12k_mac_station_add(ar, vif, sta);
3534 		if (ret)
3535 			ath12k_warn(ar->ab, "Failed to add station: %pM for VDEV: %d\n",
3536 				    sta->addr, arvif->vdev_id);
3537 	} else if ((old_state == IEEE80211_STA_NONE &&
3538 		    new_state == IEEE80211_STA_NOTEXIST)) {
3539 		ath12k_dp_peer_cleanup(ar, arvif->vdev_id, sta->addr);
3540 
3541 		ret = ath12k_peer_delete(ar, arvif->vdev_id, sta->addr);
3542 		if (ret)
3543 			ath12k_warn(ar->ab, "Failed to delete peer: %pM for VDEV: %d\n",
3544 				    sta->addr, arvif->vdev_id);
3545 		else
3546 			ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "Removed peer: %pM for VDEV: %d\n",
3547 				   sta->addr, arvif->vdev_id);
3548 
3549 		ath12k_mac_dec_num_stations(arvif, sta);
3550 		spin_lock_bh(&ar->ab->base_lock);
3551 		peer = ath12k_peer_find(ar->ab, arvif->vdev_id, sta->addr);
3552 		if (peer && peer->sta == sta) {
3553 			ath12k_warn(ar->ab, "Found peer entry %pM n vdev %i after it was supposedly removed\n",
3554 				    vif->addr, arvif->vdev_id);
3555 			peer->sta = NULL;
3556 			list_del(&peer->list);
3557 			kfree(peer);
3558 			ar->num_peers--;
3559 		}
3560 		spin_unlock_bh(&ar->ab->base_lock);
3561 
3562 		kfree(arsta->rx_stats);
3563 		arsta->rx_stats = NULL;
3564 	} else if (old_state == IEEE80211_STA_AUTH &&
3565 		   new_state == IEEE80211_STA_ASSOC &&
3566 		   (vif->type == NL80211_IFTYPE_AP ||
3567 		    vif->type == NL80211_IFTYPE_MESH_POINT ||
3568 		    vif->type == NL80211_IFTYPE_ADHOC)) {
3569 		ret = ath12k_station_assoc(ar, vif, sta, false);
3570 		if (ret)
3571 			ath12k_warn(ar->ab, "Failed to associate station: %pM\n",
3572 				    sta->addr);
3573 
3574 		spin_lock_bh(&ar->data_lock);
3575 
3576 		arsta->bw = ath12k_mac_ieee80211_sta_bw_to_wmi(ar, sta);
3577 		arsta->bw_prev = sta->deflink.bandwidth;
3578 
3579 		spin_unlock_bh(&ar->data_lock);
3580 	} else if (old_state == IEEE80211_STA_ASSOC &&
3581 		   new_state == IEEE80211_STA_AUTHORIZED) {
3582 		spin_lock_bh(&ar->ab->base_lock);
3583 
3584 		peer = ath12k_peer_find(ar->ab, arvif->vdev_id, sta->addr);
3585 		if (peer)
3586 			peer->is_authorized = true;
3587 
3588 		spin_unlock_bh(&ar->ab->base_lock);
3589 
3590 		if (vif->type == NL80211_IFTYPE_STATION && arvif->is_up) {
3591 			ret = ath12k_wmi_set_peer_param(ar, sta->addr,
3592 							arvif->vdev_id,
3593 							WMI_PEER_AUTHORIZE,
3594 							1);
3595 			if (ret)
3596 				ath12k_warn(ar->ab, "Unable to authorize peer %pM vdev %d: %d\n",
3597 					    sta->addr, arvif->vdev_id, ret);
3598 		}
3599 	} else if (old_state == IEEE80211_STA_AUTHORIZED &&
3600 		   new_state == IEEE80211_STA_ASSOC) {
3601 		spin_lock_bh(&ar->ab->base_lock);
3602 
3603 		peer = ath12k_peer_find(ar->ab, arvif->vdev_id, sta->addr);
3604 		if (peer)
3605 			peer->is_authorized = false;
3606 
3607 		spin_unlock_bh(&ar->ab->base_lock);
3608 	} else if (old_state == IEEE80211_STA_ASSOC &&
3609 		   new_state == IEEE80211_STA_AUTH &&
3610 		   (vif->type == NL80211_IFTYPE_AP ||
3611 		    vif->type == NL80211_IFTYPE_MESH_POINT ||
3612 		    vif->type == NL80211_IFTYPE_ADHOC)) {
3613 		ret = ath12k_station_disassoc(ar, vif, sta);
3614 		if (ret)
3615 			ath12k_warn(ar->ab, "Failed to disassociate station: %pM\n",
3616 				    sta->addr);
3617 	}
3618 
3619 	mutex_unlock(&ar->conf_mutex);
3620 	return ret;
3621 }
3622 
3623 static int ath12k_mac_op_sta_set_txpwr(struct ieee80211_hw *hw,
3624 				       struct ieee80211_vif *vif,
3625 				       struct ieee80211_sta *sta)
3626 {
3627 	struct ath12k *ar = hw->priv;
3628 	struct ath12k_vif *arvif = (void *)vif->drv_priv;
3629 	int ret;
3630 	s16 txpwr;
3631 
3632 	if (sta->deflink.txpwr.type == NL80211_TX_POWER_AUTOMATIC) {
3633 		txpwr = 0;
3634 	} else {
3635 		txpwr = sta->deflink.txpwr.power;
3636 		if (!txpwr)
3637 			return -EINVAL;
3638 	}
3639 
3640 	if (txpwr > ATH12K_TX_POWER_MAX_VAL || txpwr < ATH12K_TX_POWER_MIN_VAL)
3641 		return -EINVAL;
3642 
3643 	mutex_lock(&ar->conf_mutex);
3644 
3645 	ret = ath12k_wmi_set_peer_param(ar, sta->addr, arvif->vdev_id,
3646 					WMI_PEER_USE_FIXED_PWR, txpwr);
3647 	if (ret) {
3648 		ath12k_warn(ar->ab, "failed to set tx power for station ret: %d\n",
3649 			    ret);
3650 		goto out;
3651 	}
3652 
3653 out:
3654 	mutex_unlock(&ar->conf_mutex);
3655 	return ret;
3656 }
3657 
3658 static void ath12k_mac_op_sta_rc_update(struct ieee80211_hw *hw,
3659 					struct ieee80211_vif *vif,
3660 					struct ieee80211_sta *sta,
3661 					u32 changed)
3662 {
3663 	struct ath12k *ar = hw->priv;
3664 	struct ath12k_sta *arsta = (struct ath12k_sta *)sta->drv_priv;
3665 	struct ath12k_vif *arvif = (void *)vif->drv_priv;
3666 	struct ath12k_peer *peer;
3667 	u32 bw, smps;
3668 
3669 	spin_lock_bh(&ar->ab->base_lock);
3670 
3671 	peer = ath12k_peer_find(ar->ab, arvif->vdev_id, sta->addr);
3672 	if (!peer) {
3673 		spin_unlock_bh(&ar->ab->base_lock);
3674 		ath12k_warn(ar->ab, "mac sta rc update failed to find peer %pM on vdev %i\n",
3675 			    sta->addr, arvif->vdev_id);
3676 		return;
3677 	}
3678 
3679 	spin_unlock_bh(&ar->ab->base_lock);
3680 
3681 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
3682 		   "mac sta rc update for %pM changed %08x bw %d nss %d smps %d\n",
3683 		   sta->addr, changed, sta->deflink.bandwidth, sta->deflink.rx_nss,
3684 		   sta->deflink.smps_mode);
3685 
3686 	spin_lock_bh(&ar->data_lock);
3687 
3688 	if (changed & IEEE80211_RC_BW_CHANGED) {
3689 		bw = ath12k_mac_ieee80211_sta_bw_to_wmi(ar, sta);
3690 		arsta->bw_prev = arsta->bw;
3691 		arsta->bw = bw;
3692 	}
3693 
3694 	if (changed & IEEE80211_RC_NSS_CHANGED)
3695 		arsta->nss = sta->deflink.rx_nss;
3696 
3697 	if (changed & IEEE80211_RC_SMPS_CHANGED) {
3698 		smps = WMI_PEER_SMPS_PS_NONE;
3699 
3700 		switch (sta->deflink.smps_mode) {
3701 		case IEEE80211_SMPS_AUTOMATIC:
3702 		case IEEE80211_SMPS_OFF:
3703 			smps = WMI_PEER_SMPS_PS_NONE;
3704 			break;
3705 		case IEEE80211_SMPS_STATIC:
3706 			smps = WMI_PEER_SMPS_STATIC;
3707 			break;
3708 		case IEEE80211_SMPS_DYNAMIC:
3709 			smps = WMI_PEER_SMPS_DYNAMIC;
3710 			break;
3711 		default:
3712 			ath12k_warn(ar->ab, "Invalid smps %d in sta rc update for %pM\n",
3713 				    sta->deflink.smps_mode, sta->addr);
3714 			smps = WMI_PEER_SMPS_PS_NONE;
3715 			break;
3716 		}
3717 
3718 		arsta->smps = smps;
3719 	}
3720 
3721 	arsta->changed |= changed;
3722 
3723 	spin_unlock_bh(&ar->data_lock);
3724 
3725 	ieee80211_queue_work(hw, &arsta->update_wk);
3726 }
3727 
3728 static int ath12k_conf_tx_uapsd(struct ath12k *ar, struct ieee80211_vif *vif,
3729 				u16 ac, bool enable)
3730 {
3731 	struct ath12k_vif *arvif = ath12k_vif_to_arvif(vif);
3732 	u32 value;
3733 	int ret;
3734 
3735 	if (arvif->vdev_type != WMI_VDEV_TYPE_STA)
3736 		return 0;
3737 
3738 	switch (ac) {
3739 	case IEEE80211_AC_VO:
3740 		value = WMI_STA_PS_UAPSD_AC3_DELIVERY_EN |
3741 			WMI_STA_PS_UAPSD_AC3_TRIGGER_EN;
3742 		break;
3743 	case IEEE80211_AC_VI:
3744 		value = WMI_STA_PS_UAPSD_AC2_DELIVERY_EN |
3745 			WMI_STA_PS_UAPSD_AC2_TRIGGER_EN;
3746 		break;
3747 	case IEEE80211_AC_BE:
3748 		value = WMI_STA_PS_UAPSD_AC1_DELIVERY_EN |
3749 			WMI_STA_PS_UAPSD_AC1_TRIGGER_EN;
3750 		break;
3751 	case IEEE80211_AC_BK:
3752 		value = WMI_STA_PS_UAPSD_AC0_DELIVERY_EN |
3753 			WMI_STA_PS_UAPSD_AC0_TRIGGER_EN;
3754 		break;
3755 	}
3756 
3757 	if (enable)
3758 		arvif->u.sta.uapsd |= value;
3759 	else
3760 		arvif->u.sta.uapsd &= ~value;
3761 
3762 	ret = ath12k_wmi_set_sta_ps_param(ar, arvif->vdev_id,
3763 					  WMI_STA_PS_PARAM_UAPSD,
3764 					  arvif->u.sta.uapsd);
3765 	if (ret) {
3766 		ath12k_warn(ar->ab, "could not set uapsd params %d\n", ret);
3767 		goto exit;
3768 	}
3769 
3770 	if (arvif->u.sta.uapsd)
3771 		value = WMI_STA_PS_RX_WAKE_POLICY_POLL_UAPSD;
3772 	else
3773 		value = WMI_STA_PS_RX_WAKE_POLICY_WAKE;
3774 
3775 	ret = ath12k_wmi_set_sta_ps_param(ar, arvif->vdev_id,
3776 					  WMI_STA_PS_PARAM_RX_WAKE_POLICY,
3777 					  value);
3778 	if (ret)
3779 		ath12k_warn(ar->ab, "could not set rx wake param %d\n", ret);
3780 
3781 exit:
3782 	return ret;
3783 }
3784 
3785 static int ath12k_mac_op_conf_tx(struct ieee80211_hw *hw,
3786 				 struct ieee80211_vif *vif,
3787 				 unsigned int link_id, u16 ac,
3788 				 const struct ieee80211_tx_queue_params *params)
3789 {
3790 	struct ath12k *ar = hw->priv;
3791 	struct ath12k_vif *arvif = (void *)vif->drv_priv;
3792 	struct wmi_wmm_params_arg *p = NULL;
3793 	int ret;
3794 
3795 	mutex_lock(&ar->conf_mutex);
3796 
3797 	switch (ac) {
3798 	case IEEE80211_AC_VO:
3799 		p = &arvif->wmm_params.ac_vo;
3800 		break;
3801 	case IEEE80211_AC_VI:
3802 		p = &arvif->wmm_params.ac_vi;
3803 		break;
3804 	case IEEE80211_AC_BE:
3805 		p = &arvif->wmm_params.ac_be;
3806 		break;
3807 	case IEEE80211_AC_BK:
3808 		p = &arvif->wmm_params.ac_bk;
3809 		break;
3810 	}
3811 
3812 	if (WARN_ON(!p)) {
3813 		ret = -EINVAL;
3814 		goto exit;
3815 	}
3816 
3817 	p->cwmin = params->cw_min;
3818 	p->cwmax = params->cw_max;
3819 	p->aifs = params->aifs;
3820 	p->txop = params->txop;
3821 
3822 	ret = ath12k_wmi_send_wmm_update_cmd(ar, arvif->vdev_id,
3823 					     &arvif->wmm_params);
3824 	if (ret) {
3825 		ath12k_warn(ar->ab, "failed to set wmm params: %d\n", ret);
3826 		goto exit;
3827 	}
3828 
3829 	ret = ath12k_conf_tx_uapsd(ar, vif, ac, params->uapsd);
3830 
3831 	if (ret)
3832 		ath12k_warn(ar->ab, "failed to set sta uapsd: %d\n", ret);
3833 
3834 exit:
3835 	mutex_unlock(&ar->conf_mutex);
3836 	return ret;
3837 }
3838 
3839 static struct ieee80211_sta_ht_cap
3840 ath12k_create_ht_cap(struct ath12k *ar, u32 ar_ht_cap, u32 rate_cap_rx_chainmask)
3841 {
3842 	int i;
3843 	struct ieee80211_sta_ht_cap ht_cap = {0};
3844 	u32 ar_vht_cap = ar->pdev->cap.vht_cap;
3845 
3846 	if (!(ar_ht_cap & WMI_HT_CAP_ENABLED))
3847 		return ht_cap;
3848 
3849 	ht_cap.ht_supported = 1;
3850 	ht_cap.ampdu_factor = IEEE80211_HT_MAX_AMPDU_64K;
3851 	ht_cap.ampdu_density = IEEE80211_HT_MPDU_DENSITY_NONE;
3852 	ht_cap.cap |= IEEE80211_HT_CAP_SUP_WIDTH_20_40;
3853 	ht_cap.cap |= IEEE80211_HT_CAP_DSSSCCK40;
3854 	ht_cap.cap |= WLAN_HT_CAP_SM_PS_STATIC << IEEE80211_HT_CAP_SM_PS_SHIFT;
3855 
3856 	if (ar_ht_cap & WMI_HT_CAP_HT20_SGI)
3857 		ht_cap.cap |= IEEE80211_HT_CAP_SGI_20;
3858 
3859 	if (ar_ht_cap & WMI_HT_CAP_HT40_SGI)
3860 		ht_cap.cap |= IEEE80211_HT_CAP_SGI_40;
3861 
3862 	if (ar_ht_cap & WMI_HT_CAP_DYNAMIC_SMPS) {
3863 		u32 smps;
3864 
3865 		smps   = WLAN_HT_CAP_SM_PS_DYNAMIC;
3866 		smps <<= IEEE80211_HT_CAP_SM_PS_SHIFT;
3867 
3868 		ht_cap.cap |= smps;
3869 	}
3870 
3871 	if (ar_ht_cap & WMI_HT_CAP_TX_STBC)
3872 		ht_cap.cap |= IEEE80211_HT_CAP_TX_STBC;
3873 
3874 	if (ar_ht_cap & WMI_HT_CAP_RX_STBC) {
3875 		u32 stbc;
3876 
3877 		stbc   = ar_ht_cap;
3878 		stbc  &= WMI_HT_CAP_RX_STBC;
3879 		stbc >>= WMI_HT_CAP_RX_STBC_MASK_SHIFT;
3880 		stbc <<= IEEE80211_HT_CAP_RX_STBC_SHIFT;
3881 		stbc  &= IEEE80211_HT_CAP_RX_STBC;
3882 
3883 		ht_cap.cap |= stbc;
3884 	}
3885 
3886 	if (ar_ht_cap & WMI_HT_CAP_RX_LDPC)
3887 		ht_cap.cap |= IEEE80211_HT_CAP_LDPC_CODING;
3888 
3889 	if (ar_ht_cap & WMI_HT_CAP_L_SIG_TXOP_PROT)
3890 		ht_cap.cap |= IEEE80211_HT_CAP_LSIG_TXOP_PROT;
3891 
3892 	if (ar_vht_cap & WMI_VHT_CAP_MAX_MPDU_LEN_MASK)
3893 		ht_cap.cap |= IEEE80211_HT_CAP_MAX_AMSDU;
3894 
3895 	for (i = 0; i < ar->num_rx_chains; i++) {
3896 		if (rate_cap_rx_chainmask & BIT(i))
3897 			ht_cap.mcs.rx_mask[i] = 0xFF;
3898 	}
3899 
3900 	ht_cap.mcs.tx_params |= IEEE80211_HT_MCS_TX_DEFINED;
3901 
3902 	return ht_cap;
3903 }
3904 
3905 static int ath12k_mac_set_txbf_conf(struct ath12k_vif *arvif)
3906 {
3907 	u32 value = 0;
3908 	struct ath12k *ar = arvif->ar;
3909 	int nsts;
3910 	int sound_dim;
3911 	u32 vht_cap = ar->pdev->cap.vht_cap;
3912 	u32 vdev_param = WMI_VDEV_PARAM_TXBF;
3913 
3914 	if (vht_cap & (IEEE80211_VHT_CAP_SU_BEAMFORMEE_CAPABLE)) {
3915 		nsts = vht_cap & IEEE80211_VHT_CAP_BEAMFORMEE_STS_MASK;
3916 		nsts >>= IEEE80211_VHT_CAP_BEAMFORMEE_STS_SHIFT;
3917 		value |= SM(nsts, WMI_TXBF_STS_CAP_OFFSET);
3918 	}
3919 
3920 	if (vht_cap & (IEEE80211_VHT_CAP_SU_BEAMFORMER_CAPABLE)) {
3921 		sound_dim = vht_cap &
3922 			    IEEE80211_VHT_CAP_SOUNDING_DIMENSIONS_MASK;
3923 		sound_dim >>= IEEE80211_VHT_CAP_SOUNDING_DIMENSIONS_SHIFT;
3924 		if (sound_dim > (ar->num_tx_chains - 1))
3925 			sound_dim = ar->num_tx_chains - 1;
3926 		value |= SM(sound_dim, WMI_BF_SOUND_DIM_OFFSET);
3927 	}
3928 
3929 	if (!value)
3930 		return 0;
3931 
3932 	if (vht_cap & IEEE80211_VHT_CAP_SU_BEAMFORMER_CAPABLE) {
3933 		value |= WMI_VDEV_PARAM_TXBF_SU_TX_BFER;
3934 
3935 		if ((vht_cap & IEEE80211_VHT_CAP_MU_BEAMFORMER_CAPABLE) &&
3936 		    arvif->vdev_type == WMI_VDEV_TYPE_AP)
3937 			value |= WMI_VDEV_PARAM_TXBF_MU_TX_BFER;
3938 	}
3939 
3940 	if (vht_cap & IEEE80211_VHT_CAP_SU_BEAMFORMEE_CAPABLE) {
3941 		value |= WMI_VDEV_PARAM_TXBF_SU_TX_BFEE;
3942 
3943 		if ((vht_cap & IEEE80211_VHT_CAP_MU_BEAMFORMEE_CAPABLE) &&
3944 		    arvif->vdev_type == WMI_VDEV_TYPE_STA)
3945 			value |= WMI_VDEV_PARAM_TXBF_MU_TX_BFEE;
3946 	}
3947 
3948 	return ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
3949 					     vdev_param, value);
3950 }
3951 
3952 static void ath12k_set_vht_txbf_cap(struct ath12k *ar, u32 *vht_cap)
3953 {
3954 	bool subfer, subfee;
3955 	int sound_dim = 0;
3956 
3957 	subfer = !!(*vht_cap & (IEEE80211_VHT_CAP_SU_BEAMFORMER_CAPABLE));
3958 	subfee = !!(*vht_cap & (IEEE80211_VHT_CAP_SU_BEAMFORMEE_CAPABLE));
3959 
3960 	if (ar->num_tx_chains < 2) {
3961 		*vht_cap &= ~(IEEE80211_VHT_CAP_SU_BEAMFORMER_CAPABLE);
3962 		subfer = false;
3963 	}
3964 
3965 	/* If SU Beaformer is not set, then disable MU Beamformer Capability */
3966 	if (!subfer)
3967 		*vht_cap &= ~(IEEE80211_VHT_CAP_MU_BEAMFORMER_CAPABLE);
3968 
3969 	/* If SU Beaformee is not set, then disable MU Beamformee Capability */
3970 	if (!subfee)
3971 		*vht_cap &= ~(IEEE80211_VHT_CAP_MU_BEAMFORMEE_CAPABLE);
3972 
3973 	sound_dim = u32_get_bits(*vht_cap,
3974 				 IEEE80211_VHT_CAP_SOUNDING_DIMENSIONS_MASK);
3975 	*vht_cap = u32_replace_bits(*vht_cap, 0,
3976 				    IEEE80211_VHT_CAP_SOUNDING_DIMENSIONS_MASK);
3977 
3978 	/* TODO: Need to check invalid STS and Sound_dim values set by FW? */
3979 
3980 	/* Enable Sounding Dimension Field only if SU BF is enabled */
3981 	if (subfer) {
3982 		if (sound_dim > (ar->num_tx_chains - 1))
3983 			sound_dim = ar->num_tx_chains - 1;
3984 
3985 		*vht_cap = u32_replace_bits(*vht_cap, sound_dim,
3986 					    IEEE80211_VHT_CAP_SOUNDING_DIMENSIONS_MASK);
3987 	}
3988 
3989 	/* Use the STS advertised by FW unless SU Beamformee is not supported*/
3990 	if (!subfee)
3991 		*vht_cap &= ~(IEEE80211_VHT_CAP_BEAMFORMEE_STS_MASK);
3992 }
3993 
3994 static struct ieee80211_sta_vht_cap
3995 ath12k_create_vht_cap(struct ath12k *ar, u32 rate_cap_tx_chainmask,
3996 		      u32 rate_cap_rx_chainmask)
3997 {
3998 	struct ieee80211_sta_vht_cap vht_cap = {0};
3999 	u16 txmcs_map, rxmcs_map;
4000 	int i;
4001 
4002 	vht_cap.vht_supported = 1;
4003 	vht_cap.cap = ar->pdev->cap.vht_cap;
4004 
4005 	ath12k_set_vht_txbf_cap(ar, &vht_cap.cap);
4006 
4007 	/* TODO: Enable back VHT160 mode once association issues are fixed */
4008 	/* Disabling VHT160 and VHT80+80 modes */
4009 	vht_cap.cap &= ~IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_MASK;
4010 	vht_cap.cap &= ~IEEE80211_VHT_CAP_SHORT_GI_160;
4011 
4012 	rxmcs_map = 0;
4013 	txmcs_map = 0;
4014 	for (i = 0; i < 8; i++) {
4015 		if (i < ar->num_tx_chains && rate_cap_tx_chainmask & BIT(i))
4016 			txmcs_map |= IEEE80211_VHT_MCS_SUPPORT_0_9 << (i * 2);
4017 		else
4018 			txmcs_map |= IEEE80211_VHT_MCS_NOT_SUPPORTED << (i * 2);
4019 
4020 		if (i < ar->num_rx_chains && rate_cap_rx_chainmask & BIT(i))
4021 			rxmcs_map |= IEEE80211_VHT_MCS_SUPPORT_0_9 << (i * 2);
4022 		else
4023 			rxmcs_map |= IEEE80211_VHT_MCS_NOT_SUPPORTED << (i * 2);
4024 	}
4025 
4026 	if (rate_cap_tx_chainmask <= 1)
4027 		vht_cap.cap &= ~IEEE80211_VHT_CAP_TXSTBC;
4028 
4029 	vht_cap.vht_mcs.rx_mcs_map = cpu_to_le16(rxmcs_map);
4030 	vht_cap.vht_mcs.tx_mcs_map = cpu_to_le16(txmcs_map);
4031 
4032 	return vht_cap;
4033 }
4034 
4035 static void ath12k_mac_setup_ht_vht_cap(struct ath12k *ar,
4036 					struct ath12k_pdev_cap *cap,
4037 					u32 *ht_cap_info)
4038 {
4039 	struct ieee80211_supported_band *band;
4040 	u32 rate_cap_tx_chainmask;
4041 	u32 rate_cap_rx_chainmask;
4042 	u32 ht_cap;
4043 
4044 	rate_cap_tx_chainmask = ar->cfg_tx_chainmask >> cap->tx_chain_mask_shift;
4045 	rate_cap_rx_chainmask = ar->cfg_rx_chainmask >> cap->rx_chain_mask_shift;
4046 
4047 	if (cap->supported_bands & WMI_HOST_WLAN_2G_CAP) {
4048 		band = &ar->mac.sbands[NL80211_BAND_2GHZ];
4049 		ht_cap = cap->band[NL80211_BAND_2GHZ].ht_cap_info;
4050 		if (ht_cap_info)
4051 			*ht_cap_info = ht_cap;
4052 		band->ht_cap = ath12k_create_ht_cap(ar, ht_cap,
4053 						    rate_cap_rx_chainmask);
4054 	}
4055 
4056 	if (cap->supported_bands & WMI_HOST_WLAN_5G_CAP &&
4057 	    (ar->ab->hw_params->single_pdev_only ||
4058 	     !ar->supports_6ghz)) {
4059 		band = &ar->mac.sbands[NL80211_BAND_5GHZ];
4060 		ht_cap = cap->band[NL80211_BAND_5GHZ].ht_cap_info;
4061 		if (ht_cap_info)
4062 			*ht_cap_info = ht_cap;
4063 		band->ht_cap = ath12k_create_ht_cap(ar, ht_cap,
4064 						    rate_cap_rx_chainmask);
4065 		band->vht_cap = ath12k_create_vht_cap(ar, rate_cap_tx_chainmask,
4066 						      rate_cap_rx_chainmask);
4067 	}
4068 }
4069 
4070 static int ath12k_check_chain_mask(struct ath12k *ar, u32 ant, bool is_tx_ant)
4071 {
4072 	/* TODO: Check the request chainmask against the supported
4073 	 * chainmask table which is advertised in extented_service_ready event
4074 	 */
4075 
4076 	return 0;
4077 }
4078 
4079 static void ath12k_gen_ppe_thresh(struct ath12k_wmi_ppe_threshold_arg *fw_ppet,
4080 				  u8 *he_ppet)
4081 {
4082 	int nss, ru;
4083 	u8 bit = 7;
4084 
4085 	he_ppet[0] = fw_ppet->numss_m1 & IEEE80211_PPE_THRES_NSS_MASK;
4086 	he_ppet[0] |= (fw_ppet->ru_bit_mask <<
4087 		       IEEE80211_PPE_THRES_RU_INDEX_BITMASK_POS) &
4088 		      IEEE80211_PPE_THRES_RU_INDEX_BITMASK_MASK;
4089 	for (nss = 0; nss <= fw_ppet->numss_m1; nss++) {
4090 		for (ru = 0; ru < 4; ru++) {
4091 			u8 val;
4092 			int i;
4093 
4094 			if ((fw_ppet->ru_bit_mask & BIT(ru)) == 0)
4095 				continue;
4096 			val = (fw_ppet->ppet16_ppet8_ru3_ru0[nss] >> (ru * 6)) &
4097 			       0x3f;
4098 			val = ((val >> 3) & 0x7) | ((val & 0x7) << 3);
4099 			for (i = 5; i >= 0; i--) {
4100 				he_ppet[bit / 8] |=
4101 					((val >> i) & 0x1) << ((bit % 8));
4102 				bit++;
4103 			}
4104 		}
4105 	}
4106 }
4107 
4108 static void
4109 ath12k_mac_filter_he_cap_mesh(struct ieee80211_he_cap_elem *he_cap_elem)
4110 {
4111 	u8 m;
4112 
4113 	m = IEEE80211_HE_MAC_CAP0_TWT_RES |
4114 	    IEEE80211_HE_MAC_CAP0_TWT_REQ;
4115 	he_cap_elem->mac_cap_info[0] &= ~m;
4116 
4117 	m = IEEE80211_HE_MAC_CAP2_TRS |
4118 	    IEEE80211_HE_MAC_CAP2_BCAST_TWT |
4119 	    IEEE80211_HE_MAC_CAP2_MU_CASCADING;
4120 	he_cap_elem->mac_cap_info[2] &= ~m;
4121 
4122 	m = IEEE80211_HE_MAC_CAP3_FLEX_TWT_SCHED |
4123 	    IEEE80211_HE_MAC_CAP2_BCAST_TWT |
4124 	    IEEE80211_HE_MAC_CAP2_MU_CASCADING;
4125 	he_cap_elem->mac_cap_info[3] &= ~m;
4126 
4127 	m = IEEE80211_HE_MAC_CAP4_BSRP_BQRP_A_MPDU_AGG |
4128 	    IEEE80211_HE_MAC_CAP4_BQR;
4129 	he_cap_elem->mac_cap_info[4] &= ~m;
4130 
4131 	m = IEEE80211_HE_MAC_CAP5_SUBCHAN_SELECTIVE_TRANSMISSION |
4132 	    IEEE80211_HE_MAC_CAP5_UL_2x996_TONE_RU |
4133 	    IEEE80211_HE_MAC_CAP5_PUNCTURED_SOUNDING |
4134 	    IEEE80211_HE_MAC_CAP5_HT_VHT_TRIG_FRAME_RX;
4135 	he_cap_elem->mac_cap_info[5] &= ~m;
4136 
4137 	m = IEEE80211_HE_PHY_CAP2_UL_MU_FULL_MU_MIMO |
4138 	    IEEE80211_HE_PHY_CAP2_UL_MU_PARTIAL_MU_MIMO;
4139 	he_cap_elem->phy_cap_info[2] &= ~m;
4140 
4141 	m = IEEE80211_HE_PHY_CAP3_RX_PARTIAL_BW_SU_IN_20MHZ_MU |
4142 	    IEEE80211_HE_PHY_CAP3_DCM_MAX_CONST_TX_MASK |
4143 	    IEEE80211_HE_PHY_CAP3_DCM_MAX_CONST_RX_MASK;
4144 	he_cap_elem->phy_cap_info[3] &= ~m;
4145 
4146 	m = IEEE80211_HE_PHY_CAP4_MU_BEAMFORMER;
4147 	he_cap_elem->phy_cap_info[4] &= ~m;
4148 
4149 	m = IEEE80211_HE_PHY_CAP5_NG16_MU_FEEDBACK;
4150 	he_cap_elem->phy_cap_info[5] &= ~m;
4151 
4152 	m = IEEE80211_HE_PHY_CAP6_CODEBOOK_SIZE_75_MU |
4153 	    IEEE80211_HE_PHY_CAP6_TRIG_MU_BEAMFORMING_PARTIAL_BW_FB |
4154 	    IEEE80211_HE_PHY_CAP6_TRIG_CQI_FB |
4155 	    IEEE80211_HE_PHY_CAP6_PARTIAL_BANDWIDTH_DL_MUMIMO;
4156 	he_cap_elem->phy_cap_info[6] &= ~m;
4157 
4158 	m = IEEE80211_HE_PHY_CAP7_PSR_BASED_SR |
4159 	    IEEE80211_HE_PHY_CAP7_POWER_BOOST_FACTOR_SUPP |
4160 	    IEEE80211_HE_PHY_CAP7_STBC_TX_ABOVE_80MHZ |
4161 	    IEEE80211_HE_PHY_CAP7_STBC_RX_ABOVE_80MHZ;
4162 	he_cap_elem->phy_cap_info[7] &= ~m;
4163 
4164 	m = IEEE80211_HE_PHY_CAP8_HE_ER_SU_PPDU_4XLTF_AND_08_US_GI |
4165 	    IEEE80211_HE_PHY_CAP8_20MHZ_IN_40MHZ_HE_PPDU_IN_2G |
4166 	    IEEE80211_HE_PHY_CAP8_20MHZ_IN_160MHZ_HE_PPDU |
4167 	    IEEE80211_HE_PHY_CAP8_80MHZ_IN_160MHZ_HE_PPDU;
4168 	he_cap_elem->phy_cap_info[8] &= ~m;
4169 
4170 	m = IEEE80211_HE_PHY_CAP9_LONGER_THAN_16_SIGB_OFDM_SYM |
4171 	    IEEE80211_HE_PHY_CAP9_NON_TRIGGERED_CQI_FEEDBACK |
4172 	    IEEE80211_HE_PHY_CAP9_RX_1024_QAM_LESS_THAN_242_TONE_RU |
4173 	    IEEE80211_HE_PHY_CAP9_TX_1024_QAM_LESS_THAN_242_TONE_RU |
4174 	    IEEE80211_HE_PHY_CAP9_RX_FULL_BW_SU_USING_MU_WITH_COMP_SIGB |
4175 	    IEEE80211_HE_PHY_CAP9_RX_FULL_BW_SU_USING_MU_WITH_NON_COMP_SIGB;
4176 	he_cap_elem->phy_cap_info[9] &= ~m;
4177 }
4178 
4179 static __le16 ath12k_mac_setup_he_6ghz_cap(struct ath12k_pdev_cap *pcap,
4180 					   struct ath12k_band_cap *bcap)
4181 {
4182 	u8 val;
4183 
4184 	bcap->he_6ghz_capa = IEEE80211_HT_MPDU_DENSITY_NONE;
4185 	if (bcap->ht_cap_info & WMI_HT_CAP_DYNAMIC_SMPS)
4186 		bcap->he_6ghz_capa |=
4187 			u32_encode_bits(WLAN_HT_CAP_SM_PS_DYNAMIC,
4188 					IEEE80211_HE_6GHZ_CAP_SM_PS);
4189 	else
4190 		bcap->he_6ghz_capa |=
4191 			u32_encode_bits(WLAN_HT_CAP_SM_PS_DISABLED,
4192 					IEEE80211_HE_6GHZ_CAP_SM_PS);
4193 	val = u32_get_bits(pcap->vht_cap,
4194 			   IEEE80211_VHT_CAP_MAX_A_MPDU_LENGTH_EXPONENT_MASK);
4195 	bcap->he_6ghz_capa |=
4196 		u32_encode_bits(val, IEEE80211_HE_6GHZ_CAP_MAX_AMPDU_LEN_EXP);
4197 	val = u32_get_bits(pcap->vht_cap,
4198 			   IEEE80211_VHT_CAP_MAX_MPDU_MASK);
4199 	bcap->he_6ghz_capa |=
4200 		u32_encode_bits(val, IEEE80211_HE_6GHZ_CAP_MAX_MPDU_LEN);
4201 	if (pcap->vht_cap & IEEE80211_VHT_CAP_RX_ANTENNA_PATTERN)
4202 		bcap->he_6ghz_capa |= IEEE80211_HE_6GHZ_CAP_RX_ANTPAT_CONS;
4203 	if (pcap->vht_cap & IEEE80211_VHT_CAP_TX_ANTENNA_PATTERN)
4204 		bcap->he_6ghz_capa |= IEEE80211_HE_6GHZ_CAP_TX_ANTPAT_CONS;
4205 
4206 	return cpu_to_le16(bcap->he_6ghz_capa);
4207 }
4208 
4209 static int ath12k_mac_copy_he_cap(struct ath12k *ar,
4210 				  struct ath12k_pdev_cap *cap,
4211 				  struct ieee80211_sband_iftype_data *data,
4212 				  int band)
4213 {
4214 	int i, idx = 0;
4215 
4216 	for (i = 0; i < NUM_NL80211_IFTYPES; i++) {
4217 		struct ieee80211_sta_he_cap *he_cap = &data[idx].he_cap;
4218 		struct ath12k_band_cap *band_cap = &cap->band[band];
4219 		struct ieee80211_he_cap_elem *he_cap_elem =
4220 				&he_cap->he_cap_elem;
4221 
4222 		switch (i) {
4223 		case NL80211_IFTYPE_STATION:
4224 		case NL80211_IFTYPE_AP:
4225 		case NL80211_IFTYPE_MESH_POINT:
4226 			break;
4227 
4228 		default:
4229 			continue;
4230 		}
4231 
4232 		data[idx].types_mask = BIT(i);
4233 		he_cap->has_he = true;
4234 		memcpy(he_cap_elem->mac_cap_info, band_cap->he_cap_info,
4235 		       sizeof(he_cap_elem->mac_cap_info));
4236 		memcpy(he_cap_elem->phy_cap_info, band_cap->he_cap_phy_info,
4237 		       sizeof(he_cap_elem->phy_cap_info));
4238 
4239 		he_cap_elem->mac_cap_info[1] &=
4240 			IEEE80211_HE_MAC_CAP1_TF_MAC_PAD_DUR_MASK;
4241 
4242 		he_cap_elem->phy_cap_info[5] &=
4243 			~IEEE80211_HE_PHY_CAP5_BEAMFORMEE_NUM_SND_DIM_UNDER_80MHZ_MASK;
4244 		he_cap_elem->phy_cap_info[5] &=
4245 			~IEEE80211_HE_PHY_CAP5_BEAMFORMEE_NUM_SND_DIM_ABOVE_80MHZ_MASK;
4246 		he_cap_elem->phy_cap_info[5] |= ar->num_tx_chains - 1;
4247 
4248 		switch (i) {
4249 		case NL80211_IFTYPE_AP:
4250 			he_cap_elem->phy_cap_info[3] &=
4251 				~IEEE80211_HE_PHY_CAP3_DCM_MAX_CONST_TX_MASK;
4252 			he_cap_elem->phy_cap_info[9] |=
4253 				IEEE80211_HE_PHY_CAP9_RX_1024_QAM_LESS_THAN_242_TONE_RU;
4254 			break;
4255 		case NL80211_IFTYPE_STATION:
4256 			he_cap_elem->mac_cap_info[0] &=
4257 				~IEEE80211_HE_MAC_CAP0_TWT_RES;
4258 			he_cap_elem->mac_cap_info[0] |=
4259 				IEEE80211_HE_MAC_CAP0_TWT_REQ;
4260 			he_cap_elem->phy_cap_info[9] |=
4261 				IEEE80211_HE_PHY_CAP9_TX_1024_QAM_LESS_THAN_242_TONE_RU;
4262 			break;
4263 		case NL80211_IFTYPE_MESH_POINT:
4264 			ath12k_mac_filter_he_cap_mesh(he_cap_elem);
4265 			break;
4266 		}
4267 
4268 		he_cap->he_mcs_nss_supp.rx_mcs_80 =
4269 			cpu_to_le16(band_cap->he_mcs & 0xffff);
4270 		he_cap->he_mcs_nss_supp.tx_mcs_80 =
4271 			cpu_to_le16(band_cap->he_mcs & 0xffff);
4272 		he_cap->he_mcs_nss_supp.rx_mcs_160 =
4273 			cpu_to_le16((band_cap->he_mcs >> 16) & 0xffff);
4274 		he_cap->he_mcs_nss_supp.tx_mcs_160 =
4275 			cpu_to_le16((band_cap->he_mcs >> 16) & 0xffff);
4276 		he_cap->he_mcs_nss_supp.rx_mcs_80p80 =
4277 			cpu_to_le16((band_cap->he_mcs >> 16) & 0xffff);
4278 		he_cap->he_mcs_nss_supp.tx_mcs_80p80 =
4279 			cpu_to_le16((band_cap->he_mcs >> 16) & 0xffff);
4280 
4281 		memset(he_cap->ppe_thres, 0, sizeof(he_cap->ppe_thres));
4282 		if (he_cap_elem->phy_cap_info[6] &
4283 		    IEEE80211_HE_PHY_CAP6_PPE_THRESHOLD_PRESENT)
4284 			ath12k_gen_ppe_thresh(&band_cap->he_ppet,
4285 					      he_cap->ppe_thres);
4286 
4287 		if (band == NL80211_BAND_6GHZ) {
4288 			data[idx].he_6ghz_capa.capa =
4289 				ath12k_mac_setup_he_6ghz_cap(cap, band_cap);
4290 		}
4291 		idx++;
4292 	}
4293 
4294 	return idx;
4295 }
4296 
4297 static void ath12k_mac_setup_he_cap(struct ath12k *ar,
4298 				    struct ath12k_pdev_cap *cap)
4299 {
4300 	struct ieee80211_supported_band *band;
4301 	int count;
4302 
4303 	if (cap->supported_bands & WMI_HOST_WLAN_2G_CAP) {
4304 		count = ath12k_mac_copy_he_cap(ar, cap,
4305 					       ar->mac.iftype[NL80211_BAND_2GHZ],
4306 					       NL80211_BAND_2GHZ);
4307 		band = &ar->mac.sbands[NL80211_BAND_2GHZ];
4308 		band->iftype_data = ar->mac.iftype[NL80211_BAND_2GHZ];
4309 		band->n_iftype_data = count;
4310 	}
4311 
4312 	if (cap->supported_bands & WMI_HOST_WLAN_5G_CAP) {
4313 		count = ath12k_mac_copy_he_cap(ar, cap,
4314 					       ar->mac.iftype[NL80211_BAND_5GHZ],
4315 					       NL80211_BAND_5GHZ);
4316 		band = &ar->mac.sbands[NL80211_BAND_5GHZ];
4317 		band->iftype_data = ar->mac.iftype[NL80211_BAND_5GHZ];
4318 		band->n_iftype_data = count;
4319 	}
4320 
4321 	if (cap->supported_bands & WMI_HOST_WLAN_5G_CAP &&
4322 	    ar->supports_6ghz) {
4323 		count = ath12k_mac_copy_he_cap(ar, cap,
4324 					       ar->mac.iftype[NL80211_BAND_6GHZ],
4325 					       NL80211_BAND_6GHZ);
4326 		band = &ar->mac.sbands[NL80211_BAND_6GHZ];
4327 		band->iftype_data = ar->mac.iftype[NL80211_BAND_6GHZ];
4328 		band->n_iftype_data = count;
4329 	}
4330 }
4331 
4332 static int __ath12k_set_antenna(struct ath12k *ar, u32 tx_ant, u32 rx_ant)
4333 {
4334 	int ret;
4335 
4336 	lockdep_assert_held(&ar->conf_mutex);
4337 
4338 	if (ath12k_check_chain_mask(ar, tx_ant, true))
4339 		return -EINVAL;
4340 
4341 	if (ath12k_check_chain_mask(ar, rx_ant, false))
4342 		return -EINVAL;
4343 
4344 	ar->cfg_tx_chainmask = tx_ant;
4345 	ar->cfg_rx_chainmask = rx_ant;
4346 
4347 	if (ar->state != ATH12K_STATE_ON &&
4348 	    ar->state != ATH12K_STATE_RESTARTED)
4349 		return 0;
4350 
4351 	ret = ath12k_wmi_pdev_set_param(ar, WMI_PDEV_PARAM_TX_CHAIN_MASK,
4352 					tx_ant, ar->pdev->pdev_id);
4353 	if (ret) {
4354 		ath12k_warn(ar->ab, "failed to set tx-chainmask: %d, req 0x%x\n",
4355 			    ret, tx_ant);
4356 		return ret;
4357 	}
4358 
4359 	ar->num_tx_chains = hweight32(tx_ant);
4360 
4361 	ret = ath12k_wmi_pdev_set_param(ar, WMI_PDEV_PARAM_RX_CHAIN_MASK,
4362 					rx_ant, ar->pdev->pdev_id);
4363 	if (ret) {
4364 		ath12k_warn(ar->ab, "failed to set rx-chainmask: %d, req 0x%x\n",
4365 			    ret, rx_ant);
4366 		return ret;
4367 	}
4368 
4369 	ar->num_rx_chains = hweight32(rx_ant);
4370 
4371 	/* Reload HT/VHT/HE capability */
4372 	ath12k_mac_setup_ht_vht_cap(ar, &ar->pdev->cap, NULL);
4373 	ath12k_mac_setup_he_cap(ar, &ar->pdev->cap);
4374 
4375 	return 0;
4376 }
4377 
4378 int ath12k_mac_tx_mgmt_pending_free(int buf_id, void *skb, void *ctx)
4379 {
4380 	struct sk_buff *msdu = skb;
4381 	struct ieee80211_tx_info *info;
4382 	struct ath12k *ar = ctx;
4383 	struct ath12k_base *ab = ar->ab;
4384 
4385 	spin_lock_bh(&ar->txmgmt_idr_lock);
4386 	idr_remove(&ar->txmgmt_idr, buf_id);
4387 	spin_unlock_bh(&ar->txmgmt_idr_lock);
4388 	dma_unmap_single(ab->dev, ATH12K_SKB_CB(msdu)->paddr, msdu->len,
4389 			 DMA_TO_DEVICE);
4390 
4391 	info = IEEE80211_SKB_CB(msdu);
4392 	memset(&info->status, 0, sizeof(info->status));
4393 
4394 	ieee80211_free_txskb(ar->hw, msdu);
4395 
4396 	return 0;
4397 }
4398 
4399 static int ath12k_mac_vif_txmgmt_idr_remove(int buf_id, void *skb, void *ctx)
4400 {
4401 	struct ieee80211_vif *vif = ctx;
4402 	struct ath12k_skb_cb *skb_cb = ATH12K_SKB_CB(skb);
4403 	struct sk_buff *msdu = skb;
4404 	struct ath12k *ar = skb_cb->ar;
4405 	struct ath12k_base *ab = ar->ab;
4406 
4407 	if (skb_cb->vif == vif) {
4408 		spin_lock_bh(&ar->txmgmt_idr_lock);
4409 		idr_remove(&ar->txmgmt_idr, buf_id);
4410 		spin_unlock_bh(&ar->txmgmt_idr_lock);
4411 		dma_unmap_single(ab->dev, skb_cb->paddr, msdu->len,
4412 				 DMA_TO_DEVICE);
4413 	}
4414 
4415 	return 0;
4416 }
4417 
4418 static int ath12k_mac_mgmt_tx_wmi(struct ath12k *ar, struct ath12k_vif *arvif,
4419 				  struct sk_buff *skb)
4420 {
4421 	struct ath12k_base *ab = ar->ab;
4422 	struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
4423 	struct ieee80211_tx_info *info;
4424 	dma_addr_t paddr;
4425 	int buf_id;
4426 	int ret;
4427 
4428 	spin_lock_bh(&ar->txmgmt_idr_lock);
4429 	buf_id = idr_alloc(&ar->txmgmt_idr, skb, 0,
4430 			   ATH12K_TX_MGMT_NUM_PENDING_MAX, GFP_ATOMIC);
4431 	spin_unlock_bh(&ar->txmgmt_idr_lock);
4432 	if (buf_id < 0)
4433 		return -ENOSPC;
4434 
4435 	info = IEEE80211_SKB_CB(skb);
4436 	if (!(info->flags & IEEE80211_TX_CTL_HW_80211_ENCAP)) {
4437 		if ((ieee80211_is_action(hdr->frame_control) ||
4438 		     ieee80211_is_deauth(hdr->frame_control) ||
4439 		     ieee80211_is_disassoc(hdr->frame_control)) &&
4440 		     ieee80211_has_protected(hdr->frame_control)) {
4441 			skb_put(skb, IEEE80211_CCMP_MIC_LEN);
4442 		}
4443 	}
4444 
4445 	paddr = dma_map_single(ab->dev, skb->data, skb->len, DMA_TO_DEVICE);
4446 	if (dma_mapping_error(ab->dev, paddr)) {
4447 		ath12k_warn(ab, "failed to DMA map mgmt Tx buffer\n");
4448 		ret = -EIO;
4449 		goto err_free_idr;
4450 	}
4451 
4452 	ATH12K_SKB_CB(skb)->paddr = paddr;
4453 
4454 	ret = ath12k_wmi_mgmt_send(ar, arvif->vdev_id, buf_id, skb);
4455 	if (ret) {
4456 		ath12k_warn(ar->ab, "failed to send mgmt frame: %d\n", ret);
4457 		goto err_unmap_buf;
4458 	}
4459 
4460 	return 0;
4461 
4462 err_unmap_buf:
4463 	dma_unmap_single(ab->dev, ATH12K_SKB_CB(skb)->paddr,
4464 			 skb->len, DMA_TO_DEVICE);
4465 err_free_idr:
4466 	spin_lock_bh(&ar->txmgmt_idr_lock);
4467 	idr_remove(&ar->txmgmt_idr, buf_id);
4468 	spin_unlock_bh(&ar->txmgmt_idr_lock);
4469 
4470 	return ret;
4471 }
4472 
4473 static void ath12k_mgmt_over_wmi_tx_purge(struct ath12k *ar)
4474 {
4475 	struct sk_buff *skb;
4476 
4477 	while ((skb = skb_dequeue(&ar->wmi_mgmt_tx_queue)) != NULL)
4478 		ieee80211_free_txskb(ar->hw, skb);
4479 }
4480 
4481 static void ath12k_mgmt_over_wmi_tx_work(struct work_struct *work)
4482 {
4483 	struct ath12k *ar = container_of(work, struct ath12k, wmi_mgmt_tx_work);
4484 	struct ath12k_skb_cb *skb_cb;
4485 	struct ath12k_vif *arvif;
4486 	struct sk_buff *skb;
4487 	int ret;
4488 
4489 	while ((skb = skb_dequeue(&ar->wmi_mgmt_tx_queue)) != NULL) {
4490 		skb_cb = ATH12K_SKB_CB(skb);
4491 		if (!skb_cb->vif) {
4492 			ath12k_warn(ar->ab, "no vif found for mgmt frame\n");
4493 			ieee80211_free_txskb(ar->hw, skb);
4494 			continue;
4495 		}
4496 
4497 		arvif = ath12k_vif_to_arvif(skb_cb->vif);
4498 		if (ar->allocated_vdev_map & (1LL << arvif->vdev_id) &&
4499 		    arvif->is_started) {
4500 			ret = ath12k_mac_mgmt_tx_wmi(ar, arvif, skb);
4501 			if (ret) {
4502 				ath12k_warn(ar->ab, "failed to tx mgmt frame, vdev_id %d :%d\n",
4503 					    arvif->vdev_id, ret);
4504 				ieee80211_free_txskb(ar->hw, skb);
4505 			} else {
4506 				atomic_inc(&ar->num_pending_mgmt_tx);
4507 			}
4508 		} else {
4509 			ath12k_warn(ar->ab,
4510 				    "dropping mgmt frame for vdev %d, is_started %d\n",
4511 				    arvif->vdev_id,
4512 				    arvif->is_started);
4513 			ieee80211_free_txskb(ar->hw, skb);
4514 		}
4515 	}
4516 }
4517 
4518 static int ath12k_mac_mgmt_tx(struct ath12k *ar, struct sk_buff *skb,
4519 			      bool is_prb_rsp)
4520 {
4521 	struct sk_buff_head *q = &ar->wmi_mgmt_tx_queue;
4522 
4523 	if (test_bit(ATH12K_FLAG_CRASH_FLUSH, &ar->ab->dev_flags))
4524 		return -ESHUTDOWN;
4525 
4526 	/* Drop probe response packets when the pending management tx
4527 	 * count has reached a certain threshold, so as to prioritize
4528 	 * other mgmt packets like auth and assoc to be sent on time
4529 	 * for establishing successful connections.
4530 	 */
4531 	if (is_prb_rsp &&
4532 	    atomic_read(&ar->num_pending_mgmt_tx) > ATH12K_PRB_RSP_DROP_THRESHOLD) {
4533 		ath12k_warn(ar->ab,
4534 			    "dropping probe response as pending queue is almost full\n");
4535 		return -ENOSPC;
4536 	}
4537 
4538 	if (skb_queue_len(q) == ATH12K_TX_MGMT_NUM_PENDING_MAX) {
4539 		ath12k_warn(ar->ab, "mgmt tx queue is full\n");
4540 		return -ENOSPC;
4541 	}
4542 
4543 	skb_queue_tail(q, skb);
4544 	ieee80211_queue_work(ar->hw, &ar->wmi_mgmt_tx_work);
4545 
4546 	return 0;
4547 }
4548 
4549 static void ath12k_mac_op_tx(struct ieee80211_hw *hw,
4550 			     struct ieee80211_tx_control *control,
4551 			     struct sk_buff *skb)
4552 {
4553 	struct ath12k_skb_cb *skb_cb = ATH12K_SKB_CB(skb);
4554 	struct ath12k *ar = hw->priv;
4555 	struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
4556 	struct ieee80211_vif *vif = info->control.vif;
4557 	struct ath12k_vif *arvif = ath12k_vif_to_arvif(vif);
4558 	struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
4559 	struct ieee80211_key_conf *key = info->control.hw_key;
4560 	u32 info_flags = info->flags;
4561 	bool is_prb_rsp;
4562 	int ret;
4563 
4564 	memset(skb_cb, 0, sizeof(*skb_cb));
4565 	skb_cb->vif = vif;
4566 
4567 	if (key) {
4568 		skb_cb->cipher = key->cipher;
4569 		skb_cb->flags |= ATH12K_SKB_CIPHER_SET;
4570 	}
4571 
4572 	if (info_flags & IEEE80211_TX_CTL_HW_80211_ENCAP) {
4573 		skb_cb->flags |= ATH12K_SKB_HW_80211_ENCAP;
4574 	} else if (ieee80211_is_mgmt(hdr->frame_control)) {
4575 		is_prb_rsp = ieee80211_is_probe_resp(hdr->frame_control);
4576 		ret = ath12k_mac_mgmt_tx(ar, skb, is_prb_rsp);
4577 		if (ret) {
4578 			ath12k_warn(ar->ab, "failed to queue management frame %d\n",
4579 				    ret);
4580 			ieee80211_free_txskb(ar->hw, skb);
4581 		}
4582 		return;
4583 	}
4584 
4585 	ret = ath12k_dp_tx(ar, arvif, skb);
4586 	if (ret) {
4587 		ath12k_warn(ar->ab, "failed to transmit frame %d\n", ret);
4588 		ieee80211_free_txskb(ar->hw, skb);
4589 	}
4590 }
4591 
4592 void ath12k_mac_drain_tx(struct ath12k *ar)
4593 {
4594 	/* make sure rcu-protected mac80211 tx path itself is drained */
4595 	synchronize_net();
4596 
4597 	cancel_work_sync(&ar->wmi_mgmt_tx_work);
4598 	ath12k_mgmt_over_wmi_tx_purge(ar);
4599 }
4600 
4601 static int ath12k_mac_config_mon_status_default(struct ath12k *ar, bool enable)
4602 {
4603 	return -ENOTSUPP;
4604 	/* TODO: Need to support new monitor mode */
4605 }
4606 
4607 static void ath12k_mac_wait_reconfigure(struct ath12k_base *ab)
4608 {
4609 	int recovery_start_count;
4610 
4611 	if (!ab->is_reset)
4612 		return;
4613 
4614 	recovery_start_count = atomic_inc_return(&ab->recovery_start_count);
4615 
4616 	ath12k_dbg(ab, ATH12K_DBG_MAC, "recovery start count %d\n", recovery_start_count);
4617 
4618 	if (recovery_start_count == ab->num_radios) {
4619 		complete(&ab->recovery_start);
4620 		ath12k_dbg(ab, ATH12K_DBG_MAC, "recovery started success\n");
4621 	}
4622 
4623 	ath12k_dbg(ab, ATH12K_DBG_MAC, "waiting reconfigure...\n");
4624 
4625 	wait_for_completion_timeout(&ab->reconfigure_complete,
4626 				    ATH12K_RECONFIGURE_TIMEOUT_HZ);
4627 }
4628 
4629 static int ath12k_mac_op_start(struct ieee80211_hw *hw)
4630 {
4631 	struct ath12k *ar = hw->priv;
4632 	struct ath12k_base *ab = ar->ab;
4633 	struct ath12k_pdev *pdev = ar->pdev;
4634 	int ret;
4635 
4636 	ath12k_mac_drain_tx(ar);
4637 	mutex_lock(&ar->conf_mutex);
4638 
4639 	switch (ar->state) {
4640 	case ATH12K_STATE_OFF:
4641 		ar->state = ATH12K_STATE_ON;
4642 		break;
4643 	case ATH12K_STATE_RESTARTING:
4644 		ar->state = ATH12K_STATE_RESTARTED;
4645 		ath12k_mac_wait_reconfigure(ab);
4646 		break;
4647 	case ATH12K_STATE_RESTARTED:
4648 	case ATH12K_STATE_WEDGED:
4649 	case ATH12K_STATE_ON:
4650 		WARN_ON(1);
4651 		ret = -EINVAL;
4652 		goto err;
4653 	}
4654 
4655 	ret = ath12k_wmi_pdev_set_param(ar, WMI_PDEV_PARAM_PMF_QOS,
4656 					1, pdev->pdev_id);
4657 
4658 	if (ret) {
4659 		ath12k_err(ar->ab, "failed to enable PMF QOS: (%d\n", ret);
4660 		goto err;
4661 	}
4662 
4663 	ret = ath12k_wmi_pdev_set_param(ar, WMI_PDEV_PARAM_DYNAMIC_BW, 1,
4664 					pdev->pdev_id);
4665 	if (ret) {
4666 		ath12k_err(ar->ab, "failed to enable dynamic bw: %d\n", ret);
4667 		goto err;
4668 	}
4669 
4670 	ret = ath12k_wmi_pdev_set_param(ar, WMI_PDEV_PARAM_ARP_AC_OVERRIDE,
4671 					0, pdev->pdev_id);
4672 	if (ret) {
4673 		ath12k_err(ab, "failed to set ac override for ARP: %d\n",
4674 			   ret);
4675 		goto err;
4676 	}
4677 
4678 	ret = ath12k_wmi_send_dfs_phyerr_offload_enable_cmd(ar, pdev->pdev_id);
4679 	if (ret) {
4680 		ath12k_err(ab, "failed to offload radar detection: %d\n",
4681 			   ret);
4682 		goto err;
4683 	}
4684 
4685 	ret = ath12k_dp_tx_htt_h2t_ppdu_stats_req(ar,
4686 						  HTT_PPDU_STATS_TAG_DEFAULT);
4687 	if (ret) {
4688 		ath12k_err(ab, "failed to req ppdu stats: %d\n", ret);
4689 		goto err;
4690 	}
4691 
4692 	ret = ath12k_wmi_pdev_set_param(ar, WMI_PDEV_PARAM_MESH_MCAST_ENABLE,
4693 					1, pdev->pdev_id);
4694 
4695 	if (ret) {
4696 		ath12k_err(ar->ab, "failed to enable MESH MCAST ENABLE: (%d\n", ret);
4697 		goto err;
4698 	}
4699 
4700 	__ath12k_set_antenna(ar, ar->cfg_tx_chainmask, ar->cfg_rx_chainmask);
4701 
4702 	/* TODO: Do we need to enable ANI? */
4703 
4704 	ath12k_reg_update_chan_list(ar);
4705 
4706 	ar->num_started_vdevs = 0;
4707 	ar->num_created_vdevs = 0;
4708 	ar->num_peers = 0;
4709 	ar->allocated_vdev_map = 0;
4710 
4711 	/* Configure monitor status ring with default rx_filter to get rx status
4712 	 * such as rssi, rx_duration.
4713 	 */
4714 	ret = ath12k_mac_config_mon_status_default(ar, true);
4715 	if (ret && (ret != -ENOTSUPP)) {
4716 		ath12k_err(ab, "failed to configure monitor status ring with default rx_filter: (%d)\n",
4717 			   ret);
4718 		goto err;
4719 	}
4720 
4721 	if (ret == -ENOTSUPP)
4722 		ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
4723 			   "monitor status config is not yet supported");
4724 
4725 	/* Configure the hash seed for hash based reo dest ring selection */
4726 	ath12k_wmi_pdev_lro_cfg(ar, ar->pdev->pdev_id);
4727 
4728 	/* allow device to enter IMPS */
4729 	if (ab->hw_params->idle_ps) {
4730 		ret = ath12k_wmi_pdev_set_param(ar, WMI_PDEV_PARAM_IDLE_PS_CONFIG,
4731 						1, pdev->pdev_id);
4732 		if (ret) {
4733 			ath12k_err(ab, "failed to enable idle ps: %d\n", ret);
4734 			goto err;
4735 		}
4736 	}
4737 
4738 	mutex_unlock(&ar->conf_mutex);
4739 
4740 	rcu_assign_pointer(ab->pdevs_active[ar->pdev_idx],
4741 			   &ab->pdevs[ar->pdev_idx]);
4742 
4743 	return 0;
4744 
4745 err:
4746 	ar->state = ATH12K_STATE_OFF;
4747 	mutex_unlock(&ar->conf_mutex);
4748 
4749 	return ret;
4750 }
4751 
4752 static void ath12k_mac_op_stop(struct ieee80211_hw *hw)
4753 {
4754 	struct ath12k *ar = hw->priv;
4755 	struct htt_ppdu_stats_info *ppdu_stats, *tmp;
4756 	int ret;
4757 
4758 	ath12k_mac_drain_tx(ar);
4759 
4760 	mutex_lock(&ar->conf_mutex);
4761 	ret = ath12k_mac_config_mon_status_default(ar, false);
4762 	if (ret && (ret != -ENOTSUPP))
4763 		ath12k_err(ar->ab, "failed to clear rx_filter for monitor status ring: (%d)\n",
4764 			   ret);
4765 
4766 	clear_bit(ATH12K_CAC_RUNNING, &ar->dev_flags);
4767 	ar->state = ATH12K_STATE_OFF;
4768 	mutex_unlock(&ar->conf_mutex);
4769 
4770 	cancel_delayed_work_sync(&ar->scan.timeout);
4771 	cancel_work_sync(&ar->regd_update_work);
4772 
4773 	spin_lock_bh(&ar->data_lock);
4774 	list_for_each_entry_safe(ppdu_stats, tmp, &ar->ppdu_stats_info, list) {
4775 		list_del(&ppdu_stats->list);
4776 		kfree(ppdu_stats);
4777 	}
4778 	spin_unlock_bh(&ar->data_lock);
4779 
4780 	rcu_assign_pointer(ar->ab->pdevs_active[ar->pdev_idx], NULL);
4781 
4782 	synchronize_rcu();
4783 
4784 	atomic_set(&ar->num_pending_mgmt_tx, 0);
4785 }
4786 
4787 static u8
4788 ath12k_mac_get_vdev_stats_id(struct ath12k_vif *arvif)
4789 {
4790 	struct ath12k_base *ab = arvif->ar->ab;
4791 	u8 vdev_stats_id = 0;
4792 
4793 	do {
4794 		if (ab->free_vdev_stats_id_map & (1LL << vdev_stats_id)) {
4795 			vdev_stats_id++;
4796 			if (vdev_stats_id <= ATH12K_INVAL_VDEV_STATS_ID) {
4797 				vdev_stats_id = ATH12K_INVAL_VDEV_STATS_ID;
4798 				break;
4799 			}
4800 		} else {
4801 			ab->free_vdev_stats_id_map |= (1LL << vdev_stats_id);
4802 			break;
4803 		}
4804 	} while (vdev_stats_id);
4805 
4806 	arvif->vdev_stats_id = vdev_stats_id;
4807 	return vdev_stats_id;
4808 }
4809 
4810 static void ath12k_mac_setup_vdev_create_arg(struct ath12k_vif *arvif,
4811 					     struct ath12k_wmi_vdev_create_arg *arg)
4812 {
4813 	struct ath12k *ar = arvif->ar;
4814 	struct ath12k_pdev *pdev = ar->pdev;
4815 
4816 	arg->if_id = arvif->vdev_id;
4817 	arg->type = arvif->vdev_type;
4818 	arg->subtype = arvif->vdev_subtype;
4819 	arg->pdev_id = pdev->pdev_id;
4820 
4821 	if (pdev->cap.supported_bands & WMI_HOST_WLAN_2G_CAP) {
4822 		arg->chains[NL80211_BAND_2GHZ].tx = ar->num_tx_chains;
4823 		arg->chains[NL80211_BAND_2GHZ].rx = ar->num_rx_chains;
4824 	}
4825 	if (pdev->cap.supported_bands & WMI_HOST_WLAN_5G_CAP) {
4826 		arg->chains[NL80211_BAND_5GHZ].tx = ar->num_tx_chains;
4827 		arg->chains[NL80211_BAND_5GHZ].rx = ar->num_rx_chains;
4828 	}
4829 	if (pdev->cap.supported_bands & WMI_HOST_WLAN_5G_CAP &&
4830 	    ar->supports_6ghz) {
4831 		arg->chains[NL80211_BAND_6GHZ].tx = ar->num_tx_chains;
4832 		arg->chains[NL80211_BAND_6GHZ].rx = ar->num_rx_chains;
4833 	}
4834 
4835 	arg->if_stats_id = ath12k_mac_get_vdev_stats_id(arvif);
4836 }
4837 
4838 static u32
4839 ath12k_mac_prepare_he_mode(struct ath12k_pdev *pdev, u32 viftype)
4840 {
4841 	struct ath12k_pdev_cap *pdev_cap = &pdev->cap;
4842 	struct ath12k_band_cap *cap_band = NULL;
4843 	u32 *hecap_phy_ptr = NULL;
4844 	u32 hemode;
4845 
4846 	if (pdev->cap.supported_bands & WMI_HOST_WLAN_2G_CAP)
4847 		cap_band = &pdev_cap->band[NL80211_BAND_2GHZ];
4848 	else
4849 		cap_band = &pdev_cap->band[NL80211_BAND_5GHZ];
4850 
4851 	hecap_phy_ptr = &cap_band->he_cap_phy_info[0];
4852 
4853 	hemode = u32_encode_bits(HE_SU_BFEE_ENABLE, HE_MODE_SU_TX_BFEE) |
4854 		 u32_encode_bits(HECAP_PHY_SUBFMR_GET(hecap_phy_ptr),
4855 				 HE_MODE_SU_TX_BFER) |
4856 		 u32_encode_bits(HECAP_PHY_ULMUMIMO_GET(hecap_phy_ptr),
4857 				 HE_MODE_UL_MUMIMO);
4858 
4859 	/* TODO: WDS and other modes */
4860 	if (viftype == NL80211_IFTYPE_AP) {
4861 		hemode |= u32_encode_bits(HECAP_PHY_MUBFMR_GET(hecap_phy_ptr),
4862 					  HE_MODE_MU_TX_BFER) |
4863 			  u32_encode_bits(HE_DL_MUOFDMA_ENABLE, HE_MODE_DL_OFDMA) |
4864 			  u32_encode_bits(HE_UL_MUOFDMA_ENABLE, HE_MODE_UL_OFDMA);
4865 	} else {
4866 		hemode |= u32_encode_bits(HE_MU_BFEE_ENABLE, HE_MODE_MU_TX_BFEE);
4867 	}
4868 
4869 	return hemode;
4870 }
4871 
4872 static int ath12k_set_he_mu_sounding_mode(struct ath12k *ar,
4873 					  struct ath12k_vif *arvif)
4874 {
4875 	u32 param_id, param_value;
4876 	struct ath12k_base *ab = ar->ab;
4877 	int ret;
4878 
4879 	param_id = WMI_VDEV_PARAM_SET_HEMU_MODE;
4880 	param_value = ath12k_mac_prepare_he_mode(ar->pdev, arvif->vif->type);
4881 	ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
4882 					    param_id, param_value);
4883 	if (ret) {
4884 		ath12k_warn(ab, "failed to set vdev %d HE MU mode: %d param_value %x\n",
4885 			    arvif->vdev_id, ret, param_value);
4886 		return ret;
4887 	}
4888 	param_id = WMI_VDEV_PARAM_SET_HE_SOUNDING_MODE;
4889 	param_value =
4890 		u32_encode_bits(HE_VHT_SOUNDING_MODE_ENABLE, HE_VHT_SOUNDING_MODE) |
4891 		u32_encode_bits(HE_TRIG_NONTRIG_SOUNDING_MODE_ENABLE,
4892 				HE_TRIG_NONTRIG_SOUNDING_MODE);
4893 	ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
4894 					    param_id, param_value);
4895 	if (ret) {
4896 		ath12k_warn(ab, "failed to set vdev %d HE MU mode: %d\n",
4897 			    arvif->vdev_id, ret);
4898 		return ret;
4899 	}
4900 	return ret;
4901 }
4902 
4903 static void ath12k_mac_op_update_vif_offload(struct ieee80211_hw *hw,
4904 					     struct ieee80211_vif *vif)
4905 {
4906 	struct ath12k *ar = hw->priv;
4907 	struct ath12k_base *ab = ar->ab;
4908 	struct ath12k_vif *arvif = ath12k_vif_to_arvif(vif);
4909 	u32 param_id, param_value;
4910 	int ret;
4911 
4912 	param_id = WMI_VDEV_PARAM_TX_ENCAP_TYPE;
4913 	if (vif->type != NL80211_IFTYPE_STATION &&
4914 	    vif->type != NL80211_IFTYPE_AP)
4915 		vif->offload_flags &= ~(IEEE80211_OFFLOAD_ENCAP_ENABLED |
4916 					IEEE80211_OFFLOAD_DECAP_ENABLED);
4917 
4918 	if (vif->offload_flags & IEEE80211_OFFLOAD_ENCAP_ENABLED)
4919 		arvif->tx_encap_type = ATH12K_HW_TXRX_ETHERNET;
4920 	else if (test_bit(ATH12K_FLAG_RAW_MODE, &ab->dev_flags))
4921 		arvif->tx_encap_type = ATH12K_HW_TXRX_RAW;
4922 	else
4923 		arvif->tx_encap_type = ATH12K_HW_TXRX_NATIVE_WIFI;
4924 
4925 	ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
4926 					    param_id, arvif->tx_encap_type);
4927 	if (ret) {
4928 		ath12k_warn(ab, "failed to set vdev %d tx encap mode: %d\n",
4929 			    arvif->vdev_id, ret);
4930 		vif->offload_flags &= ~IEEE80211_OFFLOAD_ENCAP_ENABLED;
4931 	}
4932 
4933 	param_id = WMI_VDEV_PARAM_RX_DECAP_TYPE;
4934 	if (vif->offload_flags & IEEE80211_OFFLOAD_DECAP_ENABLED)
4935 		param_value = ATH12K_HW_TXRX_ETHERNET;
4936 	else if (test_bit(ATH12K_FLAG_RAW_MODE, &ab->dev_flags))
4937 		param_value = ATH12K_HW_TXRX_RAW;
4938 	else
4939 		param_value = ATH12K_HW_TXRX_NATIVE_WIFI;
4940 
4941 	ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
4942 					    param_id, param_value);
4943 	if (ret) {
4944 		ath12k_warn(ab, "failed to set vdev %d rx decap mode: %d\n",
4945 			    arvif->vdev_id, ret);
4946 		vif->offload_flags &= ~IEEE80211_OFFLOAD_DECAP_ENABLED;
4947 	}
4948 }
4949 
4950 static int ath12k_mac_op_add_interface(struct ieee80211_hw *hw,
4951 				       struct ieee80211_vif *vif)
4952 {
4953 	struct ath12k *ar = hw->priv;
4954 	struct ath12k_base *ab = ar->ab;
4955 	struct ath12k_vif *arvif = ath12k_vif_to_arvif(vif);
4956 	struct ath12k_wmi_vdev_create_arg vdev_arg = {0};
4957 	struct ath12k_wmi_peer_create_arg peer_param;
4958 	u32 param_id, param_value;
4959 	u16 nss;
4960 	int i;
4961 	int ret;
4962 	int bit;
4963 
4964 	vif->driver_flags |= IEEE80211_VIF_SUPPORTS_UAPSD;
4965 
4966 	mutex_lock(&ar->conf_mutex);
4967 
4968 	if (vif->type == NL80211_IFTYPE_AP &&
4969 	    ar->num_peers > (ar->max_num_peers - 1)) {
4970 		ath12k_warn(ab, "failed to create vdev due to insufficient peer entry resource in firmware\n");
4971 		ret = -ENOBUFS;
4972 		goto err;
4973 	}
4974 
4975 	if (ar->num_created_vdevs > (TARGET_NUM_VDEVS - 1)) {
4976 		ath12k_warn(ab, "failed to create vdev, reached max vdev limit %d\n",
4977 			    TARGET_NUM_VDEVS);
4978 		ret = -EBUSY;
4979 		goto err;
4980 	}
4981 
4982 	memset(arvif, 0, sizeof(*arvif));
4983 
4984 	arvif->ar = ar;
4985 	arvif->vif = vif;
4986 
4987 	INIT_LIST_HEAD(&arvif->list);
4988 
4989 	/* Should we initialize any worker to handle connection loss indication
4990 	 * from firmware in sta mode?
4991 	 */
4992 
4993 	for (i = 0; i < ARRAY_SIZE(arvif->bitrate_mask.control); i++) {
4994 		arvif->bitrate_mask.control[i].legacy = 0xffffffff;
4995 		memset(arvif->bitrate_mask.control[i].ht_mcs, 0xff,
4996 		       sizeof(arvif->bitrate_mask.control[i].ht_mcs));
4997 		memset(arvif->bitrate_mask.control[i].vht_mcs, 0xff,
4998 		       sizeof(arvif->bitrate_mask.control[i].vht_mcs));
4999 	}
5000 
5001 	bit = __ffs64(ab->free_vdev_map);
5002 
5003 	arvif->vdev_id = bit;
5004 	arvif->vdev_subtype = WMI_VDEV_SUBTYPE_NONE;
5005 
5006 	switch (vif->type) {
5007 	case NL80211_IFTYPE_UNSPECIFIED:
5008 	case NL80211_IFTYPE_STATION:
5009 		arvif->vdev_type = WMI_VDEV_TYPE_STA;
5010 		break;
5011 	case NL80211_IFTYPE_MESH_POINT:
5012 		arvif->vdev_subtype = WMI_VDEV_SUBTYPE_MESH_11S;
5013 		fallthrough;
5014 	case NL80211_IFTYPE_AP:
5015 		arvif->vdev_type = WMI_VDEV_TYPE_AP;
5016 		break;
5017 	case NL80211_IFTYPE_MONITOR:
5018 		arvif->vdev_type = WMI_VDEV_TYPE_MONITOR;
5019 		ar->monitor_vdev_id = bit;
5020 		break;
5021 	default:
5022 		WARN_ON(1);
5023 		break;
5024 	}
5025 
5026 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac add interface id %d type %d subtype %d map %llx\n",
5027 		   arvif->vdev_id, arvif->vdev_type, arvif->vdev_subtype,
5028 		   ab->free_vdev_map);
5029 
5030 	vif->cab_queue = arvif->vdev_id % (ATH12K_HW_MAX_QUEUES - 1);
5031 	for (i = 0; i < ARRAY_SIZE(vif->hw_queue); i++)
5032 		vif->hw_queue[i] = i % (ATH12K_HW_MAX_QUEUES - 1);
5033 
5034 	ath12k_mac_setup_vdev_create_arg(arvif, &vdev_arg);
5035 
5036 	ret = ath12k_wmi_vdev_create(ar, vif->addr, &vdev_arg);
5037 	if (ret) {
5038 		ath12k_warn(ab, "failed to create WMI vdev %d: %d\n",
5039 			    arvif->vdev_id, ret);
5040 		goto err;
5041 	}
5042 
5043 	ar->num_created_vdevs++;
5044 	ath12k_dbg(ab, ATH12K_DBG_MAC, "vdev %pM created, vdev_id %d\n",
5045 		   vif->addr, arvif->vdev_id);
5046 	ar->allocated_vdev_map |= 1LL << arvif->vdev_id;
5047 	ab->free_vdev_map &= ~(1LL << arvif->vdev_id);
5048 
5049 	spin_lock_bh(&ar->data_lock);
5050 	list_add(&arvif->list, &ar->arvifs);
5051 	spin_unlock_bh(&ar->data_lock);
5052 
5053 	ath12k_mac_op_update_vif_offload(hw, vif);
5054 
5055 	nss = hweight32(ar->cfg_tx_chainmask) ? : 1;
5056 	ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
5057 					    WMI_VDEV_PARAM_NSS, nss);
5058 	if (ret) {
5059 		ath12k_warn(ab, "failed to set vdev %d chainmask 0x%x, nss %d :%d\n",
5060 			    arvif->vdev_id, ar->cfg_tx_chainmask, nss, ret);
5061 		goto err_vdev_del;
5062 	}
5063 
5064 	switch (arvif->vdev_type) {
5065 	case WMI_VDEV_TYPE_AP:
5066 		peer_param.vdev_id = arvif->vdev_id;
5067 		peer_param.peer_addr = vif->addr;
5068 		peer_param.peer_type = WMI_PEER_TYPE_DEFAULT;
5069 		ret = ath12k_peer_create(ar, arvif, NULL, &peer_param);
5070 		if (ret) {
5071 			ath12k_warn(ab, "failed to vdev %d create peer for AP: %d\n",
5072 				    arvif->vdev_id, ret);
5073 			goto err_vdev_del;
5074 		}
5075 
5076 		ret = ath12k_mac_set_kickout(arvif);
5077 		if (ret) {
5078 			ath12k_warn(ar->ab, "failed to set vdev %i kickout parameters: %d\n",
5079 				    arvif->vdev_id, ret);
5080 			goto err_peer_del;
5081 		}
5082 		break;
5083 	case WMI_VDEV_TYPE_STA:
5084 		param_id = WMI_STA_PS_PARAM_RX_WAKE_POLICY;
5085 		param_value = WMI_STA_PS_RX_WAKE_POLICY_WAKE;
5086 		ret = ath12k_wmi_set_sta_ps_param(ar, arvif->vdev_id,
5087 						  param_id, param_value);
5088 		if (ret) {
5089 			ath12k_warn(ar->ab, "failed to set vdev %d RX wake policy: %d\n",
5090 				    arvif->vdev_id, ret);
5091 			goto err_peer_del;
5092 		}
5093 
5094 		param_id = WMI_STA_PS_PARAM_TX_WAKE_THRESHOLD;
5095 		param_value = WMI_STA_PS_TX_WAKE_THRESHOLD_ALWAYS;
5096 		ret = ath12k_wmi_set_sta_ps_param(ar, arvif->vdev_id,
5097 						  param_id, param_value);
5098 		if (ret) {
5099 			ath12k_warn(ar->ab, "failed to set vdev %d TX wake threshold: %d\n",
5100 				    arvif->vdev_id, ret);
5101 			goto err_peer_del;
5102 		}
5103 
5104 		param_id = WMI_STA_PS_PARAM_PSPOLL_COUNT;
5105 		param_value = WMI_STA_PS_PSPOLL_COUNT_NO_MAX;
5106 		ret = ath12k_wmi_set_sta_ps_param(ar, arvif->vdev_id,
5107 						  param_id, param_value);
5108 		if (ret) {
5109 			ath12k_warn(ar->ab, "failed to set vdev %d pspoll count: %d\n",
5110 				    arvif->vdev_id, ret);
5111 			goto err_peer_del;
5112 		}
5113 
5114 		ret = ath12k_wmi_pdev_set_ps_mode(ar, arvif->vdev_id, false);
5115 		if (ret) {
5116 			ath12k_warn(ar->ab, "failed to disable vdev %d ps mode: %d\n",
5117 				    arvif->vdev_id, ret);
5118 			goto err_peer_del;
5119 		}
5120 		break;
5121 	default:
5122 		break;
5123 	}
5124 
5125 	arvif->txpower = vif->bss_conf.txpower;
5126 	ret = ath12k_mac_txpower_recalc(ar);
5127 	if (ret)
5128 		goto err_peer_del;
5129 
5130 	param_id = WMI_VDEV_PARAM_RTS_THRESHOLD;
5131 	param_value = ar->hw->wiphy->rts_threshold;
5132 	ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
5133 					    param_id, param_value);
5134 	if (ret) {
5135 		ath12k_warn(ar->ab, "failed to set rts threshold for vdev %d: %d\n",
5136 			    arvif->vdev_id, ret);
5137 	}
5138 
5139 	ath12k_dp_vdev_tx_attach(ar, arvif);
5140 
5141 	if (vif->type != NL80211_IFTYPE_MONITOR && ar->monitor_conf_enabled)
5142 		ath12k_mac_monitor_vdev_create(ar);
5143 
5144 	mutex_unlock(&ar->conf_mutex);
5145 
5146 	return ret;
5147 
5148 err_peer_del:
5149 	if (arvif->vdev_type == WMI_VDEV_TYPE_AP) {
5150 		reinit_completion(&ar->peer_delete_done);
5151 
5152 		ret = ath12k_wmi_send_peer_delete_cmd(ar, vif->addr,
5153 						      arvif->vdev_id);
5154 		if (ret) {
5155 			ath12k_warn(ar->ab, "failed to delete peer vdev_id %d addr %pM\n",
5156 				    arvif->vdev_id, vif->addr);
5157 			goto err;
5158 		}
5159 
5160 		ret = ath12k_wait_for_peer_delete_done(ar, arvif->vdev_id,
5161 						       vif->addr);
5162 		if (ret)
5163 			goto err;
5164 
5165 		ar->num_peers--;
5166 	}
5167 
5168 err_vdev_del:
5169 	ath12k_wmi_vdev_delete(ar, arvif->vdev_id);
5170 	ar->num_created_vdevs--;
5171 	ar->allocated_vdev_map &= ~(1LL << arvif->vdev_id);
5172 	ab->free_vdev_map |= 1LL << arvif->vdev_id;
5173 	ab->free_vdev_stats_id_map &= ~(1LL << arvif->vdev_stats_id);
5174 	spin_lock_bh(&ar->data_lock);
5175 	list_del(&arvif->list);
5176 	spin_unlock_bh(&ar->data_lock);
5177 
5178 err:
5179 	mutex_unlock(&ar->conf_mutex);
5180 
5181 	return ret;
5182 }
5183 
5184 static void ath12k_mac_vif_unref(struct ath12k_dp *dp, struct ieee80211_vif *vif)
5185 {
5186 	struct ath12k_tx_desc_info *tx_desc_info, *tmp1;
5187 	struct ath12k_skb_cb *skb_cb;
5188 	struct sk_buff *skb;
5189 	int i;
5190 
5191 	for (i = 0; i < ATH12K_HW_MAX_QUEUES; i++) {
5192 		spin_lock_bh(&dp->tx_desc_lock[i]);
5193 
5194 		list_for_each_entry_safe(tx_desc_info, tmp1, &dp->tx_desc_used_list[i],
5195 					 list) {
5196 			skb = tx_desc_info->skb;
5197 			if (!skb)
5198 				continue;
5199 
5200 			skb_cb = ATH12K_SKB_CB(skb);
5201 			if (skb_cb->vif == vif)
5202 				skb_cb->vif = NULL;
5203 		}
5204 
5205 		spin_unlock_bh(&dp->tx_desc_lock[i]);
5206 	}
5207 }
5208 
5209 static void ath12k_mac_op_remove_interface(struct ieee80211_hw *hw,
5210 					   struct ieee80211_vif *vif)
5211 {
5212 	struct ath12k *ar = hw->priv;
5213 	struct ath12k_vif *arvif = ath12k_vif_to_arvif(vif);
5214 	struct ath12k_base *ab = ar->ab;
5215 	unsigned long time_left;
5216 	int ret;
5217 
5218 	mutex_lock(&ar->conf_mutex);
5219 
5220 	ath12k_dbg(ab, ATH12K_DBG_MAC, "mac remove interface (vdev %d)\n",
5221 		   arvif->vdev_id);
5222 
5223 	if (arvif->vdev_type == WMI_VDEV_TYPE_AP) {
5224 		ret = ath12k_peer_delete(ar, arvif->vdev_id, vif->addr);
5225 		if (ret)
5226 			ath12k_warn(ab, "failed to submit AP self-peer removal on vdev %d: %d\n",
5227 				    arvif->vdev_id, ret);
5228 	}
5229 
5230 	reinit_completion(&ar->vdev_delete_done);
5231 
5232 	ret = ath12k_wmi_vdev_delete(ar, arvif->vdev_id);
5233 	if (ret) {
5234 		ath12k_warn(ab, "failed to delete WMI vdev %d: %d\n",
5235 			    arvif->vdev_id, ret);
5236 		goto err_vdev_del;
5237 	}
5238 
5239 	time_left = wait_for_completion_timeout(&ar->vdev_delete_done,
5240 						ATH12K_VDEV_DELETE_TIMEOUT_HZ);
5241 	if (time_left == 0) {
5242 		ath12k_warn(ab, "Timeout in receiving vdev delete response\n");
5243 		goto err_vdev_del;
5244 	}
5245 
5246 	if (arvif->vdev_type == WMI_VDEV_TYPE_MONITOR) {
5247 		ar->monitor_vdev_id = -1;
5248 		ar->monitor_vdev_created = false;
5249 	} else if (ar->monitor_vdev_created && !ar->monitor_started) {
5250 		ret = ath12k_mac_monitor_vdev_delete(ar);
5251 	}
5252 
5253 	ab->free_vdev_map |= 1LL << (arvif->vdev_id);
5254 	ar->allocated_vdev_map &= ~(1LL << arvif->vdev_id);
5255 	ab->free_vdev_stats_id_map &= ~(1LL << arvif->vdev_stats_id);
5256 	ar->num_created_vdevs--;
5257 
5258 	ath12k_dbg(ab, ATH12K_DBG_MAC, "vdev %pM deleted, vdev_id %d\n",
5259 		   vif->addr, arvif->vdev_id);
5260 
5261 err_vdev_del:
5262 	spin_lock_bh(&ar->data_lock);
5263 	list_del(&arvif->list);
5264 	spin_unlock_bh(&ar->data_lock);
5265 
5266 	ath12k_peer_cleanup(ar, arvif->vdev_id);
5267 
5268 	idr_for_each(&ar->txmgmt_idr,
5269 		     ath12k_mac_vif_txmgmt_idr_remove, vif);
5270 
5271 	ath12k_mac_vif_unref(&ab->dp, vif);
5272 	ath12k_dp_tx_put_bank_profile(&ab->dp, arvif->bank_id);
5273 
5274 	/* Recalc txpower for remaining vdev */
5275 	ath12k_mac_txpower_recalc(ar);
5276 	clear_bit(ATH12K_FLAG_MONITOR_ENABLED, &ar->monitor_flags);
5277 
5278 	/* TODO: recal traffic pause state based on the available vdevs */
5279 
5280 	mutex_unlock(&ar->conf_mutex);
5281 }
5282 
5283 /* FIXME: Has to be verified. */
5284 #define SUPPORTED_FILTERS			\
5285 	(FIF_ALLMULTI |				\
5286 	FIF_CONTROL |				\
5287 	FIF_PSPOLL |				\
5288 	FIF_OTHER_BSS |				\
5289 	FIF_BCN_PRBRESP_PROMISC |		\
5290 	FIF_PROBE_REQ |				\
5291 	FIF_FCSFAIL)
5292 
5293 static void ath12k_mac_op_configure_filter(struct ieee80211_hw *hw,
5294 					   unsigned int changed_flags,
5295 					   unsigned int *total_flags,
5296 					   u64 multicast)
5297 {
5298 	struct ath12k *ar = hw->priv;
5299 	bool reset_flag;
5300 	int ret;
5301 
5302 	mutex_lock(&ar->conf_mutex);
5303 
5304 	changed_flags &= SUPPORTED_FILTERS;
5305 	*total_flags &= SUPPORTED_FILTERS;
5306 	ar->filter_flags = *total_flags;
5307 
5308 	/* For monitor mode */
5309 	reset_flag = !(ar->filter_flags & FIF_BCN_PRBRESP_PROMISC);
5310 
5311 	ret = ath12k_dp_tx_htt_monitor_mode_ring_config(ar, reset_flag);
5312 	if (!ret) {
5313 		if (!reset_flag)
5314 			set_bit(ATH12K_FLAG_MONITOR_ENABLED, &ar->monitor_flags);
5315 		else
5316 			clear_bit(ATH12K_FLAG_MONITOR_ENABLED, &ar->monitor_flags);
5317 	} else {
5318 		ath12k_warn(ar->ab,
5319 			    "fail to set monitor filter: %d\n", ret);
5320 	}
5321 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
5322 		   "changed_flags:0x%x, total_flags:0x%x, reset_flag:%d\n",
5323 		   changed_flags, *total_flags, reset_flag);
5324 
5325 	mutex_unlock(&ar->conf_mutex);
5326 }
5327 
5328 static int ath12k_mac_op_get_antenna(struct ieee80211_hw *hw, u32 *tx_ant, u32 *rx_ant)
5329 {
5330 	struct ath12k *ar = hw->priv;
5331 
5332 	mutex_lock(&ar->conf_mutex);
5333 
5334 	*tx_ant = ar->cfg_tx_chainmask;
5335 	*rx_ant = ar->cfg_rx_chainmask;
5336 
5337 	mutex_unlock(&ar->conf_mutex);
5338 
5339 	return 0;
5340 }
5341 
5342 static int ath12k_mac_op_set_antenna(struct ieee80211_hw *hw, u32 tx_ant, u32 rx_ant)
5343 {
5344 	struct ath12k *ar = hw->priv;
5345 	int ret;
5346 
5347 	mutex_lock(&ar->conf_mutex);
5348 	ret = __ath12k_set_antenna(ar, tx_ant, rx_ant);
5349 	mutex_unlock(&ar->conf_mutex);
5350 
5351 	return ret;
5352 }
5353 
5354 static int ath12k_mac_op_ampdu_action(struct ieee80211_hw *hw,
5355 				      struct ieee80211_vif *vif,
5356 				      struct ieee80211_ampdu_params *params)
5357 {
5358 	struct ath12k *ar = hw->priv;
5359 	int ret = -EINVAL;
5360 
5361 	mutex_lock(&ar->conf_mutex);
5362 
5363 	switch (params->action) {
5364 	case IEEE80211_AMPDU_RX_START:
5365 		ret = ath12k_dp_rx_ampdu_start(ar, params);
5366 		break;
5367 	case IEEE80211_AMPDU_RX_STOP:
5368 		ret = ath12k_dp_rx_ampdu_stop(ar, params);
5369 		break;
5370 	case IEEE80211_AMPDU_TX_START:
5371 	case IEEE80211_AMPDU_TX_STOP_CONT:
5372 	case IEEE80211_AMPDU_TX_STOP_FLUSH:
5373 	case IEEE80211_AMPDU_TX_STOP_FLUSH_CONT:
5374 	case IEEE80211_AMPDU_TX_OPERATIONAL:
5375 		/* Tx A-MPDU aggregation offloaded to hw/fw so deny mac80211
5376 		 * Tx aggregation requests.
5377 		 */
5378 		ret = -EOPNOTSUPP;
5379 		break;
5380 	}
5381 
5382 	mutex_unlock(&ar->conf_mutex);
5383 
5384 	return ret;
5385 }
5386 
5387 static int ath12k_mac_op_add_chanctx(struct ieee80211_hw *hw,
5388 				     struct ieee80211_chanctx_conf *ctx)
5389 {
5390 	struct ath12k *ar = hw->priv;
5391 	struct ath12k_base *ab = ar->ab;
5392 
5393 	ath12k_dbg(ab, ATH12K_DBG_MAC,
5394 		   "mac chanctx add freq %u width %d ptr %pK\n",
5395 		   ctx->def.chan->center_freq, ctx->def.width, ctx);
5396 
5397 	mutex_lock(&ar->conf_mutex);
5398 
5399 	spin_lock_bh(&ar->data_lock);
5400 	/* TODO: In case of multiple channel context, populate rx_channel from
5401 	 * Rx PPDU desc information.
5402 	 */
5403 	ar->rx_channel = ctx->def.chan;
5404 	spin_unlock_bh(&ar->data_lock);
5405 
5406 	mutex_unlock(&ar->conf_mutex);
5407 
5408 	return 0;
5409 }
5410 
5411 static void ath12k_mac_op_remove_chanctx(struct ieee80211_hw *hw,
5412 					 struct ieee80211_chanctx_conf *ctx)
5413 {
5414 	struct ath12k *ar = hw->priv;
5415 	struct ath12k_base *ab = ar->ab;
5416 
5417 	ath12k_dbg(ab, ATH12K_DBG_MAC,
5418 		   "mac chanctx remove freq %u width %d ptr %pK\n",
5419 		   ctx->def.chan->center_freq, ctx->def.width, ctx);
5420 
5421 	mutex_lock(&ar->conf_mutex);
5422 
5423 	spin_lock_bh(&ar->data_lock);
5424 	/* TODO: In case of there is one more channel context left, populate
5425 	 * rx_channel with the channel of that remaining channel context.
5426 	 */
5427 	ar->rx_channel = NULL;
5428 	spin_unlock_bh(&ar->data_lock);
5429 
5430 	mutex_unlock(&ar->conf_mutex);
5431 }
5432 
5433 static int
5434 ath12k_mac_vdev_start_restart(struct ath12k_vif *arvif,
5435 			      const struct cfg80211_chan_def *chandef,
5436 			      bool restart)
5437 {
5438 	struct ath12k *ar = arvif->ar;
5439 	struct ath12k_base *ab = ar->ab;
5440 	struct wmi_vdev_start_req_arg arg = {};
5441 	int he_support = arvif->vif->bss_conf.he_support;
5442 	int ret;
5443 
5444 	lockdep_assert_held(&ar->conf_mutex);
5445 
5446 	reinit_completion(&ar->vdev_setup_done);
5447 
5448 	arg.vdev_id = arvif->vdev_id;
5449 	arg.dtim_period = arvif->dtim_period;
5450 	arg.bcn_intval = arvif->beacon_interval;
5451 
5452 	arg.freq = chandef->chan->center_freq;
5453 	arg.band_center_freq1 = chandef->center_freq1;
5454 	arg.band_center_freq2 = chandef->center_freq2;
5455 	arg.mode = ath12k_phymodes[chandef->chan->band][chandef->width];
5456 
5457 	arg.min_power = 0;
5458 	arg.max_power = chandef->chan->max_power * 2;
5459 	arg.max_reg_power = chandef->chan->max_reg_power * 2;
5460 	arg.max_antenna_gain = chandef->chan->max_antenna_gain * 2;
5461 
5462 	arg.pref_tx_streams = ar->num_tx_chains;
5463 	arg.pref_rx_streams = ar->num_rx_chains;
5464 
5465 	if (arvif->vdev_type == WMI_VDEV_TYPE_AP) {
5466 		arg.ssid = arvif->u.ap.ssid;
5467 		arg.ssid_len = arvif->u.ap.ssid_len;
5468 		arg.hidden_ssid = arvif->u.ap.hidden_ssid;
5469 
5470 		/* For now allow DFS for AP mode */
5471 		arg.chan_radar = !!(chandef->chan->flags & IEEE80211_CHAN_RADAR);
5472 
5473 		arg.passive = arg.chan_radar;
5474 
5475 		spin_lock_bh(&ab->base_lock);
5476 		arg.regdomain = ar->ab->dfs_region;
5477 		spin_unlock_bh(&ab->base_lock);
5478 
5479 		/* TODO: Notify if secondary 80Mhz also needs radar detection */
5480 		if (he_support) {
5481 			ret = ath12k_set_he_mu_sounding_mode(ar, arvif);
5482 			if (ret) {
5483 				ath12k_warn(ar->ab, "failed to set he mode vdev %i\n",
5484 					    arg.vdev_id);
5485 				return ret;
5486 			}
5487 		}
5488 	}
5489 
5490 	arg.passive |= !!(chandef->chan->flags & IEEE80211_CHAN_NO_IR);
5491 
5492 	ath12k_dbg(ab, ATH12K_DBG_MAC,
5493 		   "mac vdev %d start center_freq %d phymode %s\n",
5494 		   arg.vdev_id, arg.freq,
5495 		   ath12k_mac_phymode_str(arg.mode));
5496 
5497 	ret = ath12k_wmi_vdev_start(ar, &arg, restart);
5498 	if (ret) {
5499 		ath12k_warn(ar->ab, "failed to %s WMI vdev %i\n",
5500 			    restart ? "restart" : "start", arg.vdev_id);
5501 		return ret;
5502 	}
5503 
5504 	ret = ath12k_mac_vdev_setup_sync(ar);
5505 	if (ret) {
5506 		ath12k_warn(ab, "failed to synchronize setup for vdev %i %s: %d\n",
5507 			    arg.vdev_id, restart ? "restart" : "start", ret);
5508 		return ret;
5509 	}
5510 
5511 	ar->num_started_vdevs++;
5512 	ath12k_dbg(ab, ATH12K_DBG_MAC,  "vdev %pM started, vdev_id %d\n",
5513 		   arvif->vif->addr, arvif->vdev_id);
5514 
5515 	/* Enable CAC Flag in the driver by checking the channel DFS cac time,
5516 	 * i.e dfs_cac_ms value which will be valid only for radar channels
5517 	 * and state as NL80211_DFS_USABLE which indicates CAC needs to be
5518 	 * done before channel usage. This flags is used to drop rx packets.
5519 	 * during CAC.
5520 	 */
5521 	/* TODO: Set the flag for other interface types as required */
5522 	if (arvif->vdev_type == WMI_VDEV_TYPE_AP &&
5523 	    chandef->chan->dfs_cac_ms &&
5524 	    chandef->chan->dfs_state == NL80211_DFS_USABLE) {
5525 		set_bit(ATH12K_CAC_RUNNING, &ar->dev_flags);
5526 		ath12k_dbg(ab, ATH12K_DBG_MAC,
5527 			   "CAC Started in chan_freq %d for vdev %d\n",
5528 			   arg.freq, arg.vdev_id);
5529 	}
5530 
5531 	ret = ath12k_mac_set_txbf_conf(arvif);
5532 	if (ret)
5533 		ath12k_warn(ab, "failed to set txbf conf for vdev %d: %d\n",
5534 			    arvif->vdev_id, ret);
5535 
5536 	return 0;
5537 }
5538 
5539 static int ath12k_mac_vdev_stop(struct ath12k_vif *arvif)
5540 {
5541 	struct ath12k *ar = arvif->ar;
5542 	int ret;
5543 
5544 	lockdep_assert_held(&ar->conf_mutex);
5545 
5546 	reinit_completion(&ar->vdev_setup_done);
5547 
5548 	ret = ath12k_wmi_vdev_stop(ar, arvif->vdev_id);
5549 	if (ret) {
5550 		ath12k_warn(ar->ab, "failed to stop WMI vdev %i: %d\n",
5551 			    arvif->vdev_id, ret);
5552 		goto err;
5553 	}
5554 
5555 	ret = ath12k_mac_vdev_setup_sync(ar);
5556 	if (ret) {
5557 		ath12k_warn(ar->ab, "failed to synchronize setup for vdev %i: %d\n",
5558 			    arvif->vdev_id, ret);
5559 		goto err;
5560 	}
5561 
5562 	WARN_ON(ar->num_started_vdevs == 0);
5563 
5564 	ar->num_started_vdevs--;
5565 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "vdev %pM stopped, vdev_id %d\n",
5566 		   arvif->vif->addr, arvif->vdev_id);
5567 
5568 	if (test_bit(ATH12K_CAC_RUNNING, &ar->dev_flags)) {
5569 		clear_bit(ATH12K_CAC_RUNNING, &ar->dev_flags);
5570 		ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "CAC Stopped for vdev %d\n",
5571 			   arvif->vdev_id);
5572 	}
5573 
5574 	return 0;
5575 err:
5576 	return ret;
5577 }
5578 
5579 static int ath12k_mac_vdev_start(struct ath12k_vif *arvif,
5580 				 const struct cfg80211_chan_def *chandef)
5581 {
5582 	return ath12k_mac_vdev_start_restart(arvif, chandef, false);
5583 }
5584 
5585 static int ath12k_mac_vdev_restart(struct ath12k_vif *arvif,
5586 				   const struct cfg80211_chan_def *chandef)
5587 {
5588 	return ath12k_mac_vdev_start_restart(arvif, chandef, true);
5589 }
5590 
5591 struct ath12k_mac_change_chanctx_arg {
5592 	struct ieee80211_chanctx_conf *ctx;
5593 	struct ieee80211_vif_chanctx_switch *vifs;
5594 	int n_vifs;
5595 	int next_vif;
5596 };
5597 
5598 static void
5599 ath12k_mac_change_chanctx_cnt_iter(void *data, u8 *mac,
5600 				   struct ieee80211_vif *vif)
5601 {
5602 	struct ath12k_mac_change_chanctx_arg *arg = data;
5603 
5604 	if (rcu_access_pointer(vif->bss_conf.chanctx_conf) != arg->ctx)
5605 		return;
5606 
5607 	arg->n_vifs++;
5608 }
5609 
5610 static void
5611 ath12k_mac_change_chanctx_fill_iter(void *data, u8 *mac,
5612 				    struct ieee80211_vif *vif)
5613 {
5614 	struct ath12k_mac_change_chanctx_arg *arg = data;
5615 	struct ieee80211_chanctx_conf *ctx;
5616 
5617 	ctx = rcu_access_pointer(vif->bss_conf.chanctx_conf);
5618 	if (ctx != arg->ctx)
5619 		return;
5620 
5621 	if (WARN_ON(arg->next_vif == arg->n_vifs))
5622 		return;
5623 
5624 	arg->vifs[arg->next_vif].vif = vif;
5625 	arg->vifs[arg->next_vif].old_ctx = ctx;
5626 	arg->vifs[arg->next_vif].new_ctx = ctx;
5627 	arg->next_vif++;
5628 }
5629 
5630 static void
5631 ath12k_mac_update_vif_chan(struct ath12k *ar,
5632 			   struct ieee80211_vif_chanctx_switch *vifs,
5633 			   int n_vifs)
5634 {
5635 	struct ath12k_base *ab = ar->ab;
5636 	struct ath12k_vif *arvif;
5637 	int ret;
5638 	int i;
5639 	bool monitor_vif = false;
5640 
5641 	lockdep_assert_held(&ar->conf_mutex);
5642 
5643 	for (i = 0; i < n_vifs; i++) {
5644 		arvif = (void *)vifs[i].vif->drv_priv;
5645 
5646 		if (vifs[i].vif->type == NL80211_IFTYPE_MONITOR)
5647 			monitor_vif = true;
5648 
5649 		ath12k_dbg(ab, ATH12K_DBG_MAC,
5650 			   "mac chanctx switch vdev_id %i freq %u->%u width %d->%d\n",
5651 			   arvif->vdev_id,
5652 			   vifs[i].old_ctx->def.chan->center_freq,
5653 			   vifs[i].new_ctx->def.chan->center_freq,
5654 			   vifs[i].old_ctx->def.width,
5655 			   vifs[i].new_ctx->def.width);
5656 
5657 		if (WARN_ON(!arvif->is_started))
5658 			continue;
5659 
5660 		if (WARN_ON(!arvif->is_up))
5661 			continue;
5662 
5663 		ret = ath12k_wmi_vdev_down(ar, arvif->vdev_id);
5664 		if (ret) {
5665 			ath12k_warn(ab, "failed to down vdev %d: %d\n",
5666 				    arvif->vdev_id, ret);
5667 			continue;
5668 		}
5669 	}
5670 
5671 	/* All relevant vdevs are downed and associated channel resources
5672 	 * should be available for the channel switch now.
5673 	 */
5674 
5675 	/* TODO: Update ar->rx_channel */
5676 
5677 	for (i = 0; i < n_vifs; i++) {
5678 		arvif = (void *)vifs[i].vif->drv_priv;
5679 
5680 		if (WARN_ON(!arvif->is_started))
5681 			continue;
5682 
5683 		if (WARN_ON(!arvif->is_up))
5684 			continue;
5685 
5686 		ret = ath12k_mac_vdev_restart(arvif, &vifs[i].new_ctx->def);
5687 		if (ret) {
5688 			ath12k_warn(ab, "failed to restart vdev %d: %d\n",
5689 				    arvif->vdev_id, ret);
5690 			continue;
5691 		}
5692 
5693 		ret = ath12k_mac_setup_bcn_tmpl(arvif);
5694 		if (ret)
5695 			ath12k_warn(ab, "failed to update bcn tmpl during csa: %d\n",
5696 				    ret);
5697 
5698 		ret = ath12k_wmi_vdev_up(arvif->ar, arvif->vdev_id, arvif->aid,
5699 					 arvif->bssid);
5700 		if (ret) {
5701 			ath12k_warn(ab, "failed to bring vdev up %d: %d\n",
5702 				    arvif->vdev_id, ret);
5703 			continue;
5704 		}
5705 	}
5706 
5707 	/* Restart the internal monitor vdev on new channel */
5708 	if (!monitor_vif && ar->monitor_vdev_created) {
5709 		if (!ath12k_mac_monitor_stop(ar))
5710 			ath12k_mac_monitor_start(ar);
5711 	}
5712 }
5713 
5714 static void
5715 ath12k_mac_update_active_vif_chan(struct ath12k *ar,
5716 				  struct ieee80211_chanctx_conf *ctx)
5717 {
5718 	struct ath12k_mac_change_chanctx_arg arg = { .ctx = ctx };
5719 
5720 	lockdep_assert_held(&ar->conf_mutex);
5721 
5722 	ieee80211_iterate_active_interfaces_atomic(ar->hw,
5723 						   IEEE80211_IFACE_ITER_NORMAL,
5724 						   ath12k_mac_change_chanctx_cnt_iter,
5725 						   &arg);
5726 	if (arg.n_vifs == 0)
5727 		return;
5728 
5729 	arg.vifs = kcalloc(arg.n_vifs, sizeof(arg.vifs[0]), GFP_KERNEL);
5730 	if (!arg.vifs)
5731 		return;
5732 
5733 	ieee80211_iterate_active_interfaces_atomic(ar->hw,
5734 						   IEEE80211_IFACE_ITER_NORMAL,
5735 						   ath12k_mac_change_chanctx_fill_iter,
5736 						   &arg);
5737 
5738 	ath12k_mac_update_vif_chan(ar, arg.vifs, arg.n_vifs);
5739 
5740 	kfree(arg.vifs);
5741 }
5742 
5743 static void ath12k_mac_op_change_chanctx(struct ieee80211_hw *hw,
5744 					 struct ieee80211_chanctx_conf *ctx,
5745 					 u32 changed)
5746 {
5747 	struct ath12k *ar = hw->priv;
5748 	struct ath12k_base *ab = ar->ab;
5749 
5750 	mutex_lock(&ar->conf_mutex);
5751 
5752 	ath12k_dbg(ab, ATH12K_DBG_MAC,
5753 		   "mac chanctx change freq %u width %d ptr %pK changed %x\n",
5754 		   ctx->def.chan->center_freq, ctx->def.width, ctx, changed);
5755 
5756 	/* This shouldn't really happen because channel switching should use
5757 	 * switch_vif_chanctx().
5758 	 */
5759 	if (WARN_ON(changed & IEEE80211_CHANCTX_CHANGE_CHANNEL))
5760 		goto unlock;
5761 
5762 	if (changed & IEEE80211_CHANCTX_CHANGE_WIDTH)
5763 		ath12k_mac_update_active_vif_chan(ar, ctx);
5764 
5765 	/* TODO: Recalc radar detection */
5766 
5767 unlock:
5768 	mutex_unlock(&ar->conf_mutex);
5769 }
5770 
5771 static int ath12k_start_vdev_delay(struct ieee80211_hw *hw,
5772 				   struct ieee80211_vif *vif)
5773 {
5774 	struct ath12k *ar = hw->priv;
5775 	struct ath12k_base *ab = ar->ab;
5776 	struct ath12k_vif *arvif = (void *)vif->drv_priv;
5777 	int ret;
5778 
5779 	if (WARN_ON(arvif->is_started))
5780 		return -EBUSY;
5781 
5782 	ret = ath12k_mac_vdev_start(arvif, &arvif->chanctx.def);
5783 	if (ret) {
5784 		ath12k_warn(ab, "failed to start vdev %i addr %pM on freq %d: %d\n",
5785 			    arvif->vdev_id, vif->addr,
5786 			    arvif->chanctx.def.chan->center_freq, ret);
5787 		return ret;
5788 	}
5789 
5790 	if (arvif->vdev_type == WMI_VDEV_TYPE_MONITOR) {
5791 		ret = ath12k_monitor_vdev_up(ar, arvif->vdev_id);
5792 		if (ret) {
5793 			ath12k_warn(ab, "failed put monitor up: %d\n", ret);
5794 			return ret;
5795 		}
5796 	}
5797 
5798 	arvif->is_started = true;
5799 
5800 	/* TODO: Setup ps and cts/rts protection */
5801 	return 0;
5802 }
5803 
5804 static int
5805 ath12k_mac_op_assign_vif_chanctx(struct ieee80211_hw *hw,
5806 				 struct ieee80211_vif *vif,
5807 				 struct ieee80211_bss_conf *link_conf,
5808 				 struct ieee80211_chanctx_conf *ctx)
5809 {
5810 	struct ath12k *ar = hw->priv;
5811 	struct ath12k_base *ab = ar->ab;
5812 	struct ath12k_vif *arvif = (void *)vif->drv_priv;
5813 	int ret;
5814 	struct ath12k_wmi_peer_create_arg param;
5815 
5816 	mutex_lock(&ar->conf_mutex);
5817 
5818 	ath12k_dbg(ab, ATH12K_DBG_MAC,
5819 		   "mac chanctx assign ptr %pK vdev_id %i\n",
5820 		   ctx, arvif->vdev_id);
5821 
5822 	/* for some targets bss peer must be created before vdev_start */
5823 	if (ab->hw_params->vdev_start_delay &&
5824 	    arvif->vdev_type != WMI_VDEV_TYPE_AP &&
5825 	    arvif->vdev_type != WMI_VDEV_TYPE_MONITOR &&
5826 	    !ath12k_peer_exist_by_vdev_id(ab, arvif->vdev_id)) {
5827 		memcpy(&arvif->chanctx, ctx, sizeof(*ctx));
5828 		ret = 0;
5829 		goto out;
5830 	}
5831 
5832 	if (WARN_ON(arvif->is_started)) {
5833 		ret = -EBUSY;
5834 		goto out;
5835 	}
5836 
5837 	if (ab->hw_params->vdev_start_delay &&
5838 	    (arvif->vdev_type == WMI_VDEV_TYPE_AP ||
5839 	    arvif->vdev_type == WMI_VDEV_TYPE_MONITOR)) {
5840 		param.vdev_id = arvif->vdev_id;
5841 		param.peer_type = WMI_PEER_TYPE_DEFAULT;
5842 		param.peer_addr = ar->mac_addr;
5843 
5844 		ret = ath12k_peer_create(ar, arvif, NULL, &param);
5845 		if (ret) {
5846 			ath12k_warn(ab, "failed to create peer after vdev start delay: %d",
5847 				    ret);
5848 			goto out;
5849 		}
5850 	}
5851 
5852 	if (arvif->vdev_type == WMI_VDEV_TYPE_MONITOR) {
5853 		ret = ath12k_mac_monitor_start(ar);
5854 		if (ret)
5855 			goto out;
5856 		arvif->is_started = true;
5857 		goto out;
5858 	}
5859 
5860 	ret = ath12k_mac_vdev_start(arvif, &ctx->def);
5861 	if (ret) {
5862 		ath12k_warn(ab, "failed to start vdev %i addr %pM on freq %d: %d\n",
5863 			    arvif->vdev_id, vif->addr,
5864 			    ctx->def.chan->center_freq, ret);
5865 		goto out;
5866 	}
5867 
5868 	if (arvif->vdev_type != WMI_VDEV_TYPE_MONITOR && ar->monitor_vdev_created)
5869 		ath12k_mac_monitor_start(ar);
5870 
5871 	arvif->is_started = true;
5872 
5873 	/* TODO: Setup ps and cts/rts protection */
5874 
5875 out:
5876 	mutex_unlock(&ar->conf_mutex);
5877 
5878 	return ret;
5879 }
5880 
5881 static void
5882 ath12k_mac_op_unassign_vif_chanctx(struct ieee80211_hw *hw,
5883 				   struct ieee80211_vif *vif,
5884 				   struct ieee80211_bss_conf *link_conf,
5885 				   struct ieee80211_chanctx_conf *ctx)
5886 {
5887 	struct ath12k *ar = hw->priv;
5888 	struct ath12k_base *ab = ar->ab;
5889 	struct ath12k_vif *arvif = (void *)vif->drv_priv;
5890 	int ret;
5891 
5892 	mutex_lock(&ar->conf_mutex);
5893 
5894 	ath12k_dbg(ab, ATH12K_DBG_MAC,
5895 		   "mac chanctx unassign ptr %pK vdev_id %i\n",
5896 		   ctx, arvif->vdev_id);
5897 
5898 	WARN_ON(!arvif->is_started);
5899 
5900 	if (ab->hw_params->vdev_start_delay &&
5901 	    arvif->vdev_type == WMI_VDEV_TYPE_MONITOR &&
5902 	    ath12k_peer_find_by_addr(ab, ar->mac_addr))
5903 		ath12k_peer_delete(ar, arvif->vdev_id, ar->mac_addr);
5904 
5905 	if (arvif->vdev_type == WMI_VDEV_TYPE_MONITOR) {
5906 		ret = ath12k_mac_monitor_stop(ar);
5907 		if (ret) {
5908 			mutex_unlock(&ar->conf_mutex);
5909 			return;
5910 		}
5911 
5912 		arvif->is_started = false;
5913 		mutex_unlock(&ar->conf_mutex);
5914 	}
5915 
5916 	ret = ath12k_mac_vdev_stop(arvif);
5917 	if (ret)
5918 		ath12k_warn(ab, "failed to stop vdev %i: %d\n",
5919 			    arvif->vdev_id, ret);
5920 
5921 	arvif->is_started = false;
5922 
5923 	if (ab->hw_params->vdev_start_delay &&
5924 	    arvif->vdev_type == WMI_VDEV_TYPE_MONITOR)
5925 		ath12k_wmi_vdev_down(ar, arvif->vdev_id);
5926 
5927 	if (arvif->vdev_type != WMI_VDEV_TYPE_MONITOR &&
5928 	    ar->num_started_vdevs == 1 && ar->monitor_vdev_created)
5929 		ath12k_mac_monitor_stop(ar);
5930 
5931 	mutex_unlock(&ar->conf_mutex);
5932 }
5933 
5934 static int
5935 ath12k_mac_op_switch_vif_chanctx(struct ieee80211_hw *hw,
5936 				 struct ieee80211_vif_chanctx_switch *vifs,
5937 				 int n_vifs,
5938 				 enum ieee80211_chanctx_switch_mode mode)
5939 {
5940 	struct ath12k *ar = hw->priv;
5941 
5942 	mutex_lock(&ar->conf_mutex);
5943 
5944 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
5945 		   "mac chanctx switch n_vifs %d mode %d\n",
5946 		   n_vifs, mode);
5947 	ath12k_mac_update_vif_chan(ar, vifs, n_vifs);
5948 
5949 	mutex_unlock(&ar->conf_mutex);
5950 
5951 	return 0;
5952 }
5953 
5954 static int
5955 ath12k_set_vdev_param_to_all_vifs(struct ath12k *ar, int param, u32 value)
5956 {
5957 	struct ath12k_vif *arvif;
5958 	int ret = 0;
5959 
5960 	mutex_lock(&ar->conf_mutex);
5961 	list_for_each_entry(arvif, &ar->arvifs, list) {
5962 		ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "setting mac vdev %d param %d value %d\n",
5963 			   param, arvif->vdev_id, value);
5964 
5965 		ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
5966 						    param, value);
5967 		if (ret) {
5968 			ath12k_warn(ar->ab, "failed to set param %d for vdev %d: %d\n",
5969 				    param, arvif->vdev_id, ret);
5970 			break;
5971 		}
5972 	}
5973 	mutex_unlock(&ar->conf_mutex);
5974 	return ret;
5975 }
5976 
5977 /* mac80211 stores device specific RTS/Fragmentation threshold value,
5978  * this is set interface specific to firmware from ath12k driver
5979  */
5980 static int ath12k_mac_op_set_rts_threshold(struct ieee80211_hw *hw, u32 value)
5981 {
5982 	struct ath12k *ar = hw->priv;
5983 	int param_id = WMI_VDEV_PARAM_RTS_THRESHOLD;
5984 
5985 	return ath12k_set_vdev_param_to_all_vifs(ar, param_id, value);
5986 }
5987 
5988 static int ath12k_mac_op_set_frag_threshold(struct ieee80211_hw *hw, u32 value)
5989 {
5990 	/* Even though there's a WMI vdev param for fragmentation threshold no
5991 	 * known firmware actually implements it. Moreover it is not possible to
5992 	 * rely frame fragmentation to mac80211 because firmware clears the
5993 	 * "more fragments" bit in frame control making it impossible for remote
5994 	 * devices to reassemble frames.
5995 	 *
5996 	 * Hence implement a dummy callback just to say fragmentation isn't
5997 	 * supported. This effectively prevents mac80211 from doing frame
5998 	 * fragmentation in software.
5999 	 */
6000 	return -EOPNOTSUPP;
6001 }
6002 
6003 static void ath12k_mac_op_flush(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
6004 				u32 queues, bool drop)
6005 {
6006 	struct ath12k *ar = hw->priv;
6007 	long time_left;
6008 
6009 	if (drop)
6010 		return;
6011 
6012 	time_left = wait_event_timeout(ar->dp.tx_empty_waitq,
6013 				       (atomic_read(&ar->dp.num_tx_pending) == 0),
6014 				       ATH12K_FLUSH_TIMEOUT);
6015 	if (time_left == 0)
6016 		ath12k_warn(ar->ab, "failed to flush transmit queue %ld\n", time_left);
6017 }
6018 
6019 static int
6020 ath12k_mac_bitrate_mask_num_ht_rates(struct ath12k *ar,
6021 				     enum nl80211_band band,
6022 				     const struct cfg80211_bitrate_mask *mask)
6023 {
6024 	int num_rates = 0;
6025 	int i;
6026 
6027 	for (i = 0; i < ARRAY_SIZE(mask->control[band].ht_mcs); i++)
6028 		num_rates += hweight16(mask->control[band].ht_mcs[i]);
6029 
6030 	return num_rates;
6031 }
6032 
6033 static bool
6034 ath12k_mac_has_single_legacy_rate(struct ath12k *ar,
6035 				  enum nl80211_band band,
6036 				  const struct cfg80211_bitrate_mask *mask)
6037 {
6038 	int num_rates = 0;
6039 
6040 	num_rates = hweight32(mask->control[band].legacy);
6041 
6042 	if (ath12k_mac_bitrate_mask_num_ht_rates(ar, band, mask))
6043 		return false;
6044 
6045 	if (ath12k_mac_bitrate_mask_num_vht_rates(ar, band, mask))
6046 		return false;
6047 
6048 	return num_rates == 1;
6049 }
6050 
6051 static bool
6052 ath12k_mac_bitrate_mask_get_single_nss(struct ath12k *ar,
6053 				       enum nl80211_band band,
6054 				       const struct cfg80211_bitrate_mask *mask,
6055 				       int *nss)
6056 {
6057 	struct ieee80211_supported_band *sband = &ar->mac.sbands[band];
6058 	u16 vht_mcs_map = le16_to_cpu(sband->vht_cap.vht_mcs.tx_mcs_map);
6059 	u8 ht_nss_mask = 0;
6060 	u8 vht_nss_mask = 0;
6061 	int i;
6062 
6063 	/* No need to consider legacy here. Basic rates are always present
6064 	 * in bitrate mask
6065 	 */
6066 
6067 	for (i = 0; i < ARRAY_SIZE(mask->control[band].ht_mcs); i++) {
6068 		if (mask->control[band].ht_mcs[i] == 0)
6069 			continue;
6070 		else if (mask->control[band].ht_mcs[i] ==
6071 			 sband->ht_cap.mcs.rx_mask[i])
6072 			ht_nss_mask |= BIT(i);
6073 		else
6074 			return false;
6075 	}
6076 
6077 	for (i = 0; i < ARRAY_SIZE(mask->control[band].vht_mcs); i++) {
6078 		if (mask->control[band].vht_mcs[i] == 0)
6079 			continue;
6080 		else if (mask->control[band].vht_mcs[i] ==
6081 			 ath12k_mac_get_max_vht_mcs_map(vht_mcs_map, i))
6082 			vht_nss_mask |= BIT(i);
6083 		else
6084 			return false;
6085 	}
6086 
6087 	if (ht_nss_mask != vht_nss_mask)
6088 		return false;
6089 
6090 	if (ht_nss_mask == 0)
6091 		return false;
6092 
6093 	if (BIT(fls(ht_nss_mask)) - 1 != ht_nss_mask)
6094 		return false;
6095 
6096 	*nss = fls(ht_nss_mask);
6097 
6098 	return true;
6099 }
6100 
6101 static int
6102 ath12k_mac_get_single_legacy_rate(struct ath12k *ar,
6103 				  enum nl80211_band band,
6104 				  const struct cfg80211_bitrate_mask *mask,
6105 				  u32 *rate, u8 *nss)
6106 {
6107 	int rate_idx;
6108 	u16 bitrate;
6109 	u8 preamble;
6110 	u8 hw_rate;
6111 
6112 	if (hweight32(mask->control[band].legacy) != 1)
6113 		return -EINVAL;
6114 
6115 	rate_idx = ffs(mask->control[band].legacy) - 1;
6116 
6117 	if (band == NL80211_BAND_5GHZ || band == NL80211_BAND_6GHZ)
6118 		rate_idx += ATH12K_MAC_FIRST_OFDM_RATE_IDX;
6119 
6120 	hw_rate = ath12k_legacy_rates[rate_idx].hw_value;
6121 	bitrate = ath12k_legacy_rates[rate_idx].bitrate;
6122 
6123 	if (ath12k_mac_bitrate_is_cck(bitrate))
6124 		preamble = WMI_RATE_PREAMBLE_CCK;
6125 	else
6126 		preamble = WMI_RATE_PREAMBLE_OFDM;
6127 
6128 	*nss = 1;
6129 	*rate = ATH12K_HW_RATE_CODE(hw_rate, 0, preamble);
6130 
6131 	return 0;
6132 }
6133 
6134 static int ath12k_mac_set_fixed_rate_params(struct ath12k_vif *arvif,
6135 					    u32 rate, u8 nss, u8 sgi, u8 ldpc)
6136 {
6137 	struct ath12k *ar = arvif->ar;
6138 	u32 vdev_param;
6139 	int ret;
6140 
6141 	lockdep_assert_held(&ar->conf_mutex);
6142 
6143 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac set fixed rate params vdev %i rate 0x%02x nss %u sgi %u\n",
6144 		   arvif->vdev_id, rate, nss, sgi);
6145 
6146 	vdev_param = WMI_VDEV_PARAM_FIXED_RATE;
6147 	ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
6148 					    vdev_param, rate);
6149 	if (ret) {
6150 		ath12k_warn(ar->ab, "failed to set fixed rate param 0x%02x: %d\n",
6151 			    rate, ret);
6152 		return ret;
6153 	}
6154 
6155 	vdev_param = WMI_VDEV_PARAM_NSS;
6156 	ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
6157 					    vdev_param, nss);
6158 	if (ret) {
6159 		ath12k_warn(ar->ab, "failed to set nss param %d: %d\n",
6160 			    nss, ret);
6161 		return ret;
6162 	}
6163 
6164 	vdev_param = WMI_VDEV_PARAM_SGI;
6165 	ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
6166 					    vdev_param, sgi);
6167 	if (ret) {
6168 		ath12k_warn(ar->ab, "failed to set sgi param %d: %d\n",
6169 			    sgi, ret);
6170 		return ret;
6171 	}
6172 
6173 	vdev_param = WMI_VDEV_PARAM_LDPC;
6174 	ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
6175 					    vdev_param, ldpc);
6176 	if (ret) {
6177 		ath12k_warn(ar->ab, "failed to set ldpc param %d: %d\n",
6178 			    ldpc, ret);
6179 		return ret;
6180 	}
6181 
6182 	return 0;
6183 }
6184 
6185 static bool
6186 ath12k_mac_vht_mcs_range_present(struct ath12k *ar,
6187 				 enum nl80211_band band,
6188 				 const struct cfg80211_bitrate_mask *mask)
6189 {
6190 	int i;
6191 	u16 vht_mcs;
6192 
6193 	for (i = 0; i < NL80211_VHT_NSS_MAX; i++) {
6194 		vht_mcs = mask->control[band].vht_mcs[i];
6195 
6196 		switch (vht_mcs) {
6197 		case 0:
6198 		case BIT(8) - 1:
6199 		case BIT(9) - 1:
6200 		case BIT(10) - 1:
6201 			break;
6202 		default:
6203 			return false;
6204 		}
6205 	}
6206 
6207 	return true;
6208 }
6209 
6210 static void ath12k_mac_set_bitrate_mask_iter(void *data,
6211 					     struct ieee80211_sta *sta)
6212 {
6213 	struct ath12k_vif *arvif = data;
6214 	struct ath12k_sta *arsta = (struct ath12k_sta *)sta->drv_priv;
6215 	struct ath12k *ar = arvif->ar;
6216 
6217 	spin_lock_bh(&ar->data_lock);
6218 	arsta->changed |= IEEE80211_RC_SUPP_RATES_CHANGED;
6219 	spin_unlock_bh(&ar->data_lock);
6220 
6221 	ieee80211_queue_work(ar->hw, &arsta->update_wk);
6222 }
6223 
6224 static void ath12k_mac_disable_peer_fixed_rate(void *data,
6225 					       struct ieee80211_sta *sta)
6226 {
6227 	struct ath12k_vif *arvif = data;
6228 	struct ath12k *ar = arvif->ar;
6229 	int ret;
6230 
6231 	ret = ath12k_wmi_set_peer_param(ar, sta->addr,
6232 					arvif->vdev_id,
6233 					WMI_PEER_PARAM_FIXED_RATE,
6234 					WMI_FIXED_RATE_NONE);
6235 	if (ret)
6236 		ath12k_warn(ar->ab,
6237 			    "failed to disable peer fixed rate for STA %pM ret %d\n",
6238 			    sta->addr, ret);
6239 }
6240 
6241 static int
6242 ath12k_mac_op_set_bitrate_mask(struct ieee80211_hw *hw,
6243 			       struct ieee80211_vif *vif,
6244 			       const struct cfg80211_bitrate_mask *mask)
6245 {
6246 	struct ath12k_vif *arvif = (void *)vif->drv_priv;
6247 	struct cfg80211_chan_def def;
6248 	struct ath12k *ar = arvif->ar;
6249 	enum nl80211_band band;
6250 	const u8 *ht_mcs_mask;
6251 	const u16 *vht_mcs_mask;
6252 	u32 rate;
6253 	u8 nss;
6254 	u8 sgi;
6255 	u8 ldpc;
6256 	int single_nss;
6257 	int ret;
6258 	int num_rates;
6259 
6260 	if (ath12k_mac_vif_chan(vif, &def))
6261 		return -EPERM;
6262 
6263 	band = def.chan->band;
6264 	ht_mcs_mask = mask->control[band].ht_mcs;
6265 	vht_mcs_mask = mask->control[band].vht_mcs;
6266 	ldpc = !!(ar->ht_cap_info & WMI_HT_CAP_LDPC);
6267 
6268 	sgi = mask->control[band].gi;
6269 	if (sgi == NL80211_TXRATE_FORCE_LGI)
6270 		return -EINVAL;
6271 
6272 	/* mac80211 doesn't support sending a fixed HT/VHT MCS alone, rather it
6273 	 * requires passing at least one of used basic rates along with them.
6274 	 * Fixed rate setting across different preambles(legacy, HT, VHT) is
6275 	 * not supported by the FW. Hence use of FIXED_RATE vdev param is not
6276 	 * suitable for setting single HT/VHT rates.
6277 	 * But, there could be a single basic rate passed from userspace which
6278 	 * can be done through the FIXED_RATE param.
6279 	 */
6280 	if (ath12k_mac_has_single_legacy_rate(ar, band, mask)) {
6281 		ret = ath12k_mac_get_single_legacy_rate(ar, band, mask, &rate,
6282 							&nss);
6283 		if (ret) {
6284 			ath12k_warn(ar->ab, "failed to get single legacy rate for vdev %i: %d\n",
6285 				    arvif->vdev_id, ret);
6286 			return ret;
6287 		}
6288 		ieee80211_iterate_stations_atomic(ar->hw,
6289 						  ath12k_mac_disable_peer_fixed_rate,
6290 						  arvif);
6291 	} else if (ath12k_mac_bitrate_mask_get_single_nss(ar, band, mask,
6292 							  &single_nss)) {
6293 		rate = WMI_FIXED_RATE_NONE;
6294 		nss = single_nss;
6295 	} else {
6296 		rate = WMI_FIXED_RATE_NONE;
6297 		nss = min_t(u32, ar->num_tx_chains,
6298 			    max(ath12k_mac_max_ht_nss(ht_mcs_mask),
6299 				ath12k_mac_max_vht_nss(vht_mcs_mask)));
6300 
6301 		/* If multiple rates across different preambles are given
6302 		 * we can reconfigure this info with all peers using PEER_ASSOC
6303 		 * command with the below exception cases.
6304 		 * - Single VHT Rate : peer_assoc command accommodates only MCS
6305 		 * range values i.e 0-7, 0-8, 0-9 for VHT. Though mac80211
6306 		 * mandates passing basic rates along with HT/VHT rates, FW
6307 		 * doesn't allow switching from VHT to Legacy. Hence instead of
6308 		 * setting legacy and VHT rates using RATEMASK_CMD vdev cmd,
6309 		 * we could set this VHT rate as peer fixed rate param, which
6310 		 * will override FIXED rate and FW rate control algorithm.
6311 		 * If single VHT rate is passed along with HT rates, we select
6312 		 * the VHT rate as fixed rate for vht peers.
6313 		 * - Multiple VHT Rates : When Multiple VHT rates are given,this
6314 		 * can be set using RATEMASK CMD which uses FW rate-ctl alg.
6315 		 * TODO: Setting multiple VHT MCS and replacing peer_assoc with
6316 		 * RATEMASK_CMDID can cover all use cases of setting rates
6317 		 * across multiple preambles and rates within same type.
6318 		 * But requires more validation of the command at this point.
6319 		 */
6320 
6321 		num_rates = ath12k_mac_bitrate_mask_num_vht_rates(ar, band,
6322 								  mask);
6323 
6324 		if (!ath12k_mac_vht_mcs_range_present(ar, band, mask) &&
6325 		    num_rates > 1) {
6326 			/* TODO: Handle multiple VHT MCS values setting using
6327 			 * RATEMASK CMD
6328 			 */
6329 			ath12k_warn(ar->ab,
6330 				    "Setting more than one MCS Value in bitrate mask not supported\n");
6331 			return -EINVAL;
6332 		}
6333 
6334 		ieee80211_iterate_stations_atomic(ar->hw,
6335 						  ath12k_mac_disable_peer_fixed_rate,
6336 						  arvif);
6337 
6338 		mutex_lock(&ar->conf_mutex);
6339 
6340 		arvif->bitrate_mask = *mask;
6341 		ieee80211_iterate_stations_atomic(ar->hw,
6342 						  ath12k_mac_set_bitrate_mask_iter,
6343 						  arvif);
6344 
6345 		mutex_unlock(&ar->conf_mutex);
6346 	}
6347 
6348 	mutex_lock(&ar->conf_mutex);
6349 
6350 	ret = ath12k_mac_set_fixed_rate_params(arvif, rate, nss, sgi, ldpc);
6351 	if (ret) {
6352 		ath12k_warn(ar->ab, "failed to set fixed rate params on vdev %i: %d\n",
6353 			    arvif->vdev_id, ret);
6354 	}
6355 
6356 	mutex_unlock(&ar->conf_mutex);
6357 
6358 	return ret;
6359 }
6360 
6361 static void
6362 ath12k_mac_op_reconfig_complete(struct ieee80211_hw *hw,
6363 				enum ieee80211_reconfig_type reconfig_type)
6364 {
6365 	struct ath12k *ar = hw->priv;
6366 	struct ath12k_base *ab = ar->ab;
6367 	int recovery_count;
6368 
6369 	if (reconfig_type != IEEE80211_RECONFIG_TYPE_RESTART)
6370 		return;
6371 
6372 	mutex_lock(&ar->conf_mutex);
6373 
6374 	if (ar->state == ATH12K_STATE_RESTARTED) {
6375 		ath12k_warn(ar->ab, "pdev %d successfully recovered\n",
6376 			    ar->pdev->pdev_id);
6377 		ar->state = ATH12K_STATE_ON;
6378 		ieee80211_wake_queues(ar->hw);
6379 
6380 		if (ab->is_reset) {
6381 			recovery_count = atomic_inc_return(&ab->recovery_count);
6382 			ath12k_dbg(ab, ATH12K_DBG_BOOT, "recovery count %d\n",
6383 				   recovery_count);
6384 			/* When there are multiple radios in an SOC,
6385 			 * the recovery has to be done for each radio
6386 			 */
6387 			if (recovery_count == ab->num_radios) {
6388 				atomic_dec(&ab->reset_count);
6389 				complete(&ab->reset_complete);
6390 				ab->is_reset = false;
6391 				atomic_set(&ab->fail_cont_count, 0);
6392 				ath12k_dbg(ab, ATH12K_DBG_BOOT, "reset success\n");
6393 			}
6394 		}
6395 	}
6396 
6397 	mutex_unlock(&ar->conf_mutex);
6398 }
6399 
6400 static void
6401 ath12k_mac_update_bss_chan_survey(struct ath12k *ar,
6402 				  struct ieee80211_channel *channel)
6403 {
6404 	int ret;
6405 	enum wmi_bss_chan_info_req_type type = WMI_BSS_SURVEY_REQ_TYPE_READ;
6406 
6407 	lockdep_assert_held(&ar->conf_mutex);
6408 
6409 	if (!test_bit(WMI_TLV_SERVICE_BSS_CHANNEL_INFO_64, ar->ab->wmi_ab.svc_map) ||
6410 	    ar->rx_channel != channel)
6411 		return;
6412 
6413 	if (ar->scan.state != ATH12K_SCAN_IDLE) {
6414 		ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
6415 			   "ignoring bss chan info req while scanning..\n");
6416 		return;
6417 	}
6418 
6419 	reinit_completion(&ar->bss_survey_done);
6420 
6421 	ret = ath12k_wmi_pdev_bss_chan_info_request(ar, type);
6422 	if (ret) {
6423 		ath12k_warn(ar->ab, "failed to send pdev bss chan info request\n");
6424 		return;
6425 	}
6426 
6427 	ret = wait_for_completion_timeout(&ar->bss_survey_done, 3 * HZ);
6428 	if (ret == 0)
6429 		ath12k_warn(ar->ab, "bss channel survey timed out\n");
6430 }
6431 
6432 static int ath12k_mac_op_get_survey(struct ieee80211_hw *hw, int idx,
6433 				    struct survey_info *survey)
6434 {
6435 	struct ath12k *ar = hw->priv;
6436 	struct ieee80211_supported_band *sband;
6437 	struct survey_info *ar_survey;
6438 	int ret = 0;
6439 
6440 	if (idx >= ATH12K_NUM_CHANS)
6441 		return -ENOENT;
6442 
6443 	ar_survey = &ar->survey[idx];
6444 
6445 	mutex_lock(&ar->conf_mutex);
6446 
6447 	sband = hw->wiphy->bands[NL80211_BAND_2GHZ];
6448 	if (sband && idx >= sband->n_channels) {
6449 		idx -= sband->n_channels;
6450 		sband = NULL;
6451 	}
6452 
6453 	if (!sband)
6454 		sband = hw->wiphy->bands[NL80211_BAND_5GHZ];
6455 
6456 	if (!sband || idx >= sband->n_channels) {
6457 		ret = -ENOENT;
6458 		goto exit;
6459 	}
6460 
6461 	ath12k_mac_update_bss_chan_survey(ar, &sband->channels[idx]);
6462 
6463 	spin_lock_bh(&ar->data_lock);
6464 	memcpy(survey, ar_survey, sizeof(*survey));
6465 	spin_unlock_bh(&ar->data_lock);
6466 
6467 	survey->channel = &sband->channels[idx];
6468 
6469 	if (ar->rx_channel == survey->channel)
6470 		survey->filled |= SURVEY_INFO_IN_USE;
6471 
6472 exit:
6473 	mutex_unlock(&ar->conf_mutex);
6474 	return ret;
6475 }
6476 
6477 static void ath12k_mac_op_sta_statistics(struct ieee80211_hw *hw,
6478 					 struct ieee80211_vif *vif,
6479 					 struct ieee80211_sta *sta,
6480 					 struct station_info *sinfo)
6481 {
6482 	struct ath12k_sta *arsta = (struct ath12k_sta *)sta->drv_priv;
6483 
6484 	sinfo->rx_duration = arsta->rx_duration;
6485 	sinfo->filled |= BIT_ULL(NL80211_STA_INFO_RX_DURATION);
6486 
6487 	sinfo->tx_duration = arsta->tx_duration;
6488 	sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_DURATION);
6489 
6490 	if (!arsta->txrate.legacy && !arsta->txrate.nss)
6491 		return;
6492 
6493 	if (arsta->txrate.legacy) {
6494 		sinfo->txrate.legacy = arsta->txrate.legacy;
6495 	} else {
6496 		sinfo->txrate.mcs = arsta->txrate.mcs;
6497 		sinfo->txrate.nss = arsta->txrate.nss;
6498 		sinfo->txrate.bw = arsta->txrate.bw;
6499 		sinfo->txrate.he_gi = arsta->txrate.he_gi;
6500 		sinfo->txrate.he_dcm = arsta->txrate.he_dcm;
6501 		sinfo->txrate.he_ru_alloc = arsta->txrate.he_ru_alloc;
6502 	}
6503 	sinfo->txrate.flags = arsta->txrate.flags;
6504 	sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_BITRATE);
6505 
6506 	/* TODO: Use real NF instead of default one. */
6507 	sinfo->signal = arsta->rssi_comb + ATH12K_DEFAULT_NOISE_FLOOR;
6508 	sinfo->filled |= BIT_ULL(NL80211_STA_INFO_SIGNAL);
6509 }
6510 
6511 static const struct ieee80211_ops ath12k_ops = {
6512 	.tx				= ath12k_mac_op_tx,
6513 	.wake_tx_queue			= ieee80211_handle_wake_tx_queue,
6514 	.start                          = ath12k_mac_op_start,
6515 	.stop                           = ath12k_mac_op_stop,
6516 	.reconfig_complete              = ath12k_mac_op_reconfig_complete,
6517 	.add_interface                  = ath12k_mac_op_add_interface,
6518 	.remove_interface		= ath12k_mac_op_remove_interface,
6519 	.update_vif_offload		= ath12k_mac_op_update_vif_offload,
6520 	.config                         = ath12k_mac_op_config,
6521 	.bss_info_changed               = ath12k_mac_op_bss_info_changed,
6522 	.configure_filter		= ath12k_mac_op_configure_filter,
6523 	.hw_scan                        = ath12k_mac_op_hw_scan,
6524 	.cancel_hw_scan                 = ath12k_mac_op_cancel_hw_scan,
6525 	.set_key                        = ath12k_mac_op_set_key,
6526 	.sta_state                      = ath12k_mac_op_sta_state,
6527 	.sta_set_txpwr			= ath12k_mac_op_sta_set_txpwr,
6528 	.sta_rc_update			= ath12k_mac_op_sta_rc_update,
6529 	.conf_tx                        = ath12k_mac_op_conf_tx,
6530 	.set_antenna			= ath12k_mac_op_set_antenna,
6531 	.get_antenna			= ath12k_mac_op_get_antenna,
6532 	.ampdu_action			= ath12k_mac_op_ampdu_action,
6533 	.add_chanctx			= ath12k_mac_op_add_chanctx,
6534 	.remove_chanctx			= ath12k_mac_op_remove_chanctx,
6535 	.change_chanctx			= ath12k_mac_op_change_chanctx,
6536 	.assign_vif_chanctx		= ath12k_mac_op_assign_vif_chanctx,
6537 	.unassign_vif_chanctx		= ath12k_mac_op_unassign_vif_chanctx,
6538 	.switch_vif_chanctx		= ath12k_mac_op_switch_vif_chanctx,
6539 	.set_rts_threshold		= ath12k_mac_op_set_rts_threshold,
6540 	.set_frag_threshold		= ath12k_mac_op_set_frag_threshold,
6541 	.set_bitrate_mask		= ath12k_mac_op_set_bitrate_mask,
6542 	.get_survey			= ath12k_mac_op_get_survey,
6543 	.flush				= ath12k_mac_op_flush,
6544 	.sta_statistics			= ath12k_mac_op_sta_statistics,
6545 };
6546 
6547 static void ath12k_mac_update_ch_list(struct ath12k *ar,
6548 				      struct ieee80211_supported_band *band,
6549 				      u32 freq_low, u32 freq_high)
6550 {
6551 	int i;
6552 
6553 	if (!(freq_low && freq_high))
6554 		return;
6555 
6556 	for (i = 0; i < band->n_channels; i++) {
6557 		if (band->channels[i].center_freq < freq_low ||
6558 		    band->channels[i].center_freq > freq_high)
6559 			band->channels[i].flags |= IEEE80211_CHAN_DISABLED;
6560 	}
6561 }
6562 
6563 static u32 ath12k_get_phy_id(struct ath12k *ar, u32 band)
6564 {
6565 	struct ath12k_pdev *pdev = ar->pdev;
6566 	struct ath12k_pdev_cap *pdev_cap = &pdev->cap;
6567 
6568 	if (band == WMI_HOST_WLAN_2G_CAP)
6569 		return pdev_cap->band[NL80211_BAND_2GHZ].phy_id;
6570 
6571 	if (band == WMI_HOST_WLAN_5G_CAP)
6572 		return pdev_cap->band[NL80211_BAND_5GHZ].phy_id;
6573 
6574 	ath12k_warn(ar->ab, "unsupported phy cap:%d\n", band);
6575 
6576 	return 0;
6577 }
6578 
6579 static int ath12k_mac_setup_channels_rates(struct ath12k *ar,
6580 					   u32 supported_bands)
6581 {
6582 	struct ieee80211_supported_band *band;
6583 	struct ath12k_wmi_hal_reg_capabilities_ext_arg *reg_cap;
6584 	void *channels;
6585 	u32 phy_id;
6586 
6587 	BUILD_BUG_ON((ARRAY_SIZE(ath12k_2ghz_channels) +
6588 		      ARRAY_SIZE(ath12k_5ghz_channels) +
6589 		      ARRAY_SIZE(ath12k_6ghz_channels)) !=
6590 		     ATH12K_NUM_CHANS);
6591 
6592 	reg_cap = &ar->ab->hal_reg_cap[ar->pdev_idx];
6593 
6594 	if (supported_bands & WMI_HOST_WLAN_2G_CAP) {
6595 		channels = kmemdup(ath12k_2ghz_channels,
6596 				   sizeof(ath12k_2ghz_channels),
6597 				   GFP_KERNEL);
6598 		if (!channels)
6599 			return -ENOMEM;
6600 
6601 		band = &ar->mac.sbands[NL80211_BAND_2GHZ];
6602 		band->band = NL80211_BAND_2GHZ;
6603 		band->n_channels = ARRAY_SIZE(ath12k_2ghz_channels);
6604 		band->channels = channels;
6605 		band->n_bitrates = ath12k_g_rates_size;
6606 		band->bitrates = ath12k_g_rates;
6607 		ar->hw->wiphy->bands[NL80211_BAND_2GHZ] = band;
6608 
6609 		if (ar->ab->hw_params->single_pdev_only) {
6610 			phy_id = ath12k_get_phy_id(ar, WMI_HOST_WLAN_2G_CAP);
6611 			reg_cap = &ar->ab->hal_reg_cap[phy_id];
6612 		}
6613 		ath12k_mac_update_ch_list(ar, band,
6614 					  reg_cap->low_2ghz_chan,
6615 					  reg_cap->high_2ghz_chan);
6616 	}
6617 
6618 	if (supported_bands & WMI_HOST_WLAN_5G_CAP) {
6619 		if (reg_cap->high_5ghz_chan >= ATH12K_MAX_6G_FREQ) {
6620 			channels = kmemdup(ath12k_6ghz_channels,
6621 					   sizeof(ath12k_6ghz_channels), GFP_KERNEL);
6622 			if (!channels) {
6623 				kfree(ar->mac.sbands[NL80211_BAND_2GHZ].channels);
6624 				return -ENOMEM;
6625 			}
6626 
6627 			ar->supports_6ghz = true;
6628 			band = &ar->mac.sbands[NL80211_BAND_6GHZ];
6629 			band->band = NL80211_BAND_6GHZ;
6630 			band->n_channels = ARRAY_SIZE(ath12k_6ghz_channels);
6631 			band->channels = channels;
6632 			band->n_bitrates = ath12k_a_rates_size;
6633 			band->bitrates = ath12k_a_rates;
6634 			ar->hw->wiphy->bands[NL80211_BAND_6GHZ] = band;
6635 			ath12k_mac_update_ch_list(ar, band,
6636 						  reg_cap->low_5ghz_chan,
6637 						  reg_cap->high_5ghz_chan);
6638 		}
6639 
6640 		if (reg_cap->low_5ghz_chan < ATH12K_MIN_6G_FREQ) {
6641 			channels = kmemdup(ath12k_5ghz_channels,
6642 					   sizeof(ath12k_5ghz_channels),
6643 					   GFP_KERNEL);
6644 			if (!channels) {
6645 				kfree(ar->mac.sbands[NL80211_BAND_2GHZ].channels);
6646 				kfree(ar->mac.sbands[NL80211_BAND_6GHZ].channels);
6647 				return -ENOMEM;
6648 			}
6649 
6650 			band = &ar->mac.sbands[NL80211_BAND_5GHZ];
6651 			band->band = NL80211_BAND_5GHZ;
6652 			band->n_channels = ARRAY_SIZE(ath12k_5ghz_channels);
6653 			band->channels = channels;
6654 			band->n_bitrates = ath12k_a_rates_size;
6655 			band->bitrates = ath12k_a_rates;
6656 			ar->hw->wiphy->bands[NL80211_BAND_5GHZ] = band;
6657 
6658 			if (ar->ab->hw_params->single_pdev_only) {
6659 				phy_id = ath12k_get_phy_id(ar, WMI_HOST_WLAN_5G_CAP);
6660 				reg_cap = &ar->ab->hal_reg_cap[phy_id];
6661 			}
6662 
6663 			ath12k_mac_update_ch_list(ar, band,
6664 						  reg_cap->low_5ghz_chan,
6665 						  reg_cap->high_5ghz_chan);
6666 		}
6667 	}
6668 
6669 	return 0;
6670 }
6671 
6672 static int ath12k_mac_setup_iface_combinations(struct ath12k *ar)
6673 {
6674 	struct ath12k_base *ab = ar->ab;
6675 	struct ieee80211_iface_combination *combinations;
6676 	struct ieee80211_iface_limit *limits;
6677 	int n_limits, max_interfaces;
6678 	bool ap, mesh;
6679 
6680 	ap = ab->hw_params->interface_modes & BIT(NL80211_IFTYPE_AP);
6681 
6682 	mesh = IS_ENABLED(CONFIG_MAC80211_MESH) &&
6683 		ab->hw_params->interface_modes & BIT(NL80211_IFTYPE_MESH_POINT);
6684 
6685 	combinations = kzalloc(sizeof(*combinations), GFP_KERNEL);
6686 	if (!combinations)
6687 		return -ENOMEM;
6688 
6689 	if (ap || mesh) {
6690 		n_limits = 2;
6691 		max_interfaces = 16;
6692 	} else {
6693 		n_limits = 1;
6694 		max_interfaces = 1;
6695 	}
6696 
6697 	limits = kcalloc(n_limits, sizeof(*limits), GFP_KERNEL);
6698 	if (!limits) {
6699 		kfree(combinations);
6700 		return -ENOMEM;
6701 	}
6702 
6703 	limits[0].max = 1;
6704 	limits[0].types |= BIT(NL80211_IFTYPE_STATION);
6705 
6706 	if (ap) {
6707 		limits[1].max = max_interfaces;
6708 		limits[1].types |= BIT(NL80211_IFTYPE_AP);
6709 	}
6710 
6711 	if (mesh)
6712 		limits[1].types |= BIT(NL80211_IFTYPE_MESH_POINT);
6713 
6714 	combinations[0].limits = limits;
6715 	combinations[0].n_limits = n_limits;
6716 	combinations[0].max_interfaces = max_interfaces;
6717 	combinations[0].num_different_channels = 1;
6718 	combinations[0].beacon_int_infra_match = true;
6719 	combinations[0].beacon_int_min_gcd = 100;
6720 	combinations[0].radar_detect_widths = BIT(NL80211_CHAN_WIDTH_20_NOHT) |
6721 						BIT(NL80211_CHAN_WIDTH_20) |
6722 						BIT(NL80211_CHAN_WIDTH_40) |
6723 						BIT(NL80211_CHAN_WIDTH_80);
6724 
6725 	ar->hw->wiphy->iface_combinations = combinations;
6726 	ar->hw->wiphy->n_iface_combinations = 1;
6727 
6728 	return 0;
6729 }
6730 
6731 static const u8 ath12k_if_types_ext_capa[] = {
6732 	[0] = WLAN_EXT_CAPA1_EXT_CHANNEL_SWITCHING,
6733 	[7] = WLAN_EXT_CAPA8_OPMODE_NOTIF,
6734 };
6735 
6736 static const u8 ath12k_if_types_ext_capa_sta[] = {
6737 	[0] = WLAN_EXT_CAPA1_EXT_CHANNEL_SWITCHING,
6738 	[7] = WLAN_EXT_CAPA8_OPMODE_NOTIF,
6739 	[9] = WLAN_EXT_CAPA10_TWT_REQUESTER_SUPPORT,
6740 };
6741 
6742 static const u8 ath12k_if_types_ext_capa_ap[] = {
6743 	[0] = WLAN_EXT_CAPA1_EXT_CHANNEL_SWITCHING,
6744 	[7] = WLAN_EXT_CAPA8_OPMODE_NOTIF,
6745 	[9] = WLAN_EXT_CAPA10_TWT_RESPONDER_SUPPORT,
6746 };
6747 
6748 static const struct wiphy_iftype_ext_capab ath12k_iftypes_ext_capa[] = {
6749 	{
6750 		.extended_capabilities = ath12k_if_types_ext_capa,
6751 		.extended_capabilities_mask = ath12k_if_types_ext_capa,
6752 		.extended_capabilities_len = sizeof(ath12k_if_types_ext_capa),
6753 	}, {
6754 		.iftype = NL80211_IFTYPE_STATION,
6755 		.extended_capabilities = ath12k_if_types_ext_capa_sta,
6756 		.extended_capabilities_mask = ath12k_if_types_ext_capa_sta,
6757 		.extended_capabilities_len =
6758 				sizeof(ath12k_if_types_ext_capa_sta),
6759 	}, {
6760 		.iftype = NL80211_IFTYPE_AP,
6761 		.extended_capabilities = ath12k_if_types_ext_capa_ap,
6762 		.extended_capabilities_mask = ath12k_if_types_ext_capa_ap,
6763 		.extended_capabilities_len =
6764 				sizeof(ath12k_if_types_ext_capa_ap),
6765 	},
6766 };
6767 
6768 static void __ath12k_mac_unregister(struct ath12k *ar)
6769 {
6770 	cancel_work_sync(&ar->regd_update_work);
6771 
6772 	ieee80211_unregister_hw(ar->hw);
6773 
6774 	idr_for_each(&ar->txmgmt_idr, ath12k_mac_tx_mgmt_pending_free, ar);
6775 	idr_destroy(&ar->txmgmt_idr);
6776 
6777 	kfree(ar->mac.sbands[NL80211_BAND_2GHZ].channels);
6778 	kfree(ar->mac.sbands[NL80211_BAND_5GHZ].channels);
6779 	kfree(ar->mac.sbands[NL80211_BAND_6GHZ].channels);
6780 
6781 	kfree(ar->hw->wiphy->iface_combinations[0].limits);
6782 	kfree(ar->hw->wiphy->iface_combinations);
6783 
6784 	SET_IEEE80211_DEV(ar->hw, NULL);
6785 }
6786 
6787 void ath12k_mac_unregister(struct ath12k_base *ab)
6788 {
6789 	struct ath12k *ar;
6790 	struct ath12k_pdev *pdev;
6791 	int i;
6792 
6793 	for (i = 0; i < ab->num_radios; i++) {
6794 		pdev = &ab->pdevs[i];
6795 		ar = pdev->ar;
6796 		if (!ar)
6797 			continue;
6798 
6799 		__ath12k_mac_unregister(ar);
6800 	}
6801 }
6802 
6803 static int __ath12k_mac_register(struct ath12k *ar)
6804 {
6805 	struct ath12k_base *ab = ar->ab;
6806 	struct ath12k_pdev_cap *cap = &ar->pdev->cap;
6807 	static const u32 cipher_suites[] = {
6808 		WLAN_CIPHER_SUITE_TKIP,
6809 		WLAN_CIPHER_SUITE_CCMP,
6810 		WLAN_CIPHER_SUITE_AES_CMAC,
6811 		WLAN_CIPHER_SUITE_BIP_CMAC_256,
6812 		WLAN_CIPHER_SUITE_BIP_GMAC_128,
6813 		WLAN_CIPHER_SUITE_BIP_GMAC_256,
6814 		WLAN_CIPHER_SUITE_GCMP,
6815 		WLAN_CIPHER_SUITE_GCMP_256,
6816 		WLAN_CIPHER_SUITE_CCMP_256,
6817 	};
6818 	int ret;
6819 	u32 ht_cap = 0;
6820 
6821 	ath12k_pdev_caps_update(ar);
6822 
6823 	SET_IEEE80211_PERM_ADDR(ar->hw, ar->mac_addr);
6824 
6825 	SET_IEEE80211_DEV(ar->hw, ab->dev);
6826 
6827 	ret = ath12k_mac_setup_channels_rates(ar,
6828 					      cap->supported_bands);
6829 	if (ret)
6830 		goto err;
6831 
6832 	ath12k_mac_setup_ht_vht_cap(ar, cap, &ht_cap);
6833 	ath12k_mac_setup_he_cap(ar, cap);
6834 
6835 	ret = ath12k_mac_setup_iface_combinations(ar);
6836 	if (ret) {
6837 		ath12k_err(ar->ab, "failed to setup interface combinations: %d\n", ret);
6838 		goto err_free_channels;
6839 	}
6840 
6841 	ar->hw->wiphy->available_antennas_rx = cap->rx_chain_mask;
6842 	ar->hw->wiphy->available_antennas_tx = cap->tx_chain_mask;
6843 
6844 	ar->hw->wiphy->interface_modes = ab->hw_params->interface_modes;
6845 
6846 	ieee80211_hw_set(ar->hw, SIGNAL_DBM);
6847 	ieee80211_hw_set(ar->hw, SUPPORTS_PS);
6848 	ieee80211_hw_set(ar->hw, SUPPORTS_DYNAMIC_PS);
6849 	ieee80211_hw_set(ar->hw, MFP_CAPABLE);
6850 	ieee80211_hw_set(ar->hw, REPORTS_TX_ACK_STATUS);
6851 	ieee80211_hw_set(ar->hw, HAS_RATE_CONTROL);
6852 	ieee80211_hw_set(ar->hw, AP_LINK_PS);
6853 	ieee80211_hw_set(ar->hw, SPECTRUM_MGMT);
6854 	ieee80211_hw_set(ar->hw, CONNECTION_MONITOR);
6855 	ieee80211_hw_set(ar->hw, SUPPORTS_PER_STA_GTK);
6856 	ieee80211_hw_set(ar->hw, CHANCTX_STA_CSA);
6857 	ieee80211_hw_set(ar->hw, QUEUE_CONTROL);
6858 	ieee80211_hw_set(ar->hw, SUPPORTS_TX_FRAG);
6859 	ieee80211_hw_set(ar->hw, REPORTS_LOW_ACK);
6860 
6861 	if (ht_cap & WMI_HT_CAP_ENABLED) {
6862 		ieee80211_hw_set(ar->hw, AMPDU_AGGREGATION);
6863 		ieee80211_hw_set(ar->hw, TX_AMPDU_SETUP_IN_HW);
6864 		ieee80211_hw_set(ar->hw, SUPPORTS_REORDERING_BUFFER);
6865 		ieee80211_hw_set(ar->hw, SUPPORTS_AMSDU_IN_AMPDU);
6866 		ieee80211_hw_set(ar->hw, USES_RSS);
6867 	}
6868 
6869 	ar->hw->wiphy->features |= NL80211_FEATURE_STATIC_SMPS;
6870 	ar->hw->wiphy->flags |= WIPHY_FLAG_IBSS_RSN;
6871 
6872 	/* TODO: Check if HT capability advertised from firmware is different
6873 	 * for each band for a dual band capable radio. It will be tricky to
6874 	 * handle it when the ht capability different for each band.
6875 	 */
6876 	if (ht_cap & WMI_HT_CAP_DYNAMIC_SMPS)
6877 		ar->hw->wiphy->features |= NL80211_FEATURE_DYNAMIC_SMPS;
6878 
6879 	ar->hw->wiphy->max_scan_ssids = WLAN_SCAN_PARAMS_MAX_SSID;
6880 	ar->hw->wiphy->max_scan_ie_len = WLAN_SCAN_PARAMS_MAX_IE_LEN;
6881 
6882 	ar->hw->max_listen_interval = ATH12K_MAX_HW_LISTEN_INTERVAL;
6883 
6884 	ar->hw->wiphy->flags |= WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL;
6885 	ar->hw->wiphy->flags |= WIPHY_FLAG_HAS_CHANNEL_SWITCH;
6886 	ar->hw->wiphy->max_remain_on_channel_duration = 5000;
6887 
6888 	ar->hw->wiphy->flags |= WIPHY_FLAG_AP_UAPSD;
6889 	ar->hw->wiphy->features |= NL80211_FEATURE_AP_MODE_CHAN_WIDTH_CHANGE |
6890 				   NL80211_FEATURE_AP_SCAN;
6891 
6892 	ar->max_num_stations = TARGET_NUM_STATIONS;
6893 	ar->max_num_peers = TARGET_NUM_PEERS_PDEV;
6894 
6895 	ar->hw->wiphy->max_ap_assoc_sta = ar->max_num_stations;
6896 
6897 	ar->hw->queues = ATH12K_HW_MAX_QUEUES;
6898 	ar->hw->wiphy->tx_queue_len = ATH12K_QUEUE_LEN;
6899 	ar->hw->offchannel_tx_hw_queue = ATH12K_HW_MAX_QUEUES - 1;
6900 	ar->hw->max_rx_aggregation_subframes = IEEE80211_MAX_AMPDU_BUF_HE;
6901 
6902 	ar->hw->vif_data_size = sizeof(struct ath12k_vif);
6903 	ar->hw->sta_data_size = sizeof(struct ath12k_sta);
6904 
6905 	wiphy_ext_feature_set(ar->hw->wiphy, NL80211_EXT_FEATURE_CQM_RSSI_LIST);
6906 	wiphy_ext_feature_set(ar->hw->wiphy, NL80211_EXT_FEATURE_STA_TX_PWR);
6907 
6908 	ar->hw->wiphy->cipher_suites = cipher_suites;
6909 	ar->hw->wiphy->n_cipher_suites = ARRAY_SIZE(cipher_suites);
6910 
6911 	ar->hw->wiphy->iftype_ext_capab = ath12k_iftypes_ext_capa;
6912 	ar->hw->wiphy->num_iftype_ext_capab =
6913 		ARRAY_SIZE(ath12k_iftypes_ext_capa);
6914 
6915 	if (ar->supports_6ghz) {
6916 		wiphy_ext_feature_set(ar->hw->wiphy,
6917 				      NL80211_EXT_FEATURE_FILS_DISCOVERY);
6918 		wiphy_ext_feature_set(ar->hw->wiphy,
6919 				      NL80211_EXT_FEATURE_UNSOL_BCAST_PROBE_RESP);
6920 	}
6921 
6922 	ath12k_reg_init(ar);
6923 
6924 	if (!test_bit(ATH12K_FLAG_RAW_MODE, &ab->dev_flags)) {
6925 		ar->hw->netdev_features = NETIF_F_HW_CSUM;
6926 		ieee80211_hw_set(ar->hw, SW_CRYPTO_CONTROL);
6927 		ieee80211_hw_set(ar->hw, SUPPORT_FAST_XMIT);
6928 	}
6929 
6930 	ret = ieee80211_register_hw(ar->hw);
6931 	if (ret) {
6932 		ath12k_err(ar->ab, "ieee80211 registration failed: %d\n", ret);
6933 		goto err_free_if_combs;
6934 	}
6935 
6936 	if (!ab->hw_params->supports_monitor)
6937 		/* There's a race between calling ieee80211_register_hw()
6938 		 * and here where the monitor mode is enabled for a little
6939 		 * while. But that time is so short and in practise it make
6940 		 * a difference in real life.
6941 		 */
6942 		ar->hw->wiphy->interface_modes &= ~BIT(NL80211_IFTYPE_MONITOR);
6943 
6944 	/* Apply the regd received during initialization */
6945 	ret = ath12k_regd_update(ar, true);
6946 	if (ret) {
6947 		ath12k_err(ar->ab, "ath12k regd update failed: %d\n", ret);
6948 		goto err_unregister_hw;
6949 	}
6950 
6951 	return 0;
6952 
6953 err_unregister_hw:
6954 	ieee80211_unregister_hw(ar->hw);
6955 
6956 err_free_if_combs:
6957 	kfree(ar->hw->wiphy->iface_combinations[0].limits);
6958 	kfree(ar->hw->wiphy->iface_combinations);
6959 
6960 err_free_channels:
6961 	kfree(ar->mac.sbands[NL80211_BAND_2GHZ].channels);
6962 	kfree(ar->mac.sbands[NL80211_BAND_5GHZ].channels);
6963 	kfree(ar->mac.sbands[NL80211_BAND_6GHZ].channels);
6964 
6965 err:
6966 	SET_IEEE80211_DEV(ar->hw, NULL);
6967 	return ret;
6968 }
6969 
6970 int ath12k_mac_register(struct ath12k_base *ab)
6971 {
6972 	struct ath12k *ar;
6973 	struct ath12k_pdev *pdev;
6974 	int i;
6975 	int ret;
6976 
6977 	if (test_bit(ATH12K_FLAG_REGISTERED, &ab->dev_flags))
6978 		return 0;
6979 
6980 	for (i = 0; i < ab->num_radios; i++) {
6981 		pdev = &ab->pdevs[i];
6982 		ar = pdev->ar;
6983 		if (ab->pdevs_macaddr_valid) {
6984 			ether_addr_copy(ar->mac_addr, pdev->mac_addr);
6985 		} else {
6986 			ether_addr_copy(ar->mac_addr, ab->mac_addr);
6987 			ar->mac_addr[4] += i;
6988 		}
6989 
6990 		ret = __ath12k_mac_register(ar);
6991 		if (ret)
6992 			goto err_cleanup;
6993 
6994 		idr_init(&ar->txmgmt_idr);
6995 		spin_lock_init(&ar->txmgmt_idr_lock);
6996 	}
6997 
6998 	/* Initialize channel counters frequency value in hertz */
6999 	ab->cc_freq_hz = 320000;
7000 	ab->free_vdev_map = (1LL << (ab->num_radios * TARGET_NUM_VDEVS)) - 1;
7001 
7002 	return 0;
7003 
7004 err_cleanup:
7005 	for (i = i - 1; i >= 0; i--) {
7006 		pdev = &ab->pdevs[i];
7007 		ar = pdev->ar;
7008 		__ath12k_mac_unregister(ar);
7009 	}
7010 
7011 	return ret;
7012 }
7013 
7014 int ath12k_mac_allocate(struct ath12k_base *ab)
7015 {
7016 	struct ieee80211_hw *hw;
7017 	struct ath12k *ar;
7018 	struct ath12k_pdev *pdev;
7019 	int ret;
7020 	int i;
7021 
7022 	if (test_bit(ATH12K_FLAG_REGISTERED, &ab->dev_flags))
7023 		return 0;
7024 
7025 	for (i = 0; i < ab->num_radios; i++) {
7026 		pdev = &ab->pdevs[i];
7027 		hw = ieee80211_alloc_hw(sizeof(struct ath12k), &ath12k_ops);
7028 		if (!hw) {
7029 			ath12k_warn(ab, "failed to allocate mac80211 hw device\n");
7030 			ret = -ENOMEM;
7031 			goto err_free_mac;
7032 		}
7033 
7034 		ar = hw->priv;
7035 		ar->hw = hw;
7036 		ar->ab = ab;
7037 		ar->pdev = pdev;
7038 		ar->pdev_idx = i;
7039 		ar->lmac_id = ath12k_hw_get_mac_from_pdev_id(ab->hw_params, i);
7040 
7041 		ar->wmi = &ab->wmi_ab.wmi[i];
7042 		/* FIXME: wmi[0] is already initialized during attach,
7043 		 * Should we do this again?
7044 		 */
7045 		ath12k_wmi_pdev_attach(ab, i);
7046 
7047 		ar->cfg_tx_chainmask = pdev->cap.tx_chain_mask;
7048 		ar->cfg_rx_chainmask = pdev->cap.rx_chain_mask;
7049 		ar->num_tx_chains = hweight32(pdev->cap.tx_chain_mask);
7050 		ar->num_rx_chains = hweight32(pdev->cap.rx_chain_mask);
7051 
7052 		pdev->ar = ar;
7053 		spin_lock_init(&ar->data_lock);
7054 		INIT_LIST_HEAD(&ar->arvifs);
7055 		INIT_LIST_HEAD(&ar->ppdu_stats_info);
7056 		mutex_init(&ar->conf_mutex);
7057 		init_completion(&ar->vdev_setup_done);
7058 		init_completion(&ar->vdev_delete_done);
7059 		init_completion(&ar->peer_assoc_done);
7060 		init_completion(&ar->peer_delete_done);
7061 		init_completion(&ar->install_key_done);
7062 		init_completion(&ar->bss_survey_done);
7063 		init_completion(&ar->scan.started);
7064 		init_completion(&ar->scan.completed);
7065 
7066 		INIT_DELAYED_WORK(&ar->scan.timeout, ath12k_scan_timeout_work);
7067 		INIT_WORK(&ar->regd_update_work, ath12k_regd_update_work);
7068 
7069 		INIT_WORK(&ar->wmi_mgmt_tx_work, ath12k_mgmt_over_wmi_tx_work);
7070 		skb_queue_head_init(&ar->wmi_mgmt_tx_queue);
7071 		clear_bit(ATH12K_FLAG_MONITOR_ENABLED, &ar->monitor_flags);
7072 	}
7073 
7074 	return 0;
7075 
7076 err_free_mac:
7077 	ath12k_mac_destroy(ab);
7078 
7079 	return ret;
7080 }
7081 
7082 void ath12k_mac_destroy(struct ath12k_base *ab)
7083 {
7084 	struct ath12k *ar;
7085 	struct ath12k_pdev *pdev;
7086 	int i;
7087 
7088 	for (i = 0; i < ab->num_radios; i++) {
7089 		pdev = &ab->pdevs[i];
7090 		ar = pdev->ar;
7091 		if (!ar)
7092 			continue;
7093 
7094 		ieee80211_free_hw(ar->hw);
7095 		pdev->ar = NULL;
7096 	}
7097 }
7098