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