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