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