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