xref: /linux/drivers/net/wireless/ath/ath12k/mac.c (revision 91ec2035134982b98fab0609a9fd8480e8217dc1)
1 // SPDX-License-Identifier: BSD-3-Clause-Clear
2 /*
3  * Copyright (c) 2018-2021 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 
11 #include "mac.h"
12 #include "core.h"
13 #include "debug.h"
14 #include "wmi.h"
15 #include "hw.h"
16 #include "dp_tx.h"
17 #include "dp_rx.h"
18 #include "testmode.h"
19 #include "peer.h"
20 #include "debugfs.h"
21 #include "hif.h"
22 #include "wow.h"
23 #include "debugfs_sta.h"
24 #include "dp.h"
25 #include "dp_cmn.h"
26 
27 #define CHAN2G(_channel, _freq, _flags) { \
28 	.band                   = NL80211_BAND_2GHZ, \
29 	.hw_value               = (_channel), \
30 	.center_freq            = (_freq), \
31 	.flags                  = (_flags), \
32 	.max_antenna_gain       = 0, \
33 	.max_power              = 30, \
34 }
35 
36 #define CHAN5G(_channel, _freq, _flags) { \
37 	.band                   = NL80211_BAND_5GHZ, \
38 	.hw_value               = (_channel), \
39 	.center_freq            = (_freq), \
40 	.flags                  = (_flags), \
41 	.max_antenna_gain       = 0, \
42 	.max_power              = 30, \
43 }
44 
45 #define CHAN6G(_channel, _freq, _flags) { \
46 	.band                   = NL80211_BAND_6GHZ, \
47 	.hw_value               = (_channel), \
48 	.center_freq            = (_freq), \
49 	.flags                  = (_flags), \
50 	.max_antenna_gain       = 0, \
51 	.max_power              = 30, \
52 }
53 
54 #define ATH12K_5_9_GHZ_MIN_FREQ 5845
55 #define ATH12K_5_9_GHZ_MAX_FREQ 5885
56 
57 static const struct ieee80211_channel ath12k_2ghz_channels[] = {
58 	CHAN2G(1, 2412, 0),
59 	CHAN2G(2, 2417, 0),
60 	CHAN2G(3, 2422, 0),
61 	CHAN2G(4, 2427, 0),
62 	CHAN2G(5, 2432, 0),
63 	CHAN2G(6, 2437, 0),
64 	CHAN2G(7, 2442, 0),
65 	CHAN2G(8, 2447, 0),
66 	CHAN2G(9, 2452, 0),
67 	CHAN2G(10, 2457, 0),
68 	CHAN2G(11, 2462, 0),
69 	CHAN2G(12, 2467, 0),
70 	CHAN2G(13, 2472, 0),
71 	CHAN2G(14, 2484, 0),
72 };
73 
74 static const struct ieee80211_channel ath12k_5ghz_channels[] = {
75 	CHAN5G(36, 5180, 0),
76 	CHAN5G(40, 5200, 0),
77 	CHAN5G(44, 5220, 0),
78 	CHAN5G(48, 5240, 0),
79 	CHAN5G(52, 5260, 0),
80 	CHAN5G(56, 5280, 0),
81 	CHAN5G(60, 5300, 0),
82 	CHAN5G(64, 5320, 0),
83 	CHAN5G(100, 5500, 0),
84 	CHAN5G(104, 5520, 0),
85 	CHAN5G(108, 5540, 0),
86 	CHAN5G(112, 5560, 0),
87 	CHAN5G(116, 5580, 0),
88 	CHAN5G(120, 5600, 0),
89 	CHAN5G(124, 5620, 0),
90 	CHAN5G(128, 5640, 0),
91 	CHAN5G(132, 5660, 0),
92 	CHAN5G(136, 5680, 0),
93 	CHAN5G(140, 5700, 0),
94 	CHAN5G(144, 5720, 0),
95 	CHAN5G(149, 5745, 0),
96 	CHAN5G(153, 5765, 0),
97 	CHAN5G(157, 5785, 0),
98 	CHAN5G(161, 5805, 0),
99 	CHAN5G(165, 5825, 0),
100 	CHAN5G(169, 5845, 0),
101 	CHAN5G(173, 5865, 0),
102 	CHAN5G(177, 5885, 0),
103 };
104 
105 static const struct ieee80211_channel ath12k_6ghz_channels[] = {
106 	/* Operating Class 136 */
107 	CHAN6G(2, 5935, 0),
108 
109 	/* Operating Classes 131-135 */
110 	CHAN6G(1, 5955, 0),
111 	CHAN6G(5, 5975, 0),
112 	CHAN6G(9, 5995, 0),
113 	CHAN6G(13, 6015, 0),
114 	CHAN6G(17, 6035, 0),
115 	CHAN6G(21, 6055, 0),
116 	CHAN6G(25, 6075, 0),
117 	CHAN6G(29, 6095, 0),
118 	CHAN6G(33, 6115, 0),
119 	CHAN6G(37, 6135, 0),
120 	CHAN6G(41, 6155, 0),
121 	CHAN6G(45, 6175, 0),
122 	CHAN6G(49, 6195, 0),
123 	CHAN6G(53, 6215, 0),
124 	CHAN6G(57, 6235, 0),
125 	CHAN6G(61, 6255, 0),
126 	CHAN6G(65, 6275, 0),
127 	CHAN6G(69, 6295, 0),
128 	CHAN6G(73, 6315, 0),
129 	CHAN6G(77, 6335, 0),
130 	CHAN6G(81, 6355, 0),
131 	CHAN6G(85, 6375, 0),
132 	CHAN6G(89, 6395, 0),
133 	CHAN6G(93, 6415, 0),
134 	CHAN6G(97, 6435, 0),
135 	CHAN6G(101, 6455, 0),
136 	CHAN6G(105, 6475, 0),
137 	CHAN6G(109, 6495, 0),
138 	CHAN6G(113, 6515, 0),
139 	CHAN6G(117, 6535, 0),
140 	CHAN6G(121, 6555, 0),
141 	CHAN6G(125, 6575, 0),
142 	CHAN6G(129, 6595, 0),
143 	CHAN6G(133, 6615, 0),
144 	CHAN6G(137, 6635, 0),
145 	CHAN6G(141, 6655, 0),
146 	CHAN6G(145, 6675, 0),
147 	CHAN6G(149, 6695, 0),
148 	CHAN6G(153, 6715, 0),
149 	CHAN6G(157, 6735, 0),
150 	CHAN6G(161, 6755, 0),
151 	CHAN6G(165, 6775, 0),
152 	CHAN6G(169, 6795, 0),
153 	CHAN6G(173, 6815, 0),
154 	CHAN6G(177, 6835, 0),
155 	CHAN6G(181, 6855, 0),
156 	CHAN6G(185, 6875, 0),
157 	CHAN6G(189, 6895, 0),
158 	CHAN6G(193, 6915, 0),
159 	CHAN6G(197, 6935, 0),
160 	CHAN6G(201, 6955, 0),
161 	CHAN6G(205, 6975, 0),
162 	CHAN6G(209, 6995, 0),
163 	CHAN6G(213, 7015, 0),
164 	CHAN6G(217, 7035, 0),
165 	CHAN6G(221, 7055, 0),
166 	CHAN6G(225, 7075, 0),
167 	CHAN6G(229, 7095, 0),
168 	CHAN6G(233, 7115, 0),
169 };
170 
171 #define ATH12K_MAC_RATE_A_M(bps, code) \
172 	{ .bitrate = (bps), .hw_value = (code),\
173 	  .flags = IEEE80211_RATE_MANDATORY_A }
174 
175 #define ATH12K_MAC_RATE_B(bps, code, code_short) \
176 	{ .bitrate = (bps), .hw_value = (code), .hw_value_short = (code_short),\
177 	  .flags = IEEE80211_RATE_SHORT_PREAMBLE }
178 
179 static struct ieee80211_rate ath12k_legacy_rates[] = {
180 	{ .bitrate = 10,
181 	  .hw_value = ATH12K_HW_RATE_CCK_LP_1M },
182 	ATH12K_MAC_RATE_B(20, ATH12K_HW_RATE_CCK_LP_2M,
183 			  ATH12K_HW_RATE_CCK_SP_2M),
184 	ATH12K_MAC_RATE_B(55, ATH12K_HW_RATE_CCK_LP_5_5M,
185 			  ATH12K_HW_RATE_CCK_SP_5_5M),
186 	ATH12K_MAC_RATE_B(110, ATH12K_HW_RATE_CCK_LP_11M,
187 			  ATH12K_HW_RATE_CCK_SP_11M),
188 	ATH12K_MAC_RATE_A_M(60, ATH12K_HW_RATE_OFDM_6M),
189 	ATH12K_MAC_RATE_A_M(90, ATH12K_HW_RATE_OFDM_9M),
190 	ATH12K_MAC_RATE_A_M(120, ATH12K_HW_RATE_OFDM_12M),
191 	ATH12K_MAC_RATE_A_M(180, ATH12K_HW_RATE_OFDM_18M),
192 	ATH12K_MAC_RATE_A_M(240, ATH12K_HW_RATE_OFDM_24M),
193 	ATH12K_MAC_RATE_A_M(360, ATH12K_HW_RATE_OFDM_36M),
194 	ATH12K_MAC_RATE_A_M(480, ATH12K_HW_RATE_OFDM_48M),
195 	ATH12K_MAC_RATE_A_M(540, ATH12K_HW_RATE_OFDM_54M),
196 };
197 
198 static const int
199 ath12k_phymodes[NUM_NL80211_BANDS][ATH12K_CHAN_WIDTH_NUM] = {
200 	[NL80211_BAND_2GHZ] = {
201 			[NL80211_CHAN_WIDTH_5] = MODE_UNKNOWN,
202 			[NL80211_CHAN_WIDTH_10] = MODE_UNKNOWN,
203 			[NL80211_CHAN_WIDTH_20_NOHT] = MODE_11BE_EHT20_2G,
204 			[NL80211_CHAN_WIDTH_20] = MODE_11BE_EHT20_2G,
205 			[NL80211_CHAN_WIDTH_40] = MODE_11BE_EHT40_2G,
206 			[NL80211_CHAN_WIDTH_80] = MODE_UNKNOWN,
207 			[NL80211_CHAN_WIDTH_80P80] = MODE_UNKNOWN,
208 			[NL80211_CHAN_WIDTH_160] = MODE_UNKNOWN,
209 			[NL80211_CHAN_WIDTH_320] = MODE_UNKNOWN,
210 	},
211 	[NL80211_BAND_5GHZ] = {
212 			[NL80211_CHAN_WIDTH_5] = MODE_UNKNOWN,
213 			[NL80211_CHAN_WIDTH_10] = MODE_UNKNOWN,
214 			[NL80211_CHAN_WIDTH_20_NOHT] = MODE_11BE_EHT20,
215 			[NL80211_CHAN_WIDTH_20] = MODE_11BE_EHT20,
216 			[NL80211_CHAN_WIDTH_40] = MODE_11BE_EHT40,
217 			[NL80211_CHAN_WIDTH_80] = MODE_11BE_EHT80,
218 			[NL80211_CHAN_WIDTH_160] = MODE_11BE_EHT160,
219 			[NL80211_CHAN_WIDTH_80P80] = MODE_UNKNOWN,
220 			[NL80211_CHAN_WIDTH_320] = MODE_11BE_EHT320,
221 	},
222 	[NL80211_BAND_6GHZ] = {
223 			[NL80211_CHAN_WIDTH_5] = MODE_UNKNOWN,
224 			[NL80211_CHAN_WIDTH_10] = MODE_UNKNOWN,
225 			[NL80211_CHAN_WIDTH_20_NOHT] = MODE_11BE_EHT20,
226 			[NL80211_CHAN_WIDTH_20] = MODE_11BE_EHT20,
227 			[NL80211_CHAN_WIDTH_40] = MODE_11BE_EHT40,
228 			[NL80211_CHAN_WIDTH_80] = MODE_11BE_EHT80,
229 			[NL80211_CHAN_WIDTH_160] = MODE_11BE_EHT160,
230 			[NL80211_CHAN_WIDTH_80P80] = MODE_UNKNOWN,
231 			[NL80211_CHAN_WIDTH_320] = MODE_11BE_EHT320,
232 	},
233 
234 };
235 
236 const struct htt_rx_ring_tlv_filter ath12k_mac_mon_status_filter_default = {
237 	.rx_filter = HTT_RX_FILTER_TLV_FLAGS_MPDU_START |
238 		     HTT_RX_FILTER_TLV_FLAGS_PPDU_END |
239 		     HTT_RX_FILTER_TLV_FLAGS_PPDU_END_STATUS_DONE |
240 		     HTT_RX_FILTER_TLV_FLAGS_PPDU_START_USER_INFO,
241 	.pkt_filter_flags0 = HTT_RX_FP_MGMT_FILTER_FLAGS0,
242 	.pkt_filter_flags1 = HTT_RX_FP_MGMT_FILTER_FLAGS1,
243 	.pkt_filter_flags2 = HTT_RX_FP_CTRL_FILTER_FLASG2,
244 	.pkt_filter_flags3 = HTT_RX_FP_DATA_FILTER_FLASG3 |
245 			     HTT_RX_FP_CTRL_FILTER_FLASG3
246 };
247 
248 #define ATH12K_MAC_FIRST_OFDM_RATE_IDX 4
249 #define ath12k_g_rates ath12k_legacy_rates
250 #define ath12k_g_rates_size (ARRAY_SIZE(ath12k_legacy_rates))
251 #define ath12k_a_rates (ath12k_legacy_rates + 4)
252 #define ath12k_a_rates_size (ARRAY_SIZE(ath12k_legacy_rates) - 4)
253 
254 #define ATH12K_MAC_SCAN_TIMEOUT_MSECS 200 /* in msecs */
255 
256 static const u32 ath12k_smps_map[] = {
257 	[WLAN_HT_CAP_SM_PS_STATIC] = WMI_PEER_SMPS_STATIC,
258 	[WLAN_HT_CAP_SM_PS_DYNAMIC] = WMI_PEER_SMPS_DYNAMIC,
259 	[WLAN_HT_CAP_SM_PS_INVALID] = WMI_PEER_SMPS_PS_NONE,
260 	[WLAN_HT_CAP_SM_PS_DISABLED] = WMI_PEER_SMPS_PS_NONE,
261 };
262 
263 static int ath12k_start_vdev_delay(struct ath12k *ar,
264 				   struct ath12k_link_vif *arvif);
265 static void ath12k_mac_stop(struct ath12k *ar);
266 static int ath12k_mac_vdev_create(struct ath12k *ar, struct ath12k_link_vif *arvif);
267 static int ath12k_mac_vdev_delete(struct ath12k *ar, struct ath12k_link_vif *arvif);
268 
ath12k_mac_phymode_str(enum wmi_phy_mode mode)269 static const char *ath12k_mac_phymode_str(enum wmi_phy_mode mode)
270 {
271 	switch (mode) {
272 	case MODE_11A:
273 		return "11a";
274 	case MODE_11G:
275 		return "11g";
276 	case MODE_11B:
277 		return "11b";
278 	case MODE_11GONLY:
279 		return "11gonly";
280 	case MODE_11NA_HT20:
281 		return "11na-ht20";
282 	case MODE_11NG_HT20:
283 		return "11ng-ht20";
284 	case MODE_11NA_HT40:
285 		return "11na-ht40";
286 	case MODE_11NG_HT40:
287 		return "11ng-ht40";
288 	case MODE_11AC_VHT20:
289 		return "11ac-vht20";
290 	case MODE_11AC_VHT40:
291 		return "11ac-vht40";
292 	case MODE_11AC_VHT80:
293 		return "11ac-vht80";
294 	case MODE_11AC_VHT160:
295 		return "11ac-vht160";
296 	case MODE_11AC_VHT80_80:
297 		return "11ac-vht80+80";
298 	case MODE_11AC_VHT20_2G:
299 		return "11ac-vht20-2g";
300 	case MODE_11AC_VHT40_2G:
301 		return "11ac-vht40-2g";
302 	case MODE_11AC_VHT80_2G:
303 		return "11ac-vht80-2g";
304 	case MODE_11AX_HE20:
305 		return "11ax-he20";
306 	case MODE_11AX_HE40:
307 		return "11ax-he40";
308 	case MODE_11AX_HE80:
309 		return "11ax-he80";
310 	case MODE_11AX_HE80_80:
311 		return "11ax-he80+80";
312 	case MODE_11AX_HE160:
313 		return "11ax-he160";
314 	case MODE_11AX_HE20_2G:
315 		return "11ax-he20-2g";
316 	case MODE_11AX_HE40_2G:
317 		return "11ax-he40-2g";
318 	case MODE_11AX_HE80_2G:
319 		return "11ax-he80-2g";
320 	case MODE_11BE_EHT20:
321 		return "11be-eht20";
322 	case MODE_11BE_EHT40:
323 		return "11be-eht40";
324 	case MODE_11BE_EHT80:
325 		return "11be-eht80";
326 	case MODE_11BE_EHT80_80:
327 		return "11be-eht80+80";
328 	case MODE_11BE_EHT160:
329 		return "11be-eht160";
330 	case MODE_11BE_EHT160_160:
331 		return "11be-eht160+160";
332 	case MODE_11BE_EHT320:
333 		return "11be-eht320";
334 	case MODE_11BE_EHT20_2G:
335 		return "11be-eht20-2g";
336 	case MODE_11BE_EHT40_2G:
337 		return "11be-eht40-2g";
338 	case MODE_UNKNOWN:
339 		/* skip */
340 		break;
341 
342 		/* no default handler to allow compiler to check that the
343 		 * enum is fully handled
344 		 */
345 	}
346 
347 	return "<unknown>";
348 }
349 
ath12k_mac_he_convert_tones_to_ru_tones(u16 tones)350 u16 ath12k_mac_he_convert_tones_to_ru_tones(u16 tones)
351 {
352 	switch (tones) {
353 	case 26:
354 		return RU_26;
355 	case 52:
356 		return RU_52;
357 	case 106:
358 		return RU_106;
359 	case 242:
360 		return RU_242;
361 	case 484:
362 		return RU_484;
363 	case 996:
364 		return RU_996;
365 	case (996 * 2):
366 		return RU_2X996;
367 	default:
368 		return RU_26;
369 	}
370 }
371 EXPORT_SYMBOL(ath12k_mac_he_convert_tones_to_ru_tones);
372 
ath12k_mac_eht_gi_to_nl80211_eht_gi(u8 sgi)373 enum nl80211_eht_gi ath12k_mac_eht_gi_to_nl80211_eht_gi(u8 sgi)
374 {
375 	switch (sgi) {
376 	case RX_MSDU_START_SGI_0_8_US:
377 		return NL80211_RATE_INFO_EHT_GI_0_8;
378 	case RX_MSDU_START_SGI_1_6_US:
379 		return NL80211_RATE_INFO_EHT_GI_1_6;
380 	case RX_MSDU_START_SGI_3_2_US:
381 		return NL80211_RATE_INFO_EHT_GI_3_2;
382 	default:
383 		return NL80211_RATE_INFO_EHT_GI_0_8;
384 	}
385 }
386 EXPORT_SYMBOL(ath12k_mac_eht_gi_to_nl80211_eht_gi);
387 
ath12k_mac_eht_ru_tones_to_nl80211_eht_ru_alloc(u16 ru_tones)388 enum nl80211_eht_ru_alloc ath12k_mac_eht_ru_tones_to_nl80211_eht_ru_alloc(u16 ru_tones)
389 {
390 	switch (ru_tones) {
391 	case 26:
392 		return NL80211_RATE_INFO_EHT_RU_ALLOC_26;
393 	case 52:
394 		return NL80211_RATE_INFO_EHT_RU_ALLOC_52;
395 	case (52 + 26):
396 		return NL80211_RATE_INFO_EHT_RU_ALLOC_52P26;
397 	case 106:
398 		return NL80211_RATE_INFO_EHT_RU_ALLOC_106;
399 	case (106 + 26):
400 		return NL80211_RATE_INFO_EHT_RU_ALLOC_106P26;
401 	case 242:
402 		return NL80211_RATE_INFO_EHT_RU_ALLOC_242;
403 	case 484:
404 		return NL80211_RATE_INFO_EHT_RU_ALLOC_484;
405 	case (484 + 242):
406 		return NL80211_RATE_INFO_EHT_RU_ALLOC_484P242;
407 	case 996:
408 		return NL80211_RATE_INFO_EHT_RU_ALLOC_996;
409 	case (996 + 484):
410 		return NL80211_RATE_INFO_EHT_RU_ALLOC_996P484;
411 	case (996 + 484 + 242):
412 		return NL80211_RATE_INFO_EHT_RU_ALLOC_996P484P242;
413 	case (2 * 996):
414 		return NL80211_RATE_INFO_EHT_RU_ALLOC_2x996;
415 	case (2 * 996 + 484):
416 		return NL80211_RATE_INFO_EHT_RU_ALLOC_2x996P484;
417 	case (3 * 996):
418 		return NL80211_RATE_INFO_EHT_RU_ALLOC_3x996;
419 	case (3 * 996 + 484):
420 		return NL80211_RATE_INFO_EHT_RU_ALLOC_3x996P484;
421 	case (4 * 996):
422 		return NL80211_RATE_INFO_EHT_RU_ALLOC_4x996;
423 	default:
424 		return NL80211_RATE_INFO_EHT_RU_ALLOC_26;
425 	}
426 }
427 EXPORT_SYMBOL(ath12k_mac_eht_ru_tones_to_nl80211_eht_ru_alloc);
428 
429 enum rate_info_bw
ath12k_mac_bw_to_mac80211_bw(enum ath12k_supported_bw bw)430 ath12k_mac_bw_to_mac80211_bw(enum ath12k_supported_bw bw)
431 {
432 	u8 ret = RATE_INFO_BW_20;
433 
434 	switch (bw) {
435 	case ATH12K_BW_20:
436 		ret = RATE_INFO_BW_20;
437 		break;
438 	case ATH12K_BW_40:
439 		ret = RATE_INFO_BW_40;
440 		break;
441 	case ATH12K_BW_80:
442 		ret = RATE_INFO_BW_80;
443 		break;
444 	case ATH12K_BW_160:
445 		ret = RATE_INFO_BW_160;
446 		break;
447 	case ATH12K_BW_320:
448 		ret = RATE_INFO_BW_320;
449 		break;
450 	}
451 
452 	return ret;
453 }
454 EXPORT_SYMBOL(ath12k_mac_bw_to_mac80211_bw);
455 
ath12k_mac_mac80211_bw_to_ath12k_bw(enum rate_info_bw bw)456 enum ath12k_supported_bw ath12k_mac_mac80211_bw_to_ath12k_bw(enum rate_info_bw bw)
457 {
458 	switch (bw) {
459 	case RATE_INFO_BW_20:
460 		return ATH12K_BW_20;
461 	case RATE_INFO_BW_40:
462 		return ATH12K_BW_40;
463 	case RATE_INFO_BW_80:
464 		return ATH12K_BW_80;
465 	case RATE_INFO_BW_160:
466 		return ATH12K_BW_160;
467 	case RATE_INFO_BW_320:
468 		return ATH12K_BW_320;
469 	default:
470 		return ATH12K_BW_20;
471 	}
472 }
473 
ath12k_mac_hw_ratecode_to_legacy_rate(u8 hw_rc,u8 preamble,u8 * rateidx,u16 * rate)474 int ath12k_mac_hw_ratecode_to_legacy_rate(u8 hw_rc, u8 preamble, u8 *rateidx,
475 					  u16 *rate)
476 {
477 	/* As default, it is OFDM rates */
478 	int i = ATH12K_MAC_FIRST_OFDM_RATE_IDX;
479 	int max_rates_idx = ath12k_g_rates_size;
480 
481 	if (preamble == WMI_RATE_PREAMBLE_CCK) {
482 		hw_rc &= ~ATH12K_HW_RATECODE_CCK_SHORT_PREAM_MASK;
483 		i = 0;
484 		max_rates_idx = ATH12K_MAC_FIRST_OFDM_RATE_IDX;
485 	}
486 
487 	while (i < max_rates_idx) {
488 		if (hw_rc == ath12k_legacy_rates[i].hw_value) {
489 			*rateidx = i;
490 			*rate = ath12k_legacy_rates[i].bitrate;
491 			return 0;
492 		}
493 		i++;
494 	}
495 
496 	return -EINVAL;
497 }
498 EXPORT_SYMBOL(ath12k_mac_hw_ratecode_to_legacy_rate);
499 
ath12k_mac_bitrate_to_idx(const struct ieee80211_supported_band * sband,u32 bitrate)500 u8 ath12k_mac_bitrate_to_idx(const struct ieee80211_supported_band *sband,
501 			     u32 bitrate)
502 {
503 	int i;
504 
505 	for (i = 0; i < sband->n_bitrates; i++)
506 		if (sband->bitrates[i].bitrate == bitrate)
507 			return i;
508 
509 	return 0;
510 }
511 
512 static u32
ath12k_mac_max_ht_nss(const u8 * ht_mcs_mask)513 ath12k_mac_max_ht_nss(const u8 *ht_mcs_mask)
514 {
515 	int nss;
516 
517 	for (nss = IEEE80211_HT_MCS_MASK_LEN - 1; nss >= 0; nss--)
518 		if (ht_mcs_mask[nss])
519 			return nss + 1;
520 
521 	return 1;
522 }
523 
524 static u32
ath12k_mac_max_vht_nss(const u16 * vht_mcs_mask)525 ath12k_mac_max_vht_nss(const u16 *vht_mcs_mask)
526 {
527 	int nss;
528 
529 	for (nss = NL80211_VHT_NSS_MAX - 1; nss >= 0; nss--)
530 		if (vht_mcs_mask[nss])
531 			return nss + 1;
532 
533 	return 1;
534 }
535 
536 static u32
ath12k_mac_max_he_nss(const u16 he_mcs_mask[NL80211_HE_NSS_MAX])537 ath12k_mac_max_he_nss(const u16 he_mcs_mask[NL80211_HE_NSS_MAX])
538 {
539 	int nss;
540 
541 	for (nss = NL80211_HE_NSS_MAX - 1; nss >= 0; nss--)
542 		if (he_mcs_mask[nss])
543 			return nss + 1;
544 
545 	return 1;
546 }
547 
548 static u32
ath12k_mac_max_eht_nss(const u16 eht_mcs_mask[NL80211_EHT_NSS_MAX])549 ath12k_mac_max_eht_nss(const u16 eht_mcs_mask[NL80211_EHT_NSS_MAX])
550 {
551 	int nss;
552 
553 	for (nss = NL80211_EHT_NSS_MAX - 1; nss >= 0; nss--)
554 		if (eht_mcs_mask[nss])
555 			return nss + 1;
556 
557 	return 1;
558 }
559 
560 static u32
ath12k_mac_max_eht_mcs_nss(const u8 * eht_mcs,int eht_mcs_set_size)561 ath12k_mac_max_eht_mcs_nss(const u8 *eht_mcs, int eht_mcs_set_size)
562 {
563 	int i;
564 	u8 nss = 0;
565 
566 	for (i = 0; i < eht_mcs_set_size; i++)
567 		nss = max(nss, u8_get_bits(eht_mcs[i], IEEE80211_EHT_MCS_NSS_RX));
568 
569 	return nss;
570 }
571 
ath12k_parse_mpdudensity(u8 mpdudensity)572 static u8 ath12k_parse_mpdudensity(u8 mpdudensity)
573 {
574 /*  From IEEE Std 802.11-2020 defined values for "Minimum MPDU Start Spacing":
575  *   0 for no restriction
576  *   1 for 1/4 us
577  *   2 for 1/2 us
578  *   3 for 1 us
579  *   4 for 2 us
580  *   5 for 4 us
581  *   6 for 8 us
582  *   7 for 16 us
583  */
584 	switch (mpdudensity) {
585 	case 0:
586 		return 0;
587 	case 1:
588 	case 2:
589 	case 3:
590 	/* Our lower layer calculations limit our precision to
591 	 * 1 microsecond
592 	 */
593 		return 1;
594 	case 4:
595 		return 2;
596 	case 5:
597 		return 4;
598 	case 6:
599 		return 8;
600 	case 7:
601 		return 16;
602 	default:
603 		return 0;
604 	}
605 }
606 
ath12k_mac_vif_link_chan(struct ieee80211_vif * vif,u8 link_id,struct cfg80211_chan_def * def)607 static int ath12k_mac_vif_link_chan(struct ieee80211_vif *vif, u8 link_id,
608 				    struct cfg80211_chan_def *def)
609 {
610 	struct ieee80211_bss_conf *link_conf;
611 	struct ieee80211_chanctx_conf *conf;
612 
613 	rcu_read_lock();
614 	link_conf = rcu_dereference(vif->link_conf[link_id]);
615 
616 	if (!link_conf) {
617 		rcu_read_unlock();
618 		return -ENOLINK;
619 	}
620 
621 	conf = rcu_dereference(link_conf->chanctx_conf);
622 	if (!conf) {
623 		rcu_read_unlock();
624 		return -ENOENT;
625 	}
626 	*def = conf->def;
627 	rcu_read_unlock();
628 
629 	return 0;
630 }
631 
632 static struct ath12k_link_vif *
ath12k_mac_get_tx_arvif(struct ath12k_link_vif * arvif,struct ieee80211_bss_conf * link_conf)633 ath12k_mac_get_tx_arvif(struct ath12k_link_vif *arvif,
634 			struct ieee80211_bss_conf *link_conf)
635 {
636 	struct ieee80211_bss_conf *tx_bss_conf;
637 	struct ath12k *ar = arvif->ar;
638 	struct ath12k_vif *tx_ahvif;
639 
640 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
641 
642 	tx_bss_conf = wiphy_dereference(ath12k_ar_to_hw(ar)->wiphy,
643 					link_conf->tx_bss_conf);
644 	if (tx_bss_conf) {
645 		tx_ahvif = ath12k_vif_to_ahvif(tx_bss_conf->vif);
646 		return wiphy_dereference(tx_ahvif->ah->hw->wiphy,
647 					 tx_ahvif->link[tx_bss_conf->link_id]);
648 	}
649 
650 	return NULL;
651 }
652 
ath12k_mac_get_tx_bssid(struct ath12k_link_vif * arvif)653 static const u8 *ath12k_mac_get_tx_bssid(struct ath12k_link_vif *arvif)
654 {
655 	struct ieee80211_bss_conf *link_conf;
656 	struct ath12k_link_vif *tx_arvif;
657 	struct ath12k *ar = arvif->ar;
658 
659 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
660 
661 	link_conf = ath12k_mac_get_link_bss_conf(arvif);
662 	if (!link_conf) {
663 		ath12k_warn(ar->ab,
664 			    "unable to access bss link conf for link %u required to retrieve transmitting link conf\n",
665 			    arvif->link_id);
666 		return NULL;
667 	}
668 	if (link_conf->vif->type == NL80211_IFTYPE_STATION) {
669 		if (link_conf->nontransmitted)
670 			return link_conf->transmitter_bssid;
671 	} else {
672 		tx_arvif = ath12k_mac_get_tx_arvif(arvif, link_conf);
673 		if (tx_arvif)
674 			return tx_arvif->bssid;
675 	}
676 
677 	return NULL;
678 }
679 
680 struct ieee80211_bss_conf *
ath12k_mac_get_link_bss_conf(struct ath12k_link_vif * arvif)681 ath12k_mac_get_link_bss_conf(struct ath12k_link_vif *arvif)
682 {
683 	struct ieee80211_vif *vif = arvif->ahvif->vif;
684 	struct ieee80211_bss_conf *link_conf;
685 	struct ath12k *ar = arvif->ar;
686 
687 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
688 
689 	if (arvif->link_id >= IEEE80211_MLD_MAX_NUM_LINKS)
690 		return NULL;
691 
692 	link_conf = wiphy_dereference(ath12k_ar_to_hw(ar)->wiphy,
693 				      vif->link_conf[arvif->link_id]);
694 
695 	return link_conf;
696 }
697 
ath12k_mac_get_link_sta(struct ath12k_link_sta * arsta)698 static struct ieee80211_link_sta *ath12k_mac_get_link_sta(struct ath12k_link_sta *arsta)
699 {
700 	struct ath12k_sta *ahsta = arsta->ahsta;
701 	struct ieee80211_sta *sta = ath12k_ahsta_to_sta(ahsta);
702 	struct ieee80211_link_sta *link_sta;
703 
704 	lockdep_assert_wiphy(ahsta->ahvif->ah->hw->wiphy);
705 
706 	if (arsta->link_id >= IEEE80211_MLD_MAX_NUM_LINKS)
707 		return NULL;
708 
709 	link_sta = wiphy_dereference(ahsta->ahvif->ah->hw->wiphy,
710 				     sta->link[arsta->link_id]);
711 
712 	return link_sta;
713 }
714 
ath12k_mac_bitrate_is_cck(int bitrate)715 static bool ath12k_mac_bitrate_is_cck(int bitrate)
716 {
717 	switch (bitrate) {
718 	case 10:
719 	case 20:
720 	case 55:
721 	case 110:
722 		return true;
723 	}
724 
725 	return false;
726 }
727 
ath12k_mac_hw_rate_to_idx(const struct ieee80211_supported_band * sband,u8 hw_rate,bool cck)728 u8 ath12k_mac_hw_rate_to_idx(const struct ieee80211_supported_band *sband,
729 			     u8 hw_rate, bool cck)
730 {
731 	const struct ieee80211_rate *rate;
732 	int i;
733 
734 	for (i = 0; i < sband->n_bitrates; i++) {
735 		rate = &sband->bitrates[i];
736 
737 		if (ath12k_mac_bitrate_is_cck(rate->bitrate) != cck)
738 			continue;
739 
740 		/* To handle 802.11a PPDU type */
741 		if ((!cck) && (rate->hw_value == hw_rate) &&
742 		    (rate->flags & IEEE80211_RATE_MANDATORY_A))
743 			return i;
744 		/* To handle 802.11b short PPDU type */
745 		else if (rate->flags & IEEE80211_RATE_SHORT_PREAMBLE &&
746 			 rate->hw_value_short == hw_rate)
747 			return i;
748 		/* To handle 802.11b long PPDU type */
749 		else if (rate->hw_value == hw_rate)
750 			return i;
751 	}
752 
753 	return 0;
754 }
755 
ath12k_mac_bitrate_to_rate(int bitrate)756 static u8 ath12k_mac_bitrate_to_rate(int bitrate)
757 {
758 	return DIV_ROUND_UP(bitrate, 5) |
759 	       (ath12k_mac_bitrate_is_cck(bitrate) ? BIT(7) : 0);
760 }
761 
ath12k_get_arvif_iter(void * data,u8 * mac,struct ieee80211_vif * vif)762 static void ath12k_get_arvif_iter(void *data, u8 *mac,
763 				  struct ieee80211_vif *vif)
764 {
765 	struct ath12k_vif_iter *arvif_iter = data;
766 	struct ath12k_vif *ahvif = ath12k_vif_to_ahvif(vif);
767 	unsigned long links_map = ahvif->links_map;
768 	struct ath12k_link_vif *arvif;
769 	u8 link_id;
770 
771 	for_each_set_bit(link_id, &links_map, IEEE80211_MLD_MAX_NUM_LINKS) {
772 		arvif = rcu_dereference(ahvif->link[link_id]);
773 
774 		if (WARN_ON(!arvif))
775 			continue;
776 
777 		if (!arvif->is_created)
778 			continue;
779 
780 		if (arvif->vdev_id == arvif_iter->vdev_id &&
781 		    arvif->ar == arvif_iter->ar) {
782 			arvif_iter->arvif = arvif;
783 			break;
784 		}
785 	}
786 }
787 
ath12k_mac_get_arvif(struct ath12k * ar,u32 vdev_id)788 struct ath12k_link_vif *ath12k_mac_get_arvif(struct ath12k *ar, u32 vdev_id)
789 {
790 	struct ath12k_vif_iter arvif_iter = {};
791 	u32 flags;
792 
793 	/* To use the arvif returned, caller must have held rcu read lock.
794 	 */
795 	lockdep_assert_in_rcu_read_lock();
796 	arvif_iter.vdev_id = vdev_id;
797 	arvif_iter.ar = ar;
798 
799 	flags = IEEE80211_IFACE_ITER_RESUME_ALL;
800 	ieee80211_iterate_active_interfaces_atomic(ath12k_ar_to_hw(ar),
801 						   flags,
802 						   ath12k_get_arvif_iter,
803 						   &arvif_iter);
804 	if (!arvif_iter.arvif) {
805 		ath12k_warn(ar->ab, "No VIF found for vdev %d\n", vdev_id);
806 		return NULL;
807 	}
808 
809 	return arvif_iter.arvif;
810 }
811 
ath12k_mac_get_arvif_by_vdev_id(struct ath12k_base * ab,u32 vdev_id)812 struct ath12k_link_vif *ath12k_mac_get_arvif_by_vdev_id(struct ath12k_base *ab,
813 							u32 vdev_id)
814 {
815 	int i;
816 	struct ath12k_pdev *pdev;
817 	struct ath12k_link_vif *arvif;
818 
819 	for (i = 0; i < ab->num_radios; i++) {
820 		pdev = rcu_dereference(ab->pdevs_active[i]);
821 		if (pdev && pdev->ar &&
822 		    (pdev->ar->allocated_vdev_map & (1LL << vdev_id))) {
823 			arvif = ath12k_mac_get_arvif(pdev->ar, vdev_id);
824 			if (arvif)
825 				return arvif;
826 		}
827 	}
828 
829 	return NULL;
830 }
831 
ath12k_mac_get_ar_by_vdev_id(struct ath12k_base * ab,u32 vdev_id)832 struct ath12k *ath12k_mac_get_ar_by_vdev_id(struct ath12k_base *ab, u32 vdev_id)
833 {
834 	int i;
835 	struct ath12k_pdev *pdev;
836 
837 	for (i = 0; i < ab->num_radios; i++) {
838 		pdev = rcu_dereference(ab->pdevs_active[i]);
839 		if (pdev && pdev->ar) {
840 			if (pdev->ar->allocated_vdev_map & (1LL << vdev_id))
841 				return pdev->ar;
842 		}
843 	}
844 
845 	return NULL;
846 }
847 
ath12k_mac_get_ar_by_pdev_id(struct ath12k_base * ab,u32 pdev_id)848 struct ath12k *ath12k_mac_get_ar_by_pdev_id(struct ath12k_base *ab, u32 pdev_id)
849 {
850 	int i;
851 	struct ath12k_pdev *pdev;
852 
853 	if (ab->hw_params->single_pdev_only) {
854 		pdev = rcu_dereference(ab->pdevs_active[0]);
855 		return pdev ? pdev->ar : NULL;
856 	}
857 
858 	if (WARN_ON(pdev_id > ab->num_radios))
859 		return NULL;
860 
861 	for (i = 0; i < ab->num_radios; i++) {
862 		if (ab->fw_mode == ATH12K_QMI_FIRMWARE_MODE_FTM)
863 			pdev = &ab->pdevs[i];
864 		else
865 			pdev = rcu_dereference(ab->pdevs_active[i]);
866 
867 		if (pdev && pdev->pdev_id == pdev_id)
868 			return (pdev->ar ? pdev->ar : NULL);
869 	}
870 
871 	return NULL;
872 }
873 
ath12k_mac_is_ml_arvif(struct ath12k_link_vif * arvif)874 static bool ath12k_mac_is_ml_arvif(struct ath12k_link_vif *arvif)
875 {
876 	struct ath12k_vif *ahvif = arvif->ahvif;
877 
878 	lockdep_assert_wiphy(ahvif->ah->hw->wiphy);
879 
880 	if (ahvif->vif->valid_links & BIT(arvif->link_id))
881 		return true;
882 
883 	return false;
884 }
885 
ath12k_mac_get_ar_by_chan(struct ieee80211_hw * hw,struct ieee80211_channel * channel)886 static struct ath12k *ath12k_mac_get_ar_by_chan(struct ieee80211_hw *hw,
887 						struct ieee80211_channel *channel)
888 {
889 	struct ath12k_hw *ah = hw->priv;
890 	struct ath12k *ar;
891 	int i;
892 
893 	ar = ah->radio;
894 
895 	if (ah->num_radio == 1)
896 		return ar;
897 
898 	for_each_ar(ah, ar, i) {
899 		if (channel->center_freq >= KHZ_TO_MHZ(ar->freq_range.start_freq) &&
900 		    channel->center_freq <= KHZ_TO_MHZ(ar->freq_range.end_freq))
901 			return ar;
902 	}
903 	return NULL;
904 }
905 
ath12k_get_ar_by_ctx(struct ieee80211_hw * hw,struct ieee80211_chanctx_conf * ctx)906 static struct ath12k *ath12k_get_ar_by_ctx(struct ieee80211_hw *hw,
907 					   struct ieee80211_chanctx_conf *ctx)
908 {
909 	if (!ctx)
910 		return NULL;
911 
912 	return ath12k_mac_get_ar_by_chan(hw, ctx->def.chan);
913 }
914 
ath12k_get_ar_by_vif(struct ieee80211_hw * hw,struct ieee80211_vif * vif,u8 link_id)915 struct ath12k *ath12k_get_ar_by_vif(struct ieee80211_hw *hw,
916 				    struct ieee80211_vif *vif,
917 				    u8 link_id)
918 {
919 	struct ath12k_vif *ahvif = ath12k_vif_to_ahvif(vif);
920 	struct ath12k_hw *ah = ath12k_hw_to_ah(hw);
921 	struct ath12k_link_vif *arvif;
922 
923 	lockdep_assert_wiphy(hw->wiphy);
924 
925 	/* If there is one pdev within ah, then we return
926 	 * ar directly.
927 	 */
928 	if (ah->num_radio == 1)
929 		return ah->radio;
930 
931 	if (!(ahvif->links_map & BIT(link_id)))
932 		return NULL;
933 
934 	arvif = wiphy_dereference(hw->wiphy, ahvif->link[link_id]);
935 	if (arvif && arvif->is_created)
936 		return arvif->ar;
937 
938 	return NULL;
939 }
940 
ath12k_mac_get_any_chanctx_conf_iter(struct ieee80211_hw * hw,struct ieee80211_chanctx_conf * conf,void * data)941 void ath12k_mac_get_any_chanctx_conf_iter(struct ieee80211_hw *hw,
942 					  struct ieee80211_chanctx_conf *conf,
943 					  void *data)
944 {
945 	struct ath12k_mac_get_any_chanctx_conf_arg *arg = data;
946 	struct ath12k *ctx_ar = ath12k_get_ar_by_ctx(hw, conf);
947 
948 	if (ctx_ar == arg->ar)
949 		arg->chanctx_conf = conf;
950 }
951 
ath12k_mac_get_vif_up(struct ath12k * ar)952 static struct ath12k_link_vif *ath12k_mac_get_vif_up(struct ath12k *ar)
953 {
954 	struct ath12k_link_vif *arvif;
955 
956 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
957 
958 	list_for_each_entry(arvif, &ar->arvifs, list) {
959 		if (arvif->is_up)
960 			return arvif;
961 	}
962 
963 	return NULL;
964 }
965 
ath12k_mac_band_match(enum nl80211_band band1,enum WMI_HOST_WLAN_BAND band2)966 static bool ath12k_mac_band_match(enum nl80211_band band1, enum WMI_HOST_WLAN_BAND band2)
967 {
968 	switch (band1) {
969 	case NL80211_BAND_2GHZ:
970 		if (band2 & WMI_HOST_WLAN_2GHZ_CAP)
971 			return true;
972 		break;
973 	case NL80211_BAND_5GHZ:
974 	case NL80211_BAND_6GHZ:
975 		if (band2 & WMI_HOST_WLAN_5GHZ_CAP)
976 			return true;
977 		break;
978 	default:
979 		return false;
980 	}
981 
982 	return false;
983 }
984 
ath12k_mac_get_target_pdev_id_from_vif(struct ath12k_link_vif * arvif)985 static u8 ath12k_mac_get_target_pdev_id_from_vif(struct ath12k_link_vif *arvif)
986 {
987 	struct ath12k *ar = arvif->ar;
988 	struct ath12k_base *ab = ar->ab;
989 	struct ieee80211_vif *vif = arvif->ahvif->vif;
990 	struct cfg80211_chan_def def;
991 	enum nl80211_band band;
992 	u8 pdev_id = ab->fw_pdev[0].pdev_id;
993 	int i;
994 
995 	if (WARN_ON(ath12k_mac_vif_link_chan(vif, arvif->link_id, &def)))
996 		return pdev_id;
997 
998 	band = def.chan->band;
999 
1000 	for (i = 0; i < ab->fw_pdev_count; i++) {
1001 		if (ath12k_mac_band_match(band, ab->fw_pdev[i].supported_bands))
1002 			return ab->fw_pdev[i].pdev_id;
1003 	}
1004 
1005 	return pdev_id;
1006 }
1007 
ath12k_mac_get_target_pdev_id(struct ath12k * ar)1008 u8 ath12k_mac_get_target_pdev_id(struct ath12k *ar)
1009 {
1010 	struct ath12k_link_vif *arvif;
1011 	struct ath12k_base *ab = ar->ab;
1012 
1013 	if (!ab->hw_params->single_pdev_only)
1014 		return ar->pdev->pdev_id;
1015 
1016 	arvif = ath12k_mac_get_vif_up(ar);
1017 
1018 	/* fw_pdev array has pdev ids derived from phy capability
1019 	 * service ready event (pdev_and_hw_link_ids).
1020 	 * If no vif is active, return default first index.
1021 	 */
1022 	if (!arvif)
1023 		return ar->ab->fw_pdev[0].pdev_id;
1024 
1025 	/* If active vif is found, return the pdev id matching chandef band */
1026 	return ath12k_mac_get_target_pdev_id_from_vif(arvif);
1027 }
1028 
ath12k_pdev_caps_update(struct ath12k * ar)1029 static void ath12k_pdev_caps_update(struct ath12k *ar)
1030 {
1031 	struct ath12k_base *ab = ar->ab;
1032 
1033 	ar->max_tx_power = ab->target_caps.hw_max_tx_power;
1034 
1035 	/* FIXME: Set min_tx_power to ab->target_caps.hw_min_tx_power.
1036 	 * But since the received value in svcrdy is same as hw_max_tx_power,
1037 	 * we can set ar->min_tx_power to 0 currently until
1038 	 * this is fixed in firmware
1039 	 */
1040 	ar->min_tx_power = 0;
1041 
1042 	ar->txpower_limit_2g = ar->max_tx_power;
1043 	ar->txpower_limit_5g = ar->max_tx_power;
1044 	ar->txpower_scale = WMI_HOST_TP_SCALE_MAX;
1045 }
1046 
ath12k_mac_txpower_recalc(struct ath12k * ar)1047 static int ath12k_mac_txpower_recalc(struct ath12k *ar)
1048 {
1049 	struct ath12k_pdev *pdev = ar->pdev;
1050 	struct ath12k_link_vif *arvif;
1051 	int ret, txpower = -1;
1052 	u32 param;
1053 
1054 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
1055 
1056 	list_for_each_entry(arvif, &ar->arvifs, list) {
1057 		if (arvif->txpower <= 0)
1058 			continue;
1059 
1060 		if (txpower == -1)
1061 			txpower = arvif->txpower;
1062 		else
1063 			txpower = min(txpower, arvif->txpower);
1064 	}
1065 
1066 	if (txpower == -1)
1067 		return 0;
1068 
1069 	/* txpwr is set as 2 units per dBm in FW*/
1070 	txpower = min_t(u32, max_t(u32, ar->min_tx_power, txpower),
1071 			ar->max_tx_power) * 2;
1072 
1073 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "txpower to set in hw %d\n",
1074 		   txpower / 2);
1075 
1076 	if ((pdev->cap.supported_bands & WMI_HOST_WLAN_2GHZ_CAP) &&
1077 	    ar->txpower_limit_2g != txpower) {
1078 		param = WMI_PDEV_PARAM_TXPOWER_LIMIT2G;
1079 		ret = ath12k_wmi_pdev_set_param(ar, param,
1080 						txpower, ar->pdev->pdev_id);
1081 		if (ret)
1082 			goto fail;
1083 		ar->txpower_limit_2g = txpower;
1084 	}
1085 
1086 	if ((pdev->cap.supported_bands & WMI_HOST_WLAN_5GHZ_CAP) &&
1087 	    ar->txpower_limit_5g != txpower) {
1088 		param = WMI_PDEV_PARAM_TXPOWER_LIMIT5G;
1089 		ret = ath12k_wmi_pdev_set_param(ar, param,
1090 						txpower, ar->pdev->pdev_id);
1091 		if (ret)
1092 			goto fail;
1093 		ar->txpower_limit_5g = txpower;
1094 	}
1095 
1096 	return 0;
1097 
1098 fail:
1099 	ath12k_warn(ar->ab, "failed to recalc txpower limit %d using pdev param %d: %d\n",
1100 		    txpower / 2, param, ret);
1101 	return ret;
1102 }
1103 
ath12k_recalc_rtscts_prot(struct ath12k_link_vif * arvif)1104 static int ath12k_recalc_rtscts_prot(struct ath12k_link_vif *arvif)
1105 {
1106 	struct ath12k *ar = arvif->ar;
1107 	u32 vdev_param, rts_cts;
1108 	int ret;
1109 
1110 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
1111 
1112 	vdev_param = WMI_VDEV_PARAM_ENABLE_RTSCTS;
1113 
1114 	/* Enable RTS/CTS protection for sw retries (when legacy stations
1115 	 * are in BSS) or by default only for second rate series.
1116 	 * TODO: Check if we need to enable CTS 2 Self in any case
1117 	 */
1118 	rts_cts = WMI_USE_RTS_CTS;
1119 
1120 	if (arvif->num_legacy_stations > 0)
1121 		rts_cts |= WMI_RTSCTS_ACROSS_SW_RETRIES << 4;
1122 	else
1123 		rts_cts |= WMI_RTSCTS_FOR_SECOND_RATESERIES << 4;
1124 
1125 	/* Need not send duplicate param value to firmware */
1126 	if (arvif->rtscts_prot_mode == rts_cts)
1127 		return 0;
1128 
1129 	arvif->rtscts_prot_mode = rts_cts;
1130 
1131 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac vdev %d recalc rts/cts prot %d\n",
1132 		   arvif->vdev_id, rts_cts);
1133 
1134 	ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
1135 					    vdev_param, rts_cts);
1136 	if (ret)
1137 		ath12k_warn(ar->ab, "failed to recalculate rts/cts prot for vdev %d: %d\n",
1138 			    arvif->vdev_id, ret);
1139 
1140 	return ret;
1141 }
1142 
ath12k_mac_set_kickout(struct ath12k_link_vif * arvif)1143 static int ath12k_mac_set_kickout(struct ath12k_link_vif *arvif)
1144 {
1145 	struct ath12k *ar = arvif->ar;
1146 	u32 param;
1147 	int ret;
1148 
1149 	ret = ath12k_wmi_pdev_set_param(ar, WMI_PDEV_PARAM_STA_KICKOUT_TH,
1150 					ATH12K_KICKOUT_THRESHOLD,
1151 					ar->pdev->pdev_id);
1152 	if (ret) {
1153 		ath12k_warn(ar->ab, "failed to set kickout threshold on vdev %i: %d\n",
1154 			    arvif->vdev_id, ret);
1155 		return ret;
1156 	}
1157 
1158 	param = WMI_VDEV_PARAM_AP_KEEPALIVE_MIN_IDLE_INACTIVE_TIME_SECS;
1159 	ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id, param,
1160 					    ATH12K_KEEPALIVE_MIN_IDLE);
1161 	if (ret) {
1162 		ath12k_warn(ar->ab, "failed to set keepalive minimum idle time on vdev %i: %d\n",
1163 			    arvif->vdev_id, ret);
1164 		return ret;
1165 	}
1166 
1167 	param = WMI_VDEV_PARAM_AP_KEEPALIVE_MAX_IDLE_INACTIVE_TIME_SECS;
1168 	ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id, param,
1169 					    ATH12K_KEEPALIVE_MAX_IDLE);
1170 	if (ret) {
1171 		ath12k_warn(ar->ab, "failed to set keepalive maximum idle time on vdev %i: %d\n",
1172 			    arvif->vdev_id, ret);
1173 		return ret;
1174 	}
1175 
1176 	param = WMI_VDEV_PARAM_AP_KEEPALIVE_MAX_UNRESPONSIVE_TIME_SECS;
1177 	ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id, param,
1178 					    ATH12K_KEEPALIVE_MAX_UNRESPONSIVE);
1179 	if (ret) {
1180 		ath12k_warn(ar->ab, "failed to set keepalive maximum unresponsive time on vdev %i: %d\n",
1181 			    arvif->vdev_id, ret);
1182 		return ret;
1183 	}
1184 
1185 	return 0;
1186 }
1187 
ath12k_mac_link_sta_rhash_cleanup(void * data,struct ieee80211_sta * sta)1188 static void ath12k_mac_link_sta_rhash_cleanup(void *data, struct ieee80211_sta *sta)
1189 {
1190 	u8 link_id;
1191 	unsigned long links_map;
1192 	struct ath12k_sta *ahsta;
1193 	struct ath12k *ar = data;
1194 	struct ath12k_link_sta *arsta;
1195 	struct ath12k_link_vif *arvif;
1196 	struct ath12k_base *ab = ar->ab;
1197 
1198 	ahsta = ath12k_sta_to_ahsta(sta);
1199 	links_map = ahsta->links_map;
1200 
1201 	rcu_read_lock();
1202 	for_each_set_bit(link_id, &links_map, IEEE80211_MLD_MAX_NUM_LINKS) {
1203 		arsta = rcu_dereference(ahsta->link[link_id]);
1204 		if (!arsta)
1205 			continue;
1206 		arvif = arsta->arvif;
1207 		if (!(arvif->ar == ar))
1208 			continue;
1209 
1210 		spin_lock_bh(&ab->base_lock);
1211 		ath12k_link_sta_rhash_delete(ab, arsta);
1212 		spin_unlock_bh(&ab->base_lock);
1213 	}
1214 	rcu_read_unlock();
1215 }
1216 
ath12k_mac_peer_cleanup_all(struct ath12k * ar)1217 void ath12k_mac_peer_cleanup_all(struct ath12k *ar)
1218 {
1219 	struct ath12k_dp_link_peer *peer, *tmp;
1220 	struct ath12k_base *ab = ar->ab;
1221 	struct ath12k_dp *dp = ath12k_ab_to_dp(ab);
1222 	struct ath12k_link_vif *arvif, *tmp_vif;
1223 	struct ath12k_dp_hw *dp_hw = &ar->ah->dp_hw;
1224 	struct ath12k_dp_peer *dp_peer = NULL;
1225 	u16 peerid_index;
1226 	struct list_head peers;
1227 
1228 	INIT_LIST_HEAD(&peers);
1229 
1230 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
1231 
1232 	spin_lock_bh(&dp->dp_lock);
1233 	list_for_each_entry_safe(peer, tmp, &dp->peers, list) {
1234 		/* Skip Rx TID cleanup for self peer */
1235 		if (peer->sta && peer->dp_peer)
1236 			ath12k_dp_rx_peer_tid_cleanup(ar, peer);
1237 
1238 		/* cleanup dp peer */
1239 		spin_lock_bh(&dp_hw->peer_lock);
1240 		dp_peer = peer->dp_peer;
1241 		if (dp_peer) {
1242 			peerid_index = ath12k_dp_peer_get_peerid_index(dp, peer->peer_id);
1243 			rcu_assign_pointer(dp_peer->link_peers[peer->link_id], NULL);
1244 			WRITE_ONCE(dp_peer->link_peers_map,
1245 				   READ_ONCE(dp_peer->link_peers_map) & ~BIT(peer->link_id));
1246 			rcu_assign_pointer(dp_hw->dp_peers[peerid_index], NULL);
1247 		}
1248 		spin_unlock_bh(&dp_hw->peer_lock);
1249 
1250 		ath12k_dp_link_peer_rhash_delete(dp, peer);
1251 
1252 		list_move(&peer->list, &peers);
1253 	}
1254 	spin_unlock_bh(&dp->dp_lock);
1255 
1256 	synchronize_rcu();
1257 
1258 	list_for_each_entry_safe(peer, tmp, &peers, list) {
1259 		ath12k_dp_link_peer_free(peer);
1260 	}
1261 
1262 	ar->num_peers = 0;
1263 	ar->num_stations = 0;
1264 
1265 	/* Cleanup rhash table maintained for arsta by iterating over sta */
1266 	ieee80211_iterate_stations_mtx(ar->ah->hw, ath12k_mac_link_sta_rhash_cleanup,
1267 				       ar);
1268 
1269 	/* Delete all the self dp_peers on asserted radio */
1270 	list_for_each_entry_safe_reverse(arvif, tmp_vif, &ar->arvifs, list) {
1271 		if ((arvif->ahvif->vdev_type == WMI_VDEV_TYPE_AP) &&
1272 		    (arvif->link_id < IEEE80211_MLD_MAX_NUM_LINKS)) {
1273 			ath12k_dp_peer_delete(dp_hw, arvif->bssid, NULL);
1274 			arvif->num_stations = 0;
1275 		}
1276 	}
1277 }
1278 
ath12k_mac_dp_peer_cleanup(struct ath12k_hw * ah)1279 void ath12k_mac_dp_peer_cleanup(struct ath12k_hw *ah)
1280 {
1281 	struct list_head peers;
1282 	struct ath12k_dp_peer *dp_peer, *tmp;
1283 	struct ath12k_dp_hw *dp_hw = &ah->dp_hw;
1284 
1285 	lockdep_assert_wiphy(ah->hw->wiphy);
1286 
1287 	INIT_LIST_HEAD(&peers);
1288 
1289 	spin_lock_bh(&dp_hw->peer_lock);
1290 	list_for_each_entry_safe(dp_peer, tmp, &dp_hw->dp_peers_list, list) {
1291 		if (dp_peer->is_mlo) {
1292 			if (dp_peer->peer_id != ATH12K_MLO_PEER_ID_PENDING)
1293 				rcu_assign_pointer(dp_hw->dp_peers[dp_peer->peer_id],
1294 						   NULL);
1295 			ath12k_peer_ml_free(ah, ath12k_sta_to_ahsta(dp_peer->sta));
1296 		}
1297 
1298 		list_move(&dp_peer->list, &peers);
1299 	}
1300 
1301 	spin_unlock_bh(&dp_hw->peer_lock);
1302 
1303 	synchronize_rcu();
1304 
1305 	list_for_each_entry_safe(dp_peer, tmp, &peers, list) {
1306 		list_del(&dp_peer->list);
1307 		kfree(dp_peer);
1308 	}
1309 }
1310 
ath12k_mac_vdev_setup_sync(struct ath12k * ar)1311 static int ath12k_mac_vdev_setup_sync(struct ath12k *ar)
1312 {
1313 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
1314 
1315 	if (test_bit(ATH12K_FLAG_CRASH_FLUSH, &ar->ab->dev_flags))
1316 		return -ESHUTDOWN;
1317 
1318 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "vdev setup timeout %d\n",
1319 		   ATH12K_VDEV_SETUP_TIMEOUT_HZ);
1320 
1321 	if (!wait_for_completion_timeout(&ar->vdev_setup_done,
1322 					 ATH12K_VDEV_SETUP_TIMEOUT_HZ))
1323 		return -ETIMEDOUT;
1324 
1325 	return ar->last_wmi_vdev_start_status ? -EINVAL : 0;
1326 }
1327 
ath12k_monitor_vdev_up(struct ath12k * ar,int vdev_id)1328 static int ath12k_monitor_vdev_up(struct ath12k *ar, int vdev_id)
1329 {
1330 	struct ath12k_wmi_vdev_up_params params = {};
1331 	int ret;
1332 
1333 	params.vdev_id = vdev_id;
1334 	params.bssid = ar->mac_addr;
1335 	ret = ath12k_wmi_vdev_up(ar, &params);
1336 	if (ret) {
1337 		ath12k_warn(ar->ab, "failed to put up monitor vdev %i: %d\n",
1338 			    vdev_id, ret);
1339 		return ret;
1340 	}
1341 
1342 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac monitor vdev %i started\n",
1343 		   vdev_id);
1344 	return 0;
1345 }
1346 
ath12k_mac_monitor_vdev_start(struct ath12k * ar,int vdev_id,struct cfg80211_chan_def * chandef)1347 static int ath12k_mac_monitor_vdev_start(struct ath12k *ar, int vdev_id,
1348 					 struct cfg80211_chan_def *chandef)
1349 {
1350 	struct ieee80211_channel *channel;
1351 	struct wmi_vdev_start_req_arg arg = {};
1352 	struct ath12k_wmi_vdev_up_params params = {};
1353 	int ret;
1354 
1355 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
1356 
1357 	channel = chandef->chan;
1358 	arg.vdev_id = vdev_id;
1359 	arg.freq = channel->center_freq;
1360 	arg.band_center_freq1 = chandef->center_freq1;
1361 	arg.band_center_freq2 = chandef->center_freq2;
1362 	arg.mode = ath12k_phymodes[chandef->chan->band][chandef->width];
1363 	arg.chan_radar = !!(channel->flags & IEEE80211_CHAN_RADAR);
1364 
1365 	arg.min_power = 0;
1366 	arg.max_power = channel->max_power;
1367 	arg.max_reg_power = channel->max_reg_power;
1368 	arg.max_antenna_gain = channel->max_antenna_gain;
1369 
1370 	arg.pref_tx_streams = ar->num_tx_chains;
1371 	arg.pref_rx_streams = ar->num_rx_chains;
1372 	arg.punct_bitmap = 0xFFFFFFFF;
1373 
1374 	arg.passive |= !!(chandef->chan->flags & IEEE80211_CHAN_NO_IR);
1375 
1376 	reinit_completion(&ar->vdev_setup_done);
1377 	reinit_completion(&ar->vdev_delete_done);
1378 
1379 	ret = ath12k_wmi_vdev_start(ar, &arg, false);
1380 	if (ret) {
1381 		ath12k_warn(ar->ab, "failed to request monitor vdev %i start: %d\n",
1382 			    vdev_id, ret);
1383 		return ret;
1384 	}
1385 
1386 	ret = ath12k_mac_vdev_setup_sync(ar);
1387 	if (ret) {
1388 		ath12k_warn(ar->ab, "failed to synchronize setup for monitor vdev %i start: %d\n",
1389 			    vdev_id, ret);
1390 		return ret;
1391 	}
1392 
1393 	params.vdev_id = vdev_id;
1394 	params.bssid = ar->mac_addr;
1395 	ret = ath12k_wmi_vdev_up(ar, &params);
1396 	if (ret) {
1397 		ath12k_warn(ar->ab, "failed to put up monitor vdev %i: %d\n",
1398 			    vdev_id, ret);
1399 		goto vdev_stop;
1400 	}
1401 
1402 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac monitor vdev %i started\n",
1403 		   vdev_id);
1404 	return 0;
1405 
1406 vdev_stop:
1407 	ret = ath12k_wmi_vdev_stop(ar, vdev_id);
1408 	if (ret)
1409 		ath12k_warn(ar->ab, "failed to stop monitor vdev %i after start failure: %d\n",
1410 			    vdev_id, ret);
1411 	return ret;
1412 }
1413 
ath12k_mac_monitor_vdev_stop(struct ath12k * ar)1414 static int ath12k_mac_monitor_vdev_stop(struct ath12k *ar)
1415 {
1416 	int ret;
1417 
1418 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
1419 
1420 	reinit_completion(&ar->vdev_setup_done);
1421 
1422 	ret = ath12k_wmi_vdev_stop(ar, ar->monitor_vdev_id);
1423 	if (ret)
1424 		ath12k_warn(ar->ab, "failed to request monitor vdev %i stop: %d\n",
1425 			    ar->monitor_vdev_id, ret);
1426 
1427 	ret = ath12k_mac_vdev_setup_sync(ar);
1428 	if (ret)
1429 		ath12k_warn(ar->ab, "failed to synchronize monitor vdev %i stop: %d\n",
1430 			    ar->monitor_vdev_id, ret);
1431 
1432 	ret = ath12k_wmi_vdev_down(ar, ar->monitor_vdev_id);
1433 	if (ret)
1434 		ath12k_warn(ar->ab, "failed to put down monitor vdev %i: %d\n",
1435 			    ar->monitor_vdev_id, ret);
1436 
1437 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac monitor vdev %i stopped\n",
1438 		   ar->monitor_vdev_id);
1439 	return ret;
1440 }
1441 
ath12k_mac_monitor_vdev_delete(struct ath12k * ar)1442 static int ath12k_mac_monitor_vdev_delete(struct ath12k *ar)
1443 {
1444 	int ret;
1445 	unsigned long time_left;
1446 
1447 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
1448 
1449 	if (!ar->monitor_vdev_created)
1450 		return 0;
1451 
1452 	reinit_completion(&ar->vdev_delete_done);
1453 
1454 	ret = ath12k_wmi_vdev_delete(ar, ar->monitor_vdev_id);
1455 	if (ret) {
1456 		ath12k_warn(ar->ab, "failed to request wmi monitor vdev %i removal: %d\n",
1457 			    ar->monitor_vdev_id, ret);
1458 		return ret;
1459 	}
1460 
1461 	time_left = wait_for_completion_timeout(&ar->vdev_delete_done,
1462 						ATH12K_VDEV_DELETE_TIMEOUT_HZ);
1463 	if (time_left == 0) {
1464 		ath12k_warn(ar->ab, "Timeout in receiving vdev delete response\n");
1465 	} else {
1466 		ar->allocated_vdev_map &= ~(1LL << ar->monitor_vdev_id);
1467 		ar->ab->free_vdev_map |= 1LL << (ar->monitor_vdev_id);
1468 		ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac monitor vdev %d deleted\n",
1469 			   ar->monitor_vdev_id);
1470 		ar->num_created_vdevs--;
1471 		ar->monitor_vdev_id = -1;
1472 		ar->monitor_vdev_created = false;
1473 	}
1474 
1475 	return ret;
1476 }
1477 
ath12k_mac_monitor_start(struct ath12k * ar)1478 static int ath12k_mac_monitor_start(struct ath12k *ar)
1479 {
1480 	struct ath12k_mac_get_any_chanctx_conf_arg arg;
1481 	int ret;
1482 
1483 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
1484 
1485 	if (ar->monitor_started)
1486 		return 0;
1487 
1488 	arg.ar = ar;
1489 	arg.chanctx_conf = NULL;
1490 	ieee80211_iter_chan_contexts_atomic(ath12k_ar_to_hw(ar),
1491 					    ath12k_mac_get_any_chanctx_conf_iter,
1492 					    &arg);
1493 	if (!arg.chanctx_conf)
1494 		return 0;
1495 
1496 	ret = ath12k_mac_monitor_vdev_start(ar, ar->monitor_vdev_id,
1497 					    &arg.chanctx_conf->def);
1498 	if (ret) {
1499 		ath12k_warn(ar->ab, "failed to start monitor vdev: %d\n", ret);
1500 		return ret;
1501 	}
1502 
1503 	ret = ath12k_dp_tx_htt_monitor_mode_ring_config(ar, false);
1504 	if (ret) {
1505 		ath12k_warn(ar->ab, "fail to set monitor filter: %d\n", ret);
1506 		return ret;
1507 	}
1508 
1509 	ar->monitor_started = true;
1510 	ar->num_started_vdevs++;
1511 
1512 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac monitor started\n");
1513 
1514 	return 0;
1515 }
1516 
ath12k_mac_monitor_stop(struct ath12k * ar)1517 static int ath12k_mac_monitor_stop(struct ath12k *ar)
1518 {
1519 	int ret;
1520 
1521 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
1522 
1523 	if (!ar->monitor_started)
1524 		return 0;
1525 
1526 	ret = ath12k_mac_monitor_vdev_stop(ar);
1527 	if (ret) {
1528 		ath12k_warn(ar->ab, "failed to stop monitor vdev: %d\n", ret);
1529 		return ret;
1530 	}
1531 
1532 	ar->monitor_started = false;
1533 	ar->num_started_vdevs--;
1534 	ret = ath12k_dp_tx_htt_monitor_mode_ring_config(ar, true);
1535 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac monitor stopped ret %d\n", ret);
1536 	return ret;
1537 }
1538 
ath12k_mac_vdev_stop(struct ath12k_link_vif * arvif)1539 int ath12k_mac_vdev_stop(struct ath12k_link_vif *arvif)
1540 {
1541 	struct ath12k_vif *ahvif = arvif->ahvif;
1542 	struct ath12k *ar = arvif->ar;
1543 	int ret;
1544 
1545 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
1546 
1547 	reinit_completion(&ar->vdev_setup_done);
1548 
1549 	ret = ath12k_wmi_vdev_stop(ar, arvif->vdev_id);
1550 	if (ret) {
1551 		ath12k_warn(ar->ab, "failed to stop WMI vdev %i: %d\n",
1552 			    arvif->vdev_id, ret);
1553 		goto err;
1554 	}
1555 
1556 	ret = ath12k_mac_vdev_setup_sync(ar);
1557 	if (ret) {
1558 		ath12k_warn(ar->ab, "failed to synchronize setup for vdev %i: %d\n",
1559 			    arvif->vdev_id, ret);
1560 		goto err;
1561 	}
1562 
1563 	WARN_ON(ar->num_started_vdevs == 0);
1564 
1565 	ar->num_started_vdevs--;
1566 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "vdev %pM stopped, vdev_id %d\n",
1567 		   ahvif->vif->addr, arvif->vdev_id);
1568 
1569 	if (test_bit(ATH12K_FLAG_CAC_RUNNING, &ar->dev_flags)) {
1570 		clear_bit(ATH12K_FLAG_CAC_RUNNING, &ar->dev_flags);
1571 		ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "CAC Stopped for vdev %d\n",
1572 			   arvif->vdev_id);
1573 	}
1574 
1575 	return 0;
1576 err:
1577 	return ret;
1578 }
1579 
ath12k_mac_op_config(struct ieee80211_hw * hw,int radio_idx,u32 changed)1580 int ath12k_mac_op_config(struct ieee80211_hw *hw, int radio_idx, u32 changed)
1581 {
1582 	return 0;
1583 }
1584 EXPORT_SYMBOL(ath12k_mac_op_config);
1585 
ath12k_mac_setup_bcn_p2p_ie(struct ath12k_link_vif * arvif,struct sk_buff * bcn)1586 static int ath12k_mac_setup_bcn_p2p_ie(struct ath12k_link_vif *arvif,
1587 				       struct sk_buff *bcn)
1588 {
1589 	struct ath12k *ar = arvif->ar;
1590 	struct ieee80211_mgmt *mgmt;
1591 	const u8 *p2p_ie;
1592 	int ret;
1593 
1594 	mgmt = (void *)bcn->data;
1595 	p2p_ie = cfg80211_find_vendor_ie(WLAN_OUI_WFA, WLAN_OUI_TYPE_WFA_P2P,
1596 					 mgmt->u.beacon.variable,
1597 					 bcn->len - (mgmt->u.beacon.variable -
1598 						     bcn->data));
1599 	if (!p2p_ie) {
1600 		ath12k_warn(ar->ab, "no P2P ie found in beacon\n");
1601 		return -ENOENT;
1602 	}
1603 
1604 	ret = ath12k_wmi_p2p_go_bcn_ie(ar, arvif->vdev_id, p2p_ie);
1605 	if (ret) {
1606 		ath12k_warn(ar->ab, "failed to submit P2P GO bcn ie for vdev %i: %d\n",
1607 			    arvif->vdev_id, ret);
1608 		return ret;
1609 	}
1610 
1611 	return 0;
1612 }
1613 
ath12k_mac_remove_vendor_ie(struct sk_buff * skb,unsigned int oui,u8 oui_type,size_t ie_offset)1614 static int ath12k_mac_remove_vendor_ie(struct sk_buff *skb, unsigned int oui,
1615 				       u8 oui_type, size_t ie_offset)
1616 {
1617 	const u8 *next, *end;
1618 	size_t len;
1619 	u8 *ie;
1620 
1621 	if (WARN_ON(skb->len < ie_offset))
1622 		return -EINVAL;
1623 
1624 	ie = (u8 *)cfg80211_find_vendor_ie(oui, oui_type,
1625 					   skb->data + ie_offset,
1626 					   skb->len - ie_offset);
1627 	if (!ie)
1628 		return -ENOENT;
1629 
1630 	len = ie[1] + 2;
1631 	end = skb->data + skb->len;
1632 	next = ie + len;
1633 
1634 	if (WARN_ON(next > end))
1635 		return -EINVAL;
1636 
1637 	memmove(ie, next, end - next);
1638 	skb_trim(skb, skb->len - len);
1639 
1640 	return 0;
1641 }
1642 
ath12k_mac_set_arvif_ies(struct ath12k_link_vif * arvif,struct ath12k_link_vif * tx_arvif,struct sk_buff * bcn,u8 bssid_index,bool * nontx_profile_found)1643 static void ath12k_mac_set_arvif_ies(struct ath12k_link_vif *arvif,
1644 				     struct ath12k_link_vif *tx_arvif,
1645 				     struct sk_buff *bcn,
1646 				     u8 bssid_index, bool *nontx_profile_found)
1647 {
1648 	struct ieee80211_mgmt *mgmt = (struct ieee80211_mgmt *)bcn->data;
1649 	const struct element *elem, *nontx, *index, *nie, *ext_cap_ie;
1650 	const u8 *start, *tail;
1651 	u16 rem_len;
1652 	u8 i;
1653 
1654 	start = bcn->data + ieee80211_get_hdrlen_from_skb(bcn) + sizeof(mgmt->u.beacon);
1655 	tail = skb_tail_pointer(bcn);
1656 	rem_len = tail - start;
1657 
1658 	arvif->rsnie_present = false;
1659 	arvif->wpaie_present = false;
1660 
1661 	if (cfg80211_find_ie(WLAN_EID_RSN, start, rem_len))
1662 		arvif->rsnie_present = true;
1663 	if (cfg80211_find_vendor_ie(WLAN_OUI_MICROSOFT, WLAN_OUI_TYPE_MICROSOFT_WPA,
1664 				    start, rem_len))
1665 		arvif->wpaie_present = true;
1666 
1667 	ext_cap_ie = cfg80211_find_elem(WLAN_EID_EXT_CAPABILITY, start, rem_len);
1668 	if (ext_cap_ie && ext_cap_ie->datalen >= 11 &&
1669 	    (ext_cap_ie->data[10] & WLAN_EXT_CAPA11_BCN_PROTECT))
1670 		tx_arvif->beacon_prot = true;
1671 
1672 	/* Return from here for the transmitted profile */
1673 	if (!bssid_index)
1674 		return;
1675 
1676 	/* Initial rsnie_present for the nontransmitted profile is set to be same as that
1677 	 * of the transmitted profile. It will be changed if security configurations are
1678 	 * different.
1679 	 */
1680 	*nontx_profile_found = false;
1681 	for_each_element_id(elem, WLAN_EID_MULTIPLE_BSSID, start, rem_len) {
1682 		/* Fixed minimum MBSSID element length with at least one
1683 		 * nontransmitted BSSID profile is 12 bytes as given below;
1684 		 * 1 (max BSSID indicator) +
1685 		 * 2 (Nontransmitted BSSID profile: Subelement ID + length) +
1686 		 * 4 (Nontransmitted BSSID Capabilities: tag + length + info)
1687 		 * 2 (Nontransmitted BSSID SSID: tag + length)
1688 		 * 3 (Nontransmitted BSSID Index: tag + length + BSSID index
1689 		 */
1690 		if (elem->datalen < 12 || elem->data[0] < 1)
1691 			continue; /* Max BSSID indicator must be >=1 */
1692 
1693 		for_each_element(nontx, elem->data + 1, elem->datalen - 1) {
1694 			start = nontx->data;
1695 
1696 			if (nontx->id != 0 || nontx->datalen < 4)
1697 				continue; /* Invalid nontransmitted profile */
1698 
1699 			if (nontx->data[0] != WLAN_EID_NON_TX_BSSID_CAP ||
1700 			    nontx->data[1] != 2) {
1701 				continue; /* Missing nontransmitted BSS capabilities */
1702 			}
1703 
1704 			if (nontx->data[4] != WLAN_EID_SSID)
1705 				continue; /* Missing SSID for nontransmitted BSS */
1706 
1707 			index = cfg80211_find_elem(WLAN_EID_MULTI_BSSID_IDX,
1708 						   start, nontx->datalen);
1709 			if (!index || index->datalen < 1 || index->data[0] == 0)
1710 				continue; /* Invalid MBSSID Index element */
1711 
1712 			if (index->data[0] == bssid_index) {
1713 				*nontx_profile_found = true;
1714 
1715 				/* Check if nontx BSS has beacon protection enabled */
1716 				if (!tx_arvif->beacon_prot) {
1717 					ext_cap_ie =
1718 					    cfg80211_find_elem(WLAN_EID_EXT_CAPABILITY,
1719 							       nontx->data,
1720 							       nontx->datalen);
1721 					if (ext_cap_ie && ext_cap_ie->datalen >= 11 &&
1722 					    (ext_cap_ie->data[10] &
1723 					     WLAN_EXT_CAPA11_BCN_PROTECT))
1724 						tx_arvif->beacon_prot = true;
1725 				}
1726 
1727 				if (cfg80211_find_ie(WLAN_EID_RSN,
1728 						     nontx->data,
1729 						     nontx->datalen)) {
1730 					arvif->rsnie_present = true;
1731 					return;
1732 				} else if (!arvif->rsnie_present) {
1733 					return; /* Both tx and nontx BSS are open */
1734 				}
1735 
1736 				nie = cfg80211_find_ext_elem(WLAN_EID_EXT_NON_INHERITANCE,
1737 							     nontx->data,
1738 							     nontx->datalen);
1739 				if (!nie || nie->datalen < 2)
1740 					return; /* Invalid non-inheritance element */
1741 
1742 				for (i = 1; i < nie->datalen - 1; i++) {
1743 					if (nie->data[i] == WLAN_EID_RSN) {
1744 						arvif->rsnie_present = false;
1745 						break;
1746 					}
1747 				}
1748 
1749 				return;
1750 			}
1751 		}
1752 	}
1753 }
1754 
ath12k_mac_setup_bcn_tmpl_ema(struct ath12k_link_vif * arvif,struct ath12k_link_vif * tx_arvif,u8 bssid_index)1755 static int ath12k_mac_setup_bcn_tmpl_ema(struct ath12k_link_vif *arvif,
1756 					 struct ath12k_link_vif *tx_arvif,
1757 					 u8 bssid_index)
1758 {
1759 	struct ath12k_wmi_bcn_tmpl_ema_arg ema_args;
1760 	struct ieee80211_ema_beacons *beacons;
1761 	bool nontx_profile_found = false;
1762 	int ret = 0;
1763 	u8 i;
1764 
1765 	beacons = ieee80211_beacon_get_template_ema_list(ath12k_ar_to_hw(tx_arvif->ar),
1766 							 tx_arvif->ahvif->vif,
1767 							 tx_arvif->link_id);
1768 	if (!beacons || !beacons->cnt) {
1769 		ath12k_warn(arvif->ar->ab,
1770 			    "failed to get ema beacon templates from mac80211\n");
1771 		return -EPERM;
1772 	}
1773 
1774 	if (tx_arvif == arvif)
1775 		ath12k_mac_set_arvif_ies(arvif, tx_arvif, beacons->bcn[0].skb, 0, NULL);
1776 
1777 	for (i = 0; i < beacons->cnt; i++) {
1778 		if (tx_arvif != arvif && !nontx_profile_found)
1779 			ath12k_mac_set_arvif_ies(arvif, tx_arvif, beacons->bcn[i].skb,
1780 						 bssid_index,
1781 						 &nontx_profile_found);
1782 
1783 		ema_args.bcn_cnt = beacons->cnt;
1784 		ema_args.bcn_index = i;
1785 		ret = ath12k_wmi_bcn_tmpl(tx_arvif, &beacons->bcn[i].offs,
1786 					  beacons->bcn[i].skb, &ema_args);
1787 		if (ret) {
1788 			ath12k_warn(tx_arvif->ar->ab,
1789 				    "failed to set ema beacon template id %i error %d\n",
1790 				    i, ret);
1791 			break;
1792 		}
1793 	}
1794 
1795 	if (tx_arvif != arvif && !nontx_profile_found)
1796 		ath12k_warn(arvif->ar->ab,
1797 			    "nontransmitted bssid index %u not found in beacon template\n",
1798 			    bssid_index);
1799 
1800 	ieee80211_beacon_free_ema_list(beacons);
1801 	return ret;
1802 }
1803 
ath12k_mac_setup_bcn_tmpl(struct ath12k_link_vif * arvif)1804 static int ath12k_mac_setup_bcn_tmpl(struct ath12k_link_vif *arvif)
1805 {
1806 	struct ath12k_vif *ahvif = arvif->ahvif;
1807 	struct ieee80211_vif *vif = ath12k_ahvif_to_vif(ahvif);
1808 	struct ieee80211_bss_conf *link_conf;
1809 	struct ath12k_link_vif *tx_arvif;
1810 	struct ath12k *ar = arvif->ar;
1811 	struct ath12k_base *ab = ar->ab;
1812 	struct ieee80211_mutable_offsets offs = {};
1813 	bool nontx_profile_found = false;
1814 	struct sk_buff *bcn;
1815 	int ret;
1816 
1817 	if (ahvif->vdev_type != WMI_VDEV_TYPE_AP)
1818 		return 0;
1819 
1820 	link_conf = ath12k_mac_get_link_bss_conf(arvif);
1821 	if (!link_conf) {
1822 		ath12k_warn(ar->ab, "unable to access bss link conf to set bcn tmpl for vif %pM link %u\n",
1823 			    vif->addr, arvif->link_id);
1824 		return -ENOLINK;
1825 	}
1826 
1827 	tx_arvif = ath12k_mac_get_tx_arvif(arvif, link_conf);
1828 	if (tx_arvif) {
1829 		if (tx_arvif != arvif && arvif->is_up)
1830 			return 0;
1831 
1832 		if (link_conf->ema_ap)
1833 			return ath12k_mac_setup_bcn_tmpl_ema(arvif, tx_arvif,
1834 							     link_conf->bssid_index);
1835 	} else {
1836 		tx_arvif = arvif;
1837 	}
1838 
1839 	bcn = ieee80211_beacon_get_template(ath12k_ar_to_hw(tx_arvif->ar),
1840 					    tx_arvif->ahvif->vif,
1841 					    &offs, tx_arvif->link_id);
1842 	if (!bcn) {
1843 		ath12k_warn(ab, "failed to get beacon template from mac80211\n");
1844 		return -EPERM;
1845 	}
1846 
1847 	if (tx_arvif == arvif) {
1848 		ath12k_mac_set_arvif_ies(arvif, tx_arvif, bcn, 0, NULL);
1849 	} else {
1850 		ath12k_mac_set_arvif_ies(arvif, tx_arvif, bcn,
1851 					 link_conf->bssid_index,
1852 					 &nontx_profile_found);
1853 		if (!nontx_profile_found)
1854 			ath12k_warn(ab,
1855 				    "nontransmitted profile not found in beacon template\n");
1856 	}
1857 
1858 	if (ahvif->vif->type == NL80211_IFTYPE_AP && ahvif->vif->p2p) {
1859 		ret = ath12k_mac_setup_bcn_p2p_ie(arvif, bcn);
1860 		if (ret) {
1861 			ath12k_warn(ab, "failed to setup P2P GO bcn ie: %d\n",
1862 				    ret);
1863 			goto free_bcn_skb;
1864 		}
1865 
1866 		/* P2P IE is inserted by firmware automatically (as
1867 		 * configured above) so remove it from the base beacon
1868 		 * template to avoid duplicate P2P IEs in beacon frames.
1869 		 */
1870 		ret = ath12k_mac_remove_vendor_ie(bcn, WLAN_OUI_WFA,
1871 						  WLAN_OUI_TYPE_WFA_P2P,
1872 						  offsetof(struct ieee80211_mgmt,
1873 							   u.beacon.variable));
1874 		if (ret) {
1875 			ath12k_warn(ab, "failed to remove P2P vendor ie: %d\n",
1876 				    ret);
1877 			goto free_bcn_skb;
1878 		}
1879 	}
1880 
1881 	ret = ath12k_wmi_bcn_tmpl(arvif, &offs, bcn, NULL);
1882 
1883 	if (ret)
1884 		ath12k_warn(ab, "failed to submit beacon template command: %d\n",
1885 			    ret);
1886 
1887 free_bcn_skb:
1888 	kfree_skb(bcn);
1889 	return ret;
1890 }
1891 
ath12k_control_beaconing(struct ath12k_link_vif * arvif,struct ieee80211_bss_conf * info)1892 static void ath12k_control_beaconing(struct ath12k_link_vif *arvif,
1893 				     struct ieee80211_bss_conf *info)
1894 {
1895 	struct ath12k_wmi_vdev_up_params params = {};
1896 	struct ath12k_vif *ahvif = arvif->ahvif;
1897 	struct ath12k *ar = arvif->ar;
1898 	int ret;
1899 
1900 	lockdep_assert_wiphy(ath12k_ar_to_hw(arvif->ar)->wiphy);
1901 
1902 	if (!info->enable_beacon) {
1903 		ret = ath12k_wmi_vdev_down(ar, arvif->vdev_id);
1904 		if (ret)
1905 			ath12k_warn(ar->ab, "failed to down vdev_id %i: %d\n",
1906 				    arvif->vdev_id, ret);
1907 
1908 		arvif->is_up = false;
1909 		return;
1910 	}
1911 
1912 	/* Install the beacon template to the FW */
1913 	ret = ath12k_mac_setup_bcn_tmpl(arvif);
1914 	if (ret) {
1915 		ath12k_warn(ar->ab, "failed to update bcn tmpl during vdev up: %d\n",
1916 			    ret);
1917 		return;
1918 	}
1919 
1920 	ahvif->aid = 0;
1921 
1922 	ether_addr_copy(arvif->bssid, info->addr);
1923 
1924 	params.vdev_id = arvif->vdev_id;
1925 	params.aid = ahvif->aid;
1926 	params.bssid = arvif->bssid;
1927 	params.tx_bssid = ath12k_mac_get_tx_bssid(arvif);
1928 	if (params.tx_bssid) {
1929 		params.nontx_profile_idx = info->bssid_index;
1930 		params.nontx_profile_cnt = 1 << info->bssid_indicator;
1931 	}
1932 	ret = ath12k_wmi_vdev_up(arvif->ar, &params);
1933 	if (ret) {
1934 		ath12k_warn(ar->ab, "failed to bring up vdev %d: %i\n",
1935 			    arvif->vdev_id, ret);
1936 		return;
1937 	}
1938 
1939 	arvif->is_up = true;
1940 
1941 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac vdev %d up\n", arvif->vdev_id);
1942 }
1943 
ath12k_mac_handle_beacon_iter(void * data,u8 * mac,struct ieee80211_vif * vif)1944 static void ath12k_mac_handle_beacon_iter(void *data, u8 *mac,
1945 					  struct ieee80211_vif *vif)
1946 {
1947 	struct sk_buff *skb = data;
1948 	struct ieee80211_mgmt *mgmt = (void *)skb->data;
1949 	struct ath12k_vif *ahvif = ath12k_vif_to_ahvif(vif);
1950 	struct ath12k_link_vif *arvif = &ahvif->deflink;
1951 
1952 	if (vif->type != NL80211_IFTYPE_STATION || !arvif->is_created)
1953 		return;
1954 
1955 	if (!ether_addr_equal(mgmt->bssid, vif->bss_conf.bssid))
1956 		return;
1957 
1958 	cancel_delayed_work(&arvif->connection_loss_work);
1959 }
1960 
ath12k_mac_handle_beacon(struct ath12k * ar,struct sk_buff * skb)1961 void ath12k_mac_handle_beacon(struct ath12k *ar, struct sk_buff *skb)
1962 {
1963 	ieee80211_iterate_active_interfaces_atomic(ath12k_ar_to_hw(ar),
1964 						   IEEE80211_IFACE_ITER_NORMAL,
1965 						   ath12k_mac_handle_beacon_iter,
1966 						   skb);
1967 }
1968 
ath12k_mac_handle_beacon_miss(struct ath12k * ar,struct ath12k_link_vif * arvif)1969 void ath12k_mac_handle_beacon_miss(struct ath12k *ar,
1970 				   struct ath12k_link_vif *arvif)
1971 {
1972 	struct ieee80211_hw *hw = ath12k_ar_to_hw(ar);
1973 	struct ieee80211_vif *vif = ath12k_ahvif_to_vif(arvif->ahvif);
1974 
1975 	if (!(arvif->is_created && arvif->is_up))
1976 		return;
1977 
1978 	ieee80211_beacon_loss(vif);
1979 
1980 	/* Firmware doesn't report beacon loss events repeatedly. If AP probe
1981 	 * (done by mac80211) succeeds but beacons do not resume then it
1982 	 * doesn't make sense to continue operation. Queue connection loss work
1983 	 * which can be cancelled when beacon is received.
1984 	 */
1985 	ieee80211_queue_delayed_work(hw, &arvif->connection_loss_work,
1986 				     ATH12K_CONNECTION_LOSS_HZ);
1987 }
1988 
ath12k_mac_vif_sta_connection_loss_work(struct work_struct * work)1989 static void ath12k_mac_vif_sta_connection_loss_work(struct work_struct *work)
1990 {
1991 	struct ath12k_link_vif *arvif = container_of(work, struct ath12k_link_vif,
1992 						     connection_loss_work.work);
1993 	struct ieee80211_vif *vif = arvif->ahvif->vif;
1994 
1995 	if (!arvif->is_up)
1996 		return;
1997 
1998 	ieee80211_connection_loss(vif);
1999 }
2000 
ath12k_peer_assoc_h_basic(struct ath12k * ar,struct ath12k_link_vif * arvif,struct ath12k_link_sta * arsta,struct ath12k_wmi_peer_assoc_arg * arg)2001 static void ath12k_peer_assoc_h_basic(struct ath12k *ar,
2002 				      struct ath12k_link_vif *arvif,
2003 				      struct ath12k_link_sta *arsta,
2004 				      struct ath12k_wmi_peer_assoc_arg *arg)
2005 {
2006 	struct ieee80211_vif *vif = ath12k_ahvif_to_vif(arvif->ahvif);
2007 	struct ieee80211_sta *sta = ath12k_ahsta_to_sta(arsta->ahsta);
2008 	struct ieee80211_hw *hw = ath12k_ar_to_hw(ar);
2009 	struct ieee80211_bss_conf *bss_conf;
2010 	u32 aid;
2011 
2012 	lockdep_assert_wiphy(hw->wiphy);
2013 
2014 	if (vif->type == NL80211_IFTYPE_STATION)
2015 		aid = vif->cfg.aid;
2016 	else
2017 		aid = sta->aid;
2018 
2019 	ether_addr_copy(arg->peer_mac, arsta->addr);
2020 	arg->vdev_id = arvif->vdev_id;
2021 	arg->peer_associd = aid;
2022 	arg->auth_flag = true;
2023 	/* TODO: STA WAR in ath10k for listen interval required? */
2024 	arg->peer_listen_intval = hw->conf.listen_interval;
2025 	arg->peer_nss = 1;
2026 
2027 	bss_conf = ath12k_mac_get_link_bss_conf(arvif);
2028 	if (!bss_conf) {
2029 		ath12k_warn(ar->ab, "unable to access bss link conf in peer assoc for vif %pM link %u\n",
2030 			    vif->addr, arvif->link_id);
2031 		return;
2032 	}
2033 
2034 	arg->peer_caps = bss_conf->assoc_capability;
2035 }
2036 
ath12k_peer_assoc_h_crypto(struct ath12k * ar,struct ath12k_link_vif * arvif,struct ath12k_link_sta * arsta,struct ath12k_wmi_peer_assoc_arg * arg)2037 static void ath12k_peer_assoc_h_crypto(struct ath12k *ar,
2038 				       struct ath12k_link_vif *arvif,
2039 				       struct ath12k_link_sta *arsta,
2040 				       struct ath12k_wmi_peer_assoc_arg *arg)
2041 {
2042 	struct ieee80211_vif *vif = ath12k_ahvif_to_vif(arvif->ahvif);
2043 	struct ieee80211_sta *sta = ath12k_ahsta_to_sta(arsta->ahsta);
2044 	struct ieee80211_bss_conf *info;
2045 	struct cfg80211_chan_def def;
2046 	struct cfg80211_bss *bss;
2047 	struct ieee80211_hw *hw = ath12k_ar_to_hw(ar);
2048 	const u8 *rsnie = NULL;
2049 	const u8 *wpaie = NULL;
2050 
2051 	lockdep_assert_wiphy(hw->wiphy);
2052 
2053 	info = ath12k_mac_get_link_bss_conf(arvif);
2054 	if (!info) {
2055 		ath12k_warn(ar->ab, "unable to access bss link conf for peer assoc crypto for vif %pM link %u\n",
2056 			    vif->addr, arvif->link_id);
2057 		return;
2058 	}
2059 
2060 	if (WARN_ON(ath12k_mac_vif_link_chan(vif, arvif->link_id, &def)))
2061 		return;
2062 
2063 	bss = cfg80211_get_bss(hw->wiphy, def.chan, info->bssid, NULL, 0,
2064 			       IEEE80211_BSS_TYPE_ANY, IEEE80211_PRIVACY_ANY);
2065 
2066 	if (arvif->rsnie_present || arvif->wpaie_present) {
2067 		arg->need_ptk_4_way = true;
2068 		if (arvif->wpaie_present)
2069 			arg->need_gtk_2_way = true;
2070 	} else if (bss) {
2071 		const struct cfg80211_bss_ies *ies;
2072 
2073 		rcu_read_lock();
2074 		rsnie = ieee80211_bss_get_ie(bss, WLAN_EID_RSN);
2075 
2076 		ies = rcu_dereference(bss->ies);
2077 
2078 		wpaie = cfg80211_find_vendor_ie(WLAN_OUI_MICROSOFT,
2079 						WLAN_OUI_TYPE_MICROSOFT_WPA,
2080 						ies->data,
2081 						ies->len);
2082 		rcu_read_unlock();
2083 		cfg80211_put_bss(hw->wiphy, bss);
2084 	}
2085 
2086 	/* FIXME: base on RSN IE/WPA IE is a correct idea? */
2087 	if (rsnie || wpaie) {
2088 		ath12k_dbg(ar->ab, ATH12K_DBG_WMI,
2089 			   "%s: rsn ie found\n", __func__);
2090 		arg->need_ptk_4_way = true;
2091 	}
2092 
2093 	if (wpaie) {
2094 		ath12k_dbg(ar->ab, ATH12K_DBG_WMI,
2095 			   "%s: wpa ie found\n", __func__);
2096 		arg->need_gtk_2_way = true;
2097 	}
2098 
2099 	if (sta->mfp) {
2100 		/* TODO: Need to check if FW supports PMF? */
2101 		arg->is_pmf_enabled = true;
2102 	}
2103 
2104 	/* TODO: safe_mode_enabled (bypass 4-way handshake) flag req? */
2105 }
2106 
ath12k_peer_assoc_h_rates(struct ath12k * ar,struct ath12k_link_vif * arvif,struct ath12k_link_sta * arsta,struct ath12k_wmi_peer_assoc_arg * arg)2107 static void ath12k_peer_assoc_h_rates(struct ath12k *ar,
2108 				      struct ath12k_link_vif *arvif,
2109 				      struct ath12k_link_sta *arsta,
2110 				      struct ath12k_wmi_peer_assoc_arg *arg)
2111 {
2112 	struct ieee80211_vif *vif = ath12k_ahvif_to_vif(arvif->ahvif);
2113 	struct ieee80211_sta *sta = ath12k_ahsta_to_sta(arsta->ahsta);
2114 	struct wmi_rate_set_arg *rateset = &arg->peer_legacy_rates;
2115 	struct ieee80211_link_sta *link_sta;
2116 	struct cfg80211_chan_def def;
2117 	const struct ieee80211_supported_band *sband;
2118 	const struct ieee80211_rate *rates;
2119 	struct ieee80211_hw *hw = ath12k_ar_to_hw(ar);
2120 	enum nl80211_band band;
2121 	u32 ratemask;
2122 	u8 rate;
2123 	int i;
2124 
2125 	lockdep_assert_wiphy(hw->wiphy);
2126 
2127 	if (WARN_ON(ath12k_mac_vif_link_chan(vif, arvif->link_id, &def)))
2128 		return;
2129 
2130 	link_sta = ath12k_mac_get_link_sta(arsta);
2131 	if (!link_sta) {
2132 		ath12k_warn(ar->ab, "unable to access link sta in peer assoc rates for sta %pM link %u\n",
2133 			    sta->addr, arsta->link_id);
2134 		return;
2135 	}
2136 
2137 	band = def.chan->band;
2138 	sband = hw->wiphy->bands[band];
2139 	ratemask = link_sta->supp_rates[band];
2140 	ratemask &= arvif->bitrate_mask.control[band].legacy;
2141 	rates = sband->bitrates;
2142 
2143 	rateset->num_rates = 0;
2144 
2145 	for (i = 0; i < 32; i++, ratemask >>= 1, rates++) {
2146 		if (!(ratemask & 1))
2147 			continue;
2148 
2149 		rate = ath12k_mac_bitrate_to_rate(rates->bitrate);
2150 		rateset->rates[rateset->num_rates] = rate;
2151 		rateset->num_rates++;
2152 	}
2153 }
2154 
2155 static bool
ath12k_peer_assoc_h_ht_masked(const u8 * ht_mcs_mask)2156 ath12k_peer_assoc_h_ht_masked(const u8 *ht_mcs_mask)
2157 {
2158 	int nss;
2159 
2160 	for (nss = 0; nss < IEEE80211_HT_MCS_MASK_LEN; nss++)
2161 		if (ht_mcs_mask[nss])
2162 			return false;
2163 
2164 	return true;
2165 }
2166 
2167 static bool
ath12k_peer_assoc_h_vht_masked(const u16 * vht_mcs_mask)2168 ath12k_peer_assoc_h_vht_masked(const u16 *vht_mcs_mask)
2169 {
2170 	int nss;
2171 
2172 	for (nss = 0; nss < NL80211_VHT_NSS_MAX; nss++)
2173 		if (vht_mcs_mask[nss])
2174 			return false;
2175 
2176 	return true;
2177 }
2178 
ath12k_peer_assoc_h_ht(struct ath12k * ar,struct ath12k_link_vif * arvif,struct ath12k_link_sta * arsta,struct ath12k_wmi_peer_assoc_arg * arg)2179 static void ath12k_peer_assoc_h_ht(struct ath12k *ar,
2180 				   struct ath12k_link_vif *arvif,
2181 				   struct ath12k_link_sta *arsta,
2182 				   struct ath12k_wmi_peer_assoc_arg *arg)
2183 {
2184 	struct ieee80211_vif *vif = ath12k_ahvif_to_vif(arvif->ahvif);
2185 	struct ieee80211_sta *sta = ath12k_ahsta_to_sta(arsta->ahsta);
2186 	const struct ieee80211_sta_ht_cap *ht_cap;
2187 	struct ieee80211_link_sta *link_sta;
2188 	struct cfg80211_chan_def def;
2189 	enum nl80211_band band;
2190 	const u8 *ht_mcs_mask;
2191 	int i, n;
2192 	u8 max_nss;
2193 	u32 stbc;
2194 
2195 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
2196 
2197 	if (WARN_ON(ath12k_mac_vif_link_chan(vif, arvif->link_id, &def)))
2198 		return;
2199 
2200 	link_sta = ath12k_mac_get_link_sta(arsta);
2201 	if (!link_sta) {
2202 		ath12k_warn(ar->ab, "unable to access link sta in peer assoc ht for sta %pM link %u\n",
2203 			    sta->addr, arsta->link_id);
2204 		return;
2205 	}
2206 
2207 	ht_cap = &link_sta->ht_cap;
2208 	if (!ht_cap->ht_supported)
2209 		return;
2210 
2211 	band = def.chan->band;
2212 	ht_mcs_mask = arvif->bitrate_mask.control[band].ht_mcs;
2213 
2214 	if (ath12k_peer_assoc_h_ht_masked(ht_mcs_mask))
2215 		return;
2216 
2217 	arg->ht_flag = true;
2218 
2219 	arg->peer_max_mpdu = (1 << (IEEE80211_HT_MAX_AMPDU_FACTOR +
2220 				    ht_cap->ampdu_factor)) - 1;
2221 
2222 	arg->peer_mpdu_density =
2223 		ath12k_parse_mpdudensity(ht_cap->ampdu_density);
2224 
2225 	arg->peer_ht_caps = ht_cap->cap;
2226 	arg->peer_rate_caps |= WMI_HOST_RC_HT_FLAG;
2227 
2228 	if (ht_cap->cap & IEEE80211_HT_CAP_LDPC_CODING)
2229 		arg->ldpc_flag = true;
2230 
2231 	if (link_sta->bandwidth >= IEEE80211_STA_RX_BW_40) {
2232 		arg->bw_40 = true;
2233 		arg->peer_rate_caps |= WMI_HOST_RC_CW40_FLAG;
2234 	}
2235 
2236 	/* As firmware handles these two flags (IEEE80211_HT_CAP_SGI_20
2237 	 * and IEEE80211_HT_CAP_SGI_40) for enabling SGI, reset both
2238 	 * flags if guard interval is to force Long GI
2239 	 */
2240 	if (arvif->bitrate_mask.control[band].gi == NL80211_TXRATE_FORCE_LGI) {
2241 		arg->peer_ht_caps &= ~(IEEE80211_HT_CAP_SGI_20 | IEEE80211_HT_CAP_SGI_40);
2242 	} else {
2243 		/* Enable SGI flag if either SGI_20 or SGI_40 is supported */
2244 		if (ht_cap->cap & (IEEE80211_HT_CAP_SGI_20 | IEEE80211_HT_CAP_SGI_40))
2245 			arg->peer_rate_caps |= WMI_HOST_RC_SGI_FLAG;
2246 	}
2247 
2248 	if (ht_cap->cap & IEEE80211_HT_CAP_TX_STBC) {
2249 		arg->peer_rate_caps |= WMI_HOST_RC_TX_STBC_FLAG;
2250 		arg->stbc_flag = true;
2251 	}
2252 
2253 	if (ht_cap->cap & IEEE80211_HT_CAP_RX_STBC) {
2254 		stbc = ht_cap->cap & IEEE80211_HT_CAP_RX_STBC;
2255 		stbc = stbc >> IEEE80211_HT_CAP_RX_STBC_SHIFT;
2256 		stbc = stbc << WMI_HOST_RC_RX_STBC_FLAG_S;
2257 		arg->peer_rate_caps |= stbc;
2258 		arg->stbc_flag = true;
2259 	}
2260 
2261 	if (ht_cap->mcs.rx_mask[1] && ht_cap->mcs.rx_mask[2])
2262 		arg->peer_rate_caps |= WMI_HOST_RC_TS_FLAG;
2263 	else if (ht_cap->mcs.rx_mask[1])
2264 		arg->peer_rate_caps |= WMI_HOST_RC_DS_FLAG;
2265 
2266 	for (i = 0, n = 0, max_nss = 0; i < IEEE80211_HT_MCS_MASK_LEN * 8; i++)
2267 		if ((ht_cap->mcs.rx_mask[i / 8] & BIT(i % 8)) &&
2268 		    (ht_mcs_mask[i / 8] & BIT(i % 8))) {
2269 			max_nss = (i / 8) + 1;
2270 			arg->peer_ht_rates.rates[n++] = i;
2271 		}
2272 
2273 	/* This is a workaround for HT-enabled STAs which break the spec
2274 	 * and have no HT capabilities RX mask (no HT RX MCS map).
2275 	 *
2276 	 * As per spec, in section 20.3.5 Modulation and coding scheme (MCS),
2277 	 * MCS 0 through 7 are mandatory in 20MHz with 800 ns GI at all STAs.
2278 	 *
2279 	 * Firmware asserts if such situation occurs.
2280 	 */
2281 	if (n == 0) {
2282 		arg->peer_ht_rates.num_rates = 8;
2283 		for (i = 0; i < arg->peer_ht_rates.num_rates; i++)
2284 			arg->peer_ht_rates.rates[i] = i;
2285 	} else {
2286 		arg->peer_ht_rates.num_rates = n;
2287 		arg->peer_nss = min(link_sta->rx_nss, max_nss);
2288 	}
2289 
2290 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac ht peer %pM mcs cnt %d nss %d\n",
2291 		   arg->peer_mac,
2292 		   arg->peer_ht_rates.num_rates,
2293 		   arg->peer_nss);
2294 }
2295 
ath12k_mac_get_max_vht_mcs_map(u16 mcs_map,int nss)2296 static int ath12k_mac_get_max_vht_mcs_map(u16 mcs_map, int nss)
2297 {
2298 	switch ((mcs_map >> (2 * nss)) & 0x3) {
2299 	case IEEE80211_VHT_MCS_SUPPORT_0_7: return BIT(8) - 1;
2300 	case IEEE80211_VHT_MCS_SUPPORT_0_8: return BIT(9) - 1;
2301 	case IEEE80211_VHT_MCS_SUPPORT_0_9: return BIT(10) - 1;
2302 	}
2303 	return 0;
2304 }
2305 
2306 static u16
ath12k_peer_assoc_h_vht_limit(u16 tx_mcs_set,const u16 vht_mcs_limit[NL80211_VHT_NSS_MAX])2307 ath12k_peer_assoc_h_vht_limit(u16 tx_mcs_set,
2308 			      const u16 vht_mcs_limit[NL80211_VHT_NSS_MAX])
2309 {
2310 	int idx_limit;
2311 	int nss;
2312 	u16 mcs_map;
2313 	u16 mcs;
2314 
2315 	for (nss = 0; nss < NL80211_VHT_NSS_MAX; nss++) {
2316 		mcs_map = ath12k_mac_get_max_vht_mcs_map(tx_mcs_set, nss) &
2317 			  vht_mcs_limit[nss];
2318 
2319 		if (mcs_map)
2320 			idx_limit = fls(mcs_map) - 1;
2321 		else
2322 			idx_limit = -1;
2323 
2324 		switch (idx_limit) {
2325 		case 0:
2326 		case 1:
2327 		case 2:
2328 		case 3:
2329 		case 4:
2330 		case 5:
2331 		case 6:
2332 		case 7:
2333 			mcs = IEEE80211_VHT_MCS_SUPPORT_0_7;
2334 			break;
2335 		case 8:
2336 			mcs = IEEE80211_VHT_MCS_SUPPORT_0_8;
2337 			break;
2338 		case 9:
2339 			mcs = IEEE80211_VHT_MCS_SUPPORT_0_9;
2340 			break;
2341 		default:
2342 			WARN_ON(1);
2343 			fallthrough;
2344 		case -1:
2345 			mcs = IEEE80211_VHT_MCS_NOT_SUPPORTED;
2346 			break;
2347 		}
2348 
2349 		tx_mcs_set &= ~(0x3 << (nss * 2));
2350 		tx_mcs_set |= mcs << (nss * 2);
2351 	}
2352 
2353 	return tx_mcs_set;
2354 }
2355 
ath12k_get_nss_160mhz(struct ath12k * ar,u8 max_nss)2356 static u8 ath12k_get_nss_160mhz(struct ath12k *ar,
2357 				u8 max_nss)
2358 {
2359 	u8 nss_ratio_info = ar->pdev->cap.nss_ratio_info;
2360 	u8 max_sup_nss = 0;
2361 
2362 	switch (nss_ratio_info) {
2363 	case WMI_NSS_RATIO_1BY2_NSS:
2364 		max_sup_nss = max_nss >> 1;
2365 		break;
2366 	case WMI_NSS_RATIO_3BY4_NSS:
2367 		ath12k_warn(ar->ab, "WMI_NSS_RATIO_3BY4_NSS not supported\n");
2368 		break;
2369 	case WMI_NSS_RATIO_1_NSS:
2370 		max_sup_nss = max_nss;
2371 		break;
2372 	case WMI_NSS_RATIO_2_NSS:
2373 		ath12k_warn(ar->ab, "WMI_NSS_RATIO_2_NSS not supported\n");
2374 		break;
2375 	default:
2376 		ath12k_warn(ar->ab, "invalid nss ratio received from fw: %d\n",
2377 			    nss_ratio_info);
2378 		break;
2379 	}
2380 
2381 	return max_sup_nss;
2382 }
2383 
ath12k_peer_assoc_h_vht(struct ath12k * ar,struct ath12k_link_vif * arvif,struct ath12k_link_sta * arsta,struct ath12k_wmi_peer_assoc_arg * arg)2384 static void ath12k_peer_assoc_h_vht(struct ath12k *ar,
2385 				    struct ath12k_link_vif *arvif,
2386 				    struct ath12k_link_sta *arsta,
2387 				    struct ath12k_wmi_peer_assoc_arg *arg)
2388 {
2389 	struct ieee80211_vif *vif = ath12k_ahvif_to_vif(arvif->ahvif);
2390 	struct ieee80211_sta *sta = ath12k_ahsta_to_sta(arsta->ahsta);
2391 	const struct ieee80211_sta_vht_cap *vht_cap;
2392 	struct ieee80211_link_sta *link_sta;
2393 	struct cfg80211_chan_def def;
2394 	enum nl80211_band band;
2395 	u16 *vht_mcs_mask;
2396 	u8 ampdu_factor;
2397 	u8 max_nss, vht_mcs;
2398 	int i, vht_nss, nss_idx;
2399 	bool user_rate_valid = true;
2400 	u32 rx_nss, tx_nss, nss_160;
2401 
2402 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
2403 
2404 	if (WARN_ON(ath12k_mac_vif_link_chan(vif, arvif->link_id, &def)))
2405 		return;
2406 
2407 	link_sta = ath12k_mac_get_link_sta(arsta);
2408 	if (!link_sta) {
2409 		ath12k_warn(ar->ab, "unable to access link sta in peer assoc vht for sta %pM link %u\n",
2410 			    sta->addr, arsta->link_id);
2411 		return;
2412 	}
2413 
2414 	vht_cap = &link_sta->vht_cap;
2415 	if (!vht_cap->vht_supported)
2416 		return;
2417 
2418 	band = def.chan->band;
2419 	vht_mcs_mask = arvif->bitrate_mask.control[band].vht_mcs;
2420 
2421 	if (ath12k_peer_assoc_h_vht_masked(vht_mcs_mask))
2422 		return;
2423 
2424 	arg->vht_flag = true;
2425 
2426 	/* TODO: similar flags required? */
2427 	arg->vht_capable = true;
2428 
2429 	if (def.chan->band == NL80211_BAND_2GHZ)
2430 		arg->vht_ng_flag = true;
2431 
2432 	arg->peer_vht_caps = vht_cap->cap;
2433 
2434 	ampdu_factor = (vht_cap->cap &
2435 			IEEE80211_VHT_CAP_MAX_A_MPDU_LENGTH_EXPONENT_MASK) >>
2436 		       IEEE80211_VHT_CAP_MAX_A_MPDU_LENGTH_EXPONENT_SHIFT;
2437 
2438 	/* Workaround: Some Netgear/Linksys 11ac APs set Rx A-MPDU factor to
2439 	 * zero in VHT IE. Using it would result in degraded throughput.
2440 	 * arg->peer_max_mpdu at this point contains HT max_mpdu so keep
2441 	 * it if VHT max_mpdu is smaller.
2442 	 */
2443 	arg->peer_max_mpdu = max(arg->peer_max_mpdu,
2444 				 (1U << (IEEE80211_HT_MAX_AMPDU_FACTOR +
2445 					ampdu_factor)) - 1);
2446 
2447 	if (link_sta->bandwidth == IEEE80211_STA_RX_BW_80)
2448 		arg->bw_80 = true;
2449 
2450 	if (link_sta->bandwidth == IEEE80211_STA_RX_BW_160)
2451 		arg->bw_160 = true;
2452 
2453 	vht_nss =  ath12k_mac_max_vht_nss(vht_mcs_mask);
2454 
2455 	if (vht_nss > link_sta->rx_nss) {
2456 		user_rate_valid = false;
2457 		for (nss_idx = link_sta->rx_nss - 1; nss_idx >= 0; nss_idx--) {
2458 			if (vht_mcs_mask[nss_idx]) {
2459 				user_rate_valid = true;
2460 				break;
2461 			}
2462 		}
2463 	}
2464 
2465 	if (!user_rate_valid) {
2466 		ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
2467 			   "Setting vht range MCS value to peer supported nss:%d for peer %pM\n",
2468 			   link_sta->rx_nss, arsta->addr);
2469 		vht_mcs_mask[link_sta->rx_nss - 1] = vht_mcs_mask[vht_nss - 1];
2470 	}
2471 
2472 	/* Calculate peer NSS capability from VHT capabilities if STA
2473 	 * supports VHT.
2474 	 */
2475 	for (i = 0, max_nss = 0, vht_mcs = 0; i < NL80211_VHT_NSS_MAX; i++) {
2476 		vht_mcs = __le16_to_cpu(vht_cap->vht_mcs.rx_mcs_map) >>
2477 			  (2 * i) & 3;
2478 
2479 		if (vht_mcs != IEEE80211_VHT_MCS_NOT_SUPPORTED &&
2480 		    vht_mcs_mask[i])
2481 			max_nss = i + 1;
2482 	}
2483 	arg->peer_nss = min(link_sta->rx_nss, max_nss);
2484 	arg->rx_max_rate = __le16_to_cpu(vht_cap->vht_mcs.rx_highest);
2485 	arg->rx_mcs_set = __le16_to_cpu(vht_cap->vht_mcs.rx_mcs_map);
2486 	arg->rx_mcs_set = ath12k_peer_assoc_h_vht_limit(arg->rx_mcs_set, vht_mcs_mask);
2487 
2488 	arg->tx_max_rate = __le16_to_cpu(vht_cap->vht_mcs.tx_highest);
2489 	arg->tx_mcs_set = __le16_to_cpu(vht_cap->vht_mcs.tx_mcs_map);
2490 
2491 	/* In QCN9274 platform, VHT MCS rate 10 and 11 is enabled by default.
2492 	 * VHT MCS rate 10 and 11 is not supported in 11ac standard.
2493 	 * so explicitly disable the VHT MCS rate 10 and 11 in 11ac mode.
2494 	 */
2495 	arg->tx_mcs_set &= ~IEEE80211_VHT_MCS_SUPPORT_0_11_MASK;
2496 	arg->tx_mcs_set |= IEEE80211_DISABLE_VHT_MCS_SUPPORT_0_11;
2497 
2498 	if ((arg->tx_mcs_set & IEEE80211_VHT_MCS_NOT_SUPPORTED) ==
2499 			IEEE80211_VHT_MCS_NOT_SUPPORTED)
2500 		arg->peer_vht_caps &= ~IEEE80211_VHT_CAP_MU_BEAMFORMEE_CAPABLE;
2501 
2502 	/* TODO:  Check */
2503 	arg->tx_max_mcs_nss = 0xFF;
2504 
2505 	if (arg->peer_phymode == MODE_11AC_VHT160) {
2506 		tx_nss = ath12k_get_nss_160mhz(ar, max_nss);
2507 		rx_nss = min(arg->peer_nss, tx_nss);
2508 		arg->peer_bw_rxnss_override = ATH12K_BW_NSS_MAP_ENABLE;
2509 
2510 		if (!rx_nss) {
2511 			ath12k_warn(ar->ab, "invalid max_nss\n");
2512 			return;
2513 		}
2514 
2515 		nss_160 = u32_encode_bits(rx_nss - 1, ATH12K_PEER_RX_NSS_160MHZ);
2516 		arg->peer_bw_rxnss_override |= nss_160;
2517 	}
2518 
2519 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
2520 		   "mac vht peer %pM max_mpdu %d flags 0x%x nss_override 0x%x\n",
2521 		   arsta->addr, arg->peer_max_mpdu, arg->peer_flags,
2522 		   arg->peer_bw_rxnss_override);
2523 }
2524 
ath12k_mac_get_max_he_mcs_map(u16 mcs_map,int nss)2525 static int ath12k_mac_get_max_he_mcs_map(u16 mcs_map, int nss)
2526 {
2527 	switch ((mcs_map >> (2 * nss)) & 0x3) {
2528 	case IEEE80211_HE_MCS_SUPPORT_0_7: return BIT(8) - 1;
2529 	case IEEE80211_HE_MCS_SUPPORT_0_9: return BIT(10) - 1;
2530 	case IEEE80211_HE_MCS_SUPPORT_0_11: return BIT(12) - 1;
2531 	}
2532 	return 0;
2533 }
2534 
ath12k_peer_assoc_h_he_limit(u16 tx_mcs_set,const u16 * he_mcs_limit)2535 static u16 ath12k_peer_assoc_h_he_limit(u16 tx_mcs_set,
2536 					const u16 *he_mcs_limit)
2537 {
2538 	int idx_limit;
2539 	int nss;
2540 	u16 mcs_map;
2541 	u16 mcs;
2542 
2543 	for (nss = 0; nss < NL80211_HE_NSS_MAX; nss++) {
2544 		mcs_map = ath12k_mac_get_max_he_mcs_map(tx_mcs_set, nss) &
2545 			he_mcs_limit[nss];
2546 
2547 		if (mcs_map)
2548 			idx_limit = fls(mcs_map) - 1;
2549 		else
2550 			idx_limit = -1;
2551 
2552 		switch (idx_limit) {
2553 		case 0 ... 7:
2554 			mcs = IEEE80211_HE_MCS_SUPPORT_0_7;
2555 			break;
2556 		case 8:
2557 		case 9:
2558 			mcs = IEEE80211_HE_MCS_SUPPORT_0_9;
2559 			break;
2560 		case 10:
2561 		case 11:
2562 			mcs = IEEE80211_HE_MCS_SUPPORT_0_11;
2563 			break;
2564 		default:
2565 			WARN_ON(1);
2566 			fallthrough;
2567 		case -1:
2568 			mcs = IEEE80211_HE_MCS_NOT_SUPPORTED;
2569 			break;
2570 		}
2571 
2572 		tx_mcs_set &= ~(0x3 << (nss * 2));
2573 		tx_mcs_set |= mcs << (nss * 2);
2574 	}
2575 
2576 	return tx_mcs_set;
2577 }
2578 
2579 static bool
ath12k_peer_assoc_h_he_masked(const u16 he_mcs_mask[NL80211_HE_NSS_MAX])2580 ath12k_peer_assoc_h_he_masked(const u16 he_mcs_mask[NL80211_HE_NSS_MAX])
2581 {
2582 	int nss;
2583 
2584 	for (nss = 0; nss < NL80211_HE_NSS_MAX; nss++)
2585 		if (he_mcs_mask[nss])
2586 			return false;
2587 
2588 	return true;
2589 }
2590 
ath12k_peer_assoc_h_he(struct ath12k * ar,struct ath12k_link_vif * arvif,struct ath12k_link_sta * arsta,struct ath12k_wmi_peer_assoc_arg * arg)2591 static void ath12k_peer_assoc_h_he(struct ath12k *ar,
2592 				   struct ath12k_link_vif *arvif,
2593 				   struct ath12k_link_sta *arsta,
2594 				   struct ath12k_wmi_peer_assoc_arg *arg)
2595 {
2596 	struct ieee80211_vif *vif = ath12k_ahvif_to_vif(arvif->ahvif);
2597 	struct ieee80211_sta *sta = ath12k_ahsta_to_sta(arsta->ahsta);
2598 	const struct ieee80211_sta_he_cap *he_cap;
2599 	struct ieee80211_bss_conf *link_conf;
2600 	struct ieee80211_link_sta *link_sta;
2601 	struct cfg80211_chan_def def;
2602 	int i;
2603 	u8 ampdu_factor, max_nss;
2604 	u8 rx_mcs_80 = IEEE80211_HE_MCS_NOT_SUPPORTED;
2605 	u8 rx_mcs_160 = IEEE80211_HE_MCS_NOT_SUPPORTED;
2606 	u16 mcs_160_map, mcs_80_map;
2607 	u8 link_id = arvif->link_id;
2608 	bool support_160;
2609 	enum nl80211_band band;
2610 	u16 *he_mcs_mask;
2611 	u8 he_mcs;
2612 	u16 he_tx_mcs = 0, v = 0;
2613 	int he_nss, nss_idx;
2614 	bool user_rate_valid = true;
2615 	u32 rx_nss, tx_nss, nss_160;
2616 
2617 	if (WARN_ON(ath12k_mac_vif_link_chan(vif, link_id, &def)))
2618 		return;
2619 
2620 	link_conf = ath12k_mac_get_link_bss_conf(arvif);
2621 	if (!link_conf) {
2622 		ath12k_warn(ar->ab, "unable to access bss link conf in peer assoc he for vif %pM link %u",
2623 			    vif->addr, link_id);
2624 		return;
2625 	}
2626 
2627 	link_sta = ath12k_mac_get_link_sta(arsta);
2628 	if (!link_sta) {
2629 		ath12k_warn(ar->ab, "unable to access link sta in peer assoc he for sta %pM link %u\n",
2630 			    sta->addr, arsta->link_id);
2631 		return;
2632 	}
2633 
2634 	he_cap = &link_sta->he_cap;
2635 	if (!he_cap->has_he)
2636 		return;
2637 
2638 	band = def.chan->band;
2639 	he_mcs_mask = arvif->bitrate_mask.control[band].he_mcs;
2640 
2641 	if (ath12k_peer_assoc_h_he_masked(he_mcs_mask))
2642 		return;
2643 
2644 	arg->he_flag = true;
2645 
2646 	support_160 = !!(he_cap->he_cap_elem.phy_cap_info[0] &
2647 		  IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_160MHZ_IN_5G);
2648 
2649 	/* Supported HE-MCS and NSS Set of peer he_cap is intersection with self he_cp */
2650 	mcs_160_map = le16_to_cpu(he_cap->he_mcs_nss_supp.rx_mcs_160);
2651 	mcs_80_map = le16_to_cpu(he_cap->he_mcs_nss_supp.rx_mcs_80);
2652 
2653 	if (support_160) {
2654 		for (i = 7; i >= 0; i--) {
2655 			u8 mcs_160 = (mcs_160_map >> (2 * i)) & 3;
2656 
2657 			if (mcs_160 != IEEE80211_HE_MCS_NOT_SUPPORTED) {
2658 				rx_mcs_160 = i + 1;
2659 				break;
2660 			}
2661 		}
2662 	}
2663 
2664 	for (i = 7; i >= 0; i--) {
2665 		u8 mcs_80 = (mcs_80_map >> (2 * i)) & 3;
2666 
2667 		if (mcs_80 != IEEE80211_HE_MCS_NOT_SUPPORTED) {
2668 			rx_mcs_80 = i + 1;
2669 			break;
2670 		}
2671 	}
2672 
2673 	if (support_160)
2674 		max_nss = min(rx_mcs_80, rx_mcs_160);
2675 	else
2676 		max_nss = rx_mcs_80;
2677 
2678 	arg->peer_nss = min(link_sta->rx_nss, max_nss);
2679 
2680 	memcpy(&arg->peer_he_cap_macinfo, he_cap->he_cap_elem.mac_cap_info,
2681 	       sizeof(he_cap->he_cap_elem.mac_cap_info));
2682 	memcpy(&arg->peer_he_cap_phyinfo, he_cap->he_cap_elem.phy_cap_info,
2683 	       sizeof(he_cap->he_cap_elem.phy_cap_info));
2684 	arg->peer_he_ops = link_conf->he_oper.params;
2685 
2686 	/* the top most byte is used to indicate BSS color info */
2687 	arg->peer_he_ops &= 0xffffff;
2688 
2689 	/* As per section 26.6.1 IEEE Std 802.11ax‐2022, if the Max AMPDU
2690 	 * Exponent Extension in HE cap is zero, use the arg->peer_max_mpdu
2691 	 * as calculated while parsing VHT caps(if VHT caps is present)
2692 	 * or HT caps (if VHT caps is not present).
2693 	 *
2694 	 * For non-zero value of Max AMPDU Exponent Extension in HE MAC caps,
2695 	 * if a HE STA sends VHT cap and HE cap IE in assoc request then, use
2696 	 * MAX_AMPDU_LEN_FACTOR as 20 to calculate max_ampdu length.
2697 	 * If a HE STA that does not send VHT cap, but HE and HT cap in assoc
2698 	 * request, then use MAX_AMPDU_LEN_FACTOR as 16 to calculate max_ampdu
2699 	 * length.
2700 	 */
2701 	ampdu_factor = u8_get_bits(he_cap->he_cap_elem.mac_cap_info[3],
2702 				   IEEE80211_HE_MAC_CAP3_MAX_AMPDU_LEN_EXP_MASK);
2703 
2704 	if (ampdu_factor) {
2705 		if (link_sta->vht_cap.vht_supported)
2706 			arg->peer_max_mpdu = (1 << (IEEE80211_HE_VHT_MAX_AMPDU_FACTOR +
2707 						    ampdu_factor)) - 1;
2708 		else if (link_sta->ht_cap.ht_supported)
2709 			arg->peer_max_mpdu = (1 << (IEEE80211_HE_HT_MAX_AMPDU_FACTOR +
2710 						    ampdu_factor)) - 1;
2711 	}
2712 
2713 	if (he_cap->he_cap_elem.phy_cap_info[6] &
2714 	    IEEE80211_HE_PHY_CAP6_PPE_THRESHOLD_PRESENT) {
2715 		int bit = 7;
2716 		int nss, ru;
2717 
2718 		arg->peer_ppet.numss_m1 = he_cap->ppe_thres[0] &
2719 					  IEEE80211_PPE_THRES_NSS_MASK;
2720 		arg->peer_ppet.ru_bit_mask =
2721 			(he_cap->ppe_thres[0] &
2722 			 IEEE80211_PPE_THRES_RU_INDEX_BITMASK_MASK) >>
2723 			IEEE80211_PPE_THRES_RU_INDEX_BITMASK_POS;
2724 
2725 		for (nss = 0; nss <= arg->peer_ppet.numss_m1; nss++) {
2726 			for (ru = 0; ru < 4; ru++) {
2727 				u32 val = 0;
2728 				int i;
2729 
2730 				if ((arg->peer_ppet.ru_bit_mask & BIT(ru)) == 0)
2731 					continue;
2732 				for (i = 0; i < 6; i++) {
2733 					val >>= 1;
2734 					val |= ((he_cap->ppe_thres[bit / 8] >>
2735 						 (bit % 8)) & 0x1) << 5;
2736 					bit++;
2737 				}
2738 				arg->peer_ppet.ppet16_ppet8_ru3_ru0[nss] |=
2739 								val << (ru * 6);
2740 			}
2741 		}
2742 	}
2743 
2744 	if (he_cap->he_cap_elem.mac_cap_info[0] & IEEE80211_HE_MAC_CAP0_TWT_RES)
2745 		arg->twt_responder = true;
2746 	if (he_cap->he_cap_elem.mac_cap_info[0] & IEEE80211_HE_MAC_CAP0_TWT_REQ)
2747 		arg->twt_requester = true;
2748 
2749 	he_nss = ath12k_mac_max_he_nss(he_mcs_mask);
2750 
2751 	if (he_nss > link_sta->rx_nss) {
2752 		user_rate_valid = false;
2753 		for (nss_idx = link_sta->rx_nss - 1; nss_idx >= 0; nss_idx--) {
2754 			if (he_mcs_mask[nss_idx]) {
2755 				user_rate_valid = true;
2756 				break;
2757 			}
2758 		}
2759 	}
2760 
2761 	if (!user_rate_valid) {
2762 		ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
2763 			   "Setting he range MCS value to peer supported nss:%d for peer %pM\n",
2764 			   link_sta->rx_nss, arsta->addr);
2765 		he_mcs_mask[link_sta->rx_nss - 1] = he_mcs_mask[he_nss - 1];
2766 	}
2767 
2768 	switch (link_sta->bandwidth) {
2769 	case IEEE80211_STA_RX_BW_160:
2770 		v = le16_to_cpu(he_cap->he_mcs_nss_supp.rx_mcs_160);
2771 		v = ath12k_peer_assoc_h_he_limit(v, he_mcs_mask);
2772 		arg->peer_he_rx_mcs_set[WMI_HECAP_TXRX_MCS_NSS_IDX_160] = v;
2773 
2774 		v = le16_to_cpu(he_cap->he_mcs_nss_supp.tx_mcs_160);
2775 		arg->peer_he_tx_mcs_set[WMI_HECAP_TXRX_MCS_NSS_IDX_160] = v;
2776 
2777 		arg->peer_he_mcs_count++;
2778 		if (!he_tx_mcs)
2779 			he_tx_mcs = v;
2780 		fallthrough;
2781 
2782 	default:
2783 		v = le16_to_cpu(he_cap->he_mcs_nss_supp.rx_mcs_80);
2784 		v = ath12k_peer_assoc_h_he_limit(v, he_mcs_mask);
2785 		arg->peer_he_rx_mcs_set[WMI_HECAP_TXRX_MCS_NSS_IDX_80] = v;
2786 
2787 		v = le16_to_cpu(he_cap->he_mcs_nss_supp.tx_mcs_80);
2788 		arg->peer_he_tx_mcs_set[WMI_HECAP_TXRX_MCS_NSS_IDX_80] = v;
2789 
2790 		arg->peer_he_mcs_count++;
2791 		if (!he_tx_mcs)
2792 			he_tx_mcs = v;
2793 		break;
2794 	}
2795 
2796 	/* Calculate peer NSS capability from HE capabilities if STA
2797 	 * supports HE.
2798 	 */
2799 	for (i = 0, max_nss = 0, he_mcs = 0; i < NL80211_HE_NSS_MAX; i++) {
2800 		he_mcs = he_tx_mcs >> (2 * i) & 3;
2801 
2802 		/* In case of fixed rates, MCS Range in he_tx_mcs might have
2803 		 * unsupported range, with he_mcs_mask set, so check either of them
2804 		 * to find nss.
2805 		 */
2806 		if (he_mcs != IEEE80211_HE_MCS_NOT_SUPPORTED ||
2807 		    he_mcs_mask[i])
2808 			max_nss = i + 1;
2809 	}
2810 
2811 	max_nss = min(max_nss, ar->num_tx_chains);
2812 	arg->peer_nss = min(link_sta->rx_nss, max_nss);
2813 
2814 	if (arg->peer_phymode == MODE_11AX_HE160) {
2815 		tx_nss = ath12k_get_nss_160mhz(ar, ar->num_tx_chains);
2816 		rx_nss = min(arg->peer_nss, tx_nss);
2817 
2818 		arg->peer_nss = min(link_sta->rx_nss, ar->num_rx_chains);
2819 		arg->peer_bw_rxnss_override = ATH12K_BW_NSS_MAP_ENABLE;
2820 
2821 		if (!rx_nss) {
2822 			ath12k_warn(ar->ab, "invalid max_nss\n");
2823 			return;
2824 		}
2825 
2826 		nss_160 = u32_encode_bits(rx_nss - 1, ATH12K_PEER_RX_NSS_160MHZ);
2827 		arg->peer_bw_rxnss_override |= nss_160;
2828 	}
2829 
2830 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
2831 		   "mac he peer %pM nss %d mcs cnt %d nss_override 0x%x\n",
2832 		   arsta->addr, arg->peer_nss,
2833 		   arg->peer_he_mcs_count,
2834 		   arg->peer_bw_rxnss_override);
2835 }
2836 
ath12k_peer_assoc_h_he_6ghz(struct ath12k * ar,struct ath12k_link_vif * arvif,struct ath12k_link_sta * arsta,struct ath12k_wmi_peer_assoc_arg * arg)2837 static void ath12k_peer_assoc_h_he_6ghz(struct ath12k *ar,
2838 					struct ath12k_link_vif *arvif,
2839 					struct ath12k_link_sta *arsta,
2840 					struct ath12k_wmi_peer_assoc_arg *arg)
2841 {
2842 	struct ieee80211_vif *vif = ath12k_ahvif_to_vif(arvif->ahvif);
2843 	struct ieee80211_sta *sta = ath12k_ahsta_to_sta(arsta->ahsta);
2844 	const struct ieee80211_sta_he_cap *he_cap;
2845 	struct ieee80211_link_sta *link_sta;
2846 	struct cfg80211_chan_def def;
2847 	enum nl80211_band band;
2848 	u8 ampdu_factor, mpdu_density;
2849 
2850 	if (WARN_ON(ath12k_mac_vif_link_chan(vif, arvif->link_id, &def)))
2851 		return;
2852 
2853 	band = def.chan->band;
2854 
2855 	link_sta = ath12k_mac_get_link_sta(arsta);
2856 	if (!link_sta) {
2857 		ath12k_warn(ar->ab, "unable to access link sta in peer assoc he 6ghz for sta %pM link %u\n",
2858 			    sta->addr, arsta->link_id);
2859 		return;
2860 	}
2861 
2862 	he_cap = &link_sta->he_cap;
2863 
2864 	if (!arg->he_flag || band != NL80211_BAND_6GHZ || !link_sta->he_6ghz_capa.capa)
2865 		return;
2866 
2867 	if (link_sta->bandwidth == IEEE80211_STA_RX_BW_40)
2868 		arg->bw_40 = true;
2869 
2870 	if (link_sta->bandwidth == IEEE80211_STA_RX_BW_80)
2871 		arg->bw_80 = true;
2872 
2873 	if (link_sta->bandwidth == IEEE80211_STA_RX_BW_160)
2874 		arg->bw_160 = true;
2875 
2876 	if (link_sta->bandwidth == IEEE80211_STA_RX_BW_320)
2877 		arg->bw_320 = true;
2878 
2879 	arg->peer_he_caps_6ghz = le16_to_cpu(link_sta->he_6ghz_capa.capa);
2880 
2881 	mpdu_density = u32_get_bits(arg->peer_he_caps_6ghz,
2882 				    IEEE80211_HE_6GHZ_CAP_MIN_MPDU_START);
2883 	arg->peer_mpdu_density = ath12k_parse_mpdudensity(mpdu_density);
2884 
2885 	/* From IEEE Std 802.11ax-2021 - Section 10.12.2: An HE STA shall be capable of
2886 	 * receiving A-MPDU where the A-MPDU pre-EOF padding length is up to the value
2887 	 * indicated by the Maximum A-MPDU Length Exponent Extension field in the HE
2888 	 * Capabilities element and the Maximum A-MPDU Length Exponent field in HE 6 GHz
2889 	 * Band Capabilities element in the 6 GHz band.
2890 	 *
2891 	 * Here, we are extracting the Max A-MPDU Exponent Extension from HE caps and
2892 	 * factor is the Maximum A-MPDU Length Exponent from HE 6 GHZ Band capability.
2893 	 */
2894 	ampdu_factor = u8_get_bits(he_cap->he_cap_elem.mac_cap_info[3],
2895 				   IEEE80211_HE_MAC_CAP3_MAX_AMPDU_LEN_EXP_MASK) +
2896 			u32_get_bits(arg->peer_he_caps_6ghz,
2897 				     IEEE80211_HE_6GHZ_CAP_MAX_AMPDU_LEN_EXP);
2898 
2899 	arg->peer_max_mpdu = (1u << (IEEE80211_HE_6GHZ_MAX_AMPDU_FACTOR +
2900 				     ampdu_factor)) - 1;
2901 }
2902 
ath12k_get_smps_from_capa(const struct ieee80211_sta_ht_cap * ht_cap,const struct ieee80211_he_6ghz_capa * he_6ghz_capa,int * smps)2903 static int ath12k_get_smps_from_capa(const struct ieee80211_sta_ht_cap *ht_cap,
2904 				     const struct ieee80211_he_6ghz_capa *he_6ghz_capa,
2905 				     int *smps)
2906 {
2907 	if (ht_cap->ht_supported)
2908 		*smps = u16_get_bits(ht_cap->cap, IEEE80211_HT_CAP_SM_PS);
2909 	else
2910 		*smps = le16_get_bits(he_6ghz_capa->capa,
2911 				      IEEE80211_HE_6GHZ_CAP_SM_PS);
2912 
2913 	if (*smps >= ARRAY_SIZE(ath12k_smps_map))
2914 		return -EINVAL;
2915 
2916 	return 0;
2917 }
2918 
ath12k_peer_assoc_h_smps(struct ath12k_link_sta * arsta,struct ath12k_wmi_peer_assoc_arg * arg)2919 static void ath12k_peer_assoc_h_smps(struct ath12k_link_sta *arsta,
2920 				     struct ath12k_wmi_peer_assoc_arg *arg)
2921 {
2922 	struct ieee80211_sta *sta = ath12k_ahsta_to_sta(arsta->ahsta);
2923 	const struct ieee80211_he_6ghz_capa *he_6ghz_capa;
2924 	struct ath12k_link_vif *arvif = arsta->arvif;
2925 	const struct ieee80211_sta_ht_cap *ht_cap;
2926 	struct ieee80211_link_sta *link_sta;
2927 	struct ath12k *ar = arvif->ar;
2928 	int smps;
2929 
2930 	link_sta = ath12k_mac_get_link_sta(arsta);
2931 	if (!link_sta) {
2932 		ath12k_warn(ar->ab, "unable to access link sta in peer assoc he for sta %pM link %u\n",
2933 			    sta->addr, arsta->link_id);
2934 		return;
2935 	}
2936 
2937 	he_6ghz_capa = &link_sta->he_6ghz_capa;
2938 	ht_cap = &link_sta->ht_cap;
2939 
2940 	if (!ht_cap->ht_supported && !he_6ghz_capa->capa)
2941 		return;
2942 
2943 	if (ath12k_get_smps_from_capa(ht_cap, he_6ghz_capa, &smps))
2944 		return;
2945 
2946 	switch (smps) {
2947 	case WLAN_HT_CAP_SM_PS_STATIC:
2948 		arg->static_mimops_flag = true;
2949 		break;
2950 	case WLAN_HT_CAP_SM_PS_DYNAMIC:
2951 		arg->dynamic_mimops_flag = true;
2952 		break;
2953 	case WLAN_HT_CAP_SM_PS_DISABLED:
2954 		arg->spatial_mux_flag = true;
2955 		break;
2956 	default:
2957 		break;
2958 	}
2959 }
2960 
ath12k_peer_assoc_h_qos(struct ath12k * ar,struct ath12k_link_vif * arvif,struct ath12k_link_sta * arsta,struct ath12k_wmi_peer_assoc_arg * arg)2961 static void ath12k_peer_assoc_h_qos(struct ath12k *ar,
2962 				    struct ath12k_link_vif *arvif,
2963 				    struct ath12k_link_sta *arsta,
2964 				    struct ath12k_wmi_peer_assoc_arg *arg)
2965 {
2966 	struct ieee80211_sta *sta = ath12k_ahsta_to_sta(arsta->ahsta);
2967 
2968 	switch (arvif->ahvif->vdev_type) {
2969 	case WMI_VDEV_TYPE_AP:
2970 		if (sta->wme) {
2971 			/* TODO: Check WME vs QoS */
2972 			arg->is_wme_set = true;
2973 			arg->qos_flag = true;
2974 		}
2975 
2976 		if (sta->wme && sta->uapsd_queues) {
2977 			/* TODO: Check WME vs QoS */
2978 			arg->is_wme_set = true;
2979 			arg->apsd_flag = true;
2980 			arg->peer_rate_caps |= WMI_HOST_RC_UAPSD_FLAG;
2981 		}
2982 		break;
2983 	case WMI_VDEV_TYPE_STA:
2984 		if (sta->wme) {
2985 			arg->is_wme_set = true;
2986 			arg->qos_flag = true;
2987 		}
2988 		break;
2989 	default:
2990 		break;
2991 	}
2992 
2993 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac peer %pM qos %d\n",
2994 		   arsta->addr, arg->qos_flag);
2995 }
2996 
ath12k_peer_assoc_qos_ap(struct ath12k * ar,struct ath12k_link_vif * arvif,struct ath12k_link_sta * arsta)2997 static int ath12k_peer_assoc_qos_ap(struct ath12k *ar,
2998 				    struct ath12k_link_vif *arvif,
2999 				    struct ath12k_link_sta *arsta)
3000 {
3001 	struct ieee80211_sta *sta = ath12k_ahsta_to_sta(arsta->ahsta);
3002 	struct ath12k_wmi_ap_ps_arg arg;
3003 	u32 max_sp;
3004 	u32 uapsd;
3005 	int ret;
3006 
3007 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
3008 
3009 	arg.vdev_id = arvif->vdev_id;
3010 
3011 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac uapsd_queues 0x%x max_sp %d\n",
3012 		   sta->uapsd_queues, sta->max_sp);
3013 
3014 	uapsd = 0;
3015 	if (sta->uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_VO)
3016 		uapsd |= WMI_AP_PS_UAPSD_AC3_DELIVERY_EN |
3017 			 WMI_AP_PS_UAPSD_AC3_TRIGGER_EN;
3018 	if (sta->uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_VI)
3019 		uapsd |= WMI_AP_PS_UAPSD_AC2_DELIVERY_EN |
3020 			 WMI_AP_PS_UAPSD_AC2_TRIGGER_EN;
3021 	if (sta->uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_BK)
3022 		uapsd |= WMI_AP_PS_UAPSD_AC1_DELIVERY_EN |
3023 			 WMI_AP_PS_UAPSD_AC1_TRIGGER_EN;
3024 	if (sta->uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_BE)
3025 		uapsd |= WMI_AP_PS_UAPSD_AC0_DELIVERY_EN |
3026 			 WMI_AP_PS_UAPSD_AC0_TRIGGER_EN;
3027 
3028 	max_sp = 0;
3029 	if (sta->max_sp < MAX_WMI_AP_PS_PEER_PARAM_MAX_SP)
3030 		max_sp = sta->max_sp;
3031 
3032 	arg.param = WMI_AP_PS_PEER_PARAM_UAPSD;
3033 	arg.value = uapsd;
3034 	ret = ath12k_wmi_send_set_ap_ps_param_cmd(ar, arsta->addr, &arg);
3035 	if (ret)
3036 		goto err;
3037 
3038 	arg.param = WMI_AP_PS_PEER_PARAM_MAX_SP;
3039 	arg.value = max_sp;
3040 	ret = ath12k_wmi_send_set_ap_ps_param_cmd(ar, arsta->addr, &arg);
3041 	if (ret)
3042 		goto err;
3043 
3044 	/* TODO: revisit during testing */
3045 	arg.param = WMI_AP_PS_PEER_PARAM_SIFS_RESP_FRMTYPE;
3046 	arg.value = DISABLE_SIFS_RESPONSE_TRIGGER;
3047 	ret = ath12k_wmi_send_set_ap_ps_param_cmd(ar, arsta->addr, &arg);
3048 	if (ret)
3049 		goto err;
3050 
3051 	arg.param = WMI_AP_PS_PEER_PARAM_SIFS_RESP_UAPSD;
3052 	arg.value = DISABLE_SIFS_RESPONSE_TRIGGER;
3053 	ret = ath12k_wmi_send_set_ap_ps_param_cmd(ar, arsta->addr, &arg);
3054 	if (ret)
3055 		goto err;
3056 
3057 	return 0;
3058 
3059 err:
3060 	ath12k_warn(ar->ab, "failed to set ap ps peer param %d for vdev %i: %d\n",
3061 		    arg.param, arvif->vdev_id, ret);
3062 	return ret;
3063 }
3064 
ath12k_mac_sta_has_ofdm_only(struct ieee80211_link_sta * sta)3065 static bool ath12k_mac_sta_has_ofdm_only(struct ieee80211_link_sta *sta)
3066 {
3067 	return sta->supp_rates[NL80211_BAND_2GHZ] >>
3068 	       ATH12K_MAC_FIRST_OFDM_RATE_IDX;
3069 }
3070 
ath12k_mac_get_phymode_vht(struct ath12k * ar,struct ieee80211_link_sta * link_sta)3071 static enum wmi_phy_mode ath12k_mac_get_phymode_vht(struct ath12k *ar,
3072 						    struct ieee80211_link_sta *link_sta)
3073 {
3074 	if (link_sta->bandwidth == IEEE80211_STA_RX_BW_160) {
3075 		if (link_sta->vht_cap.cap & (IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_160MHZ |
3076 		    IEEE80211_VHT_CAP_EXT_NSS_BW_MASK))
3077 			return MODE_11AC_VHT160;
3078 
3079 		/* Allow STA to connect even if it does not explicitly advertise 160 MHz
3080 		 * support
3081 		 */
3082 		return MODE_11AC_VHT160;
3083 	}
3084 
3085 	if (link_sta->bandwidth == IEEE80211_STA_RX_BW_80)
3086 		return MODE_11AC_VHT80;
3087 
3088 	if (link_sta->bandwidth == IEEE80211_STA_RX_BW_40)
3089 		return MODE_11AC_VHT40;
3090 
3091 	if (link_sta->bandwidth == IEEE80211_STA_RX_BW_20)
3092 		return MODE_11AC_VHT20;
3093 
3094 	return MODE_UNKNOWN;
3095 }
3096 
ath12k_mac_get_phymode_he(struct ath12k * ar,struct ieee80211_link_sta * link_sta)3097 static enum wmi_phy_mode ath12k_mac_get_phymode_he(struct ath12k *ar,
3098 						   struct ieee80211_link_sta *link_sta)
3099 {
3100 	if (link_sta->bandwidth == IEEE80211_STA_RX_BW_160) {
3101 		if (link_sta->he_cap.he_cap_elem.phy_cap_info[0] &
3102 		     IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_160MHZ_IN_5G)
3103 			return MODE_11AX_HE160;
3104 
3105 		return MODE_UNKNOWN;
3106 	}
3107 
3108 	if (link_sta->bandwidth == IEEE80211_STA_RX_BW_80)
3109 		return MODE_11AX_HE80;
3110 
3111 	if (link_sta->bandwidth == IEEE80211_STA_RX_BW_40)
3112 		return MODE_11AX_HE40;
3113 
3114 	if (link_sta->bandwidth == IEEE80211_STA_RX_BW_20)
3115 		return MODE_11AX_HE20;
3116 
3117 	return MODE_UNKNOWN;
3118 }
3119 
ath12k_mac_get_phymode_eht(struct ath12k * ar,struct ieee80211_link_sta * link_sta)3120 static enum wmi_phy_mode ath12k_mac_get_phymode_eht(struct ath12k *ar,
3121 						    struct ieee80211_link_sta *link_sta)
3122 {
3123 	if (link_sta->bandwidth == IEEE80211_STA_RX_BW_320)
3124 		if (link_sta->eht_cap.eht_cap_elem.phy_cap_info[0] &
3125 		    IEEE80211_EHT_PHY_CAP0_320MHZ_IN_6GHZ)
3126 			return MODE_11BE_EHT320;
3127 
3128 	if (link_sta->bandwidth == IEEE80211_STA_RX_BW_160) {
3129 		if (link_sta->he_cap.he_cap_elem.phy_cap_info[0] &
3130 		    IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_160MHZ_IN_5G)
3131 			return MODE_11BE_EHT160;
3132 
3133 		ath12k_warn(ar->ab, "invalid EHT PHY capability info for 160 Mhz: %d\n",
3134 			    link_sta->he_cap.he_cap_elem.phy_cap_info[0]);
3135 
3136 		return MODE_UNKNOWN;
3137 	}
3138 
3139 	if (link_sta->bandwidth == IEEE80211_STA_RX_BW_80)
3140 		return MODE_11BE_EHT80;
3141 
3142 	if (link_sta->bandwidth == IEEE80211_STA_RX_BW_40)
3143 		return MODE_11BE_EHT40;
3144 
3145 	if (link_sta->bandwidth == IEEE80211_STA_RX_BW_20)
3146 		return MODE_11BE_EHT20;
3147 
3148 	return MODE_UNKNOWN;
3149 }
3150 
3151 static bool
ath12k_peer_assoc_h_eht_masked(const u16 eht_mcs_mask[NL80211_EHT_NSS_MAX])3152 ath12k_peer_assoc_h_eht_masked(const u16 eht_mcs_mask[NL80211_EHT_NSS_MAX])
3153 {
3154 	int nss;
3155 
3156 	for (nss = 0; nss < NL80211_EHT_NSS_MAX; nss++)
3157 		if (eht_mcs_mask[nss])
3158 			return false;
3159 
3160 	return true;
3161 }
3162 
ath12k_peer_assoc_h_phymode(struct ath12k * ar,struct ath12k_link_vif * arvif,struct ath12k_link_sta * arsta,struct ath12k_wmi_peer_assoc_arg * arg)3163 static void ath12k_peer_assoc_h_phymode(struct ath12k *ar,
3164 					struct ath12k_link_vif *arvif,
3165 					struct ath12k_link_sta *arsta,
3166 					struct ath12k_wmi_peer_assoc_arg *arg)
3167 {
3168 	struct ieee80211_link_sta *link_sta;
3169 	struct cfg80211_chan_def def;
3170 	enum nl80211_band band;
3171 	const u8 *ht_mcs_mask;
3172 	const u16 *vht_mcs_mask;
3173 	const u16 *he_mcs_mask;
3174 	const u16 *eht_mcs_mask;
3175 	enum wmi_phy_mode phymode = MODE_UNKNOWN;
3176 
3177 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
3178 
3179 	struct ieee80211_vif *vif = ath12k_ahvif_to_vif(arvif->ahvif);
3180 	struct ieee80211_sta *sta = ath12k_ahsta_to_sta(arsta->ahsta);
3181 
3182 	if (WARN_ON(ath12k_mac_vif_link_chan(vif, arvif->link_id, &def)))
3183 		return;
3184 
3185 	band = def.chan->band;
3186 	ht_mcs_mask = arvif->bitrate_mask.control[band].ht_mcs;
3187 	vht_mcs_mask = arvif->bitrate_mask.control[band].vht_mcs;
3188 	he_mcs_mask = arvif->bitrate_mask.control[band].he_mcs;
3189 	eht_mcs_mask = arvif->bitrate_mask.control[band].eht_mcs;
3190 
3191 	link_sta = ath12k_mac_get_link_sta(arsta);
3192 	if (!link_sta) {
3193 		ath12k_warn(ar->ab, "unable to access link sta in peer assoc he for sta %pM link %u\n",
3194 			    sta->addr, arsta->link_id);
3195 		return;
3196 	}
3197 
3198 	switch (band) {
3199 	case NL80211_BAND_2GHZ:
3200 		if (link_sta->eht_cap.has_eht &&
3201 		    !ath12k_peer_assoc_h_eht_masked(eht_mcs_mask)) {
3202 			if (link_sta->bandwidth == IEEE80211_STA_RX_BW_40)
3203 				phymode = MODE_11BE_EHT40_2G;
3204 			else
3205 				phymode = MODE_11BE_EHT20_2G;
3206 		} else if (link_sta->he_cap.has_he &&
3207 			   !ath12k_peer_assoc_h_he_masked(he_mcs_mask)) {
3208 			if (link_sta->bandwidth == IEEE80211_STA_RX_BW_80)
3209 				phymode = MODE_11AX_HE80_2G;
3210 			else if (link_sta->bandwidth == IEEE80211_STA_RX_BW_40)
3211 				phymode = MODE_11AX_HE40_2G;
3212 			else
3213 				phymode = MODE_11AX_HE20_2G;
3214 		} else if (link_sta->vht_cap.vht_supported &&
3215 		    !ath12k_peer_assoc_h_vht_masked(vht_mcs_mask)) {
3216 			if (link_sta->bandwidth == IEEE80211_STA_RX_BW_40)
3217 				phymode = MODE_11AC_VHT40;
3218 			else
3219 				phymode = MODE_11AC_VHT20;
3220 		} else if (link_sta->ht_cap.ht_supported &&
3221 			   !ath12k_peer_assoc_h_ht_masked(ht_mcs_mask)) {
3222 			if (link_sta->bandwidth == IEEE80211_STA_RX_BW_40)
3223 				phymode = MODE_11NG_HT40;
3224 			else
3225 				phymode = MODE_11NG_HT20;
3226 		} else if (ath12k_mac_sta_has_ofdm_only(link_sta)) {
3227 			phymode = MODE_11G;
3228 		} else {
3229 			phymode = MODE_11B;
3230 		}
3231 		break;
3232 	case NL80211_BAND_5GHZ:
3233 	case NL80211_BAND_6GHZ:
3234 		/* Check EHT first */
3235 		if (link_sta->eht_cap.has_eht) {
3236 			phymode = ath12k_mac_get_phymode_eht(ar, link_sta);
3237 		} else if (link_sta->he_cap.has_he &&
3238 			   !ath12k_peer_assoc_h_he_masked(he_mcs_mask)) {
3239 			phymode = ath12k_mac_get_phymode_he(ar, link_sta);
3240 		} else if (link_sta->vht_cap.vht_supported &&
3241 		    !ath12k_peer_assoc_h_vht_masked(vht_mcs_mask)) {
3242 			phymode = ath12k_mac_get_phymode_vht(ar, link_sta);
3243 		} else if (link_sta->ht_cap.ht_supported &&
3244 			   !ath12k_peer_assoc_h_ht_masked(ht_mcs_mask)) {
3245 			if (link_sta->bandwidth >= IEEE80211_STA_RX_BW_40)
3246 				phymode = MODE_11NA_HT40;
3247 			else
3248 				phymode = MODE_11NA_HT20;
3249 		} else {
3250 			phymode = MODE_11A;
3251 		}
3252 		break;
3253 	default:
3254 		break;
3255 	}
3256 
3257 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac peer %pM phymode %s\n",
3258 		   arsta->addr, ath12k_mac_phymode_str(phymode));
3259 
3260 	arg->peer_phymode = phymode;
3261 	WARN_ON(phymode == MODE_UNKNOWN);
3262 }
3263 
3264 #define ATH12K_EHT_MCS_7_ENABLED	0x00FF
3265 #define ATH12K_EHT_MCS_9_ENABLED	0x0300
3266 #define ATH12K_EHT_MCS_11_ENABLED	0x0C00
3267 #define ATH12K_EHT_MCS_13_ENABLED	0x3000
3268 
ath12k_mac_set_eht_mcs(u8 rx_tx_mcs7,u8 rx_tx_mcs9,u8 rx_tx_mcs11,u8 rx_tx_mcs13,u32 * rx_mcs,u32 * tx_mcs,const u16 eht_mcs_limit[NL80211_EHT_NSS_MAX])3269 static void ath12k_mac_set_eht_mcs(u8 rx_tx_mcs7, u8 rx_tx_mcs9,
3270 				   u8 rx_tx_mcs11, u8 rx_tx_mcs13,
3271 				   u32 *rx_mcs, u32 *tx_mcs,
3272 				   const u16 eht_mcs_limit[NL80211_EHT_NSS_MAX])
3273 {
3274 	int nss;
3275 	u8 mcs_7 = 0, mcs_9 = 0, mcs_11 = 0, mcs_13 = 0;
3276 	u8 peer_mcs_7, peer_mcs_9, peer_mcs_11, peer_mcs_13;
3277 
3278 	for (nss = 0; nss < NL80211_EHT_NSS_MAX; nss++) {
3279 		if (eht_mcs_limit[nss] & ATH12K_EHT_MCS_7_ENABLED)
3280 			mcs_7++;
3281 		if (eht_mcs_limit[nss] & ATH12K_EHT_MCS_9_ENABLED)
3282 			mcs_9++;
3283 		if (eht_mcs_limit[nss] & ATH12K_EHT_MCS_11_ENABLED)
3284 			mcs_11++;
3285 		if (eht_mcs_limit[nss] & ATH12K_EHT_MCS_13_ENABLED)
3286 			mcs_13++;
3287 	}
3288 
3289 	peer_mcs_7 = u8_get_bits(rx_tx_mcs7, IEEE80211_EHT_MCS_NSS_RX);
3290 	peer_mcs_9 = u8_get_bits(rx_tx_mcs9, IEEE80211_EHT_MCS_NSS_RX);
3291 	peer_mcs_11 = u8_get_bits(rx_tx_mcs11, IEEE80211_EHT_MCS_NSS_RX);
3292 	peer_mcs_13 = u8_get_bits(rx_tx_mcs13, IEEE80211_EHT_MCS_NSS_RX);
3293 
3294 	*rx_mcs = u32_encode_bits(min(peer_mcs_7, mcs_7), WMI_EHT_MCS_NSS_0_7) |
3295 		  u32_encode_bits(min(peer_mcs_9, mcs_9), WMI_EHT_MCS_NSS_8_9) |
3296 		  u32_encode_bits(min(peer_mcs_11, mcs_11), WMI_EHT_MCS_NSS_10_11) |
3297 		  u32_encode_bits(min(peer_mcs_13, mcs_13), WMI_EHT_MCS_NSS_12_13);
3298 
3299 	peer_mcs_7 = u8_get_bits(rx_tx_mcs7, IEEE80211_EHT_MCS_NSS_TX);
3300 	peer_mcs_9 = u8_get_bits(rx_tx_mcs9, IEEE80211_EHT_MCS_NSS_TX);
3301 	peer_mcs_11 = u8_get_bits(rx_tx_mcs11, IEEE80211_EHT_MCS_NSS_TX);
3302 	peer_mcs_13 = u8_get_bits(rx_tx_mcs13, IEEE80211_EHT_MCS_NSS_TX);
3303 
3304 	*tx_mcs = u32_encode_bits(min(peer_mcs_7, mcs_7), WMI_EHT_MCS_NSS_0_7) |
3305 		  u32_encode_bits(min(peer_mcs_9, mcs_9), WMI_EHT_MCS_NSS_8_9) |
3306 		  u32_encode_bits(min(peer_mcs_11, mcs_11), WMI_EHT_MCS_NSS_10_11) |
3307 		  u32_encode_bits(min(peer_mcs_13, mcs_13), WMI_EHT_MCS_NSS_12_13);
3308 }
3309 
ath12k_mac_set_eht_ppe_threshold(const u8 * ppe_thres,struct ath12k_wmi_ppe_threshold_arg * ppet)3310 static void ath12k_mac_set_eht_ppe_threshold(const u8 *ppe_thres,
3311 					     struct ath12k_wmi_ppe_threshold_arg *ppet)
3312 {
3313 	u32 bit_pos = IEEE80211_EHT_PPE_THRES_INFO_HEADER_SIZE, val;
3314 	u8 nss, ru, i;
3315 	u8 ppet_bit_len_per_ru = IEEE80211_EHT_PPE_THRES_INFO_PPET_SIZE * 2;
3316 
3317 	ppet->numss_m1 = u8_get_bits(ppe_thres[0], IEEE80211_EHT_PPE_THRES_NSS_MASK);
3318 	ppet->ru_bit_mask = u16_get_bits(get_unaligned_le16(ppe_thres),
3319 					 IEEE80211_EHT_PPE_THRES_RU_INDEX_BITMASK_MASK);
3320 
3321 	for (nss = 0; nss <= ppet->numss_m1; nss++) {
3322 		for (ru = 0;
3323 		     ru < hweight16(IEEE80211_EHT_PPE_THRES_RU_INDEX_BITMASK_MASK);
3324 		     ru++) {
3325 			if ((ppet->ru_bit_mask & BIT(ru)) == 0)
3326 				continue;
3327 
3328 			val = 0;
3329 			for (i = 0; i < ppet_bit_len_per_ru; i++) {
3330 				val |= (((ppe_thres[bit_pos / 8] >>
3331 					  (bit_pos % 8)) & 0x1) << i);
3332 				bit_pos++;
3333 			}
3334 			ppet->ppet16_ppet8_ru3_ru0[nss] |=
3335 					(val << (ru * ppet_bit_len_per_ru));
3336 		}
3337 	}
3338 }
3339 
ath12k_peer_assoc_h_eht(struct ath12k * ar,struct ath12k_link_vif * arvif,struct ath12k_link_sta * arsta,struct ath12k_wmi_peer_assoc_arg * arg)3340 static void ath12k_peer_assoc_h_eht(struct ath12k *ar,
3341 				    struct ath12k_link_vif *arvif,
3342 				    struct ath12k_link_sta *arsta,
3343 				    struct ath12k_wmi_peer_assoc_arg *arg)
3344 {
3345 	struct ieee80211_sta *sta = ath12k_ahsta_to_sta(arsta->ahsta);
3346 	struct ieee80211_vif *vif = ath12k_ahvif_to_vif(arvif->ahvif);
3347 	const struct ieee80211_eht_mcs_nss_supp *own_eht_mcs_nss_supp;
3348 	const struct ieee80211_eht_mcs_nss_supp_20mhz_only *bw_20;
3349 	const struct ieee80211_sta_eht_cap *eht_cap, *own_eht_cap;
3350 	const struct ieee80211_sband_iftype_data *iftd;
3351 	const struct ieee80211_eht_mcs_nss_supp_bw *bw;
3352 	const struct ieee80211_sta_he_cap *he_cap;
3353 	struct ieee80211_link_sta *link_sta;
3354 	struct ieee80211_bss_conf *link_conf;
3355 	struct cfg80211_chan_def def;
3356 	bool user_rate_valid = true;
3357 	enum nl80211_band band;
3358 	int eht_nss, nss_idx;
3359 	u32 *rx_mcs, *tx_mcs;
3360 	u16 *eht_mcs_mask;
3361 	u8 max_nss = 0;
3362 
3363 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
3364 
3365 	link_sta = ath12k_mac_get_link_sta(arsta);
3366 	if (!link_sta) {
3367 		ath12k_warn(ar->ab, "unable to access link sta in peer assoc eht for sta %pM link %u\n",
3368 			    sta->addr, arsta->link_id);
3369 		return;
3370 	}
3371 
3372 	link_conf = ath12k_mac_get_link_bss_conf(arvif);
3373 	if (!link_conf) {
3374 		ath12k_warn(ar->ab, "unable to access link_conf in peer assoc eht set\n");
3375 		return;
3376 	}
3377 
3378 	eht_cap = &link_sta->eht_cap;
3379 	he_cap = &link_sta->he_cap;
3380 	if (!he_cap->has_he || !eht_cap->has_eht)
3381 		return;
3382 
3383 	if (WARN_ON(ath12k_mac_vif_link_chan(vif, arvif->link_id, &def)))
3384 		return;
3385 
3386 	band = def.chan->band;
3387 	eht_mcs_mask = arvif->bitrate_mask.control[band].eht_mcs;
3388 
3389 	iftd = ieee80211_get_sband_iftype_data(&ar->mac.sbands[band], vif->type);
3390 	if (!iftd) {
3391 		ath12k_warn(ar->ab,
3392 			    "unable to access iftype_data in struct ieee80211_supported_band\n");
3393 		return;
3394 	}
3395 
3396 	own_eht_cap = &iftd->eht_cap;
3397 	own_eht_mcs_nss_supp = &own_eht_cap->eht_mcs_nss_supp;
3398 
3399 	arg->eht_flag = true;
3400 
3401 	if ((eht_cap->eht_cap_elem.phy_cap_info[5] &
3402 	     IEEE80211_EHT_PHY_CAP5_PPE_THRESHOLD_PRESENT) &&
3403 	    eht_cap->eht_ppe_thres[0] != 0)
3404 		ath12k_mac_set_eht_ppe_threshold(eht_cap->eht_ppe_thres,
3405 						 &arg->peer_eht_ppet);
3406 
3407 	memcpy(arg->peer_eht_cap_mac, eht_cap->eht_cap_elem.mac_cap_info,
3408 	       sizeof(eht_cap->eht_cap_elem.mac_cap_info));
3409 	memcpy(arg->peer_eht_cap_phy, eht_cap->eht_cap_elem.phy_cap_info,
3410 	       sizeof(eht_cap->eht_cap_elem.phy_cap_info));
3411 
3412 	rx_mcs = arg->peer_eht_rx_mcs_set;
3413 	tx_mcs = arg->peer_eht_tx_mcs_set;
3414 
3415 	eht_nss = ath12k_mac_max_eht_mcs_nss((void *)own_eht_mcs_nss_supp,
3416 					     sizeof(*own_eht_mcs_nss_supp));
3417 	if (eht_nss > link_sta->rx_nss) {
3418 		user_rate_valid = false;
3419 		for (nss_idx = (link_sta->rx_nss - 1); nss_idx >= 0; nss_idx--) {
3420 			if (eht_mcs_mask[nss_idx]) {
3421 				user_rate_valid = true;
3422 				break;
3423 			}
3424 		}
3425 	}
3426 
3427 	if (!user_rate_valid) {
3428 		ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
3429 			   "Setting eht range MCS value to peer supported nss %d for peer %pM\n",
3430 			   link_sta->rx_nss, arsta->addr);
3431 		eht_mcs_mask[link_sta->rx_nss - 1] = eht_mcs_mask[eht_nss - 1];
3432 	}
3433 
3434 	bw_20 = &eht_cap->eht_mcs_nss_supp.only_20mhz;
3435 	bw = &eht_cap->eht_mcs_nss_supp.bw._80;
3436 
3437 	switch (link_sta->bandwidth) {
3438 	case IEEE80211_STA_RX_BW_320:
3439 		bw = &eht_cap->eht_mcs_nss_supp.bw._320;
3440 		ath12k_mac_set_eht_mcs(bw->rx_tx_mcs9_max_nss,
3441 				       bw->rx_tx_mcs9_max_nss,
3442 				       bw->rx_tx_mcs11_max_nss,
3443 				       bw->rx_tx_mcs13_max_nss,
3444 				       &rx_mcs[WMI_EHTCAP_TXRX_MCS_NSS_IDX_320],
3445 				       &tx_mcs[WMI_EHTCAP_TXRX_MCS_NSS_IDX_320],
3446 				       eht_mcs_mask);
3447 		arg->peer_eht_mcs_count++;
3448 		fallthrough;
3449 	case IEEE80211_STA_RX_BW_160:
3450 		bw = &eht_cap->eht_mcs_nss_supp.bw._160;
3451 		ath12k_mac_set_eht_mcs(bw->rx_tx_mcs9_max_nss,
3452 				       bw->rx_tx_mcs9_max_nss,
3453 				       bw->rx_tx_mcs11_max_nss,
3454 				       bw->rx_tx_mcs13_max_nss,
3455 				       &rx_mcs[WMI_EHTCAP_TXRX_MCS_NSS_IDX_160],
3456 				       &tx_mcs[WMI_EHTCAP_TXRX_MCS_NSS_IDX_160],
3457 				       eht_mcs_mask);
3458 		arg->peer_eht_mcs_count++;
3459 		fallthrough;
3460 	default:
3461 		if ((vif->type == NL80211_IFTYPE_AP ||
3462 		     vif->type == NL80211_IFTYPE_MESH_POINT) &&
3463 		    !(link_sta->he_cap.he_cap_elem.phy_cap_info[0] &
3464 		      IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_MASK_ALL)) {
3465 			bw_20 = &eht_cap->eht_mcs_nss_supp.only_20mhz;
3466 
3467 			ath12k_mac_set_eht_mcs(bw_20->rx_tx_mcs7_max_nss,
3468 					       bw_20->rx_tx_mcs9_max_nss,
3469 					       bw_20->rx_tx_mcs11_max_nss,
3470 					       bw_20->rx_tx_mcs13_max_nss,
3471 					       &rx_mcs[WMI_EHTCAP_TXRX_MCS_NSS_IDX_80],
3472 					       &tx_mcs[WMI_EHTCAP_TXRX_MCS_NSS_IDX_80],
3473 					       eht_mcs_mask);
3474 		} else {
3475 			bw = &eht_cap->eht_mcs_nss_supp.bw._80;
3476 			ath12k_mac_set_eht_mcs(bw->rx_tx_mcs9_max_nss,
3477 					       bw->rx_tx_mcs9_max_nss,
3478 					       bw->rx_tx_mcs11_max_nss,
3479 					       bw->rx_tx_mcs13_max_nss,
3480 					       &rx_mcs[WMI_EHTCAP_TXRX_MCS_NSS_IDX_80],
3481 					       &tx_mcs[WMI_EHTCAP_TXRX_MCS_NSS_IDX_80],
3482 					       eht_mcs_mask);
3483 		}
3484 
3485 		arg->peer_eht_mcs_count++;
3486 		break;
3487 	}
3488 
3489 	arg->punct_bitmap = ~arvif->punct_bitmap;
3490 	arg->eht_disable_mcs15 = link_conf->eht_disable_mcs15;
3491 
3492 	if ((vif->type == NL80211_IFTYPE_AP ||
3493 	     vif->type == NL80211_IFTYPE_MESH_POINT) &&
3494 	    !(link_sta->he_cap.he_cap_elem.phy_cap_info[0] &
3495 	      IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_MASK_ALL)) {
3496 		if (bw_20->rx_tx_mcs13_max_nss)
3497 			max_nss = max(max_nss, u8_get_bits(bw_20->rx_tx_mcs13_max_nss,
3498 							   IEEE80211_EHT_MCS_NSS_RX));
3499 		if (bw_20->rx_tx_mcs11_max_nss)
3500 			max_nss = max(max_nss, u8_get_bits(bw_20->rx_tx_mcs11_max_nss,
3501 							   IEEE80211_EHT_MCS_NSS_RX));
3502 		if (bw_20->rx_tx_mcs9_max_nss)
3503 			max_nss = max(max_nss, u8_get_bits(bw_20->rx_tx_mcs9_max_nss,
3504 							   IEEE80211_EHT_MCS_NSS_RX));
3505 		if (bw_20->rx_tx_mcs7_max_nss)
3506 			max_nss = max(max_nss, u8_get_bits(bw_20->rx_tx_mcs7_max_nss,
3507 							   IEEE80211_EHT_MCS_NSS_RX));
3508 	} else {
3509 		if (bw->rx_tx_mcs13_max_nss)
3510 			max_nss = max(max_nss, u8_get_bits(bw->rx_tx_mcs13_max_nss,
3511 							   IEEE80211_EHT_MCS_NSS_RX));
3512 		if (bw->rx_tx_mcs11_max_nss)
3513 			max_nss = max(max_nss, u8_get_bits(bw->rx_tx_mcs11_max_nss,
3514 							   IEEE80211_EHT_MCS_NSS_RX));
3515 		if (bw->rx_tx_mcs9_max_nss)
3516 			max_nss = max(max_nss, u8_get_bits(bw->rx_tx_mcs9_max_nss,
3517 							   IEEE80211_EHT_MCS_NSS_RX));
3518 	}
3519 
3520 	max_nss = min(max_nss, (uint8_t)eht_nss);
3521 
3522 	arg->peer_nss = min(link_sta->rx_nss, max_nss);
3523 
3524 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
3525 		   "mac eht peer %pM nss %d mcs cnt %d ru_punct_bitmap 0x%x\n",
3526 		   arsta->addr, arg->peer_nss, arg->peer_eht_mcs_count,
3527 		   arg->punct_bitmap);
3528 }
3529 
ath12k_peer_assoc_h_mlo(struct ath12k_link_sta * arsta,struct ath12k_wmi_peer_assoc_arg * arg)3530 static void ath12k_peer_assoc_h_mlo(struct ath12k_link_sta *arsta,
3531 				    struct ath12k_wmi_peer_assoc_arg *arg)
3532 {
3533 	struct ieee80211_sta *sta = ath12k_ahsta_to_sta(arsta->ahsta);
3534 	struct peer_assoc_mlo_params *ml = &arg->ml;
3535 	struct ath12k_sta *ahsta = arsta->ahsta;
3536 	struct ath12k_link_sta *arsta_p;
3537 	struct ath12k_link_vif *arvif;
3538 	struct ath12k_hw *ah = arsta->arvif->ar->ah;
3539 	unsigned long links;
3540 	u8 link_id;
3541 	int i;
3542 
3543 	if (!sta->mlo)
3544 		return;
3545 
3546 	if (ah->host_alloc_ml_id &&
3547 	    ahsta->ml_peer_id == ATH12K_MLO_PEER_ID_INVALID)
3548 		return;
3549 
3550 	ml->enabled = true;
3551 	ml->assoc_link = arsta->is_assoc_link;
3552 
3553 	/* For now considering the primary umac based on assoc link */
3554 	ml->primary_umac = arsta->is_assoc_link;
3555 	/*
3556 	 * Only chips that allocate the MLD peer ID on the host send a valid
3557 	 * ml_peer_id in WMI_PEER_ASSOC_CMDID. For chips where the firmware
3558 	 * picks the ID, leave peer_id_valid false to avoid unexpected issues.
3559 	 */
3560 	ml->peer_id_valid = ah->host_alloc_ml_id;
3561 	ml->logical_link_idx_valid = true;
3562 
3563 	ether_addr_copy(ml->mld_addr, sta->addr);
3564 	ml->logical_link_idx = arsta->link_idx;
3565 	/*
3566 	 * WMI_MLO_PEER_ASSOC_PARAMS expects the raw ML peer ID without
3567 	 * the host-side ATH12K_PEER_ML_ID_VALID bookkeeping bit. For chips
3568 	 * where the firmware allocates the ID, the field is unused (the
3569 	 * firmware always allocates regardless of the value here); send 0
3570 	 * to make that intent explicit.
3571 	 */
3572 	ml->ml_peer_id = ah->host_alloc_ml_id ?
3573 			 (ahsta->ml_peer_id & ~ATH12K_PEER_ML_ID_VALID) : 0;
3574 	ml->ieee_link_id = arsta->link_id;
3575 	ml->num_partner_links = 0;
3576 	ml->eml_cap = sta->eml_cap;
3577 	links = ahsta->links_map;
3578 
3579 	rcu_read_lock();
3580 
3581 	i = 0;
3582 
3583 	for_each_set_bit(link_id, &links, IEEE80211_MLD_MAX_NUM_LINKS) {
3584 		if (i >= ATH12K_WMI_MLO_MAX_LINKS)
3585 			break;
3586 
3587 		arsta_p = rcu_dereference(ahsta->link[link_id]);
3588 		arvif = rcu_dereference(ahsta->ahvif->link[link_id]);
3589 
3590 		if (arsta_p == arsta)
3591 			continue;
3592 
3593 		if (!arvif->is_started)
3594 			continue;
3595 
3596 		ml->partner_info[i].vdev_id = arvif->vdev_id;
3597 		ml->partner_info[i].hw_link_id = arvif->ar->pdev->hw_link_id;
3598 		ml->partner_info[i].assoc_link = arsta_p->is_assoc_link;
3599 		ml->partner_info[i].primary_umac = arsta_p->is_assoc_link;
3600 		ml->partner_info[i].logical_link_idx_valid = true;
3601 		ml->partner_info[i].logical_link_idx = arsta_p->link_idx;
3602 		ml->num_partner_links++;
3603 
3604 		i++;
3605 	}
3606 
3607 	rcu_read_unlock();
3608 }
3609 
ath12k_peer_assoc_prepare(struct ath12k * ar,struct ath12k_link_vif * arvif,struct ath12k_link_sta * arsta,struct ath12k_wmi_peer_assoc_arg * arg,bool reassoc)3610 static void ath12k_peer_assoc_prepare(struct ath12k *ar,
3611 				      struct ath12k_link_vif *arvif,
3612 				      struct ath12k_link_sta *arsta,
3613 				      struct ath12k_wmi_peer_assoc_arg *arg,
3614 				      bool reassoc)
3615 {
3616 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
3617 
3618 	memset(arg, 0, sizeof(*arg));
3619 
3620 	arg->peer_new_assoc = !reassoc;
3621 	ath12k_peer_assoc_h_basic(ar, arvif, arsta, arg);
3622 	ath12k_peer_assoc_h_crypto(ar, arvif, arsta, arg);
3623 	ath12k_peer_assoc_h_rates(ar, arvif, arsta, arg);
3624 	ath12k_peer_assoc_h_ht(ar, arvif, arsta, arg);
3625 	ath12k_peer_assoc_h_vht(ar, arvif, arsta, arg);
3626 	ath12k_peer_assoc_h_he(ar, arvif, arsta, arg);
3627 	ath12k_peer_assoc_h_he_6ghz(ar, arvif, arsta, arg);
3628 	ath12k_peer_assoc_h_eht(ar, arvif, arsta, arg);
3629 	ath12k_peer_assoc_h_qos(ar, arvif, arsta, arg);
3630 	ath12k_peer_assoc_h_phymode(ar, arvif, arsta, arg);
3631 	ath12k_peer_assoc_h_smps(arsta, arg);
3632 	ath12k_peer_assoc_h_mlo(arsta, arg);
3633 
3634 	arsta->peer_nss = arg->peer_nss;
3635 	/* TODO: amsdu_disable req? */
3636 }
3637 
ath12k_setup_peer_smps(struct ath12k * ar,struct ath12k_link_vif * arvif,const u8 * addr,const struct ieee80211_sta_ht_cap * ht_cap,const struct ieee80211_he_6ghz_capa * he_6ghz_capa)3638 static int ath12k_setup_peer_smps(struct ath12k *ar, struct ath12k_link_vif *arvif,
3639 				  const u8 *addr,
3640 				  const struct ieee80211_sta_ht_cap *ht_cap,
3641 				  const struct ieee80211_he_6ghz_capa *he_6ghz_capa)
3642 {
3643 	int smps, ret = 0;
3644 
3645 	if (!ht_cap->ht_supported && !he_6ghz_capa)
3646 		return 0;
3647 
3648 	ret = ath12k_get_smps_from_capa(ht_cap, he_6ghz_capa, &smps);
3649 	if (ret < 0)
3650 		return ret;
3651 
3652 	return ath12k_wmi_set_peer_param(ar, addr, arvif->vdev_id,
3653 					 WMI_PEER_MIMO_PS_STATE,
3654 					 ath12k_smps_map[smps]);
3655 }
3656 
ath12k_mac_set_he_txbf_conf(struct ath12k_link_vif * arvif)3657 static int ath12k_mac_set_he_txbf_conf(struct ath12k_link_vif *arvif)
3658 {
3659 	struct ath12k_vif *ahvif = arvif->ahvif;
3660 	struct ath12k *ar = arvif->ar;
3661 	u32 param = WMI_VDEV_PARAM_SET_HEMU_MODE;
3662 	u32 value = 0;
3663 	int ret;
3664 	struct ieee80211_bss_conf *link_conf;
3665 
3666 	link_conf = ath12k_mac_get_link_bss_conf(arvif);
3667 	if (!link_conf) {
3668 		ath12k_warn(ar->ab, "unable to access bss link conf in txbf conf\n");
3669 		return -EINVAL;
3670 	}
3671 
3672 	if (!link_conf->he_support)
3673 		return 0;
3674 
3675 	if (link_conf->he_su_beamformer) {
3676 		value |= u32_encode_bits(HE_SU_BFER_ENABLE, HE_MODE_SU_TX_BFER);
3677 		if (link_conf->he_mu_beamformer &&
3678 		    ahvif->vdev_type == WMI_VDEV_TYPE_AP)
3679 			value |= u32_encode_bits(HE_MU_BFER_ENABLE, HE_MODE_MU_TX_BFER);
3680 	}
3681 
3682 	if (ahvif->vif->type != NL80211_IFTYPE_MESH_POINT) {
3683 		value |= u32_encode_bits(HE_DL_MUOFDMA_ENABLE, HE_MODE_DL_OFDMA) |
3684 			 u32_encode_bits(HE_UL_MUOFDMA_ENABLE, HE_MODE_UL_OFDMA);
3685 
3686 		if (link_conf->he_full_ul_mumimo)
3687 			value |= u32_encode_bits(HE_UL_MUMIMO_ENABLE, HE_MODE_UL_MUMIMO);
3688 
3689 		if (link_conf->he_su_beamformee)
3690 			value |= u32_encode_bits(HE_SU_BFEE_ENABLE, HE_MODE_SU_TX_BFEE);
3691 	}
3692 
3693 	ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id, param, value);
3694 	if (ret) {
3695 		ath12k_warn(ar->ab, "failed to set vdev %d HE MU mode: %d\n",
3696 			    arvif->vdev_id, ret);
3697 		return ret;
3698 	}
3699 
3700 	param = WMI_VDEV_PARAM_SET_HE_SOUNDING_MODE;
3701 	value =	u32_encode_bits(HE_VHT_SOUNDING_MODE_ENABLE, HE_VHT_SOUNDING_MODE) |
3702 		u32_encode_bits(HE_TRIG_NONTRIG_SOUNDING_MODE_ENABLE,
3703 				HE_TRIG_NONTRIG_SOUNDING_MODE);
3704 	ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
3705 					    param, value);
3706 	if (ret) {
3707 		ath12k_warn(ar->ab, "failed to set vdev %d sounding mode: %d\n",
3708 			    arvif->vdev_id, ret);
3709 		return ret;
3710 	}
3711 
3712 	return 0;
3713 }
3714 
ath12k_mac_vif_recalc_sta_he_txbf(struct ath12k * ar,struct ath12k_link_vif * arvif,struct ieee80211_sta_he_cap * he_cap,int * hemode)3715 static int ath12k_mac_vif_recalc_sta_he_txbf(struct ath12k *ar,
3716 					     struct ath12k_link_vif *arvif,
3717 					     struct ieee80211_sta_he_cap *he_cap,
3718 					     int *hemode)
3719 {
3720 	struct ieee80211_vif *vif = arvif->ahvif->vif;
3721 	struct ieee80211_he_cap_elem he_cap_elem = {};
3722 	struct ieee80211_sta_he_cap *cap_band;
3723 	struct cfg80211_chan_def def;
3724 	u8 link_id = arvif->link_id;
3725 	struct ieee80211_bss_conf *link_conf;
3726 
3727 	link_conf = ath12k_mac_get_link_bss_conf(arvif);
3728 	if (!link_conf) {
3729 		ath12k_warn(ar->ab, "unable to access bss link conf in recalc txbf conf\n");
3730 		return -EINVAL;
3731 	}
3732 
3733 	if (!link_conf->he_support)
3734 		return 0;
3735 
3736 	if (vif->type != NL80211_IFTYPE_STATION)
3737 		return -EINVAL;
3738 
3739 	if (WARN_ON(ath12k_mac_vif_link_chan(vif, link_id, &def)))
3740 		return -EINVAL;
3741 
3742 	if (def.chan->band == NL80211_BAND_2GHZ)
3743 		cap_band = &ar->mac.iftype[NL80211_BAND_2GHZ][vif->type].he_cap;
3744 	else
3745 		cap_band = &ar->mac.iftype[NL80211_BAND_5GHZ][vif->type].he_cap;
3746 
3747 	memcpy(&he_cap_elem, &cap_band->he_cap_elem, sizeof(he_cap_elem));
3748 
3749 	*hemode = 0;
3750 	if (HECAP_PHY_SUBFME_GET(he_cap_elem.phy_cap_info)) {
3751 		if (HECAP_PHY_SUBFMR_GET(he_cap->he_cap_elem.phy_cap_info))
3752 			*hemode |= u32_encode_bits(HE_SU_BFEE_ENABLE, HE_MODE_SU_TX_BFEE);
3753 		if (HECAP_PHY_MUBFMR_GET(he_cap->he_cap_elem.phy_cap_info))
3754 			*hemode |= u32_encode_bits(HE_MU_BFEE_ENABLE, HE_MODE_MU_TX_BFEE);
3755 	}
3756 
3757 	if (vif->type != NL80211_IFTYPE_MESH_POINT) {
3758 		*hemode |= u32_encode_bits(HE_DL_MUOFDMA_ENABLE, HE_MODE_DL_OFDMA) |
3759 			  u32_encode_bits(HE_UL_MUOFDMA_ENABLE, HE_MODE_UL_OFDMA);
3760 
3761 		if (HECAP_PHY_ULMUMIMO_GET(he_cap_elem.phy_cap_info))
3762 			if (HECAP_PHY_ULMUMIMO_GET(he_cap->he_cap_elem.phy_cap_info))
3763 				*hemode |= u32_encode_bits(HE_UL_MUMIMO_ENABLE,
3764 							  HE_MODE_UL_MUMIMO);
3765 
3766 		if (u32_get_bits(*hemode, HE_MODE_MU_TX_BFEE))
3767 			*hemode |= u32_encode_bits(HE_SU_BFEE_ENABLE, HE_MODE_SU_TX_BFEE);
3768 
3769 		if (u32_get_bits(*hemode, HE_MODE_MU_TX_BFER))
3770 			*hemode |= u32_encode_bits(HE_SU_BFER_ENABLE, HE_MODE_SU_TX_BFER);
3771 	}
3772 
3773 	return 0;
3774 }
3775 
ath12k_mac_set_eht_txbf_conf(struct ath12k_link_vif * arvif)3776 static int ath12k_mac_set_eht_txbf_conf(struct ath12k_link_vif *arvif)
3777 {
3778 	struct ath12k_vif *ahvif = arvif->ahvif;
3779 	struct ath12k *ar = arvif->ar;
3780 	u32 param = WMI_VDEV_PARAM_SET_EHT_MU_MODE;
3781 	u32 value = 0;
3782 	int ret;
3783 	struct ieee80211_bss_conf *link_conf;
3784 
3785 	link_conf = ath12k_mac_get_link_bss_conf(arvif);
3786 	if (!link_conf) {
3787 		ath12k_warn(ar->ab, "unable to access bss link conf in eht txbf conf\n");
3788 		return -ENOENT;
3789 	}
3790 
3791 	if (!link_conf->eht_support)
3792 		return 0;
3793 
3794 	if (link_conf->eht_su_beamformer) {
3795 		value |= u32_encode_bits(EHT_SU_BFER_ENABLE, EHT_MODE_SU_TX_BFER);
3796 		if (link_conf->eht_mu_beamformer &&
3797 		    ahvif->vdev_type == WMI_VDEV_TYPE_AP)
3798 			value |= u32_encode_bits(EHT_MU_BFER_ENABLE,
3799 						 EHT_MODE_MU_TX_BFER) |
3800 				 u32_encode_bits(EHT_DL_MUOFDMA_ENABLE,
3801 						 EHT_MODE_DL_OFDMA_MUMIMO) |
3802 				 u32_encode_bits(EHT_UL_MUOFDMA_ENABLE,
3803 						 EHT_MODE_UL_OFDMA_MUMIMO);
3804 	}
3805 
3806 	if (ahvif->vif->type != NL80211_IFTYPE_MESH_POINT) {
3807 		value |= u32_encode_bits(EHT_DL_MUOFDMA_ENABLE, EHT_MODE_DL_OFDMA) |
3808 			 u32_encode_bits(EHT_UL_MUOFDMA_ENABLE, EHT_MODE_UL_OFDMA);
3809 
3810 		if (link_conf->eht_80mhz_full_bw_ul_mumimo)
3811 			value |= u32_encode_bits(EHT_UL_MUMIMO_ENABLE, EHT_MODE_MUMIMO);
3812 
3813 		if (link_conf->eht_su_beamformee)
3814 			value |= u32_encode_bits(EHT_SU_BFEE_ENABLE,
3815 						 EHT_MODE_SU_TX_BFEE);
3816 	}
3817 
3818 	ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id, param, value);
3819 	if (ret) {
3820 		ath12k_warn(ar->ab, "failed to set vdev %d EHT MU mode: %d\n",
3821 			    arvif->vdev_id, ret);
3822 		return ret;
3823 	}
3824 
3825 	return 0;
3826 }
3827 
ath12k_mac_ieee80211_sta_bw_to_wmi(struct ath12k * ar,struct ieee80211_link_sta * link_sta)3828 static u32 ath12k_mac_ieee80211_sta_bw_to_wmi(struct ath12k *ar,
3829 					      struct ieee80211_link_sta *link_sta)
3830 {
3831 	u32 bw;
3832 
3833 	switch (link_sta->bandwidth) {
3834 	case IEEE80211_STA_RX_BW_20:
3835 		bw = WMI_PEER_CHWIDTH_20MHZ;
3836 		break;
3837 	case IEEE80211_STA_RX_BW_40:
3838 		bw = WMI_PEER_CHWIDTH_40MHZ;
3839 		break;
3840 	case IEEE80211_STA_RX_BW_80:
3841 		bw = WMI_PEER_CHWIDTH_80MHZ;
3842 		break;
3843 	case IEEE80211_STA_RX_BW_160:
3844 		bw = WMI_PEER_CHWIDTH_160MHZ;
3845 		break;
3846 	case IEEE80211_STA_RX_BW_320:
3847 		bw = WMI_PEER_CHWIDTH_320MHZ;
3848 		break;
3849 	default:
3850 		ath12k_warn(ar->ab, "Invalid bandwidth %d for link station %pM\n",
3851 			    link_sta->bandwidth, link_sta->addr);
3852 		bw = WMI_PEER_CHWIDTH_20MHZ;
3853 		break;
3854 	}
3855 
3856 	return bw;
3857 }
3858 
ath12k_mac_peer_assoc(struct ath12k * ar,struct ath12k_wmi_peer_assoc_arg * peer_arg)3859 static int ath12k_mac_peer_assoc(struct ath12k *ar,
3860 				 struct ath12k_wmi_peer_assoc_arg *peer_arg)
3861 {
3862 	struct ath12k_hw *ah = ath12k_ar_to_ah(ar);
3863 	int ret;
3864 
3865 	reinit_completion(&ar->peer_assoc_done);
3866 	reinit_completion(&ah->peer_ml_id_done);
3867 
3868 	ret = ath12k_wmi_send_peer_assoc_cmd(ar, peer_arg);
3869 	if (ret) {
3870 		ath12k_warn(ar->ab, "failed to run peer assoc for %pM vdev %i: %d\n",
3871 			    peer_arg->peer_mac, peer_arg->vdev_id, ret);
3872 		return ret;
3873 	}
3874 
3875 	if (!wait_for_completion_timeout(&ar->peer_assoc_done, 1 * HZ)) {
3876 		ath12k_warn(ar->ab, "failed to get peer assoc conf event for %pM vdev %i\n",
3877 			    peer_arg->peer_mac, peer_arg->vdev_id);
3878 		return -ETIMEDOUT;
3879 	}
3880 
3881 	/*
3882 	 * For devices where the firmware allocates the MLD peer ID, the host
3883 	 * learns the real ID only from the MLO_RX_PEER_MAP HTT event, which is
3884 	 * handled in a softirq (BH workqueue) context that cannot take the
3885 	 * wiphy lock. Block here, while still holding the wiphy lock, until
3886 	 * that event has fixed up the ID. This serialises the fixup against
3887 	 * all other wiphy-locked ml_peer_id accesses.
3888 	 *
3889 	 * The firmware sends the event only once, in response to the assoc-link
3890 	 * peer assoc, so block only for that link.
3891 	 */
3892 	if (!ah->host_alloc_ml_id &&
3893 	    peer_arg->is_assoc &&
3894 	    peer_arg->ml.enabled &&
3895 	    peer_arg->ml.assoc_link &&
3896 	    !wait_for_completion_timeout(&ah->peer_ml_id_done, 1 * HZ)) {
3897 		ath12k_warn(ar->ab, "failed to get MLO peer map event for %pM vdev %i\n",
3898 			    peer_arg->peer_mac, peer_arg->vdev_id);
3899 		return -ETIMEDOUT;
3900 	}
3901 
3902 	return 0;
3903 }
3904 
ath12k_bss_assoc(struct ath12k * ar,struct ath12k_link_vif * arvif,struct ieee80211_bss_conf * bss_conf)3905 static void ath12k_bss_assoc(struct ath12k *ar,
3906 			     struct ath12k_link_vif *arvif,
3907 			     struct ieee80211_bss_conf *bss_conf)
3908 {
3909 	struct ath12k_vif *ahvif = arvif->ahvif;
3910 	struct ieee80211_vif *vif = ath12k_ahvif_to_vif(ahvif);
3911 	struct ath12k_wmi_vdev_up_params params = {};
3912 	struct ieee80211_link_sta *link_sta;
3913 	u8 link_id = bss_conf->link_id;
3914 	struct ath12k_link_sta *arsta;
3915 	struct ieee80211_sta *ap_sta;
3916 	struct ath12k_sta *ahsta;
3917 	struct ath12k_dp_link_peer *peer;
3918 	bool is_auth = false;
3919 	u32 hemode = 0;
3920 	int ret;
3921 	struct ath12k_dp *dp = ath12k_ab_to_dp(ar->ab);
3922 
3923 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
3924 
3925 	struct ath12k_wmi_peer_assoc_arg *peer_arg __free(kfree) =
3926 					kzalloc_obj(*peer_arg);
3927 	if (!peer_arg)
3928 		return;
3929 
3930 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
3931 		   "mac vdev %i link id %u assoc bssid %pM aid %d\n",
3932 		   arvif->vdev_id, link_id, arvif->bssid, ahvif->aid);
3933 
3934 	rcu_read_lock();
3935 
3936 	/* During ML connection, cfg.ap_addr has the MLD address. For
3937 	 * non-ML connection, it has the BSSID.
3938 	 */
3939 	ap_sta = ieee80211_find_sta(vif, vif->cfg.ap_addr);
3940 	if (!ap_sta) {
3941 		ath12k_warn(ar->ab, "failed to find station entry for bss %pM vdev %i\n",
3942 			    vif->cfg.ap_addr, arvif->vdev_id);
3943 		rcu_read_unlock();
3944 		return;
3945 	}
3946 
3947 	ahsta = ath12k_sta_to_ahsta(ap_sta);
3948 
3949 	arsta = wiphy_dereference(ath12k_ar_to_hw(ar)->wiphy,
3950 				  ahsta->link[link_id]);
3951 	if (WARN_ON(!arsta)) {
3952 		rcu_read_unlock();
3953 		return;
3954 	}
3955 
3956 	link_sta = ath12k_mac_get_link_sta(arsta);
3957 	if (WARN_ON(!link_sta)) {
3958 		rcu_read_unlock();
3959 		return;
3960 	}
3961 
3962 	ath12k_peer_assoc_prepare(ar, arvif, arsta, peer_arg, false);
3963 
3964 	/* link_sta->he_cap must be protected by rcu_read_lock */
3965 	ret = ath12k_mac_vif_recalc_sta_he_txbf(ar, arvif, &link_sta->he_cap, &hemode);
3966 	if (ret) {
3967 		ath12k_warn(ar->ab, "failed to recalc he txbf for vdev %i on bss %pM: %d\n",
3968 			    arvif->vdev_id, bss_conf->bssid, ret);
3969 		rcu_read_unlock();
3970 		return;
3971 	}
3972 
3973 	rcu_read_unlock();
3974 
3975 	/* keep this before ath12k_wmi_send_peer_assoc_cmd() */
3976 	ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
3977 					    WMI_VDEV_PARAM_SET_HEMU_MODE, hemode);
3978 	if (ret) {
3979 		ath12k_warn(ar->ab, "failed to submit vdev param txbf 0x%x: %d\n",
3980 			    hemode, ret);
3981 		return;
3982 	}
3983 
3984 	peer_arg->is_assoc = true;
3985 
3986 	ret = ath12k_mac_peer_assoc(ar, peer_arg);
3987 	if (ret)
3988 		return;
3989 
3990 	ret = ath12k_setup_peer_smps(ar, arvif, bss_conf->bssid,
3991 				     &link_sta->ht_cap, &link_sta->he_6ghz_capa);
3992 	if (ret) {
3993 		ath12k_warn(ar->ab, "failed to setup peer SMPS for vdev %d: %d\n",
3994 			    arvif->vdev_id, ret);
3995 		return;
3996 	}
3997 
3998 	WARN_ON(arvif->is_up);
3999 
4000 	ahvif->aid = vif->cfg.aid;
4001 	ether_addr_copy(arvif->bssid, bss_conf->bssid);
4002 
4003 	params.vdev_id = arvif->vdev_id;
4004 	params.aid = ahvif->aid;
4005 	params.bssid = arvif->bssid;
4006 	params.tx_bssid = ath12k_mac_get_tx_bssid(arvif);
4007 	if (params.tx_bssid) {
4008 		params.nontx_profile_idx = bss_conf->bssid_index;
4009 		params.nontx_profile_cnt = 1 << bss_conf->bssid_indicator;
4010 	}
4011 	ret = ath12k_wmi_vdev_up(ar, &params);
4012 	if (ret) {
4013 		ath12k_warn(ar->ab, "failed to set vdev %d up: %d\n",
4014 			    arvif->vdev_id, ret);
4015 		return;
4016 	}
4017 
4018 	arvif->is_up = true;
4019 	arvif->rekey_data.enable_offload = false;
4020 
4021 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
4022 		   "mac vdev %d up (associated) bssid %pM aid %d\n",
4023 		   arvif->vdev_id, bss_conf->bssid, vif->cfg.aid);
4024 
4025 	spin_lock_bh(&dp->dp_lock);
4026 
4027 	peer = ath12k_dp_link_peer_find_by_vdev_and_addr(dp, arvif->vdev_id,
4028 							 arvif->bssid);
4029 	if (peer && peer->is_authorized)
4030 		is_auth = true;
4031 
4032 	spin_unlock_bh(&dp->dp_lock);
4033 
4034 	/* Authorize BSS Peer */
4035 	if (is_auth) {
4036 		ret = ath12k_wmi_set_peer_param(ar, arvif->bssid,
4037 						arvif->vdev_id,
4038 						WMI_PEER_AUTHORIZE,
4039 						1);
4040 		if (ret)
4041 			ath12k_warn(ar->ab, "Unable to authorize BSS peer: %d\n", ret);
4042 	}
4043 
4044 	ret = ath12k_wmi_send_obss_spr_cmd(ar, arvif->vdev_id,
4045 					   &bss_conf->he_obss_pd);
4046 	if (ret)
4047 		ath12k_warn(ar->ab, "failed to set vdev %i OBSS PD parameters: %d\n",
4048 			    arvif->vdev_id, ret);
4049 
4050 	if (ar->ab->hw_params->supports_sta_ps &&
4051 	    ahvif->vdev_type == WMI_VDEV_TYPE_STA &&
4052 	    ahvif->vdev_subtype == WMI_VDEV_SUBTYPE_NONE) {
4053 		ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
4054 						    WMI_VDEV_PARAM_DTIM_POLICY,
4055 						    WMI_DTIM_POLICY_STICK);
4056 		if (ret)
4057 			ath12k_warn(ar->ab, "failed to set vdev %d stick DTIM policy: %d\n",
4058 				    arvif->vdev_id, ret);
4059 	}
4060 
4061 	if (test_bit(WMI_TLV_SERVICE_11D_OFFLOAD, ar->ab->wmi_ab.svc_map) &&
4062 	    ahvif->vdev_type == WMI_VDEV_TYPE_STA &&
4063 	    ahvif->vdev_subtype == WMI_VDEV_SUBTYPE_NONE)
4064 		ath12k_mac_11d_scan_stop_all(ar->ab);
4065 }
4066 
ath12k_bss_disassoc(struct ath12k * ar,struct ath12k_link_vif * arvif)4067 static void ath12k_bss_disassoc(struct ath12k *ar,
4068 				struct ath12k_link_vif *arvif)
4069 {
4070 	int ret;
4071 
4072 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
4073 
4074 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac vdev %i disassoc bssid %pM\n",
4075 		   arvif->vdev_id, arvif->bssid);
4076 
4077 	ret = ath12k_wmi_vdev_down(ar, arvif->vdev_id);
4078 	if (ret)
4079 		ath12k_warn(ar->ab, "failed to down vdev %i: %d\n",
4080 			    arvif->vdev_id, ret);
4081 
4082 	arvif->is_up = false;
4083 
4084 	memset(&arvif->rekey_data, 0, sizeof(arvif->rekey_data));
4085 
4086 	cancel_delayed_work(&arvif->connection_loss_work);
4087 }
4088 
ath12k_mac_get_rate_hw_value(int bitrate)4089 static u32 ath12k_mac_get_rate_hw_value(int bitrate)
4090 {
4091 	u32 preamble;
4092 	u16 hw_value;
4093 	int rate;
4094 	size_t i;
4095 
4096 	if (ath12k_mac_bitrate_is_cck(bitrate))
4097 		preamble = WMI_RATE_PREAMBLE_CCK;
4098 	else
4099 		preamble = WMI_RATE_PREAMBLE_OFDM;
4100 
4101 	for (i = 0; i < ARRAY_SIZE(ath12k_legacy_rates); i++) {
4102 		if (ath12k_legacy_rates[i].bitrate != bitrate)
4103 			continue;
4104 
4105 		hw_value = ath12k_legacy_rates[i].hw_value;
4106 		rate = ATH12K_HW_RATE_CODE(hw_value, 0, preamble);
4107 
4108 		return rate;
4109 	}
4110 
4111 	return -EINVAL;
4112 }
4113 
ath12k_recalculate_mgmt_rate(struct ath12k * ar,struct ath12k_link_vif * arvif,struct cfg80211_chan_def * def)4114 static void ath12k_recalculate_mgmt_rate(struct ath12k *ar,
4115 					 struct ath12k_link_vif *arvif,
4116 					 struct cfg80211_chan_def *def)
4117 {
4118 	struct ieee80211_vif *vif = ath12k_ahvif_to_vif(arvif->ahvif);
4119 	struct ieee80211_hw *hw = ath12k_ar_to_hw(ar);
4120 	const struct ieee80211_supported_band *sband;
4121 	struct ieee80211_bss_conf *bss_conf;
4122 	u8 basic_rate_idx;
4123 	int hw_rate_code;
4124 	u32 vdev_param;
4125 	u16 bitrate;
4126 	int ret;
4127 
4128 	lockdep_assert_wiphy(hw->wiphy);
4129 
4130 	bss_conf = ath12k_mac_get_link_bss_conf(arvif);
4131 	if (!bss_conf) {
4132 		ath12k_warn(ar->ab, "unable to access bss link conf in mgmt rate calc for vif %pM link %u\n",
4133 			    vif->addr, arvif->link_id);
4134 		return;
4135 	}
4136 
4137 	sband = hw->wiphy->bands[def->chan->band];
4138 	if (bss_conf->basic_rates)
4139 		basic_rate_idx = __ffs(bss_conf->basic_rates);
4140 	else
4141 		basic_rate_idx = 0;
4142 	bitrate = sband->bitrates[basic_rate_idx].bitrate;
4143 
4144 	hw_rate_code = ath12k_mac_get_rate_hw_value(bitrate);
4145 	if (hw_rate_code < 0) {
4146 		ath12k_warn(ar->ab, "bitrate not supported %d\n", bitrate);
4147 		return;
4148 	}
4149 
4150 	vdev_param = WMI_VDEV_PARAM_MGMT_RATE;
4151 	ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id, vdev_param,
4152 					    hw_rate_code);
4153 	if (ret)
4154 		ath12k_warn(ar->ab, "failed to set mgmt tx rate %d\n", ret);
4155 
4156 	vdev_param = WMI_VDEV_PARAM_BEACON_RATE;
4157 	ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id, vdev_param,
4158 					    hw_rate_code);
4159 	if (ret)
4160 		ath12k_warn(ar->ab, "failed to set beacon tx rate %d\n", ret);
4161 }
4162 
ath12k_mac_bcn_tx_event(struct ath12k_link_vif * arvif)4163 static void ath12k_mac_bcn_tx_event(struct ath12k_link_vif *arvif)
4164 {
4165 	struct ieee80211_vif *vif = ath12k_ahvif_to_vif(arvif->ahvif);
4166 	struct ieee80211_bss_conf *link_conf;
4167 
4168 	link_conf = ath12k_mac_get_link_bss_conf(arvif);
4169 	if (!link_conf) {
4170 		ath12k_warn(arvif->ar->ab, "failed to get link conf for vdev %u\n",
4171 			    arvif->vdev_id);
4172 		return;
4173 	}
4174 
4175 	if (link_conf->color_change_active) {
4176 		if (ieee80211_beacon_cntdwn_is_complete(vif, arvif->link_id)) {
4177 			ieee80211_color_change_finish(vif, arvif->link_id);
4178 			return;
4179 		}
4180 
4181 		ieee80211_beacon_update_cntdwn(vif, arvif->link_id);
4182 		ath12k_mac_setup_bcn_tmpl(arvif);
4183 	}
4184 }
4185 
ath12k_mac_bcn_tx_work(struct wiphy * wiphy,struct wiphy_work * work)4186 static void ath12k_mac_bcn_tx_work(struct wiphy *wiphy, struct wiphy_work *work)
4187 {
4188 	struct ath12k_link_vif *arvif = container_of(work, struct ath12k_link_vif,
4189 						     bcn_tx_work);
4190 
4191 	lockdep_assert_wiphy(wiphy);
4192 	ath12k_mac_bcn_tx_event(arvif);
4193 }
4194 
ath12k_mac_init_arvif(struct ath12k_vif * ahvif,struct ath12k_link_vif * arvif,int link_id)4195 static void ath12k_mac_init_arvif(struct ath12k_vif *ahvif,
4196 				  struct ath12k_link_vif *arvif, int link_id)
4197 {
4198 	struct ath12k_hw *ah = ahvif->ah;
4199 	u8 _link_id;
4200 	int i;
4201 
4202 	lockdep_assert_wiphy(ah->hw->wiphy);
4203 
4204 	if (WARN_ON(!arvif))
4205 		return;
4206 
4207 	if (WARN_ON(link_id >= ATH12K_NUM_MAX_LINKS))
4208 		return;
4209 
4210 	if (link_id < 0)
4211 		_link_id = 0;
4212 	else
4213 		_link_id = link_id;
4214 
4215 	arvif->ahvif = ahvif;
4216 	arvif->link_id = _link_id;
4217 
4218 	/* Protects the datapath stats update on a per link basis */
4219 	spin_lock_init(&arvif->link_stats_lock);
4220 
4221 	INIT_LIST_HEAD(&arvif->list);
4222 	INIT_DELAYED_WORK(&arvif->connection_loss_work,
4223 			  ath12k_mac_vif_sta_connection_loss_work);
4224 	wiphy_work_init(&arvif->bcn_tx_work, ath12k_mac_bcn_tx_work);
4225 
4226 	arvif->num_stations = 0;
4227 
4228 	for (i = 0; i < ARRAY_SIZE(arvif->bitrate_mask.control); i++) {
4229 		arvif->bitrate_mask.control[i].legacy = 0xffffffff;
4230 		arvif->bitrate_mask.control[i].gi = NL80211_TXRATE_DEFAULT_GI;
4231 		memset(arvif->bitrate_mask.control[i].ht_mcs, 0xff,
4232 		       sizeof(arvif->bitrate_mask.control[i].ht_mcs));
4233 		memset(arvif->bitrate_mask.control[i].vht_mcs, 0xff,
4234 		       sizeof(arvif->bitrate_mask.control[i].vht_mcs));
4235 		memset(arvif->bitrate_mask.control[i].he_mcs, 0xff,
4236 		       sizeof(arvif->bitrate_mask.control[i].he_mcs));
4237 		memset(arvif->bitrate_mask.control[i].eht_mcs, 0xff,
4238 		       sizeof(arvif->bitrate_mask.control[i].eht_mcs));
4239 	}
4240 
4241 	/* Handle MLO related assignments */
4242 	if (link_id >= 0) {
4243 		rcu_assign_pointer(ahvif->link[arvif->link_id], arvif);
4244 		ahvif->links_map |= BIT(_link_id);
4245 	}
4246 
4247 	ath12k_generic_dbg(ATH12K_DBG_MAC,
4248 			   "mac init link arvif (link_id %d%s) for vif %pM. links_map 0x%x",
4249 			   _link_id, (link_id < 0) ? " deflink" : "", ahvif->vif->addr,
4250 			   ahvif->links_map);
4251 }
4252 
ath12k_mac_remove_link_interface(struct ieee80211_hw * hw,struct ath12k_link_vif * arvif)4253 static void ath12k_mac_remove_link_interface(struct ieee80211_hw *hw,
4254 					     struct ath12k_link_vif *arvif)
4255 {
4256 	struct ath12k_vif *ahvif = arvif->ahvif;
4257 	struct ath12k_hw *ah = hw->priv;
4258 	struct ath12k *ar = arvif->ar;
4259 	int ret;
4260 
4261 	lockdep_assert_wiphy(ah->hw->wiphy);
4262 
4263 	cancel_delayed_work_sync(&arvif->connection_loss_work);
4264 	wiphy_work_cancel(ath12k_ar_to_hw(ar)->wiphy, &arvif->bcn_tx_work);
4265 
4266 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac remove link interface (vdev %d link id %d)",
4267 		   arvif->vdev_id, arvif->link_id);
4268 
4269 	if (test_bit(WMI_TLV_SERVICE_11D_OFFLOAD, ar->ab->wmi_ab.svc_map) &&
4270 	    ahvif->vdev_type == WMI_VDEV_TYPE_STA &&
4271 	    ahvif->vdev_subtype == WMI_VDEV_SUBTYPE_NONE)
4272 		ath12k_mac_11d_scan_stop(ar);
4273 
4274 	if (ahvif->vdev_type == WMI_VDEV_TYPE_AP) {
4275 		ret = ath12k_peer_delete(ar, arvif->vdev_id, arvif->bssid);
4276 		if (ret)
4277 			ath12k_warn(ar->ab, "failed to submit AP self-peer removal on vdev %d link id %d: %d",
4278 				    arvif->vdev_id, arvif->link_id, ret);
4279 
4280 		if (arvif->link_id < IEEE80211_MLD_MAX_NUM_LINKS)
4281 			ath12k_dp_peer_delete(&ah->dp_hw, arvif->bssid, NULL);
4282 	}
4283 	ath12k_mac_vdev_delete(ar, arvif);
4284 }
4285 
ath12k_mac_assign_link_vif(struct ath12k_hw * ah,struct ieee80211_vif * vif,u8 link_id)4286 static struct ath12k_link_vif *ath12k_mac_assign_link_vif(struct ath12k_hw *ah,
4287 							  struct ieee80211_vif *vif,
4288 							  u8 link_id)
4289 {
4290 	struct ath12k_vif *ahvif = ath12k_vif_to_ahvif(vif);
4291 	struct ath12k_link_vif *arvif;
4292 
4293 	lockdep_assert_wiphy(ah->hw->wiphy);
4294 
4295 	arvif = wiphy_dereference(ah->hw->wiphy, ahvif->link[link_id]);
4296 	if (arvif)
4297 		return arvif;
4298 
4299 	/* If this is the first link arvif being created for an ML VIF
4300 	 * use the preallocated deflink memory except for scan arvifs
4301 	 */
4302 	if (!ahvif->links_map && link_id < ATH12K_FIRST_SCAN_LINK) {
4303 		arvif = &ahvif->deflink;
4304 
4305 		if (vif->type == NL80211_IFTYPE_STATION)
4306 			arvif->is_sta_assoc_link = true;
4307 	} else {
4308 		arvif = kzalloc_obj(*arvif);
4309 		if (!arvif)
4310 			return NULL;
4311 	}
4312 
4313 	ath12k_mac_init_arvif(ahvif, arvif, link_id);
4314 
4315 	return arvif;
4316 }
4317 
ath12k_mac_unassign_link_vif(struct ath12k_link_vif * arvif)4318 static void ath12k_mac_unassign_link_vif(struct ath12k_link_vif *arvif)
4319 {
4320 	struct ath12k_vif *ahvif = arvif->ahvif;
4321 	struct ath12k_hw *ah = ahvif->ah;
4322 
4323 	lockdep_assert_wiphy(ah->hw->wiphy);
4324 
4325 	rcu_assign_pointer(ahvif->link[arvif->link_id], NULL);
4326 	synchronize_rcu();
4327 	ahvif->links_map &= ~BIT(arvif->link_id);
4328 
4329 	if (arvif != &ahvif->deflink)
4330 		kfree(arvif);
4331 	else
4332 		memset(arvif, 0, sizeof(*arvif));
4333 }
4334 
4335 int
ath12k_mac_op_change_vif_links(struct ieee80211_hw * hw,struct ieee80211_vif * vif,u16 old_links,u16 new_links,struct ieee80211_bss_conf * ol[IEEE80211_MLD_MAX_NUM_LINKS])4336 ath12k_mac_op_change_vif_links(struct ieee80211_hw *hw,
4337 			       struct ieee80211_vif *vif,
4338 			       u16 old_links, u16 new_links,
4339 			       struct ieee80211_bss_conf *ol[IEEE80211_MLD_MAX_NUM_LINKS])
4340 {
4341 	struct ath12k_vif *ahvif = ath12k_vif_to_ahvif(vif);
4342 	unsigned long to_remove = old_links & ~new_links;
4343 	unsigned long to_add = ~old_links & new_links;
4344 	struct ath12k_hw *ah = ath12k_hw_to_ah(hw);
4345 	struct ath12k_link_vif *arvif;
4346 	u8 link_id;
4347 
4348 	lockdep_assert_wiphy(hw->wiphy);
4349 
4350 	ath12k_generic_dbg(ATH12K_DBG_MAC,
4351 			   "mac vif link changed for MLD %pM old_links 0x%x new_links 0x%x\n",
4352 			   vif->addr, old_links, new_links);
4353 
4354 	for_each_set_bit(link_id, &to_add, IEEE80211_MLD_MAX_NUM_LINKS) {
4355 		arvif = wiphy_dereference(hw->wiphy, ahvif->link[link_id]);
4356 		/* mac80211 wants to add link but driver already has the
4357 		 * link. This should not happen ideally.
4358 		 */
4359 		if (WARN_ON(arvif))
4360 			return -EINVAL;
4361 
4362 		arvif = ath12k_mac_assign_link_vif(ah, vif, link_id);
4363 		if (WARN_ON(!arvif))
4364 			return -EINVAL;
4365 	}
4366 
4367 	for_each_set_bit(link_id, &to_remove, IEEE80211_MLD_MAX_NUM_LINKS) {
4368 		arvif = wiphy_dereference(hw->wiphy, ahvif->link[link_id]);
4369 		if (WARN_ON(!arvif))
4370 			return -EINVAL;
4371 
4372 		if (!arvif->is_created) {
4373 			ath12k_mac_unassign_link_vif(arvif);
4374 			continue;
4375 		}
4376 
4377 		if (WARN_ON(!arvif->ar))
4378 			return -EINVAL;
4379 
4380 		ath12k_mac_remove_link_interface(hw, arvif);
4381 		ath12k_mac_unassign_link_vif(arvif);
4382 	}
4383 
4384 	return 0;
4385 }
4386 EXPORT_SYMBOL(ath12k_mac_op_change_vif_links);
4387 
ath12k_mac_fils_discovery(struct ath12k_link_vif * arvif,struct ieee80211_bss_conf * info)4388 static int ath12k_mac_fils_discovery(struct ath12k_link_vif *arvif,
4389 				     struct ieee80211_bss_conf *info)
4390 {
4391 	struct ieee80211_vif *vif = ath12k_ahvif_to_vif(arvif->ahvif);
4392 	struct ath12k *ar = arvif->ar;
4393 	struct ieee80211_hw *hw = ath12k_ar_to_hw(ar);
4394 	struct sk_buff *tmpl;
4395 	int ret;
4396 	u32 interval;
4397 	bool unsol_bcast_probe_resp_enabled = false;
4398 
4399 	if (info->fils_discovery.max_interval) {
4400 		interval = info->fils_discovery.max_interval;
4401 
4402 		tmpl = ieee80211_get_fils_discovery_tmpl(hw, vif,
4403 							 info->link_id);
4404 		if (tmpl)
4405 			ret = ath12k_wmi_fils_discovery_tmpl(ar, arvif->vdev_id,
4406 							     tmpl);
4407 	} else if (info->unsol_bcast_probe_resp_interval) {
4408 		unsol_bcast_probe_resp_enabled = 1;
4409 		interval = info->unsol_bcast_probe_resp_interval;
4410 
4411 		tmpl = ieee80211_get_unsol_bcast_probe_resp_tmpl(hw, vif,
4412 								 info->link_id);
4413 		if (tmpl)
4414 			ret = ath12k_wmi_probe_resp_tmpl(ar, arvif->vdev_id,
4415 							 tmpl);
4416 	} else { /* Disable */
4417 		return ath12k_wmi_fils_discovery(ar, arvif->vdev_id, 0, false);
4418 	}
4419 
4420 	if (!tmpl) {
4421 		ath12k_warn(ar->ab,
4422 			    "mac vdev %i failed to retrieve %s template\n",
4423 			    arvif->vdev_id, (unsol_bcast_probe_resp_enabled ?
4424 			    "unsolicited broadcast probe response" :
4425 			    "FILS discovery"));
4426 		return -EPERM;
4427 	}
4428 	kfree_skb(tmpl);
4429 
4430 	if (!ret)
4431 		ret = ath12k_wmi_fils_discovery(ar, arvif->vdev_id, interval,
4432 						unsol_bcast_probe_resp_enabled);
4433 
4434 	return ret;
4435 }
4436 
ath12k_mac_op_vif_cfg_changed(struct ieee80211_hw * hw,struct ieee80211_vif * vif,u64 changed)4437 void ath12k_mac_op_vif_cfg_changed(struct ieee80211_hw *hw,
4438 				   struct ieee80211_vif *vif,
4439 				   u64 changed)
4440 {
4441 	struct ath12k_vif *ahvif = ath12k_vif_to_ahvif(vif);
4442 	unsigned long links = ahvif->links_map;
4443 	struct ieee80211_bss_conf *info;
4444 	struct ath12k_link_vif *arvif;
4445 	struct ieee80211_sta *sta;
4446 	struct ath12k_sta *ahsta;
4447 	struct ath12k *ar;
4448 	u8 link_id;
4449 
4450 	lockdep_assert_wiphy(hw->wiphy);
4451 
4452 	if (changed & BSS_CHANGED_SSID && vif->type == NL80211_IFTYPE_AP) {
4453 		ahvif->u.ap.ssid_len = vif->cfg.ssid_len;
4454 		if (vif->cfg.ssid_len)
4455 			memcpy(ahvif->u.ap.ssid, vif->cfg.ssid, vif->cfg.ssid_len);
4456 	}
4457 
4458 	if (changed & BSS_CHANGED_ASSOC) {
4459 		if (vif->cfg.assoc) {
4460 			/* only in station mode we can get here, so it's safe
4461 			 * to use ap_addr
4462 			 */
4463 			rcu_read_lock();
4464 			sta = ieee80211_find_sta(vif, vif->cfg.ap_addr);
4465 			if (!sta) {
4466 				rcu_read_unlock();
4467 				WARN_ONCE(1, "failed to find sta with addr %pM\n",
4468 					  vif->cfg.ap_addr);
4469 				return;
4470 			}
4471 
4472 			ahsta = ath12k_sta_to_ahsta(sta);
4473 			arvif = wiphy_dereference(hw->wiphy,
4474 						  ahvif->link[ahsta->assoc_link_id]);
4475 			rcu_read_unlock();
4476 
4477 			ar = arvif->ar;
4478 			/* there is no reason for which an assoc link's
4479 			 * bss info does not exist
4480 			 */
4481 			info = ath12k_mac_get_link_bss_conf(arvif);
4482 			ath12k_bss_assoc(ar, arvif, info);
4483 
4484 			/* exclude assoc link as it is done above */
4485 			links &= ~BIT(ahsta->assoc_link_id);
4486 		}
4487 
4488 		for_each_set_bit(link_id, &links, IEEE80211_MLD_MAX_NUM_LINKS) {
4489 			arvif = wiphy_dereference(hw->wiphy, ahvif->link[link_id]);
4490 			if (!arvif || !arvif->ar)
4491 				continue;
4492 
4493 			ar = arvif->ar;
4494 
4495 			if (vif->cfg.assoc) {
4496 				info = ath12k_mac_get_link_bss_conf(arvif);
4497 				if (!info)
4498 					continue;
4499 
4500 				ath12k_bss_assoc(ar, arvif, info);
4501 			} else {
4502 				ath12k_bss_disassoc(ar, arvif);
4503 			}
4504 		}
4505 	}
4506 }
4507 EXPORT_SYMBOL(ath12k_mac_op_vif_cfg_changed);
4508 
ath12k_mac_vif_setup_ps(struct ath12k_link_vif * arvif)4509 static void ath12k_mac_vif_setup_ps(struct ath12k_link_vif *arvif)
4510 {
4511 	struct ath12k *ar = arvif->ar;
4512 	struct ieee80211_vif *vif = arvif->ahvif->vif;
4513 	struct ieee80211_conf *conf = &ath12k_ar_to_hw(ar)->conf;
4514 	enum wmi_sta_powersave_param param;
4515 	struct ieee80211_bss_conf *info;
4516 	enum wmi_sta_ps_mode psmode;
4517 	int ret;
4518 	int timeout;
4519 	bool enable_ps;
4520 
4521 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
4522 
4523 	if (vif->type != NL80211_IFTYPE_STATION)
4524 		return;
4525 
4526 	enable_ps = arvif->ahvif->ps;
4527 	if (enable_ps) {
4528 		psmode = WMI_STA_PS_MODE_ENABLED;
4529 		param = WMI_STA_PS_PARAM_INACTIVITY_TIME;
4530 
4531 		timeout = conf->dynamic_ps_timeout;
4532 		if (timeout == 0) {
4533 			info = ath12k_mac_get_link_bss_conf(arvif);
4534 			if (!info) {
4535 				ath12k_warn(ar->ab, "unable to access bss link conf in setup ps for vif %pM link %u\n",
4536 					    vif->addr, arvif->link_id);
4537 				return;
4538 			}
4539 
4540 			/* firmware doesn't like 0 */
4541 			timeout = ieee80211_tu_to_usec(info->beacon_int) / 1000;
4542 		}
4543 
4544 		ret = ath12k_wmi_set_sta_ps_param(ar, arvif->vdev_id, param,
4545 						  timeout);
4546 		if (ret) {
4547 			ath12k_warn(ar->ab, "failed to set inactivity time for vdev %d: %i\n",
4548 				    arvif->vdev_id, ret);
4549 			return;
4550 		}
4551 	} else {
4552 		psmode = WMI_STA_PS_MODE_DISABLED;
4553 	}
4554 
4555 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac vdev %d psmode %s\n",
4556 		   arvif->vdev_id, psmode ? "enable" : "disable");
4557 
4558 	ret = ath12k_wmi_pdev_set_ps_mode(ar, arvif->vdev_id, psmode);
4559 	if (ret)
4560 		ath12k_warn(ar->ab, "failed to set sta power save mode %d for vdev %d: %d\n",
4561 			    psmode, arvif->vdev_id, ret);
4562 }
4563 
ath12k_mac_supports_tpc(struct ath12k * ar,struct ath12k_vif * ahvif,const struct cfg80211_chan_def * chandef)4564 static bool ath12k_mac_supports_tpc(struct ath12k *ar, struct ath12k_vif *ahvif,
4565 				    const struct cfg80211_chan_def *chandef)
4566 {
4567 	return ath12k_wmi_supports_6ghz_cc_ext(ar) &&
4568 		test_bit(WMI_TLV_SERVICE_EXT_TPC_REG_SUPPORT, ar->ab->wmi_ab.svc_map) &&
4569 		(ahvif->vdev_type == WMI_VDEV_TYPE_STA  ||
4570 		 ahvif->vdev_type == WMI_VDEV_TYPE_AP) &&
4571 		ahvif->vdev_subtype == WMI_VDEV_SUBTYPE_NONE &&
4572 		chandef->chan &&
4573 		chandef->chan->band == NL80211_BAND_6GHZ;
4574 }
4575 
ath12k_wmi_vdev_params_up(struct ath12k * ar,struct ath12k_link_vif * arvif,struct ath12k_link_vif * tx_arvif,struct ieee80211_bss_conf * info,u16 aid)4576 static void ath12k_wmi_vdev_params_up(struct ath12k *ar,
4577 				      struct ath12k_link_vif *arvif,
4578 				      struct ath12k_link_vif *tx_arvif,
4579 				      struct ieee80211_bss_conf *info, u16 aid)
4580 {
4581 	struct ath12k_wmi_vdev_up_params params = {
4582 		.vdev_id = arvif->vdev_id,
4583 		.aid = aid,
4584 		.bssid = arvif->bssid
4585 	};
4586 	int ret;
4587 
4588 	if (tx_arvif) {
4589 		params.tx_bssid = tx_arvif->bssid;
4590 		params.nontx_profile_idx = info->bssid_index;
4591 		params.nontx_profile_cnt = 1 << info->bssid_indicator;
4592 	}
4593 
4594 	ret = ath12k_wmi_vdev_up(arvif->ar, &params);
4595 	if (ret)
4596 		ath12k_warn(ar->ab, "failed to bring vdev up %d: %d\n",
4597 			    arvif->vdev_id, ret);
4598 }
4599 
ath12k_mac_config_obss_pd(struct ath12k_link_vif * arvif,const struct ieee80211_he_obss_pd * he_obss_pd)4600 static int ath12k_mac_config_obss_pd(struct ath12k_link_vif *arvif,
4601 				     const struct ieee80211_he_obss_pd *he_obss_pd)
4602 {
4603 	struct ath12k_wmi_obss_pd_arg obss_pd_arg = {};
4604 	u32 srg_bitmap[2], non_srg_bitmap[2];
4605 	struct ath12k *ar = arvif->ar;
4606 	u32 param_id, pdev_id;
4607 	u32 param_val;
4608 	int ret;
4609 
4610 	if (ar->ab->hw_params->single_pdev_only)
4611 		pdev_id = ath12k_mac_get_target_pdev_id_from_vif(arvif);
4612 	else
4613 		pdev_id = ar->pdev->pdev_id;
4614 
4615 	/* Set and enable SRG/non-SRG OBSS PD threshold */
4616 	param_id = WMI_PDEV_PARAM_SET_CMD_OBSS_PD_THRESHOLD;
4617 	if (ar->monitor_started || !he_obss_pd->enable) {
4618 		ret = ath12k_wmi_pdev_set_param(ar, param_id, 0, pdev_id);
4619 		if (ret)
4620 			ath12k_warn(ar->ab,
4621 				    "failed to set OBSS PD threshold for pdev %u: %d\n",
4622 				    pdev_id, ret);
4623 		return ret;
4624 	}
4625 
4626 	/*
4627 	 * This service flag indicates firmware support for SRG/SRP-based
4628 	 * spatial reuse. It also specifies whether OBSS PD threshold values
4629 	 * should be interpreted as dB (offset) or dBm (absolute) units.
4630 	 */
4631 	obss_pd_arg.srp_support = test_bit(WMI_TLV_SERVICE_SRG_SRP_SPATIAL_REUSE_SUPPORT,
4632 					   ar->ab->wmi_ab.svc_map);
4633 
4634 	if (!(he_obss_pd->sr_ctrl &
4635 	      IEEE80211_HE_SPR_NON_SRG_OBSS_PD_SR_DISALLOWED)) {
4636 		if (he_obss_pd->sr_ctrl & IEEE80211_HE_SPR_NON_SRG_OFFSET_PRESENT)
4637 			obss_pd_arg.non_srg_th = ATH12K_OBSS_PD_MAX_THRESHOLD +
4638 						 he_obss_pd->non_srg_max_offset;
4639 		else
4640 			obss_pd_arg.non_srg_th = ATH12K_OBSS_PD_NON_SRG_MAX_THRESHOLD;
4641 
4642 		if (!obss_pd_arg.srp_support)
4643 			obss_pd_arg.non_srg_th -= ATH12K_DEFAULT_NOISE_FLOOR;
4644 
4645 		obss_pd_arg.non_srg_enabled = true;
4646 	}
4647 
4648 	if (he_obss_pd->sr_ctrl & IEEE80211_HE_SPR_SRG_INFORMATION_PRESENT) {
4649 		obss_pd_arg.srg_th = ATH12K_OBSS_PD_MAX_THRESHOLD +
4650 				     he_obss_pd->max_offset;
4651 		obss_pd_arg.srg_enabled = true;
4652 	}
4653 
4654 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
4655 		   "pdev %u OBSS PD sr_ctrl 0x%x srg_th %d dBm non_srg_th %d dBm\n",
4656 		   pdev_id, he_obss_pd->sr_ctrl,
4657 		   obss_pd_arg.srg_th, obss_pd_arg.non_srg_th);
4658 
4659 	param_val = ath12k_wmi_build_obss_pd(&obss_pd_arg);
4660 	ret = ath12k_wmi_pdev_set_param(ar, param_id, param_val, pdev_id);
4661 	if (ret) {
4662 		ath12k_warn(ar->ab,
4663 			    "failed to set OBSS PD threshold for pdev %u: %d\n",
4664 			    pdev_id, ret);
4665 		return ret;
4666 	}
4667 
4668 	/* Enable OBSS PD for all access category */
4669 	param_id  = WMI_PDEV_PARAM_SET_CMD_OBSS_PD_PER_AC;
4670 	param_val = 0xf;
4671 	ret = ath12k_wmi_pdev_set_param(ar, param_id, param_val, pdev_id);
4672 	if (ret) {
4673 		ath12k_warn(ar->ab,
4674 			    "failed to set OBSS PD per ac for pdev %u: %d\n",
4675 			    pdev_id, ret);
4676 		return ret;
4677 	}
4678 
4679 	/* Set SR prohibit */
4680 	param_id  = WMI_PDEV_PARAM_ENABLE_SR_PROHIBIT;
4681 	param_val = !!(he_obss_pd->sr_ctrl &
4682 		       IEEE80211_HE_SPR_HESIGA_SR_VAL15_ALLOWED);
4683 	ret = ath12k_wmi_pdev_set_param(ar, param_id, param_val, pdev_id);
4684 	if (ret) {
4685 		ath12k_warn(ar->ab, "failed to set SR prohibit for pdev %u: %d\n",
4686 			    pdev_id, ret);
4687 		return ret;
4688 	}
4689 
4690 	if (!obss_pd_arg.srp_support)
4691 		return 0;
4692 
4693 	memcpy(srg_bitmap, he_obss_pd->bss_color_bitmap, sizeof(srg_bitmap));
4694 	/* Set SRG BSS color bitmap */
4695 	ret = ath12k_wmi_pdev_set_srg_bss_color_bitmap(ar, pdev_id, srg_bitmap);
4696 	if (ret) {
4697 		ath12k_warn(ar->ab,
4698 			    "failed to set SRG bss color bitmap for pdev %u: %d\n",
4699 			    pdev_id, ret);
4700 		return ret;
4701 	}
4702 
4703 	/* Enable BSS colors for SRG */
4704 	ret = ath12k_wmi_pdev_srg_obss_color_enable_bitmap(ar, pdev_id, srg_bitmap);
4705 	if (ret) {
4706 		ath12k_warn(ar->ab,
4707 			    "failed to enable SRG bss color bitmap pdev %u: %d\n",
4708 			    pdev_id, ret);
4709 		return ret;
4710 	}
4711 
4712 	memcpy(srg_bitmap, he_obss_pd->partial_bssid_bitmap, sizeof(srg_bitmap));
4713 	/* Set SRG partial bssid bitmap */
4714 	ret = ath12k_wmi_pdev_set_srg_partial_bssid_bitmap(ar, pdev_id, srg_bitmap);
4715 	if (ret) {
4716 		ath12k_warn(ar->ab,
4717 			    "failed to set SRG partial bssid bitmap for pdev %u: %d\n",
4718 			    pdev_id, ret);
4719 		return ret;
4720 	}
4721 
4722 	/* Enable partial bssid mask for SRG */
4723 	ret = ath12k_wmi_pdev_srg_obss_bssid_enable_bitmap(ar, pdev_id, srg_bitmap);
4724 	if (ret) {
4725 		ath12k_warn(ar->ab,
4726 			    "failed to enable SRG bssid bitmap pdev %u: %d\n",
4727 			    pdev_id, ret);
4728 		return ret;
4729 	}
4730 
4731 	/*
4732 	 * No explicit non-SRG bitmap from mac80211; enable all colors/bssids
4733 	 * as non-SRG candidates. Actual SRG members are filtered by SRG bitmaps.
4734 	 */
4735 	memset(non_srg_bitmap, 0xff, sizeof(non_srg_bitmap));
4736 
4737 	/* Enable BSS colors for non-SRG */
4738 	ret = ath12k_wmi_pdev_non_srg_obss_color_enable_bitmap(ar, pdev_id,
4739 							       non_srg_bitmap);
4740 	if (ret) {
4741 		ath12k_warn(ar->ab,
4742 			    "failed to enable non SRG color bitmap pdev %u: %d\n",
4743 			    pdev_id, ret);
4744 		return ret;
4745 	}
4746 
4747 	/* Enable partial bssid mask for non-SRG */
4748 	ret = ath12k_wmi_pdev_non_srg_obss_bssid_enable_bitmap(ar, pdev_id,
4749 							       non_srg_bitmap);
4750 	if (ret) {
4751 		ath12k_warn(ar->ab,
4752 			    "failed to enable non SRG bssid bitmap pdev %u: %d\n",
4753 			    pdev_id, ret);
4754 		return ret;
4755 	}
4756 
4757 	return 0;
4758 }
4759 
ath12k_mac_bss_info_changed(struct ath12k * ar,struct ath12k_link_vif * arvif,struct ieee80211_bss_conf * info,u64 changed)4760 static void ath12k_mac_bss_info_changed(struct ath12k *ar,
4761 					struct ath12k_link_vif *arvif,
4762 					struct ieee80211_bss_conf *info,
4763 					u64 changed)
4764 {
4765 	struct ath12k_vif *ahvif = arvif->ahvif;
4766 	struct ieee80211_vif *vif = ath12k_ahvif_to_vif(ahvif);
4767 	struct ieee80211_vif_cfg *vif_cfg = &vif->cfg;
4768 	struct ath12k_link_vif *tx_arvif;
4769 	struct cfg80211_chan_def def;
4770 	u32 param_id, param_value;
4771 	enum nl80211_band band;
4772 	u32 vdev_param;
4773 	int mcast_rate;
4774 	u32 preamble;
4775 	u16 hw_value;
4776 	u16 bitrate;
4777 	u8 rateidx;
4778 	u32 rate;
4779 	int ret;
4780 
4781 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
4782 
4783 	if (changed & BSS_CHANGED_BEACON_INT) {
4784 		arvif->beacon_interval = info->beacon_int;
4785 
4786 		param_id = WMI_VDEV_PARAM_BEACON_INTERVAL;
4787 		ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
4788 						    param_id,
4789 						    arvif->beacon_interval);
4790 		if (ret)
4791 			ath12k_warn(ar->ab, "Failed to set beacon interval for VDEV: %d\n",
4792 				    arvif->vdev_id);
4793 		else
4794 			ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
4795 				   "Beacon interval: %d set for VDEV: %d\n",
4796 				   arvif->beacon_interval, arvif->vdev_id);
4797 	}
4798 
4799 	if (changed & BSS_CHANGED_BEACON) {
4800 		param_id = WMI_PDEV_PARAM_BEACON_TX_MODE;
4801 		param_value = WMI_BEACON_BURST_MODE;
4802 		ret = ath12k_wmi_pdev_set_param(ar, param_id,
4803 						param_value, ar->pdev->pdev_id);
4804 		if (ret)
4805 			ath12k_warn(ar->ab, "Failed to set beacon mode for VDEV: %d\n",
4806 				    arvif->vdev_id);
4807 		else
4808 			ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
4809 				   "Set burst beacon mode for VDEV: %d\n",
4810 				   arvif->vdev_id);
4811 
4812 		/* In MBSSID case, need to install transmitting VIF's template first */
4813 
4814 		ret = ath12k_mac_setup_bcn_tmpl(arvif);
4815 		if (ret)
4816 			ath12k_warn(ar->ab, "failed to update bcn template: %d\n",
4817 				    ret);
4818 
4819 		if (!arvif->is_csa_in_progress)
4820 			goto skip_vdev_up;
4821 
4822 		tx_arvif = ath12k_mac_get_tx_arvif(arvif, info);
4823 		if (tx_arvif && arvif != tx_arvif && tx_arvif->is_csa_in_progress)
4824 			/* skip non tx vif's */
4825 			goto skip_vdev_up;
4826 
4827 		ath12k_wmi_vdev_params_up(ar, arvif, tx_arvif, info, ahvif->aid);
4828 
4829 		arvif->is_csa_in_progress = false;
4830 
4831 		if (tx_arvif && arvif == tx_arvif) {
4832 			struct ath12k_link_vif *arvif_itr;
4833 
4834 			list_for_each_entry(arvif_itr, &ar->arvifs, list) {
4835 				if (!arvif_itr->is_csa_in_progress)
4836 					continue;
4837 
4838 				ath12k_wmi_vdev_params_up(ar, arvif, tx_arvif,
4839 							  info, ahvif->aid);
4840 				arvif_itr->is_csa_in_progress = false;
4841 			}
4842 		}
4843 	}
4844 
4845 skip_vdev_up:
4846 
4847 	if (changed & (BSS_CHANGED_BEACON_INFO | BSS_CHANGED_BEACON)) {
4848 		arvif->dtim_period = info->dtim_period;
4849 
4850 		param_id = WMI_VDEV_PARAM_DTIM_PERIOD;
4851 		ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
4852 						    param_id,
4853 						    arvif->dtim_period);
4854 
4855 		if (ret)
4856 			ath12k_warn(ar->ab, "Failed to set dtim period for VDEV %d: %i\n",
4857 				    arvif->vdev_id, ret);
4858 		else
4859 			ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
4860 				   "DTIM period: %d set for VDEV: %d\n",
4861 				   arvif->dtim_period, arvif->vdev_id);
4862 	}
4863 
4864 	if (changed & BSS_CHANGED_SSID &&
4865 	    vif->type == NL80211_IFTYPE_AP) {
4866 		ahvif->u.ap.ssid_len = vif->cfg.ssid_len;
4867 		if (vif->cfg.ssid_len)
4868 			memcpy(ahvif->u.ap.ssid, vif->cfg.ssid, vif->cfg.ssid_len);
4869 		ahvif->u.ap.hidden_ssid = info->hidden_ssid;
4870 	}
4871 
4872 	if (changed & BSS_CHANGED_BSSID && !is_zero_ether_addr(info->bssid))
4873 		ether_addr_copy(arvif->bssid, info->bssid);
4874 
4875 	if (changed & BSS_CHANGED_BEACON_ENABLED) {
4876 		if (info->enable_beacon) {
4877 			ret = ath12k_mac_set_he_txbf_conf(arvif);
4878 			if (ret)
4879 				ath12k_warn(ar->ab,
4880 					    "failed to set HE TXBF config for vdev: %d\n",
4881 					    arvif->vdev_id);
4882 
4883 			ret = ath12k_mac_set_eht_txbf_conf(arvif);
4884 			if (ret)
4885 				ath12k_warn(ar->ab,
4886 					    "failed to set EHT TXBF config for vdev: %d\n",
4887 					    arvif->vdev_id);
4888 		}
4889 		ath12k_control_beaconing(arvif, info);
4890 
4891 		if (arvif->is_up && info->he_support &&
4892 		    info->he_oper.params) {
4893 			/* TODO: Extend to support 1024 BA Bitmap size */
4894 			ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
4895 							    WMI_VDEV_PARAM_BA_MODE,
4896 							    WMI_BA_MODE_BUFFER_SIZE_256);
4897 			if (ret)
4898 				ath12k_warn(ar->ab,
4899 					    "failed to set BA BUFFER SIZE 256 for vdev: %d\n",
4900 					    arvif->vdev_id);
4901 
4902 			param_id = WMI_VDEV_PARAM_HEOPS_0_31;
4903 			param_value = info->he_oper.params;
4904 			ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
4905 							    param_id, param_value);
4906 			ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
4907 				   "he oper param: %x set for VDEV: %d\n",
4908 				   param_value, arvif->vdev_id);
4909 
4910 			if (ret)
4911 				ath12k_warn(ar->ab, "Failed to set he oper params %x for VDEV %d: %i\n",
4912 					    param_value, arvif->vdev_id, ret);
4913 		}
4914 	}
4915 
4916 	if (changed & BSS_CHANGED_ERP_CTS_PROT) {
4917 		u32 cts_prot;
4918 
4919 		cts_prot = !!(info->use_cts_prot);
4920 		param_id = WMI_VDEV_PARAM_PROTECTION_MODE;
4921 
4922 		if (arvif->is_started) {
4923 			ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
4924 							    param_id, cts_prot);
4925 			if (ret)
4926 				ath12k_warn(ar->ab, "Failed to set CTS prot for VDEV: %d\n",
4927 					    arvif->vdev_id);
4928 			else
4929 				ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "Set CTS prot: %d for VDEV: %d\n",
4930 					   cts_prot, arvif->vdev_id);
4931 		} else {
4932 			ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "defer protection mode setup, vdev is not ready yet\n");
4933 		}
4934 	}
4935 
4936 	if (changed & BSS_CHANGED_ERP_SLOT) {
4937 		u32 slottime;
4938 
4939 		if (info->use_short_slot)
4940 			slottime = WMI_VDEV_SLOT_TIME_SHORT; /* 9us */
4941 
4942 		else
4943 			slottime = WMI_VDEV_SLOT_TIME_LONG; /* 20us */
4944 
4945 		param_id = WMI_VDEV_PARAM_SLOT_TIME;
4946 		ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
4947 						    param_id, slottime);
4948 		if (ret)
4949 			ath12k_warn(ar->ab, "Failed to set erp slot for VDEV: %d\n",
4950 				    arvif->vdev_id);
4951 		else
4952 			ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
4953 				   "Set slottime: %d for VDEV: %d\n",
4954 				   slottime, arvif->vdev_id);
4955 	}
4956 
4957 	if (changed & BSS_CHANGED_ERP_PREAMBLE) {
4958 		u32 preamble;
4959 
4960 		if (info->use_short_preamble)
4961 			preamble = WMI_VDEV_PREAMBLE_SHORT;
4962 		else
4963 			preamble = WMI_VDEV_PREAMBLE_LONG;
4964 
4965 		param_id = WMI_VDEV_PARAM_PREAMBLE;
4966 		ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
4967 						    param_id, preamble);
4968 		if (ret)
4969 			ath12k_warn(ar->ab, "Failed to set preamble for VDEV: %d\n",
4970 				    arvif->vdev_id);
4971 		else
4972 			ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
4973 				   "Set preamble: %d for VDEV: %d\n",
4974 				   preamble, arvif->vdev_id);
4975 	}
4976 
4977 	if (changed & BSS_CHANGED_ASSOC) {
4978 		if (vif->cfg.assoc)
4979 			ath12k_bss_assoc(ar, arvif, info);
4980 		else
4981 			ath12k_bss_disassoc(ar, arvif);
4982 	}
4983 
4984 	if (changed & BSS_CHANGED_TXPOWER) {
4985 		ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac vdev_id %i txpower %d\n",
4986 			   arvif->vdev_id, info->txpower);
4987 
4988 		arvif->txpower = info->txpower;
4989 		ath12k_mac_txpower_recalc(ar);
4990 	}
4991 
4992 	if (changed & BSS_CHANGED_MCAST_RATE &&
4993 	    !ath12k_mac_vif_link_chan(vif, arvif->link_id, &def)) {
4994 		band = def.chan->band;
4995 		mcast_rate = info->mcast_rate[band];
4996 
4997 		if (mcast_rate > 0) {
4998 			rateidx = mcast_rate - 1;
4999 		} else {
5000 			if (info->basic_rates)
5001 				rateidx = __ffs(info->basic_rates);
5002 			else
5003 				rateidx = 0;
5004 		}
5005 
5006 		if (ar->pdev->cap.supported_bands & WMI_HOST_WLAN_5GHZ_CAP)
5007 			rateidx += ATH12K_MAC_FIRST_OFDM_RATE_IDX;
5008 
5009 		bitrate = ath12k_legacy_rates[rateidx].bitrate;
5010 		hw_value = ath12k_legacy_rates[rateidx].hw_value;
5011 
5012 		if (ath12k_mac_bitrate_is_cck(bitrate))
5013 			preamble = WMI_RATE_PREAMBLE_CCK;
5014 		else
5015 			preamble = WMI_RATE_PREAMBLE_OFDM;
5016 
5017 		rate = ATH12K_HW_RATE_CODE(hw_value, 0, preamble);
5018 
5019 		ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
5020 			   "mac vdev %d mcast_rate %x\n",
5021 			   arvif->vdev_id, rate);
5022 
5023 		vdev_param = WMI_VDEV_PARAM_MCAST_DATA_RATE;
5024 		ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
5025 						    vdev_param, rate);
5026 		if (ret)
5027 			ath12k_warn(ar->ab,
5028 				    "failed to set mcast rate on vdev %i: %d\n",
5029 				    arvif->vdev_id,  ret);
5030 
5031 		vdev_param = WMI_VDEV_PARAM_BCAST_DATA_RATE;
5032 		ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
5033 						    vdev_param, rate);
5034 		if (ret)
5035 			ath12k_warn(ar->ab,
5036 				    "failed to set bcast rate on vdev %i: %d\n",
5037 				    arvif->vdev_id,  ret);
5038 	}
5039 
5040 	if (changed & BSS_CHANGED_BASIC_RATES &&
5041 	    !ath12k_mac_vif_link_chan(vif, arvif->link_id, &def))
5042 		ath12k_recalculate_mgmt_rate(ar, arvif, &def);
5043 
5044 	if (changed & BSS_CHANGED_TWT) {
5045 		if (info->twt_requester || info->twt_responder)
5046 			ath12k_wmi_send_twt_enable_cmd(ar, ar->pdev->pdev_id);
5047 		else
5048 			ath12k_wmi_send_twt_disable_cmd(ar, ar->pdev->pdev_id);
5049 	}
5050 
5051 	if (changed & BSS_CHANGED_HE_OBSS_PD) {
5052 		if (vif->type == NL80211_IFTYPE_AP)
5053 			ath12k_mac_config_obss_pd(arvif, &info->he_obss_pd);
5054 		else
5055 			ath12k_wmi_send_obss_spr_cmd(ar, arvif->vdev_id,
5056 						     &info->he_obss_pd);
5057 	}
5058 
5059 	if (changed & BSS_CHANGED_HE_BSS_COLOR) {
5060 		if (vif->type == NL80211_IFTYPE_AP) {
5061 			ret = ath12k_wmi_obss_color_cfg_cmd(ar,
5062 							    arvif->vdev_id,
5063 							    info->he_bss_color.color,
5064 							    ATH12K_BSS_COLOR_AP_PERIODS,
5065 							    info->he_bss_color.enabled);
5066 			if (ret)
5067 				ath12k_warn(ar->ab, "failed to set bss color collision on vdev %u: %d\n",
5068 					    arvif->vdev_id,  ret);
5069 
5070 			param_id = WMI_VDEV_PARAM_BSS_COLOR;
5071 			if (info->he_bss_color.enabled)
5072 				param_value = info->he_bss_color.color <<
5073 					      IEEE80211_HE_OPERATION_BSS_COLOR_OFFSET;
5074 			else
5075 				param_value = IEEE80211_HE_OPERATION_BSS_COLOR_DISABLED;
5076 
5077 			ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
5078 							    param_id,
5079 							    param_value);
5080 			if (ret)
5081 				ath12k_warn(ar->ab, "failed to set bss color param on vdev %u: %d\n",
5082 					    arvif->vdev_id,  ret);
5083 			else
5084 				ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "bss color param 0x%x set on vdev %u\n",
5085 					   param_value, arvif->vdev_id);
5086 		} else if (vif->type == NL80211_IFTYPE_STATION) {
5087 			ret = ath12k_wmi_send_bss_color_change_enable_cmd(ar,
5088 									  arvif->vdev_id,
5089 									  1);
5090 			if (ret)
5091 				ath12k_warn(ar->ab, "failed to enable bss color change on vdev %i: %d\n",
5092 					    arvif->vdev_id,  ret);
5093 			ret = ath12k_wmi_obss_color_cfg_cmd(ar,
5094 							    arvif->vdev_id,
5095 							    0,
5096 							    ATH12K_BSS_COLOR_STA_PERIODS,
5097 							    1);
5098 			if (ret)
5099 				ath12k_warn(ar->ab, "failed to set bss color collision on vdev %i: %d\n",
5100 					    arvif->vdev_id,  ret);
5101 		}
5102 	}
5103 
5104 	ath12k_mac_fils_discovery(arvif, info);
5105 
5106 	if (changed & BSS_CHANGED_PS &&
5107 	    ar->ab->hw_params->supports_sta_ps) {
5108 		ahvif->ps = vif_cfg->ps;
5109 		ath12k_mac_vif_setup_ps(arvif);
5110 	}
5111 }
5112 
ath12k_ahvif_get_link_cache(struct ath12k_vif * ahvif,u8 link_id)5113 static struct ath12k_vif_cache *ath12k_ahvif_get_link_cache(struct ath12k_vif *ahvif,
5114 							    u8 link_id)
5115 {
5116 	if (!ahvif->cache[link_id]) {
5117 		ahvif->cache[link_id] = kzalloc_obj(*ahvif->cache[0]);
5118 		if (ahvif->cache[link_id])
5119 			INIT_LIST_HEAD(&ahvif->cache[link_id]->key_conf.list);
5120 	}
5121 
5122 	return ahvif->cache[link_id];
5123 }
5124 
ath12k_ahvif_put_link_key_cache(struct ath12k_vif_cache * cache)5125 static void ath12k_ahvif_put_link_key_cache(struct ath12k_vif_cache *cache)
5126 {
5127 	struct ath12k_key_conf *key_conf, *tmp;
5128 
5129 	if (!cache || list_empty(&cache->key_conf.list))
5130 		return;
5131 	list_for_each_entry_safe(key_conf, tmp, &cache->key_conf.list, list) {
5132 		list_del(&key_conf->list);
5133 		kfree(key_conf);
5134 	}
5135 }
5136 
ath12k_ahvif_put_link_cache(struct ath12k_vif * ahvif,u8 link_id)5137 static void ath12k_ahvif_put_link_cache(struct ath12k_vif *ahvif, u8 link_id)
5138 {
5139 	if (link_id >= IEEE80211_MLD_MAX_NUM_LINKS)
5140 		return;
5141 
5142 	ath12k_ahvif_put_link_key_cache(ahvif->cache[link_id]);
5143 	kfree(ahvif->cache[link_id]);
5144 	ahvif->cache[link_id] = NULL;
5145 }
5146 
ath12k_mac_op_link_info_changed(struct ieee80211_hw * hw,struct ieee80211_vif * vif,struct ieee80211_bss_conf * info,u64 changed)5147 void ath12k_mac_op_link_info_changed(struct ieee80211_hw *hw,
5148 				     struct ieee80211_vif *vif,
5149 				     struct ieee80211_bss_conf *info,
5150 				     u64 changed)
5151 {
5152 	struct ath12k *ar;
5153 	struct ath12k_vif *ahvif = ath12k_vif_to_ahvif(vif);
5154 	struct ath12k_vif_cache *cache;
5155 	struct ath12k_link_vif *arvif;
5156 	u8 link_id = info->link_id;
5157 
5158 	lockdep_assert_wiphy(hw->wiphy);
5159 
5160 	arvif = wiphy_dereference(hw->wiphy, ahvif->link[link_id]);
5161 
5162 	/* if the vdev is not created on a certain radio,
5163 	 * cache the info to be updated later on vdev creation
5164 	 */
5165 
5166 	if (!arvif || !arvif->is_created) {
5167 		cache = ath12k_ahvif_get_link_cache(ahvif, link_id);
5168 		if (!cache)
5169 			return;
5170 
5171 		cache->bss_conf_changed |= changed;
5172 
5173 		return;
5174 	}
5175 
5176 	ar = arvif->ar;
5177 
5178 	ath12k_mac_bss_info_changed(ar, arvif, info, changed);
5179 }
5180 EXPORT_SYMBOL(ath12k_mac_op_link_info_changed);
5181 
5182 static struct ath12k*
ath12k_mac_select_scan_device(struct ieee80211_hw * hw,struct ieee80211_vif * vif,u32 center_freq)5183 ath12k_mac_select_scan_device(struct ieee80211_hw *hw,
5184 			      struct ieee80211_vif *vif,
5185 			      u32 center_freq)
5186 {
5187 	struct ath12k_hw *ah = hw->priv;
5188 	enum nl80211_band band;
5189 	struct ath12k *ar;
5190 	int i;
5191 
5192 	if (ah->num_radio == 1)
5193 		return ah->radio;
5194 
5195 	/* Currently mac80211 supports splitting scan requests into
5196 	 * multiple scan requests per band.
5197 	 * Loop through first channel and determine the scan radio
5198 	 * TODO: There could be 5 GHz low/high channels in that case
5199 	 * split the hw request and perform multiple scans
5200 	 */
5201 
5202 	if (center_freq < ATH12K_MIN_5GHZ_FREQ)
5203 		band = NL80211_BAND_2GHZ;
5204 	else if (center_freq < ATH12K_MIN_6GHZ_FREQ)
5205 		band = NL80211_BAND_5GHZ;
5206 	else
5207 		band = NL80211_BAND_6GHZ;
5208 
5209 	for_each_ar(ah, ar, i) {
5210 		if (ar->mac.sbands[band].channels &&
5211 		    center_freq >= KHZ_TO_MHZ(ar->freq_range.start_freq) &&
5212 		    center_freq <= KHZ_TO_MHZ(ar->freq_range.end_freq))
5213 			return ar;
5214 	}
5215 
5216 	return NULL;
5217 }
5218 
__ath12k_mac_scan_finish(struct ath12k * ar)5219 void __ath12k_mac_scan_finish(struct ath12k *ar)
5220 {
5221 	struct ieee80211_hw *hw = ath12k_ar_to_hw(ar);
5222 
5223 	lockdep_assert_held(&ar->data_lock);
5224 
5225 	switch (ar->scan.state) {
5226 	case ATH12K_SCAN_IDLE:
5227 		break;
5228 	case ATH12K_SCAN_RUNNING:
5229 	case ATH12K_SCAN_ABORTING:
5230 		if (ar->scan.is_roc && ar->scan.roc_notify)
5231 			ieee80211_remain_on_channel_expired(hw);
5232 		fallthrough;
5233 	case ATH12K_SCAN_STARTING:
5234 		cancel_delayed_work(&ar->scan.timeout);
5235 		complete_all(&ar->scan.completed);
5236 		wiphy_work_queue(ar->ah->hw->wiphy, &ar->scan.vdev_clean_wk);
5237 		break;
5238 	}
5239 }
5240 
ath12k_mac_scan_finish(struct ath12k * ar)5241 void ath12k_mac_scan_finish(struct ath12k *ar)
5242 {
5243 	spin_lock_bh(&ar->data_lock);
5244 	__ath12k_mac_scan_finish(ar);
5245 	spin_unlock_bh(&ar->data_lock);
5246 }
5247 
ath12k_scan_stop(struct ath12k * ar)5248 static int ath12k_scan_stop(struct ath12k *ar)
5249 {
5250 	struct ath12k_wmi_scan_cancel_arg arg = {
5251 		.req_type = WLAN_SCAN_CANCEL_SINGLE,
5252 		.scan_id = ATH12K_SCAN_ID,
5253 	};
5254 	int ret;
5255 
5256 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
5257 
5258 	/* TODO: Fill other STOP Params */
5259 	arg.pdev_id = ar->pdev->pdev_id;
5260 
5261 	ret = ath12k_wmi_send_scan_stop_cmd(ar, &arg);
5262 	if (ret) {
5263 		ath12k_warn(ar->ab, "failed to stop wmi scan: %d\n", ret);
5264 		goto out;
5265 	}
5266 
5267 	ret = wait_for_completion_timeout(&ar->scan.completed, 3 * HZ);
5268 	if (ret == 0) {
5269 		ath12k_warn(ar->ab,
5270 			    "failed to receive scan abort comple: timed out\n");
5271 		ret = -ETIMEDOUT;
5272 	} else if (ret > 0) {
5273 		ret = 0;
5274 	}
5275 
5276 out:
5277 	/* Scan state should be updated in scan completion worker but in
5278 	 * case firmware fails to deliver the event (for whatever reason)
5279 	 * it is desired to clean up scan state anyway. Firmware may have
5280 	 * just dropped the scan completion event delivery due to transport
5281 	 * pipe being overflown with data and/or it can recover on its own
5282 	 * before next scan request is submitted.
5283 	 */
5284 	spin_lock_bh(&ar->data_lock);
5285 	if (ret)
5286 		__ath12k_mac_scan_finish(ar);
5287 	spin_unlock_bh(&ar->data_lock);
5288 
5289 	return ret;
5290 }
5291 
ath12k_scan_abort(struct ath12k * ar)5292 static void ath12k_scan_abort(struct ath12k *ar)
5293 {
5294 	int ret;
5295 
5296 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
5297 
5298 	spin_lock_bh(&ar->data_lock);
5299 
5300 	switch (ar->scan.state) {
5301 	case ATH12K_SCAN_IDLE:
5302 		/* This can happen if timeout worker kicked in and called
5303 		 * abortion while scan completion was being processed.
5304 		 */
5305 		break;
5306 	case ATH12K_SCAN_STARTING:
5307 	case ATH12K_SCAN_ABORTING:
5308 		ath12k_warn(ar->ab, "refusing scan abortion due to invalid scan state: %d\n",
5309 			    ar->scan.state);
5310 		break;
5311 	case ATH12K_SCAN_RUNNING:
5312 		ar->scan.state = ATH12K_SCAN_ABORTING;
5313 		spin_unlock_bh(&ar->data_lock);
5314 
5315 		ret = ath12k_scan_stop(ar);
5316 		if (ret)
5317 			ath12k_warn(ar->ab, "failed to abort scan: %d\n", ret);
5318 
5319 		spin_lock_bh(&ar->data_lock);
5320 		break;
5321 	}
5322 
5323 	spin_unlock_bh(&ar->data_lock);
5324 }
5325 
ath12k_scan_timeout_work(struct work_struct * work)5326 static void ath12k_scan_timeout_work(struct work_struct *work)
5327 {
5328 	struct ath12k *ar = container_of(work, struct ath12k,
5329 					 scan.timeout.work);
5330 
5331 	wiphy_lock(ath12k_ar_to_hw(ar)->wiphy);
5332 	ath12k_scan_abort(ar);
5333 	wiphy_unlock(ath12k_ar_to_hw(ar)->wiphy);
5334 }
5335 
ath12k_mac_scan_send_complete(struct ath12k * ar,struct cfg80211_scan_info * info)5336 static void ath12k_mac_scan_send_complete(struct ath12k *ar,
5337 					  struct cfg80211_scan_info *info)
5338 {
5339 	struct ath12k_hw *ah = ar->ah;
5340 	struct ath12k *partner_ar;
5341 	int i;
5342 
5343 	lockdep_assert_wiphy(ah->hw->wiphy);
5344 
5345 	for_each_ar(ah, partner_ar, i)
5346 		if (partner_ar != ar &&
5347 		    partner_ar->scan.state == ATH12K_SCAN_RUNNING)
5348 			return;
5349 
5350 	ieee80211_scan_completed(ah->hw, info);
5351 }
5352 
ath12k_scan_vdev_clean_work(struct wiphy * wiphy,struct wiphy_work * work)5353 static void ath12k_scan_vdev_clean_work(struct wiphy *wiphy, struct wiphy_work *work)
5354 {
5355 	struct ath12k *ar = container_of(work, struct ath12k,
5356 					 scan.vdev_clean_wk);
5357 	struct ath12k_hw *ah = ar->ah;
5358 	struct ath12k_link_vif *arvif;
5359 
5360 	lockdep_assert_wiphy(wiphy);
5361 
5362 	arvif = ar->scan.arvif;
5363 
5364 	/* The scan vdev has already been deleted. This can occur when a
5365 	 * new scan request is made on the same vif with a different
5366 	 * frequency, causing the scan arvif to move from one radio to
5367 	 * another. Or, scan was abrupted and via remove interface, the
5368 	 * arvif is already deleted. Alternatively, if the scan vdev is not
5369 	 * being used as an actual vdev, then do not delete it.
5370 	 */
5371 	if (!arvif || arvif->is_started)
5372 		goto work_complete;
5373 
5374 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac clean scan vdev (link id %u)",
5375 		   arvif->link_id);
5376 
5377 	ath12k_mac_remove_link_interface(ah->hw, arvif);
5378 	ath12k_mac_unassign_link_vif(arvif);
5379 
5380 work_complete:
5381 	spin_lock_bh(&ar->data_lock);
5382 	ar->scan.arvif = NULL;
5383 	if (!ar->scan.is_roc) {
5384 		struct cfg80211_scan_info info = {
5385 			.aborted = ((ar->scan.state ==
5386 				    ATH12K_SCAN_ABORTING) ||
5387 				    (ar->scan.state ==
5388 				    ATH12K_SCAN_STARTING)),
5389 		};
5390 
5391 		ath12k_mac_scan_send_complete(ar, &info);
5392 	}
5393 
5394 	ar->scan.state = ATH12K_SCAN_IDLE;
5395 	ar->scan_channel = NULL;
5396 	ar->scan.roc_freq = 0;
5397 	spin_unlock_bh(&ar->data_lock);
5398 }
5399 
ath12k_start_scan(struct ath12k * ar,struct ath12k_wmi_scan_req_arg * arg)5400 static int ath12k_start_scan(struct ath12k *ar,
5401 			     struct ath12k_wmi_scan_req_arg *arg)
5402 {
5403 	int ret;
5404 
5405 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
5406 
5407 	ret = ath12k_wmi_send_scan_start_cmd(ar, arg);
5408 	if (ret)
5409 		return ret;
5410 
5411 	ret = wait_for_completion_timeout(&ar->scan.started, 1 * HZ);
5412 	if (ret == 0) {
5413 		ret = ath12k_scan_stop(ar);
5414 		if (ret)
5415 			ath12k_warn(ar->ab, "failed to stop scan: %d\n", ret);
5416 
5417 		return -ETIMEDOUT;
5418 	}
5419 
5420 	/* If we failed to start the scan, return error code at
5421 	 * this point.  This is probably due to some issue in the
5422 	 * firmware, but no need to wedge the driver due to that...
5423 	 */
5424 	spin_lock_bh(&ar->data_lock);
5425 	if (ar->scan.state == ATH12K_SCAN_IDLE) {
5426 		spin_unlock_bh(&ar->data_lock);
5427 		return -EINVAL;
5428 	}
5429 	spin_unlock_bh(&ar->data_lock);
5430 
5431 	return 0;
5432 }
5433 
ath12k_mac_get_fw_stats(struct ath12k * ar,struct ath12k_fw_stats_req_params * param)5434 int ath12k_mac_get_fw_stats(struct ath12k *ar,
5435 			    struct ath12k_fw_stats_req_params *param)
5436 {
5437 	struct ath12k_base *ab = ar->ab;
5438 	struct ath12k_hw *ah = ath12k_ar_to_ah(ar);
5439 	unsigned long time_left;
5440 	int ret;
5441 
5442 	guard(mutex)(&ah->hw_mutex);
5443 
5444 	if (ah->state != ATH12K_HW_STATE_ON)
5445 		return -ENETDOWN;
5446 
5447 	reinit_completion(&ar->fw_stats_complete);
5448 	reinit_completion(&ar->fw_stats_done);
5449 
5450 	ret = ath12k_wmi_send_stats_request_cmd(ar, param->stats_id,
5451 						param->vdev_id, param->pdev_id);
5452 	if (ret) {
5453 		ath12k_warn(ab, "failed to request fw stats: %d\n", ret);
5454 		return ret;
5455 	}
5456 
5457 	ath12k_dbg(ab, ATH12K_DBG_WMI,
5458 		   "get fw stat pdev id %d vdev id %d stats id 0x%x\n",
5459 		   param->pdev_id, param->vdev_id, param->stats_id);
5460 
5461 	time_left = wait_for_completion_timeout(&ar->fw_stats_complete, 1 * HZ);
5462 	if (!time_left) {
5463 		ath12k_warn(ab, "time out while waiting for get fw stats\n");
5464 		return -ETIMEDOUT;
5465 	}
5466 
5467 	/* Firmware sends WMI_UPDATE_STATS_EVENTID back-to-back
5468 	 * when stats data buffer limit is reached. fw_stats_complete
5469 	 * is completed once host receives first event from firmware, but
5470 	 * still there could be more events following. Below is to wait
5471 	 * until firmware completes sending all the events.
5472 	 */
5473 	time_left = wait_for_completion_timeout(&ar->fw_stats_done, 3 * HZ);
5474 	if (!time_left) {
5475 		ath12k_warn(ab, "time out while waiting for fw stats done\n");
5476 		return -ETIMEDOUT;
5477 	}
5478 
5479 	return 0;
5480 }
5481 
ath12k_mac_op_get_txpower(struct ieee80211_hw * hw,struct ieee80211_vif * vif,unsigned int link_id,int * dbm)5482 int ath12k_mac_op_get_txpower(struct ieee80211_hw *hw,
5483 			      struct ieee80211_vif *vif,
5484 			      unsigned int link_id,
5485 			      int *dbm)
5486 {
5487 	struct ath12k_vif *ahvif = ath12k_vif_to_ahvif(vif);
5488 	struct ath12k_fw_stats_req_params params = {};
5489 	struct ath12k_fw_stats_pdev *pdev;
5490 	struct ath12k_hw *ah = hw->priv;
5491 	struct ath12k_link_vif *arvif;
5492 	struct ath12k_base *ab;
5493 	struct ath12k *ar;
5494 	int ret;
5495 
5496 	/* Final Tx power is minimum of Target Power, CTL power, Regulatory
5497 	 * Power, PSD EIRP Power. We just know the Regulatory power from the
5498 	 * regulatory rules obtained. FW knows all these power and sets the min
5499 	 * of these. Hence, we request the FW pdev stats in which FW reports
5500 	 * the minimum of all vdev's channel Tx power.
5501 	 */
5502 	lockdep_assert_wiphy(hw->wiphy);
5503 
5504 	arvif = wiphy_dereference(ah->hw->wiphy, ahvif->link[link_id]);
5505 	if (!arvif || !arvif->ar)
5506 		return -EINVAL;
5507 
5508 	ar = arvif->ar;
5509 	ab = ar->ab;
5510 	if (ah->state != ATH12K_HW_STATE_ON)
5511 		goto err_fallback;
5512 
5513 	if (test_bit(ATH12K_FLAG_CAC_RUNNING, &ar->dev_flags))
5514 		return -EAGAIN;
5515 
5516 	/* Limit the requests to Firmware for fetching the tx power */
5517 	if (ar->chan_tx_pwr != ATH12K_PDEV_TX_POWER_INVALID &&
5518 	    time_before(jiffies,
5519 			msecs_to_jiffies(ATH12K_PDEV_TX_POWER_REFRESH_TIME_MSECS) +
5520 					 ar->last_tx_power_update))
5521 		goto send_tx_power;
5522 
5523 	params.pdev_id = ath12k_mac_get_target_pdev_id(ar);
5524 	params.vdev_id = arvif->vdev_id;
5525 	params.stats_id = WMI_REQUEST_PDEV_STAT;
5526 	ret = ath12k_mac_get_fw_stats(ar, &params);
5527 	if (ret) {
5528 		ath12k_warn(ab, "failed to request fw pdev stats: %d\n", ret);
5529 		goto err_fallback;
5530 	}
5531 
5532 	spin_lock_bh(&ar->data_lock);
5533 	pdev = list_first_entry_or_null(&ar->fw_stats.pdevs,
5534 					struct ath12k_fw_stats_pdev, list);
5535 	if (!pdev) {
5536 		spin_unlock_bh(&ar->data_lock);
5537 		goto err_fallback;
5538 	}
5539 
5540 	/* tx power reported by firmware is in units of 0.5 dBm */
5541 	ar->chan_tx_pwr = pdev->chan_tx_power / 2;
5542 	spin_unlock_bh(&ar->data_lock);
5543 	ar->last_tx_power_update = jiffies;
5544 	ath12k_fw_stats_reset(ar);
5545 
5546 send_tx_power:
5547 	*dbm = ar->chan_tx_pwr;
5548 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "txpower fetched from firmware %d dBm\n",
5549 		   *dbm);
5550 	return 0;
5551 
5552 err_fallback:
5553 	/* We didn't get txpower from FW. Hence, relying on vif->bss_conf.txpower */
5554 	*dbm = vif->bss_conf.txpower;
5555 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "txpower from firmware NaN, reported %d dBm\n",
5556 		   *dbm);
5557 	return 0;
5558 }
5559 EXPORT_SYMBOL(ath12k_mac_op_get_txpower);
5560 
5561 static u8
ath12k_mac_find_link_id_by_ar(struct ath12k_vif * ahvif,struct ath12k * ar)5562 ath12k_mac_find_link_id_by_ar(struct ath12k_vif *ahvif, struct ath12k *ar)
5563 {
5564 	struct ath12k_link_vif *arvif;
5565 	struct ath12k_hw *ah = ahvif->ah;
5566 	unsigned long links = ahvif->links_map;
5567 	unsigned long scan_links_map;
5568 	u8 link_id;
5569 
5570 	lockdep_assert_wiphy(ah->hw->wiphy);
5571 
5572 	for_each_set_bit(link_id, &links, ATH12K_NUM_MAX_LINKS) {
5573 		arvif = wiphy_dereference(ah->hw->wiphy, ahvif->link[link_id]);
5574 
5575 		if (!arvif || !arvif->is_created)
5576 			continue;
5577 
5578 		if (ar == arvif->ar)
5579 			return link_id;
5580 	}
5581 
5582 	/* input ar is not assigned to any of the links of ML VIF, use next
5583 	 * available scan link for scan vdev creation. There are cases where
5584 	 * single scan req needs to be split in driver and initiate separate
5585 	 * scan requests to firmware based on device.
5586 	 */
5587 
5588 	 /* Unset all non-scan links (0-14) of scan_links_map so that ffs() will
5589 	  * choose an available link among scan links (i.e link id >= 15)
5590 	  */
5591 	scan_links_map = ~ahvif->links_map & ATH12K_SCAN_LINKS_MASK;
5592 	if (scan_links_map)
5593 		return __ffs(scan_links_map);
5594 
5595 	return ATH12K_FIRST_SCAN_LINK;
5596 }
5597 
ath12k_mac_initiate_hw_scan(struct ieee80211_hw * hw,struct ieee80211_vif * vif,struct ieee80211_scan_request * hw_req,int n_channels,struct ieee80211_channel ** chan_list,struct ath12k * ar)5598 static int ath12k_mac_initiate_hw_scan(struct ieee80211_hw *hw,
5599 				       struct ieee80211_vif *vif,
5600 				       struct ieee80211_scan_request *hw_req,
5601 				       int n_channels,
5602 				       struct ieee80211_channel **chan_list,
5603 				       struct ath12k *ar)
5604 {
5605 	struct ath12k_hw *ah = ath12k_hw_to_ah(hw);
5606 	struct ath12k_vif *ahvif = ath12k_vif_to_ahvif(vif);
5607 	struct ath12k_link_vif *arvif;
5608 	struct cfg80211_scan_request *req = &hw_req->req;
5609 	struct ath12k_wmi_scan_req_arg *arg = NULL;
5610 	u8 link_id;
5611 	int ret;
5612 	int i;
5613 	bool create = true;
5614 
5615 	lockdep_assert_wiphy(hw->wiphy);
5616 
5617 	arvif = &ahvif->deflink;
5618 
5619 	/* check if any of the links of ML VIF is already started on
5620 	 * radio(ar) corresponding to given scan frequency and use it,
5621 	 * if not use scan link (link id >= 15) for scan purpose.
5622 	 */
5623 	link_id = ath12k_mac_find_link_id_by_ar(ahvif, ar);
5624 	/* All scan links are occupied. ideally this shouldn't happen as
5625 	 * mac80211 won't schedule scan for same band until ongoing scan is
5626 	 * completed, don't try to exceed max links just in case if it happens.
5627 	 */
5628 	if (link_id >= ATH12K_NUM_MAX_LINKS)
5629 		return -EBUSY;
5630 
5631 	arvif = ath12k_mac_assign_link_vif(ah, vif, link_id);
5632 
5633 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac link ID %d selected for scan",
5634 		   arvif->link_id);
5635 
5636 	/* If the vif is already assigned to a specific vdev of an ar,
5637 	 * check whether its already started, vdev which is started
5638 	 * are not allowed to switch to a new radio.
5639 	 * If the vdev is not started, but was earlier created on a
5640 	 * different ar, delete that vdev and create a new one. We don't
5641 	 * delete at the scan stop as an optimization to avoid redundant
5642 	 * delete-create vdev's for the same ar, in case the request is
5643 	 * always on the same band for the vif
5644 	 */
5645 	if (arvif->is_created) {
5646 		if (WARN_ON(!arvif->ar))
5647 			return -EINVAL;
5648 
5649 		if (ar != arvif->ar && arvif->is_started)
5650 			return -EINVAL;
5651 
5652 		if (ar != arvif->ar) {
5653 			ath12k_mac_remove_link_interface(hw, arvif);
5654 			ath12k_mac_unassign_link_vif(arvif);
5655 		} else {
5656 			create = false;
5657 		}
5658 	}
5659 
5660 	if (create) {
5661 		/* Previous arvif would've been cleared in radio switch block
5662 		 * above, assign arvif again for create.
5663 		 */
5664 		arvif = ath12k_mac_assign_link_vif(ah, vif, link_id);
5665 
5666 		ret = ath12k_mac_vdev_create(ar, arvif);
5667 		if (ret) {
5668 			ath12k_warn(ar->ab, "unable to create scan vdev %d\n", ret);
5669 			ath12k_mac_unassign_link_vif(arvif);
5670 			return ret;
5671 		}
5672 	}
5673 
5674 	spin_lock_bh(&ar->data_lock);
5675 	switch (ar->scan.state) {
5676 	case ATH12K_SCAN_IDLE:
5677 		reinit_completion(&ar->scan.started);
5678 		reinit_completion(&ar->scan.completed);
5679 		ar->scan.state = ATH12K_SCAN_STARTING;
5680 		ar->scan.is_roc = false;
5681 		ar->scan.arvif = arvif;
5682 		ret = 0;
5683 		break;
5684 	case ATH12K_SCAN_STARTING:
5685 	case ATH12K_SCAN_RUNNING:
5686 	case ATH12K_SCAN_ABORTING:
5687 		ret = -EBUSY;
5688 		break;
5689 	}
5690 	spin_unlock_bh(&ar->data_lock);
5691 
5692 	if (ret)
5693 		goto exit;
5694 
5695 	arg = kzalloc_flex(*arg, chan_list, n_channels);
5696 	if (!arg) {
5697 		ret = -ENOMEM;
5698 		goto exit;
5699 	}
5700 
5701 	arg->num_chan = n_channels;
5702 
5703 	ath12k_wmi_start_scan_init(ar, arg);
5704 	arg->vdev_id = arvif->vdev_id;
5705 	arg->scan_id = ATH12K_SCAN_ID;
5706 
5707 	if (req->ie_len) {
5708 		arg->extraie.ptr = kmemdup(req->ie, req->ie_len, GFP_KERNEL);
5709 		if (!arg->extraie.ptr) {
5710 			ret = -ENOMEM;
5711 			goto exit;
5712 		}
5713 		arg->extraie.len = req->ie_len;
5714 	}
5715 
5716 	if (req->n_ssids) {
5717 		arg->num_ssids = req->n_ssids;
5718 		for (i = 0; i < arg->num_ssids; i++)
5719 			arg->ssid[i] = req->ssids[i];
5720 	} else {
5721 		arg->scan_f_passive = 1;
5722 	}
5723 
5724 	for (i = 0; i < arg->num_chan; i++)
5725 		arg->chan_list[i] = chan_list[i]->center_freq;
5726 
5727 	ret = ath12k_start_scan(ar, arg);
5728 	if (ret) {
5729 		if (ret == -EBUSY)
5730 			ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
5731 				   "scan engine is busy 11d state %d\n", ar->state_11d);
5732 		else
5733 			ath12k_warn(ar->ab, "failed to start hw scan: %d\n", ret);
5734 
5735 		spin_lock_bh(&ar->data_lock);
5736 		ar->scan.state = ATH12K_SCAN_IDLE;
5737 		spin_unlock_bh(&ar->data_lock);
5738 		goto exit;
5739 	}
5740 
5741 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac scan started");
5742 
5743 	/* Add a margin to account for event/command processing */
5744 	ieee80211_queue_delayed_work(ath12k_ar_to_hw(ar), &ar->scan.timeout,
5745 				     msecs_to_jiffies(arg->max_scan_time +
5746 						      ATH12K_MAC_SCAN_TIMEOUT_MSECS));
5747 
5748 exit:
5749 	if (arg) {
5750 		kfree(arg->extraie.ptr);
5751 		kfree(arg);
5752 	}
5753 
5754 	if (ar->state_11d == ATH12K_11D_PREPARING &&
5755 	    ahvif->vdev_type == WMI_VDEV_TYPE_STA &&
5756 	    ahvif->vdev_subtype == WMI_VDEV_SUBTYPE_NONE)
5757 		ath12k_mac_11d_scan_start(ar, arvif->vdev_id);
5758 
5759 	return ret;
5760 }
5761 
ath12k_mac_op_hw_scan(struct ieee80211_hw * hw,struct ieee80211_vif * vif,struct ieee80211_scan_request * hw_req)5762 int ath12k_mac_op_hw_scan(struct ieee80211_hw *hw,
5763 			  struct ieee80211_vif *vif,
5764 			  struct ieee80211_scan_request *hw_req)
5765 {
5766 	struct ath12k_vif *ahvif = ath12k_vif_to_ahvif(vif);
5767 	struct ieee80211_channel **chan_list, *chan;
5768 	struct ath12k_hw *ah = ath12k_hw_to_ah(hw);
5769 	unsigned long links_map, link_id;
5770 	struct ath12k_link_vif *arvif;
5771 	struct ath12k *ar, *scan_ar;
5772 	int i, j, ret = 0;
5773 
5774 	lockdep_assert_wiphy(hw->wiphy);
5775 
5776 	chan_list = kzalloc_objs(*chan_list, hw_req->req.n_channels);
5777 	if (!chan_list)
5778 		return -ENOMEM;
5779 
5780 	/* There could be channels that belong to multiple underlying radio
5781 	 * in same scan request as mac80211 sees it as single band. In that
5782 	 * case split the hw_req based on frequency range and schedule scans to
5783 	 * corresponding radio.
5784 	 */
5785 	for_each_ar(ah, ar, i) {
5786 		int n_chans = 0;
5787 
5788 		for (j = 0; j < hw_req->req.n_channels; j++) {
5789 			chan = hw_req->req.channels[j];
5790 			scan_ar = ath12k_mac_select_scan_device(hw, vif,
5791 								chan->center_freq);
5792 			if (!scan_ar) {
5793 				ath12k_hw_warn(ah, "unable to select scan device for freq %d\n",
5794 					       chan->center_freq);
5795 				ret = -EINVAL;
5796 				goto abort;
5797 			}
5798 			if (ar != scan_ar)
5799 				continue;
5800 
5801 			chan_list[n_chans++] = chan;
5802 		}
5803 		if (n_chans) {
5804 			ret = ath12k_mac_initiate_hw_scan(hw, vif, hw_req, n_chans,
5805 							  chan_list, ar);
5806 			if (ret)
5807 				goto abort;
5808 		}
5809 	}
5810 abort:
5811 	/* If any of the parallel scans initiated fails, abort all and
5812 	 * remove the scan interfaces created. Return complete scan
5813 	 * failure as mac80211 assumes this as single scan request.
5814 	 */
5815 	if (ret) {
5816 		ath12k_hw_warn(ah, "Scan failed %d , cleanup all scan vdevs\n", ret);
5817 		links_map = ahvif->links_map;
5818 		for_each_set_bit(link_id, &links_map, ATH12K_NUM_MAX_LINKS) {
5819 			arvif = wiphy_dereference(hw->wiphy, ahvif->link[link_id]);
5820 			if (!arvif)
5821 				continue;
5822 
5823 			ar = arvif->ar;
5824 			if (ar->scan.arvif == arvif) {
5825 				wiphy_work_cancel(hw->wiphy, &ar->scan.vdev_clean_wk);
5826 				spin_lock_bh(&ar->data_lock);
5827 				ar->scan.arvif = NULL;
5828 				ar->scan.state = ATH12K_SCAN_IDLE;
5829 				ar->scan_channel = NULL;
5830 				ar->scan.roc_freq = 0;
5831 				spin_unlock_bh(&ar->data_lock);
5832 			}
5833 			if (link_id >= ATH12K_FIRST_SCAN_LINK) {
5834 				ath12k_mac_remove_link_interface(hw, arvif);
5835 				ath12k_mac_unassign_link_vif(arvif);
5836 			}
5837 		}
5838 	}
5839 	kfree(chan_list);
5840 	return ret;
5841 }
5842 EXPORT_SYMBOL(ath12k_mac_op_hw_scan);
5843 
ath12k_mac_op_cancel_hw_scan(struct ieee80211_hw * hw,struct ieee80211_vif * vif)5844 void ath12k_mac_op_cancel_hw_scan(struct ieee80211_hw *hw,
5845 				  struct ieee80211_vif *vif)
5846 {
5847 	struct ath12k_vif *ahvif = ath12k_vif_to_ahvif(vif);
5848 	unsigned long link_id, links_map = ahvif->links_map;
5849 	struct ath12k_link_vif *arvif;
5850 	struct ath12k *ar;
5851 
5852 	lockdep_assert_wiphy(hw->wiphy);
5853 
5854 	for_each_set_bit(link_id, &links_map, ATH12K_NUM_MAX_LINKS) {
5855 		arvif = wiphy_dereference(hw->wiphy, ahvif->link[link_id]);
5856 		if (!arvif || !arvif->is_created ||
5857 		    arvif->ar->scan.arvif != arvif)
5858 			continue;
5859 
5860 		ar = arvif->ar;
5861 
5862 		ath12k_scan_abort(ar);
5863 
5864 		cancel_delayed_work_sync(&ar->scan.timeout);
5865 	}
5866 }
5867 EXPORT_SYMBOL(ath12k_mac_op_cancel_hw_scan);
5868 
ath12k_install_key(struct ath12k_link_vif * arvif,struct ieee80211_key_conf * key,enum set_key_cmd cmd,const u8 * macaddr,u32 flags)5869 static int ath12k_install_key(struct ath12k_link_vif *arvif,
5870 			      struct ieee80211_key_conf *key,
5871 			      enum set_key_cmd cmd,
5872 			      const u8 *macaddr, u32 flags)
5873 {
5874 	int ret;
5875 	struct ath12k *ar = arvif->ar;
5876 	struct wmi_vdev_install_key_arg arg = {
5877 		.vdev_id = arvif->vdev_id,
5878 		.key_idx = key->keyidx,
5879 		.key_len = key->keylen,
5880 		.key_data = key->key,
5881 		.key_flags = flags,
5882 		.ieee80211_key_cipher = key->cipher,
5883 		.macaddr = macaddr,
5884 	};
5885 	struct ath12k_vif *ahvif = arvif->ahvif;
5886 
5887 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
5888 
5889 	if (test_bit(ATH12K_FLAG_HW_CRYPTO_DISABLED, &ar->ab->dev_flags))
5890 		return 0;
5891 
5892 	if (cmd == DISABLE_KEY) {
5893 		/* TODO: Check if FW expects  value other than NONE for del */
5894 		/* arg.key_cipher = WMI_CIPHER_NONE; */
5895 		arg.key_len = 0;
5896 		arg.key_data = NULL;
5897 		goto check_order;
5898 	}
5899 
5900 	switch (key->cipher) {
5901 	case WLAN_CIPHER_SUITE_CCMP:
5902 	case WLAN_CIPHER_SUITE_CCMP_256:
5903 		arg.key_cipher = WMI_CIPHER_AES_CCM;
5904 		key->flags |= IEEE80211_KEY_FLAG_GENERATE_IV_MGMT;
5905 		break;
5906 	case WLAN_CIPHER_SUITE_TKIP:
5907 		arg.key_cipher = WMI_CIPHER_TKIP;
5908 		arg.key_txmic_len = 8;
5909 		arg.key_rxmic_len = 8;
5910 		break;
5911 	case WLAN_CIPHER_SUITE_GCMP:
5912 	case WLAN_CIPHER_SUITE_GCMP_256:
5913 		arg.key_cipher = WMI_CIPHER_AES_GCM;
5914 		key->flags |= IEEE80211_KEY_FLAG_GENERATE_IV_MGMT;
5915 		break;
5916 	case WLAN_CIPHER_SUITE_AES_CMAC:
5917 		arg.key_cipher = WMI_CIPHER_AES_CMAC;
5918 		break;
5919 	case WLAN_CIPHER_SUITE_BIP_GMAC_128:
5920 	case WLAN_CIPHER_SUITE_BIP_GMAC_256:
5921 		arg.key_cipher = WMI_CIPHER_AES_GMAC;
5922 		break;
5923 	case WLAN_CIPHER_SUITE_BIP_CMAC_256:
5924 		arg.key_cipher = WMI_CIPHER_AES_CMAC;
5925 		break;
5926 	default:
5927 		ath12k_warn(ar->ab, "cipher %d is not supported\n", key->cipher);
5928 		return -EOPNOTSUPP;
5929 	}
5930 
5931 	if (test_bit(ATH12K_FLAG_RAW_MODE, &ar->ab->dev_flags))
5932 		key->flags |= IEEE80211_KEY_FLAG_GENERATE_IV |
5933 			      IEEE80211_KEY_FLAG_RESERVE_TAILROOM;
5934 
5935 check_order:
5936 	if (ahvif->vdev_type == WMI_VDEV_TYPE_STA &&
5937 	    arg.key_flags == WMI_KEY_GROUP) {
5938 		if (cmd == SET_KEY) {
5939 			if (arvif->pairwise_key_done) {
5940 				ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
5941 					   "vdev %u pairwise key done, go install group key\n",
5942 					   arg.vdev_id);
5943 				goto install;
5944 			} else {
5945 				/* WCN7850 firmware requires pairwise key to be installed
5946 				 * before group key. In case group key comes first, cache
5947 				 * it and return. Will revisit it once pairwise key gets
5948 				 * installed.
5949 				 */
5950 				arvif->group_key = arg;
5951 				arvif->group_key_valid = true;
5952 				ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
5953 					   "vdev %u group key before pairwise key, cache and skip\n",
5954 					   arg.vdev_id);
5955 
5956 				ret = 0;
5957 				goto out;
5958 			}
5959 		} else {
5960 			arvif->group_key_valid = false;
5961 		}
5962 	}
5963 
5964 install:
5965 	reinit_completion(&ar->install_key_done);
5966 
5967 	ret = ath12k_wmi_vdev_install_key(arvif->ar, &arg);
5968 	if (ret)
5969 		return ret;
5970 
5971 	if (!wait_for_completion_timeout(&ar->install_key_done, 1 * HZ))
5972 		return -ETIMEDOUT;
5973 
5974 	if (ether_addr_equal(arg.macaddr, arvif->bssid))
5975 		ahvif->dp_vif.key_cipher = arg.ieee80211_key_cipher;
5976 
5977 	if (ar->install_key_status) {
5978 		ret = -EINVAL;
5979 		goto out;
5980 	}
5981 
5982 	if (ahvif->vdev_type == WMI_VDEV_TYPE_STA &&
5983 	    arg.key_flags == WMI_KEY_PAIRWISE) {
5984 		if (cmd == SET_KEY) {
5985 			arvif->pairwise_key_done = true;
5986 			if (arvif->group_key_valid) {
5987 				/* Install cached GTK */
5988 				arvif->group_key_valid = false;
5989 				arg = arvif->group_key;
5990 				ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
5991 					   "vdev %u pairwise key done, group key ready, go install\n",
5992 					   arg.vdev_id);
5993 				goto install;
5994 			}
5995 		} else {
5996 			arvif->pairwise_key_done = false;
5997 		}
5998 	}
5999 
6000 out:
6001 	if (ret) {
6002 		/* In case of failure userspace may not do DISABLE_KEY
6003 		 * but triggers re-connection directly, so manually reset
6004 		 * status here.
6005 		 */
6006 		arvif->group_key_valid = false;
6007 		arvif->pairwise_key_done = false;
6008 	}
6009 
6010 	return ret;
6011 }
6012 
ath12k_clear_peer_keys(struct ath12k_link_vif * arvif,const u8 * addr)6013 static int ath12k_clear_peer_keys(struct ath12k_link_vif *arvif,
6014 				  const u8 *addr)
6015 {
6016 	struct ath12k *ar = arvif->ar;
6017 	struct ath12k_base *ab = ar->ab;
6018 	struct ath12k_dp_link_peer *peer;
6019 	int first_errno = 0;
6020 	int ret;
6021 	int i, len;
6022 	u32 flags = 0;
6023 	struct ath12k_dp *dp = ath12k_ab_to_dp(ab);
6024 	struct ieee80211_key_conf *keys[WMI_MAX_KEY_INDEX + 1] = {};
6025 
6026 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
6027 
6028 	spin_lock_bh(&dp->dp_lock);
6029 	peer = ath12k_dp_link_peer_find_by_vdev_and_addr(dp, arvif->vdev_id, addr);
6030 	if (!peer || !peer->dp_peer) {
6031 		spin_unlock_bh(&dp->dp_lock);
6032 		return -ENOENT;
6033 	}
6034 
6035 	len = ARRAY_SIZE(peer->dp_peer->keys);
6036 	for (i = 0; i < len; i++) {
6037 		if (!peer->dp_peer->keys[i])
6038 			continue;
6039 
6040 		keys[i] = peer->dp_peer->keys[i];
6041 		peer->dp_peer->keys[i] = NULL;
6042 	}
6043 
6044 	spin_unlock_bh(&dp->dp_lock);
6045 
6046 	for (i = 0; i < len; i++) {
6047 		if (!keys[i])
6048 			continue;
6049 
6050 		/* key flags are not required to delete the key */
6051 		ret = ath12k_install_key(arvif, keys[i],
6052 					 DISABLE_KEY, addr, flags);
6053 		if (ret < 0 && first_errno == 0)
6054 			first_errno = ret;
6055 
6056 		if (ret < 0)
6057 			ath12k_warn(ab, "failed to remove peer key %d: %d\n",
6058 				    i, ret);
6059 	}
6060 
6061 	return first_errno;
6062 }
6063 
ath12k_mac_set_key(struct ath12k * ar,enum set_key_cmd cmd,struct ath12k_link_vif * arvif,struct ath12k_link_sta * arsta,struct ieee80211_key_conf * key)6064 static int ath12k_mac_set_key(struct ath12k *ar, enum set_key_cmd cmd,
6065 			      struct ath12k_link_vif *arvif,
6066 			      struct ath12k_link_sta *arsta,
6067 			      struct ieee80211_key_conf *key)
6068 {
6069 	struct ieee80211_sta *sta = NULL;
6070 	struct ath12k_base *ab = ar->ab;
6071 	struct ath12k_dp_link_peer *peer;
6072 	struct ath12k_sta *ahsta;
6073 	const u8 *peer_addr;
6074 	int ret;
6075 	u32 flags = 0;
6076 	struct ath12k_dp *dp = ath12k_ab_to_dp(ab);
6077 
6078 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
6079 
6080 	if (arsta)
6081 		sta = ath12k_ahsta_to_sta(arsta->ahsta);
6082 
6083 	if (test_bit(ATH12K_FLAG_HW_CRYPTO_DISABLED, &ab->dev_flags))
6084 		return 1;
6085 
6086 	if (sta)
6087 		peer_addr = arsta->addr;
6088 	else
6089 		peer_addr = arvif->bssid;
6090 
6091 	key->hw_key_idx = key->keyidx;
6092 
6093 	/* the peer should not disappear in mid-way (unless FW goes awry) since
6094 	 * we already hold wiphy lock. we just make sure its there now.
6095 	 */
6096 	spin_lock_bh(&dp->dp_lock);
6097 	peer = ath12k_dp_link_peer_find_by_vdev_and_addr(dp, arvif->vdev_id,
6098 							 peer_addr);
6099 	if (!peer || !peer->dp_peer) {
6100 		spin_unlock_bh(&dp->dp_lock);
6101 
6102 		if (cmd == SET_KEY) {
6103 			ath12k_warn(ab, "cannot install key for non-existent peer %pM\n",
6104 				    peer_addr);
6105 			return -EOPNOTSUPP;
6106 		}
6107 
6108 		/* if the peer doesn't exist there is no key to disable
6109 		 * anymore
6110 		 */
6111 		return 0;
6112 	}
6113 
6114 	spin_unlock_bh(&dp->dp_lock);
6115 
6116 	if (key->flags & IEEE80211_KEY_FLAG_PAIRWISE)
6117 		flags = WMI_KEY_PAIRWISE;
6118 	else
6119 		flags = WMI_KEY_GROUP;
6120 
6121 	ret = ath12k_install_key(arvif, key, cmd, peer_addr, flags);
6122 	if (ret) {
6123 		ath12k_warn(ab, "ath12k_install_key failed (%d)\n", ret);
6124 		return ret;
6125 	}
6126 
6127 	ret = ath12k_dp_rx_peer_pn_replay_config(arvif, peer_addr, cmd, key);
6128 	if (ret) {
6129 		ath12k_warn(ab, "failed to offload PN replay detection %d\n", ret);
6130 		return ret;
6131 	}
6132 
6133 	spin_lock_bh(&dp->dp_lock);
6134 	peer = ath12k_dp_link_peer_find_by_vdev_and_addr(dp, arvif->vdev_id,
6135 							 peer_addr);
6136 	if (peer && peer->dp_peer && cmd == SET_KEY) {
6137 		peer->dp_peer->keys[key->keyidx] = key;
6138 		if (key->flags & IEEE80211_KEY_FLAG_PAIRWISE) {
6139 			peer->dp_peer->ucast_keyidx = key->keyidx;
6140 			peer->dp_peer->sec_type =
6141 					ath12k_dp_tx_get_encrypt_type(key->cipher);
6142 		} else {
6143 			peer->dp_peer->mcast_keyidx = key->keyidx;
6144 			peer->dp_peer->sec_type_grp =
6145 					ath12k_dp_tx_get_encrypt_type(key->cipher);
6146 		}
6147 	} else if (peer && peer->dp_peer && cmd == DISABLE_KEY) {
6148 		peer->dp_peer->keys[key->keyidx] = NULL;
6149 		if (key->flags & IEEE80211_KEY_FLAG_PAIRWISE)
6150 			peer->dp_peer->ucast_keyidx = 0;
6151 		else
6152 			peer->dp_peer->mcast_keyidx = 0;
6153 	} else if (!peer)
6154 		/* impossible unless FW goes crazy */
6155 		ath12k_warn(ab, "peer %pM disappeared!\n", peer_addr);
6156 
6157 	if (sta) {
6158 		ahsta = ath12k_sta_to_ahsta(sta);
6159 
6160 		switch (key->cipher) {
6161 		case WLAN_CIPHER_SUITE_TKIP:
6162 		case WLAN_CIPHER_SUITE_CCMP:
6163 		case WLAN_CIPHER_SUITE_CCMP_256:
6164 		case WLAN_CIPHER_SUITE_GCMP:
6165 		case WLAN_CIPHER_SUITE_GCMP_256:
6166 			if (cmd == SET_KEY)
6167 				ahsta->pn_type = HAL_PN_TYPE_WPA;
6168 			else
6169 				ahsta->pn_type = HAL_PN_TYPE_NONE;
6170 			break;
6171 		default:
6172 			ahsta->pn_type = HAL_PN_TYPE_NONE;
6173 			break;
6174 		}
6175 	}
6176 
6177 	spin_unlock_bh(&dp->dp_lock);
6178 
6179 	return 0;
6180 }
6181 
ath12k_mac_update_key_cache(struct ath12k_vif_cache * cache,enum set_key_cmd cmd,struct ieee80211_sta * sta,struct ieee80211_key_conf * key)6182 static int ath12k_mac_update_key_cache(struct ath12k_vif_cache *cache,
6183 				       enum set_key_cmd cmd,
6184 				       struct ieee80211_sta *sta,
6185 				       struct ieee80211_key_conf *key)
6186 {
6187 	struct ath12k_key_conf *key_conf, *tmp;
6188 
6189 	list_for_each_entry_safe(key_conf, tmp, &cache->key_conf.list, list) {
6190 		if (key_conf->key != key)
6191 			continue;
6192 
6193 		/* If SET key entry is already present in cache, nothing to do,
6194 		 * just return
6195 		 */
6196 		if (cmd == SET_KEY)
6197 			return 0;
6198 
6199 		/* DEL key for an old SET key which driver hasn't flushed yet.
6200 		 */
6201 		list_del(&key_conf->list);
6202 		kfree(key_conf);
6203 	}
6204 
6205 	if (cmd == SET_KEY) {
6206 		key_conf = kzalloc_obj(*key_conf);
6207 
6208 		if (!key_conf)
6209 			return -ENOMEM;
6210 
6211 		key_conf->cmd = cmd;
6212 		key_conf->sta = sta;
6213 		key_conf->key = key;
6214 		list_add_tail(&key_conf->list,
6215 			      &cache->key_conf.list);
6216 	}
6217 
6218 	return 0;
6219 }
6220 
ath12k_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)6221 int ath12k_mac_op_set_key(struct ieee80211_hw *hw, enum set_key_cmd cmd,
6222 			  struct ieee80211_vif *vif, struct ieee80211_sta *sta,
6223 			  struct ieee80211_key_conf *key)
6224 {
6225 	struct ath12k_vif *ahvif = ath12k_vif_to_ahvif(vif);
6226 	struct ath12k_link_vif *arvif;
6227 	struct ath12k_link_sta *arsta = NULL;
6228 	struct ath12k_vif_cache *cache;
6229 	struct ath12k_sta *ahsta;
6230 	unsigned long links;
6231 	u8 link_id;
6232 	int ret;
6233 
6234 	lockdep_assert_wiphy(hw->wiphy);
6235 
6236 	/* IGTK needs to be done in host software */
6237 	if (key->keyidx == 4 || key->keyidx == 5)
6238 		return 1;
6239 
6240 	if (key->keyidx > WMI_MAX_KEY_INDEX)
6241 		return -ENOSPC;
6242 
6243 	if (sta) {
6244 		ahsta = ath12k_sta_to_ahsta(sta);
6245 
6246 		/* For an ML STA Pairwise key is same for all associated link Stations,
6247 		 * hence do set key for all link STAs which are active.
6248 		 */
6249 		if (sta->mlo) {
6250 			links = ahsta->links_map;
6251 			for_each_set_bit(link_id, &links, IEEE80211_MLD_MAX_NUM_LINKS) {
6252 				arvif = wiphy_dereference(hw->wiphy,
6253 							  ahvif->link[link_id]);
6254 				arsta = wiphy_dereference(hw->wiphy,
6255 							  ahsta->link[link_id]);
6256 
6257 				if (WARN_ON(!arvif || !arsta))
6258 					/* arvif and arsta are expected to be valid when
6259 					 * STA is present.
6260 					 */
6261 					continue;
6262 
6263 				ret = ath12k_mac_set_key(arvif->ar, cmd, arvif,
6264 							 arsta, key);
6265 				if (ret)
6266 					break;
6267 			}
6268 
6269 			return 0;
6270 		}
6271 
6272 		arsta = &ahsta->deflink;
6273 		arvif = arsta->arvif;
6274 		if (WARN_ON(!arvif))
6275 			return -EINVAL;
6276 
6277 		ret = ath12k_mac_set_key(arvif->ar, cmd, arvif, arsta, key);
6278 		if (ret)
6279 			return ret;
6280 
6281 		return 0;
6282 	}
6283 
6284 	if (key->link_id >= 0 && key->link_id < IEEE80211_MLD_MAX_NUM_LINKS) {
6285 		link_id = key->link_id;
6286 		arvif = wiphy_dereference(hw->wiphy, ahvif->link[link_id]);
6287 	} else {
6288 		link_id = 0;
6289 		arvif = &ahvif->deflink;
6290 	}
6291 
6292 	if (!arvif || !arvif->is_created) {
6293 		cache = ath12k_ahvif_get_link_cache(ahvif, link_id);
6294 		if (!cache)
6295 			return -ENOSPC;
6296 
6297 		ret = ath12k_mac_update_key_cache(cache, cmd, sta, key);
6298 		if (ret)
6299 			return ret;
6300 
6301 		return 0;
6302 	}
6303 
6304 	ret = ath12k_mac_set_key(arvif->ar, cmd, arvif, NULL, key);
6305 	if (ret)
6306 		return ret;
6307 
6308 	return 0;
6309 }
6310 EXPORT_SYMBOL(ath12k_mac_op_set_key);
6311 
6312 static int
ath12k_mac_bitrate_mask_num_vht_rates(struct ath12k * ar,enum nl80211_band band,const struct cfg80211_bitrate_mask * mask)6313 ath12k_mac_bitrate_mask_num_vht_rates(struct ath12k *ar,
6314 				      enum nl80211_band band,
6315 				      const struct cfg80211_bitrate_mask *mask)
6316 {
6317 	int num_rates = 0;
6318 	int i;
6319 
6320 	for (i = 0; i < ARRAY_SIZE(mask->control[band].vht_mcs); i++)
6321 		num_rates += hweight16(mask->control[band].vht_mcs[i]);
6322 
6323 	return num_rates;
6324 }
6325 
6326 static int
ath12k_mac_bitrate_mask_num_he_rates(struct ath12k * ar,enum nl80211_band band,const struct cfg80211_bitrate_mask * mask)6327 ath12k_mac_bitrate_mask_num_he_rates(struct ath12k *ar,
6328 				     enum nl80211_band band,
6329 				     const struct cfg80211_bitrate_mask *mask)
6330 {
6331 	int num_rates = 0;
6332 	int i;
6333 
6334 	for (i = 0; i < ARRAY_SIZE(mask->control[band].he_mcs); i++)
6335 		num_rates += hweight16(mask->control[band].he_mcs[i]);
6336 
6337 	return num_rates;
6338 }
6339 
6340 static int
ath12k_mac_bitrate_mask_num_eht_rates(struct ath12k * ar,enum nl80211_band band,const struct cfg80211_bitrate_mask * mask)6341 ath12k_mac_bitrate_mask_num_eht_rates(struct ath12k *ar,
6342 				      enum nl80211_band band,
6343 				      const struct cfg80211_bitrate_mask *mask)
6344 {
6345 	int num_rates = 0;
6346 	int i;
6347 
6348 	for (i = 0; i < ARRAY_SIZE(mask->control[band].eht_mcs); i++)
6349 		num_rates += hweight16(mask->control[band].eht_mcs[i]);
6350 
6351 	return num_rates;
6352 }
6353 
6354 static int
ath12k_mac_set_peer_vht_fixed_rate(struct ath12k_link_vif * arvif,struct ath12k_link_sta * arsta,const struct cfg80211_bitrate_mask * mask,enum nl80211_band band)6355 ath12k_mac_set_peer_vht_fixed_rate(struct ath12k_link_vif *arvif,
6356 				   struct ath12k_link_sta *arsta,
6357 				   const struct cfg80211_bitrate_mask *mask,
6358 				   enum nl80211_band band)
6359 {
6360 	struct ath12k *ar = arvif->ar;
6361 	u8 vht_rate, nss;
6362 	u32 rate_code;
6363 	int ret, i;
6364 
6365 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
6366 
6367 	nss = 0;
6368 
6369 	for (i = 0; i < ARRAY_SIZE(mask->control[band].vht_mcs); i++) {
6370 		if (hweight16(mask->control[band].vht_mcs[i]) == 1) {
6371 			nss = i + 1;
6372 			vht_rate = ffs(mask->control[band].vht_mcs[i]) - 1;
6373 		}
6374 	}
6375 
6376 	if (!nss) {
6377 		ath12k_warn(ar->ab, "No single VHT Fixed rate found to set for %pM",
6378 			    arsta->addr);
6379 		return -EINVAL;
6380 	}
6381 
6382 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
6383 		   "Setting Fixed VHT Rate for peer %pM. Device will not switch to any other selected rates",
6384 		   arsta->addr);
6385 
6386 	rate_code = ATH12K_HW_RATE_CODE(vht_rate, nss - 1,
6387 					WMI_RATE_PREAMBLE_VHT);
6388 	ret = ath12k_wmi_set_peer_param(ar, arsta->addr,
6389 					arvif->vdev_id,
6390 					WMI_PEER_PARAM_FIXED_RATE,
6391 					rate_code);
6392 	if (ret)
6393 		ath12k_warn(ar->ab,
6394 			    "failed to update STA %pM Fixed Rate %d: %d\n",
6395 			     arsta->addr, rate_code, ret);
6396 
6397 	return ret;
6398 }
6399 
6400 static int
ath12k_mac_set_peer_he_fixed_rate(struct ath12k_link_vif * arvif,struct ath12k_link_sta * arsta,const struct cfg80211_bitrate_mask * mask,enum nl80211_band band)6401 ath12k_mac_set_peer_he_fixed_rate(struct ath12k_link_vif *arvif,
6402 				  struct ath12k_link_sta *arsta,
6403 				  const struct cfg80211_bitrate_mask *mask,
6404 				  enum nl80211_band band)
6405 {
6406 	struct ath12k *ar = arvif->ar;
6407 	u8 he_rate, nss;
6408 	u32 rate_code;
6409 	int ret, i;
6410 	struct ath12k_sta *ahsta = arsta->ahsta;
6411 	struct ieee80211_sta *sta;
6412 
6413 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
6414 
6415 	sta = ath12k_ahsta_to_sta(ahsta);
6416 	nss = 0;
6417 
6418 	for (i = 0; i < ARRAY_SIZE(mask->control[band].he_mcs); i++) {
6419 		if (hweight16(mask->control[band].he_mcs[i]) == 1) {
6420 			nss = i + 1;
6421 			he_rate = ffs(mask->control[band].he_mcs[i]) - 1;
6422 		}
6423 	}
6424 
6425 	if (!nss) {
6426 		ath12k_warn(ar->ab, "No single HE Fixed rate found to set for %pM",
6427 			    arsta->addr);
6428 		return -EINVAL;
6429 	}
6430 
6431 	/* Avoid updating invalid nss as fixed rate*/
6432 	if (nss > sta->deflink.rx_nss)
6433 		return -EINVAL;
6434 
6435 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
6436 		   "Setting Fixed HE Rate for peer %pM. Device will not switch to any other selected rates",
6437 		   arsta->addr);
6438 
6439 	rate_code = ATH12K_HW_RATE_CODE(he_rate, nss - 1,
6440 					WMI_RATE_PREAMBLE_HE);
6441 
6442 	ret = ath12k_wmi_set_peer_param(ar, arsta->addr,
6443 					arvif->vdev_id,
6444 					WMI_PEER_PARAM_FIXED_RATE,
6445 					rate_code);
6446 	if (ret)
6447 		ath12k_warn(ar->ab,
6448 			    "failed to update STA %pM Fixed Rate %d: %d\n",
6449 			    arsta->addr, rate_code, ret);
6450 
6451 	return ret;
6452 }
6453 
6454 static int
ath12k_mac_set_peer_eht_fixed_rate(struct ath12k_link_vif * arvif,struct ath12k_link_sta * arsta,const struct cfg80211_bitrate_mask * mask,enum nl80211_band band)6455 ath12k_mac_set_peer_eht_fixed_rate(struct ath12k_link_vif *arvif,
6456 				   struct ath12k_link_sta *arsta,
6457 				   const struct cfg80211_bitrate_mask *mask,
6458 				   enum nl80211_band band)
6459 {
6460 	struct ath12k_sta *ahsta = arsta->ahsta;
6461 	struct ath12k *ar = arvif->ar;
6462 	struct ieee80211_sta *sta;
6463 	struct ieee80211_link_sta *link_sta;
6464 	u8 eht_rate, nss = 0;
6465 	u32 rate_code;
6466 	int ret, i;
6467 
6468 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
6469 
6470 	sta = ath12k_ahsta_to_sta(ahsta);
6471 
6472 	for (i = 0; i < ARRAY_SIZE(mask->control[band].eht_mcs); i++) {
6473 		if (hweight16(mask->control[band].eht_mcs[i]) == 1) {
6474 			nss = i + 1;
6475 			eht_rate = ffs(mask->control[band].eht_mcs[i]) - 1;
6476 		}
6477 	}
6478 
6479 	if (!nss) {
6480 		ath12k_warn(ar->ab, "No single EHT Fixed rate found to set for %pM\n",
6481 			    arsta->addr);
6482 		return -EINVAL;
6483 	}
6484 
6485 	/* Avoid updating invalid nss as fixed rate*/
6486 	link_sta = ath12k_mac_get_link_sta(arsta);
6487 	if (!link_sta || nss > link_sta->rx_nss) {
6488 		ath12k_warn(ar->ab,
6489 			    "unable to access link sta for sta %pM link %u or fixed nss of %u is not supported by sta\n",
6490 			    sta->addr, arsta->link_id, nss);
6491 		return -EINVAL;
6492 	}
6493 
6494 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
6495 		   "Setting Fixed EHT Rate for peer %pM. Device will not switch to any other selected rates\n",
6496 		   arsta->addr);
6497 
6498 	rate_code = ATH12K_HW_RATE_CODE(eht_rate, nss - 1,
6499 					WMI_RATE_PREAMBLE_EHT);
6500 
6501 	ret = ath12k_wmi_set_peer_param(ar, arsta->addr,
6502 					arvif->vdev_id,
6503 					WMI_PEER_PARAM_FIXED_RATE,
6504 					rate_code);
6505 	if (ret)
6506 		ath12k_warn(ar->ab,
6507 			    "failed to update STA %pM Fixed Rate %d: %d\n",
6508 			    arsta->addr, rate_code, ret);
6509 
6510 	return ret;
6511 }
6512 
ath12k_mac_station_assoc(struct ath12k * ar,struct ath12k_link_vif * arvif,struct ath12k_link_sta * arsta,bool reassoc)6513 static int ath12k_mac_station_assoc(struct ath12k *ar,
6514 				    struct ath12k_link_vif *arvif,
6515 				    struct ath12k_link_sta *arsta,
6516 				    bool reassoc)
6517 {
6518 	struct ieee80211_vif *vif = ath12k_ahvif_to_vif(arvif->ahvif);
6519 	struct ieee80211_sta *sta = ath12k_ahsta_to_sta(arsta->ahsta);
6520 	struct ieee80211_link_sta *link_sta;
6521 	int ret;
6522 	struct cfg80211_chan_def def;
6523 	enum nl80211_band band;
6524 	struct cfg80211_bitrate_mask *mask;
6525 	u8 num_vht_rates, num_he_rates, num_eht_rates;
6526 	u8 link_id = arvif->link_id;
6527 
6528 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
6529 
6530 	if (WARN_ON(ath12k_mac_vif_link_chan(vif, arvif->link_id, &def)))
6531 		return -EPERM;
6532 
6533 	if (WARN_ON(!rcu_access_pointer(sta->link[link_id])))
6534 		return -EINVAL;
6535 
6536 	band = def.chan->band;
6537 	mask = &arvif->bitrate_mask;
6538 
6539 	struct ath12k_wmi_peer_assoc_arg *peer_arg __free(kfree) =
6540 		kzalloc_obj(*peer_arg);
6541 	if (!peer_arg)
6542 		return -ENOMEM;
6543 
6544 	ath12k_peer_assoc_prepare(ar, arvif, arsta, peer_arg, reassoc);
6545 
6546 	if (peer_arg->peer_nss < 1) {
6547 		ath12k_warn(ar->ab,
6548 			    "invalid peer NSS %d\n", peer_arg->peer_nss);
6549 		return -EINVAL;
6550 	}
6551 
6552 	peer_arg->is_assoc = true;
6553 
6554 	ret = ath12k_mac_peer_assoc(ar, peer_arg);
6555 	if (ret)
6556 		return ret;
6557 
6558 	num_vht_rates = ath12k_mac_bitrate_mask_num_vht_rates(ar, band, mask);
6559 	num_he_rates = ath12k_mac_bitrate_mask_num_he_rates(ar, band, mask);
6560 	num_eht_rates = ath12k_mac_bitrate_mask_num_eht_rates(ar, band, mask);
6561 
6562 	/* If single VHT/HE/EHT rate is configured (by set_bitrate_mask()),
6563 	 * peer_assoc will disable VHT/HE/EHT. This is now enabled by a peer
6564 	 * specific fixed param.
6565 	 * Note that all other rates and NSS will be disabled for this peer.
6566 	 */
6567 	link_sta = ath12k_mac_get_link_sta(arsta);
6568 	if (!link_sta) {
6569 		ath12k_warn(ar->ab, "unable to access link sta in station assoc\n");
6570 		return -EINVAL;
6571 	}
6572 
6573 	spin_lock_bh(&ar->data_lock);
6574 	arsta->bw = ath12k_mac_ieee80211_sta_bw_to_wmi(ar, link_sta);
6575 	arsta->bw_prev = link_sta->bandwidth;
6576 	spin_unlock_bh(&ar->data_lock);
6577 
6578 	if (link_sta->vht_cap.vht_supported && num_vht_rates == 1) {
6579 		ret = ath12k_mac_set_peer_vht_fixed_rate(arvif, arsta, mask, band);
6580 	} else if (link_sta->he_cap.has_he && num_he_rates == 1) {
6581 		ret = ath12k_mac_set_peer_he_fixed_rate(arvif, arsta, mask, band);
6582 		if (ret)
6583 			return ret;
6584 	} else if (link_sta->eht_cap.has_eht && num_eht_rates == 1) {
6585 		ret = ath12k_mac_set_peer_eht_fixed_rate(arvif, arsta, mask, band);
6586 		if (ret)
6587 			return ret;
6588 	}
6589 
6590 	/* Re-assoc is run only to update supported rates for given station. It
6591 	 * doesn't make much sense to reconfigure the peer completely.
6592 	 */
6593 	if (reassoc)
6594 		return 0;
6595 
6596 	ret = ath12k_setup_peer_smps(ar, arvif, arsta->addr,
6597 				     &link_sta->ht_cap, &link_sta->he_6ghz_capa);
6598 	if (ret) {
6599 		ath12k_warn(ar->ab, "failed to setup peer SMPS for vdev %d: %d\n",
6600 			    arvif->vdev_id, ret);
6601 		return ret;
6602 	}
6603 
6604 	if (!sta->wme) {
6605 		arvif->num_legacy_stations++;
6606 		ret = ath12k_recalc_rtscts_prot(arvif);
6607 		if (ret)
6608 			return ret;
6609 	}
6610 
6611 	if (sta->wme && sta->uapsd_queues) {
6612 		ret = ath12k_peer_assoc_qos_ap(ar, arvif, arsta);
6613 		if (ret) {
6614 			ath12k_warn(ar->ab, "failed to set qos params for STA %pM for vdev %i: %d\n",
6615 				    arsta->addr, arvif->vdev_id, ret);
6616 			return ret;
6617 		}
6618 	}
6619 
6620 	return 0;
6621 }
6622 
ath12k_mac_station_disassoc(struct ath12k * ar,struct ath12k_link_vif * arvif,struct ath12k_link_sta * arsta)6623 static int ath12k_mac_station_disassoc(struct ath12k *ar,
6624 				       struct ath12k_link_vif *arvif,
6625 				       struct ath12k_link_sta *arsta)
6626 {
6627 	struct ieee80211_sta *sta = ath12k_ahsta_to_sta(arsta->ahsta);
6628 
6629 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
6630 
6631 	if (!sta->wme) {
6632 		arvif->num_legacy_stations--;
6633 		return ath12k_recalc_rtscts_prot(arvif);
6634 	}
6635 
6636 	return 0;
6637 }
6638 
ath12k_mac_sta_set_4addr(struct wiphy * wiphy,struct ath12k_sta * ahsta)6639 static int ath12k_mac_sta_set_4addr(struct wiphy *wiphy, struct ath12k_sta *ahsta)
6640 {
6641 	struct ath12k_dp_link_peer *peer;
6642 	struct ath12k_link_vif *arvif;
6643 	struct ath12k_link_sta *arsta;
6644 	struct ath12k_vif *ahvif;
6645 	struct ath12k_dp *dp;
6646 	unsigned long links;
6647 	struct ath12k *ar;
6648 	u8 link_id;
6649 	int ret;
6650 
6651 	links = ahsta->links_map;
6652 	for_each_set_bit(link_id, &links, IEEE80211_MLD_MAX_NUM_LINKS) {
6653 		arsta = wiphy_dereference(wiphy, ahsta->link[link_id]);
6654 		if (!arsta)
6655 			continue;
6656 
6657 		arvif = arsta->arvif;
6658 		ahvif = arvif->ahvif;
6659 		ar = arvif->ar;
6660 
6661 		if (arvif->set_wds_vdev_param)
6662 			goto skip_nawds;
6663 
6664 		ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
6665 			   "setting USE_4ADDR for peer %pM\n", arsta->addr);
6666 
6667 		ret = ath12k_wmi_set_peer_param(ar, arsta->addr,
6668 						arvif->vdev_id,
6669 						WMI_PEER_USE_4ADDR,
6670 						WMI_PEER_4ADDR_ALLOW_EAPOL_DATA_FRAME);
6671 		if (ret) {
6672 			ath12k_warn(ar->ab, "failed to set peer %pM 4addr capability: %d\n",
6673 				    arsta->addr, ret);
6674 			return ret;
6675 		}
6676 
6677 		if (ahvif->dp_vif.tx_encap_type != ATH12K_HW_TXRX_ETHERNET)
6678 			goto skip_nawds;
6679 
6680 		ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
6681 						    WMI_VDEV_PARAM_AP_ENABLE_NAWDS,
6682 						    WDS_EXT_ENABLE);
6683 		if (ret) {
6684 			ath12k_warn(ar->ab, "failed to set vdev %d nawds parameter: %d\n",
6685 				    arvif->vdev_id, ret);
6686 			return ret;
6687 		}
6688 
6689 		arvif->nawds_enabled = true;
6690 
6691 skip_nawds:
6692 		dp = ath12k_ab_to_dp(ar->ab);
6693 		spin_lock_bh(&dp->dp_lock);
6694 		peer = ath12k_dp_link_peer_find_by_vdev_and_addr(dp, arvif->vdev_id,
6695 								 arsta->addr);
6696 		if (peer && peer->dp_peer) {
6697 			peer->dp_peer->ucast_ra_only = true;
6698 			peer->dp_peer->use_4addr = true;
6699 		} else {
6700 			spin_unlock_bh(&dp->dp_lock);
6701 			ath12k_warn(ar->ab, "failed to find DP peer for %pM\n",
6702 				    arsta->addr);
6703 			return -ENOENT;
6704 		}
6705 
6706 		spin_unlock_bh(&dp->dp_lock);
6707 	}
6708 
6709 	return 0;
6710 }
6711 
ath12k_sta_rc_update_wk(struct wiphy * wiphy,struct wiphy_work * wk)6712 static void ath12k_sta_rc_update_wk(struct wiphy *wiphy, struct wiphy_work *wk)
6713 {
6714 	struct ieee80211_link_sta *link_sta;
6715 	struct ath12k *ar;
6716 	struct ath12k_link_vif *arvif;
6717 	struct ieee80211_sta *sta;
6718 	struct cfg80211_chan_def def;
6719 	enum nl80211_band band;
6720 	const u8 *ht_mcs_mask;
6721 	const u16 *vht_mcs_mask;
6722 	const u16 *he_mcs_mask;
6723 	const u16 *eht_mcs_mask;
6724 	u32 changed, bw, nss, mac_nss, smps, bw_prev;
6725 	int err, num_vht_rates, num_he_rates, num_eht_rates;
6726 	const struct cfg80211_bitrate_mask *mask;
6727 	enum wmi_phy_mode peer_phymode;
6728 	struct ath12k_link_sta *arsta;
6729 	struct ieee80211_vif *vif;
6730 
6731 	lockdep_assert_wiphy(wiphy);
6732 
6733 	arsta = container_of(wk, struct ath12k_link_sta, update_wk);
6734 	sta = ath12k_ahsta_to_sta(arsta->ahsta);
6735 	arvif = arsta->arvif;
6736 	vif = ath12k_ahvif_to_vif(arvif->ahvif);
6737 	ar = arvif->ar;
6738 
6739 	if (WARN_ON(ath12k_mac_vif_link_chan(vif, arvif->link_id, &def)))
6740 		return;
6741 
6742 	band = def.chan->band;
6743 	ht_mcs_mask = arvif->bitrate_mask.control[band].ht_mcs;
6744 	vht_mcs_mask = arvif->bitrate_mask.control[band].vht_mcs;
6745 	he_mcs_mask = arvif->bitrate_mask.control[band].he_mcs;
6746 	eht_mcs_mask = arvif->bitrate_mask.control[band].eht_mcs;
6747 
6748 	spin_lock_bh(&ar->data_lock);
6749 
6750 	changed = arsta->changed;
6751 	arsta->changed = 0;
6752 
6753 	bw = arsta->bw;
6754 	bw_prev = arsta->bw_prev;
6755 	nss = arsta->nss;
6756 	smps = arsta->smps;
6757 
6758 	spin_unlock_bh(&ar->data_lock);
6759 
6760 	nss = max_t(u32, 1, nss);
6761 	mac_nss = max3(ath12k_mac_max_ht_nss(ht_mcs_mask),
6762 		       ath12k_mac_max_vht_nss(vht_mcs_mask),
6763 		       ath12k_mac_max_he_nss(he_mcs_mask));
6764 	mac_nss = max(mac_nss, ath12k_mac_max_eht_nss(eht_mcs_mask));
6765 	nss = min(nss, mac_nss);
6766 
6767 	struct ath12k_wmi_peer_assoc_arg *peer_arg __free(kfree) =
6768 					kzalloc_obj(*peer_arg);
6769 	if (!peer_arg)
6770 		return;
6771 
6772 	if (changed & IEEE80211_RC_BW_CHANGED) {
6773 		ath12k_peer_assoc_h_phymode(ar, arvif, arsta, peer_arg);
6774 		peer_phymode = peer_arg->peer_phymode;
6775 
6776 		if (bw > bw_prev) {
6777 			/* Phymode shows maximum supported channel width, if we
6778 			 * upgrade bandwidth then due to sanity check of firmware,
6779 			 * we have to send WMI_PEER_PHYMODE followed by
6780 			 * WMI_PEER_CHWIDTH
6781 			 */
6782 			ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac bandwidth upgrade for sta %pM new %d old %d\n",
6783 				   arsta->addr, bw, bw_prev);
6784 			err = ath12k_wmi_set_peer_param(ar, arsta->addr,
6785 							arvif->vdev_id, WMI_PEER_PHYMODE,
6786 							peer_phymode);
6787 			if (err) {
6788 				ath12k_warn(ar->ab, "failed to update STA %pM to peer phymode %d: %d\n",
6789 					    arsta->addr, peer_phymode, err);
6790 				return;
6791 			}
6792 			err = ath12k_wmi_set_peer_param(ar, arsta->addr,
6793 							arvif->vdev_id, WMI_PEER_CHWIDTH,
6794 							bw);
6795 			if (err)
6796 				ath12k_warn(ar->ab, "failed to update STA %pM to peer bandwidth %d: %d\n",
6797 					    arsta->addr, bw, err);
6798 		} else {
6799 			/* When we downgrade bandwidth this will conflict with phymode
6800 			 * and cause to trigger firmware crash. In this case we send
6801 			 * WMI_PEER_CHWIDTH followed by WMI_PEER_PHYMODE
6802 			 */
6803 			ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac bandwidth downgrade for sta %pM new %d old %d\n",
6804 				   arsta->addr, bw, bw_prev);
6805 			err = ath12k_wmi_set_peer_param(ar, arsta->addr,
6806 							arvif->vdev_id, WMI_PEER_CHWIDTH,
6807 							bw);
6808 			if (err) {
6809 				ath12k_warn(ar->ab, "failed to update STA %pM peer to bandwidth %d: %d\n",
6810 					    arsta->addr, bw, err);
6811 				return;
6812 			}
6813 			err = ath12k_wmi_set_peer_param(ar, arsta->addr,
6814 							arvif->vdev_id, WMI_PEER_PHYMODE,
6815 							peer_phymode);
6816 			if (err)
6817 				ath12k_warn(ar->ab, "failed to update STA %pM to peer phymode %d: %d\n",
6818 					    arsta->addr, peer_phymode, err);
6819 		}
6820 	}
6821 
6822 	if (changed & IEEE80211_RC_NSS_CHANGED) {
6823 		ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac update sta %pM nss %d\n",
6824 			   arsta->addr, nss);
6825 
6826 		err = ath12k_wmi_set_peer_param(ar, arsta->addr, arvif->vdev_id,
6827 						WMI_PEER_NSS, nss);
6828 		if (err)
6829 			ath12k_warn(ar->ab, "failed to update STA %pM nss %d: %d\n",
6830 				    arsta->addr, nss, err);
6831 	}
6832 
6833 	if (changed & IEEE80211_RC_SMPS_CHANGED) {
6834 		ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac update sta %pM smps %d\n",
6835 			   arsta->addr, smps);
6836 
6837 		err = ath12k_wmi_set_peer_param(ar, arsta->addr, arvif->vdev_id,
6838 						WMI_PEER_MIMO_PS_STATE, smps);
6839 		if (err)
6840 			ath12k_warn(ar->ab, "failed to update STA %pM smps %d: %d\n",
6841 				    arsta->addr, smps, err);
6842 	}
6843 
6844 	if (changed & IEEE80211_RC_SUPP_RATES_CHANGED) {
6845 		mask = &arvif->bitrate_mask;
6846 		num_vht_rates = ath12k_mac_bitrate_mask_num_vht_rates(ar, band,
6847 								      mask);
6848 		num_he_rates = ath12k_mac_bitrate_mask_num_he_rates(ar, band,
6849 								    mask);
6850 		num_eht_rates = ath12k_mac_bitrate_mask_num_eht_rates(ar, band,
6851 								      mask);
6852 
6853 		/* Peer_assoc_prepare will reject vht rates in
6854 		 * bitrate_mask if its not available in range format and
6855 		 * sets vht tx_rateset as unsupported. So multiple VHT MCS
6856 		 * setting(eg. MCS 4,5,6) per peer is not supported here.
6857 		 * But, Single rate in VHT mask can be set as per-peer
6858 		 * fixed rate. But even if any HT rates are configured in
6859 		 * the bitrate mask, device will not switch to those rates
6860 		 * when per-peer Fixed rate is set.
6861 		 * TODO: Check RATEMASK_CMDID to support auto rates selection
6862 		 * across HT/VHT and for multiple VHT MCS support.
6863 		 */
6864 		link_sta = ath12k_mac_get_link_sta(arsta);
6865 		if (!link_sta) {
6866 			ath12k_warn(ar->ab, "unable to access link sta in peer assoc he for sta %pM link %u\n",
6867 				    sta->addr, arsta->link_id);
6868 			return;
6869 		}
6870 
6871 		if (link_sta->vht_cap.vht_supported && num_vht_rates == 1) {
6872 			ath12k_mac_set_peer_vht_fixed_rate(arvif, arsta, mask,
6873 							   band);
6874 		} else if (link_sta->he_cap.has_he && num_he_rates == 1) {
6875 			ath12k_mac_set_peer_he_fixed_rate(arvif, arsta, mask, band);
6876 		} else if (link_sta->eht_cap.has_eht && num_eht_rates == 1) {
6877 			err = ath12k_mac_set_peer_eht_fixed_rate(arvif, arsta,
6878 								 mask, band);
6879 			if (err) {
6880 				ath12k_warn(ar->ab,
6881 					    "failed to set peer EHT fixed rate for STA %pM ret %d\n",
6882 					    arsta->addr, err);
6883 				return;
6884 			}
6885 		} else {
6886 			/* If the peer is non-VHT/HE/EHT or no fixed VHT/HE/EHT
6887 			 * rate is provided in the new bitrate mask we set the
6888 			 * other rates using peer_assoc command. Also clear
6889 			 * the peer fixed rate settings as it has higher proprity
6890 			 * than peer assoc
6891 			 */
6892 			err = ath12k_wmi_set_peer_param(ar, arsta->addr,
6893 							arvif->vdev_id,
6894 							WMI_PEER_PARAM_FIXED_RATE,
6895 							WMI_FIXED_RATE_NONE);
6896 			if (err)
6897 				ath12k_warn(ar->ab,
6898 					    "failed to disable peer fixed rate for STA %pM ret %d\n",
6899 					    arsta->addr, err);
6900 
6901 			ath12k_peer_assoc_prepare(ar, arvif, arsta,
6902 						  peer_arg, true);
6903 
6904 			peer_arg->is_assoc = false;
6905 
6906 			ath12k_mac_peer_assoc(ar, peer_arg);
6907 		}
6908 	}
6909 }
6910 
ath12k_mac_free_unassign_link_sta(struct ath12k_hw * ah,struct ath12k_sta * ahsta,u8 link_id)6911 static void ath12k_mac_free_unassign_link_sta(struct ath12k_hw *ah,
6912 					      struct ath12k_sta *ahsta,
6913 					      u8 link_id)
6914 {
6915 	struct ath12k_link_sta *arsta;
6916 
6917 	lockdep_assert_wiphy(ah->hw->wiphy);
6918 
6919 	if (WARN_ON(link_id >= IEEE80211_MLD_MAX_NUM_LINKS))
6920 		return;
6921 
6922 	arsta = wiphy_dereference(ah->hw->wiphy, ahsta->link[link_id]);
6923 	if (WARN_ON(!arsta))
6924 		return;
6925 
6926 	ahsta->links_map &= ~BIT(link_id);
6927 	ahsta->free_logical_link_idx_map |= BIT(arsta->link_idx);
6928 
6929 	rcu_assign_pointer(ahsta->link[link_id], NULL);
6930 	synchronize_rcu();
6931 
6932 	if (arsta == &ahsta->deflink) {
6933 		arsta->link_id = ATH12K_INVALID_LINK_ID;
6934 		arsta->ahsta = NULL;
6935 		arsta->arvif = NULL;
6936 		return;
6937 	}
6938 
6939 	kfree(arsta);
6940 }
6941 
ath12k_mac_inc_num_stations(struct ath12k_link_vif * arvif,struct ath12k_link_sta * arsta)6942 static int ath12k_mac_inc_num_stations(struct ath12k_link_vif *arvif,
6943 				       struct ath12k_link_sta *arsta)
6944 {
6945 	struct ieee80211_sta *sta = ath12k_ahsta_to_sta(arsta->ahsta);
6946 	struct ath12k *ar = arvif->ar;
6947 
6948 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
6949 
6950 	if (arvif->ahvif->vdev_type == WMI_VDEV_TYPE_STA && !sta->tdls)
6951 		return 0;
6952 
6953 	if (ar->num_stations >= ar->max_num_stations)
6954 		return -ENOBUFS;
6955 
6956 	ar->num_stations++;
6957 	arvif->num_stations++;
6958 
6959 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
6960 		   "mac station %pM connected to vdev %u num_stations %u\n",
6961 		   arsta->addr, arvif->vdev_id, arvif->num_stations);
6962 
6963 	return 0;
6964 }
6965 
ath12k_mac_dec_num_stations(struct ath12k_link_vif * arvif,struct ath12k_link_sta * arsta)6966 static void ath12k_mac_dec_num_stations(struct ath12k_link_vif *arvif,
6967 					struct ath12k_link_sta *arsta)
6968 {
6969 	struct ieee80211_sta *sta = ath12k_ahsta_to_sta(arsta->ahsta);
6970 	struct ath12k *ar = arvif->ar;
6971 
6972 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
6973 
6974 	if (arvif->ahvif->vdev_type == WMI_VDEV_TYPE_STA && !sta->tdls)
6975 		return;
6976 
6977 	ar->num_stations--;
6978 
6979 	if (arvif->num_stations) {
6980 		arvif->num_stations--;
6981 		ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
6982 			   "mac station %pM disconnected from vdev %u num_stations %u\n",
6983 			   arsta->addr, arvif->vdev_id, arvif->num_stations);
6984 	} else {
6985 		ath12k_warn(ar->ab,
6986 			    "mac station %pM disconnect for vdev %u without any connected station\n",
6987 			    arsta->addr, arvif->vdev_id);
6988 	}
6989 }
6990 
ath12k_mac_station_post_remove(struct ath12k * ar,struct ath12k_link_vif * arvif,struct ath12k_link_sta * arsta)6991 static void ath12k_mac_station_post_remove(struct ath12k *ar,
6992 					   struct ath12k_link_vif *arvif,
6993 					   struct ath12k_link_sta *arsta)
6994 {
6995 	struct ieee80211_vif *vif = ath12k_ahvif_to_vif(arvif->ahvif);
6996 	struct ieee80211_sta *sta = ath12k_ahsta_to_sta(arsta->ahsta);
6997 	struct ath12k_dp_link_peer *peer;
6998 	struct ath12k_dp *dp = ath12k_ab_to_dp(ar->ab);
6999 
7000 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
7001 
7002 	ath12k_mac_dec_num_stations(arvif, arsta);
7003 
7004 	spin_lock_bh(&dp->dp_lock);
7005 
7006 	peer = ath12k_dp_link_peer_find_by_vdev_and_addr(dp, arvif->vdev_id,
7007 							 arsta->addr);
7008 	if (peer && peer->sta == sta) {
7009 		ath12k_warn(ar->ab, "Found peer entry %pM n vdev %i after it was supposedly removed\n",
7010 			    vif->addr, arvif->vdev_id);
7011 		peer->sta = NULL;
7012 
7013 		ath12k_dp_link_peer_free(peer);
7014 		ar->num_peers--;
7015 	}
7016 
7017 	spin_unlock_bh(&dp->dp_lock);
7018 }
7019 
ath12k_mac_station_unauthorize(struct ath12k * ar,struct ath12k_link_vif * arvif,struct ath12k_link_sta * arsta)7020 static int ath12k_mac_station_unauthorize(struct ath12k *ar,
7021 					  struct ath12k_link_vif *arvif,
7022 					  struct ath12k_link_sta *arsta)
7023 {
7024 	struct ath12k_dp_link_peer *peer;
7025 	int ret;
7026 	struct ath12k_dp *dp = ath12k_ab_to_dp(ar->ab);
7027 
7028 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
7029 
7030 	spin_lock_bh(&dp->dp_lock);
7031 
7032 	peer = ath12k_dp_link_peer_find_by_vdev_and_addr(dp, arvif->vdev_id,
7033 							 arsta->addr);
7034 	if (peer)
7035 		peer->is_authorized = false;
7036 
7037 	spin_unlock_bh(&dp->dp_lock);
7038 
7039 	/* Driver must clear the keys during the state change from
7040 	 * IEEE80211_STA_AUTHORIZED to IEEE80211_STA_ASSOC, since after
7041 	 * returning from here, mac80211 is going to delete the keys
7042 	 * in __sta_info_destroy_part2(). This will ensure that the driver does
7043 	 * not retain stale key references after mac80211 deletes the keys.
7044 	 */
7045 	ret = ath12k_clear_peer_keys(arvif, arsta->addr);
7046 	if (ret) {
7047 		ath12k_warn(ar->ab, "failed to clear all peer keys for vdev %i: %d\n",
7048 			    arvif->vdev_id, ret);
7049 		return ret;
7050 	}
7051 
7052 	return 0;
7053 }
7054 
ath12k_mac_station_authorize(struct ath12k * ar,struct ath12k_link_vif * arvif,struct ath12k_link_sta * arsta)7055 static int ath12k_mac_station_authorize(struct ath12k *ar,
7056 					struct ath12k_link_vif *arvif,
7057 					struct ath12k_link_sta *arsta)
7058 {
7059 	struct ath12k_dp_link_peer *peer;
7060 	struct ieee80211_vif *vif = ath12k_ahvif_to_vif(arvif->ahvif);
7061 	int ret;
7062 	struct ath12k_dp *dp = ath12k_ab_to_dp(ar->ab);
7063 
7064 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
7065 
7066 	spin_lock_bh(&dp->dp_lock);
7067 
7068 	peer = ath12k_dp_link_peer_find_by_vdev_and_addr(dp, arvif->vdev_id,
7069 							 arsta->addr);
7070 	if (peer)
7071 		peer->is_authorized = true;
7072 
7073 	spin_unlock_bh(&dp->dp_lock);
7074 
7075 	if (vif->type == NL80211_IFTYPE_STATION && arvif->is_up) {
7076 		ret = ath12k_wmi_set_peer_param(ar, arsta->addr,
7077 						arvif->vdev_id,
7078 						WMI_PEER_AUTHORIZE,
7079 						1);
7080 		if (ret) {
7081 			ath12k_warn(ar->ab, "Unable to authorize peer %pM vdev %d: %d\n",
7082 				    arsta->addr, arvif->vdev_id, ret);
7083 			return ret;
7084 		}
7085 	}
7086 
7087 	return 0;
7088 }
7089 
ath12k_mac_station_remove(struct ath12k * ar,struct ath12k_link_vif * arvif,struct ath12k_link_sta * arsta)7090 static int ath12k_mac_station_remove(struct ath12k *ar,
7091 				     struct ath12k_link_vif *arvif,
7092 				     struct ath12k_link_sta *arsta)
7093 {
7094 	struct ieee80211_sta *sta = ath12k_ahsta_to_sta(arsta->ahsta);
7095 	struct ath12k_vif *ahvif = arvif->ahvif;
7096 	int ret = 0;
7097 	struct ath12k_link_sta *temp_arsta;
7098 
7099 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
7100 
7101 	wiphy_work_cancel(ar->ah->hw->wiphy, &arsta->update_wk);
7102 
7103 	if (ahvif->vdev_type == WMI_VDEV_TYPE_STA) {
7104 		ath12k_bss_disassoc(ar, arvif);
7105 		ret = ath12k_mac_vdev_stop(arvif);
7106 		if (ret)
7107 			ath12k_warn(ar->ab, "failed to stop vdev %i: %d\n",
7108 				    arvif->vdev_id, ret);
7109 	}
7110 
7111 	if (sta->mlo)
7112 		return ret;
7113 
7114 	ath12k_dp_peer_cleanup(ar, arvif->vdev_id, arsta->addr);
7115 
7116 	ret = ath12k_peer_delete(ar, arvif->vdev_id, arsta->addr);
7117 	if (ret)
7118 		ath12k_warn(ar->ab, "Failed to delete peer: %pM for VDEV: %d\n",
7119 			    arsta->addr, arvif->vdev_id);
7120 	else
7121 		ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "Removed peer: %pM for VDEV: %d\n",
7122 			   arsta->addr, arvif->vdev_id);
7123 
7124 	ath12k_mac_station_post_remove(ar, arvif, arsta);
7125 
7126 	spin_lock_bh(&ar->ab->base_lock);
7127 
7128 	/* To handle roaming and split phy scenario */
7129 	temp_arsta = ath12k_link_sta_find_by_addr(ar->ab, arsta->addr);
7130 	if (temp_arsta && temp_arsta->arvif->ar == ar)
7131 		ath12k_link_sta_rhash_delete(ar->ab, arsta);
7132 
7133 	spin_unlock_bh(&ar->ab->base_lock);
7134 
7135 	if (sta->valid_links)
7136 		ath12k_mac_free_unassign_link_sta(ahvif->ah,
7137 						  arsta->ahsta, arsta->link_id);
7138 
7139 	return ret;
7140 }
7141 
ath12k_mac_station_add(struct ath12k * ar,struct ath12k_link_vif * arvif,struct ath12k_link_sta * arsta)7142 static int ath12k_mac_station_add(struct ath12k *ar,
7143 				  struct ath12k_link_vif *arvif,
7144 				  struct ath12k_link_sta *arsta)
7145 {
7146 	struct ath12k_base *ab = ar->ab;
7147 	struct ieee80211_vif *vif = ath12k_ahvif_to_vif(arvif->ahvif);
7148 	struct ieee80211_sta *sta = ath12k_ahsta_to_sta(arsta->ahsta);
7149 	struct ath12k_wmi_peer_create_arg peer_param = {};
7150 	int ret;
7151 	struct ath12k_link_sta *temp_arsta;
7152 
7153 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
7154 
7155 	ret = ath12k_mac_inc_num_stations(arvif, arsta);
7156 	if (ret) {
7157 		ath12k_warn(ab, "refusing to associate station: too many connected already (%d)\n",
7158 			    ar->max_num_stations);
7159 		goto exit;
7160 	}
7161 
7162 	spin_lock_bh(&ab->base_lock);
7163 
7164 	/*
7165 	 * In case of Split PHY and roaming scenario, pdev idx
7166 	 * might differ but both the pdev will share same rhash
7167 	 * table. In that case update the rhash table if link_sta is
7168 	 * already present
7169 	 */
7170 	temp_arsta = ath12k_link_sta_find_by_addr(ab, arsta->addr);
7171 	if (temp_arsta && temp_arsta->arvif->ar != ar)
7172 		ath12k_link_sta_rhash_delete(ab, temp_arsta);
7173 
7174 	ret = ath12k_link_sta_rhash_add(ab, arsta);
7175 	spin_unlock_bh(&ab->base_lock);
7176 	if (ret) {
7177 		ath12k_warn(ab, "Failed to add arsta: %pM to hash table, ret: %d",
7178 			    arsta->addr, ret);
7179 		goto dec_num_station;
7180 	}
7181 
7182 	peer_param.vdev_id = arvif->vdev_id;
7183 	peer_param.peer_addr = arsta->addr;
7184 	peer_param.peer_type = WMI_PEER_TYPE_DEFAULT;
7185 	peer_param.ml_enabled = sta->mlo;
7186 
7187 	ret = ath12k_peer_create(ar, arvif, sta, &peer_param);
7188 	if (ret) {
7189 		ath12k_warn(ab, "Failed to add peer: %pM for VDEV: %d\n",
7190 			    arsta->addr, arvif->vdev_id);
7191 		goto free_peer;
7192 	}
7193 
7194 	ath12k_dbg(ab, ATH12K_DBG_MAC, "Added peer: %pM for VDEV: %d\n",
7195 		   arsta->addr, arvif->vdev_id);
7196 
7197 	if (ieee80211_vif_is_mesh(vif)) {
7198 		ret = ath12k_wmi_set_peer_param(ar, arsta->addr,
7199 						arvif->vdev_id,
7200 						WMI_PEER_USE_4ADDR, 1);
7201 		if (ret) {
7202 			ath12k_warn(ab, "failed to STA %pM 4addr capability: %d\n",
7203 				    arsta->addr, ret);
7204 			goto free_peer;
7205 		}
7206 	}
7207 
7208 	ret = ath12k_dp_peer_setup(ar, arvif->vdev_id, arsta->addr);
7209 	if (ret) {
7210 		ath12k_warn(ab, "failed to setup dp for peer %pM on vdev %i (%d)\n",
7211 			    arsta->addr, arvif->vdev_id, ret);
7212 		goto free_peer;
7213 	}
7214 
7215 	if (ab->hw_params->vdev_start_delay &&
7216 	    !arvif->is_started &&
7217 	    arvif->ahvif->vdev_type != WMI_VDEV_TYPE_AP) {
7218 		ret = ath12k_start_vdev_delay(ar, arvif);
7219 		if (ret) {
7220 			ath12k_warn(ab, "failed to delay vdev start: %d\n", ret);
7221 			goto free_peer;
7222 		}
7223 	}
7224 
7225 	return 0;
7226 
7227 free_peer:
7228 	ath12k_peer_delete(ar, arvif->vdev_id, arsta->addr);
7229 	spin_lock_bh(&ab->base_lock);
7230 	ath12k_link_sta_rhash_delete(ab, arsta);
7231 	spin_unlock_bh(&ab->base_lock);
7232 dec_num_station:
7233 	ath12k_mac_dec_num_stations(arvif, arsta);
7234 exit:
7235 	return ret;
7236 }
7237 
ath12k_mac_assign_link_sta(struct ath12k_hw * ah,struct ath12k_sta * ahsta,struct ath12k_link_sta * arsta,struct ath12k_vif * ahvif,u8 link_id)7238 static int ath12k_mac_assign_link_sta(struct ath12k_hw *ah,
7239 				      struct ath12k_sta *ahsta,
7240 				      struct ath12k_link_sta *arsta,
7241 				      struct ath12k_vif *ahvif,
7242 				      u8 link_id)
7243 {
7244 	struct ieee80211_sta *sta = ath12k_ahsta_to_sta(ahsta);
7245 	struct ieee80211_link_sta *link_sta;
7246 	struct ath12k_link_vif *arvif;
7247 	int link_idx;
7248 
7249 	lockdep_assert_wiphy(ah->hw->wiphy);
7250 
7251 	if (!arsta || link_id >= IEEE80211_MLD_MAX_NUM_LINKS)
7252 		return -EINVAL;
7253 
7254 	arvif = wiphy_dereference(ah->hw->wiphy, ahvif->link[link_id]);
7255 	if (!arvif)
7256 		return -EINVAL;
7257 
7258 	memset(arsta, 0, sizeof(*arsta));
7259 
7260 	link_sta = wiphy_dereference(ah->hw->wiphy, sta->link[link_id]);
7261 	if (!link_sta)
7262 		return -EINVAL;
7263 
7264 	ether_addr_copy(arsta->addr, link_sta->addr);
7265 
7266 	if (!ahsta->free_logical_link_idx_map)
7267 		return -ENOSPC;
7268 
7269 	/*
7270 	 * Allocate a logical link index by selecting the first available bit
7271 	 * from the free logical index map
7272 	 */
7273 	link_idx = __ffs(ahsta->free_logical_link_idx_map);
7274 	ahsta->free_logical_link_idx_map &= ~BIT(link_idx);
7275 	arsta->link_idx = link_idx;
7276 
7277 	arsta->link_id = link_id;
7278 	ahsta->links_map |= BIT(arsta->link_id);
7279 	arsta->arvif = arvif;
7280 	arsta->ahsta = ahsta;
7281 	ahsta->ahvif = ahvif;
7282 
7283 	wiphy_work_init(&arsta->update_wk, ath12k_sta_rc_update_wk);
7284 
7285 	rcu_assign_pointer(ahsta->link[link_id], arsta);
7286 
7287 	return 0;
7288 }
7289 
ath12k_mac_ml_station_remove(struct ath12k_vif * ahvif,struct ath12k_sta * ahsta)7290 static void ath12k_mac_ml_station_remove(struct ath12k_vif *ahvif,
7291 					 struct ath12k_sta *ahsta)
7292 {
7293 	struct ieee80211_sta *sta = ath12k_ahsta_to_sta(ahsta);
7294 	struct ath12k_hw *ah = ahvif->ah;
7295 	struct ath12k_link_vif *arvif;
7296 	struct ath12k_link_sta *arsta;
7297 	unsigned long links;
7298 	struct ath12k *ar;
7299 	u8 link_id;
7300 
7301 	lockdep_assert_wiphy(ah->hw->wiphy);
7302 
7303 	ath12k_peer_mlo_link_peers_delete(ahvif, ahsta);
7304 
7305 	/* validate link station removal and clear arsta links */
7306 	links = ahsta->links_map;
7307 	for_each_set_bit(link_id, &links, IEEE80211_MLD_MAX_NUM_LINKS) {
7308 		arvif = wiphy_dereference(ah->hw->wiphy, ahvif->link[link_id]);
7309 		arsta = wiphy_dereference(ah->hw->wiphy, ahsta->link[link_id]);
7310 		if (!arvif || !arsta)
7311 			continue;
7312 
7313 		ar = arvif->ar;
7314 
7315 		ath12k_mac_station_post_remove(ar, arvif, arsta);
7316 
7317 		spin_lock_bh(&ar->ab->base_lock);
7318 		ath12k_link_sta_rhash_delete(ar->ab, arsta);
7319 		spin_unlock_bh(&ar->ab->base_lock);
7320 
7321 		ath12k_mac_free_unassign_link_sta(ah, ahsta, link_id);
7322 	}
7323 
7324 	if (sta->mlo)
7325 		ath12k_peer_ml_free(ah, ahsta);
7326 }
7327 
ath12k_mac_handle_link_sta_state(struct ieee80211_hw * hw,struct ath12k_link_vif * arvif,struct ath12k_link_sta * arsta,enum ieee80211_sta_state old_state,enum ieee80211_sta_state new_state)7328 static int ath12k_mac_handle_link_sta_state(struct ieee80211_hw *hw,
7329 					    struct ath12k_link_vif *arvif,
7330 					    struct ath12k_link_sta *arsta,
7331 					    enum ieee80211_sta_state old_state,
7332 					    enum ieee80211_sta_state new_state)
7333 {
7334 	struct ieee80211_vif *vif = ath12k_ahvif_to_vif(arvif->ahvif);
7335 	struct ieee80211_bss_conf *link_conf;
7336 	struct ath12k *ar = arvif->ar;
7337 	struct ath12k_reg_info *reg_info;
7338 	struct ath12k_base *ab = ar->ab;
7339 	int ret = 0;
7340 
7341 	lockdep_assert_wiphy(hw->wiphy);
7342 
7343 	ath12k_dbg(ab, ATH12K_DBG_MAC, "mac handle link %u sta %pM state %d -> %d\n",
7344 		   arsta->link_id, arsta->addr, old_state, new_state);
7345 
7346 	/* IEEE80211_STA_NONE -> IEEE80211_STA_NOTEXIST: Remove the station
7347 	 * from driver
7348 	 */
7349 	if ((old_state == IEEE80211_STA_NONE &&
7350 	     new_state == IEEE80211_STA_NOTEXIST)) {
7351 		ret = ath12k_mac_station_remove(ar, arvif, arsta);
7352 		if (ret) {
7353 			ath12k_warn(ab, "Failed to remove station: %pM for VDEV: %d\n",
7354 				    arsta->addr, arvif->vdev_id);
7355 			goto exit;
7356 		}
7357 	}
7358 
7359 	/* IEEE80211_STA_NOTEXIST -> IEEE80211_STA_NONE: Add new station to driver */
7360 	if (old_state == IEEE80211_STA_NOTEXIST &&
7361 	    new_state == IEEE80211_STA_NONE) {
7362 		ret = ath12k_mac_station_add(ar, arvif, arsta);
7363 		if (ret)
7364 			ath12k_warn(ab, "Failed to add station: %pM for VDEV: %d\n",
7365 				    arsta->addr, arvif->vdev_id);
7366 
7367 	/* IEEE80211_STA_AUTH -> IEEE80211_STA_ASSOC: Send station assoc command for
7368 	 * peer associated to AP/Mesh/ADHOC vif type.
7369 	 */
7370 	} else if (old_state == IEEE80211_STA_AUTH &&
7371 		   new_state == IEEE80211_STA_ASSOC &&
7372 		   (vif->type == NL80211_IFTYPE_AP ||
7373 		    vif->type == NL80211_IFTYPE_MESH_POINT ||
7374 		    vif->type == NL80211_IFTYPE_ADHOC)) {
7375 		ret = ath12k_mac_station_assoc(ar, arvif, arsta, false);
7376 		if (ret)
7377 			ath12k_warn(ab, "Failed to associate station: %pM\n",
7378 				    arsta->addr);
7379 
7380 	/* IEEE80211_STA_ASSOC -> IEEE80211_STA_AUTHORIZED: set peer status as
7381 	 * authorized
7382 	 */
7383 	} else if (old_state == IEEE80211_STA_ASSOC &&
7384 		   new_state == IEEE80211_STA_AUTHORIZED) {
7385 		ret = ath12k_mac_station_authorize(ar, arvif, arsta);
7386 		if (ret) {
7387 			ath12k_warn(ab, "Failed to authorize station: %pM\n",
7388 				    arsta->addr);
7389 			goto exit;
7390 		}
7391 
7392 		if (ath12k_wmi_supports_6ghz_cc_ext(ar) &&
7393 		    arvif->ahvif->vdev_type == WMI_VDEV_TYPE_STA) {
7394 			link_conf = ath12k_mac_get_link_bss_conf(arvif);
7395 			reg_info = ab->reg_info[ar->pdev_idx];
7396 			ath12k_dbg(ab, ATH12K_DBG_MAC, "connection done, update reg rules\n");
7397 			ath12k_hw_to_ah(hw)->regd_updated = false;
7398 			ath12k_reg_handle_chan_list(ab, reg_info, arvif->ahvif->vdev_type,
7399 						    link_conf->power_type);
7400 		}
7401 
7402 	/* IEEE80211_STA_AUTHORIZED -> IEEE80211_STA_ASSOC: station may be in removal,
7403 	 * deauthorize it.
7404 	 */
7405 	} else if (old_state == IEEE80211_STA_AUTHORIZED &&
7406 		   new_state == IEEE80211_STA_ASSOC) {
7407 		ath12k_mac_station_unauthorize(ar, arvif, arsta);
7408 
7409 	/* IEEE80211_STA_ASSOC -> IEEE80211_STA_AUTH: disassoc peer connected to
7410 	 * AP/mesh/ADHOC vif type.
7411 	 */
7412 	} else if (old_state == IEEE80211_STA_ASSOC &&
7413 		   new_state == IEEE80211_STA_AUTH &&
7414 		   (vif->type == NL80211_IFTYPE_AP ||
7415 		    vif->type == NL80211_IFTYPE_MESH_POINT ||
7416 		    vif->type == NL80211_IFTYPE_ADHOC)) {
7417 		ret = ath12k_mac_station_disassoc(ar, arvif, arsta);
7418 		if (ret)
7419 			ath12k_warn(ab, "Failed to disassociate station: %pM\n",
7420 				    arsta->addr);
7421 	}
7422 
7423 exit:
7424 	return ret;
7425 }
7426 
ath12k_mac_is_freq_on_mac(struct ath12k_hw_mode_freq_range_arg * freq_range,u32 freq,u8 mac_id)7427 static bool ath12k_mac_is_freq_on_mac(struct ath12k_hw_mode_freq_range_arg *freq_range,
7428 				      u32 freq, u8 mac_id)
7429 {
7430 	return (freq >= freq_range[mac_id].low_2ghz_freq &&
7431 		freq <= freq_range[mac_id].high_2ghz_freq) ||
7432 	       (freq >= freq_range[mac_id].low_5ghz_freq &&
7433 		freq <= freq_range[mac_id].high_5ghz_freq);
7434 }
7435 
7436 static bool
ath12k_mac_2_freq_same_mac_in_freq_range(struct ath12k_base * ab,struct ath12k_hw_mode_freq_range_arg * freq_range,u32 freq_link1,u32 freq_link2)7437 ath12k_mac_2_freq_same_mac_in_freq_range(struct ath12k_base *ab,
7438 					 struct ath12k_hw_mode_freq_range_arg *freq_range,
7439 					 u32 freq_link1, u32 freq_link2)
7440 {
7441 	u8 i;
7442 
7443 	for (i = 0; i < MAX_RADIOS; i++) {
7444 		if (ath12k_mac_is_freq_on_mac(freq_range, freq_link1, i) &&
7445 		    ath12k_mac_is_freq_on_mac(freq_range, freq_link2, i))
7446 			return true;
7447 	}
7448 
7449 	return false;
7450 }
7451 
ath12k_mac_is_hw_dbs_capable(struct ath12k_base * ab)7452 static bool ath12k_mac_is_hw_dbs_capable(struct ath12k_base *ab)
7453 {
7454 	return test_bit(WMI_TLV_SERVICE_DUAL_BAND_SIMULTANEOUS_SUPPORT,
7455 			ab->wmi_ab.svc_map) &&
7456 	       ab->wmi_ab.hw_mode_info.support_dbs;
7457 }
7458 
ath12k_mac_2_freq_same_mac_in_dbs(struct ath12k_base * ab,u32 freq_link1,u32 freq_link2)7459 static bool ath12k_mac_2_freq_same_mac_in_dbs(struct ath12k_base *ab,
7460 					      u32 freq_link1, u32 freq_link2)
7461 {
7462 	struct ath12k_hw_mode_freq_range_arg *freq_range;
7463 
7464 	if (!ath12k_mac_is_hw_dbs_capable(ab))
7465 		return true;
7466 
7467 	freq_range = ab->wmi_ab.hw_mode_info.freq_range_caps[ATH12K_HW_MODE_DBS];
7468 	return ath12k_mac_2_freq_same_mac_in_freq_range(ab, freq_range,
7469 							freq_link1, freq_link2);
7470 }
7471 
ath12k_mac_is_hw_sbs_capable(struct ath12k_base * ab)7472 static bool ath12k_mac_is_hw_sbs_capable(struct ath12k_base *ab)
7473 {
7474 	return test_bit(WMI_TLV_SERVICE_DUAL_BAND_SIMULTANEOUS_SUPPORT,
7475 			ab->wmi_ab.svc_map) &&
7476 	       ab->wmi_ab.hw_mode_info.support_sbs;
7477 }
7478 
ath12k_mac_2_freq_same_mac_in_sbs(struct ath12k_base * ab,u32 freq_link1,u32 freq_link2)7479 static bool ath12k_mac_2_freq_same_mac_in_sbs(struct ath12k_base *ab,
7480 					      u32 freq_link1, u32 freq_link2)
7481 {
7482 	struct ath12k_hw_mode_info *info = &ab->wmi_ab.hw_mode_info;
7483 	struct ath12k_hw_mode_freq_range_arg *sbs_uppr_share;
7484 	struct ath12k_hw_mode_freq_range_arg *sbs_low_share;
7485 	struct ath12k_hw_mode_freq_range_arg *sbs_range;
7486 
7487 	if (!ath12k_mac_is_hw_sbs_capable(ab))
7488 		return true;
7489 
7490 	if (ab->wmi_ab.sbs_lower_band_end_freq) {
7491 		sbs_uppr_share = info->freq_range_caps[ATH12K_HW_MODE_SBS_UPPER_SHARE];
7492 		sbs_low_share = info->freq_range_caps[ATH12K_HW_MODE_SBS_LOWER_SHARE];
7493 
7494 		return ath12k_mac_2_freq_same_mac_in_freq_range(ab, sbs_low_share,
7495 								freq_link1, freq_link2) ||
7496 		       ath12k_mac_2_freq_same_mac_in_freq_range(ab, sbs_uppr_share,
7497 								freq_link1, freq_link2);
7498 	}
7499 
7500 	sbs_range = info->freq_range_caps[ATH12K_HW_MODE_SBS];
7501 	return ath12k_mac_2_freq_same_mac_in_freq_range(ab, sbs_range,
7502 							freq_link1, freq_link2);
7503 }
7504 
ath12k_mac_freqs_on_same_mac(struct ath12k_base * ab,u32 freq_link1,u32 freq_link2)7505 static bool ath12k_mac_freqs_on_same_mac(struct ath12k_base *ab,
7506 					 u32 freq_link1, u32 freq_link2)
7507 {
7508 	return ath12k_mac_2_freq_same_mac_in_dbs(ab, freq_link1, freq_link2) &&
7509 	       ath12k_mac_2_freq_same_mac_in_sbs(ab, freq_link1, freq_link2);
7510 }
7511 
ath12k_mac_mlo_sta_set_link_active(struct ath12k_base * ab,enum wmi_mlo_link_force_reason reason,enum wmi_mlo_link_force_mode mode,u8 * mlo_vdev_id_lst,u8 num_mlo_vdev,u8 * mlo_inactive_vdev_lst,u8 num_mlo_inactive_vdev)7512 static int ath12k_mac_mlo_sta_set_link_active(struct ath12k_base *ab,
7513 					      enum wmi_mlo_link_force_reason reason,
7514 					      enum wmi_mlo_link_force_mode mode,
7515 					      u8 *mlo_vdev_id_lst,
7516 					      u8 num_mlo_vdev,
7517 					      u8 *mlo_inactive_vdev_lst,
7518 					      u8 num_mlo_inactive_vdev)
7519 {
7520 	struct wmi_mlo_link_set_active_arg param = {};
7521 	u32 entry_idx, entry_offset, vdev_idx;
7522 	u8 vdev_id;
7523 
7524 	param.reason = reason;
7525 	param.force_mode = mode;
7526 
7527 	for (vdev_idx = 0; vdev_idx < num_mlo_vdev; vdev_idx++) {
7528 		vdev_id = mlo_vdev_id_lst[vdev_idx];
7529 		entry_idx = vdev_id / 32;
7530 		entry_offset = vdev_id % 32;
7531 		if (entry_idx >= WMI_MLO_LINK_NUM_SZ) {
7532 			ath12k_warn(ab, "Invalid entry_idx %d num_mlo_vdev %d vdev %d",
7533 				    entry_idx, num_mlo_vdev, vdev_id);
7534 			return -EINVAL;
7535 		}
7536 		param.vdev_bitmap[entry_idx] |= 1 << entry_offset;
7537 		/* update entry number if entry index changed */
7538 		if (param.num_vdev_bitmap < entry_idx + 1)
7539 			param.num_vdev_bitmap = entry_idx + 1;
7540 	}
7541 
7542 	ath12k_dbg(ab, ATH12K_DBG_MAC,
7543 		   "num_vdev_bitmap %d vdev_bitmap[0] = 0x%x, vdev_bitmap[1] = 0x%x",
7544 		   param.num_vdev_bitmap, param.vdev_bitmap[0], param.vdev_bitmap[1]);
7545 
7546 	if (mode == WMI_MLO_LINK_FORCE_MODE_ACTIVE_INACTIVE) {
7547 		for (vdev_idx = 0; vdev_idx < num_mlo_inactive_vdev; vdev_idx++) {
7548 			vdev_id = mlo_inactive_vdev_lst[vdev_idx];
7549 			entry_idx = vdev_id / 32;
7550 			entry_offset = vdev_id % 32;
7551 			if (entry_idx >= WMI_MLO_LINK_NUM_SZ) {
7552 				ath12k_warn(ab, "Invalid entry_idx %d num_mlo_vdev %d vdev %d",
7553 					    entry_idx, num_mlo_inactive_vdev, vdev_id);
7554 				return -EINVAL;
7555 			}
7556 			param.inactive_vdev_bitmap[entry_idx] |= 1 << entry_offset;
7557 			/* update entry number if entry index changed */
7558 			if (param.num_inactive_vdev_bitmap < entry_idx + 1)
7559 				param.num_inactive_vdev_bitmap = entry_idx + 1;
7560 		}
7561 
7562 		ath12k_dbg(ab, ATH12K_DBG_MAC,
7563 			   "num_vdev_bitmap %d inactive_vdev_bitmap[0] = 0x%x, inactive_vdev_bitmap[1] = 0x%x",
7564 			   param.num_inactive_vdev_bitmap,
7565 			   param.inactive_vdev_bitmap[0],
7566 			   param.inactive_vdev_bitmap[1]);
7567 	}
7568 
7569 	if (mode == WMI_MLO_LINK_FORCE_MODE_ACTIVE_LINK_NUM ||
7570 	    mode == WMI_MLO_LINK_FORCE_MODE_INACTIVE_LINK_NUM) {
7571 		param.num_link_entry = 1;
7572 		param.link_num[0].num_of_link = num_mlo_vdev - 1;
7573 	}
7574 
7575 	return ath12k_wmi_send_mlo_link_set_active_cmd(ab, &param);
7576 }
7577 
ath12k_mac_mlo_sta_update_link_active(struct ath12k_base * ab,struct ieee80211_hw * hw,struct ath12k_vif * ahvif)7578 static int ath12k_mac_mlo_sta_update_link_active(struct ath12k_base *ab,
7579 						 struct ieee80211_hw *hw,
7580 						 struct ath12k_vif *ahvif)
7581 {
7582 	u8 mlo_vdev_id_lst[IEEE80211_MLD_MAX_NUM_LINKS] = {};
7583 	u32 mlo_freq_list[IEEE80211_MLD_MAX_NUM_LINKS] = {};
7584 	unsigned long links = ahvif->links_map;
7585 	enum wmi_mlo_link_force_reason reason;
7586 	struct ieee80211_chanctx_conf *conf;
7587 	enum wmi_mlo_link_force_mode mode;
7588 	struct ieee80211_bss_conf *info;
7589 	struct ath12k_link_vif *arvif;
7590 	u8 num_mlo_vdev = 0;
7591 	u8 link_id;
7592 
7593 	for_each_set_bit(link_id, &links, IEEE80211_MLD_MAX_NUM_LINKS) {
7594 		arvif = wiphy_dereference(hw->wiphy, ahvif->link[link_id]);
7595 		/* make sure vdev is created on this device */
7596 		if (!arvif || !arvif->is_created || arvif->ar->ab != ab)
7597 			continue;
7598 
7599 		info = ath12k_mac_get_link_bss_conf(arvif);
7600 		conf = wiphy_dereference(hw->wiphy, info->chanctx_conf);
7601 		mlo_freq_list[num_mlo_vdev] = conf->def.chan->center_freq;
7602 
7603 		mlo_vdev_id_lst[num_mlo_vdev] = arvif->vdev_id;
7604 		num_mlo_vdev++;
7605 	}
7606 
7607 	/* It is not allowed to activate more links than a single device
7608 	 * supported. Something goes wrong if we reach here.
7609 	 */
7610 	if (num_mlo_vdev > ATH12K_NUM_MAX_ACTIVE_LINKS_PER_DEVICE) {
7611 		WARN_ON_ONCE(1);
7612 		return -EINVAL;
7613 	}
7614 
7615 	/* if 2 links are established and both link channels fall on the
7616 	 * same hardware MAC, send command to firmware to deactivate one
7617 	 * of them.
7618 	 */
7619 	if (num_mlo_vdev == 2 &&
7620 	    ath12k_mac_freqs_on_same_mac(ab, mlo_freq_list[0],
7621 					 mlo_freq_list[1])) {
7622 		mode = WMI_MLO_LINK_FORCE_MODE_INACTIVE_LINK_NUM;
7623 		reason = WMI_MLO_LINK_FORCE_REASON_NEW_CONNECT;
7624 		return ath12k_mac_mlo_sta_set_link_active(ab, reason, mode,
7625 							  mlo_vdev_id_lst, num_mlo_vdev,
7626 							  NULL, 0);
7627 	}
7628 
7629 	return 0;
7630 }
7631 
ath12k_mac_are_sbs_chan(struct ath12k_base * ab,u32 freq_1,u32 freq_2)7632 static bool ath12k_mac_are_sbs_chan(struct ath12k_base *ab, u32 freq_1, u32 freq_2)
7633 {
7634 	if (!ath12k_mac_is_hw_sbs_capable(ab))
7635 		return false;
7636 
7637 	if (ath12k_is_2ghz_channel_freq(freq_1) ||
7638 	    ath12k_is_2ghz_channel_freq(freq_2))
7639 		return false;
7640 
7641 	return !ath12k_mac_2_freq_same_mac_in_sbs(ab, freq_1, freq_2);
7642 }
7643 
ath12k_mac_are_dbs_chan(struct ath12k_base * ab,u32 freq_1,u32 freq_2)7644 static bool ath12k_mac_are_dbs_chan(struct ath12k_base *ab, u32 freq_1, u32 freq_2)
7645 {
7646 	if (!ath12k_mac_is_hw_dbs_capable(ab))
7647 		return false;
7648 
7649 	return !ath12k_mac_2_freq_same_mac_in_dbs(ab, freq_1, freq_2);
7650 }
7651 
ath12k_mac_select_links(struct ath12k_base * ab,struct ieee80211_vif * vif,struct ieee80211_hw * hw,u16 * selected_links)7652 static int ath12k_mac_select_links(struct ath12k_base *ab,
7653 				   struct ieee80211_vif *vif,
7654 				   struct ieee80211_hw *hw,
7655 				   u16 *selected_links)
7656 {
7657 	unsigned long useful_links = ieee80211_vif_usable_links(vif);
7658 	struct ath12k_vif *ahvif = ath12k_vif_to_ahvif(vif);
7659 	u8 num_useful_links = hweight_long(useful_links);
7660 	struct ieee80211_chanctx_conf *chanctx;
7661 	struct ath12k_link_vif *assoc_arvif;
7662 	u32 assoc_link_freq, partner_freq;
7663 	u16 sbs_links = 0, dbs_links = 0;
7664 	struct ieee80211_bss_conf *info;
7665 	struct ieee80211_channel *chan;
7666 	struct ieee80211_sta *sta;
7667 	struct ath12k_sta *ahsta;
7668 	u8 link_id;
7669 
7670 	/* activate all useful links if less than max supported */
7671 	if (num_useful_links <= ATH12K_NUM_MAX_ACTIVE_LINKS_PER_DEVICE) {
7672 		*selected_links = useful_links;
7673 		return 0;
7674 	}
7675 
7676 	/* only in station mode we can get here, so it's safe
7677 	 * to use ap_addr
7678 	 */
7679 	rcu_read_lock();
7680 	sta = ieee80211_find_sta(vif, vif->cfg.ap_addr);
7681 	if (!sta) {
7682 		rcu_read_unlock();
7683 		ath12k_warn(ab, "failed to find sta with addr %pM\n", vif->cfg.ap_addr);
7684 		return -EINVAL;
7685 	}
7686 
7687 	ahsta = ath12k_sta_to_ahsta(sta);
7688 	assoc_arvif = wiphy_dereference(hw->wiphy, ahvif->link[ahsta->assoc_link_id]);
7689 	info = ath12k_mac_get_link_bss_conf(assoc_arvif);
7690 	chanctx = rcu_dereference(info->chanctx_conf);
7691 	assoc_link_freq = chanctx->def.chan->center_freq;
7692 	rcu_read_unlock();
7693 	ath12k_dbg(ab, ATH12K_DBG_MAC, "assoc link %u freq %u\n",
7694 		   assoc_arvif->link_id, assoc_link_freq);
7695 
7696 	/* assoc link is already activated and has to be kept active,
7697 	 * only need to select a partner link from others.
7698 	 */
7699 	useful_links &= ~BIT(assoc_arvif->link_id);
7700 	for_each_set_bit(link_id, &useful_links, IEEE80211_MLD_MAX_NUM_LINKS) {
7701 		info = wiphy_dereference(hw->wiphy, vif->link_conf[link_id]);
7702 		if (!info) {
7703 			ath12k_warn(ab, "failed to get link info for link: %u\n",
7704 				    link_id);
7705 			return -ENOLINK;
7706 		}
7707 
7708 		chan = info->chanreq.oper.chan;
7709 		if (!chan) {
7710 			ath12k_warn(ab, "failed to get chan for link: %u\n", link_id);
7711 			return -EINVAL;
7712 		}
7713 
7714 		partner_freq = chan->center_freq;
7715 		if (ath12k_mac_are_sbs_chan(ab, assoc_link_freq, partner_freq)) {
7716 			sbs_links |= BIT(link_id);
7717 			ath12k_dbg(ab, ATH12K_DBG_MAC, "new SBS link %u freq %u\n",
7718 				   link_id, partner_freq);
7719 			continue;
7720 		}
7721 
7722 		if (ath12k_mac_are_dbs_chan(ab, assoc_link_freq, partner_freq)) {
7723 			dbs_links |= BIT(link_id);
7724 			ath12k_dbg(ab, ATH12K_DBG_MAC, "new DBS link %u freq %u\n",
7725 				   link_id, partner_freq);
7726 			continue;
7727 		}
7728 
7729 		ath12k_dbg(ab, ATH12K_DBG_MAC, "non DBS/SBS link %u freq %u\n",
7730 			   link_id, partner_freq);
7731 	}
7732 
7733 	/* choose the first candidate no matter how many is in the list */
7734 	if (sbs_links)
7735 		link_id = __ffs(sbs_links);
7736 	else if (dbs_links)
7737 		link_id = __ffs(dbs_links);
7738 	else
7739 		link_id = ffs(useful_links) - 1;
7740 
7741 	ath12k_dbg(ab, ATH12K_DBG_MAC, "select partner link %u\n", link_id);
7742 
7743 	*selected_links = BIT(assoc_arvif->link_id) | BIT(link_id);
7744 
7745 	return 0;
7746 }
7747 
ath12k_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)7748 int ath12k_mac_op_sta_state(struct ieee80211_hw *hw,
7749 			    struct ieee80211_vif *vif,
7750 			    struct ieee80211_sta *sta,
7751 			    enum ieee80211_sta_state old_state,
7752 			    enum ieee80211_sta_state new_state)
7753 {
7754 	struct ath12k_vif *ahvif = ath12k_vif_to_ahvif(vif);
7755 	struct ath12k_sta *ahsta = ath12k_sta_to_ahsta(sta);
7756 	struct ath12k_hw *ah = ath12k_hw_to_ah(hw);
7757 	struct ath12k_base *prev_ab = NULL, *ab;
7758 	struct ath12k_link_vif *arvif;
7759 	struct ath12k_link_sta *arsta;
7760 	unsigned long valid_links;
7761 	u16 selected_links = 0;
7762 	u8 link_id = 0, i;
7763 	struct ath12k *ar;
7764 	int ret = -EINVAL;
7765 	struct ath12k_dp_peer_create_params dp_params = {};
7766 
7767 	lockdep_assert_wiphy(hw->wiphy);
7768 
7769 	if (ieee80211_vif_is_mld(vif) && sta->valid_links) {
7770 		WARN_ON(!sta->mlo && hweight16(sta->valid_links) != 1);
7771 		link_id = ffs(sta->valid_links) - 1;
7772 	}
7773 
7774 	/* IEEE80211_STA_NOTEXIST -> IEEE80211_STA_NONE:
7775 	 * New station add received. If this is a ML station then
7776 	 * ahsta->links_map will be zero and sta->valid_links will be 1.
7777 	 * Assign default link to the first link sta.
7778 	 */
7779 	if (old_state == IEEE80211_STA_NOTEXIST &&
7780 	    new_state == IEEE80211_STA_NONE) {
7781 		memset(ahsta, 0, sizeof(*ahsta));
7782 		ahsta->free_logical_link_idx_map = U16_MAX;
7783 
7784 		arsta = &ahsta->deflink;
7785 
7786 		/* ML sta */
7787 		if (sta->mlo && !ahsta->links_map &&
7788 		    (hweight16(sta->valid_links) == 1)) {
7789 			if (ah->host_alloc_ml_id) {
7790 				ahsta->ml_peer_id = ath12k_peer_ml_alloc(ah);
7791 				if (ahsta->ml_peer_id == ATH12K_MLO_PEER_ID_INVALID) {
7792 					ath12k_hw_warn(ah, "unable to allocate ML peer id for sta %pM",
7793 						       sta->addr);
7794 					goto exit;
7795 				}
7796 			} else {
7797 				/*
7798 				 * firmware allocates the ML peer ID and notifies
7799 				 * the host via HTT_T2H_MSG_TYPE_MLO_RX_PEER_MAP
7800 				 */
7801 				ahsta->ml_peer_id = ATH12K_MLO_PEER_ID_PENDING;
7802 			}
7803 
7804 			dp_params.is_mlo = true;
7805 			dp_params.peer_id = ahsta->ml_peer_id;
7806 		}
7807 
7808 		dp_params.sta = sta;
7809 
7810 		if (vif->type == NL80211_IFTYPE_AP)
7811 			dp_params.ucast_ra_only = true;
7812 
7813 		ret = ath12k_dp_peer_create(&ah->dp_hw, sta->addr, &dp_params);
7814 		if (ret) {
7815 			ath12k_hw_warn(ah, "unable to create ath12k_dp_peer for sta %pM, ret: %d",
7816 				       sta->addr, ret);
7817 
7818 			goto ml_peer_id_clear;
7819 		}
7820 
7821 		ret = ath12k_mac_assign_link_sta(ah, ahsta, arsta, ahvif,
7822 						 link_id);
7823 		if (ret) {
7824 			ath12k_hw_warn(ah, "unable assign link %d for sta %pM",
7825 				       link_id, sta->addr);
7826 			goto peer_delete;
7827 		}
7828 
7829 		/* above arsta will get memset, hence do this after assign
7830 		 * link sta
7831 		 */
7832 		if (sta->mlo) {
7833 			/* For station mode, arvif->is_sta_assoc_link has been set when
7834 			 * vdev starts. Make sure the arvif/arsta pair have same setting
7835 			 */
7836 			if (vif->type == NL80211_IFTYPE_STATION &&
7837 			    !arsta->arvif->is_sta_assoc_link) {
7838 				ath12k_hw_warn(ah, "failed to verify assoc link setting with link id %u\n",
7839 					       link_id);
7840 				ret = -EINVAL;
7841 				goto exit;
7842 			}
7843 
7844 			arsta->is_assoc_link = true;
7845 			ahsta->assoc_link_id = link_id;
7846 		}
7847 	}
7848 
7849 	/* In the ML station scenario, activate all partner links once the
7850 	 * client is transitioning to the associated state.
7851 	 *
7852 	 * FIXME: Ideally, this activation should occur when the client
7853 	 * transitions to the authorized state. However, there are some
7854 	 * issues with handling this in the firmware. Until the firmware
7855 	 * can manage it properly, activate the links when the client is
7856 	 * about to move to the associated state.
7857 	 */
7858 	if (ieee80211_vif_is_mld(vif) && vif->type == NL80211_IFTYPE_STATION &&
7859 	    old_state == IEEE80211_STA_AUTH && new_state == IEEE80211_STA_ASSOC) {
7860 		/* TODO: for now only do link selection for single device
7861 		 * MLO case. Other cases would be handled in the future.
7862 		 */
7863 		ab = ah->radio[0].ab;
7864 		if (ab->ag->num_devices == 1) {
7865 			ret = ath12k_mac_select_links(ab, vif, hw, &selected_links);
7866 			if (ret) {
7867 				ath12k_warn(ab,
7868 					    "failed to get selected links: %d\n", ret);
7869 				goto exit;
7870 			}
7871 		} else {
7872 			selected_links = ieee80211_vif_usable_links(vif);
7873 		}
7874 
7875 		ieee80211_set_active_links(vif, selected_links);
7876 	}
7877 
7878 	/* Handle all the other state transitions in generic way */
7879 	valid_links = ahsta->links_map;
7880 	for_each_set_bit(link_id, &valid_links, IEEE80211_MLD_MAX_NUM_LINKS) {
7881 		arvif = wiphy_dereference(hw->wiphy, ahvif->link[link_id]);
7882 		arsta = wiphy_dereference(hw->wiphy, ahsta->link[link_id]);
7883 		/* some assumptions went wrong! */
7884 		if (WARN_ON(!arvif || !arsta))
7885 			continue;
7886 
7887 		/* vdev might be in deleted */
7888 		if (WARN_ON(!arvif->ar))
7889 			continue;
7890 
7891 		ret = ath12k_mac_handle_link_sta_state(hw, arvif, arsta,
7892 						       old_state, new_state);
7893 		if (ret) {
7894 			ath12k_hw_warn(ah, "unable to move link sta %d of sta %pM from state %d to %d",
7895 				       link_id, arsta->addr, old_state, new_state);
7896 
7897 			if (old_state == IEEE80211_STA_NOTEXIST &&
7898 			    new_state == IEEE80211_STA_NONE)
7899 				goto peer_delete;
7900 			else
7901 				goto exit;
7902 		}
7903 	}
7904 
7905 	if (ieee80211_vif_is_mld(vif) && vif->type == NL80211_IFTYPE_STATION &&
7906 	    old_state == IEEE80211_STA_ASSOC && new_state == IEEE80211_STA_AUTHORIZED) {
7907 		for_each_ar(ah, ar, i) {
7908 			ab = ar->ab;
7909 			if (prev_ab == ab)
7910 				continue;
7911 
7912 			ret = ath12k_mac_mlo_sta_update_link_active(ab, hw, ahvif);
7913 			if (ret) {
7914 				ath12k_warn(ab,
7915 					    "failed to update link active state on connect %d\n",
7916 					    ret);
7917 				goto exit;
7918 			}
7919 
7920 			prev_ab = ab;
7921 		}
7922 	}
7923 	/* IEEE80211_STA_NONE -> IEEE80211_STA_NOTEXIST:
7924 	 * Remove the station from driver (handle ML sta here since that
7925 	 * needs special handling. Normal sta will be handled in generic
7926 	 * handler below
7927 	 */
7928 	if (old_state == IEEE80211_STA_NONE &&
7929 	    new_state == IEEE80211_STA_NOTEXIST) {
7930 		if (sta->mlo)
7931 			ath12k_mac_ml_station_remove(ahvif, ahsta);
7932 
7933 		ath12k_dp_peer_delete(&ah->dp_hw, sta->addr, sta);
7934 	}
7935 
7936 	ret = 0;
7937 	goto exit;
7938 
7939 peer_delete:
7940 	ath12k_dp_peer_delete(&ah->dp_hw, sta->addr, sta);
7941 ml_peer_id_clear:
7942 	if (sta->mlo)
7943 		ath12k_peer_ml_free(ah, ahsta);
7944 exit:
7945 	/* update the state if everything went well */
7946 	if (!ret)
7947 		ahsta->state = new_state;
7948 
7949 	return ret;
7950 }
7951 EXPORT_SYMBOL(ath12k_mac_op_sta_state);
7952 
ath12k_mac_op_sta_set_txpwr(struct ieee80211_hw * hw,struct ieee80211_vif * vif,struct ieee80211_sta * sta)7953 int ath12k_mac_op_sta_set_txpwr(struct ieee80211_hw *hw,
7954 				struct ieee80211_vif *vif,
7955 				struct ieee80211_sta *sta)
7956 {
7957 	struct ath12k_sta *ahsta = ath12k_sta_to_ahsta(sta);
7958 	struct ath12k *ar;
7959 	struct ath12k_vif *ahvif = ath12k_vif_to_ahvif(vif);
7960 	struct ath12k_link_vif *arvif;
7961 	struct ath12k_link_sta *arsta;
7962 	u8 link_id;
7963 	int ret;
7964 	s16 txpwr;
7965 
7966 	lockdep_assert_wiphy(hw->wiphy);
7967 
7968 	/* TODO: use link id from mac80211 once that's implemented */
7969 	link_id = 0;
7970 
7971 	arvif = wiphy_dereference(hw->wiphy, ahvif->link[link_id]);
7972 	arsta = wiphy_dereference(hw->wiphy, ahsta->link[link_id]);
7973 
7974 	if (sta->deflink.txpwr.type == NL80211_TX_POWER_AUTOMATIC) {
7975 		txpwr = 0;
7976 	} else {
7977 		txpwr = sta->deflink.txpwr.power;
7978 		if (!txpwr) {
7979 			ret = -EINVAL;
7980 			goto out;
7981 		}
7982 	}
7983 
7984 	if (txpwr > ATH12K_TX_POWER_MAX_VAL || txpwr < ATH12K_TX_POWER_MIN_VAL) {
7985 		ret = -EINVAL;
7986 		goto out;
7987 	}
7988 
7989 	ar = arvif->ar;
7990 
7991 	ret = ath12k_wmi_set_peer_param(ar, arsta->addr, arvif->vdev_id,
7992 					WMI_PEER_USE_FIXED_PWR, txpwr);
7993 	if (ret) {
7994 		ath12k_warn(ar->ab, "failed to set tx power for station ret: %d\n",
7995 			    ret);
7996 		goto out;
7997 	}
7998 
7999 out:
8000 	return ret;
8001 }
8002 EXPORT_SYMBOL(ath12k_mac_op_sta_set_txpwr);
8003 
ath12k_mac_op_sta_set_4addr(struct ieee80211_hw * hw,struct ieee80211_vif * vif,struct ieee80211_sta * sta,bool enabled)8004 void ath12k_mac_op_sta_set_4addr(struct ieee80211_hw *hw,
8005 				 struct ieee80211_vif *vif,
8006 				 struct ieee80211_sta *sta, bool enabled)
8007 {
8008 	struct ath12k_sta *ahsta = ath12k_sta_to_ahsta(sta);
8009 
8010 	lockdep_assert_wiphy(hw->wiphy);
8011 
8012 	/*
8013 	 * 4-address mode disabled option is available only for station
8014 	 * interface from mac80211, and we have wds_vdev_param for station
8015 	 * interface and target will not allow to disable the wds_vdev_param
8016 	 * during run time. So, add support only for enable case, for
8017 	 * disable case station interface needs to be reconnect.
8018 	 */
8019 	if (enabled && !ahsta->enable_4addr) {
8020 		if (!ath12k_mac_sta_set_4addr(hw->wiphy, ahsta))
8021 			ahsta->enable_4addr = true;
8022 	}
8023 }
8024 EXPORT_SYMBOL(ath12k_mac_op_sta_set_4addr);
8025 
ath12k_mac_op_link_sta_rc_update(struct ieee80211_hw * hw,struct ieee80211_vif * vif,struct ieee80211_link_sta * link_sta,u32 changed)8026 void ath12k_mac_op_link_sta_rc_update(struct ieee80211_hw *hw,
8027 				      struct ieee80211_vif *vif,
8028 				      struct ieee80211_link_sta *link_sta,
8029 				      u32 changed)
8030 {
8031 	struct ieee80211_sta *sta = link_sta->sta;
8032 	struct ath12k *ar;
8033 	struct ath12k_sta *ahsta = ath12k_sta_to_ahsta(sta);
8034 	struct ath12k_vif *ahvif = ath12k_vif_to_ahvif(vif);
8035 	struct ath12k_hw *ah = ath12k_hw_to_ah(hw);
8036 	struct ath12k_link_sta *arsta;
8037 	struct ath12k_link_vif *arvif;
8038 	struct ath12k_dp_link_peer *peer;
8039 	u32 bw, smps;
8040 	struct ath12k_dp *dp;
8041 
8042 	rcu_read_lock();
8043 	arvif = rcu_dereference(ahvif->link[link_sta->link_id]);
8044 	if (!arvif) {
8045 		ath12k_hw_warn(ah, "mac sta rc update failed to fetch link vif on link id %u for peer %pM\n",
8046 			       link_sta->link_id, sta->addr);
8047 		rcu_read_unlock();
8048 		return;
8049 	}
8050 
8051 	ar = arvif->ar;
8052 	dp = ath12k_ab_to_dp(ar->ab);
8053 
8054 	arsta = rcu_dereference(ahsta->link[link_sta->link_id]);
8055 	if (!arsta) {
8056 		rcu_read_unlock();
8057 		ath12k_warn(ar->ab, "mac sta rc update failed to fetch link sta on link id %u for peer %pM\n",
8058 			    link_sta->link_id, sta->addr);
8059 		return;
8060 	}
8061 	spin_lock_bh(&dp->dp_lock);
8062 
8063 	peer = ath12k_dp_link_peer_find_by_vdev_and_addr(dp, arvif->vdev_id,
8064 							 arsta->addr);
8065 	if (!peer) {
8066 		spin_unlock_bh(&dp->dp_lock);
8067 		rcu_read_unlock();
8068 		ath12k_warn(ar->ab, "mac sta rc update failed to find peer %pM on vdev %i\n",
8069 			    arsta->addr, arvif->vdev_id);
8070 		return;
8071 	}
8072 
8073 	spin_unlock_bh(&dp->dp_lock);
8074 
8075 	if (arsta->link_id >= IEEE80211_MLD_MAX_NUM_LINKS) {
8076 		rcu_read_unlock();
8077 		return;
8078 	}
8079 
8080 	link_sta = rcu_dereference(sta->link[arsta->link_id]);
8081 	if (!link_sta) {
8082 		rcu_read_unlock();
8083 		ath12k_warn(ar->ab, "unable to access link sta in rc update for sta %pM link %u\n",
8084 			    sta->addr, arsta->link_id);
8085 		return;
8086 	}
8087 
8088 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
8089 		   "mac sta rc update for %pM changed %08x bw %d nss %d smps %d\n",
8090 		   arsta->addr, changed, link_sta->bandwidth, link_sta->rx_nss,
8091 		   link_sta->smps_mode);
8092 
8093 	spin_lock_bh(&ar->data_lock);
8094 
8095 	if (changed & IEEE80211_RC_BW_CHANGED) {
8096 		bw = ath12k_mac_ieee80211_sta_bw_to_wmi(ar, link_sta);
8097 		arsta->bw_prev = arsta->bw;
8098 		arsta->bw = bw;
8099 	}
8100 
8101 	if (changed & IEEE80211_RC_NSS_CHANGED)
8102 		arsta->nss = link_sta->rx_nss;
8103 
8104 	if (changed & IEEE80211_RC_SMPS_CHANGED) {
8105 		smps = WMI_PEER_SMPS_PS_NONE;
8106 
8107 		switch (link_sta->smps_mode) {
8108 		case IEEE80211_SMPS_AUTOMATIC:
8109 		case IEEE80211_SMPS_OFF:
8110 			smps = WMI_PEER_SMPS_PS_NONE;
8111 			break;
8112 		case IEEE80211_SMPS_STATIC:
8113 			smps = WMI_PEER_SMPS_STATIC;
8114 			break;
8115 		case IEEE80211_SMPS_DYNAMIC:
8116 			smps = WMI_PEER_SMPS_DYNAMIC;
8117 			break;
8118 		default:
8119 			ath12k_warn(ar->ab, "Invalid smps %d in sta rc update for %pM link %u\n",
8120 				    link_sta->smps_mode, arsta->addr, link_sta->link_id);
8121 			smps = WMI_PEER_SMPS_PS_NONE;
8122 			break;
8123 		}
8124 
8125 		arsta->smps = smps;
8126 	}
8127 
8128 	arsta->changed |= changed;
8129 
8130 	spin_unlock_bh(&ar->data_lock);
8131 
8132 	wiphy_work_queue(hw->wiphy, &arsta->update_wk);
8133 
8134 	rcu_read_unlock();
8135 }
8136 EXPORT_SYMBOL(ath12k_mac_op_link_sta_rc_update);
8137 
ath12k_mac_alloc_assign_link_sta(struct ath12k_hw * ah,struct ath12k_sta * ahsta,struct ath12k_vif * ahvif,u8 link_id)8138 static struct ath12k_link_sta *ath12k_mac_alloc_assign_link_sta(struct ath12k_hw *ah,
8139 								struct ath12k_sta *ahsta,
8140 								struct ath12k_vif *ahvif,
8141 								u8 link_id)
8142 {
8143 	struct ath12k_link_sta *arsta;
8144 	int ret;
8145 
8146 	lockdep_assert_wiphy(ah->hw->wiphy);
8147 
8148 	if (link_id >= IEEE80211_MLD_MAX_NUM_LINKS)
8149 		return NULL;
8150 
8151 	arsta = wiphy_dereference(ah->hw->wiphy, ahsta->link[link_id]);
8152 	if (arsta)
8153 		return NULL;
8154 
8155 	arsta = kmalloc_obj(*arsta);
8156 	if (!arsta)
8157 		return NULL;
8158 
8159 	ret = ath12k_mac_assign_link_sta(ah, ahsta, arsta, ahvif, link_id);
8160 	if (ret) {
8161 		kfree(arsta);
8162 		return NULL;
8163 	}
8164 
8165 	return arsta;
8166 }
8167 
ath12k_mac_op_change_sta_links(struct ieee80211_hw * hw,struct ieee80211_vif * vif,struct ieee80211_sta * sta,u16 old_links,u16 new_links)8168 int ath12k_mac_op_change_sta_links(struct ieee80211_hw *hw,
8169 				   struct ieee80211_vif *vif,
8170 				   struct ieee80211_sta *sta,
8171 				   u16 old_links, u16 new_links)
8172 {
8173 	struct ath12k_vif *ahvif = ath12k_vif_to_ahvif(vif);
8174 	struct ath12k_sta *ahsta = ath12k_sta_to_ahsta(sta);
8175 	struct ath12k_hw *ah = hw->priv;
8176 	struct ath12k_link_vif *arvif;
8177 	struct ath12k_link_sta *arsta;
8178 	unsigned long valid_links;
8179 	struct ath12k *ar;
8180 	u8 link_id;
8181 	int ret;
8182 
8183 	lockdep_assert_wiphy(hw->wiphy);
8184 
8185 	if (!sta->valid_links)
8186 		return -EINVAL;
8187 
8188 	/* Firmware does not support removal of one of link stas. All sta
8189 	 * would be removed during ML STA delete in sta_state(), hence link
8190 	 * sta removal is not handled here.
8191 	 */
8192 	if (new_links < old_links)
8193 		return 0;
8194 
8195 	if (ahsta->ml_peer_id == ATH12K_MLO_PEER_ID_INVALID) {
8196 		ath12k_hw_warn(ah, "unable to add link for ml sta %pM", sta->addr);
8197 		return -EINVAL;
8198 	}
8199 
8200 	/* this op is expected only after initial sta insertion with default link */
8201 	if (WARN_ON(ahsta->links_map == 0))
8202 		return -EINVAL;
8203 
8204 	valid_links = new_links;
8205 	for_each_set_bit(link_id, &valid_links, IEEE80211_MLD_MAX_NUM_LINKS) {
8206 		if (ahsta->links_map & BIT(link_id))
8207 			continue;
8208 
8209 		arvif = wiphy_dereference(hw->wiphy, ahvif->link[link_id]);
8210 		if (!arvif || !arvif->is_created)
8211 			continue;
8212 
8213 		arsta = ath12k_mac_alloc_assign_link_sta(ah, ahsta, ahvif, link_id);
8214 		if (!arsta) {
8215 			ath12k_hw_warn(ah, "Failed to alloc/assign link sta");
8216 			continue;
8217 		}
8218 
8219 		ar = arvif->ar;
8220 
8221 		ret = ath12k_mac_station_add(ar, arvif, arsta);
8222 		if (ret) {
8223 			ath12k_warn(ar->ab, "Failed to add station: %pM for VDEV: %d\n",
8224 				    arsta->addr, arvif->vdev_id);
8225 			ath12k_mac_free_unassign_link_sta(ah, ahsta, link_id);
8226 			return ret;
8227 		}
8228 	}
8229 
8230 	return 0;
8231 }
8232 EXPORT_SYMBOL(ath12k_mac_op_change_sta_links);
8233 
ath12k_mac_op_can_activate_links(struct ieee80211_hw * hw,struct ieee80211_vif * vif,u16 active_links)8234 bool ath12k_mac_op_can_activate_links(struct ieee80211_hw *hw,
8235 				      struct ieee80211_vif *vif,
8236 				      u16 active_links)
8237 {
8238 	/* TODO: Handle recovery case */
8239 
8240 	return true;
8241 }
8242 EXPORT_SYMBOL(ath12k_mac_op_can_activate_links);
8243 
ath12k_conf_tx_uapsd(struct ath12k_link_vif * arvif,u16 ac,bool enable)8244 static int ath12k_conf_tx_uapsd(struct ath12k_link_vif *arvif,
8245 				u16 ac, bool enable)
8246 {
8247 	struct ath12k *ar = arvif->ar;
8248 	struct ath12k_vif *ahvif = arvif->ahvif;
8249 	u32 value;
8250 	int ret;
8251 
8252 	if (ahvif->vdev_type != WMI_VDEV_TYPE_STA)
8253 		return 0;
8254 
8255 	switch (ac) {
8256 	case IEEE80211_AC_VO:
8257 		value = WMI_STA_PS_UAPSD_AC3_DELIVERY_EN |
8258 			WMI_STA_PS_UAPSD_AC3_TRIGGER_EN;
8259 		break;
8260 	case IEEE80211_AC_VI:
8261 		value = WMI_STA_PS_UAPSD_AC2_DELIVERY_EN |
8262 			WMI_STA_PS_UAPSD_AC2_TRIGGER_EN;
8263 		break;
8264 	case IEEE80211_AC_BE:
8265 		value = WMI_STA_PS_UAPSD_AC1_DELIVERY_EN |
8266 			WMI_STA_PS_UAPSD_AC1_TRIGGER_EN;
8267 		break;
8268 	case IEEE80211_AC_BK:
8269 		value = WMI_STA_PS_UAPSD_AC0_DELIVERY_EN |
8270 			WMI_STA_PS_UAPSD_AC0_TRIGGER_EN;
8271 		break;
8272 	}
8273 
8274 	if (enable)
8275 		ahvif->u.sta.uapsd |= value;
8276 	else
8277 		ahvif->u.sta.uapsd &= ~value;
8278 
8279 	ret = ath12k_wmi_set_sta_ps_param(ar, arvif->vdev_id,
8280 					  WMI_STA_PS_PARAM_UAPSD,
8281 					  ahvif->u.sta.uapsd);
8282 	if (ret) {
8283 		ath12k_warn(ar->ab, "could not set uapsd params %d\n", ret);
8284 		goto exit;
8285 	}
8286 
8287 	if (ahvif->u.sta.uapsd)
8288 		value = WMI_STA_PS_RX_WAKE_POLICY_POLL_UAPSD;
8289 	else
8290 		value = WMI_STA_PS_RX_WAKE_POLICY_WAKE;
8291 
8292 	ret = ath12k_wmi_set_sta_ps_param(ar, arvif->vdev_id,
8293 					  WMI_STA_PS_PARAM_RX_WAKE_POLICY,
8294 					  value);
8295 	if (ret)
8296 		ath12k_warn(ar->ab, "could not set rx wake param %d\n", ret);
8297 
8298 exit:
8299 	return ret;
8300 }
8301 
ath12k_mac_conf_tx(struct ath12k_link_vif * arvif,u16 ac,const struct ieee80211_tx_queue_params * params)8302 static int ath12k_mac_conf_tx(struct ath12k_link_vif *arvif, u16 ac,
8303 			      const struct ieee80211_tx_queue_params *params)
8304 {
8305 	struct wmi_wmm_params_arg *p = NULL;
8306 	struct ath12k *ar = arvif->ar;
8307 	struct ath12k_base *ab = ar->ab;
8308 	int ret;
8309 
8310 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
8311 
8312 	switch (ac) {
8313 	case IEEE80211_AC_VO:
8314 		p = &arvif->wmm_params.ac_vo;
8315 		break;
8316 	case IEEE80211_AC_VI:
8317 		p = &arvif->wmm_params.ac_vi;
8318 		break;
8319 	case IEEE80211_AC_BE:
8320 		p = &arvif->wmm_params.ac_be;
8321 		break;
8322 	case IEEE80211_AC_BK:
8323 		p = &arvif->wmm_params.ac_bk;
8324 		break;
8325 	}
8326 
8327 	if (WARN_ON(!p)) {
8328 		ret = -EINVAL;
8329 		goto exit;
8330 	}
8331 
8332 	p->cwmin = params->cw_min;
8333 	p->cwmax = params->cw_max;
8334 	p->aifs = params->aifs;
8335 	p->txop = params->txop;
8336 
8337 	ret = ath12k_wmi_send_wmm_update_cmd(ar, arvif->vdev_id,
8338 					     &arvif->wmm_params);
8339 	if (ret) {
8340 		ath12k_warn(ab, "pdev idx %d failed to set wmm params: %d\n",
8341 			    ar->pdev_idx, ret);
8342 		goto exit;
8343 	}
8344 
8345 	ret = ath12k_conf_tx_uapsd(arvif, ac, params->uapsd);
8346 	if (ret)
8347 		ath12k_warn(ab, "pdev idx %d failed to set sta uapsd: %d\n",
8348 			    ar->pdev_idx, ret);
8349 
8350 exit:
8351 	return ret;
8352 }
8353 
ath12k_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)8354 int ath12k_mac_op_conf_tx(struct ieee80211_hw *hw,
8355 			  struct ieee80211_vif *vif,
8356 			  unsigned int link_id, u16 ac,
8357 			  const struct ieee80211_tx_queue_params *params)
8358 {
8359 	struct ath12k_vif *ahvif = ath12k_vif_to_ahvif(vif);
8360 	struct ath12k_link_vif *arvif;
8361 	struct ath12k_vif_cache *cache;
8362 	int ret;
8363 
8364 	lockdep_assert_wiphy(hw->wiphy);
8365 
8366 	if (link_id >= IEEE80211_MLD_MAX_NUM_LINKS)
8367 		return -EINVAL;
8368 
8369 	arvif = wiphy_dereference(hw->wiphy, ahvif->link[link_id]);
8370 	if (!arvif || !arvif->is_created) {
8371 		cache = ath12k_ahvif_get_link_cache(ahvif, link_id);
8372 		if (!cache)
8373 			return -ENOSPC;
8374 
8375 		cache->tx_conf.changed = true;
8376 		cache->tx_conf.ac = ac;
8377 		cache->tx_conf.tx_queue_params = *params;
8378 
8379 		return 0;
8380 	}
8381 
8382 	ret = ath12k_mac_conf_tx(arvif, ac, params);
8383 
8384 	return ret;
8385 }
8386 EXPORT_SYMBOL(ath12k_mac_op_conf_tx);
8387 
8388 static struct ieee80211_sta_ht_cap
ath12k_create_ht_cap(struct ath12k * ar,u32 ar_ht_cap,u32 rate_cap_rx_chainmask)8389 ath12k_create_ht_cap(struct ath12k *ar, u32 ar_ht_cap, u32 rate_cap_rx_chainmask)
8390 {
8391 	int i;
8392 	struct ieee80211_sta_ht_cap ht_cap = {};
8393 	u32 ar_vht_cap = ar->pdev->cap.vht_cap;
8394 
8395 	if (!(ar_ht_cap & WMI_HT_CAP_ENABLED))
8396 		return ht_cap;
8397 
8398 	ht_cap.ht_supported = 1;
8399 	ht_cap.ampdu_factor = IEEE80211_HT_MAX_AMPDU_64K;
8400 	ht_cap.ampdu_density = IEEE80211_HT_MPDU_DENSITY_NONE;
8401 	ht_cap.cap |= IEEE80211_HT_CAP_SUP_WIDTH_20_40;
8402 	ht_cap.cap |= IEEE80211_HT_CAP_DSSSCCK40;
8403 	ht_cap.cap |= WLAN_HT_CAP_SM_PS_STATIC << IEEE80211_HT_CAP_SM_PS_SHIFT;
8404 
8405 	if (ar_ht_cap & WMI_HT_CAP_HT20_SGI)
8406 		ht_cap.cap |= IEEE80211_HT_CAP_SGI_20;
8407 
8408 	if (ar_ht_cap & WMI_HT_CAP_HT40_SGI)
8409 		ht_cap.cap |= IEEE80211_HT_CAP_SGI_40;
8410 
8411 	if (ar_ht_cap & WMI_HT_CAP_DYNAMIC_SMPS) {
8412 		u32 smps;
8413 
8414 		smps   = WLAN_HT_CAP_SM_PS_DYNAMIC;
8415 		smps <<= IEEE80211_HT_CAP_SM_PS_SHIFT;
8416 
8417 		ht_cap.cap |= smps;
8418 	}
8419 
8420 	if (ar_ht_cap & WMI_HT_CAP_TX_STBC)
8421 		ht_cap.cap |= IEEE80211_HT_CAP_TX_STBC;
8422 
8423 	if (ar_ht_cap & WMI_HT_CAP_RX_STBC) {
8424 		u32 stbc;
8425 
8426 		stbc   = ar_ht_cap;
8427 		stbc  &= WMI_HT_CAP_RX_STBC;
8428 		stbc >>= WMI_HT_CAP_RX_STBC_MASK_SHIFT;
8429 		stbc <<= IEEE80211_HT_CAP_RX_STBC_SHIFT;
8430 		stbc  &= IEEE80211_HT_CAP_RX_STBC;
8431 
8432 		ht_cap.cap |= stbc;
8433 	}
8434 
8435 	if (ar_ht_cap & WMI_HT_CAP_RX_LDPC)
8436 		ht_cap.cap |= IEEE80211_HT_CAP_LDPC_CODING;
8437 
8438 	if (ar_ht_cap & WMI_HT_CAP_L_SIG_TXOP_PROT)
8439 		ht_cap.cap |= IEEE80211_HT_CAP_LSIG_TXOP_PROT;
8440 
8441 	if (ar_vht_cap & WMI_VHT_CAP_MAX_MPDU_LEN_MASK)
8442 		ht_cap.cap |= IEEE80211_HT_CAP_MAX_AMSDU;
8443 
8444 	for (i = 0; i < ar->num_rx_chains; i++) {
8445 		if (rate_cap_rx_chainmask & BIT(i))
8446 			ht_cap.mcs.rx_mask[i] = 0xFF;
8447 	}
8448 
8449 	ht_cap.mcs.tx_params |= IEEE80211_HT_MCS_TX_DEFINED;
8450 
8451 	return ht_cap;
8452 }
8453 
ath12k_mac_set_txbf_conf(struct ath12k_link_vif * arvif)8454 static int ath12k_mac_set_txbf_conf(struct ath12k_link_vif *arvif)
8455 {
8456 	u32 value = 0;
8457 	struct ath12k *ar = arvif->ar;
8458 	struct ath12k_vif *ahvif = arvif->ahvif;
8459 	int nsts;
8460 	int sound_dim;
8461 	u32 vht_cap = ar->pdev->cap.vht_cap;
8462 	u32 vdev_param = WMI_VDEV_PARAM_TXBF;
8463 
8464 	if (vht_cap & (IEEE80211_VHT_CAP_SU_BEAMFORMEE_CAPABLE)) {
8465 		nsts = vht_cap & IEEE80211_VHT_CAP_BEAMFORMEE_STS_MASK;
8466 		nsts >>= IEEE80211_VHT_CAP_BEAMFORMEE_STS_SHIFT;
8467 		value |= SM(nsts, WMI_TXBF_STS_CAP_OFFSET);
8468 	}
8469 
8470 	if (vht_cap & (IEEE80211_VHT_CAP_SU_BEAMFORMER_CAPABLE)) {
8471 		sound_dim = vht_cap &
8472 			    IEEE80211_VHT_CAP_SOUNDING_DIMENSIONS_MASK;
8473 		sound_dim >>= IEEE80211_VHT_CAP_SOUNDING_DIMENSIONS_SHIFT;
8474 		if (sound_dim > (ar->num_tx_chains - 1))
8475 			sound_dim = ar->num_tx_chains - 1;
8476 		value |= SM(sound_dim, WMI_BF_SOUND_DIM_OFFSET);
8477 	}
8478 
8479 	if (!value)
8480 		return 0;
8481 
8482 	if (vht_cap & IEEE80211_VHT_CAP_SU_BEAMFORMER_CAPABLE) {
8483 		value |= WMI_VDEV_PARAM_TXBF_SU_TX_BFER;
8484 
8485 		if ((vht_cap & IEEE80211_VHT_CAP_MU_BEAMFORMER_CAPABLE) &&
8486 		    ahvif->vdev_type == WMI_VDEV_TYPE_AP)
8487 			value |= WMI_VDEV_PARAM_TXBF_MU_TX_BFER;
8488 	}
8489 
8490 	if (vht_cap & IEEE80211_VHT_CAP_SU_BEAMFORMEE_CAPABLE) {
8491 		value |= WMI_VDEV_PARAM_TXBF_SU_TX_BFEE;
8492 
8493 		if ((vht_cap & IEEE80211_VHT_CAP_MU_BEAMFORMEE_CAPABLE) &&
8494 		    ahvif->vdev_type == WMI_VDEV_TYPE_STA)
8495 			value |= WMI_VDEV_PARAM_TXBF_MU_TX_BFEE;
8496 	}
8497 
8498 	return ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
8499 					     vdev_param, value);
8500 }
8501 
ath12k_set_vht_txbf_cap(struct ath12k * ar,u32 * vht_cap)8502 static void ath12k_set_vht_txbf_cap(struct ath12k *ar, u32 *vht_cap)
8503 {
8504 	bool subfer, subfee;
8505 	int sound_dim = 0;
8506 
8507 	subfer = !!(*vht_cap & (IEEE80211_VHT_CAP_SU_BEAMFORMER_CAPABLE));
8508 	subfee = !!(*vht_cap & (IEEE80211_VHT_CAP_SU_BEAMFORMEE_CAPABLE));
8509 
8510 	if (ar->num_tx_chains < 2) {
8511 		*vht_cap &= ~(IEEE80211_VHT_CAP_SU_BEAMFORMER_CAPABLE);
8512 		subfer = false;
8513 	}
8514 
8515 	/* If SU Beaformer is not set, then disable MU Beamformer Capability */
8516 	if (!subfer)
8517 		*vht_cap &= ~(IEEE80211_VHT_CAP_MU_BEAMFORMER_CAPABLE);
8518 
8519 	/* If SU Beaformee is not set, then disable MU Beamformee Capability */
8520 	if (!subfee)
8521 		*vht_cap &= ~(IEEE80211_VHT_CAP_MU_BEAMFORMEE_CAPABLE);
8522 
8523 	sound_dim = u32_get_bits(*vht_cap,
8524 				 IEEE80211_VHT_CAP_SOUNDING_DIMENSIONS_MASK);
8525 	*vht_cap = u32_replace_bits(*vht_cap, 0,
8526 				    IEEE80211_VHT_CAP_SOUNDING_DIMENSIONS_MASK);
8527 
8528 	/* TODO: Need to check invalid STS and Sound_dim values set by FW? */
8529 
8530 	/* Enable Sounding Dimension Field only if SU BF is enabled */
8531 	if (subfer) {
8532 		if (sound_dim > (ar->num_tx_chains - 1))
8533 			sound_dim = ar->num_tx_chains - 1;
8534 
8535 		*vht_cap = u32_replace_bits(*vht_cap, sound_dim,
8536 					    IEEE80211_VHT_CAP_SOUNDING_DIMENSIONS_MASK);
8537 	}
8538 
8539 	/* Use the STS advertised by FW unless SU Beamformee is not supported*/
8540 	if (!subfee)
8541 		*vht_cap &= ~(IEEE80211_VHT_CAP_BEAMFORMEE_STS_MASK);
8542 }
8543 
8544 static struct ieee80211_sta_vht_cap
ath12k_create_vht_cap(struct ath12k * ar,u32 rate_cap_tx_chainmask,u32 rate_cap_rx_chainmask)8545 ath12k_create_vht_cap(struct ath12k *ar, u32 rate_cap_tx_chainmask,
8546 		      u32 rate_cap_rx_chainmask)
8547 {
8548 	struct ieee80211_sta_vht_cap vht_cap = {};
8549 	u16 txmcs_map, rxmcs_map;
8550 	int i;
8551 
8552 	vht_cap.vht_supported = 1;
8553 	vht_cap.cap = ar->pdev->cap.vht_cap;
8554 
8555 	if (ar->pdev->cap.nss_ratio_enabled)
8556 		vht_cap.vht_mcs.tx_highest |=
8557 			cpu_to_le16(IEEE80211_VHT_EXT_NSS_BW_CAPABLE);
8558 
8559 	ath12k_set_vht_txbf_cap(ar, &vht_cap.cap);
8560 
8561 	/* 80P80 is not supported */
8562 	vht_cap.cap &= ~IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_160_80PLUS80MHZ;
8563 
8564 	rxmcs_map = 0;
8565 	txmcs_map = 0;
8566 	for (i = 0; i < 8; i++) {
8567 		if (i < ar->num_tx_chains && rate_cap_tx_chainmask & BIT(i))
8568 			txmcs_map |= IEEE80211_VHT_MCS_SUPPORT_0_9 << (i * 2);
8569 		else
8570 			txmcs_map |= IEEE80211_VHT_MCS_NOT_SUPPORTED << (i * 2);
8571 
8572 		if (i < ar->num_rx_chains && rate_cap_rx_chainmask & BIT(i))
8573 			rxmcs_map |= IEEE80211_VHT_MCS_SUPPORT_0_9 << (i * 2);
8574 		else
8575 			rxmcs_map |= IEEE80211_VHT_MCS_NOT_SUPPORTED << (i * 2);
8576 	}
8577 
8578 	if (rate_cap_tx_chainmask <= 1)
8579 		vht_cap.cap &= ~IEEE80211_VHT_CAP_TXSTBC;
8580 
8581 	vht_cap.vht_mcs.rx_mcs_map = cpu_to_le16(rxmcs_map);
8582 	vht_cap.vht_mcs.tx_mcs_map = cpu_to_le16(txmcs_map);
8583 
8584 	/* Check if the HW supports 1:1 NSS ratio and reset
8585 	 * EXT NSS BW Support field to 0 to indicate 1:1 ratio
8586 	 */
8587 	if (ar->pdev->cap.nss_ratio_info == WMI_NSS_RATIO_1_NSS)
8588 		vht_cap.cap &= ~IEEE80211_VHT_CAP_EXT_NSS_BW_MASK;
8589 
8590 	return vht_cap;
8591 }
8592 
ath12k_mac_setup_ht_vht_cap(struct ath12k * ar,struct ath12k_pdev_cap * cap,u32 * ht_cap_info)8593 static void ath12k_mac_setup_ht_vht_cap(struct ath12k *ar,
8594 					struct ath12k_pdev_cap *cap,
8595 					u32 *ht_cap_info)
8596 {
8597 	struct ieee80211_supported_band *band;
8598 	u32 rate_cap_tx_chainmask;
8599 	u32 rate_cap_rx_chainmask;
8600 	u32 ht_cap;
8601 
8602 	rate_cap_tx_chainmask = ar->cfg_tx_chainmask >> cap->tx_chain_mask_shift;
8603 	rate_cap_rx_chainmask = ar->cfg_rx_chainmask >> cap->rx_chain_mask_shift;
8604 
8605 	if (cap->supported_bands & WMI_HOST_WLAN_2GHZ_CAP) {
8606 		band = &ar->mac.sbands[NL80211_BAND_2GHZ];
8607 		ht_cap = cap->band[NL80211_BAND_2GHZ].ht_cap_info;
8608 		if (ht_cap_info)
8609 			*ht_cap_info = ht_cap;
8610 		band->ht_cap = ath12k_create_ht_cap(ar, ht_cap,
8611 						    rate_cap_rx_chainmask);
8612 	}
8613 
8614 	if (cap->supported_bands & WMI_HOST_WLAN_5GHZ_CAP &&
8615 	    (ar->ab->hw_params->single_pdev_only ||
8616 	     !ar->supports_6ghz)) {
8617 		band = &ar->mac.sbands[NL80211_BAND_5GHZ];
8618 		ht_cap = cap->band[NL80211_BAND_5GHZ].ht_cap_info;
8619 		if (ht_cap_info)
8620 			*ht_cap_info = ht_cap;
8621 		band->ht_cap = ath12k_create_ht_cap(ar, ht_cap,
8622 						    rate_cap_rx_chainmask);
8623 		band->vht_cap = ath12k_create_vht_cap(ar, rate_cap_tx_chainmask,
8624 						      rate_cap_rx_chainmask);
8625 	}
8626 }
8627 
ath12k_check_chain_mask(struct ath12k * ar,u32 ant,bool is_tx_ant)8628 static int ath12k_check_chain_mask(struct ath12k *ar, u32 ant, bool is_tx_ant)
8629 {
8630 	/* TODO: Check the request chainmask against the supported
8631 	 * chainmask table which is advertised in extented_service_ready event
8632 	 */
8633 
8634 	return 0;
8635 }
8636 
ath12k_gen_ppe_thresh(struct ath12k_wmi_ppe_threshold_arg * fw_ppet,u8 * he_ppet)8637 static void ath12k_gen_ppe_thresh(struct ath12k_wmi_ppe_threshold_arg *fw_ppet,
8638 				  u8 *he_ppet)
8639 {
8640 	int nss, ru;
8641 	u8 bit = 7;
8642 
8643 	he_ppet[0] = fw_ppet->numss_m1 & IEEE80211_PPE_THRES_NSS_MASK;
8644 	he_ppet[0] |= (fw_ppet->ru_bit_mask <<
8645 		       IEEE80211_PPE_THRES_RU_INDEX_BITMASK_POS) &
8646 		      IEEE80211_PPE_THRES_RU_INDEX_BITMASK_MASK;
8647 	for (nss = 0; nss <= fw_ppet->numss_m1; nss++) {
8648 		for (ru = 0; ru < 4; ru++) {
8649 			u8 val;
8650 			int i;
8651 
8652 			if ((fw_ppet->ru_bit_mask & BIT(ru)) == 0)
8653 				continue;
8654 			val = (fw_ppet->ppet16_ppet8_ru3_ru0[nss] >> (ru * 6)) &
8655 			       0x3f;
8656 			val = ((val >> 3) & 0x7) | ((val & 0x7) << 3);
8657 			for (i = 5; i >= 0; i--) {
8658 				he_ppet[bit / 8] |=
8659 					((val >> i) & 0x1) << ((bit % 8));
8660 				bit++;
8661 			}
8662 		}
8663 	}
8664 }
8665 
8666 static void
ath12k_mac_filter_he_cap_mesh(struct ieee80211_he_cap_elem * he_cap_elem)8667 ath12k_mac_filter_he_cap_mesh(struct ieee80211_he_cap_elem *he_cap_elem)
8668 {
8669 	u8 m;
8670 
8671 	m = IEEE80211_HE_MAC_CAP0_TWT_RES |
8672 	    IEEE80211_HE_MAC_CAP0_TWT_REQ;
8673 	he_cap_elem->mac_cap_info[0] &= ~m;
8674 
8675 	m = IEEE80211_HE_MAC_CAP2_TRS |
8676 	    IEEE80211_HE_MAC_CAP2_BCAST_TWT |
8677 	    IEEE80211_HE_MAC_CAP2_MU_CASCADING;
8678 	he_cap_elem->mac_cap_info[2] &= ~m;
8679 
8680 	m = IEEE80211_HE_MAC_CAP3_FLEX_TWT_SCHED |
8681 	    IEEE80211_HE_MAC_CAP2_BCAST_TWT |
8682 	    IEEE80211_HE_MAC_CAP2_MU_CASCADING;
8683 	he_cap_elem->mac_cap_info[3] &= ~m;
8684 
8685 	m = IEEE80211_HE_MAC_CAP4_BSRP_BQRP_A_MPDU_AGG |
8686 	    IEEE80211_HE_MAC_CAP4_BQR;
8687 	he_cap_elem->mac_cap_info[4] &= ~m;
8688 
8689 	m = IEEE80211_HE_MAC_CAP5_SUBCHAN_SELECTIVE_TRANSMISSION |
8690 	    IEEE80211_HE_MAC_CAP5_UL_2x996_TONE_RU |
8691 	    IEEE80211_HE_MAC_CAP5_PUNCTURED_SOUNDING |
8692 	    IEEE80211_HE_MAC_CAP5_HT_VHT_TRIG_FRAME_RX;
8693 	he_cap_elem->mac_cap_info[5] &= ~m;
8694 
8695 	m = IEEE80211_HE_PHY_CAP2_UL_MU_FULL_MU_MIMO |
8696 	    IEEE80211_HE_PHY_CAP2_UL_MU_PARTIAL_MU_MIMO;
8697 	he_cap_elem->phy_cap_info[2] &= ~m;
8698 
8699 	m = IEEE80211_HE_PHY_CAP3_RX_PARTIAL_BW_SU_IN_20MHZ_MU |
8700 	    IEEE80211_HE_PHY_CAP3_DCM_MAX_CONST_TX_MASK |
8701 	    IEEE80211_HE_PHY_CAP3_DCM_MAX_CONST_RX_MASK;
8702 	he_cap_elem->phy_cap_info[3] &= ~m;
8703 
8704 	m = IEEE80211_HE_PHY_CAP4_MU_BEAMFORMER;
8705 	he_cap_elem->phy_cap_info[4] &= ~m;
8706 
8707 	m = IEEE80211_HE_PHY_CAP5_NG16_MU_FEEDBACK;
8708 	he_cap_elem->phy_cap_info[5] &= ~m;
8709 
8710 	m = IEEE80211_HE_PHY_CAP6_CODEBOOK_SIZE_75_MU |
8711 	    IEEE80211_HE_PHY_CAP6_TRIG_MU_BEAMFORMING_PARTIAL_BW_FB |
8712 	    IEEE80211_HE_PHY_CAP6_TRIG_CQI_FB |
8713 	    IEEE80211_HE_PHY_CAP6_PARTIAL_BANDWIDTH_DL_MUMIMO;
8714 	he_cap_elem->phy_cap_info[6] &= ~m;
8715 
8716 	m = IEEE80211_HE_PHY_CAP7_PSR_BASED_SR |
8717 	    IEEE80211_HE_PHY_CAP7_POWER_BOOST_FACTOR_SUPP |
8718 	    IEEE80211_HE_PHY_CAP7_STBC_TX_ABOVE_80MHZ |
8719 	    IEEE80211_HE_PHY_CAP7_STBC_RX_ABOVE_80MHZ;
8720 	he_cap_elem->phy_cap_info[7] &= ~m;
8721 
8722 	m = IEEE80211_HE_PHY_CAP8_HE_ER_SU_PPDU_4XLTF_AND_08_US_GI |
8723 	    IEEE80211_HE_PHY_CAP8_20MHZ_IN_40MHZ_HE_PPDU_IN_2G |
8724 	    IEEE80211_HE_PHY_CAP8_20MHZ_IN_160MHZ_HE_PPDU |
8725 	    IEEE80211_HE_PHY_CAP8_80MHZ_IN_160MHZ_HE_PPDU;
8726 	he_cap_elem->phy_cap_info[8] &= ~m;
8727 
8728 	m = IEEE80211_HE_PHY_CAP9_LONGER_THAN_16_SIGB_OFDM_SYM |
8729 	    IEEE80211_HE_PHY_CAP9_NON_TRIGGERED_CQI_FEEDBACK |
8730 	    IEEE80211_HE_PHY_CAP9_RX_1024_QAM_LESS_THAN_242_TONE_RU |
8731 	    IEEE80211_HE_PHY_CAP9_TX_1024_QAM_LESS_THAN_242_TONE_RU |
8732 	    IEEE80211_HE_PHY_CAP9_RX_FULL_BW_SU_USING_MU_WITH_COMP_SIGB |
8733 	    IEEE80211_HE_PHY_CAP9_RX_FULL_BW_SU_USING_MU_WITH_NON_COMP_SIGB;
8734 	he_cap_elem->phy_cap_info[9] &= ~m;
8735 }
8736 
ath12k_mac_setup_he_6ghz_cap(struct ath12k_pdev_cap * pcap,struct ath12k_band_cap * bcap)8737 static __le16 ath12k_mac_setup_he_6ghz_cap(struct ath12k_pdev_cap *pcap,
8738 					   struct ath12k_band_cap *bcap)
8739 {
8740 	u8 val;
8741 
8742 	bcap->he_6ghz_capa = IEEE80211_HT_MPDU_DENSITY_NONE;
8743 	if (bcap->ht_cap_info & WMI_HT_CAP_DYNAMIC_SMPS)
8744 		bcap->he_6ghz_capa |=
8745 			u32_encode_bits(WLAN_HT_CAP_SM_PS_DYNAMIC,
8746 					IEEE80211_HE_6GHZ_CAP_SM_PS);
8747 	else
8748 		bcap->he_6ghz_capa |=
8749 			u32_encode_bits(WLAN_HT_CAP_SM_PS_DISABLED,
8750 					IEEE80211_HE_6GHZ_CAP_SM_PS);
8751 	val = u32_get_bits(pcap->vht_cap,
8752 			   IEEE80211_VHT_CAP_MAX_A_MPDU_LENGTH_EXPONENT_MASK);
8753 	bcap->he_6ghz_capa |=
8754 		u32_encode_bits(val, IEEE80211_HE_6GHZ_CAP_MAX_AMPDU_LEN_EXP);
8755 	val = u32_get_bits(pcap->vht_cap,
8756 			   IEEE80211_VHT_CAP_MAX_MPDU_MASK);
8757 	bcap->he_6ghz_capa |=
8758 		u32_encode_bits(val, IEEE80211_HE_6GHZ_CAP_MAX_MPDU_LEN);
8759 	if (pcap->vht_cap & IEEE80211_VHT_CAP_RX_ANTENNA_PATTERN)
8760 		bcap->he_6ghz_capa |= IEEE80211_HE_6GHZ_CAP_RX_ANTPAT_CONS;
8761 	if (pcap->vht_cap & IEEE80211_VHT_CAP_TX_ANTENNA_PATTERN)
8762 		bcap->he_6ghz_capa |= IEEE80211_HE_6GHZ_CAP_TX_ANTPAT_CONS;
8763 
8764 	return cpu_to_le16(bcap->he_6ghz_capa);
8765 }
8766 
ath12k_mac_set_hemcsmap(struct ath12k * ar,struct ath12k_pdev_cap * cap,struct ieee80211_sta_he_cap * he_cap)8767 static void ath12k_mac_set_hemcsmap(struct ath12k *ar,
8768 				    struct ath12k_pdev_cap *cap,
8769 				    struct ieee80211_sta_he_cap *he_cap)
8770 {
8771 	struct ieee80211_he_mcs_nss_supp *mcs_nss = &he_cap->he_mcs_nss_supp;
8772 	u8 maxtxnss_160 = ath12k_get_nss_160mhz(ar, ar->num_tx_chains);
8773 	u8 maxrxnss_160 = ath12k_get_nss_160mhz(ar, ar->num_rx_chains);
8774 	u16 txmcs_map_160 = 0, rxmcs_map_160 = 0;
8775 	u16 txmcs_map = 0, rxmcs_map = 0;
8776 	u32 i;
8777 
8778 	for (i = 0; i < 8; i++) {
8779 		if (i < ar->num_tx_chains &&
8780 		    (ar->cfg_tx_chainmask >> cap->tx_chain_mask_shift) & BIT(i))
8781 			txmcs_map |= IEEE80211_HE_MCS_SUPPORT_0_11 << (i * 2);
8782 		else
8783 			txmcs_map |= IEEE80211_HE_MCS_NOT_SUPPORTED << (i * 2);
8784 
8785 		if (i < ar->num_rx_chains &&
8786 		    (ar->cfg_rx_chainmask >> cap->tx_chain_mask_shift) & BIT(i))
8787 			rxmcs_map |= IEEE80211_HE_MCS_SUPPORT_0_11 << (i * 2);
8788 		else
8789 			rxmcs_map |= IEEE80211_HE_MCS_NOT_SUPPORTED << (i * 2);
8790 
8791 		if (i < maxtxnss_160 &&
8792 		    (ar->cfg_tx_chainmask >> cap->tx_chain_mask_shift) & BIT(i))
8793 			txmcs_map_160 |= IEEE80211_HE_MCS_SUPPORT_0_11 << (i * 2);
8794 		else
8795 			txmcs_map_160 |= IEEE80211_HE_MCS_NOT_SUPPORTED << (i * 2);
8796 
8797 		if (i < maxrxnss_160 &&
8798 		    (ar->cfg_tx_chainmask >> cap->tx_chain_mask_shift) & BIT(i))
8799 			rxmcs_map_160 |= IEEE80211_HE_MCS_SUPPORT_0_11 << (i * 2);
8800 		else
8801 			rxmcs_map_160 |= IEEE80211_HE_MCS_NOT_SUPPORTED << (i * 2);
8802 	}
8803 
8804 	mcs_nss->rx_mcs_80 = cpu_to_le16(rxmcs_map & 0xffff);
8805 	mcs_nss->tx_mcs_80 = cpu_to_le16(txmcs_map & 0xffff);
8806 	mcs_nss->rx_mcs_160 = cpu_to_le16(rxmcs_map_160 & 0xffff);
8807 	mcs_nss->tx_mcs_160 = cpu_to_le16(txmcs_map_160 & 0xffff);
8808 }
8809 
ath12k_mac_copy_he_cap(struct ath12k * ar,struct ath12k_band_cap * band_cap,int iftype,u8 num_tx_chains,struct ieee80211_sta_he_cap * he_cap)8810 static void ath12k_mac_copy_he_cap(struct ath12k *ar,
8811 				   struct ath12k_band_cap *band_cap,
8812 				   int iftype, u8 num_tx_chains,
8813 				   struct ieee80211_sta_he_cap *he_cap)
8814 {
8815 	struct ieee80211_he_cap_elem *he_cap_elem = &he_cap->he_cap_elem;
8816 
8817 	he_cap->has_he = true;
8818 	memcpy(he_cap_elem->mac_cap_info, band_cap->he_cap_info,
8819 	       sizeof(he_cap_elem->mac_cap_info));
8820 	memcpy(he_cap_elem->phy_cap_info, band_cap->he_cap_phy_info,
8821 	       sizeof(he_cap_elem->phy_cap_info));
8822 
8823 	he_cap_elem->mac_cap_info[1] &=
8824 		IEEE80211_HE_MAC_CAP1_TF_MAC_PAD_DUR_MASK;
8825 	he_cap_elem->phy_cap_info[0] &=
8826 		IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_40MHZ_IN_2G |
8827 		IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_40MHZ_80MHZ_IN_5G |
8828 		IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_160MHZ_IN_5G;
8829 	/* 80PLUS80 is not supported */
8830 	he_cap_elem->phy_cap_info[0] &=
8831 		~IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_80PLUS80_MHZ_IN_5G;
8832 	he_cap_elem->phy_cap_info[5] &=
8833 		~IEEE80211_HE_PHY_CAP5_BEAMFORMEE_NUM_SND_DIM_UNDER_80MHZ_MASK;
8834 	he_cap_elem->phy_cap_info[5] |= num_tx_chains - 1;
8835 
8836 	switch (iftype) {
8837 	case NL80211_IFTYPE_AP:
8838 		he_cap_elem->mac_cap_info[2] &=
8839 			~IEEE80211_HE_MAC_CAP2_BCAST_TWT;
8840 		he_cap_elem->phy_cap_info[3] &=
8841 			~IEEE80211_HE_PHY_CAP3_DCM_MAX_CONST_TX_MASK;
8842 		he_cap_elem->phy_cap_info[9] |=
8843 			IEEE80211_HE_PHY_CAP9_RX_1024_QAM_LESS_THAN_242_TONE_RU;
8844 		break;
8845 	case NL80211_IFTYPE_STATION:
8846 		he_cap_elem->mac_cap_info[0] &= ~IEEE80211_HE_MAC_CAP0_TWT_RES;
8847 		he_cap_elem->mac_cap_info[0] |= IEEE80211_HE_MAC_CAP0_TWT_REQ;
8848 		he_cap_elem->phy_cap_info[9] |=
8849 			IEEE80211_HE_PHY_CAP9_TX_1024_QAM_LESS_THAN_242_TONE_RU;
8850 		break;
8851 	case NL80211_IFTYPE_MESH_POINT:
8852 		ath12k_mac_filter_he_cap_mesh(he_cap_elem);
8853 		break;
8854 	}
8855 
8856 	ath12k_mac_set_hemcsmap(ar, &ar->pdev->cap, he_cap);
8857 	memset(he_cap->ppe_thres, 0, sizeof(he_cap->ppe_thres));
8858 	if (he_cap_elem->phy_cap_info[6] &
8859 	    IEEE80211_HE_PHY_CAP6_PPE_THRESHOLD_PRESENT)
8860 		ath12k_gen_ppe_thresh(&band_cap->he_ppet, he_cap->ppe_thres);
8861 }
8862 
8863 static void
ath12k_mac_copy_eht_mcs_nss(struct ath12k_band_cap * band_cap,struct ieee80211_eht_mcs_nss_supp * mcs_nss,const struct ieee80211_he_cap_elem * he_cap,const struct ieee80211_eht_cap_elem_fixed * eht_cap)8864 ath12k_mac_copy_eht_mcs_nss(struct ath12k_band_cap *band_cap,
8865 			    struct ieee80211_eht_mcs_nss_supp *mcs_nss,
8866 			    const struct ieee80211_he_cap_elem *he_cap,
8867 			    const struct ieee80211_eht_cap_elem_fixed *eht_cap)
8868 {
8869 	if ((he_cap->phy_cap_info[0] &
8870 	     (IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_40MHZ_IN_2G |
8871 	      IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_40MHZ_80MHZ_IN_5G |
8872 	      IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_160MHZ_IN_5G |
8873 	      IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_80PLUS80_MHZ_IN_5G)) == 0)
8874 		memcpy(&mcs_nss->only_20mhz, &band_cap->eht_mcs_20_only,
8875 		       sizeof(struct ieee80211_eht_mcs_nss_supp_20mhz_only));
8876 
8877 	if (he_cap->phy_cap_info[0] &
8878 	    (IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_40MHZ_IN_2G |
8879 	     IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_40MHZ_80MHZ_IN_5G))
8880 		memcpy(&mcs_nss->bw._80, &band_cap->eht_mcs_80,
8881 		       sizeof(struct ieee80211_eht_mcs_nss_supp_bw));
8882 
8883 	if (he_cap->phy_cap_info[0] &
8884 	    IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_160MHZ_IN_5G)
8885 		memcpy(&mcs_nss->bw._160, &band_cap->eht_mcs_160,
8886 		       sizeof(struct ieee80211_eht_mcs_nss_supp_bw));
8887 
8888 	if (eht_cap->phy_cap_info[0] & IEEE80211_EHT_PHY_CAP0_320MHZ_IN_6GHZ)
8889 		memcpy(&mcs_nss->bw._320, &band_cap->eht_mcs_320,
8890 		       sizeof(struct ieee80211_eht_mcs_nss_supp_bw));
8891 }
8892 
ath12k_mac_copy_eht_ppe_thresh(struct ath12k_wmi_ppe_threshold_arg * fw_ppet,struct ieee80211_sta_eht_cap * cap)8893 static void ath12k_mac_copy_eht_ppe_thresh(struct ath12k_wmi_ppe_threshold_arg *fw_ppet,
8894 					   struct ieee80211_sta_eht_cap *cap)
8895 {
8896 	u16 bit = IEEE80211_EHT_PPE_THRES_INFO_HEADER_SIZE;
8897 	u8 i, nss, ru, ppet_bit_len_per_ru = IEEE80211_EHT_PPE_THRES_INFO_PPET_SIZE * 2;
8898 
8899 	u8p_replace_bits(&cap->eht_ppe_thres[0], fw_ppet->numss_m1,
8900 			 IEEE80211_EHT_PPE_THRES_NSS_MASK);
8901 
8902 	u16p_replace_bits((u16 *)&cap->eht_ppe_thres[0], fw_ppet->ru_bit_mask,
8903 			  IEEE80211_EHT_PPE_THRES_RU_INDEX_BITMASK_MASK);
8904 
8905 	for (nss = 0; nss <= fw_ppet->numss_m1; nss++) {
8906 		for (ru = 0;
8907 		     ru < hweight16(IEEE80211_EHT_PPE_THRES_RU_INDEX_BITMASK_MASK);
8908 		     ru++) {
8909 			u32 val = 0;
8910 
8911 			if ((fw_ppet->ru_bit_mask & BIT(ru)) == 0)
8912 				continue;
8913 
8914 			u32p_replace_bits(&val, fw_ppet->ppet16_ppet8_ru3_ru0[nss] >>
8915 						(ru * ppet_bit_len_per_ru),
8916 					  GENMASK(ppet_bit_len_per_ru - 1, 0));
8917 
8918 			for (i = 0; i < ppet_bit_len_per_ru; i++) {
8919 				cap->eht_ppe_thres[bit / 8] |=
8920 					(((val >> i) & 0x1) << ((bit % 8)));
8921 				bit++;
8922 			}
8923 		}
8924 	}
8925 }
8926 
8927 static void
ath12k_mac_filter_eht_cap_mesh(struct ieee80211_eht_cap_elem_fixed * eht_cap_elem)8928 ath12k_mac_filter_eht_cap_mesh(struct ieee80211_eht_cap_elem_fixed
8929 			       *eht_cap_elem)
8930 {
8931 	u8 m;
8932 
8933 	m = IEEE80211_EHT_MAC_CAP0_EPCS_PRIO_ACCESS;
8934 	eht_cap_elem->mac_cap_info[0] &= ~m;
8935 
8936 	m = IEEE80211_EHT_PHY_CAP0_PARTIAL_BW_UL_MU_MIMO;
8937 	eht_cap_elem->phy_cap_info[0] &= ~m;
8938 
8939 	m = IEEE80211_EHT_PHY_CAP3_NG_16_MU_FEEDBACK |
8940 	    IEEE80211_EHT_PHY_CAP3_CODEBOOK_7_5_MU_FDBK |
8941 	    IEEE80211_EHT_PHY_CAP3_TRIG_MU_BF_PART_BW_FDBK |
8942 	    IEEE80211_EHT_PHY_CAP3_TRIG_CQI_FDBK;
8943 	eht_cap_elem->phy_cap_info[3] &= ~m;
8944 
8945 	m = IEEE80211_EHT_PHY_CAP4_PART_BW_DL_MU_MIMO |
8946 	    IEEE80211_EHT_PHY_CAP4_PSR_SR_SUPP |
8947 	    IEEE80211_EHT_PHY_CAP4_POWER_BOOST_FACT_SUPP |
8948 	    IEEE80211_EHT_PHY_CAP4_EHT_MU_PPDU_4_EHT_LTF_08_GI;
8949 	eht_cap_elem->phy_cap_info[4] &= ~m;
8950 
8951 	m = IEEE80211_EHT_PHY_CAP5_NON_TRIG_CQI_FEEDBACK |
8952 	    IEEE80211_EHT_PHY_CAP5_TX_LESS_242_TONE_RU_SUPP |
8953 	    IEEE80211_EHT_PHY_CAP5_RX_LESS_242_TONE_RU_SUPP |
8954 	    IEEE80211_EHT_PHY_CAP5_MAX_NUM_SUPP_EHT_LTF_MASK;
8955 	eht_cap_elem->phy_cap_info[5] &= ~m;
8956 
8957 	m = IEEE80211_EHT_PHY_CAP6_MAX_NUM_SUPP_EHT_LTF_MASK;
8958 	eht_cap_elem->phy_cap_info[6] &= ~m;
8959 
8960 	m = IEEE80211_EHT_PHY_CAP7_NON_OFDMA_UL_MU_MIMO_80MHZ |
8961 	    IEEE80211_EHT_PHY_CAP7_NON_OFDMA_UL_MU_MIMO_160MHZ |
8962 	    IEEE80211_EHT_PHY_CAP7_NON_OFDMA_UL_MU_MIMO_320MHZ |
8963 	    IEEE80211_EHT_PHY_CAP7_MU_BEAMFORMER_80MHZ |
8964 	    IEEE80211_EHT_PHY_CAP7_MU_BEAMFORMER_160MHZ |
8965 	    IEEE80211_EHT_PHY_CAP7_MU_BEAMFORMER_320MHZ;
8966 	eht_cap_elem->phy_cap_info[7] &= ~m;
8967 }
8968 
ath12k_mac_copy_eht_cap(struct ath12k * ar,struct ath12k_band_cap * band_cap,struct ieee80211_he_cap_elem * he_cap_elem,int iftype,struct ieee80211_sta_eht_cap * eht_cap)8969 static void ath12k_mac_copy_eht_cap(struct ath12k *ar,
8970 				    struct ath12k_band_cap *band_cap,
8971 				    struct ieee80211_he_cap_elem *he_cap_elem,
8972 				    int iftype,
8973 				    struct ieee80211_sta_eht_cap *eht_cap)
8974 {
8975 	struct ieee80211_eht_cap_elem_fixed *eht_cap_elem = &eht_cap->eht_cap_elem;
8976 
8977 	memset(eht_cap, 0, sizeof(struct ieee80211_sta_eht_cap));
8978 
8979 	if (!(test_bit(WMI_TLV_SERVICE_11BE, ar->ab->wmi_ab.svc_map)) ||
8980 	    ath12k_acpi_get_disable_11be(ar->ab))
8981 		return;
8982 
8983 	eht_cap->has_eht = true;
8984 	memcpy(eht_cap_elem->mac_cap_info, band_cap->eht_cap_mac_info,
8985 	       sizeof(eht_cap_elem->mac_cap_info));
8986 	memcpy(eht_cap_elem->phy_cap_info, band_cap->eht_cap_phy_info,
8987 	       sizeof(eht_cap_elem->phy_cap_info));
8988 
8989 	switch (iftype) {
8990 	case NL80211_IFTYPE_AP:
8991 		eht_cap_elem->phy_cap_info[0] &=
8992 			~IEEE80211_EHT_PHY_CAP0_242_TONE_RU_GT20MHZ;
8993 		eht_cap_elem->phy_cap_info[4] &=
8994 			~IEEE80211_EHT_PHY_CAP4_PART_BW_DL_MU_MIMO;
8995 		eht_cap_elem->phy_cap_info[5] &=
8996 			~IEEE80211_EHT_PHY_CAP5_TX_LESS_242_TONE_RU_SUPP;
8997 		break;
8998 	case NL80211_IFTYPE_STATION:
8999 		eht_cap_elem->phy_cap_info[7] &=
9000 			~(IEEE80211_EHT_PHY_CAP7_NON_OFDMA_UL_MU_MIMO_80MHZ |
9001 			  IEEE80211_EHT_PHY_CAP7_NON_OFDMA_UL_MU_MIMO_160MHZ |
9002 			  IEEE80211_EHT_PHY_CAP7_NON_OFDMA_UL_MU_MIMO_320MHZ);
9003 		eht_cap_elem->phy_cap_info[7] &=
9004 			~(IEEE80211_EHT_PHY_CAP7_MU_BEAMFORMER_80MHZ |
9005 			  IEEE80211_EHT_PHY_CAP7_MU_BEAMFORMER_160MHZ |
9006 			  IEEE80211_EHT_PHY_CAP7_MU_BEAMFORMER_320MHZ);
9007 		break;
9008 	case NL80211_IFTYPE_MESH_POINT:
9009 		ath12k_mac_filter_eht_cap_mesh(eht_cap_elem);
9010 		break;
9011 	default:
9012 		break;
9013 	}
9014 
9015 	ath12k_mac_copy_eht_mcs_nss(band_cap, &eht_cap->eht_mcs_nss_supp,
9016 				    he_cap_elem, eht_cap_elem);
9017 
9018 	if (eht_cap_elem->phy_cap_info[5] &
9019 	    IEEE80211_EHT_PHY_CAP5_PPE_THRESHOLD_PRESENT)
9020 		ath12k_mac_copy_eht_ppe_thresh(&band_cap->eht_ppet, eht_cap);
9021 }
9022 
ath12k_mac_copy_sband_iftype_data(struct ath12k * ar,struct ath12k_pdev_cap * cap,struct ieee80211_sband_iftype_data * data,int band)9023 static int ath12k_mac_copy_sband_iftype_data(struct ath12k *ar,
9024 					     struct ath12k_pdev_cap *cap,
9025 					     struct ieee80211_sband_iftype_data *data,
9026 					     int band)
9027 {
9028 	struct ath12k_band_cap *band_cap = &cap->band[band];
9029 	int i, idx = 0;
9030 
9031 	for (i = 0; i < NUM_NL80211_IFTYPES; i++) {
9032 		struct ieee80211_sta_he_cap *he_cap = &data[idx].he_cap;
9033 
9034 		switch (i) {
9035 		case NL80211_IFTYPE_STATION:
9036 		case NL80211_IFTYPE_AP:
9037 		case NL80211_IFTYPE_MESH_POINT:
9038 			break;
9039 
9040 		default:
9041 			continue;
9042 		}
9043 
9044 		data[idx].types_mask = BIT(i);
9045 
9046 		ath12k_mac_copy_he_cap(ar, band_cap, i, ar->num_tx_chains, he_cap);
9047 		if (band == NL80211_BAND_6GHZ) {
9048 			data[idx].he_6ghz_capa.capa =
9049 				ath12k_mac_setup_he_6ghz_cap(cap, band_cap);
9050 		}
9051 		ath12k_mac_copy_eht_cap(ar, band_cap, &he_cap->he_cap_elem, i,
9052 					&data[idx].eht_cap);
9053 		idx++;
9054 	}
9055 
9056 	return idx;
9057 }
9058 
ath12k_mac_setup_sband_iftype_data(struct ath12k * ar,struct ath12k_pdev_cap * cap)9059 static void ath12k_mac_setup_sband_iftype_data(struct ath12k *ar,
9060 					       struct ath12k_pdev_cap *cap)
9061 {
9062 	struct ieee80211_supported_band *sband;
9063 	enum nl80211_band band;
9064 	int count;
9065 
9066 	if (cap->supported_bands & WMI_HOST_WLAN_2GHZ_CAP) {
9067 		band = NL80211_BAND_2GHZ;
9068 		count = ath12k_mac_copy_sband_iftype_data(ar, cap,
9069 							  ar->mac.iftype[band],
9070 							  band);
9071 		sband = &ar->mac.sbands[band];
9072 		_ieee80211_set_sband_iftype_data(sband, ar->mac.iftype[band],
9073 						 count);
9074 	}
9075 
9076 	if (cap->supported_bands & WMI_HOST_WLAN_5GHZ_CAP) {
9077 		band = NL80211_BAND_5GHZ;
9078 		count = ath12k_mac_copy_sband_iftype_data(ar, cap,
9079 							  ar->mac.iftype[band],
9080 							  band);
9081 		sband = &ar->mac.sbands[band];
9082 		_ieee80211_set_sband_iftype_data(sband, ar->mac.iftype[band],
9083 						 count);
9084 	}
9085 
9086 	if (cap->supported_bands & WMI_HOST_WLAN_5GHZ_CAP &&
9087 	    ar->supports_6ghz) {
9088 		band = NL80211_BAND_6GHZ;
9089 		count = ath12k_mac_copy_sband_iftype_data(ar, cap,
9090 							  ar->mac.iftype[band],
9091 							  band);
9092 		sband = &ar->mac.sbands[band];
9093 		_ieee80211_set_sband_iftype_data(sband, ar->mac.iftype[band],
9094 						 count);
9095 	}
9096 }
9097 
__ath12k_set_antenna(struct ath12k * ar,u32 tx_ant,u32 rx_ant)9098 static int __ath12k_set_antenna(struct ath12k *ar, u32 tx_ant, u32 rx_ant)
9099 {
9100 	struct ath12k_hw *ah = ath12k_ar_to_ah(ar);
9101 	int ret;
9102 
9103 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
9104 
9105 	if (ath12k_check_chain_mask(ar, tx_ant, true))
9106 		return -EINVAL;
9107 
9108 	if (ath12k_check_chain_mask(ar, rx_ant, false))
9109 		return -EINVAL;
9110 
9111 	/* Since we advertised the max cap of all radios combined during wiphy
9112 	 * registration, ensure we don't set the antenna config higher than the
9113 	 * limits
9114 	 */
9115 	tx_ant = min_t(u32, tx_ant, ar->pdev->cap.tx_chain_mask);
9116 	rx_ant = min_t(u32, rx_ant, ar->pdev->cap.rx_chain_mask);
9117 
9118 	ar->cfg_tx_chainmask = tx_ant;
9119 	ar->cfg_rx_chainmask = rx_ant;
9120 
9121 	if (ah->state != ATH12K_HW_STATE_ON &&
9122 	    ah->state != ATH12K_HW_STATE_RESTARTED)
9123 		return 0;
9124 
9125 	ret = ath12k_wmi_pdev_set_param(ar, WMI_PDEV_PARAM_TX_CHAIN_MASK,
9126 					tx_ant, ar->pdev->pdev_id);
9127 	if (ret) {
9128 		ath12k_warn(ar->ab, "failed to set tx-chainmask: %d, req 0x%x\n",
9129 			    ret, tx_ant);
9130 		return ret;
9131 	}
9132 
9133 	ar->num_tx_chains = hweight32(tx_ant);
9134 
9135 	ret = ath12k_wmi_pdev_set_param(ar, WMI_PDEV_PARAM_RX_CHAIN_MASK,
9136 					rx_ant, ar->pdev->pdev_id);
9137 	if (ret) {
9138 		ath12k_warn(ar->ab, "failed to set rx-chainmask: %d, req 0x%x\n",
9139 			    ret, rx_ant);
9140 		return ret;
9141 	}
9142 
9143 	ar->num_rx_chains = hweight32(rx_ant);
9144 
9145 	/* Reload HT/VHT/HE capability */
9146 	ath12k_mac_setup_ht_vht_cap(ar, &ar->pdev->cap, NULL);
9147 	ath12k_mac_setup_sband_iftype_data(ar, &ar->pdev->cap);
9148 
9149 	return 0;
9150 }
9151 
ath12k_mgmt_over_wmi_tx_drop(struct ath12k * ar,struct sk_buff * skb)9152 static void ath12k_mgmt_over_wmi_tx_drop(struct ath12k *ar, struct sk_buff *skb)
9153 {
9154 	int num_mgmt;
9155 
9156 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
9157 
9158 	ieee80211_free_txskb(ath12k_ar_to_hw(ar), skb);
9159 
9160 	num_mgmt = atomic_dec_if_positive(&ar->num_pending_mgmt_tx);
9161 
9162 	if (num_mgmt < 0)
9163 		WARN_ON_ONCE(1);
9164 
9165 	if (!num_mgmt)
9166 		wake_up(&ar->txmgmt_empty_waitq);
9167 }
9168 
ath12k_mac_tx_mgmt_free(struct ath12k * ar,int buf_id)9169 static void ath12k_mac_tx_mgmt_free(struct ath12k *ar, int buf_id)
9170 {
9171 	struct sk_buff *msdu;
9172 	struct ieee80211_tx_info *info;
9173 
9174 	spin_lock_bh(&ar->txmgmt_idr_lock);
9175 	msdu = idr_remove(&ar->txmgmt_idr, buf_id);
9176 	spin_unlock_bh(&ar->txmgmt_idr_lock);
9177 
9178 	if (!msdu)
9179 		return;
9180 
9181 	dma_unmap_single(ar->ab->dev, ATH12K_SKB_CB(msdu)->paddr, msdu->len,
9182 			 DMA_TO_DEVICE);
9183 
9184 	info = IEEE80211_SKB_CB(msdu);
9185 	memset(&info->status, 0, sizeof(info->status));
9186 
9187 	ath12k_mgmt_over_wmi_tx_drop(ar, msdu);
9188 }
9189 
ath12k_mac_tx_mgmt_pending_free(int buf_id,void * skb,void * ctx)9190 int ath12k_mac_tx_mgmt_pending_free(int buf_id, void *skb, void *ctx)
9191 {
9192 	struct ath12k *ar = ctx;
9193 
9194 	ath12k_mac_tx_mgmt_free(ar, buf_id);
9195 
9196 	return 0;
9197 }
9198 
ath12k_mac_vif_txmgmt_idr_remove(int buf_id,void * skb,void * ctx)9199 static int ath12k_mac_vif_txmgmt_idr_remove(int buf_id, void *skb, void *ctx)
9200 {
9201 	struct ieee80211_vif *vif = ctx;
9202 	struct ath12k_skb_cb *skb_cb = ATH12K_SKB_CB(skb);
9203 	struct ath12k *ar = skb_cb->ar;
9204 
9205 	if (skb_cb->vif == vif)
9206 		ath12k_mac_tx_mgmt_free(ar, buf_id);
9207 
9208 	return 0;
9209 }
9210 
ath12k_mac_mgmt_tx_wmi(struct ath12k * ar,struct ath12k_link_vif * arvif,struct sk_buff * skb)9211 static int ath12k_mac_mgmt_tx_wmi(struct ath12k *ar, struct ath12k_link_vif *arvif,
9212 				  struct sk_buff *skb)
9213 {
9214 	struct ath12k_base *ab = ar->ab;
9215 	struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
9216 	struct ath12k_skb_cb *skb_cb = ATH12K_SKB_CB(skb);
9217 	struct ieee80211_tx_info *info;
9218 	enum hal_encrypt_type enctype;
9219 	unsigned int mic_len;
9220 	dma_addr_t paddr;
9221 	int buf_id;
9222 	int ret;
9223 
9224 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
9225 
9226 	skb_cb->ar = ar;
9227 	spin_lock_bh(&ar->txmgmt_idr_lock);
9228 	buf_id = idr_alloc(&ar->txmgmt_idr, skb, 0,
9229 			   ATH12K_TX_MGMT_NUM_PENDING_MAX, GFP_ATOMIC);
9230 	spin_unlock_bh(&ar->txmgmt_idr_lock);
9231 	if (buf_id < 0)
9232 		return -ENOSPC;
9233 
9234 	info = IEEE80211_SKB_CB(skb);
9235 	if ((skb_cb->flags & ATH12K_SKB_CIPHER_SET) &&
9236 	    !(info->flags & IEEE80211_TX_CTL_HW_80211_ENCAP)) {
9237 		if ((ieee80211_is_action(hdr->frame_control) ||
9238 		     ieee80211_is_deauth(hdr->frame_control) ||
9239 		     ieee80211_is_disassoc(hdr->frame_control)) &&
9240 		     ieee80211_has_protected(hdr->frame_control)) {
9241 			enctype = ath12k_dp_tx_get_encrypt_type(skb_cb->cipher);
9242 			mic_len = ath12k_dp_rx_crypto_mic_len(ab->dp, enctype);
9243 			skb_put(skb, mic_len);
9244 		}
9245 	}
9246 
9247 	paddr = dma_map_single(ab->dev, skb->data, skb->len, DMA_TO_DEVICE);
9248 	if (dma_mapping_error(ab->dev, paddr)) {
9249 		ath12k_warn(ab, "failed to DMA map mgmt Tx buffer\n");
9250 		ret = -EIO;
9251 		goto err_free_idr;
9252 	}
9253 
9254 	skb_cb->paddr = paddr;
9255 
9256 	ret = ath12k_wmi_mgmt_send(arvif, buf_id, skb);
9257 	if (ret) {
9258 		ath12k_warn(ar->ab, "failed to send mgmt frame: %d\n", ret);
9259 		goto err_unmap_buf;
9260 	}
9261 
9262 	return 0;
9263 
9264 err_unmap_buf:
9265 	dma_unmap_single(ab->dev, skb_cb->paddr,
9266 			 skb->len, DMA_TO_DEVICE);
9267 err_free_idr:
9268 	spin_lock_bh(&ar->txmgmt_idr_lock);
9269 	idr_remove(&ar->txmgmt_idr, buf_id);
9270 	spin_unlock_bh(&ar->txmgmt_idr_lock);
9271 
9272 	return ret;
9273 }
9274 
ath12k_mgmt_over_wmi_tx_purge(struct ath12k * ar)9275 static void ath12k_mgmt_over_wmi_tx_purge(struct ath12k *ar)
9276 {
9277 	struct sk_buff *skb;
9278 
9279 	while ((skb = skb_dequeue(&ar->wmi_mgmt_tx_queue)) != NULL)
9280 		ath12k_mgmt_over_wmi_tx_drop(ar, skb);
9281 }
9282 
ath12k_mac_mgmt_action_frame_fill_elem_data(struct ath12k_link_vif * arvif,struct sk_buff * skb)9283 static int ath12k_mac_mgmt_action_frame_fill_elem_data(struct ath12k_link_vif *arvif,
9284 						       struct sk_buff *skb)
9285 {
9286 	struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
9287 	u8 category, *buf, iv_len, action_code, dialog_token;
9288 	struct ieee80211_bss_conf *link_conf;
9289 	struct ieee80211_chanctx_conf *conf;
9290 	int cur_tx_power, max_tx_power;
9291 	struct ath12k *ar = arvif->ar;
9292 	struct ieee80211_hw *hw = ath12k_ar_to_hw(ar);
9293 	struct wiphy *wiphy = hw->wiphy;
9294 	struct ath12k_skb_cb *skb_cb;
9295 	struct ieee80211_mgmt *mgmt;
9296 	unsigned int remaining_len;
9297 	bool has_protected;
9298 
9299 	lockdep_assert_wiphy(wiphy);
9300 
9301 	/* make sure category field is present */
9302 	if (skb->len < IEEE80211_MIN_ACTION_SIZE(category))
9303 		return -EINVAL;
9304 
9305 	remaining_len = skb->len - IEEE80211_MIN_ACTION_SIZE(category);
9306 	has_protected = ieee80211_has_protected(hdr->frame_control);
9307 
9308 	/* In case of SW crypto and hdr protected (PMF), packet will already be encrypted,
9309 	 * we can't put in data in this case
9310 	 */
9311 	if (test_bit(ATH12K_FLAG_HW_CRYPTO_DISABLED, &ar->ab->dev_flags) &&
9312 	    has_protected)
9313 		return 0;
9314 
9315 	mgmt = (struct ieee80211_mgmt *)hdr;
9316 	buf = (u8 *)&mgmt->u.action;
9317 
9318 	/* FCTL_PROTECTED frame might have extra space added for HDR_LEN. Offset that
9319 	 * many bytes if it is there
9320 	 */
9321 	if (has_protected) {
9322 		skb_cb = ATH12K_SKB_CB(skb);
9323 
9324 		switch (skb_cb->cipher) {
9325 		/* Cipher suite having flag %IEEE80211_KEY_FLAG_GENERATE_IV_MGMT set in
9326 		 * key needs to be processed. See ath12k_install_key()
9327 		 */
9328 		case WLAN_CIPHER_SUITE_CCMP:
9329 		case WLAN_CIPHER_SUITE_CCMP_256:
9330 		case WLAN_CIPHER_SUITE_GCMP:
9331 		case WLAN_CIPHER_SUITE_GCMP_256:
9332 			iv_len = IEEE80211_CCMP_HDR_LEN;
9333 			break;
9334 		case WLAN_CIPHER_SUITE_TKIP:
9335 			iv_len = 0;
9336 			break;
9337 		default:
9338 			return -EINVAL;
9339 		}
9340 
9341 		if (remaining_len < iv_len)
9342 			return -EINVAL;
9343 
9344 		buf += iv_len;
9345 		remaining_len -= iv_len;
9346 	}
9347 
9348 	category = *buf++;
9349 	/* category code is already taken care in %IEEE80211_MIN_ACTION_SIZE hence
9350 	 * no need to adjust remaining_len
9351 	 */
9352 
9353 	switch (category) {
9354 	case WLAN_CATEGORY_RADIO_MEASUREMENT:
9355 		/* need action code and dialog token */
9356 		if (remaining_len < 2)
9357 			return -EINVAL;
9358 
9359 		/* Packet Format:
9360 		 *	Action Code | Dialog Token | Variable Len (based on Action Code)
9361 		 */
9362 		action_code = *buf++;
9363 		dialog_token = *buf++;
9364 		remaining_len -= 2;
9365 
9366 		link_conf = ath12k_mac_get_link_bss_conf(arvif);
9367 		if (!link_conf) {
9368 			ath12k_warn(ar->ab,
9369 				    "failed to get bss link conf for vdev %d in RM handling\n",
9370 				    arvif->vdev_id);
9371 			return -EINVAL;
9372 		}
9373 
9374 		conf = wiphy_dereference(wiphy, link_conf->chanctx_conf);
9375 		if (!conf)
9376 			return -ENOENT;
9377 
9378 		cur_tx_power = link_conf->txpower;
9379 		max_tx_power = min(conf->def.chan->max_reg_power,
9380 				   (int)ar->max_tx_power / 2);
9381 
9382 		ath12k_mac_op_get_txpower(hw, arvif->ahvif->vif, arvif->link_id,
9383 					  &cur_tx_power);
9384 
9385 		switch (action_code) {
9386 		case WLAN_RM_ACTION_LINK_MEASUREMENT_REQUEST:
9387 			/* need variable fields to be present in len */
9388 			if (remaining_len < 2)
9389 				return -EINVAL;
9390 
9391 			/* Variable length format as defined in IEEE 802.11-2024,
9392 			 * Figure 9-1187-Link Measurement Request frame Action field
9393 			 * format.
9394 			 *	Transmit Power | Max Tx Power
9395 			 * We fill both of these.
9396 			 */
9397 			*buf++ = cur_tx_power;
9398 			*buf = max_tx_power;
9399 
9400 			ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
9401 				   "RRM: Link Measurement Req dialog_token %u cur_tx_power %d max_tx_power %d\n",
9402 				   dialog_token, cur_tx_power, max_tx_power);
9403 			break;
9404 		case WLAN_RM_ACTION_LINK_MEASUREMENT_REPORT:
9405 			/* need variable fields to be present in len */
9406 			if (remaining_len < 3)
9407 				return -EINVAL;
9408 
9409 			/* Variable length format as defined in IEEE 802.11-2024,
9410 			 * Figure 9-1188-Link Measurement Report frame Action field format
9411 			 *	TPC Report | Variable Fields
9412 			 *
9413 			 * TPC Report Format:
9414 			 *	Element ID | Len | Tx Power | Link Margin
9415 			 *
9416 			 * We fill Tx power in the TPC Report (2nd index)
9417 			 */
9418 			buf[2] = cur_tx_power;
9419 
9420 			/* TODO: At present, Link margin data is not present so can't
9421 			 * really fill it now. Once it is available, it can be added
9422 			 * here
9423 			 */
9424 			ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
9425 				   "RRM: Link Measurement Report dialog_token %u cur_tx_power %d\n",
9426 				   dialog_token, cur_tx_power);
9427 			break;
9428 		default:
9429 			return -EINVAL;
9430 		}
9431 		break;
9432 	default:
9433 		/* nothing to fill */
9434 		return 0;
9435 	}
9436 
9437 	return 0;
9438 }
9439 
ath12k_mac_mgmt_frame_fill_elem_data(struct ath12k_link_vif * arvif,struct sk_buff * skb)9440 static int ath12k_mac_mgmt_frame_fill_elem_data(struct ath12k_link_vif *arvif,
9441 						struct sk_buff *skb)
9442 {
9443 	struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
9444 
9445 	if (!ieee80211_is_action(hdr->frame_control))
9446 		return 0;
9447 
9448 	return ath12k_mac_mgmt_action_frame_fill_elem_data(arvif, skb);
9449 }
9450 
ath12k_mgmt_over_wmi_tx_work(struct wiphy * wiphy,struct wiphy_work * work)9451 static void ath12k_mgmt_over_wmi_tx_work(struct wiphy *wiphy, struct wiphy_work *work)
9452 {
9453 	struct ath12k *ar = container_of(work, struct ath12k, wmi_mgmt_tx_work);
9454 	struct ath12k_hw *ah = ar->ah;
9455 	struct ath12k_skb_cb *skb_cb;
9456 	struct ath12k_vif *ahvif;
9457 	struct ath12k_link_vif *arvif;
9458 	struct sk_buff *skb;
9459 	int ret;
9460 
9461 	lockdep_assert_wiphy(wiphy);
9462 
9463 	while ((skb = skb_dequeue(&ar->wmi_mgmt_tx_queue)) != NULL) {
9464 		skb_cb = ATH12K_SKB_CB(skb);
9465 		if (!skb_cb->vif) {
9466 			ath12k_warn(ar->ab, "no vif found for mgmt frame\n");
9467 			ath12k_mgmt_over_wmi_tx_drop(ar, skb);
9468 			continue;
9469 		}
9470 
9471 		ahvif = ath12k_vif_to_ahvif(skb_cb->vif);
9472 		if (!(ahvif->links_map & BIT(skb_cb->link_id))) {
9473 			ath12k_warn(ar->ab,
9474 				    "invalid linkid %u in mgmt over wmi tx with linkmap 0x%x\n",
9475 				    skb_cb->link_id, ahvif->links_map);
9476 			ath12k_mgmt_over_wmi_tx_drop(ar, skb);
9477 			continue;
9478 		}
9479 
9480 		arvif = wiphy_dereference(ah->hw->wiphy, ahvif->link[skb_cb->link_id]);
9481 		if (ar->allocated_vdev_map & (1LL << arvif->vdev_id)) {
9482 			/* Fill in the data which is required to be filled by the driver
9483 			 * For example: Max Tx power in Link Measurement Request/Report
9484 			 */
9485 			ret = ath12k_mac_mgmt_frame_fill_elem_data(arvif, skb);
9486 			if (ret) {
9487 				/* If we couldn't fill the data due to any reason,
9488 				 * let's not discard transmitting the packet.
9489 				 * For example: Software crypto and PMF case
9490 				 */
9491 				ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
9492 					   "Failed to fill the required data for the mgmt packet err %d\n",
9493 					   ret);
9494 			}
9495 
9496 			ret = ath12k_mac_mgmt_tx_wmi(ar, arvif, skb);
9497 			if (ret) {
9498 				ath12k_warn(ar->ab, "failed to tx mgmt frame, vdev_id %d :%d\n",
9499 					    arvif->vdev_id, ret);
9500 				ath12k_mgmt_over_wmi_tx_drop(ar, skb);
9501 			}
9502 		} else {
9503 			ath12k_warn(ar->ab,
9504 				    "dropping mgmt frame for vdev %d link %u is_started %d\n",
9505 				    arvif->vdev_id,
9506 				    skb_cb->link_id,
9507 				    arvif->is_started);
9508 			ath12k_mgmt_over_wmi_tx_drop(ar, skb);
9509 		}
9510 	}
9511 }
9512 
ath12k_mac_mgmt_tx(struct ath12k * ar,struct sk_buff * skb,bool is_prb_rsp)9513 int ath12k_mac_mgmt_tx(struct ath12k *ar, struct sk_buff *skb,
9514 		       bool is_prb_rsp)
9515 {
9516 	struct sk_buff_head *q = &ar->wmi_mgmt_tx_queue;
9517 
9518 	if (test_bit(ATH12K_FLAG_CRASH_FLUSH, &ar->ab->dev_flags))
9519 		return -ESHUTDOWN;
9520 
9521 	/* Drop probe response packets when the pending management tx
9522 	 * count has reached a certain threshold, so as to prioritize
9523 	 * other mgmt packets like auth and assoc to be sent on time
9524 	 * for establishing successful connections.
9525 	 */
9526 	if (is_prb_rsp &&
9527 	    atomic_read(&ar->num_pending_mgmt_tx) > ATH12K_PRB_RSP_DROP_THRESHOLD) {
9528 		ath12k_warn(ar->ab,
9529 			    "dropping probe response as pending queue is almost full\n");
9530 		return -ENOSPC;
9531 	}
9532 
9533 	if (skb_queue_len_lockless(q) >= ATH12K_TX_MGMT_NUM_PENDING_MAX) {
9534 		ath12k_warn(ar->ab, "mgmt tx queue is full\n");
9535 		return -ENOSPC;
9536 	}
9537 
9538 	skb_queue_tail(q, skb);
9539 	atomic_inc(&ar->num_pending_mgmt_tx);
9540 	wiphy_work_queue(ath12k_ar_to_hw(ar)->wiphy, &ar->wmi_mgmt_tx_work);
9541 
9542 	return 0;
9543 }
9544 EXPORT_SYMBOL(ath12k_mac_mgmt_tx);
9545 
ath12k_mac_add_p2p_noa_ie(struct ath12k * ar,struct ieee80211_vif * vif,struct sk_buff * skb,bool is_prb_rsp)9546 void ath12k_mac_add_p2p_noa_ie(struct ath12k *ar,
9547 			       struct ieee80211_vif *vif,
9548 			       struct sk_buff *skb,
9549 			       bool is_prb_rsp)
9550 {
9551 	struct ath12k_vif *ahvif = ath12k_vif_to_ahvif(vif);
9552 
9553 	if (likely(!is_prb_rsp))
9554 		return;
9555 
9556 	spin_lock_bh(&ar->data_lock);
9557 
9558 	if (ahvif->u.ap.noa_data &&
9559 	    !pskb_expand_head(skb, 0, ahvif->u.ap.noa_len,
9560 			      GFP_ATOMIC))
9561 		skb_put_data(skb, ahvif->u.ap.noa_data,
9562 			     ahvif->u.ap.noa_len);
9563 
9564 	spin_unlock_bh(&ar->data_lock);
9565 }
9566 EXPORT_SYMBOL(ath12k_mac_add_p2p_noa_ie);
9567 
9568 /* Note: called under rcu_read_lock() */
ath12k_mlo_mcast_update_tx_link_address(struct ieee80211_vif * vif,u8 link_id,struct sk_buff * skb,u32 info_flags)9569 void ath12k_mlo_mcast_update_tx_link_address(struct ieee80211_vif *vif,
9570 					     u8 link_id, struct sk_buff *skb,
9571 					     u32 info_flags)
9572 {
9573 	struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
9574 	struct ieee80211_bss_conf *bss_conf;
9575 
9576 	if (info_flags & IEEE80211_TX_CTL_HW_80211_ENCAP)
9577 		return;
9578 
9579 	bss_conf = rcu_dereference(vif->link_conf[link_id]);
9580 	if (bss_conf)
9581 		ether_addr_copy(hdr->addr2, bss_conf->addr);
9582 }
9583 EXPORT_SYMBOL(ath12k_mlo_mcast_update_tx_link_address);
9584 
9585 /* Note: called under rcu_read_lock() */
ath12k_mac_get_tx_link(struct ieee80211_sta * sta,struct ieee80211_vif * vif,u8 link,struct sk_buff * skb,u32 info_flags)9586 u8 ath12k_mac_get_tx_link(struct ieee80211_sta *sta, struct ieee80211_vif *vif,
9587 			  u8 link, struct sk_buff *skb, u32 info_flags)
9588 {
9589 	struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
9590 	struct ath12k_vif *ahvif = ath12k_vif_to_ahvif(vif);
9591 	struct ieee80211_link_sta *link_sta;
9592 	struct ieee80211_bss_conf *bss_conf;
9593 	struct ath12k_sta *ahsta;
9594 
9595 	/* Use the link id passed or the default vif link */
9596 	if (!sta) {
9597 		if (link != IEEE80211_LINK_UNSPECIFIED)
9598 			return link;
9599 
9600 		return ahvif->deflink.link_id;
9601 	}
9602 
9603 	ahsta = ath12k_sta_to_ahsta(sta);
9604 
9605 	/* Below translation ensures we pass proper A2 & A3 for non ML clients.
9606 	 * Also it assumes for now support only for MLO AP in this path
9607 	 */
9608 	if (!sta->mlo) {
9609 		link = ahsta->deflink.link_id;
9610 
9611 		if (info_flags & IEEE80211_TX_CTL_HW_80211_ENCAP)
9612 			return link;
9613 
9614 		bss_conf = rcu_dereference(vif->link_conf[link]);
9615 		if (bss_conf) {
9616 			ether_addr_copy(hdr->addr2, bss_conf->addr);
9617 			if (!ieee80211_has_tods(hdr->frame_control) &&
9618 			    !ieee80211_has_fromds(hdr->frame_control))
9619 				ether_addr_copy(hdr->addr3, bss_conf->addr);
9620 		}
9621 
9622 		return link;
9623 	}
9624 
9625 	/* enqueue eth enacap & data frames on primary link, FW does link
9626 	 * selection and address translation.
9627 	 */
9628 	if (info_flags & IEEE80211_TX_CTL_HW_80211_ENCAP ||
9629 	    ieee80211_is_data(hdr->frame_control))
9630 		return ahsta->assoc_link_id;
9631 
9632 	/* 802.11 frame cases */
9633 	if (link == IEEE80211_LINK_UNSPECIFIED)
9634 		link = ahsta->deflink.link_id;
9635 
9636 	if (!ieee80211_is_mgmt(hdr->frame_control))
9637 		return link;
9638 
9639 	/* Perform address conversion for ML STA Tx */
9640 	bss_conf = rcu_dereference(vif->link_conf[link]);
9641 	link_sta = rcu_dereference(sta->link[link]);
9642 
9643 	if (bss_conf && link_sta) {
9644 		ether_addr_copy(hdr->addr1, link_sta->addr);
9645 		ether_addr_copy(hdr->addr2, bss_conf->addr);
9646 
9647 		if (vif->type == NL80211_IFTYPE_STATION && bss_conf->bssid)
9648 			ether_addr_copy(hdr->addr3, bss_conf->bssid);
9649 		else if (vif->type == NL80211_IFTYPE_AP)
9650 			ether_addr_copy(hdr->addr3, bss_conf->addr);
9651 
9652 		return link;
9653 	}
9654 
9655 	if (bss_conf) {
9656 		/* In certain cases where a ML sta associated and added subset of
9657 		 * links on which the ML AP is active, but now sends some frame
9658 		 * (ex. Probe request) on a different link which is active in our
9659 		 * MLD but was not added during previous association, we can
9660 		 * still honor the Tx to that ML STA via the requested link.
9661 		 * The control would reach here in such case only when that link
9662 		 * address is same as the MLD address or in worst case clients
9663 		 * used MLD address at TA wrongly which would have helped
9664 		 * identify the ML sta object and pass it here.
9665 		 * If the link address of that STA is different from MLD address,
9666 		 * then the sta object would be NULL and control won't reach
9667 		 * here but return at the start of the function itself with !sta
9668 		 * check. Also this would not need any translation at hdr->addr1
9669 		 * from MLD to link address since the RA is the MLD address
9670 		 * (same as that link address ideally) already.
9671 		 */
9672 		ether_addr_copy(hdr->addr2, bss_conf->addr);
9673 
9674 		if (vif->type == NL80211_IFTYPE_STATION && bss_conf->bssid)
9675 			ether_addr_copy(hdr->addr3, bss_conf->bssid);
9676 		else if (vif->type == NL80211_IFTYPE_AP)
9677 			ether_addr_copy(hdr->addr3, bss_conf->addr);
9678 	}
9679 
9680 	return link;
9681 }
9682 EXPORT_SYMBOL(ath12k_mac_get_tx_link);
9683 
ath12k_mac_drain_tx(struct ath12k * ar)9684 void ath12k_mac_drain_tx(struct ath12k *ar)
9685 {
9686 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
9687 
9688 	/* make sure rcu-protected mac80211 tx path itself is drained */
9689 	synchronize_net();
9690 
9691 	wiphy_work_cancel(ath12k_ar_to_hw(ar)->wiphy, &ar->wmi_mgmt_tx_work);
9692 	ath12k_mgmt_over_wmi_tx_purge(ar);
9693 }
9694 
ath12k_mac_config_mon_status_default(struct ath12k * ar,bool enable)9695 static int ath12k_mac_config_mon_status_default(struct ath12k *ar, bool enable)
9696 {
9697 	struct htt_rx_ring_tlv_filter tlv_filter = {};
9698 	struct ath12k_base *ab = ar->ab;
9699 	u32 ring_id, i;
9700 	int ret = 0;
9701 
9702 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
9703 
9704 	if (!ab->hw_params->rxdma1_enable)
9705 		return ret;
9706 
9707 	if (enable) {
9708 		tlv_filter = ath12k_mac_mon_status_filter_default;
9709 
9710 		if (ath12k_debugfs_rx_filter(ar))
9711 			tlv_filter.rx_filter = ath12k_debugfs_rx_filter(ar);
9712 	} else {
9713 		tlv_filter.rxmon_disable = true;
9714 	}
9715 
9716 	for (i = 0; i < ab->hw_params->num_rxdma_per_pdev; i++) {
9717 		ring_id = ar->dp.rxdma_mon_dst_ring[i].ring_id;
9718 		ret = ath12k_dp_tx_htt_rx_filter_setup(ab, ring_id,
9719 						       ar->dp.mac_id + i,
9720 						       HAL_RXDMA_MONITOR_DST,
9721 						       DP_RXDMA_REFILL_RING_SIZE,
9722 						       &tlv_filter);
9723 		if (ret) {
9724 			ath12k_err(ab,
9725 				   "failed to setup filter for monitor buf %d\n",
9726 				   ret);
9727 		}
9728 	}
9729 
9730 	return ret;
9731 }
9732 
ath12k_mac_start(struct ath12k * ar)9733 static int ath12k_mac_start(struct ath12k *ar)
9734 {
9735 	struct ath12k_hw *ah = ar->ah;
9736 	struct ath12k_base *ab = ar->ab;
9737 	struct ath12k_pdev *pdev = ar->pdev;
9738 	int ret;
9739 
9740 	lockdep_assert_held(&ah->hw_mutex);
9741 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
9742 
9743 	ret = ath12k_wmi_pdev_set_param(ar, WMI_PDEV_PARAM_PMF_QOS,
9744 					1, pdev->pdev_id);
9745 
9746 	if (ret) {
9747 		ath12k_err(ab, "failed to enable PMF QOS: %d\n", ret);
9748 		goto err;
9749 	}
9750 
9751 	ret = ath12k_wmi_pdev_set_param(ar, WMI_PDEV_PARAM_DYNAMIC_BW, 1,
9752 					pdev->pdev_id);
9753 	if (ret) {
9754 		ath12k_err(ab, "failed to enable dynamic bw: %d\n", ret);
9755 		goto err;
9756 	}
9757 
9758 	ret = ath12k_wmi_pdev_set_param(ar, WMI_PDEV_PARAM_ARP_AC_OVERRIDE,
9759 					0, pdev->pdev_id);
9760 	if (ret) {
9761 		ath12k_err(ab, "failed to set ac override for ARP: %d\n",
9762 			   ret);
9763 		goto err;
9764 	}
9765 
9766 	ret = ath12k_wmi_send_dfs_phyerr_offload_enable_cmd(ar, pdev->pdev_id);
9767 	if (ret) {
9768 		ath12k_err(ab, "failed to offload radar detection: %d\n",
9769 			   ret);
9770 		goto err;
9771 	}
9772 
9773 	ret = ath12k_dp_tx_htt_h2t_ppdu_stats_req(ar,
9774 						  HTT_PPDU_STATS_TAG_DEFAULT);
9775 	if (ret) {
9776 		ath12k_err(ab, "failed to req ppdu stats: %d\n", ret);
9777 		goto err;
9778 	}
9779 
9780 	ret = ath12k_wmi_pdev_set_param(ar, WMI_PDEV_PARAM_MESH_MCAST_ENABLE,
9781 					1, pdev->pdev_id);
9782 
9783 	if (ret) {
9784 		ath12k_err(ab, "failed to enable MESH MCAST ENABLE: (%d\n", ret);
9785 		goto err;
9786 	}
9787 
9788 	if (ab->hw_params->supports_cong_ctrl_max_msdus) {
9789 		ret = ath12k_wmi_pdev_set_param(ar,
9790 						WMI_PDEV_PARAM_SET_CONG_CTRL_MAX_MSDUS,
9791 						ATH12K_NUM_POOL_TX_DESC(ab),
9792 						pdev->pdev_id);
9793 		if (ret) {
9794 			ath12k_err(ab,
9795 				   "failed to set congestion control MAX MSDUS: %d\n",
9796 				   ret);
9797 			goto err;
9798 		}
9799 	}
9800 
9801 	__ath12k_set_antenna(ar, ar->cfg_tx_chainmask, ar->cfg_rx_chainmask);
9802 
9803 	/* TODO: Do we need to enable ANI? */
9804 
9805 	ret = ath12k_reg_update_chan_list(ar, false);
9806 
9807 	/* The ar state alone can be turned off for non supported country
9808 	 * without returning the error value. As we need to update the channel
9809 	 * for the next ar.
9810 	 */
9811 	if (ret) {
9812 		if (ret == -EINVAL)
9813 			ret = 0;
9814 		goto err;
9815 	}
9816 
9817 	ar->num_started_vdevs = 0;
9818 	ar->num_created_vdevs = 0;
9819 	ar->num_peers = 0;
9820 	ar->allocated_vdev_map = 0;
9821 	ar->chan_tx_pwr = ATH12K_PDEV_TX_POWER_INVALID;
9822 
9823 	spin_lock_bh(&ar->data_lock);
9824 	ar->incumbent_signal_interference.handling_in_progress = false;
9825 	spin_unlock_bh(&ar->data_lock);
9826 
9827 	/* Configure monitor status ring with default rx_filter to get rx status
9828 	 * such as rssi, rx_duration.
9829 	 */
9830 	ret = ath12k_mac_config_mon_status_default(ar, true);
9831 	if (ret && (ret != -EOPNOTSUPP)) {
9832 		ath12k_err(ab, "failed to configure monitor status ring with default rx_filter: (%d)\n",
9833 			   ret);
9834 		goto err;
9835 	}
9836 
9837 	if (ret == -EOPNOTSUPP)
9838 		ath12k_dbg(ab, ATH12K_DBG_MAC,
9839 			   "monitor status config is not yet supported");
9840 
9841 	/* Configure the hash seed for hash based reo dest ring selection */
9842 	ath12k_wmi_pdev_lro_cfg(ar, ar->pdev->pdev_id);
9843 
9844 	/* allow device to enter IMPS */
9845 	if (ab->hw_params->idle_ps) {
9846 		ret = ath12k_wmi_pdev_set_param(ar, WMI_PDEV_PARAM_IDLE_PS_CONFIG,
9847 						1, pdev->pdev_id);
9848 		if (ret) {
9849 			ath12k_err(ab, "failed to enable idle ps: %d\n", ret);
9850 			goto err;
9851 		}
9852 	}
9853 
9854 	ret = ath12k_thermal_throttling_config_default(ar);
9855 	if (ret) {
9856 		ath12k_err(ab, "failed to set thermal throttle: %d\n", ret);
9857 		goto err;
9858 	}
9859 
9860 	rcu_assign_pointer(ab->pdevs_active[ar->pdev_idx],
9861 			   &ab->pdevs[ar->pdev_idx]);
9862 
9863 	return 0;
9864 err:
9865 
9866 	return ret;
9867 }
9868 
ath12k_drain_tx(struct ath12k_hw * ah)9869 static void ath12k_drain_tx(struct ath12k_hw *ah)
9870 {
9871 	struct ath12k *ar;
9872 	int i;
9873 
9874 	lockdep_assert_wiphy(ah->hw->wiphy);
9875 
9876 	for_each_ar(ah, ar, i)
9877 		ath12k_mac_drain_tx(ar);
9878 }
9879 
ath12k_mac_op_start(struct ieee80211_hw * hw)9880 int ath12k_mac_op_start(struct ieee80211_hw *hw)
9881 {
9882 	struct ath12k_hw *ah = ath12k_hw_to_ah(hw);
9883 	struct ath12k *ar;
9884 	int ret, i;
9885 
9886 	if (ath12k_ftm_mode)
9887 		return -EPERM;
9888 
9889 	lockdep_assert_wiphy(hw->wiphy);
9890 
9891 	ath12k_drain_tx(ah);
9892 
9893 	guard(mutex)(&ah->hw_mutex);
9894 
9895 	switch (ah->state) {
9896 	case ATH12K_HW_STATE_OFF:
9897 		ah->state = ATH12K_HW_STATE_ON;
9898 		break;
9899 	case ATH12K_HW_STATE_RESTARTING:
9900 		ah->state = ATH12K_HW_STATE_RESTARTED;
9901 		break;
9902 	case ATH12K_HW_STATE_RESTARTED:
9903 	case ATH12K_HW_STATE_WEDGED:
9904 	case ATH12K_HW_STATE_ON:
9905 	case ATH12K_HW_STATE_TM:
9906 		ah->state = ATH12K_HW_STATE_OFF;
9907 
9908 		WARN_ON(1);
9909 		return -EINVAL;
9910 	}
9911 
9912 	for_each_ar(ah, ar, i) {
9913 		ret = ath12k_mac_start(ar);
9914 		if (ret) {
9915 			ah->state = ATH12K_HW_STATE_OFF;
9916 
9917 			ath12k_err(ar->ab, "fail to start mac operations in pdev idx %d ret %d\n",
9918 				   ar->pdev_idx, ret);
9919 			goto fail_start;
9920 		}
9921 	}
9922 
9923 	return 0;
9924 
9925 fail_start:
9926 	for (; i > 0; i--) {
9927 		ar = ath12k_ah_to_ar(ah, i - 1);
9928 		ath12k_mac_stop(ar);
9929 	}
9930 
9931 	return ret;
9932 }
9933 EXPORT_SYMBOL(ath12k_mac_op_start);
9934 
ath12k_mac_rfkill_config(struct ath12k * ar)9935 int ath12k_mac_rfkill_config(struct ath12k *ar)
9936 {
9937 	struct ath12k_base *ab = ar->ab;
9938 	u32 param;
9939 	int ret;
9940 
9941 	if (ab->hw_params->rfkill_pin == 0)
9942 		return -EOPNOTSUPP;
9943 
9944 	ath12k_dbg(ab, ATH12K_DBG_MAC,
9945 		   "mac rfkill_pin %d rfkill_cfg %d rfkill_on_level %d",
9946 		   ab->hw_params->rfkill_pin, ab->hw_params->rfkill_cfg,
9947 		   ab->hw_params->rfkill_on_level);
9948 
9949 	param = u32_encode_bits(ab->hw_params->rfkill_on_level,
9950 				WMI_RFKILL_CFG_RADIO_LEVEL) |
9951 		u32_encode_bits(ab->hw_params->rfkill_pin,
9952 				WMI_RFKILL_CFG_GPIO_PIN_NUM) |
9953 		u32_encode_bits(ab->hw_params->rfkill_cfg,
9954 				WMI_RFKILL_CFG_PIN_AS_GPIO);
9955 
9956 	ret = ath12k_wmi_pdev_set_param(ar, WMI_PDEV_PARAM_HW_RFKILL_CONFIG,
9957 					param, ar->pdev->pdev_id);
9958 	if (ret) {
9959 		ath12k_warn(ab,
9960 			    "failed to set rfkill config 0x%x: %d\n",
9961 			    param, ret);
9962 		return ret;
9963 	}
9964 
9965 	return 0;
9966 }
9967 
ath12k_mac_rfkill_enable_radio(struct ath12k * ar,bool enable)9968 int ath12k_mac_rfkill_enable_radio(struct ath12k *ar, bool enable)
9969 {
9970 	enum wmi_rfkill_enable_radio param;
9971 	int ret;
9972 
9973 	if (enable)
9974 		param = WMI_RFKILL_ENABLE_RADIO_ON;
9975 	else
9976 		param = WMI_RFKILL_ENABLE_RADIO_OFF;
9977 
9978 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac %d rfkill enable %d",
9979 		   ar->pdev_idx, param);
9980 
9981 	ret = ath12k_wmi_pdev_set_param(ar, WMI_PDEV_PARAM_RFKILL_ENABLE,
9982 					param, ar->pdev->pdev_id);
9983 	if (ret) {
9984 		ath12k_warn(ar->ab, "failed to set rfkill enable param %d: %d\n",
9985 			    param, ret);
9986 		return ret;
9987 	}
9988 
9989 	return 0;
9990 }
9991 
ath12k_mac_stop(struct ath12k * ar)9992 static void ath12k_mac_stop(struct ath12k *ar)
9993 {
9994 	struct ath12k_pdev_dp *dp_pdev = &ar->dp;
9995 	struct ath12k_hw *ah = ar->ah;
9996 	struct htt_ppdu_stats_info *ppdu_stats, *tmp;
9997 	struct ath12k_wmi_scan_chan_list_arg *arg;
9998 	int ret;
9999 
10000 	lockdep_assert_held(&ah->hw_mutex);
10001 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
10002 
10003 	ret = ath12k_mac_config_mon_status_default(ar, false);
10004 	if (ret && (ret != -EOPNOTSUPP))
10005 		ath12k_err(ar->ab, "failed to clear rx_filter for monitor status ring: (%d)\n",
10006 			   ret);
10007 
10008 	clear_bit(ATH12K_FLAG_CAC_RUNNING, &ar->dev_flags);
10009 
10010 	cancel_delayed_work_sync(&ar->scan.timeout);
10011 	wiphy_work_cancel(ath12k_ar_to_hw(ar)->wiphy, &ar->scan.vdev_clean_wk);
10012 	cancel_work_sync(&ar->regd_channel_update_work);
10013 	cancel_work_sync(&ar->regd_update_work);
10014 	cancel_work_sync(&ar->ab->rfkill_work);
10015 	cancel_work_sync(&ar->ab->update_11d_work);
10016 	ar->state_11d = ATH12K_11D_IDLE;
10017 	complete(&ar->completed_11d_scan);
10018 
10019 	spin_lock_bh(&dp_pdev->ppdu_list_lock);
10020 	list_for_each_entry_safe(ppdu_stats, tmp, &dp_pdev->ppdu_stats_info, list) {
10021 		list_del(&ppdu_stats->list);
10022 		kfree(ppdu_stats);
10023 	}
10024 	spin_unlock_bh(&dp_pdev->ppdu_list_lock);
10025 
10026 	spin_lock_bh(&ar->data_lock);
10027 	while ((arg = list_first_entry_or_null(&ar->regd_channel_update_queue,
10028 					       struct ath12k_wmi_scan_chan_list_arg,
10029 					       list))) {
10030 		list_del(&arg->list);
10031 		kfree(arg);
10032 	}
10033 	spin_unlock_bh(&ar->data_lock);
10034 
10035 	rcu_assign_pointer(ar->ab->pdevs_active[ar->pdev_idx], NULL);
10036 
10037 	synchronize_rcu();
10038 
10039 	atomic_set(&ar->num_pending_mgmt_tx, 0);
10040 
10041 	spin_lock_bh(&ar->data_lock);
10042 	ar->incumbent_signal_interference.handling_in_progress = false;
10043 	spin_unlock_bh(&ar->data_lock);
10044 }
10045 
ath12k_mac_op_stop(struct ieee80211_hw * hw,bool suspend)10046 void ath12k_mac_op_stop(struct ieee80211_hw *hw, bool suspend)
10047 {
10048 	struct ath12k_hw *ah = ath12k_hw_to_ah(hw);
10049 	struct ath12k *ar;
10050 	int i;
10051 
10052 	lockdep_assert_wiphy(hw->wiphy);
10053 
10054 	ath12k_drain_tx(ah);
10055 
10056 	mutex_lock(&ah->hw_mutex);
10057 
10058 	ah->state = ATH12K_HW_STATE_OFF;
10059 
10060 	for_each_ar(ah, ar, i)
10061 		ath12k_mac_stop(ar);
10062 
10063 	mutex_unlock(&ah->hw_mutex);
10064 }
10065 EXPORT_SYMBOL(ath12k_mac_op_stop);
10066 
10067 static u8
ath12k_mac_get_vdev_stats_id(struct ath12k_link_vif * arvif)10068 ath12k_mac_get_vdev_stats_id(struct ath12k_link_vif *arvif)
10069 {
10070 	struct ath12k_base *ab = arvif->ar->ab;
10071 	u8 vdev_stats_id = 0;
10072 
10073 	do {
10074 		if (ab->free_vdev_stats_id_map & (1LL << vdev_stats_id)) {
10075 			vdev_stats_id++;
10076 			if (vdev_stats_id >= ATH12K_MAX_VDEV_STATS_ID) {
10077 				vdev_stats_id = ATH12K_INVAL_VDEV_STATS_ID;
10078 				break;
10079 			}
10080 		} else {
10081 			ab->free_vdev_stats_id_map |= (1LL << vdev_stats_id);
10082 			break;
10083 		}
10084 	} while (vdev_stats_id);
10085 
10086 	arvif->vdev_stats_id = vdev_stats_id;
10087 	return vdev_stats_id;
10088 }
10089 
ath12k_mac_setup_vdev_params_mbssid(struct ath12k_link_vif * arvif,u32 * flags,u32 * tx_vdev_id)10090 static int ath12k_mac_setup_vdev_params_mbssid(struct ath12k_link_vif *arvif,
10091 					       u32 *flags, u32 *tx_vdev_id)
10092 {
10093 	struct ath12k_vif *ahvif = arvif->ahvif;
10094 	struct ieee80211_bss_conf *link_conf;
10095 	struct ath12k *ar = arvif->ar;
10096 	struct ath12k_link_vif *tx_arvif;
10097 
10098 	link_conf = ath12k_mac_get_link_bss_conf(arvif);
10099 	if (!link_conf) {
10100 		ath12k_warn(ar->ab, "unable to access bss link conf in set mbssid params for vif %pM link %u\n",
10101 			    ahvif->vif->addr, arvif->link_id);
10102 		return -ENOLINK;
10103 	}
10104 
10105 	tx_arvif = ath12k_mac_get_tx_arvif(arvif, link_conf);
10106 	if (!tx_arvif)
10107 		return 0;
10108 
10109 	if (link_conf->nontransmitted) {
10110 		if (ath12k_ar_to_hw(ar)->wiphy !=
10111 		    ath12k_ar_to_hw(tx_arvif->ar)->wiphy)
10112 			return -EINVAL;
10113 
10114 		*flags = WMI_VDEV_MBSSID_FLAGS_NON_TRANSMIT_AP;
10115 		*tx_vdev_id = tx_arvif->vdev_id;
10116 	} else if (tx_arvif == arvif) {
10117 		*flags = WMI_VDEV_MBSSID_FLAGS_TRANSMIT_AP;
10118 	} else {
10119 		return -EINVAL;
10120 	}
10121 
10122 	if (link_conf->ema_ap)
10123 		*flags |= WMI_VDEV_MBSSID_FLAGS_EMA_MODE;
10124 
10125 	return 0;
10126 }
10127 
ath12k_mac_setup_vdev_create_arg(struct ath12k_link_vif * arvif,struct ath12k_wmi_vdev_create_arg * arg)10128 static int ath12k_mac_setup_vdev_create_arg(struct ath12k_link_vif *arvif,
10129 					    struct ath12k_wmi_vdev_create_arg *arg)
10130 {
10131 	struct ath12k *ar = arvif->ar;
10132 	struct ath12k_pdev *pdev = ar->pdev;
10133 	struct ath12k_vif *ahvif = arvif->ahvif;
10134 	int ret;
10135 
10136 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
10137 
10138 	arg->if_id = arvif->vdev_id;
10139 	arg->type = ahvif->vdev_type;
10140 	arg->subtype = ahvif->vdev_subtype;
10141 	arg->pdev_id = pdev->pdev_id;
10142 
10143 	arg->mbssid_flags = WMI_VDEV_MBSSID_FLAGS_NON_MBSSID_AP;
10144 	arg->mbssid_tx_vdev_id = 0;
10145 	if (!test_bit(WMI_TLV_SERVICE_MBSS_PARAM_IN_VDEV_START_SUPPORT,
10146 		      ar->ab->wmi_ab.svc_map)) {
10147 		ret = ath12k_mac_setup_vdev_params_mbssid(arvif,
10148 							  &arg->mbssid_flags,
10149 							  &arg->mbssid_tx_vdev_id);
10150 		if (ret)
10151 			return ret;
10152 	}
10153 
10154 	if (pdev->cap.supported_bands & WMI_HOST_WLAN_2GHZ_CAP) {
10155 		arg->chains[NL80211_BAND_2GHZ].tx = ar->num_tx_chains;
10156 		arg->chains[NL80211_BAND_2GHZ].rx = ar->num_rx_chains;
10157 	}
10158 	if (pdev->cap.supported_bands & WMI_HOST_WLAN_5GHZ_CAP) {
10159 		arg->chains[NL80211_BAND_5GHZ].tx = ar->num_tx_chains;
10160 		arg->chains[NL80211_BAND_5GHZ].rx = ar->num_rx_chains;
10161 	}
10162 	if (pdev->cap.supported_bands & WMI_HOST_WLAN_5GHZ_CAP &&
10163 	    ar->supports_6ghz) {
10164 		arg->chains[NL80211_BAND_6GHZ].tx = ar->num_tx_chains;
10165 		arg->chains[NL80211_BAND_6GHZ].rx = ar->num_rx_chains;
10166 	}
10167 
10168 	arg->if_stats_id = ath12k_mac_get_vdev_stats_id(arvif);
10169 
10170 	if (ath12k_mac_is_ml_arvif(arvif)) {
10171 		if (hweight16(ahvif->vif->valid_links) > ATH12K_WMI_MLO_MAX_LINKS) {
10172 			ath12k_warn(ar->ab, "too many MLO links during setting up vdev: %d",
10173 				    ahvif->vif->valid_links);
10174 			return -EINVAL;
10175 		}
10176 
10177 		ether_addr_copy(arg->mld_addr, ahvif->vif->addr);
10178 	}
10179 
10180 	return 0;
10181 }
10182 
ath12k_mac_update_vif_offload(struct ath12k_link_vif * arvif)10183 static void ath12k_mac_update_vif_offload(struct ath12k_link_vif *arvif)
10184 {
10185 	struct ath12k_vif *ahvif = arvif->ahvif;
10186 	struct ieee80211_vif *vif = ath12k_ahvif_to_vif(ahvif);
10187 	struct ath12k *ar = arvif->ar;
10188 	struct ath12k_base *ab = ar->ab;
10189 	u32 param_id, param_value;
10190 	int ret;
10191 
10192 	param_id = WMI_VDEV_PARAM_TX_ENCAP_TYPE;
10193 	if (vif->type != NL80211_IFTYPE_STATION &&
10194 	    vif->type != NL80211_IFTYPE_AP)
10195 		vif->offload_flags &= ~(IEEE80211_OFFLOAD_ENCAP_ENABLED |
10196 					IEEE80211_OFFLOAD_DECAP_ENABLED |
10197 					IEEE80211_OFFLOAD_ENCAP_MCAST |
10198 					IEEE80211_OFFLOAD_ENCAP_4ADDR);
10199 
10200 	if (vif->offload_flags & IEEE80211_OFFLOAD_ENCAP_ENABLED)
10201 		ahvif->dp_vif.tx_encap_type = ATH12K_HW_TXRX_ETHERNET;
10202 	else if (test_bit(ATH12K_FLAG_RAW_MODE, &ab->dev_flags))
10203 		ahvif->dp_vif.tx_encap_type = ATH12K_HW_TXRX_RAW;
10204 	else
10205 		ahvif->dp_vif.tx_encap_type = ATH12K_HW_TXRX_NATIVE_WIFI;
10206 
10207 	ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
10208 					    param_id, ahvif->dp_vif.tx_encap_type);
10209 	if (ret) {
10210 		ath12k_warn(ab, "failed to set vdev %d tx encap mode: %d\n",
10211 			    arvif->vdev_id, ret);
10212 		vif->offload_flags &= ~IEEE80211_OFFLOAD_ENCAP_ENABLED;
10213 	}
10214 
10215 	if (vif->offload_flags & IEEE80211_OFFLOAD_ENCAP_ENABLED)
10216 		vif->offload_flags |= (IEEE80211_OFFLOAD_ENCAP_MCAST |
10217 				       IEEE80211_OFFLOAD_ENCAP_4ADDR);
10218 
10219 	param_id = WMI_VDEV_PARAM_RX_DECAP_TYPE;
10220 	if (vif->offload_flags & IEEE80211_OFFLOAD_DECAP_ENABLED)
10221 		param_value = ATH12K_HW_TXRX_ETHERNET;
10222 	else if (test_bit(ATH12K_FLAG_RAW_MODE, &ab->dev_flags))
10223 		param_value = ATH12K_HW_TXRX_RAW;
10224 	else
10225 		param_value = ATH12K_HW_TXRX_NATIVE_WIFI;
10226 
10227 	ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
10228 					    param_id, param_value);
10229 	if (ret) {
10230 		ath12k_warn(ab, "failed to set vdev %d rx decap mode: %d\n",
10231 			    arvif->vdev_id, ret);
10232 		vif->offload_flags &= ~IEEE80211_OFFLOAD_DECAP_ENABLED;
10233 	}
10234 }
10235 
ath12k_mac_op_update_vif_offload(struct ieee80211_hw * hw,struct ieee80211_vif * vif)10236 void ath12k_mac_op_update_vif_offload(struct ieee80211_hw *hw,
10237 				      struct ieee80211_vif *vif)
10238 {
10239 	struct ath12k_vif *ahvif = ath12k_vif_to_ahvif(vif);
10240 	struct ath12k_link_vif *arvif;
10241 	unsigned long links;
10242 	int link_id;
10243 
10244 	lockdep_assert_wiphy(hw->wiphy);
10245 
10246 	if (vif->valid_links) {
10247 		links = vif->valid_links;
10248 		for_each_set_bit(link_id, &links, IEEE80211_MLD_MAX_NUM_LINKS) {
10249 			arvif = wiphy_dereference(hw->wiphy, ahvif->link[link_id]);
10250 			if (!(arvif && arvif->ar))
10251 				continue;
10252 
10253 			ath12k_mac_update_vif_offload(arvif);
10254 		}
10255 
10256 		return;
10257 	}
10258 
10259 	ath12k_mac_update_vif_offload(&ahvif->deflink);
10260 }
10261 EXPORT_SYMBOL(ath12k_mac_op_update_vif_offload);
10262 
ath12k_mac_vif_ap_active_any(struct ath12k_base * ab)10263 static bool ath12k_mac_vif_ap_active_any(struct ath12k_base *ab)
10264 {
10265 	struct ath12k *ar;
10266 	struct ath12k_pdev *pdev;
10267 	struct ath12k_link_vif *arvif;
10268 	int i;
10269 
10270 	for (i = 0; i < ab->num_radios; i++) {
10271 		pdev = &ab->pdevs[i];
10272 		ar = pdev->ar;
10273 		list_for_each_entry(arvif, &ar->arvifs, list) {
10274 			if (arvif->is_up &&
10275 			    arvif->ahvif->vdev_type == WMI_VDEV_TYPE_AP)
10276 				return true;
10277 		}
10278 	}
10279 	return false;
10280 }
10281 
ath12k_mac_11d_scan_start(struct ath12k * ar,u32 vdev_id)10282 void ath12k_mac_11d_scan_start(struct ath12k *ar, u32 vdev_id)
10283 {
10284 	struct wmi_11d_scan_start_arg arg;
10285 	int ret;
10286 
10287 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
10288 
10289 	if (ar->regdom_set_by_user)
10290 		goto fin;
10291 
10292 	if (ar->vdev_id_11d_scan != ATH12K_11D_INVALID_VDEV_ID)
10293 		goto fin;
10294 
10295 	if (!test_bit(WMI_TLV_SERVICE_11D_OFFLOAD, ar->ab->wmi_ab.svc_map))
10296 		goto fin;
10297 
10298 	if (ath12k_mac_vif_ap_active_any(ar->ab))
10299 		goto fin;
10300 
10301 	arg.vdev_id = vdev_id;
10302 	arg.start_interval_msec = 0;
10303 	arg.scan_period_msec = ATH12K_SCAN_11D_INTERVAL;
10304 
10305 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
10306 		   "mac start 11d scan for vdev %d\n", vdev_id);
10307 
10308 	ret = ath12k_wmi_send_11d_scan_start_cmd(ar, &arg);
10309 	if (ret) {
10310 		ath12k_warn(ar->ab, "failed to start 11d scan vdev %d ret: %d\n",
10311 			    vdev_id, ret);
10312 	} else {
10313 		ar->vdev_id_11d_scan = vdev_id;
10314 		if (ar->state_11d == ATH12K_11D_PREPARING)
10315 			ar->state_11d = ATH12K_11D_RUNNING;
10316 	}
10317 
10318 fin:
10319 	if (ar->state_11d == ATH12K_11D_PREPARING) {
10320 		ar->state_11d = ATH12K_11D_IDLE;
10321 		complete(&ar->completed_11d_scan);
10322 	}
10323 }
10324 
ath12k_mac_11d_scan_stop(struct ath12k * ar)10325 void ath12k_mac_11d_scan_stop(struct ath12k *ar)
10326 {
10327 	int ret;
10328 	u32 vdev_id;
10329 
10330 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
10331 
10332 	if (!test_bit(WMI_TLV_SERVICE_11D_OFFLOAD, ar->ab->wmi_ab.svc_map))
10333 		return;
10334 
10335 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac stop 11d for vdev %d\n",
10336 		   ar->vdev_id_11d_scan);
10337 
10338 	if (ar->state_11d == ATH12K_11D_PREPARING) {
10339 		ar->state_11d = ATH12K_11D_IDLE;
10340 		complete(&ar->completed_11d_scan);
10341 	}
10342 
10343 	if (ar->vdev_id_11d_scan != ATH12K_11D_INVALID_VDEV_ID) {
10344 		vdev_id = ar->vdev_id_11d_scan;
10345 
10346 		ret = ath12k_wmi_send_11d_scan_stop_cmd(ar, vdev_id);
10347 		if (ret) {
10348 			ath12k_warn(ar->ab,
10349 				    "failed to stopt 11d scan vdev %d ret: %d\n",
10350 				    vdev_id, ret);
10351 		} else {
10352 			ar->vdev_id_11d_scan = ATH12K_11D_INVALID_VDEV_ID;
10353 			ar->state_11d = ATH12K_11D_IDLE;
10354 			complete(&ar->completed_11d_scan);
10355 		}
10356 	}
10357 }
10358 
ath12k_mac_11d_scan_stop_all(struct ath12k_base * ab)10359 void ath12k_mac_11d_scan_stop_all(struct ath12k_base *ab)
10360 {
10361 	struct ath12k *ar;
10362 	struct ath12k_pdev *pdev;
10363 	int i;
10364 
10365 	ath12k_dbg(ab, ATH12K_DBG_MAC, "mac stop soc 11d scan\n");
10366 
10367 	for (i = 0; i < ab->num_radios; i++) {
10368 		pdev = &ab->pdevs[i];
10369 		ar = pdev->ar;
10370 
10371 		ath12k_mac_11d_scan_stop(ar);
10372 	}
10373 }
10374 
ath12k_mac_determine_vdev_type(struct ieee80211_vif * vif,struct ath12k_vif * ahvif)10375 static void ath12k_mac_determine_vdev_type(struct ieee80211_vif *vif,
10376 					   struct ath12k_vif *ahvif)
10377 {
10378 	ahvif->vdev_subtype = WMI_VDEV_SUBTYPE_NONE;
10379 
10380 	switch (vif->type) {
10381 	case NL80211_IFTYPE_UNSPECIFIED:
10382 	case NL80211_IFTYPE_STATION:
10383 		ahvif->vdev_type = WMI_VDEV_TYPE_STA;
10384 
10385 		if (vif->p2p)
10386 			ahvif->vdev_subtype = WMI_VDEV_SUBTYPE_P2P_CLIENT;
10387 
10388 		break;
10389 	case NL80211_IFTYPE_MESH_POINT:
10390 		ahvif->vdev_subtype = WMI_VDEV_SUBTYPE_MESH_11S;
10391 		fallthrough;
10392 	case NL80211_IFTYPE_AP:
10393 		ahvif->vdev_type = WMI_VDEV_TYPE_AP;
10394 
10395 		if (vif->p2p)
10396 			ahvif->vdev_subtype = WMI_VDEV_SUBTYPE_P2P_GO;
10397 
10398 		break;
10399 	case NL80211_IFTYPE_MONITOR:
10400 		ahvif->vdev_type = WMI_VDEV_TYPE_MONITOR;
10401 		break;
10402 	case NL80211_IFTYPE_P2P_DEVICE:
10403 		ahvif->vdev_type = WMI_VDEV_TYPE_STA;
10404 		ahvif->vdev_subtype = WMI_VDEV_SUBTYPE_P2P_DEVICE;
10405 		break;
10406 	default:
10407 		WARN_ON(1);
10408 		break;
10409 	}
10410 }
10411 
ath12k_mac_vdev_create(struct ath12k * ar,struct ath12k_link_vif * arvif)10412 int ath12k_mac_vdev_create(struct ath12k *ar, struct ath12k_link_vif *arvif)
10413 {
10414 	struct ath12k_hw *ah = ar->ah;
10415 	struct ath12k_base *ab = ar->ab;
10416 	struct ieee80211_hw *hw = ah->hw;
10417 	struct ath12k_vif *ahvif = arvif->ahvif;
10418 	struct ieee80211_vif *vif = ath12k_ahvif_to_vif(ahvif);
10419 	struct wireless_dev *wdev = ieee80211_vif_to_wdev(vif);
10420 	struct ath12k_wmi_vdev_create_arg vdev_arg = {};
10421 	struct ath12k_wmi_peer_create_arg peer_param = {};
10422 	struct ieee80211_bss_conf *link_conf = NULL;
10423 	u32 param_id, param_value;
10424 	u16 nss;
10425 	int i;
10426 	int ret, vdev_id;
10427 	u8 link_id;
10428 	struct ath12k_dp_link_vif *dp_link_vif = NULL;
10429 	struct ath12k_dp_peer_create_params params = {};
10430 	bool dp_peer_created = false;
10431 
10432 	lockdep_assert_wiphy(hw->wiphy);
10433 
10434 	/* In NO_VIRTUAL_MONITOR, its necessary to restrict only one monitor
10435 	 * interface in each radio
10436 	 */
10437 	if (vif->type == NL80211_IFTYPE_MONITOR && ar->monitor_vdev_created)
10438 		return -EINVAL;
10439 
10440 	if (ar->num_created_vdevs >= TARGET_NUM_VDEVS(ab)) {
10441 		ath12k_warn(ab, "failed to create vdev, reached max vdev limit %d\n",
10442 			    TARGET_NUM_VDEVS(ab));
10443 		return -ENOSPC;
10444 	}
10445 
10446 	link_id = arvif->link_id;
10447 
10448 	if (link_id < IEEE80211_MLD_MAX_NUM_LINKS) {
10449 		link_conf = wiphy_dereference(hw->wiphy, vif->link_conf[link_id]);
10450 		if (!link_conf) {
10451 			ath12k_warn(ar->ab, "unable to access bss link conf in vdev create for vif %pM link %u\n",
10452 				    vif->addr, arvif->link_id);
10453 			return -ENOLINK;
10454 		}
10455 	}
10456 
10457 	if (link_conf)
10458 		memcpy(arvif->bssid, link_conf->addr, ETH_ALEN);
10459 	else
10460 		memcpy(arvif->bssid, vif->addr, ETH_ALEN);
10461 
10462 	arvif->ar = ar;
10463 	vdev_id = __ffs64(ab->free_vdev_map);
10464 	arvif->vdev_id = vdev_id;
10465 	if (vif->type == NL80211_IFTYPE_MONITOR)
10466 		ar->monitor_vdev_id = vdev_id;
10467 
10468 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac vdev create id %d type %d subtype %d map %llx\n",
10469 		   arvif->vdev_id, ahvif->vdev_type, ahvif->vdev_subtype,
10470 		   ab->free_vdev_map);
10471 
10472 	vif->cab_queue = arvif->vdev_id % (ATH12K_HW_MAX_QUEUES - 1);
10473 	for (i = 0; i < ARRAY_SIZE(vif->hw_queue); i++)
10474 		vif->hw_queue[i] = i % (ATH12K_HW_MAX_QUEUES - 1);
10475 
10476 	ret = ath12k_mac_setup_vdev_create_arg(arvif, &vdev_arg);
10477 	if (ret) {
10478 		ath12k_warn(ab, "failed to create vdev parameters %d: %d\n",
10479 			    arvif->vdev_id, ret);
10480 		goto err;
10481 	}
10482 
10483 	ret = ath12k_wmi_vdev_create(ar, arvif->bssid, &vdev_arg);
10484 	if (ret) {
10485 		ath12k_warn(ab, "failed to create WMI vdev %d: %d\n",
10486 			    arvif->vdev_id, ret);
10487 		goto err;
10488 	}
10489 
10490 	ar->num_created_vdevs++;
10491 	arvif->is_created = true;
10492 	ath12k_dbg(ab, ATH12K_DBG_MAC, "vdev %pM created, vdev_id %d\n",
10493 		   vif->addr, arvif->vdev_id);
10494 	ar->allocated_vdev_map |= 1LL << arvif->vdev_id;
10495 	ab->free_vdev_map &= ~(1LL << arvif->vdev_id);
10496 
10497 	spin_lock_bh(&ar->data_lock);
10498 	list_add(&arvif->list, &ar->arvifs);
10499 	spin_unlock_bh(&ar->data_lock);
10500 
10501 	ath12k_mac_update_vif_offload(arvif);
10502 
10503 	nss = hweight32(ar->cfg_tx_chainmask) ? : 1;
10504 	ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
10505 					    WMI_VDEV_PARAM_NSS, nss);
10506 	if (ret) {
10507 		ath12k_warn(ab, "failed to set vdev %d chainmask 0x%x, nss %d :%d\n",
10508 			    arvif->vdev_id, ar->cfg_tx_chainmask, nss, ret);
10509 		goto err_vdev_del;
10510 	}
10511 
10512 	dp_link_vif = ath12k_dp_vif_to_dp_link_vif(&ahvif->dp_vif, arvif->link_id);
10513 
10514 	dp_link_vif->vdev_id = arvif->vdev_id;
10515 	dp_link_vif->lmac_id = ar->lmac_id;
10516 	dp_link_vif->pdev_idx = ar->pdev_idx;
10517 
10518 	switch (ahvif->vdev_type) {
10519 	case WMI_VDEV_TYPE_AP:
10520 		params.ucast_ra_only = true;
10521 
10522 		if (arvif->link_id < IEEE80211_MLD_MAX_NUM_LINKS) {
10523 			ret = ath12k_dp_peer_create(&ah->dp_hw, arvif->bssid, &params);
10524 			if (ret) {
10525 				ath12k_warn(ab, "failed to vdev %d create dp_peer for AP: %d\n",
10526 					    arvif->vdev_id, ret);
10527 				goto err_vdev_del;
10528 			}
10529 			dp_peer_created = true;
10530 		}
10531 
10532 		peer_param.vdev_id = arvif->vdev_id;
10533 		peer_param.peer_addr = arvif->bssid;
10534 		peer_param.peer_type = WMI_PEER_TYPE_DEFAULT;
10535 		ret = ath12k_peer_create(ar, arvif, NULL, &peer_param);
10536 		if (ret) {
10537 			ath12k_warn(ab, "failed to vdev %d create peer for AP: %d\n",
10538 				    arvif->vdev_id, ret);
10539 			goto err_dp_peer_del;
10540 		}
10541 
10542 		ret = ath12k_mac_set_kickout(arvif);
10543 		if (ret) {
10544 			ath12k_warn(ar->ab, "failed to set vdev %i kickout parameters: %d\n",
10545 				    arvif->vdev_id, ret);
10546 			goto err_peer_del;
10547 		}
10548 		ath12k_mac_11d_scan_stop_all(ar->ab);
10549 		break;
10550 	case WMI_VDEV_TYPE_STA:
10551 		param_id = WMI_STA_PS_PARAM_RX_WAKE_POLICY;
10552 		param_value = WMI_STA_PS_RX_WAKE_POLICY_WAKE;
10553 		ret = ath12k_wmi_set_sta_ps_param(ar, arvif->vdev_id,
10554 						  param_id, param_value);
10555 		if (ret) {
10556 			ath12k_warn(ar->ab, "failed to set vdev %d RX wake policy: %d\n",
10557 				    arvif->vdev_id, ret);
10558 			goto err_peer_del;
10559 		}
10560 
10561 		param_id = WMI_STA_PS_PARAM_TX_WAKE_THRESHOLD;
10562 		param_value = WMI_STA_PS_TX_WAKE_THRESHOLD_ALWAYS;
10563 		ret = ath12k_wmi_set_sta_ps_param(ar, arvif->vdev_id,
10564 						  param_id, param_value);
10565 		if (ret) {
10566 			ath12k_warn(ar->ab, "failed to set vdev %d TX wake threshold: %d\n",
10567 				    arvif->vdev_id, ret);
10568 			goto err_peer_del;
10569 		}
10570 
10571 		param_id = WMI_STA_PS_PARAM_PSPOLL_COUNT;
10572 		param_value = WMI_STA_PS_PSPOLL_COUNT_NO_MAX;
10573 		ret = ath12k_wmi_set_sta_ps_param(ar, arvif->vdev_id,
10574 						  param_id, param_value);
10575 		if (ret) {
10576 			ath12k_warn(ar->ab, "failed to set vdev %d pspoll count: %d\n",
10577 				    arvif->vdev_id, ret);
10578 			goto err_peer_del;
10579 		}
10580 
10581 		ret = ath12k_wmi_pdev_set_ps_mode(ar, arvif->vdev_id, false);
10582 		if (ret) {
10583 			ath12k_warn(ar->ab, "failed to disable vdev %d ps mode: %d\n",
10584 				    arvif->vdev_id, ret);
10585 			goto err_peer_del;
10586 		}
10587 
10588 		/*
10589 		 * There could be race condition in firmware for the station
10590 		 * interface between enabling 4-address peer WMI param and
10591 		 * sending 4-address frame (NULL or EAPOL via TCL).
10592 		 * Make the station as WDS while bringup itself
10593 		 * to avoid the race condition
10594 		 */
10595 		if (vif->type == NL80211_IFTYPE_STATION &&
10596 		    (wdev && wdev->use_4addr)) {
10597 			ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
10598 							    WMI_VDEV_PARAM_WDS,
10599 							    1);
10600 			if (ret) {
10601 				ath12k_warn(ar->ab, "failed to set WDS vdev param: %d\n",
10602 					    ret);
10603 				goto err_peer_del;
10604 			}
10605 			arvif->set_wds_vdev_param = true;
10606 		}
10607 
10608 		if (test_bit(WMI_TLV_SERVICE_11D_OFFLOAD, ab->wmi_ab.svc_map) &&
10609 		    ahvif->vdev_type == WMI_VDEV_TYPE_STA &&
10610 		    ahvif->vdev_subtype == WMI_VDEV_SUBTYPE_NONE) {
10611 			reinit_completion(&ar->completed_11d_scan);
10612 			ar->state_11d = ATH12K_11D_PREPARING;
10613 		}
10614 		break;
10615 	case WMI_VDEV_TYPE_MONITOR:
10616 		ar->monitor_vdev_created = true;
10617 		break;
10618 	default:
10619 		break;
10620 	}
10621 
10622 	if (link_conf)
10623 		arvif->txpower = link_conf->txpower;
10624 	else
10625 		arvif->txpower = NL80211_TX_POWER_AUTOMATIC;
10626 
10627 	ret = ath12k_mac_txpower_recalc(ar);
10628 	if (ret)
10629 		goto err_peer_del;
10630 
10631 	param_id = WMI_VDEV_PARAM_RTS_THRESHOLD;
10632 	param_value = hw->wiphy->rts_threshold;
10633 	ar->rts_threshold = param_value;
10634 	ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
10635 					    param_id, param_value);
10636 	if (ret) {
10637 		ath12k_warn(ar->ab, "failed to set rts threshold for vdev %d: %d\n",
10638 			    arvif->vdev_id, ret);
10639 	}
10640 
10641 	ath12k_dp_vdev_tx_attach(ar, arvif);
10642 
10643 	return ret;
10644 
10645 err_peer_del:
10646 	if (ahvif->vdev_type == WMI_VDEV_TYPE_AP) {
10647 		/* ignore return value: propagate the original error */
10648 		ath12k_peer_delete(ar, arvif->vdev_id, arvif->bssid);
10649 	}
10650 
10651 err_dp_peer_del:
10652 	if (dp_peer_created)
10653 		ath12k_dp_peer_delete(&ah->dp_hw, arvif->bssid, NULL);
10654 
10655 err_vdev_del:
10656 	if (ahvif->vdev_type == WMI_VDEV_TYPE_MONITOR) {
10657 		ar->monitor_vdev_id = -1;
10658 		ar->monitor_vdev_created = false;
10659 	}
10660 
10661 	ath12k_wmi_vdev_delete(ar, arvif->vdev_id);
10662 	ar->num_created_vdevs--;
10663 	ar->allocated_vdev_map &= ~(1LL << arvif->vdev_id);
10664 	ab->free_vdev_map |= 1LL << arvif->vdev_id;
10665 	ab->free_vdev_stats_id_map &= ~(1LL << arvif->vdev_stats_id);
10666 	spin_lock_bh(&ar->data_lock);
10667 	list_del(&arvif->list);
10668 	spin_unlock_bh(&ar->data_lock);
10669 
10670 err:
10671 	arvif->is_created = false;
10672 	arvif->ar = NULL;
10673 	return ret;
10674 }
10675 
ath12k_mac_vif_flush_key_cache(struct ath12k_link_vif * arvif)10676 static void ath12k_mac_vif_flush_key_cache(struct ath12k_link_vif *arvif)
10677 {
10678 	struct ath12k_key_conf *key_conf, *tmp;
10679 	struct ath12k_vif *ahvif = arvif->ahvif;
10680 	struct ath12k_hw *ah = ahvif->ah;
10681 	struct ath12k_sta *ahsta;
10682 	struct ath12k_link_sta *arsta;
10683 	struct ath12k_vif_cache *cache = ahvif->cache[arvif->link_id];
10684 	int ret;
10685 
10686 	lockdep_assert_wiphy(ah->hw->wiphy);
10687 
10688 	list_for_each_entry_safe(key_conf, tmp, &cache->key_conf.list, list) {
10689 		arsta = NULL;
10690 		if (key_conf->sta) {
10691 			ahsta = ath12k_sta_to_ahsta(key_conf->sta);
10692 			arsta = wiphy_dereference(ah->hw->wiphy,
10693 						  ahsta->link[arvif->link_id]);
10694 			if (!arsta)
10695 				goto free_cache;
10696 		}
10697 
10698 		ret = ath12k_mac_set_key(arvif->ar, key_conf->cmd,
10699 					 arvif, arsta,
10700 					 key_conf->key);
10701 		if (ret)
10702 			ath12k_warn(arvif->ar->ab, "unable to apply set key param to vdev %d ret %d\n",
10703 				    arvif->vdev_id, ret);
10704 free_cache:
10705 		list_del(&key_conf->list);
10706 		kfree(key_conf);
10707 	}
10708 }
10709 
ath12k_mac_vif_cache_flush(struct ath12k * ar,struct ath12k_link_vif * arvif)10710 static void ath12k_mac_vif_cache_flush(struct ath12k *ar, struct ath12k_link_vif *arvif)
10711 {
10712 	struct ath12k_vif *ahvif = arvif->ahvif;
10713 	struct ieee80211_vif *vif = ath12k_ahvif_to_vif(ahvif);
10714 	struct ath12k_vif_cache *cache = ahvif->cache[arvif->link_id];
10715 	struct ath12k_base *ab = ar->ab;
10716 	struct ieee80211_bss_conf *link_conf;
10717 
10718 	int ret;
10719 
10720 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
10721 
10722 	if (!cache)
10723 		return;
10724 
10725 	if (cache->tx_conf.changed) {
10726 		ret = ath12k_mac_conf_tx(arvif, cache->tx_conf.ac,
10727 					 &cache->tx_conf.tx_queue_params);
10728 		if (ret)
10729 			ath12k_warn(ab,
10730 				    "unable to apply tx config parameters to vdev %d\n",
10731 				    ret);
10732 	}
10733 
10734 	if (cache->bss_conf_changed) {
10735 		link_conf = ath12k_mac_get_link_bss_conf(arvif);
10736 		if (!link_conf) {
10737 			ath12k_warn(ar->ab, "unable to access bss link conf in cache flush for vif %pM link %u\n",
10738 				    vif->addr, arvif->link_id);
10739 			return;
10740 		}
10741 		ath12k_mac_bss_info_changed(ar, arvif, link_conf,
10742 					    cache->bss_conf_changed);
10743 	}
10744 
10745 	if (!list_empty(&cache->key_conf.list))
10746 		ath12k_mac_vif_flush_key_cache(arvif);
10747 
10748 	ath12k_ahvif_put_link_cache(ahvif, arvif->link_id);
10749 }
10750 
ath12k_mac_assign_vif_to_vdev(struct ieee80211_hw * hw,struct ath12k_link_vif * arvif,struct ieee80211_chanctx_conf * ctx)10751 static struct ath12k *ath12k_mac_assign_vif_to_vdev(struct ieee80211_hw *hw,
10752 						    struct ath12k_link_vif *arvif,
10753 						    struct ieee80211_chanctx_conf *ctx)
10754 {
10755 	struct ath12k_vif *ahvif = arvif->ahvif;
10756 	struct ieee80211_vif *vif = ath12k_ahvif_to_vif(ahvif);
10757 	struct ath12k_link_vif *scan_arvif;
10758 	struct ath12k_hw *ah = hw->priv;
10759 	struct ath12k *ar;
10760 	struct ath12k_base *ab;
10761 	u8 link_id = arvif->link_id, scan_link_id;
10762 	unsigned long scan_link_map;
10763 	int ret;
10764 
10765 	lockdep_assert_wiphy(hw->wiphy);
10766 
10767 	if (ah->num_radio == 1)
10768 		ar = ah->radio;
10769 	else if (ctx)
10770 		ar = ath12k_get_ar_by_ctx(hw, ctx);
10771 	else
10772 		return NULL;
10773 
10774 	if (!ar)
10775 		return NULL;
10776 
10777 	/* cleanup the scan vdev if we are done scan on that ar
10778 	 * and now we want to create for actual usage.
10779 	 */
10780 	if (ieee80211_vif_is_mld(vif)) {
10781 		scan_link_map = ahvif->links_map & ATH12K_SCAN_LINKS_MASK;
10782 		for_each_set_bit(scan_link_id, &scan_link_map, ATH12K_NUM_MAX_LINKS) {
10783 			scan_arvif = wiphy_dereference(hw->wiphy,
10784 						       ahvif->link[scan_link_id]);
10785 			if (scan_arvif && scan_arvif->ar == ar) {
10786 				ar->scan.arvif = NULL;
10787 				ath12k_mac_remove_link_interface(hw, scan_arvif);
10788 				ath12k_mac_unassign_link_vif(scan_arvif);
10789 				break;
10790 			}
10791 		}
10792 	}
10793 
10794 	if (arvif->ar) {
10795 		/* This is not expected really */
10796 		if (WARN_ON(!arvif->is_created)) {
10797 			arvif->ar = NULL;
10798 			return NULL;
10799 		}
10800 
10801 		if (ah->num_radio == 1)
10802 			return arvif->ar;
10803 
10804 		/* This can happen as scan vdev gets created during multiple scans
10805 		 * across different radios before a vdev is brought up in
10806 		 * a certain radio.
10807 		 */
10808 		if (ar != arvif->ar) {
10809 			if (WARN_ON(arvif->is_started))
10810 				return NULL;
10811 
10812 			ath12k_mac_remove_link_interface(hw, arvif);
10813 			ath12k_mac_unassign_link_vif(arvif);
10814 		}
10815 	}
10816 
10817 	ab = ar->ab;
10818 
10819 	/* Assign arvif again here since previous radio switch block
10820 	 * would've unassigned and cleared it.
10821 	 */
10822 	arvif = ath12k_mac_assign_link_vif(ah, vif, link_id);
10823 	if (vif->type == NL80211_IFTYPE_AP &&
10824 	    ar->num_peers > (ar->max_num_peers - 1)) {
10825 		ath12k_warn(ab, "failed to create vdev due to insufficient peer entry resource in firmware\n");
10826 		goto unlock;
10827 	}
10828 
10829 	if (arvif->is_created)
10830 		goto flush;
10831 
10832 	ret = ath12k_mac_vdev_create(ar, arvif);
10833 	if (ret) {
10834 		ath12k_warn(ab, "failed to create vdev %pM ret %d", vif->addr, ret);
10835 		goto unlock;
10836 	}
10837 
10838 flush:
10839 	/* If the vdev is created during channel assign and not during
10840 	 * add_interface(), Apply any parameters for the vdev which were received
10841 	 * after add_interface, corresponding to this vif.
10842 	 */
10843 	ath12k_mac_vif_cache_flush(ar, arvif);
10844 unlock:
10845 	return arvif->ar;
10846 }
10847 
ath12k_mac_op_add_interface(struct ieee80211_hw * hw,struct ieee80211_vif * vif)10848 int ath12k_mac_op_add_interface(struct ieee80211_hw *hw,
10849 				struct ieee80211_vif *vif)
10850 {
10851 	struct ath12k_hw *ah = ath12k_hw_to_ah(hw);
10852 	struct ath12k_vif *ahvif = ath12k_vif_to_ahvif(vif);
10853 	struct ath12k_reg_info *reg_info;
10854 	struct ath12k_link_vif *arvif;
10855 	struct ath12k_base *ab;
10856 	struct ath12k *ar;
10857 	int i;
10858 
10859 	lockdep_assert_wiphy(hw->wiphy);
10860 
10861 	memset(ahvif, 0, sizeof(*ahvif));
10862 
10863 	ahvif->ah = ah;
10864 	ahvif->vif = vif;
10865 	arvif = &ahvif->deflink;
10866 
10867 	ath12k_mac_init_arvif(ahvif, arvif, -1);
10868 
10869 	/* Allocate Default Queue now and reassign during actual vdev create */
10870 	vif->cab_queue = ATH12K_HW_DEFAULT_QUEUE;
10871 	for (i = 0; i < ARRAY_SIZE(vif->hw_queue); i++)
10872 		vif->hw_queue[i] = ATH12K_HW_DEFAULT_QUEUE;
10873 
10874 	vif->driver_flags |= IEEE80211_VIF_SUPPORTS_UAPSD;
10875 
10876 	ath12k_mac_determine_vdev_type(vif, ahvif);
10877 
10878 	for_each_ar(ah, ar, i) {
10879 		if (!ath12k_wmi_supports_6ghz_cc_ext(ar))
10880 			continue;
10881 
10882 		ab = ar->ab;
10883 		reg_info = ab->reg_info[ar->pdev_idx];
10884 		ath12k_dbg(ab, ATH12K_DBG_MAC, "interface added to change reg rules\n");
10885 		ah->regd_updated = false;
10886 		ath12k_reg_handle_chan_list(ab, reg_info, ahvif->vdev_type,
10887 					    IEEE80211_REG_UNSET_AP);
10888 		break;
10889 	}
10890 
10891 	/* Defer vdev creation until assign_chanctx or hw_scan is initiated as driver
10892 	 * will not know if this interface is an ML vif at this point.
10893 	 */
10894 	return 0;
10895 }
10896 EXPORT_SYMBOL(ath12k_mac_op_add_interface);
10897 
ath12k_mac_vif_unref(struct ath12k_dp * dp,struct ieee80211_vif * vif)10898 static void ath12k_mac_vif_unref(struct ath12k_dp *dp, struct ieee80211_vif *vif)
10899 {
10900 	struct ath12k_tx_desc_info *tx_desc_info;
10901 	struct ath12k_skb_cb *skb_cb;
10902 	struct sk_buff *skb;
10903 	int i;
10904 
10905 	for (i = 0; i < ATH12K_HW_MAX_QUEUES; i++) {
10906 		spin_lock_bh(&dp->tx_desc_lock[i]);
10907 
10908 		list_for_each_entry(tx_desc_info, &dp->tx_desc_used_list[i],
10909 				    list) {
10910 			skb = tx_desc_info->skb;
10911 			if (!skb)
10912 				continue;
10913 
10914 			skb_cb = ATH12K_SKB_CB(skb);
10915 			if (skb_cb->vif == vif)
10916 				skb_cb->vif = NULL;
10917 		}
10918 
10919 		spin_unlock_bh(&dp->tx_desc_lock[i]);
10920 	}
10921 }
10922 
ath12k_mac_vdev_delete(struct ath12k * ar,struct ath12k_link_vif * arvif)10923 static int ath12k_mac_vdev_delete(struct ath12k *ar, struct ath12k_link_vif *arvif)
10924 {
10925 	struct ath12k_vif *ahvif = arvif->ahvif;
10926 	struct ieee80211_vif *vif = ath12k_ahvif_to_vif(ahvif);
10927 	struct ath12k_dp_link_vif *dp_link_vif;
10928 	struct ath12k_base *ab = ar->ab;
10929 	unsigned long time_left;
10930 	int ret;
10931 
10932 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
10933 
10934 	reinit_completion(&ar->vdev_delete_done);
10935 
10936 	ret = ath12k_wmi_vdev_delete(ar, arvif->vdev_id);
10937 	if (ret) {
10938 		ath12k_warn(ab, "failed to delete WMI vdev %d: %d\n",
10939 			    arvif->vdev_id, ret);
10940 		goto err_vdev_del;
10941 	}
10942 
10943 	time_left = wait_for_completion_timeout(&ar->vdev_delete_done,
10944 						ATH12K_VDEV_DELETE_TIMEOUT_HZ);
10945 	if (time_left == 0) {
10946 		ath12k_warn(ab, "Timeout in receiving vdev delete response\n");
10947 		goto err_vdev_del;
10948 	}
10949 
10950 	ab->free_vdev_map |= 1LL << arvif->vdev_id;
10951 	ar->allocated_vdev_map &= ~(1LL << arvif->vdev_id);
10952 	ar->num_created_vdevs--;
10953 
10954 	if (ahvif->vdev_type == WMI_VDEV_TYPE_MONITOR) {
10955 		ar->monitor_vdev_id = -1;
10956 		ar->monitor_vdev_created = false;
10957 	}
10958 
10959 	ath12k_dbg(ab, ATH12K_DBG_MAC, "vdev %pM deleted, vdev_id %d\n",
10960 		   vif->addr, arvif->vdev_id);
10961 
10962 err_vdev_del:
10963 	spin_lock_bh(&ar->data_lock);
10964 	list_del(&arvif->list);
10965 	spin_unlock_bh(&ar->data_lock);
10966 
10967 	ath12k_peer_cleanup(ar, arvif->vdev_id);
10968 	ath12k_ahvif_put_link_cache(ahvif, arvif->link_id);
10969 
10970 	idr_for_each(&ar->txmgmt_idr,
10971 		     ath12k_mac_vif_txmgmt_idr_remove, vif);
10972 
10973 	ath12k_mac_vif_unref(ath12k_ab_to_dp(ab), vif);
10974 
10975 	dp_link_vif = ath12k_dp_vif_to_dp_link_vif(&ahvif->dp_vif, arvif->link_id);
10976 	ath12k_dp_tx_put_bank_profile(ath12k_ab_to_dp(ab), dp_link_vif->bank_id);
10977 
10978 	/* Recalc txpower for remaining vdev */
10979 	ath12k_mac_txpower_recalc(ar);
10980 
10981 	/* TODO: recal traffic pause state based on the available vdevs */
10982 	arvif->is_created = false;
10983 	arvif->ar = NULL;
10984 
10985 	return ret;
10986 }
10987 
ath12k_mac_op_remove_interface(struct ieee80211_hw * hw,struct ieee80211_vif * vif)10988 void ath12k_mac_op_remove_interface(struct ieee80211_hw *hw,
10989 				    struct ieee80211_vif *vif)
10990 {
10991 	struct ath12k_vif *ahvif = ath12k_vif_to_ahvif(vif);
10992 	struct ath12k_link_vif *arvif;
10993 	struct ath12k *ar;
10994 	u8 link_id;
10995 
10996 	lockdep_assert_wiphy(hw->wiphy);
10997 
10998 	for (link_id = 0; link_id < ATH12K_NUM_MAX_LINKS; link_id++) {
10999 		/* if we cached some config but never received assign chanctx,
11000 		 * free the allocated cache.
11001 		 */
11002 		ath12k_ahvif_put_link_cache(ahvif, link_id);
11003 		arvif = wiphy_dereference(hw->wiphy, ahvif->link[link_id]);
11004 		if (!arvif || !arvif->is_created)
11005 			continue;
11006 
11007 		ar = arvif->ar;
11008 
11009 		/* Scan abortion is in progress since before this, cancel_hw_scan()
11010 		 * is expected to be executed. Since link is anyways going to be removed
11011 		 * now, just cancel the worker and send the scan aborted to user space
11012 		 */
11013 		if (ar->scan.arvif == arvif) {
11014 			wiphy_work_cancel(hw->wiphy, &ar->scan.vdev_clean_wk);
11015 
11016 			spin_lock_bh(&ar->data_lock);
11017 			ar->scan.arvif = NULL;
11018 			if (!ar->scan.is_roc) {
11019 				struct cfg80211_scan_info info = {
11020 					.aborted = true,
11021 				};
11022 
11023 				ath12k_mac_scan_send_complete(ar, &info);
11024 			}
11025 
11026 			ar->scan.state = ATH12K_SCAN_IDLE;
11027 			ar->scan_channel = NULL;
11028 			ar->scan.roc_freq = 0;
11029 			spin_unlock_bh(&ar->data_lock);
11030 		}
11031 
11032 		ath12k_mac_remove_link_interface(hw, arvif);
11033 		ath12k_mac_unassign_link_vif(arvif);
11034 	}
11035 }
11036 EXPORT_SYMBOL(ath12k_mac_op_remove_interface);
11037 
11038 /* FIXME: Has to be verified. */
11039 #define SUPPORTED_FILTERS			\
11040 	(FIF_ALLMULTI |				\
11041 	FIF_CONTROL |				\
11042 	FIF_PSPOLL |				\
11043 	FIF_OTHER_BSS |				\
11044 	FIF_BCN_PRBRESP_PROMISC |		\
11045 	FIF_PROBE_REQ |				\
11046 	FIF_FCSFAIL)
11047 
ath12k_mac_op_configure_filter(struct ieee80211_hw * hw,unsigned int changed_flags,unsigned int * total_flags,u64 multicast)11048 void ath12k_mac_op_configure_filter(struct ieee80211_hw *hw,
11049 				    unsigned int changed_flags,
11050 				    unsigned int *total_flags,
11051 				    u64 multicast)
11052 {
11053 	struct ath12k_hw *ah = ath12k_hw_to_ah(hw);
11054 	struct ath12k *ar;
11055 
11056 	lockdep_assert_wiphy(hw->wiphy);
11057 
11058 	ar = ath12k_ah_to_ar(ah, 0);
11059 
11060 	*total_flags &= SUPPORTED_FILTERS;
11061 	ar->filter_flags = *total_flags;
11062 }
11063 EXPORT_SYMBOL(ath12k_mac_op_configure_filter);
11064 
ath12k_mac_op_get_antenna(struct ieee80211_hw * hw,int radio_idx,u32 * tx_ant,u32 * rx_ant)11065 int ath12k_mac_op_get_antenna(struct ieee80211_hw *hw, int radio_idx,
11066 			      u32 *tx_ant, u32 *rx_ant)
11067 {
11068 	struct ath12k_hw *ah = ath12k_hw_to_ah(hw);
11069 	int antennas_rx = 0, antennas_tx = 0;
11070 	struct ath12k *ar;
11071 	int i;
11072 
11073 	lockdep_assert_wiphy(hw->wiphy);
11074 
11075 	for_each_ar(ah, ar, i) {
11076 		antennas_rx = max_t(u32, antennas_rx, ar->cfg_rx_chainmask);
11077 		antennas_tx = max_t(u32, antennas_tx, ar->cfg_tx_chainmask);
11078 	}
11079 
11080 	*tx_ant = antennas_tx;
11081 	*rx_ant = antennas_rx;
11082 
11083 	return 0;
11084 }
11085 EXPORT_SYMBOL(ath12k_mac_op_get_antenna);
11086 
ath12k_mac_op_set_antenna(struct ieee80211_hw * hw,int radio_idx,u32 tx_ant,u32 rx_ant)11087 int ath12k_mac_op_set_antenna(struct ieee80211_hw *hw, int radio_idx,
11088 			      u32 tx_ant, u32 rx_ant)
11089 {
11090 	struct ath12k_hw *ah = ath12k_hw_to_ah(hw);
11091 	struct ath12k *ar;
11092 	int ret = 0;
11093 	int i;
11094 
11095 	lockdep_assert_wiphy(hw->wiphy);
11096 
11097 	for_each_ar(ah, ar, i) {
11098 		ret = __ath12k_set_antenna(ar, tx_ant, rx_ant);
11099 		if (ret)
11100 			break;
11101 	}
11102 
11103 	return ret;
11104 }
11105 EXPORT_SYMBOL(ath12k_mac_op_set_antenna);
11106 
ath12k_mac_ampdu_action(struct ieee80211_hw * hw,struct ieee80211_vif * vif,struct ieee80211_ampdu_params * params,u8 link_id)11107 static int ath12k_mac_ampdu_action(struct ieee80211_hw *hw,
11108 				   struct ieee80211_vif *vif,
11109 				   struct ieee80211_ampdu_params *params,
11110 				   u8 link_id)
11111 {
11112 	struct ath12k *ar;
11113 	int ret = -EINVAL;
11114 
11115 	lockdep_assert_wiphy(hw->wiphy);
11116 
11117 	ar = ath12k_get_ar_by_vif(hw, vif, link_id);
11118 	if (!ar)
11119 		return -EINVAL;
11120 
11121 	switch (params->action) {
11122 	case IEEE80211_AMPDU_RX_START:
11123 		ret = ath12k_dp_rx_ampdu_start(ar, params, link_id);
11124 		break;
11125 	case IEEE80211_AMPDU_RX_STOP:
11126 		ret = ath12k_dp_rx_ampdu_stop(ar, params, link_id);
11127 		break;
11128 	case IEEE80211_AMPDU_TX_START:
11129 	case IEEE80211_AMPDU_TX_STOP_CONT:
11130 	case IEEE80211_AMPDU_TX_STOP_FLUSH:
11131 	case IEEE80211_AMPDU_TX_STOP_FLUSH_CONT:
11132 	case IEEE80211_AMPDU_TX_OPERATIONAL:
11133 		/* Tx A-MPDU aggregation offloaded to hw/fw so deny mac80211
11134 		 * Tx aggregation requests.
11135 		 */
11136 		ret = -EOPNOTSUPP;
11137 		break;
11138 	}
11139 
11140 	if (ret)
11141 		ath12k_warn(ar->ab, "unable to perform ampdu action %d for vif %pM link %u ret %d\n",
11142 			    params->action, vif->addr, link_id, ret);
11143 
11144 	return ret;
11145 }
11146 
ath12k_mac_op_ampdu_action(struct ieee80211_hw * hw,struct ieee80211_vif * vif,struct ieee80211_ampdu_params * params)11147 int ath12k_mac_op_ampdu_action(struct ieee80211_hw *hw,
11148 			       struct ieee80211_vif *vif,
11149 			       struct ieee80211_ampdu_params *params)
11150 {
11151 	struct ieee80211_sta *sta = params->sta;
11152 	struct ath12k_sta *ahsta = ath12k_sta_to_ahsta(sta);
11153 	unsigned long links_map = ahsta->links_map;
11154 	int ret = -EINVAL;
11155 	u8 link_id;
11156 
11157 	lockdep_assert_wiphy(hw->wiphy);
11158 
11159 	if (WARN_ON(!links_map))
11160 		return ret;
11161 
11162 	for_each_set_bit(link_id, &links_map, IEEE80211_MLD_MAX_NUM_LINKS) {
11163 		ret = ath12k_mac_ampdu_action(hw, vif, params, link_id);
11164 		if (ret)
11165 			return ret;
11166 	}
11167 
11168 	return 0;
11169 }
11170 EXPORT_SYMBOL(ath12k_mac_op_ampdu_action);
11171 
ath12k_mac_op_add_chanctx(struct ieee80211_hw * hw,struct ieee80211_chanctx_conf * ctx)11172 int ath12k_mac_op_add_chanctx(struct ieee80211_hw *hw,
11173 			      struct ieee80211_chanctx_conf *ctx)
11174 {
11175 	struct ath12k *ar;
11176 	struct ath12k_base *ab;
11177 
11178 	lockdep_assert_wiphy(hw->wiphy);
11179 
11180 	ar = ath12k_get_ar_by_ctx(hw, ctx);
11181 	if (!ar)
11182 		return -EINVAL;
11183 
11184 	ab = ar->ab;
11185 
11186 	ath12k_dbg(ab, ATH12K_DBG_MAC,
11187 		   "mac chanctx add freq %u width %d ptr %p\n",
11188 		   ctx->def.chan->center_freq, ctx->def.width, ctx);
11189 
11190 	spin_lock_bh(&ar->data_lock);
11191 	/* TODO: In case of multiple channel context, populate rx_channel from
11192 	 * Rx PPDU desc information.
11193 	 */
11194 	ar->rx_channel = ctx->def.chan;
11195 	spin_unlock_bh(&ar->data_lock);
11196 	ar->chan_tx_pwr = ATH12K_PDEV_TX_POWER_INVALID;
11197 
11198 	return 0;
11199 }
11200 EXPORT_SYMBOL(ath12k_mac_op_add_chanctx);
11201 
ath12k_mac_op_remove_chanctx(struct ieee80211_hw * hw,struct ieee80211_chanctx_conf * ctx)11202 void ath12k_mac_op_remove_chanctx(struct ieee80211_hw *hw,
11203 				  struct ieee80211_chanctx_conf *ctx)
11204 {
11205 	struct ath12k *ar;
11206 	struct ath12k_base *ab;
11207 
11208 	lockdep_assert_wiphy(hw->wiphy);
11209 
11210 	ar = ath12k_get_ar_by_ctx(hw, ctx);
11211 	if (!ar)
11212 		return;
11213 
11214 	ab = ar->ab;
11215 
11216 	ath12k_dbg(ab, ATH12K_DBG_MAC,
11217 		   "mac chanctx remove freq %u width %d ptr %p\n",
11218 		   ctx->def.chan->center_freq, ctx->def.width, ctx);
11219 
11220 	spin_lock_bh(&ar->data_lock);
11221 	/* TODO: In case of there is one more channel context left, populate
11222 	 * rx_channel with the channel of that remaining channel context.
11223 	 */
11224 	ar->rx_channel = NULL;
11225 	spin_unlock_bh(&ar->data_lock);
11226 	ar->chan_tx_pwr = ATH12K_PDEV_TX_POWER_INVALID;
11227 }
11228 EXPORT_SYMBOL(ath12k_mac_op_remove_chanctx);
11229 
11230 static enum wmi_phy_mode
ath12k_mac_check_down_grade_phy_mode(struct ath12k * ar,enum wmi_phy_mode mode,enum nl80211_band band,enum nl80211_iftype type)11231 ath12k_mac_check_down_grade_phy_mode(struct ath12k *ar,
11232 				     enum wmi_phy_mode mode,
11233 				     enum nl80211_band band,
11234 				     enum nl80211_iftype type)
11235 {
11236 	struct ieee80211_sta_eht_cap *eht_cap = NULL;
11237 	enum wmi_phy_mode down_mode;
11238 	int n = ar->mac.sbands[band].n_iftype_data;
11239 	int i;
11240 	struct ieee80211_sband_iftype_data *data;
11241 
11242 	if (mode < MODE_11BE_EHT20)
11243 		return mode;
11244 
11245 	data = ar->mac.iftype[band];
11246 	for (i = 0; i < n; i++) {
11247 		if (data[i].types_mask & BIT(type)) {
11248 			eht_cap = &data[i].eht_cap;
11249 			break;
11250 		}
11251 	}
11252 
11253 	if (eht_cap && eht_cap->has_eht)
11254 		return mode;
11255 
11256 	switch (mode) {
11257 	case MODE_11BE_EHT20:
11258 		down_mode = MODE_11AX_HE20;
11259 		break;
11260 	case MODE_11BE_EHT40:
11261 		down_mode = MODE_11AX_HE40;
11262 		break;
11263 	case MODE_11BE_EHT80:
11264 		down_mode = MODE_11AX_HE80;
11265 		break;
11266 	case MODE_11BE_EHT80_80:
11267 		down_mode = MODE_11AX_HE80_80;
11268 		break;
11269 	case MODE_11BE_EHT160:
11270 	case MODE_11BE_EHT160_160:
11271 	case MODE_11BE_EHT320:
11272 		down_mode = MODE_11AX_HE160;
11273 		break;
11274 	case MODE_11BE_EHT20_2G:
11275 		down_mode = MODE_11AX_HE20_2G;
11276 		break;
11277 	case MODE_11BE_EHT40_2G:
11278 		down_mode = MODE_11AX_HE40_2G;
11279 		break;
11280 	default:
11281 		down_mode = mode;
11282 		break;
11283 	}
11284 
11285 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
11286 		   "mac vdev start phymode %s downgrade to %s\n",
11287 		   ath12k_mac_phymode_str(mode),
11288 		   ath12k_mac_phymode_str(down_mode));
11289 
11290 	return down_mode;
11291 }
11292 
11293 static void
ath12k_mac_mlo_get_vdev_args(struct ath12k_link_vif * arvif,struct wmi_ml_arg * ml_arg)11294 ath12k_mac_mlo_get_vdev_args(struct ath12k_link_vif *arvif,
11295 			     struct wmi_ml_arg *ml_arg)
11296 {
11297 	struct ath12k_vif *ahvif = arvif->ahvif;
11298 	struct wmi_ml_partner_info *partner_info;
11299 	struct ieee80211_bss_conf *link_conf;
11300 	struct ath12k_link_vif *arvif_p;
11301 	unsigned long links;
11302 	u8 link_id;
11303 
11304 	lockdep_assert_wiphy(ahvif->ah->hw->wiphy);
11305 
11306 	if (!ath12k_mac_is_ml_arvif(arvif))
11307 		return;
11308 
11309 	if (hweight16(ahvif->vif->valid_links) > ATH12K_WMI_MLO_MAX_LINKS)
11310 		return;
11311 
11312 	ml_arg->enabled = true;
11313 
11314 	/* Driver always add a new link via VDEV START, FW takes
11315 	 * care of internally adding this link to existing
11316 	 * link vdevs which are advertised as partners below
11317 	 */
11318 	ml_arg->link_add = true;
11319 
11320 	ml_arg->assoc_link = arvif->is_sta_assoc_link;
11321 
11322 	ml_arg->ieee_link_id = arvif->link_id;
11323 
11324 	partner_info = ml_arg->partner_info;
11325 
11326 	links = ahvif->links_map;
11327 	for_each_set_bit(link_id, &links, IEEE80211_MLD_MAX_NUM_LINKS) {
11328 		arvif_p = wiphy_dereference(ahvif->ah->hw->wiphy, ahvif->link[link_id]);
11329 
11330 		if (WARN_ON(!arvif_p))
11331 			continue;
11332 
11333 		if (arvif == arvif_p)
11334 			continue;
11335 
11336 		if (!arvif_p->is_started)
11337 			continue;
11338 
11339 		link_conf = wiphy_dereference(ahvif->ah->hw->wiphy,
11340 					      ahvif->vif->link_conf[arvif_p->link_id]);
11341 
11342 		if (!link_conf)
11343 			continue;
11344 
11345 		partner_info->vdev_id = arvif_p->vdev_id;
11346 		partner_info->hw_link_id = arvif_p->ar->pdev->hw_link_id;
11347 		partner_info->ieee_link_id = arvif_p->link_id;
11348 		ether_addr_copy(partner_info->addr, link_conf->addr);
11349 		ml_arg->num_partner_links++;
11350 		partner_info++;
11351 	}
11352 }
11353 
11354 static int
ath12k_mac_vdev_start_restart(struct ath12k_link_vif * arvif,struct ieee80211_chanctx_conf * ctx,bool restart)11355 ath12k_mac_vdev_start_restart(struct ath12k_link_vif *arvif,
11356 			      struct ieee80211_chanctx_conf *ctx,
11357 			      bool restart)
11358 {
11359 	struct ath12k *ar = arvif->ar;
11360 	struct ath12k_base *ab = ar->ab;
11361 	struct wmi_vdev_start_req_arg arg = {};
11362 	const struct cfg80211_chan_def *chandef = &ctx->def;
11363 	struct ieee80211_hw *hw = ath12k_ar_to_hw(ar);
11364 	struct ath12k_vif *ahvif = arvif->ahvif;
11365 	struct ieee80211_bss_conf *link_conf;
11366 	unsigned int dfs_cac_time;
11367 	int ret;
11368 
11369 	lockdep_assert_wiphy(hw->wiphy);
11370 
11371 	link_conf = ath12k_mac_get_link_bss_conf(arvif);
11372 	if (!link_conf) {
11373 		ath12k_warn(ar->ab, "unable to access bss link conf in vdev start for vif %pM link %u\n",
11374 			    ahvif->vif->addr, arvif->link_id);
11375 		return -ENOLINK;
11376 	}
11377 
11378 	reinit_completion(&ar->vdev_setup_done);
11379 
11380 	arg.vdev_id = arvif->vdev_id;
11381 	arg.dtim_period = arvif->dtim_period;
11382 	arg.bcn_intval = arvif->beacon_interval;
11383 	arg.punct_bitmap = ~arvif->punct_bitmap;
11384 
11385 	arg.freq = chandef->chan->center_freq;
11386 	arg.band_center_freq1 = chandef->center_freq1;
11387 	arg.band_center_freq2 = chandef->center_freq2;
11388 	arg.mode = ath12k_phymodes[chandef->chan->band][chandef->width];
11389 
11390 	arg.mode = ath12k_mac_check_down_grade_phy_mode(ar, arg.mode,
11391 							chandef->chan->band,
11392 							ahvif->vif->type);
11393 	arg.min_power = 0;
11394 	arg.max_power = chandef->chan->max_power;
11395 	arg.max_reg_power = chandef->chan->max_reg_power;
11396 	arg.max_antenna_gain = chandef->chan->max_antenna_gain;
11397 
11398 	arg.pref_tx_streams = ar->num_tx_chains;
11399 	arg.pref_rx_streams = ar->num_rx_chains;
11400 
11401 	arg.mbssid_flags = WMI_VDEV_MBSSID_FLAGS_NON_MBSSID_AP;
11402 	arg.mbssid_tx_vdev_id = 0;
11403 	if (test_bit(WMI_TLV_SERVICE_MBSS_PARAM_IN_VDEV_START_SUPPORT,
11404 		     ar->ab->wmi_ab.svc_map)) {
11405 		ret = ath12k_mac_setup_vdev_params_mbssid(arvif,
11406 							  &arg.mbssid_flags,
11407 							  &arg.mbssid_tx_vdev_id);
11408 		if (ret)
11409 			return ret;
11410 	}
11411 
11412 	if (ahvif->vdev_type == WMI_VDEV_TYPE_AP) {
11413 		arg.ssid = ahvif->u.ap.ssid;
11414 		arg.ssid_len = ahvif->u.ap.ssid_len;
11415 		arg.hidden_ssid = ahvif->u.ap.hidden_ssid;
11416 
11417 		/* For now allow DFS for AP mode */
11418 		arg.chan_radar = !!(chandef->chan->flags & IEEE80211_CHAN_RADAR);
11419 
11420 		arg.freq2_radar = ctx->radar_enabled;
11421 
11422 		arg.passive = arg.chan_radar;
11423 
11424 		spin_lock_bh(&ab->base_lock);
11425 		arg.regdomain = ar->ab->dfs_region;
11426 		spin_unlock_bh(&ab->base_lock);
11427 
11428 		/* TODO: Notify if secondary 80Mhz also needs radar detection */
11429 	}
11430 
11431 	arg.passive |= !!(chandef->chan->flags & IEEE80211_CHAN_NO_IR);
11432 
11433 	if (!restart)
11434 		ath12k_mac_mlo_get_vdev_args(arvif, &arg.ml);
11435 
11436 	ath12k_dbg(ab, ATH12K_DBG_MAC,
11437 		   "mac vdev %d start center_freq %d phymode %s punct_bitmap 0x%x\n",
11438 		   arg.vdev_id, arg.freq,
11439 		   ath12k_mac_phymode_str(arg.mode), arg.punct_bitmap);
11440 
11441 	ret = ath12k_wmi_vdev_start(ar, &arg, restart);
11442 	if (ret) {
11443 		ath12k_warn(ar->ab, "failed to %s WMI vdev %i\n",
11444 			    restart ? "restart" : "start", arg.vdev_id);
11445 		return ret;
11446 	}
11447 
11448 	ret = ath12k_mac_vdev_setup_sync(ar);
11449 	if (ret) {
11450 		ath12k_warn(ab, "failed to synchronize setup for vdev %i %s: %d\n",
11451 			    arg.vdev_id, restart ? "restart" : "start", ret);
11452 		return ret;
11453 	}
11454 
11455 	/* TODO: For now we only set TPC power here. However when
11456 	 * channel changes, say CSA, it should be updated again.
11457 	 */
11458 	if (ath12k_mac_supports_tpc(ar, ahvif, chandef)) {
11459 		ath12k_mac_fill_reg_tpc_info(ar, arvif, ctx);
11460 		ath12k_wmi_send_vdev_set_tpc_power(ar, arvif->vdev_id,
11461 						   &arvif->reg_tpc_info);
11462 	}
11463 
11464 	ar->num_started_vdevs++;
11465 	ath12k_dbg(ab, ATH12K_DBG_MAC,  "vdev %pM started, vdev_id %d\n",
11466 		   ahvif->vif->addr, arvif->vdev_id);
11467 
11468 	/* Enable CAC Running Flag in the driver by checking all sub-channel's DFS
11469 	 * state as NL80211_DFS_USABLE which indicates CAC needs to be
11470 	 * done before channel usage. This flag is used to drop rx packets.
11471 	 * during CAC.
11472 	 */
11473 	/* TODO: Set the flag for other interface types as required */
11474 	if (arvif->ahvif->vdev_type == WMI_VDEV_TYPE_AP && ctx->radar_enabled &&
11475 	    cfg80211_chandef_dfs_usable(hw->wiphy, chandef)) {
11476 		set_bit(ATH12K_FLAG_CAC_RUNNING, &ar->dev_flags);
11477 		dfs_cac_time = cfg80211_chandef_dfs_cac_time(hw->wiphy, chandef);
11478 
11479 		ath12k_dbg(ab, ATH12K_DBG_MAC,
11480 			   "CAC started dfs_cac_time %u center_freq %d center_freq1 %d for vdev %d\n",
11481 			   dfs_cac_time, arg.freq, arg.band_center_freq1, arg.vdev_id);
11482 	}
11483 
11484 	ret = ath12k_mac_set_txbf_conf(arvif);
11485 	if (ret)
11486 		ath12k_warn(ab, "failed to set txbf conf for vdev %d: %d\n",
11487 			    arvif->vdev_id, ret);
11488 
11489 	return 0;
11490 }
11491 
ath12k_mac_vdev_start(struct ath12k_link_vif * arvif,struct ieee80211_chanctx_conf * ctx)11492 static int ath12k_mac_vdev_start(struct ath12k_link_vif *arvif,
11493 				 struct ieee80211_chanctx_conf *ctx)
11494 {
11495 	return ath12k_mac_vdev_start_restart(arvif, ctx, false);
11496 }
11497 
ath12k_mac_vdev_restart(struct ath12k_link_vif * arvif,struct ieee80211_chanctx_conf * ctx)11498 static int ath12k_mac_vdev_restart(struct ath12k_link_vif *arvif,
11499 				   struct ieee80211_chanctx_conf *ctx)
11500 {
11501 	return ath12k_mac_vdev_start_restart(arvif, ctx, true);
11502 }
11503 
11504 struct ath12k_mac_change_chanctx_arg {
11505 	struct ieee80211_chanctx_conf *ctx;
11506 	struct ieee80211_vif_chanctx_switch *vifs;
11507 	int n_vifs;
11508 	int next_vif;
11509 	struct ath12k *ar;
11510 };
11511 
11512 static void
ath12k_mac_change_chanctx_cnt_iter(void * data,u8 * mac,struct ieee80211_vif * vif)11513 ath12k_mac_change_chanctx_cnt_iter(void *data, u8 *mac,
11514 				   struct ieee80211_vif *vif)
11515 {
11516 	struct ath12k_vif *ahvif = ath12k_vif_to_ahvif(vif);
11517 	struct ath12k_mac_change_chanctx_arg *arg = data;
11518 	struct ieee80211_bss_conf *link_conf;
11519 	struct ath12k_link_vif *arvif;
11520 	unsigned long links_map;
11521 	u8 link_id;
11522 
11523 	lockdep_assert_wiphy(ahvif->ah->hw->wiphy);
11524 
11525 	links_map = ahvif->links_map;
11526 	for_each_set_bit(link_id, &links_map, IEEE80211_MLD_MAX_NUM_LINKS) {
11527 		arvif = wiphy_dereference(ahvif->ah->hw->wiphy, ahvif->link[link_id]);
11528 		if (WARN_ON(!arvif))
11529 			continue;
11530 
11531 		if (!arvif->is_created || arvif->ar != arg->ar)
11532 			continue;
11533 
11534 		link_conf = wiphy_dereference(ahvif->ah->hw->wiphy,
11535 					      vif->link_conf[link_id]);
11536 		if (WARN_ON(!link_conf))
11537 			continue;
11538 
11539 		if (rcu_access_pointer(link_conf->chanctx_conf) != arg->ctx)
11540 			continue;
11541 
11542 		arg->n_vifs++;
11543 	}
11544 }
11545 
11546 static void
ath12k_mac_change_chanctx_fill_iter(void * data,u8 * mac,struct ieee80211_vif * vif)11547 ath12k_mac_change_chanctx_fill_iter(void *data, u8 *mac,
11548 				    struct ieee80211_vif *vif)
11549 {
11550 	struct ath12k_vif *ahvif = ath12k_vif_to_ahvif(vif);
11551 	struct ath12k_mac_change_chanctx_arg *arg = data;
11552 	struct ieee80211_bss_conf *link_conf;
11553 	struct ieee80211_chanctx_conf *ctx;
11554 	struct ath12k_link_vif *arvif;
11555 	unsigned long links_map;
11556 	u8 link_id;
11557 
11558 	lockdep_assert_wiphy(ahvif->ah->hw->wiphy);
11559 
11560 	links_map = ahvif->links_map;
11561 	for_each_set_bit(link_id, &links_map, IEEE80211_MLD_MAX_NUM_LINKS) {
11562 		arvif = wiphy_dereference(ahvif->ah->hw->wiphy, ahvif->link[link_id]);
11563 		if (WARN_ON(!arvif))
11564 			continue;
11565 
11566 		if (!arvif->is_created || arvif->ar != arg->ar)
11567 			continue;
11568 
11569 		link_conf = wiphy_dereference(ahvif->ah->hw->wiphy,
11570 					      vif->link_conf[arvif->link_id]);
11571 		if (WARN_ON(!link_conf))
11572 			continue;
11573 
11574 		ctx = rcu_access_pointer(link_conf->chanctx_conf);
11575 		if (ctx != arg->ctx)
11576 			continue;
11577 
11578 		if (WARN_ON(arg->next_vif == arg->n_vifs))
11579 			return;
11580 
11581 		arg->vifs[arg->next_vif].vif = vif;
11582 		arg->vifs[arg->next_vif].old_ctx = ctx;
11583 		arg->vifs[arg->next_vif].new_ctx = ctx;
11584 		arg->vifs[arg->next_vif].link_conf = link_conf;
11585 		arg->next_vif++;
11586 	}
11587 }
11588 
ath12k_mac_nlwidth_to_wmiwidth(enum nl80211_chan_width width)11589 static u32 ath12k_mac_nlwidth_to_wmiwidth(enum nl80211_chan_width width)
11590 {
11591 	switch (width) {
11592 	case NL80211_CHAN_WIDTH_20:
11593 		return WMI_CHAN_WIDTH_20;
11594 	case NL80211_CHAN_WIDTH_40:
11595 		return WMI_CHAN_WIDTH_40;
11596 	case NL80211_CHAN_WIDTH_80:
11597 		return WMI_CHAN_WIDTH_80;
11598 	case NL80211_CHAN_WIDTH_160:
11599 		return WMI_CHAN_WIDTH_160;
11600 	case NL80211_CHAN_WIDTH_80P80:
11601 		return WMI_CHAN_WIDTH_80P80;
11602 	case NL80211_CHAN_WIDTH_5:
11603 		return WMI_CHAN_WIDTH_5;
11604 	case NL80211_CHAN_WIDTH_10:
11605 		return WMI_CHAN_WIDTH_10;
11606 	case NL80211_CHAN_WIDTH_320:
11607 		return WMI_CHAN_WIDTH_320;
11608 	default:
11609 		WARN_ON(1);
11610 		return WMI_CHAN_WIDTH_20;
11611 	}
11612 }
11613 
ath12k_mac_update_peer_puncturing_width(struct ath12k * ar,struct ath12k_link_vif * arvif,struct cfg80211_chan_def def)11614 static int ath12k_mac_update_peer_puncturing_width(struct ath12k *ar,
11615 						   struct ath12k_link_vif *arvif,
11616 						   struct cfg80211_chan_def def)
11617 {
11618 	u32 param_id, param_value;
11619 	int ret;
11620 
11621 	if (arvif->ahvif->vdev_type != WMI_VDEV_TYPE_STA)
11622 		return 0;
11623 
11624 	param_id = WMI_PEER_CHWIDTH_PUNCTURE_20MHZ_BITMAP;
11625 	param_value = ath12k_mac_nlwidth_to_wmiwidth(def.width) |
11626 		u32_encode_bits((~def.punctured),
11627 				WMI_PEER_PUNCTURE_BITMAP);
11628 
11629 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
11630 		   "punctured bitmap %02x width %d vdev %d\n",
11631 		   def.punctured, def.width, arvif->vdev_id);
11632 
11633 	ret = ath12k_wmi_set_peer_param(ar, arvif->bssid,
11634 					arvif->vdev_id, param_id,
11635 					param_value);
11636 
11637 	return ret;
11638 }
11639 
11640 static void
ath12k_mac_update_vif_chan(struct ath12k * ar,struct ieee80211_vif_chanctx_switch * vifs,int n_vifs)11641 ath12k_mac_update_vif_chan(struct ath12k *ar,
11642 			   struct ieee80211_vif_chanctx_switch *vifs,
11643 			   int n_vifs)
11644 {
11645 	struct ath12k_incumbent_signal_interference *incumbent;
11646 	struct ath12k_wmi_vdev_up_params params = {};
11647 	struct ieee80211_bss_conf *link_conf;
11648 	struct cfg80211_chan_def *chandef;
11649 	struct ath12k_base *ab = ar->ab;
11650 	struct ath12k_link_vif *arvif;
11651 	struct ieee80211_vif *vif;
11652 	struct ath12k_vif *ahvif;
11653 	u8 link_id;
11654 	int ret;
11655 	int i;
11656 	bool monitor_vif = false;
11657 
11658 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
11659 
11660 	for (i = 0; i < n_vifs; i++) {
11661 		vif = vifs[i].vif;
11662 		ahvif = ath12k_vif_to_ahvif(vif);
11663 		link_conf = vifs[i].link_conf;
11664 		link_id = link_conf->link_id;
11665 		arvif = wiphy_dereference(ath12k_ar_to_hw(ar)->wiphy,
11666 					  ahvif->link[link_id]);
11667 
11668 		if (vif->type == NL80211_IFTYPE_MONITOR) {
11669 			monitor_vif = true;
11670 			continue;
11671 		}
11672 
11673 		if (WARN_ON(!arvif))
11674 			continue;
11675 
11676 		ath12k_dbg(ab, ATH12K_DBG_MAC,
11677 			   "mac chanctx switch vdev_id %i freq %u->%u width %d->%d\n",
11678 			   arvif->vdev_id,
11679 			   vifs[i].old_ctx->def.chan->center_freq,
11680 			   vifs[i].new_ctx->def.chan->center_freq,
11681 			   vifs[i].old_ctx->def.width,
11682 			   vifs[i].new_ctx->def.width);
11683 
11684 		if (WARN_ON(!arvif->is_started))
11685 			continue;
11686 
11687 		arvif->punct_bitmap = vifs[i].new_ctx->def.punctured;
11688 
11689 		/* Firmware expect vdev_restart only if vdev is up.
11690 		 * If vdev is down then it expect vdev_stop->vdev_start.
11691 		 */
11692 		if (arvif->is_up) {
11693 			ret = ath12k_mac_vdev_restart(arvif, vifs[i].new_ctx);
11694 			if (ret) {
11695 				ath12k_warn(ab, "failed to restart vdev %d: %d\n",
11696 					    arvif->vdev_id, ret);
11697 				continue;
11698 			}
11699 		} else {
11700 			ret = ath12k_mac_vdev_stop(arvif);
11701 			if (ret) {
11702 				ath12k_warn(ab, "failed to stop vdev %d: %d\n",
11703 					    arvif->vdev_id, ret);
11704 				continue;
11705 			}
11706 
11707 			ret = ath12k_mac_vdev_start(arvif, vifs[i].new_ctx);
11708 			if (ret)
11709 				ath12k_warn(ab, "failed to start vdev %d: %d\n",
11710 					    arvif->vdev_id, ret);
11711 			continue;
11712 		}
11713 
11714 		ret = ath12k_mac_update_peer_puncturing_width(arvif->ar, arvif,
11715 							      vifs[i].new_ctx->def);
11716 		if (ret) {
11717 			ath12k_warn(ar->ab,
11718 				    "failed to update puncturing bitmap %02x and width %d: %d\n",
11719 				    vifs[i].new_ctx->def.punctured,
11720 				    vifs[i].new_ctx->def.width, ret);
11721 			continue;
11722 		}
11723 
11724 		/* Defer VDEV bring-up during CSA to avoid installing stale
11725 		 * beacon templates. The beacon content is updated only
11726 		 * after CSA finalize, so we mark CSA in progress and skip
11727 		 * VDEV_UP for now. It will be handled later in
11728 		 * bss_info_changed().
11729 		 */
11730 		if (link_conf->csa_active &&
11731 		    arvif->ahvif->vdev_type == WMI_VDEV_TYPE_AP) {
11732 			arvif->is_csa_in_progress = true;
11733 			continue;
11734 		}
11735 
11736 		ret = ath12k_mac_setup_bcn_tmpl(arvif);
11737 		if (ret)
11738 			ath12k_warn(ab, "failed to update bcn tmpl during csa: %d\n",
11739 				    ret);
11740 
11741 		memset(&params, 0, sizeof(params));
11742 		params.vdev_id = arvif->vdev_id;
11743 		params.aid = ahvif->aid;
11744 		params.bssid = arvif->bssid;
11745 		params.tx_bssid = ath12k_mac_get_tx_bssid(arvif);
11746 		if (params.tx_bssid) {
11747 			params.nontx_profile_idx = link_conf->bssid_index;
11748 			params.nontx_profile_cnt = 1 << link_conf->bssid_indicator;
11749 		}
11750 		ret = ath12k_wmi_vdev_up(arvif->ar, &params);
11751 		if (ret) {
11752 			ath12k_warn(ab, "failed to bring vdev up %d: %d\n",
11753 				    arvif->vdev_id, ret);
11754 			continue;
11755 		}
11756 	}
11757 
11758 	/* Restart the internal monitor vdev on new channel */
11759 	if (!monitor_vif && ar->monitor_vdev_created) {
11760 		if (!ath12k_mac_monitor_stop(ar))
11761 			ath12k_mac_monitor_start(ar);
11762 	}
11763 
11764 	incumbent = &ar->incumbent_signal_interference;
11765 	spin_lock_bh(&ar->data_lock);
11766 	if (incumbent->handling_in_progress) {
11767 		chandef = &vifs[0].new_ctx->def;
11768 		if (incumbent->chan_bw_interference_bitmap &
11769 		    ATH12K_WMI_DCS_SEG_PRI20) {
11770 			if (incumbent->center_freq !=
11771 			    chandef->chan->center_freq) {
11772 				incumbent->chan_bw_interference_bitmap = 0;
11773 				incumbent->handling_in_progress = false;
11774 				ath12k_dbg(ab, ATH12K_DBG_MAC,
11775 					   "incumbent signal interference chan switch completed\n");
11776 			} else {
11777 				ath12k_warn(ab,
11778 					    "incumbent signal interference chan switch not done, freq %u\n",
11779 					    incumbent->center_freq);
11780 			}
11781 		} else {
11782 			if (incumbent->center_freq !=
11783 			    chandef->chan->center_freq ||
11784 			    incumbent->width != chandef->width) {
11785 				incumbent->chan_bw_interference_bitmap = 0;
11786 				incumbent->handling_in_progress = false;
11787 				ath12k_dbg(ab, ATH12K_DBG_MAC,
11788 					   "Bandwidth/channel change due to incumbent signal interference completed\n");
11789 			} else {
11790 				ath12k_warn(ab, "Bandwidth/channel change due to incumbent sig intf not done intf_freq %u chan_freq %u intf_width %u chan_width %u\n",
11791 					    incumbent->center_freq,
11792 					    chandef->chan->center_freq,
11793 					    incumbent->width,
11794 					    chandef->width);
11795 			}
11796 		}
11797 	}
11798 	spin_unlock_bh(&ar->data_lock);
11799 }
11800 
11801 static void
ath12k_mac_update_active_vif_chan(struct ath12k * ar,struct ieee80211_chanctx_conf * ctx)11802 ath12k_mac_update_active_vif_chan(struct ath12k *ar,
11803 				  struct ieee80211_chanctx_conf *ctx)
11804 {
11805 	struct ath12k_mac_change_chanctx_arg arg = { .ctx = ctx, .ar = ar };
11806 	struct ieee80211_hw *hw = ath12k_ar_to_hw(ar);
11807 
11808 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
11809 
11810 	ieee80211_iterate_active_interfaces_atomic(hw,
11811 						   IEEE80211_IFACE_ITER_NORMAL,
11812 						   ath12k_mac_change_chanctx_cnt_iter,
11813 						   &arg);
11814 	if (arg.n_vifs == 0)
11815 		return;
11816 
11817 	arg.vifs = kzalloc_objs(arg.vifs[0], arg.n_vifs);
11818 	if (!arg.vifs)
11819 		return;
11820 
11821 	ieee80211_iterate_active_interfaces_atomic(hw,
11822 						   IEEE80211_IFACE_ITER_NORMAL,
11823 						   ath12k_mac_change_chanctx_fill_iter,
11824 						   &arg);
11825 
11826 	ath12k_mac_update_vif_chan(ar, arg.vifs, arg.n_vifs);
11827 
11828 	kfree(arg.vifs);
11829 }
11830 
ath12k_mac_op_change_chanctx(struct ieee80211_hw * hw,struct ieee80211_chanctx_conf * ctx,u32 changed)11831 void ath12k_mac_op_change_chanctx(struct ieee80211_hw *hw,
11832 				  struct ieee80211_chanctx_conf *ctx,
11833 				  u32 changed)
11834 {
11835 	struct ath12k *ar;
11836 	struct ath12k_base *ab;
11837 
11838 	lockdep_assert_wiphy(hw->wiphy);
11839 
11840 	ar = ath12k_get_ar_by_ctx(hw, ctx);
11841 	if (!ar)
11842 		return;
11843 
11844 	ab = ar->ab;
11845 
11846 	ath12k_dbg(ab, ATH12K_DBG_MAC,
11847 		   "mac chanctx change freq %u width %d ptr %p changed %x\n",
11848 		   ctx->def.chan->center_freq, ctx->def.width, ctx, changed);
11849 
11850 	/* This shouldn't really happen because channel switching should use
11851 	 * switch_vif_chanctx().
11852 	 */
11853 	if (WARN_ON(changed & IEEE80211_CHANCTX_CHANGE_CHANNEL))
11854 		return;
11855 
11856 	if (changed & IEEE80211_CHANCTX_CHANGE_WIDTH ||
11857 	    changed & IEEE80211_CHANCTX_CHANGE_RADAR ||
11858 	    changed & IEEE80211_CHANCTX_CHANGE_PUNCTURING)
11859 		ath12k_mac_update_active_vif_chan(ar, ctx);
11860 
11861 	/* TODO: Recalc radar detection */
11862 }
11863 EXPORT_SYMBOL(ath12k_mac_op_change_chanctx);
11864 
ath12k_start_vdev_delay(struct ath12k * ar,struct ath12k_link_vif * arvif)11865 static int ath12k_start_vdev_delay(struct ath12k *ar,
11866 				   struct ath12k_link_vif *arvif)
11867 {
11868 	struct ath12k_base *ab = ar->ab;
11869 	struct ath12k_vif *ahvif = arvif->ahvif;
11870 	struct ieee80211_vif *vif = ath12k_ahvif_to_vif(arvif->ahvif);
11871 	struct ieee80211_chanctx_conf *chanctx;
11872 	struct ieee80211_bss_conf *link_conf;
11873 	int ret;
11874 
11875 	if (WARN_ON(arvif->is_started))
11876 		return -EBUSY;
11877 
11878 	link_conf = ath12k_mac_get_link_bss_conf(arvif);
11879 	if (!link_conf) {
11880 		ath12k_warn(ab, "failed to get link conf for vdev %u\n", arvif->vdev_id);
11881 		return -EINVAL;
11882 	}
11883 
11884 	chanctx	= wiphy_dereference(ath12k_ar_to_hw(arvif->ar)->wiphy,
11885 				    link_conf->chanctx_conf);
11886 	ret = ath12k_mac_vdev_start(arvif, chanctx);
11887 	if (ret) {
11888 		ath12k_warn(ab, "failed to start vdev %i addr %pM on freq %d: %d\n",
11889 			    arvif->vdev_id, vif->addr,
11890 			    chanctx->def.chan->center_freq, ret);
11891 		return ret;
11892 	}
11893 
11894 	if (ahvif->vdev_type == WMI_VDEV_TYPE_MONITOR) {
11895 		ret = ath12k_monitor_vdev_up(ar, arvif->vdev_id);
11896 		if (ret) {
11897 			ath12k_warn(ab, "failed put monitor up: %d\n", ret);
11898 			return ret;
11899 		}
11900 	}
11901 
11902 	arvif->is_started = true;
11903 
11904 	/* TODO: Setup ps and cts/rts protection */
11905 	return 0;
11906 }
11907 
ath12k_mac_get_num_pwr_levels(struct cfg80211_chan_def * chan_def)11908 static u8 ath12k_mac_get_num_pwr_levels(struct cfg80211_chan_def *chan_def)
11909 {
11910 	if (chan_def->chan->flags & IEEE80211_CHAN_PSD) {
11911 		switch (chan_def->width) {
11912 		case NL80211_CHAN_WIDTH_20:
11913 			return 1;
11914 		case NL80211_CHAN_WIDTH_40:
11915 			return 2;
11916 		case NL80211_CHAN_WIDTH_80:
11917 			return 4;
11918 		case NL80211_CHAN_WIDTH_160:
11919 			return 8;
11920 		case NL80211_CHAN_WIDTH_320:
11921 			return 16;
11922 		default:
11923 			return 1;
11924 		}
11925 	} else {
11926 		switch (chan_def->width) {
11927 		case NL80211_CHAN_WIDTH_20:
11928 			return 1;
11929 		case NL80211_CHAN_WIDTH_40:
11930 			return 2;
11931 		case NL80211_CHAN_WIDTH_80:
11932 			return 3;
11933 		case NL80211_CHAN_WIDTH_160:
11934 			return 4;
11935 		case NL80211_CHAN_WIDTH_320:
11936 			return 5;
11937 		default:
11938 			return 1;
11939 		}
11940 	}
11941 }
11942 
ath12k_mac_get_6ghz_start_frequency(struct cfg80211_chan_def * chan_def)11943 static u16 ath12k_mac_get_6ghz_start_frequency(struct cfg80211_chan_def *chan_def)
11944 {
11945 	u16 diff_seq;
11946 
11947 	/* It is to get the lowest channel number's center frequency of the chan.
11948 	 * For example,
11949 	 * bandwidth=40 MHz, center frequency is 5965, lowest channel is 1
11950 	 * with center frequency 5955, its diff is 5965 - 5955 = 10.
11951 	 * bandwidth=80 MHz, center frequency is 5985, lowest channel is 1
11952 	 * with center frequency 5955, its diff is 5985 - 5955 = 30.
11953 	 * bandwidth=160 MHz, center frequency is 6025, lowest channel is 1
11954 	 * with center frequency 5955, its diff is 6025 - 5955 = 70.
11955 	 * bandwidth=320 MHz, center frequency is 6105, lowest channel is 1
11956 	 * with center frequency 5955, its diff is 6105 - 5955 = 70.
11957 	 */
11958 	switch (chan_def->width) {
11959 	case NL80211_CHAN_WIDTH_320:
11960 		diff_seq = 150;
11961 		break;
11962 	case NL80211_CHAN_WIDTH_160:
11963 		diff_seq = 70;
11964 		break;
11965 	case NL80211_CHAN_WIDTH_80:
11966 		diff_seq = 30;
11967 		break;
11968 	case NL80211_CHAN_WIDTH_40:
11969 		diff_seq = 10;
11970 		break;
11971 	default:
11972 		diff_seq = 0;
11973 	}
11974 
11975 	return chan_def->center_freq1 - diff_seq;
11976 }
11977 
ath12k_mac_get_seg_freq(struct cfg80211_chan_def * chan_def,u16 start_seq,u8 seq)11978 static u16 ath12k_mac_get_seg_freq(struct cfg80211_chan_def *chan_def,
11979 				   u16 start_seq, u8 seq)
11980 {
11981 	u16 seg_seq;
11982 
11983 	/* It is to get the center frequency of the specific bandwidth.
11984 	 * start_seq means the lowest channel number's center frequency.
11985 	 * seq 0/1/2/3 means 20 MHz/40 MHz/80 MHz/160 MHz.
11986 	 * For example,
11987 	 * lowest channel is 1, its center frequency 5955,
11988 	 * center frequency is 5955 when bandwidth=20 MHz, its diff is 5955 - 5955 = 0.
11989 	 * lowest channel is 1, its center frequency 5955,
11990 	 * center frequency is 5965 when bandwidth=40 MHz, its diff is 5965 - 5955 = 10.
11991 	 * lowest channel is 1, its center frequency 5955,
11992 	 * center frequency is 5985 when bandwidth=80 MHz, its diff is 5985 - 5955 = 30.
11993 	 * lowest channel is 1, its center frequency 5955,
11994 	 * center frequency is 6025 when bandwidth=160 MHz, its diff is 6025 - 5955 = 70.
11995 	 */
11996 	seg_seq = 10 * (BIT(seq) - 1);
11997 	return seg_seq + start_seq;
11998 }
11999 
ath12k_mac_get_psd_channel(struct ath12k * ar,u16 step_freq,u16 * start_freq,u16 * center_freq,u8 i,struct ieee80211_channel ** temp_chan,s8 * tx_power)12000 static void ath12k_mac_get_psd_channel(struct ath12k *ar,
12001 				       u16 step_freq,
12002 				       u16 *start_freq,
12003 				       u16 *center_freq,
12004 				       u8 i,
12005 				       struct ieee80211_channel **temp_chan,
12006 				       s8 *tx_power)
12007 {
12008 	/* It is to get the center frequency for each 20 MHz.
12009 	 * For example, if the chan is 160 MHz and center frequency is 6025,
12010 	 * then it include 8 channels, they are 1/5/9/13/17/21/25/29,
12011 	 * channel number 1's center frequency is 5955, it is parameter start_freq.
12012 	 * parameter i is the step of the 8 channels. i is 0~7 for the 8 channels.
12013 	 * the channel 1/5/9/13/17/21/25/29 maps i=0/1/2/3/4/5/6/7,
12014 	 * and maps its center frequency is 5955/5975/5995/6015/6035/6055/6075/6095,
12015 	 * the gap is 20 for each channel, parameter step_freq means the gap.
12016 	 * after get the center frequency of each channel, it is easy to find the
12017 	 * struct ieee80211_channel of it and get the max_reg_power.
12018 	 */
12019 	*center_freq = *start_freq + i * step_freq;
12020 	*temp_chan = ieee80211_get_channel(ar->ah->hw->wiphy, *center_freq);
12021 	*tx_power = (*temp_chan)->max_reg_power;
12022 }
12023 
ath12k_mac_get_eirp_power(struct ath12k * ar,u16 * start_freq,u16 * center_freq,u8 i,struct ieee80211_channel ** temp_chan,struct cfg80211_chan_def * def,s8 * tx_power)12024 static void ath12k_mac_get_eirp_power(struct ath12k *ar,
12025 				      u16 *start_freq,
12026 				      u16 *center_freq,
12027 				      u8 i,
12028 				      struct ieee80211_channel **temp_chan,
12029 				      struct cfg80211_chan_def *def,
12030 				      s8 *tx_power)
12031 {
12032 	/* It is to get the center frequency for 20 MHz/40 MHz/80 MHz/
12033 	 * 160 MHz bandwidth, and then plus 10 to the center frequency,
12034 	 * it is the center frequency of a channel number.
12035 	 * For example, when configured channel number is 1.
12036 	 * center frequency is 5965 when bandwidth=40 MHz, after plus 10, it is 5975,
12037 	 * then it is channel number 5.
12038 	 * center frequency is 5985 when bandwidth=80 MHz, after plus 10, it is 5995,
12039 	 * then it is channel number 9.
12040 	 * center frequency is 6025 when bandwidth=160 MHz, after plus 10, it is 6035,
12041 	 * then it is channel number 17.
12042 	 * after get the center frequency of each channel, it is easy to find the
12043 	 * struct ieee80211_channel of it and get the max_reg_power.
12044 	 */
12045 	*center_freq = ath12k_mac_get_seg_freq(def, *start_freq, i);
12046 
12047 	/* For the 20 MHz, its center frequency is same with same channel */
12048 	if (i != 0)
12049 		*center_freq += 10;
12050 
12051 	*temp_chan = ieee80211_get_channel(ar->ah->hw->wiphy, *center_freq);
12052 	*tx_power = (*temp_chan)->max_reg_power;
12053 }
12054 
ath12k_mac_fill_reg_tpc_info(struct ath12k * ar,struct ath12k_link_vif * arvif,struct ieee80211_chanctx_conf * ctx)12055 void ath12k_mac_fill_reg_tpc_info(struct ath12k *ar,
12056 				  struct ath12k_link_vif *arvif,
12057 				  struct ieee80211_chanctx_conf *ctx)
12058 {
12059 	struct ath12k_base *ab = ar->ab;
12060 	struct ath12k_reg_tpc_power_info *reg_tpc_info = &arvif->reg_tpc_info;
12061 	struct ieee80211_bss_conf *bss_conf = ath12k_mac_get_link_bss_conf(arvif);
12062 	struct ieee80211_channel *chan, *temp_chan;
12063 	u8 pwr_lvl_idx, num_pwr_levels, pwr_reduction;
12064 	bool is_psd_power = false, is_tpe_present = false;
12065 	s8 max_tx_power[ATH12K_NUM_PWR_LEVELS], psd_power, tx_power;
12066 	s8 eirp_power = 0;
12067 	struct ath12k_vif *ahvif = arvif->ahvif;
12068 	u16 start_freq, center_freq;
12069 	u8 reg_6ghz_power_mode;
12070 
12071 	chan = ctx->def.chan;
12072 	start_freq = ath12k_mac_get_6ghz_start_frequency(&ctx->def);
12073 	pwr_reduction = bss_conf->pwr_reduction;
12074 
12075 	if (arvif->reg_tpc_info.num_pwr_levels) {
12076 		is_tpe_present = true;
12077 		num_pwr_levels = arvif->reg_tpc_info.num_pwr_levels;
12078 	} else {
12079 		num_pwr_levels = ath12k_mac_get_num_pwr_levels(&ctx->def);
12080 	}
12081 
12082 	for (pwr_lvl_idx = 0; pwr_lvl_idx < num_pwr_levels; pwr_lvl_idx++) {
12083 		/* STA received TPE IE*/
12084 		if (is_tpe_present) {
12085 			/* local power is PSD power*/
12086 			if (chan->flags & IEEE80211_CHAN_PSD) {
12087 				/* Connecting AP is psd power */
12088 				if (reg_tpc_info->is_psd_power) {
12089 					is_psd_power = true;
12090 					ath12k_mac_get_psd_channel(ar, 20,
12091 								   &start_freq,
12092 								   &center_freq,
12093 								   pwr_lvl_idx,
12094 								   &temp_chan,
12095 								   &tx_power);
12096 					psd_power = temp_chan->psd;
12097 					eirp_power = tx_power;
12098 					max_tx_power[pwr_lvl_idx] =
12099 						min_t(s8,
12100 						      psd_power,
12101 						      reg_tpc_info->tpe[pwr_lvl_idx]);
12102 				/* Connecting AP is not psd power */
12103 				} else {
12104 					ath12k_mac_get_eirp_power(ar,
12105 								  &start_freq,
12106 								  &center_freq,
12107 								  pwr_lvl_idx,
12108 								  &temp_chan,
12109 								  &ctx->def,
12110 								  &tx_power);
12111 					psd_power = temp_chan->psd;
12112 					/* convert psd power to EIRP power based
12113 					 * on channel width
12114 					 */
12115 					tx_power =
12116 						min_t(s8, tx_power,
12117 						      psd_power + 13 + pwr_lvl_idx * 3);
12118 					max_tx_power[pwr_lvl_idx] =
12119 						min_t(s8,
12120 						      tx_power,
12121 						      reg_tpc_info->tpe[pwr_lvl_idx]);
12122 				}
12123 			/* local power is not PSD power */
12124 			} else {
12125 				/* Connecting AP is psd power */
12126 				if (reg_tpc_info->is_psd_power) {
12127 					is_psd_power = true;
12128 					ath12k_mac_get_psd_channel(ar, 20,
12129 								   &start_freq,
12130 								   &center_freq,
12131 								   pwr_lvl_idx,
12132 								   &temp_chan,
12133 								   &tx_power);
12134 					eirp_power = tx_power;
12135 					max_tx_power[pwr_lvl_idx] =
12136 						reg_tpc_info->tpe[pwr_lvl_idx];
12137 				/* Connecting AP is not psd power */
12138 				} else {
12139 					ath12k_mac_get_eirp_power(ar,
12140 								  &start_freq,
12141 								  &center_freq,
12142 								  pwr_lvl_idx,
12143 								  &temp_chan,
12144 								  &ctx->def,
12145 								  &tx_power);
12146 					max_tx_power[pwr_lvl_idx] =
12147 						min_t(s8,
12148 						      tx_power,
12149 						      reg_tpc_info->tpe[pwr_lvl_idx]);
12150 				}
12151 			}
12152 		/* STA not received TPE IE */
12153 		} else {
12154 			/* local power is PSD power*/
12155 			if (chan->flags & IEEE80211_CHAN_PSD) {
12156 				is_psd_power = true;
12157 				ath12k_mac_get_psd_channel(ar, 20,
12158 							   &start_freq,
12159 							   &center_freq,
12160 							   pwr_lvl_idx,
12161 							   &temp_chan,
12162 							   &tx_power);
12163 				psd_power = temp_chan->psd;
12164 				eirp_power = tx_power;
12165 				max_tx_power[pwr_lvl_idx] = psd_power;
12166 			} else {
12167 				ath12k_mac_get_eirp_power(ar,
12168 							  &start_freq,
12169 							  &center_freq,
12170 							  pwr_lvl_idx,
12171 							  &temp_chan,
12172 							  &ctx->def,
12173 							  &tx_power);
12174 				max_tx_power[pwr_lvl_idx] = tx_power;
12175 			}
12176 		}
12177 
12178 		if (is_psd_power) {
12179 			/* If AP local power constraint is present */
12180 			if (pwr_reduction)
12181 				eirp_power = eirp_power - pwr_reduction;
12182 
12183 			/* If firmware updated max tx power is non zero, then take
12184 			 * the min of firmware updated ap tx power
12185 			 * and max power derived from above mentioned parameters.
12186 			 */
12187 			ath12k_dbg(ab, ATH12K_DBG_MAC,
12188 				   "eirp power : %d firmware report power : %d\n",
12189 				   eirp_power, ar->max_allowed_tx_power);
12190 			/* Firmware reports lower max_allowed_tx_power during vdev
12191 			 * start response. In case of 6 GHz, firmware is not aware
12192 			 * of EIRP power unless driver sets EIRP power through WMI
12193 			 * TPC command. So radio which does not support idle power
12194 			 * save can set maximum calculated EIRP power directly to
12195 			 * firmware through TPC command without min comparison with
12196 			 * vdev start response's max_allowed_tx_power.
12197 			 */
12198 			if (ar->max_allowed_tx_power && ab->hw_params->idle_ps)
12199 				eirp_power = min_t(s8,
12200 						   eirp_power,
12201 						   ar->max_allowed_tx_power);
12202 		} else {
12203 			/* If AP local power constraint is present */
12204 			if (pwr_reduction)
12205 				max_tx_power[pwr_lvl_idx] =
12206 					max_tx_power[pwr_lvl_idx] - pwr_reduction;
12207 			/* If firmware updated max tx power is non zero, then take
12208 			 * the min of firmware updated ap tx power
12209 			 * and max power derived from above mentioned parameters.
12210 			 */
12211 			if (ar->max_allowed_tx_power && ab->hw_params->idle_ps)
12212 				max_tx_power[pwr_lvl_idx] =
12213 					min_t(s8,
12214 					      max_tx_power[pwr_lvl_idx],
12215 					      ar->max_allowed_tx_power);
12216 		}
12217 		reg_tpc_info->chan_power_info[pwr_lvl_idx].chan_cfreq = center_freq;
12218 		reg_tpc_info->chan_power_info[pwr_lvl_idx].tx_power =
12219 			max_tx_power[pwr_lvl_idx];
12220 	}
12221 
12222 	reg_tpc_info->num_pwr_levels = num_pwr_levels;
12223 	reg_tpc_info->is_psd_power = is_psd_power;
12224 	reg_tpc_info->eirp_power = eirp_power;
12225 	if (ahvif->vdev_type == WMI_VDEV_TYPE_STA)
12226 		reg_6ghz_power_mode = bss_conf->power_type;
12227 	else
12228 		/* For now, LPI is the only supported AP power mode */
12229 		reg_6ghz_power_mode = IEEE80211_REG_LPI_AP;
12230 
12231 	reg_tpc_info->ap_power_type =
12232 		ath12k_reg_ap_pwr_convert(reg_6ghz_power_mode);
12233 }
12234 
ath12k_mac_parse_tx_pwr_env(struct ath12k * ar,struct ath12k_link_vif * arvif)12235 static void ath12k_mac_parse_tx_pwr_env(struct ath12k *ar,
12236 					struct ath12k_link_vif *arvif)
12237 {
12238 	struct ieee80211_bss_conf *bss_conf = ath12k_mac_get_link_bss_conf(arvif);
12239 	struct ath12k_reg_tpc_power_info *tpc_info = &arvif->reg_tpc_info;
12240 	struct ieee80211_parsed_tpe_eirp *local_non_psd, *reg_non_psd;
12241 	struct ieee80211_parsed_tpe_psd *local_psd, *reg_psd;
12242 	struct ieee80211_parsed_tpe *tpe = &bss_conf->tpe;
12243 	enum wmi_reg_6g_client_type client_type;
12244 	struct ath12k_reg_info *reg_info;
12245 	struct ath12k_base *ab = ar->ab;
12246 	bool psd_valid, non_psd_valid;
12247 	int i;
12248 
12249 	reg_info = ab->reg_info[ar->pdev_idx];
12250 	client_type = reg_info->client_type;
12251 
12252 	local_psd = &tpe->psd_local[client_type];
12253 	reg_psd = &tpe->psd_reg_client[client_type];
12254 	local_non_psd = &tpe->max_local[client_type];
12255 	reg_non_psd = &tpe->max_reg_client[client_type];
12256 
12257 	psd_valid = local_psd->valid | reg_psd->valid;
12258 	non_psd_valid = local_non_psd->valid | reg_non_psd->valid;
12259 
12260 	if (!psd_valid && !non_psd_valid) {
12261 		ath12k_warn(ab,
12262 			    "no transmit power envelope match client power type %d\n",
12263 			    client_type);
12264 		return;
12265 	}
12266 
12267 	if (psd_valid) {
12268 		tpc_info->is_psd_power = true;
12269 
12270 		tpc_info->num_pwr_levels = max(local_psd->count,
12271 					       reg_psd->count);
12272 		tpc_info->num_pwr_levels =
12273 				min3(tpc_info->num_pwr_levels,
12274 				     IEEE80211_TPE_PSD_ENTRIES_320MHZ,
12275 				     ATH12K_NUM_PWR_LEVELS);
12276 
12277 		for (i = 0; i < tpc_info->num_pwr_levels; i++) {
12278 			tpc_info->tpe[i] = min(local_psd->power[i],
12279 					       reg_psd->power[i]) / 2;
12280 			ath12k_dbg(ab, ATH12K_DBG_MAC,
12281 				   "TPE PSD power[%d] : %d\n",
12282 				   i, tpc_info->tpe[i]);
12283 		}
12284 	} else {
12285 		tpc_info->is_psd_power = false;
12286 		tpc_info->eirp_power = 0;
12287 
12288 		tpc_info->num_pwr_levels = max(local_non_psd->count,
12289 					       reg_non_psd->count);
12290 		tpc_info->num_pwr_levels =
12291 				min3(tpc_info->num_pwr_levels,
12292 				     IEEE80211_TPE_EIRP_ENTRIES_320MHZ,
12293 				     ATH12K_NUM_PWR_LEVELS);
12294 
12295 		for (i = 0; i < tpc_info->num_pwr_levels; i++) {
12296 			tpc_info->tpe[i] = min(local_non_psd->power[i],
12297 					       reg_non_psd->power[i]) / 2;
12298 			ath12k_dbg(ab, ATH12K_DBG_MAC,
12299 				   "non PSD power[%d] : %d\n",
12300 				   i, tpc_info->tpe[i]);
12301 		}
12302 	}
12303 }
12304 
12305 int
ath12k_mac_op_assign_vif_chanctx(struct ieee80211_hw * hw,struct ieee80211_vif * vif,struct ieee80211_bss_conf * link_conf,struct ieee80211_chanctx_conf * ctx)12306 ath12k_mac_op_assign_vif_chanctx(struct ieee80211_hw *hw,
12307 				 struct ieee80211_vif *vif,
12308 				 struct ieee80211_bss_conf *link_conf,
12309 				 struct ieee80211_chanctx_conf *ctx)
12310 {
12311 	struct ath12k_hw *ah = ath12k_hw_to_ah(hw);
12312 	struct ath12k *ar;
12313 	struct ath12k_base *ab;
12314 	struct ath12k_vif *ahvif = ath12k_vif_to_ahvif(vif);
12315 	u8 link_id = link_conf->link_id;
12316 	struct ath12k_link_vif *arvif;
12317 	int ret;
12318 
12319 	lockdep_assert_wiphy(hw->wiphy);
12320 
12321 	/* For multi radio wiphy, the vdev was not created during add_interface
12322 	 * create now since we have a channel ctx now to assign to a specific ar/fw
12323 	 */
12324 	arvif = ath12k_mac_assign_link_vif(ah, vif, link_id);
12325 	if (!arvif) {
12326 		WARN_ON(1);
12327 		return -ENOMEM;
12328 	}
12329 
12330 	ar = ath12k_mac_assign_vif_to_vdev(hw, arvif, ctx);
12331 	if (!ar) {
12332 		ath12k_hw_warn(ah, "failed to assign chanctx for vif %pM link id %u link vif is already started",
12333 			       vif->addr, link_id);
12334 		return -EINVAL;
12335 	}
12336 
12337 	ab = ar->ab;
12338 
12339 	ath12k_dbg(ab, ATH12K_DBG_MAC,
12340 		   "mac chanctx assign ptr %p vdev_id %i\n",
12341 		   ctx, arvif->vdev_id);
12342 
12343 	if (ath12k_wmi_supports_6ghz_cc_ext(ar) &&
12344 	    ctx->def.chan->band == NL80211_BAND_6GHZ &&
12345 	    ahvif->vdev_type == WMI_VDEV_TYPE_STA)
12346 		ath12k_mac_parse_tx_pwr_env(ar, arvif);
12347 
12348 	arvif->punct_bitmap = ctx->def.punctured;
12349 
12350 	/* for some targets bss peer must be created before vdev_start */
12351 	if (ab->hw_params->vdev_start_delay &&
12352 	    ahvif->vdev_type != WMI_VDEV_TYPE_AP &&
12353 	    ahvif->vdev_type != WMI_VDEV_TYPE_MONITOR &&
12354 	    !ath12k_dp_link_peer_exist_by_vdev_id(ath12k_ab_to_dp(ab), arvif->vdev_id)) {
12355 		ret = 0;
12356 		goto out;
12357 	}
12358 
12359 	if (WARN_ON(arvif->is_started)) {
12360 		ret = -EBUSY;
12361 		goto out;
12362 	}
12363 
12364 	if (ahvif->vdev_type == WMI_VDEV_TYPE_MONITOR) {
12365 		ret = ath12k_mac_monitor_start(ar);
12366 		if (ret) {
12367 			ath12k_mac_monitor_vdev_delete(ar);
12368 			goto out;
12369 		}
12370 
12371 		arvif->is_started = true;
12372 		goto out;
12373 	}
12374 
12375 	ret = ath12k_mac_vdev_start(arvif, ctx);
12376 	if (ret) {
12377 		ath12k_warn(ab, "failed to start vdev %i addr %pM on freq %d: %d\n",
12378 			    arvif->vdev_id, vif->addr,
12379 			    ctx->def.chan->center_freq, ret);
12380 		goto out;
12381 	}
12382 
12383 	arvif->is_started = true;
12384 
12385 	/* TODO: Setup ps and cts/rts protection */
12386 
12387 out:
12388 	return ret;
12389 }
12390 EXPORT_SYMBOL(ath12k_mac_op_assign_vif_chanctx);
12391 
12392 void
ath12k_mac_op_unassign_vif_chanctx(struct ieee80211_hw * hw,struct ieee80211_vif * vif,struct ieee80211_bss_conf * link_conf,struct ieee80211_chanctx_conf * ctx)12393 ath12k_mac_op_unassign_vif_chanctx(struct ieee80211_hw *hw,
12394 				   struct ieee80211_vif *vif,
12395 				   struct ieee80211_bss_conf *link_conf,
12396 				   struct ieee80211_chanctx_conf *ctx)
12397 {
12398 	struct ath12k *ar;
12399 	struct ath12k_base *ab;
12400 	struct ath12k_vif *ahvif = ath12k_vif_to_ahvif(vif);
12401 	struct ath12k_link_vif *arvif;
12402 	u8 link_id = link_conf->link_id;
12403 	int ret;
12404 
12405 	lockdep_assert_wiphy(hw->wiphy);
12406 
12407 	arvif = wiphy_dereference(hw->wiphy, ahvif->link[link_id]);
12408 
12409 	/* The vif is expected to be attached to an ar's VDEV.
12410 	 * We leave the vif/vdev in this function as is
12411 	 * and not delete the vdev symmetric to assign_vif_chanctx()
12412 	 * the VDEV will be deleted and unassigned either during
12413 	 * remove_interface() or when there is a change in channel
12414 	 * that moves the vif to a new ar
12415 	 */
12416 	if (!arvif || !arvif->is_created)
12417 		return;
12418 
12419 	ar = arvif->ar;
12420 	ab = ar->ab;
12421 
12422 	ath12k_dbg(ab, ATH12K_DBG_MAC,
12423 		   "mac chanctx unassign ptr %p vdev_id %i\n",
12424 		   ctx, arvif->vdev_id);
12425 
12426 	WARN_ON(!arvif->is_started);
12427 
12428 	if (ahvif->vdev_type == WMI_VDEV_TYPE_MONITOR) {
12429 		ret = ath12k_mac_monitor_stop(ar);
12430 		if (ret)
12431 			return;
12432 
12433 		arvif->is_started = false;
12434 	}
12435 
12436 	if (ahvif->vdev_type != WMI_VDEV_TYPE_STA &&
12437 	    ahvif->vdev_type != WMI_VDEV_TYPE_MONITOR) {
12438 		ath12k_bss_disassoc(ar, arvif);
12439 		ret = ath12k_mac_vdev_stop(arvif);
12440 		if (ret)
12441 			ath12k_warn(ab, "failed to stop vdev %i: %d\n",
12442 				    arvif->vdev_id, ret);
12443 	}
12444 	arvif->is_started = false;
12445 
12446 	if (test_bit(WMI_TLV_SERVICE_11D_OFFLOAD, ab->wmi_ab.svc_map) &&
12447 	    ahvif->vdev_type == WMI_VDEV_TYPE_STA &&
12448 	    ahvif->vdev_subtype == WMI_VDEV_SUBTYPE_NONE &&
12449 	    ar->state_11d != ATH12K_11D_PREPARING) {
12450 		reinit_completion(&ar->completed_11d_scan);
12451 		ar->state_11d = ATH12K_11D_PREPARING;
12452 	}
12453 
12454 	if (ar->scan.arvif == arvif && ar->scan.state == ATH12K_SCAN_RUNNING) {
12455 		ath12k_scan_abort(ar);
12456 		ar->scan.arvif = NULL;
12457 	}
12458 }
12459 EXPORT_SYMBOL(ath12k_mac_op_unassign_vif_chanctx);
12460 
12461 int
ath12k_mac_op_switch_vif_chanctx(struct ieee80211_hw * hw,struct ieee80211_vif_chanctx_switch * vifs,int n_vifs,enum ieee80211_chanctx_switch_mode mode)12462 ath12k_mac_op_switch_vif_chanctx(struct ieee80211_hw *hw,
12463 				 struct ieee80211_vif_chanctx_switch *vifs,
12464 				 int n_vifs,
12465 				 enum ieee80211_chanctx_switch_mode mode)
12466 {
12467 	struct ath12k *curr_ar, *new_ar, *group_ar;
12468 	struct ieee80211_vif_chanctx_switch *v;
12469 	int i, j, count = 0;
12470 
12471 	lockdep_assert_wiphy(hw->wiphy);
12472 
12473 	if (n_vifs == 0)
12474 		return 0;
12475 
12476 	struct ath12k **ar_map __free(kfree) = kzalloc_objs(*ar_map, n_vifs);
12477 
12478 	if (!ar_map)
12479 		return -ENOMEM;
12480 
12481 	for (i = 0; i < n_vifs; i++) {
12482 		v = &vifs[i];
12483 
12484 		if (v->old_ctx->def.chan->band != v->new_ctx->def.chan->band) {
12485 			ath12k_generic_dbg(ATH12K_DBG_MAC,
12486 					   "mac chanctx switch band change not supported\n");
12487 			return -EOPNOTSUPP;
12488 		}
12489 
12490 		curr_ar = ath12k_get_ar_by_ctx(hw, v->old_ctx);
12491 		new_ar = ath12k_get_ar_by_ctx(hw, v->new_ctx);
12492 
12493 		if (!curr_ar || !new_ar) {
12494 			ath12k_generic_dbg(ATH12K_DBG_MAC,
12495 					   "unable to determine device for the passed channel ctx\n");
12496 			ath12k_generic_dbg(ATH12K_DBG_MAC,
12497 					   "Old freq %d MHz (device %s) to new freq %d MHz (device %s)\n",
12498 					   v->old_ctx->def.chan->center_freq,
12499 					   curr_ar ? "valid" : "invalid",
12500 					   v->new_ctx->def.chan->center_freq,
12501 					   new_ar ? "valid" : "invalid");
12502 			return -EINVAL;
12503 		}
12504 
12505 		/* Switching a vif between two radios is not allowed */
12506 		if (curr_ar != new_ar) {
12507 			ath12k_dbg(curr_ar->ab, ATH12K_DBG_MAC,
12508 				   "mac chanctx switch to another radio not supported\n");
12509 			return -EOPNOTSUPP;
12510 		}
12511 
12512 		ar_map[i] = curr_ar;
12513 	}
12514 
12515 	/* Group vifs by radio (ar) and process each group independently. */
12516 	bool *processed __free(kfree) = kzalloc_objs(*processed, n_vifs);
12517 
12518 	if (!processed)
12519 		return -ENOMEM;
12520 
12521 	struct ieee80211_vif_chanctx_switch *group_vifs __free(kfree) =
12522 						kzalloc_objs(*group_vifs, n_vifs);
12523 
12524 	if (!group_vifs)
12525 		return -ENOMEM;
12526 
12527 	for (i = 0; i < n_vifs; i++) {
12528 		if (processed[i])
12529 			continue;
12530 
12531 		group_ar = ar_map[i];
12532 
12533 		count = 0;
12534 		for (j = 0; j < n_vifs; j++) {
12535 			if (!processed[j] && ar_map[j] == group_ar) {
12536 				group_vifs[count++] = vifs[j];
12537 				processed[j] = true;
12538 			}
12539 		}
12540 
12541 		ath12k_dbg(group_ar->ab, ATH12K_DBG_MAC,
12542 			   "mac chanctx switch n_vifs %d mode %d\n",
12543 			   count, mode);
12544 		ath12k_mac_update_vif_chan(group_ar, group_vifs, count);
12545 	}
12546 	return 0;
12547 }
12548 EXPORT_SYMBOL(ath12k_mac_op_switch_vif_chanctx);
12549 
12550 static int
ath12k_set_vdev_param_to_all_vifs(struct ath12k * ar,int param,u32 value)12551 ath12k_set_vdev_param_to_all_vifs(struct ath12k *ar, int param, u32 value)
12552 {
12553 	struct ath12k_link_vif *arvif;
12554 	int ret = 0;
12555 
12556 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
12557 
12558 	list_for_each_entry(arvif, &ar->arvifs, list) {
12559 		ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "setting mac vdev %d param %d value %d\n",
12560 			   param, arvif->vdev_id, value);
12561 
12562 		ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
12563 						    param, value);
12564 		if (ret) {
12565 			ath12k_warn(ar->ab, "failed to set param %d for vdev %d: %d\n",
12566 				    param, arvif->vdev_id, ret);
12567 			break;
12568 		}
12569 	}
12570 
12571 	return ret;
12572 }
12573 
12574 /* mac80211 stores device specific RTS/Fragmentation threshold value,
12575  * this is set interface specific to firmware from ath12k driver
12576  */
ath12k_mac_op_set_rts_threshold(struct ieee80211_hw * hw,int radio_idx,u32 value)12577 int ath12k_mac_op_set_rts_threshold(struct ieee80211_hw *hw,
12578 				    int radio_idx, u32 value)
12579 {
12580 	struct ath12k_hw *ah = ath12k_hw_to_ah(hw);
12581 	struct wiphy *wiphy = hw->wiphy;
12582 	struct ath12k *ar;
12583 	int param_id = WMI_VDEV_PARAM_RTS_THRESHOLD;
12584 	int ret = 0, ret_err, i;
12585 
12586 	lockdep_assert_wiphy(hw->wiphy);
12587 
12588 	if (radio_idx >= wiphy->n_radio || radio_idx < -1)
12589 		return -EINVAL;
12590 
12591 	if (radio_idx != -1) {
12592 		/* Update RTS threshold in specified radio */
12593 		ar = ath12k_ah_to_ar(ah, radio_idx);
12594 		ret = ath12k_set_vdev_param_to_all_vifs(ar, param_id, value);
12595 		if (ret) {
12596 			ath12k_warn(ar->ab,
12597 				    "failed to set RTS config for all vdevs of pdev %d",
12598 				    ar->pdev->pdev_id);
12599 			return ret;
12600 		}
12601 
12602 		ar->rts_threshold = value;
12603 		return 0;
12604 	}
12605 
12606 	/* Radio_index passed is -1, so set RTS threshold for all radios. */
12607 	for_each_ar(ah, ar, i) {
12608 		ret = ath12k_set_vdev_param_to_all_vifs(ar, param_id, value);
12609 		if (ret) {
12610 			ath12k_warn(ar->ab, "failed to set RTS config for all vdevs of pdev %d",
12611 				    ar->pdev->pdev_id);
12612 			break;
12613 		}
12614 	}
12615 	if (!ret) {
12616 		/* Setting new RTS threshold for vdevs of all radios passed, so update
12617 		 * the RTS threshold value for all radios
12618 		 */
12619 		for_each_ar(ah, ar, i)
12620 			ar->rts_threshold = value;
12621 		return 0;
12622 	}
12623 
12624 	/* RTS threshold config failed, revert to the previous RTS threshold */
12625 	for (i = i - 1; i >= 0; i--) {
12626 		ar = ath12k_ah_to_ar(ah, i);
12627 		ret_err = ath12k_set_vdev_param_to_all_vifs(ar, param_id,
12628 							    ar->rts_threshold);
12629 		if (ret_err)
12630 			ath12k_warn(ar->ab,
12631 				    "failed to restore RTS threshold for all vdevs of pdev %d",
12632 				    ar->pdev->pdev_id);
12633 	}
12634 
12635 	return ret;
12636 }
12637 EXPORT_SYMBOL(ath12k_mac_op_set_rts_threshold);
12638 
ath12k_mac_op_set_frag_threshold(struct ieee80211_hw * hw,int radio_idx,u32 value)12639 int ath12k_mac_op_set_frag_threshold(struct ieee80211_hw *hw,
12640 				     int radio_idx, u32 value)
12641 {
12642 	/* Even though there's a WMI vdev param for fragmentation threshold no
12643 	 * known firmware actually implements it. Moreover it is not possible to
12644 	 * rely frame fragmentation to mac80211 because firmware clears the
12645 	 * "more fragments" bit in frame control making it impossible for remote
12646 	 * devices to reassemble frames.
12647 	 *
12648 	 * Hence implement a dummy callback just to say fragmentation isn't
12649 	 * supported. This effectively prevents mac80211 from doing frame
12650 	 * fragmentation in software.
12651 	 */
12652 
12653 	lockdep_assert_wiphy(hw->wiphy);
12654 
12655 	return -EOPNOTSUPP;
12656 }
12657 EXPORT_SYMBOL(ath12k_mac_op_set_frag_threshold);
12658 
ath12k_mac_flush(struct ath12k * ar)12659 static int ath12k_mac_flush(struct ath12k *ar)
12660 {
12661 	long time_left;
12662 	int ret = 0;
12663 
12664 	time_left = wait_event_timeout(ar->dp.tx_empty_waitq,
12665 				       (atomic_read(&ar->dp.num_tx_pending) == 0),
12666 				       ATH12K_FLUSH_TIMEOUT);
12667 	if (time_left == 0) {
12668 		ath12k_warn(ar->ab,
12669 			    "failed to flush transmit queue, data pkts pending %d\n",
12670 			    atomic_read(&ar->dp.num_tx_pending));
12671 		ret = -ETIMEDOUT;
12672 	}
12673 
12674 	time_left = wait_event_timeout(ar->txmgmt_empty_waitq,
12675 				       (atomic_read(&ar->num_pending_mgmt_tx) == 0),
12676 				       ATH12K_FLUSH_TIMEOUT);
12677 	if (time_left == 0) {
12678 		ath12k_warn(ar->ab,
12679 			    "failed to flush mgmt transmit queue, mgmt pkts pending %d\n",
12680 			    atomic_read(&ar->num_pending_mgmt_tx));
12681 		ret = -ETIMEDOUT;
12682 	}
12683 
12684 	return ret;
12685 }
12686 
ath12k_mac_wait_tx_complete(struct ath12k * ar)12687 int ath12k_mac_wait_tx_complete(struct ath12k *ar)
12688 {
12689 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
12690 
12691 	ath12k_mac_drain_tx(ar);
12692 	return ath12k_mac_flush(ar);
12693 }
12694 
ath12k_mac_op_flush(struct ieee80211_hw * hw,struct ieee80211_vif * vif,u32 queues,bool drop)12695 void ath12k_mac_op_flush(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
12696 			 u32 queues, bool drop)
12697 {
12698 	struct ath12k_hw *ah = ath12k_hw_to_ah(hw);
12699 	struct ath12k_link_vif *arvif;
12700 	struct ath12k_vif *ahvif;
12701 	unsigned long links;
12702 	struct ath12k *ar;
12703 	u8 link_id;
12704 	int i;
12705 
12706 	lockdep_assert_wiphy(hw->wiphy);
12707 
12708 	if (drop)
12709 		return;
12710 
12711 	for_each_ar(ah, ar, i)
12712 		wiphy_work_flush(hw->wiphy, &ar->wmi_mgmt_tx_work);
12713 
12714 	/* vif can be NULL when flush() is considered for hw */
12715 	if (!vif) {
12716 		for_each_ar(ah, ar, i)
12717 			ath12k_mac_flush(ar);
12718 		return;
12719 	}
12720 
12721 	ahvif = ath12k_vif_to_ahvif(vif);
12722 	links = ahvif->links_map;
12723 	for_each_set_bit(link_id, &links, IEEE80211_MLD_MAX_NUM_LINKS) {
12724 		arvif = wiphy_dereference(hw->wiphy, ahvif->link[link_id]);
12725 		if (!(arvif && arvif->ar))
12726 			continue;
12727 
12728 		ath12k_mac_flush(arvif->ar);
12729 	}
12730 }
12731 EXPORT_SYMBOL(ath12k_mac_op_flush);
12732 
12733 static int
ath12k_mac_bitrate_mask_num_ht_rates(struct ath12k * ar,enum nl80211_band band,const struct cfg80211_bitrate_mask * mask)12734 ath12k_mac_bitrate_mask_num_ht_rates(struct ath12k *ar,
12735 				     enum nl80211_band band,
12736 				     const struct cfg80211_bitrate_mask *mask)
12737 {
12738 	int num_rates = 0;
12739 	int i;
12740 
12741 	for (i = 0; i < ARRAY_SIZE(mask->control[band].ht_mcs); i++)
12742 		num_rates += hweight16(mask->control[band].ht_mcs[i]);
12743 
12744 	return num_rates;
12745 }
12746 
12747 static bool
ath12k_mac_has_single_legacy_rate(struct ath12k * ar,enum nl80211_band band,const struct cfg80211_bitrate_mask * mask)12748 ath12k_mac_has_single_legacy_rate(struct ath12k *ar,
12749 				  enum nl80211_band band,
12750 				  const struct cfg80211_bitrate_mask *mask)
12751 {
12752 	int num_rates = 0;
12753 
12754 	num_rates = hweight32(mask->control[band].legacy);
12755 
12756 	if (ath12k_mac_bitrate_mask_num_ht_rates(ar, band, mask))
12757 		return false;
12758 
12759 	if (ath12k_mac_bitrate_mask_num_vht_rates(ar, band, mask))
12760 		return false;
12761 
12762 	if (ath12k_mac_bitrate_mask_num_he_rates(ar, band, mask))
12763 		return false;
12764 
12765 	if (ath12k_mac_bitrate_mask_num_eht_rates(ar, band, mask))
12766 		return false;
12767 
12768 	return num_rates == 1;
12769 }
12770 
12771 static __le16
ath12k_mac_get_tx_mcs_map(const struct ieee80211_sta_he_cap * he_cap)12772 ath12k_mac_get_tx_mcs_map(const struct ieee80211_sta_he_cap *he_cap)
12773 {
12774 	if (he_cap->he_cap_elem.phy_cap_info[0] &
12775 	    IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_160MHZ_IN_5G)
12776 		return he_cap->he_mcs_nss_supp.tx_mcs_160;
12777 
12778 	return he_cap->he_mcs_nss_supp.tx_mcs_80;
12779 }
12780 
12781 static bool
ath12k_mac_bitrate_mask_get_single_nss(struct ath12k * ar,struct ieee80211_vif * vif,enum nl80211_band band,const struct cfg80211_bitrate_mask * mask,int * nss)12782 ath12k_mac_bitrate_mask_get_single_nss(struct ath12k *ar,
12783 				       struct ieee80211_vif *vif,
12784 				       enum nl80211_band band,
12785 				       const struct cfg80211_bitrate_mask *mask,
12786 				       int *nss)
12787 {
12788 	struct ieee80211_supported_band *sband = &ar->mac.sbands[band];
12789 	u16 vht_mcs_map = le16_to_cpu(sband->vht_cap.vht_mcs.tx_mcs_map);
12790 	const struct ieee80211_sband_iftype_data *data;
12791 	const struct ieee80211_sta_he_cap *he_cap;
12792 	u16 he_mcs_map = 0;
12793 	u16 eht_mcs_map = 0;
12794 	u8 ht_nss_mask = 0;
12795 	u8 vht_nss_mask = 0;
12796 	u8 he_nss_mask = 0;
12797 	u8 eht_nss_mask = 0;
12798 	u8 mcs_nss_len;
12799 	int i;
12800 
12801 	/* No need to consider legacy here. Basic rates are always present
12802 	 * in bitrate mask
12803 	 */
12804 
12805 	for (i = 0; i < ARRAY_SIZE(mask->control[band].ht_mcs); i++) {
12806 		if (mask->control[band].ht_mcs[i] == 0)
12807 			continue;
12808 		else if (mask->control[band].ht_mcs[i] ==
12809 			 sband->ht_cap.mcs.rx_mask[i])
12810 			ht_nss_mask |= BIT(i);
12811 		else
12812 			return false;
12813 	}
12814 
12815 	for (i = 0; i < ARRAY_SIZE(mask->control[band].vht_mcs); i++) {
12816 		if (mask->control[band].vht_mcs[i] == 0)
12817 			continue;
12818 		else if (mask->control[band].vht_mcs[i] ==
12819 			 ath12k_mac_get_max_vht_mcs_map(vht_mcs_map, i))
12820 			vht_nss_mask |= BIT(i);
12821 		else
12822 			return false;
12823 	}
12824 
12825 	he_cap = ieee80211_get_he_iftype_cap_vif(sband, vif);
12826 	if (!he_cap)
12827 		return false;
12828 
12829 	he_mcs_map = le16_to_cpu(ath12k_mac_get_tx_mcs_map(he_cap));
12830 
12831 	for (i = 0; i < ARRAY_SIZE(mask->control[band].he_mcs); i++) {
12832 		if (mask->control[band].he_mcs[i] == 0)
12833 			continue;
12834 
12835 		if (mask->control[band].he_mcs[i] ==
12836 		    ath12k_mac_get_max_he_mcs_map(he_mcs_map, i))
12837 			he_nss_mask |= BIT(i);
12838 		else
12839 			return false;
12840 	}
12841 
12842 	data = ieee80211_get_sband_iftype_data(sband, vif->type);
12843 
12844 	mcs_nss_len = ieee80211_eht_mcs_nss_size(&data->he_cap.he_cap_elem,
12845 						 &data->eht_cap.eht_cap_elem,
12846 						 false);
12847 	if (mcs_nss_len == 4) {
12848 		/* 20 MHz only STA case */
12849 		const struct ieee80211_eht_mcs_nss_supp_20mhz_only *eht_mcs_nss =
12850 			&data->eht_cap.eht_mcs_nss_supp.only_20mhz;
12851 		if (eht_mcs_nss->rx_tx_mcs13_max_nss)
12852 			eht_mcs_map = 0x1fff;
12853 		else if (eht_mcs_nss->rx_tx_mcs11_max_nss)
12854 			eht_mcs_map = 0x07ff;
12855 		else if (eht_mcs_nss->rx_tx_mcs9_max_nss)
12856 			eht_mcs_map = 0x01ff;
12857 		else
12858 			eht_mcs_map = 0x007f;
12859 	} else {
12860 		const struct ieee80211_eht_mcs_nss_supp_bw *eht_mcs_nss;
12861 
12862 		switch (mcs_nss_len) {
12863 		case 9:
12864 			eht_mcs_nss = &data->eht_cap.eht_mcs_nss_supp.bw._320;
12865 			break;
12866 		case 6:
12867 			eht_mcs_nss = &data->eht_cap.eht_mcs_nss_supp.bw._160;
12868 			break;
12869 		case 3:
12870 			eht_mcs_nss = &data->eht_cap.eht_mcs_nss_supp.bw._80;
12871 			break;
12872 		default:
12873 			return false;
12874 		}
12875 
12876 		if (eht_mcs_nss->rx_tx_mcs13_max_nss)
12877 			eht_mcs_map = 0x1fff;
12878 		else if (eht_mcs_nss->rx_tx_mcs11_max_nss)
12879 			eht_mcs_map = 0x7ff;
12880 		else
12881 			eht_mcs_map = 0x1ff;
12882 	}
12883 
12884 	for (i = 0; i < ARRAY_SIZE(mask->control[band].eht_mcs); i++) {
12885 		if (mask->control[band].eht_mcs[i] == 0)
12886 			continue;
12887 
12888 		if (mask->control[band].eht_mcs[i] < eht_mcs_map)
12889 			eht_nss_mask |= BIT(i);
12890 		else
12891 			return false;
12892 	}
12893 
12894 	if (ht_nss_mask != vht_nss_mask || ht_nss_mask != he_nss_mask ||
12895 	    ht_nss_mask != eht_nss_mask)
12896 		return false;
12897 
12898 	if (ht_nss_mask == 0)
12899 		return false;
12900 
12901 	if (BIT(fls(ht_nss_mask)) - 1 != ht_nss_mask)
12902 		return false;
12903 
12904 	*nss = fls(ht_nss_mask);
12905 
12906 	return true;
12907 }
12908 
12909 static int
ath12k_mac_get_single_legacy_rate(struct ath12k * ar,enum nl80211_band band,const struct cfg80211_bitrate_mask * mask,u32 * rate,u8 * nss)12910 ath12k_mac_get_single_legacy_rate(struct ath12k *ar,
12911 				  enum nl80211_band band,
12912 				  const struct cfg80211_bitrate_mask *mask,
12913 				  u32 *rate, u8 *nss)
12914 {
12915 	int rate_idx;
12916 	u16 bitrate;
12917 	u8 preamble;
12918 	u8 hw_rate;
12919 
12920 	if (hweight32(mask->control[band].legacy) != 1)
12921 		return -EINVAL;
12922 
12923 	rate_idx = ffs(mask->control[band].legacy) - 1;
12924 
12925 	if (band == NL80211_BAND_5GHZ || band == NL80211_BAND_6GHZ)
12926 		rate_idx += ATH12K_MAC_FIRST_OFDM_RATE_IDX;
12927 
12928 	hw_rate = ath12k_legacy_rates[rate_idx].hw_value;
12929 	bitrate = ath12k_legacy_rates[rate_idx].bitrate;
12930 
12931 	if (ath12k_mac_bitrate_is_cck(bitrate))
12932 		preamble = WMI_RATE_PREAMBLE_CCK;
12933 	else
12934 		preamble = WMI_RATE_PREAMBLE_OFDM;
12935 
12936 	*nss = 1;
12937 	*rate = ATH12K_HW_RATE_CODE(hw_rate, 0, preamble);
12938 
12939 	return 0;
12940 }
12941 
12942 static int
ath12k_mac_set_fixed_rate_gi_ltf(struct ath12k_link_vif * arvif,u8 gi,u8 ltf,u32 param)12943 ath12k_mac_set_fixed_rate_gi_ltf(struct ath12k_link_vif *arvif, u8 gi, u8 ltf,
12944 				 u32 param)
12945 {
12946 	struct ath12k *ar = arvif->ar;
12947 	int ret;
12948 
12949 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
12950 
12951 	/* 0.8 = 0, 1.6 = 2 and 3.2 = 3. */
12952 	if (gi && gi != 0xFF)
12953 		gi += 1;
12954 
12955 	ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
12956 					    WMI_VDEV_PARAM_SGI, gi);
12957 	if (ret) {
12958 		ath12k_warn(ar->ab, "failed to set GI:%d, error:%d\n",
12959 			    gi, ret);
12960 		return ret;
12961 	}
12962 
12963 	if (param == WMI_VDEV_PARAM_HE_LTF) {
12964 		/* HE values start from 1 */
12965 		if (ltf != 0xFF)
12966 			ltf += 1;
12967 	} else {
12968 		/* EHT values start from 5 */
12969 		if (ltf != 0xFF)
12970 			ltf += 4;
12971 	}
12972 
12973 	ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
12974 					    param, ltf);
12975 	if (ret) {
12976 		ath12k_warn(ar->ab, "failed to set LTF:%d, error:%d\n",
12977 			    ltf, ret);
12978 		return ret;
12979 	}
12980 	return 0;
12981 }
12982 
12983 static int
ath12k_mac_set_auto_rate_gi_ltf(struct ath12k_link_vif * arvif,u16 gi,u8 ltf)12984 ath12k_mac_set_auto_rate_gi_ltf(struct ath12k_link_vif *arvif, u16 gi, u8 ltf)
12985 {
12986 	struct ath12k *ar = arvif->ar;
12987 	int ret;
12988 	u32 ar_gi_ltf;
12989 
12990 	if (gi != 0xFF) {
12991 		switch (gi) {
12992 		case ATH12K_RATE_INFO_GI_0_8:
12993 			gi = WMI_AUTORATE_800NS_GI;
12994 			break;
12995 		case ATH12K_RATE_INFO_GI_1_6:
12996 			gi = WMI_AUTORATE_1600NS_GI;
12997 			break;
12998 		case ATH12K_RATE_INFO_GI_3_2:
12999 			gi = WMI_AUTORATE_3200NS_GI;
13000 			break;
13001 		default:
13002 			ath12k_warn(ar->ab, "Invalid GI\n");
13003 			return -EINVAL;
13004 		}
13005 	}
13006 
13007 	if (ltf != 0xFF) {
13008 		switch (ltf) {
13009 		case ATH12K_RATE_INFO_1XLTF:
13010 			ltf = WMI_AUTORATE_LTF_1X;
13011 			break;
13012 		case ATH12K_RATE_INFO_2XLTF:
13013 			ltf = WMI_AUTORATE_LTF_2X;
13014 			break;
13015 		case ATH12K_RATE_INFO_4XLTF:
13016 			ltf = WMI_AUTORATE_LTF_4X;
13017 			break;
13018 		default:
13019 			ath12k_warn(ar->ab, "Invalid LTF\n");
13020 			return -EINVAL;
13021 		}
13022 	}
13023 
13024 	ar_gi_ltf = gi | ltf;
13025 
13026 	ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
13027 					    WMI_VDEV_PARAM_AUTORATE_MISC_CFG,
13028 					    ar_gi_ltf);
13029 	if (ret) {
13030 		ath12k_warn(ar->ab,
13031 			    "failed to set autorate GI:%u, LTF:%u params, error:%d\n",
13032 			    gi, ltf, ret);
13033 		return ret;
13034 	}
13035 
13036 	return 0;
13037 }
13038 
ath12k_mac_nlgi_to_wmigi(enum nl80211_txrate_gi gi)13039 static u32 ath12k_mac_nlgi_to_wmigi(enum nl80211_txrate_gi gi)
13040 {
13041 	switch (gi) {
13042 	case NL80211_TXRATE_DEFAULT_GI:
13043 		return WMI_GI_400_NS;
13044 	case NL80211_TXRATE_FORCE_LGI:
13045 		return WMI_GI_800_NS;
13046 	default:
13047 		return WMI_GI_400_NS;
13048 	}
13049 }
13050 
ath12k_mac_set_rate_params(struct ath12k_link_vif * arvif,u32 rate,u8 nss,u8 sgi,u8 ldpc,u8 he_gi,u8 he_ltf,bool he_fixed_rate,u8 eht_gi,u8 eht_ltf,bool eht_fixed_rate)13051 static int ath12k_mac_set_rate_params(struct ath12k_link_vif *arvif,
13052 				      u32 rate, u8 nss, u8 sgi, u8 ldpc,
13053 				      u8 he_gi, u8 he_ltf, bool he_fixed_rate,
13054 				      u8 eht_gi, u8 eht_ltf,
13055 				      bool eht_fixed_rate)
13056 {
13057 	struct ieee80211_bss_conf *link_conf;
13058 	struct ath12k *ar = arvif->ar;
13059 	bool he_support, eht_support, gi_ltf_set = false;
13060 	u32 vdev_param;
13061 	u32 param_value;
13062 	int ret;
13063 
13064 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
13065 
13066 	link_conf = ath12k_mac_get_link_bss_conf(arvif);
13067 	if (!link_conf)
13068 		return -EINVAL;
13069 
13070 	he_support = link_conf->he_support;
13071 	eht_support = link_conf->eht_support;
13072 
13073 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
13074 		   "mac set rate params vdev %i rate 0x%02x nss 0x%02x sgi 0x%02x ldpc 0x%02x\n",
13075 		   arvif->vdev_id, rate, nss, sgi, ldpc);
13076 
13077 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
13078 		   "he_gi 0x%02x he_ltf 0x%02x he_fixed_rate %d\n", he_gi,
13079 		   he_ltf, he_fixed_rate);
13080 
13081 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
13082 		   "eht_gi 0x%02x eht_ltf 0x%02x eht_fixed_rate %d\n",
13083 		   eht_gi, eht_ltf, eht_fixed_rate);
13084 
13085 	if (!he_support && !eht_support) {
13086 		vdev_param = WMI_VDEV_PARAM_FIXED_RATE;
13087 		ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
13088 						    vdev_param, rate);
13089 		if (ret) {
13090 			ath12k_warn(ar->ab, "failed to set fixed rate param 0x%02x: %d\n",
13091 				    rate, ret);
13092 			return ret;
13093 		}
13094 	}
13095 
13096 	vdev_param = WMI_VDEV_PARAM_NSS;
13097 
13098 	ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
13099 					    vdev_param, nss);
13100 	if (ret) {
13101 		ath12k_warn(ar->ab, "failed to set nss param %d: %d\n",
13102 			    nss, ret);
13103 		return ret;
13104 	}
13105 
13106 	ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
13107 					    WMI_VDEV_PARAM_LDPC, ldpc);
13108 	if (ret) {
13109 		ath12k_warn(ar->ab, "failed to set ldpc param %d: %d\n",
13110 			    ldpc, ret);
13111 		return ret;
13112 	}
13113 
13114 	if (eht_support) {
13115 		if (eht_fixed_rate)
13116 			ret = ath12k_mac_set_fixed_rate_gi_ltf(arvif, eht_gi, eht_ltf,
13117 							       WMI_VDEV_PARAM_EHT_LTF);
13118 		else
13119 			ret = ath12k_mac_set_auto_rate_gi_ltf(arvif, eht_gi, eht_ltf);
13120 
13121 		if (ret) {
13122 			ath12k_warn(ar->ab,
13123 				    "failed to set EHT LTF/GI params %d/%d: %d\n",
13124 				    eht_gi, eht_ltf, ret);
13125 			return ret;
13126 		}
13127 		gi_ltf_set = true;
13128 	}
13129 
13130 	if (he_support) {
13131 		if (he_fixed_rate)
13132 			ret = ath12k_mac_set_fixed_rate_gi_ltf(arvif, he_gi, he_ltf,
13133 							       WMI_VDEV_PARAM_HE_LTF);
13134 		else
13135 			ret = ath12k_mac_set_auto_rate_gi_ltf(arvif, he_gi, he_ltf);
13136 		if (ret)
13137 			return ret;
13138 		gi_ltf_set = true;
13139 	}
13140 
13141 	if (!gi_ltf_set) {
13142 		vdev_param = WMI_VDEV_PARAM_SGI;
13143 		param_value = ath12k_mac_nlgi_to_wmigi(sgi);
13144 		ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
13145 						    vdev_param, param_value);
13146 		if (ret) {
13147 			ath12k_warn(ar->ab, "failed to set sgi param %d: %d\n",
13148 				    sgi, ret);
13149 			return ret;
13150 		}
13151 	}
13152 
13153 	return 0;
13154 }
13155 
13156 static bool
ath12k_mac_vht_mcs_range_present(struct ath12k * ar,enum nl80211_band band,const struct cfg80211_bitrate_mask * mask)13157 ath12k_mac_vht_mcs_range_present(struct ath12k *ar,
13158 				 enum nl80211_band band,
13159 				 const struct cfg80211_bitrate_mask *mask)
13160 {
13161 	int i;
13162 	u16 vht_mcs;
13163 
13164 	for (i = 0; i < NL80211_VHT_NSS_MAX; i++) {
13165 		vht_mcs = mask->control[band].vht_mcs[i];
13166 
13167 		switch (vht_mcs) {
13168 		case 0:
13169 		case BIT(8) - 1:
13170 		case BIT(9) - 1:
13171 		case BIT(10) - 1:
13172 			break;
13173 		default:
13174 			return false;
13175 		}
13176 	}
13177 
13178 	return true;
13179 }
13180 
13181 static bool
ath12k_mac_he_mcs_range_present(struct ath12k * ar,enum nl80211_band band,const struct cfg80211_bitrate_mask * mask)13182 ath12k_mac_he_mcs_range_present(struct ath12k *ar,
13183 				enum nl80211_band band,
13184 				const struct cfg80211_bitrate_mask *mask)
13185 {
13186 	int i;
13187 	u16 he_mcs;
13188 
13189 	for (i = 0; i < NL80211_HE_NSS_MAX; i++) {
13190 		he_mcs = mask->control[band].he_mcs[i];
13191 
13192 		switch (he_mcs) {
13193 		case 0:
13194 		case BIT(8) - 1:
13195 		case BIT(10) - 1:
13196 		case BIT(12) - 1:
13197 			break;
13198 		default:
13199 			return false;
13200 		}
13201 	}
13202 
13203 	return true;
13204 }
13205 
13206 static bool
ath12k_mac_eht_mcs_range_present(struct ath12k * ar,enum nl80211_band band,const struct cfg80211_bitrate_mask * mask)13207 ath12k_mac_eht_mcs_range_present(struct ath12k *ar,
13208 				 enum nl80211_band band,
13209 				 const struct cfg80211_bitrate_mask *mask)
13210 {
13211 	u16 eht_mcs;
13212 	int i;
13213 
13214 	for (i = 0; i < NL80211_EHT_NSS_MAX; i++) {
13215 		eht_mcs = mask->control[band].eht_mcs[i];
13216 
13217 		switch (eht_mcs) {
13218 		case 0:
13219 		case BIT(8) - 1:
13220 		case BIT(10) - 1:
13221 		case BIT(12) - 1:
13222 		case BIT(14) - 1:
13223 			break;
13224 		case BIT(15) - 1:
13225 		case BIT(16) - 1:
13226 		case BIT(16) - BIT(14) - 1:
13227 			if (i != 0)
13228 				return false;
13229 			break;
13230 		default:
13231 			return false;
13232 		}
13233 	}
13234 
13235 	return true;
13236 }
13237 
ath12k_mac_set_bitrate_mask_iter(void * data,struct ieee80211_sta * sta)13238 static void ath12k_mac_set_bitrate_mask_iter(void *data,
13239 					     struct ieee80211_sta *sta)
13240 {
13241 	struct ath12k_link_vif *arvif = data;
13242 	struct ath12k_sta *ahsta = ath12k_sta_to_ahsta(sta);
13243 	struct ath12k_link_sta *arsta;
13244 	struct ath12k *ar = arvif->ar;
13245 
13246 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
13247 
13248 	arsta = wiphy_dereference(ath12k_ar_to_hw(ar)->wiphy,
13249 				  ahsta->link[arvif->link_id]);
13250 	if (!arsta || arsta->arvif != arvif)
13251 		return;
13252 
13253 	spin_lock_bh(&ar->data_lock);
13254 	arsta->changed |= IEEE80211_RC_SUPP_RATES_CHANGED;
13255 	spin_unlock_bh(&ar->data_lock);
13256 
13257 	wiphy_work_queue(ath12k_ar_to_hw(ar)->wiphy, &arsta->update_wk);
13258 }
13259 
ath12k_mac_disable_peer_fixed_rate(void * data,struct ieee80211_sta * sta)13260 static void ath12k_mac_disable_peer_fixed_rate(void *data,
13261 					       struct ieee80211_sta *sta)
13262 {
13263 	struct ath12k_link_vif *arvif = data;
13264 	struct ath12k_sta *ahsta = ath12k_sta_to_ahsta(sta);
13265 	struct ath12k_link_sta *arsta;
13266 	struct ath12k *ar = arvif->ar;
13267 	int ret;
13268 
13269 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
13270 
13271 	arsta = wiphy_dereference(ath12k_ar_to_hw(ar)->wiphy,
13272 				  ahsta->link[arvif->link_id]);
13273 
13274 	if (!arsta || arsta->arvif != arvif)
13275 		return;
13276 
13277 	ret = ath12k_wmi_set_peer_param(ar, arsta->addr,
13278 					arvif->vdev_id,
13279 					WMI_PEER_PARAM_FIXED_RATE,
13280 					WMI_FIXED_RATE_NONE);
13281 	if (ret)
13282 		ath12k_warn(ar->ab,
13283 			    "failed to disable peer fixed rate for STA %pM ret %d\n",
13284 			    arsta->addr, ret);
13285 }
13286 
13287 static bool
ath12k_mac_validate_fixed_rate_settings(struct ath12k * ar,enum nl80211_band band,const struct cfg80211_bitrate_mask * mask,unsigned int link_id)13288 ath12k_mac_validate_fixed_rate_settings(struct ath12k *ar, enum nl80211_band band,
13289 					const struct cfg80211_bitrate_mask *mask,
13290 					unsigned int link_id)
13291 {
13292 	bool eht_fixed_rate = false, he_fixed_rate = false, vht_fixed_rate = false;
13293 	const u16 *vht_mcs_mask, *he_mcs_mask, *eht_mcs_mask;
13294 	struct ieee80211_link_sta *link_sta;
13295 	struct ath12k_dp_link_peer *peer, *tmp;
13296 	u8 vht_nss, he_nss, eht_nss;
13297 	int ret = true;
13298 	struct ath12k_base *ab = ar->ab;
13299 	struct ath12k_dp *dp = ath12k_ab_to_dp(ab);
13300 
13301 	vht_mcs_mask = mask->control[band].vht_mcs;
13302 	he_mcs_mask = mask->control[band].he_mcs;
13303 	eht_mcs_mask = mask->control[band].eht_mcs;
13304 
13305 	if (ath12k_mac_bitrate_mask_num_vht_rates(ar, band, mask) == 1)
13306 		vht_fixed_rate = true;
13307 
13308 	if (ath12k_mac_bitrate_mask_num_he_rates(ar, band, mask) == 1)
13309 		he_fixed_rate = true;
13310 
13311 	if (ath12k_mac_bitrate_mask_num_eht_rates(ar, band, mask) == 1)
13312 		eht_fixed_rate = true;
13313 
13314 	if (!vht_fixed_rate && !he_fixed_rate && !eht_fixed_rate)
13315 		return true;
13316 
13317 	vht_nss = ath12k_mac_max_vht_nss(vht_mcs_mask);
13318 	he_nss =  ath12k_mac_max_he_nss(he_mcs_mask);
13319 	eht_nss = ath12k_mac_max_eht_nss(eht_mcs_mask);
13320 
13321 	rcu_read_lock();
13322 	spin_lock_bh(&dp->dp_lock);
13323 	list_for_each_entry_safe(peer, tmp, &dp->peers, list) {
13324 		if (peer->sta) {
13325 			link_sta = rcu_dereference(peer->sta->link[link_id]);
13326 			if (!link_sta) {
13327 				ret = false;
13328 				goto exit;
13329 			}
13330 
13331 			if (vht_fixed_rate && (!link_sta->vht_cap.vht_supported ||
13332 					       link_sta->rx_nss < vht_nss)) {
13333 				ret = false;
13334 				goto exit;
13335 			}
13336 			if (he_fixed_rate && (!link_sta->he_cap.has_he ||
13337 					      link_sta->rx_nss < he_nss)) {
13338 				ret = false;
13339 				goto exit;
13340 			}
13341 			if (eht_fixed_rate && (!link_sta->eht_cap.has_eht ||
13342 					       link_sta->rx_nss < eht_nss)) {
13343 				ret = false;
13344 				goto exit;
13345 			}
13346 		}
13347 	}
13348 exit:
13349 	spin_unlock_bh(&dp->dp_lock);
13350 	rcu_read_unlock();
13351 	return ret;
13352 }
13353 
13354 int
ath12k_mac_op_set_bitrate_mask(struct ieee80211_hw * hw,struct ieee80211_vif * vif,const struct cfg80211_bitrate_mask * mask)13355 ath12k_mac_op_set_bitrate_mask(struct ieee80211_hw *hw,
13356 			       struct ieee80211_vif *vif,
13357 			       const struct cfg80211_bitrate_mask *mask)
13358 {
13359 	struct ath12k_vif *ahvif = ath12k_vif_to_ahvif(vif);
13360 	struct ath12k_link_vif *arvif;
13361 	struct cfg80211_chan_def def;
13362 	struct ath12k *ar;
13363 	enum nl80211_band band;
13364 	const u8 *ht_mcs_mask;
13365 	const u16 *vht_mcs_mask;
13366 	const u16 *he_mcs_mask;
13367 	const u16 *eht_mcs_mask;
13368 	u8 he_ltf = 0;
13369 	u8 he_gi = 0;
13370 	u8 eht_ltf = 0, eht_gi = 0;
13371 	u32 rate;
13372 	u8 nss, mac_nss;
13373 	u8 sgi;
13374 	u8 ldpc;
13375 	int single_nss;
13376 	int ret;
13377 	int num_rates;
13378 	bool he_fixed_rate = false;
13379 	bool eht_fixed_rate = false;
13380 
13381 	lockdep_assert_wiphy(hw->wiphy);
13382 
13383 	arvif = &ahvif->deflink;
13384 
13385 	ar = arvif->ar;
13386 	if (ath12k_mac_vif_link_chan(vif, arvif->link_id, &def)) {
13387 		ret = -EPERM;
13388 		goto out;
13389 	}
13390 
13391 	band = def.chan->band;
13392 	ht_mcs_mask = mask->control[band].ht_mcs;
13393 	vht_mcs_mask = mask->control[band].vht_mcs;
13394 	he_mcs_mask = mask->control[band].he_mcs;
13395 	eht_mcs_mask = mask->control[band].eht_mcs;
13396 	ldpc = !!(ar->ht_cap_info & WMI_HT_CAP_LDPC);
13397 
13398 	sgi = mask->control[band].gi;
13399 	if (sgi == NL80211_TXRATE_FORCE_SGI) {
13400 		ret = -EINVAL;
13401 		goto out;
13402 	}
13403 
13404 	he_gi = mask->control[band].he_gi;
13405 	he_ltf = mask->control[band].he_ltf;
13406 
13407 	eht_gi = mask->control[band].eht_gi;
13408 	eht_ltf = mask->control[band].eht_ltf;
13409 
13410 	/* mac80211 doesn't support sending a fixed HT/VHT MCS alone, rather it
13411 	 * requires passing at least one of used basic rates along with them.
13412 	 * Fixed rate setting across different preambles(legacy, HT, VHT) is
13413 	 * not supported by the FW. Hence use of FIXED_RATE vdev param is not
13414 	 * suitable for setting single HT/VHT rates.
13415 	 * But, there could be a single basic rate passed from userspace which
13416 	 * can be done through the FIXED_RATE param.
13417 	 */
13418 	if (ath12k_mac_has_single_legacy_rate(ar, band, mask)) {
13419 		ret = ath12k_mac_get_single_legacy_rate(ar, band, mask, &rate,
13420 							&nss);
13421 		if (ret) {
13422 			ath12k_warn(ar->ab, "failed to get single legacy rate for vdev %i: %d\n",
13423 				    arvif->vdev_id, ret);
13424 			goto out;
13425 		}
13426 
13427 		ieee80211_iterate_stations_mtx(hw,
13428 					       ath12k_mac_disable_peer_fixed_rate,
13429 					       arvif);
13430 	} else if (ath12k_mac_bitrate_mask_get_single_nss(ar, vif, band, mask,
13431 							  &single_nss)) {
13432 		rate = WMI_FIXED_RATE_NONE;
13433 		nss = single_nss;
13434 		arvif->bitrate_mask = *mask;
13435 
13436 		ieee80211_iterate_stations_atomic(hw,
13437 						  ath12k_mac_set_bitrate_mask_iter,
13438 						  arvif);
13439 	} else {
13440 		rate = WMI_FIXED_RATE_NONE;
13441 
13442 		if (!ath12k_mac_validate_fixed_rate_settings(ar, band,
13443 							     mask, arvif->link_id))
13444 			ath12k_warn(ar->ab,
13445 				    "failed to update fixed rate settings due to mcs/nss incompatibility\n");
13446 
13447 		mac_nss = max(max3(ath12k_mac_max_ht_nss(ht_mcs_mask),
13448 				   ath12k_mac_max_vht_nss(vht_mcs_mask),
13449 				   ath12k_mac_max_he_nss(he_mcs_mask)),
13450 			       ath12k_mac_max_eht_nss(eht_mcs_mask));
13451 		nss = min_t(u32, ar->num_tx_chains, mac_nss);
13452 
13453 		/* If multiple rates across different preambles are given
13454 		 * we can reconfigure this info with all peers using PEER_ASSOC
13455 		 * command with the below exception cases.
13456 		 * - Single VHT Rate : peer_assoc command accommodates only MCS
13457 		 * range values i.e 0-7, 0-8, 0-9 for VHT. Though mac80211
13458 		 * mandates passing basic rates along with HT/VHT rates, FW
13459 		 * doesn't allow switching from VHT to Legacy. Hence instead of
13460 		 * setting legacy and VHT rates using RATEMASK_CMD vdev cmd,
13461 		 * we could set this VHT rate as peer fixed rate param, which
13462 		 * will override FIXED rate and FW rate control algorithm.
13463 		 * If single VHT rate is passed along with HT rates, we select
13464 		 * the VHT rate as fixed rate for vht peers.
13465 		 * - Multiple VHT Rates : When Multiple VHT rates are given,this
13466 		 * can be set using RATEMASK CMD which uses FW rate-ctl alg.
13467 		 * TODO: Setting multiple VHT MCS and replacing peer_assoc with
13468 		 * RATEMASK_CMDID can cover all use cases of setting rates
13469 		 * across multiple preambles and rates within same type.
13470 		 * But requires more validation of the command at this point.
13471 		 */
13472 
13473 		num_rates = ath12k_mac_bitrate_mask_num_vht_rates(ar, band,
13474 								  mask);
13475 
13476 		if (!ath12k_mac_vht_mcs_range_present(ar, band, mask) &&
13477 		    num_rates > 1) {
13478 			/* TODO: Handle multiple VHT MCS values setting using
13479 			 * RATEMASK CMD
13480 			 */
13481 			ath12k_warn(ar->ab,
13482 				    "Setting more than one MCS Value in bitrate mask not supported\n");
13483 			ret = -EINVAL;
13484 			goto out;
13485 		}
13486 
13487 		num_rates = ath12k_mac_bitrate_mask_num_he_rates(ar, band, mask);
13488 		if (num_rates == 1)
13489 			he_fixed_rate = true;
13490 
13491 		if (!ath12k_mac_he_mcs_range_present(ar, band, mask) &&
13492 		    num_rates > 1) {
13493 			ath12k_warn(ar->ab,
13494 				    "Setting more than one HE MCS Value in bitrate mask not supported\n");
13495 			ret = -EINVAL;
13496 			goto out;
13497 		}
13498 
13499 		num_rates = ath12k_mac_bitrate_mask_num_eht_rates(ar, band,
13500 								  mask);
13501 		if (num_rates == 1)
13502 			eht_fixed_rate = true;
13503 
13504 		if (!ath12k_mac_eht_mcs_range_present(ar, band, mask) &&
13505 		    num_rates > 1) {
13506 			ath12k_warn(ar->ab,
13507 				    "Setting more than one EHT MCS Value in bitrate mask not supported\n");
13508 			ret = -EINVAL;
13509 			goto out;
13510 		}
13511 
13512 		ieee80211_iterate_stations_mtx(hw,
13513 					       ath12k_mac_disable_peer_fixed_rate,
13514 					       arvif);
13515 
13516 		arvif->bitrate_mask = *mask;
13517 		ieee80211_iterate_stations_mtx(hw,
13518 					       ath12k_mac_set_bitrate_mask_iter,
13519 					       arvif);
13520 	}
13521 
13522 	ret = ath12k_mac_set_rate_params(arvif, rate, nss, sgi, ldpc, he_gi,
13523 					 he_ltf, he_fixed_rate, eht_gi, eht_ltf,
13524 					 eht_fixed_rate);
13525 	if (ret) {
13526 		ath12k_warn(ar->ab, "failed to set rate params on vdev %i: %d\n",
13527 			    arvif->vdev_id, ret);
13528 	}
13529 
13530 out:
13531 	return ret;
13532 }
13533 EXPORT_SYMBOL(ath12k_mac_op_set_bitrate_mask);
13534 
13535 void
ath12k_mac_op_reconfig_complete(struct ieee80211_hw * hw,enum ieee80211_reconfig_type reconfig_type)13536 ath12k_mac_op_reconfig_complete(struct ieee80211_hw *hw,
13537 				enum ieee80211_reconfig_type reconfig_type)
13538 {
13539 	struct ath12k_hw *ah = ath12k_hw_to_ah(hw);
13540 	struct ath12k *ar;
13541 	struct ath12k_base *ab;
13542 	struct ath12k_vif *ahvif;
13543 	struct ath12k_link_vif *arvif;
13544 	int recovery_count, i;
13545 
13546 	lockdep_assert_wiphy(hw->wiphy);
13547 
13548 	if (reconfig_type != IEEE80211_RECONFIG_TYPE_RESTART)
13549 		return;
13550 
13551 	guard(mutex)(&ah->hw_mutex);
13552 
13553 	if (ah->state != ATH12K_HW_STATE_RESTARTED)
13554 		return;
13555 
13556 	ah->state = ATH12K_HW_STATE_ON;
13557 	ieee80211_wake_queues(hw);
13558 
13559 	for_each_ar(ah, ar, i) {
13560 		ab = ar->ab;
13561 
13562 		ath12k_warn(ar->ab, "pdev %d successfully recovered\n",
13563 			    ar->pdev->pdev_id);
13564 
13565 		if (ar->ab->hw_params->current_cc_support &&
13566 		    ar->alpha2[0] != 0 && ar->alpha2[1] != 0) {
13567 			struct wmi_set_current_country_arg arg = {};
13568 
13569 			memcpy(&arg.alpha2, ar->alpha2, 2);
13570 			reinit_completion(&ar->regd_update_completed);
13571 			ath12k_wmi_send_set_current_country_cmd(ar, &arg);
13572 		}
13573 
13574 		if (ab->is_reset) {
13575 			recovery_count = atomic_inc_return(&ab->recovery_count);
13576 
13577 			ath12k_dbg(ab, ATH12K_DBG_BOOT, "recovery count %d\n",
13578 				   recovery_count);
13579 
13580 			/* When there are multiple radios in an SOC,
13581 			 * the recovery has to be done for each radio
13582 			 */
13583 			if (recovery_count == ab->num_radios) {
13584 				atomic_dec(&ab->reset_count);
13585 				complete(&ab->reset_complete);
13586 				ab->is_reset = false;
13587 				atomic_set(&ab->fail_cont_count, 0);
13588 				ath12k_dbg(ab, ATH12K_DBG_BOOT, "reset success\n");
13589 			}
13590 		}
13591 
13592 		list_for_each_entry(arvif, &ar->arvifs, list) {
13593 			ahvif = arvif->ahvif;
13594 			ath12k_dbg(ab, ATH12K_DBG_BOOT,
13595 				   "reconfig cipher %d up %d vdev type %d\n",
13596 				   ahvif->dp_vif.key_cipher,
13597 				   arvif->is_up,
13598 				   ahvif->vdev_type);
13599 
13600 			/* After trigger disconnect, then upper layer will
13601 			 * trigger connect again, then the PN number of
13602 			 * upper layer will be reset to keep up with AP
13603 			 * side, hence PN number mismatch will not happen.
13604 			 */
13605 			if (arvif->is_up &&
13606 			    ahvif->vdev_type == WMI_VDEV_TYPE_STA &&
13607 			    ahvif->vdev_subtype == WMI_VDEV_SUBTYPE_NONE) {
13608 				ieee80211_hw_restart_disconnect(ahvif->vif);
13609 
13610 				ath12k_dbg(ab, ATH12K_DBG_BOOT,
13611 					   "restart disconnect\n");
13612 			}
13613 		}
13614 	}
13615 }
13616 EXPORT_SYMBOL(ath12k_mac_op_reconfig_complete);
13617 
13618 static void
ath12k_mac_update_bss_chan_survey(struct ath12k * ar,struct ieee80211_channel * channel)13619 ath12k_mac_update_bss_chan_survey(struct ath12k *ar,
13620 				  struct ieee80211_channel *channel)
13621 {
13622 	int ret;
13623 	enum wmi_bss_chan_info_req_type type = WMI_BSS_SURVEY_REQ_TYPE_READ;
13624 
13625 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
13626 
13627 	if (!test_bit(WMI_TLV_SERVICE_BSS_CHANNEL_INFO_64, ar->ab->wmi_ab.svc_map) ||
13628 	    ar->rx_channel != channel)
13629 		return;
13630 
13631 	if (ar->scan.state != ATH12K_SCAN_IDLE) {
13632 		ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
13633 			   "ignoring bss chan info req while scanning..\n");
13634 		return;
13635 	}
13636 
13637 	reinit_completion(&ar->bss_survey_done);
13638 
13639 	ret = ath12k_wmi_pdev_bss_chan_info_request(ar, type);
13640 	if (ret) {
13641 		ath12k_warn(ar->ab, "failed to send pdev bss chan info request\n");
13642 		return;
13643 	}
13644 
13645 	ret = wait_for_completion_timeout(&ar->bss_survey_done, 3 * HZ);
13646 	if (ret == 0)
13647 		ath12k_warn(ar->ab, "bss channel survey timed out\n");
13648 }
13649 
ath12k_mac_op_get_survey(struct ieee80211_hw * hw,int idx,struct survey_info * survey)13650 int ath12k_mac_op_get_survey(struct ieee80211_hw *hw, int idx,
13651 			     struct survey_info *survey)
13652 {
13653 	struct ath12k_hw *ah = hw->priv;
13654 	struct ath12k *ar;
13655 	struct ieee80211_supported_band *sband;
13656 	struct survey_info *ah_survey;
13657 	int sband_idx = idx;
13658 
13659 	lockdep_assert_wiphy(hw->wiphy);
13660 
13661 	if (sband_idx >= ATH12K_NUM_CHANS)
13662 		return -ENOENT;
13663 
13664 	sband = hw->wiphy->bands[NL80211_BAND_2GHZ];
13665 	if (sband && sband_idx >= sband->n_channels) {
13666 		sband_idx -= sband->n_channels;
13667 		sband = NULL;
13668 	}
13669 
13670 	if (!sband)
13671 		sband = hw->wiphy->bands[NL80211_BAND_5GHZ];
13672 	if (sband && sband_idx >= sband->n_channels) {
13673 		sband_idx -= sband->n_channels;
13674 		sband = NULL;
13675 	}
13676 
13677 	if (!sband)
13678 		sband = hw->wiphy->bands[NL80211_BAND_6GHZ];
13679 
13680 	if (!sband || sband_idx >= sband->n_channels)
13681 		return -ENOENT;
13682 
13683 	ar = ath12k_mac_get_ar_by_chan(hw, &sband->channels[sband_idx]);
13684 	if (!ar) {
13685 		if (sband->channels[sband_idx].flags & IEEE80211_CHAN_DISABLED) {
13686 			memset(survey, 0, sizeof(*survey));
13687 			return 0;
13688 		}
13689 		return -ENOENT;
13690 	}
13691 
13692 	ah_survey = &ah->survey[idx];
13693 
13694 	ath12k_mac_update_bss_chan_survey(ar, &sband->channels[sband_idx]);
13695 
13696 	scoped_guard(spinlock_bh, &ah->survey_lock) {
13697 		memcpy(survey, ah_survey, sizeof(*survey));
13698 	}
13699 
13700 	survey->channel = &sband->channels[sband_idx];
13701 
13702 	if (ar->rx_channel == survey->channel)
13703 		survey->filled |= SURVEY_INFO_IN_USE;
13704 
13705 	return 0;
13706 }
13707 EXPORT_SYMBOL(ath12k_mac_op_get_survey);
13708 
ath12k_mac_put_chain_rssi(struct station_info * sinfo,struct ath12k_link_sta * arsta)13709 static void ath12k_mac_put_chain_rssi(struct station_info *sinfo,
13710 				      struct ath12k_link_sta *arsta)
13711 {
13712 	s8 rssi;
13713 	int i;
13714 
13715 	for (i = 0; i < ARRAY_SIZE(sinfo->chain_signal); i++) {
13716 		sinfo->chains &= ~BIT(i);
13717 		rssi = arsta->chain_signal[i];
13718 
13719 		if (rssi != ATH12K_DEFAULT_NOISE_FLOOR &&
13720 		    rssi != ATH12K_INVALID_RSSI_FULL &&
13721 		    rssi != ATH12K_INVALID_RSSI_EMPTY &&
13722 		    rssi != 0) {
13723 			sinfo->chain_signal[i] = rssi;
13724 			sinfo->chains |= BIT(i);
13725 			sinfo->filled |= BIT_ULL(NL80211_STA_INFO_CHAIN_SIGNAL);
13726 		}
13727 	}
13728 }
13729 
ath12k_mac_op_sta_statistics(struct ieee80211_hw * hw,struct ieee80211_vif * vif,struct ieee80211_sta * sta,struct station_info * sinfo)13730 void ath12k_mac_op_sta_statistics(struct ieee80211_hw *hw,
13731 				  struct ieee80211_vif *vif,
13732 				  struct ieee80211_sta *sta,
13733 				  struct station_info *sinfo)
13734 {
13735 	struct ath12k_sta *ahsta = ath12k_sta_to_ahsta(sta);
13736 	struct ath12k_dp_link_peer_rate_info rate_info = {};
13737 	struct ath12k_fw_stats_req_params params = {};
13738 	struct ath12k_dp_link_peer *peer;
13739 	struct ath12k_link_sta *arsta;
13740 	s8 signal, noise_floor;
13741 	struct ath12k_dp *dp;
13742 	struct ath12k *ar;
13743 	bool db2dbm;
13744 
13745 	lockdep_assert_wiphy(hw->wiphy);
13746 
13747 	arsta = &ahsta->deflink;
13748 	ar = ath12k_get_ar_by_vif(hw, vif, arsta->link_id);
13749 	if (!ar)
13750 		return;
13751 
13752 	dp = ath12k_ab_to_dp(ar->ab);
13753 	ath12k_dp_link_peer_get_sta_rate_info_stats(dp, arsta->addr, &rate_info);
13754 
13755 	db2dbm = test_bit(WMI_TLV_SERVICE_HW_DB2DBM_CONVERSION_SUPPORT,
13756 			  ar->ab->wmi_ab.svc_map);
13757 
13758 	sinfo->rx_duration = rate_info.rx_duration;
13759 	sinfo->filled |= BIT_ULL(NL80211_STA_INFO_RX_DURATION);
13760 
13761 	sinfo->tx_duration = rate_info.tx_duration;
13762 	sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_DURATION);
13763 
13764 	if (rate_info.txrate.legacy || rate_info.txrate.nss) {
13765 		if (rate_info.txrate.legacy) {
13766 			sinfo->txrate.legacy = rate_info.txrate.legacy;
13767 		} else {
13768 			sinfo->txrate.mcs = rate_info.txrate.mcs;
13769 			sinfo->txrate.nss = rate_info.txrate.nss;
13770 			sinfo->txrate.bw = rate_info.txrate.bw;
13771 			sinfo->txrate.he_gi = rate_info.txrate.he_gi;
13772 			sinfo->txrate.he_dcm = rate_info.txrate.he_dcm;
13773 			sinfo->txrate.he_ru_alloc = rate_info.txrate.he_ru_alloc;
13774 			sinfo->txrate.eht_gi = rate_info.txrate.eht_gi;
13775 			sinfo->txrate.eht_ru_alloc = rate_info.txrate.eht_ru_alloc;
13776 		}
13777 		sinfo->txrate.flags = rate_info.txrate.flags;
13778 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_BITRATE);
13779 	}
13780 
13781 	/* TODO: Use real NF instead of default one. */
13782 	signal = rate_info.rssi_comb;
13783 
13784 	params.pdev_id = ath12k_mac_get_target_pdev_id(ar);
13785 	params.vdev_id = 0;
13786 	params.stats_id = WMI_REQUEST_VDEV_STAT;
13787 
13788 	if (!signal &&
13789 	    ahsta->ahvif->vdev_type == WMI_VDEV_TYPE_STA &&
13790 	    !(ath12k_mac_get_fw_stats(ar, &params))) {
13791 		signal = arsta->rssi_beacon;
13792 		ath12k_fw_stats_reset(ar);
13793 	}
13794 
13795 	params.stats_id = WMI_REQUEST_RSSI_PER_CHAIN_STAT;
13796 	if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_CHAIN_SIGNAL)) &&
13797 	    ahsta->ahvif->vdev_type == WMI_VDEV_TYPE_STA &&
13798 	    !(ath12k_mac_get_fw_stats(ar, &params))) {
13799 		ath12k_mac_put_chain_rssi(sinfo, arsta);
13800 		ath12k_fw_stats_reset(ar);
13801 	}
13802 
13803 	spin_lock_bh(&ar->data_lock);
13804 	noise_floor = ath12k_pdev_get_noise_floor(ar);
13805 	spin_unlock_bh(&ar->data_lock);
13806 
13807 	if (signal) {
13808 		sinfo->signal = db2dbm ? signal : signal + noise_floor;
13809 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_SIGNAL);
13810 	}
13811 
13812 	sinfo->signal_avg = rate_info.signal_avg;
13813 
13814 	if (!db2dbm)
13815 		sinfo->signal_avg += noise_floor;
13816 
13817 	sinfo->filled |= BIT_ULL(NL80211_STA_INFO_SIGNAL_AVG);
13818 
13819 	spin_lock_bh(&dp->dp_lock);
13820 	peer = ath12k_dp_link_peer_find_by_addr(dp, arsta->addr);
13821 	if (!peer) {
13822 		spin_unlock_bh(&dp->dp_lock);
13823 		return;
13824 	}
13825 
13826 	sinfo->tx_retries = peer->tx_retry_count;
13827 	sinfo->tx_failed = peer->tx_retry_failed;
13828 	sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_RETRIES);
13829 	sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_FAILED);
13830 
13831 	spin_unlock_bh(&dp->dp_lock);
13832 }
13833 EXPORT_SYMBOL(ath12k_mac_op_sta_statistics);
13834 
ath12k_mac_op_link_sta_statistics(struct ieee80211_hw * hw,struct ieee80211_vif * vif,struct ieee80211_link_sta * link_sta,struct link_station_info * link_sinfo)13835 void ath12k_mac_op_link_sta_statistics(struct ieee80211_hw *hw,
13836 				       struct ieee80211_vif *vif,
13837 				       struct ieee80211_link_sta *link_sta,
13838 				       struct link_station_info *link_sinfo)
13839 {
13840 	struct ath12k_sta *ahsta = ath12k_sta_to_ahsta(link_sta->sta);
13841 	struct ath12k_fw_stats_req_params params = {};
13842 	struct ath12k_dp_link_peer *peer;
13843 	struct ath12k_link_sta *arsta;
13844 	struct ath12k *ar;
13845 	s8 signal;
13846 	bool db2dbm;
13847 
13848 	lockdep_assert_wiphy(hw->wiphy);
13849 
13850 	arsta = wiphy_dereference(hw->wiphy, ahsta->link[link_sta->link_id]);
13851 
13852 	if (!arsta)
13853 		return;
13854 
13855 	ar = ath12k_get_ar_by_vif(hw, vif, arsta->link_id);
13856 	if (!ar)
13857 		return;
13858 
13859 	db2dbm = test_bit(WMI_TLV_SERVICE_HW_DB2DBM_CONVERSION_SUPPORT,
13860 			  ar->ab->wmi_ab.svc_map);
13861 
13862 	spin_lock_bh(&ar->ab->dp->dp_lock);
13863 	peer = ath12k_dp_link_peer_find_by_addr(ar->ab->dp, arsta->addr);
13864 	if (!peer) {
13865 		spin_unlock_bh(&ar->ab->dp->dp_lock);
13866 		return;
13867 	}
13868 
13869 	link_sinfo->rx_duration = peer->rx_duration;
13870 	link_sinfo->filled |= BIT_ULL(NL80211_STA_INFO_RX_DURATION);
13871 
13872 	link_sinfo->tx_duration = peer->tx_duration;
13873 	link_sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_DURATION);
13874 
13875 	if (peer->txrate.legacy || peer->txrate.nss) {
13876 		if (peer->txrate.legacy) {
13877 			link_sinfo->txrate.legacy = peer->txrate.legacy;
13878 		} else {
13879 			link_sinfo->txrate.mcs = peer->txrate.mcs;
13880 			link_sinfo->txrate.nss = peer->txrate.nss;
13881 			link_sinfo->txrate.bw = peer->txrate.bw;
13882 			link_sinfo->txrate.he_gi = peer->txrate.he_gi;
13883 			link_sinfo->txrate.he_dcm = peer->txrate.he_dcm;
13884 			link_sinfo->txrate.he_ru_alloc =
13885 				peer->txrate.he_ru_alloc;
13886 			link_sinfo->txrate.eht_gi = peer->txrate.eht_gi;
13887 			link_sinfo->txrate.eht_ru_alloc =
13888 				peer->txrate.eht_ru_alloc;
13889 		}
13890 		link_sinfo->txrate.flags = peer->txrate.flags;
13891 		link_sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_BITRATE);
13892 	}
13893 
13894 	link_sinfo->signal_avg = ewma_avg_rssi_read(&peer->avg_rssi);
13895 
13896 	if (!db2dbm)
13897 		link_sinfo->signal_avg += ATH12K_DEFAULT_NOISE_FLOOR;
13898 
13899 	link_sinfo->filled |= BIT_ULL(NL80211_STA_INFO_SIGNAL_AVG);
13900 
13901 	link_sinfo->tx_retries = peer->tx_retry_count;
13902 	link_sinfo->tx_failed = peer->tx_retry_failed;
13903 	link_sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_RETRIES);
13904 	link_sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_FAILED);
13905 
13906 	/* TODO: Use real NF instead of default one. */
13907 	signal = peer->rssi_comb;
13908 
13909 	spin_unlock_bh(&ar->ab->dp->dp_lock);
13910 
13911 	if (!signal && ahsta->ahvif->vdev_type == WMI_VDEV_TYPE_STA) {
13912 		params.pdev_id = ath12k_mac_get_target_pdev_id(ar);
13913 		params.vdev_id = 0;
13914 		params.stats_id = WMI_REQUEST_VDEV_STAT;
13915 
13916 		if (!ath12k_mac_get_fw_stats(ar, &params)) {
13917 			signal = arsta->rssi_beacon;
13918 			ath12k_fw_stats_reset(ar);
13919 		}
13920 	}
13921 
13922 	if (signal) {
13923 		link_sinfo->signal =
13924 			db2dbm ? signal : signal + ATH12K_DEFAULT_NOISE_FLOOR;
13925 		link_sinfo->filled |= BIT_ULL(NL80211_STA_INFO_SIGNAL);
13926 	}
13927 }
13928 EXPORT_SYMBOL(ath12k_mac_op_link_sta_statistics);
13929 
ath12k_mac_op_cancel_remain_on_channel(struct ieee80211_hw * hw,struct ieee80211_vif * vif)13930 int ath12k_mac_op_cancel_remain_on_channel(struct ieee80211_hw *hw,
13931 					   struct ieee80211_vif *vif)
13932 {
13933 	struct ath12k_hw *ah = ath12k_hw_to_ah(hw);
13934 	struct ath12k *ar;
13935 
13936 	ar = ath12k_ah_to_ar(ah, 0);
13937 
13938 	lockdep_assert_wiphy(hw->wiphy);
13939 
13940 	spin_lock_bh(&ar->data_lock);
13941 	ar->scan.roc_notify = false;
13942 	spin_unlock_bh(&ar->data_lock);
13943 
13944 	ath12k_scan_abort(ar);
13945 
13946 	cancel_delayed_work_sync(&ar->scan.timeout);
13947 	wiphy_work_flush(hw->wiphy, &ar->scan.vdev_clean_wk);
13948 
13949 	return 0;
13950 }
13951 EXPORT_SYMBOL(ath12k_mac_op_cancel_remain_on_channel);
13952 
ath12k_mac_op_remain_on_channel(struct ieee80211_hw * hw,struct ieee80211_vif * vif,struct ieee80211_channel * chan,int duration,enum ieee80211_roc_type type)13953 int ath12k_mac_op_remain_on_channel(struct ieee80211_hw *hw,
13954 				    struct ieee80211_vif *vif,
13955 				    struct ieee80211_channel *chan,
13956 				    int duration,
13957 				    enum ieee80211_roc_type type)
13958 {
13959 	struct ath12k_vif *ahvif = ath12k_vif_to_ahvif(vif);
13960 	struct ath12k_hw *ah = ath12k_hw_to_ah(hw);
13961 	struct ath12k_link_vif *arvif;
13962 	struct ath12k *ar;
13963 	u32 scan_time_msec;
13964 	bool create = true;
13965 	u8 link_id;
13966 	int ret;
13967 
13968 	lockdep_assert_wiphy(hw->wiphy);
13969 
13970 	ar = ath12k_mac_select_scan_device(hw, vif, chan->center_freq);
13971 	if (!ar)
13972 		return -EINVAL;
13973 
13974 	/* check if any of the links of ML VIF is already started on
13975 	 * radio(ar) corresponding to given scan frequency and use it,
13976 	 * if not use deflink(link 0) for scan purpose.
13977 	 */
13978 
13979 	link_id = ath12k_mac_find_link_id_by_ar(ahvif, ar);
13980 	arvif = ath12k_mac_assign_link_vif(ah, vif, link_id);
13981 	/* If the vif is already assigned to a specific vdev of an ar,
13982 	 * check whether its already started, vdev which is started
13983 	 * are not allowed to switch to a new radio.
13984 	 * If the vdev is not started, but was earlier created on a
13985 	 * different ar, delete that vdev and create a new one. We don't
13986 	 * delete at the scan stop as an optimization to avoid redundant
13987 	 * delete-create vdev's for the same ar, in case the request is
13988 	 * always on the same band for the vif
13989 	 */
13990 	if (arvif->is_created) {
13991 		if (WARN_ON(!arvif->ar))
13992 			return -EINVAL;
13993 
13994 		if (ar != arvif->ar && arvif->is_started)
13995 			return -EBUSY;
13996 
13997 		if (ar != arvif->ar) {
13998 			ath12k_mac_remove_link_interface(hw, arvif);
13999 			ath12k_mac_unassign_link_vif(arvif);
14000 		} else {
14001 			create = false;
14002 		}
14003 	}
14004 
14005 	if (create) {
14006 		arvif = ath12k_mac_assign_link_vif(ah, vif, link_id);
14007 
14008 		ret = ath12k_mac_vdev_create(ar, arvif);
14009 		if (ret) {
14010 			ath12k_warn(ar->ab, "unable to create scan vdev for roc: %d\n",
14011 				    ret);
14012 			ath12k_mac_unassign_link_vif(arvif);
14013 			return ret;
14014 		}
14015 	}
14016 
14017 	spin_lock_bh(&ar->data_lock);
14018 
14019 	switch (ar->scan.state) {
14020 	case ATH12K_SCAN_IDLE:
14021 		reinit_completion(&ar->scan.started);
14022 		reinit_completion(&ar->scan.completed);
14023 		reinit_completion(&ar->scan.on_channel);
14024 		ar->scan.state = ATH12K_SCAN_STARTING;
14025 		ar->scan.is_roc = true;
14026 		ar->scan.arvif = arvif;
14027 		ar->scan.roc_freq = chan->center_freq;
14028 		ar->scan.roc_notify = true;
14029 		ret = 0;
14030 		break;
14031 	case ATH12K_SCAN_STARTING:
14032 	case ATH12K_SCAN_RUNNING:
14033 	case ATH12K_SCAN_ABORTING:
14034 		ret = -EBUSY;
14035 		break;
14036 	}
14037 
14038 	spin_unlock_bh(&ar->data_lock);
14039 
14040 	if (ret)
14041 		return ret;
14042 
14043 	scan_time_msec = hw->wiphy->max_remain_on_channel_duration * 2;
14044 
14045 	struct ath12k_wmi_scan_req_arg *arg __free(kfree) =
14046 					kzalloc_flex(*arg, chan_list, 1);
14047 	if (!arg)
14048 		return -ENOMEM;
14049 
14050 	arg->num_chan = 1;
14051 	ath12k_wmi_start_scan_init(ar, arg);
14052 
14053 	arg->vdev_id = arvif->vdev_id;
14054 	arg->scan_id = ATH12K_SCAN_ID;
14055 	arg->chan_list[0] = chan->center_freq;
14056 	arg->dwell_time_active = scan_time_msec;
14057 	arg->dwell_time_passive = scan_time_msec;
14058 	arg->max_scan_time = scan_time_msec;
14059 	arg->scan_f_passive = 1;
14060 	arg->burst_duration = duration;
14061 
14062 	ret = ath12k_start_scan(ar, arg);
14063 	if (ret) {
14064 		ath12k_warn(ar->ab, "failed to start roc scan: %d\n", ret);
14065 
14066 		spin_lock_bh(&ar->data_lock);
14067 		ar->scan.state = ATH12K_SCAN_IDLE;
14068 		spin_unlock_bh(&ar->data_lock);
14069 		return ret;
14070 	}
14071 
14072 	ret = wait_for_completion_timeout(&ar->scan.on_channel, 3 * HZ);
14073 	if (ret == 0) {
14074 		ath12k_warn(ar->ab, "failed to switch to channel for roc scan\n");
14075 		ret = ath12k_scan_stop(ar);
14076 		if (ret)
14077 			ath12k_warn(ar->ab, "failed to stop scan: %d\n", ret);
14078 		return -ETIMEDOUT;
14079 	}
14080 
14081 	ieee80211_queue_delayed_work(hw, &ar->scan.timeout,
14082 				     msecs_to_jiffies(duration));
14083 
14084 	return 0;
14085 }
14086 EXPORT_SYMBOL(ath12k_mac_op_remain_on_channel);
14087 
ath12k_mac_op_set_rekey_data(struct ieee80211_hw * hw,struct ieee80211_vif * vif,struct cfg80211_gtk_rekey_data * data)14088 void ath12k_mac_op_set_rekey_data(struct ieee80211_hw *hw,
14089 				  struct ieee80211_vif *vif,
14090 				  struct cfg80211_gtk_rekey_data *data)
14091 {
14092 	struct ath12k_vif *ahvif = ath12k_vif_to_ahvif(vif);
14093 	struct ath12k_rekey_data *rekey_data;
14094 	struct ath12k_hw *ah = ath12k_hw_to_ah(hw);
14095 	struct ath12k *ar = ath12k_ah_to_ar(ah, 0);
14096 	struct ath12k_link_vif *arvif;
14097 
14098 	lockdep_assert_wiphy(hw->wiphy);
14099 
14100 	arvif = &ahvif->deflink;
14101 	rekey_data = &arvif->rekey_data;
14102 
14103 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac set rekey data vdev %d\n",
14104 		   arvif->vdev_id);
14105 
14106 	memcpy(rekey_data->kck, data->kck, NL80211_KCK_LEN);
14107 	memcpy(rekey_data->kek, data->kek, NL80211_KEK_LEN);
14108 
14109 	/* The supplicant works on big-endian, the firmware expects it on
14110 	 * little endian.
14111 	 */
14112 	rekey_data->replay_ctr = get_unaligned_be64(data->replay_ctr);
14113 
14114 	arvif->rekey_data.enable_offload = true;
14115 
14116 	ath12k_dbg_dump(ar->ab, ATH12K_DBG_MAC, "kck", NULL,
14117 			rekey_data->kck, NL80211_KCK_LEN);
14118 	ath12k_dbg_dump(ar->ab, ATH12K_DBG_MAC, "kek", NULL,
14119 			rekey_data->kck, NL80211_KEK_LEN);
14120 	ath12k_dbg_dump(ar->ab, ATH12K_DBG_MAC, "replay ctr", NULL,
14121 			&rekey_data->replay_ctr, sizeof(rekey_data->replay_ctr));
14122 }
14123 EXPORT_SYMBOL(ath12k_mac_op_set_rekey_data);
14124 
ath12k_mac_update_freq_range(struct ath12k * ar,u32 freq_low,u32 freq_high)14125 void ath12k_mac_update_freq_range(struct ath12k *ar,
14126 				  u32 freq_low, u32 freq_high)
14127 {
14128 	if (!(freq_low && freq_high))
14129 		return;
14130 
14131 	if (ar->freq_range.start_freq || ar->freq_range.end_freq) {
14132 		ar->freq_range.start_freq = min(ar->freq_range.start_freq,
14133 						MHZ_TO_KHZ(freq_low));
14134 		ar->freq_range.end_freq = max(ar->freq_range.end_freq,
14135 					      MHZ_TO_KHZ(freq_high));
14136 	} else {
14137 		ar->freq_range.start_freq = MHZ_TO_KHZ(freq_low);
14138 		ar->freq_range.end_freq = MHZ_TO_KHZ(freq_high);
14139 	}
14140 
14141 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
14142 		   "mac pdev %u freq limit updated. New range %u->%u MHz\n",
14143 		   ar->pdev->pdev_id, KHZ_TO_MHZ(ar->freq_range.start_freq),
14144 		   KHZ_TO_MHZ(ar->freq_range.end_freq));
14145 }
14146 
ath12k_mac_update_ch_list(struct ath12k * ar,struct ieee80211_supported_band * band,u32 freq_low,u32 freq_high)14147 static void ath12k_mac_update_ch_list(struct ath12k *ar,
14148 				      struct ieee80211_supported_band *band,
14149 				      u32 freq_low, u32 freq_high)
14150 {
14151 	int i;
14152 
14153 	if (!(freq_low && freq_high))
14154 		return;
14155 
14156 	for (i = 0; i < band->n_channels; i++) {
14157 		if (band->channels[i].center_freq < freq_low ||
14158 		    band->channels[i].center_freq > freq_high)
14159 			band->channels[i].flags |= IEEE80211_CHAN_DISABLED;
14160 	}
14161 }
14162 
ath12k_get_phy_id(struct ath12k * ar,u32 band)14163 static u32 ath12k_get_phy_id(struct ath12k *ar, u32 band)
14164 {
14165 	struct ath12k_pdev *pdev = ar->pdev;
14166 	struct ath12k_pdev_cap *pdev_cap = &pdev->cap;
14167 
14168 	if (band == WMI_HOST_WLAN_2GHZ_CAP)
14169 		return pdev_cap->band[NL80211_BAND_2GHZ].phy_id;
14170 
14171 	if (band == WMI_HOST_WLAN_5GHZ_CAP)
14172 		return pdev_cap->band[NL80211_BAND_5GHZ].phy_id;
14173 
14174 	ath12k_warn(ar->ab, "unsupported phy cap:%d\n", band);
14175 
14176 	return 0;
14177 }
14178 
ath12k_mac_update_band(struct ath12k * ar,struct ieee80211_supported_band * orig_band,struct ieee80211_supported_band * new_band)14179 static int ath12k_mac_update_band(struct ath12k *ar,
14180 				  struct ieee80211_supported_band *orig_band,
14181 				  struct ieee80211_supported_band *new_band)
14182 {
14183 	int i;
14184 
14185 	if (!orig_band || !new_band)
14186 		return -EINVAL;
14187 
14188 	if (orig_band->band != new_band->band)
14189 		return -EINVAL;
14190 
14191 	for (i = 0; i < new_band->n_channels; i++) {
14192 		if (new_band->channels[i].flags & IEEE80211_CHAN_DISABLED)
14193 			continue;
14194 		/* An enabled channel in new_band should not be already enabled
14195 		 * in the orig_band
14196 		 */
14197 		if (WARN_ON(!(orig_band->channels[i].flags &
14198 			      IEEE80211_CHAN_DISABLED)))
14199 			return -EINVAL;
14200 		orig_band->channels[i].flags &= ~IEEE80211_CHAN_DISABLED;
14201 	}
14202 	return 0;
14203 }
14204 
ath12k_mac_update_5_9_ghz_ch_list(struct ath12k * ar,struct ieee80211_supported_band * band)14205 static void ath12k_mac_update_5_9_ghz_ch_list(struct ath12k *ar,
14206 					      struct ieee80211_supported_band *band)
14207 {
14208 	int i;
14209 
14210 	if (test_bit(WMI_TLV_SERVICE_5_9GHZ_SUPPORT,
14211 		     ar->ab->wmi_ab.svc_map))
14212 		return;
14213 
14214 	guard(spinlock_bh)(&ar->ab->base_lock);
14215 	if (ar->ab->dfs_region != ATH12K_DFS_REG_FCC)
14216 		return;
14217 
14218 	for (i = 0; i < band->n_channels; i++) {
14219 		if (band->channels[i].center_freq >= ATH12K_5_9_GHZ_MIN_FREQ &&
14220 		    band->channels[i].center_freq <= ATH12K_5_9_GHZ_MAX_FREQ)
14221 			band->channels[i].flags |= IEEE80211_CHAN_DISABLED;
14222 	}
14223 }
14224 
ath12k_mac_setup_channels_rates(struct ath12k * ar,u32 supported_bands,struct ieee80211_supported_band * bands[])14225 static int ath12k_mac_setup_channels_rates(struct ath12k *ar,
14226 					   u32 supported_bands,
14227 					   struct ieee80211_supported_band *bands[])
14228 {
14229 	struct ieee80211_supported_band *band;
14230 	struct ath12k_wmi_hal_reg_capabilities_ext_arg *reg_cap;
14231 	struct ath12k_base *ab = ar->ab;
14232 	u32 phy_id, freq_low, freq_high;
14233 	struct ath12k_hw *ah = ar->ah;
14234 	void *channels;
14235 	int ret;
14236 
14237 	BUILD_BUG_ON((ARRAY_SIZE(ath12k_2ghz_channels) +
14238 		      ARRAY_SIZE(ath12k_5ghz_channels) +
14239 		      ARRAY_SIZE(ath12k_6ghz_channels)) !=
14240 		     ATH12K_NUM_CHANS);
14241 
14242 	reg_cap = &ab->hal_reg_cap[ar->pdev_idx];
14243 
14244 	if (supported_bands & WMI_HOST_WLAN_2GHZ_CAP) {
14245 		channels = kmemdup(ath12k_2ghz_channels,
14246 				   sizeof(ath12k_2ghz_channels),
14247 				   GFP_KERNEL);
14248 		if (!channels)
14249 			return -ENOMEM;
14250 
14251 		band = &ar->mac.sbands[NL80211_BAND_2GHZ];
14252 		band->band = NL80211_BAND_2GHZ;
14253 		band->n_channels = ARRAY_SIZE(ath12k_2ghz_channels);
14254 		band->channels = channels;
14255 		band->n_bitrates = ath12k_g_rates_size;
14256 		band->bitrates = ath12k_g_rates;
14257 
14258 		if (ab->hw_params->single_pdev_only) {
14259 			phy_id = ath12k_get_phy_id(ar, WMI_HOST_WLAN_2GHZ_CAP);
14260 			reg_cap = &ab->hal_reg_cap[phy_id];
14261 		}
14262 
14263 		freq_low = max(reg_cap->low_2ghz_chan,
14264 			       ab->reg_freq_2ghz.start_freq);
14265 		freq_high = min(reg_cap->high_2ghz_chan,
14266 				ab->reg_freq_2ghz.end_freq);
14267 
14268 		ath12k_mac_update_ch_list(ar, band,
14269 					  reg_cap->low_2ghz_chan,
14270 					  reg_cap->high_2ghz_chan);
14271 
14272 		ath12k_mac_update_freq_range(ar, freq_low, freq_high);
14273 
14274 		if (!bands[NL80211_BAND_2GHZ]) {
14275 			bands[NL80211_BAND_2GHZ] = band;
14276 		} else {
14277 			/* Split mac in same band under same wiphy */
14278 			ret = ath12k_mac_update_band(ar, bands[NL80211_BAND_2GHZ], band);
14279 			if (ret) {
14280 				kfree(channels);
14281 				band->channels = NULL;
14282 				return ret;
14283 			}
14284 			ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac pdev %u identified as 2 GHz split mac with start freq %d end freq %d",
14285 				   ar->pdev->pdev_id,
14286 				   KHZ_TO_MHZ(ar->freq_range.start_freq),
14287 				   KHZ_TO_MHZ(ar->freq_range.end_freq));
14288 		}
14289 	}
14290 
14291 	if (supported_bands & WMI_HOST_WLAN_5GHZ_CAP) {
14292 		if (reg_cap->high_5ghz_chan >= ATH12K_MIN_6GHZ_FREQ) {
14293 			channels = kmemdup(ath12k_6ghz_channels,
14294 					   sizeof(ath12k_6ghz_channels), GFP_KERNEL);
14295 			if (!channels) {
14296 				kfree(ar->mac.sbands[NL80211_BAND_2GHZ].channels);
14297 				return -ENOMEM;
14298 			}
14299 
14300 			ar->supports_6ghz = true;
14301 			band = &ar->mac.sbands[NL80211_BAND_6GHZ];
14302 			band->band = NL80211_BAND_6GHZ;
14303 			band->n_channels = ARRAY_SIZE(ath12k_6ghz_channels);
14304 			band->channels = channels;
14305 			band->n_bitrates = ath12k_a_rates_size;
14306 			band->bitrates = ath12k_a_rates;
14307 
14308 			freq_low = max(reg_cap->low_5ghz_chan,
14309 				       ab->reg_freq_6ghz.start_freq);
14310 			freq_high = min(reg_cap->high_5ghz_chan,
14311 					ab->reg_freq_6ghz.end_freq);
14312 
14313 			ath12k_mac_update_ch_list(ar, band,
14314 						  reg_cap->low_5ghz_chan,
14315 						  reg_cap->high_5ghz_chan);
14316 
14317 			ath12k_mac_update_freq_range(ar, freq_low, freq_high);
14318 			ah->use_6ghz_regd = true;
14319 
14320 			if (!bands[NL80211_BAND_6GHZ]) {
14321 				bands[NL80211_BAND_6GHZ] = band;
14322 			} else {
14323 				/* Split mac in same band under same wiphy */
14324 				ret = ath12k_mac_update_band(ar,
14325 							     bands[NL80211_BAND_6GHZ],
14326 							     band);
14327 				if (ret) {
14328 					kfree(ar->mac.sbands[NL80211_BAND_2GHZ].channels);
14329 					ar->mac.sbands[NL80211_BAND_2GHZ].channels = NULL;
14330 					kfree(channels);
14331 					band->channels = NULL;
14332 					return ret;
14333 				}
14334 				ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac pdev %u identified as 6 GHz split mac with start freq %d end freq %d",
14335 					   ar->pdev->pdev_id,
14336 					   KHZ_TO_MHZ(ar->freq_range.start_freq),
14337 					   KHZ_TO_MHZ(ar->freq_range.end_freq));
14338 			}
14339 		}
14340 
14341 		if (reg_cap->low_5ghz_chan < ATH12K_MIN_6GHZ_FREQ) {
14342 			channels = kmemdup(ath12k_5ghz_channels,
14343 					   sizeof(ath12k_5ghz_channels),
14344 					   GFP_KERNEL);
14345 			if (!channels) {
14346 				kfree(ar->mac.sbands[NL80211_BAND_2GHZ].channels);
14347 				kfree(ar->mac.sbands[NL80211_BAND_6GHZ].channels);
14348 				return -ENOMEM;
14349 			}
14350 
14351 			band = &ar->mac.sbands[NL80211_BAND_5GHZ];
14352 			band->band = NL80211_BAND_5GHZ;
14353 			band->n_channels = ARRAY_SIZE(ath12k_5ghz_channels);
14354 			band->channels = channels;
14355 			band->n_bitrates = ath12k_a_rates_size;
14356 			band->bitrates = ath12k_a_rates;
14357 
14358 			ath12k_mac_update_5_9_ghz_ch_list(ar, band);
14359 
14360 			if (ab->hw_params->single_pdev_only) {
14361 				phy_id = ath12k_get_phy_id(ar, WMI_HOST_WLAN_5GHZ_CAP);
14362 				reg_cap = &ab->hal_reg_cap[phy_id];
14363 			}
14364 
14365 			freq_low = max(reg_cap->low_5ghz_chan,
14366 				       ab->reg_freq_5ghz.start_freq);
14367 			freq_high = min(reg_cap->high_5ghz_chan,
14368 					ab->reg_freq_5ghz.end_freq);
14369 
14370 			ath12k_mac_update_ch_list(ar, band,
14371 						  reg_cap->low_5ghz_chan,
14372 						  reg_cap->high_5ghz_chan);
14373 
14374 			ath12k_mac_update_freq_range(ar, freq_low, freq_high);
14375 
14376 			if (!bands[NL80211_BAND_5GHZ]) {
14377 				bands[NL80211_BAND_5GHZ] = band;
14378 			} else {
14379 				/* Split mac in same band under same wiphy */
14380 				ret = ath12k_mac_update_band(ar,
14381 							     bands[NL80211_BAND_5GHZ],
14382 							     band);
14383 				if (ret) {
14384 					kfree(ar->mac.sbands[NL80211_BAND_2GHZ].channels);
14385 					ar->mac.sbands[NL80211_BAND_2GHZ].channels = NULL;
14386 					kfree(ar->mac.sbands[NL80211_BAND_6GHZ].channels);
14387 					ar->mac.sbands[NL80211_BAND_2GHZ].channels = NULL;
14388 					kfree(channels);
14389 					band->channels = NULL;
14390 					return ret;
14391 				}
14392 				ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac pdev %u identified as 5 GHz split mac with start freq %d end freq %d",
14393 					   ar->pdev->pdev_id,
14394 					   KHZ_TO_MHZ(ar->freq_range.start_freq),
14395 					   KHZ_TO_MHZ(ar->freq_range.end_freq));
14396 			}
14397 		}
14398 	}
14399 
14400 	return 0;
14401 }
14402 
ath12k_mac_get_ifmodes(struct ath12k_hw * ah)14403 static u16 ath12k_mac_get_ifmodes(struct ath12k_hw *ah)
14404 {
14405 	struct ath12k *ar;
14406 	int i;
14407 	u16 interface_modes = U16_MAX;
14408 
14409 	for_each_ar(ah, ar, i)
14410 		interface_modes &= ar->ab->hw_params->interface_modes;
14411 
14412 	return interface_modes == U16_MAX ? 0 : interface_modes;
14413 }
14414 
ath12k_mac_is_iface_mode_enable(struct ath12k_hw * ah,enum nl80211_iftype type)14415 static bool ath12k_mac_is_iface_mode_enable(struct ath12k_hw *ah,
14416 					    enum nl80211_iftype type)
14417 {
14418 	struct ath12k *ar;
14419 	int i;
14420 	u16 interface_modes, mode = 0;
14421 	bool is_enable = false;
14422 
14423 	if (type == NL80211_IFTYPE_MESH_POINT) {
14424 		if (IS_ENABLED(CONFIG_MAC80211_MESH))
14425 			mode = BIT(type);
14426 	} else {
14427 		mode = BIT(type);
14428 	}
14429 
14430 	for_each_ar(ah, ar, i) {
14431 		interface_modes = ar->ab->hw_params->interface_modes;
14432 		if (interface_modes & mode) {
14433 			is_enable = true;
14434 			break;
14435 		}
14436 	}
14437 
14438 	return is_enable;
14439 }
14440 
14441 static int
ath12k_mac_setup_radio_iface_comb(struct ath12k * ar,struct ieee80211_iface_combination * comb)14442 ath12k_mac_setup_radio_iface_comb(struct ath12k *ar,
14443 				  struct ieee80211_iface_combination *comb)
14444 {
14445 	u16 interface_modes = ar->ab->hw_params->interface_modes;
14446 	struct ieee80211_iface_limit *limits;
14447 	int n_limits, max_interfaces;
14448 	bool ap, mesh, p2p;
14449 
14450 	ap = interface_modes & BIT(NL80211_IFTYPE_AP);
14451 	p2p = interface_modes & BIT(NL80211_IFTYPE_P2P_DEVICE);
14452 
14453 	mesh = IS_ENABLED(CONFIG_MAC80211_MESH) &&
14454 	       (interface_modes & BIT(NL80211_IFTYPE_MESH_POINT));
14455 
14456 	if ((ap || mesh) && !p2p) {
14457 		n_limits = 2;
14458 		max_interfaces = 16;
14459 	} else if (p2p) {
14460 		n_limits = 3;
14461 		if (ap || mesh)
14462 			max_interfaces = 16;
14463 		else
14464 			max_interfaces = 3;
14465 	} else {
14466 		n_limits = 1;
14467 		max_interfaces = 1;
14468 	}
14469 
14470 	limits = kzalloc_objs(*limits, n_limits);
14471 	if (!limits)
14472 		return -ENOMEM;
14473 
14474 	limits[0].max = 1;
14475 	limits[0].types |= BIT(NL80211_IFTYPE_STATION);
14476 
14477 	if (ap || mesh || p2p)
14478 		limits[1].max = max_interfaces;
14479 
14480 	if (ap)
14481 		limits[1].types |= BIT(NL80211_IFTYPE_AP);
14482 
14483 	if (mesh)
14484 		limits[1].types |= BIT(NL80211_IFTYPE_MESH_POINT);
14485 
14486 	if (p2p) {
14487 		limits[1].types |= BIT(NL80211_IFTYPE_P2P_CLIENT) |
14488 					BIT(NL80211_IFTYPE_P2P_GO);
14489 		limits[2].max = 1;
14490 		limits[2].types |= BIT(NL80211_IFTYPE_P2P_DEVICE);
14491 	}
14492 
14493 	comb[0].limits = limits;
14494 	comb[0].n_limits = n_limits;
14495 	comb[0].max_interfaces = max_interfaces;
14496 	comb[0].beacon_int_infra_match = true;
14497 	comb[0].beacon_int_min_gcd = 100;
14498 
14499 	comb[0].num_different_channels = 1;
14500 	comb[0].radar_detect_widths = BIT(NL80211_CHAN_WIDTH_20_NOHT) |
14501 				      BIT(NL80211_CHAN_WIDTH_20) |
14502 				      BIT(NL80211_CHAN_WIDTH_40) |
14503 				      BIT(NL80211_CHAN_WIDTH_80) |
14504 				      BIT(NL80211_CHAN_WIDTH_160);
14505 
14506 	return 0;
14507 }
14508 
14509 static int
ath12k_mac_setup_global_iface_comb(struct ath12k_hw * ah,struct wiphy_radio * radio,u8 n_radio,struct ieee80211_iface_combination * comb)14510 ath12k_mac_setup_global_iface_comb(struct ath12k_hw *ah,
14511 				   struct wiphy_radio *radio,
14512 				   u8 n_radio,
14513 				   struct ieee80211_iface_combination *comb)
14514 {
14515 	const struct ieee80211_iface_combination *iter_comb;
14516 	struct ieee80211_iface_limit *limits;
14517 	int i, j, n_limits;
14518 	bool ap, mesh, p2p;
14519 
14520 	if (!n_radio)
14521 		return 0;
14522 
14523 	ap = ath12k_mac_is_iface_mode_enable(ah, NL80211_IFTYPE_AP);
14524 	p2p = ath12k_mac_is_iface_mode_enable(ah, NL80211_IFTYPE_P2P_DEVICE);
14525 	mesh = ath12k_mac_is_iface_mode_enable(ah, NL80211_IFTYPE_MESH_POINT);
14526 
14527 	if ((ap || mesh) && !p2p)
14528 		n_limits = 2;
14529 	else if (p2p)
14530 		n_limits = 3;
14531 	else
14532 		n_limits = 1;
14533 
14534 	limits = kzalloc_objs(*limits, n_limits);
14535 	if (!limits)
14536 		return -ENOMEM;
14537 
14538 	for (i = 0; i < n_radio; i++) {
14539 		iter_comb = radio[i].iface_combinations;
14540 		for (j = 0; j < iter_comb->n_limits && j < n_limits; j++) {
14541 			limits[j].types |= iter_comb->limits[j].types;
14542 			limits[j].max += iter_comb->limits[j].max;
14543 		}
14544 
14545 		comb->max_interfaces += iter_comb->max_interfaces;
14546 		comb->num_different_channels += iter_comb->num_different_channels;
14547 		comb->radar_detect_widths |= iter_comb->radar_detect_widths;
14548 	}
14549 
14550 	comb->limits = limits;
14551 	comb->n_limits = n_limits;
14552 	comb->beacon_int_infra_match = true;
14553 	comb->beacon_int_min_gcd = 100;
14554 
14555 	return 0;
14556 }
14557 
14558 static
ath12k_mac_cleanup_iface_comb(const struct ieee80211_iface_combination * iface_comb)14559 void ath12k_mac_cleanup_iface_comb(const struct ieee80211_iface_combination *iface_comb)
14560 {
14561 	kfree(iface_comb[0].limits);
14562 	kfree(iface_comb);
14563 }
14564 
ath12k_mac_cleanup_iface_combinations(struct ath12k_hw * ah)14565 static void ath12k_mac_cleanup_iface_combinations(struct ath12k_hw *ah)
14566 {
14567 	struct wiphy *wiphy = ah->hw->wiphy;
14568 	const struct wiphy_radio *radio;
14569 	int i;
14570 
14571 	if (wiphy->n_radio > 0) {
14572 		radio = wiphy->radio;
14573 		for (i = 0; i < wiphy->n_radio; i++)
14574 			ath12k_mac_cleanup_iface_comb(radio[i].iface_combinations);
14575 
14576 		kfree(wiphy->radio);
14577 	}
14578 
14579 	ath12k_mac_cleanup_iface_comb(wiphy->iface_combinations);
14580 }
14581 
ath12k_mac_setup_iface_combinations(struct ath12k_hw * ah)14582 static int ath12k_mac_setup_iface_combinations(struct ath12k_hw *ah)
14583 {
14584 	struct ieee80211_iface_combination *combinations, *comb;
14585 	struct wiphy *wiphy = ah->hw->wiphy;
14586 	struct wiphy_radio *radio;
14587 	int n_combinations = 1;
14588 	struct ath12k *ar;
14589 	int i, ret;
14590 
14591 	if (ah->num_radio == 1) {
14592 		ar = &ah->radio[0];
14593 
14594 		if (ar->ab->hw_params->single_pdev_only)
14595 			n_combinations = 2;
14596 
14597 		combinations = kzalloc_objs(*combinations, n_combinations);
14598 		if (!combinations)
14599 			return -ENOMEM;
14600 
14601 		ret = ath12k_mac_setup_radio_iface_comb(ar, combinations);
14602 		if (ret) {
14603 			ath12k_hw_warn(ah, "failed to setup radio interface combinations for one radio: %d",
14604 				       ret);
14605 			goto err_free_combinations;
14606 		}
14607 
14608 		if (ar->ab->hw_params->single_pdev_only) {
14609 			comb = combinations + 1;
14610 			memcpy(comb, combinations, sizeof(*comb));
14611 			comb->num_different_channels = 2;
14612 			comb->radar_detect_widths = 0;
14613 		}
14614 
14615 		goto out;
14616 	}
14617 
14618 	combinations = kzalloc_objs(*combinations, n_combinations);
14619 	if (!combinations)
14620 		return -ENOMEM;
14621 
14622 	/* there are multiple radios */
14623 
14624 	radio = kzalloc_objs(*radio, ah->num_radio);
14625 	if (!radio) {
14626 		ret = -ENOMEM;
14627 		goto err_free_combinations;
14628 	}
14629 
14630 	for_each_ar(ah, ar, i) {
14631 		comb = kzalloc_obj(*comb);
14632 		if (!comb) {
14633 			ret = -ENOMEM;
14634 			goto err_free_radios;
14635 		}
14636 
14637 		ret = ath12k_mac_setup_radio_iface_comb(ar, comb);
14638 		if (ret) {
14639 			ath12k_hw_warn(ah, "failed to setup radio interface combinations for radio %d: %d",
14640 				       i, ret);
14641 			kfree(comb);
14642 			goto err_free_radios;
14643 		}
14644 
14645 		radio[i].freq_range = &ar->freq_range;
14646 		radio[i].n_freq_range = 1;
14647 
14648 		radio[i].iface_combinations = comb;
14649 		radio[i].n_iface_combinations = 1;
14650 	}
14651 
14652 	ret = ath12k_mac_setup_global_iface_comb(ah, radio, ah->num_radio, combinations);
14653 	if (ret) {
14654 		ath12k_hw_warn(ah, "failed to setup global interface combinations: %d",
14655 			       ret);
14656 		goto err_free_all_radios;
14657 	}
14658 
14659 	wiphy->radio = radio;
14660 	wiphy->n_radio = ah->num_radio;
14661 
14662 out:
14663 	wiphy->iface_combinations = combinations;
14664 	wiphy->n_iface_combinations = n_combinations;
14665 
14666 	return 0;
14667 
14668 err_free_all_radios:
14669 	i = ah->num_radio;
14670 
14671 err_free_radios:
14672 	while (i--)
14673 		ath12k_mac_cleanup_iface_comb(radio[i].iface_combinations);
14674 
14675 	kfree(radio);
14676 
14677 err_free_combinations:
14678 	kfree(combinations);
14679 
14680 	return ret;
14681 }
14682 
14683 static const u8 ath12k_if_types_ext_capa[] = {
14684 	[0] = WLAN_EXT_CAPA1_EXT_CHANNEL_SWITCHING,
14685 	[2] = WLAN_EXT_CAPA3_MULTI_BSSID_SUPPORT,
14686 	[7] = WLAN_EXT_CAPA8_OPMODE_NOTIF,
14687 };
14688 
14689 static const u8 ath12k_if_types_ext_capa_sta[] = {
14690 	[0] = WLAN_EXT_CAPA1_EXT_CHANNEL_SWITCHING,
14691 	[2] = WLAN_EXT_CAPA3_MULTI_BSSID_SUPPORT,
14692 	[7] = WLAN_EXT_CAPA8_OPMODE_NOTIF,
14693 	[9] = WLAN_EXT_CAPA10_TWT_REQUESTER_SUPPORT,
14694 };
14695 
14696 static const u8 ath12k_if_types_ext_capa_ap[] = {
14697 	[0] = WLAN_EXT_CAPA1_EXT_CHANNEL_SWITCHING,
14698 	[2] = WLAN_EXT_CAPA3_MULTI_BSSID_SUPPORT,
14699 	[7] = WLAN_EXT_CAPA8_OPMODE_NOTIF,
14700 	[9] = WLAN_EXT_CAPA10_TWT_RESPONDER_SUPPORT,
14701 	[10] = WLAN_EXT_CAPA11_EMA_SUPPORT,
14702 };
14703 
14704 static struct wiphy_iftype_ext_capab ath12k_iftypes_ext_capa[] = {
14705 	{
14706 		.extended_capabilities = ath12k_if_types_ext_capa,
14707 		.extended_capabilities_mask = ath12k_if_types_ext_capa,
14708 		.extended_capabilities_len = sizeof(ath12k_if_types_ext_capa),
14709 	}, {
14710 		.iftype = NL80211_IFTYPE_STATION,
14711 		.extended_capabilities = ath12k_if_types_ext_capa_sta,
14712 		.extended_capabilities_mask = ath12k_if_types_ext_capa_sta,
14713 		.extended_capabilities_len =
14714 				sizeof(ath12k_if_types_ext_capa_sta),
14715 	}, {
14716 		.iftype = NL80211_IFTYPE_AP,
14717 		.extended_capabilities = ath12k_if_types_ext_capa_ap,
14718 		.extended_capabilities_mask = ath12k_if_types_ext_capa_ap,
14719 		.extended_capabilities_len =
14720 				sizeof(ath12k_if_types_ext_capa_ap),
14721 		.eml_capabilities = 0,
14722 		.mld_capa_and_ops = 0,
14723 	},
14724 };
14725 
ath12k_mac_cleanup_unregister(struct ath12k * ar)14726 static void ath12k_mac_cleanup_unregister(struct ath12k *ar)
14727 {
14728 	idr_for_each(&ar->txmgmt_idr, ath12k_mac_tx_mgmt_pending_free, ar);
14729 	idr_destroy(&ar->txmgmt_idr);
14730 
14731 	kfree(ar->mac.sbands[NL80211_BAND_2GHZ].channels);
14732 	kfree(ar->mac.sbands[NL80211_BAND_5GHZ].channels);
14733 	kfree(ar->mac.sbands[NL80211_BAND_6GHZ].channels);
14734 }
14735 
ath12k_mac_hw_unregister(struct ath12k_hw * ah)14736 static void ath12k_mac_hw_unregister(struct ath12k_hw *ah)
14737 {
14738 	struct ieee80211_hw *hw = ah->hw;
14739 	struct ath12k *ar;
14740 	int i;
14741 
14742 	for_each_ar(ah, ar, i) {
14743 		cancel_work_sync(&ar->regd_channel_update_work);
14744 		cancel_work_sync(&ar->regd_update_work);
14745 		ath12k_debugfs_unregister(ar);
14746 		ath12k_fw_stats_reset(ar);
14747 	}
14748 
14749 	ieee80211_unregister_hw(hw);
14750 
14751 	for_each_ar(ah, ar, i)
14752 		ath12k_mac_cleanup_unregister(ar);
14753 
14754 	ath12k_mac_cleanup_iface_combinations(ah);
14755 
14756 	SET_IEEE80211_DEV(hw, NULL);
14757 }
14758 
ath12k_mac_setup_register(struct ath12k * ar,u32 * ht_cap,struct ieee80211_supported_band * bands[])14759 static int ath12k_mac_setup_register(struct ath12k *ar,
14760 				     u32 *ht_cap,
14761 				     struct ieee80211_supported_band *bands[])
14762 {
14763 	struct ath12k_pdev_cap *cap = &ar->pdev->cap;
14764 	int ret;
14765 
14766 	init_waitqueue_head(&ar->txmgmt_empty_waitq);
14767 	idr_init(&ar->txmgmt_idr);
14768 	spin_lock_init(&ar->txmgmt_idr_lock);
14769 
14770 	ath12k_pdev_caps_update(ar);
14771 
14772 	ret = ath12k_mac_setup_channels_rates(ar,
14773 					      cap->supported_bands,
14774 					      bands);
14775 	if (ret)
14776 		return ret;
14777 
14778 	ath12k_mac_setup_ht_vht_cap(ar, cap, ht_cap);
14779 	ath12k_mac_setup_sband_iftype_data(ar, cap);
14780 
14781 	ar->max_num_stations = ath12k_core_get_max_station_per_radio(ar->ab);
14782 	ar->max_num_peers = ath12k_core_get_max_peers_per_radio(ar->ab);
14783 
14784 	ar->rssi_info.min_nf_dbm = ATH12K_DEFAULT_NOISE_FLOOR;
14785 	ar->rssi_info.temp_offset = 0;
14786 	ar->rssi_info.noise_floor = ar->rssi_info.min_nf_dbm + ar->rssi_info.temp_offset;
14787 
14788 	ath12k_thermal_init_configs(ar);
14789 
14790 	return 0;
14791 }
14792 
ath12k_mac_hw_register(struct ath12k_hw * ah)14793 static int ath12k_mac_hw_register(struct ath12k_hw *ah)
14794 {
14795 	struct ieee80211_hw *hw = ah->hw;
14796 	struct wiphy *wiphy = hw->wiphy;
14797 	struct ath12k *ar = ath12k_ah_to_ar(ah, 0);
14798 	struct ath12k_base *ab = ar->ab;
14799 	struct ath12k_pdev *pdev;
14800 	struct ath12k_pdev_cap *cap;
14801 	static const u32 cipher_suites[] = {
14802 		WLAN_CIPHER_SUITE_TKIP,
14803 		WLAN_CIPHER_SUITE_CCMP,
14804 		WLAN_CIPHER_SUITE_AES_CMAC,
14805 		WLAN_CIPHER_SUITE_BIP_CMAC_256,
14806 		WLAN_CIPHER_SUITE_BIP_GMAC_128,
14807 		WLAN_CIPHER_SUITE_BIP_GMAC_256,
14808 		WLAN_CIPHER_SUITE_GCMP,
14809 		WLAN_CIPHER_SUITE_GCMP_256,
14810 		WLAN_CIPHER_SUITE_CCMP_256,
14811 	};
14812 	int ret, i, j;
14813 	u32 ht_cap = U32_MAX, antennas_rx = 0, antennas_tx = 0;
14814 	bool is_6ghz = false, is_raw_mode = false, is_monitor_disable = false;
14815 	u8 *mac_addr = NULL;
14816 	u8 mbssid_max_interfaces = 0;
14817 
14818 	wiphy->max_ap_assoc_sta = 0;
14819 
14820 	for_each_ar(ah, ar, i) {
14821 		u32 ht_cap_info = 0;
14822 
14823 		pdev = ar->pdev;
14824 		if (ar->ab->pdevs_macaddr_valid) {
14825 			ether_addr_copy(ar->mac_addr, pdev->mac_addr);
14826 		} else {
14827 			ether_addr_copy(ar->mac_addr, ar->ab->mac_addr);
14828 			ar->mac_addr[4] += ar->pdev_idx;
14829 		}
14830 
14831 		ret = ath12k_mac_setup_register(ar, &ht_cap_info, hw->wiphy->bands);
14832 		if (ret)
14833 			goto err_cleanup_unregister;
14834 
14835 		/* 6 GHz does not support HT Cap, hence do not consider it */
14836 		if (!ar->supports_6ghz)
14837 			ht_cap &= ht_cap_info;
14838 
14839 		wiphy->max_ap_assoc_sta += ar->max_num_stations;
14840 
14841 		/* Advertise the max antenna support of all radios, driver can handle
14842 		 * per pdev specific antenna setting based on pdev cap when antenna
14843 		 * changes are made
14844 		 */
14845 		cap = &pdev->cap;
14846 
14847 		antennas_rx = max_t(u32, antennas_rx, cap->rx_chain_mask);
14848 		antennas_tx = max_t(u32, antennas_tx, cap->tx_chain_mask);
14849 
14850 		if (ar->supports_6ghz)
14851 			is_6ghz = true;
14852 
14853 		if (test_bit(ATH12K_FLAG_RAW_MODE, &ar->ab->dev_flags))
14854 			is_raw_mode = true;
14855 
14856 		if (!ar->ab->hw_params->supports_monitor)
14857 			is_monitor_disable = true;
14858 
14859 		if (i == 0)
14860 			mac_addr = ar->mac_addr;
14861 		else
14862 			mac_addr = ab->mac_addr;
14863 
14864 		mbssid_max_interfaces += TARGET_NUM_VDEVS(ar->ab);
14865 	}
14866 
14867 	wiphy->available_antennas_rx = antennas_rx;
14868 	wiphy->available_antennas_tx = antennas_tx;
14869 
14870 	SET_IEEE80211_PERM_ADDR(hw, mac_addr);
14871 	SET_IEEE80211_DEV(hw, ab->dev);
14872 
14873 	ret = ath12k_mac_setup_iface_combinations(ah);
14874 	if (ret) {
14875 		ath12k_err(ab, "failed to setup interface combinations: %d\n", ret);
14876 		goto err_complete_cleanup_unregister;
14877 	}
14878 
14879 	wiphy->interface_modes = ath12k_mac_get_ifmodes(ah);
14880 
14881 	if (ah->num_radio == 1 &&
14882 	    wiphy->bands[NL80211_BAND_2GHZ] &&
14883 	    wiphy->bands[NL80211_BAND_5GHZ] &&
14884 	    wiphy->bands[NL80211_BAND_6GHZ])
14885 		ieee80211_hw_set(hw, SINGLE_SCAN_ON_ALL_BANDS);
14886 
14887 	ieee80211_hw_set(hw, SIGNAL_DBM);
14888 	ieee80211_hw_set(hw, SUPPORTS_PS);
14889 	ieee80211_hw_set(hw, SUPPORTS_DYNAMIC_PS);
14890 	ieee80211_hw_set(hw, MFP_CAPABLE);
14891 	ieee80211_hw_set(hw, REPORTS_TX_ACK_STATUS);
14892 	ieee80211_hw_set(hw, HAS_RATE_CONTROL);
14893 	ieee80211_hw_set(hw, AP_LINK_PS);
14894 	ieee80211_hw_set(hw, SPECTRUM_MGMT);
14895 	ieee80211_hw_set(hw, CONNECTION_MONITOR);
14896 	ieee80211_hw_set(hw, SUPPORTS_PER_STA_GTK);
14897 	ieee80211_hw_set(hw, CHANCTX_STA_CSA);
14898 	ieee80211_hw_set(hw, QUEUE_CONTROL);
14899 	ieee80211_hw_set(hw, SUPPORTS_TX_FRAG);
14900 	ieee80211_hw_set(hw, REPORTS_LOW_ACK);
14901 	ieee80211_hw_set(hw, NO_VIRTUAL_MONITOR);
14902 
14903 	if (test_bit(WMI_TLV_SERVICE_ETH_OFFLOAD, ar->wmi->wmi_ab->svc_map)) {
14904 		ieee80211_hw_set(hw, SUPPORTS_TX_ENCAP_OFFLOAD);
14905 		ieee80211_hw_set(hw, SUPPORTS_RX_DECAP_OFFLOAD);
14906 	}
14907 
14908 	if (cap->nss_ratio_enabled)
14909 		ieee80211_hw_set(hw, SUPPORTS_VHT_EXT_NSS_BW);
14910 
14911 	if ((ht_cap & WMI_HT_CAP_ENABLED) || is_6ghz) {
14912 		ieee80211_hw_set(hw, AMPDU_AGGREGATION);
14913 		ieee80211_hw_set(hw, TX_AMPDU_SETUP_IN_HW);
14914 		ieee80211_hw_set(hw, SUPPORTS_REORDERING_BUFFER);
14915 		ieee80211_hw_set(hw, SUPPORTS_AMSDU_IN_AMPDU);
14916 		ieee80211_hw_set(hw, USES_RSS);
14917 	}
14918 
14919 	wiphy->features |= NL80211_FEATURE_STATIC_SMPS;
14920 	wiphy->flags |= WIPHY_FLAG_IBSS_RSN;
14921 
14922 	/* TODO: Check if HT capability advertised from firmware is different
14923 	 * for each band for a dual band capable radio. It will be tricky to
14924 	 * handle it when the ht capability different for each band.
14925 	 */
14926 	if (ht_cap & WMI_HT_CAP_DYNAMIC_SMPS ||
14927 	    (is_6ghz && ab->hw_params->supports_dynamic_smps_6ghz))
14928 		wiphy->features |= NL80211_FEATURE_DYNAMIC_SMPS;
14929 
14930 	wiphy->max_scan_ssids = WLAN_SCAN_PARAMS_MAX_SSID;
14931 	wiphy->max_scan_ie_len = WLAN_SCAN_PARAMS_MAX_IE_LEN;
14932 
14933 	hw->max_listen_interval = ATH12K_MAX_HW_LISTEN_INTERVAL;
14934 
14935 	wiphy->flags |= WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL;
14936 	wiphy->flags |= WIPHY_FLAG_HAS_CHANNEL_SWITCH;
14937 	wiphy->max_remain_on_channel_duration = 5000;
14938 
14939 	wiphy->flags |= WIPHY_FLAG_AP_UAPSD;
14940 	wiphy->features |= NL80211_FEATURE_AP_MODE_CHAN_WIDTH_CHANGE |
14941 				   NL80211_FEATURE_AP_SCAN;
14942 
14943 	wiphy->features |= NL80211_FEATURE_TX_POWER_INSERTION;
14944 
14945 	/* Copy over MLO related capabilities received from
14946 	 * WMI_SERVICE_READY_EXT2_EVENT if single_chip_mlo_supp is set.
14947 	 */
14948 	if (ab->ag->mlo_capable) {
14949 		ath12k_iftypes_ext_capa[2].eml_capabilities = cap->eml_cap;
14950 		ath12k_iftypes_ext_capa[2].mld_capa_and_ops = cap->mld_cap;
14951 		wiphy->flags |= WIPHY_FLAG_SUPPORTS_MLO;
14952 
14953 		ieee80211_hw_set(hw, MLO_MCAST_MULTI_LINK_TX);
14954 	}
14955 
14956 	hw->queues = ATH12K_HW_MAX_QUEUES;
14957 	wiphy->tx_queue_len = ATH12K_QUEUE_LEN;
14958 	hw->offchannel_tx_hw_queue = ATH12K_HW_MAX_QUEUES - 1;
14959 	hw->max_rx_aggregation_subframes = IEEE80211_MAX_AMPDU_BUF_EHT;
14960 
14961 	hw->vif_data_size = sizeof(struct ath12k_vif);
14962 	hw->sta_data_size = sizeof(struct ath12k_sta);
14963 	hw->extra_tx_headroom = ab->hw_params->iova_mask;
14964 
14965 	wiphy_ext_feature_set(wiphy, NL80211_EXT_FEATURE_CQM_RSSI_LIST);
14966 	wiphy_ext_feature_set(wiphy, NL80211_EXT_FEATURE_STA_TX_PWR);
14967 	wiphy_ext_feature_set(wiphy, NL80211_EXT_FEATURE_ACK_SIGNAL_SUPPORT);
14968 	if (test_bit(WMI_TLV_SERVICE_BSS_COLOR_OFFLOAD,
14969 		     ab->wmi_ab.svc_map)) {
14970 		wiphy_ext_feature_set(wiphy, NL80211_EXT_FEATURE_BSS_COLOR);
14971 		ieee80211_hw_set(hw, DETECTS_COLOR_COLLISION);
14972 	}
14973 
14974 	wiphy->cipher_suites = cipher_suites;
14975 	wiphy->n_cipher_suites = ARRAY_SIZE(cipher_suites);
14976 
14977 	wiphy->iftype_ext_capab = ath12k_iftypes_ext_capa;
14978 	wiphy->num_iftype_ext_capab = ARRAY_SIZE(ath12k_iftypes_ext_capa);
14979 
14980 	wiphy->mbssid_max_interfaces = mbssid_max_interfaces;
14981 	wiphy->ema_max_profile_periodicity = TARGET_EMA_MAX_PROFILE_PERIOD;
14982 	ieee80211_hw_set(hw, SUPPORTS_MULTI_BSSID);
14983 
14984 	if (is_6ghz) {
14985 		wiphy_ext_feature_set(wiphy,
14986 				      NL80211_EXT_FEATURE_FILS_DISCOVERY);
14987 		wiphy_ext_feature_set(wiphy,
14988 				      NL80211_EXT_FEATURE_UNSOL_BCAST_PROBE_RESP);
14989 	}
14990 
14991 	wiphy_ext_feature_set(wiphy, NL80211_EXT_FEATURE_PUNCT);
14992 	if (test_bit(WMI_TLV_SERVICE_BEACON_PROTECTION_SUPPORT, ab->wmi_ab.svc_map))
14993 		wiphy_ext_feature_set(hw->wiphy, NL80211_EXT_FEATURE_BEACON_PROTECTION);
14994 
14995 	ath12k_reg_init(hw);
14996 
14997 	if (!is_raw_mode) {
14998 		hw->netdev_features = NETIF_F_HW_CSUM;
14999 		ieee80211_hw_set(hw, SW_CRYPTO_CONTROL);
15000 		ieee80211_hw_set(hw, SUPPORT_FAST_XMIT);
15001 	}
15002 
15003 	if (test_bit(WMI_TLV_SERVICE_NLO, ar->wmi->wmi_ab->svc_map)) {
15004 		wiphy->max_sched_scan_ssids = WMI_PNO_MAX_SUPP_NETWORKS;
15005 		wiphy->max_match_sets = WMI_PNO_MAX_SUPP_NETWORKS;
15006 		wiphy->max_sched_scan_ie_len = WMI_PNO_MAX_IE_LENGTH;
15007 		wiphy->max_sched_scan_plans = WMI_PNO_MAX_SCHED_SCAN_PLANS;
15008 		wiphy->max_sched_scan_plan_interval =
15009 					WMI_PNO_MAX_SCHED_SCAN_PLAN_INT;
15010 		wiphy->max_sched_scan_plan_iterations =
15011 					WMI_PNO_MAX_SCHED_SCAN_PLAN_ITRNS;
15012 		wiphy->features |= NL80211_FEATURE_ND_RANDOM_MAC_ADDR;
15013 	}
15014 
15015 	ret = ath12k_wow_init(ar);
15016 	if (ret) {
15017 		ath12k_warn(ar->ab, "failed to init wow: %d\n", ret);
15018 		goto err_cleanup_if_combs;
15019 	}
15020 
15021 	/* Boot-time regulatory updates have already been processed.
15022 	 * Mark them as complete now, because after registration,
15023 	 * cfg80211 will notify us again if there are any pending hints.
15024 	 * We need to wait for those hints to be processed, so it's
15025 	 * important to mark the boot-time updates as complete before
15026 	 * proceeding with registration.
15027 	 */
15028 	for_each_ar(ah, ar, i)
15029 		complete_all(&ar->regd_update_completed);
15030 
15031 	ret = ieee80211_register_hw(hw);
15032 	if (ret) {
15033 		ath12k_err(ab, "ieee80211 registration failed: %d\n", ret);
15034 		goto err_cleanup_if_combs;
15035 	}
15036 
15037 	if (is_monitor_disable)
15038 		/* There's a race between calling ieee80211_register_hw()
15039 		 * and here where the monitor mode is enabled for a little
15040 		 * while. But that time is so short and in practice it doesn't make
15041 		 * a difference in real life.
15042 		 */
15043 		wiphy->interface_modes &= ~BIT(NL80211_IFTYPE_MONITOR);
15044 
15045 	for_each_ar(ah, ar, i) {
15046 		/* Apply the regd received during initialization */
15047 		ret = ath12k_regd_update(ar, true);
15048 		if (ret) {
15049 			ath12k_err(ar->ab, "ath12k regd update failed: %d\n", ret);
15050 			goto err_unregister_hw;
15051 		}
15052 
15053 		if (ar->ab->hw_params->current_cc_support && ab->new_alpha2[0]) {
15054 			struct wmi_set_current_country_arg current_cc = {};
15055 
15056 			memcpy(&current_cc.alpha2, ab->new_alpha2, 2);
15057 			memcpy(&ar->alpha2, ab->new_alpha2, 2);
15058 
15059 			reinit_completion(&ar->regd_update_completed);
15060 
15061 			ret = ath12k_wmi_send_set_current_country_cmd(ar, &current_cc);
15062 			if (ret)
15063 				ath12k_warn(ar->ab,
15064 					    "failed set cc code for mac register: %d\n",
15065 					    ret);
15066 		}
15067 
15068 		ath12k_fw_stats_init(ar);
15069 		ath12k_debugfs_register(ar);
15070 	}
15071 
15072 	return 0;
15073 
15074 err_unregister_hw:
15075 	for_each_ar(ah, ar, i)
15076 		ath12k_debugfs_unregister(ar);
15077 
15078 	ieee80211_unregister_hw(hw);
15079 
15080 err_cleanup_if_combs:
15081 	ath12k_mac_cleanup_iface_combinations(ah);
15082 
15083 err_complete_cleanup_unregister:
15084 	i = ah->num_radio;
15085 
15086 err_cleanup_unregister:
15087 	for (j = 0; j < i; j++) {
15088 		ar = ath12k_ah_to_ar(ah, j);
15089 		ath12k_mac_cleanup_unregister(ar);
15090 	}
15091 
15092 	SET_IEEE80211_DEV(hw, NULL);
15093 
15094 	return ret;
15095 }
15096 
ath12k_mac_setup(struct ath12k * ar)15097 static void ath12k_mac_setup(struct ath12k *ar)
15098 {
15099 	struct ath12k_base *ab = ar->ab;
15100 	struct ath12k_pdev *pdev = ar->pdev;
15101 	u8 pdev_idx = ar->pdev_idx;
15102 
15103 	ar->lmac_id = ath12k_hw_get_mac_from_pdev_id(ab->hw_params, pdev_idx);
15104 
15105 	ar->wmi = &ab->wmi_ab.wmi[pdev_idx];
15106 	/* FIXME: wmi[0] is already initialized during attach,
15107 	 * Should we do this again?
15108 	 */
15109 	ath12k_wmi_pdev_attach(ab, pdev_idx);
15110 
15111 	ar->cfg_tx_chainmask = pdev->cap.tx_chain_mask;
15112 	ar->cfg_rx_chainmask = pdev->cap.rx_chain_mask;
15113 	ar->num_tx_chains = hweight32(pdev->cap.tx_chain_mask);
15114 	ar->num_rx_chains = hweight32(pdev->cap.rx_chain_mask);
15115 	ar->scan.arvif = NULL;
15116 	ar->vdev_id_11d_scan = ATH12K_11D_INVALID_VDEV_ID;
15117 
15118 	spin_lock_init(&ar->data_lock);
15119 	spin_lock_init(&ar->dp.ppdu_list_lock);
15120 	INIT_LIST_HEAD(&ar->arvifs);
15121 	INIT_LIST_HEAD(&ar->dp.ppdu_stats_info);
15122 	INIT_LIST_HEAD(&ar->peer_delete_waits);
15123 
15124 	init_completion(&ar->vdev_setup_done);
15125 	init_completion(&ar->vdev_delete_done);
15126 	init_completion(&ar->peer_assoc_done);
15127 	init_completion(&ar->install_key_done);
15128 	init_completion(&ar->bss_survey_done);
15129 	init_completion(&ar->scan.started);
15130 	init_completion(&ar->scan.completed);
15131 	init_completion(&ar->scan.on_channel);
15132 	init_completion(&ar->mlo_setup_done);
15133 	init_completion(&ar->completed_11d_scan);
15134 	init_completion(&ar->regd_update_completed);
15135 	init_completion(&ar->thermal.wmi_sync);
15136 	mutex_init(&ar->thermal.lock);
15137 
15138 	ar->thermal.temperature = 0;
15139 	ar->thermal.hwmon_dev = NULL;
15140 
15141 	INIT_DELAYED_WORK(&ar->scan.timeout, ath12k_scan_timeout_work);
15142 	wiphy_work_init(&ar->scan.vdev_clean_wk, ath12k_scan_vdev_clean_work);
15143 	INIT_WORK(&ar->regd_channel_update_work, ath12k_regd_update_chan_list_work);
15144 	INIT_LIST_HEAD(&ar->regd_channel_update_queue);
15145 	INIT_WORK(&ar->regd_update_work, ath12k_regd_update_work);
15146 
15147 	wiphy_work_init(&ar->wmi_mgmt_tx_work, ath12k_mgmt_over_wmi_tx_work);
15148 	skb_queue_head_init(&ar->wmi_mgmt_tx_queue);
15149 
15150 	ar->monitor_vdev_id = -1;
15151 	ar->monitor_vdev_created = false;
15152 	ar->monitor_started = false;
15153 }
15154 
__ath12k_mac_mlo_setup(struct ath12k * ar)15155 static int __ath12k_mac_mlo_setup(struct ath12k *ar)
15156 {
15157 	u8 num_link = 0, partner_link_id[ATH12K_GROUP_MAX_RADIO] = {};
15158 	struct ath12k_base *partner_ab, *ab = ar->ab;
15159 	struct ath12k_hw_group *ag = ab->ag;
15160 	struct wmi_mlo_setup_arg mlo = {};
15161 	struct ath12k_pdev *pdev;
15162 	unsigned long time_left;
15163 	int i, j, ret;
15164 
15165 	lockdep_assert_held(&ag->mutex);
15166 
15167 	reinit_completion(&ar->mlo_setup_done);
15168 
15169 	for (i = 0; i < ag->num_devices; i++) {
15170 		partner_ab = ag->ab[i];
15171 
15172 		for (j = 0; j < partner_ab->num_radios; j++) {
15173 			pdev = &partner_ab->pdevs[j];
15174 
15175 			/* Avoid the self link */
15176 			if (ar == pdev->ar)
15177 				continue;
15178 
15179 			partner_link_id[num_link] = pdev->hw_link_id;
15180 			num_link++;
15181 
15182 			ath12k_dbg(ab, ATH12K_DBG_MAC, "device %d pdev %d hw_link_id %d num_link %d\n",
15183 				   i, j, pdev->hw_link_id, num_link);
15184 		}
15185 	}
15186 
15187 	if (num_link == 0)
15188 		return 0;
15189 
15190 	mlo.group_id = cpu_to_le32(ag->id);
15191 	mlo.partner_link_id = partner_link_id;
15192 	mlo.num_partner_links = num_link;
15193 	ar->mlo_setup_status = 0;
15194 
15195 	ath12k_dbg(ab, ATH12K_DBG_MAC, "group id %d num_link %d\n", ag->id, num_link);
15196 
15197 	ret = ath12k_wmi_mlo_setup(ar, &mlo);
15198 	if (ret) {
15199 		ath12k_err(ab, "failed to send  setup MLO WMI command for pdev %d: %d\n",
15200 			   ar->pdev_idx, ret);
15201 		return ret;
15202 	}
15203 
15204 	time_left = wait_for_completion_timeout(&ar->mlo_setup_done,
15205 						WMI_MLO_CMD_TIMEOUT_HZ);
15206 
15207 	if (!time_left || ar->mlo_setup_status)
15208 		return ar->mlo_setup_status ? : -ETIMEDOUT;
15209 
15210 	ath12k_dbg(ab, ATH12K_DBG_MAC, "mlo setup done for pdev %d\n", ar->pdev_idx);
15211 
15212 	return 0;
15213 }
15214 
__ath12k_mac_mlo_teardown(struct ath12k * ar)15215 static int __ath12k_mac_mlo_teardown(struct ath12k *ar)
15216 {
15217 	struct ath12k_base *ab = ar->ab;
15218 	int ret;
15219 	u8 num_link;
15220 
15221 	if (test_bit(ATH12K_FLAG_RECOVERY, &ab->dev_flags))
15222 		return 0;
15223 
15224 	num_link = ath12k_get_num_partner_link(ar);
15225 
15226 	if (num_link == 0)
15227 		return 0;
15228 
15229 	ret = ath12k_wmi_mlo_teardown(ar);
15230 	if (ret) {
15231 		ath12k_warn(ab, "failed to send MLO teardown WMI command for pdev %d: %d\n",
15232 			    ar->pdev_idx, ret);
15233 		return ret;
15234 	}
15235 
15236 	ath12k_dbg(ab, ATH12K_DBG_MAC, "mlo teardown for pdev %d\n", ar->pdev_idx);
15237 
15238 	return 0;
15239 }
15240 
ath12k_mac_mlo_setup(struct ath12k_hw_group * ag)15241 int ath12k_mac_mlo_setup(struct ath12k_hw_group *ag)
15242 {
15243 	struct ath12k_hw *ah;
15244 	struct ath12k *ar;
15245 	int ret;
15246 	int i, j;
15247 
15248 	for (i = 0; i < ag->num_hw; i++) {
15249 		ah = ag->ah[i];
15250 		if (!ah)
15251 			continue;
15252 
15253 		for_each_ar(ah, ar, j) {
15254 			ar = &ah->radio[j];
15255 			ret = __ath12k_mac_mlo_setup(ar);
15256 			if (ret) {
15257 				ath12k_err(ar->ab, "failed to setup MLO: %d\n", ret);
15258 				goto err_setup;
15259 			}
15260 		}
15261 	}
15262 
15263 	return 0;
15264 
15265 err_setup:
15266 	for (i = i - 1; i >= 0; i--) {
15267 		ah = ag->ah[i];
15268 		if (!ah)
15269 			continue;
15270 
15271 		for (j = j - 1; j >= 0; j--) {
15272 			ar = &ah->radio[j];
15273 			if (!ar)
15274 				continue;
15275 
15276 			__ath12k_mac_mlo_teardown(ar);
15277 		}
15278 	}
15279 
15280 	return ret;
15281 }
15282 
ath12k_mac_mlo_teardown(struct ath12k_hw_group * ag)15283 void ath12k_mac_mlo_teardown(struct ath12k_hw_group *ag)
15284 {
15285 	struct ath12k_hw *ah;
15286 	struct ath12k *ar;
15287 	int ret, i, j;
15288 
15289 	for (i = 0; i < ag->num_hw; i++) {
15290 		ah = ag->ah[i];
15291 		if (!ah)
15292 			continue;
15293 
15294 		for_each_ar(ah, ar, j) {
15295 			ar = &ah->radio[j];
15296 			ret = __ath12k_mac_mlo_teardown(ar);
15297 			if (ret) {
15298 				ath12k_err(ar->ab, "failed to teardown MLO: %d\n", ret);
15299 				break;
15300 			}
15301 		}
15302 	}
15303 }
15304 
ath12k_mac_register(struct ath12k_hw_group * ag)15305 int ath12k_mac_register(struct ath12k_hw_group *ag)
15306 {
15307 	struct ath12k_hw *ah;
15308 	int i;
15309 	int ret;
15310 
15311 	for (i = 0; i < ag->num_hw; i++) {
15312 		ah = ath12k_ag_to_ah(ag, i);
15313 
15314 		ret = ath12k_mac_hw_register(ah);
15315 		if (ret)
15316 			goto err;
15317 	}
15318 
15319 	return 0;
15320 
15321 err:
15322 	for (i = i - 1; i >= 0; i--) {
15323 		ah = ath12k_ag_to_ah(ag, i);
15324 		if (!ah)
15325 			continue;
15326 
15327 		ath12k_mac_hw_unregister(ah);
15328 	}
15329 
15330 	return ret;
15331 }
15332 
ath12k_mac_unregister(struct ath12k_hw_group * ag)15333 void ath12k_mac_unregister(struct ath12k_hw_group *ag)
15334 {
15335 	struct ath12k_hw *ah;
15336 	int i;
15337 
15338 	for (i = ag->num_hw - 1; i >= 0; i--) {
15339 		ah = ath12k_ag_to_ah(ag, i);
15340 		if (!ah)
15341 			continue;
15342 
15343 		ath12k_mac_hw_unregister(ah);
15344 	}
15345 }
15346 
ath12k_mac_hw_destroy(struct ath12k_hw * ah)15347 static void ath12k_mac_hw_destroy(struct ath12k_hw *ah)
15348 {
15349 	ieee80211_free_hw(ah->hw);
15350 }
15351 
ath12k_mac_hw_allocate(struct ath12k_hw_group * ag,struct ath12k_pdev_map * pdev_map,u8 num_pdev_map)15352 static struct ath12k_hw *ath12k_mac_hw_allocate(struct ath12k_hw_group *ag,
15353 						struct ath12k_pdev_map *pdev_map,
15354 						u8 num_pdev_map)
15355 {
15356 	struct ieee80211_hw *hw;
15357 	struct ath12k *ar;
15358 	struct ath12k_base *ab;
15359 	struct ath12k_pdev *pdev;
15360 	struct ath12k_hw *ah;
15361 	int i;
15362 	u8 pdev_idx;
15363 
15364 	hw = ieee80211_alloc_hw(struct_size(ah, radio, num_pdev_map),
15365 				pdev_map->ab->ath12k_ops);
15366 	if (!hw)
15367 		return NULL;
15368 
15369 	ah = ath12k_hw_to_ah(hw);
15370 	ah->hw = hw;
15371 	ah->num_radio = num_pdev_map;
15372 
15373 	mutex_init(&ah->hw_mutex);
15374 	init_completion(&ah->peer_ml_id_done);
15375 
15376 	spin_lock_init(&ah->survey_lock);
15377 	spin_lock_init(&ah->dp_hw.peer_lock);
15378 	INIT_LIST_HEAD(&ah->dp_hw.dp_peers_list);
15379 
15380 	for (i = 0; i < num_pdev_map; i++) {
15381 		ab = pdev_map[i].ab;
15382 		pdev_idx = pdev_map[i].pdev_idx;
15383 		pdev = &ab->pdevs[pdev_idx];
15384 
15385 		ar = ath12k_ah_to_ar(ah, i);
15386 		ar->ah = ah;
15387 		ar->ab = ab;
15388 		ar->hw_link_id = pdev->hw_link_id;
15389 		ar->pdev = pdev;
15390 		ar->pdev_idx = pdev_idx;
15391 		ar->radio_idx = i;
15392 		pdev->ar = ar;
15393 
15394 		ag->hw_links[ar->hw_link_id].device_id = ab->device_id;
15395 		ag->hw_links[ar->hw_link_id].pdev_idx = pdev_idx;
15396 
15397 		ath12k_mac_setup(ar);
15398 		ath12k_dp_pdev_pre_alloc(ar);
15399 	}
15400 
15401 	return ah;
15402 }
15403 
ath12k_mac_destroy(struct ath12k_hw_group * ag)15404 void ath12k_mac_destroy(struct ath12k_hw_group *ag)
15405 {
15406 	struct ath12k_pdev *pdev;
15407 	struct ath12k_base *ab = ag->ab[0];
15408 	int i, j;
15409 	struct ath12k_hw *ah;
15410 
15411 	for (i = 0; i < ag->num_devices; i++) {
15412 		ab = ag->ab[i];
15413 		if (!ab)
15414 			continue;
15415 
15416 		for (j = 0; j < ab->num_radios; j++) {
15417 			pdev = &ab->pdevs[j];
15418 			if (!pdev->ar)
15419 				continue;
15420 			pdev->ar = NULL;
15421 		}
15422 	}
15423 
15424 	for (i = 0; i < ag->num_hw; i++) {
15425 		ah = ath12k_ag_to_ah(ag, i);
15426 		if (!ah)
15427 			continue;
15428 
15429 		ath12k_mac_hw_destroy(ah);
15430 		ath12k_ag_set_ah(ag, i, NULL);
15431 	}
15432 }
15433 
ath12k_mac_set_device_defaults(struct ath12k_base * ab)15434 static void ath12k_mac_set_device_defaults(struct ath12k_base *ab)
15435 {
15436 	int total_vdev;
15437 
15438 	/* Initialize channel counters frequency value in hertz */
15439 	ab->cc_freq_hz = 320000;
15440 	total_vdev = ab->num_radios * TARGET_NUM_VDEVS(ab);
15441 	ab->free_vdev_map = (1LL << total_vdev) - 1;
15442 }
15443 
ath12k_mac_allocate(struct ath12k_hw_group * ag)15444 int ath12k_mac_allocate(struct ath12k_hw_group *ag)
15445 {
15446 	struct ath12k_pdev_map pdev_map[ATH12K_GROUP_MAX_RADIO];
15447 	int mac_id, device_id, total_radio, num_hw;
15448 	struct ath12k_base *ab;
15449 	struct ath12k_hw *ah;
15450 	bool conf = false;
15451 	u8 radio_per_hw;
15452 	int ret, i, j;
15453 
15454 	total_radio = 0;
15455 	for (i = 0; i < ag->num_devices; i++) {
15456 		ab = ag->ab[i];
15457 		if (!ab)
15458 			continue;
15459 
15460 		ath12k_mac_set_device_defaults(ab);
15461 		total_radio += ab->num_radios;
15462 	}
15463 
15464 	if (!total_radio)
15465 		return -EINVAL;
15466 
15467 	if (WARN_ON(total_radio > ATH12K_GROUP_MAX_RADIO))
15468 		return -ENOSPC;
15469 
15470 	/* All pdev get combined and register as single wiphy based on
15471 	 * hardware group which participate in multi-link operation else
15472 	 * each pdev get register separately.
15473 	 */
15474 	if (ag->mlo_capable)
15475 		radio_per_hw = total_radio;
15476 	else
15477 		radio_per_hw = 1;
15478 
15479 	num_hw = total_radio / radio_per_hw;
15480 
15481 	ag->num_hw = 0;
15482 	device_id = 0;
15483 	mac_id = 0;
15484 	for (i = 0; i < num_hw; i++) {
15485 		for (j = 0; j < radio_per_hw; j++) {
15486 			if (device_id >= ag->num_devices || !ag->ab[device_id]) {
15487 				ret = -ENOSPC;
15488 				goto err;
15489 			}
15490 
15491 			ab = ag->ab[device_id];
15492 
15493 			/*
15494 			 * the assumption is all devices within an ah
15495 			 * share the same host_alloc_ml_id configuration
15496 			 */
15497 			if (j == 0) {
15498 				conf = ab->hw_params->host_alloc_ml_id;
15499 			} else if (conf != ab->hw_params->host_alloc_ml_id) {
15500 				ath12k_warn(ab, "inconsistent ML ID config within ah, device 0 uses %s allocated ID, while device %u doesn't\n",
15501 					    conf ? "host" : "firmware", device_id);
15502 				ret = -EINVAL;
15503 				goto err;
15504 			}
15505 
15506 			pdev_map[j].ab = ab;
15507 			pdev_map[j].pdev_idx = mac_id;
15508 			mac_id++;
15509 
15510 			/* If mac_id falls beyond the current device MACs then
15511 			 * move to next device
15512 			 */
15513 			if (mac_id >= ab->num_radios) {
15514 				mac_id = 0;
15515 				device_id++;
15516 			}
15517 		}
15518 
15519 		ab = pdev_map->ab;
15520 
15521 		ah = ath12k_mac_hw_allocate(ag, pdev_map, radio_per_hw);
15522 		if (!ah) {
15523 			ath12k_warn(ab, "failed to allocate mac80211 hw device for hw_idx %d\n",
15524 				    i);
15525 			ret = -ENOMEM;
15526 			goto err;
15527 		}
15528 
15529 		ah->dev = ab->dev;
15530 		ah->host_alloc_ml_id = conf;
15531 
15532 		ag->ah[i] = ah;
15533 		ag->num_hw++;
15534 	}
15535 
15536 	return 0;
15537 
15538 err:
15539 	for (i = i - 1; i >= 0; i--) {
15540 		ah = ath12k_ag_to_ah(ag, i);
15541 		if (!ah)
15542 			continue;
15543 
15544 		ath12k_mac_hw_destroy(ah);
15545 		ath12k_ag_set_ah(ag, i, NULL);
15546 	}
15547 
15548 	return ret;
15549 }
15550 
ath12k_mac_vif_set_keepalive(struct ath12k_link_vif * arvif,enum wmi_sta_keepalive_method method,u32 interval)15551 int ath12k_mac_vif_set_keepalive(struct ath12k_link_vif *arvif,
15552 				 enum wmi_sta_keepalive_method method,
15553 				 u32 interval)
15554 {
15555 	struct wmi_sta_keepalive_arg arg = {};
15556 	struct ath12k *ar = arvif->ar;
15557 	int ret;
15558 
15559 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
15560 
15561 	if (arvif->ahvif->vdev_type != WMI_VDEV_TYPE_STA)
15562 		return 0;
15563 
15564 	if (!test_bit(WMI_TLV_SERVICE_STA_KEEP_ALIVE, ar->ab->wmi_ab.svc_map))
15565 		return 0;
15566 
15567 	arg.vdev_id = arvif->vdev_id;
15568 	arg.enabled = 1;
15569 	arg.method = method;
15570 	arg.interval = interval;
15571 
15572 	ret = ath12k_wmi_sta_keepalive(ar, &arg);
15573 	if (ret) {
15574 		ath12k_warn(ar->ab, "failed to set keepalive on vdev %i: %d\n",
15575 			    arvif->vdev_id, ret);
15576 		return ret;
15577 	}
15578 
15579 	return 0;
15580 }
15581