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