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