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