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