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