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