xref: /openbmc/linux/drivers/net/wireless/ath/ath11k/mac.c (revision 335a92765d308dfe22826f5562cd4b4389b45e71)
1 // SPDX-License-Identifier: BSD-3-Clause-Clear
2 /*
3  * Copyright (c) 2018-2019 The Linux Foundation. All rights reserved.
4  * Copyright (c) 2021-2022 Qualcomm Innovation Center, Inc. All rights reserved.
5  */
6 
7 #include <net/mac80211.h>
8 #include <linux/etherdevice.h>
9 #include <linux/bitfield.h>
10 #include <linux/inetdevice.h>
11 #include <net/if_inet6.h>
12 #include <net/ipv6.h>
13 
14 #include "mac.h"
15 #include "core.h"
16 #include "debug.h"
17 #include "wmi.h"
18 #include "hw.h"
19 #include "dp_tx.h"
20 #include "dp_rx.h"
21 #include "testmode.h"
22 #include "peer.h"
23 #include "debugfs_sta.h"
24 #include "hif.h"
25 #include "wow.h"
26 
27 #define CHAN2G(_channel, _freq, _flags) { \
28 	.band                   = NL80211_BAND_2GHZ, \
29 	.hw_value               = (_channel), \
30 	.center_freq            = (_freq), \
31 	.flags                  = (_flags), \
32 	.max_antenna_gain       = 0, \
33 	.max_power              = 30, \
34 }
35 
36 #define CHAN5G(_channel, _freq, _flags) { \
37 	.band                   = NL80211_BAND_5GHZ, \
38 	.hw_value               = (_channel), \
39 	.center_freq            = (_freq), \
40 	.flags                  = (_flags), \
41 	.max_antenna_gain       = 0, \
42 	.max_power              = 30, \
43 }
44 
45 #define CHAN6G(_channel, _freq, _flags) { \
46 	.band                   = NL80211_BAND_6GHZ, \
47 	.hw_value               = (_channel), \
48 	.center_freq            = (_freq), \
49 	.flags                  = (_flags), \
50 	.max_antenna_gain       = 0, \
51 	.max_power              = 30, \
52 }
53 
54 static const struct ieee80211_channel ath11k_2ghz_channels[] = {
55 	CHAN2G(1, 2412, 0),
56 	CHAN2G(2, 2417, 0),
57 	CHAN2G(3, 2422, 0),
58 	CHAN2G(4, 2427, 0),
59 	CHAN2G(5, 2432, 0),
60 	CHAN2G(6, 2437, 0),
61 	CHAN2G(7, 2442, 0),
62 	CHAN2G(8, 2447, 0),
63 	CHAN2G(9, 2452, 0),
64 	CHAN2G(10, 2457, 0),
65 	CHAN2G(11, 2462, 0),
66 	CHAN2G(12, 2467, 0),
67 	CHAN2G(13, 2472, 0),
68 	CHAN2G(14, 2484, 0),
69 };
70 
71 static const struct ieee80211_channel ath11k_5ghz_channels[] = {
72 	CHAN5G(36, 5180, 0),
73 	CHAN5G(40, 5200, 0),
74 	CHAN5G(44, 5220, 0),
75 	CHAN5G(48, 5240, 0),
76 	CHAN5G(52, 5260, 0),
77 	CHAN5G(56, 5280, 0),
78 	CHAN5G(60, 5300, 0),
79 	CHAN5G(64, 5320, 0),
80 	CHAN5G(100, 5500, 0),
81 	CHAN5G(104, 5520, 0),
82 	CHAN5G(108, 5540, 0),
83 	CHAN5G(112, 5560, 0),
84 	CHAN5G(116, 5580, 0),
85 	CHAN5G(120, 5600, 0),
86 	CHAN5G(124, 5620, 0),
87 	CHAN5G(128, 5640, 0),
88 	CHAN5G(132, 5660, 0),
89 	CHAN5G(136, 5680, 0),
90 	CHAN5G(140, 5700, 0),
91 	CHAN5G(144, 5720, 0),
92 	CHAN5G(149, 5745, 0),
93 	CHAN5G(153, 5765, 0),
94 	CHAN5G(157, 5785, 0),
95 	CHAN5G(161, 5805, 0),
96 	CHAN5G(165, 5825, 0),
97 	CHAN5G(169, 5845, 0),
98 	CHAN5G(173, 5865, 0),
99 	CHAN5G(177, 5885, 0),
100 };
101 
102 static const struct ieee80211_channel ath11k_6ghz_channels[] = {
103 	CHAN6G(1, 5955, 0),
104 	CHAN6G(5, 5975, 0),
105 	CHAN6G(9, 5995, 0),
106 	CHAN6G(13, 6015, 0),
107 	CHAN6G(17, 6035, 0),
108 	CHAN6G(21, 6055, 0),
109 	CHAN6G(25, 6075, 0),
110 	CHAN6G(29, 6095, 0),
111 	CHAN6G(33, 6115, 0),
112 	CHAN6G(37, 6135, 0),
113 	CHAN6G(41, 6155, 0),
114 	CHAN6G(45, 6175, 0),
115 	CHAN6G(49, 6195, 0),
116 	CHAN6G(53, 6215, 0),
117 	CHAN6G(57, 6235, 0),
118 	CHAN6G(61, 6255, 0),
119 	CHAN6G(65, 6275, 0),
120 	CHAN6G(69, 6295, 0),
121 	CHAN6G(73, 6315, 0),
122 	CHAN6G(77, 6335, 0),
123 	CHAN6G(81, 6355, 0),
124 	CHAN6G(85, 6375, 0),
125 	CHAN6G(89, 6395, 0),
126 	CHAN6G(93, 6415, 0),
127 	CHAN6G(97, 6435, 0),
128 	CHAN6G(101, 6455, 0),
129 	CHAN6G(105, 6475, 0),
130 	CHAN6G(109, 6495, 0),
131 	CHAN6G(113, 6515, 0),
132 	CHAN6G(117, 6535, 0),
133 	CHAN6G(121, 6555, 0),
134 	CHAN6G(125, 6575, 0),
135 	CHAN6G(129, 6595, 0),
136 	CHAN6G(133, 6615, 0),
137 	CHAN6G(137, 6635, 0),
138 	CHAN6G(141, 6655, 0),
139 	CHAN6G(145, 6675, 0),
140 	CHAN6G(149, 6695, 0),
141 	CHAN6G(153, 6715, 0),
142 	CHAN6G(157, 6735, 0),
143 	CHAN6G(161, 6755, 0),
144 	CHAN6G(165, 6775, 0),
145 	CHAN6G(169, 6795, 0),
146 	CHAN6G(173, 6815, 0),
147 	CHAN6G(177, 6835, 0),
148 	CHAN6G(181, 6855, 0),
149 	CHAN6G(185, 6875, 0),
150 	CHAN6G(189, 6895, 0),
151 	CHAN6G(193, 6915, 0),
152 	CHAN6G(197, 6935, 0),
153 	CHAN6G(201, 6955, 0),
154 	CHAN6G(205, 6975, 0),
155 	CHAN6G(209, 6995, 0),
156 	CHAN6G(213, 7015, 0),
157 	CHAN6G(217, 7035, 0),
158 	CHAN6G(221, 7055, 0),
159 	CHAN6G(225, 7075, 0),
160 	CHAN6G(229, 7095, 0),
161 	CHAN6G(233, 7115, 0),
162 
163 	/* new addition in IEEE Std 802.11ax-2021 */
164 	CHAN6G(2, 5935, 0),
165 };
166 
167 static struct ieee80211_rate ath11k_legacy_rates[] = {
168 	{ .bitrate = 10,
169 	  .hw_value = ATH11K_HW_RATE_CCK_LP_1M },
170 	{ .bitrate = 20,
171 	  .hw_value = ATH11K_HW_RATE_CCK_LP_2M,
172 	  .hw_value_short = ATH11K_HW_RATE_CCK_SP_2M,
173 	  .flags = IEEE80211_RATE_SHORT_PREAMBLE },
174 	{ .bitrate = 55,
175 	  .hw_value = ATH11K_HW_RATE_CCK_LP_5_5M,
176 	  .hw_value_short = ATH11K_HW_RATE_CCK_SP_5_5M,
177 	  .flags = IEEE80211_RATE_SHORT_PREAMBLE },
178 	{ .bitrate = 110,
179 	  .hw_value = ATH11K_HW_RATE_CCK_LP_11M,
180 	  .hw_value_short = ATH11K_HW_RATE_CCK_SP_11M,
181 	  .flags = IEEE80211_RATE_SHORT_PREAMBLE },
182 
183 	{ .bitrate = 60, .hw_value = ATH11K_HW_RATE_OFDM_6M },
184 	{ .bitrate = 90, .hw_value = ATH11K_HW_RATE_OFDM_9M },
185 	{ .bitrate = 120, .hw_value = ATH11K_HW_RATE_OFDM_12M },
186 	{ .bitrate = 180, .hw_value = ATH11K_HW_RATE_OFDM_18M },
187 	{ .bitrate = 240, .hw_value = ATH11K_HW_RATE_OFDM_24M },
188 	{ .bitrate = 360, .hw_value = ATH11K_HW_RATE_OFDM_36M },
189 	{ .bitrate = 480, .hw_value = ATH11K_HW_RATE_OFDM_48M },
190 	{ .bitrate = 540, .hw_value = ATH11K_HW_RATE_OFDM_54M },
191 };
192 
193 static const int
194 ath11k_phymodes[NUM_NL80211_BANDS][ATH11K_CHAN_WIDTH_NUM] = {
195 	[NL80211_BAND_2GHZ] = {
196 			[NL80211_CHAN_WIDTH_5] = MODE_UNKNOWN,
197 			[NL80211_CHAN_WIDTH_10] = MODE_UNKNOWN,
198 			[NL80211_CHAN_WIDTH_20_NOHT] = MODE_11AX_HE20_2G,
199 			[NL80211_CHAN_WIDTH_20] = MODE_11AX_HE20_2G,
200 			[NL80211_CHAN_WIDTH_40] = MODE_11AX_HE40_2G,
201 			[NL80211_CHAN_WIDTH_80] = MODE_11AX_HE80_2G,
202 			[NL80211_CHAN_WIDTH_80P80] = MODE_UNKNOWN,
203 			[NL80211_CHAN_WIDTH_160] = MODE_UNKNOWN,
204 	},
205 	[NL80211_BAND_5GHZ] = {
206 			[NL80211_CHAN_WIDTH_5] = MODE_UNKNOWN,
207 			[NL80211_CHAN_WIDTH_10] = MODE_UNKNOWN,
208 			[NL80211_CHAN_WIDTH_20_NOHT] = MODE_11AX_HE20,
209 			[NL80211_CHAN_WIDTH_20] = MODE_11AX_HE20,
210 			[NL80211_CHAN_WIDTH_40] = MODE_11AX_HE40,
211 			[NL80211_CHAN_WIDTH_80] = MODE_11AX_HE80,
212 			[NL80211_CHAN_WIDTH_160] = MODE_11AX_HE160,
213 			[NL80211_CHAN_WIDTH_80P80] = MODE_11AX_HE80_80,
214 	},
215 	[NL80211_BAND_6GHZ] = {
216 			[NL80211_CHAN_WIDTH_5] = MODE_UNKNOWN,
217 			[NL80211_CHAN_WIDTH_10] = MODE_UNKNOWN,
218 			[NL80211_CHAN_WIDTH_20_NOHT] = MODE_11AX_HE20,
219 			[NL80211_CHAN_WIDTH_20] = MODE_11AX_HE20,
220 			[NL80211_CHAN_WIDTH_40] = MODE_11AX_HE40,
221 			[NL80211_CHAN_WIDTH_80] = MODE_11AX_HE80,
222 			[NL80211_CHAN_WIDTH_160] = MODE_11AX_HE160,
223 			[NL80211_CHAN_WIDTH_80P80] = MODE_11AX_HE80_80,
224 	},
225 
226 };
227 
228 const struct htt_rx_ring_tlv_filter ath11k_mac_mon_status_filter_default = {
229 	.rx_filter = HTT_RX_FILTER_TLV_FLAGS_MPDU_START |
230 		     HTT_RX_FILTER_TLV_FLAGS_PPDU_END |
231 		     HTT_RX_FILTER_TLV_FLAGS_PPDU_END_STATUS_DONE,
232 	.pkt_filter_flags0 = HTT_RX_FP_MGMT_FILTER_FLAGS0,
233 	.pkt_filter_flags1 = HTT_RX_FP_MGMT_FILTER_FLAGS1,
234 	.pkt_filter_flags2 = HTT_RX_FP_CTRL_FILTER_FLASG2,
235 	.pkt_filter_flags3 = HTT_RX_FP_DATA_FILTER_FLASG3 |
236 			     HTT_RX_FP_CTRL_FILTER_FLASG3
237 };
238 
239 #define ATH11K_MAC_FIRST_OFDM_RATE_IDX 4
240 #define ath11k_g_rates ath11k_legacy_rates
241 #define ath11k_g_rates_size (ARRAY_SIZE(ath11k_legacy_rates))
242 #define ath11k_a_rates (ath11k_legacy_rates + 4)
243 #define ath11k_a_rates_size (ARRAY_SIZE(ath11k_legacy_rates) - 4)
244 
245 #define ATH11K_MAC_SCAN_CMD_EVT_OVERHEAD		200 /* in msecs */
246 
247 /* Overhead due to the processing of channel switch events from FW */
248 #define ATH11K_SCAN_CHANNEL_SWITCH_WMI_EVT_OVERHEAD	10 /* in msecs */
249 
250 static const u32 ath11k_smps_map[] = {
251 	[WLAN_HT_CAP_SM_PS_STATIC] = WMI_PEER_SMPS_STATIC,
252 	[WLAN_HT_CAP_SM_PS_DYNAMIC] = WMI_PEER_SMPS_DYNAMIC,
253 	[WLAN_HT_CAP_SM_PS_INVALID] = WMI_PEER_SMPS_PS_NONE,
254 	[WLAN_HT_CAP_SM_PS_DISABLED] = WMI_PEER_SMPS_PS_NONE,
255 };
256 
257 static int ath11k_start_vdev_delay(struct ieee80211_hw *hw,
258 				   struct ieee80211_vif *vif);
259 
260 enum nl80211_he_ru_alloc ath11k_mac_phy_he_ru_to_nl80211_he_ru_alloc(u16 ru_phy)
261 {
262 	enum nl80211_he_ru_alloc ret;
263 
264 	switch (ru_phy) {
265 	case RU_26:
266 		ret = NL80211_RATE_INFO_HE_RU_ALLOC_26;
267 		break;
268 	case RU_52:
269 		ret = NL80211_RATE_INFO_HE_RU_ALLOC_52;
270 		break;
271 	case RU_106:
272 		ret = NL80211_RATE_INFO_HE_RU_ALLOC_106;
273 		break;
274 	case RU_242:
275 		ret = NL80211_RATE_INFO_HE_RU_ALLOC_242;
276 		break;
277 	case RU_484:
278 		ret = NL80211_RATE_INFO_HE_RU_ALLOC_484;
279 		break;
280 	case RU_996:
281 		ret = NL80211_RATE_INFO_HE_RU_ALLOC_996;
282 		break;
283 	default:
284 		ret = NL80211_RATE_INFO_HE_RU_ALLOC_26;
285 		break;
286 	}
287 
288 	return ret;
289 }
290 
291 enum nl80211_he_ru_alloc ath11k_mac_he_ru_tones_to_nl80211_he_ru_alloc(u16 ru_tones)
292 {
293 	enum nl80211_he_ru_alloc ret;
294 
295 	switch (ru_tones) {
296 	case 26:
297 		ret = NL80211_RATE_INFO_HE_RU_ALLOC_26;
298 		break;
299 	case 52:
300 		ret = NL80211_RATE_INFO_HE_RU_ALLOC_52;
301 		break;
302 	case 106:
303 		ret = NL80211_RATE_INFO_HE_RU_ALLOC_106;
304 		break;
305 	case 242:
306 		ret = NL80211_RATE_INFO_HE_RU_ALLOC_242;
307 		break;
308 	case 484:
309 		ret = NL80211_RATE_INFO_HE_RU_ALLOC_484;
310 		break;
311 	case 996:
312 		ret = NL80211_RATE_INFO_HE_RU_ALLOC_996;
313 		break;
314 	case (996 * 2):
315 		ret = NL80211_RATE_INFO_HE_RU_ALLOC_2x996;
316 		break;
317 	default:
318 		ret = NL80211_RATE_INFO_HE_RU_ALLOC_26;
319 		break;
320 	}
321 
322 	return ret;
323 }
324 
325 enum nl80211_he_gi ath11k_mac_he_gi_to_nl80211_he_gi(u8 sgi)
326 {
327 	enum nl80211_he_gi ret;
328 
329 	switch (sgi) {
330 	case RX_MSDU_START_SGI_0_8_US:
331 		ret = NL80211_RATE_INFO_HE_GI_0_8;
332 		break;
333 	case RX_MSDU_START_SGI_1_6_US:
334 		ret = NL80211_RATE_INFO_HE_GI_1_6;
335 		break;
336 	case RX_MSDU_START_SGI_3_2_US:
337 		ret = NL80211_RATE_INFO_HE_GI_3_2;
338 		break;
339 	default:
340 		ret = NL80211_RATE_INFO_HE_GI_0_8;
341 		break;
342 	}
343 
344 	return ret;
345 }
346 
347 u8 ath11k_mac_bw_to_mac80211_bw(u8 bw)
348 {
349 	u8 ret = 0;
350 
351 	switch (bw) {
352 	case ATH11K_BW_20:
353 		ret = RATE_INFO_BW_20;
354 		break;
355 	case ATH11K_BW_40:
356 		ret = RATE_INFO_BW_40;
357 		break;
358 	case ATH11K_BW_80:
359 		ret = RATE_INFO_BW_80;
360 		break;
361 	case ATH11K_BW_160:
362 		ret = RATE_INFO_BW_160;
363 		break;
364 	}
365 
366 	return ret;
367 }
368 
369 enum ath11k_supported_bw ath11k_mac_mac80211_bw_to_ath11k_bw(enum rate_info_bw bw)
370 {
371 	switch (bw) {
372 	case RATE_INFO_BW_20:
373 		return ATH11K_BW_20;
374 	case RATE_INFO_BW_40:
375 		return ATH11K_BW_40;
376 	case RATE_INFO_BW_80:
377 		return ATH11K_BW_80;
378 	case RATE_INFO_BW_160:
379 		return ATH11K_BW_160;
380 	default:
381 		return ATH11K_BW_20;
382 	}
383 }
384 
385 int ath11k_mac_hw_ratecode_to_legacy_rate(u8 hw_rc, u8 preamble, u8 *rateidx,
386 					  u16 *rate)
387 {
388 	/* As default, it is OFDM rates */
389 	int i = ATH11K_MAC_FIRST_OFDM_RATE_IDX;
390 	int max_rates_idx = ath11k_g_rates_size;
391 
392 	if (preamble == WMI_RATE_PREAMBLE_CCK) {
393 		hw_rc &= ~ATH11k_HW_RATECODE_CCK_SHORT_PREAM_MASK;
394 		i = 0;
395 		max_rates_idx = ATH11K_MAC_FIRST_OFDM_RATE_IDX;
396 	}
397 
398 	while (i < max_rates_idx) {
399 		if (hw_rc == ath11k_legacy_rates[i].hw_value) {
400 			*rateidx = i;
401 			*rate = ath11k_legacy_rates[i].bitrate;
402 			return 0;
403 		}
404 		i++;
405 	}
406 
407 	return -EINVAL;
408 }
409 
410 static int get_num_chains(u32 mask)
411 {
412 	int num_chains = 0;
413 
414 	while (mask) {
415 		if (mask & BIT(0))
416 			num_chains++;
417 		mask >>= 1;
418 	}
419 
420 	return num_chains;
421 }
422 
423 u8 ath11k_mac_bitrate_to_idx(const struct ieee80211_supported_band *sband,
424 			     u32 bitrate)
425 {
426 	int i;
427 
428 	for (i = 0; i < sband->n_bitrates; i++)
429 		if (sband->bitrates[i].bitrate == bitrate)
430 			return i;
431 
432 	return 0;
433 }
434 
435 static u32
436 ath11k_mac_max_ht_nss(const u8 *ht_mcs_mask)
437 {
438 	int nss;
439 
440 	for (nss = IEEE80211_HT_MCS_MASK_LEN - 1; nss >= 0; nss--)
441 		if (ht_mcs_mask[nss])
442 			return nss + 1;
443 
444 	return 1;
445 }
446 
447 static u32
448 ath11k_mac_max_vht_nss(const u16 *vht_mcs_mask)
449 {
450 	int nss;
451 
452 	for (nss = NL80211_VHT_NSS_MAX - 1; nss >= 0; nss--)
453 		if (vht_mcs_mask[nss])
454 			return nss + 1;
455 
456 	return 1;
457 }
458 
459 static u32
460 ath11k_mac_max_he_nss(const u16 *he_mcs_mask)
461 {
462 	int nss;
463 
464 	for (nss = NL80211_HE_NSS_MAX - 1; nss >= 0; nss--)
465 		if (he_mcs_mask[nss])
466 			return nss + 1;
467 
468 	return 1;
469 }
470 
471 static u8 ath11k_parse_mpdudensity(u8 mpdudensity)
472 {
473 /* 802.11n D2.0 defined values for "Minimum MPDU Start Spacing":
474  *   0 for no restriction
475  *   1 for 1/4 us
476  *   2 for 1/2 us
477  *   3 for 1 us
478  *   4 for 2 us
479  *   5 for 4 us
480  *   6 for 8 us
481  *   7 for 16 us
482  */
483 	switch (mpdudensity) {
484 	case 0:
485 		return 0;
486 	case 1:
487 	case 2:
488 	case 3:
489 	/* Our lower layer calculations limit our precision to
490 	 * 1 microsecond
491 	 */
492 		return 1;
493 	case 4:
494 		return 2;
495 	case 5:
496 		return 4;
497 	case 6:
498 		return 8;
499 	case 7:
500 		return 16;
501 	default:
502 		return 0;
503 	}
504 }
505 
506 static int ath11k_mac_vif_chan(struct ieee80211_vif *vif,
507 			       struct cfg80211_chan_def *def)
508 {
509 	struct ieee80211_chanctx_conf *conf;
510 
511 	rcu_read_lock();
512 	conf = rcu_dereference(vif->bss_conf.chanctx_conf);
513 	if (!conf) {
514 		rcu_read_unlock();
515 		return -ENOENT;
516 	}
517 
518 	*def = conf->def;
519 	rcu_read_unlock();
520 
521 	return 0;
522 }
523 
524 static bool ath11k_mac_bitrate_is_cck(int bitrate)
525 {
526 	switch (bitrate) {
527 	case 10:
528 	case 20:
529 	case 55:
530 	case 110:
531 		return true;
532 	}
533 
534 	return false;
535 }
536 
537 u8 ath11k_mac_hw_rate_to_idx(const struct ieee80211_supported_band *sband,
538 			     u8 hw_rate, bool cck)
539 {
540 	const struct ieee80211_rate *rate;
541 	int i;
542 
543 	for (i = 0; i < sband->n_bitrates; i++) {
544 		rate = &sband->bitrates[i];
545 
546 		if (ath11k_mac_bitrate_is_cck(rate->bitrate) != cck)
547 			continue;
548 
549 		if (rate->hw_value == hw_rate)
550 			return i;
551 		else if (rate->flags & IEEE80211_RATE_SHORT_PREAMBLE &&
552 			 rate->hw_value_short == hw_rate)
553 			return i;
554 	}
555 
556 	return 0;
557 }
558 
559 static u8 ath11k_mac_bitrate_to_rate(int bitrate)
560 {
561 	return DIV_ROUND_UP(bitrate, 5) |
562 	       (ath11k_mac_bitrate_is_cck(bitrate) ? BIT(7) : 0);
563 }
564 
565 static void ath11k_get_arvif_iter(void *data, u8 *mac,
566 				  struct ieee80211_vif *vif)
567 {
568 	struct ath11k_vif_iter *arvif_iter = data;
569 	struct ath11k_vif *arvif = (void *)vif->drv_priv;
570 
571 	if (arvif->vdev_id == arvif_iter->vdev_id)
572 		arvif_iter->arvif = arvif;
573 }
574 
575 struct ath11k_vif *ath11k_mac_get_arvif(struct ath11k *ar, u32 vdev_id)
576 {
577 	struct ath11k_vif_iter arvif_iter;
578 	u32 flags;
579 
580 	memset(&arvif_iter, 0, sizeof(struct ath11k_vif_iter));
581 	arvif_iter.vdev_id = vdev_id;
582 
583 	flags = IEEE80211_IFACE_ITER_RESUME_ALL;
584 	ieee80211_iterate_active_interfaces_atomic(ar->hw,
585 						   flags,
586 						   ath11k_get_arvif_iter,
587 						   &arvif_iter);
588 	if (!arvif_iter.arvif) {
589 		ath11k_warn(ar->ab, "No VIF found for vdev %d\n", vdev_id);
590 		return NULL;
591 	}
592 
593 	return arvif_iter.arvif;
594 }
595 
596 struct ath11k_vif *ath11k_mac_get_arvif_by_vdev_id(struct ath11k_base *ab,
597 						   u32 vdev_id)
598 {
599 	int i;
600 	struct ath11k_pdev *pdev;
601 	struct ath11k_vif *arvif;
602 
603 	for (i = 0; i < ab->num_radios; i++) {
604 		pdev = rcu_dereference(ab->pdevs_active[i]);
605 		if (pdev && pdev->ar &&
606 		    (pdev->ar->allocated_vdev_map & (1LL << vdev_id))) {
607 			arvif = ath11k_mac_get_arvif(pdev->ar, vdev_id);
608 			if (arvif)
609 				return arvif;
610 		}
611 	}
612 
613 	return NULL;
614 }
615 
616 struct ath11k *ath11k_mac_get_ar_by_vdev_id(struct ath11k_base *ab, u32 vdev_id)
617 {
618 	int i;
619 	struct ath11k_pdev *pdev;
620 
621 	for (i = 0; i < ab->num_radios; i++) {
622 		pdev = rcu_dereference(ab->pdevs_active[i]);
623 		if (pdev && pdev->ar) {
624 			if (pdev->ar->allocated_vdev_map & (1LL << vdev_id))
625 				return pdev->ar;
626 		}
627 	}
628 
629 	return NULL;
630 }
631 
632 struct ath11k *ath11k_mac_get_ar_by_pdev_id(struct ath11k_base *ab, u32 pdev_id)
633 {
634 	int i;
635 	struct ath11k_pdev *pdev;
636 
637 	if (ab->hw_params.single_pdev_only) {
638 		pdev = rcu_dereference(ab->pdevs_active[0]);
639 		return pdev ? pdev->ar : NULL;
640 	}
641 
642 	if (WARN_ON(pdev_id > ab->num_radios))
643 		return NULL;
644 
645 	for (i = 0; i < ab->num_radios; i++) {
646 		pdev = rcu_dereference(ab->pdevs_active[i]);
647 
648 		if (pdev && pdev->pdev_id == pdev_id)
649 			return (pdev->ar ? pdev->ar : NULL);
650 	}
651 
652 	return NULL;
653 }
654 
655 struct ath11k_vif *ath11k_mac_get_vif_up(struct ath11k_base *ab)
656 {
657 	struct ath11k *ar;
658 	struct ath11k_pdev *pdev;
659 	struct ath11k_vif *arvif;
660 	int i;
661 
662 	for (i = 0; i < ab->num_radios; i++) {
663 		pdev = &ab->pdevs[i];
664 		ar = pdev->ar;
665 		list_for_each_entry(arvif, &ar->arvifs, list) {
666 			if (arvif->is_up)
667 				return arvif;
668 		}
669 	}
670 
671 	return NULL;
672 }
673 
674 static bool ath11k_mac_band_match(enum nl80211_band band1, enum WMI_HOST_WLAN_BAND band2)
675 {
676 	return (((band1 == NL80211_BAND_2GHZ) && (band2 & WMI_HOST_WLAN_2G_CAP)) ||
677 		(((band1 == NL80211_BAND_5GHZ) || (band1 == NL80211_BAND_6GHZ)) &&
678 		   (band2 & WMI_HOST_WLAN_5G_CAP)));
679 }
680 
681 u8 ath11k_mac_get_target_pdev_id_from_vif(struct ath11k_vif *arvif)
682 {
683 	struct ath11k *ar = arvif->ar;
684 	struct ath11k_base *ab = ar->ab;
685 	struct ieee80211_vif *vif = arvif->vif;
686 	struct cfg80211_chan_def def;
687 	enum nl80211_band band;
688 	u8 pdev_id = ab->target_pdev_ids[0].pdev_id;
689 	int i;
690 
691 	if (WARN_ON(ath11k_mac_vif_chan(vif, &def)))
692 		return pdev_id;
693 
694 	band = def.chan->band;
695 
696 	for (i = 0; i < ab->target_pdev_count; i++) {
697 		if (ath11k_mac_band_match(band, ab->target_pdev_ids[i].supported_bands))
698 			return ab->target_pdev_ids[i].pdev_id;
699 	}
700 
701 	return pdev_id;
702 }
703 
704 u8 ath11k_mac_get_target_pdev_id(struct ath11k *ar)
705 {
706 	struct ath11k_vif *arvif;
707 
708 	arvif = ath11k_mac_get_vif_up(ar->ab);
709 
710 	if (arvif)
711 		return ath11k_mac_get_target_pdev_id_from_vif(arvif);
712 	else
713 		return ar->ab->target_pdev_ids[0].pdev_id;
714 }
715 
716 static void ath11k_pdev_caps_update(struct ath11k *ar)
717 {
718 	struct ath11k_base *ab = ar->ab;
719 
720 	ar->max_tx_power = ab->target_caps.hw_max_tx_power;
721 
722 	/* FIXME Set min_tx_power to ab->target_caps.hw_min_tx_power.
723 	 * But since the received value in svcrdy is same as hw_max_tx_power,
724 	 * we can set ar->min_tx_power to 0 currently until
725 	 * this is fixed in firmware
726 	 */
727 	ar->min_tx_power = 0;
728 
729 	ar->txpower_limit_2g = ar->max_tx_power;
730 	ar->txpower_limit_5g = ar->max_tx_power;
731 	ar->txpower_scale = WMI_HOST_TP_SCALE_MAX;
732 }
733 
734 static int ath11k_mac_txpower_recalc(struct ath11k *ar)
735 {
736 	struct ath11k_pdev *pdev = ar->pdev;
737 	struct ath11k_vif *arvif;
738 	int ret, txpower = -1;
739 	u32 param;
740 
741 	lockdep_assert_held(&ar->conf_mutex);
742 
743 	list_for_each_entry(arvif, &ar->arvifs, list) {
744 		if (arvif->txpower <= 0)
745 			continue;
746 
747 		if (txpower == -1)
748 			txpower = arvif->txpower;
749 		else
750 			txpower = min(txpower, arvif->txpower);
751 	}
752 
753 	if (txpower == -1)
754 		return 0;
755 
756 	/* txpwr is set as 2 units per dBm in FW*/
757 	txpower = min_t(u32, max_t(u32, ar->min_tx_power, txpower),
758 			ar->max_tx_power) * 2;
759 
760 	ath11k_dbg(ar->ab, ATH11K_DBG_MAC, "txpower to set in hw %d\n",
761 		   txpower / 2);
762 
763 	if ((pdev->cap.supported_bands & WMI_HOST_WLAN_2G_CAP) &&
764 	    ar->txpower_limit_2g != txpower) {
765 		param = WMI_PDEV_PARAM_TXPOWER_LIMIT2G;
766 		ret = ath11k_wmi_pdev_set_param(ar, param,
767 						txpower, ar->pdev->pdev_id);
768 		if (ret)
769 			goto fail;
770 		ar->txpower_limit_2g = txpower;
771 	}
772 
773 	if ((pdev->cap.supported_bands & WMI_HOST_WLAN_5G_CAP) &&
774 	    ar->txpower_limit_5g != txpower) {
775 		param = WMI_PDEV_PARAM_TXPOWER_LIMIT5G;
776 		ret = ath11k_wmi_pdev_set_param(ar, param,
777 						txpower, ar->pdev->pdev_id);
778 		if (ret)
779 			goto fail;
780 		ar->txpower_limit_5g = txpower;
781 	}
782 
783 	return 0;
784 
785 fail:
786 	ath11k_warn(ar->ab, "failed to recalc txpower limit %d using pdev param %d: %d\n",
787 		    txpower / 2, param, ret);
788 	return ret;
789 }
790 
791 static int ath11k_recalc_rtscts_prot(struct ath11k_vif *arvif)
792 {
793 	struct ath11k *ar = arvif->ar;
794 	u32 vdev_param, rts_cts = 0;
795 	int ret;
796 
797 	lockdep_assert_held(&ar->conf_mutex);
798 
799 	vdev_param = WMI_VDEV_PARAM_ENABLE_RTSCTS;
800 
801 	/* Enable RTS/CTS protection for sw retries (when legacy stations
802 	 * are in BSS) or by default only for second rate series.
803 	 * TODO: Check if we need to enable CTS 2 Self in any case
804 	 */
805 	rts_cts = WMI_USE_RTS_CTS;
806 
807 	if (arvif->num_legacy_stations > 0)
808 		rts_cts |= WMI_RTSCTS_ACROSS_SW_RETRIES << 4;
809 	else
810 		rts_cts |= WMI_RTSCTS_FOR_SECOND_RATESERIES << 4;
811 
812 	/* Need not send duplicate param value to firmware */
813 	if (arvif->rtscts_prot_mode == rts_cts)
814 		return 0;
815 
816 	arvif->rtscts_prot_mode = rts_cts;
817 
818 	ath11k_dbg(ar->ab, ATH11K_DBG_MAC, "mac vdev %d recalc rts/cts prot %d\n",
819 		   arvif->vdev_id, rts_cts);
820 
821 	ret =  ath11k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
822 					     vdev_param, rts_cts);
823 	if (ret)
824 		ath11k_warn(ar->ab, "failed to recalculate rts/cts prot for vdev %d: %d\n",
825 			    arvif->vdev_id, ret);
826 
827 	return ret;
828 }
829 
830 static int ath11k_mac_set_kickout(struct ath11k_vif *arvif)
831 {
832 	struct ath11k *ar = arvif->ar;
833 	u32 param;
834 	int ret;
835 
836 	ret = ath11k_wmi_pdev_set_param(ar, WMI_PDEV_PARAM_STA_KICKOUT_TH,
837 					ATH11K_KICKOUT_THRESHOLD,
838 					ar->pdev->pdev_id);
839 	if (ret) {
840 		ath11k_warn(ar->ab, "failed to set kickout threshold on vdev %i: %d\n",
841 			    arvif->vdev_id, ret);
842 		return ret;
843 	}
844 
845 	param = WMI_VDEV_PARAM_AP_KEEPALIVE_MIN_IDLE_INACTIVE_TIME_SECS;
846 	ret = ath11k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id, param,
847 					    ATH11K_KEEPALIVE_MIN_IDLE);
848 	if (ret) {
849 		ath11k_warn(ar->ab, "failed to set keepalive minimum idle time on vdev %i: %d\n",
850 			    arvif->vdev_id, ret);
851 		return ret;
852 	}
853 
854 	param = WMI_VDEV_PARAM_AP_KEEPALIVE_MAX_IDLE_INACTIVE_TIME_SECS;
855 	ret = ath11k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id, param,
856 					    ATH11K_KEEPALIVE_MAX_IDLE);
857 	if (ret) {
858 		ath11k_warn(ar->ab, "failed to set keepalive maximum idle time on vdev %i: %d\n",
859 			    arvif->vdev_id, ret);
860 		return ret;
861 	}
862 
863 	param = WMI_VDEV_PARAM_AP_KEEPALIVE_MAX_UNRESPONSIVE_TIME_SECS;
864 	ret = ath11k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id, param,
865 					    ATH11K_KEEPALIVE_MAX_UNRESPONSIVE);
866 	if (ret) {
867 		ath11k_warn(ar->ab, "failed to set keepalive maximum unresponsive time on vdev %i: %d\n",
868 			    arvif->vdev_id, ret);
869 		return ret;
870 	}
871 
872 	return 0;
873 }
874 
875 void ath11k_mac_peer_cleanup_all(struct ath11k *ar)
876 {
877 	struct ath11k_peer *peer, *tmp;
878 	struct ath11k_base *ab = ar->ab;
879 
880 	lockdep_assert_held(&ar->conf_mutex);
881 
882 	mutex_lock(&ab->tbl_mtx_lock);
883 	spin_lock_bh(&ab->base_lock);
884 	list_for_each_entry_safe(peer, tmp, &ab->peers, list) {
885 		ath11k_peer_rx_tid_cleanup(ar, peer);
886 		ath11k_peer_rhash_delete(ab, peer);
887 		list_del(&peer->list);
888 		kfree(peer);
889 	}
890 	spin_unlock_bh(&ab->base_lock);
891 	mutex_unlock(&ab->tbl_mtx_lock);
892 
893 	ar->num_peers = 0;
894 	ar->num_stations = 0;
895 }
896 
897 static inline int ath11k_mac_vdev_setup_sync(struct ath11k *ar)
898 {
899 	lockdep_assert_held(&ar->conf_mutex);
900 
901 	if (test_bit(ATH11K_FLAG_CRASH_FLUSH, &ar->ab->dev_flags))
902 		return -ESHUTDOWN;
903 
904 	if (!wait_for_completion_timeout(&ar->vdev_setup_done,
905 					 ATH11K_VDEV_SETUP_TIMEOUT_HZ))
906 		return -ETIMEDOUT;
907 
908 	return ar->last_wmi_vdev_start_status ? -EINVAL : 0;
909 }
910 
911 static void
912 ath11k_mac_get_any_chandef_iter(struct ieee80211_hw *hw,
913 				struct ieee80211_chanctx_conf *conf,
914 				void *data)
915 {
916 	struct cfg80211_chan_def **def = data;
917 
918 	*def = &conf->def;
919 }
920 
921 static int ath11k_mac_monitor_vdev_start(struct ath11k *ar, int vdev_id,
922 					 struct cfg80211_chan_def *chandef)
923 {
924 	struct ieee80211_channel *channel;
925 	struct wmi_vdev_start_req_arg arg = {};
926 	int ret;
927 
928 	lockdep_assert_held(&ar->conf_mutex);
929 
930 	channel = chandef->chan;
931 
932 	arg.vdev_id = vdev_id;
933 	arg.channel.freq = channel->center_freq;
934 	arg.channel.band_center_freq1 = chandef->center_freq1;
935 	arg.channel.band_center_freq2 = chandef->center_freq2;
936 
937 	arg.channel.mode = ath11k_phymodes[chandef->chan->band][chandef->width];
938 	arg.channel.chan_radar = !!(channel->flags & IEEE80211_CHAN_RADAR);
939 
940 	arg.channel.min_power = 0;
941 	arg.channel.max_power = channel->max_power;
942 	arg.channel.max_reg_power = channel->max_reg_power;
943 	arg.channel.max_antenna_gain = channel->max_antenna_gain;
944 
945 	arg.pref_tx_streams = ar->num_tx_chains;
946 	arg.pref_rx_streams = ar->num_rx_chains;
947 
948 	arg.channel.passive = !!(chandef->chan->flags & IEEE80211_CHAN_NO_IR);
949 
950 	reinit_completion(&ar->vdev_setup_done);
951 	reinit_completion(&ar->vdev_delete_done);
952 
953 	ret = ath11k_wmi_vdev_start(ar, &arg, false);
954 	if (ret) {
955 		ath11k_warn(ar->ab, "failed to request monitor vdev %i start: %d\n",
956 			    vdev_id, ret);
957 		return ret;
958 	}
959 
960 	ret = ath11k_mac_vdev_setup_sync(ar);
961 	if (ret) {
962 		ath11k_warn(ar->ab, "failed to synchronize setup for monitor vdev %i start: %d\n",
963 			    vdev_id, ret);
964 		return ret;
965 	}
966 
967 	ret = ath11k_wmi_vdev_up(ar, vdev_id, 0, ar->mac_addr, NULL, 0, 0);
968 	if (ret) {
969 		ath11k_warn(ar->ab, "failed to put up monitor vdev %i: %d\n",
970 			    vdev_id, ret);
971 		goto vdev_stop;
972 	}
973 
974 	ath11k_dbg(ar->ab, ATH11K_DBG_MAC, "mac monitor vdev %i started\n",
975 		   vdev_id);
976 
977 	return 0;
978 
979 vdev_stop:
980 	reinit_completion(&ar->vdev_setup_done);
981 
982 	ret = ath11k_wmi_vdev_stop(ar, vdev_id);
983 	if (ret) {
984 		ath11k_warn(ar->ab, "failed to stop monitor vdev %i after start failure: %d\n",
985 			    vdev_id, ret);
986 		return ret;
987 	}
988 
989 	ret = ath11k_mac_vdev_setup_sync(ar);
990 	if (ret) {
991 		ath11k_warn(ar->ab, "failed to synchronize setup for vdev %i stop: %d\n",
992 			    vdev_id, ret);
993 		return ret;
994 	}
995 
996 	return -EIO;
997 }
998 
999 static int ath11k_mac_monitor_vdev_stop(struct ath11k *ar)
1000 {
1001 	int ret;
1002 
1003 	lockdep_assert_held(&ar->conf_mutex);
1004 
1005 	reinit_completion(&ar->vdev_setup_done);
1006 
1007 	ret = ath11k_wmi_vdev_stop(ar, ar->monitor_vdev_id);
1008 	if (ret) {
1009 		ath11k_warn(ar->ab, "failed to request monitor vdev %i stop: %d\n",
1010 			    ar->monitor_vdev_id, ret);
1011 		return ret;
1012 	}
1013 
1014 	ret = ath11k_mac_vdev_setup_sync(ar);
1015 	if (ret) {
1016 		ath11k_warn(ar->ab, "failed to synchronize monitor vdev %i stop: %d\n",
1017 			    ar->monitor_vdev_id, ret);
1018 		return ret;
1019 	}
1020 
1021 	ret = ath11k_wmi_vdev_down(ar, ar->monitor_vdev_id);
1022 	if (ret) {
1023 		ath11k_warn(ar->ab, "failed to put down monitor vdev %i: %d\n",
1024 			    ar->monitor_vdev_id, ret);
1025 		return ret;
1026 	}
1027 
1028 	ath11k_dbg(ar->ab, ATH11K_DBG_MAC, "mac monitor vdev %i stopped\n",
1029 		   ar->monitor_vdev_id);
1030 
1031 	return 0;
1032 }
1033 
1034 static int ath11k_mac_monitor_vdev_create(struct ath11k *ar)
1035 {
1036 	struct ath11k_pdev *pdev = ar->pdev;
1037 	struct vdev_create_params param = {};
1038 	int bit, ret;
1039 	u8 tmp_addr[6] = {0};
1040 	u16 nss;
1041 
1042 	lockdep_assert_held(&ar->conf_mutex);
1043 
1044 	if (test_bit(ATH11K_FLAG_MONITOR_VDEV_CREATED, &ar->monitor_flags))
1045 		return 0;
1046 
1047 	if (ar->ab->free_vdev_map == 0) {
1048 		ath11k_warn(ar->ab, "failed to find free vdev id for monitor vdev\n");
1049 		return -ENOMEM;
1050 	}
1051 
1052 	bit = __ffs64(ar->ab->free_vdev_map);
1053 
1054 	ar->monitor_vdev_id = bit;
1055 
1056 	param.if_id = ar->monitor_vdev_id;
1057 	param.type = WMI_VDEV_TYPE_MONITOR;
1058 	param.subtype = WMI_VDEV_SUBTYPE_NONE;
1059 	param.pdev_id = pdev->pdev_id;
1060 
1061 	if (pdev->cap.supported_bands & WMI_HOST_WLAN_2G_CAP) {
1062 		param.chains[NL80211_BAND_2GHZ].tx = ar->num_tx_chains;
1063 		param.chains[NL80211_BAND_2GHZ].rx = ar->num_rx_chains;
1064 	}
1065 	if (pdev->cap.supported_bands & WMI_HOST_WLAN_5G_CAP) {
1066 		param.chains[NL80211_BAND_5GHZ].tx = ar->num_tx_chains;
1067 		param.chains[NL80211_BAND_5GHZ].rx = ar->num_rx_chains;
1068 	}
1069 
1070 	ret = ath11k_wmi_vdev_create(ar, tmp_addr, &param);
1071 	if (ret) {
1072 		ath11k_warn(ar->ab, "failed to request monitor vdev %i creation: %d\n",
1073 			    ar->monitor_vdev_id, ret);
1074 		ar->monitor_vdev_id = -1;
1075 		return ret;
1076 	}
1077 
1078 	nss = get_num_chains(ar->cfg_tx_chainmask) ? : 1;
1079 	ret = ath11k_wmi_vdev_set_param_cmd(ar, ar->monitor_vdev_id,
1080 					    WMI_VDEV_PARAM_NSS, nss);
1081 	if (ret) {
1082 		ath11k_warn(ar->ab, "failed to set vdev %d chainmask 0x%x, nss %d :%d\n",
1083 			    ar->monitor_vdev_id, ar->cfg_tx_chainmask, nss, ret);
1084 		goto err_vdev_del;
1085 	}
1086 
1087 	ret = ath11k_mac_txpower_recalc(ar);
1088 	if (ret) {
1089 		ath11k_warn(ar->ab, "failed to recalc txpower for monitor vdev %d: %d\n",
1090 			    ar->monitor_vdev_id, ret);
1091 		goto err_vdev_del;
1092 	}
1093 
1094 	ar->allocated_vdev_map |= 1LL << ar->monitor_vdev_id;
1095 	ar->ab->free_vdev_map &= ~(1LL << ar->monitor_vdev_id);
1096 	ar->num_created_vdevs++;
1097 	set_bit(ATH11K_FLAG_MONITOR_VDEV_CREATED, &ar->monitor_flags);
1098 
1099 	ath11k_dbg(ar->ab, ATH11K_DBG_MAC, "mac monitor vdev %d created\n",
1100 		   ar->monitor_vdev_id);
1101 
1102 	return 0;
1103 
1104 err_vdev_del:
1105 	ath11k_wmi_vdev_delete(ar, ar->monitor_vdev_id);
1106 	ar->monitor_vdev_id = -1;
1107 	return ret;
1108 }
1109 
1110 static int ath11k_mac_monitor_vdev_delete(struct ath11k *ar)
1111 {
1112 	int ret;
1113 	unsigned long time_left;
1114 
1115 	lockdep_assert_held(&ar->conf_mutex);
1116 
1117 	if (!test_bit(ATH11K_FLAG_MONITOR_VDEV_CREATED, &ar->monitor_flags))
1118 		return 0;
1119 
1120 	reinit_completion(&ar->vdev_delete_done);
1121 
1122 	ret = ath11k_wmi_vdev_delete(ar, ar->monitor_vdev_id);
1123 	if (ret) {
1124 		ath11k_warn(ar->ab, "failed to request wmi monitor vdev %i removal: %d\n",
1125 			    ar->monitor_vdev_id, ret);
1126 		return ret;
1127 	}
1128 
1129 	time_left = wait_for_completion_timeout(&ar->vdev_delete_done,
1130 						ATH11K_VDEV_DELETE_TIMEOUT_HZ);
1131 	if (time_left == 0) {
1132 		ath11k_warn(ar->ab, "Timeout in receiving vdev delete response\n");
1133 	} else {
1134 		ath11k_dbg(ar->ab, ATH11K_DBG_MAC, "mac monitor vdev %d deleted\n",
1135 			   ar->monitor_vdev_id);
1136 
1137 		ar->allocated_vdev_map &= ~(1LL << ar->monitor_vdev_id);
1138 		ar->ab->free_vdev_map |= 1LL << (ar->monitor_vdev_id);
1139 		ar->num_created_vdevs--;
1140 		ar->monitor_vdev_id = -1;
1141 		clear_bit(ATH11K_FLAG_MONITOR_VDEV_CREATED, &ar->monitor_flags);
1142 	}
1143 
1144 	return ret;
1145 }
1146 
1147 static int ath11k_mac_monitor_start(struct ath11k *ar)
1148 {
1149 	struct cfg80211_chan_def *chandef = NULL;
1150 	int ret;
1151 
1152 	lockdep_assert_held(&ar->conf_mutex);
1153 
1154 	if (test_bit(ATH11K_FLAG_MONITOR_STARTED, &ar->monitor_flags))
1155 		return 0;
1156 
1157 	ieee80211_iter_chan_contexts_atomic(ar->hw,
1158 					    ath11k_mac_get_any_chandef_iter,
1159 					    &chandef);
1160 	if (!chandef)
1161 		return 0;
1162 
1163 	ret = ath11k_mac_monitor_vdev_start(ar, ar->monitor_vdev_id, chandef);
1164 	if (ret) {
1165 		ath11k_warn(ar->ab, "failed to start monitor vdev: %d\n", ret);
1166 		ath11k_mac_monitor_vdev_delete(ar);
1167 		return ret;
1168 	}
1169 
1170 	set_bit(ATH11K_FLAG_MONITOR_STARTED, &ar->monitor_flags);
1171 
1172 	ar->num_started_vdevs++;
1173 	ret = ath11k_dp_tx_htt_monitor_mode_ring_config(ar, false);
1174 	if (ret) {
1175 		ath11k_warn(ar->ab, "failed to configure htt monitor mode ring during start: %d",
1176 			    ret);
1177 		return ret;
1178 	}
1179 
1180 	ath11k_dbg(ar->ab, ATH11K_DBG_MAC, "mac monitor started\n");
1181 
1182 	return 0;
1183 }
1184 
1185 static int ath11k_mac_monitor_stop(struct ath11k *ar)
1186 {
1187 	int ret;
1188 
1189 	lockdep_assert_held(&ar->conf_mutex);
1190 
1191 	if (!test_bit(ATH11K_FLAG_MONITOR_STARTED, &ar->monitor_flags))
1192 		return 0;
1193 
1194 	ret = ath11k_mac_monitor_vdev_stop(ar);
1195 	if (ret) {
1196 		ath11k_warn(ar->ab, "failed to stop monitor vdev: %d\n", ret);
1197 		return ret;
1198 	}
1199 
1200 	clear_bit(ATH11K_FLAG_MONITOR_STARTED, &ar->monitor_flags);
1201 	ar->num_started_vdevs--;
1202 
1203 	ret = ath11k_dp_tx_htt_monitor_mode_ring_config(ar, true);
1204 	if (ret) {
1205 		ath11k_warn(ar->ab, "failed to configure htt monitor mode ring during stop: %d",
1206 			    ret);
1207 		return ret;
1208 	}
1209 
1210 	ath11k_dbg(ar->ab, ATH11K_DBG_MAC, "mac monitor stopped ret %d\n", ret);
1211 
1212 	return 0;
1213 }
1214 
1215 static int ath11k_mac_vif_setup_ps(struct ath11k_vif *arvif)
1216 {
1217 	struct ath11k *ar = arvif->ar;
1218 	struct ieee80211_vif *vif = arvif->vif;
1219 	struct ieee80211_conf *conf = &ar->hw->conf;
1220 	enum wmi_sta_powersave_param param;
1221 	enum wmi_sta_ps_mode psmode;
1222 	int ret;
1223 	int timeout;
1224 	bool enable_ps;
1225 
1226 	lockdep_assert_held(&arvif->ar->conf_mutex);
1227 
1228 	if (arvif->vif->type != NL80211_IFTYPE_STATION)
1229 		return 0;
1230 
1231 	enable_ps = arvif->ps;
1232 
1233 	if (!arvif->is_started) {
1234 		/* mac80211 can update vif powersave state while disconnected.
1235 		 * Firmware doesn't behave nicely and consumes more power than
1236 		 * necessary if PS is disabled on a non-started vdev. Hence
1237 		 * force-enable PS for non-running vdevs.
1238 		 */
1239 		psmode = WMI_STA_PS_MODE_ENABLED;
1240 	} else if (enable_ps) {
1241 		psmode = WMI_STA_PS_MODE_ENABLED;
1242 		param = WMI_STA_PS_PARAM_INACTIVITY_TIME;
1243 
1244 		timeout = conf->dynamic_ps_timeout;
1245 		if (timeout == 0) {
1246 			/* firmware doesn't like 0 */
1247 			timeout = ieee80211_tu_to_usec(vif->bss_conf.beacon_int) / 1000;
1248 		}
1249 
1250 		ret = ath11k_wmi_set_sta_ps_param(ar, arvif->vdev_id, param,
1251 						  timeout);
1252 		if (ret) {
1253 			ath11k_warn(ar->ab, "failed to set inactivity time for vdev %d: %i\n",
1254 				    arvif->vdev_id, ret);
1255 			return ret;
1256 		}
1257 	} else {
1258 		psmode = WMI_STA_PS_MODE_DISABLED;
1259 	}
1260 
1261 	ath11k_dbg(ar->ab, ATH11K_DBG_MAC, "mac vdev %d psmode %s\n",
1262 		   arvif->vdev_id, psmode ? "enable" : "disable");
1263 
1264 	ret = ath11k_wmi_pdev_set_ps_mode(ar, arvif->vdev_id, psmode);
1265 	if (ret) {
1266 		ath11k_warn(ar->ab, "failed to set sta power save mode %d for vdev %d: %d\n",
1267 			    psmode, arvif->vdev_id, ret);
1268 		return ret;
1269 	}
1270 
1271 	return 0;
1272 }
1273 
1274 static int ath11k_mac_config_ps(struct ath11k *ar)
1275 {
1276 	struct ath11k_vif *arvif;
1277 	int ret = 0;
1278 
1279 	lockdep_assert_held(&ar->conf_mutex);
1280 
1281 	list_for_each_entry(arvif, &ar->arvifs, list) {
1282 		ret = ath11k_mac_vif_setup_ps(arvif);
1283 		if (ret) {
1284 			ath11k_warn(ar->ab, "failed to setup powersave: %d\n", ret);
1285 			break;
1286 		}
1287 	}
1288 
1289 	return ret;
1290 }
1291 
1292 static int ath11k_mac_op_config(struct ieee80211_hw *hw, u32 changed)
1293 {
1294 	struct ath11k *ar = hw->priv;
1295 	struct ieee80211_conf *conf = &hw->conf;
1296 	int ret = 0;
1297 
1298 	mutex_lock(&ar->conf_mutex);
1299 
1300 	if (changed & IEEE80211_CONF_CHANGE_MONITOR) {
1301 		if (conf->flags & IEEE80211_CONF_MONITOR) {
1302 			set_bit(ATH11K_FLAG_MONITOR_CONF_ENABLED, &ar->monitor_flags);
1303 
1304 			if (test_bit(ATH11K_FLAG_MONITOR_VDEV_CREATED,
1305 				     &ar->monitor_flags))
1306 				goto out;
1307 
1308 			ret = ath11k_mac_monitor_vdev_create(ar);
1309 			if (ret) {
1310 				ath11k_warn(ar->ab, "failed to create monitor vdev: %d",
1311 					    ret);
1312 				goto out;
1313 			}
1314 
1315 			ret = ath11k_mac_monitor_start(ar);
1316 			if (ret) {
1317 				ath11k_warn(ar->ab, "failed to start monitor: %d",
1318 					    ret);
1319 				goto err_mon_del;
1320 			}
1321 		} else {
1322 			clear_bit(ATH11K_FLAG_MONITOR_CONF_ENABLED, &ar->monitor_flags);
1323 
1324 			if (!test_bit(ATH11K_FLAG_MONITOR_VDEV_CREATED,
1325 				      &ar->monitor_flags))
1326 				goto out;
1327 
1328 			ret = ath11k_mac_monitor_stop(ar);
1329 			if (ret) {
1330 				ath11k_warn(ar->ab, "failed to stop monitor: %d",
1331 					    ret);
1332 				goto out;
1333 			}
1334 
1335 			ret = ath11k_mac_monitor_vdev_delete(ar);
1336 			if (ret) {
1337 				ath11k_warn(ar->ab, "failed to delete monitor vdev: %d",
1338 					    ret);
1339 				goto out;
1340 			}
1341 		}
1342 	}
1343 
1344 out:
1345 	mutex_unlock(&ar->conf_mutex);
1346 	return ret;
1347 
1348 err_mon_del:
1349 	ath11k_mac_monitor_vdev_delete(ar);
1350 	mutex_unlock(&ar->conf_mutex);
1351 	return ret;
1352 }
1353 
1354 static void ath11k_mac_setup_nontx_vif_rsnie(struct ath11k_vif *arvif,
1355 					     bool tx_arvif_rsnie_present,
1356 					     const u8 *profile, u8 profile_len)
1357 {
1358 	if (cfg80211_find_ie(WLAN_EID_RSN, profile, profile_len)) {
1359 		arvif->rsnie_present = true;
1360 	} else if (tx_arvif_rsnie_present) {
1361 		int i;
1362 		u8 nie_len;
1363 		const u8 *nie = cfg80211_find_ext_ie(WLAN_EID_EXT_NON_INHERITANCE,
1364 						     profile, profile_len);
1365 		if (!nie)
1366 			return;
1367 
1368 		nie_len = nie[1];
1369 		nie += 2;
1370 		for (i = 0; i < nie_len; i++) {
1371 			if (nie[i] == WLAN_EID_RSN) {
1372 				arvif->rsnie_present = false;
1373 				break;
1374 			}
1375 		}
1376 	}
1377 }
1378 
1379 static bool ath11k_mac_set_nontx_vif_params(struct ath11k_vif *tx_arvif,
1380 					    struct ath11k_vif *arvif,
1381 					    struct sk_buff *bcn)
1382 {
1383 	struct ieee80211_mgmt *mgmt;
1384 	const u8 *ies, *profile, *next_profile;
1385 	int ies_len;
1386 
1387 	ies = bcn->data + ieee80211_get_hdrlen_from_skb(bcn);
1388 	mgmt = (struct ieee80211_mgmt *)bcn->data;
1389 	ies += sizeof(mgmt->u.beacon);
1390 	ies_len = skb_tail_pointer(bcn) - ies;
1391 
1392 	ies = cfg80211_find_ie(WLAN_EID_MULTIPLE_BSSID, ies, ies_len);
1393 	arvif->rsnie_present = tx_arvif->rsnie_present;
1394 
1395 	while (ies) {
1396 		u8 mbssid_len;
1397 
1398 		ies_len -= (2 + ies[1]);
1399 		mbssid_len = ies[1] - 1;
1400 		profile = &ies[3];
1401 
1402 		while (mbssid_len) {
1403 			u8 profile_len;
1404 
1405 			profile_len = profile[1];
1406 			next_profile = profile + (2 + profile_len);
1407 			mbssid_len -= (2 + profile_len);
1408 
1409 			profile += 2;
1410 			profile_len -= (2 + profile[1]);
1411 			profile += (2 + profile[1]); /* nontx capabilities */
1412 			profile_len -= (2 + profile[1]);
1413 			profile += (2 + profile[1]); /* SSID */
1414 			if (profile[2] == arvif->vif->bss_conf.bssid_index) {
1415 				profile_len -= 5;
1416 				profile = profile + 5;
1417 				ath11k_mac_setup_nontx_vif_rsnie(arvif,
1418 								 tx_arvif->rsnie_present,
1419 								 profile,
1420 								 profile_len);
1421 				return true;
1422 			}
1423 			profile = next_profile;
1424 		}
1425 		ies = cfg80211_find_ie(WLAN_EID_MULTIPLE_BSSID, profile,
1426 				       ies_len);
1427 	}
1428 
1429 	return false;
1430 }
1431 
1432 static void ath11k_mac_set_vif_params(struct ath11k_vif *arvif,
1433 				      struct sk_buff *bcn)
1434 {
1435 	struct ieee80211_mgmt *mgmt;
1436 	u8 *ies;
1437 
1438 	ies = bcn->data + ieee80211_get_hdrlen_from_skb(bcn);
1439 	mgmt = (struct ieee80211_mgmt *)bcn->data;
1440 	ies += sizeof(mgmt->u.beacon);
1441 
1442 	if (cfg80211_find_ie(WLAN_EID_RSN, ies, (skb_tail_pointer(bcn) - ies)))
1443 		arvif->rsnie_present = true;
1444 	else
1445 		arvif->rsnie_present = false;
1446 
1447 	if (cfg80211_find_vendor_ie(WLAN_OUI_MICROSOFT,
1448 				    WLAN_OUI_TYPE_MICROSOFT_WPA,
1449 				    ies, (skb_tail_pointer(bcn) - ies)))
1450 		arvif->wpaie_present = true;
1451 	else
1452 		arvif->wpaie_present = false;
1453 }
1454 
1455 static int ath11k_mac_setup_bcn_tmpl_mbssid(struct ath11k_vif *arvif)
1456 {
1457 	struct ath11k *ar = arvif->ar;
1458 	struct ath11k_base *ab = ar->ab;
1459 	struct ath11k_vif *tx_arvif = arvif;
1460 	struct ieee80211_hw *hw = ar->hw;
1461 	struct ieee80211_vif *vif = arvif->vif;
1462 	struct ieee80211_mutable_offsets offs = {};
1463 	struct sk_buff *bcn;
1464 	int ret;
1465 
1466 	if (arvif->vif->mbssid_tx_vif) {
1467 		tx_arvif = (void *)arvif->vif->mbssid_tx_vif->drv_priv;
1468 		if (tx_arvif != arvif) {
1469 			ar = tx_arvif->ar;
1470 			ab = ar->ab;
1471 			hw = ar->hw;
1472 			vif = tx_arvif->vif;
1473 		}
1474 	}
1475 
1476 	bcn = ieee80211_beacon_get_template(hw, vif, &offs, 0);
1477 	if (!bcn) {
1478 		ath11k_warn(ab, "failed to get beacon template from mac80211\n");
1479 		return -EPERM;
1480 	}
1481 
1482 	if (tx_arvif == arvif)
1483 		ath11k_mac_set_vif_params(tx_arvif, bcn);
1484 	else if (!ath11k_mac_set_nontx_vif_params(tx_arvif, arvif, bcn))
1485 		return -EINVAL;
1486 
1487 	ret = ath11k_wmi_bcn_tmpl(ar, arvif->vdev_id, &offs, bcn);
1488 	kfree_skb(bcn);
1489 
1490 	if (ret)
1491 		ath11k_warn(ab, "failed to submit beacon template command: %d\n",
1492 			    ret);
1493 
1494 	return ret;
1495 }
1496 
1497 static int ath11k_mac_setup_bcn_tmpl(struct ath11k_vif *arvif)
1498 {
1499 	struct ieee80211_vif *vif = arvif->vif;
1500 
1501 	if (arvif->vdev_type != WMI_VDEV_TYPE_AP)
1502 		return 0;
1503 
1504 	/* Target does not expect beacon templates for the already up
1505 	 * non-transmitting interfaces, and results in a crash if sent.
1506 	 */
1507 	if (vif->mbssid_tx_vif &&
1508 	    arvif != (void *)vif->mbssid_tx_vif->drv_priv && arvif->is_up)
1509 		return 0;
1510 
1511 	return ath11k_mac_setup_bcn_tmpl_mbssid(arvif);
1512 }
1513 
1514 void ath11k_mac_bcn_tx_event(struct ath11k_vif *arvif)
1515 {
1516 	struct ieee80211_vif *vif = arvif->vif;
1517 
1518 	if (!vif->bss_conf.color_change_active && !arvif->bcca_zero_sent)
1519 		return;
1520 
1521 	if (vif->bss_conf.color_change_active &&
1522 	    ieee80211_beacon_cntdwn_is_complete(vif)) {
1523 		arvif->bcca_zero_sent = true;
1524 		ieee80211_color_change_finish(vif);
1525 		return;
1526 	}
1527 
1528 	arvif->bcca_zero_sent = false;
1529 
1530 	if (vif->bss_conf.color_change_active)
1531 		ieee80211_beacon_update_cntdwn(vif);
1532 	ath11k_mac_setup_bcn_tmpl(arvif);
1533 }
1534 
1535 static void ath11k_control_beaconing(struct ath11k_vif *arvif,
1536 				     struct ieee80211_bss_conf *info)
1537 {
1538 	struct ath11k *ar = arvif->ar;
1539 	struct ath11k_vif *tx_arvif = NULL;
1540 	int ret = 0;
1541 
1542 	lockdep_assert_held(&arvif->ar->conf_mutex);
1543 
1544 	if (!info->enable_beacon) {
1545 		ret = ath11k_wmi_vdev_down(ar, arvif->vdev_id);
1546 		if (ret)
1547 			ath11k_warn(ar->ab, "failed to down vdev_id %i: %d\n",
1548 				    arvif->vdev_id, ret);
1549 
1550 		arvif->is_up = false;
1551 		return;
1552 	}
1553 
1554 	/* Install the beacon template to the FW */
1555 	ret = ath11k_mac_setup_bcn_tmpl(arvif);
1556 	if (ret) {
1557 		ath11k_warn(ar->ab, "failed to update bcn tmpl during vdev up: %d\n",
1558 			    ret);
1559 		return;
1560 	}
1561 
1562 	arvif->tx_seq_no = 0x1000;
1563 
1564 	arvif->aid = 0;
1565 
1566 	ether_addr_copy(arvif->bssid, info->bssid);
1567 
1568 	if (arvif->vif->mbssid_tx_vif)
1569 		tx_arvif = (struct ath11k_vif *)arvif->vif->mbssid_tx_vif->drv_priv;
1570 
1571 	ret = ath11k_wmi_vdev_up(arvif->ar, arvif->vdev_id, arvif->aid,
1572 				 arvif->bssid,
1573 				 tx_arvif ? tx_arvif->bssid : NULL,
1574 				 info->bssid_index,
1575 				 1 << info->bssid_indicator);
1576 	if (ret) {
1577 		ath11k_warn(ar->ab, "failed to bring up vdev %d: %i\n",
1578 			    arvif->vdev_id, ret);
1579 		return;
1580 	}
1581 
1582 	arvif->is_up = true;
1583 
1584 	ath11k_dbg(ar->ab, ATH11K_DBG_MAC, "mac vdev %d up\n", arvif->vdev_id);
1585 }
1586 
1587 static void ath11k_mac_handle_beacon_iter(void *data, u8 *mac,
1588 					  struct ieee80211_vif *vif)
1589 {
1590 	struct sk_buff *skb = data;
1591 	struct ieee80211_mgmt *mgmt = (void *)skb->data;
1592 	struct ath11k_vif *arvif = (void *)vif->drv_priv;
1593 
1594 	if (vif->type != NL80211_IFTYPE_STATION)
1595 		return;
1596 
1597 	if (!ether_addr_equal(mgmt->bssid, vif->bss_conf.bssid))
1598 		return;
1599 
1600 	cancel_delayed_work(&arvif->connection_loss_work);
1601 }
1602 
1603 void ath11k_mac_handle_beacon(struct ath11k *ar, struct sk_buff *skb)
1604 {
1605 	ieee80211_iterate_active_interfaces_atomic(ar->hw,
1606 						   IEEE80211_IFACE_ITER_NORMAL,
1607 						   ath11k_mac_handle_beacon_iter,
1608 						   skb);
1609 }
1610 
1611 static void ath11k_mac_handle_beacon_miss_iter(void *data, u8 *mac,
1612 					       struct ieee80211_vif *vif)
1613 {
1614 	u32 *vdev_id = data;
1615 	struct ath11k_vif *arvif = (void *)vif->drv_priv;
1616 	struct ath11k *ar = arvif->ar;
1617 	struct ieee80211_hw *hw = ar->hw;
1618 
1619 	if (arvif->vdev_id != *vdev_id)
1620 		return;
1621 
1622 	if (!arvif->is_up)
1623 		return;
1624 
1625 	ieee80211_beacon_loss(vif);
1626 
1627 	/* Firmware doesn't report beacon loss events repeatedly. If AP probe
1628 	 * (done by mac80211) succeeds but beacons do not resume then it
1629 	 * doesn't make sense to continue operation. Queue connection loss work
1630 	 * which can be cancelled when beacon is received.
1631 	 */
1632 	ieee80211_queue_delayed_work(hw, &arvif->connection_loss_work,
1633 				     ATH11K_CONNECTION_LOSS_HZ);
1634 }
1635 
1636 void ath11k_mac_handle_beacon_miss(struct ath11k *ar, u32 vdev_id)
1637 {
1638 	ieee80211_iterate_active_interfaces_atomic(ar->hw,
1639 						   IEEE80211_IFACE_ITER_NORMAL,
1640 						   ath11k_mac_handle_beacon_miss_iter,
1641 						   &vdev_id);
1642 }
1643 
1644 static void ath11k_mac_vif_sta_connection_loss_work(struct work_struct *work)
1645 {
1646 	struct ath11k_vif *arvif = container_of(work, struct ath11k_vif,
1647 						connection_loss_work.work);
1648 	struct ieee80211_vif *vif = arvif->vif;
1649 
1650 	if (!arvif->is_up)
1651 		return;
1652 
1653 	ieee80211_connection_loss(vif);
1654 }
1655 
1656 static void ath11k_peer_assoc_h_basic(struct ath11k *ar,
1657 				      struct ieee80211_vif *vif,
1658 				      struct ieee80211_sta *sta,
1659 				      struct peer_assoc_params *arg)
1660 {
1661 	struct ath11k_vif *arvif = (void *)vif->drv_priv;
1662 	u32 aid;
1663 
1664 	lockdep_assert_held(&ar->conf_mutex);
1665 
1666 	if (vif->type == NL80211_IFTYPE_STATION)
1667 		aid = vif->cfg.aid;
1668 	else
1669 		aid = sta->aid;
1670 
1671 	ether_addr_copy(arg->peer_mac, sta->addr);
1672 	arg->vdev_id = arvif->vdev_id;
1673 	arg->peer_associd = aid;
1674 	arg->auth_flag = true;
1675 	/* TODO: STA WAR in ath10k for listen interval required? */
1676 	arg->peer_listen_intval = ar->hw->conf.listen_interval;
1677 	arg->peer_nss = 1;
1678 	arg->peer_caps = vif->bss_conf.assoc_capability;
1679 }
1680 
1681 static void ath11k_peer_assoc_h_crypto(struct ath11k *ar,
1682 				       struct ieee80211_vif *vif,
1683 				       struct ieee80211_sta *sta,
1684 				       struct peer_assoc_params *arg)
1685 {
1686 	struct ieee80211_bss_conf *info = &vif->bss_conf;
1687 	struct cfg80211_chan_def def;
1688 	struct cfg80211_bss *bss;
1689 	struct ath11k_vif *arvif = (struct ath11k_vif *)vif->drv_priv;
1690 	const u8 *rsnie = NULL;
1691 	const u8 *wpaie = NULL;
1692 
1693 	lockdep_assert_held(&ar->conf_mutex);
1694 
1695 	if (WARN_ON(ath11k_mac_vif_chan(vif, &def)))
1696 		return;
1697 
1698 	bss = cfg80211_get_bss(ar->hw->wiphy, def.chan, info->bssid, NULL, 0,
1699 			       IEEE80211_BSS_TYPE_ANY, IEEE80211_PRIVACY_ANY);
1700 
1701 	if (arvif->rsnie_present || arvif->wpaie_present) {
1702 		arg->need_ptk_4_way = true;
1703 		if (arvif->wpaie_present)
1704 			arg->need_gtk_2_way = true;
1705 	} else if (bss) {
1706 		const struct cfg80211_bss_ies *ies;
1707 
1708 		rcu_read_lock();
1709 		rsnie = ieee80211_bss_get_ie(bss, WLAN_EID_RSN);
1710 
1711 		ies = rcu_dereference(bss->ies);
1712 
1713 		wpaie = cfg80211_find_vendor_ie(WLAN_OUI_MICROSOFT,
1714 						WLAN_OUI_TYPE_MICROSOFT_WPA,
1715 						ies->data,
1716 						ies->len);
1717 		rcu_read_unlock();
1718 		cfg80211_put_bss(ar->hw->wiphy, bss);
1719 	}
1720 
1721 	/* FIXME: base on RSN IE/WPA IE is a correct idea? */
1722 	if (rsnie || wpaie) {
1723 		ath11k_dbg(ar->ab, ATH11K_DBG_WMI,
1724 			   "%s: rsn ie found\n", __func__);
1725 		arg->need_ptk_4_way = true;
1726 	}
1727 
1728 	if (wpaie) {
1729 		ath11k_dbg(ar->ab, ATH11K_DBG_WMI,
1730 			   "%s: wpa ie found\n", __func__);
1731 		arg->need_gtk_2_way = true;
1732 	}
1733 
1734 	if (sta->mfp) {
1735 		/* TODO: Need to check if FW supports PMF? */
1736 		arg->is_pmf_enabled = true;
1737 	}
1738 
1739 	/* TODO: safe_mode_enabled (bypass 4-way handshake) flag req? */
1740 }
1741 
1742 static void ath11k_peer_assoc_h_rates(struct ath11k *ar,
1743 				      struct ieee80211_vif *vif,
1744 				      struct ieee80211_sta *sta,
1745 				      struct peer_assoc_params *arg)
1746 {
1747 	struct ath11k_vif *arvif = (void *)vif->drv_priv;
1748 	struct wmi_rate_set_arg *rateset = &arg->peer_legacy_rates;
1749 	struct cfg80211_chan_def def;
1750 	const struct ieee80211_supported_band *sband;
1751 	const struct ieee80211_rate *rates;
1752 	enum nl80211_band band;
1753 	u32 ratemask;
1754 	u8 rate;
1755 	int i;
1756 
1757 	lockdep_assert_held(&ar->conf_mutex);
1758 
1759 	if (WARN_ON(ath11k_mac_vif_chan(vif, &def)))
1760 		return;
1761 
1762 	band = def.chan->band;
1763 	sband = ar->hw->wiphy->bands[band];
1764 	ratemask = sta->deflink.supp_rates[band];
1765 	ratemask &= arvif->bitrate_mask.control[band].legacy;
1766 	rates = sband->bitrates;
1767 
1768 	rateset->num_rates = 0;
1769 
1770 	for (i = 0; i < 32; i++, ratemask >>= 1, rates++) {
1771 		if (!(ratemask & 1))
1772 			continue;
1773 
1774 		rate = ath11k_mac_bitrate_to_rate(rates->bitrate);
1775 		rateset->rates[rateset->num_rates] = rate;
1776 		rateset->num_rates++;
1777 	}
1778 }
1779 
1780 static bool
1781 ath11k_peer_assoc_h_ht_masked(const u8 *ht_mcs_mask)
1782 {
1783 	int nss;
1784 
1785 	for (nss = 0; nss < IEEE80211_HT_MCS_MASK_LEN; nss++)
1786 		if (ht_mcs_mask[nss])
1787 			return false;
1788 
1789 	return true;
1790 }
1791 
1792 static bool
1793 ath11k_peer_assoc_h_vht_masked(const u16 *vht_mcs_mask)
1794 {
1795 	int nss;
1796 
1797 	for (nss = 0; nss < NL80211_VHT_NSS_MAX; nss++)
1798 		if (vht_mcs_mask[nss])
1799 			return false;
1800 
1801 	return true;
1802 }
1803 
1804 static void ath11k_peer_assoc_h_ht(struct ath11k *ar,
1805 				   struct ieee80211_vif *vif,
1806 				   struct ieee80211_sta *sta,
1807 				   struct peer_assoc_params *arg)
1808 {
1809 	const struct ieee80211_sta_ht_cap *ht_cap = &sta->deflink.ht_cap;
1810 	struct ath11k_vif *arvif = (void *)vif->drv_priv;
1811 	struct cfg80211_chan_def def;
1812 	enum nl80211_band band;
1813 	const u8 *ht_mcs_mask;
1814 	int i, n;
1815 	u8 max_nss;
1816 	u32 stbc;
1817 
1818 	lockdep_assert_held(&ar->conf_mutex);
1819 
1820 	if (WARN_ON(ath11k_mac_vif_chan(vif, &def)))
1821 		return;
1822 
1823 	if (!ht_cap->ht_supported)
1824 		return;
1825 
1826 	band = def.chan->band;
1827 	ht_mcs_mask = arvif->bitrate_mask.control[band].ht_mcs;
1828 
1829 	if (ath11k_peer_assoc_h_ht_masked(ht_mcs_mask))
1830 		return;
1831 
1832 	arg->ht_flag = true;
1833 
1834 	arg->peer_max_mpdu = (1 << (IEEE80211_HT_MAX_AMPDU_FACTOR +
1835 				    ht_cap->ampdu_factor)) - 1;
1836 
1837 	arg->peer_mpdu_density =
1838 		ath11k_parse_mpdudensity(ht_cap->ampdu_density);
1839 
1840 	arg->peer_ht_caps = ht_cap->cap;
1841 	arg->peer_rate_caps |= WMI_HOST_RC_HT_FLAG;
1842 
1843 	if (ht_cap->cap & IEEE80211_HT_CAP_LDPC_CODING)
1844 		arg->ldpc_flag = true;
1845 
1846 	if (sta->deflink.bandwidth >= IEEE80211_STA_RX_BW_40) {
1847 		arg->bw_40 = true;
1848 		arg->peer_rate_caps |= WMI_HOST_RC_CW40_FLAG;
1849 	}
1850 
1851 	/* As firmware handles this two flags (IEEE80211_HT_CAP_SGI_20
1852 	 * and IEEE80211_HT_CAP_SGI_40) for enabling SGI, we reset
1853 	 * both flags if guard interval is Default GI
1854 	 */
1855 	if (arvif->bitrate_mask.control[band].gi == NL80211_TXRATE_DEFAULT_GI)
1856 		arg->peer_ht_caps &= ~(IEEE80211_HT_CAP_SGI_20 |
1857 				IEEE80211_HT_CAP_SGI_40);
1858 
1859 	if (arvif->bitrate_mask.control[band].gi != NL80211_TXRATE_FORCE_LGI) {
1860 		if (ht_cap->cap & (IEEE80211_HT_CAP_SGI_20 |
1861 		    IEEE80211_HT_CAP_SGI_40))
1862 			arg->peer_rate_caps |= WMI_HOST_RC_SGI_FLAG;
1863 	}
1864 
1865 	if (ht_cap->cap & IEEE80211_HT_CAP_TX_STBC) {
1866 		arg->peer_rate_caps |= WMI_HOST_RC_TX_STBC_FLAG;
1867 		arg->stbc_flag = true;
1868 	}
1869 
1870 	if (ht_cap->cap & IEEE80211_HT_CAP_RX_STBC) {
1871 		stbc = ht_cap->cap & IEEE80211_HT_CAP_RX_STBC;
1872 		stbc = stbc >> IEEE80211_HT_CAP_RX_STBC_SHIFT;
1873 		stbc = stbc << WMI_HOST_RC_RX_STBC_FLAG_S;
1874 		arg->peer_rate_caps |= stbc;
1875 		arg->stbc_flag = true;
1876 	}
1877 
1878 	if (ht_cap->mcs.rx_mask[1] && ht_cap->mcs.rx_mask[2])
1879 		arg->peer_rate_caps |= WMI_HOST_RC_TS_FLAG;
1880 	else if (ht_cap->mcs.rx_mask[1])
1881 		arg->peer_rate_caps |= WMI_HOST_RC_DS_FLAG;
1882 
1883 	for (i = 0, n = 0, max_nss = 0; i < IEEE80211_HT_MCS_MASK_LEN * 8; i++)
1884 		if ((ht_cap->mcs.rx_mask[i / 8] & BIT(i % 8)) &&
1885 		    (ht_mcs_mask[i / 8] & BIT(i % 8))) {
1886 			max_nss = (i / 8) + 1;
1887 			arg->peer_ht_rates.rates[n++] = i;
1888 		}
1889 
1890 	/* This is a workaround for HT-enabled STAs which break the spec
1891 	 * and have no HT capabilities RX mask (no HT RX MCS map).
1892 	 *
1893 	 * As per spec, in section 20.3.5 Modulation and coding scheme (MCS),
1894 	 * MCS 0 through 7 are mandatory in 20MHz with 800 ns GI at all STAs.
1895 	 *
1896 	 * Firmware asserts if such situation occurs.
1897 	 */
1898 	if (n == 0) {
1899 		arg->peer_ht_rates.num_rates = 8;
1900 		for (i = 0; i < arg->peer_ht_rates.num_rates; i++)
1901 			arg->peer_ht_rates.rates[i] = i;
1902 	} else {
1903 		arg->peer_ht_rates.num_rates = n;
1904 		arg->peer_nss = min(sta->deflink.rx_nss, max_nss);
1905 	}
1906 
1907 	ath11k_dbg(ar->ab, ATH11K_DBG_MAC, "mac ht peer %pM mcs cnt %d nss %d\n",
1908 		   arg->peer_mac,
1909 		   arg->peer_ht_rates.num_rates,
1910 		   arg->peer_nss);
1911 }
1912 
1913 static int ath11k_mac_get_max_vht_mcs_map(u16 mcs_map, int nss)
1914 {
1915 	switch ((mcs_map >> (2 * nss)) & 0x3) {
1916 	case IEEE80211_VHT_MCS_SUPPORT_0_7: return BIT(8) - 1;
1917 	case IEEE80211_VHT_MCS_SUPPORT_0_8: return BIT(9) - 1;
1918 	case IEEE80211_VHT_MCS_SUPPORT_0_9: return BIT(10) - 1;
1919 	}
1920 	return 0;
1921 }
1922 
1923 static u16
1924 ath11k_peer_assoc_h_vht_limit(u16 tx_mcs_set,
1925 			      const u16 vht_mcs_limit[NL80211_VHT_NSS_MAX])
1926 {
1927 	int idx_limit;
1928 	int nss;
1929 	u16 mcs_map;
1930 	u16 mcs;
1931 
1932 	for (nss = 0; nss < NL80211_VHT_NSS_MAX; nss++) {
1933 		mcs_map = ath11k_mac_get_max_vht_mcs_map(tx_mcs_set, nss) &
1934 			  vht_mcs_limit[nss];
1935 
1936 		if (mcs_map)
1937 			idx_limit = fls(mcs_map) - 1;
1938 		else
1939 			idx_limit = -1;
1940 
1941 		switch (idx_limit) {
1942 		case 0:
1943 		case 1:
1944 		case 2:
1945 		case 3:
1946 		case 4:
1947 		case 5:
1948 		case 6:
1949 		case 7:
1950 			mcs = IEEE80211_VHT_MCS_SUPPORT_0_7;
1951 			break;
1952 		case 8:
1953 			mcs = IEEE80211_VHT_MCS_SUPPORT_0_8;
1954 			break;
1955 		case 9:
1956 			mcs = IEEE80211_VHT_MCS_SUPPORT_0_9;
1957 			break;
1958 		default:
1959 			WARN_ON(1);
1960 			fallthrough;
1961 		case -1:
1962 			mcs = IEEE80211_VHT_MCS_NOT_SUPPORTED;
1963 			break;
1964 		}
1965 
1966 		tx_mcs_set &= ~(0x3 << (nss * 2));
1967 		tx_mcs_set |= mcs << (nss * 2);
1968 	}
1969 
1970 	return tx_mcs_set;
1971 }
1972 
1973 static u8 ath11k_get_nss_160mhz(struct ath11k *ar,
1974 				u8 max_nss)
1975 {
1976 	u8 nss_ratio_info = ar->pdev->cap.nss_ratio_info;
1977 	u8 max_sup_nss = 0;
1978 
1979 	switch (nss_ratio_info) {
1980 	case WMI_NSS_RATIO_1BY2_NSS:
1981 		max_sup_nss = max_nss >> 1;
1982 		break;
1983 	case WMI_NSS_RATIO_3BY4_NSS:
1984 		ath11k_warn(ar->ab, "WMI_NSS_RATIO_3BY4_NSS not supported\n");
1985 		break;
1986 	case WMI_NSS_RATIO_1_NSS:
1987 		max_sup_nss = max_nss;
1988 		break;
1989 	case WMI_NSS_RATIO_2_NSS:
1990 		ath11k_warn(ar->ab, "WMI_NSS_RATIO_2_NSS not supported\n");
1991 		break;
1992 	default:
1993 		ath11k_warn(ar->ab, "invalid nss ratio received from firmware: %d\n",
1994 			    nss_ratio_info);
1995 		break;
1996 	}
1997 
1998 	return max_sup_nss;
1999 }
2000 
2001 static void ath11k_peer_assoc_h_vht(struct ath11k *ar,
2002 				    struct ieee80211_vif *vif,
2003 				    struct ieee80211_sta *sta,
2004 				    struct peer_assoc_params *arg)
2005 {
2006 	const struct ieee80211_sta_vht_cap *vht_cap = &sta->deflink.vht_cap;
2007 	struct ath11k_vif *arvif = (void *)vif->drv_priv;
2008 	struct cfg80211_chan_def def;
2009 	enum nl80211_band band;
2010 	u16 *vht_mcs_mask;
2011 	u8 ampdu_factor;
2012 	u8 max_nss, vht_mcs;
2013 	int i, vht_nss, nss_idx;
2014 	bool user_rate_valid = true;
2015 	u32 rx_nss, tx_nss, nss_160;
2016 
2017 	if (WARN_ON(ath11k_mac_vif_chan(vif, &def)))
2018 		return;
2019 
2020 	if (!vht_cap->vht_supported)
2021 		return;
2022 
2023 	band = def.chan->band;
2024 	vht_mcs_mask = arvif->bitrate_mask.control[band].vht_mcs;
2025 
2026 	if (ath11k_peer_assoc_h_vht_masked(vht_mcs_mask))
2027 		return;
2028 
2029 	arg->vht_flag = true;
2030 
2031 	/* TODO: similar flags required? */
2032 	arg->vht_capable = true;
2033 
2034 	if (def.chan->band == NL80211_BAND_2GHZ)
2035 		arg->vht_ng_flag = true;
2036 
2037 	arg->peer_vht_caps = vht_cap->cap;
2038 
2039 	ampdu_factor = (vht_cap->cap &
2040 			IEEE80211_VHT_CAP_MAX_A_MPDU_LENGTH_EXPONENT_MASK) >>
2041 		       IEEE80211_VHT_CAP_MAX_A_MPDU_LENGTH_EXPONENT_SHIFT;
2042 
2043 	/* Workaround: Some Netgear/Linksys 11ac APs set Rx A-MPDU factor to
2044 	 * zero in VHT IE. Using it would result in degraded throughput.
2045 	 * arg->peer_max_mpdu at this point contains HT max_mpdu so keep
2046 	 * it if VHT max_mpdu is smaller.
2047 	 */
2048 	arg->peer_max_mpdu = max(arg->peer_max_mpdu,
2049 				 (1U << (IEEE80211_HT_MAX_AMPDU_FACTOR +
2050 					ampdu_factor)) - 1);
2051 
2052 	if (sta->deflink.bandwidth == IEEE80211_STA_RX_BW_80)
2053 		arg->bw_80 = true;
2054 
2055 	if (sta->deflink.bandwidth == IEEE80211_STA_RX_BW_160)
2056 		arg->bw_160 = true;
2057 
2058 	vht_nss =  ath11k_mac_max_vht_nss(vht_mcs_mask);
2059 
2060 	if (vht_nss > sta->deflink.rx_nss) {
2061 		user_rate_valid = false;
2062 		for (nss_idx = sta->deflink.rx_nss - 1; nss_idx >= 0; nss_idx--) {
2063 			if (vht_mcs_mask[nss_idx]) {
2064 				user_rate_valid = true;
2065 				break;
2066 			}
2067 		}
2068 	}
2069 
2070 	if (!user_rate_valid) {
2071 		ath11k_dbg(ar->ab, ATH11K_DBG_MAC, "mac setting vht range mcs value to peer supported nss %d for peer %pM\n",
2072 			   sta->deflink.rx_nss, sta->addr);
2073 		vht_mcs_mask[sta->deflink.rx_nss - 1] = vht_mcs_mask[vht_nss - 1];
2074 	}
2075 
2076 	/* Calculate peer NSS capability from VHT capabilities if STA
2077 	 * supports VHT.
2078 	 */
2079 	for (i = 0, max_nss = 0; i < NL80211_VHT_NSS_MAX; i++) {
2080 		vht_mcs = __le16_to_cpu(vht_cap->vht_mcs.rx_mcs_map) >>
2081 			  (2 * i) & 3;
2082 
2083 		if (vht_mcs != IEEE80211_VHT_MCS_NOT_SUPPORTED &&
2084 		    vht_mcs_mask[i])
2085 			max_nss = i + 1;
2086 	}
2087 	arg->peer_nss = min(sta->deflink.rx_nss, max_nss);
2088 	arg->rx_max_rate = __le16_to_cpu(vht_cap->vht_mcs.rx_highest);
2089 	arg->rx_mcs_set = __le16_to_cpu(vht_cap->vht_mcs.rx_mcs_map);
2090 	arg->tx_max_rate = __le16_to_cpu(vht_cap->vht_mcs.tx_highest);
2091 	arg->tx_mcs_set = ath11k_peer_assoc_h_vht_limit(
2092 		__le16_to_cpu(vht_cap->vht_mcs.tx_mcs_map), vht_mcs_mask);
2093 
2094 	/* In IPQ8074 platform, VHT mcs rate 10 and 11 is enabled by default.
2095 	 * VHT mcs rate 10 and 11 is not suppoerted in 11ac standard.
2096 	 * so explicitly disable the VHT MCS rate 10 and 11 in 11ac mode.
2097 	 */
2098 	arg->tx_mcs_set &= ~IEEE80211_VHT_MCS_SUPPORT_0_11_MASK;
2099 	arg->tx_mcs_set |= IEEE80211_DISABLE_VHT_MCS_SUPPORT_0_11;
2100 
2101 	if ((arg->tx_mcs_set & IEEE80211_VHT_MCS_NOT_SUPPORTED) ==
2102 			IEEE80211_VHT_MCS_NOT_SUPPORTED)
2103 		arg->peer_vht_caps &= ~IEEE80211_VHT_CAP_MU_BEAMFORMEE_CAPABLE;
2104 
2105 	/* TODO:  Check */
2106 	arg->tx_max_mcs_nss = 0xFF;
2107 
2108 	if (arg->peer_phymode == MODE_11AC_VHT160 ||
2109 	    arg->peer_phymode == MODE_11AC_VHT80_80) {
2110 		tx_nss = ath11k_get_nss_160mhz(ar, max_nss);
2111 		rx_nss = min(arg->peer_nss, tx_nss);
2112 		arg->peer_bw_rxnss_override = ATH11K_BW_NSS_MAP_ENABLE;
2113 
2114 		if (!rx_nss) {
2115 			ath11k_warn(ar->ab, "invalid max_nss\n");
2116 			return;
2117 		}
2118 
2119 		if (arg->peer_phymode == MODE_11AC_VHT160)
2120 			nss_160 = FIELD_PREP(ATH11K_PEER_RX_NSS_160MHZ, rx_nss - 1);
2121 		else
2122 			nss_160 = FIELD_PREP(ATH11K_PEER_RX_NSS_80_80MHZ, rx_nss - 1);
2123 
2124 		arg->peer_bw_rxnss_override |= nss_160;
2125 	}
2126 
2127 	ath11k_dbg(ar->ab, ATH11K_DBG_MAC,
2128 		   "mac vht peer %pM max_mpdu %d flags 0x%x nss_override 0x%x\n",
2129 		   sta->addr, arg->peer_max_mpdu, arg->peer_flags,
2130 		   arg->peer_bw_rxnss_override);
2131 }
2132 
2133 static int ath11k_mac_get_max_he_mcs_map(u16 mcs_map, int nss)
2134 {
2135 	switch ((mcs_map >> (2 * nss)) & 0x3) {
2136 	case IEEE80211_HE_MCS_SUPPORT_0_7: return BIT(8) - 1;
2137 	case IEEE80211_HE_MCS_SUPPORT_0_9: return BIT(10) - 1;
2138 	case IEEE80211_HE_MCS_SUPPORT_0_11: return BIT(12) - 1;
2139 	}
2140 	return 0;
2141 }
2142 
2143 static u16 ath11k_peer_assoc_h_he_limit(u16 tx_mcs_set,
2144 					const u16 he_mcs_limit[NL80211_HE_NSS_MAX])
2145 {
2146 	int idx_limit;
2147 	int nss;
2148 	u16 mcs_map;
2149 	u16 mcs;
2150 
2151 	for (nss = 0; nss < NL80211_HE_NSS_MAX; nss++) {
2152 		mcs_map = ath11k_mac_get_max_he_mcs_map(tx_mcs_set, nss) &
2153 			he_mcs_limit[nss];
2154 
2155 		if (mcs_map)
2156 			idx_limit = fls(mcs_map) - 1;
2157 		else
2158 			idx_limit = -1;
2159 
2160 		switch (idx_limit) {
2161 		case 0 ... 7:
2162 			mcs = IEEE80211_HE_MCS_SUPPORT_0_7;
2163 			break;
2164 		case 8:
2165 		case 9:
2166 			mcs = IEEE80211_HE_MCS_SUPPORT_0_9;
2167 			break;
2168 		case 10:
2169 		case 11:
2170 			mcs = IEEE80211_HE_MCS_SUPPORT_0_11;
2171 			break;
2172 		default:
2173 			WARN_ON(1);
2174 			fallthrough;
2175 		case -1:
2176 			mcs = IEEE80211_HE_MCS_NOT_SUPPORTED;
2177 			break;
2178 		}
2179 
2180 		tx_mcs_set &= ~(0x3 << (nss * 2));
2181 		tx_mcs_set |= mcs << (nss * 2);
2182 	}
2183 
2184 	return tx_mcs_set;
2185 }
2186 
2187 static bool
2188 ath11k_peer_assoc_h_he_masked(const u16 *he_mcs_mask)
2189 {
2190 	int nss;
2191 
2192 	for (nss = 0; nss < NL80211_HE_NSS_MAX; nss++)
2193 		if (he_mcs_mask[nss])
2194 			return false;
2195 
2196 	return true;
2197 }
2198 
2199 static void ath11k_peer_assoc_h_he(struct ath11k *ar,
2200 				   struct ieee80211_vif *vif,
2201 				   struct ieee80211_sta *sta,
2202 				   struct peer_assoc_params *arg)
2203 {
2204 	struct ath11k_vif *arvif = (void *)vif->drv_priv;
2205 	struct cfg80211_chan_def def;
2206 	const struct ieee80211_sta_he_cap *he_cap = &sta->deflink.he_cap;
2207 	enum nl80211_band band;
2208 	u16 he_mcs_mask[NL80211_HE_NSS_MAX];
2209 	u8 max_nss, he_mcs;
2210 	u16 he_tx_mcs = 0, v = 0;
2211 	int i, he_nss, nss_idx;
2212 	bool user_rate_valid = true;
2213 	u32 rx_nss, tx_nss, nss_160;
2214 	u8 ampdu_factor, rx_mcs_80, rx_mcs_160;
2215 	u16 mcs_160_map, mcs_80_map;
2216 	bool support_160;
2217 
2218 	if (WARN_ON(ath11k_mac_vif_chan(vif, &def)))
2219 		return;
2220 
2221 	if (!he_cap->has_he)
2222 		return;
2223 
2224 	band = def.chan->band;
2225 	memcpy(he_mcs_mask, arvif->bitrate_mask.control[band].he_mcs,
2226 	       sizeof(he_mcs_mask));
2227 
2228 	if (ath11k_peer_assoc_h_he_masked(he_mcs_mask))
2229 		return;
2230 
2231 	arg->he_flag = true;
2232 	support_160 = !!(he_cap->he_cap_elem.phy_cap_info[0] &
2233 		  IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_160MHZ_IN_5G);
2234 
2235 	/* Supported HE-MCS and NSS Set of peer he_cap is intersection with self he_cp */
2236 	mcs_160_map = le16_to_cpu(he_cap->he_mcs_nss_supp.rx_mcs_160);
2237 	mcs_80_map = le16_to_cpu(he_cap->he_mcs_nss_supp.rx_mcs_80);
2238 
2239 	if (support_160) {
2240 		for (i = 7; i >= 0; i--) {
2241 			u8 mcs_160 = (mcs_160_map >> (2 * i)) & 3;
2242 
2243 			if (mcs_160 != IEEE80211_VHT_MCS_NOT_SUPPORTED) {
2244 				rx_mcs_160 = i + 1;
2245 				break;
2246 			}
2247 		}
2248 	}
2249 
2250 	for (i = 7; i >= 0; i--) {
2251 		u8 mcs_80 = (mcs_80_map >> (2 * i)) & 3;
2252 
2253 		if (mcs_80 != IEEE80211_VHT_MCS_NOT_SUPPORTED) {
2254 			rx_mcs_80 = i + 1;
2255 			break;
2256 		}
2257 	}
2258 
2259 	if (support_160)
2260 		max_nss = min(rx_mcs_80, rx_mcs_160);
2261 	else
2262 		max_nss = rx_mcs_80;
2263 
2264 	arg->peer_nss = min(sta->deflink.rx_nss, max_nss);
2265 
2266 	memcpy_and_pad(&arg->peer_he_cap_macinfo,
2267 		       sizeof(arg->peer_he_cap_macinfo),
2268 		       he_cap->he_cap_elem.mac_cap_info,
2269 		       sizeof(he_cap->he_cap_elem.mac_cap_info),
2270 		       0);
2271 	memcpy_and_pad(&arg->peer_he_cap_phyinfo,
2272 		       sizeof(arg->peer_he_cap_phyinfo),
2273 		       he_cap->he_cap_elem.phy_cap_info,
2274 		       sizeof(he_cap->he_cap_elem.phy_cap_info),
2275 		       0);
2276 	arg->peer_he_ops = vif->bss_conf.he_oper.params;
2277 
2278 	/* the top most byte is used to indicate BSS color info */
2279 	arg->peer_he_ops &= 0xffffff;
2280 
2281 	/* As per section 26.6.1 11ax Draft5.0, if the Max AMPDU Exponent Extension
2282 	 * in HE cap is zero, use the arg->peer_max_mpdu as calculated while parsing
2283 	 * VHT caps(if VHT caps is present) or HT caps (if VHT caps is not present).
2284 	 *
2285 	 * For non-zero value of Max AMPDU Extponent Extension in HE MAC caps,
2286 	 * if a HE STA sends VHT cap and HE cap IE in assoc request then, use
2287 	 * MAX_AMPDU_LEN_FACTOR as 20 to calculate max_ampdu length.
2288 	 * If a HE STA that does not send VHT cap, but HE and HT cap in assoc
2289 	 * request, then use MAX_AMPDU_LEN_FACTOR as 16 to calculate max_ampdu
2290 	 * length.
2291 	 */
2292 	ampdu_factor = u8_get_bits(he_cap->he_cap_elem.mac_cap_info[3],
2293 				   IEEE80211_HE_MAC_CAP3_MAX_AMPDU_LEN_EXP_MASK);
2294 
2295 	if (ampdu_factor) {
2296 		if (sta->deflink.vht_cap.vht_supported)
2297 			arg->peer_max_mpdu = (1 << (IEEE80211_HE_VHT_MAX_AMPDU_FACTOR +
2298 						    ampdu_factor)) - 1;
2299 		else if (sta->deflink.ht_cap.ht_supported)
2300 			arg->peer_max_mpdu = (1 << (IEEE80211_HE_HT_MAX_AMPDU_FACTOR +
2301 						    ampdu_factor)) - 1;
2302 	}
2303 
2304 	if (he_cap->he_cap_elem.phy_cap_info[6] &
2305 	    IEEE80211_HE_PHY_CAP6_PPE_THRESHOLD_PRESENT) {
2306 		int bit = 7;
2307 		int nss, ru;
2308 
2309 		arg->peer_ppet.numss_m1 = he_cap->ppe_thres[0] &
2310 					  IEEE80211_PPE_THRES_NSS_MASK;
2311 		arg->peer_ppet.ru_bit_mask =
2312 			(he_cap->ppe_thres[0] &
2313 			 IEEE80211_PPE_THRES_RU_INDEX_BITMASK_MASK) >>
2314 			IEEE80211_PPE_THRES_RU_INDEX_BITMASK_POS;
2315 
2316 		for (nss = 0; nss <= arg->peer_ppet.numss_m1; nss++) {
2317 			for (ru = 0; ru < 4; ru++) {
2318 				u32 val = 0;
2319 				int i;
2320 
2321 				if ((arg->peer_ppet.ru_bit_mask & BIT(ru)) == 0)
2322 					continue;
2323 				for (i = 0; i < 6; i++) {
2324 					val >>= 1;
2325 					val |= ((he_cap->ppe_thres[bit / 8] >>
2326 						 (bit % 8)) & 0x1) << 5;
2327 					bit++;
2328 				}
2329 				arg->peer_ppet.ppet16_ppet8_ru3_ru0[nss] |=
2330 								val << (ru * 6);
2331 			}
2332 		}
2333 	}
2334 
2335 	if (he_cap->he_cap_elem.mac_cap_info[0] & IEEE80211_HE_MAC_CAP0_TWT_RES)
2336 		arg->twt_responder = true;
2337 	if (he_cap->he_cap_elem.mac_cap_info[0] & IEEE80211_HE_MAC_CAP0_TWT_REQ)
2338 		arg->twt_requester = true;
2339 
2340 	he_nss =  ath11k_mac_max_he_nss(he_mcs_mask);
2341 
2342 	if (he_nss > sta->deflink.rx_nss) {
2343 		user_rate_valid = false;
2344 		for (nss_idx = sta->deflink.rx_nss - 1; nss_idx >= 0; nss_idx--) {
2345 			if (he_mcs_mask[nss_idx]) {
2346 				user_rate_valid = true;
2347 				break;
2348 			}
2349 		}
2350 	}
2351 
2352 	if (!user_rate_valid) {
2353 		ath11k_dbg(ar->ab, ATH11K_DBG_MAC, "mac setting he range mcs value to peer supported nss %d for peer %pM\n",
2354 			   sta->deflink.rx_nss, sta->addr);
2355 		he_mcs_mask[sta->deflink.rx_nss - 1] = he_mcs_mask[he_nss - 1];
2356 	}
2357 
2358 	switch (sta->deflink.bandwidth) {
2359 	case IEEE80211_STA_RX_BW_160:
2360 		if (he_cap->he_cap_elem.phy_cap_info[0] &
2361 		    IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_80PLUS80_MHZ_IN_5G) {
2362 			v = le16_to_cpu(he_cap->he_mcs_nss_supp.rx_mcs_80p80);
2363 			v = ath11k_peer_assoc_h_he_limit(v, he_mcs_mask);
2364 			arg->peer_he_rx_mcs_set[WMI_HECAP_TXRX_MCS_NSS_IDX_80_80] = v;
2365 
2366 			v = le16_to_cpu(he_cap->he_mcs_nss_supp.tx_mcs_80p80);
2367 			arg->peer_he_tx_mcs_set[WMI_HECAP_TXRX_MCS_NSS_IDX_80_80] = v;
2368 
2369 			arg->peer_he_mcs_count++;
2370 			he_tx_mcs = v;
2371 		}
2372 		v = le16_to_cpu(he_cap->he_mcs_nss_supp.rx_mcs_160);
2373 		arg->peer_he_rx_mcs_set[WMI_HECAP_TXRX_MCS_NSS_IDX_160] = v;
2374 
2375 		v = le16_to_cpu(he_cap->he_mcs_nss_supp.tx_mcs_160);
2376 		v = ath11k_peer_assoc_h_he_limit(v, he_mcs_mask);
2377 		arg->peer_he_tx_mcs_set[WMI_HECAP_TXRX_MCS_NSS_IDX_160] = v;
2378 
2379 		arg->peer_he_mcs_count++;
2380 		if (!he_tx_mcs)
2381 			he_tx_mcs = v;
2382 		fallthrough;
2383 
2384 	default:
2385 		v = le16_to_cpu(he_cap->he_mcs_nss_supp.rx_mcs_80);
2386 		arg->peer_he_rx_mcs_set[WMI_HECAP_TXRX_MCS_NSS_IDX_80] = v;
2387 
2388 		v = le16_to_cpu(he_cap->he_mcs_nss_supp.tx_mcs_80);
2389 		v = ath11k_peer_assoc_h_he_limit(v, he_mcs_mask);
2390 		arg->peer_he_tx_mcs_set[WMI_HECAP_TXRX_MCS_NSS_IDX_80] = v;
2391 
2392 		arg->peer_he_mcs_count++;
2393 		if (!he_tx_mcs)
2394 			he_tx_mcs = v;
2395 		break;
2396 	}
2397 
2398 	/* Calculate peer NSS capability from HE capabilities if STA
2399 	 * supports HE.
2400 	 */
2401 	for (i = 0, max_nss = 0; i < NL80211_HE_NSS_MAX; i++) {
2402 		he_mcs = he_tx_mcs >> (2 * i) & 3;
2403 
2404 		/* In case of fixed rates, MCS Range in he_tx_mcs might have
2405 		 * unsupported range, with he_mcs_mask set, so check either of them
2406 		 * to find nss.
2407 		 */
2408 		if (he_mcs != IEEE80211_HE_MCS_NOT_SUPPORTED ||
2409 		    he_mcs_mask[i])
2410 			max_nss = i + 1;
2411 	}
2412 	arg->peer_nss = min(sta->deflink.rx_nss, max_nss);
2413 
2414 	if (arg->peer_phymode == MODE_11AX_HE160 ||
2415 	    arg->peer_phymode == MODE_11AX_HE80_80) {
2416 		tx_nss = ath11k_get_nss_160mhz(ar, max_nss);
2417 		rx_nss = min(arg->peer_nss, tx_nss);
2418 		arg->peer_bw_rxnss_override = ATH11K_BW_NSS_MAP_ENABLE;
2419 
2420 		if (!rx_nss) {
2421 			ath11k_warn(ar->ab, "invalid max_nss\n");
2422 			return;
2423 		}
2424 
2425 		if (arg->peer_phymode == MODE_11AX_HE160)
2426 			nss_160 = FIELD_PREP(ATH11K_PEER_RX_NSS_160MHZ, rx_nss - 1);
2427 		else
2428 			nss_160 = FIELD_PREP(ATH11K_PEER_RX_NSS_80_80MHZ, rx_nss - 1);
2429 
2430 		arg->peer_bw_rxnss_override |= nss_160;
2431 	}
2432 
2433 	ath11k_dbg(ar->ab, ATH11K_DBG_MAC,
2434 		   "mac he peer %pM nss %d mcs cnt %d nss_override 0x%x\n",
2435 		   sta->addr, arg->peer_nss,
2436 		   arg->peer_he_mcs_count,
2437 		   arg->peer_bw_rxnss_override);
2438 }
2439 
2440 static void ath11k_peer_assoc_h_he_6ghz(struct ath11k *ar,
2441 					struct ieee80211_vif *vif,
2442 					struct ieee80211_sta *sta,
2443 					struct peer_assoc_params *arg)
2444 {
2445 	const struct ieee80211_sta_he_cap *he_cap = &sta->deflink.he_cap;
2446 	struct cfg80211_chan_def def;
2447 	enum nl80211_band band;
2448 	u8  ampdu_factor;
2449 
2450 	if (WARN_ON(ath11k_mac_vif_chan(vif, &def)))
2451 		return;
2452 
2453 	band = def.chan->band;
2454 
2455 	if (!arg->he_flag || band != NL80211_BAND_6GHZ || !sta->deflink.he_6ghz_capa.capa)
2456 		return;
2457 
2458 	if (sta->deflink.bandwidth == IEEE80211_STA_RX_BW_40)
2459 		arg->bw_40 = true;
2460 
2461 	if (sta->deflink.bandwidth == IEEE80211_STA_RX_BW_80)
2462 		arg->bw_80 = true;
2463 
2464 	if (sta->deflink.bandwidth == IEEE80211_STA_RX_BW_160)
2465 		arg->bw_160 = true;
2466 
2467 	arg->peer_he_caps_6ghz = le16_to_cpu(sta->deflink.he_6ghz_capa.capa);
2468 	arg->peer_mpdu_density =
2469 		ath11k_parse_mpdudensity(FIELD_GET(IEEE80211_HE_6GHZ_CAP_MIN_MPDU_START,
2470 						   arg->peer_he_caps_6ghz));
2471 
2472 	/* From IEEE Std 802.11ax-2021 - Section 10.12.2: An HE STA shall be capable of
2473 	 * receiving A-MPDU where the A-MPDU pre-EOF padding length is up to the value
2474 	 * indicated by the Maximum A-MPDU Length Exponent Extension field in the HE
2475 	 * Capabilities element and the Maximum A-MPDU Length Exponent field in HE 6 GHz
2476 	 * Band Capabilities element in the 6 GHz band.
2477 	 *
2478 	 * Here, we are extracting the Max A-MPDU Exponent Extension from HE caps and
2479 	 * factor is the Maximum A-MPDU Length Exponent from HE 6 GHZ Band capability.
2480 	 */
2481 	ampdu_factor = FIELD_GET(IEEE80211_HE_MAC_CAP3_MAX_AMPDU_LEN_EXP_MASK,
2482 				 he_cap->he_cap_elem.mac_cap_info[3]) +
2483 			FIELD_GET(IEEE80211_HE_6GHZ_CAP_MAX_AMPDU_LEN_EXP,
2484 				  arg->peer_he_caps_6ghz);
2485 
2486 	arg->peer_max_mpdu = (1u << (IEEE80211_HE_6GHZ_MAX_AMPDU_FACTOR +
2487 				     ampdu_factor)) - 1;
2488 }
2489 
2490 static void ath11k_peer_assoc_h_smps(struct ieee80211_sta *sta,
2491 				     struct peer_assoc_params *arg)
2492 {
2493 	const struct ieee80211_sta_ht_cap *ht_cap = &sta->deflink.ht_cap;
2494 	int smps;
2495 
2496 	if (!ht_cap->ht_supported && !sta->deflink.he_6ghz_capa.capa)
2497 		return;
2498 
2499 	if (ht_cap->ht_supported) {
2500 		smps = ht_cap->cap & IEEE80211_HT_CAP_SM_PS;
2501 		smps >>= IEEE80211_HT_CAP_SM_PS_SHIFT;
2502 	} else {
2503 		smps = le16_get_bits(sta->deflink.he_6ghz_capa.capa,
2504 				     IEEE80211_HE_6GHZ_CAP_SM_PS);
2505 	}
2506 
2507 	switch (smps) {
2508 	case WLAN_HT_CAP_SM_PS_STATIC:
2509 		arg->static_mimops_flag = true;
2510 		break;
2511 	case WLAN_HT_CAP_SM_PS_DYNAMIC:
2512 		arg->dynamic_mimops_flag = true;
2513 		break;
2514 	case WLAN_HT_CAP_SM_PS_DISABLED:
2515 		arg->spatial_mux_flag = true;
2516 		break;
2517 	default:
2518 		break;
2519 	}
2520 }
2521 
2522 static void ath11k_peer_assoc_h_qos(struct ath11k *ar,
2523 				    struct ieee80211_vif *vif,
2524 				    struct ieee80211_sta *sta,
2525 				    struct peer_assoc_params *arg)
2526 {
2527 	struct ath11k_vif *arvif = (void *)vif->drv_priv;
2528 
2529 	switch (arvif->vdev_type) {
2530 	case WMI_VDEV_TYPE_AP:
2531 		if (sta->wme) {
2532 			/* TODO: Check WME vs QoS */
2533 			arg->is_wme_set = true;
2534 			arg->qos_flag = true;
2535 		}
2536 
2537 		if (sta->wme && sta->uapsd_queues) {
2538 			/* TODO: Check WME vs QoS */
2539 			arg->is_wme_set = true;
2540 			arg->apsd_flag = true;
2541 			arg->peer_rate_caps |= WMI_HOST_RC_UAPSD_FLAG;
2542 		}
2543 		break;
2544 	case WMI_VDEV_TYPE_STA:
2545 		if (sta->wme) {
2546 			arg->is_wme_set = true;
2547 			arg->qos_flag = true;
2548 		}
2549 		break;
2550 	default:
2551 		break;
2552 	}
2553 
2554 	ath11k_dbg(ar->ab, ATH11K_DBG_MAC, "mac peer %pM qos %d\n",
2555 		   sta->addr, arg->qos_flag);
2556 }
2557 
2558 static int ath11k_peer_assoc_qos_ap(struct ath11k *ar,
2559 				    struct ath11k_vif *arvif,
2560 				    struct ieee80211_sta *sta)
2561 {
2562 	struct ap_ps_params params;
2563 	u32 max_sp;
2564 	u32 uapsd;
2565 	int ret;
2566 
2567 	lockdep_assert_held(&ar->conf_mutex);
2568 
2569 	params.vdev_id = arvif->vdev_id;
2570 
2571 	ath11k_dbg(ar->ab, ATH11K_DBG_MAC, "mac uapsd_queues 0x%x max_sp %d\n",
2572 		   sta->uapsd_queues, sta->max_sp);
2573 
2574 	uapsd = 0;
2575 	if (sta->uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_VO)
2576 		uapsd |= WMI_AP_PS_UAPSD_AC3_DELIVERY_EN |
2577 			 WMI_AP_PS_UAPSD_AC3_TRIGGER_EN;
2578 	if (sta->uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_VI)
2579 		uapsd |= WMI_AP_PS_UAPSD_AC2_DELIVERY_EN |
2580 			 WMI_AP_PS_UAPSD_AC2_TRIGGER_EN;
2581 	if (sta->uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_BK)
2582 		uapsd |= WMI_AP_PS_UAPSD_AC1_DELIVERY_EN |
2583 			 WMI_AP_PS_UAPSD_AC1_TRIGGER_EN;
2584 	if (sta->uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_BE)
2585 		uapsd |= WMI_AP_PS_UAPSD_AC0_DELIVERY_EN |
2586 			 WMI_AP_PS_UAPSD_AC0_TRIGGER_EN;
2587 
2588 	max_sp = 0;
2589 	if (sta->max_sp < MAX_WMI_AP_PS_PEER_PARAM_MAX_SP)
2590 		max_sp = sta->max_sp;
2591 
2592 	params.param = WMI_AP_PS_PEER_PARAM_UAPSD;
2593 	params.value = uapsd;
2594 	ret = ath11k_wmi_send_set_ap_ps_param_cmd(ar, sta->addr, &params);
2595 	if (ret)
2596 		goto err;
2597 
2598 	params.param = WMI_AP_PS_PEER_PARAM_MAX_SP;
2599 	params.value = max_sp;
2600 	ret = ath11k_wmi_send_set_ap_ps_param_cmd(ar, sta->addr, &params);
2601 	if (ret)
2602 		goto err;
2603 
2604 	/* TODO revisit during testing */
2605 	params.param = WMI_AP_PS_PEER_PARAM_SIFS_RESP_FRMTYPE;
2606 	params.value = DISABLE_SIFS_RESPONSE_TRIGGER;
2607 	ret = ath11k_wmi_send_set_ap_ps_param_cmd(ar, sta->addr, &params);
2608 	if (ret)
2609 		goto err;
2610 
2611 	params.param = WMI_AP_PS_PEER_PARAM_SIFS_RESP_UAPSD;
2612 	params.value = DISABLE_SIFS_RESPONSE_TRIGGER;
2613 	ret = ath11k_wmi_send_set_ap_ps_param_cmd(ar, sta->addr, &params);
2614 	if (ret)
2615 		goto err;
2616 
2617 	return 0;
2618 
2619 err:
2620 	ath11k_warn(ar->ab, "failed to set ap ps peer param %d for vdev %i: %d\n",
2621 		    params.param, arvif->vdev_id, ret);
2622 	return ret;
2623 }
2624 
2625 static bool ath11k_mac_sta_has_ofdm_only(struct ieee80211_sta *sta)
2626 {
2627 	return sta->deflink.supp_rates[NL80211_BAND_2GHZ] >>
2628 	       ATH11K_MAC_FIRST_OFDM_RATE_IDX;
2629 }
2630 
2631 static enum wmi_phy_mode ath11k_mac_get_phymode_vht(struct ath11k *ar,
2632 						    struct ieee80211_sta *sta)
2633 {
2634 	if (sta->deflink.bandwidth == IEEE80211_STA_RX_BW_160) {
2635 		switch (sta->deflink.vht_cap.cap &
2636 			IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_MASK) {
2637 		case IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_160MHZ:
2638 			return MODE_11AC_VHT160;
2639 		case IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_160_80PLUS80MHZ:
2640 			return MODE_11AC_VHT80_80;
2641 		default:
2642 			/* not sure if this is a valid case? */
2643 			return MODE_11AC_VHT160;
2644 		}
2645 	}
2646 
2647 	if (sta->deflink.bandwidth == IEEE80211_STA_RX_BW_80)
2648 		return MODE_11AC_VHT80;
2649 
2650 	if (sta->deflink.bandwidth == IEEE80211_STA_RX_BW_40)
2651 		return MODE_11AC_VHT40;
2652 
2653 	if (sta->deflink.bandwidth == IEEE80211_STA_RX_BW_20)
2654 		return MODE_11AC_VHT20;
2655 
2656 	return MODE_UNKNOWN;
2657 }
2658 
2659 static enum wmi_phy_mode ath11k_mac_get_phymode_he(struct ath11k *ar,
2660 						   struct ieee80211_sta *sta)
2661 {
2662 	if (sta->deflink.bandwidth == IEEE80211_STA_RX_BW_160) {
2663 		if (sta->deflink.he_cap.he_cap_elem.phy_cap_info[0] &
2664 		     IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_160MHZ_IN_5G)
2665 			return MODE_11AX_HE160;
2666 		else if (sta->deflink.he_cap.he_cap_elem.phy_cap_info[0] &
2667 			 IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_80PLUS80_MHZ_IN_5G)
2668 			return MODE_11AX_HE80_80;
2669 		/* not sure if this is a valid case? */
2670 		return MODE_11AX_HE160;
2671 	}
2672 
2673 	if (sta->deflink.bandwidth == IEEE80211_STA_RX_BW_80)
2674 		return MODE_11AX_HE80;
2675 
2676 	if (sta->deflink.bandwidth == IEEE80211_STA_RX_BW_40)
2677 		return MODE_11AX_HE40;
2678 
2679 	if (sta->deflink.bandwidth == IEEE80211_STA_RX_BW_20)
2680 		return MODE_11AX_HE20;
2681 
2682 	return MODE_UNKNOWN;
2683 }
2684 
2685 static void ath11k_peer_assoc_h_phymode(struct ath11k *ar,
2686 					struct ieee80211_vif *vif,
2687 					struct ieee80211_sta *sta,
2688 					struct peer_assoc_params *arg)
2689 {
2690 	struct ath11k_vif *arvif = (void *)vif->drv_priv;
2691 	struct cfg80211_chan_def def;
2692 	enum nl80211_band band;
2693 	const u8 *ht_mcs_mask;
2694 	const u16 *vht_mcs_mask;
2695 	const u16 *he_mcs_mask;
2696 	enum wmi_phy_mode phymode = MODE_UNKNOWN;
2697 
2698 	if (WARN_ON(ath11k_mac_vif_chan(vif, &def)))
2699 		return;
2700 
2701 	band = def.chan->band;
2702 	ht_mcs_mask = arvif->bitrate_mask.control[band].ht_mcs;
2703 	vht_mcs_mask = arvif->bitrate_mask.control[band].vht_mcs;
2704 	he_mcs_mask = arvif->bitrate_mask.control[band].he_mcs;
2705 
2706 	switch (band) {
2707 	case NL80211_BAND_2GHZ:
2708 		if (sta->deflink.he_cap.has_he &&
2709 		    !ath11k_peer_assoc_h_he_masked(he_mcs_mask)) {
2710 			if (sta->deflink.bandwidth == IEEE80211_STA_RX_BW_80)
2711 				phymode = MODE_11AX_HE80_2G;
2712 			else if (sta->deflink.bandwidth == IEEE80211_STA_RX_BW_40)
2713 				phymode = MODE_11AX_HE40_2G;
2714 			else
2715 				phymode = MODE_11AX_HE20_2G;
2716 		} else if (sta->deflink.vht_cap.vht_supported &&
2717 			   !ath11k_peer_assoc_h_vht_masked(vht_mcs_mask)) {
2718 			if (sta->deflink.bandwidth == IEEE80211_STA_RX_BW_40)
2719 				phymode = MODE_11AC_VHT40;
2720 			else
2721 				phymode = MODE_11AC_VHT20;
2722 		} else if (sta->deflink.ht_cap.ht_supported &&
2723 			   !ath11k_peer_assoc_h_ht_masked(ht_mcs_mask)) {
2724 			if (sta->deflink.bandwidth == IEEE80211_STA_RX_BW_40)
2725 				phymode = MODE_11NG_HT40;
2726 			else
2727 				phymode = MODE_11NG_HT20;
2728 		} else if (ath11k_mac_sta_has_ofdm_only(sta)) {
2729 			phymode = MODE_11G;
2730 		} else {
2731 			phymode = MODE_11B;
2732 		}
2733 		break;
2734 	case NL80211_BAND_5GHZ:
2735 	case NL80211_BAND_6GHZ:
2736 		/* Check HE first */
2737 		if (sta->deflink.he_cap.has_he &&
2738 		    !ath11k_peer_assoc_h_he_masked(he_mcs_mask)) {
2739 			phymode = ath11k_mac_get_phymode_he(ar, sta);
2740 		} else if (sta->deflink.vht_cap.vht_supported &&
2741 			   !ath11k_peer_assoc_h_vht_masked(vht_mcs_mask)) {
2742 			phymode = ath11k_mac_get_phymode_vht(ar, sta);
2743 		} else if (sta->deflink.ht_cap.ht_supported &&
2744 			   !ath11k_peer_assoc_h_ht_masked(ht_mcs_mask)) {
2745 			if (sta->deflink.bandwidth >= IEEE80211_STA_RX_BW_40)
2746 				phymode = MODE_11NA_HT40;
2747 			else
2748 				phymode = MODE_11NA_HT20;
2749 		} else {
2750 			phymode = MODE_11A;
2751 		}
2752 		break;
2753 	default:
2754 		break;
2755 	}
2756 
2757 	ath11k_dbg(ar->ab, ATH11K_DBG_MAC, "mac peer %pM phymode %s\n",
2758 		   sta->addr, ath11k_wmi_phymode_str(phymode));
2759 
2760 	arg->peer_phymode = phymode;
2761 	WARN_ON(phymode == MODE_UNKNOWN);
2762 }
2763 
2764 static void ath11k_peer_assoc_prepare(struct ath11k *ar,
2765 				      struct ieee80211_vif *vif,
2766 				      struct ieee80211_sta *sta,
2767 				      struct peer_assoc_params *arg,
2768 				      bool reassoc)
2769 {
2770 	struct ath11k_sta *arsta;
2771 
2772 	lockdep_assert_held(&ar->conf_mutex);
2773 
2774 	arsta = (struct ath11k_sta *)sta->drv_priv;
2775 
2776 	memset(arg, 0, sizeof(*arg));
2777 
2778 	reinit_completion(&ar->peer_assoc_done);
2779 
2780 	arg->peer_new_assoc = !reassoc;
2781 	ath11k_peer_assoc_h_basic(ar, vif, sta, arg);
2782 	ath11k_peer_assoc_h_crypto(ar, vif, sta, arg);
2783 	ath11k_peer_assoc_h_rates(ar, vif, sta, arg);
2784 	ath11k_peer_assoc_h_phymode(ar, vif, sta, arg);
2785 	ath11k_peer_assoc_h_ht(ar, vif, sta, arg);
2786 	ath11k_peer_assoc_h_vht(ar, vif, sta, arg);
2787 	ath11k_peer_assoc_h_he(ar, vif, sta, arg);
2788 	ath11k_peer_assoc_h_he_6ghz(ar, vif, sta, arg);
2789 	ath11k_peer_assoc_h_qos(ar, vif, sta, arg);
2790 	ath11k_peer_assoc_h_smps(sta, arg);
2791 
2792 	arsta->peer_nss = arg->peer_nss;
2793 
2794 	/* TODO: amsdu_disable req? */
2795 }
2796 
2797 static int ath11k_setup_peer_smps(struct ath11k *ar, struct ath11k_vif *arvif,
2798 				  const u8 *addr,
2799 				  const struct ieee80211_sta_ht_cap *ht_cap,
2800 				  u16 he_6ghz_capa)
2801 {
2802 	int smps;
2803 
2804 	if (!ht_cap->ht_supported && !he_6ghz_capa)
2805 		return 0;
2806 
2807 	if (ht_cap->ht_supported) {
2808 		smps = ht_cap->cap & IEEE80211_HT_CAP_SM_PS;
2809 		smps >>= IEEE80211_HT_CAP_SM_PS_SHIFT;
2810 	} else {
2811 		smps = FIELD_GET(IEEE80211_HE_6GHZ_CAP_SM_PS, he_6ghz_capa);
2812 	}
2813 
2814 	if (smps >= ARRAY_SIZE(ath11k_smps_map))
2815 		return -EINVAL;
2816 
2817 	return ath11k_wmi_set_peer_param(ar, addr, arvif->vdev_id,
2818 					 WMI_PEER_MIMO_PS_STATE,
2819 					 ath11k_smps_map[smps]);
2820 }
2821 
2822 static bool ath11k_mac_set_he_txbf_conf(struct ath11k_vif *arvif)
2823 {
2824 	struct ath11k *ar = arvif->ar;
2825 	u32 param, value;
2826 	int ret;
2827 
2828 	if (!arvif->vif->bss_conf.he_support)
2829 		return true;
2830 
2831 	param = WMI_VDEV_PARAM_SET_HEMU_MODE;
2832 	value = 0;
2833 	if (arvif->vif->bss_conf.he_su_beamformer) {
2834 		value |= FIELD_PREP(HE_MODE_SU_TX_BFER, HE_SU_BFER_ENABLE);
2835 		if (arvif->vif->bss_conf.he_mu_beamformer &&
2836 		    arvif->vdev_type == WMI_VDEV_TYPE_AP)
2837 			value |= FIELD_PREP(HE_MODE_MU_TX_BFER, HE_MU_BFER_ENABLE);
2838 	}
2839 
2840 	if (arvif->vif->type != NL80211_IFTYPE_MESH_POINT) {
2841 		value |= FIELD_PREP(HE_MODE_DL_OFDMA, HE_DL_MUOFDMA_ENABLE) |
2842 			 FIELD_PREP(HE_MODE_UL_OFDMA, HE_UL_MUOFDMA_ENABLE);
2843 
2844 		if (arvif->vif->bss_conf.he_full_ul_mumimo)
2845 			value |= FIELD_PREP(HE_MODE_UL_MUMIMO, HE_UL_MUMIMO_ENABLE);
2846 
2847 		if (arvif->vif->bss_conf.he_su_beamformee)
2848 			value |= FIELD_PREP(HE_MODE_SU_TX_BFEE, HE_SU_BFEE_ENABLE);
2849 	}
2850 
2851 	ret = ath11k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id, param, value);
2852 	if (ret) {
2853 		ath11k_warn(ar->ab, "failed to set vdev %d HE MU mode: %d\n",
2854 			    arvif->vdev_id, ret);
2855 		return false;
2856 	}
2857 
2858 	param = WMI_VDEV_PARAM_SET_HE_SOUNDING_MODE;
2859 	value =	FIELD_PREP(HE_VHT_SOUNDING_MODE, HE_VHT_SOUNDING_MODE_ENABLE) |
2860 		FIELD_PREP(HE_TRIG_NONTRIG_SOUNDING_MODE,
2861 			   HE_TRIG_NONTRIG_SOUNDING_MODE_ENABLE);
2862 	ret = ath11k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
2863 					    param, value);
2864 	if (ret) {
2865 		ath11k_warn(ar->ab, "failed to set vdev %d sounding mode: %d\n",
2866 			    arvif->vdev_id, ret);
2867 		return false;
2868 	}
2869 	return true;
2870 }
2871 
2872 static bool ath11k_mac_vif_recalc_sta_he_txbf(struct ath11k *ar,
2873 					      struct ieee80211_vif *vif,
2874 					      struct ieee80211_sta_he_cap *he_cap)
2875 {
2876 	struct ath11k_vif *arvif = (void *)vif->drv_priv;
2877 	struct ieee80211_he_cap_elem he_cap_elem = {0};
2878 	struct ieee80211_sta_he_cap *cap_band = NULL;
2879 	struct cfg80211_chan_def def;
2880 	u32 param = WMI_VDEV_PARAM_SET_HEMU_MODE;
2881 	u32 hemode = 0;
2882 	int ret;
2883 
2884 	if (!vif->bss_conf.he_support)
2885 		return true;
2886 
2887 	if (vif->type != NL80211_IFTYPE_STATION)
2888 		return false;
2889 
2890 	if (WARN_ON(ath11k_mac_vif_chan(vif, &def)))
2891 		return false;
2892 
2893 	if (def.chan->band == NL80211_BAND_2GHZ)
2894 		cap_band = &ar->mac.iftype[NL80211_BAND_2GHZ][vif->type].he_cap;
2895 	else
2896 		cap_band = &ar->mac.iftype[NL80211_BAND_5GHZ][vif->type].he_cap;
2897 
2898 	memcpy(&he_cap_elem, &cap_band->he_cap_elem, sizeof(he_cap_elem));
2899 
2900 	if (HECAP_PHY_SUBFME_GET(he_cap_elem.phy_cap_info)) {
2901 		if (HECAP_PHY_SUBFMR_GET(he_cap->he_cap_elem.phy_cap_info))
2902 			hemode |= FIELD_PREP(HE_MODE_SU_TX_BFEE, HE_SU_BFEE_ENABLE);
2903 		if (HECAP_PHY_MUBFMR_GET(he_cap->he_cap_elem.phy_cap_info))
2904 			hemode |= FIELD_PREP(HE_MODE_MU_TX_BFEE, HE_MU_BFEE_ENABLE);
2905 	}
2906 
2907 	if (vif->type != NL80211_IFTYPE_MESH_POINT) {
2908 		hemode |= FIELD_PREP(HE_MODE_DL_OFDMA, HE_DL_MUOFDMA_ENABLE) |
2909 			  FIELD_PREP(HE_MODE_UL_OFDMA, HE_UL_MUOFDMA_ENABLE);
2910 
2911 		if (HECAP_PHY_ULMUMIMO_GET(he_cap_elem.phy_cap_info))
2912 			if (HECAP_PHY_ULMUMIMO_GET(he_cap->he_cap_elem.phy_cap_info))
2913 				hemode |= FIELD_PREP(HE_MODE_UL_MUMIMO,
2914 						     HE_UL_MUMIMO_ENABLE);
2915 
2916 		if (FIELD_GET(HE_MODE_MU_TX_BFEE, hemode))
2917 			hemode |= FIELD_PREP(HE_MODE_SU_TX_BFEE, HE_SU_BFEE_ENABLE);
2918 
2919 		if (FIELD_GET(HE_MODE_MU_TX_BFER, hemode))
2920 			hemode |= FIELD_PREP(HE_MODE_SU_TX_BFER, HE_SU_BFER_ENABLE);
2921 	}
2922 
2923 	ret = ath11k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id, param, hemode);
2924 	if (ret) {
2925 		ath11k_warn(ar->ab, "failed to submit vdev param txbf 0x%x: %d\n",
2926 			    hemode, ret);
2927 		return false;
2928 	}
2929 
2930 	return true;
2931 }
2932 
2933 static void ath11k_bss_assoc(struct ieee80211_hw *hw,
2934 			     struct ieee80211_vif *vif,
2935 			     struct ieee80211_bss_conf *bss_conf)
2936 {
2937 	struct ath11k *ar = hw->priv;
2938 	struct ath11k_vif *arvif = (void *)vif->drv_priv;
2939 	struct peer_assoc_params peer_arg;
2940 	struct ieee80211_sta *ap_sta;
2941 	struct ath11k_peer *peer;
2942 	bool is_auth = false;
2943 	struct ieee80211_sta_he_cap  he_cap;
2944 	int ret;
2945 
2946 	lockdep_assert_held(&ar->conf_mutex);
2947 
2948 	ath11k_dbg(ar->ab, ATH11K_DBG_MAC, "mac vdev %i assoc bssid %pM aid %d\n",
2949 		   arvif->vdev_id, arvif->bssid, arvif->aid);
2950 
2951 	rcu_read_lock();
2952 
2953 	ap_sta = ieee80211_find_sta(vif, bss_conf->bssid);
2954 	if (!ap_sta) {
2955 		ath11k_warn(ar->ab, "failed to find station entry for bss %pM vdev %i\n",
2956 			    bss_conf->bssid, arvif->vdev_id);
2957 		rcu_read_unlock();
2958 		return;
2959 	}
2960 
2961 	/* he_cap here is updated at assoc success for sta mode only */
2962 	he_cap  = ap_sta->deflink.he_cap;
2963 
2964 	ath11k_peer_assoc_prepare(ar, vif, ap_sta, &peer_arg, false);
2965 
2966 	rcu_read_unlock();
2967 
2968 	peer_arg.is_assoc = true;
2969 	ret = ath11k_wmi_send_peer_assoc_cmd(ar, &peer_arg);
2970 	if (ret) {
2971 		ath11k_warn(ar->ab, "failed to run peer assoc for %pM vdev %i: %d\n",
2972 			    bss_conf->bssid, arvif->vdev_id, ret);
2973 		return;
2974 	}
2975 
2976 	if (!wait_for_completion_timeout(&ar->peer_assoc_done, 1 * HZ)) {
2977 		ath11k_warn(ar->ab, "failed to get peer assoc conf event for %pM vdev %i\n",
2978 			    bss_conf->bssid, arvif->vdev_id);
2979 		return;
2980 	}
2981 
2982 	ret = ath11k_setup_peer_smps(ar, arvif, bss_conf->bssid,
2983 				     &ap_sta->deflink.ht_cap,
2984 				     le16_to_cpu(ap_sta->deflink.he_6ghz_capa.capa));
2985 	if (ret) {
2986 		ath11k_warn(ar->ab, "failed to setup peer SMPS for vdev %d: %d\n",
2987 			    arvif->vdev_id, ret);
2988 		return;
2989 	}
2990 
2991 	if (!ath11k_mac_vif_recalc_sta_he_txbf(ar, vif, &he_cap)) {
2992 		ath11k_warn(ar->ab, "failed to recalc he txbf for vdev %i on bss %pM\n",
2993 			    arvif->vdev_id, bss_conf->bssid);
2994 		return;
2995 	}
2996 
2997 	WARN_ON(arvif->is_up);
2998 
2999 	arvif->aid = vif->cfg.aid;
3000 	ether_addr_copy(arvif->bssid, bss_conf->bssid);
3001 
3002 	ret = ath11k_wmi_vdev_up(ar, arvif->vdev_id, arvif->aid, arvif->bssid,
3003 				 NULL, 0, 0);
3004 	if (ret) {
3005 		ath11k_warn(ar->ab, "failed to set vdev %d up: %d\n",
3006 			    arvif->vdev_id, ret);
3007 		return;
3008 	}
3009 
3010 	arvif->is_up = true;
3011 	arvif->rekey_data.enable_offload = false;
3012 
3013 	ath11k_dbg(ar->ab, ATH11K_DBG_MAC,
3014 		   "mac vdev %d up (associated) bssid %pM aid %d\n",
3015 		   arvif->vdev_id, bss_conf->bssid, vif->cfg.aid);
3016 
3017 	spin_lock_bh(&ar->ab->base_lock);
3018 
3019 	peer = ath11k_peer_find(ar->ab, arvif->vdev_id, arvif->bssid);
3020 	if (peer && peer->is_authorized)
3021 		is_auth = true;
3022 
3023 	spin_unlock_bh(&ar->ab->base_lock);
3024 
3025 	if (is_auth) {
3026 		ret = ath11k_wmi_set_peer_param(ar, arvif->bssid,
3027 						arvif->vdev_id,
3028 						WMI_PEER_AUTHORIZE,
3029 						1);
3030 		if (ret)
3031 			ath11k_warn(ar->ab, "Unable to authorize BSS peer: %d\n", ret);
3032 	}
3033 
3034 	ret = ath11k_wmi_send_obss_spr_cmd(ar, arvif->vdev_id,
3035 					   &bss_conf->he_obss_pd);
3036 	if (ret)
3037 		ath11k_warn(ar->ab, "failed to set vdev %i OBSS PD parameters: %d\n",
3038 			    arvif->vdev_id, ret);
3039 
3040 	ret = ath11k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
3041 					    WMI_VDEV_PARAM_DTIM_POLICY,
3042 					    WMI_DTIM_POLICY_STICK);
3043 	if (ret)
3044 		ath11k_warn(ar->ab, "failed to set vdev %d dtim policy: %d\n",
3045 			    arvif->vdev_id, ret);
3046 
3047 	ath11k_mac_11d_scan_stop_all(ar->ab);
3048 }
3049 
3050 static void ath11k_bss_disassoc(struct ieee80211_hw *hw,
3051 				struct ieee80211_vif *vif)
3052 {
3053 	struct ath11k *ar = hw->priv;
3054 	struct ath11k_vif *arvif = (void *)vif->drv_priv;
3055 	int ret;
3056 
3057 	lockdep_assert_held(&ar->conf_mutex);
3058 
3059 	ath11k_dbg(ar->ab, ATH11K_DBG_MAC, "mac vdev %i disassoc bssid %pM\n",
3060 		   arvif->vdev_id, arvif->bssid);
3061 
3062 	ret = ath11k_wmi_vdev_down(ar, arvif->vdev_id);
3063 	if (ret)
3064 		ath11k_warn(ar->ab, "failed to down vdev %i: %d\n",
3065 			    arvif->vdev_id, ret);
3066 
3067 	arvif->is_up = false;
3068 
3069 	memset(&arvif->rekey_data, 0, sizeof(arvif->rekey_data));
3070 
3071 	cancel_delayed_work_sync(&arvif->connection_loss_work);
3072 }
3073 
3074 static u32 ath11k_mac_get_rate_hw_value(int bitrate)
3075 {
3076 	u32 preamble;
3077 	u16 hw_value;
3078 	int rate;
3079 	size_t i;
3080 
3081 	if (ath11k_mac_bitrate_is_cck(bitrate))
3082 		preamble = WMI_RATE_PREAMBLE_CCK;
3083 	else
3084 		preamble = WMI_RATE_PREAMBLE_OFDM;
3085 
3086 	for (i = 0; i < ARRAY_SIZE(ath11k_legacy_rates); i++) {
3087 		if (ath11k_legacy_rates[i].bitrate != bitrate)
3088 			continue;
3089 
3090 		hw_value = ath11k_legacy_rates[i].hw_value;
3091 		rate = ATH11K_HW_RATE_CODE(hw_value, 0, preamble);
3092 
3093 		return rate;
3094 	}
3095 
3096 	return -EINVAL;
3097 }
3098 
3099 static void ath11k_recalculate_mgmt_rate(struct ath11k *ar,
3100 					 struct ieee80211_vif *vif,
3101 					 struct cfg80211_chan_def *def)
3102 {
3103 	struct ath11k_vif *arvif = (void *)vif->drv_priv;
3104 	const struct ieee80211_supported_band *sband;
3105 	u8 basic_rate_idx;
3106 	int hw_rate_code;
3107 	u32 vdev_param;
3108 	u16 bitrate;
3109 	int ret;
3110 
3111 	lockdep_assert_held(&ar->conf_mutex);
3112 
3113 	sband = ar->hw->wiphy->bands[def->chan->band];
3114 	basic_rate_idx = ffs(vif->bss_conf.basic_rates) - 1;
3115 	bitrate = sband->bitrates[basic_rate_idx].bitrate;
3116 
3117 	hw_rate_code = ath11k_mac_get_rate_hw_value(bitrate);
3118 	if (hw_rate_code < 0) {
3119 		ath11k_warn(ar->ab, "bitrate not supported %d\n", bitrate);
3120 		return;
3121 	}
3122 
3123 	vdev_param = WMI_VDEV_PARAM_MGMT_RATE;
3124 	ret = ath11k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id, vdev_param,
3125 					    hw_rate_code);
3126 	if (ret)
3127 		ath11k_warn(ar->ab, "failed to set mgmt tx rate %d\n", ret);
3128 
3129 	/* For WCN6855, firmware will clear this param when vdev starts, hence
3130 	 * cache it here so that we can reconfigure it once vdev starts.
3131 	 */
3132 	ar->hw_rate_code = hw_rate_code;
3133 
3134 	vdev_param = WMI_VDEV_PARAM_BEACON_RATE;
3135 	ret = ath11k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id, vdev_param,
3136 					    hw_rate_code);
3137 	if (ret)
3138 		ath11k_warn(ar->ab, "failed to set beacon tx rate %d\n", ret);
3139 }
3140 
3141 static int ath11k_mac_fils_discovery(struct ath11k_vif *arvif,
3142 				     struct ieee80211_bss_conf *info)
3143 {
3144 	struct ath11k *ar = arvif->ar;
3145 	struct sk_buff *tmpl;
3146 	int ret;
3147 	u32 interval;
3148 	bool unsol_bcast_probe_resp_enabled = false;
3149 
3150 	if (info->fils_discovery.max_interval) {
3151 		interval = info->fils_discovery.max_interval;
3152 
3153 		tmpl = ieee80211_get_fils_discovery_tmpl(ar->hw, arvif->vif);
3154 		if (tmpl)
3155 			ret = ath11k_wmi_fils_discovery_tmpl(ar, arvif->vdev_id,
3156 							     tmpl);
3157 	} else if (info->unsol_bcast_probe_resp_interval) {
3158 		unsol_bcast_probe_resp_enabled = 1;
3159 		interval = info->unsol_bcast_probe_resp_interval;
3160 
3161 		tmpl = ieee80211_get_unsol_bcast_probe_resp_tmpl(ar->hw,
3162 								 arvif->vif);
3163 		if (tmpl)
3164 			ret = ath11k_wmi_probe_resp_tmpl(ar, arvif->vdev_id,
3165 							 tmpl);
3166 	} else { /* Disable */
3167 		return ath11k_wmi_fils_discovery(ar, arvif->vdev_id, 0, false);
3168 	}
3169 
3170 	if (!tmpl) {
3171 		ath11k_warn(ar->ab,
3172 			    "mac vdev %i failed to retrieve %s template\n",
3173 			    arvif->vdev_id, (unsol_bcast_probe_resp_enabled ?
3174 			    "unsolicited broadcast probe response" :
3175 			    "FILS discovery"));
3176 		return -EPERM;
3177 	}
3178 	kfree_skb(tmpl);
3179 
3180 	if (!ret)
3181 		ret = ath11k_wmi_fils_discovery(ar, arvif->vdev_id, interval,
3182 						unsol_bcast_probe_resp_enabled);
3183 
3184 	return ret;
3185 }
3186 
3187 static int ath11k_mac_config_obss_pd(struct ath11k *ar,
3188 				     struct ieee80211_he_obss_pd *he_obss_pd)
3189 {
3190 	u32 bitmap[2], param_id, param_val, pdev_id;
3191 	int ret;
3192 	s8 non_srg_th = 0, srg_th = 0;
3193 
3194 	pdev_id = ar->pdev->pdev_id;
3195 
3196 	/* Set and enable SRG/non-SRG OBSS PD Threshold */
3197 	param_id = WMI_PDEV_PARAM_SET_CMD_OBSS_PD_THRESHOLD;
3198 	if (test_bit(ATH11K_FLAG_MONITOR_STARTED, &ar->monitor_flags)) {
3199 		ret = ath11k_wmi_pdev_set_param(ar, param_id, 0, pdev_id);
3200 		if (ret)
3201 			ath11k_warn(ar->ab,
3202 				    "failed to set obss_pd_threshold for pdev: %u\n",
3203 				    pdev_id);
3204 		return ret;
3205 	}
3206 
3207 	ath11k_dbg(ar->ab, ATH11K_DBG_MAC,
3208 		   "mac obss pd sr_ctrl %x non_srg_thres %u srg_max %u\n",
3209 		   he_obss_pd->sr_ctrl, he_obss_pd->non_srg_max_offset,
3210 		   he_obss_pd->max_offset);
3211 
3212 	param_val = 0;
3213 
3214 	if (he_obss_pd->sr_ctrl &
3215 	    IEEE80211_HE_SPR_NON_SRG_OBSS_PD_SR_DISALLOWED) {
3216 		non_srg_th = ATH11K_OBSS_PD_MAX_THRESHOLD;
3217 	} else {
3218 		if (he_obss_pd->sr_ctrl & IEEE80211_HE_SPR_NON_SRG_OFFSET_PRESENT)
3219 			non_srg_th = (ATH11K_OBSS_PD_MAX_THRESHOLD +
3220 				      he_obss_pd->non_srg_max_offset);
3221 		else
3222 			non_srg_th = ATH11K_OBSS_PD_NON_SRG_MAX_THRESHOLD;
3223 
3224 		param_val |= ATH11K_OBSS_PD_NON_SRG_EN;
3225 	}
3226 
3227 	if (he_obss_pd->sr_ctrl & IEEE80211_HE_SPR_SRG_INFORMATION_PRESENT) {
3228 		srg_th = ATH11K_OBSS_PD_MAX_THRESHOLD + he_obss_pd->max_offset;
3229 		param_val |= ATH11K_OBSS_PD_SRG_EN;
3230 	}
3231 
3232 	if (test_bit(WMI_TLV_SERVICE_SRG_SRP_SPATIAL_REUSE_SUPPORT,
3233 		     ar->ab->wmi_ab.svc_map)) {
3234 		param_val |= ATH11K_OBSS_PD_THRESHOLD_IN_DBM;
3235 		param_val |= FIELD_PREP(GENMASK(15, 8), srg_th);
3236 	} else {
3237 		non_srg_th -= ATH11K_DEFAULT_NOISE_FLOOR;
3238 		/* SRG not supported and threshold in dB */
3239 		param_val &= ~(ATH11K_OBSS_PD_SRG_EN |
3240 			       ATH11K_OBSS_PD_THRESHOLD_IN_DBM);
3241 	}
3242 
3243 	param_val |= (non_srg_th & GENMASK(7, 0));
3244 	ret = ath11k_wmi_pdev_set_param(ar, param_id, param_val, pdev_id);
3245 	if (ret) {
3246 		ath11k_warn(ar->ab,
3247 			    "failed to set obss_pd_threshold for pdev: %u\n",
3248 			    pdev_id);
3249 		return ret;
3250 	}
3251 
3252 	/* Enable OBSS PD for all access category */
3253 	param_id  = WMI_PDEV_PARAM_SET_CMD_OBSS_PD_PER_AC;
3254 	param_val = 0xf;
3255 	ret = ath11k_wmi_pdev_set_param(ar, param_id, param_val, pdev_id);
3256 	if (ret) {
3257 		ath11k_warn(ar->ab,
3258 			    "failed to set obss_pd_per_ac for pdev: %u\n",
3259 			    pdev_id);
3260 		return ret;
3261 	}
3262 
3263 	/* Set SR Prohibit */
3264 	param_id  = WMI_PDEV_PARAM_ENABLE_SR_PROHIBIT;
3265 	param_val = !!(he_obss_pd->sr_ctrl &
3266 		       IEEE80211_HE_SPR_HESIGA_SR_VAL15_ALLOWED);
3267 	ret = ath11k_wmi_pdev_set_param(ar, param_id, param_val, pdev_id);
3268 	if (ret) {
3269 		ath11k_warn(ar->ab, "failed to set sr_prohibit for pdev: %u\n",
3270 			    pdev_id);
3271 		return ret;
3272 	}
3273 
3274 	if (!test_bit(WMI_TLV_SERVICE_SRG_SRP_SPATIAL_REUSE_SUPPORT,
3275 		      ar->ab->wmi_ab.svc_map))
3276 		return 0;
3277 
3278 	/* Set SRG BSS Color Bitmap */
3279 	memcpy(bitmap, he_obss_pd->bss_color_bitmap, sizeof(bitmap));
3280 	ret = ath11k_wmi_pdev_set_srg_bss_color_bitmap(ar, bitmap);
3281 	if (ret) {
3282 		ath11k_warn(ar->ab,
3283 			    "failed to set bss_color_bitmap for pdev: %u\n",
3284 			    pdev_id);
3285 		return ret;
3286 	}
3287 
3288 	/* Set SRG Partial BSSID Bitmap */
3289 	memcpy(bitmap, he_obss_pd->partial_bssid_bitmap, sizeof(bitmap));
3290 	ret = ath11k_wmi_pdev_set_srg_patial_bssid_bitmap(ar, bitmap);
3291 	if (ret) {
3292 		ath11k_warn(ar->ab,
3293 			    "failed to set partial_bssid_bitmap for pdev: %u\n",
3294 			    pdev_id);
3295 		return ret;
3296 	}
3297 
3298 	memset(bitmap, 0xff, sizeof(bitmap));
3299 
3300 	/* Enable all BSS Colors for SRG */
3301 	ret = ath11k_wmi_pdev_srg_obss_color_enable_bitmap(ar, bitmap);
3302 	if (ret) {
3303 		ath11k_warn(ar->ab,
3304 			    "failed to set srg_color_en_bitmap pdev: %u\n",
3305 			    pdev_id);
3306 		return ret;
3307 	}
3308 
3309 	/* Enable all partial BSSID mask for SRG */
3310 	ret = ath11k_wmi_pdev_srg_obss_bssid_enable_bitmap(ar, bitmap);
3311 	if (ret) {
3312 		ath11k_warn(ar->ab,
3313 			    "failed to set srg_bssid_en_bitmap pdev: %u\n",
3314 			    pdev_id);
3315 		return ret;
3316 	}
3317 
3318 	/* Enable all BSS Colors for non-SRG */
3319 	ret = ath11k_wmi_pdev_non_srg_obss_color_enable_bitmap(ar, bitmap);
3320 	if (ret) {
3321 		ath11k_warn(ar->ab,
3322 			    "failed to set non_srg_color_en_bitmap pdev: %u\n",
3323 			    pdev_id);
3324 		return ret;
3325 	}
3326 
3327 	/* Enable all partial BSSID mask for non-SRG */
3328 	ret = ath11k_wmi_pdev_non_srg_obss_bssid_enable_bitmap(ar, bitmap);
3329 	if (ret) {
3330 		ath11k_warn(ar->ab,
3331 			    "failed to set non_srg_bssid_en_bitmap pdev: %u\n",
3332 			    pdev_id);
3333 		return ret;
3334 	}
3335 
3336 	return 0;
3337 }
3338 
3339 static void ath11k_mac_op_bss_info_changed(struct ieee80211_hw *hw,
3340 					   struct ieee80211_vif *vif,
3341 					   struct ieee80211_bss_conf *info,
3342 					   u64 changed)
3343 {
3344 	struct ath11k *ar = hw->priv;
3345 	struct ath11k_vif *arvif = ath11k_vif_to_arvif(vif);
3346 	struct cfg80211_chan_def def;
3347 	u32 param_id, param_value;
3348 	enum nl80211_band band;
3349 	u32 vdev_param;
3350 	int mcast_rate;
3351 	u32 preamble;
3352 	u16 hw_value;
3353 	u16 bitrate;
3354 	int ret = 0;
3355 	u8 rateidx;
3356 	u32 rate, param;
3357 	u32 ipv4_cnt;
3358 
3359 	mutex_lock(&ar->conf_mutex);
3360 
3361 	if (changed & BSS_CHANGED_BEACON_INT) {
3362 		arvif->beacon_interval = info->beacon_int;
3363 
3364 		param_id = WMI_VDEV_PARAM_BEACON_INTERVAL;
3365 		ret = ath11k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
3366 						    param_id,
3367 						    arvif->beacon_interval);
3368 		if (ret)
3369 			ath11k_warn(ar->ab, "Failed to set beacon interval for VDEV: %d\n",
3370 				    arvif->vdev_id);
3371 		else
3372 			ath11k_dbg(ar->ab, ATH11K_DBG_MAC,
3373 				   "Beacon interval: %d set for VDEV: %d\n",
3374 				   arvif->beacon_interval, arvif->vdev_id);
3375 	}
3376 
3377 	if (changed & BSS_CHANGED_BEACON) {
3378 		param_id = WMI_PDEV_PARAM_BEACON_TX_MODE;
3379 		param_value = WMI_BEACON_STAGGERED_MODE;
3380 		ret = ath11k_wmi_pdev_set_param(ar, param_id,
3381 						param_value, ar->pdev->pdev_id);
3382 		if (ret)
3383 			ath11k_warn(ar->ab, "Failed to set beacon mode for VDEV: %d\n",
3384 				    arvif->vdev_id);
3385 		else
3386 			ath11k_dbg(ar->ab, ATH11K_DBG_MAC,
3387 				   "Set staggered beacon mode for VDEV: %d\n",
3388 				   arvif->vdev_id);
3389 
3390 		if (!arvif->do_not_send_tmpl || !arvif->bcca_zero_sent) {
3391 			ret = ath11k_mac_setup_bcn_tmpl(arvif);
3392 			if (ret)
3393 				ath11k_warn(ar->ab, "failed to update bcn template: %d\n",
3394 					    ret);
3395 		}
3396 
3397 		if (arvif->bcca_zero_sent)
3398 			arvif->do_not_send_tmpl = true;
3399 		else
3400 			arvif->do_not_send_tmpl = false;
3401 
3402 		if (vif->bss_conf.he_support) {
3403 			ret = ath11k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
3404 							    WMI_VDEV_PARAM_BA_MODE,
3405 							    WMI_BA_MODE_BUFFER_SIZE_256);
3406 			if (ret)
3407 				ath11k_warn(ar->ab,
3408 					    "failed to set BA BUFFER SIZE 256 for vdev: %d\n",
3409 					    arvif->vdev_id);
3410 			else
3411 				ath11k_dbg(ar->ab, ATH11K_DBG_MAC,
3412 					   "Set BA BUFFER SIZE 256 for VDEV: %d\n",
3413 					   arvif->vdev_id);
3414 		}
3415 	}
3416 
3417 	if (changed & (BSS_CHANGED_BEACON_INFO | BSS_CHANGED_BEACON)) {
3418 		arvif->dtim_period = info->dtim_period;
3419 
3420 		param_id = WMI_VDEV_PARAM_DTIM_PERIOD;
3421 		ret = ath11k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
3422 						    param_id,
3423 						    arvif->dtim_period);
3424 
3425 		if (ret)
3426 			ath11k_warn(ar->ab, "Failed to set dtim period for VDEV %d: %i\n",
3427 				    arvif->vdev_id, ret);
3428 		else
3429 			ath11k_dbg(ar->ab, ATH11K_DBG_MAC,
3430 				   "DTIM period: %d set for VDEV: %d\n",
3431 				   arvif->dtim_period, arvif->vdev_id);
3432 	}
3433 
3434 	if (changed & BSS_CHANGED_SSID &&
3435 	    vif->type == NL80211_IFTYPE_AP) {
3436 		arvif->u.ap.ssid_len = vif->cfg.ssid_len;
3437 		if (vif->cfg.ssid_len)
3438 			memcpy(arvif->u.ap.ssid, vif->cfg.ssid,
3439 			       vif->cfg.ssid_len);
3440 		arvif->u.ap.hidden_ssid = info->hidden_ssid;
3441 	}
3442 
3443 	if (changed & BSS_CHANGED_BSSID && !is_zero_ether_addr(info->bssid))
3444 		ether_addr_copy(arvif->bssid, info->bssid);
3445 
3446 	if (changed & BSS_CHANGED_BEACON_ENABLED) {
3447 		if (info->enable_beacon)
3448 			ath11k_mac_set_he_txbf_conf(arvif);
3449 		ath11k_control_beaconing(arvif, info);
3450 
3451 		if (arvif->is_up && vif->bss_conf.he_support &&
3452 		    vif->bss_conf.he_oper.params) {
3453 			param_id = WMI_VDEV_PARAM_HEOPS_0_31;
3454 			param_value = vif->bss_conf.he_oper.params;
3455 			ret = ath11k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
3456 							    param_id, param_value);
3457 			ath11k_dbg(ar->ab, ATH11K_DBG_MAC,
3458 				   "he oper param: %x set for VDEV: %d\n",
3459 				   param_value, arvif->vdev_id);
3460 
3461 			if (ret)
3462 				ath11k_warn(ar->ab, "Failed to set he oper params %x for VDEV %d: %i\n",
3463 					    param_value, arvif->vdev_id, ret);
3464 		}
3465 	}
3466 
3467 	if (changed & BSS_CHANGED_ERP_CTS_PROT) {
3468 		u32 cts_prot;
3469 
3470 		cts_prot = !!(info->use_cts_prot);
3471 		param_id = WMI_VDEV_PARAM_PROTECTION_MODE;
3472 
3473 		if (arvif->is_started) {
3474 			ret = ath11k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
3475 							    param_id, cts_prot);
3476 			if (ret)
3477 				ath11k_warn(ar->ab, "Failed to set CTS prot for VDEV: %d\n",
3478 					    arvif->vdev_id);
3479 			else
3480 				ath11k_dbg(ar->ab, ATH11K_DBG_MAC, "Set CTS prot: %d for VDEV: %d\n",
3481 					   cts_prot, arvif->vdev_id);
3482 		} else {
3483 			ath11k_dbg(ar->ab, ATH11K_DBG_MAC, "defer protection mode setup, vdev is not ready yet\n");
3484 		}
3485 	}
3486 
3487 	if (changed & BSS_CHANGED_ERP_SLOT) {
3488 		u32 slottime;
3489 
3490 		if (info->use_short_slot)
3491 			slottime = WMI_VDEV_SLOT_TIME_SHORT; /* 9us */
3492 
3493 		else
3494 			slottime = WMI_VDEV_SLOT_TIME_LONG; /* 20us */
3495 
3496 		param_id = WMI_VDEV_PARAM_SLOT_TIME;
3497 		ret = ath11k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
3498 						    param_id, slottime);
3499 		if (ret)
3500 			ath11k_warn(ar->ab, "Failed to set erp slot for VDEV: %d\n",
3501 				    arvif->vdev_id);
3502 		else
3503 			ath11k_dbg(ar->ab, ATH11K_DBG_MAC,
3504 				   "Set slottime: %d for VDEV: %d\n",
3505 				   slottime, arvif->vdev_id);
3506 	}
3507 
3508 	if (changed & BSS_CHANGED_ERP_PREAMBLE) {
3509 		u32 preamble;
3510 
3511 		if (info->use_short_preamble)
3512 			preamble = WMI_VDEV_PREAMBLE_SHORT;
3513 		else
3514 			preamble = WMI_VDEV_PREAMBLE_LONG;
3515 
3516 		param_id = WMI_VDEV_PARAM_PREAMBLE;
3517 		ret = ath11k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
3518 						    param_id, preamble);
3519 		if (ret)
3520 			ath11k_warn(ar->ab, "Failed to set preamble for VDEV: %d\n",
3521 				    arvif->vdev_id);
3522 		else
3523 			ath11k_dbg(ar->ab, ATH11K_DBG_MAC,
3524 				   "Set preamble: %d for VDEV: %d\n",
3525 				   preamble, arvif->vdev_id);
3526 	}
3527 
3528 	if (changed & BSS_CHANGED_ASSOC) {
3529 		if (vif->cfg.assoc)
3530 			ath11k_bss_assoc(hw, vif, info);
3531 		else
3532 			ath11k_bss_disassoc(hw, vif);
3533 	}
3534 
3535 	if (changed & BSS_CHANGED_TXPOWER) {
3536 		ath11k_dbg(ar->ab, ATH11K_DBG_MAC, "mac vdev_id %i txpower %d\n",
3537 			   arvif->vdev_id, info->txpower);
3538 
3539 		arvif->txpower = info->txpower;
3540 		ath11k_mac_txpower_recalc(ar);
3541 	}
3542 
3543 	if (changed & BSS_CHANGED_PS &&
3544 	    ar->ab->hw_params.supports_sta_ps) {
3545 		arvif->ps = vif->cfg.ps;
3546 
3547 		ret = ath11k_mac_config_ps(ar);
3548 		if (ret)
3549 			ath11k_warn(ar->ab, "failed to setup ps on vdev %i: %d\n",
3550 				    arvif->vdev_id, ret);
3551 	}
3552 
3553 	if (changed & BSS_CHANGED_MCAST_RATE &&
3554 	    !ath11k_mac_vif_chan(arvif->vif, &def)) {
3555 		band = def.chan->band;
3556 		mcast_rate = vif->bss_conf.mcast_rate[band];
3557 
3558 		if (mcast_rate > 0)
3559 			rateidx = mcast_rate - 1;
3560 		else
3561 			rateidx = ffs(vif->bss_conf.basic_rates) - 1;
3562 
3563 		if (ar->pdev->cap.supported_bands & WMI_HOST_WLAN_5G_CAP)
3564 			rateidx += ATH11K_MAC_FIRST_OFDM_RATE_IDX;
3565 
3566 		bitrate = ath11k_legacy_rates[rateidx].bitrate;
3567 		hw_value = ath11k_legacy_rates[rateidx].hw_value;
3568 
3569 		if (ath11k_mac_bitrate_is_cck(bitrate))
3570 			preamble = WMI_RATE_PREAMBLE_CCK;
3571 		else
3572 			preamble = WMI_RATE_PREAMBLE_OFDM;
3573 
3574 		rate = ATH11K_HW_RATE_CODE(hw_value, 0, preamble);
3575 
3576 		ath11k_dbg(ar->ab, ATH11K_DBG_MAC,
3577 			   "mac vdev %d mcast_rate %x\n",
3578 			   arvif->vdev_id, rate);
3579 
3580 		vdev_param = WMI_VDEV_PARAM_MCAST_DATA_RATE;
3581 		ret = ath11k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
3582 						    vdev_param, rate);
3583 		if (ret)
3584 			ath11k_warn(ar->ab,
3585 				    "failed to set mcast rate on vdev %i: %d\n",
3586 				    arvif->vdev_id,  ret);
3587 
3588 		vdev_param = WMI_VDEV_PARAM_BCAST_DATA_RATE;
3589 		ret = ath11k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
3590 						    vdev_param, rate);
3591 		if (ret)
3592 			ath11k_warn(ar->ab,
3593 				    "failed to set bcast rate on vdev %i: %d\n",
3594 				    arvif->vdev_id,  ret);
3595 	}
3596 
3597 	if (changed & BSS_CHANGED_BASIC_RATES &&
3598 	    !ath11k_mac_vif_chan(arvif->vif, &def))
3599 		ath11k_recalculate_mgmt_rate(ar, vif, &def);
3600 
3601 	if (changed & BSS_CHANGED_TWT) {
3602 		struct wmi_twt_enable_params twt_params = {0};
3603 
3604 		if (info->twt_requester || info->twt_responder) {
3605 			ath11k_wmi_fill_default_twt_params(&twt_params);
3606 			ath11k_wmi_send_twt_enable_cmd(ar, ar->pdev->pdev_id,
3607 						       &twt_params);
3608 		} else {
3609 			ath11k_wmi_send_twt_disable_cmd(ar, ar->pdev->pdev_id);
3610 		}
3611 	}
3612 
3613 	if (changed & BSS_CHANGED_HE_OBSS_PD)
3614 		ath11k_mac_config_obss_pd(ar, &info->he_obss_pd);
3615 
3616 	if (changed & BSS_CHANGED_HE_BSS_COLOR) {
3617 		if (vif->type == NL80211_IFTYPE_AP) {
3618 			ret = ath11k_wmi_send_obss_color_collision_cfg_cmd(
3619 				ar, arvif->vdev_id, info->he_bss_color.color,
3620 				ATH11K_BSS_COLOR_COLLISION_DETECTION_AP_PERIOD_MS,
3621 				info->he_bss_color.enabled);
3622 			if (ret)
3623 				ath11k_warn(ar->ab, "failed to set bss color collision on vdev %i: %d\n",
3624 					    arvif->vdev_id,  ret);
3625 
3626 			param_id = WMI_VDEV_PARAM_BSS_COLOR;
3627 			if (info->he_bss_color.enabled)
3628 				param_value = info->he_bss_color.color <<
3629 						IEEE80211_HE_OPERATION_BSS_COLOR_OFFSET;
3630 			else
3631 				param_value = IEEE80211_HE_OPERATION_BSS_COLOR_DISABLED;
3632 
3633 			ret = ath11k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
3634 							    param_id,
3635 							    param_value);
3636 			if (ret)
3637 				ath11k_warn(ar->ab,
3638 					    "failed to set bss color param on vdev %i: %d\n",
3639 					    arvif->vdev_id,  ret);
3640 
3641 			ath11k_dbg(ar->ab, ATH11K_DBG_MAC,
3642 				   "bss color param 0x%x set on vdev %i\n",
3643 				   param_value, arvif->vdev_id);
3644 		} else if (vif->type == NL80211_IFTYPE_STATION) {
3645 			ret = ath11k_wmi_send_bss_color_change_enable_cmd(ar,
3646 									  arvif->vdev_id,
3647 									  1);
3648 			if (ret)
3649 				ath11k_warn(ar->ab, "failed to enable bss color change on vdev %i: %d\n",
3650 					    arvif->vdev_id,  ret);
3651 			ret = ath11k_wmi_send_obss_color_collision_cfg_cmd(
3652 				ar, arvif->vdev_id, 0,
3653 				ATH11K_BSS_COLOR_COLLISION_DETECTION_STA_PERIOD_MS, 1);
3654 			if (ret)
3655 				ath11k_warn(ar->ab, "failed to set bss color collision on vdev %i: %d\n",
3656 					    arvif->vdev_id,  ret);
3657 		}
3658 	}
3659 
3660 	if (changed & BSS_CHANGED_FTM_RESPONDER &&
3661 	    arvif->ftm_responder != info->ftm_responder &&
3662 	    test_bit(WMI_TLV_SERVICE_RTT, ar->ab->wmi_ab.svc_map) &&
3663 	    (vif->type == NL80211_IFTYPE_AP ||
3664 	     vif->type == NL80211_IFTYPE_MESH_POINT)) {
3665 		arvif->ftm_responder = info->ftm_responder;
3666 		param = WMI_VDEV_PARAM_ENABLE_DISABLE_RTT_RESPONDER_ROLE;
3667 		ret = ath11k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id, param,
3668 						    arvif->ftm_responder);
3669 		if (ret)
3670 			ath11k_warn(ar->ab, "Failed to set ftm responder %i: %d\n",
3671 				    arvif->vdev_id, ret);
3672 	}
3673 
3674 	if (changed & BSS_CHANGED_FILS_DISCOVERY ||
3675 	    changed & BSS_CHANGED_UNSOL_BCAST_PROBE_RESP)
3676 		ath11k_mac_fils_discovery(arvif, info);
3677 
3678 	if (changed & BSS_CHANGED_ARP_FILTER) {
3679 		ipv4_cnt = min(vif->cfg.arp_addr_cnt, ATH11K_IPV4_MAX_COUNT);
3680 		memcpy(arvif->arp_ns_offload.ipv4_addr,
3681 		       vif->cfg.arp_addr_list,
3682 		       ipv4_cnt * sizeof(u32));
3683 		memcpy(arvif->arp_ns_offload.mac_addr, vif->addr, ETH_ALEN);
3684 		arvif->arp_ns_offload.ipv4_count = ipv4_cnt;
3685 
3686 		ath11k_dbg(ar->ab, ATH11K_DBG_MAC, "mac arp_addr_cnt %d vif->addr %pM, offload_addr %pI4\n",
3687 			   vif->cfg.arp_addr_cnt,
3688 			   vif->addr, arvif->arp_ns_offload.ipv4_addr);
3689 	}
3690 
3691 	mutex_unlock(&ar->conf_mutex);
3692 }
3693 
3694 void __ath11k_mac_scan_finish(struct ath11k *ar)
3695 {
3696 	lockdep_assert_held(&ar->data_lock);
3697 
3698 	switch (ar->scan.state) {
3699 	case ATH11K_SCAN_IDLE:
3700 		break;
3701 	case ATH11K_SCAN_RUNNING:
3702 	case ATH11K_SCAN_ABORTING:
3703 		if (ar->scan.is_roc && ar->scan.roc_notify)
3704 			ieee80211_remain_on_channel_expired(ar->hw);
3705 		fallthrough;
3706 	case ATH11K_SCAN_STARTING:
3707 		if (!ar->scan.is_roc) {
3708 			struct cfg80211_scan_info info = {
3709 				.aborted = ((ar->scan.state ==
3710 					    ATH11K_SCAN_ABORTING) ||
3711 					    (ar->scan.state ==
3712 					    ATH11K_SCAN_STARTING)),
3713 			};
3714 
3715 			ieee80211_scan_completed(ar->hw, &info);
3716 		}
3717 
3718 		ar->scan.state = ATH11K_SCAN_IDLE;
3719 		ar->scan_channel = NULL;
3720 		ar->scan.roc_freq = 0;
3721 		cancel_delayed_work(&ar->scan.timeout);
3722 		complete_all(&ar->scan.completed);
3723 		break;
3724 	}
3725 }
3726 
3727 void ath11k_mac_scan_finish(struct ath11k *ar)
3728 {
3729 	spin_lock_bh(&ar->data_lock);
3730 	__ath11k_mac_scan_finish(ar);
3731 	spin_unlock_bh(&ar->data_lock);
3732 }
3733 
3734 static int ath11k_scan_stop(struct ath11k *ar)
3735 {
3736 	struct scan_cancel_param arg = {
3737 		.req_type = WLAN_SCAN_CANCEL_SINGLE,
3738 		.scan_id = ATH11K_SCAN_ID,
3739 	};
3740 	int ret;
3741 
3742 	lockdep_assert_held(&ar->conf_mutex);
3743 
3744 	/* TODO: Fill other STOP Params */
3745 	arg.pdev_id = ar->pdev->pdev_id;
3746 
3747 	ret = ath11k_wmi_send_scan_stop_cmd(ar, &arg);
3748 	if (ret) {
3749 		ath11k_warn(ar->ab, "failed to stop wmi scan: %d\n", ret);
3750 		goto out;
3751 	}
3752 
3753 	ret = wait_for_completion_timeout(&ar->scan.completed, 3 * HZ);
3754 	if (ret == 0) {
3755 		ath11k_warn(ar->ab,
3756 			    "failed to receive scan abort comple: timed out\n");
3757 		ret = -ETIMEDOUT;
3758 	} else if (ret > 0) {
3759 		ret = 0;
3760 	}
3761 
3762 out:
3763 	/* Scan state should be updated upon scan completion but in case
3764 	 * firmware fails to deliver the event (for whatever reason) it is
3765 	 * desired to clean up scan state anyway. Firmware may have just
3766 	 * dropped the scan completion event delivery due to transport pipe
3767 	 * being overflown with data and/or it can recover on its own before
3768 	 * next scan request is submitted.
3769 	 */
3770 	spin_lock_bh(&ar->data_lock);
3771 	if (ar->scan.state != ATH11K_SCAN_IDLE)
3772 		__ath11k_mac_scan_finish(ar);
3773 	spin_unlock_bh(&ar->data_lock);
3774 
3775 	return ret;
3776 }
3777 
3778 static void ath11k_scan_abort(struct ath11k *ar)
3779 {
3780 	int ret;
3781 
3782 	lockdep_assert_held(&ar->conf_mutex);
3783 
3784 	spin_lock_bh(&ar->data_lock);
3785 
3786 	switch (ar->scan.state) {
3787 	case ATH11K_SCAN_IDLE:
3788 		/* This can happen if timeout worker kicked in and called
3789 		 * abortion while scan completion was being processed.
3790 		 */
3791 		break;
3792 	case ATH11K_SCAN_STARTING:
3793 	case ATH11K_SCAN_ABORTING:
3794 		ath11k_warn(ar->ab, "refusing scan abortion due to invalid scan state: %d\n",
3795 			    ar->scan.state);
3796 		break;
3797 	case ATH11K_SCAN_RUNNING:
3798 		ar->scan.state = ATH11K_SCAN_ABORTING;
3799 		spin_unlock_bh(&ar->data_lock);
3800 
3801 		ret = ath11k_scan_stop(ar);
3802 		if (ret)
3803 			ath11k_warn(ar->ab, "failed to abort scan: %d\n", ret);
3804 
3805 		spin_lock_bh(&ar->data_lock);
3806 		break;
3807 	}
3808 
3809 	spin_unlock_bh(&ar->data_lock);
3810 }
3811 
3812 static void ath11k_scan_timeout_work(struct work_struct *work)
3813 {
3814 	struct ath11k *ar = container_of(work, struct ath11k,
3815 					 scan.timeout.work);
3816 
3817 	mutex_lock(&ar->conf_mutex);
3818 	ath11k_scan_abort(ar);
3819 	mutex_unlock(&ar->conf_mutex);
3820 }
3821 
3822 static int ath11k_start_scan(struct ath11k *ar,
3823 			     struct scan_req_params *arg)
3824 {
3825 	int ret;
3826 	unsigned long timeout = 1 * HZ;
3827 
3828 	lockdep_assert_held(&ar->conf_mutex);
3829 
3830 	if (ath11k_spectral_get_mode(ar) == ATH11K_SPECTRAL_BACKGROUND)
3831 		ath11k_spectral_reset_buffer(ar);
3832 
3833 	ret = ath11k_wmi_send_scan_start_cmd(ar, arg);
3834 	if (ret)
3835 		return ret;
3836 
3837 	if (test_bit(WMI_TLV_SERVICE_11D_OFFLOAD, ar->ab->wmi_ab.svc_map)) {
3838 		timeout = 5 * HZ;
3839 
3840 		if (ar->supports_6ghz)
3841 			timeout += 5 * HZ;
3842 	}
3843 
3844 	ret = wait_for_completion_timeout(&ar->scan.started, timeout);
3845 	if (ret == 0) {
3846 		ret = ath11k_scan_stop(ar);
3847 		if (ret)
3848 			ath11k_warn(ar->ab, "failed to stop scan: %d\n", ret);
3849 
3850 		return -ETIMEDOUT;
3851 	}
3852 
3853 	/* If we failed to start the scan, return error code at
3854 	 * this point.  This is probably due to some issue in the
3855 	 * firmware, but no need to wedge the driver due to that...
3856 	 */
3857 	spin_lock_bh(&ar->data_lock);
3858 	if (ar->scan.state == ATH11K_SCAN_IDLE) {
3859 		spin_unlock_bh(&ar->data_lock);
3860 		return -EINVAL;
3861 	}
3862 	spin_unlock_bh(&ar->data_lock);
3863 
3864 	return 0;
3865 }
3866 
3867 static int ath11k_mac_op_hw_scan(struct ieee80211_hw *hw,
3868 				 struct ieee80211_vif *vif,
3869 				 struct ieee80211_scan_request *hw_req)
3870 {
3871 	struct ath11k *ar = hw->priv;
3872 	struct ath11k_vif *arvif = ath11k_vif_to_arvif(vif);
3873 	struct cfg80211_scan_request *req = &hw_req->req;
3874 	struct scan_req_params *arg = NULL;
3875 	int ret = 0;
3876 	int i;
3877 	u32 scan_timeout;
3878 
3879 	/* Firmwares advertising the support of triggering 11D algorithm
3880 	 * on the scan results of a regular scan expects driver to send
3881 	 * WMI_11D_SCAN_START_CMDID before sending WMI_START_SCAN_CMDID.
3882 	 * With this feature, separate 11D scan can be avoided since
3883 	 * regdomain can be determined with the scan results of the
3884 	 * regular scan.
3885 	 */
3886 	if (ar->state_11d == ATH11K_11D_PREPARING &&
3887 	    test_bit(WMI_TLV_SERVICE_SUPPORT_11D_FOR_HOST_SCAN,
3888 		     ar->ab->wmi_ab.svc_map))
3889 		ath11k_mac_11d_scan_start(ar, arvif->vdev_id);
3890 
3891 	mutex_lock(&ar->conf_mutex);
3892 
3893 	spin_lock_bh(&ar->data_lock);
3894 	switch (ar->scan.state) {
3895 	case ATH11K_SCAN_IDLE:
3896 		reinit_completion(&ar->scan.started);
3897 		reinit_completion(&ar->scan.completed);
3898 		ar->scan.state = ATH11K_SCAN_STARTING;
3899 		ar->scan.is_roc = false;
3900 		ar->scan.vdev_id = arvif->vdev_id;
3901 		ret = 0;
3902 		break;
3903 	case ATH11K_SCAN_STARTING:
3904 	case ATH11K_SCAN_RUNNING:
3905 	case ATH11K_SCAN_ABORTING:
3906 		ret = -EBUSY;
3907 		break;
3908 	}
3909 	spin_unlock_bh(&ar->data_lock);
3910 
3911 	if (ret)
3912 		goto exit;
3913 
3914 	arg = kzalloc(sizeof(*arg), GFP_KERNEL);
3915 
3916 	if (!arg) {
3917 		ret = -ENOMEM;
3918 		goto exit;
3919 	}
3920 
3921 	ath11k_wmi_start_scan_init(ar, arg);
3922 	arg->vdev_id = arvif->vdev_id;
3923 	arg->scan_id = ATH11K_SCAN_ID;
3924 
3925 	if (req->ie_len) {
3926 		arg->extraie.ptr = kmemdup(req->ie, req->ie_len, GFP_KERNEL);
3927 		if (!arg->extraie.ptr) {
3928 			ret = -ENOMEM;
3929 			goto exit;
3930 		}
3931 		arg->extraie.len = req->ie_len;
3932 	}
3933 
3934 	if (req->n_ssids) {
3935 		arg->num_ssids = req->n_ssids;
3936 		for (i = 0; i < arg->num_ssids; i++) {
3937 			arg->ssid[i].length  = req->ssids[i].ssid_len;
3938 			memcpy(&arg->ssid[i].ssid, req->ssids[i].ssid,
3939 			       req->ssids[i].ssid_len);
3940 		}
3941 	} else {
3942 		arg->scan_flags |= WMI_SCAN_FLAG_PASSIVE;
3943 	}
3944 
3945 	if (req->n_channels) {
3946 		arg->num_chan = req->n_channels;
3947 		arg->chan_list = kcalloc(arg->num_chan, sizeof(*arg->chan_list),
3948 					 GFP_KERNEL);
3949 
3950 		if (!arg->chan_list) {
3951 			ret = -ENOMEM;
3952 			goto exit;
3953 		}
3954 
3955 		for (i = 0; i < arg->num_chan; i++) {
3956 			if (test_bit(WMI_TLV_SERVICE_SCAN_CONFIG_PER_CHANNEL,
3957 				     ar->ab->wmi_ab.svc_map)) {
3958 				arg->chan_list[i] =
3959 					u32_encode_bits(req->channels[i]->center_freq,
3960 							WMI_SCAN_CONFIG_PER_CHANNEL_MASK);
3961 
3962 				/* If NL80211_SCAN_FLAG_COLOCATED_6GHZ is set in scan
3963 				 * flags, then scan all PSC channels in 6 GHz band and
3964 				 * those non-PSC channels where RNR IE is found during
3965 				 * the legacy 2.4/5 GHz scan.
3966 				 * If NL80211_SCAN_FLAG_COLOCATED_6GHZ is not set,
3967 				 * then all channels in 6 GHz will be scanned.
3968 				 */
3969 				if (req->channels[i]->band == NL80211_BAND_6GHZ &&
3970 				    req->flags & NL80211_SCAN_FLAG_COLOCATED_6GHZ &&
3971 				    !cfg80211_channel_is_psc(req->channels[i]))
3972 					arg->chan_list[i] |=
3973 						WMI_SCAN_CH_FLAG_SCAN_ONLY_IF_RNR_FOUND;
3974 			} else {
3975 				arg->chan_list[i] = req->channels[i]->center_freq;
3976 			}
3977 		}
3978 	}
3979 
3980 	if (req->flags & NL80211_SCAN_FLAG_RANDOM_ADDR) {
3981 		arg->scan_f_add_spoofed_mac_in_probe = 1;
3982 		ether_addr_copy(arg->mac_addr.addr, req->mac_addr);
3983 		ether_addr_copy(arg->mac_mask.addr, req->mac_addr_mask);
3984 	}
3985 
3986 	/* if duration is set, default dwell times will be overwritten */
3987 	if (req->duration) {
3988 		arg->dwell_time_active = req->duration;
3989 		arg->dwell_time_active_2g = req->duration;
3990 		arg->dwell_time_active_6g = req->duration;
3991 		arg->dwell_time_passive = req->duration;
3992 		arg->dwell_time_passive_6g = req->duration;
3993 		arg->burst_duration = req->duration;
3994 
3995 		scan_timeout = min_t(u32, arg->max_rest_time *
3996 				(arg->num_chan - 1) + (req->duration +
3997 				ATH11K_SCAN_CHANNEL_SWITCH_WMI_EVT_OVERHEAD) *
3998 				arg->num_chan, arg->max_scan_time);
3999 	} else {
4000 		scan_timeout = arg->max_scan_time;
4001 	}
4002 
4003 	/* Add a margin to account for event/command processing */
4004 	scan_timeout += ATH11K_MAC_SCAN_CMD_EVT_OVERHEAD;
4005 
4006 	ret = ath11k_start_scan(ar, arg);
4007 	if (ret) {
4008 		ath11k_warn(ar->ab, "failed to start hw scan: %d\n", ret);
4009 		spin_lock_bh(&ar->data_lock);
4010 		ar->scan.state = ATH11K_SCAN_IDLE;
4011 		spin_unlock_bh(&ar->data_lock);
4012 	}
4013 
4014 	ieee80211_queue_delayed_work(ar->hw, &ar->scan.timeout,
4015 				     msecs_to_jiffies(scan_timeout));
4016 
4017 exit:
4018 	if (arg) {
4019 		kfree(arg->chan_list);
4020 		kfree(arg->extraie.ptr);
4021 		kfree(arg);
4022 	}
4023 
4024 	mutex_unlock(&ar->conf_mutex);
4025 
4026 	if (ar->state_11d == ATH11K_11D_PREPARING)
4027 		ath11k_mac_11d_scan_start(ar, arvif->vdev_id);
4028 
4029 	return ret;
4030 }
4031 
4032 static void ath11k_mac_op_cancel_hw_scan(struct ieee80211_hw *hw,
4033 					 struct ieee80211_vif *vif)
4034 {
4035 	struct ath11k *ar = hw->priv;
4036 
4037 	mutex_lock(&ar->conf_mutex);
4038 	ath11k_scan_abort(ar);
4039 	mutex_unlock(&ar->conf_mutex);
4040 
4041 	cancel_delayed_work_sync(&ar->scan.timeout);
4042 }
4043 
4044 static int ath11k_install_key(struct ath11k_vif *arvif,
4045 			      struct ieee80211_key_conf *key,
4046 			      enum set_key_cmd cmd,
4047 			      const u8 *macaddr, u32 flags)
4048 {
4049 	int ret;
4050 	struct ath11k *ar = arvif->ar;
4051 	struct wmi_vdev_install_key_arg arg = {
4052 		.vdev_id = arvif->vdev_id,
4053 		.key_idx = key->keyidx,
4054 		.key_len = key->keylen,
4055 		.key_data = key->key,
4056 		.key_flags = flags,
4057 		.macaddr = macaddr,
4058 	};
4059 
4060 	lockdep_assert_held(&arvif->ar->conf_mutex);
4061 
4062 	reinit_completion(&ar->install_key_done);
4063 
4064 	if (test_bit(ATH11K_FLAG_HW_CRYPTO_DISABLED, &ar->ab->dev_flags))
4065 		return 0;
4066 
4067 	if (cmd == DISABLE_KEY) {
4068 		arg.key_cipher = WMI_CIPHER_NONE;
4069 		arg.key_data = NULL;
4070 		goto install;
4071 	}
4072 
4073 	switch (key->cipher) {
4074 	case WLAN_CIPHER_SUITE_CCMP:
4075 		arg.key_cipher = WMI_CIPHER_AES_CCM;
4076 		/* TODO: Re-check if flag is valid */
4077 		key->flags |= IEEE80211_KEY_FLAG_GENERATE_IV_MGMT;
4078 		break;
4079 	case WLAN_CIPHER_SUITE_TKIP:
4080 		arg.key_cipher = WMI_CIPHER_TKIP;
4081 		arg.key_txmic_len = 8;
4082 		arg.key_rxmic_len = 8;
4083 		break;
4084 	case WLAN_CIPHER_SUITE_CCMP_256:
4085 		arg.key_cipher = WMI_CIPHER_AES_CCM;
4086 		break;
4087 	case WLAN_CIPHER_SUITE_GCMP:
4088 	case WLAN_CIPHER_SUITE_GCMP_256:
4089 		arg.key_cipher = WMI_CIPHER_AES_GCM;
4090 		break;
4091 	default:
4092 		ath11k_warn(ar->ab, "cipher %d is not supported\n", key->cipher);
4093 		return -EOPNOTSUPP;
4094 	}
4095 
4096 	if (test_bit(ATH11K_FLAG_RAW_MODE, &ar->ab->dev_flags))
4097 		key->flags |= IEEE80211_KEY_FLAG_GENERATE_IV |
4098 			      IEEE80211_KEY_FLAG_RESERVE_TAILROOM;
4099 
4100 install:
4101 	ret = ath11k_wmi_vdev_install_key(arvif->ar, &arg);
4102 
4103 	if (ret)
4104 		return ret;
4105 
4106 	if (!wait_for_completion_timeout(&ar->install_key_done, 1 * HZ))
4107 		return -ETIMEDOUT;
4108 
4109 	return ar->install_key_status ? -EINVAL : 0;
4110 }
4111 
4112 static int ath11k_clear_peer_keys(struct ath11k_vif *arvif,
4113 				  const u8 *addr)
4114 {
4115 	struct ath11k *ar = arvif->ar;
4116 	struct ath11k_base *ab = ar->ab;
4117 	struct ath11k_peer *peer;
4118 	int first_errno = 0;
4119 	int ret;
4120 	int i;
4121 	u32 flags = 0;
4122 
4123 	lockdep_assert_held(&ar->conf_mutex);
4124 
4125 	spin_lock_bh(&ab->base_lock);
4126 	peer = ath11k_peer_find(ab, arvif->vdev_id, addr);
4127 	spin_unlock_bh(&ab->base_lock);
4128 
4129 	if (!peer)
4130 		return -ENOENT;
4131 
4132 	for (i = 0; i < ARRAY_SIZE(peer->keys); i++) {
4133 		if (!peer->keys[i])
4134 			continue;
4135 
4136 		/* key flags are not required to delete the key */
4137 		ret = ath11k_install_key(arvif, peer->keys[i],
4138 					 DISABLE_KEY, addr, flags);
4139 		if (ret < 0 && first_errno == 0)
4140 			first_errno = ret;
4141 
4142 		if (ret < 0)
4143 			ath11k_warn(ab, "failed to remove peer key %d: %d\n",
4144 				    i, ret);
4145 
4146 		spin_lock_bh(&ab->base_lock);
4147 		peer->keys[i] = NULL;
4148 		spin_unlock_bh(&ab->base_lock);
4149 	}
4150 
4151 	return first_errno;
4152 }
4153 
4154 static int ath11k_mac_op_set_key(struct ieee80211_hw *hw, enum set_key_cmd cmd,
4155 				 struct ieee80211_vif *vif, struct ieee80211_sta *sta,
4156 				 struct ieee80211_key_conf *key)
4157 {
4158 	struct ath11k *ar = hw->priv;
4159 	struct ath11k_base *ab = ar->ab;
4160 	struct ath11k_vif *arvif = ath11k_vif_to_arvif(vif);
4161 	struct ath11k_peer *peer;
4162 	struct ath11k_sta *arsta;
4163 	const u8 *peer_addr;
4164 	int ret = 0;
4165 	u32 flags = 0;
4166 
4167 	/* BIP needs to be done in software */
4168 	if (key->cipher == WLAN_CIPHER_SUITE_AES_CMAC ||
4169 	    key->cipher == WLAN_CIPHER_SUITE_BIP_GMAC_128 ||
4170 	    key->cipher == WLAN_CIPHER_SUITE_BIP_GMAC_256 ||
4171 	    key->cipher == WLAN_CIPHER_SUITE_BIP_CMAC_256)
4172 		return 1;
4173 
4174 	if (test_bit(ATH11K_FLAG_HW_CRYPTO_DISABLED, &ar->ab->dev_flags))
4175 		return 1;
4176 
4177 	if (key->keyidx > WMI_MAX_KEY_INDEX)
4178 		return -ENOSPC;
4179 
4180 	mutex_lock(&ar->conf_mutex);
4181 
4182 	if (sta)
4183 		peer_addr = sta->addr;
4184 	else if (arvif->vdev_type == WMI_VDEV_TYPE_STA)
4185 		peer_addr = vif->bss_conf.bssid;
4186 	else
4187 		peer_addr = vif->addr;
4188 
4189 	key->hw_key_idx = key->keyidx;
4190 
4191 	/* the peer should not disappear in mid-way (unless FW goes awry) since
4192 	 * we already hold conf_mutex. we just make sure its there now.
4193 	 */
4194 	spin_lock_bh(&ab->base_lock);
4195 	peer = ath11k_peer_find(ab, arvif->vdev_id, peer_addr);
4196 
4197 	/* flush the fragments cache during key (re)install to
4198 	 * ensure all frags in the new frag list belong to the same key.
4199 	 */
4200 	if (peer && sta && cmd == SET_KEY)
4201 		ath11k_peer_frags_flush(ar, peer);
4202 	spin_unlock_bh(&ab->base_lock);
4203 
4204 	if (!peer) {
4205 		if (cmd == SET_KEY) {
4206 			ath11k_warn(ab, "cannot install key for non-existent peer %pM\n",
4207 				    peer_addr);
4208 			ret = -EOPNOTSUPP;
4209 			goto exit;
4210 		} else {
4211 			/* if the peer doesn't exist there is no key to disable
4212 			 * anymore
4213 			 */
4214 			goto exit;
4215 		}
4216 	}
4217 
4218 	if (key->flags & IEEE80211_KEY_FLAG_PAIRWISE)
4219 		flags |= WMI_KEY_PAIRWISE;
4220 	else
4221 		flags |= WMI_KEY_GROUP;
4222 
4223 	ret = ath11k_install_key(arvif, key, cmd, peer_addr, flags);
4224 	if (ret) {
4225 		ath11k_warn(ab, "ath11k_install_key failed (%d)\n", ret);
4226 		goto exit;
4227 	}
4228 
4229 	ret = ath11k_dp_peer_rx_pn_replay_config(arvif, peer_addr, cmd, key);
4230 	if (ret) {
4231 		ath11k_warn(ab, "failed to offload PN replay detection %d\n", ret);
4232 		goto exit;
4233 	}
4234 
4235 	spin_lock_bh(&ab->base_lock);
4236 	peer = ath11k_peer_find(ab, arvif->vdev_id, peer_addr);
4237 	if (peer && cmd == SET_KEY) {
4238 		peer->keys[key->keyidx] = key;
4239 		if (key->flags & IEEE80211_KEY_FLAG_PAIRWISE) {
4240 			peer->ucast_keyidx = key->keyidx;
4241 			peer->sec_type = ath11k_dp_tx_get_encrypt_type(key->cipher);
4242 		} else {
4243 			peer->mcast_keyidx = key->keyidx;
4244 			peer->sec_type_grp = ath11k_dp_tx_get_encrypt_type(key->cipher);
4245 		}
4246 	} else if (peer && cmd == DISABLE_KEY) {
4247 		peer->keys[key->keyidx] = NULL;
4248 		if (key->flags & IEEE80211_KEY_FLAG_PAIRWISE)
4249 			peer->ucast_keyidx = 0;
4250 		else
4251 			peer->mcast_keyidx = 0;
4252 	} else if (!peer)
4253 		/* impossible unless FW goes crazy */
4254 		ath11k_warn(ab, "peer %pM disappeared!\n", peer_addr);
4255 
4256 	if (sta) {
4257 		arsta = (struct ath11k_sta *)sta->drv_priv;
4258 
4259 		switch (key->cipher) {
4260 		case WLAN_CIPHER_SUITE_TKIP:
4261 		case WLAN_CIPHER_SUITE_CCMP:
4262 		case WLAN_CIPHER_SUITE_CCMP_256:
4263 		case WLAN_CIPHER_SUITE_GCMP:
4264 		case WLAN_CIPHER_SUITE_GCMP_256:
4265 			if (cmd == SET_KEY)
4266 				arsta->pn_type = HAL_PN_TYPE_WPA;
4267 			else
4268 				arsta->pn_type = HAL_PN_TYPE_NONE;
4269 			break;
4270 		default:
4271 			arsta->pn_type = HAL_PN_TYPE_NONE;
4272 			break;
4273 		}
4274 	}
4275 
4276 	spin_unlock_bh(&ab->base_lock);
4277 
4278 exit:
4279 	mutex_unlock(&ar->conf_mutex);
4280 	return ret;
4281 }
4282 
4283 static int
4284 ath11k_mac_bitrate_mask_num_vht_rates(struct ath11k *ar,
4285 				      enum nl80211_band band,
4286 				      const struct cfg80211_bitrate_mask *mask)
4287 {
4288 	int num_rates = 0;
4289 	int i;
4290 
4291 	for (i = 0; i < ARRAY_SIZE(mask->control[band].vht_mcs); i++)
4292 		num_rates += hweight16(mask->control[band].vht_mcs[i]);
4293 
4294 	return num_rates;
4295 }
4296 
4297 static int
4298 ath11k_mac_bitrate_mask_num_he_rates(struct ath11k *ar,
4299 				     enum nl80211_band band,
4300 				     const struct cfg80211_bitrate_mask *mask)
4301 {
4302 	int num_rates = 0;
4303 	int i;
4304 
4305 	for (i = 0; i < ARRAY_SIZE(mask->control[band].he_mcs); i++)
4306 		num_rates += hweight16(mask->control[band].he_mcs[i]);
4307 
4308 	return num_rates;
4309 }
4310 
4311 static int
4312 ath11k_mac_set_peer_vht_fixed_rate(struct ath11k_vif *arvif,
4313 				   struct ieee80211_sta *sta,
4314 				   const struct cfg80211_bitrate_mask *mask,
4315 				   enum nl80211_band band)
4316 {
4317 	struct ath11k *ar = arvif->ar;
4318 	u8 vht_rate, nss;
4319 	u32 rate_code;
4320 	int ret, i;
4321 
4322 	lockdep_assert_held(&ar->conf_mutex);
4323 
4324 	nss = 0;
4325 
4326 	for (i = 0; i < ARRAY_SIZE(mask->control[band].vht_mcs); i++) {
4327 		if (hweight16(mask->control[band].vht_mcs[i]) == 1) {
4328 			nss = i + 1;
4329 			vht_rate = ffs(mask->control[band].vht_mcs[i]) - 1;
4330 		}
4331 	}
4332 
4333 	if (!nss) {
4334 		ath11k_warn(ar->ab, "No single VHT Fixed rate found to set for %pM",
4335 			    sta->addr);
4336 		return -EINVAL;
4337 	}
4338 
4339 	/* Avoid updating invalid nss as fixed rate*/
4340 	if (nss > sta->deflink.rx_nss)
4341 		return -EINVAL;
4342 
4343 	ath11k_dbg(ar->ab, ATH11K_DBG_MAC,
4344 		   "Setting Fixed VHT Rate for peer %pM. Device will not switch to any other selected rates",
4345 		   sta->addr);
4346 
4347 	rate_code = ATH11K_HW_RATE_CODE(vht_rate, nss - 1,
4348 					WMI_RATE_PREAMBLE_VHT);
4349 	ret = ath11k_wmi_set_peer_param(ar, sta->addr,
4350 					arvif->vdev_id,
4351 					WMI_PEER_PARAM_FIXED_RATE,
4352 					rate_code);
4353 	if (ret)
4354 		ath11k_warn(ar->ab,
4355 			    "failed to update STA %pM Fixed Rate %d: %d\n",
4356 			     sta->addr, rate_code, ret);
4357 
4358 	return ret;
4359 }
4360 
4361 static int
4362 ath11k_mac_set_peer_he_fixed_rate(struct ath11k_vif *arvif,
4363 				  struct ieee80211_sta *sta,
4364 				  const struct cfg80211_bitrate_mask *mask,
4365 				  enum nl80211_band band)
4366 {
4367 	struct ath11k *ar = arvif->ar;
4368 	u8 he_rate, nss;
4369 	u32 rate_code;
4370 	int ret, i;
4371 
4372 	lockdep_assert_held(&ar->conf_mutex);
4373 
4374 	nss = 0;
4375 
4376 	for (i = 0; i < ARRAY_SIZE(mask->control[band].he_mcs); i++) {
4377 		if (hweight16(mask->control[band].he_mcs[i]) == 1) {
4378 			nss = i + 1;
4379 			he_rate = ffs(mask->control[band].he_mcs[i]) - 1;
4380 		}
4381 	}
4382 
4383 	if (!nss) {
4384 		ath11k_warn(ar->ab, "No single he fixed rate found to set for %pM",
4385 			    sta->addr);
4386 		return -EINVAL;
4387 	}
4388 
4389 	/* Avoid updating invalid nss as fixed rate */
4390 	if (nss > sta->deflink.rx_nss)
4391 		return -EINVAL;
4392 
4393 	ath11k_dbg(ar->ab, ATH11K_DBG_MAC,
4394 		   "mac setting fixed he rate for peer %pM, device will not switch to any other selected rates",
4395 		   sta->addr);
4396 
4397 	rate_code = ATH11K_HW_RATE_CODE(he_rate, nss - 1,
4398 					WMI_RATE_PREAMBLE_HE);
4399 
4400 	ret = ath11k_wmi_set_peer_param(ar, sta->addr,
4401 					arvif->vdev_id,
4402 					WMI_PEER_PARAM_FIXED_RATE,
4403 					rate_code);
4404 	if (ret)
4405 		ath11k_warn(ar->ab,
4406 			    "failed to update sta %pM fixed rate %d: %d\n",
4407 			    sta->addr, rate_code, ret);
4408 
4409 	return ret;
4410 }
4411 
4412 static int ath11k_station_assoc(struct ath11k *ar,
4413 				struct ieee80211_vif *vif,
4414 				struct ieee80211_sta *sta,
4415 				bool reassoc)
4416 {
4417 	struct ath11k_vif *arvif = ath11k_vif_to_arvif(vif);
4418 	struct peer_assoc_params peer_arg;
4419 	int ret = 0;
4420 	struct cfg80211_chan_def def;
4421 	enum nl80211_band band;
4422 	struct cfg80211_bitrate_mask *mask;
4423 	u8 num_vht_rates, num_he_rates;
4424 
4425 	lockdep_assert_held(&ar->conf_mutex);
4426 
4427 	if (WARN_ON(ath11k_mac_vif_chan(vif, &def)))
4428 		return -EPERM;
4429 
4430 	band = def.chan->band;
4431 	mask = &arvif->bitrate_mask;
4432 
4433 	ath11k_peer_assoc_prepare(ar, vif, sta, &peer_arg, reassoc);
4434 
4435 	peer_arg.is_assoc = true;
4436 	ret = ath11k_wmi_send_peer_assoc_cmd(ar, &peer_arg);
4437 	if (ret) {
4438 		ath11k_warn(ar->ab, "failed to run peer assoc for STA %pM vdev %i: %d\n",
4439 			    sta->addr, arvif->vdev_id, ret);
4440 		return ret;
4441 	}
4442 
4443 	if (!wait_for_completion_timeout(&ar->peer_assoc_done, 1 * HZ)) {
4444 		ath11k_warn(ar->ab, "failed to get peer assoc conf event for %pM vdev %i\n",
4445 			    sta->addr, arvif->vdev_id);
4446 		return -ETIMEDOUT;
4447 	}
4448 
4449 	num_vht_rates = ath11k_mac_bitrate_mask_num_vht_rates(ar, band, mask);
4450 	num_he_rates = ath11k_mac_bitrate_mask_num_he_rates(ar, band, mask);
4451 
4452 	/* If single VHT/HE rate is configured (by set_bitrate_mask()),
4453 	 * peer_assoc will disable VHT/HE. This is now enabled by a peer specific
4454 	 * fixed param.
4455 	 * Note that all other rates and NSS will be disabled for this peer.
4456 	 */
4457 	if (sta->deflink.vht_cap.vht_supported && num_vht_rates == 1) {
4458 		ret = ath11k_mac_set_peer_vht_fixed_rate(arvif, sta, mask,
4459 							 band);
4460 		if (ret)
4461 			return ret;
4462 	} else if (sta->deflink.he_cap.has_he && num_he_rates == 1) {
4463 		ret = ath11k_mac_set_peer_he_fixed_rate(arvif, sta, mask,
4464 							band);
4465 		if (ret)
4466 			return ret;
4467 	}
4468 
4469 	/* Re-assoc is run only to update supported rates for given station. It
4470 	 * doesn't make much sense to reconfigure the peer completely.
4471 	 */
4472 	if (reassoc)
4473 		return 0;
4474 
4475 	ret = ath11k_setup_peer_smps(ar, arvif, sta->addr,
4476 				     &sta->deflink.ht_cap,
4477 				     le16_to_cpu(sta->deflink.he_6ghz_capa.capa));
4478 	if (ret) {
4479 		ath11k_warn(ar->ab, "failed to setup peer SMPS for vdev %d: %d\n",
4480 			    arvif->vdev_id, ret);
4481 		return ret;
4482 	}
4483 
4484 	if (!sta->wme) {
4485 		arvif->num_legacy_stations++;
4486 		ret = ath11k_recalc_rtscts_prot(arvif);
4487 		if (ret)
4488 			return ret;
4489 	}
4490 
4491 	if (sta->wme && sta->uapsd_queues) {
4492 		ret = ath11k_peer_assoc_qos_ap(ar, arvif, sta);
4493 		if (ret) {
4494 			ath11k_warn(ar->ab, "failed to set qos params for STA %pM for vdev %i: %d\n",
4495 				    sta->addr, arvif->vdev_id, ret);
4496 			return ret;
4497 		}
4498 	}
4499 
4500 	return 0;
4501 }
4502 
4503 static int ath11k_station_disassoc(struct ath11k *ar,
4504 				   struct ieee80211_vif *vif,
4505 				   struct ieee80211_sta *sta)
4506 {
4507 	struct ath11k_vif *arvif = (void *)vif->drv_priv;
4508 	int ret = 0;
4509 
4510 	lockdep_assert_held(&ar->conf_mutex);
4511 
4512 	if (!sta->wme) {
4513 		arvif->num_legacy_stations--;
4514 		ret = ath11k_recalc_rtscts_prot(arvif);
4515 		if (ret)
4516 			return ret;
4517 	}
4518 
4519 	ret = ath11k_clear_peer_keys(arvif, sta->addr);
4520 	if (ret) {
4521 		ath11k_warn(ar->ab, "failed to clear all peer keys for vdev %i: %d\n",
4522 			    arvif->vdev_id, ret);
4523 		return ret;
4524 	}
4525 	return 0;
4526 }
4527 
4528 static void ath11k_sta_rc_update_wk(struct work_struct *wk)
4529 {
4530 	struct ath11k *ar;
4531 	struct ath11k_vif *arvif;
4532 	struct ath11k_sta *arsta;
4533 	struct ieee80211_sta *sta;
4534 	struct cfg80211_chan_def def;
4535 	enum nl80211_band band;
4536 	const u8 *ht_mcs_mask;
4537 	const u16 *vht_mcs_mask;
4538 	const u16 *he_mcs_mask;
4539 	u32 changed, bw, nss, smps, bw_prev;
4540 	int err, num_vht_rates, num_he_rates;
4541 	const struct cfg80211_bitrate_mask *mask;
4542 	struct peer_assoc_params peer_arg;
4543 	enum wmi_phy_mode peer_phymode;
4544 
4545 	arsta = container_of(wk, struct ath11k_sta, update_wk);
4546 	sta = container_of((void *)arsta, struct ieee80211_sta, drv_priv);
4547 	arvif = arsta->arvif;
4548 	ar = arvif->ar;
4549 
4550 	if (WARN_ON(ath11k_mac_vif_chan(arvif->vif, &def)))
4551 		return;
4552 
4553 	band = def.chan->band;
4554 	ht_mcs_mask = arvif->bitrate_mask.control[band].ht_mcs;
4555 	vht_mcs_mask = arvif->bitrate_mask.control[band].vht_mcs;
4556 	he_mcs_mask = arvif->bitrate_mask.control[band].he_mcs;
4557 
4558 	spin_lock_bh(&ar->data_lock);
4559 
4560 	changed = arsta->changed;
4561 	arsta->changed = 0;
4562 
4563 	bw = arsta->bw;
4564 	bw_prev = arsta->bw_prev;
4565 	nss = arsta->nss;
4566 	smps = arsta->smps;
4567 
4568 	spin_unlock_bh(&ar->data_lock);
4569 
4570 	mutex_lock(&ar->conf_mutex);
4571 
4572 	nss = max_t(u32, 1, nss);
4573 	nss = min(nss, max(max(ath11k_mac_max_ht_nss(ht_mcs_mask),
4574 			       ath11k_mac_max_vht_nss(vht_mcs_mask)),
4575 			   ath11k_mac_max_he_nss(he_mcs_mask)));
4576 
4577 	if (changed & IEEE80211_RC_BW_CHANGED) {
4578 		/* Get the peer phymode */
4579 		ath11k_peer_assoc_h_phymode(ar, arvif->vif, sta, &peer_arg);
4580 		peer_phymode = peer_arg.peer_phymode;
4581 
4582 		ath11k_dbg(ar->ab, ATH11K_DBG_MAC, "mac update sta %pM peer bw %d phymode %d\n",
4583 			   sta->addr, bw, peer_phymode);
4584 
4585 		if (bw > bw_prev) {
4586 			/* BW is upgraded. In this case we send WMI_PEER_PHYMODE
4587 			 * followed by WMI_PEER_CHWIDTH
4588 			 */
4589 			ath11k_dbg(ar->ab, ATH11K_DBG_MAC, "mac BW upgrade for sta %pM new BW %d, old BW %d\n",
4590 				   sta->addr, bw, bw_prev);
4591 
4592 			err = ath11k_wmi_set_peer_param(ar, sta->addr, arvif->vdev_id,
4593 							WMI_PEER_PHYMODE, peer_phymode);
4594 
4595 			if (err) {
4596 				ath11k_warn(ar->ab, "failed to update STA %pM peer phymode %d: %d\n",
4597 					    sta->addr, peer_phymode, err);
4598 				goto err_rc_bw_changed;
4599 			}
4600 
4601 			err = ath11k_wmi_set_peer_param(ar, sta->addr, arvif->vdev_id,
4602 							WMI_PEER_CHWIDTH, bw);
4603 
4604 			if (err)
4605 				ath11k_warn(ar->ab, "failed to update STA %pM peer bw %d: %d\n",
4606 					    sta->addr, bw, err);
4607 		} else {
4608 			/* BW is downgraded. In this case we send WMI_PEER_CHWIDTH
4609 			 * followed by WMI_PEER_PHYMODE
4610 			 */
4611 			ath11k_dbg(ar->ab, ATH11K_DBG_MAC, "mac BW downgrade for sta %pM new BW %d,old BW %d\n",
4612 				   sta->addr, bw, bw_prev);
4613 
4614 			err = ath11k_wmi_set_peer_param(ar, sta->addr, arvif->vdev_id,
4615 							WMI_PEER_CHWIDTH, bw);
4616 
4617 			if (err) {
4618 				ath11k_warn(ar->ab, "failed to update STA %pM peer bw %d: %d\n",
4619 					    sta->addr, bw, err);
4620 				goto err_rc_bw_changed;
4621 			}
4622 
4623 			err = ath11k_wmi_set_peer_param(ar, sta->addr, arvif->vdev_id,
4624 							WMI_PEER_PHYMODE, peer_phymode);
4625 
4626 			if (err)
4627 				ath11k_warn(ar->ab, "failed to update STA %pM peer phymode %d: %d\n",
4628 					    sta->addr, peer_phymode, err);
4629 		}
4630 	}
4631 
4632 	if (changed & IEEE80211_RC_NSS_CHANGED) {
4633 		ath11k_dbg(ar->ab, ATH11K_DBG_MAC, "mac update sta %pM nss %d\n",
4634 			   sta->addr, nss);
4635 
4636 		err = ath11k_wmi_set_peer_param(ar, sta->addr, arvif->vdev_id,
4637 						WMI_PEER_NSS, nss);
4638 		if (err)
4639 			ath11k_warn(ar->ab, "failed to update STA %pM nss %d: %d\n",
4640 				    sta->addr, nss, err);
4641 	}
4642 
4643 	if (changed & IEEE80211_RC_SMPS_CHANGED) {
4644 		ath11k_dbg(ar->ab, ATH11K_DBG_MAC, "mac update sta %pM smps %d\n",
4645 			   sta->addr, smps);
4646 
4647 		err = ath11k_wmi_set_peer_param(ar, sta->addr, arvif->vdev_id,
4648 						WMI_PEER_MIMO_PS_STATE, smps);
4649 		if (err)
4650 			ath11k_warn(ar->ab, "failed to update STA %pM smps %d: %d\n",
4651 				    sta->addr, smps, err);
4652 	}
4653 
4654 	if (changed & IEEE80211_RC_SUPP_RATES_CHANGED) {
4655 		mask = &arvif->bitrate_mask;
4656 		num_vht_rates = ath11k_mac_bitrate_mask_num_vht_rates(ar, band,
4657 								      mask);
4658 		num_he_rates = ath11k_mac_bitrate_mask_num_he_rates(ar, band,
4659 								    mask);
4660 
4661 		/* Peer_assoc_prepare will reject vht rates in
4662 		 * bitrate_mask if its not available in range format and
4663 		 * sets vht tx_rateset as unsupported. So multiple VHT MCS
4664 		 * setting(eg. MCS 4,5,6) per peer is not supported here.
4665 		 * But, Single rate in VHT mask can be set as per-peer
4666 		 * fixed rate. But even if any HT rates are configured in
4667 		 * the bitrate mask, device will not switch to those rates
4668 		 * when per-peer Fixed rate is set.
4669 		 * TODO: Check RATEMASK_CMDID to support auto rates selection
4670 		 * across HT/VHT and for multiple VHT MCS support.
4671 		 */
4672 		if (sta->deflink.vht_cap.vht_supported && num_vht_rates == 1) {
4673 			ath11k_mac_set_peer_vht_fixed_rate(arvif, sta, mask,
4674 							   band);
4675 		} else if (sta->deflink.he_cap.has_he && num_he_rates == 1) {
4676 			ath11k_mac_set_peer_he_fixed_rate(arvif, sta, mask,
4677 							  band);
4678 		} else {
4679 			/* If the peer is non-VHT/HE or no fixed VHT/HE rate
4680 			 * is provided in the new bitrate mask we set the
4681 			 * other rates using peer_assoc command. Also clear
4682 			 * the peer fixed rate settings as it has higher proprity
4683 			 * than peer assoc
4684 			 */
4685 			err = ath11k_wmi_set_peer_param(ar, sta->addr,
4686 							arvif->vdev_id,
4687 							WMI_PEER_PARAM_FIXED_RATE,
4688 							WMI_FIXED_RATE_NONE);
4689 			if (err)
4690 				ath11k_warn(ar->ab,
4691 					    "failed to disable peer fixed rate for sta %pM: %d\n",
4692 					    sta->addr, err);
4693 
4694 			ath11k_peer_assoc_prepare(ar, arvif->vif, sta,
4695 						  &peer_arg, true);
4696 
4697 			peer_arg.is_assoc = false;
4698 			err = ath11k_wmi_send_peer_assoc_cmd(ar, &peer_arg);
4699 			if (err)
4700 				ath11k_warn(ar->ab, "failed to run peer assoc for STA %pM vdev %i: %d\n",
4701 					    sta->addr, arvif->vdev_id, err);
4702 
4703 			if (!wait_for_completion_timeout(&ar->peer_assoc_done, 1 * HZ))
4704 				ath11k_warn(ar->ab, "failed to get peer assoc conf event for %pM vdev %i\n",
4705 					    sta->addr, arvif->vdev_id);
4706 		}
4707 	}
4708 
4709 err_rc_bw_changed:
4710 	mutex_unlock(&ar->conf_mutex);
4711 }
4712 
4713 static void ath11k_sta_set_4addr_wk(struct work_struct *wk)
4714 {
4715 	struct ath11k *ar;
4716 	struct ath11k_vif *arvif;
4717 	struct ath11k_sta *arsta;
4718 	struct ieee80211_sta *sta;
4719 	int ret = 0;
4720 
4721 	arsta = container_of(wk, struct ath11k_sta, set_4addr_wk);
4722 	sta = container_of((void *)arsta, struct ieee80211_sta, drv_priv);
4723 	arvif = arsta->arvif;
4724 	ar = arvif->ar;
4725 
4726 	ath11k_dbg(ar->ab, ATH11K_DBG_MAC,
4727 		   "setting USE_4ADDR for peer %pM\n", sta->addr);
4728 
4729 	ret = ath11k_wmi_set_peer_param(ar, sta->addr,
4730 					arvif->vdev_id,
4731 					WMI_PEER_USE_4ADDR, 1);
4732 
4733 	if (ret)
4734 		ath11k_warn(ar->ab, "failed to set peer %pM 4addr capability: %d\n",
4735 			    sta->addr, ret);
4736 }
4737 
4738 static int ath11k_mac_inc_num_stations(struct ath11k_vif *arvif,
4739 				       struct ieee80211_sta *sta)
4740 {
4741 	struct ath11k *ar = arvif->ar;
4742 
4743 	lockdep_assert_held(&ar->conf_mutex);
4744 
4745 	if (arvif->vdev_type == WMI_VDEV_TYPE_STA && !sta->tdls)
4746 		return 0;
4747 
4748 	if (ar->num_stations >= ar->max_num_stations)
4749 		return -ENOBUFS;
4750 
4751 	ar->num_stations++;
4752 
4753 	return 0;
4754 }
4755 
4756 static void ath11k_mac_dec_num_stations(struct ath11k_vif *arvif,
4757 					struct ieee80211_sta *sta)
4758 {
4759 	struct ath11k *ar = arvif->ar;
4760 
4761 	lockdep_assert_held(&ar->conf_mutex);
4762 
4763 	if (arvif->vdev_type == WMI_VDEV_TYPE_STA && !sta->tdls)
4764 		return;
4765 
4766 	ar->num_stations--;
4767 }
4768 
4769 static int ath11k_mac_station_add(struct ath11k *ar,
4770 				  struct ieee80211_vif *vif,
4771 				  struct ieee80211_sta *sta)
4772 {
4773 	struct ath11k_base *ab = ar->ab;
4774 	struct ath11k_vif *arvif = ath11k_vif_to_arvif(vif);
4775 	struct ath11k_sta *arsta = (struct ath11k_sta *)sta->drv_priv;
4776 	struct peer_create_params peer_param;
4777 	int ret;
4778 
4779 	lockdep_assert_held(&ar->conf_mutex);
4780 
4781 	ret = ath11k_mac_inc_num_stations(arvif, sta);
4782 	if (ret) {
4783 		ath11k_warn(ab, "refusing to associate station: too many connected already (%d)\n",
4784 			    ar->max_num_stations);
4785 		goto exit;
4786 	}
4787 
4788 	arsta->rx_stats = kzalloc(sizeof(*arsta->rx_stats), GFP_KERNEL);
4789 	if (!arsta->rx_stats) {
4790 		ret = -ENOMEM;
4791 		goto dec_num_station;
4792 	}
4793 
4794 	peer_param.vdev_id = arvif->vdev_id;
4795 	peer_param.peer_addr = sta->addr;
4796 	peer_param.peer_type = WMI_PEER_TYPE_DEFAULT;
4797 
4798 	ret = ath11k_peer_create(ar, arvif, sta, &peer_param);
4799 	if (ret) {
4800 		ath11k_warn(ab, "Failed to add peer: %pM for VDEV: %d\n",
4801 			    sta->addr, arvif->vdev_id);
4802 		goto free_rx_stats;
4803 	}
4804 
4805 	ath11k_dbg(ab, ATH11K_DBG_MAC, "Added peer: %pM for VDEV: %d\n",
4806 		   sta->addr, arvif->vdev_id);
4807 
4808 	if (ath11k_debugfs_is_extd_tx_stats_enabled(ar)) {
4809 		arsta->tx_stats = kzalloc(sizeof(*arsta->tx_stats), GFP_KERNEL);
4810 		if (!arsta->tx_stats) {
4811 			ret = -ENOMEM;
4812 			goto free_peer;
4813 		}
4814 	}
4815 
4816 	if (ieee80211_vif_is_mesh(vif)) {
4817 		ath11k_dbg(ab, ATH11K_DBG_MAC,
4818 			   "setting USE_4ADDR for mesh STA %pM\n", sta->addr);
4819 		ret = ath11k_wmi_set_peer_param(ar, sta->addr,
4820 						arvif->vdev_id,
4821 						WMI_PEER_USE_4ADDR, 1);
4822 		if (ret) {
4823 			ath11k_warn(ab, "failed to set mesh STA %pM 4addr capability: %d\n",
4824 				    sta->addr, ret);
4825 			goto free_tx_stats;
4826 		}
4827 	}
4828 
4829 	ret = ath11k_dp_peer_setup(ar, arvif->vdev_id, sta->addr);
4830 	if (ret) {
4831 		ath11k_warn(ab, "failed to setup dp for peer %pM on vdev %i (%d)\n",
4832 			    sta->addr, arvif->vdev_id, ret);
4833 		goto free_tx_stats;
4834 	}
4835 
4836 	if (ab->hw_params.vdev_start_delay &&
4837 	    !arvif->is_started &&
4838 	    arvif->vdev_type != WMI_VDEV_TYPE_AP) {
4839 		ret = ath11k_start_vdev_delay(ar->hw, vif);
4840 		if (ret) {
4841 			ath11k_warn(ab, "failed to delay vdev start: %d\n", ret);
4842 			goto free_tx_stats;
4843 		}
4844 	}
4845 
4846 	ewma_avg_rssi_init(&arsta->avg_rssi);
4847 	return 0;
4848 
4849 free_tx_stats:
4850 	kfree(arsta->tx_stats);
4851 	arsta->tx_stats = NULL;
4852 free_peer:
4853 	ath11k_peer_delete(ar, arvif->vdev_id, sta->addr);
4854 free_rx_stats:
4855 	kfree(arsta->rx_stats);
4856 	arsta->rx_stats = NULL;
4857 dec_num_station:
4858 	ath11k_mac_dec_num_stations(arvif, sta);
4859 exit:
4860 	return ret;
4861 }
4862 
4863 static u32 ath11k_mac_ieee80211_sta_bw_to_wmi(struct ath11k *ar,
4864 					      struct ieee80211_sta *sta)
4865 {
4866 	u32 bw = WMI_PEER_CHWIDTH_20MHZ;
4867 
4868 	switch (sta->deflink.bandwidth) {
4869 	case IEEE80211_STA_RX_BW_20:
4870 		bw = WMI_PEER_CHWIDTH_20MHZ;
4871 		break;
4872 	case IEEE80211_STA_RX_BW_40:
4873 		bw = WMI_PEER_CHWIDTH_40MHZ;
4874 		break;
4875 	case IEEE80211_STA_RX_BW_80:
4876 		bw = WMI_PEER_CHWIDTH_80MHZ;
4877 		break;
4878 	case IEEE80211_STA_RX_BW_160:
4879 		bw = WMI_PEER_CHWIDTH_160MHZ;
4880 		break;
4881 	default:
4882 		ath11k_warn(ar->ab, "Invalid bandwidth %d for %pM\n",
4883 			    sta->deflink.bandwidth, sta->addr);
4884 		bw = WMI_PEER_CHWIDTH_20MHZ;
4885 		break;
4886 	}
4887 
4888 	return bw;
4889 }
4890 
4891 static int ath11k_mac_op_sta_state(struct ieee80211_hw *hw,
4892 				   struct ieee80211_vif *vif,
4893 				   struct ieee80211_sta *sta,
4894 				   enum ieee80211_sta_state old_state,
4895 				   enum ieee80211_sta_state new_state)
4896 {
4897 	struct ath11k *ar = hw->priv;
4898 	struct ath11k_vif *arvif = ath11k_vif_to_arvif(vif);
4899 	struct ath11k_sta *arsta = (struct ath11k_sta *)sta->drv_priv;
4900 	struct ath11k_peer *peer;
4901 	int ret = 0;
4902 
4903 	/* cancel must be done outside the mutex to avoid deadlock */
4904 	if ((old_state == IEEE80211_STA_NONE &&
4905 	     new_state == IEEE80211_STA_NOTEXIST)) {
4906 		cancel_work_sync(&arsta->update_wk);
4907 		cancel_work_sync(&arsta->set_4addr_wk);
4908 	}
4909 
4910 	mutex_lock(&ar->conf_mutex);
4911 
4912 	if (old_state == IEEE80211_STA_NOTEXIST &&
4913 	    new_state == IEEE80211_STA_NONE) {
4914 		memset(arsta, 0, sizeof(*arsta));
4915 		arsta->arvif = arvif;
4916 		arsta->peer_ps_state = WMI_PEER_PS_STATE_DISABLED;
4917 		INIT_WORK(&arsta->update_wk, ath11k_sta_rc_update_wk);
4918 		INIT_WORK(&arsta->set_4addr_wk, ath11k_sta_set_4addr_wk);
4919 
4920 		ret = ath11k_mac_station_add(ar, vif, sta);
4921 		if (ret)
4922 			ath11k_warn(ar->ab, "Failed to add station: %pM for VDEV: %d\n",
4923 				    sta->addr, arvif->vdev_id);
4924 	} else if ((old_state == IEEE80211_STA_NONE &&
4925 		    new_state == IEEE80211_STA_NOTEXIST)) {
4926 		bool skip_peer_delete = ar->ab->hw_params.vdev_start_delay &&
4927 			vif->type == NL80211_IFTYPE_STATION;
4928 
4929 		ath11k_dp_peer_cleanup(ar, arvif->vdev_id, sta->addr);
4930 
4931 		if (!skip_peer_delete) {
4932 			ret = ath11k_peer_delete(ar, arvif->vdev_id, sta->addr);
4933 			if (ret)
4934 				ath11k_warn(ar->ab,
4935 					    "Failed to delete peer: %pM for VDEV: %d\n",
4936 					    sta->addr, arvif->vdev_id);
4937 			else
4938 				ath11k_dbg(ar->ab,
4939 					   ATH11K_DBG_MAC,
4940 					   "Removed peer: %pM for VDEV: %d\n",
4941 					   sta->addr, arvif->vdev_id);
4942 		}
4943 
4944 		ath11k_mac_dec_num_stations(arvif, sta);
4945 		mutex_lock(&ar->ab->tbl_mtx_lock);
4946 		spin_lock_bh(&ar->ab->base_lock);
4947 		peer = ath11k_peer_find(ar->ab, arvif->vdev_id, sta->addr);
4948 		if (skip_peer_delete && peer) {
4949 			peer->sta = NULL;
4950 		} else if (peer && peer->sta == sta) {
4951 			ath11k_warn(ar->ab, "Found peer entry %pM n vdev %i after it was supposedly removed\n",
4952 				    vif->addr, arvif->vdev_id);
4953 			ath11k_peer_rhash_delete(ar->ab, peer);
4954 			peer->sta = NULL;
4955 			list_del(&peer->list);
4956 			kfree(peer);
4957 			ar->num_peers--;
4958 		}
4959 		spin_unlock_bh(&ar->ab->base_lock);
4960 		mutex_unlock(&ar->ab->tbl_mtx_lock);
4961 
4962 		kfree(arsta->tx_stats);
4963 		arsta->tx_stats = NULL;
4964 
4965 		kfree(arsta->rx_stats);
4966 		arsta->rx_stats = NULL;
4967 	} else if (old_state == IEEE80211_STA_AUTH &&
4968 		   new_state == IEEE80211_STA_ASSOC &&
4969 		   (vif->type == NL80211_IFTYPE_AP ||
4970 		    vif->type == NL80211_IFTYPE_MESH_POINT ||
4971 		    vif->type == NL80211_IFTYPE_ADHOC)) {
4972 		ret = ath11k_station_assoc(ar, vif, sta, false);
4973 		if (ret)
4974 			ath11k_warn(ar->ab, "Failed to associate station: %pM\n",
4975 				    sta->addr);
4976 
4977 		spin_lock_bh(&ar->data_lock);
4978 		/* Set arsta bw and prev bw */
4979 		arsta->bw = ath11k_mac_ieee80211_sta_bw_to_wmi(ar, sta);
4980 		arsta->bw_prev = arsta->bw;
4981 		spin_unlock_bh(&ar->data_lock);
4982 	} else if (old_state == IEEE80211_STA_ASSOC &&
4983 		   new_state == IEEE80211_STA_AUTHORIZED) {
4984 		spin_lock_bh(&ar->ab->base_lock);
4985 
4986 		peer = ath11k_peer_find(ar->ab, arvif->vdev_id, sta->addr);
4987 		if (peer)
4988 			peer->is_authorized = true;
4989 
4990 		spin_unlock_bh(&ar->ab->base_lock);
4991 
4992 		if (vif->type == NL80211_IFTYPE_STATION && arvif->is_up) {
4993 			ret = ath11k_wmi_set_peer_param(ar, sta->addr,
4994 							arvif->vdev_id,
4995 							WMI_PEER_AUTHORIZE,
4996 							1);
4997 			if (ret)
4998 				ath11k_warn(ar->ab, "Unable to authorize peer %pM vdev %d: %d\n",
4999 					    sta->addr, arvif->vdev_id, ret);
5000 		}
5001 	} else if (old_state == IEEE80211_STA_AUTHORIZED &&
5002 		   new_state == IEEE80211_STA_ASSOC) {
5003 		spin_lock_bh(&ar->ab->base_lock);
5004 
5005 		peer = ath11k_peer_find(ar->ab, arvif->vdev_id, sta->addr);
5006 		if (peer)
5007 			peer->is_authorized = false;
5008 
5009 		spin_unlock_bh(&ar->ab->base_lock);
5010 	} else if (old_state == IEEE80211_STA_ASSOC &&
5011 		   new_state == IEEE80211_STA_AUTH &&
5012 		   (vif->type == NL80211_IFTYPE_AP ||
5013 		    vif->type == NL80211_IFTYPE_MESH_POINT ||
5014 		    vif->type == NL80211_IFTYPE_ADHOC)) {
5015 		ret = ath11k_station_disassoc(ar, vif, sta);
5016 		if (ret)
5017 			ath11k_warn(ar->ab, "Failed to disassociate station: %pM\n",
5018 				    sta->addr);
5019 	}
5020 
5021 	mutex_unlock(&ar->conf_mutex);
5022 	return ret;
5023 }
5024 
5025 static int ath11k_mac_op_sta_set_txpwr(struct ieee80211_hw *hw,
5026 				       struct ieee80211_vif *vif,
5027 				       struct ieee80211_sta *sta)
5028 {
5029 	struct ath11k *ar = hw->priv;
5030 	struct ath11k_vif *arvif = (void *)vif->drv_priv;
5031 	int ret = 0;
5032 	s16 txpwr;
5033 
5034 	if (sta->deflink.txpwr.type == NL80211_TX_POWER_AUTOMATIC) {
5035 		txpwr = 0;
5036 	} else {
5037 		txpwr = sta->deflink.txpwr.power;
5038 		if (!txpwr)
5039 			return -EINVAL;
5040 	}
5041 
5042 	if (txpwr > ATH11K_TX_POWER_MAX_VAL || txpwr < ATH11K_TX_POWER_MIN_VAL)
5043 		return -EINVAL;
5044 
5045 	mutex_lock(&ar->conf_mutex);
5046 
5047 	ret = ath11k_wmi_set_peer_param(ar, sta->addr, arvif->vdev_id,
5048 					WMI_PEER_USE_FIXED_PWR, txpwr);
5049 	if (ret) {
5050 		ath11k_warn(ar->ab, "failed to set tx power for station ret: %d\n",
5051 			    ret);
5052 		goto out;
5053 	}
5054 
5055 out:
5056 	mutex_unlock(&ar->conf_mutex);
5057 	return ret;
5058 }
5059 
5060 static void ath11k_mac_op_sta_set_4addr(struct ieee80211_hw *hw,
5061 					struct ieee80211_vif *vif,
5062 					struct ieee80211_sta *sta, bool enabled)
5063 {
5064 	struct ath11k *ar = hw->priv;
5065 	struct ath11k_sta *arsta = (struct ath11k_sta *)sta->drv_priv;
5066 
5067 	if (enabled && !arsta->use_4addr_set) {
5068 		ieee80211_queue_work(ar->hw, &arsta->set_4addr_wk);
5069 		arsta->use_4addr_set = true;
5070 	}
5071 }
5072 
5073 static void ath11k_mac_op_sta_rc_update(struct ieee80211_hw *hw,
5074 					struct ieee80211_vif *vif,
5075 					struct ieee80211_sta *sta,
5076 					u32 changed)
5077 {
5078 	struct ath11k *ar = hw->priv;
5079 	struct ath11k_sta *arsta = (struct ath11k_sta *)sta->drv_priv;
5080 	struct ath11k_vif *arvif = (void *)vif->drv_priv;
5081 	struct ath11k_peer *peer;
5082 	u32 bw, smps;
5083 
5084 	spin_lock_bh(&ar->ab->base_lock);
5085 
5086 	peer = ath11k_peer_find(ar->ab, arvif->vdev_id, sta->addr);
5087 	if (!peer) {
5088 		spin_unlock_bh(&ar->ab->base_lock);
5089 		ath11k_warn(ar->ab, "mac sta rc update failed to find peer %pM on vdev %i\n",
5090 			    sta->addr, arvif->vdev_id);
5091 		return;
5092 	}
5093 
5094 	spin_unlock_bh(&ar->ab->base_lock);
5095 
5096 	ath11k_dbg(ar->ab, ATH11K_DBG_MAC,
5097 		   "mac sta rc update for %pM changed %08x bw %d nss %d smps %d\n",
5098 		   sta->addr, changed, sta->deflink.bandwidth,
5099 		   sta->deflink.rx_nss,
5100 		   sta->deflink.smps_mode);
5101 
5102 	spin_lock_bh(&ar->data_lock);
5103 
5104 	if (changed & IEEE80211_RC_BW_CHANGED) {
5105 		bw = ath11k_mac_ieee80211_sta_bw_to_wmi(ar, sta);
5106 		arsta->bw_prev = arsta->bw;
5107 		arsta->bw = bw;
5108 	}
5109 
5110 	if (changed & IEEE80211_RC_NSS_CHANGED)
5111 		arsta->nss = sta->deflink.rx_nss;
5112 
5113 	if (changed & IEEE80211_RC_SMPS_CHANGED) {
5114 		smps = WMI_PEER_SMPS_PS_NONE;
5115 
5116 		switch (sta->deflink.smps_mode) {
5117 		case IEEE80211_SMPS_AUTOMATIC:
5118 		case IEEE80211_SMPS_OFF:
5119 			smps = WMI_PEER_SMPS_PS_NONE;
5120 			break;
5121 		case IEEE80211_SMPS_STATIC:
5122 			smps = WMI_PEER_SMPS_STATIC;
5123 			break;
5124 		case IEEE80211_SMPS_DYNAMIC:
5125 			smps = WMI_PEER_SMPS_DYNAMIC;
5126 			break;
5127 		default:
5128 			ath11k_warn(ar->ab, "Invalid smps %d in sta rc update for %pM\n",
5129 				    sta->deflink.smps_mode, sta->addr);
5130 			smps = WMI_PEER_SMPS_PS_NONE;
5131 			break;
5132 		}
5133 
5134 		arsta->smps = smps;
5135 	}
5136 
5137 	arsta->changed |= changed;
5138 
5139 	spin_unlock_bh(&ar->data_lock);
5140 
5141 	ieee80211_queue_work(hw, &arsta->update_wk);
5142 }
5143 
5144 static int ath11k_conf_tx_uapsd(struct ath11k *ar, struct ieee80211_vif *vif,
5145 				u16 ac, bool enable)
5146 {
5147 	struct ath11k_vif *arvif = ath11k_vif_to_arvif(vif);
5148 	u32 value = 0;
5149 	int ret = 0;
5150 
5151 	if (arvif->vdev_type != WMI_VDEV_TYPE_STA)
5152 		return 0;
5153 
5154 	switch (ac) {
5155 	case IEEE80211_AC_VO:
5156 		value = WMI_STA_PS_UAPSD_AC3_DELIVERY_EN |
5157 			WMI_STA_PS_UAPSD_AC3_TRIGGER_EN;
5158 		break;
5159 	case IEEE80211_AC_VI:
5160 		value = WMI_STA_PS_UAPSD_AC2_DELIVERY_EN |
5161 			WMI_STA_PS_UAPSD_AC2_TRIGGER_EN;
5162 		break;
5163 	case IEEE80211_AC_BE:
5164 		value = WMI_STA_PS_UAPSD_AC1_DELIVERY_EN |
5165 			WMI_STA_PS_UAPSD_AC1_TRIGGER_EN;
5166 		break;
5167 	case IEEE80211_AC_BK:
5168 		value = WMI_STA_PS_UAPSD_AC0_DELIVERY_EN |
5169 			WMI_STA_PS_UAPSD_AC0_TRIGGER_EN;
5170 		break;
5171 	}
5172 
5173 	if (enable)
5174 		arvif->u.sta.uapsd |= value;
5175 	else
5176 		arvif->u.sta.uapsd &= ~value;
5177 
5178 	ret = ath11k_wmi_set_sta_ps_param(ar, arvif->vdev_id,
5179 					  WMI_STA_PS_PARAM_UAPSD,
5180 					  arvif->u.sta.uapsd);
5181 	if (ret) {
5182 		ath11k_warn(ar->ab, "could not set uapsd params %d\n", ret);
5183 		goto exit;
5184 	}
5185 
5186 	if (arvif->u.sta.uapsd)
5187 		value = WMI_STA_PS_RX_WAKE_POLICY_POLL_UAPSD;
5188 	else
5189 		value = WMI_STA_PS_RX_WAKE_POLICY_WAKE;
5190 
5191 	ret = ath11k_wmi_set_sta_ps_param(ar, arvif->vdev_id,
5192 					  WMI_STA_PS_PARAM_RX_WAKE_POLICY,
5193 					  value);
5194 	if (ret)
5195 		ath11k_warn(ar->ab, "could not set rx wake param %d\n", ret);
5196 
5197 exit:
5198 	return ret;
5199 }
5200 
5201 static int ath11k_mac_op_conf_tx(struct ieee80211_hw *hw,
5202 				 struct ieee80211_vif *vif,
5203 				 unsigned int link_id, u16 ac,
5204 				 const struct ieee80211_tx_queue_params *params)
5205 {
5206 	struct ath11k *ar = hw->priv;
5207 	struct ath11k_vif *arvif = (void *)vif->drv_priv;
5208 	struct wmi_wmm_params_arg *p = NULL;
5209 	int ret;
5210 
5211 	mutex_lock(&ar->conf_mutex);
5212 
5213 	switch (ac) {
5214 	case IEEE80211_AC_VO:
5215 		p = &arvif->wmm_params.ac_vo;
5216 		break;
5217 	case IEEE80211_AC_VI:
5218 		p = &arvif->wmm_params.ac_vi;
5219 		break;
5220 	case IEEE80211_AC_BE:
5221 		p = &arvif->wmm_params.ac_be;
5222 		break;
5223 	case IEEE80211_AC_BK:
5224 		p = &arvif->wmm_params.ac_bk;
5225 		break;
5226 	}
5227 
5228 	if (WARN_ON(!p)) {
5229 		ret = -EINVAL;
5230 		goto exit;
5231 	}
5232 
5233 	p->cwmin = params->cw_min;
5234 	p->cwmax = params->cw_max;
5235 	p->aifs = params->aifs;
5236 	p->txop = params->txop;
5237 
5238 	ret = ath11k_wmi_send_wmm_update_cmd_tlv(ar, arvif->vdev_id,
5239 						 &arvif->wmm_params);
5240 	if (ret) {
5241 		ath11k_warn(ar->ab, "failed to set wmm params: %d\n", ret);
5242 		goto exit;
5243 	}
5244 
5245 	ret = ath11k_conf_tx_uapsd(ar, vif, ac, params->uapsd);
5246 
5247 	if (ret)
5248 		ath11k_warn(ar->ab, "failed to set sta uapsd: %d\n", ret);
5249 
5250 exit:
5251 	mutex_unlock(&ar->conf_mutex);
5252 	return ret;
5253 }
5254 
5255 static struct ieee80211_sta_ht_cap
5256 ath11k_create_ht_cap(struct ath11k *ar, u32 ar_ht_cap, u32 rate_cap_rx_chainmask)
5257 {
5258 	int i;
5259 	struct ieee80211_sta_ht_cap ht_cap = {0};
5260 	u32 ar_vht_cap = ar->pdev->cap.vht_cap;
5261 
5262 	if (!(ar_ht_cap & WMI_HT_CAP_ENABLED))
5263 		return ht_cap;
5264 
5265 	ht_cap.ht_supported = 1;
5266 	ht_cap.ampdu_factor = IEEE80211_HT_MAX_AMPDU_64K;
5267 	ht_cap.ampdu_density = IEEE80211_HT_MPDU_DENSITY_NONE;
5268 	ht_cap.cap |= IEEE80211_HT_CAP_SUP_WIDTH_20_40;
5269 	ht_cap.cap |= IEEE80211_HT_CAP_DSSSCCK40;
5270 	ht_cap.cap |= WLAN_HT_CAP_SM_PS_STATIC << IEEE80211_HT_CAP_SM_PS_SHIFT;
5271 
5272 	if (ar_ht_cap & WMI_HT_CAP_HT20_SGI)
5273 		ht_cap.cap |= IEEE80211_HT_CAP_SGI_20;
5274 
5275 	if (ar_ht_cap & WMI_HT_CAP_HT40_SGI)
5276 		ht_cap.cap |= IEEE80211_HT_CAP_SGI_40;
5277 
5278 	if (ar_ht_cap & WMI_HT_CAP_DYNAMIC_SMPS) {
5279 		u32 smps;
5280 
5281 		smps   = WLAN_HT_CAP_SM_PS_DYNAMIC;
5282 		smps <<= IEEE80211_HT_CAP_SM_PS_SHIFT;
5283 
5284 		ht_cap.cap |= smps;
5285 	}
5286 
5287 	if (ar_ht_cap & WMI_HT_CAP_TX_STBC)
5288 		ht_cap.cap |= IEEE80211_HT_CAP_TX_STBC;
5289 
5290 	if (ar_ht_cap & WMI_HT_CAP_RX_STBC) {
5291 		u32 stbc;
5292 
5293 		stbc   = ar_ht_cap;
5294 		stbc  &= WMI_HT_CAP_RX_STBC;
5295 		stbc >>= WMI_HT_CAP_RX_STBC_MASK_SHIFT;
5296 		stbc <<= IEEE80211_HT_CAP_RX_STBC_SHIFT;
5297 		stbc  &= IEEE80211_HT_CAP_RX_STBC;
5298 
5299 		ht_cap.cap |= stbc;
5300 	}
5301 
5302 	if (ar_ht_cap & WMI_HT_CAP_RX_LDPC)
5303 		ht_cap.cap |= IEEE80211_HT_CAP_LDPC_CODING;
5304 
5305 	if (ar_ht_cap & WMI_HT_CAP_L_SIG_TXOP_PROT)
5306 		ht_cap.cap |= IEEE80211_HT_CAP_LSIG_TXOP_PROT;
5307 
5308 	if (ar_vht_cap & WMI_VHT_CAP_MAX_MPDU_LEN_MASK)
5309 		ht_cap.cap |= IEEE80211_HT_CAP_MAX_AMSDU;
5310 
5311 	for (i = 0; i < ar->num_rx_chains; i++) {
5312 		if (rate_cap_rx_chainmask & BIT(i))
5313 			ht_cap.mcs.rx_mask[i] = 0xFF;
5314 	}
5315 
5316 	ht_cap.mcs.tx_params |= IEEE80211_HT_MCS_TX_DEFINED;
5317 
5318 	return ht_cap;
5319 }
5320 
5321 static int ath11k_mac_set_txbf_conf(struct ath11k_vif *arvif)
5322 {
5323 	u32 value = 0;
5324 	struct ath11k *ar = arvif->ar;
5325 	int nsts;
5326 	int sound_dim;
5327 	u32 vht_cap = ar->pdev->cap.vht_cap;
5328 	u32 vdev_param = WMI_VDEV_PARAM_TXBF;
5329 
5330 	if (vht_cap & (IEEE80211_VHT_CAP_SU_BEAMFORMEE_CAPABLE)) {
5331 		nsts = vht_cap & IEEE80211_VHT_CAP_BEAMFORMEE_STS_MASK;
5332 		nsts >>= IEEE80211_VHT_CAP_BEAMFORMEE_STS_SHIFT;
5333 		if (nsts > (ar->num_rx_chains - 1))
5334 			nsts = ar->num_rx_chains - 1;
5335 		value |= SM(nsts, WMI_TXBF_STS_CAP_OFFSET);
5336 	}
5337 
5338 	if (vht_cap & (IEEE80211_VHT_CAP_SU_BEAMFORMER_CAPABLE)) {
5339 		sound_dim = vht_cap &
5340 			    IEEE80211_VHT_CAP_SOUNDING_DIMENSIONS_MASK;
5341 		sound_dim >>= IEEE80211_VHT_CAP_SOUNDING_DIMENSIONS_SHIFT;
5342 		if (sound_dim > (ar->num_tx_chains - 1))
5343 			sound_dim = ar->num_tx_chains - 1;
5344 		value |= SM(sound_dim, WMI_BF_SOUND_DIM_OFFSET);
5345 	}
5346 
5347 	if (!value)
5348 		return 0;
5349 
5350 	if (vht_cap & IEEE80211_VHT_CAP_SU_BEAMFORMER_CAPABLE) {
5351 		value |= WMI_VDEV_PARAM_TXBF_SU_TX_BFER;
5352 
5353 		if ((vht_cap & IEEE80211_VHT_CAP_MU_BEAMFORMER_CAPABLE) &&
5354 		    arvif->vdev_type == WMI_VDEV_TYPE_AP)
5355 			value |= WMI_VDEV_PARAM_TXBF_MU_TX_BFER;
5356 	}
5357 
5358 	/* TODO: SUBFEE not validated in HK, disable here until validated? */
5359 
5360 	if (vht_cap & IEEE80211_VHT_CAP_SU_BEAMFORMEE_CAPABLE) {
5361 		value |= WMI_VDEV_PARAM_TXBF_SU_TX_BFEE;
5362 
5363 		if ((vht_cap & IEEE80211_VHT_CAP_MU_BEAMFORMEE_CAPABLE) &&
5364 		    arvif->vdev_type == WMI_VDEV_TYPE_STA)
5365 			value |= WMI_VDEV_PARAM_TXBF_MU_TX_BFEE;
5366 	}
5367 
5368 	return ath11k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
5369 					     vdev_param, value);
5370 }
5371 
5372 static void ath11k_set_vht_txbf_cap(struct ath11k *ar, u32 *vht_cap)
5373 {
5374 	bool subfer, subfee;
5375 	int sound_dim = 0, nsts = 0;
5376 
5377 	subfer = !!(*vht_cap & (IEEE80211_VHT_CAP_SU_BEAMFORMER_CAPABLE));
5378 	subfee = !!(*vht_cap & (IEEE80211_VHT_CAP_SU_BEAMFORMEE_CAPABLE));
5379 
5380 	if (ar->num_tx_chains < 2) {
5381 		*vht_cap &= ~(IEEE80211_VHT_CAP_SU_BEAMFORMER_CAPABLE);
5382 		subfer = false;
5383 	}
5384 
5385 	if (ar->num_rx_chains < 2) {
5386 		*vht_cap &= ~(IEEE80211_VHT_CAP_SU_BEAMFORMEE_CAPABLE);
5387 		subfee = false;
5388 	}
5389 
5390 	/* If SU Beaformer is not set, then disable MU Beamformer Capability */
5391 	if (!subfer)
5392 		*vht_cap &= ~(IEEE80211_VHT_CAP_MU_BEAMFORMER_CAPABLE);
5393 
5394 	/* If SU Beaformee is not set, then disable MU Beamformee Capability */
5395 	if (!subfee)
5396 		*vht_cap &= ~(IEEE80211_VHT_CAP_MU_BEAMFORMEE_CAPABLE);
5397 
5398 	sound_dim = (*vht_cap & IEEE80211_VHT_CAP_SOUNDING_DIMENSIONS_MASK);
5399 	sound_dim >>= IEEE80211_VHT_CAP_SOUNDING_DIMENSIONS_SHIFT;
5400 	*vht_cap &= ~IEEE80211_VHT_CAP_SOUNDING_DIMENSIONS_MASK;
5401 
5402 	nsts = (*vht_cap & IEEE80211_VHT_CAP_BEAMFORMEE_STS_MASK);
5403 	nsts >>= IEEE80211_VHT_CAP_BEAMFORMEE_STS_SHIFT;
5404 	*vht_cap &= ~IEEE80211_VHT_CAP_BEAMFORMEE_STS_MASK;
5405 
5406 	/* Enable Sounding Dimension Field only if SU BF is enabled */
5407 	if (subfer) {
5408 		if (sound_dim > (ar->num_tx_chains - 1))
5409 			sound_dim = ar->num_tx_chains - 1;
5410 
5411 		sound_dim <<= IEEE80211_VHT_CAP_SOUNDING_DIMENSIONS_SHIFT;
5412 		sound_dim &=  IEEE80211_VHT_CAP_SOUNDING_DIMENSIONS_MASK;
5413 		*vht_cap |= sound_dim;
5414 	}
5415 
5416 	/* Enable Beamformee STS Field only if SU BF is enabled */
5417 	if (subfee) {
5418 		if (nsts > (ar->num_rx_chains - 1))
5419 			nsts = ar->num_rx_chains - 1;
5420 
5421 		nsts <<= IEEE80211_VHT_CAP_BEAMFORMEE_STS_SHIFT;
5422 		nsts &=  IEEE80211_VHT_CAP_BEAMFORMEE_STS_MASK;
5423 		*vht_cap |= nsts;
5424 	}
5425 }
5426 
5427 static struct ieee80211_sta_vht_cap
5428 ath11k_create_vht_cap(struct ath11k *ar, u32 rate_cap_tx_chainmask,
5429 		      u32 rate_cap_rx_chainmask)
5430 {
5431 	struct ieee80211_sta_vht_cap vht_cap = {0};
5432 	u16 txmcs_map, rxmcs_map;
5433 	int i;
5434 
5435 	vht_cap.vht_supported = 1;
5436 	vht_cap.cap = ar->pdev->cap.vht_cap;
5437 
5438 	if (ar->pdev->cap.nss_ratio_enabled)
5439 		vht_cap.vht_mcs.tx_highest |=
5440 			cpu_to_le16(IEEE80211_VHT_EXT_NSS_BW_CAPABLE);
5441 
5442 	ath11k_set_vht_txbf_cap(ar, &vht_cap.cap);
5443 
5444 	rxmcs_map = 0;
5445 	txmcs_map = 0;
5446 	for (i = 0; i < 8; i++) {
5447 		if (i < ar->num_tx_chains && rate_cap_tx_chainmask & BIT(i))
5448 			txmcs_map |= IEEE80211_VHT_MCS_SUPPORT_0_9 << (i * 2);
5449 		else
5450 			txmcs_map |= IEEE80211_VHT_MCS_NOT_SUPPORTED << (i * 2);
5451 
5452 		if (i < ar->num_rx_chains && rate_cap_rx_chainmask & BIT(i))
5453 			rxmcs_map |= IEEE80211_VHT_MCS_SUPPORT_0_9 << (i * 2);
5454 		else
5455 			rxmcs_map |= IEEE80211_VHT_MCS_NOT_SUPPORTED << (i * 2);
5456 	}
5457 
5458 	if (rate_cap_tx_chainmask <= 1)
5459 		vht_cap.cap &= ~IEEE80211_VHT_CAP_TXSTBC;
5460 
5461 	vht_cap.vht_mcs.rx_mcs_map = cpu_to_le16(rxmcs_map);
5462 	vht_cap.vht_mcs.tx_mcs_map = cpu_to_le16(txmcs_map);
5463 
5464 	return vht_cap;
5465 }
5466 
5467 static void ath11k_mac_setup_ht_vht_cap(struct ath11k *ar,
5468 					struct ath11k_pdev_cap *cap,
5469 					u32 *ht_cap_info)
5470 {
5471 	struct ieee80211_supported_band *band;
5472 	u32 rate_cap_tx_chainmask;
5473 	u32 rate_cap_rx_chainmask;
5474 	u32 ht_cap;
5475 
5476 	rate_cap_tx_chainmask = ar->cfg_tx_chainmask >> cap->tx_chain_mask_shift;
5477 	rate_cap_rx_chainmask = ar->cfg_rx_chainmask >> cap->rx_chain_mask_shift;
5478 
5479 	if (cap->supported_bands & WMI_HOST_WLAN_2G_CAP) {
5480 		band = &ar->mac.sbands[NL80211_BAND_2GHZ];
5481 		ht_cap = cap->band[NL80211_BAND_2GHZ].ht_cap_info;
5482 		if (ht_cap_info)
5483 			*ht_cap_info = ht_cap;
5484 		band->ht_cap = ath11k_create_ht_cap(ar, ht_cap,
5485 						    rate_cap_rx_chainmask);
5486 	}
5487 
5488 	if (cap->supported_bands & WMI_HOST_WLAN_5G_CAP &&
5489 	    (ar->ab->hw_params.single_pdev_only ||
5490 	     !ar->supports_6ghz)) {
5491 		band = &ar->mac.sbands[NL80211_BAND_5GHZ];
5492 		ht_cap = cap->band[NL80211_BAND_5GHZ].ht_cap_info;
5493 		if (ht_cap_info)
5494 			*ht_cap_info = ht_cap;
5495 		band->ht_cap = ath11k_create_ht_cap(ar, ht_cap,
5496 						    rate_cap_rx_chainmask);
5497 		band->vht_cap = ath11k_create_vht_cap(ar, rate_cap_tx_chainmask,
5498 						      rate_cap_rx_chainmask);
5499 	}
5500 }
5501 
5502 static int ath11k_check_chain_mask(struct ath11k *ar, u32 ant, bool is_tx_ant)
5503 {
5504 	/* TODO: Check the request chainmask against the supported
5505 	 * chainmask table which is advertised in extented_service_ready event
5506 	 */
5507 
5508 	return 0;
5509 }
5510 
5511 static void ath11k_gen_ppe_thresh(struct ath11k_ppe_threshold *fw_ppet,
5512 				  u8 *he_ppet)
5513 {
5514 	int nss, ru;
5515 	u8 bit = 7;
5516 
5517 	he_ppet[0] = fw_ppet->numss_m1 & IEEE80211_PPE_THRES_NSS_MASK;
5518 	he_ppet[0] |= (fw_ppet->ru_bit_mask <<
5519 		       IEEE80211_PPE_THRES_RU_INDEX_BITMASK_POS) &
5520 		      IEEE80211_PPE_THRES_RU_INDEX_BITMASK_MASK;
5521 	for (nss = 0; nss <= fw_ppet->numss_m1; nss++) {
5522 		for (ru = 0; ru < 4; ru++) {
5523 			u8 val;
5524 			int i;
5525 
5526 			if ((fw_ppet->ru_bit_mask & BIT(ru)) == 0)
5527 				continue;
5528 			val = (fw_ppet->ppet16_ppet8_ru3_ru0[nss] >> (ru * 6)) &
5529 			       0x3f;
5530 			val = ((val >> 3) & 0x7) | ((val & 0x7) << 3);
5531 			for (i = 5; i >= 0; i--) {
5532 				he_ppet[bit / 8] |=
5533 					((val >> i) & 0x1) << ((bit % 8));
5534 				bit++;
5535 			}
5536 		}
5537 	}
5538 }
5539 
5540 static void
5541 ath11k_mac_filter_he_cap_mesh(struct ieee80211_he_cap_elem *he_cap_elem)
5542 {
5543 	u8 m;
5544 
5545 	m = IEEE80211_HE_MAC_CAP0_TWT_RES |
5546 	    IEEE80211_HE_MAC_CAP0_TWT_REQ;
5547 	he_cap_elem->mac_cap_info[0] &= ~m;
5548 
5549 	m = IEEE80211_HE_MAC_CAP2_TRS |
5550 	    IEEE80211_HE_MAC_CAP2_BCAST_TWT |
5551 	    IEEE80211_HE_MAC_CAP2_MU_CASCADING;
5552 	he_cap_elem->mac_cap_info[2] &= ~m;
5553 
5554 	m = IEEE80211_HE_MAC_CAP3_FLEX_TWT_SCHED |
5555 	    IEEE80211_HE_MAC_CAP2_BCAST_TWT |
5556 	    IEEE80211_HE_MAC_CAP2_MU_CASCADING;
5557 	he_cap_elem->mac_cap_info[3] &= ~m;
5558 
5559 	m = IEEE80211_HE_MAC_CAP4_BSRP_BQRP_A_MPDU_AGG |
5560 	    IEEE80211_HE_MAC_CAP4_BQR;
5561 	he_cap_elem->mac_cap_info[4] &= ~m;
5562 
5563 	m = IEEE80211_HE_MAC_CAP5_SUBCHAN_SELECTIVE_TRANSMISSION |
5564 	    IEEE80211_HE_MAC_CAP5_UL_2x996_TONE_RU |
5565 	    IEEE80211_HE_MAC_CAP5_PUNCTURED_SOUNDING |
5566 	    IEEE80211_HE_MAC_CAP5_HT_VHT_TRIG_FRAME_RX;
5567 	he_cap_elem->mac_cap_info[5] &= ~m;
5568 
5569 	m = IEEE80211_HE_PHY_CAP2_UL_MU_FULL_MU_MIMO |
5570 	    IEEE80211_HE_PHY_CAP2_UL_MU_PARTIAL_MU_MIMO;
5571 	he_cap_elem->phy_cap_info[2] &= ~m;
5572 
5573 	m = IEEE80211_HE_PHY_CAP3_RX_PARTIAL_BW_SU_IN_20MHZ_MU |
5574 	    IEEE80211_HE_PHY_CAP3_DCM_MAX_CONST_TX_MASK |
5575 	    IEEE80211_HE_PHY_CAP3_DCM_MAX_CONST_RX_MASK;
5576 	he_cap_elem->phy_cap_info[3] &= ~m;
5577 
5578 	m = IEEE80211_HE_PHY_CAP4_MU_BEAMFORMER;
5579 	he_cap_elem->phy_cap_info[4] &= ~m;
5580 
5581 	m = IEEE80211_HE_PHY_CAP5_NG16_MU_FEEDBACK;
5582 	he_cap_elem->phy_cap_info[5] &= ~m;
5583 
5584 	m = IEEE80211_HE_PHY_CAP6_CODEBOOK_SIZE_75_MU |
5585 	    IEEE80211_HE_PHY_CAP6_TRIG_MU_BEAMFORMING_PARTIAL_BW_FB |
5586 	    IEEE80211_HE_PHY_CAP6_TRIG_CQI_FB |
5587 	    IEEE80211_HE_PHY_CAP6_PARTIAL_BANDWIDTH_DL_MUMIMO;
5588 	he_cap_elem->phy_cap_info[6] &= ~m;
5589 
5590 	m = IEEE80211_HE_PHY_CAP7_PSR_BASED_SR |
5591 	    IEEE80211_HE_PHY_CAP7_POWER_BOOST_FACTOR_SUPP |
5592 	    IEEE80211_HE_PHY_CAP7_STBC_TX_ABOVE_80MHZ |
5593 	    IEEE80211_HE_PHY_CAP7_STBC_RX_ABOVE_80MHZ;
5594 	he_cap_elem->phy_cap_info[7] &= ~m;
5595 
5596 	m = IEEE80211_HE_PHY_CAP8_HE_ER_SU_PPDU_4XLTF_AND_08_US_GI |
5597 	    IEEE80211_HE_PHY_CAP8_20MHZ_IN_40MHZ_HE_PPDU_IN_2G |
5598 	    IEEE80211_HE_PHY_CAP8_20MHZ_IN_160MHZ_HE_PPDU |
5599 	    IEEE80211_HE_PHY_CAP8_80MHZ_IN_160MHZ_HE_PPDU;
5600 	he_cap_elem->phy_cap_info[8] &= ~m;
5601 
5602 	m = IEEE80211_HE_PHY_CAP9_LONGER_THAN_16_SIGB_OFDM_SYM |
5603 	    IEEE80211_HE_PHY_CAP9_NON_TRIGGERED_CQI_FEEDBACK |
5604 	    IEEE80211_HE_PHY_CAP9_RX_1024_QAM_LESS_THAN_242_TONE_RU |
5605 	    IEEE80211_HE_PHY_CAP9_TX_1024_QAM_LESS_THAN_242_TONE_RU |
5606 	    IEEE80211_HE_PHY_CAP9_RX_FULL_BW_SU_USING_MU_WITH_COMP_SIGB |
5607 	    IEEE80211_HE_PHY_CAP9_RX_FULL_BW_SU_USING_MU_WITH_NON_COMP_SIGB;
5608 	he_cap_elem->phy_cap_info[9] &= ~m;
5609 }
5610 
5611 static __le16 ath11k_mac_setup_he_6ghz_cap(struct ath11k_pdev_cap *pcap,
5612 					   struct ath11k_band_cap *bcap)
5613 {
5614 	u8 val;
5615 
5616 	bcap->he_6ghz_capa = IEEE80211_HT_MPDU_DENSITY_NONE;
5617 	if (bcap->ht_cap_info & WMI_HT_CAP_DYNAMIC_SMPS)
5618 		bcap->he_6ghz_capa |=
5619 			FIELD_PREP(IEEE80211_HE_6GHZ_CAP_SM_PS,
5620 				   WLAN_HT_CAP_SM_PS_DYNAMIC);
5621 	else
5622 		bcap->he_6ghz_capa |=
5623 			FIELD_PREP(IEEE80211_HE_6GHZ_CAP_SM_PS,
5624 				   WLAN_HT_CAP_SM_PS_DISABLED);
5625 	val = FIELD_GET(IEEE80211_VHT_CAP_MAX_A_MPDU_LENGTH_EXPONENT_MASK,
5626 			pcap->vht_cap);
5627 	bcap->he_6ghz_capa |=
5628 		FIELD_PREP(IEEE80211_HE_6GHZ_CAP_MAX_AMPDU_LEN_EXP, val);
5629 	val = FIELD_GET(IEEE80211_VHT_CAP_MAX_MPDU_MASK, pcap->vht_cap);
5630 	bcap->he_6ghz_capa |=
5631 		FIELD_PREP(IEEE80211_HE_6GHZ_CAP_MAX_MPDU_LEN, val);
5632 	if (pcap->vht_cap & IEEE80211_VHT_CAP_RX_ANTENNA_PATTERN)
5633 		bcap->he_6ghz_capa |= IEEE80211_HE_6GHZ_CAP_RX_ANTPAT_CONS;
5634 	if (pcap->vht_cap & IEEE80211_VHT_CAP_TX_ANTENNA_PATTERN)
5635 		bcap->he_6ghz_capa |= IEEE80211_HE_6GHZ_CAP_TX_ANTPAT_CONS;
5636 
5637 	return cpu_to_le16(bcap->he_6ghz_capa);
5638 }
5639 
5640 static void ath11k_mac_set_hemcsmap(struct ath11k *ar,
5641 				    struct ath11k_pdev_cap *cap,
5642 				    struct ieee80211_sta_he_cap *he_cap,
5643 				    int band)
5644 {
5645 	u16 txmcs_map, rxmcs_map;
5646 	u32 i;
5647 
5648 	rxmcs_map = 0;
5649 	txmcs_map = 0;
5650 	for (i = 0; i < 8; i++) {
5651 		if (i < ar->num_tx_chains &&
5652 		    (ar->cfg_tx_chainmask >> cap->tx_chain_mask_shift) & BIT(i))
5653 			txmcs_map |= IEEE80211_HE_MCS_SUPPORT_0_11 << (i * 2);
5654 		else
5655 			txmcs_map |= IEEE80211_HE_MCS_NOT_SUPPORTED << (i * 2);
5656 
5657 		if (i < ar->num_rx_chains &&
5658 		    (ar->cfg_rx_chainmask >> cap->tx_chain_mask_shift) & BIT(i))
5659 			rxmcs_map |= IEEE80211_HE_MCS_SUPPORT_0_11 << (i * 2);
5660 		else
5661 			rxmcs_map |= IEEE80211_HE_MCS_NOT_SUPPORTED << (i * 2);
5662 	}
5663 	he_cap->he_mcs_nss_supp.rx_mcs_80 =
5664 		cpu_to_le16(rxmcs_map & 0xffff);
5665 	he_cap->he_mcs_nss_supp.tx_mcs_80 =
5666 		cpu_to_le16(txmcs_map & 0xffff);
5667 	he_cap->he_mcs_nss_supp.rx_mcs_160 =
5668 		cpu_to_le16(rxmcs_map & 0xffff);
5669 	he_cap->he_mcs_nss_supp.tx_mcs_160 =
5670 		cpu_to_le16(txmcs_map & 0xffff);
5671 	he_cap->he_mcs_nss_supp.rx_mcs_80p80 =
5672 		cpu_to_le16(rxmcs_map & 0xffff);
5673 	he_cap->he_mcs_nss_supp.tx_mcs_80p80 =
5674 		cpu_to_le16(txmcs_map & 0xffff);
5675 }
5676 
5677 static int ath11k_mac_copy_he_cap(struct ath11k *ar,
5678 				  struct ath11k_pdev_cap *cap,
5679 				  struct ieee80211_sband_iftype_data *data,
5680 				  int band)
5681 {
5682 	int i, idx = 0;
5683 
5684 	for (i = 0; i < NUM_NL80211_IFTYPES; i++) {
5685 		struct ieee80211_sta_he_cap *he_cap = &data[idx].he_cap;
5686 		struct ath11k_band_cap *band_cap = &cap->band[band];
5687 		struct ieee80211_he_cap_elem *he_cap_elem =
5688 				&he_cap->he_cap_elem;
5689 
5690 		switch (i) {
5691 		case NL80211_IFTYPE_STATION:
5692 		case NL80211_IFTYPE_AP:
5693 		case NL80211_IFTYPE_MESH_POINT:
5694 			break;
5695 
5696 		default:
5697 			continue;
5698 		}
5699 
5700 		data[idx].types_mask = BIT(i);
5701 		he_cap->has_he = true;
5702 		memcpy(he_cap_elem->mac_cap_info, band_cap->he_cap_info,
5703 		       sizeof(he_cap_elem->mac_cap_info));
5704 		memcpy(he_cap_elem->phy_cap_info, band_cap->he_cap_phy_info,
5705 		       sizeof(he_cap_elem->phy_cap_info));
5706 
5707 		he_cap_elem->mac_cap_info[1] &=
5708 			IEEE80211_HE_MAC_CAP1_TF_MAC_PAD_DUR_MASK;
5709 
5710 		he_cap_elem->phy_cap_info[5] &=
5711 			~IEEE80211_HE_PHY_CAP5_BEAMFORMEE_NUM_SND_DIM_UNDER_80MHZ_MASK;
5712 		he_cap_elem->phy_cap_info[5] |= ar->num_tx_chains - 1;
5713 
5714 		switch (i) {
5715 		case NL80211_IFTYPE_AP:
5716 			he_cap_elem->phy_cap_info[3] &=
5717 				~IEEE80211_HE_PHY_CAP3_DCM_MAX_CONST_TX_MASK;
5718 			he_cap_elem->phy_cap_info[9] |=
5719 				IEEE80211_HE_PHY_CAP9_RX_1024_QAM_LESS_THAN_242_TONE_RU;
5720 			break;
5721 		case NL80211_IFTYPE_STATION:
5722 			he_cap_elem->mac_cap_info[0] &=
5723 				~IEEE80211_HE_MAC_CAP0_TWT_RES;
5724 			he_cap_elem->mac_cap_info[0] |=
5725 				IEEE80211_HE_MAC_CAP0_TWT_REQ;
5726 			he_cap_elem->phy_cap_info[9] |=
5727 				IEEE80211_HE_PHY_CAP9_TX_1024_QAM_LESS_THAN_242_TONE_RU;
5728 			break;
5729 		case NL80211_IFTYPE_MESH_POINT:
5730 			ath11k_mac_filter_he_cap_mesh(he_cap_elem);
5731 			break;
5732 		}
5733 
5734 		ath11k_mac_set_hemcsmap(ar, cap, he_cap, band);
5735 
5736 		memset(he_cap->ppe_thres, 0, sizeof(he_cap->ppe_thres));
5737 		if (he_cap_elem->phy_cap_info[6] &
5738 		    IEEE80211_HE_PHY_CAP6_PPE_THRESHOLD_PRESENT)
5739 			ath11k_gen_ppe_thresh(&band_cap->he_ppet,
5740 					      he_cap->ppe_thres);
5741 
5742 		if (band == NL80211_BAND_6GHZ) {
5743 			data[idx].he_6ghz_capa.capa =
5744 				ath11k_mac_setup_he_6ghz_cap(cap, band_cap);
5745 		}
5746 		idx++;
5747 	}
5748 
5749 	return idx;
5750 }
5751 
5752 static void ath11k_mac_setup_he_cap(struct ath11k *ar,
5753 				    struct ath11k_pdev_cap *cap)
5754 {
5755 	struct ieee80211_supported_band *band;
5756 	int count;
5757 
5758 	if (cap->supported_bands & WMI_HOST_WLAN_2G_CAP) {
5759 		count = ath11k_mac_copy_he_cap(ar, cap,
5760 					       ar->mac.iftype[NL80211_BAND_2GHZ],
5761 					       NL80211_BAND_2GHZ);
5762 		band = &ar->mac.sbands[NL80211_BAND_2GHZ];
5763 		band->iftype_data = ar->mac.iftype[NL80211_BAND_2GHZ];
5764 		band->n_iftype_data = count;
5765 	}
5766 
5767 	if (cap->supported_bands & WMI_HOST_WLAN_5G_CAP) {
5768 		count = ath11k_mac_copy_he_cap(ar, cap,
5769 					       ar->mac.iftype[NL80211_BAND_5GHZ],
5770 					       NL80211_BAND_5GHZ);
5771 		band = &ar->mac.sbands[NL80211_BAND_5GHZ];
5772 		band->iftype_data = ar->mac.iftype[NL80211_BAND_5GHZ];
5773 		band->n_iftype_data = count;
5774 	}
5775 
5776 	if (cap->supported_bands & WMI_HOST_WLAN_5G_CAP &&
5777 	    ar->supports_6ghz) {
5778 		count = ath11k_mac_copy_he_cap(ar, cap,
5779 					       ar->mac.iftype[NL80211_BAND_6GHZ],
5780 					       NL80211_BAND_6GHZ);
5781 		band = &ar->mac.sbands[NL80211_BAND_6GHZ];
5782 		band->iftype_data = ar->mac.iftype[NL80211_BAND_6GHZ];
5783 		band->n_iftype_data = count;
5784 	}
5785 }
5786 
5787 static int __ath11k_set_antenna(struct ath11k *ar, u32 tx_ant, u32 rx_ant)
5788 {
5789 	int ret;
5790 
5791 	lockdep_assert_held(&ar->conf_mutex);
5792 
5793 	if (ath11k_check_chain_mask(ar, tx_ant, true))
5794 		return -EINVAL;
5795 
5796 	if (ath11k_check_chain_mask(ar, rx_ant, false))
5797 		return -EINVAL;
5798 
5799 	ar->cfg_tx_chainmask = tx_ant;
5800 	ar->cfg_rx_chainmask = rx_ant;
5801 
5802 	if (ar->state != ATH11K_STATE_ON &&
5803 	    ar->state != ATH11K_STATE_RESTARTED)
5804 		return 0;
5805 
5806 	ret = ath11k_wmi_pdev_set_param(ar, WMI_PDEV_PARAM_TX_CHAIN_MASK,
5807 					tx_ant, ar->pdev->pdev_id);
5808 	if (ret) {
5809 		ath11k_warn(ar->ab, "failed to set tx-chainmask: %d, req 0x%x\n",
5810 			    ret, tx_ant);
5811 		return ret;
5812 	}
5813 
5814 	ar->num_tx_chains = get_num_chains(tx_ant);
5815 
5816 	ret = ath11k_wmi_pdev_set_param(ar, WMI_PDEV_PARAM_RX_CHAIN_MASK,
5817 					rx_ant, ar->pdev->pdev_id);
5818 	if (ret) {
5819 		ath11k_warn(ar->ab, "failed to set rx-chainmask: %d, req 0x%x\n",
5820 			    ret, rx_ant);
5821 		return ret;
5822 	}
5823 
5824 	ar->num_rx_chains = get_num_chains(rx_ant);
5825 
5826 	/* Reload HT/VHT/HE capability */
5827 	ath11k_mac_setup_ht_vht_cap(ar, &ar->pdev->cap, NULL);
5828 	ath11k_mac_setup_he_cap(ar, &ar->pdev->cap);
5829 
5830 	return 0;
5831 }
5832 
5833 static void ath11k_mgmt_over_wmi_tx_drop(struct ath11k *ar, struct sk_buff *skb)
5834 {
5835 	int num_mgmt;
5836 
5837 	ieee80211_free_txskb(ar->hw, skb);
5838 
5839 	num_mgmt = atomic_dec_if_positive(&ar->num_pending_mgmt_tx);
5840 
5841 	if (num_mgmt < 0)
5842 		WARN_ON_ONCE(1);
5843 
5844 	if (!num_mgmt)
5845 		wake_up(&ar->txmgmt_empty_waitq);
5846 }
5847 
5848 static void ath11k_mac_tx_mgmt_free(struct ath11k *ar, int buf_id)
5849 {
5850 	struct sk_buff *msdu;
5851 	struct ieee80211_tx_info *info;
5852 
5853 	spin_lock_bh(&ar->txmgmt_idr_lock);
5854 	msdu = idr_remove(&ar->txmgmt_idr, buf_id);
5855 	spin_unlock_bh(&ar->txmgmt_idr_lock);
5856 
5857 	if (!msdu)
5858 		return;
5859 
5860 	dma_unmap_single(ar->ab->dev, ATH11K_SKB_CB(msdu)->paddr, msdu->len,
5861 			 DMA_TO_DEVICE);
5862 
5863 	info = IEEE80211_SKB_CB(msdu);
5864 	memset(&info->status, 0, sizeof(info->status));
5865 
5866 	ath11k_mgmt_over_wmi_tx_drop(ar, msdu);
5867 }
5868 
5869 int ath11k_mac_tx_mgmt_pending_free(int buf_id, void *skb, void *ctx)
5870 {
5871 	struct ath11k *ar = ctx;
5872 
5873 	ath11k_mac_tx_mgmt_free(ar, buf_id);
5874 
5875 	return 0;
5876 }
5877 
5878 static int ath11k_mac_vif_txmgmt_idr_remove(int buf_id, void *skb, void *ctx)
5879 {
5880 	struct ieee80211_vif *vif = ctx;
5881 	struct ath11k_skb_cb *skb_cb = ATH11K_SKB_CB((struct sk_buff *)skb);
5882 	struct ath11k *ar = skb_cb->ar;
5883 
5884 	if (skb_cb->vif == vif)
5885 		ath11k_mac_tx_mgmt_free(ar, buf_id);
5886 
5887 	return 0;
5888 }
5889 
5890 static int ath11k_mac_mgmt_tx_wmi(struct ath11k *ar, struct ath11k_vif *arvif,
5891 				  struct sk_buff *skb)
5892 {
5893 	struct ath11k_base *ab = ar->ab;
5894 	struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
5895 	struct ieee80211_tx_info *info;
5896 	dma_addr_t paddr;
5897 	int buf_id;
5898 	int ret;
5899 
5900 	ATH11K_SKB_CB(skb)->ar = ar;
5901 
5902 	spin_lock_bh(&ar->txmgmt_idr_lock);
5903 	buf_id = idr_alloc(&ar->txmgmt_idr, skb, 0,
5904 			   ATH11K_TX_MGMT_NUM_PENDING_MAX, GFP_ATOMIC);
5905 	spin_unlock_bh(&ar->txmgmt_idr_lock);
5906 
5907 	ath11k_dbg(ar->ab, ATH11K_DBG_MAC,
5908 		   "mac tx mgmt frame, buf id %d\n", buf_id);
5909 
5910 	if (buf_id < 0)
5911 		return -ENOSPC;
5912 
5913 	info = IEEE80211_SKB_CB(skb);
5914 	if (!(info->flags & IEEE80211_TX_CTL_HW_80211_ENCAP)) {
5915 		if ((ieee80211_is_action(hdr->frame_control) ||
5916 		     ieee80211_is_deauth(hdr->frame_control) ||
5917 		     ieee80211_is_disassoc(hdr->frame_control)) &&
5918 		     ieee80211_has_protected(hdr->frame_control)) {
5919 			skb_put(skb, IEEE80211_CCMP_MIC_LEN);
5920 		}
5921 	}
5922 
5923 	paddr = dma_map_single(ab->dev, skb->data, skb->len, DMA_TO_DEVICE);
5924 	if (dma_mapping_error(ab->dev, paddr)) {
5925 		ath11k_warn(ab, "failed to DMA map mgmt Tx buffer\n");
5926 		ret = -EIO;
5927 		goto err_free_idr;
5928 	}
5929 
5930 	ATH11K_SKB_CB(skb)->paddr = paddr;
5931 
5932 	ret = ath11k_wmi_mgmt_send(ar, arvif->vdev_id, buf_id, skb);
5933 	if (ret) {
5934 		ath11k_warn(ar->ab, "failed to send mgmt frame: %d\n", ret);
5935 		goto err_unmap_buf;
5936 	}
5937 
5938 	return 0;
5939 
5940 err_unmap_buf:
5941 	dma_unmap_single(ab->dev, ATH11K_SKB_CB(skb)->paddr,
5942 			 skb->len, DMA_TO_DEVICE);
5943 err_free_idr:
5944 	spin_lock_bh(&ar->txmgmt_idr_lock);
5945 	idr_remove(&ar->txmgmt_idr, buf_id);
5946 	spin_unlock_bh(&ar->txmgmt_idr_lock);
5947 
5948 	return ret;
5949 }
5950 
5951 static void ath11k_mgmt_over_wmi_tx_purge(struct ath11k *ar)
5952 {
5953 	struct sk_buff *skb;
5954 
5955 	while ((skb = skb_dequeue(&ar->wmi_mgmt_tx_queue)) != NULL)
5956 		ath11k_mgmt_over_wmi_tx_drop(ar, skb);
5957 }
5958 
5959 static void ath11k_mgmt_over_wmi_tx_work(struct work_struct *work)
5960 {
5961 	struct ath11k *ar = container_of(work, struct ath11k, wmi_mgmt_tx_work);
5962 	struct ath11k_skb_cb *skb_cb;
5963 	struct ath11k_vif *arvif;
5964 	struct sk_buff *skb;
5965 	int ret;
5966 
5967 	while ((skb = skb_dequeue(&ar->wmi_mgmt_tx_queue)) != NULL) {
5968 		skb_cb = ATH11K_SKB_CB(skb);
5969 		if (!skb_cb->vif) {
5970 			ath11k_warn(ar->ab, "no vif found for mgmt frame\n");
5971 			ath11k_mgmt_over_wmi_tx_drop(ar, skb);
5972 			continue;
5973 		}
5974 
5975 		arvif = ath11k_vif_to_arvif(skb_cb->vif);
5976 		mutex_lock(&ar->conf_mutex);
5977 		if (ar->allocated_vdev_map & (1LL << arvif->vdev_id)) {
5978 			ret = ath11k_mac_mgmt_tx_wmi(ar, arvif, skb);
5979 			if (ret) {
5980 				ath11k_warn(ar->ab, "failed to tx mgmt frame, vdev_id %d :%d\n",
5981 					    arvif->vdev_id, ret);
5982 				ath11k_mgmt_over_wmi_tx_drop(ar, skb);
5983 			} else {
5984 				ath11k_dbg(ar->ab, ATH11K_DBG_MAC,
5985 					   "mac tx mgmt frame, vdev_id %d\n",
5986 					   arvif->vdev_id);
5987 			}
5988 		} else {
5989 			ath11k_warn(ar->ab,
5990 				    "dropping mgmt frame for vdev %d, is_started %d\n",
5991 				    arvif->vdev_id,
5992 				    arvif->is_started);
5993 			ath11k_mgmt_over_wmi_tx_drop(ar, skb);
5994 		}
5995 		mutex_unlock(&ar->conf_mutex);
5996 	}
5997 }
5998 
5999 static int ath11k_mac_mgmt_tx(struct ath11k *ar, struct sk_buff *skb,
6000 			      bool is_prb_rsp)
6001 {
6002 	struct sk_buff_head *q = &ar->wmi_mgmt_tx_queue;
6003 
6004 	if (test_bit(ATH11K_FLAG_CRASH_FLUSH, &ar->ab->dev_flags))
6005 		return -ESHUTDOWN;
6006 
6007 	/* Drop probe response packets when the pending management tx
6008 	 * count has reached a certain threshold, so as to prioritize
6009 	 * other mgmt packets like auth and assoc to be sent on time
6010 	 * for establishing successful connections.
6011 	 */
6012 	if (is_prb_rsp &&
6013 	    atomic_read(&ar->num_pending_mgmt_tx) > ATH11K_PRB_RSP_DROP_THRESHOLD) {
6014 		ath11k_warn(ar->ab,
6015 			    "dropping probe response as pending queue is almost full\n");
6016 		return -ENOSPC;
6017 	}
6018 
6019 	if (skb_queue_len_lockless(q) >= ATH11K_TX_MGMT_NUM_PENDING_MAX) {
6020 		ath11k_warn(ar->ab, "mgmt tx queue is full\n");
6021 		return -ENOSPC;
6022 	}
6023 
6024 	skb_queue_tail(q, skb);
6025 	atomic_inc(&ar->num_pending_mgmt_tx);
6026 	queue_work(ar->ab->workqueue_aux, &ar->wmi_mgmt_tx_work);
6027 
6028 	return 0;
6029 }
6030 
6031 static void ath11k_mac_op_tx(struct ieee80211_hw *hw,
6032 			     struct ieee80211_tx_control *control,
6033 			     struct sk_buff *skb)
6034 {
6035 	struct ath11k_skb_cb *skb_cb = ATH11K_SKB_CB(skb);
6036 	struct ath11k *ar = hw->priv;
6037 	struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
6038 	struct ieee80211_vif *vif = info->control.vif;
6039 	struct ath11k_vif *arvif = ath11k_vif_to_arvif(vif);
6040 	struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
6041 	struct ieee80211_key_conf *key = info->control.hw_key;
6042 	struct ath11k_sta *arsta = NULL;
6043 	u32 info_flags = info->flags;
6044 	bool is_prb_rsp;
6045 	int ret;
6046 
6047 	memset(skb_cb, 0, sizeof(*skb_cb));
6048 	skb_cb->vif = vif;
6049 
6050 	if (key) {
6051 		skb_cb->cipher = key->cipher;
6052 		skb_cb->flags |= ATH11K_SKB_CIPHER_SET;
6053 	}
6054 
6055 	if (info_flags & IEEE80211_TX_CTL_HW_80211_ENCAP) {
6056 		skb_cb->flags |= ATH11K_SKB_HW_80211_ENCAP;
6057 	} else if (ieee80211_is_mgmt(hdr->frame_control)) {
6058 		is_prb_rsp = ieee80211_is_probe_resp(hdr->frame_control);
6059 		ret = ath11k_mac_mgmt_tx(ar, skb, is_prb_rsp);
6060 		if (ret) {
6061 			ath11k_warn(ar->ab, "failed to queue management frame %d\n",
6062 				    ret);
6063 			ieee80211_free_txskb(ar->hw, skb);
6064 		}
6065 		return;
6066 	}
6067 
6068 	if (control->sta)
6069 		arsta = (struct ath11k_sta *)control->sta->drv_priv;
6070 
6071 	ret = ath11k_dp_tx(ar, arvif, arsta, skb);
6072 	if (unlikely(ret)) {
6073 		ath11k_warn(ar->ab, "failed to transmit frame %d\n", ret);
6074 		ieee80211_free_txskb(ar->hw, skb);
6075 	}
6076 }
6077 
6078 void ath11k_mac_drain_tx(struct ath11k *ar)
6079 {
6080 	/* make sure rcu-protected mac80211 tx path itself is drained */
6081 	synchronize_net();
6082 
6083 	cancel_work_sync(&ar->wmi_mgmt_tx_work);
6084 	ath11k_mgmt_over_wmi_tx_purge(ar);
6085 }
6086 
6087 static int ath11k_mac_config_mon_status_default(struct ath11k *ar, bool enable)
6088 {
6089 	struct htt_rx_ring_tlv_filter tlv_filter = {0};
6090 	struct ath11k_base *ab = ar->ab;
6091 	int i, ret = 0;
6092 	u32 ring_id;
6093 
6094 	if (enable) {
6095 		tlv_filter = ath11k_mac_mon_status_filter_default;
6096 		if (ath11k_debugfs_rx_filter(ar))
6097 			tlv_filter.rx_filter = ath11k_debugfs_rx_filter(ar);
6098 	}
6099 
6100 	for (i = 0; i < ab->hw_params.num_rxmda_per_pdev; i++) {
6101 		ring_id = ar->dp.rx_mon_status_refill_ring[i].refill_buf_ring.ring_id;
6102 		ret = ath11k_dp_tx_htt_rx_filter_setup(ar->ab, ring_id,
6103 						       ar->dp.mac_id + i,
6104 						       HAL_RXDMA_MONITOR_STATUS,
6105 						       DP_RX_BUFFER_SIZE,
6106 						       &tlv_filter);
6107 	}
6108 
6109 	if (enable && !ar->ab->hw_params.rxdma1_enable)
6110 		mod_timer(&ar->ab->mon_reap_timer, jiffies +
6111 			  msecs_to_jiffies(ATH11K_MON_TIMER_INTERVAL));
6112 
6113 	return ret;
6114 }
6115 
6116 static void ath11k_mac_wait_reconfigure(struct ath11k_base *ab)
6117 {
6118 	int recovery_start_count;
6119 
6120 	if (!ab->is_reset)
6121 		return;
6122 
6123 	recovery_start_count = atomic_inc_return(&ab->recovery_start_count);
6124 	ath11k_dbg(ab, ATH11K_DBG_MAC, "recovery start count %d\n", recovery_start_count);
6125 
6126 	if (recovery_start_count == ab->num_radios) {
6127 		complete(&ab->recovery_start);
6128 		ath11k_dbg(ab, ATH11K_DBG_MAC, "recovery started success\n");
6129 	}
6130 
6131 	ath11k_dbg(ab, ATH11K_DBG_MAC, "waiting reconfigure...\n");
6132 
6133 	wait_for_completion_timeout(&ab->reconfigure_complete,
6134 				    ATH11K_RECONFIGURE_TIMEOUT_HZ);
6135 }
6136 
6137 static int ath11k_mac_op_start(struct ieee80211_hw *hw)
6138 {
6139 	struct ath11k *ar = hw->priv;
6140 	struct ath11k_base *ab = ar->ab;
6141 	struct ath11k_pdev *pdev = ar->pdev;
6142 	int ret;
6143 
6144 	ath11k_mac_drain_tx(ar);
6145 	mutex_lock(&ar->conf_mutex);
6146 
6147 	switch (ar->state) {
6148 	case ATH11K_STATE_OFF:
6149 		ar->state = ATH11K_STATE_ON;
6150 		break;
6151 	case ATH11K_STATE_RESTARTING:
6152 		ar->state = ATH11K_STATE_RESTARTED;
6153 		ath11k_mac_wait_reconfigure(ab);
6154 		break;
6155 	case ATH11K_STATE_RESTARTED:
6156 	case ATH11K_STATE_WEDGED:
6157 	case ATH11K_STATE_ON:
6158 		WARN_ON(1);
6159 		ret = -EINVAL;
6160 		goto err;
6161 	}
6162 
6163 	ret = ath11k_wmi_pdev_set_param(ar, WMI_PDEV_PARAM_PMF_QOS,
6164 					1, pdev->pdev_id);
6165 
6166 	if (ret) {
6167 		ath11k_err(ar->ab, "failed to enable PMF QOS: (%d\n", ret);
6168 		goto err;
6169 	}
6170 
6171 	ret = ath11k_wmi_pdev_set_param(ar, WMI_PDEV_PARAM_DYNAMIC_BW, 1,
6172 					pdev->pdev_id);
6173 	if (ret) {
6174 		ath11k_err(ar->ab, "failed to enable dynamic bw: %d\n", ret);
6175 		goto err;
6176 	}
6177 
6178 	if (test_bit(WMI_TLV_SERVICE_SPOOF_MAC_SUPPORT, ar->wmi->wmi_ab->svc_map)) {
6179 		ret = ath11k_wmi_scan_prob_req_oui(ar, ar->mac_addr);
6180 		if (ret) {
6181 			ath11k_err(ab, "failed to set prob req oui: %i\n", ret);
6182 			goto err;
6183 		}
6184 	}
6185 
6186 	ret = ath11k_wmi_pdev_set_param(ar, WMI_PDEV_PARAM_ARP_AC_OVERRIDE,
6187 					0, pdev->pdev_id);
6188 	if (ret) {
6189 		ath11k_err(ab, "failed to set ac override for ARP: %d\n",
6190 			   ret);
6191 		goto err;
6192 	}
6193 
6194 	ret = ath11k_wmi_send_dfs_phyerr_offload_enable_cmd(ar, pdev->pdev_id);
6195 	if (ret) {
6196 		ath11k_err(ab, "failed to offload radar detection: %d\n",
6197 			   ret);
6198 		goto err;
6199 	}
6200 
6201 	ret = ath11k_dp_tx_htt_h2t_ppdu_stats_req(ar,
6202 						  HTT_PPDU_STATS_TAG_DEFAULT);
6203 	if (ret) {
6204 		ath11k_err(ab, "failed to req ppdu stats: %d\n", ret);
6205 		goto err;
6206 	}
6207 
6208 	ret = ath11k_wmi_pdev_set_param(ar, WMI_PDEV_PARAM_MESH_MCAST_ENABLE,
6209 					1, pdev->pdev_id);
6210 
6211 	if (ret) {
6212 		ath11k_err(ar->ab, "failed to enable MESH MCAST ENABLE: (%d\n", ret);
6213 		goto err;
6214 	}
6215 
6216 	__ath11k_set_antenna(ar, ar->cfg_tx_chainmask, ar->cfg_rx_chainmask);
6217 
6218 	/* TODO: Do we need to enable ANI? */
6219 
6220 	ath11k_reg_update_chan_list(ar, false);
6221 
6222 	ar->num_started_vdevs = 0;
6223 	ar->num_created_vdevs = 0;
6224 	ar->num_peers = 0;
6225 	ar->allocated_vdev_map = 0;
6226 
6227 	/* Configure monitor status ring with default rx_filter to get rx status
6228 	 * such as rssi, rx_duration.
6229 	 */
6230 	ret = ath11k_mac_config_mon_status_default(ar, true);
6231 	if (ret) {
6232 		ath11k_err(ab, "failed to configure monitor status ring with default rx_filter: (%d)\n",
6233 			   ret);
6234 		goto err;
6235 	}
6236 
6237 	/* Configure the hash seed for hash based reo dest ring selection */
6238 	ath11k_wmi_pdev_lro_cfg(ar, ar->pdev->pdev_id);
6239 
6240 	/* allow device to enter IMPS */
6241 	if (ab->hw_params.idle_ps) {
6242 		ret = ath11k_wmi_pdev_set_param(ar, WMI_PDEV_PARAM_IDLE_PS_CONFIG,
6243 						1, pdev->pdev_id);
6244 		if (ret) {
6245 			ath11k_err(ab, "failed to enable idle ps: %d\n", ret);
6246 			goto err;
6247 		}
6248 	}
6249 
6250 	mutex_unlock(&ar->conf_mutex);
6251 
6252 	rcu_assign_pointer(ab->pdevs_active[ar->pdev_idx],
6253 			   &ab->pdevs[ar->pdev_idx]);
6254 
6255 	return 0;
6256 
6257 err:
6258 	ar->state = ATH11K_STATE_OFF;
6259 	mutex_unlock(&ar->conf_mutex);
6260 
6261 	return ret;
6262 }
6263 
6264 static void ath11k_mac_op_stop(struct ieee80211_hw *hw)
6265 {
6266 	struct ath11k *ar = hw->priv;
6267 	struct htt_ppdu_stats_info *ppdu_stats, *tmp;
6268 	int ret;
6269 
6270 	ath11k_mac_drain_tx(ar);
6271 
6272 	mutex_lock(&ar->conf_mutex);
6273 	ret = ath11k_mac_config_mon_status_default(ar, false);
6274 	if (ret)
6275 		ath11k_err(ar->ab, "failed to clear rx_filter for monitor status ring: (%d)\n",
6276 			   ret);
6277 
6278 	clear_bit(ATH11K_CAC_RUNNING, &ar->dev_flags);
6279 	ar->state = ATH11K_STATE_OFF;
6280 	mutex_unlock(&ar->conf_mutex);
6281 
6282 	cancel_delayed_work_sync(&ar->scan.timeout);
6283 	cancel_work_sync(&ar->regd_update_work);
6284 	cancel_work_sync(&ar->ab->update_11d_work);
6285 
6286 	if (ar->state_11d == ATH11K_11D_PREPARING) {
6287 		ar->state_11d = ATH11K_11D_IDLE;
6288 		complete(&ar->completed_11d_scan);
6289 	}
6290 
6291 	spin_lock_bh(&ar->data_lock);
6292 	list_for_each_entry_safe(ppdu_stats, tmp, &ar->ppdu_stats_info, list) {
6293 		list_del(&ppdu_stats->list);
6294 		kfree(ppdu_stats);
6295 	}
6296 	spin_unlock_bh(&ar->data_lock);
6297 
6298 	rcu_assign_pointer(ar->ab->pdevs_active[ar->pdev_idx], NULL);
6299 
6300 	synchronize_rcu();
6301 
6302 	atomic_set(&ar->num_pending_mgmt_tx, 0);
6303 }
6304 
6305 static int ath11k_mac_setup_vdev_params_mbssid(struct ath11k_vif *arvif,
6306 					       u32 *flags, u32 *tx_vdev_id)
6307 {
6308 	struct ath11k *ar = arvif->ar;
6309 	struct ath11k_vif *tx_arvif;
6310 	struct ieee80211_vif *tx_vif;
6311 
6312 	*tx_vdev_id = 0;
6313 	tx_vif = arvif->vif->mbssid_tx_vif;
6314 	if (!tx_vif) {
6315 		*flags = WMI_HOST_VDEV_FLAGS_NON_MBSSID_AP;
6316 		return 0;
6317 	}
6318 
6319 	tx_arvif = (void *)tx_vif->drv_priv;
6320 
6321 	if (arvif->vif->bss_conf.nontransmitted) {
6322 		if (ar->hw->wiphy != ieee80211_vif_to_wdev(tx_vif)->wiphy)
6323 			return -EINVAL;
6324 
6325 		*flags = WMI_HOST_VDEV_FLAGS_NON_TRANSMIT_AP;
6326 		*tx_vdev_id = ath11k_vif_to_arvif(tx_vif)->vdev_id;
6327 	} else if (tx_arvif == arvif) {
6328 		*flags = WMI_HOST_VDEV_FLAGS_TRANSMIT_AP;
6329 	} else {
6330 		return -EINVAL;
6331 	}
6332 
6333 	if (arvif->vif->bss_conf.ema_ap)
6334 		*flags |= WMI_HOST_VDEV_FLAGS_EMA_MODE;
6335 
6336 	return 0;
6337 }
6338 
6339 static int ath11k_mac_setup_vdev_create_params(struct ath11k_vif *arvif,
6340 					       struct vdev_create_params *params)
6341 {
6342 	struct ath11k *ar = arvif->ar;
6343 	struct ath11k_pdev *pdev = ar->pdev;
6344 	int ret;
6345 
6346 	params->if_id = arvif->vdev_id;
6347 	params->type = arvif->vdev_type;
6348 	params->subtype = arvif->vdev_subtype;
6349 	params->pdev_id = pdev->pdev_id;
6350 	params->mbssid_flags = 0;
6351 	params->mbssid_tx_vdev_id = 0;
6352 
6353 	if (!test_bit(WMI_TLV_SERVICE_MBSS_PARAM_IN_VDEV_START_SUPPORT,
6354 		      ar->ab->wmi_ab.svc_map)) {
6355 		ret = ath11k_mac_setup_vdev_params_mbssid(arvif,
6356 							  &params->mbssid_flags,
6357 							  &params->mbssid_tx_vdev_id);
6358 		if (ret)
6359 			return ret;
6360 	}
6361 
6362 	if (pdev->cap.supported_bands & WMI_HOST_WLAN_2G_CAP) {
6363 		params->chains[NL80211_BAND_2GHZ].tx = ar->num_tx_chains;
6364 		params->chains[NL80211_BAND_2GHZ].rx = ar->num_rx_chains;
6365 	}
6366 	if (pdev->cap.supported_bands & WMI_HOST_WLAN_5G_CAP) {
6367 		params->chains[NL80211_BAND_5GHZ].tx = ar->num_tx_chains;
6368 		params->chains[NL80211_BAND_5GHZ].rx = ar->num_rx_chains;
6369 	}
6370 	if (pdev->cap.supported_bands & WMI_HOST_WLAN_5G_CAP &&
6371 	    ar->supports_6ghz) {
6372 		params->chains[NL80211_BAND_6GHZ].tx = ar->num_tx_chains;
6373 		params->chains[NL80211_BAND_6GHZ].rx = ar->num_rx_chains;
6374 	}
6375 	return 0;
6376 }
6377 
6378 static void ath11k_mac_op_update_vif_offload(struct ieee80211_hw *hw,
6379 					     struct ieee80211_vif *vif)
6380 {
6381 	struct ath11k *ar = hw->priv;
6382 	struct ath11k_base *ab = ar->ab;
6383 	struct ath11k_vif *arvif = ath11k_vif_to_arvif(vif);
6384 	u32 param_id, param_value;
6385 	int ret;
6386 
6387 	param_id = WMI_VDEV_PARAM_TX_ENCAP_TYPE;
6388 	if (ath11k_frame_mode != ATH11K_HW_TXRX_ETHERNET ||
6389 	    (vif->type != NL80211_IFTYPE_STATION &&
6390 	     vif->type != NL80211_IFTYPE_AP))
6391 		vif->offload_flags &= ~(IEEE80211_OFFLOAD_ENCAP_ENABLED |
6392 					IEEE80211_OFFLOAD_DECAP_ENABLED);
6393 
6394 	if (vif->offload_flags & IEEE80211_OFFLOAD_ENCAP_ENABLED)
6395 		param_value = ATH11K_HW_TXRX_ETHERNET;
6396 	else if (test_bit(ATH11K_FLAG_RAW_MODE, &ab->dev_flags))
6397 		param_value = ATH11K_HW_TXRX_RAW;
6398 	else
6399 		param_value = ATH11K_HW_TXRX_NATIVE_WIFI;
6400 
6401 	ret = ath11k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
6402 					    param_id, param_value);
6403 	if (ret) {
6404 		ath11k_warn(ab, "failed to set vdev %d tx encap mode: %d\n",
6405 			    arvif->vdev_id, ret);
6406 		vif->offload_flags &= ~IEEE80211_OFFLOAD_ENCAP_ENABLED;
6407 	}
6408 
6409 	param_id = WMI_VDEV_PARAM_RX_DECAP_TYPE;
6410 	if (vif->offload_flags & IEEE80211_OFFLOAD_DECAP_ENABLED)
6411 		param_value = ATH11K_HW_TXRX_ETHERNET;
6412 	else if (test_bit(ATH11K_FLAG_RAW_MODE, &ab->dev_flags))
6413 		param_value = ATH11K_HW_TXRX_RAW;
6414 	else
6415 		param_value = ATH11K_HW_TXRX_NATIVE_WIFI;
6416 
6417 	ret = ath11k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
6418 					    param_id, param_value);
6419 	if (ret) {
6420 		ath11k_warn(ab, "failed to set vdev %d rx decap mode: %d\n",
6421 			    arvif->vdev_id, ret);
6422 		vif->offload_flags &= ~IEEE80211_OFFLOAD_DECAP_ENABLED;
6423 	}
6424 }
6425 
6426 static bool ath11k_mac_vif_ap_active_any(struct ath11k_base *ab)
6427 {
6428 	struct ath11k *ar;
6429 	struct ath11k_pdev *pdev;
6430 	struct ath11k_vif *arvif;
6431 	int i;
6432 
6433 	for (i = 0; i < ab->num_radios; i++) {
6434 		pdev = &ab->pdevs[i];
6435 		ar = pdev->ar;
6436 		list_for_each_entry(arvif, &ar->arvifs, list) {
6437 			if (arvif->is_up && arvif->vdev_type == WMI_VDEV_TYPE_AP)
6438 				return true;
6439 		}
6440 	}
6441 	return false;
6442 }
6443 
6444 void ath11k_mac_11d_scan_start(struct ath11k *ar, u32 vdev_id)
6445 {
6446 	struct wmi_11d_scan_start_params param;
6447 	int ret;
6448 
6449 	mutex_lock(&ar->ab->vdev_id_11d_lock);
6450 
6451 	ath11k_dbg(ar->ab, ATH11K_DBG_MAC, "mac vdev id for 11d scan %d\n",
6452 		   ar->vdev_id_11d_scan);
6453 
6454 	if (ar->regdom_set_by_user)
6455 		goto fin;
6456 
6457 	if (ar->vdev_id_11d_scan != ATH11K_11D_INVALID_VDEV_ID)
6458 		goto fin;
6459 
6460 	if (!test_bit(WMI_TLV_SERVICE_11D_OFFLOAD, ar->ab->wmi_ab.svc_map))
6461 		goto fin;
6462 
6463 	if (ath11k_mac_vif_ap_active_any(ar->ab))
6464 		goto fin;
6465 
6466 	param.vdev_id = vdev_id;
6467 	param.start_interval_msec = 0;
6468 	param.scan_period_msec = ATH11K_SCAN_11D_INTERVAL;
6469 
6470 	ath11k_dbg(ar->ab, ATH11K_DBG_MAC, "mac start 11d scan\n");
6471 
6472 	ret = ath11k_wmi_send_11d_scan_start_cmd(ar, &param);
6473 	if (ret) {
6474 		ath11k_warn(ar->ab, "failed to start 11d scan vdev %d ret: %d\n",
6475 			    vdev_id, ret);
6476 	} else {
6477 		ar->vdev_id_11d_scan = vdev_id;
6478 		if (ar->state_11d == ATH11K_11D_PREPARING)
6479 			ar->state_11d = ATH11K_11D_RUNNING;
6480 	}
6481 
6482 fin:
6483 	if (ar->state_11d == ATH11K_11D_PREPARING) {
6484 		ar->state_11d = ATH11K_11D_IDLE;
6485 		complete(&ar->completed_11d_scan);
6486 	}
6487 
6488 	mutex_unlock(&ar->ab->vdev_id_11d_lock);
6489 }
6490 
6491 void ath11k_mac_11d_scan_stop(struct ath11k *ar)
6492 {
6493 	int ret;
6494 	u32 vdev_id;
6495 
6496 	if (!test_bit(WMI_TLV_SERVICE_11D_OFFLOAD, ar->ab->wmi_ab.svc_map))
6497 		return;
6498 
6499 	ath11k_dbg(ar->ab, ATH11K_DBG_MAC, "mac stop 11d scan\n");
6500 
6501 	mutex_lock(&ar->ab->vdev_id_11d_lock);
6502 
6503 	ath11k_dbg(ar->ab, ATH11K_DBG_MAC, "mac stop 11d vdev id %d\n",
6504 		   ar->vdev_id_11d_scan);
6505 
6506 	if (ar->state_11d == ATH11K_11D_PREPARING) {
6507 		ar->state_11d = ATH11K_11D_IDLE;
6508 		complete(&ar->completed_11d_scan);
6509 	}
6510 
6511 	if (ar->vdev_id_11d_scan != ATH11K_11D_INVALID_VDEV_ID) {
6512 		vdev_id = ar->vdev_id_11d_scan;
6513 
6514 		ret = ath11k_wmi_send_11d_scan_stop_cmd(ar, vdev_id);
6515 		if (ret) {
6516 			ath11k_warn(ar->ab,
6517 				    "failed to stopt 11d scan vdev %d ret: %d\n",
6518 				    vdev_id, ret);
6519 		} else {
6520 			ar->vdev_id_11d_scan = ATH11K_11D_INVALID_VDEV_ID;
6521 			ar->state_11d = ATH11K_11D_IDLE;
6522 			complete(&ar->completed_11d_scan);
6523 		}
6524 	}
6525 	mutex_unlock(&ar->ab->vdev_id_11d_lock);
6526 }
6527 
6528 void ath11k_mac_11d_scan_stop_all(struct ath11k_base *ab)
6529 {
6530 	struct ath11k *ar;
6531 	struct ath11k_pdev *pdev;
6532 	int i;
6533 
6534 	ath11k_dbg(ab, ATH11K_DBG_MAC, "mac stop soc 11d scan\n");
6535 
6536 	for (i = 0; i < ab->num_radios; i++) {
6537 		pdev = &ab->pdevs[i];
6538 		ar = pdev->ar;
6539 
6540 		ath11k_mac_11d_scan_stop(ar);
6541 	}
6542 }
6543 
6544 static int ath11k_mac_vdev_delete(struct ath11k *ar, struct ath11k_vif *arvif)
6545 {
6546 	unsigned long time_left;
6547 	struct ieee80211_vif *vif = arvif->vif;
6548 	int ret = 0;
6549 
6550 	lockdep_assert_held(&ar->conf_mutex);
6551 
6552 	reinit_completion(&ar->vdev_delete_done);
6553 
6554 	ret = ath11k_wmi_vdev_delete(ar, arvif->vdev_id);
6555 	if (ret) {
6556 		ath11k_warn(ar->ab, "failed to delete WMI vdev %d: %d\n",
6557 			    arvif->vdev_id, ret);
6558 		return ret;
6559 	}
6560 
6561 	time_left = wait_for_completion_timeout(&ar->vdev_delete_done,
6562 						ATH11K_VDEV_DELETE_TIMEOUT_HZ);
6563 	if (time_left == 0) {
6564 		ath11k_warn(ar->ab, "Timeout in receiving vdev delete response\n");
6565 		return -ETIMEDOUT;
6566 	}
6567 
6568 	ar->ab->free_vdev_map |= 1LL << (arvif->vdev_id);
6569 	ar->allocated_vdev_map &= ~(1LL << arvif->vdev_id);
6570 	ar->num_created_vdevs--;
6571 
6572 	ath11k_dbg(ar->ab, ATH11K_DBG_MAC, "vdev %pM deleted, vdev_id %d\n",
6573 		   vif->addr, arvif->vdev_id);
6574 
6575 	return ret;
6576 }
6577 
6578 static int ath11k_mac_op_add_interface(struct ieee80211_hw *hw,
6579 				       struct ieee80211_vif *vif)
6580 {
6581 	struct ath11k *ar = hw->priv;
6582 	struct ath11k_base *ab = ar->ab;
6583 	struct ath11k_vif *arvif = ath11k_vif_to_arvif(vif);
6584 	struct vdev_create_params vdev_param = {0};
6585 	struct peer_create_params peer_param;
6586 	u32 param_id, param_value;
6587 	u16 nss;
6588 	int i;
6589 	int ret, fbret;
6590 	int bit;
6591 
6592 	vif->driver_flags |= IEEE80211_VIF_SUPPORTS_UAPSD;
6593 
6594 	mutex_lock(&ar->conf_mutex);
6595 
6596 	if (vif->type == NL80211_IFTYPE_AP &&
6597 	    ar->num_peers > (ar->max_num_peers - 1)) {
6598 		ath11k_warn(ab, "failed to create vdev due to insufficient peer entry resource in firmware\n");
6599 		ret = -ENOBUFS;
6600 		goto err;
6601 	}
6602 
6603 	if (ar->num_created_vdevs > (TARGET_NUM_VDEVS(ab) - 1)) {
6604 		ath11k_warn(ab, "failed to create vdev %u, reached max vdev limit %d\n",
6605 			    ar->num_created_vdevs, TARGET_NUM_VDEVS(ab));
6606 		ret = -EBUSY;
6607 		goto err;
6608 	}
6609 
6610 	/* In the case of hardware recovery, debugfs files are
6611 	 * not deleted since ieee80211_ops.remove_interface() is
6612 	 * not invoked. In such cases, try to delete the files.
6613 	 * These will be re-created later.
6614 	 */
6615 	ath11k_debugfs_remove_interface(arvif);
6616 
6617 	memset(arvif, 0, sizeof(*arvif));
6618 
6619 	arvif->ar = ar;
6620 	arvif->vif = vif;
6621 
6622 	INIT_LIST_HEAD(&arvif->list);
6623 	INIT_DELAYED_WORK(&arvif->connection_loss_work,
6624 			  ath11k_mac_vif_sta_connection_loss_work);
6625 
6626 	for (i = 0; i < ARRAY_SIZE(arvif->bitrate_mask.control); i++) {
6627 		arvif->bitrate_mask.control[i].legacy = 0xffffffff;
6628 		arvif->bitrate_mask.control[i].gi = NL80211_TXRATE_FORCE_SGI;
6629 		memset(arvif->bitrate_mask.control[i].ht_mcs, 0xff,
6630 		       sizeof(arvif->bitrate_mask.control[i].ht_mcs));
6631 		memset(arvif->bitrate_mask.control[i].vht_mcs, 0xff,
6632 		       sizeof(arvif->bitrate_mask.control[i].vht_mcs));
6633 		memset(arvif->bitrate_mask.control[i].he_mcs, 0xff,
6634 		       sizeof(arvif->bitrate_mask.control[i].he_mcs));
6635 	}
6636 
6637 	bit = __ffs64(ab->free_vdev_map);
6638 
6639 	arvif->vdev_id = bit;
6640 	arvif->vdev_subtype = WMI_VDEV_SUBTYPE_NONE;
6641 
6642 	switch (vif->type) {
6643 	case NL80211_IFTYPE_UNSPECIFIED:
6644 	case NL80211_IFTYPE_STATION:
6645 		arvif->vdev_type = WMI_VDEV_TYPE_STA;
6646 		break;
6647 	case NL80211_IFTYPE_MESH_POINT:
6648 		arvif->vdev_subtype = WMI_VDEV_SUBTYPE_MESH_11S;
6649 		fallthrough;
6650 	case NL80211_IFTYPE_AP:
6651 		arvif->vdev_type = WMI_VDEV_TYPE_AP;
6652 		break;
6653 	case NL80211_IFTYPE_MONITOR:
6654 		arvif->vdev_type = WMI_VDEV_TYPE_MONITOR;
6655 		ar->monitor_vdev_id = bit;
6656 		break;
6657 	default:
6658 		WARN_ON(1);
6659 		break;
6660 	}
6661 
6662 	ath11k_dbg(ar->ab, ATH11K_DBG_MAC, "mac add interface id %d type %d subtype %d map %llx\n",
6663 		   arvif->vdev_id, arvif->vdev_type, arvif->vdev_subtype,
6664 		   ab->free_vdev_map);
6665 
6666 	vif->cab_queue = arvif->vdev_id % (ATH11K_HW_MAX_QUEUES - 1);
6667 	for (i = 0; i < ARRAY_SIZE(vif->hw_queue); i++)
6668 		vif->hw_queue[i] = i % (ATH11K_HW_MAX_QUEUES - 1);
6669 
6670 	ret = ath11k_mac_setup_vdev_create_params(arvif, &vdev_param);
6671 	if (ret) {
6672 		ath11k_warn(ab, "failed to create vdev parameters %d: %d\n",
6673 			    arvif->vdev_id, ret);
6674 		goto err;
6675 	}
6676 
6677 	ret = ath11k_wmi_vdev_create(ar, vif->addr, &vdev_param);
6678 	if (ret) {
6679 		ath11k_warn(ab, "failed to create WMI vdev %d: %d\n",
6680 			    arvif->vdev_id, ret);
6681 		goto err;
6682 	}
6683 
6684 	ar->num_created_vdevs++;
6685 	ath11k_dbg(ab, ATH11K_DBG_MAC, "vdev %pM created, vdev_id %d\n",
6686 		   vif->addr, arvif->vdev_id);
6687 	ar->allocated_vdev_map |= 1LL << arvif->vdev_id;
6688 	ab->free_vdev_map &= ~(1LL << arvif->vdev_id);
6689 
6690 	spin_lock_bh(&ar->data_lock);
6691 	list_add(&arvif->list, &ar->arvifs);
6692 	spin_unlock_bh(&ar->data_lock);
6693 
6694 	ath11k_mac_op_update_vif_offload(hw, vif);
6695 
6696 	nss = get_num_chains(ar->cfg_tx_chainmask) ? : 1;
6697 	ret = ath11k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
6698 					    WMI_VDEV_PARAM_NSS, nss);
6699 	if (ret) {
6700 		ath11k_warn(ab, "failed to set vdev %d chainmask 0x%x, nss %d :%d\n",
6701 			    arvif->vdev_id, ar->cfg_tx_chainmask, nss, ret);
6702 		goto err_vdev_del;
6703 	}
6704 
6705 	switch (arvif->vdev_type) {
6706 	case WMI_VDEV_TYPE_AP:
6707 		peer_param.vdev_id = arvif->vdev_id;
6708 		peer_param.peer_addr = vif->addr;
6709 		peer_param.peer_type = WMI_PEER_TYPE_DEFAULT;
6710 		ret = ath11k_peer_create(ar, arvif, NULL, &peer_param);
6711 		if (ret) {
6712 			ath11k_warn(ab, "failed to vdev %d create peer for AP: %d\n",
6713 				    arvif->vdev_id, ret);
6714 			goto err_vdev_del;
6715 		}
6716 
6717 		ret = ath11k_mac_set_kickout(arvif);
6718 		if (ret) {
6719 			ath11k_warn(ar->ab, "failed to set vdev %i kickout parameters: %d\n",
6720 				    arvif->vdev_id, ret);
6721 			goto err_peer_del;
6722 		}
6723 
6724 		ath11k_mac_11d_scan_stop_all(ar->ab);
6725 		break;
6726 	case WMI_VDEV_TYPE_STA:
6727 		param_id = WMI_STA_PS_PARAM_RX_WAKE_POLICY;
6728 		param_value = WMI_STA_PS_RX_WAKE_POLICY_WAKE;
6729 		ret = ath11k_wmi_set_sta_ps_param(ar, arvif->vdev_id,
6730 						  param_id, param_value);
6731 		if (ret) {
6732 			ath11k_warn(ar->ab, "failed to set vdev %d RX wake policy: %d\n",
6733 				    arvif->vdev_id, ret);
6734 			goto err_peer_del;
6735 		}
6736 
6737 		param_id = WMI_STA_PS_PARAM_TX_WAKE_THRESHOLD;
6738 		param_value = WMI_STA_PS_TX_WAKE_THRESHOLD_ALWAYS;
6739 		ret = ath11k_wmi_set_sta_ps_param(ar, arvif->vdev_id,
6740 						  param_id, param_value);
6741 		if (ret) {
6742 			ath11k_warn(ar->ab, "failed to set vdev %d TX wake threshold: %d\n",
6743 				    arvif->vdev_id, ret);
6744 			goto err_peer_del;
6745 		}
6746 
6747 		param_id = WMI_STA_PS_PARAM_PSPOLL_COUNT;
6748 		param_value = WMI_STA_PS_PSPOLL_COUNT_NO_MAX;
6749 		ret = ath11k_wmi_set_sta_ps_param(ar, arvif->vdev_id,
6750 						  param_id, param_value);
6751 		if (ret) {
6752 			ath11k_warn(ar->ab, "failed to set vdev %d pspoll count: %d\n",
6753 				    arvif->vdev_id, ret);
6754 			goto err_peer_del;
6755 		}
6756 
6757 		ret = ath11k_wmi_pdev_set_ps_mode(ar, arvif->vdev_id,
6758 						  WMI_STA_PS_MODE_DISABLED);
6759 		if (ret) {
6760 			ath11k_warn(ar->ab, "failed to disable vdev %d ps mode: %d\n",
6761 				    arvif->vdev_id, ret);
6762 			goto err_peer_del;
6763 		}
6764 
6765 		if (test_bit(WMI_TLV_SERVICE_11D_OFFLOAD, ab->wmi_ab.svc_map)) {
6766 			reinit_completion(&ar->completed_11d_scan);
6767 			ar->state_11d = ATH11K_11D_PREPARING;
6768 		}
6769 		break;
6770 	case WMI_VDEV_TYPE_MONITOR:
6771 		set_bit(ATH11K_FLAG_MONITOR_VDEV_CREATED, &ar->monitor_flags);
6772 		break;
6773 	default:
6774 		break;
6775 	}
6776 
6777 	arvif->txpower = vif->bss_conf.txpower;
6778 	ret = ath11k_mac_txpower_recalc(ar);
6779 	if (ret)
6780 		goto err_peer_del;
6781 
6782 	param_id = WMI_VDEV_PARAM_RTS_THRESHOLD;
6783 	param_value = ar->hw->wiphy->rts_threshold;
6784 	ret = ath11k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
6785 					    param_id, param_value);
6786 	if (ret) {
6787 		ath11k_warn(ar->ab, "failed to set rts threshold for vdev %d: %d\n",
6788 			    arvif->vdev_id, ret);
6789 	}
6790 
6791 	ath11k_dp_vdev_tx_attach(ar, arvif);
6792 
6793 	ath11k_debugfs_add_interface(arvif);
6794 
6795 	if (vif->type != NL80211_IFTYPE_MONITOR &&
6796 	    test_bit(ATH11K_FLAG_MONITOR_CONF_ENABLED, &ar->monitor_flags)) {
6797 		ret = ath11k_mac_monitor_vdev_create(ar);
6798 		if (ret)
6799 			ath11k_warn(ar->ab, "failed to create monitor vdev during add interface: %d",
6800 				    ret);
6801 	}
6802 
6803 	mutex_unlock(&ar->conf_mutex);
6804 
6805 	return 0;
6806 
6807 err_peer_del:
6808 	if (arvif->vdev_type == WMI_VDEV_TYPE_AP) {
6809 		fbret = ath11k_peer_delete(ar, arvif->vdev_id, vif->addr);
6810 		if (fbret) {
6811 			ath11k_warn(ar->ab, "fallback fail to delete peer addr %pM vdev_id %d ret %d\n",
6812 				    vif->addr, arvif->vdev_id, fbret);
6813 			goto err;
6814 		}
6815 	}
6816 
6817 err_vdev_del:
6818 	ath11k_mac_vdev_delete(ar, arvif);
6819 	spin_lock_bh(&ar->data_lock);
6820 	list_del(&arvif->list);
6821 	spin_unlock_bh(&ar->data_lock);
6822 
6823 err:
6824 	mutex_unlock(&ar->conf_mutex);
6825 
6826 	return ret;
6827 }
6828 
6829 static int ath11k_mac_vif_unref(int buf_id, void *skb, void *ctx)
6830 {
6831 	struct ieee80211_vif *vif = (struct ieee80211_vif *)ctx;
6832 	struct ath11k_skb_cb *skb_cb = ATH11K_SKB_CB((struct sk_buff *)skb);
6833 
6834 	if (skb_cb->vif == vif)
6835 		skb_cb->vif = NULL;
6836 
6837 	return 0;
6838 }
6839 
6840 static void ath11k_mac_op_remove_interface(struct ieee80211_hw *hw,
6841 					   struct ieee80211_vif *vif)
6842 {
6843 	struct ath11k *ar = hw->priv;
6844 	struct ath11k_vif *arvif = ath11k_vif_to_arvif(vif);
6845 	struct ath11k_base *ab = ar->ab;
6846 	int ret;
6847 	int i;
6848 
6849 	cancel_delayed_work_sync(&arvif->connection_loss_work);
6850 
6851 	mutex_lock(&ar->conf_mutex);
6852 
6853 	ath11k_dbg(ab, ATH11K_DBG_MAC, "mac remove interface (vdev %d)\n",
6854 		   arvif->vdev_id);
6855 
6856 	ret = ath11k_spectral_vif_stop(arvif);
6857 	if (ret)
6858 		ath11k_warn(ab, "failed to stop spectral for vdev %i: %d\n",
6859 			    arvif->vdev_id, ret);
6860 
6861 	if (arvif->vdev_type == WMI_VDEV_TYPE_STA)
6862 		ath11k_mac_11d_scan_stop(ar);
6863 
6864 	if (arvif->vdev_type == WMI_VDEV_TYPE_AP) {
6865 		ret = ath11k_peer_delete(ar, arvif->vdev_id, vif->addr);
6866 		if (ret)
6867 			ath11k_warn(ab, "failed to submit AP self-peer removal on vdev %d: %d\n",
6868 				    arvif->vdev_id, ret);
6869 	}
6870 
6871 	ret = ath11k_mac_vdev_delete(ar, arvif);
6872 	if (ret) {
6873 		ath11k_warn(ab, "failed to delete vdev %d: %d\n",
6874 			    arvif->vdev_id, ret);
6875 		goto err_vdev_del;
6876 	}
6877 
6878 	if (arvif->vdev_type == WMI_VDEV_TYPE_MONITOR) {
6879 		clear_bit(ATH11K_FLAG_MONITOR_VDEV_CREATED, &ar->monitor_flags);
6880 		ar->monitor_vdev_id = -1;
6881 	} else if (test_bit(ATH11K_FLAG_MONITOR_VDEV_CREATED, &ar->monitor_flags) &&
6882 		   !test_bit(ATH11K_FLAG_MONITOR_STARTED, &ar->monitor_flags)) {
6883 		ret = ath11k_mac_monitor_vdev_delete(ar);
6884 		if (ret)
6885 			/* continue even if there's an error */
6886 			ath11k_warn(ar->ab, "failed to delete vdev monitor during remove interface: %d",
6887 				    ret);
6888 	}
6889 
6890 err_vdev_del:
6891 	spin_lock_bh(&ar->data_lock);
6892 	list_del(&arvif->list);
6893 	spin_unlock_bh(&ar->data_lock);
6894 
6895 	ath11k_peer_cleanup(ar, arvif->vdev_id);
6896 
6897 	idr_for_each(&ar->txmgmt_idr,
6898 		     ath11k_mac_vif_txmgmt_idr_remove, vif);
6899 
6900 	for (i = 0; i < ab->hw_params.max_tx_ring; i++) {
6901 		spin_lock_bh(&ab->dp.tx_ring[i].tx_idr_lock);
6902 		idr_for_each(&ab->dp.tx_ring[i].txbuf_idr,
6903 			     ath11k_mac_vif_unref, vif);
6904 		spin_unlock_bh(&ab->dp.tx_ring[i].tx_idr_lock);
6905 	}
6906 
6907 	/* Recalc txpower for remaining vdev */
6908 	ath11k_mac_txpower_recalc(ar);
6909 
6910 	ath11k_debugfs_remove_interface(arvif);
6911 
6912 	/* TODO: recal traffic pause state based on the available vdevs */
6913 
6914 	mutex_unlock(&ar->conf_mutex);
6915 }
6916 
6917 /* FIXME: Has to be verified. */
6918 #define SUPPORTED_FILTERS			\
6919 	(FIF_ALLMULTI |				\
6920 	FIF_CONTROL |				\
6921 	FIF_PSPOLL |				\
6922 	FIF_OTHER_BSS |				\
6923 	FIF_BCN_PRBRESP_PROMISC |		\
6924 	FIF_PROBE_REQ |				\
6925 	FIF_FCSFAIL)
6926 
6927 static void ath11k_mac_op_configure_filter(struct ieee80211_hw *hw,
6928 					   unsigned int changed_flags,
6929 					   unsigned int *total_flags,
6930 					   u64 multicast)
6931 {
6932 	struct ath11k *ar = hw->priv;
6933 
6934 	mutex_lock(&ar->conf_mutex);
6935 
6936 	*total_flags &= SUPPORTED_FILTERS;
6937 	ar->filter_flags = *total_flags;
6938 
6939 	mutex_unlock(&ar->conf_mutex);
6940 }
6941 
6942 static int ath11k_mac_op_get_antenna(struct ieee80211_hw *hw, u32 *tx_ant, u32 *rx_ant)
6943 {
6944 	struct ath11k *ar = hw->priv;
6945 
6946 	mutex_lock(&ar->conf_mutex);
6947 
6948 	*tx_ant = ar->cfg_tx_chainmask;
6949 	*rx_ant = ar->cfg_rx_chainmask;
6950 
6951 	mutex_unlock(&ar->conf_mutex);
6952 
6953 	return 0;
6954 }
6955 
6956 static int ath11k_mac_op_set_antenna(struct ieee80211_hw *hw, u32 tx_ant, u32 rx_ant)
6957 {
6958 	struct ath11k *ar = hw->priv;
6959 	int ret;
6960 
6961 	mutex_lock(&ar->conf_mutex);
6962 	ret = __ath11k_set_antenna(ar, tx_ant, rx_ant);
6963 	mutex_unlock(&ar->conf_mutex);
6964 
6965 	return ret;
6966 }
6967 
6968 static int ath11k_mac_op_ampdu_action(struct ieee80211_hw *hw,
6969 				      struct ieee80211_vif *vif,
6970 				      struct ieee80211_ampdu_params *params)
6971 {
6972 	struct ath11k *ar = hw->priv;
6973 	int ret = -EINVAL;
6974 
6975 	mutex_lock(&ar->conf_mutex);
6976 
6977 	switch (params->action) {
6978 	case IEEE80211_AMPDU_RX_START:
6979 		ret = ath11k_dp_rx_ampdu_start(ar, params);
6980 		break;
6981 	case IEEE80211_AMPDU_RX_STOP:
6982 		ret = ath11k_dp_rx_ampdu_stop(ar, params);
6983 		break;
6984 	case IEEE80211_AMPDU_TX_START:
6985 	case IEEE80211_AMPDU_TX_STOP_CONT:
6986 	case IEEE80211_AMPDU_TX_STOP_FLUSH:
6987 	case IEEE80211_AMPDU_TX_STOP_FLUSH_CONT:
6988 	case IEEE80211_AMPDU_TX_OPERATIONAL:
6989 		/* Tx A-MPDU aggregation offloaded to hw/fw so deny mac80211
6990 		 * Tx aggregation requests.
6991 		 */
6992 		ret = -EOPNOTSUPP;
6993 		break;
6994 	}
6995 
6996 	mutex_unlock(&ar->conf_mutex);
6997 
6998 	return ret;
6999 }
7000 
7001 static int ath11k_mac_op_add_chanctx(struct ieee80211_hw *hw,
7002 				     struct ieee80211_chanctx_conf *ctx)
7003 {
7004 	struct ath11k *ar = hw->priv;
7005 	struct ath11k_base *ab = ar->ab;
7006 
7007 	ath11k_dbg(ab, ATH11K_DBG_MAC,
7008 		   "mac chanctx add freq %u width %d ptr %pK\n",
7009 		   ctx->def.chan->center_freq, ctx->def.width, ctx);
7010 
7011 	mutex_lock(&ar->conf_mutex);
7012 
7013 	spin_lock_bh(&ar->data_lock);
7014 	/* TODO: In case of multiple channel context, populate rx_channel from
7015 	 * Rx PPDU desc information.
7016 	 */
7017 	ar->rx_channel = ctx->def.chan;
7018 	spin_unlock_bh(&ar->data_lock);
7019 
7020 	mutex_unlock(&ar->conf_mutex);
7021 
7022 	return 0;
7023 }
7024 
7025 static void ath11k_mac_op_remove_chanctx(struct ieee80211_hw *hw,
7026 					 struct ieee80211_chanctx_conf *ctx)
7027 {
7028 	struct ath11k *ar = hw->priv;
7029 	struct ath11k_base *ab = ar->ab;
7030 
7031 	ath11k_dbg(ab, ATH11K_DBG_MAC,
7032 		   "mac chanctx remove freq %u width %d ptr %pK\n",
7033 		   ctx->def.chan->center_freq, ctx->def.width, ctx);
7034 
7035 	mutex_lock(&ar->conf_mutex);
7036 
7037 	spin_lock_bh(&ar->data_lock);
7038 	/* TODO: In case of there is one more channel context left, populate
7039 	 * rx_channel with the channel of that remaining channel context.
7040 	 */
7041 	ar->rx_channel = NULL;
7042 	spin_unlock_bh(&ar->data_lock);
7043 
7044 	mutex_unlock(&ar->conf_mutex);
7045 }
7046 
7047 static int
7048 ath11k_mac_vdev_start_restart(struct ath11k_vif *arvif,
7049 			      struct ieee80211_chanctx_conf *ctx,
7050 			      bool restart)
7051 {
7052 	struct ath11k *ar = arvif->ar;
7053 	struct ath11k_base *ab = ar->ab;
7054 	struct wmi_vdev_start_req_arg arg = {};
7055 	const struct cfg80211_chan_def *chandef = &ctx->def;
7056 	int ret = 0;
7057 
7058 	lockdep_assert_held(&ar->conf_mutex);
7059 
7060 	reinit_completion(&ar->vdev_setup_done);
7061 
7062 	arg.vdev_id = arvif->vdev_id;
7063 	arg.dtim_period = arvif->dtim_period;
7064 	arg.bcn_intval = arvif->beacon_interval;
7065 
7066 	arg.channel.freq = chandef->chan->center_freq;
7067 	arg.channel.band_center_freq1 = chandef->center_freq1;
7068 	arg.channel.band_center_freq2 = chandef->center_freq2;
7069 	arg.channel.mode =
7070 		ath11k_phymodes[chandef->chan->band][chandef->width];
7071 
7072 	arg.channel.min_power = 0;
7073 	arg.channel.max_power = chandef->chan->max_power;
7074 	arg.channel.max_reg_power = chandef->chan->max_reg_power;
7075 	arg.channel.max_antenna_gain = chandef->chan->max_antenna_gain;
7076 
7077 	arg.pref_tx_streams = ar->num_tx_chains;
7078 	arg.pref_rx_streams = ar->num_rx_chains;
7079 
7080 	arg.mbssid_flags = 0;
7081 	arg.mbssid_tx_vdev_id = 0;
7082 	if (test_bit(WMI_TLV_SERVICE_MBSS_PARAM_IN_VDEV_START_SUPPORT,
7083 		     ar->ab->wmi_ab.svc_map)) {
7084 		ret = ath11k_mac_setup_vdev_params_mbssid(arvif,
7085 							  &arg.mbssid_flags,
7086 							  &arg.mbssid_tx_vdev_id);
7087 		if (ret)
7088 			return ret;
7089 	}
7090 
7091 	if (arvif->vdev_type == WMI_VDEV_TYPE_AP) {
7092 		arg.ssid = arvif->u.ap.ssid;
7093 		arg.ssid_len = arvif->u.ap.ssid_len;
7094 		arg.hidden_ssid = arvif->u.ap.hidden_ssid;
7095 
7096 		/* For now allow DFS for AP mode */
7097 		arg.channel.chan_radar =
7098 			!!(chandef->chan->flags & IEEE80211_CHAN_RADAR);
7099 
7100 		arg.channel.freq2_radar = ctx->radar_enabled;
7101 
7102 		arg.channel.passive = arg.channel.chan_radar;
7103 
7104 		spin_lock_bh(&ab->base_lock);
7105 		arg.regdomain = ar->ab->dfs_region;
7106 		spin_unlock_bh(&ab->base_lock);
7107 	}
7108 
7109 	arg.channel.passive |= !!(chandef->chan->flags & IEEE80211_CHAN_NO_IR);
7110 
7111 	ath11k_dbg(ab, ATH11K_DBG_MAC,
7112 		   "mac vdev %d start center_freq %d phymode %s\n",
7113 		   arg.vdev_id, arg.channel.freq,
7114 		   ath11k_wmi_phymode_str(arg.channel.mode));
7115 
7116 	ret = ath11k_wmi_vdev_start(ar, &arg, restart);
7117 	if (ret) {
7118 		ath11k_warn(ar->ab, "failed to %s WMI vdev %i\n",
7119 			    restart ? "restart" : "start", arg.vdev_id);
7120 		return ret;
7121 	}
7122 
7123 	ret = ath11k_mac_vdev_setup_sync(ar);
7124 	if (ret) {
7125 		ath11k_warn(ab, "failed to synchronize setup for vdev %i %s: %d\n",
7126 			    arg.vdev_id, restart ? "restart" : "start", ret);
7127 		return ret;
7128 	}
7129 
7130 	if (!restart)
7131 		ar->num_started_vdevs++;
7132 
7133 	ath11k_dbg(ab, ATH11K_DBG_MAC,  "vdev %pM started, vdev_id %d\n",
7134 		   arvif->vif->addr, arvif->vdev_id);
7135 
7136 	/* Enable CAC Flag in the driver by checking the channel DFS cac time,
7137 	 * i.e dfs_cac_ms value which will be valid only for radar channels
7138 	 * and state as NL80211_DFS_USABLE which indicates CAC needs to be
7139 	 * done before channel usage. This flags is used to drop rx packets.
7140 	 * during CAC.
7141 	 */
7142 	/* TODO Set the flag for other interface types as required */
7143 	if (arvif->vdev_type == WMI_VDEV_TYPE_AP &&
7144 	    chandef->chan->dfs_cac_ms &&
7145 	    chandef->chan->dfs_state == NL80211_DFS_USABLE) {
7146 		set_bit(ATH11K_CAC_RUNNING, &ar->dev_flags);
7147 		ath11k_dbg(ab, ATH11K_DBG_MAC,
7148 			   "CAC Started in chan_freq %d for vdev %d\n",
7149 			   arg.channel.freq, arg.vdev_id);
7150 	}
7151 
7152 	ret = ath11k_mac_set_txbf_conf(arvif);
7153 	if (ret)
7154 		ath11k_warn(ab, "failed to set txbf conf for vdev %d: %d\n",
7155 			    arvif->vdev_id, ret);
7156 
7157 	return 0;
7158 }
7159 
7160 static int ath11k_mac_vdev_stop(struct ath11k_vif *arvif)
7161 {
7162 	struct ath11k *ar = arvif->ar;
7163 	int ret;
7164 
7165 	lockdep_assert_held(&ar->conf_mutex);
7166 
7167 	reinit_completion(&ar->vdev_setup_done);
7168 
7169 	ret = ath11k_wmi_vdev_stop(ar, arvif->vdev_id);
7170 	if (ret) {
7171 		ath11k_warn(ar->ab, "failed to stop WMI vdev %i: %d\n",
7172 			    arvif->vdev_id, ret);
7173 		goto err;
7174 	}
7175 
7176 	ret = ath11k_mac_vdev_setup_sync(ar);
7177 	if (ret) {
7178 		ath11k_warn(ar->ab, "failed to synchronize setup for vdev %i: %d\n",
7179 			    arvif->vdev_id, ret);
7180 		goto err;
7181 	}
7182 
7183 	WARN_ON(ar->num_started_vdevs == 0);
7184 
7185 	ar->num_started_vdevs--;
7186 	ath11k_dbg(ar->ab, ATH11K_DBG_MAC, "vdev %pM stopped, vdev_id %d\n",
7187 		   arvif->vif->addr, arvif->vdev_id);
7188 
7189 	if (test_bit(ATH11K_CAC_RUNNING, &ar->dev_flags)) {
7190 		clear_bit(ATH11K_CAC_RUNNING, &ar->dev_flags);
7191 		ath11k_dbg(ar->ab, ATH11K_DBG_MAC, "CAC Stopped for vdev %d\n",
7192 			   arvif->vdev_id);
7193 	}
7194 
7195 	return 0;
7196 err:
7197 	return ret;
7198 }
7199 
7200 static int ath11k_mac_vdev_start(struct ath11k_vif *arvif,
7201 				 struct ieee80211_chanctx_conf *ctx)
7202 {
7203 	return ath11k_mac_vdev_start_restart(arvif, ctx, false);
7204 }
7205 
7206 static int ath11k_mac_vdev_restart(struct ath11k_vif *arvif,
7207 				   struct ieee80211_chanctx_conf *ctx)
7208 {
7209 	return ath11k_mac_vdev_start_restart(arvif, ctx, true);
7210 }
7211 
7212 struct ath11k_mac_change_chanctx_arg {
7213 	struct ieee80211_chanctx_conf *ctx;
7214 	struct ieee80211_vif_chanctx_switch *vifs;
7215 	int n_vifs;
7216 	int next_vif;
7217 };
7218 
7219 static void
7220 ath11k_mac_change_chanctx_cnt_iter(void *data, u8 *mac,
7221 				   struct ieee80211_vif *vif)
7222 {
7223 	struct ath11k_mac_change_chanctx_arg *arg = data;
7224 
7225 	if (rcu_access_pointer(vif->bss_conf.chanctx_conf) != arg->ctx)
7226 		return;
7227 
7228 	arg->n_vifs++;
7229 }
7230 
7231 static void
7232 ath11k_mac_change_chanctx_fill_iter(void *data, u8 *mac,
7233 				    struct ieee80211_vif *vif)
7234 {
7235 	struct ath11k_mac_change_chanctx_arg *arg = data;
7236 	struct ieee80211_chanctx_conf *ctx;
7237 
7238 	ctx = rcu_access_pointer(vif->bss_conf.chanctx_conf);
7239 	if (ctx != arg->ctx)
7240 		return;
7241 
7242 	if (WARN_ON(arg->next_vif == arg->n_vifs))
7243 		return;
7244 
7245 	arg->vifs[arg->next_vif].vif = vif;
7246 	arg->vifs[arg->next_vif].old_ctx = ctx;
7247 	arg->vifs[arg->next_vif].new_ctx = ctx;
7248 	arg->next_vif++;
7249 }
7250 
7251 static void
7252 ath11k_mac_update_vif_chan(struct ath11k *ar,
7253 			   struct ieee80211_vif_chanctx_switch *vifs,
7254 			   int n_vifs)
7255 {
7256 	struct ath11k_base *ab = ar->ab;
7257 	struct ath11k_vif *arvif, *tx_arvif = NULL;
7258 	struct ieee80211_vif *mbssid_tx_vif;
7259 	int ret;
7260 	int i;
7261 	bool monitor_vif = false;
7262 
7263 	lockdep_assert_held(&ar->conf_mutex);
7264 
7265 	/* Associated channel resources of all relevant vdevs
7266 	 * should be available for the channel switch now.
7267 	 */
7268 
7269 	/* TODO: Update ar->rx_channel */
7270 
7271 	for (i = 0; i < n_vifs; i++) {
7272 		arvif = (void *)vifs[i].vif->drv_priv;
7273 
7274 		if (WARN_ON(!arvif->is_started))
7275 			continue;
7276 
7277 		/* change_chanctx can be called even before vdev_up from
7278 		 * ieee80211_start_ap->ieee80211_vif_use_channel->
7279 		 * ieee80211_recalc_radar_chanctx.
7280 		 *
7281 		 * Firmware expect vdev_restart only if vdev is up.
7282 		 * If vdev is down then it expect vdev_stop->vdev_start.
7283 		 */
7284 		if (arvif->is_up) {
7285 			ret = ath11k_mac_vdev_restart(arvif, vifs[i].new_ctx);
7286 			if (ret) {
7287 				ath11k_warn(ab, "failed to restart vdev %d: %d\n",
7288 					    arvif->vdev_id, ret);
7289 				continue;
7290 			}
7291 		} else {
7292 			ret = ath11k_mac_vdev_stop(arvif);
7293 			if (ret) {
7294 				ath11k_warn(ab, "failed to stop vdev %d: %d\n",
7295 					    arvif->vdev_id, ret);
7296 				continue;
7297 			}
7298 
7299 			ret = ath11k_mac_vdev_start(arvif, vifs[i].new_ctx);
7300 			if (ret)
7301 				ath11k_warn(ab, "failed to start vdev %d: %d\n",
7302 					    arvif->vdev_id, ret);
7303 
7304 			continue;
7305 		}
7306 
7307 		ret = ath11k_mac_setup_bcn_tmpl(arvif);
7308 		if (ret)
7309 			ath11k_warn(ab, "failed to update bcn tmpl during csa: %d\n",
7310 				    ret);
7311 
7312 		mbssid_tx_vif = arvif->vif->mbssid_tx_vif;
7313 		if (mbssid_tx_vif)
7314 			tx_arvif = (struct ath11k_vif *)mbssid_tx_vif->drv_priv;
7315 
7316 		ret = ath11k_wmi_vdev_up(arvif->ar, arvif->vdev_id, arvif->aid,
7317 					 arvif->bssid,
7318 					 tx_arvif ? tx_arvif->bssid : NULL,
7319 					 arvif->vif->bss_conf.bssid_index,
7320 					 1 << arvif->vif->bss_conf.bssid_indicator);
7321 		if (ret) {
7322 			ath11k_warn(ab, "failed to bring vdev up %d: %d\n",
7323 				    arvif->vdev_id, ret);
7324 			continue;
7325 		}
7326 	}
7327 
7328 	/* Restart the internal monitor vdev on new channel */
7329 	if (!monitor_vif &&
7330 	    test_bit(ATH11K_FLAG_MONITOR_VDEV_CREATED, &ar->monitor_flags)) {
7331 		ret = ath11k_mac_monitor_stop(ar);
7332 		if (ret) {
7333 			ath11k_warn(ar->ab, "failed to stop monitor during vif channel update: %d",
7334 				    ret);
7335 			return;
7336 		}
7337 
7338 		ret = ath11k_mac_monitor_start(ar);
7339 		if (ret) {
7340 			ath11k_warn(ar->ab, "failed to start monitor during vif channel update: %d",
7341 				    ret);
7342 			return;
7343 		}
7344 	}
7345 }
7346 
7347 static void
7348 ath11k_mac_update_active_vif_chan(struct ath11k *ar,
7349 				  struct ieee80211_chanctx_conf *ctx)
7350 {
7351 	struct ath11k_mac_change_chanctx_arg arg = { .ctx = ctx };
7352 
7353 	lockdep_assert_held(&ar->conf_mutex);
7354 
7355 	ieee80211_iterate_active_interfaces_atomic(ar->hw,
7356 						   IEEE80211_IFACE_ITER_NORMAL,
7357 						   ath11k_mac_change_chanctx_cnt_iter,
7358 						   &arg);
7359 	if (arg.n_vifs == 0)
7360 		return;
7361 
7362 	arg.vifs = kcalloc(arg.n_vifs, sizeof(arg.vifs[0]), GFP_KERNEL);
7363 	if (!arg.vifs)
7364 		return;
7365 
7366 	ieee80211_iterate_active_interfaces_atomic(ar->hw,
7367 						   IEEE80211_IFACE_ITER_NORMAL,
7368 						   ath11k_mac_change_chanctx_fill_iter,
7369 						   &arg);
7370 
7371 	ath11k_mac_update_vif_chan(ar, arg.vifs, arg.n_vifs);
7372 
7373 	kfree(arg.vifs);
7374 }
7375 
7376 static void ath11k_mac_op_change_chanctx(struct ieee80211_hw *hw,
7377 					 struct ieee80211_chanctx_conf *ctx,
7378 					 u32 changed)
7379 {
7380 	struct ath11k *ar = hw->priv;
7381 	struct ath11k_base *ab = ar->ab;
7382 
7383 	mutex_lock(&ar->conf_mutex);
7384 
7385 	ath11k_dbg(ab, ATH11K_DBG_MAC,
7386 		   "mac chanctx change freq %u width %d ptr %pK changed %x\n",
7387 		   ctx->def.chan->center_freq, ctx->def.width, ctx, changed);
7388 
7389 	/* This shouldn't really happen because channel switching should use
7390 	 * switch_vif_chanctx().
7391 	 */
7392 	if (WARN_ON(changed & IEEE80211_CHANCTX_CHANGE_CHANNEL))
7393 		goto unlock;
7394 
7395 	if (changed & IEEE80211_CHANCTX_CHANGE_WIDTH ||
7396 	    changed & IEEE80211_CHANCTX_CHANGE_RADAR)
7397 		ath11k_mac_update_active_vif_chan(ar, ctx);
7398 
7399 	/* TODO: Recalc radar detection */
7400 
7401 unlock:
7402 	mutex_unlock(&ar->conf_mutex);
7403 }
7404 
7405 static int ath11k_start_vdev_delay(struct ieee80211_hw *hw,
7406 				   struct ieee80211_vif *vif)
7407 {
7408 	struct ath11k *ar = hw->priv;
7409 	struct ath11k_base *ab = ar->ab;
7410 	struct ath11k_vif *arvif = (void *)vif->drv_priv;
7411 	int ret;
7412 
7413 	if (WARN_ON(arvif->is_started))
7414 		return -EBUSY;
7415 
7416 	ret = ath11k_mac_vdev_start(arvif, &arvif->chanctx);
7417 	if (ret) {
7418 		ath11k_warn(ab, "failed to start vdev %i addr %pM on freq %d: %d\n",
7419 			    arvif->vdev_id, vif->addr,
7420 			    arvif->chanctx.def.chan->center_freq, ret);
7421 		return ret;
7422 	}
7423 
7424 	/* Reconfigure hardware rate code since it is cleared by firmware.
7425 	 */
7426 	if (ar->hw_rate_code > 0) {
7427 		u32 vdev_param = WMI_VDEV_PARAM_MGMT_RATE;
7428 
7429 		ret = ath11k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id, vdev_param,
7430 						    ar->hw_rate_code);
7431 		if (ret) {
7432 			ath11k_warn(ar->ab, "failed to set mgmt tx rate %d\n", ret);
7433 			return ret;
7434 		}
7435 	}
7436 
7437 	if (arvif->vdev_type == WMI_VDEV_TYPE_MONITOR) {
7438 		ret = ath11k_wmi_vdev_up(ar, arvif->vdev_id, 0, ar->mac_addr,
7439 					 NULL, 0, 0);
7440 		if (ret) {
7441 			ath11k_warn(ab, "failed put monitor up: %d\n", ret);
7442 			return ret;
7443 		}
7444 	}
7445 
7446 	arvif->is_started = true;
7447 
7448 	/* TODO: Setup ps and cts/rts protection */
7449 	return 0;
7450 }
7451 
7452 static int
7453 ath11k_mac_op_assign_vif_chanctx(struct ieee80211_hw *hw,
7454 				 struct ieee80211_vif *vif,
7455 				 struct ieee80211_bss_conf *link_conf,
7456 				 struct ieee80211_chanctx_conf *ctx)
7457 {
7458 	struct ath11k *ar = hw->priv;
7459 	struct ath11k_base *ab = ar->ab;
7460 	struct ath11k_vif *arvif = (void *)vif->drv_priv;
7461 	int ret;
7462 	struct peer_create_params param;
7463 
7464 	mutex_lock(&ar->conf_mutex);
7465 
7466 	ath11k_dbg(ab, ATH11K_DBG_MAC,
7467 		   "mac chanctx assign ptr %pK vdev_id %i\n",
7468 		   ctx, arvif->vdev_id);
7469 
7470 	/* for QCA6390 bss peer must be created before vdev_start */
7471 	if (ab->hw_params.vdev_start_delay &&
7472 	    arvif->vdev_type != WMI_VDEV_TYPE_AP &&
7473 	    arvif->vdev_type != WMI_VDEV_TYPE_MONITOR &&
7474 	    !ath11k_peer_find_by_vdev_id(ab, arvif->vdev_id)) {
7475 		memcpy(&arvif->chanctx, ctx, sizeof(*ctx));
7476 		ret = 0;
7477 		goto out;
7478 	}
7479 
7480 	if (WARN_ON(arvif->is_started)) {
7481 		ret = -EBUSY;
7482 		goto out;
7483 	}
7484 
7485 	if (ab->hw_params.vdev_start_delay &&
7486 	    arvif->vdev_type != WMI_VDEV_TYPE_AP &&
7487 	    arvif->vdev_type != WMI_VDEV_TYPE_MONITOR) {
7488 		param.vdev_id = arvif->vdev_id;
7489 		param.peer_type = WMI_PEER_TYPE_DEFAULT;
7490 		param.peer_addr = ar->mac_addr;
7491 
7492 		ret = ath11k_peer_create(ar, arvif, NULL, &param);
7493 		if (ret) {
7494 			ath11k_warn(ab, "failed to create peer after vdev start delay: %d",
7495 				    ret);
7496 			goto out;
7497 		}
7498 	}
7499 
7500 	if (arvif->vdev_type == WMI_VDEV_TYPE_MONITOR) {
7501 		ret = ath11k_mac_monitor_start(ar);
7502 		if (ret) {
7503 			ath11k_warn(ar->ab, "failed to start monitor during vif channel context assignment: %d",
7504 				    ret);
7505 			goto out;
7506 		}
7507 
7508 		arvif->is_started = true;
7509 		goto out;
7510 	}
7511 
7512 	ret = ath11k_mac_vdev_start(arvif, ctx);
7513 	if (ret) {
7514 		ath11k_warn(ab, "failed to start vdev %i addr %pM on freq %d: %d\n",
7515 			    arvif->vdev_id, vif->addr,
7516 			    ctx->def.chan->center_freq, ret);
7517 		goto out;
7518 	}
7519 
7520 	arvif->is_started = true;
7521 
7522 	if (arvif->vdev_type != WMI_VDEV_TYPE_MONITOR &&
7523 	    test_bit(ATH11K_FLAG_MONITOR_VDEV_CREATED, &ar->monitor_flags)) {
7524 		ret = ath11k_mac_monitor_start(ar);
7525 		if (ret) {
7526 			ath11k_warn(ar->ab, "failed to start monitor during vif channel context assignment: %d",
7527 				    ret);
7528 			goto out;
7529 		}
7530 	}
7531 
7532 	/* TODO: Setup ps and cts/rts protection */
7533 
7534 	ret = 0;
7535 
7536 out:
7537 	mutex_unlock(&ar->conf_mutex);
7538 
7539 	return ret;
7540 }
7541 
7542 static void
7543 ath11k_mac_op_unassign_vif_chanctx(struct ieee80211_hw *hw,
7544 				   struct ieee80211_vif *vif,
7545 				   struct ieee80211_bss_conf *link_conf,
7546 				   struct ieee80211_chanctx_conf *ctx)
7547 {
7548 	struct ath11k *ar = hw->priv;
7549 	struct ath11k_base *ab = ar->ab;
7550 	struct ath11k_vif *arvif = (void *)vif->drv_priv;
7551 	struct ath11k_peer *peer;
7552 	int ret;
7553 
7554 	mutex_lock(&ar->conf_mutex);
7555 
7556 	ath11k_dbg(ab, ATH11K_DBG_MAC,
7557 		   "mac chanctx unassign ptr %pK vdev_id %i\n",
7558 		   ctx, arvif->vdev_id);
7559 
7560 	WARN_ON(!arvif->is_started);
7561 
7562 	if (ab->hw_params.vdev_start_delay &&
7563 	    arvif->vdev_type == WMI_VDEV_TYPE_MONITOR) {
7564 		spin_lock_bh(&ab->base_lock);
7565 		peer = ath11k_peer_find_by_addr(ab, ar->mac_addr);
7566 		spin_unlock_bh(&ab->base_lock);
7567 		if (peer)
7568 			ath11k_peer_delete(ar, arvif->vdev_id, ar->mac_addr);
7569 	}
7570 
7571 	if (arvif->vdev_type == WMI_VDEV_TYPE_MONITOR) {
7572 		ret = ath11k_mac_monitor_stop(ar);
7573 		if (ret) {
7574 			ath11k_warn(ar->ab, "failed to stop monitor during vif channel context unassignment: %d",
7575 				    ret);
7576 			mutex_unlock(&ar->conf_mutex);
7577 			return;
7578 		}
7579 
7580 		arvif->is_started = false;
7581 		mutex_unlock(&ar->conf_mutex);
7582 		return;
7583 	}
7584 
7585 	ret = ath11k_mac_vdev_stop(arvif);
7586 	if (ret)
7587 		ath11k_warn(ab, "failed to stop vdev %i: %d\n",
7588 			    arvif->vdev_id, ret);
7589 
7590 	arvif->is_started = false;
7591 
7592 	if (ab->hw_params.vdev_start_delay &&
7593 	    arvif->vdev_type == WMI_VDEV_TYPE_STA) {
7594 		ret = ath11k_peer_delete(ar, arvif->vdev_id, arvif->bssid);
7595 		if (ret)
7596 			ath11k_warn(ar->ab,
7597 				    "failed to delete peer %pM for vdev %d: %d\n",
7598 				    arvif->bssid, arvif->vdev_id, ret);
7599 		else
7600 			ath11k_dbg(ar->ab, ATH11K_DBG_MAC,
7601 				   "mac removed peer %pM  vdev %d after vdev stop\n",
7602 				   arvif->bssid, arvif->vdev_id);
7603 	}
7604 
7605 	if (ab->hw_params.vdev_start_delay &&
7606 	    arvif->vdev_type == WMI_VDEV_TYPE_MONITOR)
7607 		ath11k_wmi_vdev_down(ar, arvif->vdev_id);
7608 
7609 	if (arvif->vdev_type != WMI_VDEV_TYPE_MONITOR &&
7610 	    ar->num_started_vdevs == 1 &&
7611 	    test_bit(ATH11K_FLAG_MONITOR_VDEV_CREATED, &ar->monitor_flags)) {
7612 		ret = ath11k_mac_monitor_stop(ar);
7613 		if (ret)
7614 			/* continue even if there's an error */
7615 			ath11k_warn(ar->ab, "failed to stop monitor during vif channel context unassignment: %d",
7616 				    ret);
7617 	}
7618 
7619 	if (arvif->vdev_type == WMI_VDEV_TYPE_STA)
7620 		ath11k_mac_11d_scan_start(ar, arvif->vdev_id);
7621 
7622 	mutex_unlock(&ar->conf_mutex);
7623 }
7624 
7625 static int
7626 ath11k_mac_op_switch_vif_chanctx(struct ieee80211_hw *hw,
7627 				 struct ieee80211_vif_chanctx_switch *vifs,
7628 				 int n_vifs,
7629 				 enum ieee80211_chanctx_switch_mode mode)
7630 {
7631 	struct ath11k *ar = hw->priv;
7632 
7633 	mutex_lock(&ar->conf_mutex);
7634 
7635 	ath11k_dbg(ar->ab, ATH11K_DBG_MAC,
7636 		   "mac chanctx switch n_vifs %d mode %d\n",
7637 		   n_vifs, mode);
7638 	ath11k_mac_update_vif_chan(ar, vifs, n_vifs);
7639 
7640 	mutex_unlock(&ar->conf_mutex);
7641 
7642 	return 0;
7643 }
7644 
7645 static int
7646 ath11k_set_vdev_param_to_all_vifs(struct ath11k *ar, int param, u32 value)
7647 {
7648 	struct ath11k_vif *arvif;
7649 	int ret = 0;
7650 
7651 	mutex_lock(&ar->conf_mutex);
7652 	list_for_each_entry(arvif, &ar->arvifs, list) {
7653 		ath11k_dbg(ar->ab, ATH11K_DBG_MAC, "setting mac vdev %d param %d value %d\n",
7654 			   param, arvif->vdev_id, value);
7655 
7656 		ret = ath11k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
7657 						    param, value);
7658 		if (ret) {
7659 			ath11k_warn(ar->ab, "failed to set param %d for vdev %d: %d\n",
7660 				    param, arvif->vdev_id, ret);
7661 			break;
7662 		}
7663 	}
7664 	mutex_unlock(&ar->conf_mutex);
7665 	return ret;
7666 }
7667 
7668 /* mac80211 stores device specific RTS/Fragmentation threshold value,
7669  * this is set interface specific to firmware from ath11k driver
7670  */
7671 static int ath11k_mac_op_set_rts_threshold(struct ieee80211_hw *hw, u32 value)
7672 {
7673 	struct ath11k *ar = hw->priv;
7674 	int param_id = WMI_VDEV_PARAM_RTS_THRESHOLD;
7675 
7676 	return ath11k_set_vdev_param_to_all_vifs(ar, param_id, value);
7677 }
7678 
7679 static int ath11k_mac_op_set_frag_threshold(struct ieee80211_hw *hw, u32 value)
7680 {
7681 	/* Even though there's a WMI vdev param for fragmentation threshold no
7682 	 * known firmware actually implements it. Moreover it is not possible to
7683 	 * rely frame fragmentation to mac80211 because firmware clears the
7684 	 * "more fragments" bit in frame control making it impossible for remote
7685 	 * devices to reassemble frames.
7686 	 *
7687 	 * Hence implement a dummy callback just to say fragmentation isn't
7688 	 * supported. This effectively prevents mac80211 from doing frame
7689 	 * fragmentation in software.
7690 	 */
7691 	return -EOPNOTSUPP;
7692 }
7693 
7694 static int ath11k_mac_flush_tx_complete(struct ath11k *ar)
7695 {
7696 	long time_left;
7697 	int ret = 0;
7698 
7699 	time_left = wait_event_timeout(ar->dp.tx_empty_waitq,
7700 				       (atomic_read(&ar->dp.num_tx_pending) == 0),
7701 				       ATH11K_FLUSH_TIMEOUT);
7702 	if (time_left == 0) {
7703 		ath11k_warn(ar->ab, "failed to flush transmit queue, data pkts pending %d\n",
7704 			    atomic_read(&ar->dp.num_tx_pending));
7705 		ret = -ETIMEDOUT;
7706 	}
7707 
7708 	time_left = wait_event_timeout(ar->txmgmt_empty_waitq,
7709 				       (atomic_read(&ar->num_pending_mgmt_tx) == 0),
7710 				       ATH11K_FLUSH_TIMEOUT);
7711 	if (time_left == 0) {
7712 		ath11k_warn(ar->ab, "failed to flush mgmt transmit queue, mgmt pkts pending %d\n",
7713 			    atomic_read(&ar->num_pending_mgmt_tx));
7714 		ret = -ETIMEDOUT;
7715 	}
7716 
7717 	return ret;
7718 }
7719 
7720 int ath11k_mac_wait_tx_complete(struct ath11k *ar)
7721 {
7722 	ath11k_mac_drain_tx(ar);
7723 	return ath11k_mac_flush_tx_complete(ar);
7724 }
7725 
7726 static void ath11k_mac_op_flush(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
7727 				u32 queues, bool drop)
7728 {
7729 	struct ath11k *ar = hw->priv;
7730 
7731 	if (drop)
7732 		return;
7733 
7734 	ath11k_mac_flush_tx_complete(ar);
7735 }
7736 
7737 static int
7738 ath11k_mac_bitrate_mask_num_ht_rates(struct ath11k *ar,
7739 				     enum nl80211_band band,
7740 				     const struct cfg80211_bitrate_mask *mask)
7741 {
7742 	int num_rates = 0;
7743 	int i;
7744 
7745 	for (i = 0; i < ARRAY_SIZE(mask->control[band].ht_mcs); i++)
7746 		num_rates += hweight16(mask->control[band].ht_mcs[i]);
7747 
7748 	return num_rates;
7749 }
7750 
7751 static bool
7752 ath11k_mac_has_single_legacy_rate(struct ath11k *ar,
7753 				  enum nl80211_band band,
7754 				  const struct cfg80211_bitrate_mask *mask)
7755 {
7756 	int num_rates = 0;
7757 
7758 	num_rates = hweight32(mask->control[band].legacy);
7759 
7760 	if (ath11k_mac_bitrate_mask_num_ht_rates(ar, band, mask))
7761 		return false;
7762 
7763 	if (ath11k_mac_bitrate_mask_num_vht_rates(ar, band, mask))
7764 		return false;
7765 
7766 	if (ath11k_mac_bitrate_mask_num_he_rates(ar, band, mask))
7767 		return false;
7768 
7769 	return num_rates == 1;
7770 }
7771 
7772 static __le16
7773 ath11k_mac_get_tx_mcs_map(const struct ieee80211_sta_he_cap *he_cap)
7774 {
7775 	if (he_cap->he_cap_elem.phy_cap_info[0] &
7776 	    IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_80PLUS80_MHZ_IN_5G)
7777 		return he_cap->he_mcs_nss_supp.tx_mcs_80p80;
7778 
7779 	if (he_cap->he_cap_elem.phy_cap_info[0] &
7780 	    IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_160MHZ_IN_5G)
7781 		return he_cap->he_mcs_nss_supp.tx_mcs_160;
7782 
7783 	return he_cap->he_mcs_nss_supp.tx_mcs_80;
7784 }
7785 
7786 static bool
7787 ath11k_mac_bitrate_mask_get_single_nss(struct ath11k *ar,
7788 				       enum nl80211_band band,
7789 				       const struct cfg80211_bitrate_mask *mask,
7790 				       int *nss)
7791 {
7792 	struct ieee80211_supported_band *sband = &ar->mac.sbands[band];
7793 	u16 vht_mcs_map = le16_to_cpu(sband->vht_cap.vht_mcs.tx_mcs_map);
7794 	u16 he_mcs_map = 0;
7795 	u8 ht_nss_mask = 0;
7796 	u8 vht_nss_mask = 0;
7797 	u8 he_nss_mask = 0;
7798 	int i;
7799 
7800 	/* No need to consider legacy here. Basic rates are always present
7801 	 * in bitrate mask
7802 	 */
7803 
7804 	for (i = 0; i < ARRAY_SIZE(mask->control[band].ht_mcs); i++) {
7805 		if (mask->control[band].ht_mcs[i] == 0)
7806 			continue;
7807 		else if (mask->control[band].ht_mcs[i] ==
7808 			 sband->ht_cap.mcs.rx_mask[i])
7809 			ht_nss_mask |= BIT(i);
7810 		else
7811 			return false;
7812 	}
7813 
7814 	for (i = 0; i < ARRAY_SIZE(mask->control[band].vht_mcs); i++) {
7815 		if (mask->control[band].vht_mcs[i] == 0)
7816 			continue;
7817 		else if (mask->control[band].vht_mcs[i] ==
7818 			 ath11k_mac_get_max_vht_mcs_map(vht_mcs_map, i))
7819 			vht_nss_mask |= BIT(i);
7820 		else
7821 			return false;
7822 	}
7823 
7824 	he_mcs_map = le16_to_cpu(ath11k_mac_get_tx_mcs_map(&sband->iftype_data->he_cap));
7825 
7826 	for (i = 0; i < ARRAY_SIZE(mask->control[band].he_mcs); i++) {
7827 		if (mask->control[band].he_mcs[i] == 0)
7828 			continue;
7829 
7830 		if (mask->control[band].he_mcs[i] ==
7831 		    ath11k_mac_get_max_he_mcs_map(he_mcs_map, i))
7832 			he_nss_mask |= BIT(i);
7833 		else
7834 			return false;
7835 	}
7836 
7837 	if (ht_nss_mask != vht_nss_mask || ht_nss_mask != he_nss_mask)
7838 		return false;
7839 
7840 	if (ht_nss_mask == 0)
7841 		return false;
7842 
7843 	if (BIT(fls(ht_nss_mask)) - 1 != ht_nss_mask)
7844 		return false;
7845 
7846 	*nss = fls(ht_nss_mask);
7847 
7848 	return true;
7849 }
7850 
7851 static int
7852 ath11k_mac_get_single_legacy_rate(struct ath11k *ar,
7853 				  enum nl80211_band band,
7854 				  const struct cfg80211_bitrate_mask *mask,
7855 				  u32 *rate, u8 *nss)
7856 {
7857 	int rate_idx;
7858 	u16 bitrate;
7859 	u8 preamble;
7860 	u8 hw_rate;
7861 
7862 	if (hweight32(mask->control[band].legacy) != 1)
7863 		return -EINVAL;
7864 
7865 	rate_idx = ffs(mask->control[band].legacy) - 1;
7866 
7867 	if (band == NL80211_BAND_5GHZ || band == NL80211_BAND_6GHZ)
7868 		rate_idx += ATH11K_MAC_FIRST_OFDM_RATE_IDX;
7869 
7870 	hw_rate = ath11k_legacy_rates[rate_idx].hw_value;
7871 	bitrate = ath11k_legacy_rates[rate_idx].bitrate;
7872 
7873 	if (ath11k_mac_bitrate_is_cck(bitrate))
7874 		preamble = WMI_RATE_PREAMBLE_CCK;
7875 	else
7876 		preamble = WMI_RATE_PREAMBLE_OFDM;
7877 
7878 	*nss = 1;
7879 	*rate = ATH11K_HW_RATE_CODE(hw_rate, 0, preamble);
7880 
7881 	return 0;
7882 }
7883 
7884 static int
7885 ath11k_mac_set_fixed_rate_gi_ltf(struct ath11k_vif *arvif, u8 he_gi, u8 he_ltf)
7886 {
7887 	struct ath11k *ar = arvif->ar;
7888 	int ret;
7889 
7890 	/* 0.8 = 0, 1.6 = 2 and 3.2 = 3. */
7891 	if (he_gi && he_gi != 0xFF)
7892 		he_gi += 1;
7893 
7894 	ret = ath11k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
7895 					    WMI_VDEV_PARAM_SGI, he_gi);
7896 	if (ret) {
7897 		ath11k_warn(ar->ab, "failed to set he gi %d: %d\n",
7898 			    he_gi, ret);
7899 		return ret;
7900 	}
7901 	/* start from 1 */
7902 	if (he_ltf != 0xFF)
7903 		he_ltf += 1;
7904 
7905 	ret = ath11k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
7906 					    WMI_VDEV_PARAM_HE_LTF, he_ltf);
7907 	if (ret) {
7908 		ath11k_warn(ar->ab, "failed to set he ltf %d: %d\n",
7909 			    he_ltf, ret);
7910 		return ret;
7911 	}
7912 
7913 	return 0;
7914 }
7915 
7916 static int
7917 ath11k_mac_set_auto_rate_gi_ltf(struct ath11k_vif *arvif, u16 he_gi, u8 he_ltf)
7918 {
7919 	struct ath11k *ar = arvif->ar;
7920 	int ret;
7921 	u32 he_ar_gi_ltf;
7922 
7923 	if (he_gi != 0xFF) {
7924 		switch (he_gi) {
7925 		case NL80211_RATE_INFO_HE_GI_0_8:
7926 			he_gi = WMI_AUTORATE_800NS_GI;
7927 			break;
7928 		case NL80211_RATE_INFO_HE_GI_1_6:
7929 			he_gi = WMI_AUTORATE_1600NS_GI;
7930 			break;
7931 		case NL80211_RATE_INFO_HE_GI_3_2:
7932 			he_gi = WMI_AUTORATE_3200NS_GI;
7933 			break;
7934 		default:
7935 			ath11k_warn(ar->ab, "invalid he gi: %d\n", he_gi);
7936 			return -EINVAL;
7937 		}
7938 	}
7939 
7940 	if (he_ltf != 0xFF) {
7941 		switch (he_ltf) {
7942 		case NL80211_RATE_INFO_HE_1XLTF:
7943 			he_ltf = WMI_HE_AUTORATE_LTF_1X;
7944 			break;
7945 		case NL80211_RATE_INFO_HE_2XLTF:
7946 			he_ltf = WMI_HE_AUTORATE_LTF_2X;
7947 			break;
7948 		case NL80211_RATE_INFO_HE_4XLTF:
7949 			he_ltf = WMI_HE_AUTORATE_LTF_4X;
7950 			break;
7951 		default:
7952 			ath11k_warn(ar->ab, "invalid he ltf: %d\n", he_ltf);
7953 			return -EINVAL;
7954 		}
7955 	}
7956 
7957 	he_ar_gi_ltf = he_gi | he_ltf;
7958 	ret = ath11k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
7959 					    WMI_VDEV_PARAM_AUTORATE_MISC_CFG,
7960 					    he_ar_gi_ltf);
7961 	if (ret) {
7962 		ath11k_warn(ar->ab,
7963 			    "failed to set he autorate gi %u ltf %u: %d\n",
7964 			    he_gi, he_ltf, ret);
7965 		return ret;
7966 	}
7967 
7968 	return 0;
7969 }
7970 
7971 static int ath11k_mac_set_rate_params(struct ath11k_vif *arvif,
7972 				      u32 rate, u8 nss, u8 sgi, u8 ldpc,
7973 				      u8 he_gi, u8 he_ltf, bool he_fixed_rate)
7974 {
7975 	struct ath11k *ar = arvif->ar;
7976 	u32 vdev_param;
7977 	int ret;
7978 
7979 	lockdep_assert_held(&ar->conf_mutex);
7980 
7981 	ath11k_dbg(ar->ab, ATH11K_DBG_MAC,
7982 		   "mac set rate params vdev %i rate 0x%02x nss 0x%02x sgi 0x%02x ldpc 0x%02x he_gi 0x%02x he_ltf 0x%02x he_fixed_rate %d\n",
7983 		   arvif->vdev_id, rate, nss, sgi, ldpc, he_gi,
7984 		   he_ltf, he_fixed_rate);
7985 
7986 	if (!arvif->vif->bss_conf.he_support) {
7987 		vdev_param = WMI_VDEV_PARAM_FIXED_RATE;
7988 		ret = ath11k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
7989 						    vdev_param, rate);
7990 		if (ret) {
7991 			ath11k_warn(ar->ab, "failed to set fixed rate param 0x%02x: %d\n",
7992 				    rate, ret);
7993 			return ret;
7994 		}
7995 	}
7996 
7997 	vdev_param = WMI_VDEV_PARAM_NSS;
7998 	ret = ath11k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
7999 					    vdev_param, nss);
8000 	if (ret) {
8001 		ath11k_warn(ar->ab, "failed to set nss param %d: %d\n",
8002 			    nss, ret);
8003 		return ret;
8004 	}
8005 
8006 	vdev_param = WMI_VDEV_PARAM_LDPC;
8007 	ret = ath11k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
8008 					    vdev_param, ldpc);
8009 	if (ret) {
8010 		ath11k_warn(ar->ab, "failed to set ldpc param %d: %d\n",
8011 			    ldpc, ret);
8012 		return ret;
8013 	}
8014 
8015 	if (arvif->vif->bss_conf.he_support) {
8016 		if (he_fixed_rate) {
8017 			ret = ath11k_mac_set_fixed_rate_gi_ltf(arvif, he_gi,
8018 							       he_ltf);
8019 			if (ret) {
8020 				ath11k_warn(ar->ab, "failed to set fixed rate gi ltf: %d\n",
8021 					    ret);
8022 				return ret;
8023 			}
8024 		} else {
8025 			ret = ath11k_mac_set_auto_rate_gi_ltf(arvif, he_gi,
8026 							      he_ltf);
8027 			if (ret) {
8028 				ath11k_warn(ar->ab, "failed to set auto rate gi ltf: %d\n",
8029 					    ret);
8030 				return ret;
8031 			}
8032 		}
8033 	} else {
8034 		vdev_param = WMI_VDEV_PARAM_SGI;
8035 		ret = ath11k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
8036 						    vdev_param, sgi);
8037 		if (ret) {
8038 			ath11k_warn(ar->ab, "failed to set sgi param %d: %d\n",
8039 				    sgi, ret);
8040 			return ret;
8041 		}
8042 	}
8043 
8044 	return 0;
8045 }
8046 
8047 static bool
8048 ath11k_mac_vht_mcs_range_present(struct ath11k *ar,
8049 				 enum nl80211_band band,
8050 				 const struct cfg80211_bitrate_mask *mask)
8051 {
8052 	int i;
8053 	u16 vht_mcs;
8054 
8055 	for (i = 0; i < NL80211_VHT_NSS_MAX; i++) {
8056 		vht_mcs = mask->control[band].vht_mcs[i];
8057 
8058 		switch (vht_mcs) {
8059 		case 0:
8060 		case BIT(8) - 1:
8061 		case BIT(9) - 1:
8062 		case BIT(10) - 1:
8063 			break;
8064 		default:
8065 			return false;
8066 		}
8067 	}
8068 
8069 	return true;
8070 }
8071 
8072 static bool
8073 ath11k_mac_he_mcs_range_present(struct ath11k *ar,
8074 				enum nl80211_band band,
8075 				const struct cfg80211_bitrate_mask *mask)
8076 {
8077 	int i;
8078 	u16 he_mcs;
8079 
8080 	for (i = 0; i < NL80211_HE_NSS_MAX; i++) {
8081 		he_mcs = mask->control[band].he_mcs[i];
8082 
8083 		switch (he_mcs) {
8084 		case 0:
8085 		case BIT(8) - 1:
8086 		case BIT(10) - 1:
8087 		case BIT(12) - 1:
8088 			break;
8089 		default:
8090 			return false;
8091 		}
8092 	}
8093 
8094 	return true;
8095 }
8096 
8097 static void ath11k_mac_set_bitrate_mask_iter(void *data,
8098 					     struct ieee80211_sta *sta)
8099 {
8100 	struct ath11k_vif *arvif = data;
8101 	struct ath11k_sta *arsta = (struct ath11k_sta *)sta->drv_priv;
8102 	struct ath11k *ar = arvif->ar;
8103 
8104 	spin_lock_bh(&ar->data_lock);
8105 	arsta->changed |= IEEE80211_RC_SUPP_RATES_CHANGED;
8106 	spin_unlock_bh(&ar->data_lock);
8107 
8108 	ieee80211_queue_work(ar->hw, &arsta->update_wk);
8109 }
8110 
8111 static void ath11k_mac_disable_peer_fixed_rate(void *data,
8112 					       struct ieee80211_sta *sta)
8113 {
8114 	struct ath11k_vif *arvif = data;
8115 	struct ath11k *ar = arvif->ar;
8116 	int ret;
8117 
8118 	ret = ath11k_wmi_set_peer_param(ar, sta->addr,
8119 					arvif->vdev_id,
8120 					WMI_PEER_PARAM_FIXED_RATE,
8121 					WMI_FIXED_RATE_NONE);
8122 	if (ret)
8123 		ath11k_warn(ar->ab,
8124 			    "failed to disable peer fixed rate for STA %pM ret %d\n",
8125 			    sta->addr, ret);
8126 }
8127 
8128 static bool
8129 ath11k_mac_validate_vht_he_fixed_rate_settings(struct ath11k *ar, enum nl80211_band band,
8130 					       const struct cfg80211_bitrate_mask *mask)
8131 {
8132 	bool he_fixed_rate = false, vht_fixed_rate = false;
8133 	struct ath11k_peer *peer, *tmp;
8134 	const u16 *vht_mcs_mask, *he_mcs_mask;
8135 	struct ieee80211_link_sta *deflink;
8136 	u8 vht_nss, he_nss;
8137 	bool ret = true;
8138 
8139 	vht_mcs_mask = mask->control[band].vht_mcs;
8140 	he_mcs_mask = mask->control[band].he_mcs;
8141 
8142 	if (ath11k_mac_bitrate_mask_num_vht_rates(ar, band, mask) == 1)
8143 		vht_fixed_rate = true;
8144 
8145 	if (ath11k_mac_bitrate_mask_num_he_rates(ar, band, mask) == 1)
8146 		he_fixed_rate = true;
8147 
8148 	if (!vht_fixed_rate && !he_fixed_rate)
8149 		return true;
8150 
8151 	vht_nss = ath11k_mac_max_vht_nss(vht_mcs_mask);
8152 	he_nss =  ath11k_mac_max_he_nss(he_mcs_mask);
8153 
8154 	rcu_read_lock();
8155 	spin_lock_bh(&ar->ab->base_lock);
8156 	list_for_each_entry_safe(peer, tmp, &ar->ab->peers, list) {
8157 		if (peer->sta) {
8158 			deflink = &peer->sta->deflink;
8159 
8160 			if (vht_fixed_rate && (!deflink->vht_cap.vht_supported ||
8161 					       deflink->rx_nss < vht_nss)) {
8162 				ret = false;
8163 				goto out;
8164 			}
8165 
8166 			if (he_fixed_rate && (!deflink->he_cap.has_he ||
8167 					      deflink->rx_nss < he_nss)) {
8168 				ret = false;
8169 				goto out;
8170 			}
8171 		}
8172 	}
8173 
8174 out:
8175 	spin_unlock_bh(&ar->ab->base_lock);
8176 	rcu_read_unlock();
8177 	return ret;
8178 }
8179 
8180 static int
8181 ath11k_mac_op_set_bitrate_mask(struct ieee80211_hw *hw,
8182 			       struct ieee80211_vif *vif,
8183 			       const struct cfg80211_bitrate_mask *mask)
8184 {
8185 	struct ath11k_vif *arvif = (void *)vif->drv_priv;
8186 	struct cfg80211_chan_def def;
8187 	struct ath11k_pdev_cap *cap;
8188 	struct ath11k *ar = arvif->ar;
8189 	enum nl80211_band band;
8190 	const u8 *ht_mcs_mask;
8191 	const u16 *vht_mcs_mask;
8192 	const u16 *he_mcs_mask;
8193 	u8 he_ltf = 0;
8194 	u8 he_gi = 0;
8195 	u32 rate;
8196 	u8 nss;
8197 	u8 sgi;
8198 	u8 ldpc;
8199 	int single_nss;
8200 	int ret;
8201 	int num_rates;
8202 	bool he_fixed_rate = false;
8203 
8204 	if (ath11k_mac_vif_chan(vif, &def))
8205 		return -EPERM;
8206 
8207 	band = def.chan->band;
8208 	cap = &ar->pdev->cap;
8209 	ht_mcs_mask = mask->control[band].ht_mcs;
8210 	vht_mcs_mask = mask->control[band].vht_mcs;
8211 	he_mcs_mask = mask->control[band].he_mcs;
8212 	ldpc = !!(cap->band[band].ht_cap_info & WMI_HT_CAP_TX_LDPC);
8213 
8214 	sgi = mask->control[band].gi;
8215 	if (sgi == NL80211_TXRATE_FORCE_LGI)
8216 		return -EINVAL;
8217 
8218 	he_gi = mask->control[band].he_gi;
8219 	he_ltf = mask->control[band].he_ltf;
8220 
8221 	/* mac80211 doesn't support sending a fixed HT/VHT MCS alone, rather it
8222 	 * requires passing at least one of used basic rates along with them.
8223 	 * Fixed rate setting across different preambles(legacy, HT, VHT) is
8224 	 * not supported by the FW. Hence use of FIXED_RATE vdev param is not
8225 	 * suitable for setting single HT/VHT rates.
8226 	 * But, there could be a single basic rate passed from userspace which
8227 	 * can be done through the FIXED_RATE param.
8228 	 */
8229 	if (ath11k_mac_has_single_legacy_rate(ar, band, mask)) {
8230 		ret = ath11k_mac_get_single_legacy_rate(ar, band, mask, &rate,
8231 							&nss);
8232 		if (ret) {
8233 			ath11k_warn(ar->ab, "failed to get single legacy rate for vdev %i: %d\n",
8234 				    arvif->vdev_id, ret);
8235 			return ret;
8236 		}
8237 		ieee80211_iterate_stations_atomic(ar->hw,
8238 						  ath11k_mac_disable_peer_fixed_rate,
8239 						  arvif);
8240 	} else if (ath11k_mac_bitrate_mask_get_single_nss(ar, band, mask,
8241 							  &single_nss)) {
8242 		rate = WMI_FIXED_RATE_NONE;
8243 		nss = single_nss;
8244 		mutex_lock(&ar->conf_mutex);
8245 		arvif->bitrate_mask = *mask;
8246 		ieee80211_iterate_stations_atomic(ar->hw,
8247 						  ath11k_mac_set_bitrate_mask_iter,
8248 						  arvif);
8249 		mutex_unlock(&ar->conf_mutex);
8250 	} else {
8251 		rate = WMI_FIXED_RATE_NONE;
8252 
8253 		if (!ath11k_mac_validate_vht_he_fixed_rate_settings(ar, band, mask))
8254 			ath11k_warn(ar->ab,
8255 				    "could not update fixed rate settings to all peers due to mcs/nss incompatibility\n");
8256 		nss = min_t(u32, ar->num_tx_chains,
8257 			    max(max(ath11k_mac_max_ht_nss(ht_mcs_mask),
8258 				    ath11k_mac_max_vht_nss(vht_mcs_mask)),
8259 				ath11k_mac_max_he_nss(he_mcs_mask)));
8260 
8261 		/* If multiple rates across different preambles are given
8262 		 * we can reconfigure this info with all peers using PEER_ASSOC
8263 		 * command with the below exception cases.
8264 		 * - Single VHT Rate : peer_assoc command accommodates only MCS
8265 		 * range values i.e 0-7, 0-8, 0-9 for VHT. Though mac80211
8266 		 * mandates passing basic rates along with HT/VHT rates, FW
8267 		 * doesn't allow switching from VHT to Legacy. Hence instead of
8268 		 * setting legacy and VHT rates using RATEMASK_CMD vdev cmd,
8269 		 * we could set this VHT rate as peer fixed rate param, which
8270 		 * will override FIXED rate and FW rate control algorithm.
8271 		 * If single VHT rate is passed along with HT rates, we select
8272 		 * the VHT rate as fixed rate for vht peers.
8273 		 * - Multiple VHT Rates : When Multiple VHT rates are given,this
8274 		 * can be set using RATEMASK CMD which uses FW rate-ctl alg.
8275 		 * TODO: Setting multiple VHT MCS and replacing peer_assoc with
8276 		 * RATEMASK_CMDID can cover all use cases of setting rates
8277 		 * across multiple preambles and rates within same type.
8278 		 * But requires more validation of the command at this point.
8279 		 */
8280 
8281 		num_rates = ath11k_mac_bitrate_mask_num_vht_rates(ar, band,
8282 								  mask);
8283 
8284 		if (!ath11k_mac_vht_mcs_range_present(ar, band, mask) &&
8285 		    num_rates > 1) {
8286 			/* TODO: Handle multiple VHT MCS values setting using
8287 			 * RATEMASK CMD
8288 			 */
8289 			ath11k_warn(ar->ab,
8290 				    "setting %d mcs values in bitrate mask not supported\n",
8291 				num_rates);
8292 			return -EINVAL;
8293 		}
8294 
8295 		num_rates = ath11k_mac_bitrate_mask_num_he_rates(ar, band,
8296 								 mask);
8297 		if (num_rates == 1)
8298 			he_fixed_rate = true;
8299 
8300 		if (!ath11k_mac_he_mcs_range_present(ar, band, mask) &&
8301 		    num_rates > 1) {
8302 			ath11k_warn(ar->ab,
8303 				    "Setting more than one HE MCS Value in bitrate mask not supported\n");
8304 			return -EINVAL;
8305 		}
8306 
8307 		mutex_lock(&ar->conf_mutex);
8308 		ieee80211_iterate_stations_atomic(ar->hw,
8309 						  ath11k_mac_disable_peer_fixed_rate,
8310 						  arvif);
8311 
8312 		arvif->bitrate_mask = *mask;
8313 		ieee80211_iterate_stations_atomic(ar->hw,
8314 						  ath11k_mac_set_bitrate_mask_iter,
8315 						  arvif);
8316 
8317 		mutex_unlock(&ar->conf_mutex);
8318 	}
8319 
8320 	mutex_lock(&ar->conf_mutex);
8321 
8322 	ret = ath11k_mac_set_rate_params(arvif, rate, nss, sgi, ldpc, he_gi,
8323 					 he_ltf, he_fixed_rate);
8324 	if (ret) {
8325 		ath11k_warn(ar->ab, "failed to set rate params on vdev %i: %d\n",
8326 			    arvif->vdev_id, ret);
8327 	}
8328 
8329 	mutex_unlock(&ar->conf_mutex);
8330 
8331 	return ret;
8332 }
8333 
8334 static void
8335 ath11k_mac_op_reconfig_complete(struct ieee80211_hw *hw,
8336 				enum ieee80211_reconfig_type reconfig_type)
8337 {
8338 	struct ath11k *ar = hw->priv;
8339 	struct ath11k_base *ab = ar->ab;
8340 	int recovery_count;
8341 	struct ath11k_vif *arvif;
8342 
8343 	if (reconfig_type != IEEE80211_RECONFIG_TYPE_RESTART)
8344 		return;
8345 
8346 	mutex_lock(&ar->conf_mutex);
8347 
8348 	if (ar->state == ATH11K_STATE_RESTARTED) {
8349 		ath11k_warn(ar->ab, "pdev %d successfully recovered\n",
8350 			    ar->pdev->pdev_id);
8351 		ar->state = ATH11K_STATE_ON;
8352 		ieee80211_wake_queues(ar->hw);
8353 
8354 		if (ar->ab->hw_params.current_cc_support &&
8355 		    ar->alpha2[0] != 0 && ar->alpha2[1] != 0) {
8356 			struct wmi_set_current_country_params set_current_param = {};
8357 
8358 			memcpy(&set_current_param.alpha2, ar->alpha2, 2);
8359 			ath11k_wmi_send_set_current_country_cmd(ar, &set_current_param);
8360 		}
8361 
8362 		if (ab->is_reset) {
8363 			recovery_count = atomic_inc_return(&ab->recovery_count);
8364 			ath11k_dbg(ab, ATH11K_DBG_BOOT,
8365 				   "recovery count %d\n", recovery_count);
8366 			/* When there are multiple radios in an SOC,
8367 			 * the recovery has to be done for each radio
8368 			 */
8369 			if (recovery_count == ab->num_radios) {
8370 				atomic_dec(&ab->reset_count);
8371 				complete(&ab->reset_complete);
8372 				ab->is_reset = false;
8373 				atomic_set(&ab->fail_cont_count, 0);
8374 				ath11k_dbg(ab, ATH11K_DBG_BOOT, "reset success\n");
8375 			}
8376 		}
8377 		if (ar->ab->hw_params.support_fw_mac_sequence) {
8378 			list_for_each_entry(arvif, &ar->arvifs, list) {
8379 				if (arvif->is_up && arvif->vdev_type == WMI_VDEV_TYPE_STA)
8380 					ieee80211_hw_restart_disconnect(arvif->vif);
8381 			}
8382 		}
8383 	}
8384 
8385 	mutex_unlock(&ar->conf_mutex);
8386 }
8387 
8388 static void
8389 ath11k_mac_update_bss_chan_survey(struct ath11k *ar,
8390 				  struct ieee80211_channel *channel)
8391 {
8392 	int ret;
8393 	enum wmi_bss_chan_info_req_type type = WMI_BSS_SURVEY_REQ_TYPE_READ;
8394 
8395 	lockdep_assert_held(&ar->conf_mutex);
8396 
8397 	if (!test_bit(WMI_TLV_SERVICE_BSS_CHANNEL_INFO_64, ar->ab->wmi_ab.svc_map) ||
8398 	    ar->rx_channel != channel)
8399 		return;
8400 
8401 	if (ar->scan.state != ATH11K_SCAN_IDLE) {
8402 		ath11k_dbg(ar->ab, ATH11K_DBG_MAC,
8403 			   "ignoring bss chan info req while scanning..\n");
8404 		return;
8405 	}
8406 
8407 	reinit_completion(&ar->bss_survey_done);
8408 
8409 	ret = ath11k_wmi_pdev_bss_chan_info_request(ar, type);
8410 	if (ret) {
8411 		ath11k_warn(ar->ab, "failed to send pdev bss chan info request\n");
8412 		return;
8413 	}
8414 
8415 	ret = wait_for_completion_timeout(&ar->bss_survey_done, 3 * HZ);
8416 	if (ret == 0)
8417 		ath11k_warn(ar->ab, "bss channel survey timed out\n");
8418 }
8419 
8420 static int ath11k_mac_op_get_survey(struct ieee80211_hw *hw, int idx,
8421 				    struct survey_info *survey)
8422 {
8423 	struct ath11k *ar = hw->priv;
8424 	struct ieee80211_supported_band *sband;
8425 	struct survey_info *ar_survey;
8426 	int ret = 0;
8427 
8428 	if (idx >= ATH11K_NUM_CHANS)
8429 		return -ENOENT;
8430 
8431 	ar_survey = &ar->survey[idx];
8432 
8433 	mutex_lock(&ar->conf_mutex);
8434 
8435 	sband = hw->wiphy->bands[NL80211_BAND_2GHZ];
8436 	if (sband && idx >= sband->n_channels) {
8437 		idx -= sband->n_channels;
8438 		sband = NULL;
8439 	}
8440 
8441 	if (!sband)
8442 		sband = hw->wiphy->bands[NL80211_BAND_5GHZ];
8443 	if (sband && idx >= sband->n_channels) {
8444 		idx -= sband->n_channels;
8445 		sband = NULL;
8446 	}
8447 
8448 	if (!sband)
8449 		sband = hw->wiphy->bands[NL80211_BAND_6GHZ];
8450 	if (!sband || idx >= sband->n_channels) {
8451 		ret = -ENOENT;
8452 		goto exit;
8453 	}
8454 
8455 	ath11k_mac_update_bss_chan_survey(ar, &sband->channels[idx]);
8456 
8457 	spin_lock_bh(&ar->data_lock);
8458 	memcpy(survey, ar_survey, sizeof(*survey));
8459 	spin_unlock_bh(&ar->data_lock);
8460 
8461 	survey->channel = &sband->channels[idx];
8462 
8463 	if (ar->rx_channel == survey->channel)
8464 		survey->filled |= SURVEY_INFO_IN_USE;
8465 
8466 exit:
8467 	mutex_unlock(&ar->conf_mutex);
8468 	return ret;
8469 }
8470 
8471 static void ath11k_mac_put_chain_rssi(struct station_info *sinfo,
8472 				      struct ath11k_sta *arsta,
8473 				      char *pre,
8474 				      bool clear)
8475 {
8476 	struct ath11k *ar = arsta->arvif->ar;
8477 	int i;
8478 	s8 rssi;
8479 
8480 	for (i = 0; i < ARRAY_SIZE(sinfo->chain_signal); i++) {
8481 		sinfo->chains &= ~BIT(i);
8482 		rssi = arsta->chain_signal[i];
8483 		if (clear)
8484 			arsta->chain_signal[i] = ATH11K_INVALID_RSSI_FULL;
8485 
8486 		ath11k_dbg(ar->ab, ATH11K_DBG_MAC,
8487 			   "mac sta statistics %s rssi[%d] %d\n", pre, i, rssi);
8488 
8489 		if (rssi != ATH11K_DEFAULT_NOISE_FLOOR &&
8490 		    rssi != ATH11K_INVALID_RSSI_FULL &&
8491 		    rssi != ATH11K_INVALID_RSSI_EMPTY &&
8492 		    rssi != 0) {
8493 			sinfo->chain_signal[i] = rssi;
8494 			sinfo->chains |= BIT(i);
8495 			sinfo->filled |= BIT_ULL(NL80211_STA_INFO_CHAIN_SIGNAL);
8496 		}
8497 	}
8498 }
8499 
8500 static void ath11k_mac_op_sta_statistics(struct ieee80211_hw *hw,
8501 					 struct ieee80211_vif *vif,
8502 					 struct ieee80211_sta *sta,
8503 					 struct station_info *sinfo)
8504 {
8505 	struct ath11k_sta *arsta = (struct ath11k_sta *)sta->drv_priv;
8506 	struct ath11k *ar = arsta->arvif->ar;
8507 	s8 signal;
8508 	bool db2dbm = test_bit(WMI_TLV_SERVICE_HW_DB2DBM_CONVERSION_SUPPORT,
8509 			       ar->ab->wmi_ab.svc_map);
8510 
8511 	sinfo->rx_duration = arsta->rx_duration;
8512 	sinfo->filled |= BIT_ULL(NL80211_STA_INFO_RX_DURATION);
8513 
8514 	sinfo->tx_duration = arsta->tx_duration;
8515 	sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_DURATION);
8516 
8517 	if (arsta->txrate.legacy || arsta->txrate.nss) {
8518 		if (arsta->txrate.legacy) {
8519 			sinfo->txrate.legacy = arsta->txrate.legacy;
8520 		} else {
8521 			sinfo->txrate.mcs = arsta->txrate.mcs;
8522 			sinfo->txrate.nss = arsta->txrate.nss;
8523 			sinfo->txrate.bw = arsta->txrate.bw;
8524 			sinfo->txrate.he_gi = arsta->txrate.he_gi;
8525 			sinfo->txrate.he_dcm = arsta->txrate.he_dcm;
8526 			sinfo->txrate.he_ru_alloc = arsta->txrate.he_ru_alloc;
8527 		}
8528 		sinfo->txrate.flags = arsta->txrate.flags;
8529 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_BITRATE);
8530 	}
8531 
8532 	ath11k_mac_put_chain_rssi(sinfo, arsta, "ppdu", false);
8533 
8534 	if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_CHAIN_SIGNAL)) &&
8535 	    arsta->arvif->vdev_type == WMI_VDEV_TYPE_STA &&
8536 	    ar->ab->hw_params.supports_rssi_stats &&
8537 	    !ath11k_debugfs_get_fw_stats(ar, ar->pdev->pdev_id, 0,
8538 					 WMI_REQUEST_RSSI_PER_CHAIN_STAT)) {
8539 		ath11k_mac_put_chain_rssi(sinfo, arsta, "fw stats", true);
8540 	}
8541 
8542 	signal = arsta->rssi_comb;
8543 	if (!signal &&
8544 	    arsta->arvif->vdev_type == WMI_VDEV_TYPE_STA &&
8545 	    ar->ab->hw_params.supports_rssi_stats &&
8546 	    !(ath11k_debugfs_get_fw_stats(ar, ar->pdev->pdev_id, 0,
8547 					WMI_REQUEST_VDEV_STAT)))
8548 		signal = arsta->rssi_beacon;
8549 
8550 	ath11k_dbg(ar->ab, ATH11K_DBG_MAC,
8551 		   "mac sta statistics db2dbm %u rssi comb %d rssi beacon %d\n",
8552 		   db2dbm, arsta->rssi_comb, arsta->rssi_beacon);
8553 
8554 	if (signal) {
8555 		sinfo->signal = db2dbm ? signal : signal + ATH11K_DEFAULT_NOISE_FLOOR;
8556 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_SIGNAL);
8557 	}
8558 
8559 	sinfo->signal_avg = ewma_avg_rssi_read(&arsta->avg_rssi) +
8560 		ATH11K_DEFAULT_NOISE_FLOOR;
8561 	sinfo->filled |= BIT_ULL(NL80211_STA_INFO_SIGNAL_AVG);
8562 }
8563 
8564 #if IS_ENABLED(CONFIG_IPV6)
8565 static void ath11k_generate_ns_mc_addr(struct ath11k *ar,
8566 				       struct ath11k_arp_ns_offload *offload)
8567 {
8568 	int i;
8569 
8570 	for (i = 0; i < offload->ipv6_count; i++) {
8571 		offload->self_ipv6_addr[i][0] = 0xff;
8572 		offload->self_ipv6_addr[i][1] = 0x02;
8573 		offload->self_ipv6_addr[i][11] = 0x01;
8574 		offload->self_ipv6_addr[i][12] = 0xff;
8575 		offload->self_ipv6_addr[i][13] =
8576 					offload->ipv6_addr[i][13];
8577 		offload->self_ipv6_addr[i][14] =
8578 					offload->ipv6_addr[i][14];
8579 		offload->self_ipv6_addr[i][15] =
8580 					offload->ipv6_addr[i][15];
8581 		ath11k_dbg(ar->ab, ATH11K_DBG_MAC, "NS solicited addr %pI6\n",
8582 			   offload->self_ipv6_addr[i]);
8583 	}
8584 }
8585 
8586 static void ath11k_mac_op_ipv6_changed(struct ieee80211_hw *hw,
8587 				       struct ieee80211_vif *vif,
8588 				       struct inet6_dev *idev)
8589 {
8590 	struct ath11k *ar = hw->priv;
8591 	struct ath11k_arp_ns_offload *offload;
8592 	struct ath11k_vif *arvif = ath11k_vif_to_arvif(vif);
8593 	struct inet6_ifaddr *ifa6;
8594 	struct ifacaddr6 *ifaca6;
8595 	struct list_head *p;
8596 	u32 count, scope;
8597 
8598 	ath11k_dbg(ar->ab, ATH11K_DBG_MAC, "mac op ipv6 changed\n");
8599 
8600 	offload = &arvif->arp_ns_offload;
8601 	count = 0;
8602 
8603 	read_lock_bh(&idev->lock);
8604 
8605 	memset(offload->ipv6_addr, 0, sizeof(offload->ipv6_addr));
8606 	memset(offload->self_ipv6_addr, 0, sizeof(offload->self_ipv6_addr));
8607 	memcpy(offload->mac_addr, vif->addr, ETH_ALEN);
8608 
8609 	/* get unicast address */
8610 	list_for_each(p, &idev->addr_list) {
8611 		if (count >= ATH11K_IPV6_MAX_COUNT)
8612 			goto generate;
8613 
8614 		ifa6 = list_entry(p, struct inet6_ifaddr, if_list);
8615 		if (ifa6->flags & IFA_F_DADFAILED)
8616 			continue;
8617 		scope = ipv6_addr_src_scope(&ifa6->addr);
8618 		if (scope == IPV6_ADDR_SCOPE_LINKLOCAL ||
8619 		    scope == IPV6_ADDR_SCOPE_GLOBAL) {
8620 			memcpy(offload->ipv6_addr[count], &ifa6->addr.s6_addr,
8621 			       sizeof(ifa6->addr.s6_addr));
8622 			offload->ipv6_type[count] = ATH11K_IPV6_UC_TYPE;
8623 			ath11k_dbg(ar->ab, ATH11K_DBG_MAC, "mac count %d ipv6 uc %pI6 scope %d\n",
8624 				   count, offload->ipv6_addr[count],
8625 				   scope);
8626 			count++;
8627 		} else {
8628 			ath11k_warn(ar->ab, "Unsupported ipv6 scope: %d\n", scope);
8629 		}
8630 	}
8631 
8632 	/* get anycast address */
8633 	for (ifaca6 = idev->ac_list; ifaca6; ifaca6 = ifaca6->aca_next) {
8634 		if (count >= ATH11K_IPV6_MAX_COUNT)
8635 			goto generate;
8636 
8637 		scope = ipv6_addr_src_scope(&ifaca6->aca_addr);
8638 		if (scope == IPV6_ADDR_SCOPE_LINKLOCAL ||
8639 		    scope == IPV6_ADDR_SCOPE_GLOBAL) {
8640 			memcpy(offload->ipv6_addr[count], &ifaca6->aca_addr,
8641 			       sizeof(ifaca6->aca_addr));
8642 			offload->ipv6_type[count] = ATH11K_IPV6_AC_TYPE;
8643 			ath11k_dbg(ar->ab, ATH11K_DBG_MAC, "mac count %d ipv6 ac %pI6 scope %d\n",
8644 				   count, offload->ipv6_addr[count],
8645 				   scope);
8646 			count++;
8647 		} else {
8648 			ath11k_warn(ar->ab, "Unsupported ipv scope: %d\n", scope);
8649 		}
8650 	}
8651 
8652 generate:
8653 	offload->ipv6_count = count;
8654 	read_unlock_bh(&idev->lock);
8655 
8656 	/* generate ns multicast address */
8657 	ath11k_generate_ns_mc_addr(ar, offload);
8658 }
8659 #endif
8660 
8661 static void ath11k_mac_op_set_rekey_data(struct ieee80211_hw *hw,
8662 					 struct ieee80211_vif *vif,
8663 					 struct cfg80211_gtk_rekey_data *data)
8664 {
8665 	struct ath11k *ar = hw->priv;
8666 	struct ath11k_vif *arvif = ath11k_vif_to_arvif(vif);
8667 	struct ath11k_rekey_data *rekey_data = &arvif->rekey_data;
8668 
8669 	ath11k_dbg(ar->ab, ATH11K_DBG_MAC, "mac set rekey data vdev %d\n",
8670 		   arvif->vdev_id);
8671 
8672 	mutex_lock(&ar->conf_mutex);
8673 
8674 	memcpy(rekey_data->kck, data->kck, NL80211_KCK_LEN);
8675 	memcpy(rekey_data->kek, data->kek, NL80211_KEK_LEN);
8676 
8677 	/* The supplicant works on big-endian, the firmware expects it on
8678 	 * little endian.
8679 	 */
8680 	rekey_data->replay_ctr = get_unaligned_be64(data->replay_ctr);
8681 
8682 	arvif->rekey_data.enable_offload = true;
8683 
8684 	ath11k_dbg_dump(ar->ab, ATH11K_DBG_MAC, "kck", NULL,
8685 			rekey_data->kck, NL80211_KCK_LEN);
8686 	ath11k_dbg_dump(ar->ab, ATH11K_DBG_MAC, "kek", NULL,
8687 			rekey_data->kck, NL80211_KEK_LEN);
8688 	ath11k_dbg_dump(ar->ab, ATH11K_DBG_MAC, "replay ctr", NULL,
8689 			&rekey_data->replay_ctr, sizeof(rekey_data->replay_ctr));
8690 
8691 	mutex_unlock(&ar->conf_mutex);
8692 }
8693 
8694 static int ath11k_mac_op_set_bios_sar_specs(struct ieee80211_hw *hw,
8695 					    const struct cfg80211_sar_specs *sar)
8696 {
8697 	struct ath11k *ar = hw->priv;
8698 	const struct cfg80211_sar_sub_specs *sspec;
8699 	int ret, index;
8700 	u8 *sar_tbl;
8701 	u32 i;
8702 
8703 	if (!sar || sar->type != NL80211_SAR_TYPE_POWER ||
8704 	    sar->num_sub_specs == 0)
8705 		return -EINVAL;
8706 
8707 	mutex_lock(&ar->conf_mutex);
8708 
8709 	if (!test_bit(WMI_TLV_SERVICE_BIOS_SAR_SUPPORT, ar->ab->wmi_ab.svc_map) ||
8710 	    !ar->ab->hw_params.bios_sar_capa) {
8711 		ret = -EOPNOTSUPP;
8712 		goto exit;
8713 	}
8714 
8715 	ret = ath11k_wmi_pdev_set_bios_geo_table_param(ar);
8716 	if (ret) {
8717 		ath11k_warn(ar->ab, "failed to set geo table: %d\n", ret);
8718 		goto exit;
8719 	}
8720 
8721 	sar_tbl = kzalloc(BIOS_SAR_TABLE_LEN, GFP_KERNEL);
8722 	if (!sar_tbl) {
8723 		ret = -ENOMEM;
8724 		goto exit;
8725 	}
8726 
8727 	sspec = sar->sub_specs;
8728 	for (i = 0; i < sar->num_sub_specs; i++) {
8729 		if (sspec->freq_range_index >= (BIOS_SAR_TABLE_LEN >> 1)) {
8730 			ath11k_warn(ar->ab, "Ignore bad frequency index %u, max allowed %u\n",
8731 				    sspec->freq_range_index, BIOS_SAR_TABLE_LEN >> 1);
8732 			continue;
8733 		}
8734 
8735 		/* chain0 and chain1 share same power setting */
8736 		sar_tbl[sspec->freq_range_index] = sspec->power;
8737 		index = sspec->freq_range_index + (BIOS_SAR_TABLE_LEN >> 1);
8738 		sar_tbl[index] = sspec->power;
8739 		ath11k_dbg(ar->ab, ATH11K_DBG_MAC, "sar tbl[%d] = %d\n",
8740 			   sspec->freq_range_index, sar_tbl[sspec->freq_range_index]);
8741 		sspec++;
8742 	}
8743 
8744 	ret = ath11k_wmi_pdev_set_bios_sar_table_param(ar, sar_tbl);
8745 	if (ret)
8746 		ath11k_warn(ar->ab, "failed to set sar power: %d", ret);
8747 
8748 	kfree(sar_tbl);
8749 exit:
8750 	mutex_unlock(&ar->conf_mutex);
8751 
8752 	return ret;
8753 }
8754 
8755 static int ath11k_mac_op_cancel_remain_on_channel(struct ieee80211_hw *hw,
8756 						  struct ieee80211_vif *vif)
8757 {
8758 	struct ath11k *ar = hw->priv;
8759 
8760 	mutex_lock(&ar->conf_mutex);
8761 
8762 	spin_lock_bh(&ar->data_lock);
8763 	ar->scan.roc_notify = false;
8764 	spin_unlock_bh(&ar->data_lock);
8765 
8766 	ath11k_scan_abort(ar);
8767 
8768 	mutex_unlock(&ar->conf_mutex);
8769 
8770 	cancel_delayed_work_sync(&ar->scan.timeout);
8771 
8772 	return 0;
8773 }
8774 
8775 static int ath11k_mac_op_remain_on_channel(struct ieee80211_hw *hw,
8776 					   struct ieee80211_vif *vif,
8777 					   struct ieee80211_channel *chan,
8778 					   int duration,
8779 					   enum ieee80211_roc_type type)
8780 {
8781 	struct ath11k *ar = hw->priv;
8782 	struct ath11k_vif *arvif = (void *)vif->drv_priv;
8783 	struct scan_req_params arg;
8784 	int ret;
8785 	u32 scan_time_msec;
8786 
8787 	mutex_lock(&ar->conf_mutex);
8788 
8789 	spin_lock_bh(&ar->data_lock);
8790 	switch (ar->scan.state) {
8791 	case ATH11K_SCAN_IDLE:
8792 		reinit_completion(&ar->scan.started);
8793 		reinit_completion(&ar->scan.completed);
8794 		reinit_completion(&ar->scan.on_channel);
8795 		ar->scan.state = ATH11K_SCAN_STARTING;
8796 		ar->scan.is_roc = true;
8797 		ar->scan.vdev_id = arvif->vdev_id;
8798 		ar->scan.roc_freq = chan->center_freq;
8799 		ar->scan.roc_notify = true;
8800 		ret = 0;
8801 		break;
8802 	case ATH11K_SCAN_STARTING:
8803 	case ATH11K_SCAN_RUNNING:
8804 	case ATH11K_SCAN_ABORTING:
8805 		ret = -EBUSY;
8806 		break;
8807 	}
8808 	spin_unlock_bh(&ar->data_lock);
8809 
8810 	if (ret)
8811 		goto exit;
8812 
8813 	scan_time_msec = ar->hw->wiphy->max_remain_on_channel_duration * 2;
8814 
8815 	memset(&arg, 0, sizeof(arg));
8816 	ath11k_wmi_start_scan_init(ar, &arg);
8817 	arg.num_chan = 1;
8818 	arg.chan_list = kcalloc(arg.num_chan, sizeof(*arg.chan_list),
8819 				GFP_KERNEL);
8820 	if (!arg.chan_list) {
8821 		ret = -ENOMEM;
8822 		goto exit;
8823 	}
8824 
8825 	arg.vdev_id = arvif->vdev_id;
8826 	arg.scan_id = ATH11K_SCAN_ID;
8827 	arg.chan_list[0] = chan->center_freq;
8828 	arg.dwell_time_active = scan_time_msec;
8829 	arg.dwell_time_passive = scan_time_msec;
8830 	arg.max_scan_time = scan_time_msec;
8831 	arg.scan_flags |= WMI_SCAN_FLAG_PASSIVE;
8832 	arg.scan_flags |= WMI_SCAN_FILTER_PROBE_REQ;
8833 	arg.burst_duration = duration;
8834 
8835 	ret = ath11k_start_scan(ar, &arg);
8836 	if (ret) {
8837 		ath11k_warn(ar->ab, "failed to start roc scan: %d\n", ret);
8838 
8839 		spin_lock_bh(&ar->data_lock);
8840 		ar->scan.state = ATH11K_SCAN_IDLE;
8841 		spin_unlock_bh(&ar->data_lock);
8842 		goto free_chan_list;
8843 	}
8844 
8845 	ret = wait_for_completion_timeout(&ar->scan.on_channel, 3 * HZ);
8846 	if (ret == 0) {
8847 		ath11k_warn(ar->ab, "failed to switch to channel for roc scan\n");
8848 		ret = ath11k_scan_stop(ar);
8849 		if (ret)
8850 			ath11k_warn(ar->ab, "failed to stop scan: %d\n", ret);
8851 		ret = -ETIMEDOUT;
8852 		goto free_chan_list;
8853 	}
8854 
8855 	ieee80211_queue_delayed_work(ar->hw, &ar->scan.timeout,
8856 				     msecs_to_jiffies(duration));
8857 
8858 	ret = 0;
8859 
8860 free_chan_list:
8861 	kfree(arg.chan_list);
8862 exit:
8863 	mutex_unlock(&ar->conf_mutex);
8864 	return ret;
8865 }
8866 
8867 static int ath11k_fw_stats_request(struct ath11k *ar,
8868 				   struct stats_request_params *req_param)
8869 {
8870 	struct ath11k_base *ab = ar->ab;
8871 	unsigned long time_left;
8872 	int ret;
8873 
8874 	lockdep_assert_held(&ar->conf_mutex);
8875 
8876 	spin_lock_bh(&ar->data_lock);
8877 	ar->fw_stats_done = false;
8878 	ath11k_fw_stats_pdevs_free(&ar->fw_stats.pdevs);
8879 	spin_unlock_bh(&ar->data_lock);
8880 
8881 	reinit_completion(&ar->fw_stats_complete);
8882 
8883 	ret = ath11k_wmi_send_stats_request_cmd(ar, req_param);
8884 	if (ret) {
8885 		ath11k_warn(ab, "could not request fw stats (%d)\n",
8886 			    ret);
8887 		return ret;
8888 	}
8889 
8890 	time_left = wait_for_completion_timeout(&ar->fw_stats_complete,
8891 						1 * HZ);
8892 
8893 	if (!time_left)
8894 		return -ETIMEDOUT;
8895 
8896 	return 0;
8897 }
8898 
8899 static int ath11k_mac_op_get_txpower(struct ieee80211_hw *hw,
8900 				     struct ieee80211_vif *vif,
8901 				     int *dbm)
8902 {
8903 	struct ath11k *ar = hw->priv;
8904 	struct ath11k_base *ab = ar->ab;
8905 	struct stats_request_params req_param = {0};
8906 	struct ath11k_fw_stats_pdev *pdev;
8907 	int ret;
8908 
8909 	/* Final Tx power is minimum of Target Power, CTL power, Regulatory
8910 	 * Power, PSD EIRP Power. We just know the Regulatory power from the
8911 	 * regulatory rules obtained. FW knows all these power and sets the min
8912 	 * of these. Hence, we request the FW pdev stats in which FW reports
8913 	 * the minimum of all vdev's channel Tx power.
8914 	 */
8915 	mutex_lock(&ar->conf_mutex);
8916 
8917 	if (ar->state != ATH11K_STATE_ON)
8918 		goto err_fallback;
8919 
8920 	req_param.pdev_id = ar->pdev->pdev_id;
8921 	req_param.stats_id = WMI_REQUEST_PDEV_STAT;
8922 
8923 	ret = ath11k_fw_stats_request(ar, &req_param);
8924 	if (ret) {
8925 		ath11k_warn(ab, "failed to request fw pdev stats: %d\n", ret);
8926 		goto err_fallback;
8927 	}
8928 
8929 	spin_lock_bh(&ar->data_lock);
8930 	pdev = list_first_entry_or_null(&ar->fw_stats.pdevs,
8931 					struct ath11k_fw_stats_pdev, list);
8932 	if (!pdev) {
8933 		spin_unlock_bh(&ar->data_lock);
8934 		goto err_fallback;
8935 	}
8936 
8937 	/* tx power is set as 2 units per dBm in FW. */
8938 	*dbm = pdev->chan_tx_power / 2;
8939 
8940 	spin_unlock_bh(&ar->data_lock);
8941 	mutex_unlock(&ar->conf_mutex);
8942 
8943 	ath11k_dbg(ar->ab, ATH11K_DBG_MAC, "txpower from firmware %d, reported %d dBm\n",
8944 		   pdev->chan_tx_power, *dbm);
8945 	return 0;
8946 
8947 err_fallback:
8948 	mutex_unlock(&ar->conf_mutex);
8949 	/* We didn't get txpower from FW. Hence, relying on vif->bss_conf.txpower */
8950 	*dbm = vif->bss_conf.txpower;
8951 	ath11k_dbg(ar->ab, ATH11K_DBG_MAC, "txpower from firmware NaN, reported %d dBm\n",
8952 		   *dbm);
8953 	return 0;
8954 }
8955 
8956 static const struct ieee80211_ops ath11k_ops = {
8957 	.tx				= ath11k_mac_op_tx,
8958 	.wake_tx_queue			= ieee80211_handle_wake_tx_queue,
8959 	.start                          = ath11k_mac_op_start,
8960 	.stop                           = ath11k_mac_op_stop,
8961 	.reconfig_complete              = ath11k_mac_op_reconfig_complete,
8962 	.add_interface                  = ath11k_mac_op_add_interface,
8963 	.remove_interface		= ath11k_mac_op_remove_interface,
8964 	.update_vif_offload		= ath11k_mac_op_update_vif_offload,
8965 	.config                         = ath11k_mac_op_config,
8966 	.bss_info_changed               = ath11k_mac_op_bss_info_changed,
8967 	.configure_filter		= ath11k_mac_op_configure_filter,
8968 	.hw_scan                        = ath11k_mac_op_hw_scan,
8969 	.cancel_hw_scan                 = ath11k_mac_op_cancel_hw_scan,
8970 	.set_key                        = ath11k_mac_op_set_key,
8971 	.set_rekey_data	                = ath11k_mac_op_set_rekey_data,
8972 	.sta_state                      = ath11k_mac_op_sta_state,
8973 	.sta_set_4addr                  = ath11k_mac_op_sta_set_4addr,
8974 	.sta_set_txpwr			= ath11k_mac_op_sta_set_txpwr,
8975 	.sta_rc_update			= ath11k_mac_op_sta_rc_update,
8976 	.conf_tx                        = ath11k_mac_op_conf_tx,
8977 	.set_antenna			= ath11k_mac_op_set_antenna,
8978 	.get_antenna			= ath11k_mac_op_get_antenna,
8979 	.ampdu_action			= ath11k_mac_op_ampdu_action,
8980 	.add_chanctx			= ath11k_mac_op_add_chanctx,
8981 	.remove_chanctx			= ath11k_mac_op_remove_chanctx,
8982 	.change_chanctx			= ath11k_mac_op_change_chanctx,
8983 	.assign_vif_chanctx		= ath11k_mac_op_assign_vif_chanctx,
8984 	.unassign_vif_chanctx		= ath11k_mac_op_unassign_vif_chanctx,
8985 	.switch_vif_chanctx		= ath11k_mac_op_switch_vif_chanctx,
8986 	.set_rts_threshold		= ath11k_mac_op_set_rts_threshold,
8987 	.set_frag_threshold		= ath11k_mac_op_set_frag_threshold,
8988 	.set_bitrate_mask		= ath11k_mac_op_set_bitrate_mask,
8989 	.get_survey			= ath11k_mac_op_get_survey,
8990 	.flush				= ath11k_mac_op_flush,
8991 	.sta_statistics			= ath11k_mac_op_sta_statistics,
8992 	CFG80211_TESTMODE_CMD(ath11k_tm_cmd)
8993 
8994 #ifdef CONFIG_PM
8995 	.suspend			= ath11k_wow_op_suspend,
8996 	.resume				= ath11k_wow_op_resume,
8997 	.set_wakeup			= ath11k_wow_op_set_wakeup,
8998 #endif
8999 
9000 #ifdef CONFIG_ATH11K_DEBUGFS
9001 	.sta_add_debugfs		= ath11k_debugfs_sta_op_add,
9002 #endif
9003 
9004 #if IS_ENABLED(CONFIG_IPV6)
9005 	.ipv6_addr_change = ath11k_mac_op_ipv6_changed,
9006 #endif
9007 	.get_txpower                    = ath11k_mac_op_get_txpower,
9008 
9009 	.set_sar_specs			= ath11k_mac_op_set_bios_sar_specs,
9010 	.remain_on_channel		= ath11k_mac_op_remain_on_channel,
9011 	.cancel_remain_on_channel	= ath11k_mac_op_cancel_remain_on_channel,
9012 };
9013 
9014 static void ath11k_mac_update_ch_list(struct ath11k *ar,
9015 				      struct ieee80211_supported_band *band,
9016 				      u32 freq_low, u32 freq_high)
9017 {
9018 	int i;
9019 
9020 	if (!(freq_low && freq_high))
9021 		return;
9022 
9023 	for (i = 0; i < band->n_channels; i++) {
9024 		if (band->channels[i].center_freq < freq_low ||
9025 		    band->channels[i].center_freq > freq_high)
9026 			band->channels[i].flags |= IEEE80211_CHAN_DISABLED;
9027 	}
9028 }
9029 
9030 static u32 ath11k_get_phy_id(struct ath11k *ar, u32 band)
9031 {
9032 	struct ath11k_pdev *pdev = ar->pdev;
9033 	struct ath11k_pdev_cap *pdev_cap = &pdev->cap;
9034 
9035 	if (band == WMI_HOST_WLAN_2G_CAP)
9036 		return pdev_cap->band[NL80211_BAND_2GHZ].phy_id;
9037 
9038 	if (band == WMI_HOST_WLAN_5G_CAP)
9039 		return pdev_cap->band[NL80211_BAND_5GHZ].phy_id;
9040 
9041 	ath11k_warn(ar->ab, "unsupported phy cap:%d\n", band);
9042 
9043 	return 0;
9044 }
9045 
9046 static int ath11k_mac_setup_channels_rates(struct ath11k *ar,
9047 					   u32 supported_bands)
9048 {
9049 	struct ieee80211_supported_band *band;
9050 	struct ath11k_hal_reg_capabilities_ext *reg_cap, *temp_reg_cap;
9051 	void *channels;
9052 	u32 phy_id;
9053 
9054 	BUILD_BUG_ON((ARRAY_SIZE(ath11k_2ghz_channels) +
9055 		      ARRAY_SIZE(ath11k_5ghz_channels) +
9056 		      ARRAY_SIZE(ath11k_6ghz_channels)) !=
9057 		     ATH11K_NUM_CHANS);
9058 
9059 	reg_cap = &ar->ab->hal_reg_cap[ar->pdev_idx];
9060 	temp_reg_cap = reg_cap;
9061 
9062 	if (supported_bands & WMI_HOST_WLAN_2G_CAP) {
9063 		channels = kmemdup(ath11k_2ghz_channels,
9064 				   sizeof(ath11k_2ghz_channels),
9065 				   GFP_KERNEL);
9066 		if (!channels)
9067 			return -ENOMEM;
9068 
9069 		band = &ar->mac.sbands[NL80211_BAND_2GHZ];
9070 		band->band = NL80211_BAND_2GHZ;
9071 		band->n_channels = ARRAY_SIZE(ath11k_2ghz_channels);
9072 		band->channels = channels;
9073 		band->n_bitrates = ath11k_g_rates_size;
9074 		band->bitrates = ath11k_g_rates;
9075 		ar->hw->wiphy->bands[NL80211_BAND_2GHZ] = band;
9076 
9077 		if (ar->ab->hw_params.single_pdev_only) {
9078 			phy_id = ath11k_get_phy_id(ar, WMI_HOST_WLAN_2G_CAP);
9079 			temp_reg_cap = &ar->ab->hal_reg_cap[phy_id];
9080 		}
9081 		ath11k_mac_update_ch_list(ar, band,
9082 					  temp_reg_cap->low_2ghz_chan,
9083 					  temp_reg_cap->high_2ghz_chan);
9084 	}
9085 
9086 	if (supported_bands & WMI_HOST_WLAN_5G_CAP) {
9087 		if (reg_cap->high_5ghz_chan >= ATH11K_MIN_6G_FREQ) {
9088 			channels = kmemdup(ath11k_6ghz_channels,
9089 					   sizeof(ath11k_6ghz_channels), GFP_KERNEL);
9090 			if (!channels) {
9091 				kfree(ar->mac.sbands[NL80211_BAND_2GHZ].channels);
9092 				return -ENOMEM;
9093 			}
9094 
9095 			ar->supports_6ghz = true;
9096 			band = &ar->mac.sbands[NL80211_BAND_6GHZ];
9097 			band->band = NL80211_BAND_6GHZ;
9098 			band->n_channels = ARRAY_SIZE(ath11k_6ghz_channels);
9099 			band->channels = channels;
9100 			band->n_bitrates = ath11k_a_rates_size;
9101 			band->bitrates = ath11k_a_rates;
9102 			ar->hw->wiphy->bands[NL80211_BAND_6GHZ] = band;
9103 
9104 			if (ar->ab->hw_params.single_pdev_only) {
9105 				phy_id = ath11k_get_phy_id(ar, WMI_HOST_WLAN_5G_CAP);
9106 				temp_reg_cap = &ar->ab->hal_reg_cap[phy_id];
9107 			}
9108 
9109 			ath11k_mac_update_ch_list(ar, band,
9110 						  temp_reg_cap->low_5ghz_chan,
9111 						  temp_reg_cap->high_5ghz_chan);
9112 		}
9113 
9114 		if (reg_cap->low_5ghz_chan < ATH11K_MIN_6G_FREQ) {
9115 			channels = kmemdup(ath11k_5ghz_channels,
9116 					   sizeof(ath11k_5ghz_channels),
9117 					   GFP_KERNEL);
9118 			if (!channels) {
9119 				kfree(ar->mac.sbands[NL80211_BAND_2GHZ].channels);
9120 				kfree(ar->mac.sbands[NL80211_BAND_6GHZ].channels);
9121 				return -ENOMEM;
9122 			}
9123 
9124 			band = &ar->mac.sbands[NL80211_BAND_5GHZ];
9125 			band->band = NL80211_BAND_5GHZ;
9126 			band->n_channels = ARRAY_SIZE(ath11k_5ghz_channels);
9127 			band->channels = channels;
9128 			band->n_bitrates = ath11k_a_rates_size;
9129 			band->bitrates = ath11k_a_rates;
9130 			ar->hw->wiphy->bands[NL80211_BAND_5GHZ] = band;
9131 
9132 			if (ar->ab->hw_params.single_pdev_only) {
9133 				phy_id = ath11k_get_phy_id(ar, WMI_HOST_WLAN_5G_CAP);
9134 				temp_reg_cap = &ar->ab->hal_reg_cap[phy_id];
9135 			}
9136 
9137 			ath11k_mac_update_ch_list(ar, band,
9138 						  temp_reg_cap->low_5ghz_chan,
9139 						  temp_reg_cap->high_5ghz_chan);
9140 		}
9141 	}
9142 
9143 	return 0;
9144 }
9145 
9146 static int ath11k_mac_setup_iface_combinations(struct ath11k *ar)
9147 {
9148 	struct ath11k_base *ab = ar->ab;
9149 	struct ieee80211_iface_combination *combinations;
9150 	struct ieee80211_iface_limit *limits;
9151 	int n_limits;
9152 
9153 	combinations = kzalloc(sizeof(*combinations), GFP_KERNEL);
9154 	if (!combinations)
9155 		return -ENOMEM;
9156 
9157 	n_limits = 2;
9158 
9159 	limits = kcalloc(n_limits, sizeof(*limits), GFP_KERNEL);
9160 	if (!limits) {
9161 		kfree(combinations);
9162 		return -ENOMEM;
9163 	}
9164 
9165 	limits[0].max = 1;
9166 	limits[0].types |= BIT(NL80211_IFTYPE_STATION);
9167 
9168 	limits[1].max = 16;
9169 	limits[1].types |= BIT(NL80211_IFTYPE_AP);
9170 
9171 	if (IS_ENABLED(CONFIG_MAC80211_MESH) &&
9172 	    ab->hw_params.interface_modes & BIT(NL80211_IFTYPE_MESH_POINT))
9173 		limits[1].types |= BIT(NL80211_IFTYPE_MESH_POINT);
9174 
9175 	combinations[0].limits = limits;
9176 	combinations[0].n_limits = n_limits;
9177 	combinations[0].max_interfaces = 16;
9178 	combinations[0].num_different_channels = 1;
9179 	combinations[0].beacon_int_infra_match = true;
9180 	combinations[0].beacon_int_min_gcd = 100;
9181 	combinations[0].radar_detect_widths = BIT(NL80211_CHAN_WIDTH_20_NOHT) |
9182 						BIT(NL80211_CHAN_WIDTH_20) |
9183 						BIT(NL80211_CHAN_WIDTH_40) |
9184 						BIT(NL80211_CHAN_WIDTH_80) |
9185 						BIT(NL80211_CHAN_WIDTH_80P80) |
9186 						BIT(NL80211_CHAN_WIDTH_160);
9187 
9188 	ar->hw->wiphy->iface_combinations = combinations;
9189 	ar->hw->wiphy->n_iface_combinations = 1;
9190 
9191 	return 0;
9192 }
9193 
9194 static const u8 ath11k_if_types_ext_capa[] = {
9195 	[0] = WLAN_EXT_CAPA1_EXT_CHANNEL_SWITCHING,
9196 	[2] = WLAN_EXT_CAPA3_MULTI_BSSID_SUPPORT,
9197 	[7] = WLAN_EXT_CAPA8_OPMODE_NOTIF,
9198 };
9199 
9200 static const u8 ath11k_if_types_ext_capa_sta[] = {
9201 	[0] = WLAN_EXT_CAPA1_EXT_CHANNEL_SWITCHING,
9202 	[2] = WLAN_EXT_CAPA3_MULTI_BSSID_SUPPORT,
9203 	[7] = WLAN_EXT_CAPA8_OPMODE_NOTIF,
9204 	[9] = WLAN_EXT_CAPA10_TWT_REQUESTER_SUPPORT,
9205 };
9206 
9207 static const u8 ath11k_if_types_ext_capa_ap[] = {
9208 	[0] = WLAN_EXT_CAPA1_EXT_CHANNEL_SWITCHING,
9209 	[2] = WLAN_EXT_CAPA3_MULTI_BSSID_SUPPORT,
9210 	[7] = WLAN_EXT_CAPA8_OPMODE_NOTIF,
9211 	[9] = WLAN_EXT_CAPA10_TWT_RESPONDER_SUPPORT,
9212 	[10] = WLAN_EXT_CAPA11_EMA_SUPPORT,
9213 };
9214 
9215 static const struct wiphy_iftype_ext_capab ath11k_iftypes_ext_capa[] = {
9216 	{
9217 		.extended_capabilities = ath11k_if_types_ext_capa,
9218 		.extended_capabilities_mask = ath11k_if_types_ext_capa,
9219 		.extended_capabilities_len = sizeof(ath11k_if_types_ext_capa),
9220 	}, {
9221 		.iftype = NL80211_IFTYPE_STATION,
9222 		.extended_capabilities = ath11k_if_types_ext_capa_sta,
9223 		.extended_capabilities_mask = ath11k_if_types_ext_capa_sta,
9224 		.extended_capabilities_len =
9225 				sizeof(ath11k_if_types_ext_capa_sta),
9226 	}, {
9227 		.iftype = NL80211_IFTYPE_AP,
9228 		.extended_capabilities = ath11k_if_types_ext_capa_ap,
9229 		.extended_capabilities_mask = ath11k_if_types_ext_capa_ap,
9230 		.extended_capabilities_len =
9231 				sizeof(ath11k_if_types_ext_capa_ap),
9232 	},
9233 };
9234 
9235 static void __ath11k_mac_unregister(struct ath11k *ar)
9236 {
9237 	cancel_work_sync(&ar->regd_update_work);
9238 
9239 	ieee80211_unregister_hw(ar->hw);
9240 
9241 	idr_for_each(&ar->txmgmt_idr, ath11k_mac_tx_mgmt_pending_free, ar);
9242 	idr_destroy(&ar->txmgmt_idr);
9243 
9244 	kfree(ar->mac.sbands[NL80211_BAND_2GHZ].channels);
9245 	kfree(ar->mac.sbands[NL80211_BAND_5GHZ].channels);
9246 	kfree(ar->mac.sbands[NL80211_BAND_6GHZ].channels);
9247 
9248 	kfree(ar->hw->wiphy->iface_combinations[0].limits);
9249 	kfree(ar->hw->wiphy->iface_combinations);
9250 
9251 	SET_IEEE80211_DEV(ar->hw, NULL);
9252 }
9253 
9254 void ath11k_mac_unregister(struct ath11k_base *ab)
9255 {
9256 	struct ath11k *ar;
9257 	struct ath11k_pdev *pdev;
9258 	int i;
9259 
9260 	for (i = 0; i < ab->num_radios; i++) {
9261 		pdev = &ab->pdevs[i];
9262 		ar = pdev->ar;
9263 		if (!ar)
9264 			continue;
9265 
9266 		__ath11k_mac_unregister(ar);
9267 	}
9268 
9269 	ath11k_peer_rhash_tbl_destroy(ab);
9270 }
9271 
9272 static int __ath11k_mac_register(struct ath11k *ar)
9273 {
9274 	struct ath11k_base *ab = ar->ab;
9275 	struct ath11k_pdev_cap *cap = &ar->pdev->cap;
9276 	static const u32 cipher_suites[] = {
9277 		WLAN_CIPHER_SUITE_TKIP,
9278 		WLAN_CIPHER_SUITE_CCMP,
9279 		WLAN_CIPHER_SUITE_AES_CMAC,
9280 		WLAN_CIPHER_SUITE_BIP_CMAC_256,
9281 		WLAN_CIPHER_SUITE_BIP_GMAC_128,
9282 		WLAN_CIPHER_SUITE_BIP_GMAC_256,
9283 		WLAN_CIPHER_SUITE_GCMP,
9284 		WLAN_CIPHER_SUITE_GCMP_256,
9285 		WLAN_CIPHER_SUITE_CCMP_256,
9286 	};
9287 	int ret;
9288 	u32 ht_cap = 0;
9289 
9290 	ath11k_pdev_caps_update(ar);
9291 
9292 	SET_IEEE80211_PERM_ADDR(ar->hw, ar->mac_addr);
9293 
9294 	SET_IEEE80211_DEV(ar->hw, ab->dev);
9295 
9296 	ret = ath11k_mac_setup_channels_rates(ar,
9297 					      cap->supported_bands);
9298 	if (ret)
9299 		goto err;
9300 
9301 	ath11k_mac_setup_ht_vht_cap(ar, cap, &ht_cap);
9302 	ath11k_mac_setup_he_cap(ar, cap);
9303 
9304 	ret = ath11k_mac_setup_iface_combinations(ar);
9305 	if (ret) {
9306 		ath11k_err(ar->ab, "failed to setup interface combinations: %d\n", ret);
9307 		goto err_free_channels;
9308 	}
9309 
9310 	ar->hw->wiphy->available_antennas_rx = cap->rx_chain_mask;
9311 	ar->hw->wiphy->available_antennas_tx = cap->tx_chain_mask;
9312 
9313 	ar->hw->wiphy->interface_modes = ab->hw_params.interface_modes;
9314 
9315 	if (ab->hw_params.single_pdev_only && ar->supports_6ghz)
9316 		ieee80211_hw_set(ar->hw, SINGLE_SCAN_ON_ALL_BANDS);
9317 
9318 	if (ab->hw_params.supports_multi_bssid) {
9319 		ieee80211_hw_set(ar->hw, SUPPORTS_MULTI_BSSID);
9320 		ieee80211_hw_set(ar->hw, SUPPORTS_ONLY_HE_MULTI_BSSID);
9321 	}
9322 
9323 	ieee80211_hw_set(ar->hw, SIGNAL_DBM);
9324 	ieee80211_hw_set(ar->hw, SUPPORTS_PS);
9325 	ieee80211_hw_set(ar->hw, SUPPORTS_DYNAMIC_PS);
9326 	ieee80211_hw_set(ar->hw, MFP_CAPABLE);
9327 	ieee80211_hw_set(ar->hw, REPORTS_TX_ACK_STATUS);
9328 	ieee80211_hw_set(ar->hw, HAS_RATE_CONTROL);
9329 	ieee80211_hw_set(ar->hw, AP_LINK_PS);
9330 	ieee80211_hw_set(ar->hw, SPECTRUM_MGMT);
9331 	ieee80211_hw_set(ar->hw, CONNECTION_MONITOR);
9332 	ieee80211_hw_set(ar->hw, SUPPORTS_PER_STA_GTK);
9333 	ieee80211_hw_set(ar->hw, WANT_MONITOR_VIF);
9334 	ieee80211_hw_set(ar->hw, CHANCTX_STA_CSA);
9335 	ieee80211_hw_set(ar->hw, QUEUE_CONTROL);
9336 	ieee80211_hw_set(ar->hw, SUPPORTS_TX_FRAG);
9337 	ieee80211_hw_set(ar->hw, REPORTS_LOW_ACK);
9338 
9339 	if (ath11k_frame_mode == ATH11K_HW_TXRX_ETHERNET) {
9340 		ieee80211_hw_set(ar->hw, SUPPORTS_TX_ENCAP_OFFLOAD);
9341 		ieee80211_hw_set(ar->hw, SUPPORTS_RX_DECAP_OFFLOAD);
9342 	}
9343 
9344 	if (cap->nss_ratio_enabled)
9345 		ieee80211_hw_set(ar->hw, SUPPORTS_VHT_EXT_NSS_BW);
9346 
9347 	if ((ht_cap & WMI_HT_CAP_ENABLED) || ar->supports_6ghz) {
9348 		ieee80211_hw_set(ar->hw, AMPDU_AGGREGATION);
9349 		ieee80211_hw_set(ar->hw, TX_AMPDU_SETUP_IN_HW);
9350 		ieee80211_hw_set(ar->hw, SUPPORTS_REORDERING_BUFFER);
9351 		ieee80211_hw_set(ar->hw, SUPPORTS_AMSDU_IN_AMPDU);
9352 		ieee80211_hw_set(ar->hw, USES_RSS);
9353 	}
9354 
9355 	ar->hw->wiphy->features |= NL80211_FEATURE_STATIC_SMPS;
9356 	ar->hw->wiphy->flags |= WIPHY_FLAG_IBSS_RSN;
9357 
9358 	/* TODO: Check if HT capability advertised from firmware is different
9359 	 * for each band for a dual band capable radio. It will be tricky to
9360 	 * handle it when the ht capability different for each band.
9361 	 */
9362 	if (ht_cap & WMI_HT_CAP_DYNAMIC_SMPS ||
9363 	    (ar->supports_6ghz && ab->hw_params.supports_dynamic_smps_6ghz))
9364 		ar->hw->wiphy->features |= NL80211_FEATURE_DYNAMIC_SMPS;
9365 
9366 	ar->hw->wiphy->max_scan_ssids = WLAN_SCAN_PARAMS_MAX_SSID;
9367 	ar->hw->wiphy->max_scan_ie_len = WLAN_SCAN_PARAMS_MAX_IE_LEN;
9368 
9369 	ar->hw->max_listen_interval = ATH11K_MAX_HW_LISTEN_INTERVAL;
9370 
9371 	ar->hw->wiphy->flags |= WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL;
9372 	ar->hw->wiphy->flags |= WIPHY_FLAG_HAS_CHANNEL_SWITCH;
9373 	ar->hw->wiphy->max_remain_on_channel_duration = 5000;
9374 
9375 	ar->hw->wiphy->flags |= WIPHY_FLAG_AP_UAPSD;
9376 	ar->hw->wiphy->features |= NL80211_FEATURE_AP_MODE_CHAN_WIDTH_CHANGE |
9377 				   NL80211_FEATURE_AP_SCAN;
9378 
9379 	ar->max_num_stations = TARGET_NUM_STATIONS(ab);
9380 	ar->max_num_peers = TARGET_NUM_PEERS_PDEV(ab);
9381 
9382 	ar->hw->wiphy->max_ap_assoc_sta = ar->max_num_stations;
9383 
9384 	if (test_bit(WMI_TLV_SERVICE_SPOOF_MAC_SUPPORT, ar->wmi->wmi_ab->svc_map)) {
9385 		ar->hw->wiphy->features |=
9386 			NL80211_FEATURE_SCAN_RANDOM_MAC_ADDR;
9387 	}
9388 
9389 	if (test_bit(WMI_TLV_SERVICE_NLO, ar->wmi->wmi_ab->svc_map)) {
9390 		ar->hw->wiphy->max_sched_scan_ssids = WMI_PNO_MAX_SUPP_NETWORKS;
9391 		ar->hw->wiphy->max_match_sets = WMI_PNO_MAX_SUPP_NETWORKS;
9392 		ar->hw->wiphy->max_sched_scan_ie_len = WMI_PNO_MAX_IE_LENGTH;
9393 		ar->hw->wiphy->max_sched_scan_plans = WMI_PNO_MAX_SCHED_SCAN_PLANS;
9394 		ar->hw->wiphy->max_sched_scan_plan_interval =
9395 			WMI_PNO_MAX_SCHED_SCAN_PLAN_INT;
9396 		ar->hw->wiphy->max_sched_scan_plan_iterations =
9397 			WMI_PNO_MAX_SCHED_SCAN_PLAN_ITRNS;
9398 		ar->hw->wiphy->features |= NL80211_FEATURE_ND_RANDOM_MAC_ADDR;
9399 	}
9400 
9401 	ret = ath11k_wow_init(ar);
9402 	if (ret) {
9403 		ath11k_warn(ar->ab, "failed to init wow: %d\n", ret);
9404 		goto err_free_if_combs;
9405 	}
9406 
9407 	if (test_bit(WMI_TLV_SERVICE_TX_DATA_MGMT_ACK_RSSI,
9408 		     ar->ab->wmi_ab.svc_map))
9409 		wiphy_ext_feature_set(ar->hw->wiphy,
9410 				      NL80211_EXT_FEATURE_ACK_SIGNAL_SUPPORT);
9411 
9412 	ar->hw->queues = ATH11K_HW_MAX_QUEUES;
9413 	ar->hw->wiphy->tx_queue_len = ATH11K_QUEUE_LEN;
9414 	ar->hw->offchannel_tx_hw_queue = ATH11K_HW_MAX_QUEUES - 1;
9415 	ar->hw->max_rx_aggregation_subframes = IEEE80211_MAX_AMPDU_BUF_HE;
9416 
9417 	ar->hw->vif_data_size = sizeof(struct ath11k_vif);
9418 	ar->hw->sta_data_size = sizeof(struct ath11k_sta);
9419 
9420 	wiphy_ext_feature_set(ar->hw->wiphy, NL80211_EXT_FEATURE_CQM_RSSI_LIST);
9421 	wiphy_ext_feature_set(ar->hw->wiphy, NL80211_EXT_FEATURE_STA_TX_PWR);
9422 	if (test_bit(WMI_TLV_SERVICE_BSS_COLOR_OFFLOAD,
9423 		     ar->ab->wmi_ab.svc_map)) {
9424 		wiphy_ext_feature_set(ar->hw->wiphy,
9425 				      NL80211_EXT_FEATURE_BSS_COLOR);
9426 		ieee80211_hw_set(ar->hw, DETECTS_COLOR_COLLISION);
9427 	}
9428 
9429 	ar->hw->wiphy->cipher_suites = cipher_suites;
9430 	ar->hw->wiphy->n_cipher_suites = ARRAY_SIZE(cipher_suites);
9431 
9432 	ar->hw->wiphy->iftype_ext_capab = ath11k_iftypes_ext_capa;
9433 	ar->hw->wiphy->num_iftype_ext_capab =
9434 		ARRAY_SIZE(ath11k_iftypes_ext_capa);
9435 
9436 	if (ar->supports_6ghz) {
9437 		wiphy_ext_feature_set(ar->hw->wiphy,
9438 				      NL80211_EXT_FEATURE_FILS_DISCOVERY);
9439 		wiphy_ext_feature_set(ar->hw->wiphy,
9440 				      NL80211_EXT_FEATURE_UNSOL_BCAST_PROBE_RESP);
9441 	}
9442 
9443 	wiphy_ext_feature_set(ar->hw->wiphy,
9444 			      NL80211_EXT_FEATURE_SET_SCAN_DWELL);
9445 
9446 	if (test_bit(WMI_TLV_SERVICE_RTT, ar->ab->wmi_ab.svc_map))
9447 		wiphy_ext_feature_set(ar->hw->wiphy,
9448 				      NL80211_EXT_FEATURE_ENABLE_FTM_RESPONDER);
9449 
9450 	ar->hw->wiphy->mbssid_max_interfaces = TARGET_NUM_VDEVS(ab);
9451 	ar->hw->wiphy->ema_max_profile_periodicity = TARGET_EMA_MAX_PROFILE_PERIOD;
9452 
9453 	ath11k_reg_init(ar);
9454 
9455 	if (!test_bit(ATH11K_FLAG_RAW_MODE, &ab->dev_flags)) {
9456 		ar->hw->netdev_features = NETIF_F_HW_CSUM;
9457 		ieee80211_hw_set(ar->hw, SW_CRYPTO_CONTROL);
9458 		ieee80211_hw_set(ar->hw, SUPPORT_FAST_XMIT);
9459 	}
9460 
9461 	if (test_bit(WMI_TLV_SERVICE_BIOS_SAR_SUPPORT, ar->ab->wmi_ab.svc_map) &&
9462 	    ab->hw_params.bios_sar_capa)
9463 		ar->hw->wiphy->sar_capa = ab->hw_params.bios_sar_capa;
9464 
9465 	ret = ieee80211_register_hw(ar->hw);
9466 	if (ret) {
9467 		ath11k_err(ar->ab, "ieee80211 registration failed: %d\n", ret);
9468 		goto err_free_if_combs;
9469 	}
9470 
9471 	if (!ab->hw_params.supports_monitor)
9472 		/* There's a race between calling ieee80211_register_hw()
9473 		 * and here where the monitor mode is enabled for a little
9474 		 * while. But that time is so short and in practise it make
9475 		 * a difference in real life.
9476 		 */
9477 		ar->hw->wiphy->interface_modes &= ~BIT(NL80211_IFTYPE_MONITOR);
9478 
9479 	/* Apply the regd received during initialization */
9480 	ret = ath11k_regd_update(ar);
9481 	if (ret) {
9482 		ath11k_err(ar->ab, "ath11k regd update failed: %d\n", ret);
9483 		goto err_unregister_hw;
9484 	}
9485 
9486 	if (ab->hw_params.current_cc_support && ab->new_alpha2[0]) {
9487 		struct wmi_set_current_country_params set_current_param = {};
9488 
9489 		memcpy(&set_current_param.alpha2, ab->new_alpha2, 2);
9490 		memcpy(&ar->alpha2, ab->new_alpha2, 2);
9491 		ret = ath11k_wmi_send_set_current_country_cmd(ar, &set_current_param);
9492 		if (ret)
9493 			ath11k_warn(ar->ab,
9494 				    "failed set cc code for mac register: %d\n", ret);
9495 	}
9496 
9497 	ret = ath11k_debugfs_register(ar);
9498 	if (ret) {
9499 		ath11k_err(ar->ab, "debugfs registration failed: %d\n", ret);
9500 		goto err_unregister_hw;
9501 	}
9502 
9503 	return 0;
9504 
9505 err_unregister_hw:
9506 	ieee80211_unregister_hw(ar->hw);
9507 
9508 err_free_if_combs:
9509 	kfree(ar->hw->wiphy->iface_combinations[0].limits);
9510 	kfree(ar->hw->wiphy->iface_combinations);
9511 
9512 err_free_channels:
9513 	kfree(ar->mac.sbands[NL80211_BAND_2GHZ].channels);
9514 	kfree(ar->mac.sbands[NL80211_BAND_5GHZ].channels);
9515 	kfree(ar->mac.sbands[NL80211_BAND_6GHZ].channels);
9516 
9517 err:
9518 	SET_IEEE80211_DEV(ar->hw, NULL);
9519 	return ret;
9520 }
9521 
9522 int ath11k_mac_register(struct ath11k_base *ab)
9523 {
9524 	struct ath11k *ar;
9525 	struct ath11k_pdev *pdev;
9526 	int i;
9527 	int ret;
9528 	u8 mac_addr[ETH_ALEN] = {0};
9529 
9530 	if (test_bit(ATH11K_FLAG_REGISTERED, &ab->dev_flags))
9531 		return 0;
9532 
9533 	/* Initialize channel counters frequency value in hertz */
9534 	ab->cc_freq_hz = IPQ8074_CC_FREQ_HERTZ;
9535 	ab->free_vdev_map = (1LL << (ab->num_radios * TARGET_NUM_VDEVS(ab))) - 1;
9536 
9537 	ret = ath11k_peer_rhash_tbl_init(ab);
9538 	if (ret)
9539 		return ret;
9540 
9541 	device_get_mac_address(ab->dev, mac_addr);
9542 
9543 	for (i = 0; i < ab->num_radios; i++) {
9544 		pdev = &ab->pdevs[i];
9545 		ar = pdev->ar;
9546 		if (ab->pdevs_macaddr_valid) {
9547 			ether_addr_copy(ar->mac_addr, pdev->mac_addr);
9548 		} else {
9549 			if (is_zero_ether_addr(mac_addr))
9550 				ether_addr_copy(ar->mac_addr, ab->mac_addr);
9551 			else
9552 				ether_addr_copy(ar->mac_addr, mac_addr);
9553 			ar->mac_addr[4] += i;
9554 		}
9555 
9556 		idr_init(&ar->txmgmt_idr);
9557 		spin_lock_init(&ar->txmgmt_idr_lock);
9558 
9559 		ret = __ath11k_mac_register(ar);
9560 		if (ret)
9561 			goto err_cleanup;
9562 
9563 		init_waitqueue_head(&ar->txmgmt_empty_waitq);
9564 	}
9565 
9566 	return 0;
9567 
9568 err_cleanup:
9569 	for (i = i - 1; i >= 0; i--) {
9570 		pdev = &ab->pdevs[i];
9571 		ar = pdev->ar;
9572 		__ath11k_mac_unregister(ar);
9573 	}
9574 
9575 	ath11k_peer_rhash_tbl_destroy(ab);
9576 
9577 	return ret;
9578 }
9579 
9580 int ath11k_mac_allocate(struct ath11k_base *ab)
9581 {
9582 	struct ieee80211_hw *hw;
9583 	struct ath11k *ar;
9584 	struct ath11k_pdev *pdev;
9585 	int ret;
9586 	int i;
9587 
9588 	if (test_bit(ATH11K_FLAG_REGISTERED, &ab->dev_flags))
9589 		return 0;
9590 
9591 	for (i = 0; i < ab->num_radios; i++) {
9592 		pdev = &ab->pdevs[i];
9593 		hw = ieee80211_alloc_hw(sizeof(struct ath11k), &ath11k_ops);
9594 		if (!hw) {
9595 			ath11k_warn(ab, "failed to allocate mac80211 hw device\n");
9596 			ret = -ENOMEM;
9597 			goto err_free_mac;
9598 		}
9599 
9600 		ar = hw->priv;
9601 		ar->hw = hw;
9602 		ar->ab = ab;
9603 		ar->pdev = pdev;
9604 		ar->pdev_idx = i;
9605 		ar->lmac_id = ath11k_hw_get_mac_from_pdev_id(&ab->hw_params, i);
9606 
9607 		ar->wmi = &ab->wmi_ab.wmi[i];
9608 		/* FIXME wmi[0] is already initialized during attach,
9609 		 * Should we do this again?
9610 		 */
9611 		ath11k_wmi_pdev_attach(ab, i);
9612 
9613 		ar->cfg_tx_chainmask = pdev->cap.tx_chain_mask;
9614 		ar->cfg_rx_chainmask = pdev->cap.rx_chain_mask;
9615 		ar->num_tx_chains = get_num_chains(pdev->cap.tx_chain_mask);
9616 		ar->num_rx_chains = get_num_chains(pdev->cap.rx_chain_mask);
9617 
9618 		pdev->ar = ar;
9619 		spin_lock_init(&ar->data_lock);
9620 		INIT_LIST_HEAD(&ar->arvifs);
9621 		INIT_LIST_HEAD(&ar->ppdu_stats_info);
9622 		mutex_init(&ar->conf_mutex);
9623 		init_completion(&ar->vdev_setup_done);
9624 		init_completion(&ar->vdev_delete_done);
9625 		init_completion(&ar->peer_assoc_done);
9626 		init_completion(&ar->peer_delete_done);
9627 		init_completion(&ar->install_key_done);
9628 		init_completion(&ar->bss_survey_done);
9629 		init_completion(&ar->scan.started);
9630 		init_completion(&ar->scan.completed);
9631 		init_completion(&ar->scan.on_channel);
9632 		init_completion(&ar->thermal.wmi_sync);
9633 
9634 		INIT_DELAYED_WORK(&ar->scan.timeout, ath11k_scan_timeout_work);
9635 		INIT_WORK(&ar->regd_update_work, ath11k_regd_update_work);
9636 
9637 		INIT_WORK(&ar->wmi_mgmt_tx_work, ath11k_mgmt_over_wmi_tx_work);
9638 		skb_queue_head_init(&ar->wmi_mgmt_tx_queue);
9639 
9640 		clear_bit(ATH11K_FLAG_MONITOR_STARTED, &ar->monitor_flags);
9641 
9642 		ar->monitor_vdev_id = -1;
9643 		clear_bit(ATH11K_FLAG_MONITOR_VDEV_CREATED, &ar->monitor_flags);
9644 		ar->vdev_id_11d_scan = ATH11K_11D_INVALID_VDEV_ID;
9645 		init_completion(&ar->completed_11d_scan);
9646 
9647 		ath11k_fw_stats_init(ar);
9648 	}
9649 
9650 	return 0;
9651 
9652 err_free_mac:
9653 	ath11k_mac_destroy(ab);
9654 
9655 	return ret;
9656 }
9657 
9658 void ath11k_mac_destroy(struct ath11k_base *ab)
9659 {
9660 	struct ath11k *ar;
9661 	struct ath11k_pdev *pdev;
9662 	int i;
9663 
9664 	for (i = 0; i < ab->num_radios; i++) {
9665 		pdev = &ab->pdevs[i];
9666 		ar = pdev->ar;
9667 		if (!ar)
9668 			continue;
9669 
9670 		ieee80211_free_hw(ar->hw);
9671 		pdev->ar = NULL;
9672 	}
9673 }
9674 
9675 int ath11k_mac_vif_set_keepalive(struct ath11k_vif *arvif,
9676 				 enum wmi_sta_keepalive_method method,
9677 				 u32 interval)
9678 {
9679 	struct ath11k *ar = arvif->ar;
9680 	struct wmi_sta_keepalive_arg arg = {};
9681 	int ret;
9682 
9683 	lockdep_assert_held(&ar->conf_mutex);
9684 
9685 	if (arvif->vdev_type != WMI_VDEV_TYPE_STA)
9686 		return 0;
9687 
9688 	if (!test_bit(WMI_TLV_SERVICE_STA_KEEP_ALIVE, ar->ab->wmi_ab.svc_map))
9689 		return 0;
9690 
9691 	arg.vdev_id = arvif->vdev_id;
9692 	arg.enabled = 1;
9693 	arg.method = method;
9694 	arg.interval = interval;
9695 
9696 	ret = ath11k_wmi_sta_keepalive(ar, &arg);
9697 	if (ret) {
9698 		ath11k_warn(ar->ab, "failed to set keepalive on vdev %i: %d\n",
9699 			    arvif->vdev_id, ret);
9700 		return ret;
9701 	}
9702 
9703 	return 0;
9704 }
9705