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