xref: /openbmc/linux/drivers/net/wireless/ath/ath11k/mac.c (revision 842ed298)
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->is_started &&
3025 	    arvif->vdev_type != WMI_VDEV_TYPE_AP) {
3026 		ret = ath11k_start_vdev_delay(ar->hw, vif);
3027 		if (ret) {
3028 			ath11k_warn(ab, "failed to delay vdev start: %d\n", ret);
3029 			goto free_tx_stats;
3030 		}
3031 	}
3032 
3033 	return 0;
3034 
3035 free_tx_stats:
3036 	kfree(arsta->tx_stats);
3037 	arsta->tx_stats = NULL;
3038 free_peer:
3039 	ath11k_peer_delete(ar, arvif->vdev_id, sta->addr);
3040 free_rx_stats:
3041 	kfree(arsta->rx_stats);
3042 	arsta->rx_stats = NULL;
3043 dec_num_station:
3044 	ath11k_mac_dec_num_stations(arvif, sta);
3045 exit:
3046 	return ret;
3047 }
3048 
3049 static int ath11k_mac_op_sta_state(struct ieee80211_hw *hw,
3050 				   struct ieee80211_vif *vif,
3051 				   struct ieee80211_sta *sta,
3052 				   enum ieee80211_sta_state old_state,
3053 				   enum ieee80211_sta_state new_state)
3054 {
3055 	struct ath11k *ar = hw->priv;
3056 	struct ath11k_vif *arvif = ath11k_vif_to_arvif(vif);
3057 	struct ath11k_sta *arsta = (struct ath11k_sta *)sta->drv_priv;
3058 	struct ath11k_peer *peer;
3059 	int ret = 0;
3060 
3061 	/* cancel must be done outside the mutex to avoid deadlock */
3062 	if ((old_state == IEEE80211_STA_NONE &&
3063 	     new_state == IEEE80211_STA_NOTEXIST))
3064 		cancel_work_sync(&arsta->update_wk);
3065 
3066 	mutex_lock(&ar->conf_mutex);
3067 
3068 	if (old_state == IEEE80211_STA_NOTEXIST &&
3069 	    new_state == IEEE80211_STA_NONE) {
3070 		memset(arsta, 0, sizeof(*arsta));
3071 		arsta->arvif = arvif;
3072 		INIT_WORK(&arsta->update_wk, ath11k_sta_rc_update_wk);
3073 
3074 		ret = ath11k_mac_station_add(ar, vif, sta);
3075 		if (ret)
3076 			ath11k_warn(ar->ab, "Failed to add station: %pM for VDEV: %d\n",
3077 				    sta->addr, arvif->vdev_id);
3078 	} else if ((old_state == IEEE80211_STA_NONE &&
3079 		    new_state == IEEE80211_STA_NOTEXIST)) {
3080 		ath11k_dp_peer_cleanup(ar, arvif->vdev_id, sta->addr);
3081 
3082 		ret = ath11k_peer_delete(ar, arvif->vdev_id, sta->addr);
3083 		if (ret)
3084 			ath11k_warn(ar->ab, "Failed to delete peer: %pM for VDEV: %d\n",
3085 				    sta->addr, arvif->vdev_id);
3086 		else
3087 			ath11k_dbg(ar->ab, ATH11K_DBG_MAC, "Removed peer: %pM for VDEV: %d\n",
3088 				   sta->addr, arvif->vdev_id);
3089 
3090 		ath11k_mac_dec_num_stations(arvif, sta);
3091 		spin_lock_bh(&ar->ab->base_lock);
3092 		peer = ath11k_peer_find(ar->ab, arvif->vdev_id, sta->addr);
3093 		if (peer && peer->sta == sta) {
3094 			ath11k_warn(ar->ab, "Found peer entry %pM n vdev %i after it was supposedly removed\n",
3095 				    vif->addr, arvif->vdev_id);
3096 			peer->sta = NULL;
3097 			list_del(&peer->list);
3098 			kfree(peer);
3099 			ar->num_peers--;
3100 		}
3101 		spin_unlock_bh(&ar->ab->base_lock);
3102 
3103 		kfree(arsta->tx_stats);
3104 		arsta->tx_stats = NULL;
3105 
3106 		kfree(arsta->rx_stats);
3107 		arsta->rx_stats = NULL;
3108 	} else if (old_state == IEEE80211_STA_AUTH &&
3109 		   new_state == IEEE80211_STA_ASSOC &&
3110 		   (vif->type == NL80211_IFTYPE_AP ||
3111 		    vif->type == NL80211_IFTYPE_MESH_POINT ||
3112 		    vif->type == NL80211_IFTYPE_ADHOC)) {
3113 		ret = ath11k_station_assoc(ar, vif, sta, false);
3114 		if (ret)
3115 			ath11k_warn(ar->ab, "Failed to associate station: %pM\n",
3116 				    sta->addr);
3117 	} else if (old_state == IEEE80211_STA_ASSOC &&
3118 		   new_state == IEEE80211_STA_AUTH &&
3119 		   (vif->type == NL80211_IFTYPE_AP ||
3120 		    vif->type == NL80211_IFTYPE_MESH_POINT ||
3121 		    vif->type == NL80211_IFTYPE_ADHOC)) {
3122 		ret = ath11k_station_disassoc(ar, vif, sta);
3123 		if (ret)
3124 			ath11k_warn(ar->ab, "Failed to disassociate station: %pM\n",
3125 				    sta->addr);
3126 	}
3127 
3128 	mutex_unlock(&ar->conf_mutex);
3129 	return ret;
3130 }
3131 
3132 static int ath11k_mac_op_sta_set_txpwr(struct ieee80211_hw *hw,
3133 				       struct ieee80211_vif *vif,
3134 				       struct ieee80211_sta *sta)
3135 {
3136 	struct ath11k *ar = hw->priv;
3137 	struct ath11k_vif *arvif = (void *)vif->drv_priv;
3138 	int ret = 0;
3139 	s16 txpwr;
3140 
3141 	if (sta->txpwr.type == NL80211_TX_POWER_AUTOMATIC) {
3142 		txpwr = 0;
3143 	} else {
3144 		txpwr = sta->txpwr.power;
3145 		if (!txpwr)
3146 			return -EINVAL;
3147 	}
3148 
3149 	if (txpwr > ATH11K_TX_POWER_MAX_VAL || txpwr < ATH11K_TX_POWER_MIN_VAL)
3150 		return -EINVAL;
3151 
3152 	mutex_lock(&ar->conf_mutex);
3153 
3154 	ret = ath11k_wmi_set_peer_param(ar, sta->addr, arvif->vdev_id,
3155 					WMI_PEER_USE_FIXED_PWR, txpwr);
3156 	if (ret) {
3157 		ath11k_warn(ar->ab, "failed to set tx power for station ret: %d\n",
3158 			    ret);
3159 		goto out;
3160 	}
3161 
3162 out:
3163 	mutex_unlock(&ar->conf_mutex);
3164 	return ret;
3165 }
3166 
3167 static void ath11k_mac_op_sta_rc_update(struct ieee80211_hw *hw,
3168 					struct ieee80211_vif *vif,
3169 					struct ieee80211_sta *sta,
3170 					u32 changed)
3171 {
3172 	struct ath11k *ar = hw->priv;
3173 	struct ath11k_sta *arsta = (struct ath11k_sta *)sta->drv_priv;
3174 	struct ath11k_vif *arvif = (void *)vif->drv_priv;
3175 	struct ath11k_peer *peer;
3176 	u32 bw, smps;
3177 
3178 	spin_lock_bh(&ar->ab->base_lock);
3179 
3180 	peer = ath11k_peer_find(ar->ab, arvif->vdev_id, sta->addr);
3181 	if (!peer) {
3182 		spin_unlock_bh(&ar->ab->base_lock);
3183 		ath11k_warn(ar->ab, "mac sta rc update failed to find peer %pM on vdev %i\n",
3184 			    sta->addr, arvif->vdev_id);
3185 		return;
3186 	}
3187 
3188 	spin_unlock_bh(&ar->ab->base_lock);
3189 
3190 	ath11k_dbg(ar->ab, ATH11K_DBG_MAC,
3191 		   "mac sta rc update for %pM changed %08x bw %d nss %d smps %d\n",
3192 		   sta->addr, changed, sta->bandwidth, sta->rx_nss,
3193 		   sta->smps_mode);
3194 
3195 	spin_lock_bh(&ar->data_lock);
3196 
3197 	if (changed & IEEE80211_RC_BW_CHANGED) {
3198 		bw = WMI_PEER_CHWIDTH_20MHZ;
3199 
3200 		switch (sta->bandwidth) {
3201 		case IEEE80211_STA_RX_BW_20:
3202 			bw = WMI_PEER_CHWIDTH_20MHZ;
3203 			break;
3204 		case IEEE80211_STA_RX_BW_40:
3205 			bw = WMI_PEER_CHWIDTH_40MHZ;
3206 			break;
3207 		case IEEE80211_STA_RX_BW_80:
3208 			bw = WMI_PEER_CHWIDTH_80MHZ;
3209 			break;
3210 		case IEEE80211_STA_RX_BW_160:
3211 			bw = WMI_PEER_CHWIDTH_160MHZ;
3212 			break;
3213 		default:
3214 			ath11k_warn(ar->ab, "Invalid bandwidth %d in rc update for %pM\n",
3215 				    sta->bandwidth, sta->addr);
3216 			bw = WMI_PEER_CHWIDTH_20MHZ;
3217 			break;
3218 		}
3219 
3220 		arsta->bw = bw;
3221 	}
3222 
3223 	if (changed & IEEE80211_RC_NSS_CHANGED)
3224 		arsta->nss = sta->rx_nss;
3225 
3226 	if (changed & IEEE80211_RC_SMPS_CHANGED) {
3227 		smps = WMI_PEER_SMPS_PS_NONE;
3228 
3229 		switch (sta->smps_mode) {
3230 		case IEEE80211_SMPS_AUTOMATIC:
3231 		case IEEE80211_SMPS_OFF:
3232 			smps = WMI_PEER_SMPS_PS_NONE;
3233 			break;
3234 		case IEEE80211_SMPS_STATIC:
3235 			smps = WMI_PEER_SMPS_STATIC;
3236 			break;
3237 		case IEEE80211_SMPS_DYNAMIC:
3238 			smps = WMI_PEER_SMPS_DYNAMIC;
3239 			break;
3240 		default:
3241 			ath11k_warn(ar->ab, "Invalid smps %d in sta rc update for %pM\n",
3242 				    sta->smps_mode, sta->addr);
3243 			smps = WMI_PEER_SMPS_PS_NONE;
3244 			break;
3245 		}
3246 
3247 		arsta->smps = smps;
3248 	}
3249 
3250 	arsta->changed |= changed;
3251 
3252 	spin_unlock_bh(&ar->data_lock);
3253 
3254 	ieee80211_queue_work(hw, &arsta->update_wk);
3255 }
3256 
3257 static int ath11k_conf_tx_uapsd(struct ath11k *ar, struct ieee80211_vif *vif,
3258 				u16 ac, bool enable)
3259 {
3260 	struct ath11k_vif *arvif = ath11k_vif_to_arvif(vif);
3261 	u32 value = 0;
3262 	int ret = 0;
3263 
3264 	if (arvif->vdev_type != WMI_VDEV_TYPE_STA)
3265 		return 0;
3266 
3267 	switch (ac) {
3268 	case IEEE80211_AC_VO:
3269 		value = WMI_STA_PS_UAPSD_AC3_DELIVERY_EN |
3270 			WMI_STA_PS_UAPSD_AC3_TRIGGER_EN;
3271 		break;
3272 	case IEEE80211_AC_VI:
3273 		value = WMI_STA_PS_UAPSD_AC2_DELIVERY_EN |
3274 			WMI_STA_PS_UAPSD_AC2_TRIGGER_EN;
3275 		break;
3276 	case IEEE80211_AC_BE:
3277 		value = WMI_STA_PS_UAPSD_AC1_DELIVERY_EN |
3278 			WMI_STA_PS_UAPSD_AC1_TRIGGER_EN;
3279 		break;
3280 	case IEEE80211_AC_BK:
3281 		value = WMI_STA_PS_UAPSD_AC0_DELIVERY_EN |
3282 			WMI_STA_PS_UAPSD_AC0_TRIGGER_EN;
3283 		break;
3284 	}
3285 
3286 	if (enable)
3287 		arvif->u.sta.uapsd |= value;
3288 	else
3289 		arvif->u.sta.uapsd &= ~value;
3290 
3291 	ret = ath11k_wmi_set_sta_ps_param(ar, arvif->vdev_id,
3292 					  WMI_STA_PS_PARAM_UAPSD,
3293 					  arvif->u.sta.uapsd);
3294 	if (ret) {
3295 		ath11k_warn(ar->ab, "could not set uapsd params %d\n", ret);
3296 		goto exit;
3297 	}
3298 
3299 	if (arvif->u.sta.uapsd)
3300 		value = WMI_STA_PS_RX_WAKE_POLICY_POLL_UAPSD;
3301 	else
3302 		value = WMI_STA_PS_RX_WAKE_POLICY_WAKE;
3303 
3304 	ret = ath11k_wmi_set_sta_ps_param(ar, arvif->vdev_id,
3305 					  WMI_STA_PS_PARAM_RX_WAKE_POLICY,
3306 					  value);
3307 	if (ret)
3308 		ath11k_warn(ar->ab, "could not set rx wake param %d\n", ret);
3309 
3310 exit:
3311 	return ret;
3312 }
3313 
3314 static int ath11k_mac_op_conf_tx(struct ieee80211_hw *hw,
3315 				 struct ieee80211_vif *vif, u16 ac,
3316 				 const struct ieee80211_tx_queue_params *params)
3317 {
3318 	struct ath11k *ar = hw->priv;
3319 	struct ath11k_vif *arvif = (void *)vif->drv_priv;
3320 	struct wmi_wmm_params_arg *p = NULL;
3321 	int ret;
3322 
3323 	mutex_lock(&ar->conf_mutex);
3324 
3325 	switch (ac) {
3326 	case IEEE80211_AC_VO:
3327 		p = &arvif->wmm_params.ac_vo;
3328 		break;
3329 	case IEEE80211_AC_VI:
3330 		p = &arvif->wmm_params.ac_vi;
3331 		break;
3332 	case IEEE80211_AC_BE:
3333 		p = &arvif->wmm_params.ac_be;
3334 		break;
3335 	case IEEE80211_AC_BK:
3336 		p = &arvif->wmm_params.ac_bk;
3337 		break;
3338 	}
3339 
3340 	if (WARN_ON(!p)) {
3341 		ret = -EINVAL;
3342 		goto exit;
3343 	}
3344 
3345 	p->cwmin = params->cw_min;
3346 	p->cwmax = params->cw_max;
3347 	p->aifs = params->aifs;
3348 	p->txop = params->txop;
3349 
3350 	ret = ath11k_wmi_send_wmm_update_cmd_tlv(ar, arvif->vdev_id,
3351 						 &arvif->wmm_params);
3352 	if (ret) {
3353 		ath11k_warn(ar->ab, "failed to set wmm params: %d\n", ret);
3354 		goto exit;
3355 	}
3356 
3357 	ret = ath11k_conf_tx_uapsd(ar, vif, ac, params->uapsd);
3358 
3359 	if (ret)
3360 		ath11k_warn(ar->ab, "failed to set sta uapsd: %d\n", ret);
3361 
3362 exit:
3363 	mutex_unlock(&ar->conf_mutex);
3364 	return ret;
3365 }
3366 
3367 static struct ieee80211_sta_ht_cap
3368 ath11k_create_ht_cap(struct ath11k *ar, u32 ar_ht_cap, u32 rate_cap_rx_chainmask)
3369 {
3370 	int i;
3371 	struct ieee80211_sta_ht_cap ht_cap = {0};
3372 	u32 ar_vht_cap = ar->pdev->cap.vht_cap;
3373 
3374 	if (!(ar_ht_cap & WMI_HT_CAP_ENABLED))
3375 		return ht_cap;
3376 
3377 	ht_cap.ht_supported = 1;
3378 	ht_cap.ampdu_factor = IEEE80211_HT_MAX_AMPDU_64K;
3379 	ht_cap.ampdu_density = IEEE80211_HT_MPDU_DENSITY_NONE;
3380 	ht_cap.cap |= IEEE80211_HT_CAP_SUP_WIDTH_20_40;
3381 	ht_cap.cap |= IEEE80211_HT_CAP_DSSSCCK40;
3382 	ht_cap.cap |= WLAN_HT_CAP_SM_PS_STATIC << IEEE80211_HT_CAP_SM_PS_SHIFT;
3383 
3384 	if (ar_ht_cap & WMI_HT_CAP_HT20_SGI)
3385 		ht_cap.cap |= IEEE80211_HT_CAP_SGI_20;
3386 
3387 	if (ar_ht_cap & WMI_HT_CAP_HT40_SGI)
3388 		ht_cap.cap |= IEEE80211_HT_CAP_SGI_40;
3389 
3390 	if (ar_ht_cap & WMI_HT_CAP_DYNAMIC_SMPS) {
3391 		u32 smps;
3392 
3393 		smps   = WLAN_HT_CAP_SM_PS_DYNAMIC;
3394 		smps <<= IEEE80211_HT_CAP_SM_PS_SHIFT;
3395 
3396 		ht_cap.cap |= smps;
3397 	}
3398 
3399 	if (ar_ht_cap & WMI_HT_CAP_TX_STBC)
3400 		ht_cap.cap |= IEEE80211_HT_CAP_TX_STBC;
3401 
3402 	if (ar_ht_cap & WMI_HT_CAP_RX_STBC) {
3403 		u32 stbc;
3404 
3405 		stbc   = ar_ht_cap;
3406 		stbc  &= WMI_HT_CAP_RX_STBC;
3407 		stbc >>= WMI_HT_CAP_RX_STBC_MASK_SHIFT;
3408 		stbc <<= IEEE80211_HT_CAP_RX_STBC_SHIFT;
3409 		stbc  &= IEEE80211_HT_CAP_RX_STBC;
3410 
3411 		ht_cap.cap |= stbc;
3412 	}
3413 
3414 	if (ar_ht_cap & WMI_HT_CAP_RX_LDPC)
3415 		ht_cap.cap |= IEEE80211_HT_CAP_LDPC_CODING;
3416 
3417 	if (ar_ht_cap & WMI_HT_CAP_L_SIG_TXOP_PROT)
3418 		ht_cap.cap |= IEEE80211_HT_CAP_LSIG_TXOP_PROT;
3419 
3420 	if (ar_vht_cap & WMI_VHT_CAP_MAX_MPDU_LEN_MASK)
3421 		ht_cap.cap |= IEEE80211_HT_CAP_MAX_AMSDU;
3422 
3423 	for (i = 0; i < ar->num_rx_chains; i++) {
3424 		if (rate_cap_rx_chainmask & BIT(i))
3425 			ht_cap.mcs.rx_mask[i] = 0xFF;
3426 	}
3427 
3428 	ht_cap.mcs.tx_params |= IEEE80211_HT_MCS_TX_DEFINED;
3429 
3430 	return ht_cap;
3431 }
3432 
3433 static int ath11k_mac_set_txbf_conf(struct ath11k_vif *arvif)
3434 {
3435 	u32 value = 0;
3436 	struct ath11k *ar = arvif->ar;
3437 	int nsts;
3438 	int sound_dim;
3439 	u32 vht_cap = ar->pdev->cap.vht_cap;
3440 	u32 vdev_param = WMI_VDEV_PARAM_TXBF;
3441 
3442 	if (vht_cap & (IEEE80211_VHT_CAP_SU_BEAMFORMEE_CAPABLE)) {
3443 		nsts = vht_cap & IEEE80211_VHT_CAP_BEAMFORMEE_STS_MASK;
3444 		nsts >>= IEEE80211_VHT_CAP_BEAMFORMEE_STS_SHIFT;
3445 		value |= SM(nsts, WMI_TXBF_STS_CAP_OFFSET);
3446 	}
3447 
3448 	if (vht_cap & (IEEE80211_VHT_CAP_SU_BEAMFORMER_CAPABLE)) {
3449 		sound_dim = vht_cap &
3450 			    IEEE80211_VHT_CAP_SOUNDING_DIMENSIONS_MASK;
3451 		sound_dim >>= IEEE80211_VHT_CAP_SOUNDING_DIMENSIONS_SHIFT;
3452 		if (sound_dim > (ar->num_tx_chains - 1))
3453 			sound_dim = ar->num_tx_chains - 1;
3454 		value |= SM(sound_dim, WMI_BF_SOUND_DIM_OFFSET);
3455 	}
3456 
3457 	if (!value)
3458 		return 0;
3459 
3460 	if (vht_cap & IEEE80211_VHT_CAP_SU_BEAMFORMER_CAPABLE) {
3461 		value |= WMI_VDEV_PARAM_TXBF_SU_TX_BFER;
3462 
3463 		if ((vht_cap & IEEE80211_VHT_CAP_MU_BEAMFORMER_CAPABLE) &&
3464 		    arvif->vdev_type == WMI_VDEV_TYPE_AP)
3465 			value |= WMI_VDEV_PARAM_TXBF_MU_TX_BFER;
3466 	}
3467 
3468 	/* TODO: SUBFEE not validated in HK, disable here until validated? */
3469 
3470 	if (vht_cap & IEEE80211_VHT_CAP_SU_BEAMFORMEE_CAPABLE) {
3471 		value |= WMI_VDEV_PARAM_TXBF_SU_TX_BFEE;
3472 
3473 		if ((vht_cap & IEEE80211_VHT_CAP_MU_BEAMFORMEE_CAPABLE) &&
3474 		    arvif->vdev_type == WMI_VDEV_TYPE_STA)
3475 			value |= WMI_VDEV_PARAM_TXBF_MU_TX_BFEE;
3476 	}
3477 
3478 	return ath11k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
3479 					     vdev_param, value);
3480 }
3481 
3482 static void ath11k_set_vht_txbf_cap(struct ath11k *ar, u32 *vht_cap)
3483 {
3484 	bool subfer, subfee;
3485 	int sound_dim = 0;
3486 
3487 	subfer = !!(*vht_cap & (IEEE80211_VHT_CAP_SU_BEAMFORMER_CAPABLE));
3488 	subfee = !!(*vht_cap & (IEEE80211_VHT_CAP_SU_BEAMFORMEE_CAPABLE));
3489 
3490 	if (ar->num_tx_chains < 2) {
3491 		*vht_cap &= ~(IEEE80211_VHT_CAP_SU_BEAMFORMER_CAPABLE);
3492 		subfer = false;
3493 	}
3494 
3495 	/* If SU Beaformer is not set, then disable MU Beamformer Capability */
3496 	if (!subfer)
3497 		*vht_cap &= ~(IEEE80211_VHT_CAP_MU_BEAMFORMER_CAPABLE);
3498 
3499 	/* If SU Beaformee is not set, then disable MU Beamformee Capability */
3500 	if (!subfee)
3501 		*vht_cap &= ~(IEEE80211_VHT_CAP_MU_BEAMFORMEE_CAPABLE);
3502 
3503 	sound_dim = (*vht_cap & IEEE80211_VHT_CAP_SOUNDING_DIMENSIONS_MASK);
3504 	sound_dim >>= IEEE80211_VHT_CAP_SOUNDING_DIMENSIONS_SHIFT;
3505 	*vht_cap &= ~IEEE80211_VHT_CAP_SOUNDING_DIMENSIONS_MASK;
3506 
3507 	/* TODO: Need to check invalid STS and Sound_dim values set by FW? */
3508 
3509 	/* Enable Sounding Dimension Field only if SU BF is enabled */
3510 	if (subfer) {
3511 		if (sound_dim > (ar->num_tx_chains - 1))
3512 			sound_dim = ar->num_tx_chains - 1;
3513 
3514 		sound_dim <<= IEEE80211_VHT_CAP_SOUNDING_DIMENSIONS_SHIFT;
3515 		sound_dim &=  IEEE80211_VHT_CAP_SOUNDING_DIMENSIONS_MASK;
3516 		*vht_cap |= sound_dim;
3517 	}
3518 
3519 	/* Use the STS advertised by FW unless SU Beamformee is not supported*/
3520 	if (!subfee)
3521 		*vht_cap &= ~(IEEE80211_VHT_CAP_BEAMFORMEE_STS_MASK);
3522 }
3523 
3524 static struct ieee80211_sta_vht_cap
3525 ath11k_create_vht_cap(struct ath11k *ar, u32 rate_cap_tx_chainmask,
3526 		      u32 rate_cap_rx_chainmask)
3527 {
3528 	struct ieee80211_sta_vht_cap vht_cap = {0};
3529 	u16 txmcs_map, rxmcs_map;
3530 	int i;
3531 
3532 	vht_cap.vht_supported = 1;
3533 	vht_cap.cap = ar->pdev->cap.vht_cap;
3534 
3535 	ath11k_set_vht_txbf_cap(ar, &vht_cap.cap);
3536 
3537 	/* TODO: Enable back VHT160 mode once association issues are fixed */
3538 	/* Disabling VHT160 and VHT80+80 modes */
3539 	vht_cap.cap &= ~IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_MASK;
3540 	vht_cap.cap &= ~IEEE80211_VHT_CAP_SHORT_GI_160;
3541 
3542 	rxmcs_map = 0;
3543 	txmcs_map = 0;
3544 	for (i = 0; i < 8; i++) {
3545 		if (i < ar->num_tx_chains && rate_cap_tx_chainmask & BIT(i))
3546 			txmcs_map |= IEEE80211_VHT_MCS_SUPPORT_0_9 << (i * 2);
3547 		else
3548 			txmcs_map |= IEEE80211_VHT_MCS_NOT_SUPPORTED << (i * 2);
3549 
3550 		if (i < ar->num_rx_chains && rate_cap_rx_chainmask & BIT(i))
3551 			rxmcs_map |= IEEE80211_VHT_MCS_SUPPORT_0_9 << (i * 2);
3552 		else
3553 			rxmcs_map |= IEEE80211_VHT_MCS_NOT_SUPPORTED << (i * 2);
3554 	}
3555 
3556 	if (rate_cap_tx_chainmask <= 1)
3557 		vht_cap.cap &= ~IEEE80211_VHT_CAP_TXSTBC;
3558 
3559 	vht_cap.vht_mcs.rx_mcs_map = cpu_to_le16(rxmcs_map);
3560 	vht_cap.vht_mcs.tx_mcs_map = cpu_to_le16(txmcs_map);
3561 
3562 	return vht_cap;
3563 }
3564 
3565 static void ath11k_mac_setup_ht_vht_cap(struct ath11k *ar,
3566 					struct ath11k_pdev_cap *cap,
3567 					u32 *ht_cap_info)
3568 {
3569 	struct ieee80211_supported_band *band;
3570 	u32 rate_cap_tx_chainmask;
3571 	u32 rate_cap_rx_chainmask;
3572 	u32 ht_cap;
3573 
3574 	rate_cap_tx_chainmask = ar->cfg_tx_chainmask >> cap->tx_chain_mask_shift;
3575 	rate_cap_rx_chainmask = ar->cfg_rx_chainmask >> cap->rx_chain_mask_shift;
3576 
3577 	if (cap->supported_bands & WMI_HOST_WLAN_2G_CAP) {
3578 		band = &ar->mac.sbands[NL80211_BAND_2GHZ];
3579 		ht_cap = cap->band[NL80211_BAND_2GHZ].ht_cap_info;
3580 		if (ht_cap_info)
3581 			*ht_cap_info = ht_cap;
3582 		band->ht_cap = ath11k_create_ht_cap(ar, ht_cap,
3583 						    rate_cap_rx_chainmask);
3584 	}
3585 
3586 	if (cap->supported_bands & WMI_HOST_WLAN_5G_CAP && !ar->supports_6ghz) {
3587 		band = &ar->mac.sbands[NL80211_BAND_5GHZ];
3588 		ht_cap = cap->band[NL80211_BAND_5GHZ].ht_cap_info;
3589 		if (ht_cap_info)
3590 			*ht_cap_info = ht_cap;
3591 		band->ht_cap = ath11k_create_ht_cap(ar, ht_cap,
3592 						    rate_cap_rx_chainmask);
3593 		band->vht_cap = ath11k_create_vht_cap(ar, rate_cap_tx_chainmask,
3594 						      rate_cap_rx_chainmask);
3595 	}
3596 }
3597 
3598 static int ath11k_check_chain_mask(struct ath11k *ar, u32 ant, bool is_tx_ant)
3599 {
3600 	/* TODO: Check the request chainmask against the supported
3601 	 * chainmask table which is advertised in extented_service_ready event
3602 	 */
3603 
3604 	return 0;
3605 }
3606 
3607 static void ath11k_gen_ppe_thresh(struct ath11k_ppe_threshold *fw_ppet,
3608 				  u8 *he_ppet)
3609 {
3610 	int nss, ru;
3611 	u8 bit = 7;
3612 
3613 	he_ppet[0] = fw_ppet->numss_m1 & IEEE80211_PPE_THRES_NSS_MASK;
3614 	he_ppet[0] |= (fw_ppet->ru_bit_mask <<
3615 		       IEEE80211_PPE_THRES_RU_INDEX_BITMASK_POS) &
3616 		      IEEE80211_PPE_THRES_RU_INDEX_BITMASK_MASK;
3617 	for (nss = 0; nss <= fw_ppet->numss_m1; nss++) {
3618 		for (ru = 0; ru < 4; ru++) {
3619 			u8 val;
3620 			int i;
3621 
3622 			if ((fw_ppet->ru_bit_mask & BIT(ru)) == 0)
3623 				continue;
3624 			val = (fw_ppet->ppet16_ppet8_ru3_ru0[nss] >> (ru * 6)) &
3625 			       0x3f;
3626 			val = ((val >> 3) & 0x7) | ((val & 0x7) << 3);
3627 			for (i = 5; i >= 0; i--) {
3628 				he_ppet[bit / 8] |=
3629 					((val >> i) & 0x1) << ((bit % 8));
3630 				bit++;
3631 			}
3632 		}
3633 	}
3634 }
3635 
3636 static void
3637 ath11k_mac_filter_he_cap_mesh(struct ieee80211_he_cap_elem *he_cap_elem)
3638 {
3639 	u8 m;
3640 
3641 	m = IEEE80211_HE_MAC_CAP0_TWT_RES |
3642 	    IEEE80211_HE_MAC_CAP0_TWT_REQ;
3643 	he_cap_elem->mac_cap_info[0] &= ~m;
3644 
3645 	m = IEEE80211_HE_MAC_CAP2_TRS |
3646 	    IEEE80211_HE_MAC_CAP2_BCAST_TWT |
3647 	    IEEE80211_HE_MAC_CAP2_MU_CASCADING;
3648 	he_cap_elem->mac_cap_info[2] &= ~m;
3649 
3650 	m = IEEE80211_HE_MAC_CAP3_FLEX_TWT_SCHED |
3651 	    IEEE80211_HE_MAC_CAP2_BCAST_TWT |
3652 	    IEEE80211_HE_MAC_CAP2_MU_CASCADING;
3653 	he_cap_elem->mac_cap_info[3] &= ~m;
3654 
3655 	m = IEEE80211_HE_MAC_CAP4_BSRP_BQRP_A_MPDU_AGG |
3656 	    IEEE80211_HE_MAC_CAP4_BQR;
3657 	he_cap_elem->mac_cap_info[4] &= ~m;
3658 
3659 	m = IEEE80211_HE_MAC_CAP5_SUBCHAN_SELECVITE_TRANSMISSION |
3660 	    IEEE80211_HE_MAC_CAP5_UL_2x996_TONE_RU |
3661 	    IEEE80211_HE_MAC_CAP5_PUNCTURED_SOUNDING |
3662 	    IEEE80211_HE_MAC_CAP5_HT_VHT_TRIG_FRAME_RX;
3663 	he_cap_elem->mac_cap_info[5] &= ~m;
3664 
3665 	m = IEEE80211_HE_PHY_CAP2_UL_MU_FULL_MU_MIMO |
3666 	    IEEE80211_HE_PHY_CAP2_UL_MU_PARTIAL_MU_MIMO;
3667 	he_cap_elem->phy_cap_info[2] &= ~m;
3668 
3669 	m = IEEE80211_HE_PHY_CAP3_RX_HE_MU_PPDU_FROM_NON_AP_STA |
3670 	    IEEE80211_HE_PHY_CAP3_DCM_MAX_CONST_TX_MASK |
3671 	    IEEE80211_HE_PHY_CAP3_DCM_MAX_CONST_RX_MASK;
3672 	he_cap_elem->phy_cap_info[3] &= ~m;
3673 
3674 	m = IEEE80211_HE_PHY_CAP4_MU_BEAMFORMER;
3675 	he_cap_elem->phy_cap_info[4] &= ~m;
3676 
3677 	m = IEEE80211_HE_PHY_CAP5_NG16_MU_FEEDBACK;
3678 	he_cap_elem->phy_cap_info[5] &= ~m;
3679 
3680 	m = IEEE80211_HE_PHY_CAP6_CODEBOOK_SIZE_75_MU |
3681 	    IEEE80211_HE_PHY_CAP6_TRIG_MU_BEAMFORMER_FB |
3682 	    IEEE80211_HE_PHY_CAP6_TRIG_CQI_FB |
3683 	    IEEE80211_HE_PHY_CAP6_PARTIAL_BANDWIDTH_DL_MUMIMO;
3684 	he_cap_elem->phy_cap_info[6] &= ~m;
3685 
3686 	m = IEEE80211_HE_PHY_CAP7_SRP_BASED_SR |
3687 	    IEEE80211_HE_PHY_CAP7_POWER_BOOST_FACTOR_AR |
3688 	    IEEE80211_HE_PHY_CAP7_STBC_TX_ABOVE_80MHZ |
3689 	    IEEE80211_HE_PHY_CAP7_STBC_RX_ABOVE_80MHZ;
3690 	he_cap_elem->phy_cap_info[7] &= ~m;
3691 
3692 	m = IEEE80211_HE_PHY_CAP8_HE_ER_SU_PPDU_4XLTF_AND_08_US_GI |
3693 	    IEEE80211_HE_PHY_CAP8_20MHZ_IN_40MHZ_HE_PPDU_IN_2G |
3694 	    IEEE80211_HE_PHY_CAP8_20MHZ_IN_160MHZ_HE_PPDU |
3695 	    IEEE80211_HE_PHY_CAP8_80MHZ_IN_160MHZ_HE_PPDU;
3696 	he_cap_elem->phy_cap_info[8] &= ~m;
3697 
3698 	m = IEEE80211_HE_PHY_CAP9_LONGER_THAN_16_SIGB_OFDM_SYM |
3699 	    IEEE80211_HE_PHY_CAP9_NON_TRIGGERED_CQI_FEEDBACK |
3700 	    IEEE80211_HE_PHY_CAP9_RX_1024_QAM_LESS_THAN_242_TONE_RU |
3701 	    IEEE80211_HE_PHY_CAP9_TX_1024_QAM_LESS_THAN_242_TONE_RU |
3702 	    IEEE80211_HE_PHY_CAP9_RX_FULL_BW_SU_USING_MU_WITH_COMP_SIGB |
3703 	    IEEE80211_HE_PHY_CAP9_RX_FULL_BW_SU_USING_MU_WITH_NON_COMP_SIGB;
3704 	he_cap_elem->phy_cap_info[9] &= ~m;
3705 }
3706 
3707 static __le16 ath11k_mac_setup_he_6ghz_cap(struct ath11k_pdev_cap *pcap,
3708 					   struct ath11k_band_cap *bcap)
3709 {
3710 	u8 val;
3711 
3712 	bcap->he_6ghz_capa = IEEE80211_HT_MPDU_DENSITY_NONE;
3713 	if (bcap->ht_cap_info & WMI_HT_CAP_DYNAMIC_SMPS)
3714 		bcap->he_6ghz_capa |=
3715 			FIELD_PREP(IEEE80211_HE_6GHZ_CAP_SM_PS,
3716 				   WLAN_HT_CAP_SM_PS_DYNAMIC);
3717 	else
3718 		bcap->he_6ghz_capa |=
3719 			FIELD_PREP(IEEE80211_HE_6GHZ_CAP_SM_PS,
3720 				   WLAN_HT_CAP_SM_PS_DISABLED);
3721 	val = FIELD_GET(IEEE80211_VHT_CAP_MAX_A_MPDU_LENGTH_EXPONENT_MASK,
3722 			pcap->vht_cap);
3723 	bcap->he_6ghz_capa |=
3724 		FIELD_PREP(IEEE80211_HE_6GHZ_CAP_MAX_AMPDU_LEN_EXP, val);
3725 	val = FIELD_GET(IEEE80211_VHT_CAP_MAX_MPDU_MASK, pcap->vht_cap);
3726 	bcap->he_6ghz_capa |=
3727 		FIELD_PREP(IEEE80211_HE_6GHZ_CAP_MAX_MPDU_LEN, val);
3728 	if (pcap->vht_cap & IEEE80211_VHT_CAP_RX_ANTENNA_PATTERN)
3729 		bcap->he_6ghz_capa |= IEEE80211_HE_6GHZ_CAP_RX_ANTPAT_CONS;
3730 	if (pcap->vht_cap & IEEE80211_VHT_CAP_TX_ANTENNA_PATTERN)
3731 		bcap->he_6ghz_capa |= IEEE80211_HE_6GHZ_CAP_TX_ANTPAT_CONS;
3732 
3733 	return cpu_to_le16(bcap->he_6ghz_capa);
3734 }
3735 
3736 static int ath11k_mac_copy_he_cap(struct ath11k *ar,
3737 				  struct ath11k_pdev_cap *cap,
3738 				  struct ieee80211_sband_iftype_data *data,
3739 				  int band)
3740 {
3741 	int i, idx = 0;
3742 
3743 	for (i = 0; i < NUM_NL80211_IFTYPES; i++) {
3744 		struct ieee80211_sta_he_cap *he_cap = &data[idx].he_cap;
3745 		struct ath11k_band_cap *band_cap = &cap->band[band];
3746 		struct ieee80211_he_cap_elem *he_cap_elem =
3747 				&he_cap->he_cap_elem;
3748 
3749 		switch (i) {
3750 		case NL80211_IFTYPE_STATION:
3751 		case NL80211_IFTYPE_AP:
3752 		case NL80211_IFTYPE_MESH_POINT:
3753 			break;
3754 
3755 		default:
3756 			continue;
3757 		}
3758 
3759 		data[idx].types_mask = BIT(i);
3760 		he_cap->has_he = true;
3761 		memcpy(he_cap_elem->mac_cap_info, band_cap->he_cap_info,
3762 		       sizeof(he_cap_elem->mac_cap_info));
3763 		memcpy(he_cap_elem->phy_cap_info, band_cap->he_cap_phy_info,
3764 		       sizeof(he_cap_elem->phy_cap_info));
3765 
3766 		he_cap_elem->mac_cap_info[1] &=
3767 			IEEE80211_HE_MAC_CAP1_TF_MAC_PAD_DUR_MASK;
3768 
3769 		he_cap_elem->phy_cap_info[5] &=
3770 			~IEEE80211_HE_PHY_CAP5_BEAMFORMEE_NUM_SND_DIM_UNDER_80MHZ_MASK;
3771 		he_cap_elem->phy_cap_info[5] &=
3772 			~IEEE80211_HE_PHY_CAP5_BEAMFORMEE_NUM_SND_DIM_ABOVE_80MHZ_MASK;
3773 		he_cap_elem->phy_cap_info[5] |= ar->num_tx_chains - 1;
3774 
3775 		switch (i) {
3776 		case NL80211_IFTYPE_AP:
3777 			he_cap_elem->phy_cap_info[3] &=
3778 				~IEEE80211_HE_PHY_CAP3_DCM_MAX_CONST_TX_MASK;
3779 			he_cap_elem->phy_cap_info[9] |=
3780 				IEEE80211_HE_PHY_CAP9_RX_1024_QAM_LESS_THAN_242_TONE_RU;
3781 			break;
3782 		case NL80211_IFTYPE_STATION:
3783 			he_cap_elem->mac_cap_info[0] &=
3784 				~IEEE80211_HE_MAC_CAP0_TWT_RES;
3785 			he_cap_elem->mac_cap_info[0] |=
3786 				IEEE80211_HE_MAC_CAP0_TWT_REQ;
3787 			he_cap_elem->phy_cap_info[9] |=
3788 				IEEE80211_HE_PHY_CAP9_TX_1024_QAM_LESS_THAN_242_TONE_RU;
3789 			break;
3790 		case NL80211_IFTYPE_MESH_POINT:
3791 			ath11k_mac_filter_he_cap_mesh(he_cap_elem);
3792 			break;
3793 		}
3794 
3795 		he_cap->he_mcs_nss_supp.rx_mcs_80 =
3796 			cpu_to_le16(band_cap->he_mcs & 0xffff);
3797 		he_cap->he_mcs_nss_supp.tx_mcs_80 =
3798 			cpu_to_le16(band_cap->he_mcs & 0xffff);
3799 		he_cap->he_mcs_nss_supp.rx_mcs_160 =
3800 			cpu_to_le16((band_cap->he_mcs >> 16) & 0xffff);
3801 		he_cap->he_mcs_nss_supp.tx_mcs_160 =
3802 			cpu_to_le16((band_cap->he_mcs >> 16) & 0xffff);
3803 		he_cap->he_mcs_nss_supp.rx_mcs_80p80 =
3804 			cpu_to_le16((band_cap->he_mcs >> 16) & 0xffff);
3805 		he_cap->he_mcs_nss_supp.tx_mcs_80p80 =
3806 			cpu_to_le16((band_cap->he_mcs >> 16) & 0xffff);
3807 
3808 		memset(he_cap->ppe_thres, 0, sizeof(he_cap->ppe_thres));
3809 		if (he_cap_elem->phy_cap_info[6] &
3810 		    IEEE80211_HE_PHY_CAP6_PPE_THRESHOLD_PRESENT)
3811 			ath11k_gen_ppe_thresh(&band_cap->he_ppet,
3812 					      he_cap->ppe_thres);
3813 
3814 		if (band == NL80211_BAND_6GHZ) {
3815 			data[idx].he_6ghz_capa.capa =
3816 				ath11k_mac_setup_he_6ghz_cap(cap, band_cap);
3817 		}
3818 		idx++;
3819 	}
3820 
3821 	return idx;
3822 }
3823 
3824 static void ath11k_mac_setup_he_cap(struct ath11k *ar,
3825 				    struct ath11k_pdev_cap *cap)
3826 {
3827 	struct ieee80211_supported_band *band;
3828 	int count;
3829 
3830 	if (cap->supported_bands & WMI_HOST_WLAN_2G_CAP) {
3831 		count = ath11k_mac_copy_he_cap(ar, cap,
3832 					       ar->mac.iftype[NL80211_BAND_2GHZ],
3833 					       NL80211_BAND_2GHZ);
3834 		band = &ar->mac.sbands[NL80211_BAND_2GHZ];
3835 		band->iftype_data = ar->mac.iftype[NL80211_BAND_2GHZ];
3836 		band->n_iftype_data = count;
3837 	}
3838 
3839 	if (cap->supported_bands & WMI_HOST_WLAN_5G_CAP) {
3840 		count = ath11k_mac_copy_he_cap(ar, cap,
3841 					       ar->mac.iftype[NL80211_BAND_5GHZ],
3842 					       NL80211_BAND_5GHZ);
3843 		band = &ar->mac.sbands[NL80211_BAND_5GHZ];
3844 		band->iftype_data = ar->mac.iftype[NL80211_BAND_5GHZ];
3845 		band->n_iftype_data = count;
3846 	}
3847 
3848 	if (cap->supported_bands & WMI_HOST_WLAN_5G_CAP &&
3849 	    ar->supports_6ghz) {
3850 		count = ath11k_mac_copy_he_cap(ar, cap,
3851 					       ar->mac.iftype[NL80211_BAND_6GHZ],
3852 					       NL80211_BAND_6GHZ);
3853 		band = &ar->mac.sbands[NL80211_BAND_6GHZ];
3854 		band->iftype_data = ar->mac.iftype[NL80211_BAND_6GHZ];
3855 		band->n_iftype_data = count;
3856 	}
3857 }
3858 
3859 static int __ath11k_set_antenna(struct ath11k *ar, u32 tx_ant, u32 rx_ant)
3860 {
3861 	int ret;
3862 
3863 	lockdep_assert_held(&ar->conf_mutex);
3864 
3865 	if (ath11k_check_chain_mask(ar, tx_ant, true))
3866 		return -EINVAL;
3867 
3868 	if (ath11k_check_chain_mask(ar, rx_ant, false))
3869 		return -EINVAL;
3870 
3871 	ar->cfg_tx_chainmask = tx_ant;
3872 	ar->cfg_rx_chainmask = rx_ant;
3873 
3874 	if (ar->state != ATH11K_STATE_ON &&
3875 	    ar->state != ATH11K_STATE_RESTARTED)
3876 		return 0;
3877 
3878 	ret = ath11k_wmi_pdev_set_param(ar, WMI_PDEV_PARAM_TX_CHAIN_MASK,
3879 					tx_ant, ar->pdev->pdev_id);
3880 	if (ret) {
3881 		ath11k_warn(ar->ab, "failed to set tx-chainmask: %d, req 0x%x\n",
3882 			    ret, tx_ant);
3883 		return ret;
3884 	}
3885 
3886 	ar->num_tx_chains = get_num_chains(tx_ant);
3887 
3888 	ret = ath11k_wmi_pdev_set_param(ar, WMI_PDEV_PARAM_RX_CHAIN_MASK,
3889 					rx_ant, ar->pdev->pdev_id);
3890 	if (ret) {
3891 		ath11k_warn(ar->ab, "failed to set rx-chainmask: %d, req 0x%x\n",
3892 			    ret, rx_ant);
3893 		return ret;
3894 	}
3895 
3896 	ar->num_rx_chains = get_num_chains(rx_ant);
3897 
3898 	/* Reload HT/VHT/HE capability */
3899 	ath11k_mac_setup_ht_vht_cap(ar, &ar->pdev->cap, NULL);
3900 	ath11k_mac_setup_he_cap(ar, &ar->pdev->cap);
3901 
3902 	return 0;
3903 }
3904 
3905 int ath11k_mac_tx_mgmt_pending_free(int buf_id, void *skb, void *ctx)
3906 {
3907 	struct sk_buff *msdu = skb;
3908 	struct ieee80211_tx_info *info;
3909 	struct ath11k *ar = ctx;
3910 	struct ath11k_base *ab = ar->ab;
3911 
3912 	spin_lock_bh(&ar->txmgmt_idr_lock);
3913 	idr_remove(&ar->txmgmt_idr, buf_id);
3914 	spin_unlock_bh(&ar->txmgmt_idr_lock);
3915 	dma_unmap_single(ab->dev, ATH11K_SKB_CB(msdu)->paddr, msdu->len,
3916 			 DMA_TO_DEVICE);
3917 
3918 	info = IEEE80211_SKB_CB(msdu);
3919 	memset(&info->status, 0, sizeof(info->status));
3920 
3921 	ieee80211_free_txskb(ar->hw, msdu);
3922 
3923 	return 0;
3924 }
3925 
3926 static int ath11k_mac_vif_txmgmt_idr_remove(int buf_id, void *skb, void *ctx)
3927 {
3928 	struct ieee80211_vif *vif = ctx;
3929 	struct ath11k_skb_cb *skb_cb = ATH11K_SKB_CB((struct sk_buff *)skb);
3930 	struct sk_buff *msdu = skb;
3931 	struct ath11k *ar = skb_cb->ar;
3932 	struct ath11k_base *ab = ar->ab;
3933 
3934 	if (skb_cb->vif == vif) {
3935 		spin_lock_bh(&ar->txmgmt_idr_lock);
3936 		idr_remove(&ar->txmgmt_idr, buf_id);
3937 		spin_unlock_bh(&ar->txmgmt_idr_lock);
3938 		dma_unmap_single(ab->dev, skb_cb->paddr, msdu->len,
3939 				 DMA_TO_DEVICE);
3940 	}
3941 
3942 	return 0;
3943 }
3944 
3945 static int ath11k_mac_mgmt_tx_wmi(struct ath11k *ar, struct ath11k_vif *arvif,
3946 				  struct sk_buff *skb)
3947 {
3948 	struct ath11k_base *ab = ar->ab;
3949 	struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
3950 	struct ieee80211_tx_info *info;
3951 	dma_addr_t paddr;
3952 	int buf_id;
3953 	int ret;
3954 
3955 	spin_lock_bh(&ar->txmgmt_idr_lock);
3956 	buf_id = idr_alloc(&ar->txmgmt_idr, skb, 0,
3957 			   ATH11K_TX_MGMT_NUM_PENDING_MAX, GFP_ATOMIC);
3958 	spin_unlock_bh(&ar->txmgmt_idr_lock);
3959 	if (buf_id < 0)
3960 		return -ENOSPC;
3961 
3962 	info = IEEE80211_SKB_CB(skb);
3963 	if (!(info->flags & IEEE80211_TX_CTL_HW_80211_ENCAP)) {
3964 		if ((ieee80211_is_action(hdr->frame_control) ||
3965 		     ieee80211_is_deauth(hdr->frame_control) ||
3966 		     ieee80211_is_disassoc(hdr->frame_control)) &&
3967 		     ieee80211_has_protected(hdr->frame_control)) {
3968 			skb_put(skb, IEEE80211_CCMP_MIC_LEN);
3969 		}
3970 	}
3971 
3972 	paddr = dma_map_single(ab->dev, skb->data, skb->len, DMA_TO_DEVICE);
3973 	if (dma_mapping_error(ab->dev, paddr)) {
3974 		ath11k_warn(ab, "failed to DMA map mgmt Tx buffer\n");
3975 		ret = -EIO;
3976 		goto err_free_idr;
3977 	}
3978 
3979 	ATH11K_SKB_CB(skb)->paddr = paddr;
3980 
3981 	ret = ath11k_wmi_mgmt_send(ar, arvif->vdev_id, buf_id, skb);
3982 	if (ret) {
3983 		ath11k_warn(ar->ab, "failed to send mgmt frame: %d\n", ret);
3984 		goto err_unmap_buf;
3985 	}
3986 
3987 	return 0;
3988 
3989 err_unmap_buf:
3990 	dma_unmap_single(ab->dev, ATH11K_SKB_CB(skb)->paddr,
3991 			 skb->len, DMA_TO_DEVICE);
3992 err_free_idr:
3993 	spin_lock_bh(&ar->txmgmt_idr_lock);
3994 	idr_remove(&ar->txmgmt_idr, buf_id);
3995 	spin_unlock_bh(&ar->txmgmt_idr_lock);
3996 
3997 	return ret;
3998 }
3999 
4000 static void ath11k_mgmt_over_wmi_tx_purge(struct ath11k *ar)
4001 {
4002 	struct sk_buff *skb;
4003 
4004 	while ((skb = skb_dequeue(&ar->wmi_mgmt_tx_queue)) != NULL)
4005 		ieee80211_free_txskb(ar->hw, skb);
4006 }
4007 
4008 static void ath11k_mgmt_over_wmi_tx_work(struct work_struct *work)
4009 {
4010 	struct ath11k *ar = container_of(work, struct ath11k, wmi_mgmt_tx_work);
4011 	struct ath11k_skb_cb *skb_cb;
4012 	struct ath11k_vif *arvif;
4013 	struct sk_buff *skb;
4014 	int ret;
4015 
4016 	while ((skb = skb_dequeue(&ar->wmi_mgmt_tx_queue)) != NULL) {
4017 		skb_cb = ATH11K_SKB_CB(skb);
4018 		if (!skb_cb->vif) {
4019 			ath11k_warn(ar->ab, "no vif found for mgmt frame\n");
4020 			ieee80211_free_txskb(ar->hw, skb);
4021 			continue;
4022 		}
4023 
4024 		arvif = ath11k_vif_to_arvif(skb_cb->vif);
4025 		if (ar->allocated_vdev_map & (1LL << arvif->vdev_id) &&
4026 		    arvif->is_started) {
4027 			ret = ath11k_mac_mgmt_tx_wmi(ar, arvif, skb);
4028 			if (ret) {
4029 				ath11k_warn(ar->ab, "failed to tx mgmt frame, vdev_id %d :%d\n",
4030 					    arvif->vdev_id, ret);
4031 				ieee80211_free_txskb(ar->hw, skb);
4032 			} else {
4033 				atomic_inc(&ar->num_pending_mgmt_tx);
4034 			}
4035 		} else {
4036 			ath11k_warn(ar->ab,
4037 				    "dropping mgmt frame for vdev %d, is_started %d\n",
4038 				    arvif->vdev_id,
4039 				    arvif->is_started);
4040 			ieee80211_free_txskb(ar->hw, skb);
4041 		}
4042 	}
4043 }
4044 
4045 static int ath11k_mac_mgmt_tx(struct ath11k *ar, struct sk_buff *skb,
4046 			      bool is_prb_rsp)
4047 {
4048 	struct sk_buff_head *q = &ar->wmi_mgmt_tx_queue;
4049 
4050 	if (test_bit(ATH11K_FLAG_CRASH_FLUSH, &ar->ab->dev_flags))
4051 		return -ESHUTDOWN;
4052 
4053 	/* Drop probe response packets when the pending management tx
4054 	 * count has reached a certain threshold, so as to prioritize
4055 	 * other mgmt packets like auth and assoc to be sent on time
4056 	 * for establishing successful connections.
4057 	 */
4058 	if (is_prb_rsp &&
4059 	    atomic_read(&ar->num_pending_mgmt_tx) > ATH11K_PRB_RSP_DROP_THRESHOLD) {
4060 		ath11k_warn(ar->ab,
4061 			    "dropping probe response as pending queue is almost full\n");
4062 		return -ENOSPC;
4063 	}
4064 
4065 	if (skb_queue_len(q) == ATH11K_TX_MGMT_NUM_PENDING_MAX) {
4066 		ath11k_warn(ar->ab, "mgmt tx queue is full\n");
4067 		return -ENOSPC;
4068 	}
4069 
4070 	skb_queue_tail(q, skb);
4071 	ieee80211_queue_work(ar->hw, &ar->wmi_mgmt_tx_work);
4072 
4073 	return 0;
4074 }
4075 
4076 static void ath11k_mac_op_tx(struct ieee80211_hw *hw,
4077 			     struct ieee80211_tx_control *control,
4078 			     struct sk_buff *skb)
4079 {
4080 	struct ath11k_skb_cb *skb_cb = ATH11K_SKB_CB(skb);
4081 	struct ath11k *ar = hw->priv;
4082 	struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
4083 	struct ieee80211_vif *vif = info->control.vif;
4084 	struct ath11k_vif *arvif = ath11k_vif_to_arvif(vif);
4085 	struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
4086 	struct ieee80211_key_conf *key = info->control.hw_key;
4087 	u32 info_flags = info->flags;
4088 	bool is_prb_rsp;
4089 	int ret;
4090 
4091 	memset(skb_cb, 0, sizeof(*skb_cb));
4092 	skb_cb->vif = vif;
4093 
4094 	if (key) {
4095 		skb_cb->cipher = key->cipher;
4096 		skb_cb->flags |= ATH11K_SKB_CIPHER_SET;
4097 	}
4098 
4099 	if (info_flags & IEEE80211_TX_CTL_HW_80211_ENCAP) {
4100 		skb_cb->flags |= ATH11K_SKB_HW_80211_ENCAP;
4101 	} else if (ieee80211_is_mgmt(hdr->frame_control)) {
4102 		is_prb_rsp = ieee80211_is_probe_resp(hdr->frame_control);
4103 		ret = ath11k_mac_mgmt_tx(ar, skb, is_prb_rsp);
4104 		if (ret) {
4105 			ath11k_warn(ar->ab, "failed to queue management frame %d\n",
4106 				    ret);
4107 			ieee80211_free_txskb(ar->hw, skb);
4108 		}
4109 		return;
4110 	}
4111 
4112 	ret = ath11k_dp_tx(ar, arvif, skb);
4113 	if (ret) {
4114 		ath11k_warn(ar->ab, "failed to transmit frame %d\n", ret);
4115 		ieee80211_free_txskb(ar->hw, skb);
4116 	}
4117 }
4118 
4119 void ath11k_mac_drain_tx(struct ath11k *ar)
4120 {
4121 	/* make sure rcu-protected mac80211 tx path itself is drained */
4122 	synchronize_net();
4123 
4124 	cancel_work_sync(&ar->wmi_mgmt_tx_work);
4125 	ath11k_mgmt_over_wmi_tx_purge(ar);
4126 }
4127 
4128 static int ath11k_mac_config_mon_status_default(struct ath11k *ar, bool enable)
4129 {
4130 	struct htt_rx_ring_tlv_filter tlv_filter = {0};
4131 	struct ath11k_base *ab = ar->ab;
4132 	int i, ret = 0;
4133 	u32 ring_id;
4134 
4135 	if (enable) {
4136 		tlv_filter = ath11k_mac_mon_status_filter_default;
4137 		if (ath11k_debugfs_rx_filter(ar))
4138 			tlv_filter.rx_filter = ath11k_debugfs_rx_filter(ar);
4139 	}
4140 
4141 	for (i = 0; i < ab->hw_params.num_rxmda_per_pdev; i++) {
4142 		ring_id = ar->dp.rx_mon_status_refill_ring[i].refill_buf_ring.ring_id;
4143 		ret = ath11k_dp_tx_htt_rx_filter_setup(ar->ab, ring_id,
4144 						       ar->dp.mac_id + i,
4145 						       HAL_RXDMA_MONITOR_STATUS,
4146 						       DP_RX_BUFFER_SIZE,
4147 						       &tlv_filter);
4148 	}
4149 
4150 	if (enable && !ar->ab->hw_params.rxdma1_enable)
4151 		mod_timer(&ar->ab->mon_reap_timer, jiffies +
4152 			  msecs_to_jiffies(ATH11K_MON_TIMER_INTERVAL));
4153 
4154 	return ret;
4155 }
4156 
4157 static int ath11k_mac_op_start(struct ieee80211_hw *hw)
4158 {
4159 	struct ath11k *ar = hw->priv;
4160 	struct ath11k_base *ab = ar->ab;
4161 	struct ath11k_pdev *pdev = ar->pdev;
4162 	int ret;
4163 
4164 	ath11k_mac_drain_tx(ar);
4165 	mutex_lock(&ar->conf_mutex);
4166 
4167 	switch (ar->state) {
4168 	case ATH11K_STATE_OFF:
4169 		ar->state = ATH11K_STATE_ON;
4170 		break;
4171 	case ATH11K_STATE_RESTARTING:
4172 		ar->state = ATH11K_STATE_RESTARTED;
4173 		break;
4174 	case ATH11K_STATE_RESTARTED:
4175 	case ATH11K_STATE_WEDGED:
4176 	case ATH11K_STATE_ON:
4177 		WARN_ON(1);
4178 		ret = -EINVAL;
4179 		goto err;
4180 	}
4181 
4182 	ret = ath11k_wmi_pdev_set_param(ar, WMI_PDEV_PARAM_PMF_QOS,
4183 					1, pdev->pdev_id);
4184 
4185 	if (ret) {
4186 		ath11k_err(ar->ab, "failed to enable PMF QOS: (%d\n", ret);
4187 		goto err;
4188 	}
4189 
4190 	ret = ath11k_wmi_pdev_set_param(ar, WMI_PDEV_PARAM_DYNAMIC_BW, 1,
4191 					pdev->pdev_id);
4192 	if (ret) {
4193 		ath11k_err(ar->ab, "failed to enable dynamic bw: %d\n", ret);
4194 		goto err;
4195 	}
4196 
4197 	ret = ath11k_wmi_pdev_set_param(ar, WMI_PDEV_PARAM_ARP_AC_OVERRIDE,
4198 					0, pdev->pdev_id);
4199 	if (ret) {
4200 		ath11k_err(ab, "failed to set ac override for ARP: %d\n",
4201 			   ret);
4202 		goto err;
4203 	}
4204 
4205 	ret = ath11k_wmi_send_dfs_phyerr_offload_enable_cmd(ar, pdev->pdev_id);
4206 	if (ret) {
4207 		ath11k_err(ab, "failed to offload radar detection: %d\n",
4208 			   ret);
4209 		goto err;
4210 	}
4211 
4212 	ret = ath11k_dp_tx_htt_h2t_ppdu_stats_req(ar,
4213 						  HTT_PPDU_STATS_TAG_DEFAULT);
4214 	if (ret) {
4215 		ath11k_err(ab, "failed to req ppdu stats: %d\n", ret);
4216 		goto err;
4217 	}
4218 
4219 	ret = ath11k_wmi_pdev_set_param(ar, WMI_PDEV_PARAM_MESH_MCAST_ENABLE,
4220 					1, pdev->pdev_id);
4221 
4222 	if (ret) {
4223 		ath11k_err(ar->ab, "failed to enable MESH MCAST ENABLE: (%d\n", ret);
4224 		goto err;
4225 	}
4226 
4227 	__ath11k_set_antenna(ar, ar->cfg_tx_chainmask, ar->cfg_rx_chainmask);
4228 
4229 	/* TODO: Do we need to enable ANI? */
4230 
4231 	ath11k_reg_update_chan_list(ar);
4232 
4233 	ar->num_started_vdevs = 0;
4234 	ar->num_created_vdevs = 0;
4235 	ar->num_peers = 0;
4236 	ar->allocated_vdev_map = 0;
4237 
4238 	/* Configure monitor status ring with default rx_filter to get rx status
4239 	 * such as rssi, rx_duration.
4240 	 */
4241 	ret = ath11k_mac_config_mon_status_default(ar, true);
4242 	if (ret) {
4243 		ath11k_err(ab, "failed to configure monitor status ring with default rx_filter: (%d)\n",
4244 			   ret);
4245 		goto err;
4246 	}
4247 
4248 	/* Configure the hash seed for hash based reo dest ring selection */
4249 	ath11k_wmi_pdev_lro_cfg(ar, ar->pdev->pdev_id);
4250 
4251 	mutex_unlock(&ar->conf_mutex);
4252 
4253 	rcu_assign_pointer(ab->pdevs_active[ar->pdev_idx],
4254 			   &ab->pdevs[ar->pdev_idx]);
4255 
4256 	/* allow device to enter IMPS */
4257 	if (ab->hw_params.idle_ps) {
4258 		ret = ath11k_wmi_pdev_set_param(ar, WMI_PDEV_PARAM_IDLE_PS_CONFIG,
4259 						1, pdev->pdev_id);
4260 		if (ret) {
4261 			ath11k_err(ab, "failed to enable idle ps: %d\n", ret);
4262 			goto err;
4263 		}
4264 	}
4265 	return 0;
4266 
4267 err:
4268 	ar->state = ATH11K_STATE_OFF;
4269 	mutex_unlock(&ar->conf_mutex);
4270 
4271 	return ret;
4272 }
4273 
4274 static void ath11k_mac_op_stop(struct ieee80211_hw *hw)
4275 {
4276 	struct ath11k *ar = hw->priv;
4277 	struct htt_ppdu_stats_info *ppdu_stats, *tmp;
4278 	int ret;
4279 
4280 	ath11k_mac_drain_tx(ar);
4281 
4282 	mutex_lock(&ar->conf_mutex);
4283 	ret = ath11k_mac_config_mon_status_default(ar, false);
4284 	if (ret)
4285 		ath11k_err(ar->ab, "failed to clear rx_filter for monitor status ring: (%d)\n",
4286 			   ret);
4287 
4288 	clear_bit(ATH11K_CAC_RUNNING, &ar->dev_flags);
4289 	ar->state = ATH11K_STATE_OFF;
4290 	mutex_unlock(&ar->conf_mutex);
4291 
4292 	cancel_delayed_work_sync(&ar->scan.timeout);
4293 	cancel_work_sync(&ar->regd_update_work);
4294 
4295 	spin_lock_bh(&ar->data_lock);
4296 	list_for_each_entry_safe(ppdu_stats, tmp, &ar->ppdu_stats_info, list) {
4297 		list_del(&ppdu_stats->list);
4298 		kfree(ppdu_stats);
4299 	}
4300 	spin_unlock_bh(&ar->data_lock);
4301 
4302 	rcu_assign_pointer(ar->ab->pdevs_active[ar->pdev_idx], NULL);
4303 
4304 	synchronize_rcu();
4305 
4306 	atomic_set(&ar->num_pending_mgmt_tx, 0);
4307 }
4308 
4309 static void
4310 ath11k_mac_setup_vdev_create_params(struct ath11k_vif *arvif,
4311 				    struct vdev_create_params *params)
4312 {
4313 	struct ath11k *ar = arvif->ar;
4314 	struct ath11k_pdev *pdev = ar->pdev;
4315 
4316 	params->if_id = arvif->vdev_id;
4317 	params->type = arvif->vdev_type;
4318 	params->subtype = arvif->vdev_subtype;
4319 	params->pdev_id = pdev->pdev_id;
4320 
4321 	if (pdev->cap.supported_bands & WMI_HOST_WLAN_2G_CAP) {
4322 		params->chains[NL80211_BAND_2GHZ].tx = ar->num_tx_chains;
4323 		params->chains[NL80211_BAND_2GHZ].rx = ar->num_rx_chains;
4324 	}
4325 	if (pdev->cap.supported_bands & WMI_HOST_WLAN_5G_CAP) {
4326 		params->chains[NL80211_BAND_5GHZ].tx = ar->num_tx_chains;
4327 		params->chains[NL80211_BAND_5GHZ].rx = ar->num_rx_chains;
4328 	}
4329 	if (pdev->cap.supported_bands & WMI_HOST_WLAN_5G_CAP &&
4330 	    ar->supports_6ghz) {
4331 		params->chains[NL80211_BAND_6GHZ].tx = ar->num_tx_chains;
4332 		params->chains[NL80211_BAND_6GHZ].rx = ar->num_rx_chains;
4333 	}
4334 }
4335 
4336 static u32
4337 ath11k_mac_prepare_he_mode(struct ath11k_pdev *pdev, u32 viftype)
4338 {
4339 	struct ath11k_pdev_cap *pdev_cap = &pdev->cap;
4340 	struct ath11k_band_cap *cap_band = NULL;
4341 	u32 *hecap_phy_ptr = NULL;
4342 	u32 hemode = 0;
4343 
4344 	if (pdev->cap.supported_bands & WMI_HOST_WLAN_2G_CAP)
4345 		cap_band = &pdev_cap->band[NL80211_BAND_2GHZ];
4346 	else
4347 		cap_band = &pdev_cap->band[NL80211_BAND_5GHZ];
4348 
4349 	hecap_phy_ptr = &cap_band->he_cap_phy_info[0];
4350 
4351 	hemode = FIELD_PREP(HE_MODE_SU_TX_BFEE, HE_SU_BFEE_ENABLE) |
4352 		 FIELD_PREP(HE_MODE_SU_TX_BFER, HECAP_PHY_SUBFMR_GET(hecap_phy_ptr)) |
4353 		 FIELD_PREP(HE_MODE_UL_MUMIMO, HECAP_PHY_ULMUMIMO_GET(hecap_phy_ptr));
4354 
4355 	/* TODO WDS and other modes */
4356 	if (viftype == NL80211_IFTYPE_AP) {
4357 		hemode |= FIELD_PREP(HE_MODE_MU_TX_BFER,
4358 			  HECAP_PHY_MUBFMR_GET(hecap_phy_ptr)) |
4359 			  FIELD_PREP(HE_MODE_DL_OFDMA, HE_DL_MUOFDMA_ENABLE) |
4360 			  FIELD_PREP(HE_MODE_UL_OFDMA, HE_UL_MUOFDMA_ENABLE);
4361 	} else {
4362 		hemode |= FIELD_PREP(HE_MODE_MU_TX_BFEE, HE_MU_BFEE_ENABLE);
4363 	}
4364 
4365 	return hemode;
4366 }
4367 
4368 static int ath11k_set_he_mu_sounding_mode(struct ath11k *ar,
4369 					  struct ath11k_vif *arvif)
4370 {
4371 	u32 param_id, param_value;
4372 	struct ath11k_base *ab = ar->ab;
4373 	int ret = 0;
4374 
4375 	param_id = WMI_VDEV_PARAM_SET_HEMU_MODE;
4376 	param_value = ath11k_mac_prepare_he_mode(ar->pdev, arvif->vif->type);
4377 	ret = ath11k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
4378 					    param_id, param_value);
4379 	if (ret) {
4380 		ath11k_warn(ab, "failed to set vdev %d HE MU mode: %d param_value %x\n",
4381 			    arvif->vdev_id, ret, param_value);
4382 		return ret;
4383 	}
4384 	param_id = WMI_VDEV_PARAM_SET_HE_SOUNDING_MODE;
4385 	param_value =
4386 		FIELD_PREP(HE_VHT_SOUNDING_MODE, HE_VHT_SOUNDING_MODE_ENABLE) |
4387 		FIELD_PREP(HE_TRIG_NONTRIG_SOUNDING_MODE,
4388 			   HE_TRIG_NONTRIG_SOUNDING_MODE_ENABLE);
4389 	ret = ath11k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
4390 					    param_id, param_value);
4391 	if (ret) {
4392 		ath11k_warn(ab, "failed to set vdev %d HE MU mode: %d\n",
4393 			    arvif->vdev_id, ret);
4394 		return ret;
4395 	}
4396 	return ret;
4397 }
4398 
4399 static void ath11k_mac_op_update_vif_offload(struct ieee80211_hw *hw,
4400 					     struct ieee80211_vif *vif)
4401 {
4402 	struct ath11k *ar = hw->priv;
4403 	struct ath11k_base *ab = ar->ab;
4404 	struct ath11k_vif *arvif = ath11k_vif_to_arvif(vif);
4405 	u32 param_id, param_value;
4406 	int ret;
4407 
4408 	param_id = WMI_VDEV_PARAM_TX_ENCAP_TYPE;
4409 	if (ath11k_frame_mode != ATH11K_HW_TXRX_ETHERNET ||
4410 	    (vif->type != NL80211_IFTYPE_STATION &&
4411 	     vif->type != NL80211_IFTYPE_AP))
4412 		vif->offload_flags &= ~IEEE80211_OFFLOAD_ENCAP_ENABLED;
4413 
4414 	if (vif->offload_flags & IEEE80211_OFFLOAD_ENCAP_ENABLED)
4415 		param_value = ATH11K_HW_TXRX_ETHERNET;
4416 	else if (test_bit(ATH11K_FLAG_RAW_MODE, &ab->dev_flags))
4417 		param_value = ATH11K_HW_TXRX_RAW;
4418 	else
4419 		param_value = ATH11K_HW_TXRX_NATIVE_WIFI;
4420 
4421 	ret = ath11k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
4422 					    param_id, param_value);
4423 	if (ret) {
4424 		ath11k_warn(ab, "failed to set vdev %d tx encap mode: %d\n",
4425 			    arvif->vdev_id, ret);
4426 		vif->offload_flags &= ~IEEE80211_OFFLOAD_ENCAP_ENABLED;
4427 	}
4428 }
4429 
4430 static int ath11k_mac_op_add_interface(struct ieee80211_hw *hw,
4431 				       struct ieee80211_vif *vif)
4432 {
4433 	struct ath11k *ar = hw->priv;
4434 	struct ath11k_base *ab = ar->ab;
4435 	struct ath11k_vif *arvif = ath11k_vif_to_arvif(vif);
4436 	struct vdev_create_params vdev_param = {0};
4437 	struct peer_create_params peer_param;
4438 	u32 param_id, param_value;
4439 	u16 nss;
4440 	int i;
4441 	int ret;
4442 	int bit;
4443 
4444 	vif->driver_flags |= IEEE80211_VIF_SUPPORTS_UAPSD;
4445 
4446 	mutex_lock(&ar->conf_mutex);
4447 
4448 	if (vif->type == NL80211_IFTYPE_AP &&
4449 	    ar->num_peers > (ar->max_num_peers - 1)) {
4450 		ath11k_warn(ab, "failed to create vdev due to insufficient peer entry resource in firmware\n");
4451 		ret = -ENOBUFS;
4452 		goto err;
4453 	}
4454 
4455 	if (ar->num_created_vdevs > (TARGET_NUM_VDEVS - 1)) {
4456 		ath11k_warn(ab, "failed to create vdev, reached max vdev limit %d\n",
4457 			    TARGET_NUM_VDEVS);
4458 		ret = -EBUSY;
4459 		goto err;
4460 	}
4461 
4462 	memset(arvif, 0, sizeof(*arvif));
4463 
4464 	arvif->ar = ar;
4465 	arvif->vif = vif;
4466 
4467 	INIT_LIST_HEAD(&arvif->list);
4468 
4469 	/* Should we initialize any worker to handle connection loss indication
4470 	 * from firmware in sta mode?
4471 	 */
4472 
4473 	for (i = 0; i < ARRAY_SIZE(arvif->bitrate_mask.control); i++) {
4474 		arvif->bitrate_mask.control[i].legacy = 0xffffffff;
4475 		memset(arvif->bitrate_mask.control[i].ht_mcs, 0xff,
4476 		       sizeof(arvif->bitrate_mask.control[i].ht_mcs));
4477 		memset(arvif->bitrate_mask.control[i].vht_mcs, 0xff,
4478 		       sizeof(arvif->bitrate_mask.control[i].vht_mcs));
4479 	}
4480 
4481 	bit = __ffs64(ab->free_vdev_map);
4482 
4483 	arvif->vdev_id = bit;
4484 	arvif->vdev_subtype = WMI_VDEV_SUBTYPE_NONE;
4485 
4486 	switch (vif->type) {
4487 	case NL80211_IFTYPE_UNSPECIFIED:
4488 	case NL80211_IFTYPE_STATION:
4489 		arvif->vdev_type = WMI_VDEV_TYPE_STA;
4490 		break;
4491 	case NL80211_IFTYPE_MESH_POINT:
4492 		arvif->vdev_subtype = WMI_VDEV_SUBTYPE_MESH_11S;
4493 		fallthrough;
4494 	case NL80211_IFTYPE_AP:
4495 		arvif->vdev_type = WMI_VDEV_TYPE_AP;
4496 		break;
4497 	case NL80211_IFTYPE_MONITOR:
4498 		arvif->vdev_type = WMI_VDEV_TYPE_MONITOR;
4499 		break;
4500 	default:
4501 		WARN_ON(1);
4502 		break;
4503 	}
4504 
4505 	ath11k_dbg(ar->ab, ATH11K_DBG_MAC, "mac add interface id %d type %d subtype %d map %llx\n",
4506 		   arvif->vdev_id, arvif->vdev_type, arvif->vdev_subtype,
4507 		   ab->free_vdev_map);
4508 
4509 	vif->cab_queue = arvif->vdev_id % (ATH11K_HW_MAX_QUEUES - 1);
4510 	for (i = 0; i < ARRAY_SIZE(vif->hw_queue); i++)
4511 		vif->hw_queue[i] = i % (ATH11K_HW_MAX_QUEUES - 1);
4512 
4513 	ath11k_mac_setup_vdev_create_params(arvif, &vdev_param);
4514 
4515 	ret = ath11k_wmi_vdev_create(ar, vif->addr, &vdev_param);
4516 	if (ret) {
4517 		ath11k_warn(ab, "failed to create WMI vdev %d: %d\n",
4518 			    arvif->vdev_id, ret);
4519 		goto err;
4520 	}
4521 
4522 	ar->num_created_vdevs++;
4523 	ath11k_dbg(ab, ATH11K_DBG_MAC, "vdev %pM created, vdev_id %d\n",
4524 		   vif->addr, arvif->vdev_id);
4525 	ar->allocated_vdev_map |= 1LL << arvif->vdev_id;
4526 	ab->free_vdev_map &= ~(1LL << arvif->vdev_id);
4527 
4528 	spin_lock_bh(&ar->data_lock);
4529 	list_add(&arvif->list, &ar->arvifs);
4530 	spin_unlock_bh(&ar->data_lock);
4531 
4532 	ath11k_mac_op_update_vif_offload(hw, vif);
4533 
4534 	nss = get_num_chains(ar->cfg_tx_chainmask) ? : 1;
4535 	ret = ath11k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
4536 					    WMI_VDEV_PARAM_NSS, nss);
4537 	if (ret) {
4538 		ath11k_warn(ab, "failed to set vdev %d chainmask 0x%x, nss %d :%d\n",
4539 			    arvif->vdev_id, ar->cfg_tx_chainmask, nss, ret);
4540 		goto err_vdev_del;
4541 	}
4542 
4543 	switch (arvif->vdev_type) {
4544 	case WMI_VDEV_TYPE_AP:
4545 		peer_param.vdev_id = arvif->vdev_id;
4546 		peer_param.peer_addr = vif->addr;
4547 		peer_param.peer_type = WMI_PEER_TYPE_DEFAULT;
4548 		ret = ath11k_peer_create(ar, arvif, NULL, &peer_param);
4549 		if (ret) {
4550 			ath11k_warn(ab, "failed to vdev %d create peer for AP: %d\n",
4551 				    arvif->vdev_id, ret);
4552 			goto err_vdev_del;
4553 		}
4554 
4555 		ret = ath11k_mac_set_kickout(arvif);
4556 		if (ret) {
4557 			ath11k_warn(ar->ab, "failed to set vdev %i kickout parameters: %d\n",
4558 				    arvif->vdev_id, ret);
4559 			goto err_peer_del;
4560 		}
4561 		break;
4562 	case WMI_VDEV_TYPE_STA:
4563 		param_id = WMI_STA_PS_PARAM_RX_WAKE_POLICY;
4564 		param_value = WMI_STA_PS_RX_WAKE_POLICY_WAKE;
4565 		ret = ath11k_wmi_set_sta_ps_param(ar, arvif->vdev_id,
4566 						  param_id, param_value);
4567 		if (ret) {
4568 			ath11k_warn(ar->ab, "failed to set vdev %d RX wake policy: %d\n",
4569 				    arvif->vdev_id, ret);
4570 			goto err_peer_del;
4571 		}
4572 
4573 		param_id = WMI_STA_PS_PARAM_TX_WAKE_THRESHOLD;
4574 		param_value = WMI_STA_PS_TX_WAKE_THRESHOLD_ALWAYS;
4575 		ret = ath11k_wmi_set_sta_ps_param(ar, arvif->vdev_id,
4576 						  param_id, param_value);
4577 		if (ret) {
4578 			ath11k_warn(ar->ab, "failed to set vdev %d TX wake threshold: %d\n",
4579 				    arvif->vdev_id, ret);
4580 			goto err_peer_del;
4581 		}
4582 
4583 		param_id = WMI_STA_PS_PARAM_PSPOLL_COUNT;
4584 		param_value = WMI_STA_PS_PSPOLL_COUNT_NO_MAX;
4585 		ret = ath11k_wmi_set_sta_ps_param(ar, arvif->vdev_id,
4586 						  param_id, param_value);
4587 		if (ret) {
4588 			ath11k_warn(ar->ab, "failed to set vdev %d pspoll count: %d\n",
4589 				    arvif->vdev_id, ret);
4590 			goto err_peer_del;
4591 		}
4592 
4593 		ret = ath11k_wmi_pdev_set_ps_mode(ar, arvif->vdev_id, false);
4594 		if (ret) {
4595 			ath11k_warn(ar->ab, "failed to disable vdev %d ps mode: %d\n",
4596 				    arvif->vdev_id, ret);
4597 			goto err_peer_del;
4598 		}
4599 		break;
4600 	default:
4601 		break;
4602 	}
4603 
4604 	arvif->txpower = vif->bss_conf.txpower;
4605 	ret = ath11k_mac_txpower_recalc(ar);
4606 	if (ret)
4607 		goto err_peer_del;
4608 
4609 	param_id = WMI_VDEV_PARAM_RTS_THRESHOLD;
4610 	param_value = ar->hw->wiphy->rts_threshold;
4611 	ret = ath11k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
4612 					    param_id, param_value);
4613 	if (ret) {
4614 		ath11k_warn(ar->ab, "failed to set rts threshold for vdev %d: %d\n",
4615 			    arvif->vdev_id, ret);
4616 	}
4617 
4618 	ath11k_dp_vdev_tx_attach(ar, arvif);
4619 
4620 	mutex_unlock(&ar->conf_mutex);
4621 
4622 	return 0;
4623 
4624 err_peer_del:
4625 	if (arvif->vdev_type == WMI_VDEV_TYPE_AP) {
4626 		reinit_completion(&ar->peer_delete_done);
4627 
4628 		ret = ath11k_wmi_send_peer_delete_cmd(ar, vif->addr,
4629 						      arvif->vdev_id);
4630 		if (ret) {
4631 			ath11k_warn(ar->ab, "failed to delete peer vdev_id %d addr %pM\n",
4632 				    arvif->vdev_id, vif->addr);
4633 			goto err;
4634 		}
4635 
4636 		ret = ath11k_wait_for_peer_delete_done(ar, arvif->vdev_id,
4637 						       vif->addr);
4638 		if (ret)
4639 			goto err;
4640 
4641 		ar->num_peers--;
4642 	}
4643 
4644 err_vdev_del:
4645 	ath11k_wmi_vdev_delete(ar, arvif->vdev_id);
4646 	ar->num_created_vdevs--;
4647 	ar->allocated_vdev_map &= ~(1LL << arvif->vdev_id);
4648 	ab->free_vdev_map |= 1LL << arvif->vdev_id;
4649 	spin_lock_bh(&ar->data_lock);
4650 	list_del(&arvif->list);
4651 	spin_unlock_bh(&ar->data_lock);
4652 
4653 err:
4654 	mutex_unlock(&ar->conf_mutex);
4655 
4656 	return ret;
4657 }
4658 
4659 static int ath11k_mac_vif_unref(int buf_id, void *skb, void *ctx)
4660 {
4661 	struct ieee80211_vif *vif = (struct ieee80211_vif *)ctx;
4662 	struct ath11k_skb_cb *skb_cb = ATH11K_SKB_CB((struct sk_buff *)skb);
4663 
4664 	if (skb_cb->vif == vif)
4665 		skb_cb->vif = NULL;
4666 
4667 	return 0;
4668 }
4669 
4670 static void ath11k_mac_op_remove_interface(struct ieee80211_hw *hw,
4671 					   struct ieee80211_vif *vif)
4672 {
4673 	struct ath11k *ar = hw->priv;
4674 	struct ath11k_vif *arvif = ath11k_vif_to_arvif(vif);
4675 	struct ath11k_base *ab = ar->ab;
4676 	unsigned long time_left;
4677 	int ret;
4678 	int i;
4679 
4680 	mutex_lock(&ar->conf_mutex);
4681 
4682 	ath11k_dbg(ab, ATH11K_DBG_MAC, "mac remove interface (vdev %d)\n",
4683 		   arvif->vdev_id);
4684 
4685 	if (arvif->vdev_type == WMI_VDEV_TYPE_AP) {
4686 		ret = ath11k_peer_delete(ar, arvif->vdev_id, vif->addr);
4687 		if (ret)
4688 			ath11k_warn(ab, "failed to submit AP self-peer removal on vdev %d: %d\n",
4689 				    arvif->vdev_id, ret);
4690 	}
4691 
4692 	reinit_completion(&ar->vdev_delete_done);
4693 
4694 	ret = ath11k_wmi_vdev_delete(ar, arvif->vdev_id);
4695 	if (ret) {
4696 		ath11k_warn(ab, "failed to delete WMI vdev %d: %d\n",
4697 			    arvif->vdev_id, ret);
4698 		goto err_vdev_del;
4699 	}
4700 
4701 	time_left = wait_for_completion_timeout(&ar->vdev_delete_done,
4702 						ATH11K_VDEV_DELETE_TIMEOUT_HZ);
4703 	if (time_left == 0) {
4704 		ath11k_warn(ab, "Timeout in receiving vdev delete response\n");
4705 		goto err_vdev_del;
4706 	}
4707 
4708 	ab->free_vdev_map |= 1LL << (arvif->vdev_id);
4709 	ar->allocated_vdev_map &= ~(1LL << arvif->vdev_id);
4710 	ar->num_created_vdevs--;
4711 
4712 	ath11k_dbg(ab, ATH11K_DBG_MAC, "vdev %pM deleted, vdev_id %d\n",
4713 		   vif->addr, arvif->vdev_id);
4714 
4715 err_vdev_del:
4716 	spin_lock_bh(&ar->data_lock);
4717 	list_del(&arvif->list);
4718 	spin_unlock_bh(&ar->data_lock);
4719 
4720 	ath11k_peer_cleanup(ar, arvif->vdev_id);
4721 
4722 	idr_for_each(&ar->txmgmt_idr,
4723 		     ath11k_mac_vif_txmgmt_idr_remove, vif);
4724 
4725 	for (i = 0; i < DP_TCL_NUM_RING_MAX; i++) {
4726 		spin_lock_bh(&ab->dp.tx_ring[i].tx_idr_lock);
4727 		idr_for_each(&ab->dp.tx_ring[i].txbuf_idr,
4728 			     ath11k_mac_vif_unref, vif);
4729 		spin_unlock_bh(&ab->dp.tx_ring[i].tx_idr_lock);
4730 	}
4731 
4732 	/* Recalc txpower for remaining vdev */
4733 	ath11k_mac_txpower_recalc(ar);
4734 	clear_bit(ATH11K_FLAG_MONITOR_ENABLED, &ar->monitor_flags);
4735 
4736 	/* TODO: recal traffic pause state based on the available vdevs */
4737 
4738 	mutex_unlock(&ar->conf_mutex);
4739 }
4740 
4741 /* FIXME: Has to be verified. */
4742 #define SUPPORTED_FILTERS			\
4743 	(FIF_ALLMULTI |				\
4744 	FIF_CONTROL |				\
4745 	FIF_PSPOLL |				\
4746 	FIF_OTHER_BSS |				\
4747 	FIF_BCN_PRBRESP_PROMISC |		\
4748 	FIF_PROBE_REQ |				\
4749 	FIF_FCSFAIL)
4750 
4751 static void ath11k_mac_op_configure_filter(struct ieee80211_hw *hw,
4752 					   unsigned int changed_flags,
4753 					   unsigned int *total_flags,
4754 					   u64 multicast)
4755 {
4756 	struct ath11k *ar = hw->priv;
4757 	bool reset_flag = false;
4758 	int ret = 0;
4759 
4760 	mutex_lock(&ar->conf_mutex);
4761 
4762 	changed_flags &= SUPPORTED_FILTERS;
4763 	*total_flags &= SUPPORTED_FILTERS;
4764 	ar->filter_flags = *total_flags;
4765 
4766 	/* For monitor mode */
4767 	reset_flag = !(ar->filter_flags & FIF_BCN_PRBRESP_PROMISC);
4768 
4769 	ret = ath11k_dp_tx_htt_monitor_mode_ring_config(ar, reset_flag);
4770 	if (!ret) {
4771 		if (!reset_flag)
4772 			set_bit(ATH11K_FLAG_MONITOR_ENABLED, &ar->monitor_flags);
4773 		else
4774 			clear_bit(ATH11K_FLAG_MONITOR_ENABLED, &ar->monitor_flags);
4775 	} else {
4776 		ath11k_warn(ar->ab,
4777 			    "fail to set monitor filter: %d\n", ret);
4778 	}
4779 	ath11k_dbg(ar->ab, ATH11K_DBG_MAC,
4780 		   "changed_flags:0x%x, total_flags:0x%x, reset_flag:%d\n",
4781 		   changed_flags, *total_flags, reset_flag);
4782 
4783 	mutex_unlock(&ar->conf_mutex);
4784 }
4785 
4786 static int ath11k_mac_op_get_antenna(struct ieee80211_hw *hw, u32 *tx_ant, u32 *rx_ant)
4787 {
4788 	struct ath11k *ar = hw->priv;
4789 
4790 	mutex_lock(&ar->conf_mutex);
4791 
4792 	*tx_ant = ar->cfg_tx_chainmask;
4793 	*rx_ant = ar->cfg_rx_chainmask;
4794 
4795 	mutex_unlock(&ar->conf_mutex);
4796 
4797 	return 0;
4798 }
4799 
4800 static int ath11k_mac_op_set_antenna(struct ieee80211_hw *hw, u32 tx_ant, u32 rx_ant)
4801 {
4802 	struct ath11k *ar = hw->priv;
4803 	int ret;
4804 
4805 	mutex_lock(&ar->conf_mutex);
4806 	ret = __ath11k_set_antenna(ar, tx_ant, rx_ant);
4807 	mutex_unlock(&ar->conf_mutex);
4808 
4809 	return ret;
4810 }
4811 
4812 static int ath11k_mac_op_ampdu_action(struct ieee80211_hw *hw,
4813 				      struct ieee80211_vif *vif,
4814 				      struct ieee80211_ampdu_params *params)
4815 {
4816 	struct ath11k *ar = hw->priv;
4817 	int ret = -EINVAL;
4818 
4819 	mutex_lock(&ar->conf_mutex);
4820 
4821 	switch (params->action) {
4822 	case IEEE80211_AMPDU_RX_START:
4823 		ret = ath11k_dp_rx_ampdu_start(ar, params);
4824 		break;
4825 	case IEEE80211_AMPDU_RX_STOP:
4826 		ret = ath11k_dp_rx_ampdu_stop(ar, params);
4827 		break;
4828 	case IEEE80211_AMPDU_TX_START:
4829 	case IEEE80211_AMPDU_TX_STOP_CONT:
4830 	case IEEE80211_AMPDU_TX_STOP_FLUSH:
4831 	case IEEE80211_AMPDU_TX_STOP_FLUSH_CONT:
4832 	case IEEE80211_AMPDU_TX_OPERATIONAL:
4833 		/* Tx A-MPDU aggregation offloaded to hw/fw so deny mac80211
4834 		 * Tx aggregation requests.
4835 		 */
4836 		ret = -EOPNOTSUPP;
4837 		break;
4838 	}
4839 
4840 	mutex_unlock(&ar->conf_mutex);
4841 
4842 	return ret;
4843 }
4844 
4845 static int ath11k_mac_op_add_chanctx(struct ieee80211_hw *hw,
4846 				     struct ieee80211_chanctx_conf *ctx)
4847 {
4848 	struct ath11k *ar = hw->priv;
4849 	struct ath11k_base *ab = ar->ab;
4850 
4851 	ath11k_dbg(ab, ATH11K_DBG_MAC,
4852 		   "mac chanctx add freq %hu width %d ptr %pK\n",
4853 		   ctx->def.chan->center_freq, ctx->def.width, ctx);
4854 
4855 	mutex_lock(&ar->conf_mutex);
4856 
4857 	spin_lock_bh(&ar->data_lock);
4858 	/* TODO: In case of multiple channel context, populate rx_channel from
4859 	 * Rx PPDU desc information.
4860 	 */
4861 	ar->rx_channel = ctx->def.chan;
4862 	spin_unlock_bh(&ar->data_lock);
4863 
4864 	mutex_unlock(&ar->conf_mutex);
4865 
4866 	return 0;
4867 }
4868 
4869 static void ath11k_mac_op_remove_chanctx(struct ieee80211_hw *hw,
4870 					 struct ieee80211_chanctx_conf *ctx)
4871 {
4872 	struct ath11k *ar = hw->priv;
4873 	struct ath11k_base *ab = ar->ab;
4874 
4875 	ath11k_dbg(ab, ATH11K_DBG_MAC,
4876 		   "mac chanctx remove freq %hu width %d ptr %pK\n",
4877 		   ctx->def.chan->center_freq, ctx->def.width, ctx);
4878 
4879 	mutex_lock(&ar->conf_mutex);
4880 
4881 	spin_lock_bh(&ar->data_lock);
4882 	/* TODO: In case of there is one more channel context left, populate
4883 	 * rx_channel with the channel of that remaining channel context.
4884 	 */
4885 	ar->rx_channel = NULL;
4886 	spin_unlock_bh(&ar->data_lock);
4887 
4888 	mutex_unlock(&ar->conf_mutex);
4889 }
4890 
4891 static inline int ath11k_mac_vdev_setup_sync(struct ath11k *ar)
4892 {
4893 	lockdep_assert_held(&ar->conf_mutex);
4894 
4895 	if (test_bit(ATH11K_FLAG_CRASH_FLUSH, &ar->ab->dev_flags))
4896 		return -ESHUTDOWN;
4897 
4898 	if (!wait_for_completion_timeout(&ar->vdev_setup_done,
4899 					 ATH11K_VDEV_SETUP_TIMEOUT_HZ))
4900 		return -ETIMEDOUT;
4901 
4902 	return ar->last_wmi_vdev_start_status ? -EINVAL : 0;
4903 }
4904 
4905 static int
4906 ath11k_mac_vdev_start_restart(struct ath11k_vif *arvif,
4907 			      const struct cfg80211_chan_def *chandef,
4908 			      bool restart)
4909 {
4910 	struct ath11k *ar = arvif->ar;
4911 	struct ath11k_base *ab = ar->ab;
4912 	struct wmi_vdev_start_req_arg arg = {};
4913 	int he_support = arvif->vif->bss_conf.he_support;
4914 	int ret = 0;
4915 
4916 	lockdep_assert_held(&ar->conf_mutex);
4917 
4918 	reinit_completion(&ar->vdev_setup_done);
4919 
4920 	arg.vdev_id = arvif->vdev_id;
4921 	arg.dtim_period = arvif->dtim_period;
4922 	arg.bcn_intval = arvif->beacon_interval;
4923 
4924 	arg.channel.freq = chandef->chan->center_freq;
4925 	arg.channel.band_center_freq1 = chandef->center_freq1;
4926 	arg.channel.band_center_freq2 = chandef->center_freq2;
4927 	arg.channel.mode =
4928 		ath11k_phymodes[chandef->chan->band][chandef->width];
4929 
4930 	arg.channel.min_power = 0;
4931 	arg.channel.max_power = chandef->chan->max_power * 2;
4932 	arg.channel.max_reg_power = chandef->chan->max_reg_power * 2;
4933 	arg.channel.max_antenna_gain = chandef->chan->max_antenna_gain * 2;
4934 
4935 	arg.pref_tx_streams = ar->num_tx_chains;
4936 	arg.pref_rx_streams = ar->num_rx_chains;
4937 
4938 	if (arvif->vdev_type == WMI_VDEV_TYPE_AP) {
4939 		arg.ssid = arvif->u.ap.ssid;
4940 		arg.ssid_len = arvif->u.ap.ssid_len;
4941 		arg.hidden_ssid = arvif->u.ap.hidden_ssid;
4942 
4943 		/* For now allow DFS for AP mode */
4944 		arg.channel.chan_radar =
4945 			!!(chandef->chan->flags & IEEE80211_CHAN_RADAR);
4946 
4947 		arg.channel.passive = arg.channel.chan_radar;
4948 
4949 		spin_lock_bh(&ab->base_lock);
4950 		arg.regdomain = ar->ab->dfs_region;
4951 		spin_unlock_bh(&ab->base_lock);
4952 
4953 		/* TODO: Notify if secondary 80Mhz also needs radar detection */
4954 		if (he_support) {
4955 			ret = ath11k_set_he_mu_sounding_mode(ar, arvif);
4956 			if (ret) {
4957 				ath11k_warn(ar->ab, "failed to set he mode vdev %i\n",
4958 					    arg.vdev_id);
4959 				return ret;
4960 			}
4961 		}
4962 	}
4963 
4964 	arg.channel.passive |= !!(chandef->chan->flags & IEEE80211_CHAN_NO_IR);
4965 
4966 	ath11k_dbg(ab, ATH11K_DBG_MAC,
4967 		   "mac vdev %d start center_freq %d phymode %s\n",
4968 		   arg.vdev_id, arg.channel.freq,
4969 		   ath11k_wmi_phymode_str(arg.channel.mode));
4970 
4971 	ret = ath11k_wmi_vdev_start(ar, &arg, restart);
4972 	if (ret) {
4973 		ath11k_warn(ar->ab, "failed to %s WMI vdev %i\n",
4974 			    restart ? "restart" : "start", arg.vdev_id);
4975 		return ret;
4976 	}
4977 
4978 	ret = ath11k_mac_vdev_setup_sync(ar);
4979 	if (ret) {
4980 		ath11k_warn(ab, "failed to synchronize setup for vdev %i %s: %d\n",
4981 			    arg.vdev_id, restart ? "restart" : "start", ret);
4982 		return ret;
4983 	}
4984 
4985 	ar->num_started_vdevs++;
4986 	ath11k_dbg(ab, ATH11K_DBG_MAC,  "vdev %pM started, vdev_id %d\n",
4987 		   arvif->vif->addr, arvif->vdev_id);
4988 
4989 	/* Enable CAC Flag in the driver by checking the channel DFS cac time,
4990 	 * i.e dfs_cac_ms value which will be valid only for radar channels
4991 	 * and state as NL80211_DFS_USABLE which indicates CAC needs to be
4992 	 * done before channel usage. This flags is used to drop rx packets.
4993 	 * during CAC.
4994 	 */
4995 	/* TODO Set the flag for other interface types as required */
4996 	if (arvif->vdev_type == WMI_VDEV_TYPE_AP &&
4997 	    chandef->chan->dfs_cac_ms &&
4998 	    chandef->chan->dfs_state == NL80211_DFS_USABLE) {
4999 		set_bit(ATH11K_CAC_RUNNING, &ar->dev_flags);
5000 		ath11k_dbg(ab, ATH11K_DBG_MAC,
5001 			   "CAC Started in chan_freq %d for vdev %d\n",
5002 			   arg.channel.freq, arg.vdev_id);
5003 	}
5004 
5005 	ret = ath11k_mac_set_txbf_conf(arvif);
5006 	if (ret)
5007 		ath11k_warn(ab, "failed to set txbf conf for vdev %d: %d\n",
5008 			    arvif->vdev_id, ret);
5009 
5010 	return 0;
5011 }
5012 
5013 static int ath11k_mac_vdev_stop(struct ath11k_vif *arvif)
5014 {
5015 	struct ath11k *ar = arvif->ar;
5016 	int ret;
5017 
5018 	lockdep_assert_held(&ar->conf_mutex);
5019 
5020 	reinit_completion(&ar->vdev_setup_done);
5021 
5022 	ret = ath11k_wmi_vdev_stop(ar, arvif->vdev_id);
5023 	if (ret) {
5024 		ath11k_warn(ar->ab, "failed to stop WMI vdev %i: %d\n",
5025 			    arvif->vdev_id, ret);
5026 		goto err;
5027 	}
5028 
5029 	ret = ath11k_mac_vdev_setup_sync(ar);
5030 	if (ret) {
5031 		ath11k_warn(ar->ab, "failed to synchronize setup for vdev %i: %d\n",
5032 			    arvif->vdev_id, ret);
5033 		goto err;
5034 	}
5035 
5036 	WARN_ON(ar->num_started_vdevs == 0);
5037 
5038 	ar->num_started_vdevs--;
5039 	ath11k_dbg(ar->ab, ATH11K_DBG_MAC, "vdev %pM stopped, vdev_id %d\n",
5040 		   arvif->vif->addr, arvif->vdev_id);
5041 
5042 	if (test_bit(ATH11K_CAC_RUNNING, &ar->dev_flags)) {
5043 		clear_bit(ATH11K_CAC_RUNNING, &ar->dev_flags);
5044 		ath11k_dbg(ar->ab, ATH11K_DBG_MAC, "CAC Stopped for vdev %d\n",
5045 			   arvif->vdev_id);
5046 	}
5047 
5048 	return 0;
5049 err:
5050 	return ret;
5051 }
5052 
5053 static int ath11k_mac_vdev_start(struct ath11k_vif *arvif,
5054 				 const struct cfg80211_chan_def *chandef)
5055 {
5056 	return ath11k_mac_vdev_start_restart(arvif, chandef, false);
5057 }
5058 
5059 static int ath11k_mac_vdev_restart(struct ath11k_vif *arvif,
5060 				   const struct cfg80211_chan_def *chandef)
5061 {
5062 	return ath11k_mac_vdev_start_restart(arvif, chandef, true);
5063 }
5064 
5065 struct ath11k_mac_change_chanctx_arg {
5066 	struct ieee80211_chanctx_conf *ctx;
5067 	struct ieee80211_vif_chanctx_switch *vifs;
5068 	int n_vifs;
5069 	int next_vif;
5070 };
5071 
5072 static void
5073 ath11k_mac_change_chanctx_cnt_iter(void *data, u8 *mac,
5074 				   struct ieee80211_vif *vif)
5075 {
5076 	struct ath11k_mac_change_chanctx_arg *arg = data;
5077 
5078 	if (rcu_access_pointer(vif->chanctx_conf) != arg->ctx)
5079 		return;
5080 
5081 	arg->n_vifs++;
5082 }
5083 
5084 static void
5085 ath11k_mac_change_chanctx_fill_iter(void *data, u8 *mac,
5086 				    struct ieee80211_vif *vif)
5087 {
5088 	struct ath11k_mac_change_chanctx_arg *arg = data;
5089 	struct ieee80211_chanctx_conf *ctx;
5090 
5091 	ctx = rcu_access_pointer(vif->chanctx_conf);
5092 	if (ctx != arg->ctx)
5093 		return;
5094 
5095 	if (WARN_ON(arg->next_vif == arg->n_vifs))
5096 		return;
5097 
5098 	arg->vifs[arg->next_vif].vif = vif;
5099 	arg->vifs[arg->next_vif].old_ctx = ctx;
5100 	arg->vifs[arg->next_vif].new_ctx = ctx;
5101 	arg->next_vif++;
5102 }
5103 
5104 static void
5105 ath11k_mac_update_vif_chan(struct ath11k *ar,
5106 			   struct ieee80211_vif_chanctx_switch *vifs,
5107 			   int n_vifs)
5108 {
5109 	struct ath11k_base *ab = ar->ab;
5110 	struct ath11k_vif *arvif;
5111 	int ret;
5112 	int i;
5113 
5114 	lockdep_assert_held(&ar->conf_mutex);
5115 
5116 	for (i = 0; i < n_vifs; i++) {
5117 		arvif = (void *)vifs[i].vif->drv_priv;
5118 
5119 		ath11k_dbg(ab, ATH11K_DBG_MAC,
5120 			   "mac chanctx switch vdev_id %i freq %hu->%hu width %d->%d\n",
5121 			   arvif->vdev_id,
5122 			   vifs[i].old_ctx->def.chan->center_freq,
5123 			   vifs[i].new_ctx->def.chan->center_freq,
5124 			   vifs[i].old_ctx->def.width,
5125 			   vifs[i].new_ctx->def.width);
5126 
5127 		if (WARN_ON(!arvif->is_started))
5128 			continue;
5129 
5130 		if (WARN_ON(!arvif->is_up))
5131 			continue;
5132 
5133 		ret = ath11k_wmi_vdev_down(ar, arvif->vdev_id);
5134 		if (ret) {
5135 			ath11k_warn(ab, "failed to down vdev %d: %d\n",
5136 				    arvif->vdev_id, ret);
5137 			continue;
5138 		}
5139 	}
5140 
5141 	/* All relevant vdevs are downed and associated channel resources
5142 	 * should be available for the channel switch now.
5143 	 */
5144 
5145 	/* TODO: Update ar->rx_channel */
5146 
5147 	for (i = 0; i < n_vifs; i++) {
5148 		arvif = (void *)vifs[i].vif->drv_priv;
5149 
5150 		if (WARN_ON(!arvif->is_started))
5151 			continue;
5152 
5153 		if (WARN_ON(!arvif->is_up))
5154 			continue;
5155 
5156 		ret = ath11k_mac_setup_bcn_tmpl(arvif);
5157 		if (ret)
5158 			ath11k_warn(ab, "failed to update bcn tmpl during csa: %d\n",
5159 				    ret);
5160 
5161 		ret = ath11k_mac_vdev_restart(arvif, &vifs[i].new_ctx->def);
5162 		if (ret) {
5163 			ath11k_warn(ab, "failed to restart vdev %d: %d\n",
5164 				    arvif->vdev_id, ret);
5165 			continue;
5166 		}
5167 
5168 		ret = ath11k_wmi_vdev_up(arvif->ar, arvif->vdev_id, arvif->aid,
5169 					 arvif->bssid);
5170 		if (ret) {
5171 			ath11k_warn(ab, "failed to bring vdev up %d: %d\n",
5172 				    arvif->vdev_id, ret);
5173 			continue;
5174 		}
5175 	}
5176 }
5177 
5178 static void
5179 ath11k_mac_update_active_vif_chan(struct ath11k *ar,
5180 				  struct ieee80211_chanctx_conf *ctx)
5181 {
5182 	struct ath11k_mac_change_chanctx_arg arg = { .ctx = ctx };
5183 
5184 	lockdep_assert_held(&ar->conf_mutex);
5185 
5186 	ieee80211_iterate_active_interfaces_atomic(ar->hw,
5187 						   IEEE80211_IFACE_ITER_NORMAL,
5188 						   ath11k_mac_change_chanctx_cnt_iter,
5189 						   &arg);
5190 	if (arg.n_vifs == 0)
5191 		return;
5192 
5193 	arg.vifs = kcalloc(arg.n_vifs, sizeof(arg.vifs[0]), GFP_KERNEL);
5194 	if (!arg.vifs)
5195 		return;
5196 
5197 	ieee80211_iterate_active_interfaces_atomic(ar->hw,
5198 						   IEEE80211_IFACE_ITER_NORMAL,
5199 						   ath11k_mac_change_chanctx_fill_iter,
5200 						   &arg);
5201 
5202 	ath11k_mac_update_vif_chan(ar, arg.vifs, arg.n_vifs);
5203 
5204 	kfree(arg.vifs);
5205 }
5206 
5207 static void ath11k_mac_op_change_chanctx(struct ieee80211_hw *hw,
5208 					 struct ieee80211_chanctx_conf *ctx,
5209 					 u32 changed)
5210 {
5211 	struct ath11k *ar = hw->priv;
5212 	struct ath11k_base *ab = ar->ab;
5213 
5214 	mutex_lock(&ar->conf_mutex);
5215 
5216 	ath11k_dbg(ab, ATH11K_DBG_MAC,
5217 		   "mac chanctx change freq %hu width %d ptr %pK changed %x\n",
5218 		   ctx->def.chan->center_freq, ctx->def.width, ctx, changed);
5219 
5220 	/* This shouldn't really happen because channel switching should use
5221 	 * switch_vif_chanctx().
5222 	 */
5223 	if (WARN_ON(changed & IEEE80211_CHANCTX_CHANGE_CHANNEL))
5224 		goto unlock;
5225 
5226 	if (changed & IEEE80211_CHANCTX_CHANGE_WIDTH)
5227 		ath11k_mac_update_active_vif_chan(ar, ctx);
5228 
5229 	/* TODO: Recalc radar detection */
5230 
5231 unlock:
5232 	mutex_unlock(&ar->conf_mutex);
5233 }
5234 
5235 static int ath11k_start_vdev_delay(struct ieee80211_hw *hw,
5236 				   struct ieee80211_vif *vif)
5237 {
5238 	struct ath11k *ar = hw->priv;
5239 	struct ath11k_base *ab = ar->ab;
5240 	struct ath11k_vif *arvif = (void *)vif->drv_priv;
5241 	int ret;
5242 
5243 	if (WARN_ON(arvif->is_started))
5244 		return -EBUSY;
5245 
5246 	ret = ath11k_mac_vdev_start(arvif, &arvif->chanctx.def);
5247 	if (ret) {
5248 		ath11k_warn(ab, "failed to start vdev %i addr %pM on freq %d: %d\n",
5249 			    arvif->vdev_id, vif->addr,
5250 			    arvif->chanctx.def.chan->center_freq, ret);
5251 		return ret;
5252 	}
5253 
5254 	if (arvif->vdev_type == WMI_VDEV_TYPE_MONITOR) {
5255 		ret = ath11k_monitor_vdev_up(ar, arvif->vdev_id);
5256 		if (ret) {
5257 			ath11k_warn(ab, "failed put monitor up: %d\n", ret);
5258 			return ret;
5259 		}
5260 	}
5261 
5262 	arvif->is_started = true;
5263 
5264 	/* TODO: Setup ps and cts/rts protection */
5265 	return 0;
5266 }
5267 
5268 static int
5269 ath11k_mac_op_assign_vif_chanctx(struct ieee80211_hw *hw,
5270 				 struct ieee80211_vif *vif,
5271 				 struct ieee80211_chanctx_conf *ctx)
5272 {
5273 	struct ath11k *ar = hw->priv;
5274 	struct ath11k_base *ab = ar->ab;
5275 	struct ath11k_vif *arvif = (void *)vif->drv_priv;
5276 	int ret;
5277 	struct peer_create_params param;
5278 
5279 	mutex_lock(&ar->conf_mutex);
5280 
5281 	ath11k_dbg(ab, ATH11K_DBG_MAC,
5282 		   "mac chanctx assign ptr %pK vdev_id %i\n",
5283 		   ctx, arvif->vdev_id);
5284 
5285 	/* for QCA6390 bss peer must be created before vdev_start */
5286 	if (ab->hw_params.vdev_start_delay &&
5287 	    arvif->vdev_type != WMI_VDEV_TYPE_AP &&
5288 	    arvif->vdev_type != WMI_VDEV_TYPE_MONITOR &&
5289 	    !ath11k_peer_find_by_vdev_id(ab, arvif->vdev_id)) {
5290 		memcpy(&arvif->chanctx, ctx, sizeof(*ctx));
5291 		ret = 0;
5292 		goto out;
5293 	}
5294 
5295 	if (WARN_ON(arvif->is_started)) {
5296 		ret = -EBUSY;
5297 		goto out;
5298 	}
5299 
5300 	if (ab->hw_params.vdev_start_delay &&
5301 	    (arvif->vdev_type == WMI_VDEV_TYPE_AP ||
5302 	    arvif->vdev_type == WMI_VDEV_TYPE_MONITOR)) {
5303 		param.vdev_id = arvif->vdev_id;
5304 		param.peer_type = WMI_PEER_TYPE_DEFAULT;
5305 		param.peer_addr = ar->mac_addr;
5306 
5307 		ret = ath11k_peer_create(ar, arvif, NULL, &param);
5308 		if (ret) {
5309 			ath11k_warn(ab, "failed to create peer after vdev start delay: %d",
5310 				    ret);
5311 			goto out;
5312 		}
5313 	}
5314 
5315 	ret = ath11k_mac_vdev_start(arvif, &ctx->def);
5316 	if (ret) {
5317 		ath11k_warn(ab, "failed to start vdev %i addr %pM on freq %d: %d\n",
5318 			    arvif->vdev_id, vif->addr,
5319 			    ctx->def.chan->center_freq, ret);
5320 		goto out;
5321 	}
5322 	if (arvif->vdev_type == WMI_VDEV_TYPE_MONITOR) {
5323 		ret = ath11k_monitor_vdev_up(ar, arvif->vdev_id);
5324 		if (ret)
5325 			goto out;
5326 	}
5327 
5328 	arvif->is_started = true;
5329 
5330 	/* TODO: Setup ps and cts/rts protection */
5331 
5332 	ret = 0;
5333 
5334 out:
5335 	mutex_unlock(&ar->conf_mutex);
5336 
5337 	return ret;
5338 }
5339 
5340 static void
5341 ath11k_mac_op_unassign_vif_chanctx(struct ieee80211_hw *hw,
5342 				   struct ieee80211_vif *vif,
5343 				   struct ieee80211_chanctx_conf *ctx)
5344 {
5345 	struct ath11k *ar = hw->priv;
5346 	struct ath11k_base *ab = ar->ab;
5347 	struct ath11k_vif *arvif = (void *)vif->drv_priv;
5348 	int ret;
5349 
5350 	mutex_lock(&ar->conf_mutex);
5351 
5352 	ath11k_dbg(ab, ATH11K_DBG_MAC,
5353 		   "mac chanctx unassign ptr %pK vdev_id %i\n",
5354 		   ctx, arvif->vdev_id);
5355 
5356 	WARN_ON(!arvif->is_started);
5357 
5358 	if (ab->hw_params.vdev_start_delay &&
5359 	    arvif->vdev_type == WMI_VDEV_TYPE_MONITOR &&
5360 	    ath11k_peer_find_by_addr(ab, ar->mac_addr))
5361 		ath11k_peer_delete(ar, arvif->vdev_id, ar->mac_addr);
5362 
5363 	ret = ath11k_mac_vdev_stop(arvif);
5364 	if (ret)
5365 		ath11k_warn(ab, "failed to stop vdev %i: %d\n",
5366 			    arvif->vdev_id, ret);
5367 
5368 	arvif->is_started = false;
5369 
5370 	if (ab->hw_params.vdev_start_delay &&
5371 	    arvif->vdev_type == WMI_VDEV_TYPE_MONITOR)
5372 		ath11k_wmi_vdev_down(ar, arvif->vdev_id);
5373 
5374 	mutex_unlock(&ar->conf_mutex);
5375 }
5376 
5377 static int
5378 ath11k_mac_op_switch_vif_chanctx(struct ieee80211_hw *hw,
5379 				 struct ieee80211_vif_chanctx_switch *vifs,
5380 				 int n_vifs,
5381 				 enum ieee80211_chanctx_switch_mode mode)
5382 {
5383 	struct ath11k *ar = hw->priv;
5384 
5385 	mutex_lock(&ar->conf_mutex);
5386 
5387 	ath11k_dbg(ar->ab, ATH11K_DBG_MAC,
5388 		   "mac chanctx switch n_vifs %d mode %d\n",
5389 		   n_vifs, mode);
5390 	ath11k_mac_update_vif_chan(ar, vifs, n_vifs);
5391 
5392 	mutex_unlock(&ar->conf_mutex);
5393 
5394 	return 0;
5395 }
5396 
5397 static int
5398 ath11k_set_vdev_param_to_all_vifs(struct ath11k *ar, int param, u32 value)
5399 {
5400 	struct ath11k_vif *arvif;
5401 	int ret = 0;
5402 
5403 	mutex_lock(&ar->conf_mutex);
5404 	list_for_each_entry(arvif, &ar->arvifs, list) {
5405 		ath11k_dbg(ar->ab, ATH11K_DBG_MAC, "setting mac vdev %d param %d value %d\n",
5406 			   param, arvif->vdev_id, value);
5407 
5408 		ret = ath11k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
5409 						    param, value);
5410 		if (ret) {
5411 			ath11k_warn(ar->ab, "failed to set param %d for vdev %d: %d\n",
5412 				    param, arvif->vdev_id, ret);
5413 			break;
5414 		}
5415 	}
5416 	mutex_unlock(&ar->conf_mutex);
5417 	return ret;
5418 }
5419 
5420 /* mac80211 stores device specific RTS/Fragmentation threshold value,
5421  * this is set interface specific to firmware from ath11k driver
5422  */
5423 static int ath11k_mac_op_set_rts_threshold(struct ieee80211_hw *hw, u32 value)
5424 {
5425 	struct ath11k *ar = hw->priv;
5426 	int param_id = WMI_VDEV_PARAM_RTS_THRESHOLD;
5427 
5428 	return ath11k_set_vdev_param_to_all_vifs(ar, param_id, value);
5429 }
5430 
5431 static int ath11k_mac_op_set_frag_threshold(struct ieee80211_hw *hw, u32 value)
5432 {
5433 	/* Even though there's a WMI vdev param for fragmentation threshold no
5434 	 * known firmware actually implements it. Moreover it is not possible to
5435 	 * rely frame fragmentation to mac80211 because firmware clears the
5436 	 * "more fragments" bit in frame control making it impossible for remote
5437 	 * devices to reassemble frames.
5438 	 *
5439 	 * Hence implement a dummy callback just to say fragmentation isn't
5440 	 * supported. This effectively prevents mac80211 from doing frame
5441 	 * fragmentation in software.
5442 	 */
5443 	return -EOPNOTSUPP;
5444 }
5445 
5446 static void ath11k_mac_op_flush(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
5447 				u32 queues, bool drop)
5448 {
5449 	struct ath11k *ar = hw->priv;
5450 	long time_left;
5451 
5452 	if (drop)
5453 		return;
5454 
5455 	time_left = wait_event_timeout(ar->dp.tx_empty_waitq,
5456 				       (atomic_read(&ar->dp.num_tx_pending) == 0),
5457 				       ATH11K_FLUSH_TIMEOUT);
5458 	if (time_left == 0)
5459 		ath11k_warn(ar->ab, "failed to flush transmit queue %ld\n", time_left);
5460 }
5461 
5462 static int
5463 ath11k_mac_bitrate_mask_num_ht_rates(struct ath11k *ar,
5464 				     enum nl80211_band band,
5465 				     const struct cfg80211_bitrate_mask *mask)
5466 {
5467 	int num_rates = 0;
5468 	int i;
5469 
5470 	for (i = 0; i < ARRAY_SIZE(mask->control[band].ht_mcs); i++)
5471 		num_rates += hweight16(mask->control[band].ht_mcs[i]);
5472 
5473 	return num_rates;
5474 }
5475 
5476 static bool
5477 ath11k_mac_has_single_legacy_rate(struct ath11k *ar,
5478 				  enum nl80211_band band,
5479 				  const struct cfg80211_bitrate_mask *mask)
5480 {
5481 	int num_rates = 0;
5482 
5483 	num_rates = hweight32(mask->control[band].legacy);
5484 
5485 	if (ath11k_mac_bitrate_mask_num_ht_rates(ar, band, mask))
5486 		return false;
5487 
5488 	if (ath11k_mac_bitrate_mask_num_vht_rates(ar, band, mask))
5489 		return false;
5490 
5491 	return num_rates == 1;
5492 }
5493 
5494 static bool
5495 ath11k_mac_bitrate_mask_get_single_nss(struct ath11k *ar,
5496 				       enum nl80211_band band,
5497 				       const struct cfg80211_bitrate_mask *mask,
5498 				       int *nss)
5499 {
5500 	struct ieee80211_supported_band *sband = &ar->mac.sbands[band];
5501 	u16 vht_mcs_map = le16_to_cpu(sband->vht_cap.vht_mcs.tx_mcs_map);
5502 	u8 ht_nss_mask = 0;
5503 	u8 vht_nss_mask = 0;
5504 	int i;
5505 
5506 	/* No need to consider legacy here. Basic rates are always present
5507 	 * in bitrate mask
5508 	 */
5509 
5510 	for (i = 0; i < ARRAY_SIZE(mask->control[band].ht_mcs); i++) {
5511 		if (mask->control[band].ht_mcs[i] == 0)
5512 			continue;
5513 		else if (mask->control[band].ht_mcs[i] ==
5514 			 sband->ht_cap.mcs.rx_mask[i])
5515 			ht_nss_mask |= BIT(i);
5516 		else
5517 			return false;
5518 	}
5519 
5520 	for (i = 0; i < ARRAY_SIZE(mask->control[band].vht_mcs); i++) {
5521 		if (mask->control[band].vht_mcs[i] == 0)
5522 			continue;
5523 		else if (mask->control[band].vht_mcs[i] ==
5524 			 ath11k_mac_get_max_vht_mcs_map(vht_mcs_map, i))
5525 			vht_nss_mask |= BIT(i);
5526 		else
5527 			return false;
5528 	}
5529 
5530 	if (ht_nss_mask != vht_nss_mask)
5531 		return false;
5532 
5533 	if (ht_nss_mask == 0)
5534 		return false;
5535 
5536 	if (BIT(fls(ht_nss_mask)) - 1 != ht_nss_mask)
5537 		return false;
5538 
5539 	*nss = fls(ht_nss_mask);
5540 
5541 	return true;
5542 }
5543 
5544 static int
5545 ath11k_mac_get_single_legacy_rate(struct ath11k *ar,
5546 				  enum nl80211_band band,
5547 				  const struct cfg80211_bitrate_mask *mask,
5548 				  u32 *rate, u8 *nss)
5549 {
5550 	int rate_idx;
5551 	u16 bitrate;
5552 	u8 preamble;
5553 	u8 hw_rate;
5554 
5555 	if (hweight32(mask->control[band].legacy) != 1)
5556 		return -EINVAL;
5557 
5558 	rate_idx = ffs(mask->control[band].legacy) - 1;
5559 
5560 	if (band == NL80211_BAND_5GHZ || band == NL80211_BAND_6GHZ)
5561 		rate_idx += ATH11K_MAC_FIRST_OFDM_RATE_IDX;
5562 
5563 	hw_rate = ath11k_legacy_rates[rate_idx].hw_value;
5564 	bitrate = ath11k_legacy_rates[rate_idx].bitrate;
5565 
5566 	if (ath11k_mac_bitrate_is_cck(bitrate))
5567 		preamble = WMI_RATE_PREAMBLE_CCK;
5568 	else
5569 		preamble = WMI_RATE_PREAMBLE_OFDM;
5570 
5571 	*nss = 1;
5572 	*rate = ATH11K_HW_RATE_CODE(hw_rate, 0, preamble);
5573 
5574 	return 0;
5575 }
5576 
5577 static int ath11k_mac_set_fixed_rate_params(struct ath11k_vif *arvif,
5578 					    u32 rate, u8 nss, u8 sgi, u8 ldpc)
5579 {
5580 	struct ath11k *ar = arvif->ar;
5581 	u32 vdev_param;
5582 	int ret;
5583 
5584 	lockdep_assert_held(&ar->conf_mutex);
5585 
5586 	ath11k_dbg(ar->ab, ATH11K_DBG_MAC, "mac set fixed rate params vdev %i rate 0x%02hhx nss %hhu sgi %hhu\n",
5587 		   arvif->vdev_id, rate, nss, sgi);
5588 
5589 	vdev_param = WMI_VDEV_PARAM_FIXED_RATE;
5590 	ret = ath11k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
5591 					    vdev_param, rate);
5592 	if (ret) {
5593 		ath11k_warn(ar->ab, "failed to set fixed rate param 0x%02x: %d\n",
5594 			    rate, ret);
5595 		return ret;
5596 	}
5597 
5598 	vdev_param = WMI_VDEV_PARAM_NSS;
5599 	ret = ath11k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
5600 					    vdev_param, nss);
5601 	if (ret) {
5602 		ath11k_warn(ar->ab, "failed to set nss param %d: %d\n",
5603 			    nss, ret);
5604 		return ret;
5605 	}
5606 
5607 	vdev_param = WMI_VDEV_PARAM_SGI;
5608 	ret = ath11k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
5609 					    vdev_param, sgi);
5610 	if (ret) {
5611 		ath11k_warn(ar->ab, "failed to set sgi param %d: %d\n",
5612 			    sgi, ret);
5613 		return ret;
5614 	}
5615 
5616 	vdev_param = WMI_VDEV_PARAM_LDPC;
5617 	ret = ath11k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
5618 					    vdev_param, ldpc);
5619 	if (ret) {
5620 		ath11k_warn(ar->ab, "failed to set ldpc param %d: %d\n",
5621 			    ldpc, ret);
5622 		return ret;
5623 	}
5624 
5625 	return 0;
5626 }
5627 
5628 static bool
5629 ath11k_mac_vht_mcs_range_present(struct ath11k *ar,
5630 				 enum nl80211_band band,
5631 				 const struct cfg80211_bitrate_mask *mask)
5632 {
5633 	int i;
5634 	u16 vht_mcs;
5635 
5636 	for (i = 0; i < NL80211_VHT_NSS_MAX; i++) {
5637 		vht_mcs = mask->control[band].vht_mcs[i];
5638 
5639 		switch (vht_mcs) {
5640 		case 0:
5641 		case BIT(8) - 1:
5642 		case BIT(9) - 1:
5643 		case BIT(10) - 1:
5644 			break;
5645 		default:
5646 			return false;
5647 		}
5648 	}
5649 
5650 	return true;
5651 }
5652 
5653 static void ath11k_mac_set_bitrate_mask_iter(void *data,
5654 					     struct ieee80211_sta *sta)
5655 {
5656 	struct ath11k_vif *arvif = data;
5657 	struct ath11k_sta *arsta = (struct ath11k_sta *)sta->drv_priv;
5658 	struct ath11k *ar = arvif->ar;
5659 
5660 	spin_lock_bh(&ar->data_lock);
5661 	arsta->changed |= IEEE80211_RC_SUPP_RATES_CHANGED;
5662 	spin_unlock_bh(&ar->data_lock);
5663 
5664 	ieee80211_queue_work(ar->hw, &arsta->update_wk);
5665 }
5666 
5667 static void ath11k_mac_disable_peer_fixed_rate(void *data,
5668 					       struct ieee80211_sta *sta)
5669 {
5670 	struct ath11k_vif *arvif = data;
5671 	struct ath11k *ar = arvif->ar;
5672 	int ret;
5673 
5674 	ret = ath11k_wmi_set_peer_param(ar, sta->addr,
5675 					arvif->vdev_id,
5676 					WMI_PEER_PARAM_FIXED_RATE,
5677 					WMI_FIXED_RATE_NONE);
5678 	if (ret)
5679 		ath11k_warn(ar->ab,
5680 			    "failed to disable peer fixed rate for STA %pM ret %d\n",
5681 			    sta->addr, ret);
5682 }
5683 
5684 static int
5685 ath11k_mac_op_set_bitrate_mask(struct ieee80211_hw *hw,
5686 			       struct ieee80211_vif *vif,
5687 			       const struct cfg80211_bitrate_mask *mask)
5688 {
5689 	struct ath11k_vif *arvif = (void *)vif->drv_priv;
5690 	struct cfg80211_chan_def def;
5691 	struct ath11k *ar = arvif->ar;
5692 	enum nl80211_band band;
5693 	const u8 *ht_mcs_mask;
5694 	const u16 *vht_mcs_mask;
5695 	u32 rate;
5696 	u8 nss;
5697 	u8 sgi;
5698 	u8 ldpc;
5699 	int single_nss;
5700 	int ret;
5701 	int num_rates;
5702 
5703 	if (ath11k_mac_vif_chan(vif, &def))
5704 		return -EPERM;
5705 
5706 	band = def.chan->band;
5707 	ht_mcs_mask = mask->control[band].ht_mcs;
5708 	vht_mcs_mask = mask->control[band].vht_mcs;
5709 	ldpc = !!(ar->ht_cap_info & WMI_HT_CAP_LDPC);
5710 
5711 	sgi = mask->control[band].gi;
5712 	if (sgi == NL80211_TXRATE_FORCE_LGI)
5713 		return -EINVAL;
5714 
5715 	/* mac80211 doesn't support sending a fixed HT/VHT MCS alone, rather it
5716 	 * requires passing atleast one of used basic rates along with them.
5717 	 * Fixed rate setting across different preambles(legacy, HT, VHT) is
5718 	 * not supported by the FW. Hence use of FIXED_RATE vdev param is not
5719 	 * suitable for setting single HT/VHT rates.
5720 	 * But, there could be a single basic rate passed from userspace which
5721 	 * can be done through the FIXED_RATE param.
5722 	 */
5723 	if (ath11k_mac_has_single_legacy_rate(ar, band, mask)) {
5724 		ret = ath11k_mac_get_single_legacy_rate(ar, band, mask, &rate,
5725 							&nss);
5726 		if (ret) {
5727 			ath11k_warn(ar->ab, "failed to get single legacy rate for vdev %i: %d\n",
5728 				    arvif->vdev_id, ret);
5729 			return ret;
5730 		}
5731 		ieee80211_iterate_stations_atomic(ar->hw,
5732 						  ath11k_mac_disable_peer_fixed_rate,
5733 						  arvif);
5734 	} else if (ath11k_mac_bitrate_mask_get_single_nss(ar, band, mask,
5735 							  &single_nss)) {
5736 		rate = WMI_FIXED_RATE_NONE;
5737 		nss = single_nss;
5738 	} else {
5739 		rate = WMI_FIXED_RATE_NONE;
5740 		nss = min_t(u32, ar->num_tx_chains,
5741 			    max(ath11k_mac_max_ht_nss(ht_mcs_mask),
5742 				ath11k_mac_max_vht_nss(vht_mcs_mask)));
5743 
5744 		/* If multiple rates across different preambles are given
5745 		 * we can reconfigure this info with all peers using PEER_ASSOC
5746 		 * command with the below exception cases.
5747 		 * - Single VHT Rate : peer_assoc command accommodates only MCS
5748 		 * range values i.e 0-7, 0-8, 0-9 for VHT. Though mac80211
5749 		 * mandates passing basic rates along with HT/VHT rates, FW
5750 		 * doesn't allow switching from VHT to Legacy. Hence instead of
5751 		 * setting legacy and VHT rates using RATEMASK_CMD vdev cmd,
5752 		 * we could set this VHT rate as peer fixed rate param, which
5753 		 * will override FIXED rate and FW rate control algorithm.
5754 		 * If single VHT rate is passed along with HT rates, we select
5755 		 * the VHT rate as fixed rate for vht peers.
5756 		 * - Multiple VHT Rates : When Multiple VHT rates are given,this
5757 		 * can be set using RATEMASK CMD which uses FW rate-ctl alg.
5758 		 * TODO: Setting multiple VHT MCS and replacing peer_assoc with
5759 		 * RATEMASK_CMDID can cover all use cases of setting rates
5760 		 * across multiple preambles and rates within same type.
5761 		 * But requires more validation of the command at this point.
5762 		 */
5763 
5764 		num_rates = ath11k_mac_bitrate_mask_num_vht_rates(ar, band,
5765 								  mask);
5766 
5767 		if (!ath11k_mac_vht_mcs_range_present(ar, band, mask) &&
5768 		    num_rates > 1) {
5769 			/* TODO: Handle multiple VHT MCS values setting using
5770 			 * RATEMASK CMD
5771 			 */
5772 			ath11k_warn(ar->ab,
5773 				    "Setting more than one MCS Value in bitrate mask not supported\n");
5774 			return -EINVAL;
5775 		}
5776 
5777 		ieee80211_iterate_stations_atomic(ar->hw,
5778 						  ath11k_mac_disable_peer_fixed_rate,
5779 						  arvif);
5780 
5781 		mutex_lock(&ar->conf_mutex);
5782 
5783 		arvif->bitrate_mask = *mask;
5784 		ieee80211_iterate_stations_atomic(ar->hw,
5785 						  ath11k_mac_set_bitrate_mask_iter,
5786 						  arvif);
5787 
5788 		mutex_unlock(&ar->conf_mutex);
5789 	}
5790 
5791 	mutex_lock(&ar->conf_mutex);
5792 
5793 	ret = ath11k_mac_set_fixed_rate_params(arvif, rate, nss, sgi, ldpc);
5794 	if (ret) {
5795 		ath11k_warn(ar->ab, "failed to set fixed rate params on vdev %i: %d\n",
5796 			    arvif->vdev_id, ret);
5797 	}
5798 
5799 	mutex_unlock(&ar->conf_mutex);
5800 
5801 	return ret;
5802 }
5803 
5804 static void
5805 ath11k_mac_op_reconfig_complete(struct ieee80211_hw *hw,
5806 				enum ieee80211_reconfig_type reconfig_type)
5807 {
5808 	struct ath11k *ar = hw->priv;
5809 
5810 	if (reconfig_type != IEEE80211_RECONFIG_TYPE_RESTART)
5811 		return;
5812 
5813 	mutex_lock(&ar->conf_mutex);
5814 
5815 	if (ar->state == ATH11K_STATE_RESTARTED) {
5816 		ath11k_warn(ar->ab, "pdev %d successfully recovered\n",
5817 			    ar->pdev->pdev_id);
5818 		ar->state = ATH11K_STATE_ON;
5819 		ieee80211_wake_queues(ar->hw);
5820 	}
5821 
5822 	mutex_unlock(&ar->conf_mutex);
5823 }
5824 
5825 static void
5826 ath11k_mac_update_bss_chan_survey(struct ath11k *ar,
5827 				  struct ieee80211_channel *channel)
5828 {
5829 	int ret;
5830 	enum wmi_bss_chan_info_req_type type = WMI_BSS_SURVEY_REQ_TYPE_READ;
5831 
5832 	lockdep_assert_held(&ar->conf_mutex);
5833 
5834 	if (!test_bit(WMI_TLV_SERVICE_BSS_CHANNEL_INFO_64, ar->ab->wmi_ab.svc_map) ||
5835 	    ar->rx_channel != channel)
5836 		return;
5837 
5838 	if (ar->scan.state != ATH11K_SCAN_IDLE) {
5839 		ath11k_dbg(ar->ab, ATH11K_DBG_MAC,
5840 			   "ignoring bss chan info req while scanning..\n");
5841 		return;
5842 	}
5843 
5844 	reinit_completion(&ar->bss_survey_done);
5845 
5846 	ret = ath11k_wmi_pdev_bss_chan_info_request(ar, type);
5847 	if (ret) {
5848 		ath11k_warn(ar->ab, "failed to send pdev bss chan info request\n");
5849 		return;
5850 	}
5851 
5852 	ret = wait_for_completion_timeout(&ar->bss_survey_done, 3 * HZ);
5853 	if (ret == 0)
5854 		ath11k_warn(ar->ab, "bss channel survey timed out\n");
5855 }
5856 
5857 static int ath11k_mac_op_get_survey(struct ieee80211_hw *hw, int idx,
5858 				    struct survey_info *survey)
5859 {
5860 	struct ath11k *ar = hw->priv;
5861 	struct ieee80211_supported_band *sband;
5862 	struct survey_info *ar_survey;
5863 	int ret = 0;
5864 
5865 	if (idx >= ATH11K_NUM_CHANS)
5866 		return -ENOENT;
5867 
5868 	ar_survey = &ar->survey[idx];
5869 
5870 	mutex_lock(&ar->conf_mutex);
5871 
5872 	sband = hw->wiphy->bands[NL80211_BAND_2GHZ];
5873 	if (sband && idx >= sband->n_channels) {
5874 		idx -= sband->n_channels;
5875 		sband = NULL;
5876 	}
5877 
5878 	if (!sband)
5879 		sband = hw->wiphy->bands[NL80211_BAND_5GHZ];
5880 
5881 	if (!sband || idx >= sband->n_channels) {
5882 		ret = -ENOENT;
5883 		goto exit;
5884 	}
5885 
5886 	ath11k_mac_update_bss_chan_survey(ar, &sband->channels[idx]);
5887 
5888 	spin_lock_bh(&ar->data_lock);
5889 	memcpy(survey, ar_survey, sizeof(*survey));
5890 	spin_unlock_bh(&ar->data_lock);
5891 
5892 	survey->channel = &sband->channels[idx];
5893 
5894 	if (ar->rx_channel == survey->channel)
5895 		survey->filled |= SURVEY_INFO_IN_USE;
5896 
5897 exit:
5898 	mutex_unlock(&ar->conf_mutex);
5899 	return ret;
5900 }
5901 
5902 static void ath11k_mac_op_sta_statistics(struct ieee80211_hw *hw,
5903 					 struct ieee80211_vif *vif,
5904 					 struct ieee80211_sta *sta,
5905 					 struct station_info *sinfo)
5906 {
5907 	struct ath11k_sta *arsta = (struct ath11k_sta *)sta->drv_priv;
5908 
5909 	sinfo->rx_duration = arsta->rx_duration;
5910 	sinfo->filled |= BIT_ULL(NL80211_STA_INFO_RX_DURATION);
5911 
5912 	sinfo->tx_duration = arsta->tx_duration;
5913 	sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_DURATION);
5914 
5915 	if (!arsta->txrate.legacy && !arsta->txrate.nss)
5916 		return;
5917 
5918 	if (arsta->txrate.legacy) {
5919 		sinfo->txrate.legacy = arsta->txrate.legacy;
5920 	} else {
5921 		sinfo->txrate.mcs = arsta->txrate.mcs;
5922 		sinfo->txrate.nss = arsta->txrate.nss;
5923 		sinfo->txrate.bw = arsta->txrate.bw;
5924 		sinfo->txrate.he_gi = arsta->txrate.he_gi;
5925 		sinfo->txrate.he_dcm = arsta->txrate.he_dcm;
5926 		sinfo->txrate.he_ru_alloc = arsta->txrate.he_ru_alloc;
5927 	}
5928 	sinfo->txrate.flags = arsta->txrate.flags;
5929 	sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_BITRATE);
5930 
5931 	/* TODO: Use real NF instead of default one. */
5932 	sinfo->signal = arsta->rssi_comb + ATH11K_DEFAULT_NOISE_FLOOR;
5933 }
5934 
5935 static const struct ieee80211_ops ath11k_ops = {
5936 	.tx				= ath11k_mac_op_tx,
5937 	.start                          = ath11k_mac_op_start,
5938 	.stop                           = ath11k_mac_op_stop,
5939 	.reconfig_complete              = ath11k_mac_op_reconfig_complete,
5940 	.add_interface                  = ath11k_mac_op_add_interface,
5941 	.remove_interface		= ath11k_mac_op_remove_interface,
5942 	.update_vif_offload		= ath11k_mac_op_update_vif_offload,
5943 	.config                         = ath11k_mac_op_config,
5944 	.bss_info_changed               = ath11k_mac_op_bss_info_changed,
5945 	.configure_filter		= ath11k_mac_op_configure_filter,
5946 	.hw_scan                        = ath11k_mac_op_hw_scan,
5947 	.cancel_hw_scan                 = ath11k_mac_op_cancel_hw_scan,
5948 	.set_key                        = ath11k_mac_op_set_key,
5949 	.sta_state                      = ath11k_mac_op_sta_state,
5950 	.sta_set_txpwr			= ath11k_mac_op_sta_set_txpwr,
5951 	.sta_rc_update			= ath11k_mac_op_sta_rc_update,
5952 	.conf_tx                        = ath11k_mac_op_conf_tx,
5953 	.set_antenna			= ath11k_mac_op_set_antenna,
5954 	.get_antenna			= ath11k_mac_op_get_antenna,
5955 	.ampdu_action			= ath11k_mac_op_ampdu_action,
5956 	.add_chanctx			= ath11k_mac_op_add_chanctx,
5957 	.remove_chanctx			= ath11k_mac_op_remove_chanctx,
5958 	.change_chanctx			= ath11k_mac_op_change_chanctx,
5959 	.assign_vif_chanctx		= ath11k_mac_op_assign_vif_chanctx,
5960 	.unassign_vif_chanctx		= ath11k_mac_op_unassign_vif_chanctx,
5961 	.switch_vif_chanctx		= ath11k_mac_op_switch_vif_chanctx,
5962 	.set_rts_threshold		= ath11k_mac_op_set_rts_threshold,
5963 	.set_frag_threshold		= ath11k_mac_op_set_frag_threshold,
5964 	.set_bitrate_mask		= ath11k_mac_op_set_bitrate_mask,
5965 	.get_survey			= ath11k_mac_op_get_survey,
5966 	.flush				= ath11k_mac_op_flush,
5967 	.sta_statistics			= ath11k_mac_op_sta_statistics,
5968 	CFG80211_TESTMODE_CMD(ath11k_tm_cmd)
5969 #ifdef CONFIG_ATH11K_DEBUGFS
5970 	.sta_add_debugfs		= ath11k_debugfs_sta_op_add,
5971 #endif
5972 };
5973 
5974 static void ath11k_mac_update_ch_list(struct ath11k *ar,
5975 				      struct ieee80211_supported_band *band,
5976 				      u32 freq_low, u32 freq_high)
5977 {
5978 	int i;
5979 
5980 	if (!(freq_low && freq_high))
5981 		return;
5982 
5983 	for (i = 0; i < band->n_channels; i++) {
5984 		if (band->channels[i].center_freq < freq_low ||
5985 		    band->channels[i].center_freq > freq_high)
5986 			band->channels[i].flags |= IEEE80211_CHAN_DISABLED;
5987 	}
5988 }
5989 
5990 static u32 ath11k_get_phy_id(struct ath11k *ar, u32 band)
5991 {
5992 	struct ath11k_pdev *pdev = ar->pdev;
5993 	struct ath11k_pdev_cap *pdev_cap = &pdev->cap;
5994 
5995 	if (band == WMI_HOST_WLAN_2G_CAP)
5996 		return pdev_cap->band[NL80211_BAND_2GHZ].phy_id;
5997 
5998 	if (band == WMI_HOST_WLAN_5G_CAP)
5999 		return pdev_cap->band[NL80211_BAND_5GHZ].phy_id;
6000 
6001 	ath11k_warn(ar->ab, "unsupported phy cap:%d\n", band);
6002 
6003 	return 0;
6004 }
6005 
6006 static int ath11k_mac_setup_channels_rates(struct ath11k *ar,
6007 					   u32 supported_bands)
6008 {
6009 	struct ieee80211_supported_band *band;
6010 	struct ath11k_hal_reg_capabilities_ext *reg_cap;
6011 	void *channels;
6012 	u32 phy_id;
6013 
6014 	BUILD_BUG_ON((ARRAY_SIZE(ath11k_2ghz_channels) +
6015 		      ARRAY_SIZE(ath11k_5ghz_channels) +
6016 		      ARRAY_SIZE(ath11k_6ghz_channels)) !=
6017 		     ATH11K_NUM_CHANS);
6018 
6019 	reg_cap = &ar->ab->hal_reg_cap[ar->pdev_idx];
6020 
6021 	if (supported_bands & WMI_HOST_WLAN_2G_CAP) {
6022 		channels = kmemdup(ath11k_2ghz_channels,
6023 				   sizeof(ath11k_2ghz_channels),
6024 				   GFP_KERNEL);
6025 		if (!channels)
6026 			return -ENOMEM;
6027 
6028 		band = &ar->mac.sbands[NL80211_BAND_2GHZ];
6029 		band->band = NL80211_BAND_2GHZ;
6030 		band->n_channels = ARRAY_SIZE(ath11k_2ghz_channels);
6031 		band->channels = channels;
6032 		band->n_bitrates = ath11k_g_rates_size;
6033 		band->bitrates = ath11k_g_rates;
6034 		ar->hw->wiphy->bands[NL80211_BAND_2GHZ] = band;
6035 
6036 		if (ar->ab->hw_params.single_pdev_only) {
6037 			phy_id = ath11k_get_phy_id(ar, WMI_HOST_WLAN_2G_CAP);
6038 			reg_cap = &ar->ab->hal_reg_cap[phy_id];
6039 		}
6040 		ath11k_mac_update_ch_list(ar, band,
6041 					  reg_cap->low_2ghz_chan,
6042 					  reg_cap->high_2ghz_chan);
6043 	}
6044 
6045 	if (supported_bands & WMI_HOST_WLAN_5G_CAP) {
6046 		if (reg_cap->high_5ghz_chan >= ATH11K_MAX_6G_FREQ) {
6047 			channels = kmemdup(ath11k_6ghz_channels,
6048 					   sizeof(ath11k_6ghz_channels), GFP_KERNEL);
6049 			if (!channels) {
6050 				kfree(ar->mac.sbands[NL80211_BAND_2GHZ].channels);
6051 				return -ENOMEM;
6052 			}
6053 
6054 			ar->supports_6ghz = true;
6055 			band = &ar->mac.sbands[NL80211_BAND_6GHZ];
6056 			band->band = NL80211_BAND_6GHZ;
6057 			band->n_channels = ARRAY_SIZE(ath11k_6ghz_channels);
6058 			band->channels = channels;
6059 			band->n_bitrates = ath11k_a_rates_size;
6060 			band->bitrates = ath11k_a_rates;
6061 			ar->hw->wiphy->bands[NL80211_BAND_6GHZ] = band;
6062 			ath11k_mac_update_ch_list(ar, band,
6063 						  reg_cap->low_5ghz_chan,
6064 						  reg_cap->high_5ghz_chan);
6065 		}
6066 
6067 		if (reg_cap->low_5ghz_chan < ATH11K_MIN_6G_FREQ) {
6068 			channels = kmemdup(ath11k_5ghz_channels,
6069 					   sizeof(ath11k_5ghz_channels),
6070 					   GFP_KERNEL);
6071 			if (!channels) {
6072 				kfree(ar->mac.sbands[NL80211_BAND_2GHZ].channels);
6073 				kfree(ar->mac.sbands[NL80211_BAND_6GHZ].channels);
6074 				return -ENOMEM;
6075 			}
6076 
6077 			band = &ar->mac.sbands[NL80211_BAND_5GHZ];
6078 			band->band = NL80211_BAND_5GHZ;
6079 			band->n_channels = ARRAY_SIZE(ath11k_5ghz_channels);
6080 			band->channels = channels;
6081 			band->n_bitrates = ath11k_a_rates_size;
6082 			band->bitrates = ath11k_a_rates;
6083 			ar->hw->wiphy->bands[NL80211_BAND_5GHZ] = band;
6084 
6085 			if (ar->ab->hw_params.single_pdev_only) {
6086 				phy_id = ath11k_get_phy_id(ar, WMI_HOST_WLAN_5G_CAP);
6087 				reg_cap = &ar->ab->hal_reg_cap[phy_id];
6088 			}
6089 
6090 			ath11k_mac_update_ch_list(ar, band,
6091 						  reg_cap->low_5ghz_chan,
6092 						  reg_cap->high_5ghz_chan);
6093 		}
6094 	}
6095 
6096 	return 0;
6097 }
6098 
6099 static int ath11k_mac_setup_iface_combinations(struct ath11k *ar)
6100 {
6101 	struct ath11k_base *ab = ar->ab;
6102 	struct ieee80211_iface_combination *combinations;
6103 	struct ieee80211_iface_limit *limits;
6104 	int n_limits;
6105 
6106 	combinations = kzalloc(sizeof(*combinations), GFP_KERNEL);
6107 	if (!combinations)
6108 		return -ENOMEM;
6109 
6110 	n_limits = 2;
6111 
6112 	limits = kcalloc(n_limits, sizeof(*limits), GFP_KERNEL);
6113 	if (!limits) {
6114 		kfree(combinations);
6115 		return -ENOMEM;
6116 	}
6117 
6118 	limits[0].max = 1;
6119 	limits[0].types |= BIT(NL80211_IFTYPE_STATION);
6120 
6121 	limits[1].max = 16;
6122 	limits[1].types |= BIT(NL80211_IFTYPE_AP);
6123 
6124 	if (IS_ENABLED(CONFIG_MAC80211_MESH) &&
6125 	    ab->hw_params.interface_modes & BIT(NL80211_IFTYPE_MESH_POINT))
6126 		limits[1].types |= BIT(NL80211_IFTYPE_MESH_POINT);
6127 
6128 	combinations[0].limits = limits;
6129 	combinations[0].n_limits = n_limits;
6130 	combinations[0].max_interfaces = 16;
6131 	combinations[0].num_different_channels = 1;
6132 	combinations[0].beacon_int_infra_match = true;
6133 	combinations[0].beacon_int_min_gcd = 100;
6134 	combinations[0].radar_detect_widths = BIT(NL80211_CHAN_WIDTH_20_NOHT) |
6135 						BIT(NL80211_CHAN_WIDTH_20) |
6136 						BIT(NL80211_CHAN_WIDTH_40) |
6137 						BIT(NL80211_CHAN_WIDTH_80);
6138 
6139 	ar->hw->wiphy->iface_combinations = combinations;
6140 	ar->hw->wiphy->n_iface_combinations = 1;
6141 
6142 	return 0;
6143 }
6144 
6145 static const u8 ath11k_if_types_ext_capa[] = {
6146 	[0] = WLAN_EXT_CAPA1_EXT_CHANNEL_SWITCHING,
6147 	[7] = WLAN_EXT_CAPA8_OPMODE_NOTIF,
6148 };
6149 
6150 static const u8 ath11k_if_types_ext_capa_sta[] = {
6151 	[0] = WLAN_EXT_CAPA1_EXT_CHANNEL_SWITCHING,
6152 	[7] = WLAN_EXT_CAPA8_OPMODE_NOTIF,
6153 	[9] = WLAN_EXT_CAPA10_TWT_REQUESTER_SUPPORT,
6154 };
6155 
6156 static const u8 ath11k_if_types_ext_capa_ap[] = {
6157 	[0] = WLAN_EXT_CAPA1_EXT_CHANNEL_SWITCHING,
6158 	[7] = WLAN_EXT_CAPA8_OPMODE_NOTIF,
6159 	[9] = WLAN_EXT_CAPA10_TWT_RESPONDER_SUPPORT,
6160 };
6161 
6162 static const struct wiphy_iftype_ext_capab ath11k_iftypes_ext_capa[] = {
6163 	{
6164 		.extended_capabilities = ath11k_if_types_ext_capa,
6165 		.extended_capabilities_mask = ath11k_if_types_ext_capa,
6166 		.extended_capabilities_len = sizeof(ath11k_if_types_ext_capa),
6167 	}, {
6168 		.iftype = NL80211_IFTYPE_STATION,
6169 		.extended_capabilities = ath11k_if_types_ext_capa_sta,
6170 		.extended_capabilities_mask = ath11k_if_types_ext_capa_sta,
6171 		.extended_capabilities_len =
6172 				sizeof(ath11k_if_types_ext_capa_sta),
6173 	}, {
6174 		.iftype = NL80211_IFTYPE_AP,
6175 		.extended_capabilities = ath11k_if_types_ext_capa_ap,
6176 		.extended_capabilities_mask = ath11k_if_types_ext_capa_ap,
6177 		.extended_capabilities_len =
6178 				sizeof(ath11k_if_types_ext_capa_ap),
6179 	},
6180 };
6181 
6182 static void __ath11k_mac_unregister(struct ath11k *ar)
6183 {
6184 	cancel_work_sync(&ar->regd_update_work);
6185 
6186 	ieee80211_unregister_hw(ar->hw);
6187 
6188 	idr_for_each(&ar->txmgmt_idr, ath11k_mac_tx_mgmt_pending_free, ar);
6189 	idr_destroy(&ar->txmgmt_idr);
6190 
6191 	kfree(ar->mac.sbands[NL80211_BAND_2GHZ].channels);
6192 	kfree(ar->mac.sbands[NL80211_BAND_5GHZ].channels);
6193 	kfree(ar->mac.sbands[NL80211_BAND_6GHZ].channels);
6194 
6195 	kfree(ar->hw->wiphy->iface_combinations[0].limits);
6196 	kfree(ar->hw->wiphy->iface_combinations);
6197 
6198 	SET_IEEE80211_DEV(ar->hw, NULL);
6199 }
6200 
6201 void ath11k_mac_unregister(struct ath11k_base *ab)
6202 {
6203 	struct ath11k *ar;
6204 	struct ath11k_pdev *pdev;
6205 	int i;
6206 
6207 	for (i = 0; i < ab->num_radios; i++) {
6208 		pdev = &ab->pdevs[i];
6209 		ar = pdev->ar;
6210 		if (!ar)
6211 			continue;
6212 
6213 		__ath11k_mac_unregister(ar);
6214 	}
6215 }
6216 
6217 static int __ath11k_mac_register(struct ath11k *ar)
6218 {
6219 	struct ath11k_base *ab = ar->ab;
6220 	struct ath11k_pdev_cap *cap = &ar->pdev->cap;
6221 	static const u32 cipher_suites[] = {
6222 		WLAN_CIPHER_SUITE_TKIP,
6223 		WLAN_CIPHER_SUITE_CCMP,
6224 		WLAN_CIPHER_SUITE_AES_CMAC,
6225 		WLAN_CIPHER_SUITE_BIP_CMAC_256,
6226 		WLAN_CIPHER_SUITE_BIP_GMAC_128,
6227 		WLAN_CIPHER_SUITE_BIP_GMAC_256,
6228 		WLAN_CIPHER_SUITE_GCMP,
6229 		WLAN_CIPHER_SUITE_GCMP_256,
6230 		WLAN_CIPHER_SUITE_CCMP_256,
6231 	};
6232 	int ret;
6233 	u32 ht_cap = 0;
6234 
6235 	ath11k_pdev_caps_update(ar);
6236 
6237 	SET_IEEE80211_PERM_ADDR(ar->hw, ar->mac_addr);
6238 
6239 	SET_IEEE80211_DEV(ar->hw, ab->dev);
6240 
6241 	ret = ath11k_mac_setup_channels_rates(ar,
6242 					      cap->supported_bands);
6243 	if (ret)
6244 		goto err;
6245 
6246 	ath11k_mac_setup_ht_vht_cap(ar, cap, &ht_cap);
6247 	ath11k_mac_setup_he_cap(ar, cap);
6248 
6249 	ret = ath11k_mac_setup_iface_combinations(ar);
6250 	if (ret) {
6251 		ath11k_err(ar->ab, "failed to setup interface combinations: %d\n", ret);
6252 		goto err_free_channels;
6253 	}
6254 
6255 	ar->hw->wiphy->available_antennas_rx = cap->rx_chain_mask;
6256 	ar->hw->wiphy->available_antennas_tx = cap->tx_chain_mask;
6257 
6258 	ar->hw->wiphy->interface_modes = ab->hw_params.interface_modes;
6259 
6260 	ieee80211_hw_set(ar->hw, SIGNAL_DBM);
6261 	ieee80211_hw_set(ar->hw, SUPPORTS_PS);
6262 	ieee80211_hw_set(ar->hw, SUPPORTS_DYNAMIC_PS);
6263 	ieee80211_hw_set(ar->hw, MFP_CAPABLE);
6264 	ieee80211_hw_set(ar->hw, REPORTS_TX_ACK_STATUS);
6265 	ieee80211_hw_set(ar->hw, HAS_RATE_CONTROL);
6266 	ieee80211_hw_set(ar->hw, AP_LINK_PS);
6267 	ieee80211_hw_set(ar->hw, SPECTRUM_MGMT);
6268 	ieee80211_hw_set(ar->hw, CONNECTION_MONITOR);
6269 	ieee80211_hw_set(ar->hw, SUPPORTS_PER_STA_GTK);
6270 	ieee80211_hw_set(ar->hw, WANT_MONITOR_VIF);
6271 	ieee80211_hw_set(ar->hw, CHANCTX_STA_CSA);
6272 	ieee80211_hw_set(ar->hw, QUEUE_CONTROL);
6273 	ieee80211_hw_set(ar->hw, SUPPORTS_TX_FRAG);
6274 	ieee80211_hw_set(ar->hw, REPORTS_LOW_ACK);
6275 	ieee80211_hw_set(ar->hw, SUPPORTS_TX_ENCAP_OFFLOAD);
6276 	if (ht_cap & WMI_HT_CAP_ENABLED) {
6277 		ieee80211_hw_set(ar->hw, AMPDU_AGGREGATION);
6278 		ieee80211_hw_set(ar->hw, TX_AMPDU_SETUP_IN_HW);
6279 		ieee80211_hw_set(ar->hw, SUPPORTS_REORDERING_BUFFER);
6280 		ieee80211_hw_set(ar->hw, SUPPORTS_AMSDU_IN_AMPDU);
6281 		ieee80211_hw_set(ar->hw, USES_RSS);
6282 	}
6283 
6284 	ar->hw->wiphy->features |= NL80211_FEATURE_STATIC_SMPS;
6285 	ar->hw->wiphy->flags |= WIPHY_FLAG_IBSS_RSN;
6286 
6287 	/* TODO: Check if HT capability advertised from firmware is different
6288 	 * for each band for a dual band capable radio. It will be tricky to
6289 	 * handle it when the ht capability different for each band.
6290 	 */
6291 	if (ht_cap & WMI_HT_CAP_DYNAMIC_SMPS)
6292 		ar->hw->wiphy->features |= NL80211_FEATURE_DYNAMIC_SMPS;
6293 
6294 	ar->hw->wiphy->max_scan_ssids = WLAN_SCAN_PARAMS_MAX_SSID;
6295 	ar->hw->wiphy->max_scan_ie_len = WLAN_SCAN_PARAMS_MAX_IE_LEN;
6296 
6297 	ar->hw->max_listen_interval = ATH11K_MAX_HW_LISTEN_INTERVAL;
6298 
6299 	ar->hw->wiphy->flags |= WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL;
6300 	ar->hw->wiphy->flags |= WIPHY_FLAG_HAS_CHANNEL_SWITCH;
6301 	ar->hw->wiphy->max_remain_on_channel_duration = 5000;
6302 
6303 	ar->hw->wiphy->flags |= WIPHY_FLAG_AP_UAPSD;
6304 	ar->hw->wiphy->features |= NL80211_FEATURE_AP_MODE_CHAN_WIDTH_CHANGE |
6305 				   NL80211_FEATURE_AP_SCAN;
6306 
6307 	ar->max_num_stations = TARGET_NUM_STATIONS;
6308 	ar->max_num_peers = TARGET_NUM_PEERS_PDEV;
6309 
6310 	ar->hw->wiphy->max_ap_assoc_sta = ar->max_num_stations;
6311 
6312 	ar->hw->queues = ATH11K_HW_MAX_QUEUES;
6313 	ar->hw->wiphy->tx_queue_len = ATH11K_QUEUE_LEN;
6314 	ar->hw->offchannel_tx_hw_queue = ATH11K_HW_MAX_QUEUES - 1;
6315 	ar->hw->max_rx_aggregation_subframes = IEEE80211_MAX_AMPDU_BUF;
6316 
6317 	ar->hw->vif_data_size = sizeof(struct ath11k_vif);
6318 	ar->hw->sta_data_size = sizeof(struct ath11k_sta);
6319 
6320 	wiphy_ext_feature_set(ar->hw->wiphy, NL80211_EXT_FEATURE_CQM_RSSI_LIST);
6321 	wiphy_ext_feature_set(ar->hw->wiphy, NL80211_EXT_FEATURE_STA_TX_PWR);
6322 
6323 	ar->hw->wiphy->cipher_suites = cipher_suites;
6324 	ar->hw->wiphy->n_cipher_suites = ARRAY_SIZE(cipher_suites);
6325 
6326 	ar->hw->wiphy->iftype_ext_capab = ath11k_iftypes_ext_capa;
6327 	ar->hw->wiphy->num_iftype_ext_capab =
6328 		ARRAY_SIZE(ath11k_iftypes_ext_capa);
6329 
6330 	if (ar->supports_6ghz) {
6331 		wiphy_ext_feature_set(ar->hw->wiphy,
6332 				      NL80211_EXT_FEATURE_FILS_DISCOVERY);
6333 		wiphy_ext_feature_set(ar->hw->wiphy,
6334 				      NL80211_EXT_FEATURE_UNSOL_BCAST_PROBE_RESP);
6335 	}
6336 
6337 	ath11k_reg_init(ar);
6338 
6339 	if (!test_bit(ATH11K_FLAG_RAW_MODE, &ab->dev_flags)) {
6340 		ar->hw->netdev_features = NETIF_F_HW_CSUM;
6341 		ieee80211_hw_set(ar->hw, SW_CRYPTO_CONTROL);
6342 		ieee80211_hw_set(ar->hw, SUPPORT_FAST_XMIT);
6343 	}
6344 
6345 	ret = ieee80211_register_hw(ar->hw);
6346 	if (ret) {
6347 		ath11k_err(ar->ab, "ieee80211 registration failed: %d\n", ret);
6348 		goto err_free_if_combs;
6349 	}
6350 
6351 	if (!ab->hw_params.supports_monitor)
6352 		/* There's a race between calling ieee80211_register_hw()
6353 		 * and here where the monitor mode is enabled for a little
6354 		 * while. But that time is so short and in practise it make
6355 		 * a difference in real life.
6356 		 */
6357 		ar->hw->wiphy->interface_modes &= ~BIT(NL80211_IFTYPE_MONITOR);
6358 
6359 	/* Apply the regd received during initialization */
6360 	ret = ath11k_regd_update(ar, true);
6361 	if (ret) {
6362 		ath11k_err(ar->ab, "ath11k regd update failed: %d\n", ret);
6363 		goto err_free_if_combs;
6364 	}
6365 
6366 	ret = ath11k_debugfs_register(ar);
6367 	if (ret) {
6368 		ath11k_err(ar->ab, "debugfs registration failed: %d\n", ret);
6369 		goto err_free_if_combs;
6370 	}
6371 
6372 	return 0;
6373 
6374 err_free_if_combs:
6375 	kfree(ar->hw->wiphy->iface_combinations[0].limits);
6376 	kfree(ar->hw->wiphy->iface_combinations);
6377 
6378 err_free_channels:
6379 	kfree(ar->mac.sbands[NL80211_BAND_2GHZ].channels);
6380 	kfree(ar->mac.sbands[NL80211_BAND_5GHZ].channels);
6381 	kfree(ar->mac.sbands[NL80211_BAND_6GHZ].channels);
6382 
6383 err:
6384 	SET_IEEE80211_DEV(ar->hw, NULL);
6385 	return ret;
6386 }
6387 
6388 int ath11k_mac_register(struct ath11k_base *ab)
6389 {
6390 	struct ath11k *ar;
6391 	struct ath11k_pdev *pdev;
6392 	int i;
6393 	int ret;
6394 
6395 	if (test_bit(ATH11K_FLAG_REGISTERED, &ab->dev_flags))
6396 		return 0;
6397 
6398 	for (i = 0; i < ab->num_radios; i++) {
6399 		pdev = &ab->pdevs[i];
6400 		ar = pdev->ar;
6401 		if (ab->pdevs_macaddr_valid) {
6402 			ether_addr_copy(ar->mac_addr, pdev->mac_addr);
6403 		} else {
6404 			ether_addr_copy(ar->mac_addr, ab->mac_addr);
6405 			ar->mac_addr[4] += i;
6406 		}
6407 
6408 		ret = __ath11k_mac_register(ar);
6409 		if (ret)
6410 			goto err_cleanup;
6411 
6412 		idr_init(&ar->txmgmt_idr);
6413 		spin_lock_init(&ar->txmgmt_idr_lock);
6414 	}
6415 
6416 	/* Initialize channel counters frequency value in hertz */
6417 	ab->cc_freq_hz = IPQ8074_CC_FREQ_HERTZ;
6418 	ab->free_vdev_map = (1LL << (ab->num_radios * TARGET_NUM_VDEVS)) - 1;
6419 
6420 	return 0;
6421 
6422 err_cleanup:
6423 	for (i = i - 1; i >= 0; i--) {
6424 		pdev = &ab->pdevs[i];
6425 		ar = pdev->ar;
6426 		__ath11k_mac_unregister(ar);
6427 	}
6428 
6429 	return ret;
6430 }
6431 
6432 int ath11k_mac_allocate(struct ath11k_base *ab)
6433 {
6434 	struct ieee80211_hw *hw;
6435 	struct ath11k *ar;
6436 	struct ath11k_pdev *pdev;
6437 	int ret;
6438 	int i;
6439 
6440 	if (test_bit(ATH11K_FLAG_REGISTERED, &ab->dev_flags))
6441 		return 0;
6442 
6443 	for (i = 0; i < ab->num_radios; i++) {
6444 		pdev = &ab->pdevs[i];
6445 		hw = ieee80211_alloc_hw(sizeof(struct ath11k), &ath11k_ops);
6446 		if (!hw) {
6447 			ath11k_warn(ab, "failed to allocate mac80211 hw device\n");
6448 			ret = -ENOMEM;
6449 			goto err_free_mac;
6450 		}
6451 
6452 		ar = hw->priv;
6453 		ar->hw = hw;
6454 		ar->ab = ab;
6455 		ar->pdev = pdev;
6456 		ar->pdev_idx = i;
6457 		ar->lmac_id = ath11k_hw_get_mac_from_pdev_id(&ab->hw_params, i);
6458 
6459 		ar->wmi = &ab->wmi_ab.wmi[i];
6460 		/* FIXME wmi[0] is already initialized during attach,
6461 		 * Should we do this again?
6462 		 */
6463 		ath11k_wmi_pdev_attach(ab, i);
6464 
6465 		ar->cfg_tx_chainmask = pdev->cap.tx_chain_mask;
6466 		ar->cfg_rx_chainmask = pdev->cap.rx_chain_mask;
6467 		ar->num_tx_chains = get_num_chains(pdev->cap.tx_chain_mask);
6468 		ar->num_rx_chains = get_num_chains(pdev->cap.rx_chain_mask);
6469 
6470 		pdev->ar = ar;
6471 		spin_lock_init(&ar->data_lock);
6472 		INIT_LIST_HEAD(&ar->arvifs);
6473 		INIT_LIST_HEAD(&ar->ppdu_stats_info);
6474 		mutex_init(&ar->conf_mutex);
6475 		init_completion(&ar->vdev_setup_done);
6476 		init_completion(&ar->vdev_delete_done);
6477 		init_completion(&ar->peer_assoc_done);
6478 		init_completion(&ar->peer_delete_done);
6479 		init_completion(&ar->install_key_done);
6480 		init_completion(&ar->bss_survey_done);
6481 		init_completion(&ar->scan.started);
6482 		init_completion(&ar->scan.completed);
6483 		init_completion(&ar->thermal.wmi_sync);
6484 
6485 		INIT_DELAYED_WORK(&ar->scan.timeout, ath11k_scan_timeout_work);
6486 		INIT_WORK(&ar->regd_update_work, ath11k_regd_update_work);
6487 
6488 		INIT_WORK(&ar->wmi_mgmt_tx_work, ath11k_mgmt_over_wmi_tx_work);
6489 		skb_queue_head_init(&ar->wmi_mgmt_tx_queue);
6490 		clear_bit(ATH11K_FLAG_MONITOR_ENABLED, &ar->monitor_flags);
6491 	}
6492 
6493 	return 0;
6494 
6495 err_free_mac:
6496 	ath11k_mac_destroy(ab);
6497 
6498 	return ret;
6499 }
6500 
6501 void ath11k_mac_destroy(struct ath11k_base *ab)
6502 {
6503 	struct ath11k *ar;
6504 	struct ath11k_pdev *pdev;
6505 	int i;
6506 
6507 	for (i = 0; i < ab->num_radios; i++) {
6508 		pdev = &ab->pdevs[i];
6509 		ar = pdev->ar;
6510 		if (!ar)
6511 			continue;
6512 
6513 		ieee80211_free_hw(ar->hw);
6514 		pdev->ar = NULL;
6515 	}
6516 }
6517