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