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