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