xref: /openbmc/linux/drivers/net/wireless/ath/ath11k/mac.c (revision 424e5ac9)
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 		arg.key_cipher = WMI_CIPHER_AES_CCM;
4134 		/* TODO: Re-check if flag is valid */
4135 		key->flags |= IEEE80211_KEY_FLAG_GENERATE_IV_MGMT;
4136 		break;
4137 	case WLAN_CIPHER_SUITE_TKIP:
4138 		arg.key_cipher = WMI_CIPHER_TKIP;
4139 		arg.key_txmic_len = 8;
4140 		arg.key_rxmic_len = 8;
4141 		break;
4142 	case WLAN_CIPHER_SUITE_CCMP_256:
4143 		arg.key_cipher = WMI_CIPHER_AES_CCM;
4144 		break;
4145 	case WLAN_CIPHER_SUITE_GCMP:
4146 	case WLAN_CIPHER_SUITE_GCMP_256:
4147 		arg.key_cipher = WMI_CIPHER_AES_GCM;
4148 		break;
4149 	default:
4150 		ath11k_warn(ar->ab, "cipher %d is not supported\n", key->cipher);
4151 		return -EOPNOTSUPP;
4152 	}
4153 
4154 	if (test_bit(ATH11K_FLAG_RAW_MODE, &ar->ab->dev_flags))
4155 		key->flags |= IEEE80211_KEY_FLAG_GENERATE_IV |
4156 			      IEEE80211_KEY_FLAG_RESERVE_TAILROOM;
4157 
4158 install:
4159 	ret = ath11k_wmi_vdev_install_key(arvif->ar, &arg);
4160 
4161 	if (ret)
4162 		return ret;
4163 
4164 	if (!wait_for_completion_timeout(&ar->install_key_done, 1 * HZ))
4165 		return -ETIMEDOUT;
4166 
4167 	return ar->install_key_status ? -EINVAL : 0;
4168 }
4169 
ath11k_clear_peer_keys(struct ath11k_vif * arvif,const u8 * addr)4170 static int ath11k_clear_peer_keys(struct ath11k_vif *arvif,
4171 				  const u8 *addr)
4172 {
4173 	struct ath11k *ar = arvif->ar;
4174 	struct ath11k_base *ab = ar->ab;
4175 	struct ath11k_peer *peer;
4176 	int first_errno = 0;
4177 	int ret;
4178 	int i;
4179 	u32 flags = 0;
4180 
4181 	lockdep_assert_held(&ar->conf_mutex);
4182 
4183 	spin_lock_bh(&ab->base_lock);
4184 	peer = ath11k_peer_find(ab, arvif->vdev_id, addr);
4185 	spin_unlock_bh(&ab->base_lock);
4186 
4187 	if (!peer)
4188 		return -ENOENT;
4189 
4190 	for (i = 0; i < ARRAY_SIZE(peer->keys); i++) {
4191 		if (!peer->keys[i])
4192 			continue;
4193 
4194 		/* key flags are not required to delete the key */
4195 		ret = ath11k_install_key(arvif, peer->keys[i],
4196 					 DISABLE_KEY, addr, flags);
4197 		if (ret < 0 && first_errno == 0)
4198 			first_errno = ret;
4199 
4200 		if (ret < 0)
4201 			ath11k_warn(ab, "failed to remove peer key %d: %d\n",
4202 				    i, ret);
4203 
4204 		spin_lock_bh(&ab->base_lock);
4205 		peer->keys[i] = NULL;
4206 		spin_unlock_bh(&ab->base_lock);
4207 	}
4208 
4209 	return first_errno;
4210 }
4211 
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)4212 static int ath11k_mac_op_set_key(struct ieee80211_hw *hw, enum set_key_cmd cmd,
4213 				 struct ieee80211_vif *vif, struct ieee80211_sta *sta,
4214 				 struct ieee80211_key_conf *key)
4215 {
4216 	struct ath11k *ar = hw->priv;
4217 	struct ath11k_base *ab = ar->ab;
4218 	struct ath11k_vif *arvif = ath11k_vif_to_arvif(vif);
4219 	struct ath11k_peer *peer;
4220 	struct ath11k_sta *arsta;
4221 	const u8 *peer_addr;
4222 	int ret = 0;
4223 	u32 flags = 0;
4224 
4225 	/* BIP needs to be done in software */
4226 	if (key->cipher == WLAN_CIPHER_SUITE_AES_CMAC ||
4227 	    key->cipher == WLAN_CIPHER_SUITE_BIP_GMAC_128 ||
4228 	    key->cipher == WLAN_CIPHER_SUITE_BIP_GMAC_256 ||
4229 	    key->cipher == WLAN_CIPHER_SUITE_BIP_CMAC_256)
4230 		return 1;
4231 
4232 	if (test_bit(ATH11K_FLAG_HW_CRYPTO_DISABLED, &ar->ab->dev_flags))
4233 		return 1;
4234 
4235 	if (key->keyidx > WMI_MAX_KEY_INDEX)
4236 		return -ENOSPC;
4237 
4238 	mutex_lock(&ar->conf_mutex);
4239 
4240 	if (sta)
4241 		peer_addr = sta->addr;
4242 	else if (arvif->vdev_type == WMI_VDEV_TYPE_STA)
4243 		peer_addr = vif->bss_conf.bssid;
4244 	else
4245 		peer_addr = vif->addr;
4246 
4247 	key->hw_key_idx = key->keyidx;
4248 
4249 	/* the peer should not disappear in mid-way (unless FW goes awry) since
4250 	 * we already hold conf_mutex. we just make sure its there now.
4251 	 */
4252 	spin_lock_bh(&ab->base_lock);
4253 	peer = ath11k_peer_find(ab, arvif->vdev_id, peer_addr);
4254 
4255 	/* flush the fragments cache during key (re)install to
4256 	 * ensure all frags in the new frag list belong to the same key.
4257 	 */
4258 	if (peer && sta && cmd == SET_KEY)
4259 		ath11k_peer_frags_flush(ar, peer);
4260 	spin_unlock_bh(&ab->base_lock);
4261 
4262 	if (!peer) {
4263 		if (cmd == SET_KEY) {
4264 			ath11k_warn(ab, "cannot install key for non-existent peer %pM\n",
4265 				    peer_addr);
4266 			ret = -EOPNOTSUPP;
4267 			goto exit;
4268 		} else {
4269 			/* if the peer doesn't exist there is no key to disable
4270 			 * anymore
4271 			 */
4272 			goto exit;
4273 		}
4274 	}
4275 
4276 	if (key->flags & IEEE80211_KEY_FLAG_PAIRWISE)
4277 		flags |= WMI_KEY_PAIRWISE;
4278 	else
4279 		flags |= WMI_KEY_GROUP;
4280 
4281 	ret = ath11k_install_key(arvif, key, cmd, peer_addr, flags);
4282 	if (ret) {
4283 		ath11k_warn(ab, "ath11k_install_key failed (%d)\n", ret);
4284 		goto exit;
4285 	}
4286 
4287 	ret = ath11k_dp_peer_rx_pn_replay_config(arvif, peer_addr, cmd, key);
4288 	if (ret) {
4289 		ath11k_warn(ab, "failed to offload PN replay detection %d\n", ret);
4290 		goto exit;
4291 	}
4292 
4293 	spin_lock_bh(&ab->base_lock);
4294 	peer = ath11k_peer_find(ab, arvif->vdev_id, peer_addr);
4295 	if (peer && cmd == SET_KEY) {
4296 		peer->keys[key->keyidx] = key;
4297 		if (key->flags & IEEE80211_KEY_FLAG_PAIRWISE) {
4298 			peer->ucast_keyidx = key->keyidx;
4299 			peer->sec_type = ath11k_dp_tx_get_encrypt_type(key->cipher);
4300 		} else {
4301 			peer->mcast_keyidx = key->keyidx;
4302 			peer->sec_type_grp = ath11k_dp_tx_get_encrypt_type(key->cipher);
4303 		}
4304 	} else if (peer && cmd == DISABLE_KEY) {
4305 		peer->keys[key->keyidx] = NULL;
4306 		if (key->flags & IEEE80211_KEY_FLAG_PAIRWISE)
4307 			peer->ucast_keyidx = 0;
4308 		else
4309 			peer->mcast_keyidx = 0;
4310 	} else if (!peer)
4311 		/* impossible unless FW goes crazy */
4312 		ath11k_warn(ab, "peer %pM disappeared!\n", peer_addr);
4313 
4314 	if (sta) {
4315 		arsta = (struct ath11k_sta *)sta->drv_priv;
4316 
4317 		switch (key->cipher) {
4318 		case WLAN_CIPHER_SUITE_TKIP:
4319 		case WLAN_CIPHER_SUITE_CCMP:
4320 		case WLAN_CIPHER_SUITE_CCMP_256:
4321 		case WLAN_CIPHER_SUITE_GCMP:
4322 		case WLAN_CIPHER_SUITE_GCMP_256:
4323 			if (cmd == SET_KEY)
4324 				arsta->pn_type = HAL_PN_TYPE_WPA;
4325 			else
4326 				arsta->pn_type = HAL_PN_TYPE_NONE;
4327 			break;
4328 		default:
4329 			arsta->pn_type = HAL_PN_TYPE_NONE;
4330 			break;
4331 		}
4332 	}
4333 
4334 	spin_unlock_bh(&ab->base_lock);
4335 
4336 exit:
4337 	mutex_unlock(&ar->conf_mutex);
4338 	return ret;
4339 }
4340 
4341 static int
ath11k_mac_bitrate_mask_num_ht_rates(struct ath11k * ar,enum nl80211_band band,const struct cfg80211_bitrate_mask * mask)4342 ath11k_mac_bitrate_mask_num_ht_rates(struct ath11k *ar,
4343 				     enum nl80211_band band,
4344 				     const struct cfg80211_bitrate_mask *mask)
4345 {
4346 	int num_rates = 0;
4347 	int i;
4348 
4349 	for (i = 0; i < ARRAY_SIZE(mask->control[band].ht_mcs); i++)
4350 		num_rates += hweight8(mask->control[band].ht_mcs[i]);
4351 
4352 	return num_rates;
4353 }
4354 
4355 static int
ath11k_mac_bitrate_mask_num_vht_rates(struct ath11k * ar,enum nl80211_band band,const struct cfg80211_bitrate_mask * mask)4356 ath11k_mac_bitrate_mask_num_vht_rates(struct ath11k *ar,
4357 				      enum nl80211_band band,
4358 				      const struct cfg80211_bitrate_mask *mask)
4359 {
4360 	int num_rates = 0;
4361 	int i;
4362 
4363 	for (i = 0; i < ARRAY_SIZE(mask->control[band].vht_mcs); i++)
4364 		num_rates += hweight16(mask->control[band].vht_mcs[i]);
4365 
4366 	return num_rates;
4367 }
4368 
4369 static int
ath11k_mac_bitrate_mask_num_he_rates(struct ath11k * ar,enum nl80211_band band,const struct cfg80211_bitrate_mask * mask)4370 ath11k_mac_bitrate_mask_num_he_rates(struct ath11k *ar,
4371 				     enum nl80211_band band,
4372 				     const struct cfg80211_bitrate_mask *mask)
4373 {
4374 	int num_rates = 0;
4375 	int i;
4376 
4377 	for (i = 0; i < ARRAY_SIZE(mask->control[band].he_mcs); i++)
4378 		num_rates += hweight16(mask->control[band].he_mcs[i]);
4379 
4380 	return num_rates;
4381 }
4382 
4383 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)4384 ath11k_mac_set_peer_vht_fixed_rate(struct ath11k_vif *arvif,
4385 				   struct ieee80211_sta *sta,
4386 				   const struct cfg80211_bitrate_mask *mask,
4387 				   enum nl80211_band band)
4388 {
4389 	struct ath11k *ar = arvif->ar;
4390 	u8 vht_rate, nss;
4391 	u32 rate_code;
4392 	int ret, i;
4393 
4394 	lockdep_assert_held(&ar->conf_mutex);
4395 
4396 	nss = 0;
4397 
4398 	for (i = 0; i < ARRAY_SIZE(mask->control[band].vht_mcs); i++) {
4399 		if (hweight16(mask->control[band].vht_mcs[i]) == 1) {
4400 			nss = i + 1;
4401 			vht_rate = ffs(mask->control[band].vht_mcs[i]) - 1;
4402 		}
4403 	}
4404 
4405 	if (!nss) {
4406 		ath11k_warn(ar->ab, "No single VHT Fixed rate found to set for %pM",
4407 			    sta->addr);
4408 		return -EINVAL;
4409 	}
4410 
4411 	/* Avoid updating invalid nss as fixed rate*/
4412 	if (nss > sta->deflink.rx_nss)
4413 		return -EINVAL;
4414 
4415 	ath11k_dbg(ar->ab, ATH11K_DBG_MAC,
4416 		   "Setting Fixed VHT Rate for peer %pM. Device will not switch to any other selected rates",
4417 		   sta->addr);
4418 
4419 	rate_code = ATH11K_HW_RATE_CODE(vht_rate, nss - 1,
4420 					WMI_RATE_PREAMBLE_VHT);
4421 	ret = ath11k_wmi_set_peer_param(ar, sta->addr,
4422 					arvif->vdev_id,
4423 					WMI_PEER_PARAM_FIXED_RATE,
4424 					rate_code);
4425 	if (ret)
4426 		ath11k_warn(ar->ab,
4427 			    "failed to update STA %pM Fixed Rate %d: %d\n",
4428 			     sta->addr, rate_code, ret);
4429 
4430 	return ret;
4431 }
4432 
4433 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)4434 ath11k_mac_set_peer_he_fixed_rate(struct ath11k_vif *arvif,
4435 				  struct ieee80211_sta *sta,
4436 				  const struct cfg80211_bitrate_mask *mask,
4437 				  enum nl80211_band band)
4438 {
4439 	struct ath11k *ar = arvif->ar;
4440 	u8 he_rate, nss;
4441 	u32 rate_code;
4442 	int ret, i;
4443 
4444 	lockdep_assert_held(&ar->conf_mutex);
4445 
4446 	nss = 0;
4447 
4448 	for (i = 0; i < ARRAY_SIZE(mask->control[band].he_mcs); i++) {
4449 		if (hweight16(mask->control[band].he_mcs[i]) == 1) {
4450 			nss = i + 1;
4451 			he_rate = ffs(mask->control[band].he_mcs[i]) - 1;
4452 		}
4453 	}
4454 
4455 	if (!nss) {
4456 		ath11k_warn(ar->ab, "No single he fixed rate found to set for %pM",
4457 			    sta->addr);
4458 		return -EINVAL;
4459 	}
4460 
4461 	/* Avoid updating invalid nss as fixed rate */
4462 	if (nss > sta->deflink.rx_nss)
4463 		return -EINVAL;
4464 
4465 	ath11k_dbg(ar->ab, ATH11K_DBG_MAC,
4466 		   "setting fixed he rate for peer %pM, device will not switch to any other selected rates",
4467 		   sta->addr);
4468 
4469 	rate_code = ATH11K_HW_RATE_CODE(he_rate, nss - 1,
4470 					WMI_RATE_PREAMBLE_HE);
4471 
4472 	ret = ath11k_wmi_set_peer_param(ar, sta->addr,
4473 					arvif->vdev_id,
4474 					WMI_PEER_PARAM_FIXED_RATE,
4475 					rate_code);
4476 	if (ret)
4477 		ath11k_warn(ar->ab,
4478 			    "failed to update sta %pM fixed rate %d: %d\n",
4479 			    sta->addr, rate_code, ret);
4480 
4481 	return ret;
4482 }
4483 
4484 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)4485 ath11k_mac_set_peer_ht_fixed_rate(struct ath11k_vif *arvif,
4486 				  struct ieee80211_sta *sta,
4487 				  const struct cfg80211_bitrate_mask *mask,
4488 				  enum nl80211_band band)
4489 {
4490 	struct ath11k *ar = arvif->ar;
4491 	u8 ht_rate, nss = 0;
4492 	u32 rate_code;
4493 	int ret, i;
4494 
4495 	lockdep_assert_held(&ar->conf_mutex);
4496 
4497 	for (i = 0; i < ARRAY_SIZE(mask->control[band].ht_mcs); i++) {
4498 		if (hweight8(mask->control[band].ht_mcs[i]) == 1) {
4499 			nss = i + 1;
4500 			ht_rate = ffs(mask->control[band].ht_mcs[i]) - 1;
4501 		}
4502 	}
4503 
4504 	if (!nss) {
4505 		ath11k_warn(ar->ab, "No single HT Fixed rate found to set for %pM",
4506 			    sta->addr);
4507 		return -EINVAL;
4508 	}
4509 
4510 	/* Avoid updating invalid nss as fixed rate*/
4511 	if (nss > sta->deflink.rx_nss)
4512 		return -EINVAL;
4513 
4514 	ath11k_dbg(ar->ab, ATH11K_DBG_MAC,
4515 		   "Setting Fixed HT Rate for peer %pM. Device will not switch to any other selected rates",
4516 		   sta->addr);
4517 
4518 	rate_code = ATH11K_HW_RATE_CODE(ht_rate, nss - 1,
4519 					WMI_RATE_PREAMBLE_HT);
4520 	ret = ath11k_wmi_set_peer_param(ar, sta->addr,
4521 					arvif->vdev_id,
4522 					WMI_PEER_PARAM_FIXED_RATE,
4523 					rate_code);
4524 	if (ret)
4525 		ath11k_warn(ar->ab,
4526 			    "failed to update STA %pM HT Fixed Rate %d: %d\n",
4527 			    sta->addr, rate_code, ret);
4528 
4529 	return ret;
4530 }
4531 
ath11k_station_assoc(struct ath11k * ar,struct ieee80211_vif * vif,struct ieee80211_sta * sta,bool reassoc)4532 static int ath11k_station_assoc(struct ath11k *ar,
4533 				struct ieee80211_vif *vif,
4534 				struct ieee80211_sta *sta,
4535 				bool reassoc)
4536 {
4537 	struct ath11k_vif *arvif = ath11k_vif_to_arvif(vif);
4538 	struct peer_assoc_params peer_arg;
4539 	int ret = 0;
4540 	struct cfg80211_chan_def def;
4541 	enum nl80211_band band;
4542 	struct cfg80211_bitrate_mask *mask;
4543 	u8 num_ht_rates, num_vht_rates, num_he_rates;
4544 
4545 	lockdep_assert_held(&ar->conf_mutex);
4546 
4547 	if (WARN_ON(ath11k_mac_vif_chan(vif, &def)))
4548 		return -EPERM;
4549 
4550 	band = def.chan->band;
4551 	mask = &arvif->bitrate_mask;
4552 
4553 	ath11k_peer_assoc_prepare(ar, vif, sta, &peer_arg, reassoc);
4554 
4555 	peer_arg.is_assoc = true;
4556 	ret = ath11k_wmi_send_peer_assoc_cmd(ar, &peer_arg);
4557 	if (ret) {
4558 		ath11k_warn(ar->ab, "failed to run peer assoc for STA %pM vdev %i: %d\n",
4559 			    sta->addr, arvif->vdev_id, ret);
4560 		return ret;
4561 	}
4562 
4563 	if (!wait_for_completion_timeout(&ar->peer_assoc_done, 1 * HZ)) {
4564 		ath11k_warn(ar->ab, "failed to get peer assoc conf event for %pM vdev %i\n",
4565 			    sta->addr, arvif->vdev_id);
4566 		return -ETIMEDOUT;
4567 	}
4568 
4569 	num_vht_rates = ath11k_mac_bitrate_mask_num_vht_rates(ar, band, mask);
4570 	num_he_rates = ath11k_mac_bitrate_mask_num_he_rates(ar, band, mask);
4571 	num_ht_rates = ath11k_mac_bitrate_mask_num_ht_rates(ar, band, mask);
4572 
4573 	/* If single VHT/HE rate is configured (by set_bitrate_mask()),
4574 	 * peer_assoc will disable VHT/HE. This is now enabled by a peer specific
4575 	 * fixed param.
4576 	 * Note that all other rates and NSS will be disabled for this peer.
4577 	 */
4578 	if (sta->deflink.vht_cap.vht_supported && num_vht_rates == 1) {
4579 		ret = ath11k_mac_set_peer_vht_fixed_rate(arvif, sta, mask,
4580 							 band);
4581 		if (ret)
4582 			return ret;
4583 	} else if (sta->deflink.he_cap.has_he && num_he_rates == 1) {
4584 		ret = ath11k_mac_set_peer_he_fixed_rate(arvif, sta, mask,
4585 							band);
4586 		if (ret)
4587 			return ret;
4588 	} else if (sta->deflink.ht_cap.ht_supported && num_ht_rates == 1) {
4589 		ret = ath11k_mac_set_peer_ht_fixed_rate(arvif, sta, mask,
4590 							band);
4591 		if (ret)
4592 			return ret;
4593 	}
4594 
4595 	/* Re-assoc is run only to update supported rates for given station. It
4596 	 * doesn't make much sense to reconfigure the peer completely.
4597 	 */
4598 	if (reassoc)
4599 		return 0;
4600 
4601 	ret = ath11k_setup_peer_smps(ar, arvif, sta->addr,
4602 				     &sta->deflink.ht_cap,
4603 				     le16_to_cpu(sta->deflink.he_6ghz_capa.capa));
4604 	if (ret) {
4605 		ath11k_warn(ar->ab, "failed to setup peer SMPS for vdev %d: %d\n",
4606 			    arvif->vdev_id, ret);
4607 		return ret;
4608 	}
4609 
4610 	if (!sta->wme) {
4611 		arvif->num_legacy_stations++;
4612 		ret = ath11k_recalc_rtscts_prot(arvif);
4613 		if (ret)
4614 			return ret;
4615 	}
4616 
4617 	if (sta->wme && sta->uapsd_queues) {
4618 		ret = ath11k_peer_assoc_qos_ap(ar, arvif, sta);
4619 		if (ret) {
4620 			ath11k_warn(ar->ab, "failed to set qos params for STA %pM for vdev %i: %d\n",
4621 				    sta->addr, arvif->vdev_id, ret);
4622 			return ret;
4623 		}
4624 	}
4625 
4626 	return 0;
4627 }
4628 
ath11k_station_disassoc(struct ath11k * ar,struct ieee80211_vif * vif,struct ieee80211_sta * sta)4629 static int ath11k_station_disassoc(struct ath11k *ar,
4630 				   struct ieee80211_vif *vif,
4631 				   struct ieee80211_sta *sta)
4632 {
4633 	struct ath11k_vif *arvif = ath11k_vif_to_arvif(vif);
4634 	int ret = 0;
4635 
4636 	lockdep_assert_held(&ar->conf_mutex);
4637 
4638 	if (!sta->wme) {
4639 		arvif->num_legacy_stations--;
4640 		ret = ath11k_recalc_rtscts_prot(arvif);
4641 		if (ret)
4642 			return ret;
4643 	}
4644 
4645 	ret = ath11k_clear_peer_keys(arvif, sta->addr);
4646 	if (ret) {
4647 		ath11k_warn(ar->ab, "failed to clear all peer keys for vdev %i: %d\n",
4648 			    arvif->vdev_id, ret);
4649 		return ret;
4650 	}
4651 	return 0;
4652 }
4653 
ath11k_sta_rc_update_wk(struct work_struct * wk)4654 static void ath11k_sta_rc_update_wk(struct work_struct *wk)
4655 {
4656 	struct ath11k *ar;
4657 	struct ath11k_vif *arvif;
4658 	struct ath11k_sta *arsta;
4659 	struct ieee80211_sta *sta;
4660 	struct cfg80211_chan_def def;
4661 	enum nl80211_band band;
4662 	const u8 *ht_mcs_mask;
4663 	const u16 *vht_mcs_mask;
4664 	const u16 *he_mcs_mask;
4665 	u32 changed, bw, nss, smps, bw_prev;
4666 	int err, num_ht_rates, num_vht_rates, num_he_rates;
4667 	const struct cfg80211_bitrate_mask *mask;
4668 	struct peer_assoc_params peer_arg;
4669 	enum wmi_phy_mode peer_phymode;
4670 
4671 	arsta = container_of(wk, struct ath11k_sta, update_wk);
4672 	sta = container_of((void *)arsta, struct ieee80211_sta, drv_priv);
4673 	arvif = arsta->arvif;
4674 	ar = arvif->ar;
4675 
4676 	if (WARN_ON(ath11k_mac_vif_chan(arvif->vif, &def)))
4677 		return;
4678 
4679 	band = def.chan->band;
4680 	ht_mcs_mask = arvif->bitrate_mask.control[band].ht_mcs;
4681 	vht_mcs_mask = arvif->bitrate_mask.control[band].vht_mcs;
4682 	he_mcs_mask = arvif->bitrate_mask.control[band].he_mcs;
4683 
4684 	spin_lock_bh(&ar->data_lock);
4685 
4686 	changed = arsta->changed;
4687 	arsta->changed = 0;
4688 
4689 	bw = arsta->bw;
4690 	bw_prev = arsta->bw_prev;
4691 	nss = arsta->nss;
4692 	smps = arsta->smps;
4693 
4694 	spin_unlock_bh(&ar->data_lock);
4695 
4696 	mutex_lock(&ar->conf_mutex);
4697 
4698 	nss = max_t(u32, 1, nss);
4699 	nss = min(nss, max(max(ath11k_mac_max_ht_nss(ht_mcs_mask),
4700 			       ath11k_mac_max_vht_nss(vht_mcs_mask)),
4701 			   ath11k_mac_max_he_nss(he_mcs_mask)));
4702 
4703 	if (changed & IEEE80211_RC_BW_CHANGED) {
4704 		/* Get the peer phymode */
4705 		ath11k_peer_assoc_h_phymode(ar, arvif->vif, sta, &peer_arg);
4706 		peer_phymode = peer_arg.peer_phymode;
4707 
4708 		ath11k_dbg(ar->ab, ATH11K_DBG_MAC, "update sta %pM peer bw %d phymode %d\n",
4709 			   sta->addr, bw, peer_phymode);
4710 
4711 		if (bw > bw_prev) {
4712 			/* BW is upgraded. In this case we send WMI_PEER_PHYMODE
4713 			 * followed by WMI_PEER_CHWIDTH
4714 			 */
4715 			ath11k_dbg(ar->ab, ATH11K_DBG_MAC, "BW upgrade for sta %pM new BW %d, old BW %d\n",
4716 				   sta->addr, bw, bw_prev);
4717 
4718 			err = ath11k_wmi_set_peer_param(ar, sta->addr, arvif->vdev_id,
4719 							WMI_PEER_PHYMODE, peer_phymode);
4720 
4721 			if (err) {
4722 				ath11k_warn(ar->ab, "failed to update STA %pM peer phymode %d: %d\n",
4723 					    sta->addr, peer_phymode, err);
4724 				goto err_rc_bw_changed;
4725 			}
4726 
4727 			err = ath11k_wmi_set_peer_param(ar, sta->addr, arvif->vdev_id,
4728 							WMI_PEER_CHWIDTH, bw);
4729 
4730 			if (err)
4731 				ath11k_warn(ar->ab, "failed to update STA %pM peer bw %d: %d\n",
4732 					    sta->addr, bw, err);
4733 		} else {
4734 			/* BW is downgraded. In this case we send WMI_PEER_CHWIDTH
4735 			 * followed by WMI_PEER_PHYMODE
4736 			 */
4737 			ath11k_dbg(ar->ab, ATH11K_DBG_MAC, "BW downgrade for sta %pM new BW %d,old BW %d\n",
4738 				   sta->addr, bw, bw_prev);
4739 
4740 			err = ath11k_wmi_set_peer_param(ar, sta->addr, arvif->vdev_id,
4741 							WMI_PEER_CHWIDTH, bw);
4742 
4743 			if (err) {
4744 				ath11k_warn(ar->ab, "failed to update STA %pM peer bw %d: %d\n",
4745 					    sta->addr, bw, err);
4746 				goto err_rc_bw_changed;
4747 			}
4748 
4749 			err = ath11k_wmi_set_peer_param(ar, sta->addr, arvif->vdev_id,
4750 							WMI_PEER_PHYMODE, peer_phymode);
4751 
4752 			if (err)
4753 				ath11k_warn(ar->ab, "failed to update STA %pM peer phymode %d: %d\n",
4754 					    sta->addr, peer_phymode, err);
4755 		}
4756 	}
4757 
4758 	if (changed & IEEE80211_RC_NSS_CHANGED) {
4759 		ath11k_dbg(ar->ab, ATH11K_DBG_MAC, "update sta %pM nss %d\n",
4760 			   sta->addr, nss);
4761 
4762 		err = ath11k_wmi_set_peer_param(ar, sta->addr, arvif->vdev_id,
4763 						WMI_PEER_NSS, nss);
4764 		if (err)
4765 			ath11k_warn(ar->ab, "failed to update STA %pM nss %d: %d\n",
4766 				    sta->addr, nss, err);
4767 	}
4768 
4769 	if (changed & IEEE80211_RC_SMPS_CHANGED) {
4770 		ath11k_dbg(ar->ab, ATH11K_DBG_MAC, "update sta %pM smps %d\n",
4771 			   sta->addr, smps);
4772 
4773 		err = ath11k_wmi_set_peer_param(ar, sta->addr, arvif->vdev_id,
4774 						WMI_PEER_MIMO_PS_STATE, smps);
4775 		if (err)
4776 			ath11k_warn(ar->ab, "failed to update STA %pM smps %d: %d\n",
4777 				    sta->addr, smps, err);
4778 	}
4779 
4780 	if (changed & IEEE80211_RC_SUPP_RATES_CHANGED) {
4781 		mask = &arvif->bitrate_mask;
4782 		num_ht_rates = ath11k_mac_bitrate_mask_num_ht_rates(ar, band,
4783 								    mask);
4784 		num_vht_rates = ath11k_mac_bitrate_mask_num_vht_rates(ar, band,
4785 								      mask);
4786 		num_he_rates = ath11k_mac_bitrate_mask_num_he_rates(ar, band,
4787 								    mask);
4788 
4789 		/* Peer_assoc_prepare will reject vht rates in
4790 		 * bitrate_mask if its not available in range format and
4791 		 * sets vht tx_rateset as unsupported. So multiple VHT MCS
4792 		 * setting(eg. MCS 4,5,6) per peer is not supported here.
4793 		 * But, Single rate in VHT mask can be set as per-peer
4794 		 * fixed rate. But even if any HT rates are configured in
4795 		 * the bitrate mask, device will not switch to those rates
4796 		 * when per-peer Fixed rate is set.
4797 		 * TODO: Check RATEMASK_CMDID to support auto rates selection
4798 		 * across HT/VHT and for multiple VHT MCS support.
4799 		 */
4800 		if (sta->deflink.vht_cap.vht_supported && num_vht_rates == 1) {
4801 			ath11k_mac_set_peer_vht_fixed_rate(arvif, sta, mask,
4802 							   band);
4803 		} else if (sta->deflink.he_cap.has_he && num_he_rates == 1) {
4804 			ath11k_mac_set_peer_he_fixed_rate(arvif, sta, mask,
4805 							  band);
4806 		} else if (sta->deflink.ht_cap.ht_supported && num_ht_rates == 1) {
4807 			ath11k_mac_set_peer_ht_fixed_rate(arvif, sta, mask,
4808 							  band);
4809 		} else {
4810 			/* If the peer is non-VHT/HE or no fixed VHT/HE rate
4811 			 * is provided in the new bitrate mask we set the
4812 			 * other rates using peer_assoc command. Also clear
4813 			 * the peer fixed rate settings as it has higher proprity
4814 			 * than peer assoc
4815 			 */
4816 			err = ath11k_wmi_set_peer_param(ar, sta->addr,
4817 							arvif->vdev_id,
4818 							WMI_PEER_PARAM_FIXED_RATE,
4819 							WMI_FIXED_RATE_NONE);
4820 			if (err)
4821 				ath11k_warn(ar->ab,
4822 					    "failed to disable peer fixed rate for sta %pM: %d\n",
4823 					    sta->addr, err);
4824 
4825 			ath11k_peer_assoc_prepare(ar, arvif->vif, sta,
4826 						  &peer_arg, true);
4827 
4828 			peer_arg.is_assoc = false;
4829 			err = ath11k_wmi_send_peer_assoc_cmd(ar, &peer_arg);
4830 			if (err)
4831 				ath11k_warn(ar->ab, "failed to run peer assoc for STA %pM vdev %i: %d\n",
4832 					    sta->addr, arvif->vdev_id, err);
4833 
4834 			if (!wait_for_completion_timeout(&ar->peer_assoc_done, 1 * HZ))
4835 				ath11k_warn(ar->ab, "failed to get peer assoc conf event for %pM vdev %i\n",
4836 					    sta->addr, arvif->vdev_id);
4837 		}
4838 	}
4839 
4840 err_rc_bw_changed:
4841 	mutex_unlock(&ar->conf_mutex);
4842 }
4843 
ath11k_sta_set_4addr_wk(struct work_struct * wk)4844 static void ath11k_sta_set_4addr_wk(struct work_struct *wk)
4845 {
4846 	struct ath11k *ar;
4847 	struct ath11k_vif *arvif;
4848 	struct ath11k_sta *arsta;
4849 	struct ieee80211_sta *sta;
4850 	int ret = 0;
4851 
4852 	arsta = container_of(wk, struct ath11k_sta, set_4addr_wk);
4853 	sta = container_of((void *)arsta, struct ieee80211_sta, drv_priv);
4854 	arvif = arsta->arvif;
4855 	ar = arvif->ar;
4856 
4857 	ath11k_dbg(ar->ab, ATH11K_DBG_MAC,
4858 		   "setting USE_4ADDR for peer %pM\n", sta->addr);
4859 
4860 	ret = ath11k_wmi_set_peer_param(ar, sta->addr,
4861 					arvif->vdev_id,
4862 					WMI_PEER_USE_4ADDR, 1);
4863 
4864 	if (ret)
4865 		ath11k_warn(ar->ab, "failed to set peer %pM 4addr capability: %d\n",
4866 			    sta->addr, ret);
4867 }
4868 
ath11k_mac_inc_num_stations(struct ath11k_vif * arvif,struct ieee80211_sta * sta)4869 static int ath11k_mac_inc_num_stations(struct ath11k_vif *arvif,
4870 				       struct ieee80211_sta *sta)
4871 {
4872 	struct ath11k *ar = arvif->ar;
4873 
4874 	lockdep_assert_held(&ar->conf_mutex);
4875 
4876 	if (arvif->vdev_type == WMI_VDEV_TYPE_STA && !sta->tdls)
4877 		return 0;
4878 
4879 	if (ar->num_stations >= ar->max_num_stations)
4880 		return -ENOBUFS;
4881 
4882 	ar->num_stations++;
4883 
4884 	return 0;
4885 }
4886 
ath11k_mac_dec_num_stations(struct ath11k_vif * arvif,struct ieee80211_sta * sta)4887 static void ath11k_mac_dec_num_stations(struct ath11k_vif *arvif,
4888 					struct ieee80211_sta *sta)
4889 {
4890 	struct ath11k *ar = arvif->ar;
4891 
4892 	lockdep_assert_held(&ar->conf_mutex);
4893 
4894 	if (arvif->vdev_type == WMI_VDEV_TYPE_STA && !sta->tdls)
4895 		return;
4896 
4897 	ar->num_stations--;
4898 }
4899 
ath11k_mac_station_add(struct ath11k * ar,struct ieee80211_vif * vif,struct ieee80211_sta * sta)4900 static int ath11k_mac_station_add(struct ath11k *ar,
4901 				  struct ieee80211_vif *vif,
4902 				  struct ieee80211_sta *sta)
4903 {
4904 	struct ath11k_base *ab = ar->ab;
4905 	struct ath11k_vif *arvif = ath11k_vif_to_arvif(vif);
4906 	struct ath11k_sta *arsta = (struct ath11k_sta *)sta->drv_priv;
4907 	struct peer_create_params peer_param;
4908 	int ret;
4909 
4910 	lockdep_assert_held(&ar->conf_mutex);
4911 
4912 	ret = ath11k_mac_inc_num_stations(arvif, sta);
4913 	if (ret) {
4914 		ath11k_warn(ab, "refusing to associate station: too many connected already (%d)\n",
4915 			    ar->max_num_stations);
4916 		goto exit;
4917 	}
4918 
4919 	arsta->rx_stats = kzalloc(sizeof(*arsta->rx_stats), GFP_KERNEL);
4920 	if (!arsta->rx_stats) {
4921 		ret = -ENOMEM;
4922 		goto dec_num_station;
4923 	}
4924 
4925 	peer_param.vdev_id = arvif->vdev_id;
4926 	peer_param.peer_addr = sta->addr;
4927 	peer_param.peer_type = WMI_PEER_TYPE_DEFAULT;
4928 
4929 	ret = ath11k_peer_create(ar, arvif, sta, &peer_param);
4930 	if (ret) {
4931 		ath11k_warn(ab, "Failed to add peer: %pM for VDEV: %d\n",
4932 			    sta->addr, arvif->vdev_id);
4933 		goto free_rx_stats;
4934 	}
4935 
4936 	ath11k_dbg(ab, ATH11K_DBG_MAC, "Added peer: %pM for VDEV: %d\n",
4937 		   sta->addr, arvif->vdev_id);
4938 
4939 	if (ath11k_debugfs_is_extd_tx_stats_enabled(ar)) {
4940 		arsta->tx_stats = kzalloc(sizeof(*arsta->tx_stats), GFP_KERNEL);
4941 		if (!arsta->tx_stats) {
4942 			ret = -ENOMEM;
4943 			goto free_peer;
4944 		}
4945 	}
4946 
4947 	if (ieee80211_vif_is_mesh(vif)) {
4948 		ath11k_dbg(ab, ATH11K_DBG_MAC,
4949 			   "setting USE_4ADDR for mesh STA %pM\n", sta->addr);
4950 		ret = ath11k_wmi_set_peer_param(ar, sta->addr,
4951 						arvif->vdev_id,
4952 						WMI_PEER_USE_4ADDR, 1);
4953 		if (ret) {
4954 			ath11k_warn(ab, "failed to set mesh STA %pM 4addr capability: %d\n",
4955 				    sta->addr, ret);
4956 			goto free_tx_stats;
4957 		}
4958 	}
4959 
4960 	ret = ath11k_dp_peer_setup(ar, arvif->vdev_id, sta->addr);
4961 	if (ret) {
4962 		ath11k_warn(ab, "failed to setup dp for peer %pM on vdev %i (%d)\n",
4963 			    sta->addr, arvif->vdev_id, ret);
4964 		goto free_tx_stats;
4965 	}
4966 
4967 	if (ab->hw_params.vdev_start_delay &&
4968 	    !arvif->is_started &&
4969 	    arvif->vdev_type != WMI_VDEV_TYPE_AP) {
4970 		ret = ath11k_start_vdev_delay(ar->hw, vif);
4971 		if (ret) {
4972 			ath11k_warn(ab, "failed to delay vdev start: %d\n", ret);
4973 			goto free_tx_stats;
4974 		}
4975 	}
4976 
4977 	ewma_avg_rssi_init(&arsta->avg_rssi);
4978 	return 0;
4979 
4980 free_tx_stats:
4981 	kfree(arsta->tx_stats);
4982 	arsta->tx_stats = NULL;
4983 free_peer:
4984 	ath11k_peer_delete(ar, arvif->vdev_id, sta->addr);
4985 free_rx_stats:
4986 	kfree(arsta->rx_stats);
4987 	arsta->rx_stats = NULL;
4988 dec_num_station:
4989 	ath11k_mac_dec_num_stations(arvif, sta);
4990 exit:
4991 	return ret;
4992 }
4993 
ath11k_mac_ieee80211_sta_bw_to_wmi(struct ath11k * ar,struct ieee80211_sta * sta)4994 static u32 ath11k_mac_ieee80211_sta_bw_to_wmi(struct ath11k *ar,
4995 					      struct ieee80211_sta *sta)
4996 {
4997 	u32 bw = WMI_PEER_CHWIDTH_20MHZ;
4998 
4999 	switch (sta->deflink.bandwidth) {
5000 	case IEEE80211_STA_RX_BW_20:
5001 		bw = WMI_PEER_CHWIDTH_20MHZ;
5002 		break;
5003 	case IEEE80211_STA_RX_BW_40:
5004 		bw = WMI_PEER_CHWIDTH_40MHZ;
5005 		break;
5006 	case IEEE80211_STA_RX_BW_80:
5007 		bw = WMI_PEER_CHWIDTH_80MHZ;
5008 		break;
5009 	case IEEE80211_STA_RX_BW_160:
5010 		bw = WMI_PEER_CHWIDTH_160MHZ;
5011 		break;
5012 	default:
5013 		ath11k_warn(ar->ab, "Invalid bandwidth %d for %pM\n",
5014 			    sta->deflink.bandwidth, sta->addr);
5015 		bw = WMI_PEER_CHWIDTH_20MHZ;
5016 		break;
5017 	}
5018 
5019 	return bw;
5020 }
5021 
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)5022 static int ath11k_mac_op_sta_state(struct ieee80211_hw *hw,
5023 				   struct ieee80211_vif *vif,
5024 				   struct ieee80211_sta *sta,
5025 				   enum ieee80211_sta_state old_state,
5026 				   enum ieee80211_sta_state new_state)
5027 {
5028 	struct ath11k *ar = hw->priv;
5029 	struct ath11k_vif *arvif = ath11k_vif_to_arvif(vif);
5030 	struct ath11k_sta *arsta = (struct ath11k_sta *)sta->drv_priv;
5031 	struct ath11k_peer *peer;
5032 	int ret = 0;
5033 
5034 	/* cancel must be done outside the mutex to avoid deadlock */
5035 	if ((old_state == IEEE80211_STA_NONE &&
5036 	     new_state == IEEE80211_STA_NOTEXIST)) {
5037 		cancel_work_sync(&arsta->update_wk);
5038 		cancel_work_sync(&arsta->set_4addr_wk);
5039 	}
5040 
5041 	mutex_lock(&ar->conf_mutex);
5042 
5043 	if (old_state == IEEE80211_STA_NOTEXIST &&
5044 	    new_state == IEEE80211_STA_NONE) {
5045 		memset(arsta, 0, sizeof(*arsta));
5046 		arsta->arvif = arvif;
5047 		arsta->peer_ps_state = WMI_PEER_PS_STATE_DISABLED;
5048 		INIT_WORK(&arsta->update_wk, ath11k_sta_rc_update_wk);
5049 		INIT_WORK(&arsta->set_4addr_wk, ath11k_sta_set_4addr_wk);
5050 
5051 		ret = ath11k_mac_station_add(ar, vif, sta);
5052 		if (ret)
5053 			ath11k_warn(ar->ab, "Failed to add station: %pM for VDEV: %d\n",
5054 				    sta->addr, arvif->vdev_id);
5055 	} else if ((old_state == IEEE80211_STA_NONE &&
5056 		    new_state == IEEE80211_STA_NOTEXIST)) {
5057 		bool skip_peer_delete = ar->ab->hw_params.vdev_start_delay &&
5058 			vif->type == NL80211_IFTYPE_STATION;
5059 
5060 		ath11k_dp_peer_cleanup(ar, arvif->vdev_id, sta->addr);
5061 
5062 		if (!skip_peer_delete) {
5063 			ret = ath11k_peer_delete(ar, arvif->vdev_id, sta->addr);
5064 			if (ret)
5065 				ath11k_warn(ar->ab,
5066 					    "Failed to delete peer: %pM for VDEV: %d\n",
5067 					    sta->addr, arvif->vdev_id);
5068 			else
5069 				ath11k_dbg(ar->ab,
5070 					   ATH11K_DBG_MAC,
5071 					   "Removed peer: %pM for VDEV: %d\n",
5072 					   sta->addr, arvif->vdev_id);
5073 		}
5074 
5075 		ath11k_mac_dec_num_stations(arvif, sta);
5076 		mutex_lock(&ar->ab->tbl_mtx_lock);
5077 		spin_lock_bh(&ar->ab->base_lock);
5078 		peer = ath11k_peer_find(ar->ab, arvif->vdev_id, sta->addr);
5079 		if (skip_peer_delete && peer) {
5080 			peer->sta = NULL;
5081 		} else if (peer && peer->sta == sta) {
5082 			ath11k_warn(ar->ab, "Found peer entry %pM n vdev %i after it was supposedly removed\n",
5083 				    vif->addr, arvif->vdev_id);
5084 			ath11k_peer_rhash_delete(ar->ab, peer);
5085 			peer->sta = NULL;
5086 			list_del(&peer->list);
5087 			kfree(peer);
5088 			ar->num_peers--;
5089 		}
5090 		spin_unlock_bh(&ar->ab->base_lock);
5091 		mutex_unlock(&ar->ab->tbl_mtx_lock);
5092 
5093 		kfree(arsta->tx_stats);
5094 		arsta->tx_stats = NULL;
5095 
5096 		kfree(arsta->rx_stats);
5097 		arsta->rx_stats = NULL;
5098 	} else if (old_state == IEEE80211_STA_AUTH &&
5099 		   new_state == IEEE80211_STA_ASSOC &&
5100 		   (vif->type == NL80211_IFTYPE_AP ||
5101 		    vif->type == NL80211_IFTYPE_MESH_POINT ||
5102 		    vif->type == NL80211_IFTYPE_ADHOC)) {
5103 		ret = ath11k_station_assoc(ar, vif, sta, false);
5104 		if (ret)
5105 			ath11k_warn(ar->ab, "Failed to associate station: %pM\n",
5106 				    sta->addr);
5107 
5108 		spin_lock_bh(&ar->data_lock);
5109 		/* Set arsta bw and prev bw */
5110 		arsta->bw = ath11k_mac_ieee80211_sta_bw_to_wmi(ar, sta);
5111 		arsta->bw_prev = arsta->bw;
5112 		spin_unlock_bh(&ar->data_lock);
5113 	} else if (old_state == IEEE80211_STA_ASSOC &&
5114 		   new_state == IEEE80211_STA_AUTHORIZED) {
5115 		spin_lock_bh(&ar->ab->base_lock);
5116 
5117 		peer = ath11k_peer_find(ar->ab, arvif->vdev_id, sta->addr);
5118 		if (peer)
5119 			peer->is_authorized = true;
5120 
5121 		spin_unlock_bh(&ar->ab->base_lock);
5122 
5123 		if (vif->type == NL80211_IFTYPE_STATION && arvif->is_up) {
5124 			ret = ath11k_wmi_set_peer_param(ar, sta->addr,
5125 							arvif->vdev_id,
5126 							WMI_PEER_AUTHORIZE,
5127 							1);
5128 			if (ret)
5129 				ath11k_warn(ar->ab, "Unable to authorize peer %pM vdev %d: %d\n",
5130 					    sta->addr, arvif->vdev_id, ret);
5131 		}
5132 	} else if (old_state == IEEE80211_STA_AUTHORIZED &&
5133 		   new_state == IEEE80211_STA_ASSOC) {
5134 		spin_lock_bh(&ar->ab->base_lock);
5135 
5136 		peer = ath11k_peer_find(ar->ab, arvif->vdev_id, sta->addr);
5137 		if (peer)
5138 			peer->is_authorized = false;
5139 
5140 		spin_unlock_bh(&ar->ab->base_lock);
5141 	} else if (old_state == IEEE80211_STA_ASSOC &&
5142 		   new_state == IEEE80211_STA_AUTH &&
5143 		   (vif->type == NL80211_IFTYPE_AP ||
5144 		    vif->type == NL80211_IFTYPE_MESH_POINT ||
5145 		    vif->type == NL80211_IFTYPE_ADHOC)) {
5146 		ret = ath11k_station_disassoc(ar, vif, sta);
5147 		if (ret)
5148 			ath11k_warn(ar->ab, "Failed to disassociate station: %pM\n",
5149 				    sta->addr);
5150 	}
5151 
5152 	mutex_unlock(&ar->conf_mutex);
5153 	return ret;
5154 }
5155 
ath11k_mac_op_sta_set_txpwr(struct ieee80211_hw * hw,struct ieee80211_vif * vif,struct ieee80211_sta * sta)5156 static int ath11k_mac_op_sta_set_txpwr(struct ieee80211_hw *hw,
5157 				       struct ieee80211_vif *vif,
5158 				       struct ieee80211_sta *sta)
5159 {
5160 	struct ath11k *ar = hw->priv;
5161 	struct ath11k_vif *arvif = ath11k_vif_to_arvif(vif);
5162 	int ret = 0;
5163 	s16 txpwr;
5164 
5165 	if (sta->deflink.txpwr.type == NL80211_TX_POWER_AUTOMATIC) {
5166 		txpwr = 0;
5167 	} else {
5168 		txpwr = sta->deflink.txpwr.power;
5169 		if (!txpwr)
5170 			return -EINVAL;
5171 	}
5172 
5173 	if (txpwr > ATH11K_TX_POWER_MAX_VAL || txpwr < ATH11K_TX_POWER_MIN_VAL)
5174 		return -EINVAL;
5175 
5176 	mutex_lock(&ar->conf_mutex);
5177 
5178 	ret = ath11k_wmi_set_peer_param(ar, sta->addr, arvif->vdev_id,
5179 					WMI_PEER_USE_FIXED_PWR, txpwr);
5180 	if (ret) {
5181 		ath11k_warn(ar->ab, "failed to set tx power for station ret: %d\n",
5182 			    ret);
5183 		goto out;
5184 	}
5185 
5186 out:
5187 	mutex_unlock(&ar->conf_mutex);
5188 	return ret;
5189 }
5190 
ath11k_mac_op_sta_set_4addr(struct ieee80211_hw * hw,struct ieee80211_vif * vif,struct ieee80211_sta * sta,bool enabled)5191 static void ath11k_mac_op_sta_set_4addr(struct ieee80211_hw *hw,
5192 					struct ieee80211_vif *vif,
5193 					struct ieee80211_sta *sta, bool enabled)
5194 {
5195 	struct ath11k *ar = hw->priv;
5196 	struct ath11k_sta *arsta = (struct ath11k_sta *)sta->drv_priv;
5197 
5198 	if (enabled && !arsta->use_4addr_set) {
5199 		ieee80211_queue_work(ar->hw, &arsta->set_4addr_wk);
5200 		arsta->use_4addr_set = true;
5201 	}
5202 }
5203 
ath11k_mac_op_sta_rc_update(struct ieee80211_hw * hw,struct ieee80211_vif * vif,struct ieee80211_sta * sta,u32 changed)5204 static void ath11k_mac_op_sta_rc_update(struct ieee80211_hw *hw,
5205 					struct ieee80211_vif *vif,
5206 					struct ieee80211_sta *sta,
5207 					u32 changed)
5208 {
5209 	struct ath11k *ar = hw->priv;
5210 	struct ath11k_sta *arsta = (struct ath11k_sta *)sta->drv_priv;
5211 	struct ath11k_vif *arvif = ath11k_vif_to_arvif(vif);
5212 	struct ath11k_peer *peer;
5213 	u32 bw, smps;
5214 
5215 	spin_lock_bh(&ar->ab->base_lock);
5216 
5217 	peer = ath11k_peer_find(ar->ab, arvif->vdev_id, sta->addr);
5218 	if (!peer) {
5219 		spin_unlock_bh(&ar->ab->base_lock);
5220 		ath11k_warn(ar->ab, "mac sta rc update failed to find peer %pM on vdev %i\n",
5221 			    sta->addr, arvif->vdev_id);
5222 		return;
5223 	}
5224 
5225 	spin_unlock_bh(&ar->ab->base_lock);
5226 
5227 	ath11k_dbg(ar->ab, ATH11K_DBG_MAC,
5228 		   "sta rc update for %pM changed %08x bw %d nss %d smps %d\n",
5229 		   sta->addr, changed, sta->deflink.bandwidth,
5230 		   sta->deflink.rx_nss,
5231 		   sta->deflink.smps_mode);
5232 
5233 	spin_lock_bh(&ar->data_lock);
5234 
5235 	if (changed & IEEE80211_RC_BW_CHANGED) {
5236 		bw = ath11k_mac_ieee80211_sta_bw_to_wmi(ar, sta);
5237 		arsta->bw_prev = arsta->bw;
5238 		arsta->bw = bw;
5239 	}
5240 
5241 	if (changed & IEEE80211_RC_NSS_CHANGED)
5242 		arsta->nss = sta->deflink.rx_nss;
5243 
5244 	if (changed & IEEE80211_RC_SMPS_CHANGED) {
5245 		smps = WMI_PEER_SMPS_PS_NONE;
5246 
5247 		switch (sta->deflink.smps_mode) {
5248 		case IEEE80211_SMPS_AUTOMATIC:
5249 		case IEEE80211_SMPS_OFF:
5250 			smps = WMI_PEER_SMPS_PS_NONE;
5251 			break;
5252 		case IEEE80211_SMPS_STATIC:
5253 			smps = WMI_PEER_SMPS_STATIC;
5254 			break;
5255 		case IEEE80211_SMPS_DYNAMIC:
5256 			smps = WMI_PEER_SMPS_DYNAMIC;
5257 			break;
5258 		default:
5259 			ath11k_warn(ar->ab, "Invalid smps %d in sta rc update for %pM\n",
5260 				    sta->deflink.smps_mode, sta->addr);
5261 			smps = WMI_PEER_SMPS_PS_NONE;
5262 			break;
5263 		}
5264 
5265 		arsta->smps = smps;
5266 	}
5267 
5268 	arsta->changed |= changed;
5269 
5270 	spin_unlock_bh(&ar->data_lock);
5271 
5272 	ieee80211_queue_work(hw, &arsta->update_wk);
5273 }
5274 
ath11k_conf_tx_uapsd(struct ath11k * ar,struct ieee80211_vif * vif,u16 ac,bool enable)5275 static int ath11k_conf_tx_uapsd(struct ath11k *ar, struct ieee80211_vif *vif,
5276 				u16 ac, bool enable)
5277 {
5278 	struct ath11k_vif *arvif = ath11k_vif_to_arvif(vif);
5279 	u32 value = 0;
5280 	int ret = 0;
5281 
5282 	if (arvif->vdev_type != WMI_VDEV_TYPE_STA)
5283 		return 0;
5284 
5285 	switch (ac) {
5286 	case IEEE80211_AC_VO:
5287 		value = WMI_STA_PS_UAPSD_AC3_DELIVERY_EN |
5288 			WMI_STA_PS_UAPSD_AC3_TRIGGER_EN;
5289 		break;
5290 	case IEEE80211_AC_VI:
5291 		value = WMI_STA_PS_UAPSD_AC2_DELIVERY_EN |
5292 			WMI_STA_PS_UAPSD_AC2_TRIGGER_EN;
5293 		break;
5294 	case IEEE80211_AC_BE:
5295 		value = WMI_STA_PS_UAPSD_AC1_DELIVERY_EN |
5296 			WMI_STA_PS_UAPSD_AC1_TRIGGER_EN;
5297 		break;
5298 	case IEEE80211_AC_BK:
5299 		value = WMI_STA_PS_UAPSD_AC0_DELIVERY_EN |
5300 			WMI_STA_PS_UAPSD_AC0_TRIGGER_EN;
5301 		break;
5302 	}
5303 
5304 	if (enable)
5305 		arvif->u.sta.uapsd |= value;
5306 	else
5307 		arvif->u.sta.uapsd &= ~value;
5308 
5309 	ret = ath11k_wmi_set_sta_ps_param(ar, arvif->vdev_id,
5310 					  WMI_STA_PS_PARAM_UAPSD,
5311 					  arvif->u.sta.uapsd);
5312 	if (ret) {
5313 		ath11k_warn(ar->ab, "could not set uapsd params %d\n", ret);
5314 		goto exit;
5315 	}
5316 
5317 	if (arvif->u.sta.uapsd)
5318 		value = WMI_STA_PS_RX_WAKE_POLICY_POLL_UAPSD;
5319 	else
5320 		value = WMI_STA_PS_RX_WAKE_POLICY_WAKE;
5321 
5322 	ret = ath11k_wmi_set_sta_ps_param(ar, arvif->vdev_id,
5323 					  WMI_STA_PS_PARAM_RX_WAKE_POLICY,
5324 					  value);
5325 	if (ret)
5326 		ath11k_warn(ar->ab, "could not set rx wake param %d\n", ret);
5327 
5328 exit:
5329 	return ret;
5330 }
5331 
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)5332 static int ath11k_mac_op_conf_tx(struct ieee80211_hw *hw,
5333 				 struct ieee80211_vif *vif,
5334 				 unsigned int link_id, u16 ac,
5335 				 const struct ieee80211_tx_queue_params *params)
5336 {
5337 	struct ath11k *ar = hw->priv;
5338 	struct ath11k_vif *arvif = ath11k_vif_to_arvif(vif);
5339 	struct wmi_wmm_params_arg *p = NULL;
5340 	int ret;
5341 
5342 	mutex_lock(&ar->conf_mutex);
5343 
5344 	switch (ac) {
5345 	case IEEE80211_AC_VO:
5346 		p = &arvif->wmm_params.ac_vo;
5347 		break;
5348 	case IEEE80211_AC_VI:
5349 		p = &arvif->wmm_params.ac_vi;
5350 		break;
5351 	case IEEE80211_AC_BE:
5352 		p = &arvif->wmm_params.ac_be;
5353 		break;
5354 	case IEEE80211_AC_BK:
5355 		p = &arvif->wmm_params.ac_bk;
5356 		break;
5357 	}
5358 
5359 	if (WARN_ON(!p)) {
5360 		ret = -EINVAL;
5361 		goto exit;
5362 	}
5363 
5364 	p->cwmin = params->cw_min;
5365 	p->cwmax = params->cw_max;
5366 	p->aifs = params->aifs;
5367 	p->txop = params->txop;
5368 
5369 	ret = ath11k_wmi_send_wmm_update_cmd_tlv(ar, arvif->vdev_id,
5370 						 &arvif->wmm_params);
5371 	if (ret) {
5372 		ath11k_warn(ar->ab, "failed to set wmm params: %d\n", ret);
5373 		goto exit;
5374 	}
5375 
5376 	ret = ath11k_conf_tx_uapsd(ar, vif, ac, params->uapsd);
5377 
5378 	if (ret)
5379 		ath11k_warn(ar->ab, "failed to set sta uapsd: %d\n", ret);
5380 
5381 exit:
5382 	mutex_unlock(&ar->conf_mutex);
5383 	return ret;
5384 }
5385 
5386 static struct ieee80211_sta_ht_cap
ath11k_create_ht_cap(struct ath11k * ar,u32 ar_ht_cap,u32 rate_cap_rx_chainmask)5387 ath11k_create_ht_cap(struct ath11k *ar, u32 ar_ht_cap, u32 rate_cap_rx_chainmask)
5388 {
5389 	int i;
5390 	struct ieee80211_sta_ht_cap ht_cap = {0};
5391 	u32 ar_vht_cap = ar->pdev->cap.vht_cap;
5392 
5393 	if (!(ar_ht_cap & WMI_HT_CAP_ENABLED))
5394 		return ht_cap;
5395 
5396 	ht_cap.ht_supported = 1;
5397 	ht_cap.ampdu_factor = IEEE80211_HT_MAX_AMPDU_64K;
5398 	ht_cap.ampdu_density = IEEE80211_HT_MPDU_DENSITY_NONE;
5399 	ht_cap.cap |= IEEE80211_HT_CAP_SUP_WIDTH_20_40;
5400 	ht_cap.cap |= IEEE80211_HT_CAP_DSSSCCK40;
5401 	ht_cap.cap |= WLAN_HT_CAP_SM_PS_STATIC << IEEE80211_HT_CAP_SM_PS_SHIFT;
5402 
5403 	if (ar_ht_cap & WMI_HT_CAP_HT20_SGI)
5404 		ht_cap.cap |= IEEE80211_HT_CAP_SGI_20;
5405 
5406 	if (ar_ht_cap & WMI_HT_CAP_HT40_SGI)
5407 		ht_cap.cap |= IEEE80211_HT_CAP_SGI_40;
5408 
5409 	if (ar_ht_cap & WMI_HT_CAP_DYNAMIC_SMPS) {
5410 		u32 smps;
5411 
5412 		smps   = WLAN_HT_CAP_SM_PS_DYNAMIC;
5413 		smps <<= IEEE80211_HT_CAP_SM_PS_SHIFT;
5414 
5415 		ht_cap.cap |= smps;
5416 	}
5417 
5418 	if (ar_ht_cap & WMI_HT_CAP_TX_STBC)
5419 		ht_cap.cap |= IEEE80211_HT_CAP_TX_STBC;
5420 
5421 	if (ar_ht_cap & WMI_HT_CAP_RX_STBC) {
5422 		u32 stbc;
5423 
5424 		stbc   = ar_ht_cap;
5425 		stbc  &= WMI_HT_CAP_RX_STBC;
5426 		stbc >>= WMI_HT_CAP_RX_STBC_MASK_SHIFT;
5427 		stbc <<= IEEE80211_HT_CAP_RX_STBC_SHIFT;
5428 		stbc  &= IEEE80211_HT_CAP_RX_STBC;
5429 
5430 		ht_cap.cap |= stbc;
5431 	}
5432 
5433 	if (ar_ht_cap & WMI_HT_CAP_RX_LDPC)
5434 		ht_cap.cap |= IEEE80211_HT_CAP_LDPC_CODING;
5435 
5436 	if (ar_ht_cap & WMI_HT_CAP_L_SIG_TXOP_PROT)
5437 		ht_cap.cap |= IEEE80211_HT_CAP_LSIG_TXOP_PROT;
5438 
5439 	if (ar_vht_cap & WMI_VHT_CAP_MAX_MPDU_LEN_MASK)
5440 		ht_cap.cap |= IEEE80211_HT_CAP_MAX_AMSDU;
5441 
5442 	for (i = 0; i < ar->num_rx_chains; i++) {
5443 		if (rate_cap_rx_chainmask & BIT(i))
5444 			ht_cap.mcs.rx_mask[i] = 0xFF;
5445 	}
5446 
5447 	ht_cap.mcs.tx_params |= IEEE80211_HT_MCS_TX_DEFINED;
5448 
5449 	return ht_cap;
5450 }
5451 
ath11k_mac_set_txbf_conf(struct ath11k_vif * arvif)5452 static int ath11k_mac_set_txbf_conf(struct ath11k_vif *arvif)
5453 {
5454 	u32 value = 0;
5455 	struct ath11k *ar = arvif->ar;
5456 	int nsts;
5457 	int sound_dim;
5458 	u32 vht_cap = ar->pdev->cap.vht_cap;
5459 	u32 vdev_param = WMI_VDEV_PARAM_TXBF;
5460 
5461 	if (vht_cap & (IEEE80211_VHT_CAP_SU_BEAMFORMEE_CAPABLE)) {
5462 		nsts = vht_cap & IEEE80211_VHT_CAP_BEAMFORMEE_STS_MASK;
5463 		nsts >>= IEEE80211_VHT_CAP_BEAMFORMEE_STS_SHIFT;
5464 		if (nsts > (ar->num_rx_chains - 1))
5465 			nsts = ar->num_rx_chains - 1;
5466 		value |= SM(nsts, WMI_TXBF_STS_CAP_OFFSET);
5467 	}
5468 
5469 	if (vht_cap & (IEEE80211_VHT_CAP_SU_BEAMFORMER_CAPABLE)) {
5470 		sound_dim = vht_cap &
5471 			    IEEE80211_VHT_CAP_SOUNDING_DIMENSIONS_MASK;
5472 		sound_dim >>= IEEE80211_VHT_CAP_SOUNDING_DIMENSIONS_SHIFT;
5473 		if (sound_dim > (ar->num_tx_chains - 1))
5474 			sound_dim = ar->num_tx_chains - 1;
5475 		value |= SM(sound_dim, WMI_BF_SOUND_DIM_OFFSET);
5476 	}
5477 
5478 	if (!value)
5479 		return 0;
5480 
5481 	if (vht_cap & IEEE80211_VHT_CAP_SU_BEAMFORMER_CAPABLE) {
5482 		value |= WMI_VDEV_PARAM_TXBF_SU_TX_BFER;
5483 
5484 		if ((vht_cap & IEEE80211_VHT_CAP_MU_BEAMFORMER_CAPABLE) &&
5485 		    arvif->vdev_type == WMI_VDEV_TYPE_AP)
5486 			value |= WMI_VDEV_PARAM_TXBF_MU_TX_BFER;
5487 	}
5488 
5489 	/* TODO: SUBFEE not validated in HK, disable here until validated? */
5490 
5491 	if (vht_cap & IEEE80211_VHT_CAP_SU_BEAMFORMEE_CAPABLE) {
5492 		value |= WMI_VDEV_PARAM_TXBF_SU_TX_BFEE;
5493 
5494 		if ((vht_cap & IEEE80211_VHT_CAP_MU_BEAMFORMEE_CAPABLE) &&
5495 		    arvif->vdev_type == WMI_VDEV_TYPE_STA)
5496 			value |= WMI_VDEV_PARAM_TXBF_MU_TX_BFEE;
5497 	}
5498 
5499 	return ath11k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
5500 					     vdev_param, value);
5501 }
5502 
ath11k_set_vht_txbf_cap(struct ath11k * ar,u32 * vht_cap)5503 static void ath11k_set_vht_txbf_cap(struct ath11k *ar, u32 *vht_cap)
5504 {
5505 	bool subfer, subfee;
5506 	int sound_dim = 0, nsts = 0;
5507 
5508 	subfer = !!(*vht_cap & (IEEE80211_VHT_CAP_SU_BEAMFORMER_CAPABLE));
5509 	subfee = !!(*vht_cap & (IEEE80211_VHT_CAP_SU_BEAMFORMEE_CAPABLE));
5510 
5511 	if (ar->num_tx_chains < 2) {
5512 		*vht_cap &= ~(IEEE80211_VHT_CAP_SU_BEAMFORMER_CAPABLE);
5513 		subfer = false;
5514 	}
5515 
5516 	if (ar->num_rx_chains < 2) {
5517 		*vht_cap &= ~(IEEE80211_VHT_CAP_SU_BEAMFORMEE_CAPABLE);
5518 		subfee = false;
5519 	}
5520 
5521 	/* If SU Beaformer is not set, then disable MU Beamformer Capability */
5522 	if (!subfer)
5523 		*vht_cap &= ~(IEEE80211_VHT_CAP_MU_BEAMFORMER_CAPABLE);
5524 
5525 	/* If SU Beaformee is not set, then disable MU Beamformee Capability */
5526 	if (!subfee)
5527 		*vht_cap &= ~(IEEE80211_VHT_CAP_MU_BEAMFORMEE_CAPABLE);
5528 
5529 	sound_dim = (*vht_cap & IEEE80211_VHT_CAP_SOUNDING_DIMENSIONS_MASK);
5530 	sound_dim >>= IEEE80211_VHT_CAP_SOUNDING_DIMENSIONS_SHIFT;
5531 	*vht_cap &= ~IEEE80211_VHT_CAP_SOUNDING_DIMENSIONS_MASK;
5532 
5533 	nsts = (*vht_cap & IEEE80211_VHT_CAP_BEAMFORMEE_STS_MASK);
5534 	nsts >>= IEEE80211_VHT_CAP_BEAMFORMEE_STS_SHIFT;
5535 	*vht_cap &= ~IEEE80211_VHT_CAP_BEAMFORMEE_STS_MASK;
5536 
5537 	/* Enable Sounding Dimension Field only if SU BF is enabled */
5538 	if (subfer) {
5539 		if (sound_dim > (ar->num_tx_chains - 1))
5540 			sound_dim = ar->num_tx_chains - 1;
5541 
5542 		sound_dim <<= IEEE80211_VHT_CAP_SOUNDING_DIMENSIONS_SHIFT;
5543 		sound_dim &=  IEEE80211_VHT_CAP_SOUNDING_DIMENSIONS_MASK;
5544 		*vht_cap |= sound_dim;
5545 	}
5546 
5547 	/* Enable Beamformee STS Field only if SU BF is enabled */
5548 	if (subfee) {
5549 		if (nsts > (ar->num_rx_chains - 1))
5550 			nsts = ar->num_rx_chains - 1;
5551 
5552 		nsts <<= IEEE80211_VHT_CAP_BEAMFORMEE_STS_SHIFT;
5553 		nsts &=  IEEE80211_VHT_CAP_BEAMFORMEE_STS_MASK;
5554 		*vht_cap |= nsts;
5555 	}
5556 }
5557 
5558 static struct ieee80211_sta_vht_cap
ath11k_create_vht_cap(struct ath11k * ar,u32 rate_cap_tx_chainmask,u32 rate_cap_rx_chainmask)5559 ath11k_create_vht_cap(struct ath11k *ar, u32 rate_cap_tx_chainmask,
5560 		      u32 rate_cap_rx_chainmask)
5561 {
5562 	struct ieee80211_sta_vht_cap vht_cap = {0};
5563 	u16 txmcs_map, rxmcs_map;
5564 	int i;
5565 
5566 	vht_cap.vht_supported = 1;
5567 	vht_cap.cap = ar->pdev->cap.vht_cap;
5568 
5569 	if (ar->pdev->cap.nss_ratio_enabled)
5570 		vht_cap.vht_mcs.tx_highest |=
5571 			cpu_to_le16(IEEE80211_VHT_EXT_NSS_BW_CAPABLE);
5572 
5573 	ath11k_set_vht_txbf_cap(ar, &vht_cap.cap);
5574 
5575 	rxmcs_map = 0;
5576 	txmcs_map = 0;
5577 	for (i = 0; i < 8; i++) {
5578 		if (i < ar->num_tx_chains && rate_cap_tx_chainmask & BIT(i))
5579 			txmcs_map |= IEEE80211_VHT_MCS_SUPPORT_0_9 << (i * 2);
5580 		else
5581 			txmcs_map |= IEEE80211_VHT_MCS_NOT_SUPPORTED << (i * 2);
5582 
5583 		if (i < ar->num_rx_chains && rate_cap_rx_chainmask & BIT(i))
5584 			rxmcs_map |= IEEE80211_VHT_MCS_SUPPORT_0_9 << (i * 2);
5585 		else
5586 			rxmcs_map |= IEEE80211_VHT_MCS_NOT_SUPPORTED << (i * 2);
5587 	}
5588 
5589 	if (rate_cap_tx_chainmask <= 1)
5590 		vht_cap.cap &= ~IEEE80211_VHT_CAP_TXSTBC;
5591 
5592 	vht_cap.vht_mcs.rx_mcs_map = cpu_to_le16(rxmcs_map);
5593 	vht_cap.vht_mcs.tx_mcs_map = cpu_to_le16(txmcs_map);
5594 
5595 	return vht_cap;
5596 }
5597 
ath11k_mac_setup_ht_vht_cap(struct ath11k * ar,struct ath11k_pdev_cap * cap,u32 * ht_cap_info)5598 static void ath11k_mac_setup_ht_vht_cap(struct ath11k *ar,
5599 					struct ath11k_pdev_cap *cap,
5600 					u32 *ht_cap_info)
5601 {
5602 	struct ieee80211_supported_band *band;
5603 	u32 rate_cap_tx_chainmask;
5604 	u32 rate_cap_rx_chainmask;
5605 	u32 ht_cap;
5606 
5607 	rate_cap_tx_chainmask = ar->cfg_tx_chainmask >> cap->tx_chain_mask_shift;
5608 	rate_cap_rx_chainmask = ar->cfg_rx_chainmask >> cap->rx_chain_mask_shift;
5609 
5610 	if (cap->supported_bands & WMI_HOST_WLAN_2G_CAP) {
5611 		band = &ar->mac.sbands[NL80211_BAND_2GHZ];
5612 		ht_cap = cap->band[NL80211_BAND_2GHZ].ht_cap_info;
5613 		if (ht_cap_info)
5614 			*ht_cap_info = ht_cap;
5615 		band->ht_cap = ath11k_create_ht_cap(ar, ht_cap,
5616 						    rate_cap_rx_chainmask);
5617 	}
5618 
5619 	if (cap->supported_bands & WMI_HOST_WLAN_5G_CAP &&
5620 	    (ar->ab->hw_params.single_pdev_only ||
5621 	     !ar->supports_6ghz)) {
5622 		band = &ar->mac.sbands[NL80211_BAND_5GHZ];
5623 		ht_cap = cap->band[NL80211_BAND_5GHZ].ht_cap_info;
5624 		if (ht_cap_info)
5625 			*ht_cap_info = ht_cap;
5626 		band->ht_cap = ath11k_create_ht_cap(ar, ht_cap,
5627 						    rate_cap_rx_chainmask);
5628 		band->vht_cap = ath11k_create_vht_cap(ar, rate_cap_tx_chainmask,
5629 						      rate_cap_rx_chainmask);
5630 	}
5631 }
5632 
ath11k_check_chain_mask(struct ath11k * ar,u32 ant,bool is_tx_ant)5633 static int ath11k_check_chain_mask(struct ath11k *ar, u32 ant, bool is_tx_ant)
5634 {
5635 	/* TODO: Check the request chainmask against the supported
5636 	 * chainmask table which is advertised in extented_service_ready event
5637 	 */
5638 
5639 	return 0;
5640 }
5641 
ath11k_gen_ppe_thresh(struct ath11k_ppe_threshold * fw_ppet,u8 * he_ppet)5642 static void ath11k_gen_ppe_thresh(struct ath11k_ppe_threshold *fw_ppet,
5643 				  u8 *he_ppet)
5644 {
5645 	int nss, ru;
5646 	u8 bit = 7;
5647 
5648 	he_ppet[0] = fw_ppet->numss_m1 & IEEE80211_PPE_THRES_NSS_MASK;
5649 	he_ppet[0] |= (fw_ppet->ru_bit_mask <<
5650 		       IEEE80211_PPE_THRES_RU_INDEX_BITMASK_POS) &
5651 		      IEEE80211_PPE_THRES_RU_INDEX_BITMASK_MASK;
5652 	for (nss = 0; nss <= fw_ppet->numss_m1; nss++) {
5653 		for (ru = 0; ru < 4; ru++) {
5654 			u8 val;
5655 			int i;
5656 
5657 			if ((fw_ppet->ru_bit_mask & BIT(ru)) == 0)
5658 				continue;
5659 			val = (fw_ppet->ppet16_ppet8_ru3_ru0[nss] >> (ru * 6)) &
5660 			       0x3f;
5661 			val = ((val >> 3) & 0x7) | ((val & 0x7) << 3);
5662 			for (i = 5; i >= 0; i--) {
5663 				he_ppet[bit / 8] |=
5664 					((val >> i) & 0x1) << ((bit % 8));
5665 				bit++;
5666 			}
5667 		}
5668 	}
5669 }
5670 
5671 static void
ath11k_mac_filter_he_cap_mesh(struct ieee80211_he_cap_elem * he_cap_elem)5672 ath11k_mac_filter_he_cap_mesh(struct ieee80211_he_cap_elem *he_cap_elem)
5673 {
5674 	u8 m;
5675 
5676 	m = IEEE80211_HE_MAC_CAP0_TWT_RES |
5677 	    IEEE80211_HE_MAC_CAP0_TWT_REQ;
5678 	he_cap_elem->mac_cap_info[0] &= ~m;
5679 
5680 	m = IEEE80211_HE_MAC_CAP2_TRS |
5681 	    IEEE80211_HE_MAC_CAP2_BCAST_TWT |
5682 	    IEEE80211_HE_MAC_CAP2_MU_CASCADING;
5683 	he_cap_elem->mac_cap_info[2] &= ~m;
5684 
5685 	m = IEEE80211_HE_MAC_CAP3_FLEX_TWT_SCHED |
5686 	    IEEE80211_HE_MAC_CAP2_BCAST_TWT |
5687 	    IEEE80211_HE_MAC_CAP2_MU_CASCADING;
5688 	he_cap_elem->mac_cap_info[3] &= ~m;
5689 
5690 	m = IEEE80211_HE_MAC_CAP4_BSRP_BQRP_A_MPDU_AGG |
5691 	    IEEE80211_HE_MAC_CAP4_BQR;
5692 	he_cap_elem->mac_cap_info[4] &= ~m;
5693 
5694 	m = IEEE80211_HE_MAC_CAP5_SUBCHAN_SELECTIVE_TRANSMISSION |
5695 	    IEEE80211_HE_MAC_CAP5_UL_2x996_TONE_RU |
5696 	    IEEE80211_HE_MAC_CAP5_PUNCTURED_SOUNDING |
5697 	    IEEE80211_HE_MAC_CAP5_HT_VHT_TRIG_FRAME_RX;
5698 	he_cap_elem->mac_cap_info[5] &= ~m;
5699 
5700 	m = IEEE80211_HE_PHY_CAP2_UL_MU_FULL_MU_MIMO |
5701 	    IEEE80211_HE_PHY_CAP2_UL_MU_PARTIAL_MU_MIMO;
5702 	he_cap_elem->phy_cap_info[2] &= ~m;
5703 
5704 	m = IEEE80211_HE_PHY_CAP3_RX_PARTIAL_BW_SU_IN_20MHZ_MU |
5705 	    IEEE80211_HE_PHY_CAP3_DCM_MAX_CONST_TX_MASK |
5706 	    IEEE80211_HE_PHY_CAP3_DCM_MAX_CONST_RX_MASK;
5707 	he_cap_elem->phy_cap_info[3] &= ~m;
5708 
5709 	m = IEEE80211_HE_PHY_CAP4_MU_BEAMFORMER;
5710 	he_cap_elem->phy_cap_info[4] &= ~m;
5711 
5712 	m = IEEE80211_HE_PHY_CAP5_NG16_MU_FEEDBACK;
5713 	he_cap_elem->phy_cap_info[5] &= ~m;
5714 
5715 	m = IEEE80211_HE_PHY_CAP6_CODEBOOK_SIZE_75_MU |
5716 	    IEEE80211_HE_PHY_CAP6_TRIG_MU_BEAMFORMING_PARTIAL_BW_FB |
5717 	    IEEE80211_HE_PHY_CAP6_TRIG_CQI_FB |
5718 	    IEEE80211_HE_PHY_CAP6_PARTIAL_BANDWIDTH_DL_MUMIMO;
5719 	he_cap_elem->phy_cap_info[6] &= ~m;
5720 
5721 	m = IEEE80211_HE_PHY_CAP7_PSR_BASED_SR |
5722 	    IEEE80211_HE_PHY_CAP7_POWER_BOOST_FACTOR_SUPP |
5723 	    IEEE80211_HE_PHY_CAP7_STBC_TX_ABOVE_80MHZ |
5724 	    IEEE80211_HE_PHY_CAP7_STBC_RX_ABOVE_80MHZ;
5725 	he_cap_elem->phy_cap_info[7] &= ~m;
5726 
5727 	m = IEEE80211_HE_PHY_CAP8_HE_ER_SU_PPDU_4XLTF_AND_08_US_GI |
5728 	    IEEE80211_HE_PHY_CAP8_20MHZ_IN_40MHZ_HE_PPDU_IN_2G |
5729 	    IEEE80211_HE_PHY_CAP8_20MHZ_IN_160MHZ_HE_PPDU |
5730 	    IEEE80211_HE_PHY_CAP8_80MHZ_IN_160MHZ_HE_PPDU;
5731 	he_cap_elem->phy_cap_info[8] &= ~m;
5732 
5733 	m = IEEE80211_HE_PHY_CAP9_LONGER_THAN_16_SIGB_OFDM_SYM |
5734 	    IEEE80211_HE_PHY_CAP9_NON_TRIGGERED_CQI_FEEDBACK |
5735 	    IEEE80211_HE_PHY_CAP9_RX_1024_QAM_LESS_THAN_242_TONE_RU |
5736 	    IEEE80211_HE_PHY_CAP9_TX_1024_QAM_LESS_THAN_242_TONE_RU |
5737 	    IEEE80211_HE_PHY_CAP9_RX_FULL_BW_SU_USING_MU_WITH_COMP_SIGB |
5738 	    IEEE80211_HE_PHY_CAP9_RX_FULL_BW_SU_USING_MU_WITH_NON_COMP_SIGB;
5739 	he_cap_elem->phy_cap_info[9] &= ~m;
5740 }
5741 
ath11k_mac_setup_he_6ghz_cap(struct ath11k_pdev_cap * pcap,struct ath11k_band_cap * bcap)5742 static __le16 ath11k_mac_setup_he_6ghz_cap(struct ath11k_pdev_cap *pcap,
5743 					   struct ath11k_band_cap *bcap)
5744 {
5745 	u8 val;
5746 
5747 	bcap->he_6ghz_capa = IEEE80211_HT_MPDU_DENSITY_NONE;
5748 	if (bcap->ht_cap_info & WMI_HT_CAP_DYNAMIC_SMPS)
5749 		bcap->he_6ghz_capa |=
5750 			FIELD_PREP(IEEE80211_HE_6GHZ_CAP_SM_PS,
5751 				   WLAN_HT_CAP_SM_PS_DYNAMIC);
5752 	else
5753 		bcap->he_6ghz_capa |=
5754 			FIELD_PREP(IEEE80211_HE_6GHZ_CAP_SM_PS,
5755 				   WLAN_HT_CAP_SM_PS_DISABLED);
5756 	val = FIELD_GET(IEEE80211_VHT_CAP_MAX_A_MPDU_LENGTH_EXPONENT_MASK,
5757 			pcap->vht_cap);
5758 	bcap->he_6ghz_capa |=
5759 		FIELD_PREP(IEEE80211_HE_6GHZ_CAP_MAX_AMPDU_LEN_EXP, val);
5760 	val = FIELD_GET(IEEE80211_VHT_CAP_MAX_MPDU_MASK, pcap->vht_cap);
5761 	bcap->he_6ghz_capa |=
5762 		FIELD_PREP(IEEE80211_HE_6GHZ_CAP_MAX_MPDU_LEN, val);
5763 	if (pcap->vht_cap & IEEE80211_VHT_CAP_RX_ANTENNA_PATTERN)
5764 		bcap->he_6ghz_capa |= IEEE80211_HE_6GHZ_CAP_RX_ANTPAT_CONS;
5765 	if (pcap->vht_cap & IEEE80211_VHT_CAP_TX_ANTENNA_PATTERN)
5766 		bcap->he_6ghz_capa |= IEEE80211_HE_6GHZ_CAP_TX_ANTPAT_CONS;
5767 
5768 	return cpu_to_le16(bcap->he_6ghz_capa);
5769 }
5770 
ath11k_mac_set_hemcsmap(struct ath11k * ar,struct ath11k_pdev_cap * cap,struct ieee80211_sta_he_cap * he_cap,int band)5771 static void ath11k_mac_set_hemcsmap(struct ath11k *ar,
5772 				    struct ath11k_pdev_cap *cap,
5773 				    struct ieee80211_sta_he_cap *he_cap,
5774 				    int band)
5775 {
5776 	u16 txmcs_map, rxmcs_map;
5777 	u32 i;
5778 
5779 	rxmcs_map = 0;
5780 	txmcs_map = 0;
5781 	for (i = 0; i < 8; i++) {
5782 		if (i < ar->num_tx_chains &&
5783 		    (ar->cfg_tx_chainmask >> cap->tx_chain_mask_shift) & BIT(i))
5784 			txmcs_map |= IEEE80211_HE_MCS_SUPPORT_0_11 << (i * 2);
5785 		else
5786 			txmcs_map |= IEEE80211_HE_MCS_NOT_SUPPORTED << (i * 2);
5787 
5788 		if (i < ar->num_rx_chains &&
5789 		    (ar->cfg_rx_chainmask >> cap->tx_chain_mask_shift) & BIT(i))
5790 			rxmcs_map |= IEEE80211_HE_MCS_SUPPORT_0_11 << (i * 2);
5791 		else
5792 			rxmcs_map |= IEEE80211_HE_MCS_NOT_SUPPORTED << (i * 2);
5793 	}
5794 	he_cap->he_mcs_nss_supp.rx_mcs_80 =
5795 		cpu_to_le16(rxmcs_map & 0xffff);
5796 	he_cap->he_mcs_nss_supp.tx_mcs_80 =
5797 		cpu_to_le16(txmcs_map & 0xffff);
5798 	he_cap->he_mcs_nss_supp.rx_mcs_160 =
5799 		cpu_to_le16(rxmcs_map & 0xffff);
5800 	he_cap->he_mcs_nss_supp.tx_mcs_160 =
5801 		cpu_to_le16(txmcs_map & 0xffff);
5802 	he_cap->he_mcs_nss_supp.rx_mcs_80p80 =
5803 		cpu_to_le16(rxmcs_map & 0xffff);
5804 	he_cap->he_mcs_nss_supp.tx_mcs_80p80 =
5805 		cpu_to_le16(txmcs_map & 0xffff);
5806 }
5807 
ath11k_mac_copy_he_cap(struct ath11k * ar,struct ath11k_pdev_cap * cap,struct ieee80211_sband_iftype_data * data,int band)5808 static int ath11k_mac_copy_he_cap(struct ath11k *ar,
5809 				  struct ath11k_pdev_cap *cap,
5810 				  struct ieee80211_sband_iftype_data *data,
5811 				  int band)
5812 {
5813 	int i, idx = 0;
5814 
5815 	for (i = 0; i < NUM_NL80211_IFTYPES; i++) {
5816 		struct ieee80211_sta_he_cap *he_cap = &data[idx].he_cap;
5817 		struct ath11k_band_cap *band_cap = &cap->band[band];
5818 		struct ieee80211_he_cap_elem *he_cap_elem =
5819 				&he_cap->he_cap_elem;
5820 
5821 		switch (i) {
5822 		case NL80211_IFTYPE_STATION:
5823 		case NL80211_IFTYPE_AP:
5824 		case NL80211_IFTYPE_MESH_POINT:
5825 			break;
5826 
5827 		default:
5828 			continue;
5829 		}
5830 
5831 		data[idx].types_mask = BIT(i);
5832 		he_cap->has_he = true;
5833 		memcpy(he_cap_elem->mac_cap_info, band_cap->he_cap_info,
5834 		       sizeof(he_cap_elem->mac_cap_info));
5835 		memcpy(he_cap_elem->phy_cap_info, band_cap->he_cap_phy_info,
5836 		       sizeof(he_cap_elem->phy_cap_info));
5837 
5838 		he_cap_elem->mac_cap_info[1] &=
5839 			IEEE80211_HE_MAC_CAP1_TF_MAC_PAD_DUR_MASK;
5840 
5841 		he_cap_elem->phy_cap_info[5] &=
5842 			~IEEE80211_HE_PHY_CAP5_BEAMFORMEE_NUM_SND_DIM_UNDER_80MHZ_MASK;
5843 		he_cap_elem->phy_cap_info[5] |= ar->num_tx_chains - 1;
5844 
5845 		switch (i) {
5846 		case NL80211_IFTYPE_AP:
5847 			he_cap_elem->phy_cap_info[3] &=
5848 				~IEEE80211_HE_PHY_CAP3_DCM_MAX_CONST_TX_MASK;
5849 			he_cap_elem->phy_cap_info[9] |=
5850 				IEEE80211_HE_PHY_CAP9_RX_1024_QAM_LESS_THAN_242_TONE_RU;
5851 			break;
5852 		case NL80211_IFTYPE_STATION:
5853 			he_cap_elem->mac_cap_info[0] &=
5854 				~IEEE80211_HE_MAC_CAP0_TWT_RES;
5855 			he_cap_elem->mac_cap_info[0] |=
5856 				IEEE80211_HE_MAC_CAP0_TWT_REQ;
5857 			he_cap_elem->phy_cap_info[9] |=
5858 				IEEE80211_HE_PHY_CAP9_TX_1024_QAM_LESS_THAN_242_TONE_RU;
5859 			break;
5860 		case NL80211_IFTYPE_MESH_POINT:
5861 			ath11k_mac_filter_he_cap_mesh(he_cap_elem);
5862 			break;
5863 		}
5864 
5865 		ath11k_mac_set_hemcsmap(ar, cap, he_cap, band);
5866 
5867 		memset(he_cap->ppe_thres, 0, sizeof(he_cap->ppe_thres));
5868 		if (he_cap_elem->phy_cap_info[6] &
5869 		    IEEE80211_HE_PHY_CAP6_PPE_THRESHOLD_PRESENT)
5870 			ath11k_gen_ppe_thresh(&band_cap->he_ppet,
5871 					      he_cap->ppe_thres);
5872 
5873 		if (band == NL80211_BAND_6GHZ) {
5874 			data[idx].he_6ghz_capa.capa =
5875 				ath11k_mac_setup_he_6ghz_cap(cap, band_cap);
5876 		}
5877 		idx++;
5878 	}
5879 
5880 	return idx;
5881 }
5882 
ath11k_mac_setup_he_cap(struct ath11k * ar,struct ath11k_pdev_cap * cap)5883 static void ath11k_mac_setup_he_cap(struct ath11k *ar,
5884 				    struct ath11k_pdev_cap *cap)
5885 {
5886 	struct ieee80211_supported_band *band;
5887 	int count;
5888 
5889 	if (cap->supported_bands & WMI_HOST_WLAN_2G_CAP) {
5890 		count = ath11k_mac_copy_he_cap(ar, cap,
5891 					       ar->mac.iftype[NL80211_BAND_2GHZ],
5892 					       NL80211_BAND_2GHZ);
5893 		band = &ar->mac.sbands[NL80211_BAND_2GHZ];
5894 		band->iftype_data = ar->mac.iftype[NL80211_BAND_2GHZ];
5895 		band->n_iftype_data = count;
5896 	}
5897 
5898 	if (cap->supported_bands & WMI_HOST_WLAN_5G_CAP) {
5899 		count = ath11k_mac_copy_he_cap(ar, cap,
5900 					       ar->mac.iftype[NL80211_BAND_5GHZ],
5901 					       NL80211_BAND_5GHZ);
5902 		band = &ar->mac.sbands[NL80211_BAND_5GHZ];
5903 		band->iftype_data = ar->mac.iftype[NL80211_BAND_5GHZ];
5904 		band->n_iftype_data = count;
5905 	}
5906 
5907 	if (cap->supported_bands & WMI_HOST_WLAN_5G_CAP &&
5908 	    ar->supports_6ghz) {
5909 		count = ath11k_mac_copy_he_cap(ar, cap,
5910 					       ar->mac.iftype[NL80211_BAND_6GHZ],
5911 					       NL80211_BAND_6GHZ);
5912 		band = &ar->mac.sbands[NL80211_BAND_6GHZ];
5913 		band->iftype_data = ar->mac.iftype[NL80211_BAND_6GHZ];
5914 		band->n_iftype_data = count;
5915 	}
5916 }
5917 
__ath11k_set_antenna(struct ath11k * ar,u32 tx_ant,u32 rx_ant)5918 static int __ath11k_set_antenna(struct ath11k *ar, u32 tx_ant, u32 rx_ant)
5919 {
5920 	int ret;
5921 
5922 	lockdep_assert_held(&ar->conf_mutex);
5923 
5924 	if (ath11k_check_chain_mask(ar, tx_ant, true))
5925 		return -EINVAL;
5926 
5927 	if (ath11k_check_chain_mask(ar, rx_ant, false))
5928 		return -EINVAL;
5929 
5930 	ar->cfg_tx_chainmask = tx_ant;
5931 	ar->cfg_rx_chainmask = rx_ant;
5932 
5933 	if (ar->state != ATH11K_STATE_ON &&
5934 	    ar->state != ATH11K_STATE_RESTARTED)
5935 		return 0;
5936 
5937 	ret = ath11k_wmi_pdev_set_param(ar, WMI_PDEV_PARAM_TX_CHAIN_MASK,
5938 					tx_ant, ar->pdev->pdev_id);
5939 	if (ret) {
5940 		ath11k_warn(ar->ab, "failed to set tx-chainmask: %d, req 0x%x\n",
5941 			    ret, tx_ant);
5942 		return ret;
5943 	}
5944 
5945 	ar->num_tx_chains = get_num_chains(tx_ant);
5946 
5947 	ret = ath11k_wmi_pdev_set_param(ar, WMI_PDEV_PARAM_RX_CHAIN_MASK,
5948 					rx_ant, ar->pdev->pdev_id);
5949 	if (ret) {
5950 		ath11k_warn(ar->ab, "failed to set rx-chainmask: %d, req 0x%x\n",
5951 			    ret, rx_ant);
5952 		return ret;
5953 	}
5954 
5955 	ar->num_rx_chains = get_num_chains(rx_ant);
5956 
5957 	/* Reload HT/VHT/HE capability */
5958 	ath11k_mac_setup_ht_vht_cap(ar, &ar->pdev->cap, NULL);
5959 	ath11k_mac_setup_he_cap(ar, &ar->pdev->cap);
5960 
5961 	return 0;
5962 }
5963 
ath11k_mgmt_over_wmi_tx_drop(struct ath11k * ar,struct sk_buff * skb)5964 static void ath11k_mgmt_over_wmi_tx_drop(struct ath11k *ar, struct sk_buff *skb)
5965 {
5966 	int num_mgmt;
5967 
5968 	ieee80211_free_txskb(ar->hw, skb);
5969 
5970 	num_mgmt = atomic_dec_if_positive(&ar->num_pending_mgmt_tx);
5971 
5972 	if (num_mgmt < 0)
5973 		WARN_ON_ONCE(1);
5974 
5975 	if (!num_mgmt)
5976 		wake_up(&ar->txmgmt_empty_waitq);
5977 }
5978 
ath11k_mac_tx_mgmt_free(struct ath11k * ar,int buf_id)5979 static void ath11k_mac_tx_mgmt_free(struct ath11k *ar, int buf_id)
5980 {
5981 	struct sk_buff *msdu;
5982 	struct ieee80211_tx_info *info;
5983 
5984 	spin_lock_bh(&ar->txmgmt_idr_lock);
5985 	msdu = idr_remove(&ar->txmgmt_idr, buf_id);
5986 	spin_unlock_bh(&ar->txmgmt_idr_lock);
5987 
5988 	if (!msdu)
5989 		return;
5990 
5991 	dma_unmap_single(ar->ab->dev, ATH11K_SKB_CB(msdu)->paddr, msdu->len,
5992 			 DMA_TO_DEVICE);
5993 
5994 	info = IEEE80211_SKB_CB(msdu);
5995 	memset(&info->status, 0, sizeof(info->status));
5996 
5997 	ath11k_mgmt_over_wmi_tx_drop(ar, msdu);
5998 }
5999 
ath11k_mac_tx_mgmt_pending_free(int buf_id,void * skb,void * ctx)6000 int ath11k_mac_tx_mgmt_pending_free(int buf_id, void *skb, void *ctx)
6001 {
6002 	struct ath11k *ar = ctx;
6003 
6004 	ath11k_mac_tx_mgmt_free(ar, buf_id);
6005 
6006 	return 0;
6007 }
6008 
ath11k_mac_vif_txmgmt_idr_remove(int buf_id,void * skb,void * ctx)6009 static int ath11k_mac_vif_txmgmt_idr_remove(int buf_id, void *skb, void *ctx)
6010 {
6011 	struct ieee80211_vif *vif = ctx;
6012 	struct ath11k_skb_cb *skb_cb = ATH11K_SKB_CB((struct sk_buff *)skb);
6013 	struct ath11k *ar = skb_cb->ar;
6014 
6015 	if (skb_cb->vif == vif)
6016 		ath11k_mac_tx_mgmt_free(ar, buf_id);
6017 
6018 	return 0;
6019 }
6020 
ath11k_mac_mgmt_tx_wmi(struct ath11k * ar,struct ath11k_vif * arvif,struct sk_buff * skb)6021 static int ath11k_mac_mgmt_tx_wmi(struct ath11k *ar, struct ath11k_vif *arvif,
6022 				  struct sk_buff *skb)
6023 {
6024 	struct ath11k_base *ab = ar->ab;
6025 	struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
6026 	struct ieee80211_tx_info *info;
6027 	dma_addr_t paddr;
6028 	int buf_id;
6029 	int ret;
6030 
6031 	ATH11K_SKB_CB(skb)->ar = ar;
6032 
6033 	spin_lock_bh(&ar->txmgmt_idr_lock);
6034 	buf_id = idr_alloc(&ar->txmgmt_idr, skb, 0,
6035 			   ATH11K_TX_MGMT_NUM_PENDING_MAX, GFP_ATOMIC);
6036 	spin_unlock_bh(&ar->txmgmt_idr_lock);
6037 
6038 	ath11k_dbg(ar->ab, ATH11K_DBG_MAC,
6039 		   "tx mgmt frame, buf id %d\n", buf_id);
6040 
6041 	if (buf_id < 0)
6042 		return -ENOSPC;
6043 
6044 	info = IEEE80211_SKB_CB(skb);
6045 	if (!(info->flags & IEEE80211_TX_CTL_HW_80211_ENCAP)) {
6046 		if ((ieee80211_is_action(hdr->frame_control) ||
6047 		     ieee80211_is_deauth(hdr->frame_control) ||
6048 		     ieee80211_is_disassoc(hdr->frame_control)) &&
6049 		     ieee80211_has_protected(hdr->frame_control)) {
6050 			skb_put(skb, IEEE80211_CCMP_MIC_LEN);
6051 		}
6052 	}
6053 
6054 	paddr = dma_map_single(ab->dev, skb->data, skb->len, DMA_TO_DEVICE);
6055 	if (dma_mapping_error(ab->dev, paddr)) {
6056 		ath11k_warn(ab, "failed to DMA map mgmt Tx buffer\n");
6057 		ret = -EIO;
6058 		goto err_free_idr;
6059 	}
6060 
6061 	ATH11K_SKB_CB(skb)->paddr = paddr;
6062 
6063 	ret = ath11k_wmi_mgmt_send(ar, arvif->vdev_id, buf_id, skb);
6064 	if (ret) {
6065 		ath11k_warn(ar->ab, "failed to send mgmt frame: %d\n", ret);
6066 		goto err_unmap_buf;
6067 	}
6068 
6069 	return 0;
6070 
6071 err_unmap_buf:
6072 	dma_unmap_single(ab->dev, ATH11K_SKB_CB(skb)->paddr,
6073 			 skb->len, DMA_TO_DEVICE);
6074 err_free_idr:
6075 	spin_lock_bh(&ar->txmgmt_idr_lock);
6076 	idr_remove(&ar->txmgmt_idr, buf_id);
6077 	spin_unlock_bh(&ar->txmgmt_idr_lock);
6078 
6079 	return ret;
6080 }
6081 
ath11k_mgmt_over_wmi_tx_purge(struct ath11k * ar)6082 static void ath11k_mgmt_over_wmi_tx_purge(struct ath11k *ar)
6083 {
6084 	struct sk_buff *skb;
6085 
6086 	while ((skb = skb_dequeue(&ar->wmi_mgmt_tx_queue)) != NULL)
6087 		ath11k_mgmt_over_wmi_tx_drop(ar, skb);
6088 }
6089 
ath11k_mgmt_over_wmi_tx_work(struct work_struct * work)6090 static void ath11k_mgmt_over_wmi_tx_work(struct work_struct *work)
6091 {
6092 	struct ath11k *ar = container_of(work, struct ath11k, wmi_mgmt_tx_work);
6093 	struct ath11k_skb_cb *skb_cb;
6094 	struct ath11k_vif *arvif;
6095 	struct sk_buff *skb;
6096 	int ret;
6097 
6098 	while ((skb = skb_dequeue(&ar->wmi_mgmt_tx_queue)) != NULL) {
6099 		skb_cb = ATH11K_SKB_CB(skb);
6100 		if (!skb_cb->vif) {
6101 			ath11k_warn(ar->ab, "no vif found for mgmt frame\n");
6102 			ath11k_mgmt_over_wmi_tx_drop(ar, skb);
6103 			continue;
6104 		}
6105 
6106 		arvif = ath11k_vif_to_arvif(skb_cb->vif);
6107 		mutex_lock(&ar->conf_mutex);
6108 		if (ar->allocated_vdev_map & (1LL << arvif->vdev_id)) {
6109 			ret = ath11k_mac_mgmt_tx_wmi(ar, arvif, skb);
6110 			if (ret) {
6111 				ath11k_warn(ar->ab, "failed to tx mgmt frame, vdev_id %d :%d\n",
6112 					    arvif->vdev_id, ret);
6113 				ath11k_mgmt_over_wmi_tx_drop(ar, skb);
6114 			} else {
6115 				ath11k_dbg(ar->ab, ATH11K_DBG_MAC,
6116 					   "tx mgmt frame, vdev_id %d\n",
6117 					   arvif->vdev_id);
6118 			}
6119 		} else {
6120 			ath11k_warn(ar->ab,
6121 				    "dropping mgmt frame for vdev %d, is_started %d\n",
6122 				    arvif->vdev_id,
6123 				    arvif->is_started);
6124 			ath11k_mgmt_over_wmi_tx_drop(ar, skb);
6125 		}
6126 		mutex_unlock(&ar->conf_mutex);
6127 	}
6128 }
6129 
ath11k_mac_mgmt_tx(struct ath11k * ar,struct sk_buff * skb,bool is_prb_rsp)6130 static int ath11k_mac_mgmt_tx(struct ath11k *ar, struct sk_buff *skb,
6131 			      bool is_prb_rsp)
6132 {
6133 	struct sk_buff_head *q = &ar->wmi_mgmt_tx_queue;
6134 
6135 	if (test_bit(ATH11K_FLAG_CRASH_FLUSH, &ar->ab->dev_flags))
6136 		return -ESHUTDOWN;
6137 
6138 	/* Drop probe response packets when the pending management tx
6139 	 * count has reached a certain threshold, so as to prioritize
6140 	 * other mgmt packets like auth and assoc to be sent on time
6141 	 * for establishing successful connections.
6142 	 */
6143 	if (is_prb_rsp &&
6144 	    atomic_read(&ar->num_pending_mgmt_tx) > ATH11K_PRB_RSP_DROP_THRESHOLD) {
6145 		ath11k_warn(ar->ab,
6146 			    "dropping probe response as pending queue is almost full\n");
6147 		return -ENOSPC;
6148 	}
6149 
6150 	if (skb_queue_len_lockless(q) >= ATH11K_TX_MGMT_NUM_PENDING_MAX) {
6151 		ath11k_warn(ar->ab, "mgmt tx queue is full\n");
6152 		return -ENOSPC;
6153 	}
6154 
6155 	skb_queue_tail(q, skb);
6156 	atomic_inc(&ar->num_pending_mgmt_tx);
6157 	queue_work(ar->ab->workqueue_aux, &ar->wmi_mgmt_tx_work);
6158 
6159 	return 0;
6160 }
6161 
ath11k_mac_op_tx(struct ieee80211_hw * hw,struct ieee80211_tx_control * control,struct sk_buff * skb)6162 static void ath11k_mac_op_tx(struct ieee80211_hw *hw,
6163 			     struct ieee80211_tx_control *control,
6164 			     struct sk_buff *skb)
6165 {
6166 	struct ath11k_skb_cb *skb_cb = ATH11K_SKB_CB(skb);
6167 	struct ath11k *ar = hw->priv;
6168 	struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
6169 	struct ieee80211_vif *vif = info->control.vif;
6170 	struct ath11k_vif *arvif = ath11k_vif_to_arvif(vif);
6171 	struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
6172 	struct ieee80211_key_conf *key = info->control.hw_key;
6173 	struct ath11k_sta *arsta = NULL;
6174 	u32 info_flags = info->flags;
6175 	bool is_prb_rsp;
6176 	int ret;
6177 
6178 	memset(skb_cb, 0, sizeof(*skb_cb));
6179 	skb_cb->vif = vif;
6180 
6181 	if (key) {
6182 		skb_cb->cipher = key->cipher;
6183 		skb_cb->flags |= ATH11K_SKB_CIPHER_SET;
6184 	}
6185 
6186 	if (info_flags & IEEE80211_TX_CTL_HW_80211_ENCAP) {
6187 		skb_cb->flags |= ATH11K_SKB_HW_80211_ENCAP;
6188 	} else if (ieee80211_is_mgmt(hdr->frame_control)) {
6189 		is_prb_rsp = ieee80211_is_probe_resp(hdr->frame_control);
6190 		ret = ath11k_mac_mgmt_tx(ar, skb, is_prb_rsp);
6191 		if (ret) {
6192 			ath11k_warn(ar->ab, "failed to queue management frame %d\n",
6193 				    ret);
6194 			ieee80211_free_txskb(ar->hw, skb);
6195 		}
6196 		return;
6197 	}
6198 
6199 	if (control->sta)
6200 		arsta = (struct ath11k_sta *)control->sta->drv_priv;
6201 
6202 	ret = ath11k_dp_tx(ar, arvif, arsta, skb);
6203 	if (unlikely(ret)) {
6204 		ath11k_warn(ar->ab, "failed to transmit frame %d\n", ret);
6205 		ieee80211_free_txskb(ar->hw, skb);
6206 	}
6207 }
6208 
ath11k_mac_drain_tx(struct ath11k * ar)6209 void ath11k_mac_drain_tx(struct ath11k *ar)
6210 {
6211 	/* make sure rcu-protected mac80211 tx path itself is drained */
6212 	synchronize_net();
6213 
6214 	cancel_work_sync(&ar->wmi_mgmt_tx_work);
6215 	ath11k_mgmt_over_wmi_tx_purge(ar);
6216 }
6217 
ath11k_mac_config_mon_status_default(struct ath11k * ar,bool enable)6218 static int ath11k_mac_config_mon_status_default(struct ath11k *ar, bool enable)
6219 {
6220 	struct htt_rx_ring_tlv_filter tlv_filter = {0};
6221 	struct ath11k_base *ab = ar->ab;
6222 	int i, ret = 0;
6223 	u32 ring_id;
6224 
6225 	if (enable) {
6226 		tlv_filter = ath11k_mac_mon_status_filter_default;
6227 		if (ath11k_debugfs_rx_filter(ar))
6228 			tlv_filter.rx_filter = ath11k_debugfs_rx_filter(ar);
6229 	}
6230 
6231 	for (i = 0; i < ab->hw_params.num_rxmda_per_pdev; i++) {
6232 		ring_id = ar->dp.rx_mon_status_refill_ring[i].refill_buf_ring.ring_id;
6233 		ret = ath11k_dp_tx_htt_rx_filter_setup(ar->ab, ring_id,
6234 						       ar->dp.mac_id + i,
6235 						       HAL_RXDMA_MONITOR_STATUS,
6236 						       DP_RX_BUFFER_SIZE,
6237 						       &tlv_filter);
6238 	}
6239 
6240 	if (enable && !ar->ab->hw_params.rxdma1_enable)
6241 		mod_timer(&ar->ab->mon_reap_timer, jiffies +
6242 			  msecs_to_jiffies(ATH11K_MON_TIMER_INTERVAL));
6243 
6244 	return ret;
6245 }
6246 
ath11k_mac_wait_reconfigure(struct ath11k_base * ab)6247 static void ath11k_mac_wait_reconfigure(struct ath11k_base *ab)
6248 {
6249 	int recovery_start_count;
6250 
6251 	if (!ab->is_reset)
6252 		return;
6253 
6254 	recovery_start_count = atomic_inc_return(&ab->recovery_start_count);
6255 	ath11k_dbg(ab, ATH11K_DBG_MAC, "recovery start count %d\n", recovery_start_count);
6256 
6257 	if (recovery_start_count == ab->num_radios) {
6258 		complete(&ab->recovery_start);
6259 		ath11k_dbg(ab, ATH11K_DBG_MAC, "recovery started success\n");
6260 	}
6261 
6262 	ath11k_dbg(ab, ATH11K_DBG_MAC, "waiting reconfigure...\n");
6263 
6264 	wait_for_completion_timeout(&ab->reconfigure_complete,
6265 				    ATH11K_RECONFIGURE_TIMEOUT_HZ);
6266 }
6267 
ath11k_mac_op_start(struct ieee80211_hw * hw)6268 static int ath11k_mac_op_start(struct ieee80211_hw *hw)
6269 {
6270 	struct ath11k *ar = hw->priv;
6271 	struct ath11k_base *ab = ar->ab;
6272 	struct ath11k_pdev *pdev = ar->pdev;
6273 	int ret;
6274 
6275 	if (ath11k_ftm_mode) {
6276 		ath11k_warn(ab, "mac operations not supported in factory test mode\n");
6277 		return -EOPNOTSUPP;
6278 	}
6279 
6280 	ath11k_mac_drain_tx(ar);
6281 	mutex_lock(&ar->conf_mutex);
6282 
6283 	switch (ar->state) {
6284 	case ATH11K_STATE_OFF:
6285 		ar->state = ATH11K_STATE_ON;
6286 		break;
6287 	case ATH11K_STATE_RESTARTING:
6288 		ar->state = ATH11K_STATE_RESTARTED;
6289 		ath11k_mac_wait_reconfigure(ab);
6290 		break;
6291 	case ATH11K_STATE_RESTARTED:
6292 	case ATH11K_STATE_WEDGED:
6293 	case ATH11K_STATE_ON:
6294 	case ATH11K_STATE_FTM:
6295 		WARN_ON(1);
6296 		ret = -EINVAL;
6297 		goto err;
6298 	}
6299 
6300 	ret = ath11k_wmi_pdev_set_param(ar, WMI_PDEV_PARAM_PMF_QOS,
6301 					1, pdev->pdev_id);
6302 
6303 	if (ret) {
6304 		ath11k_err(ar->ab, "failed to enable PMF QOS: (%d\n", ret);
6305 		goto err;
6306 	}
6307 
6308 	ret = ath11k_wmi_pdev_set_param(ar, WMI_PDEV_PARAM_DYNAMIC_BW, 1,
6309 					pdev->pdev_id);
6310 	if (ret) {
6311 		ath11k_err(ar->ab, "failed to enable dynamic bw: %d\n", ret);
6312 		goto err;
6313 	}
6314 
6315 	if (test_bit(WMI_TLV_SERVICE_SPOOF_MAC_SUPPORT, ar->wmi->wmi_ab->svc_map)) {
6316 		ret = ath11k_wmi_scan_prob_req_oui(ar, ar->mac_addr);
6317 		if (ret) {
6318 			ath11k_err(ab, "failed to set prob req oui: %i\n", ret);
6319 			goto err;
6320 		}
6321 	}
6322 
6323 	ret = ath11k_wmi_pdev_set_param(ar, WMI_PDEV_PARAM_ARP_AC_OVERRIDE,
6324 					0, pdev->pdev_id);
6325 	if (ret) {
6326 		ath11k_err(ab, "failed to set ac override for ARP: %d\n",
6327 			   ret);
6328 		goto err;
6329 	}
6330 
6331 	ret = ath11k_wmi_send_dfs_phyerr_offload_enable_cmd(ar, pdev->pdev_id);
6332 	if (ret) {
6333 		ath11k_err(ab, "failed to offload radar detection: %d\n",
6334 			   ret);
6335 		goto err;
6336 	}
6337 
6338 	ret = ath11k_dp_tx_htt_h2t_ppdu_stats_req(ar,
6339 						  HTT_PPDU_STATS_TAG_DEFAULT);
6340 	if (ret) {
6341 		ath11k_err(ab, "failed to req ppdu stats: %d\n", ret);
6342 		goto err;
6343 	}
6344 
6345 	ret = ath11k_wmi_pdev_set_param(ar, WMI_PDEV_PARAM_MESH_MCAST_ENABLE,
6346 					1, pdev->pdev_id);
6347 
6348 	if (ret) {
6349 		ath11k_err(ar->ab, "failed to enable MESH MCAST ENABLE: (%d\n", ret);
6350 		goto err;
6351 	}
6352 
6353 	__ath11k_set_antenna(ar, ar->cfg_tx_chainmask, ar->cfg_rx_chainmask);
6354 
6355 	/* TODO: Do we need to enable ANI? */
6356 
6357 	ath11k_reg_update_chan_list(ar, false);
6358 
6359 	ar->num_started_vdevs = 0;
6360 	ar->num_created_vdevs = 0;
6361 	ar->num_peers = 0;
6362 	ar->allocated_vdev_map = 0;
6363 
6364 	/* Configure monitor status ring with default rx_filter to get rx status
6365 	 * such as rssi, rx_duration.
6366 	 */
6367 	ret = ath11k_mac_config_mon_status_default(ar, true);
6368 	if (ret) {
6369 		ath11k_err(ab, "failed to configure monitor status ring with default rx_filter: (%d)\n",
6370 			   ret);
6371 		goto err;
6372 	}
6373 
6374 	/* Configure the hash seed for hash based reo dest ring selection */
6375 	ath11k_wmi_pdev_lro_cfg(ar, ar->pdev->pdev_id);
6376 
6377 	/* allow device to enter IMPS */
6378 	if (ab->hw_params.idle_ps) {
6379 		ret = ath11k_wmi_pdev_set_param(ar, WMI_PDEV_PARAM_IDLE_PS_CONFIG,
6380 						1, pdev->pdev_id);
6381 		if (ret) {
6382 			ath11k_err(ab, "failed to enable idle ps: %d\n", ret);
6383 			goto err;
6384 		}
6385 	}
6386 
6387 	mutex_unlock(&ar->conf_mutex);
6388 
6389 	rcu_assign_pointer(ab->pdevs_active[ar->pdev_idx],
6390 			   &ab->pdevs[ar->pdev_idx]);
6391 
6392 	return 0;
6393 
6394 err:
6395 	ar->state = ATH11K_STATE_OFF;
6396 	mutex_unlock(&ar->conf_mutex);
6397 
6398 	return ret;
6399 }
6400 
ath11k_mac_op_stop(struct ieee80211_hw * hw)6401 static void ath11k_mac_op_stop(struct ieee80211_hw *hw)
6402 {
6403 	struct ath11k *ar = hw->priv;
6404 	struct htt_ppdu_stats_info *ppdu_stats, *tmp;
6405 	int ret;
6406 
6407 	ath11k_mac_drain_tx(ar);
6408 
6409 	mutex_lock(&ar->conf_mutex);
6410 	ret = ath11k_mac_config_mon_status_default(ar, false);
6411 	if (ret)
6412 		ath11k_err(ar->ab, "failed to clear rx_filter for monitor status ring: (%d)\n",
6413 			   ret);
6414 
6415 	clear_bit(ATH11K_CAC_RUNNING, &ar->dev_flags);
6416 	ar->state = ATH11K_STATE_OFF;
6417 	mutex_unlock(&ar->conf_mutex);
6418 
6419 	cancel_delayed_work_sync(&ar->scan.timeout);
6420 	cancel_work_sync(&ar->regd_update_work);
6421 	cancel_work_sync(&ar->ab->update_11d_work);
6422 
6423 	if (ar->state_11d == ATH11K_11D_PREPARING) {
6424 		ar->state_11d = ATH11K_11D_IDLE;
6425 		complete(&ar->completed_11d_scan);
6426 	}
6427 
6428 	spin_lock_bh(&ar->data_lock);
6429 	list_for_each_entry_safe(ppdu_stats, tmp, &ar->ppdu_stats_info, list) {
6430 		list_del(&ppdu_stats->list);
6431 		kfree(ppdu_stats);
6432 	}
6433 	spin_unlock_bh(&ar->data_lock);
6434 
6435 	rcu_assign_pointer(ar->ab->pdevs_active[ar->pdev_idx], NULL);
6436 
6437 	synchronize_rcu();
6438 
6439 	atomic_set(&ar->num_pending_mgmt_tx, 0);
6440 }
6441 
ath11k_mac_setup_vdev_params_mbssid(struct ath11k_vif * arvif,u32 * flags,u32 * tx_vdev_id)6442 static int ath11k_mac_setup_vdev_params_mbssid(struct ath11k_vif *arvif,
6443 					       u32 *flags, u32 *tx_vdev_id)
6444 {
6445 	struct ath11k *ar = arvif->ar;
6446 	struct ath11k_vif *tx_arvif;
6447 	struct ieee80211_vif *tx_vif;
6448 
6449 	*tx_vdev_id = 0;
6450 	tx_vif = arvif->vif->mbssid_tx_vif;
6451 	if (!tx_vif) {
6452 		*flags = WMI_HOST_VDEV_FLAGS_NON_MBSSID_AP;
6453 		return 0;
6454 	}
6455 
6456 	tx_arvif = ath11k_vif_to_arvif(tx_vif);
6457 
6458 	if (arvif->vif->bss_conf.nontransmitted) {
6459 		if (ar->hw->wiphy != ieee80211_vif_to_wdev(tx_vif)->wiphy)
6460 			return -EINVAL;
6461 
6462 		*flags = WMI_HOST_VDEV_FLAGS_NON_TRANSMIT_AP;
6463 		*tx_vdev_id = ath11k_vif_to_arvif(tx_vif)->vdev_id;
6464 	} else if (tx_arvif == arvif) {
6465 		*flags = WMI_HOST_VDEV_FLAGS_TRANSMIT_AP;
6466 	} else {
6467 		return -EINVAL;
6468 	}
6469 
6470 	if (arvif->vif->bss_conf.ema_ap)
6471 		*flags |= WMI_HOST_VDEV_FLAGS_EMA_MODE;
6472 
6473 	return 0;
6474 }
6475 
ath11k_mac_setup_vdev_create_params(struct ath11k_vif * arvif,struct vdev_create_params * params)6476 static int ath11k_mac_setup_vdev_create_params(struct ath11k_vif *arvif,
6477 					       struct vdev_create_params *params)
6478 {
6479 	struct ath11k *ar = arvif->ar;
6480 	struct ath11k_pdev *pdev = ar->pdev;
6481 	int ret;
6482 
6483 	params->if_id = arvif->vdev_id;
6484 	params->type = arvif->vdev_type;
6485 	params->subtype = arvif->vdev_subtype;
6486 	params->pdev_id = pdev->pdev_id;
6487 	params->mbssid_flags = 0;
6488 	params->mbssid_tx_vdev_id = 0;
6489 
6490 	if (!test_bit(WMI_TLV_SERVICE_MBSS_PARAM_IN_VDEV_START_SUPPORT,
6491 		      ar->ab->wmi_ab.svc_map)) {
6492 		ret = ath11k_mac_setup_vdev_params_mbssid(arvif,
6493 							  &params->mbssid_flags,
6494 							  &params->mbssid_tx_vdev_id);
6495 		if (ret)
6496 			return ret;
6497 	}
6498 
6499 	if (pdev->cap.supported_bands & WMI_HOST_WLAN_2G_CAP) {
6500 		params->chains[NL80211_BAND_2GHZ].tx = ar->num_tx_chains;
6501 		params->chains[NL80211_BAND_2GHZ].rx = ar->num_rx_chains;
6502 	}
6503 	if (pdev->cap.supported_bands & WMI_HOST_WLAN_5G_CAP) {
6504 		params->chains[NL80211_BAND_5GHZ].tx = ar->num_tx_chains;
6505 		params->chains[NL80211_BAND_5GHZ].rx = ar->num_rx_chains;
6506 	}
6507 	if (pdev->cap.supported_bands & WMI_HOST_WLAN_5G_CAP &&
6508 	    ar->supports_6ghz) {
6509 		params->chains[NL80211_BAND_6GHZ].tx = ar->num_tx_chains;
6510 		params->chains[NL80211_BAND_6GHZ].rx = ar->num_rx_chains;
6511 	}
6512 	return 0;
6513 }
6514 
ath11k_mac_op_update_vif_offload(struct ieee80211_hw * hw,struct ieee80211_vif * vif)6515 static void ath11k_mac_op_update_vif_offload(struct ieee80211_hw *hw,
6516 					     struct ieee80211_vif *vif)
6517 {
6518 	struct ath11k *ar = hw->priv;
6519 	struct ath11k_base *ab = ar->ab;
6520 	struct ath11k_vif *arvif = ath11k_vif_to_arvif(vif);
6521 	u32 param_id, param_value;
6522 	int ret;
6523 
6524 	param_id = WMI_VDEV_PARAM_TX_ENCAP_TYPE;
6525 	if (ath11k_frame_mode != ATH11K_HW_TXRX_ETHERNET ||
6526 	    (vif->type != NL80211_IFTYPE_STATION &&
6527 	     vif->type != NL80211_IFTYPE_AP))
6528 		vif->offload_flags &= ~(IEEE80211_OFFLOAD_ENCAP_ENABLED |
6529 					IEEE80211_OFFLOAD_DECAP_ENABLED);
6530 
6531 	if (vif->offload_flags & IEEE80211_OFFLOAD_ENCAP_ENABLED)
6532 		param_value = ATH11K_HW_TXRX_ETHERNET;
6533 	else if (test_bit(ATH11K_FLAG_RAW_MODE, &ab->dev_flags))
6534 		param_value = ATH11K_HW_TXRX_RAW;
6535 	else
6536 		param_value = ATH11K_HW_TXRX_NATIVE_WIFI;
6537 
6538 	ret = ath11k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
6539 					    param_id, param_value);
6540 	if (ret) {
6541 		ath11k_warn(ab, "failed to set vdev %d tx encap mode: %d\n",
6542 			    arvif->vdev_id, ret);
6543 		vif->offload_flags &= ~IEEE80211_OFFLOAD_ENCAP_ENABLED;
6544 	}
6545 
6546 	param_id = WMI_VDEV_PARAM_RX_DECAP_TYPE;
6547 	if (vif->offload_flags & IEEE80211_OFFLOAD_DECAP_ENABLED)
6548 		param_value = ATH11K_HW_TXRX_ETHERNET;
6549 	else if (test_bit(ATH11K_FLAG_RAW_MODE, &ab->dev_flags))
6550 		param_value = ATH11K_HW_TXRX_RAW;
6551 	else
6552 		param_value = ATH11K_HW_TXRX_NATIVE_WIFI;
6553 
6554 	ret = ath11k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
6555 					    param_id, param_value);
6556 	if (ret) {
6557 		ath11k_warn(ab, "failed to set vdev %d rx decap mode: %d\n",
6558 			    arvif->vdev_id, ret);
6559 		vif->offload_flags &= ~IEEE80211_OFFLOAD_DECAP_ENABLED;
6560 	}
6561 }
6562 
ath11k_mac_vif_ap_active_any(struct ath11k_base * ab)6563 static bool ath11k_mac_vif_ap_active_any(struct ath11k_base *ab)
6564 {
6565 	struct ath11k *ar;
6566 	struct ath11k_pdev *pdev;
6567 	struct ath11k_vif *arvif;
6568 	int i;
6569 
6570 	for (i = 0; i < ab->num_radios; i++) {
6571 		pdev = &ab->pdevs[i];
6572 		ar = pdev->ar;
6573 		list_for_each_entry(arvif, &ar->arvifs, list) {
6574 			if (arvif->is_up && arvif->vdev_type == WMI_VDEV_TYPE_AP)
6575 				return true;
6576 		}
6577 	}
6578 	return false;
6579 }
6580 
ath11k_mac_11d_scan_start(struct ath11k * ar,u32 vdev_id)6581 void ath11k_mac_11d_scan_start(struct ath11k *ar, u32 vdev_id)
6582 {
6583 	struct wmi_11d_scan_start_params param;
6584 	int ret;
6585 
6586 	mutex_lock(&ar->ab->vdev_id_11d_lock);
6587 
6588 	ath11k_dbg(ar->ab, ATH11K_DBG_MAC, "vdev id for 11d scan %d\n",
6589 		   ar->vdev_id_11d_scan);
6590 
6591 	if (ar->regdom_set_by_user)
6592 		goto fin;
6593 
6594 	if (ar->vdev_id_11d_scan != ATH11K_11D_INVALID_VDEV_ID)
6595 		goto fin;
6596 
6597 	if (!test_bit(WMI_TLV_SERVICE_11D_OFFLOAD, ar->ab->wmi_ab.svc_map))
6598 		goto fin;
6599 
6600 	if (ath11k_mac_vif_ap_active_any(ar->ab))
6601 		goto fin;
6602 
6603 	param.vdev_id = vdev_id;
6604 	param.start_interval_msec = 0;
6605 	param.scan_period_msec = ATH11K_SCAN_11D_INTERVAL;
6606 
6607 	ath11k_dbg(ar->ab, ATH11K_DBG_MAC, "start 11d scan\n");
6608 
6609 	ret = ath11k_wmi_send_11d_scan_start_cmd(ar, &param);
6610 	if (ret) {
6611 		ath11k_warn(ar->ab, "failed to start 11d scan vdev %d ret: %d\n",
6612 			    vdev_id, ret);
6613 	} else {
6614 		ar->vdev_id_11d_scan = vdev_id;
6615 		if (ar->state_11d == ATH11K_11D_PREPARING)
6616 			ar->state_11d = ATH11K_11D_RUNNING;
6617 	}
6618 
6619 fin:
6620 	if (ar->state_11d == ATH11K_11D_PREPARING) {
6621 		ar->state_11d = ATH11K_11D_IDLE;
6622 		complete(&ar->completed_11d_scan);
6623 	}
6624 
6625 	mutex_unlock(&ar->ab->vdev_id_11d_lock);
6626 }
6627 
ath11k_mac_11d_scan_stop(struct ath11k * ar)6628 void ath11k_mac_11d_scan_stop(struct ath11k *ar)
6629 {
6630 	int ret;
6631 	u32 vdev_id;
6632 
6633 	if (!test_bit(WMI_TLV_SERVICE_11D_OFFLOAD, ar->ab->wmi_ab.svc_map))
6634 		return;
6635 
6636 	ath11k_dbg(ar->ab, ATH11K_DBG_MAC, "stop 11d scan\n");
6637 
6638 	mutex_lock(&ar->ab->vdev_id_11d_lock);
6639 
6640 	ath11k_dbg(ar->ab, ATH11K_DBG_MAC, "stop 11d vdev id %d\n",
6641 		   ar->vdev_id_11d_scan);
6642 
6643 	if (ar->state_11d == ATH11K_11D_PREPARING) {
6644 		ar->state_11d = ATH11K_11D_IDLE;
6645 		complete(&ar->completed_11d_scan);
6646 	}
6647 
6648 	if (ar->vdev_id_11d_scan != ATH11K_11D_INVALID_VDEV_ID) {
6649 		vdev_id = ar->vdev_id_11d_scan;
6650 
6651 		ret = ath11k_wmi_send_11d_scan_stop_cmd(ar, vdev_id);
6652 		if (ret) {
6653 			ath11k_warn(ar->ab,
6654 				    "failed to stopt 11d scan vdev %d ret: %d\n",
6655 				    vdev_id, ret);
6656 		} else {
6657 			ar->vdev_id_11d_scan = ATH11K_11D_INVALID_VDEV_ID;
6658 			ar->state_11d = ATH11K_11D_IDLE;
6659 			complete(&ar->completed_11d_scan);
6660 		}
6661 	}
6662 	mutex_unlock(&ar->ab->vdev_id_11d_lock);
6663 }
6664 
ath11k_mac_11d_scan_stop_all(struct ath11k_base * ab)6665 void ath11k_mac_11d_scan_stop_all(struct ath11k_base *ab)
6666 {
6667 	struct ath11k *ar;
6668 	struct ath11k_pdev *pdev;
6669 	int i;
6670 
6671 	ath11k_dbg(ab, ATH11K_DBG_MAC, "stop soc 11d scan\n");
6672 
6673 	for (i = 0; i < ab->num_radios; i++) {
6674 		pdev = &ab->pdevs[i];
6675 		ar = pdev->ar;
6676 
6677 		ath11k_mac_11d_scan_stop(ar);
6678 	}
6679 }
6680 
ath11k_mac_vdev_delete(struct ath11k * ar,struct ath11k_vif * arvif)6681 static int ath11k_mac_vdev_delete(struct ath11k *ar, struct ath11k_vif *arvif)
6682 {
6683 	unsigned long time_left;
6684 	struct ieee80211_vif *vif = arvif->vif;
6685 	int ret = 0;
6686 
6687 	lockdep_assert_held(&ar->conf_mutex);
6688 
6689 	reinit_completion(&ar->vdev_delete_done);
6690 
6691 	ret = ath11k_wmi_vdev_delete(ar, arvif->vdev_id);
6692 	if (ret) {
6693 		ath11k_warn(ar->ab, "failed to delete WMI vdev %d: %d\n",
6694 			    arvif->vdev_id, ret);
6695 		return ret;
6696 	}
6697 
6698 	time_left = wait_for_completion_timeout(&ar->vdev_delete_done,
6699 						ATH11K_VDEV_DELETE_TIMEOUT_HZ);
6700 	if (time_left == 0) {
6701 		ath11k_warn(ar->ab, "Timeout in receiving vdev delete response\n");
6702 		return -ETIMEDOUT;
6703 	}
6704 
6705 	ar->ab->free_vdev_map |= 1LL << (arvif->vdev_id);
6706 	ar->allocated_vdev_map &= ~(1LL << arvif->vdev_id);
6707 	ar->num_created_vdevs--;
6708 
6709 	ath11k_dbg(ar->ab, ATH11K_DBG_MAC, "vdev %pM deleted, vdev_id %d\n",
6710 		   vif->addr, arvif->vdev_id);
6711 
6712 	return ret;
6713 }
6714 
ath11k_mac_op_add_interface(struct ieee80211_hw * hw,struct ieee80211_vif * vif)6715 static int ath11k_mac_op_add_interface(struct ieee80211_hw *hw,
6716 				       struct ieee80211_vif *vif)
6717 {
6718 	struct ath11k *ar = hw->priv;
6719 	struct ath11k_base *ab = ar->ab;
6720 	struct ath11k_vif *arvif = ath11k_vif_to_arvif(vif);
6721 	struct vdev_create_params vdev_param = {0};
6722 	struct peer_create_params peer_param;
6723 	u32 param_id, param_value;
6724 	u16 nss;
6725 	int i;
6726 	int ret, fbret;
6727 	int bit;
6728 
6729 	vif->driver_flags |= IEEE80211_VIF_SUPPORTS_UAPSD;
6730 
6731 	mutex_lock(&ar->conf_mutex);
6732 
6733 	if (vif->type == NL80211_IFTYPE_AP &&
6734 	    ar->num_peers > (ar->max_num_peers - 1)) {
6735 		ath11k_warn(ab, "failed to create vdev due to insufficient peer entry resource in firmware\n");
6736 		ret = -ENOBUFS;
6737 		goto err;
6738 	}
6739 
6740 	if (ar->num_created_vdevs > (TARGET_NUM_VDEVS(ab) - 1)) {
6741 		ath11k_warn(ab, "failed to create vdev %u, reached max vdev limit %d\n",
6742 			    ar->num_created_vdevs, TARGET_NUM_VDEVS(ab));
6743 		ret = -EBUSY;
6744 		goto err;
6745 	}
6746 
6747 	/* In the case of hardware recovery, debugfs files are
6748 	 * not deleted since ieee80211_ops.remove_interface() is
6749 	 * not invoked. In such cases, try to delete the files.
6750 	 * These will be re-created later.
6751 	 */
6752 	ath11k_debugfs_remove_interface(arvif);
6753 
6754 	memset(arvif, 0, sizeof(*arvif));
6755 
6756 	arvif->ar = ar;
6757 	arvif->vif = vif;
6758 
6759 	INIT_LIST_HEAD(&arvif->list);
6760 	INIT_DELAYED_WORK(&arvif->connection_loss_work,
6761 			  ath11k_mac_vif_sta_connection_loss_work);
6762 
6763 	for (i = 0; i < ARRAY_SIZE(arvif->bitrate_mask.control); i++) {
6764 		arvif->bitrate_mask.control[i].legacy = 0xffffffff;
6765 		arvif->bitrate_mask.control[i].gi = NL80211_TXRATE_FORCE_SGI;
6766 		memset(arvif->bitrate_mask.control[i].ht_mcs, 0xff,
6767 		       sizeof(arvif->bitrate_mask.control[i].ht_mcs));
6768 		memset(arvif->bitrate_mask.control[i].vht_mcs, 0xff,
6769 		       sizeof(arvif->bitrate_mask.control[i].vht_mcs));
6770 		memset(arvif->bitrate_mask.control[i].he_mcs, 0xff,
6771 		       sizeof(arvif->bitrate_mask.control[i].he_mcs));
6772 	}
6773 
6774 	bit = __ffs64(ab->free_vdev_map);
6775 
6776 	arvif->vdev_id = bit;
6777 	arvif->vdev_subtype = WMI_VDEV_SUBTYPE_NONE;
6778 
6779 	switch (vif->type) {
6780 	case NL80211_IFTYPE_UNSPECIFIED:
6781 	case NL80211_IFTYPE_STATION:
6782 		arvif->vdev_type = WMI_VDEV_TYPE_STA;
6783 		break;
6784 	case NL80211_IFTYPE_MESH_POINT:
6785 		arvif->vdev_subtype = WMI_VDEV_SUBTYPE_MESH_11S;
6786 		fallthrough;
6787 	case NL80211_IFTYPE_AP:
6788 		arvif->vdev_type = WMI_VDEV_TYPE_AP;
6789 		break;
6790 	case NL80211_IFTYPE_MONITOR:
6791 		arvif->vdev_type = WMI_VDEV_TYPE_MONITOR;
6792 		ar->monitor_vdev_id = bit;
6793 		break;
6794 	default:
6795 		WARN_ON(1);
6796 		break;
6797 	}
6798 
6799 	ath11k_dbg(ar->ab, ATH11K_DBG_MAC, "add interface id %d type %d subtype %d map %llx\n",
6800 		   arvif->vdev_id, arvif->vdev_type, arvif->vdev_subtype,
6801 		   ab->free_vdev_map);
6802 
6803 	vif->cab_queue = arvif->vdev_id % (ATH11K_HW_MAX_QUEUES - 1);
6804 	for (i = 0; i < ARRAY_SIZE(vif->hw_queue); i++)
6805 		vif->hw_queue[i] = i % (ATH11K_HW_MAX_QUEUES - 1);
6806 
6807 	ret = ath11k_mac_setup_vdev_create_params(arvif, &vdev_param);
6808 	if (ret) {
6809 		ath11k_warn(ab, "failed to create vdev parameters %d: %d\n",
6810 			    arvif->vdev_id, ret);
6811 		goto err;
6812 	}
6813 
6814 	ret = ath11k_wmi_vdev_create(ar, vif->addr, &vdev_param);
6815 	if (ret) {
6816 		ath11k_warn(ab, "failed to create WMI vdev %d: %d\n",
6817 			    arvif->vdev_id, ret);
6818 		goto err;
6819 	}
6820 
6821 	ar->num_created_vdevs++;
6822 	ath11k_dbg(ab, ATH11K_DBG_MAC, "vdev %pM created, vdev_id %d\n",
6823 		   vif->addr, arvif->vdev_id);
6824 	ar->allocated_vdev_map |= 1LL << arvif->vdev_id;
6825 	ab->free_vdev_map &= ~(1LL << arvif->vdev_id);
6826 
6827 	spin_lock_bh(&ar->data_lock);
6828 	list_add(&arvif->list, &ar->arvifs);
6829 	spin_unlock_bh(&ar->data_lock);
6830 
6831 	ath11k_mac_op_update_vif_offload(hw, vif);
6832 
6833 	nss = get_num_chains(ar->cfg_tx_chainmask) ? : 1;
6834 	ret = ath11k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
6835 					    WMI_VDEV_PARAM_NSS, nss);
6836 	if (ret) {
6837 		ath11k_warn(ab, "failed to set vdev %d chainmask 0x%x, nss %d :%d\n",
6838 			    arvif->vdev_id, ar->cfg_tx_chainmask, nss, ret);
6839 		goto err_vdev_del;
6840 	}
6841 
6842 	switch (arvif->vdev_type) {
6843 	case WMI_VDEV_TYPE_AP:
6844 		peer_param.vdev_id = arvif->vdev_id;
6845 		peer_param.peer_addr = vif->addr;
6846 		peer_param.peer_type = WMI_PEER_TYPE_DEFAULT;
6847 		ret = ath11k_peer_create(ar, arvif, NULL, &peer_param);
6848 		if (ret) {
6849 			ath11k_warn(ab, "failed to vdev %d create peer for AP: %d\n",
6850 				    arvif->vdev_id, ret);
6851 			goto err_vdev_del;
6852 		}
6853 
6854 		ret = ath11k_mac_set_kickout(arvif);
6855 		if (ret) {
6856 			ath11k_warn(ar->ab, "failed to set vdev %i kickout parameters: %d\n",
6857 				    arvif->vdev_id, ret);
6858 			goto err_peer_del;
6859 		}
6860 
6861 		ath11k_mac_11d_scan_stop_all(ar->ab);
6862 		break;
6863 	case WMI_VDEV_TYPE_STA:
6864 		param_id = WMI_STA_PS_PARAM_RX_WAKE_POLICY;
6865 		param_value = WMI_STA_PS_RX_WAKE_POLICY_WAKE;
6866 		ret = ath11k_wmi_set_sta_ps_param(ar, arvif->vdev_id,
6867 						  param_id, param_value);
6868 		if (ret) {
6869 			ath11k_warn(ar->ab, "failed to set vdev %d RX wake policy: %d\n",
6870 				    arvif->vdev_id, ret);
6871 			goto err_peer_del;
6872 		}
6873 
6874 		param_id = WMI_STA_PS_PARAM_TX_WAKE_THRESHOLD;
6875 		param_value = WMI_STA_PS_TX_WAKE_THRESHOLD_ALWAYS;
6876 		ret = ath11k_wmi_set_sta_ps_param(ar, arvif->vdev_id,
6877 						  param_id, param_value);
6878 		if (ret) {
6879 			ath11k_warn(ar->ab, "failed to set vdev %d TX wake threshold: %d\n",
6880 				    arvif->vdev_id, ret);
6881 			goto err_peer_del;
6882 		}
6883 
6884 		param_id = WMI_STA_PS_PARAM_PSPOLL_COUNT;
6885 		param_value = WMI_STA_PS_PSPOLL_COUNT_NO_MAX;
6886 		ret = ath11k_wmi_set_sta_ps_param(ar, arvif->vdev_id,
6887 						  param_id, param_value);
6888 		if (ret) {
6889 			ath11k_warn(ar->ab, "failed to set vdev %d pspoll count: %d\n",
6890 				    arvif->vdev_id, ret);
6891 			goto err_peer_del;
6892 		}
6893 
6894 		ret = ath11k_wmi_pdev_set_ps_mode(ar, arvif->vdev_id,
6895 						  WMI_STA_PS_MODE_DISABLED);
6896 		if (ret) {
6897 			ath11k_warn(ar->ab, "failed to disable vdev %d ps mode: %d\n",
6898 				    arvif->vdev_id, ret);
6899 			goto err_peer_del;
6900 		}
6901 
6902 		if (test_bit(WMI_TLV_SERVICE_11D_OFFLOAD, ab->wmi_ab.svc_map)) {
6903 			reinit_completion(&ar->completed_11d_scan);
6904 			ar->state_11d = ATH11K_11D_PREPARING;
6905 		}
6906 		break;
6907 	case WMI_VDEV_TYPE_MONITOR:
6908 		set_bit(ATH11K_FLAG_MONITOR_VDEV_CREATED, &ar->monitor_flags);
6909 		break;
6910 	default:
6911 		break;
6912 	}
6913 
6914 	arvif->txpower = vif->bss_conf.txpower;
6915 	ret = ath11k_mac_txpower_recalc(ar);
6916 	if (ret)
6917 		goto err_peer_del;
6918 
6919 	param_id = WMI_VDEV_PARAM_RTS_THRESHOLD;
6920 	param_value = ar->hw->wiphy->rts_threshold;
6921 	ret = ath11k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
6922 					    param_id, param_value);
6923 	if (ret) {
6924 		ath11k_warn(ar->ab, "failed to set rts threshold for vdev %d: %d\n",
6925 			    arvif->vdev_id, ret);
6926 	}
6927 
6928 	ath11k_dp_vdev_tx_attach(ar, arvif);
6929 
6930 	ath11k_debugfs_add_interface(arvif);
6931 
6932 	if (vif->type != NL80211_IFTYPE_MONITOR &&
6933 	    test_bit(ATH11K_FLAG_MONITOR_CONF_ENABLED, &ar->monitor_flags)) {
6934 		ret = ath11k_mac_monitor_vdev_create(ar);
6935 		if (ret)
6936 			ath11k_warn(ar->ab, "failed to create monitor vdev during add interface: %d",
6937 				    ret);
6938 	}
6939 
6940 	mutex_unlock(&ar->conf_mutex);
6941 
6942 	return 0;
6943 
6944 err_peer_del:
6945 	if (arvif->vdev_type == WMI_VDEV_TYPE_AP) {
6946 		fbret = ath11k_peer_delete(ar, arvif->vdev_id, vif->addr);
6947 		if (fbret) {
6948 			ath11k_warn(ar->ab, "fallback fail to delete peer addr %pM vdev_id %d ret %d\n",
6949 				    vif->addr, arvif->vdev_id, fbret);
6950 			goto err;
6951 		}
6952 	}
6953 
6954 err_vdev_del:
6955 	ath11k_mac_vdev_delete(ar, arvif);
6956 	spin_lock_bh(&ar->data_lock);
6957 	list_del(&arvif->list);
6958 	spin_unlock_bh(&ar->data_lock);
6959 
6960 err:
6961 	mutex_unlock(&ar->conf_mutex);
6962 
6963 	return ret;
6964 }
6965 
ath11k_mac_vif_unref(int buf_id,void * skb,void * ctx)6966 static int ath11k_mac_vif_unref(int buf_id, void *skb, void *ctx)
6967 {
6968 	struct ieee80211_vif *vif = (struct ieee80211_vif *)ctx;
6969 	struct ath11k_skb_cb *skb_cb = ATH11K_SKB_CB((struct sk_buff *)skb);
6970 
6971 	if (skb_cb->vif == vif)
6972 		skb_cb->vif = NULL;
6973 
6974 	return 0;
6975 }
6976 
ath11k_mac_op_remove_interface(struct ieee80211_hw * hw,struct ieee80211_vif * vif)6977 static void ath11k_mac_op_remove_interface(struct ieee80211_hw *hw,
6978 					   struct ieee80211_vif *vif)
6979 {
6980 	struct ath11k *ar = hw->priv;
6981 	struct ath11k_vif *arvif = ath11k_vif_to_arvif(vif);
6982 	struct ath11k_base *ab = ar->ab;
6983 	int ret;
6984 	int i;
6985 
6986 	cancel_delayed_work_sync(&arvif->connection_loss_work);
6987 
6988 	mutex_lock(&ar->conf_mutex);
6989 
6990 	ath11k_dbg(ab, ATH11K_DBG_MAC, "remove interface (vdev %d)\n",
6991 		   arvif->vdev_id);
6992 
6993 	ret = ath11k_spectral_vif_stop(arvif);
6994 	if (ret)
6995 		ath11k_warn(ab, "failed to stop spectral for vdev %i: %d\n",
6996 			    arvif->vdev_id, ret);
6997 
6998 	if (arvif->vdev_type == WMI_VDEV_TYPE_STA)
6999 		ath11k_mac_11d_scan_stop(ar);
7000 
7001 	if (arvif->vdev_type == WMI_VDEV_TYPE_AP) {
7002 		ret = ath11k_peer_delete(ar, arvif->vdev_id, vif->addr);
7003 		if (ret)
7004 			ath11k_warn(ab, "failed to submit AP self-peer removal on vdev %d: %d\n",
7005 				    arvif->vdev_id, ret);
7006 	}
7007 
7008 	ret = ath11k_mac_vdev_delete(ar, arvif);
7009 	if (ret) {
7010 		ath11k_warn(ab, "failed to delete vdev %d: %d\n",
7011 			    arvif->vdev_id, ret);
7012 		goto err_vdev_del;
7013 	}
7014 
7015 	if (arvif->vdev_type == WMI_VDEV_TYPE_MONITOR) {
7016 		clear_bit(ATH11K_FLAG_MONITOR_VDEV_CREATED, &ar->monitor_flags);
7017 		ar->monitor_vdev_id = -1;
7018 	} else if (test_bit(ATH11K_FLAG_MONITOR_VDEV_CREATED, &ar->monitor_flags) &&
7019 		   !test_bit(ATH11K_FLAG_MONITOR_STARTED, &ar->monitor_flags)) {
7020 		ret = ath11k_mac_monitor_vdev_delete(ar);
7021 		if (ret)
7022 			/* continue even if there's an error */
7023 			ath11k_warn(ar->ab, "failed to delete vdev monitor during remove interface: %d",
7024 				    ret);
7025 	}
7026 
7027 err_vdev_del:
7028 	spin_lock_bh(&ar->data_lock);
7029 	list_del(&arvif->list);
7030 	spin_unlock_bh(&ar->data_lock);
7031 
7032 	ath11k_peer_cleanup(ar, arvif->vdev_id);
7033 
7034 	idr_for_each(&ar->txmgmt_idr,
7035 		     ath11k_mac_vif_txmgmt_idr_remove, vif);
7036 
7037 	for (i = 0; i < ab->hw_params.max_tx_ring; i++) {
7038 		spin_lock_bh(&ab->dp.tx_ring[i].tx_idr_lock);
7039 		idr_for_each(&ab->dp.tx_ring[i].txbuf_idr,
7040 			     ath11k_mac_vif_unref, vif);
7041 		spin_unlock_bh(&ab->dp.tx_ring[i].tx_idr_lock);
7042 	}
7043 
7044 	/* Recalc txpower for remaining vdev */
7045 	ath11k_mac_txpower_recalc(ar);
7046 
7047 	ath11k_debugfs_remove_interface(arvif);
7048 
7049 	/* TODO: recal traffic pause state based on the available vdevs */
7050 
7051 	mutex_unlock(&ar->conf_mutex);
7052 }
7053 
7054 /* FIXME: Has to be verified. */
7055 #define SUPPORTED_FILTERS			\
7056 	(FIF_ALLMULTI |				\
7057 	FIF_CONTROL |				\
7058 	FIF_PSPOLL |				\
7059 	FIF_OTHER_BSS |				\
7060 	FIF_BCN_PRBRESP_PROMISC |		\
7061 	FIF_PROBE_REQ |				\
7062 	FIF_FCSFAIL)
7063 
ath11k_mac_op_configure_filter(struct ieee80211_hw * hw,unsigned int changed_flags,unsigned int * total_flags,u64 multicast)7064 static void ath11k_mac_op_configure_filter(struct ieee80211_hw *hw,
7065 					   unsigned int changed_flags,
7066 					   unsigned int *total_flags,
7067 					   u64 multicast)
7068 {
7069 	struct ath11k *ar = hw->priv;
7070 
7071 	mutex_lock(&ar->conf_mutex);
7072 
7073 	*total_flags &= SUPPORTED_FILTERS;
7074 	ar->filter_flags = *total_flags;
7075 
7076 	mutex_unlock(&ar->conf_mutex);
7077 }
7078 
ath11k_mac_op_get_antenna(struct ieee80211_hw * hw,u32 * tx_ant,u32 * rx_ant)7079 static int ath11k_mac_op_get_antenna(struct ieee80211_hw *hw, u32 *tx_ant, u32 *rx_ant)
7080 {
7081 	struct ath11k *ar = hw->priv;
7082 
7083 	mutex_lock(&ar->conf_mutex);
7084 
7085 	*tx_ant = ar->cfg_tx_chainmask;
7086 	*rx_ant = ar->cfg_rx_chainmask;
7087 
7088 	mutex_unlock(&ar->conf_mutex);
7089 
7090 	return 0;
7091 }
7092 
ath11k_mac_op_set_antenna(struct ieee80211_hw * hw,u32 tx_ant,u32 rx_ant)7093 static int ath11k_mac_op_set_antenna(struct ieee80211_hw *hw, u32 tx_ant, u32 rx_ant)
7094 {
7095 	struct ath11k *ar = hw->priv;
7096 	int ret;
7097 
7098 	mutex_lock(&ar->conf_mutex);
7099 	ret = __ath11k_set_antenna(ar, tx_ant, rx_ant);
7100 	mutex_unlock(&ar->conf_mutex);
7101 
7102 	return ret;
7103 }
7104 
ath11k_mac_op_ampdu_action(struct ieee80211_hw * hw,struct ieee80211_vif * vif,struct ieee80211_ampdu_params * params)7105 static int ath11k_mac_op_ampdu_action(struct ieee80211_hw *hw,
7106 				      struct ieee80211_vif *vif,
7107 				      struct ieee80211_ampdu_params *params)
7108 {
7109 	struct ath11k *ar = hw->priv;
7110 	int ret = -EINVAL;
7111 
7112 	mutex_lock(&ar->conf_mutex);
7113 
7114 	switch (params->action) {
7115 	case IEEE80211_AMPDU_RX_START:
7116 		ret = ath11k_dp_rx_ampdu_start(ar, params);
7117 		break;
7118 	case IEEE80211_AMPDU_RX_STOP:
7119 		ret = ath11k_dp_rx_ampdu_stop(ar, params);
7120 		break;
7121 	case IEEE80211_AMPDU_TX_START:
7122 	case IEEE80211_AMPDU_TX_STOP_CONT:
7123 	case IEEE80211_AMPDU_TX_STOP_FLUSH:
7124 	case IEEE80211_AMPDU_TX_STOP_FLUSH_CONT:
7125 	case IEEE80211_AMPDU_TX_OPERATIONAL:
7126 		/* Tx A-MPDU aggregation offloaded to hw/fw so deny mac80211
7127 		 * Tx aggregation requests.
7128 		 */
7129 		ret = -EOPNOTSUPP;
7130 		break;
7131 	}
7132 
7133 	mutex_unlock(&ar->conf_mutex);
7134 
7135 	return ret;
7136 }
7137 
ath11k_mac_op_add_chanctx(struct ieee80211_hw * hw,struct ieee80211_chanctx_conf * ctx)7138 static int ath11k_mac_op_add_chanctx(struct ieee80211_hw *hw,
7139 				     struct ieee80211_chanctx_conf *ctx)
7140 {
7141 	struct ath11k *ar = hw->priv;
7142 	struct ath11k_base *ab = ar->ab;
7143 
7144 	ath11k_dbg(ab, ATH11K_DBG_MAC,
7145 		   "chanctx add freq %u width %d ptr %p\n",
7146 		   ctx->def.chan->center_freq, ctx->def.width, ctx);
7147 
7148 	mutex_lock(&ar->conf_mutex);
7149 
7150 	spin_lock_bh(&ar->data_lock);
7151 	/* TODO: In case of multiple channel context, populate rx_channel from
7152 	 * Rx PPDU desc information.
7153 	 */
7154 	ar->rx_channel = ctx->def.chan;
7155 	spin_unlock_bh(&ar->data_lock);
7156 
7157 	mutex_unlock(&ar->conf_mutex);
7158 
7159 	return 0;
7160 }
7161 
ath11k_mac_op_remove_chanctx(struct ieee80211_hw * hw,struct ieee80211_chanctx_conf * ctx)7162 static void ath11k_mac_op_remove_chanctx(struct ieee80211_hw *hw,
7163 					 struct ieee80211_chanctx_conf *ctx)
7164 {
7165 	struct ath11k *ar = hw->priv;
7166 	struct ath11k_base *ab = ar->ab;
7167 
7168 	ath11k_dbg(ab, ATH11K_DBG_MAC,
7169 		   "chanctx remove freq %u width %d ptr %p\n",
7170 		   ctx->def.chan->center_freq, ctx->def.width, ctx);
7171 
7172 	mutex_lock(&ar->conf_mutex);
7173 
7174 	spin_lock_bh(&ar->data_lock);
7175 	/* TODO: In case of there is one more channel context left, populate
7176 	 * rx_channel with the channel of that remaining channel context.
7177 	 */
7178 	ar->rx_channel = NULL;
7179 	spin_unlock_bh(&ar->data_lock);
7180 
7181 	mutex_unlock(&ar->conf_mutex);
7182 }
7183 
7184 static int
ath11k_mac_vdev_start_restart(struct ath11k_vif * arvif,struct ieee80211_chanctx_conf * ctx,bool restart)7185 ath11k_mac_vdev_start_restart(struct ath11k_vif *arvif,
7186 			      struct ieee80211_chanctx_conf *ctx,
7187 			      bool restart)
7188 {
7189 	struct ath11k *ar = arvif->ar;
7190 	struct ath11k_base *ab = ar->ab;
7191 	struct wmi_vdev_start_req_arg arg = {};
7192 	const struct cfg80211_chan_def *chandef = &ctx->def;
7193 	int ret = 0;
7194 
7195 	lockdep_assert_held(&ar->conf_mutex);
7196 
7197 	reinit_completion(&ar->vdev_setup_done);
7198 
7199 	arg.vdev_id = arvif->vdev_id;
7200 	arg.dtim_period = arvif->dtim_period;
7201 	arg.bcn_intval = arvif->beacon_interval;
7202 
7203 	arg.channel.freq = chandef->chan->center_freq;
7204 	arg.channel.band_center_freq1 = chandef->center_freq1;
7205 	arg.channel.band_center_freq2 = chandef->center_freq2;
7206 	arg.channel.mode =
7207 		ath11k_phymodes[chandef->chan->band][chandef->width];
7208 
7209 	arg.channel.min_power = 0;
7210 	arg.channel.max_power = chandef->chan->max_power;
7211 	arg.channel.max_reg_power = chandef->chan->max_reg_power;
7212 	arg.channel.max_antenna_gain = chandef->chan->max_antenna_gain;
7213 
7214 	arg.pref_tx_streams = ar->num_tx_chains;
7215 	arg.pref_rx_streams = ar->num_rx_chains;
7216 
7217 	arg.mbssid_flags = 0;
7218 	arg.mbssid_tx_vdev_id = 0;
7219 	if (test_bit(WMI_TLV_SERVICE_MBSS_PARAM_IN_VDEV_START_SUPPORT,
7220 		     ar->ab->wmi_ab.svc_map)) {
7221 		ret = ath11k_mac_setup_vdev_params_mbssid(arvif,
7222 							  &arg.mbssid_flags,
7223 							  &arg.mbssid_tx_vdev_id);
7224 		if (ret)
7225 			return ret;
7226 	}
7227 
7228 	if (arvif->vdev_type == WMI_VDEV_TYPE_AP) {
7229 		arg.ssid = arvif->u.ap.ssid;
7230 		arg.ssid_len = arvif->u.ap.ssid_len;
7231 		arg.hidden_ssid = arvif->u.ap.hidden_ssid;
7232 
7233 		/* For now allow DFS for AP mode */
7234 		arg.channel.chan_radar =
7235 			!!(chandef->chan->flags & IEEE80211_CHAN_RADAR);
7236 
7237 		arg.channel.freq2_radar = ctx->radar_enabled;
7238 
7239 		arg.channel.passive = arg.channel.chan_radar;
7240 
7241 		spin_lock_bh(&ab->base_lock);
7242 		arg.regdomain = ar->ab->dfs_region;
7243 		spin_unlock_bh(&ab->base_lock);
7244 	}
7245 
7246 	arg.channel.passive |= !!(chandef->chan->flags & IEEE80211_CHAN_NO_IR);
7247 
7248 	ath11k_dbg(ab, ATH11K_DBG_MAC,
7249 		   "vdev %d start center_freq %d phymode %s\n",
7250 		   arg.vdev_id, arg.channel.freq,
7251 		   ath11k_wmi_phymode_str(arg.channel.mode));
7252 
7253 	ret = ath11k_wmi_vdev_start(ar, &arg, restart);
7254 	if (ret) {
7255 		ath11k_warn(ar->ab, "failed to %s WMI vdev %i\n",
7256 			    restart ? "restart" : "start", arg.vdev_id);
7257 		return ret;
7258 	}
7259 
7260 	ret = ath11k_mac_vdev_setup_sync(ar);
7261 	if (ret) {
7262 		ath11k_warn(ab, "failed to synchronize setup for vdev %i %s: %d\n",
7263 			    arg.vdev_id, restart ? "restart" : "start", ret);
7264 		return ret;
7265 	}
7266 
7267 	if (!restart)
7268 		ar->num_started_vdevs++;
7269 
7270 	ath11k_dbg(ab, ATH11K_DBG_MAC,  "vdev %pM started, vdev_id %d\n",
7271 		   arvif->vif->addr, arvif->vdev_id);
7272 
7273 	/* Enable CAC Flag in the driver by checking the channel DFS cac time,
7274 	 * i.e dfs_cac_ms value which will be valid only for radar channels
7275 	 * and state as NL80211_DFS_USABLE which indicates CAC needs to be
7276 	 * done before channel usage. This flags is used to drop rx packets.
7277 	 * during CAC.
7278 	 */
7279 	/* TODO Set the flag for other interface types as required */
7280 	if (arvif->vdev_type == WMI_VDEV_TYPE_AP &&
7281 	    chandef->chan->dfs_cac_ms &&
7282 	    chandef->chan->dfs_state == NL80211_DFS_USABLE) {
7283 		set_bit(ATH11K_CAC_RUNNING, &ar->dev_flags);
7284 		ath11k_dbg(ab, ATH11K_DBG_MAC,
7285 			   "CAC Started in chan_freq %d for vdev %d\n",
7286 			   arg.channel.freq, arg.vdev_id);
7287 	}
7288 
7289 	ret = ath11k_mac_set_txbf_conf(arvif);
7290 	if (ret)
7291 		ath11k_warn(ab, "failed to set txbf conf for vdev %d: %d\n",
7292 			    arvif->vdev_id, ret);
7293 
7294 	return 0;
7295 }
7296 
ath11k_mac_vdev_stop(struct ath11k_vif * arvif)7297 static int ath11k_mac_vdev_stop(struct ath11k_vif *arvif)
7298 {
7299 	struct ath11k *ar = arvif->ar;
7300 	int ret;
7301 
7302 	lockdep_assert_held(&ar->conf_mutex);
7303 
7304 	reinit_completion(&ar->vdev_setup_done);
7305 
7306 	ret = ath11k_wmi_vdev_stop(ar, arvif->vdev_id);
7307 	if (ret) {
7308 		ath11k_warn(ar->ab, "failed to stop WMI vdev %i: %d\n",
7309 			    arvif->vdev_id, ret);
7310 		goto err;
7311 	}
7312 
7313 	ret = ath11k_mac_vdev_setup_sync(ar);
7314 	if (ret) {
7315 		ath11k_warn(ar->ab, "failed to synchronize setup for vdev %i: %d\n",
7316 			    arvif->vdev_id, ret);
7317 		goto err;
7318 	}
7319 
7320 	WARN_ON(ar->num_started_vdevs == 0);
7321 
7322 	ar->num_started_vdevs--;
7323 	ath11k_dbg(ar->ab, ATH11K_DBG_MAC, "vdev %pM stopped, vdev_id %d\n",
7324 		   arvif->vif->addr, arvif->vdev_id);
7325 
7326 	if (test_bit(ATH11K_CAC_RUNNING, &ar->dev_flags)) {
7327 		clear_bit(ATH11K_CAC_RUNNING, &ar->dev_flags);
7328 		ath11k_dbg(ar->ab, ATH11K_DBG_MAC, "CAC Stopped for vdev %d\n",
7329 			   arvif->vdev_id);
7330 	}
7331 
7332 	return 0;
7333 err:
7334 	return ret;
7335 }
7336 
ath11k_mac_vdev_start(struct ath11k_vif * arvif,struct ieee80211_chanctx_conf * ctx)7337 static int ath11k_mac_vdev_start(struct ath11k_vif *arvif,
7338 				 struct ieee80211_chanctx_conf *ctx)
7339 {
7340 	return ath11k_mac_vdev_start_restart(arvif, ctx, false);
7341 }
7342 
ath11k_mac_vdev_restart(struct ath11k_vif * arvif,struct ieee80211_chanctx_conf * ctx)7343 static int ath11k_mac_vdev_restart(struct ath11k_vif *arvif,
7344 				   struct ieee80211_chanctx_conf *ctx)
7345 {
7346 	return ath11k_mac_vdev_start_restart(arvif, ctx, true);
7347 }
7348 
7349 struct ath11k_mac_change_chanctx_arg {
7350 	struct ieee80211_chanctx_conf *ctx;
7351 	struct ieee80211_vif_chanctx_switch *vifs;
7352 	int n_vifs;
7353 	int next_vif;
7354 };
7355 
7356 static void
ath11k_mac_change_chanctx_cnt_iter(void * data,u8 * mac,struct ieee80211_vif * vif)7357 ath11k_mac_change_chanctx_cnt_iter(void *data, u8 *mac,
7358 				   struct ieee80211_vif *vif)
7359 {
7360 	struct ath11k_mac_change_chanctx_arg *arg = data;
7361 
7362 	if (rcu_access_pointer(vif->bss_conf.chanctx_conf) != arg->ctx)
7363 		return;
7364 
7365 	arg->n_vifs++;
7366 }
7367 
7368 static void
ath11k_mac_change_chanctx_fill_iter(void * data,u8 * mac,struct ieee80211_vif * vif)7369 ath11k_mac_change_chanctx_fill_iter(void *data, u8 *mac,
7370 				    struct ieee80211_vif *vif)
7371 {
7372 	struct ath11k_mac_change_chanctx_arg *arg = data;
7373 	struct ieee80211_chanctx_conf *ctx;
7374 
7375 	ctx = rcu_access_pointer(vif->bss_conf.chanctx_conf);
7376 	if (ctx != arg->ctx)
7377 		return;
7378 
7379 	if (WARN_ON(arg->next_vif == arg->n_vifs))
7380 		return;
7381 
7382 	arg->vifs[arg->next_vif].vif = vif;
7383 	arg->vifs[arg->next_vif].old_ctx = ctx;
7384 	arg->vifs[arg->next_vif].new_ctx = ctx;
7385 	arg->next_vif++;
7386 }
7387 
7388 static void
ath11k_mac_update_vif_chan(struct ath11k * ar,struct ieee80211_vif_chanctx_switch * vifs,int n_vifs)7389 ath11k_mac_update_vif_chan(struct ath11k *ar,
7390 			   struct ieee80211_vif_chanctx_switch *vifs,
7391 			   int n_vifs)
7392 {
7393 	struct ath11k_base *ab = ar->ab;
7394 	struct ath11k_vif *arvif, *tx_arvif = NULL;
7395 	struct ieee80211_vif *mbssid_tx_vif;
7396 	int ret;
7397 	int i;
7398 	bool monitor_vif = false;
7399 
7400 	lockdep_assert_held(&ar->conf_mutex);
7401 
7402 	/* Associated channel resources of all relevant vdevs
7403 	 * should be available for the channel switch now.
7404 	 */
7405 
7406 	/* TODO: Update ar->rx_channel */
7407 
7408 	for (i = 0; i < n_vifs; i++) {
7409 		arvif = ath11k_vif_to_arvif(vifs[i].vif);
7410 
7411 		if (WARN_ON(!arvif->is_started))
7412 			continue;
7413 
7414 		/* change_chanctx can be called even before vdev_up from
7415 		 * ieee80211_start_ap->ieee80211_vif_use_channel->
7416 		 * ieee80211_recalc_radar_chanctx.
7417 		 *
7418 		 * Firmware expect vdev_restart only if vdev is up.
7419 		 * If vdev is down then it expect vdev_stop->vdev_start.
7420 		 */
7421 		if (arvif->is_up) {
7422 			ret = ath11k_mac_vdev_restart(arvif, vifs[i].new_ctx);
7423 			if (ret) {
7424 				ath11k_warn(ab, "failed to restart vdev %d: %d\n",
7425 					    arvif->vdev_id, ret);
7426 				continue;
7427 			}
7428 		} else {
7429 			ret = ath11k_mac_vdev_stop(arvif);
7430 			if (ret) {
7431 				ath11k_warn(ab, "failed to stop vdev %d: %d\n",
7432 					    arvif->vdev_id, ret);
7433 				continue;
7434 			}
7435 
7436 			ret = ath11k_mac_vdev_start(arvif, vifs[i].new_ctx);
7437 			if (ret)
7438 				ath11k_warn(ab, "failed to start vdev %d: %d\n",
7439 					    arvif->vdev_id, ret);
7440 
7441 			continue;
7442 		}
7443 
7444 		ret = ath11k_mac_setup_bcn_tmpl(arvif);
7445 		if (ret)
7446 			ath11k_warn(ab, "failed to update bcn tmpl during csa: %d\n",
7447 				    ret);
7448 
7449 		mbssid_tx_vif = arvif->vif->mbssid_tx_vif;
7450 		if (mbssid_tx_vif)
7451 			tx_arvif = ath11k_vif_to_arvif(mbssid_tx_vif);
7452 
7453 		ret = ath11k_wmi_vdev_up(arvif->ar, arvif->vdev_id, arvif->aid,
7454 					 arvif->bssid,
7455 					 tx_arvif ? tx_arvif->bssid : NULL,
7456 					 arvif->vif->bss_conf.bssid_index,
7457 					 1 << arvif->vif->bss_conf.bssid_indicator);
7458 		if (ret) {
7459 			ath11k_warn(ab, "failed to bring vdev up %d: %d\n",
7460 				    arvif->vdev_id, ret);
7461 			continue;
7462 		}
7463 	}
7464 
7465 	/* Restart the internal monitor vdev on new channel */
7466 	if (!monitor_vif &&
7467 	    test_bit(ATH11K_FLAG_MONITOR_VDEV_CREATED, &ar->monitor_flags)) {
7468 		ret = ath11k_mac_monitor_stop(ar);
7469 		if (ret) {
7470 			ath11k_warn(ar->ab, "failed to stop monitor during vif channel update: %d",
7471 				    ret);
7472 			return;
7473 		}
7474 
7475 		ret = ath11k_mac_monitor_start(ar);
7476 		if (ret) {
7477 			ath11k_warn(ar->ab, "failed to start monitor during vif channel update: %d",
7478 				    ret);
7479 			return;
7480 		}
7481 	}
7482 }
7483 
7484 static void
ath11k_mac_update_active_vif_chan(struct ath11k * ar,struct ieee80211_chanctx_conf * ctx)7485 ath11k_mac_update_active_vif_chan(struct ath11k *ar,
7486 				  struct ieee80211_chanctx_conf *ctx)
7487 {
7488 	struct ath11k_mac_change_chanctx_arg arg = { .ctx = ctx };
7489 
7490 	lockdep_assert_held(&ar->conf_mutex);
7491 
7492 	ieee80211_iterate_active_interfaces_atomic(ar->hw,
7493 						   IEEE80211_IFACE_ITER_NORMAL,
7494 						   ath11k_mac_change_chanctx_cnt_iter,
7495 						   &arg);
7496 	if (arg.n_vifs == 0)
7497 		return;
7498 
7499 	arg.vifs = kcalloc(arg.n_vifs, sizeof(arg.vifs[0]), GFP_KERNEL);
7500 	if (!arg.vifs)
7501 		return;
7502 
7503 	ieee80211_iterate_active_interfaces_atomic(ar->hw,
7504 						   IEEE80211_IFACE_ITER_NORMAL,
7505 						   ath11k_mac_change_chanctx_fill_iter,
7506 						   &arg);
7507 
7508 	ath11k_mac_update_vif_chan(ar, arg.vifs, arg.n_vifs);
7509 
7510 	kfree(arg.vifs);
7511 }
7512 
ath11k_mac_op_change_chanctx(struct ieee80211_hw * hw,struct ieee80211_chanctx_conf * ctx,u32 changed)7513 static void ath11k_mac_op_change_chanctx(struct ieee80211_hw *hw,
7514 					 struct ieee80211_chanctx_conf *ctx,
7515 					 u32 changed)
7516 {
7517 	struct ath11k *ar = hw->priv;
7518 	struct ath11k_base *ab = ar->ab;
7519 
7520 	mutex_lock(&ar->conf_mutex);
7521 
7522 	ath11k_dbg(ab, ATH11K_DBG_MAC,
7523 		   "chanctx change freq %u width %d ptr %p changed %x\n",
7524 		   ctx->def.chan->center_freq, ctx->def.width, ctx, changed);
7525 
7526 	/* This shouldn't really happen because channel switching should use
7527 	 * switch_vif_chanctx().
7528 	 */
7529 	if (WARN_ON(changed & IEEE80211_CHANCTX_CHANGE_CHANNEL))
7530 		goto unlock;
7531 
7532 	if (changed & IEEE80211_CHANCTX_CHANGE_WIDTH ||
7533 	    changed & IEEE80211_CHANCTX_CHANGE_RADAR)
7534 		ath11k_mac_update_active_vif_chan(ar, ctx);
7535 
7536 	/* TODO: Recalc radar detection */
7537 
7538 unlock:
7539 	mutex_unlock(&ar->conf_mutex);
7540 }
7541 
ath11k_start_vdev_delay(struct ieee80211_hw * hw,struct ieee80211_vif * vif)7542 static int ath11k_start_vdev_delay(struct ieee80211_hw *hw,
7543 				   struct ieee80211_vif *vif)
7544 {
7545 	struct ath11k *ar = hw->priv;
7546 	struct ath11k_base *ab = ar->ab;
7547 	struct ath11k_vif *arvif = ath11k_vif_to_arvif(vif);
7548 	int ret;
7549 
7550 	if (WARN_ON(arvif->is_started))
7551 		return -EBUSY;
7552 
7553 	ret = ath11k_mac_vdev_start(arvif, &arvif->chanctx);
7554 	if (ret) {
7555 		ath11k_warn(ab, "failed to start vdev %i addr %pM on freq %d: %d\n",
7556 			    arvif->vdev_id, vif->addr,
7557 			    arvif->chanctx.def.chan->center_freq, ret);
7558 		return ret;
7559 	}
7560 
7561 	/* Reconfigure hardware rate code since it is cleared by firmware.
7562 	 */
7563 	if (ar->hw_rate_code > 0) {
7564 		u32 vdev_param = WMI_VDEV_PARAM_MGMT_RATE;
7565 
7566 		ret = ath11k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id, vdev_param,
7567 						    ar->hw_rate_code);
7568 		if (ret) {
7569 			ath11k_warn(ar->ab, "failed to set mgmt tx rate %d\n", ret);
7570 			return ret;
7571 		}
7572 	}
7573 
7574 	if (arvif->vdev_type == WMI_VDEV_TYPE_MONITOR) {
7575 		ret = ath11k_wmi_vdev_up(ar, arvif->vdev_id, 0, ar->mac_addr,
7576 					 NULL, 0, 0);
7577 		if (ret) {
7578 			ath11k_warn(ab, "failed put monitor up: %d\n", ret);
7579 			return ret;
7580 		}
7581 	}
7582 
7583 	arvif->is_started = true;
7584 
7585 	/* TODO: Setup ps and cts/rts protection */
7586 	return 0;
7587 }
7588 
7589 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)7590 ath11k_mac_op_assign_vif_chanctx(struct ieee80211_hw *hw,
7591 				 struct ieee80211_vif *vif,
7592 				 struct ieee80211_bss_conf *link_conf,
7593 				 struct ieee80211_chanctx_conf *ctx)
7594 {
7595 	struct ath11k *ar = hw->priv;
7596 	struct ath11k_base *ab = ar->ab;
7597 	struct ath11k_vif *arvif = ath11k_vif_to_arvif(vif);
7598 	int ret;
7599 	struct peer_create_params param;
7600 
7601 	mutex_lock(&ar->conf_mutex);
7602 
7603 	ath11k_dbg(ab, ATH11K_DBG_MAC,
7604 		   "chanctx assign ptr %p vdev_id %i\n",
7605 		   ctx, arvif->vdev_id);
7606 
7607 	/* for QCA6390 bss peer must be created before vdev_start */
7608 	if (ab->hw_params.vdev_start_delay &&
7609 	    arvif->vdev_type != WMI_VDEV_TYPE_AP &&
7610 	    arvif->vdev_type != WMI_VDEV_TYPE_MONITOR &&
7611 	    !ath11k_peer_find_by_vdev_id(ab, arvif->vdev_id)) {
7612 		memcpy(&arvif->chanctx, ctx, sizeof(*ctx));
7613 		ret = 0;
7614 		goto out;
7615 	}
7616 
7617 	if (WARN_ON(arvif->is_started)) {
7618 		ret = -EBUSY;
7619 		goto out;
7620 	}
7621 
7622 	if (ab->hw_params.vdev_start_delay &&
7623 	    arvif->vdev_type != WMI_VDEV_TYPE_AP &&
7624 	    arvif->vdev_type != WMI_VDEV_TYPE_MONITOR) {
7625 		param.vdev_id = arvif->vdev_id;
7626 		param.peer_type = WMI_PEER_TYPE_DEFAULT;
7627 		param.peer_addr = ar->mac_addr;
7628 
7629 		ret = ath11k_peer_create(ar, arvif, NULL, &param);
7630 		if (ret) {
7631 			ath11k_warn(ab, "failed to create peer after vdev start delay: %d",
7632 				    ret);
7633 			goto out;
7634 		}
7635 	}
7636 
7637 	if (arvif->vdev_type == WMI_VDEV_TYPE_MONITOR) {
7638 		ret = ath11k_mac_monitor_start(ar);
7639 		if (ret) {
7640 			ath11k_warn(ar->ab, "failed to start monitor during vif channel context assignment: %d",
7641 				    ret);
7642 			goto out;
7643 		}
7644 
7645 		arvif->is_started = true;
7646 		goto out;
7647 	}
7648 
7649 	ret = ath11k_mac_vdev_start(arvif, ctx);
7650 	if (ret) {
7651 		ath11k_warn(ab, "failed to start vdev %i addr %pM on freq %d: %d\n",
7652 			    arvif->vdev_id, vif->addr,
7653 			    ctx->def.chan->center_freq, ret);
7654 		goto out;
7655 	}
7656 
7657 	arvif->is_started = true;
7658 
7659 	if (arvif->vdev_type != WMI_VDEV_TYPE_MONITOR &&
7660 	    test_bit(ATH11K_FLAG_MONITOR_VDEV_CREATED, &ar->monitor_flags)) {
7661 		ret = ath11k_mac_monitor_start(ar);
7662 		if (ret) {
7663 			ath11k_warn(ar->ab, "failed to start monitor during vif channel context assignment: %d",
7664 				    ret);
7665 			goto out;
7666 		}
7667 	}
7668 
7669 	/* TODO: Setup ps and cts/rts protection */
7670 
7671 	ret = 0;
7672 
7673 out:
7674 	mutex_unlock(&ar->conf_mutex);
7675 
7676 	return ret;
7677 }
7678 
7679 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)7680 ath11k_mac_op_unassign_vif_chanctx(struct ieee80211_hw *hw,
7681 				   struct ieee80211_vif *vif,
7682 				   struct ieee80211_bss_conf *link_conf,
7683 				   struct ieee80211_chanctx_conf *ctx)
7684 {
7685 	struct ath11k *ar = hw->priv;
7686 	struct ath11k_base *ab = ar->ab;
7687 	struct ath11k_vif *arvif = ath11k_vif_to_arvif(vif);
7688 	struct ath11k_peer *peer;
7689 	int ret;
7690 
7691 	mutex_lock(&ar->conf_mutex);
7692 
7693 	ath11k_dbg(ab, ATH11K_DBG_MAC,
7694 		   "chanctx unassign ptr %p vdev_id %i\n",
7695 		   ctx, arvif->vdev_id);
7696 
7697 	WARN_ON(!arvif->is_started);
7698 
7699 	if (ab->hw_params.vdev_start_delay &&
7700 	    arvif->vdev_type == WMI_VDEV_TYPE_MONITOR) {
7701 		spin_lock_bh(&ab->base_lock);
7702 		peer = ath11k_peer_find_by_addr(ab, ar->mac_addr);
7703 		spin_unlock_bh(&ab->base_lock);
7704 		if (peer)
7705 			ath11k_peer_delete(ar, arvif->vdev_id, ar->mac_addr);
7706 	}
7707 
7708 	if (arvif->vdev_type == WMI_VDEV_TYPE_MONITOR) {
7709 		ret = ath11k_mac_monitor_stop(ar);
7710 		if (ret) {
7711 			ath11k_warn(ar->ab, "failed to stop monitor during vif channel context unassignment: %d",
7712 				    ret);
7713 			mutex_unlock(&ar->conf_mutex);
7714 			return;
7715 		}
7716 
7717 		arvif->is_started = false;
7718 		mutex_unlock(&ar->conf_mutex);
7719 		return;
7720 	}
7721 
7722 	ret = ath11k_mac_vdev_stop(arvif);
7723 	if (ret)
7724 		ath11k_warn(ab, "failed to stop vdev %i: %d\n",
7725 			    arvif->vdev_id, ret);
7726 
7727 	arvif->is_started = false;
7728 
7729 	if (ab->hw_params.vdev_start_delay &&
7730 	    arvif->vdev_type == WMI_VDEV_TYPE_STA) {
7731 		ret = ath11k_peer_delete(ar, arvif->vdev_id, arvif->bssid);
7732 		if (ret)
7733 			ath11k_warn(ar->ab,
7734 				    "failed to delete peer %pM for vdev %d: %d\n",
7735 				    arvif->bssid, arvif->vdev_id, ret);
7736 		else
7737 			ath11k_dbg(ar->ab, ATH11K_DBG_MAC,
7738 				   "removed peer %pM  vdev %d after vdev stop\n",
7739 				   arvif->bssid, arvif->vdev_id);
7740 	}
7741 
7742 	if (ab->hw_params.vdev_start_delay &&
7743 	    arvif->vdev_type == WMI_VDEV_TYPE_MONITOR)
7744 		ath11k_wmi_vdev_down(ar, arvif->vdev_id);
7745 
7746 	if (arvif->vdev_type != WMI_VDEV_TYPE_MONITOR &&
7747 	    ar->num_started_vdevs == 1 &&
7748 	    test_bit(ATH11K_FLAG_MONITOR_VDEV_CREATED, &ar->monitor_flags)) {
7749 		ret = ath11k_mac_monitor_stop(ar);
7750 		if (ret)
7751 			/* continue even if there's an error */
7752 			ath11k_warn(ar->ab, "failed to stop monitor during vif channel context unassignment: %d",
7753 				    ret);
7754 	}
7755 
7756 	if (arvif->vdev_type == WMI_VDEV_TYPE_STA)
7757 		ath11k_mac_11d_scan_start(ar, arvif->vdev_id);
7758 
7759 	mutex_unlock(&ar->conf_mutex);
7760 }
7761 
7762 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)7763 ath11k_mac_op_switch_vif_chanctx(struct ieee80211_hw *hw,
7764 				 struct ieee80211_vif_chanctx_switch *vifs,
7765 				 int n_vifs,
7766 				 enum ieee80211_chanctx_switch_mode mode)
7767 {
7768 	struct ath11k *ar = hw->priv;
7769 
7770 	mutex_lock(&ar->conf_mutex);
7771 
7772 	ath11k_dbg(ar->ab, ATH11K_DBG_MAC,
7773 		   "chanctx switch n_vifs %d mode %d\n",
7774 		   n_vifs, mode);
7775 	ath11k_mac_update_vif_chan(ar, vifs, n_vifs);
7776 
7777 	mutex_unlock(&ar->conf_mutex);
7778 
7779 	return 0;
7780 }
7781 
7782 static int
ath11k_set_vdev_param_to_all_vifs(struct ath11k * ar,int param,u32 value)7783 ath11k_set_vdev_param_to_all_vifs(struct ath11k *ar, int param, u32 value)
7784 {
7785 	struct ath11k_vif *arvif;
7786 	int ret = 0;
7787 
7788 	mutex_lock(&ar->conf_mutex);
7789 	list_for_each_entry(arvif, &ar->arvifs, list) {
7790 		ath11k_dbg(ar->ab, ATH11K_DBG_MAC, "setting mac vdev %d param %d value %d\n",
7791 			   param, arvif->vdev_id, value);
7792 
7793 		ret = ath11k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
7794 						    param, value);
7795 		if (ret) {
7796 			ath11k_warn(ar->ab, "failed to set param %d for vdev %d: %d\n",
7797 				    param, arvif->vdev_id, ret);
7798 			break;
7799 		}
7800 	}
7801 	mutex_unlock(&ar->conf_mutex);
7802 	return ret;
7803 }
7804 
7805 /* mac80211 stores device specific RTS/Fragmentation threshold value,
7806  * this is set interface specific to firmware from ath11k driver
7807  */
ath11k_mac_op_set_rts_threshold(struct ieee80211_hw * hw,u32 value)7808 static int ath11k_mac_op_set_rts_threshold(struct ieee80211_hw *hw, u32 value)
7809 {
7810 	struct ath11k *ar = hw->priv;
7811 	int param_id = WMI_VDEV_PARAM_RTS_THRESHOLD;
7812 
7813 	return ath11k_set_vdev_param_to_all_vifs(ar, param_id, value);
7814 }
7815 
ath11k_mac_op_set_frag_threshold(struct ieee80211_hw * hw,u32 value)7816 static int ath11k_mac_op_set_frag_threshold(struct ieee80211_hw *hw, u32 value)
7817 {
7818 	/* Even though there's a WMI vdev param for fragmentation threshold no
7819 	 * known firmware actually implements it. Moreover it is not possible to
7820 	 * rely frame fragmentation to mac80211 because firmware clears the
7821 	 * "more fragments" bit in frame control making it impossible for remote
7822 	 * devices to reassemble frames.
7823 	 *
7824 	 * Hence implement a dummy callback just to say fragmentation isn't
7825 	 * supported. This effectively prevents mac80211 from doing frame
7826 	 * fragmentation in software.
7827 	 */
7828 	return -EOPNOTSUPP;
7829 }
7830 
ath11k_mac_flush_tx_complete(struct ath11k * ar)7831 static int ath11k_mac_flush_tx_complete(struct ath11k *ar)
7832 {
7833 	long time_left;
7834 	int ret = 0;
7835 
7836 	time_left = wait_event_timeout(ar->dp.tx_empty_waitq,
7837 				       (atomic_read(&ar->dp.num_tx_pending) == 0),
7838 				       ATH11K_FLUSH_TIMEOUT);
7839 	if (time_left == 0) {
7840 		ath11k_warn(ar->ab, "failed to flush transmit queue, data pkts pending %d\n",
7841 			    atomic_read(&ar->dp.num_tx_pending));
7842 		ret = -ETIMEDOUT;
7843 	}
7844 
7845 	time_left = wait_event_timeout(ar->txmgmt_empty_waitq,
7846 				       (atomic_read(&ar->num_pending_mgmt_tx) == 0),
7847 				       ATH11K_FLUSH_TIMEOUT);
7848 	if (time_left == 0) {
7849 		ath11k_warn(ar->ab, "failed to flush mgmt transmit queue, mgmt pkts pending %d\n",
7850 			    atomic_read(&ar->num_pending_mgmt_tx));
7851 		ret = -ETIMEDOUT;
7852 	}
7853 
7854 	return ret;
7855 }
7856 
ath11k_mac_wait_tx_complete(struct ath11k * ar)7857 int ath11k_mac_wait_tx_complete(struct ath11k *ar)
7858 {
7859 	ath11k_mac_drain_tx(ar);
7860 	return ath11k_mac_flush_tx_complete(ar);
7861 }
7862 
ath11k_mac_op_flush(struct ieee80211_hw * hw,struct ieee80211_vif * vif,u32 queues,bool drop)7863 static void ath11k_mac_op_flush(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
7864 				u32 queues, bool drop)
7865 {
7866 	struct ath11k *ar = hw->priv;
7867 
7868 	if (drop)
7869 		return;
7870 
7871 	ath11k_mac_flush_tx_complete(ar);
7872 }
7873 
7874 static bool
ath11k_mac_has_single_legacy_rate(struct ath11k * ar,enum nl80211_band band,const struct cfg80211_bitrate_mask * mask)7875 ath11k_mac_has_single_legacy_rate(struct ath11k *ar,
7876 				  enum nl80211_band band,
7877 				  const struct cfg80211_bitrate_mask *mask)
7878 {
7879 	int num_rates = 0;
7880 
7881 	num_rates = hweight32(mask->control[band].legacy);
7882 
7883 	if (ath11k_mac_bitrate_mask_num_ht_rates(ar, band, mask))
7884 		return false;
7885 
7886 	if (ath11k_mac_bitrate_mask_num_vht_rates(ar, band, mask))
7887 		return false;
7888 
7889 	if (ath11k_mac_bitrate_mask_num_he_rates(ar, band, mask))
7890 		return false;
7891 
7892 	return num_rates == 1;
7893 }
7894 
7895 static __le16
ath11k_mac_get_tx_mcs_map(const struct ieee80211_sta_he_cap * he_cap)7896 ath11k_mac_get_tx_mcs_map(const struct ieee80211_sta_he_cap *he_cap)
7897 {
7898 	if (he_cap->he_cap_elem.phy_cap_info[0] &
7899 	    IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_80PLUS80_MHZ_IN_5G)
7900 		return he_cap->he_mcs_nss_supp.tx_mcs_80p80;
7901 
7902 	if (he_cap->he_cap_elem.phy_cap_info[0] &
7903 	    IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_160MHZ_IN_5G)
7904 		return he_cap->he_mcs_nss_supp.tx_mcs_160;
7905 
7906 	return he_cap->he_mcs_nss_supp.tx_mcs_80;
7907 }
7908 
7909 static bool
ath11k_mac_bitrate_mask_get_single_nss(struct ath11k * ar,enum nl80211_band band,const struct cfg80211_bitrate_mask * mask,int * nss)7910 ath11k_mac_bitrate_mask_get_single_nss(struct ath11k *ar,
7911 				       enum nl80211_band band,
7912 				       const struct cfg80211_bitrate_mask *mask,
7913 				       int *nss)
7914 {
7915 	struct ieee80211_supported_band *sband = &ar->mac.sbands[band];
7916 	u16 vht_mcs_map = le16_to_cpu(sband->vht_cap.vht_mcs.tx_mcs_map);
7917 	u16 he_mcs_map = 0;
7918 	u8 ht_nss_mask = 0;
7919 	u8 vht_nss_mask = 0;
7920 	u8 he_nss_mask = 0;
7921 	int i;
7922 
7923 	/* No need to consider legacy here. Basic rates are always present
7924 	 * in bitrate mask
7925 	 */
7926 
7927 	for (i = 0; i < ARRAY_SIZE(mask->control[band].ht_mcs); i++) {
7928 		if (mask->control[band].ht_mcs[i] == 0)
7929 			continue;
7930 		else if (mask->control[band].ht_mcs[i] ==
7931 			 sband->ht_cap.mcs.rx_mask[i])
7932 			ht_nss_mask |= BIT(i);
7933 		else
7934 			return false;
7935 	}
7936 
7937 	for (i = 0; i < ARRAY_SIZE(mask->control[band].vht_mcs); i++) {
7938 		if (mask->control[band].vht_mcs[i] == 0)
7939 			continue;
7940 		else if (mask->control[band].vht_mcs[i] ==
7941 			 ath11k_mac_get_max_vht_mcs_map(vht_mcs_map, i))
7942 			vht_nss_mask |= BIT(i);
7943 		else
7944 			return false;
7945 	}
7946 
7947 	he_mcs_map = le16_to_cpu(ath11k_mac_get_tx_mcs_map(&sband->iftype_data->he_cap));
7948 
7949 	for (i = 0; i < ARRAY_SIZE(mask->control[band].he_mcs); i++) {
7950 		if (mask->control[band].he_mcs[i] == 0)
7951 			continue;
7952 
7953 		if (mask->control[band].he_mcs[i] ==
7954 		    ath11k_mac_get_max_he_mcs_map(he_mcs_map, i))
7955 			he_nss_mask |= BIT(i);
7956 		else
7957 			return false;
7958 	}
7959 
7960 	if (ht_nss_mask != vht_nss_mask || ht_nss_mask != he_nss_mask)
7961 		return false;
7962 
7963 	if (ht_nss_mask == 0)
7964 		return false;
7965 
7966 	if (BIT(fls(ht_nss_mask)) - 1 != ht_nss_mask)
7967 		return false;
7968 
7969 	*nss = fls(ht_nss_mask);
7970 
7971 	return true;
7972 }
7973 
7974 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)7975 ath11k_mac_get_single_legacy_rate(struct ath11k *ar,
7976 				  enum nl80211_band band,
7977 				  const struct cfg80211_bitrate_mask *mask,
7978 				  u32 *rate, u8 *nss)
7979 {
7980 	int rate_idx;
7981 	u16 bitrate;
7982 	u8 preamble;
7983 	u8 hw_rate;
7984 
7985 	if (hweight32(mask->control[band].legacy) != 1)
7986 		return -EINVAL;
7987 
7988 	rate_idx = ffs(mask->control[band].legacy) - 1;
7989 
7990 	if (band == NL80211_BAND_5GHZ || band == NL80211_BAND_6GHZ)
7991 		rate_idx += ATH11K_MAC_FIRST_OFDM_RATE_IDX;
7992 
7993 	hw_rate = ath11k_legacy_rates[rate_idx].hw_value;
7994 	bitrate = ath11k_legacy_rates[rate_idx].bitrate;
7995 
7996 	if (ath11k_mac_bitrate_is_cck(bitrate))
7997 		preamble = WMI_RATE_PREAMBLE_CCK;
7998 	else
7999 		preamble = WMI_RATE_PREAMBLE_OFDM;
8000 
8001 	*nss = 1;
8002 	*rate = ATH11K_HW_RATE_CODE(hw_rate, 0, preamble);
8003 
8004 	return 0;
8005 }
8006 
8007 static int
ath11k_mac_set_fixed_rate_gi_ltf(struct ath11k_vif * arvif,u8 he_gi,u8 he_ltf)8008 ath11k_mac_set_fixed_rate_gi_ltf(struct ath11k_vif *arvif, u8 he_gi, u8 he_ltf)
8009 {
8010 	struct ath11k *ar = arvif->ar;
8011 	int ret;
8012 
8013 	/* 0.8 = 0, 1.6 = 2 and 3.2 = 3. */
8014 	if (he_gi && he_gi != 0xFF)
8015 		he_gi += 1;
8016 
8017 	ret = ath11k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
8018 					    WMI_VDEV_PARAM_SGI, he_gi);
8019 	if (ret) {
8020 		ath11k_warn(ar->ab, "failed to set he gi %d: %d\n",
8021 			    he_gi, ret);
8022 		return ret;
8023 	}
8024 	/* start from 1 */
8025 	if (he_ltf != 0xFF)
8026 		he_ltf += 1;
8027 
8028 	ret = ath11k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
8029 					    WMI_VDEV_PARAM_HE_LTF, he_ltf);
8030 	if (ret) {
8031 		ath11k_warn(ar->ab, "failed to set he ltf %d: %d\n",
8032 			    he_ltf, ret);
8033 		return ret;
8034 	}
8035 
8036 	return 0;
8037 }
8038 
8039 static int
ath11k_mac_set_auto_rate_gi_ltf(struct ath11k_vif * arvif,u16 he_gi,u8 he_ltf)8040 ath11k_mac_set_auto_rate_gi_ltf(struct ath11k_vif *arvif, u16 he_gi, u8 he_ltf)
8041 {
8042 	struct ath11k *ar = arvif->ar;
8043 	int ret;
8044 	u32 he_ar_gi_ltf;
8045 
8046 	if (he_gi != 0xFF) {
8047 		switch (he_gi) {
8048 		case NL80211_RATE_INFO_HE_GI_0_8:
8049 			he_gi = WMI_AUTORATE_800NS_GI;
8050 			break;
8051 		case NL80211_RATE_INFO_HE_GI_1_6:
8052 			he_gi = WMI_AUTORATE_1600NS_GI;
8053 			break;
8054 		case NL80211_RATE_INFO_HE_GI_3_2:
8055 			he_gi = WMI_AUTORATE_3200NS_GI;
8056 			break;
8057 		default:
8058 			ath11k_warn(ar->ab, "invalid he gi: %d\n", he_gi);
8059 			return -EINVAL;
8060 		}
8061 	}
8062 
8063 	if (he_ltf != 0xFF) {
8064 		switch (he_ltf) {
8065 		case NL80211_RATE_INFO_HE_1XLTF:
8066 			he_ltf = WMI_HE_AUTORATE_LTF_1X;
8067 			break;
8068 		case NL80211_RATE_INFO_HE_2XLTF:
8069 			he_ltf = WMI_HE_AUTORATE_LTF_2X;
8070 			break;
8071 		case NL80211_RATE_INFO_HE_4XLTF:
8072 			he_ltf = WMI_HE_AUTORATE_LTF_4X;
8073 			break;
8074 		default:
8075 			ath11k_warn(ar->ab, "invalid he ltf: %d\n", he_ltf);
8076 			return -EINVAL;
8077 		}
8078 	}
8079 
8080 	he_ar_gi_ltf = he_gi | he_ltf;
8081 	ret = ath11k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
8082 					    WMI_VDEV_PARAM_AUTORATE_MISC_CFG,
8083 					    he_ar_gi_ltf);
8084 	if (ret) {
8085 		ath11k_warn(ar->ab,
8086 			    "failed to set he autorate gi %u ltf %u: %d\n",
8087 			    he_gi, he_ltf, ret);
8088 		return ret;
8089 	}
8090 
8091 	return 0;
8092 }
8093 
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)8094 static int ath11k_mac_set_rate_params(struct ath11k_vif *arvif,
8095 				      u32 rate, u8 nss, u8 sgi, u8 ldpc,
8096 				      u8 he_gi, u8 he_ltf, bool he_fixed_rate)
8097 {
8098 	struct ath11k *ar = arvif->ar;
8099 	u32 vdev_param;
8100 	int ret;
8101 
8102 	lockdep_assert_held(&ar->conf_mutex);
8103 
8104 	ath11k_dbg(ar->ab, ATH11K_DBG_MAC,
8105 		   "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",
8106 		   arvif->vdev_id, rate, nss, sgi, ldpc, he_gi,
8107 		   he_ltf, he_fixed_rate);
8108 
8109 	if (!arvif->vif->bss_conf.he_support) {
8110 		vdev_param = WMI_VDEV_PARAM_FIXED_RATE;
8111 		ret = ath11k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
8112 						    vdev_param, rate);
8113 		if (ret) {
8114 			ath11k_warn(ar->ab, "failed to set fixed rate param 0x%02x: %d\n",
8115 				    rate, ret);
8116 			return ret;
8117 		}
8118 	}
8119 
8120 	vdev_param = WMI_VDEV_PARAM_NSS;
8121 	ret = ath11k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
8122 					    vdev_param, nss);
8123 	if (ret) {
8124 		ath11k_warn(ar->ab, "failed to set nss param %d: %d\n",
8125 			    nss, ret);
8126 		return ret;
8127 	}
8128 
8129 	vdev_param = WMI_VDEV_PARAM_LDPC;
8130 	ret = ath11k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
8131 					    vdev_param, ldpc);
8132 	if (ret) {
8133 		ath11k_warn(ar->ab, "failed to set ldpc param %d: %d\n",
8134 			    ldpc, ret);
8135 		return ret;
8136 	}
8137 
8138 	if (arvif->vif->bss_conf.he_support) {
8139 		if (he_fixed_rate) {
8140 			ret = ath11k_mac_set_fixed_rate_gi_ltf(arvif, he_gi,
8141 							       he_ltf);
8142 			if (ret) {
8143 				ath11k_warn(ar->ab, "failed to set fixed rate gi ltf: %d\n",
8144 					    ret);
8145 				return ret;
8146 			}
8147 		} else {
8148 			ret = ath11k_mac_set_auto_rate_gi_ltf(arvif, he_gi,
8149 							      he_ltf);
8150 			if (ret) {
8151 				ath11k_warn(ar->ab, "failed to set auto rate gi ltf: %d\n",
8152 					    ret);
8153 				return ret;
8154 			}
8155 		}
8156 	} else {
8157 		vdev_param = WMI_VDEV_PARAM_SGI;
8158 		ret = ath11k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
8159 						    vdev_param, sgi);
8160 		if (ret) {
8161 			ath11k_warn(ar->ab, "failed to set sgi param %d: %d\n",
8162 				    sgi, ret);
8163 			return ret;
8164 		}
8165 	}
8166 
8167 	return 0;
8168 }
8169 
8170 static bool
ath11k_mac_vht_mcs_range_present(struct ath11k * ar,enum nl80211_band band,const struct cfg80211_bitrate_mask * mask)8171 ath11k_mac_vht_mcs_range_present(struct ath11k *ar,
8172 				 enum nl80211_band band,
8173 				 const struct cfg80211_bitrate_mask *mask)
8174 {
8175 	int i;
8176 	u16 vht_mcs;
8177 
8178 	for (i = 0; i < NL80211_VHT_NSS_MAX; i++) {
8179 		vht_mcs = mask->control[band].vht_mcs[i];
8180 
8181 		switch (vht_mcs) {
8182 		case 0:
8183 		case BIT(8) - 1:
8184 		case BIT(9) - 1:
8185 		case BIT(10) - 1:
8186 			break;
8187 		default:
8188 			return false;
8189 		}
8190 	}
8191 
8192 	return true;
8193 }
8194 
8195 static bool
ath11k_mac_he_mcs_range_present(struct ath11k * ar,enum nl80211_band band,const struct cfg80211_bitrate_mask * mask)8196 ath11k_mac_he_mcs_range_present(struct ath11k *ar,
8197 				enum nl80211_band band,
8198 				const struct cfg80211_bitrate_mask *mask)
8199 {
8200 	int i;
8201 	u16 he_mcs;
8202 
8203 	for (i = 0; i < NL80211_HE_NSS_MAX; i++) {
8204 		he_mcs = mask->control[band].he_mcs[i];
8205 
8206 		switch (he_mcs) {
8207 		case 0:
8208 		case BIT(8) - 1:
8209 		case BIT(10) - 1:
8210 		case BIT(12) - 1:
8211 			break;
8212 		default:
8213 			return false;
8214 		}
8215 	}
8216 
8217 	return true;
8218 }
8219 
ath11k_mac_set_bitrate_mask_iter(void * data,struct ieee80211_sta * sta)8220 static void ath11k_mac_set_bitrate_mask_iter(void *data,
8221 					     struct ieee80211_sta *sta)
8222 {
8223 	struct ath11k_vif *arvif = data;
8224 	struct ath11k_sta *arsta = (struct ath11k_sta *)sta->drv_priv;
8225 	struct ath11k *ar = arvif->ar;
8226 
8227 	spin_lock_bh(&ar->data_lock);
8228 	arsta->changed |= IEEE80211_RC_SUPP_RATES_CHANGED;
8229 	spin_unlock_bh(&ar->data_lock);
8230 
8231 	ieee80211_queue_work(ar->hw, &arsta->update_wk);
8232 }
8233 
ath11k_mac_disable_peer_fixed_rate(void * data,struct ieee80211_sta * sta)8234 static void ath11k_mac_disable_peer_fixed_rate(void *data,
8235 					       struct ieee80211_sta *sta)
8236 {
8237 	struct ath11k_vif *arvif = data;
8238 	struct ath11k *ar = arvif->ar;
8239 	int ret;
8240 
8241 	ret = ath11k_wmi_set_peer_param(ar, sta->addr,
8242 					arvif->vdev_id,
8243 					WMI_PEER_PARAM_FIXED_RATE,
8244 					WMI_FIXED_RATE_NONE);
8245 	if (ret)
8246 		ath11k_warn(ar->ab,
8247 			    "failed to disable peer fixed rate for STA %pM ret %d\n",
8248 			    sta->addr, ret);
8249 }
8250 
8251 static bool
ath11k_mac_validate_vht_he_fixed_rate_settings(struct ath11k * ar,enum nl80211_band band,const struct cfg80211_bitrate_mask * mask)8252 ath11k_mac_validate_vht_he_fixed_rate_settings(struct ath11k *ar, enum nl80211_band band,
8253 					       const struct cfg80211_bitrate_mask *mask)
8254 {
8255 	bool he_fixed_rate = false, vht_fixed_rate = false;
8256 	struct ath11k_peer *peer;
8257 	const u16 *vht_mcs_mask, *he_mcs_mask;
8258 	struct ieee80211_link_sta *deflink;
8259 	u8 vht_nss, he_nss;
8260 	bool ret = true;
8261 
8262 	vht_mcs_mask = mask->control[band].vht_mcs;
8263 	he_mcs_mask = mask->control[band].he_mcs;
8264 
8265 	if (ath11k_mac_bitrate_mask_num_vht_rates(ar, band, mask) == 1)
8266 		vht_fixed_rate = true;
8267 
8268 	if (ath11k_mac_bitrate_mask_num_he_rates(ar, band, mask) == 1)
8269 		he_fixed_rate = true;
8270 
8271 	if (!vht_fixed_rate && !he_fixed_rate)
8272 		return true;
8273 
8274 	vht_nss = ath11k_mac_max_vht_nss(vht_mcs_mask);
8275 	he_nss =  ath11k_mac_max_he_nss(he_mcs_mask);
8276 
8277 	rcu_read_lock();
8278 	spin_lock_bh(&ar->ab->base_lock);
8279 	list_for_each_entry(peer, &ar->ab->peers, list) {
8280 		if (peer->sta) {
8281 			deflink = &peer->sta->deflink;
8282 
8283 			if (vht_fixed_rate && (!deflink->vht_cap.vht_supported ||
8284 					       deflink->rx_nss < vht_nss)) {
8285 				ret = false;
8286 				goto out;
8287 			}
8288 
8289 			if (he_fixed_rate && (!deflink->he_cap.has_he ||
8290 					      deflink->rx_nss < he_nss)) {
8291 				ret = false;
8292 				goto out;
8293 			}
8294 		}
8295 	}
8296 
8297 out:
8298 	spin_unlock_bh(&ar->ab->base_lock);
8299 	rcu_read_unlock();
8300 	return ret;
8301 }
8302 
8303 static int
ath11k_mac_op_set_bitrate_mask(struct ieee80211_hw * hw,struct ieee80211_vif * vif,const struct cfg80211_bitrate_mask * mask)8304 ath11k_mac_op_set_bitrate_mask(struct ieee80211_hw *hw,
8305 			       struct ieee80211_vif *vif,
8306 			       const struct cfg80211_bitrate_mask *mask)
8307 {
8308 	struct ath11k_vif *arvif = ath11k_vif_to_arvif(vif);
8309 	struct cfg80211_chan_def def;
8310 	struct ath11k_pdev_cap *cap;
8311 	struct ath11k *ar = arvif->ar;
8312 	enum nl80211_band band;
8313 	const u8 *ht_mcs_mask;
8314 	const u16 *vht_mcs_mask;
8315 	const u16 *he_mcs_mask;
8316 	u8 he_ltf = 0;
8317 	u8 he_gi = 0;
8318 	u32 rate;
8319 	u8 nss;
8320 	u8 sgi;
8321 	u8 ldpc;
8322 	int single_nss;
8323 	int ret;
8324 	int num_rates;
8325 	bool he_fixed_rate = false;
8326 
8327 	if (ath11k_mac_vif_chan(vif, &def))
8328 		return -EPERM;
8329 
8330 	band = def.chan->band;
8331 	cap = &ar->pdev->cap;
8332 	ht_mcs_mask = mask->control[band].ht_mcs;
8333 	vht_mcs_mask = mask->control[band].vht_mcs;
8334 	he_mcs_mask = mask->control[band].he_mcs;
8335 	ldpc = !!(cap->band[band].ht_cap_info & WMI_HT_CAP_TX_LDPC);
8336 
8337 	sgi = mask->control[band].gi;
8338 	if (sgi == NL80211_TXRATE_FORCE_LGI)
8339 		return -EINVAL;
8340 
8341 	he_gi = mask->control[band].he_gi;
8342 	he_ltf = mask->control[band].he_ltf;
8343 
8344 	/* mac80211 doesn't support sending a fixed HT/VHT MCS alone, rather it
8345 	 * requires passing at least one of used basic rates along with them.
8346 	 * Fixed rate setting across different preambles(legacy, HT, VHT) is
8347 	 * not supported by the FW. Hence use of FIXED_RATE vdev param is not
8348 	 * suitable for setting single HT/VHT rates.
8349 	 * But, there could be a single basic rate passed from userspace which
8350 	 * can be done through the FIXED_RATE param.
8351 	 */
8352 	if (ath11k_mac_has_single_legacy_rate(ar, band, mask)) {
8353 		ret = ath11k_mac_get_single_legacy_rate(ar, band, mask, &rate,
8354 							&nss);
8355 		if (ret) {
8356 			ath11k_warn(ar->ab, "failed to get single legacy rate for vdev %i: %d\n",
8357 				    arvif->vdev_id, ret);
8358 			return ret;
8359 		}
8360 		ieee80211_iterate_stations_atomic(ar->hw,
8361 						  ath11k_mac_disable_peer_fixed_rate,
8362 						  arvif);
8363 	} else if (ath11k_mac_bitrate_mask_get_single_nss(ar, band, mask,
8364 							  &single_nss)) {
8365 		rate = WMI_FIXED_RATE_NONE;
8366 		nss = single_nss;
8367 		mutex_lock(&ar->conf_mutex);
8368 		arvif->bitrate_mask = *mask;
8369 		ieee80211_iterate_stations_atomic(ar->hw,
8370 						  ath11k_mac_set_bitrate_mask_iter,
8371 						  arvif);
8372 		mutex_unlock(&ar->conf_mutex);
8373 	} else {
8374 		rate = WMI_FIXED_RATE_NONE;
8375 
8376 		if (!ath11k_mac_validate_vht_he_fixed_rate_settings(ar, band, mask))
8377 			ath11k_warn(ar->ab,
8378 				    "could not update fixed rate settings to all peers due to mcs/nss incompatibility\n");
8379 		nss = min_t(u32, ar->num_tx_chains,
8380 			    max(max(ath11k_mac_max_ht_nss(ht_mcs_mask),
8381 				    ath11k_mac_max_vht_nss(vht_mcs_mask)),
8382 				ath11k_mac_max_he_nss(he_mcs_mask)));
8383 
8384 		/* If multiple rates across different preambles are given
8385 		 * we can reconfigure this info with all peers using PEER_ASSOC
8386 		 * command with the below exception cases.
8387 		 * - Single VHT Rate : peer_assoc command accommodates only MCS
8388 		 * range values i.e 0-7, 0-8, 0-9 for VHT. Though mac80211
8389 		 * mandates passing basic rates along with HT/VHT rates, FW
8390 		 * doesn't allow switching from VHT to Legacy. Hence instead of
8391 		 * setting legacy and VHT rates using RATEMASK_CMD vdev cmd,
8392 		 * we could set this VHT rate as peer fixed rate param, which
8393 		 * will override FIXED rate and FW rate control algorithm.
8394 		 * If single VHT rate is passed along with HT rates, we select
8395 		 * the VHT rate as fixed rate for vht peers.
8396 		 * - Multiple VHT Rates : When Multiple VHT rates are given,this
8397 		 * can be set using RATEMASK CMD which uses FW rate-ctl alg.
8398 		 * TODO: Setting multiple VHT MCS and replacing peer_assoc with
8399 		 * RATEMASK_CMDID can cover all use cases of setting rates
8400 		 * across multiple preambles and rates within same type.
8401 		 * But requires more validation of the command at this point.
8402 		 */
8403 
8404 		num_rates = ath11k_mac_bitrate_mask_num_vht_rates(ar, band,
8405 								  mask);
8406 
8407 		if (!ath11k_mac_vht_mcs_range_present(ar, band, mask) &&
8408 		    num_rates > 1) {
8409 			/* TODO: Handle multiple VHT MCS values setting using
8410 			 * RATEMASK CMD
8411 			 */
8412 			ath11k_warn(ar->ab,
8413 				    "setting %d mcs values in bitrate mask not supported\n",
8414 				num_rates);
8415 			return -EINVAL;
8416 		}
8417 
8418 		num_rates = ath11k_mac_bitrate_mask_num_he_rates(ar, band,
8419 								 mask);
8420 		if (num_rates == 1)
8421 			he_fixed_rate = true;
8422 
8423 		if (!ath11k_mac_he_mcs_range_present(ar, band, mask) &&
8424 		    num_rates > 1) {
8425 			ath11k_warn(ar->ab,
8426 				    "Setting more than one HE MCS Value in bitrate mask not supported\n");
8427 			return -EINVAL;
8428 		}
8429 
8430 		mutex_lock(&ar->conf_mutex);
8431 		ieee80211_iterate_stations_atomic(ar->hw,
8432 						  ath11k_mac_disable_peer_fixed_rate,
8433 						  arvif);
8434 
8435 		arvif->bitrate_mask = *mask;
8436 		ieee80211_iterate_stations_atomic(ar->hw,
8437 						  ath11k_mac_set_bitrate_mask_iter,
8438 						  arvif);
8439 
8440 		mutex_unlock(&ar->conf_mutex);
8441 	}
8442 
8443 	mutex_lock(&ar->conf_mutex);
8444 
8445 	ret = ath11k_mac_set_rate_params(arvif, rate, nss, sgi, ldpc, he_gi,
8446 					 he_ltf, he_fixed_rate);
8447 	if (ret) {
8448 		ath11k_warn(ar->ab, "failed to set rate params on vdev %i: %d\n",
8449 			    arvif->vdev_id, ret);
8450 	}
8451 
8452 	mutex_unlock(&ar->conf_mutex);
8453 
8454 	return ret;
8455 }
8456 
8457 static void
ath11k_mac_op_reconfig_complete(struct ieee80211_hw * hw,enum ieee80211_reconfig_type reconfig_type)8458 ath11k_mac_op_reconfig_complete(struct ieee80211_hw *hw,
8459 				enum ieee80211_reconfig_type reconfig_type)
8460 {
8461 	struct ath11k *ar = hw->priv;
8462 	struct ath11k_base *ab = ar->ab;
8463 	int recovery_count;
8464 	struct ath11k_vif *arvif;
8465 
8466 	if (reconfig_type != IEEE80211_RECONFIG_TYPE_RESTART)
8467 		return;
8468 
8469 	mutex_lock(&ar->conf_mutex);
8470 
8471 	if (ar->state == ATH11K_STATE_RESTARTED) {
8472 		ath11k_warn(ar->ab, "pdev %d successfully recovered\n",
8473 			    ar->pdev->pdev_id);
8474 		ar->state = ATH11K_STATE_ON;
8475 		ieee80211_wake_queues(ar->hw);
8476 
8477 		if (ar->ab->hw_params.current_cc_support &&
8478 		    ar->alpha2[0] != 0 && ar->alpha2[1] != 0) {
8479 			struct wmi_set_current_country_params set_current_param = {};
8480 
8481 			memcpy(&set_current_param.alpha2, ar->alpha2, 2);
8482 			ath11k_wmi_send_set_current_country_cmd(ar, &set_current_param);
8483 		}
8484 
8485 		if (ab->is_reset) {
8486 			recovery_count = atomic_inc_return(&ab->recovery_count);
8487 			ath11k_dbg(ab, ATH11K_DBG_BOOT,
8488 				   "recovery count %d\n", recovery_count);
8489 			/* When there are multiple radios in an SOC,
8490 			 * the recovery has to be done for each radio
8491 			 */
8492 			if (recovery_count == ab->num_radios) {
8493 				atomic_dec(&ab->reset_count);
8494 				complete(&ab->reset_complete);
8495 				ab->is_reset = false;
8496 				atomic_set(&ab->fail_cont_count, 0);
8497 				ath11k_dbg(ab, ATH11K_DBG_BOOT, "reset success\n");
8498 			}
8499 		}
8500 		if (ar->ab->hw_params.support_fw_mac_sequence) {
8501 			list_for_each_entry(arvif, &ar->arvifs, list) {
8502 				if (arvif->is_up && arvif->vdev_type == WMI_VDEV_TYPE_STA)
8503 					ieee80211_hw_restart_disconnect(arvif->vif);
8504 			}
8505 		}
8506 	}
8507 
8508 	mutex_unlock(&ar->conf_mutex);
8509 }
8510 
8511 static void
ath11k_mac_update_bss_chan_survey(struct ath11k * ar,struct ieee80211_channel * channel)8512 ath11k_mac_update_bss_chan_survey(struct ath11k *ar,
8513 				  struct ieee80211_channel *channel)
8514 {
8515 	int ret;
8516 	enum wmi_bss_chan_info_req_type type = WMI_BSS_SURVEY_REQ_TYPE_READ;
8517 
8518 	lockdep_assert_held(&ar->conf_mutex);
8519 
8520 	if (!test_bit(WMI_TLV_SERVICE_BSS_CHANNEL_INFO_64, ar->ab->wmi_ab.svc_map) ||
8521 	    ar->rx_channel != channel)
8522 		return;
8523 
8524 	if (ar->scan.state != ATH11K_SCAN_IDLE) {
8525 		ath11k_dbg(ar->ab, ATH11K_DBG_MAC,
8526 			   "ignoring bss chan info req while scanning..\n");
8527 		return;
8528 	}
8529 
8530 	reinit_completion(&ar->bss_survey_done);
8531 
8532 	ret = ath11k_wmi_pdev_bss_chan_info_request(ar, type);
8533 	if (ret) {
8534 		ath11k_warn(ar->ab, "failed to send pdev bss chan info request\n");
8535 		return;
8536 	}
8537 
8538 	ret = wait_for_completion_timeout(&ar->bss_survey_done, 3 * HZ);
8539 	if (ret == 0)
8540 		ath11k_warn(ar->ab, "bss channel survey timed out\n");
8541 }
8542 
ath11k_mac_op_get_survey(struct ieee80211_hw * hw,int idx,struct survey_info * survey)8543 static int ath11k_mac_op_get_survey(struct ieee80211_hw *hw, int idx,
8544 				    struct survey_info *survey)
8545 {
8546 	struct ath11k *ar = hw->priv;
8547 	struct ieee80211_supported_band *sband;
8548 	struct survey_info *ar_survey;
8549 	int ret = 0;
8550 
8551 	if (idx >= ATH11K_NUM_CHANS)
8552 		return -ENOENT;
8553 
8554 	ar_survey = &ar->survey[idx];
8555 
8556 	mutex_lock(&ar->conf_mutex);
8557 
8558 	sband = hw->wiphy->bands[NL80211_BAND_2GHZ];
8559 	if (sband && idx >= sband->n_channels) {
8560 		idx -= sband->n_channels;
8561 		sband = NULL;
8562 	}
8563 
8564 	if (!sband)
8565 		sband = hw->wiphy->bands[NL80211_BAND_5GHZ];
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_6GHZ];
8573 	if (!sband || idx >= sband->n_channels) {
8574 		ret = -ENOENT;
8575 		goto exit;
8576 	}
8577 
8578 	ath11k_mac_update_bss_chan_survey(ar, &sband->channels[idx]);
8579 
8580 	spin_lock_bh(&ar->data_lock);
8581 	memcpy(survey, ar_survey, sizeof(*survey));
8582 	spin_unlock_bh(&ar->data_lock);
8583 
8584 	survey->channel = &sband->channels[idx];
8585 
8586 	if (ar->rx_channel == survey->channel)
8587 		survey->filled |= SURVEY_INFO_IN_USE;
8588 
8589 exit:
8590 	mutex_unlock(&ar->conf_mutex);
8591 	return ret;
8592 }
8593 
ath11k_mac_put_chain_rssi(struct station_info * sinfo,struct ath11k_sta * arsta,char * pre,bool clear)8594 static void ath11k_mac_put_chain_rssi(struct station_info *sinfo,
8595 				      struct ath11k_sta *arsta,
8596 				      char *pre,
8597 				      bool clear)
8598 {
8599 	struct ath11k *ar = arsta->arvif->ar;
8600 	int i;
8601 	s8 rssi;
8602 
8603 	for (i = 0; i < ARRAY_SIZE(sinfo->chain_signal); i++) {
8604 		sinfo->chains &= ~BIT(i);
8605 		rssi = arsta->chain_signal[i];
8606 		if (clear)
8607 			arsta->chain_signal[i] = ATH11K_INVALID_RSSI_FULL;
8608 
8609 		ath11k_dbg(ar->ab, ATH11K_DBG_MAC,
8610 			   "sta statistics %s rssi[%d] %d\n", pre, i, rssi);
8611 
8612 		if (rssi != ATH11K_DEFAULT_NOISE_FLOOR &&
8613 		    rssi != ATH11K_INVALID_RSSI_FULL &&
8614 		    rssi != ATH11K_INVALID_RSSI_EMPTY &&
8615 		    rssi != 0) {
8616 			sinfo->chain_signal[i] = rssi;
8617 			sinfo->chains |= BIT(i);
8618 			sinfo->filled |= BIT_ULL(NL80211_STA_INFO_CHAIN_SIGNAL);
8619 		}
8620 	}
8621 }
8622 
ath11k_mac_op_sta_statistics(struct ieee80211_hw * hw,struct ieee80211_vif * vif,struct ieee80211_sta * sta,struct station_info * sinfo)8623 static void ath11k_mac_op_sta_statistics(struct ieee80211_hw *hw,
8624 					 struct ieee80211_vif *vif,
8625 					 struct ieee80211_sta *sta,
8626 					 struct station_info *sinfo)
8627 {
8628 	struct ath11k_sta *arsta = (struct ath11k_sta *)sta->drv_priv;
8629 	struct ath11k *ar = arsta->arvif->ar;
8630 	s8 signal;
8631 	bool db2dbm = test_bit(WMI_TLV_SERVICE_HW_DB2DBM_CONVERSION_SUPPORT,
8632 			       ar->ab->wmi_ab.svc_map);
8633 
8634 	sinfo->rx_duration = arsta->rx_duration;
8635 	sinfo->filled |= BIT_ULL(NL80211_STA_INFO_RX_DURATION);
8636 
8637 	sinfo->tx_duration = arsta->tx_duration;
8638 	sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_DURATION);
8639 
8640 	if (arsta->txrate.legacy || arsta->txrate.nss) {
8641 		if (arsta->txrate.legacy) {
8642 			sinfo->txrate.legacy = arsta->txrate.legacy;
8643 		} else {
8644 			sinfo->txrate.mcs = arsta->txrate.mcs;
8645 			sinfo->txrate.nss = arsta->txrate.nss;
8646 			sinfo->txrate.bw = arsta->txrate.bw;
8647 			sinfo->txrate.he_gi = arsta->txrate.he_gi;
8648 			sinfo->txrate.he_dcm = arsta->txrate.he_dcm;
8649 			sinfo->txrate.he_ru_alloc = arsta->txrate.he_ru_alloc;
8650 		}
8651 		sinfo->txrate.flags = arsta->txrate.flags;
8652 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_BITRATE);
8653 	}
8654 
8655 	ath11k_mac_put_chain_rssi(sinfo, arsta, "ppdu", false);
8656 
8657 	if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_CHAIN_SIGNAL)) &&
8658 	    arsta->arvif->vdev_type == WMI_VDEV_TYPE_STA &&
8659 	    ar->ab->hw_params.supports_rssi_stats &&
8660 	    !ath11k_debugfs_get_fw_stats(ar, ar->pdev->pdev_id, 0,
8661 					 WMI_REQUEST_RSSI_PER_CHAIN_STAT)) {
8662 		ath11k_mac_put_chain_rssi(sinfo, arsta, "fw stats", true);
8663 	}
8664 
8665 	signal = arsta->rssi_comb;
8666 	if (!signal &&
8667 	    arsta->arvif->vdev_type == WMI_VDEV_TYPE_STA &&
8668 	    ar->ab->hw_params.supports_rssi_stats &&
8669 	    !(ath11k_debugfs_get_fw_stats(ar, ar->pdev->pdev_id, 0,
8670 					WMI_REQUEST_VDEV_STAT)))
8671 		signal = arsta->rssi_beacon;
8672 
8673 	ath11k_dbg(ar->ab, ATH11K_DBG_MAC,
8674 		   "sta statistics db2dbm %u rssi comb %d rssi beacon %d\n",
8675 		   db2dbm, arsta->rssi_comb, arsta->rssi_beacon);
8676 
8677 	if (signal) {
8678 		sinfo->signal = db2dbm ? signal : signal + ATH11K_DEFAULT_NOISE_FLOOR;
8679 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_SIGNAL);
8680 	}
8681 
8682 	sinfo->signal_avg = ewma_avg_rssi_read(&arsta->avg_rssi) +
8683 		ATH11K_DEFAULT_NOISE_FLOOR;
8684 	sinfo->filled |= BIT_ULL(NL80211_STA_INFO_SIGNAL_AVG);
8685 }
8686 
8687 #if IS_ENABLED(CONFIG_IPV6)
ath11k_generate_ns_mc_addr(struct ath11k * ar,struct ath11k_arp_ns_offload * offload)8688 static void ath11k_generate_ns_mc_addr(struct ath11k *ar,
8689 				       struct ath11k_arp_ns_offload *offload)
8690 {
8691 	int i;
8692 
8693 	for (i = 0; i < offload->ipv6_count; i++) {
8694 		offload->self_ipv6_addr[i][0] = 0xff;
8695 		offload->self_ipv6_addr[i][1] = 0x02;
8696 		offload->self_ipv6_addr[i][11] = 0x01;
8697 		offload->self_ipv6_addr[i][12] = 0xff;
8698 		offload->self_ipv6_addr[i][13] =
8699 					offload->ipv6_addr[i][13];
8700 		offload->self_ipv6_addr[i][14] =
8701 					offload->ipv6_addr[i][14];
8702 		offload->self_ipv6_addr[i][15] =
8703 					offload->ipv6_addr[i][15];
8704 		ath11k_dbg(ar->ab, ATH11K_DBG_MAC, "NS solicited addr %pI6\n",
8705 			   offload->self_ipv6_addr[i]);
8706 	}
8707 }
8708 
ath11k_mac_op_ipv6_changed(struct ieee80211_hw * hw,struct ieee80211_vif * vif,struct inet6_dev * idev)8709 static void ath11k_mac_op_ipv6_changed(struct ieee80211_hw *hw,
8710 				       struct ieee80211_vif *vif,
8711 				       struct inet6_dev *idev)
8712 {
8713 	struct ath11k *ar = hw->priv;
8714 	struct ath11k_arp_ns_offload *offload;
8715 	struct ath11k_vif *arvif = ath11k_vif_to_arvif(vif);
8716 	struct inet6_ifaddr *ifa6;
8717 	struct ifacaddr6 *ifaca6;
8718 	struct list_head *p;
8719 	u32 count, scope;
8720 
8721 	ath11k_dbg(ar->ab, ATH11K_DBG_MAC, "op ipv6 changed\n");
8722 
8723 	offload = &arvif->arp_ns_offload;
8724 	count = 0;
8725 
8726 	read_lock_bh(&idev->lock);
8727 
8728 	memset(offload->ipv6_addr, 0, sizeof(offload->ipv6_addr));
8729 	memset(offload->self_ipv6_addr, 0, sizeof(offload->self_ipv6_addr));
8730 	memcpy(offload->mac_addr, vif->addr, ETH_ALEN);
8731 
8732 	/* get unicast address */
8733 	list_for_each(p, &idev->addr_list) {
8734 		if (count >= ATH11K_IPV6_MAX_COUNT)
8735 			goto generate;
8736 
8737 		ifa6 = list_entry(p, struct inet6_ifaddr, if_list);
8738 		if (ifa6->flags & IFA_F_DADFAILED)
8739 			continue;
8740 		scope = ipv6_addr_src_scope(&ifa6->addr);
8741 		if (scope == IPV6_ADDR_SCOPE_LINKLOCAL ||
8742 		    scope == IPV6_ADDR_SCOPE_GLOBAL) {
8743 			memcpy(offload->ipv6_addr[count], &ifa6->addr.s6_addr,
8744 			       sizeof(ifa6->addr.s6_addr));
8745 			offload->ipv6_type[count] = ATH11K_IPV6_UC_TYPE;
8746 			ath11k_dbg(ar->ab, ATH11K_DBG_MAC, "count %d ipv6 uc %pI6 scope %d\n",
8747 				   count, offload->ipv6_addr[count],
8748 				   scope);
8749 			count++;
8750 		} else {
8751 			ath11k_warn(ar->ab, "Unsupported ipv6 scope: %d\n", scope);
8752 		}
8753 	}
8754 
8755 	/* get anycast address */
8756 	for (ifaca6 = idev->ac_list; ifaca6; ifaca6 = ifaca6->aca_next) {
8757 		if (count >= ATH11K_IPV6_MAX_COUNT)
8758 			goto generate;
8759 
8760 		scope = ipv6_addr_src_scope(&ifaca6->aca_addr);
8761 		if (scope == IPV6_ADDR_SCOPE_LINKLOCAL ||
8762 		    scope == IPV6_ADDR_SCOPE_GLOBAL) {
8763 			memcpy(offload->ipv6_addr[count], &ifaca6->aca_addr,
8764 			       sizeof(ifaca6->aca_addr));
8765 			offload->ipv6_type[count] = ATH11K_IPV6_AC_TYPE;
8766 			ath11k_dbg(ar->ab, ATH11K_DBG_MAC, "count %d ipv6 ac %pI6 scope %d\n",
8767 				   count, offload->ipv6_addr[count],
8768 				   scope);
8769 			count++;
8770 		} else {
8771 			ath11k_warn(ar->ab, "Unsupported ipv scope: %d\n", scope);
8772 		}
8773 	}
8774 
8775 generate:
8776 	offload->ipv6_count = count;
8777 	read_unlock_bh(&idev->lock);
8778 
8779 	/* generate ns multicast address */
8780 	ath11k_generate_ns_mc_addr(ar, offload);
8781 }
8782 #endif
8783 
ath11k_mac_op_set_rekey_data(struct ieee80211_hw * hw,struct ieee80211_vif * vif,struct cfg80211_gtk_rekey_data * data)8784 static void ath11k_mac_op_set_rekey_data(struct ieee80211_hw *hw,
8785 					 struct ieee80211_vif *vif,
8786 					 struct cfg80211_gtk_rekey_data *data)
8787 {
8788 	struct ath11k *ar = hw->priv;
8789 	struct ath11k_vif *arvif = ath11k_vif_to_arvif(vif);
8790 	struct ath11k_rekey_data *rekey_data = &arvif->rekey_data;
8791 
8792 	ath11k_dbg(ar->ab, ATH11K_DBG_MAC, "set rekey data vdev %d\n",
8793 		   arvif->vdev_id);
8794 
8795 	mutex_lock(&ar->conf_mutex);
8796 
8797 	memcpy(rekey_data->kck, data->kck, NL80211_KCK_LEN);
8798 	memcpy(rekey_data->kek, data->kek, NL80211_KEK_LEN);
8799 
8800 	/* The supplicant works on big-endian, the firmware expects it on
8801 	 * little endian.
8802 	 */
8803 	rekey_data->replay_ctr = get_unaligned_be64(data->replay_ctr);
8804 
8805 	arvif->rekey_data.enable_offload = true;
8806 
8807 	ath11k_dbg_dump(ar->ab, ATH11K_DBG_MAC, "kck", NULL,
8808 			rekey_data->kck, NL80211_KCK_LEN);
8809 	ath11k_dbg_dump(ar->ab, ATH11K_DBG_MAC, "kek", NULL,
8810 			rekey_data->kck, NL80211_KEK_LEN);
8811 	ath11k_dbg_dump(ar->ab, ATH11K_DBG_MAC, "replay ctr", NULL,
8812 			&rekey_data->replay_ctr, sizeof(rekey_data->replay_ctr));
8813 
8814 	mutex_unlock(&ar->conf_mutex);
8815 }
8816 
ath11k_mac_op_set_bios_sar_specs(struct ieee80211_hw * hw,const struct cfg80211_sar_specs * sar)8817 static int ath11k_mac_op_set_bios_sar_specs(struct ieee80211_hw *hw,
8818 					    const struct cfg80211_sar_specs *sar)
8819 {
8820 	struct ath11k *ar = hw->priv;
8821 	const struct cfg80211_sar_sub_specs *sspec;
8822 	int ret, index;
8823 	u8 *sar_tbl;
8824 	u32 i;
8825 
8826 	if (!sar || sar->type != NL80211_SAR_TYPE_POWER ||
8827 	    sar->num_sub_specs == 0)
8828 		return -EINVAL;
8829 
8830 	mutex_lock(&ar->conf_mutex);
8831 
8832 	if (!test_bit(WMI_TLV_SERVICE_BIOS_SAR_SUPPORT, ar->ab->wmi_ab.svc_map) ||
8833 	    !ar->ab->hw_params.bios_sar_capa) {
8834 		ret = -EOPNOTSUPP;
8835 		goto exit;
8836 	}
8837 
8838 	ret = ath11k_wmi_pdev_set_bios_geo_table_param(ar);
8839 	if (ret) {
8840 		ath11k_warn(ar->ab, "failed to set geo table: %d\n", ret);
8841 		goto exit;
8842 	}
8843 
8844 	sar_tbl = kzalloc(BIOS_SAR_TABLE_LEN, GFP_KERNEL);
8845 	if (!sar_tbl) {
8846 		ret = -ENOMEM;
8847 		goto exit;
8848 	}
8849 
8850 	sspec = sar->sub_specs;
8851 	for (i = 0; i < sar->num_sub_specs; i++) {
8852 		if (sspec->freq_range_index >= (BIOS_SAR_TABLE_LEN >> 1)) {
8853 			ath11k_warn(ar->ab, "Ignore bad frequency index %u, max allowed %u\n",
8854 				    sspec->freq_range_index, BIOS_SAR_TABLE_LEN >> 1);
8855 			continue;
8856 		}
8857 
8858 		/* chain0 and chain1 share same power setting */
8859 		sar_tbl[sspec->freq_range_index] = sspec->power;
8860 		index = sspec->freq_range_index + (BIOS_SAR_TABLE_LEN >> 1);
8861 		sar_tbl[index] = sspec->power;
8862 		ath11k_dbg(ar->ab, ATH11K_DBG_MAC, "sar tbl[%d] = %d\n",
8863 			   sspec->freq_range_index, sar_tbl[sspec->freq_range_index]);
8864 		sspec++;
8865 	}
8866 
8867 	ret = ath11k_wmi_pdev_set_bios_sar_table_param(ar, sar_tbl);
8868 	if (ret)
8869 		ath11k_warn(ar->ab, "failed to set sar power: %d", ret);
8870 
8871 	kfree(sar_tbl);
8872 exit:
8873 	mutex_unlock(&ar->conf_mutex);
8874 
8875 	return ret;
8876 }
8877 
ath11k_mac_op_cancel_remain_on_channel(struct ieee80211_hw * hw,struct ieee80211_vif * vif)8878 static int ath11k_mac_op_cancel_remain_on_channel(struct ieee80211_hw *hw,
8879 						  struct ieee80211_vif *vif)
8880 {
8881 	struct ath11k *ar = hw->priv;
8882 
8883 	mutex_lock(&ar->conf_mutex);
8884 
8885 	spin_lock_bh(&ar->data_lock);
8886 	ar->scan.roc_notify = false;
8887 	spin_unlock_bh(&ar->data_lock);
8888 
8889 	ath11k_scan_abort(ar);
8890 
8891 	mutex_unlock(&ar->conf_mutex);
8892 
8893 	cancel_delayed_work_sync(&ar->scan.timeout);
8894 
8895 	return 0;
8896 }
8897 
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)8898 static int ath11k_mac_op_remain_on_channel(struct ieee80211_hw *hw,
8899 					   struct ieee80211_vif *vif,
8900 					   struct ieee80211_channel *chan,
8901 					   int duration,
8902 					   enum ieee80211_roc_type type)
8903 {
8904 	struct ath11k *ar = hw->priv;
8905 	struct ath11k_vif *arvif = ath11k_vif_to_arvif(vif);
8906 	struct scan_req_params arg;
8907 	int ret;
8908 	u32 scan_time_msec;
8909 
8910 	mutex_lock(&ar->conf_mutex);
8911 
8912 	spin_lock_bh(&ar->data_lock);
8913 	switch (ar->scan.state) {
8914 	case ATH11K_SCAN_IDLE:
8915 		reinit_completion(&ar->scan.started);
8916 		reinit_completion(&ar->scan.completed);
8917 		reinit_completion(&ar->scan.on_channel);
8918 		ar->scan.state = ATH11K_SCAN_STARTING;
8919 		ar->scan.is_roc = true;
8920 		ar->scan.vdev_id = arvif->vdev_id;
8921 		ar->scan.roc_freq = chan->center_freq;
8922 		ar->scan.roc_notify = true;
8923 		ret = 0;
8924 		break;
8925 	case ATH11K_SCAN_STARTING:
8926 	case ATH11K_SCAN_RUNNING:
8927 	case ATH11K_SCAN_ABORTING:
8928 		ret = -EBUSY;
8929 		break;
8930 	}
8931 	spin_unlock_bh(&ar->data_lock);
8932 
8933 	if (ret)
8934 		goto exit;
8935 
8936 	scan_time_msec = ar->hw->wiphy->max_remain_on_channel_duration * 2;
8937 
8938 	memset(&arg, 0, sizeof(arg));
8939 	ath11k_wmi_start_scan_init(ar, &arg);
8940 	arg.num_chan = 1;
8941 	arg.chan_list = kcalloc(arg.num_chan, sizeof(*arg.chan_list),
8942 				GFP_KERNEL);
8943 	if (!arg.chan_list) {
8944 		ret = -ENOMEM;
8945 		goto exit;
8946 	}
8947 
8948 	arg.vdev_id = arvif->vdev_id;
8949 	arg.scan_id = ATH11K_SCAN_ID;
8950 	arg.chan_list[0] = chan->center_freq;
8951 	arg.dwell_time_active = scan_time_msec;
8952 	arg.dwell_time_passive = scan_time_msec;
8953 	arg.max_scan_time = scan_time_msec;
8954 	arg.scan_flags |= WMI_SCAN_FLAG_PASSIVE;
8955 	arg.scan_flags |= WMI_SCAN_FILTER_PROBE_REQ;
8956 	arg.burst_duration = duration;
8957 
8958 	ret = ath11k_start_scan(ar, &arg);
8959 	if (ret) {
8960 		ath11k_warn(ar->ab, "failed to start roc scan: %d\n", ret);
8961 
8962 		spin_lock_bh(&ar->data_lock);
8963 		ar->scan.state = ATH11K_SCAN_IDLE;
8964 		spin_unlock_bh(&ar->data_lock);
8965 		goto free_chan_list;
8966 	}
8967 
8968 	ret = wait_for_completion_timeout(&ar->scan.on_channel, 3 * HZ);
8969 	if (ret == 0) {
8970 		ath11k_warn(ar->ab, "failed to switch to channel for roc scan\n");
8971 		ret = ath11k_scan_stop(ar);
8972 		if (ret)
8973 			ath11k_warn(ar->ab, "failed to stop scan: %d\n", ret);
8974 		ret = -ETIMEDOUT;
8975 		goto free_chan_list;
8976 	}
8977 
8978 	ieee80211_queue_delayed_work(ar->hw, &ar->scan.timeout,
8979 				     msecs_to_jiffies(duration));
8980 
8981 	ret = 0;
8982 
8983 free_chan_list:
8984 	kfree(arg.chan_list);
8985 exit:
8986 	mutex_unlock(&ar->conf_mutex);
8987 	return ret;
8988 }
8989 
ath11k_fw_stats_request(struct ath11k * ar,struct stats_request_params * req_param)8990 static int ath11k_fw_stats_request(struct ath11k *ar,
8991 				   struct stats_request_params *req_param)
8992 {
8993 	struct ath11k_base *ab = ar->ab;
8994 	unsigned long time_left;
8995 	int ret;
8996 
8997 	lockdep_assert_held(&ar->conf_mutex);
8998 
8999 	spin_lock_bh(&ar->data_lock);
9000 	ar->fw_stats_done = false;
9001 	ath11k_fw_stats_pdevs_free(&ar->fw_stats.pdevs);
9002 	spin_unlock_bh(&ar->data_lock);
9003 
9004 	reinit_completion(&ar->fw_stats_complete);
9005 
9006 	ret = ath11k_wmi_send_stats_request_cmd(ar, req_param);
9007 	if (ret) {
9008 		ath11k_warn(ab, "could not request fw stats (%d)\n",
9009 			    ret);
9010 		return ret;
9011 	}
9012 
9013 	time_left = wait_for_completion_timeout(&ar->fw_stats_complete,
9014 						1 * HZ);
9015 
9016 	if (!time_left)
9017 		return -ETIMEDOUT;
9018 
9019 	return 0;
9020 }
9021 
ath11k_mac_op_get_txpower(struct ieee80211_hw * hw,struct ieee80211_vif * vif,int * dbm)9022 static int ath11k_mac_op_get_txpower(struct ieee80211_hw *hw,
9023 				     struct ieee80211_vif *vif,
9024 				     int *dbm)
9025 {
9026 	struct ath11k *ar = hw->priv;
9027 	struct ath11k_base *ab = ar->ab;
9028 	struct stats_request_params req_param = {0};
9029 	struct ath11k_fw_stats_pdev *pdev;
9030 	int ret;
9031 
9032 	/* Final Tx power is minimum of Target Power, CTL power, Regulatory
9033 	 * Power, PSD EIRP Power. We just know the Regulatory power from the
9034 	 * regulatory rules obtained. FW knows all these power and sets the min
9035 	 * of these. Hence, we request the FW pdev stats in which FW reports
9036 	 * the minimum of all vdev's channel Tx power.
9037 	 */
9038 	mutex_lock(&ar->conf_mutex);
9039 
9040 	if (ar->state != ATH11K_STATE_ON)
9041 		goto err_fallback;
9042 
9043 	/* Firmware doesn't provide Tx power during CAC hence no need to fetch
9044 	 * the stats.
9045 	 */
9046 	if (test_bit(ATH11K_CAC_RUNNING, &ar->dev_flags)) {
9047 		mutex_unlock(&ar->conf_mutex);
9048 		return -EAGAIN;
9049 	}
9050 
9051 	req_param.pdev_id = ar->pdev->pdev_id;
9052 	req_param.stats_id = WMI_REQUEST_PDEV_STAT;
9053 
9054 	ret = ath11k_fw_stats_request(ar, &req_param);
9055 	if (ret) {
9056 		ath11k_warn(ab, "failed to request fw pdev stats: %d\n", ret);
9057 		goto err_fallback;
9058 	}
9059 
9060 	spin_lock_bh(&ar->data_lock);
9061 	pdev = list_first_entry_or_null(&ar->fw_stats.pdevs,
9062 					struct ath11k_fw_stats_pdev, list);
9063 	if (!pdev) {
9064 		spin_unlock_bh(&ar->data_lock);
9065 		goto err_fallback;
9066 	}
9067 
9068 	/* tx power is set as 2 units per dBm in FW. */
9069 	*dbm = pdev->chan_tx_power / 2;
9070 
9071 	spin_unlock_bh(&ar->data_lock);
9072 	mutex_unlock(&ar->conf_mutex);
9073 
9074 	ath11k_dbg(ar->ab, ATH11K_DBG_MAC, "txpower from firmware %d, reported %d dBm\n",
9075 		   pdev->chan_tx_power, *dbm);
9076 	return 0;
9077 
9078 err_fallback:
9079 	mutex_unlock(&ar->conf_mutex);
9080 	/* We didn't get txpower from FW. Hence, relying on vif->bss_conf.txpower */
9081 	*dbm = vif->bss_conf.txpower;
9082 	ath11k_dbg(ar->ab, ATH11K_DBG_MAC, "txpower from firmware NaN, reported %d dBm\n",
9083 		   *dbm);
9084 	return 0;
9085 }
9086 
9087 static const struct ieee80211_ops ath11k_ops = {
9088 	.tx				= ath11k_mac_op_tx,
9089 	.wake_tx_queue			= ieee80211_handle_wake_tx_queue,
9090 	.start                          = ath11k_mac_op_start,
9091 	.stop                           = ath11k_mac_op_stop,
9092 	.reconfig_complete              = ath11k_mac_op_reconfig_complete,
9093 	.add_interface                  = ath11k_mac_op_add_interface,
9094 	.remove_interface		= ath11k_mac_op_remove_interface,
9095 	.update_vif_offload		= ath11k_mac_op_update_vif_offload,
9096 	.config                         = ath11k_mac_op_config,
9097 	.bss_info_changed               = ath11k_mac_op_bss_info_changed,
9098 	.configure_filter		= ath11k_mac_op_configure_filter,
9099 	.hw_scan                        = ath11k_mac_op_hw_scan,
9100 	.cancel_hw_scan                 = ath11k_mac_op_cancel_hw_scan,
9101 	.set_key                        = ath11k_mac_op_set_key,
9102 	.set_rekey_data	                = ath11k_mac_op_set_rekey_data,
9103 	.sta_state                      = ath11k_mac_op_sta_state,
9104 	.sta_set_4addr                  = ath11k_mac_op_sta_set_4addr,
9105 	.sta_set_txpwr			= ath11k_mac_op_sta_set_txpwr,
9106 	.sta_rc_update			= ath11k_mac_op_sta_rc_update,
9107 	.conf_tx                        = ath11k_mac_op_conf_tx,
9108 	.set_antenna			= ath11k_mac_op_set_antenna,
9109 	.get_antenna			= ath11k_mac_op_get_antenna,
9110 	.ampdu_action			= ath11k_mac_op_ampdu_action,
9111 	.add_chanctx			= ath11k_mac_op_add_chanctx,
9112 	.remove_chanctx			= ath11k_mac_op_remove_chanctx,
9113 	.change_chanctx			= ath11k_mac_op_change_chanctx,
9114 	.assign_vif_chanctx		= ath11k_mac_op_assign_vif_chanctx,
9115 	.unassign_vif_chanctx		= ath11k_mac_op_unassign_vif_chanctx,
9116 	.switch_vif_chanctx		= ath11k_mac_op_switch_vif_chanctx,
9117 	.set_rts_threshold		= ath11k_mac_op_set_rts_threshold,
9118 	.set_frag_threshold		= ath11k_mac_op_set_frag_threshold,
9119 	.set_bitrate_mask		= ath11k_mac_op_set_bitrate_mask,
9120 	.get_survey			= ath11k_mac_op_get_survey,
9121 	.flush				= ath11k_mac_op_flush,
9122 	.sta_statistics			= ath11k_mac_op_sta_statistics,
9123 	CFG80211_TESTMODE_CMD(ath11k_tm_cmd)
9124 
9125 #ifdef CONFIG_PM
9126 	.suspend			= ath11k_wow_op_suspend,
9127 	.resume				= ath11k_wow_op_resume,
9128 	.set_wakeup			= ath11k_wow_op_set_wakeup,
9129 #endif
9130 
9131 #ifdef CONFIG_ATH11K_DEBUGFS
9132 	.sta_add_debugfs		= ath11k_debugfs_sta_op_add,
9133 #endif
9134 
9135 #if IS_ENABLED(CONFIG_IPV6)
9136 	.ipv6_addr_change = ath11k_mac_op_ipv6_changed,
9137 #endif
9138 	.get_txpower                    = ath11k_mac_op_get_txpower,
9139 
9140 	.set_sar_specs			= ath11k_mac_op_set_bios_sar_specs,
9141 	.remain_on_channel		= ath11k_mac_op_remain_on_channel,
9142 	.cancel_remain_on_channel	= ath11k_mac_op_cancel_remain_on_channel,
9143 };
9144 
ath11k_mac_update_ch_list(struct ath11k * ar,struct ieee80211_supported_band * band,u32 freq_low,u32 freq_high)9145 static void ath11k_mac_update_ch_list(struct ath11k *ar,
9146 				      struct ieee80211_supported_band *band,
9147 				      u32 freq_low, u32 freq_high)
9148 {
9149 	int i;
9150 
9151 	if (!(freq_low && freq_high))
9152 		return;
9153 
9154 	for (i = 0; i < band->n_channels; i++) {
9155 		if (band->channels[i].center_freq < freq_low ||
9156 		    band->channels[i].center_freq > freq_high)
9157 			band->channels[i].flags |= IEEE80211_CHAN_DISABLED;
9158 	}
9159 }
9160 
ath11k_get_phy_id(struct ath11k * ar,u32 band)9161 static u32 ath11k_get_phy_id(struct ath11k *ar, u32 band)
9162 {
9163 	struct ath11k_pdev *pdev = ar->pdev;
9164 	struct ath11k_pdev_cap *pdev_cap = &pdev->cap;
9165 
9166 	if (band == WMI_HOST_WLAN_2G_CAP)
9167 		return pdev_cap->band[NL80211_BAND_2GHZ].phy_id;
9168 
9169 	if (band == WMI_HOST_WLAN_5G_CAP)
9170 		return pdev_cap->band[NL80211_BAND_5GHZ].phy_id;
9171 
9172 	ath11k_warn(ar->ab, "unsupported phy cap:%d\n", band);
9173 
9174 	return 0;
9175 }
9176 
ath11k_mac_setup_channels_rates(struct ath11k * ar,u32 supported_bands)9177 static int ath11k_mac_setup_channels_rates(struct ath11k *ar,
9178 					   u32 supported_bands)
9179 {
9180 	struct ieee80211_supported_band *band;
9181 	struct ath11k_hal_reg_capabilities_ext *reg_cap, *temp_reg_cap;
9182 	void *channels;
9183 	u32 phy_id;
9184 
9185 	BUILD_BUG_ON((ARRAY_SIZE(ath11k_2ghz_channels) +
9186 		      ARRAY_SIZE(ath11k_5ghz_channels) +
9187 		      ARRAY_SIZE(ath11k_6ghz_channels)) !=
9188 		     ATH11K_NUM_CHANS);
9189 
9190 	reg_cap = &ar->ab->hal_reg_cap[ar->pdev_idx];
9191 	temp_reg_cap = reg_cap;
9192 
9193 	if (supported_bands & WMI_HOST_WLAN_2G_CAP) {
9194 		channels = kmemdup(ath11k_2ghz_channels,
9195 				   sizeof(ath11k_2ghz_channels),
9196 				   GFP_KERNEL);
9197 		if (!channels)
9198 			return -ENOMEM;
9199 
9200 		band = &ar->mac.sbands[NL80211_BAND_2GHZ];
9201 		band->band = NL80211_BAND_2GHZ;
9202 		band->n_channels = ARRAY_SIZE(ath11k_2ghz_channels);
9203 		band->channels = channels;
9204 		band->n_bitrates = ath11k_g_rates_size;
9205 		band->bitrates = ath11k_g_rates;
9206 		ar->hw->wiphy->bands[NL80211_BAND_2GHZ] = band;
9207 
9208 		if (ar->ab->hw_params.single_pdev_only) {
9209 			phy_id = ath11k_get_phy_id(ar, WMI_HOST_WLAN_2G_CAP);
9210 			temp_reg_cap = &ar->ab->hal_reg_cap[phy_id];
9211 		}
9212 		ath11k_mac_update_ch_list(ar, band,
9213 					  temp_reg_cap->low_2ghz_chan,
9214 					  temp_reg_cap->high_2ghz_chan);
9215 	}
9216 
9217 	if (supported_bands & WMI_HOST_WLAN_5G_CAP) {
9218 		if (reg_cap->high_5ghz_chan >= ATH11K_MIN_6G_FREQ) {
9219 			channels = kmemdup(ath11k_6ghz_channels,
9220 					   sizeof(ath11k_6ghz_channels), GFP_KERNEL);
9221 			if (!channels) {
9222 				kfree(ar->mac.sbands[NL80211_BAND_2GHZ].channels);
9223 				return -ENOMEM;
9224 			}
9225 
9226 			ar->supports_6ghz = true;
9227 			band = &ar->mac.sbands[NL80211_BAND_6GHZ];
9228 			band->band = NL80211_BAND_6GHZ;
9229 			band->n_channels = ARRAY_SIZE(ath11k_6ghz_channels);
9230 			band->channels = channels;
9231 			band->n_bitrates = ath11k_a_rates_size;
9232 			band->bitrates = ath11k_a_rates;
9233 			ar->hw->wiphy->bands[NL80211_BAND_6GHZ] = band;
9234 
9235 			if (ar->ab->hw_params.single_pdev_only) {
9236 				phy_id = ath11k_get_phy_id(ar, WMI_HOST_WLAN_5G_CAP);
9237 				temp_reg_cap = &ar->ab->hal_reg_cap[phy_id];
9238 			}
9239 
9240 			ath11k_mac_update_ch_list(ar, band,
9241 						  temp_reg_cap->low_5ghz_chan,
9242 						  temp_reg_cap->high_5ghz_chan);
9243 		}
9244 
9245 		if (reg_cap->low_5ghz_chan < ATH11K_MIN_6G_FREQ) {
9246 			channels = kmemdup(ath11k_5ghz_channels,
9247 					   sizeof(ath11k_5ghz_channels),
9248 					   GFP_KERNEL);
9249 			if (!channels) {
9250 				kfree(ar->mac.sbands[NL80211_BAND_2GHZ].channels);
9251 				kfree(ar->mac.sbands[NL80211_BAND_6GHZ].channels);
9252 				return -ENOMEM;
9253 			}
9254 
9255 			band = &ar->mac.sbands[NL80211_BAND_5GHZ];
9256 			band->band = NL80211_BAND_5GHZ;
9257 			band->n_channels = ARRAY_SIZE(ath11k_5ghz_channels);
9258 			band->channels = channels;
9259 			band->n_bitrates = ath11k_a_rates_size;
9260 			band->bitrates = ath11k_a_rates;
9261 			ar->hw->wiphy->bands[NL80211_BAND_5GHZ] = band;
9262 
9263 			if (ar->ab->hw_params.single_pdev_only) {
9264 				phy_id = ath11k_get_phy_id(ar, WMI_HOST_WLAN_5G_CAP);
9265 				temp_reg_cap = &ar->ab->hal_reg_cap[phy_id];
9266 			}
9267 
9268 			ath11k_mac_update_ch_list(ar, band,
9269 						  temp_reg_cap->low_5ghz_chan,
9270 						  temp_reg_cap->high_5ghz_chan);
9271 		}
9272 	}
9273 
9274 	return 0;
9275 }
9276 
ath11k_mac_setup_iface_combinations(struct ath11k * ar)9277 static int ath11k_mac_setup_iface_combinations(struct ath11k *ar)
9278 {
9279 	struct ath11k_base *ab = ar->ab;
9280 	struct ieee80211_iface_combination *combinations;
9281 	struct ieee80211_iface_limit *limits;
9282 	int n_limits;
9283 
9284 	combinations = kzalloc(sizeof(*combinations), GFP_KERNEL);
9285 	if (!combinations)
9286 		return -ENOMEM;
9287 
9288 	n_limits = 2;
9289 
9290 	limits = kcalloc(n_limits, sizeof(*limits), GFP_KERNEL);
9291 	if (!limits) {
9292 		kfree(combinations);
9293 		return -ENOMEM;
9294 	}
9295 
9296 	limits[0].max = 1;
9297 	limits[0].types |= BIT(NL80211_IFTYPE_STATION);
9298 
9299 	limits[1].max = 16;
9300 	limits[1].types |= BIT(NL80211_IFTYPE_AP);
9301 
9302 	if (IS_ENABLED(CONFIG_MAC80211_MESH) &&
9303 	    ab->hw_params.interface_modes & BIT(NL80211_IFTYPE_MESH_POINT))
9304 		limits[1].types |= BIT(NL80211_IFTYPE_MESH_POINT);
9305 
9306 	combinations[0].limits = limits;
9307 	combinations[0].n_limits = n_limits;
9308 	combinations[0].max_interfaces = 16;
9309 	combinations[0].num_different_channels = 1;
9310 	combinations[0].beacon_int_infra_match = true;
9311 	combinations[0].beacon_int_min_gcd = 100;
9312 	combinations[0].radar_detect_widths = BIT(NL80211_CHAN_WIDTH_20_NOHT) |
9313 						BIT(NL80211_CHAN_WIDTH_20) |
9314 						BIT(NL80211_CHAN_WIDTH_40) |
9315 						BIT(NL80211_CHAN_WIDTH_80) |
9316 						BIT(NL80211_CHAN_WIDTH_80P80) |
9317 						BIT(NL80211_CHAN_WIDTH_160);
9318 
9319 	ar->hw->wiphy->iface_combinations = combinations;
9320 	ar->hw->wiphy->n_iface_combinations = 1;
9321 
9322 	return 0;
9323 }
9324 
9325 static const u8 ath11k_if_types_ext_capa[] = {
9326 	[0] = WLAN_EXT_CAPA1_EXT_CHANNEL_SWITCHING,
9327 	[2] = WLAN_EXT_CAPA3_MULTI_BSSID_SUPPORT,
9328 	[7] = WLAN_EXT_CAPA8_OPMODE_NOTIF,
9329 };
9330 
9331 static const u8 ath11k_if_types_ext_capa_sta[] = {
9332 	[0] = WLAN_EXT_CAPA1_EXT_CHANNEL_SWITCHING,
9333 	[2] = WLAN_EXT_CAPA3_MULTI_BSSID_SUPPORT,
9334 	[7] = WLAN_EXT_CAPA8_OPMODE_NOTIF,
9335 	[9] = WLAN_EXT_CAPA10_TWT_REQUESTER_SUPPORT,
9336 };
9337 
9338 static const u8 ath11k_if_types_ext_capa_ap[] = {
9339 	[0] = WLAN_EXT_CAPA1_EXT_CHANNEL_SWITCHING,
9340 	[2] = WLAN_EXT_CAPA3_MULTI_BSSID_SUPPORT,
9341 	[7] = WLAN_EXT_CAPA8_OPMODE_NOTIF,
9342 	[9] = WLAN_EXT_CAPA10_TWT_RESPONDER_SUPPORT,
9343 	[10] = WLAN_EXT_CAPA11_EMA_SUPPORT,
9344 };
9345 
9346 static const struct wiphy_iftype_ext_capab ath11k_iftypes_ext_capa[] = {
9347 	{
9348 		.extended_capabilities = ath11k_if_types_ext_capa,
9349 		.extended_capabilities_mask = ath11k_if_types_ext_capa,
9350 		.extended_capabilities_len = sizeof(ath11k_if_types_ext_capa),
9351 	}, {
9352 		.iftype = NL80211_IFTYPE_STATION,
9353 		.extended_capabilities = ath11k_if_types_ext_capa_sta,
9354 		.extended_capabilities_mask = ath11k_if_types_ext_capa_sta,
9355 		.extended_capabilities_len =
9356 				sizeof(ath11k_if_types_ext_capa_sta),
9357 	}, {
9358 		.iftype = NL80211_IFTYPE_AP,
9359 		.extended_capabilities = ath11k_if_types_ext_capa_ap,
9360 		.extended_capabilities_mask = ath11k_if_types_ext_capa_ap,
9361 		.extended_capabilities_len =
9362 				sizeof(ath11k_if_types_ext_capa_ap),
9363 	},
9364 };
9365 
__ath11k_mac_unregister(struct ath11k * ar)9366 static void __ath11k_mac_unregister(struct ath11k *ar)
9367 {
9368 	cancel_work_sync(&ar->regd_update_work);
9369 
9370 	ieee80211_unregister_hw(ar->hw);
9371 
9372 	idr_for_each(&ar->txmgmt_idr, ath11k_mac_tx_mgmt_pending_free, ar);
9373 	idr_destroy(&ar->txmgmt_idr);
9374 
9375 	kfree(ar->mac.sbands[NL80211_BAND_2GHZ].channels);
9376 	kfree(ar->mac.sbands[NL80211_BAND_5GHZ].channels);
9377 	kfree(ar->mac.sbands[NL80211_BAND_6GHZ].channels);
9378 
9379 	kfree(ar->hw->wiphy->iface_combinations[0].limits);
9380 	kfree(ar->hw->wiphy->iface_combinations);
9381 
9382 	SET_IEEE80211_DEV(ar->hw, NULL);
9383 }
9384 
ath11k_mac_unregister(struct ath11k_base * ab)9385 void ath11k_mac_unregister(struct ath11k_base *ab)
9386 {
9387 	struct ath11k *ar;
9388 	struct ath11k_pdev *pdev;
9389 	int i;
9390 
9391 	for (i = 0; i < ab->num_radios; i++) {
9392 		pdev = &ab->pdevs[i];
9393 		ar = pdev->ar;
9394 		if (!ar)
9395 			continue;
9396 
9397 		__ath11k_mac_unregister(ar);
9398 	}
9399 
9400 	ath11k_peer_rhash_tbl_destroy(ab);
9401 }
9402 
__ath11k_mac_register(struct ath11k * ar)9403 static int __ath11k_mac_register(struct ath11k *ar)
9404 {
9405 	struct ath11k_base *ab = ar->ab;
9406 	struct ath11k_pdev_cap *cap = &ar->pdev->cap;
9407 	static const u32 cipher_suites[] = {
9408 		WLAN_CIPHER_SUITE_TKIP,
9409 		WLAN_CIPHER_SUITE_CCMP,
9410 		WLAN_CIPHER_SUITE_AES_CMAC,
9411 		WLAN_CIPHER_SUITE_BIP_CMAC_256,
9412 		WLAN_CIPHER_SUITE_BIP_GMAC_128,
9413 		WLAN_CIPHER_SUITE_BIP_GMAC_256,
9414 		WLAN_CIPHER_SUITE_GCMP,
9415 		WLAN_CIPHER_SUITE_GCMP_256,
9416 		WLAN_CIPHER_SUITE_CCMP_256,
9417 	};
9418 	int ret;
9419 	u32 ht_cap = 0;
9420 
9421 	ath11k_pdev_caps_update(ar);
9422 
9423 	SET_IEEE80211_PERM_ADDR(ar->hw, ar->mac_addr);
9424 
9425 	SET_IEEE80211_DEV(ar->hw, ab->dev);
9426 
9427 	ret = ath11k_mac_setup_channels_rates(ar,
9428 					      cap->supported_bands);
9429 	if (ret)
9430 		goto err;
9431 
9432 	ath11k_mac_setup_ht_vht_cap(ar, cap, &ht_cap);
9433 	ath11k_mac_setup_he_cap(ar, cap);
9434 
9435 	ret = ath11k_mac_setup_iface_combinations(ar);
9436 	if (ret) {
9437 		ath11k_err(ar->ab, "failed to setup interface combinations: %d\n", ret);
9438 		goto err_free_channels;
9439 	}
9440 
9441 	ar->hw->wiphy->available_antennas_rx = cap->rx_chain_mask;
9442 	ar->hw->wiphy->available_antennas_tx = cap->tx_chain_mask;
9443 
9444 	ar->hw->wiphy->interface_modes = ab->hw_params.interface_modes;
9445 
9446 	if (ab->hw_params.single_pdev_only && ar->supports_6ghz)
9447 		ieee80211_hw_set(ar->hw, SINGLE_SCAN_ON_ALL_BANDS);
9448 
9449 	if (ab->hw_params.supports_multi_bssid) {
9450 		ieee80211_hw_set(ar->hw, SUPPORTS_MULTI_BSSID);
9451 		ieee80211_hw_set(ar->hw, SUPPORTS_ONLY_HE_MULTI_BSSID);
9452 	}
9453 
9454 	ieee80211_hw_set(ar->hw, SIGNAL_DBM);
9455 	ieee80211_hw_set(ar->hw, SUPPORTS_PS);
9456 	ieee80211_hw_set(ar->hw, SUPPORTS_DYNAMIC_PS);
9457 	ieee80211_hw_set(ar->hw, MFP_CAPABLE);
9458 	ieee80211_hw_set(ar->hw, REPORTS_TX_ACK_STATUS);
9459 	ieee80211_hw_set(ar->hw, HAS_RATE_CONTROL);
9460 	ieee80211_hw_set(ar->hw, AP_LINK_PS);
9461 	ieee80211_hw_set(ar->hw, SPECTRUM_MGMT);
9462 	ieee80211_hw_set(ar->hw, CONNECTION_MONITOR);
9463 	ieee80211_hw_set(ar->hw, SUPPORTS_PER_STA_GTK);
9464 	ieee80211_hw_set(ar->hw, WANT_MONITOR_VIF);
9465 	ieee80211_hw_set(ar->hw, CHANCTX_STA_CSA);
9466 	ieee80211_hw_set(ar->hw, QUEUE_CONTROL);
9467 	ieee80211_hw_set(ar->hw, SUPPORTS_TX_FRAG);
9468 	ieee80211_hw_set(ar->hw, REPORTS_LOW_ACK);
9469 
9470 	if (ath11k_frame_mode == ATH11K_HW_TXRX_ETHERNET) {
9471 		ieee80211_hw_set(ar->hw, SUPPORTS_TX_ENCAP_OFFLOAD);
9472 		ieee80211_hw_set(ar->hw, SUPPORTS_RX_DECAP_OFFLOAD);
9473 	}
9474 
9475 	if (cap->nss_ratio_enabled)
9476 		ieee80211_hw_set(ar->hw, SUPPORTS_VHT_EXT_NSS_BW);
9477 
9478 	if ((ht_cap & WMI_HT_CAP_ENABLED) || ar->supports_6ghz) {
9479 		ieee80211_hw_set(ar->hw, AMPDU_AGGREGATION);
9480 		ieee80211_hw_set(ar->hw, TX_AMPDU_SETUP_IN_HW);
9481 		ieee80211_hw_set(ar->hw, SUPPORTS_REORDERING_BUFFER);
9482 		ieee80211_hw_set(ar->hw, SUPPORTS_AMSDU_IN_AMPDU);
9483 		ieee80211_hw_set(ar->hw, USES_RSS);
9484 	}
9485 
9486 	ar->hw->wiphy->features |= NL80211_FEATURE_STATIC_SMPS;
9487 	ar->hw->wiphy->flags |= WIPHY_FLAG_IBSS_RSN;
9488 
9489 	/* TODO: Check if HT capability advertised from firmware is different
9490 	 * for each band for a dual band capable radio. It will be tricky to
9491 	 * handle it when the ht capability different for each band.
9492 	 */
9493 	if (ht_cap & WMI_HT_CAP_DYNAMIC_SMPS ||
9494 	    (ar->supports_6ghz && ab->hw_params.supports_dynamic_smps_6ghz))
9495 		ar->hw->wiphy->features |= NL80211_FEATURE_DYNAMIC_SMPS;
9496 
9497 	ar->hw->wiphy->max_scan_ssids = WLAN_SCAN_PARAMS_MAX_SSID;
9498 	ar->hw->wiphy->max_scan_ie_len = WLAN_SCAN_PARAMS_MAX_IE_LEN;
9499 
9500 	ar->hw->max_listen_interval = ATH11K_MAX_HW_LISTEN_INTERVAL;
9501 
9502 	ar->hw->wiphy->flags |= WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL;
9503 	ar->hw->wiphy->flags |= WIPHY_FLAG_HAS_CHANNEL_SWITCH;
9504 	ar->hw->wiphy->max_remain_on_channel_duration = 5000;
9505 
9506 	ar->hw->wiphy->flags |= WIPHY_FLAG_AP_UAPSD;
9507 	ar->hw->wiphy->features |= NL80211_FEATURE_AP_MODE_CHAN_WIDTH_CHANGE |
9508 				   NL80211_FEATURE_AP_SCAN;
9509 
9510 	ar->max_num_stations = TARGET_NUM_STATIONS(ab);
9511 	ar->max_num_peers = TARGET_NUM_PEERS_PDEV(ab);
9512 
9513 	ar->hw->wiphy->max_ap_assoc_sta = ar->max_num_stations;
9514 
9515 	if (test_bit(WMI_TLV_SERVICE_SPOOF_MAC_SUPPORT, ar->wmi->wmi_ab->svc_map)) {
9516 		ar->hw->wiphy->features |=
9517 			NL80211_FEATURE_SCAN_RANDOM_MAC_ADDR;
9518 	}
9519 
9520 	if (test_bit(WMI_TLV_SERVICE_NLO, ar->wmi->wmi_ab->svc_map)) {
9521 		ar->hw->wiphy->max_sched_scan_ssids = WMI_PNO_MAX_SUPP_NETWORKS;
9522 		ar->hw->wiphy->max_match_sets = WMI_PNO_MAX_SUPP_NETWORKS;
9523 		ar->hw->wiphy->max_sched_scan_ie_len = WMI_PNO_MAX_IE_LENGTH;
9524 		ar->hw->wiphy->max_sched_scan_plans = WMI_PNO_MAX_SCHED_SCAN_PLANS;
9525 		ar->hw->wiphy->max_sched_scan_plan_interval =
9526 			WMI_PNO_MAX_SCHED_SCAN_PLAN_INT;
9527 		ar->hw->wiphy->max_sched_scan_plan_iterations =
9528 			WMI_PNO_MAX_SCHED_SCAN_PLAN_ITRNS;
9529 		ar->hw->wiphy->features |= NL80211_FEATURE_ND_RANDOM_MAC_ADDR;
9530 	}
9531 
9532 	ret = ath11k_wow_init(ar);
9533 	if (ret) {
9534 		ath11k_warn(ar->ab, "failed to init wow: %d\n", ret);
9535 		goto err_free_if_combs;
9536 	}
9537 
9538 	if (test_bit(WMI_TLV_SERVICE_TX_DATA_MGMT_ACK_RSSI,
9539 		     ar->ab->wmi_ab.svc_map))
9540 		wiphy_ext_feature_set(ar->hw->wiphy,
9541 				      NL80211_EXT_FEATURE_ACK_SIGNAL_SUPPORT);
9542 
9543 	ar->hw->queues = ATH11K_HW_MAX_QUEUES;
9544 	ar->hw->wiphy->tx_queue_len = ATH11K_QUEUE_LEN;
9545 	ar->hw->offchannel_tx_hw_queue = ATH11K_HW_MAX_QUEUES - 1;
9546 	ar->hw->max_rx_aggregation_subframes = IEEE80211_MAX_AMPDU_BUF_HE;
9547 
9548 	ar->hw->vif_data_size = sizeof(struct ath11k_vif);
9549 	ar->hw->sta_data_size = sizeof(struct ath11k_sta);
9550 
9551 	wiphy_ext_feature_set(ar->hw->wiphy, NL80211_EXT_FEATURE_CQM_RSSI_LIST);
9552 	wiphy_ext_feature_set(ar->hw->wiphy, NL80211_EXT_FEATURE_STA_TX_PWR);
9553 	if (test_bit(WMI_TLV_SERVICE_BSS_COLOR_OFFLOAD,
9554 		     ar->ab->wmi_ab.svc_map)) {
9555 		wiphy_ext_feature_set(ar->hw->wiphy,
9556 				      NL80211_EXT_FEATURE_BSS_COLOR);
9557 		ieee80211_hw_set(ar->hw, DETECTS_COLOR_COLLISION);
9558 	}
9559 
9560 	ar->hw->wiphy->cipher_suites = cipher_suites;
9561 	ar->hw->wiphy->n_cipher_suites = ARRAY_SIZE(cipher_suites);
9562 
9563 	ar->hw->wiphy->iftype_ext_capab = ath11k_iftypes_ext_capa;
9564 	ar->hw->wiphy->num_iftype_ext_capab =
9565 		ARRAY_SIZE(ath11k_iftypes_ext_capa);
9566 
9567 	if (ar->supports_6ghz) {
9568 		wiphy_ext_feature_set(ar->hw->wiphy,
9569 				      NL80211_EXT_FEATURE_FILS_DISCOVERY);
9570 		wiphy_ext_feature_set(ar->hw->wiphy,
9571 				      NL80211_EXT_FEATURE_UNSOL_BCAST_PROBE_RESP);
9572 	}
9573 
9574 	wiphy_ext_feature_set(ar->hw->wiphy,
9575 			      NL80211_EXT_FEATURE_SET_SCAN_DWELL);
9576 
9577 	if (test_bit(WMI_TLV_SERVICE_RTT, ar->ab->wmi_ab.svc_map))
9578 		wiphy_ext_feature_set(ar->hw->wiphy,
9579 				      NL80211_EXT_FEATURE_ENABLE_FTM_RESPONDER);
9580 
9581 	ar->hw->wiphy->mbssid_max_interfaces = TARGET_NUM_VDEVS(ab);
9582 	ar->hw->wiphy->ema_max_profile_periodicity = TARGET_EMA_MAX_PROFILE_PERIOD;
9583 
9584 	ath11k_reg_init(ar);
9585 
9586 	if (!test_bit(ATH11K_FLAG_RAW_MODE, &ab->dev_flags)) {
9587 		ar->hw->netdev_features = NETIF_F_HW_CSUM;
9588 		ieee80211_hw_set(ar->hw, SW_CRYPTO_CONTROL);
9589 		ieee80211_hw_set(ar->hw, SUPPORT_FAST_XMIT);
9590 	}
9591 
9592 	if (test_bit(WMI_TLV_SERVICE_BIOS_SAR_SUPPORT, ar->ab->wmi_ab.svc_map) &&
9593 	    ab->hw_params.bios_sar_capa)
9594 		ar->hw->wiphy->sar_capa = ab->hw_params.bios_sar_capa;
9595 
9596 	ret = ieee80211_register_hw(ar->hw);
9597 	if (ret) {
9598 		ath11k_err(ar->ab, "ieee80211 registration failed: %d\n", ret);
9599 		goto err_free_if_combs;
9600 	}
9601 
9602 	if (!ab->hw_params.supports_monitor)
9603 		/* There's a race between calling ieee80211_register_hw()
9604 		 * and here where the monitor mode is enabled for a little
9605 		 * while. But that time is so short and in practise it make
9606 		 * a difference in real life.
9607 		 */
9608 		ar->hw->wiphy->interface_modes &= ~BIT(NL80211_IFTYPE_MONITOR);
9609 
9610 	/* Apply the regd received during initialization */
9611 	ret = ath11k_regd_update(ar);
9612 	if (ret) {
9613 		ath11k_err(ar->ab, "ath11k regd update failed: %d\n", ret);
9614 		goto err_unregister_hw;
9615 	}
9616 
9617 	if (ab->hw_params.current_cc_support && ab->new_alpha2[0]) {
9618 		struct wmi_set_current_country_params set_current_param = {};
9619 
9620 		memcpy(&set_current_param.alpha2, ab->new_alpha2, 2);
9621 		memcpy(&ar->alpha2, ab->new_alpha2, 2);
9622 		ret = ath11k_wmi_send_set_current_country_cmd(ar, &set_current_param);
9623 		if (ret)
9624 			ath11k_warn(ar->ab,
9625 				    "failed set cc code for mac register: %d\n", ret);
9626 	}
9627 
9628 	ret = ath11k_debugfs_register(ar);
9629 	if (ret) {
9630 		ath11k_err(ar->ab, "debugfs registration failed: %d\n", ret);
9631 		goto err_unregister_hw;
9632 	}
9633 
9634 	return 0;
9635 
9636 err_unregister_hw:
9637 	ieee80211_unregister_hw(ar->hw);
9638 
9639 err_free_if_combs:
9640 	kfree(ar->hw->wiphy->iface_combinations[0].limits);
9641 	kfree(ar->hw->wiphy->iface_combinations);
9642 
9643 err_free_channels:
9644 	kfree(ar->mac.sbands[NL80211_BAND_2GHZ].channels);
9645 	kfree(ar->mac.sbands[NL80211_BAND_5GHZ].channels);
9646 	kfree(ar->mac.sbands[NL80211_BAND_6GHZ].channels);
9647 
9648 err:
9649 	SET_IEEE80211_DEV(ar->hw, NULL);
9650 	return ret;
9651 }
9652 
ath11k_mac_register(struct ath11k_base * ab)9653 int ath11k_mac_register(struct ath11k_base *ab)
9654 {
9655 	struct ath11k *ar;
9656 	struct ath11k_pdev *pdev;
9657 	int i;
9658 	int ret;
9659 	u8 mac_addr[ETH_ALEN] = {0};
9660 
9661 	if (test_bit(ATH11K_FLAG_REGISTERED, &ab->dev_flags))
9662 		return 0;
9663 
9664 	/* Initialize channel counters frequency value in hertz */
9665 	ab->cc_freq_hz = IPQ8074_CC_FREQ_HERTZ;
9666 	ab->free_vdev_map = (1LL << (ab->num_radios * TARGET_NUM_VDEVS(ab))) - 1;
9667 
9668 	ret = ath11k_peer_rhash_tbl_init(ab);
9669 	if (ret)
9670 		return ret;
9671 
9672 	device_get_mac_address(ab->dev, mac_addr);
9673 
9674 	for (i = 0; i < ab->num_radios; i++) {
9675 		pdev = &ab->pdevs[i];
9676 		ar = pdev->ar;
9677 		if (ab->pdevs_macaddr_valid) {
9678 			ether_addr_copy(ar->mac_addr, pdev->mac_addr);
9679 		} else {
9680 			if (is_zero_ether_addr(mac_addr))
9681 				ether_addr_copy(ar->mac_addr, ab->mac_addr);
9682 			else
9683 				ether_addr_copy(ar->mac_addr, mac_addr);
9684 			ar->mac_addr[4] += i;
9685 		}
9686 
9687 		idr_init(&ar->txmgmt_idr);
9688 		spin_lock_init(&ar->txmgmt_idr_lock);
9689 
9690 		ret = __ath11k_mac_register(ar);
9691 		if (ret)
9692 			goto err_cleanup;
9693 
9694 		init_waitqueue_head(&ar->txmgmt_empty_waitq);
9695 	}
9696 
9697 	return 0;
9698 
9699 err_cleanup:
9700 	for (i = i - 1; i >= 0; i--) {
9701 		pdev = &ab->pdevs[i];
9702 		ar = pdev->ar;
9703 		__ath11k_mac_unregister(ar);
9704 	}
9705 
9706 	ath11k_peer_rhash_tbl_destroy(ab);
9707 
9708 	return ret;
9709 }
9710 
ath11k_mac_allocate(struct ath11k_base * ab)9711 int ath11k_mac_allocate(struct ath11k_base *ab)
9712 {
9713 	struct ieee80211_hw *hw;
9714 	struct ath11k *ar;
9715 	struct ath11k_pdev *pdev;
9716 	int ret;
9717 	int i;
9718 
9719 	if (test_bit(ATH11K_FLAG_REGISTERED, &ab->dev_flags))
9720 		return 0;
9721 
9722 	for (i = 0; i < ab->num_radios; i++) {
9723 		pdev = &ab->pdevs[i];
9724 		hw = ieee80211_alloc_hw(sizeof(struct ath11k), &ath11k_ops);
9725 		if (!hw) {
9726 			ath11k_warn(ab, "failed to allocate mac80211 hw device\n");
9727 			ret = -ENOMEM;
9728 			goto err_free_mac;
9729 		}
9730 
9731 		ar = hw->priv;
9732 		ar->hw = hw;
9733 		ar->ab = ab;
9734 		ar->pdev = pdev;
9735 		ar->pdev_idx = i;
9736 		ar->lmac_id = ath11k_hw_get_mac_from_pdev_id(&ab->hw_params, i);
9737 
9738 		ar->wmi = &ab->wmi_ab.wmi[i];
9739 		/* FIXME wmi[0] is already initialized during attach,
9740 		 * Should we do this again?
9741 		 */
9742 		ath11k_wmi_pdev_attach(ab, i);
9743 
9744 		ar->cfg_tx_chainmask = pdev->cap.tx_chain_mask;
9745 		ar->cfg_rx_chainmask = pdev->cap.rx_chain_mask;
9746 		ar->num_tx_chains = get_num_chains(pdev->cap.tx_chain_mask);
9747 		ar->num_rx_chains = get_num_chains(pdev->cap.rx_chain_mask);
9748 
9749 		pdev->ar = ar;
9750 		spin_lock_init(&ar->data_lock);
9751 		INIT_LIST_HEAD(&ar->arvifs);
9752 		INIT_LIST_HEAD(&ar->ppdu_stats_info);
9753 		mutex_init(&ar->conf_mutex);
9754 		init_completion(&ar->vdev_setup_done);
9755 		init_completion(&ar->vdev_delete_done);
9756 		init_completion(&ar->peer_assoc_done);
9757 		init_completion(&ar->peer_delete_done);
9758 		init_completion(&ar->install_key_done);
9759 		init_completion(&ar->bss_survey_done);
9760 		init_completion(&ar->scan.started);
9761 		init_completion(&ar->scan.completed);
9762 		init_completion(&ar->scan.on_channel);
9763 		init_completion(&ar->thermal.wmi_sync);
9764 
9765 		INIT_DELAYED_WORK(&ar->scan.timeout, ath11k_scan_timeout_work);
9766 		INIT_WORK(&ar->regd_update_work, ath11k_regd_update_work);
9767 
9768 		INIT_WORK(&ar->wmi_mgmt_tx_work, ath11k_mgmt_over_wmi_tx_work);
9769 		skb_queue_head_init(&ar->wmi_mgmt_tx_queue);
9770 
9771 		clear_bit(ATH11K_FLAG_MONITOR_STARTED, &ar->monitor_flags);
9772 
9773 		ar->monitor_vdev_id = -1;
9774 		clear_bit(ATH11K_FLAG_MONITOR_VDEV_CREATED, &ar->monitor_flags);
9775 		ar->vdev_id_11d_scan = ATH11K_11D_INVALID_VDEV_ID;
9776 		init_completion(&ar->completed_11d_scan);
9777 
9778 		ath11k_fw_stats_init(ar);
9779 	}
9780 
9781 	return 0;
9782 
9783 err_free_mac:
9784 	ath11k_mac_destroy(ab);
9785 
9786 	return ret;
9787 }
9788 
ath11k_mac_destroy(struct ath11k_base * ab)9789 void ath11k_mac_destroy(struct ath11k_base *ab)
9790 {
9791 	struct ath11k *ar;
9792 	struct ath11k_pdev *pdev;
9793 	int i;
9794 
9795 	for (i = 0; i < ab->num_radios; i++) {
9796 		pdev = &ab->pdevs[i];
9797 		ar = pdev->ar;
9798 		if (!ar)
9799 			continue;
9800 
9801 		ath11k_fw_stats_free(&ar->fw_stats);
9802 		ieee80211_free_hw(ar->hw);
9803 		pdev->ar = NULL;
9804 	}
9805 }
9806 
ath11k_mac_vif_set_keepalive(struct ath11k_vif * arvif,enum wmi_sta_keepalive_method method,u32 interval)9807 int ath11k_mac_vif_set_keepalive(struct ath11k_vif *arvif,
9808 				 enum wmi_sta_keepalive_method method,
9809 				 u32 interval)
9810 {
9811 	struct ath11k *ar = arvif->ar;
9812 	struct wmi_sta_keepalive_arg arg = {};
9813 	int ret;
9814 
9815 	lockdep_assert_held(&ar->conf_mutex);
9816 
9817 	if (arvif->vdev_type != WMI_VDEV_TYPE_STA)
9818 		return 0;
9819 
9820 	if (!test_bit(WMI_TLV_SERVICE_STA_KEEP_ALIVE, ar->ab->wmi_ab.svc_map))
9821 		return 0;
9822 
9823 	arg.vdev_id = arvif->vdev_id;
9824 	arg.enabled = 1;
9825 	arg.method = method;
9826 	arg.interval = interval;
9827 
9828 	ret = ath11k_wmi_sta_keepalive(ar, &arg);
9829 	if (ret) {
9830 		ath11k_warn(ar->ab, "failed to set keepalive on vdev %i: %d\n",
9831 			    arvif->vdev_id, ret);
9832 		return ret;
9833 	}
9834 
9835 	return 0;
9836 }
9837