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