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