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