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