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