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