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