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